blob: 434a351fb626be9a790f1a8cafa1e77595bd9dfd [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
Liu Ying40f260c2014-05-28 18:50:12 +080022#define MX3_PWMCR 0x00 /* PWM Control Register */
Liu Ying137fd452014-05-28 18:50:13 +080023#define MX3_PWMSR 0x04 /* PWM Status Register */
Liu Ying40f260c2014-05-28 18:50:12 +080024#define MX3_PWMSAR 0x0C /* PWM Sample Register */
25#define MX3_PWMPR 0x10 /* PWM Period Register */
Michal Vokáč9f617ad2018-10-01 16:19:47 +020026
27#define MX3_PWMCR_FWM GENMASK(27, 26)
28#define MX3_PWMCR_STOPEN BIT(25)
29#define MX3_PWMCR_DOZEN BIT(24)
30#define MX3_PWMCR_WAITEN BIT(23)
31#define MX3_PWMCR_DBGEN BIT(22)
32#define MX3_PWMCR_BCTR BIT(21)
33#define MX3_PWMCR_HCTR BIT(20)
34
35#define MX3_PWMCR_POUTC GENMASK(19, 18)
36#define MX3_PWMCR_POUTC_NORMAL 0
37#define MX3_PWMCR_POUTC_INVERTED 1
38#define MX3_PWMCR_POUTC_OFF 2
39
40#define MX3_PWMCR_CLKSRC GENMASK(17, 16)
41#define MX3_PWMCR_CLKSRC_OFF 0
42#define MX3_PWMCR_CLKSRC_IPG 1
43#define MX3_PWMCR_CLKSRC_IPG_HIGH 2
44#define MX3_PWMCR_CLKSRC_IPG_32K 3
45
46#define MX3_PWMCR_PRESCALER GENMASK(15, 4)
47
48#define MX3_PWMCR_SWR BIT(3)
49
50#define MX3_PWMCR_REPEAT GENMASK(2, 1)
51#define MX3_PWMCR_REPEAT_1X 0
52#define MX3_PWMCR_REPEAT_2X 1
53#define MX3_PWMCR_REPEAT_4X 2
54#define MX3_PWMCR_REPEAT_8X 3
55
56#define MX3_PWMCR_EN BIT(0)
57
58#define MX3_PWMSR_FWE BIT(6)
59#define MX3_PWMSR_CMP BIT(5)
60#define MX3_PWMSR_ROV BIT(4)
61#define MX3_PWMSR_FE BIT(3)
62
63#define MX3_PWMSR_FIFOAV GENMASK(2, 0)
64#define MX3_PWMSR_FIFOAV_EMPTY 0
65#define MX3_PWMSR_FIFOAV_1WORD 1
66#define MX3_PWMSR_FIFOAV_2WORDS 2
67#define MX3_PWMSR_FIFOAV_3WORDS 3
68#define MX3_PWMSR_FIFOAV_4WORDS 4
69
70#define MX3_PWMCR_PRESCALER_SET(x) FIELD_PREP(MX3_PWMCR_PRESCALER, (x) - 1)
71#define MX3_PWMCR_PRESCALER_GET(x) (FIELD_GET(MX3_PWMCR_PRESCALER, \
72 (x)) + 1)
Liu Ying137fd452014-05-28 18:50:13 +080073
74#define MX3_PWM_SWR_LOOP 5
Sascha Hauer29693242012-03-15 10:04:35 +010075
Michal Vokáčbf9b0b12018-10-01 16:19:48 +020076/* PWMPR register value of 0xffff has the same effect as 0xfffe */
77#define MX3_PWMPR_MAX 0xfffe
78
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +010079struct pwm_imx27_chip {
Anson Huang9f4c8f92018-12-19 05:24:58 +000080 struct clk *clk_ipg;
Philipp Zabel7b27c162012-06-25 16:15:20 +020081 struct clk *clk_per;
Sascha Hauer29693242012-03-15 10:04:35 +010082 void __iomem *mmio_base;
Sascha Hauer29693242012-03-15 10:04:35 +010083 struct pwm_chip chip;
84};
85
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +010086#define to_pwm_imx27_chip(chip) container_of(chip, struct pwm_imx27_chip, chip)
Sascha Hauer29693242012-03-15 10:04:35 +010087
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +010088static int pwm_imx27_clk_prepare_enable(struct pwm_chip *chip)
Anson Huang9f4c8f92018-12-19 05:24:58 +000089{
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +010090 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
Anson Huang9f4c8f92018-12-19 05:24:58 +000091 int ret;
92
93 ret = clk_prepare_enable(imx->clk_ipg);
94 if (ret)
95 return ret;
96
97 ret = clk_prepare_enable(imx->clk_per);
98 if (ret) {
99 clk_disable_unprepare(imx->clk_ipg);
100 return ret;
101 }
102
103 return 0;
104}
105
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100106static void pwm_imx27_clk_disable_unprepare(struct pwm_chip *chip)
Anson Huang9f4c8f92018-12-19 05:24:58 +0000107{
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100108 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
Anson Huang9f4c8f92018-12-19 05:24:58 +0000109
110 clk_disable_unprepare(imx->clk_per);
111 clk_disable_unprepare(imx->clk_ipg);
112}
113
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100114static void pwm_imx27_get_state(struct pwm_chip *chip,
115 struct pwm_device *pwm, struct pwm_state *state)
Michal Vokáčbf9b0b12018-10-01 16:19:48 +0200116{
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100117 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
Dan Carpenter7ca17b22019-01-09 11:27:47 +0300118 u32 period, prescaler, pwm_clk, val;
Michal Vokáčbf9b0b12018-10-01 16:19:48 +0200119 u64 tmp;
Dan Carpenter7ca17b22019-01-09 11:27:47 +0300120 int ret;
Michal Vokáčbf9b0b12018-10-01 16:19:48 +0200121
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100122 ret = pwm_imx27_clk_prepare_enable(chip);
Anson Huang9f4c8f92018-12-19 05:24:58 +0000123 if (ret < 0)
124 return;
125
Michal Vokáčbf9b0b12018-10-01 16:19:48 +0200126 val = readl(imx->mmio_base + MX3_PWMCR);
127
Uwe Kleine-König519ef9b2019-01-10 20:33:53 +0100128 if (val & MX3_PWMCR_EN)
Michal Vokáčbf9b0b12018-10-01 16:19:48 +0200129 state->enabled = true;
Uwe Kleine-König519ef9b2019-01-10 20:33:53 +0100130 else
Michal Vokáčbf9b0b12018-10-01 16:19:48 +0200131 state->enabled = false;
Michal Vokáčbf9b0b12018-10-01 16:19:48 +0200132
133 switch (FIELD_GET(MX3_PWMCR_POUTC, val)) {
134 case MX3_PWMCR_POUTC_NORMAL:
135 state->polarity = PWM_POLARITY_NORMAL;
136 break;
137 case MX3_PWMCR_POUTC_INVERTED:
138 state->polarity = PWM_POLARITY_INVERSED;
139 break;
140 default:
141 dev_warn(chip->dev, "can't set polarity, output disconnected");
142 }
143
144 prescaler = MX3_PWMCR_PRESCALER_GET(val);
145 pwm_clk = clk_get_rate(imx->clk_per);
146 pwm_clk = DIV_ROUND_CLOSEST_ULL(pwm_clk, prescaler);
147 val = readl(imx->mmio_base + MX3_PWMPR);
148 period = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val;
149
150 /* PWMOUT (Hz) = PWMCLK / (PWMPR + 2) */
151 tmp = NSEC_PER_SEC * (u64)(period + 2);
152 state->period = DIV_ROUND_CLOSEST_ULL(tmp, pwm_clk);
153
154 /* PWMSAR can be read only if PWM is enabled */
155 if (state->enabled) {
156 val = readl(imx->mmio_base + MX3_PWMSAR);
157 tmp = NSEC_PER_SEC * (u64)(val);
158 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, pwm_clk);
159 } else {
160 state->duty_cycle = 0;
161 }
Anson Huang9f4c8f92018-12-19 05:24:58 +0000162
Uwe Kleine-König519ef9b2019-01-10 20:33:53 +0100163 if (!state->enabled)
164 pwm_imx27_clk_disable_unprepare(chip);
Michal Vokáčbf9b0b12018-10-01 16:19:48 +0200165}
166
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100167static void pwm_imx27_sw_reset(struct pwm_chip *chip)
Sascha Hauer19e73332012-07-03 17:28:14 +0200168{
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100169 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
Lukasz Majewski970247a2017-01-29 22:54:09 +0100170 struct device *dev = chip->dev;
171 int wait_count = 0;
172 u32 cr;
173
174 writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR);
175 do {
176 usleep_range(200, 1000);
177 cr = readl(imx->mmio_base + MX3_PWMCR);
178 } while ((cr & MX3_PWMCR_SWR) &&
179 (wait_count++ < MX3_PWM_SWR_LOOP));
180
181 if (cr & MX3_PWMCR_SWR)
182 dev_warn(dev, "software reset timeout\n");
183}
184
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100185static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip,
186 struct pwm_device *pwm)
Lukasz Majewski73b1ff12017-01-29 22:54:10 +0100187{
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100188 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
Lukasz Majewski73b1ff12017-01-29 22:54:10 +0100189 struct device *dev = chip->dev;
190 unsigned int period_ms;
191 int fifoav;
192 u32 sr;
193
194 sr = readl(imx->mmio_base + MX3_PWMSR);
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200195 fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr);
Lukasz Majewski73b1ff12017-01-29 22:54:10 +0100196 if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) {
197 period_ms = DIV_ROUND_UP(pwm_get_period(pwm),
198 NSEC_PER_MSEC);
199 msleep(period_ms);
200
201 sr = readl(imx->mmio_base + MX3_PWMSR);
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200202 if (fifoav == FIELD_GET(MX3_PWMSR_FIFOAV, sr))
Lukasz Majewski73b1ff12017-01-29 22:54:10 +0100203 dev_warn(dev, "there is no free FIFO slot\n");
204 }
205}
Lukasz Majewski970247a2017-01-29 22:54:09 +0100206
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100207static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,
208 struct pwm_state *state)
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100209{
210 unsigned long period_cycles, duty_cycles, prescale;
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100211 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100212 struct pwm_state cstate;
213 unsigned long long c;
214 int ret;
Lukasz Majewski326ed312017-01-29 22:54:15 +0100215 u32 cr;
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100216
217 pwm_get_state(pwm, &cstate);
218
219 if (state->enabled) {
220 c = clk_get_rate(imx->clk_per);
221 c *= state->period;
222
223 do_div(c, 1000000000);
224 period_cycles = c;
225
226 prescale = period_cycles / 0x10000 + 1;
227
228 period_cycles /= prescale;
229 c = (unsigned long long)period_cycles * state->duty_cycle;
230 do_div(c, state->period);
231 duty_cycles = c;
232
233 /*
234 * according to imx pwm RM, the real period value should be
235 * PERIOD value in PWMPR plus 2.
236 */
237 if (period_cycles > 2)
238 period_cycles -= 2;
239 else
240 period_cycles = 0;
241
242 /*
243 * Wait for a free FIFO slot if the PWM is already enabled, and
244 * flush the FIFO if the PWM was disabled and is about to be
245 * enabled.
246 */
247 if (cstate.enabled) {
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100248 pwm_imx27_wait_fifo_slot(chip, pwm);
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100249 } else {
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100250 ret = pwm_imx27_clk_prepare_enable(chip);
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100251 if (ret)
252 return ret;
253
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100254 pwm_imx27_sw_reset(chip);
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100255 }
256
257 writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
258 writel(period_cycles, imx->mmio_base + MX3_PWMPR);
259
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200260 cr = MX3_PWMCR_PRESCALER_SET(prescale) |
261 MX3_PWMCR_STOPEN | MX3_PWMCR_DOZEN | MX3_PWMCR_WAITEN |
262 FIELD_PREP(MX3_PWMCR_CLKSRC, MX3_PWMCR_CLKSRC_IPG_HIGH) |
263 MX3_PWMCR_DBGEN | MX3_PWMCR_EN;
Lukasz Majewski326ed312017-01-29 22:54:15 +0100264
265 if (state->polarity == PWM_POLARITY_INVERSED)
Michal Vokáč9f617ad2018-10-01 16:19:47 +0200266 cr |= FIELD_PREP(MX3_PWMCR_POUTC,
267 MX3_PWMCR_POUTC_INVERTED);
Lukasz Majewski326ed312017-01-29 22:54:15 +0100268
269 writel(cr, imx->mmio_base + MX3_PWMCR);
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100270 } else if (cstate.enabled) {
271 writel(0, imx->mmio_base + MX3_PWMCR);
272
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100273 pwm_imx27_clk_disable_unprepare(chip);
Lukasz Majewski0ca1a112017-01-29 22:54:11 +0100274 }
275
276 return 0;
277}
278
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100279static const struct pwm_ops pwm_imx27_ops = {
280 .apply = pwm_imx27_apply,
281 .get_state = pwm_imx27_get_state,
Lukasz Majewski00389222017-01-29 22:54:07 +0100282 .owner = THIS_MODULE,
283};
284
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100285static const struct of_device_id pwm_imx27_dt_ids[] = {
286 { .compatible = "fsl,imx27-pwm", },
Philipp Zabel479e2e32012-06-25 16:16:25 +0200287 { /* sentinel */ }
288};
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100289MODULE_DEVICE_TABLE(of, pwm_imx27_dt_ids);
Philipp Zabel479e2e32012-06-25 16:16:25 +0200290
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100291static int pwm_imx27_probe(struct platform_device *pdev)
Sascha Hauer29693242012-03-15 10:04:35 +0100292{
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100293 struct pwm_imx27_chip *imx;
Sascha Hauer29693242012-03-15 10:04:35 +0100294
Axel Lina9970e32012-07-01 08:27:23 +0800295 imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
Jingoo Han1cbec742014-04-23 18:39:49 +0900296 if (imx == NULL)
Sascha Hauer29693242012-03-15 10:04:35 +0100297 return -ENOMEM;
Sascha Hauer29693242012-03-15 10:04:35 +0100298
Uwe Kleine-Königf20b1872019-01-07 20:53:50 +0100299 platform_set_drvdata(pdev, imx);
300
Anson Huang9f4c8f92018-12-19 05:24:58 +0000301 imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
302 if (IS_ERR(imx->clk_ipg)) {
303 dev_err(&pdev->dev, "getting ipg clock failed with %ld\n",
304 PTR_ERR(imx->clk_ipg));
305 return PTR_ERR(imx->clk_ipg);
306 }
307
Philipp Zabel7b27c162012-06-25 16:15:20 +0200308 imx->clk_per = devm_clk_get(&pdev->dev, "per");
309 if (IS_ERR(imx->clk_per)) {
Uwe Kleine-Königb9a5c602019-01-07 20:53:51 +0100310 int ret = PTR_ERR(imx->clk_per);
311
312 if (ret != -EPROBE_DEFER)
313 dev_err(&pdev->dev,
314 "failed to get peripheral clock: %d\n",
315 ret);
316
317 return ret;
Philipp Zabel7b27c162012-06-25 16:15:20 +0200318 }
Sascha Hauer29693242012-03-15 10:04:35 +0100319
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100320 imx->chip.ops = &pwm_imx27_ops;
Sascha Hauer29693242012-03-15 10:04:35 +0100321 imx->chip.dev = &pdev->dev;
322 imx->chip.base = -1;
323 imx->chip.npwm = 1;
324
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100325 imx->chip.of_xlate = of_pwm_xlate_with_flags;
326 imx->chip.of_pwm_n_cells = 3;
Lukasz Majewski326ed312017-01-29 22:54:15 +0100327
Anson Huang1347c942019-04-01 05:24:02 +0000328 imx->mmio_base = devm_platform_ioremap_resource(pdev, 0);
Thierry Reding6d4294d2013-01-21 11:09:16 +0100329 if (IS_ERR(imx->mmio_base))
330 return PTR_ERR(imx->mmio_base);
Sascha Hauer29693242012-03-15 10:04:35 +0100331
Uwe Kleine-Königf20b1872019-01-07 20:53:50 +0100332 return pwmchip_add(&imx->chip);
Sascha Hauer29693242012-03-15 10:04:35 +0100333}
334
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100335static int pwm_imx27_remove(struct platform_device *pdev)
Sascha Hauer29693242012-03-15 10:04:35 +0100336{
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100337 struct pwm_imx27_chip *imx;
Sascha Hauer29693242012-03-15 10:04:35 +0100338
339 imx = platform_get_drvdata(pdev);
Sascha Hauer29693242012-03-15 10:04:35 +0100340
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100341 pwm_imx27_clk_disable_unprepare(&imx->chip);
Anson Huang9f4c8f92018-12-19 05:24:58 +0000342
Axel Lina9970e32012-07-01 08:27:23 +0800343 return pwmchip_remove(&imx->chip);
Sascha Hauer29693242012-03-15 10:04:35 +0100344}
345
346static struct platform_driver imx_pwm_driver = {
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100347 .driver = {
348 .name = "pwm-imx27",
349 .of_match_table = pwm_imx27_dt_ids,
Sascha Hauer29693242012-03-15 10:04:35 +0100350 },
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100351 .probe = pwm_imx27_probe,
352 .remove = pwm_imx27_remove,
Sascha Hauer29693242012-03-15 10:04:35 +0100353};
Sascha Hauer208d0382012-08-28 08:27:40 +0200354module_platform_driver(imx_pwm_driver);
Sascha Hauer29693242012-03-15 10:04:35 +0100355
356MODULE_LICENSE("GPL v2");
357MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");