extcon-max77693.c 37 KB

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