extcon-max77693.c 37 KB

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