pwm-imx.c 8.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * simple driver for PWM (Pulse Width Modulator) controller
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
14
#include <linux/slab.h>
15 16
#include <linux/err.h>
#include <linux/clk.h>
17
#include <linux/delay.h>
18 19
#include <linux/io.h>
#include <linux/pwm.h>
20
#include <linux/of.h>
21
#include <linux/of_device.h>
22 23 24

/* i.MX1 and i.MX21 share the same PWM function block: */

25 26 27
#define MX1_PWMC			0x00   /* PWM Control Register */
#define MX1_PWMS			0x04   /* PWM Sample Register */
#define MX1_PWMP			0x08   /* PWM Period Register */
28

29
#define MX1_PWMC_EN			(1 << 4)
30 31 32

/* i.MX27, i.MX31, i.MX35 share the same PWM function block: */

33
#define MX3_PWMCR			0x00    /* PWM Control Register */
34
#define MX3_PWMSR			0x04    /* PWM Status Register */
35 36 37 38 39
#define MX3_PWMSAR			0x0C    /* PWM Sample Register */
#define MX3_PWMPR			0x10    /* PWM Period Register */
#define MX3_PWMCR_PRESCALER(x)		((((x) - 1) & 0xFFF) << 4)
#define MX3_PWMCR_DOZEEN		(1 << 24)
#define MX3_PWMCR_WAITEN		(1 << 23)
40
#define MX3_PWMCR_DBGEN			(1 << 22)
41
#define MX3_PWMCR_POUTC			(1 << 18)
42 43
#define MX3_PWMCR_CLKSRC_IPG_HIGH	(2 << 16)
#define MX3_PWMCR_CLKSRC_IPG		(1 << 16)
44
#define MX3_PWMCR_SWR			(1 << 3)
45
#define MX3_PWMCR_EN			(1 << 0)
46 47 48 49
#define MX3_PWMSR_FIFOAV_4WORDS		0x4
#define MX3_PWMSR_FIFOAV_MASK		0x7

#define MX3_PWM_SWR_LOOP		5
50

51
struct imx_chip {
Philipp Zabel's avatar
Philipp Zabel committed
52
	struct clk	*clk_per;
53 54 55

	void __iomem	*mmio_base;

56
	struct pwm_chip	chip;
57 58
};

59 60
#define to_imx_chip(chip)	container_of(chip, struct imx_chip, chip)

61
static int imx_pwm_config_v1(struct pwm_chip *chip,
62
		struct pwm_device *pwm, int duty_ns, int period_ns)
63
{
64
	struct imx_chip *imx = to_imx_chip(chip);
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	/*
	 * The PWM subsystem allows for exact frequencies. However,
	 * I cannot connect a scope on my device to the PWM line and
	 * thus cannot provide the program the PWM controller
	 * exactly. Instead, I'm relying on the fact that the
	 * Bootloader (u-boot or WinCE+haret) has programmed the PWM
	 * function group already. So I'll just modify the PWM sample
	 * register to follow the ratio of duty_ns vs. period_ns
	 * accordingly.
	 *
	 * This is good enough for programming the brightness of
	 * the LCD backlight.
	 *
	 * The real implementation would divide PERCLK[0] first by
	 * both the prescaler (/1 .. /128) and then by CLKSEL
	 * (/2 .. /16).
	 */
	u32 max = readl(imx->mmio_base + MX1_PWMP);
	u32 p = max * duty_ns / period_ns;
	writel(max - p, imx->mmio_base + MX1_PWMS);

	return 0;
}

90
static int imx_pwm_enable_v1(struct pwm_chip *chip, struct pwm_device *pwm)
91 92 93
{
	struct imx_chip *imx = to_imx_chip(chip);
	u32 val;
94
	int ret;
95

96 97 98
	ret = clk_prepare_enable(imx->clk_per);
	if (ret < 0)
		return ret;
99 100

	val = readl(imx->mmio_base + MX1_PWMC);
101
	val |= MX1_PWMC_EN;
102
	writel(val, imx->mmio_base + MX1_PWMC);
103 104 105 106

	return 0;
}

