extcon-max77693.c 36 KB

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