2 * Tahvo USB transceiver driver
4 * Copyright (C) 2005-2006 Nokia Corporation
6 * Parts copied from isp1301_omap.c.
7 * Copyright (C) 2004 Texas Instruments
8 * Copyright (C) 2004 David Brownell
10 * Original driver written by Juha Yrjölä, Tony Lindgren and Timo Teräs.
11 * Modified for Retu/Tahvo MFD by Aaro Koskinen.
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.
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.
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>
34 #define DRIVER_NAME "tahvo-usb"
36 #define TAHVO_REG_IDSR 0x02
37 #define TAHVO_REG_USBR 0x06
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)
49 #define TAHVO_MODE_HOST 0
50 #define TAHVO_MODE_PERIPHERAL 1
53 struct platform_device
*pt_dev
;
56 struct mutex serialize
;
60 struct extcon_dev
*extcon
;
63 static const unsigned int tahvo_cable
[] = {
70 static ssize_t
vbus_state_show(struct device
*device
,
71 struct device_attribute
*attr
, char *buf
)
73 struct tahvo_usb
*tu
= dev_get_drvdata(device
);
74 return sprintf(buf
, "%s\n", tu
->vbus_state
? "on" : "off");
76 static DEVICE_ATTR(vbus
, 0444, vbus_state_show
, NULL
);
78 static void check_vbus_state(struct tahvo_usb
*tu
)
80 struct retu_dev
*rdev
= dev_get_drvdata(tu
->pt_dev
->dev
.parent
);
83 reg
= retu_read(rdev
, TAHVO_REG_IDSR
);
84 if (reg
& TAHVO_STAT_VBUS
) {
85 switch (tu
->phy
.otg
->state
) {
86 case OTG_STATE_B_IDLE
:
87 /* Enable the gadget driver */
88 if (tu
->phy
.otg
->gadget
)
89 usb_gadget_vbus_connect(tu
->phy
.otg
->gadget
);
90 tu
->phy
.otg
->state
= OTG_STATE_B_PERIPHERAL
;
91 usb_phy_set_event(&tu
->phy
, USB_EVENT_ENUMERATED
);
93 case OTG_STATE_A_IDLE
:
95 * Session is now valid assuming the USB hub is driving
98 tu
->phy
.otg
->state
= OTG_STATE_A_HOST
;
103 dev_info(&tu
->pt_dev
->dev
, "USB cable connected\n");
105 switch (tu
->phy
.otg
->state
) {
106 case OTG_STATE_B_PERIPHERAL
:
107 if (tu
->phy
.otg
->gadget
)
108 usb_gadget_vbus_disconnect(tu
->phy
.otg
->gadget
);
109 tu
->phy
.otg
->state
= OTG_STATE_B_IDLE
;
110 usb_phy_set_event(&tu
->phy
, USB_EVENT_NONE
);
112 case OTG_STATE_A_HOST
:
113 tu
->phy
.otg
->state
= OTG_STATE_A_IDLE
;
118 dev_info(&tu
->pt_dev
->dev
, "USB cable disconnected\n");
121 prev_state
= tu
->vbus_state
;
122 tu
->vbus_state
= reg
& TAHVO_STAT_VBUS
;
123 if (prev_state
!= tu
->vbus_state
) {
124 extcon_set_cable_state_(tu
->extcon
, EXTCON_USB
, tu
->vbus_state
);
125 sysfs_notify(&tu
->pt_dev
->dev
.kobj
, NULL
, "vbus_state");
129 static void tahvo_usb_become_host(struct tahvo_usb
*tu
)
131 struct retu_dev
*rdev
= dev_get_drvdata(tu
->pt_dev
->dev
.parent
);
133 extcon_set_cable_state_(tu
->extcon
, EXTCON_USB_HOST
, true);
135 /* Power up the transceiver in USB host mode */
136 retu_write(rdev
, TAHVO_REG_USBR
, USBR_REGOUT
| USBR_NSUSPEND
|
137 USBR_MASTER_SW2
| USBR_MASTER_SW1
);
138 tu
->phy
.otg
->state
= OTG_STATE_A_IDLE
;
140 check_vbus_state(tu
);
143 static void tahvo_usb_stop_host(struct tahvo_usb
*tu
)
145 tu
->phy
.otg
->state
= OTG_STATE_A_IDLE
;
148 static void tahvo_usb_become_peripheral(struct tahvo_usb
*tu
)
150 struct retu_dev
*rdev
= dev_get_drvdata(tu
->pt_dev
->dev
.parent
);
152 extcon_set_cable_state_(tu
->extcon
, EXTCON_USB_HOST
, false);
154 /* Power up transceiver and set it in USB peripheral mode */
155 retu_write(rdev
, TAHVO_REG_USBR
, USBR_SLAVE_CONTROL
| USBR_REGOUT
|
156 USBR_NSUSPEND
| USBR_SLAVE_SW
);
157 tu
->phy
.otg
->state
= OTG_STATE_B_IDLE
;
159 check_vbus_state(tu
);
162 static void tahvo_usb_stop_peripheral(struct tahvo_usb
*tu
)
164 if (tu
->phy
.otg
->gadget
)
165 usb_gadget_vbus_disconnect(tu
->phy
.otg
->gadget
);
166 tu
->phy
.otg
->state
= OTG_STATE_B_IDLE
;
169 static void tahvo_usb_power_off(struct tahvo_usb
*tu
)
171 struct retu_dev
*rdev
= dev_get_drvdata(tu
->pt_dev
->dev
.parent
);
173 /* Disable gadget controller if any */
174 if (tu
->phy
.otg
->gadget
)
175 usb_gadget_vbus_disconnect(tu
->phy
.otg
->gadget
);
177 /* Power off transceiver */
178 retu_write(rdev
, TAHVO_REG_USBR
, 0);
179 tu
->phy
.otg
->state
= OTG_STATE_UNDEFINED
;
182 static int tahvo_usb_set_suspend(struct usb_phy
*dev
, int suspend
)
184 struct tahvo_usb
*tu
= container_of(dev
, struct tahvo_usb
, phy
);
185 struct retu_dev
*rdev
= dev_get_drvdata(tu
->pt_dev
->dev
.parent
);
188 dev_dbg(&tu
->pt_dev
->dev
, "%s\n", __func__
);
190 w
= retu_read(rdev
, TAHVO_REG_USBR
);
195 retu_write(rdev
, TAHVO_REG_USBR
, w
);
200 static int tahvo_usb_set_host(struct usb_otg
*otg
, struct usb_bus
*host
)
202 struct tahvo_usb
*tu
= container_of(otg
->usb_phy
, struct tahvo_usb
,
205 dev_dbg(&tu
->pt_dev
->dev
, "%s %p\n", __func__
, host
);
207 mutex_lock(&tu
->serialize
);
210 if (tu
->tahvo_mode
== TAHVO_MODE_HOST
)
211 tahvo_usb_power_off(tu
);
213 mutex_unlock(&tu
->serialize
);
217 if (tu
->tahvo_mode
== TAHVO_MODE_HOST
) {
219 tahvo_usb_become_host(tu
);
224 mutex_unlock(&tu
->serialize
);
229 static int tahvo_usb_set_peripheral(struct usb_otg
*otg
,
230 struct usb_gadget
*gadget
)
232 struct tahvo_usb
*tu
= container_of(otg
->usb_phy
, struct tahvo_usb
,
235 dev_dbg(&tu
->pt_dev
->dev
, "%s %p\n", __func__
, gadget
);
237 mutex_lock(&tu
->serialize
);
240 if (tu
->tahvo_mode
== TAHVO_MODE_PERIPHERAL
)
241 tahvo_usb_power_off(tu
);
242 tu
->phy
.otg
->gadget
= NULL
;
243 mutex_unlock(&tu
->serialize
);
247 tu
->phy
.otg
->gadget
= gadget
;
248 if (tu
->tahvo_mode
== TAHVO_MODE_PERIPHERAL
)
249 tahvo_usb_become_peripheral(tu
);
251 mutex_unlock(&tu
->serialize
);
256 static irqreturn_t
tahvo_usb_vbus_interrupt(int irq
, void *_tu
)
258 struct tahvo_usb
*tu
= _tu
;
260 mutex_lock(&tu
->serialize
);
261 check_vbus_state(tu
);
262 mutex_unlock(&tu
->serialize
);
267 static ssize_t
otg_mode_show(struct device
*device
,
268 struct device_attribute
*attr
, char *buf
)
270 struct tahvo_usb
*tu
= dev_get_drvdata(device
);
272 switch (tu
->tahvo_mode
) {
273 case TAHVO_MODE_HOST
:
274 return sprintf(buf
, "host\n");
275 case TAHVO_MODE_PERIPHERAL
:
276 return sprintf(buf
, "peripheral\n");
282 static ssize_t
otg_mode_store(struct device
*device
,
283 struct device_attribute
*attr
,
284 const char *buf
, size_t count
)
286 struct tahvo_usb
*tu
= dev_get_drvdata(device
);
289 mutex_lock(&tu
->serialize
);
290 if (count
>= 4 && strncmp(buf
, "host", 4) == 0) {
291 if (tu
->tahvo_mode
== TAHVO_MODE_PERIPHERAL
)
292 tahvo_usb_stop_peripheral(tu
);
293 tu
->tahvo_mode
= TAHVO_MODE_HOST
;
294 if (tu
->phy
.otg
->host
) {
295 dev_info(device
, "HOST mode: host controller present\n");
296 tahvo_usb_become_host(tu
);
298 dev_info(device
, "HOST mode: no host controller, powering off\n");
299 tahvo_usb_power_off(tu
);
302 } else if (count
>= 10 && strncmp(buf
, "peripheral", 10) == 0) {
303 if (tu
->tahvo_mode
== TAHVO_MODE_HOST
)
304 tahvo_usb_stop_host(tu
);
305 tu
->tahvo_mode
= TAHVO_MODE_PERIPHERAL
;
306 if (tu
->phy
.otg
->gadget
) {
307 dev_info(device
, "PERIPHERAL mode: gadget driver present\n");
308 tahvo_usb_become_peripheral(tu
);
310 dev_info(device
, "PERIPHERAL mode: no gadget driver, powering off\n");
311 tahvo_usb_power_off(tu
);
317 mutex_unlock(&tu
->serialize
);
321 static DEVICE_ATTR(otg_mode
, 0644, otg_mode_show
, otg_mode_store
);
323 static struct attribute
*tahvo_attributes
[] = {
325 &dev_attr_otg_mode
.attr
,
329 static struct attribute_group tahvo_attr_group
= {
330 .attrs
= tahvo_attributes
,
333 static int tahvo_usb_probe(struct platform_device
*pdev
)
335 struct retu_dev
*rdev
= dev_get_drvdata(pdev
->dev
.parent
);
336 struct tahvo_usb
*tu
;
339 tu
= devm_kzalloc(&pdev
->dev
, sizeof(*tu
), GFP_KERNEL
);
343 tu
->phy
.otg
= devm_kzalloc(&pdev
->dev
, sizeof(*tu
->phy
.otg
),
351 #ifdef CONFIG_TAHVO_USB_HOST_BY_DEFAULT
352 tu
->tahvo_mode
= TAHVO_MODE_HOST
;
354 tu
->tahvo_mode
= TAHVO_MODE_PERIPHERAL
;
357 mutex_init(&tu
->serialize
);
359 tu
->ick
= devm_clk_get(&pdev
->dev
, "usb_l4_ick");
360 if (!IS_ERR(tu
->ick
))
364 * Set initial state, so that we generate kevents only on state changes.
366 tu
->vbus_state
= retu_read(rdev
, TAHVO_REG_IDSR
) & TAHVO_STAT_VBUS
;
368 tu
->extcon
= devm_extcon_dev_allocate(&pdev
->dev
, tahvo_cable
);
369 if (IS_ERR(tu
->extcon
)) {
370 dev_err(&pdev
->dev
, "failed to allocate memory for extcon\n");
371 ret
= PTR_ERR(tu
->extcon
);
372 goto err_disable_clk
;
375 ret
= devm_extcon_dev_register(&pdev
->dev
, tu
->extcon
);
377 dev_err(&pdev
->dev
, "could not register extcon device: %d\n",
379 goto err_disable_clk
;
382 /* Set the initial cable state. */
383 extcon_set_cable_state_(tu
->extcon
, EXTCON_USB_HOST
,
384 tu
->tahvo_mode
== TAHVO_MODE_HOST
);
385 extcon_set_cable_state_(tu
->extcon
, EXTCON_USB
, tu
->vbus_state
);
387 /* Create OTG interface */
388 tahvo_usb_power_off(tu
);
389 tu
->phy
.dev
= &pdev
->dev
;
390 tu
->phy
.otg
->state
= OTG_STATE_UNDEFINED
;
391 tu
->phy
.label
= DRIVER_NAME
;
392 tu
->phy
.set_suspend
= tahvo_usb_set_suspend
;
394 tu
->phy
.otg
->usb_phy
= &tu
->phy
;
395 tu
->phy
.otg
->set_host
= tahvo_usb_set_host
;
396 tu
->phy
.otg
->set_peripheral
= tahvo_usb_set_peripheral
;
398 ret
= usb_add_phy(&tu
->phy
, USB_PHY_TYPE_USB2
);
400 dev_err(&pdev
->dev
, "cannot register USB transceiver: %d\n",
402 goto err_disable_clk
;
405 dev_set_drvdata(&pdev
->dev
, tu
);
407 tu
->irq
= platform_get_irq(pdev
, 0);
408 ret
= request_threaded_irq(tu
->irq
, NULL
, tahvo_usb_vbus_interrupt
,
412 dev_err(&pdev
->dev
, "could not register tahvo-vbus irq: %d\n",
418 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &tahvo_attr_group
);
420 dev_err(&pdev
->dev
, "cannot create sysfs group: %d\n", ret
);
427 free_irq(tu
->irq
, tu
);
429 usb_remove_phy(&tu
->phy
);
431 if (!IS_ERR(tu
->ick
))
432 clk_disable(tu
->ick
);
437 static int tahvo_usb_remove(struct platform_device
*pdev
)
439 struct tahvo_usb
*tu
= platform_get_drvdata(pdev
);
441 sysfs_remove_group(&pdev
->dev
.kobj
, &tahvo_attr_group
);
442 free_irq(tu
->irq
, tu
);
443 usb_remove_phy(&tu
->phy
);
444 if (!IS_ERR(tu
->ick
))
445 clk_disable(tu
->ick
);
450 static struct platform_driver tahvo_usb_driver
= {
451 .probe
= tahvo_usb_probe
,
452 .remove
= tahvo_usb_remove
,
457 module_platform_driver(tahvo_usb_driver
);
459 MODULE_DESCRIPTION("Tahvo USB transceiver driver");
460 MODULE_LICENSE("GPL");
461 MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");
462 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");