1 /*
2  * extcon-max14577.c - MAX14577/77836 extcon driver to support MUIC
3  *
4  * Copyright (C) 2013,2014 Samsung Electronics
5  * Chanwoo Choi <[email protected]>
6  * Krzysztof Kozlowski <[email protected]>
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  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
24 #include <linux/mfd/max14577.h>
25 #include <linux/mfd/max14577-private.h>
26 #include <linux/extcon.h>
27 
28 #define	DELAY_MS_DEFAULT		17000		/* unit: millisecond */
29 
30 enum max14577_muic_adc_debounce_time {
31 	ADC_DEBOUNCE_TIME_5MS = 0,
32 	ADC_DEBOUNCE_TIME_10MS,
33 	ADC_DEBOUNCE_TIME_25MS,
34 	ADC_DEBOUNCE_TIME_38_62MS,
35 };
36 
37 enum max14577_muic_status {
38 	MAX14577_MUIC_STATUS1 = 0,
39 	MAX14577_MUIC_STATUS2 = 1,
40 	MAX14577_MUIC_STATUS_END,
41 };
42 
43 /**
44  * struct max14577_muic_irq
45  * @irq: the index of irq list of MUIC device.
46  * @name: the name of irq.
47  * @virq: the virtual irq to use irq domain
48  */
49 struct max14577_muic_irq {
50 	unsigned int irq;
51 	const char *name;
52 	unsigned int virq;
53 };
54 
55 static struct max14577_muic_irq max14577_muic_irqs[] = {
56 	{ MAX14577_IRQ_INT1_ADC,	"muic-ADC" },
57 	{ MAX14577_IRQ_INT1_ADCLOW,	"muic-ADCLOW" },
58 	{ MAX14577_IRQ_INT1_ADCERR,	"muic-ADCError" },
59 	{ MAX14577_IRQ_INT2_CHGTYP,	"muic-CHGTYP" },
60 	{ MAX14577_IRQ_INT2_CHGDETRUN,	"muic-CHGDETRUN" },
61 	{ MAX14577_IRQ_INT2_DCDTMR,	"muic-DCDTMR" },
62 	{ MAX14577_IRQ_INT2_DBCHG,	"muic-DBCHG" },
63 	{ MAX14577_IRQ_INT2_VBVOLT,	"muic-VBVOLT" },
64 };
65 
66 static struct max14577_muic_irq max77836_muic_irqs[] = {
67 	{ MAX14577_IRQ_INT1_ADC,	"muic-ADC" },
68 	{ MAX14577_IRQ_INT1_ADCLOW,	"muic-ADCLOW" },
69 	{ MAX14577_IRQ_INT1_ADCERR,	"muic-ADCError" },
70 	{ MAX77836_IRQ_INT1_ADC1K,	"muic-ADC1K" },
71 	{ MAX14577_IRQ_INT2_CHGTYP,	"muic-CHGTYP" },
72 	{ MAX14577_IRQ_INT2_CHGDETRUN,	"muic-CHGDETRUN" },
73 	{ MAX14577_IRQ_INT2_DCDTMR,	"muic-DCDTMR" },
74 	{ MAX14577_IRQ_INT2_DBCHG,	"muic-DBCHG" },
75 	{ MAX14577_IRQ_INT2_VBVOLT,	"muic-VBVOLT" },
76 	{ MAX77836_IRQ_INT2_VIDRM,	"muic-VIDRM" },
77 };
78 
79 struct max14577_muic_info {
80 	struct device *dev;
81 	struct max14577 *max14577;
82 	struct extcon_dev *edev;
83 	int prev_cable_type;
84 	int prev_chg_type;
85 	u8 status[MAX14577_MUIC_STATUS_END];
86 
87 	struct max14577_muic_irq *muic_irqs;
88 	unsigned int muic_irqs_num;
89 	bool irq_adc;
90 	bool irq_chg;
91 	struct work_struct irq_work;
92 	struct mutex mutex;
93 
94 	/*
95 	 * Use delayed workqueue to detect cable state and then
96 	 * notify cable state to notifiee/platform through uevent.
97 	 * After completing the booting of platform, the extcon provider
98 	 * driver should notify cable state to upper layer.
99 	 */
100 	struct delayed_work wq_detcable;
101 
102 	/*
103 	 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
104 	 * h/w path of COMP2/COMN1 on CONTROL1 register.
105 	 */
106 	int path_usb;
107 	int path_uart;
108 };
109 
110 enum max14577_muic_cable_group {
111 	MAX14577_CABLE_GROUP_ADC = 0,
112 	MAX14577_CABLE_GROUP_CHG,
113 };
114 
115 /* Define supported accessory type */
116 enum max14577_muic_acc_type {
117 	MAX14577_MUIC_ADC_GROUND = 0x0,
118 	MAX14577_MUIC_ADC_SEND_END_BUTTON,
119 	MAX14577_MUIC_ADC_REMOTE_S1_BUTTON,
120 	MAX14577_MUIC_ADC_REMOTE_S2_BUTTON,
121 	MAX14577_MUIC_ADC_REMOTE_S3_BUTTON,
122 	MAX14577_MUIC_ADC_REMOTE_S4_BUTTON,
123 	MAX14577_MUIC_ADC_REMOTE_S5_BUTTON,
124 	MAX14577_MUIC_ADC_REMOTE_S6_BUTTON,
125 	MAX14577_MUIC_ADC_REMOTE_S7_BUTTON,
126 	MAX14577_MUIC_ADC_REMOTE_S8_BUTTON,
127 	MAX14577_MUIC_ADC_REMOTE_S9_BUTTON,
128 	MAX14577_MUIC_ADC_REMOTE_S10_BUTTON,
129 	MAX14577_MUIC_ADC_REMOTE_S11_BUTTON,
130 	MAX14577_MUIC_ADC_REMOTE_S12_BUTTON,
131 	MAX14577_MUIC_ADC_RESERVED_ACC_1,
132 	MAX14577_MUIC_ADC_RESERVED_ACC_2,
133 	MAX14577_MUIC_ADC_RESERVED_ACC_3,
134 	MAX14577_MUIC_ADC_RESERVED_ACC_4,
135 	MAX14577_MUIC_ADC_RESERVED_ACC_5,
136 	MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2,
137 	MAX14577_MUIC_ADC_PHONE_POWERED_DEV,
138 	MAX14577_MUIC_ADC_TTY_CONVERTER,
139 	MAX14577_MUIC_ADC_UART_CABLE,
140 	MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG,
141 	MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF,
142 	MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON,
143 	MAX14577_MUIC_ADC_AV_CABLE_NOLOAD,
144 	MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG,
145 	MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF,
146 	MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON,
147 	MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1, /* with Remote and Simple Ctrl */
148 	MAX14577_MUIC_ADC_OPEN,
149 };
150 
151 /* max14577 MUIC device support below list of accessories(external connector) */
152 enum {
153 	EXTCON_CABLE_USB = 0,
154 	EXTCON_CABLE_TA,
155 	EXTCON_CABLE_FAST_CHARGER,
156 	EXTCON_CABLE_SLOW_CHARGER,
157 	EXTCON_CABLE_CHARGE_DOWNSTREAM,
158 	EXTCON_CABLE_JIG,
159 
160 	_EXTCON_CABLE_NUM,
161 };
162 
163 static const char *max14577_extcon_cable[] = {
164 	[EXTCON_CABLE_USB]			= "USB",
165 	[EXTCON_CABLE_TA]			= "TA",
166 	[EXTCON_CABLE_FAST_CHARGER]		= "Fast-charger",
167 	[EXTCON_CABLE_SLOW_CHARGER]		= "Slow-charger",
168 	[EXTCON_CABLE_CHARGE_DOWNSTREAM]	= "Charge-downstream",
169 	[EXTCON_CABLE_JIG]			= "JIG",
170 
171 	NULL,
172 };
173 
174 /*
175  * max14577_muic_set_debounce_time - Set the debounce time of ADC
176  * @info: the instance including private data of max14577 MUIC
177  * @time: the debounce time of ADC
178  */
179 static int max14577_muic_set_debounce_time(struct max14577_muic_info *info,
180 		enum max14577_muic_adc_debounce_time time)
181 {
182 	u8 ret;
183 
184 	switch (time) {
185 	case ADC_DEBOUNCE_TIME_5MS:
186 	case ADC_DEBOUNCE_TIME_10MS:
187 	case ADC_DEBOUNCE_TIME_25MS:
188 	case ADC_DEBOUNCE_TIME_38_62MS:
189 		ret = max14577_update_reg(info->max14577->regmap,
190 					  MAX14577_MUIC_REG_CONTROL3,
191 					  CTRL3_ADCDBSET_MASK,
192 					  time << CTRL3_ADCDBSET_SHIFT);
193 		if (ret) {
194 			dev_err(info->dev, "failed to set ADC debounce time\n");
195 			return ret;
196 		}
197 		break;
198 	default:
199 		dev_err(info->dev, "invalid ADC debounce time\n");
200 		return -EINVAL;
201 	}
202 
203 	return 0;
204 };
205 
206 /*
207  * max14577_muic_set_path - Set hardware line according to attached cable
208  * @info: the instance including private data of max14577 MUIC
209  * @value: the path according to attached cable
210  * @attached: the state of cable (true:attached, false:detached)
211  *
212  * The max14577 MUIC device share outside H/W line among a varity of cables
213  * so, this function set internal path of H/W line according to the type of
214  * attached cable.
215  */
216 static int max14577_muic_set_path(struct max14577_muic_info *info,
217 		u8 val, bool attached)
218 {
219 	int ret = 0;
220 	u8 ctrl1, ctrl2 = 0;
221 
222 	/* Set open state to path before changing hw path */
223 	ret = max14577_update_reg(info->max14577->regmap,
224 				MAX14577_MUIC_REG_CONTROL1,
225 				CLEAR_IDBEN_MICEN_MASK, CTRL1_SW_OPEN);
226 	if (ret < 0) {
227 		dev_err(info->dev, "failed to update MUIC register\n");
228 		return ret;
229 	}
230 
231 	if (attached)
232 		ctrl1 = val;
233 	else
234 		ctrl1 = CTRL1_SW_OPEN;
235 
236 	ret = max14577_update_reg(info->max14577->regmap,
237 				MAX14577_MUIC_REG_CONTROL1,
238 				CLEAR_IDBEN_MICEN_MASK, ctrl1);
239 	if (ret < 0) {
240 		dev_err(info->dev, "failed to update MUIC register\n");
241 		return ret;
242 	}
243 
244 	if (attached)
245 		ctrl2 |= CTRL2_CPEN_MASK;	/* LowPwr=0, CPEn=1 */
246 	else
247 		ctrl2 |= CTRL2_LOWPWR_MASK;	/* LowPwr=1, CPEn=0 */
248 
249 	ret = max14577_update_reg(info->max14577->regmap,
250 			MAX14577_REG_CONTROL2,
251 			CTRL2_LOWPWR_MASK | CTRL2_CPEN_MASK, ctrl2);
252 	if (ret < 0) {
253 		dev_err(info->dev, "failed to update MUIC register\n");
254 		return ret;
255 	}
256 
257 	dev_dbg(info->dev,
258 		"CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
259 		ctrl1, ctrl2, attached ? "attached" : "detached");
260 
261 	return 0;
262 }
263 
264 /*
265  * max14577_muic_get_cable_type - Return cable type and check cable state
266  * @info: the instance including private data of max14577 MUIC
267  * @group: the path according to attached cable
268  * @attached: store cable state and return
269  *
270  * This function check the cable state either attached or detached,
271  * and then divide precise type of cable according to cable group.
272  *	- max14577_CABLE_GROUP_ADC
273  *	- max14577_CABLE_GROUP_CHG
274  */
275 static int max14577_muic_get_cable_type(struct max14577_muic_info *info,
276 		enum max14577_muic_cable_group group, bool *attached)
277 {
278 	int cable_type = 0;
279 	int adc;
280 	int chg_type;
281 
282 	switch (group) {
283 	case MAX14577_CABLE_GROUP_ADC:
284 		/*
285 		 * Read ADC value to check cable type and decide cable state
286 		 * according to cable type
287 		 */
288 		adc = info->status[MAX14577_MUIC_STATUS1] & STATUS1_ADC_MASK;
289 		adc >>= STATUS1_ADC_SHIFT;
290 
291 		/*
292 		 * Check current cable state/cable type and store cable type
293 		 * (info->prev_cable_type) for handling cable when cable is
294 		 * detached.
295 		 */
296 		if (adc == MAX14577_MUIC_ADC_OPEN) {
297 			*attached = false;
298 
299 			cable_type = info->prev_cable_type;
300 			info->prev_cable_type = MAX14577_MUIC_ADC_OPEN;
301 		} else {
302 			*attached = true;
303 
304 			cable_type = info->prev_cable_type = adc;
305 		}
306 		break;
307 	case MAX14577_CABLE_GROUP_CHG:
308 		/*
309 		 * Read charger type to check cable type and decide cable state
310 		 * according to type of charger cable.
311 		 */
312 		chg_type = info->status[MAX14577_MUIC_STATUS2] &
313 			STATUS2_CHGTYP_MASK;
314 		chg_type >>= STATUS2_CHGTYP_SHIFT;
315 
316 		if (chg_type == MAX14577_CHARGER_TYPE_NONE) {
317 			*attached = false;
318 
319 			cable_type = info->prev_chg_type;
320 			info->prev_chg_type = MAX14577_CHARGER_TYPE_NONE;
321 		} else {
322 			*attached = true;
323 
324 			/*
325 			 * Check current cable state/cable type and store cable
326 			 * type(info->prev_chg_type) for handling cable when
327 			 * charger cable is detached.
328 			 */
329 			cable_type = info->prev_chg_type = chg_type;
330 		}
331 
332 		break;
333 	default:
334 		dev_err(info->dev, "Unknown cable group (%d)\n", group);
335 		cable_type = -EINVAL;
336 		break;
337 	}
338 
339 	return cable_type;
340 }
341 
342 static int max14577_muic_jig_handler(struct max14577_muic_info *info,
343 		int cable_type, bool attached)
344 {
345 	int ret = 0;
346 	u8 path = CTRL1_SW_OPEN;
347 
348 	dev_dbg(info->dev,
349 		"external connector is %s (adc:0x%02x)\n",
350 		attached ? "attached" : "detached", cable_type);
351 
352 	switch (cable_type) {
353 	case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF:	/* ADC_JIG_USB_OFF */
354 	case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON:	/* ADC_JIG_USB_ON */
355 		/* PATH:AP_USB */
356 		path = CTRL1_SW_USB;
357 		break;
358 	case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF:	/* ADC_JIG_UART_OFF */
359 		/* PATH:AP_UART */
360 		path = CTRL1_SW_UART;
361 		break;
362 	default:
363 		dev_err(info->dev, "failed to detect %s jig cable\n",
364 			attached ? "attached" : "detached");
365 		return -EINVAL;
366 	}
367 
368 	ret = max14577_muic_set_path(info, path, attached);
369 	if (ret < 0)
370 		return ret;
371 
372 	extcon_set_cable_state(info->edev, "JIG", attached);
373 
374 	return 0;
375 }
376 
377 static int max14577_muic_adc_handler(struct max14577_muic_info *info)
378 {
379 	int cable_type;
380 	bool attached;
381 	int ret = 0;
382 
383 	/* Check accessory state which is either detached or attached */
384 	cable_type = max14577_muic_get_cable_type(info,
385 				MAX14577_CABLE_GROUP_ADC, &attached);
386 
387 	dev_dbg(info->dev,
388 		"external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
389 		attached ? "attached" : "detached", cable_type,
390 		info->prev_cable_type);
391 
392 	switch (cable_type) {
393 	case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF:
394 	case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON:
395 	case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF:
396 		/* JIG */
397 		ret = max14577_muic_jig_handler(info, cable_type, attached);
398 		if (ret < 0)
399 			return ret;
400 		break;
401 	case MAX14577_MUIC_ADC_GROUND:
402 	case MAX14577_MUIC_ADC_SEND_END_BUTTON:
403 	case MAX14577_MUIC_ADC_REMOTE_S1_BUTTON:
404 	case MAX14577_MUIC_ADC_REMOTE_S2_BUTTON:
405 	case MAX14577_MUIC_ADC_REMOTE_S3_BUTTON:
406 	case MAX14577_MUIC_ADC_REMOTE_S4_BUTTON:
407 	case MAX14577_MUIC_ADC_REMOTE_S5_BUTTON:
408 	case MAX14577_MUIC_ADC_REMOTE_S6_BUTTON:
409 	case MAX14577_MUIC_ADC_REMOTE_S7_BUTTON:
410 	case MAX14577_MUIC_ADC_REMOTE_S8_BUTTON:
411 	case MAX14577_MUIC_ADC_REMOTE_S9_BUTTON:
412 	case MAX14577_MUIC_ADC_REMOTE_S10_BUTTON:
413 	case MAX14577_MUIC_ADC_REMOTE_S11_BUTTON:
414 	case MAX14577_MUIC_ADC_REMOTE_S12_BUTTON:
415 	case MAX14577_MUIC_ADC_RESERVED_ACC_1:
416 	case MAX14577_MUIC_ADC_RESERVED_ACC_2:
417 	case MAX14577_MUIC_ADC_RESERVED_ACC_3:
418 	case MAX14577_MUIC_ADC_RESERVED_ACC_4:
419 	case MAX14577_MUIC_ADC_RESERVED_ACC_5:
420 	case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2:
421 	case MAX14577_MUIC_ADC_PHONE_POWERED_DEV:
422 	case MAX14577_MUIC_ADC_TTY_CONVERTER:
423 	case MAX14577_MUIC_ADC_UART_CABLE:
424 	case MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG:
425 	case MAX14577_MUIC_ADC_AV_CABLE_NOLOAD:
426 	case MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG:
427 	case MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON:
428 	case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1:
429 		/*
430 		 * This accessory isn't used in general case if it is specially
431 		 * needed to detect additional accessory, should implement
432 		 * proper operation when this accessory is attached/detached.
433 		 */
434 		dev_info(info->dev,
435 			"accessory is %s but it isn't used (adc:0x%x)\n",
436 			attached ? "attached" : "detached", cable_type);
437 		return -EAGAIN;
438 	default:
439 		dev_err(info->dev,
440 			"failed to detect %s accessory (adc:0x%x)\n",
441 			attached ? "attached" : "detached", cable_type);
442 		return -EINVAL;
443 	}
444 
445 	return 0;
446 }
447 
448 static int max14577_muic_chg_handler(struct max14577_muic_info *info)
449 {
450 	int chg_type;
451 	bool attached;
452 	int ret = 0;
453 
454 	chg_type = max14577_muic_get_cable_type(info,
455 				MAX14577_CABLE_GROUP_CHG, &attached);
456 
457 	dev_dbg(info->dev,
458 		"external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
459 			attached ? "attached" : "detached",
460 			chg_type, info->prev_chg_type);
461 
462 	switch (chg_type) {
463 	case MAX14577_CHARGER_TYPE_USB:
464 		/* PATH:AP_USB */
465 		ret = max14577_muic_set_path(info, info->path_usb, attached);
466 		if (ret < 0)
467 			return ret;
468 
469 		extcon_set_cable_state(info->edev, "USB", attached);
470 		break;
471 	case MAX14577_CHARGER_TYPE_DEDICATED_CHG:
472 		extcon_set_cable_state(info->edev, "TA", attached);
473 		break;
474 	case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT:
475 		extcon_set_cable_state(info->edev,
476 				"Charge-downstream", attached);
477 		break;
478 	case MAX14577_CHARGER_TYPE_SPECIAL_500MA:
479 		extcon_set_cable_state(info->edev, "Slow-charger", attached);
480 		break;
481 	case MAX14577_CHARGER_TYPE_SPECIAL_1A:
482 		extcon_set_cable_state(info->edev, "Fast-charger", attached);
483 		break;
484 	case MAX14577_CHARGER_TYPE_NONE:
485 	case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
486 		break;
487 	default:
488 		dev_err(info->dev,
489 			"failed to detect %s accessory (chg_type:0x%x)\n",
490 			attached ? "attached" : "detached", chg_type);
491 		return -EINVAL;
492 	}
493 
494 	return 0;
495 }
496 
497 static void max14577_muic_irq_work(struct work_struct *work)
498 {
499 	struct max14577_muic_info *info = container_of(work,
500 			struct max14577_muic_info, irq_work);
501 	int ret = 0;
502 
503 	if (!info->edev)
504 		return;
505 
506 	mutex_lock(&info->mutex);
507 
508 	ret = max14577_bulk_read(info->max14577->regmap,
509 			MAX14577_MUIC_REG_STATUS1, info->status, 2);
510 	if (ret) {
511 		dev_err(info->dev, "failed to read MUIC register\n");
512 		mutex_unlock(&info->mutex);
513 		return;
514 	}
515 
516 	if (info->irq_adc) {
517 		ret = max14577_muic_adc_handler(info);
518 		info->irq_adc = false;
519 	}
520 	if (info->irq_chg) {
521 		ret = max14577_muic_chg_handler(info);
522 		info->irq_chg = false;
523 	}
524 
525 	if (ret < 0)
526 		dev_err(info->dev, "failed to handle MUIC interrupt\n");
527 
528 	mutex_unlock(&info->mutex);
529 }
530 
531 /*
532  * Sets irq_adc or irq_chg in max14577_muic_info and returns 1.
533  * Returns 0 if irq_type does not match registered IRQ for this device type.
534  */
535 static int max14577_parse_irq(struct max14577_muic_info *info, int irq_type)
536 {
537 	switch (irq_type) {
538 	case MAX14577_IRQ_INT1_ADC:
539 	case MAX14577_IRQ_INT1_ADCLOW:
540 	case MAX14577_IRQ_INT1_ADCERR:
541 		/* Handle all of accessory except for
542 		   type of charger accessory */
543 		info->irq_adc = true;
544 		return 1;
545 	case MAX14577_IRQ_INT2_CHGTYP:
546 	case MAX14577_IRQ_INT2_CHGDETRUN:
547 	case MAX14577_IRQ_INT2_DCDTMR:
548 	case MAX14577_IRQ_INT2_DBCHG:
549 	case MAX14577_IRQ_INT2_VBVOLT:
550 		/* Handle charger accessory */
551 		info->irq_chg = true;
552 		return 1;
553 	default:
554 		return 0;
555 	}
556 }
557 
558 /*
559  * Sets irq_adc or irq_chg in max14577_muic_info and returns 1.
560  * Returns 0 if irq_type does not match registered IRQ for this device type.
561  */
562 static int max77836_parse_irq(struct max14577_muic_info *info, int irq_type)
563 {
564 	/* First check common max14577 interrupts */
565 	if (max14577_parse_irq(info, irq_type))
566 		return 1;
567 
568 	switch (irq_type) {
569 	case MAX77836_IRQ_INT1_ADC1K:
570 		info->irq_adc = true;
571 		return 1;
572 	case MAX77836_IRQ_INT2_VIDRM:
573 		/* Handle charger accessory */
574 		info->irq_chg = true;
575 		return 1;
576 	default:
577 		return 0;
578 	}
579 }
580 
581 static irqreturn_t max14577_muic_irq_handler(int irq, void *data)
582 {
583 	struct max14577_muic_info *info = data;
584 	int i, irq_type = -1;
585 	bool irq_parsed;
586 
587 	/*
588 	 * We may be called multiple times for different nested IRQ-s.
589 	 * Including changes in INT1_ADC and INT2_CGHTYP at once.
590 	 * However we only need to know whether it was ADC, charger
591 	 * or both interrupts so decode IRQ and turn on proper flags.
592 	 */
593 	for (i = 0; i < info->muic_irqs_num; i++)
594 		if (irq == info->muic_irqs[i].virq)
595 			irq_type = info->muic_irqs[i].irq;
596 
597 	switch (info->max14577->dev_type) {
598 	case MAXIM_DEVICE_TYPE_MAX77836:
599 		irq_parsed = max77836_parse_irq(info, irq_type);
600 		break;
601 	case MAXIM_DEVICE_TYPE_MAX14577:
602 	default:
603 		irq_parsed = max14577_parse_irq(info, irq_type);
604 		break;
605 	}
606 
607 	if (!irq_parsed) {
608 		dev_err(info->dev, "muic interrupt: irq %d occurred, skipped\n",
609 				irq_type);
610 		return IRQ_HANDLED;
611 	}
612 	schedule_work(&info->irq_work);
613 
614 	return IRQ_HANDLED;
615 }
616 
617 static int max14577_muic_detect_accessory(struct max14577_muic_info *info)
618 {
619 	int ret = 0;
620 	int adc;
621 	int chg_type;
622 	bool attached;
623 
624 	mutex_lock(&info->mutex);
625 
626 	/* Read STATUSx register to detect accessory */
627 	ret = max14577_bulk_read(info->max14577->regmap,
628 			MAX14577_MUIC_REG_STATUS1, info->status, 2);
629 	if (ret) {
630 		dev_err(info->dev, "failed to read MUIC register\n");
631 		mutex_unlock(&info->mutex);
632 		return ret;
633 	}
634 
635 	adc = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_ADC,
636 					&attached);
637 	if (attached && adc != MAX14577_MUIC_ADC_OPEN) {
638 		ret = max14577_muic_adc_handler(info);
639 		if (ret < 0) {
640 			dev_err(info->dev, "Cannot detect accessory\n");
641 			mutex_unlock(&info->mutex);
642 			return ret;
643 		}
644 	}
645 
646 	chg_type = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_CHG,
647 					&attached);
648 	if (attached && chg_type != MAX14577_CHARGER_TYPE_NONE) {
649 		ret = max14577_muic_chg_handler(info);
650 		if (ret < 0) {
651 			dev_err(info->dev, "Cannot detect charger accessory\n");
652 			mutex_unlock(&info->mutex);
653 			return ret;
654 		}
655 	}
656 
657 	mutex_unlock(&info->mutex);
658 
659 	return 0;
660 }
661 
662 static void max14577_muic_detect_cable_wq(struct work_struct *work)
663 {
664 	struct max14577_muic_info *info = container_of(to_delayed_work(work),
665 				struct max14577_muic_info, wq_detcable);
666 
667 	max14577_muic_detect_accessory(info);
668 }
669 
670 static int max14577_muic_probe(struct platform_device *pdev)
671 {
672 	struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
673 	struct max14577_muic_info *info;
674 	int delay_jiffies;
675 	int ret;
676 	int i;
677 	u8 id;
678 
679 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
680 	if (!info)
681 		return -ENOMEM;
682 
683 	info->dev = &pdev->dev;
684 	info->max14577 = max14577;
685 
686 	platform_set_drvdata(pdev, info);
687 	mutex_init(&info->mutex);
688 
689 	INIT_WORK(&info->irq_work, max14577_muic_irq_work);
690 
691 	switch (max14577->dev_type) {
692 	case MAXIM_DEVICE_TYPE_MAX77836:
693 		info->muic_irqs = max77836_muic_irqs;
694 		info->muic_irqs_num = ARRAY_SIZE(max77836_muic_irqs);
695 		break;
696 	case MAXIM_DEVICE_TYPE_MAX14577:
697 	default:
698 		info->muic_irqs = max14577_muic_irqs;
699 		info->muic_irqs_num = ARRAY_SIZE(max14577_muic_irqs);
700 	}
701 
702 	/* Support irq domain for max14577 MUIC device */
703 	for (i = 0; i < info->muic_irqs_num; i++) {
704 		struct max14577_muic_irq *muic_irq = &info->muic_irqs[i];
705 		unsigned int virq = 0;
706 
707 		virq = regmap_irq_get_virq(max14577->irq_data, muic_irq->irq);
708 		if (virq <= 0)
709 			return -EINVAL;
710 		muic_irq->virq = virq;
711 
712 		ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
713 				max14577_muic_irq_handler,
714 				IRQF_NO_SUSPEND,
715 				muic_irq->name, info);
716 		if (ret) {
717 			dev_err(&pdev->dev,
718 				"failed: irq request (IRQ: %d, error :%d)\n",
719 				muic_irq->irq, ret);
720 			return ret;
721 		}
722 	}
723 
724 	/* Initialize extcon device */
725 	info->edev = devm_extcon_dev_allocate(&pdev->dev,
726 					      max14577_extcon_cable);
727 	if (IS_ERR(info->edev)) {
728 		dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
729 		return -ENOMEM;
730 	}
731 
732 	ret = devm_extcon_dev_register(&pdev->dev, info->edev);
733 	if (ret) {
734 		dev_err(&pdev->dev, "failed to register extcon device\n");
735 		return ret;
736 	}
737 
738 	/* Default h/w line path */
739 	info->path_usb = CTRL1_SW_USB;
740 	info->path_uart = CTRL1_SW_UART;
741 	delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
742 
743 	/* Set initial path for UART */
744 	max14577_muic_set_path(info, info->path_uart, true);
745 
746 	/* Check revision number of MUIC device*/
747 	ret = max14577_read_reg(info->max14577->regmap,
748 			MAX14577_REG_DEVICEID, &id);
749 	if (ret < 0) {
750 		dev_err(&pdev->dev, "failed to read revision number\n");
751 		return ret;
752 	}
753 	dev_info(info->dev, "device ID : 0x%x\n", id);
754 
755 	/* Set ADC debounce time */
756 	max14577_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
757 
758 	/*
759 	 * Detect accessory after completing the initialization of platform
760 	 *
761 	 * - Use delayed workqueue to detect cable state and then
762 	 * notify cable state to notifiee/platform through uevent.
763 	 * After completing the booting of platform, the extcon provider
764 	 * driver should notify cable state to upper layer.
765 	 */
766 	INIT_DELAYED_WORK(&info->wq_detcable, max14577_muic_detect_cable_wq);
767 	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
768 			delay_jiffies);
769 
770 	return ret;
771 }
772 
773 static int max14577_muic_remove(struct platform_device *pdev)
774 {
775 	struct max14577_muic_info *info = platform_get_drvdata(pdev);
776 
777 	cancel_work_sync(&info->irq_work);
778 
779 	return 0;
780 }
781 
782 static const struct platform_device_id max14577_muic_id[] = {
783 	{ "max14577-muic", MAXIM_DEVICE_TYPE_MAX14577, },
784 	{ "max77836-muic", MAXIM_DEVICE_TYPE_MAX77836, },
785 	{ }
786 };
787 MODULE_DEVICE_TABLE(platform, max14577_muic_id);
788 
789 static struct platform_driver max14577_muic_driver = {
790 	.driver		= {
791 		.name	= "max14577-muic",
792 	},
793 	.probe		= max14577_muic_probe,
794 	.remove		= max14577_muic_remove,
795 	.id_table	= max14577_muic_id,
796 };
797 
798 module_platform_driver(max14577_muic_driver);
799 
800 MODULE_DESCRIPTION("Maxim 14577/77836 Extcon driver");
801 MODULE_AUTHOR("Chanwoo Choi <[email protected]>, Krzysztof Kozlowski <[email protected]>");
802 MODULE_LICENSE("GPL");
803 MODULE_ALIAS("platform:extcon-max14577");
804