xref: /linux/drivers/iio/dac/ad5064.c (revision c367982ad9b927ec1b1fd66a462b72950fe5370b)
1dbdc025bSLars-Peter Clausen /*
26a17a076SLars-Peter Clausen  * AD5024, AD5025, AD5044, AD5045, AD5064, AD5064-1, AD5065, AD5628, AD5629R,
36a17a076SLars-Peter Clausen  * AD5648, AD5666, AD5668, AD5669R Digital to analog converters driver
4dbdc025bSLars-Peter Clausen  *
5dbdc025bSLars-Peter Clausen  * Copyright 2011 Analog Devices Inc.
6dbdc025bSLars-Peter Clausen  *
7dbdc025bSLars-Peter Clausen  * Licensed under the GPL-2.
8dbdc025bSLars-Peter Clausen  */
9dbdc025bSLars-Peter Clausen 
10dbdc025bSLars-Peter Clausen #include <linux/device.h>
11dbdc025bSLars-Peter Clausen #include <linux/err.h>
12dbdc025bSLars-Peter Clausen #include <linux/module.h>
13dbdc025bSLars-Peter Clausen #include <linux/kernel.h>
14dbdc025bSLars-Peter Clausen #include <linux/spi/spi.h>
156a17a076SLars-Peter Clausen #include <linux/i2c.h>
16dbdc025bSLars-Peter Clausen #include <linux/slab.h>
17dbdc025bSLars-Peter Clausen #include <linux/sysfs.h>
18dbdc025bSLars-Peter Clausen #include <linux/regulator/consumer.h>
196a17a076SLars-Peter Clausen #include <asm/unaligned.h>
20dbdc025bSLars-Peter Clausen 
21dbdc025bSLars-Peter Clausen #include <linux/iio/iio.h>
22dbdc025bSLars-Peter Clausen #include <linux/iio/sysfs.h>
23dbdc025bSLars-Peter Clausen 
24dbdc025bSLars-Peter Clausen #define AD5064_MAX_DAC_CHANNELS			8
25dbdc025bSLars-Peter Clausen #define AD5064_MAX_VREFS			4
26dbdc025bSLars-Peter Clausen 
27dbdc025bSLars-Peter Clausen #define AD5064_ADDR(x)				((x) << 20)
28dbdc025bSLars-Peter Clausen #define AD5064_CMD(x)				((x) << 24)
29dbdc025bSLars-Peter Clausen 
30dbdc025bSLars-Peter Clausen #define AD5064_ADDR_ALL_DAC			0xF
31dbdc025bSLars-Peter Clausen 
32dbdc025bSLars-Peter Clausen #define AD5064_CMD_WRITE_INPUT_N		0x0
33dbdc025bSLars-Peter Clausen #define AD5064_CMD_UPDATE_DAC_N			0x1
34dbdc025bSLars-Peter Clausen #define AD5064_CMD_WRITE_INPUT_N_UPDATE_ALL	0x2
35dbdc025bSLars-Peter Clausen #define AD5064_CMD_WRITE_INPUT_N_UPDATE_N	0x3
36dbdc025bSLars-Peter Clausen #define AD5064_CMD_POWERDOWN_DAC		0x4
37dbdc025bSLars-Peter Clausen #define AD5064_CMD_CLEAR			0x5
38dbdc025bSLars-Peter Clausen #define AD5064_CMD_LDAC_MASK			0x6
39dbdc025bSLars-Peter Clausen #define AD5064_CMD_RESET			0x7
40dbdc025bSLars-Peter Clausen #define AD5064_CMD_CONFIG			0x8
41dbdc025bSLars-Peter Clausen 
42dbdc025bSLars-Peter Clausen #define AD5064_CONFIG_DAISY_CHAIN_ENABLE	BIT(1)
43dbdc025bSLars-Peter Clausen #define AD5064_CONFIG_INT_VREF_ENABLE		BIT(0)
44dbdc025bSLars-Peter Clausen 
45dbdc025bSLars-Peter Clausen #define AD5064_LDAC_PWRDN_NONE			0x0
46dbdc025bSLars-Peter Clausen #define AD5064_LDAC_PWRDN_1K			0x1
47dbdc025bSLars-Peter Clausen #define AD5064_LDAC_PWRDN_100K			0x2
48dbdc025bSLars-Peter Clausen #define AD5064_LDAC_PWRDN_3STATE		0x3
49dbdc025bSLars-Peter Clausen 
50dbdc025bSLars-Peter Clausen /**
51dbdc025bSLars-Peter Clausen  * struct ad5064_chip_info - chip specific information
52dbdc025bSLars-Peter Clausen  * @shared_vref:	whether the vref supply is shared between channels
53dbdc025bSLars-Peter Clausen  * @internal_vref:	internal reference voltage. 0 if the chip has no internal
54dbdc025bSLars-Peter Clausen  *			vref.
55dbdc025bSLars-Peter Clausen  * @channel:		channel specification
56dbdc025bSLars-Peter Clausen  * @num_channels:	number of channels
57dbdc025bSLars-Peter Clausen  */
58dbdc025bSLars-Peter Clausen 
59dbdc025bSLars-Peter Clausen struct ad5064_chip_info {
60dbdc025bSLars-Peter Clausen 	bool shared_vref;
61dbdc025bSLars-Peter Clausen 	unsigned long internal_vref;
62dbdc025bSLars-Peter Clausen 	const struct iio_chan_spec *channels;
63dbdc025bSLars-Peter Clausen 	unsigned int num_channels;
64dbdc025bSLars-Peter Clausen };
65dbdc025bSLars-Peter Clausen 
666a17a076SLars-Peter Clausen struct ad5064_state;
676a17a076SLars-Peter Clausen 
686a17a076SLars-Peter Clausen typedef int (*ad5064_write_func)(struct ad5064_state *st, unsigned int cmd,
696a17a076SLars-Peter Clausen 		unsigned int addr, unsigned int val);
706a17a076SLars-Peter Clausen 
71dbdc025bSLars-Peter Clausen /**
72dbdc025bSLars-Peter Clausen  * struct ad5064_state - driver instance specific data
736a17a076SLars-Peter Clausen  * @dev:		the device for this driver instance
74dbdc025bSLars-Peter Clausen  * @chip_info:		chip model specific constants, available modes etc
75dbdc025bSLars-Peter Clausen  * @vref_reg:		vref supply regulators
76dbdc025bSLars-Peter Clausen  * @pwr_down:		whether channel is powered down
77dbdc025bSLars-Peter Clausen  * @pwr_down_mode:	channel's current power down mode
78dbdc025bSLars-Peter Clausen  * @dac_cache:		current DAC raw value (chip does not support readback)
79dbdc025bSLars-Peter Clausen  * @use_internal_vref:	set to true if the internal reference voltage should be
80dbdc025bSLars-Peter Clausen  *			used.
816a17a076SLars-Peter Clausen  * @write:		register write callback
826a17a076SLars-Peter Clausen  * @data:		i2c/spi transfer buffers
83dbdc025bSLars-Peter Clausen  */
84dbdc025bSLars-Peter Clausen 
85dbdc025bSLars-Peter Clausen struct ad5064_state {
866a17a076SLars-Peter Clausen 	struct device			*dev;
87dbdc025bSLars-Peter Clausen 	const struct ad5064_chip_info	*chip_info;
88dbdc025bSLars-Peter Clausen 	struct regulator_bulk_data	vref_reg[AD5064_MAX_VREFS];
89dbdc025bSLars-Peter Clausen 	bool				pwr_down[AD5064_MAX_DAC_CHANNELS];
90dbdc025bSLars-Peter Clausen 	u8				pwr_down_mode[AD5064_MAX_DAC_CHANNELS];
91dbdc025bSLars-Peter Clausen 	unsigned int			dac_cache[AD5064_MAX_DAC_CHANNELS];
92dbdc025bSLars-Peter Clausen 	bool				use_internal_vref;
93dbdc025bSLars-Peter Clausen 
946a17a076SLars-Peter Clausen 	ad5064_write_func		write;
956a17a076SLars-Peter Clausen 
96dbdc025bSLars-Peter Clausen 	/*
97dbdc025bSLars-Peter Clausen 	 * DMA (thus cache coherency maintenance) requires the
98dbdc025bSLars-Peter Clausen 	 * transfer buffers to live in their own cache lines.
99dbdc025bSLars-Peter Clausen 	 */
1006a17a076SLars-Peter Clausen 	union {
1016a17a076SLars-Peter Clausen 		u8 i2c[3];
1026a17a076SLars-Peter Clausen 		__be32 spi;
1036a17a076SLars-Peter Clausen 	} data ____cacheline_aligned;
104dbdc025bSLars-Peter Clausen };
105dbdc025bSLars-Peter Clausen 
106dbdc025bSLars-Peter Clausen enum ad5064_type {
107dbdc025bSLars-Peter Clausen 	ID_AD5024,
108dbdc025bSLars-Peter Clausen 	ID_AD5025,
109dbdc025bSLars-Peter Clausen 	ID_AD5044,
110dbdc025bSLars-Peter Clausen 	ID_AD5045,
111dbdc025bSLars-Peter Clausen 	ID_AD5064,
112dbdc025bSLars-Peter Clausen 	ID_AD5064_1,
113dbdc025bSLars-Peter Clausen 	ID_AD5065,
114dbdc025bSLars-Peter Clausen 	ID_AD5628_1,
115dbdc025bSLars-Peter Clausen 	ID_AD5628_2,
116dbdc025bSLars-Peter Clausen 	ID_AD5648_1,
117dbdc025bSLars-Peter Clausen 	ID_AD5648_2,
118dbdc025bSLars-Peter Clausen 	ID_AD5666_1,
119dbdc025bSLars-Peter Clausen 	ID_AD5666_2,
120dbdc025bSLars-Peter Clausen 	ID_AD5668_1,
121dbdc025bSLars-Peter Clausen 	ID_AD5668_2,
122dbdc025bSLars-Peter Clausen };
123dbdc025bSLars-Peter Clausen 
1246a17a076SLars-Peter Clausen static int ad5064_write(struct ad5064_state *st, unsigned int cmd,
125dbdc025bSLars-Peter Clausen 	unsigned int addr, unsigned int val, unsigned int shift)
126dbdc025bSLars-Peter Clausen {
127dbdc025bSLars-Peter Clausen 	val <<= shift;
128dbdc025bSLars-Peter Clausen 
1296a17a076SLars-Peter Clausen 	return st->write(st, cmd, addr, val);
130dbdc025bSLars-Peter Clausen }
131dbdc025bSLars-Peter Clausen 
132dbdc025bSLars-Peter Clausen static int ad5064_sync_powerdown_mode(struct ad5064_state *st,
133a2630262SLars-Peter Clausen 	const struct iio_chan_spec *chan)
134dbdc025bSLars-Peter Clausen {
135dbdc025bSLars-Peter Clausen 	unsigned int val;
136dbdc025bSLars-Peter Clausen 	int ret;
137dbdc025bSLars-Peter Clausen 
138a2630262SLars-Peter Clausen 	val = (0x1 << chan->address);
139dbdc025bSLars-Peter Clausen 
140a2630262SLars-Peter Clausen 	if (st->pwr_down[chan->channel])
141a2630262SLars-Peter Clausen 		val |= st->pwr_down_mode[chan->channel] << 8;
142dbdc025bSLars-Peter Clausen 
1436a17a076SLars-Peter Clausen 	ret = ad5064_write(st, AD5064_CMD_POWERDOWN_DAC, 0, val, 0);
144dbdc025bSLars-Peter Clausen 
145dbdc025bSLars-Peter Clausen 	return ret;
146dbdc025bSLars-Peter Clausen }
147dbdc025bSLars-Peter Clausen 
148dbdc025bSLars-Peter Clausen static const char * const ad5064_powerdown_modes[] = {
149dbdc025bSLars-Peter Clausen 	"1kohm_to_gnd",
150dbdc025bSLars-Peter Clausen 	"100kohm_to_gnd",
151dbdc025bSLars-Peter Clausen 	"three_state",
152dbdc025bSLars-Peter Clausen };
153dbdc025bSLars-Peter Clausen 
154dbdc025bSLars-Peter Clausen static int ad5064_get_powerdown_mode(struct iio_dev *indio_dev,
155dbdc025bSLars-Peter Clausen 	const struct iio_chan_spec *chan)
156dbdc025bSLars-Peter Clausen {
157dbdc025bSLars-Peter Clausen 	struct ad5064_state *st = iio_priv(indio_dev);
158dbdc025bSLars-Peter Clausen 
159dbdc025bSLars-Peter Clausen 	return st->pwr_down_mode[chan->channel] - 1;
160dbdc025bSLars-Peter Clausen }
161dbdc025bSLars-Peter Clausen 
162dbdc025bSLars-Peter Clausen static int ad5064_set_powerdown_mode(struct iio_dev *indio_dev,
163dbdc025bSLars-Peter Clausen 	const struct iio_chan_spec *chan, unsigned int mode)
164dbdc025bSLars-Peter Clausen {
165dbdc025bSLars-Peter Clausen 	struct ad5064_state *st = iio_priv(indio_dev);
166dbdc025bSLars-Peter Clausen 	int ret;
167dbdc025bSLars-Peter Clausen 
168dbdc025bSLars-Peter Clausen 	mutex_lock(&indio_dev->mlock);
169dbdc025bSLars-Peter Clausen 	st->pwr_down_mode[chan->channel] = mode + 1;
170dbdc025bSLars-Peter Clausen 
171a2630262SLars-Peter Clausen 	ret = ad5064_sync_powerdown_mode(st, chan);
172dbdc025bSLars-Peter Clausen 	mutex_unlock(&indio_dev->mlock);
173dbdc025bSLars-Peter Clausen 
174dbdc025bSLars-Peter Clausen 	return ret;
175dbdc025bSLars-Peter Clausen }
176dbdc025bSLars-Peter Clausen 
177dbdc025bSLars-Peter Clausen static const struct iio_enum ad5064_powerdown_mode_enum = {
178dbdc025bSLars-Peter Clausen 	.items = ad5064_powerdown_modes,
179dbdc025bSLars-Peter Clausen 	.num_items = ARRAY_SIZE(ad5064_powerdown_modes),
180dbdc025bSLars-Peter Clausen 	.get = ad5064_get_powerdown_mode,
181dbdc025bSLars-Peter Clausen 	.set = ad5064_set_powerdown_mode,
182dbdc025bSLars-Peter Clausen };
183dbdc025bSLars-Peter Clausen 
184dbdc025bSLars-Peter Clausen static ssize_t ad5064_read_dac_powerdown(struct iio_dev *indio_dev,
185dbdc025bSLars-Peter Clausen 	uintptr_t private, const struct iio_chan_spec *chan, char *buf)
186dbdc025bSLars-Peter Clausen {
187dbdc025bSLars-Peter Clausen 	struct ad5064_state *st = iio_priv(indio_dev);
188dbdc025bSLars-Peter Clausen 
189dbdc025bSLars-Peter Clausen 	return sprintf(buf, "%d\n", st->pwr_down[chan->channel]);
190dbdc025bSLars-Peter Clausen }
191dbdc025bSLars-Peter Clausen 
192dbdc025bSLars-Peter Clausen static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev,
193dbdc025bSLars-Peter Clausen 	 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
194dbdc025bSLars-Peter Clausen 	 size_t len)
195dbdc025bSLars-Peter Clausen {
196dbdc025bSLars-Peter Clausen 	struct ad5064_state *st = iio_priv(indio_dev);
197dbdc025bSLars-Peter Clausen 	bool pwr_down;
198dbdc025bSLars-Peter Clausen 	int ret;
199dbdc025bSLars-Peter Clausen 
200dbdc025bSLars-Peter Clausen 	ret = strtobool(buf, &pwr_down);
201dbdc025bSLars-Peter Clausen 	if (ret)
202dbdc025bSLars-Peter Clausen 		return ret;
203dbdc025bSLars-Peter Clausen 
204dbdc025bSLars-Peter Clausen 	mutex_lock(&indio_dev->mlock);
205dbdc025bSLars-Peter Clausen 	st->pwr_down[chan->channel] = pwr_down;
206dbdc025bSLars-Peter Clausen 
207a2630262SLars-Peter Clausen 	ret = ad5064_sync_powerdown_mode(st, chan);
208dbdc025bSLars-Peter Clausen 	mutex_unlock(&indio_dev->mlock);
209dbdc025bSLars-Peter Clausen 	return ret ? ret : len;
210dbdc025bSLars-Peter Clausen }
211dbdc025bSLars-Peter Clausen 
212dbdc025bSLars-Peter Clausen static int ad5064_get_vref(struct ad5064_state *st,
213dbdc025bSLars-Peter Clausen 	struct iio_chan_spec const *chan)
214dbdc025bSLars-Peter Clausen {
215dbdc025bSLars-Peter Clausen 	unsigned int i;
216dbdc025bSLars-Peter Clausen 
217dbdc025bSLars-Peter Clausen 	if (st->use_internal_vref)
218dbdc025bSLars-Peter Clausen 		return st->chip_info->internal_vref;
219dbdc025bSLars-Peter Clausen 
220dbdc025bSLars-Peter Clausen 	i = st->chip_info->shared_vref ? 0 : chan->channel;
221dbdc025bSLars-Peter Clausen 	return regulator_get_voltage(st->vref_reg[i].consumer);
222dbdc025bSLars-Peter Clausen }
223dbdc025bSLars-Peter Clausen 
224dbdc025bSLars-Peter Clausen static int ad5064_read_raw(struct iio_dev *indio_dev,
225dbdc025bSLars-Peter Clausen 			   struct iio_chan_spec const *chan,
226dbdc025bSLars-Peter Clausen 			   int *val,
227dbdc025bSLars-Peter Clausen 			   int *val2,
228dbdc025bSLars-Peter Clausen 			   long m)
229dbdc025bSLars-Peter Clausen {
230dbdc025bSLars-Peter Clausen 	struct ad5064_state *st = iio_priv(indio_dev);
231dbdc025bSLars-Peter Clausen 	int scale_uv;
232dbdc025bSLars-Peter Clausen 
233dbdc025bSLars-Peter Clausen 	switch (m) {
234dbdc025bSLars-Peter Clausen 	case IIO_CHAN_INFO_RAW:
235dbdc025bSLars-Peter Clausen 		*val = st->dac_cache[chan->channel];
236dbdc025bSLars-Peter Clausen 		return IIO_VAL_INT;
237dbdc025bSLars-Peter Clausen 	case IIO_CHAN_INFO_SCALE:
238dbdc025bSLars-Peter Clausen 		scale_uv = ad5064_get_vref(st, chan);
239dbdc025bSLars-Peter Clausen 		if (scale_uv < 0)
240dbdc025bSLars-Peter Clausen 			return scale_uv;
241dbdc025bSLars-Peter Clausen 
242dbdc025bSLars-Peter Clausen 		scale_uv = (scale_uv * 100) >> chan->scan_type.realbits;
243dbdc025bSLars-Peter Clausen 		*val =  scale_uv / 100000;
244dbdc025bSLars-Peter Clausen 		*val2 = (scale_uv % 100000) * 10;
245dbdc025bSLars-Peter Clausen 		return IIO_VAL_INT_PLUS_MICRO;
246dbdc025bSLars-Peter Clausen 	default:
247dbdc025bSLars-Peter Clausen 		break;
248dbdc025bSLars-Peter Clausen 	}
249dbdc025bSLars-Peter Clausen 	return -EINVAL;
250dbdc025bSLars-Peter Clausen }
251dbdc025bSLars-Peter Clausen 
252dbdc025bSLars-Peter Clausen static int ad5064_write_raw(struct iio_dev *indio_dev,
253dbdc025bSLars-Peter Clausen 	struct iio_chan_spec const *chan, int val, int val2, long mask)
254dbdc025bSLars-Peter Clausen {
255dbdc025bSLars-Peter Clausen 	struct ad5064_state *st = iio_priv(indio_dev);
256dbdc025bSLars-Peter Clausen 	int ret;
257dbdc025bSLars-Peter Clausen 
258dbdc025bSLars-Peter Clausen 	switch (mask) {
259dbdc025bSLars-Peter Clausen 	case IIO_CHAN_INFO_RAW:
260c5ef717aSLars-Peter Clausen 		if (val >= (1 << chan->scan_type.realbits) || val < 0)
261dbdc025bSLars-Peter Clausen 			return -EINVAL;
262dbdc025bSLars-Peter Clausen 
263dbdc025bSLars-Peter Clausen 		mutex_lock(&indio_dev->mlock);
2646a17a076SLars-Peter Clausen 		ret = ad5064_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N,
265dbdc025bSLars-Peter Clausen 				chan->address, val, chan->scan_type.shift);
266dbdc025bSLars-Peter Clausen 		if (ret == 0)
267dbdc025bSLars-Peter Clausen 			st->dac_cache[chan->channel] = val;
268dbdc025bSLars-Peter Clausen 		mutex_unlock(&indio_dev->mlock);
269dbdc025bSLars-Peter Clausen 		break;
270dbdc025bSLars-Peter Clausen 	default:
271dbdc025bSLars-Peter Clausen 		ret = -EINVAL;
272dbdc025bSLars-Peter Clausen 	}
273dbdc025bSLars-Peter Clausen 
274dbdc025bSLars-Peter Clausen 	return ret;
275dbdc025bSLars-Peter Clausen }
276dbdc025bSLars-Peter Clausen 
277dbdc025bSLars-Peter Clausen static const struct iio_info ad5064_info = {
278dbdc025bSLars-Peter Clausen 	.read_raw = ad5064_read_raw,
279dbdc025bSLars-Peter Clausen 	.write_raw = ad5064_write_raw,
280dbdc025bSLars-Peter Clausen 	.driver_module = THIS_MODULE,
281dbdc025bSLars-Peter Clausen };
282dbdc025bSLars-Peter Clausen 
283dbdc025bSLars-Peter Clausen static const struct iio_chan_spec_ext_info ad5064_ext_info[] = {
284dbdc025bSLars-Peter Clausen 	{
285dbdc025bSLars-Peter Clausen 		.name = "powerdown",
286dbdc025bSLars-Peter Clausen 		.read = ad5064_read_dac_powerdown,
287dbdc025bSLars-Peter Clausen 		.write = ad5064_write_dac_powerdown,
288dbdc025bSLars-Peter Clausen 	},
289dbdc025bSLars-Peter Clausen 	IIO_ENUM("powerdown_mode", false, &ad5064_powerdown_mode_enum),
290dbdc025bSLars-Peter Clausen 	IIO_ENUM_AVAILABLE("powerdown_mode", &ad5064_powerdown_mode_enum),
291dbdc025bSLars-Peter Clausen 	{ },
292dbdc025bSLars-Peter Clausen };
293dbdc025bSLars-Peter Clausen 
294a2630262SLars-Peter Clausen #define AD5064_CHANNEL(chan, addr, bits) {			\
295dbdc025bSLars-Peter Clausen 	.type = IIO_VOLTAGE,					\
296dbdc025bSLars-Peter Clausen 	.indexed = 1,						\
297dbdc025bSLars-Peter Clausen 	.output = 1,						\
298dbdc025bSLars-Peter Clausen 	.channel = (chan),					\
29920a0edddSJonathan Cameron 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
30020a0edddSJonathan Cameron 	BIT(IIO_CHAN_INFO_SCALE),					\
301a2630262SLars-Peter Clausen 	.address = addr,					\
302dbdc025bSLars-Peter Clausen 	.scan_type = IIO_ST('u', (bits), 16, 20 - (bits)),	\
303dbdc025bSLars-Peter Clausen 	.ext_info = ad5064_ext_info,				\
304dbdc025bSLars-Peter Clausen }
305dbdc025bSLars-Peter Clausen 
306dbdc025bSLars-Peter Clausen #define DECLARE_AD5064_CHANNELS(name, bits) \
307dbdc025bSLars-Peter Clausen const struct iio_chan_spec name[] = { \
308a2630262SLars-Peter Clausen 	AD5064_CHANNEL(0, 0, bits), \
309a2630262SLars-Peter Clausen 	AD5064_CHANNEL(1, 1, bits), \
310a2630262SLars-Peter Clausen 	AD5064_CHANNEL(2, 2, bits), \
311a2630262SLars-Peter Clausen 	AD5064_CHANNEL(3, 3, bits), \
312a2630262SLars-Peter Clausen 	AD5064_CHANNEL(4, 4, bits), \
313a2630262SLars-Peter Clausen 	AD5064_CHANNEL(5, 5, bits), \
314a2630262SLars-Peter Clausen 	AD5064_CHANNEL(6, 6, bits), \
315a2630262SLars-Peter Clausen 	AD5064_CHANNEL(7, 7, bits), \
316a2630262SLars-Peter Clausen }
317a2630262SLars-Peter Clausen 
318a2630262SLars-Peter Clausen #define DECLARE_AD5065_CHANNELS(name, bits) \
319a2630262SLars-Peter Clausen const struct iio_chan_spec name[] = { \
320a2630262SLars-Peter Clausen 	AD5064_CHANNEL(0, 0, bits), \
321a2630262SLars-Peter Clausen 	AD5064_CHANNEL(1, 3, bits), \
322dbdc025bSLars-Peter Clausen }
323dbdc025bSLars-Peter Clausen 
324dbdc025bSLars-Peter Clausen static DECLARE_AD5064_CHANNELS(ad5024_channels, 12);
325dbdc025bSLars-Peter Clausen static DECLARE_AD5064_CHANNELS(ad5044_channels, 14);
326dbdc025bSLars-Peter Clausen static DECLARE_AD5064_CHANNELS(ad5064_channels, 16);
327dbdc025bSLars-Peter Clausen 
328a2630262SLars-Peter Clausen static DECLARE_AD5065_CHANNELS(ad5025_channels, 12);
329a2630262SLars-Peter Clausen static DECLARE_AD5065_CHANNELS(ad5045_channels, 14);
330a2630262SLars-Peter Clausen static DECLARE_AD5065_CHANNELS(ad5065_channels, 16);
331a2630262SLars-Peter Clausen 
332dbdc025bSLars-Peter Clausen static const struct ad5064_chip_info ad5064_chip_info_tbl[] = {
333dbdc025bSLars-Peter Clausen 	[ID_AD5024] = {
334dbdc025bSLars-Peter Clausen 		.shared_vref = false,
335dbdc025bSLars-Peter Clausen 		.channels = ad5024_channels,
336dbdc025bSLars-Peter Clausen 		.num_channels = 4,
337dbdc025bSLars-Peter Clausen 	},
338dbdc025bSLars-Peter Clausen 	[ID_AD5025] = {
339dbdc025bSLars-Peter Clausen 		.shared_vref = false,
340a2630262SLars-Peter Clausen 		.channels = ad5025_channels,
341dbdc025bSLars-Peter Clausen 		.num_channels = 2,
342dbdc025bSLars-Peter Clausen 	},
343dbdc025bSLars-Peter Clausen 	[ID_AD5044] = {
344dbdc025bSLars-Peter Clausen 		.shared_vref = false,
345dbdc025bSLars-Peter Clausen 		.channels = ad5044_channels,
346dbdc025bSLars-Peter Clausen 		.num_channels = 4,
347dbdc025bSLars-Peter Clausen 	},
348dbdc025bSLars-Peter Clausen 	[ID_AD5045] = {
349dbdc025bSLars-Peter Clausen 		.shared_vref = false,
350a2630262SLars-Peter Clausen 		.channels = ad5045_channels,
351dbdc025bSLars-Peter Clausen 		.num_channels = 2,
352dbdc025bSLars-Peter Clausen 	},
353dbdc025bSLars-Peter Clausen 	[ID_AD5064] = {
354dbdc025bSLars-Peter Clausen 		.shared_vref = false,
355dbdc025bSLars-Peter Clausen 		.channels = ad5064_channels,
356dbdc025bSLars-Peter Clausen 		.num_channels = 4,
357dbdc025bSLars-Peter Clausen 	},
358dbdc025bSLars-Peter Clausen 	[ID_AD5064_1] = {
359dbdc025bSLars-Peter Clausen 		.shared_vref = true,
360dbdc025bSLars-Peter Clausen 		.channels = ad5064_channels,
361dbdc025bSLars-Peter Clausen 		.num_channels = 4,
362dbdc025bSLars-Peter Clausen 	},
363dbdc025bSLars-Peter Clausen 	[ID_AD5065] = {
364dbdc025bSLars-Peter Clausen 		.shared_vref = false,
365a2630262SLars-Peter Clausen 		.channels = ad5065_channels,
366dbdc025bSLars-Peter Clausen 		.num_channels = 2,
367dbdc025bSLars-Peter Clausen 	},
368dbdc025bSLars-Peter Clausen 	[ID_AD5628_1] = {
369dbdc025bSLars-Peter Clausen 		.shared_vref = true,
370dbdc025bSLars-Peter Clausen 		.internal_vref = 2500000,
371dbdc025bSLars-Peter Clausen 		.channels = ad5024_channels,
372dbdc025bSLars-Peter Clausen 		.num_channels = 8,
373dbdc025bSLars-Peter Clausen 	},
374dbdc025bSLars-Peter Clausen 	[ID_AD5628_2] = {
375dbdc025bSLars-Peter Clausen 		.shared_vref = true,
376dbdc025bSLars-Peter Clausen 		.internal_vref = 5000000,
377dbdc025bSLars-Peter Clausen 		.channels = ad5024_channels,
378dbdc025bSLars-Peter Clausen 		.num_channels = 8,
379dbdc025bSLars-Peter Clausen 	},
380dbdc025bSLars-Peter Clausen 	[ID_AD5648_1] = {
381dbdc025bSLars-Peter Clausen 		.shared_vref = true,
382dbdc025bSLars-Peter Clausen 		.internal_vref = 2500000,
383dbdc025bSLars-Peter Clausen 		.channels = ad5044_channels,
384dbdc025bSLars-Peter Clausen 		.num_channels = 8,
385dbdc025bSLars-Peter Clausen 	},
386dbdc025bSLars-Peter Clausen 	[ID_AD5648_2] = {
387dbdc025bSLars-Peter Clausen 		.shared_vref = true,
388dbdc025bSLars-Peter Clausen 		.internal_vref = 5000000,
389dbdc025bSLars-Peter Clausen 		.channels = ad5044_channels,
390dbdc025bSLars-Peter Clausen 		.num_channels = 8,
391dbdc025bSLars-Peter Clausen 	},
392dbdc025bSLars-Peter Clausen 	[ID_AD5666_1] = {
393dbdc025bSLars-Peter Clausen 		.shared_vref = true,
394dbdc025bSLars-Peter Clausen 		.internal_vref = 2500000,
395dbdc025bSLars-Peter Clausen 		.channels = ad5064_channels,
396dbdc025bSLars-Peter Clausen 		.num_channels = 4,
397dbdc025bSLars-Peter Clausen 	},
398dbdc025bSLars-Peter Clausen 	[ID_AD5666_2] = {
399dbdc025bSLars-Peter Clausen 		.shared_vref = true,
400dbdc025bSLars-Peter Clausen 		.internal_vref = 5000000,
401dbdc025bSLars-Peter Clausen 		.channels = ad5064_channels,
402dbdc025bSLars-Peter Clausen 		.num_channels = 4,
403dbdc025bSLars-Peter Clausen 	},
404dbdc025bSLars-Peter Clausen 	[ID_AD5668_1] = {
405dbdc025bSLars-Peter Clausen 		.shared_vref = true,
406dbdc025bSLars-Peter Clausen 		.internal_vref = 2500000,
407dbdc025bSLars-Peter Clausen 		.channels = ad5064_channels,
408dbdc025bSLars-Peter Clausen 		.num_channels = 8,
409dbdc025bSLars-Peter Clausen 	},
410dbdc025bSLars-Peter Clausen 	[ID_AD5668_2] = {
411dbdc025bSLars-Peter Clausen 		.shared_vref = true,
412dbdc025bSLars-Peter Clausen 		.internal_vref = 5000000,
413dbdc025bSLars-Peter Clausen 		.channels = ad5064_channels,
414dbdc025bSLars-Peter Clausen 		.num_channels = 8,
415dbdc025bSLars-Peter Clausen 	},
416dbdc025bSLars-Peter Clausen };
417dbdc025bSLars-Peter Clausen 
418dbdc025bSLars-Peter Clausen static inline unsigned int ad5064_num_vref(struct ad5064_state *st)
419dbdc025bSLars-Peter Clausen {
420dbdc025bSLars-Peter Clausen 	return st->chip_info->shared_vref ? 1 : st->chip_info->num_channels;
421dbdc025bSLars-Peter Clausen }
422dbdc025bSLars-Peter Clausen 
423dbdc025bSLars-Peter Clausen static const char * const ad5064_vref_names[] = {
424dbdc025bSLars-Peter Clausen 	"vrefA",
425dbdc025bSLars-Peter Clausen 	"vrefB",
426dbdc025bSLars-Peter Clausen 	"vrefC",
427dbdc025bSLars-Peter Clausen 	"vrefD",
428dbdc025bSLars-Peter Clausen };
429dbdc025bSLars-Peter Clausen 
430dbdc025bSLars-Peter Clausen static const char * const ad5064_vref_name(struct ad5064_state *st,
431dbdc025bSLars-Peter Clausen 	unsigned int vref)
432dbdc025bSLars-Peter Clausen {
433dbdc025bSLars-Peter Clausen 	return st->chip_info->shared_vref ? "vref" : ad5064_vref_names[vref];
434dbdc025bSLars-Peter Clausen }
435dbdc025bSLars-Peter Clausen 
436fc52692cSGreg Kroah-Hartman static int ad5064_probe(struct device *dev, enum ad5064_type type,
4376a17a076SLars-Peter Clausen 			const char *name, ad5064_write_func write)
438dbdc025bSLars-Peter Clausen {
439dbdc025bSLars-Peter Clausen 	struct iio_dev *indio_dev;
440dbdc025bSLars-Peter Clausen 	struct ad5064_state *st;
441f77ae9d8SLars-Peter Clausen 	unsigned int midscale;
442dbdc025bSLars-Peter Clausen 	unsigned int i;
443dbdc025bSLars-Peter Clausen 	int ret;
444dbdc025bSLars-Peter Clausen 
445*c367982aSSachin Kamat 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
446dbdc025bSLars-Peter Clausen 	if (indio_dev == NULL)
447dbdc025bSLars-Peter Clausen 		return  -ENOMEM;
448dbdc025bSLars-Peter Clausen 
449dbdc025bSLars-Peter Clausen 	st = iio_priv(indio_dev);
4506a17a076SLars-Peter Clausen 	dev_set_drvdata(dev, indio_dev);
451dbdc025bSLars-Peter Clausen 
452dbdc025bSLars-Peter Clausen 	st->chip_info = &ad5064_chip_info_tbl[type];
4536a17a076SLars-Peter Clausen 	st->dev = dev;
4546a17a076SLars-Peter Clausen 	st->write = write;
455dbdc025bSLars-Peter Clausen 
456dbdc025bSLars-Peter Clausen 	for (i = 0; i < ad5064_num_vref(st); ++i)
457dbdc025bSLars-Peter Clausen 		st->vref_reg[i].supply = ad5064_vref_name(st, i);
458dbdc025bSLars-Peter Clausen 
459*c367982aSSachin Kamat 	ret = devm_regulator_bulk_get(dev, ad5064_num_vref(st),
460dbdc025bSLars-Peter Clausen 		st->vref_reg);
461dbdc025bSLars-Peter Clausen 	if (ret) {
462dbdc025bSLars-Peter Clausen 		if (!st->chip_info->internal_vref)
463*c367982aSSachin Kamat 			return ret;
464dbdc025bSLars-Peter Clausen 		st->use_internal_vref = true;
4656a17a076SLars-Peter Clausen 		ret = ad5064_write(st, AD5064_CMD_CONFIG, 0,
466dbdc025bSLars-Peter Clausen 			AD5064_CONFIG_INT_VREF_ENABLE, 0);
467dbdc025bSLars-Peter Clausen 		if (ret) {
4686a17a076SLars-Peter Clausen 			dev_err(dev, "Failed to enable internal vref: %d\n",
469dbdc025bSLars-Peter Clausen 				ret);
470*c367982aSSachin Kamat 			return ret;
471dbdc025bSLars-Peter Clausen 		}
472dbdc025bSLars-Peter Clausen 	} else {
473dbdc025bSLars-Peter Clausen 		ret = regulator_bulk_enable(ad5064_num_vref(st), st->vref_reg);
474dbdc025bSLars-Peter Clausen 		if (ret)
475*c367982aSSachin Kamat 			return ret;
476dbdc025bSLars-Peter Clausen 	}
477dbdc025bSLars-Peter Clausen 
4786a17a076SLars-Peter Clausen 	indio_dev->dev.parent = dev;
4796a17a076SLars-Peter Clausen 	indio_dev->name = name;
480dbdc025bSLars-Peter Clausen 	indio_dev->info = &ad5064_info;
481dbdc025bSLars-Peter Clausen 	indio_dev->modes = INDIO_DIRECT_MODE;
482dbdc025bSLars-Peter Clausen 	indio_dev->channels = st->chip_info->channels;
483dbdc025bSLars-Peter Clausen 	indio_dev->num_channels = st->chip_info->num_channels;
484dbdc025bSLars-Peter Clausen 
485f77ae9d8SLars-Peter Clausen 	midscale = (1 << indio_dev->channels[0].scan_type.realbits) /  2;
486f77ae9d8SLars-Peter Clausen 
487f77ae9d8SLars-Peter Clausen 	for (i = 0; i < st->chip_info->num_channels; ++i) {
488f77ae9d8SLars-Peter Clausen 		st->pwr_down_mode[i] = AD5064_LDAC_PWRDN_1K;
489f77ae9d8SLars-Peter Clausen 		st->dac_cache[i] = midscale;
490f77ae9d8SLars-Peter Clausen 	}
491f77ae9d8SLars-Peter Clausen 
492dbdc025bSLars-Peter Clausen 	ret = iio_device_register(indio_dev);
493dbdc025bSLars-Peter Clausen 	if (ret)
494dbdc025bSLars-Peter Clausen 		goto error_disable_reg;
495dbdc025bSLars-Peter Clausen 
496dbdc025bSLars-Peter Clausen 	return 0;
497dbdc025bSLars-Peter Clausen 
498dbdc025bSLars-Peter Clausen error_disable_reg:
499dbdc025bSLars-Peter Clausen 	if (!st->use_internal_vref)
500dbdc025bSLars-Peter Clausen 		regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
501dbdc025bSLars-Peter Clausen 
502dbdc025bSLars-Peter Clausen 	return ret;
503dbdc025bSLars-Peter Clausen }
504dbdc025bSLars-Peter Clausen 
505fc52692cSGreg Kroah-Hartman static int ad5064_remove(struct device *dev)
506dbdc025bSLars-Peter Clausen {
5076a17a076SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
508dbdc025bSLars-Peter Clausen 	struct ad5064_state *st = iio_priv(indio_dev);
509dbdc025bSLars-Peter Clausen 
510dbdc025bSLars-Peter Clausen 	iio_device_unregister(indio_dev);
511dbdc025bSLars-Peter Clausen 
512*c367982aSSachin Kamat 	if (!st->use_internal_vref)
513dbdc025bSLars-Peter Clausen 		regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
514dbdc025bSLars-Peter Clausen 
515dbdc025bSLars-Peter Clausen 	return 0;
516dbdc025bSLars-Peter Clausen }
517dbdc025bSLars-Peter Clausen 
5186a17a076SLars-Peter Clausen #if IS_ENABLED(CONFIG_SPI_MASTER)
5196a17a076SLars-Peter Clausen 
5209660ac70SLars-Peter Clausen static int ad5064_spi_write(struct ad5064_state *st, unsigned int cmd,
5219660ac70SLars-Peter Clausen 	unsigned int addr, unsigned int val)
5229660ac70SLars-Peter Clausen {
5239660ac70SLars-Peter Clausen 	struct spi_device *spi = to_spi_device(st->dev);
5249660ac70SLars-Peter Clausen 
5259660ac70SLars-Peter Clausen 	st->data.spi = cpu_to_be32(AD5064_CMD(cmd) | AD5064_ADDR(addr) | val);
5269660ac70SLars-Peter Clausen 	return spi_write(spi, &st->data.spi, sizeof(st->data.spi));
5279660ac70SLars-Peter Clausen }
5289660ac70SLars-Peter Clausen 
529fc52692cSGreg Kroah-Hartman static int ad5064_spi_probe(struct spi_device *spi)
5306a17a076SLars-Peter Clausen {
5316a17a076SLars-Peter Clausen 	const struct spi_device_id *id = spi_get_device_id(spi);
5326a17a076SLars-Peter Clausen 
5336a17a076SLars-Peter Clausen 	return ad5064_probe(&spi->dev, id->driver_data, id->name,
5346a17a076SLars-Peter Clausen 				ad5064_spi_write);
5356a17a076SLars-Peter Clausen }
5366a17a076SLars-Peter Clausen 
537fc52692cSGreg Kroah-Hartman static int ad5064_spi_remove(struct spi_device *spi)
5386a17a076SLars-Peter Clausen {
5396a17a076SLars-Peter Clausen 	return ad5064_remove(&spi->dev);
5406a17a076SLars-Peter Clausen }
5416a17a076SLars-Peter Clausen 
5426a17a076SLars-Peter Clausen static const struct spi_device_id ad5064_spi_ids[] = {
543dbdc025bSLars-Peter Clausen 	{"ad5024", ID_AD5024},
544dbdc025bSLars-Peter Clausen 	{"ad5025", ID_AD5025},
545dbdc025bSLars-Peter Clausen 	{"ad5044", ID_AD5044},
546dbdc025bSLars-Peter Clausen 	{"ad5045", ID_AD5045},
547dbdc025bSLars-Peter Clausen 	{"ad5064", ID_AD5064},
548dbdc025bSLars-Peter Clausen 	{"ad5064-1", ID_AD5064_1},
549dbdc025bSLars-Peter Clausen 	{"ad5065", ID_AD5065},
550dbdc025bSLars-Peter Clausen 	{"ad5628-1", ID_AD5628_1},
551dbdc025bSLars-Peter Clausen 	{"ad5628-2", ID_AD5628_2},
552dbdc025bSLars-Peter Clausen 	{"ad5648-1", ID_AD5648_1},
553dbdc025bSLars-Peter Clausen 	{"ad5648-2", ID_AD5648_2},
554dbdc025bSLars-Peter Clausen 	{"ad5666-1", ID_AD5666_1},
555dbdc025bSLars-Peter Clausen 	{"ad5666-2", ID_AD5666_2},
556dbdc025bSLars-Peter Clausen 	{"ad5668-1", ID_AD5668_1},
557dbdc025bSLars-Peter Clausen 	{"ad5668-2", ID_AD5668_2},
558dbdc025bSLars-Peter Clausen 	{"ad5668-3", ID_AD5668_2}, /* similar enough to ad5668-2 */
559dbdc025bSLars-Peter Clausen 	{}
560dbdc025bSLars-Peter Clausen };
5616a17a076SLars-Peter Clausen MODULE_DEVICE_TABLE(spi, ad5064_spi_ids);
562dbdc025bSLars-Peter Clausen 
5636a17a076SLars-Peter Clausen static struct spi_driver ad5064_spi_driver = {
564dbdc025bSLars-Peter Clausen 	.driver = {
565dbdc025bSLars-Peter Clausen 		   .name = "ad5064",
566dbdc025bSLars-Peter Clausen 		   .owner = THIS_MODULE,
567dbdc025bSLars-Peter Clausen 	},
5686a17a076SLars-Peter Clausen 	.probe = ad5064_spi_probe,
569fc52692cSGreg Kroah-Hartman 	.remove = ad5064_spi_remove,
5706a17a076SLars-Peter Clausen 	.id_table = ad5064_spi_ids,
571dbdc025bSLars-Peter Clausen };
5726a17a076SLars-Peter Clausen 
5736a17a076SLars-Peter Clausen static int __init ad5064_spi_register_driver(void)
5746a17a076SLars-Peter Clausen {
5756a17a076SLars-Peter Clausen 	return spi_register_driver(&ad5064_spi_driver);
5766a17a076SLars-Peter Clausen }
5776a17a076SLars-Peter Clausen 
57821fa54e4SGerard Snitselaar static void ad5064_spi_unregister_driver(void)
5796a17a076SLars-Peter Clausen {
5806a17a076SLars-Peter Clausen 	spi_unregister_driver(&ad5064_spi_driver);
5816a17a076SLars-Peter Clausen }
5826a17a076SLars-Peter Clausen 
5836a17a076SLars-Peter Clausen #else
5846a17a076SLars-Peter Clausen 
5856a17a076SLars-Peter Clausen static inline int ad5064_spi_register_driver(void) { return 0; }
5866a17a076SLars-Peter Clausen static inline void ad5064_spi_unregister_driver(void) { }
5876a17a076SLars-Peter Clausen 
5886a17a076SLars-Peter Clausen #endif
5896a17a076SLars-Peter Clausen 
5906a17a076SLars-Peter Clausen #if IS_ENABLED(CONFIG_I2C)
5916a17a076SLars-Peter Clausen 
5929660ac70SLars-Peter Clausen static int ad5064_i2c_write(struct ad5064_state *st, unsigned int cmd,
5939660ac70SLars-Peter Clausen 	unsigned int addr, unsigned int val)
5949660ac70SLars-Peter Clausen {
5959660ac70SLars-Peter Clausen 	struct i2c_client *i2c = to_i2c_client(st->dev);
5969660ac70SLars-Peter Clausen 
5979660ac70SLars-Peter Clausen 	st->data.i2c[0] = (cmd << 4) | addr;
5989660ac70SLars-Peter Clausen 	put_unaligned_be16(val, &st->data.i2c[1]);
5999660ac70SLars-Peter Clausen 	return i2c_master_send(i2c, st->data.i2c, 3);
6009660ac70SLars-Peter Clausen }
6019660ac70SLars-Peter Clausen 
602fc52692cSGreg Kroah-Hartman static int ad5064_i2c_probe(struct i2c_client *i2c,
6036a17a076SLars-Peter Clausen 	const struct i2c_device_id *id)
6046a17a076SLars-Peter Clausen {
6056a17a076SLars-Peter Clausen 	return ad5064_probe(&i2c->dev, id->driver_data, id->name,
6066a17a076SLars-Peter Clausen 						ad5064_i2c_write);
6076a17a076SLars-Peter Clausen }
6086a17a076SLars-Peter Clausen 
609fc52692cSGreg Kroah-Hartman static int ad5064_i2c_remove(struct i2c_client *i2c)
6106a17a076SLars-Peter Clausen {
6116a17a076SLars-Peter Clausen 	return ad5064_remove(&i2c->dev);
6126a17a076SLars-Peter Clausen }
6136a17a076SLars-Peter Clausen 
6146a17a076SLars-Peter Clausen static const struct i2c_device_id ad5064_i2c_ids[] = {
6156a17a076SLars-Peter Clausen 	{"ad5629-1", ID_AD5628_1},
6166a17a076SLars-Peter Clausen 	{"ad5629-2", ID_AD5628_2},
6176a17a076SLars-Peter Clausen 	{"ad5629-3", ID_AD5628_2}, /* similar enough to ad5629-2 */
6186a17a076SLars-Peter Clausen 	{"ad5669-1", ID_AD5668_1},
6196a17a076SLars-Peter Clausen 	{"ad5669-2", ID_AD5668_2},
6206a17a076SLars-Peter Clausen 	{"ad5669-3", ID_AD5668_2}, /* similar enough to ad5669-2 */
6216a17a076SLars-Peter Clausen 	{}
6226a17a076SLars-Peter Clausen };
6236a17a076SLars-Peter Clausen MODULE_DEVICE_TABLE(i2c, ad5064_i2c_ids);
6246a17a076SLars-Peter Clausen 
6256a17a076SLars-Peter Clausen static struct i2c_driver ad5064_i2c_driver = {
6266a17a076SLars-Peter Clausen 	.driver = {
6276a17a076SLars-Peter Clausen 		   .name = "ad5064",
6286a17a076SLars-Peter Clausen 		   .owner = THIS_MODULE,
6296a17a076SLars-Peter Clausen 	},
6306a17a076SLars-Peter Clausen 	.probe = ad5064_i2c_probe,
631fc52692cSGreg Kroah-Hartman 	.remove = ad5064_i2c_remove,
6326a17a076SLars-Peter Clausen 	.id_table = ad5064_i2c_ids,
6336a17a076SLars-Peter Clausen };
6346a17a076SLars-Peter Clausen 
6356a17a076SLars-Peter Clausen static int __init ad5064_i2c_register_driver(void)
6366a17a076SLars-Peter Clausen {
6376a17a076SLars-Peter Clausen 	return i2c_add_driver(&ad5064_i2c_driver);
6386a17a076SLars-Peter Clausen }
6396a17a076SLars-Peter Clausen 
6406a17a076SLars-Peter Clausen static void __exit ad5064_i2c_unregister_driver(void)
6416a17a076SLars-Peter Clausen {
6426a17a076SLars-Peter Clausen 	i2c_del_driver(&ad5064_i2c_driver);
6436a17a076SLars-Peter Clausen }
6446a17a076SLars-Peter Clausen 
6456a17a076SLars-Peter Clausen #else
6466a17a076SLars-Peter Clausen 
6476a17a076SLars-Peter Clausen static inline int ad5064_i2c_register_driver(void) { return 0; }
6486a17a076SLars-Peter Clausen static inline void ad5064_i2c_unregister_driver(void) { }
6496a17a076SLars-Peter Clausen 
6506a17a076SLars-Peter Clausen #endif
6516a17a076SLars-Peter Clausen 
6526a17a076SLars-Peter Clausen static int __init ad5064_init(void)
6536a17a076SLars-Peter Clausen {
6546a17a076SLars-Peter Clausen 	int ret;
6556a17a076SLars-Peter Clausen 
6566a17a076SLars-Peter Clausen 	ret = ad5064_spi_register_driver();
6576a17a076SLars-Peter Clausen 	if (ret)
6586a17a076SLars-Peter Clausen 		return ret;
6596a17a076SLars-Peter Clausen 
6606a17a076SLars-Peter Clausen 	ret = ad5064_i2c_register_driver();
6616a17a076SLars-Peter Clausen 	if (ret) {
6626a17a076SLars-Peter Clausen 		ad5064_spi_unregister_driver();
6636a17a076SLars-Peter Clausen 		return ret;
6646a17a076SLars-Peter Clausen 	}
6656a17a076SLars-Peter Clausen 
6666a17a076SLars-Peter Clausen 	return 0;
6676a17a076SLars-Peter Clausen }
6686a17a076SLars-Peter Clausen module_init(ad5064_init);
6696a17a076SLars-Peter Clausen 
6706a17a076SLars-Peter Clausen static void __exit ad5064_exit(void)
6716a17a076SLars-Peter Clausen {
6726a17a076SLars-Peter Clausen 	ad5064_i2c_unregister_driver();
6736a17a076SLars-Peter Clausen 	ad5064_spi_unregister_driver();
6746a17a076SLars-Peter Clausen }
6756a17a076SLars-Peter Clausen module_exit(ad5064_exit);
676dbdc025bSLars-Peter Clausen 
677dbdc025bSLars-Peter Clausen MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
6786a17a076SLars-Peter Clausen MODULE_DESCRIPTION("Analog Devices AD5024 and similar multi-channel DACs");
679dbdc025bSLars-Peter Clausen MODULE_LICENSE("GPL v2");
680