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