extcon-max14577.c 20 KB

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