extcon-max8997.c 21 KB

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