04447f36c994f0243fc665187ea7dd91bbc06561
[cascardo/linux.git] / drivers / extcon / extcon-rt8973a.c
1 /*
2  * extcon-rt8973a.c - Richtek RT8973A extcon driver 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-rt8973a.h"
26
27 #define DELAY_MS_DEFAULT                20000   /* 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         u8 mask;
38         u8 val;
39         bool invert;
40 };
41
42 struct rt8973a_muic_info {
43         struct device *dev;
44         struct extcon_dev *edev;
45
46         struct i2c_client *i2c;
47         struct regmap *regmap;
48
49         struct regmap_irq_chip_data *irq_data;
50         struct muic_irq *muic_irqs;
51         unsigned int num_muic_irqs;
52         int irq;
53         bool irq_attach;
54         bool irq_detach;
55         bool irq_ovp;
56         bool irq_otp;
57         struct work_struct irq_work;
58
59         struct reg_data *reg_data;
60         unsigned int num_reg_data;
61         bool auto_config;
62
63         struct mutex mutex;
64
65         /*
66          * Use delayed workqueue to detect cable state and then
67          * notify cable state to notifiee/platform through uevent.
68          * After completing the booting of platform, the extcon provider
69          * driver should notify cable state to upper layer.
70          */
71         struct delayed_work wq_detcable;
72 };
73
74 /* Default value of RT8973A register to bring up MUIC device. */
75 static struct reg_data rt8973a_reg_data[] = {
76         {
77                 .reg = RT8973A_REG_CONTROL1,
78                 .mask = RT8973A_REG_CONTROL1_ADC_EN_MASK
79                         | RT8973A_REG_CONTROL1_USB_CHD_EN_MASK
80                         | RT8973A_REG_CONTROL1_CHGTYP_MASK
81                         | RT8973A_REG_CONTROL1_SWITCH_OPEN_MASK
82                         | RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK
83                         | RT8973A_REG_CONTROL1_INTM_MASK,
84                 .val = RT8973A_REG_CONTROL1_ADC_EN_MASK
85                         | RT8973A_REG_CONTROL1_USB_CHD_EN_MASK
86                         | RT8973A_REG_CONTROL1_CHGTYP_MASK,
87                 .invert = false,
88         },
89         { /* sentinel */ }
90 };
91
92 /* List of detectable cables */
93 enum {
94         EXTCON_CABLE_USB = 0,
95         EXTCON_CABLE_USB_HOST,
96         EXTCON_CABLE_TA,
97         EXTCON_CABLE_JIG,
98
99         EXTCON_CABLE_END,
100 };
101
102 static const char *rt8973a_extcon_cable[] = {
103         [EXTCON_CABLE_USB]              = "USB",
104         [EXTCON_CABLE_USB_HOST]         = "USB-Host",
105         [EXTCON_CABLE_TA]               = "TA",
106         [EXTCON_CABLE_JIG]              = "JIG",
107         NULL,
108 };
109
110 /* Define OVP (Over Voltage Protection), OTP (Over Temperature Protection) */
111 enum rt8973a_event_type {
112         RT8973A_EVENT_ATTACH = 1,
113         RT8973A_EVENT_DETACH,
114         RT8973A_EVENT_OVP,
115         RT8973A_EVENT_OTP,
116 };
117
118 /* Define supported accessory type */
119 enum rt8973a_muic_acc_type {
120         RT8973A_MUIC_ADC_OTG = 0x0,
121         RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON,
122         RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON,
123         RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON,
124         RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON,
125         RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON,
126         RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON,
127         RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON,
128         RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON,
129         RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON,
130         RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON,
131         RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON,
132         RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON,
133         RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON,
134         RT8973A_MUIC_ADC_RESERVED_ACC_1,
135         RT8973A_MUIC_ADC_RESERVED_ACC_2,
136         RT8973A_MUIC_ADC_RESERVED_ACC_3,
137         RT8973A_MUIC_ADC_RESERVED_ACC_4,
138         RT8973A_MUIC_ADC_RESERVED_ACC_5,
139         RT8973A_MUIC_ADC_AUDIO_TYPE2,
140         RT8973A_MUIC_ADC_PHONE_POWERED_DEV,
141         RT8973A_MUIC_ADC_UNKNOWN_ACC_1,
142         RT8973A_MUIC_ADC_UNKNOWN_ACC_2,
143         RT8973A_MUIC_ADC_TA,
144         RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
145         RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
146         RT8973A_MUIC_ADC_UNKNOWN_ACC_3,
147         RT8973A_MUIC_ADC_UNKNOWN_ACC_4,
148         RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
149         RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
150         RT8973A_MUIC_ADC_UNKNOWN_ACC_5,
151         RT8973A_MUIC_ADC_OPEN = 0x1f,
152
153         /* The below accessories has same ADC value (0x1f).
154            So, Device type1 is used to separate specific accessory. */
155                                         /* |---------|--ADC| */
156                                         /* |    [7:5]|[4:0]| */
157         RT8973A_MUIC_ADC_USB = 0x3f,    /* |      001|11111| */
158 };
159
160 /* List of supported interrupt for RT8973A */
161 static struct muic_irq rt8973a_muic_irqs[] = {
162         { RT8973A_INT1_ATTACH,          "muic-attach" },
163         { RT8973A_INT1_DETACH,          "muic-detach" },
164         { RT8973A_INT1_CHGDET,          "muic-chgdet" },
165         { RT8973A_INT1_DCD_T,           "muic-dcd-t" },
166         { RT8973A_INT1_OVP,             "muic-ovp" },
167         { RT8973A_INT1_CONNECT,         "muic-connect" },
168         { RT8973A_INT1_ADC_CHG,         "muic-adc-chg" },
169         { RT8973A_INT1_OTP,             "muic-otp" },
170         { RT8973A_INT2_UVLO,            "muic-uvlo" },
171         { RT8973A_INT2_POR,             "muic-por" },
172         { RT8973A_INT2_OTP_FET,         "muic-otp-fet" },
173         { RT8973A_INT2_OVP_FET,         "muic-ovp-fet" },
174         { RT8973A_INT2_OCP_LATCH,       "muic-ocp-latch" },
175         { RT8973A_INT2_OCP,             "muic-ocp" },
176         { RT8973A_INT2_OVP_OCP,         "muic-ovp-ocp" },
177 };
178
179 /* Define interrupt list of RT8973A to register regmap_irq */
180 static const struct regmap_irq rt8973a_irqs[] = {
181         /* INT1 interrupts */
182         { .reg_offset = 0, .mask = RT8973A_INT1_ATTACH_MASK, },
183         { .reg_offset = 0, .mask = RT8973A_INT1_DETACH_MASK, },
184         { .reg_offset = 0, .mask = RT8973A_INT1_CHGDET_MASK, },
185         { .reg_offset = 0, .mask = RT8973A_INT1_DCD_T_MASK, },
186         { .reg_offset = 0, .mask = RT8973A_INT1_OVP_MASK, },
187         { .reg_offset = 0, .mask = RT8973A_INT1_CONNECT_MASK, },
188         { .reg_offset = 0, .mask = RT8973A_INT1_ADC_CHG_MASK, },
189         { .reg_offset = 0, .mask = RT8973A_INT1_OTP_MASK, },
190
191         /* INT2 interrupts */
192         { .reg_offset = 1, .mask = RT8973A_INT2_UVLOT_MASK,},
193         { .reg_offset = 1, .mask = RT8973A_INT2_POR_MASK, },
194         { .reg_offset = 1, .mask = RT8973A_INT2_OTP_FET_MASK, },
195         { .reg_offset = 1, .mask = RT8973A_INT2_OVP_FET_MASK, },
196         { .reg_offset = 1, .mask = RT8973A_INT2_OCP_LATCH_MASK, },
197         { .reg_offset = 1, .mask = RT8973A_INT2_OCP_MASK, },
198         { .reg_offset = 1, .mask = RT8973A_INT2_OVP_OCP_MASK, },
199 };
200
201 static const struct regmap_irq_chip rt8973a_muic_irq_chip = {
202         .name                   = "rt8973a",
203         .status_base            = RT8973A_REG_INT1,
204         .mask_base              = RT8973A_REG_INTM1,
205         .mask_invert            = false,
206         .num_regs               = 2,
207         .irqs                   = rt8973a_irqs,
208         .num_irqs               = ARRAY_SIZE(rt8973a_irqs),
209 };
210
211 /* Define regmap configuration of RT8973A for I2C communication  */
212 static bool rt8973a_muic_volatile_reg(struct device *dev, unsigned int reg)
213 {
214         switch (reg) {
215         case RT8973A_REG_INTM1:
216         case RT8973A_REG_INTM2:
217                 return true;
218         default:
219                 break;
220         }
221         return false;
222 }
223
224 static const struct regmap_config rt8973a_muic_regmap_config = {
225         .reg_bits       = 8,
226         .val_bits       = 8,
227         .volatile_reg   = rt8973a_muic_volatile_reg,
228         .max_register   = RT8973A_REG_END,
229 };
230
231 /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
232 static int rt8973a_muic_set_path(struct rt8973a_muic_info *info,
233                                 unsigned int con_sw, bool attached)
234 {
235         int ret;
236
237         /*
238          * Don't need to set h/w path according to cable type
239          * if Auto-configuration mode of CONTROL1 register is true.
240          */
241         if (info->auto_config)
242                 return 0;
243
244         if (!attached)
245                 con_sw  = DM_DP_SWITCH_UART;
246
247         switch (con_sw) {
248         case DM_DP_SWITCH_OPEN:
249         case DM_DP_SWITCH_USB:
250         case DM_DP_SWITCH_UART:
251                 ret = regmap_update_bits(info->regmap, RT8973A_REG_MANUAL_SW1,
252                                         RT8973A_REG_MANUAL_SW1_DP_MASK |
253                                         RT8973A_REG_MANUAL_SW1_DM_MASK,
254                                         con_sw);
255                 if (ret < 0) {
256                         dev_err(info->dev,
257                                 "cannot update DM_CON/DP_CON switch\n");
258                         return ret;
259                 }
260                 break;
261         default:
262                 dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
263                                 con_sw);
264                 return -EINVAL;
265         }
266
267         return 0;
268 }
269
270 static int rt8973a_muic_get_cable_type(struct rt8973a_muic_info *info)
271 {
272         unsigned int adc, dev1;
273         int ret, cable_type;
274
275         /* Read ADC value according to external cable or button */
276         ret = regmap_read(info->regmap, RT8973A_REG_ADC, &adc);
277         if (ret) {
278                 dev_err(info->dev, "failed to read ADC register\n");
279                 return ret;
280         }
281         cable_type = adc & RT8973A_REG_ADC_MASK;
282
283         /* Read Device 1 reigster to identify correct cable type */
284         ret = regmap_read(info->regmap, RT8973A_REG_DEV1, &dev1);
285         if (ret) {
286                 dev_err(info->dev, "failed to read DEV1 register\n");
287                 return ret;
288         }
289
290         switch (adc) {
291         case RT8973A_MUIC_ADC_OPEN:
292                 if (dev1 & RT8973A_REG_DEV1_USB_MASK)
293                         cable_type = RT8973A_MUIC_ADC_USB;
294                 else if (dev1 & RT8973A_REG_DEV1_DCPORT_MASK)
295                         cable_type = RT8973A_MUIC_ADC_TA;
296                 else
297                         cable_type = RT8973A_MUIC_ADC_OPEN;
298                 break;
299         default:
300                 break;
301         }
302
303         return cable_type;
304 }
305
306 static int rt8973a_muic_cable_handler(struct rt8973a_muic_info *info,
307                                         enum rt8973a_event_type event)
308 {
309         static unsigned int prev_cable_type;
310         const char **cable_names = info->edev->supported_cable;
311         unsigned int con_sw = DM_DP_SWITCH_UART;
312         int ret, idx = 0, cable_type;
313         bool attached = false;
314
315         if (!cable_names)
316                 return 0;
317
318         switch (event) {
319         case RT8973A_EVENT_ATTACH:
320                 cable_type = rt8973a_muic_get_cable_type(info);
321                 attached = true;
322                 break;
323         case RT8973A_EVENT_DETACH:
324                 cable_type = prev_cable_type;
325                 attached = false;
326                 break;
327         case RT8973A_EVENT_OVP:
328         case RT8973A_EVENT_OTP:
329                 dev_warn(info->dev,
330                         "happen Over %s issue. Need to disconnect all cables\n",
331                         event == RT8973A_EVENT_OVP ? "Voltage" : "Temperature");
332                 cable_type = prev_cable_type;
333                 attached = false;
334                 break;
335         default:
336                 dev_err(info->dev,
337                         "Cannot handle this event (event:%d)\n", event);
338                 return -EINVAL;
339         }
340         prev_cable_type = cable_type;
341
342         switch (cable_type) {
343         case RT8973A_MUIC_ADC_OTG:
344                 idx = EXTCON_CABLE_USB_HOST;
345                 con_sw = DM_DP_SWITCH_USB;
346                 break;
347         case RT8973A_MUIC_ADC_TA:
348                 idx = EXTCON_CABLE_TA;
349                 con_sw = DM_DP_SWITCH_OPEN;
350                 break;
351         case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
352         case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
353                 idx = EXTCON_CABLE_JIG;
354                 con_sw = DM_DP_SWITCH_USB;
355                 break;
356         case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
357         case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
358                 idx = EXTCON_CABLE_JIG;
359                 con_sw = DM_DP_SWITCH_UART;
360                 break;
361         case RT8973A_MUIC_ADC_USB:
362                 idx = EXTCON_CABLE_USB;
363                 con_sw = DM_DP_SWITCH_USB;
364                 break;
365         case RT8973A_MUIC_ADC_OPEN:
366                 return 0;
367         case RT8973A_MUIC_ADC_UNKNOWN_ACC_1:
368         case RT8973A_MUIC_ADC_UNKNOWN_ACC_2:
369         case RT8973A_MUIC_ADC_UNKNOWN_ACC_3:
370         case RT8973A_MUIC_ADC_UNKNOWN_ACC_4:
371         case RT8973A_MUIC_ADC_UNKNOWN_ACC_5:
372                 dev_warn(info->dev,
373                         "Unknown accessory type (adc:0x%x)\n", cable_type);
374                 return 0;
375         case RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON:
376         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON:
377         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON:
378         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON:
379         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON:
380         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON:
381         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON:
382         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON:
383         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON:
384         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON:
385         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON:
386         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON:
387         case RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON:
388         case RT8973A_MUIC_ADC_AUDIO_TYPE2:
389                 dev_warn(info->dev,
390                         "Audio device/button type (adc:0x%x)\n", cable_type);
391                 return 0;
392         case RT8973A_MUIC_ADC_RESERVED_ACC_1:
393         case RT8973A_MUIC_ADC_RESERVED_ACC_2:
394         case RT8973A_MUIC_ADC_RESERVED_ACC_3:
395         case RT8973A_MUIC_ADC_RESERVED_ACC_4:
396         case RT8973A_MUIC_ADC_RESERVED_ACC_5:
397         case RT8973A_MUIC_ADC_PHONE_POWERED_DEV:
398                 return 0;
399         default:
400                 dev_err(info->dev,
401                         "Cannot handle this cable_type (adc:0x%x)\n",
402                         cable_type);
403                 return -EINVAL;
404         }
405
406         /* Change internal hardware path(DM_CON/DP_CON) */
407         ret = rt8973a_muic_set_path(info, con_sw, attached);
408         if (ret < 0)
409                 return ret;
410
411         /* Change the state of external accessory */
412         extcon_set_cable_state(info->edev, cable_names[idx], attached);
413
414         return 0;
415 }
416
417 static void rt8973a_muic_irq_work(struct work_struct *work)
418 {
419         struct rt8973a_muic_info *info = container_of(work,
420                         struct rt8973a_muic_info, irq_work);
421         int ret = 0;
422
423         if (!info->edev)
424                 return;
425
426         mutex_lock(&info->mutex);
427
428         /* Detect attached or detached cables */
429         if (info->irq_attach) {
430                 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_ATTACH);
431                 info->irq_attach = false;
432         }
433
434         if (info->irq_detach) {
435                 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_DETACH);
436                 info->irq_detach = false;
437         }
438
439         if (info->irq_ovp) {
440                 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_OVP);
441                 info->irq_ovp = false;
442         }
443
444         if (info->irq_otp) {
445                 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_OTP);
446                 info->irq_otp = 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 static irqreturn_t rt8973a_muic_irq_handler(int irq, void *data)
456 {
457         struct rt8973a_muic_info *info = data;
458         int i, irq_type = -1;
459
460         for (i = 0; i < info->num_muic_irqs; i++)
461                 if (irq == info->muic_irqs[i].virq)
462                         irq_type = info->muic_irqs[i].irq;
463
464         switch (irq_type) {
465         case RT8973A_INT1_ATTACH:
466                 info->irq_attach = true;
467                 break;
468         case RT8973A_INT1_DETACH:
469                 info->irq_detach = true;
470                 break;
471         case RT8973A_INT1_OVP:
472                 info->irq_ovp = true;
473                 break;
474         case RT8973A_INT1_OTP:
475                 info->irq_otp = true;
476                 break;
477         case RT8973A_INT1_CHGDET:
478         case RT8973A_INT1_DCD_T:
479         case RT8973A_INT1_CONNECT:
480         case RT8973A_INT1_ADC_CHG:
481         case RT8973A_INT2_UVLO:
482         case RT8973A_INT2_POR:
483         case RT8973A_INT2_OTP_FET:
484         case RT8973A_INT2_OVP_FET:
485         case RT8973A_INT2_OCP_LATCH:
486         case RT8973A_INT2_OCP:
487         case RT8973A_INT2_OVP_OCP:
488         default:
489                 dev_dbg(info->dev,
490                         "Cannot handle this interrupt (%d)\n", irq_type);
491                 break;
492         }
493
494         schedule_work(&info->irq_work);
495
496         return IRQ_HANDLED;
497 }
498
499 static void rt8973a_muic_detect_cable_wq(struct work_struct *work)
500 {
501         struct rt8973a_muic_info *info = container_of(to_delayed_work(work),
502                                 struct rt8973a_muic_info, wq_detcable);
503         int ret;
504
505         /* Notify the state of connector cable or not  */
506         ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_ATTACH);
507         if (ret < 0)
508                 dev_warn(info->dev, "failed to detect cable state\n");
509 }
510
511 static void rt8973a_init_dev_type(struct rt8973a_muic_info *info)
512 {
513         unsigned int data, vendor_id, version_id;
514         int i, ret;
515
516         /* To test I2C, Print version_id and vendor_id of RT8973A */
517         ret = regmap_read(info->regmap, RT8973A_REG_DEVICE_ID, &data);
518         if (ret) {
519                 dev_err(info->dev,
520                         "failed to read DEVICE_ID register: %d\n", ret);
521                 return;
522         }
523
524         vendor_id = ((data & RT8973A_REG_DEVICE_ID_VENDOR_MASK) >>
525                                 RT8973A_REG_DEVICE_ID_VENDOR_SHIFT);
526         version_id = ((data & RT8973A_REG_DEVICE_ID_VERSION_MASK) >>
527                                 RT8973A_REG_DEVICE_ID_VERSION_SHIFT);
528
529         dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
530                             version_id, vendor_id);
531
532         /* Initiazle the register of RT8973A device to bring-up */
533         for (i = 0; i < info->num_reg_data; i++) {
534                 u8 reg = info->reg_data[i].reg;
535                 u8 mask = info->reg_data[i].mask;
536                 u8 val = 0;
537
538                 if (info->reg_data[i].invert)
539                         val = ~info->reg_data[i].val;
540                 else
541                         val = info->reg_data[i].val;
542
543                 regmap_update_bits(info->regmap, reg, mask, val);
544         }
545
546         /* Check whether RT8973A is auto swithcing mode or not */
547         ret = regmap_read(info->regmap, RT8973A_REG_CONTROL1, &data);
548         if (ret) {
549                 dev_err(info->dev,
550                         "failed to read CONTROL1 register: %d\n", ret);
551                 return;
552         }
553
554         data &= RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK;
555         if (data) {
556                 info->auto_config = true;
557                 dev_info(info->dev,
558                         "Enable Auto-configuration for internal path\n");
559         }
560 }
561
562 static int rt8973a_muic_i2c_probe(struct i2c_client *i2c,
563                                  const struct i2c_device_id *id)
564 {
565         struct device_node *np = i2c->dev.of_node;
566         struct rt8973a_muic_info *info;
567         int i, ret, irq_flags;
568
569         if (!np)
570                 return -EINVAL;
571
572         info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
573         if (!info)
574                 return -ENOMEM;
575         i2c_set_clientdata(i2c, info);
576
577         info->dev = &i2c->dev;
578         info->i2c = i2c;
579         info->irq = i2c->irq;
580         info->muic_irqs = rt8973a_muic_irqs;
581         info->num_muic_irqs = ARRAY_SIZE(rt8973a_muic_irqs);
582         info->reg_data = rt8973a_reg_data;
583         info->num_reg_data = ARRAY_SIZE(rt8973a_reg_data);
584
585         mutex_init(&info->mutex);
586
587         INIT_WORK(&info->irq_work, rt8973a_muic_irq_work);
588
589         info->regmap = devm_regmap_init_i2c(i2c, &rt8973a_muic_regmap_config);
590         if (IS_ERR(info->regmap)) {
591                 ret = PTR_ERR(info->regmap);
592                 dev_err(info->dev, "failed to allocate register map: %d\n",
593                                    ret);
594                 return ret;
595         }
596
597         /* Support irq domain for RT8973A MUIC device */
598         irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
599         ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0,
600                                   &rt8973a_muic_irq_chip, &info->irq_data);
601         if (ret != 0) {
602                 dev_err(info->dev, "failed to add irq_chip (irq:%d, err:%d)\n",
603                                     info->irq, ret);
604                 return ret;
605         }
606
607         for (i = 0; i < info->num_muic_irqs; i++) {
608                 struct muic_irq *muic_irq = &info->muic_irqs[i];
609                 unsigned int virq = 0;
610
611                 virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
612                 if (virq <= 0)
613                         return -EINVAL;
614                 muic_irq->virq = virq;
615
616                 ret = devm_request_threaded_irq(info->dev, virq, NULL,
617                                                 rt8973a_muic_irq_handler,
618                                                 IRQF_NO_SUSPEND,
619                                                 muic_irq->name, info);
620                 if (ret) {
621                         dev_err(info->dev,
622                                 "failed: irq request (IRQ: %d, error :%d)\n",
623                                 muic_irq->irq, ret);
624                         return ret;
625                 }
626         }
627
628         /* Allocate extcon device */
629         info->edev = devm_extcon_dev_allocate(info->dev, rt8973a_extcon_cable);
630         if (IS_ERR(info->edev)) {
631                 dev_err(info->dev, "failed to allocate memory for extcon\n");
632                 return -ENOMEM;
633         }
634
635         /* Register extcon device */
636         ret = devm_extcon_dev_register(info->dev, info->edev);
637         if (ret) {
638                 dev_err(info->dev, "failed to register extcon device\n");
639                 return ret;
640         }
641
642         /*
643          * Detect accessory after completing the initialization of platform
644          *
645          * - Use delayed workqueue to detect cable state and then
646          * notify cable state to notifiee/platform through uevent.
647          * After completing the booting of platform, the extcon provider
648          * driver should notify cable state to upper layer.
649          */
650         INIT_DELAYED_WORK(&info->wq_detcable, rt8973a_muic_detect_cable_wq);
651         queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
652                         msecs_to_jiffies(DELAY_MS_DEFAULT));
653
654         /* Initialize RT8973A device and print vendor id and version id */
655         rt8973a_init_dev_type(info);
656
657         return 0;
658 }
659
660 static int rt8973a_muic_i2c_remove(struct i2c_client *i2c)
661 {
662         struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
663
664         regmap_del_irq_chip(info->irq, info->irq_data);
665
666         return 0;
667 }
668
669 static const struct of_device_id rt8973a_dt_match[] = {
670         { .compatible = "richtek,rt8973a-muic" },
671         { },
672 };
673
674 #ifdef CONFIG_PM_SLEEP
675 static int rt8973a_muic_suspend(struct device *dev)
676 {
677         struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
678         struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
679
680         enable_irq_wake(info->irq);
681
682         return 0;
683 }
684
685 static int rt8973a_muic_resume(struct device *dev)
686 {
687         struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
688         struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
689
690         disable_irq_wake(info->irq);
691
692         return 0;
693 }
694 #endif
695
696 static SIMPLE_DEV_PM_OPS(rt8973a_muic_pm_ops,
697                          rt8973a_muic_suspend, rt8973a_muic_resume);
698
699 static const struct i2c_device_id rt8973a_i2c_id[] = {
700         { "rt8973a", TYPE_RT8973A },
701         { }
702 };
703 MODULE_DEVICE_TABLE(i2c, rt8973a_i2c_id);
704
705 static struct i2c_driver rt8973a_muic_i2c_driver = {
706         .driver         = {
707                 .name   = "rt8973a",
708                 .owner  = THIS_MODULE,
709                 .pm     = &rt8973a_muic_pm_ops,
710                 .of_match_table = rt8973a_dt_match,
711         },
712         .probe  = rt8973a_muic_i2c_probe,
713         .remove = rt8973a_muic_i2c_remove,
714         .id_table = rt8973a_i2c_id,
715 };
716
717 static int __init rt8973a_muic_i2c_init(void)
718 {
719         return i2c_add_driver(&rt8973a_muic_i2c_driver);
720 }
721 subsys_initcall(rt8973a_muic_i2c_init);
722
723 MODULE_DESCRIPTION("Richtek RT8973A Extcon driver");
724 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
725 MODULE_LICENSE("GPL");