12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2914b881fSChanwoo Choi /*
3914b881fSChanwoo Choi * extcon-sm5502.c - Silicon Mitus SM5502 extcon drvier to support USB switches
4914b881fSChanwoo Choi *
5914b881fSChanwoo Choi * Copyright (c) 2014 Samsung Electronics Co., Ltd
6914b881fSChanwoo Choi * Author: Chanwoo Choi <cw00.choi@samsung.com>
7914b881fSChanwoo Choi */
8914b881fSChanwoo Choi
9914b881fSChanwoo Choi #include <linux/err.h>
10914b881fSChanwoo Choi #include <linux/i2c.h>
11914b881fSChanwoo Choi #include <linux/interrupt.h>
12914b881fSChanwoo Choi #include <linux/irqdomain.h>
13914b881fSChanwoo Choi #include <linux/kernel.h>
14914b881fSChanwoo Choi #include <linux/module.h>
15914b881fSChanwoo Choi #include <linux/platform_device.h>
16914b881fSChanwoo Choi #include <linux/regmap.h>
17914b881fSChanwoo Choi #include <linux/slab.h>
18176aa360SChanwoo Choi #include <linux/extcon-provider.h>
19ca2a07e4SChanwoo Choi
20ca2a07e4SChanwoo Choi #include "extcon-sm5502.h"
21914b881fSChanwoo Choi
22e1954452SChanwoo Choi #define DELAY_MS_DEFAULT 17000 /* unit: millisecond */
23e1954452SChanwoo Choi
24914b881fSChanwoo Choi struct muic_irq {
25914b881fSChanwoo Choi unsigned int irq;
26914b881fSChanwoo Choi const char *name;
27914b881fSChanwoo Choi unsigned int virq;
28914b881fSChanwoo Choi };
29914b881fSChanwoo Choi
30914b881fSChanwoo Choi struct reg_data {
31914b881fSChanwoo Choi u8 reg;
32914b881fSChanwoo Choi unsigned int val;
33914b881fSChanwoo Choi bool invert;
34914b881fSChanwoo Choi };
35914b881fSChanwoo Choi
36914b881fSChanwoo Choi struct sm5502_muic_info {
37914b881fSChanwoo Choi struct device *dev;
38914b881fSChanwoo Choi struct extcon_dev *edev;
39914b881fSChanwoo Choi
40914b881fSChanwoo Choi struct i2c_client *i2c;
41914b881fSChanwoo Choi struct regmap *regmap;
42914b881fSChanwoo Choi
43f33c056dSStephan Gerhold const struct sm5502_type *type;
44914b881fSChanwoo Choi struct regmap_irq_chip_data *irq_data;
45914b881fSChanwoo Choi int irq;
46914b881fSChanwoo Choi bool irq_attach;
47914b881fSChanwoo Choi bool irq_detach;
48914b881fSChanwoo Choi struct work_struct irq_work;
49914b881fSChanwoo Choi
50914b881fSChanwoo Choi struct mutex mutex;
51e1954452SChanwoo Choi
52e1954452SChanwoo Choi /*
53e1954452SChanwoo Choi * Use delayed workqueue to detect cable state and then
54e1954452SChanwoo Choi * notify cable state to notifiee/platform through uevent.
55e1954452SChanwoo Choi * After completing the booting of platform, the extcon provider
56e1954452SChanwoo Choi * driver should notify cable state to upper layer.
57e1954452SChanwoo Choi */
58e1954452SChanwoo Choi struct delayed_work wq_detcable;
59914b881fSChanwoo Choi };
60914b881fSChanwoo Choi
61f33c056dSStephan Gerhold struct sm5502_type {
62f33c056dSStephan Gerhold struct muic_irq *muic_irqs;
63f33c056dSStephan Gerhold unsigned int num_muic_irqs;
64f33c056dSStephan Gerhold const struct regmap_irq_chip *irq_chip;
65f33c056dSStephan Gerhold
66f33c056dSStephan Gerhold struct reg_data *reg_data;
67f33c056dSStephan Gerhold unsigned int num_reg_data;
68f33c056dSStephan Gerhold
69d97c0ff5SStephan Gerhold unsigned int otg_dev_type1;
70f33c056dSStephan Gerhold int (*parse_irq)(struct sm5502_muic_info *info, int irq_type);
71f33c056dSStephan Gerhold };
72f33c056dSStephan Gerhold
73914b881fSChanwoo Choi /* Default value of SM5502 register to bring up MUIC device. */
74914b881fSChanwoo Choi static struct reg_data sm5502_reg_data[] = {
75914b881fSChanwoo Choi {
7669426350SStephan Gerhold .reg = SM5502_REG_RESET,
7769426350SStephan Gerhold .val = SM5502_REG_RESET_MASK,
7869426350SStephan Gerhold .invert = true,
7969426350SStephan Gerhold }, {
80914b881fSChanwoo Choi .reg = SM5502_REG_CONTROL,
81914b881fSChanwoo Choi .val = SM5502_REG_CONTROL_MASK_INT_MASK,
82914b881fSChanwoo Choi .invert = false,
83914b881fSChanwoo Choi }, {
84914b881fSChanwoo Choi .reg = SM5502_REG_INTMASK1,
85914b881fSChanwoo Choi .val = SM5502_REG_INTM1_KP_MASK
86914b881fSChanwoo Choi | SM5502_REG_INTM1_LKP_MASK
87914b881fSChanwoo Choi | SM5502_REG_INTM1_LKR_MASK,
88914b881fSChanwoo Choi .invert = true,
89914b881fSChanwoo Choi }, {
90914b881fSChanwoo Choi .reg = SM5502_REG_INTMASK2,
91914b881fSChanwoo Choi .val = SM5502_REG_INTM2_VBUS_DET_MASK
92914b881fSChanwoo Choi | SM5502_REG_INTM2_REV_ACCE_MASK
93914b881fSChanwoo Choi | SM5502_REG_INTM2_ADC_CHG_MASK
94914b881fSChanwoo Choi | SM5502_REG_INTM2_STUCK_KEY_MASK
95914b881fSChanwoo Choi | SM5502_REG_INTM2_STUCK_KEY_RCV_MASK
96914b881fSChanwoo Choi | SM5502_REG_INTM2_MHL_MASK,
97914b881fSChanwoo Choi .invert = true,
98914b881fSChanwoo Choi },
99914b881fSChanwoo Choi };
100914b881fSChanwoo Choi
101d97c0ff5SStephan Gerhold /* Default value of SM5504 register to bring up MUIC device. */
102d97c0ff5SStephan Gerhold static struct reg_data sm5504_reg_data[] = {
103d97c0ff5SStephan Gerhold {
104d97c0ff5SStephan Gerhold .reg = SM5502_REG_RESET,
105d97c0ff5SStephan Gerhold .val = SM5502_REG_RESET_MASK,
106d97c0ff5SStephan Gerhold .invert = true,
107d97c0ff5SStephan Gerhold }, {
108d97c0ff5SStephan Gerhold .reg = SM5502_REG_INTMASK1,
109d97c0ff5SStephan Gerhold .val = SM5504_REG_INTM1_ATTACH_MASK
110d97c0ff5SStephan Gerhold | SM5504_REG_INTM1_DETACH_MASK,
111d97c0ff5SStephan Gerhold .invert = false,
112d97c0ff5SStephan Gerhold }, {
113d97c0ff5SStephan Gerhold .reg = SM5502_REG_INTMASK2,
114d97c0ff5SStephan Gerhold .val = SM5504_REG_INTM2_RID_CHG_MASK
115d97c0ff5SStephan Gerhold | SM5504_REG_INTM2_UVLO_MASK
116d97c0ff5SStephan Gerhold | SM5504_REG_INTM2_POR_MASK,
117d97c0ff5SStephan Gerhold .invert = true,
118d97c0ff5SStephan Gerhold }, {
119d97c0ff5SStephan Gerhold .reg = SM5502_REG_CONTROL,
120d97c0ff5SStephan Gerhold .val = SM5502_REG_CONTROL_MANUAL_SW_MASK
121d97c0ff5SStephan Gerhold | SM5504_REG_CONTROL_CHGTYP_MASK
122d97c0ff5SStephan Gerhold | SM5504_REG_CONTROL_USBCHDEN_MASK
123d97c0ff5SStephan Gerhold | SM5504_REG_CONTROL_ADC_EN_MASK,
124d97c0ff5SStephan Gerhold .invert = true,
125d97c0ff5SStephan Gerhold },
126d97c0ff5SStephan Gerhold };
127d97c0ff5SStephan Gerhold
128914b881fSChanwoo Choi /* List of detectable cables */
12973b6ecdbSChanwoo Choi static const unsigned int sm5502_extcon_cable[] = {
1302a9de9c0SChanwoo Choi EXTCON_USB,
1312a9de9c0SChanwoo Choi EXTCON_USB_HOST,
1328b45b6a0SChanwoo Choi EXTCON_CHG_USB_SDP,
13311eecf91SChanwoo Choi EXTCON_CHG_USB_DCP,
1342a9de9c0SChanwoo Choi EXTCON_NONE,
135914b881fSChanwoo Choi };
136914b881fSChanwoo Choi
137914b881fSChanwoo Choi /* Define supported accessory type */
138914b881fSChanwoo Choi enum sm5502_muic_acc_type {
139914b881fSChanwoo Choi SM5502_MUIC_ADC_GROUND = 0x0,
140914b881fSChanwoo Choi SM5502_MUIC_ADC_SEND_END_BUTTON,
141914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S1_BUTTON,
142914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S2_BUTTON,
143914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S3_BUTTON,
144914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S4_BUTTON,
145914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S5_BUTTON,
146914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S6_BUTTON,
147914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S7_BUTTON,
148914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S8_BUTTON,
149914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S9_BUTTON,
150914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S10_BUTTON,
151914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S11_BUTTON,
152914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S12_BUTTON,
153914b881fSChanwoo Choi SM5502_MUIC_ADC_RESERVED_ACC_1,
154914b881fSChanwoo Choi SM5502_MUIC_ADC_RESERVED_ACC_2,
155914b881fSChanwoo Choi SM5502_MUIC_ADC_RESERVED_ACC_3,
156914b881fSChanwoo Choi SM5502_MUIC_ADC_RESERVED_ACC_4,
157914b881fSChanwoo Choi SM5502_MUIC_ADC_RESERVED_ACC_5,
158914b881fSChanwoo Choi SM5502_MUIC_ADC_AUDIO_TYPE2,
159914b881fSChanwoo Choi SM5502_MUIC_ADC_PHONE_POWERED_DEV,
160914b881fSChanwoo Choi SM5502_MUIC_ADC_TTY_CONVERTER,
161914b881fSChanwoo Choi SM5502_MUIC_ADC_UART_CABLE,
162914b881fSChanwoo Choi SM5502_MUIC_ADC_TYPE1_CHARGER,
163914b881fSChanwoo Choi SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
164914b881fSChanwoo Choi SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
165914b881fSChanwoo Choi SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE,
166914b881fSChanwoo Choi SM5502_MUIC_ADC_TYPE2_CHARGER,
167914b881fSChanwoo Choi SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
168914b881fSChanwoo Choi SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
169914b881fSChanwoo Choi SM5502_MUIC_ADC_AUDIO_TYPE1,
170914b881fSChanwoo Choi SM5502_MUIC_ADC_OPEN = 0x1f,
171914b881fSChanwoo Choi
172af57fa4dSSrikant Ritolia /*
173af57fa4dSSrikant Ritolia * The below accessories have same ADC value (0x1f or 0x1e).
174af57fa4dSSrikant Ritolia * So, Device type1 is used to separate specific accessory.
175af57fa4dSSrikant Ritolia */
176914b881fSChanwoo Choi /* |---------|--ADC| */
177914b881fSChanwoo Choi /* | [7:5]|[4:0]| */
178914b881fSChanwoo Choi SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* | 001|11110| */
179914b881fSChanwoo Choi SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e, /* | 010|11110| */
180914b881fSChanwoo Choi /* |Dev Type1|--ADC| */
181e3f60329SNikita Travkin SM5502_MUIC_ADC_GROUND_USB_OTG = 0x80, /* | 100|00000| */
182914b881fSChanwoo Choi SM5502_MUIC_ADC_OPEN_USB = 0x5f, /* | 010|11111| */
183914b881fSChanwoo Choi SM5502_MUIC_ADC_OPEN_TA = 0xdf, /* | 110|11111| */
184914b881fSChanwoo Choi SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff, /* | 111|11111| */
185914b881fSChanwoo Choi };
186914b881fSChanwoo Choi
187914b881fSChanwoo Choi /* List of supported interrupt for SM5502 */
188914b881fSChanwoo Choi static struct muic_irq sm5502_muic_irqs[] = {
189914b881fSChanwoo Choi { SM5502_IRQ_INT1_ATTACH, "muic-attach" },
190914b881fSChanwoo Choi { SM5502_IRQ_INT1_DETACH, "muic-detach" },
191914b881fSChanwoo Choi { SM5502_IRQ_INT1_KP, "muic-kp" },
192914b881fSChanwoo Choi { SM5502_IRQ_INT1_LKP, "muic-lkp" },
193914b881fSChanwoo Choi { SM5502_IRQ_INT1_LKR, "muic-lkr" },
194914b881fSChanwoo Choi { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" },
195914b881fSChanwoo Choi { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" },
196914b881fSChanwoo Choi { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" },
197914b881fSChanwoo Choi { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" },
198914b881fSChanwoo Choi { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" },
199914b881fSChanwoo Choi { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" },
200914b881fSChanwoo Choi { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" },
201914b881fSChanwoo Choi { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" },
202914b881fSChanwoo Choi { SM5502_IRQ_INT2_MHL, "muic-mhl" },
203914b881fSChanwoo Choi };
204914b881fSChanwoo Choi
205914b881fSChanwoo Choi /* Define interrupt list of SM5502 to register regmap_irq */
206914b881fSChanwoo Choi static const struct regmap_irq sm5502_irqs[] = {
207914b881fSChanwoo Choi /* INT1 interrupts */
208914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, },
209914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, },
210914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, },
211914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, },
212914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, },
213914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, },
214914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, },
215914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, },
216914b881fSChanwoo Choi
217914b881fSChanwoo Choi /* INT2 interrupts */
218914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,},
219914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, },
220914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, },
221914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, },
222914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, },
223914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, },
224914b881fSChanwoo Choi };
225914b881fSChanwoo Choi
226914b881fSChanwoo Choi static const struct regmap_irq_chip sm5502_muic_irq_chip = {
227914b881fSChanwoo Choi .name = "sm5502",
228914b881fSChanwoo Choi .status_base = SM5502_REG_INT1,
229914b881fSChanwoo Choi .mask_base = SM5502_REG_INTMASK1,
230914b881fSChanwoo Choi .num_regs = 2,
231914b881fSChanwoo Choi .irqs = sm5502_irqs,
232914b881fSChanwoo Choi .num_irqs = ARRAY_SIZE(sm5502_irqs),
233914b881fSChanwoo Choi };
234914b881fSChanwoo Choi
235d97c0ff5SStephan Gerhold /* List of supported interrupt for SM5504 */
236d97c0ff5SStephan Gerhold static struct muic_irq sm5504_muic_irqs[] = {
237d97c0ff5SStephan Gerhold { SM5504_IRQ_INT1_ATTACH, "muic-attach" },
238d97c0ff5SStephan Gerhold { SM5504_IRQ_INT1_DETACH, "muic-detach" },
239d97c0ff5SStephan Gerhold { SM5504_IRQ_INT1_CHG_DET, "muic-chg-det" },
240d97c0ff5SStephan Gerhold { SM5504_IRQ_INT1_DCD_OUT, "muic-dcd-out" },
241d97c0ff5SStephan Gerhold { SM5504_IRQ_INT1_OVP_EVENT, "muic-ovp-event" },
242d97c0ff5SStephan Gerhold { SM5504_IRQ_INT1_CONNECT, "muic-connect" },
243d97c0ff5SStephan Gerhold { SM5504_IRQ_INT1_ADC_CHG, "muic-adc-chg" },
244d97c0ff5SStephan Gerhold { SM5504_IRQ_INT2_RID_CHG, "muic-rid-chg" },
245d97c0ff5SStephan Gerhold { SM5504_IRQ_INT2_UVLO, "muic-uvlo" },
246d97c0ff5SStephan Gerhold { SM5504_IRQ_INT2_POR, "muic-por" },
247d97c0ff5SStephan Gerhold { SM5504_IRQ_INT2_OVP_FET, "muic-ovp-fet" },
248d97c0ff5SStephan Gerhold { SM5504_IRQ_INT2_OCP_LATCH, "muic-ocp-latch" },
249d97c0ff5SStephan Gerhold { SM5504_IRQ_INT2_OCP_EVENT, "muic-ocp-event" },
250d97c0ff5SStephan Gerhold { SM5504_IRQ_INT2_OVP_OCP_EVENT, "muic-ovp-ocp-event" },
251d97c0ff5SStephan Gerhold };
252d97c0ff5SStephan Gerhold
253d97c0ff5SStephan Gerhold /* Define interrupt list of SM5504 to register regmap_irq */
254d97c0ff5SStephan Gerhold static const struct regmap_irq sm5504_irqs[] = {
255d97c0ff5SStephan Gerhold /* INT1 interrupts */
256d97c0ff5SStephan Gerhold { .reg_offset = 0, .mask = SM5504_IRQ_INT1_ATTACH_MASK, },
257d97c0ff5SStephan Gerhold { .reg_offset = 0, .mask = SM5504_IRQ_INT1_DETACH_MASK, },
258d97c0ff5SStephan Gerhold { .reg_offset = 0, .mask = SM5504_IRQ_INT1_CHG_DET_MASK, },
259d97c0ff5SStephan Gerhold { .reg_offset = 0, .mask = SM5504_IRQ_INT1_DCD_OUT_MASK, },
260d97c0ff5SStephan Gerhold { .reg_offset = 0, .mask = SM5504_IRQ_INT1_OVP_MASK, },
261d97c0ff5SStephan Gerhold { .reg_offset = 0, .mask = SM5504_IRQ_INT1_CONNECT_MASK, },
262d97c0ff5SStephan Gerhold { .reg_offset = 0, .mask = SM5504_IRQ_INT1_ADC_CHG_MASK, },
263d97c0ff5SStephan Gerhold
264d97c0ff5SStephan Gerhold /* INT2 interrupts */
265d97c0ff5SStephan Gerhold { .reg_offset = 1, .mask = SM5504_IRQ_INT2_RID_CHG_MASK,},
266d97c0ff5SStephan Gerhold { .reg_offset = 1, .mask = SM5504_IRQ_INT2_UVLO_MASK, },
267d97c0ff5SStephan Gerhold { .reg_offset = 1, .mask = SM5504_IRQ_INT2_POR_MASK, },
268d97c0ff5SStephan Gerhold { .reg_offset = 1, .mask = SM5504_IRQ_INT2_OVP_FET_MASK, },
269d97c0ff5SStephan Gerhold { .reg_offset = 1, .mask = SM5504_IRQ_INT2_OCP_LATCH_MASK, },
270d97c0ff5SStephan Gerhold { .reg_offset = 1, .mask = SM5504_IRQ_INT2_OCP_EVENT_MASK, },
271d97c0ff5SStephan Gerhold { .reg_offset = 1, .mask = SM5504_IRQ_INT2_OVP_OCP_EVENT_MASK, },
272d97c0ff5SStephan Gerhold };
273d97c0ff5SStephan Gerhold
274d97c0ff5SStephan Gerhold static const struct regmap_irq_chip sm5504_muic_irq_chip = {
275d97c0ff5SStephan Gerhold .name = "sm5504",
276d97c0ff5SStephan Gerhold .status_base = SM5502_REG_INT1,
277d97c0ff5SStephan Gerhold .mask_base = SM5502_REG_INTMASK1,
278d97c0ff5SStephan Gerhold .num_regs = 2,
279d97c0ff5SStephan Gerhold .irqs = sm5504_irqs,
280d97c0ff5SStephan Gerhold .num_irqs = ARRAY_SIZE(sm5504_irqs),
281d97c0ff5SStephan Gerhold };
282d97c0ff5SStephan Gerhold
283914b881fSChanwoo Choi /* Define regmap configuration of SM5502 for I2C communication */
sm5502_muic_volatile_reg(struct device * dev,unsigned int reg)284914b881fSChanwoo Choi static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg)
285914b881fSChanwoo Choi {
286914b881fSChanwoo Choi switch (reg) {
287914b881fSChanwoo Choi case SM5502_REG_INTMASK1:
288914b881fSChanwoo Choi case SM5502_REG_INTMASK2:
289914b881fSChanwoo Choi return true;
290914b881fSChanwoo Choi default:
291914b881fSChanwoo Choi break;
292914b881fSChanwoo Choi }
293914b881fSChanwoo Choi return false;
294914b881fSChanwoo Choi }
295914b881fSChanwoo Choi
296914b881fSChanwoo Choi static const struct regmap_config sm5502_muic_regmap_config = {
297914b881fSChanwoo Choi .reg_bits = 8,
298914b881fSChanwoo Choi .val_bits = 8,
299914b881fSChanwoo Choi .volatile_reg = sm5502_muic_volatile_reg,
300914b881fSChanwoo Choi .max_register = SM5502_REG_END,
301914b881fSChanwoo Choi };
302914b881fSChanwoo Choi
303a75fed2eSChanwoo Choi /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
sm5502_muic_set_path(struct sm5502_muic_info * info,unsigned int con_sw,unsigned int vbus_sw,bool attached)304a75fed2eSChanwoo Choi static int sm5502_muic_set_path(struct sm5502_muic_info *info,
305a75fed2eSChanwoo Choi unsigned int con_sw, unsigned int vbus_sw,
306a75fed2eSChanwoo Choi bool attached)
307a75fed2eSChanwoo Choi {
308a75fed2eSChanwoo Choi int ret;
309a75fed2eSChanwoo Choi
310a75fed2eSChanwoo Choi if (!attached) {
311a75fed2eSChanwoo Choi con_sw = DM_DP_SWITCH_OPEN;
312a75fed2eSChanwoo Choi vbus_sw = VBUSIN_SWITCH_OPEN;
313a75fed2eSChanwoo Choi }
314a75fed2eSChanwoo Choi
315a75fed2eSChanwoo Choi switch (con_sw) {
316a75fed2eSChanwoo Choi case DM_DP_SWITCH_OPEN:
317a75fed2eSChanwoo Choi case DM_DP_SWITCH_USB:
318a75fed2eSChanwoo Choi case DM_DP_SWITCH_AUDIO:
319a75fed2eSChanwoo Choi case DM_DP_SWITCH_UART:
320a75fed2eSChanwoo Choi ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
321a75fed2eSChanwoo Choi SM5502_REG_MANUAL_SW1_DP_MASK |
322a75fed2eSChanwoo Choi SM5502_REG_MANUAL_SW1_DM_MASK,
323a75fed2eSChanwoo Choi con_sw);
324a75fed2eSChanwoo Choi if (ret < 0) {
325a75fed2eSChanwoo Choi dev_err(info->dev,
326a75fed2eSChanwoo Choi "cannot update DM_CON/DP_CON switch\n");
327a75fed2eSChanwoo Choi return ret;
328a75fed2eSChanwoo Choi }
329a75fed2eSChanwoo Choi break;
330a75fed2eSChanwoo Choi default:
331a75fed2eSChanwoo Choi dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
332a75fed2eSChanwoo Choi con_sw);
333a75fed2eSChanwoo Choi return -EINVAL;
3342ddf50a7SXu Wang }
335a75fed2eSChanwoo Choi
336a75fed2eSChanwoo Choi switch (vbus_sw) {
337a75fed2eSChanwoo Choi case VBUSIN_SWITCH_OPEN:
338a75fed2eSChanwoo Choi case VBUSIN_SWITCH_VBUSOUT:
339a75fed2eSChanwoo Choi case VBUSIN_SWITCH_MIC:
340a75fed2eSChanwoo Choi case VBUSIN_SWITCH_VBUSOUT_WITH_USB:
341a75fed2eSChanwoo Choi ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
342a75fed2eSChanwoo Choi SM5502_REG_MANUAL_SW1_VBUSIN_MASK,
343a75fed2eSChanwoo Choi vbus_sw);
344a75fed2eSChanwoo Choi if (ret < 0) {
345a75fed2eSChanwoo Choi dev_err(info->dev,
346a75fed2eSChanwoo Choi "cannot update VBUSIN switch\n");
347a75fed2eSChanwoo Choi return ret;
348a75fed2eSChanwoo Choi }
349a75fed2eSChanwoo Choi break;
350a75fed2eSChanwoo Choi default:
351a75fed2eSChanwoo Choi dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw);
352a75fed2eSChanwoo Choi return -EINVAL;
3532ddf50a7SXu Wang }
354a75fed2eSChanwoo Choi
355a75fed2eSChanwoo Choi return 0;
356a75fed2eSChanwoo Choi }
357a75fed2eSChanwoo Choi
358914b881fSChanwoo Choi /* Return cable type of attached or detached accessories */
sm5502_muic_get_cable_type(struct sm5502_muic_info * info)359914b881fSChanwoo Choi static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info)
360914b881fSChanwoo Choi {
361ddd1bbbaSColin Ian King unsigned int cable_type, adc, dev_type1;
362914b881fSChanwoo Choi int ret;
363914b881fSChanwoo Choi
364914b881fSChanwoo Choi /* Read ADC value according to external cable or button */
365914b881fSChanwoo Choi ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc);
366914b881fSChanwoo Choi if (ret) {
367914b881fSChanwoo Choi dev_err(info->dev, "failed to read ADC register\n");
368914b881fSChanwoo Choi return ret;
369914b881fSChanwoo Choi }
370914b881fSChanwoo Choi
371914b881fSChanwoo Choi /*
372914b881fSChanwoo Choi * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't
373914b881fSChanwoo Choi * connected with to MUIC device.
374914b881fSChanwoo Choi */
3750ccc7955SChanwoo Choi cable_type = adc & SM5502_REG_ADC_MASK;
376914b881fSChanwoo Choi
377914b881fSChanwoo Choi switch (cable_type) {
378914b881fSChanwoo Choi case SM5502_MUIC_ADC_GROUND:
379e3f60329SNikita Travkin ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
380e3f60329SNikita Travkin &dev_type1);
381e3f60329SNikita Travkin if (ret) {
382e3f60329SNikita Travkin dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
383e3f60329SNikita Travkin return ret;
384e3f60329SNikita Travkin }
385e3f60329SNikita Travkin
386d97c0ff5SStephan Gerhold if (dev_type1 == info->type->otg_dev_type1) {
387e3f60329SNikita Travkin cable_type = SM5502_MUIC_ADC_GROUND_USB_OTG;
388d97c0ff5SStephan Gerhold } else {
389e3f60329SNikita Travkin dev_dbg(info->dev,
390e3f60329SNikita Travkin "cannot identify the cable type: adc(0x%x), dev_type1(0x%x)\n",
391e3f60329SNikita Travkin adc, dev_type1);
392e3f60329SNikita Travkin return -EINVAL;
393e3f60329SNikita Travkin }
394e3f60329SNikita Travkin break;
395914b881fSChanwoo Choi case SM5502_MUIC_ADC_SEND_END_BUTTON:
396914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S1_BUTTON:
397914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S2_BUTTON:
398914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S3_BUTTON:
399914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S4_BUTTON:
400914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S5_BUTTON:
401914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S6_BUTTON:
402914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S7_BUTTON:
403914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S8_BUTTON:
404914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S9_BUTTON:
405914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S10_BUTTON:
406914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S11_BUTTON:
407914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S12_BUTTON:
408914b881fSChanwoo Choi case SM5502_MUIC_ADC_RESERVED_ACC_1:
409914b881fSChanwoo Choi case SM5502_MUIC_ADC_RESERVED_ACC_2:
410914b881fSChanwoo Choi case SM5502_MUIC_ADC_RESERVED_ACC_3:
411914b881fSChanwoo Choi case SM5502_MUIC_ADC_RESERVED_ACC_4:
412914b881fSChanwoo Choi case SM5502_MUIC_ADC_RESERVED_ACC_5:
413914b881fSChanwoo Choi case SM5502_MUIC_ADC_AUDIO_TYPE2:
414914b881fSChanwoo Choi case SM5502_MUIC_ADC_PHONE_POWERED_DEV:
415914b881fSChanwoo Choi case SM5502_MUIC_ADC_TTY_CONVERTER:
416914b881fSChanwoo Choi case SM5502_MUIC_ADC_UART_CABLE:
417914b881fSChanwoo Choi case SM5502_MUIC_ADC_TYPE1_CHARGER:
418914b881fSChanwoo Choi case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
419914b881fSChanwoo Choi case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
420914b881fSChanwoo Choi case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE:
421914b881fSChanwoo Choi case SM5502_MUIC_ADC_TYPE2_CHARGER:
422914b881fSChanwoo Choi case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
423914b881fSChanwoo Choi case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
424914b881fSChanwoo Choi break;
425914b881fSChanwoo Choi case SM5502_MUIC_ADC_AUDIO_TYPE1:
426914b881fSChanwoo Choi /*
427914b881fSChanwoo Choi * Check whether cable type is
428914b881fSChanwoo Choi * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE
429914b881fSChanwoo Choi * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END
430914b881fSChanwoo Choi * by using Button event.
431914b881fSChanwoo Choi */
432914b881fSChanwoo Choi break;
433914b881fSChanwoo Choi case SM5502_MUIC_ADC_OPEN:
434914b881fSChanwoo Choi ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
435914b881fSChanwoo Choi &dev_type1);
436914b881fSChanwoo Choi if (ret) {
437914b881fSChanwoo Choi dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
438914b881fSChanwoo Choi return ret;
439914b881fSChanwoo Choi }
440914b881fSChanwoo Choi
441d97c0ff5SStephan Gerhold if (dev_type1 == info->type->otg_dev_type1) {
442d97c0ff5SStephan Gerhold cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG;
443d97c0ff5SStephan Gerhold break;
444d97c0ff5SStephan Gerhold }
445d97c0ff5SStephan Gerhold
446914b881fSChanwoo Choi switch (dev_type1) {
447914b881fSChanwoo Choi case SM5502_REG_DEV_TYPE1_USB_SDP_MASK:
448914b881fSChanwoo Choi cable_type = SM5502_MUIC_ADC_OPEN_USB;
449914b881fSChanwoo Choi break;
450914b881fSChanwoo Choi case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK:
451914b881fSChanwoo Choi cable_type = SM5502_MUIC_ADC_OPEN_TA;
452914b881fSChanwoo Choi break;
453914b881fSChanwoo Choi default:
454914b881fSChanwoo Choi dev_dbg(info->dev,
45534825e51SChanwoo Choi "cannot identify the cable type: adc(0x%x)\n",
45634825e51SChanwoo Choi adc);
457914b881fSChanwoo Choi return -EINVAL;
4582ddf50a7SXu Wang }
459914b881fSChanwoo Choi break;
460914b881fSChanwoo Choi default:
461914b881fSChanwoo Choi dev_err(info->dev,
462914b881fSChanwoo Choi "failed to identify the cable type: adc(0x%x)\n", adc);
463914b881fSChanwoo Choi return -EINVAL;
4642ddf50a7SXu Wang }
465914b881fSChanwoo Choi
466914b881fSChanwoo Choi return cable_type;
467914b881fSChanwoo Choi }
468914b881fSChanwoo Choi
sm5502_muic_cable_handler(struct sm5502_muic_info * info,bool attached)469914b881fSChanwoo Choi static int sm5502_muic_cable_handler(struct sm5502_muic_info *info,
470914b881fSChanwoo Choi bool attached)
471914b881fSChanwoo Choi {
472914b881fSChanwoo Choi static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND;
473914b881fSChanwoo Choi unsigned int cable_type = SM5502_MUIC_ADC_GROUND;
474a75fed2eSChanwoo Choi unsigned int con_sw = DM_DP_SWITCH_OPEN;
475a75fed2eSChanwoo Choi unsigned int vbus_sw = VBUSIN_SWITCH_OPEN;
47673b6ecdbSChanwoo Choi unsigned int id;
477a75fed2eSChanwoo Choi int ret;
478914b881fSChanwoo Choi
479914b881fSChanwoo Choi /* Get the type of attached or detached cable */
480914b881fSChanwoo Choi if (attached)
481914b881fSChanwoo Choi cable_type = sm5502_muic_get_cable_type(info);
482fbae30d8SChanwoo Choi else
483914b881fSChanwoo Choi cable_type = prev_cable_type;
484914b881fSChanwoo Choi prev_cable_type = cable_type;
485914b881fSChanwoo Choi
486914b881fSChanwoo Choi switch (cable_type) {
487914b881fSChanwoo Choi case SM5502_MUIC_ADC_OPEN_USB:
4882a9de9c0SChanwoo Choi id = EXTCON_USB;
489a75fed2eSChanwoo Choi con_sw = DM_DP_SWITCH_USB;
490a75fed2eSChanwoo Choi vbus_sw = VBUSIN_SWITCH_VBUSOUT_WITH_USB;
491914b881fSChanwoo Choi break;
492914b881fSChanwoo Choi case SM5502_MUIC_ADC_OPEN_TA:
49311eecf91SChanwoo Choi id = EXTCON_CHG_USB_DCP;
494a75fed2eSChanwoo Choi con_sw = DM_DP_SWITCH_OPEN;
495a75fed2eSChanwoo Choi vbus_sw = VBUSIN_SWITCH_VBUSOUT;
496914b881fSChanwoo Choi break;
497e3f60329SNikita Travkin case SM5502_MUIC_ADC_GROUND_USB_OTG:
498914b881fSChanwoo Choi case SM5502_MUIC_ADC_OPEN_USB_OTG:
4992a9de9c0SChanwoo Choi id = EXTCON_USB_HOST;
500a75fed2eSChanwoo Choi con_sw = DM_DP_SWITCH_USB;
501a75fed2eSChanwoo Choi vbus_sw = VBUSIN_SWITCH_OPEN;
502e1954452SChanwoo Choi break;
503914b881fSChanwoo Choi default:
504914b881fSChanwoo Choi dev_dbg(info->dev,
505914b881fSChanwoo Choi "cannot handle this cable_type (0x%x)\n", cable_type);
506914b881fSChanwoo Choi return 0;
5072ddf50a7SXu Wang }
508914b881fSChanwoo Choi
509a75fed2eSChanwoo Choi /* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */
510a75fed2eSChanwoo Choi ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached);
511a75fed2eSChanwoo Choi if (ret < 0)
512a75fed2eSChanwoo Choi return ret;
513a75fed2eSChanwoo Choi
514a75fed2eSChanwoo Choi /* Change the state of external accessory */
5158670b459SChanwoo Choi extcon_set_state_sync(info->edev, id, attached);
5168b45b6a0SChanwoo Choi if (id == EXTCON_USB)
5178670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
5188b45b6a0SChanwoo Choi attached);
519914b881fSChanwoo Choi
520914b881fSChanwoo Choi return 0;
521914b881fSChanwoo Choi }
522914b881fSChanwoo Choi
sm5502_muic_irq_work(struct work_struct * work)523914b881fSChanwoo Choi static void sm5502_muic_irq_work(struct work_struct *work)
524914b881fSChanwoo Choi {
525914b881fSChanwoo Choi struct sm5502_muic_info *info = container_of(work,
526914b881fSChanwoo Choi struct sm5502_muic_info, irq_work);
527914b881fSChanwoo Choi int ret = 0;
528914b881fSChanwoo Choi
529914b881fSChanwoo Choi if (!info->edev)
530914b881fSChanwoo Choi return;
531914b881fSChanwoo Choi
532914b881fSChanwoo Choi mutex_lock(&info->mutex);
533914b881fSChanwoo Choi
534914b881fSChanwoo Choi /* Detect attached or detached cables */
535914b881fSChanwoo Choi if (info->irq_attach) {
536914b881fSChanwoo Choi ret = sm5502_muic_cable_handler(info, true);
537914b881fSChanwoo Choi info->irq_attach = false;
538914b881fSChanwoo Choi }
539914b881fSChanwoo Choi if (info->irq_detach) {
540914b881fSChanwoo Choi ret = sm5502_muic_cable_handler(info, false);
541914b881fSChanwoo Choi info->irq_detach = false;
542914b881fSChanwoo Choi }
543914b881fSChanwoo Choi
544914b881fSChanwoo Choi if (ret < 0)
545914b881fSChanwoo Choi dev_err(info->dev, "failed to handle MUIC interrupt\n");
546914b881fSChanwoo Choi
547914b881fSChanwoo Choi mutex_unlock(&info->mutex);
548914b881fSChanwoo Choi }
549914b881fSChanwoo Choi
550914b881fSChanwoo Choi /*
551914b881fSChanwoo Choi * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0.
552914b881fSChanwoo Choi * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
553914b881fSChanwoo Choi */
sm5502_parse_irq(struct sm5502_muic_info * info,int irq_type)554914b881fSChanwoo Choi static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type)
555914b881fSChanwoo Choi {
556914b881fSChanwoo Choi switch (irq_type) {
557914b881fSChanwoo Choi case SM5502_IRQ_INT1_ATTACH:
558914b881fSChanwoo Choi info->irq_attach = true;
559914b881fSChanwoo Choi break;
560914b881fSChanwoo Choi case SM5502_IRQ_INT1_DETACH:
561914b881fSChanwoo Choi info->irq_detach = true;
562914b881fSChanwoo Choi break;
563914b881fSChanwoo Choi case SM5502_IRQ_INT1_KP:
564914b881fSChanwoo Choi case SM5502_IRQ_INT1_LKP:
565914b881fSChanwoo Choi case SM5502_IRQ_INT1_LKR:
566914b881fSChanwoo Choi case SM5502_IRQ_INT1_OVP_EVENT:
567914b881fSChanwoo Choi case SM5502_IRQ_INT1_OCP_EVENT:
568914b881fSChanwoo Choi case SM5502_IRQ_INT1_OVP_OCP_DIS:
569914b881fSChanwoo Choi case SM5502_IRQ_INT2_VBUS_DET:
570914b881fSChanwoo Choi case SM5502_IRQ_INT2_REV_ACCE:
571914b881fSChanwoo Choi case SM5502_IRQ_INT2_ADC_CHG:
572914b881fSChanwoo Choi case SM5502_IRQ_INT2_STUCK_KEY:
573914b881fSChanwoo Choi case SM5502_IRQ_INT2_STUCK_KEY_RCV:
574914b881fSChanwoo Choi case SM5502_IRQ_INT2_MHL:
575914b881fSChanwoo Choi default:
576914b881fSChanwoo Choi break;
577914b881fSChanwoo Choi }
578914b881fSChanwoo Choi
579914b881fSChanwoo Choi return 0;
580914b881fSChanwoo Choi }
581914b881fSChanwoo Choi
sm5504_parse_irq(struct sm5502_muic_info * info,int irq_type)582d97c0ff5SStephan Gerhold static int sm5504_parse_irq(struct sm5502_muic_info *info, int irq_type)
583d97c0ff5SStephan Gerhold {
584d97c0ff5SStephan Gerhold switch (irq_type) {
585d97c0ff5SStephan Gerhold case SM5504_IRQ_INT1_ATTACH:
586d97c0ff5SStephan Gerhold info->irq_attach = true;
587d97c0ff5SStephan Gerhold break;
588d97c0ff5SStephan Gerhold case SM5504_IRQ_INT1_DETACH:
589d97c0ff5SStephan Gerhold info->irq_detach = true;
590d97c0ff5SStephan Gerhold break;
591d97c0ff5SStephan Gerhold case SM5504_IRQ_INT1_CHG_DET:
592d97c0ff5SStephan Gerhold case SM5504_IRQ_INT1_DCD_OUT:
593d97c0ff5SStephan Gerhold case SM5504_IRQ_INT1_OVP_EVENT:
594d97c0ff5SStephan Gerhold case SM5504_IRQ_INT1_CONNECT:
595d97c0ff5SStephan Gerhold case SM5504_IRQ_INT1_ADC_CHG:
596d97c0ff5SStephan Gerhold case SM5504_IRQ_INT2_RID_CHG:
597d97c0ff5SStephan Gerhold case SM5504_IRQ_INT2_UVLO:
598d97c0ff5SStephan Gerhold case SM5504_IRQ_INT2_POR:
599d97c0ff5SStephan Gerhold case SM5504_IRQ_INT2_OVP_FET:
600d97c0ff5SStephan Gerhold case SM5504_IRQ_INT2_OCP_LATCH:
601d97c0ff5SStephan Gerhold case SM5504_IRQ_INT2_OCP_EVENT:
602d97c0ff5SStephan Gerhold case SM5504_IRQ_INT2_OVP_OCP_EVENT:
603d97c0ff5SStephan Gerhold default:
604d97c0ff5SStephan Gerhold break;
605d97c0ff5SStephan Gerhold }
606d97c0ff5SStephan Gerhold
607d97c0ff5SStephan Gerhold return 0;
608d97c0ff5SStephan Gerhold }
609d97c0ff5SStephan Gerhold
sm5502_muic_irq_handler(int irq,void * data)610914b881fSChanwoo Choi static irqreturn_t sm5502_muic_irq_handler(int irq, void *data)
611914b881fSChanwoo Choi {
612914b881fSChanwoo Choi struct sm5502_muic_info *info = data;
613914b881fSChanwoo Choi int i, irq_type = -1, ret;
614914b881fSChanwoo Choi
615f33c056dSStephan Gerhold for (i = 0; i < info->type->num_muic_irqs; i++)
616f33c056dSStephan Gerhold if (irq == info->type->muic_irqs[i].virq)
617f33c056dSStephan Gerhold irq_type = info->type->muic_irqs[i].irq;
618914b881fSChanwoo Choi
619f33c056dSStephan Gerhold ret = info->type->parse_irq(info, irq_type);
620914b881fSChanwoo Choi if (ret < 0) {
621914b881fSChanwoo Choi dev_warn(info->dev, "cannot handle is interrupt:%d\n",
622914b881fSChanwoo Choi irq_type);
623914b881fSChanwoo Choi return IRQ_HANDLED;
624914b881fSChanwoo Choi }
625914b881fSChanwoo Choi schedule_work(&info->irq_work);
626914b881fSChanwoo Choi
627914b881fSChanwoo Choi return IRQ_HANDLED;
628914b881fSChanwoo Choi }
629914b881fSChanwoo Choi
sm5502_muic_detect_cable_wq(struct work_struct * work)630e1954452SChanwoo Choi static void sm5502_muic_detect_cable_wq(struct work_struct *work)
631e1954452SChanwoo Choi {
632e1954452SChanwoo Choi struct sm5502_muic_info *info = container_of(to_delayed_work(work),
633e1954452SChanwoo Choi struct sm5502_muic_info, wq_detcable);
634e1954452SChanwoo Choi int ret;
635e1954452SChanwoo Choi
636e1954452SChanwoo Choi /* Notify the state of connector cable or not */
637e1954452SChanwoo Choi ret = sm5502_muic_cable_handler(info, true);
638e1954452SChanwoo Choi if (ret < 0)
639e1954452SChanwoo Choi dev_warn(info->dev, "failed to detect cable state\n");
640e1954452SChanwoo Choi }
641e1954452SChanwoo Choi
sm5502_init_dev_type(struct sm5502_muic_info * info)642914b881fSChanwoo Choi static void sm5502_init_dev_type(struct sm5502_muic_info *info)
643914b881fSChanwoo Choi {
644914b881fSChanwoo Choi unsigned int reg_data, vendor_id, version_id;
645914b881fSChanwoo Choi int i, ret;
646914b881fSChanwoo Choi
647914b881fSChanwoo Choi /* To test I2C, Print version_id and vendor_id of SM5502 */
648914b881fSChanwoo Choi ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, ®_data);
649914b881fSChanwoo Choi if (ret) {
650914b881fSChanwoo Choi dev_err(info->dev,
651914b881fSChanwoo Choi "failed to read DEVICE_ID register: %d\n", ret);
652914b881fSChanwoo Choi return;
653914b881fSChanwoo Choi }
654914b881fSChanwoo Choi
655914b881fSChanwoo Choi vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >>
656914b881fSChanwoo Choi SM5502_REG_DEVICE_ID_VENDOR_SHIFT);
657914b881fSChanwoo Choi version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >>
658914b881fSChanwoo Choi SM5502_REG_DEVICE_ID_VERSION_SHIFT);
659914b881fSChanwoo Choi
660914b881fSChanwoo Choi dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
661914b881fSChanwoo Choi version_id, vendor_id);
662914b881fSChanwoo Choi
663914b881fSChanwoo Choi /* Initiazle the register of SM5502 device to bring-up */
664f33c056dSStephan Gerhold for (i = 0; i < info->type->num_reg_data; i++) {
665914b881fSChanwoo Choi unsigned int val = 0;
666914b881fSChanwoo Choi
667f33c056dSStephan Gerhold if (!info->type->reg_data[i].invert)
668f33c056dSStephan Gerhold val |= ~info->type->reg_data[i].val;
669914b881fSChanwoo Choi else
670f33c056dSStephan Gerhold val = info->type->reg_data[i].val;
671f33c056dSStephan Gerhold regmap_write(info->regmap, info->type->reg_data[i].reg, val);
672914b881fSChanwoo Choi }
673914b881fSChanwoo Choi }
674914b881fSChanwoo Choi
sm5022_muic_i2c_probe(struct i2c_client * i2c)675b1b76af2SStephan Gerhold static int sm5022_muic_i2c_probe(struct i2c_client *i2c)
676914b881fSChanwoo Choi {
677914b881fSChanwoo Choi struct device_node *np = i2c->dev.of_node;
678914b881fSChanwoo Choi struct sm5502_muic_info *info;
679914b881fSChanwoo Choi int i, ret, irq_flags;
680914b881fSChanwoo Choi
681914b881fSChanwoo Choi if (!np)
682914b881fSChanwoo Choi return -EINVAL;
683914b881fSChanwoo Choi
684914b881fSChanwoo Choi info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
685914b881fSChanwoo Choi if (!info)
686914b881fSChanwoo Choi return -ENOMEM;
687914b881fSChanwoo Choi i2c_set_clientdata(i2c, info);
688914b881fSChanwoo Choi
689914b881fSChanwoo Choi info->dev = &i2c->dev;
690914b881fSChanwoo Choi info->i2c = i2c;
691914b881fSChanwoo Choi info->irq = i2c->irq;
692f33c056dSStephan Gerhold info->type = device_get_match_data(info->dev);
693f33c056dSStephan Gerhold if (!info->type)
694f33c056dSStephan Gerhold return -EINVAL;
695f33c056dSStephan Gerhold if (!info->type->parse_irq) {
696f33c056dSStephan Gerhold dev_err(info->dev, "parse_irq missing in struct sm5502_type\n");
697f33c056dSStephan Gerhold return -EINVAL;
698f33c056dSStephan Gerhold }
699914b881fSChanwoo Choi
700914b881fSChanwoo Choi mutex_init(&info->mutex);
701914b881fSChanwoo Choi
702914b881fSChanwoo Choi INIT_WORK(&info->irq_work, sm5502_muic_irq_work);
703914b881fSChanwoo Choi
704914b881fSChanwoo Choi info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config);
705914b881fSChanwoo Choi if (IS_ERR(info->regmap)) {
706914b881fSChanwoo Choi ret = PTR_ERR(info->regmap);
707914b881fSChanwoo Choi dev_err(info->dev, "failed to allocate register map: %d\n",
708914b881fSChanwoo Choi ret);
709914b881fSChanwoo Choi return ret;
710914b881fSChanwoo Choi }
711914b881fSChanwoo Choi
712914b881fSChanwoo Choi /* Support irq domain for SM5502 MUIC device */
713914b881fSChanwoo Choi irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
714f33c056dSStephan Gerhold ret = devm_regmap_add_irq_chip(info->dev, info->regmap, info->irq,
715f33c056dSStephan Gerhold irq_flags, 0, info->type->irq_chip,
716f33c056dSStephan Gerhold &info->irq_data);
717914b881fSChanwoo Choi if (ret != 0) {
718914b881fSChanwoo Choi dev_err(info->dev, "failed to request IRQ %d: %d\n",
719914b881fSChanwoo Choi info->irq, ret);
720914b881fSChanwoo Choi return ret;
721914b881fSChanwoo Choi }
722914b881fSChanwoo Choi
723f33c056dSStephan Gerhold for (i = 0; i < info->type->num_muic_irqs; i++) {
724f33c056dSStephan Gerhold struct muic_irq *muic_irq = &info->type->muic_irqs[i];
725363b3891SAndrzej Hajda int virq = 0;
726914b881fSChanwoo Choi
727914b881fSChanwoo Choi virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
728914b881fSChanwoo Choi if (virq <= 0)
729914b881fSChanwoo Choi return -EINVAL;
730914b881fSChanwoo Choi muic_irq->virq = virq;
731914b881fSChanwoo Choi
732914b881fSChanwoo Choi ret = devm_request_threaded_irq(info->dev, virq, NULL,
733914b881fSChanwoo Choi sm5502_muic_irq_handler,
734005ad187SVasyl Gomonovych IRQF_NO_SUSPEND | IRQF_ONESHOT,
735914b881fSChanwoo Choi muic_irq->name, info);
736914b881fSChanwoo Choi if (ret) {
737fbae30d8SChanwoo Choi dev_err(info->dev,
738fbae30d8SChanwoo Choi "failed: irq request (IRQ: %d, error :%d)\n",
739fbae30d8SChanwoo Choi muic_irq->irq, ret);
740914b881fSChanwoo Choi return ret;
741914b881fSChanwoo Choi }
742914b881fSChanwoo Choi }
743914b881fSChanwoo Choi
744914b881fSChanwoo Choi /* Allocate extcon device */
745914b881fSChanwoo Choi info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable);
746914b881fSChanwoo Choi if (IS_ERR(info->edev)) {
747914b881fSChanwoo Choi dev_err(info->dev, "failed to allocate memory for extcon\n");
748914b881fSChanwoo Choi return -ENOMEM;
749914b881fSChanwoo Choi }
750914b881fSChanwoo Choi
751914b881fSChanwoo Choi /* Register extcon device */
752914b881fSChanwoo Choi ret = devm_extcon_dev_register(info->dev, info->edev);
753914b881fSChanwoo Choi if (ret) {
754914b881fSChanwoo Choi dev_err(info->dev, "failed to register extcon device\n");
755914b881fSChanwoo Choi return ret;
756914b881fSChanwoo Choi }
757914b881fSChanwoo Choi
758e1954452SChanwoo Choi /*
759e1954452SChanwoo Choi * Detect accessory after completing the initialization of platform
760e1954452SChanwoo Choi *
761e1954452SChanwoo Choi * - Use delayed workqueue to detect cable state and then
762e1954452SChanwoo Choi * notify cable state to notifiee/platform through uevent.
763e1954452SChanwoo Choi * After completing the booting of platform, the extcon provider
764e1954452SChanwoo Choi * driver should notify cable state to upper layer.
765e1954452SChanwoo Choi */
766e1954452SChanwoo Choi INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
767e1954452SChanwoo Choi queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
768e1954452SChanwoo Choi msecs_to_jiffies(DELAY_MS_DEFAULT));
769e1954452SChanwoo Choi
770914b881fSChanwoo Choi /* Initialize SM5502 device and print vendor id and version id */
771914b881fSChanwoo Choi sm5502_init_dev_type(info);
772914b881fSChanwoo Choi
773914b881fSChanwoo Choi return 0;
774914b881fSChanwoo Choi }
775914b881fSChanwoo Choi
776f33c056dSStephan Gerhold static const struct sm5502_type sm5502_data = {
777f33c056dSStephan Gerhold .muic_irqs = sm5502_muic_irqs,
778f33c056dSStephan Gerhold .num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs),
779f33c056dSStephan Gerhold .irq_chip = &sm5502_muic_irq_chip,
780f33c056dSStephan Gerhold .reg_data = sm5502_reg_data,
781f33c056dSStephan Gerhold .num_reg_data = ARRAY_SIZE(sm5502_reg_data),
782d97c0ff5SStephan Gerhold .otg_dev_type1 = SM5502_REG_DEV_TYPE1_USB_OTG_MASK,
783f33c056dSStephan Gerhold .parse_irq = sm5502_parse_irq,
784f33c056dSStephan Gerhold };
785f33c056dSStephan Gerhold
786d97c0ff5SStephan Gerhold static const struct sm5502_type sm5504_data = {
787d97c0ff5SStephan Gerhold .muic_irqs = sm5504_muic_irqs,
788d97c0ff5SStephan Gerhold .num_muic_irqs = ARRAY_SIZE(sm5504_muic_irqs),
789d97c0ff5SStephan Gerhold .irq_chip = &sm5504_muic_irq_chip,
790d97c0ff5SStephan Gerhold .reg_data = sm5504_reg_data,
791d97c0ff5SStephan Gerhold .num_reg_data = ARRAY_SIZE(sm5504_reg_data),
792d97c0ff5SStephan Gerhold .otg_dev_type1 = SM5504_REG_DEV_TYPE1_USB_OTG_MASK,
793d97c0ff5SStephan Gerhold .parse_irq = sm5504_parse_irq,
794d97c0ff5SStephan Gerhold };
795d97c0ff5SStephan Gerhold
79634825e51SChanwoo Choi static const struct of_device_id sm5502_dt_match[] = {
797f33c056dSStephan Gerhold { .compatible = "siliconmitus,sm5502-muic", .data = &sm5502_data },
798d97c0ff5SStephan Gerhold { .compatible = "siliconmitus,sm5504-muic", .data = &sm5504_data },
799a84df1c7SMarkuss Broks { .compatible = "siliconmitus,sm5703-muic", .data = &sm5502_data },
800914b881fSChanwoo Choi { },
801914b881fSChanwoo Choi };
802ff612f91SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, sm5502_dt_match);
803914b881fSChanwoo Choi
804914b881fSChanwoo Choi #ifdef CONFIG_PM_SLEEP
sm5502_muic_suspend(struct device * dev)805914b881fSChanwoo Choi static int sm5502_muic_suspend(struct device *dev)
806914b881fSChanwoo Choi {
807d5859342SGeliang Tang struct i2c_client *i2c = to_i2c_client(dev);
808914b881fSChanwoo Choi struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
809914b881fSChanwoo Choi
810914b881fSChanwoo Choi enable_irq_wake(info->irq);
811914b881fSChanwoo Choi
812914b881fSChanwoo Choi return 0;
813914b881fSChanwoo Choi }
814914b881fSChanwoo Choi
sm5502_muic_resume(struct device * dev)815914b881fSChanwoo Choi static int sm5502_muic_resume(struct device *dev)
816914b881fSChanwoo Choi {
817d5859342SGeliang Tang struct i2c_client *i2c = to_i2c_client(dev);
818914b881fSChanwoo Choi struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
819914b881fSChanwoo Choi
820914b881fSChanwoo Choi disable_irq_wake(info->irq);
821914b881fSChanwoo Choi
822914b881fSChanwoo Choi return 0;
823914b881fSChanwoo Choi }
824914b881fSChanwoo Choi #endif
825914b881fSChanwoo Choi
826914b881fSChanwoo Choi static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops,
827914b881fSChanwoo Choi sm5502_muic_suspend, sm5502_muic_resume);
828914b881fSChanwoo Choi
829914b881fSChanwoo Choi static const struct i2c_device_id sm5502_i2c_id[] = {
830f33c056dSStephan Gerhold { "sm5502", (kernel_ulong_t)&sm5502_data },
831d97c0ff5SStephan Gerhold { "sm5504", (kernel_ulong_t)&sm5504_data },
8325faf7cbaSMarkuss Broks { "sm5703-muic", (kernel_ulong_t)&sm5502_data },
833914b881fSChanwoo Choi { }
834914b881fSChanwoo Choi };
835914b881fSChanwoo Choi MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id);
836914b881fSChanwoo Choi
837914b881fSChanwoo Choi static struct i2c_driver sm5502_muic_i2c_driver = {
838914b881fSChanwoo Choi .driver = {
839914b881fSChanwoo Choi .name = "sm5502",
840914b881fSChanwoo Choi .pm = &sm5502_muic_pm_ops,
841914b881fSChanwoo Choi .of_match_table = sm5502_dt_match,
842914b881fSChanwoo Choi },
843*bcfa8e33SUwe Kleine-König .probe = sm5022_muic_i2c_probe,
844914b881fSChanwoo Choi .id_table = sm5502_i2c_id,
845914b881fSChanwoo Choi };
846914b881fSChanwoo Choi
sm5502_muic_i2c_init(void)847914b881fSChanwoo Choi static int __init sm5502_muic_i2c_init(void)
848914b881fSChanwoo Choi {
849914b881fSChanwoo Choi return i2c_add_driver(&sm5502_muic_i2c_driver);
850914b881fSChanwoo Choi }
851914b881fSChanwoo Choi subsys_initcall(sm5502_muic_i2c_init);
852914b881fSChanwoo Choi
853914b881fSChanwoo Choi MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver");
854914b881fSChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
855914b881fSChanwoo Choi MODULE_LICENSE("GPL");
856