searchcode logo
Dogecoin is an open source peer-to-peer cryptocurrency

sony-kernel-u8500 /drivers/input/misc/ab8505_micro_usb_iddet.c

git clone https://github.com/Lor-n82/sony-kernel-u8500.git

Language

C

MD5 Hash

192956c34ce8bf21a834fc3ac9be6517

Lines in File 1266
Blank Lines 183 (14.45%)
Comment Lines 114 (9%)
Code Lines 968 (76%)
Estimated Development Effort (Months)2.32
Estimated Development Effort (Years)0
Estimated Developers0.67
Estimated Cost$26110.00
Generated using David A. Wheeler's 'SLOCCount'.
  1. /*
  2.  * Copyright (C) ST-Ericsson SA 2012
  3.  *
  4.  * License terms: GNU General Public License (GPL) version 2
  5.  * Author: Etienne CARRIERE <etienne.carriere@stericsson.com>
  6.  * Author: Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>
  7.  */
  8.  
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/mfd/abx500.h>
  14. #include <linux/hrtimer.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/mfd/abx500/ab8500-gpadc.h>
  17. #include <linux/input.h>
  18. #include <linux/delay.h>
  19. #include <linux/input/ab8505_micro_usb_iddet.h>
  20. #include <sound/jack.h>
  21.  
  22. #define KEY_DEBOUCE_TIME_12MS 0x00
  23. #define KEY_DEBOUCE_TIME_25MS 0x01
  24. #define KEY_DEBOUCE_TIME_37MS 0x02
  25. #define KEY_DEBOUCE_TIME_50MS 0x03
  26. #define KEY_DEBOUCE_TIME_62MS 0x04
  27. #define KEY_DEBOUCE_TIME_75MS 0x05
  28. #define KEY_DEBOUCE_TIME_87MS 0x06
  29. #define KEY_DEBOUCE_TIME_100MS 0x07
  30. #define KEY_DEBOUCE_TIME_0MS 0x08
  31.  
  32. #define KEY_PRESS_TIME_100MS 0x00
  33. #define KEY_PRESS_TIME_200MS 0x01
  34. #define KEY_PRESS_TIME_300MS 0x02
  35. #define KEY_PRESS_TIME_400MS 0x03
  36. #define KEY_PRESS_TIME_500MS 0x04
  37. #define KEY_PRESS_TIME_600MS 0x05
  38. #define KEY_PRESS_TIME_700MS 0x06
  39. #define KEY_PRESS_TIME_800MS 0x07
  40. #define KEY_PRESS_TIME_900MS 0x08
  41. #define KEY_PRESS_TIME_1000MS 0x09
  42. #define KEY_PRESS_TIME_1100MS 0x0a
  43. #define KEY_PRESS_TIME_1200MS 0x0b
  44. #define KEY_PRESS_TIME_1300MS 0x0c
  45. #define KEY_PRESS_TIME_1400MS 0x0d
  46. #define KEY_PRESS_TIME_1500MS 0x0e
  47.  
  48. #define IDHOSTENA 0x02
  49. #define IDDEVENA 0x01
  50. #define IDDETSWCTRLENA 0x80
  51. #define IDDETPU1ENA 0x20
  52. #define PLUGDETCOMPENA 0x08
  53. #define IDDETPLUGDETCOMP 0x08
  54. #define IDDETPU200K18VENA 0x40
  55. #define IDDETADCENA 0x01
  56. #define ITSOURCE2 0x01
  57. #define VUSBENA 0x01
  58. #define VBUSDET 0x80
  59. #define VBUSVALIDENA 0x20
  60. #define USBSWCTRL 0x04
  61. #define USBCPMICSUBCLKENA 0x08
  62. #define USBHOSTMODEENA 0x01
  63. #define REGIDDETKEYDEGLITCH 0xAF
  64. #define REGIDDETKEYTIMER1 0xB0
  65. #define IDDETKEYDEGLITCH 0x0F
  66. #define IDDETKEYPRESSTIME 0xF0
  67. #define IDDETKEYLONGTIME 0x0F
  68. #define ENUHSR 0x01
  69. #define ENUHSL 0x02
  70. #define USBMICSEL 0x30
  71. #define ENCKLOL 0x80
  72. #define ENCKLOR 0x40
  73. #define ENCKLOLDM 0x20
  74. #define ENCKLOLDP 0x10
  75. #define ROUTE_CARKIT_HEADSET (ENCKLOL | ENCKLOR | ENCKLOLDM |\
  76. ENCKLOLDP)
  77. #define VUSBCTRL 0x82
  78. #define USBOTGCTRL 0x87
  79. #define REGIDDETCTRL1 0xA0
  80. #define REGIDDETCTRL2 0xA1
  81. #define REGIDDETCTRL3 0xA2
  82. #define REGIDDETSTATE 0xA7
  83. #define REGIDDETKEYLEVEL 0xA4
  84. #define MISSKEYPRESS 0xA5
  85. #define REGIDDETKEYTIMER2 0xB1
  86. #define REGIDDETKEYTIMER3 0xB2
  87. #define USBLINK1STATUS 0x94
  88. #define USBLINECTRL1 0x81
  89. #define USBDRVCTRL 0x74
  90. #define USBGAINMICSEL 0x75
  91. #define DAPATHCONF 0x09
  92. #define ENDACHSL 0x20
  93. #define ENDACHSR 0x10
  94. #define ENABLE 0x01
  95. #define DISABLE 0x00
  96. #define AB8505_SUPPLY_CONTROL 0x03
  97. #define LINK1_STATUS_MASK 0xF8
  98. #define REGIDDETVTH 0xA6
  99. #define IDDETFMDETCOMPHIENA 0x01
  100. #define IDDETFMDETLOENA 0x02
  101. #define IDDETFMDETREFPUENA 0x04
  102. #define IDDETPU200KVISENA 0x80
  103. #define FMCOMPENA (IDDETFMDETCOMPHIENA | IDDETFMDETLOENA\
  104. | IDDETFMDETREFPUENA)
  105. #define IDDETVTHDET619KENA 0x40
  106. #define IDDETVTHDET523KENA 0x20
  107. #define IDDETVTHDET440KENA 0x10
  108. #define IDDETVTHDET301KENA 0x08
  109. #define IDDETVTHDET255KENA 0x04
  110. #define IDDETVTHDET200KENA 0x02
  111. #define IDDETFMDETCOMPHI 0x40
  112. #define IDDETFMDETCOMPLO 0x20
  113. #define IDDETFMCOMPSTAT (IDDETFMDETCOMPHI | IDDETFMDETCOMPLO)
  114. #define SERVICESWCTRLENA 0x80
  115. #define SERVICEFORCEHZENA 0x40
  116. #define USBHSGAIN 0x73
  117. #define VUSBFLP 0x02
  118.  
  119. #define IDDETKEYCOMPPENA 0x01
  120. #define KEYPADSINGLECOMPMODE 0x02
  121. #define IDDETKEYRDREQ 0x08
  122. #define REGIDDETKEYTIMER3_MASK 0x03
  123. #define IDDETPU10K1V8ENA 0x08
  124. #define IDDETPU10KVADCENA 0x10
  125. #define IDDETPU1UENA 0x20
  126. #define IDIDETPLUGDETCOMPENA 0x08
  127. #define IDDETKEYCOMPNENA 0x02
  128. #define IDDETKEYNLEVEL 0x0F
  129. #define IDDETKEYPLEVEL 0xF0
  130. #define PLEVEL_500MV 0x30
  131. #define NLEVEL_200MV 0x01
  132. #define KEYPADENAFLANK 0x01
  133. #define IDDETKEYNEGPOSON 0x04
  134.  
  135. #define DETECTION_INTERVAL_MS 500
  136. #define BTN_DETECTION_RESET_INTERVAL_MS 800
  137.  
  138. struct key_interrupt_types {
  139. char *name;
  140. irqreturn_t (*function) (int, void*);
  141. int irq;
  142. };
  143.  
  144. static int (*acessory_func_list[])(struct usb_accessory_state *, bool);
  145. static int init_button_press_detection(struct device *dev, int connected);
  146.  
  147. static struct key_interrupt_types key_press_interrupts[];
  148. struct workqueue_struct *btn_detection_work_queue;
  149. static struct input_dev *usb_button;
  150.  
  151. static BLOCKING_NOTIFIER_HEAD(micro_usb_notifier_list);
  152.  
  153. static int key_glitch_time = KEY_DEBOUCE_TIME_50MS;
  154. static int key_press_time = KEY_PRESS_TIME_500MS;
  155. static int key_log_press_time = KEY_PRESS_TIME_1000MS;
  156.  
  157. int micro_usb_register_notifier(struct notifier_block *nb)
  158. {
  159. return blocking_notifier_chain_register(&micro_usb_notifier_list, nb);
  160. }
  161. EXPORT_SYMBOL(micro_usb_register_notifier);
  162.  
  163. int micro_usb_unregister_notifier(struct notifier_block *nb)
  164. {
  165. return blocking_notifier_chain_unregister(&micro_usb_notifier_list, nb);
  166. }
  167. EXPORT_SYMBOL(micro_usb_unregister_notifier);
  168.  
  169. void get_iddet_adc_val(struct kthread_work *work)
  170. {
  171. int id_voltage;
  172. struct usb_accessory_state *accessory
  173. = container_of(work, struct usb_accessory_state,
  174. read_adc_work);
  175. struct device *dev = accessory->dev;
  176. struct button_param_list *tmp = accessory->btn_param_list;
  177.  
  178. id_voltage = ab8500_gpadc_convert(accessory->gpadc, USB_ID);
  179. if (id_voltage < 0) {
  180. dev_err(dev, "GPADC read failed %d\n", id_voltage);
  181. return;
  182. }
  183.  
  184. dev_dbg(dev, "GPADC USB_ID read %d\n", id_voltage);
  185.  
  186. while (tmp->vmax != 0) {
  187. if ((id_voltage <= tmp->vmax) && (id_voltage >= tmp->vmin)) {
  188. dev_info(dev, "Button ID %d name %s\n", tmp->btn_id,
  189. tmp->btn_name);
  190. break;
  191. }
  192. tmp++;
  193. }
  194. }
  195.  
  196. static irqreturn_t key_glitch_interrupt_handler(int irq, void *data)
  197. {
  198. int ret;
  199. struct usb_accessory_state *accessory = data;
  200. struct device *dev = accessory->dev;
  201.  
  202. input_report_key(usb_button, KEY_MEDIA, 1);
  203. input_sync(usb_button);
  204. input_report_key(usb_button, KEY_MEDIA, 0);
  205. input_sync(usb_button);
  206.  
  207. /* IdDetKeyCompPEna Ena */
  208. ret = abx500_mask_and_set(dev, AB8505_CHARGER, MISSKEYPRESS,
  209. IDDETKEYCOMPPENA, IDDETKEYCOMPPENA);
  210. if (ret < 0) {
  211. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  212. goto handled;
  213. }
  214.  
  215. /* KeypadSingleCompMode Dis */
  216. ret = abx500_mask_and_set(dev, AB8505_CHARGER, REGIDDETCTRL3,
  217. KEYPADSINGLECOMPMODE, 0x00);
  218. if (ret < 0) {
  219. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  220. goto handled;
  221. }
  222.  
  223. queue_kthread_work(&accessory->kworker, &accessory->read_adc_work);
  224.  
  225. handled:
  226. return IRQ_HANDLED;
  227. }
  228.  
  229. static irqreturn_t key_press_interrupt_handler(int irq, void *data)
  230. {
  231. int ret;
  232. struct usb_accessory_state *accessory = data;
  233. struct device *dev = accessory->dev;
  234. u8 tmp1, tmp2;
  235. u16 iddetkeytime;
  236.  
  237. /* IdDetKeyRdReq Ena */
  238. ret = abx500_mask_and_set(dev, AB8505_CHARGER, MISSKEYPRESS,
  239. IDDETKEYRDREQ, IDDETKEYRDREQ);
  240. if (ret < 0) {
  241. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  242. goto handled;
  243. }
  244.  
  245. /* Read IdDetKeyTime[7:0] */
  246. ret = abx500_get(dev, AB8505_CHARGER, REGIDDETKEYTIMER2, &tmp1);
  247. if (ret < 0) {
  248. dev_err(dev, "%s read failed %d\n", __func__, __LINE__);
  249. goto handled;
  250. }
  251.  
  252. /* Read IdDetKeyTime[9:8] */
  253. ret = abx500_get(dev, AB8505_CHARGER, REGIDDETKEYTIMER3, &tmp2);
  254. if (ret < 0) {
  255. dev_err(dev, "%s read failed %d\n", __func__, __LINE__);
  256. goto handled;
  257. }
  258.  
  259. /* IdDetKeyRdReq Dis */
  260. ret = abx500_mask_and_set(dev, AB8505_CHARGER, MISSKEYPRESS,
  261. IDDETKEYRDREQ, 0x0);
  262. if (ret < 0) {
  263. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  264. goto handled;
  265. }
  266.  
  267. /* IdDetKeyTime[7:0] | IdDetKeyTime[9:8] */
  268. iddetkeytime = tmp1 | ((tmp2 & REGIDDETKEYTIMER3_MASK) << 8);
  269.  
  270. dev_dbg(dev, "Key Press Time read (%d * 12.5)ms\n", iddetkeytime);
  271. handled:
  272. return IRQ_HANDLED;
  273. }
  274.  
  275. /*TODO: Task needs to be defined for the below 3 handlers */
  276. static irqreturn_t long_key_press_interrupt_handler(int irq, void *data)
  277. {
  278. return IRQ_HANDLED;
  279. }
  280.  
  281. static irqreturn_t ikr_interrupt_handler(int irq, void *data)
  282. {
  283. return IRQ_HANDLED;
  284. }
  285.  
  286. static irqreturn_t key_stuck_interrupt_handler(int irq, void *data)
  287. {
  288. return IRQ_HANDLED;
  289. }
  290.  
  291. static struct key_interrupt_types key_press_interrupts[] = {
  292. {"KeyDeglitch", &key_glitch_interrupt_handler},
  293. {"KP", &key_press_interrupt_handler},
  294. {"IKP", &long_key_press_interrupt_handler},
  295. {"IKR", &ikr_interrupt_handler},
  296. {"KeyStuck", &key_stuck_interrupt_handler},
  297. };
  298.  
  299. static int key_press_interrupt(bool connected)
  300. {
  301. int i;
  302.  
  303. for (i = 0; i < sizeof(key_press_interrupts)/
  304. sizeof(key_press_interrupts[0]); i++) {
  305. if (connected)
  306. enable_irq(key_press_interrupts[i].irq);
  307. else
  308. disable_irq(key_press_interrupts[i].irq);
  309. }
  310.  
  311. return 0;
  312. };
  313.  
  314. static int uart_boot_off(struct usb_accessory_state *accessory, bool connected)
  315. {
  316. struct device *dev = accessory->dev;
  317.  
  318. dev_info(dev, "UART Boot-OFF %s\n",
  319. connected ? "PLUGGED" : "UNPLUGGED");
  320. acessory_func_list[USBSWITCH_UART](accessory, connected);
  321. return 0;
  322. }
  323.  
  324. static int uart_boot_on(struct usb_accessory_state *accessory, bool connected)
  325. {
  326. struct device *dev = accessory->dev;
  327.  
  328. dev_info(dev, "UART Boot-ON %s\n", connected ? "PLUGGED" : "UNPLUGGED");
  329. acessory_func_list[USBSWITCH_UART](accessory, connected);
  330. return 0;
  331. }
  332.  
  333. static int usb_boot_on(struct usb_accessory_state *accessory, bool connected)
  334. {
  335. int ret;
  336. struct device *dev = accessory->dev;
  337.  
  338. dev_info(dev, "USB Boot-ON %s\n", connected ? "PLUGGED" : "UNPLUGGED");
  339.  
  340. /* Service controlled by i2c */
  341. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  342. REGIDDETCTRL4, SERVICESWCTRLENA,
  343. connected ? SERVICESWCTRLENA : 0x0);
  344. if (ret < 0) {
  345. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  346. return ret;
  347. }
  348.  
  349. /* Service is forced Hig hZ */
  350. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  351. REGIDDETCTRL4, SERVICEFORCEHZENA,
  352. connected ? SERVICEFORCEHZENA : 0x0);
  353. if (ret < 0) {
  354. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  355. return ret;
  356. }
  357.  
  358. blocking_notifier_call_chain(&micro_usb_notifier_list,
  359. connected ? USB_BOOT_ON_PLUGGED :
  360. USB_BOOT_ON_UNPLUGGED, NULL);
  361.  
  362. return ret;
  363. }
  364.  
  365. static int usb_boot_off(struct usb_accessory_state *accessory, bool connected)
  366. {
  367. int ret;
  368. struct device *dev = accessory->dev;
  369.  
  370. dev_info(dev, "USB Boot-OFF %s\n", connected ? "PLUGGED" : "UNPLUGGED");
  371.  
  372. /* Service controlled by i2c */
  373. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  374. REGIDDETCTRL4, SERVICESWCTRLENA,
  375. connected ? SERVICESWCTRLENA : 0x0);
  376. if (ret < 0) {
  377. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  378. return ret;
  379. }
  380.  
  381. /* Service is forced Hig hZ */
  382. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  383. REGIDDETCTRL4, SERVICEFORCEHZENA,
  384. 0x0);
  385. if (ret < 0) {
  386. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  387. return ret;
  388. }
  389.  
  390. blocking_notifier_call_chain(&micro_usb_notifier_list,
  391. connected ? USB_BOOT_OFF_PLUGGED :
  392. USB_BOOT_OFF_UNPLUGGED, NULL);
  393.  
  394. return ret;
  395. }
  396.  
  397. static int tty_converter(struct usb_accessory_state *accessory, bool connected)
  398. {
  399. int ret;
  400. struct device *dev = accessory->dev;
  401. static unsigned char usbhsgain;
  402. static unsigned char eargainmicsel;
  403.  
  404. dev_info(dev, "TTY Device %s\n", connected ? "PLUGGED" : "UNPLUGGED");
  405.  
  406. /* Vbus OVP external switch force opened */
  407. ret = abx500_mask_and_set(dev, AB8505_CHARGER, REGIDDETCTRL3, USBSWCTRL,
  408. connected ? USBSWCTRL : 0x0);
  409. if (ret < 0) {
  410. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  411. return ret;
  412. }
  413.  
  414. if (connected) {
  415. /* Take backup and Set Left,Right Usb Headset analog gain */
  416. ret = abx500_get(dev, AB8505_AUDIO, USBHSGAIN, &usbhsgain);
  417. if (ret < 0) {
  418. dev_err(dev, "%s read failed %d\n", __func__, __LINE__);
  419. return ret;
  420. }
  421.  
  422. ret = abx500_set(dev, AB8505_AUDIO, USBHSGAIN, 0x0);
  423. if (ret < 0) {
  424. dev_err(dev, "%s write failed %d\n", __func__,
  425. __LINE__);
  426. return ret;
  427. }
  428. } else {
  429. ret = abx500_set(dev, AB8505_AUDIO, USBHSGAIN, usbhsgain);
  430. if (ret < 0) {
  431. dev_err(dev, "%s write failed %d\n", __func__,
  432. __LINE__);
  433. return ret;
  434. }
  435. }
  436.  
  437. /* Enable Left and Right USB Headset */
  438. ret = abx500_set(dev, AB8505_AUDIO, USBDRVCTRL,
  439. connected ? ENUHSR | ENUHSL : 0xC);
  440. if (ret < 0) {
  441. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  442. return ret;
  443. }
  444.  
  445. if (connected) {
  446. ret = abx500_get(dev, AB8505_AUDIO, USBGAINMICSEL,
  447. &eargainmicsel);
  448. if (ret < 0) {
  449. dev_err(dev, "%s get failed %d\n", __func__, __LINE__);
  450. return ret;
  451. }
  452.  
  453. /* DP ball is selected as usb microphone input */
  454. ret = abx500_mask_and_set(dev, AB8505_AUDIO,
  455. USBGAINMICSEL, USBMICSEL, 0x20);
  456. if (ret < 0) {
  457. dev_err(dev, "%s write failed %d\n", __func__,
  458. __LINE__);
  459. return ret;
  460. }
  461. } else {
  462. ret = abx500_set(dev, AB8505_AUDIO, USBGAINMICSEL,
  463. eargainmicsel);
  464. if (ret < 0) {
  465. dev_err(dev, "%s write failed %d\n", __func__,
  466. __LINE__);
  467. return ret;
  468. }
  469. }
  470.  
  471. /* Enable Vusb */
  472. ret = abx500_mask_and_set(dev, AB8505_SUPPLY_CONTROL,
  473. VUSBCTRL, VUSBENA | VUSBFLP,
  474. connected ? VUSBENA | VUSBFLP : 0x0);
  475. if (ret < 0)
  476. dev_err(dev, "%s write failed %d\n", __func__,
  477. __LINE__);
  478.  
  479. return ret;
  480. }
  481.  
  482. static int init_button_press_detection(struct device *dev, int connected)
  483. {
  484. int ret;
  485.  
  486. /* IDDetPu10k1v8Ena Ena & IDDetPu10kVadcEna Dis & IDDetPu1uEna Ena */
  487. ret = abx500_mask_and_set(dev, AB8505_CHARGER, REGIDDETCTRL2,
  488. IDDETPU10K1V8ENA | IDDETPU10KVADCENA | IDDETPU1UENA,
  489. connected ? IDDETPU1UENA | IDDETPU10K1V8ENA : 0x0);
  490. if (ret < 0) {
  491. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  492. return ret;
  493. }
  494.  
  495. /* IDDetPlugDetCompEna Ena */
  496. ret = abx500_mask_and_set(dev, AB8505_CHARGER, REGIDDETCTRL1,
  497. IDIDETPLUGDETCOMPENA, connected ?
  498. IDIDETPLUGDETCOMPENA : 0x0);
  499. if (ret < 0) {
  500. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  501. return ret;
  502. }
  503.  
  504. /* IDDetKeyCompNEna Ena & IDDetKeyCompPEna Dis */
  505. ret = abx500_mask_and_set(dev, AB8505_CHARGER, MISSKEYPRESS,
  506. IDDETKEYCOMPNENA | IDDETKEYCOMPPENA, connected ?
  507. IDDETKEYCOMPNENA : 0x0);
  508. if (ret < 0) {
  509. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  510. return ret;
  511. }
  512.  
  513. /* IdDetKeyNlevel[3:0] & IdDetKeyPlevel[7:4] */
  514. ret = abx500_mask_and_set(dev, AB8505_CHARGER, REGIDDETKEYLEVEL,
  515. IDDETKEYNLEVEL | IDDETKEYPLEVEL, connected ?
  516. PLEVEL_500MV | NLEVEL_200MV : 0x0);
  517. if (ret < 0) {
  518. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  519. return ret;
  520. }
  521.  
  522. /* IdDetKeyPressTime[7:4] */
  523. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  524. REGIDDETKEYTIMER1, IDDETKEYPRESSTIME,
  525. key_press_time << 4);
  526. if (ret < 0) {
  527. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  528. return ret;
  529. }
  530.  
  531. /* IdDetKeyLongTime[3:0] */
  532. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  533. REGIDDETKEYTIMER1, IDDETKEYLONGTIME,
  534. key_log_press_time);
  535. if (ret < 0) {
  536. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  537. return ret;
  538. }
  539.  
  540. /* set key glitch time */
  541. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  542. REGIDDETKEYDEGLITCH, IDDETKEYDEGLITCH,
  543. key_glitch_time);
  544. if (ret < 0) {
  545. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  546. return ret;
  547. }
  548.  
  549. /* KeypadSingleCompMode Ena & KeypadEnaFlank Dis */
  550. ret = abx500_mask_and_set(dev, AB8505_CHARGER, REGIDDETCTRL3,
  551. KEYPADSINGLECOMPMODE | KEYPADENAFLANK,
  552. KEYPADSINGLECOMPMODE);
  553. if (ret < 0) {
  554. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  555. return ret;
  556. }
  557.  
  558. /* IdDetKeyNegPosOn Dis */
  559. ret = abx500_mask_and_set(dev, AB8505_CHARGER, MISSKEYPRESS,
  560. IDDETKEYNEGPOSON, 0x0);
  561. if (ret < 0)
  562. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  563.  
  564. return ret;
  565. }
  566.  
  567. static void button_detection_function(struct work_struct *work)
  568. {
  569. int ret;
  570. u8 id_voltage;
  571. struct usb_accessory_state *accessory = container_of(work,
  572. struct usb_accessory_state, detect_button.work);
  573. struct device *dev = accessory->dev;
  574.  
  575. /* IdDetKeyNegPosOn Dis */
  576. ret = abx500_mask_and_set(dev, AB8505_CHARGER, MISSKEYPRESS,
  577. IDDETKEYNEGPOSON, 0x00);
  578. if (ret < 0) {
  579. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  580. goto btn_det_restart;
  581. }
  582.  
  583. /* IDDetPu10k1v8Ena Dis Here */
  584. ret = abx500_mask_and_set(dev, AB8505_CHARGER, REGIDDETCTRL2,
  585. IDDETPU10K1V8ENA, 0x0);
  586. if (ret < 0) {
  587. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  588. goto btn_det_restart;
  589. }
  590.  
  591. /* read Iddet comparator state */
  592. ret = abx500_get(dev, AB8505_CHARGER,
  593. REGIDDETSTATE, &id_voltage);
  594. if (ret < 0) {
  595. dev_err(dev, "%s read failed %d\n", __func__, __LINE__);
  596. goto btn_det_restart;
  597. }
  598.  
  599. /* Check if the cable was UnPluged */
  600. if (!(id_voltage & IDDETPLUGDETCOMP)) {
  601. queue_delayed_work(accessory->iddet_workqueue,
  602. &accessory->cable_detection,
  603. msecs_to_jiffies(DETECTION_INTERVAL_MS));
  604.  
  605. acessory_func_list[accessory->cable_last_detected]
  606. (accessory, false);
  607.  
  608. accessory->cable_last_detected = USBSWITCH_NONE;
  609. return;
  610. }
  611.  
  612. /*TODO:ForABcut2: IDDetPu10k1v8Ena Ena Here */
  613.  
  614. /* IdDetKeyNegPosOn Ena */
  615. ret = abx500_mask_and_set(dev, AB8505_CHARGER, MISSKEYPRESS,
  616. IDDETKEYNEGPOSON, IDDETKEYNEGPOSON);
  617. if (ret < 0) {
  618. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  619. goto btn_det_restart;
  620. }
  621.  
  622. btn_det_restart:
  623. queue_delayed_work(btn_detection_work_queue, &accessory->detect_button,
  624. msecs_to_jiffies(BTN_DETECTION_RESET_INTERVAL_MS));
  625. }
  626.  
  627. static int audio_dev_type1(struct usb_accessory_state *accessory,
  628. bool connected)
  629. {
  630. int ret;
  631. struct device *dev = accessory->dev;
  632. static unsigned char eargainmicsel;
  633.  
  634. dev_info(dev, "Audio Device %s\n", connected ? "PLUGGED" : "UNPLUGGED");
  635.  
  636. /* Vbus OVP external switch force opened */
  637. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  638. REGIDDETCTRL3, USBSWCTRL | USBCPMICSUBCLKENA,
  639. connected ? USBSWCTRL | USBCPMICSUBCLKENA : 0x0);
  640. if (ret < 0) {
  641. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  642. return ret;
  643. }
  644.  
  645. /* Enable Left and Right USB Headset */
  646. ret = abx500_set(dev, AB8505_AUDIO, USBDRVCTRL,
  647. connected ? ENUHSR | ENUHSL : 0xC);
  648. if (ret < 0) {
  649. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  650. return ret;
  651. }
  652.  
  653. if (connected) {
  654. ret = abx500_get(dev, AB8505_AUDIO,
  655. USBGAINMICSEL, &eargainmicsel);
  656. if (ret < 0) {
  657. dev_err(dev, "%s read failed %d\n", __func__, __LINE__);
  658. return ret;
  659. }
  660.  
  661. /* DP ball is selected as usb microphone input */
  662. ret = abx500_mask_and_set(dev, AB8505_AUDIO,
  663. USBGAINMICSEL, USBMICSEL, 0x20);
  664. if (ret < 0) {
  665. dev_err(dev, "%s write failed %d\n", __func__,
  666. __LINE__);
  667. return ret;
  668. }
  669. } else {
  670. ret = abx500_set(dev, AB8505_AUDIO,
  671. USBGAINMICSEL, eargainmicsel);
  672. if (ret < 0) {
  673. dev_err(dev, "%s write failed %d\n", __func__,
  674. __LINE__);
  675. return ret;
  676. }
  677. }
  678.  
  679. /* Enable VusbEna and VusbFlp */
  680. ret = abx500_mask_and_set(dev, AB8505_SUPPLY_CONTROL,
  681. VUSBCTRL, VUSBENA | VUSBFLP,
  682. connected ? VUSBENA | VUSBFLP : 0x0);
  683. if (ret < 0) {
  684. dev_err(dev, "%s write failed %d\n", __func__,
  685. __LINE__);
  686. return ret;
  687. }
  688.  
  689. set_android_switch_state(connected);
  690.  
  691. init_button_press_detection(dev, connected);
  692.  
  693. key_press_interrupt(connected);
  694.  
  695. /* IdDetKeyNegPosOn Ena */
  696. ret = abx500_mask_and_set(dev, AB8505_CHARGER, MISSKEYPRESS,
  697. IDDETKEYNEGPOSON, connected ? IDDETKEYNEGPOSON : 0x0);
  698. if (ret < 0) {
  699. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  700. return ret;
  701. }
  702.  
  703. if (connected)
  704. queue_delayed_work(btn_detection_work_queue,
  705. &accessory->detect_button,
  706. msecs_to_jiffies
  707. (BTN_DETECTION_RESET_INTERVAL_MS));
  708. return ret;
  709. }
  710.  
  711. static int cable_unknown(struct usb_accessory_state *accessory, bool connected)
  712. {
  713. struct device *dev = accessory->dev;
  714. dev_warn(dev, "Unknown Cable %s\n", connected ? "PLUGGED" :
  715. "UNPLUGGED");
  716. return 0;
  717. }
  718.  
  719. static int legacy_charger(struct usb_accessory_state *accessory, bool connected)
  720. {
  721. struct device *dev = accessory->dev;
  722. dev_info(dev, "Legacy charger %s\n", connected ? "PLUGGED" :
  723. "UNPLUGGED");
  724. blocking_notifier_call_chain(&micro_usb_notifier_list,
  725. connected ? LEGACY_CHARGER_PLUGGED :
  726. LEGACY_CHARGER_UNPLUGGED, NULL);
  727. return 0;
  728. }
  729.  
  730. /* Callbacks for the type of cable connected */
  731. static int (*acessory_func_list[])(struct usb_accessory_state *, bool) = {
  732. [USBSWITCH_UART_BOOT_ON] = uart_boot_on,
  733. [USBSWITCH_UART_BOOT_OFF] = uart_boot_off,
  734. [USBSWITCH_USB_BOOT_ON] = usb_boot_on,
  735. [USBSWITCH_USB_BOOT_OFF] = usb_boot_off,
  736. [USBSWITCH_AUDIODEV_TYPE1] = audio_dev_type1,
  737. [USBSWITCH_TTY_CONV] = tty_converter,
  738. [USBSWITCH_UART] = NULL,
  739. [USBSWITCH_LEGACY_CHARGER] = legacy_charger,
  740. [USBSWITCH_UNKNOWN] = cable_unknown,
  741. };
  742.  
  743. /*
  744.  * Detec the cable connected depending on the voltage across the
  745.  * ID line.
  746.  */
  747. static int detect_depending_on_id_resistance(struct usb_accessory_state
  748. *accessory)
  749. {
  750. int ret;
  751. struct cust_rid_adcid *p;
  752. int id_voltage;
  753. struct device *dev = accessory->dev;
  754.  
  755. /* 1microAmp current source and 200k pull up enable */
  756. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  757. REGIDDETCTRL2, IDDETPU200K18VENA,
  758. IDDETPU200K18VENA);
  759. if (ret < 0) {
  760. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  761. return ret;
  762. }
  763.  
  764. /* Connect ID line to GPADC input */
  765. ret = abx500_mask_and_set(dev, AB8505_USB,
  766. USBLINECTRL1, IDDETADCENA, IDDETADCENA);
  767. if (ret < 0) {
  768. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  769. return ret;
  770. }
  771.  
  772. id_voltage = ab8500_gpadc_convert(accessory->gpadc, USB_ID);
  773. dev_dbg(dev, "USB-ID %d\n", id_voltage);
  774. if (id_voltage) {
  775. p = accessory->cables_param_list;
  776. while (p->max != 0) {
  777. if ((id_voltage >= p->min) && (id_voltage <= p->max)) {
  778. dev_dbg(dev, "Matched %d\n", p->cable_id);
  779. accessory->cable_detected = p->cable_id;
  780. goto detected;
  781. }
  782. p++;
  783. }
  784. }
  785.  
  786. accessory->cable_detected = USBSWITCH_UNKNOWN;
  787.  
  788. detected:
  789. /* 1microAmp current source and 200k pull up disable */
  790. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  791. REGIDDETCTRL2, IDDETPU200K18VENA, 0);
  792. if (ret < 0) {
  793. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  794. return ret;
  795. }
  796.  
  797. /* Disconnect ID line to GPADC input */
  798. ret = abx500_mask_and_set(dev, AB8505_USB,
  799. USBLINECTRL1, IDDETADCENA, 0);
  800. if (ret < 0)
  801. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  802.  
  803. return ret;
  804. }
  805.  
  806. /*
  807.  * set the registers for scaning the USB device connected
  808.  * here HW detection of the device connected is disabled.
  809.  */
  810. static int usb_switch_init(struct device *dev)
  811. {
  812. int ret;
  813.  
  814. /* Disable host, device detection and enable VBUS Valid comporator */
  815. ret = abx500_mask_and_set(dev, AB8505_USB,
  816. USBOTGCTRL, IDHOSTENA | IDDEVENA |
  817. VBUSVALIDENA, 0x0);
  818. if (ret < 0) {
  819. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  820. return ret;
  821. }
  822.  
  823. /* Set iddet IP SW controllable */
  824. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  825. REGIDDETCTRL3, IDDETSWCTRLENA, IDDETSWCTRLENA);
  826. if (ret < 0) {
  827. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  828. return ret;
  829. }
  830.  
  831. /* Turn off USB PHY */
  832. ret = abx500_mask_and_set(dev, AB8505_USB, USBPHYCTRL,
  833. USBDEVICEMODEENA | USBHOSTMODEENA, 0x0);
  834. if (ret < 0)
  835. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  836.  
  837. return ret;
  838. }
  839.  
  840. /* Check if the connected accessory has ID-USB resistance */
  841. static int plug_unplug_monitor_init(struct device *dev)
  842. {
  843. int ret;
  844. unsigned char id_voltage;
  845.  
  846. /* 1microAmp current source pull up enable */
  847. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  848. REGIDDETCTRL2, IDDETPU1ENA, IDDETPU1ENA);
  849. if (ret < 0) {
  850. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  851. return ret;
  852. }
  853.  
  854. /* Enable ID detect comparator */
  855. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  856. REGIDDETCTRL1, PLUGDETCOMPENA, PLUGDETCOMPENA);
  857. if (ret < 0) {
  858. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  859. return ret;
  860. }
  861.  
  862. /* read Iddet comparator state */
  863. ret = abx500_get(dev, AB8505_CHARGER,
  864. REGIDDETSTATE, &id_voltage);
  865. if (ret < 0) {
  866. dev_err(dev, "%s read failed %d\n", __func__, __LINE__);
  867. return ret;
  868. }
  869.  
  870. /* 1microAmp current source pull up disable */
  871. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  872. REGIDDETCTRL2, IDDETPU1ENA, 0x0);
  873. if (ret < 0) {
  874. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  875. return ret;
  876. }
  877.  
  878. /* Disable ID detect comparator */
  879. ret = abx500_mask_and_set(dev, AB8505_CHARGER,
  880. REGIDDETCTRL1, PLUGDETCOMPENA, 0x0);
  881. if (ret < 0) {
  882. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  883. return ret;
  884. }
  885.  
  886. dev_dbg(dev, "Anything connected with ID-USB resistance? %s\n",
  887. (id_voltage & IDDETPLUGDETCOMP) ?
  888. "true" : "false");
  889.  
  890. /* Return ID-USB resistance detected or not */
  891. return (ret < 0) ? ret : !!(id_voltage & IDDETPLUGDETCOMP);
  892. }
  893.  
  894. /*
  895.  * Work function to detect the uUSB cable plug/unplug.
  896.  * On detecting a cable, cable specific call back is called to do the
  897.  * cable specific initializations and on unplug of the same cable the
  898.  * initializations done are undone.
  899.  */
  900. static void micro_usb_accessory_detect(struct work_struct *work)
  901. {
  902. int ret;
  903. unsigned char vbusdet;
  904. bool restore_regs = true;
  905. static unsigned char usbotgctrl, usbphyctrl;
  906. unsigned char usblink1status;
  907. struct usb_accessory_state *accessory
  908. = container_of(work, struct usb_accessory_state,
  909. cable_detection.work);
  910. struct device *dev = accessory->dev;
  911.  
  912. ret = abx500_get(dev, AB8505_USB, USBLINK1STATUS, &usblink1status);
  913. if (ret < 0) {
  914. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  915. goto restart;
  916. }
  917.  
  918. /* Skip detection if UsbLink1Status[4:0] is set & != 0x80 */
  919. if ((usblink1status & 0xF8) && (usblink1status != 0x80))
  920. goto restart;
  921.  
  922. /*
  923. * On Audio device connection, stop the cable unplug detection.
  924. * Audio device routine, will do the unplg detection and will
  925. * queue iddet_workqueue on Audio device Unplug.
  926. */
  927. if (accessory->cable_last_detected == USBSWITCH_AUDIODEV_TYPE1)
  928. return;
  929.  
  930. if (accessory->cable_last_detected == USBSWITCH_UART_BOOT_ON ||
  931. accessory->cable_last_detected ==
  932. USBSWITCH_UART_BOOT_OFF ||
  933. accessory->cable_last_detected ==
  934. USBSWITCH_UART)
  935. return;
  936.  
  937. /*
  938. * If already a cable was detected, skip to id resistance change
  939. * or vbus change detection direclty, i.e for unplug detection.
  940. */
  941. if (accessory->cable_last_detected != USBSWITCH_NONE) {
  942. restore_regs = false;
  943. goto detect_unplug;
  944. }
  945.  
  946. accessory->cable_detected = USBSWITCH_NONE;
  947.  
  948. /* Take a backup of the registers, about to be changed */
  949. ret = abx500_get(dev, AB8505_USB, USBOTGCTRL, &usbotgctrl);
  950. if (ret < 0) {
  951. dev_err(dev, "%s read failed %d\n", __func__, __LINE__);
  952. goto restart;
  953. }
  954.  
  955. ret = abx500_get(dev, AB8505_USB, USBPHYCTRL, &usbphyctrl);
  956. if (ret < 0) {
  957. dev_err(dev, "%s read failed %d\n", __func__, __LINE__);
  958. goto restart;
  959. }
  960.  
  961. ret = usb_switch_init(dev);
  962. if (ret < 0)
  963. goto restore;
  964.  
  965. detect_unplug:
  966. /* read VbusDet state */
  967. ret = abx500_get(dev, AB8505_INTERRUPT, ITSOURCE2, &vbusdet);
  968. if (ret < 0) {
  969. dev_err(dev, "%s read failed %d\n", __func__, __LINE__);
  970. goto restore;
  971. }
  972.  
  973. vbusdet &= VBUSDET;
  974.  
  975. ret = plug_unplug_monitor_init(dev);
  976. if (!ret) {
  977. /*
  978. * No ID Pin resistance detected.
  979. * This can happen when
  980. * 1 ) Connected cable is removed. cable_last_detected
  981. * is not NONE.
  982. * 2 ) Cable was never connected.
  983. * 3 ) Cable with no resistance is conencted and is not
  984. * detected by the link1status registor. If vbus
  985. * is detected then it is a carkit charger, with D+, D-,
  986. * ID pin floating.
  987. */
  988. if (vbusdet) {
  989. accessory->cable_detected =
  990. USBSWITCH_LEGACY_CHARGER;
  991. if (accessory->cable_last_detected !=
  992. USBSWITCH_LEGACY_CHARGER)
  993. goto done;
  994. else
  995. goto restart;
  996. }
  997.  
  998. if (accessory->cable_last_detected != USBSWITCH_NONE) {
  999. acessory_func_list[accessory->cable_last_detected]
  1000. (accessory, false);
  1001. accessory->cable_last_detected = USBSWITCH_NONE;
  1002. }
  1003.  
  1004. goto restore;
  1005. }
  1006.  
  1007. ret = detect_depending_on_id_resistance(accessory);
  1008. if (ret < 0)
  1009. goto restore;
  1010.  
  1011. done:
  1012. dev_dbg(dev, "Cable ID Detected present %d last %d\n",
  1013. accessory->cable_detected,
  1014. accessory->cable_last_detected);
  1015.  
  1016. if (accessory->cable_detected == accessory->cable_last_detected)
  1017. goto restart;
  1018.  
  1019. if (accessory->cable_last_detected != USBSWITCH_NONE)
  1020. /* Call restore callback for the last connected device */
  1021. acessory_func_list[accessory->cable_last_detected](accessory,
  1022. false);
  1023.  
  1024. accessory->cable_last_detected = accessory->cable_detected;
  1025.  
  1026. /* Do cable specific plug related work */
  1027. acessory_func_list[accessory->cable_detected](accessory, true);
  1028.  
  1029. goto restart;
  1030.  
  1031. restore:
  1032. /* Restore back registers */
  1033. ret = abx500_set(dev, AB8505_USB, USBOTGCTRL, usbotgctrl);
  1034. if (ret < 0)
  1035. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  1036.  
  1037. ret = abx500_set(dev, AB8505_USB, USBPHYCTRL, usbphyctrl);
  1038. if (ret < 0)
  1039. dev_err(dev, "%s write failed %d\n", __func__, __LINE__);
  1040. restart:
  1041. queue_delayed_work(accessory->iddet_workqueue,
  1042. &accessory->cable_detection,
  1043. msecs_to_jiffies(DETECTION_INTERVAL_MS));
  1044. }
  1045.  
  1046. static int init_key_press(struct platform_device *pdev,
  1047. struct usb_accessory_state *accessory)
  1048. {
  1049. int i;
  1050. int irq;
  1051. int ret;
  1052.  
  1053. for (i = 0; i < sizeof(key_press_interrupts)/
  1054. sizeof(key_press_interrupts[0]); i++) {
  1055.  
  1056. irq = platform_get_irq_byname(pdev,
  1057. key_press_interrupts[i].name);
  1058. if (irq < 0) {
  1059. dev_err(&pdev->dev,
  1060. "%s: Failed to get irq %s\n", __func__,
  1061. key_press_interrupts[i].name);
  1062. return irq;
  1063. }
  1064.  
  1065. ret = request_threaded_irq(irq, NULL,
  1066. key_press_interrupts[i].function,
  1067. IRQF_TRIGGER_FALLING,
  1068. key_press_interrupts[i].name,
  1069. accessory);
  1070. if (ret != 0) {
  1071. dev_err(&pdev->dev,
  1072. "%s: Failed to claim irq %s (%d)\n",
  1073. __func__,
  1074. key_press_interrupts[i].name,
  1075. ret);
  1076. return ret;
  1077. }
  1078.  
  1079. key_press_interrupts[i].irq = irq;
  1080. disable_irq(key_press_interrupts[i].irq);
  1081. }
  1082.  
  1083. return 0;
  1084. }
  1085.  
  1086. /*
  1087.  * create input device for button press reporting
  1088.  */
  1089. static int init_button(struct usb_accessory_state *accessory)
  1090. {
  1091. int err;
  1092.  
  1093. usb_button = input_allocate_device();
  1094. if (!usb_button) {
  1095. dev_err(accessory->dev, "Input device alloc failed.\n");
  1096. return -ENOMEM;
  1097. }
  1098.  
  1099. input_set_capability(usb_button, EV_KEY, KEY_MEDIA);
  1100.  
  1101. usb_button->name = "uUSB button";
  1102. usb_button->uniq = "uUSBbtn";
  1103. usb_button->dev.parent = accessory->dev;
  1104.  
  1105. err = input_register_device(usb_button);
  1106. if (err) {
  1107. dev_err(accessory->dev, "Input device registration failed %d",
  1108. err);
  1109. input_free_device(usb_button);
  1110. usb_button = NULL;
  1111. }
  1112.  
  1113. return err;
  1114. }
  1115.  
  1116. static int __devinit ab8505_iddet_probe(struct platform_device *pdev)
  1117. {
  1118. int ret = 0;
  1119. /* Set the thread as a priority one, with MAX_RT_PRIO-5 */
  1120. struct sched_param param = { .sched_priority = MAX_RT_PRIO-5 };
  1121. struct ab8500_platform_data *plat;
  1122. struct ab8505_iddet_platdata *pdata;
  1123. struct usb_accessory_state *accessory;
  1124.  
  1125. accessory = kzalloc(sizeof(*accessory), GFP_KERNEL);
  1126. if (!accessory) {
  1127. dev_err(&pdev->dev, "alloc failed\n");
  1128. return -ENOMEM;
  1129. }
  1130.  
  1131. accessory->gpadc = ab8500_gpadc_get();
  1132. if (!accessory->gpadc) {
  1133. ret = -ENODEV;
  1134. goto gpadc_get_fail;
  1135.  
  1136. }
  1137.  
  1138. accessory->dev = &pdev->dev;
  1139. plat = dev_get_platdata(pdev->dev.parent);
  1140. pdata = plat->iddet;
  1141. accessory->cables_param_list = pdata->adc_id_list;
  1142. acessory_func_list[USBSWITCH_UART] = pdata->uart_cable;
  1143. accessory->btn_param_list = pdata->btn_list;
  1144.  
  1145. ret = init_key_press(pdev, accessory);
  1146. if (ret < 0) {
  1147. dev_err(&pdev->dev, "key press interrupt init failed\n");
  1148. goto key_init_fail;
  1149. }
  1150.  
  1151. ret = init_button(accessory);
  1152. if (ret < 0) {
  1153. dev_err(&pdev->dev, "Button init failed %d\n", ret);
  1154. goto btn_init_fail;
  1155. }
  1156.  
  1157. /* Cable Plug/Unplug detection thread */
  1158. INIT_DELAYED_WORK(&accessory->cable_detection,
  1159. micro_usb_accessory_detect);
  1160.  
  1161. accessory->iddet_workqueue =
  1162. create_singlethread_workqueue("iddet_btn_detect");
  1163. if (!accessory->iddet_workqueue) {
  1164. dev_err(&pdev->dev, "%s: Failed to create wq\n", __func__);
  1165. ret = -ENOMEM;
  1166. goto create_iddet_worqueue_fail;
  1167. }
  1168.  
  1169. INIT_DELAYED_WORK(&accessory->detect_button, button_detection_function);
  1170.  
  1171. btn_detection_work_queue =
  1172. create_singlethread_workqueue("iddet_btn_detect");
  1173. if (!btn_detection_work_queue) {
  1174. dev_err(&pdev->dev, "%s: Failed to create wq\n", __func__);
  1175. ret = -ENOMEM;
  1176. goto create_btn_detection_fail;
  1177. }
  1178.  
  1179. /*
  1180. * Thread to read the gpadc value, needed to detect the button on
  1181. * the headset pressed. Since the GPADC read depends on a AB interrupt,
  1182. * the GPADC cannot be read here in the key_press AB interrupt. So a
  1183. * HIGH Priotiry thread is created.
  1184. */
  1185. init_kthread_worker(&accessory->kworker);
  1186. accessory->gpadc_read_thread = kthread_run(kthread_worker_fn,
  1187. &accessory->kworker,
  1188. dev_name(accessory->dev));
  1189. if (IS_ERR(accessory->gpadc_read_thread)) {
  1190. dev_err(&pdev->dev,
  1191. "failed to create id detect thread\n");
  1192. ret = -ENOMEM;
  1193. goto kthread_err;
  1194. }
  1195.  
  1196. /*
  1197. * The key press may last for a very small time, it needs to be read
  1198. * quickly so make GPADC reading thread HIGHer priority one.
  1199. */
  1200. sched_setscheduler(accessory->gpadc_read_thread, SCHED_FIFO, &param);
  1201.  
  1202. init_kthread_work(&accessory->read_adc_work,
  1203. get_iddet_adc_val);
  1204.  
  1205. accessory->cable_last_detected = USBSWITCH_NONE;
  1206.  
  1207. platform_set_drvdata(pdev, accessory);
  1208.  
  1209. queue_delayed_work(accessory->iddet_workqueue,
  1210. &accessory->cable_detection,
  1211. msecs_to_jiffies(DETECTION_INTERVAL_MS));
  1212.  
  1213. return 0;
  1214.  
  1215. kthread_err:
  1216. flush_workqueue(btn_detection_work_queue);
  1217. destroy_workqueue(btn_detection_work_queue);
  1218. create_btn_detection_fail:
  1219. flush_workqueue(accessory->iddet_workqueue);
  1220. destroy_workqueue(accessory->iddet_workqueue);
  1221. create_iddet_worqueue_fail:
  1222. btn_init_fail:
  1223. key_init_fail:
  1224. gpadc_get_fail:
  1225. kfree(accessory);
  1226. return ret;
  1227. }
  1228.  
  1229. static int __devexit ab8505_id_remove(struct platform_device *pdev)
  1230. {
  1231. struct usb_accessory_state *accessory;
  1232.  
  1233. accessory = platform_get_drvdata(pdev);
  1234. hrtimer_cancel(&accessory->timer);
  1235. flush_kthread_worker(&accessory->kworker);
  1236. kthread_stop(accessory->gpadc_read_thread);
  1237. kfree(accessory);
  1238.  
  1239. return 0;
  1240. }
  1241.  
  1242. static struct platform_driver ab8505_iddet_driver = {
  1243. .driver = {
  1244. .owner = THIS_MODULE,
  1245. .name = "ab-iddet",
  1246. },
  1247. .probe = ab8505_iddet_probe,
  1248. .remove = __devexit_p(ab8505_id_remove),
  1249. };
  1250.  
  1251. static int __init ab8505_iddet_init(void)
  1252. {
  1253. return platform_driver_register(&ab8505_iddet_driver);
  1254. }
  1255.  
  1256. static void __exit ab8505_iddet_exit(void)
  1257. {
  1258. platform_driver_unregister(&ab8505_iddet_driver);
  1259. }
  1260.  
  1261. late_initcall(ab8505_iddet_init);
  1262. module_exit(ab8505_iddet_exit);
  1263.  
  1264. MODULE_LICENSE("GPL v2");
  1265. MODULE_ALIAS("micro usb accessory detection");
  1266.