stm32-timers.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include <linux/mfd/stm32-timers.h>
  9. #include <linux/module.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/reset.h>
  12. static const struct regmap_config stm32_timers_regmap_cfg = {
  13. .reg_bits = 32,
  14. .val_bits = 32,
  15. .reg_stride = sizeof(u32),
  16. .max_register = 0x3fc,
  17. };
  18. static void stm32_timers_get_arr_size(struct stm32_timers *ddata)
  19. {
  20. u32 arr;
  21. /* Backup ARR to restore it after getting the maximum value */
  22. regmap_read(ddata->regmap, TIM_ARR, &arr);
  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, arr);
  30. }
  31. static int stm32_timers_probe(struct platform_device *pdev)
  32. {
  33. struct device *dev = &pdev->dev;
  34. struct stm32_timers *ddata;
  35. struct resource *res;
  36. void __iomem *mmio;
  37. ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
  38. if (!ddata)
  39. return -ENOMEM;
  40. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  41. mmio = devm_ioremap_resource(dev, res);
  42. if (IS_ERR(mmio))
  43. return PTR_ERR(mmio);
  44. ddata->regmap = devm_regmap_init_mmio_clk(dev, "int", mmio,
  45. &stm32_timers_regmap_cfg);
  46. if (IS_ERR(ddata->regmap))
  47. return PTR_ERR(ddata->regmap);
  48. ddata->clk = devm_clk_get(dev, NULL);
  49. if (IS_ERR(ddata->clk))
  50. return PTR_ERR(ddata->clk);
  51. stm32_timers_get_arr_size(ddata);
  52. platform_set_drvdata(pdev, ddata);
  53. return devm_of_platform_populate(&pdev->dev);
  54. }
  55. static const struct of_device_id stm32_timers_of_match[] = {
  56. { .compatible = "st,stm32-timers", },
  57. { /* end node */ },
  58. };
  59. MODULE_DEVICE_TABLE(of, stm32_timers_of_match);
  60. static struct platform_driver stm32_timers_driver = {
  61. .probe = stm32_timers_probe,
  62. .driver = {
  63. .name = "stm32-timers",
  64. .of_match_table = stm32_timers_of_match,
  65. },
  66. };
  67. module_platform_driver(stm32_timers_driver);
  68. MODULE_DESCRIPTION("STMicroelectronics STM32 Timers");
  69. MODULE_LICENSE("GPL v2");