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