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