1 // SPDX-License-Identifier: GPL-2.0
3 * STM32 Low-Power Timer Encoder and Counter driver
5 * Copyright (C) STMicroelectronics 2017
7 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
9 * Inspired by 104-quad-8 and stm32-timer-trigger drivers.
13 #include <linux/bitfield.h>
14 #include <linux/counter.h>
15 #include <linux/iio/iio.h>
16 #include <linux/mfd/stm32-lptimer.h>
17 #include <linux/module.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/platform_device.h>
21 struct stm32_lptim_cnt
{
22 struct counter_device counter
;
24 struct regmap
*regmap
;
32 static int stm32_lptim_is_enabled(struct stm32_lptim_cnt
*priv
)
37 ret
= regmap_read(priv
->regmap
, STM32_LPTIM_CR
, &val
);
41 return FIELD_GET(STM32_LPTIM_ENABLE
, val
);
44 static int stm32_lptim_set_enable_state(struct stm32_lptim_cnt
*priv
,
50 val
= FIELD_PREP(STM32_LPTIM_ENABLE
, enable
);
51 ret
= regmap_write(priv
->regmap
, STM32_LPTIM_CR
, val
);
56 clk_disable(priv
->clk
);
57 priv
->enabled
= false;
61 /* LP timer must be enabled before writing CMP & ARR */
62 ret
= regmap_write(priv
->regmap
, STM32_LPTIM_ARR
, priv
->ceiling
);
66 ret
= regmap_write(priv
->regmap
, STM32_LPTIM_CMP
, 0);
70 /* ensure CMP & ARR registers are properly written */
71 ret
= regmap_read_poll_timeout(priv
->regmap
, STM32_LPTIM_ISR
, val
,
72 (val
& STM32_LPTIM_CMPOK_ARROK
),
77 ret
= regmap_write(priv
->regmap
, STM32_LPTIM_ICR
,
78 STM32_LPTIM_CMPOKCF_ARROKCF
);
82 ret
= clk_enable(priv
->clk
);
84 regmap_write(priv
->regmap
, STM32_LPTIM_CR
, 0);
89 /* Start LP timer in continuous mode */
90 return regmap_update_bits(priv
->regmap
, STM32_LPTIM_CR
,
91 STM32_LPTIM_CNTSTRT
, STM32_LPTIM_CNTSTRT
);
94 static int stm32_lptim_setup(struct stm32_lptim_cnt
*priv
, int enable
)
96 u32 mask
= STM32_LPTIM_ENC
| STM32_LPTIM_COUNTMODE
|
97 STM32_LPTIM_CKPOL
| STM32_LPTIM_PRESC
;
100 /* Setup LP timer encoder/counter and polarity, without prescaler */
101 if (priv
->quadrature_mode
)
102 val
= enable
? STM32_LPTIM_ENC
: 0;
104 val
= enable
? STM32_LPTIM_COUNTMODE
: 0;
105 val
|= FIELD_PREP(STM32_LPTIM_CKPOL
, enable
? priv
->polarity
: 0);
107 return regmap_update_bits(priv
->regmap
, STM32_LPTIM_CFGR
, mask
, val
);
110 static int stm32_lptim_write_raw(struct iio_dev
*indio_dev
,
111 struct iio_chan_spec
const *chan
,
112 int val
, int val2
, long mask
)
114 struct stm32_lptim_cnt
*priv
= iio_priv(indio_dev
);
118 case IIO_CHAN_INFO_ENABLE
:
119 if (val
< 0 || val
> 1)
122 /* Check nobody uses the timer, or already disabled/enabled */
123 ret
= stm32_lptim_is_enabled(priv
);
124 if ((ret
< 0) || (!ret
&& !val
))
129 ret
= stm32_lptim_setup(priv
, val
);
132 return stm32_lptim_set_enable_state(priv
, val
);
139 static int stm32_lptim_read_raw(struct iio_dev
*indio_dev
,
140 struct iio_chan_spec
const *chan
,
141 int *val
, int *val2
, long mask
)
143 struct stm32_lptim_cnt
*priv
= iio_priv(indio_dev
);
148 case IIO_CHAN_INFO_RAW
:
149 ret
= regmap_read(priv
->regmap
, STM32_LPTIM_CNT
, &dat
);
155 case IIO_CHAN_INFO_ENABLE
:
156 ret
= stm32_lptim_is_enabled(priv
);
162 case IIO_CHAN_INFO_SCALE
:
163 /* Non-quadrature mode: scale = 1 */
166 if (priv
->quadrature_mode
) {
168 * Quadrature encoder mode:
169 * - both edges, quarter cycle, scale is 0.25
170 * - either rising/falling edge scale is 0.5
172 if (priv
->polarity
> 1)
177 return IIO_VAL_FRACTIONAL_LOG2
;
184 static const struct iio_info stm32_lptim_cnt_iio_info
= {
185 .read_raw
= stm32_lptim_read_raw
,
186 .write_raw
= stm32_lptim_write_raw
,
189 static const char *const stm32_lptim_quadrature_modes
[] = {
194 static int stm32_lptim_get_quadrature_mode(struct iio_dev
*indio_dev
,
195 const struct iio_chan_spec
*chan
)
197 struct stm32_lptim_cnt
*priv
= iio_priv(indio_dev
);
199 return priv
->quadrature_mode
;
202 static int stm32_lptim_set_quadrature_mode(struct iio_dev
*indio_dev
,
203 const struct iio_chan_spec
*chan
,
206 struct stm32_lptim_cnt
*priv
= iio_priv(indio_dev
);
208 if (stm32_lptim_is_enabled(priv
))
211 priv
->quadrature_mode
= type
;
216 static const struct iio_enum stm32_lptim_quadrature_mode_en
= {
217 .items
= stm32_lptim_quadrature_modes
,
218 .num_items
= ARRAY_SIZE(stm32_lptim_quadrature_modes
),
219 .get
= stm32_lptim_get_quadrature_mode
,
220 .set
= stm32_lptim_set_quadrature_mode
,
223 static const char * const stm32_lptim_cnt_polarity
[] = {
224 "rising-edge", "falling-edge", "both-edges",
227 static int stm32_lptim_cnt_get_polarity(struct iio_dev
*indio_dev
,
228 const struct iio_chan_spec
*chan
)
230 struct stm32_lptim_cnt
*priv
= iio_priv(indio_dev
);
232 return priv
->polarity
;
235 static int stm32_lptim_cnt_set_polarity(struct iio_dev
*indio_dev
,
236 const struct iio_chan_spec
*chan
,
239 struct stm32_lptim_cnt
*priv
= iio_priv(indio_dev
);
241 if (stm32_lptim_is_enabled(priv
))
244 priv
->polarity
= type
;
249 static const struct iio_enum stm32_lptim_cnt_polarity_en
= {
250 .items
= stm32_lptim_cnt_polarity
,
251 .num_items
= ARRAY_SIZE(stm32_lptim_cnt_polarity
),
252 .get
= stm32_lptim_cnt_get_polarity
,
253 .set
= stm32_lptim_cnt_set_polarity
,
256 static ssize_t
stm32_lptim_cnt_get_ceiling(struct stm32_lptim_cnt
*priv
,
259 return snprintf(buf
, PAGE_SIZE
, "%u\n", priv
->ceiling
);
262 static ssize_t
stm32_lptim_cnt_set_ceiling(struct stm32_lptim_cnt
*priv
,
263 const char *buf
, size_t len
)
267 if (stm32_lptim_is_enabled(priv
))
270 ret
= kstrtouint(buf
, 0, &priv
->ceiling
);
274 if (priv
->ceiling
> STM32_LPTIM_MAX_ARR
)
280 static ssize_t
stm32_lptim_cnt_get_preset_iio(struct iio_dev
*indio_dev
,
282 const struct iio_chan_spec
*chan
,
285 struct stm32_lptim_cnt
*priv
= iio_priv(indio_dev
);
287 return stm32_lptim_cnt_get_ceiling(priv
, buf
);
290 static ssize_t
stm32_lptim_cnt_set_preset_iio(struct iio_dev
*indio_dev
,
292 const struct iio_chan_spec
*chan
,
293 const char *buf
, size_t len
)
295 struct stm32_lptim_cnt
*priv
= iio_priv(indio_dev
);
297 return stm32_lptim_cnt_set_ceiling(priv
, buf
, len
);
300 /* LP timer with encoder */
301 static const struct iio_chan_spec_ext_info stm32_lptim_enc_ext_info
[] = {
304 .shared
= IIO_SEPARATE
,
305 .read
= stm32_lptim_cnt_get_preset_iio
,
306 .write
= stm32_lptim_cnt_set_preset_iio
,
308 IIO_ENUM("polarity", IIO_SEPARATE
, &stm32_lptim_cnt_polarity_en
),
309 IIO_ENUM_AVAILABLE("polarity", &stm32_lptim_cnt_polarity_en
),
310 IIO_ENUM("quadrature_mode", IIO_SEPARATE
,
311 &stm32_lptim_quadrature_mode_en
),
312 IIO_ENUM_AVAILABLE("quadrature_mode", &stm32_lptim_quadrature_mode_en
),
316 static const struct iio_chan_spec stm32_lptim_enc_channels
= {
319 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
320 BIT(IIO_CHAN_INFO_ENABLE
) |
321 BIT(IIO_CHAN_INFO_SCALE
),
322 .ext_info
= stm32_lptim_enc_ext_info
,
326 /* LP timer without encoder (counter only) */
327 static const struct iio_chan_spec_ext_info stm32_lptim_cnt_ext_info
[] = {
330 .shared
= IIO_SEPARATE
,
331 .read
= stm32_lptim_cnt_get_preset_iio
,
332 .write
= stm32_lptim_cnt_set_preset_iio
,
334 IIO_ENUM("polarity", IIO_SEPARATE
, &stm32_lptim_cnt_polarity_en
),
335 IIO_ENUM_AVAILABLE("polarity", &stm32_lptim_cnt_polarity_en
),
339 static const struct iio_chan_spec stm32_lptim_cnt_channels
= {
342 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
343 BIT(IIO_CHAN_INFO_ENABLE
) |
344 BIT(IIO_CHAN_INFO_SCALE
),
345 .ext_info
= stm32_lptim_cnt_ext_info
,
350 * enum stm32_lptim_cnt_function - enumerates LPTimer counter & encoder modes
351 * @STM32_LPTIM_COUNTER_INCREASE: up count on IN1 rising, falling or both edges
352 * @STM32_LPTIM_ENCODER_BOTH_EDGE: count on both edges (IN1 & IN2 quadrature)
354 enum stm32_lptim_cnt_function
{
355 STM32_LPTIM_COUNTER_INCREASE
,
356 STM32_LPTIM_ENCODER_BOTH_EDGE
,
359 static enum counter_count_function stm32_lptim_cnt_functions
[] = {
360 [STM32_LPTIM_COUNTER_INCREASE
] = COUNTER_COUNT_FUNCTION_INCREASE
,
361 [STM32_LPTIM_ENCODER_BOTH_EDGE
] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4
,
364 enum stm32_lptim_synapse_action
{
365 STM32_LPTIM_SYNAPSE_ACTION_RISING_EDGE
,
366 STM32_LPTIM_SYNAPSE_ACTION_FALLING_EDGE
,
367 STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES
,
368 STM32_LPTIM_SYNAPSE_ACTION_NONE
,
371 static enum counter_synapse_action stm32_lptim_cnt_synapse_actions
[] = {
372 /* Index must match with stm32_lptim_cnt_polarity[] (priv->polarity) */
373 [STM32_LPTIM_SYNAPSE_ACTION_RISING_EDGE
] = COUNTER_SYNAPSE_ACTION_RISING_EDGE
,
374 [STM32_LPTIM_SYNAPSE_ACTION_FALLING_EDGE
] = COUNTER_SYNAPSE_ACTION_FALLING_EDGE
,
375 [STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES
] = COUNTER_SYNAPSE_ACTION_BOTH_EDGES
,
376 [STM32_LPTIM_SYNAPSE_ACTION_NONE
] = COUNTER_SYNAPSE_ACTION_NONE
,
379 static int stm32_lptim_cnt_read(struct counter_device
*counter
,
380 struct counter_count
*count
, unsigned long *val
)
382 struct stm32_lptim_cnt
*const priv
= counter
->priv
;
386 ret
= regmap_read(priv
->regmap
, STM32_LPTIM_CNT
, &cnt
);
395 static int stm32_lptim_cnt_function_get(struct counter_device
*counter
,
396 struct counter_count
*count
,
399 struct stm32_lptim_cnt
*const priv
= counter
->priv
;
401 if (!priv
->quadrature_mode
) {
402 *function
= STM32_LPTIM_COUNTER_INCREASE
;
406 if (priv
->polarity
== STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES
) {
407 *function
= STM32_LPTIM_ENCODER_BOTH_EDGE
;
414 static int stm32_lptim_cnt_function_set(struct counter_device
*counter
,
415 struct counter_count
*count
,
418 struct stm32_lptim_cnt
*const priv
= counter
->priv
;
420 if (stm32_lptim_is_enabled(priv
))
424 case STM32_LPTIM_COUNTER_INCREASE
:
425 priv
->quadrature_mode
= 0;
427 case STM32_LPTIM_ENCODER_BOTH_EDGE
:
428 priv
->quadrature_mode
= 1;
429 priv
->polarity
= STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES
;
436 static ssize_t
stm32_lptim_cnt_enable_read(struct counter_device
*counter
,
437 struct counter_count
*count
,
438 void *private, char *buf
)
440 struct stm32_lptim_cnt
*const priv
= counter
->priv
;
443 ret
= stm32_lptim_is_enabled(priv
);
447 return scnprintf(buf
, PAGE_SIZE
, "%u\n", ret
);
450 static ssize_t
stm32_lptim_cnt_enable_write(struct counter_device
*counter
,
451 struct counter_count
*count
,
453 const char *buf
, size_t len
)
455 struct stm32_lptim_cnt
*const priv
= counter
->priv
;
459 ret
= kstrtobool(buf
, &enable
);
463 /* Check nobody uses the timer, or already disabled/enabled */
464 ret
= stm32_lptim_is_enabled(priv
);
465 if ((ret
< 0) || (!ret
&& !enable
))
470 ret
= stm32_lptim_setup(priv
, enable
);
474 ret
= stm32_lptim_set_enable_state(priv
, enable
);
481 static ssize_t
stm32_lptim_cnt_ceiling_read(struct counter_device
*counter
,
482 struct counter_count
*count
,
483 void *private, char *buf
)
485 struct stm32_lptim_cnt
*const priv
= counter
->priv
;
487 return stm32_lptim_cnt_get_ceiling(priv
, buf
);
490 static ssize_t
stm32_lptim_cnt_ceiling_write(struct counter_device
*counter
,
491 struct counter_count
*count
,
493 const char *buf
, size_t len
)
495 struct stm32_lptim_cnt
*const priv
= counter
->priv
;
497 return stm32_lptim_cnt_set_ceiling(priv
, buf
, len
);
500 static const struct counter_count_ext stm32_lptim_cnt_ext
[] = {
503 .read
= stm32_lptim_cnt_enable_read
,
504 .write
= stm32_lptim_cnt_enable_write
508 .read
= stm32_lptim_cnt_ceiling_read
,
509 .write
= stm32_lptim_cnt_ceiling_write
513 static int stm32_lptim_cnt_action_get(struct counter_device
*counter
,
514 struct counter_count
*count
,
515 struct counter_synapse
*synapse
,
518 struct stm32_lptim_cnt
*const priv
= counter
->priv
;
522 err
= stm32_lptim_cnt_function_get(counter
, count
, &function
);
527 case STM32_LPTIM_COUNTER_INCREASE
:
528 /* LP Timer acts as up-counter on input 1 */
529 if (synapse
->signal
->id
== count
->synapses
[0].signal
->id
)
530 *action
= priv
->polarity
;
532 *action
= STM32_LPTIM_SYNAPSE_ACTION_NONE
;
534 case STM32_LPTIM_ENCODER_BOTH_EDGE
:
535 *action
= priv
->polarity
;
542 static int stm32_lptim_cnt_action_set(struct counter_device
*counter
,
543 struct counter_count
*count
,
544 struct counter_synapse
*synapse
,
547 struct stm32_lptim_cnt
*const priv
= counter
->priv
;
551 if (stm32_lptim_is_enabled(priv
))
554 err
= stm32_lptim_cnt_function_get(counter
, count
, &function
);
558 /* only set polarity when in counter mode (on input 1) */
559 if (function
== STM32_LPTIM_COUNTER_INCREASE
560 && synapse
->signal
->id
== count
->synapses
[0].signal
->id
) {
562 case STM32_LPTIM_SYNAPSE_ACTION_RISING_EDGE
:
563 case STM32_LPTIM_SYNAPSE_ACTION_FALLING_EDGE
:
564 case STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES
:
565 priv
->polarity
= action
;
573 static const struct counter_ops stm32_lptim_cnt_ops
= {
574 .count_read
= stm32_lptim_cnt_read
,
575 .function_get
= stm32_lptim_cnt_function_get
,
576 .function_set
= stm32_lptim_cnt_function_set
,
577 .action_get
= stm32_lptim_cnt_action_get
,
578 .action_set
= stm32_lptim_cnt_action_set
,
581 static struct counter_signal stm32_lptim_cnt_signals
[] = {
584 .name
= "Channel 1 Quadrature A"
588 .name
= "Channel 1 Quadrature B"
592 static struct counter_synapse stm32_lptim_cnt_synapses
[] = {
594 .actions_list
= stm32_lptim_cnt_synapse_actions
,
595 .num_actions
= ARRAY_SIZE(stm32_lptim_cnt_synapse_actions
),
596 .signal
= &stm32_lptim_cnt_signals
[0]
599 .actions_list
= stm32_lptim_cnt_synapse_actions
,
600 .num_actions
= ARRAY_SIZE(stm32_lptim_cnt_synapse_actions
),
601 .signal
= &stm32_lptim_cnt_signals
[1]
605 /* LP timer with encoder */
606 static struct counter_count stm32_lptim_enc_counts
= {
608 .name
= "LPTimer Count",
609 .functions_list
= stm32_lptim_cnt_functions
,
610 .num_functions
= ARRAY_SIZE(stm32_lptim_cnt_functions
),
611 .synapses
= stm32_lptim_cnt_synapses
,
612 .num_synapses
= ARRAY_SIZE(stm32_lptim_cnt_synapses
),
613 .ext
= stm32_lptim_cnt_ext
,
614 .num_ext
= ARRAY_SIZE(stm32_lptim_cnt_ext
)
617 /* LP timer without encoder (counter only) */
618 static struct counter_count stm32_lptim_in1_counts
= {
620 .name
= "LPTimer Count",
621 .functions_list
= stm32_lptim_cnt_functions
,
623 .synapses
= stm32_lptim_cnt_synapses
,
625 .ext
= stm32_lptim_cnt_ext
,
626 .num_ext
= ARRAY_SIZE(stm32_lptim_cnt_ext
)
629 static int stm32_lptim_cnt_probe(struct platform_device
*pdev
)
631 struct stm32_lptimer
*ddata
= dev_get_drvdata(pdev
->dev
.parent
);
632 struct stm32_lptim_cnt
*priv
;
633 struct iio_dev
*indio_dev
;
636 if (IS_ERR_OR_NULL(ddata
))
639 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*priv
));
643 priv
= iio_priv(indio_dev
);
644 priv
->dev
= &pdev
->dev
;
645 priv
->regmap
= ddata
->regmap
;
646 priv
->clk
= ddata
->clk
;
647 priv
->ceiling
= STM32_LPTIM_MAX_ARR
;
649 /* Initialize IIO device */
650 indio_dev
->name
= dev_name(&pdev
->dev
);
651 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
652 indio_dev
->info
= &stm32_lptim_cnt_iio_info
;
653 if (ddata
->has_encoder
)
654 indio_dev
->channels
= &stm32_lptim_enc_channels
;
656 indio_dev
->channels
= &stm32_lptim_cnt_channels
;
657 indio_dev
->num_channels
= 1;
659 /* Initialize Counter device */
660 priv
->counter
.name
= dev_name(&pdev
->dev
);
661 priv
->counter
.parent
= &pdev
->dev
;
662 priv
->counter
.ops
= &stm32_lptim_cnt_ops
;
663 if (ddata
->has_encoder
) {
664 priv
->counter
.counts
= &stm32_lptim_enc_counts
;
665 priv
->counter
.num_signals
= ARRAY_SIZE(stm32_lptim_cnt_signals
);
667 priv
->counter
.counts
= &stm32_lptim_in1_counts
;
668 priv
->counter
.num_signals
= 1;
670 priv
->counter
.num_counts
= 1;
671 priv
->counter
.signals
= stm32_lptim_cnt_signals
;
672 priv
->counter
.priv
= priv
;
674 platform_set_drvdata(pdev
, priv
);
676 ret
= devm_iio_device_register(&pdev
->dev
, indio_dev
);
680 return devm_counter_register(&pdev
->dev
, &priv
->counter
);
683 #ifdef CONFIG_PM_SLEEP
684 static int stm32_lptim_cnt_suspend(struct device
*dev
)
686 struct stm32_lptim_cnt
*priv
= dev_get_drvdata(dev
);
689 /* Only take care of enabled counter: don't disturb other MFD child */
691 ret
= stm32_lptim_setup(priv
, 0);
695 ret
= stm32_lptim_set_enable_state(priv
, 0);
699 /* Force enable state for later resume */
700 priv
->enabled
= true;
703 return pinctrl_pm_select_sleep_state(dev
);
706 static int stm32_lptim_cnt_resume(struct device
*dev
)
708 struct stm32_lptim_cnt
*priv
= dev_get_drvdata(dev
);
711 ret
= pinctrl_pm_select_default_state(dev
);
716 priv
->enabled
= false;
717 ret
= stm32_lptim_setup(priv
, 1);
721 ret
= stm32_lptim_set_enable_state(priv
, 1);
730 static SIMPLE_DEV_PM_OPS(stm32_lptim_cnt_pm_ops
, stm32_lptim_cnt_suspend
,
731 stm32_lptim_cnt_resume
);
733 static const struct of_device_id stm32_lptim_cnt_of_match
[] = {
734 { .compatible
= "st,stm32-lptimer-counter", },
737 MODULE_DEVICE_TABLE(of
, stm32_lptim_cnt_of_match
);
739 static struct platform_driver stm32_lptim_cnt_driver
= {
740 .probe
= stm32_lptim_cnt_probe
,
742 .name
= "stm32-lptimer-counter",
743 .of_match_table
= stm32_lptim_cnt_of_match
,
744 .pm
= &stm32_lptim_cnt_pm_ops
,
747 module_platform_driver(stm32_lptim_cnt_driver
);
749 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
750 MODULE_ALIAS("platform:stm32-lptimer-counter");
751 MODULE_DESCRIPTION("STMicroelectronics STM32 LPTIM counter driver");
752 MODULE_LICENSE("GPL v2");