extcon-max77843.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * extcon-max77843.c - Maxim MAX77843 extcon driver to support
  3. * MUIC(Micro USB Interface Controller)
  4. *
  5. * Copyright (C) 2015 Samsung Electronics
  6. * Author: Jaewon Kim <jaewon02.kim@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. #include <linux/extcon.h>
  14. #include <linux/i2c.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/max77843-private.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/workqueue.h>
  21. #define DELAY_MS_DEFAULT 15000 /* unit: millisecond */
  22. enum max77843_muic_status {
  23. MAX77843_MUIC_STATUS1 = 0,
  24. MAX77843_MUIC_STATUS2,
  25. MAX77843_MUIC_STATUS3,
  26. MAX77843_MUIC_STATUS_NUM,
  27. };
  28. struct max77843_muic_info {
  29. struct device *dev;
  30. struct max77843 *max77843;
  31. struct extcon_dev *edev;
  32. struct mutex mutex;
  33. struct work_struct irq_work;
  34. struct delayed_work wq_detcable;
  35. u8 status[MAX77843_MUIC_STATUS_NUM];
  36. int prev_cable_type;
  37. int prev_chg_type;
  38. int prev_gnd_type;
  39. bool irq_adc;
  40. bool irq_chg;
  41. };
  42. enum max77843_muic_cable_group {
  43. MAX77843_CABLE_GROUP_ADC = 0,
  44. MAX77843_CABLE_GROUP_ADC_GND,
  45. MAX77843_CABLE_GROUP_CHG,
  46. };
  47. enum max77843_muic_adc_debounce_time {
  48. MAX77843_DEBOUNCE_TIME_5MS = 0,
  49. MAX77843_DEBOUNCE_TIME_10MS,
  50. MAX77843_DEBOUNCE_TIME_25MS,
  51. MAX77843_DEBOUNCE_TIME_38_62MS,
  52. };
  53. /* Define accessory cable type */
  54. enum max77843_muic_accessory_type {
  55. MAX77843_MUIC_ADC_GROUND = 0,
  56. MAX77843_MUIC_ADC_SEND_END_BUTTON,
  57. MAX77843_MUIC_ADC_REMOTE_S1_BUTTON,
  58. MAX77843_MUIC_ADC_REMOTE_S2_BUTTON,
  59. MAX77843_MUIC_ADC_REMOTE_S3_BUTTON,
  60. MAX77843_MUIC_ADC_REMOTE_S4_BUTTON,
  61. MAX77843_MUIC_ADC_REMOTE_S5_BUTTON,
  62. MAX77843_MUIC_ADC_REMOTE_S6_BUTTON,
  63. MAX77843_MUIC_ADC_REMOTE_S7_BUTTON,
  64. MAX77843_MUIC_ADC_REMOTE_S8_BUTTON,
  65. MAX77843_MUIC_ADC_REMOTE_S9_BUTTON,
  66. MAX77843_MUIC_ADC_REMOTE_S10_BUTTON,
  67. MAX77843_MUIC_ADC_REMOTE_S11_BUTTON,
  68. MAX77843_MUIC_ADC_REMOTE_S12_BUTTON,
  69. MAX77843_MUIC_ADC_RESERVED_ACC_1,
  70. MAX77843_MUIC_ADC_RESERVED_ACC_2,
  71. MAX77843_MUIC_ADC_RESERVED_ACC_3,
  72. MAX77843_MUIC_ADC_RESERVED_ACC_4,
  73. MAX77843_MUIC_ADC_RESERVED_ACC_5,
  74. MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2,
  75. MAX77843_MUIC_ADC_PHONE_POWERED_DEV,
  76. MAX77843_MUIC_ADC_TTY_CONVERTER,
  77. MAX77843_MUIC_ADC_UART_CABLE,
  78. MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG,
  79. MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF,
  80. MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON,
  81. MAX77843_MUIC_ADC_AV_CABLE_NOLOAD,
  82. MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG,
  83. MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF,
  84. MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON,
  85. MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1,
  86. MAX77843_MUIC_ADC_OPEN,
  87. /* The blow accessories should check
  88. not only ADC value but also ADC1K and VBVolt value. */
  89. /* Offset|ADC1K|VBVolt| */
  90. MAX77843_MUIC_GND_USB_HOST = 0x100, /* 0x1| 0| 0| */
  91. MAX77843_MUIC_GND_USB_HOST_VB = 0x101, /* 0x1| 0| 1| */
  92. MAX77843_MUIC_GND_MHL = 0x102, /* 0x1| 1| 0| */
  93. MAX77843_MUIC_GND_MHL_VB = 0x103, /* 0x1| 1| 1| */
  94. };
  95. /* Define charger cable type */
  96. enum max77843_muic_charger_type {
  97. MAX77843_MUIC_CHG_NONE = 0,
  98. MAX77843_MUIC_CHG_USB,
  99. MAX77843_MUIC_CHG_DOWNSTREAM,
  100. MAX77843_MUIC_CHG_DEDICATED,
  101. MAX77843_MUIC_CHG_SPECIAL_500MA,
  102. MAX77843_MUIC_CHG_SPECIAL_1A,
  103. MAX77843_MUIC_CHG_SPECIAL_BIAS,
  104. MAX77843_MUIC_CHG_RESERVED,
  105. MAX77843_MUIC_CHG_GND,
  106. };
  107. enum {
  108. MAX77843_CABLE_USB = 0,
  109. MAX77843_CABLE_USB_HOST,
  110. MAX77843_CABLE_TA,
  111. MAX77843_CABLE_CHARGE_DOWNSTREAM,
  112. MAX77843_CABLE_FAST_CHARGER,
  113. MAX77843_CABLE_SLOW_CHARGER,
  114. MAX77843_CABLE_MHL,
  115. MAX77843_CABLE_JIG,
  116. MAX77843_CABLE_NUM,
  117. };
  118. static const char *max77843_extcon_cable[] = {
  119. [MAX77843_CABLE_USB] = "USB",
  120. [MAX77843_CABLE_USB_HOST] = "USB-HOST",
  121. [MAX77843_CABLE_TA] = "TA",
  122. [MAX77843_CABLE_CHARGE_DOWNSTREAM] = "CHARGER-DOWNSTREAM",
  123. [MAX77843_CABLE_FAST_CHARGER] = "FAST-CHARGER",
  124. [MAX77843_CABLE_SLOW_CHARGER] = "SLOW-CHARGER",
  125. [MAX77843_CABLE_MHL] = "MHL",
  126. [MAX77843_CABLE_JIG] = "JIG",
  127. };
  128. struct max77843_muic_irq {
  129. unsigned int irq;
  130. const char *name;
  131. unsigned int virq;
  132. };
  133. static struct max77843_muic_irq max77843_muic_irqs[] = {
  134. { MAX77843_MUIC_IRQ_INT1_ADC, "MUIC-ADC" },
  135. { MAX77843_MUIC_IRQ_INT1_ADCERROR, "MUIC-ADC_ERROR" },
  136. { MAX77843_MUIC_IRQ_INT1_ADC1K, "MUIC-ADC1K" },
  137. { MAX77843_MUIC_IRQ_INT2_CHGTYP, "MUIC-CHGTYP" },
  138. { MAX77843_MUIC_IRQ_INT2_CHGDETRUN, "MUIC-CHGDETRUN" },
  139. { MAX77843_MUIC_IRQ_INT2_DCDTMR, "MUIC-DCDTMR" },
  140. { MAX77843_MUIC_IRQ_INT2_DXOVP, "MUIC-DXOVP" },
  141. { MAX77843_MUIC_IRQ_INT2_VBVOLT, "MUIC-VBVOLT" },
  142. { MAX77843_MUIC_IRQ_INT3_VBADC, "MUIC-VBADC" },
  143. { MAX77843_MUIC_IRQ_INT3_VDNMON, "MUIC-VDNMON" },
  144. { MAX77843_MUIC_IRQ_INT3_DNRES, "MUIC-DNRES" },
  145. { MAX77843_MUIC_IRQ_INT3_MPNACK, "MUIC-MPNACK"},
  146. { MAX77843_MUIC_IRQ_INT3_MRXBUFOW, "MUIC-MRXBUFOW"},
  147. { MAX77843_MUIC_IRQ_INT3_MRXTRF, "MUIC-MRXTRF"},
  148. { MAX77843_MUIC_IRQ_INT3_MRXPERR, "MUIC-MRXPERR"},
  149. { MAX77843_MUIC_IRQ_INT3_MRXRDY, "MUIC-MRXRDY"},
  150. };
  151. static const struct regmap_config max77843_muic_regmap_config = {
  152. .reg_bits = 8,
  153. .val_bits = 8,
  154. .max_register = MAX77843_MUIC_REG_END,
  155. };
  156. static const struct regmap_irq max77843_muic_irq[] = {
  157. /* INT1 interrupt */
  158. { .reg_offset = 0, .mask = MAX77843_MUIC_ADC, },
  159. { .reg_offset = 0, .mask = MAX77843_MUIC_ADCERROR, },
  160. { .reg_offset = 0, .mask = MAX77843_MUIC_ADC1K, },
  161. /* INT2 interrupt */
  162. { .reg_offset = 1, .mask = MAX77843_MUIC_CHGTYP, },
  163. { .reg_offset = 1, .mask = MAX77843_MUIC_CHGDETRUN, },
  164. { .reg_offset = 1, .mask = MAX77843_MUIC_DCDTMR, },
  165. { .reg_offset = 1, .mask = MAX77843_MUIC_DXOVP, },
  166. { .reg_offset = 1, .mask = MAX77843_MUIC_VBVOLT, },
  167. /* INT3 interrupt */
  168. { .reg_offset = 2, .mask = MAX77843_MUIC_VBADC, },
  169. { .reg_offset = 2, .mask = MAX77843_MUIC_VDNMON, },
  170. { .reg_offset = 2, .mask = MAX77843_MUIC_DNRES, },
  171. { .reg_offset = 2, .mask = MAX77843_MUIC_MPNACK, },
  172. { .reg_offset = 2, .mask = MAX77843_MUIC_MRXBUFOW, },
  173. { .reg_offset = 2, .mask = MAX77843_MUIC_MRXTRF, },
  174. { .reg_offset = 2, .mask = MAX77843_MUIC_MRXPERR, },
  175. { .reg_offset = 2, .mask = MAX77843_MUIC_MRXRDY, },
  176. };
  177. static const struct regmap_irq_chip max77843_muic_irq_chip = {
  178. .name = "max77843-muic",
  179. .status_base = MAX77843_MUIC_REG_INT1,
  180. .mask_base = MAX77843_MUIC_REG_INTMASK1,
  181. .mask_invert = true,
  182. .num_regs = 3,
  183. .irqs = max77843_muic_irq,
  184. .num_irqs = ARRAY_SIZE(max77843_muic_irq),
  185. };
  186. static int max77843_muic_set_path(struct max77843_muic_info *info,
  187. u8 val, bool attached)
  188. {
  189. struct max77843 *max77843 = info->max77843;
  190. int ret = 0;
  191. unsigned int ctrl1, ctrl2;
  192. if (attached)
  193. ctrl1 = val;
  194. else
  195. ctrl1 = CONTROL1_SW_OPEN;
  196. ret = regmap_update_bits(max77843->regmap_muic,
  197. MAX77843_MUIC_REG_CONTROL1,
  198. CONTROL1_COM_SW, ctrl1);
  199. if (ret < 0) {
  200. dev_err(info->dev, "Cannot switch MUIC port\n");
  201. return ret;
  202. }
  203. if (attached)
  204. ctrl2 = MAX77843_MUIC_CONTROL2_CPEN_MASK;
  205. else
  206. ctrl2 = MAX77843_MUIC_CONTROL2_LOWPWR_MASK;
  207. ret = regmap_update_bits(max77843->regmap_muic,
  208. MAX77843_MUIC_REG_CONTROL2,
  209. MAX77843_MUIC_CONTROL2_LOWPWR_MASK |
  210. MAX77843_MUIC_CONTROL2_CPEN_MASK, ctrl2);
  211. if (ret < 0) {
  212. dev_err(info->dev, "Cannot update lowpower mode\n");
  213. return ret;
  214. }
  215. dev_dbg(info->dev,
  216. "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
  217. ctrl1, ctrl2, attached ? "attached" : "detached");
  218. return 0;
  219. }
  220. static int max77843_muic_get_cable_type(struct max77843_muic_info *info,
  221. enum max77843_muic_cable_group group, bool *attached)
  222. {
  223. int adc, chg_type, cable_type, gnd_type;
  224. adc = info->status[MAX77843_MUIC_STATUS1] &
  225. MAX77843_MUIC_STATUS1_ADC_MASK;
  226. adc >>= STATUS1_ADC_SHIFT;
  227. switch (group) {
  228. case MAX77843_CABLE_GROUP_ADC:
  229. if (adc == MAX77843_MUIC_ADC_OPEN) {
  230. *attached = false;
  231. cable_type = info->prev_cable_type;
  232. info->prev_cable_type = MAX77843_MUIC_ADC_OPEN;
  233. } else {
  234. *attached = true;
  235. cable_type = info->prev_cable_type = adc;
  236. }
  237. break;
  238. case MAX77843_CABLE_GROUP_CHG:
  239. chg_type = info->status[MAX77843_MUIC_STATUS2] &
  240. MAX77843_MUIC_STATUS2_CHGTYP_MASK;
  241. /* Check GROUND accessory with charger cable */
  242. if (adc == MAX77843_MUIC_ADC_GROUND) {
  243. if (chg_type == MAX77843_MUIC_CHG_NONE) {
  244. /* The following state when charger cable is
  245. * disconnected but the GROUND accessory still
  246. * connected */
  247. *attached = false;
  248. cable_type = info->prev_chg_type;
  249. info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
  250. } else {
  251. /* The following state when charger cable is
  252. * connected on the GROUND accessory */
  253. *attached = true;
  254. cable_type = MAX77843_MUIC_CHG_GND;
  255. info->prev_chg_type = MAX77843_MUIC_CHG_GND;
  256. }
  257. break;
  258. }
  259. if (chg_type == MAX77843_MUIC_CHG_NONE) {
  260. *attached = false;
  261. cable_type = info->prev_chg_type;
  262. info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
  263. } else {
  264. *attached = true;
  265. cable_type = info->prev_chg_type = chg_type;
  266. }
  267. break;
  268. case MAX77843_CABLE_GROUP_ADC_GND:
  269. if (adc == MAX77843_MUIC_ADC_OPEN) {
  270. *attached = false;
  271. cable_type = info->prev_gnd_type;
  272. info->prev_gnd_type = MAX77843_MUIC_ADC_OPEN;
  273. } else {
  274. *attached = true;
  275. /* Offset|ADC1K|VBVolt|
  276. * 0x1| 0| 0| USB-HOST
  277. * 0x1| 0| 1| USB-HOST with VB
  278. * 0x1| 1| 0| MHL
  279. * 0x1| 1| 1| MHL with VB */
  280. /* Get ADC1K register bit */
  281. gnd_type = (info->status[MAX77843_MUIC_STATUS1] &
  282. MAX77843_MUIC_STATUS1_ADC1K_MASK);
  283. /* Get VBVolt register bit */
  284. gnd_type |= (info->status[MAX77843_MUIC_STATUS2] &
  285. MAX77843_MUIC_STATUS2_VBVOLT_MASK);
  286. gnd_type >>= STATUS2_VBVOLT_SHIFT;
  287. /* Offset of GND cable */
  288. gnd_type |= MAX77843_MUIC_GND_USB_HOST;
  289. cable_type = info->prev_gnd_type = gnd_type;
  290. }
  291. break;
  292. default:
  293. dev_err(info->dev, "Unknown cable group (%d)\n", group);
  294. cable_type = -EINVAL;
  295. break;
  296. }
  297. return cable_type;
  298. }
  299. static int max77843_muic_adc_gnd_handler(struct max77843_muic_info *info)
  300. {
  301. int ret, gnd_cable_type;
  302. bool attached;
  303. gnd_cable_type = max77843_muic_get_cable_type(info,
  304. MAX77843_CABLE_GROUP_ADC_GND, &attached);
  305. dev_dbg(info->dev, "external connector is %s (gnd:0x%02x)\n",
  306. attached ? "attached" : "detached", gnd_cable_type);
  307. switch (gnd_cable_type) {
  308. case MAX77843_MUIC_GND_USB_HOST:
  309. case MAX77843_MUIC_GND_USB_HOST_VB:
  310. ret = max77843_muic_set_path(info, CONTROL1_SW_USB, attached);
  311. if (ret < 0)
  312. return ret;
  313. extcon_set_cable_state(info->edev, "USB-HOST", attached);
  314. break;
  315. case MAX77843_MUIC_GND_MHL_VB:
  316. case MAX77843_MUIC_GND_MHL:
  317. ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
  318. if (ret < 0)
  319. return ret;
  320. extcon_set_cable_state(info->edev, "MHL", attached);
  321. break;
  322. default:
  323. dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n",
  324. attached ? "attached" : "detached", gnd_cable_type);
  325. return -EINVAL;
  326. }
  327. return 0;
  328. }
  329. static int max77843_muic_jig_handler(struct max77843_muic_info *info,
  330. int cable_type, bool attached)
  331. {
  332. int ret;
  333. u8 path = CONTROL1_SW_OPEN;
  334. dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n",
  335. attached ? "attached" : "detached", cable_type);
  336. switch (cable_type) {
  337. case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
  338. case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
  339. path = CONTROL1_SW_USB;
  340. break;
  341. case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
  342. path = CONTROL1_SW_UART;
  343. break;
  344. default:
  345. return -EINVAL;
  346. }
  347. ret = max77843_muic_set_path(info, path, attached);
  348. if (ret < 0)
  349. return ret;
  350. extcon_set_cable_state(info->edev, "JIG", attached);
  351. return 0;
  352. }
  353. static int max77843_muic_adc_handler(struct max77843_muic_info *info)
  354. {
  355. int ret, cable_type;
  356. bool attached;
  357. cable_type = max77843_muic_get_cable_type(info,
  358. MAX77843_CABLE_GROUP_ADC, &attached);
  359. dev_dbg(info->dev,
  360. "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
  361. attached ? "attached" : "detached", cable_type,
  362. info->prev_cable_type);
  363. switch (cable_type) {
  364. case MAX77843_MUIC_ADC_GROUND:
  365. ret = max77843_muic_adc_gnd_handler(info);
  366. if (ret < 0)
  367. return ret;
  368. break;
  369. case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
  370. case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
  371. case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
  372. ret = max77843_muic_jig_handler(info, cable_type, attached);
  373. if (ret < 0)
  374. return ret;
  375. break;
  376. case MAX77843_MUIC_ADC_SEND_END_BUTTON:
  377. case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON:
  378. case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON:
  379. case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON:
  380. case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON:
  381. case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON:
  382. case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON:
  383. case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON:
  384. case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON:
  385. case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON:
  386. case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON:
  387. case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON:
  388. case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON:
  389. case MAX77843_MUIC_ADC_RESERVED_ACC_1:
  390. case MAX77843_MUIC_ADC_RESERVED_ACC_2:
  391. case MAX77843_MUIC_ADC_RESERVED_ACC_3:
  392. case MAX77843_MUIC_ADC_RESERVED_ACC_4:
  393. case MAX77843_MUIC_ADC_RESERVED_ACC_5:
  394. case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2:
  395. case MAX77843_MUIC_ADC_PHONE_POWERED_DEV:
  396. case MAX77843_MUIC_ADC_TTY_CONVERTER:
  397. case MAX77843_MUIC_ADC_UART_CABLE:
  398. case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG:
  399. case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD:
  400. case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG:
  401. case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON:
  402. case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1:
  403. case MAX77843_MUIC_ADC_OPEN:
  404. dev_err(info->dev,
  405. "accessory is %s but it isn't used (adc:0x%x)\n",
  406. attached ? "attached" : "detached", cable_type);
  407. return -EAGAIN;
  408. default:
  409. dev_err(info->dev,
  410. "failed to detect %s accessory (adc:0x%x)\n",
  411. attached ? "attached" : "detached", cable_type);
  412. return -EINVAL;
  413. }
  414. return 0;
  415. }
  416. static int max77843_muic_chg_handler(struct max77843_muic_info *info)
  417. {
  418. int ret, chg_type, gnd_type;
  419. bool attached;
  420. chg_type = max77843_muic_get_cable_type(info,
  421. MAX77843_CABLE_GROUP_CHG, &attached);
  422. dev_dbg(info->dev,
  423. "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
  424. attached ? "attached" : "detached",
  425. chg_type, info->prev_chg_type);
  426. switch (chg_type) {
  427. case MAX77843_MUIC_CHG_USB:
  428. ret = max77843_muic_set_path(info, CONTROL1_SW_USB, attached);
  429. if (ret < 0)
  430. return ret;
  431. extcon_set_cable_state(info->edev, "USB", attached);
  432. break;
  433. case MAX77843_MUIC_CHG_DOWNSTREAM:
  434. ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
  435. if (ret < 0)
  436. return ret;
  437. extcon_set_cable_state(info->edev,
  438. "CHARGER-DOWNSTREAM", attached);
  439. break;
  440. case MAX77843_MUIC_CHG_DEDICATED:
  441. ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
  442. if (ret < 0)
  443. return ret;
  444. extcon_set_cable_state(info->edev, "TA", attached);
  445. break;
  446. case MAX77843_MUIC_CHG_SPECIAL_500MA:
  447. ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
  448. if (ret < 0)
  449. return ret;
  450. extcon_set_cable_state(info->edev, "SLOW-CHAREGER", attached);
  451. break;
  452. case MAX77843_MUIC_CHG_SPECIAL_1A:
  453. ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
  454. if (ret < 0)
  455. return ret;
  456. extcon_set_cable_state(info->edev, "FAST-CHARGER", attached);
  457. break;
  458. case MAX77843_MUIC_CHG_GND:
  459. gnd_type = max77843_muic_get_cable_type(info,
  460. MAX77843_CABLE_GROUP_ADC_GND, &attached);
  461. /* Charger cable on MHL accessory is attach or detach */
  462. if (gnd_type == MAX77843_MUIC_GND_MHL_VB)
  463. extcon_set_cable_state(info->edev, "TA", true);
  464. else if (gnd_type == MAX77843_MUIC_GND_MHL)
  465. extcon_set_cable_state(info->edev, "TA", false);
  466. break;
  467. case MAX77843_MUIC_CHG_NONE:
  468. break;
  469. default:
  470. dev_err(info->dev,
  471. "failed to detect %s accessory (chg_type:0x%x)\n",
  472. attached ? "attached" : "detached", chg_type);
  473. max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
  474. return -EINVAL;
  475. }
  476. return 0;
  477. }
  478. static void max77843_muic_irq_work(struct work_struct *work)
  479. {
  480. struct max77843_muic_info *info = container_of(work,
  481. struct max77843_muic_info, irq_work);
  482. struct max77843 *max77843 = info->max77843;
  483. int ret = 0;
  484. mutex_lock(&info->mutex);
  485. ret = regmap_bulk_read(max77843->regmap_muic,
  486. MAX77843_MUIC_REG_STATUS1, info->status,
  487. MAX77843_MUIC_STATUS_NUM);
  488. if (ret) {
  489. dev_err(info->dev, "Cannot read STATUS registers\n");
  490. mutex_unlock(&info->mutex);
  491. return;
  492. }
  493. if (info->irq_adc) {
  494. ret = max77843_muic_adc_handler(info);
  495. if (ret)
  496. dev_err(info->dev, "Unknown cable type\n");
  497. info->irq_adc = false;
  498. }
  499. if (info->irq_chg) {
  500. ret = max77843_muic_chg_handler(info);
  501. if (ret)
  502. dev_err(info->dev, "Unknown charger type\n");
  503. info->irq_chg = false;
  504. }
  505. mutex_unlock(&info->mutex);
  506. }
  507. static irqreturn_t max77843_muic_irq_handler(int irq, void *data)
  508. {
  509. struct max77843_muic_info *info = data;
  510. int i, irq_type = -1;
  511. for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++)
  512. if (irq == max77843_muic_irqs[i].virq)
  513. irq_type = max77843_muic_irqs[i].irq;
  514. switch (irq_type) {
  515. case MAX77843_MUIC_IRQ_INT1_ADC:
  516. case MAX77843_MUIC_IRQ_INT1_ADCERROR:
  517. case MAX77843_MUIC_IRQ_INT1_ADC1K:
  518. info->irq_adc = true;
  519. break;
  520. case MAX77843_MUIC_IRQ_INT2_CHGTYP:
  521. case MAX77843_MUIC_IRQ_INT2_CHGDETRUN:
  522. case MAX77843_MUIC_IRQ_INT2_DCDTMR:
  523. case MAX77843_MUIC_IRQ_INT2_DXOVP:
  524. case MAX77843_MUIC_IRQ_INT2_VBVOLT:
  525. info->irq_chg = true;
  526. break;
  527. case MAX77843_MUIC_IRQ_INT3_VBADC:
  528. case MAX77843_MUIC_IRQ_INT3_VDNMON:
  529. case MAX77843_MUIC_IRQ_INT3_DNRES:
  530. case MAX77843_MUIC_IRQ_INT3_MPNACK:
  531. case MAX77843_MUIC_IRQ_INT3_MRXBUFOW:
  532. case MAX77843_MUIC_IRQ_INT3_MRXTRF:
  533. case MAX77843_MUIC_IRQ_INT3_MRXPERR:
  534. case MAX77843_MUIC_IRQ_INT3_MRXRDY:
  535. break;
  536. default:
  537. dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type);
  538. break;
  539. }
  540. schedule_work(&info->irq_work);
  541. return IRQ_HANDLED;
  542. }
  543. static void max77843_muic_detect_cable_wq(struct work_struct *work)
  544. {
  545. struct max77843_muic_info *info = container_of(to_delayed_work(work),
  546. struct max77843_muic_info, wq_detcable);
  547. struct max77843 *max77843 = info->max77843;
  548. int chg_type, adc, ret;
  549. bool attached;
  550. mutex_lock(&info->mutex);
  551. ret = regmap_bulk_read(max77843->regmap_muic,
  552. MAX77843_MUIC_REG_STATUS1, info->status,
  553. MAX77843_MUIC_STATUS_NUM);
  554. if (ret) {
  555. dev_err(info->dev, "Cannot read STATUS registers\n");
  556. goto err_cable_wq;
  557. }
  558. adc = max77843_muic_get_cable_type(info,
  559. MAX77843_CABLE_GROUP_ADC, &attached);
  560. if (attached && adc != MAX77843_MUIC_ADC_OPEN) {
  561. ret = max77843_muic_adc_handler(info);
  562. if (ret < 0) {
  563. dev_err(info->dev, "Cannot detect accessory\n");
  564. goto err_cable_wq;
  565. }
  566. }
  567. chg_type = max77843_muic_get_cable_type(info,
  568. MAX77843_CABLE_GROUP_CHG, &attached);
  569. if (attached && chg_type != MAX77843_MUIC_CHG_NONE) {
  570. ret = max77843_muic_chg_handler(info);
  571. if (ret < 0) {
  572. dev_err(info->dev, "Cannot detect charger accessory\n");
  573. goto err_cable_wq;
  574. }
  575. }
  576. err_cable_wq:
  577. mutex_unlock(&info->mutex);
  578. }
  579. static int max77843_muic_set_debounce_time(struct max77843_muic_info *info,
  580. enum max77843_muic_adc_debounce_time time)
  581. {
  582. struct max77843 *max77843 = info->max77843;
  583. int ret;
  584. switch (time) {
  585. case MAX77843_DEBOUNCE_TIME_5MS:
  586. case MAX77843_DEBOUNCE_TIME_10MS:
  587. case MAX77843_DEBOUNCE_TIME_25MS:
  588. case MAX77843_DEBOUNCE_TIME_38_62MS:
  589. ret = regmap_update_bits(max77843->regmap_muic,
  590. MAX77843_MUIC_REG_CONTROL4,
  591. MAX77843_MUIC_CONTROL4_ADCDBSET_MASK,
  592. time << CONTROL4_ADCDBSET_SHIFT);
  593. if (ret < 0) {
  594. dev_err(info->dev, "Cannot write MUIC regmap\n");
  595. return ret;
  596. }
  597. break;
  598. default:
  599. dev_err(info->dev, "Invalid ADC debounce time\n");
  600. return -EINVAL;
  601. }
  602. return 0;
  603. }
  604. static int max77843_init_muic_regmap(struct max77843 *max77843)
  605. {
  606. int ret;
  607. max77843->i2c_muic = i2c_new_dummy(max77843->i2c->adapter,
  608. I2C_ADDR_MUIC);
  609. if (!max77843->i2c_muic) {
  610. dev_err(&max77843->i2c->dev,
  611. "Cannot allocate I2C device for MUIC\n");
  612. return -ENOMEM;
  613. }
  614. i2c_set_clientdata(max77843->i2c_muic, max77843);
  615. max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic,
  616. &max77843_muic_regmap_config);
  617. if (IS_ERR(max77843->regmap_muic)) {
  618. ret = PTR_ERR(max77843->regmap_muic);
  619. goto err_muic_i2c;
  620. }
  621. ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq,
  622. IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
  623. 0, &max77843_muic_irq_chip, &max77843->irq_data_muic);
  624. if (ret < 0) {
  625. dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n");
  626. goto err_muic_i2c;
  627. }
  628. return 0;
  629. err_muic_i2c:
  630. i2c_unregister_device(max77843->i2c_muic);
  631. return ret;
  632. }
  633. static int max77843_muic_probe(struct platform_device *pdev)
  634. {
  635. struct max77843 *max77843 = dev_get_drvdata(pdev->dev.parent);
  636. struct max77843_muic_info *info;
  637. unsigned int id;
  638. int i, ret;
  639. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  640. if (!info)
  641. return -ENOMEM;
  642. info->dev = &pdev->dev;
  643. info->max77843 = max77843;
  644. platform_set_drvdata(pdev, info);
  645. mutex_init(&info->mutex);
  646. /* Initialize i2c and regmap */
  647. ret = max77843_init_muic_regmap(max77843);
  648. if (ret) {
  649. dev_err(&pdev->dev, "Failed to init MUIC regmap\n");
  650. return ret;
  651. }
  652. /* Turn off auto detection configuration */
  653. ret = regmap_update_bits(max77843->regmap_muic,
  654. MAX77843_MUIC_REG_CONTROL4,
  655. MAX77843_MUIC_CONTROL4_USBAUTO_MASK |
  656. MAX77843_MUIC_CONTROL4_FCTAUTO_MASK,
  657. CONTROL4_AUTO_DISABLE);
  658. /* Initialize extcon device */
  659. info->edev = devm_extcon_dev_allocate(&pdev->dev,
  660. max77843_extcon_cable);
  661. if (IS_ERR(info->edev)) {
  662. dev_err(&pdev->dev, "Failed to allocate memory for extcon\n");
  663. ret = -ENODEV;
  664. goto err_muic_irq;
  665. }
  666. ret = devm_extcon_dev_register(&pdev->dev, info->edev);
  667. if (ret) {
  668. dev_err(&pdev->dev, "Failed to register extcon device\n");
  669. goto err_muic_irq;
  670. }
  671. /* Set ADC debounce time */
  672. max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS);
  673. /* Set initial path for UART */
  674. max77843_muic_set_path(info, CONTROL1_SW_UART, true);
  675. /* Check revision number of MUIC device */
  676. ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id);
  677. if (ret < 0) {
  678. dev_err(&pdev->dev, "Failed to read revision number\n");
  679. goto err_muic_irq;
  680. }
  681. dev_info(info->dev, "MUIC device ID : 0x%x\n", id);
  682. /* Support virtual irq domain for max77843 MUIC device */
  683. INIT_WORK(&info->irq_work, max77843_muic_irq_work);
  684. for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) {
  685. struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i];
  686. unsigned int virq = 0;
  687. virq = regmap_irq_get_virq(max77843->irq_data_muic,
  688. muic_irq->irq);
  689. if (virq <= 0) {
  690. ret = -EINVAL;
  691. goto err_muic_irq;
  692. }
  693. muic_irq->virq = virq;
  694. ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
  695. max77843_muic_irq_handler, IRQF_NO_SUSPEND,
  696. muic_irq->name, info);
  697. if (ret) {
  698. dev_err(&pdev->dev,
  699. "Failed to request irq (IRQ: %d, error: %d)\n",
  700. muic_irq->irq, ret);
  701. goto err_muic_irq;
  702. }
  703. }
  704. /* Detect accessory after completing the initialization of platform */
  705. INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq);
  706. queue_delayed_work(system_power_efficient_wq,
  707. &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT));
  708. return 0;
  709. err_muic_irq:
  710. regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
  711. i2c_unregister_device(max77843->i2c_muic);
  712. return ret;
  713. }
  714. static int max77843_muic_remove(struct platform_device *pdev)
  715. {
  716. struct max77843_muic_info *info = platform_get_drvdata(pdev);
  717. struct max77843 *max77843 = info->max77843;
  718. cancel_work_sync(&info->irq_work);
  719. regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
  720. i2c_unregister_device(max77843->i2c_muic);
  721. return 0;
  722. }
  723. static const struct platform_device_id max77843_muic_id[] = {
  724. { "max77843-muic", },
  725. { /* sentinel */ },
  726. };
  727. MODULE_DEVICE_TABLE(platform, max77843_muic_id);
  728. static struct platform_driver max77843_muic_driver = {
  729. .driver = {
  730. .name = "max77843-muic",
  731. },
  732. .probe = max77843_muic_probe,
  733. .remove = max77843_muic_remove,
  734. .id_table = max77843_muic_id,
  735. };
  736. static int __init max77843_muic_init(void)
  737. {
  738. return platform_driver_register(&max77843_muic_driver);
  739. }
  740. subsys_initcall(max77843_muic_init);
  741. MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
  742. MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
  743. MODULE_LICENSE("GPL");