blob: 3c424469c7a5a2c854e537b6574423794ab733fe [file] [log] [blame]
Richard Zhaod142d6b2012-09-12 14:58:05 +03001/*
2 * Copyright 2012 Freescale Semiconductor, Inc.
3 *
4 * The code contained herein is licensed under the GNU General Public
5 * License. You may obtain a copy of the GNU General Public License
6 * Version 2 or later at the following locations:
7 *
8 * http://www.opensource.org/licenses/gpl-license.html
9 * http://www.gnu.org/copyleft/gpl.html
10 */
11
12#include <linux/module.h>
13#include <linux/of_platform.h>
14#include <linux/clk.h>
15#include <linux/err.h>
16#include <linux/io.h>
17
18#include "ci13xxx_imx.h"
19
20#define USB_DEV_MAX 4
21
22#define BM_OVER_CUR_DIS BIT(7)
23
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +020024struct imx_usbmisc {
Richard Zhaod142d6b2012-09-12 14:58:05 +030025 void __iomem *base;
26 spinlock_t lock;
27 struct clk *clk;
28 struct usbmisc_usb_device usbdev[USB_DEV_MAX];
29};
30
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +020031static struct imx_usbmisc *usbmisc;
Richard Zhaod142d6b2012-09-12 14:58:05 +030032
33static struct usbmisc_usb_device *get_usbdev(struct device *dev)
34{
35 int i, ret;
36
37 for (i = 0; i < USB_DEV_MAX; i++) {
38 if (usbmisc->usbdev[i].dev == dev)
39 return &usbmisc->usbdev[i];
40 else if (!usbmisc->usbdev[i].dev)
41 break;
42 }
43
44 if (i >= USB_DEV_MAX)
45 return ERR_PTR(-EBUSY);
46
47 ret = usbmisc_get_init_data(dev, &usbmisc->usbdev[i]);
48 if (ret)
49 return ERR_PTR(ret);
50
51 return &usbmisc->usbdev[i];
52}
53
54static int usbmisc_imx6q_init(struct device *dev)
55{
56
57 struct usbmisc_usb_device *usbdev;
58 unsigned long flags;
59 u32 reg;
60
61 usbdev = get_usbdev(dev);
62 if (IS_ERR(usbdev))
63 return PTR_ERR(usbdev);
64
65 if (usbdev->disable_oc) {
66 spin_lock_irqsave(&usbmisc->lock, flags);
67 reg = readl(usbmisc->base + usbdev->index * 4);
68 writel(reg | BM_OVER_CUR_DIS,
69 usbmisc->base + usbdev->index * 4);
70 spin_unlock_irqrestore(&usbmisc->lock, flags);
71 }
72
73 return 0;
74}
75
76static const struct usbmisc_ops imx6q_usbmisc_ops = {
77 .init = usbmisc_imx6q_init,
78};
79
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +020080static const struct of_device_id usbmisc_imx_dt_ids[] = {
Richard Zhaod142d6b2012-09-12 14:58:05 +030081 { .compatible = "fsl,imx6q-usbmisc"},
82 { /* sentinel */ }
83};
84
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +020085static int usbmisc_imx_probe(struct platform_device *pdev)
Richard Zhaod142d6b2012-09-12 14:58:05 +030086{
87 struct resource *res;
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +020088 struct imx_usbmisc *data;
Richard Zhaod142d6b2012-09-12 14:58:05 +030089 int ret;
90
91 if (usbmisc)
92 return -EBUSY;
93
94 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
95 if (!data)
96 return -ENOMEM;
97
98 spin_lock_init(&data->lock);
99
100 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100101 data->base = devm_ioremap_resource(&pdev->dev, res);
102 if (IS_ERR(data->base))
103 return PTR_ERR(data->base);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300104
105 data->clk = devm_clk_get(&pdev->dev, NULL);
106 if (IS_ERR(data->clk)) {
107 dev_err(&pdev->dev,
108 "failed to get clock, err=%ld\n", PTR_ERR(data->clk));
109 return PTR_ERR(data->clk);
110 }
111
112 ret = clk_prepare_enable(data->clk);
113 if (ret) {
114 dev_err(&pdev->dev,
115 "clk_prepare_enable failed, err=%d\n", ret);
116 return ret;
117 }
118
119 ret = usbmisc_set_ops(&imx6q_usbmisc_ops);
120 if (ret) {
121 clk_disable_unprepare(data->clk);
122 return ret;
123 }
124
125 usbmisc = data;
126
127 return 0;
128}
129
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200130static int usbmisc_imx_remove(struct platform_device *pdev)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300131{
132 usbmisc_unset_ops(&imx6q_usbmisc_ops);
133 clk_disable_unprepare(usbmisc->clk);
134 return 0;
135}
136
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200137static struct platform_driver usbmisc_imx_driver = {
138 .probe = usbmisc_imx_probe,
139 .remove = usbmisc_imx_remove,
Richard Zhaod142d6b2012-09-12 14:58:05 +0300140 .driver = {
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200141 .name = "usbmisc_imx",
Richard Zhaod142d6b2012-09-12 14:58:05 +0300142 .owner = THIS_MODULE,
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200143 .of_match_table = usbmisc_imx_dt_ids,
Richard Zhaod142d6b2012-09-12 14:58:05 +0300144 },
145};
146
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200147int usbmisc_imx_drv_init(void)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300148{
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200149 return platform_driver_register(&usbmisc_imx_driver);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300150}
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200151subsys_initcall(usbmisc_imx_drv_init);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300152
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200153void usbmisc_imx_drv_exit(void)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300154{
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200155 platform_driver_unregister(&usbmisc_imx_driver);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300156}
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200157module_exit(usbmisc_imx_drv_exit);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300158
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200159MODULE_ALIAS("platform:usbmisc-imx");
Richard Zhaod142d6b2012-09-12 14:58:05 +0300160MODULE_LICENSE("GPL v2");
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200161MODULE_DESCRIPTION("driver for imx usb non-core registers");
Richard Zhaod142d6b2012-09-12 14:58:05 +0300162MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");