blob: db41d1c58efd56a1e9e14b88f2a7d8471d2dab8b [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
43 struct regmap_irq_chip_data *irq_data;
44 struct muic_irq *muic_irqs;
45 unsigned int num_muic_irqs;
46 int irq;
47 bool irq_attach;
48 bool irq_detach;
49 struct work_struct irq_work;
50
51 struct reg_data *reg_data;
52 unsigned int num_reg_data;
53
54 struct mutex mutex;
Chanwoo Choie1954452014-05-28 14:21:55 +090055
56 /*
57 * Use delayed workqueue to detect cable state and then
58 * notify cable state to notifiee/platform through uevent.
59 * After completing the booting of platform, the extcon provider
60 * driver should notify cable state to upper layer.
61 */
62 struct delayed_work wq_detcable;
Chanwoo Choi914b8812014-05-22 14:06:33 +090063};
64
65/* Default value of SM5502 register to bring up MUIC device. */
66static struct reg_data sm5502_reg_data[] = {
67 {
Stephan Gerhold69426352019-10-10 17:47:20 +020068 .reg = SM5502_REG_RESET,
69 .val = SM5502_REG_RESET_MASK,
70 .invert = true,
71 }, {
Chanwoo Choi914b8812014-05-22 14:06:33 +090072 .reg = SM5502_REG_CONTROL,
73 .val = SM5502_REG_CONTROL_MASK_INT_MASK,
74 .invert = false,
75 }, {
76 .reg = SM5502_REG_INTMASK1,
77 .val = SM5502_REG_INTM1_KP_MASK
78 | SM5502_REG_INTM1_LKP_MASK
79 | SM5502_REG_INTM1_LKR_MASK,
80 .invert = true,
81 }, {
82 .reg = SM5502_REG_INTMASK2,
83 .val = SM5502_REG_INTM2_VBUS_DET_MASK
84 | SM5502_REG_INTM2_REV_ACCE_MASK
85 | SM5502_REG_INTM2_ADC_CHG_MASK
86 | SM5502_REG_INTM2_STUCK_KEY_MASK
87 | SM5502_REG_INTM2_STUCK_KEY_RCV_MASK
88 | SM5502_REG_INTM2_MHL_MASK,
89 .invert = true,
90 },
91 { }
92};
93
94/* List of detectable cables */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090095static const unsigned int sm5502_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090096 EXTCON_USB,
97 EXTCON_USB_HOST,
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +090098 EXTCON_CHG_USB_SDP,
Chanwoo Choi11eecf92015-10-03 14:15:13 +090099 EXTCON_CHG_USB_DCP,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900100 EXTCON_NONE,
Chanwoo Choi914b8812014-05-22 14:06:33 +0900101};
102
103/* Define supported accessory type */
104enum sm5502_muic_acc_type {
105 SM5502_MUIC_ADC_GROUND = 0x0,
106 SM5502_MUIC_ADC_SEND_END_BUTTON,
107 SM5502_MUIC_ADC_REMOTE_S1_BUTTON,
108 SM5502_MUIC_ADC_REMOTE_S2_BUTTON,
109 SM5502_MUIC_ADC_REMOTE_S3_BUTTON,
110 SM5502_MUIC_ADC_REMOTE_S4_BUTTON,
111 SM5502_MUIC_ADC_REMOTE_S5_BUTTON,
112 SM5502_MUIC_ADC_REMOTE_S6_BUTTON,
113 SM5502_MUIC_ADC_REMOTE_S7_BUTTON,
114 SM5502_MUIC_ADC_REMOTE_S8_BUTTON,
115 SM5502_MUIC_ADC_REMOTE_S9_BUTTON,
116 SM5502_MUIC_ADC_REMOTE_S10_BUTTON,
117 SM5502_MUIC_ADC_REMOTE_S11_BUTTON,
118 SM5502_MUIC_ADC_REMOTE_S12_BUTTON,
119 SM5502_MUIC_ADC_RESERVED_ACC_1,
120 SM5502_MUIC_ADC_RESERVED_ACC_2,
121 SM5502_MUIC_ADC_RESERVED_ACC_3,
122 SM5502_MUIC_ADC_RESERVED_ACC_4,
123 SM5502_MUIC_ADC_RESERVED_ACC_5,
124 SM5502_MUIC_ADC_AUDIO_TYPE2,
125 SM5502_MUIC_ADC_PHONE_POWERED_DEV,
126 SM5502_MUIC_ADC_TTY_CONVERTER,
127 SM5502_MUIC_ADC_UART_CABLE,
128 SM5502_MUIC_ADC_TYPE1_CHARGER,
129 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
130 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
131 SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE,
132 SM5502_MUIC_ADC_TYPE2_CHARGER,
133 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
134 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
135 SM5502_MUIC_ADC_AUDIO_TYPE1,
136 SM5502_MUIC_ADC_OPEN = 0x1f,
137
Srikant Ritoliaaf57fa42016-12-07 17:29:39 +0530138 /*
139 * The below accessories have same ADC value (0x1f or 0x1e).
140 * So, Device type1 is used to separate specific accessory.
141 */
Chanwoo Choi914b8812014-05-22 14:06:33 +0900142 /* |---------|--ADC| */
143 /* | [7:5]|[4:0]| */
144 SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* | 001|11110| */
145 SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e, /* | 010|11110| */
146 /* |Dev Type1|--ADC| */
Nikita Travkine3f60322021-01-19 18:33:47 +0500147 SM5502_MUIC_ADC_GROUND_USB_OTG = 0x80, /* | 100|00000| */
Chanwoo Choi914b8812014-05-22 14:06:33 +0900148 SM5502_MUIC_ADC_OPEN_USB = 0x5f, /* | 010|11111| */
149 SM5502_MUIC_ADC_OPEN_TA = 0xdf, /* | 110|11111| */
150 SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff, /* | 111|11111| */
151};
152
153/* List of supported interrupt for SM5502 */
154static struct muic_irq sm5502_muic_irqs[] = {
155 { SM5502_IRQ_INT1_ATTACH, "muic-attach" },
156 { SM5502_IRQ_INT1_DETACH, "muic-detach" },
157 { SM5502_IRQ_INT1_KP, "muic-kp" },
158 { SM5502_IRQ_INT1_LKP, "muic-lkp" },
159 { SM5502_IRQ_INT1_LKR, "muic-lkr" },
160 { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" },
161 { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" },
162 { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" },
163 { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" },
164 { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" },
165 { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" },
166 { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" },
167 { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" },
168 { SM5502_IRQ_INT2_MHL, "muic-mhl" },
169};
170
171/* Define interrupt list of SM5502 to register regmap_irq */
172static const struct regmap_irq sm5502_irqs[] = {
173 /* INT1 interrupts */
174 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, },
175 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, },
176 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, },
177 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, },
178 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, },
179 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, },
180 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, },
181 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, },
182
183 /* INT2 interrupts */
184 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,},
185 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, },
186 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, },
187 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, },
188 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, },
189 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, },
190};
191
192static const struct regmap_irq_chip sm5502_muic_irq_chip = {
193 .name = "sm5502",
194 .status_base = SM5502_REG_INT1,
195 .mask_base = SM5502_REG_INTMASK1,
196 .mask_invert = false,
197 .num_regs = 2,
198 .irqs = sm5502_irqs,
199 .num_irqs = ARRAY_SIZE(sm5502_irqs),
200};
201
202/* Define regmap configuration of SM5502 for I2C communication */
203static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg)
204{
205 switch (reg) {
206 case SM5502_REG_INTMASK1:
207 case SM5502_REG_INTMASK2:
208 return true;
209 default:
210 break;
211 }
212 return false;
213}
214
215static const struct regmap_config sm5502_muic_regmap_config = {
216 .reg_bits = 8,
217 .val_bits = 8,
218 .volatile_reg = sm5502_muic_volatile_reg,
219 .max_register = SM5502_REG_END,
220};
221
Chanwoo Choia75fed22014-05-28 15:35:29 +0900222/* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
223static int sm5502_muic_set_path(struct sm5502_muic_info *info,
224 unsigned int con_sw, unsigned int vbus_sw,
225 bool attached)
226{
227 int ret;
228
229 if (!attached) {
230 con_sw = DM_DP_SWITCH_OPEN;
231 vbus_sw = VBUSIN_SWITCH_OPEN;
232 }
233
234 switch (con_sw) {
235 case DM_DP_SWITCH_OPEN:
236 case DM_DP_SWITCH_USB:
237 case DM_DP_SWITCH_AUDIO:
238 case DM_DP_SWITCH_UART:
239 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
240 SM5502_REG_MANUAL_SW1_DP_MASK |
241 SM5502_REG_MANUAL_SW1_DM_MASK,
242 con_sw);
243 if (ret < 0) {
244 dev_err(info->dev,
245 "cannot update DM_CON/DP_CON switch\n");
246 return ret;
247 }
248 break;
249 default:
250 dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
251 con_sw);
252 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000253 }
Chanwoo Choia75fed22014-05-28 15:35:29 +0900254
255 switch (vbus_sw) {
256 case VBUSIN_SWITCH_OPEN:
257 case VBUSIN_SWITCH_VBUSOUT:
258 case VBUSIN_SWITCH_MIC:
259 case VBUSIN_SWITCH_VBUSOUT_WITH_USB:
260 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
261 SM5502_REG_MANUAL_SW1_VBUSIN_MASK,
262 vbus_sw);
263 if (ret < 0) {
264 dev_err(info->dev,
265 "cannot update VBUSIN switch\n");
266 return ret;
267 }
268 break;
269 default:
270 dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw);
271 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000272 }
Chanwoo Choia75fed22014-05-28 15:35:29 +0900273
274 return 0;
275}
276
Chanwoo Choi914b8812014-05-22 14:06:33 +0900277/* Return cable type of attached or detached accessories */
278static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info)
279{
Colin Ian Kingddd1bbb2019-10-25 14:12:27 +0100280 unsigned int cable_type, adc, dev_type1;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900281 int ret;
282
283 /* Read ADC value according to external cable or button */
284 ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc);
285 if (ret) {
286 dev_err(info->dev, "failed to read ADC register\n");
287 return ret;
288 }
289
290 /*
291 * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't
292 * connected with to MUIC device.
293 */
Chanwoo Choi0ccc7952014-07-30 15:39:02 +0900294 cable_type = adc & SM5502_REG_ADC_MASK;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900295
296 switch (cable_type) {
297 case SM5502_MUIC_ADC_GROUND:
Nikita Travkine3f60322021-01-19 18:33:47 +0500298 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
299 &dev_type1);
300 if (ret) {
301 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
302 return ret;
303 }
304
305 switch (dev_type1) {
306 case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
307 cable_type = SM5502_MUIC_ADC_GROUND_USB_OTG;
308 break;
309 default:
310 dev_dbg(info->dev,
311 "cannot identify the cable type: adc(0x%x), dev_type1(0x%x)\n",
312 adc, dev_type1);
313 return -EINVAL;
314 }
315 break;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900316 case SM5502_MUIC_ADC_SEND_END_BUTTON:
317 case SM5502_MUIC_ADC_REMOTE_S1_BUTTON:
318 case SM5502_MUIC_ADC_REMOTE_S2_BUTTON:
319 case SM5502_MUIC_ADC_REMOTE_S3_BUTTON:
320 case SM5502_MUIC_ADC_REMOTE_S4_BUTTON:
321 case SM5502_MUIC_ADC_REMOTE_S5_BUTTON:
322 case SM5502_MUIC_ADC_REMOTE_S6_BUTTON:
323 case SM5502_MUIC_ADC_REMOTE_S7_BUTTON:
324 case SM5502_MUIC_ADC_REMOTE_S8_BUTTON:
325 case SM5502_MUIC_ADC_REMOTE_S9_BUTTON:
326 case SM5502_MUIC_ADC_REMOTE_S10_BUTTON:
327 case SM5502_MUIC_ADC_REMOTE_S11_BUTTON:
328 case SM5502_MUIC_ADC_REMOTE_S12_BUTTON:
329 case SM5502_MUIC_ADC_RESERVED_ACC_1:
330 case SM5502_MUIC_ADC_RESERVED_ACC_2:
331 case SM5502_MUIC_ADC_RESERVED_ACC_3:
332 case SM5502_MUIC_ADC_RESERVED_ACC_4:
333 case SM5502_MUIC_ADC_RESERVED_ACC_5:
334 case SM5502_MUIC_ADC_AUDIO_TYPE2:
335 case SM5502_MUIC_ADC_PHONE_POWERED_DEV:
336 case SM5502_MUIC_ADC_TTY_CONVERTER:
337 case SM5502_MUIC_ADC_UART_CABLE:
338 case SM5502_MUIC_ADC_TYPE1_CHARGER:
339 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
340 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
341 case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE:
342 case SM5502_MUIC_ADC_TYPE2_CHARGER:
343 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
344 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
345 break;
346 case SM5502_MUIC_ADC_AUDIO_TYPE1:
347 /*
348 * Check whether cable type is
349 * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE
350 * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END
351 * by using Button event.
352 */
353 break;
354 case SM5502_MUIC_ADC_OPEN:
355 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
356 &dev_type1);
357 if (ret) {
358 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
359 return ret;
360 }
361
362 switch (dev_type1) {
363 case SM5502_REG_DEV_TYPE1_USB_SDP_MASK:
364 cable_type = SM5502_MUIC_ADC_OPEN_USB;
365 break;
366 case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK:
367 cable_type = SM5502_MUIC_ADC_OPEN_TA;
368 break;
369 case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
370 cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG;
371 break;
372 default:
373 dev_dbg(info->dev,
Chanwoo Choi34825e52015-03-07 01:41:36 +0900374 "cannot identify the cable type: adc(0x%x)\n",
375 adc);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900376 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000377 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900378 break;
379 default:
380 dev_err(info->dev,
381 "failed to identify the cable type: adc(0x%x)\n", adc);
382 return -EINVAL;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000383 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900384
385 return cable_type;
386}
387
388static int sm5502_muic_cable_handler(struct sm5502_muic_info *info,
389 bool attached)
390{
391 static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900392 unsigned int cable_type = SM5502_MUIC_ADC_GROUND;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900393 unsigned int con_sw = DM_DP_SWITCH_OPEN;
394 unsigned int vbus_sw = VBUSIN_SWITCH_OPEN;
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900395 unsigned int id;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900396 int ret;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900397
Chanwoo Choi914b8812014-05-22 14:06:33 +0900398 /* Get the type of attached or detached cable */
399 if (attached)
400 cable_type = sm5502_muic_get_cable_type(info);
Chanwoo Choifbae30d2014-08-12 10:15:39 +0900401 else
Chanwoo Choi914b8812014-05-22 14:06:33 +0900402 cable_type = prev_cable_type;
403 prev_cable_type = cable_type;
404
405 switch (cable_type) {
406 case SM5502_MUIC_ADC_OPEN_USB:
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900407 id = EXTCON_USB;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900408 con_sw = DM_DP_SWITCH_USB;
409 vbus_sw = VBUSIN_SWITCH_VBUSOUT_WITH_USB;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900410 break;
411 case SM5502_MUIC_ADC_OPEN_TA:
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900412 id = EXTCON_CHG_USB_DCP;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900413 con_sw = DM_DP_SWITCH_OPEN;
414 vbus_sw = VBUSIN_SWITCH_VBUSOUT;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900415 break;
Nikita Travkine3f60322021-01-19 18:33:47 +0500416 case SM5502_MUIC_ADC_GROUND_USB_OTG:
Chanwoo Choi914b8812014-05-22 14:06:33 +0900417 case SM5502_MUIC_ADC_OPEN_USB_OTG:
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900418 id = EXTCON_USB_HOST;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900419 con_sw = DM_DP_SWITCH_USB;
420 vbus_sw = VBUSIN_SWITCH_OPEN;
Chanwoo Choie1954452014-05-28 14:21:55 +0900421 break;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900422 default:
423 dev_dbg(info->dev,
424 "cannot handle this cable_type (0x%x)\n", cable_type);
425 return 0;
Xu Wang2ddf50a2019-12-13 09:48:34 +0000426 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900427
Chanwoo Choia75fed22014-05-28 15:35:29 +0900428 /* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */
429 ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached);
430 if (ret < 0)
431 return ret;
432
433 /* Change the state of external accessory */
Chanwoo Choi8670b452016-08-16 15:55:34 +0900434 extcon_set_state_sync(info->edev, id, attached);
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +0900435 if (id == EXTCON_USB)
Chanwoo Choi8670b452016-08-16 15:55:34 +0900436 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +0900437 attached);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900438
439 return 0;
440}
441
442static void sm5502_muic_irq_work(struct work_struct *work)
443{
444 struct sm5502_muic_info *info = container_of(work,
445 struct sm5502_muic_info, irq_work);
446 int ret = 0;
447
448 if (!info->edev)
449 return;
450
451 mutex_lock(&info->mutex);
452
453 /* Detect attached or detached cables */
454 if (info->irq_attach) {
455 ret = sm5502_muic_cable_handler(info, true);
456 info->irq_attach = false;
457 }
458 if (info->irq_detach) {
459 ret = sm5502_muic_cable_handler(info, false);
460 info->irq_detach = false;
461 }
462
463 if (ret < 0)
464 dev_err(info->dev, "failed to handle MUIC interrupt\n");
465
466 mutex_unlock(&info->mutex);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900467}
468
469/*
470 * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0.
471 * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
472 */
473static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type)
474{
475 switch (irq_type) {
476 case SM5502_IRQ_INT1_ATTACH:
477 info->irq_attach = true;
478 break;
479 case SM5502_IRQ_INT1_DETACH:
480 info->irq_detach = true;
481 break;
482 case SM5502_IRQ_INT1_KP:
483 case SM5502_IRQ_INT1_LKP:
484 case SM5502_IRQ_INT1_LKR:
485 case SM5502_IRQ_INT1_OVP_EVENT:
486 case SM5502_IRQ_INT1_OCP_EVENT:
487 case SM5502_IRQ_INT1_OVP_OCP_DIS:
488 case SM5502_IRQ_INT2_VBUS_DET:
489 case SM5502_IRQ_INT2_REV_ACCE:
490 case SM5502_IRQ_INT2_ADC_CHG:
491 case SM5502_IRQ_INT2_STUCK_KEY:
492 case SM5502_IRQ_INT2_STUCK_KEY_RCV:
493 case SM5502_IRQ_INT2_MHL:
494 default:
495 break;
496 }
497
498 return 0;
499}
500
501static irqreturn_t sm5502_muic_irq_handler(int irq, void *data)
502{
503 struct sm5502_muic_info *info = data;
504 int i, irq_type = -1, ret;
505
506 for (i = 0; i < info->num_muic_irqs; i++)
507 if (irq == info->muic_irqs[i].virq)
508 irq_type = info->muic_irqs[i].irq;
509
510 ret = sm5502_parse_irq(info, irq_type);
511 if (ret < 0) {
512 dev_warn(info->dev, "cannot handle is interrupt:%d\n",
513 irq_type);
514 return IRQ_HANDLED;
515 }
516 schedule_work(&info->irq_work);
517
518 return IRQ_HANDLED;
519}
520
Chanwoo Choie1954452014-05-28 14:21:55 +0900521static void sm5502_muic_detect_cable_wq(struct work_struct *work)
522{
523 struct sm5502_muic_info *info = container_of(to_delayed_work(work),
524 struct sm5502_muic_info, wq_detcable);
525 int ret;
526
527 /* Notify the state of connector cable or not */
528 ret = sm5502_muic_cable_handler(info, true);
529 if (ret < 0)
530 dev_warn(info->dev, "failed to detect cable state\n");
531}
532
Chanwoo Choi914b8812014-05-22 14:06:33 +0900533static void sm5502_init_dev_type(struct sm5502_muic_info *info)
534{
535 unsigned int reg_data, vendor_id, version_id;
536 int i, ret;
537
538 /* To test I2C, Print version_id and vendor_id of SM5502 */
539 ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, &reg_data);
540 if (ret) {
541 dev_err(info->dev,
542 "failed to read DEVICE_ID register: %d\n", ret);
543 return;
544 }
545
546 vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >>
547 SM5502_REG_DEVICE_ID_VENDOR_SHIFT);
548 version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >>
549 SM5502_REG_DEVICE_ID_VERSION_SHIFT);
550
551 dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
552 version_id, vendor_id);
553
554 /* Initiazle the register of SM5502 device to bring-up */
555 for (i = 0; i < info->num_reg_data; i++) {
556 unsigned int val = 0;
557
558 if (!info->reg_data[i].invert)
559 val |= ~info->reg_data[i].val;
560 else
561 val = info->reg_data[i].val;
562 regmap_write(info->regmap, info->reg_data[i].reg, val);
563 }
564}
565
566static int sm5022_muic_i2c_probe(struct i2c_client *i2c,
567 const struct i2c_device_id *id)
568{
569 struct device_node *np = i2c->dev.of_node;
570 struct sm5502_muic_info *info;
571 int i, ret, irq_flags;
572
573 if (!np)
574 return -EINVAL;
575
576 info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
577 if (!info)
578 return -ENOMEM;
579 i2c_set_clientdata(i2c, info);
580
581 info->dev = &i2c->dev;
582 info->i2c = i2c;
583 info->irq = i2c->irq;
584 info->muic_irqs = sm5502_muic_irqs;
585 info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs);
586 info->reg_data = sm5502_reg_data;
587 info->num_reg_data = ARRAY_SIZE(sm5502_reg_data);
588
589 mutex_init(&info->mutex);
590
591 INIT_WORK(&info->irq_work, sm5502_muic_irq_work);
592
593 info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config);
594 if (IS_ERR(info->regmap)) {
595 ret = PTR_ERR(info->regmap);
596 dev_err(info->dev, "failed to allocate register map: %d\n",
597 ret);
598 return ret;
599 }
600
601 /* Support irq domain for SM5502 MUIC device */
602 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
603 ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0,
604 &sm5502_muic_irq_chip, &info->irq_data);
605 if (ret != 0) {
606 dev_err(info->dev, "failed to request IRQ %d: %d\n",
607 info->irq, ret);
608 return ret;
609 }
610
611 for (i = 0; i < info->num_muic_irqs; i++) {
612 struct muic_irq *muic_irq = &info->muic_irqs[i];
Andrzej Hajda363b3892015-09-24 16:00:21 +0200613 int virq = 0;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900614
615 virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
616 if (virq <= 0)
617 return -EINVAL;
618 muic_irq->virq = virq;
619
620 ret = devm_request_threaded_irq(info->dev, virq, NULL,
621 sm5502_muic_irq_handler,
Vasyl Gomonovych005ad182019-07-19 18:28:06 +0200622 IRQF_NO_SUSPEND | IRQF_ONESHOT,
Chanwoo Choi914b8812014-05-22 14:06:33 +0900623 muic_irq->name, info);
624 if (ret) {
Chanwoo Choifbae30d2014-08-12 10:15:39 +0900625 dev_err(info->dev,
626 "failed: irq request (IRQ: %d, error :%d)\n",
627 muic_irq->irq, ret);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900628 return ret;
629 }
630 }
631
632 /* Allocate extcon device */
633 info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable);
634 if (IS_ERR(info->edev)) {
635 dev_err(info->dev, "failed to allocate memory for extcon\n");
636 return -ENOMEM;
637 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900638
639 /* Register extcon device */
640 ret = devm_extcon_dev_register(info->dev, info->edev);
641 if (ret) {
642 dev_err(info->dev, "failed to register extcon device\n");
643 return ret;
644 }
645
Chanwoo Choie1954452014-05-28 14:21:55 +0900646 /*
647 * Detect accessory after completing the initialization of platform
648 *
649 * - Use delayed workqueue to detect cable state and then
650 * notify cable state to notifiee/platform through uevent.
651 * After completing the booting of platform, the extcon provider
652 * driver should notify cable state to upper layer.
653 */
654 INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
655 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
656 msecs_to_jiffies(DELAY_MS_DEFAULT));
657
Chanwoo Choi914b8812014-05-22 14:06:33 +0900658 /* Initialize SM5502 device and print vendor id and version id */
659 sm5502_init_dev_type(info);
660
661 return 0;
662}
663
664static int sm5502_muic_i2c_remove(struct i2c_client *i2c)
665{
666 struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
667
668 regmap_del_irq_chip(info->irq, info->irq_data);
669
670 return 0;
671}
672
Chanwoo Choi34825e52015-03-07 01:41:36 +0900673static const struct of_device_id sm5502_dt_match[] = {
Chanwoo Choi914b8812014-05-22 14:06:33 +0900674 { .compatible = "siliconmitus,sm5502-muic" },
675 { },
676};
Javier Martinez Canillasff612f92015-08-25 08:31:15 +0200677MODULE_DEVICE_TABLE(of, sm5502_dt_match);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900678
679#ifdef CONFIG_PM_SLEEP
680static int sm5502_muic_suspend(struct device *dev)
681{
Geliang Tangd5859342015-12-28 23:00:15 +0800682 struct i2c_client *i2c = to_i2c_client(dev);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900683 struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
684
685 enable_irq_wake(info->irq);
686
687 return 0;
688}
689
690static int sm5502_muic_resume(struct device *dev)
691{
Geliang Tangd5859342015-12-28 23:00:15 +0800692 struct i2c_client *i2c = to_i2c_client(dev);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900693 struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
694
695 disable_irq_wake(info->irq);
696
697 return 0;
698}
699#endif
700
701static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops,
702 sm5502_muic_suspend, sm5502_muic_resume);
703
704static const struct i2c_device_id sm5502_i2c_id[] = {
705 { "sm5502", TYPE_SM5502 },
706 { }
707};
708MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id);
709
710static struct i2c_driver sm5502_muic_i2c_driver = {
711 .driver = {
712 .name = "sm5502",
Chanwoo Choi914b8812014-05-22 14:06:33 +0900713 .pm = &sm5502_muic_pm_ops,
714 .of_match_table = sm5502_dt_match,
715 },
716 .probe = sm5022_muic_i2c_probe,
717 .remove = sm5502_muic_i2c_remove,
718 .id_table = sm5502_i2c_id,
719};
720
721static int __init sm5502_muic_i2c_init(void)
722{
723 return i2c_add_driver(&sm5502_muic_i2c_driver);
724}
725subsys_initcall(sm5502_muic_i2c_init);
726
727MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver");
728MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
729MODULE_LICENSE("GPL");