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