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 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");
231 dev_err(info->dev, "invalid ADC debounce time\n");
240 * max77693_muic_set_path - Set hardware line according to attached cable
241 * @info: the instance including private data of max77693 MUIC
242 * @value: the path according to attached cable
243 * @attached: the state of cable (true:attached, false:detached)
245 * The max77693 MUIC device share outside H/W line among a varity of cables
246 * so, this function set internal path of H/W line according to the type of
249 static int max77693_muic_set_path(struct max77693_muic_info *info,
250 u8 val, bool attached)
258 ctrl1 = CONTROL1_SW_OPEN;
260 ret = max77693_update_reg(info->max77693->regmap_muic,
261 MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
263 dev_err(info->dev, "failed to update MUIC register\n");
268 ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
270 ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
272 ret = max77693_update_reg(info->max77693->regmap_muic,
273 MAX77693_MUIC_REG_CTRL2, ctrl2,
274 CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
276 dev_err(info->dev, "failed to update MUIC register\n");
281 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
282 ctrl1, ctrl2, attached ? "attached" : "detached");
288 * max77693_muic_get_cable_type - Return cable type and check cable state
289 * @info: the instance including private data of max77693 MUIC
290 * @group: the path according to attached cable
291 * @attached: store cable state and return
293 * This function check the cable state either attached or detached,
294 * and then divide precise type of cable according to cable group.
295 * - MAX77693_CABLE_GROUP_ADC
296 * - MAX77693_CABLE_GROUP_ADC_GND
297 * - MAX77693_CABLE_GROUP_CHG
298 * - MAX77693_CABLE_GROUP_VBVOLT
300 static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
301 enum max77693_muic_cable_group group, bool *attached)
311 case MAX77693_CABLE_GROUP_ADC:
313 * Read ADC value to check cable type and decide cable state
314 * according to cable type
316 adc = info->status[0] & STATUS1_ADC_MASK;
317 adc >>= STATUS1_ADC_SHIFT;
320 * Check current cable state/cable type and store cable type
321 * (info->prev_cable_type) for handling cable when cable is
324 if (adc == MAX77693_MUIC_ADC_OPEN) {
327 cable_type = info->prev_cable_type;
328 info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
332 cable_type = info->prev_cable_type = adc;
335 case MAX77693_CABLE_GROUP_ADC_GND:
337 * Read ADC value to check cable type and decide cable state
338 * according to cable type
340 adc = info->status[0] & STATUS1_ADC_MASK;
341 adc >>= STATUS1_ADC_SHIFT;
344 * Check current cable state/cable type and store cable type
345 * (info->prev_cable_type/_gnd) for handling cable when cable
348 if (adc == MAX77693_MUIC_ADC_OPEN) {
351 cable_type = info->prev_cable_type_gnd;
352 info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
356 adclow = info->status[0] & STATUS1_ADCLOW_MASK;
357 adclow >>= STATUS1_ADCLOW_SHIFT;
358 adc1k = info->status[0] & STATUS1_ADC1K_MASK;
359 adc1k >>= STATUS1_ADC1K_SHIFT;
361 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
362 vbvolt >>= STATUS2_VBVOLT_SHIFT;
365 * [0x1][VBVolt][ADCLow][ADC1K]
366 * [0x1 0 0 0 ] : USB_OTG
367 * [0x1 1 0 0 ] : USB_OTG_VB
368 * [0x1 0 1 0 ] : Audio Video Cable with load
369 * [0x1 0 1 1 ] : MHL without charging connector
370 * [0x1 1 1 1 ] : MHL with charging connector
372 cable_type = ((0x1 << 8)
377 info->prev_cable_type = adc;
378 info->prev_cable_type_gnd = cable_type;
382 case MAX77693_CABLE_GROUP_CHG:
384 * Read charger type to check cable type and decide cable state
385 * according to type of charger cable.
387 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
388 chg_type >>= STATUS2_CHGTYP_SHIFT;
390 if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
393 cable_type = info->prev_chg_type;
394 info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
399 * Check current cable state/cable type and store cable
400 * type(info->prev_chg_type) for handling cable when
401 * charger cable is detached.
403 cable_type = info->prev_chg_type = chg_type;
407 case MAX77693_CABLE_GROUP_VBVOLT:
409 * Read ADC value to check cable type and decide cable state
410 * according to cable type
412 adc = info->status[0] & STATUS1_ADC_MASK;
413 adc >>= STATUS1_ADC_SHIFT;
414 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
415 chg_type >>= STATUS2_CHGTYP_SHIFT;
417 if (adc == MAX77693_MUIC_ADC_OPEN
418 && chg_type == MAX77693_CHARGER_TYPE_NONE)
424 * Read vbvolt field, if vbvolt is 1,
425 * this cable is used for charging.
427 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
428 vbvolt >>= STATUS2_VBVOLT_SHIFT;
433 dev_err(info->dev, "Unknown cable group (%d)\n", group);
434 cable_type = -EINVAL;
441 static int max77693_muic_dock_handler(struct max77693_muic_info *info,
442 int cable_type, bool attached)
447 char dock_name[CABLE_NAME_MAX];
450 "external connector is %s (adc:0x%02x)\n",
451 attached ? "attached" : "detached", cable_type);
453 switch (cable_type) {
454 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
456 * Check power cable whether attached or detached state.
457 * The Dock-Smart device need surely external power supply.
458 * If power cable(USB/TA) isn't connected to Dock device,
459 * user can't use Dock-Smart for desktop mode.
461 vbvolt = max77693_muic_get_cable_type(info,
462 MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);
463 if (attached && !vbvolt) {
465 "Cannot detect external power supply\n");
470 * Notify Dock-Smart/MHL state.
471 * - Dock-Smart device include three type of cable which
472 * are HDMI, USB for mouse/keyboard and micro-usb port
473 * for USB/TA cable. Dock-Smart device need always exteranl
474 * power supply(USB/TA cable through micro-usb cable). Dock-
475 * Smart device support screen output of target to separate
476 * monitor and mouse/keyboard for desktop mode.
478 * Features of 'USB/TA cable with Dock-Smart device'
480 * - Support external output feature of audio
481 * - Support charging through micro-usb port without data
482 * connection if TA cable is connected to target.
483 * - Support charging and data connection through micro-usb port
484 * if USB cable is connected between target and host
486 * - Support OTG device (Mouse/Keyboard)
488 ret = max77693_muic_set_path(info, info->path_usb, attached);
492 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
493 extcon_set_cable_state(info->edev, "MHL", attached);
495 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
496 strcpy(dock_name, "Dock-Car");
498 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
499 strcpy(dock_name, "Dock-Desk");
501 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
502 strcpy(dock_name, "Dock-Audio");
504 extcon_set_cable_state(info->edev, "USB", false);
508 /* Dock-Car/Desk/Audio, PATH:AUDIO */
509 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
512 extcon_set_cable_state(info->edev, dock_name, attached);
518 static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
519 int button_type, bool attached)
521 struct input_dev *dock = info->dock;
525 switch (button_type) {
526 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
527 ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
529 code = KEY_PREVIOUSSONG;
531 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
532 ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
536 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
538 code = KEY_VOLUMEDOWN;
540 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
544 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
545 ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
546 /* DOCK_KEY_PLAY_PAUSE */
547 code = KEY_PLAYPAUSE;
551 "failed to detect %s key (adc:0x%x)\n",
552 attached ? "pressed" : "released", button_type);
557 input_event(dock, EV_KEY, code, attached);
564 static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
570 cable_type_gnd = max77693_muic_get_cable_type(info,
571 MAX77693_CABLE_GROUP_ADC_GND, &attached);
573 switch (cable_type_gnd) {
574 case MAX77693_MUIC_GND_USB_OTG:
575 case MAX77693_MUIC_GND_USB_OTG_VB:
576 /* USB_OTG, PATH: AP_USB */
577 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
580 extcon_set_cable_state(info->edev, "USB-Host", attached);
582 case MAX77693_MUIC_GND_AV_CABLE_LOAD:
583 /* Audio Video Cable with load, PATH:AUDIO */
584 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
587 extcon_set_cable_state(info->edev,
588 "Audio-video-load", attached);
590 case MAX77693_MUIC_GND_MHL:
591 case MAX77693_MUIC_GND_MHL_VB:
592 /* MHL or MHL with USB/TA cable */
593 extcon_set_cable_state(info->edev, "MHL", attached);
596 dev_err(info->dev, "failed to detect %s accessory\n",
597 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;
635 ret = max77693_muic_set_path(info, path, attached);
639 extcon_set_cable_state(info->edev, cable_name, attached);
644 static int max77693_muic_adc_handler(struct max77693_muic_info *info)
651 /* Check accessory state which is either detached or attached */
652 cable_type = max77693_muic_get_cable_type(info,
653 MAX77693_CABLE_GROUP_ADC, &attached);
656 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
657 attached ? "attached" : "detached", cable_type,
658 info->prev_cable_type);
660 switch (cable_type) {
661 case MAX77693_MUIC_ADC_GROUND:
662 /* USB_OTG/MHL/Audio */
663 max77693_muic_adc_ground_handler(info);
665 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
666 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
667 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
669 ret = max77693_muic_jig_handler(info, cable_type, attached);
673 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
674 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
675 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
676 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
680 * The MAX77693 MUIC device can detect total 34 cable type
681 * except of charger cable and MUIC device didn't define
682 * specfic role of cable in the range of from 0x01 to 0x12
683 * of ADC value. So, can use/define cable with no role according
684 * to schema of hardware board.
686 ret = max77693_muic_dock_handler(info, cable_type, attached);
690 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON: /* DOCK_KEY_PREV */
691 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON: /* DOCK_KEY_NEXT */
692 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: /* DOCK_VOL_DOWN */
693 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: /* DOCK_VOL_UP */
694 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON: /* DOCK_KEY_PLAY_PAUSE */
696 * Button of DOCK device
697 * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
699 * The MAX77693 MUIC device can detect total 34 cable type
700 * except of charger cable and MUIC device didn't define
701 * specfic role of cable in the range of from 0x01 to 0x12
702 * of ADC value. So, can use/define cable with no role according
703 * to schema of hardware board.
706 button_type = info->prev_button_type = cable_type;
708 button_type = info->prev_button_type;
710 ret = max77693_muic_dock_button_handler(info, button_type,
715 case MAX77693_MUIC_ADC_SEND_END_BUTTON:
716 case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
717 case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
718 case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
719 case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
720 case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
721 case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
722 case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
723 case MAX77693_MUIC_ADC_RESERVED_ACC_1:
724 case MAX77693_MUIC_ADC_RESERVED_ACC_2:
725 case MAX77693_MUIC_ADC_RESERVED_ACC_4:
726 case MAX77693_MUIC_ADC_RESERVED_ACC_5:
727 case MAX77693_MUIC_ADC_CEA936_AUDIO:
728 case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
729 case MAX77693_MUIC_ADC_TTY_CONVERTER:
730 case MAX77693_MUIC_ADC_UART_CABLE:
731 case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
732 case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
734 * This accessory isn't used in general case if it is specially
735 * needed to detect additional accessory, should implement
736 * proper operation when this accessory is attached/detached.
739 "accessory is %s but it isn't used (adc:0x%x)\n",
740 attached ? "attached" : "detached", cable_type);
744 "failed to detect %s accessory (adc:0x%x)\n",
745 attached ? "attached" : "detached", cable_type);
754 static int max77693_muic_chg_handler(struct max77693_muic_info *info)
763 chg_type = max77693_muic_get_cable_type(info,
764 MAX77693_CABLE_GROUP_CHG, &attached);
767 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
768 attached ? "attached" : "detached",
769 chg_type, info->prev_chg_type);
772 case MAX77693_CHARGER_TYPE_USB:
773 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
774 case MAX77693_CHARGER_TYPE_NONE:
775 /* Check MAX77693_CABLE_GROUP_ADC_GND type */
776 cable_type_gnd = max77693_muic_get_cable_type(info,
777 MAX77693_CABLE_GROUP_ADC_GND,
779 switch (cable_type_gnd) {
780 case MAX77693_MUIC_GND_MHL:
781 case MAX77693_MUIC_GND_MHL_VB:
783 * MHL cable with MHL_TA(USB/TA) cable
784 * - MHL cable include two port(HDMI line and separate micro-
785 * usb port. When the target connect MHL cable, extcon driver
786 * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA
787 * cable is connected, extcon driver notify state to notifiee
788 * for charging battery.
790 * Features of 'MHL_TA(USB/TA) with MHL cable'
792 * - Support charging through micro-usb port without data connection
794 extcon_set_cable_state(info->edev, "MHL_TA", attached);
796 extcon_set_cable_state(info->edev, "MHL", cable_attached);
800 /* Check MAX77693_CABLE_GROUP_ADC type */
801 cable_type = max77693_muic_get_cable_type(info,
802 MAX77693_CABLE_GROUP_ADC,
804 switch (cable_type) {
805 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
807 * Dock-Audio device with USB/TA cable
808 * - Dock device include two port(Dock-Audio and micro-usb
809 * port). When the target connect Dock-Audio device, extcon
810 * driver check whether USB/TA cable is connected. If USB/TA
811 * cable is connected, extcon driver notify state to notifiee
812 * for charging battery.
814 * Features of 'USB/TA cable with Dock-Audio device'
815 * - Support external output feature of audio.
816 * - Support charging through micro-usb port without data
819 extcon_set_cable_state(info->edev, "USB", attached);
822 extcon_set_cable_state(info->edev, "Dock-Audio", cable_attached);
824 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
826 * Dock-Smart device with USB/TA cable
827 * - Dock-Desk device include three type of cable which
828 * are HDMI, USB for mouse/keyboard and micro-usb port
829 * for USB/TA cable. Dock-Smart device need always exteranl
830 * power supply(USB/TA cable through micro-usb cable). Dock-
831 * Smart device support screen output of target to separate
832 * monitor and mouse/keyboard for desktop mode.
834 * Features of 'USB/TA cable with Dock-Smart device'
836 * - Support external output feature of audio
837 * - Support charging through micro-usb port without data
838 * connection if TA cable is connected to target.
839 * - Support charging and data connection through micro-usb port
840 * if USB cable is connected between target and host
842 * - Support OTG device (Mouse/Keyboard)
844 ret = max77693_muic_set_path(info, info->path_usb, attached);
848 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
849 extcon_set_cable_state(info->edev, "MHL", attached);
854 /* Check MAX77693_CABLE_GROUP_CHG type */
856 case MAX77693_CHARGER_TYPE_NONE:
858 * When MHL(with USB/TA cable) or Dock-Audio with USB/TA cable
859 * is attached, muic device happen below two interrupt.
860 * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting MHL/Dock-Audio.
861 * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting USB/TA cable
862 * connected to MHL or Dock-Audio.
863 * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC interrupt
864 * than MAX77693_MUIC_IRQ_INT2_CHGTYP interrupt.
866 * If user attach MHL (with USB/TA cable and immediately detach
867 * MHL with USB/TA cable before MAX77693_MUIC_IRQ_INT2_CHGTYP
868 * interrupt is happened, USB/TA cable remain connected state to
869 * target. But USB/TA cable isn't connected to target. The user
870 * be face with unusual action. So, driver should check this
871 * situation in spite of, that previous charger type is N/A.
874 case MAX77693_CHARGER_TYPE_USB:
875 /* Only USB cable, PATH:AP_USB */
876 ret = max77693_muic_set_path(info, info->path_usb, attached);
880 extcon_set_cable_state(info->edev, "USB", attached);
882 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
884 extcon_set_cable_state(info->edev, "TA", attached);
888 case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
889 extcon_set_cable_state(info->edev,
890 "Charge-downstream", attached);
892 case MAX77693_CHARGER_TYPE_APPLE_500MA:
893 extcon_set_cable_state(info->edev, "Slow-charger", attached);
895 case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
896 extcon_set_cable_state(info->edev, "Fast-charger", attached);
898 case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
902 "failed to detect %s accessory (chg_type:0x%x)\n",
903 attached ? "attached" : "detached", chg_type);
910 static void max77693_muic_irq_work(struct work_struct *work)
912 struct max77693_muic_info *info = container_of(work,
913 struct max77693_muic_info, irq_work);
920 mutex_lock(&info->mutex);
922 for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
923 if (info->irq == muic_irqs[i].virq)
924 irq_type = muic_irqs[i].irq;
926 ret = max77693_bulk_read(info->max77693->regmap_muic,
927 MAX77693_MUIC_REG_STATUS1, 2, info->status);
929 dev_err(info->dev, "failed to read MUIC register\n");
930 mutex_unlock(&info->mutex);
935 case MAX77693_MUIC_IRQ_INT1_ADC:
936 case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
937 case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
938 case MAX77693_MUIC_IRQ_INT1_ADC1K:
939 /* Handle all of accessory except for
940 type of charger accessory */
941 ret = max77693_muic_adc_handler(info);
943 case MAX77693_MUIC_IRQ_INT2_CHGTYP:
944 case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
945 case MAX77693_MUIC_IRQ_INT2_DCDTMR:
946 case MAX77693_MUIC_IRQ_INT2_DXOVP:
947 case MAX77693_MUIC_IRQ_INT2_VBVOLT:
948 case MAX77693_MUIC_IRQ_INT2_VIDRM:
949 /* Handle charger accessory */
950 ret = max77693_muic_chg_handler(info);
952 case MAX77693_MUIC_IRQ_INT3_EOC:
953 case MAX77693_MUIC_IRQ_INT3_CGMBC:
954 case MAX77693_MUIC_IRQ_INT3_OVP:
955 case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
956 case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
957 case MAX77693_MUIC_IRQ_INT3_BAT_DET:
960 dev_err(info->dev, "muic interrupt: irq %d occurred\n",
966 dev_err(info->dev, "failed to handle MUIC interrupt\n");
968 mutex_unlock(&info->mutex);
973 static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
975 struct max77693_muic_info *info = data;
978 schedule_work(&info->irq_work);
983 static struct regmap_config max77693_muic_regmap_config = {
988 static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
995 mutex_lock(&info->mutex);
997 /* Read STATUSx register to detect accessory */
998 ret = max77693_bulk_read(info->max77693->regmap_muic,
999 MAX77693_MUIC_REG_STATUS1, 2, info->status);
1001 dev_err(info->dev, "failed to read MUIC register\n");
1002 mutex_unlock(&info->mutex);
1006 adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
1008 if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
1009 ret = max77693_muic_adc_handler(info);
1011 dev_err(info->dev, "Cannot detect accessory\n");
1014 chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
1016 if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
1017 ret = max77693_muic_chg_handler(info);
1019 dev_err(info->dev, "Cannot detect charger accessory\n");
1022 mutex_unlock(&info->mutex);
1027 static void max77693_muic_detect_cable_wq(struct work_struct *work)
1029 struct max77693_muic_info *info = container_of(to_delayed_work(work),
1030 struct max77693_muic_info, wq_detcable);
1032 max77693_muic_detect_accessory(info);
1035 static int max77693_muic_probe(struct platform_device *pdev)
1037 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
1038 struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
1039 struct max77693_muic_platform_data *muic_pdata = pdata->muic_data;
1040 struct max77693_muic_info *info;
1046 info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
1049 dev_err(&pdev->dev, "failed to allocate memory\n");
1052 info->dev = &pdev->dev;
1053 info->max77693 = max77693;
1054 if (info->max77693->regmap_muic) {
1055 dev_dbg(&pdev->dev, "allocate register map\n");
1057 info->max77693->regmap_muic = devm_regmap_init_i2c(
1058 info->max77693->muic,
1059 &max77693_muic_regmap_config);
1060 if (IS_ERR(info->max77693->regmap_muic)) {
1061 ret = PTR_ERR(info->max77693->regmap_muic);
1062 dev_err(max77693->dev,
1063 "failed to allocate register map: %d\n", ret);
1068 /* Register input device for button of dock device */
1069 info->dock = devm_input_allocate_device(&pdev->dev);
1071 dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
1074 info->dock->name = "max77693-muic/dock";
1075 info->dock->phys = "max77693-muic/extcon";
1076 info->dock->dev.parent = &pdev->dev;
1078 __set_bit(EV_REP, info->dock->evbit);
1080 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
1081 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
1082 input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
1083 input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
1084 input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
1086 ret = input_register_device(info->dock);
1088 dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
1093 platform_set_drvdata(pdev, info);
1094 mutex_init(&info->mutex);
1096 INIT_WORK(&info->irq_work, max77693_muic_irq_work);
1098 /* Support irq domain for MAX77693 MUIC device */
1099 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
1100 struct max77693_muic_irq *muic_irq = &muic_irqs[i];
1101 unsigned int virq = 0;
1103 virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
1108 muic_irq->virq = virq;
1110 ret = request_threaded_irq(virq, NULL,
1111 max77693_muic_irq_handler,
1113 muic_irq->name, info);
1116 "failed: irq request (IRQ: %d,"
1118 muic_irq->irq, ret);
1123 /* Initialize extcon device */
1124 info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
1127 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1131 info->edev->name = DEV_NAME;
1132 info->edev->supported_cable = max77693_extcon_cable;
1133 ret = extcon_dev_register(info->edev, NULL);
1135 dev_err(&pdev->dev, "failed to register extcon device\n");
1139 /* Initialize MUIC register by using platform data */
1140 for (i = 0 ; i < muic_pdata->num_init_data ; i++) {
1141 enum max77693_irq_source irq_src = MAX77693_IRQ_GROUP_NR;
1143 max77693_write_reg(info->max77693->regmap_muic,
1144 muic_pdata->init_data[i].addr,
1145 muic_pdata->init_data[i].data);
1147 switch (muic_pdata->init_data[i].addr) {
1148 case MAX77693_MUIC_REG_INTMASK1:
1149 irq_src = MUIC_INT1;
1151 case MAX77693_MUIC_REG_INTMASK2:
1152 irq_src = MUIC_INT2;
1154 case MAX77693_MUIC_REG_INTMASK3:
1155 irq_src = MUIC_INT3;
1159 if (irq_src < MAX77693_IRQ_GROUP_NR)
1160 info->max77693->irq_masks_cur[irq_src]
1161 = muic_pdata->init_data[i].data;
1165 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
1166 * h/w path of COMP2/COMN1 on CONTROL1 register.
1168 if (muic_pdata->path_uart)
1169 info->path_uart = muic_pdata->path_uart;
1171 info->path_uart = CONTROL1_SW_UART;
1173 if (muic_pdata->path_usb)
1174 info->path_usb = muic_pdata->path_usb;
1176 info->path_usb = CONTROL1_SW_USB;
1178 /* Set initial path for UART */
1179 max77693_muic_set_path(info, info->path_uart, true);
1181 /* Check revision number of MUIC device*/
1182 ret = max77693_read_reg(info->max77693->regmap_muic,
1183 MAX77693_MUIC_REG_ID, &id);
1185 dev_err(&pdev->dev, "failed to read revision number\n");
1188 dev_info(info->dev, "device ID : 0x%x\n", id);
1190 /* Set ADC debounce time */
1191 max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1194 * Detect accessory after completing the initialization of platform
1196 * - Use delayed workqueue to detect cable state and then
1197 * notify cable state to notifiee/platform through uevent.
1198 * After completing the booting of platform, the extcon provider
1199 * driver should notify cable state to upper layer.
1201 INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
1202 if (muic_pdata->detcable_delay_ms)
1203 delay_jiffies = msecs_to_jiffies(muic_pdata->detcable_delay_ms);
1205 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1206 schedule_delayed_work(&info->wq_detcable, delay_jiffies);
1211 extcon_dev_unregister(info->edev);
1214 free_irq(muic_irqs[i].virq, info);
1218 static int max77693_muic_remove(struct platform_device *pdev)
1220 struct max77693_muic_info *info = platform_get_drvdata(pdev);
1223 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
1224 free_irq(muic_irqs[i].virq, info);
1225 cancel_work_sync(&info->irq_work);
1226 input_unregister_device(info->dock);
1227 extcon_dev_unregister(info->edev);
1232 static struct platform_driver max77693_muic_driver = {
1235 .owner = THIS_MODULE,
1237 .probe = max77693_muic_probe,
1238 .remove = max77693_muic_remove,
1241 module_platform_driver(max77693_muic_driver);
1243 MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
1244 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1245 MODULE_LICENSE("GPL");
1246 MODULE_ALIAS("platform:extcon-max77693");