xref: /linux/drivers/extcon/extcon-sm5502.c (revision 8b45b6a0741678902810d7be95e635c210fbb198)
1914b881fSChanwoo Choi /*
2914b881fSChanwoo Choi  * extcon-sm5502.c - Silicon Mitus SM5502 extcon drvier to support USB switches
3914b881fSChanwoo Choi  *
4914b881fSChanwoo Choi  * Copyright (c) 2014 Samsung Electronics Co., Ltd
5914b881fSChanwoo Choi  * Author: Chanwoo Choi <cw00.choi@samsung.com>
6914b881fSChanwoo Choi  *
7914b881fSChanwoo Choi  * This program is free software; you can redistribute  it and/or modify it
8914b881fSChanwoo Choi  * under  the terms of  the GNU General  Public License as published by the
9914b881fSChanwoo Choi  * Free Software Foundation;  either version 2 of the  License, or (at your
10914b881fSChanwoo Choi  * option) any later version.
11914b881fSChanwoo Choi  */
12914b881fSChanwoo Choi 
13914b881fSChanwoo Choi #include <linux/err.h>
14914b881fSChanwoo Choi #include <linux/i2c.h>
15914b881fSChanwoo Choi #include <linux/interrupt.h>
16914b881fSChanwoo Choi #include <linux/irqdomain.h>
17914b881fSChanwoo Choi #include <linux/kernel.h>
18914b881fSChanwoo Choi #include <linux/module.h>
19914b881fSChanwoo Choi #include <linux/platform_device.h>
20914b881fSChanwoo Choi #include <linux/regmap.h>
21914b881fSChanwoo Choi #include <linux/slab.h>
22914b881fSChanwoo Choi #include <linux/extcon.h>
23ca2a07e4SChanwoo Choi 
24ca2a07e4SChanwoo Choi #include "extcon-sm5502.h"
25914b881fSChanwoo Choi 
26e1954452SChanwoo Choi #define	DELAY_MS_DEFAULT		17000	/* unit: millisecond */
27e1954452SChanwoo Choi 
28914b881fSChanwoo Choi struct muic_irq {
29914b881fSChanwoo Choi 	unsigned int irq;
30914b881fSChanwoo Choi 	const char *name;
31914b881fSChanwoo Choi 	unsigned int virq;
32914b881fSChanwoo Choi };
33914b881fSChanwoo Choi 
34914b881fSChanwoo Choi struct reg_data {
35914b881fSChanwoo Choi 	u8 reg;
36914b881fSChanwoo Choi 	unsigned int val;
37914b881fSChanwoo Choi 	bool invert;
38914b881fSChanwoo Choi };
39914b881fSChanwoo Choi 
40914b881fSChanwoo Choi struct sm5502_muic_info {
41914b881fSChanwoo Choi 	struct device *dev;
42914b881fSChanwoo Choi 	struct extcon_dev *edev;
43914b881fSChanwoo Choi 
44914b881fSChanwoo Choi 	struct i2c_client *i2c;
45914b881fSChanwoo Choi 	struct regmap *regmap;
46914b881fSChanwoo Choi 
47914b881fSChanwoo Choi 	struct regmap_irq_chip_data *irq_data;
48914b881fSChanwoo Choi 	struct muic_irq *muic_irqs;
49914b881fSChanwoo Choi 	unsigned int num_muic_irqs;
50914b881fSChanwoo Choi 	int irq;
51914b881fSChanwoo Choi 	bool irq_attach;
52914b881fSChanwoo Choi 	bool irq_detach;
53914b881fSChanwoo Choi 	struct work_struct irq_work;
54914b881fSChanwoo Choi 
55914b881fSChanwoo Choi 	struct reg_data *reg_data;
56914b881fSChanwoo Choi 	unsigned int num_reg_data;
57914b881fSChanwoo Choi 
58914b881fSChanwoo Choi 	struct mutex mutex;
59e1954452SChanwoo Choi 
60e1954452SChanwoo Choi 	/*
61e1954452SChanwoo Choi 	 * Use delayed workqueue to detect cable state and then
62e1954452SChanwoo Choi 	 * notify cable state to notifiee/platform through uevent.
63e1954452SChanwoo Choi 	 * After completing the booting of platform, the extcon provider
64e1954452SChanwoo Choi 	 * driver should notify cable state to upper layer.
65e1954452SChanwoo Choi 	 */
66e1954452SChanwoo Choi 	struct delayed_work wq_detcable;
67914b881fSChanwoo Choi };
68914b881fSChanwoo Choi 
69914b881fSChanwoo Choi /* Default value of SM5502 register to bring up MUIC device. */
70914b881fSChanwoo Choi static struct reg_data sm5502_reg_data[] = {
71914b881fSChanwoo Choi 	{
72914b881fSChanwoo Choi 		.reg = SM5502_REG_CONTROL,
73914b881fSChanwoo Choi 		.val = SM5502_REG_CONTROL_MASK_INT_MASK,
74914b881fSChanwoo Choi 		.invert = false,
75914b881fSChanwoo Choi 	}, {
76914b881fSChanwoo Choi 		.reg = SM5502_REG_INTMASK1,
77914b881fSChanwoo Choi 		.val = SM5502_REG_INTM1_KP_MASK
78914b881fSChanwoo Choi 			| SM5502_REG_INTM1_LKP_MASK
79914b881fSChanwoo Choi 			| SM5502_REG_INTM1_LKR_MASK,
80914b881fSChanwoo Choi 		.invert = true,
81914b881fSChanwoo Choi 	}, {
82914b881fSChanwoo Choi 		.reg = SM5502_REG_INTMASK2,
83914b881fSChanwoo Choi 		.val = SM5502_REG_INTM2_VBUS_DET_MASK
84914b881fSChanwoo Choi 			| SM5502_REG_INTM2_REV_ACCE_MASK
85914b881fSChanwoo Choi 			| SM5502_REG_INTM2_ADC_CHG_MASK
86914b881fSChanwoo Choi 			| SM5502_REG_INTM2_STUCK_KEY_MASK
87914b881fSChanwoo Choi 			| SM5502_REG_INTM2_STUCK_KEY_RCV_MASK
88914b881fSChanwoo Choi 			| SM5502_REG_INTM2_MHL_MASK,
89914b881fSChanwoo Choi 		.invert = true,
90914b881fSChanwoo Choi 	},
91914b881fSChanwoo Choi 	{ }
92914b881fSChanwoo Choi };
93914b881fSChanwoo Choi 
94914b881fSChanwoo Choi /* List of detectable cables */
9573b6ecdbSChanwoo Choi static const unsigned int sm5502_extcon_cable[] = {
962a9de9c0SChanwoo Choi 	EXTCON_USB,
972a9de9c0SChanwoo Choi 	EXTCON_USB_HOST,
98*8b45b6a0SChanwoo Choi 	EXTCON_CHG_USB_SDP,
9911eecf91SChanwoo Choi 	EXTCON_CHG_USB_DCP,
1002a9de9c0SChanwoo Choi 	EXTCON_NONE,
101914b881fSChanwoo Choi };
102914b881fSChanwoo Choi 
103914b881fSChanwoo Choi /* Define supported accessory type */
104914b881fSChanwoo Choi enum sm5502_muic_acc_type {
105914b881fSChanwoo Choi 	SM5502_MUIC_ADC_GROUND = 0x0,
106914b881fSChanwoo Choi 	SM5502_MUIC_ADC_SEND_END_BUTTON,
107914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S1_BUTTON,
108914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S2_BUTTON,
109914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S3_BUTTON,
110914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S4_BUTTON,
111914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S5_BUTTON,
112914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S6_BUTTON,
113914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S7_BUTTON,
114914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S8_BUTTON,
115914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S9_BUTTON,
116914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S10_BUTTON,
117914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S11_BUTTON,
118914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S12_BUTTON,
119914b881fSChanwoo Choi 	SM5502_MUIC_ADC_RESERVED_ACC_1,
120914b881fSChanwoo Choi 	SM5502_MUIC_ADC_RESERVED_ACC_2,
121914b881fSChanwoo Choi 	SM5502_MUIC_ADC_RESERVED_ACC_3,
122914b881fSChanwoo Choi 	SM5502_MUIC_ADC_RESERVED_ACC_4,
123914b881fSChanwoo Choi 	SM5502_MUIC_ADC_RESERVED_ACC_5,
124914b881fSChanwoo Choi 	SM5502_MUIC_ADC_AUDIO_TYPE2,
125914b881fSChanwoo Choi 	SM5502_MUIC_ADC_PHONE_POWERED_DEV,
126914b881fSChanwoo Choi 	SM5502_MUIC_ADC_TTY_CONVERTER,
127914b881fSChanwoo Choi 	SM5502_MUIC_ADC_UART_CABLE,
128914b881fSChanwoo Choi 	SM5502_MUIC_ADC_TYPE1_CHARGER,
129914b881fSChanwoo Choi 	SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
130914b881fSChanwoo Choi 	SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
131914b881fSChanwoo Choi 	SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE,
132914b881fSChanwoo Choi 	SM5502_MUIC_ADC_TYPE2_CHARGER,
133914b881fSChanwoo Choi 	SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
134914b881fSChanwoo Choi 	SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
135914b881fSChanwoo Choi 	SM5502_MUIC_ADC_AUDIO_TYPE1,
136914b881fSChanwoo Choi 	SM5502_MUIC_ADC_OPEN = 0x1f,
137914b881fSChanwoo Choi 
138914b881fSChanwoo Choi 	/* The below accessories have same ADC value (0x1f or 0x1e).
139914b881fSChanwoo Choi 	   So, Device type1 is used to separate specific accessory. */
140914b881fSChanwoo Choi 							/* |---------|--ADC| */
141914b881fSChanwoo Choi 							/* |    [7:5]|[4:0]| */
142914b881fSChanwoo Choi 	SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e,	/* |      001|11110| */
143914b881fSChanwoo Choi 	SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e,	/* |      010|11110| */
144914b881fSChanwoo Choi 							/* |Dev Type1|--ADC| */
145914b881fSChanwoo Choi 	SM5502_MUIC_ADC_OPEN_USB = 0x5f,		/* |      010|11111| */
146914b881fSChanwoo Choi 	SM5502_MUIC_ADC_OPEN_TA = 0xdf,			/* |      110|11111| */
147914b881fSChanwoo Choi 	SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff,		/* |      111|11111| */
148914b881fSChanwoo Choi };
149914b881fSChanwoo Choi 
150914b881fSChanwoo Choi /* List of supported interrupt for SM5502 */
151914b881fSChanwoo Choi static struct muic_irq sm5502_muic_irqs[] = {
152914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_ATTACH,	"muic-attach" },
153914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_DETACH,	"muic-detach" },
154914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_KP,		"muic-kp" },
155914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_LKP,		"muic-lkp" },
156914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_LKR,		"muic-lkr" },
157914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_OVP_EVENT,	"muic-ovp-event" },
158914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_OCP_EVENT,	"muic-ocp-event" },
159914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_OVP_OCP_DIS,	"muic-ovp-ocp-dis" },
160914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_VBUS_DET,	"muic-vbus-det" },
161914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_REV_ACCE,	"muic-rev-acce" },
162914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_ADC_CHG,	"muic-adc-chg" },
163914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_STUCK_KEY,	"muic-stuck-key" },
164914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" },
165914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_MHL,		"muic-mhl" },
166914b881fSChanwoo Choi };
167914b881fSChanwoo Choi 
168914b881fSChanwoo Choi /* Define interrupt list of SM5502 to register regmap_irq */
169914b881fSChanwoo Choi static const struct regmap_irq sm5502_irqs[] = {
170914b881fSChanwoo Choi 	/* INT1 interrupts */
171914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, },
172914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, },
173914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, },
174914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, },
175914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, },
176914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, },
177914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, },
178914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, },
179914b881fSChanwoo Choi 
180914b881fSChanwoo Choi 	/* INT2 interrupts */
181914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,},
182914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, },
183914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, },
184914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, },
185914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, },
186914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, },
187914b881fSChanwoo Choi };
188914b881fSChanwoo Choi 
189914b881fSChanwoo Choi static const struct regmap_irq_chip sm5502_muic_irq_chip = {
190914b881fSChanwoo Choi 	.name			= "sm5502",
191914b881fSChanwoo Choi 	.status_base		= SM5502_REG_INT1,
192914b881fSChanwoo Choi 	.mask_base		= SM5502_REG_INTMASK1,
193914b881fSChanwoo Choi 	.mask_invert		= false,
194914b881fSChanwoo Choi 	.num_regs		= 2,
195914b881fSChanwoo Choi 	.irqs			= sm5502_irqs,
196914b881fSChanwoo Choi 	.num_irqs		= ARRAY_SIZE(sm5502_irqs),
197914b881fSChanwoo Choi };
198914b881fSChanwoo Choi 
199914b881fSChanwoo Choi /* Define regmap configuration of SM5502 for I2C communication  */
200914b881fSChanwoo Choi static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg)
201914b881fSChanwoo Choi {
202914b881fSChanwoo Choi 	switch (reg) {
203914b881fSChanwoo Choi 	case SM5502_REG_INTMASK1:
204914b881fSChanwoo Choi 	case SM5502_REG_INTMASK2:
205914b881fSChanwoo Choi 		return true;
206914b881fSChanwoo Choi 	default:
207914b881fSChanwoo Choi 		break;
208914b881fSChanwoo Choi 	}
209914b881fSChanwoo Choi 	return false;
210914b881fSChanwoo Choi }
211914b881fSChanwoo Choi 
212914b881fSChanwoo Choi static const struct regmap_config sm5502_muic_regmap_config = {
213914b881fSChanwoo Choi 	.reg_bits	= 8,
214914b881fSChanwoo Choi 	.val_bits	= 8,
215914b881fSChanwoo Choi 	.volatile_reg	= sm5502_muic_volatile_reg,
216914b881fSChanwoo Choi 	.max_register	= SM5502_REG_END,
217914b881fSChanwoo Choi };
218914b881fSChanwoo Choi 
219a75fed2eSChanwoo Choi /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
220a75fed2eSChanwoo Choi static int sm5502_muic_set_path(struct sm5502_muic_info *info,
221a75fed2eSChanwoo Choi 				unsigned int con_sw, unsigned int vbus_sw,
222a75fed2eSChanwoo Choi 				bool attached)
223a75fed2eSChanwoo Choi {
224a75fed2eSChanwoo Choi 	int ret;
225a75fed2eSChanwoo Choi 
226a75fed2eSChanwoo Choi 	if (!attached) {
227a75fed2eSChanwoo Choi 		con_sw	= DM_DP_SWITCH_OPEN;
228a75fed2eSChanwoo Choi 		vbus_sw	= VBUSIN_SWITCH_OPEN;
229a75fed2eSChanwoo Choi 	}
230a75fed2eSChanwoo Choi 
231a75fed2eSChanwoo Choi 	switch (con_sw) {
232a75fed2eSChanwoo Choi 	case DM_DP_SWITCH_OPEN:
233a75fed2eSChanwoo Choi 	case DM_DP_SWITCH_USB:
234a75fed2eSChanwoo Choi 	case DM_DP_SWITCH_AUDIO:
235a75fed2eSChanwoo Choi 	case DM_DP_SWITCH_UART:
236a75fed2eSChanwoo Choi 		ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
237a75fed2eSChanwoo Choi 					 SM5502_REG_MANUAL_SW1_DP_MASK |
238a75fed2eSChanwoo Choi 					 SM5502_REG_MANUAL_SW1_DM_MASK,
239a75fed2eSChanwoo Choi 					 con_sw);
240a75fed2eSChanwoo Choi 		if (ret < 0) {
241a75fed2eSChanwoo Choi 			dev_err(info->dev,
242a75fed2eSChanwoo Choi 				"cannot update DM_CON/DP_CON switch\n");
243a75fed2eSChanwoo Choi 			return ret;
244a75fed2eSChanwoo Choi 		}
245a75fed2eSChanwoo Choi 		break;
246a75fed2eSChanwoo Choi 	default:
247a75fed2eSChanwoo Choi 		dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
248a75fed2eSChanwoo Choi 				con_sw);
249a75fed2eSChanwoo Choi 		return -EINVAL;
250a75fed2eSChanwoo Choi 	};
251a75fed2eSChanwoo Choi 
252a75fed2eSChanwoo Choi 	switch (vbus_sw) {
253a75fed2eSChanwoo Choi 	case VBUSIN_SWITCH_OPEN:
254a75fed2eSChanwoo Choi 	case VBUSIN_SWITCH_VBUSOUT:
255a75fed2eSChanwoo Choi 	case VBUSIN_SWITCH_MIC:
256a75fed2eSChanwoo Choi 	case VBUSIN_SWITCH_VBUSOUT_WITH_USB:
257a75fed2eSChanwoo Choi 		ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
258a75fed2eSChanwoo Choi 					 SM5502_REG_MANUAL_SW1_VBUSIN_MASK,
259a75fed2eSChanwoo Choi 					 vbus_sw);
260a75fed2eSChanwoo Choi 		if (ret < 0) {
261a75fed2eSChanwoo Choi 			dev_err(info->dev,
262a75fed2eSChanwoo Choi 				"cannot update VBUSIN switch\n");
263a75fed2eSChanwoo Choi 			return ret;
264a75fed2eSChanwoo Choi 		}
265a75fed2eSChanwoo Choi 		break;
266a75fed2eSChanwoo Choi 	default:
267a75fed2eSChanwoo Choi 		dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw);
268a75fed2eSChanwoo Choi 		return -EINVAL;
269a75fed2eSChanwoo Choi 	};
270a75fed2eSChanwoo Choi 
271a75fed2eSChanwoo Choi 	return 0;
272a75fed2eSChanwoo Choi }
273a75fed2eSChanwoo Choi 
274914b881fSChanwoo Choi /* Return cable type of attached or detached accessories */
275914b881fSChanwoo Choi static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info)
276914b881fSChanwoo Choi {
277914b881fSChanwoo Choi 	unsigned int cable_type = -1, adc, dev_type1;
278914b881fSChanwoo Choi 	int ret;
279914b881fSChanwoo Choi 
280914b881fSChanwoo Choi 	/* Read ADC value according to external cable or button */
281914b881fSChanwoo Choi 	ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc);
282914b881fSChanwoo Choi 	if (ret) {
283914b881fSChanwoo Choi 		dev_err(info->dev, "failed to read ADC register\n");
284914b881fSChanwoo Choi 		return ret;
285914b881fSChanwoo Choi 	}
286914b881fSChanwoo Choi 
287914b881fSChanwoo Choi 	/*
288914b881fSChanwoo Choi 	 * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't
289914b881fSChanwoo Choi 	 * connected with to MUIC device.
290914b881fSChanwoo Choi 	 */
2910ccc7955SChanwoo Choi 	cable_type = adc & SM5502_REG_ADC_MASK;
292914b881fSChanwoo Choi 	if (cable_type == SM5502_MUIC_ADC_GROUND)
293914b881fSChanwoo Choi 		return SM5502_MUIC_ADC_GROUND;
294914b881fSChanwoo Choi 
295914b881fSChanwoo Choi 	switch (cable_type) {
296914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_GROUND:
297914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_SEND_END_BUTTON:
298914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S1_BUTTON:
299914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S2_BUTTON:
300914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S3_BUTTON:
301914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S4_BUTTON:
302914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S5_BUTTON:
303914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S6_BUTTON:
304914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S7_BUTTON:
305914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S8_BUTTON:
306914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S9_BUTTON:
307914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S10_BUTTON:
308914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S11_BUTTON:
309914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S12_BUTTON:
310914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_RESERVED_ACC_1:
311914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_RESERVED_ACC_2:
312914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_RESERVED_ACC_3:
313914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_RESERVED_ACC_4:
314914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_RESERVED_ACC_5:
315914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_AUDIO_TYPE2:
316914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_PHONE_POWERED_DEV:
317914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_TTY_CONVERTER:
318914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_UART_CABLE:
319914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_TYPE1_CHARGER:
320914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
321914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
322914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE:
323914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_TYPE2_CHARGER:
324914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
325914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
326914b881fSChanwoo Choi 		break;
327914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_AUDIO_TYPE1:
328914b881fSChanwoo Choi 		/*
329914b881fSChanwoo Choi 		 * Check whether cable type is
330914b881fSChanwoo Choi 		 * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE
331914b881fSChanwoo Choi 		 * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END
332914b881fSChanwoo Choi 		 * by using Button event.
333914b881fSChanwoo Choi 		 */
334914b881fSChanwoo Choi 		break;
335914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_OPEN:
336914b881fSChanwoo Choi 		ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
337914b881fSChanwoo Choi 				  &dev_type1);
338914b881fSChanwoo Choi 		if (ret) {
339914b881fSChanwoo Choi 			dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
340914b881fSChanwoo Choi 			return ret;
341914b881fSChanwoo Choi 		}
342914b881fSChanwoo Choi 
343914b881fSChanwoo Choi 		switch (dev_type1) {
344914b881fSChanwoo Choi 		case SM5502_REG_DEV_TYPE1_USB_SDP_MASK:
345914b881fSChanwoo Choi 			cable_type = SM5502_MUIC_ADC_OPEN_USB;
346914b881fSChanwoo Choi 			break;
347914b881fSChanwoo Choi 		case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK:
348914b881fSChanwoo Choi 			cable_type = SM5502_MUIC_ADC_OPEN_TA;
349914b881fSChanwoo Choi 			break;
350914b881fSChanwoo Choi 		case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
351914b881fSChanwoo Choi 			cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG;
352914b881fSChanwoo Choi 			break;
353914b881fSChanwoo Choi 		default:
354914b881fSChanwoo Choi 			dev_dbg(info->dev,
35534825e51SChanwoo Choi 				"cannot identify the cable type: adc(0x%x)\n",
35634825e51SChanwoo Choi 				adc);
357914b881fSChanwoo Choi 			return -EINVAL;
358914b881fSChanwoo Choi 		};
359914b881fSChanwoo Choi 		break;
360914b881fSChanwoo Choi 	default:
361914b881fSChanwoo Choi 		dev_err(info->dev,
362914b881fSChanwoo Choi 			"failed to identify the cable type: adc(0x%x)\n", adc);
363914b881fSChanwoo Choi 		return -EINVAL;
364914b881fSChanwoo Choi 	};
365914b881fSChanwoo Choi 
366914b881fSChanwoo Choi 	return cable_type;
367914b881fSChanwoo Choi }
368914b881fSChanwoo Choi 
369914b881fSChanwoo Choi static int sm5502_muic_cable_handler(struct sm5502_muic_info *info,
370914b881fSChanwoo Choi 				     bool attached)
371914b881fSChanwoo Choi {
372914b881fSChanwoo Choi 	static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND;
373914b881fSChanwoo Choi 	unsigned int cable_type = SM5502_MUIC_ADC_GROUND;
374a75fed2eSChanwoo Choi 	unsigned int con_sw = DM_DP_SWITCH_OPEN;
375a75fed2eSChanwoo Choi 	unsigned int vbus_sw = VBUSIN_SWITCH_OPEN;
37673b6ecdbSChanwoo Choi 	unsigned int id;
377a75fed2eSChanwoo Choi 	int ret;
378914b881fSChanwoo Choi 
379914b881fSChanwoo Choi 	/* Get the type of attached or detached cable */
380914b881fSChanwoo Choi 	if (attached)
381914b881fSChanwoo Choi 		cable_type = sm5502_muic_get_cable_type(info);
382fbae30d8SChanwoo Choi 	else
383914b881fSChanwoo Choi 		cable_type = prev_cable_type;
384914b881fSChanwoo Choi 	prev_cable_type = cable_type;
385914b881fSChanwoo Choi 
386914b881fSChanwoo Choi 	switch (cable_type) {
387914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_OPEN_USB:
3882a9de9c0SChanwoo Choi 		id	= EXTCON_USB;
389a75fed2eSChanwoo Choi 		con_sw	= DM_DP_SWITCH_USB;
390a75fed2eSChanwoo Choi 		vbus_sw	= VBUSIN_SWITCH_VBUSOUT_WITH_USB;
391914b881fSChanwoo Choi 		break;
392914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_OPEN_TA:
39311eecf91SChanwoo Choi 		id	= EXTCON_CHG_USB_DCP;
394a75fed2eSChanwoo Choi 		con_sw	= DM_DP_SWITCH_OPEN;
395a75fed2eSChanwoo Choi 		vbus_sw	= VBUSIN_SWITCH_VBUSOUT;
396914b881fSChanwoo Choi 		break;
397914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_OPEN_USB_OTG:
3982a9de9c0SChanwoo Choi 		id	= EXTCON_USB_HOST;
399a75fed2eSChanwoo Choi 		con_sw	= DM_DP_SWITCH_USB;
400a75fed2eSChanwoo Choi 		vbus_sw	= VBUSIN_SWITCH_OPEN;
401e1954452SChanwoo Choi 		break;
402914b881fSChanwoo Choi 	default:
403914b881fSChanwoo Choi 		dev_dbg(info->dev,
404914b881fSChanwoo Choi 			"cannot handle this cable_type (0x%x)\n", cable_type);
405914b881fSChanwoo Choi 		return 0;
406914b881fSChanwoo Choi 	};
407914b881fSChanwoo Choi 
408a75fed2eSChanwoo Choi 	/* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */
409a75fed2eSChanwoo Choi 	ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached);
410a75fed2eSChanwoo Choi 	if (ret < 0)
411a75fed2eSChanwoo Choi 		return ret;
412a75fed2eSChanwoo Choi 
413a75fed2eSChanwoo Choi 	/* Change the state of external accessory */
4142a9de9c0SChanwoo Choi 	extcon_set_cable_state_(info->edev, id, attached);
415*8b45b6a0SChanwoo Choi 	if (id == EXTCON_USB)
416*8b45b6a0SChanwoo Choi 		extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_SDP,
417*8b45b6a0SChanwoo Choi 					attached);
418914b881fSChanwoo Choi 
419914b881fSChanwoo Choi 	return 0;
420914b881fSChanwoo Choi }
421914b881fSChanwoo Choi 
422914b881fSChanwoo Choi static void sm5502_muic_irq_work(struct work_struct *work)
423914b881fSChanwoo Choi {
424914b881fSChanwoo Choi 	struct sm5502_muic_info *info = container_of(work,
425914b881fSChanwoo Choi 			struct sm5502_muic_info, irq_work);
426914b881fSChanwoo Choi 	int ret = 0;
427914b881fSChanwoo Choi 
428914b881fSChanwoo Choi 	if (!info->edev)
429914b881fSChanwoo Choi 		return;
430914b881fSChanwoo Choi 
431914b881fSChanwoo Choi 	mutex_lock(&info->mutex);
432914b881fSChanwoo Choi 
433914b881fSChanwoo Choi 	/* Detect attached or detached cables */
434914b881fSChanwoo Choi 	if (info->irq_attach) {
435914b881fSChanwoo Choi 		ret = sm5502_muic_cable_handler(info, true);
436914b881fSChanwoo Choi 		info->irq_attach = false;
437914b881fSChanwoo Choi 	}
438914b881fSChanwoo Choi 	if (info->irq_detach) {
439914b881fSChanwoo Choi 		ret = sm5502_muic_cable_handler(info, false);
440914b881fSChanwoo Choi 		info->irq_detach = false;
441914b881fSChanwoo Choi 	}
442914b881fSChanwoo Choi 
443914b881fSChanwoo Choi 	if (ret < 0)
444914b881fSChanwoo Choi 		dev_err(info->dev, "failed to handle MUIC interrupt\n");
445914b881fSChanwoo Choi 
446914b881fSChanwoo Choi 	mutex_unlock(&info->mutex);
447914b881fSChanwoo Choi }
448914b881fSChanwoo Choi 
449914b881fSChanwoo Choi /*
450914b881fSChanwoo Choi  * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0.
451914b881fSChanwoo Choi  * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
452914b881fSChanwoo Choi  */
453914b881fSChanwoo Choi static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type)
454914b881fSChanwoo Choi {
455914b881fSChanwoo Choi 	switch (irq_type) {
456914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_ATTACH:
457914b881fSChanwoo Choi 		info->irq_attach = true;
458914b881fSChanwoo Choi 		break;
459914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_DETACH:
460914b881fSChanwoo Choi 		info->irq_detach = true;
461914b881fSChanwoo Choi 		break;
462914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_KP:
463914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_LKP:
464914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_LKR:
465914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_OVP_EVENT:
466914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_OCP_EVENT:
467914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_OVP_OCP_DIS:
468914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_VBUS_DET:
469914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_REV_ACCE:
470914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_ADC_CHG:
471914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_STUCK_KEY:
472914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_STUCK_KEY_RCV:
473914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_MHL:
474914b881fSChanwoo Choi 	default:
475914b881fSChanwoo Choi 		break;
476914b881fSChanwoo Choi 	}
477914b881fSChanwoo Choi 
478914b881fSChanwoo Choi 	return 0;
479914b881fSChanwoo Choi }
480914b881fSChanwoo Choi 
481914b881fSChanwoo Choi static irqreturn_t sm5502_muic_irq_handler(int irq, void *data)
482914b881fSChanwoo Choi {
483914b881fSChanwoo Choi 	struct sm5502_muic_info *info = data;
484914b881fSChanwoo Choi 	int i, irq_type = -1, ret;
485914b881fSChanwoo Choi 
486914b881fSChanwoo Choi 	for (i = 0; i < info->num_muic_irqs; i++)
487914b881fSChanwoo Choi 		if (irq == info->muic_irqs[i].virq)
488914b881fSChanwoo Choi 			irq_type = info->muic_irqs[i].irq;
489914b881fSChanwoo Choi 
490914b881fSChanwoo Choi 	ret = sm5502_parse_irq(info, irq_type);
491914b881fSChanwoo Choi 	if (ret < 0) {
492914b881fSChanwoo Choi 		dev_warn(info->dev, "cannot handle is interrupt:%d\n",
493914b881fSChanwoo Choi 				    irq_type);
494914b881fSChanwoo Choi 		return IRQ_HANDLED;
495914b881fSChanwoo Choi 	}
496914b881fSChanwoo Choi 	schedule_work(&info->irq_work);
497914b881fSChanwoo Choi 
498914b881fSChanwoo Choi 	return IRQ_HANDLED;
499914b881fSChanwoo Choi }
500914b881fSChanwoo Choi 
501e1954452SChanwoo Choi static void sm5502_muic_detect_cable_wq(struct work_struct *work)
502e1954452SChanwoo Choi {
503e1954452SChanwoo Choi 	struct sm5502_muic_info *info = container_of(to_delayed_work(work),
504e1954452SChanwoo Choi 				struct sm5502_muic_info, wq_detcable);
505e1954452SChanwoo Choi 	int ret;
506e1954452SChanwoo Choi 
507e1954452SChanwoo Choi 	/* Notify the state of connector cable or not  */
508e1954452SChanwoo Choi 	ret = sm5502_muic_cable_handler(info, true);
509e1954452SChanwoo Choi 	if (ret < 0)
510e1954452SChanwoo Choi 		dev_warn(info->dev, "failed to detect cable state\n");
511e1954452SChanwoo Choi }
512e1954452SChanwoo Choi 
513914b881fSChanwoo Choi static void sm5502_init_dev_type(struct sm5502_muic_info *info)
514914b881fSChanwoo Choi {
515914b881fSChanwoo Choi 	unsigned int reg_data, vendor_id, version_id;
516914b881fSChanwoo Choi 	int i, ret;
517914b881fSChanwoo Choi 
518914b881fSChanwoo Choi 	/* To test I2C, Print version_id and vendor_id of SM5502 */
519914b881fSChanwoo Choi 	ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, &reg_data);
520914b881fSChanwoo Choi 	if (ret) {
521914b881fSChanwoo Choi 		dev_err(info->dev,
522914b881fSChanwoo Choi 			"failed to read DEVICE_ID register: %d\n", ret);
523914b881fSChanwoo Choi 		return;
524914b881fSChanwoo Choi 	}
525914b881fSChanwoo Choi 
526914b881fSChanwoo Choi 	vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >>
527914b881fSChanwoo Choi 				SM5502_REG_DEVICE_ID_VENDOR_SHIFT);
528914b881fSChanwoo Choi 	version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >>
529914b881fSChanwoo Choi 				SM5502_REG_DEVICE_ID_VERSION_SHIFT);
530914b881fSChanwoo Choi 
531914b881fSChanwoo Choi 	dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
532914b881fSChanwoo Choi 			    version_id, vendor_id);
533914b881fSChanwoo Choi 
534914b881fSChanwoo Choi 	/* Initiazle the register of SM5502 device to bring-up */
535914b881fSChanwoo Choi 	for (i = 0; i < info->num_reg_data; i++) {
536914b881fSChanwoo Choi 		unsigned int val = 0;
537914b881fSChanwoo Choi 
538914b881fSChanwoo Choi 		if (!info->reg_data[i].invert)
539914b881fSChanwoo Choi 			val |= ~info->reg_data[i].val;
540914b881fSChanwoo Choi 		else
541914b881fSChanwoo Choi 			val = info->reg_data[i].val;
542914b881fSChanwoo Choi 		regmap_write(info->regmap, info->reg_data[i].reg, val);
543914b881fSChanwoo Choi 	}
544914b881fSChanwoo Choi }
545914b881fSChanwoo Choi 
546914b881fSChanwoo Choi static int sm5022_muic_i2c_probe(struct i2c_client *i2c,
547914b881fSChanwoo Choi 				 const struct i2c_device_id *id)
548914b881fSChanwoo Choi {
549914b881fSChanwoo Choi 	struct device_node *np = i2c->dev.of_node;
550914b881fSChanwoo Choi 	struct sm5502_muic_info *info;
551914b881fSChanwoo Choi 	int i, ret, irq_flags;
552914b881fSChanwoo Choi 
553914b881fSChanwoo Choi 	if (!np)
554914b881fSChanwoo Choi 		return -EINVAL;
555914b881fSChanwoo Choi 
556914b881fSChanwoo Choi 	info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
557914b881fSChanwoo Choi 	if (!info)
558914b881fSChanwoo Choi 		return -ENOMEM;
559914b881fSChanwoo Choi 	i2c_set_clientdata(i2c, info);
560914b881fSChanwoo Choi 
561914b881fSChanwoo Choi 	info->dev = &i2c->dev;
562914b881fSChanwoo Choi 	info->i2c = i2c;
563914b881fSChanwoo Choi 	info->irq = i2c->irq;
564914b881fSChanwoo Choi 	info->muic_irqs = sm5502_muic_irqs;
565914b881fSChanwoo Choi 	info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs);
566914b881fSChanwoo Choi 	info->reg_data = sm5502_reg_data;
567914b881fSChanwoo Choi 	info->num_reg_data = ARRAY_SIZE(sm5502_reg_data);
568914b881fSChanwoo Choi 
569914b881fSChanwoo Choi 	mutex_init(&info->mutex);
570914b881fSChanwoo Choi 
571914b881fSChanwoo Choi 	INIT_WORK(&info->irq_work, sm5502_muic_irq_work);
572914b881fSChanwoo Choi 
573914b881fSChanwoo Choi 	info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config);
574914b881fSChanwoo Choi 	if (IS_ERR(info->regmap)) {
575914b881fSChanwoo Choi 		ret = PTR_ERR(info->regmap);
576914b881fSChanwoo Choi 		dev_err(info->dev, "failed to allocate register map: %d\n",
577914b881fSChanwoo Choi 				   ret);
578914b881fSChanwoo Choi 		return ret;
579914b881fSChanwoo Choi 	}
580914b881fSChanwoo Choi 
581914b881fSChanwoo Choi 	/* Support irq domain for SM5502 MUIC device */
582914b881fSChanwoo Choi 	irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
583914b881fSChanwoo Choi 	ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0,
584914b881fSChanwoo Choi 				  &sm5502_muic_irq_chip, &info->irq_data);
585914b881fSChanwoo Choi 	if (ret != 0) {
586914b881fSChanwoo Choi 		dev_err(info->dev, "failed to request IRQ %d: %d\n",
587914b881fSChanwoo Choi 				    info->irq, ret);
588914b881fSChanwoo Choi 		return ret;
589914b881fSChanwoo Choi 	}
590914b881fSChanwoo Choi 
591914b881fSChanwoo Choi 	for (i = 0; i < info->num_muic_irqs; i++) {
592914b881fSChanwoo Choi 		struct muic_irq *muic_irq = &info->muic_irqs[i];
593363b3891SAndrzej Hajda 		int virq = 0;
594914b881fSChanwoo Choi 
595914b881fSChanwoo Choi 		virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
596914b881fSChanwoo Choi 		if (virq <= 0)
597914b881fSChanwoo Choi 			return -EINVAL;
598914b881fSChanwoo Choi 		muic_irq->virq = virq;
599914b881fSChanwoo Choi 
600914b881fSChanwoo Choi 		ret = devm_request_threaded_irq(info->dev, virq, NULL,
601914b881fSChanwoo Choi 						sm5502_muic_irq_handler,
602914b881fSChanwoo Choi 						IRQF_NO_SUSPEND,
603914b881fSChanwoo Choi 						muic_irq->name, info);
604914b881fSChanwoo Choi 		if (ret) {
605fbae30d8SChanwoo Choi 			dev_err(info->dev,
606fbae30d8SChanwoo Choi 				"failed: irq request (IRQ: %d, error :%d)\n",
607fbae30d8SChanwoo Choi 				muic_irq->irq, ret);
608914b881fSChanwoo Choi 			return ret;
609914b881fSChanwoo Choi 		}
610914b881fSChanwoo Choi 	}
611914b881fSChanwoo Choi 
612914b881fSChanwoo Choi 	/* Allocate extcon device */
613914b881fSChanwoo Choi 	info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable);
614914b881fSChanwoo Choi 	if (IS_ERR(info->edev)) {
615914b881fSChanwoo Choi 		dev_err(info->dev, "failed to allocate memory for extcon\n");
616914b881fSChanwoo Choi 		return -ENOMEM;
617914b881fSChanwoo Choi 	}
618914b881fSChanwoo Choi 
619914b881fSChanwoo Choi 	/* Register extcon device */
620914b881fSChanwoo Choi 	ret = devm_extcon_dev_register(info->dev, info->edev);
621914b881fSChanwoo Choi 	if (ret) {
622914b881fSChanwoo Choi 		dev_err(info->dev, "failed to register extcon device\n");
623914b881fSChanwoo Choi 		return ret;
624914b881fSChanwoo Choi 	}
625914b881fSChanwoo Choi 
626e1954452SChanwoo Choi 	/*
627e1954452SChanwoo Choi 	 * Detect accessory after completing the initialization of platform
628e1954452SChanwoo Choi 	 *
629e1954452SChanwoo Choi 	 * - Use delayed workqueue to detect cable state and then
630e1954452SChanwoo Choi 	 * notify cable state to notifiee/platform through uevent.
631e1954452SChanwoo Choi 	 * After completing the booting of platform, the extcon provider
632e1954452SChanwoo Choi 	 * driver should notify cable state to upper layer.
633e1954452SChanwoo Choi 	 */
634e1954452SChanwoo Choi 	INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
635e1954452SChanwoo Choi 	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
636e1954452SChanwoo Choi 			msecs_to_jiffies(DELAY_MS_DEFAULT));
637e1954452SChanwoo Choi 
638914b881fSChanwoo Choi 	/* Initialize SM5502 device and print vendor id and version id */
639914b881fSChanwoo Choi 	sm5502_init_dev_type(info);
640914b881fSChanwoo Choi 
641914b881fSChanwoo Choi 	return 0;
642914b881fSChanwoo Choi }
643914b881fSChanwoo Choi 
644914b881fSChanwoo Choi static int sm5502_muic_i2c_remove(struct i2c_client *i2c)
645914b881fSChanwoo Choi {
646914b881fSChanwoo Choi 	struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
647914b881fSChanwoo Choi 
648914b881fSChanwoo Choi 	regmap_del_irq_chip(info->irq, info->irq_data);
649914b881fSChanwoo Choi 
650914b881fSChanwoo Choi 	return 0;
651914b881fSChanwoo Choi }
652914b881fSChanwoo Choi 
65334825e51SChanwoo Choi static const struct of_device_id sm5502_dt_match[] = {
654914b881fSChanwoo Choi 	{ .compatible = "siliconmitus,sm5502-muic" },
655914b881fSChanwoo Choi 	{ },
656914b881fSChanwoo Choi };
657ff612f91SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, sm5502_dt_match);
658914b881fSChanwoo Choi 
659914b881fSChanwoo Choi #ifdef CONFIG_PM_SLEEP
660914b881fSChanwoo Choi static int sm5502_muic_suspend(struct device *dev)
661914b881fSChanwoo Choi {
662d5859342SGeliang Tang 	struct i2c_client *i2c = to_i2c_client(dev);
663914b881fSChanwoo Choi 	struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
664914b881fSChanwoo Choi 
665914b881fSChanwoo Choi 	enable_irq_wake(info->irq);
666914b881fSChanwoo Choi 
667914b881fSChanwoo Choi 	return 0;
668914b881fSChanwoo Choi }
669914b881fSChanwoo Choi 
670914b881fSChanwoo Choi static int sm5502_muic_resume(struct device *dev)
671914b881fSChanwoo Choi {
672d5859342SGeliang Tang 	struct i2c_client *i2c = to_i2c_client(dev);
673914b881fSChanwoo Choi 	struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
674914b881fSChanwoo Choi 
675914b881fSChanwoo Choi 	disable_irq_wake(info->irq);
676914b881fSChanwoo Choi 
677914b881fSChanwoo Choi 	return 0;
678914b881fSChanwoo Choi }
679914b881fSChanwoo Choi #endif
680914b881fSChanwoo Choi 
681914b881fSChanwoo Choi static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops,
682914b881fSChanwoo Choi 			 sm5502_muic_suspend, sm5502_muic_resume);
683914b881fSChanwoo Choi 
684914b881fSChanwoo Choi static const struct i2c_device_id sm5502_i2c_id[] = {
685914b881fSChanwoo Choi 	{ "sm5502", TYPE_SM5502 },
686914b881fSChanwoo Choi 	{ }
687914b881fSChanwoo Choi };
688914b881fSChanwoo Choi MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id);
689914b881fSChanwoo Choi 
690914b881fSChanwoo Choi static struct i2c_driver sm5502_muic_i2c_driver = {
691914b881fSChanwoo Choi 	.driver		= {
692914b881fSChanwoo Choi 		.name	= "sm5502",
693914b881fSChanwoo Choi 		.pm	= &sm5502_muic_pm_ops,
694914b881fSChanwoo Choi 		.of_match_table = sm5502_dt_match,
695914b881fSChanwoo Choi 	},
696914b881fSChanwoo Choi 	.probe	= sm5022_muic_i2c_probe,
697914b881fSChanwoo Choi 	.remove	= sm5502_muic_i2c_remove,
698914b881fSChanwoo Choi 	.id_table = sm5502_i2c_id,
699914b881fSChanwoo Choi };
700914b881fSChanwoo Choi 
701914b881fSChanwoo Choi static int __init sm5502_muic_i2c_init(void)
702914b881fSChanwoo Choi {
703914b881fSChanwoo Choi 	return i2c_add_driver(&sm5502_muic_i2c_driver);
704914b881fSChanwoo Choi }
705914b881fSChanwoo Choi subsys_initcall(sm5502_muic_i2c_init);
706914b881fSChanwoo Choi 
707914b881fSChanwoo Choi MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver");
708914b881fSChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
709914b881fSChanwoo Choi MODULE_LICENSE("GPL");
710