1 // SPDX-License-Identifier: GPL-2.0
3 * STM32 Low-Power Timer Encoder and Counter driver
5 * Copyright (C) STMicroelectronics 2017
9 * Inspired by 104-quad-8 and stm32-timer-trigger drivers.
13 #include <linux/bitfield.h>
14 #include <linux/counter.h>
15 #include <linux/mfd/stm32-lptimer.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/module.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/platform_device.h>
20 #include <linux/types.h>
22 struct stm32_lptim_cnt {
23 struct counter_device counter;
25 struct regmap *regmap;
33 static int stm32_lptim_is_enabled(struct stm32_lptim_cnt *priv)
38 ret = regmap_read(priv->regmap, STM32_LPTIM_CR, &val);
42 return FIELD_GET(STM32_LPTIM_ENABLE, val);
45 static int stm32_lptim_set_enable_state(struct stm32_lptim_cnt *priv,
51 val = FIELD_PREP(STM32_LPTIM_ENABLE, enable);
52 ret = regmap_write(priv->regmap, STM32_LPTIM_CR, val);
57 clk_disable(priv->clk);
58 priv->enabled = false;
62 /* LP timer must be enabled before writing CMP & ARR */
63 ret = regmap_write(priv->regmap, STM32_LPTIM_ARR, priv->ceiling);
67 ret = regmap_write(priv->regmap, STM32_LPTIM_CMP, 0);
71 /* ensure CMP & ARR registers are properly written */
72 ret = regmap_read_poll_timeout(priv->regmap, STM32_LPTIM_ISR, val,
73 (val & STM32_LPTIM_CMPOK_ARROK),
78 ret = regmap_write(priv->regmap, STM32_LPTIM_ICR,
79 STM32_LPTIM_CMPOKCF_ARROKCF);
83 ret = clk_enable(priv->clk);
85 regmap_write(priv->regmap, STM32_LPTIM_CR, 0);
90 /* Start LP timer in continuous mode */
91 return regmap_update_bits(priv->regmap, STM32_LPTIM_CR,
92 STM32_LPTIM_CNTSTRT, STM32_LPTIM_CNTSTRT);
95 static int stm32_lptim_setup(struct stm32_lptim_cnt *priv, int enable)
97 u32 mask = STM32_LPTIM_ENC | STM32_LPTIM_COUNTMODE |
98 STM32_LPTIM_CKPOL | STM32_LPTIM_PRESC;
101 /* Setup LP timer encoder/counter and polarity, without prescaler */
102 if (priv->quadrature_mode)
103 val = enable ? STM32_LPTIM_ENC : 0;
105 val = enable ? STM32_LPTIM_COUNTMODE : 0;
106 val |= FIELD_PREP(STM32_LPTIM_CKPOL, enable ? priv->polarity : 0);
108 return regmap_update_bits(priv->regmap, STM32_LPTIM_CFGR, mask, val);
112 * In non-quadrature mode, device counts up on active edge.
113 * In quadrature mode, encoder counting scenarios are as follows:
114 * +---------+----------+--------------------+--------------------+
115 * | Active | Level on | IN1 signal | IN2 signal |
116 * | edge | opposite +----------+---------+----------+---------+
117 * | | signal | Rising | Falling | Rising | Falling |
118 * +---------+----------+----------+---------+----------+---------+
119 * | Rising | High -> | Down | - | Up | - |
120 * | edge | Low -> | Up | - | Down | - |
121 * +---------+----------+----------+---------+----------+---------+
122 * | Falling | High -> | - | Up | - | Down |
123 * | edge | Low -> | - | Down | - | Up |
124 * +---------+----------+----------+---------+----------+---------+
125 * | Both | High -> | Down | Up | Up | Down |
126 * | edges | Low -> | Up | Down | Down | Up |
127 * +---------+----------+----------+---------+----------+---------+
129 static const enum counter_function stm32_lptim_cnt_functions[] = {
130 COUNTER_FUNCTION_INCREASE,
131 COUNTER_FUNCTION_QUADRATURE_X4,
134 static const enum counter_synapse_action stm32_lptim_cnt_synapse_actions[] = {
135 COUNTER_SYNAPSE_ACTION_RISING_EDGE,
136 COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
137 COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
138 COUNTER_SYNAPSE_ACTION_NONE,
141 static int stm32_lptim_cnt_read(struct counter_device *counter,
142 struct counter_count *count, u64 *val)
144 struct stm32_lptim_cnt *const priv = counter->priv;
148 ret = regmap_read(priv->regmap, STM32_LPTIM_CNT, &cnt);
157 static int stm32_lptim_cnt_function_read(struct counter_device *counter,
158 struct counter_count *count,
159 enum counter_function *function)
161 struct stm32_lptim_cnt *const priv = counter->priv;
163 if (!priv->quadrature_mode) {
164 *function = COUNTER_FUNCTION_INCREASE;
168 if (priv->polarity == STM32_LPTIM_CKPOL_BOTH_EDGES) {
169 *function = COUNTER_FUNCTION_QUADRATURE_X4;
176 static int stm32_lptim_cnt_function_write(struct counter_device *counter,
177 struct counter_count *count,
178 enum counter_function function)
180 struct stm32_lptim_cnt *const priv = counter->priv;
182 if (stm32_lptim_is_enabled(priv))
186 case COUNTER_FUNCTION_INCREASE:
187 priv->quadrature_mode = 0;
189 case COUNTER_FUNCTION_QUADRATURE_X4:
190 priv->quadrature_mode = 1;
191 priv->polarity = STM32_LPTIM_CKPOL_BOTH_EDGES;
194 /* should never reach this path */
199 static int stm32_lptim_cnt_enable_read(struct counter_device *counter,
200 struct counter_count *count,
203 struct stm32_lptim_cnt *const priv = counter->priv;
206 ret = stm32_lptim_is_enabled(priv);
215 static int stm32_lptim_cnt_enable_write(struct counter_device *counter,
216 struct counter_count *count,
219 struct stm32_lptim_cnt *const priv = counter->priv;
222 /* Check nobody uses the timer, or already disabled/enabled */
223 ret = stm32_lptim_is_enabled(priv);
224 if ((ret < 0) || (!ret && !enable))
229 ret = stm32_lptim_setup(priv, enable);
233 ret = stm32_lptim_set_enable_state(priv, enable);
240 static int stm32_lptim_cnt_ceiling_read(struct counter_device *counter,
241 struct counter_count *count,
244 struct stm32_lptim_cnt *const priv = counter->priv;
246 *ceiling = priv->ceiling;
251 static int stm32_lptim_cnt_ceiling_write(struct counter_device *counter,
252 struct counter_count *count,
255 struct stm32_lptim_cnt *const priv = counter->priv;
257 if (stm32_lptim_is_enabled(priv))
260 if (ceiling > STM32_LPTIM_MAX_ARR)
263 priv->ceiling = ceiling;
268 static struct counter_comp stm32_lptim_cnt_ext[] = {
269 COUNTER_COMP_ENABLE(stm32_lptim_cnt_enable_read,
270 stm32_lptim_cnt_enable_write),
271 COUNTER_COMP_CEILING(stm32_lptim_cnt_ceiling_read,
272 stm32_lptim_cnt_ceiling_write),
275 static int stm32_lptim_cnt_action_read(struct counter_device *counter,
276 struct counter_count *count,
277 struct counter_synapse *synapse,
278 enum counter_synapse_action *action)
280 struct stm32_lptim_cnt *const priv = counter->priv;
281 enum counter_function function;
284 err = stm32_lptim_cnt_function_read(counter, count, &function);
289 case COUNTER_FUNCTION_INCREASE:
290 /* LP Timer acts as up-counter on input 1 */
291 if (synapse->signal->id != count->synapses[0].signal->id) {
292 *action = COUNTER_SYNAPSE_ACTION_NONE;
296 switch (priv->polarity) {
297 case STM32_LPTIM_CKPOL_RISING_EDGE:
298 *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
300 case STM32_LPTIM_CKPOL_FALLING_EDGE:
301 *action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
303 case STM32_LPTIM_CKPOL_BOTH_EDGES:
304 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
307 /* should never reach this path */
310 case COUNTER_FUNCTION_QUADRATURE_X4:
311 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
314 /* should never reach this path */
319 static int stm32_lptim_cnt_action_write(struct counter_device *counter,
320 struct counter_count *count,
321 struct counter_synapse *synapse,
322 enum counter_synapse_action action)
324 struct stm32_lptim_cnt *const priv = counter->priv;
325 enum counter_function function;
328 if (stm32_lptim_is_enabled(priv))
331 err = stm32_lptim_cnt_function_read(counter, count, &function);
335 /* only set polarity when in counter mode (on input 1) */
336 if (function != COUNTER_FUNCTION_INCREASE
337 || synapse->signal->id != count->synapses[0].signal->id)
341 case COUNTER_SYNAPSE_ACTION_RISING_EDGE:
342 priv->polarity = STM32_LPTIM_CKPOL_RISING_EDGE;
344 case COUNTER_SYNAPSE_ACTION_FALLING_EDGE:
345 priv->polarity = STM32_LPTIM_CKPOL_FALLING_EDGE;
347 case COUNTER_SYNAPSE_ACTION_BOTH_EDGES:
348 priv->polarity = STM32_LPTIM_CKPOL_BOTH_EDGES;
355 static const struct counter_ops stm32_lptim_cnt_ops = {
356 .count_read = stm32_lptim_cnt_read,
357 .function_read = stm32_lptim_cnt_function_read,
358 .function_write = stm32_lptim_cnt_function_write,
359 .action_read = stm32_lptim_cnt_action_read,
360 .action_write = stm32_lptim_cnt_action_write,
363 static struct counter_signal stm32_lptim_cnt_signals[] = {
366 .name = "Channel 1 Quadrature A"
370 .name = "Channel 1 Quadrature B"
374 static struct counter_synapse stm32_lptim_cnt_synapses[] = {
376 .actions_list = stm32_lptim_cnt_synapse_actions,
377 .num_actions = ARRAY_SIZE(stm32_lptim_cnt_synapse_actions),
378 .signal = &stm32_lptim_cnt_signals[0]
381 .actions_list = stm32_lptim_cnt_synapse_actions,
382 .num_actions = ARRAY_SIZE(stm32_lptim_cnt_synapse_actions),
383 .signal = &stm32_lptim_cnt_signals[1]
387 /* LP timer with encoder */
388 static struct counter_count stm32_lptim_enc_counts = {
390 .name = "LPTimer Count",
391 .functions_list = stm32_lptim_cnt_functions,
392 .num_functions = ARRAY_SIZE(stm32_lptim_cnt_functions),
393 .synapses = stm32_lptim_cnt_synapses,
394 .num_synapses = ARRAY_SIZE(stm32_lptim_cnt_synapses),
395 .ext = stm32_lptim_cnt_ext,
396 .num_ext = ARRAY_SIZE(stm32_lptim_cnt_ext)
399 /* LP timer without encoder (counter only) */
400 static struct counter_count stm32_lptim_in1_counts = {
402 .name = "LPTimer Count",
403 .functions_list = stm32_lptim_cnt_functions,
405 .synapses = stm32_lptim_cnt_synapses,
407 .ext = stm32_lptim_cnt_ext,
408 .num_ext = ARRAY_SIZE(stm32_lptim_cnt_ext)
411 static int stm32_lptim_cnt_probe(struct platform_device *pdev)
413 struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent);
414 struct stm32_lptim_cnt *priv;
416 if (IS_ERR_OR_NULL(ddata))
419 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
423 priv->dev = &pdev->dev;
424 priv->regmap = ddata->regmap;
425 priv->clk = ddata->clk;
426 priv->ceiling = STM32_LPTIM_MAX_ARR;
428 /* Initialize Counter device */
429 priv->counter.name = dev_name(&pdev->dev);
430 priv->counter.parent = &pdev->dev;
431 priv->counter.ops = &stm32_lptim_cnt_ops;
432 if (ddata->has_encoder) {
433 priv->counter.counts = &stm32_lptim_enc_counts;
434 priv->counter.num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals);
436 priv->counter.counts = &stm32_lptim_in1_counts;
437 priv->counter.num_signals = 1;
439 priv->counter.num_counts = 1;
440 priv->counter.signals = stm32_lptim_cnt_signals;
441 priv->counter.priv = priv;
443 platform_set_drvdata(pdev, priv);
445 return devm_counter_register(&pdev->dev, &priv->counter);
448 #ifdef CONFIG_PM_SLEEP
449 static int stm32_lptim_cnt_suspend(struct device *dev)
451 struct stm32_lptim_cnt *priv = dev_get_drvdata(dev);
454 /* Only take care of enabled counter: don't disturb other MFD child */
456 ret = stm32_lptim_setup(priv, 0);
460 ret = stm32_lptim_set_enable_state(priv, 0);
464 /* Force enable state for later resume */
465 priv->enabled = true;
468 return pinctrl_pm_select_sleep_state(dev);
471 static int stm32_lptim_cnt_resume(struct device *dev)
473 struct stm32_lptim_cnt *priv = dev_get_drvdata(dev);
476 ret = pinctrl_pm_select_default_state(dev);
481 priv->enabled = false;
482 ret = stm32_lptim_setup(priv, 1);
486 ret = stm32_lptim_set_enable_state(priv, 1);
495 static SIMPLE_DEV_PM_OPS(stm32_lptim_cnt_pm_ops, stm32_lptim_cnt_suspend,
496 stm32_lptim_cnt_resume);
498 static const struct of_device_id stm32_lptim_cnt_of_match[] = {
499 { .compatible = "st,stm32-lptimer-counter", },
502 MODULE_DEVICE_TABLE(of, stm32_lptim_cnt_of_match);
504 static struct platform_driver stm32_lptim_cnt_driver = {
505 .probe = stm32_lptim_cnt_probe,
507 .name = "stm32-lptimer-counter",
508 .of_match_table = stm32_lptim_cnt_of_match,
509 .pm = &stm32_lptim_cnt_pm_ops,
512 module_platform_driver(stm32_lptim_cnt_driver);
515 MODULE_ALIAS("platform:stm32-lptimer-counter");
516 MODULE_DESCRIPTION("STMicroelectronics STM32 LPTIM counter driver");
517 MODULE_LICENSE("GPL v2");