extcon-max77843.c 24 KB

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