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> |
| 17 | #include <linux/regulator/consumer.h> |
| 18 | #include <linux/err.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 20 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 21 | #include <linux/iio/iio.h> |
| 22 | #include <linux/iio/sysfs.h> |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 23 | |
| 24 | #include "ad5446.h" |
| 25 | |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 26 | static int ad5446_write(struct ad5446_state *st, unsigned val) |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 27 | { |
Lars-Peter Clausen | af836d9 | 2012-04-25 09:44:54 +0200 | [diff] [blame] | 28 | __be16 data = cpu_to_be16(val); |
| 29 | return spi_write(st->spi, &data, sizeof(data)); |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 30 | } |
| 31 | |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 32 | static int ad5660_write(struct ad5446_state *st, unsigned val) |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 33 | { |
Lars-Peter Clausen | af836d9 | 2012-04-25 09:44:54 +0200 | [diff] [blame] | 34 | uint8_t data[3]; |
| 35 | |
Lars-Peter Clausen | af836d9 | 2012-04-25 09:44:54 +0200 | [diff] [blame] | 36 | data[0] = (val >> 16) & 0xFF; |
| 37 | data[1] = (val >> 8) & 0xFF; |
| 38 | data[2] = val & 0xFF; |
| 39 | |
| 40 | return spi_write(st->spi, data, sizeof(data)); |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 43 | static const char * const ad5446_powerdown_modes[] = { |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 44 | "1kohm_to_gnd", "100kohm_to_gnd", "three_state" |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 45 | }; |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 46 | |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 47 | static int ad5446_set_powerdown_mode(struct iio_dev *indio_dev, |
| 48 | const struct iio_chan_spec *chan, unsigned int mode) |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 49 | { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 50 | struct ad5446_state *st = iio_priv(indio_dev); |
| 51 | |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 52 | st->pwr_down_mode = mode + 1; |
| 53 | |
| 54 | return 0; |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 57 | static int ad5446_get_powerdown_mode(struct iio_dev *indio_dev, |
| 58 | const struct iio_chan_spec *chan) |
| 59 | { |
| 60 | struct ad5446_state *st = iio_priv(indio_dev); |
| 61 | |
| 62 | return st->pwr_down_mode - 1; |
| 63 | } |
| 64 | |
| 65 | static const struct iio_enum ad5446_powerdown_mode_enum = { |
| 66 | .items = ad5446_powerdown_modes, |
| 67 | .num_items = ARRAY_SIZE(ad5446_powerdown_modes), |
| 68 | .get = ad5446_get_powerdown_mode, |
| 69 | .set = ad5446_set_powerdown_mode, |
| 70 | }; |
| 71 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 72 | static ssize_t ad5446_read_dac_powerdown(struct iio_dev *indio_dev, |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 73 | uintptr_t private, |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 74 | const struct iio_chan_spec *chan, |
| 75 | char *buf) |
| 76 | { |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 77 | struct ad5446_state *st = iio_priv(indio_dev); |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 78 | |
| 79 | return sprintf(buf, "%d\n", st->pwr_down); |
| 80 | } |
| 81 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 82 | static ssize_t ad5446_write_dac_powerdown(struct iio_dev *indio_dev, |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 83 | uintptr_t private, |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 84 | const struct iio_chan_spec *chan, |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 85 | const char *buf, size_t len) |
| 86 | { |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 87 | struct ad5446_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 88 | unsigned int shift; |
| 89 | unsigned int val; |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 90 | bool powerdown; |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 91 | int ret; |
| 92 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 93 | ret = strtobool(buf, &powerdown); |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 94 | if (ret) |
| 95 | return ret; |
| 96 | |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 97 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 98 | st->pwr_down = powerdown; |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 99 | |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 100 | if (st->pwr_down) { |
| 101 | shift = chan->scan_type.realbits + chan->scan_type.shift; |
| 102 | val = st->pwr_down_mode << shift; |
| 103 | } else { |
| 104 | val = st->cached_val; |
| 105 | } |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 106 | |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 107 | ret = st->chip_info->write(st, val); |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 108 | mutex_unlock(&indio_dev->mlock); |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 109 | |
| 110 | return ret ? ret : len; |
| 111 | } |
| 112 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 113 | static const struct iio_chan_spec_ext_info ad5064_ext_info_powerdown[] = { |
| 114 | { |
| 115 | .name = "powerdown", |
| 116 | .read = ad5446_read_dac_powerdown, |
| 117 | .write = ad5446_write_dac_powerdown, |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 118 | }, |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 119 | IIO_ENUM("powerdown_mode", false, &ad5446_powerdown_mode_enum), |
| 120 | IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum), |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 121 | { }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 122 | }; |
| 123 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 124 | #define _AD5446_CHANNEL(bits, storage, shift, ext) { \ |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 125 | .type = IIO_VOLTAGE, \ |
| 126 | .indexed = 1, \ |
| 127 | .output = 1, \ |
| 128 | .channel = 0, \ |
Jonathan Cameron | 09f4eb4 | 2012-04-15 17:41:19 +0100 | [diff] [blame] | 129 | .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \ |
| 130 | IIO_CHAN_INFO_SCALE_SHARED_BIT, \ |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 131 | .scan_type = IIO_ST('u', (bits), (storage), (shift)), \ |
| 132 | .ext_info = (ext), \ |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 135 | #define AD5446_CHANNEL(bits, storage, shift) \ |
| 136 | _AD5446_CHANNEL(bits, storage, shift, NULL) |
| 137 | |
| 138 | #define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \ |
| 139 | _AD5446_CHANNEL(bits, storage, shift, ad5064_ext_info_powerdown) |
| 140 | |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 141 | static const struct ad5446_chip_info ad5446_chip_info_tbl[] = { |
| 142 | [ID_AD5444] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 143 | .channel = AD5446_CHANNEL(12, 16, 2), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 144 | .write = ad5446_write, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 145 | }, |
| 146 | [ID_AD5446] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 147 | .channel = AD5446_CHANNEL(14, 16, 0), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 148 | .write = ad5446_write, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 149 | }, |
Lars-Peter Clausen | 779c0c4 | 2012-06-26 10:45:43 +0200 | [diff] [blame] | 150 | [ID_AD5450] = { |
| 151 | .channel = AD5446_CHANNEL(8, 16, 6), |
| 152 | .write = ad5446_write, |
| 153 | }, |
| 154 | [ID_AD5451] = { |
| 155 | .channel = AD5446_CHANNEL(10, 16, 4), |
| 156 | .write = ad5446_write, |
| 157 | }, |
Michael Hennerich | 67d1c1f | 2011-04-15 13:51:07 +0200 | [diff] [blame] | 158 | [ID_AD5541A] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 159 | .channel = AD5446_CHANNEL(16, 16, 0), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 160 | .write = ad5446_write, |
Michael Hennerich | 67d1c1f | 2011-04-15 13:51:07 +0200 | [diff] [blame] | 161 | }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 162 | [ID_AD5512A] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 163 | .channel = AD5446_CHANNEL(12, 16, 4), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 164 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 165 | }, |
Michael Hennerich | 0772268 | 2011-02-23 10:45:47 +0100 | [diff] [blame] | 166 | [ID_AD5553] = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 167 | .channel = AD5446_CHANNEL(14, 16, 0), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 168 | .write = ad5446_write, |
Michael Hennerich | 0772268 | 2011-02-23 10:45:47 +0100 | [diff] [blame] | 169 | }, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 170 | [ID_AD5601] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 171 | .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 172 | .write = ad5446_write, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 173 | }, |
| 174 | [ID_AD5611] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 175 | .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 176 | .write = ad5446_write, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 177 | }, |
| 178 | [ID_AD5621] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 179 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 180 | .write = ad5446_write, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 181 | }, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 182 | [ID_AD5620_2500] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 183 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 184 | .int_vref_mv = 2500, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 185 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 186 | }, |
| 187 | [ID_AD5620_1250] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 188 | .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 189 | .int_vref_mv = 1250, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 190 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 191 | }, |
| 192 | [ID_AD5640_2500] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 193 | .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 194 | .int_vref_mv = 2500, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 195 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 196 | }, |
| 197 | [ID_AD5640_1250] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 198 | .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 199 | .int_vref_mv = 1250, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 200 | .write = ad5446_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 201 | }, |
| 202 | [ID_AD5660_2500] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 203 | .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 204 | .int_vref_mv = 2500, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 205 | .write = ad5660_write, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 206 | }, |
| 207 | [ID_AD5660_1250] = { |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 208 | .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 209 | .int_vref_mv = 1250, |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 210 | .write = ad5660_write, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 211 | }, |
Lars-Peter Clausen | 18e5ab3 | 2012-04-25 09:45:01 +0200 | [diff] [blame] | 212 | [ID_AD5662] = { |
| 213 | .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), |
| 214 | .write = ad5660_write, |
| 215 | }, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 216 | }; |
| 217 | |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 218 | static int ad5446_read_raw(struct iio_dev *indio_dev, |
| 219 | struct iio_chan_spec const *chan, |
| 220 | int *val, |
| 221 | int *val2, |
| 222 | long m) |
| 223 | { |
| 224 | struct ad5446_state *st = iio_priv(indio_dev); |
| 225 | unsigned long scale_uv; |
| 226 | |
| 227 | switch (m) { |
Lars-Peter Clausen | 5e06bdf | 2012-04-25 09:45:00 +0200 | [diff] [blame] | 228 | case IIO_CHAN_INFO_RAW: |
| 229 | *val = st->cached_val; |
| 230 | return IIO_VAL_INT; |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 231 | case IIO_CHAN_INFO_SCALE: |
| 232 | scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits; |
| 233 | *val = scale_uv / 1000; |
| 234 | *val2 = (scale_uv % 1000) * 1000; |
| 235 | return IIO_VAL_INT_PLUS_MICRO; |
| 236 | |
| 237 | } |
| 238 | return -EINVAL; |
| 239 | } |
| 240 | |
| 241 | static int ad5446_write_raw(struct iio_dev *indio_dev, |
| 242 | struct iio_chan_spec const *chan, |
| 243 | int val, |
| 244 | int val2, |
| 245 | long mask) |
| 246 | { |
| 247 | struct ad5446_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | 07ffd0d | 2012-04-25 09:44:52 +0200 | [diff] [blame] | 248 | int ret = 0; |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 249 | |
| 250 | switch (mask) { |
Jonathan Cameron | 09f4eb4 | 2012-04-15 17:41:19 +0100 | [diff] [blame] | 251 | case IIO_CHAN_INFO_RAW: |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 252 | if (val >= (1 << chan->scan_type.realbits) || val < 0) |
| 253 | return -EINVAL; |
| 254 | |
| 255 | val <<= chan->scan_type.shift; |
| 256 | mutex_lock(&indio_dev->mlock); |
| 257 | st->cached_val = val; |
Lars-Peter Clausen | af836d9 | 2012-04-25 09:44:54 +0200 | [diff] [blame] | 258 | if (!st->pwr_down) |
Lars-Peter Clausen | cae329e | 2012-04-25 09:44:59 +0200 | [diff] [blame] | 259 | ret = st->chip_info->write(st, val); |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 260 | mutex_unlock(&indio_dev->mlock); |
| 261 | break; |
| 262 | default: |
| 263 | ret = -EINVAL; |
| 264 | } |
| 265 | |
| 266 | return ret; |
| 267 | } |
| 268 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 269 | static const struct iio_info ad5446_info = { |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 270 | .read_raw = ad5446_read_raw, |
| 271 | .write_raw = ad5446_write_raw, |
Jonathan Cameron | 7389266 | 2012-01-29 11:07:02 +0000 | [diff] [blame] | 272 | .driver_module = THIS_MODULE, |
| 273 | }; |
| 274 | |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 275 | static int __devinit ad5446_probe(struct spi_device *spi) |
| 276 | { |
| 277 | struct ad5446_state *st; |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 278 | struct iio_dev *indio_dev; |
| 279 | struct regulator *reg; |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 280 | int ret, voltage_uv = 0; |
| 281 | |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 282 | reg = regulator_get(&spi->dev, "vcc"); |
| 283 | if (!IS_ERR(reg)) { |
| 284 | ret = regulator_enable(reg); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 285 | if (ret) |
| 286 | goto error_put_reg; |
| 287 | |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 288 | voltage_uv = regulator_get_voltage(reg); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 291 | indio_dev = iio_device_alloc(sizeof(*st)); |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 292 | if (indio_dev == NULL) { |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 293 | ret = -ENOMEM; |
| 294 | goto error_disable_reg; |
| 295 | } |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 296 | st = iio_priv(indio_dev); |
| 297 | st->chip_info = |
| 298 | &ad5446_chip_info_tbl[spi_get_device_id(spi)->driver_data]; |
| 299 | |
| 300 | spi_set_drvdata(spi, indio_dev); |
| 301 | st->reg = reg; |
| 302 | st->spi = spi; |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 303 | |
Justin P. Mattock | 4abf6f8 | 2012-02-29 22:00:38 -0800 | [diff] [blame] | 304 | /* Establish that the iio_dev is a child of the spi device */ |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 305 | indio_dev->dev.parent = &spi->dev; |
| 306 | indio_dev->name = spi_get_device_id(spi)->name; |
Lars-Peter Clausen | 83f0f57 | 2012-04-25 09:44:58 +0200 | [diff] [blame] | 307 | indio_dev->info = &ad5446_info; |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 308 | indio_dev->modes = INDIO_DIRECT_MODE; |
Lars-Peter Clausen | 33ad6b2 | 2011-11-15 16:31:22 +0100 | [diff] [blame] | 309 | indio_dev->channels = &st->chip_info->channel; |
| 310 | indio_dev->num_channels = 1; |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 311 | |
Lars-Peter Clausen | 09d48aa | 2012-06-04 11:36:13 +0200 | [diff] [blame] | 312 | st->pwr_down_mode = MODE_PWRDWN_1k; |
| 313 | |
Lars-Peter Clausen | 4e5d3f9 | 2012-04-25 09:44:56 +0200 | [diff] [blame] | 314 | if (st->chip_info->int_vref_mv) |
Michael Hennerich | bbed4dc | 2011-03-10 13:26:47 +0100 | [diff] [blame] | 315 | st->vref_mv = st->chip_info->int_vref_mv; |
Lars-Peter Clausen | 4e5d3f9 | 2012-04-25 09:44:56 +0200 | [diff] [blame] | 316 | else if (voltage_uv) |
| 317 | st->vref_mv = voltage_uv / 1000; |
| 318 | else |
| 319 | dev_warn(&spi->dev, "reference voltage unspecified\n"); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 320 | |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 321 | ret = iio_device_register(indio_dev); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 322 | if (ret) |
| 323 | goto error_free_device; |
| 324 | |
| 325 | return 0; |
| 326 | |
| 327 | error_free_device: |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 328 | iio_device_free(indio_dev); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 329 | error_disable_reg: |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 330 | if (!IS_ERR(reg)) |
| 331 | regulator_disable(reg); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 332 | error_put_reg: |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 333 | if (!IS_ERR(reg)) |
| 334 | regulator_put(reg); |
| 335 | |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | static int ad5446_remove(struct spi_device *spi) |
| 340 | { |
Jonathan Cameron | 86729fc | 2011-06-27 13:07:33 +0100 | [diff] [blame] | 341 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 342 | struct ad5446_state *st = iio_priv(indio_dev); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 343 | |
| 344 | iio_device_unregister(indio_dev); |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 345 | if (!IS_ERR(st->reg)) { |
| 346 | regulator_disable(st->reg); |
| 347 | regulator_put(st->reg); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 348 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 349 | iio_device_free(indio_dev); |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 350 | |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static const struct spi_device_id ad5446_id[] = { |
| 355 | {"ad5444", ID_AD5444}, |
| 356 | {"ad5446", ID_AD5446}, |
Lars-Peter Clausen | 779c0c4 | 2012-06-26 10:45:43 +0200 | [diff] [blame] | 357 | {"ad5450", ID_AD5450}, |
| 358 | {"ad5451", ID_AD5451}, |
| 359 | {"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */ |
| 360 | {"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */ |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 361 | {"ad5512a", ID_AD5512A}, |
Michael Hennerich | 67d1c1f | 2011-04-15 13:51:07 +0200 | [diff] [blame] | 362 | {"ad5541a", ID_AD5541A}, |
Lars-Peter Clausen | 11a7df4 | 2012-04-25 09:44:57 +0200 | [diff] [blame] | 363 | {"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */ |
| 364 | {"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */ |
Michael Hennerich | bd51c0b | 2011-03-10 13:26:46 +0100 | [diff] [blame] | 365 | {"ad5553", ID_AD5553}, |
Michael Hennerich | 2b61535 | 2011-03-10 13:26:48 +0100 | [diff] [blame] | 366 | {"ad5601", ID_AD5601}, |
| 367 | {"ad5611", ID_AD5611}, |
| 368 | {"ad5621", ID_AD5621}, |
Michael Hennerich | d846263 | 2010-11-23 11:14:16 +0100 | [diff] [blame] | 369 | {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */ |
| 370 | {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */ |
| 371 | {"ad5640-2500", ID_AD5640_2500}, |
| 372 | {"ad5640-1250", ID_AD5640_1250}, |
| 373 | {"ad5660-2500", ID_AD5660_2500}, |
| 374 | {"ad5660-1250", ID_AD5660_1250}, |
Lars-Peter Clausen | 18e5ab3 | 2012-04-25 09:45:01 +0200 | [diff] [blame] | 375 | {"ad5662", ID_AD5662}, |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 376 | {} |
| 377 | }; |
Lars-Peter Clausen | 55e4390 | 2011-11-16 08:53:31 +0100 | [diff] [blame] | 378 | MODULE_DEVICE_TABLE(spi, ad5446_id); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 379 | |
| 380 | static struct spi_driver ad5446_driver = { |
| 381 | .driver = { |
| 382 | .name = "ad5446", |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 383 | .owner = THIS_MODULE, |
| 384 | }, |
| 385 | .probe = ad5446_probe, |
| 386 | .remove = __devexit_p(ad5446_remove), |
| 387 | .id_table = ad5446_id, |
| 388 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 389 | module_spi_driver(ad5446_driver); |
Michael Hennerich | b5a4948 | 2010-11-22 11:15:23 +0100 | [diff] [blame] | 390 | |
| 391 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 392 | MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC"); |
| 393 | MODULE_LICENSE("GPL v2"); |