Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * AD5446 SPI DAC driver |
| 3 | * |
| 4 | * Copyright 2010 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/workqueue.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/sysfs.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/spi/spi.h> |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 17 | #include <linux/i2c.h> |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 18 | #include <linux/regulator/consumer.h> |
| 19 | #include <linux/err.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 20 | #include <linux/module.h> |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 21 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 22 | #include <linux/iio/iio.h> |
| 23 | #include <linux/iio/sysfs.h> |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 24 | |
Jean-Francois Dagenais | 2e15c90 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 25 | #define MODE_PWRDWN_1k 0x1 |
| 26 | #define MODE_PWRDWN_100k 0x2 |
| 27 | #define MODE_PWRDWN_TRISTATE 0x3 |
| 28 | |
| 29 | /** |
| 30 | * struct ad5446_state - driver instance specific data |
| 31 | * @spi: spi_device |
| 32 | * @chip_info: chip model specific constants, available modes etc |
| 33 | * @reg: supply regulator |
| 34 | * @vref_mv: actual reference voltage used |
| 35 | */ |
| 36 | |
| 37 | struct ad5446_state { |
| 38 | struct device *dev; |
| 39 | const struct ad5446_chip_info *chip_info; |
| 40 | struct regulator *reg; |
| 41 | unsigned short vref_mv; |
| 42 | unsigned cached_val; |
| 43 | unsigned pwr_down_mode; |
| 44 | unsigned pwr_down; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * struct ad5446_chip_info - chip specific information |
| 49 | * @channel: channel spec for the DAC |
| 50 | * @int_vref_mv: AD5620/40/60: the internal reference voltage |
| 51 | * @write: chip specific helper function to write to the register |
| 52 | */ |
| 53 | |
| 54 | struct ad5446_chip_info { |
| 55 | struct iio_chan_spec channel; |
| 56 | u16 int_vref_mv; |
| 57 | int (*write)(struct ad5446_state *st, unsigned val); |
| 58 | }; |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 59 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 60 | static const char * const ad5446_powerdown_modes[] = { |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 61 | "1kohm_to_gnd", "100kohm_to_gnd", "three_state" |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 62 | }; |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 63 | |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 64 | static int ad5446_set_powerdown_mode(struct iio_dev *indio_dev, |
| 65 | const struct iio_chan_spec *chan, unsigned int mode) |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 66 | { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 67 | struct ad5446_state *st = iio_priv(indio_dev); |
| 68 | |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 69 | st->pwr_down_mode = mode + 1; |
| 70 | |
| 71 | return 0; |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 74 | static int ad5446_get_powerdown_mode(struct iio_dev *indio_dev, |
| 75 | const struct iio_chan_spec *chan) |
| 76 | { |
| 77 | struct ad5446_state *st = iio_priv(indio_dev); |
| 78 | |
| 79 | return st->pwr_down_mode - 1; |
| 80 | } |
| 81 | |
| 82 | static const struct iio_enum ad5446_powerdown_mode_enum = { |
| 83 | .items = ad5446_powerdown_modes, |
| 84 | .num_items = ARRAY_SIZE(ad5446_powerdown_modes), |
| 85 | .get = ad5446_get_powerdown_mode, |
| 86 | .set = ad5446_set_powerdown_mode, |
| 87 | }; |
| 88 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 89 | static ssize_t ad5446_read_dac_powerdown(struct iio_dev *indio_dev, |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 90 | uintptr_t private, |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 91 | const struct iio_chan_spec *chan, |
| 92 | char *buf) |
| 93 | { |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 94 | struct ad5446_state *st = iio_priv(indio_dev); |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 95 | |
| 96 | return sprintf(buf, "%d\n", st->pwr_down); |
| 97 | } |
| 98 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 99 | static ssize_t ad5446_write_dac_powerdown(struct iio_dev *indio_dev, |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 100 | uintptr_t private, |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 101 | const struct iio_chan_spec *chan, |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 102 | const char *buf, size_t len) |
| 103 | { |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 104 | struct ad5446_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 105 | unsigned int shift; |
| 106 | unsigned int val; |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 107 | bool powerdown; |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 108 | int ret; |
| 109 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 110 | ret = strtobool(buf, &powerdown); |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 111 | if (ret) |
| 112 | return ret; |
| 113 | |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 114 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 115 | st->pwr_down = powerdown; |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 116 | |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 117 | if (st->pwr_down) { |
| 118 | shift = chan->scan_type.realbits + chan->scan_type.shift; |
| 119 | val = st->pwr_down_mode << shift; |
| 120 | } else { |
| 121 | val = st->cached_val; |
| 122 | } |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 123 | |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 124 | ret = st->chip_info->write(st, val); |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 125 | mutex_unlock(&indio_dev->mlock); |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 126 | |
| 127 | return ret ? ret : len; |
| 128 | } |
| 129 | |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 130 | static const struct iio_chan_spec_ext_info ad5446_ext_info_powerdown[] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 131 | { |
| 132 | .name = "powerdown", |
| 133 | .read = ad5446_read_dac_powerdown, |
| 134 | .write = ad5446_write_dac_powerdown, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 135 | .shared = IIO_SEPARATE, |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 136 | }, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 137 | IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ad5446_powerdown_mode_enum), |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 138 | IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum), |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 139 | { }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 140 | }; |
| 141 | |
Jonathan Cameron | e3019c2 | 2013-12-11 18:45:00 +0000 | [diff] [blame] | 142 | #define _AD5446_CHANNEL(bits, storage, _shift, ext) { \ |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 143 | .type = IIO_VOLTAGE, \ |
| 144 | .indexed = 1, \ |
| 145 | .output = 1, \ |
| 146 | .channel = 0, \ |
Jonathan Cameron | 2f6a4a4 | 2013-02-27 19:10:35 +0000 | [diff] [blame] | 147 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 148 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
Jonathan Cameron | e3019c2 | 2013-12-11 18:45:00 +0000 | [diff] [blame] | 149 | .scan_type = { \ |
| 150 | .sign = 'u', \ |
| 151 | .realbits = (bits), \ |
| 152 | .storagebits = (storage), \ |
| 153 | .shift = (_shift), \ |
| 154 | }, \ |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 155 | .ext_info = (ext), \ |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 158 | #define AD5446_CHANNEL(bits, storage, shift) \ |
| 159 | _AD5446_CHANNEL(bits, storage, shift, NULL) |
| 160 | |
| 161 | #define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \ |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 162 | _AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown) |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 163 | |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 164 | static int ad5446_read_raw(struct iio_dev *indio_dev, |
| 165 | struct iio_chan_spec const *chan, |
| 166 | int *val, |
| 167 | int *val2, |
| 168 | long m) |
| 169 | { |
| 170 | struct ad5446_state *st = iio_priv(indio_dev); |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 171 | |
| 172 | switch (m) { |
| 173 | case IIO_CHAN_INFO_RAW: |
| 174 | *val = st->cached_val; |
| 175 | return IIO_VAL_INT; |
| 176 | case IIO_CHAN_INFO_SCALE: |
Lars-Peter Clausen | 0a99b60 | 2013-09-28 10:31:00 +0100 | [diff] [blame] | 177 | *val = st->vref_mv; |
| 178 | *val2 = chan->scan_type.realbits; |
| 179 | return IIO_VAL_FRACTIONAL_LOG2; |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 180 | } |
| 181 | return -EINVAL; |
| 182 | } |
| 183 | |
| 184 | static int ad5446_write_raw(struct iio_dev *indio_dev, |
| 185 | struct iio_chan_spec const *chan, |
| 186 | int val, |
| 187 | int val2, |
| 188 | long mask) |
| 189 | { |
| 190 | struct ad5446_state *st = iio_priv(indio_dev); |
| 191 | int ret = 0; |
| 192 | |
| 193 | switch (mask) { |
| 194 | case IIO_CHAN_INFO_RAW: |
| 195 | if (val >= (1 << chan->scan_type.realbits) || val < 0) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | val <<= chan->scan_type.shift; |
| 199 | mutex_lock(&indio_dev->mlock); |
| 200 | st->cached_val = val; |
| 201 | if (!st->pwr_down) |
| 202 | ret = st->chip_info->write(st, val); |
| 203 | mutex_unlock(&indio_dev->mlock); |
| 204 | break; |
| 205 | default: |
| 206 | ret = -EINVAL; |
| 207 | } |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static const struct iio_info ad5446_info = { |
| 213 | .read_raw = ad5446_read_raw, |
| 214 | .write_raw = ad5446_write_raw, |
| 215 | .driver_module = THIS_MODULE, |
| 216 | }; |
| 217 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 218 | static int ad5446_probe(struct device *dev, const char *name, |
| 219 | const struct ad5446_chip_info *chip_info) |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 220 | { |
| 221 | struct ad5446_state *st; |
| 222 | struct iio_dev *indio_dev; |
| 223 | struct regulator *reg; |
| 224 | int ret, voltage_uv = 0; |
| 225 | |
Sachin Kamat | ba72729 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 226 | reg = devm_regulator_get(dev, "vcc"); |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 227 | if (!IS_ERR(reg)) { |
| 228 | ret = regulator_enable(reg); |
| 229 | if (ret) |
Sachin Kamat | ba72729 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 230 | return ret; |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 231 | |
Axel Lin | 13e57ee | 2012-12-18 03:33:00 +0000 | [diff] [blame] | 232 | ret = regulator_get_voltage(reg); |
| 233 | if (ret < 0) |
| 234 | goto error_disable_reg; |
| 235 | |
| 236 | voltage_uv = ret; |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Sachin Kamat | ba72729 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 239 | indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 240 | if (indio_dev == NULL) { |
| 241 | ret = -ENOMEM; |
| 242 | goto error_disable_reg; |
| 243 | } |
| 244 | st = iio_priv(indio_dev); |
| 245 | st->chip_info = chip_info; |
| 246 | |
| 247 | dev_set_drvdata(dev, indio_dev); |
| 248 | st->reg = reg; |
| 249 | st->dev = dev; |
| 250 | |
| 251 | /* Establish that the iio_dev is a child of the device */ |
| 252 | indio_dev->dev.parent = dev; |
| 253 | indio_dev->name = name; |
| 254 | indio_dev->info = &ad5446_info; |
| 255 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 256 | indio_dev->channels = &st->chip_info->channel; |
| 257 | indio_dev->num_channels = 1; |
| 258 | |
| 259 | st->pwr_down_mode = MODE_PWRDWN_1k; |
| 260 | |
| 261 | if (st->chip_info->int_vref_mv) |
| 262 | st->vref_mv = st->chip_info->int_vref_mv; |
| 263 | else if (voltage_uv) |
| 264 | st->vref_mv = voltage_uv / 1000; |
| 265 | else |
| 266 | dev_warn(dev, "reference voltage unspecified\n"); |
| 267 | |
| 268 | ret = iio_device_register(indio_dev); |
| 269 | if (ret) |
Sachin Kamat | ba72729 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 270 | goto error_disable_reg; |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 271 | |
| 272 | return 0; |
| 273 | |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 274 | error_disable_reg: |
| 275 | if (!IS_ERR(reg)) |
| 276 | regulator_disable(reg); |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | static int ad5446_remove(struct device *dev) |
| 281 | { |
| 282 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 283 | struct ad5446_state *st = iio_priv(indio_dev); |
| 284 | |
| 285 | iio_device_unregister(indio_dev); |
Sachin Kamat | ba72729 | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 286 | if (!IS_ERR(st->reg)) |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 287 | regulator_disable(st->reg); |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | #if IS_ENABLED(CONFIG_SPI_MASTER) |
| 293 | |
| 294 | static int ad5446_write(struct ad5446_state *st, unsigned val) |
| 295 | { |
| 296 | struct spi_device *spi = to_spi_device(st->dev); |
| 297 | __be16 data = cpu_to_be16(val); |
| 298 | |
| 299 | return spi_write(spi, &data, sizeof(data)); |
| 300 | } |
| 301 | |
| 302 | static int ad5660_write(struct ad5446_state *st, unsigned val) |
| 303 | { |
| 304 | struct spi_device *spi = to_spi_device(st->dev); |
| 305 | uint8_t data[3]; |
| 306 | |
| 307 | data[0] = (val >> 16) & 0xFF; |
| 308 | data[1] = (val >> 8) & 0xFF; |
| 309 | data[2] = val & 0xFF; |
| 310 | |
| 311 | return spi_write(spi, data, sizeof(data)); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * ad5446_supported_spi_device_ids: |
| 316 | * The AD5620/40/60 parts are available in different fixed internal reference |
| 317 | * voltage options. The actual part numbers may look differently |
| 318 | * (and a bit cryptic), however this style is used to make clear which |
| 319 | * parts are supported here. |
| 320 | */ |
| 321 | enum ad5446_supported_spi_device_ids { |
Lars-Peter Clausen | 2fafbce | 2012-09-04 10:10:00 +0100 | [diff] [blame] | 322 | ID_AD5300, |
| 323 | ID_AD5310, |
| 324 | ID_AD5320, |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 325 | ID_AD5444, |
| 326 | ID_AD5446, |
| 327 | ID_AD5450, |
| 328 | ID_AD5451, |
| 329 | ID_AD5541A, |
| 330 | ID_AD5512A, |
| 331 | ID_AD5553, |
| 332 | ID_AD5601, |
| 333 | ID_AD5611, |
| 334 | ID_AD5621, |
Aida Mynzhasova | 4fa2a9e | 2013-09-23 15:19:00 +0100 | [diff] [blame] | 335 | ID_AD5641, |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 336 | ID_AD5620_2500, |
| 337 | ID_AD5620_1250, |
| 338 | ID_AD5640_2500, |
| 339 | ID_AD5640_1250, |
| 340 | ID_AD5660_2500, |
| 341 | ID_AD5660_1250, |
| 342 | ID_AD5662, |
| 343 | }; |
| 344 | |
| 345 | static const struct ad5446_chip_info ad5446_spi_chip_info[] = { |
Lars-Peter Clausen | 2fafbce | 2012-09-04 10:10:00 +0100 | [diff] [blame] | 346 | [ID_AD5300] = { |
| 347 | .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4), |
| 348 | .write = ad5446_write, |
| 349 | }, |
| 350 | [ID_AD5310] = { |
| 351 | .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2), |
| 352 | .write = ad5446_write, |
| 353 | }, |
| 354 | [ID_AD5320] = { |
| 355 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0), |
| 356 | .write = ad5446_write, |
| 357 | }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 358 | [ID_AD5444] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 359 | .channel = AD5446_CHANNEL(12, 16, 2), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 360 | .write = ad5446_write, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 361 | }, |
| 362 | [ID_AD5446] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 363 | .channel = AD5446_CHANNEL(14, 16, 0), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 364 | .write = ad5446_write, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 365 | }, |
Lars-Peter Clausen | 779c0c4 | 2012-06-26 10:45:43 +0200 | [diff] [blame] | 366 | [ID_AD5450] = { |
| 367 | .channel = AD5446_CHANNEL(8, 16, 6), |
| 368 | .write = ad5446_write, |
| 369 | }, |
| 370 | [ID_AD5451] = { |
| 371 | .channel = AD5446_CHANNEL(10, 16, 4), |
| 372 | .write = ad5446_write, |
| 373 | }, |
Michael Hennerich | 67d1c1f | 2011-04-15 13:51:07 +0200 | [diff] [blame] | 374 | [ID_AD5541A] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 375 | .channel = AD5446_CHANNEL(16, 16, 0), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 376 | .write = ad5446_write, |
Michael Hennerich | 67d1c1f | 2011-04-15 13:51:07 +0200 | [diff] [blame] | 377 | }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 378 | [ID_AD5512A] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 379 | .channel = AD5446_CHANNEL(12, 16, 4), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 380 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 381 | }, |
Michael Hennerich | 0772268 | 2011-02-23 10:45:47 +0100 | [diff] [blame] | 382 | [ID_AD5553] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 383 | .channel = AD5446_CHANNEL(14, 16, 0), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 384 | .write = ad5446_write, |
Michael Hennerich | 0772268 | 2011-02-23 10:45:47 +0100 | [diff] [blame] | 385 | }, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 386 | [ID_AD5601] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 387 | .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 388 | .write = ad5446_write, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 389 | }, |
| 390 | [ID_AD5611] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 391 | .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 392 | .write = ad5446_write, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 393 | }, |
| 394 | [ID_AD5621] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 395 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 396 | .write = ad5446_write, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 397 | }, |
Aida Mynzhasova | 4fa2a9e | 2013-09-23 15:19:00 +0100 | [diff] [blame] | 398 | [ID_AD5641] = { |
| 399 | .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0), |
| 400 | .write = ad5446_write, |
| 401 | }, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 402 | [ID_AD5620_2500] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 403 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 404 | .int_vref_mv = 2500, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 405 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 406 | }, |
| 407 | [ID_AD5620_1250] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 408 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 409 | .int_vref_mv = 1250, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 410 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 411 | }, |
| 412 | [ID_AD5640_2500] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 413 | .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 414 | .int_vref_mv = 2500, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 415 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 416 | }, |
| 417 | [ID_AD5640_1250] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 418 | .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 419 | .int_vref_mv = 1250, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 420 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 421 | }, |
| 422 | [ID_AD5660_2500] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 423 | .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 424 | .int_vref_mv = 2500, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 425 | .write = ad5660_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 426 | }, |
| 427 | [ID_AD5660_1250] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 428 | .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 429 | .int_vref_mv = 1250, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 430 | .write = ad5660_write, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 431 | }, |
Lars-Peter Clausen | 18e5ab3 | 2012-04-25 09:45:01 +0200 | [diff] [blame] | 432 | [ID_AD5662] = { |
| 433 | .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), |
| 434 | .write = ad5660_write, |
| 435 | }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 436 | }; |
| 437 | |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 438 | static const struct spi_device_id ad5446_spi_ids[] = { |
Lars-Peter Clausen | 2fafbce | 2012-09-04 10:10:00 +0100 | [diff] [blame] | 439 | {"ad5300", ID_AD5300}, |
| 440 | {"ad5310", ID_AD5310}, |
| 441 | {"ad5320", ID_AD5320}, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 442 | {"ad5444", ID_AD5444}, |
| 443 | {"ad5446", ID_AD5446}, |
Lars-Peter Clausen | 779c0c4 | 2012-06-26 10:45:43 +0200 | [diff] [blame] | 444 | {"ad5450", ID_AD5450}, |
| 445 | {"ad5451", ID_AD5451}, |
| 446 | {"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */ |
| 447 | {"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */ |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 448 | {"ad5512a", ID_AD5512A}, |
Michael Hennerich | 67d1c1f | 2011-04-15 13:51:07 +0200 | [diff] [blame] | 449 | {"ad5541a", ID_AD5541A}, |
Lars-Peter Clausen | 11a7df4 | 2012-04-25 09:44:57 +0200 | [diff] [blame] | 450 | {"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */ |
| 451 | {"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */ |
Michael Hennerich | bd51c0b | 2011-03-10 13:26:46 +0100 | [diff] [blame] | 452 | {"ad5553", ID_AD5553}, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 453 | {"ad5601", ID_AD5601}, |
| 454 | {"ad5611", ID_AD5611}, |
| 455 | {"ad5621", ID_AD5621}, |
Aida Mynzhasova | 4fa2a9e | 2013-09-23 15:19:00 +0100 | [diff] [blame] | 456 | {"ad5641", ID_AD5641}, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 457 | {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */ |
| 458 | {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */ |
| 459 | {"ad5640-2500", ID_AD5640_2500}, |
| 460 | {"ad5640-1250", ID_AD5640_1250}, |
| 461 | {"ad5660-2500", ID_AD5660_2500}, |
| 462 | {"ad5660-1250", ID_AD5660_1250}, |
Lars-Peter Clausen | 18e5ab3 | 2012-04-25 09:45:01 +0200 | [diff] [blame] | 463 | {"ad5662", ID_AD5662}, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 464 | {} |
| 465 | }; |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 466 | MODULE_DEVICE_TABLE(spi, ad5446_spi_ids); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 467 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 468 | static int ad5446_spi_probe(struct spi_device *spi) |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 469 | { |
| 470 | const struct spi_device_id *id = spi_get_device_id(spi); |
| 471 | |
| 472 | return ad5446_probe(&spi->dev, id->name, |
| 473 | &ad5446_spi_chip_info[id->driver_data]); |
| 474 | } |
| 475 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 476 | static int ad5446_spi_remove(struct spi_device *spi) |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 477 | { |
| 478 | return ad5446_remove(&spi->dev); |
| 479 | } |
| 480 | |
| 481 | static struct spi_driver ad5446_spi_driver = { |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 482 | .driver = { |
| 483 | .name = "ad5446", |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 484 | .owner = THIS_MODULE, |
| 485 | }, |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 486 | .probe = ad5446_spi_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 487 | .remove = ad5446_spi_remove, |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 488 | .id_table = ad5446_spi_ids, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 489 | }; |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 490 | |
| 491 | static int __init ad5446_spi_register_driver(void) |
| 492 | { |
| 493 | return spi_register_driver(&ad5446_spi_driver); |
| 494 | } |
| 495 | |
| 496 | static void ad5446_spi_unregister_driver(void) |
| 497 | { |
| 498 | spi_unregister_driver(&ad5446_spi_driver); |
| 499 | } |
| 500 | |
| 501 | #else |
| 502 | |
| 503 | static inline int ad5446_spi_register_driver(void) { return 0; } |
| 504 | static inline void ad5446_spi_unregister_driver(void) { } |
| 505 | |
| 506 | #endif |
| 507 | |
| 508 | #if IS_ENABLED(CONFIG_I2C) |
| 509 | |
| 510 | static int ad5622_write(struct ad5446_state *st, unsigned val) |
| 511 | { |
| 512 | struct i2c_client *client = to_i2c_client(st->dev); |
| 513 | __be16 data = cpu_to_be16(val); |
| 514 | |
| 515 | return i2c_master_send(client, (char *)&data, sizeof(data)); |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * ad5446_supported_i2c_device_ids: |
| 520 | * The AD5620/40/60 parts are available in different fixed internal reference |
| 521 | * voltage options. The actual part numbers may look differently |
| 522 | * (and a bit cryptic), however this style is used to make clear which |
| 523 | * parts are supported here. |
| 524 | */ |
| 525 | enum ad5446_supported_i2c_device_ids { |
| 526 | ID_AD5602, |
| 527 | ID_AD5612, |
| 528 | ID_AD5622, |
| 529 | }; |
| 530 | |
| 531 | static const struct ad5446_chip_info ad5446_i2c_chip_info[] = { |
| 532 | [ID_AD5602] = { |
| 533 | .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4), |
| 534 | .write = ad5622_write, |
| 535 | }, |
| 536 | [ID_AD5612] = { |
| 537 | .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2), |
| 538 | .write = ad5622_write, |
| 539 | }, |
| 540 | [ID_AD5622] = { |
| 541 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0), |
| 542 | .write = ad5622_write, |
| 543 | }, |
| 544 | }; |
| 545 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 546 | static int ad5446_i2c_probe(struct i2c_client *i2c, |
| 547 | const struct i2c_device_id *id) |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 548 | { |
| 549 | return ad5446_probe(&i2c->dev, id->name, |
| 550 | &ad5446_i2c_chip_info[id->driver_data]); |
| 551 | } |
| 552 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 553 | static int ad5446_i2c_remove(struct i2c_client *i2c) |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 554 | { |
| 555 | return ad5446_remove(&i2c->dev); |
| 556 | } |
| 557 | |
| 558 | static const struct i2c_device_id ad5446_i2c_ids[] = { |
Lars-Peter Clausen | bf83238 | 2012-09-04 10:10:00 +0100 | [diff] [blame] | 559 | {"ad5301", ID_AD5602}, |
| 560 | {"ad5311", ID_AD5612}, |
| 561 | {"ad5321", ID_AD5622}, |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 562 | {"ad5602", ID_AD5602}, |
| 563 | {"ad5612", ID_AD5612}, |
| 564 | {"ad5622", ID_AD5622}, |
| 565 | {} |
| 566 | }; |
| 567 | MODULE_DEVICE_TABLE(i2c, ad5446_i2c_ids); |
| 568 | |
| 569 | static struct i2c_driver ad5446_i2c_driver = { |
| 570 | .driver = { |
| 571 | .name = "ad5446", |
| 572 | .owner = THIS_MODULE, |
| 573 | }, |
| 574 | .probe = ad5446_i2c_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 575 | .remove = ad5446_i2c_remove, |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 576 | .id_table = ad5446_i2c_ids, |
| 577 | }; |
| 578 | |
| 579 | static int __init ad5446_i2c_register_driver(void) |
| 580 | { |
| 581 | return i2c_add_driver(&ad5446_i2c_driver); |
| 582 | } |
| 583 | |
| 584 | static void __exit ad5446_i2c_unregister_driver(void) |
| 585 | { |
| 586 | i2c_del_driver(&ad5446_i2c_driver); |
| 587 | } |
| 588 | |
| 589 | #else |
| 590 | |
| 591 | static inline int ad5446_i2c_register_driver(void) { return 0; } |
| 592 | static inline void ad5446_i2c_unregister_driver(void) { } |
| 593 | |
| 594 | #endif |
| 595 | |
| 596 | static int __init ad5446_init(void) |
| 597 | { |
| 598 | int ret; |
| 599 | |
| 600 | ret = ad5446_spi_register_driver(); |
| 601 | if (ret) |
| 602 | return ret; |
| 603 | |
| 604 | ret = ad5446_i2c_register_driver(); |
| 605 | if (ret) { |
| 606 | ad5446_spi_unregister_driver(); |
| 607 | return ret; |
| 608 | } |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | module_init(ad5446_init); |
| 613 | |
| 614 | static void __exit ad5446_exit(void) |
| 615 | { |
| 616 | ad5446_i2c_unregister_driver(); |
| 617 | ad5446_spi_unregister_driver(); |
| 618 | } |
| 619 | module_exit(ad5446_exit); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 620 | |
| 621 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 622 | MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC"); |
| 623 | MODULE_LICENSE("GPL v2"); |