stm32-timers.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 = 0x400,
  17. };
  18. static void stm32_timers_get_arr_size(struct stm32_timers *ddata)
  19. {
  20. /*
  21. * Only the available bits will be written so when readback
  22. * we get the maximum value of auto reload register
  23. */
  24. regmap_write(ddata->regmap, TIM_ARR, ~0L);
  25. regmap_read(ddata->regmap, TIM_ARR, &ddata->max_arr);
  26. regmap_write(ddata->regmap, TIM_ARR, 0x0);
  27. }
  28. static int stm32_timers_probe(struct platform_device *pdev)
  29. {
  30. struct device *dev = &pdev->dev;
  31. struct stm32_timers *ddata;
  32. struct resource *res;
  33. void __iomem *mmio;
  34. ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
  35. if (!ddata)
  36. return -ENOMEM;
  37. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  38. mmio = devm_ioremap_resource(dev, res);
  39. if (IS_ERR(mmio))
  40. return PTR_ERR(mmio);
  41. ddata->regmap = devm_regmap_init_mmio_clk(dev, "int", mmio,
  42. &stm32_timers_regmap_cfg);
  43. if (IS_ERR(ddata->regmap))
  44. return PTR_ERR(ddata->regmap);
  45. ddata->clk = devm_clk_get(dev, NULL);
  46. if (IS_ERR(ddata->clk))
  47. return PTR_ERR(ddata->clk);
  48. stm32_timers_get_arr_size(ddata);
  49. platform_set_drvdata(pdev, ddata);
  50. return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  51. }
  52. static const struct of_device_id stm32_timers_of_match[] = {
  53. { .compatible = "st,stm32-timers", },
  54. { /* end node */ },
  55. };
  56. MODULE_DEVICE_TABLE(of, stm32_timers_of_match);
  57. static struct platform_driver stm32_timers_driver = {
  58. .probe = stm32_timers_probe,
  59. .driver = {
  60. .name = "stm32-timers",
  61. .of_match_table = stm32_timers_of_match,
  62. },
  63. };
  64. module_platform_driver(stm32_timers_driver);
  65. MODULE_DESCRIPTION("STMicroelectronics STM32 Timers");
  66. MODULE_LICENSE("GPL v2");