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