2 * extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC
4 * Copyright (C) 2012 Samsung Electrnoics
5 * Chanwoo Choi <cw00.choi@samsung.com>
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.
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.
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>
32 #define DEV_NAME "max77693-muic"
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,
41 struct max77693_muic_info {
43 struct max77693_dev *max77693;
44 struct extcon_dev *edev;
46 int prev_cable_type_gnd;
52 struct work_struct irq_work;
55 /* Button of dock device */
56 struct input_dev *dock;
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,
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,
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
82 struct max77693_muic_irq {
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" },
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,
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 */
151 /* MAX77693 MUIC device support below list of accessories(external connector) */
153 EXTCON_CABLE_USB = 0,
154 EXTCON_CABLE_USB_HOST,
156 EXTCON_CABLE_FAST_CHARGER,
157 EXTCON_CABLE_SLOW_CHARGER,
158 EXTCON_CABLE_CHARGE_DOWNSTREAM,
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,
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",
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
197 static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
198 enum max77693_muic_adc_debounce_time 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);
212 dev_err(info->dev, "failed to set ADC debounce time\n");
215 dev_err(info->dev, "invalid ADC debounce time\n");
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)
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
233 static int max77693_muic_set_path(struct max77693_muic_info *info,
234 u8 val, bool attached)
242 ctrl1 = CONTROL1_SW_OPEN;
244 ret = max77693_update_reg(info->max77693->regmap_muic,
245 MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
247 dev_err(info->dev, "failed to update MUIC register\n");
252 ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
254 ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
256 ret = max77693_update_reg(info->max77693->regmap_muic,
257 MAX77693_MUIC_REG_CTRL2, ctrl2,
258 CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
260 dev_err(info->dev, "failed to update MUIC register\n");
265 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
266 ctrl1, ctrl2, attached ? "attached" : "detached");
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
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
284 static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
285 enum max77693_muic_cable_group group, bool *attached)
295 case MAX77693_CABLE_GROUP_ADC:
297 * Read ADC value to check cable type and decide cable state
298 * according to cable type
300 adc = info->status[0] & STATUS1_ADC_MASK;
301 adc >>= STATUS1_ADC_SHIFT;
304 * Check current cable state/cable type and store cable type
305 * (info->prev_cable_type) for handling cable when cable is
308 if (adc == MAX77693_MUIC_ADC_OPEN) {
311 cable_type = info->prev_cable_type;
312 info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
316 cable_type = info->prev_cable_type = adc;
319 case MAX77693_CABLE_GROUP_ADC_GND:
321 * Read ADC value to check cable type and decide cable state
322 * according to cable type
324 adc = info->status[0] & STATUS1_ADC_MASK;
325 adc >>= STATUS1_ADC_SHIFT;
328 * Check current cable state/cable type and store cable type
329 * (info->prev_cable_type/_gnd) for handling cable when cable
332 if (adc == MAX77693_MUIC_ADC_OPEN) {
335 cable_type = info->prev_cable_type_gnd;
336 info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
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;
345 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
346 vbvolt >>= STATUS2_VBVOLT_SHIFT;
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
356 cable_type = ((0x1 << 8)
361 info->prev_cable_type = adc;
362 info->prev_cable_type_gnd = cable_type;
366 case MAX77693_CABLE_GROUP_CHG:
368 * Read charger type to check cable type and decide cable state
369 * according to type of charger cable.
371 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
372 chg_type >>= STATUS2_CHGTYP_SHIFT;
374 if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
377 cable_type = info->prev_chg_type;
378 info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
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.
387 cable_type = info->prev_chg_type = chg_type;
391 case MAX77693_CABLE_GROUP_VBVOLT:
393 * Read ADC value to check cable type and decide cable state
394 * according to cable type
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;
401 if (adc == MAX77693_MUIC_ADC_OPEN
402 && chg_type == MAX77693_CHARGER_TYPE_NONE)
408 * Read vbvolt field, if vbvolt is 1,
409 * this cable is used for charging.
411 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
412 vbvolt >>= STATUS2_VBVOLT_SHIFT;
417 dev_err(info->dev, "Unknown cable group (%d)\n", group);
418 cable_type = -EINVAL;
425 static int max77693_muic_dock_handler(struct max77693_muic_info *info,
426 int cable_type, bool attached)
429 char dock_name[CABLE_NAME_MAX];
432 "external connector is %s (adc:0x%02x)\n",
433 attached ? "attached" : "detached", cable_type);
435 switch (cable_type) {
436 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
438 ret = max77693_muic_set_path(info,
439 CONTROL1_SW_USB, attached);
444 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
446 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
447 strcpy(dock_name, "Dock-Car");
449 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
450 strcpy(dock_name, "Dock-Desk");
452 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
453 strcpy(dock_name, "Dock-Audio");
455 extcon_set_cable_state(info->edev, "USB", false);
459 /* Dock-Car/Desk/Audio, PATH:AUDIO */
460 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
463 extcon_set_cable_state(info->edev, dock_name, attached);
469 static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
470 int button_type, bool attached)
472 struct input_dev *dock = info->dock;
476 switch (button_type) {
477 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
478 ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
480 code = KEY_PREVIOUSSONG;
482 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
483 ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
487 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
489 code = KEY_VOLUMEDOWN;
491 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
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;
502 "failed to detect %s key (adc:0x%x)\n",
503 attached ? "pressed" : "released", button_type);
508 input_event(dock, EV_KEY, code, attached);
515 static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
521 cable_type_gnd = max77693_muic_get_cable_type(info,
522 MAX77693_CABLE_GROUP_ADC_GND, &attached);
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);
531 extcon_set_cable_state(info->edev, "USB-Host", attached);
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);
538 extcon_set_cable_state(info->edev,
539 "Audio-video-load", attached);
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);
547 dev_err(info->dev, "failed to detect %s accessory\n",
548 attached ? "attached" : "detached");
557 static int max77693_muic_jig_handler(struct max77693_muic_info *info,
558 int cable_type, bool attached)
562 u8 path = CONTROL1_SW_OPEN;
565 "external connector is %s (adc:0x%02x)\n",
566 attached ? "attached" : "detached", cable_type);
568 switch (cable_type) {
569 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
571 strcpy(cable_name, "JIG-USB-OFF");
572 path = CONTROL1_SW_USB;
574 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
576 strcpy(cable_name, "JIG-USB-ON");
577 path = CONTROL1_SW_USB;
579 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
581 strcpy(cable_name, "JIG-UART-OFF");
582 path = CONTROL1_SW_UART;
586 ret = max77693_muic_set_path(info, path, attached);
590 extcon_set_cable_state(info->edev, cable_name, attached);
595 static int max77693_muic_adc_handler(struct max77693_muic_info *info)
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);
607 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
608 attached ? "attached" : "detached", cable_type,
609 info->prev_cable_type);
611 switch (cable_type) {
612 case MAX77693_MUIC_ADC_GROUND:
613 /* USB_OTG/MHL/Audio */
614 max77693_muic_adc_ground_handler(info);
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:
620 ret = max77693_muic_jig_handler(info, cable_type, attached);
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 */
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.
637 ret = max77693_muic_dock_handler(info, cable_type, attached);
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 */
647 * Button of DOCK device
648 * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
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.
657 button_type = info->prev_button_type = cable_type;
659 button_type = info->prev_button_type;
661 ret = max77693_muic_dock_button_handler(info, button_type,
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:
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.
690 "accessory is %s but it isn't used (adc:0x%x)\n",
691 attached ? "attached" : "detached", cable_type);
695 "failed to detect %s accessory (adc:0x%x)\n",
696 attached ? "attached" : "detached", cable_type);
705 static int max77693_muic_chg_handler(struct max77693_muic_info *info)
714 chg_type = max77693_muic_get_cable_type(info,
715 MAX77693_CABLE_GROUP_CHG, &attached);
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);
723 case MAX77693_CHARGER_TYPE_USB:
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.
732 cable_type_gnd = max77693_muic_get_cable_type(info,
733 MAX77693_CABLE_GROUP_ADC_GND,
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);
740 extcon_set_cable_state(info->edev,
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.
753 cable_type = max77693_muic_get_cable_type(info,
754 MAX77693_CABLE_GROUP_ADC,
756 if (cable_type == MAX77693_MUIC_ADC_AV_CABLE_NOLOAD) {
757 extcon_set_cable_state(info->edev, "USB", attached);
760 extcon_set_cable_state(info->edev,
761 "Dock-Audio", false);
765 /* Only USB cable, PATH:AP_USB */
766 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
769 extcon_set_cable_state(info->edev, "USB", attached);
771 case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
772 extcon_set_cable_state(info->edev,
773 "Charge-downstream", attached);
775 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
776 extcon_set_cable_state(info->edev, "TA", attached);
778 case MAX77693_CHARGER_TYPE_APPLE_500MA:
779 extcon_set_cable_state(info->edev, "Slow-charger", attached);
781 case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
782 extcon_set_cable_state(info->edev, "Fast-charger", attached);
784 case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
788 "failed to detect %s accessory (chg_type:0x%x)\n",
789 attached ? "attached" : "detached", chg_type);
798 static void max77693_muic_irq_work(struct work_struct *work)
800 struct max77693_muic_info *info = container_of(work,
801 struct max77693_muic_info, irq_work);
808 mutex_lock(&info->mutex);
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;
814 ret = max77693_bulk_read(info->max77693->regmap_muic,
815 MAX77693_MUIC_REG_STATUS1, 2, info->status);
817 dev_err(info->dev, "failed to read MUIC register\n");
818 mutex_unlock(&info->mutex);
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);
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);
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:
848 dev_err(info->dev, "muic interrupt: irq %d occurred\n",
854 dev_err(info->dev, "failed to handle MUIC interrupt\n");
856 mutex_unlock(&info->mutex);
861 static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
863 struct max77693_muic_info *info = data;
866 schedule_work(&info->irq_work);
871 static struct regmap_config max77693_muic_regmap_config = {
876 static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
883 mutex_lock(&info->mutex);
885 /* Read STATUSx register to detect accessory */
886 ret = max77693_bulk_read(info->max77693->regmap_muic,
887 MAX77693_MUIC_REG_STATUS1, 2, info->status);
889 dev_err(info->dev, "failed to read MUIC register\n");
890 mutex_unlock(&info->mutex);
894 adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
896 if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
897 ret = max77693_muic_adc_handler(info);
899 dev_err(info->dev, "Cannot detect accessory\n");
902 chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
904 if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
905 ret = max77693_muic_chg_handler(info);
907 dev_err(info->dev, "Cannot detect charger accessory\n");
910 mutex_unlock(&info->mutex);
915 static int max77693_muic_probe(struct platform_device *pdev)
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;
924 info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
927 dev_err(&pdev->dev, "failed to allocate memory\n");
930 info->dev = &pdev->dev;
931 info->max77693 = max77693;
932 if (info->max77693->regmap_muic) {
933 dev_dbg(&pdev->dev, "allocate register map\n");
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);
946 /* Register input device for button of dock device */
947 info->dock = input_allocate_device();
949 dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
952 info->dock->name = "max77693-muic/dock";
953 info->dock->phys = "max77693-muic/extcon";
954 info->dock->dev.parent = &pdev->dev;
956 __set_bit(EV_REP, info->dock->evbit);
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);
964 ret = input_register_device(info->dock);
966 dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
971 platform_set_drvdata(pdev, info);
972 mutex_init(&info->mutex);
974 INIT_WORK(&info->irq_work, max77693_muic_irq_work);
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;
981 virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
986 muic_irq->virq = virq;
988 ret = request_threaded_irq(virq, NULL,
989 max77693_muic_irq_handler,
990 IRQF_ONESHOT, muic_irq->name, info);
993 "failed: irq request (IRQ: %d,"
1001 /* Initialize extcon device */
1002 info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
1005 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1009 info->edev->name = DEV_NAME;
1010 info->edev->supported_cable = max77693_extcon_cable;
1011 ret = extcon_dev_register(info->edev, NULL);
1013 dev_err(&pdev->dev, "failed to register extcon device\n");
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;
1021 max77693_write_reg(info->max77693->regmap_muic,
1022 muic_pdata->init_data[i].addr,
1023 muic_pdata->init_data[i].data);
1025 switch (muic_pdata->init_data[i].addr) {
1026 case MAX77693_MUIC_REG_INTMASK1:
1027 irq_src = MUIC_INT1;
1029 case MAX77693_MUIC_REG_INTMASK2:
1030 irq_src = MUIC_INT2;
1032 case MAX77693_MUIC_REG_INTMASK3:
1033 irq_src = MUIC_INT3;
1037 if (irq_src < MAX77693_IRQ_GROUP_NR)
1038 info->max77693->irq_masks_cur[irq_src]
1039 = muic_pdata->init_data[i].data;
1042 /* Check revision number of MUIC device*/
1043 ret = max77693_read_reg(info->max77693->regmap_muic,
1044 MAX77693_MUIC_REG_ID, &id);
1046 dev_err(&pdev->dev, "failed to read revision number\n");
1049 dev_info(info->dev, "device ID : 0x%x\n", id);
1051 /* Set ADC debounce time */
1052 max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1054 /* Detect accessory on boot */
1055 max77693_muic_detect_accessory(info);
1060 extcon_dev_unregister(info->edev);
1063 free_irq(muic_irqs[i].virq, info);
1067 static int max77693_muic_remove(struct platform_device *pdev)
1069 struct max77693_muic_info *info = platform_get_drvdata(pdev);
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);
1081 static struct platform_driver max77693_muic_driver = {
1084 .owner = THIS_MODULE,
1086 .probe = max77693_muic_probe,
1087 .remove = max77693_muic_remove,
1090 module_platform_driver(max77693_muic_driver);
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");