xref: /linux/drivers/mfd/stm32-lptimer.c (revision e78f70bad29c5ae1e1076698b690b15794e9b81e)
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 
17 static 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 	.fast_io = true,
23 };
24 
25 static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
26 {
27 	u32 val;
28 	int ret;
29 
30 	/*
31 	 * Quadrature encoder mode bit can only be written and read back when
32 	 * Low-Power Timer supports it.
33 	 */
34 	ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
35 				 STM32_LPTIM_ENC, STM32_LPTIM_ENC);
36 	if (ret)
37 		return ret;
38 
39 	ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
40 	if (ret)
41 		return ret;
42 
43 	ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
44 				 STM32_LPTIM_ENC, 0);
45 	if (ret)
46 		return ret;
47 
48 	ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
49 
50 	return 0;
51 }
52 
53 static int stm32_lptimer_detect_hwcfgr(struct stm32_lptimer *ddata)
54 {
55 	u32 val;
56 	int ret;
57 
58 	ret = regmap_read(ddata->regmap, STM32_LPTIM_VERR, &ddata->version);
59 	if (ret)
60 		return ret;
61 
62 	/* Try to guess parameters from HWCFGR: e.g. encoder mode (STM32MP15) */
63 	ret = regmap_read(ddata->regmap, STM32_LPTIM_HWCFGR1, &val);
64 	if (ret)
65 		return ret;
66 
67 	/* Fallback to legacy init if HWCFGR isn't present */
68 	if (!val)
69 		return stm32_lptimer_detect_encoder(ddata);
70 
71 	ddata->has_encoder = FIELD_GET(STM32_LPTIM_HWCFGR1_ENCODER, val);
72 
73 	ret = regmap_read(ddata->regmap, STM32_LPTIM_HWCFGR2, &val);
74 	if (ret)
75 		return ret;
76 
77 	/* Number of capture/compare channels */
78 	ddata->num_cc_chans = FIELD_GET(STM32_LPTIM_HWCFGR2_CHAN_NUM, val);
79 
80 	return 0;
81 }
82 
83 static int stm32_lptimer_probe(struct platform_device *pdev)
84 {
85 	struct device *dev = &pdev->dev;
86 	struct stm32_lptimer *ddata;
87 	void __iomem *mmio;
88 	int ret;
89 
90 	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
91 	if (!ddata)
92 		return -ENOMEM;
93 
94 	mmio = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
95 	if (IS_ERR(mmio))
96 		return PTR_ERR(mmio);
97 
98 	ddata->regmap = devm_regmap_init_mmio_clk(dev, "mux", mmio,
99 						  &stm32_lptimer_regmap_cfg);
100 	if (IS_ERR(ddata->regmap))
101 		return PTR_ERR(ddata->regmap);
102 
103 	ddata->clk = devm_clk_get(dev, NULL);
104 	if (IS_ERR(ddata->clk))
105 		return PTR_ERR(ddata->clk);
106 
107 	ret = stm32_lptimer_detect_hwcfgr(ddata);
108 	if (ret)
109 		return ret;
110 
111 	platform_set_drvdata(pdev, ddata);
112 
113 	return devm_of_platform_populate(&pdev->dev);
114 }
115 
116 static const struct of_device_id stm32_lptimer_of_match[] = {
117 	{ .compatible = "st,stm32-lptimer", },
118 	{},
119 };
120 MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
121 
122 static struct platform_driver stm32_lptimer_driver = {
123 	.probe = stm32_lptimer_probe,
124 	.driver = {
125 		.name = "stm32-lptimer",
126 		.of_match_table = stm32_lptimer_of_match,
127 	},
128 };
129 module_platform_driver(stm32_lptimer_driver);
130 
131 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
132 MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
133 MODULE_ALIAS("platform:stm32-lptimer");
134 MODULE_LICENSE("GPL v2");
135