extcon-rt8973a.c 19 KB

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