1 // SPDX-License-Identifier: GPL-2.0
3 * STM32 Timer Encoder and Counter driver
5 * Copyright (C) STMicroelectronics 2018
10 #include <linux/counter.h>
11 #include <linux/mfd/stm32-timers.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/pinctrl/consumer.h>
15 #include <linux/platform_device.h>
16 #include <linux/types.h>
18 #define TIM_CCMR_CCXS (BIT(8) | BIT(0))
19 #define TIM_CCMR_MASK (TIM_CCMR_CC1S | TIM_CCMR_CC2S | \
20 TIM_CCMR_IC1F | TIM_CCMR_IC2F)
21 #define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \
22 TIM_CCER_CC2P | TIM_CCER_CC2NP)
24 struct stm32_timer_regs {
31 struct stm32_timer_cnt {
32 struct counter_device counter;
33 struct regmap *regmap;
37 struct stm32_timer_regs bak;
40 static const enum counter_function stm32_count_functions[] = {
41 COUNTER_FUNCTION_INCREASE,
42 COUNTER_FUNCTION_QUADRATURE_X2_A,
43 COUNTER_FUNCTION_QUADRATURE_X2_B,
44 COUNTER_FUNCTION_QUADRATURE_X4,
47 static int stm32_count_read(struct counter_device *counter,
48 struct counter_count *count, u64 *val)
50 struct stm32_timer_cnt *const priv = counter->priv;
53 regmap_read(priv->regmap, TIM_CNT, &cnt);
59 static int stm32_count_write(struct counter_device *counter,
60 struct counter_count *count, const u64 val)
62 struct stm32_timer_cnt *const priv = counter->priv;
65 regmap_read(priv->regmap, TIM_ARR, &ceiling);
69 return regmap_write(priv->regmap, TIM_CNT, val);
72 static int stm32_count_function_read(struct counter_device *counter,
73 struct counter_count *count,
74 enum counter_function *function)
76 struct stm32_timer_cnt *const priv = counter->priv;
79 regmap_read(priv->regmap, TIM_SMCR, &smcr);
81 switch (smcr & TIM_SMCR_SMS) {
82 case TIM_SMCR_SMS_SLAVE_MODE_DISABLED:
83 *function = COUNTER_FUNCTION_INCREASE;
85 case TIM_SMCR_SMS_ENCODER_MODE_1:
86 *function = COUNTER_FUNCTION_QUADRATURE_X2_A;
88 case TIM_SMCR_SMS_ENCODER_MODE_2:
89 *function = COUNTER_FUNCTION_QUADRATURE_X2_B;
91 case TIM_SMCR_SMS_ENCODER_MODE_3:
92 *function = COUNTER_FUNCTION_QUADRATURE_X4;
99 static int stm32_count_function_write(struct counter_device *counter,
100 struct counter_count *count,
101 enum counter_function function)
103 struct stm32_timer_cnt *const priv = counter->priv;
107 case COUNTER_FUNCTION_INCREASE:
108 sms = TIM_SMCR_SMS_SLAVE_MODE_DISABLED;
110 case COUNTER_FUNCTION_QUADRATURE_X2_A:
111 sms = TIM_SMCR_SMS_ENCODER_MODE_1;
113 case COUNTER_FUNCTION_QUADRATURE_X2_B:
114 sms = TIM_SMCR_SMS_ENCODER_MODE_2;
116 case COUNTER_FUNCTION_QUADRATURE_X4:
117 sms = TIM_SMCR_SMS_ENCODER_MODE_3;
123 /* Store enable status */
124 regmap_read(priv->regmap, TIM_CR1, &cr1);
126 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
128 regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
130 /* Make sure that registers are updated */
131 regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
133 /* Restore the enable status */
134 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, cr1);
139 static int stm32_count_direction_read(struct counter_device *counter,
140 struct counter_count *count,
141 enum counter_count_direction *direction)
143 struct stm32_timer_cnt *const priv = counter->priv;
146 regmap_read(priv->regmap, TIM_CR1, &cr1);
147 *direction = (cr1 & TIM_CR1_DIR) ? COUNTER_COUNT_DIRECTION_BACKWARD :
148 COUNTER_COUNT_DIRECTION_FORWARD;
153 static int stm32_count_ceiling_read(struct counter_device *counter,
154 struct counter_count *count, u64 *ceiling)
156 struct stm32_timer_cnt *const priv = counter->priv;
159 regmap_read(priv->regmap, TIM_ARR, &arr);
166 static int stm32_count_ceiling_write(struct counter_device *counter,
167 struct counter_count *count, u64 ceiling)
169 struct stm32_timer_cnt *const priv = counter->priv;
171 if (ceiling > priv->max_arr)
174 /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
175 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
176 regmap_write(priv->regmap, TIM_ARR, ceiling);
181 static int stm32_count_enable_read(struct counter_device *counter,
182 struct counter_count *count, u8 *enable)
184 struct stm32_timer_cnt *const priv = counter->priv;
187 regmap_read(priv->regmap, TIM_CR1, &cr1);
189 *enable = cr1 & TIM_CR1_CEN;
194 static int stm32_count_enable_write(struct counter_device *counter,
195 struct counter_count *count, u8 enable)
197 struct stm32_timer_cnt *const priv = counter->priv;
201 regmap_read(priv->regmap, TIM_CR1, &cr1);
202 if (!(cr1 & TIM_CR1_CEN))
203 clk_enable(priv->clk);
205 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
208 regmap_read(priv->regmap, TIM_CR1, &cr1);
209 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
210 if (cr1 & TIM_CR1_CEN)
211 clk_disable(priv->clk);
214 /* Keep enabled state to properly handle low power states */
215 priv->enabled = enable;
220 static struct counter_comp stm32_count_ext[] = {
221 COUNTER_COMP_DIRECTION(stm32_count_direction_read),
222 COUNTER_COMP_ENABLE(stm32_count_enable_read, stm32_count_enable_write),
223 COUNTER_COMP_CEILING(stm32_count_ceiling_read,
224 stm32_count_ceiling_write),
227 static const enum counter_synapse_action stm32_synapse_actions[] = {
228 COUNTER_SYNAPSE_ACTION_NONE,
229 COUNTER_SYNAPSE_ACTION_BOTH_EDGES
232 static int stm32_action_read(struct counter_device *counter,
233 struct counter_count *count,
234 struct counter_synapse *synapse,
235 enum counter_synapse_action *action)
237 enum counter_function function;
240 err = stm32_count_function_read(counter, count, &function);
245 case COUNTER_FUNCTION_INCREASE:
246 /* counts on internal clock when CEN=1 */
247 *action = COUNTER_SYNAPSE_ACTION_NONE;
249 case COUNTER_FUNCTION_QUADRATURE_X2_A:
250 /* counts up/down on TI1FP1 edge depending on TI2FP2 level */
251 if (synapse->signal->id == count->synapses[0].signal->id)
252 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
254 *action = COUNTER_SYNAPSE_ACTION_NONE;
256 case COUNTER_FUNCTION_QUADRATURE_X2_B:
257 /* counts up/down on TI2FP2 edge depending on TI1FP1 level */
258 if (synapse->signal->id == count->synapses[1].signal->id)
259 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
261 *action = COUNTER_SYNAPSE_ACTION_NONE;
263 case COUNTER_FUNCTION_QUADRATURE_X4:
264 /* counts up/down on both TI1FP1 and TI2FP2 edges */
265 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
272 static const struct counter_ops stm32_timer_cnt_ops = {
273 .count_read = stm32_count_read,
274 .count_write = stm32_count_write,
275 .function_read = stm32_count_function_read,
276 .function_write = stm32_count_function_write,
277 .action_read = stm32_action_read,
280 static struct counter_signal stm32_signals[] = {
283 .name = "Channel 1 Quadrature A"
287 .name = "Channel 1 Quadrature B"
291 static struct counter_synapse stm32_count_synapses[] = {
293 .actions_list = stm32_synapse_actions,
294 .num_actions = ARRAY_SIZE(stm32_synapse_actions),
295 .signal = &stm32_signals[0]
298 .actions_list = stm32_synapse_actions,
299 .num_actions = ARRAY_SIZE(stm32_synapse_actions),
300 .signal = &stm32_signals[1]
304 static struct counter_count stm32_counts = {
306 .name = "Channel 1 Count",
307 .functions_list = stm32_count_functions,
308 .num_functions = ARRAY_SIZE(stm32_count_functions),
309 .synapses = stm32_count_synapses,
310 .num_synapses = ARRAY_SIZE(stm32_count_synapses),
311 .ext = stm32_count_ext,
312 .num_ext = ARRAY_SIZE(stm32_count_ext)
315 static int stm32_timer_cnt_probe(struct platform_device *pdev)
317 struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
318 struct device *dev = &pdev->dev;
319 struct stm32_timer_cnt *priv;
321 if (IS_ERR_OR_NULL(ddata))
324 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
328 priv->regmap = ddata->regmap;
329 priv->clk = ddata->clk;
330 priv->max_arr = ddata->max_arr;
332 priv->counter.name = dev_name(dev);
333 priv->counter.parent = dev;
334 priv->counter.ops = &stm32_timer_cnt_ops;
335 priv->counter.counts = &stm32_counts;
336 priv->counter.num_counts = 1;
337 priv->counter.signals = stm32_signals;
338 priv->counter.num_signals = ARRAY_SIZE(stm32_signals);
339 priv->counter.priv = priv;
341 platform_set_drvdata(pdev, priv);
343 /* Register Counter device */
344 return devm_counter_register(dev, &priv->counter);
347 static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev)
349 struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
351 /* Only take care of enabled counter: don't disturb other MFD child */
353 /* Backup registers that may get lost in low power mode */
354 regmap_read(priv->regmap, TIM_SMCR, &priv->bak.smcr);
355 regmap_read(priv->regmap, TIM_ARR, &priv->bak.arr);
356 regmap_read(priv->regmap, TIM_CNT, &priv->bak.cnt);
357 regmap_read(priv->regmap, TIM_CR1, &priv->bak.cr1);
359 /* Disable the counter */
360 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
361 clk_disable(priv->clk);
364 return pinctrl_pm_select_sleep_state(dev);
367 static int __maybe_unused stm32_timer_cnt_resume(struct device *dev)
369 struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
372 ret = pinctrl_pm_select_default_state(dev);
377 clk_enable(priv->clk);
379 /* Restore registers that may have been lost */
380 regmap_write(priv->regmap, TIM_SMCR, priv->bak.smcr);
381 regmap_write(priv->regmap, TIM_ARR, priv->bak.arr);
382 regmap_write(priv->regmap, TIM_CNT, priv->bak.cnt);
384 /* Also re-enables the counter */
385 regmap_write(priv->regmap, TIM_CR1, priv->bak.cr1);
391 static SIMPLE_DEV_PM_OPS(stm32_timer_cnt_pm_ops, stm32_timer_cnt_suspend,
392 stm32_timer_cnt_resume);
394 static const struct of_device_id stm32_timer_cnt_of_match[] = {
395 { .compatible = "st,stm32-timer-counter", },
398 MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match);
400 static struct platform_driver stm32_timer_cnt_driver = {
401 .probe = stm32_timer_cnt_probe,
403 .name = "stm32-timer-counter",
404 .of_match_table = stm32_timer_cnt_of_match,
405 .pm = &stm32_timer_cnt_pm_ops,
408 module_platform_driver(stm32_timer_cnt_driver);
411 MODULE_ALIAS("platform:stm32-timer-counter");
412 MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver");
413 MODULE_LICENSE("GPL v2");