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> |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 36 | |
| 37 | struct nop_usb_xceiv { |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 38 | struct usb_phy phy; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 39 | struct device *dev; |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame^] | 40 | struct clk *clk; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 41 | }; |
| 42 | |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 43 | static struct platform_device *pd; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 44 | |
| 45 | void usb_nop_xceiv_register(void) |
| 46 | { |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 47 | if (pd) |
| 48 | return; |
| 49 | pd = platform_device_register_simple("nop_usb_xceiv", -1, NULL, 0); |
| 50 | if (!pd) { |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 51 | printk(KERN_ERR "Unable to register usb nop transceiver\n"); |
| 52 | return; |
| 53 | } |
| 54 | } |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 55 | EXPORT_SYMBOL(usb_nop_xceiv_register); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 56 | |
| 57 | void usb_nop_xceiv_unregister(void) |
| 58 | { |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 59 | platform_device_unregister(pd); |
Ajay Kumar Gupta | dc7520c | 2009-07-03 13:18:45 +0530 | [diff] [blame] | 60 | pd = NULL; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 61 | } |
David Brownell | cc835e3 | 2009-03-31 12:28:31 -0700 | [diff] [blame] | 62 | EXPORT_SYMBOL(usb_nop_xceiv_unregister); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 63 | |
Heikki Krogerus | 8675381 | 2012-02-13 13:24:02 +0200 | [diff] [blame] | 64 | static int nop_set_suspend(struct usb_phy *x, int suspend) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 65 | { |
| 66 | return 0; |
| 67 | } |
| 68 | |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame^] | 69 | static int nop_init(struct usb_phy *phy) |
| 70 | { |
| 71 | struct nop_usb_xceiv *nop = dev_get_drvdata(phy->dev); |
| 72 | |
| 73 | if (!IS_ERR(nop->clk)) |
| 74 | clk_enable(nop->clk); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static void nop_shutdown(struct usb_phy *phy) |
| 80 | { |
| 81 | struct nop_usb_xceiv *nop = dev_get_drvdata(phy->dev); |
| 82 | |
| 83 | if (!IS_ERR(nop->clk)) |
| 84 | clk_disable(nop->clk); |
| 85 | } |
| 86 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 87 | 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] | 88 | { |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 89 | if (!otg) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 90 | return -ENODEV; |
| 91 | |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 92 | if (!gadget) { |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 93 | otg->gadget = NULL; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 94 | return -ENODEV; |
| 95 | } |
| 96 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 97 | otg->gadget = gadget; |
| 98 | otg->phy->state = OTG_STATE_B_IDLE; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 102 | 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] | 103 | { |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 104 | if (!otg) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 105 | return -ENODEV; |
| 106 | |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 107 | if (!host) { |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 108 | otg->host = NULL; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 109 | return -ENODEV; |
| 110 | } |
| 111 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 112 | otg->host = host; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 116 | static int nop_usb_xceiv_probe(struct platform_device *pdev) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 117 | { |
Felipe Balbi | c84d364 | 2012-07-19 13:38:06 +0300 | [diff] [blame] | 118 | struct nop_usb_xceiv_platform_data *pdata = pdev->dev.platform_data; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 119 | struct nop_usb_xceiv *nop; |
Felipe Balbi | c84d364 | 2012-07-19 13:38:06 +0300 | [diff] [blame] | 120 | enum usb_phy_type type = USB_PHY_TYPE_USB2; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 121 | int err; |
| 122 | |
Roger Quadros | e4d7dc6 | 2013-03-12 13:24:20 +0200 | [diff] [blame] | 123 | nop = devm_kzalloc(&pdev->dev, sizeof(*nop), GFP_KERNEL); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 124 | if (!nop) |
| 125 | return -ENOMEM; |
| 126 | |
Roger Quadros | e4d7dc6 | 2013-03-12 13:24:20 +0200 | [diff] [blame] | 127 | nop->phy.otg = devm_kzalloc(&pdev->dev, sizeof(*nop->phy.otg), |
| 128 | GFP_KERNEL); |
| 129 | if (!nop->phy.otg) |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 130 | return -ENOMEM; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 131 | |
Felipe Balbi | c84d364 | 2012-07-19 13:38:06 +0300 | [diff] [blame] | 132 | if (pdata) |
| 133 | type = pdata->type; |
| 134 | |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame^] | 135 | nop->clk = devm_clk_get(&pdev->dev, "main_clk"); |
| 136 | if (IS_ERR(nop->clk)) { |
| 137 | dev_dbg(&pdev->dev, "Can't get phy clock: %ld\n", |
| 138 | PTR_ERR(nop->clk)); |
| 139 | } |
| 140 | |
| 141 | if (!IS_ERR(nop->clk) && pdata && pdata->clk_rate) { |
| 142 | err = clk_set_rate(nop->clk, pdata->clk_rate); |
| 143 | if (err) { |
| 144 | dev_err(&pdev->dev, "Error setting clock rate\n"); |
| 145 | return err; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (!IS_ERR(nop->clk)) { |
| 150 | err = clk_prepare(nop->clk); |
| 151 | if (err) { |
| 152 | dev_err(&pdev->dev, "Error preparing clock\n"); |
| 153 | return err; |
| 154 | } |
| 155 | } |
| 156 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 157 | nop->dev = &pdev->dev; |
| 158 | nop->phy.dev = nop->dev; |
| 159 | nop->phy.label = "nop-xceiv"; |
| 160 | nop->phy.set_suspend = nop_set_suspend; |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame^] | 161 | nop->phy.init = nop_init; |
| 162 | nop->phy.shutdown = nop_shutdown; |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 163 | nop->phy.state = OTG_STATE_UNDEFINED; |
| 164 | |
| 165 | nop->phy.otg->phy = &nop->phy; |
| 166 | nop->phy.otg->set_host = nop_set_host; |
| 167 | nop->phy.otg->set_peripheral = nop_set_peripheral; |
| 168 | |
Felipe Balbi | c84d364 | 2012-07-19 13:38:06 +0300 | [diff] [blame] | 169 | err = usb_add_phy(&nop->phy, type); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 170 | if (err) { |
| 171 | dev_err(&pdev->dev, "can't register transceiver, err: %d\n", |
| 172 | err); |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame^] | 173 | goto err_add; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | platform_set_drvdata(pdev, nop); |
| 177 | |
Heikki Krogerus | 41adf10 | 2012-02-13 13:24:10 +0200 | [diff] [blame] | 178 | ATOMIC_INIT_NOTIFIER_HEAD(&nop->phy.notifier); |
Ming Lei | 20831ad | 2011-01-04 20:21:32 +0800 | [diff] [blame] | 179 | |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 180 | return 0; |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame^] | 181 | |
| 182 | err_add: |
| 183 | if (!IS_ERR(nop->clk)) |
| 184 | clk_unprepare(nop->clk); |
| 185 | return err; |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 186 | } |
| 187 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 188 | static int nop_usb_xceiv_remove(struct platform_device *pdev) |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 189 | { |
| 190 | struct nop_usb_xceiv *nop = platform_get_drvdata(pdev); |
| 191 | |
Roger Quadros | 2319fb8 | 2013-03-12 13:24:21 +0200 | [diff] [blame^] | 192 | if (!IS_ERR(nop->clk)) |
| 193 | clk_unprepare(nop->clk); |
| 194 | |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 195 | usb_remove_phy(&nop->phy); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 196 | |
| 197 | platform_set_drvdata(pdev, NULL); |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static struct platform_driver nop_usb_xceiv_driver = { |
| 203 | .probe = nop_usb_xceiv_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 204 | .remove = nop_usb_xceiv_remove, |
Ajay Kumar Gupta | f6d92a0 | 2009-02-06 17:32:35 +0530 | [diff] [blame] | 205 | .driver = { |
| 206 | .name = "nop_usb_xceiv", |
| 207 | .owner = THIS_MODULE, |
| 208 | }, |
| 209 | }; |
| 210 | |
| 211 | static int __init nop_usb_xceiv_init(void) |
| 212 | { |
| 213 | return platform_driver_register(&nop_usb_xceiv_driver); |
| 214 | } |
| 215 | subsys_initcall(nop_usb_xceiv_init); |
| 216 | |
| 217 | static void __exit nop_usb_xceiv_exit(void) |
| 218 | { |
| 219 | platform_driver_unregister(&nop_usb_xceiv_driver); |
| 220 | } |
| 221 | module_exit(nop_usb_xceiv_exit); |
| 222 | |
| 223 | MODULE_ALIAS("platform:nop_usb_xceiv"); |
| 224 | MODULE_AUTHOR("Texas Instruments Inc"); |
| 225 | MODULE_DESCRIPTION("NOP USB Transceiver driver"); |
| 226 | MODULE_LICENSE("GPL"); |