xilinx-xadc-events.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Xilinx XADC driver
  4. *
  5. * Copyright 2013 Analog Devices Inc.
  6. * Author: Lars-Peter Clausen <lars@metafoo.de>
  7. */
  8. #include <linux/iio/events.h>
  9. #include <linux/iio/iio.h>
  10. #include <linux/kernel.h>
  11. #include "xilinx-xadc.h"
  12. static const struct iio_chan_spec *xadc_event_to_channel(
  13. struct iio_dev *indio_dev, unsigned int event)
  14. {
  15. switch (event) {
  16. case XADC_THRESHOLD_OT_MAX:
  17. case XADC_THRESHOLD_TEMP_MAX:
  18. return &indio_dev->channels[0];
  19. case XADC_THRESHOLD_VCCINT_MAX:
  20. case XADC_THRESHOLD_VCCAUX_MAX:
  21. return &indio_dev->channels[event];
  22. default:
  23. return &indio_dev->channels[event-1];
  24. }
  25. }
  26. static void xadc_handle_event(struct iio_dev *indio_dev, unsigned int event)
  27. {
  28. const struct iio_chan_spec *chan;
  29. /* Temperature threshold error, we don't handle this yet */
  30. if (event == 0)
  31. return;
  32. chan = xadc_event_to_channel(indio_dev, event);
  33. if (chan->type == IIO_TEMP) {
  34. /*
  35. * The temperature channel only supports over-temperature
  36. * events.
  37. */
  38. iio_push_event(indio_dev,
  39. IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
  40. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  41. iio_get_time_ns(indio_dev));
  42. } else {
  43. /*
  44. * For other channels we don't know whether it is a upper or
  45. * lower threshold event. Userspace will have to check the
  46. * channel value if it wants to know.
  47. */
  48. iio_push_event(indio_dev,
  49. IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
  50. IIO_EV_TYPE_THRESH, IIO_EV_DIR_EITHER),
  51. iio_get_time_ns(indio_dev));
  52. }
  53. }
  54. void xadc_handle_events(struct iio_dev *indio_dev, unsigned long events)
  55. {
  56. unsigned int i;
  57. for_each_set_bit(i, &events, 8)
  58. xadc_handle_event(indio_dev, i);
  59. }
  60. static unsigned int xadc_get_threshold_offset(const struct iio_chan_spec *chan,
  61. enum iio_event_direction dir)
  62. {
  63. unsigned int offset;
  64. if (chan->type == IIO_TEMP) {
  65. offset = XADC_THRESHOLD_OT_MAX;
  66. } else {
  67. if (chan->channel < 2)
  68. offset = chan->channel + 1;
  69. else
  70. offset = chan->channel + 6;
  71. }
  72. if (dir == IIO_EV_DIR_FALLING)
  73. offset += 4;
  74. return offset;
  75. }
  76. static unsigned int xadc_get_alarm_mask(const struct iio_chan_spec *chan)
  77. {
  78. if (chan->type == IIO_TEMP)
  79. return XADC_ALARM_OT_MASK;
  80. switch (chan->channel) {
  81. case 0:
  82. return XADC_ALARM_VCCINT_MASK;
  83. case 1:
  84. return XADC_ALARM_VCCAUX_MASK;
  85. case 2:
  86. return XADC_ALARM_VCCBRAM_MASK;
  87. case 3:
  88. return XADC_ALARM_VCCPINT_MASK;
  89. case 4:
  90. return XADC_ALARM_VCCPAUX_MASK;
  91. case 5:
  92. return XADC_ALARM_VCCODDR_MASK;
  93. default:
  94. /* We will never get here */
  95. return 0;
  96. }
  97. }
  98. int xadc_read_event_config(struct iio_dev *indio_dev,
  99. const struct iio_chan_spec *chan, enum iio_event_type type,
  100. enum iio_event_direction dir)
  101. {
  102. struct xadc *xadc = iio_priv(indio_dev);
  103. return (bool)(xadc->alarm_mask & xadc_get_alarm_mask(chan));
  104. }
  105. int xadc_write_event_config(struct iio_dev *indio_dev,
  106. const struct iio_chan_spec *chan, enum iio_event_type type,
  107. enum iio_event_direction dir, int state)
  108. {
  109. unsigned int alarm = xadc_get_alarm_mask(chan);
  110. struct xadc *xadc = iio_priv(indio_dev);
  111. uint16_t cfg, old_cfg;
  112. int ret;
  113. mutex_lock(&xadc->mutex);
  114. if (state)
  115. xadc->alarm_mask |= alarm;
  116. else
  117. xadc->alarm_mask &= ~alarm;
  118. xadc->ops->update_alarm(xadc, xadc->alarm_mask);
  119. ret = _xadc_read_adc_reg(xadc, XADC_REG_CONF1, &cfg);
  120. if (ret)
  121. goto err_out;
  122. old_cfg = cfg;
  123. cfg |= XADC_CONF1_ALARM_MASK;
  124. cfg &= ~((xadc->alarm_mask & 0xf0) << 4); /* bram, pint, paux, ddr */
  125. cfg &= ~((xadc->alarm_mask & 0x08) >> 3); /* ot */
  126. cfg &= ~((xadc->alarm_mask & 0x07) << 1); /* temp, vccint, vccaux */
  127. if (old_cfg != cfg)
  128. ret = _xadc_write_adc_reg(xadc, XADC_REG_CONF1, cfg);
  129. err_out:
  130. mutex_unlock(&xadc->mutex);
  131. return ret;
  132. }
  133. /* Register value is msb aligned, the lower 4 bits are ignored */
  134. #define XADC_THRESHOLD_VALUE_SHIFT 4
  135. int xadc_read_event_value(struct iio_dev *indio_dev,
  136. const struct iio_chan_spec *chan, enum iio_event_type type,
  137. enum iio_event_direction dir, enum iio_event_info info,
  138. int *val, int *val2)
  139. {
  140. unsigned int offset = xadc_get_threshold_offset(chan, dir);
  141. struct xadc *xadc = iio_priv(indio_dev);
  142. switch (info) {
  143. case IIO_EV_INFO_VALUE:
  144. *val = xadc->threshold[offset];
  145. break;
  146. case IIO_EV_INFO_HYSTERESIS:
  147. *val = xadc->temp_hysteresis;
  148. break;
  149. default:
  150. return -EINVAL;
  151. }
  152. *val >>= XADC_THRESHOLD_VALUE_SHIFT;
  153. return IIO_VAL_INT;
  154. }
  155. int xadc_write_event_value(struct iio_dev *indio_dev,
  156. const struct iio_chan_spec *chan, enum iio_event_type type,
  157. enum iio_event_direction dir, enum iio_event_info info,
  158. int val, int val2)
  159. {
  160. unsigned int offset = xadc_get_threshold_offset(chan, dir);
  161. struct xadc *xadc = iio_priv(indio_dev);
  162. int ret = 0;
  163. val <<= XADC_THRESHOLD_VALUE_SHIFT;
  164. if (val < 0 || val > 0xffff)
  165. return -EINVAL;
  166. mutex_lock(&xadc->mutex);
  167. switch (info) {
  168. case IIO_EV_INFO_VALUE:
  169. xadc->threshold[offset] = val;
  170. break;
  171. case IIO_EV_INFO_HYSTERESIS:
  172. xadc->temp_hysteresis = val;
  173. break;
  174. default:
  175. mutex_unlock(&xadc->mutex);
  176. return -EINVAL;
  177. }
  178. if (chan->type == IIO_TEMP) {
  179. /*
  180. * According to the datasheet we need to set the lower 4 bits to
  181. * 0x3, otherwise 125 degree celsius will be used as the
  182. * threshold.
  183. */
  184. val |= 0x3;
  185. /*
  186. * Since we store the hysteresis as relative (to the threshold)
  187. * value, but the hardware expects an absolute value we need to
  188. * recalcualte this value whenever the hysteresis or the
  189. * threshold changes.
  190. */
  191. if (xadc->threshold[offset] < xadc->temp_hysteresis)
  192. xadc->threshold[offset + 4] = 0;
  193. else
  194. xadc->threshold[offset + 4] = xadc->threshold[offset] -
  195. xadc->temp_hysteresis;
  196. ret = _xadc_write_adc_reg(xadc, XADC_REG_THRESHOLD(offset + 4),
  197. xadc->threshold[offset + 4]);
  198. if (ret)
  199. goto out_unlock;
  200. }
  201. if (info == IIO_EV_INFO_VALUE)
  202. ret = _xadc_write_adc_reg(xadc, XADC_REG_THRESHOLD(offset), val);
  203. out_unlock:
  204. mutex_unlock(&xadc->mutex);
  205. return ret;
  206. }