1 // SPDX-License-Identifier: GPL-2.0
3 * Tahvo USB transceiver driver
5 * Copyright (C) 2005-2006 Nokia Corporation
7 * Parts copied from isp1301_omap.c.
8 * Copyright (C) 2004 Texas Instruments
9 * Copyright (C) 2004 David Brownell
11 * Original driver written by Juha Yrjölä, Tony Lindgren and Timo Teräs.
12 * Modified for Retu/Tahvo MFD by Aaro Koskinen.
16 #include <linux/clk.h>
17 #include <linux/usb.h>
18 #include <linux/extcon-provider.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/usb/otg.h>
22 #include <linux/mfd/retu.h>
23 #include <linux/usb/gadget.h>
24 #include <linux/platform_device.h>
26 #define DRIVER_NAME "tahvo-usb"
28 #define TAHVO_REG_IDSR 0x02
29 #define TAHVO_REG_USBR 0x06
31 #define USBR_SLAVE_CONTROL (1 << 8)
32 #define USBR_VPPVIO_SW (1 << 7)
33 #define USBR_SPEED (1 << 6)
34 #define USBR_REGOUT (1 << 5)
35 #define USBR_MASTER_SW2 (1 << 4)
36 #define USBR_MASTER_SW1 (1 << 3)
37 #define USBR_SLAVE_SW (1 << 2)
38 #define USBR_NSUSPEND (1 << 1)
39 #define USBR_SEMODE (1 << 0)
41 #define TAHVO_MODE_HOST 0
42 #define TAHVO_MODE_PERIPHERAL 1
45 struct platform_device
*pt_dev
;
48 struct mutex serialize
;
52 struct extcon_dev
*extcon
;
55 static const unsigned int tahvo_cable
[] = {
62 static ssize_t
vbus_show(struct device
*device
,
63 struct device_attribute
*attr
, char *buf
)
65 struct tahvo_usb
*tu
= dev_get_drvdata(device
);
66 return sprintf(buf
, "%s\n", tu
->vbus_state
? "on" : "off");
68 static DEVICE_ATTR_RO(vbus
);
70 static void check_vbus_state(struct tahvo_usb
*tu
)
72 struct retu_dev
*rdev
= dev_get_drvdata(tu
->pt_dev
->dev
.parent
);
75 reg
= retu_read(rdev
, TAHVO_REG_IDSR
);
76 if (reg
& TAHVO_STAT_VBUS
) {
77 switch (tu
->phy
.otg
->state
) {
78 case OTG_STATE_B_IDLE
:
79 /* Enable the gadget driver */
80 if (tu
->phy
.otg
->gadget
)
81 usb_gadget_vbus_connect(tu
->phy
.otg
->gadget
);
82 tu
->phy
.otg
->state
= OTG_STATE_B_PERIPHERAL
;
83 usb_phy_set_event(&tu
->phy
, USB_EVENT_ENUMERATED
);
85 case OTG_STATE_A_IDLE
:
87 * Session is now valid assuming the USB hub is driving
90 tu
->phy
.otg
->state
= OTG_STATE_A_HOST
;
95 dev_info(&tu
->pt_dev
->dev
, "USB cable connected\n");
97 switch (tu
->phy
.otg
->state
) {
98 case OTG_STATE_B_PERIPHERAL
:
99 if (tu
->phy
.otg
->gadget
)
100 usb_gadget_vbus_disconnect(tu
->phy
.otg
->gadget
);
101 tu
->phy
.otg
->state
= OTG_STATE_B_IDLE
;
102 usb_phy_set_event(&tu
->phy
, USB_EVENT_NONE
);
104 case OTG_STATE_A_HOST
:
105 tu
->phy
.otg
->state
= OTG_STATE_A_IDLE
;
110 dev_info(&tu
->pt_dev
->dev
, "USB cable disconnected\n");
113 prev_state
= tu
->vbus_state
;
114 tu
->vbus_state
= reg
& TAHVO_STAT_VBUS
;
115 if (prev_state
!= tu
->vbus_state
) {
116 extcon_set_state_sync(tu
->extcon
, EXTCON_USB
, tu
->vbus_state
);
117 sysfs_notify(&tu
->pt_dev
->dev
.kobj
, NULL
, "vbus_state");
121 static void tahvo_usb_become_host(struct tahvo_usb
*tu
)
123 struct retu_dev
*rdev
= dev_get_drvdata(tu
->pt_dev
->dev
.parent
);
125 extcon_set_state_sync(tu
->extcon
, EXTCON_USB_HOST
, true);
127 /* Power up the transceiver in USB host mode */
128 retu_write(rdev
, TAHVO_REG_USBR
, USBR_REGOUT
| USBR_NSUSPEND
|
129 USBR_MASTER_SW2
| USBR_MASTER_SW1
);
130 tu
->phy
.otg
->state
= OTG_STATE_A_IDLE
;
132 check_vbus_state(tu
);
135 static void tahvo_usb_stop_host(struct tahvo_usb
*tu
)
137 tu
->phy
.otg
->state
= OTG_STATE_A_IDLE
;
140 static void tahvo_usb_become_peripheral(struct tahvo_usb
*tu
)
142 struct retu_dev
*rdev
= dev_get_drvdata(tu
->pt_dev
->dev
.parent
);
144 extcon_set_state_sync(tu
->extcon
, EXTCON_USB_HOST
, false);
146 /* Power up transceiver and set it in USB peripheral mode */
147 retu_write(rdev
, TAHVO_REG_USBR
, USBR_SLAVE_CONTROL
| USBR_REGOUT
|
148 USBR_NSUSPEND
| USBR_SLAVE_SW
);
149 tu
->phy
.otg
->state
= OTG_STATE_B_IDLE
;
151 check_vbus_state(tu
);
154 static void tahvo_usb_stop_peripheral(struct tahvo_usb
*tu
)
156 if (tu
->phy
.otg
->gadget
)
157 usb_gadget_vbus_disconnect(tu
->phy
.otg
->gadget
);
158 tu
->phy
.otg
->state
= OTG_STATE_B_IDLE
;
161 static void tahvo_usb_power_off(struct tahvo_usb
*tu
)
163 struct retu_dev
*rdev
= dev_get_drvdata(tu
->pt_dev
->dev
.parent
);
165 /* Disable gadget controller if any */
166 if (tu
->phy
.otg
->gadget
)
167 usb_gadget_vbus_disconnect(tu
->phy
.otg
->gadget
);
169 /* Power off transceiver */
170 retu_write(rdev
, TAHVO_REG_USBR
, 0);
171 tu
->phy
.otg
->state
= OTG_STATE_UNDEFINED
;
174 static int tahvo_usb_set_suspend(struct usb_phy
*dev
, int suspend
)
176 struct tahvo_usb
*tu
= container_of(dev
, struct tahvo_usb
, phy
);
177 struct retu_dev
*rdev
= dev_get_drvdata(tu
->pt_dev
->dev
.parent
);
180 dev_dbg(&tu
->pt_dev
->dev
, "%s\n", __func__
);
182 w
= retu_read(rdev
, TAHVO_REG_USBR
);
187 retu_write(rdev
, TAHVO_REG_USBR
, w
);
192 static int tahvo_usb_set_host(struct usb_otg
*otg
, struct usb_bus
*host
)
194 struct tahvo_usb
*tu
= container_of(otg
->usb_phy
, struct tahvo_usb
,
197 dev_dbg(&tu
->pt_dev
->dev
, "%s %p\n", __func__
, host
);
199 mutex_lock(&tu
->serialize
);
202 if (tu
->tahvo_mode
== TAHVO_MODE_HOST
)
203 tahvo_usb_power_off(tu
);
205 mutex_unlock(&tu
->serialize
);
209 if (tu
->tahvo_mode
== TAHVO_MODE_HOST
) {
211 tahvo_usb_become_host(tu
);
216 mutex_unlock(&tu
->serialize
);
221 static int tahvo_usb_set_peripheral(struct usb_otg
*otg
,
222 struct usb_gadget
*gadget
)
224 struct tahvo_usb
*tu
= container_of(otg
->usb_phy
, struct tahvo_usb
,
227 dev_dbg(&tu
->pt_dev
->dev
, "%s %p\n", __func__
, gadget
);
229 mutex_lock(&tu
->serialize
);
232 if (tu
->tahvo_mode
== TAHVO_MODE_PERIPHERAL
)
233 tahvo_usb_power_off(tu
);
234 tu
->phy
.otg
->gadget
= NULL
;
235 mutex_unlock(&tu
->serialize
);
239 tu
->phy
.otg
->gadget
= gadget
;
240 if (tu
->tahvo_mode
== TAHVO_MODE_PERIPHERAL
)
241 tahvo_usb_become_peripheral(tu
);
243 mutex_unlock(&tu
->serialize
);
248 static irqreturn_t
tahvo_usb_vbus_interrupt(int irq
, void *_tu
)
250 struct tahvo_usb
*tu
= _tu
;
252 mutex_lock(&tu
->serialize
);
253 check_vbus_state(tu
);
254 mutex_unlock(&tu
->serialize
);
259 static ssize_t
otg_mode_show(struct device
*device
,
260 struct device_attribute
*attr
, char *buf
)
262 struct tahvo_usb
*tu
= dev_get_drvdata(device
);
264 switch (tu
->tahvo_mode
) {
265 case TAHVO_MODE_HOST
:
266 return sprintf(buf
, "host\n");
267 case TAHVO_MODE_PERIPHERAL
:
268 return sprintf(buf
, "peripheral\n");
274 static ssize_t
otg_mode_store(struct device
*device
,
275 struct device_attribute
*attr
,
276 const char *buf
, size_t count
)
278 struct tahvo_usb
*tu
= dev_get_drvdata(device
);
281 mutex_lock(&tu
->serialize
);
282 if (count
>= 4 && strncmp(buf
, "host", 4) == 0) {
283 if (tu
->tahvo_mode
== TAHVO_MODE_PERIPHERAL
)
284 tahvo_usb_stop_peripheral(tu
);
285 tu
->tahvo_mode
= TAHVO_MODE_HOST
;
286 if (tu
->phy
.otg
->host
) {
287 dev_info(device
, "HOST mode: host controller present\n");
288 tahvo_usb_become_host(tu
);
290 dev_info(device
, "HOST mode: no host controller, powering off\n");
291 tahvo_usb_power_off(tu
);
294 } else if (count
>= 10 && strncmp(buf
, "peripheral", 10) == 0) {
295 if (tu
->tahvo_mode
== TAHVO_MODE_HOST
)
296 tahvo_usb_stop_host(tu
);
297 tu
->tahvo_mode
= TAHVO_MODE_PERIPHERAL
;
298 if (tu
->phy
.otg
->gadget
) {
299 dev_info(device
, "PERIPHERAL mode: gadget driver present\n");
300 tahvo_usb_become_peripheral(tu
);
302 dev_info(device
, "PERIPHERAL mode: no gadget driver, powering off\n");
303 tahvo_usb_power_off(tu
);
309 mutex_unlock(&tu
->serialize
);
313 static DEVICE_ATTR_RW(otg_mode
);
315 static struct attribute
*tahvo_attributes
[] = {
317 &dev_attr_otg_mode
.attr
,
321 static const struct attribute_group tahvo_attr_group
= {
322 .attrs
= tahvo_attributes
,
325 static int tahvo_usb_probe(struct platform_device
*pdev
)
327 struct retu_dev
*rdev
= dev_get_drvdata(pdev
->dev
.parent
);
328 struct tahvo_usb
*tu
;
331 tu
= devm_kzalloc(&pdev
->dev
, sizeof(*tu
), GFP_KERNEL
);
335 tu
->phy
.otg
= devm_kzalloc(&pdev
->dev
, sizeof(*tu
->phy
.otg
),
343 #ifdef CONFIG_TAHVO_USB_HOST_BY_DEFAULT
344 tu
->tahvo_mode
= TAHVO_MODE_HOST
;
346 tu
->tahvo_mode
= TAHVO_MODE_PERIPHERAL
;
349 mutex_init(&tu
->serialize
);
351 tu
->ick
= devm_clk_get(&pdev
->dev
, "usb_l4_ick");
352 if (!IS_ERR(tu
->ick
))
356 * Set initial state, so that we generate kevents only on state changes.
358 tu
->vbus_state
= retu_read(rdev
, TAHVO_REG_IDSR
) & TAHVO_STAT_VBUS
;
360 tu
->extcon
= devm_extcon_dev_allocate(&pdev
->dev
, tahvo_cable
);
361 if (IS_ERR(tu
->extcon
)) {
362 dev_err(&pdev
->dev
, "failed to allocate memory for extcon\n");
363 ret
= PTR_ERR(tu
->extcon
);
364 goto err_disable_clk
;
367 ret
= devm_extcon_dev_register(&pdev
->dev
, tu
->extcon
);
369 dev_err(&pdev
->dev
, "could not register extcon device: %d\n",
371 goto err_disable_clk
;
374 /* Set the initial cable state. */
375 extcon_set_state_sync(tu
->extcon
, EXTCON_USB_HOST
,
376 tu
->tahvo_mode
== TAHVO_MODE_HOST
);
377 extcon_set_state_sync(tu
->extcon
, EXTCON_USB
, tu
->vbus_state
);
379 /* Create OTG interface */
380 tahvo_usb_power_off(tu
);
381 tu
->phy
.dev
= &pdev
->dev
;
382 tu
->phy
.otg
->state
= OTG_STATE_UNDEFINED
;
383 tu
->phy
.label
= DRIVER_NAME
;
384 tu
->phy
.set_suspend
= tahvo_usb_set_suspend
;
386 tu
->phy
.otg
->usb_phy
= &tu
->phy
;
387 tu
->phy
.otg
->set_host
= tahvo_usb_set_host
;
388 tu
->phy
.otg
->set_peripheral
= tahvo_usb_set_peripheral
;
390 ret
= usb_add_phy(&tu
->phy
, USB_PHY_TYPE_USB2
);
392 dev_err(&pdev
->dev
, "cannot register USB transceiver: %d\n",
394 goto err_disable_clk
;
397 dev_set_drvdata(&pdev
->dev
, tu
);
399 tu
->irq
= platform_get_irq(pdev
, 0);
400 ret
= request_threaded_irq(tu
->irq
, NULL
, tahvo_usb_vbus_interrupt
,
404 dev_err(&pdev
->dev
, "could not register tahvo-vbus irq: %d\n",
410 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &tahvo_attr_group
);
412 dev_err(&pdev
->dev
, "cannot create sysfs group: %d\n", ret
);
419 free_irq(tu
->irq
, tu
);
421 usb_remove_phy(&tu
->phy
);
423 if (!IS_ERR(tu
->ick
))
424 clk_disable(tu
->ick
);
429 static int tahvo_usb_remove(struct platform_device
*pdev
)
431 struct tahvo_usb
*tu
= platform_get_drvdata(pdev
);
433 sysfs_remove_group(&pdev
->dev
.kobj
, &tahvo_attr_group
);
434 free_irq(tu
->irq
, tu
);
435 usb_remove_phy(&tu
->phy
);
436 if (!IS_ERR(tu
->ick
))
437 clk_disable(tu
->ick
);
442 static struct platform_driver tahvo_usb_driver
= {
443 .probe
= tahvo_usb_probe
,
444 .remove
= tahvo_usb_remove
,
449 module_platform_driver(tahvo_usb_driver
);
451 MODULE_DESCRIPTION("Tahvo USB transceiver driver");
452 MODULE_LICENSE("GPL");
453 MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");
454 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");