1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2018-2019 NXP.
6 * - The TPM counter and period counter are shared between
7 * multiple channels, so all channels should use same period
9 * - Changes to polarity cannot be latched at the time of the
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.
16 #include <linux/bitfield.h>
17 #include <linux/bitops.h>
18 #include <linux/clk.h>
19 #include <linux/err.h>
21 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/pwm.h>
25 #include <linux/slab.h>
27 #define PWM_IMX_TPM_PARAM 0x4
28 #define PWM_IMX_TPM_GLOBAL 0x8
29 #define PWM_IMX_TPM_SC 0x10
30 #define PWM_IMX_TPM_CNT 0x14
31 #define PWM_IMX_TPM_MOD 0x18
32 #define PWM_IMX_TPM_CnSC(n) (0x20 + (n) * 0x8)
33 #define PWM_IMX_TPM_CnV(n) (0x24 + (n) * 0x8)
35 #define PWM_IMX_TPM_PARAM_CHAN GENMASK(7, 0)
37 #define PWM_IMX_TPM_SC_PS GENMASK(2, 0)
38 #define PWM_IMX_TPM_SC_CMOD GENMASK(4, 3)
39 #define PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK FIELD_PREP(PWM_IMX_TPM_SC_CMOD, 1)
40 #define PWM_IMX_TPM_SC_CPWMS BIT(5)
42 #define PWM_IMX_TPM_CnSC_CHF BIT(7)
43 #define PWM_IMX_TPM_CnSC_MSB BIT(5)
44 #define PWM_IMX_TPM_CnSC_MSA BIT(4)
47 * The reference manual describes this field as two separate bits. The
48 * semantic of the two bits isn't orthogonal though, so they are treated
49 * together as a 2-bit field here.
51 #define PWM_IMX_TPM_CnSC_ELS GENMASK(3, 2)
52 #define PWM_IMX_TPM_CnSC_ELS_INVERSED FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 1)
53 #define PWM_IMX_TPM_CnSC_ELS_NORMAL FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 2)
56 #define PWM_IMX_TPM_MOD_WIDTH 16
57 #define PWM_IMX_TPM_MOD_MOD GENMASK(PWM_IMX_TPM_MOD_WIDTH - 1, 0)
59 struct imx_tpm_pwm_chip {
68 struct imx_tpm_pwm_param {
74 static inline struct imx_tpm_pwm_chip *
75 to_imx_tpm_pwm_chip(struct pwm_chip *chip)
77 return pwmchip_get_drvdata(chip);
81 * This function determines for a given pwm_state *state that a consumer
82 * might request the pwm_state *real_state that eventually is implemented
83 * by the hardware and the necessary register values (in *p) to achieve
86 static int pwm_imx_tpm_round_state(struct pwm_chip *chip,
87 struct imx_tpm_pwm_param *p,
88 struct pwm_state *real_state,
89 const struct pwm_state *state)
91 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
92 u32 rate, prescale, period_count, clock_unit;
95 rate = clk_get_rate(tpm->clk);
96 tmp = (u64)state->period * rate;
97 clock_unit = DIV_ROUND_CLOSEST_ULL(tmp, NSEC_PER_SEC);
98 if (clock_unit <= PWM_IMX_TPM_MOD_MOD)
101 prescale = ilog2(clock_unit) + 1 - PWM_IMX_TPM_MOD_WIDTH;
103 if ((!FIELD_FIT(PWM_IMX_TPM_SC_PS, prescale)))
105 p->prescale = prescale;
107 period_count = (clock_unit + ((1 << prescale) >> 1)) >> prescale;
108 p->mod = period_count;
110 /* calculate real period HW can support */
111 tmp = (u64)period_count << prescale;
113 real_state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate);
116 * if eventually the PWM output is inactive, either
117 * duty cycle is 0 or status is disabled, need to
118 * make sure the output pin is inactive.
121 real_state->duty_cycle = 0;
123 real_state->duty_cycle = state->duty_cycle;
125 tmp = (u64)p->mod * real_state->duty_cycle;
126 p->val = DIV64_U64_ROUND_CLOSEST(tmp, real_state->period);
128 real_state->polarity = state->polarity;
129 real_state->enabled = state->enabled;
134 static int pwm_imx_tpm_get_state(struct pwm_chip *chip,
135 struct pwm_device *pwm,
136 struct pwm_state *state)
138 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
139 u32 rate, val, prescale;
143 state->period = tpm->real_period;
146 rate = clk_get_rate(tpm->clk);
147 val = readl(tpm->base + PWM_IMX_TPM_SC);
148 prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
149 tmp = readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
150 tmp = (tmp << prescale) * NSEC_PER_SEC;
151 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, rate);
154 val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
155 if ((val & PWM_IMX_TPM_CnSC_ELS) == PWM_IMX_TPM_CnSC_ELS_INVERSED)
156 state->polarity = PWM_POLARITY_INVERSED;
159 * Assume reserved values (2b00 and 2b11) to yield
162 state->polarity = PWM_POLARITY_NORMAL;
164 /* get channel status */
165 state->enabled = FIELD_GET(PWM_IMX_TPM_CnSC_ELS, val) ? true : false;
170 /* this function is supposed to be called with mutex hold */
171 static int pwm_imx_tpm_apply_hw(struct pwm_chip *chip,
172 struct imx_tpm_pwm_param *p,
173 struct pwm_state *state,
174 struct pwm_device *pwm)
176 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
177 bool period_update = false;
178 bool duty_update = false;
179 u32 val, cmod, cur_prescale;
180 unsigned long timeout;
183 if (state->period != tpm->real_period) {
185 * TPM counter is shared by multiple channels, so
186 * prescale and period can NOT be modified when
187 * there are multiple channels in use with different
190 if (tpm->user_count > 1)
193 val = readl(tpm->base + PWM_IMX_TPM_SC);
194 cmod = FIELD_GET(PWM_IMX_TPM_SC_CMOD, val);
195 cur_prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
196 if (cmod && cur_prescale != p->prescale)
199 /* set TPM counter prescale */
200 val &= ~PWM_IMX_TPM_SC_PS;
201 val |= FIELD_PREP(PWM_IMX_TPM_SC_PS, p->prescale);
202 writel(val, tpm->base + PWM_IMX_TPM_SC);
206 * if the PWM is disabled (CMOD[1:0] = 2b00), then MOD register
207 * is updated when MOD register is written.
209 * if the PWM is enabled (CMOD[1:0] ≠ 2b00), the period length
210 * is latched into hardware when the next period starts.
212 writel(p->mod, tpm->base + PWM_IMX_TPM_MOD);
213 tpm->real_period = state->period;
214 period_update = true;
217 pwm_imx_tpm_get_state(chip, pwm, &c);
219 /* polarity is NOT allowed to be changed if PWM is active */
220 if (c.enabled && c.polarity != state->polarity)
223 if (state->duty_cycle != c.duty_cycle) {
226 * if the PWM is disabled (CMOD[1:0] = 2b00), then CnV register
227 * is updated when CnV register is written.
229 * if the PWM is enabled (CMOD[1:0] ≠ 2b00), the duty length
230 * is latched into hardware when the next period starts.
232 writel(p->val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
236 /* make sure MOD & CnV registers are updated */
237 if (period_update || duty_update) {
238 timeout = jiffies + msecs_to_jiffies(tpm->real_period /
240 while (readl(tpm->base + PWM_IMX_TPM_MOD) != p->mod
241 || readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm))
243 if (time_after(jiffies, timeout))
250 * polarity settings will enabled/disable output status
251 * immediately, so if the channel is disabled, need to
252 * make sure MSA/MSB/ELS are set to 0 which means channel
255 val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
256 val &= ~(PWM_IMX_TPM_CnSC_ELS | PWM_IMX_TPM_CnSC_MSA |
257 PWM_IMX_TPM_CnSC_MSB);
258 if (state->enabled) {
260 * set polarity (for edge-aligned PWM modes)
262 * ELS[1:0] = 2b10 yields normal polarity behaviour,
263 * ELS[1:0] = 2b01 yields inversed polarity.
264 * The other values are reserved.
266 val |= PWM_IMX_TPM_CnSC_MSB;
267 val |= (state->polarity == PWM_POLARITY_NORMAL) ?
268 PWM_IMX_TPM_CnSC_ELS_NORMAL :
269 PWM_IMX_TPM_CnSC_ELS_INVERSED;
271 writel(val, tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
273 /* control the counter status */
274 if (state->enabled != c.enabled) {
275 val = readl(tpm->base + PWM_IMX_TPM_SC);
276 if (state->enabled) {
277 if (++tpm->enable_count == 1)
278 val |= PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK;
280 if (--tpm->enable_count == 0)
281 val &= ~PWM_IMX_TPM_SC_CMOD;
283 writel(val, tpm->base + PWM_IMX_TPM_SC);
289 static int pwm_imx_tpm_apply(struct pwm_chip *chip,
290 struct pwm_device *pwm,
291 const struct pwm_state *state)
293 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
294 struct imx_tpm_pwm_param param;
295 struct pwm_state real_state;
298 ret = pwm_imx_tpm_round_state(chip, ¶m, &real_state, state);
302 mutex_lock(&tpm->lock);
303 ret = pwm_imx_tpm_apply_hw(chip, ¶m, &real_state, pwm);
304 mutex_unlock(&tpm->lock);
309 static int pwm_imx_tpm_request(struct pwm_chip *chip, struct pwm_device *pwm)
311 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
313 mutex_lock(&tpm->lock);
315 mutex_unlock(&tpm->lock);
320 static void pwm_imx_tpm_free(struct pwm_chip *chip, struct pwm_device *pwm)
322 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
324 mutex_lock(&tpm->lock);
326 mutex_unlock(&tpm->lock);
329 static const struct pwm_ops imx_tpm_pwm_ops = {
330 .request = pwm_imx_tpm_request,
331 .free = pwm_imx_tpm_free,
332 .get_state = pwm_imx_tpm_get_state,
333 .apply = pwm_imx_tpm_apply,
336 static int pwm_imx_tpm_probe(struct platform_device *pdev)
338 struct pwm_chip *chip;
339 struct imx_tpm_pwm_chip *tpm;
346 base = devm_platform_ioremap_resource(pdev, 0);
348 return PTR_ERR(base);
350 clk = devm_clk_get_enabled(&pdev->dev, NULL);
352 return dev_err_probe(&pdev->dev, PTR_ERR(clk),
353 "failed to get PWM clock\n");
355 /* get number of channels */
356 val = readl(base + PWM_IMX_TPM_PARAM);
357 npwm = FIELD_GET(PWM_IMX_TPM_PARAM_CHAN, val);
359 chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*tpm));
361 return PTR_ERR(chip);
362 tpm = to_imx_tpm_pwm_chip(chip);
364 platform_set_drvdata(pdev, tpm);
369 chip->ops = &imx_tpm_pwm_ops;
371 mutex_init(&tpm->lock);
373 ret = devm_pwmchip_add(&pdev->dev, chip);
375 return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n");
380 static int pwm_imx_tpm_suspend(struct device *dev)
382 struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev);
384 if (tpm->enable_count > 0)
388 * Force 'real_period' to be zero to force period update code
389 * can be executed after system resume back, since suspend causes
390 * the period related registers to become their reset values.
392 tpm->real_period = 0;
394 clk_disable_unprepare(tpm->clk);
399 static int pwm_imx_tpm_resume(struct device *dev)
401 struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev);
404 ret = clk_prepare_enable(tpm->clk);
406 dev_err(dev, "failed to prepare or enable clock: %d\n", ret);
411 static DEFINE_SIMPLE_DEV_PM_OPS(imx_tpm_pwm_pm,
412 pwm_imx_tpm_suspend, pwm_imx_tpm_resume);
414 static const struct of_device_id imx_tpm_pwm_dt_ids[] = {
415 { .compatible = "fsl,imx7ulp-pwm", },
418 MODULE_DEVICE_TABLE(of, imx_tpm_pwm_dt_ids);
420 static struct platform_driver imx_tpm_pwm_driver = {
422 .name = "imx7ulp-tpm-pwm",
423 .of_match_table = imx_tpm_pwm_dt_ids,
424 .pm = pm_ptr(&imx_tpm_pwm_pm),
426 .probe = pwm_imx_tpm_probe,
428 module_platform_driver(imx_tpm_pwm_driver);
430 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
431 MODULE_DESCRIPTION("i.MX TPM PWM Driver");
432 MODULE_LICENSE("GPL v2");