Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * ST OHCI driver
  4 *
  5 * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
  6 *
  7 * Author: Peter Griffin <peter.griffin@linaro.org>
  8 *
  9 * Derived from ohci-platform.c
 10 */
 11
 12#include <linux/clk.h>
 13#include <linux/dma-mapping.h>
 14#include <linux/hrtimer.h>
 15#include <linux/io.h>
 16#include <linux/kernel.h>
 17#include <linux/module.h>
 18#include <linux/err.h>
 19#include <linux/phy/phy.h>
 20#include <linux/platform_device.h>
 21#include <linux/reset.h>
 22#include <linux/usb/ohci_pdriver.h>
 23#include <linux/usb.h>
 24#include <linux/usb/hcd.h>
 25
 26#include "ohci.h"
 27
 28#define USB_MAX_CLKS 3
 29
 30struct st_ohci_platform_priv {
 31	struct clk *clks[USB_MAX_CLKS];
 32	struct clk *clk48;
 33	struct reset_control *rst;
 34	struct reset_control *pwr;
 35	struct phy *phy;
 36};
 37
 38#define DRIVER_DESC "OHCI STMicroelectronics driver"
 39
 40#define hcd_to_ohci_priv(h) \
 41	((struct st_ohci_platform_priv *)hcd_to_ohci(h)->priv)
 42
 
 
 43static int st_ohci_platform_power_on(struct platform_device *dev)
 44{
 45	struct usb_hcd *hcd = platform_get_drvdata(dev);
 46	struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
 47	int clk, ret;
 48
 49	ret = reset_control_deassert(priv->pwr);
 50	if (ret)
 51		return ret;
 52
 53	ret = reset_control_deassert(priv->rst);
 54	if (ret)
 55		goto err_assert_power;
 56
 57	/* some SoCs don't have a dedicated 48Mhz clock, but those that do
 58	   need the rate to be explicitly set */
 59	if (priv->clk48) {
 60		ret = clk_set_rate(priv->clk48, 48000000);
 61		if (ret)
 62			goto err_assert_reset;
 63	}
 64
 65	for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) {
 66		ret = clk_prepare_enable(priv->clks[clk]);
 67		if (ret)
 68			goto err_disable_clks;
 69	}
 70
 71	ret = phy_init(priv->phy);
 72	if (ret)
 73		goto err_disable_clks;
 74
 75	ret = phy_power_on(priv->phy);
 76	if (ret)
 77		goto err_exit_phy;
 78
 79	return 0;
 80
 81err_exit_phy:
 82	phy_exit(priv->phy);
 83err_disable_clks:
 84	while (--clk >= 0)
 85		clk_disable_unprepare(priv->clks[clk]);
 86err_assert_reset:
 87	reset_control_assert(priv->rst);
 88err_assert_power:
 89	reset_control_assert(priv->pwr);
 90
 91	return ret;
 92}
 93
 94static void st_ohci_platform_power_off(struct platform_device *dev)
 95{
 96	struct usb_hcd *hcd = platform_get_drvdata(dev);
 97	struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
 98
 99	int clk;
100
101	reset_control_assert(priv->pwr);
102
103	reset_control_assert(priv->rst);
104
105	phy_power_off(priv->phy);
106
107	phy_exit(priv->phy);
108
109	for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--)
110		if (priv->clks[clk])
111			clk_disable_unprepare(priv->clks[clk]);
112}
113
114static struct hc_driver __read_mostly ohci_platform_hc_driver;
115
116static const struct ohci_driver_overrides platform_overrides __initconst = {
117	.product_desc =		"ST OHCI controller",
118	.extra_priv_size =	sizeof(struct st_ohci_platform_priv),
119};
120
121static struct usb_ohci_pdata ohci_platform_defaults = {
122	.power_on =		st_ohci_platform_power_on,
123	.power_suspend =	st_ohci_platform_power_off,
124	.power_off =		st_ohci_platform_power_off,
125};
126
127static int st_ohci_platform_probe(struct platform_device *dev)
128{
129	struct usb_hcd *hcd;
130	struct resource *res_mem;
131	struct usb_ohci_pdata *pdata = &ohci_platform_defaults;
132	struct st_ohci_platform_priv *priv;
 
133	int err, irq, clk = 0;
134
135	if (usb_disabled())
136		return -ENODEV;
137
138	irq = platform_get_irq(dev, 0);
139	if (irq < 0)
 
140		return irq;
 
141
142	res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
143	if (!res_mem) {
144		dev_err(&dev->dev, "no memory resource provided");
145		return -ENXIO;
146	}
147
148	hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
149			dev_name(&dev->dev));
150	if (!hcd)
151		return -ENOMEM;
152
153	platform_set_drvdata(dev, hcd);
154	dev->dev.platform_data = pdata;
155	priv = hcd_to_ohci_priv(hcd);
 
