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