extcon-rt8973a.c 19 KB

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