stm32-lptimer-trigger.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * STM32 Low-Power Timer Trigger driver
  4. *
  5. * Copyright (C) STMicroelectronics 2017
  6. *
  7. * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
  8. *
  9. * Inspired by Benjamin Gaignard's stm32-timer-trigger driver
  10. */
  11. #include <linux/iio/timer/stm32-lptim-trigger.h>
  12. #include <linux/mfd/stm32-lptimer.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. /* List Low-Power Timer triggers */
  16. static const char * const stm32_lptim_triggers[] = {
  17. LPTIM1_OUT,
  18. LPTIM2_OUT,
  19. LPTIM3_OUT,
  20. };
  21. struct stm32_lptim_trigger {
  22. struct device *dev;
  23. const char *trg;
  24. };
  25. static int stm32_lptim_validate_device(struct iio_trigger *trig,
  26. struct iio_dev *indio_dev)
  27. {
  28. if (indio_dev->modes & INDIO_HARDWARE_TRIGGERED)
  29. return 0;
  30. return -EINVAL;
  31. }
  32. static const struct iio_trigger_ops stm32_lptim_trigger_ops = {
  33. .validate_device = stm32_lptim_validate_device,
  34. };
  35. /**
  36. * is_stm32_lptim_trigger
  37. * @trig: trigger to be checked
  38. *
  39. * return true if the trigger is a valid STM32 IIO Low-Power Timer Trigger
  40. * either return false
  41. */
  42. bool is_stm32_lptim_trigger(struct iio_trigger *trig)
  43. {
  44. return (trig->ops == &stm32_lptim_trigger_ops);
  45. }
  46. EXPORT_SYMBOL(is_stm32_lptim_trigger);
  47. static int stm32_lptim_setup_trig(struct stm32_lptim_trigger *priv)
  48. {
  49. struct iio_trigger *trig;
  50. trig = devm_iio_trigger_alloc(priv->dev, "%s", priv->trg);
  51. if (!trig)
  52. return -ENOMEM;
  53. trig->dev.parent = priv->dev->parent;
  54. trig->ops = &stm32_lptim_trigger_ops;
  55. iio_trigger_set_drvdata(trig, priv);
  56. return devm_iio_trigger_register(priv->dev, trig);
  57. }
  58. static int stm32_lptim_trigger_probe(struct platform_device *pdev)
  59. {
  60. struct stm32_lptim_trigger *priv;
  61. u32 index;
  62. int ret;
  63. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  64. if (!priv)
  65. return -ENOMEM;
  66. if (of_property_read_u32(pdev->dev.of_node, "reg", &index))
  67. return -EINVAL;
  68. if (index >= ARRAY_SIZE(stm32_lptim_triggers))
  69. return -EINVAL;
  70. priv->dev = &pdev->dev;
  71. priv->trg = stm32_lptim_triggers[index];
  72. ret = stm32_lptim_setup_trig(priv);
  73. if (ret)
  74. return ret;
  75. platform_set_drvdata(pdev, priv);
  76. return 0;
  77. }
  78. static const struct of_device_id stm32_lptim_trig_of_match[] = {
  79. { .compatible = "st,stm32-lptimer-trigger", },
  80. {},
  81. };
  82. MODULE_DEVICE_TABLE(of, stm32_lptim_trig_of_match);
  83. static struct platform_driver stm32_lptim_trigger_driver = {
  84. .probe = stm32_lptim_trigger_probe,
  85. .driver = {
  86. .name = "stm32-lptimer-trigger",
  87. .of_match_table = stm32_lptim_trig_of_match,
  88. },
  89. };
  90. module_platform_driver(stm32_lptim_trigger_driver);
  91. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  92. MODULE_ALIAS("platform:stm32-lptimer-trigger");
  93. MODULE_DESCRIPTION("STMicroelectronics STM32 LPTIM trigger driver");
  94. MODULE_LICENSE("GPL v2");