blob: 9d2d8a6673c887d6f687c64ff106f8b34215aae1 [file] [log] [blame]
Chanwoo Choi914b8812014-05-22 14:06:33 +09001/*
2 * extcon-sm5502.c - Silicon Mitus SM5502 extcon drvier to support USB switches
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd
5 * Author: Chanwoo Choi <cw00.choi@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
Chanwoo Choi914b8812014-05-22 14:06:33 +090011 */
12
13#include <linux/err.h>
14#include <linux/i2c.h>
Chanwoo Choi914b8812014-05-22 14:06:33 +090015#include <linux/interrupt.h>
16#include <linux/irqdomain.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/regmap.h>
21#include <linux/slab.h>
22#include <linux/extcon.h>
Chanwoo Choica2a07e2014-07-31 16:32:46 +090023
24#include "extcon-sm5502.h"
Chanwoo Choi914b8812014-05-22 14:06:33 +090025
Chanwoo Choie1954452014-05-28 14:21:55 +090026#define DELAY_MS_DEFAULT 17000 /* unit: millisecond */
27
Chanwoo Choi914b8812014-05-22 14:06:33 +090028struct muic_irq {
29 unsigned int irq;
30 const char *name;
31 unsigned int virq;
32};
33
34struct reg_data {
35 u8 reg;
36 unsigned int val;
37 bool invert;
38};
39
40struct sm5502_muic_info {
41 struct device *dev;
42 struct extcon_dev *edev;
43
44 struct i2c_client *i2c;
45 struct regmap *regmap;
46
47 struct regmap_irq_chip_data *irq_data;
48 struct muic_irq *muic_irqs;
49 unsigned int num_muic_irqs;
50 int irq;
51 bool irq_attach;
52 bool irq_detach;
53 struct work_struct irq_work;
54
55 struct reg_data *reg_data;
56 unsigned int num_reg_data;
57
58 struct mutex mutex;
Chanwoo Choie1954452014-05-28 14:21:55 +090059
60 /*
61 * Use delayed workqueue to detect cable state and then
62 * notify cable state to notifiee/platform through uevent.
63 * After completing the booting of platform, the extcon provider
64 * driver should notify cable state to upper layer.
65 */
66 struct delayed_work wq_detcable;
Chanwoo Choi914b8812014-05-22 14:06:33 +090067};
68
69/* Default value of SM5502 register to bring up MUIC device. */
70static struct reg_data sm5502_reg_data[] = {
71 {
Stephan Gerhold684db992019-10-10 17:47:20 +020072 .reg = SM5502_REG_RESET,
73 .val = SM5502_REG_RESET_MASK,
74 .invert = true,
75 }, {
Chanwoo Choi914b8812014-05-22 14:06:33 +090076 .reg = SM5502_REG_CONTROL,
77 .val = SM5502_REG_CONTROL_MASK_INT_MASK,
78 .invert = false,
79 }, {
80 .reg = SM5502_REG_INTMASK1,
81 .val = SM5502_REG_INTM1_KP_MASK
82 | SM5502_REG_INTM1_LKP_MASK
83 | SM5502_REG_INTM1_LKR_MASK,
84 .invert = true,
85 }, {
86 .reg = SM5502_REG_INTMASK2,
87 .val = SM5502_REG_INTM2_VBUS_DET_MASK
88 | SM5502_REG_INTM2_REV_ACCE_MASK
89 | SM5502_REG_INTM2_ADC_CHG_MASK
90 | SM5502_REG_INTM2_STUCK_KEY_MASK
91 | SM5502_REG_INTM2_STUCK_KEY_RCV_MASK
92 | SM5502_REG_INTM2_MHL_MASK,
93 .invert = true,
94 },
95 { }
96};
97
98/* List of detectable cables */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090099static const unsigned int sm5502_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900100 EXTCON_USB,
101 EXTCON_USB_HOST,
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +0900102 EXTCON_CHG_USB_SDP,
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900103 EXTCON_CHG_USB_DCP,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900104 EXTCON_NONE,
Chanwoo Choi914b8812014-05-22 14:06:33 +0900105};
106
107/* Define supported accessory type */
108enum sm5502_muic_acc_type {
109 SM5502_MUIC_ADC_GROUND = 0x0,
110 SM5502_MUIC_ADC_SEND_END_BUTTON,
111 SM5502_MUIC_ADC_REMOTE_S1_BUTTON,
112 SM5502_MUIC_ADC_REMOTE_S2_BUTTON,
113 SM5502_MUIC_ADC_REMOTE_S3_BUTTON,
114 SM5502_MUIC_ADC_REMOTE_S4_BUTTON,
115 SM5502_MUIC_ADC_REMOTE_S5_BUTTON,
116 SM5502_MUIC_ADC_REMOTE_S6_BUTTON,
117 SM5502_MUIC_ADC_REMOTE_S7_BUTTON,
118 SM5502_MUIC_ADC_REMOTE_S8_BUTTON,
119 SM5502_MUIC_ADC_REMOTE_S9_BUTTON,
120 SM5502_MUIC_ADC_REMOTE_S10_BUTTON,
121 SM5502_MUIC_ADC_REMOTE_S11_BUTTON,
122 SM5502_MUIC_ADC_REMOTE_S12_BUTTON,
123 SM5502_MUIC_ADC_RESERVED_ACC_1,
124 SM5502_MUIC_ADC_RESERVED_ACC_2,
125 SM5502_MUIC_ADC_RESERVED_ACC_3,
126 SM5502_MUIC_ADC_RESERVED_ACC_4,
127 SM5502_MUIC_ADC_RESERVED_ACC_5,
128 SM5502_MUIC_ADC_AUDIO_TYPE2,
129 SM5502_MUIC_ADC_PHONE_POWERED_DEV,
130 SM5502_MUIC_ADC_TTY_CONVERTER,
131 SM5502_MUIC_ADC_UART_CABLE,
132 SM5502_MUIC_ADC_TYPE1_CHARGER,
133 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
134 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
135 SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE,
136 SM5502_MUIC_ADC_TYPE2_CHARGER,
137 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
138 SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
139 SM5502_MUIC_ADC_AUDIO_TYPE1,
140 SM5502_MUIC_ADC_OPEN = 0x1f,
141
142 /* The below accessories have same ADC value (0x1f or 0x1e).
143 So, Device type1 is used to separate specific accessory. */
144 /* |---------|--ADC| */
145 /* | [7:5]|[4:0]| */
146 SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* | 001|11110| */
147 SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e, /* | 010|11110| */
148 /* |Dev Type1|--ADC| */
149 SM5502_MUIC_ADC_OPEN_USB = 0x5f, /* | 010|11111| */
150 SM5502_MUIC_ADC_OPEN_TA = 0xdf, /* | 110|11111| */
151 SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff, /* | 111|11111| */
152};
153
154/* List of supported interrupt for SM5502 */
155static struct muic_irq sm5502_muic_irqs[] = {
156 { SM5502_IRQ_INT1_ATTACH, "muic-attach" },
157 { SM5502_IRQ_INT1_DETACH, "muic-detach" },
158 { SM5502_IRQ_INT1_KP, "muic-kp" },
159 { SM5502_IRQ_INT1_LKP, "muic-lkp" },
160 { SM5502_IRQ_INT1_LKR, "muic-lkr" },
161 { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" },
162 { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" },
163 { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" },
164 { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" },
165 { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" },
166 { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" },
167 { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" },
168 { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" },
169 { SM5502_IRQ_INT2_MHL, "muic-mhl" },
170};
171
172/* Define interrupt list of SM5502 to register regmap_irq */
173static const struct regmap_irq sm5502_irqs[] = {
174 /* INT1 interrupts */
175 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, },
176 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, },
177 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, },
178 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, },
179 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, },
180 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, },
181 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, },
182 { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, },
183
184 /* INT2 interrupts */
185 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,},
186 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, },
187 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, },
188 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, },
189 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, },
190 { .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, },
191};
192
193static const struct regmap_irq_chip sm5502_muic_irq_chip = {
194 .name = "sm5502",
195 .status_base = SM5502_REG_INT1,
196 .mask_base = SM5502_REG_INTMASK1,
197 .mask_invert = false,
198 .num_regs = 2,
199 .irqs = sm5502_irqs,
200 .num_irqs = ARRAY_SIZE(sm5502_irqs),
201};
202
203/* Define regmap configuration of SM5502 for I2C communication */
204static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg)
205{
206 switch (reg) {
207 case SM5502_REG_INTMASK1:
208 case SM5502_REG_INTMASK2:
209 return true;
210 default:
211 break;
212 }
213 return false;
214}
215
216static const struct regmap_config sm5502_muic_regmap_config = {
217 .reg_bits = 8,
218 .val_bits = 8,
219 .volatile_reg = sm5502_muic_volatile_reg,
220 .max_register = SM5502_REG_END,
221};
222
Chanwoo Choia75fed22014-05-28 15:35:29 +0900223/* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
224static int sm5502_muic_set_path(struct sm5502_muic_info *info,
225 unsigned int con_sw, unsigned int vbus_sw,
226 bool attached)
227{
228 int ret;
229
230 if (!attached) {
231 con_sw = DM_DP_SWITCH_OPEN;
232 vbus_sw = VBUSIN_SWITCH_OPEN;
233 }
234
235 switch (con_sw) {
236 case DM_DP_SWITCH_OPEN:
237 case DM_DP_SWITCH_USB:
238 case DM_DP_SWITCH_AUDIO:
239 case DM_DP_SWITCH_UART:
240 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
241 SM5502_REG_MANUAL_SW1_DP_MASK |
242 SM5502_REG_MANUAL_SW1_DM_MASK,
243 con_sw);
244 if (ret < 0) {
245 dev_err(info->dev,
246 "cannot update DM_CON/DP_CON switch\n");
247 return ret;
248 }
249 break;
250 default:
251 dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
252 con_sw);
253 return -EINVAL;
254 };
255
256 switch (vbus_sw) {
257 case VBUSIN_SWITCH_OPEN:
258 case VBUSIN_SWITCH_VBUSOUT:
259 case VBUSIN_SWITCH_MIC:
260 case VBUSIN_SWITCH_VBUSOUT_WITH_USB:
261 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
262 SM5502_REG_MANUAL_SW1_VBUSIN_MASK,
263 vbus_sw);
264 if (ret < 0) {
265 dev_err(info->dev,
266 "cannot update VBUSIN switch\n");
267 return ret;
268 }
269 break;
270 default:
271 dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw);
272 return -EINVAL;
273 };
274
275 return 0;
276}
277
Chanwoo Choi914b8812014-05-22 14:06:33 +0900278/* Return cable type of attached or detached accessories */
279static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info)
280{
281 unsigned int cable_type = -1, adc, dev_type1;
282 int ret;
283
284 /* Read ADC value according to external cable or button */
285 ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc);
286 if (ret) {
287 dev_err(info->dev, "failed to read ADC register\n");
288 return ret;
289 }
290
291 /*
292 * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't
293 * connected with to MUIC device.
294 */
Chanwoo Choi0ccc7952014-07-30 15:39:02 +0900295 cable_type = adc & SM5502_REG_ADC_MASK;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900296 if (cable_type == SM5502_MUIC_ADC_GROUND)
297 return SM5502_MUIC_ADC_GROUND;
298
299 switch (cable_type) {
300 case SM5502_MUIC_ADC_GROUND:
301 case SM5502_MUIC_ADC_SEND_END_BUTTON:
302 case SM5502_MUIC_ADC_REMOTE_S1_BUTTON:
303 case SM5502_MUIC_ADC_REMOTE_S2_BUTTON:
304 case SM5502_MUIC_ADC_REMOTE_S3_BUTTON:
305 case SM5502_MUIC_ADC_REMOTE_S4_BUTTON:
306 case SM5502_MUIC_ADC_REMOTE_S5_BUTTON:
307 case SM5502_MUIC_ADC_REMOTE_S6_BUTTON:
308 case SM5502_MUIC_ADC_REMOTE_S7_BUTTON:
309 case SM5502_MUIC_ADC_REMOTE_S8_BUTTON:
310 case SM5502_MUIC_ADC_REMOTE_S9_BUTTON:
311 case SM5502_MUIC_ADC_REMOTE_S10_BUTTON:
312 case SM5502_MUIC_ADC_REMOTE_S11_BUTTON:
313 case SM5502_MUIC_ADC_REMOTE_S12_BUTTON:
314 case SM5502_MUIC_ADC_RESERVED_ACC_1:
315 case SM5502_MUIC_ADC_RESERVED_ACC_2:
316 case SM5502_MUIC_ADC_RESERVED_ACC_3:
317 case SM5502_MUIC_ADC_RESERVED_ACC_4:
318 case SM5502_MUIC_ADC_RESERVED_ACC_5:
319 case SM5502_MUIC_ADC_AUDIO_TYPE2:
320 case SM5502_MUIC_ADC_PHONE_POWERED_DEV:
321 case SM5502_MUIC_ADC_TTY_CONVERTER:
322 case SM5502_MUIC_ADC_UART_CABLE:
323 case SM5502_MUIC_ADC_TYPE1_CHARGER:
324 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
325 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
326 case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE:
327 case SM5502_MUIC_ADC_TYPE2_CHARGER:
328 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
329 case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
330 break;
331 case SM5502_MUIC_ADC_AUDIO_TYPE1:
332 /*
333 * Check whether cable type is
334 * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE
335 * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END
336 * by using Button event.
337 */
338 break;
339 case SM5502_MUIC_ADC_OPEN:
340 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
341 &dev_type1);
342 if (ret) {
343 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
344 return ret;
345 }
346
347 switch (dev_type1) {
348 case SM5502_REG_DEV_TYPE1_USB_SDP_MASK:
349 cable_type = SM5502_MUIC_ADC_OPEN_USB;
350 break;
351 case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK:
352 cable_type = SM5502_MUIC_ADC_OPEN_TA;
353 break;
354 case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
355 cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG;
356 break;
357 default:
358 dev_dbg(info->dev,
Chanwoo Choi34825e52015-03-07 01:41:36 +0900359 "cannot identify the cable type: adc(0x%x)\n",
360 adc);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900361 return -EINVAL;
362 };
363 break;
364 default:
365 dev_err(info->dev,
366 "failed to identify the cable type: adc(0x%x)\n", adc);
367 return -EINVAL;
368 };
369
370 return cable_type;
371}
372
373static int sm5502_muic_cable_handler(struct sm5502_muic_info *info,
374 bool attached)
375{
376 static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900377 unsigned int cable_type = SM5502_MUIC_ADC_GROUND;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900378 unsigned int con_sw = DM_DP_SWITCH_OPEN;
379 unsigned int vbus_sw = VBUSIN_SWITCH_OPEN;
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900380 unsigned int id;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900381 int ret;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900382
Chanwoo Choi914b8812014-05-22 14:06:33 +0900383 /* Get the type of attached or detached cable */
384 if (attached)
385 cable_type = sm5502_muic_get_cable_type(info);
Chanwoo Choifbae30d2014-08-12 10:15:39 +0900386 else
Chanwoo Choi914b8812014-05-22 14:06:33 +0900387 cable_type = prev_cable_type;
388 prev_cable_type = cable_type;
389
390 switch (cable_type) {
391 case SM5502_MUIC_ADC_OPEN_USB:
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900392 id = EXTCON_USB;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900393 con_sw = DM_DP_SWITCH_USB;
394 vbus_sw = VBUSIN_SWITCH_VBUSOUT_WITH_USB;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900395 break;
396 case SM5502_MUIC_ADC_OPEN_TA:
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900397 id = EXTCON_CHG_USB_DCP;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900398 con_sw = DM_DP_SWITCH_OPEN;
399 vbus_sw = VBUSIN_SWITCH_VBUSOUT;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900400 break;
401 case SM5502_MUIC_ADC_OPEN_USB_OTG:
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900402 id = EXTCON_USB_HOST;
Chanwoo Choia75fed22014-05-28 15:35:29 +0900403 con_sw = DM_DP_SWITCH_USB;
404 vbus_sw = VBUSIN_SWITCH_OPEN;
Chanwoo Choie1954452014-05-28 14:21:55 +0900405 break;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900406 default:
407 dev_dbg(info->dev,
408 "cannot handle this cable_type (0x%x)\n", cable_type);
409 return 0;
410 };
411
Chanwoo Choia75fed22014-05-28 15:35:29 +0900412 /* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */
413 ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached);
414 if (ret < 0)
415 return ret;
416
417 /* Change the state of external accessory */
Chanwoo Choi8670b452016-08-16 15:55:34 +0900418 extcon_set_state_sync(info->edev, id, attached);
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +0900419 if (id == EXTCON_USB)
Chanwoo Choi8670b452016-08-16 15:55:34 +0900420 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
Chanwoo Choi8b45b6a2015-11-09 10:10:15 +0900421 attached);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900422
423 return 0;
424}
425
426static void sm5502_muic_irq_work(struct work_struct *work)
427{
428 struct sm5502_muic_info *info = container_of(work,
429 struct sm5502_muic_info, irq_work);
430 int ret = 0;
431
432 if (!info->edev)
433 return;
434
435 mutex_lock(&info->mutex);
436
437 /* Detect attached or detached cables */
438 if (info->irq_attach) {
439 ret = sm5502_muic_cable_handler(info, true);
440 info->irq_attach = false;
441 }
442 if (info->irq_detach) {
443 ret = sm5502_muic_cable_handler(info, false);
444 info->irq_detach = false;
445 }
446
447 if (ret < 0)
448 dev_err(info->dev, "failed to handle MUIC interrupt\n");
449
450 mutex_unlock(&info->mutex);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900451}
452
453/*
454 * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0.
455 * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
456 */
457static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type)
458{
459 switch (irq_type) {
460 case SM5502_IRQ_INT1_ATTACH:
461 info->irq_attach = true;
462 break;
463 case SM5502_IRQ_INT1_DETACH:
464 info->irq_detach = true;
465 break;
466 case SM5502_IRQ_INT1_KP:
467 case SM5502_IRQ_INT1_LKP:
468 case SM5502_IRQ_INT1_LKR:
469 case SM5502_IRQ_INT1_OVP_EVENT:
470 case SM5502_IRQ_INT1_OCP_EVENT:
471 case SM5502_IRQ_INT1_OVP_OCP_DIS:
472 case SM5502_IRQ_INT2_VBUS_DET:
473 case SM5502_IRQ_INT2_REV_ACCE:
474 case SM5502_IRQ_INT2_ADC_CHG:
475 case SM5502_IRQ_INT2_STUCK_KEY:
476 case SM5502_IRQ_INT2_STUCK_KEY_RCV:
477 case SM5502_IRQ_INT2_MHL:
478 default:
479 break;
480 }
481
482 return 0;
483}
484
485static irqreturn_t sm5502_muic_irq_handler(int irq, void *data)
486{
487 struct sm5502_muic_info *info = data;
488 int i, irq_type = -1, ret;
489
490 for (i = 0; i < info->num_muic_irqs; i++)
491 if (irq == info->muic_irqs[i].virq)
492 irq_type = info->muic_irqs[i].irq;
493
494 ret = sm5502_parse_irq(info, irq_type);
495 if (ret < 0) {
496 dev_warn(info->dev, "cannot handle is interrupt:%d\n",
497 irq_type);
498 return IRQ_HANDLED;
499 }
500 schedule_work(&info->irq_work);
501
502 return IRQ_HANDLED;
503}
504
Chanwoo Choie1954452014-05-28 14:21:55 +0900505static void sm5502_muic_detect_cable_wq(struct work_struct *work)
506{
507 struct sm5502_muic_info *info = container_of(to_delayed_work(work),
508 struct sm5502_muic_info, wq_detcable);
509 int ret;
510
511 /* Notify the state of connector cable or not */
512 ret = sm5502_muic_cable_handler(info, true);
513 if (ret < 0)
514 dev_warn(info->dev, "failed to detect cable state\n");
515}
516
Chanwoo Choi914b8812014-05-22 14:06:33 +0900517static void sm5502_init_dev_type(struct sm5502_muic_info *info)
518{
519 unsigned int reg_data, vendor_id, version_id;
520 int i, ret;
521
522 /* To test I2C, Print version_id and vendor_id of SM5502 */
523 ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, &reg_data);
524 if (ret) {
525 dev_err(info->dev,
526 "failed to read DEVICE_ID register: %d\n", ret);
527 return;
528 }
529
530 vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >>
531 SM5502_REG_DEVICE_ID_VENDOR_SHIFT);
532 version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >>
533 SM5502_REG_DEVICE_ID_VERSION_SHIFT);
534
535 dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
536 version_id, vendor_id);
537
538 /* Initiazle the register of SM5502 device to bring-up */
539 for (i = 0; i < info->num_reg_data; i++) {
540 unsigned int val = 0;
541
542 if (!info->reg_data[i].invert)
543 val |= ~info->reg_data[i].val;
544 else
545 val = info->reg_data[i].val;
546 regmap_write(info->regmap, info->reg_data[i].reg, val);
547 }
548}
549
550static int sm5022_muic_i2c_probe(struct i2c_client *i2c,
551 const struct i2c_device_id *id)
552{
553 struct device_node *np = i2c->dev.of_node;
554 struct sm5502_muic_info *info;
555 int i, ret, irq_flags;
556
557 if (!np)
558 return -EINVAL;
559
560 info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
561 if (!info)
562 return -ENOMEM;
563 i2c_set_clientdata(i2c, info);
564
565 info->dev = &i2c->dev;
566 info->i2c = i2c;
567 info->irq = i2c->irq;
568 info->muic_irqs = sm5502_muic_irqs;
569 info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs);
570 info->reg_data = sm5502_reg_data;
571 info->num_reg_data = ARRAY_SIZE(sm5502_reg_data);
572
573 mutex_init(&info->mutex);
574
575 INIT_WORK(&info->irq_work, sm5502_muic_irq_work);
576
577 info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config);
578 if (IS_ERR(info->regmap)) {
579 ret = PTR_ERR(info->regmap);
580 dev_err(info->dev, "failed to allocate register map: %d\n",
581 ret);
582 return ret;
583 }
584
585 /* Support irq domain for SM5502 MUIC device */
586 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
587 ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0,
588 &sm5502_muic_irq_chip, &info->irq_data);
589 if (ret != 0) {
590 dev_err(info->dev, "failed to request IRQ %d: %d\n",
591 info->irq, ret);
592 return ret;
593 }
594
595 for (i = 0; i < info->num_muic_irqs; i++) {
596 struct muic_irq *muic_irq = &info->muic_irqs[i];
Andrzej Hajda363b3892015-09-24 16:00:21 +0200597 int virq = 0;
Chanwoo Choi914b8812014-05-22 14:06:33 +0900598
599 virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
600 if (virq <= 0)
601 return -EINVAL;
602 muic_irq->virq = virq;
603
604 ret = devm_request_threaded_irq(info->dev, virq, NULL,
605 sm5502_muic_irq_handler,
606 IRQF_NO_SUSPEND,
607 muic_irq->name, info);
608 if (ret) {
Chanwoo Choifbae30d2014-08-12 10:15:39 +0900609 dev_err(info->dev,
610 "failed: irq request (IRQ: %d, error :%d)\n",
611 muic_irq->irq, ret);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900612 return ret;
613 }
614 }
615
616 /* Allocate extcon device */
617 info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable);
618 if (IS_ERR(info->edev)) {
619 dev_err(info->dev, "failed to allocate memory for extcon\n");
620 return -ENOMEM;
621 }
Chanwoo Choi914b8812014-05-22 14:06:33 +0900622
623 /* Register extcon device */
624 ret = devm_extcon_dev_register(info->dev, info->edev);
625 if (ret) {
626 dev_err(info->dev, "failed to register extcon device\n");
627 return ret;
628 }
629
Chanwoo Choie1954452014-05-28 14:21:55 +0900630 /*
631 * Detect accessory after completing the initialization of platform
632 *
633 * - Use delayed workqueue to detect cable state and then
634 * notify cable state to notifiee/platform through uevent.
635 * After completing the booting of platform, the extcon provider
636 * driver should notify cable state to upper layer.
637 */
638 INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
639 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
640 msecs_to_jiffies(DELAY_MS_DEFAULT));
641
Chanwoo Choi914b8812014-05-22 14:06:33 +0900642 /* Initialize SM5502 device and print vendor id and version id */
643 sm5502_init_dev_type(info);
644
645 return 0;
646}
647
648static int sm5502_muic_i2c_remove(struct i2c_client *i2c)
649{
650 struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
651
652 regmap_del_irq_chip(info->irq, info->irq_data);
653
654 return 0;
655}
656
Chanwoo Choi34825e52015-03-07 01:41:36 +0900657static const struct of_device_id sm5502_dt_match[] = {
Chanwoo Choi914b8812014-05-22 14:06:33 +0900658 { .compatible = "siliconmitus,sm5502-muic" },
659 { },
660};
Javier Martinez Canillasff612f92015-08-25 08:31:15 +0200661MODULE_DEVICE_TABLE(of, sm5502_dt_match);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900662
663#ifdef CONFIG_PM_SLEEP
664static int sm5502_muic_suspend(struct device *dev)
665{
Geliang Tangd5859342015-12-28 23:00:15 +0800666 struct i2c_client *i2c = to_i2c_client(dev);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900667 struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
668
669 enable_irq_wake(info->irq);
670
671 return 0;
672}
673
674static int sm5502_muic_resume(struct device *dev)
675{
Geliang Tangd5859342015-12-28 23:00:15 +0800676 struct i2c_client *i2c = to_i2c_client(dev);
Chanwoo Choi914b8812014-05-22 14:06:33 +0900677 struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
678
679 disable_irq_wake(info->irq);
680
681 return 0;
682}
683#endif
684
685static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops,
686 sm5502_muic_suspend, sm5502_muic_resume);
687
688static const struct i2c_device_id sm5502_i2c_id[] = {
689 { "sm5502", TYPE_SM5502 },
690 { }
691};
692MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id);
693
694static struct i2c_driver sm5502_muic_i2c_driver = {
695 .driver = {
696 .name = "sm5502",
Chanwoo Choi914b8812014-05-22 14:06:33 +0900697 .pm = &sm5502_muic_pm_ops,
698 .of_match_table = sm5502_dt_match,
699 },
700 .probe = sm5022_muic_i2c_probe,
701 .remove = sm5502_muic_i2c_remove,
702 .id_table = sm5502_i2c_id,
703};
704
705static int __init sm5502_muic_i2c_init(void)
706{
707 return i2c_add_driver(&sm5502_muic_i2c_driver);
708}
709subsys_initcall(sm5502_muic_i2c_init);
710
711MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver");
712MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
713MODULE_LICENSE("GPL");