extcon-max8997.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
  4. //
  5. // Copyright (C) 2012 Samsung Electronics
  6. // Donggeun Kim <dg77.kim@samsung.com>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/i2c.h>
  10. #include <linux/slab.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/err.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/kobject.h>
  15. #include <linux/mfd/max8997.h>
  16. #include <linux/mfd/max8997-private.h>
  17. #include <linux/extcon-provider.h>
  18. #include <linux/irqdomain.h>
  19. #define DEV_NAME "max8997-muic"
  20. #define DELAY_MS_DEFAULT 20000 /* unit: millisecond */
  21. enum max8997_muic_adc_debounce_time {
  22. ADC_DEBOUNCE_TIME_0_5MS = 0, /* 0.5ms */
  23. ADC_DEBOUNCE_TIME_10MS, /* 10ms */
  24. ADC_DEBOUNCE_TIME_25MS, /* 25ms */
  25. ADC_DEBOUNCE_TIME_38_62MS, /* 38.62ms */
  26. };
  27. struct max8997_muic_irq {
  28. unsigned int irq;
  29. const char *name;
  30. unsigned int virq;
  31. };
  32. static struct max8997_muic_irq muic_irqs[] = {
  33. { MAX8997_MUICIRQ_ADCError, "muic-ADCERROR" },
  34. { MAX8997_MUICIRQ_ADCLow, "muic-ADCLOW" },
  35. { MAX8997_MUICIRQ_ADC, "muic-ADC" },
  36. { MAX8997_MUICIRQ_VBVolt, "muic-VBVOLT" },
  37. { MAX8997_MUICIRQ_DBChg, "muic-DBCHG" },
  38. { MAX8997_MUICIRQ_DCDTmr, "muic-DCDTMR" },
  39. { MAX8997_MUICIRQ_ChgDetRun, "muic-CHGDETRUN" },
  40. { MAX8997_MUICIRQ_ChgTyp, "muic-CHGTYP" },
  41. { MAX8997_MUICIRQ_OVP, "muic-OVP" },
  42. };
  43. /* Define supported cable type */
  44. enum max8997_muic_acc_type {
  45. MAX8997_MUIC_ADC_GROUND = 0x0,
  46. MAX8997_MUIC_ADC_MHL, /* MHL*/
  47. MAX8997_MUIC_ADC_REMOTE_S1_BUTTON,
  48. MAX8997_MUIC_ADC_REMOTE_S2_BUTTON,
  49. MAX8997_MUIC_ADC_REMOTE_S3_BUTTON,
  50. MAX8997_MUIC_ADC_REMOTE_S4_BUTTON,
  51. MAX8997_MUIC_ADC_REMOTE_S5_BUTTON,
  52. MAX8997_MUIC_ADC_REMOTE_S6_BUTTON,
  53. MAX8997_MUIC_ADC_REMOTE_S7_BUTTON,
  54. MAX8997_MUIC_ADC_REMOTE_S8_BUTTON,
  55. MAX8997_MUIC_ADC_REMOTE_S9_BUTTON,
  56. MAX8997_MUIC_ADC_REMOTE_S10_BUTTON,
  57. MAX8997_MUIC_ADC_REMOTE_S11_BUTTON,
  58. MAX8997_MUIC_ADC_REMOTE_S12_BUTTON,
  59. MAX8997_MUIC_ADC_RESERVED_ACC_1,
  60. MAX8997_MUIC_ADC_RESERVED_ACC_2,
  61. MAX8997_MUIC_ADC_RESERVED_ACC_3,
  62. MAX8997_MUIC_ADC_RESERVED_ACC_4,
  63. MAX8997_MUIC_ADC_RESERVED_ACC_5,
  64. MAX8997_MUIC_ADC_CEA936_AUDIO,
  65. MAX8997_MUIC_ADC_PHONE_POWERED_DEV,
  66. MAX8997_MUIC_ADC_TTY_CONVERTER,
  67. MAX8997_MUIC_ADC_UART_CABLE,
  68. MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG,
  69. MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF, /* JIG-USB-OFF */
  70. MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON, /* JIG-USB-ON */
  71. MAX8997_MUIC_ADC_AV_CABLE_NOLOAD, /* DESKDOCK */
  72. MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG,
  73. MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF, /* JIG-UART */
  74. MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON, /* CARDOCK */
  75. MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE,
  76. MAX8997_MUIC_ADC_OPEN, /* OPEN */
  77. };
  78. enum max8997_muic_cable_group {
  79. MAX8997_CABLE_GROUP_ADC = 0,
  80. MAX8997_CABLE_GROUP_ADC_GND,
  81. MAX8997_CABLE_GROUP_CHG,
  82. MAX8997_CABLE_GROUP_VBVOLT,
  83. };
  84. enum max8997_muic_usb_type {
  85. MAX8997_USB_HOST,
  86. MAX8997_USB_DEVICE,
  87. };
  88. enum max8997_muic_charger_type {
  89. MAX8997_CHARGER_TYPE_NONE = 0,
  90. MAX8997_CHARGER_TYPE_USB,
  91. MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT,
  92. MAX8997_CHARGER_TYPE_DEDICATED_CHG,
  93. MAX8997_CHARGER_TYPE_500MA,
  94. MAX8997_CHARGER_TYPE_1A,
  95. MAX8997_CHARGER_TYPE_DEAD_BATTERY = 7,
  96. };
  97. struct max8997_muic_info {
  98. struct device *dev;
  99. struct i2c_client *muic;
  100. struct extcon_dev *edev;
  101. int prev_cable_type;
  102. int prev_chg_type;
  103. u8 status[2];
  104. int irq;
  105. struct work_struct irq_work;
  106. struct mutex mutex;
  107. struct max8997_muic_platform_data *muic_pdata;
  108. enum max8997_muic_charger_type pre_charger_type;
  109. /*
  110. * Use delayed workqueue to detect cable state and then
  111. * notify cable state to notifiee/platform through uevent.
  112. * After completing the booting of platform, the extcon provider
  113. * driver should notify cable state to upper layer.
  114. */
  115. struct delayed_work wq_detcable;
  116. /*
  117. * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
  118. * h/w path of COMP2/COMN1 on CONTROL1 register.
  119. */
  120. int path_usb;
  121. int path_uart;
  122. };
  123. static const unsigned int max8997_extcon_cable[] = {
  124. EXTCON_USB,
  125. EXTCON_USB_HOST,
  126. EXTCON_CHG_USB_SDP,
  127. EXTCON_CHG_USB_DCP,
  128. EXTCON_CHG_USB_FAST,
  129. EXTCON_CHG_USB_SLOW,
  130. EXTCON_CHG_USB_CDP,
  131. EXTCON_DISP_MHL,
  132. EXTCON_DOCK,
  133. EXTCON_JIG,
  134. EXTCON_NONE,
  135. };
  136. /*
  137. * max8997_muic_set_debounce_time - Set the debounce time of ADC
  138. * @info: the instance including private data of max8997 MUIC
  139. * @time: the debounce time of ADC
  140. */
  141. static int max8997_muic_set_debounce_time(struct max8997_muic_info *info,
  142. enum max8997_muic_adc_debounce_time time)
  143. {
  144. int ret;
  145. switch (time) {
  146. case ADC_DEBOUNCE_TIME_0_5MS:
  147. case ADC_DEBOUNCE_TIME_10MS:
  148. case ADC_DEBOUNCE_TIME_25MS:
  149. case ADC_DEBOUNCE_TIME_38_62MS:
  150. ret = max8997_update_reg(info->muic,
  151. MAX8997_MUIC_REG_CONTROL3,
  152. time << CONTROL3_ADCDBSET_SHIFT,
  153. CONTROL3_ADCDBSET_MASK);
  154. if (ret) {
  155. dev_err(info->dev, "failed to set ADC debounce time\n");
  156. return ret;
  157. }
  158. break;
  159. default:
  160. dev_err(info->dev, "invalid ADC debounce time\n");
  161. return -EINVAL;
  162. }
  163. return 0;
  164. };
  165. /*
  166. * max8997_muic_set_path - Set hardware line according to attached cable
  167. * @info: the instance including private data of max8997 MUIC
  168. * @value: the path according to attached cable
  169. * @attached: the state of cable (true:attached, false:detached)
  170. *
  171. * The max8997 MUIC device share outside H/W line among a varity of cables,
  172. * so this function set internal path of H/W line according to the type of
  173. * attached cable.
  174. */
  175. static int max8997_muic_set_path(struct max8997_muic_info *info,
  176. u8 val, bool attached)
  177. {
  178. int ret;
  179. u8 ctrl1, ctrl2 = 0;
  180. if (attached)
  181. ctrl1 = val;
  182. else
  183. ctrl1 = CONTROL1_SW_OPEN;
  184. ret = max8997_update_reg(info->muic,
  185. MAX8997_MUIC_REG_CONTROL1, ctrl1, COMP_SW_MASK);
  186. if (ret < 0) {
  187. dev_err(info->dev, "failed to update MUIC register\n");
  188. return ret;
  189. }
  190. if (attached)
  191. ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
  192. else
  193. ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
  194. ret = max8997_update_reg(info->muic,
  195. MAX8997_MUIC_REG_CONTROL2, ctrl2,
  196. CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
  197. if (ret < 0) {
  198. dev_err(info->dev, "failed to update MUIC register\n");
  199. return ret;
  200. }
  201. dev_info(info->dev,
  202. "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
  203. ctrl1, ctrl2, attached ? "attached" : "detached");
  204. return 0;
  205. }
  206. /*
  207. * max8997_muic_get_cable_type - Return cable type and check cable state
  208. * @info: the instance including private data of max8997 MUIC
  209. * @group: the path according to attached cable
  210. * @attached: store cable state and return
  211. *
  212. * This function check the cable state either attached or detached,
  213. * and then divide precise type of cable according to cable group.
  214. * - MAX8997_CABLE_GROUP_ADC
  215. * - MAX8997_CABLE_GROUP_CHG
  216. */
  217. static int max8997_muic_get_cable_type(struct max8997_muic_info *info,
  218. enum max8997_muic_cable_group group, bool *attached)
  219. {
  220. int cable_type = 0;
  221. int adc;
  222. int chg_type;
  223. switch (group) {
  224. case MAX8997_CABLE_GROUP_ADC:
  225. /*
  226. * Read ADC value to check cable type and decide cable state
  227. * according to cable type
  228. */
  229. adc = info->status[0] & STATUS1_ADC_MASK;
  230. adc >>= STATUS1_ADC_SHIFT;
  231. /*
  232. * Check current cable state/cable type and store cable type
  233. * (info->prev_cable_type) for handling cable when cable is
  234. * detached.
  235. */
  236. if (adc == MAX8997_MUIC_ADC_OPEN) {
  237. *attached = false;
  238. cable_type = info->prev_cable_type;
  239. info->prev_cable_type = MAX8997_MUIC_ADC_OPEN;
  240. } else {
  241. *attached = true;
  242. cable_type = info->prev_cable_type = adc;
  243. }
  244. break;
  245. case MAX8997_CABLE_GROUP_CHG:
  246. /*
  247. * Read charger type to check cable type and decide cable state
  248. * according to type of charger cable.
  249. */
  250. chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
  251. chg_type >>= STATUS2_CHGTYP_SHIFT;
  252. if (chg_type == MAX8997_CHARGER_TYPE_NONE) {
  253. *attached = false;
  254. cable_type = info->prev_chg_type;
  255. info->prev_chg_type = MAX8997_CHARGER_TYPE_NONE;
  256. } else {
  257. *attached = true;
  258. /*
  259. * Check current cable state/cable type and store cable
  260. * type(info->prev_chg_type) for handling cable when
  261. * charger cable is detached.
  262. */
  263. cable_type = info->prev_chg_type = chg_type;
  264. }
  265. break;
  266. default:
  267. dev_err(info->dev, "Unknown cable group (%d)\n", group);
  268. cable_type = -EINVAL;
  269. break;
  270. }
  271. return cable_type;
  272. }
  273. static int max8997_muic_handle_usb(struct max8997_muic_info *info,
  274. enum max8997_muic_usb_type usb_type, bool attached)
  275. {
  276. int ret = 0;
  277. if (usb_type == MAX8997_USB_HOST) {
  278. ret = max8997_muic_set_path(info, info->path_usb, attached);
  279. if (ret < 0) {
  280. dev_err(info->dev, "failed to update muic register\n");
  281. return ret;
  282. }
  283. }
  284. switch (usb_type) {
  285. case MAX8997_USB_HOST:
  286. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached);
  287. break;
  288. case MAX8997_USB_DEVICE:
  289. extcon_set_state_sync(info->edev, EXTCON_USB, attached);
  290. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
  291. attached);
  292. break;
  293. default:
  294. dev_err(info->dev, "failed to detect %s usb cable\n",
  295. attached ? "attached" : "detached");
  296. return -EINVAL;
  297. }
  298. return 0;
  299. }
  300. static int max8997_muic_handle_dock(struct max8997_muic_info *info,
  301. int cable_type, bool attached)
  302. {
  303. int ret = 0;
  304. ret = max8997_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
  305. if (ret) {
  306. dev_err(info->dev, "failed to update muic register\n");
  307. return ret;
  308. }
  309. switch (cable_type) {
  310. case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
  311. case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
  312. extcon_set_state_sync(info->edev, EXTCON_DOCK, attached);
  313. break;
  314. default:
  315. dev_err(info->dev, "failed to detect %s dock device\n",
  316. attached ? "attached" : "detached");
  317. return -EINVAL;
  318. }
  319. return 0;
  320. }
  321. static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info,
  322. bool attached)
  323. {
  324. int ret = 0;
  325. /* switch to UART */
  326. ret = max8997_muic_set_path(info, info->path_uart, attached);
  327. if (ret) {
  328. dev_err(info->dev, "failed to update muic register\n");
  329. return ret;
  330. }
  331. extcon_set_state_sync(info->edev, EXTCON_JIG, attached);
  332. return 0;
  333. }
  334. static int max8997_muic_adc_handler(struct max8997_muic_info *info)
  335. {
  336. int cable_type;
  337. bool attached;
  338. int ret = 0;
  339. /* Check cable state which is either detached or attached */
  340. cable_type = max8997_muic_get_cable_type(info,
  341. MAX8997_CABLE_GROUP_ADC, &attached);
  342. switch (cable_type) {
  343. case MAX8997_MUIC_ADC_GROUND:
  344. ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, attached);
  345. if (ret < 0)
  346. return ret;
  347. break;
  348. case MAX8997_MUIC_ADC_MHL:
  349. extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
  350. break;
  351. case MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF:
  352. case MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON:
  353. ret = max8997_muic_handle_usb(info,
  354. MAX8997_USB_DEVICE, attached);
  355. if (ret < 0)
  356. return ret;
  357. break;
  358. case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
  359. case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
  360. ret = max8997_muic_handle_dock(info, cable_type, attached);
  361. if (ret < 0)
  362. return ret;
  363. break;
  364. case MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF:
  365. ret = max8997_muic_handle_jig_uart(info, attached);
  366. break;
  367. case MAX8997_MUIC_ADC_REMOTE_S1_BUTTON:
  368. case MAX8997_MUIC_ADC_REMOTE_S2_BUTTON:
  369. case MAX8997_MUIC_ADC_REMOTE_S3_BUTTON:
  370. case MAX8997_MUIC_ADC_REMOTE_S4_BUTTON:
  371. case MAX8997_MUIC_ADC_REMOTE_S5_BUTTON:
  372. case MAX8997_MUIC_ADC_REMOTE_S6_BUTTON:
  373. case MAX8997_MUIC_ADC_REMOTE_S7_BUTTON:
  374. case MAX8997_MUIC_ADC_REMOTE_S8_BUTTON:
  375. case MAX8997_MUIC_ADC_REMOTE_S9_BUTTON:
  376. case MAX8997_MUIC_ADC_REMOTE_S10_BUTTON:
  377. case MAX8997_MUIC_ADC_REMOTE_S11_BUTTON:
  378. case MAX8997_MUIC_ADC_REMOTE_S12_BUTTON:
  379. case MAX8997_MUIC_ADC_RESERVED_ACC_1:
  380. case MAX8997_MUIC_ADC_RESERVED_ACC_2:
  381. case MAX8997_MUIC_ADC_RESERVED_ACC_3:
  382. case MAX8997_MUIC_ADC_RESERVED_ACC_4:
  383. case MAX8997_MUIC_ADC_RESERVED_ACC_5:
  384. case MAX8997_MUIC_ADC_CEA936_AUDIO:
  385. case MAX8997_MUIC_ADC_PHONE_POWERED_DEV:
  386. case MAX8997_MUIC_ADC_TTY_CONVERTER:
  387. case MAX8997_MUIC_ADC_UART_CABLE:
  388. case MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG:
  389. case MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG:
  390. case MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE:
  391. /*
  392. * This cable isn't used in general case if it is specially
  393. * needed to detect additional cable, should implement
  394. * proper operation when this cable is attached/detached.
  395. */
  396. dev_info(info->dev,
  397. "cable is %s but it isn't used (type:0x%x)\n",
  398. attached ? "attached" : "detached", cable_type);
  399. return -EAGAIN;
  400. default:
  401. dev_err(info->dev,
  402. "failed to detect %s unknown cable (type:0x%x)\n",
  403. attached ? "attached" : "detached", cable_type);
  404. return -EINVAL;
  405. }
  406. return 0;
  407. }
  408. static int max8997_muic_chg_handler(struct max8997_muic_info *info)
  409. {
  410. int chg_type;
  411. bool attached;
  412. int adc;
  413. chg_type = max8997_muic_get_cable_type(info,
  414. MAX8997_CABLE_GROUP_CHG, &attached);
  415. switch (chg_type) {
  416. case MAX8997_CHARGER_TYPE_NONE:
  417. break;
  418. case MAX8997_CHARGER_TYPE_USB:
  419. adc = info->status[0] & STATUS1_ADC_MASK;
  420. adc >>= STATUS1_ADC_SHIFT;
  421. if ((adc & STATUS1_ADC_MASK) == MAX8997_MUIC_ADC_OPEN) {
  422. max8997_muic_handle_usb(info,
  423. MAX8997_USB_DEVICE, attached);
  424. }
  425. break;
  426. case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
  427. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP,
  428. attached);
  429. break;
  430. case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
  431. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
  432. attached);
  433. break;
  434. case MAX8997_CHARGER_TYPE_500MA:
  435. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW,
  436. attached);
  437. break;
  438. case MAX8997_CHARGER_TYPE_1A:
  439. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST,
  440. attached);
  441. break;
  442. default:
  443. dev_err(info->dev,
  444. "failed to detect %s unknown chg cable (type:0x%x)\n",
  445. attached ? "attached" : "detached", chg_type);
  446. return -EINVAL;
  447. }
  448. return 0;
  449. }
  450. static void max8997_muic_irq_work(struct work_struct *work)
  451. {
  452. struct max8997_muic_info *info = container_of(work,
  453. struct max8997_muic_info, irq_work);
  454. int irq_type = 0;
  455. int i, ret;
  456. if (!info->edev)
  457. return;
  458. mutex_lock(&info->mutex);
  459. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
  460. if (info->irq == muic_irqs[i].virq)
  461. irq_type = muic_irqs[i].irq;
  462. ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
  463. 2, info->status);
  464. if (ret) {
  465. dev_err(info->dev, "failed to read muic register\n");
  466. mutex_unlock(&info->mutex);
  467. return;
  468. }
  469. switch (irq_type) {
  470. case MAX8997_MUICIRQ_ADCError:
  471. case MAX8997_MUICIRQ_ADCLow:
  472. case MAX8997_MUICIRQ_ADC:
  473. /* Handle all of cable except for charger cable */
  474. ret = max8997_muic_adc_handler(info);
  475. break;
  476. case MAX8997_MUICIRQ_VBVolt:
  477. case MAX8997_MUICIRQ_DBChg:
  478. case MAX8997_MUICIRQ_DCDTmr:
  479. case MAX8997_MUICIRQ_ChgDetRun:
  480. case MAX8997_MUICIRQ_ChgTyp:
  481. /* Handle charger cable */
  482. ret = max8997_muic_chg_handler(info);
  483. break;
  484. case MAX8997_MUICIRQ_OVP:
  485. break;
  486. default:
  487. dev_info(info->dev, "misc interrupt: irq %d occurred\n",
  488. irq_type);
  489. mutex_unlock(&info->mutex);
  490. return;
  491. }
  492. if (ret < 0)
  493. dev_err(info->dev, "failed to handle MUIC interrupt\n");
  494. mutex_unlock(&info->mutex);
  495. }
  496. static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
  497. {
  498. struct max8997_muic_info *info = data;
  499. dev_dbg(info->dev, "irq:%d\n", irq);
  500. info->irq = irq;
  501. schedule_work(&info->irq_work);
  502. return IRQ_HANDLED;
  503. }
  504. static int max8997_muic_detect_dev(struct max8997_muic_info *info)
  505. {
  506. int ret = 0;
  507. int adc;
  508. int chg_type;
  509. bool attached;
  510. mutex_lock(&info->mutex);
  511. /* Read STATUSx register to detect accessory */
  512. ret = max8997_bulk_read(info->muic,
  513. MAX8997_MUIC_REG_STATUS1, 2, info->status);
  514. if (ret) {
  515. dev_err(info->dev, "failed to read MUIC register\n");
  516. mutex_unlock(&info->mutex);
  517. return ret;
  518. }
  519. adc = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_ADC,
  520. &attached);
  521. if (attached && adc != MAX8997_MUIC_ADC_OPEN) {
  522. ret = max8997_muic_adc_handler(info);
  523. if (ret < 0) {
  524. dev_err(info->dev, "Cannot detect ADC cable\n");
  525. mutex_unlock(&info->mutex);
  526. return ret;
  527. }
  528. }
  529. chg_type = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_CHG,
  530. &attached);
  531. if (attached && chg_type != MAX8997_CHARGER_TYPE_NONE) {
  532. ret = max8997_muic_chg_handler(info);
  533. if (ret < 0) {
  534. dev_err(info->dev, "Cannot detect charger cable\n");
  535. mutex_unlock(&info->mutex);
  536. return ret;
  537. }
  538. }
  539. mutex_unlock(&info->mutex);
  540. return 0;
  541. }
  542. static void max8997_muic_detect_cable_wq(struct work_struct *work)
  543. {
  544. struct max8997_muic_info *info = container_of(to_delayed_work(work),
  545. struct max8997_muic_info, wq_detcable);
  546. int ret;
  547. ret = max8997_muic_detect_dev(info);
  548. if (ret < 0)
  549. dev_err(info->dev, "failed to detect cable type\n");
  550. }
  551. static int max8997_muic_probe(struct platform_device *pdev)
  552. {
  553. struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
  554. struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
  555. struct max8997_muic_info *info;
  556. int delay_jiffies;
  557. int ret, i;
  558. info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_muic_info),
  559. GFP_KERNEL);
  560. if (!info)
  561. return -ENOMEM;
  562. info->dev = &pdev->dev;
  563. info->muic = max8997->muic;
  564. platform_set_drvdata(pdev, info);
  565. mutex_init(&info->mutex);
  566. INIT_WORK(&info->irq_work, max8997_muic_irq_work);
  567. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
  568. struct max8997_muic_irq *muic_irq = &muic_irqs[i];
  569. unsigned int virq = 0;
  570. virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
  571. if (!virq) {
  572. ret = -EINVAL;
  573. goto err_irq;
  574. }
  575. muic_irq->virq = virq;
  576. ret = request_threaded_irq(virq, NULL,
  577. max8997_muic_irq_handler,
  578. IRQF_NO_SUSPEND,
  579. muic_irq->name, info);
  580. if (ret) {
  581. dev_err(&pdev->dev,
  582. "failed: irq request (IRQ: %d, error :%d)\n",
  583. muic_irq->irq, ret);
  584. goto err_irq;
  585. }
  586. }
  587. /* External connector */
  588. info->edev = devm_extcon_dev_allocate(&pdev->dev, max8997_extcon_cable);
  589. if (IS_ERR(info->edev)) {
  590. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  591. ret = -ENOMEM;
  592. goto err_irq;
  593. }
  594. ret = devm_extcon_dev_register(&pdev->dev, info->edev);
  595. if (ret) {
  596. dev_err(&pdev->dev, "failed to register extcon device\n");
  597. goto err_irq;
  598. }
  599. if (pdata && pdata->muic_pdata) {
  600. struct max8997_muic_platform_data *muic_pdata
  601. = pdata->muic_pdata;
  602. /* Initialize registers according to platform data */
  603. for (i = 0; i < muic_pdata->num_init_data; i++) {
  604. max8997_write_reg(info->muic,
  605. muic_pdata->init_data[i].addr,
  606. muic_pdata->init_data[i].data);
  607. }
  608. /*
  609. * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
  610. * h/w path of COMP2/COMN1 on CONTROL1 register.
  611. */
  612. if (muic_pdata->path_uart)
  613. info->path_uart = muic_pdata->path_uart;
  614. else
  615. info->path_uart = CONTROL1_SW_UART;
  616. if (muic_pdata->path_usb)
  617. info->path_usb = muic_pdata->path_usb;
  618. else
  619. info->path_usb = CONTROL1_SW_USB;
  620. /*
  621. * Default delay time for detecting cable state
  622. * after certain time.
  623. */
  624. if (muic_pdata->detcable_delay_ms)
  625. delay_jiffies =
  626. msecs_to_jiffies(muic_pdata->detcable_delay_ms);
  627. else
  628. delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  629. } else {
  630. info->path_uart = CONTROL1_SW_UART;
  631. info->path_usb = CONTROL1_SW_USB;
  632. delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  633. }
  634. /* Set initial path for UART */
  635. max8997_muic_set_path(info, info->path_uart, true);
  636. /* Set ADC debounce time */
  637. max8997_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
  638. /*
  639. * Detect accessory after completing the initialization of platform
  640. *
  641. * - Use delayed workqueue to detect cable state and then
  642. * notify cable state to notifiee/platform through uevent.
  643. * After completing the booting of platform, the extcon provider
  644. * driver should notify cable state to upper layer.
  645. */
  646. INIT_DELAYED_WORK(&info->wq_detcable, max8997_muic_detect_cable_wq);
  647. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  648. delay_jiffies);
  649. return 0;
  650. err_irq:
  651. while (--i >= 0)
  652. free_irq(muic_irqs[i].virq, info);
  653. return ret;
  654. }
  655. static int max8997_muic_remove(struct platform_device *pdev)
  656. {
  657. struct max8997_muic_info *info = platform_get_drvdata(pdev);
  658. int i;
  659. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
  660. free_irq(muic_irqs[i].virq, info);
  661. cancel_work_sync(&info->irq_work);
  662. return 0;
  663. }
  664. static struct platform_driver max8997_muic_driver = {
  665. .driver = {
  666. .name = DEV_NAME,
  667. },
  668. .probe = max8997_muic_probe,
  669. .remove = max8997_muic_remove,
  670. };
  671. module_platform_driver(max8997_muic_driver);
  672. MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
  673. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  674. MODULE_LICENSE("GPL");