iio-trig-hrtimer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**
  3. * The industrial I/O periodic hrtimer trigger driver
  4. *
  5. * Copyright (C) Intuitive Aerial AB
  6. * Written by Marten Svanfeldt, marten@intuitiveaerial.com
  7. * Copyright (C) 2012, Analog Devices Inc.
  8. * Author: Lars-Peter Clausen <lars@metafoo.de>
  9. * Copyright (C) 2015, Intel Corporation
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/hrtimer.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/trigger.h>
  16. #include <linux/iio/sw_trigger.h>
  17. /* default sampling frequency - 100Hz */
  18. #define HRTIMER_DEFAULT_SAMPLING_FREQUENCY 100
  19. struct iio_hrtimer_info {
  20. struct iio_sw_trigger swt;
  21. struct hrtimer timer;
  22. unsigned long sampling_frequency;
  23. ktime_t period;
  24. };
  25. static const struct config_item_type iio_hrtimer_type = {
  26. .ct_owner = THIS_MODULE,
  27. };
  28. static
  29. ssize_t iio_hrtimer_show_sampling_frequency(struct device *dev,
  30. struct device_attribute *attr,
  31. char *buf)
  32. {
  33. struct iio_trigger *trig = to_iio_trigger(dev);
  34. struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
  35. return snprintf(buf, PAGE_SIZE, "%lu\n", info->sampling_frequency);
  36. }
  37. static
  38. ssize_t iio_hrtimer_store_sampling_frequency(struct device *dev,
  39. struct device_attribute *attr,
  40. const char *buf, size_t len)
  41. {
  42. struct iio_trigger *trig = to_iio_trigger(dev);
  43. struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
  44. unsigned long val;
  45. int ret;
  46. ret = kstrtoul(buf, 10, &val);
  47. if (ret)
  48. return ret;
  49. if (!val || val > NSEC_PER_SEC)
  50. return -EINVAL;
  51. info->sampling_frequency = val;
  52. info->period = NSEC_PER_SEC / val;
  53. return len;
  54. }
  55. static DEVICE_ATTR(sampling_frequency, S_IRUGO | S_IWUSR,
  56. iio_hrtimer_show_sampling_frequency,
  57. iio_hrtimer_store_sampling_frequency);
  58. static struct attribute *iio_hrtimer_attrs[] = {
  59. &dev_attr_sampling_frequency.attr,
  60. NULL
  61. };
  62. static const struct attribute_group iio_hrtimer_attr_group = {
  63. .attrs = iio_hrtimer_attrs,
  64. };
  65. static const struct attribute_group *iio_hrtimer_attr_groups[] = {
  66. &iio_hrtimer_attr_group,
  67. NULL
  68. };
  69. static enum hrtimer_restart iio_hrtimer_trig_handler(struct hrtimer *timer)
  70. {
  71. struct iio_hrtimer_info *info;
  72. info = container_of(timer, struct iio_hrtimer_info, timer);
  73. hrtimer_forward_now(timer, info->period);
  74. iio_trigger_poll(info->swt.trigger);
  75. return HRTIMER_RESTART;
  76. }
  77. static int iio_trig_hrtimer_set_state(struct iio_trigger *trig, bool state)
  78. {
  79. struct iio_hrtimer_info *trig_info;
  80. trig_info = iio_trigger_get_drvdata(trig);
  81. if (state)
  82. hrtimer_start(&trig_info->timer, trig_info->period,
  83. HRTIMER_MODE_REL_HARD);
  84. else
  85. hrtimer_cancel(&trig_info->timer);
  86. return 0;
  87. }
  88. static const struct iio_trigger_ops iio_hrtimer_trigger_ops = {
  89. .set_trigger_state = iio_trig_hrtimer_set_state,
  90. };
  91. static struct iio_sw_trigger *iio_trig_hrtimer_probe(const char *name)
  92. {
  93. struct iio_hrtimer_info *trig_info;
  94. int ret;
  95. trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
  96. if (!trig_info)
  97. return ERR_PTR(-ENOMEM);
  98. trig_info->swt.trigger = iio_trigger_alloc("%s", name);
  99. if (!trig_info->swt.trigger) {
  100. ret = -ENOMEM;
  101. goto err_free_trig_info;
  102. }
  103. iio_trigger_set_drvdata(trig_info->swt.trigger, trig_info);
  104. trig_info->swt.trigger->ops = &iio_hrtimer_trigger_ops;
  105. trig_info->swt.trigger->dev.groups = iio_hrtimer_attr_groups;
  106. hrtimer_init(&trig_info->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
  107. trig_info->timer.function = iio_hrtimer_trig_handler;
  108. trig_info->sampling_frequency = HRTIMER_DEFAULT_SAMPLING_FREQUENCY;
  109. trig_info->period = NSEC_PER_SEC / trig_info->sampling_frequency;
  110. ret = iio_trigger_register(trig_info->swt.trigger);
  111. if (ret)
  112. goto err_free_trigger;
  113. iio_swt_group_init_type_name(&trig_info->swt, name, &iio_hrtimer_type);
  114. return &trig_info->swt;
  115. err_free_trigger:
  116. iio_trigger_free(trig_info->swt.trigger);
  117. err_free_trig_info:
  118. kfree(trig_info);
  119. return ERR_PTR(ret);
  120. }
  121. static int iio_trig_hrtimer_remove(struct iio_sw_trigger *swt)
  122. {
  123. struct iio_hrtimer_info *trig_info;
  124. trig_info = iio_trigger_get_drvdata(swt->trigger);
  125. iio_trigger_unregister(swt->trigger);
  126. /* cancel the timer after unreg to make sure no one rearms it */
  127. hrtimer_cancel(&trig_info->timer);
  128. iio_trigger_free(swt->trigger);
  129. kfree(trig_info);
  130. return 0;
  131. }
  132. static const struct iio_sw_trigger_ops iio_trig_hrtimer_ops = {
  133. .probe = iio_trig_hrtimer_probe,
  134. .remove = iio_trig_hrtimer_remove,
  135. };
  136. static struct iio_sw_trigger_type iio_trig_hrtimer = {
  137. .name = "hrtimer",
  138. .owner = THIS_MODULE,
  139. .ops = &iio_trig_hrtimer_ops,
  140. };
  141. module_iio_sw_trigger_driver(iio_trig_hrtimer);
  142. MODULE_AUTHOR("Marten Svanfeldt <marten@intuitiveaerial.com>");
  143. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
  144. MODULE_DESCRIPTION("Periodic hrtimer trigger for the IIO subsystem");
  145. MODULE_LICENSE("GPL v2");