extcon-max77693.c 37 KB

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