extcon-max77843.c 24 KB

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