usbmisc_imx.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2012 Freescale Semiconductor, Inc.
 *
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/io.h>
17
#include <linux/delay.h>
18

19
#include "ci_hdrc_imx.h"
20

21 22 23
#define MX25_USB_PHY_CTRL_OFFSET	0x08
#define MX25_BM_EXTERNAL_VBUS_DIVIDER	BIT(23)

24 25 26 27 28 29 30
#define MX53_USB_OTG_PHY_CTRL_0_OFFSET	0x08
#define MX53_USB_UH2_CTRL_OFFSET	0x14
#define MX53_USB_UH3_CTRL_OFFSET	0x18
#define MX53_BM_OVER_CUR_DIS_H1		BIT(5)
#define MX53_BM_OVER_CUR_DIS_OTG	BIT(8)
#define MX53_BM_OVER_CUR_DIS_UHx	BIT(30)

31
#define MX6_BM_OVER_CUR_DIS		BIT(7)
32

33 34 35 36 37 38 39
struct usbmisc_ops {
	/* It's called once when probe a usb device */
	int (*init)(struct imx_usbmisc_data *data);
	/* It's called once after adding a usb device */
	int (*post)(struct imx_usbmisc_data *data);
};

40
struct imx_usbmisc {
41 42 43
	void __iomem *base;
	spinlock_t lock;
	struct clk *clk;
44
	const struct usbmisc_ops *ops;
45 46
};

47
static struct imx_usbmisc *usbmisc;
48

49
static int usbmisc_imx25_post(struct imx_usbmisc_data *data)
50 51 52 53 54
{
	void __iomem *reg;
	unsigned long flags;
	u32 val;

55 56
	if (data->index > 2)
		return -EINVAL;
57 58 59

	reg = usbmisc->base + MX25_USB_PHY_CTRL_OFFSET;

60
	if (data->evdo) {
61 62 63 64 65 66 67 68 69 70
		spin_lock_irqsave(&usbmisc->lock, flags);
		val = readl(reg);
		writel(val | MX25_BM_EXTERNAL_VBUS_DIVIDER, reg);
		spin_unlock_irqrestore(&usbmisc->lock, flags);
		usleep_range(5000, 10000); /* needed to stabilize voltage */
	}

	return 0;
}

71
static int usbmisc_imx53_init(struct imx_usbmisc_data *data)
72 73 74 75 76
{
	void __iomem *reg = NULL;
	unsigned long flags;
	u32 val = 0;

77 78
	if (data->index > 3)
		return -EINVAL;
79

80
	if (data->disable_oc) {
81
		spin_lock_irqsave(&usbmisc->lock, flags);
82
		switch (data->index) {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		case 0:
			reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
			val = readl(reg) | MX53_BM_OVER_CUR_DIS_OTG;
			break;
		case 1:
			reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
			val = readl(reg) | MX53_BM_OVER_CUR_DIS_H1;
			break;
		case 2:
			reg = usbmisc->base + MX53_USB_UH2_CTRL_OFFSET;
			val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
			break;
		case 3:
			reg = usbmisc->base + MX53_USB_UH3_CTRL_OFFSET;
			val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
			break;
		}
		if (reg && val)
			writel(val, reg);
		spin_unlock_irqrestore(&usbmisc->lock, flags);
	}

	return 0;
}

108
static int usbmisc_imx6q_init(struct imx_usbmisc_data *data)
109 110 111 112
{
	unsigned long flags;
	u32 reg;

113 114
	if (data->index > 3)
		return -EINVAL;
115

116
	if (data->disable_oc) {
117
		spin_lock_irqsave(&usbmisc->lock, flags);
118
		reg = readl(usbmisc->base + data->index * 4);
119
		writel(reg | MX6_BM_OVER_CUR_DIS,
120
			usbmisc->base + data->index * 4);
121 122 123 124 125 126
		spin_unlock_irqrestore(&usbmisc->lock, flags);
	}

	return 0;
}

127 128 129 130
static const struct usbmisc_ops imx25_usbmisc_ops = {
	.post = usbmisc_imx25_post,
};

131 132 133 134
static const struct usbmisc_ops imx53_usbmisc_ops = {
	.init = usbmisc_imx53_init,
};

135 136 137 138
static const struct usbmisc_ops imx6q_usbmisc_ops = {
	.init = usbmisc_imx6q_init,
};

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
int imx_usbmisc_init(struct imx_usbmisc_data *data)
{
	if (!usbmisc)
		return -EPROBE_DEFER;
	if (!usbmisc->ops->init)
		return 0;
	return usbmisc->ops->init(data);
}
EXPORT_SYMBOL_GPL(imx_usbmisc_init);

int imx_usbmisc_init_post(struct imx_usbmisc_data *data)
{
	if (!usbmisc)
		return -EPROBE_DEFER;
	if (!usbmisc->ops->post)
		return 0;
	return usbmisc->ops->post(data);
}
EXPORT_SYMBOL_GPL(imx_usbmisc_init_post);

159
static const struct of_device_id usbmisc_imx_dt_ids[] = {
160 161 162 163
	{
		.compatible = "fsl,imx25-usbmisc",
		.data = &imx25_usbmisc_ops,
	},
164 165 166 167
	{
		.compatible = "fsl,imx53-usbmisc",
		.data = &imx53_usbmisc_ops,
	},
168 169 170 171
	{
		.compatible = "fsl,imx6q-usbmisc",
		.data = &imx6q_usbmisc_ops,
	},
172 173
	{ /* sentinel */ }
};
174
MODULE_DEVICE_TABLE(of, usbmisc_imx_dt_ids);
175

176
static int usbmisc_imx_probe(struct platform_device *pdev)
177 178
{
	struct resource	*res;
179
	struct imx_usbmisc *data;
180
	int ret;
181
	struct of_device_id *tmp_dev;
182 183 184 185 186 187 188 189 190 191 192

	if (usbmisc)
		return -EBUSY;

	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	spin_lock_init(&data->lock);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 194 195
	data->base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(data->base))
		return PTR_ERR(data->base);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

	data->clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(data->clk)) {
		dev_err(&pdev->dev,
			"failed to get clock, err=%ld\n", PTR_ERR(data->clk));
		return PTR_ERR(data->clk);
	}

	ret = clk_prepare_enable(data->clk);
	if (ret) {
		dev_err(&pdev->dev,
			"clk_prepare_enable failed, err=%d\n", ret);
		return ret;
	}

211 212 213
	tmp_dev = (struct of_device_id *)
		of_match_device(usbmisc_imx_dt_ids, &pdev->dev);
	data->ops = (const struct usbmisc_ops *)tmp_dev->data;
214
	usbmisc = data;
215 216 217 218

	return 0;
}

219
static int usbmisc_imx_remove(struct platform_device *pdev)
220 221
{
	clk_disable_unprepare(usbmisc->clk);
222
	usbmisc = NULL;
223 224 225
	return 0;
}

226 227 228
static struct platform_driver usbmisc_imx_driver = {
	.probe = usbmisc_imx_probe,
	.remove = usbmisc_imx_remove,
229
	.driver = {
230
		.name = "usbmisc_imx",
231
		.owner = THIS_MODULE,
232
		.of_match_table = usbmisc_imx_dt_ids,
233 234 235
	 },
};

236
module_platform_driver(usbmisc_imx_driver);
237

238
MODULE_ALIAS("platform:usbmisc-imx");
239
MODULE_LICENSE("GPL v2");
240
MODULE_DESCRIPTION("driver for imx usb non-core registers");
241
MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");