blob: 07ce5a8538c0be4656d081dfc08537475794396a [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2018-2019 NXP.
4 *
5 * Limitations:
6 * - The TPM counter and period counter are shared between
7 * multiple channels, so all channels should use same period
8 * settings.
9 * - Changes to polarity cannot be latched at the time of the
10 * next period start.
11 * - Changing period and duty cycle together isn't atomic,
12 * with the wrong timing it might happen that a period is
13 * produced with old duty cycle but new period settings.
14 */
15
16#include <linux/bitfield.h>
17#include <linux/bitops.h>
18#include <linux/clk.h>
19#include <linux/err.h>
20#include <linux/io.h>
21#include <linux/log2.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/platform_device.h>
26#include <linux/pwm.h>
27#include <linux/slab.h>
28
29#define PWM_IMX_TPM_PARAM 0x4
30#define PWM_IMX_TPM_GLOBAL 0x8
31#define PWM_IMX_TPM_SC 0x10
32#define PWM_IMX_TPM_CNT 0x14
33#define PWM_IMX_TPM_MOD 0x18
34#define PWM_IMX_TPM_CnSC(n) (0x20 + (n) * 0x8)
35#define PWM_IMX_TPM_CnV(n) (0x24 + (n) * 0x8)
36
37#define PWM_IMX_TPM_PARAM_CHAN GENMASK(7, 0)
38
39#define PWM_IMX_TPM_SC_PS GENMASK(2, 0)
40#define PWM_IMX_TPM_SC_CMOD GENMASK(4, 3)
41#define PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK FIELD_PREP(PWM_IMX_TPM_SC_CMOD, 1)
42#define PWM_IMX_TPM_SC_CPWMS BIT(5)
43
44#define PWM_IMX_TPM_CnSC_CHF BIT(7)
45#define PWM_IMX_TPM_CnSC_MSB BIT(5)
46#define PWM_IMX_TPM_CnSC_MSA BIT(4)
47
48/*
49 * The reference manual describes this field as two separate bits. The
50 * semantic of the two bits isn't orthogonal though, so they are treated
51 * together as a 2-bit field here.
52 */
53#define PWM_IMX_TPM_CnSC_ELS GENMASK(3, 2)
54#define PWM_IMX_TPM_CnSC_ELS_INVERSED FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 1)
55#define PWM_IMX_TPM_CnSC_ELS_NORMAL FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 2)
56
57
58#define PWM_IMX_TPM_MOD_WIDTH 16
59#define PWM_IMX_TPM_MOD_MOD GENMASK(PWM_IMX_TPM_MOD_WIDTH - 1, 0)
60
61struct imx_tpm_pwm_chip {
62 struct pwm_chip chip;
63 struct clk *clk;
64 void __iomem *base;
65 struct mutex lock;
66 u32 user_count;
67 u32 enable_count;
68 u32 real_period;
69};
70
71struct imx_tpm_pwm_param {
72 u8 prescale;
73 u32 mod;
74 u32 val;
75};
76
77static inline struct imx_tpm_pwm_chip *
78to_imx_tpm_pwm_chip(struct pwm_chip *chip)
79{
80 return container_of(chip, struct imx_tpm_pwm_chip, chip);
81}
82
83/*
84 * This function determines for a given pwm_state *state that a consumer
85 * might request the pwm_state *real_state that eventually is implemented
86 * by the hardware and the necessary register values (in *p) to achieve
87 * this.
88 */
89static int pwm_imx_tpm_round_state(struct pwm_chip *chip,
90 struct imx_tpm_pwm_param *p,
91 struct pwm_state *real_state,
92 const struct pwm_state *state)
93{
94 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
95 u32 rate, prescale, period_count, clock_unit;
96 u64 tmp;
97
98 rate = clk_get_rate(tpm->clk);
99 tmp = (u64)state->period * rate;
100 clock_unit = DIV_ROUND_CLOSEST_ULL(tmp, NSEC_PER_SEC);
101 if (clock_unit <= PWM_IMX_TPM_MOD_MOD)
102 prescale = 0;
103 else
104 prescale = ilog2(clock_unit) + 1 - PWM_IMX_TPM_MOD_WIDTH;
105
106 if ((!FIELD_FIT(PWM_IMX_TPM_SC_PS, prescale)))
107 return -ERANGE;
108 p->prescale = prescale;
109
110 period_count = (clock_unit + ((1 << prescale) >> 1)) >> prescale;
111 if (period_count == 0)
112 return -EINVAL;
113 p->mod = period_count - 1;
114
115 /* calculate real period HW can support */
116 tmp = (u64)period_count << prescale;
117 tmp *= NSEC_PER_SEC;
118 real_state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate);
119
120 /*
121 * if eventually the PWM output is inactive, either
122 * duty cycle is 0 or status is disabled, need to
123 * make sure the output pin is inactive.
124 */
125 if (!state->enabled)
126 real_state->duty_cycle = 0;
127 else
128 real_state->duty_cycle = state->duty_cycle;
129
130 tmp = (u64)p->mod * real_state->duty_cycle;
131 p->val = DIV64_U64_ROUND_CLOSEST(tmp, real_state->period);
132
133 real_state->polarity = state->polarity;
134 real_state->enabled = state->enabled;
135
136 return 0;
137}
138
139static void pwm_imx_tpm_get_state(struct pwm_chip *chip,
140 struct pwm_device *pwm,
141 struct pwm_state *state)
142{
143 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
144 u32 rate, val, prescale;
145 u64 tmp;
146
147 /* get period */
148 state->period = tpm->real_period;
149
150 /* get duty cycle */
151 rate = clk_get_rate(tpm->clk);
152 val = readl(tpm->base + PWM_IMX_TPM_SC);
153 prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
154 tmp = readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
155 tmp = (tmp << prescale) * NSEC_PER_SEC;
156 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, rate);
157
158 /* get polarity */
159 val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
160 if ((val & PWM_IMX_TPM_CnSC_ELS) == PWM_IMX_TPM_CnSC_ELS_INVERSED)
161 state->polarity = PWM_POLARITY_INVERSED;
162 else
163 /*
164 * Assume reserved values (2b00 and 2b11) to yield
165 * normal polarity.
166 */
167 state->polarity = PWM_POLARITY_NORMAL;
168
169 /* get channel status */
170 state->enabled = FIELD_GET(PWM_IMX_TPM_CnSC_ELS, val) ? true : false;
171}
172
173/* this function is supposed to be called with mutex hold */
174static int pwm_imx_tpm_apply_hw(struct pwm_chip *chip,
175 struct imx_tpm_pwm_param *p,
176 struct pwm_state *state,
177 struct pwm_device *pwm)
178{
179 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
180 bool period_update = false;
181 bool duty_update = false;
182 u32 val, cmod, cur_prescale;
183 unsigned long timeout;
184 struct pwm_state c;
185
186 if (state->period != tpm->real_period) {
187 /*
188 * TPM counter is shared by multiple channels, so
189 * prescale and period can NOT be modified when
190 * there are multiple channels in use with different
191 * period settings.
192 */
193 if (tpm->user_count > 1)
194 return -EBUSY;
195
196 val = readl(tpm->base + PWM_IMX_TPM_SC);
197 cmod = FIELD_GET(PWM_IMX_TPM_SC_CMOD, val);
198 cur_prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
199 if (cmod && cur_prescale != p->prescale)
200 return -EBUSY;
201
202 /* set TPM counter prescale */
203 val &= ~PWM_IMX_TPM_SC_PS;
204 val |= FIELD_PREP(PWM_IMX_TPM_SC_PS, p->prescale);
205 writel(val, tpm->base + PWM_IMX_TPM_SC);
206
207 /*
208 * set period count:
209 * if the PWM is disabled (CMOD[1:0] = 2b00), then MOD register
210 * is updated when MOD register is written.
211 *
212 * if the PWM is enabled (CMOD[1:0] ≠ 2b00), the period length
213 * is latched into hardware when the next period starts.
214 */
215 writel(p->mod, tpm->base + PWM_IMX_TPM_MOD);
216 tpm->real_period = state->period;
217 period_update = true;
218 }
219
220 pwm_imx_tpm_get_state(chip, pwm, &c);
221
222 /* polarity is NOT allowed to be changed if PWM is active */
223 if (c.enabled && c.polarity != state->polarity)
224 return -EBUSY;
225
226 if (state->duty_cycle != c.duty_cycle) {
227 /*
228 * set channel value:
229 * if the PWM is disabled (CMOD[1:0] = 2b00), then CnV register
230 * is updated when CnV register is written.
231 *
232 * if the PWM is enabled (CMOD[1:0] ≠ 2b00), the duty length
233 * is latched into hardware when the next period starts.
234 */
235 writel(p->val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
236 duty_update = true;
237 }
238
239 /* make sure MOD & CnV registers are updated */
240 if (period_update || duty_update) {
241 timeout = jiffies + msecs_to_jiffies(tpm->real_period /
242 NSEC_PER_MSEC + 1);
243 while (readl(tpm->base + PWM_IMX_TPM_MOD) != p->mod
244 || readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm))
245 != p->val) {
246 if (time_after(jiffies, timeout))
247 return -ETIME;
248 cpu_relax();
249 }
250 }
251
252 /*
253 * polarity settings will enabled/disable output status
254 * immediately, so if the channel is disabled, need to
255 * make sure MSA/MSB/ELS are set to 0 which means channel
256 * disabled.
257 */
258 val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
259 val &= ~(PWM_IMX_TPM_CnSC_ELS | PWM_IMX_TPM_CnSC_MSA |
260 PWM_IMX_TPM_CnSC_MSB);
261 if (state->enabled) {
262 /*
263 * set polarity (for edge-aligned PWM modes)
264 *
265 * ELS[1:0] = 2b10 yields normal polarity behaviour,
266 * ELS[1:0] = 2b01 yields inversed polarity.
267 * The other values are reserved.
268 */
269 val |= PWM_IMX_TPM_CnSC_MSB;
270 val |= (state->polarity == PWM_POLARITY_NORMAL) ?
271 PWM_IMX_TPM_CnSC_ELS_NORMAL :
272 PWM_IMX_TPM_CnSC_ELS_INVERSED;
273 }
274 writel(val, tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
275
276 /* control the counter status */
277 if (state->enabled != c.enabled) {
278 val = readl(tpm->base + PWM_IMX_TPM_SC);
279 if (state->enabled) {
280 if (++tpm->enable_count == 1)
281 val |= PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK;
282 } else {
283 if (--tpm->enable_count == 0)
284 val &= ~PWM_IMX_TPM_SC_CMOD;
285 }
286 writel(val, tpm->base + PWM_IMX_TPM_SC);
287 }
288
289 return 0;
290}
291
292static int pwm_imx_tpm_apply(struct pwm_chip *chip,
293 struct pwm_device *pwm,
294 const struct pwm_state *state)
295{
296 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
297 struct imx_tpm_pwm_param param;
298 struct pwm_state real_state;
299 int ret;
300
301 ret = pwm_imx_tpm_round_state(chip, &param, &real_state, state);
302 if (ret)
303 return ret;
304
305 mutex_lock(&tpm->lock);
306 ret = pwm_imx_tpm_apply_hw(chip, &param, &real_state, pwm);
307 mutex_unlock(&tpm->lock);
308
309 return ret;
310}
311
312static int pwm_imx_tpm_request(struct pwm_chip *chip, struct pwm_device *pwm)
313{
314 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
315
316 mutex_lock(&tpm->lock);
317 tpm->user_count++;
318 mutex_unlock(&tpm->lock);
319
320 return 0;
321}
322
323static void pwm_imx_tpm_free(struct pwm_chip *chip, struct pwm_device *pwm)
324{
325 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
326
327 mutex_lock(&tpm->lock);
328 tpm->user_count--;
329 mutex_unlock(&tpm->lock);
330}
331
332static const struct pwm_ops imx_tpm_pwm_ops = {
333 .request = pwm_imx_tpm_request,
334 .free = pwm_imx_tpm_free,
335 .get_state = pwm_imx_tpm_get_state,
336 .apply = pwm_imx_tpm_apply,
337 .owner = THIS_MODULE,
338};
339
340static int pwm_imx_tpm_probe(struct platform_device *pdev)
341{
342 struct imx_tpm_pwm_chip *tpm;
343 int ret;
344 u32 val;
345
346 tpm = devm_kzalloc(&pdev->dev, sizeof(*tpm), GFP_KERNEL);
347 if (!tpm)
348 return -ENOMEM;
349
350 platform_set_drvdata(pdev, tpm);
351
352 tpm->base = devm_platform_ioremap_resource(pdev, 0);
353 if (IS_ERR(tpm->base))
354 return PTR_ERR(tpm->base);
355
356 tpm->clk = devm_clk_get(&pdev->dev, NULL);
357 if (IS_ERR(tpm->clk)) {
358 ret = PTR_ERR(tpm->clk);
359 if (ret != -EPROBE_DEFER)
360 dev_err(&pdev->dev,
361 "failed to get PWM clock: %d\n", ret);
362 return ret;
363 }
364
365 ret = clk_prepare_enable(tpm->clk);
366 if (ret) {
367 dev_err(&pdev->dev,
368 "failed to prepare or enable clock: %d\n", ret);
369 return ret;
370 }
371
372 tpm->chip.dev = &pdev->dev;
373 tpm->chip.ops = &imx_tpm_pwm_ops;
374 tpm->chip.base = -1;
375 tpm->chip.of_xlate = of_pwm_xlate_with_flags;
376 tpm->chip.of_pwm_n_cells = 3;
377
378 /* get number of channels */
379 val = readl(tpm->base + PWM_IMX_TPM_PARAM);
380 tpm->chip.npwm = FIELD_GET(PWM_IMX_TPM_PARAM_CHAN, val);
381
382 mutex_init(&tpm->lock);
383
384 ret = pwmchip_add(&tpm->chip);
385 if (ret) {
386 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
387 clk_disable_unprepare(tpm->clk);
388 }
389
390 return ret;
391}
392
393static int pwm_imx_tpm_remove(struct platform_device *pdev)
394{
395 struct imx_tpm_pwm_chip *tpm = platform_get_drvdata(pdev);
396 int ret = pwmchip_remove(&tpm->chip);
397
398 clk_disable_unprepare(tpm->clk);
399
400 return ret;
401}
402
403static int __maybe_unused pwm_imx_tpm_suspend(struct device *dev)
404{
405 struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev);
406
407 if (tpm->enable_count > 0)
408 return -EBUSY;
409
410 /*
411 * Force 'real_period' to be zero to force period update code
412 * can be executed after system resume back, since suspend causes
413 * the period related registers to become their reset values.
414 */
415 tpm->real_period = 0;
416
417 clk_disable_unprepare(tpm->clk);
418
419 return 0;
420}
421
422static int __maybe_unused pwm_imx_tpm_resume(struct device *dev)
423{
424 struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev);
425 int ret = 0;
426
427 ret = clk_prepare_enable(tpm->clk);
428 if (ret)
429 dev_err(dev,
430 "failed to prepare or enable clock: %d\n",
431 ret);
432
433 return ret;
434}
435
436static SIMPLE_DEV_PM_OPS(imx_tpm_pwm_pm,
437 pwm_imx_tpm_suspend, pwm_imx_tpm_resume);
438
439static const struct of_device_id imx_tpm_pwm_dt_ids[] = {
440 { .compatible = "fsl,imx7ulp-pwm", },
441 { /* sentinel */ }
442};
443MODULE_DEVICE_TABLE(of, imx_tpm_pwm_dt_ids);
444
445static struct platform_driver imx_tpm_pwm_driver = {
446 .driver = {
447 .name = "imx7ulp-tpm-pwm",
448 .of_match_table = imx_tpm_pwm_dt_ids,
449 .pm = &imx_tpm_pwm_pm,
450 },
451 .probe = pwm_imx_tpm_probe,
452 .remove = pwm_imx_tpm_remove,
453};
454module_platform_driver(imx_tpm_pwm_driver);
455
456MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
457MODULE_DESCRIPTION("i.MX TPM PWM Driver");
458MODULE_LICENSE("GPL v2");