Lines Matching +full:stm32 +full:- +full:timer +full:- +full:counter
1 // SPDX-License-Identifier: GPL-2.0
3 * STM32 Low-Power Timer Encoder and Counter driver
9 * Inspired by 104-quad-8 and stm32-timer-trigger drivers.
14 #include <linux/counter.h>
15 #include <linux/mfd/stm32-lptimer.h>
37 ret = regmap_read(priv->regmap, STM32_LPTIM_CR, &val); in stm32_lptim_is_enabled()
51 ret = regmap_write(priv->regmap, STM32_LPTIM_CR, val); in stm32_lptim_set_enable_state()
56 clk_disable(priv->clk); in stm32_lptim_set_enable_state()
57 priv->enabled = false; in stm32_lptim_set_enable_state()
61 ret = clk_enable(priv->clk); in stm32_lptim_set_enable_state()
65 /* LP timer must be enabled before writing CMP & ARR */ in stm32_lptim_set_enable_state()
66 ret = regmap_write(priv->regmap, STM32_LPTIM_ARR, priv->ceiling); in stm32_lptim_set_enable_state()
70 ret = regmap_write(priv->regmap, STM32_LPTIM_CMP, 0); in stm32_lptim_set_enable_state()
75 ret = regmap_read_poll_timeout(priv->regmap, STM32_LPTIM_ISR, val, in stm32_lptim_set_enable_state()
81 ret = regmap_write(priv->regmap, STM32_LPTIM_ICR, in stm32_lptim_set_enable_state()
86 priv->enabled = true; in stm32_lptim_set_enable_state()
88 /* Start LP timer in continuous mode */ in stm32_lptim_set_enable_state()
89 return regmap_update_bits(priv->regmap, STM32_LPTIM_CR, in stm32_lptim_set_enable_state()
93 clk_disable(priv->clk); in stm32_lptim_set_enable_state()
95 regmap_write(priv->regmap, STM32_LPTIM_CR, 0); in stm32_lptim_set_enable_state()
106 /* Setup LP timer encoder/counter and polarity, without prescaler */ in stm32_lptim_setup()
107 if (priv->quadrature_mode) in stm32_lptim_setup()
111 val |= FIELD_PREP(STM32_LPTIM_CKPOL, enable ? priv->polarity : 0); in stm32_lptim_setup()
113 return regmap_update_bits(priv->regmap, STM32_LPTIM_CFGR, mask, val); in stm32_lptim_setup()
117 * In non-quadrature mode, device counts up on active edge.
119 * +---------+----------+--------------------+--------------------+
121 * | edge | opposite +----------+---------+----------+---------+
123 * +---------+----------+----------+---------+----------+---------+
124 * | Rising | High -> | Down | - | Up | - |
125 * | edge | Low -> | Up | - | Down | - |
126 * +---------+----------+----------+---------+----------+---------+
127 * | Falling | High -> | - | Up | - | Down |
128 * | edge | Low -> | - | Down | - | Up |
129 * +---------+----------+----------+---------+----------+---------+
130 * | Both | High -> | Down | Up | Up | Down |
131 * | edges | Low -> | Up | Down | Down | Up |
132 * +---------+----------+----------+---------+----------+---------+
146 static int stm32_lptim_cnt_read(struct counter_device *counter, in stm32_lptim_cnt_read() argument
149 struct stm32_lptim_cnt *const priv = counter_priv(counter); in stm32_lptim_cnt_read()
153 ret = regmap_read(priv->regmap, STM32_LPTIM_CNT, &cnt); in stm32_lptim_cnt_read()
162 static int stm32_lptim_cnt_function_read(struct counter_device *counter, in stm32_lptim_cnt_function_read() argument
166 struct stm32_lptim_cnt *const priv = counter_priv(counter); in stm32_lptim_cnt_function_read()
168 if (!priv->quadrature_mode) { in stm32_lptim_cnt_function_read()
173 if (priv->polarity == STM32_LPTIM_CKPOL_BOTH_EDGES) { in stm32_lptim_cnt_function_read()
178 return -EINVAL; in stm32_lptim_cnt_function_read()
181 static int stm32_lptim_cnt_function_write(struct counter_device *counter, in stm32_lptim_cnt_function_write() argument
185 struct stm32_lptim_cnt *const priv = counter_priv(counter); in stm32_lptim_cnt_function_write()
188 return -EBUSY; in stm32_lptim_cnt_function_write()
192 priv->quadrature_mode = 0; in stm32_lptim_cnt_function_write()
195 priv->quadrature_mode = 1; in stm32_lptim_cnt_function_write()
196 priv->polarity = STM32_LPTIM_CKPOL_BOTH_EDGES; in stm32_lptim_cnt_function_write()
200 return -EINVAL; in stm32_lptim_cnt_function_write()
204 static int stm32_lptim_cnt_enable_read(struct counter_device *counter, in stm32_lptim_cnt_enable_read() argument
208 struct stm32_lptim_cnt *const priv = counter_priv(counter); in stm32_lptim_cnt_enable_read()
220 static int stm32_lptim_cnt_enable_write(struct counter_device *counter, in stm32_lptim_cnt_enable_write() argument
224 struct stm32_lptim_cnt *const priv = counter_priv(counter); in stm32_lptim_cnt_enable_write()
227 /* Check nobody uses the timer, or already disabled/enabled */ in stm32_lptim_cnt_enable_write()
232 return -EBUSY; in stm32_lptim_cnt_enable_write()
245 static int stm32_lptim_cnt_ceiling_read(struct counter_device *counter, in stm32_lptim_cnt_ceiling_read() argument
249 struct stm32_lptim_cnt *const priv = counter_priv(counter); in stm32_lptim_cnt_ceiling_read()
251 *ceiling = priv->ceiling; in stm32_lptim_cnt_ceiling_read()
256 static int stm32_lptim_cnt_ceiling_write(struct counter_device *counter, in stm32_lptim_cnt_ceiling_write() argument
260 struct stm32_lptim_cnt *const priv = counter_priv(counter); in stm32_lptim_cnt_ceiling_write()
263 return -EBUSY; in stm32_lptim_cnt_ceiling_write()
266 return -ERANGE; in stm32_lptim_cnt_ceiling_write()
268 priv->ceiling = ceiling; in stm32_lptim_cnt_ceiling_write()
280 static int stm32_lptim_cnt_action_read(struct counter_device *counter, in stm32_lptim_cnt_action_read() argument
285 struct stm32_lptim_cnt *const priv = counter_priv(counter); in stm32_lptim_cnt_action_read()
289 err = stm32_lptim_cnt_function_read(counter, count, &function); in stm32_lptim_cnt_action_read()
295 /* LP Timer acts as up-counter on input 1 */ in stm32_lptim_cnt_action_read()
296 if (synapse->signal->id != count->synapses[0].signal->id) { in stm32_lptim_cnt_action_read()
301 switch (priv->polarity) { in stm32_lptim_cnt_action_read()
313 return -EINVAL; in stm32_lptim_cnt_action_read()
320 return -EINVAL; in stm32_lptim_cnt_action_read()
324 static int stm32_lptim_cnt_action_write(struct counter_device *counter, in stm32_lptim_cnt_action_write() argument
329 struct stm32_lptim_cnt *const priv = counter_priv(counter); in stm32_lptim_cnt_action_write()
334 return -EBUSY; in stm32_lptim_cnt_action_write()
336 err = stm32_lptim_cnt_function_read(counter, count, &function); in stm32_lptim_cnt_action_write()
340 /* only set polarity when in counter mode (on input 1) */ in stm32_lptim_cnt_action_write()
342 || synapse->signal->id != count->synapses[0].signal->id) in stm32_lptim_cnt_action_write()
343 return -EINVAL; in stm32_lptim_cnt_action_write()
347 priv->polarity = STM32_LPTIM_CKPOL_RISING_EDGE; in stm32_lptim_cnt_action_write()
350 priv->polarity = STM32_LPTIM_CKPOL_FALLING_EDGE; in stm32_lptim_cnt_action_write()
353 priv->polarity = STM32_LPTIM_CKPOL_BOTH_EDGES; in stm32_lptim_cnt_action_write()
356 return -EINVAL; in stm32_lptim_cnt_action_write()
392 /* LP timer with encoder */
404 /* LP timer without encoder (counter only) */
418 struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent); in stm32_lptim_cnt_probe()
419 struct counter_device *counter; in stm32_lptim_cnt_probe() local
424 return -EINVAL; in stm32_lptim_cnt_probe()
426 counter = devm_counter_alloc(&pdev->dev, sizeof(*priv)); in stm32_lptim_cnt_probe()
427 if (!counter) in stm32_lptim_cnt_probe()
428 return -ENOMEM; in stm32_lptim_cnt_probe()
429 priv = counter_priv(counter); in stm32_lptim_cnt_probe()
431 priv->dev = &pdev->dev; in stm32_lptim_cnt_probe()
432 priv->regmap = ddata->regmap; in stm32_lptim_cnt_probe()
433 priv->clk = ddata->clk; in stm32_lptim_cnt_probe()
434 priv->ceiling = STM32_LPTIM_MAX_ARR; in stm32_lptim_cnt_probe()
436 /* Initialize Counter device */ in stm32_lptim_cnt_probe()
437 counter->name = dev_name(&pdev->dev); in stm32_lptim_cnt_probe()
438 counter->parent = &pdev->dev; in stm32_lptim_cnt_probe()
439 counter->ops = &stm32_lptim_cnt_ops; in stm32_lptim_cnt_probe()
440 if (ddata->has_encoder) { in stm32_lptim_cnt_probe()
441 counter->counts = &stm32_lptim_enc_counts; in stm32_lptim_cnt_probe()
442 counter->num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals); in stm32_lptim_cnt_probe()
444 counter->counts = &stm32_lptim_in1_counts; in stm32_lptim_cnt_probe()
445 counter->num_signals = 1; in stm32_lptim_cnt_probe()
447 counter->num_counts = 1; in stm32_lptim_cnt_probe()
448 counter->signals = stm32_lptim_cnt_signals; in stm32_lptim_cnt_probe()
452 ret = devm_counter_add(&pdev->dev, counter); in stm32_lptim_cnt_probe()
454 return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n"); in stm32_lptim_cnt_probe()
465 /* Only take care of enabled counter: don't disturb other MFD child */ in stm32_lptim_cnt_suspend()
466 if (priv->enabled) { in stm32_lptim_cnt_suspend()
476 priv->enabled = true; in stm32_lptim_cnt_suspend()
491 if (priv->enabled) { in stm32_lptim_cnt_resume()
492 priv->enabled = false; in stm32_lptim_cnt_resume()
510 { .compatible = "st,stm32-lptimer-counter", },
518 .name = "stm32-lptimer-counter",
526 MODULE_ALIAS("platform:stm32-lptimer-counter");
527 MODULE_DESCRIPTION("STMicroelectronics STM32 LPTIM counter driver");
529 MODULE_IMPORT_NS(COUNTER);