extcon-max77693.c 38 KB

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