extcon-sm5502.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * extcon-sm5502.c - Silicon Mitus SM5502 extcon drvier 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/interrupt.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/slab.h>
  21. #include <linux/extcon-provider.h>
  22. #include "extcon-sm5502.h"
  23. #define DELAY_MS_DEFAULT 17000 /* unit: millisecond */
  24. struct muic_irq {
  25. unsigned int irq;
  26. const char *name;
  27. unsigned int virq;
  28. };
  29. struct reg_data {
  30. u8 reg;
  31. unsigned int val;
  32. bool invert;
  33. };
  34. struct sm5502_muic_info {
  35. struct device *dev;
  36. struct extcon_dev *edev;
  37. struct i2c_client *i2c;
  38. struct regmap *regmap;
  39. struct regmap_irq_chip_data *irq_data;
  40. struct muic_irq *muic_irqs;
  41. unsigned int num_muic_irqs;
  42. int irq;
  43. bool irq_attach;
  44. bool irq_detach;
  45. struct work_struct irq_work;
  46. struct reg_data *reg_data;
  47. unsigned int num_reg_data;
  48. struct mutex mutex;
  49. /*
  50. * Use delayed workqueue to detect cable state and then
  51. * notify cable state to notifiee/platform through uevent.
  52. * After completing the booting of platform, the extcon provider
  53. * driver should notify cable state to upper layer.
  54. */
  55. struct delayed_work wq_detcable;
  56. };
  57. /* Default value of SM5502 register to bring up MUIC device. */
  58. static struct reg_data sm5502_reg_data[] = {
  59. {
  60. .reg = SM5502_REG_RESET,
  61. .val = SM5502_REG_RESET_MASK,
  62. .invert = true,
  63. }, {
  64. .reg = SM5502_REG_CONTROL,
  65. .val = SM5502_REG_CONTROL_MASK_INT_MASK,
  66. .invert = false,
  67. }, {
  68. .reg = SM5502_REG_INTMASK1,
  69. .val = SM5502_REG_INTM1_KP_MASK
  70. | SM5502_REG_INTM1_LKP_MASK
  71. | SM5502_REG_INTM1_LKR_MASK,
  72. .invert = true,
  73. }, {
  74. .reg = SM5502_REG_INTMASK2,
  75. .val = SM5502_REG_INTM2_VBUS_DET_MASK
  76. | SM5502_REG_INTM2_REV_ACCE_MASK
  77. | SM5502_REG_INTM2_ADC_CHG_MASK
  78. | SM5502_REG_INTM2_STUCK_KEY_MASK
  79. | SM5502_REG_INTM2_STUCK_KEY_RCV_MASK
  80. | SM5502_REG_INTM2_MHL_MASK,
  81. .invert = true,
  82. },
  83. { }
  84. };
  85. /* List of detectable cables */
  86. static const unsigned int sm5502_extcon_cable[] = {
  87. EXTCON_USB,
  88. EXTCON_USB_HOST,
  89. EXTCON_CHG_USB_SDP,
  90. EXTCON_CHG_USB_DCP,
  91. EXTCON_NONE,
  92. };
  93. /* Define supported accessory type */
  94. enum sm5502_muic_acc_type {
  95. SM5502_MUIC_ADC_GROUND = 0x0,
  96. SM5502_MUIC_ADC_SEND_END_BUTTON,
  97. SM5502_MUIC_ADC_REMOTE_S1_BUTTON,
  98. SM5502_MUIC_ADC_REMOTE_S2_BUTTON,
  99. SM5502_MUIC_ADC_REMOTE_S3_BUTTON,
  100. SM5502_MUIC_ADC_REMOTE_S4_BUTTON,
  101. SM5502_MUIC_ADC_REMOTE_S5_BUTTON,
  102. SM5502_MUIC_ADC_REMOTE_S6_BUTTON,
  103. SM5502_MUIC_ADC_REMOTE_S7_BUTTON,
  104. SM5502_MUIC_ADC_REMOTE_S8_BUTTON,
  105. SM5502_MUIC_ADC_REMOTE_S9_BUTTON,
  106. SM5502_MUIC_ADC_REMOTE_S10_BUTTON,
  107. SM5502_MUIC_ADC_REMOTE_S11_BUTTON,
  108. SM5502_MUIC_ADC_REMOTE_S12_BUTTON,
  109. SM5502_MUIC_ADC_RESERVED_ACC_1,
  110. SM5502_MUIC_ADC_RESERVED_ACC_2,
  111. SM5502_MUIC_ADC_RESERVED_ACC_3,
  112. SM5502_MUIC_ADC_RESERVED_ACC_4,
  113. SM5502_MUIC_ADC_RESERVED_ACC_5,
  114. SM5502_MUIC_ADC_AUDIO_TYPE2,
  115. SM5502_MUIC_ADC_PHONE_POWERED_DEV,
  116. SM5502_MUIC_ADC_TTY_CONVERTER,
  117. SM5502_MUIC_ADC_UART_CABLE,
  118. SM5502_MUIC_ADC_TYPE1_CHARGER,
  119. SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
  120. SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
  121. SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE,
  122. SM5502_MUIC_ADC_TYPE2_CHARGER,
  123. SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
  124. SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
  125. SM5502_MUIC_ADC_AUDIO_TYPE1,
  126. SM5502_MUIC_ADC_OPEN = 0x1f,
  127. /*
  128. * The below accessories have same ADC value (0x1f or 0x1e).
  129. * So, Device type1 is used to separate specific accessory.
  130. */
  131. /* |---------|--ADC| */
  132. /* | [7:5]|[4:0]| */
  133. SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* | 001|11110| */
  134. SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e, /* | 010|11110| */
  135. /* |Dev Type1|--ADC| */
  136. SM5502_MUIC_ADC_OPEN_USB = 0x5f, /* | 010|11111| */
  137. SM5502_MUIC_ADC_OPEN_TA = 0xdf, /* | 110|11111| */
  138. SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff, /* | 111|11111| */
  139. };
  140. /* List of supported interrupt for SM5502 */
  141. static struct muic_irq sm5502_muic_irqs[] = {
  142. { SM5502_IRQ_INT1_ATTACH, "muic-attach" },
  143. { SM5502_IRQ_INT1_DETACH, "muic-detach" },
  144. { SM5502_IRQ_INT1_KP, "muic-kp" },
  145. { SM5502_IRQ_INT1_LKP, "muic-lkp" },
  146. { SM5502_IRQ_INT1_LKR, "muic-lkr" },
  147. { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" },
  148. { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" },
  149. { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" },
  150. { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" },
  151. { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" },
  152. { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" },
  153. { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" },
  154. { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" },
  155. { SM5502_IRQ_INT2_MHL, "muic-mhl" },
  156. };
  157. /* Define interrupt list of SM5502 to register regmap_irq */
  158. static const struct regmap_irq sm5502_irqs[] = {
  159. /* INT1 interrupts */
  160. { .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, },
  161. { .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, },
  162. { .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, },
  163. { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, },
  164. { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, },
  165. { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, },
  166. { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, },
  167. { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, },
  168. /* INT2 interrupts */
  169. { .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,},
  170. { .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, },
  171. { .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, },
  172. { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, },
  173. { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, },
  174. { .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, },
  175. };
  176. static const struct regmap_irq_chip sm5502_muic_irq_chip = {
  177. .name = "sm5502",
  178. .status_base = SM5502_REG_INT1,
  179. .mask_base = SM5502_REG_INTMASK1,
  180. .mask_invert = false,
  181. .num_regs = 2,
  182. .irqs = sm5502_irqs,
  183. .num_irqs = ARRAY_SIZE(sm5502_irqs),
  184. };
  185. /* Define regmap configuration of SM5502 for I2C communication */
  186. static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg)
  187. {
  188. switch (reg) {
  189. case SM5502_REG_INTMASK1:
  190. case SM5502_REG_INTMASK2:
  191. return true;
  192. default:
  193. break;
  194. }
  195. return false;
  196. }
  197. static const struct regmap_config sm5502_muic_regmap_config = {
  198. .reg_bits = 8,
  199. .val_bits = 8,
  200. .volatile_reg = sm5502_muic_volatile_reg,
  201. .max_register = SM5502_REG_END,
  202. };
  203. /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
  204. static int sm5502_muic_set_path(struct sm5502_muic_info *info,
  205. unsigned int con_sw, unsigned int vbus_sw,
  206. bool attached)
  207. {
  208. int ret;
  209. if (!attached) {
  210. con_sw = DM_DP_SWITCH_OPEN;
  211. vbus_sw = VBUSIN_SWITCH_OPEN;
  212. }
  213. switch (con_sw) {
  214. case DM_DP_SWITCH_OPEN:
  215. case DM_DP_SWITCH_USB:
  216. case DM_DP_SWITCH_AUDIO:
  217. case DM_DP_SWITCH_UART:
  218. ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
  219. SM5502_REG_MANUAL_SW1_DP_MASK |
  220. SM5502_REG_MANUAL_SW1_DM_MASK,
  221. con_sw);
  222. if (ret < 0) {
  223. dev_err(info->dev,
  224. "cannot update DM_CON/DP_CON switch\n");
  225. return ret;
  226. }
  227. break;
  228. default:
  229. dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
  230. con_sw);
  231. return -EINVAL;
  232. };
  233. switch (vbus_sw) {
  234. case VBUSIN_SWITCH_OPEN:
  235. case VBUSIN_SWITCH_VBUSOUT:
  236. case VBUSIN_SWITCH_MIC:
  237. case VBUSIN_SWITCH_VBUSOUT_WITH_USB:
  238. ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
  239. SM5502_REG_MANUAL_SW1_VBUSIN_MASK,
  240. vbus_sw);
  241. if (ret < 0) {
  242. dev_err(info->dev,
  243. "cannot update VBUSIN switch\n");
  244. return ret;
  245. }
  246. break;
  247. default:
  248. dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw);
  249. return -EINVAL;
  250. };
  251. return 0;
  252. }
  253. /* Return cable type of attached or detached accessories */
  254. static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info)
  255. {
  256. unsigned int cable_type = -1, adc, dev_type1;
  257. int ret;
  258. /* Read ADC value according to external cable or button */
  259. ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc);
  260. if (ret) {
  261. dev_err(info->dev, "failed to read ADC register\n");
  262. return ret;
  263. }
  264. /*
  265. * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't
  266. * connected with to MUIC device.
  267. */
  268. cable_type = adc & SM5502_REG_ADC_MASK;
  269. if (cable_type == SM5502_MUIC_ADC_GROUND)
  270. return SM5502_MUIC_ADC_GROUND;
  271. switch (cable_type) {
  272. case SM5502_MUIC_ADC_GROUND:
  273. case SM5502_MUIC_ADC_SEND_END_BUTTON:
  274. case SM5502_MUIC_ADC_REMOTE_S1_BUTTON:
  275. case SM5502_MUIC_ADC_REMOTE_S2_BUTTON:
  276. case SM5502_MUIC_ADC_REMOTE_S3_BUTTON:
  277. case SM5502_MUIC_ADC_REMOTE_S4_BUTTON:
  278. case SM5502_MUIC_ADC_REMOTE_S5_BUTTON:
  279. case SM5502_MUIC_ADC_REMOTE_S6_BUTTON:
  280. case SM5502_MUIC_ADC_REMOTE_S7_BUTTON:
  281. case SM5502_MUIC_ADC_REMOTE_S8_BUTTON:
  282. case SM5502_MUIC_ADC_REMOTE_S9_BUTTON:
  283. case SM5502_MUIC_ADC_REMOTE_S10_BUTTON:
  284. case SM5502_MUIC_ADC_REMOTE_S11_BUTTON:
  285. case SM5502_MUIC_ADC_REMOTE_S12_BUTTON:
  286. case SM5502_MUIC_ADC_RESERVED_ACC_1:
  287. case SM5502_MUIC_ADC_RESERVED_ACC_2:
  288. case SM5502_MUIC_ADC_RESERVED_ACC_3:
  289. case SM5502_MUIC_ADC_RESERVED_ACC_4:
  290. case SM5502_MUIC_ADC_RESERVED_ACC_5:
  291. case SM5502_MUIC_ADC_AUDIO_TYPE2:
  292. case SM5502_MUIC_ADC_PHONE_POWERED_DEV:
  293. case SM5502_MUIC_ADC_TTY_CONVERTER:
  294. case SM5502_MUIC_ADC_UART_CABLE:
  295. case SM5502_MUIC_ADC_TYPE1_CHARGER:
  296. case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
  297. case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
  298. case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE:
  299. case SM5502_MUIC_ADC_TYPE2_CHARGER:
  300. case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
  301. case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
  302. break;
  303. case SM5502_MUIC_ADC_AUDIO_TYPE1:
  304. /*
  305. * Check whether cable type is
  306. * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE
  307. * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END
  308. * by using Button event.
  309. */
  310. break;
  311. case SM5502_MUIC_ADC_OPEN:
  312. ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
  313. &dev_type1);
  314. if (ret) {
  315. dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
  316. return ret;
  317. }
  318. switch (dev_type1) {
  319. case SM5502_REG_DEV_TYPE1_USB_SDP_MASK:
  320. cable_type = SM5502_MUIC_ADC_OPEN_USB;
  321. break;
  322. case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK:
  323. cable_type = SM5502_MUIC_ADC_OPEN_TA;
  324. break;
  325. case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
  326. cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG;
  327. break;
  328. default:
  329. dev_dbg(info->dev,
  330. "cannot identify the cable type: adc(0x%x)\n",
  331. adc);
  332. return -EINVAL;
  333. };
  334. break;
  335. default:
  336. dev_err(info->dev,
  337. "failed to identify the cable type: adc(0x%x)\n", adc);
  338. return -EINVAL;
  339. };
  340. return cable_type;
  341. }
  342. static int sm5502_muic_cable_handler(struct sm5502_muic_info *info,
  343. bool attached)
  344. {
  345. static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND;
  346. unsigned int cable_type = SM5502_MUIC_ADC_GROUND;
  347. unsigned int con_sw = DM_DP_SWITCH_OPEN;
  348. unsigned int vbus_sw = VBUSIN_SWITCH_OPEN;
  349. unsigned int id;
  350. int ret;
  351. /* Get the type of attached or detached cable */
  352. if (attached)
  353. cable_type = sm5502_muic_get_cable_type(info);
  354. else
  355. cable_type = prev_cable_type;
  356. prev_cable_type = cable_type;
  357. switch (cable_type) {
  358. case SM5502_MUIC_ADC_OPEN_USB:
  359. id = EXTCON_USB;
  360. con_sw = DM_DP_SWITCH_USB;
  361. vbus_sw = VBUSIN_SWITCH_VBUSOUT_WITH_USB;
  362. break;
  363. case SM5502_MUIC_ADC_OPEN_TA:
  364. id = EXTCON_CHG_USB_DCP;
  365. con_sw = DM_DP_SWITCH_OPEN;
  366. vbus_sw = VBUSIN_SWITCH_VBUSOUT;
  367. break;
  368. case SM5502_MUIC_ADC_OPEN_USB_OTG:
  369. id = EXTCON_USB_HOST;
  370. con_sw = DM_DP_SWITCH_USB;
  371. vbus_sw = VBUSIN_SWITCH_OPEN;
  372. break;
  373. default:
  374. dev_dbg(info->dev,
  375. "cannot handle this cable_type (0x%x)\n", cable_type);
  376. return 0;
  377. };
  378. /* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */
  379. ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached);
  380. if (ret < 0)
  381. return ret;
  382. /* Change the state of external accessory */
  383. extcon_set_state_sync(info->edev, id, attached);
  384. if (id == EXTCON_USB)
  385. extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
  386. attached);
  387. return 0;
  388. }
  389. static void sm5502_muic_irq_work(struct work_struct *work)
  390. {
  391. struct sm5502_muic_info *info = container_of(work,
  392. struct sm5502_muic_info, irq_work);
  393. int ret = 0;
  394. if (!info->edev)
  395. return;
  396. mutex_lock(&info->mutex);
  397. /* Detect attached or detached cables */
  398. if (info->irq_attach) {
  399. ret = sm5502_muic_cable_handler(info, true);
  400. info->irq_attach = false;
  401. }
  402. if (info->irq_detach) {
  403. ret = sm5502_muic_cable_handler(info, false);
  404. info->irq_detach = false;
  405. }
  406. if (ret < 0)
  407. dev_err(info->dev, "failed to handle MUIC interrupt\n");
  408. mutex_unlock(&info->mutex);
  409. }
  410. /*
  411. * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0.
  412. * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
  413. */
  414. static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type)
  415. {
  416. switch (irq_type) {
  417. case SM5502_IRQ_INT1_ATTACH:
  418. info->irq_attach = true;
  419. break;
  420. case SM5502_IRQ_INT1_DETACH:
  421. info->irq_detach = true;
  422. break;
  423. case SM5502_IRQ_INT1_KP:
  424. case SM5502_IRQ_INT1_LKP:
  425. case SM5502_IRQ_INT1_LKR:
  426. case SM5502_IRQ_INT1_OVP_EVENT:
  427. case SM5502_IRQ_INT1_OCP_EVENT:
  428. case SM5502_IRQ_INT1_OVP_OCP_DIS:
  429. case SM5502_IRQ_INT2_VBUS_DET:
  430. case SM5502_IRQ_INT2_REV_ACCE:
  431. case SM5502_IRQ_INT2_ADC_CHG:
  432. case SM5502_IRQ_INT2_STUCK_KEY:
  433. case SM5502_IRQ_INT2_STUCK_KEY_RCV:
  434. case SM5502_IRQ_INT2_MHL:
  435. default:
  436. break;
  437. }
  438. return 0;
  439. }
  440. static irqreturn_t sm5502_muic_irq_handler(int irq, void *data)
  441. {
  442. struct sm5502_muic_info *info = data;
  443. int i, irq_type = -1, ret;
  444. for (i = 0; i < info->num_muic_irqs; i++)
  445. if (irq == info->muic_irqs[i].virq)
  446. irq_type = info->muic_irqs[i].irq;
  447. ret = sm5502_parse_irq(info, irq_type);
  448. if (ret < 0) {
  449. dev_warn(info->dev, "cannot handle is interrupt:%d\n",
  450. irq_type);
  451. return IRQ_HANDLED;
  452. }
  453. schedule_work(&info->irq_work);
  454. return IRQ_HANDLED;
  455. }
  456. static void sm5502_muic_detect_cable_wq(struct work_struct *work)
  457. {
  458. struct sm5502_muic_info *info = container_of(to_delayed_work(work),
  459. struct sm5502_muic_info, wq_detcable);
  460. int ret;
  461. /* Notify the state of connector cable or not */
  462. ret = sm5502_muic_cable_handler(info, true);
  463. if (ret < 0)
  464. dev_warn(info->dev, "failed to detect cable state\n");
  465. }
  466. static void sm5502_init_dev_type(struct sm5502_muic_info *info)
  467. {
  468. unsigned int reg_data, vendor_id, version_id;
  469. int i, ret;
  470. /* To test I2C, Print version_id and vendor_id of SM5502 */
  471. ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, &reg_data);
  472. if (ret) {
  473. dev_err(info->dev,
  474. "failed to read DEVICE_ID register: %d\n", ret);
  475. return;
  476. }
  477. vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >>
  478. SM5502_REG_DEVICE_ID_VENDOR_SHIFT);
  479. version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >>
  480. SM5502_REG_DEVICE_ID_VERSION_SHIFT);
  481. dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
  482. version_id, vendor_id);
  483. /* Initiazle the register of SM5502 device to bring-up */
  484. for (i = 0; i < info->num_reg_data; i++) {
  485. unsigned int val = 0;
  486. if (!info->reg_data[i].invert)
  487. val |= ~info->reg_data[i].val;
  488. else
  489. val = info->reg_data[i].val;
  490. regmap_write(info->regmap, info->reg_data[i].reg, val);
  491. }
  492. }
  493. static int sm5022_muic_i2c_probe(struct i2c_client *i2c,
  494. const struct i2c_device_id *id)
  495. {
  496. struct device_node *np = i2c->dev.of_node;
  497. struct sm5502_muic_info *info;
  498. int i, ret, irq_flags;
  499. if (!np)
  500. return -EINVAL;
  501. info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
  502. if (!info)
  503. return -ENOMEM;
  504. i2c_set_clientdata(i2c, info);
  505. info->dev = &i2c->dev;
  506. info->i2c = i2c;
  507. info->irq = i2c->irq;
  508. info->muic_irqs = sm5502_muic_irqs;
  509. info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs);
  510. info->reg_data = sm5502_reg_data;
  511. info->num_reg_data = ARRAY_SIZE(sm5502_reg_data);
  512. mutex_init(&info->mutex);
  513. INIT_WORK(&info->irq_work, sm5502_muic_irq_work);
  514. info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config);
  515. if (IS_ERR(info->regmap)) {
  516. ret = PTR_ERR(info->regmap);
  517. dev_err(info->dev, "failed to allocate register map: %d\n",
  518. ret);
  519. return ret;
  520. }
  521. /* Support irq domain for SM5502 MUIC device */
  522. irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
  523. ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0,
  524. &sm5502_muic_irq_chip, &info->irq_data);
  525. if (ret != 0) {
  526. dev_err(info->dev, "failed to request IRQ %d: %d\n",
  527. info->irq, ret);
  528. return ret;
  529. }
  530. for (i = 0; i < info->num_muic_irqs; i++) {
  531. struct muic_irq *muic_irq = &info->muic_irqs[i];
  532. int virq = 0;
  533. virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
  534. if (virq <= 0)
  535. return -EINVAL;
  536. muic_irq->virq = virq;
  537. ret = devm_request_threaded_irq(info->dev, virq, NULL,
  538. sm5502_muic_irq_handler,
  539. IRQF_NO_SUSPEND,
  540. muic_irq->name, info);
  541. if (ret) {
  542. dev_err(info->dev,
  543. "failed: irq request (IRQ: %d, error :%d)\n",
  544. muic_irq->irq, ret);
  545. return ret;
  546. }
  547. }
  548. /* Allocate extcon device */
  549. info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable);
  550. if (IS_ERR(info->edev)) {
  551. dev_err(info->dev, "failed to allocate memory for extcon\n");
  552. return -ENOMEM;
  553. }
  554. /* Register extcon device */
  555. ret = devm_extcon_dev_register(info->dev, info->edev);
  556. if (ret) {
  557. dev_err(info->dev, "failed to register extcon device\n");
  558. return ret;
  559. }
  560. /*
  561. * Detect accessory after completing the initialization of platform
  562. *
  563. * - Use delayed workqueue to detect cable state and then
  564. * notify cable state to notifiee/platform through uevent.
  565. * After completing the booting of platform, the extcon provider
  566. * driver should notify cable state to upper layer.
  567. */
  568. INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
  569. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  570. msecs_to_jiffies(DELAY_MS_DEFAULT));
  571. /* Initialize SM5502 device and print vendor id and version id */
  572. sm5502_init_dev_type(info);
  573. return 0;
  574. }
  575. static int sm5502_muic_i2c_remove(struct i2c_client *i2c)
  576. {
  577. struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
  578. regmap_del_irq_chip(info->irq, info->irq_data);
  579. return 0;
  580. }
  581. static const struct of_device_id sm5502_dt_match[] = {
  582. { .compatible = "siliconmitus,sm5502-muic" },
  583. { },
  584. };
  585. MODULE_DEVICE_TABLE(of, sm5502_dt_match);
  586. #ifdef CONFIG_PM_SLEEP
  587. static int sm5502_muic_suspend(struct device *dev)
  588. {
  589. struct i2c_client *i2c = to_i2c_client(dev);
  590. struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
  591. enable_irq_wake(info->irq);
  592. return 0;
  593. }
  594. static int sm5502_muic_resume(struct device *dev)
  595. {
  596. struct i2c_client *i2c = to_i2c_client(dev);
  597. struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
  598. disable_irq_wake(info->irq);
  599. return 0;
  600. }
  601. #endif
  602. static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops,
  603. sm5502_muic_suspend, sm5502_muic_resume);
  604. static const struct i2c_device_id sm5502_i2c_id[] = {
  605. { "sm5502", TYPE_SM5502 },
  606. { }
  607. };
  608. MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id);
  609. static struct i2c_driver sm5502_muic_i2c_driver = {
  610. .driver = {
  611. .name = "sm5502",
  612. .pm = &sm5502_muic_pm_ops,
  613. .of_match_table = sm5502_dt_match,
  614. },
  615. .probe = sm5022_muic_i2c_probe,
  616. .remove = sm5502_muic_i2c_remove,
  617. .id_table = sm5502_i2c_id,
  618. };
  619. static int __init sm5502_muic_i2c_init(void)
  620. {
  621. return i2c_add_driver(&sm5502_muic_i2c_driver);
  622. }
  623. subsys_initcall(sm5502_muic_i2c_init);
  624. MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver");
  625. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  626. MODULE_LICENSE("GPL");