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/string_choices.h>
22 #include <linux/usb/otg.h>
23 #include <linux/mfd/retu.h>
24 #include <linux/usb/gadget.h>
25 #include <linux/platform_device.h>
27 #define DRIVER_NAME "tahvo-usb"
29 #define TAHVO_REG_IDSR 0x02
30 #define TAHVO_REG_USBR 0x06
32 #define USBR_SLAVE_CONTROL (1 << 8)
33 #define USBR_VPPVIO_SW (1 << 7)
34 #define USBR_SPEED (1 << 6)
35 #define USBR_REGOUT (1 << 5)
36 #define USBR_MASTER_SW2 (1 << 4)
37 #define USBR_MASTER_SW1 (1 << 3)
38 #define USBR_SLAVE_SW (1 << 2)
39 #define USBR_NSUSPEND (1 << 1)
40 #define USBR_SEMODE (1 << 0)
42 #define TAHVO_MODE_HOST 0
43 #define TAHVO_MODE_PERIPHERAL 1
46 struct platform_device *pt_dev;
49 struct mutex serialize;
53 struct extcon_dev *extcon;
56 static const unsigned int tahvo_cable[] = {
63 static ssize_t vbus_show(struct device *device,
64 struct device_attribute *attr, char *buf)
66 struct tahvo_usb *tu = dev_get_drvdata(device);
67 return sprintf(buf, "%s\n", str_on_off(tu->vbus_state));
69 static DEVICE_ATTR_RO(vbus);
71 static void check_vbus_state(struct tahvo_usb *tu)
73 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
76 reg = retu_read(rdev, TAHVO_REG_IDSR);
77 if (reg & TAHVO_STAT_VBUS) {
78 switch (tu->phy.otg->state) {
79 case OTG_STATE_B_IDLE:
80 /* Enable the gadget driver */
81 if (tu->phy.otg->gadget)
82 usb_gadget_vbus_connect(tu->phy.otg->gadget);
83 tu->phy.otg->state = OTG_STATE_B_PERIPHERAL;
84 usb_phy_set_event(&tu->phy, USB_EVENT_ENUMERATED);
86 case OTG_STATE_A_IDLE:
88 * Session is now valid assuming the USB hub is driving
91 tu->phy.otg->state = OTG_STATE_A_HOST;
96 dev_info(&tu->pt_dev->dev, "USB cable connected\n");
98 switch (tu->phy.otg->state) {
99 case OTG_STATE_B_PERIPHERAL:
100 if (tu->phy.otg->gadget)
101 usb_gadget_vbus_disconnect(tu->phy.otg->gadget);
102 tu->phy.otg->state = OTG_STATE_B_IDLE;
103 usb_phy_set_event(&tu->phy, USB_EVENT_NONE);
105 case OTG_STATE_A_HOST:
106 tu->phy.otg->state = OTG_STATE_A_IDLE;
111 dev_info(&tu->pt_dev->dev, "USB cable disconnected\n");
114 prev_state = tu->vbus_state;
115 tu->vbus_state = reg & TAHVO_STAT_VBUS;
116 if (prev_state != tu->vbus_state) {
117 extcon_set_state_sync(tu->extcon, EXTCON_USB, tu->vbus_state);
118 sysfs_notify(&tu->pt_dev->dev.kobj, NULL, "vbus_state");
122 static void tahvo_usb_become_host(struct tahvo_usb *tu)
124 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
126 extcon_set_state_sync(tu->extcon, EXTCON_USB_HOST, true);
128 /* Power up the transceiver in USB host mode */
129 retu_write(rdev, TAHVO_REG_USBR, USBR_REGOUT | USBR_NSUSPEND |
130 USBR_MASTER_SW2 | USBR_MASTER_SW1);
131 tu->phy.otg->state = OTG_STATE_A_IDLE;
133 check_vbus_state(tu);
136 static void tahvo_usb_stop_host(struct tahvo_usb *tu)
138 tu->phy.otg->state = OTG_STATE_A_IDLE;
141 static void tahvo_usb_become_peripheral(struct tahvo_usb *tu)
143 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
145 extcon_set_state_sync(tu->extcon, EXTCON_USB_HOST, false);
147 /* Power up transceiver and set it in USB peripheral mode */
148 retu_write(rdev, TAHVO_REG_USBR, USBR_SLAVE_CONTROL | USBR_REGOUT |
149 USBR_NSUSPEND | USBR_SLAVE_SW);
150 tu->phy.otg->state = OTG_STATE_B_IDLE;
152 check_vbus_state(tu);
155 static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu)
157 if (tu->phy.otg->gadget)
158 usb_gadget_vbus_disconnect(tu->phy.otg->gadget);
159 tu->phy.otg->state = OTG_STATE_B_IDLE;
162 static void tahvo_usb_power_off(struct tahvo_usb *tu)
164 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
166 /* Disable gadget controller if any */
167 if (tu->phy.otg->gadget)
168 usb_gadget_vbus_disconnect(tu->phy.otg->gadget);
170 /* Power off transceiver */
171 retu_write(rdev, TAHVO_REG_USBR, 0);
172 tu->phy.otg->state = OTG_STATE_UNDEFINED;
175 static int tahvo_usb_set_suspend(struct usb_phy *dev, int suspend)
177 struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, phy);
178 struct retu_dev *rdev = dev_get_drvdata(tu->pt_dev->dev.parent);
181 dev_dbg(&tu->pt_dev->dev, "%s\n", __func__);
183 w = retu_read(rdev, TAHVO_REG_USBR);
188 retu_write(rdev, TAHVO_REG_USBR, w);
193 static int tahvo_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
195 struct tahvo_usb *tu = container_of(otg->usb_phy, struct tahvo_usb,
198 mutex_lock(&tu->serialize);
201 if (tu->tahvo_mode == TAHVO_MODE_HOST)
202 tahvo_usb_power_off(tu);
204 mutex_unlock(&tu->serialize);
208 if (tu->tahvo_mode == TAHVO_MODE_HOST) {
210 tahvo_usb_become_host(tu);
215 mutex_unlock(&tu->serialize);
220 static int tahvo_usb_set_peripheral(struct usb_otg *otg,
221 struct usb_gadget *gadget)
223 struct tahvo_usb *tu = container_of(otg->usb_phy, struct tahvo_usb,
226 mutex_lock(&tu->serialize);
229 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
230 tahvo_usb_power_off(tu);
231 tu->phy.otg->gadget = NULL;
232 mutex_unlock(&tu->serialize);
236 tu->phy.otg->gadget = gadget;
237 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
238 tahvo_usb_become_peripheral(tu);
240 mutex_unlock(&tu->serialize);
245 static irqreturn_t tahvo_usb_vbus_interrupt(int irq, void *_tu)
247 struct tahvo_usb *tu = _tu;
249 mutex_lock(&tu->serialize);
250 check_vbus_state(tu);
251 mutex_unlock(&tu->serialize);
256 static ssize_t otg_mode_show(struct device *device,
257 struct device_attribute *attr, char *buf)
259 struct tahvo_usb *tu = dev_get_drvdata(device);
261 switch (tu->tahvo_mode) {
262 case TAHVO_MODE_HOST:
263 return sprintf(buf, "host\n");
264 case TAHVO_MODE_PERIPHERAL:
265 return sprintf(buf, "peripheral\n");
271 static ssize_t otg_mode_store(struct device *device,
272 struct device_attribute *attr,
273 const char *buf, size_t count)
275 struct tahvo_usb *tu = dev_get_drvdata(device);
278 mutex_lock(&tu->serialize);
279 if (count >= 4 && strncmp(buf, "host", 4) == 0) {
280 if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
281 tahvo_usb_stop_peripheral(tu);
282 tu->tahvo_mode = TAHVO_MODE_HOST;
283 if (tu->phy.otg->host) {
284 dev_info(device, "HOST mode: host controller present\n");
285 tahvo_usb_become_host(tu);
287 dev_info(device, "HOST mode: no host controller, powering off\n");
288 tahvo_usb_power_off(tu);
291 } else if (count >= 10 && strncmp(buf, "peripheral", 10) == 0) {
292 if (tu->tahvo_mode == TAHVO_MODE_HOST)
293 tahvo_usb_stop_host(tu);
294 tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
295 if (tu->phy.otg->gadget) {
296 dev_info(device, "PERIPHERAL mode: gadget driver present\n");
297 tahvo_usb_become_peripheral(tu);
299 dev_info(device, "PERIPHERAL mode: no gadget driver, powering off\n");
300 tahvo_usb_power_off(tu);
306 mutex_unlock(&tu->serialize);
310 static DEVICE_ATTR_RW(otg_mode);
312 static struct attribute *tahvo_attrs[] = {
314 &dev_attr_otg_mode.attr,
317 ATTRIBUTE_GROUPS(tahvo);
319 static int tahvo_usb_probe(struct platform_device *pdev)
321 struct retu_dev *rdev = dev_get_drvdata(pdev->dev.parent);
322 struct tahvo_usb *tu;
325 tu = devm_kzalloc(&pdev->dev, sizeof(*tu), GFP_KERNEL);
329 tu->phy.otg = devm_kzalloc(&pdev->dev, sizeof(*tu->phy.otg),
337 #ifdef CONFIG_TAHVO_USB_HOST_BY_DEFAULT
338 tu->tahvo_mode = TAHVO_MODE_HOST;
340 tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
343 mutex_init(&tu->serialize);
345 tu->ick = devm_clk_get(&pdev->dev, "usb_l4_ick");
346 if (!IS_ERR(tu->ick))
350 * Set initial state, so that we generate kevents only on state changes.
352 tu->vbus_state = retu_read(rdev, TAHVO_REG_IDSR) & TAHVO_STAT_VBUS;
354 tu->extcon = devm_extcon_dev_allocate(&pdev->dev, tahvo_cable);
355 if (IS_ERR(tu->extcon)) {
356 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
357 ret = PTR_ERR(tu->extcon);
358 goto err_disable_clk;
361 ret = devm_extcon_dev_register(&pdev->dev, tu->extcon);
363 dev_err(&pdev->dev, "could not register extcon device: %d\n",
365 goto err_disable_clk;
368 /* Set the initial cable state. */
369 extcon_set_state_sync(tu->extcon, EXTCON_USB_HOST,
370 tu->tahvo_mode == TAHVO_MODE_HOST);
371 extcon_set_state_sync(tu->extcon, EXTCON_USB, tu->vbus_state);
373 /* Create OTG interface */
374 tahvo_usb_power_off(tu);
375 tu->phy.dev = &pdev->dev;
376 tu->phy.otg->state = OTG_STATE_UNDEFINED;
377 tu->phy.label = DRIVER_NAME;
378 tu->phy.set_suspend = tahvo_usb_set_suspend;
380 tu->phy.otg->usb_phy = &tu->phy;
381 tu->phy.otg->set_host = tahvo_usb_set_host;
382 tu->phy.otg->set_peripheral = tahvo_usb_set_peripheral;
384 ret = usb_add_phy(&tu->phy, USB_PHY_TYPE_USB2);
386 dev_err(&pdev->dev, "cannot register USB transceiver: %d\n",
388 goto err_disable_clk;
391 dev_set_drvdata(&pdev->dev, tu);
393 tu->irq = ret = platform_get_irq(pdev, 0);
396 ret = request_threaded_irq(tu->irq, NULL, tahvo_usb_vbus_interrupt,
400 dev_err(&pdev->dev, "could not register tahvo-vbus irq: %d\n",
408 usb_remove_phy(&tu->phy);
410 if (!IS_ERR(tu->ick))
411 clk_disable(tu->ick);
416 static void tahvo_usb_remove(struct platform_device *pdev)
418 struct tahvo_usb *tu = platform_get_drvdata(pdev);
420 free_irq(tu->irq, tu);
421 usb_remove_phy(&tu->phy);
422 if (!IS_ERR(tu->ick))
423 clk_disable(tu->ick);
426 static struct platform_driver tahvo_usb_driver = {
427 .probe = tahvo_usb_probe,
428 .remove = tahvo_usb_remove,
431 .dev_groups = tahvo_groups,
434 module_platform_driver(tahvo_usb_driver);
436 MODULE_DESCRIPTION("Tahvo USB transceiver driver");
437 MODULE_LICENSE("GPL");
438 MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");