extcon-max77693.c 37 KB

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