extcon-max77843.c 24 KB

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