extcon-max77693.c 36 KB

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