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