1 #include <linux/module.h>
2 #include <linux/kernel.h>
5 #include <linux/gpio.h>
6 #include <linux/input.h>
7 #include <linux/platform_device.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>
26 MODULE_LICENSE("GPL");
30 #define MODEMDBG(x...) printk(x)
32 #define MODEMDBG(fmt,argss...)
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
43 //#define BP_RESET TCA6424_P11 //Ryan
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
48 static bool bpstatus_irq_enable = false;
49 static bool wakelock_inited;
50 static struct wake_lock mtk23d_wakelock;
55 //struct modem_dev *mt6223d_data = NULL;
56 struct rk2818_23d_data *gpdata = NULL;
58 static int get_bp_statue(struct platform_device *pdev)
60 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
62 if(gpio_get_value(pdata->bp_statue))
67 static void ap_sleep(struct platform_device *pdev)
69 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
71 MODEMDBG("ap sleep!\n");
72 gpio_set_value(pdata->ap_statue,GPIO_HIGH);
74 static void ap_wakeup(struct platform_device *pdev)
76 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
78 MODEMDBG("ap wakeup!\n");
79 gpio_set_value(pdata->ap_statue,GPIO_LOW);
82 static void ap_wakeup_bp(struct platform_device *pdev, int wake)//low to wakeup bp
84 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
85 struct modem_dev *mt6223d_data = platform_get_drvdata(pdev);
86 MODEMDBG("ap_wakeup_bp\n");
88 gpio_set_value(pdata->ap_bp_wakeup, wake); // phc
89 //gpio_set_value(RK2818_PIN_PF5, wake);
92 static void bpwakeup_work_func_work(struct work_struct *work)
94 struct modem_dev *bdata = container_of(work, struct modem_dev, work);
96 MODEMDBG("%s\n", __FUNCTION__);
100 static irqreturn_t bpwakeup_work_func(int irq, void *data)
102 struct modem_dev *mt6223d_data = (struct modem_dev *)data;
104 MODEMDBG("bpwakeup_work_func\n");
105 schedule_work(&mt6223d_data->work);
108 static irqreturn_t bp_apwakeup_work_func(int irq, void *data)
110 //struct modem_dev *dev = &mtk23d_misc;
112 MODEMDBG("bp_apwakeup_work_func\n");
113 //wake_up_interruptible(&dev->wakeup);
117 static irqreturn_t BBwakeup_isr(int irq, void *dev_id)
119 struct rk2818_23d_data *pdata = dev_id;
121 MODEMDBG("%s \n", __FUNCTION__);
122 //if(irq != gpio_to_irq(RK29_PIN1_PC0))
124 // printk("irq != gpio_to_irq(RK29_PIN1_PC0) \n");
128 // disable_irq_wake(irq);
130 if(bpstatus_irq_enable == true)
132 MODEMDBG("mtk23d_wakelock 3s \n");
133 wake_lock_timeout(&mtk23d_wakelock, 3 * HZ);
140 int modem_poweron_off(int on_off)
142 struct rk2818_23d_data *pdata = gpdata;
143 int result, error = 0, irq = 0;
147 printk("modem_poweron\n");
149 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH); // power on enable
151 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW); // release reset
153 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW); // power on relase
156 rk29_mux_api_set(GPIO1B7_UART0SOUT_NAME, GPIO1L_UART0_SOUT);
157 rk29_mux_api_set(GPIO1B6_UART0SIN_NAME, GPIO1L_UART0_SIN);
158 rk29_mux_api_set(GPIO1C1_UART0RTSN_SDMMC1WRITEPRT_NAME, GPIO1H_UART0_RTS_N);
159 rk29_mux_api_set(GPIO1C0_UART0CTSN_SDMMC1DETECTN_NAME, GPIO1H_UART0_CTS_N);
162 gpio_direction_input(pdata->bp_statue);
163 gpio_direction_input(pdata->bp_ap_wakeup);
165 /* ³õʼ»¯BP»½ÐÑAPµÄ¹¦ÄÜ */
166 wakelock_inited = false;
167 irq = gpio_to_irq(pdata->bp_statue);
169 printk("can't get pdata->bp_statue irq \n");
173 error = request_irq(irq, BBwakeup_isr,
174 IRQF_TRIGGER_FALLING,
178 printk("mtk23d_probe bp_statue request_irq error!!! \n");
181 if (!wakelock_inited) {
182 wake_lock_init(&mtk23d_wakelock, WAKE_LOCK_SUSPEND, "23d_resume");
183 wakelock_inited = true;
188 printk("modem_poweroff\n");
189 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH);
191 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
194 static int power_on =1;
195 static int mtk23d_open(struct inode *inode, struct file *file)
197 struct rk2818_23d_data *pdata = gpdata;
198 //struct rk2818_23d_data *pdata = gpdata = pdev->dev.platform_data;
199 struct platform_data *pdev = container_of(pdata, struct device, platform_data);
201 MODEMDBG("modem_open\n");
207 modem_poweron_off(1);
209 device_init_wakeup(&pdev, 1);
214 static int mtk23d_release(struct inode *inode, struct file *file)
216 MODEMDBG("mtk23d_release\n");
218 //gpio_free(pdata->bp_power);
222 //extern char imei_value[16]; // phc, no find 'imei_value' in rk29 project
223 char imei_value[16] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5};
225 static int mtk23d_ioctl(struct inode *inode,struct file *file, unsigned int cmd, unsigned long arg)
227 struct rk2818_23d_data *pdata = gpdata;
229 void __user *argp = (void __user *)arg;
230 printk("mtk23d_ioctl\n");
234 printk("MTK23D_RESET\n");
235 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_LOW:GPIO_HIGH);
237 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH);
239 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
241 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
243 case MTK23D_IMEI_READ:
244 printk("MTK23D_IMEI_READ\n");
245 if(copy_to_user(argp, &(imei_value[0]), 16))
247 printk("ERROR: copy_to_user---%s\n", __FUNCTION__);
257 static struct file_operations mtk23d_fops = {
258 .owner = THIS_MODULE,
260 .release = mtk23d_release,
261 .ioctl = mtk23d_ioctl
264 static struct miscdevice mtk23d_misc = {
265 .minor = MISC_DYNAMIC_MINOR,
270 static int mtk23d_probe(struct platform_device *pdev)
272 struct rk2818_23d_data *pdata = gpdata = pdev->dev.platform_data;
273 struct modem_dev *mt6223d_data = NULL;
274 int result, error = 0, irq = 0;
276 MODEMDBG("mtk23d_probe\n");
280 mt6223d_data = kzalloc(sizeof(struct modem_dev), GFP_KERNEL);
281 if(NULL == mt6223d_data)
283 printk("failed to request mt6223d_data\n");
286 platform_set_drvdata(pdev, mt6223d_data);
288 result = gpio_request(pdata->bp_statue, "mtk23d");
290 printk("failed to request BP_STATUS gpio\n");
294 result = gpio_request(pdata->ap_statue, "mtk23d");
296 printk("failed to request AP_STATUS gpio\n");
300 result = gpio_request(pdata->ap_bp_wakeup, "mtk23d");
302 printk("failed to request AP_BP_WAKEUP gpio\n");
305 result = gpio_request(pdata->bp_reset, "mtk23d");
307 printk("failed to request BP_RESET gpio\n");
310 result = gpio_request(pdata->bp_power, "mtk23d");
312 printk("failed to request BP_POW_EN gpio\n");
316 if(pdata->bp_ap_wakeup) // SDK°åÖУ¬¸Ã¿ÚûÓÐÒý³ö
318 result = gpio_request(pdata->bp_ap_wakeup, "mtk23d");
320 printk("failed to request BP_AP_WAKEUP gpio\n");
325 #if 1 // GPIO³õʼ»¯£¬²¢ÇÒ·Àֹ©µç
326 rk29_mux_api_set(GPIO1B7_UART0SOUT_NAME, GPIO1L_GPIO1B7);
327 gpio_request(RK29_PIN1_PB7, NULL);
328 gpio_direction_output(RK29_PIN1_PB7,GPIO_LOW);
329 gpio_pull_updown(RK29_PIN1_PB7, PullDisable); // ÏÂÀ½ûÖ¹
331 rk29_mux_api_set(GPIO1B6_UART0SIN_NAME, GPIO1L_GPIO1B6);
332 gpio_request(RK29_PIN1_PB6, NULL);
333 gpio_direction_output(RK29_PIN1_PB6,GPIO_LOW);
334 gpio_pull_updown(RK29_PIN1_PB6, PullDisable); // ÏÂÀ½ûÖ¹
336 rk29_mux_api_set(GPIO1C1_UART0RTSN_SDMMC1WRITEPRT_NAME, GPIO1H_GPIO1C1);
337 gpio_request(RK29_PIN1_PC1, NULL);
338 gpio_direction_output(RK29_PIN1_PC1,GPIO_LOW);
340 rk29_mux_api_set(GPIO1C0_UART0CTSN_SDMMC1DETECTN_NAME, GPIO1H_GPIO1C0);
341 gpio_request(RK29_PIN1_PC0, NULL);
342 //gpio_direction_input(RK29_PIN1_PC0);
343 gpio_direction_output(RK29_PIN1_PC0,GPIO_LOW);
346 gpio_direction_output(pdata->bp_power, GPIO_LOW);
347 gpio_direction_output(pdata->ap_statue, GPIO_LOW);
348 gpio_direction_output(pdata->ap_bp_wakeup, GPIO_LOW);
349 gpio_direction_output(pdata->bp_reset, GPIO_LOW);
350 //gpio_direction_output(pdata->bp_statue,GPIO_LOW);
351 gpio_direction_input(pdata->bp_statue);
352 if(pdata->bp_ap_wakeup) // SDK°åÖУ¬¸Ã¿ÚûÓÐÒý³ö
354 //gpio_direction_output(pdata->bp_ap_wakeup,GPIO_LOW);
355 gpio_direction_input(pdata->bp_ap_wakeup);
359 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_LOW:GPIO_HIGH);
361 //gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
364 INIT_WORK(&mt6223d_data->work, bpwakeup_work_func_work);
366 result = misc_register(&mtk23d_misc);
369 MODEMDBG("misc_register err\n");
371 MODEMDBG("mtk23d_probe ok\n");
375 cancel_work_sync(&mt6223d_data->work);
376 gpio_free(pdata->bp_ap_wakeup);
378 gpio_free(pdata->bp_power);
380 gpio_free(pdata->bp_reset);
382 gpio_free(pdata->ap_bp_wakeup);
384 gpio_free(pdata->ap_statue);
386 gpio_free(pdata->bp_statue);
393 int mtk23d_suspend(struct platform_device *pdev)
396 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
398 MODEMDBG("%s \n", __FUNCTION__);
400 //enable_irq_wake(irq);
402 ap_wakeup_bp(pdev, 0);
404 irq = gpio_to_irq(pdata->bp_statue);
406 printk("can't get pdata->bp_statue irq \n");
410 printk("enable pdata->bp_statue irq_wake!! \n");
411 bpstatus_irq_enable = true;
412 enable_irq_wake(irq);
418 int mtk23d_resume(struct platform_device *pdev)
420 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
423 MODEMDBG("%s \n", __FUNCTION__);
425 irq = gpio_to_irq(pdata->bp_statue);
428 printk("disable pdata->bp_statue irq_wake!! \n");
429 bpstatus_irq_enable = false;
430 disable_irq_wake(irq);
434 ap_wakeup_bp(pdev, 1);
439 void mtk23d_shutdown(struct platform_device *pdev, pm_message_t state)
441 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
442 struct modem_dev *mt6223d_data = platform_get_drvdata(pdev);
444 MODEMDBG("%s \n", __FUNCTION__);
446 modem_poweron_off(0); // power down
448 cancel_work_sync(&mt6223d_data->work);
449 gpio_free(pdata->bp_ap_wakeup);
450 gpio_free(pdata->bp_power);
451 gpio_free(pdata->bp_reset);
452 gpio_free(pdata->ap_bp_wakeup);
453 gpio_free(pdata->ap_statue);
454 gpio_free(pdata->bp_statue);
458 static struct platform_driver mtk23d_driver = {
459 .probe = mtk23d_probe,
460 .shutdown = mtk23d_shutdown,
461 .suspend = mtk23d_suspend,
462 .resume = mtk23d_resume,
465 .owner = THIS_MODULE,
469 static int __init mtk23d_init(void)
471 MODEMDBG("mtk23d_init ret=%d\n");
472 return platform_driver_register(&mtk23d_driver);
475 static void __exit mtk23d_exit(void)
477 MODEMDBG("mtk23d_exit\n");
478 platform_driver_unregister(&mtk23d_driver);
481 module_init(mtk23d_init);
482 //late_initcall_sync(mtk23d_init);
483 module_exit(mtk23d_exit);