Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * STM32 Low-Power Timer parent driver.
4 * Copyright (C) STMicroelectronics 2017
5 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
6 * Inspired by Benjamin Gaignard's stm32-timers driver
7 */
8
9#include <linux/bitfield.h>
10#include <linux/mfd/stm32-lptimer.h>
11#include <linux/module.h>
12#include <linux/of_platform.h>
13#include <linux/platform_device.h>
14
15#define STM32_LPTIM_MAX_REGISTER 0x3fc
16
17static const struct regmap_config stm32_lptimer_regmap_cfg = {
18 .reg_bits = 32,
19 .val_bits = 32,
20 .reg_stride = sizeof(u32),
21 .max_register = STM32_LPTIM_MAX_REGISTER,
22};
23
24static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
25{
26 u32 val;
27 int ret;
28
29 /*
30 * Quadrature encoder mode bit can only be written and read back when
31 * Low-Power Timer supports it.
32 */
33 ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
34 STM32_LPTIM_ENC, STM32_LPTIM_ENC);
35 if (ret)
36 return ret;
37
38 ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
39 if (ret)
40 return ret;
41
42 ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
43 STM32_LPTIM_ENC, 0);
44 if (ret)
45 return ret;
46
47 ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
48
49 return 0;
50}
51
52static int stm32_lptimer_detect_hwcfgr(struct stm32_lptimer *ddata)
53{
54 u32 val;
55 int ret;
56
57 ret = regmap_read(ddata->regmap, STM32_LPTIM_VERR, &ddata->version);
58 if (ret)
59 return ret;
60
61 /* Try to guess parameters from HWCFGR: e.g. encoder mode (STM32MP15) */
62 ret = regmap_read(ddata->regmap, STM32_LPTIM_HWCFGR1, &val);
63 if (ret)
64 return ret;
65
66 /* Fallback to legacy init if HWCFGR isn't present */
67 if (!val)
68 return stm32_lptimer_detect_encoder(ddata);
69
70 ddata->has_encoder = FIELD_GET(STM32_LPTIM_HWCFGR1_ENCODER, val);
71
72 ret = regmap_read(ddata->regmap, STM32_LPTIM_HWCFGR2, &val);
73 if (ret)
74 return ret;
75
76 /* Number of capture/compare channels */
77 ddata->num_cc_chans = FIELD_GET(STM32_LPTIM_HWCFGR2_CHAN_NUM, val);
78
79 return 0;
80}
81
82static int stm32_lptimer_probe(struct platform_device *pdev)
83{
84 struct device *dev = &pdev->dev;
85 struct stm32_lptimer *ddata;
86 void __iomem *mmio;
87 int ret;
88
89 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
90 if (!ddata)
91 return -ENOMEM;
92
93 mmio = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
94 if (IS_ERR(mmio))
95 return PTR_ERR(mmio);
96
97 ddata->regmap = devm_regmap_init_mmio_clk(dev, "mux", mmio,
98 &stm32_lptimer_regmap_cfg);
99 if (IS_ERR(ddata->regmap))
100 return PTR_ERR(ddata->regmap);
101
102 ddata->clk = devm_clk_get(dev, NULL);
103 if (IS_ERR(ddata->clk))
104 return PTR_ERR(ddata->clk);
105
106 ret = stm32_lptimer_detect_hwcfgr(ddata);
107 if (ret)
108 return ret;
109
110 platform_set_drvdata(pdev, ddata);
111
112 return devm_of_platform_populate(&pdev->dev);
113}
114
115static const struct of_device_id stm32_lptimer_of_match[] = {
116 { .compatible = "st,stm32-lptimer", },
117 {},
118};
119MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
120
121static struct platform_driver stm32_lptimer_driver = {
122 .probe = stm32_lptimer_probe,
123 .driver = {
124 .name = "stm32-lptimer",
125 .of_match_table = stm32_lptimer_of_match,
126 },
127};
128module_platform_driver(stm32_lptimer_driver);
129
130MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
131MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
132MODULE_ALIAS("platform:stm32-lptimer");
133MODULE_LICENSE("GPL v2");