xilinx-xadc-events.c 5.8 KB

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