extcon-max8997.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * Donggeun Kim <dg77.kim@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/err.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/kobject.h>
  25. #include <linux/mfd/max8997.h>
  26. #include <linux/mfd/max8997-private.h>
  27. #include <linux/extcon.h>
  28. #include <linux/irqdomain.h>
  29. #define DEV_NAME "max8997-muic"
  30. #define DELAY_MS_DEFAULT 20000 /* unit: millisecond */
  31. enum max8997_muic_adc_debounce_time {
  32. ADC_DEBOUNCE_TIME_0_5MS = 0, /* 0.5ms */
  33. ADC_DEBOUNCE_TIME_10MS, /* 10ms */
  34. ADC_DEBOUNCE_TIME_25MS, /* 25ms */
  35. ADC_DEBOUNCE_TIME_38_62MS, /* 38.62ms */
  36. };
  37. struct max8997_muic_irq {
  38. unsigned int irq;
  39. const char *name;
  40. unsigned int virq;
  41. };
  42. static struct max8997_muic_irq muic_irqs[] = {
  43. { MAX8997_MUICIRQ_ADCError, "muic-ADCERROR" },
  44. { MAX8997_MUICIRQ_ADCLow, "muic-ADCLOW" },
  45. { MAX8997_MUICIRQ_ADC, "muic-ADC" },
  46. { MAX8997_MUICIRQ_VBVolt, "muic-VBVOLT" },
  47. { MAX8997_MUICIRQ_DBChg, "muic-DBCHG" },
  48. { MAX8997_MUICIRQ_DCDTmr, "muic-DCDTMR" },
  49. { MAX8997_MUICIRQ_ChgDetRun, "muic-CHGDETRUN" },
  50. { MAX8997_MUICIRQ_ChgTyp, "muic-CHGTYP" },
  51. { MAX8997_MUICIRQ_OVP, "muic-OVP" },
  52. };
  53. /* Define supported cable type */
  54. enum max8997_muic_acc_type {
  55. MAX8997_MUIC_ADC_GROUND = 0x0,
  56. MAX8997_MUIC_ADC_MHL, /* MHL*/
  57. MAX8997_MUIC_ADC_REMOTE_S1_BUTTON,
  58. MAX8997_MUIC_ADC_REMOTE_S2_BUTTON,
  59. MAX8997_MUIC_ADC_REMOTE_S3_BUTTON,
  60. MAX8997_MUIC_ADC_REMOTE_S4_BUTTON,
  61. MAX8997_MUIC_ADC_REMOTE_S5_BUTTON,
  62. MAX8997_MUIC_ADC_REMOTE_S6_BUTTON,
  63. MAX8997_MUIC_ADC_REMOTE_S7_BUTTON,
  64. MAX8997_MUIC_ADC_REMOTE_S8_BUTTON,
  65. MAX8997_MUIC_ADC_REMOTE_S9_BUTTON,
  66. MAX8997_MUIC_ADC_REMOTE_S10_BUTTON,
  67. MAX8997_MUIC_ADC_REMOTE_S11_BUTTON,
  68. MAX8997_MUIC_ADC_REMOTE_S12_BUTTON,
  69. MAX8997_MUIC_ADC_RESERVED_ACC_1,
  70. MAX8997_MUIC_ADC_RESERVED_ACC_2,
  71. MAX8997_MUIC_ADC_RESERVED_ACC_3,
  72. MAX8997_MUIC_ADC_RESERVED_ACC_4,
  73. MAX8997_MUIC_ADC_RESERVED_ACC_5,
  74. MAX8997_MUIC_ADC_CEA936_AUDIO,
  75. MAX8997_MUIC_ADC_PHONE_POWERED_DEV,
  76. MAX8997_MUIC_ADC_TTY_CONVERTER,
  77. MAX8997_MUIC_ADC_UART_CABLE,
  78. MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG,
  79. MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF, /* JIG-USB-OFF */
  80. MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON, /* JIG-USB-ON */
  81. MAX8997_MUIC_ADC_AV_CABLE_NOLOAD, /* DESKDOCK */
  82. MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG,
  83. MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF, /* JIG-UART */
  84. MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON, /* CARDOCK */
  85. MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE,
  86. MAX8997_MUIC_ADC_OPEN, /* OPEN */
  87. };
  88. enum max8997_muic_cable_group {
  89. MAX8997_CABLE_GROUP_ADC = 0,
  90. MAX8997_CABLE_GROUP_ADC_GND,
  91. MAX8997_CABLE_GROUP_CHG,
  92. MAX8997_CABLE_GROUP_VBVOLT,
  93. };
  94. enum max8997_muic_usb_type {
  95. MAX8997_USB_HOST,
  96. MAX8997_USB_DEVICE,
  97. };
  98. enum max8997_muic_charger_type {
  99. MAX8997_CHARGER_TYPE_NONE = 0,
  100. MAX8997_CHARGER_TYPE_USB,
  101. MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT,
  102. MAX8997_CHARGER_TYPE_DEDICATED_CHG,
  103. MAX8997_CHARGER_TYPE_500MA,
  104. MAX8997_CHARGER_TYPE_1A,
  105. MAX8997_CHARGER_TYPE_DEAD_BATTERY = 7,
  106. };
  107. struct max8997_muic_info {
  108. struct device *dev;
  109. struct i2c_client *muic;
  110. struct extcon_dev *edev;
  111. int prev_cable_type;
  112. int prev_chg_type;
  113. u8 status[2];
  114. int irq;
  115. struct work_struct irq_work;
  116. struct mutex mutex;
  117. struct max8997_muic_platform_data *muic_pdata;
  118. enum max8997_muic_charger_type pre_charger_type;
  119. /*
  120. * Use delayed workqueue to detect cable state and then
  121. * notify cable state to notifiee/platform through uevent.
  122. * After completing the booting of platform, the extcon provider
  123. * driver should notify cable state to upper layer.
  124. */
  125. struct delayed_work wq_detcable;
  126. /*
  127. * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
  128. * h/w path of COMP2/COMN1 on CONTROL1 register.
  129. */
  130. int path_usb;
  131. int path_uart;
  132. };
  133. static const unsigned int max8997_extcon_cable[] = {
  134. EXTCON_USB,
  135. EXTCON_USB_HOST,
  136. EXTCON_CHG_USB_DCP,
  137. EXTCON_CHG_USB_FAST,
  138. EXTCON_CHG_USB_SLOW,
  139. EXTCON_CHG_USB_CDP,
  140. EXTCON_DISP_MHL,
  141. EXTCON_DOCK,
  142. EXTCON_JIG,
  143. EXTCON_NONE,
  144. };
  145. /*
  146. * max8997_muic_set_debounce_time - Set the debounce time of ADC
  147. * @info: the instance including private data of max8997 MUIC
  148. * @time: the debounce time of ADC
  149. */
  150. static int max8997_muic_set_debounce_time(struct max8997_muic_info *info,
  151. enum max8997_muic_adc_debounce_time time)
  152. {
  153. int ret;
  154. switch (time) {
  155. case ADC_DEBOUNCE_TIME_0_5MS:
  156. case ADC_DEBOUNCE_TIME_10MS:
  157. case ADC_DEBOUNCE_TIME_25MS:
  158. case ADC_DEBOUNCE_TIME_38_62MS:
  159. ret = max8997_update_reg(info->muic,
  160. MAX8997_MUIC_REG_CONTROL3,
  161. time << CONTROL3_ADCDBSET_SHIFT,
  162. CONTROL3_ADCDBSET_MASK);
  163. if (ret) {
  164. dev_err(info->dev, "failed to set ADC debounce time\n");
  165. return ret;
  166. }
  167. break;
  168. default:
  169. dev_err(info->dev, "invalid ADC debounce time\n");
  170. return -EINVAL;
  171. }
  172. return 0;
  173. };
  174. /*
  175. * max8997_muic_set_path - Set hardware line according to attached cable
  176. * @info: the instance including private data of max8997 MUIC
  177. * @value: the path according to attached cable
  178. * @attached: the state of cable (true:attached, false:detached)
  179. *
  180. * The max8997 MUIC device share outside H/W line among a varity of cables,
  181. * so this function set internal path of H/W line according to the type of
  182. * attached cable.
  183. */
  184. static int max8997_muic_set_path(struct max8997_muic_info *info,
  185. u8 val, bool attached)
  186. {
  187. int ret = 0;
  188. u8 ctrl1, ctrl2 = 0;
  189. if (attached)
  190. ctrl1 = val;
  191. else
  192. ctrl1 = CONTROL1_SW_OPEN;
  193. ret = max8997_update_reg(info->muic,
  194. MAX8997_MUIC_REG_CONTROL1, ctrl1, COMP_SW_MASK);
  195. if (ret < 0) {
  196. dev_err(info->dev, "failed to update MUIC register\n");
  197. return ret;
  198. }
  199. if (attached)
  200. ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
  201. else
  202. ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
  203. ret = max8997_update_reg(info->muic,
  204. MAX8997_MUIC_REG_CONTROL2, ctrl2,
  205. CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
  206. if (ret < 0) {
  207. dev_err(info->dev, "failed to update MUIC register\n");
  208. return ret;
  209. }
  210. dev_info(info->dev,
  211. "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
  212. ctrl1, ctrl2, attached ? "attached" : "detached");
  213. return 0;
  214. }
  215. /*
  216. * max8997_muic_get_cable_type - Return cable type and check cable state
  217. * @info: the instance including private data of max8997 MUIC
  218. * @group: the path according to attached cable
  219. * @attached: store cable state and return
  220. *
  221. * This function check the cable state either attached or detached,
  222. * and then divide precise type of cable according to cable group.
  223. * - MAX8997_CABLE_GROUP_ADC
  224. * - MAX8997_CABLE_GROUP_CHG
  225. */
  226. static int max8997_muic_get_cable_type(struct max8997_muic_info *info,
  227. enum max8997_muic_cable_group group, bool *attached)
  228. {
  229. int cable_type = 0;
  230. int adc;
  231. int chg_type;
  232. switch (group) {
  233. case MAX8997_CABLE_GROUP_ADC:
  234. /*
  235. * Read ADC value to check cable type and decide cable state
  236. * according to cable type
  237. */
  238. adc = info->status[0] & STATUS1_ADC_MASK;
  239. adc >>= STATUS1_ADC_SHIFT;
  240. /*
  241. * Check current cable state/cable type and store cable type
  242. * (info->prev_cable_type) for handling cable when cable is
  243. * detached.
  244. */
  245. if (adc == MAX8997_MUIC_ADC_OPEN) {
  246. *attached = false;
  247. cable_type = info->prev_cable_type;
  248. info->prev_cable_type = MAX8997_MUIC_ADC_OPEN;
  249. } else {
  250. *attached = true;
  251. cable_type = info->prev_cable_type = adc;
  252. }
  253. break;
  254. case MAX8997_CABLE_GROUP_CHG:
  255. /*
  256. * Read charger type to check cable type and decide cable state
  257. * according to type of charger cable.
  258. */
  259. chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
  260. chg_type >>= STATUS2_CHGTYP_SHIFT;
  261. if (chg_type == MAX8997_CHARGER_TYPE_NONE) {
  262. *attached = false;
  263. cable_type = info->prev_chg_type;
  264. info->prev_chg_type = MAX8997_CHARGER_TYPE_NONE;
  265. } else {
  266. *attached = true;
  267. /*
  268. * Check current cable state/cable type and store cable
  269. * type(info->prev_chg_type) for handling cable when
  270. * charger cable is detached.
  271. */
  272. cable_type = info->prev_chg_type = chg_type;
  273. }
  274. break;
  275. default:
  276. dev_err(info->dev, "Unknown cable group (%d)\n", group);
  277. cable_type = -EINVAL;
  278. break;
  279. }
  280. return cable_type;
  281. }
  282. static int max8997_muic_handle_usb(struct max8997_muic_info *info,
  283. enum max8997_muic_usb_type usb_type, bool attached)
  284. {
  285. int ret = 0;
  286. if (usb_type == MAX8997_USB_HOST) {
  287. ret = max8997_muic_set_path(info, info->path_usb, attached);
  288. if (ret < 0) {
  289. dev_err(info->dev, "failed to update muic register\n");
  290. return ret;
  291. }
  292. }
  293. switch (usb_type) {
  294. case MAX8997_USB_HOST:
  295. extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, attached);
  296. break;
  297. case MAX8997_USB_DEVICE:
  298. extcon_set_cable_state_(info->edev, EXTCON_USB, attached);
  299. break;
  300. default:
  301. dev_err(info->dev, "failed to detect %s usb cable\n",
  302. attached ? "attached" : "detached");
  303. return -EINVAL;
  304. }
  305. return 0;
  306. }
  307. static int max8997_muic_handle_dock(struct max8997_muic_info *info,
  308. int cable_type, bool attached)
  309. {
  310. int ret = 0;
  311. ret = max8997_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
  312. if (ret) {
  313. dev_err(info->dev, "failed to update muic register\n");
  314. return ret;
  315. }
  316. switch (cable_type) {
  317. case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
  318. case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
  319. extcon_set_cable_state_(info->edev, EXTCON_DOCK, attached);
  320. break;
  321. default:
  322. dev_err(info->dev, "failed to detect %s dock device\n",
  323. attached ? "attached" : "detached");
  324. return -EINVAL;
  325. }
  326. return 0;
  327. }
  328. static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info,
  329. bool attached)
  330. {
  331. int ret = 0;
  332. /* switch to UART */
  333. ret = max8997_muic_set_path(info, info->path_uart, attached);
  334. if (ret) {
  335. dev_err(info->dev, "failed to update muic register\n");
  336. return ret;
  337. }
  338. extcon_set_cable_state_(info->edev, EXTCON_JIG, attached);
  339. return 0;
  340. }
  341. static int max8997_muic_adc_handler(struct max8997_muic_info *info)
  342. {
  343. int cable_type;
  344. bool attached;
  345. int ret = 0;
  346. /* Check cable state which is either detached or attached */
  347. cable_type = max8997_muic_get_cable_type(info,
  348. MAX8997_CABLE_GROUP_ADC, &attached);
  349. switch (cable_type) {
  350. case MAX8997_MUIC_ADC_GROUND:
  351. ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, attached);
  352. if (ret < 0)
  353. return ret;
  354. break;
  355. case MAX8997_MUIC_ADC_MHL:
  356. extcon_set_cable_state_(info->edev, EXTCON_DISP_MHL, attached);
  357. break;
  358. case MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF:
  359. case MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON:
  360. ret = max8997_muic_handle_usb(info,
  361. MAX8997_USB_DEVICE, attached);
  362. if (ret < 0)
  363. return ret;
  364. break;
  365. case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
  366. case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
  367. ret = max8997_muic_handle_dock(info, cable_type, attached);
  368. if (ret < 0)
  369. return ret;
  370. break;
  371. case MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF:
  372. ret = max8997_muic_handle_jig_uart(info, attached);
  373. break;
  374. case MAX8997_MUIC_ADC_REMOTE_S1_BUTTON:
  375. case MAX8997_MUIC_ADC_REMOTE_S2_BUTTON:
  376. case MAX8997_MUIC_ADC_REMOTE_S3_BUTTON:
  377. case MAX8997_MUIC_ADC_REMOTE_S4_BUTTON:
  378. case MAX8997_MUIC_ADC_REMOTE_S5_BUTTON:
  379. case MAX8997_MUIC_ADC_REMOTE_S6_BUTTON:
  380. case MAX8997_MUIC_ADC_REMOTE_S7_BUTTON:
  381. case MAX8997_MUIC_ADC_REMOTE_S8_BUTTON:
  382. case MAX8997_MUIC_ADC_REMOTE_S9_BUTTON:
  383. case MAX8997_MUIC_ADC_REMOTE_S10_BUTTON:
  384. case MAX8997_MUIC_ADC_REMOTE_S11_BUTTON:
  385. case MAX8997_MUIC_ADC_REMOTE_S12_BUTTON:
  386. case MAX8997_MUIC_ADC_RESERVED_ACC_1:
  387. case MAX8997_MUIC_ADC_RESERVED_ACC_2:
  388. case MAX8997_MUIC_ADC_RESERVED_ACC_3:
  389. case MAX8997_MUIC_ADC_RESERVED_ACC_4:
  390. case MAX8997_MUIC_ADC_RESERVED_ACC_5:
  391. case MAX8997_MUIC_ADC_CEA936_AUDIO:
  392. case MAX8997_MUIC_ADC_PHONE_POWERED_DEV:
  393. case MAX8997_MUIC_ADC_TTY_CONVERTER:
  394. case MAX8997_MUIC_ADC_UART_CABLE:
  395. case MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG:
  396. case MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG:
  397. case MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE:
  398. /*
  399. * This cable isn't used in general case if it is specially
  400. * needed to detect additional cable, should implement
  401. * proper operation when this cable is attached/detached.
  402. */
  403. dev_info(info->dev,
  404. "cable is %s but it isn't used (type:0x%x)\n",
  405. attached ? "attached" : "detached", cable_type);
  406. return -EAGAIN;
  407. default:
  408. dev_err(info->dev,
  409. "failed to detect %s unknown cable (type:0x%x)\n",
  410. attached ? "attached" : "detached", cable_type);
  411. return -EINVAL;
  412. }
  413. return 0;
  414. }
  415. static int max8997_muic_chg_handler(struct max8997_muic_info *info)
  416. {
  417. int chg_type;
  418. bool attached;
  419. int adc;
  420. chg_type = max8997_muic_get_cable_type(info,
  421. MAX8997_CABLE_GROUP_CHG, &attached);
  422. switch (chg_type) {
  423. case MAX8997_CHARGER_TYPE_NONE:
  424. break;
  425. case MAX8997_CHARGER_TYPE_USB:
  426. adc = info->status[0] & STATUS1_ADC_MASK;
  427. adc >>= STATUS1_ADC_SHIFT;
  428. if ((adc & STATUS1_ADC_MASK) == MAX8997_MUIC_ADC_OPEN) {
  429. max8997_muic_handle_usb(info,
  430. MAX8997_USB_DEVICE, attached);
  431. }
  432. break;
  433. case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
  434. extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_CDP,
  435. attached);
  436. break;
  437. case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
  438. extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_DCP,
  439. attached);
  440. break;
  441. case MAX8997_CHARGER_TYPE_500MA:
  442. extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_SLOW,
  443. attached);
  444. break;
  445. case MAX8997_CHARGER_TYPE_1A:
  446. extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_FAST,
  447. attached);
  448. break;
  449. default:
  450. dev_err(info->dev,
  451. "failed to detect %s unknown chg cable (type:0x%x)\n",
  452. attached ? "attached" : "detached", chg_type);
  453. return -EINVAL;
  454. }
  455. return 0;
  456. }
  457. static void max8997_muic_irq_work(struct work_struct *work)
  458. {
  459. struct max8997_muic_info *info = container_of(work,
  460. struct max8997_muic_info, irq_work);
  461. int irq_type = 0;
  462. int i, ret;
  463. if (!info->edev)
  464. return;
  465. mutex_lock(&info->mutex);
  466. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
  467. if (info->irq == muic_irqs[i].virq)
  468. irq_type = muic_irqs[i].irq;
  469. ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
  470. 2, info->status);
  471. if (ret) {
  472. dev_err(info->dev, "failed to read muic register\n");
  473. mutex_unlock(&info->mutex);
  474. return;
  475. }
  476. switch (irq_type) {
  477. case MAX8997_MUICIRQ_ADCError:
  478. case MAX8997_MUICIRQ_ADCLow:
  479. case MAX8997_MUICIRQ_ADC:
  480. /* Handle all of cable except for charger cable */
  481. ret = max8997_muic_adc_handler(info);
  482. break;
  483. case MAX8997_MUICIRQ_VBVolt:
  484. case MAX8997_MUICIRQ_DBChg:
  485. case MAX8997_MUICIRQ_DCDTmr:
  486. case MAX8997_MUICIRQ_ChgDetRun:
  487. case MAX8997_MUICIRQ_ChgTyp:
  488. /* Handle charger cable */
  489. ret = max8997_muic_chg_handler(info);
  490. break;
  491. case MAX8997_MUICIRQ_OVP:
  492. break;
  493. default:
  494. dev_info(info->dev, "misc interrupt: irq %d occurred\n",
  495. irq_type);
  496. mutex_unlock(&info->mutex);
  497. return;
  498. }
  499. if (ret < 0)
  500. dev_err(info->dev, "failed to handle MUIC interrupt\n");
  501. mutex_unlock(&info->mutex);
  502. }
  503. static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
  504. {
  505. struct max8997_muic_info *info = data;
  506. dev_dbg(info->dev, "irq:%d\n", irq);
  507. info->irq = irq;
  508. schedule_work(&info->irq_work);
  509. return IRQ_HANDLED;
  510. }
  511. static int max8997_muic_detect_dev(struct max8997_muic_info *info)
  512. {
  513. int ret = 0;
  514. int adc;
  515. int chg_type;
  516. bool attached;
  517. mutex_lock(&info->mutex);
  518. /* Read STATUSx register to detect accessory */
  519. ret = max8997_bulk_read(info->muic,
  520. MAX8997_MUIC_REG_STATUS1, 2, info->status);
  521. if (ret) {
  522. dev_err(info->dev, "failed to read MUIC register\n");
  523. mutex_unlock(&info->mutex);
  524. return ret;
  525. }
  526. adc = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_ADC,
  527. &attached);
  528. if (attached && adc != MAX8997_MUIC_ADC_OPEN) {
  529. ret = max8997_muic_adc_handler(info);
  530. if (ret < 0) {
  531. dev_err(info->dev, "Cannot detect ADC cable\n");
  532. mutex_unlock(&info->mutex);
  533. return ret;
  534. }
  535. }
  536. chg_type = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_CHG,
  537. &attached);
  538. if (attached && chg_type != MAX8997_CHARGER_TYPE_NONE) {
  539. ret = max8997_muic_chg_handler(info);
  540. if (ret < 0) {
  541. dev_err(info->dev, "Cannot detect charger cable\n");
  542. mutex_unlock(&info->mutex);
  543. return ret;
  544. }
  545. }
  546. mutex_unlock(&info->mutex);
  547. return 0;
  548. }
  549. static void max8997_muic_detect_cable_wq(struct work_struct *work)
  550. {
  551. struct max8997_muic_info *info = container_of(to_delayed_work(work),
  552. struct max8997_muic_info, wq_detcable);
  553. int ret;
  554. ret = max8997_muic_detect_dev(info);
  555. if (ret < 0)
  556. dev_err(info->dev, "failed to detect cable type\n");
  557. }
  558. static int max8997_muic_probe(struct platform_device *pdev)
  559. {
  560. struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
  561. struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
  562. struct max8997_muic_info *info;
  563. int delay_jiffies;
  564. int ret, i;
  565. info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_muic_info),
  566. GFP_KERNEL);
  567. if (!info)
  568. return -ENOMEM;
  569. info->dev = &pdev->dev;
  570. info->muic = max8997->muic;
  571. platform_set_drvdata(pdev, info);
  572. mutex_init(&info->mutex);
  573. INIT_WORK(&info->irq_work, max8997_muic_irq_work);
  574. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
  575. struct max8997_muic_irq *muic_irq = &muic_irqs[i];
  576. unsigned int virq = 0;
  577. virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
  578. if (!virq) {
  579. ret = -EINVAL;
  580. goto err_irq;
  581. }
  582. muic_irq->virq = virq;
  583. ret = request_threaded_irq(virq, NULL,
  584. max8997_muic_irq_handler,
  585. IRQF_NO_SUSPEND,
  586. muic_irq->name, info);
  587. if (ret) {
  588. dev_err(&pdev->dev,
  589. "failed: irq request (IRQ: %d, error :%d)\n",
  590. muic_irq->irq, ret);
  591. goto err_irq;
  592. }
  593. }
  594. /* External connector */
  595. info->edev = devm_extcon_dev_allocate(&pdev->dev, max8997_extcon_cable);
  596. if (IS_ERR(info->edev)) {
  597. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  598. ret = -ENOMEM;
  599. goto err_irq;
  600. }
  601. ret = devm_extcon_dev_register(&pdev->dev, info->edev);
  602. if (ret) {
  603. dev_err(&pdev->dev, "failed to register extcon device\n");
  604. goto err_irq;
  605. }
  606. if (pdata && pdata->muic_pdata) {
  607. struct max8997_muic_platform_data *muic_pdata
  608. = pdata->muic_pdata;
  609. /* Initialize registers according to platform data */
  610. for (i = 0; i < muic_pdata->num_init_data; i++) {
  611. max8997_write_reg(info->muic,
  612. muic_pdata->init_data[i].addr,
  613. muic_pdata->init_data[i].data);
  614. }
  615. /*
  616. * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
  617. * h/w path of COMP2/COMN1 on CONTROL1 register.
  618. */
  619. if (muic_pdata->path_uart)
  620. info->path_uart = muic_pdata->path_uart;
  621. else
  622. info->path_uart = CONTROL1_SW_UART;
  623. if (muic_pdata->path_usb)
  624. info->path_usb = muic_pdata->path_usb;
  625. else
  626. info->path_usb = CONTROL1_SW_USB;
  627. /*
  628. * Default delay time for detecting cable state
  629. * after certain time.
  630. */
  631. if (muic_pdata->detcable_delay_ms)
  632. delay_jiffies =
  633. msecs_to_jiffies(muic_pdata->detcable_delay_ms);
  634. else
  635. delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  636. } else {
  637. info->path_uart = CONTROL1_SW_UART;
  638. info->path_usb = CONTROL1_SW_USB;
  639. delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  640. }
  641. /* Set initial path for UART */
  642. max8997_muic_set_path(info, info->path_uart, true);
  643. /* Set ADC debounce time */
  644. max8997_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
  645. /*
  646. * Detect accessory after completing the initialization of platform
  647. *
  648. * - Use delayed workqueue to detect cable state and then
  649. * notify cable state to notifiee/platform through uevent.
  650. * After completing the booting of platform, the extcon provider
  651. * driver should notify cable state to upper layer.
  652. */
  653. INIT_DELAYED_WORK(&info->wq_detcable, max8997_muic_detect_cable_wq);
  654. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  655. delay_jiffies);
  656. return 0;
  657. err_irq:
  658. while (--i >= 0)
  659. free_irq(muic_irqs[i].virq, info);
  660. return ret;
  661. }
  662. static int max8997_muic_remove(struct platform_device *pdev)
  663. {
  664. struct max8997_muic_info *info = platform_get_drvdata(pdev);
  665. int i;
  666. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
  667. free_irq(muic_irqs[i].virq, info);
  668. cancel_work_sync(&info->irq_work);
  669. return 0;
  670. }
  671. static struct platform_driver max8997_muic_driver = {
  672. .driver = {
  673. .name = DEV_NAME,
  674. },
  675. .probe = max8997_muic_probe,
  676. .remove = max8997_muic_remove,
  677. };
  678. module_platform_driver(max8997_muic_driver);
  679. MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
  680. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  681. MODULE_LICENSE("GPL");