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