Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 1 | /* |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 2 | * AD5024, AD5025, AD5044, AD5045, AD5064, AD5064-1, AD5065, AD5628, AD5629R, |
| 3 | * AD5648, AD5666, AD5668, AD5669R Digital to analog converters driver |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2011 Analog Devices Inc. |
| 6 | * |
| 7 | * Licensed under the GPL-2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/spi/spi.h> |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 15 | #include <linux/i2c.h> |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/sysfs.h> |
| 18 | #include <linux/regulator/consumer.h> |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 19 | #include <asm/unaligned.h> |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 20 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 21 | #include <linux/iio/iio.h> |
| 22 | #include <linux/iio/sysfs.h> |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 23 | |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 24 | #define AD5064_MAX_DAC_CHANNELS 8 |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 25 | #define AD5064_MAX_VREFS 4 |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 26 | |
| 27 | #define AD5064_ADDR(x) ((x) << 20) |
| 28 | #define AD5064_CMD(x) ((x) << 24) |
| 29 | |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 30 | #define AD5064_ADDR_ALL_DAC 0xF |
| 31 | |
| 32 | #define AD5064_CMD_WRITE_INPUT_N 0x0 |
| 33 | #define AD5064_CMD_UPDATE_DAC_N 0x1 |
| 34 | #define AD5064_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2 |
| 35 | #define AD5064_CMD_WRITE_INPUT_N_UPDATE_N 0x3 |
| 36 | #define AD5064_CMD_POWERDOWN_DAC 0x4 |
| 37 | #define AD5064_CMD_CLEAR 0x5 |
| 38 | #define AD5064_CMD_LDAC_MASK 0x6 |
| 39 | #define AD5064_CMD_RESET 0x7 |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 40 | #define AD5064_CMD_CONFIG 0x8 |
| 41 | |
| 42 | #define AD5064_CONFIG_DAISY_CHAIN_ENABLE BIT(1) |
| 43 | #define AD5064_CONFIG_INT_VREF_ENABLE BIT(0) |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 44 | |
| 45 | #define AD5064_LDAC_PWRDN_NONE 0x0 |
| 46 | #define AD5064_LDAC_PWRDN_1K 0x1 |
| 47 | #define AD5064_LDAC_PWRDN_100K 0x2 |
| 48 | #define AD5064_LDAC_PWRDN_3STATE 0x3 |
| 49 | |
| 50 | /** |
| 51 | * struct ad5064_chip_info - chip specific information |
| 52 | * @shared_vref: whether the vref supply is shared between channels |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 53 | * @internal_vref: internal reference voltage. 0 if the chip has no internal |
| 54 | * vref. |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 55 | * @channel: channel specification |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 56 | * @num_channels: number of channels |
| 57 | */ |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 58 | |
| 59 | struct ad5064_chip_info { |
| 60 | bool shared_vref; |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 61 | unsigned long internal_vref; |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 62 | const struct iio_chan_spec *channels; |
| 63 | unsigned int num_channels; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 66 | struct ad5064_state; |
| 67 | |
| 68 | typedef int (*ad5064_write_func)(struct ad5064_state *st, unsigned int cmd, |
| 69 | unsigned int addr, unsigned int val); |
| 70 | |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 71 | /** |
| 72 | * struct ad5064_state - driver instance specific data |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 73 | * @dev: the device for this driver instance |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 74 | * @chip_info: chip model specific constants, available modes etc |
| 75 | * @vref_reg: vref supply regulators |
| 76 | * @pwr_down: whether channel is powered down |
| 77 | * @pwr_down_mode: channel's current power down mode |
| 78 | * @dac_cache: current DAC raw value (chip does not support readback) |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 79 | * @use_internal_vref: set to true if the internal reference voltage should be |
| 80 | * used. |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 81 | * @write: register write callback |
| 82 | * @data: i2c/spi transfer buffers |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 83 | */ |
| 84 | |
| 85 | struct ad5064_state { |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 86 | struct device *dev; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 87 | const struct ad5064_chip_info *chip_info; |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 88 | struct regulator_bulk_data vref_reg[AD5064_MAX_VREFS]; |
| 89 | bool pwr_down[AD5064_MAX_DAC_CHANNELS]; |
| 90 | u8 pwr_down_mode[AD5064_MAX_DAC_CHANNELS]; |
| 91 | unsigned int dac_cache[AD5064_MAX_DAC_CHANNELS]; |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 92 | bool use_internal_vref; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 93 | |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 94 | ad5064_write_func write; |
| 95 | |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 96 | /* |
| 97 | * DMA (thus cache coherency maintenance) requires the |
| 98 | * transfer buffers to live in their own cache lines. |
| 99 | */ |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 100 | union { |
| 101 | u8 i2c[3]; |
| 102 | __be32 spi; |
| 103 | } data ____cacheline_aligned; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | enum ad5064_type { |
| 107 | ID_AD5024, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 108 | ID_AD5025, |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 109 | ID_AD5044, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 110 | ID_AD5045, |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 111 | ID_AD5064, |
| 112 | ID_AD5064_1, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 113 | ID_AD5065, |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 114 | ID_AD5628_1, |
| 115 | ID_AD5628_2, |
| 116 | ID_AD5648_1, |
| 117 | ID_AD5648_2, |
Lars-Peter Clausen | 64f4eaa | 2012-02-21 18:38:17 +0100 | [diff] [blame] | 118 | ID_AD5666_1, |
| 119 | ID_AD5666_2, |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 120 | ID_AD5668_1, |
| 121 | ID_AD5668_2, |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 124 | static int ad5064_write(struct ad5064_state *st, unsigned int cmd, |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 125 | unsigned int addr, unsigned int val, unsigned int shift) |
| 126 | { |
| 127 | val <<= shift; |
| 128 | |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 129 | return st->write(st, cmd, addr, val); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static int ad5064_sync_powerdown_mode(struct ad5064_state *st, |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 133 | const struct iio_chan_spec *chan) |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 134 | { |
| 135 | unsigned int val; |
| 136 | int ret; |
| 137 | |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 138 | val = (0x1 << chan->address); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 139 | |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 140 | if (st->pwr_down[chan->channel]) |
| 141 | val |= st->pwr_down_mode[chan->channel] << 8; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 142 | |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 143 | ret = ad5064_write(st, AD5064_CMD_POWERDOWN_DAC, 0, val, 0); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
Lars-Peter Clausen | 26628f6 | 2012-06-04 11:36:12 +0200 | [diff] [blame] | 148 | static const char * const ad5064_powerdown_modes[] = { |
| 149 | "1kohm_to_gnd", |
| 150 | "100kohm_to_gnd", |
| 151 | "three_state", |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 152 | }; |
| 153 | |
Lars-Peter Clausen | 26628f6 | 2012-06-04 11:36:12 +0200 | [diff] [blame] | 154 | static int ad5064_get_powerdown_mode(struct iio_dev *indio_dev, |
| 155 | const struct iio_chan_spec *chan) |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 156 | { |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 157 | struct ad5064_state *st = iio_priv(indio_dev); |
| 158 | |
Lars-Peter Clausen | 26628f6 | 2012-06-04 11:36:12 +0200 | [diff] [blame] | 159 | return st->pwr_down_mode[chan->channel] - 1; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Lars-Peter Clausen | 26628f6 | 2012-06-04 11:36:12 +0200 | [diff] [blame] | 162 | static int ad5064_set_powerdown_mode(struct iio_dev *indio_dev, |
| 163 | const struct iio_chan_spec *chan, unsigned int mode) |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 164 | { |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 165 | struct ad5064_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 166 | int ret; |
| 167 | |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 168 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | 26628f6 | 2012-06-04 11:36:12 +0200 | [diff] [blame] | 169 | st->pwr_down_mode[chan->channel] = mode + 1; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 170 | |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 171 | ret = ad5064_sync_powerdown_mode(st, chan); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 172 | mutex_unlock(&indio_dev->mlock); |
| 173 | |
Lars-Peter Clausen | 26628f6 | 2012-06-04 11:36:12 +0200 | [diff] [blame] | 174 | return ret; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Lars-Peter Clausen | 26628f6 | 2012-06-04 11:36:12 +0200 | [diff] [blame] | 177 | static const struct iio_enum ad5064_powerdown_mode_enum = { |
| 178 | .items = ad5064_powerdown_modes, |
| 179 | .num_items = ARRAY_SIZE(ad5064_powerdown_modes), |
| 180 | .get = ad5064_get_powerdown_mode, |
| 181 | .set = ad5064_set_powerdown_mode, |
| 182 | }; |
| 183 | |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 184 | static ssize_t ad5064_read_dac_powerdown(struct iio_dev *indio_dev, |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 185 | uintptr_t private, const struct iio_chan_spec *chan, char *buf) |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 186 | { |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 187 | struct ad5064_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 188 | |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 189 | return sprintf(buf, "%d\n", st->pwr_down[chan->channel]); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 192 | static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev, |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 193 | uintptr_t private, const struct iio_chan_spec *chan, const char *buf, |
| 194 | size_t len) |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 195 | { |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 196 | struct ad5064_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 197 | bool pwr_down; |
| 198 | int ret; |
| 199 | |
| 200 | ret = strtobool(buf, &pwr_down); |
| 201 | if (ret) |
| 202 | return ret; |
| 203 | |
| 204 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 205 | st->pwr_down[chan->channel] = pwr_down; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 206 | |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 207 | ret = ad5064_sync_powerdown_mode(st, chan); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 208 | mutex_unlock(&indio_dev->mlock); |
| 209 | return ret ? ret : len; |
| 210 | } |
| 211 | |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 212 | static int ad5064_get_vref(struct ad5064_state *st, |
| 213 | struct iio_chan_spec const *chan) |
| 214 | { |
| 215 | unsigned int i; |
| 216 | |
| 217 | if (st->use_internal_vref) |
| 218 | return st->chip_info->internal_vref; |
| 219 | |
| 220 | i = st->chip_info->shared_vref ? 0 : chan->channel; |
| 221 | return regulator_get_voltage(st->vref_reg[i].consumer); |
| 222 | } |
| 223 | |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 224 | static int ad5064_read_raw(struct iio_dev *indio_dev, |
| 225 | struct iio_chan_spec const *chan, |
| 226 | int *val, |
| 227 | int *val2, |
| 228 | long m) |
| 229 | { |
| 230 | struct ad5064_state *st = iio_priv(indio_dev); |
Dan Carpenter | 23a3b8c | 2011-10-29 10:20:42 +0300 | [diff] [blame] | 231 | int scale_uv; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 232 | |
| 233 | switch (m) { |
Jonathan Cameron | 09f4eb4 | 2012-04-15 17:41:19 +0100 | [diff] [blame] | 234 | case IIO_CHAN_INFO_RAW: |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 235 | *val = st->dac_cache[chan->channel]; |
| 236 | return IIO_VAL_INT; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 237 | case IIO_CHAN_INFO_SCALE: |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 238 | scale_uv = ad5064_get_vref(st, chan); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 239 | if (scale_uv < 0) |
| 240 | return scale_uv; |
| 241 | |
Lars-Peter Clausen | 25682ae | 2013-09-28 10:31:00 +0100 | [diff] [blame] | 242 | *val = scale_uv / 1000; |
| 243 | *val2 = chan->scan_type.realbits; |
| 244 | return IIO_VAL_FRACTIONAL_LOG2; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 245 | default: |
| 246 | break; |
| 247 | } |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | |
| 251 | static int ad5064_write_raw(struct iio_dev *indio_dev, |
| 252 | struct iio_chan_spec const *chan, int val, int val2, long mask) |
| 253 | { |
| 254 | struct ad5064_state *st = iio_priv(indio_dev); |
| 255 | int ret; |
| 256 | |
| 257 | switch (mask) { |
Jonathan Cameron | 09f4eb4 | 2012-04-15 17:41:19 +0100 | [diff] [blame] | 258 | case IIO_CHAN_INFO_RAW: |
Lars-Peter Clausen | c5ef717 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 259 | if (val >= (1 << chan->scan_type.realbits) || val < 0) |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 260 | return -EINVAL; |
| 261 | |
| 262 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 263 | ret = ad5064_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N, |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 264 | chan->address, val, chan->scan_type.shift); |
| 265 | if (ret == 0) |
| 266 | st->dac_cache[chan->channel] = val; |
| 267 | mutex_unlock(&indio_dev->mlock); |
| 268 | break; |
| 269 | default: |
| 270 | ret = -EINVAL; |
| 271 | } |
| 272 | |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | static const struct iio_info ad5064_info = { |
| 277 | .read_raw = ad5064_read_raw, |
| 278 | .write_raw = ad5064_write_raw, |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 279 | .driver_module = THIS_MODULE, |
| 280 | }; |
| 281 | |
Lars-Peter Clausen | 26628f6 | 2012-06-04 11:36:12 +0200 | [diff] [blame] | 282 | static const struct iio_chan_spec_ext_info ad5064_ext_info[] = { |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 283 | { |
| 284 | .name = "powerdown", |
| 285 | .read = ad5064_read_dac_powerdown, |
| 286 | .write = ad5064_write_dac_powerdown, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 287 | .shared = IIO_SEPARATE, |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 288 | }, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 289 | IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ad5064_powerdown_mode_enum), |
Lars-Peter Clausen | 26628f6 | 2012-06-04 11:36:12 +0200 | [diff] [blame] | 290 | IIO_ENUM_AVAILABLE("powerdown_mode", &ad5064_powerdown_mode_enum), |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 291 | { }, |
| 292 | }; |
| 293 | |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 294 | #define AD5064_CHANNEL(chan, addr, bits) { \ |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 295 | .type = IIO_VOLTAGE, \ |
| 296 | .indexed = 1, \ |
| 297 | .output = 1, \ |
| 298 | .channel = (chan), \ |
Jonathan Cameron | 20a0edd | 2013-02-27 19:08:37 +0000 | [diff] [blame] | 299 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 300 | BIT(IIO_CHAN_INFO_SCALE), \ |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 301 | .address = addr, \ |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 302 | .scan_type = IIO_ST('u', (bits), 16, 20 - (bits)), \ |
| 303 | .ext_info = ad5064_ext_info, \ |
| 304 | } |
| 305 | |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 306 | #define DECLARE_AD5064_CHANNELS(name, bits) \ |
| 307 | const struct iio_chan_spec name[] = { \ |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 308 | AD5064_CHANNEL(0, 0, bits), \ |
| 309 | AD5064_CHANNEL(1, 1, bits), \ |
| 310 | AD5064_CHANNEL(2, 2, bits), \ |
| 311 | AD5064_CHANNEL(3, 3, bits), \ |
| 312 | AD5064_CHANNEL(4, 4, bits), \ |
| 313 | AD5064_CHANNEL(5, 5, bits), \ |
| 314 | AD5064_CHANNEL(6, 6, bits), \ |
| 315 | AD5064_CHANNEL(7, 7, bits), \ |
| 316 | } |
| 317 | |
| 318 | #define DECLARE_AD5065_CHANNELS(name, bits) \ |
| 319 | const struct iio_chan_spec name[] = { \ |
| 320 | AD5064_CHANNEL(0, 0, bits), \ |
| 321 | AD5064_CHANNEL(1, 3, bits), \ |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static DECLARE_AD5064_CHANNELS(ad5024_channels, 12); |
| 325 | static DECLARE_AD5064_CHANNELS(ad5044_channels, 14); |
| 326 | static DECLARE_AD5064_CHANNELS(ad5064_channels, 16); |
| 327 | |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 328 | static DECLARE_AD5065_CHANNELS(ad5025_channels, 12); |
| 329 | static DECLARE_AD5065_CHANNELS(ad5045_channels, 14); |
| 330 | static DECLARE_AD5065_CHANNELS(ad5065_channels, 16); |
| 331 | |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 332 | static const struct ad5064_chip_info ad5064_chip_info_tbl[] = { |
| 333 | [ID_AD5024] = { |
| 334 | .shared_vref = false, |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 335 | .channels = ad5024_channels, |
| 336 | .num_channels = 4, |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 337 | }, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 338 | [ID_AD5025] = { |
| 339 | .shared_vref = false, |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 340 | .channels = ad5025_channels, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 341 | .num_channels = 2, |
| 342 | }, |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 343 | [ID_AD5044] = { |
| 344 | .shared_vref = false, |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 345 | .channels = ad5044_channels, |
| 346 | .num_channels = 4, |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 347 | }, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 348 | [ID_AD5045] = { |
| 349 | .shared_vref = false, |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 350 | .channels = ad5045_channels, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 351 | .num_channels = 2, |
| 352 | }, |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 353 | [ID_AD5064] = { |
| 354 | .shared_vref = false, |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 355 | .channels = ad5064_channels, |
| 356 | .num_channels = 4, |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 357 | }, |
| 358 | [ID_AD5064_1] = { |
| 359 | .shared_vref = true, |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 360 | .channels = ad5064_channels, |
| 361 | .num_channels = 4, |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 362 | }, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 363 | [ID_AD5065] = { |
| 364 | .shared_vref = false, |
Lars-Peter Clausen | a263026 | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 365 | .channels = ad5065_channels, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 366 | .num_channels = 2, |
| 367 | }, |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 368 | [ID_AD5628_1] = { |
| 369 | .shared_vref = true, |
| 370 | .internal_vref = 2500000, |
| 371 | .channels = ad5024_channels, |
| 372 | .num_channels = 8, |
| 373 | }, |
| 374 | [ID_AD5628_2] = { |
| 375 | .shared_vref = true, |
| 376 | .internal_vref = 5000000, |
| 377 | .channels = ad5024_channels, |
| 378 | .num_channels = 8, |
| 379 | }, |
| 380 | [ID_AD5648_1] = { |
| 381 | .shared_vref = true, |
| 382 | .internal_vref = 2500000, |
| 383 | .channels = ad5044_channels, |
| 384 | .num_channels = 8, |
| 385 | }, |
| 386 | [ID_AD5648_2] = { |
| 387 | .shared_vref = true, |
| 388 | .internal_vref = 5000000, |
| 389 | .channels = ad5044_channels, |
| 390 | .num_channels = 8, |
| 391 | }, |
Lars-Peter Clausen | 64f4eaa | 2012-02-21 18:38:17 +0100 | [diff] [blame] | 392 | [ID_AD5666_1] = { |
| 393 | .shared_vref = true, |
| 394 | .internal_vref = 2500000, |
| 395 | .channels = ad5064_channels, |
| 396 | .num_channels = 4, |
| 397 | }, |
| 398 | [ID_AD5666_2] = { |
| 399 | .shared_vref = true, |
| 400 | .internal_vref = 5000000, |
| 401 | .channels = ad5064_channels, |
| 402 | .num_channels = 4, |
| 403 | }, |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 404 | [ID_AD5668_1] = { |
| 405 | .shared_vref = true, |
| 406 | .internal_vref = 2500000, |
| 407 | .channels = ad5064_channels, |
| 408 | .num_channels = 8, |
| 409 | }, |
| 410 | [ID_AD5668_2] = { |
| 411 | .shared_vref = true, |
| 412 | .internal_vref = 5000000, |
| 413 | .channels = ad5064_channels, |
| 414 | .num_channels = 8, |
| 415 | }, |
Lars-Peter Clausen | 1d0d879 | 2012-02-21 18:38:13 +0100 | [diff] [blame] | 416 | }; |
| 417 | |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 418 | static inline unsigned int ad5064_num_vref(struct ad5064_state *st) |
| 419 | { |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 420 | return st->chip_info->shared_vref ? 1 : st->chip_info->num_channels; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static const char * const ad5064_vref_names[] = { |
| 424 | "vrefA", |
| 425 | "vrefB", |
| 426 | "vrefC", |
| 427 | "vrefD", |
| 428 | }; |
| 429 | |
| 430 | static const char * const ad5064_vref_name(struct ad5064_state *st, |
| 431 | unsigned int vref) |
| 432 | { |
| 433 | return st->chip_info->shared_vref ? "vref" : ad5064_vref_names[vref]; |
| 434 | } |
| 435 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 436 | static int ad5064_probe(struct device *dev, enum ad5064_type type, |
| 437 | const char *name, ad5064_write_func write) |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 438 | { |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 439 | struct iio_dev *indio_dev; |
| 440 | struct ad5064_state *st; |
Lars-Peter Clausen | f77ae9d | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 441 | unsigned int midscale; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 442 | unsigned int i; |
| 443 | int ret; |
| 444 | |
Sachin Kamat | c367982 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 445 | indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 446 | if (indio_dev == NULL) |
| 447 | return -ENOMEM; |
| 448 | |
| 449 | st = iio_priv(indio_dev); |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 450 | dev_set_drvdata(dev, indio_dev); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 451 | |
| 452 | st->chip_info = &ad5064_chip_info_tbl[type]; |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 453 | st->dev = dev; |
| 454 | st->write = write; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 455 | |
| 456 | for (i = 0; i < ad5064_num_vref(st); ++i) |
| 457 | st->vref_reg[i].supply = ad5064_vref_name(st, i); |
| 458 | |
Sachin Kamat | c367982 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 459 | ret = devm_regulator_bulk_get(dev, ad5064_num_vref(st), |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 460 | st->vref_reg); |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 461 | if (ret) { |
| 462 | if (!st->chip_info->internal_vref) |
Sachin Kamat | c367982 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 463 | return ret; |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 464 | st->use_internal_vref = true; |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 465 | ret = ad5064_write(st, AD5064_CMD_CONFIG, 0, |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 466 | AD5064_CONFIG_INT_VREF_ENABLE, 0); |
| 467 | if (ret) { |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 468 | dev_err(dev, "Failed to enable internal vref: %d\n", |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 469 | ret); |
Sachin Kamat | c367982 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 470 | return ret; |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 471 | } |
| 472 | } else { |
| 473 | ret = regulator_bulk_enable(ad5064_num_vref(st), st->vref_reg); |
| 474 | if (ret) |
Sachin Kamat | c367982 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 475 | return ret; |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 476 | } |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 477 | |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 478 | indio_dev->dev.parent = dev; |
| 479 | indio_dev->name = name; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 480 | indio_dev->info = &ad5064_info; |
| 481 | indio_dev->modes = INDIO_DIRECT_MODE; |
Lars-Peter Clausen | 83c169d | 2012-02-21 18:38:14 +0100 | [diff] [blame] | 482 | indio_dev->channels = st->chip_info->channels; |
| 483 | indio_dev->num_channels = st->chip_info->num_channels; |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 484 | |
Lars-Peter Clausen | f77ae9d | 2013-03-01 13:07:00 +0000 | [diff] [blame] | 485 | midscale = (1 << indio_dev->channels[0].scan_type.realbits) / 2; |
| 486 | |
| 487 | for (i = 0; i < st->chip_info->num_channels; ++i) { |
| 488 | st->pwr_down_mode[i] = AD5064_LDAC_PWRDN_1K; |
| 489 | st->dac_cache[i] = midscale; |
| 490 | } |
| 491 | |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 492 | ret = iio_device_register(indio_dev); |
| 493 | if (ret) |
| 494 | goto error_disable_reg; |
| 495 | |
| 496 | return 0; |
| 497 | |
| 498 | error_disable_reg: |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 499 | if (!st->use_internal_vref) |
| 500 | regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 501 | |
| 502 | return ret; |
| 503 | } |
| 504 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 505 | static int ad5064_remove(struct device *dev) |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 506 | { |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 507 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 508 | struct ad5064_state *st = iio_priv(indio_dev); |
| 509 | |
| 510 | iio_device_unregister(indio_dev); |
| 511 | |
Sachin Kamat | c367982 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 512 | if (!st->use_internal_vref) |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 513 | regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 518 | #if IS_ENABLED(CONFIG_SPI_MASTER) |
| 519 | |
Lars-Peter Clausen | 9660ac7 | 2012-07-11 09:01:00 +0100 | [diff] [blame] | 520 | static int ad5064_spi_write(struct ad5064_state *st, unsigned int cmd, |
| 521 | unsigned int addr, unsigned int val) |
| 522 | { |
| 523 | struct spi_device *spi = to_spi_device(st->dev); |
| 524 | |
| 525 | st->data.spi = cpu_to_be32(AD5064_CMD(cmd) | AD5064_ADDR(addr) | val); |
| 526 | return spi_write(spi, &st->data.spi, sizeof(st->data.spi)); |
| 527 | } |
| 528 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 529 | static int ad5064_spi_probe(struct spi_device *spi) |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 530 | { |
| 531 | const struct spi_device_id *id = spi_get_device_id(spi); |
| 532 | |
| 533 | return ad5064_probe(&spi->dev, id->driver_data, id->name, |
| 534 | ad5064_spi_write); |
| 535 | } |
| 536 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 537 | static int ad5064_spi_remove(struct spi_device *spi) |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 538 | { |
| 539 | return ad5064_remove(&spi->dev); |
| 540 | } |
| 541 | |
| 542 | static const struct spi_device_id ad5064_spi_ids[] = { |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 543 | {"ad5024", ID_AD5024}, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 544 | {"ad5025", ID_AD5025}, |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 545 | {"ad5044", ID_AD5044}, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 546 | {"ad5045", ID_AD5045}, |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 547 | {"ad5064", ID_AD5064}, |
| 548 | {"ad5064-1", ID_AD5064_1}, |
Lars-Peter Clausen | f8be4af | 2012-02-21 18:38:15 +0100 | [diff] [blame] | 549 | {"ad5065", ID_AD5065}, |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 550 | {"ad5628-1", ID_AD5628_1}, |
| 551 | {"ad5628-2", ID_AD5628_2}, |
| 552 | {"ad5648-1", ID_AD5648_1}, |
| 553 | {"ad5648-2", ID_AD5648_2}, |
Lars-Peter Clausen | 64f4eaa | 2012-02-21 18:38:17 +0100 | [diff] [blame] | 554 | {"ad5666-1", ID_AD5666_1}, |
| 555 | {"ad5666-2", ID_AD5666_2}, |
Lars-Peter Clausen | bb92ff3 | 2012-02-21 18:38:16 +0100 | [diff] [blame] | 556 | {"ad5668-1", ID_AD5668_1}, |
| 557 | {"ad5668-2", ID_AD5668_2}, |
| 558 | {"ad5668-3", ID_AD5668_2}, /* similar enough to ad5668-2 */ |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 559 | {} |
| 560 | }; |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 561 | MODULE_DEVICE_TABLE(spi, ad5064_spi_ids); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 562 | |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 563 | static struct spi_driver ad5064_spi_driver = { |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 564 | .driver = { |
| 565 | .name = "ad5064", |
| 566 | .owner = THIS_MODULE, |
| 567 | }, |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 568 | .probe = ad5064_spi_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 569 | .remove = ad5064_spi_remove, |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 570 | .id_table = ad5064_spi_ids, |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 571 | }; |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 572 | |
| 573 | static int __init ad5064_spi_register_driver(void) |
| 574 | { |
| 575 | return spi_register_driver(&ad5064_spi_driver); |
| 576 | } |
| 577 | |
Gerard Snitselaar | 21fa54e | 2012-07-11 05:05:00 +0100 | [diff] [blame] | 578 | static void ad5064_spi_unregister_driver(void) |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 579 | { |
| 580 | spi_unregister_driver(&ad5064_spi_driver); |
| 581 | } |
| 582 | |
| 583 | #else |
| 584 | |
| 585 | static inline int ad5064_spi_register_driver(void) { return 0; } |
| 586 | static inline void ad5064_spi_unregister_driver(void) { } |
| 587 | |
| 588 | #endif |
| 589 | |
| 590 | #if IS_ENABLED(CONFIG_I2C) |
| 591 | |
Lars-Peter Clausen | 9660ac7 | 2012-07-11 09:01:00 +0100 | [diff] [blame] | 592 | static int ad5064_i2c_write(struct ad5064_state *st, unsigned int cmd, |
| 593 | unsigned int addr, unsigned int val) |
| 594 | { |
| 595 | struct i2c_client *i2c = to_i2c_client(st->dev); |
| 596 | |
| 597 | st->data.i2c[0] = (cmd << 4) | addr; |
| 598 | put_unaligned_be16(val, &st->data.i2c[1]); |
| 599 | return i2c_master_send(i2c, st->data.i2c, 3); |
| 600 | } |
| 601 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 602 | static int ad5064_i2c_probe(struct i2c_client *i2c, |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 603 | const struct i2c_device_id *id) |
| 604 | { |
| 605 | return ad5064_probe(&i2c->dev, id->driver_data, id->name, |
| 606 | ad5064_i2c_write); |
| 607 | } |
| 608 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 609 | static int ad5064_i2c_remove(struct i2c_client *i2c) |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 610 | { |
| 611 | return ad5064_remove(&i2c->dev); |
| 612 | } |
| 613 | |
| 614 | static const struct i2c_device_id ad5064_i2c_ids[] = { |
| 615 | {"ad5629-1", ID_AD5628_1}, |
| 616 | {"ad5629-2", ID_AD5628_2}, |
| 617 | {"ad5629-3", ID_AD5628_2}, /* similar enough to ad5629-2 */ |
| 618 | {"ad5669-1", ID_AD5668_1}, |
| 619 | {"ad5669-2", ID_AD5668_2}, |
| 620 | {"ad5669-3", ID_AD5668_2}, /* similar enough to ad5669-2 */ |
| 621 | {} |
| 622 | }; |
| 623 | MODULE_DEVICE_TABLE(i2c, ad5064_i2c_ids); |
| 624 | |
| 625 | static struct i2c_driver ad5064_i2c_driver = { |
| 626 | .driver = { |
| 627 | .name = "ad5064", |
| 628 | .owner = THIS_MODULE, |
| 629 | }, |
| 630 | .probe = ad5064_i2c_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 631 | .remove = ad5064_i2c_remove, |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 632 | .id_table = ad5064_i2c_ids, |
| 633 | }; |
| 634 | |
| 635 | static int __init ad5064_i2c_register_driver(void) |
| 636 | { |
| 637 | return i2c_add_driver(&ad5064_i2c_driver); |
| 638 | } |
| 639 | |
| 640 | static void __exit ad5064_i2c_unregister_driver(void) |
| 641 | { |
| 642 | i2c_del_driver(&ad5064_i2c_driver); |
| 643 | } |
| 644 | |
| 645 | #else |
| 646 | |
| 647 | static inline int ad5064_i2c_register_driver(void) { return 0; } |
| 648 | static inline void ad5064_i2c_unregister_driver(void) { } |
| 649 | |
| 650 | #endif |
| 651 | |
| 652 | static int __init ad5064_init(void) |
| 653 | { |
| 654 | int ret; |
| 655 | |
| 656 | ret = ad5064_spi_register_driver(); |
| 657 | if (ret) |
| 658 | return ret; |
| 659 | |
| 660 | ret = ad5064_i2c_register_driver(); |
| 661 | if (ret) { |
| 662 | ad5064_spi_unregister_driver(); |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | module_init(ad5064_init); |
| 669 | |
| 670 | static void __exit ad5064_exit(void) |
| 671 | { |
| 672 | ad5064_i2c_unregister_driver(); |
| 673 | ad5064_spi_unregister_driver(); |
| 674 | } |
| 675 | module_exit(ad5064_exit); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 676 | |
| 677 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
Lars-Peter Clausen | 6a17a07 | 2012-06-26 11:04:36 +0200 | [diff] [blame] | 678 | MODULE_DESCRIPTION("Analog Devices AD5024 and similar multi-channel DACs"); |
Lars-Peter Clausen | fcf265d | 2011-10-18 09:33:03 +0200 | [diff] [blame] | 679 | MODULE_LICENSE("GPL v2"); |