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-provider.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
, /* SmartDock */
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
,
122 MAX77843_MUIC_CHG_DOCK
,
125 static const unsigned int max77843_extcon_cable
[] = {
139 struct max77843_muic_irq
{
145 static struct max77843_muic_irq max77843_muic_irqs
[] = {
146 { MAX77843_MUIC_IRQ_INT1_ADC
, "MUIC-ADC" },
147 { MAX77843_MUIC_IRQ_INT1_ADCERROR
, "MUIC-ADC_ERROR" },
148 { MAX77843_MUIC_IRQ_INT1_ADC1K
, "MUIC-ADC1K" },
149 { MAX77843_MUIC_IRQ_INT2_CHGTYP
, "MUIC-CHGTYP" },
150 { MAX77843_MUIC_IRQ_INT2_CHGDETRUN
, "MUIC-CHGDETRUN" },
151 { MAX77843_MUIC_IRQ_INT2_DCDTMR
, "MUIC-DCDTMR" },
152 { MAX77843_MUIC_IRQ_INT2_DXOVP
, "MUIC-DXOVP" },
153 { MAX77843_MUIC_IRQ_INT2_VBVOLT
, "MUIC-VBVOLT" },
154 { MAX77843_MUIC_IRQ_INT3_VBADC
, "MUIC-VBADC" },
155 { MAX77843_MUIC_IRQ_INT3_VDNMON
, "MUIC-VDNMON" },
156 { MAX77843_MUIC_IRQ_INT3_DNRES
, "MUIC-DNRES" },
157 { MAX77843_MUIC_IRQ_INT3_MPNACK
, "MUIC-MPNACK"},
158 { MAX77843_MUIC_IRQ_INT3_MRXBUFOW
, "MUIC-MRXBUFOW"},
159 { MAX77843_MUIC_IRQ_INT3_MRXTRF
, "MUIC-MRXTRF"},
160 { MAX77843_MUIC_IRQ_INT3_MRXPERR
, "MUIC-MRXPERR"},
161 { MAX77843_MUIC_IRQ_INT3_MRXRDY
, "MUIC-MRXRDY"},
164 static const struct regmap_config max77843_muic_regmap_config
= {
167 .max_register
= MAX77843_MUIC_REG_END
,
170 static const struct regmap_irq max77843_muic_irq
[] = {
172 { .reg_offset
= 0, .mask
= MAX77843_MUIC_ADC
, },
173 { .reg_offset
= 0, .mask
= MAX77843_MUIC_ADCERROR
, },
174 { .reg_offset
= 0, .mask
= MAX77843_MUIC_ADC1K
, },
177 { .reg_offset
= 1, .mask
= MAX77843_MUIC_CHGTYP
, },
178 { .reg_offset
= 1, .mask
= MAX77843_MUIC_CHGDETRUN
, },
179 { .reg_offset
= 1, .mask
= MAX77843_MUIC_DCDTMR
, },
180 { .reg_offset
= 1, .mask
= MAX77843_MUIC_DXOVP
, },
181 { .reg_offset
= 1, .mask
= MAX77843_MUIC_VBVOLT
, },
184 { .reg_offset
= 2, .mask
= MAX77843_MUIC_VBADC
, },
185 { .reg_offset
= 2, .mask
= MAX77843_MUIC_VDNMON
, },
186 { .reg_offset
= 2, .mask
= MAX77843_MUIC_DNRES
, },
187 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MPNACK
, },
188 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXBUFOW
, },
189 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXTRF
, },
190 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXPERR
, },
191 { .reg_offset
= 2, .mask
= MAX77843_MUIC_MRXRDY
, },
194 static const struct regmap_irq_chip max77843_muic_irq_chip
= {
195 .name
= "max77843-muic",
196 .status_base
= MAX77843_MUIC_REG_INT1
,
197 .mask_base
= MAX77843_MUIC_REG_INTMASK1
,
200 .irqs
= max77843_muic_irq
,
201 .num_irqs
= ARRAY_SIZE(max77843_muic_irq
),
204 static int max77843_muic_set_path(struct max77843_muic_info
*info
,
205 u8 val
, bool attached
, bool nobccomp
)
207 struct max77693_dev
*max77843
= info
->max77843
;
209 unsigned int ctrl1
, ctrl2
;
214 ctrl1
= MAX77843_MUIC_CONTROL1_SW_OPEN
;
216 /* Disable BC1.2 protocol and force manual switch control */
217 ctrl1
|= MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK
;
220 ret
= regmap_update_bits(max77843
->regmap_muic
,
221 MAX77843_MUIC_REG_CONTROL1
,
222 MAX77843_MUIC_CONTROL1_COM_SW
|
223 MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK
,
226 dev_err(info
->dev
, "Cannot switch MUIC port\n");
231 ctrl2
= MAX77843_MUIC_CONTROL2_CPEN_MASK
;
233 ctrl2
= MAX77843_MUIC_CONTROL2_LOWPWR_MASK
;
235 ret
= regmap_update_bits(max77843
->regmap_muic
,
236 MAX77843_MUIC_REG_CONTROL2
,
237 MAX77843_MUIC_CONTROL2_LOWPWR_MASK
|
238 MAX77843_MUIC_CONTROL2_CPEN_MASK
, ctrl2
);
240 dev_err(info
->dev
, "Cannot update lowpower mode\n");
245 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
246 ctrl1
, ctrl2
, attached
? "attached" : "detached");
251 static void max77843_charger_set_otg_vbus(struct max77843_muic_info
*info
,
254 struct max77693_dev
*max77843
= info
->max77843
;
258 cnfg00
= MAX77843_CHG_OTG_MASK
| MAX77843_CHG_BOOST_MASK
;
260 cnfg00
= MAX77843_CHG_ENABLE
| MAX77843_CHG_BUCK_MASK
;
262 regmap_update_bits(max77843
->regmap_chg
, MAX77843_CHG_REG_CHG_CNFG_00
,
263 MAX77843_CHG_MODE_MASK
, cnfg00
);
266 static int max77843_muic_get_cable_type(struct max77843_muic_info
*info
,
267 enum max77843_muic_cable_group group
, bool *attached
)
269 int adc
, chg_type
, cable_type
, gnd_type
;
271 adc
= info
->status
[MAX77843_MUIC_STATUS1
] &
272 MAX77843_MUIC_STATUS1_ADC_MASK
;
273 adc
>>= MAX77843_MUIC_STATUS1_ADC_SHIFT
;
276 case MAX77843_CABLE_GROUP_ADC
:
277 if (adc
== MAX77843_MUIC_ADC_OPEN
) {
279 cable_type
= info
->prev_cable_type
;
280 info
->prev_cable_type
= MAX77843_MUIC_ADC_OPEN
;
283 cable_type
= info
->prev_cable_type
= adc
;
286 case MAX77843_CABLE_GROUP_CHG
:
287 chg_type
= info
->status
[MAX77843_MUIC_STATUS2
] &
288 MAX77843_MUIC_STATUS2_CHGTYP_MASK
;
290 /* Check GROUND accessory with charger cable */
291 if (adc
== MAX77843_MUIC_ADC_GROUND
) {
292 if (chg_type
== MAX77843_MUIC_CHG_NONE
) {
294 * The following state when charger cable is
295 * disconnected but the GROUND accessory still
299 cable_type
= info
->prev_chg_type
;
300 info
->prev_chg_type
= MAX77843_MUIC_CHG_NONE
;
304 * The following state when charger cable is
305 * connected on the GROUND accessory.
308 cable_type
= MAX77843_MUIC_CHG_GND
;
309 info
->prev_chg_type
= MAX77843_MUIC_CHG_GND
;
314 if (adc
== MAX77843_MUIC_ADC_RESERVED_ACC_3
) { /* SmartDock */
315 if (chg_type
== MAX77843_MUIC_CHG_NONE
) {
317 cable_type
= info
->prev_chg_type
;
318 info
->prev_chg_type
= MAX77843_MUIC_CHG_NONE
;
321 cable_type
= MAX77843_MUIC_CHG_DOCK
;
322 info
->prev_chg_type
= MAX77843_MUIC_CHG_DOCK
;
327 if (chg_type
== MAX77843_MUIC_CHG_NONE
) {
329 cable_type
= info
->prev_chg_type
;
330 info
->prev_chg_type
= MAX77843_MUIC_CHG_NONE
;
333 cable_type
= info
->prev_chg_type
= chg_type
;
336 case MAX77843_CABLE_GROUP_ADC_GND
:
337 if (adc
== MAX77843_MUIC_ADC_OPEN
) {
339 cable_type
= info
->prev_gnd_type
;
340 info
->prev_gnd_type
= MAX77843_MUIC_ADC_OPEN
;
345 * Offset|ADC1K|VBVolt|
346 * 0x1| 0| 0| USB-HOST
347 * 0x1| 0| 1| USB-HOST with VB
349 * 0x1| 1| 1| MHL with VB
351 /* Get ADC1K register bit */
352 gnd_type
= (info
->status
[MAX77843_MUIC_STATUS1
] &
353 MAX77843_MUIC_STATUS1_ADC1K_MASK
);
355 /* Get VBVolt register bit */
356 gnd_type
|= (info
->status
[MAX77843_MUIC_STATUS2
] &
357 MAX77843_MUIC_STATUS2_VBVOLT_MASK
);
358 gnd_type
>>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT
;
360 /* Offset of GND cable */
361 gnd_type
|= MAX77843_MUIC_GND_USB_HOST
;
362 cable_type
= info
->prev_gnd_type
= gnd_type
;
366 dev_err(info
->dev
, "Unknown cable group (%d)\n", group
);
367 cable_type
= -EINVAL
;
374 static int max77843_muic_adc_gnd_handler(struct max77843_muic_info
*info
)
376 int ret
, gnd_cable_type
;
379 gnd_cable_type
= max77843_muic_get_cable_type(info
,
380 MAX77843_CABLE_GROUP_ADC_GND
, &attached
);
381 dev_dbg(info
->dev
, "external connector is %s (gnd:0x%02x)\n",
382 attached
? "attached" : "detached", gnd_cable_type
);
384 switch (gnd_cable_type
) {
385 case MAX77843_MUIC_GND_USB_HOST
:
386 case MAX77843_MUIC_GND_USB_HOST_VB
:
387 ret
= max77843_muic_set_path(info
,
388 MAX77843_MUIC_CONTROL1_SW_USB
,
393 extcon_set_state_sync(info
->edev
, EXTCON_USB_HOST
, attached
);
394 max77843_charger_set_otg_vbus(info
, attached
);
396 case MAX77843_MUIC_GND_MHL_VB
:
397 case MAX77843_MUIC_GND_MHL
:
398 ret
= max77843_muic_set_path(info
,
399 MAX77843_MUIC_CONTROL1_SW_OPEN
,
404 extcon_set_state_sync(info
->edev
, EXTCON_DISP_MHL
, attached
);
407 dev_err(info
->dev
, "failed to detect %s accessory(gnd:0x%x)\n",
408 attached
? "attached" : "detached", gnd_cable_type
);
415 static int max77843_muic_jig_handler(struct max77843_muic_info
*info
,
416 int cable_type
, bool attached
)
419 u8 path
= MAX77843_MUIC_CONTROL1_SW_OPEN
;
421 dev_dbg(info
->dev
, "external connector is %s (adc:0x%02x)\n",
422 attached
? "attached" : "detached", cable_type
);
424 switch (cable_type
) {
425 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF
:
426 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON
:
427 path
= MAX77843_MUIC_CONTROL1_SW_USB
;
429 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF
:
430 path
= MAX77843_MUIC_CONTROL1_SW_UART
;
436 ret
= max77843_muic_set_path(info
, path
, attached
, false);
440 extcon_set_state_sync(info
->edev
, EXTCON_JIG
, attached
);
445 static int max77843_muic_dock_handler(struct max77843_muic_info
*info
,
450 dev_dbg(info
->dev
, "external connector is %s (adc: 0x10)\n",
451 attached
? "attached" : "detached");
453 ret
= max77843_muic_set_path(info
, MAX77843_MUIC_CONTROL1_SW_USB
,
458 extcon_set_state_sync(info
->edev
, EXTCON_DISP_MHL
, attached
);
459 extcon_set_state_sync(info
->edev
, EXTCON_USB_HOST
, attached
);
460 extcon_set_state_sync(info
->edev
, EXTCON_DOCK
, attached
);
465 static int max77843_muic_adc_handler(struct max77843_muic_info
*info
)
470 cable_type
= max77843_muic_get_cable_type(info
,
471 MAX77843_CABLE_GROUP_ADC
, &attached
);
474 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
475 attached
? "attached" : "detached", cable_type
,
476 info
->prev_cable_type
);
478 switch (cable_type
) {
479 case MAX77843_MUIC_ADC_RESERVED_ACC_3
: /* SmartDock */
480 ret
= max77843_muic_dock_handler(info
, attached
);
484 case MAX77843_MUIC_ADC_GROUND
:
485 ret
= max77843_muic_adc_gnd_handler(info
);
489 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF
:
490 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON
:
491 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF
:
492 ret
= max77843_muic_jig_handler(info
, cable_type
, attached
);
496 case MAX77843_MUIC_ADC_SEND_END_BUTTON
:
497 case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON
:
498 case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON
:
499 case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON
:
500 case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON
:
501 case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON
:
502 case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON
:
503 case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON
:
504 case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON
:
505 case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON
:
506 case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON
:
507 case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON
:
508 case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON
:
509 case MAX77843_MUIC_ADC_RESERVED_ACC_1
:
510 case MAX77843_MUIC_ADC_RESERVED_ACC_2
:
511 case MAX77843_MUIC_ADC_RESERVED_ACC_4
:
512 case MAX77843_MUIC_ADC_RESERVED_ACC_5
:
513 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2
:
514 case MAX77843_MUIC_ADC_PHONE_POWERED_DEV
:
515 case MAX77843_MUIC_ADC_TTY_CONVERTER
:
516 case MAX77843_MUIC_ADC_UART_CABLE
:
517 case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG
:
518 case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD
:
519 case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG
:
520 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON
:
521 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1
:
522 case MAX77843_MUIC_ADC_OPEN
:
524 "accessory is %s but it isn't used (adc:0x%x)\n",
525 attached
? "attached" : "detached", cable_type
);
529 "failed to detect %s accessory (adc:0x%x)\n",
530 attached
? "attached" : "detached", cable_type
);
537 static int max77843_muic_chg_handler(struct max77843_muic_info
*info
)
539 int ret
, chg_type
, gnd_type
;
542 chg_type
= max77843_muic_get_cable_type(info
,
543 MAX77843_CABLE_GROUP_CHG
, &attached
);
546 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
547 attached
? "attached" : "detached",
548 chg_type
, info
->prev_chg_type
);
551 case MAX77843_MUIC_CHG_USB
:
552 ret
= max77843_muic_set_path(info
,
553 MAX77843_MUIC_CONTROL1_SW_USB
,
558 extcon_set_state_sync(info
->edev
, EXTCON_USB
, attached
);
559 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_SDP
,
562 case MAX77843_MUIC_CHG_DOWNSTREAM
:
563 ret
= max77843_muic_set_path(info
,
564 MAX77843_MUIC_CONTROL1_SW_OPEN
,
569 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_CDP
,
572 case MAX77843_MUIC_CHG_DEDICATED
:
573 ret
= max77843_muic_set_path(info
,
574 MAX77843_MUIC_CONTROL1_SW_OPEN
,
579 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_DCP
,
582 case MAX77843_MUIC_CHG_SPECIAL_500MA
:
583 ret
= max77843_muic_set_path(info
,
584 MAX77843_MUIC_CONTROL1_SW_OPEN
,
589 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_SLOW
,
592 case MAX77843_MUIC_CHG_SPECIAL_1A
:
593 ret
= max77843_muic_set_path(info
,
594 MAX77843_MUIC_CONTROL1_SW_OPEN
,
599 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_FAST
,
602 case MAX77843_MUIC_CHG_GND
:
603 gnd_type
= max77843_muic_get_cable_type(info
,
604 MAX77843_CABLE_GROUP_ADC_GND
, &attached
);
606 /* Charger cable on MHL accessory is attach or detach */
607 if (gnd_type
== MAX77843_MUIC_GND_MHL_VB
)
608 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_DCP
,
610 else if (gnd_type
== MAX77843_MUIC_GND_MHL
)
611 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_DCP
,
614 case MAX77843_MUIC_CHG_DOCK
:
615 extcon_set_state_sync(info
->edev
, EXTCON_CHG_USB_DCP
, attached
);
617 case MAX77843_MUIC_CHG_NONE
:
621 "failed to detect %s accessory (chg_type:0x%x)\n",
622 attached
? "attached" : "detached", chg_type
);
624 max77843_muic_set_path(info
, MAX77843_MUIC_CONTROL1_SW_OPEN
,
632 static void max77843_muic_irq_work(struct work_struct
*work
)
634 struct max77843_muic_info
*info
= container_of(work
,
635 struct max77843_muic_info
, irq_work
);
636 struct max77693_dev
*max77843
= info
->max77843
;
639 mutex_lock(&info
->mutex
);
641 ret
= regmap_bulk_read(max77843
->regmap_muic
,
642 MAX77843_MUIC_REG_STATUS1
, info
->status
,
643 MAX77843_MUIC_STATUS_NUM
);
645 dev_err(info
->dev
, "Cannot read STATUS registers\n");
646 mutex_unlock(&info
->mutex
);
651 ret
= max77843_muic_adc_handler(info
);
653 dev_err(info
->dev
, "Unknown cable type\n");
654 info
->irq_adc
= false;
658 ret
= max77843_muic_chg_handler(info
);
660 dev_err(info
->dev
, "Unknown charger type\n");
661 info
->irq_chg
= false;
664 mutex_unlock(&info
->mutex
);
667 static irqreturn_t
max77843_muic_irq_handler(int irq
, void *data
)
669 struct max77843_muic_info
*info
= data
;
670 int i
, irq_type
= -1;
672 for (i
= 0; i
< ARRAY_SIZE(max77843_muic_irqs
); i
++)
673 if (irq
== max77843_muic_irqs
[i
].virq
)
674 irq_type
= max77843_muic_irqs
[i
].irq
;
677 case MAX77843_MUIC_IRQ_INT1_ADC
:
678 case MAX77843_MUIC_IRQ_INT1_ADCERROR
:
679 case MAX77843_MUIC_IRQ_INT1_ADC1K
:
680 info
->irq_adc
= true;
682 case MAX77843_MUIC_IRQ_INT2_CHGTYP
:
683 case MAX77843_MUIC_IRQ_INT2_CHGDETRUN
:
684 case MAX77843_MUIC_IRQ_INT2_DCDTMR
:
685 case MAX77843_MUIC_IRQ_INT2_DXOVP
:
686 case MAX77843_MUIC_IRQ_INT2_VBVOLT
:
687 info
->irq_chg
= true;
689 case MAX77843_MUIC_IRQ_INT3_VBADC
:
690 case MAX77843_MUIC_IRQ_INT3_VDNMON
:
691 case MAX77843_MUIC_IRQ_INT3_DNRES
:
692 case MAX77843_MUIC_IRQ_INT3_MPNACK
:
693 case MAX77843_MUIC_IRQ_INT3_MRXBUFOW
:
694 case MAX77843_MUIC_IRQ_INT3_MRXTRF
:
695 case MAX77843_MUIC_IRQ_INT3_MRXPERR
:
696 case MAX77843_MUIC_IRQ_INT3_MRXRDY
:
699 dev_err(info
->dev
, "Cannot recognize IRQ(%d)\n", irq_type
);
703 schedule_work(&info
->irq_work
);
708 static void max77843_muic_detect_cable_wq(struct work_struct
*work
)
710 struct max77843_muic_info
*info
= container_of(to_delayed_work(work
),
711 struct max77843_muic_info
, wq_detcable
);
712 struct max77693_dev
*max77843
= info
->max77843
;
713 int chg_type
, adc
, ret
;
716 mutex_lock(&info
->mutex
);
718 ret
= regmap_bulk_read(max77843
->regmap_muic
,
719 MAX77843_MUIC_REG_STATUS1
, info
->status
,
720 MAX77843_MUIC_STATUS_NUM
);
722 dev_err(info
->dev
, "Cannot read STATUS registers\n");
726 adc
= max77843_muic_get_cable_type(info
,
727 MAX77843_CABLE_GROUP_ADC
, &attached
);
728 if (attached
&& adc
!= MAX77843_MUIC_ADC_OPEN
) {
729 ret
= max77843_muic_adc_handler(info
);
731 dev_err(info
->dev
, "Cannot detect accessory\n");
736 chg_type
= max77843_muic_get_cable_type(info
,
737 MAX77843_CABLE_GROUP_CHG
, &attached
);
738 if (attached
&& chg_type
!= MAX77843_MUIC_CHG_NONE
) {
739 ret
= max77843_muic_chg_handler(info
);
741 dev_err(info
->dev
, "Cannot detect charger accessory\n");
747 mutex_unlock(&info
->mutex
);
750 static int max77843_muic_set_debounce_time(struct max77843_muic_info
*info
,
751 enum max77843_muic_adc_debounce_time time
)
753 struct max77693_dev
*max77843
= info
->max77843
;
757 case MAX77843_DEBOUNCE_TIME_5MS
:
758 case MAX77843_DEBOUNCE_TIME_10MS
:
759 case MAX77843_DEBOUNCE_TIME_25MS
:
760 case MAX77843_DEBOUNCE_TIME_38_62MS
:
761 ret
= regmap_update_bits(max77843
->regmap_muic
,
762 MAX77843_MUIC_REG_CONTROL4
,
763 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK
,
764 time
<< MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT
);
766 dev_err(info
->dev
, "Cannot write MUIC regmap\n");
771 dev_err(info
->dev
, "Invalid ADC debounce time\n");
778 static int max77843_init_muic_regmap(struct max77693_dev
*max77843
)
782 max77843
->i2c_muic
= i2c_new_dummy(max77843
->i2c
->adapter
,
784 if (!max77843
->i2c_muic
) {
785 dev_err(&max77843
->i2c
->dev
,
786 "Cannot allocate I2C device for MUIC\n");
790 i2c_set_clientdata(max77843
->i2c_muic
, max77843
);
792 max77843
->regmap_muic
= devm_regmap_init_i2c(max77843
->i2c_muic
,
793 &max77843_muic_regmap_config
);
794 if (IS_ERR(max77843
->regmap_muic
)) {
795 ret
= PTR_ERR(max77843
->regmap_muic
);
799 ret
= regmap_add_irq_chip(max77843
->regmap_muic
, max77843
->irq
,
800 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
| IRQF_SHARED
,
801 0, &max77843_muic_irq_chip
, &max77843
->irq_data_muic
);
803 dev_err(&max77843
->i2c
->dev
, "Cannot add MUIC IRQ chip\n");
810 i2c_unregister_device(max77843
->i2c_muic
);
815 static int max77843_muic_probe(struct platform_device
*pdev
)
817 struct max77693_dev
*max77843
= dev_get_drvdata(pdev
->dev
.parent
);
818 struct max77843_muic_info
*info
;
822 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
826 info
->dev
= &pdev
->dev
;
827 info
->max77843
= max77843
;
829 platform_set_drvdata(pdev
, info
);
830 mutex_init(&info
->mutex
);
832 /* Initialize i2c and regmap */
833 ret
= max77843_init_muic_regmap(max77843
);
835 dev_err(&pdev
->dev
, "Failed to init MUIC regmap\n");
839 /* Turn off auto detection configuration */
840 ret
= regmap_update_bits(max77843
->regmap_muic
,
841 MAX77843_MUIC_REG_CONTROL4
,
842 MAX77843_MUIC_CONTROL4_USBAUTO_MASK
|
843 MAX77843_MUIC_CONTROL4_FCTAUTO_MASK
,
844 CONTROL4_AUTO_DISABLE
);
846 /* Initialize extcon device */
847 info
->edev
= devm_extcon_dev_allocate(&pdev
->dev
,
848 max77843_extcon_cable
);
849 if (IS_ERR(info
->edev
)) {
850 dev_err(&pdev
->dev
, "Failed to allocate memory for extcon\n");
855 ret
= devm_extcon_dev_register(&pdev
->dev
, info
->edev
);
857 dev_err(&pdev
->dev
, "Failed to register extcon device\n");
861 /* Set ADC debounce time */
862 max77843_muic_set_debounce_time(info
, MAX77843_DEBOUNCE_TIME_25MS
);
864 /* Set initial path for UART */
865 max77843_muic_set_path(info
, MAX77843_MUIC_CONTROL1_SW_UART
, true,
868 /* Check revision number of MUIC device */
869 ret
= regmap_read(max77843
->regmap_muic
, MAX77843_MUIC_REG_ID
, &id
);
871 dev_err(&pdev
->dev
, "Failed to read revision number\n");
874 dev_info(info
->dev
, "MUIC device ID : 0x%x\n", id
);
876 /* Support virtual irq domain for max77843 MUIC device */
877 INIT_WORK(&info
->irq_work
, max77843_muic_irq_work
);
879 /* Clear IRQ bits before request IRQs */
880 ret
= regmap_bulk_read(max77843
->regmap_muic
,
881 MAX77843_MUIC_REG_INT1
, info
->status
,
882 MAX77843_MUIC_STATUS_NUM
);
884 dev_err(&pdev
->dev
, "Failed to Clear IRQ bits\n");
888 for (i
= 0; i
< ARRAY_SIZE(max77843_muic_irqs
); i
++) {
889 struct max77843_muic_irq
*muic_irq
= &max77843_muic_irqs
[i
];
892 virq
= regmap_irq_get_virq(max77843
->irq_data_muic
,
898 muic_irq
->virq
= virq
;
900 ret
= devm_request_threaded_irq(&pdev
->dev
, virq
, NULL
,
901 max77843_muic_irq_handler
, IRQF_NO_SUSPEND
,
902 muic_irq
->name
, info
);
905 "Failed to request irq (IRQ: %d, error: %d)\n",
911 /* Detect accessory after completing the initialization of platform */
912 INIT_DELAYED_WORK(&info
->wq_detcable
, max77843_muic_detect_cable_wq
);
913 queue_delayed_work(system_power_efficient_wq
,
914 &info
->wq_detcable
, msecs_to_jiffies(DELAY_MS_DEFAULT
));
919 regmap_del_irq_chip(max77843
->irq
, max77843
->irq_data_muic
);
920 i2c_unregister_device(max77843
->i2c_muic
);
925 static int max77843_muic_remove(struct platform_device
*pdev
)
927 struct max77843_muic_info
*info
= platform_get_drvdata(pdev
);
928 struct max77693_dev
*max77843
= info
->max77843
;
930 cancel_work_sync(&info
->irq_work
);
931 regmap_del_irq_chip(max77843
->irq
, max77843
->irq_data_muic
);
932 i2c_unregister_device(max77843
->i2c_muic
);
937 static const struct platform_device_id max77843_muic_id
[] = {
938 { "max77843-muic", },
941 MODULE_DEVICE_TABLE(platform
, max77843_muic_id
);
943 static struct platform_driver max77843_muic_driver
= {
945 .name
= "max77843-muic",
947 .probe
= max77843_muic_probe
,
948 .remove
= max77843_muic_remove
,
949 .id_table
= max77843_muic_id
,
952 static int __init
max77843_muic_init(void)
954 return platform_driver_register(&max77843_muic_driver
);
956 subsys_initcall(max77843_muic_init
);
958 MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
959 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
960 MODULE_LICENSE("GPL");