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 char *tahvo_cable[] = {
69 static ssize_t vbus_state_show(struct device *device,
70 struct device_attribute *attr, char *buf)
72 struct tahvo_usb *tu = dev_get_drvdata(device);
73 return sprintf(buf, "%s\n", tu->vbus_state ? "on" : "off");
75 static DEVICE_ATTR(vbus, 0444, vbus_state_show, NULL);
77 static void check_vbus_state(struct tahvo_usb *tu)
79 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
82 reg = retu_read(rdev, TAHVO_REG_IDSR);
83 if (reg & TAHVO_STAT_VBUS) {
84 switch (tu->phy.state) {
85 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);
89 tu->phy.state = OTG_STATE_B_PERIPHERAL;
91 case OTG_STATE_A_IDLE:
93 * Session is now valid assuming the USB hub is driving
96 tu->phy.state = OTG_STATE_A_HOST;
101 dev_info(&tu->pt_dev->dev, "USB cable connected\n");
103 switch (tu->phy.state) {
104 case OTG_STATE_B_PERIPHERAL:
105 if (tu->phy.otg->gadget)
106 usb_gadget_vbus_disconnect(tu->phy.otg->gadget);
107 tu->phy.state = OTG_STATE_B_IDLE;
109 case OTG_STATE_A_HOST:
110 tu->phy.state = OTG_STATE_A_IDLE;
115 dev_info(&tu->pt_dev->dev, "USB cable disconnected\n");
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");
126 static void tahvo_usb_become_host(struct tahvo_usb *tu)
128 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
130 extcon_set_cable_state(&tu->extcon, "USB-HOST", true);
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);
135 tu->phy.state = OTG_STATE_A_IDLE;
137 check_vbus_state(tu);
140 static void tahvo_usb_stop_host(struct tahvo_usb *tu)
142 tu->phy.state = OTG_STATE_A_IDLE;
145 static void tahvo_usb_become_peripheral(struct tahvo_usb *tu)
147 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
149 extcon_set_cable_state(&tu->extcon, "USB-HOST", false);
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);
154 tu->phy.state = OTG_STATE_B_IDLE;
156 check_vbus_state(tu);
159 static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu)
161 if (tu->phy.otg->gadget)
162 usb_gadget_vbus_disconnect(tu->phy.otg->gadget);
163 tu->phy.state = OTG_STATE_B_IDLE;
166 static void tahvo_usb_power_off(struct tahvo_usb *tu)
168 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
170 /* Disable gadget controller if any */
171 if (tu->phy.otg->gadget)
172 usb_gadget_vbus_disconnect(tu->phy.otg->gadget);
174 /* Power off transceiver */
175 retu_write(rdev, TAHVO_REG_USBR, 0);
176 tu->phy.state = OTG_STATE_UNDEFINED;
179 static int tahvo_usb_set_suspend(struct usb_phy *dev, int suspend)
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);
185 dev_dbg(&tu->pt_dev->dev, "%s\n", __func__);
187 w = retu_read(rdev, TAHVO_REG_USBR);
192 retu_write(rdev, TAHVO_REG_USBR, w);
197 static int tahvo_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
199 struct tahvo_usb *tu = container_of(otg->phy, struct tahvo_usb, phy);
201 dev_dbg(&tu->pt_dev->dev, "%s %p\n", __func__, host);
206 mutex_lock(&tu->serialize);
209 if (tu->tahvo_mode == TAHVO_MODE_HOST)
210 tahvo_usb_power_off(tu);
212 mutex_unlock(&tu->serialize);
216 if (tu->tahvo_mode == TAHVO_MODE_HOST) {
218 tahvo_usb_become_host(tu);
223 mutex_unlock(&tu->serialize);
228 static int tahvo_usb_set_peripheral(struct usb_otg *otg,
229 struct usb_gadget *gadget)
231 struct tahvo_usb *tu = container_of(otg->phy, struct tahvo_usb, phy);
233 dev_dbg(&tu->pt_dev->dev, "%s %p\n", __func__, gadget);
238 mutex_lock(&tu->serialize);
241 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
242 tahvo_usb_power_off(tu);
243 tu->phy.otg->gadget = NULL;
244 mutex_unlock(&tu->serialize);
248 tu->phy.otg->gadget = gadget;
249 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
250 tahvo_usb_become_peripheral(tu);
252 mutex_unlock(&tu->serialize);
257 static irqreturn_t tahvo_usb_vbus_interrupt(int irq, void *_tu)
259 struct tahvo_usb *tu = _tu;
261 mutex_lock(&tu->serialize);
262 check_vbus_state(tu);
263 mutex_unlock(&tu->serialize);
268 static ssize_t otg_mode_show(struct device *device,
269 struct device_attribute *attr, char *buf)
271 struct tahvo_usb *tu = dev_get_drvdata(device);
273 switch (tu->tahvo_mode) {
274 case TAHVO_MODE_HOST:
275 return sprintf(buf, "host\n");
276 case TAHVO_MODE_PERIPHERAL:
277 return sprintf(buf, "peripheral\n");
283 static ssize_t otg_mode_store(struct device *device,
284 struct device_attribute *attr,
285 const char *buf, size_t count)
287 struct tahvo_usb *tu = dev_get_drvdata(device);
290 mutex_lock(&tu->serialize);
291 if (count >= 4 && strncmp(buf, "host", 4) == 0) {
292 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
293 tahvo_usb_stop_peripheral(tu);
294 tu->tahvo_mode = TAHVO_MODE_HOST;
295 if (tu->phy.otg->host) {
296 dev_info(device, "HOST mode: host controller present\n");
297 tahvo_usb_become_host(tu);
299 dev_info(device, "HOST mode: no host controller, powering off\n");
300 tahvo_usb_power_off(tu);
303 } else if (count >= 10 && strncmp(buf, "peripheral", 10) == 0) {
304 if (tu->tahvo_mode == TAHVO_MODE_HOST)
305 tahvo_usb_stop_host(tu);
306 tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
307 if (tu->phy.otg->gadget) {
308 dev_info(device, "PERIPHERAL mode: gadget driver present\n");
309 tahvo_usb_become_peripheral(tu);
311 dev_info(device, "PERIPHERAL mode: no gadget driver, powering off\n");
312 tahvo_usb_power_off(tu);
318 mutex_unlock(&tu->serialize);
322 static DEVICE_ATTR(otg_mode, 0644, otg_mode_show, otg_mode_store);
324 static struct attribute *tahvo_attributes[] = {
326 &dev_attr_otg_mode.attr,
330 static struct attribute_group tahvo_attr_group = {
331 .attrs = tahvo_attributes,
334 static int tahvo_usb_probe(struct platform_device *pdev)
336 struct retu_dev *rdev = dev_get_drvdata(pdev->dev.parent);
337 struct tahvo_usb *tu;
340 tu = devm_kzalloc(&pdev->dev, sizeof(*tu), GFP_KERNEL);
344 tu->phy.otg = devm_kzalloc(&pdev->dev, sizeof(*tu->phy.otg),
352 #ifdef CONFIG_TAHVO_USB_HOST_BY_DEFAULT
353 tu->tahvo_mode = TAHVO_MODE_HOST;
355 tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
358 mutex_init(&tu->serialize);
360 tu->ick = devm_clk_get(&pdev->dev, "usb_l4_ick");
361 if (!IS_ERR(tu->ick))
365 * Set initial state, so that we generate kevents only on state changes.
367 tu->vbus_state = retu_read(rdev, TAHVO_REG_IDSR) & TAHVO_STAT_VBUS;
369 tu->extcon.name = DRIVER_NAME;
370 tu->extcon.supported_cable = tahvo_cable;
371 tu->extcon.dev.parent = &pdev->dev;
373 ret = extcon_dev_register(&tu->extcon);
375 dev_err(&pdev->dev, "could not register extcon device: %d\n",
377 goto err_disable_clk;
380 /* Set the initial cable state. */
381 extcon_set_cable_state(&tu->extcon, "USB-HOST",
382 tu->tahvo_mode == TAHVO_MODE_HOST);
383 extcon_set_cable_state(&tu->extcon, "USB", tu->vbus_state);
385 /* Create OTG interface */
386 tahvo_usb_power_off(tu);
387 tu->phy.dev = &pdev->dev;
388 tu->phy.state = OTG_STATE_UNDEFINED;
389 tu->phy.label = DRIVER_NAME;
390 tu->phy.set_suspend = tahvo_usb_set_suspend;
392 tu->phy.otg->phy = &tu->phy;
393 tu->phy.otg->set_host = tahvo_usb_set_host;
394 tu->phy.otg->set_peripheral = tahvo_usb_set_peripheral;
396 ret = usb_add_phy(&tu->phy, USB_PHY_TYPE_USB2);
398 dev_err(&pdev->dev, "cannot register USB transceiver: %d\n",
400 goto err_extcon_unreg;
403 dev_set_drvdata(&pdev->dev, tu);
405 tu->irq = platform_get_irq(pdev, 0);
406 ret = request_threaded_irq(tu->irq, NULL, tahvo_usb_vbus_interrupt, 0,
409 dev_err(&pdev->dev, "could not register tahvo-vbus irq: %d\n",
415 ret = sysfs_create_group(&pdev->dev.kobj, &tahvo_attr_group);
417 dev_err(&pdev->dev, "cannot create sysfs group: %d\n", ret);
424 free_irq(tu->irq, tu);
426 usb_remove_phy(&tu->phy);
428 extcon_dev_unregister(&tu->extcon);
430 if (!IS_ERR(tu->ick))
431 clk_disable(tu->ick);
436 static int tahvo_usb_remove(struct platform_device *pdev)
438 struct tahvo_usb *tu = platform_get_drvdata(pdev);
440 sysfs_remove_group(&pdev->dev.kobj, &tahvo_attr_group);
441 free_irq(tu->irq, tu);
442 usb_remove_phy(&tu->phy);
443 extcon_dev_unregister(&tu->extcon);
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,
455 .owner = THIS_MODULE,
458 module_platform_driver(tahvo_usb_driver);
460 MODULE_DESCRIPTION("Tahvo USB transceiver driver");
461 MODULE_LICENSE("GPL");
462 MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");
463 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");