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