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 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 9 | #include <linux/device.h> |
Lars-Peter Clausen | 3b1cae7 | 2014-06-30 09:50:00 +0100 | [diff] [blame] | 10 | #include <linux/err.h> |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 11 | #include <linux/i2c.h> |
Lars-Peter Clausen | 3b1cae7 | 2014-06-30 09:50:00 +0100 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/kernel.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 16 | #include <linux/regulator/consumer.h> |
Lars-Peter Clausen | 3b1cae7 | 2014-06-30 09:50:00 +0100 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/sysfs.h> |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 19 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 20 | #include <linux/iio/iio.h> |
| 21 | #include <linux/iio/sysfs.h> |
| 22 | #include <linux/iio/events.h> |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 23 | |
Lars-Peter Clausen | 3b1cae7 | 2014-06-30 09:50:00 +0100 | [diff] [blame] | 24 | #include <linux/platform_data/ad7291.h> |
Lars-Peter Clausen | 1df9d1d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 25 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 26 | /* |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 27 | * Simplified handling |
| 28 | * |
| 29 | * If no events enabled - single polled channel read |
| 30 | * If event enabled direct reads disable unless channel |
| 31 | * is in the read mask. |
| 32 | * |
| 33 | * The noise-delayed bit as per datasheet suggestion is always enabled. |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 34 | */ |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 35 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 36 | /* |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 37 | * AD7291 registers definition |
| 38 | */ |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame] | 39 | #define AD7291_COMMAND 0x00 |
| 40 | #define AD7291_VOLTAGE 0x01 |
| 41 | #define AD7291_T_SENSE 0x02 |
| 42 | #define AD7291_T_AVERAGE 0x03 |
Lars-Peter Clausen | 3f8db0d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 43 | #define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4) |
| 44 | #define AD7291_DATA_LOW(x) ((x) * 3 + 0x5) |
| 45 | #define AD7291_HYST(x) ((x) * 3 + 0x6) |
Michael Hennerich | 00feefb | 2011-09-02 17:25:38 +0100 | [diff] [blame] | 46 | #define AD7291_VOLTAGE_ALERT_STATUS 0x1F |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 47 | #define AD7291_T_ALERT_STATUS 0x20 |
| 48 | |
Hartmut Knaack | 5714d0c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 49 | #define AD7291_BITS 12 |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame] | 50 | #define AD7291_VOLTAGE_LIMIT_COUNT 8 |
| 51 | |
| 52 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 53 | /* |
| 54 | * AD7291 command |
| 55 | */ |
Hartmut Knaack | 5714d0c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 56 | #define AD7291_AUTOCYCLE BIT(0) |
| 57 | #define AD7291_RESET BIT(1) |
| 58 | #define AD7291_ALERT_CLEAR BIT(2) |
| 59 | #define AD7291_ALERT_POLARITY BIT(3) |
| 60 | #define AD7291_EXT_REF BIT(4) |
| 61 | #define AD7291_NOISE_DELAY BIT(5) |
| 62 | #define AD7291_T_SENSE_MASK BIT(7) |
| 63 | #define AD7291_VOLTAGE_MASK GENMASK(15, 8) |
| 64 | #define AD7291_VOLTAGE_OFFSET 8 |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * AD7291 value masks |
| 68 | */ |
Hartmut Knaack | 5714d0c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 69 | #define AD7291_VALUE_MASK GENMASK(11, 0) |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 70 | |
Hartmut Knaack | cfa71bf | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 71 | /* |
| 72 | * AD7291 alert register bits |
| 73 | */ |
| 74 | #define AD7291_T_LOW BIT(0) |
| 75 | #define AD7291_T_HIGH BIT(1) |
| 76 | #define AD7291_T_AVG_LOW BIT(2) |
| 77 | #define AD7291_T_AVG_HIGH BIT(3) |
| 78 | #define AD7291_V_LOW(x) BIT((x) * 2) |
| 79 | #define AD7291_V_HIGH(x) BIT((x) * 2 + 1) |
| 80 | |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 81 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 82 | struct ad7291_chip_info { |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 83 | struct i2c_client *client; |
| 84 | struct regulator *reg; |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 85 | u16 command; |
Michael Hennerich | 06420c2 | 2011-09-02 17:25:43 +0100 | [diff] [blame] | 86 | u16 c_mask; /* Active voltage channels for events */ |
Michael Hennerich | e68b1db | 2011-09-02 17:25:45 +0100 | [diff] [blame] | 87 | struct mutex state_lock; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 88 | }; |
| 89 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 90 | static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data) |
| 91 | { |
| 92 | struct i2c_client *client = chip->client; |
| 93 | int ret = 0; |
| 94 | |
Lars-Peter Clausen | 7bd49fe | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 95 | ret = i2c_smbus_read_word_swapped(client, reg); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 96 | if (ret < 0) { |
| 97 | dev_err(&client->dev, "I2C read error\n"); |
| 98 | return ret; |
| 99 | } |
| 100 | |
Lars-Peter Clausen | 7bd49fe | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 101 | *data = ret; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data) |
| 107 | { |
Lars-Peter Clausen | 7bd49fe | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 108 | return i2c_smbus_write_word_swapped(chip->client, reg, data); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 111 | static irqreturn_t ad7291_event_handler(int irq, void *private) |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 112 | { |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 113 | struct iio_dev *indio_dev = private; |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 114 | struct ad7291_chip_info *chip = iio_priv(private); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 115 | u16 t_status, v_status; |
| 116 | u16 command; |
| 117 | int i; |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 118 | s64 timestamp = iio_get_time_ns(); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 119 | |
| 120 | if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status)) |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 121 | return IRQ_HANDLED; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 122 | |
| 123 | if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status)) |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 124 | return IRQ_HANDLED; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 125 | |
| 126 | if (!(t_status || v_status)) |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 127 | return IRQ_HANDLED; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 128 | |
Michael Hennerich | f22c1cf | 2011-09-02 17:25:37 +0100 | [diff] [blame] | 129 | command = chip->command | AD7291_ALERT_CLEAR; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 130 | ad7291_i2c_write(chip, AD7291_COMMAND, command); |
| 131 | |
Michael Hennerich | f22c1cf | 2011-09-02 17:25:37 +0100 | [diff] [blame] | 132 | command = chip->command & ~AD7291_ALERT_CLEAR; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 133 | ad7291_i2c_write(chip, AD7291_COMMAND, command); |
| 134 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 135 | /* For now treat t_sense and t_sense_average the same */ |
Hartmut Knaack | cfa71bf | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 136 | if ((t_status & AD7291_T_LOW) || (t_status & AD7291_T_AVG_LOW)) |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 137 | iio_push_event(indio_dev, |
Jonathan Cameron | b206c3b | 2011-05-18 14:42:15 +0100 | [diff] [blame] | 138 | IIO_UNMOD_EVENT_CODE(IIO_TEMP, |
| 139 | 0, |
| 140 | IIO_EV_TYPE_THRESH, |
| 141 | IIO_EV_DIR_FALLING), |
| 142 | timestamp); |
Hartmut Knaack | cfa71bf | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 143 | if ((t_status & AD7291_T_HIGH) || (t_status & AD7291_T_AVG_HIGH)) |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 144 | iio_push_event(indio_dev, |
Jonathan Cameron | b206c3b | 2011-05-18 14:42:15 +0100 | [diff] [blame] | 145 | IIO_UNMOD_EVENT_CODE(IIO_TEMP, |
| 146 | 0, |
| 147 | IIO_EV_TYPE_THRESH, |
| 148 | IIO_EV_DIR_RISING), |
| 149 | timestamp); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 150 | |
Hartmut Knaack | cfa71bf | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 151 | for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) { |
| 152 | if (v_status & AD7291_V_LOW(i)) |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 153 | iio_push_event(indio_dev, |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 154 | IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, |
Hartmut Knaack | cfa71bf | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 155 | i, |
Jonathan Cameron | b206c3b | 2011-05-18 14:42:15 +0100 | [diff] [blame] | 156 | IIO_EV_TYPE_THRESH, |
| 157 | IIO_EV_DIR_FALLING), |
| 158 | timestamp); |
Hartmut Knaack | cfa71bf | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 159 | if (v_status & AD7291_V_HIGH(i)) |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 160 | iio_push_event(indio_dev, |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 161 | IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, |
Hartmut Knaack | cfa71bf | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 162 | i, |
Jonathan Cameron | b206c3b | 2011-05-18 14:42:15 +0100 | [diff] [blame] | 163 | IIO_EV_TYPE_THRESH, |
| 164 | IIO_EV_DIR_RISING), |
| 165 | timestamp); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 166 | } |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 167 | |
| 168 | return IRQ_HANDLED; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 171 | static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan, |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 172 | enum iio_event_direction dir, |
| 173 | enum iio_event_info info) |
Lars-Peter Clausen | 3f8db0d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 174 | { |
| 175 | unsigned int offset; |
| 176 | |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 177 | switch (chan->type) { |
Lars-Peter Clausen | 3f8db0d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 178 | case IIO_VOLTAGE: |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 179 | offset = chan->channel; |
Lars-Peter Clausen | 3f8db0d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 180 | break; |
| 181 | case IIO_TEMP: |
Hartmut Knaack | 5714d0c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 182 | offset = AD7291_VOLTAGE_OFFSET; |
Lars-Peter Clausen | 3f8db0d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 183 | break; |
| 184 | default: |
| 185 | return 0; |
| 186 | } |
| 187 | |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 188 | switch (info) { |
| 189 | case IIO_EV_INFO_VALUE: |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 190 | if (dir == IIO_EV_DIR_FALLING) |
| 191 | return AD7291_DATA_HIGH(offset); |
| 192 | else |
| 193 | return AD7291_DATA_LOW(offset); |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 194 | case IIO_EV_INFO_HYSTERESIS: |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 195 | return AD7291_HYST(offset); |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 196 | default: |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 197 | break; |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 198 | } |
| 199 | return 0; |
Lars-Peter Clausen | 3f8db0d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 200 | } |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 201 | |
| 202 | static int ad7291_read_event_value(struct iio_dev *indio_dev, |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 203 | const struct iio_chan_spec *chan, |
| 204 | enum iio_event_type type, |
| 205 | enum iio_event_direction dir, |
| 206 | enum iio_event_info info, |
| 207 | int *val, int *val2) |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 208 | { |
| 209 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 210 | int ret; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 211 | u16 uval; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 212 | |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 213 | ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info), |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 214 | &uval); |
Lars-Peter Clausen | 3f8db0d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 215 | if (ret < 0) |
| 216 | return ret; |
| 217 | |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 218 | if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame] | 219 | *val = uval & AD7291_VALUE_MASK; |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 220 | |
| 221 | else |
Lars-Peter Clausen | 88198d2 | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 222 | *val = sign_extend32(uval, 11); |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 223 | |
| 224 | return IIO_VAL_INT; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int ad7291_write_event_value(struct iio_dev *indio_dev, |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 228 | const struct iio_chan_spec *chan, |
| 229 | enum iio_event_type type, |
| 230 | enum iio_event_direction dir, |
| 231 | enum iio_event_info info, |
| 232 | int val, int val2) |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 233 | { |
| 234 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 235 | |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 236 | if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) { |
Michael Hennerich | a3ef65d | 2011-09-02 17:25:46 +0100 | [diff] [blame] | 237 | if (val > AD7291_VALUE_MASK || val < 0) |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 238 | return -EINVAL; |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 239 | } else { |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 240 | if (val > 2047 || val < -2048) |
| 241 | return -EINVAL; |
Lars-Peter Clausen | 3f8db0d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 244 | return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info), |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 245 | val); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static int ad7291_read_event_config(struct iio_dev *indio_dev, |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 249 | const struct iio_chan_spec *chan, |
| 250 | enum iio_event_type type, |
| 251 | enum iio_event_direction dir) |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 252 | { |
| 253 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 254 | /* |
| 255 | * To be enabled the channel must simply be on. If any are enabled |
| 256 | * we are in continuous sampling mode |
| 257 | */ |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 258 | |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 259 | switch (chan->type) { |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 260 | case IIO_VOLTAGE: |
Hartmut Knaack | 8a32f65 | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 261 | return !!(chip->c_mask & BIT(15 - chan->channel)); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 262 | case IIO_TEMP: |
| 263 | /* always on */ |
| 264 | return 1; |
| 265 | default: |
| 266 | return -EINVAL; |
| 267 | } |
| 268 | |
| 269 | } |
| 270 | |
| 271 | static int ad7291_write_event_config(struct iio_dev *indio_dev, |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 272 | const struct iio_chan_spec *chan, |
| 273 | enum iio_event_type type, |
| 274 | enum iio_event_direction dir, |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 275 | int state) |
| 276 | { |
| 277 | int ret = 0; |
| 278 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 279 | unsigned int mask; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 280 | u16 regval; |
| 281 | |
| 282 | mutex_lock(&chip->state_lock); |
| 283 | regval = chip->command; |
| 284 | /* |
| 285 | * To be enabled the channel must simply be on. If any are enabled |
| 286 | * use continuous sampling mode. |
| 287 | * Possible to disable temp as well but that makes single read tricky. |
| 288 | */ |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame] | 289 | |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 290 | mask = BIT(15 - chan->channel); |
| 291 | |
| 292 | switch (chan->type) { |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 293 | case IIO_VOLTAGE: |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 294 | if ((!state) && (chip->c_mask & mask)) |
| 295 | chip->c_mask &= ~mask; |
| 296 | else if (state && (!(chip->c_mask & mask))) |
| 297 | chip->c_mask |= mask; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 298 | else |
| 299 | break; |
| 300 | |
Michael Hennerich | 81e2edb | 2011-09-02 17:25:40 +0100 | [diff] [blame] | 301 | regval &= ~AD7291_AUTOCYCLE; |
Michael Hennerich | 06420c2 | 2011-09-02 17:25:43 +0100 | [diff] [blame] | 302 | regval |= chip->c_mask; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 303 | if (chip->c_mask) /* Enable autocycle? */ |
| 304 | regval |= AD7291_AUTOCYCLE; |
| 305 | |
| 306 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval); |
| 307 | if (ret < 0) |
| 308 | goto error_ret; |
| 309 | |
| 310 | chip->command = regval; |
| 311 | break; |
| 312 | default: |
| 313 | ret = -EINVAL; |
| 314 | } |
| 315 | |
| 316 | error_ret: |
| 317 | mutex_unlock(&chip->state_lock); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static int ad7291_read_raw(struct iio_dev *indio_dev, |
| 322 | struct iio_chan_spec const *chan, |
| 323 | int *val, |
| 324 | int *val2, |
| 325 | long mask) |
| 326 | { |
| 327 | int ret; |
| 328 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
| 329 | u16 regval; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 330 | |
| 331 | switch (mask) { |
Jonathan Cameron | b11f98f | 2012-04-15 17:41:18 +0100 | [diff] [blame] | 332 | case IIO_CHAN_INFO_RAW: |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 333 | switch (chan->type) { |
| 334 | case IIO_VOLTAGE: |
| 335 | mutex_lock(&chip->state_lock); |
| 336 | /* If in autocycle mode drop through */ |
Michael Hennerich | 06420c2 | 2011-09-02 17:25:43 +0100 | [diff] [blame] | 337 | if (chip->command & AD7291_AUTOCYCLE) { |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 338 | mutex_unlock(&chip->state_lock); |
| 339 | return -EBUSY; |
| 340 | } |
| 341 | /* Enable this channel alone */ |
| 342 | regval = chip->command & (~AD7291_VOLTAGE_MASK); |
Hartmut Knaack | 5714d0c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 343 | regval |= BIT(15 - chan->channel); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 344 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval); |
| 345 | if (ret < 0) { |
| 346 | mutex_unlock(&chip->state_lock); |
| 347 | return ret; |
| 348 | } |
| 349 | /* Read voltage */ |
Lars-Peter Clausen | 7bd49fe | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 350 | ret = i2c_smbus_read_word_swapped(chip->client, |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 351 | AD7291_VOLTAGE); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 352 | if (ret < 0) { |
| 353 | mutex_unlock(&chip->state_lock); |
| 354 | return ret; |
| 355 | } |
Lars-Peter Clausen | 7bd49fe | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 356 | *val = ret & AD7291_VALUE_MASK; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 357 | mutex_unlock(&chip->state_lock); |
| 358 | return IIO_VAL_INT; |
| 359 | case IIO_TEMP: |
| 360 | /* Assumes tsense bit of command register always set */ |
Lars-Peter Clausen | 7bd49fe | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 361 | ret = i2c_smbus_read_word_swapped(chip->client, |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 362 | AD7291_T_SENSE); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 363 | if (ret < 0) |
| 364 | return ret; |
Lars-Peter Clausen | 88198d2 | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 365 | *val = sign_extend32(ret, 11); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 366 | return IIO_VAL_INT; |
| 367 | default: |
| 368 | return -EINVAL; |
| 369 | } |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 370 | case IIO_CHAN_INFO_AVERAGE_RAW: |
Lars-Peter Clausen | 7bd49fe | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 371 | ret = i2c_smbus_read_word_swapped(chip->client, |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 372 | AD7291_T_AVERAGE); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 373 | if (ret < 0) |
| 374 | return ret; |
Lars-Peter Clausen | 88198d2 | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 375 | *val = sign_extend32(ret, 11); |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 376 | return IIO_VAL_INT; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 377 | case IIO_CHAN_INFO_SCALE: |
| 378 | switch (chan->type) { |
| 379 | case IIO_VOLTAGE: |
Lars-Peter Clausen | 1df9d1d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 380 | if (chip->reg) { |
| 381 | int vref; |
Lars-Peter Clausen | 3b1cae7 | 2014-06-30 09:50:00 +0100 | [diff] [blame] | 382 | |
Lars-Peter Clausen | 1df9d1d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 383 | vref = regulator_get_voltage(chip->reg); |
| 384 | if (vref < 0) |
| 385 | return vref; |
| 386 | *val = vref / 1000; |
| 387 | } else { |
| 388 | *val = 2500; |
| 389 | } |
Lars-Peter Clausen | 73a4445 | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 390 | *val2 = AD7291_BITS; |
| 391 | return IIO_VAL_FRACTIONAL_LOG2; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 392 | case IIO_TEMP: |
| 393 | /* |
| 394 | * One LSB of the ADC corresponds to 0.25 deg C. |
| 395 | * The temperature reading is in 12-bit twos |
| 396 | * complement format |
| 397 | */ |
| 398 | *val = 250; |
| 399 | return IIO_VAL_INT; |
| 400 | default: |
| 401 | return -EINVAL; |
| 402 | } |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 403 | default: |
| 404 | return -EINVAL; |
| 405 | } |
| 406 | } |
| 407 | |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 408 | static const struct iio_event_spec ad7291_events[] = { |
| 409 | { |
| 410 | .type = IIO_EV_TYPE_THRESH, |
| 411 | .dir = IIO_EV_DIR_RISING, |
| 412 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 413 | BIT(IIO_EV_INFO_ENABLE), |
| 414 | }, { |
| 415 | .type = IIO_EV_TYPE_THRESH, |
| 416 | .dir = IIO_EV_DIR_FALLING, |
| 417 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 418 | BIT(IIO_EV_INFO_ENABLE), |
Lars-Peter Clausen | ec87197 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 419 | }, { |
| 420 | .type = IIO_EV_TYPE_THRESH, |
| 421 | .dir = IIO_EV_DIR_EITHER, |
| 422 | .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS), |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 423 | }, |
| 424 | }; |
| 425 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 426 | #define AD7291_VOLTAGE_CHAN(_chan) \ |
| 427 | { \ |
| 428 | .type = IIO_VOLTAGE, \ |
Jonathan Cameron | 0325948 | 2013-02-27 19:37:16 +0000 | [diff] [blame] | 429 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 430 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 431 | .indexed = 1, \ |
| 432 | .channel = _chan, \ |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 433 | .event_spec = ad7291_events, \ |
| 434 | .num_event_specs = ARRAY_SIZE(ad7291_events), \ |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static const struct iio_chan_spec ad7291_channels[] = { |
| 438 | AD7291_VOLTAGE_CHAN(0), |
| 439 | AD7291_VOLTAGE_CHAN(1), |
| 440 | AD7291_VOLTAGE_CHAN(2), |
| 441 | AD7291_VOLTAGE_CHAN(3), |
| 442 | AD7291_VOLTAGE_CHAN(4), |
| 443 | AD7291_VOLTAGE_CHAN(5), |
| 444 | AD7291_VOLTAGE_CHAN(6), |
| 445 | AD7291_VOLTAGE_CHAN(7), |
| 446 | { |
| 447 | .type = IIO_TEMP, |
Jonathan Cameron | 0325948 | 2013-02-27 19:37:16 +0000 | [diff] [blame] | 448 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 449 | BIT(IIO_CHAN_INFO_AVERAGE_RAW) | |
| 450 | BIT(IIO_CHAN_INFO_SCALE), |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 451 | .indexed = 1, |
| 452 | .channel = 0, |
Lars-Peter Clausen | b80a674 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 453 | .event_spec = ad7291_events, |
| 454 | .num_event_specs = ARRAY_SIZE(ad7291_events), |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 455 | } |
| 456 | }; |
| 457 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 458 | static const struct iio_info ad7291_info = { |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 459 | .read_raw = &ad7291_read_raw, |
Lars-Peter Clausen | cb95585 | 2013-12-07 10:45:00 +0000 | [diff] [blame] | 460 | .read_event_config = &ad7291_read_event_config, |
| 461 | .write_event_config = &ad7291_write_event_config, |
| 462 | .read_event_value = &ad7291_read_event_value, |
| 463 | .write_event_value = &ad7291_write_event_value, |
Wei Yongjun | bb37796 | 2013-07-04 14:46:00 +0100 | [diff] [blame] | 464 | .driver_module = THIS_MODULE, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 465 | }; |
| 466 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 467 | static int ad7291_probe(struct i2c_client *client, |
Hartmut Knaack | 975d98c | 2014-08-07 12:06:00 +0100 | [diff] [blame] | 468 | const struct i2c_device_id *id) |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 469 | { |
Lars-Peter Clausen | 1df9d1d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 470 | struct ad7291_platform_data *pdata = client->dev.platform_data; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 471 | struct ad7291_chip_info *chip; |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 472 | struct iio_dev *indio_dev; |
Dan Carpenter | b70e19c | 2014-06-20 20:22:00 +0100 | [diff] [blame] | 473 | int ret; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 474 | |
Sachin Kamat | 3e70bad | 2013-08-31 18:12:00 +0100 | [diff] [blame] | 475 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); |
| 476 | if (!indio_dev) |
| 477 | return -ENOMEM; |
Dan Carpenter | 03bda05 | 2011-06-29 22:50:48 +0300 | [diff] [blame] | 478 | chip = iio_priv(indio_dev); |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 479 | |
Lars-Peter Clausen | 1df9d1d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 480 | if (pdata && pdata->use_external_ref) { |
Sachin Kamat | 3e70bad | 2013-08-31 18:12:00 +0100 | [diff] [blame] | 481 | chip->reg = devm_regulator_get(&client->dev, "vref"); |
Lars-Peter Clausen | 1df9d1d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 482 | if (IS_ERR(chip->reg)) |
Dan Carpenter | b70e19c | 2014-06-20 20:22:00 +0100 | [diff] [blame] | 483 | return PTR_ERR(chip->reg); |
Lars-Peter Clausen | 1df9d1d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 484 | |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 485 | ret = regulator_enable(chip->reg); |
| 486 | if (ret) |
Sachin Kamat | 3e70bad | 2013-08-31 18:12:00 +0100 | [diff] [blame] | 487 | return ret; |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 488 | } |
| 489 | |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 490 | mutex_init(&chip->state_lock); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 491 | /* this is only used for device removal purposes */ |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 492 | i2c_set_clientdata(client, indio_dev); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 493 | |
| 494 | chip->client = client; |
Michael Hennerich | c11f02e | 2011-09-02 17:25:44 +0100 | [diff] [blame] | 495 | |
| 496 | chip->command = AD7291_NOISE_DELAY | |
| 497 | AD7291_T_SENSE_MASK | /* Tsense always enabled */ |
| 498 | AD7291_ALERT_POLARITY; /* set irq polarity low level */ |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 499 | |
Lars-Peter Clausen | 1df9d1d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 500 | if (pdata && pdata->use_external_ref) |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 501 | chip->command |= AD7291_EXT_REF; |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 502 | |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 503 | indio_dev->name = id->name; |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 504 | indio_dev->channels = ad7291_channels; |
| 505 | indio_dev->num_channels = ARRAY_SIZE(ad7291_channels); |
| 506 | |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 507 | indio_dev->dev.parent = &client->dev; |
| 508 | indio_dev->info = &ad7291_info; |
| 509 | indio_dev->modes = INDIO_DIRECT_MODE; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 510 | |
Michael Hennerich | c11f02e | 2011-09-02 17:25:44 +0100 | [diff] [blame] | 511 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET); |
| 512 | if (ret) { |
| 513 | ret = -EIO; |
| 514 | goto error_disable_reg; |
| 515 | } |
| 516 | |
| 517 | ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command); |
| 518 | if (ret) { |
| 519 | ret = -EIO; |
| 520 | goto error_disable_reg; |
| 521 | } |
| 522 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 523 | if (client->irq > 0) { |
Jonathan Cameron | 58c0323 | 2011-05-18 14:41:11 +0100 | [diff] [blame] | 524 | ret = request_threaded_irq(client->irq, |
| 525 | NULL, |
| 526 | &ad7291_event_handler, |
| 527 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
Jonathan Cameron | 845bd12 | 2011-05-18 14:41:44 +0100 | [diff] [blame] | 528 | id->name, |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 529 | indio_dev); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 530 | if (ret) |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 531 | goto error_disable_reg; |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 534 | ret = iio_device_register(indio_dev); |
| 535 | if (ret) |
| 536 | goto error_unreg_irq; |
| 537 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 538 | return 0; |
| 539 | |
| 540 | error_unreg_irq: |
Jonathan Cameron | 4fd2424 | 2011-09-02 17:25:32 +0100 | [diff] [blame] | 541 | if (client->irq) |
| 542 | free_irq(client->irq, indio_dev); |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 543 | error_disable_reg: |
Lars-Peter Clausen | 1df9d1d | 2013-06-14 16:58:00 +0100 | [diff] [blame] | 544 | if (chip->reg) |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 545 | regulator_disable(chip->reg); |
Sachin Kamat | 3e70bad | 2013-08-31 18:12:00 +0100 | [diff] [blame] | 546 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 547 | return ret; |
| 548 | } |
| 549 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 550 | static int ad7291_remove(struct i2c_client *client) |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 551 | { |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 552 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 553 | struct ad7291_chip_info *chip = iio_priv(indio_dev); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 554 | |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 555 | iio_device_unregister(indio_dev); |
| 556 | |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 557 | if (client->irq) |
Jonathan Cameron | d439797 | 2011-06-27 13:07:23 +0100 | [diff] [blame] | 558 | free_irq(client->irq, indio_dev); |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 559 | |
Sachin Kamat | 3e70bad | 2013-08-31 18:12:00 +0100 | [diff] [blame] | 560 | if (chip->reg) |
Michael Hennerich | 3b424f3 | 2011-09-02 17:25:39 +0100 | [diff] [blame] | 561 | regulator_disable(chip->reg); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static const struct i2c_device_id ad7291_id[] = { |
| 567 | { "ad7291", 0 }, |
| 568 | {} |
| 569 | }; |
| 570 | |
| 571 | MODULE_DEVICE_TABLE(i2c, ad7291_id); |
| 572 | |
| 573 | static struct i2c_driver ad7291_driver = { |
| 574 | .driver = { |
Michael Hennerich | e68b1db | 2011-09-02 17:25:45 +0100 | [diff] [blame] | 575 | .name = KBUILD_MODNAME, |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 576 | }, |
| 577 | .probe = ad7291_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 578 | .remove = ad7291_remove, |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 579 | .id_table = ad7291_id, |
| 580 | }; |
Lars-Peter Clausen | 6e5af18 | 2011-11-16 10:13:38 +0100 | [diff] [blame] | 581 | module_i2c_driver(ad7291_driver); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 582 | |
| 583 | MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>"); |
Michael Hennerich | e68b1db | 2011-09-02 17:25:45 +0100 | [diff] [blame] | 584 | MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver"); |
Sonic Zhang | ddaecd5 | 2010-10-27 21:43:55 -0400 | [diff] [blame] | 585 | MODULE_LICENSE("GPL v2"); |