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>
25 #include "../mtd/rknand/api_flash.h"
27 MODULE_LICENSE("GPL");
31 #define MODEMDBG(x...) printk(x)
33 #define MODEMDBG(fmt,argss...)
36 #define MTK23D_RESET 0x01
37 #define MTK23D_POWERON 0x02
38 #define MTK23D_POWER_HIGH 0x03
39 #define MTK23D_IMEI_READ 0x04
40 //#define BP_POW_EN TCA6424_P02
41 //#define BP_STATUS RK2818_PIN_PH7 //input high bp sleep
42 //#define AP_STATUS RK2818_PIN_PA4 //output high ap sleep
44 //#define BP_RESET TCA6424_P11 //Ryan
46 //#define AP_BP_WAKEUP RK2818_PIN_PF5 //output AP wake up BP used rising edge
47 //#define BP_AP_WAKEUP RK2818_PIN_PE0 //input BP wake up AP
49 static bool bpstatus_irq_enable = false;
50 static bool wakelock_inited;
51 static struct wake_lock mtk23d_wakelock;
56 //struct modem_dev *mt6223d_data = NULL;
57 struct rk2818_23d_data *gpdata = NULL;
59 static int get_bp_statue(struct platform_device *pdev)
61 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
63 if(gpio_get_value(pdata->bp_statue))
68 static void ap_sleep(struct platform_device *pdev)
70 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
72 MODEMDBG("ap sleep!\n");
73 gpio_set_value(pdata->ap_statue,GPIO_HIGH);
75 static void ap_wakeup(struct platform_device *pdev)
77 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
79 MODEMDBG("ap wakeup!\n");
80 gpio_set_value(pdata->ap_statue,GPIO_LOW);
83 static void ap_wakeup_bp(struct platform_device *pdev, int wake)//low to wakeup bp
85 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
86 struct modem_dev *mt6223d_data = platform_get_drvdata(pdev);
87 MODEMDBG("ap_wakeup_bp\n");
89 gpio_set_value(pdata->ap_bp_wakeup, wake); // phc
90 //gpio_set_value(RK2818_PIN_PF5, wake);
93 static void bpwakeup_work_func_work(struct work_struct *work)
95 struct modem_dev *bdata = container_of(work, struct modem_dev, work);
97 MODEMDBG("%s\n", __FUNCTION__);
101 static irqreturn_t bpwakeup_work_func(int irq, void *data)
103 struct modem_dev *mt6223d_data = (struct modem_dev *)data;
105 MODEMDBG("bpwakeup_work_func\n");
106 schedule_work(&mt6223d_data->work);
109 static irqreturn_t bp_apwakeup_work_func(int irq, void *data)
111 //struct modem_dev *dev = &mtk23d_misc;
113 MODEMDBG("bp_apwakeup_work_func\n");
114 //wake_up_interruptible(&dev->wakeup);
118 static irqreturn_t BBwakeup_isr(int irq, void *dev_id)
120 struct rk2818_23d_data *pdata = dev_id;
122 MODEMDBG("%s \n", __FUNCTION__);
123 //if(irq != gpio_to_irq(RK29_PIN1_PC0))
125 // printk("irq != gpio_to_irq(RK29_PIN1_PC0) \n");
129 // disable_irq_wake(irq);
131 if(bpstatus_irq_enable == true)
133 MODEMDBG("mtk23d_wakelock 3s \n");
134 wake_lock_timeout(&mtk23d_wakelock, 3 * HZ);
141 int modem_poweron_off(int on_off)
143 struct rk2818_23d_data *pdata = gpdata;
144 int result, error = 0, irq = 0;
148 printk("modem_poweron\n");
150 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH); // power on enable
152 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW); // release reset
154 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW); // power on relase
157 rk29_mux_api_set(GPIO1B7_UART0SOUT_NAME, GPIO1L_UART0_SOUT);
158 rk29_mux_api_set(GPIO1B6_UART0SIN_NAME, GPIO1L_UART0_SIN);
159 rk29_mux_api_set(GPIO1C1_UART0RTSN_SDMMC1WRITEPRT_NAME, GPIO1H_UART0_RTS_N);
160 rk29_mux_api_set(GPIO1C0_UART0CTSN_SDMMC1DETECTN_NAME, GPIO1H_UART0_CTS_N);
163 gpio_direction_input(pdata->bp_statue);
164 gpio_direction_input(pdata->bp_ap_wakeup);
166 /* ³õʼ»¯BP»½ÐÑAPµÄ¹¦ÄÜ */
167 wakelock_inited = false;
168 irq = gpio_to_irq(pdata->bp_statue);
170 printk("can't get pdata->bp_statue irq \n");
174 error = request_irq(irq, BBwakeup_isr,
175 IRQF_TRIGGER_FALLING,
179 printk("mtk23d_probe bp_statue request_irq error!!! \n");
182 if (!wakelock_inited) {
183 wake_lock_init(&mtk23d_wakelock, WAKE_LOCK_SUSPEND, "23d_resume");
184 wakelock_inited = true;
189 printk("modem_poweroff\n");
190 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH);
192 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
195 static int power_on =1;
196 static int mtk23d_open(struct inode *inode, struct file *file)
198 struct rk2818_23d_data *pdata = gpdata;
199 //struct rk2818_23d_data *pdata = gpdata = pdev->dev.platform_data;
200 struct platform_data *pdev = container_of(pdata, struct device, platform_data);
202 MODEMDBG("modem_open\n");
208 modem_poweron_off(1);
210 device_init_wakeup(&pdev, 1);
215 static int mtk23d_release(struct inode *inode, struct file *file)
217 MODEMDBG("mtk23d_release\n");
219 //gpio_free(pdata->bp_power);
223 //extern char imei_value[16]; // phc, no find 'imei_value' in rk29 project
224 //char imei_value[16] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5};
226 static int mtk23d_ioctl(struct inode *inode,struct file *file, unsigned int cmd, unsigned long arg)
228 struct rk2818_23d_data *pdata = gpdata;
230 void __user *argp = (void __user *)arg;
232 char SectorBuffer[512];
234 printk("mtk23d_ioctl\n");
238 printk("MTK23D_RESET\n");
239 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_LOW:GPIO_HIGH);
241 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH);
243 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
245 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
247 case MTK23D_IMEI_READ:
248 printk("MTK23D_IMEI_READ\n");
250 GetSNSectorInfo(SectorBuffer); // phc,20110624
252 if(copy_to_user(argp, &(SectorBuffer[451]), 16)) // IMEIºó´Ó451Æ«ÒÆ¿ªÊ¼µÄ16bytes£¬µÚÒ»¸öbyteΪ³¤¶È¹Ì¶¨Îª15
254 printk("ERROR: copy_to_user---%s\n", __FUNCTION__);
257 //printk("IMEI:%d %d %d %d\n", SectorBuffer[451], SectorBuffer[452], SectorBuffer[453], SectorBuffer[454]);
265 static struct file_operations mtk23d_fops = {
266 .owner = THIS_MODULE,
268 .release = mtk23d_release,
269 .ioctl = mtk23d_ioctl
272 static struct miscdevice mtk23d_misc = {
273 .minor = MISC_DYNAMIC_MINOR,
278 static int mtk23d_probe(struct platform_device *pdev)
280 struct rk2818_23d_data *pdata = gpdata = pdev->dev.platform_data;
281 struct modem_dev *mt6223d_data = NULL;
282 int result, error = 0, irq = 0;
284 MODEMDBG("mtk23d_probe\n");
288 mt6223d_data = kzalloc(sizeof(struct modem_dev), GFP_KERNEL);
289 if(NULL == mt6223d_data)
291 printk("failed to request mt6223d_data\n");
294 platform_set_drvdata(pdev, mt6223d_data);
296 result = gpio_request(pdata->bp_statue, "mtk23d");
298 printk("failed to request BP_STATUS gpio\n");
302 result = gpio_request(pdata->ap_statue, "mtk23d");
304 printk("failed to request AP_STATUS gpio\n");
308 result = gpio_request(pdata->ap_bp_wakeup, "mtk23d");
310 printk("failed to request AP_BP_WAKEUP gpio\n");
313 result = gpio_request(pdata->bp_reset, "mtk23d");
315 printk("failed to request BP_RESET gpio\n");
318 result = gpio_request(pdata->bp_power, "mtk23d");
320 printk("failed to request BP_POW_EN gpio\n");
324 if(pdata->bp_ap_wakeup) // SDK°åÖУ¬¸Ã¿ÚûÓÐÒý³ö
326 result = gpio_request(pdata->bp_ap_wakeup, "mtk23d");
328 printk("failed to request BP_AP_WAKEUP gpio\n");
333 #if 1 // GPIO³õʼ»¯£¬²¢ÇÒ·Àֹ©µç
334 rk29_mux_api_set(GPIO1B7_UART0SOUT_NAME, GPIO1L_GPIO1B7);
335 gpio_request(RK29_PIN1_PB7, NULL);
336 gpio_direction_output(RK29_PIN1_PB7,GPIO_LOW);
337 gpio_pull_updown(RK29_PIN1_PB7, PullDisable); // ÏÂÀ½ûÖ¹
339 rk29_mux_api_set(GPIO1B6_UART0SIN_NAME, GPIO1L_GPIO1B6);
340 gpio_request(RK29_PIN1_PB6, NULL);
341 gpio_direction_output(RK29_PIN1_PB6,GPIO_LOW);
342 gpio_pull_updown(RK29_PIN1_PB6, PullDisable); // ÏÂÀ½ûÖ¹
344 rk29_mux_api_set(GPIO1C1_UART0RTSN_SDMMC1WRITEPRT_NAME, GPIO1H_GPIO1C1);
345 gpio_request(RK29_PIN1_PC1, NULL);
346 gpio_direction_output(RK29_PIN1_PC1,GPIO_LOW);
348 rk29_mux_api_set(GPIO1C0_UART0CTSN_SDMMC1DETECTN_NAME, GPIO1H_GPIO1C0);
349 gpio_request(RK29_PIN1_PC0, NULL);
350 //gpio_direction_input(RK29_PIN1_PC0);
351 gpio_direction_output(RK29_PIN1_PC0,GPIO_LOW);
354 gpio_direction_output(pdata->bp_power, GPIO_LOW);
355 gpio_direction_output(pdata->ap_statue, GPIO_LOW);
356 gpio_direction_output(pdata->ap_bp_wakeup, GPIO_LOW);
357 gpio_direction_output(pdata->bp_reset, GPIO_LOW);
358 //gpio_direction_output(pdata->bp_statue,GPIO_LOW);
359 gpio_direction_input(pdata->bp_statue);
360 if(pdata->bp_ap_wakeup) // SDK°åÖУ¬¸Ã¿ÚûÓÐÒý³ö
362 //gpio_direction_output(pdata->bp_ap_wakeup,GPIO_LOW);
363 gpio_direction_input(pdata->bp_ap_wakeup);
367 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_LOW:GPIO_HIGH);
369 //gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
372 INIT_WORK(&mt6223d_data->work, bpwakeup_work_func_work);
374 result = misc_register(&mtk23d_misc);
377 MODEMDBG("misc_register err\n");
379 MODEMDBG("mtk23d_probe ok\n");
383 cancel_work_sync(&mt6223d_data->work);
384 gpio_free(pdata->bp_ap_wakeup);
386 gpio_free(pdata->bp_power);
388 gpio_free(pdata->bp_reset);
390 gpio_free(pdata->ap_bp_wakeup);
392 gpio_free(pdata->ap_statue);
394 gpio_free(pdata->bp_statue);
401 int mtk23d_suspend(struct platform_device *pdev)
404 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
406 MODEMDBG("%s \n", __FUNCTION__);
408 //enable_irq_wake(irq);
410 ap_wakeup_bp(pdev, 0);
412 irq = gpio_to_irq(pdata->bp_statue);
414 printk("can't get pdata->bp_statue irq \n");
418 printk("enable pdata->bp_statue irq_wake!! \n");
419 bpstatus_irq_enable = true;
420 enable_irq_wake(irq);
426 int mtk23d_resume(struct platform_device *pdev)
428 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
431 MODEMDBG("%s \n", __FUNCTION__);
433 irq = gpio_to_irq(pdata->bp_statue);
436 printk("disable pdata->bp_statue irq_wake!! \n");
437 bpstatus_irq_enable = false;
438 disable_irq_wake(irq);
442 ap_wakeup_bp(pdev, 1);
447 void mtk23d_shutdown(struct platform_device *pdev, pm_message_t state)
449 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
450 struct modem_dev *mt6223d_data = platform_get_drvdata(pdev);
452 MODEMDBG("%s \n", __FUNCTION__);
454 modem_poweron_off(0); // power down
456 cancel_work_sync(&mt6223d_data->work);
457 gpio_free(pdata->bp_ap_wakeup);
458 gpio_free(pdata->bp_power);
459 gpio_free(pdata->bp_reset);
460 gpio_free(pdata->ap_bp_wakeup);
461 gpio_free(pdata->ap_statue);
462 gpio_free(pdata->bp_statue);
466 static struct platform_driver mtk23d_driver = {
467 .probe = mtk23d_probe,
468 .shutdown = mtk23d_shutdown,
469 .suspend = mtk23d_suspend,
470 .resume = mtk23d_resume,
473 .owner = THIS_MODULE,
477 static int __init mtk23d_init(void)
479 MODEMDBG("mtk23d_init ret=%d\n");
480 return platform_driver_register(&mtk23d_driver);
483 static void __exit mtk23d_exit(void)
485 MODEMDBG("mtk23d_exit\n");
486 platform_driver_unregister(&mtk23d_driver);
489 module_init(mtk23d_init);
490 //late_initcall_sync(mtk23d_init);
491 module_exit(mtk23d_exit);