xref: /linux/drivers/extcon/extcon-sm5502.c (revision 73b6ecdb93e8e77752cae9077c424fcdc6f23c39)
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 */
95*73b6ecdbSChanwoo Choi static const unsigned int sm5502_extcon_cable[] = {
962a9de9c0SChanwoo Choi 	EXTCON_USB,
972a9de9c0SChanwoo Choi 	EXTCON_USB_HOST,
982a9de9c0SChanwoo Choi 	EXTCON_TA,
992a9de9c0SChanwoo Choi 	EXTCON_NONE,
100914b881fSChanwoo Choi };
101914b881fSChanwoo Choi 
102914b881fSChanwoo Choi /* Define supported accessory type */
103914b881fSChanwoo Choi enum sm5502_muic_acc_type {
104914b881fSChanwoo Choi 	SM5502_MUIC_ADC_GROUND = 0x0,
105914b881fSChanwoo Choi 	SM5502_MUIC_ADC_SEND_END_BUTTON,
106914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S1_BUTTON,
107914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S2_BUTTON,
108914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S3_BUTTON,
109914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S4_BUTTON,
110914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S5_BUTTON,
111914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S6_BUTTON,
112914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S7_BUTTON,
113914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S8_BUTTON,
114914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S9_BUTTON,
115914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S10_BUTTON,
116914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S11_BUTTON,
117914b881fSChanwoo Choi 	SM5502_MUIC_ADC_REMOTE_S12_BUTTON,
118914b881fSChanwoo Choi 	SM5502_MUIC_ADC_RESERVED_ACC_1,
119914b881fSChanwoo Choi 	SM5502_MUIC_ADC_RESERVED_ACC_2,
120914b881fSChanwoo Choi 	SM5502_MUIC_ADC_RESERVED_ACC_3,
121914b881fSChanwoo Choi 	SM5502_MUIC_ADC_RESERVED_ACC_4,
122914b881fSChanwoo Choi 	SM5502_MUIC_ADC_RESERVED_ACC_5,
123914b881fSChanwoo Choi 	SM5502_MUIC_ADC_AUDIO_TYPE2,
124914b881fSChanwoo Choi 	SM5502_MUIC_ADC_PHONE_POWERED_DEV,
125914b881fSChanwoo Choi 	SM5502_MUIC_ADC_TTY_CONVERTER,
126914b881fSChanwoo Choi 	SM5502_MUIC_ADC_UART_CABLE,
127914b881fSChanwoo Choi 	SM5502_MUIC_ADC_TYPE1_CHARGER,
128914b881fSChanwoo Choi 	SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
129914b881fSChanwoo Choi 	SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
130914b881fSChanwoo Choi 	SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE,
131914b881fSChanwoo Choi 	SM5502_MUIC_ADC_TYPE2_CHARGER,
132914b881fSChanwoo Choi 	SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
133914b881fSChanwoo Choi 	SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
134914b881fSChanwoo Choi 	SM5502_MUIC_ADC_AUDIO_TYPE1,
135914b881fSChanwoo Choi 	SM5502_MUIC_ADC_OPEN = 0x1f,
136914b881fSChanwoo Choi 
137914b881fSChanwoo Choi 	/* The below accessories have same ADC value (0x1f or 0x1e).
138914b881fSChanwoo Choi 	   So, Device type1 is used to separate specific accessory. */
139914b881fSChanwoo Choi 							/* |---------|--ADC| */
140914b881fSChanwoo Choi 							/* |    [7:5]|[4:0]| */
141914b881fSChanwoo Choi 	SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e,	/* |      001|11110| */
142914b881fSChanwoo Choi 	SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e,	/* |      010|11110| */
143914b881fSChanwoo Choi 							/* |Dev Type1|--ADC| */
144914b881fSChanwoo Choi 	SM5502_MUIC_ADC_OPEN_USB = 0x5f,		/* |      010|11111| */
145914b881fSChanwoo Choi 	SM5502_MUIC_ADC_OPEN_TA = 0xdf,			/* |      110|11111| */
146914b881fSChanwoo Choi 	SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff,		/* |      111|11111| */
147914b881fSChanwoo Choi };
148914b881fSChanwoo Choi 
149914b881fSChanwoo Choi /* List of supported interrupt for SM5502 */
150914b881fSChanwoo Choi static struct muic_irq sm5502_muic_irqs[] = {
151914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_ATTACH,	"muic-attach" },
152914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_DETACH,	"muic-detach" },
153914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_KP,		"muic-kp" },
154914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_LKP,		"muic-lkp" },
155914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_LKR,		"muic-lkr" },
156914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_OVP_EVENT,	"muic-ovp-event" },
157914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_OCP_EVENT,	"muic-ocp-event" },
158914b881fSChanwoo Choi 	{ SM5502_IRQ_INT1_OVP_OCP_DIS,	"muic-ovp-ocp-dis" },
159914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_VBUS_DET,	"muic-vbus-det" },
160914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_REV_ACCE,	"muic-rev-acce" },
161914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_ADC_CHG,	"muic-adc-chg" },
162914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_STUCK_KEY,	"muic-stuck-key" },
163914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" },
164914b881fSChanwoo Choi 	{ SM5502_IRQ_INT2_MHL,		"muic-mhl" },
165914b881fSChanwoo Choi };
166914b881fSChanwoo Choi 
167914b881fSChanwoo Choi /* Define interrupt list of SM5502 to register regmap_irq */
168914b881fSChanwoo Choi static const struct regmap_irq sm5502_irqs[] = {
169914b881fSChanwoo Choi 	/* INT1 interrupts */
170914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, },
171914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, },
172914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, },
173914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, },
174914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, },
175914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, },
176914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, },
177914b881fSChanwoo Choi 	{ .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, },
178914b881fSChanwoo Choi 
179914b881fSChanwoo Choi 	/* INT2 interrupts */
180914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,},
181914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, },
182914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, },
183914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, },
184914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, },
185914b881fSChanwoo Choi 	{ .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, },
186914b881fSChanwoo Choi };
187914b881fSChanwoo Choi 
188914b881fSChanwoo Choi static const struct regmap_irq_chip sm5502_muic_irq_chip = {
189914b881fSChanwoo Choi 	.name			= "sm5502",
190914b881fSChanwoo Choi 	.status_base		= SM5502_REG_INT1,
191914b881fSChanwoo Choi 	.mask_base		= SM5502_REG_INTMASK1,
192914b881fSChanwoo Choi 	.mask_invert		= false,
193914b881fSChanwoo Choi 	.num_regs		= 2,
194914b881fSChanwoo Choi 	.irqs			= sm5502_irqs,
195914b881fSChanwoo Choi 	.num_irqs		= ARRAY_SIZE(sm5502_irqs),
196914b881fSChanwoo Choi };
197914b881fSChanwoo Choi 
198914b881fSChanwoo Choi /* Define regmap configuration of SM5502 for I2C communication  */
199914b881fSChanwoo Choi static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg)
200914b881fSChanwoo Choi {
201914b881fSChanwoo Choi 	switch (reg) {
202914b881fSChanwoo Choi 	case SM5502_REG_INTMASK1:
203914b881fSChanwoo Choi 	case SM5502_REG_INTMASK2:
204914b881fSChanwoo Choi 		return true;
205914b881fSChanwoo Choi 	default:
206914b881fSChanwoo Choi 		break;
207914b881fSChanwoo Choi 	}
208914b881fSChanwoo Choi 	return false;
209914b881fSChanwoo Choi }
210914b881fSChanwoo Choi 
211914b881fSChanwoo Choi static const struct regmap_config sm5502_muic_regmap_config = {
212914b881fSChanwoo Choi 	.reg_bits	= 8,
213914b881fSChanwoo Choi 	.val_bits	= 8,
214914b881fSChanwoo Choi 	.volatile_reg	= sm5502_muic_volatile_reg,
215914b881fSChanwoo Choi 	.max_register	= SM5502_REG_END,
216914b881fSChanwoo Choi };
217914b881fSChanwoo Choi 
218a75fed2eSChanwoo Choi /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
219a75fed2eSChanwoo Choi static int sm5502_muic_set_path(struct sm5502_muic_info *info,
220a75fed2eSChanwoo Choi 				unsigned int con_sw, unsigned int vbus_sw,
221a75fed2eSChanwoo Choi 				bool attached)
222a75fed2eSChanwoo Choi {
223a75fed2eSChanwoo Choi 	int ret;
224a75fed2eSChanwoo Choi 
225a75fed2eSChanwoo Choi 	if (!attached) {
226a75fed2eSChanwoo Choi 		con_sw	= DM_DP_SWITCH_OPEN;
227a75fed2eSChanwoo Choi 		vbus_sw	= VBUSIN_SWITCH_OPEN;
228a75fed2eSChanwoo Choi 	}
229a75fed2eSChanwoo Choi 
230a75fed2eSChanwoo Choi 	switch (con_sw) {
231a75fed2eSChanwoo Choi 	case DM_DP_SWITCH_OPEN:
232a75fed2eSChanwoo Choi 	case DM_DP_SWITCH_USB:
233a75fed2eSChanwoo Choi 	case DM_DP_SWITCH_AUDIO:
234a75fed2eSChanwoo Choi 	case DM_DP_SWITCH_UART:
235a75fed2eSChanwoo Choi 		ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
236a75fed2eSChanwoo Choi 					 SM5502_REG_MANUAL_SW1_DP_MASK |
237a75fed2eSChanwoo Choi 					 SM5502_REG_MANUAL_SW1_DM_MASK,
238a75fed2eSChanwoo Choi 					 con_sw);
239a75fed2eSChanwoo Choi 		if (ret < 0) {
240a75fed2eSChanwoo Choi 			dev_err(info->dev,
241a75fed2eSChanwoo Choi 				"cannot update DM_CON/DP_CON switch\n");
242a75fed2eSChanwoo Choi 			return ret;
243a75fed2eSChanwoo Choi 		}
244a75fed2eSChanwoo Choi 		break;
245a75fed2eSChanwoo Choi 	default:
246a75fed2eSChanwoo Choi 		dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
247a75fed2eSChanwoo Choi 				con_sw);
248a75fed2eSChanwoo Choi 		return -EINVAL;
249a75fed2eSChanwoo Choi 	};
250a75fed2eSChanwoo Choi 
251a75fed2eSChanwoo Choi 	switch (vbus_sw) {
252a75fed2eSChanwoo Choi 	case VBUSIN_SWITCH_OPEN:
253a75fed2eSChanwoo Choi 	case VBUSIN_SWITCH_VBUSOUT:
254a75fed2eSChanwoo Choi 	case VBUSIN_SWITCH_MIC:
255a75fed2eSChanwoo Choi 	case VBUSIN_SWITCH_VBUSOUT_WITH_USB:
256a75fed2eSChanwoo Choi 		ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
257a75fed2eSChanwoo Choi 					 SM5502_REG_MANUAL_SW1_VBUSIN_MASK,
258a75fed2eSChanwoo Choi 					 vbus_sw);
259a75fed2eSChanwoo Choi 		if (ret < 0) {
260a75fed2eSChanwoo Choi 			dev_err(info->dev,
261a75fed2eSChanwoo Choi 				"cannot update VBUSIN switch\n");
262a75fed2eSChanwoo Choi 			return ret;
263a75fed2eSChanwoo Choi 		}
264a75fed2eSChanwoo Choi 		break;
265a75fed2eSChanwoo Choi 	default:
266a75fed2eSChanwoo Choi 		dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw);
267a75fed2eSChanwoo Choi 		return -EINVAL;
268a75fed2eSChanwoo Choi 	};
269a75fed2eSChanwoo Choi 
270a75fed2eSChanwoo Choi 	return 0;
271a75fed2eSChanwoo Choi }
272a75fed2eSChanwoo Choi 
273914b881fSChanwoo Choi /* Return cable type of attached or detached accessories */
274914b881fSChanwoo Choi static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info)
275914b881fSChanwoo Choi {
276914b881fSChanwoo Choi 	unsigned int cable_type = -1, adc, dev_type1;
277914b881fSChanwoo Choi 	int ret;
278914b881fSChanwoo Choi 
279914b881fSChanwoo Choi 	/* Read ADC value according to external cable or button */
280914b881fSChanwoo Choi 	ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc);
281914b881fSChanwoo Choi 	if (ret) {
282914b881fSChanwoo Choi 		dev_err(info->dev, "failed to read ADC register\n");
283914b881fSChanwoo Choi 		return ret;
284914b881fSChanwoo Choi 	}
285914b881fSChanwoo Choi 
286914b881fSChanwoo Choi 	/*
287914b881fSChanwoo Choi 	 * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't
288914b881fSChanwoo Choi 	 * connected with to MUIC device.
289914b881fSChanwoo Choi 	 */
2900ccc7955SChanwoo Choi 	cable_type = adc & SM5502_REG_ADC_MASK;
291914b881fSChanwoo Choi 	if (cable_type == SM5502_MUIC_ADC_GROUND)
292914b881fSChanwoo Choi 		return SM5502_MUIC_ADC_GROUND;
293914b881fSChanwoo Choi 
294914b881fSChanwoo Choi 	switch (cable_type) {
295914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_GROUND:
296914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_SEND_END_BUTTON:
297914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S1_BUTTON:
298914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S2_BUTTON:
299914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S3_BUTTON:
300914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S4_BUTTON:
301914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S5_BUTTON:
302914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S6_BUTTON:
303914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S7_BUTTON:
304914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S8_BUTTON:
305914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S9_BUTTON:
306914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S10_BUTTON:
307914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S11_BUTTON:
308914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_REMOTE_S12_BUTTON:
309914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_RESERVED_ACC_1:
310914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_RESERVED_ACC_2:
311914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_RESERVED_ACC_3:
312914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_RESERVED_ACC_4:
313914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_RESERVED_ACC_5:
314914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_AUDIO_TYPE2:
315914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_PHONE_POWERED_DEV:
316914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_TTY_CONVERTER:
317914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_UART_CABLE:
318914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_TYPE1_CHARGER:
319914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
320914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
321914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE:
322914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_TYPE2_CHARGER:
323914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
324914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
325914b881fSChanwoo Choi 		break;
326914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_AUDIO_TYPE1:
327914b881fSChanwoo Choi 		/*
328914b881fSChanwoo Choi 		 * Check whether cable type is
329914b881fSChanwoo Choi 		 * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE
330914b881fSChanwoo Choi 		 * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END
331914b881fSChanwoo Choi 		 * by using Button event.
332914b881fSChanwoo Choi 		 */
333914b881fSChanwoo Choi 		break;
334914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_OPEN:
335914b881fSChanwoo Choi 		ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
336914b881fSChanwoo Choi 				  &dev_type1);
337914b881fSChanwoo Choi 		if (ret) {
338914b881fSChanwoo Choi 			dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
339914b881fSChanwoo Choi 			return ret;
340914b881fSChanwoo Choi 		}
341914b881fSChanwoo Choi 
342914b881fSChanwoo Choi 		switch (dev_type1) {
343914b881fSChanwoo Choi 		case SM5502_REG_DEV_TYPE1_USB_SDP_MASK:
344914b881fSChanwoo Choi 			cable_type = SM5502_MUIC_ADC_OPEN_USB;
345914b881fSChanwoo Choi 			break;
346914b881fSChanwoo Choi 		case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK:
347914b881fSChanwoo Choi 			cable_type = SM5502_MUIC_ADC_OPEN_TA;
348914b881fSChanwoo Choi 			break;
349914b881fSChanwoo Choi 		case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
350914b881fSChanwoo Choi 			cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG;
351914b881fSChanwoo Choi 			break;
352914b881fSChanwoo Choi 		default:
353914b881fSChanwoo Choi 			dev_dbg(info->dev,
35434825e51SChanwoo Choi 				"cannot identify the cable type: adc(0x%x)\n",
35534825e51SChanwoo Choi 				adc);
356914b881fSChanwoo Choi 			return -EINVAL;
357914b881fSChanwoo Choi 		};
358914b881fSChanwoo Choi 		break;
359914b881fSChanwoo Choi 	default:
360914b881fSChanwoo Choi 		dev_err(info->dev,
361914b881fSChanwoo Choi 			"failed to identify the cable type: adc(0x%x)\n", adc);
362914b881fSChanwoo Choi 		return -EINVAL;
363914b881fSChanwoo Choi 	};
364914b881fSChanwoo Choi 
365914b881fSChanwoo Choi 	return cable_type;
366914b881fSChanwoo Choi }
367914b881fSChanwoo Choi 
368914b881fSChanwoo Choi static int sm5502_muic_cable_handler(struct sm5502_muic_info *info,
369914b881fSChanwoo Choi 				     bool attached)
370914b881fSChanwoo Choi {
371914b881fSChanwoo Choi 	static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND;
372914b881fSChanwoo Choi 	unsigned int cable_type = SM5502_MUIC_ADC_GROUND;
373a75fed2eSChanwoo Choi 	unsigned int con_sw = DM_DP_SWITCH_OPEN;
374a75fed2eSChanwoo Choi 	unsigned int vbus_sw = VBUSIN_SWITCH_OPEN;
375*73b6ecdbSChanwoo Choi 	unsigned int id;
376a75fed2eSChanwoo Choi 	int ret;
377914b881fSChanwoo Choi 
378914b881fSChanwoo Choi 	/* Get the type of attached or detached cable */
379914b881fSChanwoo Choi 	if (attached)
380914b881fSChanwoo Choi 		cable_type = sm5502_muic_get_cable_type(info);
381fbae30d8SChanwoo Choi 	else
382914b881fSChanwoo Choi 		cable_type = prev_cable_type;
383914b881fSChanwoo Choi 	prev_cable_type = cable_type;
384914b881fSChanwoo Choi 
385914b881fSChanwoo Choi 	switch (cable_type) {
386914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_OPEN_USB:
3872a9de9c0SChanwoo Choi 		id	= EXTCON_USB;
388a75fed2eSChanwoo Choi 		con_sw	= DM_DP_SWITCH_USB;
389a75fed2eSChanwoo Choi 		vbus_sw	= VBUSIN_SWITCH_VBUSOUT_WITH_USB;
390914b881fSChanwoo Choi 		break;
391914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_OPEN_TA:
3922a9de9c0SChanwoo Choi 		id	= EXTCON_TA;
393a75fed2eSChanwoo Choi 		con_sw	= DM_DP_SWITCH_OPEN;
394a75fed2eSChanwoo Choi 		vbus_sw	= VBUSIN_SWITCH_VBUSOUT;
395914b881fSChanwoo Choi 		break;
396914b881fSChanwoo Choi 	case SM5502_MUIC_ADC_OPEN_USB_OTG:
3972a9de9c0SChanwoo Choi 		id	= EXTCON_USB_HOST;
398a75fed2eSChanwoo Choi 		con_sw	= DM_DP_SWITCH_USB;
399a75fed2eSChanwoo Choi 		vbus_sw	= VBUSIN_SWITCH_OPEN;
400e1954452SChanwoo Choi 		break;
401914b881fSChanwoo Choi 	default:
402914b881fSChanwoo Choi 		dev_dbg(info->dev,
403914b881fSChanwoo Choi 			"cannot handle this cable_type (0x%x)\n", cable_type);
404914b881fSChanwoo Choi 		return 0;
405914b881fSChanwoo Choi 	};
406914b881fSChanwoo Choi 
407a75fed2eSChanwoo Choi 	/* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */
408a75fed2eSChanwoo Choi 	ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached);
409a75fed2eSChanwoo Choi 	if (ret < 0)
410a75fed2eSChanwoo Choi 		return ret;
411a75fed2eSChanwoo Choi 
412a75fed2eSChanwoo Choi 	/* Change the state of external accessory */
4132a9de9c0SChanwoo Choi 	extcon_set_cable_state_(info->edev, id, attached);
414914b881fSChanwoo Choi 
415914b881fSChanwoo Choi 	return 0;
416914b881fSChanwoo Choi }
417914b881fSChanwoo Choi 
418914b881fSChanwoo Choi static void sm5502_muic_irq_work(struct work_struct *work)
419914b881fSChanwoo Choi {
420914b881fSChanwoo Choi 	struct sm5502_muic_info *info = container_of(work,
421914b881fSChanwoo Choi 			struct sm5502_muic_info, irq_work);
422914b881fSChanwoo Choi 	int ret = 0;
423914b881fSChanwoo Choi 
424914b881fSChanwoo Choi 	if (!info->edev)
425914b881fSChanwoo Choi 		return;
426914b881fSChanwoo Choi 
427914b881fSChanwoo Choi 	mutex_lock(&info->mutex);
428914b881fSChanwoo Choi 
429914b881fSChanwoo Choi 	/* Detect attached or detached cables */
430914b881fSChanwoo Choi 	if (info->irq_attach) {
431914b881fSChanwoo Choi 		ret = sm5502_muic_cable_handler(info, true);
432914b881fSChanwoo Choi 		info->irq_attach = false;
433914b881fSChanwoo Choi 	}
434914b881fSChanwoo Choi 	if (info->irq_detach) {
435914b881fSChanwoo Choi 		ret = sm5502_muic_cable_handler(info, false);
436914b881fSChanwoo Choi 		info->irq_detach = false;
437914b881fSChanwoo Choi 	}
438914b881fSChanwoo Choi 
439914b881fSChanwoo Choi 	if (ret < 0)
440914b881fSChanwoo Choi 		dev_err(info->dev, "failed to handle MUIC interrupt\n");
441914b881fSChanwoo Choi 
442914b881fSChanwoo Choi 	mutex_unlock(&info->mutex);
443914b881fSChanwoo Choi }
444914b881fSChanwoo Choi 
445914b881fSChanwoo Choi /*
446914b881fSChanwoo Choi  * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0.
447914b881fSChanwoo Choi  * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
448914b881fSChanwoo Choi  */
449914b881fSChanwoo Choi static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type)
450914b881fSChanwoo Choi {
451914b881fSChanwoo Choi 	switch (irq_type) {
452914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_ATTACH:
453914b881fSChanwoo Choi 		info->irq_attach = true;
454914b881fSChanwoo Choi 		break;
455914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_DETACH:
456914b881fSChanwoo Choi 		info->irq_detach = true;
457914b881fSChanwoo Choi 		break;
458914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_KP:
459914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_LKP:
460914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_LKR:
461914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_OVP_EVENT:
462914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_OCP_EVENT:
463914b881fSChanwoo Choi 	case SM5502_IRQ_INT1_OVP_OCP_DIS:
464914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_VBUS_DET:
465914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_REV_ACCE:
466914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_ADC_CHG:
467914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_STUCK_KEY:
468914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_STUCK_KEY_RCV:
469914b881fSChanwoo Choi 	case SM5502_IRQ_INT2_MHL:
470914b881fSChanwoo Choi 	default:
471914b881fSChanwoo Choi 		break;
472914b881fSChanwoo Choi 	}
473914b881fSChanwoo Choi 
474914b881fSChanwoo Choi 	return 0;
475914b881fSChanwoo Choi }
476914b881fSChanwoo Choi 
477914b881fSChanwoo Choi static irqreturn_t sm5502_muic_irq_handler(int irq, void *data)
478914b881fSChanwoo Choi {
479914b881fSChanwoo Choi 	struct sm5502_muic_info *info = data;
480914b881fSChanwoo Choi 	int i, irq_type = -1, ret;
481914b881fSChanwoo Choi 
482914b881fSChanwoo Choi 	for (i = 0; i < info->num_muic_irqs; i++)
483914b881fSChanwoo Choi 		if (irq == info->muic_irqs[i].virq)
484914b881fSChanwoo Choi 			irq_type = info->muic_irqs[i].irq;
485914b881fSChanwoo Choi 
486914b881fSChanwoo Choi 	ret = sm5502_parse_irq(info, irq_type);
487914b881fSChanwoo Choi 	if (ret < 0) {
488914b881fSChanwoo Choi 		dev_warn(info->dev, "cannot handle is interrupt:%d\n",
489914b881fSChanwoo Choi 				    irq_type);
490914b881fSChanwoo Choi 		return IRQ_HANDLED;
491914b881fSChanwoo Choi 	}
492914b881fSChanwoo Choi 	schedule_work(&info->irq_work);
493914b881fSChanwoo Choi 
494914b881fSChanwoo Choi 	return IRQ_HANDLED;
495914b881fSChanwoo Choi }
496914b881fSChanwoo Choi 
497e1954452SChanwoo Choi static void sm5502_muic_detect_cable_wq(struct work_struct *work)
498e1954452SChanwoo Choi {
499e1954452SChanwoo Choi 	struct sm5502_muic_info *info = container_of(to_delayed_work(work),
500e1954452SChanwoo Choi 				struct sm5502_muic_info, wq_detcable);
501e1954452SChanwoo Choi 	int ret;
502e1954452SChanwoo Choi 
503e1954452SChanwoo Choi 	/* Notify the state of connector cable or not  */
504e1954452SChanwoo Choi 	ret = sm5502_muic_cable_handler(info, true);
505e1954452SChanwoo Choi 	if (ret < 0)
506e1954452SChanwoo Choi 		dev_warn(info->dev, "failed to detect cable state\n");
507e1954452SChanwoo Choi }
508e1954452SChanwoo Choi 
509914b881fSChanwoo Choi static void sm5502_init_dev_type(struct sm5502_muic_info *info)
510914b881fSChanwoo Choi {
511914b881fSChanwoo Choi 	unsigned int reg_data, vendor_id, version_id;
512914b881fSChanwoo Choi 	int i, ret;
513914b881fSChanwoo Choi 
514914b881fSChanwoo Choi 	/* To test I2C, Print version_id and vendor_id of SM5502 */
515914b881fSChanwoo Choi 	ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, &reg_data);
516914b881fSChanwoo Choi 	if (ret) {
517914b881fSChanwoo Choi 		dev_err(info->dev,
518914b881fSChanwoo Choi 			"failed to read DEVICE_ID register: %d\n", ret);
519914b881fSChanwoo Choi 		return;
520914b881fSChanwoo Choi 	}
521914b881fSChanwoo Choi 
522914b881fSChanwoo Choi 	vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >>
523914b881fSChanwoo Choi 				SM5502_REG_DEVICE_ID_VENDOR_SHIFT);
524914b881fSChanwoo Choi 	version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >>
525914b881fSChanwoo Choi 				SM5502_REG_DEVICE_ID_VERSION_SHIFT);
526914b881fSChanwoo Choi 
527914b881fSChanwoo Choi 	dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
528914b881fSChanwoo Choi 			    version_id, vendor_id);
529914b881fSChanwoo Choi 
530914b881fSChanwoo Choi 	/* Initiazle the register of SM5502 device to bring-up */
531914b881fSChanwoo Choi 	for (i = 0; i < info->num_reg_data; i++) {
532914b881fSChanwoo Choi 		unsigned int val = 0;
533914b881fSChanwoo Choi 
534914b881fSChanwoo Choi 		if (!info->reg_data[i].invert)
535914b881fSChanwoo Choi 			val |= ~info->reg_data[i].val;
536914b881fSChanwoo Choi 		else
537914b881fSChanwoo Choi 			val = info->reg_data[i].val;
538914b881fSChanwoo Choi 		regmap_write(info->regmap, info->reg_data[i].reg, val);
539914b881fSChanwoo Choi 	}
540914b881fSChanwoo Choi }
541914b881fSChanwoo Choi 
542914b881fSChanwoo Choi static int sm5022_muic_i2c_probe(struct i2c_client *i2c,
543914b881fSChanwoo Choi 				 const struct i2c_device_id *id)
544914b881fSChanwoo Choi {
545914b881fSChanwoo Choi 	struct device_node *np = i2c->dev.of_node;
546914b881fSChanwoo Choi 	struct sm5502_muic_info *info;
547914b881fSChanwoo Choi 	int i, ret, irq_flags;
548914b881fSChanwoo Choi 
549914b881fSChanwoo Choi 	if (!np)
550914b881fSChanwoo Choi 		return -EINVAL;
551914b881fSChanwoo Choi 
552914b881fSChanwoo Choi 	info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
553914b881fSChanwoo Choi 	if (!info)
554914b881fSChanwoo Choi 		return -ENOMEM;
555914b881fSChanwoo Choi 	i2c_set_clientdata(i2c, info);
556914b881fSChanwoo Choi 
557914b881fSChanwoo Choi 	info->dev = &i2c->dev;
558914b881fSChanwoo Choi 	info->i2c = i2c;
559914b881fSChanwoo Choi 	info->irq = i2c->irq;
560914b881fSChanwoo Choi 	info->muic_irqs = sm5502_muic_irqs;
561914b881fSChanwoo Choi 	info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs);
562914b881fSChanwoo Choi 	info->reg_data = sm5502_reg_data;
563914b881fSChanwoo Choi 	info->num_reg_data = ARRAY_SIZE(sm5502_reg_data);
564914b881fSChanwoo Choi 
565914b881fSChanwoo Choi 	mutex_init(&info->mutex);
566914b881fSChanwoo Choi 
567914b881fSChanwoo Choi 	INIT_WORK(&info->irq_work, sm5502_muic_irq_work);
568914b881fSChanwoo Choi 
569914b881fSChanwoo Choi 	info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config);
570914b881fSChanwoo Choi 	if (IS_ERR(info->regmap)) {
571914b881fSChanwoo Choi 		ret = PTR_ERR(info->regmap);
572914b881fSChanwoo Choi 		dev_err(info->dev, "failed to allocate register map: %d\n",
573914b881fSChanwoo Choi 				   ret);
574914b881fSChanwoo Choi 		return ret;
575914b881fSChanwoo Choi 	}
576914b881fSChanwoo Choi 
577914b881fSChanwoo Choi 	/* Support irq domain for SM5502 MUIC device */
578914b881fSChanwoo Choi 	irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
579914b881fSChanwoo Choi 	ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0,
580914b881fSChanwoo Choi 				  &sm5502_muic_irq_chip, &info->irq_data);
581914b881fSChanwoo Choi 	if (ret != 0) {
582914b881fSChanwoo Choi 		dev_err(info->dev, "failed to request IRQ %d: %d\n",
583914b881fSChanwoo Choi 				    info->irq, ret);
584914b881fSChanwoo Choi 		return ret;
585914b881fSChanwoo Choi 	}
586914b881fSChanwoo Choi 
587914b881fSChanwoo Choi 	for (i = 0; i < info->num_muic_irqs; i++) {
588914b881fSChanwoo Choi 		struct muic_irq *muic_irq = &info->muic_irqs[i];
589914b881fSChanwoo Choi 		unsigned int virq = 0;
590914b881fSChanwoo Choi 
591914b881fSChanwoo Choi 		virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
592914b881fSChanwoo Choi 		if (virq <= 0)
593914b881fSChanwoo Choi 			return -EINVAL;
594914b881fSChanwoo Choi 		muic_irq->virq = virq;
595914b881fSChanwoo Choi 
596914b881fSChanwoo Choi 		ret = devm_request_threaded_irq(info->dev, virq, NULL,
597914b881fSChanwoo Choi 						sm5502_muic_irq_handler,
598914b881fSChanwoo Choi 						IRQF_NO_SUSPEND,
599914b881fSChanwoo Choi 						muic_irq->name, info);
600914b881fSChanwoo Choi 		if (ret) {
601fbae30d8SChanwoo Choi 			dev_err(info->dev,
602fbae30d8SChanwoo Choi 				"failed: irq request (IRQ: %d, error :%d)\n",
603fbae30d8SChanwoo Choi 				muic_irq->irq, ret);
604914b881fSChanwoo Choi 			return ret;
605914b881fSChanwoo Choi 		}
606914b881fSChanwoo Choi 	}
607914b881fSChanwoo Choi 
608914b881fSChanwoo Choi 	/* Allocate extcon device */
609914b881fSChanwoo Choi 	info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable);
610914b881fSChanwoo Choi 	if (IS_ERR(info->edev)) {
611914b881fSChanwoo Choi 		dev_err(info->dev, "failed to allocate memory for extcon\n");
612914b881fSChanwoo Choi 		return -ENOMEM;
613914b881fSChanwoo Choi 	}
614914b881fSChanwoo Choi 
615914b881fSChanwoo Choi 	/* Register extcon device */
616914b881fSChanwoo Choi 	ret = devm_extcon_dev_register(info->dev, info->edev);
617914b881fSChanwoo Choi 	if (ret) {
618914b881fSChanwoo Choi 		dev_err(info->dev, "failed to register extcon device\n");
619914b881fSChanwoo Choi 		return ret;
620914b881fSChanwoo Choi 	}
621914b881fSChanwoo Choi 
622e1954452SChanwoo Choi 	/*
623e1954452SChanwoo Choi 	 * Detect accessory after completing the initialization of platform
624e1954452SChanwoo Choi 	 *
625e1954452SChanwoo Choi 	 * - Use delayed workqueue to detect cable state and then
626e1954452SChanwoo Choi 	 * notify cable state to notifiee/platform through uevent.
627e1954452SChanwoo Choi 	 * After completing the booting of platform, the extcon provider
628e1954452SChanwoo Choi 	 * driver should notify cable state to upper layer.
629e1954452SChanwoo Choi 	 */
630e1954452SChanwoo Choi 	INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
631e1954452SChanwoo Choi 	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
632e1954452SChanwoo Choi 			msecs_to_jiffies(DELAY_MS_DEFAULT));
633e1954452SChanwoo Choi 
634914b881fSChanwoo Choi 	/* Initialize SM5502 device and print vendor id and version id */
635914b881fSChanwoo Choi 	sm5502_init_dev_type(info);
636914b881fSChanwoo Choi 
637914b881fSChanwoo Choi 	return 0;
638914b881fSChanwoo Choi }
639914b881fSChanwoo Choi 
640914b881fSChanwoo Choi static int sm5502_muic_i2c_remove(struct i2c_client *i2c)
641914b881fSChanwoo Choi {
642914b881fSChanwoo Choi 	struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
643914b881fSChanwoo Choi 
644914b881fSChanwoo Choi 	regmap_del_irq_chip(info->irq, info->irq_data);
645914b881fSChanwoo Choi 
646914b881fSChanwoo Choi 	return 0;
647914b881fSChanwoo Choi }
648914b881fSChanwoo Choi 
64934825e51SChanwoo Choi static const struct of_device_id sm5502_dt_match[] = {
650914b881fSChanwoo Choi 	{ .compatible = "siliconmitus,sm5502-muic" },
651914b881fSChanwoo Choi 	{ },
652914b881fSChanwoo Choi };
653914b881fSChanwoo Choi 
654914b881fSChanwoo Choi #ifdef CONFIG_PM_SLEEP
655914b881fSChanwoo Choi static int sm5502_muic_suspend(struct device *dev)
656914b881fSChanwoo Choi {
657914b881fSChanwoo Choi 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
658914b881fSChanwoo Choi 	struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
659914b881fSChanwoo Choi 
660914b881fSChanwoo Choi 	enable_irq_wake(info->irq);
661914b881fSChanwoo Choi 
662914b881fSChanwoo Choi 	return 0;
663914b881fSChanwoo Choi }
664914b881fSChanwoo Choi 
665914b881fSChanwoo Choi static int sm5502_muic_resume(struct device *dev)
666914b881fSChanwoo Choi {
667914b881fSChanwoo Choi 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
668914b881fSChanwoo Choi 	struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
669914b881fSChanwoo Choi 
670914b881fSChanwoo Choi 	disable_irq_wake(info->irq);
671914b881fSChanwoo Choi 
672914b881fSChanwoo Choi 	return 0;
673914b881fSChanwoo Choi }
674914b881fSChanwoo Choi #endif
675914b881fSChanwoo Choi 
676914b881fSChanwoo Choi static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops,
677914b881fSChanwoo Choi 			 sm5502_muic_suspend, sm5502_muic_resume);
678914b881fSChanwoo Choi 
679914b881fSChanwoo Choi static const struct i2c_device_id sm5502_i2c_id[] = {
680914b881fSChanwoo Choi 	{ "sm5502", TYPE_SM5502 },
681914b881fSChanwoo Choi 	{ }
682914b881fSChanwoo Choi };
683914b881fSChanwoo Choi MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id);
684914b881fSChanwoo Choi 
685914b881fSChanwoo Choi static struct i2c_driver sm5502_muic_i2c_driver = {
686914b881fSChanwoo Choi 	.driver		= {
687914b881fSChanwoo Choi 		.name	= "sm5502",
688914b881fSChanwoo Choi 		.owner	= THIS_MODULE,
689914b881fSChanwoo Choi 		.pm	= &sm5502_muic_pm_ops,
690914b881fSChanwoo Choi 		.of_match_table = sm5502_dt_match,
691914b881fSChanwoo Choi 	},
692914b881fSChanwoo Choi 	.probe	= sm5022_muic_i2c_probe,
693914b881fSChanwoo Choi 	.remove	= sm5502_muic_i2c_remove,
694914b881fSChanwoo Choi 	.id_table = sm5502_i2c_id,
695914b881fSChanwoo Choi };
696914b881fSChanwoo Choi 
697914b881fSChanwoo Choi static int __init sm5502_muic_i2c_init(void)
698914b881fSChanwoo Choi {
699914b881fSChanwoo Choi 	return i2c_add_driver(&sm5502_muic_i2c_driver);
700914b881fSChanwoo Choi }
701914b881fSChanwoo Choi subsys_initcall(sm5502_muic_i2c_init);
702914b881fSChanwoo Choi 
703914b881fSChanwoo Choi MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver");
704914b881fSChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
705914b881fSChanwoo Choi MODULE_LICENSE("GPL");
706