GNU Linux-libre 4.9.292-gnu1
[releases.git] / drivers / extcon / extcon-sm5502.c
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
28 struct muic_irq {
29         unsigned int irq;
30         const char *name;
31         unsigned int virq;
32 };
33
34 struct reg_data {
35         u8 reg;
36         unsigned int val;
37         bool invert;
38 };
39
40 struct 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. */
70 static 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 /* List of detectable cables */
98 static const unsigned int sm5502_extcon_cable[] = {
99         EXTCON_USB,
100         EXTCON_USB_HOST,
101         EXTCON_CHG_USB_SDP,
102         EXTCON_CHG_USB_DCP,
103         EXTCON_NONE,
104 };
105
106 /* Define supported accessory type */
107 enum sm5502_muic_acc_type {
108         SM5502_MUIC_ADC_GROUND = 0x0,
109         SM5502_MUIC_ADC_SEND_END_BUTTON,
110         SM5502_MUIC_ADC_REMOTE_S1_BUTTON,
111         SM5502_MUIC_ADC_REMOTE_S2_BUTTON,
112         SM5502_MUIC_ADC_REMOTE_S3_BUTTON,
113         SM5502_MUIC_ADC_REMOTE_S4_BUTTON,
114         SM5502_MUIC_ADC_REMOTE_S5_BUTTON,
115         SM5502_MUIC_ADC_REMOTE_S6_BUTTON,
116         SM5502_MUIC_ADC_REMOTE_S7_BUTTON,
117         SM5502_MUIC_ADC_REMOTE_S8_BUTTON,
118         SM5502_MUIC_ADC_REMOTE_S9_BUTTON,
119         SM5502_MUIC_ADC_REMOTE_S10_BUTTON,
120         SM5502_MUIC_ADC_REMOTE_S11_BUTTON,
121         SM5502_MUIC_ADC_REMOTE_S12_BUTTON,
122         SM5502_MUIC_ADC_RESERVED_ACC_1,
123         SM5502_MUIC_ADC_RESERVED_ACC_2,
124         SM5502_MUIC_ADC_RESERVED_ACC_3,
125         SM5502_MUIC_ADC_RESERVED_ACC_4,
126         SM5502_MUIC_ADC_RESERVED_ACC_5,
127         SM5502_MUIC_ADC_AUDIO_TYPE2,
128         SM5502_MUIC_ADC_PHONE_POWERED_DEV,
129         SM5502_MUIC_ADC_TTY_CONVERTER,
130         SM5502_MUIC_ADC_UART_CABLE,
131         SM5502_MUIC_ADC_TYPE1_CHARGER,
132         SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
133         SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
134         SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE,
135         SM5502_MUIC_ADC_TYPE2_CHARGER,
136         SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
137         SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
138         SM5502_MUIC_ADC_AUDIO_TYPE1,
139         SM5502_MUIC_ADC_OPEN = 0x1f,
140
141         /* The below accessories have same ADC value (0x1f or 0x1e).
142            So, Device type1 is used to separate specific accessory. */
143                                                         /* |---------|--ADC| */
144                                                         /* |    [7:5]|[4:0]| */
145         SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* |      001|11110| */
146         SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e,    /* |      010|11110| */
147                                                         /* |Dev Type1|--ADC| */
148         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 */
154 static 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 */
172 static 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
192 static 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  */
203 static 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
215 static 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
222 /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
223 static 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;
253         };
254
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;
272         };
273
274         return 0;
275 }
276
277 /* Return cable type of attached or detached accessories */
278 static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info)
279 {
280         unsigned int cable_type = -1, adc, dev_type1;
281         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          */
294         cable_type = adc & SM5502_REG_ADC_MASK;
295         if (cable_type == SM5502_MUIC_ADC_GROUND)
296                 return SM5502_MUIC_ADC_GROUND;
297
298         switch (cable_type) {
299         case SM5502_MUIC_ADC_GROUND:
300         case SM5502_MUIC_ADC_SEND_END_BUTTON:
301         case SM5502_MUIC_ADC_REMOTE_S1_BUTTON:
302         case SM5502_MUIC_ADC_REMOTE_S2_BUTTON:
303         case SM5502_MUIC_ADC_REMOTE_S3_BUTTON:
304         case SM5502_MUIC_ADC_REMOTE_S4_BUTTON:
305         case SM5502_MUIC_ADC_REMOTE_S5_BUTTON:
306         case SM5502_MUIC_ADC_REMOTE_S6_BUTTON:
307         case SM5502_MUIC_ADC_REMOTE_S7_BUTTON:
308         case SM5502_MUIC_ADC_REMOTE_S8_BUTTON:
309         case SM5502_MUIC_ADC_REMOTE_S9_BUTTON:
310         case SM5502_MUIC_ADC_REMOTE_S10_BUTTON:
311         case SM5502_MUIC_ADC_REMOTE_S11_BUTTON:
312         case SM5502_MUIC_ADC_REMOTE_S12_BUTTON:
313         case SM5502_MUIC_ADC_RESERVED_ACC_1:
314         case SM5502_MUIC_ADC_RESERVED_ACC_2:
315         case SM5502_MUIC_ADC_RESERVED_ACC_3:
316         case SM5502_MUIC_ADC_RESERVED_ACC_4:
317         case SM5502_MUIC_ADC_RESERVED_ACC_5:
318         case SM5502_MUIC_ADC_AUDIO_TYPE2:
319         case SM5502_MUIC_ADC_PHONE_POWERED_DEV:
320         case SM5502_MUIC_ADC_TTY_CONVERTER:
321         case SM5502_MUIC_ADC_UART_CABLE:
322         case SM5502_MUIC_ADC_TYPE1_CHARGER:
323         case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
324         case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
325         case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE:
326         case SM5502_MUIC_ADC_TYPE2_CHARGER:
327         case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
328         case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
329                 break;
330         case SM5502_MUIC_ADC_AUDIO_TYPE1:
331                 /*
332                  * Check whether cable type is
333                  * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE
334                  * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END
335                  * by using Button event.
336                  */
337                 break;
338         case SM5502_MUIC_ADC_OPEN:
339                 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
340                                   &dev_type1);
341                 if (ret) {
342                         dev_err(info->dev, "failed to read DEV_TYPE1 reg\n");
343                         return ret;
344                 }
345
346                 switch (dev_type1) {
347                 case SM5502_REG_DEV_TYPE1_USB_SDP_MASK:
348                         cable_type = SM5502_MUIC_ADC_OPEN_USB;
349                         break;
350                 case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK:
351                         cable_type = SM5502_MUIC_ADC_OPEN_TA;
352                         break;
353                 case SM5502_REG_DEV_TYPE1_USB_OTG_MASK:
354                         cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG;
355                         break;
356                 default:
357                         dev_dbg(info->dev,
358                                 "cannot identify the cable type: adc(0x%x)\n",
359                                 adc);
360                         return -EINVAL;
361                 };
362                 break;
363         default:
364                 dev_err(info->dev,
365                         "failed to identify the cable type: adc(0x%x)\n", adc);
366                 return -EINVAL;
367         };
368
369         return cable_type;
370 }
371
372 static int sm5502_muic_cable_handler(struct sm5502_muic_info *info,
373                                      bool attached)
374 {
375         static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND;
376         unsigned int cable_type = SM5502_MUIC_ADC_GROUND;
377         unsigned int con_sw = DM_DP_SWITCH_OPEN;
378         unsigned int vbus_sw = VBUSIN_SWITCH_OPEN;
379         unsigned int id;
380         int ret;
381
382         /* Get the type of attached or detached cable */
383         if (attached)
384                 cable_type = sm5502_muic_get_cable_type(info);
385         else
386                 cable_type = prev_cable_type;
387         prev_cable_type = cable_type;
388
389         switch (cable_type) {
390         case SM5502_MUIC_ADC_OPEN_USB:
391                 id      = EXTCON_USB;
392                 con_sw  = DM_DP_SWITCH_USB;
393                 vbus_sw = VBUSIN_SWITCH_VBUSOUT_WITH_USB;
394                 break;
395         case SM5502_MUIC_ADC_OPEN_TA:
396                 id      = EXTCON_CHG_USB_DCP;
397                 con_sw  = DM_DP_SWITCH_OPEN;
398                 vbus_sw = VBUSIN_SWITCH_VBUSOUT;
399                 break;
400         case SM5502_MUIC_ADC_OPEN_USB_OTG:
401                 id      = EXTCON_USB_HOST;
402                 con_sw  = DM_DP_SWITCH_USB;
403                 vbus_sw = VBUSIN_SWITCH_OPEN;
404                 break;
405         default:
406                 dev_dbg(info->dev,
407                         "cannot handle this cable_type (0x%x)\n", cable_type);
408                 return 0;
409         };
410
411         /* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */
412         ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached);
413         if (ret < 0)
414                 return ret;
415
416         /* Change the state of external accessory */
417         extcon_set_state_sync(info->edev, id, attached);
418         if (id == EXTCON_USB)
419                 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
420                                         attached);
421
422         return 0;
423 }
424
425 static void sm5502_muic_irq_work(struct work_struct *work)
426 {
427         struct sm5502_muic_info *info = container_of(work,
428                         struct sm5502_muic_info, irq_work);
429         int ret = 0;
430
431         if (!info->edev)
432                 return;
433
434         mutex_lock(&info->mutex);
435
436         /* Detect attached or detached cables */
437         if (info->irq_attach) {
438                 ret = sm5502_muic_cable_handler(info, true);
439                 info->irq_attach = false;
440         }
441         if (info->irq_detach) {
442                 ret = sm5502_muic_cable_handler(info, false);
443                 info->irq_detach = false;
444         }
445
446         if (ret < 0)
447                 dev_err(info->dev, "failed to handle MUIC interrupt\n");
448
449         mutex_unlock(&info->mutex);
450 }
451
452 /*
453  * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0.
454  * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
455  */
456 static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type)
457 {
458         switch (irq_type) {
459         case SM5502_IRQ_INT1_ATTACH:
460                 info->irq_attach = true;
461                 break;
462         case SM5502_IRQ_INT1_DETACH:
463                 info->irq_detach = true;
464                 break;
465         case SM5502_IRQ_INT1_KP:
466         case SM5502_IRQ_INT1_LKP:
467         case SM5502_IRQ_INT1_LKR:
468         case SM5502_IRQ_INT1_OVP_EVENT:
469         case SM5502_IRQ_INT1_OCP_EVENT:
470         case SM5502_IRQ_INT1_OVP_OCP_DIS:
471         case SM5502_IRQ_INT2_VBUS_DET:
472         case SM5502_IRQ_INT2_REV_ACCE:
473         case SM5502_IRQ_INT2_ADC_CHG:
474         case SM5502_IRQ_INT2_STUCK_KEY:
475         case SM5502_IRQ_INT2_STUCK_KEY_RCV:
476         case SM5502_IRQ_INT2_MHL:
477         default:
478                 break;
479         }
480
481         return 0;
482 }
483
484 static irqreturn_t sm5502_muic_irq_handler(int irq, void *data)
485 {
486         struct sm5502_muic_info *info = data;
487         int i, irq_type = -1, ret;
488
489         for (i = 0; i < info->num_muic_irqs; i++)
490                 if (irq == info->muic_irqs[i].virq)
491                         irq_type = info->muic_irqs[i].irq;
492
493         ret = sm5502_parse_irq(info, irq_type);
494         if (ret < 0) {
495                 dev_warn(info->dev, "cannot handle is interrupt:%d\n",
496                                     irq_type);
497                 return IRQ_HANDLED;
498         }
499         schedule_work(&info->irq_work);
500
501         return IRQ_HANDLED;
502 }
503
504 static void sm5502_muic_detect_cable_wq(struct work_struct *work)
505 {
506         struct sm5502_muic_info *info = container_of(to_delayed_work(work),
507                                 struct sm5502_muic_info, wq_detcable);
508         int ret;
509
510         /* Notify the state of connector cable or not  */
511         ret = sm5502_muic_cable_handler(info, true);
512         if (ret < 0)
513                 dev_warn(info->dev, "failed to detect cable state\n");
514 }
515
516 static void sm5502_init_dev_type(struct sm5502_muic_info *info)
517 {
518         unsigned int reg_data, vendor_id, version_id;
519         int i, ret;
520
521         /* To test I2C, Print version_id and vendor_id of SM5502 */
522         ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, &reg_data);
523         if (ret) {
524                 dev_err(info->dev,
525                         "failed to read DEVICE_ID register: %d\n", ret);
526                 return;
527         }
528
529         vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >>
530                                 SM5502_REG_DEVICE_ID_VENDOR_SHIFT);
531         version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >>
532                                 SM5502_REG_DEVICE_ID_VERSION_SHIFT);
533
534         dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
535                             version_id, vendor_id);
536
537         /* Initiazle the register of SM5502 device to bring-up */
538         for (i = 0; i < info->num_reg_data; i++) {
539                 unsigned int val = 0;
540
541                 if (!info->reg_data[i].invert)
542                         val |= ~info->reg_data[i].val;
543                 else
544                         val = info->reg_data[i].val;
545                 regmap_write(info->regmap, info->reg_data[i].reg, val);
546         }
547 }
548
549 static int sm5022_muic_i2c_probe(struct i2c_client *i2c,
550                                  const struct i2c_device_id *id)
551 {
552         struct device_node *np = i2c->dev.of_node;
553         struct sm5502_muic_info *info;
554         int i, ret, irq_flags;
555
556         if (!np)
557                 return -EINVAL;
558
559         info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
560         if (!info)
561                 return -ENOMEM;
562         i2c_set_clientdata(i2c, info);
563
564         info->dev = &i2c->dev;
565         info->i2c = i2c;
566         info->irq = i2c->irq;
567         info->muic_irqs = sm5502_muic_irqs;
568         info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs);
569         info->reg_data = sm5502_reg_data;
570         info->num_reg_data = ARRAY_SIZE(sm5502_reg_data);
571
572         mutex_init(&info->mutex);
573
574         INIT_WORK(&info->irq_work, sm5502_muic_irq_work);
575
576         info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config);
577         if (IS_ERR(info->regmap)) {
578                 ret = PTR_ERR(info->regmap);
579                 dev_err(info->dev, "failed to allocate register map: %d\n",
580                                    ret);
581                 return ret;
582         }
583
584         /* Support irq domain for SM5502 MUIC device */
585         irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
586         ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0,
587                                   &sm5502_muic_irq_chip, &info->irq_data);
588         if (ret != 0) {
589                 dev_err(info->dev, "failed to request IRQ %d: %d\n",
590                                     info->irq, ret);
591                 return ret;
592         }
593
594         for (i = 0; i < info->num_muic_irqs; i++) {
595                 struct muic_irq *muic_irq = &info->muic_irqs[i];
596                 int virq = 0;
597
598                 virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
599                 if (virq <= 0)
600                         return -EINVAL;
601                 muic_irq->virq = virq;
602
603                 ret = devm_request_threaded_irq(info->dev, virq, NULL,
604                                                 sm5502_muic_irq_handler,
605                                                 IRQF_NO_SUSPEND,
606                                                 muic_irq->name, info);
607                 if (ret) {
608                         dev_err(info->dev,
609                                 "failed: irq request (IRQ: %d, error :%d)\n",
610                                 muic_irq->irq, ret);
611                         return ret;
612                 }
613         }
614
615         /* Allocate extcon device */
616         info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable);
617         if (IS_ERR(info->edev)) {
618                 dev_err(info->dev, "failed to allocate memory for extcon\n");
619                 return -ENOMEM;
620         }
621
622         /* Register extcon device */
623         ret = devm_extcon_dev_register(info->dev, info->edev);
624         if (ret) {
625                 dev_err(info->dev, "failed to register extcon device\n");
626                 return ret;
627         }
628
629         /*
630          * Detect accessory after completing the initialization of platform
631          *
632          * - Use delayed workqueue to detect cable state and then
633          * notify cable state to notifiee/platform through uevent.
634          * After completing the booting of platform, the extcon provider
635          * driver should notify cable state to upper layer.
636          */
637         INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
638         queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
639                         msecs_to_jiffies(DELAY_MS_DEFAULT));
640
641         /* Initialize SM5502 device and print vendor id and version id */
642         sm5502_init_dev_type(info);
643
644         return 0;
645 }
646
647 static int sm5502_muic_i2c_remove(struct i2c_client *i2c)
648 {
649         struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
650
651         regmap_del_irq_chip(info->irq, info->irq_data);
652
653         return 0;
654 }
655
656 static const struct of_device_id sm5502_dt_match[] = {
657         { .compatible = "siliconmitus,sm5502-muic" },
658         { },
659 };
660 MODULE_DEVICE_TABLE(of, sm5502_dt_match);
661
662 #ifdef CONFIG_PM_SLEEP
663 static int sm5502_muic_suspend(struct device *dev)
664 {
665         struct i2c_client *i2c = to_i2c_client(dev);
666         struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
667
668         enable_irq_wake(info->irq);
669
670         return 0;
671 }
672
673 static int sm5502_muic_resume(struct device *dev)
674 {
675         struct i2c_client *i2c = to_i2c_client(dev);
676         struct sm5502_muic_info *info = i2c_get_clientdata(i2c);
677
678         disable_irq_wake(info->irq);
679
680         return 0;
681 }
682 #endif
683
684 static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops,
685                          sm5502_muic_suspend, sm5502_muic_resume);
686
687 static const struct i2c_device_id sm5502_i2c_id[] = {
688         { "sm5502", TYPE_SM5502 },
689         { }
690 };
691 MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id);
692
693 static struct i2c_driver sm5502_muic_i2c_driver = {
694         .driver         = {
695                 .name   = "sm5502",
696                 .pm     = &sm5502_muic_pm_ops,
697                 .of_match_table = sm5502_dt_match,
698         },
699         .probe  = sm5022_muic_i2c_probe,
700         .remove = sm5502_muic_i2c_remove,
701         .id_table = sm5502_i2c_id,
702 };
703
704 static int __init sm5502_muic_i2c_init(void)
705 {
706         return i2c_add_driver(&sm5502_muic_i2c_driver);
707 }
708 subsys_initcall(sm5502_muic_i2c_init);
709
710 MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver");
711 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
712 MODULE_LICENSE("GPL");