extcon-max77693.c 36 KB

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