Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 1 | /* |
| 2 | * drivers/usb/otg/nop-usb-xceiv.c |
| 3 | * |
| 4 | * NOP USB transceiver for all USB transceiver which are either built-in |
| 5 | * into USB IP or which are mostly autonomous. |
| 6 | * |
| 7 | * Copyright (C) 2009 Texas Instruments Inc |
| 8 | * Author: Ajay Kumar Gupta <ajay.gupta@ti.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | * |
| 24 | * Current status: |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 25 | * This provides a "nop" transceiver for PHYs which are |
| 26 | * autonomous such as isp1504, isp1707, etc. |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/dma-mapping.h> |
| 32 | #include <linux/usb/otg.h> |
Felipe Balbi | 78c289f | 2012-07-19 13:32:15 +0300 | [diff] [blame] | 33 | #include <linux/usb/nop-usb-xceiv.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 35 | #include <linux/clk.h> |
Roger Quadros | 58f735f | 2013-03-12 13:24:22 +0200 | [diff] [blame] | 36 | #include <linux/regulator/consumer.h> |
Roger Quadros | 0eba387 | 2013-03-12 13:24:25 +0200 | [diff] [blame] | 37 | #include <linux/of.h> |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 38 | |
| 39 | struct nop_usb_xceiv { |
Roger Quadros | 0eba387 | 2013-03-12 13:24:25 +0200 | [diff] [blame] | 40 | struct usb_phy phy; |
| 41 | struct device *dev; |
| 42 | struct clk *clk; |
| 43 | struct regulator *vcc; |
| 44 | struct regulator *reset; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 45 | }; |
| 46 | |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 47 | static struct platform_device *pd; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 48 | |
| 49 | void usb_nop_xceiv_register(void) |
| 50 | { |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 51 | if (pd) |
| 52 | return; |
| 53 | pd = platform_device_register_simple("nop_usb_xceiv", -1, NULL, 0); |
| 54 | if (!pd) { |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 55 | printk(KERN_ERR "Unable to register usb nop transceiver\n"); |
| 56 | return; |
| 57 | } |
| 58 | } |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 59 | EXPORT_SYMBOL(usb_nop_xceiv_register); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 60 | |
| 61 | void usb_nop_xceiv_unregister(void) |
| 62 | { |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 63 | platform_device_unregister(pd); |
Ajay Kumar Gupta | dc7520c | 2009-07-03 13:18:45 +0530 | [diff] [blame] | 64 | pd = NULL; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 65 | } |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 66 | EXPORT_SYMBOL(usb_nop_xceiv_unregister); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 67 | |
Heikki Krogerus | 8675381 | 2012-02-13 13:24:02 +0200 | [diff] [blame] | 68 | static int nop_set_suspend(struct usb_phy *x, int suspend) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 69 | { |
| 70 | return 0; |
| 71 | } |
| 72 | |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 73 | static int nop_init(struct usb_phy *phy) |
| 74 | { |
| 75 | struct nop_usb_xceiv *nop = dev_get_drvdata(phy->dev); |
| 76 | |
Roger Quadros | 58f735f | 2013-03-12 13:24:22 +0200 | [diff] [blame] | 77 | if (!IS_ERR(nop->vcc)) { |
| 78 | if (regulator_enable(nop->vcc)) |
| 79 | dev_err(phy->dev, "Failed to enable power\n"); |
| 80 | } |
| 81 | |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 82 | if (!IS_ERR(nop->clk)) |
| 83 | clk_enable(nop->clk); |
| 84 | |
Roger Quadros | ad63ebfc | 2013-03-12 13:24:23 +0200 | [diff] [blame] | 85 | if (!IS_ERR(nop->reset)) { |
| 86 | /* De-assert RESET */ |
| 87 | if (regulator_enable(nop->reset)) |
| 88 | dev_err(phy->dev, "Failed to de-assert reset\n"); |
| 89 | } |
| 90 | |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static void nop_shutdown(struct usb_phy *phy) |
| 95 | { |
| 96 | struct nop_usb_xceiv *nop = dev_get_drvdata(phy->dev); |
| 97 | |
Roger Quadros | ad63ebfc | 2013-03-12 13:24:23 +0200 | [diff] [blame] | 98 | if (!IS_ERR(nop->reset)) { |
| 99 | /* Assert RESET */ |
| 100 | if (regulator_disable(nop->reset)) |
| 101 | dev_err(phy->dev, "Failed to assert reset\n"); |
| 102 | } |
| 103 | |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 104 | if (!IS_ERR(nop->clk)) |
| 105 | clk_disable(nop->clk); |
Roger Quadros | 58f735f | 2013-03-12 13:24:22 +0200 | [diff] [blame] | 106 | |
| 107 | if (!IS_ERR(nop->vcc)) { |
| 108 | if (regulator_disable(nop->vcc)) |
| 109 | dev_err(phy->dev, "Failed to disable power\n"); |
| 110 | } |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 113 | static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 114 | { |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 115 | if (!otg) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 116 | return -ENODEV; |
| 117 | |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 118 | if (!gadget) { |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 119 | otg->gadget = NULL; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 120 | return -ENODEV; |
| 121 | } |
| 122 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 123 | otg->gadget = gadget; |
| 124 | otg->phy->state = OTG_STATE_B_IDLE; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 128 | static int nop_set_host(struct usb_otg *otg, struct usb_bus *host) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 129 | { |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 130 | if (!otg) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 131 | return -ENODEV; |
| 132 | |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 133 | if (!host) { |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 134 | otg->host = NULL; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 135 | return -ENODEV; |
| 136 | } |
| 137 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 138 | otg->host = host; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 142 | static int nop_usb_xceiv_probe(struct platform_device *pdev) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 143 | { |
Roger Quadros | 0eba387 | 2013-03-12 13:24:25 +0200 | [diff] [blame] | 144 | struct device *dev = &pdev->dev; |
Felipe Balbi | c84d364 | 2012-07-19 13:38:06 +0300 | [diff] [blame] | 145 | struct nop_usb_xceiv_platform_data *pdata = pdev->dev.platform_data; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 146 | struct nop_usb_xceiv *nop; |
Felipe Balbi | c84d364 | 2012-07-19 13:38:06 +0300 | [diff] [blame] | 147 | enum usb_phy_type type = USB_PHY_TYPE_USB2; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 148 | int err; |
Roger Quadros | 0eba387 | 2013-03-12 13:24:25 +0200 | [diff] [blame] | 149 | u32 clk_rate = 0; |
Roger Quadros | b54b5f5 | 2013-03-12 13:24:26 +0200 | [diff] [blame] | 150 | bool needs_vcc = false; |
| 151 | bool needs_reset = false; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 152 | |
Roger Quadros | e4d7dc6 | 2013-03-12 13:24:20 +0200 | [diff] [blame] | 153 | nop = devm_kzalloc(&pdev->dev, sizeof(*nop), GFP_KERNEL); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 154 | if (!nop) |
| 155 | return -ENOMEM; |
| 156 | |
Roger Quadros | e4d7dc6 | 2013-03-12 13:24:20 +0200 | [diff] [blame] | 157 | nop->phy.otg = devm_kzalloc(&pdev->dev, sizeof(*nop->phy.otg), |
| 158 | GFP_KERNEL); |
| 159 | if (!nop->phy.otg) |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 160 | return -ENOMEM; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 161 | |
Roger Quadros | 0eba387 | 2013-03-12 13:24:25 +0200 | [diff] [blame] | 162 | if (dev->of_node) { |
| 163 | struct device_node *node = dev->of_node; |
| 164 | |
| 165 | if (of_property_read_u32(node, "clock-frequency", &clk_rate)) |
| 166 | clk_rate = 0; |
| 167 | |
Roger Quadros | b54b5f5 | 2013-03-12 13:24:26 +0200 | [diff] [blame] | 168 | needs_vcc = of_property_read_bool(node, "vcc-supply"); |
| 169 | needs_reset = of_property_read_bool(node, "reset-supply"); |
| 170 | |
Roger Quadros | 0eba387 | 2013-03-12 13:24:25 +0200 | [diff] [blame] | 171 | } else if (pdata) { |
Felipe Balbi | c84d364 | 2012-07-19 13:38:06 +0300 | [diff] [blame] | 172 | type = pdata->type; |
Roger Quadros | 0eba387 | 2013-03-12 13:24:25 +0200 | [diff] [blame] | 173 | clk_rate = pdata->clk_rate; |
Roger Quadros | b54b5f5 | 2013-03-12 13:24:26 +0200 | [diff] [blame] | 174 | needs_vcc = pdata->needs_vcc; |
| 175 | needs_reset = pdata->needs_reset; |
Roger Quadros | 0eba387 | 2013-03-12 13:24:25 +0200 | [diff] [blame] | 176 | } |
Felipe Balbi | c84d364 | 2012-07-19 13:38:06 +0300 | [diff] [blame] | 177 | |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 178 | nop->clk = devm_clk_get(&pdev->dev, "main_clk"); |
| 179 | if (IS_ERR(nop->clk)) { |
| 180 | dev_dbg(&pdev->dev, "Can't get phy clock: %ld\n", |
| 181 | PTR_ERR(nop->clk)); |
| 182 | } |
| 183 | |
Roger Quadros | 0eba387 | 2013-03-12 13:24:25 +0200 | [diff] [blame] | 184 | if (!IS_ERR(nop->clk) && clk_rate) { |
| 185 | err = clk_set_rate(nop->clk, clk_rate); |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 186 | if (err) { |
| 187 | dev_err(&pdev->dev, "Error setting clock rate\n"); |
| 188 | return err; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (!IS_ERR(nop->clk)) { |
| 193 | err = clk_prepare(nop->clk); |
| 194 | if (err) { |
| 195 | dev_err(&pdev->dev, "Error preparing clock\n"); |
| 196 | return err; |
| 197 | } |
| 198 | } |
| 199 | |
Roger Quadros | 58f735f | 2013-03-12 13:24:22 +0200 | [diff] [blame] | 200 | nop->vcc = devm_regulator_get(&pdev->dev, "vcc"); |
| 201 | if (IS_ERR(nop->vcc)) { |
| 202 | dev_dbg(&pdev->dev, "Error getting vcc regulator: %ld\n", |
| 203 | PTR_ERR(nop->vcc)); |
Roger Quadros | b54b5f5 | 2013-03-12 13:24:26 +0200 | [diff] [blame] | 204 | if (needs_vcc) |
| 205 | return -EPROBE_DEFER; |
Roger Quadros | 58f735f | 2013-03-12 13:24:22 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Roger Quadros | ad63ebfc | 2013-03-12 13:24:23 +0200 | [diff] [blame] | 208 | nop->reset = devm_regulator_get(&pdev->dev, "reset"); |
| 209 | if (IS_ERR(nop->reset)) { |
| 210 | dev_dbg(&pdev->dev, "Error getting reset regulator: %ld\n", |
| 211 | PTR_ERR(nop->reset)); |
Roger Quadros | b54b5f5 | 2013-03-12 13:24:26 +0200 | [diff] [blame] | 212 | if (needs_reset) |
| 213 | return -EPROBE_DEFER; |
Roger Quadros | ad63ebfc | 2013-03-12 13:24:23 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 216 | nop->dev = &pdev->dev; |
| 217 | nop->phy.dev = nop->dev; |
| 218 | nop->phy.label = "nop-xceiv"; |
| 219 | nop->phy.set_suspend = nop_set_suspend; |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 220 | nop->phy.init = nop_init; |
| 221 | nop->phy.shutdown = nop_shutdown; |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 222 | nop->phy.state = OTG_STATE_UNDEFINED; |
Roger Quadros | 90f4232 | 2013-03-12 13:24:24 +0200 | [diff] [blame] | 223 | nop->phy.type = type; |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 224 | |
| 225 | nop->phy.otg->phy = &nop->phy; |
| 226 | nop->phy.otg->set_host = nop_set_host; |
| 227 | nop->phy.otg->set_peripheral = nop_set_peripheral; |
| 228 | |
Roger Quadros | 90f4232 | 2013-03-12 13:24:24 +0200 | [diff] [blame] | 229 | err = usb_add_phy_dev(&nop->phy); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 230 | if (err) { |
| 231 | dev_err(&pdev->dev, "can't register transceiver, err: %d\n", |
| 232 | err); |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 233 | goto err_add; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | platform_set_drvdata(pdev, nop); |
| 237 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 238 | ATOMIC_INIT_NOTIFIER_HEAD(&nop->phy.notifier); |
Ming Lei | 20831ad | 2011-01-04 20:21:32 +0800 | [diff] [blame] | 239 | |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 240 | return 0; |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 241 | |
| 242 | err_add: |
| 243 | if (!IS_ERR(nop->clk)) |
| 244 | clk_unprepare(nop->clk); |
| 245 | return err; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 246 | } |
| 247 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 248 | static int nop_usb_xceiv_remove(struct platform_device *pdev) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 249 | { |
| 250 | struct nop_usb_xceiv *nop = platform_get_drvdata(pdev); |
| 251 | |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame] | 252 | if (!IS_ERR(nop->clk)) |
| 253 | clk_unprepare(nop->clk); |
| 254 | |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 255 | usb_remove_phy(&nop->phy); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 256 | |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Roger Quadros | 0eba387 | 2013-03-12 13:24:25 +0200 | [diff] [blame] | 260 | static const struct of_device_id nop_xceiv_dt_ids[] = { |
| 261 | { .compatible = "usb-nop-xceiv" }, |
| 262 | { } |
| 263 | }; |
| 264 | |
| 265 | MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids); |
| 266 | |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 267 | static struct platform_driver nop_usb_xceiv_driver = { |
| 268 | .probe = nop_usb_xceiv_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 269 | .remove = nop_usb_xceiv_remove, |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 270 | .driver = { |
| 271 | .name = "nop_usb_xceiv", |
| 272 | .owner = THIS_MODULE, |
Sachin Kamat | 5462b0d | 2013-05-21 17:17:22 +0530 | [diff] [blame] | 273 | .of_match_table = nop_xceiv_dt_ids, |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 274 | }, |
| 275 | }; |
| 276 | |
| 277 | static int __init nop_usb_xceiv_init(void) |
| 278 | { |
| 279 | return platform_driver_register(&nop_usb_xceiv_driver); |
| 280 | } |
| 281 | subsys_initcall(nop_usb_xceiv_init); |
| 282 | |
| 283 | static void __exit nop_usb_xceiv_exit(void) |
| 284 | { |
| 285 | platform_driver_unregister(&nop_usb_xceiv_driver); |
| 286 | } |
| 287 | module_exit(nop_usb_xceiv_exit); |
| 288 | |
| 289 | MODULE_ALIAS("platform:nop_usb_xceiv"); |
| 290 | MODULE_AUTHOR("Texas Instruments Inc"); |
| 291 | MODULE_DESCRIPTION("NOP USB Transceiver driver"); |
| 292 | MODULE_LICENSE("GPL"); |