extcon-max77693.c 36 KB

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