stm32-lptimer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * STM32 Low-Power Timer parent driver.
  4. * Copyright (C) STMicroelectronics 2017
  5. * Author: Fabrice Gasnier <[email protected]>
  6. * Inspired by Benjamin Gaignard's stm32-timers driver
  7. */
  8. #include <linux/mfd/stm32-lptimer.h>
  9. #include <linux/module.h>
  10. #include <linux/of_platform.h>
  11. #define STM32_LPTIM_MAX_REGISTER 0x3fc
  12. static const struct regmap_config stm32_lptimer_regmap_cfg = {
  13. .reg_bits = 32,
  14. .val_bits = 32,
  15. .reg_stride = sizeof(u32),
  16. .max_register = STM32_LPTIM_MAX_REGISTER,
  17. .fast_io = true,
  18. };
  19. static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
  20. {
  21. u32 val;
  22. int ret;
  23. /*
  24. * Quadrature encoder mode bit can only be written and read back when
  25. * Low-Power Timer supports it.
  26. */
  27. ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
  28. STM32_LPTIM_ENC, STM32_LPTIM_ENC);
  29. if (ret)
  30. return ret;
  31. ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
  32. if (ret)
  33. return ret;
  34. ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
  35. STM32_LPTIM_ENC, 0);
  36. if (ret)
  37. return ret;
  38. ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
  39. return 0;
  40. }
  41. static int stm32_lptimer_probe(struct platform_device *pdev)
  42. {
  43. struct device *dev = &pdev->dev;
  44. struct stm32_lptimer *ddata;
  45. struct resource *res;
  46. void __iomem *mmio;
  47. int ret;
  48. ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
  49. if (!ddata)
  50. return -ENOMEM;
  51. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  52. mmio = devm_ioremap_resource(dev, res);
  53. if (IS_ERR(mmio))
  54. return PTR_ERR(mmio);
  55. ddata->regmap = devm_regmap_init_mmio_clk(dev, "mux", mmio,
  56. &stm32_lptimer_regmap_cfg);
  57. if (IS_ERR(ddata->regmap))
  58. return PTR_ERR(ddata->regmap);
  59. ddata->clk = devm_clk_get(dev, NULL);
  60. if (IS_ERR(ddata->clk))
  61. return PTR_ERR(ddata->clk);
  62. ret = stm32_lptimer_detect_encoder(ddata);
  63. if (ret)
  64. return ret;
  65. platform_set_drvdata(pdev, ddata);
  66. return devm_of_platform_populate(&pdev->dev);
  67. }
  68. static const struct of_device_id stm32_lptimer_of_match[] = {
  69. { .compatible = "st,stm32-lptimer", },
  70. {},
  71. };
  72. MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
  73. static struct platform_driver stm32_lptimer_driver = {
  74. .probe = stm32_lptimer_probe,
  75. .driver = {
  76. .name = "stm32-lptimer",
  77. .of_match_table = stm32_lptimer_of_match,
  78. },
  79. };
  80. module_platform_driver(stm32_lptimer_driver);
  81. MODULE_AUTHOR("Fabrice Gasnier <[email protected]>");
  82. MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
  83. MODULE_ALIAS("platform:stm32-lptimer");
  84. MODULE_LICENSE("GPL v2");