extcon-max77693.c 33 KB

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