extcon-max14577.c 21 KB

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