extcon-max14577.c 21 KB

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