xref: /linux/drivers/extcon/extcon-max77843.c (revision 52338415)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // extcon-max77843.c - Maxim MAX77843 extcon driver to support
4 //			MUIC(Micro USB Interface Controller)
5 //
6 // Copyright (C) 2015 Samsung Electronics
7 // Author: Jaewon Kim <jaewon02.kim@samsung.com>
8 
9 #include <linux/extcon-provider.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/mfd/max77693-common.h>
14 #include <linux/mfd/max77843-private.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/workqueue.h>
18 
19 #define DELAY_MS_DEFAULT		15000	/* unit: millisecond */
20 
21 enum max77843_muic_status {
22 	MAX77843_MUIC_STATUS1 = 0,
23 	MAX77843_MUIC_STATUS2,
24 	MAX77843_MUIC_STATUS3,
25 
26 	MAX77843_MUIC_STATUS_NUM,
27 };
28 
29 struct max77843_muic_info {
30 	struct device *dev;
31 	struct max77693_dev *max77843;
32 	struct extcon_dev *edev;
33 
34 	struct mutex mutex;
35 	struct work_struct irq_work;
36 	struct delayed_work wq_detcable;
37 
38 	u8 status[MAX77843_MUIC_STATUS_NUM];
39 	int prev_cable_type;
40 	int prev_chg_type;
41 	int prev_gnd_type;
42 
43 	bool irq_adc;
44 	bool irq_chg;
45 };
46 
47 enum max77843_muic_cable_group {
48 	MAX77843_CABLE_GROUP_ADC = 0,
49 	MAX77843_CABLE_GROUP_ADC_GND,
50 	MAX77843_CABLE_GROUP_CHG,
51 };
52 
53 enum max77843_muic_adc_debounce_time {
54 	MAX77843_DEBOUNCE_TIME_5MS = 0,
55 	MAX77843_DEBOUNCE_TIME_10MS,
56 	MAX77843_DEBOUNCE_TIME_25MS,
57 	MAX77843_DEBOUNCE_TIME_38_62MS,
58 };
59 
60 /* Define accessory cable type */
61 enum max77843_muic_accessory_type {
62 	MAX77843_MUIC_ADC_GROUND = 0,
63 	MAX77843_MUIC_ADC_SEND_END_BUTTON,
64 	MAX77843_MUIC_ADC_REMOTE_S1_BUTTON,
65 	MAX77843_MUIC_ADC_REMOTE_S2_BUTTON,
66 	MAX77843_MUIC_ADC_REMOTE_S3_BUTTON,
67 	MAX77843_MUIC_ADC_REMOTE_S4_BUTTON,
68 	MAX77843_MUIC_ADC_REMOTE_S5_BUTTON,
69 	MAX77843_MUIC_ADC_REMOTE_S6_BUTTON,
70 	MAX77843_MUIC_ADC_REMOTE_S7_BUTTON,
71 	MAX77843_MUIC_ADC_REMOTE_S8_BUTTON,
72 	MAX77843_MUIC_ADC_REMOTE_S9_BUTTON,
73 	MAX77843_MUIC_ADC_REMOTE_S10_BUTTON,
74 	MAX77843_MUIC_ADC_REMOTE_S11_BUTTON,
75 	MAX77843_MUIC_ADC_REMOTE_S12_BUTTON,
76 	MAX77843_MUIC_ADC_RESERVED_ACC_1,
77 	MAX77843_MUIC_ADC_RESERVED_ACC_2,
78 	MAX77843_MUIC_ADC_RESERVED_ACC_3, /* SmartDock */
79 	MAX77843_MUIC_ADC_RESERVED_ACC_4,
80 	MAX77843_MUIC_ADC_RESERVED_ACC_5,
81 	MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2,
82 	MAX77843_MUIC_ADC_PHONE_POWERED_DEV,
83 	MAX77843_MUIC_ADC_TTY_CONVERTER,
84 	MAX77843_MUIC_ADC_UART_CABLE,
85 	MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG,
86 	MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF,
87 	MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON,
88 	MAX77843_MUIC_ADC_AV_CABLE_NOLOAD,
89 	MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG,
90 	MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF,
91 	MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON,
92 	MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1,
93 	MAX77843_MUIC_ADC_OPEN,
94 
95 	/*
96 	 * The below accessories should check
97 	 * not only ADC value but also ADC1K and VBVolt value.
98 	 */
99 						/* Offset|ADC1K|VBVolt| */
100 	MAX77843_MUIC_GND_USB_HOST = 0x100,	/*    0x1|    0|     0| */
101 	MAX77843_MUIC_GND_USB_HOST_VB = 0x101,	/*    0x1|    0|     1| */
102 	MAX77843_MUIC_GND_MHL = 0x102,		/*    0x1|    1|     0| */
103 	MAX77843_MUIC_GND_MHL_VB = 0x103,	/*    0x1|    1|     1| */
104 };
105 
106 /* Define charger cable type */
107 enum max77843_muic_charger_type {
108 	MAX77843_MUIC_CHG_NONE = 0,
109 	MAX77843_MUIC_CHG_USB,
110 	MAX77843_MUIC_CHG_DOWNSTREAM,
111 	MAX77843_MUIC_CHG_DEDICATED,
112 	MAX77843_MUIC_CHG_SPECIAL_500MA,
113 	MAX77843_MUIC_CHG_SPECIAL_1A,
114 	MAX77843_MUIC_CHG_SPECIAL_BIAS,
115 	MAX77843_MUIC_CHG_RESERVED,
116 	MAX77843_MUIC_CHG_GND,
117 	MAX77843_MUIC_CHG_DOCK,
118 };
119 
120 static const unsigned int max77843_extcon_cable[] = {
121 	EXTCON_USB,
122 	EXTCON_USB_HOST,
123 	EXTCON_CHG_USB_SDP,
124 	EXTCON_CHG_USB_DCP,
125 	EXTCON_CHG_USB_CDP,
126 	EXTCON_CHG_USB_FAST,
127 	EXTCON_CHG_USB_SLOW,
128 	EXTCON_DISP_MHL,
129 	EXTCON_DOCK,
130 	EXTCON_JIG,
131 	EXTCON_NONE,
132 };
133 
134 struct max77843_muic_irq {
135 	unsigned int irq;
136 	const char *name;
137 	unsigned int virq;
138 };
139 
140 static struct max77843_muic_irq max77843_muic_irqs[] = {
141 	{ MAX77843_MUIC_IRQ_INT1_ADC,		"MUIC-ADC" },
142 	{ MAX77843_MUIC_IRQ_INT1_ADCERROR,	"MUIC-ADC_ERROR" },
143 	{ MAX77843_MUIC_IRQ_INT1_ADC1K,		"MUIC-ADC1K" },
144 	{ MAX77843_MUIC_IRQ_INT2_CHGTYP,	"MUIC-CHGTYP" },
145 	{ MAX77843_MUIC_IRQ_INT2_CHGDETRUN,	"MUIC-CHGDETRUN" },
146 	{ MAX77843_MUIC_IRQ_INT2_DCDTMR,	"MUIC-DCDTMR" },
147 	{ MAX77843_MUIC_IRQ_INT2_DXOVP,		"MUIC-DXOVP" },
148 	{ MAX77843_MUIC_IRQ_INT2_VBVOLT,	"MUIC-VBVOLT" },
149 	{ MAX77843_MUIC_IRQ_INT3_VBADC,		"MUIC-VBADC" },
150 	{ MAX77843_MUIC_IRQ_INT3_VDNMON,	"MUIC-VDNMON" },
151 	{ MAX77843_MUIC_IRQ_INT3_DNRES,		"MUIC-DNRES" },
152 	{ MAX77843_MUIC_IRQ_INT3_MPNACK,	"MUIC-MPNACK"},
153 	{ MAX77843_MUIC_IRQ_INT3_MRXBUFOW,	"MUIC-MRXBUFOW"},
154 	{ MAX77843_MUIC_IRQ_INT3_MRXTRF,	"MUIC-MRXTRF"},
155 	{ MAX77843_MUIC_IRQ_INT3_MRXPERR,	"MUIC-MRXPERR"},
156 	{ MAX77843_MUIC_IRQ_INT3_MRXRDY,	"MUIC-MRXRDY"},
157 };
158 
159 static const struct regmap_config max77843_muic_regmap_config = {
160 	.reg_bits       = 8,
161 	.val_bits       = 8,
162 	.max_register   = MAX77843_MUIC_REG_END,
163 };
164 
165 static const struct regmap_irq max77843_muic_irq[] = {
166 	/* INT1 interrupt */
167 	{ .reg_offset = 0, .mask = MAX77843_MUIC_ADC, },
168 	{ .reg_offset = 0, .mask = MAX77843_MUIC_ADCERROR, },
169 	{ .reg_offset = 0, .mask = MAX77843_MUIC_ADC1K, },
170 
171 	/* INT2 interrupt */
172 	{ .reg_offset = 1, .mask = MAX77843_MUIC_CHGTYP, },
173 	{ .reg_offset = 1, .mask = MAX77843_MUIC_CHGDETRUN, },
174 	{ .reg_offset = 1, .mask = MAX77843_MUIC_DCDTMR, },
175 	{ .reg_offset = 1, .mask = MAX77843_MUIC_DXOVP, },
176 	{ .reg_offset = 1, .mask = MAX77843_MUIC_VBVOLT, },
177 
178 	/* INT3 interrupt */
179 	{ .reg_offset = 2, .mask = MAX77843_MUIC_VBADC, },
180 	{ .reg_offset = 2, .mask = MAX77843_MUIC_VDNMON, },
181 	{ .reg_offset = 2, .mask = MAX77843_MUIC_DNRES, },
182 	{ .reg_offset = 2, .mask = MAX77843_MUIC_MPNACK, },
183 	{ .reg_offset = 2, .mask = MAX77843_MUIC_MRXBUFOW, },
184 	{ .reg_offset = 2, .mask = MAX77843_MUIC_MRXTRF, },
185 	{ .reg_offset = 2, .mask = MAX77843_MUIC_MRXPERR, },
186 	{ .reg_offset = 2, .mask = MAX77843_MUIC_MRXRDY, },
187 };
188 
189 static const struct regmap_irq_chip max77843_muic_irq_chip = {
190 	.name           = "max77843-muic",
191 	.status_base    = MAX77843_MUIC_REG_INT1,
192 	.mask_base      = MAX77843_MUIC_REG_INTMASK1,
193 	.mask_invert    = true,
194 	.num_regs       = 3,
195 	.irqs           = max77843_muic_irq,
196 	.num_irqs       = ARRAY_SIZE(max77843_muic_irq),
197 };
198 
199 static int max77843_muic_set_path(struct max77843_muic_info *info,
200 		u8 val, bool attached, bool nobccomp)
201 {
202 	struct max77693_dev *max77843 = info->max77843;
203 	int ret = 0;
204 	unsigned int ctrl1, ctrl2;
205 
206 	if (attached)
207 		ctrl1 = val;
208 	else
209 		ctrl1 = MAX77843_MUIC_CONTROL1_SW_OPEN;
210 	if (nobccomp) {
211 		/* Disable BC1.2 protocol and force manual switch control */
212 		ctrl1 |= MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK;
213 	}
214 
215 	ret = regmap_update_bits(max77843->regmap_muic,
216 			MAX77843_MUIC_REG_CONTROL1,
217 			MAX77843_MUIC_CONTROL1_COM_SW |
218 				MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK,
219 			ctrl1);
220 	if (ret < 0) {
221 		dev_err(info->dev, "Cannot switch MUIC port\n");
222 		return ret;
223 	}
224 
225 	if (attached)
226 		ctrl2 = MAX77843_MUIC_CONTROL2_CPEN_MASK;
227 	else
228 		ctrl2 = MAX77843_MUIC_CONTROL2_LOWPWR_MASK;
229 
230 	ret = regmap_update_bits(max77843->regmap_muic,
231 			MAX77843_MUIC_REG_CONTROL2,
232 			MAX77843_MUIC_CONTROL2_LOWPWR_MASK |
233 			MAX77843_MUIC_CONTROL2_CPEN_MASK, ctrl2);
234 	if (ret < 0) {
235 		dev_err(info->dev, "Cannot update lowpower mode\n");
236 		return ret;
237 	}
238 
239 	dev_dbg(info->dev,
240 		"CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
241 		ctrl1, ctrl2, attached ? "attached" : "detached");
242 
243 	return 0;
244 }
245 
246 static void max77843_charger_set_otg_vbus(struct max77843_muic_info *info,
247 		 bool on)
248 {
249 	struct max77693_dev *max77843 = info->max77843;
250 	unsigned int cnfg00;
251 
252 	if (on)
253 		cnfg00 = MAX77843_CHG_OTG_MASK | MAX77843_CHG_BOOST_MASK;
254 	else
255 		cnfg00 = MAX77843_CHG_ENABLE | MAX77843_CHG_BUCK_MASK;
256 
257 	regmap_update_bits(max77843->regmap_chg, MAX77843_CHG_REG_CHG_CNFG_00,
258 			   MAX77843_CHG_MODE_MASK, cnfg00);
259 }
260 
261 static int max77843_muic_get_cable_type(struct max77843_muic_info *info,
262 		enum max77843_muic_cable_group group, bool *attached)
263 {
264 	int adc, chg_type, cable_type, gnd_type;
265 
266 	adc = info->status[MAX77843_MUIC_STATUS1] &
267 			MAX77843_MUIC_STATUS1_ADC_MASK;
268 	adc >>= MAX77843_MUIC_STATUS1_ADC_SHIFT;
269 
270 	switch (group) {
271 	case MAX77843_CABLE_GROUP_ADC:
272 		if (adc == MAX77843_MUIC_ADC_OPEN) {
273 			*attached = false;
274 			cable_type = info->prev_cable_type;
275 			info->prev_cable_type = MAX77843_MUIC_ADC_OPEN;
276 		} else {
277 			*attached = true;
278 			cable_type = info->prev_cable_type = adc;
279 		}
280 		break;
281 	case MAX77843_CABLE_GROUP_CHG:
282 		chg_type = info->status[MAX77843_MUIC_STATUS2] &
283 				MAX77843_MUIC_STATUS2_CHGTYP_MASK;
284 
285 		/* Check GROUND accessory with charger cable */
286 		if (adc == MAX77843_MUIC_ADC_GROUND) {
287 			if (chg_type == MAX77843_MUIC_CHG_NONE) {
288 				/*
289 				 * The following state when charger cable is
290 				 * disconnected but the GROUND accessory still
291 				 * connected.
292 				 */
293 				*attached = false;
294 				cable_type = info->prev_chg_type;
295 				info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
296 			} else {
297 
298 				/*
299 				 * The following state when charger cable is
300 				 * connected on the GROUND accessory.
301 				 */
302 				*attached = true;
303 				cable_type = MAX77843_MUIC_CHG_GND;
304 				info->prev_chg_type = MAX77843_MUIC_CHG_GND;
305 			}
306 			break;
307 		}
308 
309 		if (adc == MAX77843_MUIC_ADC_RESERVED_ACC_3) { /* SmartDock */
310 			if (chg_type == MAX77843_MUIC_CHG_NONE) {
311 				*attached = false;
312 				cable_type = info->prev_chg_type;
313 				info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
314 			} else {
315 				*attached = true;
316 				cable_type = MAX77843_MUIC_CHG_DOCK;
317 				info->prev_chg_type = MAX77843_MUIC_CHG_DOCK;
318 			}
319 			break;
320 		}
321 
322 		if (chg_type == MAX77843_MUIC_CHG_NONE) {
323 			*attached = false;
324 			cable_type = info->prev_chg_type;
325 			info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
326 		} else {
327 			*attached = true;
328 			cable_type = info->prev_chg_type = chg_type;
329 		}
330 		break;
331 	case MAX77843_CABLE_GROUP_ADC_GND:
332 		if (adc == MAX77843_MUIC_ADC_OPEN) {
333 			*attached = false;
334 			cable_type = info->prev_gnd_type;
335 			info->prev_gnd_type = MAX77843_MUIC_ADC_OPEN;
336 		} else {
337 			*attached = true;
338 
339 			/*
340 			 * Offset|ADC1K|VBVolt|
341 			 *    0x1|    0|     0| USB-HOST
342 			 *    0x1|    0|     1| USB-HOST with VB
343 			 *    0x1|    1|     0| MHL
344 			 *    0x1|    1|     1| MHL with VB
345 			 */
346 			/* Get ADC1K register bit */
347 			gnd_type = (info->status[MAX77843_MUIC_STATUS1] &
348 					MAX77843_MUIC_STATUS1_ADC1K_MASK);
349 
350 			/* Get VBVolt register bit */
351 			gnd_type |= (info->status[MAX77843_MUIC_STATUS2] &
352 					MAX77843_MUIC_STATUS2_VBVOLT_MASK);
353 			gnd_type >>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT;
354 
355 			/* Offset of GND cable */
356 			gnd_type |= MAX77843_MUIC_GND_USB_HOST;
357 			cable_type = info->prev_gnd_type = gnd_type;
358 		}
359 		break;
360 	default:
361 		dev_err(info->dev, "Unknown cable group (%d)\n", group);
362 		cable_type = -EINVAL;
363 		break;
364 	}
365 
366 	return cable_type;
367 }
368 
369 static int max77843_muic_adc_gnd_handler(struct max77843_muic_info *info)
370 {
371 	int ret, gnd_cable_type;
372 	bool attached;
373 
374 	gnd_cable_type = max77843_muic_get_cable_type(info,
375 			MAX77843_CABLE_GROUP_ADC_GND, &attached);
376 	dev_dbg(info->dev, "external connector is %s (gnd:0x%02x)\n",
377 			attached ? "attached" : "detached", gnd_cable_type);
378 
379 	switch (gnd_cable_type) {
380 	case MAX77843_MUIC_GND_USB_HOST:
381 	case MAX77843_MUIC_GND_USB_HOST_VB:
382 		ret = max77843_muic_set_path(info,
383 					     MAX77843_MUIC_CONTROL1_SW_USB,
384 					     attached, false);
385 		if (ret < 0)
386 			return ret;
387 
388 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached);
389 		max77843_charger_set_otg_vbus(info, attached);
390 		break;
391 	case MAX77843_MUIC_GND_MHL_VB:
392 	case MAX77843_MUIC_GND_MHL:
393 		ret = max77843_muic_set_path(info,
394 					     MAX77843_MUIC_CONTROL1_SW_OPEN,
395 					     attached, false);
396 		if (ret < 0)
397 			return ret;
398 
399 		extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
400 		break;
401 	default:
402 		dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n",
403 			attached ? "attached" : "detached", gnd_cable_type);
404 		return -EINVAL;
405 	}
406 
407 	return 0;
408 }
409 
410 static int max77843_muic_jig_handler(struct max77843_muic_info *info,
411 		int cable_type, bool attached)
412 {
413 	int ret;
414 	u8 path = MAX77843_MUIC_CONTROL1_SW_OPEN;
415 
416 	dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n",
417 			attached ? "attached" : "detached", cable_type);
418 
419 	switch (cable_type) {
420 	case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
421 	case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
422 		path = MAX77843_MUIC_CONTROL1_SW_USB;
423 		break;
424 	case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
425 		path = MAX77843_MUIC_CONTROL1_SW_UART;
426 		break;
427 	default:
428 		return -EINVAL;
429 	}
430 
431 	ret = max77843_muic_set_path(info, path, attached, false);
432 	if (ret < 0)
433 		return ret;
434 
435 	extcon_set_state_sync(info->edev, EXTCON_JIG, attached);
436 
437 	return 0;
438 }
439 
440 static int max77843_muic_dock_handler(struct max77843_muic_info *info,
441 		bool attached)
442 {
443 	int ret;
444 
445 	dev_dbg(info->dev, "external connector is %s (adc: 0x10)\n",
446 			attached ? "attached" : "detached");
447 
448 	ret = max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_USB,
449 				     attached, attached);
450 	if (ret < 0)
451 		return ret;
452 
453 	extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
454 	extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached);
455 	extcon_set_state_sync(info->edev, EXTCON_DOCK, attached);
456 
457 	return 0;
458 }
459 
460 static int max77843_muic_adc_handler(struct max77843_muic_info *info)
461 {
462 	int ret, cable_type;
463 	bool attached;
464 
465 	cable_type = max77843_muic_get_cable_type(info,
466 			MAX77843_CABLE_GROUP_ADC, &attached);
467 
468 	dev_dbg(info->dev,
469 		"external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
470 		attached ? "attached" : "detached", cable_type,
471 		info->prev_cable_type);
472 
473 	switch (cable_type) {
474 	case MAX77843_MUIC_ADC_RESERVED_ACC_3: /* SmartDock */
475 		ret = max77843_muic_dock_handler(info, attached);
476 		if (ret < 0)
477 			return ret;
478 		break;
479 	case MAX77843_MUIC_ADC_GROUND:
480 		ret = max77843_muic_adc_gnd_handler(info);
481 		if (ret < 0)
482 			return ret;
483 		break;
484 	case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
485 	case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
486 	case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
487 		ret = max77843_muic_jig_handler(info, cable_type, attached);
488 		if (ret < 0)
489 			return ret;
490 		break;
491 	case MAX77843_MUIC_ADC_SEND_END_BUTTON:
492 	case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON:
493 	case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON:
494 	case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON:
495 	case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON:
496 	case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON:
497 	case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON:
498 	case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON:
499 	case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON:
500 	case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON:
501 	case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON:
502 	case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON:
503 	case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON:
504 	case MAX77843_MUIC_ADC_RESERVED_ACC_1:
505 	case MAX77843_MUIC_ADC_RESERVED_ACC_2:
506 	case MAX77843_MUIC_ADC_RESERVED_ACC_4:
507 	case MAX77843_MUIC_ADC_RESERVED_ACC_5:
508 	case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2:
509 	case MAX77843_MUIC_ADC_PHONE_POWERED_DEV:
510 	case MAX77843_MUIC_ADC_TTY_CONVERTER:
511 	case MAX77843_MUIC_ADC_UART_CABLE:
512 	case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG:
513 	case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD:
514 	case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG:
515 	case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON:
516 	case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1:
517 	case MAX77843_MUIC_ADC_OPEN:
518 		dev_err(info->dev,
519 			"accessory is %s but it isn't used (adc:0x%x)\n",
520 			attached ? "attached" : "detached", cable_type);
521 		return -EAGAIN;
522 	default:
523 		dev_err(info->dev,
524 			"failed to detect %s accessory (adc:0x%x)\n",
525 			attached ? "attached" : "detached", cable_type);
526 		return -EINVAL;
527 	}
528 
529 	return 0;
530 }
531 
532 static int max77843_muic_chg_handler(struct max77843_muic_info *info)
533 {
534 	int ret, chg_type, gnd_type;
535 	bool attached;
536 
537 	chg_type = max77843_muic_get_cable_type(info,
538 			MAX77843_CABLE_GROUP_CHG, &attached);
539 
540 	dev_dbg(info->dev,
541 		"external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
542 		attached ? "attached" : "detached",
543 		chg_type, info->prev_chg_type);
544 
545 	switch (chg_type) {
546 	case MAX77843_MUIC_CHG_USB:
547 		ret = max77843_muic_set_path(info,
548 					     MAX77843_MUIC_CONTROL1_SW_USB,
549 					     attached, false);
550 		if (ret < 0)
551 			return ret;
552 
553 		extcon_set_state_sync(info->edev, EXTCON_USB, attached);
554 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
555 					attached);
556 		break;
557 	case MAX77843_MUIC_CHG_DOWNSTREAM:
558 		ret = max77843_muic_set_path(info,
559 					     MAX77843_MUIC_CONTROL1_SW_OPEN,
560 					     attached, false);
561 		if (ret < 0)
562 			return ret;
563 
564 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP,
565 					attached);
566 		break;
567 	case MAX77843_MUIC_CHG_DEDICATED:
568 		ret = max77843_muic_set_path(info,
569 					     MAX77843_MUIC_CONTROL1_SW_OPEN,
570 					     attached, false);
571 		if (ret < 0)
572 			return ret;
573 
574 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
575 					attached);
576 		break;
577 	case MAX77843_MUIC_CHG_SPECIAL_500MA:
578 		ret = max77843_muic_set_path(info,
579 					     MAX77843_MUIC_CONTROL1_SW_OPEN,
580 					     attached, false);
581 		if (ret < 0)
582 			return ret;
583 
584 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW,
585 					attached);
586 		break;
587 	case MAX77843_MUIC_CHG_SPECIAL_1A:
588 		ret = max77843_muic_set_path(info,
589 					     MAX77843_MUIC_CONTROL1_SW_OPEN,
590 					     attached, false);
591 		if (ret < 0)
592 			return ret;
593 
594 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST,
595 					attached);
596 		break;
597 	case MAX77843_MUIC_CHG_GND:
598 		gnd_type = max77843_muic_get_cable_type(info,
599 				MAX77843_CABLE_GROUP_ADC_GND, &attached);
600 
601 		/* Charger cable on MHL accessory is attach or detach */
602 		if (gnd_type == MAX77843_MUIC_GND_MHL_VB)
603 			extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
604 						true);
605 		else if (gnd_type == MAX77843_MUIC_GND_MHL)
606 			extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
607 						false);
608 		break;
609 	case MAX77843_MUIC_CHG_DOCK:
610 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, attached);
611 		break;
612 	case MAX77843_MUIC_CHG_NONE:
613 		break;
614 	default:
615 		dev_err(info->dev,
616 			"failed to detect %s accessory (chg_type:0x%x)\n",
617 			attached ? "attached" : "detached", chg_type);
618 
619 		max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_OPEN,
620 				       attached, false);
621 		return -EINVAL;
622 	}
623 
624 	return 0;
625 }
626 
627 static void max77843_muic_irq_work(struct work_struct *work)
628 {
629 	struct max77843_muic_info *info = container_of(work,
630 			struct max77843_muic_info, irq_work);
631 	struct max77693_dev *max77843 = info->max77843;
632 	int ret = 0;
633 
634 	mutex_lock(&info->mutex);
635 
636 	ret = regmap_bulk_read(max77843->regmap_muic,
637 			MAX77843_MUIC_REG_STATUS1, info->status,
638 			MAX77843_MUIC_STATUS_NUM);
639 	if (ret) {
640 		dev_err(info->dev, "Cannot read STATUS registers\n");
641 		mutex_unlock(&info->mutex);
642 		return;
643 	}
644 
645 	if (info->irq_adc) {
646 		ret = max77843_muic_adc_handler(info);
647 		if (ret)
648 			dev_err(info->dev, "Unknown cable type\n");
649 		info->irq_adc = false;
650 	}
651 
652 	if (info->irq_chg) {
653 		ret = max77843_muic_chg_handler(info);
654 		if (ret)
655 			dev_err(info->dev, "Unknown charger type\n");
656 		info->irq_chg = false;
657 	}
658 
659 	mutex_unlock(&info->mutex);
660 }
661 
662 static irqreturn_t max77843_muic_irq_handler(int irq, void *data)
663 {
664 	struct max77843_muic_info *info = data;
665 	int i, irq_type = -1;
666 
667 	for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++)
668 		if (irq == max77843_muic_irqs[i].virq)
669 			irq_type = max77843_muic_irqs[i].irq;
670 
671 	switch (irq_type) {
672 	case MAX77843_MUIC_IRQ_INT1_ADC:
673 	case MAX77843_MUIC_IRQ_INT1_ADCERROR:
674 	case MAX77843_MUIC_IRQ_INT1_ADC1K:
675 		info->irq_adc = true;
676 		break;
677 	case MAX77843_MUIC_IRQ_INT2_CHGTYP:
678 	case MAX77843_MUIC_IRQ_INT2_CHGDETRUN:
679 	case MAX77843_MUIC_IRQ_INT2_DCDTMR:
680 	case MAX77843_MUIC_IRQ_INT2_DXOVP:
681 	case MAX77843_MUIC_IRQ_INT2_VBVOLT:
682 		info->irq_chg = true;
683 		break;
684 	case MAX77843_MUIC_IRQ_INT3_VBADC:
685 	case MAX77843_MUIC_IRQ_INT3_VDNMON:
686 	case MAX77843_MUIC_IRQ_INT3_DNRES:
687 	case MAX77843_MUIC_IRQ_INT3_MPNACK:
688 	case MAX77843_MUIC_IRQ_INT3_MRXBUFOW:
689 	case MAX77843_MUIC_IRQ_INT3_MRXTRF:
690 	case MAX77843_MUIC_IRQ_INT3_MRXPERR:
691 	case MAX77843_MUIC_IRQ_INT3_MRXRDY:
692 		break;
693 	default:
694 		dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type);
695 		break;
696 	}
697 
698 	schedule_work(&info->irq_work);
699 
700 	return IRQ_HANDLED;
701 }
702 
703 static void max77843_muic_detect_cable_wq(struct work_struct *work)
704 {
705 	struct max77843_muic_info *info = container_of(to_delayed_work(work),
706 			struct max77843_muic_info, wq_detcable);
707 	struct max77693_dev *max77843 = info->max77843;
708 	int chg_type, adc, ret;
709 	bool attached;
710 
711 	mutex_lock(&info->mutex);
712 
713 	ret = regmap_bulk_read(max77843->regmap_muic,
714 			MAX77843_MUIC_REG_STATUS1, info->status,
715 			MAX77843_MUIC_STATUS_NUM);
716 	if (ret) {
717 		dev_err(info->dev, "Cannot read STATUS registers\n");
718 		goto err_cable_wq;
719 	}
720 
721 	adc = max77843_muic_get_cable_type(info,
722 			MAX77843_CABLE_GROUP_ADC, &attached);
723 	if (attached && adc != MAX77843_MUIC_ADC_OPEN) {
724 		ret = max77843_muic_adc_handler(info);
725 		if (ret < 0) {
726 			dev_err(info->dev, "Cannot detect accessory\n");
727 			goto err_cable_wq;
728 		}
729 	}
730 
731 	chg_type = max77843_muic_get_cable_type(info,
732 			MAX77843_CABLE_GROUP_CHG, &attached);
733 	if (attached && chg_type != MAX77843_MUIC_CHG_NONE) {
734 		ret = max77843_muic_chg_handler(info);
735 		if (ret < 0) {
736 			dev_err(info->dev, "Cannot detect charger accessory\n");
737 			goto err_cable_wq;
738 		}
739 	}
740 
741 err_cable_wq:
742 	mutex_unlock(&info->mutex);
743 }
744 
745 static int max77843_muic_set_debounce_time(struct max77843_muic_info *info,
746 		enum max77843_muic_adc_debounce_time time)
747 {
748 	struct max77693_dev *max77843 = info->max77843;
749 	int ret;
750 
751 	switch (time) {
752 	case MAX77843_DEBOUNCE_TIME_5MS:
753 	case MAX77843_DEBOUNCE_TIME_10MS:
754 	case MAX77843_DEBOUNCE_TIME_25MS:
755 	case MAX77843_DEBOUNCE_TIME_38_62MS:
756 		ret = regmap_update_bits(max77843->regmap_muic,
757 				MAX77843_MUIC_REG_CONTROL4,
758 				MAX77843_MUIC_CONTROL4_ADCDBSET_MASK,
759 				time << MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT);
760 		if (ret < 0) {
761 			dev_err(info->dev, "Cannot write MUIC regmap\n");
762 			return ret;
763 		}
764 		break;
765 	default:
766 		dev_err(info->dev, "Invalid ADC debounce time\n");
767 		return -EINVAL;
768 	}
769 
770 	return 0;
771 }
772 
773 static int max77843_init_muic_regmap(struct max77693_dev *max77843)
774 {
775 	int ret;
776 
777 	max77843->i2c_muic = i2c_new_dummy_device(max77843->i2c->adapter,
778 			I2C_ADDR_MUIC);
779 	if (IS_ERR(max77843->i2c_muic)) {
780 		dev_err(&max77843->i2c->dev,
781 				"Cannot allocate I2C device for MUIC\n");
782 		return PTR_ERR(max77843->i2c_muic);
783 	}
784 
785 	i2c_set_clientdata(max77843->i2c_muic, max77843);
786 
787 	max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic,
788 			&max77843_muic_regmap_config);
789 	if (IS_ERR(max77843->regmap_muic)) {
790 		ret = PTR_ERR(max77843->regmap_muic);
791 		goto err_muic_i2c;
792 	}
793 
794 	ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq,
795 			IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
796 			0, &max77843_muic_irq_chip, &max77843->irq_data_muic);
797 	if (ret < 0) {
798 		dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n");
799 		goto err_muic_i2c;
800 	}
801 
802 	return 0;
803 
804 err_muic_i2c:
805 	i2c_unregister_device(max77843->i2c_muic);
806 
807 	return ret;
808 }
809 
810 static int max77843_muic_probe(struct platform_device *pdev)
811 {
812 	struct max77693_dev *max77843 = dev_get_drvdata(pdev->dev.parent);
813 	struct max77843_muic_info *info;
814 	unsigned int id;
815 	int cable_type;
816 	bool attached;
817 	int i, ret;
818 
819 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
820 	if (!info)
821 		return -ENOMEM;
822 
823 	info->dev = &pdev->dev;
824 	info->max77843 = max77843;
825 
826 	platform_set_drvdata(pdev, info);
827 	mutex_init(&info->mutex);
828 
829 	/* Initialize i2c and regmap */
830 	ret = max77843_init_muic_regmap(max77843);
831 	if (ret) {
832 		dev_err(&pdev->dev, "Failed to init MUIC regmap\n");
833 		return ret;
834 	}
835 
836 	/* Turn off auto detection configuration */
837 	ret = regmap_update_bits(max77843->regmap_muic,
838 			MAX77843_MUIC_REG_CONTROL4,
839 			MAX77843_MUIC_CONTROL4_USBAUTO_MASK |
840 			MAX77843_MUIC_CONTROL4_FCTAUTO_MASK,
841 			CONTROL4_AUTO_DISABLE);
842 
843 	/* Initialize extcon device */
844 	info->edev = devm_extcon_dev_allocate(&pdev->dev,
845 			max77843_extcon_cable);
846 	if (IS_ERR(info->edev)) {
847 		dev_err(&pdev->dev, "Failed to allocate memory for extcon\n");
848 		ret = -ENODEV;
849 		goto err_muic_irq;
850 	}
851 
852 	ret = devm_extcon_dev_register(&pdev->dev, info->edev);
853 	if (ret) {
854 		dev_err(&pdev->dev, "Failed to register extcon device\n");
855 		goto err_muic_irq;
856 	}
857 
858 	/* Set ADC debounce time */
859 	max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS);
860 
861 	/* Set initial path for UART when JIG is connected to get serial logs */
862 	ret = regmap_bulk_read(max77843->regmap_muic,
863 			MAX77843_MUIC_REG_STATUS1, info->status,
864 			MAX77843_MUIC_STATUS_NUM);
865 	if (ret) {
866 		dev_err(info->dev, "Cannot read STATUS registers\n");
867 		goto err_muic_irq;
868 	}
869 	cable_type = max77843_muic_get_cable_type(info, MAX77843_CABLE_GROUP_ADC,
870 					 &attached);
871 	if (attached && cable_type == MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF)
872 		max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_UART,
873 				       true, false);
874 
875 	/* Check revision number of MUIC device */
876 	ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id);
877 	if (ret < 0) {
878 		dev_err(&pdev->dev, "Failed to read revision number\n");
879 		goto err_muic_irq;
880 	}
881 	dev_info(info->dev, "MUIC device ID : 0x%x\n", id);
882 
883 	/* Support virtual irq domain for max77843 MUIC device */
884 	INIT_WORK(&info->irq_work, max77843_muic_irq_work);
885 
886 	/* Clear IRQ bits before request IRQs */
887 	ret = regmap_bulk_read(max77843->regmap_muic,
888 			MAX77843_MUIC_REG_INT1, info->status,
889 			MAX77843_MUIC_STATUS_NUM);
890 	if (ret) {
891 		dev_err(&pdev->dev, "Failed to Clear IRQ bits\n");
892 		goto err_muic_irq;
893 	}
894 
895 	for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) {
896 		struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i];
897 		int virq = 0;
898 
899 		virq = regmap_irq_get_virq(max77843->irq_data_muic,
900 				muic_irq->irq);
901 		if (virq <= 0) {
902 			ret = -EINVAL;
903 			goto err_muic_irq;
904 		}
905 		muic_irq->virq = virq;
906 
907 		ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
908 				max77843_muic_irq_handler, IRQF_NO_SUSPEND,
909 				muic_irq->name, info);
910 		if (ret) {
911 			dev_err(&pdev->dev,
912 				"Failed to request irq (IRQ: %d, error: %d)\n",
913 				muic_irq->irq, ret);
914 			goto err_muic_irq;
915 		}
916 	}
917 
918 	/* Detect accessory after completing the initialization of platform */
919 	INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq);
920 	queue_delayed_work(system_power_efficient_wq,
921 			&info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT));
922 
923 	return 0;
924 
925 err_muic_irq:
926 	regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
927 	i2c_unregister_device(max77843->i2c_muic);
928 
929 	return ret;
930 }
931 
932 static int max77843_muic_remove(struct platform_device *pdev)
933 {
934 	struct max77843_muic_info *info = platform_get_drvdata(pdev);
935 	struct max77693_dev *max77843 = info->max77843;
936 
937 	cancel_work_sync(&info->irq_work);
938 	regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
939 	i2c_unregister_device(max77843->i2c_muic);
940 
941 	return 0;
942 }
943 
944 static const struct platform_device_id max77843_muic_id[] = {
945 	{ "max77843-muic", },
946 	{ /* sentinel */ },
947 };
948 MODULE_DEVICE_TABLE(platform, max77843_muic_id);
949 
950 static struct platform_driver max77843_muic_driver = {
951 	.driver		= {
952 		.name		= "max77843-muic",
953 	},
954 	.probe		= max77843_muic_probe,
955 	.remove		= max77843_muic_remove,
956 	.id_table	= max77843_muic_id,
957 };
958 
959 static int __init max77843_muic_init(void)
960 {
961 	return platform_driver_register(&max77843_muic_driver);
962 }
963 subsys_initcall(max77843_muic_init);
964 
965 MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
966 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
967 MODULE_LICENSE("GPL");
968