1 /*
2  * extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC
3  *
4  * Copyright (C) 2012 Samsung Electrnoics
5  * Chanwoo Choi <cw00.choi@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/input.h>
23 #include <linux/interrupt.h>
24 #include <linux/err.h>
25 #include <linux/platform_device.h>
26 #include <linux/mfd/max77693.h>
27 #include <linux/mfd/max77693-private.h>
28 #include <linux/extcon.h>
29 #include <linux/regmap.h>
30 #include <linux/irqdomain.h>
31 
32 #define	DEV_NAME			"max77693-muic"
33 #define	DELAY_MS_DEFAULT		20000		/* unit: millisecond */
34 
35 enum max77693_muic_adc_debounce_time {
36 	ADC_DEBOUNCE_TIME_5MS = 0,
37 	ADC_DEBOUNCE_TIME_10MS,
38 	ADC_DEBOUNCE_TIME_25MS,
39 	ADC_DEBOUNCE_TIME_38_62MS,
40 };
41 
42 struct max77693_muic_info {
43 	struct device *dev;
44 	struct max77693_dev *max77693;
45 	struct extcon_dev *edev;
46 	int prev_cable_type;
47 	int prev_cable_type_gnd;
48 	int prev_chg_type;
49 	int prev_button_type;
50 	u8 status[2];
51 
52 	int irq;
53 	struct work_struct irq_work;
54 	struct mutex mutex;
55 
56 	/*
57 	 * Use delayed workqueue to detect cable state and then
58 	 * notify cable state to notifiee/platform through uevent.
59 	 * After completing the booting of platform, the extcon provider
60 	 * driver should notify cable state to upper layer.
61 	 */
62 	struct delayed_work wq_detcable;
63 
64 	/* Button of dock device */
65 	struct input_dev *dock;
66 
67 	/*
68 	 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
69 	 * h/w path of COMP2/COMN1 on CONTROL1 register.
70 	 */
71 	int path_usb;
72 	int path_uart;
73 };
74 
75 enum max77693_muic_cable_group {
76 	MAX77693_CABLE_GROUP_ADC = 0,
77 	MAX77693_CABLE_GROUP_ADC_GND,
78 	MAX77693_CABLE_GROUP_CHG,
79 	MAX77693_CABLE_GROUP_VBVOLT,
80 };
81 
82 enum max77693_muic_charger_type {
83 	MAX77693_CHARGER_TYPE_NONE = 0,
84 	MAX77693_CHARGER_TYPE_USB,
85 	MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,
86 	MAX77693_CHARGER_TYPE_DEDICATED_CHG,
87 	MAX77693_CHARGER_TYPE_APPLE_500MA,
88 	MAX77693_CHARGER_TYPE_APPLE_1A_2A,
89 	MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,
90 };
91 
92 /**
93  * struct max77693_muic_irq
94  * @irq: the index of irq list of MUIC device.
95  * @name: the name of irq.
96  * @virq: the virtual irq to use irq domain
97  */
98 struct max77693_muic_irq {
99 	unsigned int irq;
100 	const char *name;
101 	unsigned int virq;
102 };
103 
104 static struct max77693_muic_irq muic_irqs[] = {
105 	{ MAX77693_MUIC_IRQ_INT1_ADC,		"muic-ADC" },
106 	{ MAX77693_MUIC_IRQ_INT1_ADC_LOW,	"muic-ADCLOW" },
107 	{ MAX77693_MUIC_IRQ_INT1_ADC_ERR,	"muic-ADCError" },
108 	{ MAX77693_MUIC_IRQ_INT1_ADC1K,		"muic-ADC1K" },
109 	{ MAX77693_MUIC_IRQ_INT2_CHGTYP,	"muic-CHGTYP" },
110 	{ MAX77693_MUIC_IRQ_INT2_CHGDETREUN,	"muic-CHGDETREUN" },
111 	{ MAX77693_MUIC_IRQ_INT2_DCDTMR,	"muic-DCDTMR" },
112 	{ MAX77693_MUIC_IRQ_INT2_DXOVP,		"muic-DXOVP" },
113 	{ MAX77693_MUIC_IRQ_INT2_VBVOLT,	"muic-VBVOLT" },
114 	{ MAX77693_MUIC_IRQ_INT2_VIDRM,		"muic-VIDRM" },
115 	{ MAX77693_MUIC_IRQ_INT3_EOC,		"muic-EOC" },
116 	{ MAX77693_MUIC_IRQ_INT3_CGMBC,		"muic-CGMBC" },
117 	{ MAX77693_MUIC_IRQ_INT3_OVP,		"muic-OVP" },
118 	{ MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR,	"muic-MBCCHG_ERR" },
119 	{ MAX77693_MUIC_IRQ_INT3_CHG_ENABLED,	"muic-CHG_ENABLED" },
120 	{ MAX77693_MUIC_IRQ_INT3_BAT_DET,	"muic-BAT_DET" },
121 };
122 
123 /* Define supported accessory type */
124 enum max77693_muic_acc_type {
125 	MAX77693_MUIC_ADC_GROUND = 0x0,
126 	MAX77693_MUIC_ADC_SEND_END_BUTTON,
127 	MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,
128 	MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,
129 	MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,
130 	MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,
131 	MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,
132 	MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,
133 	MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,
134 	MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,
135 	MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,
136 	MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,
137 	MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,
138 	MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,
139 	MAX77693_MUIC_ADC_RESERVED_ACC_1,
140 	MAX77693_MUIC_ADC_RESERVED_ACC_2,
141 	MAX77693_MUIC_ADC_RESERVED_ACC_3,
142 	MAX77693_MUIC_ADC_RESERVED_ACC_4,
143 	MAX77693_MUIC_ADC_RESERVED_ACC_5,
144 	MAX77693_MUIC_ADC_CEA936_AUDIO,
145 	MAX77693_MUIC_ADC_PHONE_POWERED_DEV,
146 	MAX77693_MUIC_ADC_TTY_CONVERTER,
147 	MAX77693_MUIC_ADC_UART_CABLE,
148 	MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,
149 	MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,
150 	MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,
151 	MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,
152 	MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,
153 	MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,
154 	MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,
155 	MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,
156 	MAX77693_MUIC_ADC_OPEN,
157 
158 	/* The below accessories have same ADC value so ADCLow and
159 	   ADC1K bit is used to separate specific accessory */
160 	MAX77693_MUIC_GND_USB_OTG = 0x100,	/* ADC:0x0, VBVolot:0, ADCLow:0, ADC1K:0 */
161 	MAX77693_MUIC_GND_USB_OTG_VB = 0x104,	/* ADC:0x0, VBVolot:1, ADCLow:0, ADC1K:0 */
162 	MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:0 */
163 	MAX77693_MUIC_GND_MHL = 0x103,		/* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:1 */
164 	MAX77693_MUIC_GND_MHL_VB = 0x107,	/* ADC:0x0, VBVolot:1, ADCLow:1, ADC1K:1 */
165 };
166 
167 /* MAX77693 MUIC device support below list of accessories(external connector) */
168 enum {
169 	EXTCON_CABLE_USB = 0,
170 	EXTCON_CABLE_USB_HOST,
171 	EXTCON_CABLE_TA,
172 	EXTCON_CABLE_FAST_CHARGER,
173 	EXTCON_CABLE_SLOW_CHARGER,
174 	EXTCON_CABLE_CHARGE_DOWNSTREAM,
175 	EXTCON_CABLE_MHL,
176 	EXTCON_CABLE_MHL_TA,
177 	EXTCON_CABLE_JIG_USB_ON,
178 	EXTCON_CABLE_JIG_USB_OFF,
179 	EXTCON_CABLE_JIG_UART_OFF,
180 	EXTCON_CABLE_JIG_UART_ON,
181 	EXTCON_CABLE_DOCK_SMART,
182 	EXTCON_CABLE_DOCK_DESK,
183 	EXTCON_CABLE_DOCK_AUDIO,
184 
185 	_EXTCON_CABLE_NUM,
186 };
187 
188 static const char *max77693_extcon_cable[] = {
189 	[EXTCON_CABLE_USB]			= "USB",
190 	[EXTCON_CABLE_USB_HOST]			= "USB-Host",
191 	[EXTCON_CABLE_TA]			= "TA",
192 	[EXTCON_CABLE_FAST_CHARGER]		= "Fast-charger",
193 	[EXTCON_CABLE_SLOW_CHARGER]		= "Slow-charger",
194 	[EXTCON_CABLE_CHARGE_DOWNSTREAM]	= "Charge-downstream",
195 	[EXTCON_CABLE_MHL]			= "MHL",
196 	[EXTCON_CABLE_MHL_TA]			= "MHL_TA",
197 	[EXTCON_CABLE_JIG_USB_ON]		= "JIG-USB-ON",
198 	[EXTCON_CABLE_JIG_USB_OFF]		= "JIG-USB-OFF",
199 	[EXTCON_CABLE_JIG_UART_OFF]		= "JIG-UART-OFF",
200 	[EXTCON_CABLE_JIG_UART_ON]		= "Dock-Car",
201 	[EXTCON_CABLE_DOCK_SMART]		= "Dock-Smart",
202 	[EXTCON_CABLE_DOCK_DESK]		= "Dock-Desk",
203 	[EXTCON_CABLE_DOCK_AUDIO]		= "Dock-Audio",
204 
205 	NULL,
206 };
207 
208 /*
209  * max77693_muic_set_debounce_time - Set the debounce time of ADC
210  * @info: the instance including private data of max77693 MUIC
211  * @time: the debounce time of ADC
212  */
213 static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
214 		enum max77693_muic_adc_debounce_time time)
215 {
216 	int ret;
217 
218 	switch (time) {
219 	case ADC_DEBOUNCE_TIME_5MS:
220 	case ADC_DEBOUNCE_TIME_10MS:
221 	case ADC_DEBOUNCE_TIME_25MS:
222 	case ADC_DEBOUNCE_TIME_38_62MS:
223 		ret = max77693_update_reg(info->max77693->regmap_muic,
224 					  MAX77693_MUIC_REG_CTRL3,
225 					  time << CONTROL3_ADCDBSET_SHIFT,
226 					  CONTROL3_ADCDBSET_MASK);
227 		if (ret) {
228 			dev_err(info->dev, "failed to set ADC debounce time\n");
229 			return -EAGAIN;
230 		}
231 		break;
232 	default:
233 		dev_err(info->dev, "invalid ADC debounce time\n");
234 		return -EINVAL;
235 	}
236 
237 	return 0;
238 };
239 
240 /*
241  * max77693_muic_set_path - Set hardware line according to attached cable
242  * @info: the instance including private data of max77693 MUIC
243  * @value: the path according to attached cable
244  * @attached: the state of cable (true:attached, false:detached)
245  *
246  * The max77693 MUIC device share outside H/W line among a varity of cables
247  * so, this function set internal path of H/W line according to the type of
248  * attached cable.
249  */
250 static int max77693_muic_set_path(struct max77693_muic_info *info,
251 		u8 val, bool attached)
252 {
253 	int ret = 0;
254 	u8 ctrl1, ctrl2 = 0;
255 
256 	if (attached)
257 		ctrl1 = val;
258 	else
259 		ctrl1 = CONTROL1_SW_OPEN;
260 
261 	ret = max77693_update_reg(info->max77693->regmap_muic,
262 			MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
263 	if (ret < 0) {
264 		dev_err(info->dev, "failed to update MUIC register\n");
265 		return -EAGAIN;
266 	}
267 
268 	if (attached)
269 		ctrl2 |= CONTROL2_CPEN_MASK;	/* LowPwr=0, CPEn=1 */
270 	else
271 		ctrl2 |= CONTROL2_LOWPWR_MASK;	/* LowPwr=1, CPEn=0 */
272 
273 	ret = max77693_update_reg(info->max77693->regmap_muic,
274 			MAX77693_MUIC_REG_CTRL2, ctrl2,
275 			CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
276 	if (ret < 0) {
277 		dev_err(info->dev, "failed to update MUIC register\n");
278 		return -EAGAIN;
279 	}
280 
281 	dev_info(info->dev,
282 		"CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
283 		ctrl1, ctrl2, attached ? "attached" : "detached");
284 
285 	return 0;
286 }
287 
288 /*
289  * max77693_muic_get_cable_type - Return cable type and check cable state
290  * @info: the instance including private data of max77693 MUIC
291  * @group: the path according to attached cable
292  * @attached: store cable state and return
293  *
294  * This function check the cable state either attached or detached,
295  * and then divide precise type of cable according to cable group.
296  *	- MAX77693_CABLE_GROUP_ADC
297  *	- MAX77693_CABLE_GROUP_ADC_GND
298  *	- MAX77693_CABLE_GROUP_CHG
299  *	- MAX77693_CABLE_GROUP_VBVOLT
300  */
301 static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
302 		enum max77693_muic_cable_group group, bool *attached)
303 {
304 	int cable_type = 0;
305 	int adc;
306 	int adc1k;
307 	int adclow;
308 	int vbvolt;
309 	int chg_type;
310 
311 	switch (group) {
312 	case MAX77693_CABLE_GROUP_ADC:
313 		/*
314 		 * Read ADC value to check cable type and decide cable state
315 		 * according to cable type
316 		 */
317 		adc = info->status[0] & STATUS1_ADC_MASK;
318 		adc >>= STATUS1_ADC_SHIFT;
319 
320 		/*
321 		 * Check current cable state/cable type and store cable type
322 		 * (info->prev_cable_type) for handling cable when cable is
323 		 * detached.
324 		 */
325 		if (adc == MAX77693_MUIC_ADC_OPEN) {
326 			*attached = false;
327 
328 			cable_type = info->prev_cable_type;
329 			info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
330 		} else {
331 			*attached = true;
332 
333 			cable_type = info->prev_cable_type = adc;
334 		}
335 		break;
336 	case MAX77693_CABLE_GROUP_ADC_GND:
337 		/*
338 		 * Read ADC value to check cable type and decide cable state
339 		 * according to cable type
340 		 */
341 		adc = info->status[0] & STATUS1_ADC_MASK;
342 		adc >>= STATUS1_ADC_SHIFT;
343 
344 		/*
345 		 * Check current cable state/cable type and store cable type
346 		 * (info->prev_cable_type/_gnd) for handling cable when cable
347 		 * is detached.
348 		 */
349 		if (adc == MAX77693_MUIC_ADC_OPEN) {
350 			*attached = false;
351 
352 			cable_type = info->prev_cable_type_gnd;
353 			info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
354 		} else {
355 			*attached = true;
356 
357 			adclow = info->status[0] & STATUS1_ADCLOW_MASK;
358 			adclow >>= STATUS1_ADCLOW_SHIFT;
359 			adc1k = info->status[0] & STATUS1_ADC1K_MASK;
360 			adc1k >>= STATUS1_ADC1K_SHIFT;
361 
362 			vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
363 			vbvolt >>= STATUS2_VBVOLT_SHIFT;
364 
365 			/**
366 			 * [0x1][VBVolt][ADCLow][ADC1K]
367 			 * [0x1    0	   0       0  ]	: USB_OTG
368 			 * [0x1    1	   0       0  ]	: USB_OTG_VB
369 			 * [0x1    0       1       0  ] : Audio Video Cable with load
370 			 * [0x1    0       1       1  ] : MHL without charging connector
371 			 * [0x1    1       1       1  ] : MHL with charging connector
372 			 */
373 			cable_type = ((0x1 << 8)
374 					| (vbvolt << 2)
375 					| (adclow << 1)
376 					| adc1k);
377 
378 			info->prev_cable_type = adc;
379 			info->prev_cable_type_gnd = cable_type;
380 		}
381 
382 		break;
383 	case MAX77693_CABLE_GROUP_CHG:
384 		/*
385 		 * Read charger type to check cable type and decide cable state
386 		 * according to type of charger cable.
387 		 */
388 		chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
389 		chg_type >>= STATUS2_CHGTYP_SHIFT;
390 
391 		if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
392 			*attached = false;
393 
394 			cable_type = info->prev_chg_type;
395 			info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
396 		} else {
397 			*attached = true;
398 
399 			/*
400 			 * Check current cable state/cable type and store cable
401 			 * type(info->prev_chg_type) for handling cable when
402 			 * charger cable is detached.
403 			 */
404 			cable_type = info->prev_chg_type = chg_type;
405 		}
406 
407 		break;
408 	case MAX77693_CABLE_GROUP_VBVOLT:
409 		/*
410 		 * Read ADC value to check cable type and decide cable state
411 		 * according to cable type
412 		 */
413 		adc = info->status[0] & STATUS1_ADC_MASK;
414 		adc >>= STATUS1_ADC_SHIFT;
415 		chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
416 		chg_type >>= STATUS2_CHGTYP_SHIFT;
417 
418 		if (adc == MAX77693_MUIC_ADC_OPEN
419 				&& chg_type == MAX77693_CHARGER_TYPE_NONE)
420 			*attached = false;
421 		else
422 			*attached = true;
423 
424 		/*
425 		 * Read vbvolt field, if vbvolt is 1,
426 		 * this cable is used for charging.
427 		 */
428 		vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
429 		vbvolt >>= STATUS2_VBVOLT_SHIFT;
430 
431 		cable_type = vbvolt;
432 		break;
433 	default:
434 		dev_err(info->dev, "Unknown cable group (%d)\n", group);
435 		cable_type = -EINVAL;
436 		break;
437 	}
438 
439 	return cable_type;
440 }
441 
442 static int max77693_muic_dock_handler(struct max77693_muic_info *info,
443 		int cable_type, bool attached)
444 {
445 	int ret = 0;
446 	int vbvolt;
447 	bool cable_attached;
448 	char dock_name[CABLE_NAME_MAX];
449 
450 	dev_info(info->dev,
451 		"external connector is %s (adc:0x%02x)\n",
452 		attached ? "attached" : "detached", cable_type);
453 
454 	switch (cable_type) {
455 	case MAX77693_MUIC_ADC_RESERVED_ACC_3:		/* Dock-Smart */
456 		/*
457 		 * Check power cable whether attached or detached state.
458 		 * The Dock-Smart device need surely external power supply.
459 		 * If power cable(USB/TA) isn't connected to Dock device,
460 		 * user can't use Dock-Smart for desktop mode.
461 		 */
462 		vbvolt = max77693_muic_get_cable_type(info,
463 				MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);
464 		if (attached && !vbvolt) {
465 			dev_warn(info->dev,
466 				"Cannot detect external power supply\n");
467 			return 0;
468 		}
469 
470 		/*
471 		 * Notify Dock-Smart/MHL state.
472 		 * - Dock-Smart device include three type of cable which
473 		 * are HDMI, USB for mouse/keyboard and micro-usb port
474 		 * for USB/TA cable. Dock-Smart device need always exteranl
475 		 * power supply(USB/TA cable through micro-usb cable). Dock-
476 		 * Smart device support screen output of target to separate
477 		 * monitor and mouse/keyboard for desktop mode.
478 		 *
479 		 * Features of 'USB/TA cable with Dock-Smart device'
480 		 * - Support MHL
481 		 * - Support external output feature of audio
482 		 * - Support charging through micro-usb port without data
483 		 *	     connection if TA cable is connected to target.
484 		 * - Support charging and data connection through micro-usb port
485 		 *           if USB cable is connected between target and host
486 		 *	     device.
487 		 * - Support OTG device (Mouse/Keyboard)
488 		 */
489 		ret = max77693_muic_set_path(info, info->path_usb, attached);
490 		if (ret < 0)
491 			return ret;
492 
493 		extcon_set_cable_state(info->edev, "Dock-Smart", attached);
494 		extcon_set_cable_state(info->edev, "MHL", attached);
495 		goto out;
496 	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:	/* Dock-Car */
497 		strcpy(dock_name, "Dock-Car");
498 		break;
499 	case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:	/* Dock-Desk */
500 		strcpy(dock_name, "Dock-Desk");
501 		break;
502 	case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:		/* Dock-Audio */
503 		strcpy(dock_name, "Dock-Audio");
504 		if (!attached)
505 			extcon_set_cable_state(info->edev, "USB", false);
506 		break;
507 	default:
508 		dev_err(info->dev, "failed to detect %s dock device\n",
509 			attached ? "attached" : "detached");
510 		return -EINVAL;
511 	}
512 
513 	/* Dock-Car/Desk/Audio, PATH:AUDIO */
514 	ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
515 	if (ret < 0)
516 		return ret;
517 	extcon_set_cable_state(info->edev, dock_name, attached);
518 
519 out:
520 	return 0;
521 }
522 
523 static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
524 		int button_type, bool attached)
525 {
526 	struct input_dev *dock = info->dock;
527 	unsigned int code;
528 
529 	switch (button_type) {
530 	case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
531 		... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
532 		/* DOCK_KEY_PREV */
533 		code = KEY_PREVIOUSSONG;
534 		break;
535 	case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
536 		... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
537 		/* DOCK_KEY_NEXT */
538 		code = KEY_NEXTSONG;
539 		break;
540 	case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
541 		/* DOCK_VOL_DOWN */
542 		code = KEY_VOLUMEDOWN;
543 		break;
544 	case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
545 		/* DOCK_VOL_UP */
546 		code = KEY_VOLUMEUP;
547 		break;
548 	case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
549 		... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
550 		/* DOCK_KEY_PLAY_PAUSE */
551 		code = KEY_PLAYPAUSE;
552 		break;
553 	default:
554 		dev_err(info->dev,
555 			"failed to detect %s key (adc:0x%x)\n",
556 			attached ? "pressed" : "released", button_type);
557 		return -EINVAL;
558 	}
559 
560 	input_event(dock, EV_KEY, code, attached);
561 	input_sync(dock);
562 
563 	return 0;
564 }
565 
566 static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
567 {
568 	int cable_type_gnd;
569 	int ret = 0;
570 	bool attached;
571 
572 	cable_type_gnd = max77693_muic_get_cable_type(info,
573 				MAX77693_CABLE_GROUP_ADC_GND, &attached);
574 
575 	switch (cable_type_gnd) {
576 	case MAX77693_MUIC_GND_USB_OTG:
577 	case MAX77693_MUIC_GND_USB_OTG_VB:
578 		/* USB_OTG, PATH: AP_USB */
579 		ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
580 		if (ret < 0)
581 			return ret;
582 		extcon_set_cable_state(info->edev, "USB-Host", attached);
583 		break;
584 	case MAX77693_MUIC_GND_AV_CABLE_LOAD:
585 		/* Audio Video Cable with load, PATH:AUDIO */
586 		ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
587 		if (ret < 0)
588 			return ret;
589 		extcon_set_cable_state(info->edev,
590 				"Audio-video-load", attached);
591 		break;
592 	case MAX77693_MUIC_GND_MHL:
593 	case MAX77693_MUIC_GND_MHL_VB:
594 		/* MHL or MHL with USB/TA cable */
595 		extcon_set_cable_state(info->edev, "MHL", attached);
596 		break;
597 	default:
598 		dev_err(info->dev, "failed to detect %s cable of gnd type\n",
599 			attached ? "attached" : "detached");
600 		return -EINVAL;
601 	}
602 
603 	return 0;
604 }
605 
606 static int max77693_muic_jig_handler(struct max77693_muic_info *info,
607 		int cable_type, bool attached)
608 {
609 	char cable_name[32];
610 	int ret = 0;
611 	u8 path = CONTROL1_SW_OPEN;
612 
613 	dev_info(info->dev,
614 		"external connector is %s (adc:0x%02x)\n",
615 		attached ? "attached" : "detached", cable_type);
616 
617 	switch (cable_type) {
618 	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:	/* ADC_JIG_USB_OFF */
619 		/* PATH:AP_USB */
620 		strcpy(cable_name, "JIG-USB-OFF");
621 		path = CONTROL1_SW_USB;
622 		break;
623 	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:	/* ADC_JIG_USB_ON */
624 		/* PATH:AP_USB */
625 		strcpy(cable_name, "JIG-USB-ON");
626 		path = CONTROL1_SW_USB;
627 		break;
628 	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:	/* ADC_JIG_UART_OFF */
629 		/* PATH:AP_UART */
630 		strcpy(cable_name, "JIG-UART-OFF");
631 		path = CONTROL1_SW_UART;
632 		break;
633 	default:
634 		dev_err(info->dev, "failed to detect %s jig cable\n",
635 			attached ? "attached" : "detached");
636 		return -EINVAL;
637 	}
638 
639 	ret = max77693_muic_set_path(info, path, attached);
640 	if (ret < 0)
641 		return ret;
642 
643 	extcon_set_cable_state(info->edev, cable_name, attached);
644 
645 	return 0;
646 }
647 
648 static int max77693_muic_adc_handler(struct max77693_muic_info *info)
649 {
650 	int cable_type;
651 	int button_type;
652 	bool attached;
653 	int ret = 0;
654 
655 	/* Check accessory state which is either detached or attached */
656 	cable_type = max77693_muic_get_cable_type(info,
657 				MAX77693_CABLE_GROUP_ADC, &attached);
658 
659 	dev_info(info->dev,
660 		"external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
661 		attached ? "attached" : "detached", cable_type,
662 		info->prev_cable_type);
663 
664 	switch (cable_type) {
665 	case MAX77693_MUIC_ADC_GROUND:
666 		/* USB_OTG/MHL/Audio */
667 		max77693_muic_adc_ground_handler(info);
668 		break;
669 	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
670 	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
671 	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
672 		/* JIG */
673 		ret = max77693_muic_jig_handler(info, cable_type, attached);
674 		if (ret < 0)
675 			return ret;
676 		break;
677 	case MAX77693_MUIC_ADC_RESERVED_ACC_3:		/* Dock-Smart */
678 	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:	/* Dock-Car */
679 	case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:	/* Dock-Desk */
680 	case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:		/* Dock-Audio */
681 		/*
682 		 * DOCK device
683 		 *
684 		 * The MAX77693 MUIC device can detect total 34 cable type
685 		 * except of charger cable and MUIC device didn't define
686 		 * specfic role of cable in the range of from 0x01 to 0x12
687 		 * of ADC value. So, can use/define cable with no role according
688 		 * to schema of hardware board.
689 		 */
690 		ret = max77693_muic_dock_handler(info, cable_type, attached);
691 		if (ret < 0)
692 			return ret;
693 		break;
694 	case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON:	/* DOCK_KEY_PREV */
695 	case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON:	/* DOCK_KEY_NEXT */
696 	case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:	/* DOCK_VOL_DOWN */
697 	case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:	/* DOCK_VOL_UP */
698 	case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON:	/* DOCK_KEY_PLAY_PAUSE */
699 		/*
700 		 * Button of DOCK device
701 		 * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
702 		 *
703 		 * The MAX77693 MUIC device can detect total 34 cable type
704 		 * except of charger cable and MUIC device didn't define
705 		 * specfic role of cable in the range of from 0x01 to 0x12
706 		 * of ADC value. So, can use/define cable with no role according
707 		 * to schema of hardware board.
708 		 */
709 		if (attached)
710 			button_type = info->prev_button_type = cable_type;
711 		else
712 			button_type = info->prev_button_type;
713 
714 		ret = max77693_muic_dock_button_handler(info, button_type,
715 							attached);
716 		if (ret < 0)
717 			return ret;
718 		break;
719 	case MAX77693_MUIC_ADC_SEND_END_BUTTON:
720 	case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
721 	case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
722 	case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
723 	case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
724 	case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
725 	case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
726 	case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
727 	case MAX77693_MUIC_ADC_RESERVED_ACC_1:
728 	case MAX77693_MUIC_ADC_RESERVED_ACC_2:
729 	case MAX77693_MUIC_ADC_RESERVED_ACC_4:
730 	case MAX77693_MUIC_ADC_RESERVED_ACC_5:
731 	case MAX77693_MUIC_ADC_CEA936_AUDIO:
732 	case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
733 	case MAX77693_MUIC_ADC_TTY_CONVERTER:
734 	case MAX77693_MUIC_ADC_UART_CABLE:
735 	case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
736 	case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
737 		/*
738 		 * This accessory isn't used in general case if it is specially
739 		 * needed to detect additional accessory, should implement
740 		 * proper operation when this accessory is attached/detached.
741 		 */
742 		dev_info(info->dev,
743 			"accessory is %s but it isn't used (adc:0x%x)\n",
744 			attached ? "attached" : "detached", cable_type);
745 		return -EAGAIN;
746 	default:
747 		dev_err(info->dev,
748 			"failed to detect %s accessory (adc:0x%x)\n",
749 			attached ? "attached" : "detached", cable_type);
750 		return -EINVAL;
751 	}
752 
753 	return 0;
754 }
755 
756 static int max77693_muic_chg_handler(struct max77693_muic_info *info)
757 {
758 	int chg_type;
759 	int cable_type_gnd;
760 	int cable_type;
761 	bool attached;
762 	bool cable_attached;
763 	int ret = 0;
764 
765 	chg_type = max77693_muic_get_cable_type(info,
766 				MAX77693_CABLE_GROUP_CHG, &attached);
767 
768 	dev_info(info->dev,
769 		"external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
770 			attached ? "attached" : "detached",
771 			chg_type, info->prev_chg_type);
772 
773 	switch (chg_type) {
774 	case MAX77693_CHARGER_TYPE_USB:
775 	case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
776 	case MAX77693_CHARGER_TYPE_NONE:
777 		/* Check MAX77693_CABLE_GROUP_ADC_GND type */
778 		cable_type_gnd = max77693_muic_get_cable_type(info,
779 					MAX77693_CABLE_GROUP_ADC_GND,
780 					&cable_attached);
781 		switch (cable_type_gnd) {
782 		case MAX77693_MUIC_GND_MHL:
783 		case MAX77693_MUIC_GND_MHL_VB:
784 			/*
785 			 * MHL cable with MHL_TA(USB/TA) cable
786 			 * - MHL cable include two port(HDMI line and separate micro-
787 			 * usb port. When the target connect MHL cable, extcon driver
788 			 * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA
789 			 * cable is connected, extcon driver notify state to notifiee
790 			 * for charging battery.
791 			 *
792 			 * Features of 'MHL_TA(USB/TA) with MHL cable'
793 			 * - Support MHL
794 			 * - Support charging through micro-usb port without data connection
795 			 */
796 			extcon_set_cable_state(info->edev, "MHL_TA", attached);
797 			if (!cable_attached)
798 				extcon_set_cable_state(info->edev, "MHL", cable_attached);
799 			break;
800 		}
801 
802 		/* Check MAX77693_CABLE_GROUP_ADC type */
803 		cable_type = max77693_muic_get_cable_type(info,
804 					MAX77693_CABLE_GROUP_ADC,
805 					&cable_attached);
806 		switch (cable_type) {
807 		case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:		/* Dock-Audio */
808 			/*
809 			 * Dock-Audio device with USB/TA cable
810 			 * - Dock device include two port(Dock-Audio and micro-usb
811 			 * port). When the target connect Dock-Audio device, extcon
812 			 * driver check whether USB/TA cable is connected. If USB/TA
813 			 * cable is connected, extcon driver notify state to notifiee
814 			 * for charging battery.
815 			 *
816 			 * Features of 'USB/TA cable with Dock-Audio device'
817 			 * - Support external output feature of audio.
818 			 * - Support charging through micro-usb port without data
819 			 *           connection.
820 			 */
821 			extcon_set_cable_state(info->edev, "USB", attached);
822 
823 			if (!cable_attached)
824 				extcon_set_cable_state(info->edev, "Dock-Audio", cable_attached);
825 			break;
826 		case MAX77693_MUIC_ADC_RESERVED_ACC_3:		/* Dock-Smart */
827 			/*
828 			 * Dock-Smart device with USB/TA cable
829 			 * - Dock-Desk device include three type of cable which
830 			 * are HDMI, USB for mouse/keyboard and micro-usb port
831 			 * for USB/TA cable. Dock-Smart device need always exteranl
832 			 * power supply(USB/TA cable through micro-usb cable). Dock-
833 			 * Smart device support screen output of target to separate
834 			 * monitor and mouse/keyboard for desktop mode.
835 			 *
836 			 * Features of 'USB/TA cable with Dock-Smart device'
837 			 * - Support MHL
838 			 * - Support external output feature of audio
839 			 * - Support charging through micro-usb port without data
840 			 *	     connection if TA cable is connected to target.
841 			 * - Support charging and data connection through micro-usb port
842 			 *           if USB cable is connected between target and host
843 			 *	     device.
844 			 * - Support OTG device (Mouse/Keyboard)
845 			 */
846 			ret = max77693_muic_set_path(info, info->path_usb, attached);
847 			if (ret < 0)
848 				return ret;
849 
850 			extcon_set_cable_state(info->edev, "Dock-Smart", attached);
851 			extcon_set_cable_state(info->edev, "MHL", attached);
852 
853 			break;
854 		}
855 
856 		/* Check MAX77693_CABLE_GROUP_CHG type */
857 		switch (chg_type) {
858 		case MAX77693_CHARGER_TYPE_NONE:
859 			/*
860 			 * When MHL(with USB/TA cable) or Dock-Audio with USB/TA cable
861 			 * is attached, muic device happen below two interrupt.
862 			 * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting MHL/Dock-Audio.
863 			 * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting USB/TA cable
864 			 *   connected to MHL or Dock-Audio.
865 			 * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC interrupt
866 			 * than MAX77693_MUIC_IRQ_INT2_CHGTYP interrupt.
867 			 *
868 			 * If user attach MHL (with USB/TA cable and immediately detach
869 			 * MHL with USB/TA cable before MAX77693_MUIC_IRQ_INT2_CHGTYP
870 			 * interrupt is happened, USB/TA cable remain connected state to
871 			 * target. But USB/TA cable isn't connected to target. The user
872 			 * be face with unusual action. So, driver should check this
873 			 * situation in spite of, that previous charger type is N/A.
874 			 */
875 			break;
876 		case MAX77693_CHARGER_TYPE_USB:
877 			/* Only USB cable, PATH:AP_USB */
878 			ret = max77693_muic_set_path(info, info->path_usb, attached);
879 			if (ret < 0)
880 				return ret;
881 
882 			extcon_set_cable_state(info->edev, "USB", attached);
883 			break;
884 		case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
885 			/* Only TA cable */
886 			extcon_set_cable_state(info->edev, "TA", attached);
887 			break;
888 		}
889 		break;
890 	case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
891 		extcon_set_cable_state(info->edev,
892 				"Charge-downstream", attached);
893 		break;
894 	case MAX77693_CHARGER_TYPE_APPLE_500MA:
895 		extcon_set_cable_state(info->edev, "Slow-charger", attached);
896 		break;
897 	case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
898 		extcon_set_cable_state(info->edev, "Fast-charger", attached);
899 		break;
900 	case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
901 		break;
902 	default:
903 		dev_err(info->dev,
904 			"failed to detect %s accessory (chg_type:0x%x)\n",
905 			attached ? "attached" : "detached", chg_type);
906 		return -EINVAL;
907 	}
908 
909 	return 0;
910 }
911 
912 static void max77693_muic_irq_work(struct work_struct *work)
913 {
914 	struct max77693_muic_info *info = container_of(work,
915 			struct max77693_muic_info, irq_work);
916 	int irq_type = -1;
917 	int i, ret = 0;
918 
919 	if (!info->edev)
920 		return;
921 
922 	mutex_lock(&info->mutex);
923 
924 	for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
925 		if (info->irq == muic_irqs[i].virq)
926 			irq_type = muic_irqs[i].irq;
927 
928 	ret = max77693_bulk_read(info->max77693->regmap_muic,
929 			MAX77693_MUIC_REG_STATUS1, 2, info->status);
930 	if (ret) {
931 		dev_err(info->dev, "failed to read MUIC register\n");
932 		mutex_unlock(&info->mutex);
933 		return;
934 	}
935 
936 	switch (irq_type) {
937 	case MAX77693_MUIC_IRQ_INT1_ADC:
938 	case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
939 	case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
940 	case MAX77693_MUIC_IRQ_INT1_ADC1K:
941 		/* Handle all of accessory except for
942 		   type of charger accessory */
943 		ret = max77693_muic_adc_handler(info);
944 		break;
945 	case MAX77693_MUIC_IRQ_INT2_CHGTYP:
946 	case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
947 	case MAX77693_MUIC_IRQ_INT2_DCDTMR:
948 	case MAX77693_MUIC_IRQ_INT2_DXOVP:
949 	case MAX77693_MUIC_IRQ_INT2_VBVOLT:
950 	case MAX77693_MUIC_IRQ_INT2_VIDRM:
951 		/* Handle charger accessory */
952 		ret = max77693_muic_chg_handler(info);
953 		break;
954 	case MAX77693_MUIC_IRQ_INT3_EOC:
955 	case MAX77693_MUIC_IRQ_INT3_CGMBC:
956 	case MAX77693_MUIC_IRQ_INT3_OVP:
957 	case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
958 	case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
959 	case MAX77693_MUIC_IRQ_INT3_BAT_DET:
960 		break;
961 	default:
962 		dev_err(info->dev, "muic interrupt: irq %d occurred\n",
963 				irq_type);
964 		mutex_unlock(&info->mutex);
965 		return;
966 	}
967 
968 	if (ret < 0)
969 		dev_err(info->dev, "failed to handle MUIC interrupt\n");
970 
971 	mutex_unlock(&info->mutex);
972 
973 	return;
974 }
975 
976 static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
977 {
978 	struct max77693_muic_info *info = data;
979 
980 	info->irq = irq;
981 	schedule_work(&info->irq_work);
982 
983 	return IRQ_HANDLED;
984 }
985 
986 static struct regmap_config max77693_muic_regmap_config = {
987 	.reg_bits = 8,
988 	.val_bits = 8,
989 };
990 
991 static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
992 {
993 	int ret = 0;
994 	int adc;
995 	int chg_type;
996 	bool attached;
997 
998 	mutex_lock(&info->mutex);
999 
1000 	/* Read STATUSx register to detect accessory */
1001 	ret = max77693_bulk_read(info->max77693->regmap_muic,
1002 			MAX77693_MUIC_REG_STATUS1, 2, info->status);
1003 	if (ret) {
1004 		dev_err(info->dev, "failed to read MUIC register\n");
1005 		mutex_unlock(&info->mutex);
1006 		return -EINVAL;
1007 	}
1008 
1009 	adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
1010 					&attached);
1011 	if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
1012 		ret = max77693_muic_adc_handler(info);
1013 		if (ret < 0) {
1014 			dev_err(info->dev, "Cannot detect accessory\n");
1015 			mutex_unlock(&info->mutex);
1016 			return ret;
1017 		}
1018 	}
1019 
1020 	chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
1021 					&attached);
1022 	if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
1023 		ret = max77693_muic_chg_handler(info);
1024 		if (ret < 0) {
1025 			dev_err(info->dev, "Cannot detect charger accessory\n");
1026 			mutex_unlock(&info->mutex);
1027 			return ret;
1028 		}
1029 	}
1030 
1031 	mutex_unlock(&info->mutex);
1032 
1033 	return 0;
1034 }
1035 
1036 static void max77693_muic_detect_cable_wq(struct work_struct *work)
1037 {
1038 	struct max77693_muic_info *info = container_of(to_delayed_work(work),
1039 				struct max77693_muic_info, wq_detcable);
1040 
1041 	max77693_muic_detect_accessory(info);
1042 }
1043 
1044 static int max77693_muic_probe(struct platform_device *pdev)
1045 {
1046 	struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
1047 	struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
1048 	struct max77693_muic_platform_data *muic_pdata = pdata->muic_data;
1049 	struct max77693_muic_info *info;
1050 	int delay_jiffies;
1051 	int ret;
1052 	int i;
1053 	u8 id;
1054 
1055 	info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
1056 				   GFP_KERNEL);
1057 	if (!info) {
1058 		dev_err(&pdev->dev, "failed to allocate memory\n");
1059 		return -ENOMEM;
1060 	}
1061 	info->dev = &pdev->dev;
1062 	info->max77693 = max77693;
1063 	if (info->max77693->regmap_muic) {
1064 		dev_dbg(&pdev->dev, "allocate register map\n");
1065 	} else {
1066 		info->max77693->regmap_muic = devm_regmap_init_i2c(
1067 						info->max77693->muic,
1068 						&max77693_muic_regmap_config);
1069 		if (IS_ERR(info->max77693->regmap_muic)) {
1070 			ret = PTR_ERR(info->max77693->regmap_muic);
1071 			dev_err(max77693->dev,
1072 				"failed to allocate register map: %d\n", ret);
1073 			return ret;
1074 		}
1075 	}
1076 
1077 	/* Register input device for button of dock device */
1078 	info->dock = devm_input_allocate_device(&pdev->dev);
1079 	if (!info->dock) {
1080 		dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
1081 		return -ENOMEM;
1082 	}
1083 	info->dock->name = "max77693-muic/dock";
1084 	info->dock->phys = "max77693-muic/extcon";
1085 	info->dock->dev.parent = &pdev->dev;
1086 
1087 	__set_bit(EV_REP, info->dock->evbit);
1088 
1089 	input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
1090 	input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
1091 	input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
1092 	input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
1093 	input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
1094 
1095 	ret = input_register_device(info->dock);
1096 	if (ret < 0) {
1097 		dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
1098 				ret);
1099 		return ret;
1100 	}
1101 
1102 	platform_set_drvdata(pdev, info);
1103 	mutex_init(&info->mutex);
1104 
1105 	INIT_WORK(&info->irq_work, max77693_muic_irq_work);
1106 
1107 	/* Support irq domain for MAX77693 MUIC device */
1108 	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
1109 		struct max77693_muic_irq *muic_irq = &muic_irqs[i];
1110 		unsigned int virq = 0;
1111 
1112 		virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
1113 		if (!virq) {
1114 			ret = -EINVAL;
1115 			goto err_irq;
1116 		}
1117 		muic_irq->virq = virq;
1118 
1119 		ret = request_threaded_irq(virq, NULL,
1120 				max77693_muic_irq_handler,
1121 				IRQF_NO_SUSPEND,
1122 				muic_irq->name, info);
1123 		if (ret) {
1124 			dev_err(&pdev->dev,
1125 				"failed: irq request (IRQ: %d,"
1126 				" error :%d)\n",
1127 				muic_irq->irq, ret);
1128 			goto err_irq;
1129 		}
1130 	}
1131 
1132 	/* Initialize extcon device */
1133 	info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
1134 				  GFP_KERNEL);
1135 	if (!info->edev) {
1136 		dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1137 		ret = -ENOMEM;
1138 		goto err_irq;
1139 	}
1140 	info->edev->name = DEV_NAME;
1141 	info->edev->supported_cable = max77693_extcon_cable;
1142 	ret = extcon_dev_register(info->edev, NULL);
1143 	if (ret) {
1144 		dev_err(&pdev->dev, "failed to register extcon device\n");
1145 		goto err_irq;
1146 	}
1147 
1148 	/* Initialize MUIC register by using platform data */
1149 	for (i = 0 ; i < muic_pdata->num_init_data ; i++) {
1150 		enum max77693_irq_source irq_src = MAX77693_IRQ_GROUP_NR;
1151 
1152 		max77693_write_reg(info->max77693->regmap_muic,
1153 				muic_pdata->init_data[i].addr,
1154 				muic_pdata->init_data[i].data);
1155 
1156 		switch (muic_pdata->init_data[i].addr) {
1157 		case MAX77693_MUIC_REG_INTMASK1:
1158 			irq_src = MUIC_INT1;
1159 			break;
1160 		case MAX77693_MUIC_REG_INTMASK2:
1161 			irq_src = MUIC_INT2;
1162 			break;
1163 		case MAX77693_MUIC_REG_INTMASK3:
1164 			irq_src = MUIC_INT3;
1165 			break;
1166 		}
1167 
1168 		if (irq_src < MAX77693_IRQ_GROUP_NR)
1169 			info->max77693->irq_masks_cur[irq_src]
1170 				= muic_pdata->init_data[i].data;
1171 	}
1172 
1173 	/*
1174 	 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
1175 	 * h/w path of COMP2/COMN1 on CONTROL1 register.
1176 	 */
1177 	if (muic_pdata->path_uart)
1178 		info->path_uart = muic_pdata->path_uart;
1179 	else
1180 		info->path_uart = CONTROL1_SW_UART;
1181 
1182 	if (muic_pdata->path_usb)
1183 		info->path_usb = muic_pdata->path_usb;
1184 	else
1185 		info->path_usb = CONTROL1_SW_USB;
1186 
1187 	/* Set initial path for UART */
1188 	 max77693_muic_set_path(info, info->path_uart, true);
1189 
1190 	/* Check revision number of MUIC device*/
1191 	ret = max77693_read_reg(info->max77693->regmap_muic,
1192 			MAX77693_MUIC_REG_ID, &id);
1193 	if (ret < 0) {
1194 		dev_err(&pdev->dev, "failed to read revision number\n");
1195 		goto err_extcon;
1196 	}
1197 	dev_info(info->dev, "device ID : 0x%x\n", id);
1198 
1199 	/* Set ADC debounce time */
1200 	max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1201 
1202 	/*
1203 	 * Detect accessory after completing the initialization of platform
1204 	 *
1205 	 * - Use delayed workqueue to detect cable state and then
1206 	 * notify cable state to notifiee/platform through uevent.
1207 	 * After completing the booting of platform, the extcon provider
1208 	 * driver should notify cable state to upper layer.
1209 	 */
1210 	INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
1211 	if (muic_pdata->detcable_delay_ms)
1212 		delay_jiffies = msecs_to_jiffies(muic_pdata->detcable_delay_ms);
1213 	else
1214 		delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1215 	schedule_delayed_work(&info->wq_detcable, delay_jiffies);
1216 
1217 	return ret;
1218 
1219 err_extcon:
1220 	extcon_dev_unregister(info->edev);
1221 err_irq:
1222 	while (--i >= 0)
1223 		free_irq(muic_irqs[i].virq, info);
1224 	return ret;
1225 }
1226 
1227 static int max77693_muic_remove(struct platform_device *pdev)
1228 {
1229 	struct max77693_muic_info *info = platform_get_drvdata(pdev);
1230 	int i;
1231 
1232 	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
1233 		free_irq(muic_irqs[i].virq, info);
1234 	cancel_work_sync(&info->irq_work);
1235 	input_unregister_device(info->dock);
1236 	extcon_dev_unregister(info->edev);
1237 
1238 	return 0;
1239 }
1240 
1241 static struct platform_driver max77693_muic_driver = {
1242 	.driver		= {
1243 		.name	= DEV_NAME,
1244 		.owner	= THIS_MODULE,
1245 	},
1246 	.probe		= max77693_muic_probe,
1247 	.remove		= max77693_muic_remove,
1248 };
1249 
1250 module_platform_driver(max77693_muic_driver);
1251 
1252 MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
1253 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1254 MODULE_LICENSE("GPL");
1255 MODULE_ALIAS("platform:extcon-max77693");
1256