xilinx-xadc-events.c 5.8 KB

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