extcon-max77843.c 24 KB

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