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/iio/iio.h>
12 #include <linux/iio/types.h>
13 #include <linux/mfd/stm32-timers.h>
14 #include <linux/module.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_cnt {
24 struct counter_device counter;
25 struct regmap *regmap;
31 * stm32_count_function - enumerates stm32 timer counter encoder modes
32 * @STM32_COUNT_SLAVE_MODE_DISABLED: counts on internal clock when CEN=1
33 * @STM32_COUNT_ENCODER_MODE_1: counts TI1FP1 edges, depending on TI2FP2 level
34 * @STM32_COUNT_ENCODER_MODE_2: counts TI2FP2 edges, depending on TI1FP1 level
35 * @STM32_COUNT_ENCODER_MODE_3: counts on both TI1FP1 and TI2FP2 edges
37 enum stm32_count_function {
38 STM32_COUNT_SLAVE_MODE_DISABLED = -1,
39 STM32_COUNT_ENCODER_MODE_1,
40 STM32_COUNT_ENCODER_MODE_2,
41 STM32_COUNT_ENCODER_MODE_3,
44 static enum counter_count_function stm32_count_functions[] = {
45 [STM32_COUNT_ENCODER_MODE_1] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_A,
46 [STM32_COUNT_ENCODER_MODE_2] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_B,
47 [STM32_COUNT_ENCODER_MODE_3] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4,
50 static int stm32_count_read(struct counter_device *counter,
51 struct counter_count *count,
52 struct counter_count_read_value *val)
54 struct stm32_timer_cnt *const priv = counter->priv;
57 regmap_read(priv->regmap, TIM_CNT, &cnt);
58 counter_count_read_value_set(val, COUNTER_COUNT_POSITION, &cnt);
63 static int stm32_count_write(struct counter_device *counter,
64 struct counter_count *count,
65 struct counter_count_write_value *val)
67 struct stm32_timer_cnt *const priv = counter->priv;
71 err = counter_count_write_value_get(&cnt, COUNTER_COUNT_POSITION, val);
75 if (cnt > priv->ceiling)
78 return regmap_write(priv->regmap, TIM_CNT, cnt);
81 static int stm32_count_function_get(struct counter_device *counter,
82 struct counter_count *count,
85 struct stm32_timer_cnt *const priv = counter->priv;
88 regmap_read(priv->regmap, TIM_SMCR, &smcr);
90 switch (smcr & TIM_SMCR_SMS) {
92 *function = STM32_COUNT_ENCODER_MODE_1;
95 *function = STM32_COUNT_ENCODER_MODE_2;
98 *function = STM32_COUNT_ENCODER_MODE_3;
105 static int stm32_count_function_set(struct counter_device *counter,
106 struct counter_count *count,
109 struct stm32_timer_cnt *const priv = counter->priv;
113 case STM32_COUNT_ENCODER_MODE_1:
116 case STM32_COUNT_ENCODER_MODE_2:
119 case STM32_COUNT_ENCODER_MODE_3:
127 /* Store enable status */
128 regmap_read(priv->regmap, TIM_CR1, &cr1);
130 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
132 /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
133 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
134 regmap_write(priv->regmap, TIM_ARR, priv->ceiling);
136 regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
138 /* Make sure that registers are updated */
139 regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
141 /* Restore the enable status */
142 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, cr1);
147 static ssize_t stm32_count_direction_read(struct counter_device *counter,
148 struct counter_count *count,
149 void *private, char *buf)
151 struct stm32_timer_cnt *const priv = counter->priv;
152 const char *direction;
155 regmap_read(priv->regmap, TIM_CR1, &cr1);
156 direction = (cr1 & TIM_CR1_DIR) ? "backward" : "forward";
158 return scnprintf(buf, PAGE_SIZE, "%s\n", direction);
161 static ssize_t stm32_count_ceiling_read(struct counter_device *counter,
162 struct counter_count *count,
163 void *private, char *buf)
165 struct stm32_timer_cnt *const priv = counter->priv;
168 regmap_read(priv->regmap, TIM_ARR, &arr);
170 return snprintf(buf, PAGE_SIZE, "%u\n", arr);
173 static ssize_t stm32_count_ceiling_write(struct counter_device *counter,
174 struct counter_count *count,
176 const char *buf, size_t len)
178 struct stm32_timer_cnt *const priv = counter->priv;
179 unsigned int ceiling;
182 ret = kstrtouint(buf, 0, &ceiling);
186 /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
187 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
188 regmap_write(priv->regmap, TIM_ARR, ceiling);
190 priv->ceiling = ceiling;
194 static ssize_t stm32_count_enable_read(struct counter_device *counter,
195 struct counter_count *count,
196 void *private, char *buf)
198 struct stm32_timer_cnt *const priv = counter->priv;
201 regmap_read(priv->regmap, TIM_CR1, &cr1);
203 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)(cr1 & TIM_CR1_CEN));
206 static ssize_t stm32_count_enable_write(struct counter_device *counter,
207 struct counter_count *count,
209 const char *buf, size_t len)
211 struct stm32_timer_cnt *const priv = counter->priv;
216 err = kstrtobool(buf, &enable);
221 regmap_read(priv->regmap, TIM_CR1, &cr1);
222 if (!(cr1 & TIM_CR1_CEN))
223 clk_enable(priv->clk);
225 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
228 regmap_read(priv->regmap, TIM_CR1, &cr1);
229 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
230 if (cr1 & TIM_CR1_CEN)
231 clk_disable(priv->clk);
237 static const struct counter_count_ext stm32_count_ext[] = {
240 .read = stm32_count_direction_read,
244 .read = stm32_count_enable_read,
245 .write = stm32_count_enable_write
249 .read = stm32_count_ceiling_read,
250 .write = stm32_count_ceiling_write
254 enum stm32_synapse_action {
255 STM32_SYNAPSE_ACTION_NONE,
256 STM32_SYNAPSE_ACTION_BOTH_EDGES
259 static enum counter_synapse_action stm32_synapse_actions[] = {
260 [STM32_SYNAPSE_ACTION_NONE] = COUNTER_SYNAPSE_ACTION_NONE,
261 [STM32_SYNAPSE_ACTION_BOTH_EDGES] = COUNTER_SYNAPSE_ACTION_BOTH_EDGES
264 static int stm32_action_get(struct counter_device *counter,
265 struct counter_count *count,
266 struct counter_synapse *synapse,
272 /* Default action mode (e.g. STM32_COUNT_SLAVE_MODE_DISABLED) */
273 *action = STM32_SYNAPSE_ACTION_NONE;
275 err = stm32_count_function_get(counter, count, &function);
280 case STM32_COUNT_ENCODER_MODE_1:
281 /* counts up/down on TI1FP1 edge depending on TI2FP2 level */
282 if (synapse->signal->id == count->synapses[0].signal->id)
283 *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
285 case STM32_COUNT_ENCODER_MODE_2:
286 /* counts up/down on TI2FP2 edge depending on TI1FP1 level */
287 if (synapse->signal->id == count->synapses[1].signal->id)
288 *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
290 case STM32_COUNT_ENCODER_MODE_3:
291 /* counts up/down on both TI1FP1 and TI2FP2 edges */
292 *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
299 static const struct counter_ops stm32_timer_cnt_ops = {
300 .count_read = stm32_count_read,
301 .count_write = stm32_count_write,
302 .function_get = stm32_count_function_get,
303 .function_set = stm32_count_function_set,
304 .action_get = stm32_action_get,
307 static struct counter_signal stm32_signals[] = {
310 .name = "Channel 1 Quadrature A"
314 .name = "Channel 1 Quadrature B"
318 static struct counter_synapse stm32_count_synapses[] = {
320 .actions_list = stm32_synapse_actions,
321 .num_actions = ARRAY_SIZE(stm32_synapse_actions),
322 .signal = &stm32_signals[0]
325 .actions_list = stm32_synapse_actions,
326 .num_actions = ARRAY_SIZE(stm32_synapse_actions),
327 .signal = &stm32_signals[1]
331 static struct counter_count stm32_counts = {
333 .name = "Channel 1 Count",
334 .functions_list = stm32_count_functions,
335 .num_functions = ARRAY_SIZE(stm32_count_functions),
336 .synapses = stm32_count_synapses,
337 .num_synapses = ARRAY_SIZE(stm32_count_synapses),
338 .ext = stm32_count_ext,
339 .num_ext = ARRAY_SIZE(stm32_count_ext)
342 static int stm32_timer_cnt_probe(struct platform_device *pdev)
344 struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
345 struct device *dev = &pdev->dev;
346 struct stm32_timer_cnt *priv;
348 if (IS_ERR_OR_NULL(ddata))
351 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
355 priv->regmap = ddata->regmap;
356 priv->clk = ddata->clk;
357 priv->ceiling = ddata->max_arr;
359 priv->counter.name = dev_name(dev);
360 priv->counter.parent = dev;
361 priv->counter.ops = &stm32_timer_cnt_ops;
362 priv->counter.counts = &stm32_counts;
363 priv->counter.num_counts = 1;
364 priv->counter.signals = stm32_signals;
365 priv->counter.num_signals = ARRAY_SIZE(stm32_signals);
366 priv->counter.priv = priv;
368 /* Register Counter device */
369 return devm_counter_register(dev, &priv->counter);
372 static const struct of_device_id stm32_timer_cnt_of_match[] = {
373 { .compatible = "st,stm32-timer-counter", },
376 MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match);
378 static struct platform_driver stm32_timer_cnt_driver = {
379 .probe = stm32_timer_cnt_probe,
381 .name = "stm32-timer-counter",
382 .of_match_table = stm32_timer_cnt_of_match,
385 module_platform_driver(stm32_timer_cnt_driver);
388 MODULE_ALIAS("platform:stm32-timer-counter");
389 MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver");
390 MODULE_LICENSE("GPL v2");