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 <fabrice.gasnier@st.com>
  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. };
  18. static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
  19. {
  20. u32 val;
  21. int ret;
  22. /*
  23. * Quadrature encoder mode bit can only be written and read back when
  24. * Low-Power Timer supports it.
  25. */
  26. ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
  27. STM32_LPTIM_ENC, STM32_LPTIM_ENC);
  28. if (ret)
  29. return ret;
  30. ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
  31. if (ret)
  32. return ret;
  33. ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
  34. STM32_LPTIM_ENC, 0);
  35. if (ret)
  36. return ret;
  37. ddata->has_encoder = !!(val & STM32_LPTIM_ENC);
  38. return 0;
  39. }
  40. static int stm32_lptimer_probe(struct platform_device *pdev)
  41. {
  42. struct device *dev = &pdev->dev;
  43. struct stm32_lptimer *ddata;
  44. struct resource *res;
  45. void __iomem *mmio;
  46. int ret;
  47. ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
  48. if (!ddata)
  49. return -ENOMEM;
  50. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  51. mmio = devm_ioremap_resource(dev, res);
  52. if (IS_ERR(mmio))
  53. return PTR_ERR(mmio);
  54. ddata->regmap = devm_regmap_init_mmio_clk(dev, "mux", mmio,
  55. &stm32_lptimer_regmap_cfg);
  56. if (IS_ERR(ddata->regmap))
  57. return PTR_ERR(ddata->regmap);
  58. ddata->clk = devm_clk_get(dev, NULL);
  59. if (IS_ERR(ddata->clk))
  60. return PTR_ERR(ddata->clk);
  61. ret = stm32_lptimer_detect_encoder(ddata);
  62. if (ret)
  63. return ret;
  64. platform_set_drvdata(pdev, ddata);
  65. return devm_of_platform_populate(&pdev->dev);
  66. }
  67. static const struct of_device_id stm32_lptimer_of_match[] = {
  68. { .compatible = "st,stm32-lptimer", },
  69. {},
  70. };
  71. MODULE_DEVICE_TABLE(of, stm32_lptimer_of_match);
  72. static struct platform_driver stm32_lptimer_driver = {
  73. .probe = stm32_lptimer_probe,
  74. .driver = {
  75. .name = "stm32-lptimer",
  76. .of_match_table = stm32_lptimer_of_match,
  77. },
  78. };
  79. module_platform_driver(stm32_lptimer_driver);
  80. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  81. MODULE_DESCRIPTION("STMicroelectronics STM32 Low-Power Timer");
  82. MODULE_ALIAS("platform:stm32-lptimer");
  83. MODULE_LICENSE("GPL v2");