Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * AD5504, AD5501 High Voltage Digital to Analog Converter |
| 3 | * |
| 4 | * Copyright 2011 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/interrupt.h> |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 10 | #include <linux/fs.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/spi/spi.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/sysfs.h> |
| 16 | #include <linux/regulator/consumer.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 18 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 19 | #include <linux/iio/iio.h> |
| 20 | #include <linux/iio/sysfs.h> |
| 21 | #include <linux/iio/events.h> |
Lars-Peter Clausen | dbdc025 | 2012-06-04 11:36:28 +0200 | [diff] [blame] | 22 | #include <linux/iio/dac/ad5504.h> |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 23 | |
Lars-Peter Clausen | a98348b | 2012-06-04 11:36:26 +0200 | [diff] [blame] | 24 | #define AD5505_BITS 12 |
| 25 | #define AD5504_RES_MASK ((1 << (AD5505_BITS)) - 1) |
| 26 | |
| 27 | #define AD5504_CMD_READ (1 << 15) |
| 28 | #define AD5504_CMD_WRITE (0 << 15) |
| 29 | #define AD5504_ADDR(addr) ((addr) << 12) |
| 30 | |
| 31 | /* Registers */ |
| 32 | #define AD5504_ADDR_NOOP 0 |
| 33 | #define AD5504_ADDR_DAC(x) ((x) + 1) |
| 34 | #define AD5504_ADDR_ALL_DAC 5 |
| 35 | #define AD5504_ADDR_CTRL 7 |
| 36 | |
| 37 | /* Control Register */ |
| 38 | #define AD5504_DAC_PWR(ch) ((ch) << 2) |
| 39 | #define AD5504_DAC_PWRDWN_MODE(mode) ((mode) << 6) |
| 40 | #define AD5504_DAC_PWRDN_20K 0 |
| 41 | #define AD5504_DAC_PWRDN_3STATE 1 |
| 42 | |
| 43 | /** |
| 44 | * struct ad5446_state - driver instance specific data |
| 45 | * @us: spi_device |
| 46 | * @reg: supply regulator |
| 47 | * @vref_mv: actual reference voltage used |
| 48 | * @pwr_down_mask power down mask |
| 49 | * @pwr_down_mode current power down mode |
| 50 | */ |
| 51 | |
| 52 | struct ad5504_state { |
| 53 | struct spi_device *spi; |
| 54 | struct regulator *reg; |
| 55 | unsigned short vref_mv; |
| 56 | unsigned pwr_down_mask; |
| 57 | unsigned pwr_down_mode; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * ad5504_supported_device_ids: |
| 62 | */ |
| 63 | |
| 64 | enum ad5504_supported_device_ids { |
| 65 | ID_AD5504, |
| 66 | ID_AD5501, |
| 67 | }; |
| 68 | |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 69 | static int ad5504_spi_write(struct spi_device *spi, u8 addr, u16 val) |
| 70 | { |
| 71 | u16 tmp = cpu_to_be16(AD5504_CMD_WRITE | |
| 72 | AD5504_ADDR(addr) | |
| 73 | (val & AD5504_RES_MASK)); |
| 74 | |
| 75 | return spi_write(spi, (u8 *)&tmp, 2); |
| 76 | } |
| 77 | |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 78 | static int ad5504_spi_read(struct spi_device *spi, u8 addr) |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 79 | { |
| 80 | u16 tmp = cpu_to_be16(AD5504_CMD_READ | AD5504_ADDR(addr)); |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 81 | u16 val; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 82 | int ret; |
| 83 | struct spi_transfer t = { |
| 84 | .tx_buf = &tmp, |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 85 | .rx_buf = &val, |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 86 | .len = 2, |
| 87 | }; |
| 88 | struct spi_message m; |
| 89 | |
| 90 | spi_message_init(&m); |
| 91 | spi_message_add_tail(&t, &m); |
| 92 | ret = spi_sync(spi, &m); |
| 93 | |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 94 | if (ret < 0) |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 95 | return ret; |
| 96 | |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 97 | return be16_to_cpu(val) & AD5504_RES_MASK; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 100 | static int ad5504_read_raw(struct iio_dev *indio_dev, |
| 101 | struct iio_chan_spec const *chan, |
| 102 | int *val, |
| 103 | int *val2, |
| 104 | long m) |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 105 | { |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 106 | struct ad5504_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 107 | unsigned long scale_uv; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 108 | int ret; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 109 | |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 110 | switch (m) { |
Jonathan Cameron | 09f4eb4 | 2012-04-15 17:41:19 +0100 | [diff] [blame] | 111 | case IIO_CHAN_INFO_RAW: |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 112 | ret = ad5504_spi_read(st->spi, chan->address); |
| 113 | if (ret < 0) |
| 114 | return ret; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 115 | |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 116 | *val = ret; |
| 117 | |
| 118 | return IIO_VAL_INT; |
| 119 | case IIO_CHAN_INFO_SCALE: |
| 120 | scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits; |
| 121 | *val = scale_uv / 1000; |
| 122 | *val2 = (scale_uv % 1000) * 1000; |
| 123 | return IIO_VAL_INT_PLUS_MICRO; |
| 124 | |
| 125 | } |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | static int ad5504_write_raw(struct iio_dev *indio_dev, |
| 130 | struct iio_chan_spec const *chan, |
| 131 | int val, |
| 132 | int val2, |
| 133 | long mask) |
| 134 | { |
| 135 | struct ad5504_state *st = iio_priv(indio_dev); |
| 136 | int ret; |
| 137 | |
| 138 | switch (mask) { |
Jonathan Cameron | 09f4eb4 | 2012-04-15 17:41:19 +0100 | [diff] [blame] | 139 | case IIO_CHAN_INFO_RAW: |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 140 | if (val >= (1 << chan->scan_type.realbits) || val < 0) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | return ad5504_spi_write(st->spi, chan->address, val); |
| 144 | default: |
| 145 | ret = -EINVAL; |
| 146 | } |
| 147 | |
| 148 | return -EINVAL; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 151 | static const char * const ad5504_powerdown_modes[] = { |
| 152 | "20kohm_to_gnd", |
| 153 | "three_state", |
| 154 | }; |
| 155 | |
| 156 | static int ad5504_get_powerdown_mode(struct iio_dev *indio_dev, |
| 157 | const struct iio_chan_spec *chan) |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 158 | { |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 159 | struct ad5504_state *st = iio_priv(indio_dev); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 160 | |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 161 | return st->pwr_down_mode; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 164 | static int ad5504_set_powerdown_mode(struct iio_dev *indio_dev, |
| 165 | const struct iio_chan_spec *chan, unsigned int mode) |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 166 | { |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 167 | struct ad5504_state *st = iio_priv(indio_dev); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 168 | |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 169 | st->pwr_down_mode = mode; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 170 | |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 171 | return 0; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 174 | static const struct iio_enum ad5504_powerdown_mode_enum = { |
| 175 | .items = ad5504_powerdown_modes, |
| 176 | .num_items = ARRAY_SIZE(ad5504_powerdown_modes), |
| 177 | .get = ad5504_get_powerdown_mode, |
| 178 | .set = ad5504_set_powerdown_mode, |
| 179 | }; |
| 180 | |
| 181 | static ssize_t ad5504_read_dac_powerdown(struct iio_dev *indio_dev, |
| 182 | uintptr_t private, const struct iio_chan_spec *chan, char *buf) |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 183 | { |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 184 | struct ad5504_state *st = iio_priv(indio_dev); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 185 | |
| 186 | return sprintf(buf, "%d\n", |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 187 | !(st->pwr_down_mask & (1 << chan->channel))); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 190 | static ssize_t ad5504_write_dac_powerdown(struct iio_dev *indio_dev, |
| 191 | uintptr_t private, const struct iio_chan_spec *chan, const char *buf, |
| 192 | size_t len) |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 193 | { |
Lars-Peter Clausen | 3bbbf15 | 2012-06-04 11:36:22 +0200 | [diff] [blame] | 194 | bool pwr_down; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 195 | int ret; |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 196 | struct ad5504_state *st = iio_priv(indio_dev); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 197 | |
Lars-Peter Clausen | 3bbbf15 | 2012-06-04 11:36:22 +0200 | [diff] [blame] | 198 | ret = strtobool(buf, &pwr_down); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 199 | if (ret) |
| 200 | return ret; |
| 201 | |
Lars-Peter Clausen | 3bbbf15 | 2012-06-04 11:36:22 +0200 | [diff] [blame] | 202 | if (pwr_down) |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 203 | st->pwr_down_mask |= (1 << chan->channel); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 204 | else |
Lars-Peter Clausen | 3bbbf15 | 2012-06-04 11:36:22 +0200 | [diff] [blame] | 205 | st->pwr_down_mask &= ~(1 << chan->channel); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 206 | |
| 207 | ret = ad5504_spi_write(st->spi, AD5504_ADDR_CTRL, |
| 208 | AD5504_DAC_PWRDWN_MODE(st->pwr_down_mode) | |
| 209 | AD5504_DAC_PWR(st->pwr_down_mask)); |
| 210 | |
| 211 | /* writes to the CTRL register must be followed by a NOOP */ |
| 212 | ad5504_spi_write(st->spi, AD5504_ADDR_NOOP, 0); |
| 213 | |
| 214 | return ret ? ret : len; |
| 215 | } |
| 216 | |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 217 | static IIO_CONST_ATTR(temp0_thresh_rising_value, "110000"); |
| 218 | static IIO_CONST_ATTR(temp0_thresh_rising_en, "1"); |
| 219 | |
| 220 | static struct attribute *ad5504_ev_attributes[] = { |
| 221 | &iio_const_attr_temp0_thresh_rising_value.dev_attr.attr, |
| 222 | &iio_const_attr_temp0_thresh_rising_en.dev_attr.attr, |
| 223 | NULL, |
| 224 | }; |
| 225 | |
| 226 | static struct attribute_group ad5504_ev_attribute_group = { |
| 227 | .attrs = ad5504_ev_attributes, |
Jonathan Cameron | 8e7d967 | 2011-08-30 12:32:45 +0100 | [diff] [blame] | 228 | .name = "events", |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 229 | }; |
| 230 | |
Jonathan Cameron | ce298d4 | 2011-05-18 14:41:04 +0100 | [diff] [blame] | 231 | static irqreturn_t ad5504_event_handler(int irq, void *private) |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 232 | { |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 233 | iio_push_event(private, |
Jonathan Cameron | c4b14d9 | 2011-08-12 17:56:04 +0100 | [diff] [blame] | 234 | IIO_UNMOD_EVENT_CODE(IIO_TEMP, |
Jonathan Cameron | ce298d4 | 2011-05-18 14:41:04 +0100 | [diff] [blame] | 235 | 0, |
| 236 | IIO_EV_TYPE_THRESH, |
| 237 | IIO_EV_DIR_RISING), |
| 238 | iio_get_time_ns()); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 239 | |
Jonathan Cameron | ce298d4 | 2011-05-18 14:41:04 +0100 | [diff] [blame] | 240 | return IRQ_HANDLED; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 243 | static const struct iio_info ad5504_info = { |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 244 | .write_raw = ad5504_write_raw, |
| 245 | .read_raw = ad5504_read_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 246 | .event_attrs = &ad5504_ev_attribute_group, |
| 247 | .driver_module = THIS_MODULE, |
| 248 | }; |
| 249 | |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 250 | static const struct iio_chan_spec_ext_info ad5504_ext_info[] = { |
| 251 | { |
| 252 | .name = "powerdown", |
| 253 | .read = ad5504_read_dac_powerdown, |
| 254 | .write = ad5504_write_dac_powerdown, |
| 255 | }, |
| 256 | IIO_ENUM("powerdown_mode", true, &ad5504_powerdown_mode_enum), |
| 257 | IIO_ENUM_AVAILABLE("powerdown_mode", &ad5504_powerdown_mode_enum), |
| 258 | { }, |
| 259 | }; |
| 260 | |
| 261 | #define AD5504_CHANNEL(_chan) { \ |
| 262 | .type = IIO_VOLTAGE, \ |
| 263 | .indexed = 1, \ |
| 264 | .output = 1, \ |
| 265 | .channel = (_chan), \ |
| 266 | .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \ |
| 267 | IIO_CHAN_INFO_SCALE_SHARED_BIT, \ |
| 268 | .address = AD5504_ADDR_DAC(_chan), \ |
| 269 | .scan_type = IIO_ST('u', 12, 16, 0), \ |
| 270 | .ext_info = ad5504_ext_info, \ |
| 271 | } |
| 272 | |
| 273 | static const struct iio_chan_spec ad5504_channels[] = { |
| 274 | AD5504_CHANNEL(0), |
| 275 | AD5504_CHANNEL(1), |
| 276 | AD5504_CHANNEL(2), |
| 277 | AD5504_CHANNEL(3), |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 278 | }; |
| 279 | |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 280 | static int __devinit ad5504_probe(struct spi_device *spi) |
| 281 | { |
| 282 | struct ad5504_platform_data *pdata = spi->dev.platform_data; |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 283 | struct iio_dev *indio_dev; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 284 | struct ad5504_state *st; |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 285 | struct regulator *reg; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 286 | int ret, voltage_uv = 0; |
| 287 | |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 288 | indio_dev = iio_device_alloc(sizeof(*st)); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 289 | if (indio_dev == NULL) { |
| 290 | ret = -ENOMEM; |
| 291 | goto error_ret; |
| 292 | } |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 293 | reg = regulator_get(&spi->dev, "vcc"); |
| 294 | if (!IS_ERR(reg)) { |
| 295 | ret = regulator_enable(reg); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 296 | if (ret) |
| 297 | goto error_put_reg; |
| 298 | |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 299 | voltage_uv = regulator_get_voltage(reg); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 302 | spi_set_drvdata(spi, indio_dev); |
| 303 | st = iio_priv(indio_dev); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 304 | if (voltage_uv) |
| 305 | st->vref_mv = voltage_uv / 1000; |
| 306 | else if (pdata) |
| 307 | st->vref_mv = pdata->vref_mv; |
| 308 | else |
| 309 | dev_warn(&spi->dev, "reference voltage unspecified\n"); |
| 310 | |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 311 | st->reg = reg; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 312 | st->spi = spi; |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 313 | indio_dev->dev.parent = &spi->dev; |
| 314 | indio_dev->name = spi_get_device_id(st->spi)->name; |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 315 | indio_dev->info = &ad5504_info; |
| 316 | if (spi_get_device_id(st->spi)->driver_data == ID_AD5501) |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 317 | indio_dev->num_channels = 1; |
Lars-Peter Clausen | 8d05d77 | 2012-06-04 11:36:15 +0200 | [diff] [blame] | 318 | else |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 319 | indio_dev->num_channels = 4; |
Lars-Peter Clausen | a7b1528 | 2011-11-15 16:31:23 +0100 | [diff] [blame] | 320 | indio_dev->channels = ad5504_channels; |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 321 | indio_dev->modes = INDIO_DIRECT_MODE; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 322 | |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 323 | if (spi->irq) { |
Jonathan Cameron | ce298d4 | 2011-05-18 14:41:04 +0100 | [diff] [blame] | 324 | ret = request_threaded_irq(spi->irq, |
| 325 | NULL, |
| 326 | &ad5504_event_handler, |
| 327 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 328 | spi_get_device_id(st->spi)->name, |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 329 | indio_dev); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 330 | if (ret) |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 331 | goto error_disable_reg; |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 334 | ret = iio_device_register(indio_dev); |
| 335 | if (ret) |
| 336 | goto error_free_irq; |
| 337 | |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 338 | return 0; |
| 339 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 340 | error_free_irq: |
Lars-Peter Clausen | 4f0a788 | 2012-06-04 11:36:25 +0200 | [diff] [blame] | 341 | if (spi->irq) |
| 342 | free_irq(spi->irq, indio_dev); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 343 | error_disable_reg: |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 344 | if (!IS_ERR(reg)) |
Jonathan Cameron | 0cbb2b5 | 2011-08-12 16:55:33 +0100 | [diff] [blame] | 345 | regulator_disable(reg); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 346 | error_put_reg: |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 347 | if (!IS_ERR(reg)) |
| 348 | regulator_put(reg); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 349 | |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 350 | iio_device_free(indio_dev); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 351 | error_ret: |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | static int __devexit ad5504_remove(struct spi_device *spi) |
| 356 | { |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 357 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 358 | struct ad5504_state *st = iio_priv(indio_dev); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 359 | |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 360 | iio_device_unregister(indio_dev); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 361 | if (spi->irq) |
Jonathan Cameron | a3684de | 2011-06-27 13:07:32 +0100 | [diff] [blame] | 362 | free_irq(spi->irq, indio_dev); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 363 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 364 | if (!IS_ERR(st->reg)) { |
| 365 | regulator_disable(st->reg); |
| 366 | regulator_put(st->reg); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 367 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 368 | iio_device_free(indio_dev); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 369 | |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static const struct spi_device_id ad5504_id[] = { |
| 374 | {"ad5504", ID_AD5504}, |
| 375 | {"ad5501", ID_AD5501}, |
| 376 | {} |
| 377 | }; |
Lars-Peter Clausen | 55e4390 | 2011-11-16 08:53:31 +0100 | [diff] [blame] | 378 | MODULE_DEVICE_TABLE(spi, ad5504_id); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 379 | |
| 380 | static struct spi_driver ad5504_driver = { |
| 381 | .driver = { |
| 382 | .name = "ad5504", |
| 383 | .owner = THIS_MODULE, |
| 384 | }, |
| 385 | .probe = ad5504_probe, |
| 386 | .remove = __devexit_p(ad5504_remove), |
| 387 | .id_table = ad5504_id, |
| 388 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 389 | module_spi_driver(ad5504_driver); |
Michael Hennerich | 59c23ea | 2011-04-04 15:39:15 +0200 | [diff] [blame] | 390 | |
| 391 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 392 | MODULE_DESCRIPTION("Analog Devices AD5501/AD5501 DAC"); |
| 393 | MODULE_LICENSE("GPL v2"); |