blob: 60e2d12e81a2eb73b57784bd0fdfcb89910c3aad [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Chanwoo Choi914b8812014-05-22 14:06:33 +09002/*
3 * extcon-sm5502.c - Silicon Mitus SM5502 extcon drvier to support USB switches
4 *
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd
6 * Author: Chanwoo Choi <cw00.choi@samsung.com>
Chanwoo Choi914b8812014-05-22 14:06:33 +09007 */
8
9#include <linux/err.h>
10#include <linux/i2c.h>
Chanwoo Choi914b8812014-05-22 14:06:33 +090011#include <linux/interrupt.h>
12#include <linux/irqdomain.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/regmap.h>
17#include <linux/slab.h>
Chanwoo Choi176aa362017-09-21 12:11:24 +090018#include <linux/extcon-provider.h>
Chanwoo Choica2a07e2014-07-31 16:32:46 +090019
20#include "extcon-sm5502.h"
Chanwoo Choi914b8812014-05-22 14:06:33 +090021
Chanwoo Choie1954452014-05-28 14:21:55 +090022#define DELAY_MS_DEFAULT 17000 /* unit: millisecond */
23
Chanwoo Choi914b8812014-05-22 14:06:33 +090024struct muic_irq {
25 unsigned int irq;
26 const char *name;
27 unsigned int virq;
28};
29
30struct reg_data {
31 u8 reg;
32 unsigned int val;
33 bool invert;
34};
35
36struct sm5502_muic_info {
37 struct device *dev;
38 struct extcon_dev *edev;
39
40 struct i2c_client *i2c;
41 struct regmap *regmap;
42
Stephan Gerholdf33c0562021-06-03 10:52:21 +020043 const struct sm5502_type *type;
Chanwoo Choi914b8812014-05-22 14:06:33 +090044 struct regmap_irq_chip_data *irq_data;
Chanwoo Choi914b8812014-05-22 14:06:33 +090045 int irq;
46 bool irq_attach;
47 bool irq_detach;
48 struct work_struct irq_work;
49
Chanwoo Choi914b8812014-05-22 14:06:33 +090050 struct mutex mutex;
Chanwoo Choie1954452014-05-28 14:21:55 +090051
52 /*
53 * Use delayed workqueue to detect cable state and then
54 * notify cable state to notifiee/platform through uevent.
55 * After completing the booting of platform, the extcon provider
56 * driver should notify cable state to upper layer.
57 */
58 struct delayed_work wq_detcable;
Chanwoo Choi914b8812014-05-22 14:06:33 +090059};
60
Stephan Gerholdf33c0562021-06-03 10:52:21 +020061struct sm5502_type {
62 struct muic_irq *muic_irqs;
63 unsigned int num_muic_irqs;
64 const struct regmap_irq_chip *irq_chip;
65
66 struct reg_data *reg_data;
67 unsigned int num_reg_data;
68
69 int (*parse_irq)(struct sm5502_muic_info *info, int irq_type);
70};
71
Chanwoo Choi914b8812014-05-22 14:06:33 +090072/* Default value of SM5502 register to bring up MUIC device. */
73static struct reg_data sm5502_reg_data[] = {
74 {
Stephan Gerhold69426352019-10-10 17:47:20 +020075 .reg = SM5502_REG_RESET,
76 .val = SM5502_REG_RESET_MASK,
77 .invert = true,
78 }, {
Chanwoo Choi914b8812014-05-22 14:06:33 +090079 .reg = SM5502_REG_CONTROL,
80 .val = SM5502_REG_CONTROL_MASK_INT_MASK,
81 .invert = false,
82 }, {
83 .reg = SM5502_REG_INTMASK1,
84 .val = SM5502_REG_INTM1_KP_MASK
85 | SM5502_REG_INTM1_LKP_MASK
86 | SM5502_REG_INTM1_LKR_MASK,
87 .invert = true,
88 }, {
89 .reg = SM5502_REG_INTMASK2,
90 .val = SM5502_REG_INTM2_VBUS_DET_MASK
91 | SM5502_REG_INTM2_REV_ACCE_MASK
92 | SM5502_REG_INTM2_ADC_CHG_MASK
93 | SM5502_REG_INTM2_STUCK_KEY_MASK
94 | SM5502_REG_INTM2_STUCK_KEY_RCV_MASK
95 | SM5502_REG_INTM2_MHL_MASK,
96 .invert = true,
97 },
Chanwoo Choi914b8812014-05-22 14:06:33 +090098};
99
100/* List of detectable cables */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900101static const unsigned int sm5502_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900102 EXTCON_USB,
103 EXTCON_USB_HOST,
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +0900104 EXTCON_CHG_USB_SDP,
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900105 EXTCON_CHG_USB_DCP,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900106 EXTCON_NONE,
Chanwoo Choi914b8812014-05-22 14:06:33 +0900107};
108
109/* Define supported accessory type */
110enum sm5502_muic_acc_type {
111 SM5502_MUIC_ADC_GROUND = 0x0,
112 SM5502_MUIC_ADC_SEND_END_BUTTON,
113 SM5502_MUIC_ADC_REMOTE_S1_BUTTON,
114 SM5502_MUIC_ADC_REMOTE_S2_BUTTON,
115 SM5502_MUIC_ADC_REMOTE_S3_BUTTON,
116 SM5502_MUIC_ADC_REMOTE_S4_BUTTON,
117 SM5502_MUIC_ADC_REMOTE_S5_BUTTON,
118 SM5502_MUIC_ADC_REMOTE_S6_BUTTON,
119 SM5502_MUIC_ADC_REMOTE_S7_BUTTON,
120 SM5502_MUIC_ADC_REMOTE_S8_BUTTON,
121 SM5502_MUIC_ADC_REMOTE_S9_BUTTON,
122 SM5502_MUIC_ADC_REMOTE_S10_BUTTON,
123 SM5502_MUIC_ADC_REMOTE_S11_BUTTON,
124 SM5502_MUIC_ADC_REMOTE_S12_BUTTON,
125 SM5502_MUIC_ADC_RESERVED_ACC_1,
126 SM5502_MUIC_ADC_RESERVED_ACC_2,
127 SM5502_MUIC_ADC_RESERVED_ACC_3,
128 SM5502_MUIC_ADC_RESERVED_ACC_4,
129 SM5502_MUIC_ADC_RESERVED_ACC_5,
130 SM5502_MUIC_ADC_AUDIO_TYPE2,
131 SM5502_MUIC_ADC_PHONE_POWERED_DEV,
132 SM5502_MUIC_ADC_TTY_CONVERTER,
133 SM5502_MUIC_ADC_UART_CABLE,
134 SM5502_MUIC_ADC_TYPE1_CHARGER,
135 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
136 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
137 SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE,
138 SM5502_MUIC_ADC_TYPE2_CHARGER,
139 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
140 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
141 SM5502_MUIC_ADC_AUDIO_TYPE1,
142 SM5502_MUIC_ADC_OPEN = 0x1f,
143
Srikant Ritoliaaf57fa42016-12-07 17:29:39 +0530144 /*
145 * The below accessories have same ADC value (0x1f or 0x1e).
146 * So, Device type1 is used to separate specific accessory.
147 */
Chanwoo Choi914b8812014-05-22 14:06:33 +0900148 /* |---------|--ADC| */
149 /* | [7:5]|[4:0]| */
150 SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* | 001|11110| */
151 SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e, /* | 010|11110| */
152 /* |Dev Type1|--ADC| */
Nikita Travkine3f60322021-01-19 18:33:47 +0500153 SM5502_MUIC_ADC_GROUND_USB_OTG = 0x80, /* | 100|00000| */
Chanwoo Choi914b8812014-05-22 14:06:33 +0900154 SM5502_MUIC_ADC_OPEN_USB = 0x5f, /* | 010|11111| */
155 SM5502_MUIC_ADC_OPEN_TA = 0xdf, /* | 110|11111| */
156 SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff, /* | 111|11111| */
157};
158
159/* List of supported interrupt for SM5502 */
160static struct muic_irq sm5502_muic_irqs[] = {
161 { SM5502_IRQ_INT1_ATTACH, "muic-attach" },
162 { SM5502_IRQ_INT1_DETACH, "muic-detach" },
163 { SM5502_IRQ_INT1_KP, "muic-kp" },
164 { SM5502_IRQ_INT1_LKP, "muic-lkp" },
165 { SM5502_IRQ_INT1_LKR, "muic-lkr" },
166 { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" },
167 { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" },
168 { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" },
169 { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" },
170 { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" },
171 { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" },
172 { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" },
173 { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" },
174 { SM5502_IRQ_INT2_MHL, "muic-mhl" },
175};
176
177/* Define interrupt list of SM5502 to register regmap_irq */
178static const struct regmap_irq sm5502_irqs[] = {
179 /* INT1 interrupts */
180 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, },
181 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, },
182 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, },
183 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, },
184 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, },
185 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, },
186 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, },
187 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, },
188
189 /* INT2 interrupts */
190 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,},
191 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, },
192 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, },
193 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, },
194 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, },
195 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, },
196};
197
198static const struct regmap_irq_chip sm5502_muic_irq_chip = {
199 .name = "sm5502",
200 .status_base = SM5502_REG_INT1,
201 .mask_base = SM5502_REG_INTMASK1,
202 .mask_invert = false,
203 .num_regs = 2,
204 .irqs = sm5502_irqs,
205 .num_irqs = ARRAY_SIZE(sm5502_irqs),
206};
207
208/* Define regmap configuration of SM5502 for I2C communication */
209static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg)
210{
211 switch (reg) {
212 case SM5502_REG_INTMASK1:
213 case SM5502_REG_INTMASK2:
214 return true;
215 default:
216 break;
217 }
218 return false;
219}
220
221static const struct regmap_config sm5502_muic_regmap_config = {
222 .reg_bits = 8,
223 .val_bits = 8,
224 .volatile_reg = sm5502_muic_volatile_reg,
225 .max_register = SM5502_REG_END,
226};
227
Chanwoo Choia75fed22014-05-28 15:35:29 +0900228/* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
229static int sm5502_muic_set_path(struct sm5502_muic_info *info,
230 unsigned int con_sw, unsigned int vbus_sw,
231 bool attached)
232{
233 int ret;
234
235 if (!attached) {
236 con_sw = DM_DP_SWITCH_OPEN;
237 vbus_sw = VBUSIN_SWITCH_OPEN;
238 }
239
240 switch (con_sw) {
241 case DM_DP_SWITCH_OPEN:
242 case DM_DP_SWITCH_USB:
243 case DM_DP_SWITCH_AUDIO:
244 case DM_DP_SWITCH_UART:
245 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
246 SM5502_REG_MANUAL_SW1_DP_MASK |
247 SM5502_REG_MANUAL_SW1_DM_MASK,
248 con_sw);
249 if (ret < 0) {
250 dev_err(info->dev,
251 "cannot update DM_CON/DP_CON switch\n");
252 return ret;
253 }
254 break;
255 default:
256 dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
257 con_sw);
258 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000259 }
Chanwoo Choia75fed22014-05-28 15:35:29 +0900260
261 switch (vbus_sw) {
262 case VBUSIN_SWITCH_OPEN:
263 case VBUSIN_SWITCH_VBUSOUT:
264 case VBUSIN_SWITCH_MIC:
265 case VBUSIN_SWITCH_VBUSOUT_WITH_USB:
266 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
267 SM5502_REG_MANUAL_SW1_VBUSIN_MASK,
268 vbus_sw);
269 if (ret < 0) {
270 dev_err(info->dev,
271 "cannot update VBUSIN switch\n");
272 return ret;
273 }
274 break;
275 default:
276 dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw);
277 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000278 }
Chanwoo Choia75fed22014-05-28 15:35:29 +0900279
280 return 0;
281}
282
Chanwoo Choi914b8812014-05-22 14:06:33 +0900283/* Return cable type of attached or detached accessories */
284static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info)
285{
Colin Ian Kingddd1bbb2019-10-25 14:12:27 +0100286 unsigned int cable_type, adc, dev_type1;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900287 int ret;
288
289 /* Read ADC value according to external cable or button */
290 ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc);
291 if (ret) {
292 dev_err(info->dev, "failed to read ADC register\n");
293 return ret;
294 }
295
296 /*
297 * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't
298 * connected with to MUIC device.
299 */
Chanwoo Choi0ccc7952014-07-30 15:39:02 +0900300 cable_type = adc & SM5502_REG_ADC_MASK;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900301
302 switch (cable_type) {
303 case SM5502_MUIC_ADC_GROUND:
Nikita Travkine3f60322021-01-19 18:33:47 +0500304 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
305 &dev_type1);
306 if (ret) {
307 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
308 return ret;
309 }
310
311 switch (dev_type1) {
312 case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
313 cable_type = SM5502_MUIC_ADC_GROUND_USB_OTG;
314 break;
315 default:
316 dev_dbg(info->dev,
317 "cannot identify the cable type: adc(0x%x), dev_type1(0x%x)\n",
318 adc, dev_type1);
319 return -EINVAL;
320 }
321 break;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900322 case SM5502_MUIC_ADC_SEND_END_BUTTON:
323 case SM5502_MUIC_ADC_REMOTE_S1_BUTTON:
324 case SM5502_MUIC_ADC_REMOTE_S2_BUTTON:
325 case SM5502_MUIC_ADC_REMOTE_S3_BUTTON:
326 case SM5502_MUIC_ADC_REMOTE_S4_BUTTON:
327 case SM5502_MUIC_ADC_REMOTE_S5_BUTTON:
328 case SM5502_MUIC_ADC_REMOTE_S6_BUTTON:
329 case SM5502_MUIC_ADC_REMOTE_S7_BUTTON:
330 case SM5502_MUIC_ADC_REMOTE_S8_BUTTON:
331 case SM5502_MUIC_ADC_REMOTE_S9_BUTTON:
332 case SM5502_MUIC_ADC_REMOTE_S10_BUTTON:
333 case SM5502_MUIC_ADC_REMOTE_S11_BUTTON:
334 case SM5502_MUIC_ADC_REMOTE_S12_BUTTON:
335 case SM5502_MUIC_ADC_RESERVED_ACC_1:
336 case SM5502_MUIC_ADC_RESERVED_ACC_2:
337 case SM5502_MUIC_ADC_RESERVED_ACC_3:
338 case SM5502_MUIC_ADC_RESERVED_ACC_4:
339 case SM5502_MUIC_ADC_RESERVED_ACC_5:
340 case SM5502_MUIC_ADC_AUDIO_TYPE2:
341 case SM5502_MUIC_ADC_PHONE_POWERED_DEV:
342 case SM5502_MUIC_ADC_TTY_CONVERTER:
343 case SM5502_MUIC_ADC_UART_CABLE:
344 case SM5502_MUIC_ADC_TYPE1_CHARGER:
345 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
346 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
347 case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE:
348 case SM5502_MUIC_ADC_TYPE2_CHARGER:
349 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
350 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
351 break;
352 case SM5502_MUIC_ADC_AUDIO_TYPE1:
353 /*
354 * Check whether cable type is
355 * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE
356 * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END
357 * by using Button event.
358 */
359 break;
360 case SM5502_MUIC_ADC_OPEN:
361 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
362 &dev_type1);
363 if (ret) {
364 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
365 return ret;
366 }
367
368 switch (dev_type1) {
369 case SM5502_REG_DEV_TYPE1_USB_SDP_MASK:
370 cable_type = SM5502_MUIC_ADC_OPEN_USB;
371 break;
372 case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK:
373 cable_type = SM5502_MUIC_ADC_OPEN_TA;
374 break;
375 case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
376 cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG;
377 break;
378 default:
379 dev_dbg(info->dev,
Chanwoo Choi34825e52015-03-07 01:41:36 +0900380 "cannot identify the cable type: adc(0x%x)\n",
381 adc);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900382 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000383 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900384 break;
385 default:
386 dev_err(info->dev,
387 "failed to identify the cable type: adc(0x%x)\n", adc);
388 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000389 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900390
391 return cable_type;
392}
393
394static int sm5502_muic_cable_handler(struct sm5502_muic_info *info,
395 bool attached)
396{
397 static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900398 unsigned int cable_type = SM5502_MUIC_ADC_GROUND;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900399 unsigned int con_sw = DM_DP_SWITCH_OPEN;
400 unsigned int vbus_sw = VBUSIN_SWITCH_OPEN;
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900401 unsigned int id;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900402 int ret;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900403
Chanwoo Choi914b8812014-05-22 14:06:33 +0900404 /* Get the type of attached or detached cable */
405 if (attached)
406 cable_type = sm5502_muic_get_cable_type(info);
Chanwoo Choifbae30d2014-08-12 10:15:39 +0900407 else
Chanwoo Choi914b8812014-05-22 14:06:33 +0900408 cable_type = prev_cable_type;
409 prev_cable_type = cable_type;
410
411 switch (cable_type) {
412 case SM5502_MUIC_ADC_OPEN_USB:
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900413 id = EXTCON_USB;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900414 con_sw = DM_DP_SWITCH_USB;
415 vbus_sw = VBUSIN_SWITCH_VBUSOUT_WITH_USB;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900416 break;
417 case SM5502_MUIC_ADC_OPEN_TA:
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900418 id = EXTCON_CHG_USB_DCP;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900419 con_sw = DM_DP_SWITCH_OPEN;
420 vbus_sw = VBUSIN_SWITCH_VBUSOUT;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900421 break;
Nikita Travkine3f60322021-01-19 18:33:47 +0500422 case SM5502_MUIC_ADC_GROUND_USB_OTG:
Chanwoo Choi914b8812014-05-22 14:06:33 +0900423 case SM5502_MUIC_ADC_OPEN_USB_OTG:
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900424 id = EXTCON_USB_HOST;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900425 con_sw = DM_DP_SWITCH_USB;
426 vbus_sw = VBUSIN_SWITCH_OPEN;
Chanwoo Choie1954452014-05-28 14:21:55 +0900427 break;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900428 default:
429 dev_dbg(info->dev,
430 "cannot handle this cable_type (0x%x)\n", cable_type);
431 return 0;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000432 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900433
Chanwoo Choia75fed22014-05-28 15:35:29 +0900434 /* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */
435 ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached);
436 if (ret < 0)
437 return ret;
438
439 /* Change the state of external accessory */
Chanwoo Choi8670b452016-08-16 15:55:34 +0900440 extcon_set_state_sync(info->edev, id, attached);
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +0900441 if (id == EXTCON_USB)
Chanwoo Choi8670b452016-08-16 15:55:34 +0900442 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +0900443 attached);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900444
445 return 0;
446}
447
448static void sm5502_muic_irq_work(struct work_struct *work)
449{
450 struct sm5502_muic_info *info = container_of(work,
451 struct sm5502_muic_info, irq_work);
452 int ret = 0;
453
454 if (!info->edev)
455 return;
456
457 mutex_lock(&info->mutex);
458
459 /* Detect attached or detached cables */
460 if (info->irq_attach) {
461 ret = sm5502_muic_cable_handler(info, true);
462 info->irq_attach = false;
463 }
464 if (info->irq_detach) {
465 ret = sm5502_muic_cable_handler(info, false);
466 info->irq_detach = false;
467 }
468
469 if (ret < 0)
470 dev_err(info->dev, "failed to handle MUIC interrupt\n");
471
472 mutex_unlock(&info->mutex);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900473}
474
475/*
476 * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0.
477 * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
478 */
479static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type)
480{
481 switch (irq_type) {
482 case SM5502_IRQ_INT1_ATTACH:
483 info->irq_attach = true;
484 break;
485 case SM5502_IRQ_INT1_DETACH:
486 info->irq_detach = true;
487 break;
488 case SM5502_IRQ_INT1_KP:
489 case SM5502_IRQ_INT1_LKP:
490 case SM5502_IRQ_INT1_LKR:
491 case SM5502_IRQ_INT1_OVP_EVENT:
492 case SM5502_IRQ_INT1_OCP_EVENT:
493 case SM5502_IRQ_INT1_OVP_OCP_DIS:
494 case SM5502_IRQ_INT2_VBUS_DET:
495 case SM5502_IRQ_INT2_REV_ACCE:
496 case SM5502_IRQ_INT2_ADC_CHG:
497 case SM5502_IRQ_INT2_STUCK_KEY:
498 case SM5502_IRQ_INT2_STUCK_KEY_RCV:
499 case SM5502_IRQ_INT2_MHL:
500 default:
501 break;
502 }
503
504 return 0;
505}
506
507static irqreturn_t sm5502_muic_irq_handler(int irq, void *data)
508{
509 struct sm5502_muic_info *info = data;
510 int i, irq_type = -1, ret;
511
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200512 for (i = 0; i < info->type->num_muic_irqs; i++)
513 if (irq == info->type->muic_irqs[i].virq)
514 irq_type = info->type->muic_irqs[i].irq;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900515
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200516 ret = info->type->parse_irq(info, irq_type);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900517 if (ret < 0) {
518 dev_warn(info->dev, "cannot handle is interrupt:%d\n",
519 irq_type);
520 return IRQ_HANDLED;
521 }
522 schedule_work(&info->irq_work);
523
524 return IRQ_HANDLED;
525}
526
Chanwoo Choie1954452014-05-28 14:21:55 +0900527static void sm5502_muic_detect_cable_wq(struct work_struct *work)
528{
529 struct sm5502_muic_info *info = container_of(to_delayed_work(work),
530 struct sm5502_muic_info, wq_detcable);
531 int ret;
532
533 /* Notify the state of connector cable or not */
534 ret = sm5502_muic_cable_handler(info, true);
535 if (ret < 0)
536 dev_warn(info->dev, "failed to detect cable state\n");
537}
538
Chanwoo Choi914b8812014-05-22 14:06:33 +0900539static void sm5502_init_dev_type(struct sm5502_muic_info *info)
540{
541 unsigned int reg_data, vendor_id, version_id;
542 int i, ret;
543
544 /* To test I2C, Print version_id and vendor_id of SM5502 */
545 ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, &reg_data);
546 if (ret) {
547 dev_err(info->dev,
548 "failed to read DEVICE_ID register: %d\n", ret);
549 return;
550 }
551
552 vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >>
553 SM5502_REG_DEVICE_ID_VENDOR_SHIFT);
554 version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >>
555 SM5502_REG_DEVICE_ID_VERSION_SHIFT);
556
557 dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
558 version_id, vendor_id);
559
560 /* Initiazle the register of SM5502 device to bring-up */
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200561 for (i = 0; i < info->type->num_reg_data; i++) {
Chanwoo Choi914b8812014-05-22 14:06:33 +0900562 unsigned int val = 0;
563
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200564 if (!info->type->reg_data[i].invert)
565 val |= ~info->type->reg_data[i].val;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900566 else
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200567 val = info->type->reg_data[i].val;
568 regmap_write(info->regmap, info->type->reg_data[i].reg, val);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900569 }
570}
571
Stephan Gerholdb1b76af2021-05-31 15:34:34 +0200572static int sm5022_muic_i2c_probe(struct i2c_client *i2c)
Chanwoo Choi914b8812014-05-22 14:06:33 +0900573{
574 struct device_node *np = i2c->dev.of_node;
575 struct sm5502_muic_info *info;
576 int i, ret, irq_flags;
577
578 if (!np)
579 return -EINVAL;
580
581 info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
582 if (!info)
583 return -ENOMEM;
584 i2c_set_clientdata(i2c, info);
585
586 info->dev = &i2c->dev;
587 info->i2c = i2c;
588 info->irq = i2c->irq;
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200589 info->type = device_get_match_data(info->dev);
590 if (!info->type)
591 return -EINVAL;
592 if (!info->type->parse_irq) {
593 dev_err(info->dev, "parse_irq missing in struct sm5502_type\n");
594 return -EINVAL;
595 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900596
597 mutex_init(&info->mutex);
598
599 INIT_WORK(&info->irq_work, sm5502_muic_irq_work);
600
601 info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config);
602 if (IS_ERR(info->regmap)) {
603 ret = PTR_ERR(info->regmap);
604 dev_err(info->dev, "failed to allocate register map: %d\n",
605 ret);
606 return ret;
607 }
608
609 /* Support irq domain for SM5502 MUIC device */
610 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200611 ret = devm_regmap_add_irq_chip(info->dev, info->regmap, info->irq,
612 irq_flags, 0, info->type->irq_chip,
613 &info->irq_data);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900614 if (ret != 0) {
615 dev_err(info->dev, "failed to request IRQ %d: %d\n",
616 info->irq, ret);
617 return ret;
618 }
619
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200620 for (i = 0; i < info->type->num_muic_irqs; i++) {
621 struct muic_irq *muic_irq = &info->type->muic_irqs[i];
Andrzej Hajda363b3892015-09-24 16:00:21 +0200622 int virq = 0;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900623
624 virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
625 if (virq <= 0)
626 return -EINVAL;
627 muic_irq->virq = virq;
628
629 ret = devm_request_threaded_irq(info->dev, virq, NULL,
630 sm5502_muic_irq_handler,
Vasyl Gomonovych005ad182019-07-19 18:28:06 +0200631 IRQF_NO_SUSPEND | IRQF_ONESHOT,
Chanwoo Choi914b8812014-05-22 14:06:33 +0900632 muic_irq->name, info);
633 if (ret) {
Chanwoo Choifbae30d2014-08-12 10:15:39 +0900634 dev_err(info->dev,
635 "failed: irq request (IRQ: %d, error :%d)\n",
636 muic_irq->irq, ret);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900637 return ret;
638 }
639 }
640
641 /* Allocate extcon device */
642 info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable);
643 if (IS_ERR(info->edev)) {
644 dev_err(info->dev, "failed to allocate memory for extcon\n");
645 return -ENOMEM;
646 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900647
648 /* Register extcon device */
649 ret = devm_extcon_dev_register(info->dev, info->edev);
650 if (ret) {
651 dev_err(info->dev, "failed to register extcon device\n");
652 return ret;
653 }
654
Chanwoo Choie1954452014-05-28 14:21:55 +0900655 /*
656 * Detect accessory after completing the initialization of platform
657 *
658 * - Use delayed workqueue to detect cable state and then
659 * notify cable state to notifiee/platform through uevent.
660 * After completing the booting of platform, the extcon provider
661 * driver should notify cable state to upper layer.
662 */
663 INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
664 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
665 msecs_to_jiffies(DELAY_MS_DEFAULT));
666
Chanwoo Choi914b8812014-05-22 14:06:33 +0900667 /* Initialize SM5502 device and print vendor id and version id */
668 sm5502_init_dev_type(info);
669
670 return 0;
671}
672
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200673static const struct sm5502_type sm5502_data = {
674 .muic_irqs = sm5502_muic_irqs,
675 .num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs),
676 .irq_chip = &sm5502_muic_irq_chip,
677 .reg_data = sm5502_reg_data,
678 .num_reg_data = ARRAY_SIZE(sm5502_reg_data),
679 .parse_irq = sm5502_parse_irq,
680};
681
Chanwoo Choi34825e52015-03-07 01:41:36 +0900682static const struct of_device_id sm5502_dt_match[] = {
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200683 { .compatible = "siliconmitus,sm5502-muic", .data = &sm5502_data },
Chanwoo Choi914b8812014-05-22 14:06:33 +0900684 { },
685};
Javier Martinez Canillasff612f92015-08-25 08:31:15 +0200686MODULE_DEVICE_TABLE(of, sm5502_dt_match);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900687
688#ifdef CONFIG_PM_SLEEP
689static int sm5502_muic_suspend(struct device *dev)
690{
Geliang Tangd5859342015-12-28 23:00:15 +0800691 struct i2c_client *i2c = to_i2c_client(dev);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900692 struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
693
694 enable_irq_wake(info->irq);
695
696 return 0;
697}
698
699static int sm5502_muic_resume(struct device *dev)
700{
Geliang Tangd5859342015-12-28 23:00:15 +0800701 struct i2c_client *i2c = to_i2c_client(dev);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900702 struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
703
704 disable_irq_wake(info->irq);
705
706 return 0;
707}
708#endif
709
710static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops,
711 sm5502_muic_suspend, sm5502_muic_resume);
712
713static const struct i2c_device_id sm5502_i2c_id[] = {
Stephan Gerholdf33c0562021-06-03 10:52:21 +0200714 { "sm5502", (kernel_ulong_t)&sm5502_data },
Chanwoo Choi914b8812014-05-22 14:06:33 +0900715 { }
716};
717MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id);
718
719static struct i2c_driver sm5502_muic_i2c_driver = {
720 .driver = {
721 .name = "sm5502",
Chanwoo Choi914b8812014-05-22 14:06:33 +0900722 .pm = &sm5502_muic_pm_ops,
723 .of_match_table = sm5502_dt_match,
724 },
Stephan Gerholdb1b76af2021-05-31 15:34:34 +0200725 .probe_new = sm5022_muic_i2c_probe,
Chanwoo Choi914b8812014-05-22 14:06:33 +0900726 .id_table = sm5502_i2c_id,
727};
728
729static int __init sm5502_muic_i2c_init(void)
730{
731 return i2c_add_driver(&sm5502_muic_i2c_driver);
732}
733subsys_initcall(sm5502_muic_i2c_init);
734
735MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver");
736MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
737MODULE_LICENSE("GPL");