Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
  3 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  4 *
  5 * This program is free software; you can redistribute it and/or modify it
  6 * under the terms of the GNU General Public License as published by the
  7 * Free Software Foundation; either version 2 of the License, or (at your
  8 * option) any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful, but
 11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 12 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13 * for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software Foundation,
 17 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 18 */
 19
 20#include <linux/kernel.h>
 21#include <linux/module.h>
 22#include <linux/io.h>
 23#include <linux/platform_device.h>
 24#include <linux/clk.h>
 25#include <linux/delay.h>
 26#include <linux/usb/otg.h>
 27#include <linux/usb/ulpi.h>
 28#include <linux/slab.h>
 29#include <linux/usb.h>
 30#include <linux/usb/hcd.h>
 31#include <linux/platform_data/usb-ehci-mxc.h>
 32#include "ehci.h"
 33
 34#define DRIVER_DESC "Freescale On-Chip EHCI Host driver"
 35
 36static const char hcd_name[] = "ehci-mxc";
 37
 38#define ULPI_VIEWPORT_OFFSET	0x170
 39
 40struct ehci_mxc_priv {
 41	struct clk *usbclk, *ahbclk, *phyclk;
 42};
 43
 44static struct hc_driver __read_mostly ehci_mxc_hc_driver;
 45
 46static const struct ehci_driver_overrides ehci_mxc_overrides __initconst = {
 47	.extra_priv_size =	sizeof(struct ehci_mxc_priv),
 48};
 49
 50static int ehci_mxc_drv_probe(struct platform_device *pdev)
 51{
 52	struct mxc_usbh_platform_data *pdata = dev_get_platdata(&pdev->dev);
 
 53	struct usb_hcd *hcd;
 54	struct resource *res;
 55	int irq, ret;
 56	struct ehci_mxc_priv *priv;
 57	struct device *dev = &pdev->dev;
 58	struct ehci_hcd *ehci;
 59
 60	if (!pdata) {
 61		dev_err(dev, "No platform data given, bailing out.\n");
 62		return -EINVAL;
 63	}
 64
 65	irq = platform_get_irq(pdev, 0);
 
 
 66
 67	hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
 68	if (!hcd)
 69		return -ENOMEM;
 70
 71	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 72	hcd->regs = devm_ioremap_resource(&pdev->dev, res);
 73	if (IS_ERR(hcd->regs)) {
 74		ret = PTR_ERR(hcd->regs);
 75		goto err_alloc;
 76	}
 77	hcd->rsrc_start = res->start;
 78	hcd->rsrc_len = resource_size(res);
 79
 80	hcd->has_tt = 1;
 81	ehci = hcd_to_ehci(hcd);
 82	priv = (struct ehci_mxc_priv *) ehci->priv;
 83
 84	/* enable clocks */
 85	priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
 86	if (IS_ERR(priv->usbclk)) {
 87		ret = PTR_ERR(priv->usbclk);
 88		goto err_alloc;
 89	}
 90	clk_prepare_enable(priv->usbclk);
 91
 92	priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
 93	if (IS_ERR(priv->ahbclk)) {
 94		ret = PTR_ERR(priv->ahbclk);
 95		goto err_clk_ahb;
 96	}
 97	clk_prepare_enable(priv->ahbclk);
 98
 99	/* "dr" device has its own clock on i.MX51 */
100	priv->phyclk = devm_clk_get(&pdev->dev, "phy");
101	if (IS_ERR(priv->phyclk))
102		priv->phyclk = NULL;
103	if (priv->phyclk)
104		clk_prepare_enable(priv->phyclk);
105
106
107	/* call platform specific init function */
108	if (pdata->init) {
109		ret = pdata->init(pdev);
110		if (ret) {
111			dev_err(dev, "platform init failed\n");
112			goto err_init;
113		}
114		/* platforms need some time to settle changed IO settings */
115		mdelay(10);
116	}
117
118	/* EHCI registers start at offset 0x100 */
119	ehci->caps = hcd->regs + 0x100;
120	ehci->regs = hcd->regs + 0x100 +
121		HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
122
123	/* set up the PORTSCx register */
124	ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
125
126	/* is this really needed? */
127	msleep(10);
128
129	/* Initialize the transceiver */
130	if (pdata->otg) {
131		pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
132		ret = usb_phy_init(pdata->otg);
133		if (ret) {
134			dev_err(dev, "unable to init transceiver, probably missing\n");
135			ret = -ENODEV;
136			goto err_add;
137		}
138		ret = otg_set_vbus(pdata->otg->otg, 1);
139		if (ret) {
140			dev_err(dev, "unable to enable vbus on transceiver\n");
141			goto err_add;
142		}
143	}
144
145	platform_set_drvdata(pdev, hcd);
146
147	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
148	if (ret)
149		goto err_add;
150
151	device_wakeup_enable(hcd->self.controller);
152	return 0;
153
154err_add:
155	if (pdata && pdata->exit)
156		pdata->exit(pdev);
157err_init:
158	if (priv->phyclk)
159		clk_disable_unprepare(priv->phyclk);
160
161	clk_disable_unprepare(priv->ahbclk);
162err_clk_ahb:
163	clk_disable_unprepare(priv->usbclk);
164err_alloc:
165	usb_put_hcd(hcd);
166	return ret;
167}
168
169static int ehci_mxc_drv_remove(struct platform_device *pdev)
170{
171	struct mxc_usbh_platform_data *pdata = dev_get_platdata(&pdev->dev);
172	struct usb_hcd *hcd = platform_get_drvdata(pdev);
173	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
174	struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv;
175
176	usb_remove_hcd(hcd);
177
178	if (pdata && pdata->exit)
179		pdata->exit(pdev);
180
181	if (pdata && pdata->otg)
182		usb_phy_shutdown(pdata->otg);
183
184	clk_disable_unprepare(priv->usbclk);
185	clk_disable_unprepare(priv->ahbclk);
186
187	if (priv->phyclk)
188		clk_disable_unprepare(priv->phyclk);
189
190	usb_put_hcd(hcd);
191	return 0;
192}
193
194MODULE_ALIAS("platform:mxc-ehci");
195
196static struct platform_driver ehci_mxc_driver = {
197	.probe = ehci_mxc_drv_probe,
198	.remove = ehci_mxc_drv_remove,
199	.shutdown = usb_hcd_platform_shutdown,
200	.driver = {
201		   .name = "mxc-ehci",
202	},
203};
204
205static int __init ehci_mxc_init(void)
206{
207	if (usb_disabled())
208		return -ENODEV;
209
210	pr_info("%s: " DRIVER_DESC "\n", hcd_name);
211
212	ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides);
213	return platform_driver_register(&ehci_mxc_driver);
214}
215module_init(ehci_mxc_init);
216
217static void __exit ehci_mxc_cleanup(void)
218{
219	platform_driver_unregister(&ehci_mxc_driver);
220}
221module_exit(ehci_mxc_cleanup);
222
223MODULE_DESCRIPTION(DRIVER_DESC);
224MODULE_AUTHOR("Sascha Hauer");
225MODULE_LICENSE("GPL");
v5.9
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
  4 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7#include <linux/kernel.h>
  8#include <linux/module.h>
  9#include <linux/io.h>
 10#include <linux/platform_device.h>
 11#include <linux/clk.h>
 12#include <linux/delay.h>
 13#include <linux/usb/otg.h>
 14#include <linux/usb/ulpi.h>
 15#include <linux/slab.h>
 16#include <linux/usb.h>
 17#include <linux/usb/hcd.h>
 18#include <linux/platform_data/usb-ehci-mxc.h>
 19#include "ehci.h"
 20
 21#define DRIVER_DESC "Freescale On-Chip EHCI Host driver"
 22
 23static const char hcd_name[] = "ehci-mxc";
 24
 25#define ULPI_VIEWPORT_OFFSET	0x170
 26
 27struct ehci_mxc_priv {
 28	struct clk *usbclk, *ahbclk, *phyclk;
 29};
 30
 31static struct hc_driver __read_mostly ehci_mxc_hc_driver;
 32
 33static const struct ehci_driver_overrides ehci_mxc_overrides __initconst = {
 34	.extra_priv_size =	sizeof(struct ehci_mxc_priv),
 35};
 36
 37static int ehci_mxc_drv_probe(struct platform_device *pdev)
 38{
 39	struct device *dev = &pdev->dev;
 40	struct mxc_usbh_platform_data *pdata = dev_get_platdata(dev);
 41	struct usb_hcd *hcd;
 42	struct resource *res;
 43	int irq, ret;
 44	struct ehci_mxc_priv *priv;
 
 45	struct ehci_hcd *ehci;
 46
 47	if (!pdata) {
 48		dev_err(dev, "No platform data given, bailing out.\n");
 49		return -EINVAL;
 50	}
 51
 52	irq = platform_get_irq(pdev, 0);
 53	if (irq < 0)
 54		return irq;
 55
 56	hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
 57	if (!hcd)
 58		return -ENOMEM;
 59
 60	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 61	hcd->regs = devm_ioremap_resource(dev, res);
 62	if (IS_ERR(hcd->regs)) {
 63		ret = PTR_ERR(hcd->regs);
 64		goto err_alloc;
 65	}
 66	hcd->rsrc_start = res->start;
 67	hcd->rsrc_len = resource_size(res);
 68
 69	hcd->has_tt = 1;
 70	ehci = hcd_to_ehci(hcd);
 71	priv = (struct ehci_mxc_priv *) ehci->priv;
 72
 73	/* enable clocks */
 74	priv->usbclk = devm_clk_get(dev, "ipg");
 75	if (IS_ERR(priv->usbclk)) {
 76		ret = PTR_ERR(priv->usbclk);
 77		goto err_alloc;
 78	}
 79	clk_prepare_enable(priv->usbclk);
 80
 81	priv->ahbclk = devm_clk_get(dev, "ahb");
 82	if (IS_ERR(priv->ahbclk)) {
 83		ret = PTR_ERR(priv->ahbclk);
 84		goto err_clk_ahb;
 85	}
 86	clk_prepare_enable(priv->ahbclk);
 87
 88	/* "dr" device has its own clock on i.MX51 */
 89	priv->phyclk = devm_clk_get(dev, "phy");
 90	if (IS_ERR(priv->phyclk))
 91		priv->phyclk = NULL;
 92	if (priv->phyclk)
 93		clk_prepare_enable(priv->phyclk);
 
 94
 95	/* call platform specific init function */
 96	if (pdata->init) {
 97		ret = pdata->init(pdev);
 98		if (ret) {
 99			dev_err(dev, "platform init failed\n");
100			goto err_init;
101		}
102		/* platforms need some time to settle changed IO settings */
103		mdelay(10);
104	}
105
106	/* EHCI registers start at offset 0x100 */
107	ehci->caps = hcd->regs + 0x100;
108	ehci->regs = hcd->regs + 0x100 +
109		HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
110
111	/* set up the PORTSCx register */
112	ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
113
114	/* is this really needed? */
115	msleep(10);
116
117	/* Initialize the transceiver */
118	if (pdata->otg) {
119		pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
120		ret = usb_phy_init(pdata->otg);
121		if (ret) {
122			dev_err(dev, "unable to init transceiver, probably missing\n");
123			ret = -ENODEV;
124			goto err_add;
125		}
126		ret = otg_set_vbus(pdata->otg->otg, 1);
127		if (ret) {
128			dev_err(dev, "unable to enable vbus on transceiver\n");
129			goto err_add;
130		}
131	}
132
133	platform_set_drvdata(pdev, hcd);
134
135	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
136	if (ret)
137		goto err_add;
138
139	device_wakeup_enable(hcd->self.controller);
140	return 0;
141
142err_add:
143	if (pdata && pdata->exit)
144		pdata->exit(pdev);
145err_init:
146	if (priv->phyclk)
147		clk_disable_unprepare(priv->phyclk);
148
149	clk_disable_unprepare(priv->ahbclk);
150err_clk_ahb:
151	clk_disable_unprepare(priv->usbclk);
152err_alloc:
153	usb_put_hcd(hcd);
154	return ret;
155}
156
157static int ehci_mxc_drv_remove(struct platform_device *pdev)
158{
159	struct mxc_usbh_platform_data *pdata = dev_get_platdata(&pdev->dev);
160	struct usb_hcd *hcd = platform_get_drvdata(pdev);
161	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
162	struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv;
163
164	usb_remove_hcd(hcd);
165
166	if (pdata && pdata->exit)
167		pdata->exit(pdev);
168
169	if (pdata && pdata->otg)
170		usb_phy_shutdown(pdata->otg);
171
172	clk_disable_unprepare(priv->usbclk);
173	clk_disable_unprepare(priv->ahbclk);
174
175	if (priv->phyclk)
176		clk_disable_unprepare(priv->phyclk);
177
178	usb_put_hcd(hcd);
179	return 0;
180}
181
182MODULE_ALIAS("platform:mxc-ehci");
183
184static struct platform_driver ehci_mxc_driver = {
185	.probe = ehci_mxc_drv_probe,
186	.remove = ehci_mxc_drv_remove,
187	.shutdown = usb_hcd_platform_shutdown,
188	.driver = {
189		   .name = "mxc-ehci",
190	},
191};
192
193static int __init ehci_mxc_init(void)
194{
195	if (usb_disabled())
196		return -ENODEV;
197
198	pr_info("%s: " DRIVER_DESC "\n", hcd_name);
199
200	ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides);
201	return platform_driver_register(&ehci_mxc_driver);
202}
203module_init(ehci_mxc_init);
204
205static void __exit ehci_mxc_cleanup(void)
206{
207	platform_driver_unregister(&ehci_mxc_driver);
208}
209module_exit(ehci_mxc_cleanup);
210
211MODULE_DESCRIPTION(DRIVER_DESC);
212MODULE_AUTHOR("Sascha Hauer");
213MODULE_LICENSE("GPL");