1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * NOP USB transceiver for all USB transceiver which are either built-in 4 * into USB IP or which are mostly autonomous. 5 * 6 * Copyright (C) 2009 Texas Instruments Inc 7 * Author: Ajay Kumar Gupta <ajay.gupta@ti.com> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 * 23 * Current status: 24 * This provides a "nop" transceiver for PHYs which are 25 * autonomous such as isp1504, isp1707, etc. 26 */ 27 28 #include <linux/module.h> 29 #include <linux/platform_device.h> 30 #include <linux/dma-mapping.h> 31 #include <linux/usb/gadget.h> 32 #include <linux/usb/otg.h> 33 #include <linux/usb/usb_phy_generic.h> 34 #include <linux/slab.h> 35 #include <linux/clk.h> 36 #include <linux/regulator/consumer.h> 37 #include <linux/of.h> 38 #include <linux/of_gpio.h> 39 #include <linux/gpio.h> 40 #include <linux/delay.h> 41 42 #include "phy-generic.h" 43 44 #define VBUS_IRQ_FLAGS \ 45 (IRQF_SHARED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | \ 46 IRQF_ONESHOT) 47 48 struct platform_device *usb_phy_generic_register(void) 49 { 50 return platform_device_register_simple("usb_phy_generic", 51 PLATFORM_DEVID_AUTO, NULL, 0); 52 } 53 EXPORT_SYMBOL_GPL(usb_phy_generic_register); 54 55 void usb_phy_generic_unregister(struct platform_device *pdev) 56 { 57 platform_device_unregister(pdev); 58 } 59 EXPORT_SYMBOL_GPL(usb_phy_generic_unregister); 60 61 static int nop_set_suspend(struct usb_phy *x, int suspend) 62 { 63 struct usb_phy_generic *nop = dev_get_drvdata(x->dev); 64 65 if (!IS_ERR(nop->clk)) { 66 if (suspend) 67 clk_disable_unprepare(nop->clk); 68 else 69 clk_prepare_enable(nop->clk); 70 } 71 72 return 0; 73 } 74 75 static void nop_reset(struct usb_phy_generic *nop) 76 { 77 if (!nop->gpiod_reset) 78 return; 79 80 gpiod_set_value(nop->gpiod_reset, 1); 81 usleep_range(10000, 20000); 82 gpiod_set_value(nop->gpiod_reset, 0); 83 } 84 85 /* interface to regulator framework */ 86 static void nop_set_vbus_draw(struct usb_phy_generic *nop, unsigned mA) 87 { 88 struct regulator *vbus_draw = nop->vbus_draw; 89 int enabled; 90 int ret; 91 92 if (!vbus_draw) 93 return; 94 95 enabled = nop->vbus_draw_enabled; 96 if (mA) { 97 regulator_set_current_limit(vbus_draw, 0, 1000 * mA); 98 if (!enabled) { 99 ret = regulator_enable(vbus_draw); 100 if (ret < 0) 101 return; 102 nop->vbus_draw_enabled = 1; 103 } 104 } else { 105 if (enabled) { 106 ret = regulator_disable(vbus_draw); 107 if (ret < 0) 108 return; 109 nop->vbus_draw_enabled = 0; 110 } 111 } 112 nop->mA = mA; 113 } 114 115 116 static irqreturn_t nop_gpio_vbus_thread(int irq, void *data) 117 { 118 struct usb_phy_generic *nop = data; 119 struct usb_otg *otg = nop->phy.otg; 120 int vbus, status; 121 122 vbus = gpiod_get_value(nop->gpiod_vbus); 123 if ((vbus ^ nop->vbus) == 0) 124 return IRQ_HANDLED; 125 nop->vbus = vbus; 126 127 if (vbus) { 128 status = USB_EVENT_VBUS; 129 otg->state = OTG_STATE_B_PERIPHERAL; 130 nop->phy.last_event = status; 131 132 /* drawing a "unit load" is *always* OK, except for OTG */ 133 nop_set_vbus_draw(nop, 100); 134 135 atomic_notifier_call_chain(&nop->phy.notifier, status, 136 otg->gadget); 137 } else { 138 nop_set_vbus_draw(nop, 0); 139 140 status = USB_EVENT_NONE; 141 otg->state = OTG_STATE_B_IDLE; 142 nop->phy.last_event = status; 143 144 atomic_notifier_call_chain(&nop->phy.notifier, status, 145 otg->gadget); 146 } 147 return IRQ_HANDLED; 148 } 149 150 int usb_gen_phy_init(struct usb_phy *phy) 151 { 152 struct usb_phy_generic *nop = dev_get_drvdata(phy->dev); 153 int ret; 154 155 if (!IS_ERR(nop->vcc)) { 156 if (regulator_enable(nop->vcc)) 157 dev_err(phy->dev, "Failed to enable power\n"); 158 } 159 160 if (!IS_ERR(nop->clk)) { 161 ret = clk_prepare_enable(nop->clk); 162 if (ret) 163 return ret; 164 } 165 166 nop_reset(nop); 167 168 return 0; 169 } 170 EXPORT_SYMBOL_GPL(usb_gen_phy_init); 171 172 void usb_gen_phy_shutdown(struct usb_phy *phy) 173 { 174 struct usb_phy_generic *nop = dev_get_drvdata(phy->dev); 175 176 gpiod_set_value(nop->gpiod_reset, 1); 177 178 if (!IS_ERR(nop->clk)) 179 clk_disable_unprepare(nop->clk); 180 181 if (!IS_ERR(nop->vcc)) { 182 if (regulator_disable(nop->vcc)) 183 dev_err(phy->dev, "Failed to disable power\n"); 184 } 185 } 186 EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown); 187 188 static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget) 189 { 190 if (!otg) 191 return -ENODEV; 192 193 if (!gadget) { 194 otg->gadget = NULL; 195 return -ENODEV; 196 } 197 198 otg->gadget = gadget; 199 if (otg->state == OTG_STATE_B_PERIPHERAL) 200 atomic_notifier_call_chain(&otg->usb_phy->notifier, 201 USB_EVENT_VBUS, otg->gadget); 202 else 203 otg->state = OTG_STATE_B_IDLE; 204 return 0; 205 } 206 207 static int nop_set_host(struct usb_otg *otg, struct usb_bus *host) 208 { 209 if (!otg) 210 return -ENODEV; 211 212 if (!host) { 213 otg->host = NULL; 214 return -ENODEV; 215 } 216 217 otg->host = host; 218 return 0; 219 } 220 221 int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_generic *nop, 222 struct usb_phy_generic_platform_data *pdata) 223 { 224 enum usb_phy_type type = USB_PHY_TYPE_USB2; 225 int err = 0; 226 227 u32 clk_rate = 0; 228 bool needs_vcc = false, needs_clk = false; 229 230 if (dev->of_node) { 231 struct device_node *node = dev->of_node; 232 233 if (of_property_read_u32(node, "clock-frequency", &clk_rate)) 234 clk_rate = 0; 235 236 needs_vcc = of_property_read_bool(node, "vcc-supply"); 237 needs_clk = of_property_read_bool(node, "clocks"); 238 nop->gpiod_reset = devm_gpiod_get_optional(dev, "reset", 239 GPIOD_ASIS); 240 err = PTR_ERR_OR_ZERO(nop->gpiod_reset); 241 if (!err) { 242 nop->gpiod_vbus = devm_gpiod_get_optional(dev, 243 "vbus-detect", 244 GPIOD_ASIS); 245 err = PTR_ERR_OR_ZERO(nop->gpiod_vbus); 246 } 247 } else if (pdata) { 248 type = pdata->type; 249 clk_rate = pdata->clk_rate; 250 needs_vcc = pdata->needs_vcc; 251 if (gpio_is_valid(pdata->gpio_reset)) { 252 err = devm_gpio_request_one(dev, pdata->gpio_reset, 253 GPIOF_ACTIVE_LOW, 254 dev_name(dev)); 255 if (!err) 256 nop->gpiod_reset = 257 gpio_to_desc(pdata->gpio_reset); 258 } 259 nop->gpiod_vbus = pdata->gpiod_vbus; 260 } 261 262 if (err == -EPROBE_DEFER) 263 return -EPROBE_DEFER; 264 if (err) { 265 dev_err(dev, "Error requesting RESET or VBUS GPIO\n"); 266 return err; 267 } 268 if (nop->gpiod_reset) 269 gpiod_direction_output(nop->gpiod_reset, 1); 270 271 nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg), 272 GFP_KERNEL); 273 if (!nop->phy.otg) 274 return -ENOMEM; 275 276 nop->clk = devm_clk_get(dev, "main_clk"); 277 if (IS_ERR(nop->clk)) { 278 dev_dbg(dev, "Can't get phy clock: %ld\n", 279 PTR_ERR(nop->clk)); 280 if (needs_clk) 281 return PTR_ERR(nop->clk); 282 } 283 284 if (!IS_ERR(nop->clk) && clk_rate) { 285 err = clk_set_rate(nop->clk, clk_rate); 286 if (err) { 287 dev_err(dev, "Error setting clock rate\n"); 288 return err; 289 } 290 } 291 292 nop->vcc = devm_regulator_get(dev, "vcc"); 293 if (IS_ERR(nop->vcc)) { 294 dev_dbg(dev, "Error getting vcc regulator: %ld\n", 295 PTR_ERR(nop->vcc)); 296 if (needs_vcc) 297 return -EPROBE_DEFER; 298 } 299 300 nop->dev = dev; 301 nop->phy.dev = nop->dev; 302 nop->phy.label = "nop-xceiv"; 303 nop->phy.set_suspend = nop_set_suspend; 304 nop->phy.type = type; 305 306 nop->phy.otg->state = OTG_STATE_UNDEFINED; 307 nop->phy.otg->usb_phy = &nop->phy; 308 nop->phy.otg->set_host = nop_set_host; 309 nop->phy.otg->set_peripheral = nop_set_peripheral; 310 311 return 0; 312 } 313 EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy); 314 315 static int usb_phy_generic_probe(struct platform_device *pdev) 316 { 317 struct device *dev = &pdev->dev; 318 struct usb_phy_generic *nop; 319 int err; 320 321 nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL); 322 if (!nop) 323 return -ENOMEM; 324 325 err = usb_phy_gen_create_phy(dev, nop, dev_get_platdata(&pdev->dev)); 326 if (err) 327 return err; 328 if (nop->gpiod_vbus) { 329 err = devm_request_threaded_irq(&pdev->dev, 330 gpiod_to_irq(nop->gpiod_vbus), 331 NULL, nop_gpio_vbus_thread, 332 VBUS_IRQ_FLAGS, "vbus_detect", 333 nop); 334 if (err) { 335 dev_err(&pdev->dev, "can't request irq %i, err: %d\n", 336 gpiod_to_irq(nop->gpiod_vbus), err); 337 return err; 338 } 339 nop->phy.otg->state = gpiod_get_value(nop->gpiod_vbus) ? 340 OTG_STATE_B_PERIPHERAL : OTG_STATE_B_IDLE; 341 } 342 343 nop->phy.init = usb_gen_phy_init; 344 nop->phy.shutdown = usb_gen_phy_shutdown; 345 346 err = usb_add_phy_dev(&nop->phy); 347 if (err) { 348 dev_err(&pdev->dev, "can't register transceiver, err: %d\n", 349 err); 350 return err; 351 } 352 353 platform_set_drvdata(pdev, nop); 354 355 return 0; 356 } 357 358 static int usb_phy_generic_remove(struct platform_device *pdev) 359 { 360 struct usb_phy_generic *nop = platform_get_drvdata(pdev); 361 362 usb_remove_phy(&nop->phy); 363 364 return 0; 365 } 366 367 static const struct of_device_id nop_xceiv_dt_ids[] = { 368 { .compatible = "usb-nop-xceiv" }, 369 { } 370 }; 371 372 MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids); 373 374 static struct platform_driver usb_phy_generic_driver = { 375 .probe = usb_phy_generic_probe, 376 .remove = usb_phy_generic_remove, 377 .driver = { 378 .name = "usb_phy_generic", 379 .of_match_table = nop_xceiv_dt_ids, 380 }, 381 }; 382 383 static int __init usb_phy_generic_init(void) 384 { 385 return platform_driver_register(&usb_phy_generic_driver); 386 } 387 subsys_initcall(usb_phy_generic_init); 388 389 static void __exit usb_phy_generic_exit(void) 390 { 391 platform_driver_unregister(&usb_phy_generic_driver); 392 } 393 module_exit(usb_phy_generic_exit); 394 395 MODULE_ALIAS("platform:usb_phy_generic"); 396 MODULE_AUTHOR("Texas Instruments Inc"); 397 MODULE_DESCRIPTION("NOP USB Transceiver driver"); 398 MODULE_LICENSE("GPL"); 399