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
,
100 /* The blow accessories should check
101 not only ADC value but also ADC1K and VBVolt value. */
102 /* Offset|ADC1K|VBVolt| */
103 MAX77843_MUIC_GND_USB_HOST
= 0x100, /* 0x1| 0| 0| */
104 MAX77843_MUIC_GND_USB_HOST_VB
= 0x101, /* 0x1| 0| 1| */
105 MAX77843_MUIC_GND_MHL
= 0x102, /* 0x1| 1| 0| */
106 MAX77843_MUIC_GND_MHL_VB
= 0x103, /* 0x1| 1| 1| */
109 /* Define charger cable type */
110 enum max77843_muic_charger_type
{
111 MAX77843_MUIC_CHG_NONE
= 0,
112 MAX77843_MUIC_CHG_USB
,
113 MAX77843_MUIC_CHG_DOWNSTREAM
,
114 MAX77843_MUIC_CHG_DEDICATED
,
115 MAX77843_MUIC_CHG_SPECIAL_500MA
,
116 MAX77843_MUIC_CHG_SPECIAL_1A
,
117 MAX77843_MUIC_CHG_SPECIAL_BIAS
,
118 MAX77843_MUIC_CHG_RESERVED
,
119 MAX77843_MUIC_CHG_GND
,
122 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
)
202 struct max77693_dev
*max77843
= info
->max77843
;
204 unsigned int ctrl1
, ctrl2
;
209 ctrl1
= MAX77843_MUIC_CONTROL1_SW_OPEN
;
211 ret
= regmap_update_bits(max77843
->regmap_muic
,
212 MAX77843_MUIC_REG_CONTROL1
,
213 MAX77843_MUIC_CONTROL1_COM_SW
, ctrl1
);
215 dev_err(info
->dev
, "Cannot switch MUIC port\n");
220 ctrl2
= MAX77843_MUIC_CONTROL2_CPEN_MASK
;
222 ctrl2
= MAX77843_MUIC_CONTROL2_LOWPWR_MASK
;
224 ret
= regmap_update_bits(max77843
->regmap_muic
,
225 MAX77843_MUIC_REG_CONTROL2
,
226 MAX77843_MUIC_CONTROL2_LOWPWR_MASK
|
227 MAX77843_MUIC_CONTROL2_CPEN_MASK
, ctrl2
);
229 dev_err(info
->dev
, "Cannot update lowpower mode\n");
234 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
235 ctrl1
, ctrl2
, attached
? "attached" : "detached");
240 static int max77843_muic_get_cable_type(struct max77843_muic_info
*info
,
241 enum max77843_muic_cable_group group
, bool *attached
)
243 int adc
, chg_type
, cable_type
, gnd_type
;
245 adc
= info
->status
[MAX77843_MUIC_STATUS1
] &
246 MAX77843_MUIC_STATUS1_ADC_MASK
;
247 adc
>>= MAX77843_MUIC_STATUS1_ADC_SHIFT
;
250 case MAX77843_CABLE_GROUP_ADC
:
251 if (adc
== MAX77843_MUIC_ADC_OPEN
) {
253 cable_type
= info
->prev_cable_type
;
254 info
->prev_cable_type
= MAX77843_MUIC_ADC_OPEN
;
257 cable_type
= info
->prev_cable_type
= adc
;
260 case MAX77843_CABLE_GROUP_CHG
:
261 chg_type
= info
->status
[MAX77843_MUIC_STATUS2
] &
262 MAX77843_MUIC_STATUS2_CHGTYP_MASK
;
264 /* Check GROUND accessory with charger cable */
265 if (adc
== MAX77843_MUIC_ADC_GROUND
) {
266 if (chg_type
== MAX77843_MUIC_CHG_NONE
) {
267 /* The following state when charger cable is
268 * disconnected but the GROUND accessory still
271 cable_type
= info
->prev_chg_type
;
272 info
->prev_chg_type
= MAX77843_MUIC_CHG_NONE
;
275 /* The following state when charger cable is
276 * connected on the GROUND accessory */
278 cable_type
= MAX77843_MUIC_CHG_GND
;
279 info
->prev_chg_type
= MAX77843_MUIC_CHG_GND
;
284 if (chg_type
== MAX77843_MUIC_CHG_NONE
) {
286 cable_type
= info
->prev_chg_type
;
287 info
->prev_chg_type
= MAX77843_MUIC_CHG_NONE
;
290 cable_type
= info
->prev_chg_type
= chg_type
;
293 case MAX77843_CABLE_GROUP_ADC_GND
:
294 if (adc
== MAX77843_MUIC_ADC_OPEN
) {
296 cable_type
= info
->prev_gnd_type
;
297 info
->prev_gnd_type
= MAX77843_MUIC_ADC_OPEN
;
301 /* Offset|ADC1K|VBVolt|
302 * 0x1| 0| 0| USB-HOST
303 * 0x1| 0| 1| USB-HOST with VB
305 * 0x1| 1| 1| MHL with VB */
306 /* Get ADC1K register bit */
307 gnd_type
= (info
->status
[MAX77843_MUIC_STATUS1
] &
308 MAX77843_MUIC_STATUS1_ADC1K_MASK
);
310 /* Get VBVolt register bit */
311 gnd_type
|= (info
->status
[MAX77843_MUIC_STATUS2
] &
312 MAX77843_MUIC_STATUS2_VBVOLT_MASK
);
313 gnd_type
>>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT
;
315 /* Offset of GND cable */
316 gnd_type
|= MAX77843_MUIC_GND_USB_HOST
;
317 cable_type
= info
->prev_gnd_type
= gnd_type
;
321 dev_err(info
->dev
, "Unknown cable group (%d)\n", group
);
322 cable_type
= -EINVAL
;
329 static int max77843_muic_adc_gnd_handler(struct max77843_muic_info
*info
)
331 int ret
, gnd_cable_type
;
334 gnd_cable_type
= max77843_muic_get_cable_type(info
,
335 MAX77843_CABLE_GROUP_ADC_GND
, &attached
);
336 dev_dbg(info
->dev
, "external connector is %s (gnd:0x%02x)\n",
337 attached
? "attached" : "detached", gnd_cable_type
);
339 switch (gnd_cable_type
) {
340 case MAX77843_MUIC_GND_USB_HOST
:
341 case MAX77843_MUIC_GND_USB_HOST_VB
:
342 ret
= max77843_muic_set_path(info
,
343 MAX77843_MUIC_CONTROL1_SW_USB
,
348 extcon_set_cable_state_(info
->edev
, EXTCON_USB_HOST
, attached
);
350 case MAX77843_MUIC_GND_MHL_VB
:
351 case MAX77843_MUIC_GND_MHL
:
352 ret
= max77843_muic_set_path(info
,
353 MAX77843_MUIC_CONTROL1_SW_OPEN
,
358 extcon_set_cable_state_(info
->edev
, EXTCON_DISP_MHL
, attached
);
361 dev_err(info
->dev
, "failed to detect %s accessory(gnd:0x%x)\n",
362 attached
? "attached" : "detached", gnd_cable_type
);
369 static int max77843_muic_jig_handler(struct max77843_muic_info
*info
,
370 int cable_type
, bool attached
)
373 u8 path
= MAX77843_MUIC_CONTROL1_SW_OPEN
;
375 dev_dbg(info
->dev
, "external connector is %s (adc:0x%02x)\n",
376 attached
? "attached" : "detached", cable_type
);
378 switch (cable_type
) {
379 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF
:
380 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON
:
381 path
= MAX77843_MUIC_CONTROL1_SW_USB
;
383 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF
:
384 path
= MAX77843_MUIC_CONTROL1_SW_UART
;
390 ret
= max77843_muic_set_path(info
, path
, attached
);
394 extcon_set_cable_state_(info
->edev
, EXTCON_JIG
, attached
);
399 static int max77843_muic_adc_handler(struct max77843_muic_info
*info
)
404 cable_type
= max77843_muic_get_cable_type(info
,
405 MAX77843_CABLE_GROUP_ADC
, &attached
);
408 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
409 attached
? "attached" : "detached", cable_type
,
410 info
->prev_cable_type
);
412 switch (cable_type
) {
413 case MAX77843_MUIC_ADC_GROUND
:
414 ret
= max77843_muic_adc_gnd_handler(info
);
418 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF
:
419 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON
:
420 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF
:
421 ret
= max77843_muic_jig_handler(info
, cable_type
, attached
);
425 case MAX77843_MUIC_ADC_SEND_END_BUTTON
:
426 case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON
:
427 case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON
:
428 case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON
:
429 case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON
:
430 case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON
:
431 case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON
:
432 case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON
:
433 case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON
:
434 case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON
:
435 case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON
:
436 case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON
:
437 case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON
:
438 case MAX77843_MUIC_ADC_RESERVED_ACC_1
:
439 case MAX77843_MUIC_ADC_RESERVED_ACC_2
:
440 case MAX77843_MUIC_ADC_RESERVED_ACC_3
:
441 case MAX77843_MUIC_ADC_RESERVED_ACC_4
:
442 case MAX77843_MUIC_ADC_RESERVED_ACC_5
:
443 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2
:
444 case MAX77843_MUIC_ADC_PHONE_POWERED_DEV
:
445 case MAX77843_MUIC_ADC_TTY_CONVERTER
:
446 case MAX77843_MUIC_ADC_UART_CABLE
:
447 case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG
:
448 case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD
:
449 case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG
:
450 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON
:
451 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1
:
452 case MAX77843_MUIC_ADC_OPEN
:
454 "accessory is %s but it isn't used (adc:0x%x)\n",
455 attached
? "attached" : "detached", cable_type
);
459 "failed to detect %s accessory (adc:0x%x)\n",
460 attached
? "attached" : "detached", cable_type
);
467 static int max77843_muic_chg_handler(struct max77843_muic_info
*info
)
469 int ret
, chg_type
, gnd_type
;
472 chg_type
= max77843_muic_get_cable_type(info
,
473 MAX77843_CABLE_GROUP_CHG
, &attached
);
476 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
477 attached
? "attached" : "detached",
478 chg_type
, info
->prev_chg_type
);
481 case MAX77843_MUIC_CHG_USB
:
482 ret
= max77843_muic_set_path(info
,
483 MAX77843_MUIC_CONTROL1_SW_USB
,
488 extcon_set_cable_state_(info
->edev
, EXTCON_USB
, attached
);
490 case MAX77843_MUIC_CHG_DOWNSTREAM
:
491 ret
= max77843_muic_set_path(info
,
492 MAX77843_MUIC_CONTROL1_SW_OPEN
,
497 extcon_set_cable_state_(info
->edev
, EXTCON_CHG_USB_CDP
,
500 case MAX77843_MUIC_CHG_DEDICATED
:
501 ret
= max77843_muic_set_path(info
,
502 MAX77843_MUIC_CONTROL1_SW_OPEN
,
507 extcon_set_cable_state_(info
->edev
, EXTCON_CHG_USB_DCP
,
510 case MAX77843_MUIC_CHG_SPECIAL_500MA
:
511 ret
= max77843_muic_set_path(info
,
512 MAX77843_MUIC_CONTROL1_SW_OPEN
,
517 extcon_set_cable_state_(info
->edev
, EXTCON_CHG_USB_SLOW
,
520 case MAX77843_MUIC_CHG_SPECIAL_1A
:
521 ret
= max77843_muic_set_path(info
,
522 MAX77843_MUIC_CONTROL1_SW_OPEN
,
527 extcon_set_cable_state_(info
->edev
, EXTCON_CHG_USB_FAST
,
530 case MAX77843_MUIC_CHG_GND
:
531 gnd_type
= max77843_muic_get_cable_type(info
,
532 MAX77843_CABLE_GROUP_ADC_GND
, &attached
);
534 /* Charger cable on MHL accessory is attach or detach */
535 if (gnd_type
== MAX77843_MUIC_GND_MHL_VB
)
536 extcon_set_cable_state_(info
->edev
, EXTCON_CHG_USB_DCP
,
538 else if (gnd_type
== MAX77843_MUIC_GND_MHL
)
539 extcon_set_cable_state_(info
->edev
, EXTCON_CHG_USB_DCP
,
542 case MAX77843_MUIC_CHG_NONE
:
546 "failed to detect %s accessory (chg_type:0x%x)\n",
547 attached
? "attached" : "detached", chg_type
);
549 max77843_muic_set_path(info
, MAX77843_MUIC_CONTROL1_SW_OPEN
,
557 static void max77843_muic_irq_work(struct work_struct
*work
)
559 struct max77843_muic_info
*info
= container_of(work
,
560 struct max77843_muic_info
, irq_work
);
561 struct max77693_dev
*max77843
= info
->max77843
;
564 mutex_lock(&info
->mutex
);
566 ret
= regmap_bulk_read(max77843
->regmap_muic
,
567 MAX77843_MUIC_REG_STATUS1
, info
->status
,
568 MAX77843_MUIC_STATUS_NUM
);
570 dev_err(info
->dev
, "Cannot read STATUS registers\n");
571 mutex_unlock(&info
->mutex
);
576 ret
= max77843_muic_adc_handler(info
);
578 dev_err(info
->dev
, "Unknown cable type\n");
579 info
->irq_adc
= false;
583 ret
= max77843_muic_chg_handler(info
);
585 dev_err(info
->dev
, "Unknown charger type\n");
586 info
->irq_chg
= false;
589 mutex_unlock(&info
->mutex
);
592 static irqreturn_t
max77843_muic_irq_handler(int irq
, void *data
)
594 struct max77843_muic_info
*info
= data
;
595 int i
, irq_type
= -1;
597 for (i
= 0; i
< ARRAY_SIZE(max77843_muic_irqs
); i
++)
598 if (irq
== max77843_muic_irqs
[i
].virq
)
599 irq_type
= max77843_muic_irqs
[i
].irq
;
602 case MAX77843_MUIC_IRQ_INT1_ADC
:
603 case MAX77843_MUIC_IRQ_INT1_ADCERROR
:
604 case MAX77843_MUIC_IRQ_INT1_ADC1K
:
605 info
->irq_adc
= true;
607 case MAX77843_MUIC_IRQ_INT2_CHGTYP
:
608 case MAX77843_MUIC_IRQ_INT2_CHGDETRUN
:
609 case MAX77843_MUIC_IRQ_INT2_DCDTMR
:
610 case MAX77843_MUIC_IRQ_INT2_DXOVP
:
611 case MAX77843_MUIC_IRQ_INT2_VBVOLT
:
612 info
->irq_chg
= true;
614 case MAX77843_MUIC_IRQ_INT3_VBADC
:
615 case MAX77843_MUIC_IRQ_INT3_VDNMON
:
616 case MAX77843_MUIC_IRQ_INT3_DNRES
:
617 case MAX77843_MUIC_IRQ_INT3_MPNACK
:
618 case MAX77843_MUIC_IRQ_INT3_MRXBUFOW
:
619 case MAX77843_MUIC_IRQ_INT3_MRXTRF
:
620 case MAX77843_MUIC_IRQ_INT3_MRXPERR
:
621 case MAX77843_MUIC_IRQ_INT3_MRXRDY
:
624 dev_err(info
->dev
, "Cannot recognize IRQ(%d)\n", irq_type
);
628 schedule_work(&info
->irq_work
);
633 static void max77843_muic_detect_cable_wq(struct work_struct
*work
)
635 struct max77843_muic_info
*info
= container_of(to_delayed_work(work
),
636 struct max77843_muic_info
, wq_detcable
);
637 struct max77693_dev
*max77843
= info
->max77843
;
638 int chg_type
, adc
, ret
;
641 mutex_lock(&info
->mutex
);
643 ret
= regmap_bulk_read(max77843
->regmap_muic
,
644 MAX77843_MUIC_REG_STATUS1
, info
->status
,
645 MAX77843_MUIC_STATUS_NUM
);
647 dev_err(info
->dev
, "Cannot read STATUS registers\n");
651 adc
= max77843_muic_get_cable_type(info
,
652 MAX77843_CABLE_GROUP_ADC
, &attached
);
653 if (attached
&& adc
!= MAX77843_MUIC_ADC_OPEN
) {
654 ret
= max77843_muic_adc_handler(info
);
656 dev_err(info
->dev
, "Cannot detect accessory\n");
661 chg_type
= max77843_muic_get_cable_type(info
,
662 MAX77843_CABLE_GROUP_CHG
, &attached
);
663 if (attached
&& chg_type
!= MAX77843_MUIC_CHG_NONE
) {
664 ret
= max77843_muic_chg_handler(info
);
666 dev_err(info
->dev
, "Cannot detect charger accessory\n");
672 mutex_unlock(&info
->mutex
);
675 static int max77843_muic_set_debounce_time(struct max77843_muic_info
*info
,
676 enum max77843_muic_adc_debounce_time time
)
678 struct max77693_dev
*max77843
= info
->max77843
;
682 case MAX77843_DEBOUNCE_TIME_5MS
:
683 case MAX77843_DEBOUNCE_TIME_10MS
:
684 case MAX77843_DEBOUNCE_TIME_25MS
:
685 case MAX77843_DEBOUNCE_TIME_38_62MS
:
686 ret
= regmap_update_bits(max77843
->regmap_muic
,
687 MAX77843_MUIC_REG_CONTROL4
,
688 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK
,
689 time
<< MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT
);
691 dev_err(info
->dev
, "Cannot write MUIC regmap\n");
696 dev_err(info
->dev
, "Invalid ADC debounce time\n");
703 static int max77843_init_muic_regmap(struct max77693_dev
*max77843
)
707 max77843
->i2c_muic
= i2c_new_dummy(max77843
->i2c
->adapter
,
709 if (!max77843
->i2c_muic
) {
710 dev_err(&max77843
->i2c
->dev
,
711 "Cannot allocate I2C device for MUIC\n");
715 i2c_set_clientdata(max77843
->i2c_muic
, max77843
);
717 max77843
->regmap_muic
= devm_regmap_init_i2c(max77843
->i2c_muic
,
718 &max77843_muic_regmap_config
);
719 if (IS_ERR(max77843
->regmap_muic
)) {
720 ret
= PTR_ERR(max77843
->regmap_muic
);
724 ret
= regmap_add_irq_chip(max77843
->regmap_muic
, max77843
->irq
,
725 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
| IRQF_SHARED
,
726 0, &max77843_muic_irq_chip
, &max77843
->irq_data_muic
);
728 dev_err(&max77843
->i2c
->dev
, "Cannot add MUIC IRQ chip\n");
735 i2c_unregister_device(max77843
->i2c_muic
);
740 static int max77843_muic_probe(struct platform_device
*pdev
)
742 struct max77693_dev
*max77843
= dev_get_drvdata(pdev
->dev
.parent
);
743 struct max77843_muic_info
*info
;
747 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
751 info
->dev
= &pdev
->dev
;
752 info
->max77843
= max77843
;
754 platform_set_drvdata(pdev
, info
);
755 mutex_init(&info
->mutex
);
757 /* Initialize i2c and regmap */
758 ret
= max77843_init_muic_regmap(max77843
);
760 dev_err(&pdev
->dev
, "Failed to init MUIC regmap\n");
764 /* Turn off auto detection configuration */
765 ret
= regmap_update_bits(max77843
->regmap_muic
,
766 MAX77843_MUIC_REG_CONTROL4
,
767 MAX77843_MUIC_CONTROL4_USBAUTO_MASK
|
768 MAX77843_MUIC_CONTROL4_FCTAUTO_MASK
,
769 CONTROL4_AUTO_DISABLE
);
771 /* Initialize extcon device */
772 info
->edev
= devm_extcon_dev_allocate(&pdev
->dev
,
773 max77843_extcon_cable
);
774 if (IS_ERR(info
->edev
)) {
775 dev_err(&pdev
->dev
, "Failed to allocate memory for extcon\n");
780 ret
= devm_extcon_dev_register(&pdev
->dev
, info
->edev
);
782 dev_err(&pdev
->dev
, "Failed to register extcon device\n");
786 /* Set ADC debounce time */
787 max77843_muic_set_debounce_time(info
, MAX77843_DEBOUNCE_TIME_25MS
);
789 /* Set initial path for UART */
790 max77843_muic_set_path(info
, MAX77843_MUIC_CONTROL1_SW_UART
, true);
792 /* Check revision number of MUIC device */
793 ret
= regmap_read(max77843
->regmap_muic
, MAX77843_MUIC_REG_ID
, &id
);
795 dev_err(&pdev
->dev
, "Failed to read revision number\n");
798 dev_info(info
->dev
, "MUIC device ID : 0x%x\n", id
);
800 /* Support virtual irq domain for max77843 MUIC device */
801 INIT_WORK(&info
->irq_work
, max77843_muic_irq_work
);
803 /* Clear IRQ bits before request IRQs */
804 ret
= regmap_bulk_read(max77843
->regmap_muic
,
805 MAX77843_MUIC_REG_INT1
, info
->status
,
806 MAX77843_MUIC_STATUS_NUM
);
808 dev_err(&pdev
->dev
, "Failed to Clear IRQ bits\n");
812 for (i
= 0; i
< ARRAY_SIZE(max77843_muic_irqs
); i
++) {
813 struct max77843_muic_irq
*muic_irq
= &max77843_muic_irqs
[i
];
814 unsigned int virq
= 0;
816 virq
= regmap_irq_get_virq(max77843
->irq_data_muic
,
822 muic_irq
->virq
= virq
;
824 ret
= devm_request_threaded_irq(&pdev
->dev
, virq
, NULL
,
825 max77843_muic_irq_handler
, IRQF_NO_SUSPEND
,
826 muic_irq
->name
, info
);
829 "Failed to request irq (IRQ: %d, error: %d)\n",
835 /* Detect accessory after completing the initialization of platform */
836 INIT_DELAYED_WORK(&info
->wq_detcable
, max77843_muic_detect_cable_wq
);
837 queue_delayed_work(system_power_efficient_wq
,
838 &info
->wq_detcable
, msecs_to_jiffies(DELAY_MS_DEFAULT
));
843 regmap_del_irq_chip(max77843
->irq
, max77843
->irq_data_muic
);
844 i2c_unregister_device(max77843
->i2c_muic
);
849 static int max77843_muic_remove(struct platform_device
*pdev
)
851 struct max77843_muic_info
*info
= platform_get_drvdata(pdev
);
852 struct max77693_dev
*max77843
= info
->max77843
;
854 cancel_work_sync(&info
->irq_work
);
855 regmap_del_irq_chip(max77843
->irq
, max77843
->irq_data_muic
);
856 i2c_unregister_device(max77843
->i2c_muic
);
861 static const struct platform_device_id max77843_muic_id
[] = {
862 { "max77843-muic", },
865 MODULE_DEVICE_TABLE(platform
, max77843_muic_id
);
867 static struct platform_driver max77843_muic_driver
= {
869 .name
= "max77843-muic",
871 .probe
= max77843_muic_probe
,
872 .remove
= max77843_muic_remove
,
873 .id_table
= max77843_muic_id
,
876 static int __init
max77843_muic_init(void)
878 return platform_driver_register(&max77843_muic_driver
);
880 subsys_initcall(max77843_muic_init
);
882 MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
883 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
884 MODULE_LICENSE("GPL");