extcon-sm5502.c 19 KB

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