]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/extcon/extcon-max77693.c
extcon: max77693: Convert to devm_input_allocate_device()
[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         int vbvolt;
446         bool cable_attached;
447         char dock_name[CABLE_NAME_MAX];
448
449         dev_info(info->dev,
450                 "external connector is %s (adc:0x%02x)\n",
451                 attached ? "attached" : "detached", cable_type);
452
453         switch (cable_type) {
454         case MAX77693_MUIC_ADC_RESERVED_ACC_3:          /* Dock-Smart */
455                 /*
456                  * Check power cable whether attached or detached state.
457                  * The Dock-Smart device need surely external power supply.
458                  * If power cable(USB/TA) isn't connected to Dock device,
459                  * user can't use Dock-Smart for desktop mode.
460                  */
461                 vbvolt = max77693_muic_get_cable_type(info,
462                                 MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);
463                 if (attached && !vbvolt) {
464                         dev_warn(info->dev,
465                                 "Cannot detect external power supply\n");
466                         return 0;
467                 }
468
469                 /*
470                  * Notify Dock-Smart/MHL state.
471                  * - Dock-Smart device include three type of cable which
472                  * are HDMI, USB for mouse/keyboard and micro-usb port
473                  * for USB/TA cable. Dock-Smart device need always exteranl
474                  * power supply(USB/TA cable through micro-usb cable). Dock-
475                  * Smart device support screen output of target to separate
476                  * monitor and mouse/keyboard for desktop mode.
477                  *
478                  * Features of 'USB/TA cable with Dock-Smart device'
479                  * - Support MHL
480                  * - Support external output feature of audio
481                  * - Support charging through micro-usb port without data
482                  *           connection if TA cable is connected to target.
483                  * - Support charging and data connection through micro-usb port
484                  *           if USB cable is connected between target and host
485                  *           device.
486                  * - Support OTG device (Mouse/Keyboard)
487                  */
488                 ret = max77693_muic_set_path(info, info->path_usb, attached);
489                 if (ret < 0)
490                         return ret;
491
492                 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
493                 extcon_set_cable_state(info->edev, "MHL", attached);
494                 goto out;
495         case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:    /* Dock-Car */
496                 strcpy(dock_name, "Dock-Car");
497                 break;
498         case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:       /* Dock-Desk */
499                 strcpy(dock_name, "Dock-Desk");
500                 break;
501         case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:         /* Dock-Audio */
502                 strcpy(dock_name, "Dock-Audio");
503                 if (!attached)
504                         extcon_set_cable_state(info->edev, "USB", false);
505                 break;
506         }
507
508         /* Dock-Car/Desk/Audio, PATH:AUDIO */
509         ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
510         if (ret < 0)
511                 return ret;
512         extcon_set_cable_state(info->edev, dock_name, attached);
513
514 out:
515         return 0;
516 }
517
518 static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
519                 int button_type, bool attached)
520 {
521         struct input_dev *dock = info->dock;
522         unsigned int code;
523         int ret = 0;
524
525         switch (button_type) {
526         case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
527                 ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
528                 /* DOCK_KEY_PREV */
529                 code = KEY_PREVIOUSSONG;
530                 break;
531         case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
532                 ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
533                 /* DOCK_KEY_NEXT */
534                 code = KEY_NEXTSONG;
535                 break;
536         case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
537                 /* DOCK_VOL_DOWN */
538                 code = KEY_VOLUMEDOWN;
539                 break;
540         case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
541                 /* DOCK_VOL_UP */
542                 code = KEY_VOLUMEUP;
543                 break;
544         case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
545                 ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
546                 /* DOCK_KEY_PLAY_PAUSE */
547                 code = KEY_PLAYPAUSE;
548                 break;
549         default:
550                 dev_err(info->dev,
551                         "failed to detect %s key (adc:0x%x)\n",
552                         attached ? "pressed" : "released", button_type);
553                 ret = -EINVAL;
554                 goto out;
555         }
556
557         input_event(dock, EV_KEY, code, attached);
558         input_sync(dock);
559
560 out:
561         return 0;
562 }
563
564 static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
565 {
566         int cable_type_gnd;
567         int ret = 0;
568         bool attached;
569
570         cable_type_gnd = max77693_muic_get_cable_type(info,
571                                 MAX77693_CABLE_GROUP_ADC_GND, &attached);
572
573         switch (cable_type_gnd) {
574         case MAX77693_MUIC_GND_USB_OTG:
575         case MAX77693_MUIC_GND_USB_OTG_VB:
576                 /* USB_OTG, PATH: AP_USB */
577                 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
578                 if (ret < 0)
579                         goto out;
580                 extcon_set_cable_state(info->edev, "USB-Host", attached);
581                 break;
582         case MAX77693_MUIC_GND_AV_CABLE_LOAD:
583                 /* Audio Video Cable with load, PATH:AUDIO */
584                 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
585                 if (ret < 0)
586                         goto out;
587                 extcon_set_cable_state(info->edev,
588                                 "Audio-video-load", attached);
589                 break;
590         case MAX77693_MUIC_GND_MHL:
591         case MAX77693_MUIC_GND_MHL_VB:
592                 /* MHL or MHL with USB/TA cable */
593                 extcon_set_cable_state(info->edev, "MHL", attached);
594                 break;
595         default:
596                 dev_err(info->dev, "failed to detect %s accessory\n",
597                         attached ? "attached" : "detached");
598                 ret = -EINVAL;
599                 break;
600         }
601
602 out:
603         return ret;
604 }
605
606 static int max77693_muic_jig_handler(struct max77693_muic_info *info,
607                 int cable_type, bool attached)
608 {
609         char cable_name[32];
610         int ret = 0;
611         u8 path = CONTROL1_SW_OPEN;
612
613         dev_info(info->dev,
614                 "external connector is %s (adc:0x%02x)\n",
615                 attached ? "attached" : "detached", cable_type);
616
617         switch (cable_type) {
618         case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:    /* ADC_JIG_USB_OFF */
619                 /* PATH:AP_USB */
620                 strcpy(cable_name, "JIG-USB-OFF");
621                 path = CONTROL1_SW_USB;
622                 break;
623         case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:     /* ADC_JIG_USB_ON */
624                 /* PATH:AP_USB */
625                 strcpy(cable_name, "JIG-USB-ON");
626                 path = CONTROL1_SW_USB;
627                 break;
628         case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:   /* ADC_JIG_UART_OFF */
629                 /* PATH:AP_UART */
630                 strcpy(cable_name, "JIG-UART-OFF");
631                 path = CONTROL1_SW_UART;
632                 break;
633         }
634
635         ret = max77693_muic_set_path(info, path, attached);
636         if (ret < 0)
637                 goto out;
638
639         extcon_set_cable_state(info->edev, cable_name, attached);
640 out:
641         return ret;
642 }
643
644 static int max77693_muic_adc_handler(struct max77693_muic_info *info)
645 {
646         int cable_type;
647         int button_type;
648         bool attached;
649         int ret = 0;
650
651         /* Check accessory state which is either detached or attached */
652         cable_type = max77693_muic_get_cable_type(info,
653                                 MAX77693_CABLE_GROUP_ADC, &attached);
654
655         dev_info(info->dev,
656                 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
657                 attached ? "attached" : "detached", cable_type,
658                 info->prev_cable_type);
659
660         switch (cable_type) {
661         case MAX77693_MUIC_ADC_GROUND:
662                 /* USB_OTG/MHL/Audio */
663                 max77693_muic_adc_ground_handler(info);
664                 break;
665         case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
666         case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
667         case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
668                 /* JIG */
669                 ret = max77693_muic_jig_handler(info, cable_type, attached);
670                 if (ret < 0)
671                         goto out;
672                 break;
673         case MAX77693_MUIC_ADC_RESERVED_ACC_3:          /* Dock-Smart */
674         case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:    /* Dock-Car */
675         case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:       /* Dock-Desk */
676         case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:         /* Dock-Audio */
677                 /*
678                  * DOCK device
679                  *
680                  * The MAX77693 MUIC device can detect total 34 cable type
681                  * except of charger cable and MUIC device didn't define
682                  * specfic role of cable in the range of from 0x01 to 0x12
683                  * of ADC value. So, can use/define cable with no role according
684                  * to schema of hardware board.
685                  */
686                 ret = max77693_muic_dock_handler(info, cable_type, attached);
687                 if (ret < 0)
688                         goto out;
689                 break;
690         case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON:        /* DOCK_KEY_PREV */
691         case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON:        /* DOCK_KEY_NEXT */
692         case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:        /* DOCK_VOL_DOWN */
693         case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:       /* DOCK_VOL_UP */
694         case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON:       /* DOCK_KEY_PLAY_PAUSE */
695                 /*
696                  * Button of DOCK device
697                  * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
698                  *
699                  * The MAX77693 MUIC device can detect total 34 cable type
700                  * except of charger cable and MUIC device didn't define
701                  * specfic role of cable in the range of from 0x01 to 0x12
702                  * of ADC value. So, can use/define cable with no role according
703                  * to schema of hardware board.
704                  */
705                 if (attached)
706                         button_type = info->prev_button_type = cable_type;
707                 else
708                         button_type = info->prev_button_type;
709
710                 ret = max77693_muic_dock_button_handler(info, button_type,
711                                                         attached);
712                 if (ret < 0)
713                         goto out;
714                 break;
715         case MAX77693_MUIC_ADC_SEND_END_BUTTON:
716         case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
717         case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
718         case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
719         case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
720         case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
721         case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
722         case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
723         case MAX77693_MUIC_ADC_RESERVED_ACC_1:
724         case MAX77693_MUIC_ADC_RESERVED_ACC_2:
725         case MAX77693_MUIC_ADC_RESERVED_ACC_4:
726         case MAX77693_MUIC_ADC_RESERVED_ACC_5:
727         case MAX77693_MUIC_ADC_CEA936_AUDIO:
728         case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
729         case MAX77693_MUIC_ADC_TTY_CONVERTER:
730         case MAX77693_MUIC_ADC_UART_CABLE:
731         case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
732         case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
733                 /*
734                  * This accessory isn't used in general case if it is specially
735                  * needed to detect additional accessory, should implement
736                  * proper operation when this accessory is attached/detached.
737                  */
738                 dev_info(info->dev,
739                         "accessory is %s but it isn't used (adc:0x%x)\n",
740                         attached ? "attached" : "detached", cable_type);
741                 goto out;
742         default:
743                 dev_err(info->dev,
744                         "failed to detect %s accessory (adc:0x%x)\n",
745                         attached ? "attached" : "detached", cable_type);
746                 ret = -EINVAL;
747                 goto out;
748         }
749
750 out:
751         return ret;
752 }
753
754 static int max77693_muic_chg_handler(struct max77693_muic_info *info)
755 {
756         int chg_type;
757         int cable_type_gnd;
758         int cable_type;
759         bool attached;
760         bool cable_attached;
761         int ret = 0;
762
763         chg_type = max77693_muic_get_cable_type(info,
764                                 MAX77693_CABLE_GROUP_CHG, &attached);
765
766         dev_info(info->dev,
767                 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
768                         attached ? "attached" : "detached",
769                         chg_type, info->prev_chg_type);
770
771         switch (chg_type) {
772         case MAX77693_CHARGER_TYPE_USB:
773         case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
774         case MAX77693_CHARGER_TYPE_NONE:
775                 /* Check MAX77693_CABLE_GROUP_ADC_GND type */
776                 cable_type_gnd = max77693_muic_get_cable_type(info,
777                                         MAX77693_CABLE_GROUP_ADC_GND,
778                                         &cable_attached);
779                 switch (cable_type_gnd) {
780                 case MAX77693_MUIC_GND_MHL:
781                 case MAX77693_MUIC_GND_MHL_VB:
782                         /*
783                          * MHL cable with MHL_TA(USB/TA) cable
784                          * - MHL cable include two port(HDMI line and separate micro-
785                          * usb port. When the target connect MHL cable, extcon driver
786                          * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA
787                          * cable is connected, extcon driver notify state to notifiee
788                          * for charging battery.
789                          *
790                          * Features of 'MHL_TA(USB/TA) with MHL cable'
791                          * - Support MHL
792                          * - Support charging through micro-usb port without data connection
793                          */
794                         extcon_set_cable_state(info->edev, "MHL_TA", attached);
795                         if (!cable_attached)
796                                 extcon_set_cable_state(info->edev, "MHL", cable_attached);
797                         break;
798                 }
799
800                 /* Check MAX77693_CABLE_GROUP_ADC type */
801                 cable_type = max77693_muic_get_cable_type(info,
802                                         MAX77693_CABLE_GROUP_ADC,
803                                         &cable_attached);
804                 switch (cable_type) {
805                 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:         /* Dock-Audio */
806                         /*
807                          * Dock-Audio device with USB/TA cable
808                          * - Dock device include two port(Dock-Audio and micro-usb
809                          * port). When the target connect Dock-Audio device, extcon
810                          * driver check whether USB/TA cable is connected. If USB/TA
811                          * cable is connected, extcon driver notify state to notifiee
812                          * for charging battery.
813                          *
814                          * Features of 'USB/TA cable with Dock-Audio device'
815                          * - Support external output feature of audio.
816                          * - Support charging through micro-usb port without data
817                          *           connection.
818                          */
819                         extcon_set_cable_state(info->edev, "USB", attached);
820
821                         if (!cable_attached)
822                                 extcon_set_cable_state(info->edev, "Dock-Audio", cable_attached);
823                         break;
824                 case MAX77693_MUIC_ADC_RESERVED_ACC_3:          /* Dock-Smart */
825                         /*
826                          * Dock-Smart device with USB/TA cable
827                          * - Dock-Desk device include three type of cable which
828                          * are HDMI, USB for mouse/keyboard and micro-usb port
829                          * for USB/TA cable. Dock-Smart device need always exteranl
830                          * power supply(USB/TA cable through micro-usb cable). Dock-
831                          * Smart device support screen output of target to separate
832                          * monitor and mouse/keyboard for desktop mode.
833                          *
834                          * Features of 'USB/TA cable with Dock-Smart device'
835                          * - Support MHL
836                          * - Support external output feature of audio
837                          * - Support charging through micro-usb port without data
838                          *           connection if TA cable is connected to target.
839                          * - Support charging and data connection through micro-usb port
840                          *           if USB cable is connected between target and host
841                          *           device.
842                          * - Support OTG device (Mouse/Keyboard)
843                          */
844                         ret = max77693_muic_set_path(info, info->path_usb, attached);
845                         if (ret < 0)
846                                 return ret;
847
848                         extcon_set_cable_state(info->edev, "Dock-Smart", attached);
849                         extcon_set_cable_state(info->edev, "MHL", attached);
850
851                         break;
852                 }
853
854                 /* Check MAX77693_CABLE_GROUP_CHG type */
855                 switch (chg_type) {
856                 case MAX77693_CHARGER_TYPE_NONE:
857                         /*
858                          * When MHL(with USB/TA cable) or Dock-Audio with USB/TA cable
859                          * is attached, muic device happen below two interrupt.
860                          * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting MHL/Dock-Audio.
861                          * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting USB/TA cable
862                          *   connected to MHL or Dock-Audio.
863                          * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC interrupt
864                          * than MAX77693_MUIC_IRQ_INT2_CHGTYP interrupt.
865                          *
866                          * If user attach MHL (with USB/TA cable and immediately detach
867                          * MHL with USB/TA cable before MAX77693_MUIC_IRQ_INT2_CHGTYP
868                          * interrupt is happened, USB/TA cable remain connected state to
869                          * target. But USB/TA cable isn't connected to target. The user
870                          * be face with unusual action. So, driver should check this
871                          * situation in spite of, that previous charger type is N/A.
872                          */
873                         break;
874                 case MAX77693_CHARGER_TYPE_USB:
875                         /* Only USB cable, PATH:AP_USB */
876                         ret = max77693_muic_set_path(info, info->path_usb, attached);
877                         if (ret < 0)
878                                 return ret;
879
880                         extcon_set_cable_state(info->edev, "USB", attached);
881                         break;
882                 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
883                         /* Only TA cable */
884                         extcon_set_cable_state(info->edev, "TA", attached);
885                         break;
886                 }
887                 break;
888         case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
889                 extcon_set_cable_state(info->edev,
890                                 "Charge-downstream", attached);
891                 break;
892         case MAX77693_CHARGER_TYPE_APPLE_500MA:
893                 extcon_set_cable_state(info->edev, "Slow-charger", attached);
894                 break;
895         case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
896                 extcon_set_cable_state(info->edev, "Fast-charger", attached);
897                 break;
898         case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
899                 break;
900         default:
901                 dev_err(info->dev,
902                         "failed to detect %s accessory (chg_type:0x%x)\n",
903                         attached ? "attached" : "detached", chg_type);
904                 return -EINVAL;
905         }
906
907         return 0;
908 }
909
910 static void max77693_muic_irq_work(struct work_struct *work)
911 {
912         struct max77693_muic_info *info = container_of(work,
913                         struct max77693_muic_info, irq_work);
914         int irq_type = -1;
915         int i, ret = 0;
916
917         if (!info->edev)
918                 return;
919
920         mutex_lock(&info->mutex);
921
922         for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
923                 if (info->irq == muic_irqs[i].virq)
924                         irq_type = muic_irqs[i].irq;
925
926         ret = max77693_bulk_read(info->max77693->regmap_muic,
927                         MAX77693_MUIC_REG_STATUS1, 2, info->status);
928         if (ret) {
929                 dev_err(info->dev, "failed to read MUIC register\n");
930                 mutex_unlock(&info->mutex);
931                 return;
932         }
933
934         switch (irq_type) {
935         case MAX77693_MUIC_IRQ_INT1_ADC:
936         case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
937         case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
938         case MAX77693_MUIC_IRQ_INT1_ADC1K:
939                 /* Handle all of accessory except for
940                    type of charger accessory */
941                 ret = max77693_muic_adc_handler(info);
942                 break;
943         case MAX77693_MUIC_IRQ_INT2_CHGTYP:
944         case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
945         case MAX77693_MUIC_IRQ_INT2_DCDTMR:
946         case MAX77693_MUIC_IRQ_INT2_DXOVP:
947         case MAX77693_MUIC_IRQ_INT2_VBVOLT:
948         case MAX77693_MUIC_IRQ_INT2_VIDRM:
949                 /* Handle charger accessory */
950                 ret = max77693_muic_chg_handler(info);
951                 break;
952         case MAX77693_MUIC_IRQ_INT3_EOC:
953         case MAX77693_MUIC_IRQ_INT3_CGMBC:
954         case MAX77693_MUIC_IRQ_INT3_OVP:
955         case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
956         case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
957         case MAX77693_MUIC_IRQ_INT3_BAT_DET:
958                 break;
959         default:
960                 dev_err(info->dev, "muic interrupt: irq %d occurred\n",
961                                 irq_type);
962                 break;
963         }
964
965         if (ret < 0)
966                 dev_err(info->dev, "failed to handle MUIC interrupt\n");
967
968         mutex_unlock(&info->mutex);
969
970         return;
971 }
972
973 static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
974 {
975         struct max77693_muic_info *info = data;
976
977         info->irq = irq;
978         schedule_work(&info->irq_work);
979
980         return IRQ_HANDLED;
981 }
982
983 static struct regmap_config max77693_muic_regmap_config = {
984         .reg_bits = 8,
985         .val_bits = 8,
986 };
987
988 static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
989 {
990         int ret = 0;
991         int adc;
992         int chg_type;
993         bool attached;
994
995         mutex_lock(&info->mutex);
996
997         /* Read STATUSx register to detect accessory */
998         ret = max77693_bulk_read(info->max77693->regmap_muic,
999                         MAX77693_MUIC_REG_STATUS1, 2, info->status);
1000         if (ret) {
1001                 dev_err(info->dev, "failed to read MUIC register\n");
1002                 mutex_unlock(&info->mutex);
1003                 return -EINVAL;
1004         }
1005
1006         adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
1007                                         &attached);
1008         if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
1009                 ret = max77693_muic_adc_handler(info);
1010                 if (ret < 0)
1011                         dev_err(info->dev, "Cannot detect accessory\n");
1012         }
1013
1014         chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
1015                                         &attached);
1016         if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
1017                 ret = max77693_muic_chg_handler(info);
1018                 if (ret < 0)
1019                         dev_err(info->dev, "Cannot detect charger accessory\n");
1020         }
1021
1022         mutex_unlock(&info->mutex);
1023
1024         return ret;
1025 }
1026
1027 static void max77693_muic_detect_cable_wq(struct work_struct *work)
1028 {
1029         struct max77693_muic_info *info = container_of(to_delayed_work(work),
1030                                 struct max77693_muic_info, wq_detcable);
1031
1032         max77693_muic_detect_accessory(info);
1033 }
1034
1035 static int max77693_muic_probe(struct platform_device *pdev)
1036 {
1037         struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
1038         struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
1039         struct max77693_muic_platform_data *muic_pdata = pdata->muic_data;
1040         struct max77693_muic_info *info;
1041         int delay_jiffies;
1042         int ret;
1043         int i;
1044         u8 id;
1045
1046         info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
1047                                    GFP_KERNEL);
1048         if (!info) {
1049                 dev_err(&pdev->dev, "failed to allocate memory\n");
1050                 return -ENOMEM;
1051         }
1052         info->dev = &pdev->dev;
1053         info->max77693 = max77693;
1054         if (info->max77693->regmap_muic) {
1055                 dev_dbg(&pdev->dev, "allocate register map\n");
1056         } else {
1057                 info->max77693->regmap_muic = devm_regmap_init_i2c(
1058                                                 info->max77693->muic,
1059                                                 &max77693_muic_regmap_config);
1060                 if (IS_ERR(info->max77693->regmap_muic)) {
1061                         ret = PTR_ERR(info->max77693->regmap_muic);
1062                         dev_err(max77693->dev,
1063                                 "failed to allocate register map: %d\n", ret);
1064                         return ret;
1065                 }
1066         }
1067
1068         /* Register input device for button of dock device */
1069         info->dock = devm_input_allocate_device(&pdev->dev);
1070         if (!info->dock) {
1071                 dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
1072                 return -ENOMEM;
1073         }
1074         info->dock->name = "max77693-muic/dock";
1075         info->dock->phys = "max77693-muic/extcon";
1076         info->dock->dev.parent = &pdev->dev;
1077
1078         __set_bit(EV_REP, info->dock->evbit);
1079
1080         input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
1081         input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
1082         input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
1083         input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
1084         input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
1085
1086         ret = input_register_device(info->dock);
1087         if (ret < 0) {
1088                 dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
1089                                 ret);
1090                 return ret;
1091         }
1092
1093         platform_set_drvdata(pdev, info);
1094         mutex_init(&info->mutex);
1095
1096         INIT_WORK(&info->irq_work, max77693_muic_irq_work);
1097
1098         /* Support irq domain for MAX77693 MUIC device */
1099         for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
1100                 struct max77693_muic_irq *muic_irq = &muic_irqs[i];
1101                 unsigned int virq = 0;
1102
1103                 virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
1104                 if (!virq) {
1105                         ret = -EINVAL;
1106                         goto err_irq;
1107                 }
1108                 muic_irq->virq = virq;
1109
1110                 ret = request_threaded_irq(virq, NULL,
1111                                 max77693_muic_irq_handler,
1112                                 IRQF_NO_SUSPEND,
1113                                 muic_irq->name, info);
1114                 if (ret) {
1115                         dev_err(&pdev->dev,
1116                                 "failed: irq request (IRQ: %d,"
1117                                 " error :%d)\n",
1118                                 muic_irq->irq, ret);
1119                         goto err_irq;
1120                 }
1121         }
1122
1123         /* Initialize extcon device */
1124         info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
1125                                   GFP_KERNEL);
1126         if (!info->edev) {
1127                 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1128                 ret = -ENOMEM;
1129                 goto err_irq;
1130         }
1131         info->edev->name = DEV_NAME;
1132         info->edev->supported_cable = max77693_extcon_cable;
1133         ret = extcon_dev_register(info->edev, NULL);
1134         if (ret) {
1135                 dev_err(&pdev->dev, "failed to register extcon device\n");
1136                 goto err_irq;
1137         }
1138
1139         /* Initialize MUIC register by using platform data */
1140         for (i = 0 ; i < muic_pdata->num_init_data ; i++) {
1141                 enum max77693_irq_source irq_src = MAX77693_IRQ_GROUP_NR;
1142
1143                 max77693_write_reg(info->max77693->regmap_muic,
1144                                 muic_pdata->init_data[i].addr,
1145                                 muic_pdata->init_data[i].data);
1146
1147                 switch (muic_pdata->init_data[i].addr) {
1148                 case MAX77693_MUIC_REG_INTMASK1:
1149                         irq_src = MUIC_INT1;
1150                         break;
1151                 case MAX77693_MUIC_REG_INTMASK2:
1152                         irq_src = MUIC_INT2;
1153                         break;
1154                 case MAX77693_MUIC_REG_INTMASK3:
1155                         irq_src = MUIC_INT3;
1156                         break;
1157                 }
1158
1159                 if (irq_src < MAX77693_IRQ_GROUP_NR)
1160                         info->max77693->irq_masks_cur[irq_src]
1161                                 = muic_pdata->init_data[i].data;
1162         }
1163
1164         /*
1165          * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
1166          * h/w path of COMP2/COMN1 on CONTROL1 register.
1167          */
1168         if (muic_pdata->path_uart)
1169                 info->path_uart = muic_pdata->path_uart;
1170         else
1171                 info->path_uart = CONTROL1_SW_UART;
1172
1173         if (muic_pdata->path_usb)
1174                 info->path_usb = muic_pdata->path_usb;
1175         else
1176                 info->path_usb = CONTROL1_SW_USB;
1177
1178         /* Set initial path for UART */
1179          max77693_muic_set_path(info, info->path_uart, true);
1180
1181         /* Check revision number of MUIC device*/
1182         ret = max77693_read_reg(info->max77693->regmap_muic,
1183                         MAX77693_MUIC_REG_ID, &id);
1184         if (ret < 0) {
1185                 dev_err(&pdev->dev, "failed to read revision number\n");
1186                 goto err_extcon;
1187         }
1188         dev_info(info->dev, "device ID : 0x%x\n", id);
1189
1190         /* Set ADC debounce time */
1191         max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1192
1193         /*
1194          * Detect accessory after completing the initialization of platform
1195          *
1196          * - Use delayed workqueue to detect cable state and then
1197          * notify cable state to notifiee/platform through uevent.
1198          * After completing the booting of platform, the extcon provider
1199          * driver should notify cable state to upper layer.
1200          */
1201         INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
1202         if (muic_pdata->detcable_delay_ms)
1203                 delay_jiffies = msecs_to_jiffies(muic_pdata->detcable_delay_ms);
1204         else
1205                 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1206         schedule_delayed_work(&info->wq_detcable, delay_jiffies);
1207
1208         return ret;
1209
1210 err_extcon:
1211         extcon_dev_unregister(info->edev);
1212 err_irq:
1213         while (--i >= 0)
1214                 free_irq(muic_irqs[i].virq, info);
1215         return ret;
1216 }
1217
1218 static int max77693_muic_remove(struct platform_device *pdev)
1219 {
1220         struct max77693_muic_info *info = platform_get_drvdata(pdev);
1221         int i;
1222
1223         for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
1224                 free_irq(muic_irqs[i].virq, info);
1225         cancel_work_sync(&info->irq_work);
1226         input_unregister_device(info->dock);
1227         extcon_dev_unregister(info->edev);
1228
1229         return 0;
1230 }
1231
1232 static struct platform_driver max77693_muic_driver = {
1233         .driver         = {
1234                 .name   = DEV_NAME,
1235                 .owner  = THIS_MODULE,
1236         },
1237         .probe          = max77693_muic_probe,
1238         .remove         = max77693_muic_remove,
1239 };
1240
1241 module_platform_driver(max77693_muic_driver);
1242
1243 MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
1244 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1245 MODULE_LICENSE("GPL");
1246 MODULE_ALIAS("platform:extcon-max77693");