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