2 * fsa9480.c - FSA9480 micro USB switch device driver
4 * Copyright (C) 2010 Samsung Electronics
5 * Minkyu Kang <mk7.kang@samsung.com>
6 * Wonguk Jeong <wonguk.jeong@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 version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/platform_data/fsa9480.h>
18 #include <linux/irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/workqueue.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/pm_runtime.h>
25 /* FSA9480 I2C registers */
26 #define FSA9480_REG_DEVID 0x01
27 #define FSA9480_REG_CTRL 0x02
28 #define FSA9480_REG_INT1 0x03
29 #define FSA9480_REG_INT2 0x04
30 #define FSA9480_REG_INT1_MASK 0x05
31 #define FSA9480_REG_INT2_MASK 0x06
32 #define FSA9480_REG_ADC 0x07
33 #define FSA9480_REG_TIMING1 0x08
34 #define FSA9480_REG_TIMING2 0x09
35 #define FSA9480_REG_DEV_T1 0x0a
36 #define FSA9480_REG_DEV_T2 0x0b
37 #define FSA9480_REG_BTN1 0x0c
38 #define FSA9480_REG_BTN2 0x0d
39 #define FSA9480_REG_CK 0x0e
40 #define FSA9480_REG_CK_INT1 0x0f
41 #define FSA9480_REG_CK_INT2 0x10
42 #define FSA9480_REG_CK_INTMASK1 0x11
43 #define FSA9480_REG_CK_INTMASK2 0x12
44 #define FSA9480_REG_MANSW1 0x13
45 #define FSA9480_REG_MANSW2 0x14
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)
57 #define DEV_USB_OTG (1 << 7)
58 #define DEV_DEDICATED_CHG (1 << 6)
59 #define DEV_USB_CHG (1 << 5)
60 #define DEV_CAR_KIT (1 << 4)
61 #define DEV_UART (1 << 3)
62 #define DEV_USB (1 << 2)
63 #define DEV_AUDIO_2 (1 << 1)
64 #define DEV_AUDIO_1 (1 << 0)
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)
71 #define DEV_AV (1 << 6)
72 #define DEV_TTY (1 << 5)
73 #define DEV_PPD (1 << 4)
74 #define DEV_JIG_UART_OFF (1 << 3)
75 #define DEV_JIG_UART_ON (1 << 2)
76 #define DEV_JIG_USB_OFF (1 << 1)
77 #define DEV_JIG_USB_ON (1 << 0)
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 INT_DETACH (1 << 1)
97 #define INT_ATTACH (1 << 0)
99 struct fsa9480_usbsw
{
100 struct i2c_client
*client
;
101 struct fsa9480_platform_data
*pdata
;
107 static struct fsa9480_usbsw
*chip
;
109 static int fsa9480_write_reg(struct i2c_client
*client
,
114 ret
= i2c_smbus_write_byte_data(client
, reg
, value
);
117 dev_err(&client
->dev
, "%s: err %d\n", __func__
, ret
);
122 static int fsa9480_read_reg(struct i2c_client
*client
, int reg
)
126 ret
= i2c_smbus_read_byte_data(client
, reg
);
129 dev_err(&client
->dev
, "%s: err %d\n", __func__
, ret
);
134 static int fsa9480_read_irq(struct i2c_client
*client
, int *value
)
138 ret
= i2c_smbus_read_i2c_block_data(client
,
139 FSA9480_REG_INT1
, 2, (u8
*)value
);
143 dev_err(&client
->dev
, "%s: err %d\n", __func__
, ret
);
148 static void fsa9480_set_switch(const char *buf
)
150 struct fsa9480_usbsw
*usbsw
= chip
;
151 struct i2c_client
*client
= usbsw
->client
;
153 unsigned int path
= 0;
155 value
= fsa9480_read_reg(client
, FSA9480_REG_CTRL
);
157 if (!strncmp(buf
, "VAUDIO", 6)) {
159 value
&= ~CON_MANUAL_SW
;
160 } else if (!strncmp(buf
, "UART", 4)) {
162 value
&= ~CON_MANUAL_SW
;
163 } else if (!strncmp(buf
, "AUDIO", 5)) {
165 value
&= ~CON_MANUAL_SW
;
166 } else if (!strncmp(buf
, "DHOST", 5)) {
168 value
&= ~CON_MANUAL_SW
;
169 } else if (!strncmp(buf
, "AUTO", 4)) {
171 value
|= CON_MANUAL_SW
;
173 printk(KERN_ERR
"Wrong command\n");
178 fsa9480_write_reg(client
, FSA9480_REG_MANSW1
, path
);
179 fsa9480_write_reg(client
, FSA9480_REG_CTRL
, value
);
182 static ssize_t
fsa9480_get_switch(char *buf
)
184 struct fsa9480_usbsw
*usbsw
= chip
;
185 struct i2c_client
*client
= usbsw
->client
;
188 value
= fsa9480_read_reg(client
, FSA9480_REG_MANSW1
);
190 if (value
== SW_VAUDIO
)
191 return sprintf(buf
, "VAUDIO\n");
192 else if (value
== SW_UART
)
193 return sprintf(buf
, "UART\n");
194 else if (value
== SW_AUDIO
)
195 return sprintf(buf
, "AUDIO\n");
196 else if (value
== SW_DHOST
)
197 return sprintf(buf
, "DHOST\n");
198 else if (value
== SW_AUTO
)
199 return sprintf(buf
, "AUTO\n");
201 return sprintf(buf
, "%x", value
);
204 static ssize_t
fsa9480_show_device(struct device
*dev
,
205 struct device_attribute
*attr
,
208 struct fsa9480_usbsw
*usbsw
= dev_get_drvdata(dev
);
209 struct i2c_client
*client
= usbsw
->client
;
212 dev1
= fsa9480_read_reg(client
, FSA9480_REG_DEV_T1
);
213 dev2
= fsa9480_read_reg(client
, FSA9480_REG_DEV_T2
);
216 return sprintf(buf
, "NONE\n");
219 if (dev1
& DEV_T1_USB_MASK
|| dev2
& DEV_T2_USB_MASK
)
220 return sprintf(buf
, "USB\n");
223 if (dev1
& DEV_T1_UART_MASK
|| dev2
& DEV_T2_UART_MASK
)
224 return sprintf(buf
, "UART\n");
227 if (dev1
& DEV_T1_CHARGER_MASK
)
228 return sprintf(buf
, "CHARGER\n");
231 if (dev2
& DEV_T2_JIG_MASK
)
232 return sprintf(buf
, "JIG\n");
234 return sprintf(buf
, "UNKNOWN\n");
237 static ssize_t
fsa9480_show_manualsw(struct device
*dev
,
238 struct device_attribute
*attr
, char *buf
)
240 return fsa9480_get_switch(buf
);
244 static ssize_t
fsa9480_set_manualsw(struct device
*dev
,
245 struct device_attribute
*attr
,
246 const char *buf
, size_t count
)
248 fsa9480_set_switch(buf
);
253 static DEVICE_ATTR(device
, S_IRUGO
, fsa9480_show_device
, NULL
);
254 static DEVICE_ATTR(switch, S_IRUGO
| S_IWUSR
,
255 fsa9480_show_manualsw
, fsa9480_set_manualsw
);
257 static struct attribute
*fsa9480_attributes
[] = {
258 &dev_attr_device
.attr
,
259 &dev_attr_switch
.attr
,
263 static const struct attribute_group fsa9480_group
= {
264 .attrs
= fsa9480_attributes
,
267 static void fsa9480_detect_dev(struct fsa9480_usbsw
*usbsw
, int intr
)
269 int val1
, val2
, ctrl
;
270 struct fsa9480_platform_data
*pdata
= usbsw
->pdata
;
271 struct i2c_client
*client
= usbsw
->client
;
273 val1
= fsa9480_read_reg(client
, FSA9480_REG_DEV_T1
);
274 val2
= fsa9480_read_reg(client
, FSA9480_REG_DEV_T2
);
275 ctrl
= fsa9480_read_reg(client
, FSA9480_REG_CTRL
);
277 dev_info(&client
->dev
, "intr: 0x%x, dev1: 0x%x, dev2: 0x%x\n",
283 if (intr
& INT_ATTACH
) { /* Attached */
285 if (val1
& DEV_T1_USB_MASK
|| val2
& DEV_T2_USB_MASK
) {
287 pdata
->usb_cb(FSA9480_ATTACHED
);
290 fsa9480_write_reg(client
,
291 FSA9480_REG_MANSW1
, usbsw
->mansw
);
296 if (val1
& DEV_T1_UART_MASK
|| val2
& DEV_T2_UART_MASK
) {
298 pdata
->uart_cb(FSA9480_ATTACHED
);
300 if (!(ctrl
& CON_MANUAL_SW
)) {
301 fsa9480_write_reg(client
,
302 FSA9480_REG_MANSW1
, SW_UART
);
307 if (val1
& DEV_T1_CHARGER_MASK
) {
308 if (pdata
->charger_cb
)
309 pdata
->charger_cb(FSA9480_ATTACHED
);
313 if (val2
& DEV_T2_JIG_MASK
) {
315 pdata
->jig_cb(FSA9480_ATTACHED
);
317 } else if (intr
& INT_DETACH
) { /* Detached */
319 if (usbsw
->dev1
& DEV_T1_USB_MASK
||
320 usbsw
->dev2
& DEV_T2_USB_MASK
) {
322 pdata
->usb_cb(FSA9480_DETACHED
);
326 if (usbsw
->dev1
& DEV_T1_UART_MASK
||
327 usbsw
->dev2
& DEV_T2_UART_MASK
) {
329 pdata
->uart_cb(FSA9480_DETACHED
);
333 if (usbsw
->dev1
& DEV_T1_CHARGER_MASK
) {
334 if (pdata
->charger_cb
)
335 pdata
->charger_cb(FSA9480_DETACHED
);
339 if (usbsw
->dev2
& DEV_T2_JIG_MASK
) {
341 pdata
->jig_cb(FSA9480_DETACHED
);
349 ctrl
&= ~CON_INT_MASK
;
350 fsa9480_write_reg(client
, FSA9480_REG_CTRL
, ctrl
);
353 static irqreturn_t
fsa9480_irq_handler(int irq
, void *data
)
355 struct fsa9480_usbsw
*usbsw
= data
;
356 struct i2c_client
*client
= usbsw
->client
;
359 /* clear interrupt */
360 fsa9480_read_irq(client
, &intr
);
362 /* device detection */
363 fsa9480_detect_dev(usbsw
, intr
);
368 static int fsa9480_irq_init(struct fsa9480_usbsw
*usbsw
)
370 struct fsa9480_platform_data
*pdata
= usbsw
->pdata
;
371 struct i2c_client
*client
= usbsw
->client
;
374 unsigned int ctrl
= CON_MASK
;
376 /* clear interrupt */
377 fsa9480_read_irq(client
, &intr
);
379 /* unmask interrupt (attach/detach only) */
380 fsa9480_write_reg(client
, FSA9480_REG_INT1_MASK
, 0xfc);
381 fsa9480_write_reg(client
, FSA9480_REG_INT2_MASK
, 0x1f);
383 usbsw
->mansw
= fsa9480_read_reg(client
, FSA9480_REG_MANSW1
);
386 ctrl
&= ~CON_MANUAL_SW
; /* Manual Switching Mode */
388 fsa9480_write_reg(client
, FSA9480_REG_CTRL
, ctrl
);
390 if (pdata
&& pdata
->cfg_gpio
)
394 ret
= request_threaded_irq(client
->irq
, NULL
,
396 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
397 "fsa9480 micro USB", usbsw
);
399 dev_err(&client
->dev
, "failed to reqeust IRQ\n");
403 device_init_wakeup(&client
->dev
, pdata
->wakeup
);
409 static int __devinit
fsa9480_probe(struct i2c_client
*client
,
410 const struct i2c_device_id
*id
)
412 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
413 struct fsa9480_usbsw
*usbsw
;
416 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
419 usbsw
= kzalloc(sizeof(struct fsa9480_usbsw
), GFP_KERNEL
);
421 dev_err(&client
->dev
, "failed to allocate driver data\n");
425 usbsw
->client
= client
;
426 usbsw
->pdata
= client
->dev
.platform_data
;
430 i2c_set_clientdata(client
, usbsw
);
432 ret
= fsa9480_irq_init(usbsw
);
436 ret
= sysfs_create_group(&client
->dev
.kobj
, &fsa9480_group
);
438 dev_err(&client
->dev
,
439 "failed to create fsa9480 attribute group\n");
443 /* ADC Detect Time: 500ms */
444 fsa9480_write_reg(client
, FSA9480_REG_TIMING1
, 0x6);
446 if (chip
->pdata
->reset_cb
)
447 chip
->pdata
->reset_cb();
449 /* device detection */
450 fsa9480_detect_dev(usbsw
, INT_ATTACH
);
452 pm_runtime_set_active(&client
->dev
);
458 free_irq(client
->irq
, usbsw
);
460 i2c_set_clientdata(client
, NULL
);
465 static int __devexit
fsa9480_remove(struct i2c_client
*client
)
467 struct fsa9480_usbsw
*usbsw
= i2c_get_clientdata(client
);
469 free_irq(client
->irq
, usbsw
);
470 i2c_set_clientdata(client
, NULL
);
472 sysfs_remove_group(&client
->dev
.kobj
, &fsa9480_group
);
473 device_init_wakeup(&client
->dev
, 0);
480 static int fsa9480_suspend(struct i2c_client
*client
, pm_message_t state
)
482 struct fsa9480_usbsw
*usbsw
= i2c_get_clientdata(client
);
483 struct fsa9480_platform_data
*pdata
= usbsw
->pdata
;
485 if (device_may_wakeup(&client
->dev
) && client
->irq
)
486 enable_irq_wake(client
->irq
);
488 if (pdata
->usb_power
)
494 static int fsa9480_resume(struct i2c_client
*client
)
496 struct fsa9480_usbsw
*usbsw
= i2c_get_clientdata(client
);
499 if (device_may_wakeup(&client
->dev
) && client
->irq
)
500 disable_irq_wake(client
->irq
);
503 * Clear Pending interrupt. Note that detect_dev does what
504 * the interrupt handler does. So, we don't miss pending and
505 * we reenable interrupt if there is one.
507 fsa9480_read_reg(client
, FSA9480_REG_INT1
);
508 fsa9480_read_reg(client
, FSA9480_REG_INT2
);
510 dev1
= fsa9480_read_reg(client
, FSA9480_REG_DEV_T1
);
511 dev2
= fsa9480_read_reg(client
, FSA9480_REG_DEV_T2
);
513 /* device detection */
514 fsa9480_detect_dev(usbsw
, (dev1
|| dev2
) ? INT_ATTACH
: INT_DETACH
);
521 #define fsa9480_suspend NULL
522 #define fsa9480_resume NULL
524 #endif /* CONFIG_PM */
526 static const struct i2c_device_id fsa9480_id
[] = {
530 MODULE_DEVICE_TABLE(i2c
, fsa9480_id
);
532 static struct i2c_driver fsa9480_i2c_driver
= {
536 .probe
= fsa9480_probe
,
537 .remove
= __devexit_p(fsa9480_remove
),
538 .resume
= fsa9480_resume
,
539 .suspend
= fsa9480_suspend
,
540 .id_table
= fsa9480_id
,
543 static int __init
fsa9480_init(void)
545 return i2c_add_driver(&fsa9480_i2c_driver
);
547 module_init(fsa9480_init
);
549 static void __exit
fsa9480_exit(void)
551 i2c_del_driver(&fsa9480_i2c_driver
);
553 module_exit(fsa9480_exit
);
555 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
556 MODULE_DESCRIPTION("FSA9480 USB Switch driver");
557 MODULE_LICENSE("GPL");