extcon-max14577.c 21 KB

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