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