2 * extcon-max77843.c - Maxim MAX77843 extcon driver to support
3 * MUIC(Micro USB Interface Controller)
5 * Copyright (C) 2015 Samsung Electronics
6 * Author: Jaewon Kim <jaewon02.kim@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/extcon.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/max77693-common.h>
19 #include <linux/mfd/max77843-private.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/workqueue.h>
24 #define DELAY_MS_DEFAULT 15000 /* unit: millisecond */
26 enum max77843_muic_status
{
27 MAX77843_MUIC_STATUS1
= 0,
28 MAX77843_MUIC_STATUS2
,
29 MAX77843_MUIC_STATUS3
,
31 MAX77843_MUIC_STATUS_NUM
,
34 struct max77843_muic_info
{
36 struct max77693_dev
*max77843
;
37 struct extcon_dev
*edev
;
40 struct work_struct irq_work
;
41 struct delayed_work wq_detcable
;
43 u8 status
[MAX77843_MUIC_STATUS_NUM
];
52 enum max77843_muic_cable_group
{
53 MAX77843_CABLE_GROUP_ADC
= 0,
54 MAX77843_CABLE_GROUP_ADC_GND
,
55 MAX77843_CABLE_GROUP_CHG
,
58 enum max77843_muic_adc_debounce_time
{
59 MAX77843_DEBOUNCE_TIME_5MS
= 0,
60 MAX77843_DEBOUNCE_TIME_10MS
,
61 MAX77843_DEBOUNCE_TIME_25MS
,
62 MAX77843_DEBOUNCE_TIME_38_62MS
,
65 /* Define accessory cable type */
66 enum max77843_muic_accessory_type
{
67 MAX77843_MUIC_ADC_GROUND
= 0,
68 MAX77843_MUIC_ADC_SEND_END_BUTTON
,
69 MAX77843_MUIC_ADC_REMOTE_S1_BUTTON
,
70 MAX77843_MUIC_ADC_REMOTE_S2_BUTTON
,
71 MAX77843_MUIC_ADC_REMOTE_S3_BUTTON
,
72 MAX77843_MUIC_ADC_REMOTE_S4_BUTTON
,
73 MAX77843_MUIC_ADC_REMOTE_S5_BUTTON
,
74 MAX77843_MUIC_ADC_REMOTE_S6_BUTTON
,
75 MAX77843_MUIC_ADC_REMOTE_S7_BUTTON
,
76 MAX77843_MUIC_ADC_REMOTE_S8_BUTTON
,
77 MAX77843_MUIC_ADC_REMOTE_S9_BUTTON
,
78 MAX77843_MUIC_ADC_REMOTE_S10_BUTTON
,
79 MAX77843_MUIC_ADC_REMOTE_S11_BUTTON
,
80 MAX77843_MUIC_ADC_REMOTE_S12_BUTTON
,
81 MAX77843_MUIC_ADC_RESERVED_ACC_1
,
82 MAX77843_MUIC_ADC_RESERVED_ACC_2
,
83 MAX77843_MUIC_ADC_RESERVED_ACC_3
,
84 MAX77843_MUIC_ADC_RESERVED_ACC_4
,
85 MAX77843_MUIC_ADC_RESERVED_ACC_5
,
86 MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2
,
87 MAX77843_MUIC_ADC_PHONE_POWERED_DEV
,
88 MAX77843_MUIC_ADC_TTY_CONVERTER
,
89 MAX77843_MUIC_ADC_UART_CABLE
,
90 MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG
,
91 MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF
,
92 MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON
,
93 MAX77843_MUIC_ADC_AV_CABLE_NOLOAD
,
94 MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG
,
95 MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF
,
96 MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON
,
97 MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1
,
98 MAX77843_MUIC_ADC_OPEN
,
101 * The below accessories should check
102 * not only ADC value but also ADC1K and VBVolt value.
104 /* Offset|ADC1K|VBVolt| */
105 MAX77843_MUIC_GND_USB_HOST
= 0x100, /* 0x1| 0| 0| */
106 MAX77843_MUIC_GND_USB_HOST_VB
= 0x101, /* 0x1| 0| 1| */
107 MAX77843_MUIC_GND_MHL
= 0x102, /* 0x1| 1| 0| */
108 MAX77843_MUIC_GND_MHL_VB
= 0x103, /* 0x1| 1| 1| */
111 /* Define charger cable type */
112 enum max77843_muic_charger_type
{
113 MAX77843_MUIC_CHG_NONE
= 0,
114 MAX77843_MUIC_CHG_USB
,
115 MAX77843_MUIC_CHG_DOWNSTREAM
,
116 MAX77843_MUIC_CHG_DEDICATED
,
117 MAX77843_MUIC_CHG_SPECIAL_500MA
,
118 MAX77843_MUIC_CHG_SPECIAL_1A
,
119 MAX77843_MUIC_CHG_SPECIAL_BIAS
,
120 MAX77843_MUIC_CHG_RESERVED
,
121 MAX77843_MUIC_CHG_GND
,
124 static const unsigned int max77843_extcon_cable
[] = {
137 struct max77843_muic_irq
{
143 static struct max77843_muic_irq max77843_muic_irqs
[] = {
144 { MAX77843_MUIC_IRQ_INT1_ADC
, "MUIC-ADC" },
145 { MAX77843_MUIC_IRQ_INT1_ADCERROR
, "MUIC-ADC_ERROR" },
146 { MAX77843_MUIC_IRQ_INT1_ADC1K
, "MUIC-ADC1K" },
147 { MAX77843_MUIC_IRQ_INT2_CHGTYP
, "MUIC-CHGTYP" },
148 { MAX77843_MUIC_IRQ_INT2_CHGDETRUN
, "MUIC-CHGDETRUN" },
149 { MAX77843_MUIC_IRQ_INT2_DCDTMR
, "MUIC-DCDTMR" },
150 { MAX77843_MUIC_IRQ_INT2_DXOVP
, "MUIC-DXOVP" },
151 { MAX77843_MUIC_IRQ_INT2_VBVOLT
, "MUIC-VBVOLT" },
152 { MAX77843_MUIC_IRQ_INT3_VBADC
, "MUIC-VBADC" },
153 { MAX77843_MUIC_IRQ_INT3_VDNMON
, "MUIC-VDNMON" },
154 { MAX77843_MUIC_IRQ_INT3_DNRES
, "MUIC-DNRES" },
155 { MAX77843_MUIC_IRQ_INT3_MPNACK
, "MUIC-MPNACK"},
156 { MAX77843_MUIC_IRQ_INT3_MRXBUFOW
, "MUIC-MRXBUFOW"},
157 { MAX77843_MUIC_IRQ_INT3_MRXTRF
, "MUIC-MRXTRF"},
158 { MAX77843_MUIC_IRQ_INT3_MRXPERR
, "MUIC-MRXPERR"},
159 { MAX77843_MUIC_IRQ_INT3_MRXRDY
, "MUIC-MRXRDY"},
162 static const struct regmap_config max77843_muic_regmap_config
= {
165 .max_register
= MAX77843_MUIC_REG_END
,
168 static const struct regmap_irq max77843_muic_irq
[] = {
170 { .reg_offset
= 0, .mask
= MAX77843_MUIC_ADC
, },
171 { .reg_offset
= 0, .mask
= MAX77843_MUIC_ADCERROR
, },
172 { .reg_offset
= 0, .mask
= MAX77843_MUIC_ADC1K
, },
175 { .reg_offset
= 1, .mask
= MAX77843_MUIC_CHGTYP
, },
176 { .reg_offset
= 1, .mask
= MAX77843_MUIC_CHGDETRUN
, },
177 { .reg_offset
= 1, .mask
= MAX77843_MUIC_DCDTMR
, },
178 { .reg_offset
= 1, .mask
= MAX77843_MUIC_DXOVP
, },
179 { .reg_offset
= 1, .mask
= MAX77843_MUIC_VBVOLT
, },
182 { .reg_offset
= 2, .mask
= MAX77843_MUIC_VBADC
, },
183 { .reg_offset
= 2, .mask
= MAX77843_MUIC_VDNMON
, },
184 { .reg_offset
= 2, .mask
= MAX77843_MUIC_DNRES
, },
185 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MPNACK
, },
186 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXBUFOW
, },
187 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXTRF
, },
188 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXPERR
, },
189 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXRDY
, },
192 static const struct regmap_irq_chip max77843_muic_irq_chip
= {
193 .name
= "max77843-muic",
194 .status_base
= MAX77843_MUIC_REG_INT1
,
195 .mask_base
= MAX77843_MUIC_REG_INTMASK1
,
198 .irqs
= max77843_muic_irq
,
199 .num_irqs
= ARRAY_SIZE(max77843_muic_irq
),
202 static int max77843_muic_set_path(struct max77843_muic_info
*info
,
203 u8 val
, bool attached
)
205 struct max77693_dev
*max77843
= info
->max77843
;
207 unsigned int ctrl1
, ctrl2
;
212 ctrl1
= MAX77843_MUIC_CONTROL1_SW_OPEN
;
214 ret
= regmap_update_bits(max77843
->regmap_muic
,
215 MAX77843_MUIC_REG_CONTROL1
,
216 MAX77843_MUIC_CONTROL1_COM_SW
, ctrl1
);
218 dev_err(info
->dev
, "Cannot switch MUIC port\n");
223 ctrl2
= MAX77843_MUIC_CONTROL2_CPEN_MASK
;
225 ctrl2
= MAX77843_MUIC_CONTROL2_LOWPWR_MASK
;
227 ret
= regmap_update_bits(max77843
->regmap_muic
,
228 MAX77843_MUIC_REG_CONTROL2
,
229 MAX77843_MUIC_CONTROL2_LOWPWR_MASK
|
230 MAX77843_MUIC_CONTROL2_CPEN_MASK
, ctrl2
);
232 dev_err(info
->dev
, "Cannot update lowpower mode\n");
237 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
238 ctrl1
, ctrl2
, attached
? "attached" : "detached");
243 static int max77843_muic_get_cable_type(struct max77843_muic_info
*info
,
244 enum max77843_muic_cable_group group
, bool *attached
)
246 int adc
, chg_type
, cable_type
, gnd_type
;
248 adc
= info
->status
[MAX77843_MUIC_STATUS1
] &
249 MAX77843_MUIC_STATUS1_ADC_MASK
;
250 adc
>>= MAX77843_MUIC_STATUS1_ADC_SHIFT
;
253 case MAX77843_CABLE_GROUP_ADC
:
254 if (adc
== MAX77843_MUIC_ADC_OPEN
) {
256 cable_type
= info
->prev_cable_type
;
257 info
->prev_cable_type
= MAX77843_MUIC_ADC_OPEN
;
260 cable_type
= info
->prev_cable_type
= adc
;
263 case MAX77843_CABLE_GROUP_CHG
:
264 chg_type
= info
->status
[MAX77843_MUIC_STATUS2
] &
265 MAX77843_MUIC_STATUS2_CHGTYP_MASK
;
267 /* Check GROUND accessory with charger cable */
268 if (adc
== MAX77843_MUIC_ADC_GROUND
) {
269 if (chg_type
== MAX77843_MUIC_CHG_NONE
) {
271 * The following state when charger cable is
272 * disconnected but the GROUND accessory still
276 cable_type
= info
->prev_chg_type
;
277 info
->prev_chg_type
= MAX77843_MUIC_CHG_NONE
;
281 * The following state when charger cable is
282 * connected on the GROUND accessory.
285 cable_type
= MAX77843_MUIC_CHG_GND
;
286 info
->prev_chg_type
= MAX77843_MUIC_CHG_GND
;
291 if (chg_type
== MAX77843_MUIC_CHG_NONE
) {
293 cable_type
= info
->prev_chg_type
;
294 info
->prev_chg_type
= MAX77843_MUIC_CHG_NONE
;
297 cable_type
= info
->prev_chg_type
= chg_type
;
300 case MAX77843_CABLE_GROUP_ADC_GND
:
301 if (adc
== MAX77843_MUIC_ADC_OPEN
) {
303 cable_type
= info
->prev_gnd_type
;
304 info
->prev_gnd_type
= MAX77843_MUIC_ADC_OPEN
;
309 * Offset|ADC1K|VBVolt|
310 * 0x1| 0| 0| USB-HOST
311 * 0x1| 0| 1| USB-HOST with VB
313 * 0x1| 1| 1| MHL with VB
315 /* Get ADC1K register bit */
316 gnd_type
= (info
->status
[MAX77843_MUIC_STATUS1
] &
317 MAX77843_MUIC_STATUS1_ADC1K_MASK
);
319 /* Get VBVolt register bit */
320 gnd_type
|= (info
->status
[MAX77843_MUIC_STATUS2
] &
321 MAX77843_MUIC_STATUS2_VBVOLT_MASK
);
322 gnd_type
>>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT
;
324 /* Offset of GND cable */
325 gnd_type
|= MAX77843_MUIC_GND_USB_HOST
;
326 cable_type
= info
->prev_gnd_type
= gnd_type
;
330 dev_err(info
->dev
, "Unknown cable group (%d)\n", group
);
331 cable_type
= -EINVAL
;
338 static int max77843_muic_adc_gnd_handler(struct max77843_muic_info
*info
)
340 int ret
, gnd_cable_type
;
343 gnd_cable_type
= max77843_muic_get_cable_type(info
,
344 MAX77843_CABLE_GROUP_ADC_GND
, &attached
);
345 dev_dbg(info
->dev
, "external connector is %s (gnd:0x%02x)\n",
346 attached
? "attached" : "detached", gnd_cable_type
);
348 switch (gnd_cable_type
) {
349 case MAX77843_MUIC_GND_USB_HOST
:
350 case MAX77843_MUIC_GND_USB_HOST_VB
:
351 ret
= max77843_muic_set_path(info
,
352 MAX77843_MUIC_CONTROL1_SW_USB
,
357 extcon_set_state_sync(info
->edev
, EXTCON_USB_HOST
, attached
);
359 case MAX77843_MUIC_GND_MHL_VB
:
360 case MAX77843_MUIC_GND_MHL
:
361 ret
= max77843_muic_set_path(info
,
362 MAX77843_MUIC_CONTROL1_SW_OPEN
,
367 extcon_set_state_sync(info
->edev
, EXTCON_DISP_MHL
, attached
);
370 dev_err(info
->dev
, "failed to detect %s accessory(gnd:0x%x)\n",
371 attached
? "attached" : "detached", gnd_cable_type
);
378 static int max77843_muic_jig_handler(struct max77843_muic_info
*info
,
379 int cable_type
, bool attached
)
382 u8 path
= MAX77843_MUIC_CONTROL1_SW_OPEN
;
384 dev_dbg(info
->dev
, "external connector is %s (adc:0x%02x)\n",
385 attached
? "attached" : "detached", cable_type
);
387 switch (cable_type
) {
388 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF
:
389 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON
:
390 path
= MAX77843_MUIC_CONTROL1_SW_USB
;
392 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF
:
393 path
= MAX77843_MUIC_CONTROL1_SW_UART
;
399 ret
= max77843_muic_set_path(info
, path
, attached
);
403 extcon_set_state_sync(info
->edev
, EXTCON_JIG
, attached
);
408 static int max77843_muic_adc_handler(struct max77843_muic_info
*info
)
413 cable_type
= max77843_muic_get_cable_type(info
,
414 MAX77843_CABLE_GROUP_ADC
, &attached
);
417 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
418 attached
? "attached" : "detached", cable_type
,
419 info
->prev_cable_type
);
421 switch (cable_type
) {
422 case MAX77843_MUIC_ADC_GROUND
:
423 ret
= max77843_muic_adc_gnd_handler(info
);
427 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF
:
428 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON
:
429 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF
:
430 ret
= max77843_muic_jig_handler(info
, cable_type
, attached
);
434 case MAX77843_MUIC_ADC_SEND_END_BUTTON
:
435 case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON
:
436 case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON
:
437 case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON
:
438 case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON
:
439 case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON
:
440 case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON
:
441 case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON
:
442 case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON
:
443 case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON
:
444 case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON
:
445 case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON
:
446 case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON
:
447 case MAX77843_MUIC_ADC_RESERVED_ACC_1
:
448 case MAX77843_MUIC_ADC_RESERVED_ACC_2
:
449 case MAX77843_MUIC_ADC_RESERVED_ACC_3
:
450 case MAX77843_MUIC_ADC_RESERVED_ACC_4
:
451 case MAX77843_MUIC_ADC_RESERVED_ACC_5
:
452 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2
:
453 case MAX77843_MUIC_ADC_PHONE_POWERED_DEV
:
454 case MAX77843_MUIC_ADC_TTY_CONVERTER
:
455 case MAX77843_MUIC_ADC_UART_CABLE
:
456 case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG
:
457 case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD
:
458 case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG
:
459 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON
:
460 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1
:
461 case MAX77843_MUIC_ADC_OPEN
:
463 "accessory is %s but it isn't used (adc:0x%x)\n",
464 attached
? "attached" : "detached", cable_type
);
468 "failed to detect %s accessory (adc:0x%x)\n",
469 attached
? "attached" : "detached", cable_type
);
476 static int max77843_muic_chg_handler(struct max77843_muic_info
*info
)
478 int ret
, chg_type
, gnd_type
;
481 chg_type
= max77843_muic_get_cable_type(info
,
482 MAX77843_CABLE_GROUP_CHG
, &attached
);
485 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
486 attached
? "attached" : "detached",
487 chg_type
, info
->prev_chg_type
);
490 case MAX77843_MUIC_CHG_USB
:
491 ret
= max77843_muic_set_path(info
,
492 MAX77843_MUIC_CONTROL1_SW_USB
,
497 extcon_set_state_sync(info
->edev
, EXTCON_USB
, attached
);
498 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_SDP
,
501 case MAX77843_MUIC_CHG_DOWNSTREAM
:
502 ret
= max77843_muic_set_path(info
,
503 MAX77843_MUIC_CONTROL1_SW_OPEN
,
508 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_CDP
,
511 case MAX77843_MUIC_CHG_DEDICATED
:
512 ret
= max77843_muic_set_path(info
,
513 MAX77843_MUIC_CONTROL1_SW_OPEN
,
518 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_DCP
,
521 case MAX77843_MUIC_CHG_SPECIAL_500MA
:
522 ret
= max77843_muic_set_path(info
,
523 MAX77843_MUIC_CONTROL1_SW_OPEN
,
528 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_SLOW
,
531 case MAX77843_MUIC_CHG_SPECIAL_1A
:
532 ret
= max77843_muic_set_path(info
,
533 MAX77843_MUIC_CONTROL1_SW_OPEN
,
538 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_FAST
,
541 case MAX77843_MUIC_CHG_GND
:
542 gnd_type
= max77843_muic_get_cable_type(info
,
543 MAX77843_CABLE_GROUP_ADC_GND
, &attached
);
545 /* Charger cable on MHL accessory is attach or detach */
546 if (gnd_type
== MAX77843_MUIC_GND_MHL_VB
)
547 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_DCP
,
549 else if (gnd_type
== MAX77843_MUIC_GND_MHL
)
550 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_DCP
,
553 case MAX77843_MUIC_CHG_NONE
:
557 "failed to detect %s accessory (chg_type:0x%x)\n",
558 attached
? "attached" : "detached", chg_type
);
560 max77843_muic_set_path(info
, MAX77843_MUIC_CONTROL1_SW_OPEN
,
568 static void max77843_muic_irq_work(struct work_struct
*work
)
570 struct max77843_muic_info
*info
= container_of(work
,
571 struct max77843_muic_info
, irq_work
);
572 struct max77693_dev
*max77843
= info
->max77843
;
575 mutex_lock(&info
->mutex
);
577 ret
= regmap_bulk_read(max77843
->regmap_muic
,
578 MAX77843_MUIC_REG_STATUS1
, info
->status
,
579 MAX77843_MUIC_STATUS_NUM
);
581 dev_err(info
->dev
, "Cannot read STATUS registers\n");
582 mutex_unlock(&info
->mutex
);
587 ret
= max77843_muic_adc_handler(info
);
589 dev_err(info
->dev
, "Unknown cable type\n");
590 info
->irq_adc
= false;
594 ret
= max77843_muic_chg_handler(info
);
596 dev_err(info
->dev
, "Unknown charger type\n");
597 info
->irq_chg
= false;
600 mutex_unlock(&info
->mutex
);
603 static irqreturn_t
max77843_muic_irq_handler(int irq
, void *data
)
605 struct max77843_muic_info
*info
= data
;
606 int i
, irq_type
= -1;
608 for (i
= 0; i
< ARRAY_SIZE(max77843_muic_irqs
); i
++)
609 if (irq
== max77843_muic_irqs
[i
].virq
)
610 irq_type
= max77843_muic_irqs
[i
].irq
;
613 case MAX77843_MUIC_IRQ_INT1_ADC
:
614 case MAX77843_MUIC_IRQ_INT1_ADCERROR
:
615 case MAX77843_MUIC_IRQ_INT1_ADC1K
:
616 info
->irq_adc
= true;
618 case MAX77843_MUIC_IRQ_INT2_CHGTYP
:
619 case MAX77843_MUIC_IRQ_INT2_CHGDETRUN
:
620 case MAX77843_MUIC_IRQ_INT2_DCDTMR
:
621 case MAX77843_MUIC_IRQ_INT2_DXOVP
:
622 case MAX77843_MUIC_IRQ_INT2_VBVOLT
:
623 info
->irq_chg
= true;
625 case MAX77843_MUIC_IRQ_INT3_VBADC
:
626 case MAX77843_MUIC_IRQ_INT3_VDNMON
:
627 case MAX77843_MUIC_IRQ_INT3_DNRES
:
628 case MAX77843_MUIC_IRQ_INT3_MPNACK
:
629 case MAX77843_MUIC_IRQ_INT3_MRXBUFOW
:
630 case MAX77843_MUIC_IRQ_INT3_MRXTRF
:
631 case MAX77843_MUIC_IRQ_INT3_MRXPERR
:
632 case MAX77843_MUIC_IRQ_INT3_MRXRDY
:
635 dev_err(info
->dev
, "Cannot recognize IRQ(%d)\n", irq_type
);
639 schedule_work(&info
->irq_work
);
644 static void max77843_muic_detect_cable_wq(struct work_struct
*work
)
646 struct max77843_muic_info
*info
= container_of(to_delayed_work(work
),
647 struct max77843_muic_info
, wq_detcable
);
648 struct max77693_dev
*max77843
= info
->max77843
;
649 int chg_type
, adc
, ret
;
652 mutex_lock(&info
->mutex
);
654 ret
= regmap_bulk_read(max77843
->regmap_muic
,
655 MAX77843_MUIC_REG_STATUS1
, info
->status
,
656 MAX77843_MUIC_STATUS_NUM
);
658 dev_err(info
->dev
, "Cannot read STATUS registers\n");
662 adc
= max77843_muic_get_cable_type(info
,
663 MAX77843_CABLE_GROUP_ADC
, &attached
);
664 if (attached
&& adc
!= MAX77843_MUIC_ADC_OPEN
) {
665 ret
= max77843_muic_adc_handler(info
);
667 dev_err(info
->dev
, "Cannot detect accessory\n");
672 chg_type
= max77843_muic_get_cable_type(info
,
673 MAX77843_CABLE_GROUP_CHG
, &attached
);
674 if (attached
&& chg_type
!= MAX77843_MUIC_CHG_NONE
) {
675 ret
= max77843_muic_chg_handler(info
);
677 dev_err(info
->dev
, "Cannot detect charger accessory\n");
683 mutex_unlock(&info
->mutex
);
686 static int max77843_muic_set_debounce_time(struct max77843_muic_info
*info
,
687 enum max77843_muic_adc_debounce_time time
)
689 struct max77693_dev
*max77843
= info
->max77843
;
693 case MAX77843_DEBOUNCE_TIME_5MS
:
694 case MAX77843_DEBOUNCE_TIME_10MS
:
695 case MAX77843_DEBOUNCE_TIME_25MS
:
696 case MAX77843_DEBOUNCE_TIME_38_62MS
:
697 ret
= regmap_update_bits(max77843
->regmap_muic
,
698 MAX77843_MUIC_REG_CONTROL4
,
699 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK
,
700 time
<< MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT
);
702 dev_err(info
->dev
, "Cannot write MUIC regmap\n");
707 dev_err(info
->dev
, "Invalid ADC debounce time\n");
714 static int max77843_init_muic_regmap(struct max77693_dev
*max77843
)
718 max77843
->i2c_muic
= i2c_new_dummy(max77843
->i2c
->adapter
,
720 if (!max77843
->i2c_muic
) {
721 dev_err(&max77843
->i2c
->dev
,
722 "Cannot allocate I2C device for MUIC\n");
726 i2c_set_clientdata(max77843
->i2c_muic
, max77843
);
728 max77843
->regmap_muic
= devm_regmap_init_i2c(max77843
->i2c_muic
,
729 &max77843_muic_regmap_config
);
730 if (IS_ERR(max77843
->regmap_muic
)) {
731 ret
= PTR_ERR(max77843
->regmap_muic
);
735 ret
= regmap_add_irq_chip(max77843
->regmap_muic
, max77843
->irq
,
736 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
| IRQF_SHARED
,
737 0, &max77843_muic_irq_chip
, &max77843
->irq_data_muic
);
739 dev_err(&max77843
->i2c
->dev
, "Cannot add MUIC IRQ chip\n");
746 i2c_unregister_device(max77843
->i2c_muic
);
751 static int max77843_muic_probe(struct platform_device
*pdev
)
753 struct max77693_dev
*max77843
= dev_get_drvdata(pdev
->dev
.parent
);
754 struct max77843_muic_info
*info
;
758 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
762 info
->dev
= &pdev
->dev
;
763 info
->max77843
= max77843
;
765 platform_set_drvdata(pdev
, info
);
766 mutex_init(&info
->mutex
);
768 /* Initialize i2c and regmap */
769 ret
= max77843_init_muic_regmap(max77843
);
771 dev_err(&pdev
->dev
, "Failed to init MUIC regmap\n");
775 /* Turn off auto detection configuration */
776 ret
= regmap_update_bits(max77843
->regmap_muic
,
777 MAX77843_MUIC_REG_CONTROL4
,
778 MAX77843_MUIC_CONTROL4_USBAUTO_MASK
|
779 MAX77843_MUIC_CONTROL4_FCTAUTO_MASK
,
780 CONTROL4_AUTO_DISABLE
);
782 /* Initialize extcon device */
783 info
->edev
= devm_extcon_dev_allocate(&pdev
->dev
,
784 max77843_extcon_cable
);
785 if (IS_ERR(info
->edev
)) {
786 dev_err(&pdev
->dev
, "Failed to allocate memory for extcon\n");
791 ret
= devm_extcon_dev_register(&pdev
->dev
, info
->edev
);
793 dev_err(&pdev
->dev
, "Failed to register extcon device\n");
797 /* Set ADC debounce time */
798 max77843_muic_set_debounce_time(info
, MAX77843_DEBOUNCE_TIME_25MS
);
800 /* Set initial path for UART */
801 max77843_muic_set_path(info
, MAX77843_MUIC_CONTROL1_SW_UART
, true);
803 /* Check revision number of MUIC device */
804 ret
= regmap_read(max77843
->regmap_muic
, MAX77843_MUIC_REG_ID
, &id
);
806 dev_err(&pdev
->dev
, "Failed to read revision number\n");
809 dev_info(info
->dev
, "MUIC device ID : 0x%x\n", id
);
811 /* Support virtual irq domain for max77843 MUIC device */
812 INIT_WORK(&info
->irq_work
, max77843_muic_irq_work
);
814 /* Clear IRQ bits before request IRQs */
815 ret
= regmap_bulk_read(max77843
->regmap_muic
,
816 MAX77843_MUIC_REG_INT1
, info
->status
,
817 MAX77843_MUIC_STATUS_NUM
);
819 dev_err(&pdev
->dev
, "Failed to Clear IRQ bits\n");
823 for (i
= 0; i
< ARRAY_SIZE(max77843_muic_irqs
); i
++) {
824 struct max77843_muic_irq
*muic_irq
= &max77843_muic_irqs
[i
];
827 virq
= regmap_irq_get_virq(max77843
->irq_data_muic
,
833 muic_irq
->virq
= virq
;
835 ret
= devm_request_threaded_irq(&pdev
->dev
, virq
, NULL
,
836 max77843_muic_irq_handler
, IRQF_NO_SUSPEND
,
837 muic_irq
->name
, info
);
840 "Failed to request irq (IRQ: %d, error: %d)\n",
846 /* Detect accessory after completing the initialization of platform */
847 INIT_DELAYED_WORK(&info
->wq_detcable
, max77843_muic_detect_cable_wq
);
848 queue_delayed_work(system_power_efficient_wq
,
849 &info
->wq_detcable
, msecs_to_jiffies(DELAY_MS_DEFAULT
));
854 regmap_del_irq_chip(max77843
->irq
, max77843
->irq_data_muic
);
855 i2c_unregister_device(max77843
->i2c_muic
);
860 static int max77843_muic_remove(struct platform_device
*pdev
)
862 struct max77843_muic_info
*info
= platform_get_drvdata(pdev
);
863 struct max77693_dev
*max77843
= info
->max77843
;
865 cancel_work_sync(&info
->irq_work
);
866 regmap_del_irq_chip(max77843
->irq
, max77843
->irq_data_muic
);
867 i2c_unregister_device(max77843
->i2c_muic
);
872 static const struct platform_device_id max77843_muic_id
[] = {
873 { "max77843-muic", },
876 MODULE_DEVICE_TABLE(platform
, max77843_muic_id
);
878 static struct platform_driver max77843_muic_driver
= {
880 .name
= "max77843-muic",
882 .probe
= max77843_muic_probe
,
883 .remove
= max77843_muic_remove
,
884 .id_table
= max77843_muic_id
,
887 static int __init
max77843_muic_init(void)
889 return platform_driver_register(&max77843_muic_driver
);
891 subsys_initcall(max77843_muic_init
);
893 MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
894 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
895 MODULE_LICENSE("GPL");