extcon-max14577.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // extcon-max14577.c - MAX14577/77836 extcon driver to support MUIC
  4. //
  5. // Copyright (C) 2013,2014 Samsung Electronics
  6. // Chanwoo Choi <[email protected]>
  7. // Krzysztof Kozlowski <[email protected]>
  8. #include <linux/devm-helpers.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/i2c.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/mfd/max14577.h>
  15. #include <linux/mfd/max14577-private.h>
  16. #include <linux/extcon-provider.h>
  17. #define DELAY_MS_DEFAULT 17000 /* unit: millisecond */
  18. enum max14577_muic_adc_debounce_time {
  19. ADC_DEBOUNCE_TIME_5MS = 0,
  20. ADC_DEBOUNCE_TIME_10MS,
  21. ADC_DEBOUNCE_TIME_25MS,
  22. ADC_DEBOUNCE_TIME_38_62MS,
  23. };
  24. enum max14577_muic_status {
  25. MAX14577_MUIC_STATUS1 = 0,
  26. MAX14577_MUIC_STATUS2 = 1,
  27. MAX14577_MUIC_STATUS_END,
  28. };
  29. /**
  30. * struct max14577_muic_irq
  31. * @irq: the index of irq list of MUIC device.
  32. * @name: the name of irq.
  33. * @virq: the virtual irq to use irq domain
  34. */
  35. struct max14577_muic_irq {
  36. unsigned int irq;
  37. const char *name;
  38. unsigned int virq;
  39. };
  40. static struct max14577_muic_irq max14577_muic_irqs[] = {
  41. { MAX14577_IRQ_INT1_ADC, "muic-ADC" },
  42. { MAX14577_IRQ_INT1_ADCLOW, "muic-ADCLOW" },
  43. { MAX14577_IRQ_INT1_ADCERR, "muic-ADCError" },
  44. { MAX14577_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
  45. { MAX14577_IRQ_INT2_CHGDETRUN, "muic-CHGDETRUN" },
  46. { MAX14577_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
  47. { MAX14577_IRQ_INT2_DBCHG, "muic-DBCHG" },
  48. { MAX14577_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
  49. };
  50. static struct max14577_muic_irq max77836_muic_irqs[] = {
  51. { MAX14577_IRQ_INT1_ADC, "muic-ADC" },
  52. { MAX14577_IRQ_INT1_ADCLOW, "muic-ADCLOW" },
  53. { MAX14577_IRQ_INT1_ADCERR, "muic-ADCError" },
  54. { MAX77836_IRQ_INT1_ADC1K, "muic-ADC1K" },
  55. { MAX14577_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
  56. { MAX14577_IRQ_INT2_CHGDETRUN, "muic-CHGDETRUN" },
  57. { MAX14577_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
  58. { MAX14577_IRQ_INT2_DBCHG, "muic-DBCHG" },
  59. { MAX14577_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
  60. { MAX77836_IRQ_INT2_VIDRM, "muic-VIDRM" },
  61. };
  62. struct max14577_muic_info {
  63. struct device *dev;
  64. struct max14577 *max14577;
  65. struct extcon_dev *edev;
  66. int prev_cable_type;
  67. int prev_chg_type;
  68. u8 status[MAX14577_MUIC_STATUS_END];
  69. struct max14577_muic_irq *muic_irqs;
  70. unsigned int muic_irqs_num;
  71. bool irq_adc;
  72. bool irq_chg;
  73. struct work_struct irq_work;
  74. struct mutex mutex;
  75. /*
  76. * Use delayed workqueue to detect cable state and then
  77. * notify cable state to notifiee/platform through uevent.
  78. * After completing the booting of platform, the extcon provider
  79. * driver should notify cable state to upper layer.
  80. */
  81. struct delayed_work wq_detcable;
  82. /*
  83. * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
  84. * h/w path of COMP2/COMN1 on CONTROL1 register.
  85. */
  86. int path_usb;
  87. int path_uart;
  88. };
  89. enum max14577_muic_cable_group {
  90. MAX14577_CABLE_GROUP_ADC = 0,
  91. MAX14577_CABLE_GROUP_CHG,
  92. };
  93. /* Define supported accessory type */
  94. enum max14577_muic_acc_type {
  95. MAX14577_MUIC_ADC_GROUND = 0x0,
  96. MAX14577_MUIC_ADC_SEND_END_BUTTON,
  97. MAX14577_MUIC_ADC_REMOTE_S1_BUTTON,
  98. MAX14577_MUIC_ADC_REMOTE_S2_BUTTON,
  99. MAX14577_MUIC_ADC_REMOTE_S3_BUTTON,
  100. MAX14577_MUIC_ADC_REMOTE_S4_BUTTON,
  101. MAX14577_MUIC_ADC_REMOTE_S5_BUTTON,
  102. MAX14577_MUIC_ADC_REMOTE_S6_BUTTON,
  103. MAX14577_MUIC_ADC_REMOTE_S7_BUTTON,
  104. MAX14577_MUIC_ADC_REMOTE_S8_BUTTON,
  105. MAX14577_MUIC_ADC_REMOTE_S9_BUTTON,
  106. MAX14577_MUIC_ADC_REMOTE_S10_BUTTON,
  107. MAX14577_MUIC_ADC_REMOTE_S11_BUTTON,
  108. MAX14577_MUIC_ADC_REMOTE_S12_BUTTON,
  109. MAX14577_MUIC_ADC_RESERVED_ACC_1,
  110. MAX14577_MUIC_ADC_RESERVED_ACC_2,
  111. MAX14577_MUIC_ADC_RESERVED_ACC_3,
  112. MAX14577_MUIC_ADC_RESERVED_ACC_4,
  113. MAX14577_MUIC_ADC_RESERVED_ACC_5,
  114. MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2,
  115. MAX14577_MUIC_ADC_PHONE_POWERED_DEV,
  116. MAX14577_MUIC_ADC_TTY_CONVERTER,
  117. MAX14577_MUIC_ADC_UART_CABLE,
  118. MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG,
  119. MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF,
  120. MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON,
  121. MAX14577_MUIC_ADC_AV_CABLE_NOLOAD,
  122. MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG,
  123. MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF,
  124. MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON,
  125. MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1, /* with Remote and Simple Ctrl */
  126. MAX14577_MUIC_ADC_OPEN,
  127. };
  128. static const unsigned int max14577_extcon_cable[] = {
  129. EXTCON_USB,
  130. EXTCON_CHG_USB_SDP,
  131. EXTCON_CHG_USB_DCP,
  132. EXTCON_CHG_USB_FAST,
  133. EXTCON_CHG_USB_SLOW,
  134. EXTCON_CHG_USB_CDP,
  135. EXTCON_JIG,
  136. EXTCON_NONE,
  137. };
  138. /*
  139. * max14577_muic_set_debounce_time - Set the debounce time of ADC
  140. * @info: the instance including private data of max14577 MUIC
  141. * @time: the debounce time of ADC
  142. */
  143. static int max14577_muic_set_debounce_time(struct max14577_muic_info *info,
  144. enum max14577_muic_adc_debounce_time time)
  145. {
  146. u8 ret;
  147. switch (time) {
  148. case ADC_DEBOUNCE_TIME_5MS:
  149. case ADC_DEBOUNCE_TIME_10MS:
  150. case ADC_DEBOUNCE_TIME_25MS:
  151. case ADC_DEBOUNCE_TIME_38_62MS:
  152. ret = max14577_update_reg(info->max14577->regmap,
  153. MAX14577_MUIC_REG_CONTROL3,
  154. CTRL3_ADCDBSET_MASK,
  155. time << CTRL3_ADCDBSET_SHIFT);
  156. if (ret) {
  157. dev_err(info->dev, "failed to set ADC debounce time\n");
  158. return ret;
  159. }
  160. break;
  161. default:
  162. dev_err(info->dev, "invalid ADC debounce time\n");
  163. return -EINVAL;
  164. }
  165. return 0;
  166. };
  167. /*
  168. * max14577_muic_set_path - Set hardware line according to attached cable
  169. * @info: the instance including private data of max14577 MUIC
  170. * @value: the path according to attached cable
  171. * @attached: the state of cable (true:attached, false:detached)
  172. *
  173. * The max14577 MUIC device share outside H/W line among a varity of cables
  174. * so, this function set internal path of H/W line according to the type of
  175. * attached cable.
  176. */
  177. static int max14577_muic_set_path(struct max14577_muic_info *info,
  178. u8 val, bool attached)
  179. {
  180. u8 ctrl1, ctrl2 = 0;
  181. int ret;
  182. /* Set open state to path before changing hw path */
  183. ret = max14577_update_reg(info->max14577->regmap,
  184. MAX14577_MUIC_REG_CONTROL1,
  185. CLEAR_IDBEN_MICEN_MASK, CTRL1_SW_OPEN);
  186. if (ret < 0) {
  187. dev_err(info->dev, "failed to update MUIC register\n");
  188. return ret;
  189. }
  190. if (attached)
  191. ctrl1 = val;
  192. else
  193. ctrl1 = CTRL1_SW_OPEN;
  194. ret = max14577_update_reg(info->max14577->regmap,
  195. MAX14577_MUIC_REG_CONTROL1,
  196. CLEAR_IDBEN_MICEN_MASK, ctrl1);
  197. if (ret < 0) {
  198. dev_err(info->dev, "failed to update MUIC register\n");
  199. return ret;
  200. }
  201. if (attached)
  202. ctrl2 |= CTRL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
  203. else
  204. ctrl2 |= CTRL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
  205. ret = max14577_update_reg(info->max14577->regmap,
  206. MAX14577_REG_CONTROL2,
  207. CTRL2_LOWPWR_MASK | CTRL2_CPEN_MASK, ctrl2);
  208. if (ret < 0) {
  209. dev_err(info->dev, "failed to update MUIC register\n");
  210. return ret;
  211. }
  212. dev_dbg(info->dev,
  213. "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
  214. ctrl1, ctrl2, attached ? "attached" : "detached");
  215. return 0;
  216. }
  217. /*
  218. * max14577_muic_get_cable_type - Return cable type and check cable state
  219. * @info: the instance including private data of max14577 MUIC
  220. * @group: the path according to attached cable
  221. * @attached: store cable state and return
  222. *
  223. * This function check the cable state either attached or detached,
  224. * and then divide precise type of cable according to cable group.
  225. * - max14577_CABLE_GROUP_ADC
  226. * - max14577_CABLE_GROUP_CHG
  227. */
  228. static int max14577_muic_get_cable_type(struct max14577_muic_info *info,
  229. enum max14577_muic_cable_group group, bool *attached)
  230. {
  231. int cable_type = 0;
  232. int adc;
  233. int chg_type;
  234. switch (group) {
  235. case MAX14577_CABLE_GROUP_ADC:
  236. /*
  237. * Read ADC value to check cable type and decide cable state
  238. * according to cable type
  239. */
  240. adc = info->status[MAX14577_MUIC_STATUS1] & STATUS1_ADC_MASK;
  241. adc >>= STATUS1_ADC_SHIFT;
  242. /*
  243. * Check current cable state/cable type and store cable type
  244. * (info->prev_cable_type) for handling cable when cable is
  245. * detached.
  246. */
  247. if (adc == MAX14577_MUIC_ADC_OPEN) {
  248. *attached = false;
  249. cable_type = info->prev_cable_type;
  250. info->prev_cable_type = MAX14577_MUIC_ADC_OPEN;
  251. } else {
  252. *attached = true;
  253. cable_type = info->prev_cable_type = adc;
  254. }
  255. break;
  256. case MAX14577_CABLE_GROUP_CHG:
  257. /*
  258. * Read charger type to check cable type and decide cable state
  259. * according to type of charger cable.
  260. */
  261. chg_type = info->status[MAX14577_MUIC_STATUS2] &
  262. STATUS2_CHGTYP_MASK;
  263. chg_type >>= STATUS2_CHGTYP_SHIFT;
  264. if (chg_type == MAX14577_CHARGER_TYPE_NONE) {
  265. *attached = false;
  266. cable_type = info->prev_chg_type;
  267. info->prev_chg_type = MAX14577_CHARGER_TYPE_NONE;
  268. } else {
  269. *attached = true;
  270. /*
  271. * Check current cable state/cable type and store cable
  272. * type(info->prev_chg_type) for handling cable when
  273. * charger cable is detached.
  274. */
  275. cable_type = info->prev_chg_type = chg_type;
  276. }
  277. break;
  278. default:
  279. dev_err(info->dev, "Unknown cable group (%d)\n", group);
  280. cable_type = -EINVAL;
  281. break;
  282. }
  283. return cable_type;
  284. }
  285. static int max14577_muic_jig_handler(struct max14577_muic_info *info,
  286. int cable_type, bool attached)
  287. {
  288. int ret = 0;
  289. u8 path = CTRL1_SW_OPEN;
  290. dev_dbg(info->dev,
  291. "external connector is %s (adc:0x%02x)\n",
  292. attached ? "attached" : "detached", cable_type);
  293. switch (cable_type) {
  294. case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
  295. case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
  296. /* PATH:AP_USB */
  297. path = CTRL1_SW_USB;
  298. break;
  299. case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
  300. /* PATH:AP_UART */
  301. path = CTRL1_SW_UART;
  302. break;
  303. default:
  304. dev_err(info->dev, "failed to detect %s jig cable\n",
  305. attached ? "attached" : "detached");
  306. return -EINVAL;
  307. }
  308. ret = max14577_muic_set_path(info, path, attached);
  309. if (ret < 0)
  310. return ret;
  311. extcon_set_state_sync(info->edev, EXTCON_JIG, attached);
  312. return 0;
  313. }
  314. static int max14577_muic_adc_handler(struct max14577_muic_info *info)
  315. {
  316. int cable_type;
  317. bool attached;
  318. int ret = 0;
  319. /* Check accessory state which is either detached or attached */
  320. cable_type = max14577_muic_get_cable_type(info,
  321. MAX14577_CABLE_GROUP_ADC, &attached);
  322. dev_dbg(info->dev,
  323. "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
  324. attached ? "attached" : "detached", cable_type,
  325. info->prev_cable_type);
  326. switch (cable_type) {
  327. case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF:
  328. case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON:
  329. case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF:
  330. /* JIG */
  331. ret = max14577_muic_jig_handler(info, cable_type, attached);
  332. if (ret < 0)
  333. return ret;
  334. break;
  335. case MAX14577_MUIC_ADC_GROUND:
  336. case MAX14577_MUIC_ADC_SEND_END_BUTTON:
  337. case MAX14577_MUIC_ADC_REMOTE_S1_BUTTON:
  338. case MAX14577_MUIC_ADC_REMOTE_S2_BUTTON:
  339. case MAX14577_MUIC_ADC_REMOTE_S3_BUTTON:
  340. case MAX14577_MUIC_ADC_REMOTE_S4_BUTTON:
  341. case MAX14577_MUIC_ADC_REMOTE_S5_BUTTON:
  342. case MAX14577_MUIC_ADC_REMOTE_S6_BUTTON:
  343. case MAX14577_MUIC_ADC_REMOTE_S7_BUTTON:
  344. case MAX14577_MUIC_ADC_REMOTE_S8_BUTTON:
  345. case MAX14577_MUIC_ADC_REMOTE_S9_BUTTON:
  346. case MAX14577_MUIC_ADC_REMOTE_S10_BUTTON:
  347. case MAX14577_MUIC_ADC_REMOTE_S11_BUTTON:
  348. case MAX14577_MUIC_ADC_REMOTE_S12_BUTTON:
  349. case MAX14577_MUIC_ADC_RESERVED_ACC_1:
  350. case MAX14577_MUIC_ADC_RESERVED_ACC_2:
  351. case MAX14577_MUIC_ADC_RESERVED_ACC_3:
  352. case MAX14577_MUIC_ADC_RESERVED_ACC_4:
  353. case MAX14577_MUIC_ADC_RESERVED_ACC_5:
  354. case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2:
  355. case MAX14577_MUIC_ADC_PHONE_POWERED_DEV:
  356. case MAX14577_MUIC_ADC_TTY_CONVERTER:
  357. case MAX14577_MUIC_ADC_UART_CABLE:
  358. case MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG:
  359. case MAX14577_MUIC_ADC_AV_CABLE_NOLOAD:
  360. case MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG:
  361. case MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON:
  362. case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1:
  363. /*
  364. * This accessory isn't used in general case if it is specially
  365. * needed to detect additional accessory, should implement
  366. * proper operation when this accessory is attached/detached.
  367. */
  368. dev_info(info->dev,
  369. "accessory is %s but it isn't used (adc:0x%x)\n",
  370. attached ? "attached" : "detached", cable_type);
  371. return -EAGAIN;
  372. default:
  373. dev_err(info->dev,
  374. "failed to detect %s accessory (adc:0x%x)\n",
  375. attached ? "attached" : "detached", cable_type);
  376. return -EINVAL;
  377. }
  378. return 0;
  379. }
  380. static int max14577_muic_chg_handler(struct max14577_muic_info *info)
  381. {
  382. int chg_type;
  383. bool attached;
  384. int ret = 0;
  385. chg_type = max14577_muic_get_cable_type(info,
  386. MAX14577_CABLE_GROUP_CHG, &attached);
  387. dev_dbg(info->dev,
  388. "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
  389. attached ? "attached" : "detached",
  390. chg_type, info->prev_chg_type);
  391. switch (chg_type) {
  392. case MAX14577_CHARGER_TYPE_USB:
  393. /* PATH:AP_USB */
  394. ret = max14577_muic_set_path(info, info->path_usb, attached);
  395. if (ret < 0)
  396. return ret;
  397. extcon_set_state_sync(info->edev, EXTCON_USB, attached);
  398. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
  399. attached);
  400. break;
  401. case MAX14577_CHARGER_TYPE_DEDICATED_CHG:
  402. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
  403. attached);
  404. break;
  405. case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT:
  406. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP,
  407. attached);
  408. break;
  409. case MAX14577_CHARGER_TYPE_SPECIAL_500MA:
  410. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW,
  411. attached);
  412. break;
  413. case MAX14577_CHARGER_TYPE_SPECIAL_1A:
  414. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST,
  415. attached);
  416. break;
  417. case MAX14577_CHARGER_TYPE_NONE:
  418. case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
  419. break;
  420. default:
  421. dev_err(info->dev,
  422. "failed to detect %s accessory (chg_type:0x%x)\n",
  423. attached ? "attached" : "detached", chg_type);
  424. return -EINVAL;
  425. }
  426. return 0;
  427. }
  428. static void max14577_muic_irq_work(struct work_struct *work)
  429. {
  430. struct max14577_muic_info *info = container_of(work,
  431. struct max14577_muic_info, irq_work);
  432. int ret = 0;
  433. if (!info->edev)
  434. return;
  435. mutex_lock(&info->mutex);
  436. ret = max14577_bulk_read(info->max14577->regmap,
  437. MAX14577_MUIC_REG_STATUS1, info->status, 2);
  438. if (ret) {
  439. dev_err(info->dev, "failed to read MUIC register\n");
  440. mutex_unlock(&info->mutex);
  441. return;
  442. }
  443. if (info->irq_adc) {
  444. ret = max14577_muic_adc_handler(info);
  445. info->irq_adc = false;
  446. }
  447. if (info->irq_chg) {
  448. ret = max14577_muic_chg_handler(info);
  449. info->irq_chg = false;
  450. }
  451. if (ret < 0)
  452. dev_err(info->dev, "failed to handle MUIC interrupt\n");
  453. mutex_unlock(&info->mutex);
  454. }
  455. /*
  456. * Sets irq_adc or irq_chg in max14577_muic_info and returns 1.
  457. * Returns 0 if irq_type does not match registered IRQ for this device type.
  458. */
  459. static int max14577_parse_irq(struct max14577_muic_info *info, int irq_type)
  460. {
  461. switch (irq_type) {
  462. case MAX14577_IRQ_INT1_ADC:
  463. case MAX14577_IRQ_INT1_ADCLOW:
  464. case MAX14577_IRQ_INT1_ADCERR:
  465. /*
  466. * Handle all of accessory except for
  467. * type of charger accessory.
  468. */
  469. info->irq_adc = true;
  470. return 1;
  471. case MAX14577_IRQ_INT2_CHGTYP:
  472. case MAX14577_IRQ_INT2_CHGDETRUN:
  473. case MAX14577_IRQ_INT2_DCDTMR:
  474. case MAX14577_IRQ_INT2_DBCHG:
  475. case MAX14577_IRQ_INT2_VBVOLT:
  476. /* Handle charger accessory */
  477. info->irq_chg = true;
  478. return 1;
  479. default:
  480. return 0;
  481. }
  482. }
  483. /*
  484. * Sets irq_adc or irq_chg in max14577_muic_info and returns 1.
  485. * Returns 0 if irq_type does not match registered IRQ for this device type.
  486. */
  487. static int max77836_parse_irq(struct max14577_muic_info *info, int irq_type)
  488. {
  489. /* First check common max14577 interrupts */
  490. if (max14577_parse_irq(info, irq_type))
  491. return 1;
  492. switch (irq_type) {
  493. case MAX77836_IRQ_INT1_ADC1K:
  494. info->irq_adc = true;
  495. return 1;
  496. case MAX77836_IRQ_INT2_VIDRM:
  497. /* Handle charger accessory */
  498. info->irq_chg = true;
  499. return 1;
  500. default:
  501. return 0;
  502. }
  503. }
  504. static irqreturn_t max14577_muic_irq_handler(int irq, void *data)
  505. {
  506. struct max14577_muic_info *info = data;
  507. int i, irq_type = -1;
  508. bool irq_parsed;
  509. /*
  510. * We may be called multiple times for different nested IRQ-s.
  511. * Including changes in INT1_ADC and INT2_CGHTYP at once.
  512. * However we only need to know whether it was ADC, charger
  513. * or both interrupts so decode IRQ and turn on proper flags.
  514. */
  515. for (i = 0; i < info->muic_irqs_num; i++)
  516. if (irq == info->muic_irqs[i].virq)
  517. irq_type = info->muic_irqs[i].irq;
  518. switch (info->max14577->dev_type) {
  519. case MAXIM_DEVICE_TYPE_MAX77836:
  520. irq_parsed = max77836_parse_irq(info, irq_type);
  521. break;
  522. case MAXIM_DEVICE_TYPE_MAX14577:
  523. default:
  524. irq_parsed = max14577_parse_irq(info, irq_type);
  525. break;
  526. }
  527. if (!irq_parsed) {
  528. dev_err(info->dev, "muic interrupt: irq %d occurred, skipped\n",
  529. irq_type);
  530. return IRQ_HANDLED;
  531. }
  532. schedule_work(&info->irq_work);
  533. return IRQ_HANDLED;
  534. }
  535. static int max14577_muic_detect_accessory(struct max14577_muic_info *info)
  536. {
  537. int ret = 0;
  538. int adc;
  539. int chg_type;
  540. bool attached;
  541. mutex_lock(&info->mutex);
  542. /* Read STATUSx register to detect accessory */
  543. ret = max14577_bulk_read(info->max14577->regmap,
  544. MAX14577_MUIC_REG_STATUS1, info->status, 2);
  545. if (ret) {
  546. dev_err(info->dev, "failed to read MUIC register\n");
  547. mutex_unlock(&info->mutex);
  548. return ret;
  549. }
  550. adc = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_ADC,
  551. &attached);
  552. if (attached && adc != MAX14577_MUIC_ADC_OPEN) {
  553. ret = max14577_muic_adc_handler(info);
  554. if (ret < 0) {
  555. dev_err(info->dev, "Cannot detect accessory\n");
  556. mutex_unlock(&info->mutex);
  557. return ret;
  558. }
  559. }
  560. chg_type = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_CHG,
  561. &attached);
  562. if (attached && chg_type != MAX14577_CHARGER_TYPE_NONE) {
  563. ret = max14577_muic_chg_handler(info);
  564. if (ret < 0) {
  565. dev_err(info->dev, "Cannot detect charger accessory\n");
  566. mutex_unlock(&info->mutex);
  567. return ret;
  568. }
  569. }
  570. mutex_unlock(&info->mutex);
  571. return 0;
  572. }
  573. static void max14577_muic_detect_cable_wq(struct work_struct *work)
  574. {
  575. struct max14577_muic_info *info = container_of(to_delayed_work(work),
  576. struct max14577_muic_info, wq_detcable);
  577. max14577_muic_detect_accessory(info);
  578. }
  579. static int max14577_muic_probe(struct platform_device *pdev)
  580. {
  581. struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
  582. struct max14577_muic_info *info;
  583. int delay_jiffies;
  584. int cable_type;
  585. bool attached;
  586. int ret;
  587. int i;
  588. u8 id;
  589. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  590. if (!info)
  591. return -ENOMEM;
  592. info->dev = &pdev->dev;
  593. info->max14577 = max14577;
  594. platform_set_drvdata(pdev, info);
  595. mutex_init(&info->mutex);
  596. ret = devm_work_autocancel(&pdev->dev, &info->irq_work,
  597. max14577_muic_irq_work);
  598. if (ret)
  599. return ret;
  600. switch (max14577->dev_type) {
  601. case MAXIM_DEVICE_TYPE_MAX77836:
  602. info->muic_irqs = max77836_muic_irqs;
  603. info->muic_irqs_num = ARRAY_SIZE(max77836_muic_irqs);
  604. break;
  605. case MAXIM_DEVICE_TYPE_MAX14577:
  606. default:
  607. info->muic_irqs = max14577_muic_irqs;
  608. info->muic_irqs_num = ARRAY_SIZE(max14577_muic_irqs);
  609. }
  610. /* Support irq domain for max14577 MUIC device */
  611. for (i = 0; i < info->muic_irqs_num; i++) {
  612. struct max14577_muic_irq *muic_irq = &info->muic_irqs[i];
  613. int virq = 0;
  614. virq = regmap_irq_get_virq(max14577->irq_data, muic_irq->irq);
  615. if (virq <= 0)
  616. return -EINVAL;
  617. muic_irq->virq = virq;
  618. ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
  619. max14577_muic_irq_handler,
  620. IRQF_NO_SUSPEND,
  621. muic_irq->name, info);
  622. if (ret) {
  623. dev_err(&pdev->dev,
  624. "failed: irq request (IRQ: %d, error :%d)\n",
  625. muic_irq->irq, ret);
  626. return ret;
  627. }
  628. }
  629. /* Initialize extcon device */
  630. info->edev = devm_extcon_dev_allocate(&pdev->dev,
  631. max14577_extcon_cable);
  632. if (IS_ERR(info->edev)) {
  633. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  634. return PTR_ERR(info->edev);
  635. }
  636. ret = devm_extcon_dev_register(&pdev->dev, info->edev);
  637. if (ret) {
  638. dev_err(&pdev->dev, "failed to register extcon device\n");
  639. return ret;
  640. }
  641. /* Default h/w line path */
  642. info->path_usb = CTRL1_SW_USB;
  643. info->path_uart = CTRL1_SW_UART;
  644. delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  645. /* Set initial path for UART when JIG is connected to get serial logs */
  646. ret = max14577_bulk_read(info->max14577->regmap,
  647. MAX14577_MUIC_REG_STATUS1, info->status, 2);
  648. if (ret) {
  649. dev_err(info->dev, "Cannot read STATUS registers\n");
  650. return ret;
  651. }
  652. cable_type = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_ADC,
  653. &attached);
  654. if (attached && cable_type == MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF)
  655. max14577_muic_set_path(info, info->path_uart, true);
  656. /* Check revision number of MUIC device*/
  657. ret = max14577_read_reg(info->max14577->regmap,
  658. MAX14577_REG_DEVICEID, &id);
  659. if (ret < 0) {
  660. dev_err(&pdev->dev, "failed to read revision number\n");
  661. return ret;
  662. }
  663. dev_info(info->dev, "device ID : 0x%x\n", id);
  664. /* Set ADC debounce time */
  665. max14577_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
  666. /*
  667. * Detect accessory after completing the initialization of platform
  668. *
  669. * - Use delayed workqueue to detect cable state and then
  670. * notify cable state to notifiee/platform through uevent.
  671. * After completing the booting of platform, the extcon provider
  672. * driver should notify cable state to upper layer.
  673. */
  674. INIT_DELAYED_WORK(&info->wq_detcable, max14577_muic_detect_cable_wq);
  675. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  676. delay_jiffies);
  677. return ret;
  678. }
  679. static const struct platform_device_id max14577_muic_id[] = {
  680. { "max14577-muic", MAXIM_DEVICE_TYPE_MAX14577, },
  681. { "max77836-muic", MAXIM_DEVICE_TYPE_MAX77836, },
  682. { }
  683. };
  684. MODULE_DEVICE_TABLE(platform, max14577_muic_id);
  685. static const struct of_device_id of_max14577_muic_dt_match[] = {
  686. { .compatible = "maxim,max14577-muic",
  687. .data = (void *)MAXIM_DEVICE_TYPE_MAX14577, },
  688. { .compatible = "maxim,max77836-muic",
  689. .data = (void *)MAXIM_DEVICE_TYPE_MAX77836, },
  690. { },
  691. };
  692. MODULE_DEVICE_TABLE(of, of_max14577_muic_dt_match);
  693. static struct platform_driver max14577_muic_driver = {
  694. .driver = {
  695. .name = "max14577-muic",
  696. .of_match_table = of_max14577_muic_dt_match,
  697. },
  698. .probe = max14577_muic_probe,
  699. .id_table = max14577_muic_id,
  700. };
  701. module_platform_driver(max14577_muic_driver);
  702. MODULE_DESCRIPTION("Maxim 14577/77836 Extcon driver");
  703. MODULE_AUTHOR("Chanwoo Choi <[email protected]>, Krzysztof Kozlowski <[email protected]>");
  704. MODULE_LICENSE("GPL");
  705. MODULE_ALIAS("platform:extcon-max14577");