extcon-max77693.c 36 KB

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