simcard_detect.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * linux/drivers/input/adc_kbd/adc_keypad.c
  3. *
  4. * ADC sim card detect Driver
  5. *
  6. * Copyright (C) 2010 Amlogic Corporation
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. * author : alex.deng
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/types.h>
  28. #include <linux/input.h>
  29. #include <linux/kernel.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/mutex.h>
  33. #include <linux/errno.h>
  34. #include <asm/irq.h>
  35. #include <asm/io.h>
  36. #include <mach/am_regs.h>
  37. #include <mach/pinmux.h>
  38. #include <linux/major.h>
  39. #include <linux/slab.h>
  40. #include <asm/uaccess.h>
  41. #include <linux/saradc.h>
  42. #include <linux/simcard_detect.h>
  43. #include <linux/aml_modem.h>
  44. struct sim_detect{
  45. int (*modem_control)(int param);
  46. struct timer_list timer;
  47. unsigned int cur_simcard_status;
  48. int config_major;
  49. char config_name[20];
  50. struct class *config_class;
  51. struct device *config_dev;
  52. int chan[SARADC_CHAN_NUM];
  53. int chan_num;
  54. struct simcard_status *sim_status;
  55. int sim_status_num;
  56. };
  57. static struct sim_detect *gp_sim_detect=NULL;
  58. static int in_count = 0 ;
  59. static int out_count = 0;
  60. static int start_count_timer = 0 ;
  61. static int simcard_search(struct sim_detect *p_sd)
  62. {
  63. struct simcard_status *p_sim_status;
  64. int value, i, j;
  65. for (i=0; i<p_sd->chan_num; i++) {
  66. value = get_adc_sample(p_sd->chan[i]);
  67. if (value < 0) {
  68. continue;
  69. }
  70. p_sim_status = p_sd->sim_status;
  71. for (j=0; j<p_sd->sim_status_num; j++) {
  72. if ((p_sim_status->chan == p_sd->chan[i])
  73. && (value >= p_sim_status->value - p_sim_status->tolerance)
  74. && (value <= p_sim_status->value + p_sim_status->tolerance)) {
  75. return p_sim_status->code;
  76. }
  77. p_sim_status++;
  78. }
  79. }
  80. return SIM_OUT;
  81. }
  82. static void simcard_detect_work(struct sim_detect *p_sd)
  83. {
  84. #if 0
  85. if(SIM_IN == p_sd->cur_simcard_status){
  86. if(in_count >= 5){
  87. in_count = 0 ;
  88. start_count_timer = 0 ;
  89. return ;
  90. }
  91. }
  92. else if(SIM_OUT == p_sd->cur_simcard_status){
  93. if( out_count >= 5 ){
  94. out_count = 0;
  95. start_count_timer = 0 ;
  96. return ;
  97. }
  98. }
  99. #endif
  100. int code = simcard_search(p_sd);
  101. if(SIM_IN == p_sd->cur_simcard_status){
  102. if (SIM_OUT == code) {
  103. printk("simcard pulled out.\n");
  104. p_sd->cur_simcard_status = SIM_OUT ;
  105. start_count_timer = 1 ;
  106. }
  107. else if(start_count_timer > 0){
  108. in_count++ ;
  109. }
  110. }
  111. else{
  112. if(SIM_IN == code){
  113. printk("simcard pushed in.\n");
  114. p_sd->cur_simcard_status = SIM_IN ;
  115. start_count_timer = 1 ;
  116. }
  117. else if(start_count_timer > 0){
  118. out_count++ ;
  119. }
  120. }
  121. }
  122. void simdetect_timer_sr(unsigned long data)
  123. {
  124. struct sim_detect *p_sd=(struct sim_detect *)data;
  125. simcard_detect_work(p_sd);
  126. mod_timer(&p_sd->timer,jiffies+msecs_to_jiffies(200));
  127. }
  128. static int
  129. adcsimdetect_config_open(struct inode *inode, struct file *file)
  130. {
  131. file->private_data = gp_sim_detect;
  132. return 0;
  133. }
  134. static int
  135. adcsimdetect_config_release(struct inode *inode, struct file *file)
  136. {
  137. file->private_data=NULL;
  138. return 0;
  139. }
  140. static const struct file_operations simdetect_fops = {
  141. .owner = THIS_MODULE,
  142. .open = adcsimdetect_config_open,
  143. .ioctl = NULL,
  144. .release = adcsimdetect_config_release,
  145. };
  146. static int register_simdetect_dev(struct sim_detect *p_sd, int dev_id)
  147. {
  148. int ret=0;
  149. strcpy(p_sd->config_name,"am_adc_simdetect");
  150. p_sd->config_name[strlen(p_sd->config_name)] = (char)('0' + dev_id);
  151. ret=register_chrdev(0, p_sd->config_name, &simdetect_fops);
  152. if(ret<=0)
  153. {
  154. printk("register char device error\r\n");
  155. return ret ;
  156. }
  157. p_sd->config_major=ret;
  158. printk("adc simdetect major:%d\r\n",ret);
  159. p_sd->config_class=class_create(THIS_MODULE,p_sd->config_name);
  160. p_sd->config_dev=device_create(p_sd->config_class, NULL,
  161. MKDEV(p_sd->config_major,0),NULL,p_sd->config_name);
  162. return ret;
  163. }
  164. static ssize_t simstatus_show(struct class *cla, struct class_attribute *attr, char *buf)
  165. {
  166. return sprintf(buf, "%d\n", gp_sim_detect->cur_simcard_status ); ;
  167. }
  168. static struct class_attribute simstatus_class_attrs[] = {
  169. __ATTR_RO(simstatus),
  170. __ATTR_NULL
  171. };
  172. static struct class simstatus_class = {
  173. .name = "simstatus",
  174. .class_attrs = simstatus_class_attrs,
  175. };
  176. static int __init simdetect_probe(struct platform_device *pdev)
  177. {
  178. struct sim_detect *p_sim_detect;
  179. int i, j ;
  180. struct adc_simdetect_platform_data *pdata = pdev->dev.platform_data;
  181. if (!pdata) {
  182. dev_err(&pdev->dev, "platform data is required!\n");
  183. return -EINVAL;
  184. }
  185. p_sim_detect = kzalloc(sizeof(struct sim_detect), GFP_KERNEL);
  186. if (!p_sim_detect ) {
  187. return -ENOMEM;
  188. }
  189. gp_sim_detect = p_sim_detect;
  190. platform_set_drvdata(pdev, p_sim_detect);
  191. p_sim_detect->cur_simcard_status = SIM_OUT;
  192. setup_timer(&p_sim_detect->timer, simdetect_timer_sr, p_sim_detect) ;
  193. mod_timer(&p_sim_detect->timer, jiffies+msecs_to_jiffies(100));
  194. p_sim_detect->sim_status = pdata->sim_status;
  195. p_sim_detect->sim_status_num = pdata->sim_status_num;
  196. if (pdata->modem_control){
  197. p_sim_detect->modem_control = pdata->modem_control;
  198. }
  199. struct simcard_status *sc_s = pdata->sim_status;
  200. int new_chan_flag;
  201. p_sim_detect->chan_num = 0;
  202. for (i=0; i<p_sim_detect->sim_status_num; i++) {
  203. /* search the chan */
  204. new_chan_flag = 1;
  205. for (j=0; j<p_sim_detect->chan_num; j++) {
  206. if (sc_s->chan == p_sim_detect->chan[j]) {
  207. new_chan_flag = 0;
  208. break;
  209. }
  210. }
  211. if (new_chan_flag) {
  212. p_sim_detect->chan[p_sim_detect->chan_num] = sc_s->chan;
  213. printk(KERN_INFO "chan #%d used for ADC simcard detect\n", sc_s->chan);
  214. p_sim_detect->chan_num++;
  215. }
  216. printk(KERN_INFO "%s code(%d) registed.\n", sc_s->name, sc_s->code);
  217. sc_s++;
  218. }
  219. register_simdetect_dev(gp_sim_detect, pdev->id);
  220. return 0;
  221. }
  222. static int simdetect_remove(struct platform_device *pdev)
  223. {
  224. struct sim_detect *p_sim_detect = platform_get_drvdata(pdev);
  225. p_sim_detect->modem_control(0);
  226. unregister_chrdev(p_sim_detect->config_major,p_sim_detect->config_name);
  227. if(p_sim_detect->config_class)
  228. {
  229. if(p_sim_detect->config_dev)
  230. device_destroy(p_sim_detect->config_class,MKDEV(p_sim_detect->config_major,0));
  231. class_destroy(p_sim_detect->config_class);
  232. }
  233. kfree(p_sim_detect);
  234. gp_sim_detect=NULL ;
  235. return 0;
  236. }
  237. static struct platform_driver simdetect_driver = {
  238. .probe = simdetect_probe,
  239. .remove = simdetect_remove,
  240. .suspend = NULL,
  241. .resume = NULL,
  242. .driver = {
  243. .name = "m1-adc-simdetect",
  244. },
  245. };
  246. static int __devinit simdetect_init(void)
  247. {
  248. printk(KERN_INFO "ADC Simcard detect Driver init.\n");
  249. class_register(&simstatus_class);
  250. return platform_driver_register(&simdetect_driver);
  251. }
  252. static void __exit simdetect_exit(void)
  253. {
  254. printk(KERN_INFO "ADC Simcard detect Driver exit.\n");
  255. platform_driver_unregister(&simdetect_driver);
  256. class_unregister(&simstatus_class);
  257. }
  258. module_init(simdetect_init);
  259. module_exit(simdetect_exit);
  260. MODULE_AUTHOR("Alex Deng");
  261. MODULE_DESCRIPTION("ADC Simdetec Driver");
  262. MODULE_LICENSE("GPL");