Skip to content

Commit b01ced2

Browse files
Fabrice GasnierLee Jones
authored andcommitted
iio: trigger: Add STM32 LPTimer trigger driver
Add support for LPTIMx_OUT triggers that can be found on some STM32 devices. These triggers can be used then by ADC or DAC. Typical usage is to configure LPTimer as PWM output (via pwm-stm32-lp) and have synchronised analog conversions with these triggers. Signed-off-by: Fabrice Gasnier <fabrice.gasnier@st.com> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Lee Jones <lee.jones@linaro.org>
1 parent 5dae3c9 commit b01ced2

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

drivers/iio/trigger/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ config IIO_INTERRUPT_TRIGGER
2424
To compile this driver as a module, choose M here: the
2525
module will be called iio-trig-interrupt.
2626

27+
config IIO_STM32_LPTIMER_TRIGGER
28+
tristate "STM32 Low-Power Timer Trigger"
29+
depends on MFD_STM32_LPTIMER || COMPILE_TEST
30+
help
31+
Select this option to enable STM32 Low-Power Timer Trigger.
32+
This can be used as trigger source for STM32 internal ADC
33+
and/or DAC.
34+
35+
To compile this driver as a module, choose M here: the
36+
module will be called stm32-lptimer-trigger.
37+
2738
config IIO_STM32_TIMER_TRIGGER
2839
tristate "STM32 Timer Trigger"
2940
depends on (ARCH_STM32 && OF && MFD_STM32_TIMERS) || COMPILE_TEST

drivers/iio/trigger/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
obj-$(CONFIG_IIO_HRTIMER_TRIGGER) += iio-trig-hrtimer.o
88
obj-$(CONFIG_IIO_INTERRUPT_TRIGGER) += iio-trig-interrupt.o
9+
obj-$(CONFIG_IIO_STM32_LPTIMER_TRIGGER) += stm32-lptimer-trigger.o
910
obj-$(CONFIG_IIO_STM32_TIMER_TRIGGER) += stm32-timer-trigger.o
1011
obj-$(CONFIG_IIO_SYSFS_TRIGGER) += iio-trig-sysfs.o
1112
obj-$(CONFIG_IIO_TIGHTLOOP_TRIGGER) += iio-trig-loop.o
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* STM32 Low-Power Timer Trigger driver
3+
*
4+
* Copyright (C) STMicroelectronics 2017
5+
*
6+
* Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
7+
*
8+
* License terms: GNU General Public License (GPL), version 2
9+
*
10+
* Inspired by Benjamin Gaignard's stm32-timer-trigger driver
11+
*/
12+
13+
#include <linux/iio/timer/stm32-lptim-trigger.h>
14+
#include <linux/mfd/stm32-lptimer.h>
15+
#include <linux/module.h>
16+
#include <linux/platform_device.h>
17+
18+
/* List Low-Power Timer triggers */
19+
static const char * const stm32_lptim_triggers[] = {
20+
LPTIM1_OUT,
21+
LPTIM2_OUT,
22+
LPTIM3_OUT,
23+
};
24+
25+
struct stm32_lptim_trigger {
26+
struct device *dev;
27+
const char *trg;
28+
};
29+
30+
static int stm32_lptim_validate_device(struct iio_trigger *trig,
31+
struct iio_dev *indio_dev)
32+
{
33+
if (indio_dev->modes & INDIO_HARDWARE_TRIGGERED)
34+
return 0;
35+
36+
return -EINVAL;
37+
}
38+
39+
static const struct iio_trigger_ops stm32_lptim_trigger_ops = {
40+
.owner = THIS_MODULE,
41+
.validate_device = stm32_lptim_validate_device,
42+
};
43+
44+
/**
45+
* is_stm32_lptim_trigger
46+
* @trig: trigger to be checked
47+
*
48+
* return true if the trigger is a valid STM32 IIO Low-Power Timer Trigger
49+
* either return false
50+
*/
51+
bool is_stm32_lptim_trigger(struct iio_trigger *trig)
52+
{
53+
return (trig->ops == &stm32_lptim_trigger_ops);
54+
}
55+
EXPORT_SYMBOL(is_stm32_lptim_trigger);
56+
57+
static int stm32_lptim_setup_trig(struct stm32_lptim_trigger *priv)
58+
{
59+
struct iio_trigger *trig;
60+
61+
trig = devm_iio_trigger_alloc(priv->dev, "%s", priv->trg);
62+
if (!trig)
63+
return -ENOMEM;
64+
65+
trig->dev.parent = priv->dev->parent;
66+
trig->ops = &stm32_lptim_trigger_ops;
67+
iio_trigger_set_drvdata(trig, priv);
68+
69+
return devm_iio_trigger_register(priv->dev, trig);
70+
}
71+
72+
static int stm32_lptim_trigger_probe(struct platform_device *pdev)
73+
{
74+
struct stm32_lptim_trigger *priv;
75+
u32 index;
76+
int ret;
77+
78+
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
79+
if (!priv)
80+
return -ENOMEM;
81+
82+
if (of_property_read_u32(pdev->dev.of_node, "reg", &index))
83+
return -EINVAL;
84+
85+
if (index >= ARRAY_SIZE(stm32_lptim_triggers))
86+
return -EINVAL;
87+
88+
priv->dev = &pdev->dev;
89+
priv->trg = stm32_lptim_triggers[index];
90+
91+
ret = stm32_lptim_setup_trig(priv);
92+
if (ret)
93+
return ret;
94+
95+
platform_set_drvdata(pdev, priv);
96+
97+
return 0;
98+
}
99+
100+
static const struct of_device_id stm32_lptim_trig_of_match[] = {
101+
{ .compatible = "st,stm32-lptimer-trigger", },
102+
{},
103+
};
104+
MODULE_DEVICE_TABLE(of, stm32_lptim_trig_of_match);
105+
106+
static struct platform_driver stm32_lptim_trigger_driver = {
107+
.probe = stm32_lptim_trigger_probe,
108+
.driver = {
109+
.name = "stm32-lptimer-trigger",
110+
.of_match_table = stm32_lptim_trig_of_match,
111+
},
112+
};
113+
module_platform_driver(stm32_lptim_trigger_driver);
114+
115+
MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
116+
MODULE_ALIAS("platform:stm32-lptimer-trigger");
117+
MODULE_DESCRIPTION("STMicroelectronics STM32 LPTIM trigger driver");
118+
MODULE_LICENSE("GPL v2");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) STMicroelectronics 2017
3+
*
4+
* Author: Fabrice Gasnier <fabrice.gasnier@st.com>
5+
*
6+
* License terms: GNU General Public License (GPL), version 2
7+
*/
8+
9+
#ifndef _STM32_LPTIM_TRIGGER_H_
10+
#define _STM32_LPTIM_TRIGGER_H_
11+
12+
#include <linux/iio/iio.h>
13+
#include <linux/iio/trigger.h>
14+
15+
#define LPTIM1_OUT "lptim1_out"
16+
#define LPTIM2_OUT "lptim2_out"
17+
#define LPTIM3_OUT "lptim3_out"
18+
19+
#if IS_ENABLED(CONFIG_IIO_STM32_LPTIMER_TRIGGER)
20+
bool is_stm32_lptim_trigger(struct iio_trigger *trig);
21+
#else
22+
static inline bool is_stm32_lptim_trigger(struct iio_trigger *trig)
23+
{
24+
return false;
25+
}
26+
#endif
27+
#endif

0 commit comments

Comments
 (0)