Skip to content

Commit e892400

Browse files
Fabrice GasnierLee Jones
authored andcommitted
mfd: Add STM32 LPTimer driver
STM32 Low-Power Timer hardware block can be used for: - PWM generation - IIO trigger (in sync with PWM) - IIO quadrature encoder counter PWM and IIO timer configuration are mixed in the same registers so we need a multi fonction driver to be able to share those registers. Signed-off-by: Fabrice Gasnier <[email protected]> Signed-off-by: Lee Jones <[email protected]>
1 parent a7a0db0 commit e892400

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

drivers/mfd/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,20 @@ config MFD_STW481X
17231723
in various ST Microelectronics and ST-Ericsson embedded
17241724
Nomadik series.
17251725

1726+
config MFD_STM32_LPTIMER
1727+
tristate "Support for STM32 Low-Power Timer"
1728+
depends on (ARCH_STM32 && OF) || COMPILE_TEST
1729+
select MFD_CORE
1730+
select REGMAP
1731+
select REGMAP_MMIO
1732+
help
1733+
Select this option to enable STM32 Low-Power Timer driver
1734+
used for PWM, IIO Trigger, IIO Encoder and Counter. Shared
1735+
resources are also dealt with here.
1736+
1737+
To compile this driver as a module, choose M here: the
1738+
module will be called stm32-lptimer.
1739+
17261740
config MFD_STM32_TIMERS
17271741
tristate "Support for STM32 Timers"
17281742
depends on (ARCH_STM32 && OF) || COMPILE_TEST

drivers/mfd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,6 @@ obj-$(CONFIG_MFD_MT6397) += mt6397-core.o
221221
obj-$(CONFIG_MFD_ALTERA_A10SR) += altera-a10sr.o
222222
obj-$(CONFIG_MFD_SUN4I_GPADC) += sun4i-gpadc.o
223223

224+
obj-$(CONFIG_MFD_STM32_LPTIMER) += stm32-lptimer.o
224225
obj-$(CONFIG_MFD_STM32_TIMERS) += stm32-timers.o
225226
obj-$(CONFIG_MFD_MXS_LRADC) += mxs-lradc.o

drivers/mfd/stm32-lptimer.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* STM32 Low-Power Timer parent driver.
3+
*
4+
* Copyright (C) STMicroelectronics 2017
5+
*
6+
* Author: Fabrice Gasnier <[email protected]>
7+
*
8+
* Inspired by Benjamin Gaignard's stm32-timers driver
9+
*
10+
* License terms: GNU General Public License (GPL), version 2
11+
*/
12+
13+
#include <linux/mfd/stm32-lptimer.h>
14+
#include <linux/module.h>
15+
#include <linux/of_platform.h>
16+
17+
#define STM32_LPTIM_MAX_REGISTER 0x3fc
18+
19+
static const struct regmap_config stm32_lptimer_regmap_cfg = {
20+
.reg_bits = 32,
21+
.val_bits = 32,
22+
.reg_stride = sizeof(u32),
23+
.max_register = STM32_LPTIM_MAX_REGISTER,
24+
};
25+
26+
static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
27+
{
28+
u32 val;
29+
int ret;
30+
31+
/*
32+
* Quadrature encoder mode bit can only be written and read back when
33+
* Low-Power Timer supports it.
34+
*/
35+
ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
36+
STM32_LPTIM_ENC, STM32_LPTIM_ENC);
37+
if (ret)
38+
return ret;
39+
40+
ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
41+
if (ret)
42+
return ret;
43+
44+
ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
45+
STM32_LPTIM_ENC, 0);
46+
if (ret)
47+
return ret;
48+
49+
ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
50+
51+
return 0;
52+
}
53+
54+
static int stm32_lptimer_probe(struct platform_device *pdev)
55+
{
56+
struct device *dev = &pdev->dev;
57+
struct stm32_lptimer *ddata;
58+
struct resource *res;
59+
void __iomem *mmio;
60+
int ret;
61+
62+
ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
63+
if (!ddata)
64+
return -ENOMEM;
65+
66+
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
67+
mmio = devm_ioremap_resource(dev, res);
68+
if (IS_ERR(mmio))
69+
return PTR_ERR(mmio);
70+
71+
ddata->regmap = devm_regmap_init_mmio_clk(dev, "mux", mmio,
72+
&stm32_lptimer_regmap_cfg);
73+
if (IS_ERR(ddata->regmap))
74+
return PTR_ERR(ddata->regmap);
75+
76+
ddata->clk = devm_clk_get(dev, NULL);
77+
if (IS_ERR(ddata->clk))
78+
return PTR_ERR(ddata->clk);
79+
80+
ret = stm32_lptimer_detect_encoder(ddata);
81+
if (ret)
82+
return ret;
83+
84+
platform_set_drvdata(pdev, ddata);
85+
86+
return devm_of_platform_populate(&pdev->dev);
87+
}
88+
89+
static const struct of_device_id stm32_lptimer_of_match[] = {
90+
{ .compatible = "st,stm32-lptimer", },
91+
{},
92+
};
93+
MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
94+
95+
static struct platform_driver stm32_lptimer_driver = {
96+
.probe = stm32_lptimer_probe,
97+
.driver = {
98+
.name = "stm32-lptimer",
99+
.of_match_table = stm32_lptimer_of_match,
100+
},
101+
};
102+
module_platform_driver(stm32_lptimer_driver);
103+
104+
MODULE_AUTHOR("Fabrice Gasnier <[email protected]>");
105+
MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
106+
MODULE_ALIAS("platform:stm32-lptimer");
107+
MODULE_LICENSE("GPL v2");

