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
[] = {
126 EXTCON_CHARGE_DOWNSTREAM
,
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_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_CHARGE_DOWNSTREAM
,
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_TA
, attached
);
509 case MAX77843_MUIC_CHG_SPECIAL_500MA
:
510 ret
= max77843_muic_set_path(info
,
511 MAX77843_MUIC_CONTROL1_SW_OPEN
,
516 extcon_set_cable_state_(info
->edev
, EXTCON_SLOW_CHARGER
,
519 case MAX77843_MUIC_CHG_SPECIAL_1A
:
520 ret
= max77843_muic_set_path(info
,
521 MAX77843_MUIC_CONTROL1_SW_OPEN
,
526 extcon_set_cable_state_(info
->edev
, EXTCON_FAST_CHARGER
,
529 case MAX77843_MUIC_CHG_GND
:
530 gnd_type
= max77843_muic_get_cable_type(info
,
531 MAX77843_CABLE_GROUP_ADC_GND
, &attached
);
533 /* Charger cable on MHL accessory is attach or detach */
534 if (gnd_type
== MAX77843_MUIC_GND_MHL_VB
)
535 extcon_set_cable_state_(info
->edev
, EXTCON_TA
, true);
536 else if (gnd_type
== MAX77843_MUIC_GND_MHL
)
537 extcon_set_cable_state_(info
->edev
, EXTCON_TA
, false);
539 case MAX77843_MUIC_CHG_NONE
:
543 "failed to detect %s accessory (chg_type:0x%x)\n",
544 attached
? "attached" : "detached", chg_type
);
546 max77843_muic_set_path(info
, MAX77843_MUIC_CONTROL1_SW_OPEN
,
554 static void max77843_muic_irq_work(struct work_struct
*work
)
556 struct max77843_muic_info
*info
= container_of(work
,
557 struct max77843_muic_info
, irq_work
);
558 struct max77693_dev
*max77843
= info
->max77843
;
561 mutex_lock(&info
->mutex
);
563 ret
= regmap_bulk_read(max77843
->regmap_muic
,
564 MAX77843_MUIC_REG_STATUS1
, info
->status
,
565 MAX77843_MUIC_STATUS_NUM
);
567 dev_err(info
->dev
, "Cannot read STATUS registers\n");
568 mutex_unlock(&info
->mutex
);
573 ret
= max77843_muic_adc_handler(info
);
575 dev_err(info
->dev
, "Unknown cable type\n");
576 info
->irq_adc
= false;
580 ret
= max77843_muic_chg_handler(info
);
582 dev_err(info
->dev
, "Unknown charger type\n");
583 info
->irq_chg
= false;
586 mutex_unlock(&info
->mutex
);
589 static irqreturn_t
max77843_muic_irq_handler(int irq
, void *data
)
591 struct max77843_muic_info
*info
= data
;
592 int i
, irq_type
= -1;
594 for (i
= 0; i
< ARRAY_SIZE(max77843_muic_irqs
); i
++)
595 if (irq
== max77843_muic_irqs
[i
].virq
)
596 irq_type
= max77843_muic_irqs
[i
].irq
;
599 case MAX77843_MUIC_IRQ_INT1_ADC
:
600 case MAX77843_MUIC_IRQ_INT1_ADCERROR
:
601 case MAX77843_MUIC_IRQ_INT1_ADC1K
:
602 info
->irq_adc
= true;
604 case MAX77843_MUIC_IRQ_INT2_CHGTYP
:
605 case MAX77843_MUIC_IRQ_INT2_CHGDETRUN
:
606 case MAX77843_MUIC_IRQ_INT2_DCDTMR
:
607 case MAX77843_MUIC_IRQ_INT2_DXOVP
:
608 case MAX77843_MUIC_IRQ_INT2_VBVOLT
:
609 info
->irq_chg
= true;
611 case MAX77843_MUIC_IRQ_INT3_VBADC
:
612 case MAX77843_MUIC_IRQ_INT3_VDNMON
:
613 case MAX77843_MUIC_IRQ_INT3_DNRES
:
614 case MAX77843_MUIC_IRQ_INT3_MPNACK
:
615 case MAX77843_MUIC_IRQ_INT3_MRXBUFOW
:
616 case MAX77843_MUIC_IRQ_INT3_MRXTRF
:
617 case MAX77843_MUIC_IRQ_INT3_MRXPERR
:
618 case MAX77843_MUIC_IRQ_INT3_MRXRDY
:
621 dev_err(info
->dev
, "Cannot recognize IRQ(%d)\n", irq_type
);
625 schedule_work(&info
->irq_work
);
630 static void max77843_muic_detect_cable_wq(struct work_struct
*work
)
632 struct max77843_muic_info
*info
= container_of(to_delayed_work(work
),
633 struct max77843_muic_info
, wq_detcable
);
634 struct max77693_dev
*max77843
= info
->max77843
;
635 int chg_type
, adc
, ret
;
638 mutex_lock(&info
->mutex
);
640 ret
= regmap_bulk_read(max77843
->regmap_muic
,
641 MAX77843_MUIC_REG_STATUS1
, info
->status
,
642 MAX77843_MUIC_STATUS_NUM
);
644 dev_err(info
->dev
, "Cannot read STATUS registers\n");
648 adc
= max77843_muic_get_cable_type(info
,
649 MAX77843_CABLE_GROUP_ADC
, &attached
);
650 if (attached
&& adc
!= MAX77843_MUIC_ADC_OPEN
) {
651 ret
= max77843_muic_adc_handler(info
);
653 dev_err(info
->dev
, "Cannot detect accessory\n");
658 chg_type
= max77843_muic_get_cable_type(info
,
659 MAX77843_CABLE_GROUP_CHG
, &attached
);
660 if (attached
&& chg_type
!= MAX77843_MUIC_CHG_NONE
) {
661 ret
= max77843_muic_chg_handler(info
);
663 dev_err(info
->dev
, "Cannot detect charger accessory\n");
669 mutex_unlock(&info
->mutex
);
672 static int max77843_muic_set_debounce_time(struct max77843_muic_info
*info
,
673 enum max77843_muic_adc_debounce_time time
)
675 struct max77693_dev
*max77843
= info
->max77843
;
679 case MAX77843_DEBOUNCE_TIME_5MS
:
680 case MAX77843_DEBOUNCE_TIME_10MS
:
681 case MAX77843_DEBOUNCE_TIME_25MS
:
682 case MAX77843_DEBOUNCE_TIME_38_62MS
:
683 ret
= regmap_update_bits(max77843
->regmap_muic
,
684 MAX77843_MUIC_REG_CONTROL4
,
685 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK
,
686 time
<< MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT
);
688 dev_err(info
->dev
, "Cannot write MUIC regmap\n");
693 dev_err(info
->dev
, "Invalid ADC debounce time\n");
700 static int max77843_init_muic_regmap(struct max77693_dev
*max77843
)
704 max77843
->i2c_muic
= i2c_new_dummy(max77843
->i2c
->adapter
,
706 if (!max77843
->i2c_muic
) {
707 dev_err(&max77843
->i2c
->dev
,
708 "Cannot allocate I2C device for MUIC\n");
712 i2c_set_clientdata(max77843
->i2c_muic
, max77843
);
714 max77843
->regmap_muic
= devm_regmap_init_i2c(max77843
->i2c_muic
,
715 &max77843_muic_regmap_config
);
716 if (IS_ERR(max77843
->regmap_muic
)) {
717 ret
= PTR_ERR(max77843
->regmap_muic
);
721 ret
= regmap_add_irq_chip(max77843
->regmap_muic
, max77843
->irq
,
722 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
| IRQF_SHARED
,
723 0, &max77843_muic_irq_chip
, &max77843
->irq_data_muic
);
725 dev_err(&max77843
->i2c
->dev
, "Cannot add MUIC IRQ chip\n");
732 i2c_unregister_device(max77843
->i2c_muic
);
737 static int max77843_muic_probe(struct platform_device
*pdev
)
739 struct max77693_dev
*max77843
= dev_get_drvdata(pdev
->dev
.parent
);
740 struct max77843_muic_info
*info
;
744 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
748 info
->dev
= &pdev
->dev
;
749 info
->max77843
= max77843
;
751 platform_set_drvdata(pdev
, info
);
752 mutex_init(&info
->mutex
);
754 /* Initialize i2c and regmap */
755 ret
= max77843_init_muic_regmap(max77843
);
757 dev_err(&pdev
->dev
, "Failed to init MUIC regmap\n");
761 /* Turn off auto detection configuration */
762 ret
= regmap_update_bits(max77843
->regmap_muic
,
763 MAX77843_MUIC_REG_CONTROL4
,
764 MAX77843_MUIC_CONTROL4_USBAUTO_MASK
|
765 MAX77843_MUIC_CONTROL4_FCTAUTO_MASK
,
766 CONTROL4_AUTO_DISABLE
);
768 /* Initialize extcon device */
769 info
->edev
= devm_extcon_dev_allocate(&pdev
->dev
,
770 max77843_extcon_cable
);
771 if (IS_ERR(info
->edev
)) {
772 dev_err(&pdev
->dev
, "Failed to allocate memory for extcon\n");
777 ret
= devm_extcon_dev_register(&pdev
->dev
, info
->edev
);
779 dev_err(&pdev
->dev
, "Failed to register extcon device\n");
783 /* Set ADC debounce time */
784 max77843_muic_set_debounce_time(info
, MAX77843_DEBOUNCE_TIME_25MS
);
786 /* Set initial path for UART */
787 max77843_muic_set_path(info
, MAX77843_MUIC_CONTROL1_SW_UART
, true);
789 /* Check revision number of MUIC device */
790 ret
= regmap_read(max77843
->regmap_muic
, MAX77843_MUIC_REG_ID
, &id
);
792 dev_err(&pdev
->dev
, "Failed to read revision number\n");
795 dev_info(info
->dev
, "MUIC device ID : 0x%x\n", id
);
797 /* Support virtual irq domain for max77843 MUIC device */
798 INIT_WORK(&info
->irq_work
, max77843_muic_irq_work
);
800 /* Clear IRQ bits before request IRQs */
801 ret
= regmap_bulk_read(max77843
->regmap_muic
,
802 MAX77843_MUIC_REG_INT1
, info
->status
,
803 MAX77843_MUIC_IRQ_NUM
);
805 dev_err(&pdev
->dev
, "Failed to Clear IRQ bits\n");
809 for (i
= 0; i
< ARRAY_SIZE(max77843_muic_irqs
); i
++) {
810 struct max77843_muic_irq
*muic_irq
= &max77843_muic_irqs
[i
];
811 unsigned int virq
= 0;
813 virq
= regmap_irq_get_virq(max77843
->irq_data_muic
,
819 muic_irq
->virq
= virq
;
821 ret
= devm_request_threaded_irq(&pdev
->dev
, virq
, NULL
,
822 max77843_muic_irq_handler
, IRQF_NO_SUSPEND
,
823 muic_irq
->name
, info
);
826 "Failed to request irq (IRQ: %d, error: %d)\n",
832 /* Detect accessory after completing the initialization of platform */
833 INIT_DELAYED_WORK(&info
->wq_detcable
, max77843_muic_detect_cable_wq
);
834 queue_delayed_work(system_power_efficient_wq
,
835 &info
->wq_detcable
, msecs_to_jiffies(DELAY_MS_DEFAULT
));
840 regmap_del_irq_chip(max77843
->irq
, max77843
->irq_data_muic
);
841 i2c_unregister_device(max77843
->i2c_muic
);
846 static int max77843_muic_remove(struct platform_device
*pdev
)
848 struct max77843_muic_info
*info
= platform_get_drvdata(pdev
);
849 struct max77693_dev
*max77843
= info
->max77843
;
851 cancel_work_sync(&info
->irq_work
);
852 regmap_del_irq_chip(max77843
->irq
, max77843
->irq_data_muic
);
853 i2c_unregister_device(max77843
->i2c_muic
);
858 static const struct platform_device_id max77843_muic_id
[] = {
859 { "max77843-muic", },
862 MODULE_DEVICE_TABLE(platform
, max77843_muic_id
);
864 static struct platform_driver max77843_muic_driver
= {
866 .name
= "max77843-muic",
868 .probe
= max77843_muic_probe
,
869 .remove
= max77843_muic_remove
,
870 .id_table
= max77843_muic_id
,
873 static int __init
max77843_muic_init(void)
875 return platform_driver_register(&max77843_muic_driver
);
877 subsys_initcall(max77843_muic_init
);
879 MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
880 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
881 MODULE_LICENSE("GPL");