RK29SmartPhoneSDK:mtk23d bb wakeup use Bpstatus GPIO
[firefly-linux-kernel-4.4.55.git] / drivers / misc / mtk23d.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/i2c.h>
4 #include <linux/irq.h>
5 #include <linux/gpio.h>
6 #include <linux/input.h>
7 #include <linux/platform_device.h>
8 #include <linux/fs.h>
9 #include <linux/uaccess.h>
10 #include <linux/miscdevice.h>
11 #include <linux/circ_buf.h>
12 #include <linux/interrupt.h>
13 #include <linux/miscdevice.h>
14 #include <mach/iomux.h>
15 #include <mach/gpio.h>
16 //#include <mach/spi_fpga.h>
17 #include <linux/delay.h>
18 #include <linux/poll.h>
19 #include <linux/wait.h>
20 //#include <linux/android_power.h>
21 //#include <asm/arch/gpio_extend.h>
22 #include <linux/workqueue.h>
23 #include <linux/mtk23d.h>
24 #include <linux/wakelock.h>
25
26 MODULE_LICENSE("GPL");
27
28 #define DEBUG
29 #ifdef DEBUG
30 #define MODEMDBG(x...) printk(x)
31 #else
32 #define MODEMDBG(fmt,argss...)
33 #endif
34
35 #define MTK23D_RESET 0x01
36 #define MTK23D_POWERON  0x02
37 #define MTK23D_POWER_HIGH 0x03
38 #define MTK23D_IMEI_READ  0x04
39 //#define BP_POW_EN     TCA6424_P02
40 //#define BP_STATUS    RK2818_PIN_PH7    //input  high bp sleep
41 //#define AP_STATUS    RK2818_PIN_PA4    //output high ap sleep
42
43 //#define BP_RESET      TCA6424_P11     //Ryan
44
45 //#define AP_BP_WAKEUP  RK2818_PIN_PF5   //output AP wake up BP used rising edge
46 //#define BP_AP_WAKEUP  RK2818_PIN_PE0  //input BP wake up AP
47
48 static bool wakelock_inited;
49 static struct wake_lock mtk23d_wakelock;
50
51 #define SLEEP 1
52 #define READY 0
53
54 //struct modem_dev *mt6223d_data = NULL;
55 struct rk2818_23d_data *gpdata = NULL;
56
57 static int  get_bp_statue(struct platform_device *pdev)
58 {
59         struct rk2818_23d_data *pdata = pdev->dev.platform_data;
60         
61         if(gpio_get_value(pdata->bp_statue))
62                 return SLEEP;
63         else
64                 return READY;
65 }
66 static void ap_sleep(struct platform_device *pdev)
67 {
68         struct rk2818_23d_data *pdata = pdev->dev.platform_data;
69         
70         MODEMDBG("ap sleep!\n");
71         gpio_set_value(pdata->ap_statue,GPIO_HIGH);
72 }
73 static void ap_wakeup(struct platform_device *pdev)
74 {
75         struct rk2818_23d_data *pdata = pdev->dev.platform_data;
76         
77         MODEMDBG("ap wakeup!\n");
78         gpio_set_value(pdata->ap_statue,GPIO_LOW);
79 }
80 /* */
81 static void ap_wakeup_bp(struct platform_device *pdev, int wake)//low to wakeup bp
82 {
83         struct rk2818_23d_data *pdata = pdev->dev.platform_data;
84         struct modem_dev *mt6223d_data = platform_get_drvdata(pdev);
85         MODEMDBG("ap_wakeup_bp\n");
86
87         gpio_set_value(pdata->ap_bp_wakeup, wake);  // phc
88         //gpio_set_value(RK2818_PIN_PF5, wake);
89 }
90
91 static void bpwakeup_work_func_work(struct work_struct *work)
92 {
93         struct modem_dev *bdata = container_of(work, struct modem_dev, work);
94         
95         MODEMDBG("%s\n", __FUNCTION__);
96         
97 }
98 /*  */
99 static irqreturn_t  bpwakeup_work_func(int irq, void *data)
100 {
101         struct modem_dev *mt6223d_data = (struct modem_dev *)data;
102         
103         MODEMDBG("bpwakeup_work_func\n");
104         schedule_work(&mt6223d_data->work);
105         return IRQ_HANDLED;
106 }
107 static irqreturn_t  bp_apwakeup_work_func(int irq, void *data)
108 {
109         //struct modem_dev *dev = &mtk23d_misc;
110         
111         MODEMDBG("bp_apwakeup_work_func\n");
112         //wake_up_interruptible(&dev->wakeup);
113         return IRQ_HANDLED;
114 }
115 int modem_poweron_off(int on_off)
116 {
117         struct rk2818_23d_data *pdata = gpdata;
118         
119   if(on_off)
120   {
121          printk("modem_poweron\n");
122          gpio_set_value(gpdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH);
123          msleep(4000);
124          gpio_set_value(gpdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
125   }
126   else
127   {
128          printk("modem_poweroff\n");
129          gpio_set_value(gpdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH);
130          mdelay(100);
131          gpio_set_value(gpdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
132   }
133 }
134 static int power_on =1;
135 static int mtk23d_open(struct inode *inode, struct file *file)
136 {
137         struct rk2818_23d_data *pdata = gpdata;
138         //struct rk2818_23d_data *pdata = gpdata = pdev->dev.platform_data;
139         struct platform_data *pdev = container_of(pdata, struct device, platform_data);
140
141         MODEMDBG("modem_open\n");
142
143         int ret = 0;
144         if(power_on)
145         {
146                 power_on = 0;
147                 modem_poweron_off(1);
148                 #if 1 // phc
149                 rk29_mux_api_set(GPIO1B7_UART0SOUT_NAME, GPIO1L_UART0_SOUT);
150                 rk29_mux_api_set(GPIO1B6_UART0SIN_NAME, GPIO1L_UART0_SIN); 
151                 rk29_mux_api_set(GPIO1C1_UART0RTSN_SDMMC1WRITEPRT_NAME, GPIO1H_UART0_RTS_N);
152                 rk29_mux_api_set(GPIO1C0_UART0CTSN_SDMMC1DETECTN_NAME, GPIO1H_UART0_CTS_N);     
153                 #endif
154         }
155         device_init_wakeup(&pdev, 1);
156
157         return 0;
158 }
159
160 static int mtk23d_release(struct inode *inode, struct file *file)
161 {
162         MODEMDBG("mtk23d_release\n");
163
164         //gpio_free(pdata->bp_power);
165         return 0;
166 }
167
168 //extern char imei_value[16]; // phc, no find 'imei_value' in rk29 project
169 char imei_value[16] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5};
170
171 static int mtk23d_ioctl(struct inode *inode,struct file *file, unsigned int cmd, unsigned long arg)
172 {
173         struct rk2818_23d_data *pdata = gpdata;
174         int i;
175         void __user *argp = (void __user *)arg;
176         printk("mtk23d_ioctl\n");
177         switch(cmd)
178         {
179                 case MTK23D_RESET:
180                         gpio_set_value(gpdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH);
181                         mdelay(100);
182                         gpio_direction_output(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_LOW:GPIO_HIGH);
183                         mdelay(100);
184                         gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
185                         msleep(4000);
186                         gpio_set_value(gpdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
187                         break;
188                 case MTK23D_IMEI_READ:
189                         if(copy_to_user(argp, &(imei_value[0]), 16))
190                         {
191                                 printk("ERROR: copy_to_user---%s\n", __FUNCTION__);
192                                 return -EFAULT;
193                         }
194                         break;
195                 default:
196                         break;
197         }
198         return 0;
199 }
200
201 static struct file_operations mtk23d_fops = {
202         .owner = THIS_MODULE,
203         .open = mtk23d_open,
204         .release = mtk23d_release,
205         .ioctl = mtk23d_ioctl
206 };
207
208 static struct miscdevice mtk23d_misc = {
209         .minor = MISC_DYNAMIC_MINOR,
210         .name = MODEM_NAME,
211         .fops = &mtk23d_fops
212 };
213
214 static int mtk23d_probe(struct platform_device *pdev)
215 {
216         struct rk2818_23d_data *pdata = gpdata = pdev->dev.platform_data;
217         struct modem_dev *mt6223d_data = NULL;
218         int result, irq = 0;    
219         
220         MODEMDBG("mtk23d_probe\n");
221
222 #if 1   
223         rk29_mux_api_set(GPIO1B7_UART0SOUT_NAME, GPIO1L_GPIO1B7);                       
224         gpio_request(RK29_PIN1_PB7, NULL);
225         gpio_direction_output(RK29_PIN1_PB7,GPIO_LOW);
226         gpio_pull_updown(RK29_PIN1_PB7, PullDisable);  // ÏÂÀ­½ûÖ¹
227         
228         rk29_mux_api_set(GPIO1B6_UART0SIN_NAME, GPIO1L_GPIO1B6);                
229         gpio_request(RK29_PIN1_PB6, NULL);
230         gpio_direction_output(RK29_PIN1_PB6,GPIO_LOW);  
231         gpio_pull_updown(RK29_PIN1_PB6, PullDisable);  // ÏÂÀ­½ûÖ¹
232         
233         rk29_mux_api_set(GPIO1C1_UART0RTSN_SDMMC1WRITEPRT_NAME, GPIO1H_GPIO1C1);                        
234         gpio_request(RK29_PIN1_PC1, NULL);
235         gpio_direction_output(RK29_PIN1_PC1,GPIO_LOW);
236         
237         rk29_mux_api_set(GPIO1C0_UART0CTSN_SDMMC1DETECTN_NAME, GPIO1H_GPIO1C0);                 
238         gpio_request(RK29_PIN1_PC0, NULL);
239         gpio_direction_input(RK29_PIN1_PC0);            
240 #endif
241
242         pdata->io_init();
243
244         mt6223d_data = kzalloc(sizeof(struct modem_dev), GFP_KERNEL);
245         if(NULL == mt6223d_data)
246         {
247                 printk("failed to request mt6223d_data\n");
248                 goto err6;
249         }
250         platform_set_drvdata(pdev, mt6223d_data);
251
252         result = gpio_request(pdata->bp_statue, "mtk23d");
253         if (result) {
254                 printk("failed to request BP_STATUS gpio\n");
255                 goto err5;
256         }
257         
258         result = gpio_request(pdata->ap_statue, "mtk23d");
259         if (result) {
260                 printk("failed to request AP_STATUS gpio\n");
261                 goto err4;
262         }       
263         
264         result = gpio_request(pdata->ap_bp_wakeup, "mtk23d");
265         if (result) {
266                 printk("failed to request AP_BP_WAKEUP gpio\n");
267                 goto err3;
268         }       
269         result = gpio_request(pdata->bp_reset, "mtk23d");
270         if (result) {
271                 printk("failed to request BP_RESET gpio\n");
272                 goto err2;
273         }               
274         result = gpio_request(pdata->bp_power, "mtk23d");
275         if (result) {
276                 printk("failed to request BP_POW_EN gpio\n");
277                 goto err1;
278         }
279         
280 #if 1 // phc
281         gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
282         gpio_direction_output(pdata->ap_statue, GPIO_LOW);
283         gpio_direction_output(pdata->ap_bp_wakeup, GPIO_LOW);
284         mdelay(100);
285         gpio_direction_output(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_LOW:GPIO_HIGH);
286         mdelay(100);
287         gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
288         gpio_set_value(pdata->ap_bp_wakeup, GPIO_HIGH);
289         
290         gpio_direction_input(pdata->bp_statue);
291 #endif  
292         
293 #if 0 
294         gpio_direction_input(pdata->bp_statue);
295         
296         //rk2818_mux_api_set(CXGPIO_HSADC_SEL_NAME, 0);
297         gpio_direction_output(pdata->ap_statue, GPIO_LOW);
298         
299         //rk2818_mux_api_set(GPIOF5_APWM3_DPWM3_NAME,0);
300         gpio_direction_output(pdata->ap_bp_wakeup, GPIO_LOW);
301         mdelay(100);
302         //rk2818_mux_api_set(GPIOE_SPI1_FLASH_SEL_NAME, IOMUXA_GPIO1_A3B7);
303         gpio_direction_output(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_LOW:GPIO_HIGH);
304         mdelay(100);
305         gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
306         gpio_set_value(pdata->ap_bp_wakeup, GPIO_HIGH);
307 #endif
308
309         INIT_WORK(&mt6223d_data->work, bpwakeup_work_func_work);
310         power_on = 1;
311         result = misc_register(&mtk23d_misc);
312         if(result)
313         {
314                 MODEMDBG("misc_register err\n");
315         }
316         MODEMDBG("mtk23d_probe ok\n");
317         
318         if (!wakelock_inited) {
319                 wake_lock_init(&mtk23d_wakelock, WAKE_LOCK_SUSPEND, "23d_resume");
320                 wakelock_inited = true;
321         }
322         
323         return result;
324 err0:
325         cancel_work_sync(&mt6223d_data->work);
326         gpio_free(pdata->bp_ap_wakeup);
327 err1:
328         gpio_free(pdata->bp_power);
329 err2:
330         gpio_free(pdata->bp_reset);
331 err3:
332         gpio_free(pdata->ap_bp_wakeup);
333 err4:
334         gpio_free(pdata->ap_statue);
335 err5:
336         gpio_free(pdata->bp_statue);
337 err6:
338         kfree(mt6223d_data);
339 ret:
340         return result;
341 }
342
343 static irqreturn_t BBwakeup_isr(int irq, void *dev_id)
344 {
345         struct rk2818_23d_data *pdata = dev_id;
346         
347         MODEMDBG("%s \n", __FUNCTION__);
348         //if(irq != gpio_to_irq(RK29_PIN1_PC0))
349         //{
350         //              printk("irq != gpio_to_irq(RK29_PIN1_PC0) \n");
351         //              return IRQ_NONE;
352         //}
353         
354 //      disable_irq_wake(irq);
355         
356         wake_lock_timeout(&mtk23d_wakelock, 2 * HZ);
357
358         return IRQ_HANDLED;
359 }
360
361 int mtk23d_suspend(struct platform_device *pdev)
362 {
363         int irq, error;
364         struct rk2818_23d_data *pdata = pdev->dev.platform_data;
365         
366         MODEMDBG("%s \n", __FUNCTION__);
367         //enable_irq_wake(irq);
368         ap_sleep(pdev);
369         ap_wakeup_bp(pdev, 0);
370
371         irq = gpio_to_irq(pdata->bp_statue);
372         if (irq < 0) {
373                 printk("can't get pdata->bp_statue irq \n");
374         }
375         else
376         {
377                 error = request_irq(irq, BBwakeup_isr,
378                                     IRQF_TRIGGER_FALLING,
379                                     NULL,
380                                     pdata);
381                 if (error) {
382                         printk("request_irq error!!! \n");
383                 }
384                 enable_irq_wake(irq);
385         }
386         
387         return 0;
388 }
389
390 int mtk23d_resume(struct platform_device *pdev)
391 {
392         struct rk2818_23d_data *pdata = pdev->dev.platform_data;
393         int irq = 0;
394         
395         MODEMDBG("%s \n", __FUNCTION__);
396         //disable_irq_wake(irq);
397         ap_wakeup(pdev);
398         ap_wakeup_bp(pdev, 1);
399         
400         irq = gpio_to_irq(pdata->bp_statue);
401         if(irq)
402         {
403                 printk("disable pdata->bp_statue irq_wake!! \n");
404                 disable_irq_wake(irq);
405         }
406         
407         return 0;
408 }
409
410 void mtk23d_shutdown(struct platform_device *pdev, pm_message_t state)
411 {
412         struct rk2818_23d_data *pdata = pdev->dev.platform_data;
413         struct modem_dev *mt6223d_data = platform_get_drvdata(pdev);
414         
415         MODEMDBG("%s \n", __FUNCTION__);
416         
417         modem_poweron_off(0);  // power down
418
419         cancel_work_sync(&mt6223d_data->work);
420         gpio_free(pdata->bp_ap_wakeup);
421         gpio_free(pdata->bp_power);
422         gpio_free(pdata->bp_reset);
423         gpio_free(pdata->ap_bp_wakeup);
424         gpio_free(pdata->ap_statue);
425         gpio_free(pdata->bp_statue);
426         kfree(mt6223d_data);
427 }
428
429 static struct platform_driver mtk23d_driver = {
430         .probe  = mtk23d_probe,
431         .shutdown       = mtk23d_shutdown,
432         .suspend        = mtk23d_suspend,
433         .resume         = mtk23d_resume,
434         .driver = {
435                 .name   = "mtk23d",
436                 .owner  = THIS_MODULE,
437         },
438 };
439
440 static int __init mtk23d_init(void)
441 {
442         MODEMDBG("mtk23d_init ret=%d\n");
443         return platform_driver_register(&mtk23d_driver);
444 }
445
446 static void __exit mtk23d_exit(void)
447 {
448         MODEMDBG("mtk23d_exit\n");
449         platform_driver_unregister(&mtk23d_driver);
450 }
451
452 module_init(mtk23d_init);
453 //late_initcall_sync(mtk23d_init);
454 module_exit(mtk23d_exit);