extcon-max77693.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /*
  2. * extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC
  3. *
  4. * Copyright (C) 2012 Samsung Electrnoics
  5. * Chanwoo Choi <cw00.choi@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #include <linux/input.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/err.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/mfd/max77693.h>
  26. #include <linux/mfd/max77693-private.h>
  27. #include <linux/extcon.h>
  28. #include <linux/regmap.h>
  29. #include <linux/irqdomain.h>
  30. #define DEV_NAME "max77693-muic"
  31. #define DELAY_MS_DEFAULT 20000 /* unit: millisecond */
  32. /*
  33. * Default value of MAX77693 register to bring up MUIC device.
  34. * If user don't set some initial value for MUIC device through platform data,
  35. * extcon-max77693 driver use 'default_init_data' to bring up base operation
  36. * of MAX77693 MUIC device.
  37. */
  38. static struct max77693_reg_data default_init_data[] = {
  39. {
  40. /* STATUS2 - [3]ChgDetRun */
  41. .addr = MAX77693_MUIC_REG_STATUS2,
  42. .data = STATUS2_CHGDETRUN_MASK,
  43. }, {
  44. /* INTMASK1 - Unmask [3]ADC1KM,[0]ADCM */
  45. .addr = MAX77693_MUIC_REG_INTMASK1,
  46. .data = INTMASK1_ADC1K_MASK
  47. | INTMASK1_ADC_MASK,
  48. }, {
  49. /* INTMASK2 - Unmask [0]ChgTypM */
  50. .addr = MAX77693_MUIC_REG_INTMASK2,
  51. .data = INTMASK2_CHGTYP_MASK,
  52. }, {
  53. /* INTMASK3 - Mask all of interrupts */
  54. .addr = MAX77693_MUIC_REG_INTMASK3,
  55. .data = 0x0,
  56. }, {
  57. /* CDETCTRL2 */
  58. .addr = MAX77693_MUIC_REG_CDETCTRL2,
  59. .data = CDETCTRL2_VIDRMEN_MASK
  60. | CDETCTRL2_DXOVPEN_MASK,
  61. },
  62. };
  63. enum max77693_muic_adc_debounce_time {
  64. ADC_DEBOUNCE_TIME_5MS = 0,
  65. ADC_DEBOUNCE_TIME_10MS,
  66. ADC_DEBOUNCE_TIME_25MS,
  67. ADC_DEBOUNCE_TIME_38_62MS,
  68. };
  69. struct max77693_muic_info {
  70. struct device *dev;
  71. struct max77693_dev *max77693;
  72. struct extcon_dev *edev;
  73. int prev_cable_type;
  74. int prev_cable_type_gnd;
  75. int prev_chg_type;
  76. int prev_button_type;
  77. u8 status[2];
  78. int irq;
  79. struct work_struct irq_work;
  80. struct mutex mutex;
  81. /*
  82. * Use delayed workqueue to detect cable state and then
  83. * notify cable state to notifiee/platform through uevent.
  84. * After completing the booting of platform, the extcon provider
  85. * driver should notify cable state to upper layer.
  86. */
  87. struct delayed_work wq_detcable;
  88. /* Button of dock device */
  89. struct input_dev *dock;
  90. /*
  91. * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
  92. * h/w path of COMP2/COMN1 on CONTROL1 register.
  93. */
  94. int path_usb;
  95. int path_uart;
  96. };
  97. enum max77693_muic_cable_group {
  98. MAX77693_CABLE_GROUP_ADC = 0,
  99. MAX77693_CABLE_GROUP_ADC_GND,
  100. MAX77693_CABLE_GROUP_CHG,
  101. MAX77693_CABLE_GROUP_VBVOLT,
  102. };
  103. enum max77693_muic_charger_type {
  104. MAX77693_CHARGER_TYPE_NONE = 0,
  105. MAX77693_CHARGER_TYPE_USB,
  106. MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,
  107. MAX77693_CHARGER_TYPE_DEDICATED_CHG,
  108. MAX77693_CHARGER_TYPE_APPLE_500MA,
  109. MAX77693_CHARGER_TYPE_APPLE_1A_2A,
  110. MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,
  111. };
  112. /**
  113. * struct max77693_muic_irq
  114. * @irq: the index of irq list of MUIC device.
  115. * @name: the name of irq.
  116. * @virq: the virtual irq to use irq domain
  117. */
  118. struct max77693_muic_irq {
  119. unsigned int irq;
  120. const char *name;
  121. unsigned int virq;
  122. };
  123. static struct max77693_muic_irq muic_irqs[] = {
  124. { MAX77693_MUIC_IRQ_INT1_ADC, "muic-ADC" },
  125. { MAX77693_MUIC_IRQ_INT1_ADC_LOW, "muic-ADCLOW" },
  126. { MAX77693_MUIC_IRQ_INT1_ADC_ERR, "muic-ADCError" },
  127. { MAX77693_MUIC_IRQ_INT1_ADC1K, "muic-ADC1K" },
  128. { MAX77693_MUIC_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
  129. { MAX77693_MUIC_IRQ_INT2_CHGDETREUN, "muic-CHGDETREUN" },
  130. { MAX77693_MUIC_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
  131. { MAX77693_MUIC_IRQ_INT2_DXOVP, "muic-DXOVP" },
  132. { MAX77693_MUIC_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
  133. { MAX77693_MUIC_IRQ_INT2_VIDRM, "muic-VIDRM" },
  134. { MAX77693_MUIC_IRQ_INT3_EOC, "muic-EOC" },
  135. { MAX77693_MUIC_IRQ_INT3_CGMBC, "muic-CGMBC" },
  136. { MAX77693_MUIC_IRQ_INT3_OVP, "muic-OVP" },
  137. { MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR, "muic-MBCCHG_ERR" },
  138. { MAX77693_MUIC_IRQ_INT3_CHG_ENABLED, "muic-CHG_ENABLED" },
  139. { MAX77693_MUIC_IRQ_INT3_BAT_DET, "muic-BAT_DET" },
  140. };
  141. /* Define supported accessory type */
  142. enum max77693_muic_acc_type {
  143. MAX77693_MUIC_ADC_GROUND = 0x0,
  144. MAX77693_MUIC_ADC_SEND_END_BUTTON,
  145. MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,
  146. MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,
  147. MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,
  148. MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,
  149. MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,
  150. MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,
  151. MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,
  152. MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,
  153. MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,
  154. MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,
  155. MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,
  156. MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,
  157. MAX77693_MUIC_ADC_RESERVED_ACC_1,
  158. MAX77693_MUIC_ADC_RESERVED_ACC_2,
  159. MAX77693_MUIC_ADC_RESERVED_ACC_3,
  160. MAX77693_MUIC_ADC_RESERVED_ACC_4,
  161. MAX77693_MUIC_ADC_RESERVED_ACC_5,
  162. MAX77693_MUIC_ADC_CEA936_AUDIO,
  163. MAX77693_MUIC_ADC_PHONE_POWERED_DEV,
  164. MAX77693_MUIC_ADC_TTY_CONVERTER,
  165. MAX77693_MUIC_ADC_UART_CABLE,
  166. MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,
  167. MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,
  168. MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,
  169. MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,
  170. MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,
  171. MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,
  172. MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,
  173. MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,
  174. MAX77693_MUIC_ADC_OPEN,
  175. /* The below accessories have same ADC value so ADCLow and
  176. ADC1K bit is used to separate specific accessory */
  177. /* ADC|VBVolot|ADCLow|ADC1K| */
  178. MAX77693_MUIC_GND_USB_OTG = 0x100, /* 0x0| 0| 0| 0| */
  179. MAX77693_MUIC_GND_USB_OTG_VB = 0x104, /* 0x0| 1| 0| 0| */
  180. MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* 0x0| 0| 1| 0| */
  181. MAX77693_MUIC_GND_MHL = 0x103, /* 0x0| 0| 1| 1| */
  182. MAX77693_MUIC_GND_MHL_VB = 0x107, /* 0x0| 1| 1| 1| */
  183. };
  184. /*
  185. * MAX77693 MUIC device support below list of accessories(external connector)
  186. */
  187. enum {
  188. EXTCON_CABLE_USB = 0,
  189. EXTCON_CABLE_USB_HOST,
  190. EXTCON_CABLE_TA,
  191. EXTCON_CABLE_FAST_CHARGER,
  192. EXTCON_CABLE_SLOW_CHARGER,
  193. EXTCON_CABLE_CHARGE_DOWNSTREAM,
  194. EXTCON_CABLE_MHL,
  195. EXTCON_CABLE_MHL_TA,
  196. EXTCON_CABLE_JIG_USB_ON,
  197. EXTCON_CABLE_JIG_USB_OFF,
  198. EXTCON_CABLE_JIG_UART_OFF,
  199. EXTCON_CABLE_JIG_UART_ON,
  200. EXTCON_CABLE_DOCK_SMART,
  201. EXTCON_CABLE_DOCK_DESK,
  202. EXTCON_CABLE_DOCK_AUDIO,
  203. _EXTCON_CABLE_NUM,
  204. };
  205. static const char *max77693_extcon_cable[] = {
  206. [EXTCON_CABLE_USB] = "USB",
  207. [EXTCON_CABLE_USB_HOST] = "USB-Host",
  208. [EXTCON_CABLE_TA] = "TA",
  209. [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
  210. [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
  211. [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
  212. [EXTCON_CABLE_MHL] = "MHL",
  213. [EXTCON_CABLE_MHL_TA] = "MHL_TA",
  214. [EXTCON_CABLE_JIG_USB_ON] = "JIG-USB-ON",
  215. [EXTCON_CABLE_JIG_USB_OFF] = "JIG-USB-OFF",
  216. [EXTCON_CABLE_JIG_UART_OFF] = "JIG-UART-OFF",
  217. [EXTCON_CABLE_JIG_UART_ON] = "Dock-Car",
  218. [EXTCON_CABLE_DOCK_SMART] = "Dock-Smart",
  219. [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk",
  220. [EXTCON_CABLE_DOCK_AUDIO] = "Dock-Audio",
  221. NULL,
  222. };
  223. /*
  224. * max77693_muic_set_debounce_time - Set the debounce time of ADC
  225. * @info: the instance including private data of max77693 MUIC
  226. * @time: the debounce time of ADC
  227. */
  228. static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
  229. enum max77693_muic_adc_debounce_time time)
  230. {
  231. int ret;
  232. switch (time) {
  233. case ADC_DEBOUNCE_TIME_5MS:
  234. case ADC_DEBOUNCE_TIME_10MS:
  235. case ADC_DEBOUNCE_TIME_25MS:
  236. case ADC_DEBOUNCE_TIME_38_62MS:
  237. /*
  238. * Don't touch BTLDset, JIGset when you want to change adc
  239. * debounce time. If it writes other than 0 to BTLDset, JIGset
  240. * muic device will be reset and loose current state.
  241. */
  242. ret = regmap_write(info->max77693->regmap_muic,
  243. MAX77693_MUIC_REG_CTRL3,
  244. time << CONTROL3_ADCDBSET_SHIFT);
  245. if (ret) {
  246. dev_err(info->dev, "failed to set ADC debounce time\n");
  247. return ret;
  248. }
  249. break;
  250. default:
  251. dev_err(info->dev, "invalid ADC debounce time\n");
  252. return -EINVAL;
  253. }
  254. return 0;
  255. };
  256. /*
  257. * max77693_muic_set_path - Set hardware line according to attached cable
  258. * @info: the instance including private data of max77693 MUIC
  259. * @value: the path according to attached cable
  260. * @attached: the state of cable (true:attached, false:detached)
  261. *
  262. * The max77693 MUIC device share outside H/W line among a varity of cables
  263. * so, this function set internal path of H/W line according to the type of
  264. * attached cable.
  265. */
  266. static int max77693_muic_set_path(struct max77693_muic_info *info,
  267. u8 val, bool attached)
  268. {
  269. int ret = 0;
  270. unsigned int ctrl1, ctrl2 = 0;
  271. if (attached)
  272. ctrl1 = val;
  273. else
  274. ctrl1 = CONTROL1_SW_OPEN;
  275. ret = regmap_update_bits(info->max77693->regmap_muic,
  276. MAX77693_MUIC_REG_CTRL1, COMP_SW_MASK, ctrl1);
  277. if (ret < 0) {
  278. dev_err(info->dev, "failed to update MUIC register\n");
  279. return ret;
  280. }
  281. if (attached)
  282. ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
  283. else
  284. ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
  285. ret = regmap_update_bits(info->max77693->regmap_muic,
  286. MAX77693_MUIC_REG_CTRL2,
  287. CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK, ctrl2);
  288. if (ret < 0) {
  289. dev_err(info->dev, "failed to update MUIC register\n");
  290. return ret;
  291. }
  292. dev_info(info->dev,
  293. "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
  294. ctrl1, ctrl2, attached ? "attached" : "detached");
  295. return 0;
  296. }
  297. /*
  298. * max77693_muic_get_cable_type - Return cable type and check cable state
  299. * @info: the instance including private data of max77693 MUIC
  300. * @group: the path according to attached cable
  301. * @attached: store cable state and return
  302. *
  303. * This function check the cable state either attached or detached,
  304. * and then divide precise type of cable according to cable group.
  305. * - MAX77693_CABLE_GROUP_ADC
  306. * - MAX77693_CABLE_GROUP_ADC_GND
  307. * - MAX77693_CABLE_GROUP_CHG
  308. * - MAX77693_CABLE_GROUP_VBVOLT
  309. */
  310. static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
  311. enum max77693_muic_cable_group group, bool *attached)
  312. {
  313. int cable_type = 0;
  314. int adc;
  315. int adc1k;
  316. int adclow;
  317. int vbvolt;
  318. int chg_type;
  319. switch (group) {
  320. case MAX77693_CABLE_GROUP_ADC:
  321. /*
  322. * Read ADC value to check cable type and decide cable state
  323. * according to cable type
  324. */
  325. adc = info->status[0] & STATUS1_ADC_MASK;
  326. adc >>= STATUS1_ADC_SHIFT;
  327. /*
  328. * Check current cable state/cable type and store cable type
  329. * (info->prev_cable_type) for handling cable when cable is
  330. * detached.
  331. */
  332. if (adc == MAX77693_MUIC_ADC_OPEN) {
  333. *attached = false;
  334. cable_type = info->prev_cable_type;
  335. info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
  336. } else {
  337. *attached = true;
  338. cable_type = info->prev_cable_type = adc;
  339. }
  340. break;
  341. case MAX77693_CABLE_GROUP_ADC_GND:
  342. /*
  343. * Read ADC value to check cable type and decide cable state
  344. * according to cable type
  345. */
  346. adc = info->status[0] & STATUS1_ADC_MASK;
  347. adc >>= STATUS1_ADC_SHIFT;
  348. /*
  349. * Check current cable state/cable type and store cable type
  350. * (info->prev_cable_type/_gnd) for handling cable when cable
  351. * is detached.
  352. */
  353. if (adc == MAX77693_MUIC_ADC_OPEN) {
  354. *attached = false;
  355. cable_type = info->prev_cable_type_gnd;
  356. info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
  357. } else {
  358. *attached = true;
  359. adclow = info->status[0] & STATUS1_ADCLOW_MASK;
  360. adclow >>= STATUS1_ADCLOW_SHIFT;
  361. adc1k = info->status[0] & STATUS1_ADC1K_MASK;
  362. adc1k >>= STATUS1_ADC1K_SHIFT;
  363. vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
  364. vbvolt >>= STATUS2_VBVOLT_SHIFT;
  365. /**
  366. * [0x1|VBVolt|ADCLow|ADC1K]
  367. * [0x1| 0| 0| 0] USB_OTG
  368. * [0x1| 1| 0| 0] USB_OTG_VB
  369. * [0x1| 0| 1| 0] Audio Video cable with load
  370. * [0x1| 0| 1| 1] MHL without charging cable
  371. * [0x1| 1| 1| 1] MHL with charging cable
  372. */
  373. cable_type = ((0x1 << 8)
  374. | (vbvolt << 2)
  375. | (adclow << 1)
  376. | adc1k);
  377. info->prev_cable_type = adc;
  378. info->prev_cable_type_gnd = cable_type;
  379. }
  380. break;
  381. case MAX77693_CABLE_GROUP_CHG:
  382. /*
  383. * Read charger type to check cable type and decide cable state
  384. * according to type of charger cable.
  385. */
  386. chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
  387. chg_type >>= STATUS2_CHGTYP_SHIFT;
  388. if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
  389. *attached = false;
  390. cable_type = info->prev_chg_type;
  391. info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
  392. } else {
  393. *attached = true;
  394. /*
  395. * Check current cable state/cable type and store cable
  396. * type(info->prev_chg_type) for handling cable when
  397. * charger cable is detached.
  398. */
  399. cable_type = info->prev_chg_type = chg_type;
  400. }
  401. break;
  402. case MAX77693_CABLE_GROUP_VBVOLT:
  403. /*
  404. * Read ADC value to check cable type and decide cable state
  405. * according to cable type
  406. */
  407. adc = info->status[0] & STATUS1_ADC_MASK;
  408. adc >>= STATUS1_ADC_SHIFT;
  409. chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
  410. chg_type >>= STATUS2_CHGTYP_SHIFT;
  411. if (adc == MAX77693_MUIC_ADC_OPEN
  412. && chg_type == MAX77693_CHARGER_TYPE_NONE)
  413. *attached = false;
  414. else
  415. *attached = true;
  416. /*
  417. * Read vbvolt field, if vbvolt is 1,
  418. * this cable is used for charging.
  419. */
  420. vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
  421. vbvolt >>= STATUS2_VBVOLT_SHIFT;
  422. cable_type = vbvolt;
  423. break;
  424. default:
  425. dev_err(info->dev, "Unknown cable group (%d)\n", group);
  426. cable_type = -EINVAL;
  427. break;
  428. }
  429. return cable_type;
  430. }
  431. static int max77693_muic_dock_handler(struct max77693_muic_info *info,
  432. int cable_type, bool attached)
  433. {
  434. int ret = 0;
  435. int vbvolt;
  436. bool cable_attached;
  437. char dock_name[CABLE_NAME_MAX];
  438. dev_info(info->dev,
  439. "external connector is %s (adc:0x%02x)\n",
  440. attached ? "attached" : "detached", cable_type);
  441. switch (cable_type) {
  442. case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
  443. /*
  444. * Check power cable whether attached or detached state.
  445. * The Dock-Smart device need surely external power supply.
  446. * If power cable(USB/TA) isn't connected to Dock device,
  447. * user can't use Dock-Smart for desktop mode.
  448. */
  449. vbvolt = max77693_muic_get_cable_type(info,
  450. MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);
  451. if (attached && !vbvolt) {
  452. dev_warn(info->dev,
  453. "Cannot detect external power supply\n");
  454. return 0;
  455. }
  456. /*
  457. * Notify Dock-Smart/MHL state.
  458. * - Dock-Smart device include three type of cable which
  459. * are HDMI, USB for mouse/keyboard and micro-usb port
  460. * for USB/TA cable. Dock-Smart device need always exteranl
  461. * power supply(USB/TA cable through micro-usb cable). Dock-
  462. * Smart device support screen output of target to separate
  463. * monitor and mouse/keyboard for desktop mode.
  464. *
  465. * Features of 'USB/TA cable with Dock-Smart device'
  466. * - Support MHL
  467. * - Support external output feature of audio
  468. * - Support charging through micro-usb port without data
  469. * connection if TA cable is connected to target.
  470. * - Support charging and data connection through micro-usb port
  471. * if USB cable is connected between target and host
  472. * device.
  473. * - Support OTG device (Mouse/Keyboard)
  474. */
  475. ret = max77693_muic_set_path(info, info->path_usb, attached);
  476. if (ret < 0)
  477. return ret;
  478. extcon_set_cable_state(info->edev, "Dock-Smart", attached);
  479. extcon_set_cable_state(info->edev, "MHL", attached);
  480. goto out;
  481. case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
  482. strcpy(dock_name, "Dock-Car");
  483. break;
  484. case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
  485. strcpy(dock_name, "Dock-Desk");
  486. break;
  487. case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
  488. strcpy(dock_name, "Dock-Audio");
  489. if (!attached)
  490. extcon_set_cable_state(info->edev, "USB", false);
  491. break;
  492. default:
  493. dev_err(info->dev, "failed to detect %s dock device\n",
  494. attached ? "attached" : "detached");
  495. return -EINVAL;
  496. }
  497. /* Dock-Car/Desk/Audio, PATH:AUDIO */
  498. ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
  499. if (ret < 0)
  500. return ret;
  501. extcon_set_cable_state(info->edev, dock_name, attached);
  502. out:
  503. return 0;
  504. }
  505. static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
  506. int button_type, bool attached)
  507. {
  508. struct input_dev *dock = info->dock;
  509. unsigned int code;
  510. switch (button_type) {
  511. case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
  512. ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
  513. /* DOCK_KEY_PREV */
  514. code = KEY_PREVIOUSSONG;
  515. break;
  516. case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
  517. ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
  518. /* DOCK_KEY_NEXT */
  519. code = KEY_NEXTSONG;
  520. break;
  521. case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
  522. /* DOCK_VOL_DOWN */
  523. code = KEY_VOLUMEDOWN;
  524. break;
  525. case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
  526. /* DOCK_VOL_UP */
  527. code = KEY_VOLUMEUP;
  528. break;
  529. case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
  530. ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
  531. /* DOCK_KEY_PLAY_PAUSE */
  532. code = KEY_PLAYPAUSE;
  533. break;
  534. default:
  535. dev_err(info->dev,
  536. "failed to detect %s key (adc:0x%x)\n",
  537. attached ? "pressed" : "released", button_type);
  538. return -EINVAL;
  539. }
  540. input_event(dock, EV_KEY, code, attached);
  541. input_sync(dock);
  542. return 0;
  543. }
  544. static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
  545. {
  546. int cable_type_gnd;
  547. int ret = 0;
  548. bool attached;
  549. cable_type_gnd = max77693_muic_get_cable_type(info,
  550. MAX77693_CABLE_GROUP_ADC_GND, &attached);
  551. switch (cable_type_gnd) {
  552. case MAX77693_MUIC_GND_USB_OTG:
  553. case MAX77693_MUIC_GND_USB_OTG_VB:
  554. /* USB_OTG, PATH: AP_USB */
  555. ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
  556. if (ret < 0)
  557. return ret;
  558. extcon_set_cable_state(info->edev, "USB-Host", attached);
  559. break;
  560. case MAX77693_MUIC_GND_AV_CABLE_LOAD:
  561. /* Audio Video Cable with load, PATH:AUDIO */
  562. ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
  563. if (ret < 0)
  564. return ret;
  565. extcon_set_cable_state(info->edev,
  566. "Audio-video-load", attached);
  567. break;
  568. case MAX77693_MUIC_GND_MHL:
  569. case MAX77693_MUIC_GND_MHL_VB:
  570. /* MHL or MHL with USB/TA cable */
  571. extcon_set_cable_state(info->edev, "MHL", attached);
  572. break;
  573. default:
  574. dev_err(info->dev, "failed to detect %s cable of gnd type\n",
  575. attached ? "attached" : "detached");
  576. return -EINVAL;
  577. }
  578. return 0;
  579. }
  580. static int max77693_muic_jig_handler(struct max77693_muic_info *info,
  581. int cable_type, bool attached)
  582. {
  583. char cable_name[32];
  584. int ret = 0;
  585. u8 path = CONTROL1_SW_OPEN;
  586. dev_info(info->dev,
  587. "external connector is %s (adc:0x%02x)\n",
  588. attached ? "attached" : "detached", cable_type);
  589. switch (cable_type) {
  590. case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
  591. /* PATH:AP_USB */
  592. strcpy(cable_name, "JIG-USB-OFF");
  593. path = CONTROL1_SW_USB;
  594. break;
  595. case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
  596. /* PATH:AP_USB */
  597. strcpy(cable_name, "JIG-USB-ON");
  598. path = CONTROL1_SW_USB;
  599. break;
  600. case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
  601. /* PATH:AP_UART */
  602. strcpy(cable_name, "JIG-UART-OFF");
  603. path = CONTROL1_SW_UART;
  604. break;
  605. default:
  606. dev_err(info->dev, "failed to detect %s jig cable\n",
  607. attached ? "attached" : "detached");
  608. return -EINVAL;
  609. }
  610. ret = max77693_muic_set_path(info, path, attached);
  611. if (ret < 0)
  612. return ret;
  613. extcon_set_cable_state(info->edev, cable_name, attached);
  614. return 0;
  615. }
  616. static int max77693_muic_adc_handler(struct max77693_muic_info *info)
  617. {
  618. int cable_type;
  619. int button_type;
  620. bool attached;
  621. int ret = 0;
  622. /* Check accessory state which is either detached or attached */
  623. cable_type = max77693_muic_get_cable_type(info,
  624. MAX77693_CABLE_GROUP_ADC, &attached);
  625. dev_info(info->dev,
  626. "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
  627. attached ? "attached" : "detached", cable_type,
  628. info->prev_cable_type);
  629. switch (cable_type) {
  630. case MAX77693_MUIC_ADC_GROUND:
  631. /* USB_OTG/MHL/Audio */
  632. max77693_muic_adc_ground_handler(info);
  633. break;
  634. case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
  635. case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
  636. case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
  637. /* JIG */
  638. ret = max77693_muic_jig_handler(info, cable_type, attached);
  639. if (ret < 0)
  640. return ret;
  641. break;
  642. case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
  643. case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
  644. case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
  645. case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
  646. /*
  647. * DOCK device
  648. *
  649. * The MAX77693 MUIC device can detect total 34 cable type
  650. * except of charger cable and MUIC device didn't define
  651. * specfic role of cable in the range of from 0x01 to 0x12
  652. * of ADC value. So, can use/define cable with no role according
  653. * to schema of hardware board.
  654. */
  655. ret = max77693_muic_dock_handler(info, cable_type, attached);
  656. if (ret < 0)
  657. return ret;
  658. break;
  659. case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON: /* DOCK_KEY_PREV */
  660. case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON: /* DOCK_KEY_NEXT */
  661. case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: /* DOCK_VOL_DOWN */
  662. case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: /* DOCK_VOL_UP */
  663. case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON: /* DOCK_KEY_PLAY_PAUSE */
  664. /*
  665. * Button of DOCK device
  666. * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
  667. *
  668. * The MAX77693 MUIC device can detect total 34 cable type
  669. * except of charger cable and MUIC device didn't define
  670. * specfic role of cable in the range of from 0x01 to 0x12
  671. * of ADC value. So, can use/define cable with no role according
  672. * to schema of hardware board.
  673. */
  674. if (attached)
  675. button_type = info->prev_button_type = cable_type;
  676. else
  677. button_type = info->prev_button_type;
  678. ret = max77693_muic_dock_button_handler(info, button_type,
  679. attached);
  680. if (ret < 0)
  681. return ret;
  682. break;
  683. case MAX77693_MUIC_ADC_SEND_END_BUTTON:
  684. case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
  685. case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
  686. case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
  687. case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
  688. case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
  689. case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
  690. case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
  691. case MAX77693_MUIC_ADC_RESERVED_ACC_1:
  692. case MAX77693_MUIC_ADC_RESERVED_ACC_2:
  693. case MAX77693_MUIC_ADC_RESERVED_ACC_4:
  694. case MAX77693_MUIC_ADC_RESERVED_ACC_5:
  695. case MAX77693_MUIC_ADC_CEA936_AUDIO:
  696. case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
  697. case MAX77693_MUIC_ADC_TTY_CONVERTER:
  698. case MAX77693_MUIC_ADC_UART_CABLE:
  699. case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
  700. case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
  701. /*
  702. * This accessory isn't used in general case if it is specially
  703. * needed to detect additional accessory, should implement
  704. * proper operation when this accessory is attached/detached.
  705. */
  706. dev_info(info->dev,
  707. "accessory is %s but it isn't used (adc:0x%x)\n",
  708. attached ? "attached" : "detached", cable_type);
  709. return -EAGAIN;
  710. default:
  711. dev_err(info->dev,
  712. "failed to detect %s accessory (adc:0x%x)\n",
  713. attached ? "attached" : "detached", cable_type);
  714. return -EINVAL;
  715. }
  716. return 0;
  717. }
  718. static int max77693_muic_chg_handler(struct max77693_muic_info *info)
  719. {
  720. int chg_type;
  721. int cable_type_gnd;
  722. int cable_type;
  723. bool attached;
  724. bool cable_attached;
  725. int ret = 0;
  726. chg_type = max77693_muic_get_cable_type(info,
  727. MAX77693_CABLE_GROUP_CHG, &attached);
  728. dev_info(info->dev,
  729. "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
  730. attached ? "attached" : "detached",
  731. chg_type, info->prev_chg_type);
  732. switch (chg_type) {
  733. case MAX77693_CHARGER_TYPE_USB:
  734. case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
  735. case MAX77693_CHARGER_TYPE_NONE:
  736. /* Check MAX77693_CABLE_GROUP_ADC_GND type */
  737. cable_type_gnd = max77693_muic_get_cable_type(info,
  738. MAX77693_CABLE_GROUP_ADC_GND,
  739. &cable_attached);
  740. switch (cable_type_gnd) {
  741. case MAX77693_MUIC_GND_MHL:
  742. case MAX77693_MUIC_GND_MHL_VB:
  743. /*
  744. * MHL cable with MHL_TA(USB/TA) cable
  745. * - MHL cable include two port(HDMI line and separate
  746. * micro-usb port. When the target connect MHL cable,
  747. * extcon driver check whether MHL_TA(USB/TA) cable is
  748. * connected. If MHL_TA cable is connected, extcon
  749. * driver notify state to notifiee for charging battery.
  750. *
  751. * Features of 'MHL_TA(USB/TA) with MHL cable'
  752. * - Support MHL
  753. * - Support charging through micro-usb port without
  754. * data connection
  755. */
  756. extcon_set_cable_state(info->edev, "MHL_TA", attached);
  757. if (!cable_attached)
  758. extcon_set_cable_state(info->edev,
  759. "MHL", cable_attached);
  760. break;
  761. }
  762. /* Check MAX77693_CABLE_GROUP_ADC type */
  763. cable_type = max77693_muic_get_cable_type(info,
  764. MAX77693_CABLE_GROUP_ADC,
  765. &cable_attached);
  766. switch (cable_type) {
  767. case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
  768. /*
  769. * Dock-Audio device with USB/TA cable
  770. * - Dock device include two port(Dock-Audio and micro-
  771. * usb port). When the target connect Dock-Audio device,
  772. * extcon driver check whether USB/TA cable is connected
  773. * or not. If USB/TA cable is connected, extcon driver
  774. * notify state to notifiee for charging battery.
  775. *
  776. * Features of 'USB/TA cable with Dock-Audio device'
  777. * - Support external output feature of audio.
  778. * - Support charging through micro-usb port without
  779. * data connection.
  780. */
  781. extcon_set_cable_state(info->edev, "USB", attached);
  782. if (!cable_attached)
  783. extcon_set_cable_state(info->edev, "Dock-Audio",
  784. cable_attached);
  785. break;
  786. case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
  787. /*
  788. * Dock-Smart device with USB/TA cable
  789. * - Dock-Desk device include three type of cable which
  790. * are HDMI, USB for mouse/keyboard and micro-usb port
  791. * for USB/TA cable. Dock-Smart device need always
  792. * exteranl power supply(USB/TA cable through micro-usb
  793. * cable). Dock-Smart device support screen output of
  794. * target to separate monitor and mouse/keyboard for
  795. * desktop mode.
  796. *
  797. * Features of 'USB/TA cable with Dock-Smart device'
  798. * - Support MHL
  799. * - Support external output feature of audio
  800. * - Support charging through micro-usb port without
  801. * data connection if TA cable is connected to target.
  802. * - Support charging and data connection through micro-
  803. * usb port if USB cable is connected between target
  804. * and host device
  805. * - Support OTG device (Mouse/Keyboard)
  806. */
  807. ret = max77693_muic_set_path(info, info->path_usb,
  808. attached);
  809. if (ret < 0)
  810. return ret;
  811. extcon_set_cable_state(info->edev, "Dock-Smart",
  812. attached);
  813. extcon_set_cable_state(info->edev, "MHL", attached);
  814. break;
  815. }
  816. /* Check MAX77693_CABLE_GROUP_CHG type */
  817. switch (chg_type) {
  818. case MAX77693_CHARGER_TYPE_NONE:
  819. /*
  820. * When MHL(with USB/TA cable) or Dock-Audio with USB/TA
  821. * cable is attached, muic device happen below two irq.
  822. * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting
  823. * MHL/Dock-Audio.
  824. * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting
  825. * USB/TA cable connected to MHL or Dock-Audio.
  826. * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC
  827. * irq than MAX77693_MUIC_IRQ_INT2_CHGTYP irq.
  828. *
  829. * If user attach MHL (with USB/TA cable and immediately
  830. * detach MHL with USB/TA cable before MAX77693_MUIC_IRQ
  831. * _INT2_CHGTYP irq is happened, USB/TA cable remain
  832. * connected state to target. But USB/TA cable isn't
  833. * connected to target. The user be face with unusual
  834. * action. So, driver should check this situation in
  835. * spite of, that previous charger type is N/A.
  836. */
  837. break;
  838. case MAX77693_CHARGER_TYPE_USB:
  839. /* Only USB cable, PATH:AP_USB */
  840. ret = max77693_muic_set_path(info, info->path_usb,
  841. attached);
  842. if (ret < 0)
  843. return ret;
  844. extcon_set_cable_state(info->edev, "USB", attached);
  845. break;
  846. case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
  847. /* Only TA cable */
  848. extcon_set_cable_state(info->edev, "TA", attached);
  849. break;
  850. }
  851. break;
  852. case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
  853. extcon_set_cable_state(info->edev,
  854. "Charge-downstream", attached);
  855. break;
  856. case MAX77693_CHARGER_TYPE_APPLE_500MA:
  857. extcon_set_cable_state(info->edev, "Slow-charger", attached);
  858. break;
  859. case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
  860. extcon_set_cable_state(info->edev, "Fast-charger", attached);
  861. break;
  862. case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
  863. break;
  864. default:
  865. dev_err(info->dev,
  866. "failed to detect %s accessory (chg_type:0x%x)\n",
  867. attached ? "attached" : "detached", chg_type);
  868. return -EINVAL;
  869. }
  870. return 0;
  871. }
  872. static void max77693_muic_irq_work(struct work_struct *work)
  873. {
  874. struct max77693_muic_info *info = container_of(work,
  875. struct max77693_muic_info, irq_work);
  876. int irq_type = -1;
  877. int i, ret = 0;
  878. if (!info->edev)
  879. return;
  880. mutex_lock(&info->mutex);
  881. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
  882. if (info->irq == muic_irqs[i].virq)
  883. irq_type = muic_irqs[i].irq;
  884. ret = regmap_bulk_read(info->max77693->regmap_muic,
  885. MAX77693_MUIC_REG_STATUS1, info->status, 2);
  886. if (ret) {
  887. dev_err(info->dev, "failed to read MUIC register\n");
  888. mutex_unlock(&info->mutex);
  889. return;
  890. }
  891. switch (irq_type) {
  892. case MAX77693_MUIC_IRQ_INT1_ADC:
  893. case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
  894. case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
  895. case MAX77693_MUIC_IRQ_INT1_ADC1K:
  896. /* Handle all of accessory except for
  897. type of charger accessory */
  898. ret = max77693_muic_adc_handler(info);
  899. break;
  900. case MAX77693_MUIC_IRQ_INT2_CHGTYP:
  901. case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
  902. case MAX77693_MUIC_IRQ_INT2_DCDTMR:
  903. case MAX77693_MUIC_IRQ_INT2_DXOVP:
  904. case MAX77693_MUIC_IRQ_INT2_VBVOLT:
  905. case MAX77693_MUIC_IRQ_INT2_VIDRM:
  906. /* Handle charger accessory */
  907. ret = max77693_muic_chg_handler(info);
  908. break;
  909. case MAX77693_MUIC_IRQ_INT3_EOC:
  910. case MAX77693_MUIC_IRQ_INT3_CGMBC:
  911. case MAX77693_MUIC_IRQ_INT3_OVP:
  912. case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
  913. case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
  914. case MAX77693_MUIC_IRQ_INT3_BAT_DET:
  915. break;
  916. default:
  917. dev_err(info->dev, "muic interrupt: irq %d occurred\n",
  918. irq_type);
  919. mutex_unlock(&info->mutex);
  920. return;
  921. }
  922. if (ret < 0)
  923. dev_err(info->dev, "failed to handle MUIC interrupt\n");
  924. mutex_unlock(&info->mutex);
  925. return;
  926. }
  927. static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
  928. {
  929. struct max77693_muic_info *info = data;
  930. info->irq = irq;
  931. schedule_work(&info->irq_work);
  932. return IRQ_HANDLED;
  933. }
  934. static struct regmap_config max77693_muic_regmap_config = {
  935. .reg_bits = 8,
  936. .val_bits = 8,
  937. };
  938. static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
  939. {
  940. int ret = 0;
  941. int adc;
  942. int chg_type;
  943. bool attached;
  944. mutex_lock(&info->mutex);
  945. /* Read STATUSx register to detect accessory */
  946. ret = regmap_bulk_read(info->max77693->regmap_muic,
  947. MAX77693_MUIC_REG_STATUS1, info->status, 2);
  948. if (ret) {
  949. dev_err(info->dev, "failed to read MUIC register\n");
  950. mutex_unlock(&info->mutex);
  951. return ret;
  952. }
  953. adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
  954. &attached);
  955. if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
  956. ret = max77693_muic_adc_handler(info);
  957. if (ret < 0) {
  958. dev_err(info->dev, "Cannot detect accessory\n");
  959. mutex_unlock(&info->mutex);
  960. return ret;
  961. }
  962. }
  963. chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
  964. &attached);
  965. if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
  966. ret = max77693_muic_chg_handler(info);
  967. if (ret < 0) {
  968. dev_err(info->dev, "Cannot detect charger accessory\n");
  969. mutex_unlock(&info->mutex);
  970. return ret;
  971. }
  972. }
  973. mutex_unlock(&info->mutex);
  974. return 0;
  975. }
  976. static void max77693_muic_detect_cable_wq(struct work_struct *work)
  977. {
  978. struct max77693_muic_info *info = container_of(to_delayed_work(work),
  979. struct max77693_muic_info, wq_detcable);
  980. max77693_muic_detect_accessory(info);
  981. }
  982. static int max77693_muic_probe(struct platform_device *pdev)
  983. {
  984. struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
  985. struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
  986. struct max77693_muic_info *info;
  987. struct max77693_reg_data *init_data;
  988. int num_init_data;
  989. int delay_jiffies;
  990. int ret;
  991. int i;
  992. unsigned int id;
  993. info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
  994. GFP_KERNEL);
  995. if (!info)
  996. return -ENOMEM;
  997. info->dev = &pdev->dev;
  998. info->max77693 = max77693;
  999. if (info->max77693->regmap_muic) {
  1000. dev_dbg(&pdev->dev, "allocate register map\n");
  1001. } else {
  1002. info->max77693->regmap_muic = devm_regmap_init_i2c(
  1003. info->max77693->muic,
  1004. &max77693_muic_regmap_config);
  1005. if (IS_ERR(info->max77693->regmap_muic)) {
  1006. ret = PTR_ERR(info->max77693->regmap_muic);
  1007. dev_err(max77693->dev,
  1008. "failed to allocate register map: %d\n", ret);
  1009. return ret;
  1010. }
  1011. }
  1012. /* Register input device for button of dock device */
  1013. info->dock = devm_input_allocate_device(&pdev->dev);
  1014. if (!info->dock) {
  1015. dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
  1016. return -ENOMEM;
  1017. }
  1018. info->dock->name = "max77693-muic/dock";
  1019. info->dock->phys = "max77693-muic/extcon";
  1020. info->dock->dev.parent = &pdev->dev;
  1021. __set_bit(EV_REP, info->dock->evbit);
  1022. input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
  1023. input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
  1024. input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
  1025. input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
  1026. input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
  1027. ret = input_register_device(info->dock);
  1028. if (ret < 0) {
  1029. dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
  1030. ret);
  1031. return ret;
  1032. }
  1033. platform_set_drvdata(pdev, info);
  1034. mutex_init(&info->mutex);
  1035. INIT_WORK(&info->irq_work, max77693_muic_irq_work);
  1036. /* Support irq domain for MAX77693 MUIC device */
  1037. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
  1038. struct max77693_muic_irq *muic_irq = &muic_irqs[i];
  1039. unsigned int virq = 0;
  1040. virq = regmap_irq_get_virq(max77693->irq_data_muic,
  1041. muic_irq->irq);
  1042. if (!virq)
  1043. return -EINVAL;
  1044. muic_irq->virq = virq;
  1045. ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
  1046. max77693_muic_irq_handler,
  1047. IRQF_NO_SUSPEND,
  1048. muic_irq->name, info);
  1049. if (ret) {
  1050. dev_err(&pdev->dev,
  1051. "failed: irq request (IRQ: %d,"
  1052. " error :%d)\n",
  1053. muic_irq->irq, ret);
  1054. return ret;
  1055. }
  1056. }
  1057. /* Initialize extcon device */
  1058. info->edev = devm_extcon_dev_allocate(&pdev->dev,
  1059. max77693_extcon_cable);
  1060. if (IS_ERR(info->edev)) {
  1061. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  1062. return -ENOMEM;
  1063. }
  1064. info->edev->name = DEV_NAME;
  1065. ret = devm_extcon_dev_register(&pdev->dev, info->edev);
  1066. if (ret) {
  1067. dev_err(&pdev->dev, "failed to register extcon device\n");
  1068. return ret;
  1069. }
  1070. /* Initialize MUIC register by using platform data or default data */
  1071. if (pdata && pdata->muic_data) {
  1072. init_data = pdata->muic_data->init_data;
  1073. num_init_data = pdata->muic_data->num_init_data;
  1074. } else {
  1075. init_data = default_init_data;
  1076. num_init_data = ARRAY_SIZE(default_init_data);
  1077. }
  1078. for (i = 0; i < num_init_data; i++) {
  1079. enum max77693_irq_source irq_src
  1080. = MAX77693_IRQ_GROUP_NR;
  1081. regmap_write(info->max77693->regmap_muic,
  1082. init_data[i].addr,
  1083. init_data[i].data);
  1084. switch (init_data[i].addr) {
  1085. case MAX77693_MUIC_REG_INTMASK1:
  1086. irq_src = MUIC_INT1;
  1087. break;
  1088. case MAX77693_MUIC_REG_INTMASK2:
  1089. irq_src = MUIC_INT2;
  1090. break;
  1091. case MAX77693_MUIC_REG_INTMASK3:
  1092. irq_src = MUIC_INT3;
  1093. break;
  1094. }
  1095. if (irq_src < MAX77693_IRQ_GROUP_NR)
  1096. info->max77693->irq_masks_cur[irq_src]
  1097. = init_data[i].data;
  1098. }
  1099. if (pdata && pdata->muic_data) {
  1100. struct max77693_muic_platform_data *muic_pdata
  1101. = pdata->muic_data;
  1102. /*
  1103. * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
  1104. * h/w path of COMP2/COMN1 on CONTROL1 register.
  1105. */
  1106. if (muic_pdata->path_uart)
  1107. info->path_uart = muic_pdata->path_uart;
  1108. else
  1109. info->path_uart = CONTROL1_SW_UART;
  1110. if (muic_pdata->path_usb)
  1111. info->path_usb = muic_pdata->path_usb;
  1112. else
  1113. info->path_usb = CONTROL1_SW_USB;
  1114. /*
  1115. * Default delay time for detecting cable state
  1116. * after certain time.
  1117. */
  1118. if (muic_pdata->detcable_delay_ms)
  1119. delay_jiffies =
  1120. msecs_to_jiffies(muic_pdata->detcable_delay_ms);
  1121. else
  1122. delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  1123. } else {
  1124. info->path_usb = CONTROL1_SW_USB;
  1125. info->path_uart = CONTROL1_SW_UART;
  1126. delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  1127. }
  1128. /* Set initial path for UART */
  1129. max77693_muic_set_path(info, info->path_uart, true);
  1130. /* Check revision number of MUIC device*/
  1131. ret = regmap_read(info->max77693->regmap_muic,
  1132. MAX77693_MUIC_REG_ID, &id);
  1133. if (ret < 0) {
  1134. dev_err(&pdev->dev, "failed to read revision number\n");
  1135. return ret;
  1136. }
  1137. dev_info(info->dev, "device ID : 0x%x\n", id);
  1138. /* Set ADC debounce time */
  1139. max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
  1140. /*
  1141. * Detect accessory after completing the initialization of platform
  1142. *
  1143. * - Use delayed workqueue to detect cable state and then
  1144. * notify cable state to notifiee/platform through uevent.
  1145. * After completing the booting of platform, the extcon provider
  1146. * driver should notify cable state to upper layer.
  1147. */
  1148. INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
  1149. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  1150. delay_jiffies);
  1151. return ret;
  1152. }
  1153. static int max77693_muic_remove(struct platform_device *pdev)
  1154. {
  1155. struct max77693_muic_info *info = platform_get_drvdata(pdev);
  1156. cancel_work_sync(&info->irq_work);
  1157. input_unregister_device(info->dock);
  1158. return 0;
  1159. }
  1160. static struct platform_driver max77693_muic_driver = {
  1161. .driver = {
  1162. .name = DEV_NAME,
  1163. .owner = THIS_MODULE,
  1164. },
  1165. .probe = max77693_muic_probe,
  1166. .remove = max77693_muic_remove,
  1167. };
  1168. module_platform_driver(max77693_muic_driver);
  1169. MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
  1170. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  1171. MODULE_LICENSE("GPL");
  1172. MODULE_ALIAS("platform:extcon-max77693");