extcon-max77843.c 24 KB

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