xref: /linux/drivers/mfd/stm32-timers.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
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 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
62 }
63 
64 static int stm32_timers_remove(struct platform_device *pdev)
65 {
66 	of_platform_depopulate(&pdev->dev);
67 
68 	return 0;
69 }
70 
71 static const struct of_device_id stm32_timers_of_match[] = {
72 	{ .compatible = "st,stm32-timers", },
73 	{ /* end node */ },
74 };
75 MODULE_DEVICE_TABLE(of, stm32_timers_of_match);
76 
77 static struct platform_driver stm32_timers_driver = {
78 	.probe = stm32_timers_probe,
79 	.remove = stm32_timers_remove,
80 	.driver	= {
81 		.name = "stm32-timers",
82 		.of_match_table = stm32_timers_of_match,
83 	},
84 };
85 module_platform_driver(stm32_timers_driver);
86 
87 MODULE_DESCRIPTION("STMicroelectronics STM32 Timers");
88 MODULE_LICENSE("GPL v2");
89