iio-trig-hrtimer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, [email protected]
  7. * Copyright (C) 2012, Analog Devices Inc.
  8. * Author: Lars-Peter Clausen <[email protected]>
  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. /* Defined locally, not in time64.h yet. */
  18. #define PSEC_PER_SEC 1000000000000LL
  19. /* default sampling frequency - 100Hz */
  20. #define HRTIMER_DEFAULT_SAMPLING_FREQUENCY 100
  21. struct iio_hrtimer_info {
  22. struct iio_sw_trigger swt;
  23. struct hrtimer timer;
  24. int sampling_frequency[2];
  25. ktime_t period;
  26. };
  27. static const struct config_item_type iio_hrtimer_type = {
  28. .ct_owner = THIS_MODULE,
  29. };
  30. static
  31. ssize_t iio_hrtimer_show_sampling_frequency(struct device *dev,
  32. struct device_attribute *attr,
  33. char *buf)
  34. {
  35. struct iio_trigger *trig = to_iio_trigger(dev);
  36. struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
  37. return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO,
  38. ARRAY_SIZE(info->sampling_frequency),
  39. info->sampling_frequency);
  40. }
  41. static
  42. ssize_t iio_hrtimer_store_sampling_frequency(struct device *dev,
  43. struct device_attribute *attr,
  44. const char *buf, size_t len)
  45. {
  46. struct iio_trigger *trig = to_iio_trigger(dev);
  47. struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
  48. unsigned long long val;
  49. u64 period;
  50. int integer, fract, ret;
  51. ret = iio_str_to_fixpoint(buf, 100, &integer, &fract);
  52. if (ret)
  53. return ret;
  54. if (integer < 0 || fract < 0)
  55. return -ERANGE;
  56. val = fract + 1000ULL * integer; /* mHz */
  57. if (!val || val > UINT_MAX)
  58. return -EINVAL;
  59. info->sampling_frequency[0] = integer; /* Hz */
  60. info->sampling_frequency[1] = fract * 1000; /* uHz */
  61. period = PSEC_PER_SEC;
  62. do_div(period, val);
  63. info->period = period; /* nS */
  64. return len;
  65. }
  66. static DEVICE_ATTR(sampling_frequency, S_IRUGO | S_IWUSR,
  67. iio_hrtimer_show_sampling_frequency,
  68. iio_hrtimer_store_sampling_frequency);
  69. static struct attribute *iio_hrtimer_attrs[] = {
  70. &dev_attr_sampling_frequency.attr,
  71. NULL
  72. };
  73. static const struct attribute_group iio_hrtimer_attr_group = {
  74. .attrs = iio_hrtimer_attrs,
  75. };
  76. static const struct attribute_group *iio_hrtimer_attr_groups[] = {
  77. &iio_hrtimer_attr_group,
  78. NULL
  79. };
  80. static enum hrtimer_restart iio_hrtimer_trig_handler(struct hrtimer *timer)
  81. {
  82. struct iio_hrtimer_info *info;
  83. info = container_of(timer, struct iio_hrtimer_info, timer);
  84. hrtimer_forward_now(timer, info->period);
  85. iio_trigger_poll(info->swt.trigger);
  86. return HRTIMER_RESTART;
  87. }
  88. static int iio_trig_hrtimer_set_state(struct iio_trigger *trig, bool state)
  89. {
  90. struct iio_hrtimer_info *trig_info;
  91. trig_info = iio_trigger_get_drvdata(trig);
  92. if (state)
  93. hrtimer_start(&trig_info->timer, trig_info->period,
  94. HRTIMER_MODE_REL_HARD);
  95. else
  96. hrtimer_cancel(&trig_info->timer);
  97. return 0;
  98. }
  99. static const struct iio_trigger_ops iio_hrtimer_trigger_ops = {
  100. .set_trigger_state = iio_trig_hrtimer_set_state,
  101. };
  102. static struct iio_sw_trigger *iio_trig_hrtimer_probe(const char *name)
  103. {
  104. struct iio_hrtimer_info *trig_info;
  105. int ret;
  106. trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
  107. if (!trig_info)
  108. return ERR_PTR(-ENOMEM);
  109. trig_info->swt.trigger = iio_trigger_alloc(NULL, "%s", name);
  110. if (!trig_info->swt.trigger) {
  111. ret = -ENOMEM;
  112. goto err_free_trig_info;
  113. }
  114. iio_trigger_set_drvdata(trig_info->swt.trigger, trig_info);
  115. trig_info->swt.trigger->ops = &iio_hrtimer_trigger_ops;
  116. trig_info->swt.trigger->dev.groups = iio_hrtimer_attr_groups;
  117. hrtimer_init(&trig_info->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
  118. trig_info->timer.function = iio_hrtimer_trig_handler;
  119. trig_info->sampling_frequency[0] = HRTIMER_DEFAULT_SAMPLING_FREQUENCY;
  120. trig_info->period = NSEC_PER_SEC / trig_info->sampling_frequency[0];
  121. ret = iio_trigger_register(trig_info->swt.trigger);
  122. if (ret)
  123. goto err_free_trigger;
  124. iio_swt_group_init_type_name(&trig_info->swt, name, &iio_hrtimer_type);
  125. return &trig_info->swt;
  126. err_free_trigger:
  127. iio_trigger_free(trig_info->swt.trigger);
  128. err_free_trig_info:
  129. kfree(trig_info);
  130. return ERR_PTR(ret);
  131. }
  132. static int iio_trig_hrtimer_remove(struct iio_sw_trigger *swt)
  133. {
  134. struct iio_hrtimer_info *trig_info;
  135. trig_info = iio_trigger_get_drvdata(swt->trigger);
  136. iio_trigger_unregister(swt->trigger);
  137. /* cancel the timer after unreg to make sure no one rearms it */
  138. hrtimer_cancel(&trig_info->timer);
  139. iio_trigger_free(swt->trigger);
  140. kfree(trig_info);
  141. return 0;
  142. }
  143. static const struct iio_sw_trigger_ops iio_trig_hrtimer_ops = {
  144. .probe = iio_trig_hrtimer_probe,
  145. .remove = iio_trig_hrtimer_remove,
  146. };
  147. static struct iio_sw_trigger_type iio_trig_hrtimer = {
  148. .name = "hrtimer",
  149. .owner = THIS_MODULE,
  150. .ops = &iio_trig_hrtimer_ops,
  151. };
  152. module_iio_sw_trigger_driver(iio_trig_hrtimer);
  153. MODULE_AUTHOR("Marten Svanfeldt <[email protected]>");
  154. MODULE_AUTHOR("Daniel Baluta <[email protected]>");
  155. MODULE_DESCRIPTION("Periodic hrtimer trigger for the IIO subsystem");
  156. MODULE_LICENSE("GPL v2");