extcon-max14577.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * extcon-max14577.c - MAX14577/77836 extcon driver to support MUIC
  3. *
  4. * Copyright (C) 2013,2014 Samsung Electrnoics
  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. /* max14577 MUIC device support below list of accessories(external connector) */
  138. enum {
  139. EXTCON_CABLE_USB = 0,
  140. EXTCON_CABLE_TA,
  141. EXTCON_CABLE_FAST_CHARGER,
  142. EXTCON_CABLE_SLOW_CHARGER,
  143. EXTCON_CABLE_CHARGE_DOWNSTREAM,
  144. EXTCON_CABLE_JIG_USB_ON,
  145. EXTCON_CABLE_JIG_USB_OFF,
  146. EXTCON_CABLE_JIG_UART_OFF,
  147. EXTCON_CABLE_JIG_UART_ON,
  148. _EXTCON_CABLE_NUM,
  149. };
  150. static const char *max14577_extcon_cable[] = {
  151. [EXTCON_CABLE_USB] = "USB",
  152. [EXTCON_CABLE_TA] = "TA",
  153. [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
  154. [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
  155. [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
  156. [EXTCON_CABLE_JIG_USB_ON] = "JIG-USB-ON",
  157. [EXTCON_CABLE_JIG_USB_OFF] = "JIG-USB-OFF",
  158. [EXTCON_CABLE_JIG_UART_OFF] = "JIG-UART-OFF",
  159. [EXTCON_CABLE_JIG_UART_ON] = "JIG-UART-ON",
  160. NULL,
  161. };
  162. /*
  163. * max14577_muic_set_debounce_time - Set the debounce time of ADC
  164. * @info: the instance including private data of max14577 MUIC
  165. * @time: the debounce time of ADC
  166. */
  167. static int max14577_muic_set_debounce_time(struct max14577_muic_info *info,
  168. enum max14577_muic_adc_debounce_time time)
  169. {
  170. u8 ret;
  171. switch (time) {
  172. case ADC_DEBOUNCE_TIME_5MS:
  173. case ADC_DEBOUNCE_TIME_10MS:
  174. case ADC_DEBOUNCE_TIME_25MS:
  175. case ADC_DEBOUNCE_TIME_38_62MS:
  176. ret = max14577_update_reg(info->max14577->regmap,
  177. MAX14577_MUIC_REG_CONTROL3,
  178. CTRL3_ADCDBSET_MASK,
  179. time << CTRL3_ADCDBSET_SHIFT);
  180. if (ret) {
  181. dev_err(info->dev, "failed to set ADC debounce time\n");
  182. return ret;
  183. }
  184. break;
  185. default:
  186. dev_err(info->dev, "invalid ADC debounce time\n");
  187. return -EINVAL;
  188. }
  189. return 0;
  190. };
  191. /*
  192. * max14577_muic_set_path - Set hardware line according to attached cable
  193. * @info: the instance including private data of max14577 MUIC
  194. * @value: the path according to attached cable
  195. * @attached: the state of cable (true:attached, false:detached)
  196. *
  197. * The max14577 MUIC device share outside H/W line among a varity of cables
  198. * so, this function set internal path of H/W line according to the type of
  199. * attached cable.
  200. */
  201. static int max14577_muic_set_path(struct max14577_muic_info *info,
  202. u8 val, bool attached)
  203. {
  204. int ret = 0;
  205. u8 ctrl1, ctrl2 = 0;
  206. /* Set open state to path before changing hw path */
  207. ret = max14577_update_reg(info->max14577->regmap,
  208. MAX14577_MUIC_REG_CONTROL1,
  209. CLEAR_IDBEN_MICEN_MASK, CTRL1_SW_OPEN);
  210. if (ret < 0) {
  211. dev_err(info->dev, "failed to update MUIC register\n");
  212. return ret;
  213. }
  214. if (attached)
  215. ctrl1 = val;
  216. else
  217. ctrl1 = CTRL1_SW_OPEN;
  218. ret = max14577_update_reg(info->max14577->regmap,
  219. MAX14577_MUIC_REG_CONTROL1,
  220. CLEAR_IDBEN_MICEN_MASK, ctrl1);
  221. if (ret < 0) {
  222. dev_err(info->dev, "failed to update MUIC register\n");
  223. return ret;
  224. }
  225. if (attached)
  226. ctrl2 |= CTRL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
  227. else
  228. ctrl2 |= CTRL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
  229. ret = max14577_update_reg(info->max14577->regmap,
  230. MAX14577_REG_CONTROL2,
  231. CTRL2_LOWPWR_MASK | CTRL2_CPEN_MASK, ctrl2);
  232. if (ret < 0) {
  233. dev_err(info->dev, "failed to update MUIC register\n");
  234. return ret;
  235. }
  236. dev_dbg(info->dev,
  237. "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
  238. ctrl1, ctrl2, attached ? "attached" : "detached");
  239. return 0;
  240. }
  241. /*
  242. * max14577_muic_get_cable_type - Return cable type and check cable state
  243. * @info: the instance including private data of max14577 MUIC
  244. * @group: the path according to attached cable
  245. * @attached: store cable state and return
  246. *
  247. * This function check the cable state either attached or detached,
  248. * and then divide precise type of cable according to cable group.
  249. * - max14577_CABLE_GROUP_ADC
  250. * - max14577_CABLE_GROUP_CHG
  251. */
  252. static int max14577_muic_get_cable_type(struct max14577_muic_info *info,
  253. enum max14577_muic_cable_group group, bool *attached)
  254. {
  255. int cable_type = 0;
  256. int adc;
  257. int chg_type;
  258. switch (group) {
  259. case MAX14577_CABLE_GROUP_ADC:
  260. /*
  261. * Read ADC value to check cable type and decide cable state
  262. * according to cable type
  263. */
  264. adc = info->status[MAX14577_MUIC_STATUS1] & STATUS1_ADC_MASK;
  265. adc >>= STATUS1_ADC_SHIFT;
  266. /*
  267. * Check current cable state/cable type and store cable type
  268. * (info->prev_cable_type) for handling cable when cable is
  269. * detached.
  270. */
  271. if (adc == MAX14577_MUIC_ADC_OPEN) {
  272. *attached = false;
  273. cable_type = info->prev_cable_type;
  274. info->prev_cable_type = MAX14577_MUIC_ADC_OPEN;
  275. } else {
  276. *attached = true;
  277. cable_type = info->prev_cable_type = adc;
  278. }
  279. break;
  280. case MAX14577_CABLE_GROUP_CHG:
  281. /*
  282. * Read charger type to check cable type and decide cable state
  283. * according to type of charger cable.
  284. */
  285. chg_type = info->status[MAX14577_MUIC_STATUS2] &
  286. STATUS2_CHGTYP_MASK;
  287. chg_type >>= STATUS2_CHGTYP_SHIFT;
  288. if (chg_type == MAX14577_CHARGER_TYPE_NONE) {
  289. *attached = false;
  290. cable_type = info->prev_chg_type;
  291. info->prev_chg_type = MAX14577_CHARGER_TYPE_NONE;
  292. } else {
  293. *attached = true;
  294. /*
  295. * Check current cable state/cable type and store cable
  296. * type(info->prev_chg_type) for handling cable when
  297. * charger cable is detached.
  298. */
  299. cable_type = info->prev_chg_type = chg_type;
  300. }
  301. break;
  302. default:
  303. dev_err(info->dev, "Unknown cable group (%d)\n", group);
  304. cable_type = -EINVAL;
  305. break;
  306. }
  307. return cable_type;
  308. }
  309. static int max14577_muic_jig_handler(struct max14577_muic_info *info,
  310. int cable_type, bool attached)
  311. {
  312. char cable_name[32];
  313. int ret = 0;
  314. u8 path = CTRL1_SW_OPEN;
  315. dev_dbg(info->dev,
  316. "external connector is %s (adc:0x%02x)\n",
  317. attached ? "attached" : "detached", cable_type);
  318. switch (cable_type) {
  319. case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
  320. /* PATH:AP_USB */
  321. strcpy(cable_name, "JIG-USB-OFF");
  322. path = CTRL1_SW_USB;
  323. break;
  324. case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
  325. /* PATH:AP_USB */
  326. strcpy(cable_name, "JIG-USB-ON");
  327. path = CTRL1_SW_USB;
  328. break;
  329. case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
  330. /* PATH:AP_UART */
  331. strcpy(cable_name, "JIG-UART-OFF");
  332. path = CTRL1_SW_UART;
  333. break;
  334. default:
  335. dev_err(info->dev, "failed to detect %s jig cable\n",
  336. attached ? "attached" : "detached");
  337. return -EINVAL;
  338. }
  339. ret = max14577_muic_set_path(info, path, attached);
  340. if (ret < 0)
  341. return ret;
  342. extcon_set_cable_state(info->edev, cable_name, attached);
  343. return 0;
  344. }
  345. static int max14577_muic_adc_handler(struct max14577_muic_info *info)
  346. {
  347. int cable_type;
  348. bool attached;
  349. int ret = 0;
  350. /* Check accessory state which is either detached or attached */
  351. cable_type = max14577_muic_get_cable_type(info,
  352. MAX14577_CABLE_GROUP_ADC, &attached);
  353. dev_dbg(info->dev,
  354. "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
  355. attached ? "attached" : "detached", cable_type,
  356. info->prev_cable_type);
  357. switch (cable_type) {
  358. case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF:
  359. case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON:
  360. case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF:
  361. /* JIG */
  362. ret = max14577_muic_jig_handler(info, cable_type, attached);
  363. if (ret < 0)
  364. return ret;
  365. break;
  366. case MAX14577_MUIC_ADC_GROUND:
  367. case MAX14577_MUIC_ADC_SEND_END_BUTTON:
  368. case MAX14577_MUIC_ADC_REMOTE_S1_BUTTON:
  369. case MAX14577_MUIC_ADC_REMOTE_S2_BUTTON:
  370. case MAX14577_MUIC_ADC_REMOTE_S3_BUTTON:
  371. case MAX14577_MUIC_ADC_REMOTE_S4_BUTTON:
  372. case MAX14577_MUIC_ADC_REMOTE_S5_BUTTON:
  373. case MAX14577_MUIC_ADC_REMOTE_S6_BUTTON:
  374. case MAX14577_MUIC_ADC_REMOTE_S7_BUTTON:
  375. case MAX14577_MUIC_ADC_REMOTE_S8_BUTTON:
  376. case MAX14577_MUIC_ADC_REMOTE_S9_BUTTON:
  377. case MAX14577_MUIC_ADC_REMOTE_S10_BUTTON:
  378. case MAX14577_MUIC_ADC_REMOTE_S11_BUTTON:
  379. case MAX14577_MUIC_ADC_REMOTE_S12_BUTTON:
  380. case MAX14577_MUIC_ADC_RESERVED_ACC_1:
  381. case MAX14577_MUIC_ADC_RESERVED_ACC_2:
  382. case MAX14577_MUIC_ADC_RESERVED_ACC_3:
  383. case MAX14577_MUIC_ADC_RESERVED_ACC_4:
  384. case MAX14577_MUIC_ADC_RESERVED_ACC_5:
  385. case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2:
  386. case MAX14577_MUIC_ADC_PHONE_POWERED_DEV:
  387. case MAX14577_MUIC_ADC_TTY_CONVERTER:
  388. case MAX14577_MUIC_ADC_UART_CABLE:
  389. case MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG:
  390. case MAX14577_MUIC_ADC_AV_CABLE_NOLOAD:
  391. case MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG:
  392. case MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON:
  393. case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1:
  394. /*
  395. * This accessory isn't used in general case if it is specially
  396. * needed to detect additional accessory, should implement
  397. * proper operation when this accessory is attached/detached.
  398. */
  399. dev_info(info->dev,
  400. "accessory is %s but it isn't used (adc:0x%x)\n",
  401. attached ? "attached" : "detached", cable_type);
  402. return -EAGAIN;
  403. default:
  404. dev_err(info->dev,
  405. "failed to detect %s accessory (adc:0x%x)\n",
  406. attached ? "attached" : "detached", cable_type);
  407. return -EINVAL;
  408. }
  409. return 0;
  410. }
  411. static int max14577_muic_chg_handler(struct max14577_muic_info *info)
  412. {
  413. int chg_type;
  414. bool attached;
  415. int ret = 0;
  416. chg_type = max14577_muic_get_cable_type(info,
  417. MAX14577_CABLE_GROUP_CHG, &attached);
  418. dev_dbg(info->dev,
  419. "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
  420. attached ? "attached" : "detached",
  421. chg_type, info->prev_chg_type);
  422. switch (chg_type) {
  423. case MAX14577_CHARGER_TYPE_USB:
  424. /* PATH:AP_USB */
  425. ret = max14577_muic_set_path(info, info->path_usb, attached);
  426. if (ret < 0)
  427. return ret;
  428. extcon_set_cable_state(info->edev, "USB", attached);
  429. break;
  430. case MAX14577_CHARGER_TYPE_DEDICATED_CHG:
  431. extcon_set_cable_state(info->edev, "TA", attached);
  432. break;
  433. case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT:
  434. extcon_set_cable_state(info->edev,
  435. "Charge-downstream", attached);
  436. break;
  437. case MAX14577_CHARGER_TYPE_SPECIAL_500MA:
  438. extcon_set_cable_state(info->edev, "Slow-charger", attached);
  439. break;
  440. case MAX14577_CHARGER_TYPE_SPECIAL_1A:
  441. extcon_set_cable_state(info->edev, "Fast-charger", attached);
  442. break;
  443. case MAX14577_CHARGER_TYPE_NONE:
  444. case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
  445. break;
  446. default:
  447. dev_err(info->dev,
  448. "failed to detect %s accessory (chg_type:0x%x)\n",
  449. attached ? "attached" : "detached", chg_type);
  450. return -EINVAL;
  451. }
  452. return 0;
  453. }
  454. static void max14577_muic_irq_work(struct work_struct *work)
  455. {
  456. struct max14577_muic_info *info = container_of(work,
  457. struct max14577_muic_info, irq_work);
  458. int ret = 0;
  459. if (!info->edev)
  460. return;
  461. mutex_lock(&info->mutex);
  462. ret = max14577_bulk_read(info->max14577->regmap,
  463. MAX14577_MUIC_REG_STATUS1, info->status, 2);
  464. if (ret) {
  465. dev_err(info->dev, "failed to read MUIC register\n");
  466. mutex_unlock(&info->mutex);
  467. return;
  468. }
  469. if (info->irq_adc) {
  470. ret = max14577_muic_adc_handler(info);
  471. info->irq_adc = false;
  472. }
  473. if (info->irq_chg) {
  474. ret = max14577_muic_chg_handler(info);
  475. info->irq_chg = false;
  476. }
  477. if (ret < 0)
  478. dev_err(info->dev, "failed to handle MUIC interrupt\n");
  479. mutex_unlock(&info->mutex);
  480. return;
  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 max14577_parse_irq(struct max14577_muic_info *info, int irq_type)
  487. {
  488. switch (irq_type) {
  489. case MAX14577_IRQ_INT1_ADC:
  490. case MAX14577_IRQ_INT1_ADCLOW:
  491. case MAX14577_IRQ_INT1_ADCERR:
  492. /* Handle all of accessory except for
  493. type of charger accessory */
  494. info->irq_adc = true;
  495. return 1;
  496. case MAX14577_IRQ_INT2_CHGTYP:
  497. case MAX14577_IRQ_INT2_CHGDETRUN:
  498. case MAX14577_IRQ_INT2_DCDTMR:
  499. case MAX14577_IRQ_INT2_DBCHG:
  500. case MAX14577_IRQ_INT2_VBVOLT:
  501. /* Handle charger accessory */
  502. info->irq_chg = true;
  503. return 1;
  504. default:
  505. return 0;
  506. }
  507. }
  508. /*
  509. * Sets irq_adc or irq_chg in max14577_muic_info and returns 1.
  510. * Returns 0 if irq_type does not match registered IRQ for this device type.
  511. */
  512. static int max77836_parse_irq(struct max14577_muic_info *info, int irq_type)
  513. {
  514. /* First check common max14577 interrupts */
  515. if (max14577_parse_irq(info, irq_type))
  516. return 1;
  517. switch (irq_type) {
  518. case MAX77836_IRQ_INT1_ADC1K:
  519. info->irq_adc = true;
  520. return 1;
  521. case MAX77836_IRQ_INT2_VIDRM:
  522. /* Handle charger accessory */
  523. info->irq_chg = true;
  524. return 1;
  525. default:
  526. return 0;
  527. }
  528. }
  529. static irqreturn_t max14577_muic_irq_handler(int irq, void *data)
  530. {
  531. struct max14577_muic_info *info = data;
  532. int i, irq_type = -1;
  533. bool irq_parsed;
  534. /*
  535. * We may be called multiple times for different nested IRQ-s.
  536. * Including changes in INT1_ADC and INT2_CGHTYP at once.
  537. * However we only need to know whether it was ADC, charger
  538. * or both interrupts so decode IRQ and turn on proper flags.
  539. */
  540. for (i = 0; i < info->muic_irqs_num; i++)
  541. if (irq == info->muic_irqs[i].virq)
  542. irq_type = info->muic_irqs[i].irq;
  543. switch (info->max14577->dev_type) {
  544. case MAXIM_DEVICE_TYPE_MAX77836:
  545. irq_parsed = max77836_parse_irq(info, irq_type);
  546. break;
  547. case MAXIM_DEVICE_TYPE_MAX14577:
  548. default:
  549. irq_parsed = max14577_parse_irq(info, irq_type);
  550. break;
  551. }
  552. if (!irq_parsed) {
  553. dev_err(info->dev, "muic interrupt: irq %d occurred, skipped\n",
  554. irq_type);
  555. return IRQ_HANDLED;
  556. }
  557. schedule_work(&info->irq_work);
  558. return IRQ_HANDLED;
  559. }
  560. static int max14577_muic_detect_accessory(struct max14577_muic_info *info)
  561. {
  562. int ret = 0;
  563. int adc;
  564. int chg_type;
  565. bool attached;
  566. mutex_lock(&info->mutex);
  567. /* Read STATUSx register to detect accessory */
  568. ret = max14577_bulk_read(info->max14577->regmap,
  569. MAX14577_MUIC_REG_STATUS1, info->status, 2);
  570. if (ret) {
  571. dev_err(info->dev, "failed to read MUIC register\n");
  572. mutex_unlock(&info->mutex);
  573. return ret;
  574. }
  575. adc = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_ADC,
  576. &attached);
  577. if (attached && adc != MAX14577_MUIC_ADC_OPEN) {
  578. ret = max14577_muic_adc_handler(info);
  579. if (ret < 0) {
  580. dev_err(info->dev, "Cannot detect accessory\n");
  581. mutex_unlock(&info->mutex);
  582. return ret;
  583. }
  584. }
  585. chg_type = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_CHG,
  586. &attached);
  587. if (attached && chg_type != MAX14577_CHARGER_TYPE_NONE) {
  588. ret = max14577_muic_chg_handler(info);
  589. if (ret < 0) {
  590. dev_err(info->dev, "Cannot detect charger accessory\n");
  591. mutex_unlock(&info->mutex);
  592. return ret;
  593. }
  594. }
  595. mutex_unlock(&info->mutex);
  596. return 0;
  597. }
  598. static void max14577_muic_detect_cable_wq(struct work_struct *work)
  599. {
  600. struct max14577_muic_info *info = container_of(to_delayed_work(work),
  601. struct max14577_muic_info, wq_detcable);
  602. max14577_muic_detect_accessory(info);
  603. }
  604. static int max14577_muic_probe(struct platform_device *pdev)
  605. {
  606. struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
  607. struct max14577_muic_info *info;
  608. int delay_jiffies;
  609. int ret;
  610. int i;
  611. u8 id;
  612. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  613. if (!info) {
  614. dev_err(&pdev->dev, "failed to allocate memory\n");
  615. return -ENOMEM;
  616. }
  617. info->dev = &pdev->dev;
  618. info->max14577 = max14577;
  619. platform_set_drvdata(pdev, info);
  620. mutex_init(&info->mutex);
  621. INIT_WORK(&info->irq_work, max14577_muic_irq_work);
  622. switch (max14577->dev_type) {
  623. case MAXIM_DEVICE_TYPE_MAX77836:
  624. info->muic_irqs = max77836_muic_irqs;
  625. info->muic_irqs_num = ARRAY_SIZE(max77836_muic_irqs);
  626. break;
  627. case MAXIM_DEVICE_TYPE_MAX14577:
  628. default:
  629. info->muic_irqs = max14577_muic_irqs;
  630. info->muic_irqs_num = ARRAY_SIZE(max14577_muic_irqs);
  631. }
  632. /* Support irq domain for max14577 MUIC device */
  633. for (i = 0; i < info->muic_irqs_num; i++) {
  634. struct max14577_muic_irq *muic_irq = &info->muic_irqs[i];
  635. unsigned int virq = 0;
  636. virq = regmap_irq_get_virq(max14577->irq_data, muic_irq->irq);
  637. if (virq <= 0)
  638. return -EINVAL;
  639. muic_irq->virq = virq;
  640. ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
  641. max14577_muic_irq_handler,
  642. IRQF_NO_SUSPEND,
  643. muic_irq->name, info);
  644. if (ret) {
  645. dev_err(&pdev->dev,
  646. "failed: irq request (IRQ: %d,"
  647. " error :%d)\n",
  648. muic_irq->irq, ret);
  649. return ret;
  650. }
  651. }
  652. /* Initialize extcon device */
  653. info->edev = devm_extcon_dev_allocate(&pdev->dev,
  654. max14577_extcon_cable);
  655. if (IS_ERR(info->edev)) {
  656. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  657. return -ENOMEM;
  658. }
  659. info->edev->name = dev_name(&pdev->dev);
  660. ret = devm_extcon_dev_register(&pdev->dev, info->edev);
  661. if (ret) {
  662. dev_err(&pdev->dev, "failed to register extcon device\n");
  663. return ret;
  664. }
  665. /* Default h/w line path */
  666. info->path_usb = CTRL1_SW_USB;
  667. info->path_uart = CTRL1_SW_UART;
  668. delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  669. /* Set initial path for UART */
  670. max14577_muic_set_path(info, info->path_uart, true);
  671. /* Check revision number of MUIC device*/
  672. ret = max14577_read_reg(info->max14577->regmap,
  673. MAX14577_REG_DEVICEID, &id);
  674. if (ret < 0) {
  675. dev_err(&pdev->dev, "failed to read revision number\n");
  676. return ret;
  677. }
  678. dev_info(info->dev, "device ID : 0x%x\n", id);
  679. /* Set ADC debounce time */
  680. max14577_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
  681. /*
  682. * Detect accessory after completing the initialization of platform
  683. *
  684. * - Use delayed workqueue to detect cable state and then
  685. * notify cable state to notifiee/platform through uevent.
  686. * After completing the booting of platform, the extcon provider
  687. * driver should notify cable state to upper layer.
  688. */
  689. INIT_DELAYED_WORK(&info->wq_detcable, max14577_muic_detect_cable_wq);
  690. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  691. delay_jiffies);
  692. return ret;
  693. }
  694. static int max14577_muic_remove(struct platform_device *pdev)
  695. {
  696. struct max14577_muic_info *info = platform_get_drvdata(pdev);
  697. cancel_work_sync(&info->irq_work);
  698. return 0;
  699. }
  700. static const struct platform_device_id max14577_muic_id[] = {
  701. { "max14577-muic", MAXIM_DEVICE_TYPE_MAX14577, },
  702. { "max77836-muic", MAXIM_DEVICE_TYPE_MAX77836, },
  703. { }
  704. };
  705. MODULE_DEVICE_TABLE(platform, max14577_muic_id);
  706. static struct platform_driver max14577_muic_driver = {
  707. .driver = {
  708. .name = "max14577-muic",
  709. .owner = THIS_MODULE,
  710. },
  711. .probe = max14577_muic_probe,
  712. .remove = max14577_muic_remove,
  713. .id_table = max14577_muic_id,
  714. };
  715. module_platform_driver(max14577_muic_driver);
  716. MODULE_DESCRIPTION("Maxim 14577/77836 Extcon driver");
  717. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>");
  718. MODULE_LICENSE("GPL");
  719. MODULE_ALIAS("platform:extcon-max14577");