1 /*
2  * extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
3  *
4  *  Copyright (C) 2012 Samsung Electronics
5  *  Donggeun Kim <dg77.kim@samsung.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/i2c.h>
21 #include <linux/slab.h>
22 #include <linux/interrupt.h>
23 #include <linux/err.h>
24 #include <linux/platform_device.h>
25 #include <linux/kobject.h>
26 #include <linux/mfd/max8997.h>
27 #include <linux/mfd/max8997-private.h>
28 #include <linux/extcon.h>
29 #include <linux/irqdomain.h>
30 
31 #define	DEV_NAME			"max8997-muic"
32 #define	DELAY_MS_DEFAULT		20000		/* unit: millisecond */
33 
34 enum max8997_muic_adc_debounce_time {
35 	ADC_DEBOUNCE_TIME_0_5MS = 0,	/* 0.5ms */
36 	ADC_DEBOUNCE_TIME_10MS,		/* 10ms */
37 	ADC_DEBOUNCE_TIME_25MS,		/* 25ms */
38 	ADC_DEBOUNCE_TIME_38_62MS,	/* 38.62ms */
39 };
40 
41 struct max8997_muic_irq {
42 	unsigned int irq;
43 	const char *name;
44 	unsigned int virq;
45 };
46 
47 static struct max8997_muic_irq muic_irqs[] = {
48 	{ MAX8997_MUICIRQ_ADCError,	"muic-ADCERROR" },
49 	{ MAX8997_MUICIRQ_ADCLow,	"muic-ADCLOW" },
50 	{ MAX8997_MUICIRQ_ADC,		"muic-ADC" },
51 	{ MAX8997_MUICIRQ_VBVolt,	"muic-VBVOLT" },
52 	{ MAX8997_MUICIRQ_DBChg,	"muic-DBCHG" },
53 	{ MAX8997_MUICIRQ_DCDTmr,	"muic-DCDTMR" },
54 	{ MAX8997_MUICIRQ_ChgDetRun,	"muic-CHGDETRUN" },
55 	{ MAX8997_MUICIRQ_ChgTyp,	"muic-CHGTYP" },
56 	{ MAX8997_MUICIRQ_OVP,		"muic-OVP" },
57 };
58 
59 /* Define supported cable type */
60 enum max8997_muic_acc_type {
61 	MAX8997_MUIC_ADC_GROUND = 0x0,
62 	MAX8997_MUIC_ADC_MHL,			/* MHL*/
63 	MAX8997_MUIC_ADC_REMOTE_S1_BUTTON,
64 	MAX8997_MUIC_ADC_REMOTE_S2_BUTTON,
65 	MAX8997_MUIC_ADC_REMOTE_S3_BUTTON,
66 	MAX8997_MUIC_ADC_REMOTE_S4_BUTTON,
67 	MAX8997_MUIC_ADC_REMOTE_S5_BUTTON,
68 	MAX8997_MUIC_ADC_REMOTE_S6_BUTTON,
69 	MAX8997_MUIC_ADC_REMOTE_S7_BUTTON,
70 	MAX8997_MUIC_ADC_REMOTE_S8_BUTTON,
71 	MAX8997_MUIC_ADC_REMOTE_S9_BUTTON,
72 	MAX8997_MUIC_ADC_REMOTE_S10_BUTTON,
73 	MAX8997_MUIC_ADC_REMOTE_S11_BUTTON,
74 	MAX8997_MUIC_ADC_REMOTE_S12_BUTTON,
75 	MAX8997_MUIC_ADC_RESERVED_ACC_1,
76 	MAX8997_MUIC_ADC_RESERVED_ACC_2,
77 	MAX8997_MUIC_ADC_RESERVED_ACC_3,
78 	MAX8997_MUIC_ADC_RESERVED_ACC_4,
79 	MAX8997_MUIC_ADC_RESERVED_ACC_5,
80 	MAX8997_MUIC_ADC_CEA936_AUDIO,
81 	MAX8997_MUIC_ADC_PHONE_POWERED_DEV,
82 	MAX8997_MUIC_ADC_TTY_CONVERTER,
83 	MAX8997_MUIC_ADC_UART_CABLE,
84 	MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG,
85 	MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF,	/* JIG-USB-OFF */
86 	MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON,	/* JIG-USB-ON */
87 	MAX8997_MUIC_ADC_AV_CABLE_NOLOAD,	/* DESKDOCK */
88 	MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG,
89 	MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF,	/* JIG-UART */
90 	MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON,	/* CARDOCK */
91 	MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE,
92 	MAX8997_MUIC_ADC_OPEN,			/* OPEN */
93 };
94 
95 enum max8997_muic_cable_group {
96 	MAX8997_CABLE_GROUP_ADC = 0,
97 	MAX8997_CABLE_GROUP_ADC_GND,
98 	MAX8997_CABLE_GROUP_CHG,
99 	MAX8997_CABLE_GROUP_VBVOLT,
100 };
101 
102 enum max8997_muic_usb_type {
103 	MAX8997_USB_HOST,
104 	MAX8997_USB_DEVICE,
105 };
106 
107 enum max8997_muic_charger_type {
108 	MAX8997_CHARGER_TYPE_NONE = 0,
109 	MAX8997_CHARGER_TYPE_USB,
110 	MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT,
111 	MAX8997_CHARGER_TYPE_DEDICATED_CHG,
112 	MAX8997_CHARGER_TYPE_500MA,
113 	MAX8997_CHARGER_TYPE_1A,
114 	MAX8997_CHARGER_TYPE_DEAD_BATTERY = 7,
115 };
116 
117 struct max8997_muic_info {
118 	struct device *dev;
119 	struct i2c_client *muic;
120 	struct extcon_dev *edev;
121 	int prev_cable_type;
122 	int prev_chg_type;
123 	u8 status[2];
124 
125 	int irq;
126 	struct work_struct irq_work;
127 	struct mutex mutex;
128 
129 	struct max8997_muic_platform_data *muic_pdata;
130 	enum max8997_muic_charger_type pre_charger_type;
131 
132 	/*
133 	 * Use delayed workqueue to detect cable state and then
134 	 * notify cable state to notifiee/platform through uevent.
135 	 * After completing the booting of platform, the extcon provider
136 	 * driver should notify cable state to upper layer.
137 	 */
138 	struct delayed_work wq_detcable;
139 
140 	/*
141 	 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
142 	 * h/w path of COMP2/COMN1 on CONTROL1 register.
143 	 */
144 	int path_usb;
145 	int path_uart;
146 };
147 
148 enum {
149 	EXTCON_CABLE_USB = 0,
150 	EXTCON_CABLE_USB_HOST,
151 	EXTCON_CABLE_TA,
152 	EXTCON_CABLE_FAST_CHARGER,
153 	EXTCON_CABLE_SLOW_CHARGER,
154 	EXTCON_CABLE_CHARGE_DOWNSTREAM,
155 	EXTCON_CABLE_MHL,
156 	EXTCON_CABLE_DOCK_DESK,
157 	EXTCON_CABLE_DOCK_CARD,
158 	EXTCON_CABLE_JIG,
159 
160 	_EXTCON_CABLE_NUM,
161 };
162 
163 static const char *max8997_extcon_cable[] = {
164 	[EXTCON_CABLE_USB]			= "USB",
165 	[EXTCON_CABLE_USB_HOST]			= "USB-Host",
166 	[EXTCON_CABLE_TA]			= "TA",
167 	[EXTCON_CABLE_FAST_CHARGER]		= "Fast-charger",
168 	[EXTCON_CABLE_SLOW_CHARGER]		= "Slow-charger",
169 	[EXTCON_CABLE_CHARGE_DOWNSTREAM]	= "Charge-downstream",
170 	[EXTCON_CABLE_MHL]			= "MHL",
171 	[EXTCON_CABLE_DOCK_DESK]		= "Dock-Desk",
172 	[EXTCON_CABLE_DOCK_CARD]		= "Dock-Card",
173 	[EXTCON_CABLE_JIG]			= "JIG",
174 
175 	NULL,
176 };
177 
178 /*
179  * max8997_muic_set_debounce_time - Set the debounce time of ADC
180  * @info: the instance including private data of max8997 MUIC
181  * @time: the debounce time of ADC
182  */
183 static int max8997_muic_set_debounce_time(struct max8997_muic_info *info,
184 		enum max8997_muic_adc_debounce_time time)
185 {
186 	int ret;
187 
188 	switch (time) {
189 	case ADC_DEBOUNCE_TIME_0_5MS:
190 	case ADC_DEBOUNCE_TIME_10MS:
191 	case ADC_DEBOUNCE_TIME_25MS:
192 	case ADC_DEBOUNCE_TIME_38_62MS:
193 		ret = max8997_update_reg(info->muic,
194 					  MAX8997_MUIC_REG_CONTROL3,
195 					  time << CONTROL3_ADCDBSET_SHIFT,
196 					  CONTROL3_ADCDBSET_MASK);
197 		if (ret) {
198 			dev_err(info->dev, "failed to set ADC debounce time\n");
199 			return ret;
200 		}
201 		break;
202 	default:
203 		dev_err(info->dev, "invalid ADC debounce time\n");
204 		return -EINVAL;
205 	}
206 
207 	return 0;
208 };
209 
210 /*
211  * max8997_muic_set_path - Set hardware line according to attached cable
212  * @info: the instance including private data of max8997 MUIC
213  * @value: the path according to attached cable
214  * @attached: the state of cable (true:attached, false:detached)
215  *
216  * The max8997 MUIC device share outside H/W line among a varity of cables,
217  * so this function set internal path of H/W line according to the type of
218  * attached cable.
219  */
220 static int max8997_muic_set_path(struct max8997_muic_info *info,
221 		u8 val, bool attached)
222 {
223 	int ret = 0;
224 	u8 ctrl1, ctrl2 = 0;
225 
226 	if (attached)
227 		ctrl1 = val;
228 	else
229 		ctrl1 = CONTROL1_SW_OPEN;
230 
231 	ret = max8997_update_reg(info->muic,
232 			MAX8997_MUIC_REG_CONTROL1, ctrl1, COMP_SW_MASK);
233 	if (ret < 0) {
234 		dev_err(info->dev, "failed to update MUIC register\n");
235 		return ret;
236 	}
237 
238 	if (attached)
239 		ctrl2 |= CONTROL2_CPEN_MASK;	/* LowPwr=0, CPEn=1 */
240 	else
241 		ctrl2 |= CONTROL2_LOWPWR_MASK;	/* LowPwr=1, CPEn=0 */
242 
243 	ret = max8997_update_reg(info->muic,
244 			MAX8997_MUIC_REG_CONTROL2, ctrl2,
245 			CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
246 	if (ret < 0) {
247 		dev_err(info->dev, "failed to update MUIC register\n");
248 		return ret;
249 	}
250 
251 	dev_info(info->dev,
252 		"CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
253 		ctrl1, ctrl2, attached ? "attached" : "detached");
254 
255 	return 0;
256 }
257 
258 /*
259  * max8997_muic_get_cable_type - Return cable type and check cable state
260  * @info: the instance including private data of max8997 MUIC
261  * @group: the path according to attached cable
262  * @attached: store cable state and return
263  *
264  * This function check the cable state either attached or detached,
265  * and then divide precise type of cable according to cable group.
266  *	- MAX8997_CABLE_GROUP_ADC
267  *	- MAX8997_CABLE_GROUP_CHG
268  */
269 static int max8997_muic_get_cable_type(struct max8997_muic_info *info,
270 		enum max8997_muic_cable_group group, bool *attached)
271 {
272 	int cable_type = 0;
273 	int adc;
274 	int chg_type;
275 
276 	switch (group) {
277 	case MAX8997_CABLE_GROUP_ADC:
278 		/*
279 		 * Read ADC value to check cable type and decide cable state
280 		 * according to cable type
281 		 */
282 		adc = info->status[0] & STATUS1_ADC_MASK;
283 		adc >>= STATUS1_ADC_SHIFT;
284 
285 		/*
286 		 * Check current cable state/cable type and store cable type
287 		 * (info->prev_cable_type) for handling cable when cable is
288 		 * detached.
289 		 */
290 		if (adc == MAX8997_MUIC_ADC_OPEN) {
291 			*attached = false;
292 
293 			cable_type = info->prev_cable_type;
294 			info->prev_cable_type = MAX8997_MUIC_ADC_OPEN;
295 		} else {
296 			*attached = true;
297 
298 			cable_type = info->prev_cable_type = adc;
299 		}
300 		break;
301 	case MAX8997_CABLE_GROUP_CHG:
302 		/*
303 		 * Read charger type to check cable type and decide cable state
304 		 * according to type of charger cable.
305 		 */
306 		chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
307 		chg_type >>= STATUS2_CHGTYP_SHIFT;
308 
309 		if (chg_type == MAX8997_CHARGER_TYPE_NONE) {
310 			*attached = false;
311 
312 			cable_type = info->prev_chg_type;
313 			info->prev_chg_type = MAX8997_CHARGER_TYPE_NONE;
314 		} else {
315 			*attached = true;
316 
317 			/*
318 			 * Check current cable state/cable type and store cable
319 			 * type(info->prev_chg_type) for handling cable when
320 			 * charger cable is detached.
321 			 */
322 			cable_type = info->prev_chg_type = chg_type;
323 		}
324 
325 		break;
326 	default:
327 		dev_err(info->dev, "Unknown cable group (%d)\n", group);
328 		cable_type = -EINVAL;
329 		break;
330 	}
331 
332 	return cable_type;
333 }
334 
335 static int max8997_muic_handle_usb(struct max8997_muic_info *info,
336 			enum max8997_muic_usb_type usb_type, bool attached)
337 {
338 	int ret = 0;
339 
340 	if (usb_type == MAX8997_USB_HOST) {
341 		ret = max8997_muic_set_path(info, info->path_usb, attached);
342 		if (ret < 0) {
343 			dev_err(info->dev, "failed to update muic register\n");
344 			return ret;
345 		}
346 	}
347 
348 	switch (usb_type) {
349 	case MAX8997_USB_HOST:
350 		extcon_set_cable_state(info->edev, "USB-Host", attached);
351 		break;
352 	case MAX8997_USB_DEVICE:
353 		extcon_set_cable_state(info->edev, "USB", attached);
354 		break;
355 	default:
356 		dev_err(info->dev, "failed to detect %s usb cable\n",
357 			attached ? "attached" : "detached");
358 		return -EINVAL;
359 	}
360 
361 	return 0;
362 }
363 
364 static int max8997_muic_handle_dock(struct max8997_muic_info *info,
365 			int cable_type, bool attached)
366 {
367 	int ret = 0;
368 
369 	ret = max8997_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
370 	if (ret) {
371 		dev_err(info->dev, "failed to update muic register\n");
372 		return ret;
373 	}
374 
375 	switch (cable_type) {
376 	case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
377 		extcon_set_cable_state(info->edev, "Dock-desk", attached);
378 		break;
379 	case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
380 		extcon_set_cable_state(info->edev, "Dock-card", attached);
381 		break;
382 	default:
383 		dev_err(info->dev, "failed to detect %s dock device\n",
384 			attached ? "attached" : "detached");
385 		return -EINVAL;
386 	}
387 
388 	return 0;
389 }
390 
391 static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info,
392 			bool attached)
393 {
394 	int ret = 0;
395 
396 	/* switch to UART */
397 	ret = max8997_muic_set_path(info, info->path_uart, attached);
398 	if (ret) {
399 		dev_err(info->dev, "failed to update muic register\n");
400 		return ret;
401 	}
402 
403 	extcon_set_cable_state(info->edev, "JIG", attached);
404 
405 	return 0;
406 }
407 
408 static int max8997_muic_adc_handler(struct max8997_muic_info *info)
409 {
410 	int cable_type;
411 	bool attached;
412 	int ret = 0;
413 
414 	/* Check cable state which is either detached or attached */
415 	cable_type = max8997_muic_get_cable_type(info,
416 				MAX8997_CABLE_GROUP_ADC, &attached);
417 
418 	switch (cable_type) {
419 	case MAX8997_MUIC_ADC_GROUND:
420 		ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, attached);
421 		if (ret < 0)
422 			return ret;
423 		break;
424 	case MAX8997_MUIC_ADC_MHL:
425 		extcon_set_cable_state(info->edev, "MHL", attached);
426 		break;
427 	case MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF:
428 	case MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON:
429 		ret = max8997_muic_handle_usb(info,
430 					     MAX8997_USB_DEVICE, attached);
431 		if (ret < 0)
432 			return ret;
433 		break;
434 	case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
435 	case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
436 		ret = max8997_muic_handle_dock(info, cable_type, attached);
437 		if (ret < 0)
438 			return ret;
439 		break;
440 	case MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF:
441 		ret = max8997_muic_handle_jig_uart(info, attached);
442 		break;
443 	case MAX8997_MUIC_ADC_REMOTE_S1_BUTTON:
444 	case MAX8997_MUIC_ADC_REMOTE_S2_BUTTON:
445 	case MAX8997_MUIC_ADC_REMOTE_S3_BUTTON:
446 	case MAX8997_MUIC_ADC_REMOTE_S4_BUTTON:
447 	case MAX8997_MUIC_ADC_REMOTE_S5_BUTTON:
448 	case MAX8997_MUIC_ADC_REMOTE_S6_BUTTON:
449 	case MAX8997_MUIC_ADC_REMOTE_S7_BUTTON:
450 	case MAX8997_MUIC_ADC_REMOTE_S8_BUTTON:
451 	case MAX8997_MUIC_ADC_REMOTE_S9_BUTTON:
452 	case MAX8997_MUIC_ADC_REMOTE_S10_BUTTON:
453 	case MAX8997_MUIC_ADC_REMOTE_S11_BUTTON:
454 	case MAX8997_MUIC_ADC_REMOTE_S12_BUTTON:
455 	case MAX8997_MUIC_ADC_RESERVED_ACC_1:
456 	case MAX8997_MUIC_ADC_RESERVED_ACC_2:
457 	case MAX8997_MUIC_ADC_RESERVED_ACC_3:
458 	case MAX8997_MUIC_ADC_RESERVED_ACC_4:
459 	case MAX8997_MUIC_ADC_RESERVED_ACC_5:
460 	case MAX8997_MUIC_ADC_CEA936_AUDIO:
461 	case MAX8997_MUIC_ADC_PHONE_POWERED_DEV:
462 	case MAX8997_MUIC_ADC_TTY_CONVERTER:
463 	case MAX8997_MUIC_ADC_UART_CABLE:
464 	case MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG:
465 	case MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG:
466 	case MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE:
467 		/*
468 		 * This cable isn't used in general case if it is specially
469 		 * needed to detect additional cable, should implement
470 		 * proper operation when this cable is attached/detached.
471 		 */
472 		dev_info(info->dev,
473 			"cable is %s but it isn't used (type:0x%x)\n",
474 			attached ? "attached" : "detached", cable_type);
475 		return -EAGAIN;
476 	default:
477 		dev_err(info->dev,
478 			"failed to detect %s unknown cable (type:0x%x)\n",
479 			attached ? "attached" : "detached", cable_type);
480 		return -EINVAL;
481 	}
482 
483 	return 0;
484 }
485 
486 static int max8997_muic_chg_handler(struct max8997_muic_info *info)
487 {
488 	int chg_type;
489 	bool attached;
490 	int adc;
491 
492 	chg_type = max8997_muic_get_cable_type(info,
493 				MAX8997_CABLE_GROUP_CHG, &attached);
494 
495 	switch (chg_type) {
496 	case MAX8997_CHARGER_TYPE_NONE:
497 		break;
498 	case MAX8997_CHARGER_TYPE_USB:
499 		adc = info->status[0] & STATUS1_ADC_MASK;
500 		adc >>= STATUS1_ADC_SHIFT;
501 
502 		if ((adc & STATUS1_ADC_MASK) == MAX8997_MUIC_ADC_OPEN) {
503 			max8997_muic_handle_usb(info,
504 					MAX8997_USB_DEVICE, attached);
505 		}
506 		break;
507 	case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
508 		extcon_set_cable_state(info->edev,
509 				      "Charge-downstream", attached);
510 		break;
511 	case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
512 		extcon_set_cable_state(info->edev, "TA", attached);
513 		break;
514 	case MAX8997_CHARGER_TYPE_500MA:
515 		extcon_set_cable_state(info->edev, "Slow-charger", attached);
516 		break;
517 	case MAX8997_CHARGER_TYPE_1A:
518 		extcon_set_cable_state(info->edev, "Fast-charger", attached);
519 		break;
520 	default:
521 		dev_err(info->dev,
522 			"failed to detect %s unknown chg cable (type:0x%x)\n",
523 			attached ? "attached" : "detached", chg_type);
524 		return -EINVAL;
525 	}
526 
527 	return 0;
528 }
529 
530 static void max8997_muic_irq_work(struct work_struct *work)
531 {
532 	struct max8997_muic_info *info = container_of(work,
533 			struct max8997_muic_info, irq_work);
534 	int irq_type = 0;
535 	int i, ret;
536 
537 	if (!info->edev)
538 		return;
539 
540 	mutex_lock(&info->mutex);
541 
542 	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
543 		if (info->irq == muic_irqs[i].virq)
544 			irq_type = muic_irqs[i].irq;
545 
546 	ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
547 				2, info->status);
548 	if (ret) {
549 		dev_err(info->dev, "failed to read muic register\n");
550 		mutex_unlock(&info->mutex);
551 		return;
552 	}
553 
554 	switch (irq_type) {
555 	case MAX8997_MUICIRQ_ADCError:
556 	case MAX8997_MUICIRQ_ADCLow:
557 	case MAX8997_MUICIRQ_ADC:
558 		/* Handle all of cable except for charger cable */
559 		ret = max8997_muic_adc_handler(info);
560 		break;
561 	case MAX8997_MUICIRQ_VBVolt:
562 	case MAX8997_MUICIRQ_DBChg:
563 	case MAX8997_MUICIRQ_DCDTmr:
564 	case MAX8997_MUICIRQ_ChgDetRun:
565 	case MAX8997_MUICIRQ_ChgTyp:
566 		/* Handle charger cable */
567 		ret = max8997_muic_chg_handler(info);
568 		break;
569 	case MAX8997_MUICIRQ_OVP:
570 		break;
571 	default:
572 		dev_info(info->dev, "misc interrupt: irq %d occurred\n",
573 				irq_type);
574 		mutex_unlock(&info->mutex);
575 		return;
576 	}
577 
578 	if (ret < 0)
579 		dev_err(info->dev, "failed to handle MUIC interrupt\n");
580 
581 	mutex_unlock(&info->mutex);
582 
583 	return;
584 }
585 
586 static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
587 {
588 	struct max8997_muic_info *info = data;
589 
590 	dev_dbg(info->dev, "irq:%d\n", irq);
591 	info->irq = irq;
592 
593 	schedule_work(&info->irq_work);
594 
595 	return IRQ_HANDLED;
596 }
597 
598 static int max8997_muic_detect_dev(struct max8997_muic_info *info)
599 {
600 	int ret = 0;
601 	int adc;
602 	int chg_type;
603 	bool attached;
604 
605 	mutex_lock(&info->mutex);
606 
607 	/* Read STATUSx register to detect accessory */
608 	ret = max8997_bulk_read(info->muic,
609 			MAX8997_MUIC_REG_STATUS1, 2, info->status);
610 	if (ret) {
611 		dev_err(info->dev, "failed to read MUIC register\n");
612 		mutex_unlock(&info->mutex);
613 		return ret;
614 	}
615 
616 	adc = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_ADC,
617 					&attached);
618 	if (attached && adc != MAX8997_MUIC_ADC_OPEN) {
619 		ret = max8997_muic_adc_handler(info);
620 		if (ret < 0) {
621 			dev_err(info->dev, "Cannot detect ADC cable\n");
622 			mutex_unlock(&info->mutex);
623 			return ret;
624 		}
625 	}
626 
627 	chg_type = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_CHG,
628 					&attached);
629 	if (attached && chg_type != MAX8997_CHARGER_TYPE_NONE) {
630 		ret = max8997_muic_chg_handler(info);
631 		if (ret < 0) {
632 			dev_err(info->dev, "Cannot detect charger cable\n");
633 			mutex_unlock(&info->mutex);
634 			return ret;
635 		}
636 	}
637 
638 	mutex_unlock(&info->mutex);
639 
640 	return 0;
641 }
642 
643 static void max8997_muic_detect_cable_wq(struct work_struct *work)
644 {
645 	struct max8997_muic_info *info = container_of(to_delayed_work(work),
646 				struct max8997_muic_info, wq_detcable);
647 	int ret;
648 
649 	ret = max8997_muic_detect_dev(info);
650 	if (ret < 0)
651 		dev_err(info->dev, "failed to detect cable type\n");
652 }
653 
654 static int max8997_muic_probe(struct platform_device *pdev)
655 {
656 	struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
657 	struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
658 	struct max8997_muic_info *info;
659 	int delay_jiffies;
660 	int ret, i;
661 
662 	info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_muic_info),
663 			    GFP_KERNEL);
664 	if (!info) {
665 		dev_err(&pdev->dev, "failed to allocate memory\n");
666 		return -ENOMEM;
667 	}
668 
669 	info->dev = &pdev->dev;
670 	info->muic = max8997->muic;
671 
672 	platform_set_drvdata(pdev, info);
673 	mutex_init(&info->mutex);
674 
675 	INIT_WORK(&info->irq_work, max8997_muic_irq_work);
676 
677 	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
678 		struct max8997_muic_irq *muic_irq = &muic_irqs[i];
679 		unsigned int virq = 0;
680 
681 		virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
682 		if (!virq) {
683 			ret = -EINVAL;
684 			goto err_irq;
685 		}
686 		muic_irq->virq = virq;
687 
688 		ret = request_threaded_irq(virq, NULL,
689 				max8997_muic_irq_handler,
690 				IRQF_NO_SUSPEND,
691 				muic_irq->name, info);
692 		if (ret) {
693 			dev_err(&pdev->dev,
694 				"failed: irq request (IRQ: %d,"
695 				" error :%d)\n",
696 				muic_irq->irq, ret);
697 			goto err_irq;
698 		}
699 	}
700 
701 	/* External connector */
702 	info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
703 				  GFP_KERNEL);
704 	if (!info->edev) {
705 		dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
706 		ret = -ENOMEM;
707 		goto err_irq;
708 	}
709 	info->edev->name = DEV_NAME;
710 	info->edev->dev.parent = &pdev->dev;
711 	info->edev->supported_cable = max8997_extcon_cable;
712 	ret = extcon_dev_register(info->edev);
713 	if (ret) {
714 		dev_err(&pdev->dev, "failed to register extcon device\n");
715 		goto err_irq;
716 	}
717 
718 	if (pdata->muic_pdata) {
719 		struct max8997_muic_platform_data *muic_pdata
720 			= pdata->muic_pdata;
721 
722 		/* Initialize registers according to platform data */
723 		for (i = 0; i < muic_pdata->num_init_data; i++) {
724 			max8997_write_reg(info->muic,
725 					muic_pdata->init_data[i].addr,
726 					muic_pdata->init_data[i].data);
727 		}
728 
729 		/*
730 		 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
731 		 * h/w path of COMP2/COMN1 on CONTROL1 register.
732 		 */
733 		if (muic_pdata->path_uart)
734 			info->path_uart = muic_pdata->path_uart;
735 		else
736 			info->path_uart = CONTROL1_SW_UART;
737 
738 		if (muic_pdata->path_usb)
739 			info->path_usb = muic_pdata->path_usb;
740 		else
741 			info->path_usb = CONTROL1_SW_USB;
742 
743 		/*
744 		 * Default delay time for detecting cable state
745 		 * after certain time.
746 		 */
747 		if (muic_pdata->detcable_delay_ms)
748 			delay_jiffies =
749 				msecs_to_jiffies(muic_pdata->detcable_delay_ms);
750 		else
751 			delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
752 	} else {
753 		info->path_uart = CONTROL1_SW_UART;
754 		info->path_usb = CONTROL1_SW_USB;
755 		delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
756 	}
757 
758 	/* Set initial path for UART */
759 	 max8997_muic_set_path(info, info->path_uart, true);
760 
761 	/* Set ADC debounce time */
762 	max8997_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
763 
764 	/*
765 	 * Detect accessory after completing the initialization of platform
766 	 *
767 	 * - Use delayed workqueue to detect cable state and then
768 	 * notify cable state to notifiee/platform through uevent.
769 	 * After completing the booting of platform, the extcon provider
770 	 * driver should notify cable state to upper layer.
771 	 */
772 	INIT_DELAYED_WORK(&info->wq_detcable, max8997_muic_detect_cable_wq);
773 	schedule_delayed_work(&info->wq_detcable, delay_jiffies);
774 
775 	return 0;
776 
777 err_irq:
778 	while (--i >= 0)
779 		free_irq(muic_irqs[i].virq, info);
780 	return ret;
781 }
782 
783 static int max8997_muic_remove(struct platform_device *pdev)
784 {
785 	struct max8997_muic_info *info = platform_get_drvdata(pdev);
786 	int i;
787 
788 	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
789 		free_irq(muic_irqs[i].virq, info);
790 	cancel_work_sync(&info->irq_work);
791 
792 	extcon_dev_unregister(info->edev);
793 
794 	return 0;
795 }
796 
797 static struct platform_driver max8997_muic_driver = {
798 	.driver		= {
799 		.name	= DEV_NAME,
800 		.owner	= THIS_MODULE,
801 	},
802 	.probe		= max8997_muic_probe,
803 	.remove		= max8997_muic_remove,
804 };
805 
806 module_platform_driver(max8997_muic_driver);
807 
808 MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
809 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
810 MODULE_LICENSE("GPL");
811