xref: /openbmc/linux/drivers/mfd/stm32-timers.c (revision 6aa7de05)
1 /*
2  * Copyright (C) STMicroelectronics 2016
3  *
4  * Author: Benjamin Gaignard <benjamin.gaignard@st.com>
5  *
6  * License terms:  GNU General Public License (GPL), version 2
7  */
8 
9 #include <linux/mfd/stm32-timers.h>
10 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 #include <linux/reset.h>
13 
14 static const struct regmap_config stm32_timers_regmap_cfg = {
15 	.reg_bits = 32,
16 	.val_bits = 32,
17 	.reg_stride = sizeof(u32),
18 	.max_register = 0x3fc,
19 };
20 
21 static void stm32_timers_get_arr_size(struct stm32_timers *ddata)
22 {
23 	/*
24 	 * Only the available bits will be written so when readback
25 	 * we get the maximum value of auto reload register
26 	 */
27 	regmap_write(ddata->regmap, TIM_ARR, ~0L);
28 	regmap_read(ddata->regmap, TIM_ARR, &ddata->max_arr);
29 	regmap_write(ddata->regmap, TIM_ARR, 0x0);
30 }
31 
32 static int stm32_timers_probe(struct platform_device *pdev)
33 {
34 	struct device *dev = &pdev->dev;
35 	struct stm32_timers *ddata;
36 	struct resource *res;
37 	void __iomem *mmio;
38 
39 	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
40 	if (!ddata)
41 		return -ENOMEM;
42 
43 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
44 	mmio = devm_ioremap_resource(dev, res);
45 	if (IS_ERR(mmio))
46 		return PTR_ERR(mmio);
47 
48 	ddata->regmap = devm_regmap_init_mmio_clk(dev, "int", mmio,
49 						  &stm32_timers_regmap_cfg);
50 	if (IS_ERR(ddata->regmap))
51 		return PTR_ERR(ddata->regmap);
52 
53 	ddata->clk = devm_clk_get(dev, NULL);
54 	if (IS_ERR(ddata->clk))
55 		return PTR_ERR(ddata->clk);
56 
57 	stm32_timers_get_arr_size(ddata);
58 
59 	platform_set_drvdata(pdev, ddata);
60 
61 	return devm_of_platform_populate(&pdev->dev);
62 }
63 
64 static const struct of_device_id stm32_timers_of_match[] = {
65 	{ .compatible = "st,stm32-timers", },
66 	{ /* end node */ },
67 };
68 MODULE_DEVICE_TABLE(of, stm32_timers_of_match);
69 
70 static struct platform_driver stm32_timers_driver = {
71 	.probe = stm32_timers_probe,
72 	.driver	= {
73 		.name = "stm32-timers",
74 		.of_match_table = stm32_timers_of_match,
75 	},
76 };
77 module_platform_driver(stm32_timers_driver);
78 
79 MODULE_DESCRIPTION("STMicroelectronics STM32 Timers");
80 MODULE_LICENSE("GPL v2");
81