1 /*
2  * simple driver for PWM (Pulse Width Modulator) controller
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/io.h>
19 #include <linux/pwm.h>
20 #include <linux/of.h>
21 #include <linux/of_device.h>
22 
23 /* i.MX1 and i.MX21 share the same PWM function block: */
24 
25 #define MX1_PWMC			0x00   /* PWM Control Register */
26 #define MX1_PWMS			0x04   /* PWM Sample Register */
27 #define MX1_PWMP			0x08   /* PWM Period Register */
28 
29 #define MX1_PWMC_EN			(1 << 4)
30 
31 /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
32 
33 #define MX3_PWMCR			0x00    /* PWM Control Register */
34 #define MX3_PWMSR			0x04    /* PWM Status Register */
35 #define MX3_PWMSAR			0x0C    /* PWM Sample Register */
36 #define MX3_PWMPR			0x10    /* PWM Period Register */
37 #define MX3_PWMCR_PRESCALER(x)		((((x) - 1) & 0xFFF) << 4)
38 #define MX3_PWMCR_DOZEEN		(1 << 24)
39 #define MX3_PWMCR_WAITEN		(1 << 23)
40 #define MX3_PWMCR_DBGEN			(1 << 22)
41 #define MX3_PWMCR_CLKSRC_IPG_HIGH	(2 << 16)
42 #define MX3_PWMCR_CLKSRC_IPG		(1 << 16)
43 #define MX3_PWMCR_SWR			(1 << 3)
44 #define MX3_PWMCR_EN			(1 << 0)
45 #define MX3_PWMSR_FIFOAV_4WORDS		0x4
46 #define MX3_PWMSR_FIFOAV_MASK		0x7
47 
48 #define MX3_PWM_SWR_LOOP		5
49 
50 struct imx_chip {
51 	struct clk	*clk_per;
52 	struct clk	*clk_ipg;
53 
54 	void __iomem	*mmio_base;
55 
56 	struct pwm_chip	chip;
57 
58 	int (*config)(struct pwm_chip *chip,
59 		struct pwm_device *pwm, int duty_ns, int period_ns);
60 	void (*set_enable)(struct pwm_chip *chip, bool enable);
61 };
62 
63 #define to_imx_chip(chip)	container_of(chip, struct imx_chip, chip)
64 
imx_pwm_config_v1(struct pwm_chip * chip,struct pwm_device * pwm,int duty_ns,int period_ns)65 static int imx_pwm_config_v1(struct pwm_chip *chip,
66 		struct pwm_device *pwm, int duty_ns, int period_ns)
67 {
68 	struct imx_chip *imx = to_imx_chip(chip);
69 
70 	/*
71 	 * The PWM subsystem allows for exact frequencies. However,
72 	 * I cannot connect a scope on my device to the PWM line and
73 	 * thus cannot provide the program the PWM controller
74 	 * exactly. Instead, I'm relying on the fact that the
75 	 * Bootloader (u-boot or WinCE+haret) has programmed the PWM
76 	 * function group already. So I'll just modify the PWM sample
77 	 * register to follow the ratio of duty_ns vs. period_ns
78 	 * accordingly.
79 	 *
80 	 * This is good enough for programming the brightness of
81 	 * the LCD backlight.
82 	 *
83 	 * The real implementation would divide PERCLK[0] first by
84 	 * both the prescaler (/1 .. /128) and then by CLKSEL
85 	 * (/2 .. /16).
86 	 */
87 	u32 max = readl(imx->mmio_base + MX1_PWMP);
88 	u32 p = max * duty_ns / period_ns;
89 	writel(max - p, imx->mmio_base + MX1_PWMS);
90 
91 	return 0;
92 }
93 
imx_pwm_set_enable_v1(struct pwm_chip * chip,bool enable)94 static void imx_pwm_set_enable_v1(struct pwm_chip *chip, bool enable)
95 {
96 	struct imx_chip *imx = to_imx_chip(chip);
97 	u32 val;
98 
99 	val = readl(imx->mmio_base + MX1_PWMC);
100 
101 	if (enable)
102 		val |= MX1_PWMC_EN;
103 	else
104 		val &= ~MX1_PWMC_EN;
105 
106 	writel(val, imx->mmio_base + MX1_PWMC);
107 }
108 
imx_pwm_config_v2(struct pwm_chip * chip,struct pwm_device * pwm,int duty_ns,int period_ns)109 static int imx_pwm_config_v2(struct pwm_chip *chip,
110 		struct pwm_device *pwm, int duty_ns, int period_ns)
111 {
112 	struct imx_chip *imx = to_imx_chip(chip);
113 	struct device *dev = chip->dev;
114 	unsigned long long c;
115 	unsigned long period_cycles, duty_cycles, prescale;
116 	unsigned int period_ms;
117 	bool enable = pwm_is_enabled(pwm);
118 	int wait_count = 0, fifoav;
119 	u32 cr, sr;
120 
121 	/*
122 	 * i.MX PWMv2 has a 4-word sample FIFO.
123 	 * In order to avoid FIFO overflow issue, we do software reset
124 	 * to clear all sample FIFO if the controller is disabled or
125 	 * wait for a full PWM cycle to get a relinquished FIFO slot
126 	 * when the controller is enabled and the FIFO is fully loaded.
127 	 */
128 	if (enable) {
129 		sr = readl(imx->mmio_base + MX3_PWMSR);
130 		fifoav = sr & MX3_PWMSR_FIFOAV_MASK;
131 		if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) {
132 			period_ms = DIV_ROUND_UP(pwm_get_period(pwm),
133 						 NSEC_PER_MSEC);
134 			msleep(period_ms);
135 
136 			sr = readl(imx->mmio_base + MX3_PWMSR);
137 			if (fifoav == (sr & MX3_PWMSR_FIFOAV_MASK))
138 				dev_warn(dev, "there is no free FIFO slot\n");
139 		}
140 	} else {
141 		writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR);
142 		do {
143 			usleep_range(200, 1000);
144 			cr = readl(imx->mmio_base + MX3_PWMCR);
145 		} while ((cr & MX3_PWMCR_SWR) &&
146 			 (wait_count++ < MX3_PWM_SWR_LOOP));
147 
148 		if (cr & MX3_PWMCR_SWR)
149 			dev_warn(dev, "software reset timeout\n");
150 	}
151 
152 	c = clk_get_rate(imx->clk_per);
153 	c = c * period_ns;
154 	do_div(c, 1000000000);
155 	period_cycles = c;
156 
157 	prescale = period_cycles / 0x10000 + 1;
158 
159 	period_cycles /= prescale;
160 	c = (unsigned long long)period_cycles * duty_ns;
161 	do_div(c, period_ns);
162 	duty_cycles = c;
163 
164 	/*
165 	 * according to imx pwm RM, the real period value should be
166 	 * PERIOD value in PWMPR plus 2.
167 	 */
168 	if (period_cycles > 2)
169 		period_cycles -= 2;
170 	else
171 		period_cycles = 0;
172 
173 	writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
174 	writel(period_cycles, imx->mmio_base + MX3_PWMPR);
175 
176 	cr = MX3_PWMCR_PRESCALER(prescale) |
177 		MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
178 		MX3_PWMCR_DBGEN | MX3_PWMCR_CLKSRC_IPG_HIGH;
179 
180 	if (enable)
181 		cr |= MX3_PWMCR_EN;
182 
183 	writel(cr, imx->mmio_base + MX3_PWMCR);
184 
185 	return 0;
186 }
187 
imx_pwm_set_enable_v2(struct pwm_chip * chip,bool enable)188 static void imx_pwm_set_enable_v2(struct pwm_chip *chip, bool enable)
189 {
190 	struct imx_chip *imx = to_imx_chip(chip);
191 	u32 val;
192 
193 	val = readl(imx->mmio_base + MX3_PWMCR);
194 
195 	if (enable)
196 		val |= MX3_PWMCR_EN;
197 	else
198 		val &= ~MX3_PWMCR_EN;
199 
200 	writel(val, imx->mmio_base + MX3_PWMCR);
201 }
202 
imx_pwm_config(struct pwm_chip * chip,struct pwm_device * pwm,int duty_ns,int period_ns)203 static int imx_pwm_config(struct pwm_chip *chip,
204 		struct pwm_device *pwm, int duty_ns, int period_ns)
205 {
206 	struct imx_chip *imx = to_imx_chip(chip);
207 	int ret;
208 
209 	ret = clk_prepare_enable(imx->clk_ipg);
210 	if (ret)
211 		return ret;
212 
213 	ret = imx->config(chip, pwm, duty_ns, period_ns);
214 
215 	clk_disable_unprepare(imx->clk_ipg);
216 
217 	return ret;
218 }
219 
imx_pwm_enable(struct pwm_chip * chip,struct pwm_device * pwm)220 static int imx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
221 {
222 	struct imx_chip *imx = to_imx_chip(chip);
223 	int ret;
224 
225 	ret = clk_prepare_enable(imx->clk_per);
226 	if (ret)
227 		return ret;
228 
229 	imx->set_enable(chip, true);
230 
231 	return 0;
232 }
233 
imx_pwm_disable(struct pwm_chip * chip,struct pwm_device * pwm)234 static void imx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
235 {
236 	struct imx_chip *imx = to_imx_chip(chip);
237 
238 	imx->set_enable(chip, false);
239 
240 	clk_disable_unprepare(imx->clk_per);
241 }
242 
243 static struct pwm_ops imx_pwm_ops = {
244 	.enable = imx_pwm_enable,
245 	.disable = imx_pwm_disable,
246 	.config = imx_pwm_config,
247 	.owner = THIS_MODULE,
248 };
249 
250 struct imx_pwm_data {
251 	int (*config)(struct pwm_chip *chip,
252 		struct pwm_device *pwm, int duty_ns, int period_ns);
253 	void (*set_enable)(struct pwm_chip *chip, bool enable);
254 };
255 
256 static struct imx_pwm_data imx_pwm_data_v1 = {
257 	.config = imx_pwm_config_v1,
258 	.set_enable = imx_pwm_set_enable_v1,
259 };
260 
261 static struct imx_pwm_data imx_pwm_data_v2 = {
262 	.config = imx_pwm_config_v2,
263 	.set_enable = imx_pwm_set_enable_v2,
264 };
265 
266 static const struct of_device_id imx_pwm_dt_ids[] = {
267 	{ .compatible = "fsl,imx1-pwm", .data = &imx_pwm_data_v1, },
268 	{ .compatible = "fsl,imx27-pwm", .data = &imx_pwm_data_v2, },
269 	{ /* sentinel */ }
270 };
271 MODULE_DEVICE_TABLE(of, imx_pwm_dt_ids);
272 
imx_pwm_probe(struct platform_device * pdev)273 static int imx_pwm_probe(struct platform_device *pdev)
274 {
275 	const struct of_device_id *of_id =
276 			of_match_device(imx_pwm_dt_ids, &pdev->dev);
277 	const struct imx_pwm_data *data;
278 	struct imx_chip *imx;
279 	struct resource *r;
280 	int ret = 0;
281 
282 	if (!of_id)
283 		return -ENODEV;
284 
285 	imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
286 	if (imx == NULL)
287 		return -ENOMEM;
288 
289 	imx->clk_per = devm_clk_get(&pdev->dev, "per");
290 	if (IS_ERR(imx->clk_per)) {
291 		dev_err(&pdev->dev, "getting per clock failed with %ld\n",
292 				PTR_ERR(imx->clk_per));
293 		return PTR_ERR(imx->clk_per);
294 	}
295 
296 	imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
297 	if (IS_ERR(imx->clk_ipg)) {
298 		dev_err(&pdev->dev, "getting ipg clock failed with %ld\n",
299 				PTR_ERR(imx->clk_ipg));
300 		return PTR_ERR(imx->clk_ipg);
301 	}
302 
303 	imx->chip.ops = &imx_pwm_ops;
304 	imx->chip.dev = &pdev->dev;
305 	imx->chip.base = -1;
306 	imx->chip.npwm = 1;
307 	imx->chip.can_sleep = true;
308 
309 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
310 	imx->mmio_base = devm_ioremap_resource(&pdev->dev, r);
311 	if (IS_ERR(imx->mmio_base))
312 		return PTR_ERR(imx->mmio_base);
313 
314 	data = of_id->data;
315 	imx->config = data->config;
316 	imx->set_enable = data->set_enable;
317 
318 	ret = pwmchip_add(&imx->chip);
319 	if (ret < 0)
320 		return ret;
321 
322 	platform_set_drvdata(pdev, imx);
323 	return 0;
324 }
325 
imx_pwm_remove(struct platform_device * pdev)326 static int imx_pwm_remove(struct platform_device *pdev)
327 {
328 	struct imx_chip *imx;
329 
330 	imx = platform_get_drvdata(pdev);
331 	if (imx == NULL)
332 		return -ENODEV;
333 
334 	return pwmchip_remove(&imx->chip);
335 }
336 
337 static struct platform_driver imx_pwm_driver = {
338 	.driver		= {
339 		.name	= "imx-pwm",
340 		.of_match_table = imx_pwm_dt_ids,
341 	},
342 	.probe		= imx_pwm_probe,
343 	.remove		= imx_pwm_remove,
344 };
345 
346 module_platform_driver(imx_pwm_driver);
347 
348 MODULE_LICENSE("GPL v2");
349 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
350