4 * Copyright 2013 Analog Devices Inc.
5 * Author: Lars-Peter Clauen <lars@metafoo.de>
7 * Licensed under the GPL-2.
10 #include <linux/iio/events.h>
11 #include <linux/iio/iio.h>
12 #include <linux/kernel.h>
14 #include "xilinx-xadc.h"
16 static const struct iio_chan_spec
*xadc_event_to_channel(
17 struct iio_dev
*indio_dev
, unsigned int event
)
20 case XADC_THRESHOLD_OT_MAX
:
21 case XADC_THRESHOLD_TEMP_MAX
:
22 return &indio_dev
->channels
[0];
23 case XADC_THRESHOLD_VCCINT_MAX
:
24 case XADC_THRESHOLD_VCCAUX_MAX
:
25 return &indio_dev
->channels
[event
];
27 return &indio_dev
->channels
[event
-1];
31 static void xadc_handle_event(struct iio_dev
*indio_dev
, unsigned int event
)
33 const struct iio_chan_spec
*chan
;
36 /* Temperature threshold error, we don't handle this yet */
45 chan
= xadc_event_to_channel(indio_dev
, event
);
47 if (chan
->type
== IIO_TEMP
) {
49 * The temperature channel only supports over-temperature
52 iio_push_event(indio_dev
,
53 IIO_UNMOD_EVENT_CODE(chan
->type
, chan
->channel
,
54 IIO_EV_TYPE_THRESH
, IIO_EV_DIR_RISING
),
58 * For other channels we don't know whether it is a upper or
59 * lower threshold event. Userspace will have to check the
60 * channel value if it wants to know.
62 iio_push_event(indio_dev
,
63 IIO_UNMOD_EVENT_CODE(chan
->type
, chan
->channel
,
64 IIO_EV_TYPE_THRESH
, IIO_EV_DIR_EITHER
),
69 void xadc_handle_events(struct iio_dev
*indio_dev
, unsigned long events
)
73 for_each_set_bit(i
, &events
, 8)
74 xadc_handle_event(indio_dev
, i
);
77 static unsigned xadc_get_threshold_offset(const struct iio_chan_spec
*chan
,
78 enum iio_event_direction dir
)
82 if (chan
->type
== IIO_TEMP
) {
83 offset
= XADC_THRESHOLD_OT_MAX
;
85 if (chan
->channel
< 2)
86 offset
= chan
->channel
+ 1;
88 offset
= chan
->channel
+ 6;
91 if (dir
== IIO_EV_DIR_FALLING
)
97 static unsigned int xadc_get_alarm_mask(const struct iio_chan_spec
*chan
)
99 if (chan
->type
== IIO_TEMP
) {
100 return XADC_ALARM_OT_MASK
;
102 switch (chan
->channel
) {
104 return XADC_ALARM_VCCINT_MASK
;
106 return XADC_ALARM_VCCAUX_MASK
;
108 return XADC_ALARM_VCCBRAM_MASK
;
110 return XADC_ALARM_VCCPINT_MASK
;
112 return XADC_ALARM_VCCPAUX_MASK
;
114 return XADC_ALARM_VCCODDR_MASK
;
116 /* We will never get here */
122 int xadc_read_event_config(struct iio_dev
*indio_dev
,
123 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
124 enum iio_event_direction dir
)
126 struct xadc
*xadc
= iio_priv(indio_dev
);
128 return (bool)(xadc
->alarm_mask
& xadc_get_alarm_mask(chan
));
131 int xadc_write_event_config(struct iio_dev
*indio_dev
,
132 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
133 enum iio_event_direction dir
, int state
)
135 unsigned int alarm
= xadc_get_alarm_mask(chan
);
136 struct xadc
*xadc
= iio_priv(indio_dev
);
137 uint16_t cfg
, old_cfg
;
140 mutex_lock(&xadc
->mutex
);
143 xadc
->alarm_mask
|= alarm
;
145 xadc
->alarm_mask
&= ~alarm
;
147 xadc
->ops
->update_alarm(xadc
, xadc
->alarm_mask
);
149 ret
= _xadc_read_adc_reg(xadc
, XADC_REG_CONF1
, &cfg
);
154 cfg
|= XADC_CONF1_ALARM_MASK
;
155 cfg
&= ~((xadc
->alarm_mask
& 0xf0) << 4); /* bram, pint, paux, ddr */
156 cfg
&= ~((xadc
->alarm_mask
& 0x08) >> 3); /* ot */
157 cfg
&= ~((xadc
->alarm_mask
& 0x07) << 1); /* temp, vccint, vccaux */
159 ret
= _xadc_write_adc_reg(xadc
, XADC_REG_CONF1
, cfg
);
162 mutex_unlock(&xadc
->mutex
);
167 /* Register value is msb aligned, the lower 4 bits are ignored */
168 #define XADC_THRESHOLD_VALUE_SHIFT 4
170 int xadc_read_event_value(struct iio_dev
*indio_dev
,
171 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
172 enum iio_event_direction dir
, enum iio_event_info info
,
175 unsigned int offset
= xadc_get_threshold_offset(chan
, dir
);
176 struct xadc
*xadc
= iio_priv(indio_dev
);
179 case IIO_EV_INFO_VALUE
:
180 *val
= xadc
->threshold
[offset
];
182 case IIO_EV_INFO_HYSTERESIS
:
183 *val
= xadc
->temp_hysteresis
;
189 *val
>>= XADC_THRESHOLD_VALUE_SHIFT
;
194 int xadc_write_event_value(struct iio_dev
*indio_dev
,
195 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
196 enum iio_event_direction dir
, enum iio_event_info info
,
199 unsigned int offset
= xadc_get_threshold_offset(chan
, dir
);
200 struct xadc
*xadc
= iio_priv(indio_dev
);
203 val
<<= XADC_THRESHOLD_VALUE_SHIFT
;
205 if (val
< 0 || val
> 0xffff)
208 mutex_lock(&xadc
->mutex
);
211 case IIO_EV_INFO_VALUE
:
212 xadc
->threshold
[offset
] = val
;
214 case IIO_EV_INFO_HYSTERESIS
:
215 xadc
->temp_hysteresis
= val
;
218 mutex_unlock(&xadc
->mutex
);
222 if (chan
->type
== IIO_TEMP
) {
224 * According to the datasheet we need to set the lower 4 bits to
225 * 0x3, otherwise 125 degree celsius will be used as the
231 * Since we store the hysteresis as relative (to the threshold)
232 * value, but the hardware expects an absolute value we need to
233 * recalcualte this value whenever the hysteresis or the
236 if (xadc
->threshold
[offset
] < xadc
->temp_hysteresis
)
237 xadc
->threshold
[offset
+ 4] = 0;
239 xadc
->threshold
[offset
+ 4] = xadc
->threshold
[offset
] -
240 xadc
->temp_hysteresis
;
241 ret
= _xadc_write_adc_reg(xadc
, XADC_REG_THRESHOLD(offset
+ 4),
242 xadc
->threshold
[offset
+ 4]);
247 if (info
== IIO_EV_INFO_VALUE
)
248 ret
= _xadc_write_adc_reg(xadc
, XADC_REG_THRESHOLD(offset
), val
);
251 mutex_unlock(&xadc
->mutex
);