1fda8d26eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dbdc025bSLars-Peter Clausen /*
3dbdc025bSLars-Peter Clausen * Analog devices AD5360, AD5361, AD5362, AD5363, AD5370, AD5371, AD5373
4dbdc025bSLars-Peter Clausen * multi-channel Digital to Analog Converters driver
5dbdc025bSLars-Peter Clausen *
6dbdc025bSLars-Peter Clausen * Copyright 2011 Analog Devices Inc.
7dbdc025bSLars-Peter Clausen */
8dbdc025bSLars-Peter Clausen
9dbdc025bSLars-Peter Clausen #include <linux/device.h>
10dbdc025bSLars-Peter Clausen #include <linux/err.h>
11dbdc025bSLars-Peter Clausen #include <linux/module.h>
12dbdc025bSLars-Peter Clausen #include <linux/kernel.h>
13dbdc025bSLars-Peter Clausen #include <linux/spi/spi.h>
14dbdc025bSLars-Peter Clausen #include <linux/slab.h>
15dbdc025bSLars-Peter Clausen #include <linux/sysfs.h>
16dbdc025bSLars-Peter Clausen #include <linux/regulator/consumer.h>
17dbdc025bSLars-Peter Clausen
18dbdc025bSLars-Peter Clausen #include <linux/iio/iio.h>
19dbdc025bSLars-Peter Clausen #include <linux/iio/sysfs.h>
20dbdc025bSLars-Peter Clausen
21dbdc025bSLars-Peter Clausen #define AD5360_CMD(x) ((x) << 22)
22dbdc025bSLars-Peter Clausen #define AD5360_ADDR(x) ((x) << 16)
23dbdc025bSLars-Peter Clausen
24dbdc025bSLars-Peter Clausen #define AD5360_READBACK_TYPE(x) ((x) << 13)
25dbdc025bSLars-Peter Clausen #define AD5360_READBACK_ADDR(x) ((x) << 7)
26dbdc025bSLars-Peter Clausen
27dbdc025bSLars-Peter Clausen #define AD5360_CHAN_ADDR(chan) ((chan) + 0x8)
28dbdc025bSLars-Peter Clausen
29dbdc025bSLars-Peter Clausen #define AD5360_CMD_WRITE_DATA 0x3
30dbdc025bSLars-Peter Clausen #define AD5360_CMD_WRITE_OFFSET 0x2
31dbdc025bSLars-Peter Clausen #define AD5360_CMD_WRITE_GAIN 0x1
32dbdc025bSLars-Peter Clausen #define AD5360_CMD_SPECIAL_FUNCTION 0x0
33dbdc025bSLars-Peter Clausen
34dbdc025bSLars-Peter Clausen /* Special function register addresses */
35dbdc025bSLars-Peter Clausen #define AD5360_REG_SF_NOP 0x0
36dbdc025bSLars-Peter Clausen #define AD5360_REG_SF_CTRL 0x1
37dbdc025bSLars-Peter Clausen #define AD5360_REG_SF_OFS(x) (0x2 + (x))
38dbdc025bSLars-Peter Clausen #define AD5360_REG_SF_READBACK 0x5
39dbdc025bSLars-Peter Clausen
40dbdc025bSLars-Peter Clausen #define AD5360_SF_CTRL_PWR_DOWN BIT(0)
41dbdc025bSLars-Peter Clausen
42dbdc025bSLars-Peter Clausen #define AD5360_READBACK_X1A 0x0
43dbdc025bSLars-Peter Clausen #define AD5360_READBACK_X1B 0x1
44dbdc025bSLars-Peter Clausen #define AD5360_READBACK_OFFSET 0x2
45dbdc025bSLars-Peter Clausen #define AD5360_READBACK_GAIN 0x3
46dbdc025bSLars-Peter Clausen #define AD5360_READBACK_SF 0x4
47dbdc025bSLars-Peter Clausen
48dbdc025bSLars-Peter Clausen
49dbdc025bSLars-Peter Clausen /**
50dbdc025bSLars-Peter Clausen * struct ad5360_chip_info - chip specific information
51dbdc025bSLars-Peter Clausen * @channel_template: channel specification template
52dbdc025bSLars-Peter Clausen * @num_channels: number of channels
53dbdc025bSLars-Peter Clausen * @channels_per_group: number of channels per group
54dbdc025bSLars-Peter Clausen * @num_vrefs: number of vref supplies for the chip
55dbdc025bSLars-Peter Clausen */
56dbdc025bSLars-Peter Clausen
57dbdc025bSLars-Peter Clausen struct ad5360_chip_info {
58dbdc025bSLars-Peter Clausen struct iio_chan_spec channel_template;
59dbdc025bSLars-Peter Clausen unsigned int num_channels;
60dbdc025bSLars-Peter Clausen unsigned int channels_per_group;
61dbdc025bSLars-Peter Clausen unsigned int num_vrefs;
62dbdc025bSLars-Peter Clausen };
63dbdc025bSLars-Peter Clausen
64dbdc025bSLars-Peter Clausen /**
65dbdc025bSLars-Peter Clausen * struct ad5360_state - driver instance specific data
66dbdc025bSLars-Peter Clausen * @spi: spi_device
67dbdc025bSLars-Peter Clausen * @chip_info: chip model specific constants, available modes etc
68dbdc025bSLars-Peter Clausen * @vref_reg: vref supply regulators
69dbdc025bSLars-Peter Clausen * @ctrl: control register cache
70a73051fcSLee Jones * @lock: lock to protect the data buffer during SPI ops
71dbdc025bSLars-Peter Clausen * @data: spi transfer buffers
72dbdc025bSLars-Peter Clausen */
73dbdc025bSLars-Peter Clausen
74dbdc025bSLars-Peter Clausen struct ad5360_state {
75dbdc025bSLars-Peter Clausen struct spi_device *spi;
76dbdc025bSLars-Peter Clausen const struct ad5360_chip_info *chip_info;
77dbdc025bSLars-Peter Clausen struct regulator_bulk_data vref_reg[3];
78dbdc025bSLars-Peter Clausen unsigned int ctrl;
792bbf7c6bSSergiu Cuciurean struct mutex lock;
80dbdc025bSLars-Peter Clausen
81dbdc025bSLars-Peter Clausen /*
82*94ec314eSJonathan Cameron * DMA (thus cache coherency maintenance) may require the
83dbdc025bSLars-Peter Clausen * transfer buffers to live in their own cache lines.
84dbdc025bSLars-Peter Clausen */
85dbdc025bSLars-Peter Clausen union {
86dbdc025bSLars-Peter Clausen __be32 d32;
87dbdc025bSLars-Peter Clausen u8 d8[4];
88*94ec314eSJonathan Cameron } data[2] __aligned(IIO_DMA_MINALIGN);
89dbdc025bSLars-Peter Clausen };
90dbdc025bSLars-Peter Clausen
91dbdc025bSLars-Peter Clausen enum ad5360_type {
92dbdc025bSLars-Peter Clausen ID_AD5360,
93dbdc025bSLars-Peter Clausen ID_AD5361,
94dbdc025bSLars-Peter Clausen ID_AD5362,
95dbdc025bSLars-Peter Clausen ID_AD5363,
96dbdc025bSLars-Peter Clausen ID_AD5370,
97dbdc025bSLars-Peter Clausen ID_AD5371,
98dbdc025bSLars-Peter Clausen ID_AD5372,
99dbdc025bSLars-Peter Clausen ID_AD5373,
100dbdc025bSLars-Peter Clausen };
101dbdc025bSLars-Peter Clausen
102dbdc025bSLars-Peter Clausen #define AD5360_CHANNEL(bits) { \
103dbdc025bSLars-Peter Clausen .type = IIO_VOLTAGE, \
104dbdc025bSLars-Peter Clausen .indexed = 1, \
105dbdc025bSLars-Peter Clausen .output = 1, \
1061727a301SJonathan Cameron .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1071727a301SJonathan Cameron BIT(IIO_CHAN_INFO_SCALE) | \
1081727a301SJonathan Cameron BIT(IIO_CHAN_INFO_OFFSET) | \
1091727a301SJonathan Cameron BIT(IIO_CHAN_INFO_CALIBSCALE) | \
1101727a301SJonathan Cameron BIT(IIO_CHAN_INFO_CALIBBIAS), \
111c865b537SJonathan Cameron .scan_type = { \
112c865b537SJonathan Cameron .sign = 'u', \
113c865b537SJonathan Cameron .realbits = (bits), \
114c865b537SJonathan Cameron .storagebits = 16, \
115c865b537SJonathan Cameron .shift = 16 - (bits), \
116c865b537SJonathan Cameron }, \
117dbdc025bSLars-Peter Clausen }
118dbdc025bSLars-Peter Clausen
119dbdc025bSLars-Peter Clausen static const struct ad5360_chip_info ad5360_chip_info_tbl[] = {
120dbdc025bSLars-Peter Clausen [ID_AD5360] = {
121dbdc025bSLars-Peter Clausen .channel_template = AD5360_CHANNEL(16),
122dbdc025bSLars-Peter Clausen .num_channels = 16,
123dbdc025bSLars-Peter Clausen .channels_per_group = 8,
124dbdc025bSLars-Peter Clausen .num_vrefs = 2,
125dbdc025bSLars-Peter Clausen },
126dbdc025bSLars-Peter Clausen [ID_AD5361] = {
127dbdc025bSLars-Peter Clausen .channel_template = AD5360_CHANNEL(14),
128dbdc025bSLars-Peter Clausen .num_channels = 16,
129dbdc025bSLars-Peter Clausen .channels_per_group = 8,
130dbdc025bSLars-Peter Clausen .num_vrefs = 2,
131dbdc025bSLars-Peter Clausen },
132dbdc025bSLars-Peter Clausen [ID_AD5362] = {
133dbdc025bSLars-Peter Clausen .channel_template = AD5360_CHANNEL(16),
134dbdc025bSLars-Peter Clausen .num_channels = 8,
135dbdc025bSLars-Peter Clausen .channels_per_group = 4,
136dbdc025bSLars-Peter Clausen .num_vrefs = 2,
137dbdc025bSLars-Peter Clausen },
138dbdc025bSLars-Peter Clausen [ID_AD5363] = {
139dbdc025bSLars-Peter Clausen .channel_template = AD5360_CHANNEL(14),
140dbdc025bSLars-Peter Clausen .num_channels = 8,
141dbdc025bSLars-Peter Clausen .channels_per_group = 4,
142dbdc025bSLars-Peter Clausen .num_vrefs = 2,
143dbdc025bSLars-Peter Clausen },
144dbdc025bSLars-Peter Clausen [ID_AD5370] = {
145dbdc025bSLars-Peter Clausen .channel_template = AD5360_CHANNEL(16),
146dbdc025bSLars-Peter Clausen .num_channels = 40,
147dbdc025bSLars-Peter Clausen .channels_per_group = 8,
148dbdc025bSLars-Peter Clausen .num_vrefs = 2,
149dbdc025bSLars-Peter Clausen },
150dbdc025bSLars-Peter Clausen [ID_AD5371] = {
151dbdc025bSLars-Peter Clausen .channel_template = AD5360_CHANNEL(14),
152dbdc025bSLars-Peter Clausen .num_channels = 40,
153dbdc025bSLars-Peter Clausen .channels_per_group = 8,
154dbdc025bSLars-Peter Clausen .num_vrefs = 3,
155dbdc025bSLars-Peter Clausen },
156dbdc025bSLars-Peter Clausen [ID_AD5372] = {
157dbdc025bSLars-Peter Clausen .channel_template = AD5360_CHANNEL(16),
158dbdc025bSLars-Peter Clausen .num_channels = 32,
159dbdc025bSLars-Peter Clausen .channels_per_group = 8,
160dbdc025bSLars-Peter Clausen .num_vrefs = 2,
161dbdc025bSLars-Peter Clausen },
162dbdc025bSLars-Peter Clausen [ID_AD5373] = {
163dbdc025bSLars-Peter Clausen .channel_template = AD5360_CHANNEL(14),
164dbdc025bSLars-Peter Clausen .num_channels = 32,
165dbdc025bSLars-Peter Clausen .channels_per_group = 8,
166dbdc025bSLars-Peter Clausen .num_vrefs = 2,
167dbdc025bSLars-Peter Clausen },
168dbdc025bSLars-Peter Clausen };
169dbdc025bSLars-Peter Clausen
ad5360_get_channel_vref_index(struct ad5360_state * st,unsigned int channel)170dbdc025bSLars-Peter Clausen static unsigned int ad5360_get_channel_vref_index(struct ad5360_state *st,
171dbdc025bSLars-Peter Clausen unsigned int channel)
172dbdc025bSLars-Peter Clausen {
173dbdc025bSLars-Peter Clausen unsigned int i;
174dbdc025bSLars-Peter Clausen
175dbdc025bSLars-Peter Clausen /* The first groups have their own vref, while the remaining groups
176dbdc025bSLars-Peter Clausen * share the last vref */
177dbdc025bSLars-Peter Clausen i = channel / st->chip_info->channels_per_group;
178dbdc025bSLars-Peter Clausen if (i >= st->chip_info->num_vrefs)
179dbdc025bSLars-Peter Clausen i = st->chip_info->num_vrefs - 1;
180dbdc025bSLars-Peter Clausen
181dbdc025bSLars-Peter Clausen return i;
182dbdc025bSLars-Peter Clausen }
183dbdc025bSLars-Peter Clausen
ad5360_get_channel_vref(struct ad5360_state * st,unsigned int channel)184dbdc025bSLars-Peter Clausen static int ad5360_get_channel_vref(struct ad5360_state *st,
185dbdc025bSLars-Peter Clausen unsigned int channel)
186dbdc025bSLars-Peter Clausen {
187dbdc025bSLars-Peter Clausen unsigned int i = ad5360_get_channel_vref_index(st, channel);
188dbdc025bSLars-Peter Clausen
189dbdc025bSLars-Peter Clausen return regulator_get_voltage(st->vref_reg[i].consumer);
190dbdc025bSLars-Peter Clausen }
191dbdc025bSLars-Peter Clausen
192dbdc025bSLars-Peter Clausen
ad5360_write_unlocked(struct iio_dev * indio_dev,unsigned int cmd,unsigned int addr,unsigned int val,unsigned int shift)193dbdc025bSLars-Peter Clausen static int ad5360_write_unlocked(struct iio_dev *indio_dev,
194dbdc025bSLars-Peter Clausen unsigned int cmd, unsigned int addr, unsigned int val,
195dbdc025bSLars-Peter Clausen unsigned int shift)
196dbdc025bSLars-Peter Clausen {
197dbdc025bSLars-Peter Clausen struct ad5360_state *st = iio_priv(indio_dev);
198dbdc025bSLars-Peter Clausen
199dbdc025bSLars-Peter Clausen val <<= shift;
200dbdc025bSLars-Peter Clausen val |= AD5360_CMD(cmd) | AD5360_ADDR(addr);
201dbdc025bSLars-Peter Clausen st->data[0].d32 = cpu_to_be32(val);
202dbdc025bSLars-Peter Clausen
203dbdc025bSLars-Peter Clausen return spi_write(st->spi, &st->data[0].d8[1], 3);
204dbdc025bSLars-Peter Clausen }
205dbdc025bSLars-Peter Clausen
ad5360_write(struct iio_dev * indio_dev,unsigned int cmd,unsigned int addr,unsigned int val,unsigned int shift)206dbdc025bSLars-Peter Clausen static int ad5360_write(struct iio_dev *indio_dev, unsigned int cmd,
207dbdc025bSLars-Peter Clausen unsigned int addr, unsigned int val, unsigned int shift)
208dbdc025bSLars-Peter Clausen {
209dbdc025bSLars-Peter Clausen int ret;
2102bbf7c6bSSergiu Cuciurean struct ad5360_state *st = iio_priv(indio_dev);
211dbdc025bSLars-Peter Clausen
2122bbf7c6bSSergiu Cuciurean mutex_lock(&st->lock);
213dbdc025bSLars-Peter Clausen ret = ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
2142bbf7c6bSSergiu Cuciurean mutex_unlock(&st->lock);
215dbdc025bSLars-Peter Clausen
216dbdc025bSLars-Peter Clausen return ret;
217dbdc025bSLars-Peter Clausen }
218dbdc025bSLars-Peter Clausen
ad5360_read(struct iio_dev * indio_dev,unsigned int type,unsigned int addr)219dbdc025bSLars-Peter Clausen static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
220dbdc025bSLars-Peter Clausen unsigned int addr)
221dbdc025bSLars-Peter Clausen {
222dbdc025bSLars-Peter Clausen struct ad5360_state *st = iio_priv(indio_dev);
223dbdc025bSLars-Peter Clausen int ret;
224dbdc025bSLars-Peter Clausen struct spi_transfer t[] = {
225dbdc025bSLars-Peter Clausen {
226dbdc025bSLars-Peter Clausen .tx_buf = &st->data[0].d8[1],
227dbdc025bSLars-Peter Clausen .len = 3,
228dbdc025bSLars-Peter Clausen .cs_change = 1,
229dbdc025bSLars-Peter Clausen }, {
230dbdc025bSLars-Peter Clausen .rx_buf = &st->data[1].d8[1],
231dbdc025bSLars-Peter Clausen .len = 3,
232dbdc025bSLars-Peter Clausen },
233dbdc025bSLars-Peter Clausen };
234dbdc025bSLars-Peter Clausen
2352bbf7c6bSSergiu Cuciurean mutex_lock(&st->lock);
236dbdc025bSLars-Peter Clausen
237dbdc025bSLars-Peter Clausen st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
238dbdc025bSLars-Peter Clausen AD5360_ADDR(AD5360_REG_SF_READBACK) |
239dbdc025bSLars-Peter Clausen AD5360_READBACK_TYPE(type) |
240dbdc025bSLars-Peter Clausen AD5360_READBACK_ADDR(addr));
241dbdc025bSLars-Peter Clausen
24214543a00SLars-Peter Clausen ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
243dbdc025bSLars-Peter Clausen if (ret >= 0)
244dbdc025bSLars-Peter Clausen ret = be32_to_cpu(st->data[1].d32) & 0xffff;
245dbdc025bSLars-Peter Clausen
2462bbf7c6bSSergiu Cuciurean mutex_unlock(&st->lock);
247dbdc025bSLars-Peter Clausen
248dbdc025bSLars-Peter Clausen return ret;
249dbdc025bSLars-Peter Clausen }
250dbdc025bSLars-Peter Clausen
ad5360_read_dac_powerdown(struct device * dev,struct device_attribute * attr,char * buf)251dbdc025bSLars-Peter Clausen static ssize_t ad5360_read_dac_powerdown(struct device *dev,
252dbdc025bSLars-Peter Clausen struct device_attribute *attr,
253dbdc025bSLars-Peter Clausen char *buf)
254dbdc025bSLars-Peter Clausen {
255dbdc025bSLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev);
256dbdc025bSLars-Peter Clausen struct ad5360_state *st = iio_priv(indio_dev);
257dbdc025bSLars-Peter Clausen
258f46ac009SLars-Peter Clausen return sysfs_emit(buf, "%d\n", (bool)(st->ctrl & AD5360_SF_CTRL_PWR_DOWN));
259dbdc025bSLars-Peter Clausen }
260dbdc025bSLars-Peter Clausen
ad5360_update_ctrl(struct iio_dev * indio_dev,unsigned int set,unsigned int clr)261dbdc025bSLars-Peter Clausen static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
262dbdc025bSLars-Peter Clausen unsigned int clr)
263dbdc025bSLars-Peter Clausen {
264dbdc025bSLars-Peter Clausen struct ad5360_state *st = iio_priv(indio_dev);
265dbdc025bSLars-Peter Clausen unsigned int ret;
266dbdc025bSLars-Peter Clausen
2672bbf7c6bSSergiu Cuciurean mutex_lock(&st->lock);
268dbdc025bSLars-Peter Clausen
269dbdc025bSLars-Peter Clausen st->ctrl |= set;
270dbdc025bSLars-Peter Clausen st->ctrl &= ~clr;
271dbdc025bSLars-Peter Clausen
272dbdc025bSLars-Peter Clausen ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
273dbdc025bSLars-Peter Clausen AD5360_REG_SF_CTRL, st->ctrl, 0);
274dbdc025bSLars-Peter Clausen
2752bbf7c6bSSergiu Cuciurean mutex_unlock(&st->lock);
276dbdc025bSLars-Peter Clausen
277dbdc025bSLars-Peter Clausen return ret;
278dbdc025bSLars-Peter Clausen }
279dbdc025bSLars-Peter Clausen
ad5360_write_dac_powerdown(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)280dbdc025bSLars-Peter Clausen static ssize_t ad5360_write_dac_powerdown(struct device *dev,
281dbdc025bSLars-Peter Clausen struct device_attribute *attr, const char *buf, size_t len)
282dbdc025bSLars-Peter Clausen {
283dbdc025bSLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev);
284dbdc025bSLars-Peter Clausen bool pwr_down;
285dbdc025bSLars-Peter Clausen int ret;
286dbdc025bSLars-Peter Clausen
28774f582ecSLars-Peter Clausen ret = kstrtobool(buf, &pwr_down);
288dbdc025bSLars-Peter Clausen if (ret)
289dbdc025bSLars-Peter Clausen return ret;
290dbdc025bSLars-Peter Clausen
291dbdc025bSLars-Peter Clausen if (pwr_down)
292dbdc025bSLars-Peter Clausen ret = ad5360_update_ctrl(indio_dev, AD5360_SF_CTRL_PWR_DOWN, 0);
293dbdc025bSLars-Peter Clausen else
294dbdc025bSLars-Peter Clausen ret = ad5360_update_ctrl(indio_dev, 0, AD5360_SF_CTRL_PWR_DOWN);
295dbdc025bSLars-Peter Clausen
296dbdc025bSLars-Peter Clausen return ret ? ret : len;
297dbdc025bSLars-Peter Clausen }
298dbdc025bSLars-Peter Clausen
299dbdc025bSLars-Peter Clausen static IIO_DEVICE_ATTR(out_voltage_powerdown,
300dbdc025bSLars-Peter Clausen S_IRUGO | S_IWUSR,
301dbdc025bSLars-Peter Clausen ad5360_read_dac_powerdown,
302dbdc025bSLars-Peter Clausen ad5360_write_dac_powerdown, 0);
303dbdc025bSLars-Peter Clausen
304dbdc025bSLars-Peter Clausen static struct attribute *ad5360_attributes[] = {
305dbdc025bSLars-Peter Clausen &iio_dev_attr_out_voltage_powerdown.dev_attr.attr,
306dbdc025bSLars-Peter Clausen NULL,
307dbdc025bSLars-Peter Clausen };
308dbdc025bSLars-Peter Clausen
309dbdc025bSLars-Peter Clausen static const struct attribute_group ad5360_attribute_group = {
310dbdc025bSLars-Peter Clausen .attrs = ad5360_attributes,
311dbdc025bSLars-Peter Clausen };
312dbdc025bSLars-Peter Clausen
ad5360_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)313dbdc025bSLars-Peter Clausen static int ad5360_write_raw(struct iio_dev *indio_dev,
314dbdc025bSLars-Peter Clausen struct iio_chan_spec const *chan,
315dbdc025bSLars-Peter Clausen int val,
316dbdc025bSLars-Peter Clausen int val2,
317dbdc025bSLars-Peter Clausen long mask)
318dbdc025bSLars-Peter Clausen {
319dbdc025bSLars-Peter Clausen struct ad5360_state *st = iio_priv(indio_dev);
320dbdc025bSLars-Peter Clausen int max_val = (1 << chan->scan_type.realbits);
321dbdc025bSLars-Peter Clausen unsigned int ofs_index;
322dbdc025bSLars-Peter Clausen
323dbdc025bSLars-Peter Clausen switch (mask) {
324dbdc025bSLars-Peter Clausen case IIO_CHAN_INFO_RAW:
325dbdc025bSLars-Peter Clausen if (val >= max_val || val < 0)
326dbdc025bSLars-Peter Clausen return -EINVAL;
327dbdc025bSLars-Peter Clausen
328dbdc025bSLars-Peter Clausen return ad5360_write(indio_dev, AD5360_CMD_WRITE_DATA,
329dbdc025bSLars-Peter Clausen chan->address, val, chan->scan_type.shift);
330dbdc025bSLars-Peter Clausen
331dbdc025bSLars-Peter Clausen case IIO_CHAN_INFO_CALIBBIAS:
332dbdc025bSLars-Peter Clausen if (val >= max_val || val < 0)
333dbdc025bSLars-Peter Clausen return -EINVAL;
334dbdc025bSLars-Peter Clausen
335dbdc025bSLars-Peter Clausen return ad5360_write(indio_dev, AD5360_CMD_WRITE_OFFSET,
336dbdc025bSLars-Peter Clausen chan->address, val, chan->scan_type.shift);
337dbdc025bSLars-Peter Clausen
338dbdc025bSLars-Peter Clausen case IIO_CHAN_INFO_CALIBSCALE:
339dbdc025bSLars-Peter Clausen if (val >= max_val || val < 0)
340dbdc025bSLars-Peter Clausen return -EINVAL;
341dbdc025bSLars-Peter Clausen
342dbdc025bSLars-Peter Clausen return ad5360_write(indio_dev, AD5360_CMD_WRITE_GAIN,
343dbdc025bSLars-Peter Clausen chan->address, val, chan->scan_type.shift);
344dbdc025bSLars-Peter Clausen
345dbdc025bSLars-Peter Clausen case IIO_CHAN_INFO_OFFSET:
346dbdc025bSLars-Peter Clausen if (val <= -max_val || val > 0)
347dbdc025bSLars-Peter Clausen return -EINVAL;
348dbdc025bSLars-Peter Clausen
349dbdc025bSLars-Peter Clausen val = -val;
350dbdc025bSLars-Peter Clausen
351dbdc025bSLars-Peter Clausen /* offset is supposed to have the same scale as raw, but it
352dbdc025bSLars-Peter Clausen * is always 14bits wide, so on a chip where the raw value has
353dbdc025bSLars-Peter Clausen * more bits, we need to shift offset. */
354dbdc025bSLars-Peter Clausen val >>= (chan->scan_type.realbits - 14);
355dbdc025bSLars-Peter Clausen
356dbdc025bSLars-Peter Clausen /* There is one DAC offset register per vref. Changing one
357dbdc025bSLars-Peter Clausen * channels offset will also change the offset for all other
358dbdc025bSLars-Peter Clausen * channels which share the same vref supply. */
359dbdc025bSLars-Peter Clausen ofs_index = ad5360_get_channel_vref_index(st, chan->channel);
360dbdc025bSLars-Peter Clausen return ad5360_write(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
361dbdc025bSLars-Peter Clausen AD5360_REG_SF_OFS(ofs_index), val, 0);
362dbdc025bSLars-Peter Clausen default:
363dbdc025bSLars-Peter Clausen break;
364dbdc025bSLars-Peter Clausen }
365dbdc025bSLars-Peter Clausen
366dbdc025bSLars-Peter Clausen return -EINVAL;
367dbdc025bSLars-Peter Clausen }
368dbdc025bSLars-Peter Clausen
ad5360_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)369dbdc025bSLars-Peter Clausen static int ad5360_read_raw(struct iio_dev *indio_dev,
370dbdc025bSLars-Peter Clausen struct iio_chan_spec const *chan,
371dbdc025bSLars-Peter Clausen int *val,
372dbdc025bSLars-Peter Clausen int *val2,
373dbdc025bSLars-Peter Clausen long m)
374dbdc025bSLars-Peter Clausen {
375dbdc025bSLars-Peter Clausen struct ad5360_state *st = iio_priv(indio_dev);
376dbdc025bSLars-Peter Clausen unsigned int ofs_index;
377dbdc025bSLars-Peter Clausen int scale_uv;
378dbdc025bSLars-Peter Clausen int ret;
379dbdc025bSLars-Peter Clausen
380dbdc025bSLars-Peter Clausen switch (m) {
381dbdc025bSLars-Peter Clausen case IIO_CHAN_INFO_RAW:
382dbdc025bSLars-Peter Clausen ret = ad5360_read(indio_dev, AD5360_READBACK_X1A,
383dbdc025bSLars-Peter Clausen chan->address);
384dbdc025bSLars-Peter Clausen if (ret < 0)
385dbdc025bSLars-Peter Clausen return ret;
386dbdc025bSLars-Peter Clausen *val = ret >> chan->scan_type.shift;
387dbdc025bSLars-Peter Clausen return IIO_VAL_INT;
388dbdc025bSLars-Peter Clausen case IIO_CHAN_INFO_SCALE:
389ca3bc8b6SLars-Peter Clausen scale_uv = ad5360_get_channel_vref(st, chan->channel);
390dbdc025bSLars-Peter Clausen if (scale_uv < 0)
391dbdc025bSLars-Peter Clausen return scale_uv;
392dbdc025bSLars-Peter Clausen
393ca3bc8b6SLars-Peter Clausen /* vout = 4 * vref * dac_code */
394ca3bc8b6SLars-Peter Clausen *val = scale_uv * 4 / 1000;
395ca3bc8b6SLars-Peter Clausen *val2 = chan->scan_type.realbits;
396ca3bc8b6SLars-Peter Clausen return IIO_VAL_FRACTIONAL_LOG2;
397dbdc025bSLars-Peter Clausen case IIO_CHAN_INFO_CALIBBIAS:
398dbdc025bSLars-Peter Clausen ret = ad5360_read(indio_dev, AD5360_READBACK_OFFSET,
399dbdc025bSLars-Peter Clausen chan->address);
400dbdc025bSLars-Peter Clausen if (ret < 0)
401dbdc025bSLars-Peter Clausen return ret;
402dbdc025bSLars-Peter Clausen *val = ret;
403dbdc025bSLars-Peter Clausen return IIO_VAL_INT;
404dbdc025bSLars-Peter Clausen case IIO_CHAN_INFO_CALIBSCALE:
405dbdc025bSLars-Peter Clausen ret = ad5360_read(indio_dev, AD5360_READBACK_GAIN,
406dbdc025bSLars-Peter Clausen chan->address);
407dbdc025bSLars-Peter Clausen if (ret < 0)
408dbdc025bSLars-Peter Clausen return ret;
409dbdc025bSLars-Peter Clausen *val = ret;
410dbdc025bSLars-Peter Clausen return IIO_VAL_INT;
411dbdc025bSLars-Peter Clausen case IIO_CHAN_INFO_OFFSET:
412dbdc025bSLars-Peter Clausen ofs_index = ad5360_get_channel_vref_index(st, chan->channel);
413dbdc025bSLars-Peter Clausen ret = ad5360_read(indio_dev, AD5360_READBACK_SF,
414dbdc025bSLars-Peter Clausen AD5360_REG_SF_OFS(ofs_index));
415dbdc025bSLars-Peter Clausen if (ret < 0)
416dbdc025bSLars-Peter Clausen return ret;
417dbdc025bSLars-Peter Clausen
418dbdc025bSLars-Peter Clausen ret <<= (chan->scan_type.realbits - 14);
419dbdc025bSLars-Peter Clausen *val = -ret;
420dbdc025bSLars-Peter Clausen return IIO_VAL_INT;
421dbdc025bSLars-Peter Clausen }
422dbdc025bSLars-Peter Clausen
423dbdc025bSLars-Peter Clausen return -EINVAL;
424dbdc025bSLars-Peter Clausen }
425dbdc025bSLars-Peter Clausen
426dbdc025bSLars-Peter Clausen static const struct iio_info ad5360_info = {
427dbdc025bSLars-Peter Clausen .read_raw = ad5360_read_raw,
428dbdc025bSLars-Peter Clausen .write_raw = ad5360_write_raw,
429dbdc025bSLars-Peter Clausen .attrs = &ad5360_attribute_group,
430dbdc025bSLars-Peter Clausen };
431dbdc025bSLars-Peter Clausen
432dbdc025bSLars-Peter Clausen static const char * const ad5360_vref_name[] = {
433dbdc025bSLars-Peter Clausen "vref0", "vref1", "vref2"
434dbdc025bSLars-Peter Clausen };
435dbdc025bSLars-Peter Clausen
ad5360_alloc_channels(struct iio_dev * indio_dev)436fc52692cSGreg Kroah-Hartman static int ad5360_alloc_channels(struct iio_dev *indio_dev)
437dbdc025bSLars-Peter Clausen {
438dbdc025bSLars-Peter Clausen struct ad5360_state *st = iio_priv(indio_dev);
439dbdc025bSLars-Peter Clausen struct iio_chan_spec *channels;
440dbdc025bSLars-Peter Clausen unsigned int i;
441dbdc025bSLars-Peter Clausen
442dbdc025bSLars-Peter Clausen channels = kcalloc(st->chip_info->num_channels,
443dbdc025bSLars-Peter Clausen sizeof(struct iio_chan_spec), GFP_KERNEL);
444dbdc025bSLars-Peter Clausen
445dbdc025bSLars-Peter Clausen if (!channels)
446dbdc025bSLars-Peter Clausen return -ENOMEM;
447dbdc025bSLars-Peter Clausen
448dbdc025bSLars-Peter Clausen for (i = 0; i < st->chip_info->num_channels; ++i) {
449dbdc025bSLars-Peter Clausen channels[i] = st->chip_info->channel_template;
450dbdc025bSLars-Peter Clausen channels[i].channel = i;
451dbdc025bSLars-Peter Clausen channels[i].address = AD5360_CHAN_ADDR(i);
452dbdc025bSLars-Peter Clausen }
453dbdc025bSLars-Peter Clausen
454dbdc025bSLars-Peter Clausen indio_dev->channels = channels;
455dbdc025bSLars-Peter Clausen
456dbdc025bSLars-Peter Clausen return 0;
457dbdc025bSLars-Peter Clausen }
458dbdc025bSLars-Peter Clausen
ad5360_probe(struct spi_device * spi)459fc52692cSGreg Kroah-Hartman static int ad5360_probe(struct spi_device *spi)
460dbdc025bSLars-Peter Clausen {
461dbdc025bSLars-Peter Clausen enum ad5360_type type = spi_get_device_id(spi)->driver_data;
462dbdc025bSLars-Peter Clausen struct iio_dev *indio_dev;
463dbdc025bSLars-Peter Clausen struct ad5360_state *st;
464dbdc025bSLars-Peter Clausen unsigned int i;
465dbdc025bSLars-Peter Clausen int ret;
466dbdc025bSLars-Peter Clausen
467400d36e6SSachin Kamat indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
468dbdc025bSLars-Peter Clausen if (indio_dev == NULL) {
469dbdc025bSLars-Peter Clausen dev_err(&spi->dev, "Failed to allocate iio device\n");
470dbdc025bSLars-Peter Clausen return -ENOMEM;
471dbdc025bSLars-Peter Clausen }
472dbdc025bSLars-Peter Clausen
473dbdc025bSLars-Peter Clausen st = iio_priv(indio_dev);
474dbdc025bSLars-Peter Clausen spi_set_drvdata(spi, indio_dev);
475dbdc025bSLars-Peter Clausen
476dbdc025bSLars-Peter Clausen st->chip_info = &ad5360_chip_info_tbl[type];
477dbdc025bSLars-Peter Clausen st->spi = spi;
478dbdc025bSLars-Peter Clausen
479dbdc025bSLars-Peter Clausen indio_dev->name = spi_get_device_id(spi)->name;
480dbdc025bSLars-Peter Clausen indio_dev->info = &ad5360_info;
481dbdc025bSLars-Peter Clausen indio_dev->modes = INDIO_DIRECT_MODE;
482dbdc025bSLars-Peter Clausen indio_dev->num_channels = st->chip_info->num_channels;
483dbdc025bSLars-Peter Clausen
4842bbf7c6bSSergiu Cuciurean mutex_init(&st->lock);
4852bbf7c6bSSergiu Cuciurean
486dbdc025bSLars-Peter Clausen ret = ad5360_alloc_channels(indio_dev);
487dbdc025bSLars-Peter Clausen if (ret) {
488dbdc025bSLars-Peter Clausen dev_err(&spi->dev, "Failed to allocate channel spec: %d\n", ret);
489400d36e6SSachin Kamat return ret;
490dbdc025bSLars-Peter Clausen }
491dbdc025bSLars-Peter Clausen
492dbdc025bSLars-Peter Clausen for (i = 0; i < st->chip_info->num_vrefs; ++i)
493dbdc025bSLars-Peter Clausen st->vref_reg[i].supply = ad5360_vref_name[i];
494dbdc025bSLars-Peter Clausen
495400d36e6SSachin Kamat ret = devm_regulator_bulk_get(&st->spi->dev, st->chip_info->num_vrefs,
496dbdc025bSLars-Peter Clausen st->vref_reg);
497dbdc025bSLars-Peter Clausen if (ret) {
498dbdc025bSLars-Peter Clausen dev_err(&spi->dev, "Failed to request vref regulators: %d\n", ret);
499dbdc025bSLars-Peter Clausen goto error_free_channels;
500dbdc025bSLars-Peter Clausen }
501dbdc025bSLars-Peter Clausen
502dbdc025bSLars-Peter Clausen ret = regulator_bulk_enable(st->chip_info->num_vrefs, st->vref_reg);
503dbdc025bSLars-Peter Clausen if (ret) {
504dbdc025bSLars-Peter Clausen dev_err(&spi->dev, "Failed to enable vref regulators: %d\n", ret);
505400d36e6SSachin Kamat goto error_free_channels;
506dbdc025bSLars-Peter Clausen }
507dbdc025bSLars-Peter Clausen
508dbdc025bSLars-Peter Clausen ret = iio_device_register(indio_dev);
509dbdc025bSLars-Peter Clausen if (ret) {
510dbdc025bSLars-Peter Clausen dev_err(&spi->dev, "Failed to register iio device: %d\n", ret);
511dbdc025bSLars-Peter Clausen goto error_disable_reg;
512dbdc025bSLars-Peter Clausen }
513dbdc025bSLars-Peter Clausen
514dbdc025bSLars-Peter Clausen return 0;
515dbdc025bSLars-Peter Clausen
516dbdc025bSLars-Peter Clausen error_disable_reg:
517dbdc025bSLars-Peter Clausen regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
518dbdc025bSLars-Peter Clausen error_free_channels:
519dbdc025bSLars-Peter Clausen kfree(indio_dev->channels);
520dbdc025bSLars-Peter Clausen
521dbdc025bSLars-Peter Clausen return ret;
522dbdc025bSLars-Peter Clausen }
523dbdc025bSLars-Peter Clausen
ad5360_remove(struct spi_device * spi)524a0386bbaSUwe Kleine-König static void ad5360_remove(struct spi_device *spi)
525dbdc025bSLars-Peter Clausen {
526dbdc025bSLars-Peter Clausen struct iio_dev *indio_dev = spi_get_drvdata(spi);
527dbdc025bSLars-Peter Clausen struct ad5360_state *st = iio_priv(indio_dev);
528dbdc025bSLars-Peter Clausen
529dbdc025bSLars-Peter Clausen iio_device_unregister(indio_dev);
530dbdc025bSLars-Peter Clausen
531dbdc025bSLars-Peter Clausen kfree(indio_dev->channels);
532dbdc025bSLars-Peter Clausen
533dbdc025bSLars-Peter Clausen regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
534dbdc025bSLars-Peter Clausen }
535dbdc025bSLars-Peter Clausen
536dbdc025bSLars-Peter Clausen static const struct spi_device_id ad5360_ids[] = {
537dbdc025bSLars-Peter Clausen { "ad5360", ID_AD5360 },
538dbdc025bSLars-Peter Clausen { "ad5361", ID_AD5361 },
539dbdc025bSLars-Peter Clausen { "ad5362", ID_AD5362 },
540dbdc025bSLars-Peter Clausen { "ad5363", ID_AD5363 },
541dbdc025bSLars-Peter Clausen { "ad5370", ID_AD5370 },
542dbdc025bSLars-Peter Clausen { "ad5371", ID_AD5371 },
543dbdc025bSLars-Peter Clausen { "ad5372", ID_AD5372 },
544dbdc025bSLars-Peter Clausen { "ad5373", ID_AD5373 },
545dbdc025bSLars-Peter Clausen {}
546dbdc025bSLars-Peter Clausen };
547dbdc025bSLars-Peter Clausen MODULE_DEVICE_TABLE(spi, ad5360_ids);
548dbdc025bSLars-Peter Clausen
549dbdc025bSLars-Peter Clausen static struct spi_driver ad5360_driver = {
550dbdc025bSLars-Peter Clausen .driver = {
551dbdc025bSLars-Peter Clausen .name = "ad5360",
552dbdc025bSLars-Peter Clausen },
553dbdc025bSLars-Peter Clausen .probe = ad5360_probe,
554fc52692cSGreg Kroah-Hartman .remove = ad5360_remove,
555dbdc025bSLars-Peter Clausen .id_table = ad5360_ids,
556dbdc025bSLars-Peter Clausen };
557dbdc025bSLars-Peter Clausen module_spi_driver(ad5360_driver);
558dbdc025bSLars-Peter Clausen
559dbdc025bSLars-Peter Clausen MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
560dbdc025bSLars-Peter Clausen MODULE_DESCRIPTION("Analog Devices AD5360/61/62/63/70/71/72/73 DAC");
561dbdc025bSLars-Peter Clausen MODULE_LICENSE("GPL v2");
562