PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/input/misc/ab8505_micro_usb_iddet.c

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