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