blob: 6cd3b72fbbc1062a89e898c1730363d4067da534 [file] [log] [blame]
Fabio Estevama99290c2018-07-06 19:47:17 -03001// SPDX-License-Identifier: GPL-2.0
Sascha Hauer29693242012-03-15 10:04:35 +01002/*
3 * simple driver for PWM (Pulse Width Modulator) controller
4 *
Sascha Hauer29693242012-03-15 10:04:35 +01005 * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
6 */
7
Michal Vokáč9f617ad2018-10-01 16:19:47 +02008#include <linux/bitfield.h>
9#include <linux/bitops.h>
Sascha Hauer29693242012-03-15 10:04:35 +010010#include <linux/clk.h>
Liu Ying137fd452014-05-28 18:50:13 +080011#include <linux/delay.h>
Michal Vokáče3adc7e2018-10-01 16:19:46 +020012#include <linux/err.h>
Sascha Hauer29693242012-03-15 10:04:35 +010013#include <linux/io.h>
Michal Vokáče3adc7e2018-10-01 16:19:46 +020014#include <linux/kernel.h>
15#include <linux/module.h>
Sachin Kamat2a8876c2013-09-27 16:53:23 +053016#include <linux/of.h>
Philipp Zabel479e2e32012-06-25 16:16:25 +020017#include <linux/of_device.h>
Michal Vokáče3adc7e2018-10-01 16:19:46 +020018#include <linux/platform_device.h>
19#include <linux/pwm.h>
20#include <linux/slab.h>
Sascha Hauer29693242012-03-15 10:04:35 +010021
Sascha Hauer29693242012-03-15 10:04:35 +010022/* i.MX1 and i.MX21 share the same PWM function block: */
23
Liu Ying40f260c2014-05-28 18:50:12 +080024#define MX1_PWMC 0x00 /* PWM Control Register */
25#define MX1_PWMS 0x04 /* PWM Sample Register */
26#define MX1_PWMP 0x08 /* PWM Period Register */
Sascha Hauer29693242012-03-15 10:04:35 +010027
Michal Vokáč9f617ad2018-10-01 16:19:47 +020028#define MX1_PWMC_EN BIT(4)
Sascha Hauer29693242012-03-15 10:04:35 +010029
30/* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
31
Liu Ying40f260c2014-05-28 18:50:12 +080032#define MX3_PWMCR 0x00 /* PWM Control Register */
Liu Ying137fd452014-05-28 18:50:13 +080033#define MX3_PWMSR 0x04 /* PWM Status Register */
Liu Ying40f260c2014-05-28 18:50:12 +080034#define MX3_PWMSAR 0x0C /* PWM Sample Register */
35#define MX3_PWMPR 0x10 /* PWM Period Register */
Michal Vokáč9f617ad2018-10-01 16:19:47 +020036
37#define MX3_PWMCR_FWM GENMASK(27, 26)
38#define MX3_PWMCR_STOPEN BIT(25)
39#define MX3_PWMCR_DOZEN BIT(24)
40#define MX3_PWMCR_WAITEN BIT(23)
41#define MX3_PWMCR_DBGEN BIT(22)
42#define MX3_PWMCR_BCTR BIT(21)
43#define MX3_PWMCR_HCTR BIT(20)
44
45#define MX3_PWMCR_POUTC GENMASK(19, 18)
46#define MX3_PWMCR_POUTC_NORMAL 0
47#define MX3_PWMCR_POUTC_INVERTED 1
48#define MX3_PWMCR_POUTC_OFF 2
49
50#define MX3_PWMCR_CLKSRC GENMASK(17, 16)
51#define MX3_PWMCR_CLKSRC_OFF 0
52#define MX3_PWMCR_CLKSRC_IPG 1
53#define MX3_PWMCR_CLKSRC_IPG_HIGH 2
54#define MX3_PWMCR_CLKSRC_IPG_32K 3
55
56#define MX3_PWMCR_PRESCALER GENMASK(15, 4)
57
58#define MX3_PWMCR_SWR BIT(3)
59
60#define MX3_PWMCR_REPEAT GENMASK(2, 1)
61#define MX3_PWMCR_REPEAT_1X 0
62#define MX3_PWMCR_REPEAT_2X 1
63#define MX3_PWMCR_REPEAT_4X 2
64#define MX3_PWMCR_REPEAT_8X 3
65
66#define MX3_PWMCR_EN BIT(0)
67
68#define MX3_PWMSR_FWE BIT(6)
69#define MX3_PWMSR_CMP BIT(5)
70#define MX3_PWMSR_ROV BIT(4)
71#define MX3_PWMSR_FE BIT(3)
72
73#define MX3_PWMSR_FIFOAV GENMASK(2, 0)
74#define MX3_PWMSR_FIFOAV_EMPTY 0
75#define MX3_PWMSR_FIFOAV_1WORD 1
76#define MX3_PWMSR_FIFOAV_2WORDS 2
77#define MX3_PWMSR_FIFOAV_3WORDS 3
78#define MX3_PWMSR_FIFOAV_4WORDS 4
79
80#define MX3_PWMCR_PRESCALER_SET(x) FIELD_PREP(MX3_PWMCR_PRESCALER, (x) - 1)
81#define MX3_PWMCR_PRESCALER_GET(x) (FIELD_GET(MX3_PWMCR_PRESCALER, \
82 (x)) + 1)
Liu Ying137fd452014-05-28 18:50:13 +080083
84#define MX3_PWM_SWR_LOOP 5
Sascha Hauer29693242012-03-15 10:04:35 +010085
Michal Vokáčbf9b0b12018-10-01 16:19:48 +020086/* PWMPR register value of 0xffff has the same effect as 0xfffe */
87#define MX3_PWMPR_MAX 0xfffe
88
Sascha Hauer29693242012-03-15 10:04:35 +010089struct imx_chip {
Philipp Zabel7b27c162012-06-25 16:15:20 +020090 struct clk *clk_per;
Sascha Hauer29693242012-03-15 10:04:35 +010091
Sascha Hauer29693242012-03-15 10:04:35 +010092 void __iomem *mmio_base;
93
94 struct pwm_chip chip;
95};
96
97#define to_imx_chip(chip) container_of(chip, struct imx_chip, chip)
98
Michal Vokáčbf9b0b12018-10-01 16:19:48 +020099static void imx_pwm_get_state(struct pwm_chip *chip,
100 struct pwm_device *pwm, struct pwm_state *state)
101{
102 struct imx_chip *imx = to_imx_chip(chip);
103 u32 period, prescaler, pwm_clk, ret, val;
104 u64 tmp;
105
106 val = readl(imx->mmio_base + MX3_PWMCR);
107
108 if (val & MX3_PWMCR_EN) {
109 state->enabled = true;
110 ret = clk_prepare_enable(imx->clk_per);
111 if (ret)
112 return;
113 } else {
114 state->enabled = false;
115 }
116
117 switch (FIELD_GET(MX3_PWMCR_POUTC, val)) {
118 case MX3_PWMCR_POUTC_NORMAL:
119 state->polarity = PWM_POLARITY_NORMAL;
120 break;
121 case MX3_PWMCR_POUTC_INVERTED:
122 state->polarity = PWM_POLARITY_INVERSED;
123 break;
124 default:
125 dev_warn(chip->dev, "can't set polarity, output disconnected");
126 }
127
128 prescaler = MX3_PWMCR_PRESCALER_GET(val);
129 pwm_clk = clk_get_rate(imx->clk_per);
130 pwm_clk = DIV_ROUND_CLOSEST_ULL(pwm_clk, prescaler);
131 val = readl(imx->mmio_base + MX3_PWMPR);
132 period = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val;
133
134 /* PWMOUT (Hz) = PWMCLK / (PWMPR + 2) */
135 tmp = NSEC_PER_SEC * (u64)(period + 2);
136 state->period = DIV_ROUND_CLOSEST_ULL(tmp, pwm_clk);
137
138 /* PWMSAR can be read only if PWM is enabled */
139 if (state->enabled) {
140 val = readl(imx->mmio_base + MX3_PWMSAR);
141 tmp = NSEC_PER_SEC * (u64)(val);
142 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, pwm_clk);
143 } else {
144 state->duty_cycle = 0;
145 }
146}
147
Sascha Hauer19e73332012-07-03 17:28:14 +0200148static int imx_pwm_config_v1(struct pwm_chip *chip,
149 struct pwm_device *pwm, int duty_ns, int period_ns)
150{
151 struct imx_chip *imx = to_imx_chip(chip);
152
153 /*
154 * The PWM subsystem allows for exact frequencies. However,
155 * I cannot connect a scope on my device to the PWM line and
156 * thus cannot provide the program the PWM controller
157 * exactly. Instead, I'm relying on the fact that the
158 * Bootloader (u-boot or WinCE+haret) has programmed the PWM
159 * function group already. So I'll just modify the PWM sample
160 * register to follow the ratio of duty_ns vs. period_ns
161 * accordingly.
162 *
163 * This is good enough for programming the brightness of
164 * the LCD backlight.
165 *
166 * The real implementation would divide PERCLK[0] first by
167 * both the prescaler (/1 .. /128) and then by CLKSEL
168 * (/2 .. /16).
169 */
170 u32 max = readl(imx->mmio_base + MX1_PWMP);
171 u32 p = max * duty_ns / period_ns;
172 writel(max - p, imx->mmio_base + MX1_PWMS);
173
174 return 0;
175}
176
Lukasz Majewskib3c088f2017-01-29 22:54:08 +0100177static int imx_pwm_enable_v1(struct pwm_chip *chip, struct pwm_device *pwm)
178{
179 struct imx_chip *imx = to_imx_chip(chip);
180 u32 val;
181 int ret;
182
183 ret = clk_prepare_enable(imx->clk_per);
184 if (ret < 0)
185 return ret;
186
187 val = readl(imx->mmio_base + MX1_PWMC);
188 val |= MX1_PWMC_EN;
189 writel(val, imx->mmio_base + MX1_PWMC);
190
191 return 0;
192}
193
194static void imx_pwm_disable_v1(struct pwm_chip *chip, struct pwm_device *pwm)
Sascha Hauer66ad6a62012-08-28 11:39:25 +0200195{
196 struct imx_chip *imx = to_imx_chip(chip);
197 u32 val;
198
199 val = readl(imx->mmio_base + MX1_PWMC);
Lukasz Majewskib3c088f2017-01-29 22:54:08 +0100200 val &= ~MX1_PWMC_EN;
Sascha Hauer66ad6a62012-08-28 11:39:25 +0200201 writel(val, imx->mmio_base + MX1_PWMC);
Sascha Hauer29693242012-03-15 10:04:35 +0100202
Philipp Zabel7b27c162012-06-25 16:15:20 +0200203 clk_disable_unprepare(imx->clk_per);
Sascha Hauer29693242012-03-15 10:04:35 +0100204}
205
Lukasz Majewski970247a2017-01-29 22:54:09 +0100206static void imx_pwm_sw_reset(struct pwm_chip *chip)
207{
208 struct imx_chip *imx = to_imx_chip(chip);
209 struct device *dev = chip->dev;
210 int wait_count = 0;
211 u32 cr;
212
213 writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR);
214 do {
215 usleep_range(200, 1000);
216 cr = readl(imx->mmio_base + MX3_PWMCR);
217 } while ((cr & MX3_PWMCR_SWR) &&
218 (wait_count++ < MX3_PWM_SWR_LOOP));
219
220 if (cr & MX3_PWMCR_SWR)
221 dev_warn(dev, "software reset timeout\n");
222}
223
Lukasz Majewski73b1ff12017-01-29 22:54:10 +0100224static void imx_pwm_wait_fifo_slot(struct pwm_chip *chip,
225 struct pwm_device *pwm)
226{
227 struct imx_chip *imx = to_imx_chip(chip);
228 struct device *dev = chip->dev;
229 unsigned int period_ms;
230 int fifoav;
231 u32 sr;
232
233 sr = readl(imx->mmio_base + MX3_PWMSR);
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200234 fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr);
Lukasz Majewski73b1ff12017-01-29 22:54:10 +0100235 if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) {
236 period_ms = DIV_ROUND_UP(pwm_get_period(pwm),
237 NSEC_PER_MSEC);
238 msleep(period_ms);
239
240 sr = readl(imx->mmio_base + MX3_PWMSR);
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200241 if (fifoav == FIELD_GET(MX3_PWMSR_FIFOAV, sr))
Lukasz Majewski73b1ff12017-01-29 22:54:10 +0100242 dev_warn(dev, "there is no free FIFO slot\n");
243 }
244}
Lukasz Majewski970247a2017-01-29 22:54:09 +0100245
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100246static int imx_pwm_apply_v2(struct pwm_chip *chip, struct pwm_device *pwm,
247 struct pwm_state *state)
248{
249 unsigned long period_cycles, duty_cycles, prescale;
250 struct imx_chip *imx = to_imx_chip(chip);
251 struct pwm_state cstate;
252 unsigned long long c;
253 int ret;
Lukasz Majewski326ed312017-01-29 22:54:15 +0100254 u32 cr;
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100255
256 pwm_get_state(pwm, &cstate);
257
258 if (state->enabled) {
259 c = clk_get_rate(imx->clk_per);
260 c *= state->period;
261
262 do_div(c, 1000000000);
263 period_cycles = c;
264
265 prescale = period_cycles / 0x10000 + 1;
266
267 period_cycles /= prescale;
268 c = (unsigned long long)period_cycles * state->duty_cycle;
269 do_div(c, state->period);
270 duty_cycles = c;
271
272 /*
273 * according to imx pwm RM, the real period value should be
274 * PERIOD value in PWMPR plus 2.
275 */
276 if (period_cycles > 2)
277 period_cycles -= 2;
278 else
279 period_cycles = 0;
280
281 /*
282 * Wait for a free FIFO slot if the PWM is already enabled, and
283 * flush the FIFO if the PWM was disabled and is about to be
284 * enabled.
285 */
286 if (cstate.enabled) {
287 imx_pwm_wait_fifo_slot(chip, pwm);
288 } else {
289 ret = clk_prepare_enable(imx->clk_per);
290 if (ret)
291 return ret;
292
293 imx_pwm_sw_reset(chip);
294 }
295
296 writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
297 writel(period_cycles, imx->mmio_base + MX3_PWMPR);
298
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200299 cr = MX3_PWMCR_PRESCALER_SET(prescale) |
300 MX3_PWMCR_STOPEN | MX3_PWMCR_DOZEN | MX3_PWMCR_WAITEN |
301 FIELD_PREP(MX3_PWMCR_CLKSRC, MX3_PWMCR_CLKSRC_IPG_HIGH) |
302 MX3_PWMCR_DBGEN | MX3_PWMCR_EN;
Lukasz Majewski326ed312017-01-29 22:54:15 +0100303
304 if (state->polarity == PWM_POLARITY_INVERSED)
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200305 cr |= FIELD_PREP(MX3_PWMCR_POUTC,
306 MX3_PWMCR_POUTC_INVERTED);
Lukasz Majewski326ed312017-01-29 22:54:15 +0100307
308 writel(cr, imx->mmio_base + MX3_PWMCR);
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100309 } else if (cstate.enabled) {
310 writel(0, imx->mmio_base + MX3_PWMCR);
311
312 clk_disable_unprepare(imx->clk_per);
313 }
314
315 return 0;
316}
317
Lukasz Majewski00389222017-01-29 22:54:07 +0100318static const struct pwm_ops imx_pwm_ops_v1 = {
Lukasz Majewskib3c088f2017-01-29 22:54:08 +0100319 .enable = imx_pwm_enable_v1,
320 .disable = imx_pwm_disable_v1,
321 .config = imx_pwm_config_v1,
Lukasz Majewski00389222017-01-29 22:54:07 +0100322 .owner = THIS_MODULE,
323};
324
325static const struct pwm_ops imx_pwm_ops_v2 = {
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100326 .apply = imx_pwm_apply_v2,
Michal Vokáčbf9b0b12018-10-01 16:19:48 +0200327 .get_state = imx_pwm_get_state,
Sascha Hauer29693242012-03-15 10:04:35 +0100328 .owner = THIS_MODULE,
329};
330
Philipp Zabel479e2e32012-06-25 16:16:25 +0200331struct imx_pwm_data {
Lukasz Majewski326ed312017-01-29 22:54:15 +0100332 bool polarity_supported;
Lukasz Majewski00389222017-01-29 22:54:07 +0100333 const struct pwm_ops *ops;
Philipp Zabel479e2e32012-06-25 16:16:25 +0200334};
335
336static struct imx_pwm_data imx_pwm_data_v1 = {
Lukasz Majewski00389222017-01-29 22:54:07 +0100337 .ops = &imx_pwm_ops_v1,
Philipp Zabel479e2e32012-06-25 16:16:25 +0200338};
339
340static struct imx_pwm_data imx_pwm_data_v2 = {
Lukasz Majewski326ed312017-01-29 22:54:15 +0100341 .polarity_supported = true,
Lukasz Majewski00389222017-01-29 22:54:07 +0100342 .ops = &imx_pwm_ops_v2,
Philipp Zabel479e2e32012-06-25 16:16:25 +0200343};
344
345static const struct of_device_id imx_pwm_dt_ids[] = {
346 { .compatible = "fsl,imx1-pwm", .data = &imx_pwm_data_v1, },
347 { .compatible = "fsl,imx27-pwm", .data = &imx_pwm_data_v2, },
348 { /* sentinel */ }
349};
350MODULE_DEVICE_TABLE(of, imx_pwm_dt_ids);
351
Bill Pemberton3e9fe832012-11-19 13:23:14 -0500352static int imx_pwm_probe(struct platform_device *pdev)
Sascha Hauer29693242012-03-15 10:04:35 +0100353{
Philipp Zabel479e2e32012-06-25 16:16:25 +0200354 const struct of_device_id *of_id =
355 of_match_device(imx_pwm_dt_ids, &pdev->dev);
Lothar Waßmann983290b2012-12-05 16:34:41 +0100356 const struct imx_pwm_data *data;
Sascha Hauer29693242012-03-15 10:04:35 +0100357 struct imx_chip *imx;
358 struct resource *r;
359 int ret = 0;
360
Philipp Zabel479e2e32012-06-25 16:16:25 +0200361 if (!of_id)
362 return -ENODEV;
363
Lukasz Majewski00389222017-01-29 22:54:07 +0100364 data = of_id->data;
365
Axel Lina9970e32012-07-01 08:27:23 +0800366 imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
Jingoo Han1cbec742014-04-23 18:39:49 +0900367 if (imx == NULL)
Sascha Hauer29693242012-03-15 10:04:35 +0100368 return -ENOMEM;
Sascha Hauer29693242012-03-15 10:04:35 +0100369
Philipp Zabel7b27c162012-06-25 16:15:20 +0200370 imx->clk_per = devm_clk_get(&pdev->dev, "per");
371 if (IS_ERR(imx->clk_per)) {
372 dev_err(&pdev->dev, "getting per clock failed with %ld\n",
373 PTR_ERR(imx->clk_per));
374 return PTR_ERR(imx->clk_per);
375 }
Sascha Hauer29693242012-03-15 10:04:35 +0100376
Lukasz Majewski00389222017-01-29 22:54:07 +0100377 imx->chip.ops = data->ops;
Sascha Hauer29693242012-03-15 10:04:35 +0100378 imx->chip.dev = &pdev->dev;
379 imx->chip.base = -1;
380 imx->chip.npwm = 1;
381
Lukasz Majewski326ed312017-01-29 22:54:15 +0100382 if (data->polarity_supported) {
383 dev_dbg(&pdev->dev, "PWM supports output inversion\n");
384 imx->chip.of_xlate = of_pwm_xlate_with_flags;
385 imx->chip.of_pwm_n_cells = 3;
386 }
387
Sascha Hauer29693242012-03-15 10:04:35 +0100388 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding6d4294d2013-01-21 11:09:16 +0100389 imx->mmio_base = devm_ioremap_resource(&pdev->dev, r);
390 if (IS_ERR(imx->mmio_base))
391 return PTR_ERR(imx->mmio_base);
Sascha Hauer29693242012-03-15 10:04:35 +0100392
393 ret = pwmchip_add(&imx->chip);
394 if (ret < 0)
Axel Lina9970e32012-07-01 08:27:23 +0800395 return ret;
Sascha Hauer29693242012-03-15 10:04:35 +0100396
397 platform_set_drvdata(pdev, imx);
398 return 0;
Sascha Hauer29693242012-03-15 10:04:35 +0100399}
400
Bill Pemberton77f37912012-11-19 13:26:09 -0500401static int imx_pwm_remove(struct platform_device *pdev)
Sascha Hauer29693242012-03-15 10:04:35 +0100402{
403 struct imx_chip *imx;
Sascha Hauer29693242012-03-15 10:04:35 +0100404
405 imx = platform_get_drvdata(pdev);
406 if (imx == NULL)
407 return -ENODEV;
408
Axel Lina9970e32012-07-01 08:27:23 +0800409 return pwmchip_remove(&imx->chip);
Sascha Hauer29693242012-03-15 10:04:35 +0100410}
411
412static struct platform_driver imx_pwm_driver = {
413 .driver = {
Philipp Zabel479e2e32012-06-25 16:16:25 +0200414 .name = "imx-pwm",
Sachin Kamatbecbca132013-09-30 08:56:41 +0530415 .of_match_table = imx_pwm_dt_ids,
Sascha Hauer29693242012-03-15 10:04:35 +0100416 },
417 .probe = imx_pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500418 .remove = imx_pwm_remove,
Sascha Hauer29693242012-03-15 10:04:35 +0100419};
420
Sascha Hauer208d0382012-08-28 08:27:40 +0200421module_platform_driver(imx_pwm_driver);
Sascha Hauer29693242012-03-15 10:04:35 +0100422
423MODULE_LICENSE("GPL v2");
424MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");