industrialio-hw-consumer.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2017 Analog Devices Inc.
  4. * Author: Lars-Peter Clausen <lars@metafoo.de>
  5. */
  6. #include <linux/err.h>
  7. #include <linux/export.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/iio/iio.h>
  11. #include <linux/iio/consumer.h>
  12. #include <linux/iio/hw-consumer.h>
  13. #include <linux/iio/buffer_impl.h>
  14. /**
  15. * struct iio_hw_consumer - IIO hw consumer block
  16. * @buffers: hardware buffers list head.
  17. * @channels: IIO provider channels.
  18. */
  19. struct iio_hw_consumer {
  20. struct list_head buffers;
  21. struct iio_channel *channels;
  22. };
  23. struct hw_consumer_buffer {
  24. struct list_head head;
  25. struct iio_dev *indio_dev;
  26. struct iio_buffer buffer;
  27. long scan_mask[];
  28. };
  29. static struct hw_consumer_buffer *iio_buffer_to_hw_consumer_buffer(
  30. struct iio_buffer *buffer)
  31. {
  32. return container_of(buffer, struct hw_consumer_buffer, buffer);
  33. }
  34. static void iio_hw_buf_release(struct iio_buffer *buffer)
  35. {
  36. struct hw_consumer_buffer *hw_buf =
  37. iio_buffer_to_hw_consumer_buffer(buffer);
  38. kfree(hw_buf);
  39. }
  40. static const struct iio_buffer_access_funcs iio_hw_buf_access = {
  41. .release = &iio_hw_buf_release,
  42. .modes = INDIO_BUFFER_HARDWARE,
  43. };
  44. static struct hw_consumer_buffer *iio_hw_consumer_get_buffer(
  45. struct iio_hw_consumer *hwc, struct iio_dev *indio_dev)
  46. {
  47. size_t mask_size = BITS_TO_LONGS(indio_dev->masklength) * sizeof(long);
  48. struct hw_consumer_buffer *buf;
  49. list_for_each_entry(buf, &hwc->buffers, head) {
  50. if (buf->indio_dev == indio_dev)
  51. return buf;
  52. }
  53. buf = kzalloc(sizeof(*buf) + mask_size, GFP_KERNEL);
  54. if (!buf)
  55. return NULL;
  56. buf->buffer.access = &iio_hw_buf_access;
  57. buf->indio_dev = indio_dev;
  58. buf->buffer.scan_mask = buf->scan_mask;
  59. iio_buffer_init(&buf->buffer);
  60. list_add_tail(&buf->head, &hwc->buffers);
  61. return buf;
  62. }
  63. /**
  64. * iio_hw_consumer_alloc() - Allocate IIO hardware consumer
  65. * @dev: Pointer to consumer device.
  66. *
  67. * Returns a valid iio_hw_consumer on success or a ERR_PTR() on failure.
  68. */
  69. struct iio_hw_consumer *iio_hw_consumer_alloc(struct device *dev)
  70. {
  71. struct hw_consumer_buffer *buf;
  72. struct iio_hw_consumer *hwc;
  73. struct iio_channel *chan;
  74. int ret;
  75. hwc = kzalloc(sizeof(*hwc), GFP_KERNEL);
  76. if (!hwc)
  77. return ERR_PTR(-ENOMEM);
  78. INIT_LIST_HEAD(&hwc->buffers);
  79. hwc->channels = iio_channel_get_all(dev);
  80. if (IS_ERR(hwc->channels)) {
  81. ret = PTR_ERR(hwc->channels);
  82. goto err_free_hwc;
  83. }
  84. chan = &hwc->channels[0];
  85. while (chan->indio_dev) {
  86. buf = iio_hw_consumer_get_buffer(hwc, chan->indio_dev);
  87. if (!buf) {
  88. ret = -ENOMEM;
  89. goto err_put_buffers;
  90. }
  91. set_bit(chan->channel->scan_index, buf->buffer.scan_mask);
  92. chan++;
  93. }
  94. return hwc;
  95. err_put_buffers:
  96. list_for_each_entry(buf, &hwc->buffers, head)
  97. iio_buffer_put(&buf->buffer);
  98. iio_channel_release_all(hwc->channels);
  99. err_free_hwc:
  100. kfree(hwc);
  101. return ERR_PTR(ret);
  102. }
  103. EXPORT_SYMBOL_GPL(iio_hw_consumer_alloc);
  104. /**
  105. * iio_hw_consumer_free() - Free IIO hardware consumer
  106. * @hwc: hw consumer to free.
  107. */
  108. void iio_hw_consumer_free(struct iio_hw_consumer *hwc)
  109. {
  110. struct hw_consumer_buffer *buf, *n;
  111. iio_channel_release_all(hwc->channels);
  112. list_for_each_entry_safe(buf, n, &hwc->buffers, head)
  113. iio_buffer_put(&buf->buffer);
  114. kfree(hwc);
  115. }
  116. EXPORT_SYMBOL_GPL(iio_hw_consumer_free);
  117. static void devm_iio_hw_consumer_release(struct device *dev, void *res)
  118. {
  119. iio_hw_consumer_free(*(struct iio_hw_consumer **)res);
  120. }
  121. static int devm_iio_hw_consumer_match(struct device *dev, void *res, void *data)
  122. {
  123. struct iio_hw_consumer **r = res;
  124. if (!r || !*r) {
  125. WARN_ON(!r || !*r);
  126. return 0;
  127. }
  128. return *r == data;
  129. }
  130. /**
  131. * devm_iio_hw_consumer_alloc - Resource-managed iio_hw_consumer_alloc()
  132. * @dev: Pointer to consumer device.
  133. *
  134. * Managed iio_hw_consumer_alloc. iio_hw_consumer allocated with this function
  135. * is automatically freed on driver detach.
  136. *
  137. * If an iio_hw_consumer allocated with this function needs to be freed
  138. * separately, devm_iio_hw_consumer_free() must be used.
  139. *
  140. * returns pointer to allocated iio_hw_consumer on success, NULL on failure.
  141. */
  142. struct iio_hw_consumer *devm_iio_hw_consumer_alloc(struct device *dev)
  143. {
  144. struct iio_hw_consumer **ptr, *iio_hwc;
  145. ptr = devres_alloc(devm_iio_hw_consumer_release, sizeof(*ptr),
  146. GFP_KERNEL);
  147. if (!ptr)
  148. return NULL;
  149. iio_hwc = iio_hw_consumer_alloc(dev);
  150. if (IS_ERR(iio_hwc)) {
  151. devres_free(ptr);
  152. } else {
  153. *ptr = iio_hwc;
  154. devres_add(dev, ptr);
  155. }
  156. return iio_hwc;
  157. }
  158. EXPORT_SYMBOL_GPL(devm_iio_hw_consumer_alloc);
  159. /**
  160. * devm_iio_hw_consumer_free - Resource-managed iio_hw_consumer_free()
  161. * @dev: Pointer to consumer device.
  162. * @hwc: iio_hw_consumer to free.
  163. *
  164. * Free iio_hw_consumer allocated with devm_iio_hw_consumer_alloc().
  165. */
  166. void devm_iio_hw_consumer_free(struct device *dev, struct iio_hw_consumer *hwc)
  167. {
  168. int rc;
  169. rc = devres_release(dev, devm_iio_hw_consumer_release,
  170. devm_iio_hw_consumer_match, hwc);
  171. WARN_ON(rc);
  172. }
  173. EXPORT_SYMBOL_GPL(devm_iio_hw_consumer_free);
  174. /**
  175. * iio_hw_consumer_enable() - Enable IIO hardware consumer
  176. * @hwc: iio_hw_consumer to enable.
  177. *
  178. * Returns 0 on success.
  179. */
  180. int iio_hw_consumer_enable(struct iio_hw_consumer *hwc)
  181. {
  182. struct hw_consumer_buffer *buf;
  183. int ret;
  184. list_for_each_entry(buf, &hwc->buffers, head) {
  185. ret = iio_update_buffers(buf->indio_dev, &buf->buffer, NULL);
  186. if (ret)
  187. goto err_disable_buffers;
  188. }
  189. return 0;
  190. err_disable_buffers:
  191. list_for_each_entry_continue_reverse(buf, &hwc->buffers, head)
  192. iio_update_buffers(buf->indio_dev, NULL, &buf->buffer);
  193. return ret;
  194. }
  195. EXPORT_SYMBOL_GPL(iio_hw_consumer_enable);
  196. /**
  197. * iio_hw_consumer_disable() - Disable IIO hardware consumer
  198. * @hwc: iio_hw_consumer to disable.
  199. */
  200. void iio_hw_consumer_disable(struct iio_hw_consumer *hwc)
  201. {
  202. struct hw_consumer_buffer *buf;
  203. list_for_each_entry(buf, &hwc->buffers, head)
  204. iio_update_buffers(buf->indio_dev, NULL, &buf->buffer);
  205. }
  206. EXPORT_SYMBOL_GPL(iio_hw_consumer_disable);
  207. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  208. MODULE_DESCRIPTION("Hardware consumer buffer the IIO framework");
  209. MODULE_LICENSE("GPL v2");