1 // SPDX-License-Identifier: GPL-2.0
3 * STM32 Low-Power Timer PWM driver
5 * Copyright (C) STMicroelectronics 2017
7 * Author: Gerald Baeza <gerald.baeza@st.com>
9 * Inspired by Gerald Baeza's pwm-stm32 driver
12 #include <linux/bitfield.h>
13 #include <linux/mfd/stm32-lptimer.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/pwm.h>
22 struct regmap
*regmap
;
25 static inline struct stm32_pwm_lp
*to_stm32_pwm_lp(struct pwm_chip
*chip
)
27 return container_of(chip
, struct stm32_pwm_lp
, chip
);
30 /* STM32 Low-Power Timer is preceded by a configurable power-of-2 prescaler */
31 #define STM32_LPTIM_MAX_PRESCALER 128
33 static int stm32_pwm_lp_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
34 struct pwm_state
*state
)
36 struct stm32_pwm_lp
*priv
= to_stm32_pwm_lp(chip
);
37 unsigned long long prd
, div
, dty
;
38 struct pwm_state cstate
;
39 u32 val
, mask
, cfgr
, presc
= 0;
43 pwm_get_state(pwm
, &cstate
);
44 reenable
= !cstate
.enabled
;
46 if (!state
->enabled
) {
48 /* Disable LP timer */
49 ret
= regmap_write(priv
->regmap
, STM32_LPTIM_CR
, 0);
52 /* disable clock to PWM counter */
53 clk_disable(priv
->clk
);
58 /* Calculate the period and prescaler value */
59 div
= (unsigned long long)clk_get_rate(priv
->clk
) * state
->period
;
60 do_div(div
, NSEC_PER_SEC
);
62 /* Clock is too slow to achieve requested period. */
63 dev_dbg(priv
->chip
.dev
, "Can't reach %u ns\n", state
->period
);
68 while (div
> STM32_LPTIM_MAX_ARR
) {
70 if ((1 << presc
) > STM32_LPTIM_MAX_PRESCALER
) {
71 dev_err(priv
->chip
.dev
, "max prescaler exceeded\n");
78 /* Calculate the duty cycle */
79 dty
= prd
* state
->duty_cycle
;
80 do_div(dty
, state
->period
);
82 if (!cstate
.enabled
) {
83 /* enable clock to drive PWM counter */
84 ret
= clk_enable(priv
->clk
);
89 ret
= regmap_read(priv
->regmap
, STM32_LPTIM_CFGR
, &cfgr
);
93 if ((FIELD_GET(STM32_LPTIM_PRESC
, cfgr
) != presc
) ||
94 (FIELD_GET(STM32_LPTIM_WAVPOL
, cfgr
) != state
->polarity
)) {
95 val
= FIELD_PREP(STM32_LPTIM_PRESC
, presc
);
96 val
|= FIELD_PREP(STM32_LPTIM_WAVPOL
, state
->polarity
);
97 mask
= STM32_LPTIM_PRESC
| STM32_LPTIM_WAVPOL
;
99 /* Must disable LP timer to modify CFGR */
101 ret
= regmap_write(priv
->regmap
, STM32_LPTIM_CR
, 0);
105 ret
= regmap_update_bits(priv
->regmap
, STM32_LPTIM_CFGR
, mask
,
112 /* Must (re)enable LP timer to modify CMP & ARR */
113 ret
= regmap_write(priv
->regmap
, STM32_LPTIM_CR
,
119 ret
= regmap_write(priv
->regmap
, STM32_LPTIM_ARR
, prd
- 1);
123 ret
= regmap_write(priv
->regmap
, STM32_LPTIM_CMP
, prd
- (1 + dty
));
127 /* ensure CMP & ARR registers are properly written */
128 ret
= regmap_read_poll_timeout(priv
->regmap
, STM32_LPTIM_ISR
, val
,
129 (val
& STM32_LPTIM_CMPOK_ARROK
),
132 dev_err(priv
->chip
.dev
, "ARR/CMP registers write issue\n");
135 ret
= regmap_write(priv
->regmap
, STM32_LPTIM_ICR
,
136 STM32_LPTIM_CMPOKCF_ARROKCF
);
141 /* Start LP timer in continuous mode */
142 ret
= regmap_update_bits(priv
->regmap
, STM32_LPTIM_CR
,
144 STM32_LPTIM_CNTSTRT
);
146 regmap_write(priv
->regmap
, STM32_LPTIM_CR
, 0);
154 clk_disable(priv
->clk
);
159 static void stm32_pwm_lp_get_state(struct pwm_chip
*chip
,
160 struct pwm_device
*pwm
,
161 struct pwm_state
*state
)
163 struct stm32_pwm_lp
*priv
= to_stm32_pwm_lp(chip
);
164 unsigned long rate
= clk_get_rate(priv
->clk
);
168 regmap_read(priv
->regmap
, STM32_LPTIM_CR
, &val
);
169 state
->enabled
= !!FIELD_GET(STM32_LPTIM_ENABLE
, val
);
170 /* Keep PWM counter clock refcount in sync with PWM initial state */
172 clk_enable(priv
->clk
);
174 regmap_read(priv
->regmap
, STM32_LPTIM_CFGR
, &val
);
175 presc
= FIELD_GET(STM32_LPTIM_PRESC
, val
);
176 state
->polarity
= FIELD_GET(STM32_LPTIM_WAVPOL
, val
);
178 regmap_read(priv
->regmap
, STM32_LPTIM_ARR
, &prd
);
180 tmp
= (tmp
<< presc
) * NSEC_PER_SEC
;
181 state
->period
= DIV_ROUND_CLOSEST_ULL(tmp
, rate
);
183 regmap_read(priv
->regmap
, STM32_LPTIM_CMP
, &val
);
185 tmp
= (tmp
<< presc
) * NSEC_PER_SEC
;
186 state
->duty_cycle
= DIV_ROUND_CLOSEST_ULL(tmp
, rate
);
189 static const struct pwm_ops stm32_pwm_lp_ops
= {
190 .owner
= THIS_MODULE
,
191 .apply
= stm32_pwm_lp_apply
,
192 .get_state
= stm32_pwm_lp_get_state
,
195 static int stm32_pwm_lp_probe(struct platform_device
*pdev
)
197 struct stm32_lptimer
*ddata
= dev_get_drvdata(pdev
->dev
.parent
);
198 struct stm32_pwm_lp
*priv
;
201 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
205 priv
->regmap
= ddata
->regmap
;
206 priv
->clk
= ddata
->clk
;
207 priv
->chip
.base
= -1;
208 priv
->chip
.dev
= &pdev
->dev
;
209 priv
->chip
.ops
= &stm32_pwm_lp_ops
;
211 priv
->chip
.of_xlate
= of_pwm_xlate_with_flags
;
212 priv
->chip
.of_pwm_n_cells
= 3;
214 ret
= pwmchip_add(&priv
->chip
);
218 platform_set_drvdata(pdev
, priv
);
223 static int stm32_pwm_lp_remove(struct platform_device
*pdev
)
225 struct stm32_pwm_lp
*priv
= platform_get_drvdata(pdev
);
227 pwm_disable(&priv
->chip
.pwms
[0]);
229 return pwmchip_remove(&priv
->chip
);
232 static const struct of_device_id stm32_pwm_lp_of_match
[] = {
233 { .compatible
= "st,stm32-pwm-lp", },
236 MODULE_DEVICE_TABLE(of
, stm32_pwm_lp_of_match
);
238 static struct platform_driver stm32_pwm_lp_driver
= {
239 .probe
= stm32_pwm_lp_probe
,
240 .remove
= stm32_pwm_lp_remove
,
242 .name
= "stm32-pwm-lp",
243 .of_match_table
= of_match_ptr(stm32_pwm_lp_of_match
),
246 module_platform_driver(stm32_pwm_lp_driver
);
248 MODULE_ALIAS("platform:stm32-pwm-lp");
249 MODULE_DESCRIPTION("STMicroelectronics STM32 PWM LP driver");
250 MODULE_LICENSE("GPL v2");