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