1 // SPDX-License-Identifier: GPL-2.0
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
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 = {
18 .reg_stride = sizeof(u32),
19 .max_register = STM32_LPTIM_MAX_REGISTER,
23 static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
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);
37 ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
41 ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
46 ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
51 static int stm32_lptimer_probe(struct platform_device *pdev)
53 struct device *dev = &pdev->dev;
54 struct stm32_lptimer *ddata;
58 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
62 mmio = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
66 ddata->regmap = devm_regmap_init_mmio_clk(dev, "mux", mmio,
67 &stm32_lptimer_regmap_cfg);
68 if (IS_ERR(ddata->regmap))
69 return PTR_ERR(ddata->regmap);
71 ddata->clk = devm_clk_get(dev, NULL);
72 if (IS_ERR(ddata->clk))
73 return PTR_ERR(ddata->clk);
75 ret = stm32_lptimer_detect_encoder(ddata);
79 platform_set_drvdata(pdev, ddata);
81 return devm_of_platform_populate(&pdev->dev);
84 static const struct of_device_id stm32_lptimer_of_match[] = {
85 { .compatible = "st,stm32-lptimer", },
88 MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
90 static struct platform_driver stm32_lptimer_driver = {
91 .probe = stm32_lptimer_probe,
93 .name = "stm32-lptimer",
94 .of_match_table = stm32_lptimer_of_match,
97 module_platform_driver(stm32_lptimer_driver);
99 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
100 MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
101 MODULE_ALIAS("platform:stm32-lptimer");
102 MODULE_LICENSE("GPL v2");