1 // SPDX-License-Identifier: GPL-2.0+
3 * extcon-fsa9480.c - Fairchild Semiconductor FSA9480 extcon driver
5 * Copyright (c) 2019 Tomasz Figa <tomasz.figa@gmail.com>
7 * Loosely based on old fsa9480 misc-device driver.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/i2c.h>
14 #include <linux/slab.h>
15 #include <linux/bitops.h>
16 #include <linux/interrupt.h>
17 #include <linux/err.h>
18 #include <linux/platform_device.h>
19 #include <linux/kobject.h>
20 #include <linux/extcon-provider.h>
21 #include <linux/irqdomain.h>
22 #include <linux/regmap.h>
24 /* FSA9480 I2C registers */
25 #define FSA9480_REG_DEVID 0x01
26 #define FSA9480_REG_CTRL 0x02
27 #define FSA9480_REG_INT1 0x03
28 #define FSA9480_REG_INT2 0x04
29 #define FSA9480_REG_INT1_MASK 0x05
30 #define FSA9480_REG_INT2_MASK 0x06
31 #define FSA9480_REG_ADC 0x07
32 #define FSA9480_REG_TIMING1 0x08
33 #define FSA9480_REG_TIMING2 0x09
34 #define FSA9480_REG_DEV_T1 0x0a
35 #define FSA9480_REG_DEV_T2 0x0b
36 #define FSA9480_REG_BTN1 0x0c
37 #define FSA9480_REG_BTN2 0x0d
38 #define FSA9480_REG_CK 0x0e
39 #define FSA9480_REG_CK_INT1 0x0f
40 #define FSA9480_REG_CK_INT2 0x10
41 #define FSA9480_REG_CK_INTMASK1 0x11
42 #define FSA9480_REG_CK_INTMASK2 0x12
43 #define FSA9480_REG_MANSW1 0x13
44 #define FSA9480_REG_MANSW2 0x14
45 #define FSA9480_REG_END 0x15
48 #define CON_SWITCH_OPEN (1 << 4)
49 #define CON_RAW_DATA (1 << 3)
50 #define CON_MANUAL_SW (1 << 2)
51 #define CON_WAIT (1 << 1)
52 #define CON_INT_MASK (1 << 0)
53 #define CON_MASK (CON_SWITCH_OPEN | CON_RAW_DATA | \
54 CON_MANUAL_SW | CON_WAIT)
58 #define DEV_DEDICATED_CHG 6
66 #define DEV_T1_USB_MASK (DEV_USB_OTG | DEV_USB)
67 #define DEV_T1_UART_MASK (DEV_UART)
68 #define DEV_T1_CHARGER_MASK (DEV_DEDICATED_CHG | DEV_USB_CHG)
74 #define DEV_JIG_UART_OFF 11
75 #define DEV_JIG_UART_ON 10
76 #define DEV_JIG_USB_OFF 9
77 #define DEV_JIG_USB_ON 8
79 #define DEV_T2_USB_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON)
80 #define DEV_T2_UART_MASK (DEV_JIG_UART_OFF | DEV_JIG_UART_ON)
81 #define DEV_T2_JIG_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON | \
82 DEV_JIG_UART_OFF | DEV_JIG_UART_ON)
87 * 000: Open all / 001: USB / 010: AUDIO / 011: UART / 100: V_AUDIO
89 #define SW_VAUDIO ((4 << 5) | (4 << 2))
90 #define SW_UART ((3 << 5) | (3 << 2))
91 #define SW_AUDIO ((2 << 5) | (2 << 2))
92 #define SW_DHOST ((1 << 5) | (1 << 2))
93 #define SW_AUTO ((0 << 5) | (0 << 2))
96 #define INT1_MASK (0xff << 0)
97 #define INT_DETACH (1 << 1)
98 #define INT_ATTACH (1 << 0)
100 /* Interrupt 2 mask */
101 #define INT2_MASK (0x1f << 0)
104 #define TIMING1_ADC_500MS (0x6 << 0)
106 struct fsa9480_usbsw
{
108 struct regmap
*regmap
;
109 struct extcon_dev
*edev
;
113 static const unsigned int fsa9480_extcon_cable
[] = {
119 EXTCON_JACK_LINE_OUT
,
120 EXTCON_JACK_VIDEO_OUT
,
126 static const u64 cable_types
[] = {
127 [DEV_USB_OTG
] = BIT_ULL(EXTCON_USB_HOST
),
128 [DEV_DEDICATED_CHG
] = BIT_ULL(EXTCON_USB
) | BIT_ULL(EXTCON_CHG_USB_DCP
),
129 [DEV_USB_CHG
] = BIT_ULL(EXTCON_USB
) | BIT_ULL(EXTCON_CHG_USB_SDP
),
130 [DEV_CAR_KIT
] = BIT_ULL(EXTCON_USB
) | BIT_ULL(EXTCON_CHG_USB_SDP
)
131 | BIT_ULL(EXTCON_JACK_LINE_OUT
),
132 [DEV_UART
] = BIT_ULL(EXTCON_JIG
),
133 [DEV_USB
] = BIT_ULL(EXTCON_USB
) | BIT_ULL(EXTCON_CHG_USB_SDP
),
134 [DEV_AUDIO_2
] = BIT_ULL(EXTCON_JACK_LINE_OUT
),
135 [DEV_AUDIO_1
] = BIT_ULL(EXTCON_JACK_LINE_OUT
),
136 [DEV_AV
] = BIT_ULL(EXTCON_JACK_LINE_OUT
)
137 | BIT_ULL(EXTCON_JACK_VIDEO_OUT
),
138 [DEV_TTY
] = BIT_ULL(EXTCON_JIG
),
139 [DEV_PPD
] = BIT_ULL(EXTCON_JACK_LINE_OUT
) | BIT_ULL(EXTCON_CHG_USB_ACA
),
140 [DEV_JIG_UART_OFF
] = BIT_ULL(EXTCON_JIG
),
141 [DEV_JIG_UART_ON
] = BIT_ULL(EXTCON_JIG
),
142 [DEV_JIG_USB_OFF
] = BIT_ULL(EXTCON_USB
) | BIT_ULL(EXTCON_JIG
),
143 [DEV_JIG_USB_ON
] = BIT_ULL(EXTCON_USB
) | BIT_ULL(EXTCON_JIG
),
146 /* Define regmap configuration of FSA9480 for I2C communication */
147 static bool fsa9480_volatile_reg(struct device
*dev
, unsigned int reg
)
150 case FSA9480_REG_INT1_MASK
:
158 static const struct regmap_config fsa9480_regmap_config
= {
161 .volatile_reg
= fsa9480_volatile_reg
,
162 .max_register
= FSA9480_REG_END
,
165 static int fsa9480_write_reg(struct fsa9480_usbsw
*usbsw
, int reg
, int value
)
169 ret
= regmap_write(usbsw
->regmap
, reg
, value
);
171 dev_err(usbsw
->dev
, "%s: err %d\n", __func__
, ret
);
176 static int fsa9480_read_reg(struct fsa9480_usbsw
*usbsw
, int reg
)
180 ret
= regmap_read(usbsw
->regmap
, reg
, &val
);
182 dev_err(usbsw
->dev
, "%s: err %d\n", __func__
, ret
);
189 static int fsa9480_read_irq(struct fsa9480_usbsw
*usbsw
, int *value
)
194 ret
= regmap_bulk_read(usbsw
->regmap
, FSA9480_REG_INT1
, regs
, 2);
196 dev_err(usbsw
->dev
, "%s: err %d\n", __func__
, ret
);
198 *value
= regs
[1] << 8 | regs
[0];
202 static void fsa9480_handle_change(struct fsa9480_usbsw
*usbsw
,
203 u16 mask
, bool attached
)
206 int dev
= fls64(mask
) - 1;
207 u64 cables
= cable_types
[dev
];
210 int cable
= fls64(cables
) - 1;
212 extcon_set_state_sync(usbsw
->edev
, cable
, attached
);
213 cables
&= ~BIT_ULL(cable
);
216 mask
&= ~BIT_ULL(dev
);
220 static void fsa9480_detect_dev(struct fsa9480_usbsw
*usbsw
)
225 val1
= fsa9480_read_reg(usbsw
, FSA9480_REG_DEV_T1
);
226 val2
= fsa9480_read_reg(usbsw
, FSA9480_REG_DEV_T2
);
227 if (val1
< 0 || val2
< 0) {
228 dev_err(usbsw
->dev
, "%s: failed to read registers", __func__
);
231 val
= val2
<< 8 | val1
;
233 dev_info(usbsw
->dev
, "dev1: 0x%x, dev2: 0x%x\n", val1
, val2
);
235 /* handle detached cables first */
236 fsa9480_handle_change(usbsw
, usbsw
->cable
& ~val
, false);
238 /* then handle attached ones */
239 fsa9480_handle_change(usbsw
, val
& ~usbsw
->cable
, true);
244 static irqreturn_t
fsa9480_irq_handler(int irq
, void *data
)
246 struct fsa9480_usbsw
*usbsw
= data
;
249 /* clear interrupt */
250 fsa9480_read_irq(usbsw
, &intr
);
254 /* device detection */
255 fsa9480_detect_dev(usbsw
);
260 static int fsa9480_probe(struct i2c_client
*client
,
261 const struct i2c_device_id
*id
)
263 struct fsa9480_usbsw
*info
;
267 dev_err(&client
->dev
, "no interrupt provided\n");
271 info
= devm_kzalloc(&client
->dev
, sizeof(*info
), GFP_KERNEL
);
274 info
->dev
= &client
->dev
;
276 i2c_set_clientdata(client
, info
);
278 /* External connector */
279 info
->edev
= devm_extcon_dev_allocate(info
->dev
,
280 fsa9480_extcon_cable
);
281 if (IS_ERR(info
->edev
)) {
282 dev_err(info
->dev
, "failed to allocate memory for extcon\n");
287 ret
= devm_extcon_dev_register(info
->dev
, info
->edev
);
289 dev_err(info
->dev
, "failed to register extcon device\n");
293 info
->regmap
= devm_regmap_init_i2c(client
, &fsa9480_regmap_config
);
294 if (IS_ERR(info
->regmap
)) {
295 ret
= PTR_ERR(info
->regmap
);
296 dev_err(info
->dev
, "failed to allocate register map: %d\n",
301 /* ADC Detect Time: 500ms */
302 fsa9480_write_reg(info
, FSA9480_REG_TIMING1
, TIMING1_ADC_500MS
);
304 /* configure automatic switching */
305 fsa9480_write_reg(info
, FSA9480_REG_CTRL
, CON_MASK
);
307 /* unmask interrupt (attach/detach only) */
308 fsa9480_write_reg(info
, FSA9480_REG_INT1_MASK
,
309 INT1_MASK
& ~(INT_ATTACH
| INT_DETACH
));
310 fsa9480_write_reg(info
, FSA9480_REG_INT2_MASK
, INT2_MASK
);
312 ret
= devm_request_threaded_irq(info
->dev
, client
->irq
, NULL
,
314 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
317 dev_err(info
->dev
, "failed to request IRQ\n");
321 device_init_wakeup(info
->dev
, true);
322 fsa9480_detect_dev(info
);
327 static int fsa9480_remove(struct i2c_client
*client
)
332 #ifdef CONFIG_PM_SLEEP
333 static int fsa9480_suspend(struct device
*dev
)
335 struct i2c_client
*client
= to_i2c_client(dev
);
337 if (device_may_wakeup(&client
->dev
) && client
->irq
)
338 enable_irq_wake(client
->irq
);
343 static int fsa9480_resume(struct device
*dev
)
345 struct i2c_client
*client
= to_i2c_client(dev
);
347 if (device_may_wakeup(&client
->dev
) && client
->irq
)
348 disable_irq_wake(client
->irq
);
354 static const struct dev_pm_ops fsa9480_pm_ops
= {
355 SET_SYSTEM_SLEEP_PM_OPS(fsa9480_suspend
, fsa9480_resume
)
358 static const struct i2c_device_id fsa9480_id
[] = {
362 MODULE_DEVICE_TABLE(i2c
, fsa9480_id
);
364 static const struct of_device_id fsa9480_of_match
[] = {
365 { .compatible
= "fcs,fsa9480", },
366 { .compatible
= "fcs,fsa880", },
369 MODULE_DEVICE_TABLE(of
, fsa9480_of_match
);
371 static struct i2c_driver fsa9480_i2c_driver
= {
374 .pm
= &fsa9480_pm_ops
,
375 .of_match_table
= fsa9480_of_match
,
377 .probe
= fsa9480_probe
,
378 .remove
= fsa9480_remove
,
379 .id_table
= fsa9480_id
,
382 static int __init
fsa9480_module_init(void)
384 return i2c_add_driver(&fsa9480_i2c_driver
);
386 subsys_initcall(fsa9480_module_init
);
388 static void __exit
fsa9480_module_exit(void)
390 i2c_del_driver(&fsa9480_i2c_driver
);
392 module_exit(fsa9480_module_exit
);
394 MODULE_DESCRIPTION("Fairchild Semiconductor FSA9480 extcon driver");
395 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
396 MODULE_LICENSE("GPL");