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 */
36 * Default value of MAX77693 register to bring up MUIC device.
37 * If user don't set some initial value for MUIC device through platform data,
38 * extcon-max77693 driver use 'default_init_data' to bring up base operation
39 * of MAX77693 MUIC device.
41 static struct max77693_reg_data default_init_data[] = {
43 /* STATUS2 - [3]ChgDetRun */
44 .addr = MAX77693_MUIC_REG_STATUS2,
45 .data = STATUS2_CHGDETRUN_MASK,
47 /* INTMASK1 - Unmask [3]ADC1KM,[0]ADCM */
48 .addr = MAX77693_MUIC_REG_INTMASK1,
49 .data = INTMASK1_ADC1K_MASK
52 /* INTMASK2 - Unmask [0]ChgTypM */
53 .addr = MAX77693_MUIC_REG_INTMASK2,
54 .data = INTMASK2_CHGTYP_MASK,
56 /* INTMASK3 - Mask all of interrupts */
57 .addr = MAX77693_MUIC_REG_INTMASK3,
61 .addr = MAX77693_MUIC_REG_CDETCTRL2,
62 .data = CDETCTRL2_VIDRMEN_MASK
63 | CDETCTRL2_DXOVPEN_MASK,
67 enum max77693_muic_adc_debounce_time {
68 ADC_DEBOUNCE_TIME_5MS = 0,
69 ADC_DEBOUNCE_TIME_10MS,
70 ADC_DEBOUNCE_TIME_25MS,
71 ADC_DEBOUNCE_TIME_38_62MS,
74 struct max77693_muic_info {
76 struct max77693_dev *max77693;
77 struct extcon_dev *edev;
79 int prev_cable_type_gnd;
85 struct work_struct irq_work;
89 * Use delayed workqueue to detect cable state and then
90 * notify cable state to notifiee/platform through uevent.
91 * After completing the booting of platform, the extcon provider
92 * driver should notify cable state to upper layer.
94 struct delayed_work wq_detcable;
96 /* Button of dock device */
97 struct input_dev *dock;
100 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
101 * h/w path of COMP2/COMN1 on CONTROL1 register.
107 enum max77693_muic_cable_group {
108 MAX77693_CABLE_GROUP_ADC = 0,
109 MAX77693_CABLE_GROUP_ADC_GND,
110 MAX77693_CABLE_GROUP_CHG,
111 MAX77693_CABLE_GROUP_VBVOLT,
114 enum max77693_muic_charger_type {
115 MAX77693_CHARGER_TYPE_NONE = 0,
116 MAX77693_CHARGER_TYPE_USB,
117 MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,
118 MAX77693_CHARGER_TYPE_DEDICATED_CHG,
119 MAX77693_CHARGER_TYPE_APPLE_500MA,
120 MAX77693_CHARGER_TYPE_APPLE_1A_2A,
121 MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,
125 * struct max77693_muic_irq
126 * @irq: the index of irq list of MUIC device.
127 * @name: the name of irq.
128 * @virq: the virtual irq to use irq domain
130 struct max77693_muic_irq {
136 static struct max77693_muic_irq muic_irqs[] = {
137 { MAX77693_MUIC_IRQ_INT1_ADC, "muic-ADC" },
138 { MAX77693_MUIC_IRQ_INT1_ADC_LOW, "muic-ADCLOW" },
139 { MAX77693_MUIC_IRQ_INT1_ADC_ERR, "muic-ADCError" },
140 { MAX77693_MUIC_IRQ_INT1_ADC1K, "muic-ADC1K" },
141 { MAX77693_MUIC_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
142 { MAX77693_MUIC_IRQ_INT2_CHGDETREUN, "muic-CHGDETREUN" },
143 { MAX77693_MUIC_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
144 { MAX77693_MUIC_IRQ_INT2_DXOVP, "muic-DXOVP" },
145 { MAX77693_MUIC_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
146 { MAX77693_MUIC_IRQ_INT2_VIDRM, "muic-VIDRM" },
147 { MAX77693_MUIC_IRQ_INT3_EOC, "muic-EOC" },
148 { MAX77693_MUIC_IRQ_INT3_CGMBC, "muic-CGMBC" },
149 { MAX77693_MUIC_IRQ_INT3_OVP, "muic-OVP" },
150 { MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR, "muic-MBCCHG_ERR" },
151 { MAX77693_MUIC_IRQ_INT3_CHG_ENABLED, "muic-CHG_ENABLED" },
152 { MAX77693_MUIC_IRQ_INT3_BAT_DET, "muic-BAT_DET" },
155 /* Define supported accessory type */
156 enum max77693_muic_acc_type {
157 MAX77693_MUIC_ADC_GROUND = 0x0,
158 MAX77693_MUIC_ADC_SEND_END_BUTTON,
159 MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,
160 MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,
161 MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,
162 MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,
163 MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,
164 MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,
165 MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,
166 MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,
167 MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,
168 MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,
169 MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,
170 MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,
171 MAX77693_MUIC_ADC_RESERVED_ACC_1,
172 MAX77693_MUIC_ADC_RESERVED_ACC_2,
173 MAX77693_MUIC_ADC_RESERVED_ACC_3,
174 MAX77693_MUIC_ADC_RESERVED_ACC_4,
175 MAX77693_MUIC_ADC_RESERVED_ACC_5,
176 MAX77693_MUIC_ADC_CEA936_AUDIO,
177 MAX77693_MUIC_ADC_PHONE_POWERED_DEV,
178 MAX77693_MUIC_ADC_TTY_CONVERTER,
179 MAX77693_MUIC_ADC_UART_CABLE,
180 MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,
181 MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,
182 MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,
183 MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,
184 MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,
185 MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,
186 MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,
187 MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,
188 MAX77693_MUIC_ADC_OPEN,
190 /* The below accessories have same ADC value so ADCLow and
191 ADC1K bit is used to separate specific accessory */
192 /* ADC|VBVolot|ADCLow|ADC1K| */
193 MAX77693_MUIC_GND_USB_HOST = 0x100, /* 0x0| 0| 0| 0| */
194 MAX77693_MUIC_GND_USB_HOST_VB = 0x104, /* 0x0| 1| 0| 0| */
195 MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* 0x0| 0| 1| 0| */
196 MAX77693_MUIC_GND_MHL = 0x103, /* 0x0| 0| 1| 1| */
197 MAX77693_MUIC_GND_MHL_VB = 0x107, /* 0x0| 1| 1| 1| */
201 * MAX77693 MUIC device support below list of accessories(external connector)
204 EXTCON_CABLE_USB = 0,
205 EXTCON_CABLE_USB_HOST,
207 EXTCON_CABLE_FAST_CHARGER,
208 EXTCON_CABLE_SLOW_CHARGER,
209 EXTCON_CABLE_CHARGE_DOWNSTREAM,
213 EXTCON_CABLE_DOCK_SMART,
214 EXTCON_CABLE_DOCK_DESK,
215 EXTCON_CABLE_DOCK_AUDIO,
220 static const char *max77693_extcon_cable[] = {
221 [EXTCON_CABLE_USB] = "USB",
222 [EXTCON_CABLE_USB_HOST] = "USB-Host",
223 [EXTCON_CABLE_TA] = "TA",
224 [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
225 [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
226 [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
227 [EXTCON_CABLE_MHL] = "MHL",
228 [EXTCON_CABLE_MHL_TA] = "MHL-TA",
229 [EXTCON_CABLE_JIG] = "JIG",
230 [EXTCON_CABLE_DOCK_SMART] = "Dock-Smart",
231 [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk",
232 [EXTCON_CABLE_DOCK_AUDIO] = "Dock-Audio",
238 * max77693_muic_set_debounce_time - Set the debounce time of ADC
239 * @info: the instance including private data of max77693 MUIC
240 * @time: the debounce time of ADC
242 static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
243 enum max77693_muic_adc_debounce_time time)
248 case ADC_DEBOUNCE_TIME_5MS:
249 case ADC_DEBOUNCE_TIME_10MS:
250 case ADC_DEBOUNCE_TIME_25MS:
251 case ADC_DEBOUNCE_TIME_38_62MS:
253 * Don't touch BTLDset, JIGset when you want to change adc
254 * debounce time. If it writes other than 0 to BTLDset, JIGset
255 * muic device will be reset and loose current state.
257 ret = regmap_write(info->max77693->regmap_muic,
258 MAX77693_MUIC_REG_CTRL3,
259 time << CONTROL3_ADCDBSET_SHIFT);
261 dev_err(info->dev, "failed to set ADC debounce time\n");
266 dev_err(info->dev, "invalid ADC debounce time\n");
274 * max77693_muic_set_path - Set hardware line according to attached cable
275 * @info: the instance including private data of max77693 MUIC
276 * @value: the path according to attached cable
277 * @attached: the state of cable (true:attached, false:detached)
279 * The max77693 MUIC device share outside H/W line among a varity of cables
280 * so, this function set internal path of H/W line according to the type of
283 static int max77693_muic_set_path(struct max77693_muic_info *info,
284 u8 val, bool attached)
287 unsigned int ctrl1, ctrl2 = 0;
292 ctrl1 = CONTROL1_SW_OPEN;
294 ret = regmap_update_bits(info->max77693->regmap_muic,
295 MAX77693_MUIC_REG_CTRL1, COMP_SW_MASK, ctrl1);
297 dev_err(info->dev, "failed to update MUIC register\n");
302 ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
304 ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
306 ret = regmap_update_bits(info->max77693->regmap_muic,
307 MAX77693_MUIC_REG_CTRL2,
308 CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK, ctrl2);
310 dev_err(info->dev, "failed to update MUIC register\n");
315 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
316 ctrl1, ctrl2, attached ? "attached" : "detached");
322 * max77693_muic_get_cable_type - Return cable type and check cable state
323 * @info: the instance including private data of max77693 MUIC
324 * @group: the path according to attached cable
325 * @attached: store cable state and return
327 * This function check the cable state either attached or detached,
328 * and then divide precise type of cable according to cable group.
329 * - MAX77693_CABLE_GROUP_ADC
330 * - MAX77693_CABLE_GROUP_ADC_GND
331 * - MAX77693_CABLE_GROUP_CHG
332 * - MAX77693_CABLE_GROUP_VBVOLT
334 static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
335 enum max77693_muic_cable_group group, bool *attached)
345 case MAX77693_CABLE_GROUP_ADC:
347 * Read ADC value to check cable type and decide cable state
348 * according to cable type
350 adc = info->status[0] & STATUS1_ADC_MASK;
351 adc >>= STATUS1_ADC_SHIFT;
354 * Check current cable state/cable type and store cable type
355 * (info->prev_cable_type) for handling cable when cable is
358 if (adc == MAX77693_MUIC_ADC_OPEN) {
361 cable_type = info->prev_cable_type;
362 info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
366 cable_type = info->prev_cable_type = adc;
369 case MAX77693_CABLE_GROUP_ADC_GND:
371 * Read ADC value to check cable type and decide cable state
372 * according to cable type
374 adc = info->status[0] & STATUS1_ADC_MASK;
375 adc >>= STATUS1_ADC_SHIFT;
378 * Check current cable state/cable type and store cable type
379 * (info->prev_cable_type/_gnd) for handling cable when cable
382 if (adc == MAX77693_MUIC_ADC_OPEN) {
385 cable_type = info->prev_cable_type_gnd;
386 info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
390 adclow = info->status[0] & STATUS1_ADCLOW_MASK;
391 adclow >>= STATUS1_ADCLOW_SHIFT;
392 adc1k = info->status[0] & STATUS1_ADC1K_MASK;
393 adc1k >>= STATUS1_ADC1K_SHIFT;
395 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
396 vbvolt >>= STATUS2_VBVOLT_SHIFT;
399 * [0x1|VBVolt|ADCLow|ADC1K]
400 * [0x1| 0| 0| 0] USB_HOST
401 * [0x1| 1| 0| 0] USB_HSOT_VB
402 * [0x1| 0| 1| 0] Audio Video cable with load
403 * [0x1| 0| 1| 1] MHL without charging cable
404 * [0x1| 1| 1| 1] MHL with charging cable
406 cable_type = ((0x1 << 8)
411 info->prev_cable_type = adc;
412 info->prev_cable_type_gnd = cable_type;
416 case MAX77693_CABLE_GROUP_CHG:
418 * Read charger type to check cable type and decide cable state
419 * according to type of charger cable.
421 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
422 chg_type >>= STATUS2_CHGTYP_SHIFT;
424 if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
427 cable_type = info->prev_chg_type;
428 info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
433 * Check current cable state/cable type and store cable
434 * type(info->prev_chg_type) for handling cable when
435 * charger cable is detached.
437 cable_type = info->prev_chg_type = chg_type;
441 case MAX77693_CABLE_GROUP_VBVOLT:
443 * Read ADC value to check cable type and decide cable state
444 * according to cable type
446 adc = info->status[0] & STATUS1_ADC_MASK;
447 adc >>= STATUS1_ADC_SHIFT;
448 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
449 chg_type >>= STATUS2_CHGTYP_SHIFT;
451 if (adc == MAX77693_MUIC_ADC_OPEN
452 && chg_type == MAX77693_CHARGER_TYPE_NONE)
458 * Read vbvolt field, if vbvolt is 1,
459 * this cable is used for charging.
461 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
462 vbvolt >>= STATUS2_VBVOLT_SHIFT;
467 dev_err(info->dev, "Unknown cable group (%d)\n", group);
468 cable_type = -EINVAL;
475 static int max77693_muic_dock_handler(struct max77693_muic_info *info,
476 int cable_type, bool attached)
481 char dock_name[CABLE_NAME_MAX];
484 "external connector is %s (adc:0x%02x)\n",
485 attached ? "attached" : "detached", cable_type);
487 switch (cable_type) {
488 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
490 * Check power cable whether attached or detached state.
491 * The Dock-Smart device need surely external power supply.
492 * If power cable(USB/TA) isn't connected to Dock device,
493 * user can't use Dock-Smart for desktop mode.
495 vbvolt = max77693_muic_get_cable_type(info,
496 MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);
497 if (attached && !vbvolt) {
499 "Cannot detect external power supply\n");
504 * Notify Dock-Smart/MHL state.
505 * - Dock-Smart device include three type of cable which
506 * are HDMI, USB for mouse/keyboard and micro-usb port
507 * for USB/TA cable. Dock-Smart device need always exteranl
508 * power supply(USB/TA cable through micro-usb cable). Dock-
509 * Smart device support screen output of target to separate
510 * monitor and mouse/keyboard for desktop mode.
512 * Features of 'USB/TA cable with Dock-Smart device'
514 * - Support external output feature of audio
515 * - Support charging through micro-usb port without data
516 * connection if TA cable is connected to target.
517 * - Support charging and data connection through micro-usb port
518 * if USB cable is connected between target and host
520 * - Support OTG(On-The-Go) device (Ex: Mouse/Keyboard)
522 ret = max77693_muic_set_path(info, info->path_usb, attached);
526 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
527 extcon_set_cable_state(info->edev, "MHL", attached);
529 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
530 strcpy(dock_name, "Dock-Desk");
532 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
533 strcpy(dock_name, "Dock-Audio");
535 extcon_set_cable_state(info->edev, "USB", false);
538 dev_err(info->dev, "failed to detect %s dock device\n",
539 attached ? "attached" : "detached");
543 /* Dock-Car/Desk/Audio, PATH:AUDIO */
544 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
547 extcon_set_cable_state(info->edev, dock_name, attached);
553 static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
554 int button_type, bool attached)
556 struct input_dev *dock = info->dock;
559 switch (button_type) {
560 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
561 ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
563 code = KEY_PREVIOUSSONG;
565 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
566 ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
570 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
572 code = KEY_VOLUMEDOWN;
574 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
578 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
579 ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
580 /* DOCK_KEY_PLAY_PAUSE */
581 code = KEY_PLAYPAUSE;
585 "failed to detect %s key (adc:0x%x)\n",
586 attached ? "pressed" : "released", button_type);
590 input_event(dock, EV_KEY, code, attached);
596 static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
602 cable_type_gnd = max77693_muic_get_cable_type(info,
603 MAX77693_CABLE_GROUP_ADC_GND, &attached);
605 switch (cable_type_gnd) {
606 case MAX77693_MUIC_GND_USB_HOST:
607 case MAX77693_MUIC_GND_USB_HOST_VB:
608 /* USB_HOST, PATH: AP_USB */
609 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
612 extcon_set_cable_state(info->edev, "USB-Host", attached);
614 case MAX77693_MUIC_GND_AV_CABLE_LOAD:
615 /* Audio Video Cable with load, PATH:AUDIO */
616 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
619 extcon_set_cable_state(info->edev,
620 "Audio-video-load", attached);
622 case MAX77693_MUIC_GND_MHL:
623 case MAX77693_MUIC_GND_MHL_VB:
624 /* MHL or MHL with USB/TA cable */
625 extcon_set_cable_state(info->edev, "MHL", attached);
628 dev_err(info->dev, "failed to detect %s cable of gnd type\n",
629 attached ? "attached" : "detached");
636 static int max77693_muic_jig_handler(struct max77693_muic_info *info,
637 int cable_type, bool attached)
640 u8 path = CONTROL1_SW_OPEN;
643 "external connector is %s (adc:0x%02x)\n",
644 attached ? "attached" : "detached", cable_type);
646 switch (cable_type) {
647 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
648 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
650 path = CONTROL1_SW_USB;
652 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
653 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* ADC_JIG_UART_ON */
655 path = CONTROL1_SW_UART;
658 dev_err(info->dev, "failed to detect %s jig cable\n",
659 attached ? "attached" : "detached");
663 ret = max77693_muic_set_path(info, path, attached);
667 extcon_set_cable_state(info->edev, "JIG", attached);
672 static int max77693_muic_adc_handler(struct max77693_muic_info *info)
679 /* Check accessory state which is either detached or attached */
680 cable_type = max77693_muic_get_cable_type(info,
681 MAX77693_CABLE_GROUP_ADC, &attached);
684 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
685 attached ? "attached" : "detached", cable_type,
686 info->prev_cable_type);
688 switch (cable_type) {
689 case MAX77693_MUIC_ADC_GROUND:
690 /* USB_HOST/MHL/Audio */
691 max77693_muic_adc_ground_handler(info);
693 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
694 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
695 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
696 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:
698 ret = max77693_muic_jig_handler(info, cable_type, attached);
702 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
703 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
704 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
708 * The MAX77693 MUIC device can detect total 34 cable type
709 * except of charger cable and MUIC device didn't define
710 * specfic role of cable in the range of from 0x01 to 0x12
711 * of ADC value. So, can use/define cable with no role according
712 * to schema of hardware board.
714 ret = max77693_muic_dock_handler(info, cable_type, attached);
718 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON: /* DOCK_KEY_PREV */
719 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON: /* DOCK_KEY_NEXT */
720 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: /* DOCK_VOL_DOWN */
721 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: /* DOCK_VOL_UP */
722 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON: /* DOCK_KEY_PLAY_PAUSE */
724 * Button of DOCK device
725 * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
727 * The MAX77693 MUIC device can detect total 34 cable type
728 * except of charger cable and MUIC device didn't define
729 * specfic role of cable in the range of from 0x01 to 0x12
730 * of ADC value. So, can use/define cable with no role according
731 * to schema of hardware board.
734 button_type = info->prev_button_type = cable_type;
736 button_type = info->prev_button_type;
738 ret = max77693_muic_dock_button_handler(info, button_type,
743 case MAX77693_MUIC_ADC_SEND_END_BUTTON:
744 case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
745 case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
746 case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
747 case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
748 case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
749 case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
750 case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
751 case MAX77693_MUIC_ADC_RESERVED_ACC_1:
752 case MAX77693_MUIC_ADC_RESERVED_ACC_2:
753 case MAX77693_MUIC_ADC_RESERVED_ACC_4:
754 case MAX77693_MUIC_ADC_RESERVED_ACC_5:
755 case MAX77693_MUIC_ADC_CEA936_AUDIO:
756 case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
757 case MAX77693_MUIC_ADC_TTY_CONVERTER:
758 case MAX77693_MUIC_ADC_UART_CABLE:
759 case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
760 case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
762 * This accessory isn't used in general case if it is specially
763 * needed to detect additional accessory, should implement
764 * proper operation when this accessory is attached/detached.
767 "accessory is %s but it isn't used (adc:0x%x)\n",
768 attached ? "attached" : "detached", cable_type);
772 "failed to detect %s accessory (adc:0x%x)\n",
773 attached ? "attached" : "detached", cable_type);
780 static int max77693_muic_chg_handler(struct max77693_muic_info *info)
789 chg_type = max77693_muic_get_cable_type(info,
790 MAX77693_CABLE_GROUP_CHG, &attached);
793 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
794 attached ? "attached" : "detached",
795 chg_type, info->prev_chg_type);
798 case MAX77693_CHARGER_TYPE_USB:
799 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
800 case MAX77693_CHARGER_TYPE_NONE:
801 /* Check MAX77693_CABLE_GROUP_ADC_GND type */
802 cable_type_gnd = max77693_muic_get_cable_type(info,
803 MAX77693_CABLE_GROUP_ADC_GND,
805 switch (cable_type_gnd) {
806 case MAX77693_MUIC_GND_MHL:
807 case MAX77693_MUIC_GND_MHL_VB:
809 * MHL cable with MHL-TA(USB/TA) cable
810 * - MHL cable include two port(HDMI line and separate
811 * micro-usb port. When the target connect MHL cable,
812 * extcon driver check whether MHL-TA(USB/TA) cable is
813 * connected. If MHL-TA cable is connected, extcon
814 * driver notify state to notifiee for charging battery.
816 * Features of 'MHL-TA(USB/TA) with MHL cable'
818 * - Support charging through micro-usb port without
821 extcon_set_cable_state(info->edev, "MHL-TA", attached);
823 extcon_set_cable_state(info->edev,
824 "MHL", cable_attached);
828 /* Check MAX77693_CABLE_GROUP_ADC type */
829 cable_type = max77693_muic_get_cable_type(info,
830 MAX77693_CABLE_GROUP_ADC,
832 switch (cable_type) {
833 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
835 * Dock-Audio device with USB/TA cable
836 * - Dock device include two port(Dock-Audio and micro-
837 * usb port). When the target connect Dock-Audio device,
838 * extcon driver check whether USB/TA cable is connected
839 * or not. If USB/TA cable is connected, extcon driver
840 * notify state to notifiee for charging battery.
842 * Features of 'USB/TA cable with Dock-Audio device'
843 * - Support external output feature of audio.
844 * - Support charging through micro-usb port without
847 extcon_set_cable_state(info->edev, "USB", attached);
850 extcon_set_cable_state(info->edev, "Dock-Audio",
853 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
855 * Dock-Smart device with USB/TA cable
856 * - Dock-Desk device include three type of cable which
857 * are HDMI, USB for mouse/keyboard and micro-usb port
858 * for USB/TA cable. Dock-Smart device need always
859 * exteranl power supply(USB/TA cable through micro-usb
860 * cable). Dock-Smart device support screen output of
861 * target to separate monitor and mouse/keyboard for
864 * Features of 'USB/TA cable with Dock-Smart device'
866 * - Support external output feature of audio
867 * - Support charging through micro-usb port without
868 * data connection if TA cable is connected to target.
869 * - Support charging and data connection through micro-
870 * usb port if USB cable is connected between target
872 * - Support OTG(On-The-Go) device (Ex: Mouse/Keyboard)
874 ret = max77693_muic_set_path(info, info->path_usb,
879 extcon_set_cable_state(info->edev, "Dock-Smart",
881 extcon_set_cable_state(info->edev, "MHL", attached);
886 /* Check MAX77693_CABLE_GROUP_CHG type */
888 case MAX77693_CHARGER_TYPE_NONE:
890 * When MHL(with USB/TA cable) or Dock-Audio with USB/TA
891 * cable is attached, muic device happen below two irq.
892 * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting
894 * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting
895 * USB/TA cable connected to MHL or Dock-Audio.
896 * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC
897 * irq than MAX77693_MUIC_IRQ_INT2_CHGTYP irq.
899 * If user attach MHL (with USB/TA cable and immediately
900 * detach MHL with USB/TA cable before MAX77693_MUIC_IRQ
901 * _INT2_CHGTYP irq is happened, USB/TA cable remain
902 * connected state to target. But USB/TA cable isn't
903 * connected to target. The user be face with unusual
904 * action. So, driver should check this situation in
905 * spite of, that previous charger type is N/A.
908 case MAX77693_CHARGER_TYPE_USB:
909 /* Only USB cable, PATH:AP_USB */
910 ret = max77693_muic_set_path(info, info->path_usb,
915 extcon_set_cable_state(info->edev, "USB", attached);
917 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
919 extcon_set_cable_state(info->edev, "TA", attached);
923 case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
924 extcon_set_cable_state(info->edev,
925 "Charge-downstream", attached);
927 case MAX77693_CHARGER_TYPE_APPLE_500MA:
928 extcon_set_cable_state(info->edev, "Slow-charger", attached);
930 case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
931 extcon_set_cable_state(info->edev, "Fast-charger", attached);
933 case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
937 "failed to detect %s accessory (chg_type:0x%x)\n",
938 attached ? "attached" : "detached", chg_type);
945 static void max77693_muic_irq_work(struct work_struct *work)
947 struct max77693_muic_info *info = container_of(work,
948 struct max77693_muic_info, irq_work);
955 mutex_lock(&info->mutex);
957 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
958 if (info->irq == muic_irqs[i].virq)
959 irq_type = muic_irqs[i].irq;
961 ret = regmap_bulk_read(info->max77693->regmap_muic,
962 MAX77693_MUIC_REG_STATUS1, info->status, 2);
964 dev_err(info->dev, "failed to read MUIC register\n");
965 mutex_unlock(&info->mutex);
970 case MAX77693_MUIC_IRQ_INT1_ADC:
971 case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
972 case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
973 case MAX77693_MUIC_IRQ_INT1_ADC1K:
974 /* Handle all of accessory except for
975 type of charger accessory */
976 ret = max77693_muic_adc_handler(info);
978 case MAX77693_MUIC_IRQ_INT2_CHGTYP:
979 case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
980 case MAX77693_MUIC_IRQ_INT2_DCDTMR:
981 case MAX77693_MUIC_IRQ_INT2_DXOVP:
982 case MAX77693_MUIC_IRQ_INT2_VBVOLT:
983 case MAX77693_MUIC_IRQ_INT2_VIDRM:
984 /* Handle charger accessory */
985 ret = max77693_muic_chg_handler(info);
987 case MAX77693_MUIC_IRQ_INT3_EOC:
988 case MAX77693_MUIC_IRQ_INT3_CGMBC:
989 case MAX77693_MUIC_IRQ_INT3_OVP:
990 case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
991 case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
992 case MAX77693_MUIC_IRQ_INT3_BAT_DET:
995 dev_err(info->dev, "muic interrupt: irq %d occurred\n",
997 mutex_unlock(&info->mutex);
1002 dev_err(info->dev, "failed to handle MUIC interrupt\n");
1004 mutex_unlock(&info->mutex);
1007 static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
1009 struct max77693_muic_info *info = data;
1012 schedule_work(&info->irq_work);
1017 static const struct regmap_config max77693_muic_regmap_config = {
1022 static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
1029 mutex_lock(&info->mutex);
1031 /* Read STATUSx register to detect accessory */
1032 ret = regmap_bulk_read(info->max77693->regmap_muic,
1033 MAX77693_MUIC_REG_STATUS1, info->status, 2);
1035 dev_err(info->dev, "failed to read MUIC register\n");
1036 mutex_unlock(&info->mutex);
1040 adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
1042 if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
1043 ret = max77693_muic_adc_handler(info);
1045 dev_err(info->dev, "Cannot detect accessory\n");
1046 mutex_unlock(&info->mutex);
1051 chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
1053 if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
1054 ret = max77693_muic_chg_handler(info);
1056 dev_err(info->dev, "Cannot detect charger accessory\n");
1057 mutex_unlock(&info->mutex);
1062 mutex_unlock(&info->mutex);
1067 static void max77693_muic_detect_cable_wq(struct work_struct *work)
1069 struct max77693_muic_info *info = container_of(to_delayed_work(work),
1070 struct max77693_muic_info, wq_detcable);
1072 max77693_muic_detect_accessory(info);
1075 static int max77693_muic_probe(struct platform_device *pdev)
1077 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
1078 struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
1079 struct max77693_muic_info *info;
1080 struct max77693_reg_data *init_data;
1087 info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
1092 info->dev = &pdev->dev;
1093 info->max77693 = max77693;
1094 if (info->max77693->regmap_muic) {
1095 dev_dbg(&pdev->dev, "allocate register map\n");
1097 info->max77693->regmap_muic = devm_regmap_init_i2c(
1098 info->max77693->muic,
1099 &max77693_muic_regmap_config);
1100 if (IS_ERR(info->max77693->regmap_muic)) {
1101 ret = PTR_ERR(info->max77693->regmap_muic);
1102 dev_err(max77693->dev,
1103 "failed to allocate register map: %d\n", ret);
1108 /* Register input device for button of dock device */
1109 info->dock = devm_input_allocate_device(&pdev->dev);
1111 dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
1114 info->dock->name = "max77693-muic/dock";
1115 info->dock->phys = "max77693-muic/extcon";
1116 info->dock->dev.parent = &pdev->dev;
1118 __set_bit(EV_REP, info->dock->evbit);
1120 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
1121 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
1122 input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
1123 input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
1124 input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
1126 ret = input_register_device(info->dock);
1128 dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
1133 platform_set_drvdata(pdev, info);
1134 mutex_init(&info->mutex);
1136 INIT_WORK(&info->irq_work, max77693_muic_irq_work);
1138 /* Support irq domain for MAX77693 MUIC device */
1139 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
1140 struct max77693_muic_irq *muic_irq = &muic_irqs[i];
1141 unsigned int virq = 0;
1143 virq = regmap_irq_get_virq(max77693->irq_data_muic,
1147 muic_irq->virq = virq;
1149 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
1150 max77693_muic_irq_handler,
1152 muic_irq->name, info);
1155 "failed: irq request (IRQ: %d, error :%d)\n",
1156 muic_irq->irq, ret);
1161 /* Initialize extcon device */
1162 info->edev = devm_extcon_dev_allocate(&pdev->dev,
1163 max77693_extcon_cable);
1164 if (IS_ERR(info->edev)) {
1165 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1168 info->edev->name = DEV_NAME;
1170 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
1172 dev_err(&pdev->dev, "failed to register extcon device\n");
1176 /* Initialize MUIC register by using platform data or default data */
1177 if (pdata && pdata->muic_data) {
1178 init_data = pdata->muic_data->init_data;
1179 num_init_data = pdata->muic_data->num_init_data;
1181 init_data = default_init_data;
1182 num_init_data = ARRAY_SIZE(default_init_data);
1185 for (i = 0; i < num_init_data; i++) {
1186 enum max77693_irq_source irq_src
1187 = MAX77693_IRQ_GROUP_NR;
1189 regmap_write(info->max77693->regmap_muic,
1193 switch (init_data[i].addr) {
1194 case MAX77693_MUIC_REG_INTMASK1:
1195 irq_src = MUIC_INT1;
1197 case MAX77693_MUIC_REG_INTMASK2:
1198 irq_src = MUIC_INT2;
1200 case MAX77693_MUIC_REG_INTMASK3:
1201 irq_src = MUIC_INT3;
1205 if (irq_src < MAX77693_IRQ_GROUP_NR)
1206 info->max77693->irq_masks_cur[irq_src]
1207 = init_data[i].data;
1210 if (pdata && pdata->muic_data) {
1211 struct max77693_muic_platform_data *muic_pdata
1215 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
1216 * h/w path of COMP2/COMN1 on CONTROL1 register.
1218 if (muic_pdata->path_uart)
1219 info->path_uart = muic_pdata->path_uart;
1221 info->path_uart = CONTROL1_SW_UART;
1223 if (muic_pdata->path_usb)
1224 info->path_usb = muic_pdata->path_usb;
1226 info->path_usb = CONTROL1_SW_USB;
1229 * Default delay time for detecting cable state
1230 * after certain time.
1232 if (muic_pdata->detcable_delay_ms)
1234 msecs_to_jiffies(muic_pdata->detcable_delay_ms);
1236 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1238 info->path_usb = CONTROL1_SW_USB;
1239 info->path_uart = CONTROL1_SW_UART;
1240 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1243 /* Set initial path for UART */
1244 max77693_muic_set_path(info, info->path_uart, true);
1246 /* Check revision number of MUIC device*/
1247 ret = regmap_read(info->max77693->regmap_muic,
1248 MAX77693_MUIC_REG_ID, &id);
1250 dev_err(&pdev->dev, "failed to read revision number\n");
1253 dev_info(info->dev, "device ID : 0x%x\n", id);
1255 /* Set ADC debounce time */
1256 max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1259 * Detect accessory after completing the initialization of platform
1261 * - Use delayed workqueue to detect cable state and then
1262 * notify cable state to notifiee/platform through uevent.
1263 * After completing the booting of platform, the extcon provider
1264 * driver should notify cable state to upper layer.
1266 INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
1267 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
1273 static int max77693_muic_remove(struct platform_device *pdev)
1275 struct max77693_muic_info *info = platform_get_drvdata(pdev);
1277 cancel_work_sync(&info->irq_work);
1278 input_unregister_device(info->dock);
1283 static struct platform_driver max77693_muic_driver = {
1287 .probe = max77693_muic_probe,
1288 .remove = max77693_muic_remove,
1291 module_platform_driver(max77693_muic_driver);
1293 MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
1294 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1295 MODULE_LICENSE("GPL");
1296 MODULE_ALIAS("platform:extcon-max77693");