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 wakelock_inited;
49 static struct wake_lock mtk23d_wakelock;
54 //struct modem_dev *mt6223d_data = NULL;
55 struct rk2818_23d_data *gpdata = NULL;
57 static int get_bp_statue(struct platform_device *pdev)
59 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
61 if(gpio_get_value(pdata->bp_statue))
66 static void ap_sleep(struct platform_device *pdev)
68 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
70 MODEMDBG("ap sleep!\n");
71 gpio_set_value(pdata->ap_statue,GPIO_HIGH);
73 static void ap_wakeup(struct platform_device *pdev)
75 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
77 MODEMDBG("ap wakeup!\n");
78 gpio_set_value(pdata->ap_statue,GPIO_LOW);
81 static void ap_wakeup_bp(struct platform_device *pdev, int wake)//low to wakeup bp
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");
87 gpio_set_value(pdata->ap_bp_wakeup, wake); // phc
88 //gpio_set_value(RK2818_PIN_PF5, wake);
91 static void bpwakeup_work_func_work(struct work_struct *work)
93 struct modem_dev *bdata = container_of(work, struct modem_dev, work);
95 MODEMDBG("%s\n", __FUNCTION__);
99 static irqreturn_t bpwakeup_work_func(int irq, void *data)
101 struct modem_dev *mt6223d_data = (struct modem_dev *)data;
103 MODEMDBG("bpwakeup_work_func\n");
104 schedule_work(&mt6223d_data->work);
107 static irqreturn_t bp_apwakeup_work_func(int irq, void *data)
109 //struct modem_dev *dev = &mtk23d_misc;
111 MODEMDBG("bp_apwakeup_work_func\n");
112 //wake_up_interruptible(&dev->wakeup);
115 int modem_poweron_off(int on_off)
117 struct rk2818_23d_data *pdata = gpdata;
121 printk("modem_poweron\n");
122 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH);
124 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
126 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
130 printk("modem_poweroff\n");
131 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH);
133 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
136 static int power_on =1;
137 static int mtk23d_open(struct inode *inode, struct file *file)
139 struct rk2818_23d_data *pdata = gpdata;
140 //struct rk2818_23d_data *pdata = gpdata = pdev->dev.platform_data;
141 struct platform_data *pdev = container_of(pdata, struct device, platform_data);
143 MODEMDBG("modem_open\n");
149 modem_poweron_off(1);
151 rk29_mux_api_set(GPIO1B7_UART0SOUT_NAME, GPIO1L_UART0_SOUT);
152 rk29_mux_api_set(GPIO1B6_UART0SIN_NAME, GPIO1L_UART0_SIN);
153 rk29_mux_api_set(GPIO1C1_UART0RTSN_SDMMC1WRITEPRT_NAME, GPIO1H_UART0_RTS_N);
154 rk29_mux_api_set(GPIO1C0_UART0CTSN_SDMMC1DETECTN_NAME, GPIO1H_UART0_CTS_N);
157 device_init_wakeup(&pdev, 1);
162 static int mtk23d_release(struct inode *inode, struct file *file)
164 MODEMDBG("mtk23d_release\n");
166 //gpio_free(pdata->bp_power);
170 //extern char imei_value[16]; // phc, no find 'imei_value' in rk29 project
171 char imei_value[16] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5};
173 static int mtk23d_ioctl(struct inode *inode,struct file *file, unsigned int cmd, unsigned long arg)
175 struct rk2818_23d_data *pdata = gpdata;
177 void __user *argp = (void __user *)arg;
178 printk("mtk23d_ioctl\n");
182 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_LOW:GPIO_HIGH);
184 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_LOW:GPIO_HIGH);
186 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
188 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
190 case MTK23D_IMEI_READ:
191 if(copy_to_user(argp, &(imei_value[0]), 16))
193 printk("ERROR: copy_to_user---%s\n", __FUNCTION__);
203 static struct file_operations mtk23d_fops = {
204 .owner = THIS_MODULE,
206 .release = mtk23d_release,
207 .ioctl = mtk23d_ioctl
210 static struct miscdevice mtk23d_misc = {
211 .minor = MISC_DYNAMIC_MINOR,
216 static int mtk23d_probe(struct platform_device *pdev)
218 struct rk2818_23d_data *pdata = gpdata = pdev->dev.platform_data;
219 struct modem_dev *mt6223d_data = NULL;
222 MODEMDBG("mtk23d_probe\n");
225 rk29_mux_api_set(GPIO1B7_UART0SOUT_NAME, GPIO1L_GPIO1B7);
226 gpio_request(RK29_PIN1_PB7, NULL);
227 gpio_direction_output(RK29_PIN1_PB7,GPIO_LOW);
228 gpio_pull_updown(RK29_PIN1_PB7, PullDisable); // ÏÂÀ½ûÖ¹
230 rk29_mux_api_set(GPIO1B6_UART0SIN_NAME, GPIO1L_GPIO1B6);
231 gpio_request(RK29_PIN1_PB6, NULL);
232 gpio_direction_output(RK29_PIN1_PB6,GPIO_LOW);
233 gpio_pull_updown(RK29_PIN1_PB6, PullDisable); // ÏÂÀ½ûÖ¹
235 rk29_mux_api_set(GPIO1C1_UART0RTSN_SDMMC1WRITEPRT_NAME, GPIO1H_GPIO1C1);
236 gpio_request(RK29_PIN1_PC1, NULL);
237 gpio_direction_output(RK29_PIN1_PC1,GPIO_LOW);
239 rk29_mux_api_set(GPIO1C0_UART0CTSN_SDMMC1DETECTN_NAME, GPIO1H_GPIO1C0);
240 gpio_request(RK29_PIN1_PC0, NULL);
241 gpio_direction_input(RK29_PIN1_PC0);
246 mt6223d_data = kzalloc(sizeof(struct modem_dev), GFP_KERNEL);
247 if(NULL == mt6223d_data)
249 printk("failed to request mt6223d_data\n");
252 platform_set_drvdata(pdev, mt6223d_data);
254 result = gpio_request(pdata->bp_statue, "mtk23d");
256 printk("failed to request BP_STATUS gpio\n");
260 result = gpio_request(pdata->ap_statue, "mtk23d");
262 printk("failed to request AP_STATUS gpio\n");
266 result = gpio_request(pdata->ap_bp_wakeup, "mtk23d");
268 printk("failed to request AP_BP_WAKEUP gpio\n");
271 result = gpio_request(pdata->bp_reset, "mtk23d");
273 printk("failed to request BP_RESET gpio\n");
276 result = gpio_request(pdata->bp_power, "mtk23d");
278 printk("failed to request BP_POW_EN gpio\n");
283 gpio_set_value(pdata->bp_power, pdata->bp_power_active_low? GPIO_HIGH:GPIO_LOW);
284 gpio_direction_output(pdata->ap_statue, GPIO_LOW);
285 gpio_direction_output(pdata->ap_bp_wakeup, GPIO_LOW);
287 gpio_direction_output(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_LOW:GPIO_HIGH);
289 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
290 gpio_set_value(pdata->ap_bp_wakeup, GPIO_HIGH);
292 gpio_direction_input(pdata->bp_statue);
296 gpio_direction_input(pdata->bp_statue);
298 //rk2818_mux_api_set(CXGPIO_HSADC_SEL_NAME, 0);
299 gpio_direction_output(pdata->ap_statue, GPIO_LOW);
301 //rk2818_mux_api_set(GPIOF5_APWM3_DPWM3_NAME,0);
302 gpio_direction_output(pdata->ap_bp_wakeup, GPIO_LOW);
304 //rk2818_mux_api_set(GPIOE_SPI1_FLASH_SEL_NAME, IOMUXA_GPIO1_A3B7);
305 gpio_direction_output(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_LOW:GPIO_HIGH);
307 gpio_set_value(pdata->bp_reset, pdata->bp_reset_active_low? GPIO_HIGH:GPIO_LOW);
308 gpio_set_value(pdata->ap_bp_wakeup, GPIO_HIGH);
311 INIT_WORK(&mt6223d_data->work, bpwakeup_work_func_work);
313 result = misc_register(&mtk23d_misc);
316 MODEMDBG("misc_register err\n");
318 MODEMDBG("mtk23d_probe ok\n");
320 if (!wakelock_inited) {
321 wake_lock_init(&mtk23d_wakelock, WAKE_LOCK_SUSPEND, "23d_resume");
322 wakelock_inited = true;
327 cancel_work_sync(&mt6223d_data->work);
328 gpio_free(pdata->bp_ap_wakeup);
330 gpio_free(pdata->bp_power);
332 gpio_free(pdata->bp_reset);
334 gpio_free(pdata->ap_bp_wakeup);
336 gpio_free(pdata->ap_statue);
338 gpio_free(pdata->bp_statue);
345 static irqreturn_t BBwakeup_isr(int irq, void *dev_id)
347 struct rk2818_23d_data *pdata = dev_id;
349 MODEMDBG("%s \n", __FUNCTION__);
350 //if(irq != gpio_to_irq(RK29_PIN1_PC0))
352 // printk("irq != gpio_to_irq(RK29_PIN1_PC0) \n");
356 // disable_irq_wake(irq);
358 wake_lock_timeout(&mtk23d_wakelock, 2 * HZ);
363 int mtk23d_suspend(struct platform_device *pdev)
366 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
368 MODEMDBG("%s \n", __FUNCTION__);
369 //enable_irq_wake(irq);
371 ap_wakeup_bp(pdev, 0);
373 irq = gpio_to_irq(pdata->bp_statue);
375 printk("can't get pdata->bp_statue irq \n");
379 error = request_irq(irq, BBwakeup_isr,
380 IRQF_TRIGGER_FALLING,
384 printk("request_irq error!!! \n");
386 enable_irq_wake(irq);
392 int mtk23d_resume(struct platform_device *pdev)
394 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
397 MODEMDBG("%s \n", __FUNCTION__);
398 //disable_irq_wake(irq);
400 ap_wakeup_bp(pdev, 1);
402 irq = gpio_to_irq(pdata->bp_statue);
405 printk("disable pdata->bp_statue irq_wake!! \n");
406 disable_irq_wake(irq);
412 void mtk23d_shutdown(struct platform_device *pdev, pm_message_t state)
414 struct rk2818_23d_data *pdata = pdev->dev.platform_data;
415 struct modem_dev *mt6223d_data = platform_get_drvdata(pdev);
417 MODEMDBG("%s \n", __FUNCTION__);
419 modem_poweron_off(0); // power down
421 cancel_work_sync(&mt6223d_data->work);
422 gpio_free(pdata->bp_ap_wakeup);
423 gpio_free(pdata->bp_power);
424 gpio_free(pdata->bp_reset);
425 gpio_free(pdata->ap_bp_wakeup);
426 gpio_free(pdata->ap_statue);
427 gpio_free(pdata->bp_statue);
431 static struct platform_driver mtk23d_driver = {
432 .probe = mtk23d_probe,
433 .shutdown = mtk23d_shutdown,
434 .suspend = mtk23d_suspend,
435 .resume = mtk23d_resume,
438 .owner = THIS_MODULE,
442 static int __init mtk23d_init(void)
444 MODEMDBG("mtk23d_init ret=%d\n");
445 return platform_driver_register(&mtk23d_driver);
448 static void __exit mtk23d_exit(void)
450 MODEMDBG("mtk23d_exit\n");
451 platform_driver_unregister(&mtk23d_driver);
454 module_init(mtk23d_init);
455 //late_initcall_sync(mtk23d_init);
456 module_exit(mtk23d_exit);