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/max77843-private.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/workqueue.h>
23 #define DELAY_MS_DEFAULT 15000 /* unit: millisecond */
25 enum max77843_muic_status
{
26 MAX77843_MUIC_STATUS1
= 0,
27 MAX77843_MUIC_STATUS2
,
28 MAX77843_MUIC_STATUS3
,
30 MAX77843_MUIC_STATUS_NUM
,
33 struct max77843_muic_info
{
35 struct max77843
*max77843
;
36 struct extcon_dev
*edev
;
39 struct work_struct irq_work
;
40 struct delayed_work wq_detcable
;
42 u8 status
[MAX77843_MUIC_STATUS_NUM
];
51 enum max77843_muic_cable_group
{
52 MAX77843_CABLE_GROUP_ADC
= 0,
53 MAX77843_CABLE_GROUP_ADC_GND
,
54 MAX77843_CABLE_GROUP_CHG
,
57 enum max77843_muic_adc_debounce_time
{
58 MAX77843_DEBOUNCE_TIME_5MS
= 0,
59 MAX77843_DEBOUNCE_TIME_10MS
,
60 MAX77843_DEBOUNCE_TIME_25MS
,
61 MAX77843_DEBOUNCE_TIME_38_62MS
,
64 /* Define accessory cable type */
65 enum max77843_muic_accessory_type
{
66 MAX77843_MUIC_ADC_GROUND
= 0,
67 MAX77843_MUIC_ADC_SEND_END_BUTTON
,
68 MAX77843_MUIC_ADC_REMOTE_S1_BUTTON
,
69 MAX77843_MUIC_ADC_REMOTE_S2_BUTTON
,
70 MAX77843_MUIC_ADC_REMOTE_S3_BUTTON
,
71 MAX77843_MUIC_ADC_REMOTE_S4_BUTTON
,
72 MAX77843_MUIC_ADC_REMOTE_S5_BUTTON
,
73 MAX77843_MUIC_ADC_REMOTE_S6_BUTTON
,
74 MAX77843_MUIC_ADC_REMOTE_S7_BUTTON
,
75 MAX77843_MUIC_ADC_REMOTE_S8_BUTTON
,
76 MAX77843_MUIC_ADC_REMOTE_S9_BUTTON
,
77 MAX77843_MUIC_ADC_REMOTE_S10_BUTTON
,
78 MAX77843_MUIC_ADC_REMOTE_S11_BUTTON
,
79 MAX77843_MUIC_ADC_REMOTE_S12_BUTTON
,
80 MAX77843_MUIC_ADC_RESERVED_ACC_1
,
81 MAX77843_MUIC_ADC_RESERVED_ACC_2
,
82 MAX77843_MUIC_ADC_RESERVED_ACC_3
,
83 MAX77843_MUIC_ADC_RESERVED_ACC_4
,
84 MAX77843_MUIC_ADC_RESERVED_ACC_5
,
85 MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2
,
86 MAX77843_MUIC_ADC_PHONE_POWERED_DEV
,
87 MAX77843_MUIC_ADC_TTY_CONVERTER
,
88 MAX77843_MUIC_ADC_UART_CABLE
,
89 MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG
,
90 MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF
,
91 MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON
,
92 MAX77843_MUIC_ADC_AV_CABLE_NOLOAD
,
93 MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG
,
94 MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF
,
95 MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON
,
96 MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1
,
97 MAX77843_MUIC_ADC_OPEN
,
99 /* The blow accessories should check
100 not only ADC value but also ADC1K and VBVolt value. */
101 /* Offset|ADC1K|VBVolt| */
102 MAX77843_MUIC_GND_USB_HOST
= 0x100, /* 0x1| 0| 0| */
103 MAX77843_MUIC_GND_USB_HOST_VB
= 0x101, /* 0x1| 0| 1| */
104 MAX77843_MUIC_GND_MHL
= 0x102, /* 0x1| 1| 0| */
105 MAX77843_MUIC_GND_MHL_VB
= 0x103, /* 0x1| 1| 1| */
108 /* Define charger cable type */
109 enum max77843_muic_charger_type
{
110 MAX77843_MUIC_CHG_NONE
= 0,
111 MAX77843_MUIC_CHG_USB
,
112 MAX77843_MUIC_CHG_DOWNSTREAM
,
113 MAX77843_MUIC_CHG_DEDICATED
,
114 MAX77843_MUIC_CHG_SPECIAL_500MA
,
115 MAX77843_MUIC_CHG_SPECIAL_1A
,
116 MAX77843_MUIC_CHG_SPECIAL_BIAS
,
117 MAX77843_MUIC_CHG_RESERVED
,
118 MAX77843_MUIC_CHG_GND
,
121 static const enum extcon max77843_extcon_cable
[] = {
125 EXTCON_CHARGE_DOWNSTREAM
,
133 struct max77843_muic_irq
{
139 static struct max77843_muic_irq max77843_muic_irqs
[] = {
140 { MAX77843_MUIC_IRQ_INT1_ADC
, "MUIC-ADC" },
141 { MAX77843_MUIC_IRQ_INT1_ADCERROR
, "MUIC-ADC_ERROR" },
142 { MAX77843_MUIC_IRQ_INT1_ADC1K
, "MUIC-ADC1K" },
143 { MAX77843_MUIC_IRQ_INT2_CHGTYP
, "MUIC-CHGTYP" },
144 { MAX77843_MUIC_IRQ_INT2_CHGDETRUN
, "MUIC-CHGDETRUN" },
145 { MAX77843_MUIC_IRQ_INT2_DCDTMR
, "MUIC-DCDTMR" },
146 { MAX77843_MUIC_IRQ_INT2_DXOVP
, "MUIC-DXOVP" },
147 { MAX77843_MUIC_IRQ_INT2_VBVOLT
, "MUIC-VBVOLT" },
148 { MAX77843_MUIC_IRQ_INT3_VBADC
, "MUIC-VBADC" },
149 { MAX77843_MUIC_IRQ_INT3_VDNMON
, "MUIC-VDNMON" },
150 { MAX77843_MUIC_IRQ_INT3_DNRES
, "MUIC-DNRES" },
151 { MAX77843_MUIC_IRQ_INT3_MPNACK
, "MUIC-MPNACK"},
152 { MAX77843_MUIC_IRQ_INT3_MRXBUFOW
, "MUIC-MRXBUFOW"},
153 { MAX77843_MUIC_IRQ_INT3_MRXTRF
, "MUIC-MRXTRF"},
154 { MAX77843_MUIC_IRQ_INT3_MRXPERR
, "MUIC-MRXPERR"},
155 { MAX77843_MUIC_IRQ_INT3_MRXRDY
, "MUIC-MRXRDY"},
158 static const struct regmap_config max77843_muic_regmap_config
= {
161 .max_register
= MAX77843_MUIC_REG_END
,
164 static const struct regmap_irq max77843_muic_irq
[] = {
166 { .reg_offset
= 0, .mask
= MAX77843_MUIC_ADC
, },
167 { .reg_offset
= 0, .mask
= MAX77843_MUIC_ADCERROR
, },
168 { .reg_offset
= 0, .mask
= MAX77843_MUIC_ADC1K
, },
171 { .reg_offset
= 1, .mask
= MAX77843_MUIC_CHGTYP
, },
172 { .reg_offset
= 1, .mask
= MAX77843_MUIC_CHGDETRUN
, },
173 { .reg_offset
= 1, .mask
= MAX77843_MUIC_DCDTMR
, },
174 { .reg_offset
= 1, .mask
= MAX77843_MUIC_DXOVP
, },
175 { .reg_offset
= 1, .mask
= MAX77843_MUIC_VBVOLT
, },
178 { .reg_offset
= 2, .mask
= MAX77843_MUIC_VBADC
, },
179 { .reg_offset
= 2, .mask
= MAX77843_MUIC_VDNMON
, },
180 { .reg_offset
= 2, .mask
= MAX77843_MUIC_DNRES
, },
181 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MPNACK
, },
182 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXBUFOW
, },
183 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXTRF
, },
184 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXPERR
, },
185 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXRDY
, },
188 static const struct regmap_irq_chip max77843_muic_irq_chip
= {
189 .name
= "max77843-muic",
190 .status_base
= MAX77843_MUIC_REG_INT1
,
191 .mask_base
= MAX77843_MUIC_REG_INTMASK1
,
194 .irqs
= max77843_muic_irq
,
195 .num_irqs
= ARRAY_SIZE(max77843_muic_irq
),
198 static int max77843_muic_set_path(struct max77843_muic_info
*info
,
199 u8 val
, bool attached
)
201 struct max77843
*max77843
= info
->max77843
;
203 unsigned int ctrl1
, ctrl2
;
208 ctrl1
= CONTROL1_SW_OPEN
;
210 ret
= regmap_update_bits(max77843
->regmap_muic
,
211 MAX77843_MUIC_REG_CONTROL1
,
212 CONTROL1_COM_SW
, ctrl1
);
214 dev_err(info
->dev
, "Cannot switch MUIC port\n");
219 ctrl2
= MAX77843_MUIC_CONTROL2_CPEN_MASK
;
221 ctrl2
= MAX77843_MUIC_CONTROL2_LOWPWR_MASK
;
223 ret
= regmap_update_bits(max77843
->regmap_muic
,
224 MAX77843_MUIC_REG_CONTROL2
,
225 MAX77843_MUIC_CONTROL2_LOWPWR_MASK
|
226 MAX77843_MUIC_CONTROL2_CPEN_MASK
, ctrl2
);
228 dev_err(info
->dev
, "Cannot update lowpower mode\n");
233 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
234 ctrl1
, ctrl2
, attached
? "attached" : "detached");
239 static int max77843_muic_get_cable_type(struct max77843_muic_info
*info
,
240 enum max77843_muic_cable_group group
, bool *attached
)
242 int adc
, chg_type
, cable_type
, gnd_type
;
244 adc
= info
->status
[MAX77843_MUIC_STATUS1
] &
245 MAX77843_MUIC_STATUS1_ADC_MASK
;
246 adc
>>= STATUS1_ADC_SHIFT
;
249 case MAX77843_CABLE_GROUP_ADC
:
250 if (adc
== MAX77843_MUIC_ADC_OPEN
) {
252 cable_type
= info
->prev_cable_type
;
253 info
->prev_cable_type
= MAX77843_MUIC_ADC_OPEN
;
256 cable_type
= info
->prev_cable_type
= adc
;
259 case MAX77843_CABLE_GROUP_CHG
:
260 chg_type
= info
->status
[MAX77843_MUIC_STATUS2
] &
261 MAX77843_MUIC_STATUS2_CHGTYP_MASK
;
263 /* Check GROUND accessory with charger cable */
264 if (adc
== MAX77843_MUIC_ADC_GROUND
) {
265 if (chg_type
== MAX77843_MUIC_CHG_NONE
) {
266 /* The following state when charger cable is
267 * disconnected but the GROUND accessory still
270 cable_type
= info
->prev_chg_type
;
271 info
->prev_chg_type
= MAX77843_MUIC_CHG_NONE
;
274 /* The following state when charger cable is
275 * connected on the GROUND accessory */
277 cable_type
= MAX77843_MUIC_CHG_GND
;
278 info
->prev_chg_type
= MAX77843_MUIC_CHG_GND
;
283 if (chg_type
== MAX77843_MUIC_CHG_NONE
) {
285 cable_type
= info
->prev_chg_type
;
286 info
->prev_chg_type
= MAX77843_MUIC_CHG_NONE
;
289 cable_type
= info
->prev_chg_type
= chg_type
;
292 case MAX77843_CABLE_GROUP_ADC_GND
:
293 if (adc
== MAX77843_MUIC_ADC_OPEN
) {
295 cable_type
= info
->prev_gnd_type
;
296 info
->prev_gnd_type
= MAX77843_MUIC_ADC_OPEN
;
300 /* Offset|ADC1K|VBVolt|
301 * 0x1| 0| 0| USB-HOST
302 * 0x1| 0| 1| USB-HOST with VB
304 * 0x1| 1| 1| MHL with VB */
305 /* Get ADC1K register bit */
306 gnd_type
= (info
->status
[MAX77843_MUIC_STATUS1
] &
307 MAX77843_MUIC_STATUS1_ADC1K_MASK
);
309 /* Get VBVolt register bit */
310 gnd_type
|= (info
->status
[MAX77843_MUIC_STATUS2
] &
311 MAX77843_MUIC_STATUS2_VBVOLT_MASK
);
312 gnd_type
>>= STATUS2_VBVOLT_SHIFT
;
314 /* Offset of GND cable */
315 gnd_type
|= MAX77843_MUIC_GND_USB_HOST
;
316 cable_type
= info
->prev_gnd_type
= gnd_type
;
320 dev_err(info
->dev
, "Unknown cable group (%d)\n", group
);
321 cable_type
= -EINVAL
;
328 static int max77843_muic_adc_gnd_handler(struct max77843_muic_info
*info
)
330 int ret
, gnd_cable_type
;
333 gnd_cable_type
= max77843_muic_get_cable_type(info
,
334 MAX77843_CABLE_GROUP_ADC_GND
, &attached
);
335 dev_dbg(info
->dev
, "external connector is %s (gnd:0x%02x)\n",
336 attached
? "attached" : "detached", gnd_cable_type
);
338 switch (gnd_cable_type
) {
339 case MAX77843_MUIC_GND_USB_HOST
:
340 case MAX77843_MUIC_GND_USB_HOST_VB
:
341 ret
= max77843_muic_set_path(info
, CONTROL1_SW_USB
, attached
);
345 extcon_set_cable_state_(info
->edev
, EXTCON_USB_HOST
, attached
);
347 case MAX77843_MUIC_GND_MHL_VB
:
348 case MAX77843_MUIC_GND_MHL
:
349 ret
= max77843_muic_set_path(info
, CONTROL1_SW_OPEN
, attached
);
353 extcon_set_cable_state_(info
->edev
, EXTCON_MHL
, attached
);
356 dev_err(info
->dev
, "failed to detect %s accessory(gnd:0x%x)\n",
357 attached
? "attached" : "detached", gnd_cable_type
);
364 static int max77843_muic_jig_handler(struct max77843_muic_info
*info
,
365 int cable_type
, bool attached
)
368 u8 path
= CONTROL1_SW_OPEN
;
370 dev_dbg(info
->dev
, "external connector is %s (adc:0x%02x)\n",
371 attached
? "attached" : "detached", cable_type
);
373 switch (cable_type
) {
374 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF
:
375 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON
:
376 path
= CONTROL1_SW_USB
;
378 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF
:
379 path
= CONTROL1_SW_UART
;
385 ret
= max77843_muic_set_path(info
, path
, attached
);
389 extcon_set_cable_state_(info
->edev
, EXTCON_JIG
, attached
);
394 static int max77843_muic_adc_handler(struct max77843_muic_info
*info
)
399 cable_type
= max77843_muic_get_cable_type(info
,
400 MAX77843_CABLE_GROUP_ADC
, &attached
);
403 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
404 attached
? "attached" : "detached", cable_type
,
405 info
->prev_cable_type
);
407 switch (cable_type
) {
408 case MAX77843_MUIC_ADC_GROUND
:
409 ret
= max77843_muic_adc_gnd_handler(info
);
413 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF
:
414 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON
:
415 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF
:
416 ret
= max77843_muic_jig_handler(info
, cable_type
, attached
);
420 case MAX77843_MUIC_ADC_SEND_END_BUTTON
:
421 case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON
:
422 case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON
:
423 case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON
:
424 case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON
:
425 case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON
:
426 case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON
:
427 case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON
:
428 case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON
:
429 case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON
:
430 case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON
:
431 case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON
:
432 case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON
:
433 case MAX77843_MUIC_ADC_RESERVED_ACC_1
:
434 case MAX77843_MUIC_ADC_RESERVED_ACC_2
:
435 case MAX77843_MUIC_ADC_RESERVED_ACC_3
:
436 case MAX77843_MUIC_ADC_RESERVED_ACC_4
:
437 case MAX77843_MUIC_ADC_RESERVED_ACC_5
:
438 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2
:
439 case MAX77843_MUIC_ADC_PHONE_POWERED_DEV
:
440 case MAX77843_MUIC_ADC_TTY_CONVERTER
:
441 case MAX77843_MUIC_ADC_UART_CABLE
:
442 case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG
:
443 case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD
:
444 case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG
:
445 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON
:
446 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1
:
447 case MAX77843_MUIC_ADC_OPEN
:
449 "accessory is %s but it isn't used (adc:0x%x)\n",
450 attached
? "attached" : "detached", cable_type
);
454 "failed to detect %s accessory (adc:0x%x)\n",
455 attached
? "attached" : "detached", cable_type
);
462 static int max77843_muic_chg_handler(struct max77843_muic_info
*info
)
464 int ret
, chg_type
, gnd_type
;
467 chg_type
= max77843_muic_get_cable_type(info
,
468 MAX77843_CABLE_GROUP_CHG
, &attached
);
471 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
472 attached
? "attached" : "detached",
473 chg_type
, info
->prev_chg_type
);
476 case MAX77843_MUIC_CHG_USB
:
477 ret
= max77843_muic_set_path(info
, CONTROL1_SW_USB
, attached
);
481 extcon_set_cable_state_(info
->edev
, EXTCON_USB
, attached
);
483 case MAX77843_MUIC_CHG_DOWNSTREAM
:
484 ret
= max77843_muic_set_path(info
, CONTROL1_SW_OPEN
, attached
);
488 extcon_set_cable_state_(info
->edev
, EXTCON_CHARGE_DOWNSTREAM
,
491 case MAX77843_MUIC_CHG_DEDICATED
:
492 ret
= max77843_muic_set_path(info
, CONTROL1_SW_OPEN
, attached
);
496 extcon_set_cable_state_(info
->edev
, EXTCON_TA
, attached
);
498 case MAX77843_MUIC_CHG_SPECIAL_500MA
:
499 ret
= max77843_muic_set_path(info
, CONTROL1_SW_OPEN
, attached
);
503 extcon_set_cable_state_(info
->edev
, EXTCON_SLOW_CHARGER
,
506 case MAX77843_MUIC_CHG_SPECIAL_1A
:
507 ret
= max77843_muic_set_path(info
, CONTROL1_SW_OPEN
, attached
);
511 extcon_set_cable_state_(info
->edev
, EXTCON_FAST_CHARGER
,
514 case MAX77843_MUIC_CHG_GND
:
515 gnd_type
= max77843_muic_get_cable_type(info
,
516 MAX77843_CABLE_GROUP_ADC_GND
, &attached
);
518 /* Charger cable on MHL accessory is attach or detach */
519 if (gnd_type
== MAX77843_MUIC_GND_MHL_VB
)
520 extcon_set_cable_state_(info
->edev
, EXTCON_TA
, true);
521 else if (gnd_type
== MAX77843_MUIC_GND_MHL
)
522 extcon_set_cable_state_(info
->edev
, EXTCON_TA
, false);
524 case MAX77843_MUIC_CHG_NONE
:
528 "failed to detect %s accessory (chg_type:0x%x)\n",
529 attached
? "attached" : "detached", chg_type
);
531 max77843_muic_set_path(info
, CONTROL1_SW_OPEN
, attached
);
538 static void max77843_muic_irq_work(struct work_struct
*work
)
540 struct max77843_muic_info
*info
= container_of(work
,
541 struct max77843_muic_info
, irq_work
);
542 struct max77843
*max77843
= info
->max77843
;
545 mutex_lock(&info
->mutex
);
547 ret
= regmap_bulk_read(max77843
->regmap_muic
,
548 MAX77843_MUIC_REG_STATUS1
, info
->status
,
549 MAX77843_MUIC_STATUS_NUM
);
551 dev_err(info
->dev
, "Cannot read STATUS registers\n");
552 mutex_unlock(&info
->mutex
);
557 ret
= max77843_muic_adc_handler(info
);
559 dev_err(info
->dev
, "Unknown cable type\n");
560 info
->irq_adc
= false;
564 ret
= max77843_muic_chg_handler(info
);
566 dev_err(info
->dev
, "Unknown charger type\n");
567 info
->irq_chg
= false;
570 mutex_unlock(&info
->mutex
);
573 static irqreturn_t
max77843_muic_irq_handler(int irq
, void *data
)
575 struct max77843_muic_info
*info
= data
;
576 int i
, irq_type
= -1;
578 for (i
= 0; i
< ARRAY_SIZE(max77843_muic_irqs
); i
++)
579 if (irq
== max77843_muic_irqs
[i
].virq
)
580 irq_type
= max77843_muic_irqs
[i
].irq
;
583 case MAX77843_MUIC_IRQ_INT1_ADC
:
584 case MAX77843_MUIC_IRQ_INT1_ADCERROR
:
585 case MAX77843_MUIC_IRQ_INT1_ADC1K
:
586 info
->irq_adc
= true;
588 case MAX77843_MUIC_IRQ_INT2_CHGTYP
:
589 case MAX77843_MUIC_IRQ_INT2_CHGDETRUN
:
590 case MAX77843_MUIC_IRQ_INT2_DCDTMR
:
591 case MAX77843_MUIC_IRQ_INT2_DXOVP
:
592 case MAX77843_MUIC_IRQ_INT2_VBVOLT
:
593 info
->irq_chg
= true;
595 case MAX77843_MUIC_IRQ_INT3_VBADC
:
596 case MAX77843_MUIC_IRQ_INT3_VDNMON
:
597 case MAX77843_MUIC_IRQ_INT3_DNRES
:
598 case MAX77843_MUIC_IRQ_INT3_MPNACK
:
599 case MAX77843_MUIC_IRQ_INT3_MRXBUFOW
:
600 case MAX77843_MUIC_IRQ_INT3_MRXTRF
:
601 case MAX77843_MUIC_IRQ_INT3_MRXPERR
:
602 case MAX77843_MUIC_IRQ_INT3_MRXRDY
:
605 dev_err(info
->dev
, "Cannot recognize IRQ(%d)\n", irq_type
);
609 schedule_work(&info
->irq_work
);
614 static void max77843_muic_detect_cable_wq(struct work_struct
*work
)
616 struct max77843_muic_info
*info
= container_of(to_delayed_work(work
),
617 struct max77843_muic_info
, wq_detcable
);
618 struct max77843
*max77843
= info
->max77843
;
619 int chg_type
, adc
, ret
;
622 mutex_lock(&info
->mutex
);
624 ret
= regmap_bulk_read(max77843
->regmap_muic
,
625 MAX77843_MUIC_REG_STATUS1
, info
->status
,
626 MAX77843_MUIC_STATUS_NUM
);
628 dev_err(info
->dev
, "Cannot read STATUS registers\n");
632 adc
= max77843_muic_get_cable_type(info
,
633 MAX77843_CABLE_GROUP_ADC
, &attached
);
634 if (attached
&& adc
!= MAX77843_MUIC_ADC_OPEN
) {
635 ret
= max77843_muic_adc_handler(info
);
637 dev_err(info
->dev
, "Cannot detect accessory\n");
642 chg_type
= max77843_muic_get_cable_type(info
,
643 MAX77843_CABLE_GROUP_CHG
, &attached
);
644 if (attached
&& chg_type
!= MAX77843_MUIC_CHG_NONE
) {
645 ret
= max77843_muic_chg_handler(info
);
647 dev_err(info
->dev
, "Cannot detect charger accessory\n");
653 mutex_unlock(&info
->mutex
);
656 static int max77843_muic_set_debounce_time(struct max77843_muic_info
*info
,
657 enum max77843_muic_adc_debounce_time time
)
659 struct max77843
*max77843
= info
->max77843
;
663 case MAX77843_DEBOUNCE_TIME_5MS
:
664 case MAX77843_DEBOUNCE_TIME_10MS
:
665 case MAX77843_DEBOUNCE_TIME_25MS
:
666 case MAX77843_DEBOUNCE_TIME_38_62MS
:
667 ret
= regmap_update_bits(max77843
->regmap_muic
,
668 MAX77843_MUIC_REG_CONTROL4
,
669 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK
,
670 time
<< CONTROL4_ADCDBSET_SHIFT
);
672 dev_err(info
->dev
, "Cannot write MUIC regmap\n");
677 dev_err(info
->dev
, "Invalid ADC debounce time\n");
684 static int max77843_init_muic_regmap(struct max77843
*max77843
)
688 max77843
->i2c_muic
= i2c_new_dummy(max77843
->i2c
->adapter
,
690 if (!max77843
->i2c_muic
) {
691 dev_err(&max77843
->i2c
->dev
,
692 "Cannot allocate I2C device for MUIC\n");
696 i2c_set_clientdata(max77843
->i2c_muic
, max77843
);
698 max77843
->regmap_muic
= devm_regmap_init_i2c(max77843
->i2c_muic
,
699 &max77843_muic_regmap_config
);
700 if (IS_ERR(max77843
->regmap_muic
)) {
701 ret
= PTR_ERR(max77843
->regmap_muic
);
705 ret
= regmap_add_irq_chip(max77843
->regmap_muic
, max77843
->irq
,
706 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
| IRQF_SHARED
,
707 0, &max77843_muic_irq_chip
, &max77843
->irq_data_muic
);
709 dev_err(&max77843
->i2c
->dev
, "Cannot add MUIC IRQ chip\n");
716 i2c_unregister_device(max77843
->i2c_muic
);
721 static int max77843_muic_probe(struct platform_device
*pdev
)
723 struct max77843
*max77843
= dev_get_drvdata(pdev
->dev
.parent
);
724 struct max77843_muic_info
*info
;
728 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
732 info
->dev
= &pdev
->dev
;
733 info
->max77843
= max77843
;
735 platform_set_drvdata(pdev
, info
);
736 mutex_init(&info
->mutex
);
738 /* Initialize i2c and regmap */
739 ret
= max77843_init_muic_regmap(max77843
);
741 dev_err(&pdev
->dev
, "Failed to init MUIC regmap\n");
745 /* Turn off auto detection configuration */
746 ret
= regmap_update_bits(max77843
->regmap_muic
,
747 MAX77843_MUIC_REG_CONTROL4
,
748 MAX77843_MUIC_CONTROL4_USBAUTO_MASK
|
749 MAX77843_MUIC_CONTROL4_FCTAUTO_MASK
,
750 CONTROL4_AUTO_DISABLE
);
752 /* Initialize extcon device */
753 info
->edev
= devm_extcon_dev_allocate(&pdev
->dev
,
754 max77843_extcon_cable
);
755 if (IS_ERR(info
->edev
)) {
756 dev_err(&pdev
->dev
, "Failed to allocate memory for extcon\n");
761 ret
= devm_extcon_dev_register(&pdev
->dev
, info
->edev
);
763 dev_err(&pdev
->dev
, "Failed to register extcon device\n");
767 /* Set ADC debounce time */
768 max77843_muic_set_debounce_time(info
, MAX77843_DEBOUNCE_TIME_25MS
);
770 /* Set initial path for UART */
771 max77843_muic_set_path(info
, CONTROL1_SW_UART
, true);
773 /* Check revision number of MUIC device */
774 ret
= regmap_read(max77843
->regmap_muic
, MAX77843_MUIC_REG_ID
, &id
);
776 dev_err(&pdev
->dev
, "Failed to read revision number\n");
779 dev_info(info
->dev
, "MUIC device ID : 0x%x\n", id
);
781 /* Support virtual irq domain for max77843 MUIC device */
782 INIT_WORK(&info
->irq_work
, max77843_muic_irq_work
);
784 for (i
= 0; i
< ARRAY_SIZE(max77843_muic_irqs
); i
++) {
785 struct max77843_muic_irq
*muic_irq
= &max77843_muic_irqs
[i
];
786 unsigned int virq
= 0;
788 virq
= regmap_irq_get_virq(max77843
->irq_data_muic
,
794 muic_irq
->virq
= virq
;
796 ret
= devm_request_threaded_irq(&pdev
->dev
, virq
, NULL
,
797 max77843_muic_irq_handler
, IRQF_NO_SUSPEND
,
798 muic_irq
->name
, info
);
801 "Failed to request irq (IRQ: %d, error: %d)\n",
807 /* Detect accessory after completing the initialization of platform */
808 INIT_DELAYED_WORK(&info
->wq_detcable
, max77843_muic_detect_cable_wq
);
809 queue_delayed_work(system_power_efficient_wq
,
810 &info
->wq_detcable
, msecs_to_jiffies(DELAY_MS_DEFAULT
));
815 regmap_del_irq_chip(max77843
->irq
, max77843
->irq_data_muic
);
816 i2c_unregister_device(max77843
->i2c_muic
);
821 static int max77843_muic_remove(struct platform_device
*pdev
)
823 struct max77843_muic_info
*info
= platform_get_drvdata(pdev
);
824 struct max77843
*max77843
= info
->max77843
;
826 cancel_work_sync(&info
->irq_work
);
827 regmap_del_irq_chip(max77843
->irq
, max77843
->irq_data_muic
);
828 i2c_unregister_device(max77843
->i2c_muic
);
833 static const struct platform_device_id max77843_muic_id
[] = {
834 { "max77843-muic", },
837 MODULE_DEVICE_TABLE(platform
, max77843_muic_id
);
839 static struct platform_driver max77843_muic_driver
= {
841 .name
= "max77843-muic",
843 .probe
= max77843_muic_probe
,
844 .remove
= max77843_muic_remove
,
845 .id_table
= max77843_muic_id
,
848 static int __init
max77843_muic_init(void)
850 return platform_driver_register(&max77843_muic_driver
);
852 subsys_initcall(max77843_muic_init
);
854 MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
855 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
856 MODULE_LICENSE("GPL");