extcon-max8997.c 20 KB

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