1 // SPDX-License-Identifier: GPL-2.0
3 * STM32 Timer Encoder and Counter driver
5 * Copyright (C) STMicroelectronics 2018
7 * Author: Benjamin Gaignard <benjamin.gaignard@st.com>
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>
17 #define TIM_CCMR_CCXS (BIT(8) | BIT(0))
18 #define TIM_CCMR_MASK (TIM_CCMR_CC1S | TIM_CCMR_CC2S | \
19 TIM_CCMR_IC1F | TIM_CCMR_IC2F)
20 #define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \
21 TIM_CCER_CC2P | TIM_CCER_CC2NP)
23 struct stm32_timer_regs {
30 struct stm32_timer_cnt {
31 struct counter_device counter;
32 struct regmap *regmap;
36 struct stm32_timer_regs bak;
40 * enum stm32_count_function - enumerates stm32 timer counter encoder modes
41 * @STM32_COUNT_SLAVE_MODE_DISABLED: counts on internal clock when CEN=1
42 * @STM32_COUNT_ENCODER_MODE_1: counts TI1FP1 edges, depending on TI2FP2 level
43 * @STM32_COUNT_ENCODER_MODE_2: counts TI2FP2 edges, depending on TI1FP1 level
44 * @STM32_COUNT_ENCODER_MODE_3: counts on both TI1FP1 and TI2FP2 edges
46 enum stm32_count_function {
47 STM32_COUNT_SLAVE_MODE_DISABLED,
48 STM32_COUNT_ENCODER_MODE_1,
49 STM32_COUNT_ENCODER_MODE_2,
50 STM32_COUNT_ENCODER_MODE_3,
53 static const enum counter_function stm32_count_functions[] = {
54 [STM32_COUNT_SLAVE_MODE_DISABLED] = COUNTER_FUNCTION_INCREASE,
55 [STM32_COUNT_ENCODER_MODE_1] = COUNTER_FUNCTION_QUADRATURE_X2_A,
56 [STM32_COUNT_ENCODER_MODE_2] = COUNTER_FUNCTION_QUADRATURE_X2_B,
57 [STM32_COUNT_ENCODER_MODE_3] = COUNTER_FUNCTION_QUADRATURE_X4,
60 static int stm32_count_read(struct counter_device *counter,
61 struct counter_count *count, unsigned long *val)
63 struct stm32_timer_cnt *const priv = counter->priv;
66 regmap_read(priv->regmap, TIM_CNT, &cnt);
72 static int stm32_count_write(struct counter_device *counter,
73 struct counter_count *count,
74 const unsigned long val)
76 struct stm32_timer_cnt *const priv = counter->priv;
79 regmap_read(priv->regmap, TIM_ARR, &ceiling);
83 return regmap_write(priv->regmap, TIM_CNT, val);
86 static int stm32_count_function_get(struct counter_device *counter,
87 struct counter_count *count,
90 struct stm32_timer_cnt *const priv = counter->priv;
93 regmap_read(priv->regmap, TIM_SMCR, &smcr);
95 switch (smcr & TIM_SMCR_SMS) {
97 *function = STM32_COUNT_SLAVE_MODE_DISABLED;
100 *function = STM32_COUNT_ENCODER_MODE_1;
103 *function = STM32_COUNT_ENCODER_MODE_2;
106 *function = STM32_COUNT_ENCODER_MODE_3;
113 static int stm32_count_function_set(struct counter_device *counter,
114 struct counter_count *count,
117 struct stm32_timer_cnt *const priv = counter->priv;
121 case STM32_COUNT_SLAVE_MODE_DISABLED:
124 case STM32_COUNT_ENCODER_MODE_1:
127 case STM32_COUNT_ENCODER_MODE_2:
130 case STM32_COUNT_ENCODER_MODE_3:
137 /* Store enable status */
138 regmap_read(priv->regmap, TIM_CR1, &cr1);
140 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
142 regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
144 /* Make sure that registers are updated */
145 regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
147 /* Restore the enable status */
148 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, cr1);
153 static ssize_t stm32_count_direction_read(struct counter_device *counter,
154 struct counter_count *count,
155 void *private, char *buf)
157 struct stm32_timer_cnt *const priv = counter->priv;
158 const char *direction;
161 regmap_read(priv->regmap, TIM_CR1, &cr1);
162 direction = (cr1 & TIM_CR1_DIR) ? "backward" : "forward";
164 return scnprintf(buf, PAGE_SIZE, "%s\n", direction);
167 static ssize_t stm32_count_ceiling_read(struct counter_device *counter,
168 struct counter_count *count,
169 void *private, char *buf)
171 struct stm32_timer_cnt *const priv = counter->priv;
174 regmap_read(priv->regmap, TIM_ARR, &arr);
176 return snprintf(buf, PAGE_SIZE, "%u\n", arr);
179 static ssize_t stm32_count_ceiling_write(struct counter_device *counter,
180 struct counter_count *count,
182 const char *buf, size_t len)
184 struct stm32_timer_cnt *const priv = counter->priv;
185 unsigned int ceiling;
188 ret = kstrtouint(buf, 0, &ceiling);
192 if (ceiling > priv->max_arr)
195 /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
196 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
197 regmap_write(priv->regmap, TIM_ARR, ceiling);
202 static ssize_t stm32_count_enable_read(struct counter_device *counter,
203 struct counter_count *count,
204 void *private, char *buf)
206 struct stm32_timer_cnt *const priv = counter->priv;
209 regmap_read(priv->regmap, TIM_CR1, &cr1);
211 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)(cr1 & TIM_CR1_CEN));
214 static ssize_t stm32_count_enable_write(struct counter_device *counter,
215 struct counter_count *count,
217 const char *buf, size_t len)
219 struct stm32_timer_cnt *const priv = counter->priv;
224 err = kstrtobool(buf, &enable);
229 regmap_read(priv->regmap, TIM_CR1, &cr1);
230 if (!(cr1 & TIM_CR1_CEN))
231 clk_enable(priv->clk);
233 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
236 regmap_read(priv->regmap, TIM_CR1, &cr1);
237 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
238 if (cr1 & TIM_CR1_CEN)
239 clk_disable(priv->clk);
242 /* Keep enabled state to properly handle low power states */
243 priv->enabled = enable;
248 static const struct counter_count_ext stm32_count_ext[] = {
251 .read = stm32_count_direction_read,
255 .read = stm32_count_enable_read,
256 .write = stm32_count_enable_write
260 .read = stm32_count_ceiling_read,
261 .write = stm32_count_ceiling_write
265 enum stm32_synapse_action {
266 STM32_SYNAPSE_ACTION_NONE,
267 STM32_SYNAPSE_ACTION_BOTH_EDGES
270 static const enum counter_synapse_action stm32_synapse_actions[] = {
271 [STM32_SYNAPSE_ACTION_NONE] = COUNTER_SYNAPSE_ACTION_NONE,
272 [STM32_SYNAPSE_ACTION_BOTH_EDGES] = COUNTER_SYNAPSE_ACTION_BOTH_EDGES
275 static int stm32_action_get(struct counter_device *counter,
276 struct counter_count *count,
277 struct counter_synapse *synapse,
283 err = stm32_count_function_get(counter, count, &function);
288 case STM32_COUNT_SLAVE_MODE_DISABLED:
289 /* counts on internal clock when CEN=1 */
290 *action = STM32_SYNAPSE_ACTION_NONE;
292 case STM32_COUNT_ENCODER_MODE_1:
293 /* counts up/down on TI1FP1 edge depending on TI2FP2 level */
294 if (synapse->signal->id == count->synapses[0].signal->id)
295 *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
297 *action = STM32_SYNAPSE_ACTION_NONE;
299 case STM32_COUNT_ENCODER_MODE_2:
300 /* counts up/down on TI2FP2 edge depending on TI1FP1 level */
301 if (synapse->signal->id == count->synapses[1].signal->id)
302 *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
304 *action = STM32_SYNAPSE_ACTION_NONE;
306 case STM32_COUNT_ENCODER_MODE_3:
307 /* counts up/down on both TI1FP1 and TI2FP2 edges */
308 *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
315 static const struct counter_ops stm32_timer_cnt_ops = {
316 .count_read = stm32_count_read,
317 .count_write = stm32_count_write,
318 .function_get = stm32_count_function_get,
319 .function_set = stm32_count_function_set,
320 .action_get = stm32_action_get,
323 static struct counter_signal stm32_signals[] = {
326 .name = "Channel 1 Quadrature A"
330 .name = "Channel 1 Quadrature B"
334 static struct counter_synapse stm32_count_synapses[] = {
336 .actions_list = stm32_synapse_actions,
337 .num_actions = ARRAY_SIZE(stm32_synapse_actions),
338 .signal = &stm32_signals[0]
341 .actions_list = stm32_synapse_actions,
342 .num_actions = ARRAY_SIZE(stm32_synapse_actions),
343 .signal = &stm32_signals[1]
347 static struct counter_count stm32_counts = {
349 .name = "Channel 1 Count",
350 .functions_list = stm32_count_functions,
351 .num_functions = ARRAY_SIZE(stm32_count_functions),
352 .synapses = stm32_count_synapses,
353 .num_synapses = ARRAY_SIZE(stm32_count_synapses),
354 .ext = stm32_count_ext,
355 .num_ext = ARRAY_SIZE(stm32_count_ext)
358 static int stm32_timer_cnt_probe(struct platform_device *pdev)
360 struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
361 struct device *dev = &pdev->dev;
362 struct stm32_timer_cnt *priv;
364 if (IS_ERR_OR_NULL(ddata))
367 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
371 priv->regmap = ddata->regmap;
372 priv->clk = ddata->clk;
373 priv->max_arr = ddata->max_arr;
375 priv->counter.name = dev_name(dev);
376 priv->counter.parent = dev;
377 priv->counter.ops = &stm32_timer_cnt_ops;
378 priv->counter.counts = &stm32_counts;
379 priv->counter.num_counts = 1;
380 priv->counter.signals = stm32_signals;
381 priv->counter.num_signals = ARRAY_SIZE(stm32_signals);
382 priv->counter.priv = priv;
384 platform_set_drvdata(pdev, priv);
386 /* Register Counter device */
387 return devm_counter_register(dev, &priv->counter);
390 static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev)
392 struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
394 /* Only take care of enabled counter: don't disturb other MFD child */
396 /* Backup registers that may get lost in low power mode */
397 regmap_read(priv->regmap, TIM_SMCR, &priv->bak.smcr);
398 regmap_read(priv->regmap, TIM_ARR, &priv->bak.arr);
399 regmap_read(priv->regmap, TIM_CNT, &priv->bak.cnt);
400 regmap_read(priv->regmap, TIM_CR1, &priv->bak.cr1);
402 /* Disable the counter */
403 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
404 clk_disable(priv->clk);
407 return pinctrl_pm_select_sleep_state(dev);
410 static int __maybe_unused stm32_timer_cnt_resume(struct device *dev)
412 struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
415 ret = pinctrl_pm_select_default_state(dev);
420 clk_enable(priv->clk);
422 /* Restore registers that may have been lost */
423 regmap_write(priv->regmap, TIM_SMCR, priv->bak.smcr);
424 regmap_write(priv->regmap, TIM_ARR, priv->bak.arr);
425 regmap_write(priv->regmap, TIM_CNT, priv->bak.cnt);
427 /* Also re-enables the counter */
428 regmap_write(priv->regmap, TIM_CR1, priv->bak.cr1);
434 static SIMPLE_DEV_PM_OPS(stm32_timer_cnt_pm_ops, stm32_timer_cnt_suspend,
435 stm32_timer_cnt_resume);
437 static const struct of_device_id stm32_timer_cnt_of_match[] = {
438 { .compatible = "st,stm32-timer-counter", },
441 MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match);
443 static struct platform_driver stm32_timer_cnt_driver = {
444 .probe = stm32_timer_cnt_probe,
446 .name = "stm32-timer-counter",
447 .of_match_table = stm32_timer_cnt_of_match,
448 .pm = &stm32_timer_cnt_pm_ops,
451 module_platform_driver(stm32_timer_cnt_driver);
453 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
454 MODULE_ALIAS("platform:stm32-timer-counter");
455 MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver");
456 MODULE_LICENSE("GPL v2");