107
static void imx_pwm_disable_v1(struct pwm_chip *chip, struct pwm_device *pwm)
108 109 110 111
{
	struct imx_chip *imx = to_imx_chip(chip);
	u32 val;

112 113
	val = readl(imx->mmio_base + MX1_PWMC);
	val &= ~MX1_PWMC_EN;
114 115
	writel(val, imx->mmio_base + MX1_PWMC);

116
	clk_disable_unprepare(imx->clk_per);
117 118
}

119
static void imx_pwm_sw_reset(struct pwm_chip *chip)
120 121
{
	struct imx_chip *imx = to_imx_chip(chip);
122 123 124 125 126 127 128 129 130 131 132 133 134 135
	struct device *dev = chip->dev;
	int wait_count = 0;
	u32 cr;

	writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR);
	do {
		usleep_range(200, 1000);
		cr = readl(imx->mmio_base + MX3_PWMCR);
	} while ((cr & MX3_PWMCR_SWR) &&
		 (wait_count++ < MX3_PWM_SWR_LOOP));

	if (cr & MX3_PWMCR_SWR)
		dev_warn(dev, "software reset timeout\n");
}
136

137 138 139 140 141 142 143 144
static void imx_pwm_wait_fifo_slot(struct pwm_chip *chip,
				   struct pwm_device *pwm)
{
	struct imx_chip *imx = to_imx_chip(chip);
	struct device *dev = chip->dev;
	unsigned int period_ms;
	int fifoav;
	u32 sr;
Philipp Zabel's avatar
Philipp Zabel committed
145

146 147 148 149 150 151
	sr = readl(imx->mmio_base + MX3_PWMSR);
	fifoav = sr & MX3_PWMSR_FIFOAV_MASK;
	if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) {
		period_ms = DIV_ROUND_UP(pwm_get_period(pwm),
					 NSEC_PER_MSEC);
		msleep(period_ms);
Philipp Zabel's avatar
Philipp Zabel committed
152

153 154 155 156
		sr = readl(imx->mmio_base + MX3_PWMSR);
		if (fifoav == (sr & MX3_PWMSR_FIFOAV_MASK))
			dev_warn(dev, "there is no free FIFO slot\n");
	}
157 158
}

159 160
static int imx_pwm_apply_v2(struct pwm_chip *chip, struct pwm_device *pwm,
			    struct pwm_state *state)
161
{
162
	unsigned long period_cycles, duty_cycles, prescale;
163
	struct imx_chip *imx = to_imx_chip(chip);
164 165
	struct pwm_state cstate;
	unsigned long long c;
166
	int ret;
167
	u32 cr;
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

	pwm_get_state(pwm, &cstate);

	if (state->enabled) {
		c = clk_get_rate(imx->clk_per);
		c *= state->period;

		do_div(c, 1000000000);
		period_cycles = c;

		prescale = period_cycles / 0x10000 + 1;

		period_cycles /= prescale;
		c = (unsigned long long)period_cycles * state->duty_cycle;
		do_div(c, state->period);
		duty_cycles = c;

		/*
		 * according to imx pwm RM, the real period value should be
		 * PERIOD value in PWMPR plus 2.
		 */
		if (period_cycles > 2)
			period_cycles -= 2;
		else
			period_cycles = 0;

		/*
		 * Wait for a free FIFO slot if the PWM is already enabled, and
		 * flush the FIFO if the PWM was disabled and is about to be
		 * enabled.
		 */
		if (cstate.enabled) {
			imx_pwm_wait_fifo_slot(chip, pwm);
		} else {
			ret = clk_prepare_enable(imx->clk_per);
			if (ret)
				return ret;

			imx_pwm_sw_reset(chip);
		}
208

209 210
		writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
		writel(period_cycles, imx->mmio_base + MX3_PWMPR);
211

212 213 214 215
		cr = MX3_PWMCR_PRESCALER(prescale) |
		     MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
		     MX3_PWMCR_DBGEN | MX3_PWMCR_CLKSRC_IPG_HIGH |
		     MX3_PWMCR_EN;
216

217 218
		if (state->polarity == PWM_POLARITY_INVERSED)
			cr |= MX3_PWMCR_POUTC;
219

220
		writel(cr, imx->mmio_base + MX3_PWMCR);
221 222
	} else if (cstate.enabled) {
		writel(0, imx->mmio_base + MX3_PWMCR);
223

224 225
		clk_disable_unprepare(imx->clk_per);
	}
226

227
	return 0;
228 229
}

