Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * NOP USB transceiver for all USB transceiver which are either built-in
0004  * into USB IP or which are mostly autonomous.
0005  *
0006  * Copyright (C) 2009 Texas Instruments Inc
0007  * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
0008  *
0009  * Current status:
0010  *  This provides a "nop" transceiver for PHYs which are
0011  *  autonomous such as isp1504, isp1707, etc.
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/dma-mapping.h>
0017 #include <linux/usb/gadget.h>
0018 #include <linux/usb/otg.h>
0019 #include <linux/usb/usb_phy_generic.h>
0020 #include <linux/slab.h>
0021 #include <linux/clk.h>
0022 #include <linux/regulator/consumer.h>
0023 #include <linux/of.h>
0024 #include <linux/gpio/consumer.h>
0025 #include <linux/delay.h>
0026 
0027 #include "phy-generic.h"
0028 
0029 #define VBUS_IRQ_FLAGS \
0030     (IRQF_SHARED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | \
0031         IRQF_ONESHOT)
0032 
0033 struct platform_device *usb_phy_generic_register(void)
0034 {
0035     return platform_device_register_simple("usb_phy_generic",
0036             PLATFORM_DEVID_AUTO, NULL, 0);
0037 }
0038 EXPORT_SYMBOL_GPL(usb_phy_generic_register);
0039 
0040 void usb_phy_generic_unregister(struct platform_device *pdev)
0041 {
0042     platform_device_unregister(pdev);
0043 }
0044 EXPORT_SYMBOL_GPL(usb_phy_generic_unregister);
0045 
0046 static int nop_set_suspend(struct usb_phy *x, int suspend)
0047 {
0048     struct usb_phy_generic *nop = dev_get_drvdata(x->dev);
0049 
0050     if (!IS_ERR(nop->clk)) {
0051         if (suspend)
0052             clk_disable_unprepare(nop->clk);
0053         else
0054             clk_prepare_enable(nop->clk);
0055     }
0056 
0057     return 0;
0058 }
0059 
0060 static void nop_reset(struct usb_phy_generic *nop)
0061 {
0062     if (!nop->gpiod_reset)
0063         return;
0064 
0065     gpiod_set_value_cansleep(nop->gpiod_reset, 1);
0066     usleep_range(10000, 20000);
0067     gpiod_set_value_cansleep(nop->gpiod_reset, 0);
0068 }
0069 
0070 /* interface to regulator framework */
0071 static void nop_set_vbus_draw(struct usb_phy_generic *nop, unsigned mA)
0072 {
0073     struct regulator *vbus_draw = nop->vbus_draw;
0074     int enabled;
0075     int ret;
0076 
0077     if (!vbus_draw)
0078         return;
0079 
0080     enabled = nop->vbus_draw_enabled;
0081     if (mA) {
0082         regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
0083         if (!enabled) {
0084             ret = regulator_enable(vbus_draw);
0085             if (ret < 0)
0086                 return;
0087             nop->vbus_draw_enabled = 1;
0088         }
0089     } else {
0090         if (enabled) {
0091             ret = regulator_disable(vbus_draw);
0092             if (ret < 0)
0093                 return;
0094             nop->vbus_draw_enabled = 0;
0095         }
0096     }
0097     nop->mA = mA;
0098 }
0099 
0100 
0101 static irqreturn_t nop_gpio_vbus_thread(int irq, void *data)
0102 {
0103     struct usb_phy_generic *nop = data;
0104     struct usb_otg *otg = nop->phy.otg;
0105     int vbus, status;
0106 
0107     vbus = gpiod_get_value(nop->gpiod_vbus);
0108     if ((vbus ^ nop->vbus) == 0)
0109         return IRQ_HANDLED;
0110     nop->vbus = vbus;
0111 
0112     if (vbus) {
0113         status = USB_EVENT_VBUS;
0114         otg->state = OTG_STATE_B_PERIPHERAL;
0115         nop->phy.last_event = status;
0116 
0117         /* drawing a "unit load" is *always* OK, except for OTG */
0118         nop_set_vbus_draw(nop, 100);
0119 
0120         atomic_notifier_call_chain(&nop->phy.notifier, status,
0121                        otg->gadget);
0122     } else {
0123         nop_set_vbus_draw(nop, 0);
0124 
0125         status = USB_EVENT_NONE;
0126         otg->state = OTG_STATE_B_IDLE;
0127         nop->phy.last_event = status;
0128 
0129         atomic_notifier_call_chain(&nop->phy.notifier, status,
0130                        otg->gadget);
0131     }
0132     return IRQ_HANDLED;
0133 }
0134 
0135 int usb_gen_phy_init(struct usb_phy *phy)
0136 {
0137     struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
0138     int ret;
0139 
0140     if (!IS_ERR(nop->vcc)) {
0141         if (regulator_enable(nop->vcc))
0142             dev_err(phy->dev, "Failed to enable power\n");
0143     }
0144 
0145     if (!IS_ERR(nop->clk)) {
0146         ret = clk_prepare_enable(nop->clk);
0147         if (ret)
0148             return ret;
0149     }
0150 
0151     nop_reset(nop);
0152 
0153     return 0;
0154 }
0155 EXPORT_SYMBOL_GPL(usb_gen_phy_init);
0156 
0157 void usb_gen_phy_shutdown(struct usb_phy *phy)
0158 {
0159     struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
0160 
0161     gpiod_set_value_cansleep(nop->gpiod_reset, 1);
0162 
0163     if (!IS_ERR(nop->clk))
0164         clk_disable_unprepare(nop->clk);
0165 
0166     if (!IS_ERR(nop->vcc)) {
0167         if (regulator_disable(nop->vcc))
0168             dev_err(phy->dev, "Failed to disable power\n");
0169     }
0170 }
0171 EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
0172 
0173 static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
0174 {
0175     if (!otg)
0176         return -ENODEV;
0177 
0178     if (!gadget) {
0179         otg->gadget = NULL;
0180         return -ENODEV;
0181     }
0182 
0183     otg->gadget = gadget;
0184     if (otg->state == OTG_STATE_B_PERIPHERAL)
0185         atomic_notifier_call_chain(&otg->usb_phy->notifier,
0186                        USB_EVENT_VBUS, otg->gadget);
0187     else
0188         otg->state = OTG_STATE_B_IDLE;
0189     return 0;
0190 }
0191 
0192 static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
0193 {
0194     if (!otg)
0195         return -ENODEV;
0196 
0197     if (!host) {
0198         otg->host = NULL;
0199         return -ENODEV;
0200     }
0201 
0202     otg->host = host;
0203     return 0;
0204 }
0205 
0206 int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_generic *nop)
0207 {
0208     enum usb_phy_type type = USB_PHY_TYPE_USB2;
0209     int err = 0;
0210 
0211     u32 clk_rate = 0;
0212     bool needs_vcc = false, needs_clk = false;
0213 
0214     if (dev->of_node) {
0215         struct device_node *node = dev->of_node;
0216 
0217         if (of_property_read_u32(node, "clock-frequency", &clk_rate))
0218             clk_rate = 0;
0219 
0220         needs_vcc = of_property_read_bool(node, "vcc-supply");
0221         needs_clk = of_property_read_bool(node, "clocks");
0222     }
0223     nop->gpiod_reset = devm_gpiod_get_optional(dev, "reset",
0224                            GPIOD_ASIS);
0225     err = PTR_ERR_OR_ZERO(nop->gpiod_reset);
0226     if (!err) {
0227         nop->gpiod_vbus = devm_gpiod_get_optional(dev,
0228                          "vbus-detect",
0229                          GPIOD_ASIS);
0230         err = PTR_ERR_OR_ZERO(nop->gpiod_vbus);
0231     }
0232 
0233     if (err == -EPROBE_DEFER)
0234         return -EPROBE_DEFER;
0235     if (err) {
0236         dev_err(dev, "Error requesting RESET or VBUS GPIO\n");
0237         return err;
0238     }
0239     if (nop->gpiod_reset)
0240         gpiod_direction_output(nop->gpiod_reset, 1);
0241 
0242     nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
0243             GFP_KERNEL);
0244     if (!nop->phy.otg)
0245         return -ENOMEM;
0246 
0247     nop->clk = devm_clk_get(dev, "main_clk");
0248     if (IS_ERR(nop->clk)) {
0249         dev_dbg(dev, "Can't get phy clock: %ld\n",
0250                     PTR_ERR(nop->clk));
0251         if (needs_clk)
0252             return PTR_ERR(nop->clk);
0253     }
0254 
0255     if (!IS_ERR(nop->clk) && clk_rate) {
0256         err = clk_set_rate(nop->clk, clk_rate);
0257         if (err) {
0258             dev_err(dev, "Error setting clock rate\n");
0259             return err;
0260         }
0261     }
0262 
0263     nop->vcc = devm_regulator_get(dev, "vcc");
0264     if (IS_ERR(nop->vcc)) {
0265         dev_dbg(dev, "Error getting vcc regulator: %ld\n",
0266                     PTR_ERR(nop->vcc));
0267         if (needs_vcc)
0268             return -EPROBE_DEFER;
0269     }
0270 
0271     nop->vbus_draw = devm_regulator_get_exclusive(dev, "vbus");
0272     if (PTR_ERR(nop->vbus_draw) == -ENODEV)
0273         nop->vbus_draw = NULL;
0274     if (IS_ERR(nop->vbus_draw))
0275         return dev_err_probe(dev, PTR_ERR(nop->vbus_draw),
0276                      "could not get vbus regulator\n");
0277 
0278     nop->dev        = dev;
0279     nop->phy.dev        = nop->dev;
0280     nop->phy.label      = "nop-xceiv";
0281     nop->phy.set_suspend    = nop_set_suspend;
0282     nop->phy.type       = type;
0283 
0284     nop->phy.otg->state     = OTG_STATE_UNDEFINED;
0285     nop->phy.otg->usb_phy       = &nop->phy;
0286     nop->phy.otg->set_host      = nop_set_host;
0287     nop->phy.otg->set_peripheral    = nop_set_peripheral;
0288 
0289     return 0;
0290 }
0291 EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
0292 
0293 static int usb_phy_generic_probe(struct platform_device *pdev)
0294 {
0295     struct device *dev = &pdev->dev;
0296     struct usb_phy_generic  *nop;
0297     int err;
0298 
0299     nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
0300     if (!nop)
0301         return -ENOMEM;
0302 
0303     err = usb_phy_gen_create_phy(dev, nop);
0304     if (err)
0305         return err;
0306     if (nop->gpiod_vbus) {
0307         err = devm_request_threaded_irq(&pdev->dev,
0308                         gpiod_to_irq(nop->gpiod_vbus),
0309                         NULL, nop_gpio_vbus_thread,
0310                         VBUS_IRQ_FLAGS, "vbus_detect",
0311                         nop);
0312         if (err) {
0313             dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
0314                 gpiod_to_irq(nop->gpiod_vbus), err);
0315             return err;
0316         }
0317         nop->phy.otg->state = gpiod_get_value(nop->gpiod_vbus) ?
0318             OTG_STATE_B_PERIPHERAL : OTG_STATE_B_IDLE;
0319     }
0320 
0321     nop->phy.init       = usb_gen_phy_init;
0322     nop->phy.shutdown   = usb_gen_phy_shutdown;
0323 
0324     err = usb_add_phy_dev(&nop->phy);
0325     if (err) {
0326         dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
0327             err);
0328         return err;
0329     }
0330 
0331     platform_set_drvdata(pdev, nop);
0332 
0333     return 0;
0334 }
0335 
0336 static int usb_phy_generic_remove(struct platform_device *pdev)
0337 {
0338     struct usb_phy_generic *nop = platform_get_drvdata(pdev);
0339 
0340     usb_remove_phy(&nop->phy);
0341 
0342     return 0;
0343 }
0344 
0345 static const struct of_device_id nop_xceiv_dt_ids[] = {
0346     { .compatible = "usb-nop-xceiv" },
0347     { }
0348 };
0349 
0350 MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
0351 
0352 static struct platform_driver usb_phy_generic_driver = {
0353     .probe      = usb_phy_generic_probe,
0354     .remove     = usb_phy_generic_remove,
0355     .driver     = {
0356         .name   = "usb_phy_generic",
0357         .of_match_table = nop_xceiv_dt_ids,
0358     },
0359 };
0360 
0361 static int __init usb_phy_generic_init(void)
0362 {
0363     return platform_driver_register(&usb_phy_generic_driver);
0364 }
0365 subsys_initcall(usb_phy_generic_init);
0366 
0367 static void __exit usb_phy_generic_exit(void)
0368 {
0369     platform_driver_unregister(&usb_phy_generic_driver);
0370 }
0371 module_exit(usb_phy_generic_exit);
0372 
0373 MODULE_ALIAS("platform:usb_phy_generic");
0374 MODULE_AUTHOR("Texas Instruments Inc");
0375 MODULE_DESCRIPTION("NOP USB Transceiver driver");
0376 MODULE_LICENSE("GPL");