Merge tag 'trace-v6.2-1' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux...
[linux-2.6-block.git] / drivers / mfd / stm32-lptimer.c
CommitLineData
fa93f5b7 1// SPDX-License-Identifier: GPL-2.0
e8924005
FG
2/*
3 * STM32 Low-Power Timer parent driver.
e8924005 4 * Copyright (C) STMicroelectronics 2017
e8924005 5 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
e8924005 6 * Inspired by Benjamin Gaignard's stm32-timers driver
e8924005
FG
7 */
8
9#include <linux/mfd/stm32-lptimer.h>
10#include <linux/module.h>
11#include <linux/of_platform.h>
12
13#define STM32_LPTIM_MAX_REGISTER 0x3fc
14
15static 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,
45d93065 20 .fast_io = true,
e8924005
FG
21};
22
23static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
24{
25 u32 val;
26 int ret;
27
28 /*
29 * Quadrature encoder mode bit can only be written and read back when
30 * Low-Power Timer supports it.
31 */
32 ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
33 STM32_LPTIM_ENC, STM32_LPTIM_ENC);
34 if (ret)
35 return ret;
36
37 ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
38 if (ret)
39 return ret;
40
41 ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
42 STM32_LPTIM_ENC, 0);
43 if (ret)
44 return ret;
45
46 ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
47
48 return 0;
49}
50
51static int stm32_lptimer_probe(struct platform_device *pdev)
52{
53 struct device *dev = &pdev->dev;
54 struct stm32_lptimer *ddata;
e8924005
FG
55 void __iomem *mmio;
56 int ret;
57
58 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
59 if (!ddata)
60 return -ENOMEM;
61
f359c3e5 62 mmio = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
e8924005
FG
63 if (IS_ERR(mmio))
64 return PTR_ERR(mmio);
65
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);
70
71 ddata->clk = devm_clk_get(dev, NULL);
72 if (IS_ERR(ddata->clk))
73 return PTR_ERR(ddata->clk);
74
75 ret = stm32_lptimer_detect_encoder(ddata);
76 if (ret)
77 return ret;
78
79 platform_set_drvdata(pdev, ddata);
80
81 return devm_of_platform_populate(&pdev->dev);
82}
83
84static const struct of_device_id stm32_lptimer_of_match[] = {
85 { .compatible = "st,stm32-lptimer", },
86 {},
87};
88MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
89
90static struct platform_driver stm32_lptimer_driver = {
91 .probe = stm32_lptimer_probe,
92 .driver = {
93 .name = "stm32-lptimer",
94 .of_match_table = stm32_lptimer_of_match,
95 },
96};
97module_platform_driver(stm32_lptimer_driver);
98
99MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
100MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
101MODULE_ALIAS("platform:stm32-lptimer");
102MODULE_LICENSE("GPL v2");