extcon-max77693.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. static 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. return -EAGAIN;
  210. }
  211. break;
  212. default:
  213. dev_err(info->dev, "invalid ADC debounce time\n");
  214. return -EINVAL;
  215. }
  216. return 0;
  217. };
  218. /*
  219. * max77693_muic_set_path - Set hardware line according to attached cable
  220. * @info: the instance including private data of max77693 MUIC
  221. * @value: the path according to attached cable
  222. * @attached: the state of cable (true:attached, false:detached)
  223. *
  224. * The max77693 MUIC device share outside H/W line among a varity of cables
  225. * so, this function set internal path of H/W line according to the type of
  226. * attached cable.
  227. */
  228. static int max77693_muic_set_path(struct max77693_muic_info *info,
  229. u8 val, bool attached)
  230. {
  231. int ret = 0;
  232. u8 ctrl1, ctrl2 = 0;
  233. if (attached)
  234. ctrl1 = val;
  235. else
  236. ctrl1 = CONTROL1_SW_OPEN;
  237. ret = max77693_update_reg(info->max77693->regmap_muic,
  238. MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
  239. if (ret < 0) {
  240. dev_err(info->dev, "failed to update MUIC register\n");
  241. return -EAGAIN;
  242. }
  243. if (attached)
  244. ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
  245. else
  246. ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
  247. ret = max77693_update_reg(info->max77693->regmap_muic,
  248. MAX77693_MUIC_REG_CTRL2, ctrl2,
  249. CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
  250. if (ret < 0) {
  251. dev_err(info->dev, "failed to update MUIC register\n");
  252. return -EAGAIN;
  253. }
  254. dev_info(info->dev,
  255. "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
  256. ctrl1, ctrl2, attached ? "attached" : "detached");
  257. return 0;
  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. int vbvolt;
  398. bool cable_attached;
  399. char dock_name[CABLE_NAME_MAX];
  400. dev_info(info->dev,
  401. "external connector is %s (adc:0x%02x)\n",
  402. attached ? "attached" : "detached", cable_type);
  403. switch (cable_type) {
  404. case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
  405. /*
  406. * Check power cable whether attached or detached state.
  407. * The Dock-Smart device need surely external power supply.
  408. * If power cable(USB/TA) isn't connected to Dock device,
  409. * user can't use Dock-Smart for desktop mode.
  410. */
  411. vbvolt = max77693_muic_get_cable_type(info,
  412. MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);
  413. if (attached && !vbvolt) {
  414. dev_warn(info->dev,
  415. "Cannot detect external power supply\n");
  416. return 0;
  417. }
  418. /*
  419. * Notify Dock-Smart/MHL state.
  420. * - Dock-Smart device include three type of cable which
  421. * are HDMI, USB for mouse/keyboard and micro-usb port
  422. * for USB/TA cable. Dock-Smart device need always exteranl
  423. * power supply(USB/TA cable through micro-usb cable). Dock-
  424. * Smart device support screen output of target to separate
  425. * monitor and mouse/keyboard for desktop mode.
  426. *
  427. * Features of 'USB/TA cable with Dock-Smart device'
  428. * - Support MHL
  429. * - Support external output feature of audio
  430. * - Support charging through micro-usb port without data
  431. * connection if TA cable is connected to target.
  432. * - Support charging and data connection through micro-usb port
  433. * if USB cable is connected between target and host
  434. * device.
  435. * - Support OTG device (Mouse/Keyboard)
  436. */
  437. ret = max77693_muic_set_path(info, info->path_usb, attached);
  438. if (ret < 0)
  439. return ret;
  440. extcon_set_cable_state(info->edev, "Dock-Smart", attached);
  441. extcon_set_cable_state(info->edev, "MHL", attached);
  442. goto out;
  443. case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
  444. strcpy(dock_name, "Dock-Car");
  445. break;
  446. case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
  447. strcpy(dock_name, "Dock-Desk");
  448. break;
  449. case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
  450. strcpy(dock_name, "Dock-Audio");
  451. if (!attached)
  452. extcon_set_cable_state(info->edev, "USB", false);
  453. break;
  454. default:
  455. dev_err(info->dev, "failed to detect %s dock device\n",
  456. attached ? "attached" : "detached");
  457. return -EINVAL;
  458. }
  459. /* Dock-Car/Desk/Audio, PATH:AUDIO */
  460. ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
  461. if (ret < 0)
  462. return ret;
  463. extcon_set_cable_state(info->edev, dock_name, attached);
  464. out:
  465. return 0;
  466. }
  467. static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
  468. int button_type, bool attached)
  469. {
  470. struct input_dev *dock = info->dock;
  471. unsigned int code;
  472. switch (button_type) {
  473. case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
  474. ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
  475. /* DOCK_KEY_PREV */
  476. code = KEY_PREVIOUSSONG;
  477. break;
  478. case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
  479. ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
  480. /* DOCK_KEY_NEXT */
  481. code = KEY_NEXTSONG;
  482. break;
  483. case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
  484. /* DOCK_VOL_DOWN */
  485. code = KEY_VOLUMEDOWN;
  486. break;
  487. case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
  488. /* DOCK_VOL_UP */
  489. code = KEY_VOLUMEUP;
  490. break;
  491. case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
  492. ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
  493. /* DOCK_KEY_PLAY_PAUSE */
  494. code = KEY_PLAYPAUSE;
  495. break;
  496. default:
  497. dev_err(info->dev,
  498. "failed to detect %s key (adc:0x%x)\n",
  499. attached ? "pressed" : "released", button_type);
  500. return -EINVAL;
  501. }
  502. input_event(dock, EV_KEY, code, attached);
  503. input_sync(dock);
  504. return 0;
  505. }
  506. static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
  507. {
  508. int cable_type_gnd;
  509. int ret = 0;
  510. bool attached;
  511. cable_type_gnd = max77693_muic_get_cable_type(info,
  512. MAX77693_CABLE_GROUP_ADC_GND, &attached);
  513. switch (cable_type_gnd) {
  514. case MAX77693_MUIC_GND_USB_OTG:
  515. case MAX77693_MUIC_GND_USB_OTG_VB:
  516. /* USB_OTG, PATH: AP_USB */
  517. ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
  518. if (ret < 0)
  519. return ret;
  520. extcon_set_cable_state(info->edev, "USB-Host", attached);
  521. break;
  522. case MAX77693_MUIC_GND_AV_CABLE_LOAD:
  523. /* Audio Video Cable with load, PATH:AUDIO */
  524. ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
  525. if (ret < 0)
  526. return ret;
  527. extcon_set_cable_state(info->edev,
  528. "Audio-video-load", attached);
  529. break;
  530. case MAX77693_MUIC_GND_MHL:
  531. case MAX77693_MUIC_GND_MHL_VB:
  532. /* MHL or MHL with USB/TA cable */
  533. extcon_set_cable_state(info->edev, "MHL", attached);
  534. break;
  535. default:
  536. dev_err(info->dev, "failed to detect %s cable of gnd type\n",
  537. attached ? "attached" : "detached");
  538. return -EINVAL;
  539. }
  540. return 0;
  541. }
  542. static int max77693_muic_jig_handler(struct max77693_muic_info *info,
  543. int cable_type, bool attached)
  544. {
  545. char cable_name[32];
  546. int ret = 0;
  547. u8 path = CONTROL1_SW_OPEN;
  548. dev_info(info->dev,
  549. "external connector is %s (adc:0x%02x)\n",
  550. attached ? "attached" : "detached", cable_type);
  551. switch (cable_type) {
  552. case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
  553. /* PATH:AP_USB */
  554. strcpy(cable_name, "JIG-USB-OFF");
  555. path = CONTROL1_SW_USB;
  556. break;
  557. case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
  558. /* PATH:AP_USB */
  559. strcpy(cable_name, "JIG-USB-ON");
  560. path = CONTROL1_SW_USB;
  561. break;
  562. case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
  563. /* PATH:AP_UART */
  564. strcpy(cable_name, "JIG-UART-OFF");
  565. path = CONTROL1_SW_UART;
  566. break;
  567. default:
  568. dev_err(info->dev, "failed to detect %s jig cable\n",
  569. attached ? "attached" : "detached");
  570. return -EINVAL;
  571. }
  572. ret = max77693_muic_set_path(info, path, attached);
  573. if (ret < 0)
  574. return ret;
  575. extcon_set_cable_state(info->edev, cable_name, attached);
  576. return 0;
  577. }
  578. static int max77693_muic_adc_handler(struct max77693_muic_info *info)
  579. {
  580. int cable_type;
  581. int button_type;
  582. bool attached;
  583. int ret = 0;
  584. /* Check accessory state which is either detached or attached */
  585. cable_type = max77693_muic_get_cable_type(info,
  586. MAX77693_CABLE_GROUP_ADC, &attached);
  587. dev_info(info->dev,
  588. "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
  589. attached ? "attached" : "detached", cable_type,
  590. info->prev_cable_type);
  591. switch (cable_type) {
  592. case MAX77693_MUIC_ADC_GROUND:
  593. /* USB_OTG/MHL/Audio */
  594. max77693_muic_adc_ground_handler(info);
  595. break;
  596. case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
  597. case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
  598. case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
  599. /* JIG */
  600. ret = max77693_muic_jig_handler(info, cable_type, attached);
  601. if (ret < 0)
  602. return ret;
  603. break;
  604. case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
  605. case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
  606. case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
  607. case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
  608. /*
  609. * DOCK device
  610. *
  611. * The MAX77693 MUIC device can detect total 34 cable type
  612. * except of charger cable and MUIC device didn't define
  613. * specfic role of cable in the range of from 0x01 to 0x12
  614. * of ADC value. So, can use/define cable with no role according
  615. * to schema of hardware board.
  616. */
  617. ret = max77693_muic_dock_handler(info, cable_type, attached);
  618. if (ret < 0)
  619. return ret;
  620. break;
  621. case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON: /* DOCK_KEY_PREV */
  622. case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON: /* DOCK_KEY_NEXT */
  623. case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: /* DOCK_VOL_DOWN */
  624. case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: /* DOCK_VOL_UP */
  625. case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON: /* DOCK_KEY_PLAY_PAUSE */
  626. /*
  627. * Button of DOCK device
  628. * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
  629. *
  630. * The MAX77693 MUIC device can detect total 34 cable type
  631. * except of charger cable and MUIC device didn't define
  632. * specfic role of cable in the range of from 0x01 to 0x12
  633. * of ADC value. So, can use/define cable with no role according
  634. * to schema of hardware board.
  635. */
  636. if (attached)
  637. button_type = info->prev_button_type = cable_type;
  638. else
  639. button_type = info->prev_button_type;
  640. ret = max77693_muic_dock_button_handler(info, button_type,
  641. attached);
  642. if (ret < 0)
  643. return ret;
  644. break;
  645. case MAX77693_MUIC_ADC_SEND_END_BUTTON:
  646. case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
  647. case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
  648. case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
  649. case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
  650. case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
  651. case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
  652. case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
  653. case MAX77693_MUIC_ADC_RESERVED_ACC_1:
  654. case MAX77693_MUIC_ADC_RESERVED_ACC_2:
  655. case MAX77693_MUIC_ADC_RESERVED_ACC_4:
  656. case MAX77693_MUIC_ADC_RESERVED_ACC_5:
  657. case MAX77693_MUIC_ADC_CEA936_AUDIO:
  658. case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
  659. case MAX77693_MUIC_ADC_TTY_CONVERTER:
  660. case MAX77693_MUIC_ADC_UART_CABLE:
  661. case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
  662. case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
  663. /*
  664. * This accessory isn't used in general case if it is specially
  665. * needed to detect additional accessory, should implement
  666. * proper operation when this accessory is attached/detached.
  667. */
  668. dev_info(info->dev,
  669. "accessory is %s but it isn't used (adc:0x%x)\n",
  670. attached ? "attached" : "detached", cable_type);
  671. return -EAGAIN;
  672. default:
  673. dev_err(info->dev,
  674. "failed to detect %s accessory (adc:0x%x)\n",
  675. attached ? "attached" : "detached", cable_type);
  676. return -EINVAL;
  677. }
  678. return 0;
  679. }
  680. static int max77693_muic_chg_handler(struct max77693_muic_info *info)
  681. {
  682. int chg_type;
  683. int cable_type_gnd;
  684. int cable_type;
  685. bool attached;
  686. bool cable_attached;
  687. int ret = 0;
  688. chg_type = max77693_muic_get_cable_type(info,
  689. MAX77693_CABLE_GROUP_CHG, &attached);
  690. dev_info(info->dev,
  691. "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
  692. attached ? "attached" : "detached",
  693. chg_type, info->prev_chg_type);
  694. switch (chg_type) {
  695. case MAX77693_CHARGER_TYPE_USB:
  696. case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
  697. case MAX77693_CHARGER_TYPE_NONE:
  698. /* Check MAX77693_CABLE_GROUP_ADC_GND type */
  699. cable_type_gnd = max77693_muic_get_cable_type(info,
  700. MAX77693_CABLE_GROUP_ADC_GND,
  701. &cable_attached);
  702. switch (cable_type_gnd) {
  703. case MAX77693_MUIC_GND_MHL:
  704. case MAX77693_MUIC_GND_MHL_VB:
  705. /*
  706. * MHL cable with MHL_TA(USB/TA) cable
  707. * - MHL cable include two port(HDMI line and separate micro-
  708. * usb port. When the target connect MHL cable, extcon driver
  709. * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA
  710. * cable is connected, extcon driver notify state to notifiee
  711. * for charging battery.
  712. *
  713. * Features of 'MHL_TA(USB/TA) with MHL cable'
  714. * - Support MHL
  715. * - Support charging through micro-usb port without data connection
  716. */
  717. extcon_set_cable_state(info->edev, "MHL_TA", attached);
  718. if (!cable_attached)
  719. extcon_set_cable_state(info->edev, "MHL", cable_attached);
  720. break;
  721. }
  722. /* Check MAX77693_CABLE_GROUP_ADC type */
  723. cable_type = max77693_muic_get_cable_type(info,
  724. MAX77693_CABLE_GROUP_ADC,
  725. &cable_attached);
  726. switch (cable_type) {
  727. case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
  728. /*
  729. * Dock-Audio device with USB/TA cable
  730. * - Dock device include two port(Dock-Audio and micro-usb
  731. * port). When the target connect Dock-Audio device, extcon
  732. * driver check whether USB/TA cable is connected. If USB/TA
  733. * cable is connected, extcon driver notify state to notifiee
  734. * for charging battery.
  735. *
  736. * Features of 'USB/TA cable with Dock-Audio device'
  737. * - Support external output feature of audio.
  738. * - Support charging through micro-usb port without data
  739. * connection.
  740. */
  741. extcon_set_cable_state(info->edev, "USB", attached);
  742. if (!cable_attached)
  743. extcon_set_cable_state(info->edev, "Dock-Audio", cable_attached);
  744. break;
  745. case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
  746. /*
  747. * Dock-Smart device with USB/TA cable
  748. * - Dock-Desk device include three type of cable which
  749. * are HDMI, USB for mouse/keyboard and micro-usb port
  750. * for USB/TA cable. Dock-Smart device need always exteranl
  751. * power supply(USB/TA cable through micro-usb cable). Dock-
  752. * Smart device support screen output of target to separate
  753. * monitor and mouse/keyboard for desktop mode.
  754. *
  755. * Features of 'USB/TA cable with Dock-Smart device'
  756. * - Support MHL
  757. * - Support external output feature of audio
  758. * - Support charging through micro-usb port without data
  759. * connection if TA cable is connected to target.
  760. * - Support charging and data connection through micro-usb port
  761. * if USB cable is connected between target and host
  762. * device.
  763. * - Support OTG device (Mouse/Keyboard)
  764. */
  765. ret = max77693_muic_set_path(info, info->path_usb, attached);
  766. if (ret < 0)
  767. return ret;
  768. extcon_set_cable_state(info->edev, "Dock-Smart", attached);
  769. extcon_set_cable_state(info->edev, "MHL", attached);
  770. break;
  771. }
  772. /* Check MAX77693_CABLE_GROUP_CHG type */
  773. switch (chg_type) {
  774. case MAX77693_CHARGER_TYPE_NONE:
  775. /*
  776. * When MHL(with USB/TA cable) or Dock-Audio with USB/TA cable
  777. * is attached, muic device happen below two interrupt.
  778. * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting MHL/Dock-Audio.
  779. * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting USB/TA cable
  780. * connected to MHL or Dock-Audio.
  781. * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC interrupt
  782. * than MAX77693_MUIC_IRQ_INT2_CHGTYP interrupt.
  783. *
  784. * If user attach MHL (with USB/TA cable and immediately detach
  785. * MHL with USB/TA cable before MAX77693_MUIC_IRQ_INT2_CHGTYP
  786. * interrupt is happened, USB/TA cable remain connected state to
  787. * target. But USB/TA cable isn't connected to target. The user
  788. * be face with unusual action. So, driver should check this
  789. * situation in spite of, that previous charger type is N/A.
  790. */
  791. break;
  792. case MAX77693_CHARGER_TYPE_USB:
  793. /* Only USB cable, PATH:AP_USB */
  794. ret = max77693_muic_set_path(info, info->path_usb, attached);
  795. if (ret < 0)
  796. return ret;
  797. extcon_set_cable_state(info->edev, "USB", attached);
  798. break;
  799. case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
  800. /* Only TA cable */
  801. extcon_set_cable_state(info->edev, "TA", attached);
  802. break;
  803. }
  804. break;
  805. case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
  806. extcon_set_cable_state(info->edev,
  807. "Charge-downstream", attached);
  808. break;
  809. case MAX77693_CHARGER_TYPE_APPLE_500MA:
  810. extcon_set_cable_state(info->edev, "Slow-charger", attached);
  811. break;
  812. case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
  813. extcon_set_cable_state(info->edev, "Fast-charger", attached);
  814. break;
  815. case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
  816. break;
  817. default:
  818. dev_err(info->dev,
  819. "failed to detect %s accessory (chg_type:0x%x)\n",
  820. attached ? "attached" : "detached", chg_type);
  821. return -EINVAL;
  822. }
  823. return 0;
  824. }
  825. static void max77693_muic_irq_work(struct work_struct *work)
  826. {
  827. struct max77693_muic_info *info = container_of(work,
  828. struct max77693_muic_info, irq_work);
  829. int irq_type = -1;
  830. int i, ret = 0;
  831. if (!info->edev)
  832. return;
  833. mutex_lock(&info->mutex);
  834. for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
  835. if (info->irq == muic_irqs[i].virq)
  836. irq_type = muic_irqs[i].irq;
  837. ret = max77693_bulk_read(info->max77693->regmap_muic,
  838. MAX77693_MUIC_REG_STATUS1, 2, info->status);
  839. if (ret) {
  840. dev_err(info->dev, "failed to read MUIC register\n");
  841. mutex_unlock(&info->mutex);
  842. return;
  843. }
  844. switch (irq_type) {
  845. case MAX77693_MUIC_IRQ_INT1_ADC:
  846. case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
  847. case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
  848. case MAX77693_MUIC_IRQ_INT1_ADC1K:
  849. /* Handle all of accessory except for
  850. type of charger accessory */
  851. ret = max77693_muic_adc_handler(info);
  852. break;
  853. case MAX77693_MUIC_IRQ_INT2_CHGTYP:
  854. case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
  855. case MAX77693_MUIC_IRQ_INT2_DCDTMR:
  856. case MAX77693_MUIC_IRQ_INT2_DXOVP:
  857. case MAX77693_MUIC_IRQ_INT2_VBVOLT:
  858. case MAX77693_MUIC_IRQ_INT2_VIDRM:
  859. /* Handle charger accessory */
  860. ret = max77693_muic_chg_handler(info);
  861. break;
  862. case MAX77693_MUIC_IRQ_INT3_EOC:
  863. case MAX77693_MUIC_IRQ_INT3_CGMBC:
  864. case MAX77693_MUIC_IRQ_INT3_OVP:
  865. case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
  866. case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
  867. case MAX77693_MUIC_IRQ_INT3_BAT_DET:
  868. break;
  869. default:
  870. dev_err(info->dev, "muic interrupt: irq %d occurred\n",
  871. irq_type);
  872. mutex_unlock(&info->mutex);
  873. return;
  874. }
  875. if (ret < 0)
  876. dev_err(info->dev, "failed to handle MUIC interrupt\n");
  877. mutex_unlock(&info->mutex);
  878. return;
  879. }
  880. static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
  881. {
  882. struct max77693_muic_info *info = data;
  883. info->irq = irq;
  884. schedule_work(&info->irq_work);
  885. return IRQ_HANDLED;
  886. }
  887. static struct regmap_config max77693_muic_regmap_config = {
  888. .reg_bits = 8,
  889. .val_bits = 8,
  890. };
  891. static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
  892. {
  893. int ret = 0;
  894. int adc;
  895. int chg_type;
  896. bool attached;
  897. mutex_lock(&info->mutex);
  898. /* Read STATUSx register to detect accessory */
  899. ret = max77693_bulk_read(info->max77693->regmap_muic,
  900. MAX77693_MUIC_REG_STATUS1, 2, info->status);
  901. if (ret) {
  902. dev_err(info->dev, "failed to read MUIC register\n");
  903. mutex_unlock(&info->mutex);
  904. return -EINVAL;
  905. }
  906. adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
  907. &attached);
  908. if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
  909. ret = max77693_muic_adc_handler(info);
  910. if (ret < 0) {
  911. dev_err(info->dev, "Cannot detect accessory\n");
  912. mutex_unlock(&info->mutex);
  913. return ret;
  914. }
  915. }
  916. chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
  917. &attached);
  918. if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
  919. ret = max77693_muic_chg_handler(info);
  920. if (ret < 0) {
  921. dev_err(info->dev, "Cannot detect charger accessory\n");
  922. mutex_unlock(&info->mutex);
  923. return ret;
  924. }
  925. }
  926. mutex_unlock(&info->mutex);
  927. return 0;
  928. }
  929. static void max77693_muic_detect_cable_wq(struct work_struct *work)
  930. {
  931. struct max77693_muic_info *info = container_of(to_delayed_work(work),
  932. struct max77693_muic_info, wq_detcable);
  933. max77693_muic_detect_accessory(info);
  934. }
  935. static int max77693_muic_probe(struct platform_device *pdev)
  936. {
  937. struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
  938. struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
  939. struct max77693_muic_info *info;
  940. int delay_jiffies;
  941. int ret;
  942. int i;
  943. u8 id;
  944. info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
  945. GFP_KERNEL);
  946. if (!info) {
  947. dev_err(&pdev->dev, "failed to allocate memory\n");
  948. return -ENOMEM;
  949. }
  950. info->dev = &pdev->dev;
  951. info->max77693 = max77693;
  952. if (info->max77693->regmap_muic) {
  953. dev_dbg(&pdev->dev, "allocate register map\n");
  954. } else {
  955. info->max77693->regmap_muic = devm_regmap_init_i2c(
  956. info->max77693->muic,
  957. &max77693_muic_regmap_config);
  958. if (IS_ERR(info->max77693->regmap_muic)) {
  959. ret = PTR_ERR(info->max77693->regmap_muic);
  960. dev_err(max77693->dev,
  961. "failed to allocate register map: %d\n", ret);
  962. return ret;
  963. }
  964. }
  965. /* Register input device for button of dock device */
  966. info->dock = devm_input_allocate_device(&pdev->dev);
  967. if (!info->dock) {
  968. dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
  969. return -ENOMEM;
  970. }
  971. info->dock->name = "max77693-muic/dock";
  972. info->dock->phys = "max77693-muic/extcon";
  973. info->dock->dev.parent = &pdev->dev;
  974. __set_bit(EV_REP, info->dock->evbit);
  975. input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
  976. input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
  977. input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
  978. input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
  979. input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
  980. ret = input_register_device(info->dock);
  981. if (ret < 0) {
  982. dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
  983. ret);
  984. return ret;
  985. }
  986. platform_set_drvdata(pdev, info);
  987. mutex_init(&info->mutex);
  988. INIT_WORK(&info->irq_work, max77693_muic_irq_work);
  989. /* Support irq domain for MAX77693 MUIC device */
  990. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
  991. struct max77693_muic_irq *muic_irq = &muic_irqs[i];
  992. unsigned int virq = 0;
  993. virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
  994. if (!virq) {
  995. ret = -EINVAL;
  996. goto err_irq;
  997. }
  998. muic_irq->virq = virq;
  999. ret = request_threaded_irq(virq, NULL,
  1000. max77693_muic_irq_handler,
  1001. IRQF_NO_SUSPEND,
  1002. muic_irq->name, info);
  1003. if (ret) {
  1004. dev_err(&pdev->dev,
  1005. "failed: irq request (IRQ: %d,"
  1006. " error :%d)\n",
  1007. muic_irq->irq, ret);
  1008. goto err_irq;
  1009. }
  1010. }
  1011. /* Initialize extcon device */
  1012. info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
  1013. GFP_KERNEL);
  1014. if (!info->edev) {
  1015. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  1016. ret = -ENOMEM;
  1017. goto err_irq;
  1018. }
  1019. info->edev->name = DEV_NAME;
  1020. info->edev->supported_cable = max77693_extcon_cable;
  1021. ret = extcon_dev_register(info->edev, NULL);
  1022. if (ret) {
  1023. dev_err(&pdev->dev, "failed to register extcon device\n");
  1024. goto err_irq;
  1025. }
  1026. if (pdata->muic_data) {
  1027. struct max77693_muic_platform_data *muic_pdata = pdata->muic_data;
  1028. /* Initialize MUIC register by using platform data */
  1029. for (i = 0 ; i < muic_pdata->num_init_data ; i++) {
  1030. enum max77693_irq_source irq_src
  1031. = MAX77693_IRQ_GROUP_NR;
  1032. max77693_write_reg(info->max77693->regmap_muic,
  1033. muic_pdata->init_data[i].addr,
  1034. muic_pdata->init_data[i].data);
  1035. switch (muic_pdata->init_data[i].addr) {
  1036. case MAX77693_MUIC_REG_INTMASK1:
  1037. irq_src = MUIC_INT1;
  1038. break;
  1039. case MAX77693_MUIC_REG_INTMASK2:
  1040. irq_src = MUIC_INT2;
  1041. break;
  1042. case MAX77693_MUIC_REG_INTMASK3:
  1043. irq_src = MUIC_INT3;
  1044. break;
  1045. }
  1046. if (irq_src < MAX77693_IRQ_GROUP_NR)
  1047. info->max77693->irq_masks_cur[irq_src]
  1048. = muic_pdata->init_data[i].data;
  1049. }
  1050. /*
  1051. * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
  1052. * h/w path of COMP2/COMN1 on CONTROL1 register.
  1053. */
  1054. if (muic_pdata->path_uart)
  1055. info->path_uart = muic_pdata->path_uart;
  1056. else
  1057. info->path_uart = CONTROL1_SW_UART;
  1058. if (muic_pdata->path_usb)
  1059. info->path_usb = muic_pdata->path_usb;
  1060. else
  1061. info->path_usb = CONTROL1_SW_USB;
  1062. /*
  1063. * Default delay time for detecting cable state
  1064. * after certain time.
  1065. */
  1066. if (muic_pdata->detcable_delay_ms)
  1067. delay_jiffies =
  1068. msecs_to_jiffies(muic_pdata->detcable_delay_ms);
  1069. else
  1070. delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  1071. } else {
  1072. info->path_usb = CONTROL1_SW_USB;
  1073. info->path_uart = CONTROL1_SW_UART;
  1074. delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  1075. }
  1076. /* Set initial path for UART */
  1077. max77693_muic_set_path(info, info->path_uart, true);
  1078. /* Check revision number of MUIC device*/
  1079. ret = max77693_read_reg(info->max77693->regmap_muic,
  1080. MAX77693_MUIC_REG_ID, &id);
  1081. if (ret < 0) {
  1082. dev_err(&pdev->dev, "failed to read revision number\n");
  1083. goto err_extcon;
  1084. }
  1085. dev_info(info->dev, "device ID : 0x%x\n", id);
  1086. /* Set ADC debounce time */
  1087. max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
  1088. /*
  1089. * Detect accessory after completing the initialization of platform
  1090. *
  1091. * - Use delayed workqueue to detect cable state and then
  1092. * notify cable state to notifiee/platform through uevent.
  1093. * After completing the booting of platform, the extcon provider
  1094. * driver should notify cable state to upper layer.
  1095. */
  1096. INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
  1097. schedule_delayed_work(&info->wq_detcable, delay_jiffies);
  1098. return ret;
  1099. err_extcon:
  1100. extcon_dev_unregister(info->edev);
  1101. err_irq:
  1102. while (--i >= 0)
  1103. free_irq(muic_irqs[i].virq, info);
  1104. return ret;
  1105. }
  1106. static int max77693_muic_remove(struct platform_device *pdev)
  1107. {
  1108. struct max77693_muic_info *info = platform_get_drvdata(pdev);
  1109. int i;
  1110. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
  1111. free_irq(muic_irqs[i].virq, info);
  1112. cancel_work_sync(&info->irq_work);
  1113. input_unregister_device(info->dock);
  1114. extcon_dev_unregister(info->edev);
  1115. return 0;
  1116. }
  1117. static struct platform_driver max77693_muic_driver = {
  1118. .driver = {
  1119. .name = DEV_NAME,
  1120. .owner = THIS_MODULE,
  1121. },
  1122. .probe = max77693_muic_probe,
  1123. .remove = max77693_muic_remove,
  1124. };
  1125. module_platform_driver(max77693_muic_driver);
  1126. MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
  1127. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  1128. MODULE_LICENSE("GPL");
  1129. MODULE_ALIAS("platform:extcon-max77693");