xref: /openbmc/linux/drivers/mfd/stm32-lptimer.c (revision 74ce1896)
1 /*
2  * STM32 Low-Power Timer parent driver.
3  *
4  * Copyright (C) STMicroelectronics 2017
5  *
6  * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
7  *
8  * Inspired by Benjamin Gaignard's stm32-timers driver
9  *
10  * License terms:  GNU General Public License (GPL), version 2
11  */
12 
13 #include <linux/mfd/stm32-lptimer.h>
14 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 
17 #define STM32_LPTIM_MAX_REGISTER	0x3fc
18 
19 static const struct regmap_config stm32_lptimer_regmap_cfg = {
20 	.reg_bits = 32,
21 	.val_bits = 32,
22 	.reg_stride = sizeof(u32),
23 	.max_register = STM32_LPTIM_MAX_REGISTER,
24 };
25 
26 static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
27 {
28 	u32 val;
29 	int ret;
30 
31 	/*
32 	 * Quadrature encoder mode bit can only be written and read back when
33 	 * Low-Power Timer supports it.
34 	 */
35 	ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
36 				 STM32_LPTIM_ENC, STM32_LPTIM_ENC);
37 	if (ret)
38 		return ret;
39 
40 	ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
41 	if (ret)
42 		return ret;
43 
44 	ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
45 				 STM32_LPTIM_ENC, 0);
46 	if (ret)
47 		return ret;
48 
49 	ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
50 
51 	return 0;
52 }
53 
54 static int stm32_lptimer_probe(struct platform_device *pdev)
55 {
56 	struct device *dev = &pdev->dev;
57 	struct stm32_lptimer *ddata;
58 	struct resource *res;
59 	void __iomem *mmio;
60 	int ret;
61 
62 	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
63 	if (!ddata)
64 		return -ENOMEM;
65 
66 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
67 	mmio = devm_ioremap_resource(dev, res);
68 	if (IS_ERR(mmio))
69 		return PTR_ERR(mmio);
70 
71 	ddata->regmap = devm_regmap_init_mmio_clk(dev, "mux", mmio,
72 						  &stm32_lptimer_regmap_cfg);
73 	if (IS_ERR(ddata->regmap))
74 		return PTR_ERR(ddata->regmap);
75 
76 	ddata->clk = devm_clk_get(dev, NULL);
77 	if (IS_ERR(ddata->clk))
78 		return PTR_ERR(ddata->clk);
79 
80 	ret = stm32_lptimer_detect_encoder(ddata);
81 	if (ret)
82 		return ret;
83 
84 	platform_set_drvdata(pdev, ddata);
85 
86 	return devm_of_platform_populate(&pdev->dev);
87 }
88 
89 static const struct of_device_id stm32_lptimer_of_match[] = {
90 	{ .compatible = "st,stm32-lptimer", },
91 	{},
92 };
93 MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
94 
95 static struct platform_driver stm32_lptimer_driver = {
96 	.probe = stm32_lptimer_probe,
97 	.driver = {
98 		.name = "stm32-lptimer",
99 		.of_match_table = stm32_lptimer_of_match,
100 	},
101 };
102 module_platform_driver(stm32_lptimer_driver);
103 
104 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
105 MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
106 MODULE_ALIAS("platform:stm32-lptimer");
107 MODULE_LICENSE("GPL v2");
108