1 // SPDX-License-Identifier: GPL-2.0+
3 // extcon-max77843.c - Maxim MAX77843 extcon driver to support
4 // MUIC(Micro USB Interface Controller)
6 // Copyright (C) 2015 Samsung Electronics
7 // Author: Jaewon Kim <jaewon02.kim@samsung.com>
9 #include <linux/extcon-provider.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/mfd/max77693-common.h>
14 #include <linux/mfd/max77843-private.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/workqueue.h>
19 #define DELAY_MS_DEFAULT 15000 /* unit: millisecond */
21 enum max77843_muic_status {
22 MAX77843_MUIC_STATUS1 = 0,
23 MAX77843_MUIC_STATUS2,
24 MAX77843_MUIC_STATUS3,
26 MAX77843_MUIC_STATUS_NUM,
29 struct max77843_muic_info {
31 struct max77693_dev *max77843;
32 struct extcon_dev *edev;
35 struct work_struct irq_work;
36 struct delayed_work wq_detcable;
38 u8 status[MAX77843_MUIC_STATUS_NUM];
47 enum max77843_muic_cable_group {
48 MAX77843_CABLE_GROUP_ADC = 0,
49 MAX77843_CABLE_GROUP_ADC_GND,
50 MAX77843_CABLE_GROUP_CHG,
53 enum max77843_muic_adc_debounce_time {
54 MAX77843_DEBOUNCE_TIME_5MS = 0,
55 MAX77843_DEBOUNCE_TIME_10MS,
56 MAX77843_DEBOUNCE_TIME_25MS,
57 MAX77843_DEBOUNCE_TIME_38_62MS,
60 /* Define accessory cable type */
61 enum max77843_muic_accessory_type {
62 MAX77843_MUIC_ADC_GROUND = 0,
63 MAX77843_MUIC_ADC_SEND_END_BUTTON,
64 MAX77843_MUIC_ADC_REMOTE_S1_BUTTON,
65 MAX77843_MUIC_ADC_REMOTE_S2_BUTTON,
66 MAX77843_MUIC_ADC_REMOTE_S3_BUTTON,
67 MAX77843_MUIC_ADC_REMOTE_S4_BUTTON,
68 MAX77843_MUIC_ADC_REMOTE_S5_BUTTON,
69 MAX77843_MUIC_ADC_REMOTE_S6_BUTTON,
70 MAX77843_MUIC_ADC_REMOTE_S7_BUTTON,
71 MAX77843_MUIC_ADC_REMOTE_S8_BUTTON,
72 MAX77843_MUIC_ADC_REMOTE_S9_BUTTON,
73 MAX77843_MUIC_ADC_REMOTE_S10_BUTTON,
74 MAX77843_MUIC_ADC_REMOTE_S11_BUTTON,
75 MAX77843_MUIC_ADC_REMOTE_S12_BUTTON,
76 MAX77843_MUIC_ADC_RESERVED_ACC_1,
77 MAX77843_MUIC_ADC_RESERVED_ACC_2,
78 MAX77843_MUIC_ADC_RESERVED_ACC_3, /* SmartDock */
79 MAX77843_MUIC_ADC_RESERVED_ACC_4,
80 MAX77843_MUIC_ADC_RESERVED_ACC_5,
81 MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2,
82 MAX77843_MUIC_ADC_PHONE_POWERED_DEV,
83 MAX77843_MUIC_ADC_TTY_CONVERTER,
84 MAX77843_MUIC_ADC_UART_CABLE,
85 MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG,
86 MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF,
87 MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON,
88 MAX77843_MUIC_ADC_AV_CABLE_NOLOAD,
89 MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG,
90 MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF,
91 MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON,
92 MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1,
93 MAX77843_MUIC_ADC_OPEN,
96 * The below accessories should check
97 * not only ADC value but also ADC1K and VBVolt value.
99 /* Offset|ADC1K|VBVolt| */
100 MAX77843_MUIC_GND_USB_HOST = 0x100, /* 0x1| 0| 0| */
101 MAX77843_MUIC_GND_USB_HOST_VB = 0x101, /* 0x1| 0| 1| */
102 MAX77843_MUIC_GND_MHL = 0x102, /* 0x1| 1| 0| */
103 MAX77843_MUIC_GND_MHL_VB = 0x103, /* 0x1| 1| 1| */
106 /* Define charger cable type */
107 enum max77843_muic_charger_type {
108 MAX77843_MUIC_CHG_NONE = 0,
109 MAX77843_MUIC_CHG_USB,
110 MAX77843_MUIC_CHG_DOWNSTREAM,
111 MAX77843_MUIC_CHG_DEDICATED,
112 MAX77843_MUIC_CHG_SPECIAL_500MA,
113 MAX77843_MUIC_CHG_SPECIAL_1A,
114 MAX77843_MUIC_CHG_SPECIAL_BIAS,
115 MAX77843_MUIC_CHG_RESERVED,
116 MAX77843_MUIC_CHG_GND,
117 MAX77843_MUIC_CHG_DOCK,
120 static const unsigned int max77843_extcon_cable[] = {
134 struct max77843_muic_irq {
140 static struct max77843_muic_irq max77843_muic_irqs[] = {
141 { MAX77843_MUIC_IRQ_INT1_ADC, "MUIC-ADC" },
142 { MAX77843_MUIC_IRQ_INT1_ADCERROR, "MUIC-ADC_ERROR" },
143 { MAX77843_MUIC_IRQ_INT1_ADC1K, "MUIC-ADC1K" },
144 { MAX77843_MUIC_IRQ_INT2_CHGTYP, "MUIC-CHGTYP" },
145 { MAX77843_MUIC_IRQ_INT2_CHGDETRUN, "MUIC-CHGDETRUN" },
146 { MAX77843_MUIC_IRQ_INT2_DCDTMR, "MUIC-DCDTMR" },
147 { MAX77843_MUIC_IRQ_INT2_DXOVP, "MUIC-DXOVP" },
148 { MAX77843_MUIC_IRQ_INT2_VBVOLT, "MUIC-VBVOLT" },
149 { MAX77843_MUIC_IRQ_INT3_VBADC, "MUIC-VBADC" },
150 { MAX77843_MUIC_IRQ_INT3_VDNMON, "MUIC-VDNMON" },
151 { MAX77843_MUIC_IRQ_INT3_DNRES, "MUIC-DNRES" },
152 { MAX77843_MUIC_IRQ_INT3_MPNACK, "MUIC-MPNACK"},
153 { MAX77843_MUIC_IRQ_INT3_MRXBUFOW, "MUIC-MRXBUFOW"},
154 { MAX77843_MUIC_IRQ_INT3_MRXTRF, "MUIC-MRXTRF"},
155 { MAX77843_MUIC_IRQ_INT3_MRXPERR, "MUIC-MRXPERR"},
156 { MAX77843_MUIC_IRQ_INT3_MRXRDY, "MUIC-MRXRDY"},
159 static const struct regmap_config max77843_muic_regmap_config = {
162 .max_register = MAX77843_MUIC_REG_END,
165 static const struct regmap_irq max77843_muic_irq[] = {
167 { .reg_offset = 0, .mask = MAX77843_MUIC_ADC, },
168 { .reg_offset = 0, .mask = MAX77843_MUIC_ADCERROR, },
169 { .reg_offset = 0, .mask = MAX77843_MUIC_ADC1K, },
172 { .reg_offset = 1, .mask = MAX77843_MUIC_CHGTYP, },
173 { .reg_offset = 1, .mask = MAX77843_MUIC_CHGDETRUN, },
174 { .reg_offset = 1, .mask = MAX77843_MUIC_DCDTMR, },
175 { .reg_offset = 1, .mask = MAX77843_MUIC_DXOVP, },
176 { .reg_offset = 1, .mask = MAX77843_MUIC_VBVOLT, },
179 { .reg_offset = 2, .mask = MAX77843_MUIC_VBADC, },
180 { .reg_offset = 2, .mask = MAX77843_MUIC_VDNMON, },
181 { .reg_offset = 2, .mask = MAX77843_MUIC_DNRES, },
182 { .reg_offset = 2, .mask = MAX77843_MUIC_MPNACK, },
183 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXBUFOW, },
184 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXTRF, },
185 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXPERR, },
186 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXRDY, },
189 static const struct regmap_irq_chip max77843_muic_irq_chip = {
190 .name = "max77843-muic",
191 .status_base = MAX77843_MUIC_REG_INT1,
192 .mask_base = MAX77843_MUIC_REG_INTMASK1,
195 .irqs = max77843_muic_irq,
196 .num_irqs = ARRAY_SIZE(max77843_muic_irq),
199 static int max77843_muic_set_path(struct max77843_muic_info *info,
200 u8 val, bool attached, bool nobccomp)
202 struct max77693_dev *max77843 = info->max77843;
204 unsigned int ctrl1, ctrl2;
209 ctrl1 = MAX77843_MUIC_CONTROL1_SW_OPEN;
211 /* Disable BC1.2 protocol and force manual switch control */
212 ctrl1 |= MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK;
215 ret = regmap_update_bits(max77843->regmap_muic,
216 MAX77843_MUIC_REG_CONTROL1,
217 MAX77843_MUIC_CONTROL1_COM_SW |
218 MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK,
221 dev_err(info->dev, "Cannot switch MUIC port\n");
226 ctrl2 = MAX77843_MUIC_CONTROL2_CPEN_MASK;
228 ctrl2 = MAX77843_MUIC_CONTROL2_LOWPWR_MASK;
230 ret = regmap_update_bits(max77843->regmap_muic,
231 MAX77843_MUIC_REG_CONTROL2,
232 MAX77843_MUIC_CONTROL2_LOWPWR_MASK |
233 MAX77843_MUIC_CONTROL2_CPEN_MASK, ctrl2);
235 dev_err(info->dev, "Cannot update lowpower mode\n");
240 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
241 ctrl1, ctrl2, attached ? "attached" : "detached");
246 static void max77843_charger_set_otg_vbus(struct max77843_muic_info *info,
249 struct max77693_dev *max77843 = info->max77843;
253 cnfg00 = MAX77843_CHG_OTG_MASK | MAX77843_CHG_BOOST_MASK;
255 cnfg00 = MAX77843_CHG_ENABLE | MAX77843_CHG_BUCK_MASK;
257 regmap_update_bits(max77843->regmap_chg, MAX77843_CHG_REG_CHG_CNFG_00,
258 MAX77843_CHG_MODE_MASK, cnfg00);
261 static int max77843_muic_get_cable_type(struct max77843_muic_info *info,
262 enum max77843_muic_cable_group group, bool *attached)
264 int adc, chg_type, cable_type, gnd_type;
266 adc = info->status[MAX77843_MUIC_STATUS1] &
267 MAX77843_MUIC_STATUS1_ADC_MASK;
268 adc >>= MAX77843_MUIC_STATUS1_ADC_SHIFT;
271 case MAX77843_CABLE_GROUP_ADC:
272 if (adc == MAX77843_MUIC_ADC_OPEN) {
274 cable_type = info->prev_cable_type;
275 info->prev_cable_type = MAX77843_MUIC_ADC_OPEN;
278 cable_type = info->prev_cable_type = adc;
281 case MAX77843_CABLE_GROUP_CHG:
282 chg_type = info->status[MAX77843_MUIC_STATUS2] &
283 MAX77843_MUIC_STATUS2_CHGTYP_MASK;
285 /* Check GROUND accessory with charger cable */
286 if (adc == MAX77843_MUIC_ADC_GROUND) {
287 if (chg_type == MAX77843_MUIC_CHG_NONE) {
289 * The following state when charger cable is
290 * disconnected but the GROUND accessory still
294 cable_type = info->prev_chg_type;
295 info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
299 * The following state when charger cable is
300 * connected on the GROUND accessory.
303 cable_type = MAX77843_MUIC_CHG_GND;
304 info->prev_chg_type = MAX77843_MUIC_CHG_GND;
309 if (adc == MAX77843_MUIC_ADC_RESERVED_ACC_3) { /* SmartDock */
310 if (chg_type == MAX77843_MUIC_CHG_NONE) {
312 cable_type = info->prev_chg_type;
313 info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
316 cable_type = MAX77843_MUIC_CHG_DOCK;
317 info->prev_chg_type = MAX77843_MUIC_CHG_DOCK;
322 if (chg_type == MAX77843_MUIC_CHG_NONE) {
324 cable_type = info->prev_chg_type;
325 info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
328 cable_type = info->prev_chg_type = chg_type;
331 case MAX77843_CABLE_GROUP_ADC_GND:
332 if (adc == MAX77843_MUIC_ADC_OPEN) {
334 cable_type = info->prev_gnd_type;
335 info->prev_gnd_type = MAX77843_MUIC_ADC_OPEN;
340 * Offset|ADC1K|VBVolt|
341 * 0x1| 0| 0| USB-HOST
342 * 0x1| 0| 1| USB-HOST with VB
344 * 0x1| 1| 1| MHL with VB
346 /* Get ADC1K register bit */
347 gnd_type = (info->status[MAX77843_MUIC_STATUS1] &
348 MAX77843_MUIC_STATUS1_ADC1K_MASK);
350 /* Get VBVolt register bit */
351 gnd_type |= (info->status[MAX77843_MUIC_STATUS2] &
352 MAX77843_MUIC_STATUS2_VBVOLT_MASK);
353 gnd_type >>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT;
355 /* Offset of GND cable */
356 gnd_type |= MAX77843_MUIC_GND_USB_HOST;
357 cable_type = info->prev_gnd_type = gnd_type;
361 dev_err(info->dev, "Unknown cable group (%d)\n", group);
362 cable_type = -EINVAL;
369 static int max77843_muic_adc_gnd_handler(struct max77843_muic_info *info)
371 int ret, gnd_cable_type;
374 gnd_cable_type = max77843_muic_get_cable_type(info,
375 MAX77843_CABLE_GROUP_ADC_GND, &attached);
376 dev_dbg(info->dev, "external connector is %s (gnd:0x%02x)\n",
377 attached ? "attached" : "detached", gnd_cable_type);
379 switch (gnd_cable_type) {
380 case MAX77843_MUIC_GND_USB_HOST:
381 case MAX77843_MUIC_GND_USB_HOST_VB:
382 ret = max77843_muic_set_path(info,
383 MAX77843_MUIC_CONTROL1_SW_USB,
388 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached);
389 max77843_charger_set_otg_vbus(info, attached);
391 case MAX77843_MUIC_GND_MHL_VB:
392 case MAX77843_MUIC_GND_MHL:
393 ret = max77843_muic_set_path(info,
394 MAX77843_MUIC_CONTROL1_SW_OPEN,
399 extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
402 dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n",
403 attached ? "attached" : "detached", gnd_cable_type);
410 static int max77843_muic_jig_handler(struct max77843_muic_info *info,
411 int cable_type, bool attached)
414 u8 path = MAX77843_MUIC_CONTROL1_SW_OPEN;
416 dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n",
417 attached ? "attached" : "detached", cable_type);
419 switch (cable_type) {
420 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
421 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
422 path = MAX77843_MUIC_CONTROL1_SW_USB;
424 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
425 path = MAX77843_MUIC_CONTROL1_SW_UART;
431 ret = max77843_muic_set_path(info, path, attached, false);
435 extcon_set_state_sync(info->edev, EXTCON_JIG, attached);
440 static int max77843_muic_dock_handler(struct max77843_muic_info *info,
445 dev_dbg(info->dev, "external connector is %s (adc: 0x10)\n",
446 attached ? "attached" : "detached");
448 ret = max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_USB,
453 extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
454 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached);
455 extcon_set_state_sync(info->edev, EXTCON_DOCK, attached);
460 static int max77843_muic_adc_handler(struct max77843_muic_info *info)
465 cable_type = max77843_muic_get_cable_type(info,
466 MAX77843_CABLE_GROUP_ADC, &attached);
469 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
470 attached ? "attached" : "detached", cable_type,
471 info->prev_cable_type);
473 switch (cable_type) {
474 case MAX77843_MUIC_ADC_RESERVED_ACC_3: /* SmartDock */
475 ret = max77843_muic_dock_handler(info, attached);
479 case MAX77843_MUIC_ADC_GROUND:
480 ret = max77843_muic_adc_gnd_handler(info);
484 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
485 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
486 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
487 ret = max77843_muic_jig_handler(info, cable_type, attached);
491 case MAX77843_MUIC_ADC_SEND_END_BUTTON:
492 case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON:
493 case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON:
494 case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON:
495 case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON:
496 case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON:
497 case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON:
498 case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON:
499 case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON:
500 case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON:
501 case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON:
502 case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON:
503 case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON:
504 case MAX77843_MUIC_ADC_RESERVED_ACC_1:
505 case MAX77843_MUIC_ADC_RESERVED_ACC_2:
506 case MAX77843_MUIC_ADC_RESERVED_ACC_4:
507 case MAX77843_MUIC_ADC_RESERVED_ACC_5:
508 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2:
509 case MAX77843_MUIC_ADC_PHONE_POWERED_DEV:
510 case MAX77843_MUIC_ADC_TTY_CONVERTER:
511 case MAX77843_MUIC_ADC_UART_CABLE:
512 case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG:
513 case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD:
514 case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG:
515 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON:
516 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1:
517 case MAX77843_MUIC_ADC_OPEN:
519 "accessory is %s but it isn't used (adc:0x%x)\n",
520 attached ? "attached" : "detached", cable_type);
524 "failed to detect %s accessory (adc:0x%x)\n",
525 attached ? "attached" : "detached", cable_type);
532 static int max77843_muic_chg_handler(struct max77843_muic_info *info)
534 int ret, chg_type, gnd_type;
537 chg_type = max77843_muic_get_cable_type(info,
538 MAX77843_CABLE_GROUP_CHG, &attached);
541 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
542 attached ? "attached" : "detached",
543 chg_type, info->prev_chg_type);
546 case MAX77843_MUIC_CHG_USB:
547 ret = max77843_muic_set_path(info,
548 MAX77843_MUIC_CONTROL1_SW_USB,
553 extcon_set_state_sync(info->edev, EXTCON_USB, attached);
554 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
557 case MAX77843_MUIC_CHG_DOWNSTREAM:
558 ret = max77843_muic_set_path(info,
559 MAX77843_MUIC_CONTROL1_SW_OPEN,
564 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP,
567 case MAX77843_MUIC_CHG_DEDICATED:
568 ret = max77843_muic_set_path(info,
569 MAX77843_MUIC_CONTROL1_SW_OPEN,
574 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
577 case MAX77843_MUIC_CHG_SPECIAL_500MA:
578 ret = max77843_muic_set_path(info,
579 MAX77843_MUIC_CONTROL1_SW_OPEN,
584 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW,
587 case MAX77843_MUIC_CHG_SPECIAL_1A:
588 ret = max77843_muic_set_path(info,
589 MAX77843_MUIC_CONTROL1_SW_OPEN,
594 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST,
597 case MAX77843_MUIC_CHG_GND:
598 gnd_type = max77843_muic_get_cable_type(info,
599 MAX77843_CABLE_GROUP_ADC_GND, &attached);
601 /* Charger cable on MHL accessory is attach or detach */
602 if (gnd_type == MAX77843_MUIC_GND_MHL_VB)
603 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
605 else if (gnd_type == MAX77843_MUIC_GND_MHL)
606 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
609 case MAX77843_MUIC_CHG_DOCK:
610 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, attached);
612 case MAX77843_MUIC_CHG_NONE:
616 "failed to detect %s accessory (chg_type:0x%x)\n",
617 attached ? "attached" : "detached", chg_type);
619 max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_OPEN,
627 static void max77843_muic_irq_work(struct work_struct *work)
629 struct max77843_muic_info *info = container_of(work,
630 struct max77843_muic_info, irq_work);
631 struct max77693_dev *max77843 = info->max77843;
634 mutex_lock(&info->mutex);
636 ret = regmap_bulk_read(max77843->regmap_muic,
637 MAX77843_MUIC_REG_STATUS1, info->status,
638 MAX77843_MUIC_STATUS_NUM);
640 dev_err(info->dev, "Cannot read STATUS registers\n");
641 mutex_unlock(&info->mutex);
646 ret = max77843_muic_adc_handler(info);
648 dev_err(info->dev, "Unknown cable type\n");
649 info->irq_adc = false;
653 ret = max77843_muic_chg_handler(info);
655 dev_err(info->dev, "Unknown charger type\n");
656 info->irq_chg = false;
659 mutex_unlock(&info->mutex);
662 static irqreturn_t max77843_muic_irq_handler(int irq, void *data)
664 struct max77843_muic_info *info = data;
665 int i, irq_type = -1;
667 for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++)
668 if (irq == max77843_muic_irqs[i].virq)
669 irq_type = max77843_muic_irqs[i].irq;
672 case MAX77843_MUIC_IRQ_INT1_ADC:
673 case MAX77843_MUIC_IRQ_INT1_ADCERROR:
674 case MAX77843_MUIC_IRQ_INT1_ADC1K:
675 info->irq_adc = true;
677 case MAX77843_MUIC_IRQ_INT2_CHGTYP:
678 case MAX77843_MUIC_IRQ_INT2_CHGDETRUN:
679 case MAX77843_MUIC_IRQ_INT2_DCDTMR:
680 case MAX77843_MUIC_IRQ_INT2_DXOVP:
681 case MAX77843_MUIC_IRQ_INT2_VBVOLT:
682 info->irq_chg = true;
684 case MAX77843_MUIC_IRQ_INT3_VBADC:
685 case MAX77843_MUIC_IRQ_INT3_VDNMON:
686 case MAX77843_MUIC_IRQ_INT3_DNRES:
687 case MAX77843_MUIC_IRQ_INT3_MPNACK:
688 case MAX77843_MUIC_IRQ_INT3_MRXBUFOW:
689 case MAX77843_MUIC_IRQ_INT3_MRXTRF:
690 case MAX77843_MUIC_IRQ_INT3_MRXPERR:
691 case MAX77843_MUIC_IRQ_INT3_MRXRDY:
694 dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type);
698 schedule_work(&info->irq_work);
703 static void max77843_muic_detect_cable_wq(struct work_struct *work)
705 struct max77843_muic_info *info = container_of(to_delayed_work(work),
706 struct max77843_muic_info, wq_detcable);
707 struct max77693_dev *max77843 = info->max77843;
708 int chg_type, adc, ret;
711 mutex_lock(&info->mutex);
713 ret = regmap_bulk_read(max77843->regmap_muic,
714 MAX77843_MUIC_REG_STATUS1, info->status,
715 MAX77843_MUIC_STATUS_NUM);
717 dev_err(info->dev, "Cannot read STATUS registers\n");
721 adc = max77843_muic_get_cable_type(info,
722 MAX77843_CABLE_GROUP_ADC, &attached);
723 if (attached && adc != MAX77843_MUIC_ADC_OPEN) {
724 ret = max77843_muic_adc_handler(info);
726 dev_err(info->dev, "Cannot detect accessory\n");
731 chg_type = max77843_muic_get_cable_type(info,
732 MAX77843_CABLE_GROUP_CHG, &attached);
733 if (attached && chg_type != MAX77843_MUIC_CHG_NONE) {
734 ret = max77843_muic_chg_handler(info);
736 dev_err(info->dev, "Cannot detect charger accessory\n");
742 mutex_unlock(&info->mutex);
745 static int max77843_muic_set_debounce_time(struct max77843_muic_info *info,
746 enum max77843_muic_adc_debounce_time time)
748 struct max77693_dev *max77843 = info->max77843;
752 case MAX77843_DEBOUNCE_TIME_5MS:
753 case MAX77843_DEBOUNCE_TIME_10MS:
754 case MAX77843_DEBOUNCE_TIME_25MS:
755 case MAX77843_DEBOUNCE_TIME_38_62MS:
756 ret = regmap_update_bits(max77843->regmap_muic,
757 MAX77843_MUIC_REG_CONTROL4,
758 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK,
759 time << MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT);
761 dev_err(info->dev, "Cannot write MUIC regmap\n");
766 dev_err(info->dev, "Invalid ADC debounce time\n");
773 static int max77843_init_muic_regmap(struct max77693_dev *max77843)
777 max77843->i2c_muic = i2c_new_dummy_device(max77843->i2c->adapter,
779 if (IS_ERR(max77843->i2c_muic)) {
780 dev_err(&max77843->i2c->dev,
781 "Cannot allocate I2C device for MUIC\n");
782 return PTR_ERR(max77843->i2c_muic);
785 i2c_set_clientdata(max77843->i2c_muic, max77843);
787 max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic,
788 &max77843_muic_regmap_config);
789 if (IS_ERR(max77843->regmap_muic)) {
790 ret = PTR_ERR(max77843->regmap_muic);
794 ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq,
795 IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
796 0, &max77843_muic_irq_chip, &max77843->irq_data_muic);
798 dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n");
805 i2c_unregister_device(max77843->i2c_muic);
810 static int max77843_muic_probe(struct platform_device *pdev)
812 struct max77693_dev *max77843 = dev_get_drvdata(pdev->dev.parent);
813 struct max77843_muic_info *info;
819 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
823 info->dev = &pdev->dev;
824 info->max77843 = max77843;
826 platform_set_drvdata(pdev, info);
827 mutex_init(&info->mutex);
829 /* Initialize i2c and regmap */
830 ret = max77843_init_muic_regmap(max77843);
832 dev_err(&pdev->dev, "Failed to init MUIC regmap\n");
836 /* Turn off auto detection configuration */
837 ret = regmap_update_bits(max77843->regmap_muic,
838 MAX77843_MUIC_REG_CONTROL4,
839 MAX77843_MUIC_CONTROL4_USBAUTO_MASK |
840 MAX77843_MUIC_CONTROL4_FCTAUTO_MASK,
841 CONTROL4_AUTO_DISABLE);
843 /* Initialize extcon device */
844 info->edev = devm_extcon_dev_allocate(&pdev->dev,
845 max77843_extcon_cable);
846 if (IS_ERR(info->edev)) {
847 dev_err(&pdev->dev, "Failed to allocate memory for extcon\n");
848 ret = PTR_ERR(info->edev);
852 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
854 dev_err(&pdev->dev, "Failed to register extcon device\n");
858 /* Set ADC debounce time */
859 max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS);
861 /* Set initial path for UART when JIG is connected to get serial logs */
862 ret = regmap_bulk_read(max77843->regmap_muic,
863 MAX77843_MUIC_REG_STATUS1, info->status,
864 MAX77843_MUIC_STATUS_NUM);
866 dev_err(info->dev, "Cannot read STATUS registers\n");
869 cable_type = max77843_muic_get_cable_type(info, MAX77843_CABLE_GROUP_ADC,
871 if (attached && cable_type == MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF)
872 max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_UART,
875 /* Check revision number of MUIC device */
876 ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id);
878 dev_err(&pdev->dev, "Failed to read revision number\n");
881 dev_info(info->dev, "MUIC device ID : 0x%x\n", id);
883 /* Support virtual irq domain for max77843 MUIC device */
884 INIT_WORK(&info->irq_work, max77843_muic_irq_work);
886 /* Clear IRQ bits before request IRQs */
887 ret = regmap_bulk_read(max77843->regmap_muic,
888 MAX77843_MUIC_REG_INT1, info->status,
889 MAX77843_MUIC_STATUS_NUM);
891 dev_err(&pdev->dev, "Failed to Clear IRQ bits\n");
895 for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) {
896 struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i];
899 virq = regmap_irq_get_virq(max77843->irq_data_muic,
905 muic_irq->virq = virq;
907 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
908 max77843_muic_irq_handler, IRQF_NO_SUSPEND,
909 muic_irq->name, info);
912 "Failed to request irq (IRQ: %d, error: %d)\n",
918 /* Detect accessory after completing the initialization of platform */
919 INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq);
920 queue_delayed_work(system_power_efficient_wq,
921 &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT));
926 regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
927 i2c_unregister_device(max77843->i2c_muic);
932 static int max77843_muic_remove(struct platform_device *pdev)
934 struct max77843_muic_info *info = platform_get_drvdata(pdev);
935 struct max77693_dev *max77843 = info->max77843;
937 cancel_work_sync(&info->irq_work);
938 regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
939 i2c_unregister_device(max77843->i2c_muic);
944 static const struct platform_device_id max77843_muic_id[] = {
945 { "max77843-muic", },
948 MODULE_DEVICE_TABLE(platform, max77843_muic_id);
950 static struct platform_driver max77843_muic_driver = {
952 .name = "max77843-muic",
954 .probe = max77843_muic_probe,
955 .remove = max77843_muic_remove,
956 .id_table = max77843_muic_id,
959 static int __init max77843_muic_init(void)
961 return platform_driver_register(&max77843_muic_driver);
963 subsys_initcall(max77843_muic_init);
965 MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
966 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
967 MODULE_LICENSE("GPL");