blob: 7cc3e93a2aa643799d3e2c9b77d48e28d20f45c7 [file] [log] [blame]
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +02001/*
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +02002 * AD5024, AD5025, AD5044, AD5045, AD5064, AD5064-1, AD5065, AD5628, AD5629R,
Marc Andre8d144c92016-02-08 18:01:47 +01003 * AD5648, AD5666, AD5668, AD5669R, LTC2606, LTC2607, LTC2609, LTC2616,
4 * LTC2617, LTC2619, LTC2626, LTC2627, LTC2629 Digital to analog converters
5 * driver
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +02006 *
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 Clausen6a17a072012-06-26 11:04:36 +020017#include <linux/i2c.h>
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020018#include <linux/slab.h>
19#include <linux/sysfs.h>
20#include <linux/regulator/consumer.h>
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +020021#include <asm/unaligned.h>
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020022
Jonathan Cameron06458e22012-04-25 15:54:58 +010023#include <linux/iio/iio.h>
24#include <linux/iio/sysfs.h>
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020025
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +010026#define AD5064_MAX_DAC_CHANNELS 8
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +010027#define AD5064_MAX_VREFS 4
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020028
29#define AD5064_ADDR(x) ((x) << 20)
30#define AD5064_CMD(x) ((x) << 24)
31
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020032#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 Clausenbb92ff32012-02-21 18:38:16 +010042#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 Clausenfcf265d2011-10-18 09:33:03 +020046
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 Andre78f585f2016-02-08 18:01:46 +010055 * @internal_vref: internal reference voltage. 0 if the chip has no
56 internal vref.
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020057 * @channel: channel specification
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +010058 * @num_channels: number of channels
Marc Andre78f585f2016-02-08 18:01:46 +010059 * @powerdown_ltc: Use alternative power down addressing as required by
60 * ltc2617 and others.
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +010061 */
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020062
63struct ad5064_chip_info {
64 bool shared_vref;
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +010065 unsigned long internal_vref;
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +010066 const struct iio_chan_spec *channels;
67 unsigned int num_channels;
Marc Andre78f585f2016-02-08 18:01:46 +010068 bool powerdown_ltc;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020069};
70
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +020071struct ad5064_state;
72
73typedef int (*ad5064_write_func)(struct ad5064_state *st, unsigned int cmd,
74 unsigned int addr, unsigned int val);
75
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020076/**
77 * struct ad5064_state - driver instance specific data
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +020078 * @dev: the device for this driver instance
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020079 * @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 Clausenbb92ff32012-02-21 18:38:16 +010084 * @use_internal_vref: set to true if the internal reference voltage should be
85 * used.
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +020086 * @write: register write callback
87 * @data: i2c/spi transfer buffers
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020088 */
89
90struct ad5064_state {
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +020091 struct device *dev;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020092 const struct ad5064_chip_info *chip_info;
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +010093 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 Clausenbb92ff32012-02-21 18:38:16 +010097 bool use_internal_vref;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +020098
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +020099 ad5064_write_func write;
100
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200101 /*
102 * DMA (thus cache coherency maintenance) requires the
103 * transfer buffers to live in their own cache lines.
104 */
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200105 union {
106 u8 i2c[3];
107 __be32 spi;
108 } data ____cacheline_aligned;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200109};
110
111enum ad5064_type {
112 ID_AD5024,
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100113 ID_AD5025,
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200114 ID_AD5044,
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100115 ID_AD5045,
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200116 ID_AD5064,
117 ID_AD5064_1,
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100118 ID_AD5065,
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100119 ID_AD5628_1,
120 ID_AD5628_2,
Lars-Peter Clausen5dcbe972015-10-13 18:15:38 +0200121 ID_AD5629_1,
122 ID_AD5629_2,
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100123 ID_AD5648_1,
124 ID_AD5648_2,
Lars-Peter Clausen64f4eaa2012-02-21 18:38:17 +0100125 ID_AD5666_1,
126 ID_AD5666_2,
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100127 ID_AD5668_1,
128 ID_AD5668_2,
Lars-Peter Clausen5dcbe972015-10-13 18:15:38 +0200129 ID_AD5669_1,
130 ID_AD5669_2,
Marc Andre8d144c92016-02-08 18:01:47 +0100131 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 Clausenfcf265d2011-10-18 09:33:03 +0200140};
141
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200142static int ad5064_write(struct ad5064_state *st, unsigned int cmd,
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200143 unsigned int addr, unsigned int val, unsigned int shift)
144{
145 val <<= shift;
146
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200147 return st->write(st, cmd, addr, val);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200148}
149
150static int ad5064_sync_powerdown_mode(struct ad5064_state *st,
Lars-Peter Clausena2630262013-03-01 13:07:00 +0000151 const struct iio_chan_spec *chan)
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200152{
Marc Andre78f585f2016-02-08 18:01:46 +0100153 unsigned int val, address;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200154 int ret;
155
Marc Andre78f585f2016-02-08 18:01:46 +0100156 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 Clausenfcf265d2011-10-18 09:33:03 +0200162
Marc Andre78f585f2016-02-08 18:01:46 +0100163 if (st->pwr_down[chan->channel])
164 val |= st->pwr_down_mode[chan->channel] << 8;
165 }
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200166
Marc Andre78f585f2016-02-08 18:01:46 +0100167 ret = ad5064_write(st, AD5064_CMD_POWERDOWN_DAC, address, val, 0);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200168
169 return ret;
170}
171
Lars-Peter Clausen26628f62012-06-04 11:36:12 +0200172static const char * const ad5064_powerdown_modes[] = {
173 "1kohm_to_gnd",
174 "100kohm_to_gnd",
175 "three_state",
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200176};
177
Marc Andre8d144c92016-02-08 18:01:47 +0100178static const char * const ltc2617_powerdown_modes[] = {
179 "90kohm_to_gnd",
180};
181
Lars-Peter Clausen26628f62012-06-04 11:36:12 +0200182static int ad5064_get_powerdown_mode(struct iio_dev *indio_dev,
183 const struct iio_chan_spec *chan)
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100184{
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200185 struct ad5064_state *st = iio_priv(indio_dev);
186
Lars-Peter Clausen26628f62012-06-04 11:36:12 +0200187 return st->pwr_down_mode[chan->channel] - 1;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200188}
189
Lars-Peter Clausen26628f62012-06-04 11:36:12 +0200190static int ad5064_set_powerdown_mode(struct iio_dev *indio_dev,
191 const struct iio_chan_spec *chan, unsigned int mode)
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200192{
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200193 struct ad5064_state *st = iio_priv(indio_dev);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200194 int ret;
195
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200196 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen26628f62012-06-04 11:36:12 +0200197 st->pwr_down_mode[chan->channel] = mode + 1;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200198
Lars-Peter Clausena2630262013-03-01 13:07:00 +0000199 ret = ad5064_sync_powerdown_mode(st, chan);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200200 mutex_unlock(&indio_dev->mlock);
201
Lars-Peter Clausen26628f62012-06-04 11:36:12 +0200202 return ret;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200203}
204
Lars-Peter Clausen26628f62012-06-04 11:36:12 +0200205static 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 Andre8d144c92016-02-08 18:01:47 +0100212static 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 Clausen1d0d8792012-02-21 18:38:13 +0100219static ssize_t ad5064_read_dac_powerdown(struct iio_dev *indio_dev,
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200220 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200221{
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200222 struct ad5064_state *st = iio_priv(indio_dev);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200223
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100224 return sprintf(buf, "%d\n", st->pwr_down[chan->channel]);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200225}
226
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100227static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev,
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200228 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
229 size_t len)
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200230{
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200231 struct ad5064_state *st = iio_priv(indio_dev);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200232 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 Clausen1d0d8792012-02-21 18:38:13 +0100240 st->pwr_down[chan->channel] = pwr_down;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200241
Lars-Peter Clausena2630262013-03-01 13:07:00 +0000242 ret = ad5064_sync_powerdown_mode(st, chan);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200243 mutex_unlock(&indio_dev->mlock);
244 return ret ? ret : len;
245}
246
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100247static 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 Clausenfcf265d2011-10-18 09:33:03 +0200259static 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 Carpenter23a3b8c2011-10-29 10:20:42 +0300266 int scale_uv;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200267
268 switch (m) {
Jonathan Cameron09f4eb42012-04-15 17:41:19 +0100269 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200270 *val = st->dac_cache[chan->channel];
271 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100272 case IIO_CHAN_INFO_SCALE:
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100273 scale_uv = ad5064_get_vref(st, chan);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200274 if (scale_uv < 0)
275 return scale_uv;
276
Lars-Peter Clausen25682ae2013-09-28 10:31:00 +0100277 *val = scale_uv / 1000;
278 *val2 = chan->scan_type.realbits;
279 return IIO_VAL_FRACTIONAL_LOG2;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200280 default:
281 break;
282 }
283 return -EINVAL;
284}
285
286static 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 Cameron09f4eb42012-04-15 17:41:19 +0100293 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausenc5ef7172013-03-01 13:07:00 +0000294 if (val >= (1 << chan->scan_type.realbits) || val < 0)
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200295 return -EINVAL;
296
297 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200298 ret = ad5064_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N,
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200299 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
311static const struct iio_info ad5064_info = {
312 .read_raw = ad5064_read_raw,
313 .write_raw = ad5064_write_raw,
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200314 .driver_module = THIS_MODULE,
315};
316
Lars-Peter Clausen26628f62012-06-04 11:36:12 +0200317static const struct iio_chan_spec_ext_info ad5064_ext_info[] = {
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100318 {
319 .name = "powerdown",
320 .read = ad5064_read_dac_powerdown,
321 .write = ad5064_write_dac_powerdown,
Jonathan Cameron37044322013-09-08 14:57:00 +0100322 .shared = IIO_SEPARATE,
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100323 },
Jonathan Cameron37044322013-09-08 14:57:00 +0100324 IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ad5064_powerdown_mode_enum),
Lars-Peter Clausen26628f62012-06-04 11:36:12 +0200325 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5064_powerdown_mode_enum),
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100326 { },
327};
328
Marc Andre8d144c92016-02-08 18:01:47 +0100329static 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, &ltc2617_powerdown_mode_enum),
337 IIO_ENUM_AVAILABLE("powerdown_mode", &ltc2617_powerdown_mode_enum),
338 { },
339};
340
Marc Andre78f585f2016-02-08 18:01:46 +0100341#define AD5064_CHANNEL(chan, addr, bits, _shift, _ext_info) { \
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100342 .type = IIO_VOLTAGE, \
343 .indexed = 1, \
344 .output = 1, \
345 .channel = (chan), \
Jonathan Cameron20a0edd2013-02-27 19:08:37 +0000346 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
347 BIT(IIO_CHAN_INFO_SCALE), \
Lars-Peter Clausena2630262013-03-01 13:07:00 +0000348 .address = addr, \
Jonathan Cameron81d49bc2013-12-11 18:45:00 +0000349 .scan_type = { \
350 .sign = 'u', \
351 .realbits = (bits), \
352 .storagebits = 16, \
Lars-Peter Clausen5dcbe972015-10-13 18:15:38 +0200353 .shift = (_shift), \
Jonathan Cameron81d49bc2013-12-11 18:45:00 +0000354 }, \
Marc Andre78f585f2016-02-08 18:01:46 +0100355 .ext_info = (_ext_info), \
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100356}
357
Marc Andre78f585f2016-02-08 18:01:46 +0100358#define DECLARE_AD5064_CHANNELS(name, bits, shift, ext_info) \
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +0100359const struct iio_chan_spec name[] = { \
Marc Andre78f585f2016-02-08 18:01:46 +0100360 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 Clausena2630262013-03-01 13:07:00 +0000368}
369
Marc Andre78f585f2016-02-08 18:01:46 +0100370#define DECLARE_AD5065_CHANNELS(name, bits, shift, ext_info) \
Lars-Peter Clausena2630262013-03-01 13:07:00 +0000371const struct iio_chan_spec name[] = { \
Marc Andre78f585f2016-02-08 18:01:46 +0100372 AD5064_CHANNEL(0, 0, bits, shift, ext_info), \
373 AD5064_CHANNEL(1, 3, bits, shift, ext_info), \
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +0100374}
375
Marc Andre78f585f2016-02-08 18:01:46 +0100376static DECLARE_AD5064_CHANNELS(ad5024_channels, 12, 8, ad5064_ext_info);
377static DECLARE_AD5064_CHANNELS(ad5044_channels, 14, 6, ad5064_ext_info);
378static DECLARE_AD5064_CHANNELS(ad5064_channels, 16, 4, ad5064_ext_info);
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +0100379
Marc Andre78f585f2016-02-08 18:01:46 +0100380static DECLARE_AD5065_CHANNELS(ad5025_channels, 12, 8, ad5064_ext_info);
381static DECLARE_AD5065_CHANNELS(ad5045_channels, 14, 6, ad5064_ext_info);
382static DECLARE_AD5065_CHANNELS(ad5065_channels, 16, 4, ad5064_ext_info);
Lars-Peter Clausen5dcbe972015-10-13 18:15:38 +0200383
Marc Andre78f585f2016-02-08 18:01:46 +0100384static DECLARE_AD5064_CHANNELS(ad5629_channels, 12, 4, ad5064_ext_info);
385static DECLARE_AD5064_CHANNELS(ad5669_channels, 16, 0, ad5064_ext_info);
Lars-Peter Clausena2630262013-03-01 13:07:00 +0000386
Marc Andre8d144c92016-02-08 18:01:47 +0100387static DECLARE_AD5064_CHANNELS(ltc2607_channels, 16, 0, ltc2617_ext_info);
388static DECLARE_AD5064_CHANNELS(ltc2617_channels, 14, 2, ltc2617_ext_info);
389static DECLARE_AD5064_CHANNELS(ltc2627_channels, 12, 4, ltc2617_ext_info);
390
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100391static const struct ad5064_chip_info ad5064_chip_info_tbl[] = {
392 [ID_AD5024] = {
393 .shared_vref = false,
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +0100394 .channels = ad5024_channels,
395 .num_channels = 4,
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100396 },
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100397 [ID_AD5025] = {
398 .shared_vref = false,
Lars-Peter Clausena2630262013-03-01 13:07:00 +0000399 .channels = ad5025_channels,
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100400 .num_channels = 2,
401 },
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100402 [ID_AD5044] = {
403 .shared_vref = false,
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +0100404 .channels = ad5044_channels,
405 .num_channels = 4,
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100406 },
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100407 [ID_AD5045] = {
408 .shared_vref = false,
Lars-Peter Clausena2630262013-03-01 13:07:00 +0000409 .channels = ad5045_channels,
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100410 .num_channels = 2,
411 },
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100412 [ID_AD5064] = {
413 .shared_vref = false,
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +0100414 .channels = ad5064_channels,
415 .num_channels = 4,
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100416 },
417 [ID_AD5064_1] = {
418 .shared_vref = true,
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +0100419 .channels = ad5064_channels,
420 .num_channels = 4,
Lars-Peter Clausen1d0d8792012-02-21 18:38:13 +0100421 },
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100422 [ID_AD5065] = {
423 .shared_vref = false,
Lars-Peter Clausena2630262013-03-01 13:07:00 +0000424 .channels = ad5065_channels,
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100425 .num_channels = 2,
426 },
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100427 [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 Clausen5dcbe972015-10-13 18:15:38 +0200439 [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 Clausenbb92ff32012-02-21 18:38:16 +0100451 [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 Clausen64f4eaa2012-02-21 18:38:17 +0100463 [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 Clausenbb92ff32012-02-21 18:38:16 +0100475 [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 Clausen5dcbe972015-10-13 18:15:38 +0200487 [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 Andre8d144c92016-02-08 18:01:47 +0100499 [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 Clausen1d0d8792012-02-21 18:38:13 +0100562};
563
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200564static inline unsigned int ad5064_num_vref(struct ad5064_state *st)
565{
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +0100566 return st->chip_info->shared_vref ? 1 : st->chip_info->num_channels;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200567}
568
569static const char * const ad5064_vref_names[] = {
570 "vrefA",
571 "vrefB",
572 "vrefC",
573 "vrefD",
574};
575
576static 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-Hartmanfc526922012-12-21 13:21:43 -0800582static int ad5064_probe(struct device *dev, enum ad5064_type type,
583 const char *name, ad5064_write_func write)
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200584{
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200585 struct iio_dev *indio_dev;
586 struct ad5064_state *st;
Lars-Peter Clausenf77ae9d2013-03-01 13:07:00 +0000587 unsigned int midscale;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200588 unsigned int i;
589 int ret;
590
Sachin Kamatc3679822013-08-19 12:38:00 +0100591 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200592 if (indio_dev == NULL)
593 return -ENOMEM;
594
595 st = iio_priv(indio_dev);
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200596 dev_set_drvdata(dev, indio_dev);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200597
598 st->chip_info = &ad5064_chip_info_tbl[type];
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200599 st->dev = dev;
600 st->write = write;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200601
602 for (i = 0; i < ad5064_num_vref(st); ++i)
603 st->vref_reg[i].supply = ad5064_vref_name(st, i);
604
Sachin Kamatc3679822013-08-19 12:38:00 +0100605 ret = devm_regulator_bulk_get(dev, ad5064_num_vref(st),
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200606 st->vref_reg);
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100607 if (ret) {
608 if (!st->chip_info->internal_vref)
Sachin Kamatc3679822013-08-19 12:38:00 +0100609 return ret;
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100610 st->use_internal_vref = true;
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200611 ret = ad5064_write(st, AD5064_CMD_CONFIG, 0,
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100612 AD5064_CONFIG_INT_VREF_ENABLE, 0);
613 if (ret) {
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200614 dev_err(dev, "Failed to enable internal vref: %d\n",
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100615 ret);
Sachin Kamatc3679822013-08-19 12:38:00 +0100616 return ret;
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100617 }
618 } else {
619 ret = regulator_bulk_enable(ad5064_num_vref(st), st->vref_reg);
620 if (ret)
Sachin Kamatc3679822013-08-19 12:38:00 +0100621 return ret;
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100622 }
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200623
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200624 indio_dev->dev.parent = dev;
625 indio_dev->name = name;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200626 indio_dev->info = &ad5064_info;
627 indio_dev->modes = INDIO_DIRECT_MODE;
Lars-Peter Clausen83c169d2012-02-21 18:38:14 +0100628 indio_dev->channels = st->chip_info->channels;
629 indio_dev->num_channels = st->chip_info->num_channels;
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200630
Lars-Peter Clausenf77ae9d2013-03-01 13:07:00 +0000631 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 Clausenfcf265d2011-10-18 09:33:03 +0200638 ret = iio_device_register(indio_dev);
639 if (ret)
640 goto error_disable_reg;
641
642 return 0;
643
644error_disable_reg:
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100645 if (!st->use_internal_vref)
646 regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200647
648 return ret;
649}
650
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800651static int ad5064_remove(struct device *dev)
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200652{
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200653 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200654 struct ad5064_state *st = iio_priv(indio_dev);
655
656 iio_device_unregister(indio_dev);
657
Sachin Kamatc3679822013-08-19 12:38:00 +0100658 if (!st->use_internal_vref)
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100659 regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200660
661 return 0;
662}
663
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200664#if IS_ENABLED(CONFIG_SPI_MASTER)
665
Lars-Peter Clausen9660ac72012-07-11 09:01:00 +0100666static 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-Hartmanfc526922012-12-21 13:21:43 -0800675static int ad5064_spi_probe(struct spi_device *spi)
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200676{
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-Hartmanfc526922012-12-21 13:21:43 -0800683static int ad5064_spi_remove(struct spi_device *spi)
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200684{
685 return ad5064_remove(&spi->dev);
686}
687
688static const struct spi_device_id ad5064_spi_ids[] = {
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200689 {"ad5024", ID_AD5024},
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100690 {"ad5025", ID_AD5025},
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200691 {"ad5044", ID_AD5044},
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100692 {"ad5045", ID_AD5045},
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200693 {"ad5064", ID_AD5064},
694 {"ad5064-1", ID_AD5064_1},
Lars-Peter Clausenf8be4af2012-02-21 18:38:15 +0100695 {"ad5065", ID_AD5065},
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100696 {"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 Clausen64f4eaa2012-02-21 18:38:17 +0100700 {"ad5666-1", ID_AD5666_1},
701 {"ad5666-2", ID_AD5666_2},
Lars-Peter Clausenbb92ff32012-02-21 18:38:16 +0100702 {"ad5668-1", ID_AD5668_1},
703 {"ad5668-2", ID_AD5668_2},
704 {"ad5668-3", ID_AD5668_2}, /* similar enough to ad5668-2 */
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200705 {}
706};
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200707MODULE_DEVICE_TABLE(spi, ad5064_spi_ids);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200708
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200709static struct spi_driver ad5064_spi_driver = {
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200710 .driver = {
711 .name = "ad5064",
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200712 },
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200713 .probe = ad5064_spi_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800714 .remove = ad5064_spi_remove,
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200715 .id_table = ad5064_spi_ids,
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200716};
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200717
718static int __init ad5064_spi_register_driver(void)
719{
720 return spi_register_driver(&ad5064_spi_driver);
721}
722
Gerard Snitselaar21fa54e2012-07-11 05:05:00 +0100723static void ad5064_spi_unregister_driver(void)
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200724{
725 spi_unregister_driver(&ad5064_spi_driver);
726}
727
728#else
729
730static inline int ad5064_spi_register_driver(void) { return 0; }
731static inline void ad5064_spi_unregister_driver(void) { }
732
733#endif
734
735#if IS_ENABLED(CONFIG_I2C)
736
Lars-Peter Clausen9660ac72012-07-11 09:01:00 +0100737static 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 Hennerich03fe472e2015-10-13 18:15:37 +0200741 int ret;
Lars-Peter Clausen9660ac72012-07-11 09:01:00 +0100742
743 st->data.i2c[0] = (cmd << 4) | addr;
744 put_unaligned_be16(val, &st->data.i2c[1]);
Michael Hennerich03fe472e2015-10-13 18:15:37 +0200745
746 ret = i2c_master_send(i2c, st->data.i2c, 3);
747 if (ret < 0)
748 return ret;
749
750 return 0;
Lars-Peter Clausen9660ac72012-07-11 09:01:00 +0100751}
752
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800753static int ad5064_i2c_probe(struct i2c_client *i2c,
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200754 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-Hartmanfc526922012-12-21 13:21:43 -0800760static int ad5064_i2c_remove(struct i2c_client *i2c)
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200761{
762 return ad5064_remove(&i2c->dev);
763}
764
765static const struct i2c_device_id ad5064_i2c_ids[] = {
Lars-Peter Clausen5dcbe972015-10-13 18:15:38 +0200766 {"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 Andre8d144c92016-02-08 18:01:47 +0100772 {"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 Clausen6a17a072012-06-26 11:04:36 +0200781 {}
782};
783MODULE_DEVICE_TABLE(i2c, ad5064_i2c_ids);
784
785static struct i2c_driver ad5064_i2c_driver = {
786 .driver = {
787 .name = "ad5064",
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200788 },
789 .probe = ad5064_i2c_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800790 .remove = ad5064_i2c_remove,
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200791 .id_table = ad5064_i2c_ids,
792};
793
794static int __init ad5064_i2c_register_driver(void)
795{
796 return i2c_add_driver(&ad5064_i2c_driver);
797}
798
799static void __exit ad5064_i2c_unregister_driver(void)
800{
801 i2c_del_driver(&ad5064_i2c_driver);
802}
803
804#else
805
806static inline int ad5064_i2c_register_driver(void) { return 0; }
807static inline void ad5064_i2c_unregister_driver(void) { }
808
809#endif
810
811static 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}
827module_init(ad5064_init);
828
829static void __exit ad5064_exit(void)
830{
831 ad5064_i2c_unregister_driver();
832 ad5064_spi_unregister_driver();
833}
834module_exit(ad5064_exit);
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200835
836MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
Lars-Peter Clausen6a17a072012-06-26 11:04:36 +0200837MODULE_DESCRIPTION("Analog Devices AD5024 and similar multi-channel DACs");
Lars-Peter Clausenfcf265d2011-10-18 09:33:03 +0200838MODULE_LICENSE("GPL v2");