summaryrefslogtreecommitdiff
path: root/drivers/misc/viatel/power.c
blob: 0fc22cb3bd8eae175379529ee23ed383973a594f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * viatel_cbp_power.c
 *
 * VIA CBP driver for Linux
 *
 * Copyright (C) 2009 VIA TELECOM Corporation, Inc.
 * Author: VIA TELECOM Corporation, Inc.
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <linux/ctype.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/irq.h>
#include <linux/wakelock.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <mach/viatel.h>
#include "core.h"

#define MDM_RST_LOCK_TIME   (120) 
#define MDM_RST_HOLD_DELAY  (100) //ms
#define MDM_PWR_HOLD_DELAY  (500) //ms

//#define VIA_AP_MODEM_DEBU
#ifdef VIA_AP_MODEM_DEBUG
#undef dbg
#define dbg(format, arg...) printk("[CBP_POWER]: " format "\n" , ## arg)
#else
#undef dbg
#define dbg(format, arg...) do {} while (0)
#endif

static struct wake_lock reset_lock;

void oem_power_off_modem(void)
{
    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_PWR_EN)){
        oem_gpio_direction_output(GPIO_VIATEL_MDM_PWR_EN, 0);
    }

    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_RST)){
        oem_gpio_direction_output(GPIO_VIATEL_MDM_RST, 1);
        /*just hold the reset pin if no power enable pin*/
        if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_PWR_EN)){
            mdelay(MDM_RST_HOLD_DELAY);
            oem_gpio_direction_output(GPIO_VIATEL_MDM_RST, 0);
        }
    }
}
EXPORT_SYMBOL(oem_power_off_modem);

ssize_t modem_power_show(
	struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
    int power = 0;
    int ret = 0;

    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_PWR_IND)){
        power = !!oem_gpio_get_value(GPIO_VIATEL_MDM_PWR_IND);
    }else if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_PWR_EN)){
        printk("No MDM_PWR_IND, just detect MDM_PWR_EN\n");
        power = !!oem_gpio_get_value(GPIO_VIATEL_MDM_PWR_EN);
    }else if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_RST)){
        printk("No MDM_PWR_IND, just detect MDM_PWR_RST\n");
        power = !!oem_gpio_get_value(GPIO_VIATEL_MDM_RST);
    }
    if(power){
        ret += sprintf(buf + ret, "on\n");
    }else{
        ret += sprintf(buf + ret, "off\n");
    }
    
    return ret;
}

ssize_t modem_power_store(
	struct kobject *kobj, struct kobj_attribute *attr,
	const char *buf, size_t n)
{
    int power;

    /* power the modem */
    if ( !strncmp(buf, "on", strlen("on"))) {
        power = 1;
    }else if(!strncmp(buf, "off", strlen("off"))){
        power = 0;
    }else{
        printk("%s: input %s is invalid.\n", __FUNCTION__, buf);
        return n;
    }

    printk("Warnning: Power %s modem\n", power ? "on" : "off");
    if(power){
#if 0
        if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_RST)){
            oem_gpio_direction_output(GPIO_VIATEL_MDM_RST, 1);
            mdelay(MDM_RST_HOLD_DELAY);
        }
        if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_PWR_EN)){
            oem_gpio_direction_output(GPIO_VIATEL_MDM_PWR_EN, 0);
            mdelay(MDM_PWR_HOLD_DELAY);
        }

        if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_PWR_EN)){
            oem_gpio_direction_output(GPIO_VIATEL_MDM_PWR_EN, 1);
            mdelay(MDM_PWR_HOLD_DELAY);
        }
        
        if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_RST)){
            oem_gpio_direction_output(GPIO_VIATEL_MDM_RST, 0);
            mdelay(MDM_RST_HOLD_DELAY);
        }
        
        if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_PWR_EN)){
            oem_gpio_direction_output(GPIO_VIATEL_MDM_PWR_EN, 0);
        }
#endif

        if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_PWR_EN)){
	    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_RST)){
                oem_gpio_direction_output(GPIO_VIATEL_MDM_RST, 0);
                mdelay(MDM_RST_HOLD_DELAY);
            }
            oem_gpio_direction_output(GPIO_VIATEL_MDM_PWR_EN, 1);
            mdelay(MDM_PWR_HOLD_DELAY);
        }
    }else{
        oem_power_off_modem();
    }
    return n;
}

ssize_t modem_reset_show(
	struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
    int reset = 0;
    int ret = 0;
    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_RST_IND)){
        reset = !!oem_gpio_get_value(GPIO_VIATEL_MDM_RST_IND);
    }else if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_RST)){
        reset = !!oem_gpio_get_value(GPIO_VIATEL_MDM_RST);
    }
    
    if(reset){
        ret += sprintf(buf + ret, "reset\n");
    }else{
        ret += sprintf(buf + ret, "work\n");
    }

    return ret;
}

