1/*
2 * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
3 *
4 * Copyright 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
9#include <linux/interrupt.h>
10#include <linux/device.h>
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/sysfs.h>
14#include <linux/spi/spi.h>
15#include <linux/regulator/consumer.h>
16#include <linux/err.h>
17#include <linux/sched.h>
18#include <linux/gpio.h>
19#include <linux/module.h>
20
21#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
23#include <linux/iio/adc/ad_sigma_delta.h>
24
25#include "ad7780.h"
26
27#define AD7780_RDY	BIT(7)
28#define AD7780_FILTER	BIT(6)
29#define AD7780_ERR	BIT(5)
30#define AD7780_ID1	BIT(4)
31#define AD7780_ID0	BIT(3)
32#define AD7780_GAIN	BIT(2)
33#define AD7780_PAT1	BIT(1)
34#define AD7780_PAT0	BIT(0)
35
36struct ad7780_chip_info {
37	struct iio_chan_spec	channel;
38	unsigned int		pattern_mask;
39	unsigned int		pattern;
40};
41
42struct ad7780_state {
43	const struct ad7780_chip_info	*chip_info;
44	struct regulator		*reg;
45	int				powerdown_gpio;
46	unsigned int	gain;
47	u16				int_vref_mv;
48
49	struct ad_sigma_delta sd;
50};
51
52enum ad7780_supported_device_ids {
53	ID_AD7170,
54	ID_AD7171,
55	ID_AD7780,
56	ID_AD7781,
57};
58
59static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
60{
61	return container_of(sd, struct ad7780_state, sd);
62}
63
64static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
65	enum ad_sigma_delta_mode mode)
66{
67	struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
68	unsigned val;
69
70	switch (mode) {
71	case AD_SD_MODE_SINGLE:
72	case AD_SD_MODE_CONTINUOUS:
73		val = 1;
74		break;
75	default:
76		val = 0;
77		break;
78	}
79
80	if (gpio_is_valid(st->powerdown_gpio))
81		gpio_set_value(st->powerdown_gpio, val);
82
83	return 0;
84}
85
86static int ad7780_read_raw(struct iio_dev *indio_dev,
87			   struct iio_chan_spec const *chan,
88			   int *val,
89			   int *val2,
90			   long m)
91{
92	struct ad7780_state *st = iio_priv(indio_dev);
93
94	switch (m) {
95	case IIO_CHAN_INFO_RAW:
96		return ad_sigma_delta_single_conversion(indio_dev, chan, val);
97	case IIO_CHAN_INFO_SCALE:
98		*val = st->int_vref_mv * st->gain;
99		*val2 = chan->scan_type.realbits - 1;
100		return IIO_VAL_FRACTIONAL_LOG2;
101	case IIO_CHAN_INFO_OFFSET:
102		*val -= (1 << (chan->scan_type.realbits - 1));
103		return IIO_VAL_INT;
104	}
105
106	return -EINVAL;
107}
108
109static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
110	unsigned int raw_sample)
111{
112	struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
113	const struct ad7780_chip_info *chip_info = st->chip_info;
114
115	if ((raw_sample & AD7780_ERR) ||
116		((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
117		return -EIO;
118
119	if (raw_sample & AD7780_GAIN)
120		st->gain = 1;
121	else
122		st->gain = 128;
123
124	return 0;
125}
126
127static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
128	.set_mode = ad7780_set_mode,
129	.postprocess_sample = ad7780_postprocess_sample,
130	.has_registers = false,
131};
132
133#define AD7780_CHANNEL(bits, wordsize) \
134	AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
135
136static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
137	[ID_AD7170] = {
138		.channel = AD7780_CHANNEL(12, 24),
139		.pattern = 0x5,
140		.pattern_mask = 0x7,
141	},
142	[ID_AD7171] = {
143		.channel = AD7780_CHANNEL(16, 24),
144		.pattern = 0x5,
145		.pattern_mask = 0x7,
146	},
147	[ID_AD7780] = {
148		.channel = AD7780_CHANNEL(24, 32),
149		.pattern = 0x1,
150		.pattern_mask = 0x3,
151	},
152	[ID_AD7781] = {
153		.channel = AD7780_CHANNEL(20, 32),
154		.pattern = 0x1,
155		.pattern_mask = 0x3,
156	},
157};
158
159static const struct iio_info ad7780_info = {
160	.read_raw = &ad7780_read_raw,
161	.driver_module = THIS_MODULE,
162};
163
164static int ad7780_probe(struct spi_device *spi)
165{
166	struct ad7780_platform_data *pdata = spi->dev.platform_data;
167	struct ad7780_state *st;
168	struct iio_dev *indio_dev;
169	int ret, voltage_uv = 0;
170
171	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
172	if (!indio_dev)
173		return -ENOMEM;
174
175	st = iio_priv(indio_dev);
176	st->gain = 1;
177
178	ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
179
180	st->reg = devm_regulator_get(&spi->dev, "vcc");
181	if (!IS_ERR(st->reg)) {
182		ret = regulator_enable(st->reg);
183		if (ret)
184			return ret;
185
186		voltage_uv = regulator_get_voltage(st->reg);
187	}
188
189	st->chip_info =
190		&ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
191
192	if (pdata && pdata->vref_mv)
193		st->int_vref_mv = pdata->vref_mv;
194	else if (voltage_uv)
195		st->int_vref_mv = voltage_uv / 1000;
196	else
197		dev_warn(&spi->dev, "reference voltage unspecified\n");
198
199	spi_set_drvdata(spi, indio_dev);
200
201	indio_dev->dev.parent = &spi->dev;
202	indio_dev->name = spi_get_device_id(spi)->name;
203	indio_dev->modes = INDIO_DIRECT_MODE;
204	indio_dev->channels = &st->chip_info->channel;
205	indio_dev->num_channels = 1;
206	indio_dev->info = &ad7780_info;
207
208	if (pdata && gpio_is_valid(pdata->gpio_pdrst)) {
209
210		ret = devm_gpio_request_one(&spi->dev, pdata->gpio_pdrst,
211					GPIOF_OUT_INIT_LOW, "AD7780 /PDRST");
212		if (ret) {
213			dev_err(&spi->dev, "failed to request GPIO PDRST\n");
214			goto error_disable_reg;
215		}
216		st->powerdown_gpio = pdata->gpio_pdrst;
217	} else {
218		st->powerdown_gpio = -1;
219	}
220
221	ret = ad_sd_setup_buffer_and_trigger(indio_dev);
222	if (ret)
223		goto error_disable_reg;
224
225	ret = iio_device_register(indio_dev);
226	if (ret)
227		goto error_cleanup_buffer_and_trigger;
228
229	return 0;
230
231error_cleanup_buffer_and_trigger:
232	ad_sd_cleanup_buffer_and_trigger(indio_dev);
233error_disable_reg:
234	if (!IS_ERR(st->reg))
235		regulator_disable(st->reg);
236
237	return ret;
238}
239
240static int ad7780_remove(struct spi_device *spi)
241{
242	struct iio_dev *indio_dev = spi_get_drvdata(spi);
243	struct ad7780_state *st = iio_priv(indio_dev);
244
245	iio_device_unregister(indio_dev);
246	ad_sd_cleanup_buffer_and_trigger(indio_dev);
247
248	if (!IS_ERR(st->reg))
249		regulator_disable(st->reg);
250
251	return 0;
252}
253
254static const struct spi_device_id ad7780_id[] = {
255	{"ad7170", ID_AD7170},
256	{"ad7171", ID_AD7171},
257	{"ad7780", ID_AD7780},
258	{"ad7781", ID_AD7781},
259	{}
260};
261MODULE_DEVICE_TABLE(spi, ad7780_id);
262
263static struct spi_driver ad7780_driver = {
264	.driver = {
265		.name	= "ad7780",
266		.owner	= THIS_MODULE,
267	},
268	.probe		= ad7780_probe,
269	.remove		= ad7780_remove,
270	.id_table	= ad7780_id,
271};
272module_spi_driver(ad7780_driver);
273
274MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
275MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
276MODULE_LICENSE("GPL v2");
277