blob: fab02f2da077ecbe9207384d34a4e6c2b9b0e1fb [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Donggeun Kima1bb73d2011-07-25 17:13:07 -07002/*
3 * fsa9480.c - FSA9480 micro USB switch device driver
4 *
5 * Copyright (C) 2010 Samsung Electronics
6 * Minkyu Kang <mk7.kang@samsung.com>
7 * Wonguk Jeong <wonguk.jeong@samsung.com>
Donggeun Kima1bb73d2011-07-25 17:13:07 -07008 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/err.h>
13#include <linux/i2c.h>
14#include <linux/platform_data/fsa9480.h>
15#include <linux/irq.h>
16#include <linux/interrupt.h>
17#include <linux/workqueue.h>
18#include <linux/platform_device.h>
19#include <linux/slab.h>
20#include <linux/pm_runtime.h>
21
22/* FSA9480 I2C registers */
23#define FSA9480_REG_DEVID 0x01
24#define FSA9480_REG_CTRL 0x02
25#define FSA9480_REG_INT1 0x03
26#define FSA9480_REG_INT2 0x04
27#define FSA9480_REG_INT1_MASK 0x05
28#define FSA9480_REG_INT2_MASK 0x06
29#define FSA9480_REG_ADC 0x07
30#define FSA9480_REG_TIMING1 0x08
31#define FSA9480_REG_TIMING2 0x09
32#define FSA9480_REG_DEV_T1 0x0a
33#define FSA9480_REG_DEV_T2 0x0b
34#define FSA9480_REG_BTN1 0x0c
35#define FSA9480_REG_BTN2 0x0d
36#define FSA9480_REG_CK 0x0e
37#define FSA9480_REG_CK_INT1 0x0f
38#define FSA9480_REG_CK_INT2 0x10
39#define FSA9480_REG_CK_INTMASK1 0x11
40#define FSA9480_REG_CK_INTMASK2 0x12
41#define FSA9480_REG_MANSW1 0x13
42#define FSA9480_REG_MANSW2 0x14
43
44/* Control */
45#define CON_SWITCH_OPEN (1 << 4)
46#define CON_RAW_DATA (1 << 3)
47#define CON_MANUAL_SW (1 << 2)
48#define CON_WAIT (1 << 1)
49#define CON_INT_MASK (1 << 0)
50#define CON_MASK (CON_SWITCH_OPEN | CON_RAW_DATA | \
51 CON_MANUAL_SW | CON_WAIT)
52
53/* Device Type 1 */
54#define DEV_USB_OTG (1 << 7)
55#define DEV_DEDICATED_CHG (1 << 6)
56#define DEV_USB_CHG (1 << 5)
57#define DEV_CAR_KIT (1 << 4)
58#define DEV_UART (1 << 3)
59#define DEV_USB (1 << 2)
60#define DEV_AUDIO_2 (1 << 1)
61#define DEV_AUDIO_1 (1 << 0)
62
63#define DEV_T1_USB_MASK (DEV_USB_OTG | DEV_USB)
64#define DEV_T1_UART_MASK (DEV_UART)
65#define DEV_T1_CHARGER_MASK (DEV_DEDICATED_CHG | DEV_USB_CHG)
66
67/* Device Type 2 */
68#define DEV_AV (1 << 6)
69#define DEV_TTY (1 << 5)
70#define DEV_PPD (1 << 4)
71#define DEV_JIG_UART_OFF (1 << 3)
72#define DEV_JIG_UART_ON (1 << 2)
73#define DEV_JIG_USB_OFF (1 << 1)
74#define DEV_JIG_USB_ON (1 << 0)
75
76#define DEV_T2_USB_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON)
77#define DEV_T2_UART_MASK (DEV_JIG_UART_OFF | DEV_JIG_UART_ON)
78#define DEV_T2_JIG_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON | \
79 DEV_JIG_UART_OFF | DEV_JIG_UART_ON)
80
81/*
82 * Manual Switch
83 * D- [7:5] / D+ [4:2]
84 * 000: Open all / 001: USB / 010: AUDIO / 011: UART / 100: V_AUDIO
85 */
86#define SW_VAUDIO ((4 << 5) | (4 << 2))
87#define SW_UART ((3 << 5) | (3 << 2))
88#define SW_AUDIO ((2 << 5) | (2 << 2))
89#define SW_DHOST ((1 << 5) | (1 << 2))
90#define SW_AUTO ((0 << 5) | (0 << 2))
91
92/* Interrupt 1 */
93#define INT_DETACH (1 << 1)
94#define INT_ATTACH (1 << 0)
95
96struct fsa9480_usbsw {
97 struct i2c_client *client;
98 struct fsa9480_platform_data *pdata;
99 int dev1;
100 int dev2;
101 int mansw;
102};
103
104static struct fsa9480_usbsw *chip;
105
106static int fsa9480_write_reg(struct i2c_client *client,
107 int reg, int value)
108{
109 int ret;
110
111 ret = i2c_smbus_write_byte_data(client, reg, value);
112
113 if (ret < 0)
114 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
115
116 return ret;
117}
118
119static int fsa9480_read_reg(struct i2c_client *client, int reg)
120{
121 int ret;
122
123 ret = i2c_smbus_read_byte_data(client, reg);
124
125 if (ret < 0)
126 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
127
128 return ret;
129}
130
131static int fsa9480_read_irq(struct i2c_client *client, int *value)
132{
133 int ret;
134
135 ret = i2c_smbus_read_i2c_block_data(client,
136 FSA9480_REG_INT1, 2, (u8 *)value);
137 *value &= 0xffff;
138
139 if (ret < 0)
140 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
141
142 return ret;
143}
144
145static void fsa9480_set_switch(const char *buf)
146{
147 struct fsa9480_usbsw *usbsw = chip;
148 struct i2c_client *client = usbsw->client;
149 unsigned int value;
150 unsigned int path = 0;
151
152 value = fsa9480_read_reg(client, FSA9480_REG_CTRL);
153
154 if (!strncmp(buf, "VAUDIO", 6)) {
155 path = SW_VAUDIO;
156 value &= ~CON_MANUAL_SW;
157 } else if (!strncmp(buf, "UART", 4)) {
158 path = SW_UART;
159 value &= ~CON_MANUAL_SW;
160 } else if (!strncmp(buf, "AUDIO", 5)) {
161 path = SW_AUDIO;
162 value &= ~CON_MANUAL_SW;
163 } else if (!strncmp(buf, "DHOST", 5)) {
164 path = SW_DHOST;
165 value &= ~CON_MANUAL_SW;
166 } else if (!strncmp(buf, "AUTO", 4)) {
167 path = SW_AUTO;
168 value |= CON_MANUAL_SW;
169 } else {
170 printk(KERN_ERR "Wrong command\n");
171 return;
172 }
173
174 usbsw->mansw = path;
175 fsa9480_write_reg(client, FSA9480_REG_MANSW1, path);
176 fsa9480_write_reg(client, FSA9480_REG_CTRL, value);
177}
178
179static ssize_t fsa9480_get_switch(char *buf)
180{
181 struct fsa9480_usbsw *usbsw = chip;
182 struct i2c_client *client = usbsw->client;
183 unsigned int value;
184
185 value = fsa9480_read_reg(client, FSA9480_REG_MANSW1);
186
187 if (value == SW_VAUDIO)
188 return sprintf(buf, "VAUDIO\n");
189 else if (value == SW_UART)
190 return sprintf(buf, "UART\n");
191 else if (value == SW_AUDIO)
192 return sprintf(buf, "AUDIO\n");
193 else if (value == SW_DHOST)
194 return sprintf(buf, "DHOST\n");
195 else if (value == SW_AUTO)
196 return sprintf(buf, "AUTO\n");
197 else
198 return sprintf(buf, "%x", value);
199}
200
201static ssize_t fsa9480_show_device(struct device *dev,
202 struct device_attribute *attr,
203 char *buf)
204{
205 struct fsa9480_usbsw *usbsw = dev_get_drvdata(dev);
206 struct i2c_client *client = usbsw->client;
207 int dev1, dev2;
208
209 dev1 = fsa9480_read_reg(client, FSA9480_REG_DEV_T1);
210 dev2 = fsa9480_read_reg(client, FSA9480_REG_DEV_T2);
211
212 if (!dev1 && !dev2)
213 return sprintf(buf, "NONE\n");
214
215 /* USB */
216 if (dev1 & DEV_T1_USB_MASK || dev2 & DEV_T2_USB_MASK)
217 return sprintf(buf, "USB\n");
218
219 /* UART */
220 if (dev1 & DEV_T1_UART_MASK || dev2 & DEV_T2_UART_MASK)
221 return sprintf(buf, "UART\n");
222
223 /* CHARGER */
224 if (dev1 & DEV_T1_CHARGER_MASK)
225 return sprintf(buf, "CHARGER\n");
226
227 /* JIG */
228 if (dev2 & DEV_T2_JIG_MASK)
229 return sprintf(buf, "JIG\n");
230
231 return sprintf(buf, "UNKNOWN\n");
232}
233
234static ssize_t fsa9480_show_manualsw(struct device *dev,
235 struct device_attribute *attr, char *buf)
236{
237 return fsa9480_get_switch(buf);
238
239}
240
241static ssize_t fsa9480_set_manualsw(struct device *dev,
242 struct device_attribute *attr,
243 const char *buf, size_t count)
244{
245 fsa9480_set_switch(buf);
246
247 return count;
248}
249
250static DEVICE_ATTR(device, S_IRUGO, fsa9480_show_device, NULL);
251static DEVICE_ATTR(switch, S_IRUGO | S_IWUSR,
252 fsa9480_show_manualsw, fsa9480_set_manualsw);
253
254static struct attribute *fsa9480_attributes[] = {
255 &dev_attr_device.attr,
256 &dev_attr_switch.attr,
257 NULL
258};
259
260static const struct attribute_group fsa9480_group = {
261 .attrs = fsa9480_attributes,
262};
263
264static void fsa9480_detect_dev(struct fsa9480_usbsw *usbsw, int intr)
265{
266 int val1, val2, ctrl;
267 struct fsa9480_platform_data *pdata = usbsw->pdata;
268 struct i2c_client *client = usbsw->client;
269
270 val1 = fsa9480_read_reg(client, FSA9480_REG_DEV_T1);
271 val2 = fsa9480_read_reg(client, FSA9480_REG_DEV_T2);
272 ctrl = fsa9480_read_reg(client, FSA9480_REG_CTRL);
273
274 dev_info(&client->dev, "intr: 0x%x, dev1: 0x%x, dev2: 0x%x\n",
275 intr, val1, val2);
276
277 if (!intr)
278 goto out;
279
280 if (intr & INT_ATTACH) { /* Attached */
281 /* USB */
282 if (val1 & DEV_T1_USB_MASK || val2 & DEV_T2_USB_MASK) {
283 if (pdata->usb_cb)
284 pdata->usb_cb(FSA9480_ATTACHED);
285
286 if (usbsw->mansw) {
287 fsa9480_write_reg(client,
288 FSA9480_REG_MANSW1, usbsw->mansw);
289 }
290 }
291
292 /* UART */
293 if (val1 & DEV_T1_UART_MASK || val2 & DEV_T2_UART_MASK) {
294 if (pdata->uart_cb)
295 pdata->uart_cb(FSA9480_ATTACHED);
296
297 if (!(ctrl & CON_MANUAL_SW)) {
298 fsa9480_write_reg(client,
299 FSA9480_REG_MANSW1, SW_UART);
300 }
301 }
302
303 /* CHARGER */
304 if (val1 & DEV_T1_CHARGER_MASK) {
305 if (pdata->charger_cb)
306 pdata->charger_cb(FSA9480_ATTACHED);
307 }
308
309 /* JIG */
310 if (val2 & DEV_T2_JIG_MASK) {
311 if (pdata->jig_cb)
312 pdata->jig_cb(FSA9480_ATTACHED);
313 }
314 } else if (intr & INT_DETACH) { /* Detached */
315 /* USB */
316 if (usbsw->dev1 & DEV_T1_USB_MASK ||
317 usbsw->dev2 & DEV_T2_USB_MASK) {
318 if (pdata->usb_cb)
319 pdata->usb_cb(FSA9480_DETACHED);
320 }
321
322 /* UART */
323 if (usbsw->dev1 & DEV_T1_UART_MASK ||
324 usbsw->dev2 & DEV_T2_UART_MASK) {
325 if (pdata->uart_cb)
326 pdata->uart_cb(FSA9480_DETACHED);
327 }
328
329 /* CHARGER */
330 if (usbsw->dev1 & DEV_T1_CHARGER_MASK) {
331 if (pdata->charger_cb)
332 pdata->charger_cb(FSA9480_DETACHED);
333 }
334
335 /* JIG */
336 if (usbsw->dev2 & DEV_T2_JIG_MASK) {
337 if (pdata->jig_cb)
338 pdata->jig_cb(FSA9480_DETACHED);
339 }
340 }
341
342 usbsw->dev1 = val1;
343 usbsw->dev2 = val2;
344
345out:
346 ctrl &= ~CON_INT_MASK;
347 fsa9480_write_reg(client, FSA9480_REG_CTRL, ctrl);
348}
349
350static irqreturn_t fsa9480_irq_handler(int irq, void *data)
351{
352 struct fsa9480_usbsw *usbsw = data;
353 struct i2c_client *client = usbsw->client;
354 int intr;
355
356 /* clear interrupt */
357 fsa9480_read_irq(client, &intr);
358
359 /* device detection */
360 fsa9480_detect_dev(usbsw, intr);
361
362 return IRQ_HANDLED;
363}
364
365static int fsa9480_irq_init(struct fsa9480_usbsw *usbsw)
366{
367 struct fsa9480_platform_data *pdata = usbsw->pdata;
368 struct i2c_client *client = usbsw->client;
369 int ret;
370 int intr;
371 unsigned int ctrl = CON_MASK;
372
373 /* clear interrupt */
374 fsa9480_read_irq(client, &intr);
375
376 /* unmask interrupt (attach/detach only) */
377 fsa9480_write_reg(client, FSA9480_REG_INT1_MASK, 0xfc);
378 fsa9480_write_reg(client, FSA9480_REG_INT2_MASK, 0x1f);
379
380 usbsw->mansw = fsa9480_read_reg(client, FSA9480_REG_MANSW1);
381
382 if (usbsw->mansw)
383 ctrl &= ~CON_MANUAL_SW; /* Manual Switching Mode */
384
385 fsa9480_write_reg(client, FSA9480_REG_CTRL, ctrl);
386
387 if (pdata && pdata->cfg_gpio)
388 pdata->cfg_gpio();
389
390 if (client->irq) {
391 ret = request_threaded_irq(client->irq, NULL,
392 fsa9480_irq_handler,
393 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
394 "fsa9480 micro USB", usbsw);
395 if (ret) {
Masanari Iida8faaaea2014-01-07 21:58:06 +0900396 dev_err(&client->dev, "failed to request IRQ\n");
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700397 return ret;
398 }
399
Jonghwan Choie58a0f82011-10-31 17:11:09 -0700400 if (pdata)
401 device_init_wakeup(&client->dev, pdata->wakeup);
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700402 }
403
404 return 0;
405}
406
Bill Pemberton80c8ae22012-11-19 13:23:05 -0500407static int fsa9480_probe(struct i2c_client *client,
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700408 const struct i2c_device_id *id)
409{
410 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
411 struct fsa9480_usbsw *usbsw;
412 int ret = 0;
413
414 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
415 return -EIO;
416
417 usbsw = kzalloc(sizeof(struct fsa9480_usbsw), GFP_KERNEL);
418 if (!usbsw) {
419 dev_err(&client->dev, "failed to allocate driver data\n");
420 return -ENOMEM;
421 }
422
423 usbsw->client = client;
424 usbsw->pdata = client->dev.platform_data;
425
426 chip = usbsw;
427
428 i2c_set_clientdata(client, usbsw);
429
430 ret = fsa9480_irq_init(usbsw);
431 if (ret)
432 goto fail1;
433
434 ret = sysfs_create_group(&client->dev.kobj, &fsa9480_group);
435 if (ret) {
436 dev_err(&client->dev,
437 "failed to create fsa9480 attribute group\n");
438 goto fail2;
439 }
440
441 /* ADC Detect Time: 500ms */
442 fsa9480_write_reg(client, FSA9480_REG_TIMING1, 0x6);
443
444 if (chip->pdata->reset_cb)
445 chip->pdata->reset_cb();
446
447 /* device detection */
448 fsa9480_detect_dev(usbsw, INT_ATTACH);
449
450 pm_runtime_set_active(&client->dev);
451
452 return 0;
453
454fail2:
455 if (client->irq)
Axel Linb89d5f12011-08-25 15:59:19 -0700456 free_irq(client->irq, usbsw);
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700457fail1:
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700458 kfree(usbsw);
459 return ret;
460}
461
Bill Pemberton486a5c22012-11-19 13:26:02 -0500462static int fsa9480_remove(struct i2c_client *client)
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700463{
464 struct fsa9480_usbsw *usbsw = i2c_get_clientdata(client);
Tejas Upadhyay1a3771d2017-12-20 17:47:43 +0530465
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700466 if (client->irq)
Axel Linb89d5f12011-08-25 15:59:19 -0700467 free_irq(client->irq, usbsw);
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700468
469 sysfs_remove_group(&client->dev.kobj, &fsa9480_group);
470 device_init_wakeup(&client->dev, 0);
471 kfree(usbsw);
472 return 0;
473}
474
Lars-Peter Clausen5d4717d2013-04-11 11:24:40 +0200475#ifdef CONFIG_PM_SLEEP
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700476
Lars-Peter Clausen5d4717d2013-04-11 11:24:40 +0200477static int fsa9480_suspend(struct device *dev)
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700478{
Lars-Peter Clausen5d4717d2013-04-11 11:24:40 +0200479 struct i2c_client *client = to_i2c_client(dev);
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700480 struct fsa9480_usbsw *usbsw = i2c_get_clientdata(client);
481 struct fsa9480_platform_data *pdata = usbsw->pdata;
482
483 if (device_may_wakeup(&client->dev) && client->irq)
484 enable_irq_wake(client->irq);
485
486 if (pdata->usb_power)
487 pdata->usb_power(0);
488
489 return 0;
490}
491
Lars-Peter Clausen5d4717d2013-04-11 11:24:40 +0200492static int fsa9480_resume(struct device *dev)
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700493{
Lars-Peter Clausen5d4717d2013-04-11 11:24:40 +0200494 struct i2c_client *client = to_i2c_client(dev);
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700495 struct fsa9480_usbsw *usbsw = i2c_get_clientdata(client);
496 int dev1, dev2;
497
498 if (device_may_wakeup(&client->dev) && client->irq)
499 disable_irq_wake(client->irq);
500
501 /*
502 * Clear Pending interrupt. Note that detect_dev does what
503 * the interrupt handler does. So, we don't miss pending and
504 * we reenable interrupt if there is one.
505 */
506 fsa9480_read_reg(client, FSA9480_REG_INT1);
507 fsa9480_read_reg(client, FSA9480_REG_INT2);
508
509 dev1 = fsa9480_read_reg(client, FSA9480_REG_DEV_T1);
510 dev2 = fsa9480_read_reg(client, FSA9480_REG_DEV_T2);
511
512 /* device detection */
513 fsa9480_detect_dev(usbsw, (dev1 || dev2) ? INT_ATTACH : INT_DETACH);
514
515 return 0;
516}
517
Lars-Peter Clausen5d4717d2013-04-11 11:24:40 +0200518static SIMPLE_DEV_PM_OPS(fsa9480_pm_ops, fsa9480_suspend, fsa9480_resume);
519#define FSA9480_PM_OPS (&fsa9480_pm_ops)
520
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700521#else
522
Lars-Peter Clausen5d4717d2013-04-11 11:24:40 +0200523#define FSA9480_PM_OPS NULL
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700524
Lars-Peter Clausen5d4717d2013-04-11 11:24:40 +0200525#endif /* CONFIG_PM_SLEEP */
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700526
527static const struct i2c_device_id fsa9480_id[] = {
528 {"fsa9480", 0},
529 {}
530};
531MODULE_DEVICE_TABLE(i2c, fsa9480_id);
532
533static struct i2c_driver fsa9480_i2c_driver = {
534 .driver = {
535 .name = "fsa9480",
Lars-Peter Clausen5d4717d2013-04-11 11:24:40 +0200536 .pm = FSA9480_PM_OPS,
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700537 },
538 .probe = fsa9480_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500539 .remove = fsa9480_remove,
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700540 .id_table = fsa9480_id,
541};
542
Axel Lina64fe2e2012-01-22 15:36:45 +0800543module_i2c_driver(fsa9480_i2c_driver);
Donggeun Kima1bb73d2011-07-25 17:13:07 -0700544
545MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
546MODULE_DESCRIPTION("FSA9480 USB Switch driver");
547MODULE_LICENSE("GPL");