]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/extcon/extcon-max77693.c
28eff88fca1823ce85791f356ba2658ef32df888
[karo-tx-linux.git] / drivers / extcon / extcon-max77693.c
1 /*
2  * extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC
3  *
4  * Copyright (C) 2012 Samsung Electrnoics
5  * Chanwoo Choi <cw00.choi@samsung.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/i2c.h>
21 #include <linux/slab.h>
22 #include <linux/input.h>
23 #include <linux/interrupt.h>
24 #include <linux/err.h>
25 #include <linux/platform_device.h>
26 #include <linux/mfd/max77693.h>
27 #include <linux/mfd/max77693-private.h>
28 #include <linux/extcon.h>
29 #include <linux/regmap.h>
30 #include <linux/irqdomain.h>
31
32 #define DEV_NAME                        "max77693-muic"
33 #define DELAY_MS_DEFAULT                20000           /* unit: millisecond */
34
35 enum max77693_muic_adc_debounce_time {
36         ADC_DEBOUNCE_TIME_5MS = 0,
37         ADC_DEBOUNCE_TIME_10MS,
38         ADC_DEBOUNCE_TIME_25MS,
39         ADC_DEBOUNCE_TIME_38_62MS,
40 };
41
42 struct max77693_muic_info {
43         struct device *dev;
44         struct max77693_dev *max77693;
45         struct extcon_dev *edev;
46         int prev_cable_type;
47         int prev_cable_type_gnd;
48         int prev_chg_type;
49         int prev_button_type;
50         u8 status[2];
51
52         int irq;
53         struct work_struct irq_work;
54         struct mutex mutex;
55
56         /*
57          * Use delayed workqueue to detect cable state and then
58          * notify cable state to notifiee/platform through uevent.
59          * After completing the booting of platform, the extcon provider
60          * driver should notify cable state to upper layer.
61          */
62         struct delayed_work wq_detcable;
63
64         /* Button of dock device */
65         struct input_dev *dock;
66
67         /*
68          * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
69          * h/w path of COMP2/COMN1 on CONTROL1 register.
70          */
71         int path_usb;
72         int path_uart;
73 };
74
75 enum max77693_muic_cable_group {
76         MAX77693_CABLE_GROUP_ADC = 0,
77         MAX77693_CABLE_GROUP_ADC_GND,
78         MAX77693_CABLE_GROUP_CHG,
79         MAX77693_CABLE_GROUP_VBVOLT,
80 };
81
82 enum max77693_muic_charger_type {
83         MAX77693_CHARGER_TYPE_NONE = 0,
84         MAX77693_CHARGER_TYPE_USB,
85         MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,
86         MAX77693_CHARGER_TYPE_DEDICATED_CHG,
87         MAX77693_CHARGER_TYPE_APPLE_500MA,
88         MAX77693_CHARGER_TYPE_APPLE_1A_2A,
89         MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,
90 };
91
92 /**
93  * struct max77693_muic_irq
94  * @irq: the index of irq list of MUIC device.
95  * @name: the name of irq.
96  * @virq: the virtual irq to use irq domain
97  */
98 struct max77693_muic_irq {
99         unsigned int irq;
100         const char *name;
101         unsigned int virq;
102 };
103
104 static struct max77693_muic_irq muic_irqs[] = {
105         { MAX77693_MUIC_IRQ_INT1_ADC,           "muic-ADC" },
106         { MAX77693_MUIC_IRQ_INT1_ADC_LOW,       "muic-ADCLOW" },
107         { MAX77693_MUIC_IRQ_INT1_ADC_ERR,       "muic-ADCError" },
108         { MAX77693_MUIC_IRQ_INT1_ADC1K,         "muic-ADC1K" },
109         { MAX77693_MUIC_IRQ_INT2_CHGTYP,        "muic-CHGTYP" },
110         { MAX77693_MUIC_IRQ_INT2_CHGDETREUN,    "muic-CHGDETREUN" },
111         { MAX77693_MUIC_IRQ_INT2_DCDTMR,        "muic-DCDTMR" },
112         { MAX77693_MUIC_IRQ_INT2_DXOVP,         "muic-DXOVP" },
113         { MAX77693_MUIC_IRQ_INT2_VBVOLT,        "muic-VBVOLT" },
114         { MAX77693_MUIC_IRQ_INT2_VIDRM,         "muic-VIDRM" },
115         { MAX77693_MUIC_IRQ_INT3_EOC,           "muic-EOC" },
116         { MAX77693_MUIC_IRQ_INT3_CGMBC,         "muic-CGMBC" },
117         { MAX77693_MUIC_IRQ_INT3_OVP,           "muic-OVP" },
118         { MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR,    "muic-MBCCHG_ERR" },
119         { MAX77693_MUIC_IRQ_INT3_CHG_ENABLED,   "muic-CHG_ENABLED" },
120         { MAX77693_MUIC_IRQ_INT3_BAT_DET,       "muic-BAT_DET" },
121 };
122
123 /* Define supported accessory type */
124 enum max77693_muic_acc_type {
125         MAX77693_MUIC_ADC_GROUND = 0x0,
126         MAX77693_MUIC_ADC_SEND_END_BUTTON,
127         MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,
128         MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,
129         MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,
130         MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,
131         MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,
132         MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,
133         MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,
134         MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,
135         MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,
136         MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,
137         MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,
138         MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,
139         MAX77693_MUIC_ADC_RESERVED_ACC_1,
140         MAX77693_MUIC_ADC_RESERVED_ACC_2,
141         MAX77693_MUIC_ADC_RESERVED_ACC_3,
142         MAX77693_MUIC_ADC_RESERVED_ACC_4,
143         MAX77693_MUIC_ADC_RESERVED_ACC_5,
144         MAX77693_MUIC_ADC_CEA936_AUDIO,
145         MAX77693_MUIC_ADC_PHONE_POWERED_DEV,
146         MAX77693_MUIC_ADC_TTY_CONVERTER,
147         MAX77693_MUIC_ADC_UART_CABLE,
148         MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,
149         MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,
150         MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,
151         MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,
152         MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,
153         MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,
154         MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,
155         MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,
156         MAX77693_MUIC_ADC_OPEN,
157
158         /* The below accessories have same ADC value so ADCLow and
159            ADC1K bit is used to separate specific accessory */
160         MAX77693_MUIC_GND_USB_OTG = 0x100,      /* ADC:0x0, VBVolot:0, ADCLow:0, ADC1K:0 */
161         MAX77693_MUIC_GND_USB_OTG_VB = 0x104,   /* ADC:0x0, VBVolot:1, ADCLow:0, ADC1K:0 */
162         MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:0 */
163         MAX77693_MUIC_GND_MHL = 0x103,          /* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:1 */
164         MAX77693_MUIC_GND_MHL_VB = 0x107,       /* ADC:0x0, VBVolot:1, ADCLow:1, ADC1K:1 */
165 };
166
167 /* MAX77693 MUIC device support below list of accessories(external connector) */
168 enum {
169         EXTCON_CABLE_USB = 0,
170         EXTCON_CABLE_USB_HOST,
171         EXTCON_CABLE_TA,
172         EXTCON_CABLE_FAST_CHARGER,
173         EXTCON_CABLE_SLOW_CHARGER,
174         EXTCON_CABLE_CHARGE_DOWNSTREAM,
175         EXTCON_CABLE_MHL,
176         EXTCON_CABLE_MHL_TA,
177         EXTCON_CABLE_JIG_USB_ON,
178         EXTCON_CABLE_JIG_USB_OFF,
179         EXTCON_CABLE_JIG_UART_OFF,
180         EXTCON_CABLE_JIG_UART_ON,
181         EXTCON_CABLE_DOCK_SMART,
182         EXTCON_CABLE_DOCK_DESK,
183         EXTCON_CABLE_DOCK_AUDIO,
184
185         _EXTCON_CABLE_NUM,
186 };
187
188 const char *max77693_extcon_cable[] = {
189         [EXTCON_CABLE_USB]                      = "USB",
190         [EXTCON_CABLE_USB_HOST]                 = "USB-Host",
191         [EXTCON_CABLE_TA]                       = "TA",
192         [EXTCON_CABLE_FAST_CHARGER]             = "Fast-charger",
193         [EXTCON_CABLE_SLOW_CHARGER]             = "Slow-charger",
194         [EXTCON_CABLE_CHARGE_DOWNSTREAM]        = "Charge-downstream",
195         [EXTCON_CABLE_MHL]                      = "MHL",
196         [EXTCON_CABLE_MHL_TA]                   = "MHL_TA",
197         [EXTCON_CABLE_JIG_USB_ON]               = "JIG-USB-ON",
198         [EXTCON_CABLE_JIG_USB_OFF]              = "JIG-USB-OFF",
199         [EXTCON_CABLE_JIG_UART_OFF]             = "JIG-UART-OFF",
200         [EXTCON_CABLE_JIG_UART_ON]              = "Dock-Car",
201         [EXTCON_CABLE_DOCK_SMART]               = "Dock-Smart",
202         [EXTCON_CABLE_DOCK_DESK]                = "Dock-Desk",
203         [EXTCON_CABLE_DOCK_AUDIO]               = "Dock-Audio",
204
205         NULL,
206 };
207
208 /*
209  * max77693_muic_set_debounce_time - Set the debounce time of ADC
210  * @info: the instance including private data of max77693 MUIC
211  * @time: the debounce time of ADC
212  */
213 static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
214                 enum max77693_muic_adc_debounce_time time)
215 {
216         int ret;
217
218         switch (time) {
219         case ADC_DEBOUNCE_TIME_5MS:
220         case ADC_DEBOUNCE_TIME_10MS:
221         case ADC_DEBOUNCE_TIME_25MS:
222         case ADC_DEBOUNCE_TIME_38_62MS:
223                 ret = max77693_update_reg(info->max77693->regmap_muic,
224                                           MAX77693_MUIC_REG_CTRL3,
225                                           time << CONTROL3_ADCDBSET_SHIFT,
226                                           CONTROL3_ADCDBSET_MASK);
227                 if (ret)
228                         dev_err(info->dev, "failed to set ADC debounce time\n");
229                 break;
230         default:
231                 dev_err(info->dev, "invalid ADC debounce time\n");
232                 ret = -EINVAL;
233                 break;
234         }
235
236         return ret;
237 };
238
239 /*
240  * max77693_muic_set_path - Set hardware line according to attached cable
241  * @info: the instance including private data of max77693 MUIC
242  * @value: the path according to attached cable
243  * @attached: the state of cable (true:attached, false:detached)
244  *
245  * The max77693 MUIC device share outside H/W line among a varity of cables
246  * so, this function set internal path of H/W line according to the type of
247  * attached cable.
248  */
249 static int max77693_muic_set_path(struct max77693_muic_info *info,
250                 u8 val, bool attached)
251 {
252         int ret = 0;
253         u8 ctrl1, ctrl2 = 0;
254
255         if (attached)
256                 ctrl1 = val;
257         else
258                 ctrl1 = CONTROL1_SW_OPEN;
259
260         ret = max77693_update_reg(info->max77693->regmap_muic,
261                         MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
262         if (ret < 0) {
263                 dev_err(info->dev, "failed to update MUIC register\n");
264                 goto out;
265         }
266
267         if (attached)
268                 ctrl2 |= CONTROL2_CPEN_MASK;    /* LowPwr=0, CPEn=1 */
269         else
270                 ctrl2 |= CONTROL2_LOWPWR_MASK;  /* LowPwr=1, CPEn=0 */
271
272         ret = max77693_update_reg(info->max77693->regmap_muic,
273                         MAX77693_MUIC_REG_CTRL2, ctrl2,
274                         CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
275         if (ret < 0) {
276                 dev_err(info->dev, "failed to update MUIC register\n");
277                 goto out;
278         }
279
280         dev_info(info->dev,
281                 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
282                 ctrl1, ctrl2, attached ? "attached" : "detached");
283 out:
284         return ret;
285 }
286
287 /*
288  * max77693_muic_get_cable_type - Return cable type and check cable state
289  * @info: the instance including private data of max77693 MUIC
290  * @group: the path according to attached cable
291  * @attached: store cable state and return
292  *
293  * This function check the cable state either attached or detached,
294  * and then divide precise type of cable according to cable group.
295  *      - MAX77693_CABLE_GROUP_ADC
296  *      - MAX77693_CABLE_GROUP_ADC_GND
297  *      - MAX77693_CABLE_GROUP_CHG
298  *      - MAX77693_CABLE_GROUP_VBVOLT
299  */
300 static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
301                 enum max77693_muic_cable_group group, bool *attached)
302 {
303         int cable_type = 0;
304         int adc;
305         int adc1k;
306         int adclow;
307         int vbvolt;
308         int chg_type;
309
310         switch (group) {
311         case MAX77693_CABLE_GROUP_ADC:
312                 /*
313                  * Read ADC value to check cable type and decide cable state
314                  * according to cable type
315                  */
316                 adc = info->status[0] & STATUS1_ADC_MASK;
317                 adc >>= STATUS1_ADC_SHIFT;
318
319                 /*
320                  * Check current cable state/cable type and store cable type
321                  * (info->prev_cable_type) for handling cable when cable is
322                  * detached.
323                  */
324                 if (adc == MAX77693_MUIC_ADC_OPEN) {
325                         *attached = false;
326
327                         cable_type = info->prev_cable_type;
328                         info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
329                 } else {
330                         *attached = true;
331
332                         cable_type = info->prev_cable_type = adc;
333                 }
334                 break;
335         case MAX77693_CABLE_GROUP_ADC_GND:
336                 /*
337                  * Read ADC value to check cable type and decide cable state
338                  * according to cable type
339                  */
340                 adc = info->status[0] & STATUS1_ADC_MASK;
341                 adc >>= STATUS1_ADC_SHIFT;
342
343                 /*
344                  * Check current cable state/cable type and store cable type
345                  * (info->prev_cable_type/_gnd) for handling cable when cable
346                  * is detached.
347                  */
348                 if (adc == MAX77693_MUIC_ADC_OPEN) {
349                         *attached = false;
350
351                         cable_type = info->prev_cable_type_gnd;
352                         info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
353                 } else {
354                         *attached = true;
355
356                         adclow = info->status[0] & STATUS1_ADCLOW_MASK;
357                         adclow >>= STATUS1_ADCLOW_SHIFT;
358                         adc1k = info->status[0] & STATUS1_ADC1K_MASK;
359                         adc1k >>= STATUS1_ADC1K_SHIFT;
360
361                         vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
362                         vbvolt >>= STATUS2_VBVOLT_SHIFT;
363
364                         /**
365                          * [0x1][VBVolt][ADCLow][ADC1K]
366                          * [0x1    0       0       0  ] : USB_OTG
367                          * [0x1    1       0       0  ] : USB_OTG_VB
368                          * [0x1    0       1       0  ] : Audio Video Cable with load
369                          * [0x1    0       1       1  ] : MHL without charging connector
370                          * [0x1    1       1       1  ] : MHL with charging connector
371                          */
372                         cable_type = ((0x1 << 8)
373                                         | (vbvolt << 2)
374                                         | (adclow << 1)
375                                         | adc1k);
376
377                         info->prev_cable_type = adc;
378                         info->prev_cable_type_gnd = cable_type;
379                 }
380
381                 break;
382         case MAX77693_CABLE_GROUP_CHG:
383                 /*
384                  * Read charger type to check cable type and decide cable state
385                  * according to type of charger cable.
386                  */
387                 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
388                 chg_type >>= STATUS2_CHGTYP_SHIFT;
389
390                 if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
391                         *attached = false;
392
393                         cable_type = info->prev_chg_type;
394                         info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
395                 } else {
396                         *attached = true;
397
398                         /*
399                          * Check current cable state/cable type and store cable
400                          * type(info->prev_chg_type) for handling cable when
401                          * charger cable is detached.
402                          */
403                         cable_type = info->prev_chg_type = chg_type;
404                 }
405
406                 break;
407         case MAX77693_CABLE_GROUP_VBVOLT:
408                 /*
409                  * Read ADC value to check cable type and decide cable state
410                  * according to cable type
411                  */
412                 adc = info->status[0] & STATUS1_ADC_MASK;
413                 adc >>= STATUS1_ADC_SHIFT;
414                 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
415                 chg_type >>= STATUS2_CHGTYP_SHIFT;
416
417                 if (adc == MAX77693_MUIC_ADC_OPEN
418                                 && chg_type == MAX77693_CHARGER_TYPE_NONE)
419                         *attached = false;
420                 else
421                         *attached = true;
422
423                 /*
424                  * Read vbvolt field, if vbvolt is 1,
425                  * this cable is used for charging.
426                  */
427                 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
428                 vbvolt >>= STATUS2_VBVOLT_SHIFT;
429
430                 cable_type = vbvolt;
431                 break;
432         default:
433                 dev_err(info->dev, "Unknown cable group (%d)\n", group);
434                 cable_type = -EINVAL;
435                 break;
436         }
437
438         return cable_type;
439 }
440
441 static int max77693_muic_dock_handler(struct max77693_muic_info *info,
442                 int cable_type, bool attached)
443 {
444         int ret = 0;
445         char dock_name[CABLE_NAME_MAX];
446
447         dev_info(info->dev,
448                 "external connector is %s (adc:0x%02x)\n",
449                 attached ? "attached" : "detached", cable_type);
450
451         switch (cable_type) {
452         case MAX77693_MUIC_ADC_RESERVED_ACC_3:          /* Dock-Smart */
453                 /* PATH:AP_USB */
454                 ret = max77693_muic_set_path(info,
455                                 CONTROL1_SW_USB, attached);
456                 if (ret < 0)
457                         goto out;
458
459                 /* Dock-Smart */
460                 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
461                 goto out;
462         case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:    /* Dock-Car */
463                 strcpy(dock_name, "Dock-Car");
464                 break;
465         case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:       /* Dock-Desk */
466                 strcpy(dock_name, "Dock-Desk");
467                 break;
468         case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:         /* Dock-Audio */
469                 strcpy(dock_name, "Dock-Audio");
470                 if (!attached)
471                         extcon_set_cable_state(info->edev, "USB", false);
472                 break;
473         }
474
475         /* Dock-Car/Desk/Audio, PATH:AUDIO */
476         ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
477         if (ret < 0)
478                 goto out;
479         extcon_set_cable_state(info->edev, dock_name, attached);
480
481 out:
482         return ret;
483 }
484
485 static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
486                 int button_type, bool attached)
487 {
488         struct input_dev *dock = info->dock;
489         unsigned int code;
490         int ret = 0;
491
492         switch (button_type) {
493         case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
494                 ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
495                 /* DOCK_KEY_PREV */
496                 code = KEY_PREVIOUSSONG;
497                 break;
498         case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
499                 ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
500                 /* DOCK_KEY_NEXT */
501                 code = KEY_NEXTSONG;
502                 break;
503         case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
504                 /* DOCK_VOL_DOWN */
505                 code = KEY_VOLUMEDOWN;
506                 break;
507         case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
508                 /* DOCK_VOL_UP */
509                 code = KEY_VOLUMEUP;
510                 break;
511         case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
512                 ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
513                 /* DOCK_KEY_PLAY_PAUSE */
514                 code = KEY_PLAYPAUSE;
515                 break;
516         default:
517                 dev_err(info->dev,
518                         "failed to detect %s key (adc:0x%x)\n",
519                         attached ? "pressed" : "released", button_type);
520                 ret = -EINVAL;
521                 goto out;
522         }
523
524         input_event(dock, EV_KEY, code, attached);
525         input_sync(dock);
526
527 out:
528         return 0;
529 }
530
531 static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
532 {
533         int cable_type_gnd;
534         int ret = 0;
535         bool attached;
536
537         cable_type_gnd = max77693_muic_get_cable_type(info,
538                                 MAX77693_CABLE_GROUP_ADC_GND, &attached);
539
540         switch (cable_type_gnd) {
541         case MAX77693_MUIC_GND_USB_OTG:
542         case MAX77693_MUIC_GND_USB_OTG_VB:
543                 /* USB_OTG, PATH: AP_USB */
544                 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
545                 if (ret < 0)
546                         goto out;
547                 extcon_set_cable_state(info->edev, "USB-Host", attached);
548                 break;
549         case MAX77693_MUIC_GND_AV_CABLE_LOAD:
550                 /* Audio Video Cable with load, PATH:AUDIO */
551                 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
552                 if (ret < 0)
553                         goto out;
554                 extcon_set_cable_state(info->edev,
555                                 "Audio-video-load", attached);
556                 break;
557         case MAX77693_MUIC_GND_MHL:
558         case MAX77693_MUIC_GND_MHL_VB:
559                 /* MHL or MHL with USB/TA cable */
560                 extcon_set_cable_state(info->edev, "MHL", attached);
561                 break;
562         default:
563                 dev_err(info->dev, "failed to detect %s accessory\n",
564                         attached ? "attached" : "detached");
565                 ret = -EINVAL;
566                 break;
567         }
568
569 out:
570         return ret;
571 }
572
573 static int max77693_muic_jig_handler(struct max77693_muic_info *info,
574                 int cable_type, bool attached)
575 {
576         char cable_name[32];
577         int ret = 0;
578         u8 path = CONTROL1_SW_OPEN;
579
580         dev_info(info->dev,
581                 "external connector is %s (adc:0x%02x)\n",
582                 attached ? "attached" : "detached", cable_type);
583
584         switch (cable_type) {
585         case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:    /* ADC_JIG_USB_OFF */
586                 /* PATH:AP_USB */
587                 strcpy(cable_name, "JIG-USB-OFF");
588                 path = CONTROL1_SW_USB;
589                 break;
590         case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:     /* ADC_JIG_USB_ON */
591                 /* PATH:AP_USB */
592                 strcpy(cable_name, "JIG-USB-ON");
593                 path = CONTROL1_SW_USB;
594                 break;
595         case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:   /* ADC_JIG_UART_OFF */
596                 /* PATH:AP_UART */
597                 strcpy(cable_name, "JIG-UART-OFF");
598                 path = CONTROL1_SW_UART;
599                 break;
600         }
601
602         ret = max77693_muic_set_path(info, path, attached);
603         if (ret < 0)
604                 goto out;
605
606         extcon_set_cable_state(info->edev, cable_name, attached);
607 out:
608         return ret;
609 }
610
611 static int max77693_muic_adc_handler(struct max77693_muic_info *info)
612 {
613         int cable_type;
614         int button_type;
615         bool attached;
616         int ret = 0;
617
618         /* Check accessory state which is either detached or attached */
619         cable_type = max77693_muic_get_cable_type(info,
620                                 MAX77693_CABLE_GROUP_ADC, &attached);
621
622         dev_info(info->dev,
623                 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
624                 attached ? "attached" : "detached", cable_type,
625                 info->prev_cable_type);
626
627         switch (cable_type) {
628         case MAX77693_MUIC_ADC_GROUND:
629                 /* USB_OTG/MHL/Audio */
630                 max77693_muic_adc_ground_handler(info);
631                 break;
632         case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
633         case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
634         case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
635                 /* JIG */
636                 ret = max77693_muic_jig_handler(info, cable_type, attached);
637                 if (ret < 0)
638                         goto out;
639                 break;
640         case MAX77693_MUIC_ADC_RESERVED_ACC_3:          /* Dock-Smart */
641         case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:    /* Dock-Car */
642         case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:       /* Dock-Desk */
643         case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:         /* Dock-Audio */
644                 /*
645                  * DOCK device
646                  *
647                  * The MAX77693 MUIC device can detect total 34 cable type
648                  * except of charger cable and MUIC device didn't define
649                  * specfic role of cable in the range of from 0x01 to 0x12
650                  * of ADC value. So, can use/define cable with no role according
651                  * to schema of hardware board.
652                  */
653                 ret = max77693_muic_dock_handler(info, cable_type, attached);
654                 if (ret < 0)
655                         goto out;
656                 break;
657         case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON:        /* DOCK_KEY_PREV */
658         case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON:        /* DOCK_KEY_NEXT */
659         case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:        /* DOCK_VOL_DOWN */
660         case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:       /* DOCK_VOL_UP */
661         case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON:       /* DOCK_KEY_PLAY_PAUSE */
662                 /*
663                  * Button of DOCK device
664                  * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
665                  *
666                  * The MAX77693 MUIC device can detect total 34 cable type
667                  * except of charger cable and MUIC device didn't define
668                  * specfic role of cable in the range of from 0x01 to 0x12
669                  * of ADC value. So, can use/define cable with no role according
670                  * to schema of hardware board.
671                  */
672                 if (attached)
673                         button_type = info->prev_button_type = cable_type;
674                 else
675                         button_type = info->prev_button_type;
676
677                 ret = max77693_muic_dock_button_handler(info, button_type,
678                                                         attached);
679                 if (ret < 0)
680                         goto out;
681                 break;
682         case MAX77693_MUIC_ADC_SEND_END_BUTTON:
683         case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
684         case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
685         case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
686         case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
687         case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
688         case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
689         case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
690         case MAX77693_MUIC_ADC_RESERVED_ACC_1:
691         case MAX77693_MUIC_ADC_RESERVED_ACC_2:
692         case MAX77693_MUIC_ADC_RESERVED_ACC_4:
693         case MAX77693_MUIC_ADC_RESERVED_ACC_5:
694         case MAX77693_MUIC_ADC_CEA936_AUDIO:
695         case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
696         case MAX77693_MUIC_ADC_TTY_CONVERTER:
697         case MAX77693_MUIC_ADC_UART_CABLE:
698         case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
699         case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
700                 /*
701                  * This accessory isn't used in general case if it is specially
702                  * needed to detect additional accessory, should implement
703                  * proper operation when this accessory is attached/detached.
704                  */
705                 dev_info(info->dev,
706                         "accessory is %s but it isn't used (adc:0x%x)\n",
707                         attached ? "attached" : "detached", cable_type);
708                 goto out;
709         default:
710                 dev_err(info->dev,
711                         "failed to detect %s accessory (adc:0x%x)\n",
712                         attached ? "attached" : "detached", cable_type);
713                 ret = -EINVAL;
714                 goto out;
715         }
716
717 out:
718         return ret;
719 }
720
721 static int max77693_muic_chg_handler(struct max77693_muic_info *info)
722 {
723         int chg_type;
724         int cable_type_gnd;
725         int cable_type;
726         bool attached;
727         bool cable_attached;
728         int ret = 0;
729
730         chg_type = max77693_muic_get_cable_type(info,
731                                 MAX77693_CABLE_GROUP_CHG, &attached);
732
733         dev_info(info->dev,
734                 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
735                         attached ? "attached" : "detached",
736                         chg_type, info->prev_chg_type);
737
738         switch (chg_type) {
739         case MAX77693_CHARGER_TYPE_USB:
740         case MAX77693_CHARGER_TYPE_NONE:
741                 /*
742                  * MHL_TA(USB/TA) with MHL cable
743                  * - MHL cable include two port(HDMI line and separate micro
744                  * -usb port. When the target connect MHL cable, extcon driver
745                  * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA
746                  * cable is connected, extcon driver notify state to notifiee
747                  * for charging battery.
748                  */
749                 cable_type_gnd = max77693_muic_get_cable_type(info,
750                                         MAX77693_CABLE_GROUP_ADC_GND,
751                                         &cable_attached);
752                 if (cable_type_gnd == MAX77693_MUIC_GND_MHL
753                         || cable_type_gnd == MAX77693_MUIC_GND_MHL_VB) {
754                         extcon_set_cable_state(info->edev, "MHL_TA", attached);
755
756                         if (!cable_attached)
757                                 extcon_set_cable_state(info->edev,
758                                         "MHL", false);
759                         goto out;
760                 }
761
762                 /*
763                  * USB/TA cable with Dock-Audio device
764                  * - Dock device include two port(Dock-Audio and micro-usb
765                  * port). When the target connect Dock-Audio device, extcon
766                  * driver check whether USB/TA cable is connected.
767                  * If USB/TA cable is connected, extcon driver notify state
768                  * to notifiee for charging battery.
769                  */
770                 cable_type = max77693_muic_get_cable_type(info,
771                                         MAX77693_CABLE_GROUP_ADC,
772                                         &cable_attached);
773                 if (cable_type == MAX77693_MUIC_ADC_AV_CABLE_NOLOAD) {
774                         extcon_set_cable_state(info->edev, "USB", attached);
775
776                         if (!cable_attached)
777                                 extcon_set_cable_state(info->edev,
778                                                 "Dock-Audio", false);
779                         goto out;
780                 }
781
782                 /*
783                  * When MHL(with USB/TA cable) or Dock-Audio with USB/TA cable
784                  * is attached, muic device happen below two interrupt.
785                  * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting MHL/Dock-Audio.
786                  * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting USB/TA cable
787                  *   connected to MHL or Dock-Audio.
788                  * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC interrupt
789                  * than MAX77693_MUIC_IRQ_INT2_CHGTYP interrupt.
790                  *
791                  * If user attach MHL (with USB/TA cable and immediately detach
792                  * MHL with USB/TA cable before MAX77693_MUIC_IRQ_INT2_CHGTYP
793                  * interrupt is happened, USB/TA cable remain connected state to
794                  * target. But USB/TA cable isn't connected to target. The user
795                  * be face with unusual action. So, driver should check this
796                  * situation in spite of, that previous charger type is N/A.
797                  */
798                 if (chg_type == MAX77693_CHARGER_TYPE_NONE)
799                         break;
800
801                 /* Only USB cable, PATH:AP_USB */
802                 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
803                 if (ret < 0)
804                         goto out;
805                 extcon_set_cable_state(info->edev, "USB", attached);
806                 break;
807         case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
808                 extcon_set_cable_state(info->edev,
809                                 "Charge-downstream", attached);
810                 break;
811         case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
812                 extcon_set_cable_state(info->edev, "TA", attached);
813                 break;
814         case MAX77693_CHARGER_TYPE_APPLE_500MA:
815                 extcon_set_cable_state(info->edev, "Slow-charger", attached);
816                 break;
817         case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
818                 extcon_set_cable_state(info->edev, "Fast-charger", attached);
819                 break;
820         case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
821                 break;
822         default:
823                 dev_err(info->dev,
824                         "failed to detect %s accessory (chg_type:0x%x)\n",
825                         attached ? "attached" : "detached", chg_type);
826                 ret = -EINVAL;
827                 goto out;
828         }
829
830 out:
831         return ret;
832 }
833
834 static void max77693_muic_irq_work(struct work_struct *work)
835 {
836         struct max77693_muic_info *info = container_of(work,
837                         struct max77693_muic_info, irq_work);
838         int irq_type = -1;
839         int i, ret = 0;
840
841         if (!info->edev)
842                 return;
843
844         mutex_lock(&info->mutex);
845
846         for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
847                 if (info->irq == muic_irqs[i].virq)
848                         irq_type = muic_irqs[i].irq;
849
850         ret = max77693_bulk_read(info->max77693->regmap_muic,
851                         MAX77693_MUIC_REG_STATUS1, 2, info->status);
852         if (ret) {
853                 dev_err(info->dev, "failed to read MUIC register\n");
854                 mutex_unlock(&info->mutex);
855                 return;
856         }
857
858         switch (irq_type) {
859         case MAX77693_MUIC_IRQ_INT1_ADC:
860         case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
861         case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
862         case MAX77693_MUIC_IRQ_INT1_ADC1K:
863                 /* Handle all of accessory except for
864                    type of charger accessory */
865                 ret = max77693_muic_adc_handler(info);
866                 break;
867         case MAX77693_MUIC_IRQ_INT2_CHGTYP:
868         case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
869         case MAX77693_MUIC_IRQ_INT2_DCDTMR:
870         case MAX77693_MUIC_IRQ_INT2_DXOVP:
871         case MAX77693_MUIC_IRQ_INT2_VBVOLT:
872         case MAX77693_MUIC_IRQ_INT2_VIDRM:
873                 /* Handle charger accessory */
874                 ret = max77693_muic_chg_handler(info);
875                 break;
876         case MAX77693_MUIC_IRQ_INT3_EOC:
877         case MAX77693_MUIC_IRQ_INT3_CGMBC:
878         case MAX77693_MUIC_IRQ_INT3_OVP:
879         case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
880         case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
881         case MAX77693_MUIC_IRQ_INT3_BAT_DET:
882                 break;
883         default:
884                 dev_err(info->dev, "muic interrupt: irq %d occurred\n",
885                                 irq_type);
886                 break;
887         }
888
889         if (ret < 0)
890                 dev_err(info->dev, "failed to handle MUIC interrupt\n");
891
892         mutex_unlock(&info->mutex);
893
894         return;
895 }
896
897 static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
898 {
899         struct max77693_muic_info *info = data;
900
901         info->irq = irq;
902         schedule_work(&info->irq_work);
903
904         return IRQ_HANDLED;
905 }
906
907 static struct regmap_config max77693_muic_regmap_config = {
908         .reg_bits = 8,
909         .val_bits = 8,
910 };
911
912 static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
913 {
914         int ret = 0;
915         int adc;
916         int chg_type;
917         bool attached;
918
919         mutex_lock(&info->mutex);
920
921         /* Read STATUSx register to detect accessory */
922         ret = max77693_bulk_read(info->max77693->regmap_muic,
923                         MAX77693_MUIC_REG_STATUS1, 2, info->status);
924         if (ret) {
925                 dev_err(info->dev, "failed to read MUIC register\n");
926                 mutex_unlock(&info->mutex);
927                 return -EINVAL;
928         }
929
930         adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
931                                         &attached);
932         if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
933                 ret = max77693_muic_adc_handler(info);
934                 if (ret < 0)
935                         dev_err(info->dev, "Cannot detect accessory\n");
936         }
937
938         chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
939                                         &attached);
940         if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
941                 ret = max77693_muic_chg_handler(info);
942                 if (ret < 0)
943                         dev_err(info->dev, "Cannot detect charger accessory\n");
944         }
945
946         mutex_unlock(&info->mutex);
947
948         return ret;
949 }
950
951 static void max77693_muic_detect_cable_wq(struct work_struct *work)
952 {
953         struct max77693_muic_info *info = container_of(to_delayed_work(work),
954                                 struct max77693_muic_info, wq_detcable);
955
956         max77693_muic_detect_accessory(info);
957 }
958
959 static int max77693_muic_probe(struct platform_device *pdev)
960 {
961         struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
962         struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
963         struct max77693_muic_platform_data *muic_pdata = pdata->muic_data;
964         struct max77693_muic_info *info;
965         int delay_jiffies;
966         int ret;
967         int i;
968         u8 id;
969
970         info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
971                                    GFP_KERNEL);
972         if (!info) {
973                 dev_err(&pdev->dev, "failed to allocate memory\n");
974                 return -ENOMEM;
975         }
976         info->dev = &pdev->dev;
977         info->max77693 = max77693;
978         if (info->max77693->regmap_muic) {
979                 dev_dbg(&pdev->dev, "allocate register map\n");
980         } else {
981                 info->max77693->regmap_muic = devm_regmap_init_i2c(
982                                                 info->max77693->muic,
983                                                 &max77693_muic_regmap_config);
984                 if (IS_ERR(info->max77693->regmap_muic)) {
985                         ret = PTR_ERR(info->max77693->regmap_muic);
986                         dev_err(max77693->dev,
987                                 "failed to allocate register map: %d\n", ret);
988                         return ret;
989                 }
990         }
991
992         /* Register input device for button of dock device */
993         info->dock = input_allocate_device();
994         if (!info->dock) {
995                 dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
996                 return -ENOMEM;
997         }
998         info->dock->name = "max77693-muic/dock";
999         info->dock->phys = "max77693-muic/extcon";
1000         info->dock->dev.parent = &pdev->dev;
1001
1002         __set_bit(EV_REP, info->dock->evbit);
1003
1004         input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
1005         input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
1006         input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
1007         input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
1008         input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
1009
1010         ret = input_register_device(info->dock);
1011         if (ret < 0) {
1012                 dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
1013                                 ret);
1014                 return ret;
1015         }
1016
1017         platform_set_drvdata(pdev, info);
1018         mutex_init(&info->mutex);
1019
1020         INIT_WORK(&info->irq_work, max77693_muic_irq_work);
1021
1022         /* Support irq domain for MAX77693 MUIC device */
1023         for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
1024                 struct max77693_muic_irq *muic_irq = &muic_irqs[i];
1025                 unsigned int virq = 0;
1026
1027                 virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
1028                 if (!virq) {
1029                         ret = -EINVAL;
1030                         goto err_irq;
1031                 }
1032                 muic_irq->virq = virq;
1033
1034                 ret = request_threaded_irq(virq, NULL,
1035                                 max77693_muic_irq_handler,
1036                                 IRQF_NO_SUSPEND,
1037                                 muic_irq->name, info);
1038                 if (ret) {
1039                         dev_err(&pdev->dev,
1040                                 "failed: irq request (IRQ: %d,"
1041                                 " error :%d)\n",
1042                                 muic_irq->irq, ret);
1043                         goto err_irq;
1044                 }
1045         }
1046
1047         /* Initialize extcon device */
1048         info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
1049                                   GFP_KERNEL);
1050         if (!info->edev) {
1051                 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1052                 ret = -ENOMEM;
1053                 goto err_irq;
1054         }
1055         info->edev->name = DEV_NAME;
1056         info->edev->supported_cable = max77693_extcon_cable;
1057         ret = extcon_dev_register(info->edev, NULL);
1058         if (ret) {
1059                 dev_err(&pdev->dev, "failed to register extcon device\n");
1060                 goto err_irq;
1061         }
1062
1063         /* Initialize MUIC register by using platform data */
1064         for (i = 0 ; i < muic_pdata->num_init_data ; i++) {
1065                 enum max77693_irq_source irq_src = MAX77693_IRQ_GROUP_NR;
1066
1067                 max77693_write_reg(info->max77693->regmap_muic,
1068                                 muic_pdata->init_data[i].addr,
1069                                 muic_pdata->init_data[i].data);
1070
1071                 switch (muic_pdata->init_data[i].addr) {
1072                 case MAX77693_MUIC_REG_INTMASK1:
1073                         irq_src = MUIC_INT1;
1074                         break;
1075                 case MAX77693_MUIC_REG_INTMASK2:
1076                         irq_src = MUIC_INT2;
1077                         break;
1078                 case MAX77693_MUIC_REG_INTMASK3:
1079                         irq_src = MUIC_INT3;
1080                         break;
1081                 }
1082
1083                 if (irq_src < MAX77693_IRQ_GROUP_NR)
1084                         info->max77693->irq_masks_cur[irq_src]
1085                                 = muic_pdata->init_data[i].data;
1086         }
1087
1088         /*
1089          * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
1090          * h/w path of COMP2/COMN1 on CONTROL1 register.
1091          */
1092         if (muic_pdata->path_uart)
1093                 info->path_uart = muic_pdata->path_uart;
1094         else
1095                 info->path_uart = CONTROL1_SW_UART;
1096
1097         if (muic_pdata->path_usb)
1098                 info->path_usb = muic_pdata->path_usb;
1099         else
1100                 info->path_usb = CONTROL1_SW_USB;
1101
1102         /* Set initial path for UART */
1103          max77693_muic_set_path(info, info->path_uart, true);
1104
1105         /* Check revision number of MUIC device*/
1106         ret = max77693_read_reg(info->max77693->regmap_muic,
1107                         MAX77693_MUIC_REG_ID, &id);
1108         if (ret < 0) {
1109                 dev_err(&pdev->dev, "failed to read revision number\n");
1110                 goto err_extcon;
1111         }
1112         dev_info(info->dev, "device ID : 0x%x\n", id);
1113
1114         /* Set ADC debounce time */
1115         max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1116
1117         /*
1118          * Detect accessory after completing the initialization of platform
1119          *
1120          * - Use delayed workqueue to detect cable state and then
1121          * notify cable state to notifiee/platform through uevent.
1122          * After completing the booting of platform, the extcon provider
1123          * driver should notify cable state to upper layer.
1124          */
1125         INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
1126         if (muic_pdata->detcable_delay_ms)
1127                 delay_jiffies = msecs_to_jiffies(muic_pdata->detcable_delay_ms);
1128         else
1129                 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1130         schedule_delayed_work(&info->wq_detcable, delay_jiffies);
1131
1132         return ret;
1133
1134 err_extcon:
1135         extcon_dev_unregister(info->edev);
1136 err_irq:
1137         while (--i >= 0)
1138                 free_irq(muic_irqs[i].virq, info);
1139         return ret;
1140 }
1141
1142 static int max77693_muic_remove(struct platform_device *pdev)
1143 {
1144         struct max77693_muic_info *info = platform_get_drvdata(pdev);
1145         int i;
1146
1147         for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
1148                 free_irq(muic_irqs[i].virq, info);
1149         cancel_work_sync(&info->irq_work);
1150         input_unregister_device(info->dock);
1151         extcon_dev_unregister(info->edev);
1152
1153         return 0;
1154 }
1155
1156 static struct platform_driver max77693_muic_driver = {
1157         .driver         = {
1158                 .name   = DEV_NAME,
1159                 .owner  = THIS_MODULE,
1160         },
1161         .probe          = max77693_muic_probe,
1162         .remove         = max77693_muic_remove,
1163 };
1164
1165 module_platform_driver(max77693_muic_driver);
1166
1167 MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
1168 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1169 MODULE_LICENSE("GPL");
1170 MODULE_ALIAS("platform:extcon-max77693");