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