ssize_t modem_reset_store(
	struct kobject *kobj, struct kobj_attribute *attr,
	const char *buf, size_t n)
{
    /* reset the modem */
    if ( !strncmp(buf, "reset", strlen("reset"))) {
         wake_lock_timeout(&reset_lock, MDM_RST_LOCK_TIME * HZ);
         if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_RST)){
            oem_gpio_direction_output(GPIO_VIATEL_MDM_RST, 1);
            mdelay(MDM_RST_HOLD_DELAY);
            oem_gpio_direction_output(GPIO_VIATEL_MDM_RST, 0);
            mdelay(MDM_RST_HOLD_DELAY);
         }
         printk("Warnning: reset modem\n");
    }
    
    return n;
}

ssize_t modem_ets_select_show(
	struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
    int level = 0;
    int ret = 0;
    
    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_ETS_SEL)){
        level = !!oem_gpio_get_value(GPIO_VIATEL_MDM_ETS_SEL);
    }

    ret += sprintf(buf, "%d\n", level);
    return ret;
}

ssize_t modem_ets_select_store(
	struct kobject *kobj, struct kobj_attribute *attr,
	const char *buf, size_t n)
{
    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_ETS_SEL)){
        if ( !strncmp(buf, "1", strlen("1"))) {
            oem_gpio_direction_output(GPIO_VIATEL_MDM_ETS_SEL, 1);
        }else if( !strncmp(buf, "0", strlen("0"))){
            oem_gpio_direction_output(GPIO_VIATEL_MDM_ETS_SEL, 0);
        }else{
            dbg("Unknow command.\n");
        }
    }

    return n;
}

ssize_t modem_boot_select_show(
	struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
    int ret = 0;
    int level = 0;
    
    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_BOOT_SEL)){
        level = !!oem_gpio_get_value(GPIO_VIATEL_MDM_BOOT_SEL);
    }

    ret += sprintf(buf, "%d\n", level);
    return ret;
}

ssize_t modem_boot_select_store(
	struct kobject *kobj, struct kobj_attribute *attr,
	const char *buf, size_t n)
{
    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_BOOT_SEL)){
        if ( !strncmp(buf, "1", strlen("1"))) {
            oem_gpio_direction_output(GPIO_VIATEL_MDM_BOOT_SEL, 1);
        }else if( !strncmp(buf, "0", strlen("0"))){
            oem_gpio_direction_output(GPIO_VIATEL_MDM_BOOT_SEL, 0);
        }else{
            dbg("Unknow command.\n");
        }
    }

    return n;
}

#define modem_attr(_name) \
static struct kobj_attribute _name##_attr = { \
    .attr = {                        \
        .name = __stringify(_name),   \
        .mode = 0644,                \
    },                               \
    .show   = modem_##_name##_show,  \
    .store  = modem_##_name##_store, \
}

modem_attr(power);
modem_attr(reset);
modem_attr(ets_select);
modem_attr(boot_select);

static struct attribute *g_attr[] = {
    &power_attr.attr,
    &reset_attr.attr,
    &ets_select_attr.attr,
    &boot_select_attr.attr,
    NULL
};

static struct attribute_group g_attr_group = {
    .attrs = g_attr,
};


static void modem_shutdown(struct platform_device *dev)
{
    oem_power_off_modem();
}

static struct platform_driver power_driver = {
	.driver.name = "modem_power",
    .shutdown = modem_shutdown,
};

static struct platform_device power_device = {
	.name = "modem_power",
};

static struct kobject *modem_kobj;
static int __init viatel_power_init(void)
{
    int ret = 0;

    modem_kobj = viatel_kobject_add("modem");
    if(!modem_kobj){
        ret = -ENOMEM;
        goto err_create_kobj;
    }

    ret = platform_device_register(&power_device);
    if (ret) {
        printk("platform_device_register failed\n");
        goto err_platform_device_register;
    }
    ret = platform_driver_register(&power_driver);
    if (ret) {
        printk("platform_driver_register failed\n");
        goto err_platform_driver_register;
    }
    
    wake_lock_init(&reset_lock, WAKE_LOCK_SUSPEND, "cbp_rst");
    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_PWR_IND)){
        oem_gpio_direction_input_for_irq(GPIO_VIATEL_MDM_PWR_IND);
    }
    if(GPIO_OEM_VALID(GPIO_VIATEL_MDM_RST_IND)){
        oem_gpio_direction_input_for_irq(GPIO_VIATEL_MDM_RST_IND);
    }

    //oem_gpio_direction_output(GPIO_VIATEL_MDM_RST, 0);
    //oem_gpio_direction_output(GPIO_VIATEL_MDM_PWR_EN, 1);
    ret = sysfs_create_group(modem_kobj, &g_attr_group);

    if(ret){
        printk("sysfs_create_group failed\n");
        goto err_sysfs_create_group; 
    }

    return 0;
err_sysfs_create_group:
    platform_driver_unregister(&power_driver);
err_platform_driver_register:
	platform_device_unregister(&power_device);
err_platform_device_register:
err_create_kobj:
    return ret;
}

static void  __exit viatel_power_exit(void)
{
    wake_lock_destroy(&reset_lock);
}

late_initcall_sync(viatel_power_init);
module_exit(viatel_power_exit);