230
static const struct pwm_ops imx_pwm_ops_v1 = {
231 232 233
	.enable = imx_pwm_enable_v1,
	.disable = imx_pwm_disable_v1,
	.config = imx_pwm_config_v1,
234 235 236 237
	.owner = THIS_MODULE,
};

static const struct pwm_ops imx_pwm_ops_v2 = {
238
	.apply = imx_pwm_apply_v2,
239 240
	.owner = THIS_MODULE,
};
241

242
struct imx_pwm_data {
243
	bool polarity_supported;
244
	const struct pwm_ops *ops;
245 246 247
};

static struct imx_pwm_data imx_pwm_data_v1 = {
248
	.ops = &imx_pwm_ops_v1,
249 250 251
};

static struct imx_pwm_data imx_pwm_data_v2 = {
252
	.polarity_supported = true,
253
	.ops = &imx_pwm_ops_v2,
254 255 256 257 258 259 260 261 262
};

static const struct of_device_id imx_pwm_dt_ids[] = {
	{ .compatible = "fsl,imx1-pwm", .data = &imx_pwm_data_v1, },
	{ .compatible = "fsl,imx27-pwm", .data = &imx_pwm_data_v2, },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, imx_pwm_dt_ids);

Bill Pemberton's avatar
Bill Pemberton committed
263
static int imx_pwm_probe(struct platform_device *pdev)
264
{
265 266
	const struct of_device_id *of_id =
			of_match_device(imx_pwm_dt_ids, &pdev->dev);
267
	const struct imx_pwm_data *data;
268
	struct imx_chip *imx;
269 270 271
	struct resource *r;
	int ret = 0;

272 273 274
	if (!of_id)
		return -ENODEV;

275 276
	data = of_id->data;

277
	imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
278
	if (imx == NULL)
279 280
		return -ENOMEM;

Philipp Zabel's avatar
Philipp Zabel committed
281 282 283 284 285 286
	imx->clk_per = devm_clk_get(&pdev->dev, "per");
	if (IS_ERR(imx->clk_per)) {
		dev_err(&pdev->dev, "getting per clock failed with %ld\n",
				PTR_ERR(imx->clk_per));
		return PTR_ERR(imx->clk_per);
	}
287

288
	imx->chip.ops = data->ops;
289 290 291
	imx->chip.dev = &pdev->dev;
	imx->chip.base = -1;
	imx->chip.npwm = 1;
292

293 294 295 296 297 298
	if (data->polarity_supported) {
		dev_dbg(&pdev->dev, "PWM supports output inversion\n");
		imx->chip.of_xlate = of_pwm_xlate_with_flags;
		imx->chip.of_pwm_n_cells = 3;
	}

299
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
300 301 302
	imx->mmio_base = devm_ioremap_resource(&pdev->dev, r);
	if (IS_ERR(imx->mmio_base))
		return PTR_ERR(imx->mmio_base);
303

304 305
	ret = pwmchip_add(&imx->chip);
	if (ret < 0)
306
		return ret;
307

308
	platform_set_drvdata(pdev, imx);
309 310 311
	return 0;
}

Bill Pemberton's avatar
Bill Pemberton committed
312
static int imx_pwm_remove(struct platform_device *pdev)
313
{
314
	struct imx_chip *imx;
315

316 317
	imx = platform_get_drvdata(pdev);
	if (imx == NULL)
318 319
		return -ENODEV;

320
	return pwmchip_remove(&imx->chip);
321 322
}

323
static struct platform_driver imx_pwm_driver = {
324
	.driver		= {
325
		.name	= "imx-pwm",
326
		.of_match_table = imx_pwm_dt_ids,
327
	},
328
	.probe		= imx_pwm_probe,
329
	.remove		= imx_pwm_remove,
330 331
};

332
module_platform_driver(imx_pwm_driver);
333 334 335

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");