156
157	priv->phy = devm_phy_get(&dev->dev, "usb");
158	if (IS_ERR(priv->phy)) {
159		err = PTR_ERR(priv->phy);
160		goto err_put_hcd;
161	}
162
163	for (clk = 0; clk < USB_MAX_CLKS; clk++) {
164		priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
165		if (IS_ERR(priv->clks[clk])) {
166			err = PTR_ERR(priv->clks[clk]);
167			if (err == -EPROBE_DEFER)
168				goto err_put_clks;
169			priv->clks[clk] = NULL;
170			break;
171		}
172	}
173
174	/* some SoCs don't have a dedicated 48Mhz clock, but those that
175	   do need the rate to be explicitly set */
176	priv->clk48 = devm_clk_get(&dev->dev, "clk48");
177	if (IS_ERR(priv->clk48)) {
178		dev_info(&dev->dev, "48MHz clk not found\n");
179		priv->clk48 = NULL;
180	}
181
182	priv->pwr =
183		devm_reset_control_get_optional_shared(&dev->dev, "power");
184	if (IS_ERR(priv->pwr)) {
185		err = PTR_ERR(priv->pwr);
186		goto err_put_clks;
187	}
188
189	priv->rst =
190		devm_reset_control_get_optional_shared(&dev->dev, "softreset");
191	if (IS_ERR(priv->rst)) {
192		err = PTR_ERR(priv->rst);
193		goto err_put_clks;
194	}
195
196	if (pdata->power_on) {
197		err = pdata->power_on(dev);
198		if (err < 0)
199			goto err_power;
200	}
201
202	hcd->rsrc_start = res_mem->start;
203	hcd->rsrc_len = resource_size(res_mem);
204
205	hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
206	if (IS_ERR(hcd->regs)) {
207		err = PTR_ERR(hcd->regs);
208		goto err_power;
209	}
210	err = usb_add_hcd(hcd, irq, IRQF_SHARED);
211	if (err)
212		goto err_power;
213
214	device_wakeup_enable(hcd->self.controller);
215
216	platform_set_drvdata(dev, hcd);
217
218	return err;
219
220err_power:
221	if (pdata->power_off)
222		pdata->power_off(dev);
223
224err_put_clks:
225	while (--clk >= 0)
226		clk_put(priv->clks[clk]);
227err_put_hcd:
228	if (pdata == &ohci_platform_defaults)
229		dev->dev.platform_data = NULL;
230
231	usb_put_hcd(hcd);
232
233	return err;
234}
235
236static int st_ohci_platform_remove(struct platform_device *dev)
237{
238	struct usb_hcd *hcd = platform_get_drvdata(dev);
239	struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
240	struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
241	int clk;
242
243	usb_remove_hcd(hcd);
244
245	if (pdata->power_off)
246		pdata->power_off(dev);
247
248
249	for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
250		clk_put(priv->clks[clk]);
251
252	usb_put_hcd(hcd);
253
254	if (pdata == &ohci_platform_defaults)
255		dev->dev.platform_data = NULL;
256
257	return 0;
258}
259
260#ifdef CONFIG_PM_SLEEP
261
262static int st_ohci_suspend(struct device *dev)
263{
264	struct usb_hcd *hcd = dev_get_drvdata(dev);
265	struct usb_ohci_pdata *pdata = dev->platform_data;
266	struct platform_device *pdev = to_platform_device(dev);
267	bool do_wakeup = device_may_wakeup(dev);
268	int ret;
269
270	ret = ohci_suspend(hcd, do_wakeup);
271	if (ret)
272		return ret;
273
274	if (pdata->power_suspend)
275		pdata->power_suspend(pdev);
276
277	return ret;
278}
279
280static int st_ohci_resume(struct device *dev)
281{
282	struct usb_hcd *hcd = dev_get_drvdata(dev);
283	struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
284	struct platform_device *pdev = to_platform_device(dev);
285	int err;
286
287	if (pdata->power_on) {
288		err = pdata->power_on(pdev);
289		if (err < 0)
290			return err;
291	}
292
293	ohci_resume(hcd, false);
294	return 0;
295}
296
297static SIMPLE_DEV_PM_OPS(st_ohci_pm_ops, st_ohci_suspend, st_ohci_resume);
298
299#endif /* CONFIG_PM_SLEEP */
300
301static const struct of_device_id st_ohci_platform_ids[] = {
302	{ .compatible = "st,st-ohci-300x", },
303	{ /* sentinel */ }
304};
305MODULE_DEVICE_TABLE(of, st_ohci_platform_ids);
306
307static struct platform_driver ohci_platform_driver = {
308	.probe		= st_ohci_platform_probe,
309	.remove		= st_ohci_platform_remove,
310	.shutdown	= usb_hcd_platform_shutdown,
311	.driver		= {
312		.name	= "st-ohci",
313#ifdef CONFIG_PM_SLEEP
314		.pm	= &st_ohci_pm_ops,
315#endif
316		.of_match_table = st_ohci_platform_ids,
317	}
318};
319
320static int __init ohci_platform_init(void)
321{
322	if (usb_disabled())
323		return -ENODEV;
 
 
324
325	ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
326	return platform_driver_register(&ohci_platform_driver);
327}
328module_init(ohci_platform_init);
329
330static void __exit ohci_platform_cleanup(void)
331{
332	platform_driver_unregister(&ohci_platform_driver);
333}
334module_exit(ohci_platform_cleanup);
335
336MODULE_DESCRIPTION(DRIVER_DESC);
337MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
338MODULE_LICENSE("GPL");
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * ST OHCI driver
  4 *
  5 * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
  6 *
  7 * Author: Peter Griffin <peter.griffin@linaro.org>
  8 *
  9 * Derived from ohci-platform.c
 10 */
 11
 12#include <linux/clk.h>
 13#include <linux/dma-mapping.h>
 14#include <linux/hrtimer.h>
 15#include <linux/io.h>
 16#include <linux/kernel.h>
 17#include <linux/module.h>
 18#include <linux/err.h>
 19#include <linux/phy/phy.h>
 20#include <linux/platform_device.h>
 21#include <linux/reset.h>
 22#include <linux/usb/ohci_pdriver.h>
 23#include <linux/usb.h>
 24#include <linux/usb/hcd.h>
 25
 26#include "ohci.h"
 27
 28#define USB_MAX_CLKS 3
 29
 30struct st_ohci_platform_priv {
 31	struct clk *clks[USB_MAX_CLKS];
 32	struct clk *clk48;
 33	struct reset_control *rst;
 34	struct reset_control *pwr;
 35	struct phy *phy;
 36};
 37
 38#define DRIVER_DESC "OHCI STMicroelectronics driver"
 39
 40#define hcd_to_ohci_priv(h) \
 41	((struct st_ohci_platform_priv *)hcd_to_ohci(h)->priv)
 42
 43static const char hcd_name[] = "ohci-st";
 44
 45static int st_ohci_platform_power_on(struct platform_device *dev)
 46{
 47	struct usb_hcd *hcd = platform_get_drvdata(dev);
 48	struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
 49	int clk, ret;
 50
 51	ret = reset_control_deassert(priv->pwr);
 52	if (ret)
 53		return ret;
 54
 55	ret = reset_control_deassert(priv->rst);
 56	if (ret)
 57		goto err_assert_power;
 58
 59	/* some SoCs don't have a dedicated 48Mhz clock, but those that do
 60	   need the rate to be explicitly set */
 61	if (priv->clk48) {
 62		ret = clk_set_rate(priv->clk48, 48000000);
 63		if (ret)
 64			goto err_assert_reset;
 65	}
 66
 67	for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) {
 68		ret = clk_prepare_enable(priv->clks[clk]);
 69		if (ret)
 70			goto err_disable_clks;
 71	}
 72
 73	ret = phy_init(priv->phy);
 74	if (ret)
 75		goto err_disable_clks;
 76
 77	ret = phy_power_on(priv->phy);
 78	if (ret)
 79		goto err_exit_phy;
 80
 81	return 0;
 82
 83err_exit_phy:
 84	phy_exit(priv->phy);
 85err_disable_clks:
 86	while (--clk >= 0)
 87		clk_disable_unprepare(priv->clks[clk]);
 88err_assert_reset:
 89	reset_control_assert(priv->rst);
 90err_assert_power:
 91	reset_control_assert(priv->pwr);
 92
 93	return ret;
 94}
 95
 96static void st_ohci_platform_power_off(struct platform_device *dev)
 97{
 98	struct usb_hcd *hcd = platform_get_drvdata(dev);
 99	struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
100
101	int clk;
102
103	reset_control_assert(priv->pwr);
104
105	reset_control_assert(priv->rst);
106
107	phy_power_off(priv->phy);
108
109	phy_exit(priv->phy);
110
111	for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--)
112		if (priv->clks[clk])
113			clk_disable_unprepare(priv->clks[clk]);
114}
115
116static struct hc_driver __read_mostly ohci_platform_hc_driver;
117
118static const struct ohci_driver_overrides platform_overrides __initconst = {
119	.product_desc =		"ST OHCI controller",
120	.extra_priv_size =	sizeof(struct st_ohci_platform_priv),
121};
122
123static struct usb_ohci_pdata ohci_platform_defaults = {
124	.power_on =		st_ohci_platform_power_on,
125	.power_suspend =	st_ohci_platform_power_off,
126	.power_off =		st_ohci_platform_power_off,
127};
128
129static int st_ohci_platform_probe(struct platform_device *dev)
130{
131	struct usb_hcd *hcd;
132	struct resource *res_mem;
133	struct usb_ohci_pdata *pdata = &ohci_platform_defaults;
134	struct st_ohci_platform_priv *priv;
135	struct ohci_hcd *ohci;
136	int err, irq, clk = 0;
137
138	if (usb_disabled())
139		return -ENODEV;
140
141	irq = platform_get_irq(dev, 0);
142	if (irq < 0) {
143		dev_err(&dev->dev, "no irq provided");
144		return irq;
145	}
146
147	res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
148	if (!res_mem) {
149		dev_err(&dev->dev, "no memory resource provided");
150		return -ENXIO;
151	}
152
153	hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
154			dev_name(&dev->dev));
155	if (!hcd)
156		return -ENOMEM;
157
158	platform_set_drvdata(dev, hcd);
159	dev->dev.platform_data = pdata;
160	priv = hcd_to_ohci_priv(hcd);
161	ohci = hcd_to_ohci(hcd);
162
163	priv->phy = devm_phy_get(&dev->dev, "usb");
164	if (IS_ERR(priv->phy)) {
165		err = PTR_ERR(priv->phy);
166		goto err_put_hcd;
167	}
168
169	for (clk = 0; clk < USB_MAX_CLKS; clk++) {
170		priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
171		if (IS_ERR(priv->clks[clk])) {
172			err = PTR_ERR(priv->clks[clk]);
173			if (err == -EPROBE_DEFER)
174				goto err_put_clks;
175			priv->clks[clk] = NULL;
176			break;
177		}
178	}
179
180	/* some SoCs don't have a dedicated 48Mhz clock, but those that
181	   do need the rate to be explicitly set */
182	priv->clk48 = devm_clk_get(&dev->dev, "clk48");
183	if (IS_ERR(priv->clk48)) {
184		dev_info(&dev->dev, "48MHz clk not found\n");
185		priv->clk48 = NULL;
186	}
187
188	priv->pwr =
189		devm_reset_control_get_optional_shared(&dev->dev, "power");
190	if (IS_ERR(priv->pwr)) {
191		err = PTR_ERR(priv->pwr);
192		goto err_put_clks;
193	}
194
195	priv->rst =
196		devm_reset_control_get_optional_shared(&dev->dev, "softreset");
197	if (IS_ERR(priv->rst)) {
198		err = PTR_ERR(priv->rst);
199		goto err_put_clks;
200	}
201
202	if (pdata->power_on) {
203		err = pdata->power_on(dev);
204		if (err < 0)
205			goto err_power;
206	}
207
208	hcd->rsrc_start = res_mem->start;
209	hcd->rsrc_len = resource_size(res_mem);
210
211	hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
212	if (IS_ERR(hcd->regs)) {
213		err = PTR_ERR(hcd->regs);
214		goto err_power;
215	}
216	err = usb_add_hcd(hcd, irq, IRQF_SHARED);
217	if (err)
218		goto err_power;
219
220	device_wakeup_enable(hcd->self.controller);
221
222	platform_set_drvdata(dev, hcd);
223
224	return err;
225
226err_power:
227	if (pdata->power_off)
228		pdata->power_off(dev);
229
230err_put_clks:
231	while (--clk >= 0)
232		clk_put(priv->clks[clk]);
233err_put_hcd:
234	if (pdata == &ohci_platform_defaults)
235		dev->dev.platform_data = NULL;
236
237	usb_put_hcd(hcd);
238
239	return err;
240}
241
242static int st_ohci_platform_remove(struct platform_device *dev)
243{
244	struct usb_hcd *hcd = platform_get_drvdata(dev);
245	struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
246	struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
247	int clk;
248
249	usb_remove_hcd(hcd);
250
251	if (pdata->power_off)
252		pdata->power_off(dev);
253
254
255	for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
256		clk_put(priv->clks[clk]);
257
258	usb_put_hcd(hcd);
259
260	if (pdata == &ohci_platform_defaults)
261		dev->dev.platform_data = NULL;
262
263	return 0;
264}
265
266#ifdef CONFIG_PM_SLEEP
267
268static int st_ohci_suspend(struct device *dev)
269{
270	struct usb_hcd *hcd = dev_get_drvdata(dev);
271	struct usb_ohci_pdata *pdata = dev->platform_data;
272	struct platform_device *pdev = to_platform_device(dev);
273	bool do_wakeup = device_may_wakeup(dev);
274	int ret;
275
276	ret = ohci_suspend(hcd, do_wakeup);
277	if (ret)
278		return ret;
279
280	if (pdata->power_suspend)
281		pdata->power_suspend(pdev);
282
283	return ret;
284}
285
286static int st_ohci_resume(struct device *dev)
287{
288	struct usb_hcd *hcd = dev_get_drvdata(dev);
289	struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
290	struct platform_device *pdev = to_platform_device(dev);
291	int err;
292
293	if (pdata->power_on) {
294		err = pdata->power_on(pdev);
295		if (err < 0)
296			return err;
297	}
298
299	ohci_resume(hcd, false);
300	return 0;
301}
302
303static SIMPLE_DEV_PM_OPS(st_ohci_pm_ops, st_ohci_suspend, st_ohci_resume);
304
305#endif /* CONFIG_PM_SLEEP */
306
307static const struct of_device_id st_ohci_platform_ids[] = {
308	{ .compatible = "st,st-ohci-300x", },
309	{ /* sentinel */ }
310};
311MODULE_DEVICE_TABLE(of, st_ohci_platform_ids);
312
313static struct platform_driver ohci_platform_driver = {
314	.probe		= st_ohci_platform_probe,
315	.remove		= st_ohci_platform_remove,
316	.shutdown	= usb_hcd_platform_shutdown,
317	.driver		= {
318		.name	= "st-ohci",
319#ifdef CONFIG_PM_SLEEP
320		.pm	= &st_ohci_pm_ops,
321#endif
322		.of_match_table = st_ohci_platform_ids,
323	}
324};
325
326static int __init ohci_platform_init(void)
327{
328	if (usb_disabled())
329		return -ENODEV;
330
331	pr_info("%s: " DRIVER_DESC "\n", hcd_name);
332
333	ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
334	return platform_driver_register(&ohci_platform_driver);
335}
336module_init(ohci_platform_init);
337
338static void __exit ohci_platform_cleanup(void)
339{
340	platform_driver_unregister(&ohci_platform_driver);
341}
342module_exit(ohci_platform_cleanup);
343
344MODULE_DESCRIPTION(DRIVER_DESC);
345MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
346MODULE_LICENSE("GPL");