hts221_buffer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * STMicroelectronics hts221 sensor driver
  3. *
  4. * Copyright 2016 STMicroelectronics Inc.
  5. *
  6. * Lorenzo Bianconi <lorenzo.bianconi@st.com>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irqreturn.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/trigger.h>
  17. #include <linux/iio/events.h>
  18. #include <linux/iio/trigger_consumer.h>
  19. #include <linux/iio/triggered_buffer.h>
  20. #include <linux/iio/buffer.h>
  21. #include "hts221.h"
  22. #define HTS221_REG_STATUS_ADDR 0x27
  23. #define HTS221_RH_DRDY_MASK BIT(1)
  24. #define HTS221_TEMP_DRDY_MASK BIT(0)
  25. static int hts221_trig_set_state(struct iio_trigger *trig, bool state)
  26. {
  27. struct iio_dev *iio_dev = iio_trigger_get_drvdata(trig);
  28. struct hts221_hw *hw = iio_priv(iio_dev);
  29. return hts221_config_drdy(hw, state);
  30. }
  31. static const struct iio_trigger_ops hts221_trigger_ops = {
  32. .owner = THIS_MODULE,
  33. .set_trigger_state = hts221_trig_set_state,
  34. };
  35. static irqreturn_t hts221_trigger_handler_thread(int irq, void *private)
  36. {
  37. struct hts221_hw *hw = private;
  38. u8 status;
  39. int err;
  40. err = hw->tf->read(hw->dev, HTS221_REG_STATUS_ADDR, sizeof(status),
  41. &status);
  42. if (err < 0)
  43. return IRQ_HANDLED;
  44. /*
  45. * H_DA bit (humidity data available) is routed to DRDY line.
  46. * Humidity sample is computed after temperature one.
  47. * Here we can assume data channels are both available if H_DA bit
  48. * is set in status register
  49. */
  50. if (!(status & HTS221_RH_DRDY_MASK))
  51. return IRQ_NONE;
  52. iio_trigger_poll_chained(hw->trig);
  53. return IRQ_HANDLED;
  54. }
  55. int hts221_allocate_trigger(struct hts221_hw *hw)
  56. {
  57. struct iio_dev *iio_dev = iio_priv_to_dev(hw);
  58. unsigned long irq_type;
  59. int err;
  60. irq_type = irqd_get_trigger_type(irq_get_irq_data(hw->irq));
  61. switch (irq_type) {
  62. case IRQF_TRIGGER_HIGH:
  63. case IRQF_TRIGGER_RISING:
  64. break;
  65. default:
  66. dev_info(hw->dev,
  67. "mode %lx unsupported, using IRQF_TRIGGER_RISING\n",
  68. irq_type);
  69. irq_type = IRQF_TRIGGER_RISING;
  70. break;
  71. }
  72. err = devm_request_threaded_irq(hw->dev, hw->irq, NULL,
  73. hts221_trigger_handler_thread,
  74. irq_type | IRQF_ONESHOT,
  75. hw->name, hw);
  76. if (err) {
  77. dev_err(hw->dev, "failed to request trigger irq %d\n",
  78. hw->irq);
  79. return err;
  80. }
  81. hw->trig = devm_iio_trigger_alloc(hw->dev, "%s-trigger",
  82. iio_dev->name);
  83. if (!hw->trig)
  84. return -ENOMEM;
  85. iio_trigger_set_drvdata(hw->trig, iio_dev);
  86. hw->trig->ops = &hts221_trigger_ops;
  87. hw->trig->dev.parent = hw->dev;
  88. iio_dev->trig = iio_trigger_get(hw->trig);
  89. return devm_iio_trigger_register(hw->dev, hw->trig);
  90. }
  91. static int hts221_buffer_preenable(struct iio_dev *iio_dev)
  92. {
  93. return hts221_power_on(iio_priv(iio_dev));
  94. }
  95. static int hts221_buffer_postdisable(struct iio_dev *iio_dev)
  96. {
  97. return hts221_power_off(iio_priv(iio_dev));
  98. }
  99. static const struct iio_buffer_setup_ops hts221_buffer_ops = {
  100. .preenable = hts221_buffer_preenable,
  101. .postenable = iio_triggered_buffer_postenable,
  102. .predisable = iio_triggered_buffer_predisable,
  103. .postdisable = hts221_buffer_postdisable,
  104. };
  105. static irqreturn_t hts221_buffer_handler_thread(int irq, void *p)
  106. {
  107. u8 buffer[ALIGN(2 * HTS221_DATA_SIZE, sizeof(s64)) + sizeof(s64)];
  108. struct iio_poll_func *pf = p;
  109. struct iio_dev *iio_dev = pf->indio_dev;
  110. struct hts221_hw *hw = iio_priv(iio_dev);
  111. struct iio_chan_spec const *ch;
  112. int err;
  113. /* humidity data */
  114. ch = &iio_dev->channels[HTS221_SENSOR_H];
  115. err = hw->tf->read(hw->dev, ch->address, HTS221_DATA_SIZE,
  116. buffer);
  117. if (err < 0)
  118. goto out;
  119. /* temperature data */
  120. ch = &iio_dev->channels[HTS221_SENSOR_T];
  121. err = hw->tf->read(hw->dev, ch->address, HTS221_DATA_SIZE,
  122. buffer + HTS221_DATA_SIZE);
  123. if (err < 0)
  124. goto out;
  125. iio_push_to_buffers_with_timestamp(iio_dev, buffer,
  126. iio_get_time_ns(iio_dev));
  127. out:
  128. iio_trigger_notify_done(hw->trig);
  129. return IRQ_HANDLED;
  130. }
  131. int hts221_allocate_buffers(struct hts221_hw *hw)
  132. {
  133. return devm_iio_triggered_buffer_setup(hw->dev, iio_priv_to_dev(hw),
  134. NULL, hts221_buffer_handler_thread,
  135. &hts221_buffer_ops);
  136. }
  137. MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
  138. MODULE_DESCRIPTION("STMicroelectronics hts221 buffer driver");
  139. MODULE_LICENSE("GPL v2");