blob: 9cabc1a20d1c18b78f531df2b9271db3e00b1106 [file] [log] [blame]
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +02001/*
2 * Tahvo USB transceiver driver
3 *
4 * Copyright (C) 2005-2006 Nokia Corporation
5 *
6 * Parts copied from isp1301_omap.c.
7 * Copyright (C) 2004 Texas Instruments
8 * Copyright (C) 2004 David Brownell
9 *
10 * Original driver written by Juha Yrjölä, Tony Lindgren and Timo Teräs.
11 * Modified for Retu/Tahvo MFD by Aaro Koskinen.
12 *
13 * This file is subject to the terms and conditions of the GNU General
14 * Public License. See the file "COPYING" in the main directory of this
15 * archive for more details.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 */
22
23#include <linux/io.h>
24#include <linux/clk.h>
25#include <linux/usb.h>
26#include <linux/extcon.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/usb/otg.h>
30#include <linux/mfd/retu.h>
31#include <linux/usb/gadget.h>
32#include <linux/platform_device.h>
33
34#define DRIVER_NAME "tahvo-usb"
35
36#define TAHVO_REG_IDSR 0x02
37#define TAHVO_REG_USBR 0x06
38
39#define USBR_SLAVE_CONTROL (1 << 8)
40#define USBR_VPPVIO_SW (1 << 7)
41#define USBR_SPEED (1 << 6)
42#define USBR_REGOUT (1 << 5)
43#define USBR_MASTER_SW2 (1 << 4)
44#define USBR_MASTER_SW1 (1 << 3)
45#define USBR_SLAVE_SW (1 << 2)
46#define USBR_NSUSPEND (1 << 1)
47#define USBR_SEMODE (1 << 0)
48
49#define TAHVO_MODE_HOST 0
50#define TAHVO_MODE_PERIPHERAL 1
51
52struct tahvo_usb {
53 struct platform_device *pt_dev;
54 struct usb_phy phy;
55 int vbus_state;
56 struct mutex serialize;
57 struct clk *ick;
58 int irq;
59 int tahvo_mode;
60 struct extcon_dev extcon;
61};
62
63static const char *tahvo_cable[] = {
64 "USB-HOST",
65 "USB",
66 NULL,
67};
68
69static ssize_t vbus_state_show(struct device *device,
70 struct device_attribute *attr, char *buf)
71{
72 struct tahvo_usb *tu = dev_get_drvdata(device);
73 return sprintf(buf, "%s\n", tu->vbus_state ? "on" : "off");
74}
75static DEVICE_ATTR(vbus, 0444, vbus_state_show, NULL);
76
77static void check_vbus_state(struct tahvo_usb *tu)
78{
79 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
80 int reg, prev_state;
81
82 reg = retu_read(rdev, TAHVO_REG_IDSR);
83 if (reg & TAHVO_STAT_VBUS) {
Antoine Tenarte47d9252014-10-30 18:41:13 +010084 switch (tu->phy.otg->state) {
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +020085 case OTG_STATE_B_IDLE:
86 /* Enable the gadget driver */
87 if (tu->phy.otg->gadget)
88 usb_gadget_vbus_connect(tu->phy.otg->gadget);
Antoine Tenarte47d9252014-10-30 18:41:13 +010089 tu->phy.otg->state = OTG_STATE_B_PERIPHERAL;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +020090 break;
91 case OTG_STATE_A_IDLE:
92 /*
93 * Session is now valid assuming the USB hub is driving
94 * Vbus.
95 */
Antoine Tenarte47d9252014-10-30 18:41:13 +010096 tu->phy.otg->state = OTG_STATE_A_HOST;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +020097 break;
98 default:
99 break;
100 }
101 dev_info(&tu->pt_dev->dev, "USB cable connected\n");
102 } else {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100103 switch (tu->phy.otg->state) {
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200104 case OTG_STATE_B_PERIPHERAL:
105 if (tu->phy.otg->gadget)
106 usb_gadget_vbus_disconnect(tu->phy.otg->gadget);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100107 tu->phy.otg->state = OTG_STATE_B_IDLE;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200108 break;
109 case OTG_STATE_A_HOST:
Antoine Tenarte47d9252014-10-30 18:41:13 +0100110 tu->phy.otg->state = OTG_STATE_A_IDLE;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200111 break;
112 default:
113 break;
114 }
115 dev_info(&tu->pt_dev->dev, "USB cable disconnected\n");
116 }
117
118 prev_state = tu->vbus_state;
119 tu->vbus_state = reg & TAHVO_STAT_VBUS;
120 if (prev_state != tu->vbus_state) {
121 extcon_set_cable_state(&tu->extcon, "USB", tu->vbus_state);
122 sysfs_notify(&tu->pt_dev->dev.kobj, NULL, "vbus_state");
123 }
124}
125
126static void tahvo_usb_become_host(struct tahvo_usb *tu)
127{
128 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
129
130 extcon_set_cable_state(&tu->extcon, "USB-HOST", true);
131
132 /* Power up the transceiver in USB host mode */
133 retu_write(rdev, TAHVO_REG_USBR, USBR_REGOUT | USBR_NSUSPEND |
134 USBR_MASTER_SW2 | USBR_MASTER_SW1);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100135 tu->phy.otg->state = OTG_STATE_A_IDLE;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200136
137 check_vbus_state(tu);
138}
139
140static void tahvo_usb_stop_host(struct tahvo_usb *tu)
141{
Antoine Tenarte47d9252014-10-30 18:41:13 +0100142 tu->phy.otg->state = OTG_STATE_A_IDLE;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200143}
144
145static void tahvo_usb_become_peripheral(struct tahvo_usb *tu)
146{
147 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
148
149 extcon_set_cable_state(&tu->extcon, "USB-HOST", false);
150
151 /* Power up transceiver and set it in USB peripheral mode */
152 retu_write(rdev, TAHVO_REG_USBR, USBR_SLAVE_CONTROL | USBR_REGOUT |
153 USBR_NSUSPEND | USBR_SLAVE_SW);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100154 tu->phy.otg->state = OTG_STATE_B_IDLE;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200155
156 check_vbus_state(tu);
157}
158
159static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu)
160{
161 if (tu->phy.otg->gadget)
162 usb_gadget_vbus_disconnect(tu->phy.otg->gadget);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100163 tu->phy.otg->state = OTG_STATE_B_IDLE;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200164}
165
166static void tahvo_usb_power_off(struct tahvo_usb *tu)
167{
168 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
169
170 /* Disable gadget controller if any */
171 if (tu->phy.otg->gadget)
172 usb_gadget_vbus_disconnect(tu->phy.otg->gadget);
173
174 /* Power off transceiver */
175 retu_write(rdev, TAHVO_REG_USBR, 0);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100176 tu->phy.otg->state = OTG_STATE_UNDEFINED;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200177}
178
179static int tahvo_usb_set_suspend(struct usb_phy *dev, int suspend)
180{
181 struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, phy);
182 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
183 u16 w;
184
185 dev_dbg(&tu->pt_dev->dev, "%s\n", __func__);
186
187 w = retu_read(rdev, TAHVO_REG_USBR);
188 if (suspend)
189 w &= ~USBR_NSUSPEND;
190 else
191 w |= USBR_NSUSPEND;
192 retu_write(rdev, TAHVO_REG_USBR, w);
193
194 return 0;
195}
196
197static int tahvo_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
198{
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100199 struct tahvo_usb *tu = container_of(otg->usb_phy, struct tahvo_usb,
200 phy);
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200201
202 dev_dbg(&tu->pt_dev->dev, "%s %p\n", __func__, host);
203
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200204 mutex_lock(&tu->serialize);
205
206 if (host == NULL) {
207 if (tu->tahvo_mode == TAHVO_MODE_HOST)
208 tahvo_usb_power_off(tu);
209 otg->host = NULL;
210 mutex_unlock(&tu->serialize);
211 return 0;
212 }
213
214 if (tu->tahvo_mode == TAHVO_MODE_HOST) {
215 otg->host = NULL;
216 tahvo_usb_become_host(tu);
217 }
218
219 otg->host = host;
220
221 mutex_unlock(&tu->serialize);
222
223 return 0;
224}
225
226static int tahvo_usb_set_peripheral(struct usb_otg *otg,
227 struct usb_gadget *gadget)
228{
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100229 struct tahvo_usb *tu = container_of(otg->usb_phy, struct tahvo_usb,
230 phy);
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200231
232 dev_dbg(&tu->pt_dev->dev, "%s %p\n", __func__, gadget);
233
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200234 mutex_lock(&tu->serialize);
235
236 if (!gadget) {
237 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
238 tahvo_usb_power_off(tu);
239 tu->phy.otg->gadget = NULL;
240 mutex_unlock(&tu->serialize);
241 return 0;
242 }
243
244 tu->phy.otg->gadget = gadget;
245 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
246 tahvo_usb_become_peripheral(tu);
247
248 mutex_unlock(&tu->serialize);
249
250 return 0;
251}
252
253static irqreturn_t tahvo_usb_vbus_interrupt(int irq, void *_tu)
254{
255 struct tahvo_usb *tu = _tu;
256
257 mutex_lock(&tu->serialize);
258 check_vbus_state(tu);
259 mutex_unlock(&tu->serialize);
260
261 return IRQ_HANDLED;
262}
263
264static ssize_t otg_mode_show(struct device *device,
265 struct device_attribute *attr, char *buf)
266{
267 struct tahvo_usb *tu = dev_get_drvdata(device);
268
269 switch (tu->tahvo_mode) {
270 case TAHVO_MODE_HOST:
271 return sprintf(buf, "host\n");
272 case TAHVO_MODE_PERIPHERAL:
273 return sprintf(buf, "peripheral\n");
274 }
275
276 return -EINVAL;
277}
278
279static ssize_t otg_mode_store(struct device *device,
280 struct device_attribute *attr,
281 const char *buf, size_t count)
282{
283 struct tahvo_usb *tu = dev_get_drvdata(device);
284 int r;
285
286 mutex_lock(&tu->serialize);
287 if (count >= 4 && strncmp(buf, "host", 4) == 0) {
288 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
289 tahvo_usb_stop_peripheral(tu);
290 tu->tahvo_mode = TAHVO_MODE_HOST;
291 if (tu->phy.otg->host) {
292 dev_info(device, "HOST mode: host controller present\n");
293 tahvo_usb_become_host(tu);
294 } else {
295 dev_info(device, "HOST mode: no host controller, powering off\n");
296 tahvo_usb_power_off(tu);
297 }
298 r = strlen(buf);
299 } else if (count >= 10 && strncmp(buf, "peripheral", 10) == 0) {
300 if (tu->tahvo_mode == TAHVO_MODE_HOST)
301 tahvo_usb_stop_host(tu);
302 tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
303 if (tu->phy.otg->gadget) {
304 dev_info(device, "PERIPHERAL mode: gadget driver present\n");
305 tahvo_usb_become_peripheral(tu);
306 } else {
307 dev_info(device, "PERIPHERAL mode: no gadget driver, powering off\n");
308 tahvo_usb_power_off(tu);
309 }
310 r = strlen(buf);
311 } else {
312 r = -EINVAL;
313 }
314 mutex_unlock(&tu->serialize);
315
316 return r;
317}
318static DEVICE_ATTR(otg_mode, 0644, otg_mode_show, otg_mode_store);
319
320static struct attribute *tahvo_attributes[] = {
321 &dev_attr_vbus.attr,
322 &dev_attr_otg_mode.attr,
323 NULL
324};
325
326static struct attribute_group tahvo_attr_group = {
327 .attrs = tahvo_attributes,
328};
329
330static int tahvo_usb_probe(struct platform_device *pdev)
331{
332 struct retu_dev *rdev = dev_get_drvdata(pdev->dev.parent);
333 struct tahvo_usb *tu;
334 int ret;
335
336 tu = devm_kzalloc(&pdev->dev, sizeof(*tu), GFP_KERNEL);
337 if (!tu)
338 return -ENOMEM;
339
340 tu->phy.otg = devm_kzalloc(&pdev->dev, sizeof(*tu->phy.otg),
341 GFP_KERNEL);
342 if (!tu->phy.otg)
343 return -ENOMEM;
344
345 tu->pt_dev = pdev;
346
347 /* Default mode */
348#ifdef CONFIG_TAHVO_USB_HOST_BY_DEFAULT
349 tu->tahvo_mode = TAHVO_MODE_HOST;
350#else
351 tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
352#endif
353
354 mutex_init(&tu->serialize);
355
356 tu->ick = devm_clk_get(&pdev->dev, "usb_l4_ick");
357 if (!IS_ERR(tu->ick))
358 clk_enable(tu->ick);
359
360 /*
361 * Set initial state, so that we generate kevents only on state changes.
362 */
363 tu->vbus_state = retu_read(rdev, TAHVO_REG_IDSR) & TAHVO_STAT_VBUS;
364
365 tu->extcon.name = DRIVER_NAME;
366 tu->extcon.supported_cable = tahvo_cable;
367 tu->extcon.dev.parent = &pdev->dev;
368
369 ret = extcon_dev_register(&tu->extcon);
370 if (ret) {
371 dev_err(&pdev->dev, "could not register extcon device: %d\n",
372 ret);
373 goto err_disable_clk;
374 }
375
376 /* Set the initial cable state. */
377 extcon_set_cable_state(&tu->extcon, "USB-HOST",
378 tu->tahvo_mode == TAHVO_MODE_HOST);
379 extcon_set_cable_state(&tu->extcon, "USB", tu->vbus_state);
380
381 /* Create OTG interface */
382 tahvo_usb_power_off(tu);
383 tu->phy.dev = &pdev->dev;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100384 tu->phy.otg->state = OTG_STATE_UNDEFINED;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200385 tu->phy.label = DRIVER_NAME;
386 tu->phy.set_suspend = tahvo_usb_set_suspend;
387
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100388 tu->phy.otg->usb_phy = &tu->phy;
Aaro Koskinen9ba96ae2013-12-06 16:13:07 +0200389 tu->phy.otg->set_host = tahvo_usb_set_host;
390 tu->phy.otg->set_peripheral = tahvo_usb_set_peripheral;
391
392 ret = usb_add_phy(&tu->phy, USB_PHY_TYPE_USB2);
393 if (ret < 0) {
394 dev_err(&pdev->dev, "cannot register USB transceiver: %d\n",
395 ret);
396 goto err_extcon_unreg;
397 }
398
399 dev_set_drvdata(&pdev->dev, tu);
400
401 tu->irq = platform_get_irq(pdev, 0);
402 ret = request_threaded_irq(tu->irq, NULL, tahvo_usb_vbus_interrupt, 0,
403 "tahvo-vbus", tu);
404 if (ret) {
405 dev_err(&pdev->dev, "could not register tahvo-vbus irq: %d\n",
406 ret);
407 goto err_remove_phy;
408 }
409
410 /* Attributes */
411 ret = sysfs_create_group(&pdev->dev.kobj, &tahvo_attr_group);
412 if (ret) {
413 dev_err(&pdev->dev, "cannot create sysfs group: %d\n", ret);
414 goto err_free_irq;
415 }
416
417 return 0;
418
419err_free_irq:
420 free_irq(tu->irq, tu);
421err_remove_phy:
422 usb_remove_phy(&tu->phy);
423err_extcon_unreg:
424 extcon_dev_unregister(&tu->extcon);
425err_disable_clk:
426 if (!IS_ERR(tu->ick))
427 clk_disable(tu->ick);
428
429 return ret;
430}
431
432static int tahvo_usb_remove(struct platform_device *pdev)
433{
434 struct tahvo_usb *tu = platform_get_drvdata(pdev);
435
436 sysfs_remove_group(&pdev->dev.kobj, &tahvo_attr_group);
437 free_irq(tu->irq, tu);
438 usb_remove_phy(&tu->phy);
439 extcon_dev_unregister(&tu->extcon);
440 if (!IS_ERR(tu->ick))
441 clk_disable(tu->ick);
442
443 return 0;
444}
445
446static struct platform_driver tahvo_usb_driver = {
447 .probe = tahvo_usb_probe,
448 .remove = tahvo_usb_remove,
449 .driver = {
450 .name = "tahvo-usb",
451 .owner = THIS_MODULE,
452 },
453};
454module_platform_driver(tahvo_usb_driver);
455
456MODULE_DESCRIPTION("Tahvo USB transceiver driver");
457MODULE_LICENSE("GPL");
458MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");
459MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");