blob: 7a4907b73d7c7bc463e5cb47c1ac0a698dc49d89 [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
86struct imx_chip {
Philipp Zabel7b27c162012-06-25 16:15:20 +020087 struct clk *clk_per;
Sascha Hauer29693242012-03-15 10:04:35 +010088
Sascha Hauer29693242012-03-15 10:04:35 +010089 void __iomem *mmio_base;
90
91 struct pwm_chip chip;
92};
93
94#define to_imx_chip(chip) container_of(chip, struct imx_chip, chip)
95
Sascha Hauer19e73332012-07-03 17:28:14 +020096static int imx_pwm_config_v1(struct pwm_chip *chip,
97 struct pwm_device *pwm, int duty_ns, int period_ns)
98{
99 struct imx_chip *imx = to_imx_chip(chip);
100
101 /*
102 * The PWM subsystem allows for exact frequencies. However,
103 * I cannot connect a scope on my device to the PWM line and
104 * thus cannot provide the program the PWM controller
105 * exactly. Instead, I'm relying on the fact that the
106 * Bootloader (u-boot or WinCE+haret) has programmed the PWM
107 * function group already. So I'll just modify the PWM sample
108 * register to follow the ratio of duty_ns vs. period_ns
109 * accordingly.
110 *
111 * This is good enough for programming the brightness of
112 * the LCD backlight.
113 *
114 * The real implementation would divide PERCLK[0] first by
115 * both the prescaler (/1 .. /128) and then by CLKSEL
116 * (/2 .. /16).
117 */
118 u32 max = readl(imx->mmio_base + MX1_PWMP);
119 u32 p = max * duty_ns / period_ns;
120 writel(max - p, imx->mmio_base + MX1_PWMS);
121
122 return 0;
123}
124
Lukasz Majewskib3c088f2017-01-29 22:54:08 +0100125static int imx_pwm_enable_v1(struct pwm_chip *chip, struct pwm_device *pwm)
126{
127 struct imx_chip *imx = to_imx_chip(chip);
128 u32 val;
129 int ret;
130
131 ret = clk_prepare_enable(imx->clk_per);
132 if (ret < 0)
133 return ret;
134
135 val = readl(imx->mmio_base + MX1_PWMC);
136 val |= MX1_PWMC_EN;
137 writel(val, imx->mmio_base + MX1_PWMC);
138
139 return 0;
140}
141
142static void imx_pwm_disable_v1(struct pwm_chip *chip, struct pwm_device *pwm)
Sascha Hauer66ad6a62012-08-28 11:39:25 +0200143{
144 struct imx_chip *imx = to_imx_chip(chip);
145 u32 val;
146
147 val = readl(imx->mmio_base + MX1_PWMC);
Lukasz Majewskib3c088f2017-01-29 22:54:08 +0100148 val &= ~MX1_PWMC_EN;
Sascha Hauer66ad6a62012-08-28 11:39:25 +0200149 writel(val, imx->mmio_base + MX1_PWMC);
Sascha Hauer29693242012-03-15 10:04:35 +0100150
Philipp Zabel7b27c162012-06-25 16:15:20 +0200151 clk_disable_unprepare(imx->clk_per);
Sascha Hauer29693242012-03-15 10:04:35 +0100152}
153
Lukasz Majewski970247a2017-01-29 22:54:09 +0100154static void imx_pwm_sw_reset(struct pwm_chip *chip)
155{
156 struct imx_chip *imx = to_imx_chip(chip);
157 struct device *dev = chip->dev;
158 int wait_count = 0;
159 u32 cr;
160
161 writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR);
162 do {
163 usleep_range(200, 1000);
164 cr = readl(imx->mmio_base + MX3_PWMCR);
165 } while ((cr & MX3_PWMCR_SWR) &&
166 (wait_count++ < MX3_PWM_SWR_LOOP));
167
168 if (cr & MX3_PWMCR_SWR)
169 dev_warn(dev, "software reset timeout\n");
170}
171
Lukasz Majewski73b1ff12017-01-29 22:54:10 +0100172static void imx_pwm_wait_fifo_slot(struct pwm_chip *chip,
173 struct pwm_device *pwm)
174{
175 struct imx_chip *imx = to_imx_chip(chip);
176 struct device *dev = chip->dev;
177 unsigned int period_ms;
178 int fifoav;
179 u32 sr;
180
181 sr = readl(imx->mmio_base + MX3_PWMSR);
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200182 fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr);
Lukasz Majewski73b1ff12017-01-29 22:54:10 +0100183 if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) {
184 period_ms = DIV_ROUND_UP(pwm_get_period(pwm),
185 NSEC_PER_MSEC);
186 msleep(period_ms);
187
188 sr = readl(imx->mmio_base + MX3_PWMSR);
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200189 if (fifoav == FIELD_GET(MX3_PWMSR_FIFOAV, sr))
Lukasz Majewski73b1ff12017-01-29 22:54:10 +0100190 dev_warn(dev, "there is no free FIFO slot\n");
191 }
192}
Lukasz Majewski970247a2017-01-29 22:54:09 +0100193
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100194static int imx_pwm_apply_v2(struct pwm_chip *chip, struct pwm_device *pwm,
195 struct pwm_state *state)
196{
197 unsigned long period_cycles, duty_cycles, prescale;
198 struct imx_chip *imx = to_imx_chip(chip);
199 struct pwm_state cstate;
200 unsigned long long c;
201 int ret;
Lukasz Majewski326ed312017-01-29 22:54:15 +0100202 u32 cr;
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100203
204 pwm_get_state(pwm, &cstate);
205
206 if (state->enabled) {
207 c = clk_get_rate(imx->clk_per);
208 c *= state->period;
209
210 do_div(c, 1000000000);
211 period_cycles = c;
212
213 prescale = period_cycles / 0x10000 + 1;
214
215 period_cycles /= prescale;
216 c = (unsigned long long)period_cycles * state->duty_cycle;
217 do_div(c, state->period);
218 duty_cycles = c;
219
220 /*
221 * according to imx pwm RM, the real period value should be
222 * PERIOD value in PWMPR plus 2.
223 */
224 if (period_cycles > 2)
225 period_cycles -= 2;
226 else
227 period_cycles = 0;
228
229 /*
230 * Wait for a free FIFO slot if the PWM is already enabled, and
231 * flush the FIFO if the PWM was disabled and is about to be
232 * enabled.
233 */
234 if (cstate.enabled) {
235 imx_pwm_wait_fifo_slot(chip, pwm);
236 } else {
237 ret = clk_prepare_enable(imx->clk_per);
238 if (ret)
239 return ret;
240
241 imx_pwm_sw_reset(chip);
242 }
243
244 writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
245 writel(period_cycles, imx->mmio_base + MX3_PWMPR);
246
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200247 cr = MX3_PWMCR_PRESCALER_SET(prescale) |
248 MX3_PWMCR_STOPEN | MX3_PWMCR_DOZEN | MX3_PWMCR_WAITEN |
249 FIELD_PREP(MX3_PWMCR_CLKSRC, MX3_PWMCR_CLKSRC_IPG_HIGH) |
250 MX3_PWMCR_DBGEN | MX3_PWMCR_EN;
Lukasz Majewski326ed312017-01-29 22:54:15 +0100251
252 if (state->polarity == PWM_POLARITY_INVERSED)
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200253 cr |= FIELD_PREP(MX3_PWMCR_POUTC,
254 MX3_PWMCR_POUTC_INVERTED);
Lukasz Majewski326ed312017-01-29 22:54:15 +0100255
256 writel(cr, imx->mmio_base + MX3_PWMCR);
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100257 } else if (cstate.enabled) {
258 writel(0, imx->mmio_base + MX3_PWMCR);
259
260 clk_disable_unprepare(imx->clk_per);
261 }
262
263 return 0;
264}
265
Lukasz Majewski00389222017-01-29 22:54:07 +0100266static const struct pwm_ops imx_pwm_ops_v1 = {
Lukasz Majewskib3c088f2017-01-29 22:54:08 +0100267 .enable = imx_pwm_enable_v1,
268 .disable = imx_pwm_disable_v1,
269 .config = imx_pwm_config_v1,
Lukasz Majewski00389222017-01-29 22:54:07 +0100270 .owner = THIS_MODULE,
271};
272
273static const struct pwm_ops imx_pwm_ops_v2 = {
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100274 .apply = imx_pwm_apply_v2,
Sascha Hauer29693242012-03-15 10:04:35 +0100275 .owner = THIS_MODULE,
276};
277
Philipp Zabel479e2e32012-06-25 16:16:25 +0200278struct imx_pwm_data {
Lukasz Majewski326ed312017-01-29 22:54:15 +0100279 bool polarity_supported;
Lukasz Majewski00389222017-01-29 22:54:07 +0100280 const struct pwm_ops *ops;
Philipp Zabel479e2e32012-06-25 16:16:25 +0200281};
282
283static struct imx_pwm_data imx_pwm_data_v1 = {
Lukasz Majewski00389222017-01-29 22:54:07 +0100284 .ops = &imx_pwm_ops_v1,
Philipp Zabel479e2e32012-06-25 16:16:25 +0200285};
286
287static struct imx_pwm_data imx_pwm_data_v2 = {
Lukasz Majewski326ed312017-01-29 22:54:15 +0100288 .polarity_supported = true,
Lukasz Majewski00389222017-01-29 22:54:07 +0100289 .ops = &imx_pwm_ops_v2,
Philipp Zabel479e2e32012-06-25 16:16:25 +0200290};
291
292static const struct of_device_id imx_pwm_dt_ids[] = {
293 { .compatible = "fsl,imx1-pwm", .data = &imx_pwm_data_v1, },
294 { .compatible = "fsl,imx27-pwm", .data = &imx_pwm_data_v2, },
295 { /* sentinel */ }
296};
297MODULE_DEVICE_TABLE(of, imx_pwm_dt_ids);
298
Bill Pemberton3e9fe832012-11-19 13:23:14 -0500299static int imx_pwm_probe(struct platform_device *pdev)
Sascha Hauer29693242012-03-15 10:04:35 +0100300{
Philipp Zabel479e2e32012-06-25 16:16:25 +0200301 const struct of_device_id *of_id =
302 of_match_device(imx_pwm_dt_ids, &pdev->dev);
Lothar Waßmann983290b2012-12-05 16:34:41 +0100303 const struct imx_pwm_data *data;
Sascha Hauer29693242012-03-15 10:04:35 +0100304 struct imx_chip *imx;
305 struct resource *r;
306 int ret = 0;
307
Philipp Zabel479e2e32012-06-25 16:16:25 +0200308 if (!of_id)
309 return -ENODEV;
310
Lukasz Majewski00389222017-01-29 22:54:07 +0100311 data = of_id->data;
312
Axel Lina9970e32012-07-01 08:27:23 +0800313 imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
Jingoo Han1cbec742014-04-23 18:39:49 +0900314 if (imx == NULL)
Sascha Hauer29693242012-03-15 10:04:35 +0100315 return -ENOMEM;
Sascha Hauer29693242012-03-15 10:04:35 +0100316
Philipp Zabel7b27c162012-06-25 16:15:20 +0200317 imx->clk_per = devm_clk_get(&pdev->dev, "per");
318 if (IS_ERR(imx->clk_per)) {
319 dev_err(&pdev->dev, "getting per clock failed with %ld\n",
320 PTR_ERR(imx->clk_per));
321 return PTR_ERR(imx->clk_per);
322 }
Sascha Hauer29693242012-03-15 10:04:35 +0100323
Lukasz Majewski00389222017-01-29 22:54:07 +0100324 imx->chip.ops = data->ops;
Sascha Hauer29693242012-03-15 10:04:35 +0100325 imx->chip.dev = &pdev->dev;
326 imx->chip.base = -1;
327 imx->chip.npwm = 1;
328
Lukasz Majewski326ed312017-01-29 22:54:15 +0100329 if (data->polarity_supported) {
330 dev_dbg(&pdev->dev, "PWM supports output inversion\n");
331 imx->chip.of_xlate = of_pwm_xlate_with_flags;
332 imx->chip.of_pwm_n_cells = 3;
333 }
334
Sascha Hauer29693242012-03-15 10:04:35 +0100335 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding6d4294d2013-01-21 11:09:16 +0100336 imx->mmio_base = devm_ioremap_resource(&pdev->dev, r);
337 if (IS_ERR(imx->mmio_base))
338 return PTR_ERR(imx->mmio_base);
Sascha Hauer29693242012-03-15 10:04:35 +0100339
340 ret = pwmchip_add(&imx->chip);
341 if (ret < 0)
Axel Lina9970e32012-07-01 08:27:23 +0800342 return ret;
Sascha Hauer29693242012-03-15 10:04:35 +0100343
344 platform_set_drvdata(pdev, imx);
345 return 0;
Sascha Hauer29693242012-03-15 10:04:35 +0100346}
347
Bill Pemberton77f37912012-11-19 13:26:09 -0500348static int imx_pwm_remove(struct platform_device *pdev)
Sascha Hauer29693242012-03-15 10:04:35 +0100349{
350 struct imx_chip *imx;
Sascha Hauer29693242012-03-15 10:04:35 +0100351
352 imx = platform_get_drvdata(pdev);
353 if (imx == NULL)
354 return -ENODEV;
355
Axel Lina9970e32012-07-01 08:27:23 +0800356 return pwmchip_remove(&imx->chip);
Sascha Hauer29693242012-03-15 10:04:35 +0100357}
358
359static struct platform_driver imx_pwm_driver = {
360 .driver = {
Philipp Zabel479e2e32012-06-25 16:16:25 +0200361 .name = "imx-pwm",
Sachin Kamatbecbca132013-09-30 08:56:41 +0530362 .of_match_table = imx_pwm_dt_ids,
Sascha Hauer29693242012-03-15 10:04:35 +0100363 },
364 .probe = imx_pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500365 .remove = imx_pwm_remove,
Sascha Hauer29693242012-03-15 10:04:35 +0100366};
367
Sascha Hauer208d0382012-08-28 08:27:40 +0200368module_platform_driver(imx_pwm_driver);
Sascha Hauer29693242012-03-15 10:04:35 +0100369
370MODULE_LICENSE("GPL v2");
371MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");