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