extcon-rt8973a.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * extcon-rt8973a.c - Richtek RT8973A extcon driver to support USB switches
  3. *
  4. * Copyright (c) 2014 Samsung Electronics Co., Ltd
  5. * Author: Chanwoo Choi <cw00.choi@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/input.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <linux/extcon.h>
  23. #include "extcon-rt8973a.h"
  24. #define DELAY_MS_DEFAULT 20000 /* unit: millisecond */
  25. struct muic_irq {
  26. unsigned int irq;
  27. const char *name;
  28. unsigned int virq;
  29. };
  30. struct reg_data {
  31. u8 reg;
  32. u8 mask;
  33. u8 val;
  34. bool invert;
  35. };
  36. struct rt8973a_muic_info {
  37. struct device *dev;
  38. struct extcon_dev *edev;
  39. struct i2c_client *i2c;
  40. struct regmap *regmap;
  41. struct regmap_irq_chip_data *irq_data;
  42. struct muic_irq *muic_irqs;
  43. unsigned int num_muic_irqs;
  44. int irq;
  45. bool irq_attach;
  46. bool irq_detach;
  47. bool irq_ovp;
  48. bool irq_otp;
  49. struct work_struct irq_work;
  50. struct reg_data *reg_data;
  51. unsigned int num_reg_data;
  52. bool auto_config;
  53. struct mutex mutex;
  54. /*
  55. * Use delayed workqueue to detect cable state and then
  56. * notify cable state to notifiee/platform through uevent.
  57. * After completing the booting of platform, the extcon provider
  58. * driver should notify cable state to upper layer.
  59. */
  60. struct delayed_work wq_detcable;
  61. };
  62. /* Default value of RT8973A register to bring up MUIC device. */
  63. static struct reg_data rt8973a_reg_data[] = {
  64. {
  65. .reg = RT8973A_REG_CONTROL1,
  66. .mask = RT8973A_REG_CONTROL1_ADC_EN_MASK
  67. | RT8973A_REG_CONTROL1_USB_CHD_EN_MASK
  68. | RT8973A_REG_CONTROL1_CHGTYP_MASK
  69. | RT8973A_REG_CONTROL1_SWITCH_OPEN_MASK
  70. | RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK
  71. | RT8973A_REG_CONTROL1_INTM_MASK,
  72. .val = RT8973A_REG_CONTROL1_ADC_EN_MASK
  73. | RT8973A_REG_CONTROL1_USB_CHD_EN_MASK
  74. | RT8973A_REG_CONTROL1_CHGTYP_MASK,
  75. .invert = false,
  76. },
  77. { /* sentinel */ }
  78. };
  79. /* List of detectable cables */
  80. enum {
  81. EXTCON_CABLE_USB = 0,
  82. EXTCON_CABLE_USB_HOST,
  83. EXTCON_CABLE_TA,
  84. EXTCON_CABLE_JIG_OFF_USB,
  85. EXTCON_CABLE_JIG_ON_USB,
  86. EXTCON_CABLE_JIG_OFF_UART,
  87. EXTCON_CABLE_JIG_ON_UART,
  88. EXTCON_CABLE_END,
  89. };
  90. static const char *rt8973a_extcon_cable[] = {
  91. [EXTCON_CABLE_USB] = "USB",
  92. [EXTCON_CABLE_USB_HOST] = "USB-Host",
  93. [EXTCON_CABLE_TA] = "TA",
  94. [EXTCON_CABLE_JIG_OFF_USB] = "JIG-USB-OFF",
  95. [EXTCON_CABLE_JIG_ON_USB] = "JIG-USB-ON",
  96. [EXTCON_CABLE_JIG_OFF_UART] = "JIG-UART-OFF",
  97. [EXTCON_CABLE_JIG_ON_UART] = "JIG-UART-ON",
  98. NULL,
  99. };
  100. /* Define OVP (Over Voltage Protection), OTP (Over Temperature Protection) */
  101. enum rt8973a_event_type {
  102. RT8973A_EVENT_ATTACH = 1,
  103. RT8973A_EVENT_DETACH,
  104. RT8973A_EVENT_OVP,
  105. RT8973A_EVENT_OTP,
  106. };
  107. /* Define supported accessory type */
  108. enum rt8973a_muic_acc_type {
  109. RT8973A_MUIC_ADC_OTG = 0x0,
  110. RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON,
  111. RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON,
  112. RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON,
  113. RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON,
  114. RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON,
  115. RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON,
  116. RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON,
  117. RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON,
  118. RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON,
  119. RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON,
  120. RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON,
  121. RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON,
  122. RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON,
  123. RT8973A_MUIC_ADC_RESERVED_ACC_1,
  124. RT8973A_MUIC_ADC_RESERVED_ACC_2,
  125. RT8973A_MUIC_ADC_RESERVED_ACC_3,
  126. RT8973A_MUIC_ADC_RESERVED_ACC_4,
  127. RT8973A_MUIC_ADC_RESERVED_ACC_5,
  128. RT8973A_MUIC_ADC_AUDIO_TYPE2,
  129. RT8973A_MUIC_ADC_PHONE_POWERED_DEV,
  130. RT8973A_MUIC_ADC_UNKNOWN_ACC_1,
  131. RT8973A_MUIC_ADC_UNKNOWN_ACC_2,
  132. RT8973A_MUIC_ADC_TA,
  133. RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
  134. RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
  135. RT8973A_MUIC_ADC_UNKNOWN_ACC_3,
  136. RT8973A_MUIC_ADC_UNKNOWN_ACC_4,
  137. RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
  138. RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
  139. RT8973A_MUIC_ADC_UNKNOWN_ACC_5,
  140. RT8973A_MUIC_ADC_OPEN = 0x1f,
  141. /* The below accessories has same ADC value (0x1f).
  142. So, Device type1 is used to separate specific accessory. */
  143. /* |---------|--ADC| */
  144. /* | [7:5]|[4:0]| */
  145. RT8973A_MUIC_ADC_USB = 0x3f, /* | 001|11111| */
  146. };
  147. /* List of supported interrupt for RT8973A */
  148. static struct muic_irq rt8973a_muic_irqs[] = {
  149. { RT8973A_INT1_ATTACH, "muic-attach" },
  150. { RT8973A_INT1_DETACH, "muic-detach" },
  151. { RT8973A_INT1_CHGDET, "muic-chgdet" },
  152. { RT8973A_INT1_DCD_T, "muic-dcd-t" },
  153. { RT8973A_INT1_OVP, "muic-ovp" },
  154. { RT8973A_INT1_CONNECT, "muic-connect" },
  155. { RT8973A_INT1_ADC_CHG, "muic-adc-chg" },
  156. { RT8973A_INT1_OTP, "muic-otp" },
  157. { RT8973A_INT2_UVLO, "muic-uvlo" },
  158. { RT8973A_INT2_POR, "muic-por" },
  159. { RT8973A_INT2_OTP_FET, "muic-otp-fet" },
  160. { RT8973A_INT2_OVP_FET, "muic-ovp-fet" },
  161. { RT8973A_INT2_OCP_LATCH, "muic-ocp-latch" },
  162. { RT8973A_INT2_OCP, "muic-ocp" },
  163. { RT8973A_INT2_OVP_OCP, "muic-ovp-ocp" },
  164. };
  165. /* Define interrupt list of RT8973A to register regmap_irq */
  166. static const struct regmap_irq rt8973a_irqs[] = {
  167. /* INT1 interrupts */
  168. { .reg_offset = 0, .mask = RT8973A_INT1_ATTACH_MASK, },
  169. { .reg_offset = 0, .mask = RT8973A_INT1_DETACH_MASK, },
  170. { .reg_offset = 0, .mask = RT8973A_INT1_CHGDET_MASK, },
  171. { .reg_offset = 0, .mask = RT8973A_INT1_DCD_T_MASK, },
  172. { .reg_offset = 0, .mask = RT8973A_INT1_OVP_MASK, },
  173. { .reg_offset = 0, .mask = RT8973A_INT1_CONNECT_MASK, },
  174. { .reg_offset = 0, .mask = RT8973A_INT1_ADC_CHG_MASK, },
  175. { .reg_offset = 0, .mask = RT8973A_INT1_OTP_MASK, },
  176. /* INT2 interrupts */
  177. { .reg_offset = 1, .mask = RT8973A_INT2_UVLOT_MASK,},
  178. { .reg_offset = 1, .mask = RT8973A_INT2_POR_MASK, },
  179. { .reg_offset = 1, .mask = RT8973A_INT2_OTP_FET_MASK, },
  180. { .reg_offset = 1, .mask = RT8973A_INT2_OVP_FET_MASK, },
  181. { .reg_offset = 1, .mask = RT8973A_INT2_OCP_LATCH_MASK, },
  182. { .reg_offset = 1, .mask = RT8973A_INT2_OCP_MASK, },
  183. { .reg_offset = 1, .mask = RT8973A_INT2_OVP_OCP_MASK, },
  184. };
  185. static const struct regmap_irq_chip rt8973a_muic_irq_chip = {
  186. .name = "rt8973a",
  187. .status_base = RT8973A_REG_INT1,
  188. .mask_base = RT8973A_REG_INTM1,
  189. .mask_invert = false,
  190. .num_regs = 2,
  191. .irqs = rt8973a_irqs,
  192. .num_irqs = ARRAY_SIZE(rt8973a_irqs),
  193. };
  194. /* Define regmap configuration of RT8973A for I2C communication */
  195. static bool rt8973a_muic_volatile_reg(struct device *dev, unsigned int reg)
  196. {
  197. switch (reg) {
  198. case RT8973A_REG_INTM1:
  199. case RT8973A_REG_INTM2:
  200. return true;
  201. default:
  202. break;
  203. }
  204. return false;
  205. }
  206. static const struct regmap_config rt8973a_muic_regmap_config = {
  207. .reg_bits = 8,
  208. .val_bits = 8,
  209. .volatile_reg = rt8973a_muic_volatile_reg,
  210. .max_register = RT8973A_REG_END,
  211. };
  212. /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
  213. static int rt8973a_muic_set_path(struct rt8973a_muic_info *info,
  214. unsigned int con_sw, bool attached)
  215. {
  216. int ret;
  217. /*
  218. * Don't need to set h/w path according to cable type
  219. * if Auto-configuration mode of CONTROL1 register is true.
  220. */
  221. if (info->auto_config)
  222. return 0;
  223. if (!attached)
  224. con_sw = DM_DP_SWITCH_UART;
  225. switch (con_sw) {
  226. case DM_DP_SWITCH_OPEN:
  227. case DM_DP_SWITCH_USB:
  228. case DM_DP_SWITCH_UART:
  229. ret = regmap_update_bits(info->regmap, RT8973A_REG_MANUAL_SW1,
  230. RT8973A_REG_MANUAL_SW1_DP_MASK |
  231. RT8973A_REG_MANUAL_SW1_DM_MASK,
  232. con_sw);
  233. if (ret < 0) {
  234. dev_err(info->dev,
  235. "cannot update DM_CON/DP_CON switch\n");
  236. return ret;
  237. }
  238. break;
  239. default:
  240. dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
  241. con_sw);
  242. return -EINVAL;
  243. }
  244. return 0;
  245. }
  246. static int rt8973a_muic_get_cable_type(struct rt8973a_muic_info *info)
  247. {
  248. unsigned int adc, dev1;
  249. int ret, cable_type;
  250. /* Read ADC value according to external cable or button */
  251. ret = regmap_read(info->regmap, RT8973A_REG_ADC, &adc);
  252. if (ret) {
  253. dev_err(info->dev, "failed to read ADC register\n");
  254. return ret;
  255. }
  256. cable_type = adc & RT8973A_REG_ADC_MASK;
  257. /* Read Device 1 reigster to identify correct cable type */
  258. ret = regmap_read(info->regmap, RT8973A_REG_DEV1, &dev1);
  259. if (ret) {
  260. dev_err(info->dev, "failed to read DEV1 register\n");
  261. return ret;
  262. }
  263. switch (adc) {
  264. case RT8973A_MUIC_ADC_OPEN:
  265. if (dev1 & RT8973A_REG_DEV1_USB_MASK)
  266. cable_type = RT8973A_MUIC_ADC_USB;
  267. else if (dev1 & RT8973A_REG_DEV1_DCPORT_MASK)
  268. cable_type = RT8973A_MUIC_ADC_TA;
  269. else
  270. cable_type = RT8973A_MUIC_ADC_OPEN;
  271. break;
  272. default:
  273. break;
  274. }
  275. return cable_type;
  276. }
  277. static int rt8973a_muic_cable_handler(struct rt8973a_muic_info *info,
  278. enum rt8973a_event_type event)
  279. {
  280. static unsigned int prev_cable_type;
  281. const char **cable_names = info->edev->supported_cable;
  282. unsigned int con_sw = DM_DP_SWITCH_UART;
  283. int ret, idx = 0, cable_type;
  284. bool attached = false;
  285. if (!cable_names)
  286. return 0;
  287. switch (event) {
  288. case RT8973A_EVENT_ATTACH:
  289. cable_type = rt8973a_muic_get_cable_type(info);
  290. attached = true;
  291. break;
  292. case RT8973A_EVENT_DETACH:
  293. cable_type = prev_cable_type;
  294. attached = false;
  295. break;
  296. case RT8973A_EVENT_OVP:
  297. case RT8973A_EVENT_OTP:
  298. dev_warn(info->dev,
  299. "happen Over %s issue. Need to disconnect all cables\n",
  300. event == RT8973A_EVENT_OVP ? "Voltage" : "Temperature");
  301. cable_type = prev_cable_type;
  302. attached = false;
  303. break;
  304. default:
  305. dev_err(info->dev,
  306. "Cannot handle this event (event:%d)\n", event);
  307. return -EINVAL;
  308. }
  309. prev_cable_type = cable_type;
  310. switch (cable_type) {
  311. case RT8973A_MUIC_ADC_OTG:
  312. idx = EXTCON_CABLE_USB_HOST;
  313. con_sw = DM_DP_SWITCH_USB;
  314. break;
  315. case RT8973A_MUIC_ADC_TA:
  316. idx = EXTCON_CABLE_TA;
  317. con_sw = DM_DP_SWITCH_OPEN;
  318. break;
  319. case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
  320. idx = EXTCON_CABLE_JIG_OFF_USB;
  321. con_sw = DM_DP_SWITCH_UART;
  322. break;
  323. case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
  324. idx = EXTCON_CABLE_JIG_ON_USB;
  325. con_sw = DM_DP_SWITCH_UART;
  326. break;
  327. case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
  328. idx = EXTCON_CABLE_JIG_OFF_UART;
  329. con_sw = DM_DP_SWITCH_UART;
  330. break;
  331. case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
  332. idx = EXTCON_CABLE_JIG_ON_UART;
  333. con_sw = DM_DP_SWITCH_UART;
  334. break;
  335. case RT8973A_MUIC_ADC_USB:
  336. idx = EXTCON_CABLE_USB;
  337. con_sw = DM_DP_SWITCH_USB;
  338. break;
  339. case RT8973A_MUIC_ADC_OPEN:
  340. return 0;
  341. case RT8973A_MUIC_ADC_UNKNOWN_ACC_1:
  342. case RT8973A_MUIC_ADC_UNKNOWN_ACC_2:
  343. case RT8973A_MUIC_ADC_UNKNOWN_ACC_3:
  344. case RT8973A_MUIC_ADC_UNKNOWN_ACC_4:
  345. case RT8973A_MUIC_ADC_UNKNOWN_ACC_5:
  346. dev_warn(info->dev,
  347. "Unknown accessory type (adc:0x%x)\n", cable_type);
  348. return 0;
  349. case RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON:
  350. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON:
  351. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON:
  352. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON:
  353. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON:
  354. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON:
  355. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON:
  356. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON:
  357. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON:
  358. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON:
  359. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON:
  360. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON:
  361. case RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON:
  362. case RT8973A_MUIC_ADC_AUDIO_TYPE2:
  363. dev_warn(info->dev,
  364. "Audio device/button type (adc:0x%x)\n", cable_type);
  365. return 0;
  366. case RT8973A_MUIC_ADC_RESERVED_ACC_1:
  367. case RT8973A_MUIC_ADC_RESERVED_ACC_2:
  368. case RT8973A_MUIC_ADC_RESERVED_ACC_3:
  369. case RT8973A_MUIC_ADC_RESERVED_ACC_4:
  370. case RT8973A_MUIC_ADC_RESERVED_ACC_5:
  371. case RT8973A_MUIC_ADC_PHONE_POWERED_DEV:
  372. return 0;
  373. default:
  374. dev_err(info->dev,
  375. "Cannot handle this cable_type (adc:0x%x)\n",
  376. cable_type);
  377. return -EINVAL;
  378. }
  379. /* Change internal hardware path(DM_CON/DP_CON) */
  380. ret = rt8973a_muic_set_path(info, con_sw, attached);
  381. if (ret < 0)
  382. return ret;
  383. /* Change the state of external accessory */
  384. extcon_set_cable_state(info->edev, cable_names[idx], attached);
  385. return 0;
  386. }
  387. static void rt8973a_muic_irq_work(struct work_struct *work)
  388. {
  389. struct rt8973a_muic_info *info = container_of(work,
  390. struct rt8973a_muic_info, irq_work);
  391. int ret = 0;
  392. if (!info->edev)
  393. return;
  394. mutex_lock(&info->mutex);
  395. /* Detect attached or detached cables */
  396. if (info->irq_attach) {
  397. ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_ATTACH);
  398. info->irq_attach = false;
  399. }
  400. if (info->irq_detach) {
  401. ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_DETACH);
  402. info->irq_detach = false;
  403. }
  404. if (info->irq_ovp) {
  405. ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_OVP);
  406. info->irq_ovp = false;
  407. }
  408. if (info->irq_otp) {
  409. ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_OTP);
  410. info->irq_otp = false;
  411. }
  412. if (ret < 0)
  413. dev_err(info->dev, "failed to handle MUIC interrupt\n");
  414. mutex_unlock(&info->mutex);
  415. }
  416. static irqreturn_t rt8973a_muic_irq_handler(int irq, void *data)
  417. {
  418. struct rt8973a_muic_info *info = data;
  419. int i, irq_type = -1;
  420. for (i = 0; i < info->num_muic_irqs; i++)
  421. if (irq == info->muic_irqs[i].virq)
  422. irq_type = info->muic_irqs[i].irq;
  423. switch (irq_type) {
  424. case RT8973A_INT1_ATTACH:
  425. info->irq_attach = true;
  426. break;
  427. case RT8973A_INT1_DETACH:
  428. info->irq_detach = true;
  429. break;
  430. case RT8973A_INT1_OVP:
  431. info->irq_ovp = true;
  432. break;
  433. case RT8973A_INT1_OTP:
  434. info->irq_otp = true;
  435. break;
  436. case RT8973A_INT1_CHGDET:
  437. case RT8973A_INT1_DCD_T:
  438. case RT8973A_INT1_CONNECT:
  439. case RT8973A_INT1_ADC_CHG:
  440. case RT8973A_INT2_UVLO:
  441. case RT8973A_INT2_POR:
  442. case RT8973A_INT2_OTP_FET:
  443. case RT8973A_INT2_OVP_FET:
  444. case RT8973A_INT2_OCP_LATCH:
  445. case RT8973A_INT2_OCP:
  446. case RT8973A_INT2_OVP_OCP:
  447. default:
  448. dev_dbg(info->dev,
  449. "Cannot handle this interrupt (%d)\n", irq_type);
  450. break;
  451. }
  452. schedule_work(&info->irq_work);
  453. return IRQ_HANDLED;
  454. }
  455. static void rt8973a_muic_detect_cable_wq(struct work_struct *work)
  456. {
  457. struct rt8973a_muic_info *info = container_of(to_delayed_work(work),
  458. struct rt8973a_muic_info, wq_detcable);
  459. int ret;
  460. /* Notify the state of connector cable or not */
  461. ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_ATTACH);
  462. if (ret < 0)
  463. dev_warn(info->dev, "failed to detect cable state\n");
  464. }
  465. static void rt8973a_init_dev_type(struct rt8973a_muic_info *info)
  466. {
  467. unsigned int data, vendor_id, version_id;
  468. int i, ret;
  469. /* To test I2C, Print version_id and vendor_id of RT8973A */
  470. ret = regmap_read(info->regmap, RT8973A_REG_DEVICE_ID, &data);
  471. if (ret) {
  472. dev_err(info->dev,
  473. "failed to read DEVICE_ID register: %d\n", ret);
  474. return;
  475. }
  476. vendor_id = ((data & RT8973A_REG_DEVICE_ID_VENDOR_MASK) >>
  477. RT8973A_REG_DEVICE_ID_VENDOR_SHIFT);
  478. version_id = ((data & RT8973A_REG_DEVICE_ID_VERSION_MASK) >>
  479. RT8973A_REG_DEVICE_ID_VERSION_SHIFT);
  480. dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
  481. version_id, vendor_id);
  482. /* Initiazle the register of RT8973A device to bring-up */
  483. for (i = 0; i < info->num_reg_data; i++) {
  484. u8 reg = info->reg_data[i].reg;
  485. u8 mask = info->reg_data[i].mask;
  486. u8 val = 0;
  487. if (info->reg_data[i].invert)
  488. val = ~info->reg_data[i].val;
  489. else
  490. val = info->reg_data[i].val;
  491. regmap_update_bits(info->regmap, reg, mask, val);
  492. }
  493. /* Check whether RT8973A is auto swithcing mode or not */
  494. ret = regmap_read(info->regmap, RT8973A_REG_CONTROL1, &data);
  495. if (ret) {
  496. dev_err(info->dev,
  497. "failed to read CONTROL1 register: %d\n", ret);
  498. return;
  499. }
  500. data &= RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK;
  501. if (data) {
  502. info->auto_config = true;
  503. dev_info(info->dev,
  504. "Enable Auto-configuration for internal path\n");
  505. }
  506. }
  507. static int rt8973a_muic_i2c_probe(struct i2c_client *i2c,
  508. const struct i2c_device_id *id)
  509. {
  510. struct device_node *np = i2c->dev.of_node;
  511. struct rt8973a_muic_info *info;
  512. int i, ret, irq_flags;
  513. if (!np)
  514. return -EINVAL;
  515. info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
  516. if (!info)
  517. return -ENOMEM;
  518. i2c_set_clientdata(i2c, info);
  519. info->dev = &i2c->dev;
  520. info->i2c = i2c;
  521. info->irq = i2c->irq;
  522. info->muic_irqs = rt8973a_muic_irqs;
  523. info->num_muic_irqs = ARRAY_SIZE(rt8973a_muic_irqs);
  524. info->reg_data = rt8973a_reg_data;
  525. info->num_reg_data = ARRAY_SIZE(rt8973a_reg_data);
  526. mutex_init(&info->mutex);
  527. INIT_WORK(&info->irq_work, rt8973a_muic_irq_work);
  528. info->regmap = devm_regmap_init_i2c(i2c, &rt8973a_muic_regmap_config);
  529. if (IS_ERR(info->regmap)) {
  530. ret = PTR_ERR(info->regmap);
  531. dev_err(info->dev, "failed to allocate register map: %d\n",
  532. ret);
  533. return ret;
  534. }
  535. /* Support irq domain for RT8973A MUIC device */
  536. irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
  537. ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0,
  538. &rt8973a_muic_irq_chip, &info->irq_data);
  539. if (ret != 0) {
  540. dev_err(info->dev, "failed to add irq_chip (irq:%d, err:%d)\n",
  541. info->irq, ret);
  542. return ret;
  543. }
  544. for (i = 0; i < info->num_muic_irqs; i++) {
  545. struct muic_irq *muic_irq = &info->muic_irqs[i];
  546. unsigned int virq = 0;
  547. virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
  548. if (virq <= 0)
  549. return -EINVAL;
  550. muic_irq->virq = virq;
  551. ret = devm_request_threaded_irq(info->dev, virq, NULL,
  552. rt8973a_muic_irq_handler,
  553. IRQF_NO_SUSPEND,
  554. muic_irq->name, info);
  555. if (ret) {
  556. dev_err(info->dev,
  557. "failed: irq request (IRQ: %d, error :%d)\n",
  558. muic_irq->irq, ret);
  559. return ret;
  560. }
  561. }
  562. /* Allocate extcon device */
  563. info->edev = devm_extcon_dev_allocate(info->dev, rt8973a_extcon_cable);
  564. if (IS_ERR(info->edev)) {
  565. dev_err(info->dev, "failed to allocate memory for extcon\n");
  566. return -ENOMEM;
  567. }
  568. info->edev->name = np->name;
  569. /* Register extcon device */
  570. ret = devm_extcon_dev_register(info->dev, info->edev);
  571. if (ret) {
  572. dev_err(info->dev, "failed to register extcon device\n");
  573. return ret;
  574. }
  575. /*
  576. * Detect accessory after completing the initialization of platform
  577. *
  578. * - Use delayed workqueue to detect cable state and then
  579. * notify cable state to notifiee/platform through uevent.
  580. * After completing the booting of platform, the extcon provider
  581. * driver should notify cable state to upper layer.
  582. */
  583. INIT_DELAYED_WORK(&info->wq_detcable, rt8973a_muic_detect_cable_wq);
  584. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  585. msecs_to_jiffies(DELAY_MS_DEFAULT));
  586. /* Initialize RT8973A device and print vendor id and version id */
  587. rt8973a_init_dev_type(info);
  588. return 0;
  589. }
  590. static int rt8973a_muic_i2c_remove(struct i2c_client *i2c)
  591. {
  592. struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
  593. regmap_del_irq_chip(info->irq, info->irq_data);
  594. return 0;
  595. }
  596. static const struct of_device_id rt8973a_dt_match[] = {
  597. { .compatible = "richtek,rt8973a-muic" },
  598. { },
  599. };
  600. #ifdef CONFIG_PM_SLEEP
  601. static int rt8973a_muic_suspend(struct device *dev)
  602. {
  603. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  604. struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
  605. enable_irq_wake(info->irq);
  606. return 0;
  607. }
  608. static int rt8973a_muic_resume(struct device *dev)
  609. {
  610. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  611. struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
  612. disable_irq_wake(info->irq);
  613. return 0;
  614. }
  615. #endif
  616. static SIMPLE_DEV_PM_OPS(rt8973a_muic_pm_ops,
  617. rt8973a_muic_suspend, rt8973a_muic_resume);
  618. static const struct i2c_device_id rt8973a_i2c_id[] = {
  619. { "rt8973a", TYPE_RT8973A },
  620. { }
  621. };
  622. MODULE_DEVICE_TABLE(i2c, rt8973a_i2c_id);
  623. static struct i2c_driver rt8973a_muic_i2c_driver = {
  624. .driver = {
  625. .name = "rt8973a",
  626. .owner = THIS_MODULE,
  627. .pm = &rt8973a_muic_pm_ops,
  628. .of_match_table = rt8973a_dt_match,
  629. },
  630. .probe = rt8973a_muic_i2c_probe,
  631. .remove = rt8973a_muic_i2c_remove,
  632. .id_table = rt8973a_i2c_id,
  633. };
  634. static int __init rt8973a_muic_i2c_init(void)
  635. {
  636. return i2c_add_driver(&rt8973a_muic_i2c_driver);
  637. }
  638. subsys_initcall(rt8973a_muic_i2c_init);
  639. MODULE_DESCRIPTION("Richtek RT8973A Extcon driver");
  640. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  641. MODULE_LICENSE("GPL");