Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 1 | /* |
Michael Hennerich | f22c1cf | 2011-09-02 17:25:37 +0100 | [diff] [blame] | 2 | * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 3 | * |
Michael Hennerich | f22c1cf | 2011-09-02 17:25:37 +0100 | [diff] [blame] | 4 | * Copyright 2010-2011 Analog Devices Inc. |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/interrupt.h> |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 10 | #include <linux/device.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/sysfs.h> |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 14 | #include <linux/i2c.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 17 | #include <linux/regulator/consumer.h> |
| 18 | #include <linux/err.h> |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 19 | |
| 20 | #include "../iio.h" |
| 21 | #include "../sysfs.h" |
| 22 | |
| 23 | /* |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 24 | * Simplified handling |
| 25 | * |
| 26 | * If no events enabled - single polled channel read |
| 27 | * If event enabled direct reads disable unless channel |
| 28 | * is in the read mask. |
| 29 | * |
| 30 | * The noise-delayed bit as per datasheet suggestion is always enabled. |
| 31 | * |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 32 | */ |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 33 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 34 | /* |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 35 | * AD7291 registers definition |
| 36 | */ |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 37 | #define AD7291_COMMAND 0x00 |
| 38 | #define AD7291_VOLTAGE 0x01 |
| 39 | #define AD7291_T_SENSE 0x02 |
| 40 | #define AD7291_T_AVERAGE 0x03 |
| 41 | #define AD7291_CH0_DATA_HIGH 0x04 |
| 42 | #define AD7291_CH0_DATA_LOW 0x05 |
| 43 | #define AD7291_CH0_HYST 0x06 |
| 44 | #define AD7291_CH1_DATA_HIGH 0x07 |
| 45 | #define AD7291_CH1_DATA_LOW 0x08 |
| 46 | #define AD7291_CH1_HYST 0x09 |
| 47 | #define AD7291_CH2_DATA_HIGH 0x0A |
| 48 | #define AD7291_CH2_DATA_LOW 0x0B |
| 49 | #define AD7291_CH2_HYST 0x0C |
| 50 | #define AD7291_CH3_DATA_HIGH 0x0D |
| 51 | #define AD7291_CH3_DATA_LOW 0x0E |
| 52 | #define AD7291_CH3_HYST 0x0F |
| 53 | #define AD7291_CH4_DATA_HIGH 0x10 |
| 54 | #define AD7291_CH4_DATA_LOW 0x11 |
| 55 | #define AD7291_CH4_HYST 0x12 |
| 56 | #define AD7291_CH5_DATA_HIGH 0x13 |
| 57 | #define AD7291_CH5_DATA_LOW 0x14 |
| 58 | #define AD7291_CH5_HYST 0x15 |
| 59 | #define AD7291_CH6_DATA_HIGH 0x16 |
| 60 | #define AD7291_CH6_DATA_LOW 0x17 |
| 61 | #define AD7291_CH6_HYST 0x18 |
| 62 | #define AD7291_CH7_DATA_HIGH 0x19 |
| 63 | #define AD7291_CH7_DATA_LOW 0x1A |
| 64 | #define AD7291_CH7_HYST 0x2B |
Michael Hennerich | 00feefb | 2011-09-02 17:25:38 +0100 | [diff] [blame] | 65 | #define AD7291_T_SENSE_HIGH 0x1C |
| 66 | #define AD7291_T_SENSE_LOW 0x1D |
| 67 | #define AD7291_T_SENSE_HYST 0x1E |
| 68 | #define AD7291_VOLTAGE_ALERT_STATUS 0x1F |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 69 | #define AD7291_T_ALERT_STATUS 0x20 |
| 70 | |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 71 | #define AD7291_VOLTAGE_LIMIT_COUNT 8 |
| 72 | |
| 73 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 74 | /* |
| 75 | * AD7291 command |
| 76 | */ |
Michael Hennerich | 00feefb | 2011-09-02 17:25:38 +0100 | [diff] [blame] | 77 | #define AD7291_AUTOCYCLE (1 << 0) |
| 78 | #define AD7291_RESET (1 << 1) |
| 79 | #define AD7291_ALERT_CLEAR (1 << 2) |
| 80 | #define AD7291_ALERT_POLARITY (1 << 3) |
| 81 | #define AD7291_EXT_REF (1 << 4) |
| 82 | #define AD7291_NOISE_DELAY (1 << 5) |
| 83 | #define AD7291_T_SENSE_MASK (1 << 7) |
| 84 | #define AD7291_VOLTAGE_MASK 0xFF00 |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 85 | #define AD7291_VOLTAGE_OFFSET 0x8 |
| 86 | |
| 87 | /* |
| 88 | * AD7291 value masks |
| 89 | */ |
Michael Hennerich | 00feefb | 2011-09-02 17:25:38 +0100 | [diff] [blame] | 90 | #define AD7291_CHANNEL_MASK 0xF000 |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 91 | #define AD7291_BITS 12 |
Michael Hennerich | 00feefb | 2011-09-02 17:25:38 +0100 | [diff] [blame] | 92 | #define AD7291_VALUE_MASK 0xFFF |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 93 | #define AD7291_T_VALUE_SIGN 0x400 |
| 94 | #define AD7291_T_VALUE_FLOAT_OFFSET 2 |
| 95 | #define AD7291_T_VALUE_FLOAT_MASK 0x2 |
| 96 | |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 97 | #define AD7291_BITS 12 |
| 98 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 99 | struct ad7291_chip_info { |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 100 | struct i2c_client *client; |
| 101 | struct regulator *reg; |
| 102 | u16 int_vref_mv; |
| 103 | u16 command; |
| 104 | u8 c_mask; /* Active voltage channels for events */ |
| 105 | struct mutex state_lock; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 106 | }; |
| 107 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 108 | static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data) |
| 109 | { |
| 110 | struct i2c_client *client = chip->client; |
| 111 | int ret = 0; |
| 112 | |
| 113 | ret = i2c_smbus_read_word_data(client, reg); |
| 114 | if (ret < 0) { |
| 115 | dev_err(&client->dev, "I2C read error\n"); |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | *data = swab16((u16)ret); |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data) |
| 125 | { |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 126 | return i2c_smbus_write_word_data(chip->client, reg, swab16(data)); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 129 | static ssize_t ad7291_store_reset(struct device *dev, |
| 130 | struct device_attribute *attr, |
| 131 | const char *buf, |
| 132 | size_t len) |
| 133 | { |
| 134 | struct iio_dev *dev_info = dev_get_drvdata(dev); |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 135 | struct ad7291_chip_info *chip = iio_priv(dev_info); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 136 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 137 | return ad7291_i2c_write(chip, AD7291_COMMAND, |
| 138 | chip->command | AD7291_RESET); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 141 | static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, ad7291_store_reset, 0); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 142 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 143 | static struct attribute *ad7291_attributes[] = { |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 144 | &iio_dev_attr_reset.dev_attr.attr, |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 145 | NULL, |
| 146 | }; |
| 147 | |
| 148 | static const struct attribute_group ad7291_attribute_group = { |
| 149 | .attrs = ad7291_attributes, |
| 150 | }; |
| 151 | |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 152 | static irqreturn_t ad7291_event_handler(int irq, void *private) |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 153 | { |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 154 | struct iio_dev *indio_dev = private; |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 155 | struct ad7291_chip_info *chip = iio_priv(private); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 156 | u16 t_status, v_status; |
| 157 | u16 command; |
| 158 | int i; |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 159 | s64 timestamp = iio_get_time_ns(); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 160 | |
| 161 | if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status)) |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 162 | return IRQ_HANDLED; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 163 | |
| 164 | if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status)) |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 165 | return IRQ_HANDLED; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 166 | |
| 167 | if (!(t_status || v_status)) |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 168 | return IRQ_HANDLED; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 169 | |
Michael Hennerich | f22c1cf | 2011-09-02 17:25:37 +0100 | [diff] [blame] | 170 | command = chip->command | AD7291_ALERT_CLEAR; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 171 | ad7291_i2c_write(chip, AD7291_COMMAND, command); |
| 172 | |
Michael Hennerich | f22c1cf | 2011-09-02 17:25:37 +0100 | [diff] [blame] | 173 | command = chip->command & ~AD7291_ALERT_CLEAR; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 174 | ad7291_i2c_write(chip, AD7291_COMMAND, command); |
| 175 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 176 | /* For now treat t_sense and t_sense_average the same */ |
| 177 | if ((t_status & (1 << 0)) || (t_status & (1 << 2))) |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 178 | iio_push_event(indio_dev, |
Jonathan Cameron | b206c3b | 2011-05-18 14:42:15 +0100 | [diff] [blame] | 179 | IIO_UNMOD_EVENT_CODE(IIO_TEMP, |
| 180 | 0, |
| 181 | IIO_EV_TYPE_THRESH, |
| 182 | IIO_EV_DIR_FALLING), |
| 183 | timestamp); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 184 | if ((t_status & (1 << 1)) || (t_status & (1 << 3))) |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 185 | iio_push_event(indio_dev, |
Jonathan Cameron | b206c3b | 2011-05-18 14:42:15 +0100 | [diff] [blame] | 186 | IIO_UNMOD_EVENT_CODE(IIO_TEMP, |
| 187 | 0, |
| 188 | IIO_EV_TYPE_THRESH, |
| 189 | IIO_EV_DIR_RISING), |
| 190 | timestamp); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 191 | |
Jonathan Cameron | b206c3b | 2011-05-18 14:42:15 +0100 | [diff] [blame] | 192 | for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) { |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 193 | if (v_status & (1 << i)) |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 194 | iio_push_event(indio_dev, |
Jonathan Cameron | b206c3b | 2011-05-18 14:42:15 +0100 | [diff] [blame] | 195 | IIO_UNMOD_EVENT_CODE(IIO_IN, |
| 196 | i/2, |
| 197 | IIO_EV_TYPE_THRESH, |
| 198 | IIO_EV_DIR_FALLING), |
| 199 | timestamp); |
| 200 | if (v_status & (1 << (i + 1))) |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 201 | iio_push_event(indio_dev, |
Jonathan Cameron | b206c3b | 2011-05-18 14:42:15 +0100 | [diff] [blame] | 202 | IIO_UNMOD_EVENT_CODE(IIO_IN, |
| 203 | i/2, |
| 204 | IIO_EV_TYPE_THRESH, |
| 205 | IIO_EV_DIR_RISING), |
| 206 | timestamp); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 207 | } |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 208 | |
| 209 | return IRQ_HANDLED; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 212 | static inline ssize_t ad7291_show_hyst(struct device *dev, |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 213 | struct device_attribute *attr, |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 214 | char *buf) |
| 215 | { |
| 216 | struct iio_dev *dev_info = dev_get_drvdata(dev); |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 217 | struct ad7291_chip_info *chip = iio_priv(dev_info); |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 218 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 219 | u16 data; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 220 | int ret; |
| 221 | |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 222 | ret = ad7291_i2c_read(chip, this_attr->address, &data); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 223 | if (ret < 0) |
| 224 | return ret; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 225 | |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 226 | return sprintf(buf, "%d\n", data & AD7291_VALUE_MASK); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 229 | static inline ssize_t ad7291_set_hyst(struct device *dev, |
| 230 | struct device_attribute *attr, |
| 231 | const char *buf, |
| 232 | size_t len) |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 233 | { |
| 234 | struct iio_dev *dev_info = dev_get_drvdata(dev); |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 235 | struct ad7291_chip_info *chip = iio_priv(dev_info); |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 236 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 237 | u16 data; |
| 238 | int ret; |
| 239 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 240 | ret = kstrtou16(buf, 10, &data); |
| 241 | |
| 242 | if (ret < 0) |
| 243 | return ret; |
| 244 | if (data < 4096) |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 245 | return -EINVAL; |
| 246 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 247 | return ad7291_i2c_write(chip, this_attr->address, data); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 250 | static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw, |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 251 | S_IRUGO | S_IWUSR, |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 252 | ad7291_show_hyst, ad7291_set_hyst, |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 253 | AD7291_T_SENSE_HYST); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 254 | static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw, |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 255 | S_IRUGO | S_IWUSR, |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 256 | ad7291_show_hyst, ad7291_set_hyst, AD7291_CH0_HYST); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 257 | static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw, |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 258 | S_IRUGO | S_IWUSR, |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 259 | ad7291_show_hyst, ad7291_set_hyst, AD7291_CH1_HYST); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 260 | static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw, |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 261 | S_IRUGO | S_IWUSR, |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 262 | ad7291_show_hyst, ad7291_set_hyst, AD7291_CH2_HYST); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 263 | static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw, |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 264 | S_IRUGO | S_IWUSR, |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 265 | ad7291_show_hyst, ad7291_set_hyst, AD7291_CH3_HYST); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 266 | static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw, |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 267 | S_IRUGO | S_IWUSR, |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 268 | ad7291_show_hyst, ad7291_set_hyst, AD7291_CH4_HYST); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 269 | static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw, |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 270 | S_IRUGO | S_IWUSR, |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 271 | ad7291_show_hyst, ad7291_set_hyst, AD7291_CH5_HYST); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 272 | static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw, |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 273 | S_IRUGO | S_IWUSR, |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 274 | ad7291_show_hyst, ad7291_set_hyst, AD7291_CH6_HYST); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 275 | static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw, |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 276 | S_IRUGO | S_IWUSR, |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 277 | ad7291_show_hyst, ad7291_set_hyst, AD7291_CH7_HYST); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 278 | |
| 279 | static struct attribute *ad7291_event_attributes[] = { |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 280 | &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr, |
| 281 | &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr, |
| 282 | &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr, |
| 283 | &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr, |
| 284 | &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr, |
| 285 | &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr, |
| 286 | &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr, |
| 287 | &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr, |
| 288 | &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr, |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 289 | NULL, |
| 290 | }; |
| 291 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 292 | /* high / low */ |
| 293 | static u8 ad7291_limit_regs[9][2] = { |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 294 | { AD7291_CH0_DATA_HIGH, AD7291_CH0_DATA_LOW }, |
| 295 | { AD7291_CH1_DATA_HIGH, AD7291_CH1_DATA_LOW }, |
| 296 | { AD7291_CH2_DATA_HIGH, AD7291_CH2_DATA_LOW }, |
| 297 | { AD7291_CH3_DATA_HIGH, AD7291_CH3_DATA_LOW }, /* FIXME: ? */ |
| 298 | { AD7291_CH4_DATA_HIGH, AD7291_CH4_DATA_LOW }, |
| 299 | { AD7291_CH5_DATA_HIGH, AD7291_CH5_DATA_LOW }, |
| 300 | { AD7291_CH6_DATA_HIGH, AD7291_CH6_DATA_LOW }, |
| 301 | { AD7291_CH7_DATA_HIGH, AD7291_CH7_DATA_LOW }, |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 302 | /* temp */ |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 303 | { AD7291_T_SENSE_HIGH, AD7291_T_SENSE_LOW }, |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | static int ad7291_read_event_value(struct iio_dev *indio_dev, |
| 307 | u64 event_code, |
| 308 | int *val) |
| 309 | { |
| 310 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
| 311 | |
| 312 | int ret; |
| 313 | u8 reg; |
| 314 | u16 uval; |
| 315 | s16 signval; |
| 316 | |
| 317 | switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) { |
| 318 | case IIO_VOLTAGE: |
| 319 | reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_NUM(event_code)] |
| 320 | [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) == |
| 321 | IIO_EV_DIR_RISING)]; |
| 322 | |
| 323 | ret = ad7291_i2c_read(chip, reg, &uval); |
| 324 | if (ret < 0) |
| 325 | return ret; |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 326 | *val = uval & AD7291_VALUE_MASK; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 327 | return 0; |
| 328 | |
| 329 | case IIO_TEMP: |
| 330 | reg = ad7291_limit_regs[8] |
| 331 | [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) == |
| 332 | IIO_EV_DIR_RISING)]; |
| 333 | |
| 334 | ret = ad7291_i2c_read(chip, reg, &signval); |
| 335 | if (ret < 0) |
| 336 | return ret; |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 337 | signval = (s16)((signval & AD7291_VALUE_MASK) << 4) >> 4; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 338 | *val = signval; |
| 339 | return 0; |
| 340 | default: |
| 341 | return -EINVAL; |
| 342 | }; |
| 343 | } |
| 344 | |
| 345 | static int ad7291_write_event_value(struct iio_dev *indio_dev, |
| 346 | u64 event_code, |
| 347 | int val) |
| 348 | { |
| 349 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
| 350 | u8 reg; |
| 351 | s16 signval; |
| 352 | |
| 353 | switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) { |
| 354 | case IIO_VOLTAGE: |
| 355 | if (val > 0xFFF || val < 0) |
| 356 | return -EINVAL; |
| 357 | reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_NUM(event_code)] |
| 358 | [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) == |
| 359 | IIO_EV_DIR_RISING)]; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 360 | return ad7291_i2c_write(chip, reg, val); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 361 | case IIO_TEMP: |
| 362 | if (val > 2047 || val < -2048) |
| 363 | return -EINVAL; |
| 364 | reg = ad7291_limit_regs[8] |
| 365 | [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) == |
| 366 | IIO_EV_DIR_RISING)]; |
| 367 | signval = val; |
| 368 | return ad7291_i2c_write(chip, reg, *(u16 *)&signval); |
| 369 | default: |
| 370 | return -EINVAL; |
| 371 | }; |
| 372 | } |
| 373 | |
| 374 | static int ad7291_read_event_config(struct iio_dev *indio_dev, |
| 375 | u64 event_code) |
| 376 | { |
| 377 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
| 378 | /* To be enabled the channel must simply be on. If any are enabled |
| 379 | we are in continuous sampling mode */ |
| 380 | |
| 381 | switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) { |
| 382 | case IIO_VOLTAGE: |
| 383 | if (chip->c_mask & |
| 384 | (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code))) |
| 385 | return 1; |
| 386 | else |
| 387 | return 0; |
| 388 | case IIO_TEMP: |
| 389 | /* always on */ |
| 390 | return 1; |
| 391 | default: |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | |
| 395 | } |
| 396 | |
| 397 | static int ad7291_write_event_config(struct iio_dev *indio_dev, |
| 398 | u64 event_code, |
| 399 | int state) |
| 400 | { |
| 401 | int ret = 0; |
| 402 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
| 403 | u16 regval; |
| 404 | |
| 405 | mutex_lock(&chip->state_lock); |
| 406 | regval = chip->command; |
| 407 | /* |
| 408 | * To be enabled the channel must simply be on. If any are enabled |
| 409 | * use continuous sampling mode. |
| 410 | * Possible to disable temp as well but that makes single read tricky. |
| 411 | */ |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 412 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 413 | switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) { |
| 414 | case IIO_VOLTAGE: |
| 415 | if ((!state) && (chip->c_mask & |
| 416 | (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code)))) |
| 417 | chip->c_mask &= |
| 418 | ~(1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code)); |
| 419 | else if (state && (!(chip->c_mask & |
| 420 | (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code))))) |
| 421 | chip->c_mask &= |
| 422 | (1 << IIO_EVENT_CODE_EXTRACT_NUM(event_code)); |
| 423 | else |
| 424 | break; |
| 425 | |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 426 | regval &= ~AD7291_AUTOCYCLE; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 427 | regval |= ((u16)chip->c_mask << 8); |
| 428 | if (chip->c_mask) /* Enable autocycle? */ |
| 429 | regval |= AD7291_AUTOCYCLE; |
| 430 | |
| 431 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval); |
| 432 | if (ret < 0) |
| 433 | goto error_ret; |
| 434 | |
| 435 | chip->command = regval; |
| 436 | break; |
| 437 | default: |
| 438 | ret = -EINVAL; |
| 439 | } |
| 440 | |
| 441 | error_ret: |
| 442 | mutex_unlock(&chip->state_lock); |
| 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | static int ad7291_read_raw(struct iio_dev *indio_dev, |
| 447 | struct iio_chan_spec const *chan, |
| 448 | int *val, |
| 449 | int *val2, |
| 450 | long mask) |
| 451 | { |
| 452 | int ret; |
| 453 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 454 | unsigned int scale_uv; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 455 | u16 regval; |
| 456 | s16 signval; |
| 457 | |
| 458 | switch (mask) { |
| 459 | case 0: |
| 460 | switch (chan->type) { |
| 461 | case IIO_VOLTAGE: |
| 462 | mutex_lock(&chip->state_lock); |
| 463 | /* If in autocycle mode drop through */ |
| 464 | if (chip->command & 0x1) { |
| 465 | mutex_unlock(&chip->state_lock); |
| 466 | return -EBUSY; |
| 467 | } |
| 468 | /* Enable this channel alone */ |
| 469 | regval = chip->command & (~AD7291_VOLTAGE_MASK); |
Michael Hennerich | e70ab21 | 2011-09-02 17:25:36 +0100 | [diff] [blame] | 470 | regval |= 1 << (15 - chan->channel); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 471 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval); |
| 472 | if (ret < 0) { |
| 473 | mutex_unlock(&chip->state_lock); |
| 474 | return ret; |
| 475 | } |
| 476 | /* Read voltage */ |
| 477 | ret = i2c_smbus_read_word_data(chip->client, |
| 478 | AD7291_VOLTAGE); |
| 479 | if (ret < 0) { |
| 480 | mutex_unlock(&chip->state_lock); |
| 481 | return ret; |
| 482 | } |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 483 | *val = swab16((u16)ret) & AD7291_VALUE_MASK; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 484 | mutex_unlock(&chip->state_lock); |
| 485 | return IIO_VAL_INT; |
| 486 | case IIO_TEMP: |
| 487 | /* Assumes tsense bit of command register always set */ |
| 488 | ret = i2c_smbus_read_word_data(chip->client, |
| 489 | AD7291_T_SENSE); |
| 490 | if (ret < 0) |
| 491 | return ret; |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 492 | signval = (s16)((swab16((u16)ret) & |
| 493 | AD7291_VALUE_MASK) << 4) >> 4; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 494 | *val = signval; |
| 495 | return IIO_VAL_INT; |
| 496 | default: |
| 497 | return -EINVAL; |
| 498 | } |
| 499 | case (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE): |
| 500 | ret = i2c_smbus_read_word_data(chip->client, |
| 501 | AD7291_T_AVERAGE); |
| 502 | if (ret < 0) |
| 503 | return ret; |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame^] | 504 | signval = (s16)((swab16((u16)ret) & |
| 505 | AD7291_VALUE_MASK) << 4) >> 4; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 506 | *val = signval; |
| 507 | return IIO_VAL_INT; |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 508 | case (1 << IIO_CHAN_INFO_SCALE_SHARED): |
| 509 | scale_uv = (chip->int_vref_mv * 1000) >> AD7291_BITS; |
| 510 | *val = scale_uv / 1000; |
| 511 | *val2 = (scale_uv % 1000) * 1000; |
| 512 | return IIO_VAL_INT_PLUS_MICRO; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 513 | default: |
| 514 | return -EINVAL; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | #define AD7291_VOLTAGE_CHAN(_chan) \ |
| 519 | { \ |
| 520 | .type = IIO_VOLTAGE, \ |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 521 | .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED), \ |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 522 | .indexed = 1, \ |
| 523 | .channel = _chan, \ |
| 524 | .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\ |
| 525 | IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \ |
| 526 | } |
| 527 | |
| 528 | static const struct iio_chan_spec ad7291_channels[] = { |
| 529 | AD7291_VOLTAGE_CHAN(0), |
| 530 | AD7291_VOLTAGE_CHAN(1), |
| 531 | AD7291_VOLTAGE_CHAN(2), |
| 532 | AD7291_VOLTAGE_CHAN(3), |
| 533 | AD7291_VOLTAGE_CHAN(4), |
| 534 | AD7291_VOLTAGE_CHAN(5), |
| 535 | AD7291_VOLTAGE_CHAN(6), |
| 536 | AD7291_VOLTAGE_CHAN(7), |
| 537 | { |
| 538 | .type = IIO_TEMP, |
| 539 | .info_mask = (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE), |
| 540 | .indexed = 1, |
| 541 | .channel = 0, |
| 542 | .event_mask = |
| 543 | IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)| |
| 544 | IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) |
| 545 | } |
| 546 | }; |
| 547 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 548 | static struct attribute_group ad7291_event_attribute_group = { |
| 549 | .attrs = ad7291_event_attributes, |
| 550 | }; |
| 551 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 552 | static const struct iio_info ad7291_info = { |
| 553 | .attrs = &ad7291_attribute_group, |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 554 | .read_raw = &ad7291_read_raw, |
| 555 | .read_event_config = &ad7291_read_event_config, |
| 556 | .write_event_config = &ad7291_write_event_config, |
| 557 | .read_event_value = &ad7291_read_event_value, |
| 558 | .write_event_value = &ad7291_write_event_value, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 559 | .event_attrs = &ad7291_event_attribute_group, |
| 560 | }; |
| 561 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 562 | static int __devinit ad7291_probe(struct i2c_client *client, |
| 563 | const struct i2c_device_id *id) |
| 564 | { |
| 565 | struct ad7291_chip_info *chip; |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 566 | struct iio_dev *indio_dev; |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 567 | int ret = 0, voltage_uv = 0; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 568 | |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 569 | indio_dev = iio_allocate_device(sizeof(*chip)); |
| 570 | if (indio_dev == NULL) { |
| 571 | ret = -ENOMEM; |
| 572 | goto error_ret; |
| 573 | } |
Dan Carpenter | 03bda05 | 2011-06-29 22:50:48 +0300 | [diff] [blame] | 574 | chip = iio_priv(indio_dev); |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 575 | |
| 576 | chip->reg = regulator_get(&client->dev, "vcc"); |
| 577 | if (!IS_ERR(chip->reg)) { |
| 578 | ret = regulator_enable(chip->reg); |
| 579 | if (ret) |
| 580 | goto error_put_reg; |
| 581 | voltage_uv = regulator_get_voltage(chip->reg); |
| 582 | } |
| 583 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 584 | mutex_init(&chip->state_lock); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 585 | /* this is only used for device removal purposes */ |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 586 | i2c_set_clientdata(client, indio_dev); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 587 | |
| 588 | chip->client = client; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 589 | /* Tsense always enabled */ |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 590 | chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK; |
| 591 | |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 592 | if (voltage_uv) { |
| 593 | chip->int_vref_mv = voltage_uv / 1000; |
| 594 | chip->command |= AD7291_EXT_REF; |
| 595 | } else { |
| 596 | chip->int_vref_mv = 2500; /* Build-in ref */ |
| 597 | } |
| 598 | |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 599 | indio_dev->name = id->name; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 600 | indio_dev->channels = ad7291_channels; |
| 601 | indio_dev->num_channels = ARRAY_SIZE(ad7291_channels); |
| 602 | |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 603 | indio_dev->dev.parent = &client->dev; |
| 604 | indio_dev->info = &ad7291_info; |
| 605 | indio_dev->modes = INDIO_DIRECT_MODE; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 606 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 607 | if (client->irq > 0) { |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 608 | ret = request_threaded_irq(client->irq, |
| 609 | NULL, |
| 610 | &ad7291_event_handler, |
| 611 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
Jonathan Cameron | 845bd12 | 2011-05-18 14:41:44 +0100 | [diff] [blame] | 612 | id->name, |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 613 | indio_dev); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 614 | if (ret) |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 615 | goto error_disable_reg; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 616 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 617 | /* set irq polarity low level */ |
Michael Hennerich | f22c1cf | 2011-09-02 17:25:37 +0100 | [diff] [blame] | 618 | chip->command |= AD7291_ALERT_POLARITY; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command); |
| 622 | if (ret) { |
| 623 | ret = -EIO; |
| 624 | goto error_unreg_irq; |
| 625 | } |
| 626 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 627 | ret = iio_device_register(indio_dev); |
| 628 | if (ret) |
| 629 | goto error_unreg_irq; |
| 630 | |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 631 | dev_info(&client->dev, "%s ADC registered.\n", |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 632 | id->name); |
| 633 | |
| 634 | return 0; |
| 635 | |
| 636 | error_unreg_irq: |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 637 | if (client->irq) |
| 638 | free_irq(client->irq, indio_dev); |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 639 | error_disable_reg: |
| 640 | if (!IS_ERR(chip->reg)) |
| 641 | regulator_disable(chip->reg); |
| 642 | error_put_reg: |
| 643 | if (!IS_ERR(chip->reg)) |
| 644 | regulator_put(chip->reg); |
| 645 | |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 646 | iio_free_device(indio_dev); |
| 647 | error_ret: |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | static int __devexit ad7291_remove(struct i2c_client *client) |
| 652 | { |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 653 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 654 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 655 | |
| 656 | if (client->irq) |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 657 | free_irq(client->irq, indio_dev); |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 658 | |
| 659 | if (!IS_ERR(chip->reg)) { |
| 660 | regulator_disable(chip->reg); |
| 661 | regulator_put(chip->reg); |
| 662 | } |
| 663 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 664 | iio_device_unregister(indio_dev); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static const struct i2c_device_id ad7291_id[] = { |
| 670 | { "ad7291", 0 }, |
| 671 | {} |
| 672 | }; |
| 673 | |
| 674 | MODULE_DEVICE_TABLE(i2c, ad7291_id); |
| 675 | |
| 676 | static struct i2c_driver ad7291_driver = { |
| 677 | .driver = { |
| 678 | .name = "ad7291", |
| 679 | }, |
| 680 | .probe = ad7291_probe, |
| 681 | .remove = __devexit_p(ad7291_remove), |
| 682 | .id_table = ad7291_id, |
| 683 | }; |
| 684 | |
| 685 | static __init int ad7291_init(void) |
| 686 | { |
| 687 | return i2c_add_driver(&ad7291_driver); |
| 688 | } |
| 689 | |
| 690 | static __exit void ad7291_exit(void) |
| 691 | { |
| 692 | i2c_del_driver(&ad7291_driver); |
| 693 | } |
| 694 | |
| 695 | MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>"); |
| 696 | MODULE_DESCRIPTION("Analog Devices AD7291 digital" |
| 697 | " temperature sensor driver"); |
| 698 | MODULE_LICENSE("GPL v2"); |
| 699 | |
| 700 | module_init(ad7291_init); |
| 701 | module_exit(ad7291_exit); |