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, |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 135 | }, |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 136 | IIO_ENUM("powerdown_mode", false, &ad5446_powerdown_mode_enum), |
| 137 | IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum), |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 138 | { }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 139 | }; |
| 140 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 141 | #define _AD5446_CHANNEL(bits, storage, shift, ext) { \ |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 142 | .type = IIO_VOLTAGE, \ |
| 143 | .indexed = 1, \ |
| 144 | .output = 1, \ |
| 145 | .channel = 0, \ |
Jonathan Cameron | 09f4eb4 | 2012-04-15 17:41:19 +0100 | [diff] [blame] | 146 | .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \ |
| 147 | IIO_CHAN_INFO_SCALE_SHARED_BIT, \ |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 148 | .scan_type = IIO_ST('u', (bits), (storage), (shift)), \ |
| 149 | .ext_info = (ext), \ |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 152 | #define AD5446_CHANNEL(bits, storage, shift) \ |
| 153 | _AD5446_CHANNEL(bits, storage, shift, NULL) |
| 154 | |
| 155 | #define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \ |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 156 | _AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown) |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 157 | |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 158 | static int ad5446_read_raw(struct iio_dev *indio_dev, |
| 159 | struct iio_chan_spec const *chan, |
| 160 | int *val, |
| 161 | int *val2, |
| 162 | long m) |
| 163 | { |
| 164 | struct ad5446_state *st = iio_priv(indio_dev); |
| 165 | unsigned long scale_uv; |
| 166 | |
| 167 | switch (m) { |
| 168 | case IIO_CHAN_INFO_RAW: |
| 169 | *val = st->cached_val; |
| 170 | return IIO_VAL_INT; |
| 171 | case IIO_CHAN_INFO_SCALE: |
| 172 | scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits; |
| 173 | *val = scale_uv / 1000; |
| 174 | *val2 = (scale_uv % 1000) * 1000; |
| 175 | return IIO_VAL_INT_PLUS_MICRO; |
| 176 | |
| 177 | } |
| 178 | return -EINVAL; |
| 179 | } |
| 180 | |
| 181 | static int ad5446_write_raw(struct iio_dev *indio_dev, |
| 182 | struct iio_chan_spec const *chan, |
| 183 | int val, |
| 184 | int val2, |
| 185 | long mask) |
| 186 | { |
| 187 | struct ad5446_state *st = iio_priv(indio_dev); |
| 188 | int ret = 0; |
| 189 | |
| 190 | switch (mask) { |
| 191 | case IIO_CHAN_INFO_RAW: |
| 192 | if (val >= (1 << chan->scan_type.realbits) || val < 0) |
| 193 | return -EINVAL; |
| 194 | |
| 195 | val <<= chan->scan_type.shift; |
| 196 | mutex_lock(&indio_dev->mlock); |
| 197 | st->cached_val = val; |
| 198 | if (!st->pwr_down) |
| 199 | ret = st->chip_info->write(st, val); |
| 200 | mutex_unlock(&indio_dev->mlock); |
| 201 | break; |
| 202 | default: |
| 203 | ret = -EINVAL; |
| 204 | } |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | static const struct iio_info ad5446_info = { |
| 210 | .read_raw = ad5446_read_raw, |
| 211 | .write_raw = ad5446_write_raw, |
| 212 | .driver_module = THIS_MODULE, |
| 213 | }; |
| 214 | |
| 215 | static int __devinit ad5446_probe(struct device *dev, const char *name, |
| 216 | const struct ad5446_chip_info *chip_info) |
| 217 | { |
| 218 | struct ad5446_state *st; |
| 219 | struct iio_dev *indio_dev; |
| 220 | struct regulator *reg; |
| 221 | int ret, voltage_uv = 0; |
| 222 | |
| 223 | reg = regulator_get(dev, "vcc"); |
| 224 | if (!IS_ERR(reg)) { |
| 225 | ret = regulator_enable(reg); |
| 226 | if (ret) |
| 227 | goto error_put_reg; |
| 228 | |
| 229 | voltage_uv = regulator_get_voltage(reg); |
| 230 | } |
| 231 | |
| 232 | indio_dev = iio_device_alloc(sizeof(*st)); |
| 233 | if (indio_dev == NULL) { |
| 234 | ret = -ENOMEM; |
| 235 | goto error_disable_reg; |
| 236 | } |
| 237 | st = iio_priv(indio_dev); |
| 238 | st->chip_info = chip_info; |
| 239 | |
| 240 | dev_set_drvdata(dev, indio_dev); |
| 241 | st->reg = reg; |
| 242 | st->dev = dev; |
| 243 | |
| 244 | /* Establish that the iio_dev is a child of the device */ |
| 245 | indio_dev->dev.parent = dev; |
| 246 | indio_dev->name = name; |
| 247 | indio_dev->info = &ad5446_info; |
| 248 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 249 | indio_dev->channels = &st->chip_info->channel; |
| 250 | indio_dev->num_channels = 1; |
| 251 | |
| 252 | st->pwr_down_mode = MODE_PWRDWN_1k; |
| 253 | |
| 254 | if (st->chip_info->int_vref_mv) |
| 255 | st->vref_mv = st->chip_info->int_vref_mv; |
| 256 | else if (voltage_uv) |
| 257 | st->vref_mv = voltage_uv / 1000; |
| 258 | else |
| 259 | dev_warn(dev, "reference voltage unspecified\n"); |
| 260 | |
| 261 | ret = iio_device_register(indio_dev); |
| 262 | if (ret) |
| 263 | goto error_free_device; |
| 264 | |
| 265 | return 0; |
| 266 | |
| 267 | error_free_device: |
| 268 | iio_device_free(indio_dev); |
| 269 | error_disable_reg: |
| 270 | if (!IS_ERR(reg)) |
| 271 | regulator_disable(reg); |
| 272 | error_put_reg: |
| 273 | if (!IS_ERR(reg)) |
| 274 | regulator_put(reg); |
| 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | static int ad5446_remove(struct device *dev) |
| 280 | { |
| 281 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 282 | struct ad5446_state *st = iio_priv(indio_dev); |
| 283 | |
| 284 | iio_device_unregister(indio_dev); |
| 285 | if (!IS_ERR(st->reg)) { |
| 286 | regulator_disable(st->reg); |
| 287 | regulator_put(st->reg); |
| 288 | } |
| 289 | iio_device_free(indio_dev); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | #if IS_ENABLED(CONFIG_SPI_MASTER) |
| 295 | |
| 296 | static int ad5446_write(struct ad5446_state *st, unsigned val) |
| 297 | { |
| 298 | struct spi_device *spi = to_spi_device(st->dev); |
| 299 | __be16 data = cpu_to_be16(val); |
| 300 | |
| 301 | return spi_write(spi, &data, sizeof(data)); |
| 302 | } |
| 303 | |
| 304 | static int ad5660_write(struct ad5446_state *st, unsigned val) |
| 305 | { |
| 306 | struct spi_device *spi = to_spi_device(st->dev); |
| 307 | uint8_t data[3]; |
| 308 | |
| 309 | data[0] = (val >> 16) & 0xFF; |
| 310 | data[1] = (val >> 8) & 0xFF; |
| 311 | data[2] = val & 0xFF; |
| 312 | |
| 313 | return spi_write(spi, data, sizeof(data)); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * ad5446_supported_spi_device_ids: |
| 318 | * The AD5620/40/60 parts are available in different fixed internal reference |
| 319 | * voltage options. The actual part numbers may look differently |
| 320 | * (and a bit cryptic), however this style is used to make clear which |
| 321 | * parts are supported here. |
| 322 | */ |
| 323 | enum ad5446_supported_spi_device_ids { |
Lars-Peter Clausen | 2fafbce | 2012-09-04 10:10:00 +0100 | [diff] [blame] | 324 | ID_AD5300, |
| 325 | ID_AD5310, |
| 326 | ID_AD5320, |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 327 | ID_AD5444, |
| 328 | ID_AD5446, |
| 329 | ID_AD5450, |
| 330 | ID_AD5451, |
| 331 | ID_AD5541A, |
| 332 | ID_AD5512A, |
| 333 | ID_AD5553, |
| 334 | ID_AD5601, |
| 335 | ID_AD5611, |
| 336 | ID_AD5621, |
| 337 | ID_AD5620_2500, |
| 338 | ID_AD5620_1250, |
| 339 | ID_AD5640_2500, |
| 340 | ID_AD5640_1250, |
| 341 | ID_AD5660_2500, |
| 342 | ID_AD5660_1250, |
| 343 | ID_AD5662, |
| 344 | }; |
| 345 | |
| 346 | static const struct ad5446_chip_info ad5446_spi_chip_info[] = { |
Lars-Peter Clausen | 2fafbce | 2012-09-04 10:10:00 +0100 | [diff] [blame] | 347 | [ID_AD5300] = { |
| 348 | .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4), |
| 349 | .write = ad5446_write, |
| 350 | }, |
| 351 | [ID_AD5310] = { |
| 352 | .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2), |
| 353 | .write = ad5446_write, |
| 354 | }, |
| 355 | [ID_AD5320] = { |
| 356 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0), |
| 357 | .write = ad5446_write, |
| 358 | }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 359 | [ID_AD5444] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 360 | .channel = AD5446_CHANNEL(12, 16, 2), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 361 | .write = ad5446_write, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 362 | }, |
| 363 | [ID_AD5446] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 364 | .channel = AD5446_CHANNEL(14, 16, 0), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 365 | .write = ad5446_write, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 366 | }, |
Lars-Peter Clausen | 779c0c4 | 2012-06-26 10:45:43 +0200 | [diff] [blame] | 367 | [ID_AD5450] = { |
| 368 | .channel = AD5446_CHANNEL(8, 16, 6), |
| 369 | .write = ad5446_write, |
| 370 | }, |
| 371 | [ID_AD5451] = { |
| 372 | .channel = AD5446_CHANNEL(10, 16, 4), |
| 373 | .write = ad5446_write, |
| 374 | }, |
Michael Hennerich | 67d1c1f | 2011-04-15 13:51:07 +0200 | [diff] [blame] | 375 | [ID_AD5541A] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 376 | .channel = AD5446_CHANNEL(16, 16, 0), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 377 | .write = ad5446_write, |
Michael Hennerich | 67d1c1f | 2011-04-15 13:51:07 +0200 | [diff] [blame] | 378 | }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 379 | [ID_AD5512A] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 380 | .channel = AD5446_CHANNEL(12, 16, 4), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 381 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 382 | }, |
Michael Hennerich | 0772268 | 2011-02-23 10:45:47 +0100 | [diff] [blame] | 383 | [ID_AD5553] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 384 | .channel = AD5446_CHANNEL(14, 16, 0), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 385 | .write = ad5446_write, |
Michael Hennerich | 0772268 | 2011-02-23 10:45:47 +0100 | [diff] [blame] | 386 | }, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 387 | [ID_AD5601] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 388 | .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 389 | .write = ad5446_write, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 390 | }, |
| 391 | [ID_AD5611] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 392 | .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 393 | .write = ad5446_write, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 394 | }, |
| 395 | [ID_AD5621] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 396 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 397 | .write = ad5446_write, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 398 | }, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 399 | [ID_AD5620_2500] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 400 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 401 | .int_vref_mv = 2500, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 402 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 403 | }, |
| 404 | [ID_AD5620_1250] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 405 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 406 | .int_vref_mv = 1250, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 407 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 408 | }, |
| 409 | [ID_AD5640_2500] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 410 | .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 411 | .int_vref_mv = 2500, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 412 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 413 | }, |
| 414 | [ID_AD5640_1250] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 415 | .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 416 | .int_vref_mv = 1250, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 417 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 418 | }, |
| 419 | [ID_AD5660_2500] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 420 | .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 421 | .int_vref_mv = 2500, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 422 | .write = ad5660_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 423 | }, |
| 424 | [ID_AD5660_1250] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 425 | .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 426 | .int_vref_mv = 1250, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 427 | .write = ad5660_write, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 428 | }, |
Lars-Peter Clausen | 18e5ab3 | 2012-04-25 09:45:01 +0200 | [diff] [blame] | 429 | [ID_AD5662] = { |
| 430 | .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), |
| 431 | .write = ad5660_write, |
| 432 | }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 433 | }; |
| 434 | |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 435 | static const struct spi_device_id ad5446_spi_ids[] = { |
Lars-Peter Clausen | 2fafbce | 2012-09-04 10:10:00 +0100 | [diff] [blame] | 436 | {"ad5300", ID_AD5300}, |
| 437 | {"ad5310", ID_AD5310}, |
| 438 | {"ad5320", ID_AD5320}, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 439 | {"ad5444", ID_AD5444}, |
| 440 | {"ad5446", ID_AD5446}, |
Lars-Peter Clausen | 779c0c4 | 2012-06-26 10:45:43 +0200 | [diff] [blame] | 441 | {"ad5450", ID_AD5450}, |
| 442 | {"ad5451", ID_AD5451}, |
| 443 | {"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */ |
| 444 | {"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */ |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 445 | {"ad5512a", ID_AD5512A}, |
Michael Hennerich | 67d1c1f | 2011-04-15 13:51:07 +0200 | [diff] [blame] | 446 | {"ad5541a", ID_AD5541A}, |
Lars-Peter Clausen | 11a7df4 | 2012-04-25 09:44:57 +0200 | [diff] [blame] | 447 | {"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */ |
| 448 | {"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */ |
Michael Hennerich | bd51c0b | 2011-03-10 13:26:46 +0100 | [diff] [blame] | 449 | {"ad5553", ID_AD5553}, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 450 | {"ad5601", ID_AD5601}, |
| 451 | {"ad5611", ID_AD5611}, |
| 452 | {"ad5621", ID_AD5621}, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 453 | {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */ |
| 454 | {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */ |
| 455 | {"ad5640-2500", ID_AD5640_2500}, |
| 456 | {"ad5640-1250", ID_AD5640_1250}, |
| 457 | {"ad5660-2500", ID_AD5660_2500}, |
| 458 | {"ad5660-1250", ID_AD5660_1250}, |
Lars-Peter Clausen | 18e5ab3 | 2012-04-25 09:45:01 +0200 | [diff] [blame] | 459 | {"ad5662", ID_AD5662}, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 460 | {} |
| 461 | }; |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 462 | MODULE_DEVICE_TABLE(spi, ad5446_spi_ids); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 463 | |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 464 | static int __devinit ad5446_spi_probe(struct spi_device *spi) |
| 465 | { |
| 466 | const struct spi_device_id *id = spi_get_device_id(spi); |
| 467 | |
| 468 | return ad5446_probe(&spi->dev, id->name, |
| 469 | &ad5446_spi_chip_info[id->driver_data]); |
| 470 | } |
| 471 | |
| 472 | static int __devexit ad5446_spi_remove(struct spi_device *spi) |
| 473 | { |
| 474 | return ad5446_remove(&spi->dev); |
| 475 | } |
| 476 | |
| 477 | static struct spi_driver ad5446_spi_driver = { |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 478 | .driver = { |
| 479 | .name = "ad5446", |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 480 | .owner = THIS_MODULE, |
| 481 | }, |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 482 | .probe = ad5446_spi_probe, |
| 483 | .remove = __devexit_p(ad5446_spi_remove), |
| 484 | .id_table = ad5446_spi_ids, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 485 | }; |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 486 | |
| 487 | static int __init ad5446_spi_register_driver(void) |
| 488 | { |
| 489 | return spi_register_driver(&ad5446_spi_driver); |
| 490 | } |
| 491 | |
| 492 | static void ad5446_spi_unregister_driver(void) |
| 493 | { |
| 494 | spi_unregister_driver(&ad5446_spi_driver); |
| 495 | } |
| 496 | |
| 497 | #else |
| 498 | |
| 499 | static inline int ad5446_spi_register_driver(void) { return 0; } |
| 500 | static inline void ad5446_spi_unregister_driver(void) { } |
| 501 | |
| 502 | #endif |
| 503 | |
| 504 | #if IS_ENABLED(CONFIG_I2C) |
| 505 | |
| 506 | static int ad5622_write(struct ad5446_state *st, unsigned val) |
| 507 | { |
| 508 | struct i2c_client *client = to_i2c_client(st->dev); |
| 509 | __be16 data = cpu_to_be16(val); |
| 510 | |
| 511 | return i2c_master_send(client, (char *)&data, sizeof(data)); |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * ad5446_supported_i2c_device_ids: |
| 516 | * The AD5620/40/60 parts are available in different fixed internal reference |
| 517 | * voltage options. The actual part numbers may look differently |
| 518 | * (and a bit cryptic), however this style is used to make clear which |
| 519 | * parts are supported here. |
| 520 | */ |
| 521 | enum ad5446_supported_i2c_device_ids { |
| 522 | ID_AD5602, |
| 523 | ID_AD5612, |
| 524 | ID_AD5622, |
| 525 | }; |
| 526 | |
| 527 | static const struct ad5446_chip_info ad5446_i2c_chip_info[] = { |
| 528 | [ID_AD5602] = { |
| 529 | .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4), |
| 530 | .write = ad5622_write, |
| 531 | }, |
| 532 | [ID_AD5612] = { |
| 533 | .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2), |
| 534 | .write = ad5622_write, |
| 535 | }, |
| 536 | [ID_AD5622] = { |
| 537 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0), |
| 538 | .write = ad5622_write, |
| 539 | }, |
| 540 | }; |
| 541 | |
| 542 | static int __devinit ad5446_i2c_probe(struct i2c_client *i2c, |
| 543 | const struct i2c_device_id *id) |
| 544 | { |
| 545 | return ad5446_probe(&i2c->dev, id->name, |
| 546 | &ad5446_i2c_chip_info[id->driver_data]); |
| 547 | } |
| 548 | |
| 549 | static int __devexit ad5446_i2c_remove(struct i2c_client *i2c) |
| 550 | { |
| 551 | return ad5446_remove(&i2c->dev); |
| 552 | } |
| 553 | |
| 554 | static const struct i2c_device_id ad5446_i2c_ids[] = { |
Lars-Peter Clausen | bf83238 | 2012-09-04 10:10:00 +0100 | [diff] [blame^] | 555 | {"ad5301", ID_AD5602}, |
| 556 | {"ad5311", ID_AD5612}, |
| 557 | {"ad5321", ID_AD5622}, |
Jean-Francois Dagenais | 3ec36a2 | 2012-08-21 15:28:00 +0100 | [diff] [blame] | 558 | {"ad5602", ID_AD5602}, |
| 559 | {"ad5612", ID_AD5612}, |
| 560 | {"ad5622", ID_AD5622}, |
| 561 | {} |
| 562 | }; |
| 563 | MODULE_DEVICE_TABLE(i2c, ad5446_i2c_ids); |
| 564 | |
| 565 | static struct i2c_driver ad5446_i2c_driver = { |
| 566 | .driver = { |
| 567 | .name = "ad5446", |
| 568 | .owner = THIS_MODULE, |
| 569 | }, |
| 570 | .probe = ad5446_i2c_probe, |
| 571 | .remove = __devexit_p(ad5446_i2c_remove), |
| 572 | .id_table = ad5446_i2c_ids, |
| 573 | }; |
| 574 | |
| 575 | static int __init ad5446_i2c_register_driver(void) |
| 576 | { |
| 577 | return i2c_add_driver(&ad5446_i2c_driver); |
| 578 | } |
| 579 | |
| 580 | static void __exit ad5446_i2c_unregister_driver(void) |
| 581 | { |
| 582 | i2c_del_driver(&ad5446_i2c_driver); |
| 583 | } |
| 584 | |
| 585 | #else |
| 586 | |
| 587 | static inline int ad5446_i2c_register_driver(void) { return 0; } |
| 588 | static inline void ad5446_i2c_unregister_driver(void) { } |
| 589 | |
| 590 | #endif |
| 591 | |
| 592 | static int __init ad5446_init(void) |
| 593 | { |
| 594 | int ret; |
| 595 | |
| 596 | ret = ad5446_spi_register_driver(); |
| 597 | if (ret) |
| 598 | return ret; |
| 599 | |
| 600 | ret = ad5446_i2c_register_driver(); |
| 601 | if (ret) { |
| 602 | ad5446_spi_unregister_driver(); |
| 603 | return ret; |
| 604 | } |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | module_init(ad5446_init); |
| 609 | |
| 610 | static void __exit ad5446_exit(void) |
| 611 | { |
| 612 | ad5446_i2c_unregister_driver(); |
| 613 | ad5446_spi_unregister_driver(); |
| 614 | } |
| 615 | module_exit(ad5446_exit); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 616 | |
| 617 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 618 | MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC"); |
| 619 | MODULE_LICENSE("GPL v2"); |