10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
38 #define AD7291_COMMAND 0x00
39 #define AD7291_VOLTAGE 0x01
40 #define AD7291_T_SENSE 0x02
41 #define AD7291_T_AVERAGE 0x03
42 #define AD7291_CH0_DATA_HIGH 0x04
43 #define AD7291_CH0_DATA_LOW 0x05
44 #define AD7291_CH0_HYST 0x06
45 #define AD7291_CH1_DATA_HIGH 0x07
46 #define AD7291_CH1_DATA_LOW 0x08
47 #define AD7291_CH1_HYST 0x09
48 #define AD7291_CH2_DATA_HIGH 0x0A
49 #define AD7291_CH2_DATA_LOW 0x0B
50 #define AD7291_CH2_HYST 0x0C
51 #define AD7291_CH3_DATA_HIGH 0x0D
52 #define AD7291_CH3_DATA_LOW 0x0E
53 #define AD7291_CH3_HYST 0x0F
54 #define AD7291_CH4_DATA_HIGH 0x10
55 #define AD7291_CH4_DATA_LOW 0x11
56 #define AD7291_CH4_HYST 0x12
57 #define AD7291_CH5_DATA_HIGH 0x13
58 #define AD7291_CH5_DATA_LOW 0x14
59 #define AD7291_CH5_HYST 0x15
60 #define AD7291_CH6_DATA_HIGH 0x16
61 #define AD7291_CH6_DATA_LOW 0x17
62 #define AD7291_CH6_HYST 0x18
63 #define AD7291_CH7_DATA_HIGH 0x19
64 #define AD7291_CH7_DATA_LOW 0x1A
65 #define AD7291_CH7_HYST 0x2B
66 #define AD7291_T_SENSE_HIGH 0x1C
67 #define AD7291_T_SENSE_LOW 0x1D
68 #define AD7291_T_SENSE_HYST 0x1E
69 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
70 #define AD7291_T_ALERT_STATUS 0x20
72 #define AD7291_VOLTAGE_LIMIT_COUNT 8
78 #define AD7291_AUTOCYCLE (1 << 0)
79 #define AD7291_RESET (1 << 1)
80 #define AD7291_ALERT_CLEAR (1 << 2)
81 #define AD7291_ALERT_POLARITY (1 << 3)
82 #define AD7291_EXT_REF (1 << 4)
83 #define AD7291_NOISE_DELAY (1 << 5)
84 #define AD7291_T_SENSE_MASK (1 << 7)
85 #define AD7291_VOLTAGE_MASK 0xFF00
86 #define AD7291_VOLTAGE_OFFSET 0x8
91 #define AD7291_CHANNEL_MASK 0xF000
92 #define AD7291_BITS 12
93 #define AD7291_VALUE_MASK 0xFFF
94 #define AD7291_T_VALUE_SIGN 0x400
95 #define AD7291_T_VALUE_FLOAT_OFFSET 2
96 #define AD7291_T_VALUE_FLOAT_MASK 0x2
98 #define AD7291_BITS 12
135 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
144 static struct attribute *ad7291_attributes[] = {
145 &iio_dev_attr_reset.dev_attr.attr,
150 .attrs = ad7291_attributes,
153 static irqreturn_t ad7291_event_handler(
int irq,
void *
private)
155 struct iio_dev *indio_dev =
private;
157 u16 t_status, v_status;
168 if (!(t_status || v_status))
178 if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
185 if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
194 if (v_status & (1 << i))
201 if (v_status & (1 << (i + 1)))
217 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
223 ret = ad7291_i2c_read(chip, this_attr->
address, &data);
235 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
248 ret = ad7291_i2c_write(chip, this_attr->
address, data);
257 ad7291_show_hyst, ad7291_set_hyst,
284 static struct attribute *ad7291_event_attributes[] = {
285 &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
286 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
287 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
288 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
289 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
290 &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
291 &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
292 &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
293 &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
298 static u8 ad7291_limit_regs[9][2] = {
311 static int ad7291_read_event_value(
struct iio_dev *indio_dev,
328 ret = ad7291_i2c_read(chip, reg, &uval);
335 reg = ad7291_limit_regs[8]
339 ret = ad7291_i2c_read(chip, reg, &signval);
342 signval = (
s16)((signval & AD7291_VALUE_MASK) << 4) >> 4;
350 static int ad7291_write_event_value(
struct iio_dev *indio_dev,
360 if (val > AD7291_VALUE_MASK || val < 0)
365 return ad7291_i2c_write(chip, reg, val);
367 if (val > 2047 || val < -2048)
369 reg = ad7291_limit_regs[8]
373 return ad7291_i2c_write(chip, reg, *(
u16 *)&signval);
379 static int ad7291_read_event_config(
struct iio_dev *indio_dev,
402 static int ad7291_write_event_config(
struct iio_dev *indio_dev,
420 if ((!state) && (chip->
c_mask & (1 << (15 -
424 else if (state && (!(chip->
c_mask & (1 << (15 -
451 static int ad7291_read_raw(
struct iio_dev *indio_dev,
459 unsigned int scale_uv;
465 switch (chan->
type) {
475 regval |= 1 << (15 - chan->
channel);
498 AD7291_VALUE_MASK) << 4) >> 4;
510 AD7291_VALUE_MASK) << 4) >> 4;
514 switch (chan->
type) {
517 *val = scale_uv / 1000;
518 *val2 = (scale_uv % 1000) * 1000;
536 #define AD7291_VOLTAGE_CHAN(_chan) \
538 .type = IIO_VOLTAGE, \
539 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
540 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
543 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
544 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \
570 .attrs = ad7291_event_attributes,
573 static const struct iio_info ad7291_info = {
574 .attrs = &ad7291_attribute_group,
575 .read_raw = &ad7291_read_raw,
576 .read_event_config = &ad7291_read_event_config,
577 .write_event_config = &ad7291_write_event_config,
578 .read_event_value = &ad7291_read_event_value,
579 .write_event_value = &ad7291_write_event_value,
580 .event_attrs = &ad7291_event_attribute_group,
588 int ret = 0, voltage_uv = 0;
591 if (indio_dev ==
NULL) {
595 chip = iio_priv(indio_dev);
598 if (!IS_ERR(chip->
reg)) {
607 i2c_set_clientdata(client, indio_dev);
622 indio_dev->
name =
id->name;
623 indio_dev->
channels = ad7291_channels;
626 indio_dev->
dev.parent = &client->
dev;
627 indio_dev->
info = &ad7291_info;
633 goto error_disable_reg;
639 goto error_disable_reg;
642 if (client->
irq > 0) {
645 &ad7291_event_handler,
650 goto error_disable_reg;
655 goto error_unreg_irq;
666 if (!IS_ERR(chip->
reg))
669 if (!IS_ERR(chip->
reg))
679 struct iio_dev *indio_dev = i2c_get_clientdata(client);
687 if (!IS_ERR(chip->
reg)) {
706 .name = KBUILD_MODNAME,
708 .probe = ad7291_probe,
710 .id_table = ad7291_id,