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