extcon-max77693.c 38 KB

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