Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / mfd / stm32-lptimer.c
blob746e51a17cc8eafd212e64489e3400114477ba23
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 */
9 #include <linux/mfd/stm32-lptimer.h>
10 #include <linux/module.h>
11 #include <linux/of_platform.h>
13 #define STM32_LPTIM_MAX_REGISTER 0x3fc
15 static const struct regmap_config stm32_lptimer_regmap_cfg = {
16 .reg_bits = 32,
17 .val_bits = 32,
18 .reg_stride = sizeof(u32),
19 .max_register = STM32_LPTIM_MAX_REGISTER,
20 .fast_io = true,
23 static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
25 u32 val;
26 int ret;
29 * Quadrature encoder mode bit can only be written and read back when
30 * Low-Power Timer supports it.
32 ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
33 STM32_LPTIM_ENC, STM32_LPTIM_ENC);
34 if (ret)
35 return ret;
37 ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
38 if (ret)
39 return ret;
41 ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
42 STM32_LPTIM_ENC, 0);
43 if (ret)
44 return ret;
46 ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
48 return 0;
51 static int stm32_lptimer_probe(struct platform_device *pdev)
53 struct device *dev = &pdev->dev;
54 struct stm32_lptimer *ddata;
55 struct resource *res;
56 void __iomem *mmio;
57 int ret;
59 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
60 if (!ddata)
61 return -ENOMEM;
63 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
64 mmio = devm_ioremap_resource(dev, res);
65 if (IS_ERR(mmio))
66 return PTR_ERR(mmio);
68 ddata->regmap = devm_regmap_init_mmio_clk(dev, "mux", mmio,
69 &stm32_lptimer_regmap_cfg);
70 if (IS_ERR(ddata->regmap))
71 return PTR_ERR(ddata->regmap);
73 ddata->clk = devm_clk_get(dev, NULL);
74 if (IS_ERR(ddata->clk))
75 return PTR_ERR(ddata->clk);
77 ret = stm32_lptimer_detect_encoder(ddata);
78 if (ret)
79 return ret;
81 platform_set_drvdata(pdev, ddata);
83 return devm_of_platform_populate(&pdev->dev);
86 static const struct of_device_id stm32_lptimer_of_match[] = {
87 { .compatible = "st,stm32-lptimer", },
88 {},
90 MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
92 static struct platform_driver stm32_lptimer_driver = {
93 .probe = stm32_lptimer_probe,
94 .driver = {
95 .name = "stm32-lptimer",
96 .of_match_table = stm32_lptimer_of_match,
99 module_platform_driver(stm32_lptimer_driver);
101 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
102 MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
103 MODULE_ALIAS("platform:stm32-lptimer");
104 MODULE_LICENSE("GPL v2");