staging: unisys: Use kzalloc instead of kmalloc/memset
[cascardo/linux.git] / drivers / extcon / extcon-max77843.c
1 /*
2  * extcon-max77843.c - Maxim MAX77843 extcon driver to support
3  *                      MUIC(Micro USB Interface Controller)
4  *
5  * Copyright (C) 2015 Samsung Electronics
6  * Author: Jaewon Kim <jaewon02.kim@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/extcon.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/max77843-private.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/workqueue.h>
22
23 #define DELAY_MS_DEFAULT                15000   /* unit: millisecond */
24
25 enum max77843_muic_status {
26         MAX77843_MUIC_STATUS1 = 0,
27         MAX77843_MUIC_STATUS2,
28         MAX77843_MUIC_STATUS3,
29
30         MAX77843_MUIC_STATUS_NUM,
31 };
32
33 struct max77843_muic_info {
34         struct device *dev;
35         struct max77843 *max77843;
36         struct extcon_dev *edev;
37
38         struct mutex mutex;
39         struct work_struct irq_work;
40         struct delayed_work wq_detcable;
41
42         u8 status[MAX77843_MUIC_STATUS_NUM];
43         int prev_cable_type;
44         int prev_chg_type;
45         int prev_gnd_type;
46
47         bool irq_adc;
48         bool irq_chg;
49 };
50
51 enum max77843_muic_cable_group {
52         MAX77843_CABLE_GROUP_ADC = 0,
53         MAX77843_CABLE_GROUP_ADC_GND,
54         MAX77843_CABLE_GROUP_CHG,
55 };
56
57 enum max77843_muic_adc_debounce_time {
58         MAX77843_DEBOUNCE_TIME_5MS = 0,
59         MAX77843_DEBOUNCE_TIME_10MS,
60         MAX77843_DEBOUNCE_TIME_25MS,
61         MAX77843_DEBOUNCE_TIME_38_62MS,
62 };
63
64 /* Define accessory cable type */
65 enum max77843_muic_accessory_type {
66         MAX77843_MUIC_ADC_GROUND = 0,
67         MAX77843_MUIC_ADC_SEND_END_BUTTON,
68         MAX77843_MUIC_ADC_REMOTE_S1_BUTTON,
69         MAX77843_MUIC_ADC_REMOTE_S2_BUTTON,
70         MAX77843_MUIC_ADC_REMOTE_S3_BUTTON,
71         MAX77843_MUIC_ADC_REMOTE_S4_BUTTON,
72         MAX77843_MUIC_ADC_REMOTE_S5_BUTTON,
73         MAX77843_MUIC_ADC_REMOTE_S6_BUTTON,
74         MAX77843_MUIC_ADC_REMOTE_S7_BUTTON,
75         MAX77843_MUIC_ADC_REMOTE_S8_BUTTON,
76         MAX77843_MUIC_ADC_REMOTE_S9_BUTTON,
77         MAX77843_MUIC_ADC_REMOTE_S10_BUTTON,
78         MAX77843_MUIC_ADC_REMOTE_S11_BUTTON,
79         MAX77843_MUIC_ADC_REMOTE_S12_BUTTON,
80         MAX77843_MUIC_ADC_RESERVED_ACC_1,
81         MAX77843_MUIC_ADC_RESERVED_ACC_2,
82         MAX77843_MUIC_ADC_RESERVED_ACC_3,
83         MAX77843_MUIC_ADC_RESERVED_ACC_4,
84         MAX77843_MUIC_ADC_RESERVED_ACC_5,
85         MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2,
86         MAX77843_MUIC_ADC_PHONE_POWERED_DEV,
87         MAX77843_MUIC_ADC_TTY_CONVERTER,
88         MAX77843_MUIC_ADC_UART_CABLE,
89         MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG,
90         MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF,
91         MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON,
92         MAX77843_MUIC_ADC_AV_CABLE_NOLOAD,
93         MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG,
94         MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF,
95         MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON,
96         MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1,
97         MAX77843_MUIC_ADC_OPEN,
98
99         /* The blow accessories should check
100            not only ADC value but also ADC1K and VBVolt value. */
101                                                 /* Offset|ADC1K|VBVolt| */
102         MAX77843_MUIC_GND_USB_HOST = 0x100,     /*    0x1|    0|     0| */
103         MAX77843_MUIC_GND_USB_HOST_VB = 0x101,  /*    0x1|    0|     1| */
104         MAX77843_MUIC_GND_MHL = 0x102,          /*    0x1|    1|     0| */
105         MAX77843_MUIC_GND_MHL_VB = 0x103,       /*    0x1|    1|     1| */
106 };
107
108 /* Define charger cable type */
109 enum max77843_muic_charger_type {
110         MAX77843_MUIC_CHG_NONE = 0,
111         MAX77843_MUIC_CHG_USB,
112         MAX77843_MUIC_CHG_DOWNSTREAM,
113         MAX77843_MUIC_CHG_DEDICATED,
114         MAX77843_MUIC_CHG_SPECIAL_500MA,
115         MAX77843_MUIC_CHG_SPECIAL_1A,
116         MAX77843_MUIC_CHG_SPECIAL_BIAS,
117         MAX77843_MUIC_CHG_RESERVED,
118         MAX77843_MUIC_CHG_GND,
119 };
120
121 static const unsigned int max77843_extcon_cable[] = {
122         EXTCON_USB,
123         EXTCON_USB_HOST,
124         EXTCON_TA,
125         EXTCON_CHARGE_DOWNSTREAM,
126         EXTCON_FAST_CHARGER,
127         EXTCON_SLOW_CHARGER,
128         EXTCON_MHL,
129         EXTCON_JIG,
130         EXTCON_NONE,
131 };
132
133 struct max77843_muic_irq {
134         unsigned int irq;
135         const char *name;
136         unsigned int virq;
137 };
138
139 static struct max77843_muic_irq max77843_muic_irqs[] = {
140         { MAX77843_MUIC_IRQ_INT1_ADC,           "MUIC-ADC" },
141         { MAX77843_MUIC_IRQ_INT1_ADCERROR,      "MUIC-ADC_ERROR" },
142         { MAX77843_MUIC_IRQ_INT1_ADC1K,         "MUIC-ADC1K" },
143         { MAX77843_MUIC_IRQ_INT2_CHGTYP,        "MUIC-CHGTYP" },
144         { MAX77843_MUIC_IRQ_INT2_CHGDETRUN,     "MUIC-CHGDETRUN" },
145         { MAX77843_MUIC_IRQ_INT2_DCDTMR,        "MUIC-DCDTMR" },
146         { MAX77843_MUIC_IRQ_INT2_DXOVP,         "MUIC-DXOVP" },
147         { MAX77843_MUIC_IRQ_INT2_VBVOLT,        "MUIC-VBVOLT" },
148         { MAX77843_MUIC_IRQ_INT3_VBADC,         "MUIC-VBADC" },
149         { MAX77843_MUIC_IRQ_INT3_VDNMON,        "MUIC-VDNMON" },
150         { MAX77843_MUIC_IRQ_INT3_DNRES,         "MUIC-DNRES" },
151         { MAX77843_MUIC_IRQ_INT3_MPNACK,        "MUIC-MPNACK"},
152         { MAX77843_MUIC_IRQ_INT3_MRXBUFOW,      "MUIC-MRXBUFOW"},
153         { MAX77843_MUIC_IRQ_INT3_MRXTRF,        "MUIC-MRXTRF"},
154         { MAX77843_MUIC_IRQ_INT3_MRXPERR,       "MUIC-MRXPERR"},
155         { MAX77843_MUIC_IRQ_INT3_MRXRDY,        "MUIC-MRXRDY"},
156 };
157
158 static const struct regmap_config max77843_muic_regmap_config = {
159         .reg_bits       = 8,
160         .val_bits       = 8,
161         .max_register   = MAX77843_MUIC_REG_END,
162 };
163
164 static const struct regmap_irq max77843_muic_irq[] = {
165         /* INT1 interrupt */
166         { .reg_offset = 0, .mask = MAX77843_MUIC_ADC, },
167         { .reg_offset = 0, .mask = MAX77843_MUIC_ADCERROR, },
168         { .reg_offset = 0, .mask = MAX77843_MUIC_ADC1K, },
169
170         /* INT2 interrupt */
171         { .reg_offset = 1, .mask = MAX77843_MUIC_CHGTYP, },
172         { .reg_offset = 1, .mask = MAX77843_MUIC_CHGDETRUN, },
173         { .reg_offset = 1, .mask = MAX77843_MUIC_DCDTMR, },
174         { .reg_offset = 1, .mask = MAX77843_MUIC_DXOVP, },
175         { .reg_offset = 1, .mask = MAX77843_MUIC_VBVOLT, },
176
177         /* INT3 interrupt */
178         { .reg_offset = 2, .mask = MAX77843_MUIC_VBADC, },
179         { .reg_offset = 2, .mask = MAX77843_MUIC_VDNMON, },
180         { .reg_offset = 2, .mask = MAX77843_MUIC_DNRES, },
181         { .reg_offset = 2, .mask = MAX77843_MUIC_MPNACK, },
182         { .reg_offset = 2, .mask = MAX77843_MUIC_MRXBUFOW, },
183         { .reg_offset = 2, .mask = MAX77843_MUIC_MRXTRF, },
184         { .reg_offset = 2, .mask = MAX77843_MUIC_MRXPERR, },
185         { .reg_offset = 2, .mask = MAX77843_MUIC_MRXRDY, },
186 };
187
188 static const struct regmap_irq_chip max77843_muic_irq_chip = {
189         .name           = "max77843-muic",
190         .status_base    = MAX77843_MUIC_REG_INT1,
191         .mask_base      = MAX77843_MUIC_REG_INTMASK1,
192         .mask_invert    = true,
193         .num_regs       = 3,
194         .irqs           = max77843_muic_irq,
195         .num_irqs       = ARRAY_SIZE(max77843_muic_irq),
196 };
197
198 static int max77843_muic_set_path(struct max77843_muic_info *info,
199                 u8 val, bool attached)
200 {
201         struct max77843 *max77843 = info->max77843;
202         int ret = 0;
203         unsigned int ctrl1, ctrl2;
204
205         if (attached)
206                 ctrl1 = val;
207         else
208                 ctrl1 = CONTROL1_SW_OPEN;
209
210         ret = regmap_update_bits(max77843->regmap_muic,
211                         MAX77843_MUIC_REG_CONTROL1,
212                         CONTROL1_COM_SW, ctrl1);
213         if (ret < 0) {
214                 dev_err(info->dev, "Cannot switch MUIC port\n");
215                 return ret;
216         }
217
218         if (attached)
219                 ctrl2 = MAX77843_MUIC_CONTROL2_CPEN_MASK;
220         else
221                 ctrl2 = MAX77843_MUIC_CONTROL2_LOWPWR_MASK;
222
223         ret = regmap_update_bits(max77843->regmap_muic,
224                         MAX77843_MUIC_REG_CONTROL2,
225                         MAX77843_MUIC_CONTROL2_LOWPWR_MASK |
226                         MAX77843_MUIC_CONTROL2_CPEN_MASK, ctrl2);
227         if (ret < 0) {
228                 dev_err(info->dev, "Cannot update lowpower mode\n");
229                 return ret;
230         }
231
232         dev_dbg(info->dev,
233                 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
234                 ctrl1, ctrl2, attached ? "attached" : "detached");
235
236         return 0;
237 }
238
239 static int max77843_muic_get_cable_type(struct max77843_muic_info *info,
240                 enum max77843_muic_cable_group group, bool *attached)
241 {
242         int adc, chg_type, cable_type, gnd_type;
243
244         adc = info->status[MAX77843_MUIC_STATUS1] &
245                         MAX77843_MUIC_STATUS1_ADC_MASK;
246         adc >>= STATUS1_ADC_SHIFT;
247
248         switch (group) {
249         case MAX77843_CABLE_GROUP_ADC:
250                 if (adc == MAX77843_MUIC_ADC_OPEN) {
251                         *attached = false;
252                         cable_type = info->prev_cable_type;
253                         info->prev_cable_type = MAX77843_MUIC_ADC_OPEN;
254                 } else {
255                         *attached = true;
256                         cable_type = info->prev_cable_type = adc;
257                 }
258                 break;
259         case MAX77843_CABLE_GROUP_CHG:
260                 chg_type = info->status[MAX77843_MUIC_STATUS2] &
261                                 MAX77843_MUIC_STATUS2_CHGTYP_MASK;
262
263                 /* Check GROUND accessory with charger cable */
264                 if (adc == MAX77843_MUIC_ADC_GROUND) {
265                         if (chg_type == MAX77843_MUIC_CHG_NONE) {
266                                 /* The following state when charger cable is
267                                  * disconnected but the GROUND accessory still
268                                  * connected */
269                                 *attached = false;
270                                 cable_type = info->prev_chg_type;
271                                 info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
272                         } else {
273
274                                 /* The following state when charger cable is
275                                  * connected on the GROUND accessory */
276                                 *attached = true;
277                                 cable_type = MAX77843_MUIC_CHG_GND;
278                                 info->prev_chg_type = MAX77843_MUIC_CHG_GND;
279                         }
280                         break;
281                 }
282
283                 if (chg_type == MAX77843_MUIC_CHG_NONE) {
284                         *attached = false;
285                         cable_type = info->prev_chg_type;
286                         info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
287                 } else {
288                         *attached = true;
289                         cable_type = info->prev_chg_type = chg_type;
290                 }
291                 break;
292         case MAX77843_CABLE_GROUP_ADC_GND:
293                 if (adc == MAX77843_MUIC_ADC_OPEN) {
294                         *attached = false;
295                         cable_type = info->prev_gnd_type;
296                         info->prev_gnd_type = MAX77843_MUIC_ADC_OPEN;
297                 } else {
298                         *attached = true;
299
300                         /* Offset|ADC1K|VBVolt|
301                          *    0x1|    0|     0| USB-HOST
302                          *    0x1|    0|     1| USB-HOST with VB
303                          *    0x1|    1|     0| MHL
304                          *    0x1|    1|     1| MHL with VB */
305                         /* Get ADC1K register bit */
306                         gnd_type = (info->status[MAX77843_MUIC_STATUS1] &
307                                         MAX77843_MUIC_STATUS1_ADC1K_MASK);
308
309                         /* Get VBVolt register bit */
310                         gnd_type |= (info->status[MAX77843_MUIC_STATUS2] &
311                                         MAX77843_MUIC_STATUS2_VBVOLT_MASK);
312                         gnd_type >>= STATUS2_VBVOLT_SHIFT;
313
314                         /* Offset of GND cable */
315                         gnd_type |= MAX77843_MUIC_GND_USB_HOST;
316                         cable_type = info->prev_gnd_type = gnd_type;
317                 }
318                 break;
319         default:
320                 dev_err(info->dev, "Unknown cable group (%d)\n", group);
321                 cable_type = -EINVAL;
322                 break;
323         }
324
325         return cable_type;
326 }
327
328 static int max77843_muic_adc_gnd_handler(struct max77843_muic_info *info)
329 {
330         int ret, gnd_cable_type;
331         bool attached;
332
333         gnd_cable_type = max77843_muic_get_cable_type(info,
334                         MAX77843_CABLE_GROUP_ADC_GND, &attached);
335         dev_dbg(info->dev, "external connector is %s (gnd:0x%02x)\n",
336                         attached ? "attached" : "detached", gnd_cable_type);
337
338         switch (gnd_cable_type) {
339         case MAX77843_MUIC_GND_USB_HOST:
340         case MAX77843_MUIC_GND_USB_HOST_VB:
341                 ret = max77843_muic_set_path(info, CONTROL1_SW_USB, attached);
342                 if (ret < 0)
343                         return ret;
344
345                 extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, attached);
346                 break;
347         case MAX77843_MUIC_GND_MHL_VB:
348         case MAX77843_MUIC_GND_MHL:
349                 ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
350                 if (ret < 0)
351                         return ret;
352
353                 extcon_set_cable_state_(info->edev, EXTCON_MHL, attached);
354                 break;
355         default:
356                 dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n",
357                         attached ? "attached" : "detached", gnd_cable_type);
358                 return -EINVAL;
359         }
360
361         return 0;
362 }
363
364 static int max77843_muic_jig_handler(struct max77843_muic_info *info,
365                 int cable_type, bool attached)
366 {
367         int ret;
368         u8 path = CONTROL1_SW_OPEN;
369
370         dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n",
371                         attached ? "attached" : "detached", cable_type);
372
373         switch (cable_type) {
374         case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
375         case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
376                 path = CONTROL1_SW_USB;
377                 break;
378         case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
379                 path = CONTROL1_SW_UART;
380                 break;
381         default:
382                 return -EINVAL;
383         }
384
385         ret = max77843_muic_set_path(info, path, attached);
386         if (ret < 0)
387                 return ret;
388
389         extcon_set_cable_state_(info->edev, EXTCON_JIG, attached);
390
391         return 0;
392 }
393
394 static int max77843_muic_adc_handler(struct max77843_muic_info *info)
395 {
396         int ret, cable_type;
397         bool attached;
398
399         cable_type = max77843_muic_get_cable_type(info,
400                         MAX77843_CABLE_GROUP_ADC, &attached);
401
402         dev_dbg(info->dev,
403                 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
404                 attached ? "attached" : "detached", cable_type,
405                 info->prev_cable_type);
406
407         switch (cable_type) {
408         case MAX77843_MUIC_ADC_GROUND:
409                 ret = max77843_muic_adc_gnd_handler(info);
410                 if (ret < 0)
411                         return ret;
412                 break;
413         case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
414         case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
415         case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
416                 ret = max77843_muic_jig_handler(info, cable_type, attached);
417                 if (ret < 0)
418                         return ret;
419                 break;
420         case MAX77843_MUIC_ADC_SEND_END_BUTTON:
421         case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON:
422         case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON:
423         case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON:
424         case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON:
425         case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON:
426         case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON:
427         case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON:
428         case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON:
429         case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON:
430         case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON:
431         case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON:
432         case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON:
433         case MAX77843_MUIC_ADC_RESERVED_ACC_1:
434         case MAX77843_MUIC_ADC_RESERVED_ACC_2:
435         case MAX77843_MUIC_ADC_RESERVED_ACC_3:
436         case MAX77843_MUIC_ADC_RESERVED_ACC_4:
437         case MAX77843_MUIC_ADC_RESERVED_ACC_5:
438         case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2:
439         case MAX77843_MUIC_ADC_PHONE_POWERED_DEV:
440         case MAX77843_MUIC_ADC_TTY_CONVERTER:
441         case MAX77843_MUIC_ADC_UART_CABLE:
442         case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG:
443         case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD:
444         case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG:
445         case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON:
446         case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1:
447         case MAX77843_MUIC_ADC_OPEN:
448                 dev_err(info->dev,
449                         "accessory is %s but it isn't used (adc:0x%x)\n",
450                         attached ? "attached" : "detached", cable_type);
451                 return -EAGAIN;
452         default:
453                 dev_err(info->dev,
454                         "failed to detect %s accessory (adc:0x%x)\n",
455                         attached ? "attached" : "detached", cable_type);
456                 return -EINVAL;
457         }
458
459         return 0;
460 }
461
462 static int max77843_muic_chg_handler(struct max77843_muic_info *info)
463 {
464         int ret, chg_type, gnd_type;
465         bool attached;
466
467         chg_type = max77843_muic_get_cable_type(info,
468                         MAX77843_CABLE_GROUP_CHG, &attached);
469
470         dev_dbg(info->dev,
471                 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
472                 attached ? "attached" : "detached",
473                 chg_type, info->prev_chg_type);
474
475         switch (chg_type) {
476         case MAX77843_MUIC_CHG_USB:
477                 ret = max77843_muic_set_path(info, CONTROL1_SW_USB, attached);
478                 if (ret < 0)
479                         return ret;
480
481                 extcon_set_cable_state_(info->edev, EXTCON_USB, attached);
482                 break;
483         case MAX77843_MUIC_CHG_DOWNSTREAM:
484                 ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
485                 if (ret < 0)
486                         return ret;
487
488                 extcon_set_cable_state_(info->edev, EXTCON_CHARGE_DOWNSTREAM,
489                                         attached);
490                 break;
491         case MAX77843_MUIC_CHG_DEDICATED:
492                 ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
493                 if (ret < 0)
494                         return ret;
495
496                 extcon_set_cable_state_(info->edev, EXTCON_TA, attached);
497                 break;
498         case MAX77843_MUIC_CHG_SPECIAL_500MA:
499                 ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
500                 if (ret < 0)
501                         return ret;
502
503                 extcon_set_cable_state_(info->edev, EXTCON_SLOW_CHARGER,
504                                         attached);
505                 break;
506         case MAX77843_MUIC_CHG_SPECIAL_1A:
507                 ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
508                 if (ret < 0)
509                         return ret;
510
511                 extcon_set_cable_state_(info->edev, EXTCON_FAST_CHARGER,
512                                         attached);
513                 break;
514         case MAX77843_MUIC_CHG_GND:
515                 gnd_type = max77843_muic_get_cable_type(info,
516                                 MAX77843_CABLE_GROUP_ADC_GND, &attached);
517
518                 /* Charger cable on MHL accessory is attach or detach */
519                 if (gnd_type == MAX77843_MUIC_GND_MHL_VB)
520                         extcon_set_cable_state_(info->edev, EXTCON_TA, true);
521                 else if (gnd_type == MAX77843_MUIC_GND_MHL)
522                         extcon_set_cable_state_(info->edev, EXTCON_TA, false);
523                 break;
524         case MAX77843_MUIC_CHG_NONE:
525                 break;
526         default:
527                 dev_err(info->dev,
528                         "failed to detect %s accessory (chg_type:0x%x)\n",
529                         attached ? "attached" : "detached", chg_type);
530
531                 max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
532                 return -EINVAL;
533         }
534
535         return 0;
536 }
537
538 static void max77843_muic_irq_work(struct work_struct *work)
539 {
540         struct max77843_muic_info *info = container_of(work,
541                         struct max77843_muic_info, irq_work);
542         struct max77843 *max77843 = info->max77843;
543         int ret = 0;
544
545         mutex_lock(&info->mutex);
546
547         ret = regmap_bulk_read(max77843->regmap_muic,
548                         MAX77843_MUIC_REG_STATUS1, info->status,
549                         MAX77843_MUIC_STATUS_NUM);
550         if (ret) {
551                 dev_err(info->dev, "Cannot read STATUS registers\n");
552                 mutex_unlock(&info->mutex);
553                 return;
554         }
555
556         if (info->irq_adc) {
557                 ret = max77843_muic_adc_handler(info);
558                 if (ret)
559                         dev_err(info->dev, "Unknown cable type\n");
560                 info->irq_adc = false;
561         }
562
563         if (info->irq_chg) {
564                 ret = max77843_muic_chg_handler(info);
565                 if (ret)
566                         dev_err(info->dev, "Unknown charger type\n");
567                 info->irq_chg = false;
568         }
569
570         mutex_unlock(&info->mutex);
571 }
572
573 static irqreturn_t max77843_muic_irq_handler(int irq, void *data)
574 {
575         struct max77843_muic_info *info = data;
576         int i, irq_type = -1;
577
578         for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++)
579                 if (irq == max77843_muic_irqs[i].virq)
580                         irq_type = max77843_muic_irqs[i].irq;
581
582         switch (irq_type) {
583         case MAX77843_MUIC_IRQ_INT1_ADC:
584         case MAX77843_MUIC_IRQ_INT1_ADCERROR:
585         case MAX77843_MUIC_IRQ_INT1_ADC1K:
586                 info->irq_adc = true;
587                 break;
588         case MAX77843_MUIC_IRQ_INT2_CHGTYP:
589         case MAX77843_MUIC_IRQ_INT2_CHGDETRUN:
590         case MAX77843_MUIC_IRQ_INT2_DCDTMR:
591         case MAX77843_MUIC_IRQ_INT2_DXOVP:
592         case MAX77843_MUIC_IRQ_INT2_VBVOLT:
593                 info->irq_chg = true;
594                 break;
595         case MAX77843_MUIC_IRQ_INT3_VBADC:
596         case MAX77843_MUIC_IRQ_INT3_VDNMON:
597         case MAX77843_MUIC_IRQ_INT3_DNRES:
598         case MAX77843_MUIC_IRQ_INT3_MPNACK:
599         case MAX77843_MUIC_IRQ_INT3_MRXBUFOW:
600         case MAX77843_MUIC_IRQ_INT3_MRXTRF:
601         case MAX77843_MUIC_IRQ_INT3_MRXPERR:
602         case MAX77843_MUIC_IRQ_INT3_MRXRDY:
603                 break;
604         default:
605                 dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type);
606                 break;
607         }
608
609         schedule_work(&info->irq_work);
610
611         return IRQ_HANDLED;
612 }
613
614 static void max77843_muic_detect_cable_wq(struct work_struct *work)
615 {
616         struct max77843_muic_info *info = container_of(to_delayed_work(work),
617                         struct max77843_muic_info, wq_detcable);
618         struct max77843 *max77843 = info->max77843;
619         int chg_type, adc, ret;
620         bool attached;
621
622         mutex_lock(&info->mutex);
623
624         ret = regmap_bulk_read(max77843->regmap_muic,
625                         MAX77843_MUIC_REG_STATUS1, info->status,
626                         MAX77843_MUIC_STATUS_NUM);
627         if (ret) {
628                 dev_err(info->dev, "Cannot read STATUS registers\n");
629                 goto err_cable_wq;
630         }
631
632         adc = max77843_muic_get_cable_type(info,
633                         MAX77843_CABLE_GROUP_ADC, &attached);
634         if (attached && adc != MAX77843_MUIC_ADC_OPEN) {
635                 ret = max77843_muic_adc_handler(info);
636                 if (ret < 0) {
637                         dev_err(info->dev, "Cannot detect accessory\n");
638                         goto err_cable_wq;
639                 }
640         }
641
642         chg_type = max77843_muic_get_cable_type(info,
643                         MAX77843_CABLE_GROUP_CHG, &attached);
644         if (attached && chg_type != MAX77843_MUIC_CHG_NONE) {
645                 ret = max77843_muic_chg_handler(info);
646                 if (ret < 0) {
647                         dev_err(info->dev, "Cannot detect charger accessory\n");
648                         goto err_cable_wq;
649                 }
650         }
651
652 err_cable_wq:
653         mutex_unlock(&info->mutex);
654 }
655
656 static int max77843_muic_set_debounce_time(struct max77843_muic_info *info,
657                 enum max77843_muic_adc_debounce_time time)
658 {
659         struct max77843 *max77843 = info->max77843;
660         int ret;
661
662         switch (time) {
663         case MAX77843_DEBOUNCE_TIME_5MS:
664         case MAX77843_DEBOUNCE_TIME_10MS:
665         case MAX77843_DEBOUNCE_TIME_25MS:
666         case MAX77843_DEBOUNCE_TIME_38_62MS:
667                 ret = regmap_update_bits(max77843->regmap_muic,
668                                 MAX77843_MUIC_REG_CONTROL4,
669                                 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK,
670                                 time << CONTROL4_ADCDBSET_SHIFT);
671                 if (ret < 0) {
672                         dev_err(info->dev, "Cannot write MUIC regmap\n");
673                         return ret;
674                 }
675                 break;
676         default:
677                 dev_err(info->dev, "Invalid ADC debounce time\n");
678                 return -EINVAL;
679         }
680
681         return 0;
682 }
683
684 static int max77843_init_muic_regmap(struct max77843 *max77843)
685 {
686         int ret;
687
688         max77843->i2c_muic = i2c_new_dummy(max77843->i2c->adapter,
689                         I2C_ADDR_MUIC);
690         if (!max77843->i2c_muic) {
691                 dev_err(&max77843->i2c->dev,
692                                 "Cannot allocate I2C device for MUIC\n");
693                 return -ENOMEM;
694         }
695
696         i2c_set_clientdata(max77843->i2c_muic, max77843);
697
698         max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic,
699                         &max77843_muic_regmap_config);
700         if (IS_ERR(max77843->regmap_muic)) {
701                 ret = PTR_ERR(max77843->regmap_muic);
702                 goto err_muic_i2c;
703         }
704
705         ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq,
706                         IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
707                         0, &max77843_muic_irq_chip, &max77843->irq_data_muic);
708         if (ret < 0) {
709                 dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n");
710                 goto err_muic_i2c;
711         }
712
713         return 0;
714
715 err_muic_i2c:
716         i2c_unregister_device(max77843->i2c_muic);
717
718         return ret;
719 }
720
721 static int max77843_muic_probe(struct platform_device *pdev)
722 {
723         struct max77843 *max77843 = dev_get_drvdata(pdev->dev.parent);
724         struct max77843_muic_info *info;
725         unsigned int id;
726         int i, ret;
727
728         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
729         if (!info)
730                 return -ENOMEM;
731
732         info->dev = &pdev->dev;
733         info->max77843 = max77843;
734
735         platform_set_drvdata(pdev, info);
736         mutex_init(&info->mutex);
737
738         /* Initialize i2c and regmap */
739         ret = max77843_init_muic_regmap(max77843);
740         if (ret) {
741                 dev_err(&pdev->dev, "Failed to init MUIC regmap\n");
742                 return ret;
743         }
744
745         /* Turn off auto detection configuration */
746         ret = regmap_update_bits(max77843->regmap_muic,
747                         MAX77843_MUIC_REG_CONTROL4,
748                         MAX77843_MUIC_CONTROL4_USBAUTO_MASK |
749                         MAX77843_MUIC_CONTROL4_FCTAUTO_MASK,
750                         CONTROL4_AUTO_DISABLE);
751
752         /* Initialize extcon device */
753         info->edev = devm_extcon_dev_allocate(&pdev->dev,
754                         max77843_extcon_cable);
755         if (IS_ERR(info->edev)) {
756                 dev_err(&pdev->dev, "Failed to allocate memory for extcon\n");
757                 ret = -ENODEV;
758                 goto err_muic_irq;
759         }
760
761         ret = devm_extcon_dev_register(&pdev->dev, info->edev);
762         if (ret) {
763                 dev_err(&pdev->dev, "Failed to register extcon device\n");
764                 goto err_muic_irq;
765         }
766
767         /* Set ADC debounce time */
768         max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS);
769
770         /* Set initial path for UART */
771         max77843_muic_set_path(info, CONTROL1_SW_UART, true);
772
773         /* Check revision number of MUIC device */
774         ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id);
775         if (ret < 0) {
776                 dev_err(&pdev->dev, "Failed to read revision number\n");
777                 goto err_muic_irq;
778         }
779         dev_info(info->dev, "MUIC device ID : 0x%x\n", id);
780
781         /* Support virtual irq domain for max77843 MUIC device */
782         INIT_WORK(&info->irq_work, max77843_muic_irq_work);
783
784         for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) {
785                 struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i];
786                 unsigned int virq = 0;
787
788                 virq = regmap_irq_get_virq(max77843->irq_data_muic,
789                                 muic_irq->irq);
790                 if (virq <= 0) {
791                         ret = -EINVAL;
792                         goto err_muic_irq;
793                 }
794                 muic_irq->virq = virq;
795
796                 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
797                                 max77843_muic_irq_handler, IRQF_NO_SUSPEND,
798                                 muic_irq->name, info);
799                 if (ret) {
800                         dev_err(&pdev->dev,
801                                 "Failed to request irq (IRQ: %d, error: %d)\n",
802                                 muic_irq->irq, ret);
803                         goto err_muic_irq;
804                 }
805         }
806
807         /* Detect accessory after completing the initialization of platform */
808         INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq);
809         queue_delayed_work(system_power_efficient_wq,
810                         &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT));
811
812         return 0;
813
814 err_muic_irq:
815         regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
816         i2c_unregister_device(max77843->i2c_muic);
817
818         return ret;
819 }
820
821 static int max77843_muic_remove(struct platform_device *pdev)
822 {
823         struct max77843_muic_info *info = platform_get_drvdata(pdev);
824         struct max77843 *max77843 = info->max77843;
825
826         cancel_work_sync(&info->irq_work);
827         regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
828         i2c_unregister_device(max77843->i2c_muic);
829
830         return 0;
831 }
832
833 static const struct platform_device_id max77843_muic_id[] = {
834         { "max77843-muic", },
835         { /* sentinel */ },
836 };
837 MODULE_DEVICE_TABLE(platform, max77843_muic_id);
838
839 static struct platform_driver max77843_muic_driver = {
840         .driver         = {
841                 .name           = "max77843-muic",
842         },
843         .probe          = max77843_muic_probe,
844         .remove         = max77843_muic_remove,
845         .id_table       = max77843_muic_id,
846 };
847
848 static int __init max77843_muic_init(void)
849 {
850         return platform_driver_register(&max77843_muic_driver);
851 }
852 subsys_initcall(max77843_muic_init);
853
854 MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
855 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
856 MODULE_LICENSE("GPL");