Benjamin Gaignard | d0f949e | 2017-01-20 10:15:03 +0100 | [diff] [blame] | 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), |
Fabrice Gasnier | ae6816b | 2017-02-15 09:04:32 +0100 | [diff] [blame] | 18 | .max_register = 0x3fc, |
Benjamin Gaignard | d0f949e | 2017-01-20 10:15:03 +0100 | [diff] [blame] | 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 | |
Benjamin Gaignard | c42a8edb | 2017-05-29 17:45:53 +0200 | [diff] [blame] | 61 | return devm_of_platform_populate(&pdev->dev); |
Fabrice Gasnier | f8b7ce6 | 2017-02-01 17:41:46 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Benjamin Gaignard | d0f949e | 2017-01-20 10:15:03 +0100 | [diff] [blame] | 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, |
Benjamin Gaignard | d0f949e | 2017-01-20 10:15:03 +0100 | [diff] [blame] | 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"); |