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"
33 #define DELAY_MS_DEFAULT 20000 /* unit: millisecond */
35 enum max77693_muic_adc_debounce_time {
36 ADC_DEBOUNCE_TIME_5MS = 0,
37 ADC_DEBOUNCE_TIME_10MS,
38 ADC_DEBOUNCE_TIME_25MS,
39 ADC_DEBOUNCE_TIME_38_62MS,
42 struct max77693_muic_info {
44 struct max77693_dev *max77693;
45 struct extcon_dev *edev;
47 int prev_cable_type_gnd;
53 struct work_struct irq_work;
57 * Use delayed workqueue to detect cable state and then
58 * notify cable state to notifiee/platform through uevent.
59 * After completing the booting of platform, the extcon provider
60 * driver should notify cable state to upper layer.
62 struct delayed_work wq_detcable;
64 /* Button of dock device */
65 struct input_dev *dock;
68 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
69 * h/w path of COMP2/COMN1 on CONTROL1 register.
75 enum max77693_muic_cable_group {
76 MAX77693_CABLE_GROUP_ADC = 0,
77 MAX77693_CABLE_GROUP_ADC_GND,
78 MAX77693_CABLE_GROUP_CHG,
79 MAX77693_CABLE_GROUP_VBVOLT,
82 enum max77693_muic_charger_type {
83 MAX77693_CHARGER_TYPE_NONE = 0,
84 MAX77693_CHARGER_TYPE_USB,
85 MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,
86 MAX77693_CHARGER_TYPE_DEDICATED_CHG,
87 MAX77693_CHARGER_TYPE_APPLE_500MA,
88 MAX77693_CHARGER_TYPE_APPLE_1A_2A,
89 MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,
93 * struct max77693_muic_irq
94 * @irq: the index of irq list of MUIC device.
95 * @name: the name of irq.
96 * @virq: the virtual irq to use irq domain
98 struct max77693_muic_irq {
104 static struct max77693_muic_irq muic_irqs[] = {
105 { MAX77693_MUIC_IRQ_INT1_ADC, "muic-ADC" },
106 { MAX77693_MUIC_IRQ_INT1_ADC_LOW, "muic-ADCLOW" },
107 { MAX77693_MUIC_IRQ_INT1_ADC_ERR, "muic-ADCError" },
108 { MAX77693_MUIC_IRQ_INT1_ADC1K, "muic-ADC1K" },
109 { MAX77693_MUIC_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
110 { MAX77693_MUIC_IRQ_INT2_CHGDETREUN, "muic-CHGDETREUN" },
111 { MAX77693_MUIC_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
112 { MAX77693_MUIC_IRQ_INT2_DXOVP, "muic-DXOVP" },
113 { MAX77693_MUIC_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
114 { MAX77693_MUIC_IRQ_INT2_VIDRM, "muic-VIDRM" },
115 { MAX77693_MUIC_IRQ_INT3_EOC, "muic-EOC" },
116 { MAX77693_MUIC_IRQ_INT3_CGMBC, "muic-CGMBC" },
117 { MAX77693_MUIC_IRQ_INT3_OVP, "muic-OVP" },
118 { MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR, "muic-MBCCHG_ERR" },
119 { MAX77693_MUIC_IRQ_INT3_CHG_ENABLED, "muic-CHG_ENABLED" },
120 { MAX77693_MUIC_IRQ_INT3_BAT_DET, "muic-BAT_DET" },
123 /* Define supported accessory type */
124 enum max77693_muic_acc_type {
125 MAX77693_MUIC_ADC_GROUND = 0x0,
126 MAX77693_MUIC_ADC_SEND_END_BUTTON,
127 MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,
128 MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,
129 MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,
130 MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,
131 MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,
132 MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,
133 MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,
134 MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,
135 MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,
136 MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,
137 MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,
138 MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,
139 MAX77693_MUIC_ADC_RESERVED_ACC_1,
140 MAX77693_MUIC_ADC_RESERVED_ACC_2,
141 MAX77693_MUIC_ADC_RESERVED_ACC_3,
142 MAX77693_MUIC_ADC_RESERVED_ACC_4,
143 MAX77693_MUIC_ADC_RESERVED_ACC_5,
144 MAX77693_MUIC_ADC_CEA936_AUDIO,
145 MAX77693_MUIC_ADC_PHONE_POWERED_DEV,
146 MAX77693_MUIC_ADC_TTY_CONVERTER,
147 MAX77693_MUIC_ADC_UART_CABLE,
148 MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,
149 MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,
150 MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,
151 MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,
152 MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,
153 MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,
154 MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,
155 MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,
156 MAX77693_MUIC_ADC_OPEN,
158 /* The below accessories have same ADC value so ADCLow and
159 ADC1K bit is used to separate specific accessory */
160 MAX77693_MUIC_GND_USB_OTG = 0x100, /* ADC:0x0, VBVolot:0, ADCLow:0, ADC1K:0 */
161 MAX77693_MUIC_GND_USB_OTG_VB = 0x104, /* ADC:0x0, VBVolot:1, ADCLow:0, ADC1K:0 */
162 MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:0 */
163 MAX77693_MUIC_GND_MHL = 0x103, /* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:1 */
164 MAX77693_MUIC_GND_MHL_VB = 0x107, /* ADC:0x0, VBVolot:1, ADCLow:1, ADC1K:1 */
167 /* MAX77693 MUIC device support below list of accessories(external connector) */
169 EXTCON_CABLE_USB = 0,
170 EXTCON_CABLE_USB_HOST,
172 EXTCON_CABLE_FAST_CHARGER,
173 EXTCON_CABLE_SLOW_CHARGER,
174 EXTCON_CABLE_CHARGE_DOWNSTREAM,
177 EXTCON_CABLE_JIG_USB_ON,
178 EXTCON_CABLE_JIG_USB_OFF,
179 EXTCON_CABLE_JIG_UART_OFF,
180 EXTCON_CABLE_JIG_UART_ON,
181 EXTCON_CABLE_DOCK_SMART,
182 EXTCON_CABLE_DOCK_DESK,
183 EXTCON_CABLE_DOCK_AUDIO,
188 static const char *max77693_extcon_cable[] = {
189 [EXTCON_CABLE_USB] = "USB",
190 [EXTCON_CABLE_USB_HOST] = "USB-Host",
191 [EXTCON_CABLE_TA] = "TA",
192 [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
193 [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
194 [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
195 [EXTCON_CABLE_MHL] = "MHL",
196 [EXTCON_CABLE_MHL_TA] = "MHL_TA",
197 [EXTCON_CABLE_JIG_USB_ON] = "JIG-USB-ON",
198 [EXTCON_CABLE_JIG_USB_OFF] = "JIG-USB-OFF",
199 [EXTCON_CABLE_JIG_UART_OFF] = "JIG-UART-OFF",
200 [EXTCON_CABLE_JIG_UART_ON] = "Dock-Car",
201 [EXTCON_CABLE_DOCK_SMART] = "Dock-Smart",
202 [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk",
203 [EXTCON_CABLE_DOCK_AUDIO] = "Dock-Audio",
209 * max77693_muic_set_debounce_time - Set the debounce time of ADC
210 * @info: the instance including private data of max77693 MUIC
211 * @time: the debounce time of ADC
213 static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
214 enum max77693_muic_adc_debounce_time time)
219 case ADC_DEBOUNCE_TIME_5MS:
220 case ADC_DEBOUNCE_TIME_10MS:
221 case ADC_DEBOUNCE_TIME_25MS:
222 case ADC_DEBOUNCE_TIME_38_62MS:
223 ret = max77693_update_reg(info->max77693->regmap_muic,
224 MAX77693_MUIC_REG_CTRL3,
225 time << CONTROL3_ADCDBSET_SHIFT,
226 CONTROL3_ADCDBSET_MASK);
228 dev_err(info->dev, "failed to set ADC debounce time\n");
233 dev_err(info->dev, "invalid ADC debounce time\n");
241 * max77693_muic_set_path - Set hardware line according to attached cable
242 * @info: the instance including private data of max77693 MUIC
243 * @value: the path according to attached cable
244 * @attached: the state of cable (true:attached, false:detached)
246 * The max77693 MUIC device share outside H/W line among a varity of cables
247 * so, this function set internal path of H/W line according to the type of
250 static int max77693_muic_set_path(struct max77693_muic_info *info,
251 u8 val, bool attached)
259 ctrl1 = CONTROL1_SW_OPEN;
261 ret = max77693_update_reg(info->max77693->regmap_muic,
262 MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
264 dev_err(info->dev, "failed to update MUIC register\n");
269 ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
271 ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
273 ret = max77693_update_reg(info->max77693->regmap_muic,
274 MAX77693_MUIC_REG_CTRL2, ctrl2,
275 CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
277 dev_err(info->dev, "failed to update MUIC register\n");
282 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
283 ctrl1, ctrl2, attached ? "attached" : "detached");
289 * max77693_muic_get_cable_type - Return cable type and check cable state
290 * @info: the instance including private data of max77693 MUIC
291 * @group: the path according to attached cable
292 * @attached: store cable state and return
294 * This function check the cable state either attached or detached,
295 * and then divide precise type of cable according to cable group.
296 * - MAX77693_CABLE_GROUP_ADC
297 * - MAX77693_CABLE_GROUP_ADC_GND
298 * - MAX77693_CABLE_GROUP_CHG
299 * - MAX77693_CABLE_GROUP_VBVOLT
301 static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
302 enum max77693_muic_cable_group group, bool *attached)
312 case MAX77693_CABLE_GROUP_ADC:
314 * Read ADC value to check cable type and decide cable state
315 * according to cable type
317 adc = info->status[0] & STATUS1_ADC_MASK;
318 adc >>= STATUS1_ADC_SHIFT;
321 * Check current cable state/cable type and store cable type
322 * (info->prev_cable_type) for handling cable when cable is
325 if (adc == MAX77693_MUIC_ADC_OPEN) {
328 cable_type = info->prev_cable_type;
329 info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
333 cable_type = info->prev_cable_type = adc;
336 case MAX77693_CABLE_GROUP_ADC_GND:
338 * Read ADC value to check cable type and decide cable state
339 * according to cable type
341 adc = info->status[0] & STATUS1_ADC_MASK;
342 adc >>= STATUS1_ADC_SHIFT;
345 * Check current cable state/cable type and store cable type
346 * (info->prev_cable_type/_gnd) for handling cable when cable
349 if (adc == MAX77693_MUIC_ADC_OPEN) {
352 cable_type = info->prev_cable_type_gnd;
353 info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
357 adclow = info->status[0] & STATUS1_ADCLOW_MASK;
358 adclow >>= STATUS1_ADCLOW_SHIFT;
359 adc1k = info->status[0] & STATUS1_ADC1K_MASK;
360 adc1k >>= STATUS1_ADC1K_SHIFT;
362 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
363 vbvolt >>= STATUS2_VBVOLT_SHIFT;
366 * [0x1][VBVolt][ADCLow][ADC1K]
367 * [0x1 0 0 0 ] : USB_OTG
368 * [0x1 1 0 0 ] : USB_OTG_VB
369 * [0x1 0 1 0 ] : Audio Video Cable with load
370 * [0x1 0 1 1 ] : MHL without charging connector
371 * [0x1 1 1 1 ] : MHL with charging connector
373 cable_type = ((0x1 << 8)
378 info->prev_cable_type = adc;
379 info->prev_cable_type_gnd = cable_type;
383 case MAX77693_CABLE_GROUP_CHG:
385 * Read charger type to check cable type and decide cable state
386 * according to type of charger cable.
388 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
389 chg_type >>= STATUS2_CHGTYP_SHIFT;
391 if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
394 cable_type = info->prev_chg_type;
395 info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
400 * Check current cable state/cable type and store cable
401 * type(info->prev_chg_type) for handling cable when
402 * charger cable is detached.
404 cable_type = info->prev_chg_type = chg_type;
408 case MAX77693_CABLE_GROUP_VBVOLT:
410 * Read ADC value to check cable type and decide cable state
411 * according to cable type
413 adc = info->status[0] & STATUS1_ADC_MASK;
414 adc >>= STATUS1_ADC_SHIFT;
415 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
416 chg_type >>= STATUS2_CHGTYP_SHIFT;
418 if (adc == MAX77693_MUIC_ADC_OPEN
419 && chg_type == MAX77693_CHARGER_TYPE_NONE)
425 * Read vbvolt field, if vbvolt is 1,
426 * this cable is used for charging.
428 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
429 vbvolt >>= STATUS2_VBVOLT_SHIFT;
434 dev_err(info->dev, "Unknown cable group (%d)\n", group);
435 cable_type = -EINVAL;
442 static int max77693_muic_dock_handler(struct max77693_muic_info *info,
443 int cable_type, bool attached)
448 char dock_name[CABLE_NAME_MAX];
451 "external connector is %s (adc:0x%02x)\n",
452 attached ? "attached" : "detached", cable_type);
454 switch (cable_type) {
455 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
457 * Check power cable whether attached or detached state.
458 * The Dock-Smart device need surely external power supply.
459 * If power cable(USB/TA) isn't connected to Dock device,
460 * user can't use Dock-Smart for desktop mode.
462 vbvolt = max77693_muic_get_cable_type(info,
463 MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);
464 if (attached && !vbvolt) {
466 "Cannot detect external power supply\n");
471 * Notify Dock-Smart/MHL state.
472 * - Dock-Smart device include three type of cable which
473 * are HDMI, USB for mouse/keyboard and micro-usb port
474 * for USB/TA cable. Dock-Smart device need always exteranl
475 * power supply(USB/TA cable through micro-usb cable). Dock-
476 * Smart device support screen output of target to separate
477 * monitor and mouse/keyboard for desktop mode.
479 * Features of 'USB/TA cable with Dock-Smart device'
481 * - Support external output feature of audio
482 * - Support charging through micro-usb port without data
483 * connection if TA cable is connected to target.
484 * - Support charging and data connection through micro-usb port
485 * if USB cable is connected between target and host
487 * - Support OTG device (Mouse/Keyboard)
489 ret = max77693_muic_set_path(info, info->path_usb, attached);
493 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
494 extcon_set_cable_state(info->edev, "MHL", attached);
496 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
497 strcpy(dock_name, "Dock-Car");
499 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
500 strcpy(dock_name, "Dock-Desk");
502 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
503 strcpy(dock_name, "Dock-Audio");
505 extcon_set_cable_state(info->edev, "USB", false);
508 dev_err(info->dev, "failed to detect %s dock device\n",
509 attached ? "attached" : "detached");
513 /* Dock-Car/Desk/Audio, PATH:AUDIO */
514 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
517 extcon_set_cable_state(info->edev, dock_name, attached);
523 static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
524 int button_type, bool attached)
526 struct input_dev *dock = info->dock;
529 switch (button_type) {
530 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
531 ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
533 code = KEY_PREVIOUSSONG;
535 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
536 ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
540 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
542 code = KEY_VOLUMEDOWN;
544 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
548 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
549 ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
550 /* DOCK_KEY_PLAY_PAUSE */
551 code = KEY_PLAYPAUSE;
555 "failed to detect %s key (adc:0x%x)\n",
556 attached ? "pressed" : "released", button_type);
560 input_event(dock, EV_KEY, code, attached);
566 static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
572 cable_type_gnd = max77693_muic_get_cable_type(info,
573 MAX77693_CABLE_GROUP_ADC_GND, &attached);
575 switch (cable_type_gnd) {
576 case MAX77693_MUIC_GND_USB_OTG:
577 case MAX77693_MUIC_GND_USB_OTG_VB:
578 /* USB_OTG, PATH: AP_USB */
579 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
582 extcon_set_cable_state(info->edev, "USB-Host", attached);
584 case MAX77693_MUIC_GND_AV_CABLE_LOAD:
585 /* Audio Video Cable with load, PATH:AUDIO */
586 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
589 extcon_set_cable_state(info->edev,
590 "Audio-video-load", attached);
592 case MAX77693_MUIC_GND_MHL:
593 case MAX77693_MUIC_GND_MHL_VB:
594 /* MHL or MHL with USB/TA cable */
595 extcon_set_cable_state(info->edev, "MHL", attached);
598 dev_err(info->dev, "failed to detect %s cable of gnd type\n",
599 attached ? "attached" : "detached");
606 static int max77693_muic_jig_handler(struct max77693_muic_info *info,
607 int cable_type, bool attached)
611 u8 path = CONTROL1_SW_OPEN;
614 "external connector is %s (adc:0x%02x)\n",
615 attached ? "attached" : "detached", cable_type);
617 switch (cable_type) {
618 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
620 strcpy(cable_name, "JIG-USB-OFF");
621 path = CONTROL1_SW_USB;
623 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
625 strcpy(cable_name, "JIG-USB-ON");
626 path = CONTROL1_SW_USB;
628 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
630 strcpy(cable_name, "JIG-UART-OFF");
631 path = CONTROL1_SW_UART;
634 dev_err(info->dev, "failed to detect %s jig cable\n",
635 attached ? "attached" : "detached");
639 ret = max77693_muic_set_path(info, path, attached);
643 extcon_set_cable_state(info->edev, cable_name, attached);
648 static int max77693_muic_adc_handler(struct max77693_muic_info *info)
655 /* Check accessory state which is either detached or attached */
656 cable_type = max77693_muic_get_cable_type(info,
657 MAX77693_CABLE_GROUP_ADC, &attached);
660 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
661 attached ? "attached" : "detached", cable_type,
662 info->prev_cable_type);
664 switch (cable_type) {
665 case MAX77693_MUIC_ADC_GROUND:
666 /* USB_OTG/MHL/Audio */
667 max77693_muic_adc_ground_handler(info);
669 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
670 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
671 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
673 ret = max77693_muic_jig_handler(info, cable_type, attached);
677 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
678 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
679 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
680 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
684 * The MAX77693 MUIC device can detect total 34 cable type
685 * except of charger cable and MUIC device didn't define
686 * specfic role of cable in the range of from 0x01 to 0x12
687 * of ADC value. So, can use/define cable with no role according
688 * to schema of hardware board.
690 ret = max77693_muic_dock_handler(info, cable_type, attached);
694 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON: /* DOCK_KEY_PREV */
695 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON: /* DOCK_KEY_NEXT */
696 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: /* DOCK_VOL_DOWN */
697 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: /* DOCK_VOL_UP */
698 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON: /* DOCK_KEY_PLAY_PAUSE */
700 * Button of DOCK device
701 * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
703 * The MAX77693 MUIC device can detect total 34 cable type
704 * except of charger cable and MUIC device didn't define
705 * specfic role of cable in the range of from 0x01 to 0x12
706 * of ADC value. So, can use/define cable with no role according
707 * to schema of hardware board.
710 button_type = info->prev_button_type = cable_type;
712 button_type = info->prev_button_type;
714 ret = max77693_muic_dock_button_handler(info, button_type,
719 case MAX77693_MUIC_ADC_SEND_END_BUTTON:
720 case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
721 case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
722 case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
723 case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
724 case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
725 case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
726 case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
727 case MAX77693_MUIC_ADC_RESERVED_ACC_1:
728 case MAX77693_MUIC_ADC_RESERVED_ACC_2:
729 case MAX77693_MUIC_ADC_RESERVED_ACC_4:
730 case MAX77693_MUIC_ADC_RESERVED_ACC_5:
731 case MAX77693_MUIC_ADC_CEA936_AUDIO:
732 case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
733 case MAX77693_MUIC_ADC_TTY_CONVERTER:
734 case MAX77693_MUIC_ADC_UART_CABLE:
735 case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
736 case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
738 * This accessory isn't used in general case if it is specially
739 * needed to detect additional accessory, should implement
740 * proper operation when this accessory is attached/detached.
743 "accessory is %s but it isn't used (adc:0x%x)\n",
744 attached ? "attached" : "detached", cable_type);
748 "failed to detect %s accessory (adc:0x%x)\n",
749 attached ? "attached" : "detached", cable_type);
756 static int max77693_muic_chg_handler(struct max77693_muic_info *info)
765 chg_type = max77693_muic_get_cable_type(info,
766 MAX77693_CABLE_GROUP_CHG, &attached);
769 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
770 attached ? "attached" : "detached",
771 chg_type, info->prev_chg_type);
774 case MAX77693_CHARGER_TYPE_USB:
775 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
776 case MAX77693_CHARGER_TYPE_NONE:
777 /* Check MAX77693_CABLE_GROUP_ADC_GND type */
778 cable_type_gnd = max77693_muic_get_cable_type(info,
779 MAX77693_CABLE_GROUP_ADC_GND,
781 switch (cable_type_gnd) {
782 case MAX77693_MUIC_GND_MHL:
783 case MAX77693_MUIC_GND_MHL_VB:
785 * MHL cable with MHL_TA(USB/TA) cable
786 * - MHL cable include two port(HDMI line and separate micro-
787 * usb port. When the target connect MHL cable, extcon driver
788 * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA
789 * cable is connected, extcon driver notify state to notifiee
790 * for charging battery.
792 * Features of 'MHL_TA(USB/TA) with MHL cable'
794 * - Support charging through micro-usb port without data connection
796 extcon_set_cable_state(info->edev, "MHL_TA", attached);
798 extcon_set_cable_state(info->edev, "MHL", cable_attached);
802 /* Check MAX77693_CABLE_GROUP_ADC type */
803 cable_type = max77693_muic_get_cable_type(info,
804 MAX77693_CABLE_GROUP_ADC,
806 switch (cable_type) {
807 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
809 * Dock-Audio device with USB/TA cable
810 * - Dock device include two port(Dock-Audio and micro-usb
811 * port). When the target connect Dock-Audio device, extcon
812 * driver check whether USB/TA cable is connected. If USB/TA
813 * cable is connected, extcon driver notify state to notifiee
814 * for charging battery.
816 * Features of 'USB/TA cable with Dock-Audio device'
817 * - Support external output feature of audio.
818 * - Support charging through micro-usb port without data
821 extcon_set_cable_state(info->edev, "USB", attached);
824 extcon_set_cable_state(info->edev, "Dock-Audio", cable_attached);
826 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
828 * Dock-Smart device with USB/TA cable
829 * - Dock-Desk device include three type of cable which
830 * are HDMI, USB for mouse/keyboard and micro-usb port
831 * for USB/TA cable. Dock-Smart device need always exteranl
832 * power supply(USB/TA cable through micro-usb cable). Dock-
833 * Smart device support screen output of target to separate
834 * monitor and mouse/keyboard for desktop mode.
836 * Features of 'USB/TA cable with Dock-Smart device'
838 * - Support external output feature of audio
839 * - Support charging through micro-usb port without data
840 * connection if TA cable is connected to target.
841 * - Support charging and data connection through micro-usb port
842 * if USB cable is connected between target and host
844 * - Support OTG device (Mouse/Keyboard)
846 ret = max77693_muic_set_path(info, info->path_usb, attached);
850 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
851 extcon_set_cable_state(info->edev, "MHL", attached);
856 /* Check MAX77693_CABLE_GROUP_CHG type */
858 case MAX77693_CHARGER_TYPE_NONE:
860 * When MHL(with USB/TA cable) or Dock-Audio with USB/TA cable
861 * is attached, muic device happen below two interrupt.
862 * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting MHL/Dock-Audio.
863 * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting USB/TA cable
864 * connected to MHL or Dock-Audio.
865 * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC interrupt
866 * than MAX77693_MUIC_IRQ_INT2_CHGTYP interrupt.
868 * If user attach MHL (with USB/TA cable and immediately detach
869 * MHL with USB/TA cable before MAX77693_MUIC_IRQ_INT2_CHGTYP
870 * interrupt is happened, USB/TA cable remain connected state to
871 * target. But USB/TA cable isn't connected to target. The user
872 * be face with unusual action. So, driver should check this
873 * situation in spite of, that previous charger type is N/A.
876 case MAX77693_CHARGER_TYPE_USB:
877 /* Only USB cable, PATH:AP_USB */
878 ret = max77693_muic_set_path(info, info->path_usb, attached);
882 extcon_set_cable_state(info->edev, "USB", attached);
884 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
886 extcon_set_cable_state(info->edev, "TA", attached);
890 case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
891 extcon_set_cable_state(info->edev,
892 "Charge-downstream", attached);
894 case MAX77693_CHARGER_TYPE_APPLE_500MA:
895 extcon_set_cable_state(info->edev, "Slow-charger", attached);
897 case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
898 extcon_set_cable_state(info->edev, "Fast-charger", attached);
900 case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
904 "failed to detect %s accessory (chg_type:0x%x)\n",
905 attached ? "attached" : "detached", chg_type);
912 static void max77693_muic_irq_work(struct work_struct *work)
914 struct max77693_muic_info *info = container_of(work,
915 struct max77693_muic_info, irq_work);
922 mutex_lock(&info->mutex);
924 for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
925 if (info->irq == muic_irqs[i].virq)
926 irq_type = muic_irqs[i].irq;
928 ret = max77693_bulk_read(info->max77693->regmap_muic,
929 MAX77693_MUIC_REG_STATUS1, 2, info->status);
931 dev_err(info->dev, "failed to read MUIC register\n");
932 mutex_unlock(&info->mutex);
937 case MAX77693_MUIC_IRQ_INT1_ADC:
938 case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
939 case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
940 case MAX77693_MUIC_IRQ_INT1_ADC1K:
941 /* Handle all of accessory except for
942 type of charger accessory */
943 ret = max77693_muic_adc_handler(info);
945 case MAX77693_MUIC_IRQ_INT2_CHGTYP:
946 case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
947 case MAX77693_MUIC_IRQ_INT2_DCDTMR:
948 case MAX77693_MUIC_IRQ_INT2_DXOVP:
949 case MAX77693_MUIC_IRQ_INT2_VBVOLT:
950 case MAX77693_MUIC_IRQ_INT2_VIDRM:
951 /* Handle charger accessory */
952 ret = max77693_muic_chg_handler(info);
954 case MAX77693_MUIC_IRQ_INT3_EOC:
955 case MAX77693_MUIC_IRQ_INT3_CGMBC:
956 case MAX77693_MUIC_IRQ_INT3_OVP:
957 case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
958 case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
959 case MAX77693_MUIC_IRQ_INT3_BAT_DET:
962 dev_err(info->dev, "muic interrupt: irq %d occurred\n",
964 mutex_unlock(&info->mutex);
969 dev_err(info->dev, "failed to handle MUIC interrupt\n");
971 mutex_unlock(&info->mutex);
976 static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
978 struct max77693_muic_info *info = data;
981 schedule_work(&info->irq_work);
986 static struct regmap_config max77693_muic_regmap_config = {
991 static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
998 mutex_lock(&info->mutex);
1000 /* Read STATUSx register to detect accessory */
1001 ret = max77693_bulk_read(info->max77693->regmap_muic,
1002 MAX77693_MUIC_REG_STATUS1, 2, info->status);
1004 dev_err(info->dev, "failed to read MUIC register\n");
1005 mutex_unlock(&info->mutex);
1009 adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
1011 if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
1012 ret = max77693_muic_adc_handler(info);
1014 dev_err(info->dev, "Cannot detect accessory\n");
1015 mutex_unlock(&info->mutex);
1020 chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
1022 if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
1023 ret = max77693_muic_chg_handler(info);
1025 dev_err(info->dev, "Cannot detect charger accessory\n");
1026 mutex_unlock(&info->mutex);
1031 mutex_unlock(&info->mutex);
1036 static void max77693_muic_detect_cable_wq(struct work_struct *work)
1038 struct max77693_muic_info *info = container_of(to_delayed_work(work),
1039 struct max77693_muic_info, wq_detcable);
1041 max77693_muic_detect_accessory(info);
1044 static int max77693_muic_probe(struct platform_device *pdev)
1046 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
1047 struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
1048 struct max77693_muic_platform_data *muic_pdata = pdata->muic_data;
1049 struct max77693_muic_info *info;
1055 info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
1058 dev_err(&pdev->dev, "failed to allocate memory\n");
1061 info->dev = &pdev->dev;
1062 info->max77693 = max77693;
1063 if (info->max77693->regmap_muic) {
1064 dev_dbg(&pdev->dev, "allocate register map\n");
1066 info->max77693->regmap_muic = devm_regmap_init_i2c(
1067 info->max77693->muic,
1068 &max77693_muic_regmap_config);
1069 if (IS_ERR(info->max77693->regmap_muic)) {
1070 ret = PTR_ERR(info->max77693->regmap_muic);
1071 dev_err(max77693->dev,
1072 "failed to allocate register map: %d\n", ret);
1077 /* Register input device for button of dock device */
1078 info->dock = devm_input_allocate_device(&pdev->dev);
1080 dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
1083 info->dock->name = "max77693-muic/dock";
1084 info->dock->phys = "max77693-muic/extcon";
1085 info->dock->dev.parent = &pdev->dev;
1087 __set_bit(EV_REP, info->dock->evbit);
1089 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
1090 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
1091 input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
1092 input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
1093 input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
1095 ret = input_register_device(info->dock);
1097 dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
1102 platform_set_drvdata(pdev, info);
1103 mutex_init(&info->mutex);
1105 INIT_WORK(&info->irq_work, max77693_muic_irq_work);
1107 /* Support irq domain for MAX77693 MUIC device */
1108 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
1109 struct max77693_muic_irq *muic_irq = &muic_irqs[i];
1110 unsigned int virq = 0;
1112 virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
1117 muic_irq->virq = virq;
1119 ret = request_threaded_irq(virq, NULL,
1120 max77693_muic_irq_handler,
1122 muic_irq->name, info);
1125 "failed: irq request (IRQ: %d,"
1127 muic_irq->irq, ret);
1132 /* Initialize extcon device */
1133 info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
1136 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1140 info->edev->name = DEV_NAME;
1141 info->edev->supported_cable = max77693_extcon_cable;
1142 ret = extcon_dev_register(info->edev, NULL);
1144 dev_err(&pdev->dev, "failed to register extcon device\n");
1148 /* Initialize MUIC register by using platform data */
1149 for (i = 0 ; i < muic_pdata->num_init_data ; i++) {
1150 enum max77693_irq_source irq_src = MAX77693_IRQ_GROUP_NR;
1152 max77693_write_reg(info->max77693->regmap_muic,
1153 muic_pdata->init_data[i].addr,
1154 muic_pdata->init_data[i].data);
1156 switch (muic_pdata->init_data[i].addr) {
1157 case MAX77693_MUIC_REG_INTMASK1:
1158 irq_src = MUIC_INT1;
1160 case MAX77693_MUIC_REG_INTMASK2:
1161 irq_src = MUIC_INT2;
1163 case MAX77693_MUIC_REG_INTMASK3:
1164 irq_src = MUIC_INT3;
1168 if (irq_src < MAX77693_IRQ_GROUP_NR)
1169 info->max77693->irq_masks_cur[irq_src]
1170 = muic_pdata->init_data[i].data;
1174 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
1175 * h/w path of COMP2/COMN1 on CONTROL1 register.
1177 if (muic_pdata->path_uart)
1178 info->path_uart = muic_pdata->path_uart;
1180 info->path_uart = CONTROL1_SW_UART;
1182 if (muic_pdata->path_usb)
1183 info->path_usb = muic_pdata->path_usb;
1185 info->path_usb = CONTROL1_SW_USB;
1187 /* Set initial path for UART */
1188 max77693_muic_set_path(info, info->path_uart, true);
1190 /* Check revision number of MUIC device*/
1191 ret = max77693_read_reg(info->max77693->regmap_muic,
1192 MAX77693_MUIC_REG_ID, &id);
1194 dev_err(&pdev->dev, "failed to read revision number\n");
1197 dev_info(info->dev, "device ID : 0x%x\n", id);
1199 /* Set ADC debounce time */
1200 max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1203 * Detect accessory after completing the initialization of platform
1205 * - Use delayed workqueue to detect cable state and then
1206 * notify cable state to notifiee/platform through uevent.
1207 * After completing the booting of platform, the extcon provider
1208 * driver should notify cable state to upper layer.
1210 INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
1211 if (muic_pdata->detcable_delay_ms)
1212 delay_jiffies = msecs_to_jiffies(muic_pdata->detcable_delay_ms);
1214 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1215 schedule_delayed_work(&info->wq_detcable, delay_jiffies);
1220 extcon_dev_unregister(info->edev);
1223 free_irq(muic_irqs[i].virq, info);
1227 static int max77693_muic_remove(struct platform_device *pdev)
1229 struct max77693_muic_info *info = platform_get_drvdata(pdev);
1232 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
1233 free_irq(muic_irqs[i].virq, info);
1234 cancel_work_sync(&info->irq_work);
1235 input_unregister_device(info->dock);
1236 extcon_dev_unregister(info->edev);
1241 static struct platform_driver max77693_muic_driver = {
1244 .owner = THIS_MODULE,
1246 .probe = max77693_muic_probe,
1247 .remove = max77693_muic_remove,
1250 module_platform_driver(max77693_muic_driver);
1252 MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
1253 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1254 MODULE_LICENSE("GPL");
1255 MODULE_ALIAS("platform:extcon-max77693");