extcon-max77843.c 26 KB

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