include/linux/mfd/stm32-lptimer.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* STM32 Low-Power Timer parent driver.
3+
*
4+
* Copyright (C) STMicroelectronics 2017
5+
*
6+
* Author: Fabrice Gasnier <[email protected]>
7+
*
8+
* Inspired by Benjamin Gaignard's stm32-timers driver
9+
*
10+
* License terms: GNU General Public License (GPL), version 2
11+
*/
12+
13+
#ifndef _LINUX_STM32_LPTIMER_H_
14+
#define _LINUX_STM32_LPTIMER_H_
15+
16+
#include <linux/clk.h>
17+
#include <linux/regmap.h>
18+
19+
#define STM32_LPTIM_ISR 0x00 /* Interrupt and Status Reg */
20+
#define STM32_LPTIM_ICR 0x04 /* Interrupt Clear Reg */
21+
#define STM32_LPTIM_IER 0x08 /* Interrupt Enable Reg */
22+
#define STM32_LPTIM_CFGR 0x0C /* Configuration Reg */
23+
#define STM32_LPTIM_CR 0x10 /* Control Reg */
24+
#define STM32_LPTIM_CMP 0x14 /* Compare Reg */
25+
#define STM32_LPTIM_ARR 0x18 /* Autoreload Reg */
26+
#define STM32_LPTIM_CNT 0x1C /* Counter Reg */
27+
28+
/* STM32_LPTIM_ISR - bit fields */
29+
#define STM32_LPTIM_CMPOK_ARROK GENMASK(4, 3)
30+
#define STM32_LPTIM_ARROK BIT(4)
31+
#define STM32_LPTIM_CMPOK BIT(3)
32+
33+
/* STM32_LPTIM_ICR - bit fields */
34+
#define STM32_LPTIM_CMPOKCF_ARROKCF GENMASK(4, 3)
35+
36+
/* STM32_LPTIM_CR - bit fields */
37+
#define STM32_LPTIM_CNTSTRT BIT(2)
38+
#define STM32_LPTIM_ENABLE BIT(0)
39+
40+
/* STM32_LPTIM_CFGR - bit fields */
41+
#define STM32_LPTIM_ENC BIT(24)
42+
#define STM32_LPTIM_COUNTMODE BIT(23)
43+
#define STM32_LPTIM_WAVPOL BIT(21)
44+
#define STM32_LPTIM_PRESC GENMASK(11, 9)
45+
#define STM32_LPTIM_CKPOL GENMASK(2, 1)
46+
47+
/* STM32_LPTIM_ARR */
48+
#define STM32_LPTIM_MAX_ARR 0xFFFF
49+
50+
/**
51+
* struct stm32_lptimer - STM32 Low-Power Timer data assigned by parent device
52+
* @clk: clock reference for this instance
53+
* @regmap: register map reference for this instance
54+
* @has_encoder: indicates this Low-Power Timer supports encoder mode
55+
*/
56+
struct stm32_lptimer {
57+
struct clk *clk;
58+
struct regmap *regmap;
59+
bool has_encoder;
60+
};
61+
62+
#endif

0 commit comments

Comments
 (0)