blob: 9f40bb9f1f81ddcaa9473a862b02795ff9b82a57 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Chanwoo Choi914b8812014-05-22 14:06:33 +09002/*
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>
Chanwoo Choi914b8812014-05-22 14:06:33 +09007 */
8
9#include <linux/err.h>
10#include <linux/i2c.h>
Chanwoo Choi914b8812014-05-22 14:06:33 +090011#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>
Chanwoo Choi176aa362017-09-21 12:11:24 +090018#include <linux/extcon-provider.h>
Chanwoo Choica2a07e2014-07-31 16:32:46 +090019
20#include "extcon-sm5502.h"
Chanwoo Choi914b8812014-05-22 14:06:33 +090021
Chanwoo Choie1954452014-05-28 14:21:55 +090022#define DELAY_MS_DEFAULT 17000 /* unit: millisecond */
23
Chanwoo Choi914b8812014-05-22 14:06:33 +090024struct muic_irq {
25 unsigned int irq;
26 const char *name;
27 unsigned int virq;
28};
29
30struct reg_data {
31 u8 reg;
32 unsigned int val;
33 bool invert;
34};
35
36struct sm5502_muic_info {
37 struct device *dev;
38 struct extcon_dev *edev;
39
40 struct i2c_client *i2c;
41 struct regmap *regmap;
42
43 struct regmap_irq_chip_data *irq_data;
44 struct muic_irq *muic_irqs;
45 unsigned int num_muic_irqs;
46 int irq;
47 bool irq_attach;
48 bool irq_detach;
49 struct work_struct irq_work;
50
51 struct reg_data *reg_data;
52 unsigned int num_reg_data;
53
54 struct mutex mutex;
Chanwoo Choie1954452014-05-28 14:21:55 +090055
56 /*
57 * Use delayed workqueue to detect cable state and then
58 * notify cable state to notifiee/platform through uevent.
59 * After completing the booting of platform, the extcon provider
60 * driver should notify cable state to upper layer.
61 */
62 struct delayed_work wq_detcable;
Chanwoo Choi914b8812014-05-22 14:06:33 +090063};
64
65/* Default value of SM5502 register to bring up MUIC device. */
66static struct reg_data sm5502_reg_data[] = {
67 {
Stephan Gerhold69426352019-10-10 17:47:20 +020068 .reg = SM5502_REG_RESET,
69 .val = SM5502_REG_RESET_MASK,
70 .invert = true,
71 }, {
Chanwoo Choi914b8812014-05-22 14:06:33 +090072 .reg = SM5502_REG_CONTROL,
73 .val = SM5502_REG_CONTROL_MASK_INT_MASK,
74 .invert = false,
75 }, {
76 .reg = SM5502_REG_INTMASK1,
77 .val = SM5502_REG_INTM1_KP_MASK
78 | SM5502_REG_INTM1_LKP_MASK
79 | SM5502_REG_INTM1_LKR_MASK,
80 .invert = true,
81 }, {
82 .reg = SM5502_REG_INTMASK2,
83 .val = SM5502_REG_INTM2_VBUS_DET_MASK
84 | SM5502_REG_INTM2_REV_ACCE_MASK
85 | SM5502_REG_INTM2_ADC_CHG_MASK
86 | SM5502_REG_INTM2_STUCK_KEY_MASK
87 | SM5502_REG_INTM2_STUCK_KEY_RCV_MASK
88 | SM5502_REG_INTM2_MHL_MASK,
89 .invert = true,
90 },
Chanwoo Choi914b8812014-05-22 14:06:33 +090091};
92
93/* List of detectable cables */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090094static const unsigned int sm5502_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090095 EXTCON_USB,
96 EXTCON_USB_HOST,
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +090097 EXTCON_CHG_USB_SDP,
Chanwoo Choi11eecf92015-10-03 14:15:13 +090098 EXTCON_CHG_USB_DCP,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090099 EXTCON_NONE,
Chanwoo Choi914b8812014-05-22 14:06:33 +0900100};
101
102/* Define supported accessory type */
103enum sm5502_muic_acc_type {
104 SM5502_MUIC_ADC_GROUND = 0x0,
105 SM5502_MUIC_ADC_SEND_END_BUTTON,
106 SM5502_MUIC_ADC_REMOTE_S1_BUTTON,
107 SM5502_MUIC_ADC_REMOTE_S2_BUTTON,
108 SM5502_MUIC_ADC_REMOTE_S3_BUTTON,
109 SM5502_MUIC_ADC_REMOTE_S4_BUTTON,
110 SM5502_MUIC_ADC_REMOTE_S5_BUTTON,
111 SM5502_MUIC_ADC_REMOTE_S6_BUTTON,
112 SM5502_MUIC_ADC_REMOTE_S7_BUTTON,
113 SM5502_MUIC_ADC_REMOTE_S8_BUTTON,
114 SM5502_MUIC_ADC_REMOTE_S9_BUTTON,
115 SM5502_MUIC_ADC_REMOTE_S10_BUTTON,
116 SM5502_MUIC_ADC_REMOTE_S11_BUTTON,
117 SM5502_MUIC_ADC_REMOTE_S12_BUTTON,
118 SM5502_MUIC_ADC_RESERVED_ACC_1,
119 SM5502_MUIC_ADC_RESERVED_ACC_2,
120 SM5502_MUIC_ADC_RESERVED_ACC_3,
121 SM5502_MUIC_ADC_RESERVED_ACC_4,
122 SM5502_MUIC_ADC_RESERVED_ACC_5,
123 SM5502_MUIC_ADC_AUDIO_TYPE2,
124 SM5502_MUIC_ADC_PHONE_POWERED_DEV,
125 SM5502_MUIC_ADC_TTY_CONVERTER,
126 SM5502_MUIC_ADC_UART_CABLE,
127 SM5502_MUIC_ADC_TYPE1_CHARGER,
128 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
129 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
130 SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE,
131 SM5502_MUIC_ADC_TYPE2_CHARGER,
132 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
133 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
134 SM5502_MUIC_ADC_AUDIO_TYPE1,
135 SM5502_MUIC_ADC_OPEN = 0x1f,
136
Srikant Ritoliaaf57fa42016-12-07 17:29:39 +0530137 /*
138 * The below accessories have same ADC value (0x1f or 0x1e).
139 * So, Device type1 is used to separate specific accessory.
140 */
Chanwoo Choi914b8812014-05-22 14:06:33 +0900141 /* |---------|--ADC| */
142 /* | [7:5]|[4:0]| */
143 SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* | 001|11110| */
144 SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e, /* | 010|11110| */
145 /* |Dev Type1|--ADC| */
Nikita Travkine3f60322021-01-19 18:33:47 +0500146 SM5502_MUIC_ADC_GROUND_USB_OTG = 0x80, /* | 100|00000| */
Chanwoo Choi914b8812014-05-22 14:06:33 +0900147 SM5502_MUIC_ADC_OPEN_USB = 0x5f, /* | 010|11111| */
148 SM5502_MUIC_ADC_OPEN_TA = 0xdf, /* | 110|11111| */
149 SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff, /* | 111|11111| */
150};
151
152/* List of supported interrupt for SM5502 */
153static struct muic_irq sm5502_muic_irqs[] = {
154 { SM5502_IRQ_INT1_ATTACH, "muic-attach" },
155 { SM5502_IRQ_INT1_DETACH, "muic-detach" },
156 { SM5502_IRQ_INT1_KP, "muic-kp" },
157 { SM5502_IRQ_INT1_LKP, "muic-lkp" },
158 { SM5502_IRQ_INT1_LKR, "muic-lkr" },
159 { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" },
160 { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" },
161 { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" },
162 { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" },
163 { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" },
164 { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" },
165 { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" },
166 { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" },
167 { SM5502_IRQ_INT2_MHL, "muic-mhl" },
168};
169
170/* Define interrupt list of SM5502 to register regmap_irq */
171static const struct regmap_irq sm5502_irqs[] = {
172 /* INT1 interrupts */
173 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, },
174 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, },
175 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, },
176 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, },
177 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, },
178 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, },
179 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, },
180 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, },
181
182 /* INT2 interrupts */
183 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,},
184 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, },
185 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, },
186 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, },
187 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, },
188 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, },
189};
190
191static const struct regmap_irq_chip sm5502_muic_irq_chip = {
192 .name = "sm5502",
193 .status_base = SM5502_REG_INT1,
194 .mask_base = SM5502_REG_INTMASK1,
195 .mask_invert = false,
196 .num_regs = 2,
197 .irqs = sm5502_irqs,
198 .num_irqs = ARRAY_SIZE(sm5502_irqs),
199};
200
201/* Define regmap configuration of SM5502 for I2C communication */
202static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg)
203{
204 switch (reg) {
205 case SM5502_REG_INTMASK1:
206 case SM5502_REG_INTMASK2:
207 return true;
208 default:
209 break;
210 }
211 return false;
212}
213
214static const struct regmap_config sm5502_muic_regmap_config = {
215 .reg_bits = 8,
216 .val_bits = 8,
217 .volatile_reg = sm5502_muic_volatile_reg,
218 .max_register = SM5502_REG_END,
219};
220
Chanwoo Choia75fed22014-05-28 15:35:29 +0900221/* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
222static int sm5502_muic_set_path(struct sm5502_muic_info *info,
223 unsigned int con_sw, unsigned int vbus_sw,
224 bool attached)
225{
226 int ret;
227
228 if (!attached) {
229 con_sw = DM_DP_SWITCH_OPEN;
230 vbus_sw = VBUSIN_SWITCH_OPEN;
231 }
232
233 switch (con_sw) {
234 case DM_DP_SWITCH_OPEN:
235 case DM_DP_SWITCH_USB:
236 case DM_DP_SWITCH_AUDIO:
237 case DM_DP_SWITCH_UART:
238 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
239 SM5502_REG_MANUAL_SW1_DP_MASK |
240 SM5502_REG_MANUAL_SW1_DM_MASK,
241 con_sw);
242 if (ret < 0) {
243 dev_err(info->dev,
244 "cannot update DM_CON/DP_CON switch\n");
245 return ret;
246 }
247 break;
248 default:
249 dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
250 con_sw);
251 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000252 }
Chanwoo Choia75fed22014-05-28 15:35:29 +0900253
254 switch (vbus_sw) {
255 case VBUSIN_SWITCH_OPEN:
256 case VBUSIN_SWITCH_VBUSOUT:
257 case VBUSIN_SWITCH_MIC:
258 case VBUSIN_SWITCH_VBUSOUT_WITH_USB:
259 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
260 SM5502_REG_MANUAL_SW1_VBUSIN_MASK,
261 vbus_sw);
262 if (ret < 0) {
263 dev_err(info->dev,
264 "cannot update VBUSIN switch\n");
265 return ret;
266 }
267 break;
268 default:
269 dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw);
270 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000271 }
Chanwoo Choia75fed22014-05-28 15:35:29 +0900272
273 return 0;
274}
275
Chanwoo Choi914b8812014-05-22 14:06:33 +0900276/* Return cable type of attached or detached accessories */
277static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info)
278{
Colin Ian Kingddd1bbb2019-10-25 14:12:27 +0100279 unsigned int cable_type, adc, dev_type1;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900280 int ret;
281
282 /* Read ADC value according to external cable or button */
283 ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc);
284 if (ret) {
285 dev_err(info->dev, "failed to read ADC register\n");
286 return ret;
287 }
288
289 /*
290 * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't
291 * connected with to MUIC device.
292 */
Chanwoo Choi0ccc7952014-07-30 15:39:02 +0900293 cable_type = adc & SM5502_REG_ADC_MASK;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900294
295 switch (cable_type) {
296 case SM5502_MUIC_ADC_GROUND:
Nikita Travkine3f60322021-01-19 18:33:47 +0500297 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
298 &dev_type1);
299 if (ret) {
300 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
301 return ret;
302 }
303
304 switch (dev_type1) {
305 case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
306 cable_type = SM5502_MUIC_ADC_GROUND_USB_OTG;
307 break;
308 default:
309 dev_dbg(info->dev,
310 "cannot identify the cable type: adc(0x%x), dev_type1(0x%x)\n",
311 adc, dev_type1);
312 return -EINVAL;
313 }
314 break;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900315 case SM5502_MUIC_ADC_SEND_END_BUTTON:
316 case SM5502_MUIC_ADC_REMOTE_S1_BUTTON:
317 case SM5502_MUIC_ADC_REMOTE_S2_BUTTON:
318 case SM5502_MUIC_ADC_REMOTE_S3_BUTTON:
319 case SM5502_MUIC_ADC_REMOTE_S4_BUTTON:
320 case SM5502_MUIC_ADC_REMOTE_S5_BUTTON:
321 case SM5502_MUIC_ADC_REMOTE_S6_BUTTON:
322 case SM5502_MUIC_ADC_REMOTE_S7_BUTTON:
323 case SM5502_MUIC_ADC_REMOTE_S8_BUTTON:
324 case SM5502_MUIC_ADC_REMOTE_S9_BUTTON:
325 case SM5502_MUIC_ADC_REMOTE_S10_BUTTON:
326 case SM5502_MUIC_ADC_REMOTE_S11_BUTTON:
327 case SM5502_MUIC_ADC_REMOTE_S12_BUTTON:
328 case SM5502_MUIC_ADC_RESERVED_ACC_1:
329 case SM5502_MUIC_ADC_RESERVED_ACC_2:
330 case SM5502_MUIC_ADC_RESERVED_ACC_3:
331 case SM5502_MUIC_ADC_RESERVED_ACC_4:
332 case SM5502_MUIC_ADC_RESERVED_ACC_5:
333 case SM5502_MUIC_ADC_AUDIO_TYPE2:
334 case SM5502_MUIC_ADC_PHONE_POWERED_DEV:
335 case SM5502_MUIC_ADC_TTY_CONVERTER:
336 case SM5502_MUIC_ADC_UART_CABLE:
337 case SM5502_MUIC_ADC_TYPE1_CHARGER:
338 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
339 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
340 case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE:
341 case SM5502_MUIC_ADC_TYPE2_CHARGER:
342 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
343 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
344 break;
345 case SM5502_MUIC_ADC_AUDIO_TYPE1:
346 /*
347 * Check whether cable type is
348 * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE
349 * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END
350 * by using Button event.
351 */
352 break;
353 case SM5502_MUIC_ADC_OPEN:
354 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
355 &dev_type1);
356 if (ret) {
357 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
358 return ret;
359 }
360
361 switch (dev_type1) {
362 case SM5502_REG_DEV_TYPE1_USB_SDP_MASK:
363 cable_type = SM5502_MUIC_ADC_OPEN_USB;
364 break;
365 case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK:
366 cable_type = SM5502_MUIC_ADC_OPEN_TA;
367 break;
368 case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
369 cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG;
370 break;
371 default:
372 dev_dbg(info->dev,
Chanwoo Choi34825e52015-03-07 01:41:36 +0900373 "cannot identify the cable type: adc(0x%x)\n",
374 adc);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900375 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000376 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900377 break;
378 default:
379 dev_err(info->dev,
380 "failed to identify the cable type: adc(0x%x)\n", adc);
381 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000382 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900383
384 return cable_type;
385}
386
387static int sm5502_muic_cable_handler(struct sm5502_muic_info *info,
388 bool attached)
389{
390 static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900391 unsigned int cable_type = SM5502_MUIC_ADC_GROUND;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900392 unsigned int con_sw = DM_DP_SWITCH_OPEN;
393 unsigned int vbus_sw = VBUSIN_SWITCH_OPEN;
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900394 unsigned int id;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900395 int ret;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900396
Chanwoo Choi914b8812014-05-22 14:06:33 +0900397 /* Get the type of attached or detached cable */
398 if (attached)
399 cable_type = sm5502_muic_get_cable_type(info);
Chanwoo Choifbae30d2014-08-12 10:15:39 +0900400 else
Chanwoo Choi914b8812014-05-22 14:06:33 +0900401 cable_type = prev_cable_type;
402 prev_cable_type = cable_type;
403
404 switch (cable_type) {
405 case SM5502_MUIC_ADC_OPEN_USB:
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900406 id = EXTCON_USB;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900407 con_sw = DM_DP_SWITCH_USB;
408 vbus_sw = VBUSIN_SWITCH_VBUSOUT_WITH_USB;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900409 break;
410 case SM5502_MUIC_ADC_OPEN_TA:
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900411 id = EXTCON_CHG_USB_DCP;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900412 con_sw = DM_DP_SWITCH_OPEN;
413 vbus_sw = VBUSIN_SWITCH_VBUSOUT;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900414 break;
Nikita Travkine3f60322021-01-19 18:33:47 +0500415 case SM5502_MUIC_ADC_GROUND_USB_OTG:
Chanwoo Choi914b8812014-05-22 14:06:33 +0900416 case SM5502_MUIC_ADC_OPEN_USB_OTG:
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900417 id = EXTCON_USB_HOST;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900418 con_sw = DM_DP_SWITCH_USB;
419 vbus_sw = VBUSIN_SWITCH_OPEN;
Chanwoo Choie1954452014-05-28 14:21:55 +0900420 break;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900421 default:
422 dev_dbg(info->dev,
423 "cannot handle this cable_type (0x%x)\n", cable_type);
424 return 0;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000425 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900426
Chanwoo Choia75fed22014-05-28 15:35:29 +0900427 /* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */
428 ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached);
429 if (ret < 0)
430 return ret;
431
432 /* Change the state of external accessory */
Chanwoo Choi8670b452016-08-16 15:55:34 +0900433 extcon_set_state_sync(info->edev, id, attached);
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +0900434 if (id == EXTCON_USB)
Chanwoo Choi8670b452016-08-16 15:55:34 +0900435 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +0900436 attached);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900437
438 return 0;
439}
440
441static void sm5502_muic_irq_work(struct work_struct *work)
442{
443 struct sm5502_muic_info *info = container_of(work,
444 struct sm5502_muic_info, irq_work);
445 int ret = 0;
446
447 if (!info->edev)
448 return;
449
450 mutex_lock(&info->mutex);
451
452 /* Detect attached or detached cables */
453 if (info->irq_attach) {
454 ret = sm5502_muic_cable_handler(info, true);
455 info->irq_attach = false;
456 }
457 if (info->irq_detach) {
458 ret = sm5502_muic_cable_handler(info, false);
459 info->irq_detach = false;
460 }
461
462 if (ret < 0)
463 dev_err(info->dev, "failed to handle MUIC interrupt\n");
464
465 mutex_unlock(&info->mutex);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900466}
467
468/*
469 * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0.
470 * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
471 */
472static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type)
473{
474 switch (irq_type) {
475 case SM5502_IRQ_INT1_ATTACH:
476 info->irq_attach = true;
477 break;
478 case SM5502_IRQ_INT1_DETACH:
479 info->irq_detach = true;
480 break;
481 case SM5502_IRQ_INT1_KP:
482 case SM5502_IRQ_INT1_LKP:
483 case SM5502_IRQ_INT1_LKR:
484 case SM5502_IRQ_INT1_OVP_EVENT:
485 case SM5502_IRQ_INT1_OCP_EVENT:
486 case SM5502_IRQ_INT1_OVP_OCP_DIS:
487 case SM5502_IRQ_INT2_VBUS_DET:
488 case SM5502_IRQ_INT2_REV_ACCE:
489 case SM5502_IRQ_INT2_ADC_CHG:
490 case SM5502_IRQ_INT2_STUCK_KEY:
491 case SM5502_IRQ_INT2_STUCK_KEY_RCV:
492 case SM5502_IRQ_INT2_MHL:
493 default:
494 break;
495 }
496
497 return 0;
498}
499
500static irqreturn_t sm5502_muic_irq_handler(int irq, void *data)
501{
502 struct sm5502_muic_info *info = data;
503 int i, irq_type = -1, ret;
504
505 for (i = 0; i < info->num_muic_irqs; i++)
506 if (irq == info->muic_irqs[i].virq)
507 irq_type = info->muic_irqs[i].irq;
508
509 ret = sm5502_parse_irq(info, irq_type);
510 if (ret < 0) {
511 dev_warn(info->dev, "cannot handle is interrupt:%d\n",
512 irq_type);
513 return IRQ_HANDLED;
514 }
515 schedule_work(&info->irq_work);
516
517 return IRQ_HANDLED;
518}
519
Chanwoo Choie1954452014-05-28 14:21:55 +0900520static void sm5502_muic_detect_cable_wq(struct work_struct *work)
521{
522 struct sm5502_muic_info *info = container_of(to_delayed_work(work),
523 struct sm5502_muic_info, wq_detcable);
524 int ret;
525
526 /* Notify the state of connector cable or not */
527 ret = sm5502_muic_cable_handler(info, true);
528 if (ret < 0)
529 dev_warn(info->dev, "failed to detect cable state\n");
530}
531
Chanwoo Choi914b8812014-05-22 14:06:33 +0900532static void sm5502_init_dev_type(struct sm5502_muic_info *info)
533{
534 unsigned int reg_data, vendor_id, version_id;
535 int i, ret;
536
537 /* To test I2C, Print version_id and vendor_id of SM5502 */
538 ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, &reg_data);
539 if (ret) {
540 dev_err(info->dev,
541 "failed to read DEVICE_ID register: %d\n", ret);
542 return;
543 }
544
545 vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >>
546 SM5502_REG_DEVICE_ID_VENDOR_SHIFT);
547 version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >>
548 SM5502_REG_DEVICE_ID_VERSION_SHIFT);
549
550 dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
551 version_id, vendor_id);
552
553 /* Initiazle the register of SM5502 device to bring-up */
554 for (i = 0; i < info->num_reg_data; i++) {
555 unsigned int val = 0;
556
557 if (!info->reg_data[i].invert)
558 val |= ~info->reg_data[i].val;
559 else
560 val = info->reg_data[i].val;
561 regmap_write(info->regmap, info->reg_data[i].reg, val);
562 }
563}
564
Stephan Gerholdb1b76af2021-05-31 15:34:34 +0200565static int sm5022_muic_i2c_probe(struct i2c_client *i2c)
Chanwoo Choi914b8812014-05-22 14:06:33 +0900566{
567 struct device_node *np = i2c->dev.of_node;
568 struct sm5502_muic_info *info;
569 int i, ret, irq_flags;
570
571 if (!np)
572 return -EINVAL;
573
574 info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
575 if (!info)
576 return -ENOMEM;
577 i2c_set_clientdata(i2c, info);
578
579 info->dev = &i2c->dev;
580 info->i2c = i2c;
581 info->irq = i2c->irq;
582 info->muic_irqs = sm5502_muic_irqs;
583 info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs);
584 info->reg_data = sm5502_reg_data;
585 info->num_reg_data = ARRAY_SIZE(sm5502_reg_data);
586
587 mutex_init(&info->mutex);
588
589 INIT_WORK(&info->irq_work, sm5502_muic_irq_work);
590
591 info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config);
592 if (IS_ERR(info->regmap)) {
593 ret = PTR_ERR(info->regmap);
594 dev_err(info->dev, "failed to allocate register map: %d\n",
595 ret);
596 return ret;
597 }
598
599 /* Support irq domain for SM5502 MUIC device */
600 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
Stephan Gerholdd3a213d2021-05-31 15:34:33 +0200601 ret = devm_regmap_add_irq_chip(info->dev, info->regmap, info->irq, irq_flags,
602 0, &sm5502_muic_irq_chip, &info->irq_data);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900603 if (ret != 0) {
604 dev_err(info->dev, "failed to request IRQ %d: %d\n",
605 info->irq, ret);
606 return ret;
607 }
608
609 for (i = 0; i < info->num_muic_irqs; i++) {
610 struct muic_irq *muic_irq = &info->muic_irqs[i];
Andrzej Hajda363b3892015-09-24 16:00:21 +0200611 int virq = 0;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900612
613 virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
614 if (virq <= 0)
615 return -EINVAL;
616 muic_irq->virq = virq;
617
618 ret = devm_request_threaded_irq(info->dev, virq, NULL,
619 sm5502_muic_irq_handler,
Vasyl Gomonovych005ad182019-07-19 18:28:06 +0200620 IRQF_NO_SUSPEND | IRQF_ONESHOT,
Chanwoo Choi914b8812014-05-22 14:06:33 +0900621 muic_irq->name, info);
622 if (ret) {
Chanwoo Choifbae30d2014-08-12 10:15:39 +0900623 dev_err(info->dev,
624 "failed: irq request (IRQ: %d, error :%d)\n",
625 muic_irq->irq, ret);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900626 return ret;
627 }
628 }
629
630 /* Allocate extcon device */
631 info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable);
632 if (IS_ERR(info->edev)) {
633 dev_err(info->dev, "failed to allocate memory for extcon\n");
634 return -ENOMEM;
635 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900636
637 /* Register extcon device */
638 ret = devm_extcon_dev_register(info->dev, info->edev);
639 if (ret) {
640 dev_err(info->dev, "failed to register extcon device\n");
641 return ret;
642 }
643
Chanwoo Choie1954452014-05-28 14:21:55 +0900644 /*
645 * Detect accessory after completing the initialization of platform
646 *
647 * - Use delayed workqueue to detect cable state and then
648 * notify cable state to notifiee/platform through uevent.
649 * After completing the booting of platform, the extcon provider
650 * driver should notify cable state to upper layer.
651 */
652 INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
653 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
654 msecs_to_jiffies(DELAY_MS_DEFAULT));
655
Chanwoo Choi914b8812014-05-22 14:06:33 +0900656 /* Initialize SM5502 device and print vendor id and version id */
657 sm5502_init_dev_type(info);
658
659 return 0;
660}
661
Chanwoo Choi34825e52015-03-07 01:41:36 +0900662static const struct of_device_id sm5502_dt_match[] = {
Chanwoo Choi914b8812014-05-22 14:06:33 +0900663 { .compatible = "siliconmitus,sm5502-muic" },
664 { },
665};
Javier Martinez Canillasff612f92015-08-25 08:31:15 +0200666MODULE_DEVICE_TABLE(of, sm5502_dt_match);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900667
668#ifdef CONFIG_PM_SLEEP
669static int sm5502_muic_suspend(struct device *dev)
670{
Geliang Tangd5859342015-12-28 23:00:15 +0800671 struct i2c_client *i2c = to_i2c_client(dev);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900672 struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
673
674 enable_irq_wake(info->irq);
675
676 return 0;
677}
678
679static int sm5502_muic_resume(struct device *dev)
680{
Geliang Tangd5859342015-12-28 23:00:15 +0800681 struct i2c_client *i2c = to_i2c_client(dev);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900682 struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
683
684 disable_irq_wake(info->irq);
685
686 return 0;
687}
688#endif
689
690static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops,
691 sm5502_muic_suspend, sm5502_muic_resume);
692
693static const struct i2c_device_id sm5502_i2c_id[] = {
694 { "sm5502", TYPE_SM5502 },
695 { }
696};
697MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id);
698
699static struct i2c_driver sm5502_muic_i2c_driver = {
700 .driver = {
701 .name = "sm5502",
Chanwoo Choi914b8812014-05-22 14:06:33 +0900702 .pm = &sm5502_muic_pm_ops,
703 .of_match_table = sm5502_dt_match,
704 },
Stephan Gerholdb1b76af2021-05-31 15:34:34 +0200705 .probe_new = sm5022_muic_i2c_probe,
Chanwoo Choi914b8812014-05-22 14:06:33 +0900706 .id_table = sm5502_i2c_id,
707};
708
709static int __init sm5502_muic_i2c_init(void)
710{
711 return i2c_add_driver(&sm5502_muic_i2c_driver);
712}
713subsys_initcall(sm5502_muic_i2c_init);
714
715MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver");
716MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
717MODULE_LICENSE("GPL");