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>
22 struct counter_device counter; member
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 /* LP timer must be enabled before writing CMP & ARR */ in stm32_lptim_set_enable_state()
62 ret = regmap_write(priv->regmap, STM32_LPTIM_ARR, priv->ceiling); in stm32_lptim_set_enable_state()
66 ret = regmap_write(priv->regmap, STM32_LPTIM_CMP, 0); in stm32_lptim_set_enable_state()
71 ret = regmap_read_poll_timeout(priv->regmap, STM32_LPTIM_ISR, val, in stm32_lptim_set_enable_state()
77 ret = regmap_write(priv->regmap, STM32_LPTIM_ICR, in stm32_lptim_set_enable_state()
82 ret = clk_enable(priv->clk); in stm32_lptim_set_enable_state()
84 regmap_write(priv->regmap, STM32_LPTIM_CR, 0); in stm32_lptim_set_enable_state()
87 priv->enabled = true; in stm32_lptim_set_enable_state()
89 /* Start LP timer in continuous mode */ in stm32_lptim_set_enable_state()
90 return regmap_update_bits(priv->regmap, STM32_LPTIM_CR, in stm32_lptim_set_enable_state()
100 /* Setup LP timer encoder/counter and polarity, without prescaler */ in stm32_lptim_setup()
101 if (priv->quadrature_mode) in stm32_lptim_setup()
105 val |= FIELD_PREP(STM32_LPTIM_CKPOL, enable ? priv->polarity : 0); in stm32_lptim_setup()
107 return regmap_update_bits(priv->regmap, STM32_LPTIM_CFGR, mask, val); in stm32_lptim_setup()
111 * enum stm32_lptim_cnt_function - enumerates LPTimer counter & encoder modes
115 * In non-quadrature mode, device counts up on active edge.
117 * +---------+----------+--------------------+--------------------+
119 * | edge | opposite +----------+---------+----------+---------+
121 * +---------+----------+----------+---------+----------+---------+
122 * | Rising | High -> | Down | - | Up | - |
123 * | edge | Low -> | Up | - | Down | - |
124 * +---------+----------+----------+---------+----------+---------+
125 * | Falling | High -> | - | Up | - | Down |
126 * | edge | Low -> | - | Down | - | Up |
127 * +---------+----------+----------+---------+----------+---------+
128 * | Both | High -> | Down | Up | Up | Down |
129 * | edges | Low -> | Up | Down | Down | Up |
130 * +---------+----------+----------+---------+----------+---------+
150 /* Index must match with stm32_lptim_cnt_polarity[] (priv->polarity) */
157 static int stm32_lptim_cnt_read(struct counter_device *counter, in stm32_lptim_cnt_read() argument
160 struct stm32_lptim_cnt *const priv = counter->priv; in stm32_lptim_cnt_read()
164 ret = regmap_read(priv->regmap, STM32_LPTIM_CNT, &cnt); in stm32_lptim_cnt_read()
173 static int stm32_lptim_cnt_function_get(struct counter_device *counter, in stm32_lptim_cnt_function_get() argument
177 struct stm32_lptim_cnt *const priv = counter->priv; in stm32_lptim_cnt_function_get()
179 if (!priv->quadrature_mode) { in stm32_lptim_cnt_function_get()
184 if (priv->polarity == STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES) { in stm32_lptim_cnt_function_get()
189 return -EINVAL; in stm32_lptim_cnt_function_get()
192 static int stm32_lptim_cnt_function_set(struct counter_device *counter, in stm32_lptim_cnt_function_set() argument
196 struct stm32_lptim_cnt *const priv = counter->priv; in stm32_lptim_cnt_function_set()
199 return -EBUSY; in stm32_lptim_cnt_function_set()
203 priv->quadrature_mode = 0; in stm32_lptim_cnt_function_set()
206 priv->quadrature_mode = 1; in stm32_lptim_cnt_function_set()
207 priv->polarity = STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES; in stm32_lptim_cnt_function_set()
211 return -EINVAL; in stm32_lptim_cnt_function_set()
215 static ssize_t stm32_lptim_cnt_enable_read(struct counter_device *counter, in stm32_lptim_cnt_enable_read() argument
219 struct stm32_lptim_cnt *const priv = counter->priv; in stm32_lptim_cnt_enable_read()
229 static ssize_t stm32_lptim_cnt_enable_write(struct counter_device *counter, in stm32_lptim_cnt_enable_write() argument
234 struct stm32_lptim_cnt *const priv = counter->priv; in stm32_lptim_cnt_enable_write()
242 /* Check nobody uses the timer, or already disabled/enabled */ in stm32_lptim_cnt_enable_write()
247 return -EBUSY; in stm32_lptim_cnt_enable_write()
260 static ssize_t stm32_lptim_cnt_ceiling_read(struct counter_device *counter, in stm32_lptim_cnt_ceiling_read() argument
264 struct stm32_lptim_cnt *const priv = counter->priv; in stm32_lptim_cnt_ceiling_read()
266 return snprintf(buf, PAGE_SIZE, "%u\n", priv->ceiling); in stm32_lptim_cnt_ceiling_read()
269 static ssize_t stm32_lptim_cnt_ceiling_write(struct counter_device *counter, in stm32_lptim_cnt_ceiling_write() argument
274 struct stm32_lptim_cnt *const priv = counter->priv; in stm32_lptim_cnt_ceiling_write()
279 return -EBUSY; in stm32_lptim_cnt_ceiling_write()
286 return -ERANGE; in stm32_lptim_cnt_ceiling_write()
288 priv->ceiling = ceiling; in stm32_lptim_cnt_ceiling_write()
306 static int stm32_lptim_cnt_action_get(struct counter_device *counter, in stm32_lptim_cnt_action_get() argument
311 struct stm32_lptim_cnt *const priv = counter->priv; in stm32_lptim_cnt_action_get()
315 err = stm32_lptim_cnt_function_get(counter, count, &function); in stm32_lptim_cnt_action_get()
321 /* LP Timer acts as up-counter on input 1 */ in stm32_lptim_cnt_action_get()
322 if (synapse->signal->id == count->synapses[0].signal->id) in stm32_lptim_cnt_action_get()
323 *action = priv->polarity; in stm32_lptim_cnt_action_get()
328 *action = priv->polarity; in stm32_lptim_cnt_action_get()
332 return -EINVAL; in stm32_lptim_cnt_action_get()
336 static int stm32_lptim_cnt_action_set(struct counter_device *counter, in stm32_lptim_cnt_action_set() argument
341 struct stm32_lptim_cnt *const priv = counter->priv; in stm32_lptim_cnt_action_set()
346 return -EBUSY; in stm32_lptim_cnt_action_set()
348 err = stm32_lptim_cnt_function_get(counter, count, &function); in stm32_lptim_cnt_action_set()
352 /* only set polarity when in counter mode (on input 1) */ in stm32_lptim_cnt_action_set()
354 && synapse->signal->id == count->synapses[0].signal->id) { in stm32_lptim_cnt_action_set()
359 priv->polarity = action; in stm32_lptim_cnt_action_set()
364 return -EINVAL; in stm32_lptim_cnt_action_set()
399 /* LP timer with encoder */
411 /* LP timer without encoder (counter only) */
425 struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent); in stm32_lptim_cnt_probe()
429 return -EINVAL; in stm32_lptim_cnt_probe()
431 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); in stm32_lptim_cnt_probe()
433 return -ENOMEM; in stm32_lptim_cnt_probe()
435 priv->dev = &pdev->dev; in stm32_lptim_cnt_probe()
436 priv->regmap = ddata->regmap; in stm32_lptim_cnt_probe()
437 priv->clk = ddata->clk; in stm32_lptim_cnt_probe()
438 priv->ceiling = STM32_LPTIM_MAX_ARR; in stm32_lptim_cnt_probe()
440 /* Initialize Counter device */ in stm32_lptim_cnt_probe()
441 priv->counter.name = dev_name(&pdev->dev); in stm32_lptim_cnt_probe()
442 priv->counter.parent = &pdev->dev; in stm32_lptim_cnt_probe()
443 priv->counter.ops = &stm32_lptim_cnt_ops; in stm32_lptim_cnt_probe()
444 if (ddata->has_encoder) { in stm32_lptim_cnt_probe()
445 priv->counter.counts = &stm32_lptim_enc_counts; in stm32_lptim_cnt_probe()
446 priv->counter.num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals); in stm32_lptim_cnt_probe()
448 priv->counter.counts = &stm32_lptim_in1_counts; in stm32_lptim_cnt_probe()
449 priv->counter.num_signals = 1; in stm32_lptim_cnt_probe()
451 priv->counter.num_counts = 1; in stm32_lptim_cnt_probe()
452 priv->counter.signals = stm32_lptim_cnt_signals; in stm32_lptim_cnt_probe()
453 priv->counter.priv = priv; in stm32_lptim_cnt_probe()
457 return devm_counter_register(&pdev->dev, &priv->counter); in stm32_lptim_cnt_probe()
466 /* Only take care of enabled counter: don't disturb other MFD child */ in stm32_lptim_cnt_suspend()
467 if (priv->enabled) { in stm32_lptim_cnt_suspend()
477 priv->enabled = true; in stm32_lptim_cnt_suspend()
492 if (priv->enabled) { in stm32_lptim_cnt_resume()
493 priv->enabled = false; in stm32_lptim_cnt_resume()
511 { .compatible = "st,stm32-lptimer-counter", },
519 .name = "stm32-lptimer-counter",
527 MODULE_ALIAS("platform:stm32-lptimer-counter");
528 MODULE_DESCRIPTION("STMicroelectronics STM32 LPTIM counter driver");