extcon-max77693.c 38 KB

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