stm32-timers.c 1.9 KB

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