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