xref: /openbmc/linux/drivers/iio/adc/ti-ads8688.c (revision 2a86487786b5c9ce7bee21efc9ce196bb7fe0aed)
13e87e783SSean Nyekjaer /*
23e87e783SSean Nyekjaer  * Copyright (C) 2015 Prevas A/S
33e87e783SSean Nyekjaer  *
43e87e783SSean Nyekjaer  * This program is free software; you can redistribute it and/or modify
53e87e783SSean Nyekjaer  * it under the terms of the GNU General Public License version 2 as
63e87e783SSean Nyekjaer  * published by the Free Software Foundation.
73e87e783SSean Nyekjaer  */
83e87e783SSean Nyekjaer 
93e87e783SSean Nyekjaer #include <linux/device.h>
103e87e783SSean Nyekjaer #include <linux/kernel.h>
113e87e783SSean Nyekjaer #include <linux/slab.h>
123e87e783SSean Nyekjaer #include <linux/sysfs.h>
133e87e783SSean Nyekjaer #include <linux/spi/spi.h>
143e87e783SSean Nyekjaer #include <linux/regulator/consumer.h>
153e87e783SSean Nyekjaer #include <linux/err.h>
163e87e783SSean Nyekjaer #include <linux/module.h>
173e87e783SSean Nyekjaer #include <linux/of.h>
183e87e783SSean Nyekjaer 
193e87e783SSean Nyekjaer #include <linux/iio/iio.h>
20*2a864877SSean Nyekjaer #include <linux/iio/buffer.h>
21*2a864877SSean Nyekjaer #include <linux/iio/trigger_consumer.h>
22*2a864877SSean Nyekjaer #include <linux/iio/triggered_buffer.h>
233e87e783SSean Nyekjaer #include <linux/iio/sysfs.h>
243e87e783SSean Nyekjaer 
253e87e783SSean Nyekjaer #define ADS8688_CMD_REG(x)		(x << 8)
263e87e783SSean Nyekjaer #define ADS8688_CMD_REG_NOOP		0x00
273e87e783SSean Nyekjaer #define ADS8688_CMD_REG_RST		0x85
283e87e783SSean Nyekjaer #define ADS8688_CMD_REG_MAN_CH(chan)	(0xC0 | (4 * chan))
293e87e783SSean Nyekjaer #define ADS8688_CMD_DONT_CARE_BITS	16
303e87e783SSean Nyekjaer 
313e87e783SSean Nyekjaer #define ADS8688_PROG_REG(x)		(x << 9)
323e87e783SSean Nyekjaer #define ADS8688_PROG_REG_RANGE_CH(chan)	(0x05 + chan)
333e87e783SSean Nyekjaer #define ADS8688_PROG_WR_BIT		BIT(8)
343e87e783SSean Nyekjaer #define ADS8688_PROG_DONT_CARE_BITS	8
353e87e783SSean Nyekjaer 
363e87e783SSean Nyekjaer #define ADS8688_REG_PLUSMINUS25VREF	0
373e87e783SSean Nyekjaer #define ADS8688_REG_PLUSMINUS125VREF	1
383e87e783SSean Nyekjaer #define ADS8688_REG_PLUSMINUS0625VREF	2
393e87e783SSean Nyekjaer #define ADS8688_REG_PLUS25VREF		5
403e87e783SSean Nyekjaer #define ADS8688_REG_PLUS125VREF		6
413e87e783SSean Nyekjaer 
423e87e783SSean Nyekjaer #define ADS8688_VREF_MV			4096
433e87e783SSean Nyekjaer #define ADS8688_REALBITS		16
443e87e783SSean Nyekjaer 
453e87e783SSean Nyekjaer /*
463e87e783SSean Nyekjaer  * enum ads8688_range - ADS8688 reference voltage range
473e87e783SSean Nyekjaer  * @ADS8688_PLUSMINUS25VREF: Device is configured for input range ±2.5 * VREF
483e87e783SSean Nyekjaer  * @ADS8688_PLUSMINUS125VREF: Device is configured for input range ±1.25 * VREF
493e87e783SSean Nyekjaer  * @ADS8688_PLUSMINUS0625VREF: Device is configured for input range ±0.625 * VREF
503e87e783SSean Nyekjaer  * @ADS8688_PLUS25VREF: Device is configured for input range 0 - 2.5 * VREF
513e87e783SSean Nyekjaer  * @ADS8688_PLUS125VREF: Device is configured for input range 0 - 1.25 * VREF
523e87e783SSean Nyekjaer  */
533e87e783SSean Nyekjaer enum ads8688_range {
543e87e783SSean Nyekjaer 	ADS8688_PLUSMINUS25VREF,
553e87e783SSean Nyekjaer 	ADS8688_PLUSMINUS125VREF,
563e87e783SSean Nyekjaer 	ADS8688_PLUSMINUS0625VREF,
573e87e783SSean Nyekjaer 	ADS8688_PLUS25VREF,
583e87e783SSean Nyekjaer 	ADS8688_PLUS125VREF,
593e87e783SSean Nyekjaer };
603e87e783SSean Nyekjaer 
613e87e783SSean Nyekjaer struct ads8688_chip_info {
623e87e783SSean Nyekjaer 	const struct iio_chan_spec *channels;
633e87e783SSean Nyekjaer 	unsigned int num_channels;
643e87e783SSean Nyekjaer };
653e87e783SSean Nyekjaer 
663e87e783SSean Nyekjaer struct ads8688_state {
673e87e783SSean Nyekjaer 	struct mutex			lock;
683e87e783SSean Nyekjaer 	const struct ads8688_chip_info	*chip_info;
693e87e783SSean Nyekjaer 	struct spi_device		*spi;
703e87e783SSean Nyekjaer 	struct regulator		*reg;
713e87e783SSean Nyekjaer 	unsigned int			vref_mv;
723e87e783SSean Nyekjaer 	enum ads8688_range		range[8];
733e87e783SSean Nyekjaer 	union {
743e87e783SSean Nyekjaer 		__be32 d32;
753e87e783SSean Nyekjaer 		u8 d8[4];
763e87e783SSean Nyekjaer 	} data[2] ____cacheline_aligned;
773e87e783SSean Nyekjaer };
783e87e783SSean Nyekjaer 
793e87e783SSean Nyekjaer enum ads8688_id {
803e87e783SSean Nyekjaer 	ID_ADS8684,
813e87e783SSean Nyekjaer 	ID_ADS8688,
823e87e783SSean Nyekjaer };
833e87e783SSean Nyekjaer 
843e87e783SSean Nyekjaer struct ads8688_ranges {
853e87e783SSean Nyekjaer 	enum ads8688_range range;
863e87e783SSean Nyekjaer 	unsigned int scale;
873e87e783SSean Nyekjaer 	int offset;
883e87e783SSean Nyekjaer 	u8 reg;
893e87e783SSean Nyekjaer };
903e87e783SSean Nyekjaer 
913e87e783SSean Nyekjaer static const struct ads8688_ranges ads8688_range_def[5] = {
923e87e783SSean Nyekjaer 	{
933e87e783SSean Nyekjaer 		.range = ADS8688_PLUSMINUS25VREF,
943e87e783SSean Nyekjaer 		.scale = 76295,
953e87e783SSean Nyekjaer 		.offset = -(1 << (ADS8688_REALBITS - 1)),
963e87e783SSean Nyekjaer 		.reg = ADS8688_REG_PLUSMINUS25VREF,
973e87e783SSean Nyekjaer 	}, {
983e87e783SSean Nyekjaer 		.range = ADS8688_PLUSMINUS125VREF,
993e87e783SSean Nyekjaer 		.scale = 38148,
1003e87e783SSean Nyekjaer 		.offset = -(1 << (ADS8688_REALBITS - 1)),
1013e87e783SSean Nyekjaer 		.reg = ADS8688_REG_PLUSMINUS125VREF,
1023e87e783SSean Nyekjaer 	}, {
1033e87e783SSean Nyekjaer 		.range = ADS8688_PLUSMINUS0625VREF,
1043e87e783SSean Nyekjaer 		.scale = 19074,
1053e87e783SSean Nyekjaer 		.offset = -(1 << (ADS8688_REALBITS - 1)),
1063e87e783SSean Nyekjaer 		.reg = ADS8688_REG_PLUSMINUS0625VREF,
1073e87e783SSean Nyekjaer 	}, {
1083e87e783SSean Nyekjaer 		.range = ADS8688_PLUS25VREF,
1093e87e783SSean Nyekjaer 		.scale = 38148,
1103e87e783SSean Nyekjaer 		.offset = 0,
1113e87e783SSean Nyekjaer 		.reg = ADS8688_REG_PLUS25VREF,
1123e87e783SSean Nyekjaer 	}, {
1133e87e783SSean Nyekjaer 		.range = ADS8688_PLUS125VREF,
1143e87e783SSean Nyekjaer 		.scale = 19074,
1153e87e783SSean Nyekjaer 		.offset = 0,
1163e87e783SSean Nyekjaer 		.reg = ADS8688_REG_PLUS125VREF,
1173e87e783SSean Nyekjaer 	}
1183e87e783SSean Nyekjaer };
1193e87e783SSean Nyekjaer 
1203e87e783SSean Nyekjaer static ssize_t ads8688_show_scales(struct device *dev,
1213e87e783SSean Nyekjaer 				   struct device_attribute *attr, char *buf)
1223e87e783SSean Nyekjaer {
1233e87e783SSean Nyekjaer 	struct ads8688_state *st = iio_priv(dev_to_iio_dev(dev));
1243e87e783SSean Nyekjaer 
1253e87e783SSean Nyekjaer 	return sprintf(buf, "0.%09u 0.%09u 0.%09u\n",
1263e87e783SSean Nyekjaer 		       ads8688_range_def[0].scale * st->vref_mv,
1273e87e783SSean Nyekjaer 		       ads8688_range_def[1].scale * st->vref_mv,
1283e87e783SSean Nyekjaer 		       ads8688_range_def[2].scale * st->vref_mv);
1293e87e783SSean Nyekjaer }
1303e87e783SSean Nyekjaer 
1313e87e783SSean Nyekjaer static ssize_t ads8688_show_offsets(struct device *dev,
1323e87e783SSean Nyekjaer 				    struct device_attribute *attr, char *buf)
1333e87e783SSean Nyekjaer {
1343e87e783SSean Nyekjaer 	return sprintf(buf, "%d %d\n", ads8688_range_def[0].offset,
1353e87e783SSean Nyekjaer 		       ads8688_range_def[3].offset);
1363e87e783SSean Nyekjaer }
1373e87e783SSean Nyekjaer 
1383e87e783SSean Nyekjaer static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO,
1393e87e783SSean Nyekjaer 		       ads8688_show_scales, NULL, 0);
1403e87e783SSean Nyekjaer static IIO_DEVICE_ATTR(in_voltage_offset_available, S_IRUGO,
1413e87e783SSean Nyekjaer 		       ads8688_show_offsets, NULL, 0);
1423e87e783SSean Nyekjaer 
1433e87e783SSean Nyekjaer static struct attribute *ads8688_attributes[] = {
1443e87e783SSean Nyekjaer 	&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
1453e87e783SSean Nyekjaer 	&iio_dev_attr_in_voltage_offset_available.dev_attr.attr,
1463e87e783SSean Nyekjaer 	NULL,
1473e87e783SSean Nyekjaer };
1483e87e783SSean Nyekjaer 
1493e87e783SSean Nyekjaer static const struct attribute_group ads8688_attribute_group = {
1503e87e783SSean Nyekjaer 	.attrs = ads8688_attributes,
1513e87e783SSean Nyekjaer };
1523e87e783SSean Nyekjaer 
1533e87e783SSean Nyekjaer #define ADS8688_CHAN(index)					\
1543e87e783SSean Nyekjaer {								\
1553e87e783SSean Nyekjaer 	.type = IIO_VOLTAGE,					\
1563e87e783SSean Nyekjaer 	.indexed = 1,						\
1573e87e783SSean Nyekjaer 	.channel = index,					\
1583e87e783SSean Nyekjaer 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW)		\
1593e87e783SSean Nyekjaer 			      | BIT(IIO_CHAN_INFO_SCALE)	\
1603e87e783SSean Nyekjaer 			      | BIT(IIO_CHAN_INFO_OFFSET),	\
161*2a864877SSean Nyekjaer 	.scan_index = index,					\
162*2a864877SSean Nyekjaer 	.scan_type = {						\
163*2a864877SSean Nyekjaer 		.sign = 'u',					\
164*2a864877SSean Nyekjaer 		.realbits = 16,					\
165*2a864877SSean Nyekjaer 		.storagebits = 16,				\
166*2a864877SSean Nyekjaer 		.endianness = IIO_BE,				\
167*2a864877SSean Nyekjaer 	},							\
1683e87e783SSean Nyekjaer }
1693e87e783SSean Nyekjaer 
1703e87e783SSean Nyekjaer static const struct iio_chan_spec ads8684_channels[] = {
1713e87e783SSean Nyekjaer 	ADS8688_CHAN(0),
1723e87e783SSean Nyekjaer 	ADS8688_CHAN(1),
1733e87e783SSean Nyekjaer 	ADS8688_CHAN(2),
1743e87e783SSean Nyekjaer 	ADS8688_CHAN(3),
1753e87e783SSean Nyekjaer };
1763e87e783SSean Nyekjaer 
1773e87e783SSean Nyekjaer static const struct iio_chan_spec ads8688_channels[] = {
1783e87e783SSean Nyekjaer 	ADS8688_CHAN(0),
1793e87e783SSean Nyekjaer 	ADS8688_CHAN(1),
1803e87e783SSean Nyekjaer 	ADS8688_CHAN(2),
1813e87e783SSean Nyekjaer 	ADS8688_CHAN(3),
1823e87e783SSean Nyekjaer 	ADS8688_CHAN(4),
1833e87e783SSean Nyekjaer 	ADS8688_CHAN(5),
1843e87e783SSean Nyekjaer 	ADS8688_CHAN(6),
1853e87e783SSean Nyekjaer 	ADS8688_CHAN(7),
1863e87e783SSean Nyekjaer };
1873e87e783SSean Nyekjaer 
1883e87e783SSean Nyekjaer static int ads8688_prog_write(struct iio_dev *indio_dev, unsigned int addr,
1893e87e783SSean Nyekjaer 			      unsigned int val)
1903e87e783SSean Nyekjaer {
1913e87e783SSean Nyekjaer 	struct ads8688_state *st = iio_priv(indio_dev);
1923e87e783SSean Nyekjaer 	u32 tmp;
1933e87e783SSean Nyekjaer 
1943e87e783SSean Nyekjaer 	tmp = ADS8688_PROG_REG(addr) | ADS8688_PROG_WR_BIT | val;
1953e87e783SSean Nyekjaer 	tmp <<= ADS8688_PROG_DONT_CARE_BITS;
1963e87e783SSean Nyekjaer 	st->data[0].d32 = cpu_to_be32(tmp);
1973e87e783SSean Nyekjaer 
1983e87e783SSean Nyekjaer 	return spi_write(st->spi, &st->data[0].d8[1], 3);
1993e87e783SSean Nyekjaer }
2003e87e783SSean Nyekjaer 
2013e87e783SSean Nyekjaer static int ads8688_reset(struct iio_dev *indio_dev)
2023e87e783SSean Nyekjaer {
2033e87e783SSean Nyekjaer 	struct ads8688_state *st = iio_priv(indio_dev);
2043e87e783SSean Nyekjaer 	u32 tmp;
2053e87e783SSean Nyekjaer 
2063e87e783SSean Nyekjaer 	tmp = ADS8688_CMD_REG(ADS8688_CMD_REG_RST);
2073e87e783SSean Nyekjaer 	tmp <<= ADS8688_CMD_DONT_CARE_BITS;
2083e87e783SSean Nyekjaer 	st->data[0].d32 = cpu_to_be32(tmp);
2093e87e783SSean Nyekjaer 
2103e87e783SSean Nyekjaer 	return spi_write(st->spi, &st->data[0].d8[0], 4);
2113e87e783SSean Nyekjaer }
2123e87e783SSean Nyekjaer 
2133e87e783SSean Nyekjaer static int ads8688_read(struct iio_dev *indio_dev, unsigned int chan)
2143e87e783SSean Nyekjaer {
2153e87e783SSean Nyekjaer 	struct ads8688_state *st = iio_priv(indio_dev);
2163e87e783SSean Nyekjaer 	int ret;
2173e87e783SSean Nyekjaer 	u32 tmp;
2183e87e783SSean Nyekjaer 	struct spi_transfer t[] = {
2193e87e783SSean Nyekjaer 		{
2203e87e783SSean Nyekjaer 			.tx_buf = &st->data[0].d8[0],
2213e87e783SSean Nyekjaer 			.len = 4,
2223e87e783SSean Nyekjaer 			.cs_change = 1,
2233e87e783SSean Nyekjaer 		}, {
2243e87e783SSean Nyekjaer 			.tx_buf = &st->data[1].d8[0],
2253e87e783SSean Nyekjaer 			.rx_buf = &st->data[1].d8[0],
2263e87e783SSean Nyekjaer 			.len = 4,
2273e87e783SSean Nyekjaer 		},
2283e87e783SSean Nyekjaer 	};
2293e87e783SSean Nyekjaer 
2303e87e783SSean Nyekjaer 	tmp = ADS8688_CMD_REG(ADS8688_CMD_REG_MAN_CH(chan));
2313e87e783SSean Nyekjaer 	tmp <<= ADS8688_CMD_DONT_CARE_BITS;
2323e87e783SSean Nyekjaer 	st->data[0].d32 = cpu_to_be32(tmp);
2333e87e783SSean Nyekjaer 
2343e87e783SSean Nyekjaer 	tmp = ADS8688_CMD_REG(ADS8688_CMD_REG_NOOP);
2353e87e783SSean Nyekjaer 	tmp <<= ADS8688_CMD_DONT_CARE_BITS;
2363e87e783SSean Nyekjaer 	st->data[1].d32 = cpu_to_be32(tmp);
2373e87e783SSean Nyekjaer 
2383e87e783SSean Nyekjaer 	ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
2393e87e783SSean Nyekjaer 	if (ret < 0)
2403e87e783SSean Nyekjaer 		return ret;
2413e87e783SSean Nyekjaer 
2423e87e783SSean Nyekjaer 	return be32_to_cpu(st->data[1].d32) & 0xffff;
2433e87e783SSean Nyekjaer }
2443e87e783SSean Nyekjaer 
2453e87e783SSean Nyekjaer static int ads8688_read_raw(struct iio_dev *indio_dev,
2463e87e783SSean Nyekjaer 			    struct iio_chan_spec const *chan,
2473e87e783SSean Nyekjaer 			    int *val, int *val2, long m)
2483e87e783SSean Nyekjaer {
2493e87e783SSean Nyekjaer 	int ret, offset;
2503e87e783SSean Nyekjaer 	unsigned long scale_mv;
2513e87e783SSean Nyekjaer 
2523e87e783SSean Nyekjaer 	struct ads8688_state *st = iio_priv(indio_dev);
2533e87e783SSean Nyekjaer 
2543e87e783SSean Nyekjaer 	mutex_lock(&st->lock);
2553e87e783SSean Nyekjaer 	switch (m) {
2563e87e783SSean Nyekjaer 	case IIO_CHAN_INFO_RAW:
2573e87e783SSean Nyekjaer 		ret = ads8688_read(indio_dev, chan->channel);
2583e87e783SSean Nyekjaer 		mutex_unlock(&st->lock);
2593e87e783SSean Nyekjaer 		if (ret < 0)
2603e87e783SSean Nyekjaer 			return ret;
2613e87e783SSean Nyekjaer 		*val = ret;
2623e87e783SSean Nyekjaer 		return IIO_VAL_INT;
2633e87e783SSean Nyekjaer 	case IIO_CHAN_INFO_SCALE:
2643e87e783SSean Nyekjaer 		scale_mv = st->vref_mv;
2653e87e783SSean Nyekjaer 		scale_mv *= ads8688_range_def[st->range[chan->channel]].scale;
2663e87e783SSean Nyekjaer 		*val = 0;
2673e87e783SSean Nyekjaer 		*val2 = scale_mv;
2683e87e783SSean Nyekjaer 		mutex_unlock(&st->lock);
2693e87e783SSean Nyekjaer 		return IIO_VAL_INT_PLUS_NANO;
2703e87e783SSean Nyekjaer 	case IIO_CHAN_INFO_OFFSET:
2713e87e783SSean Nyekjaer 		offset = ads8688_range_def[st->range[chan->channel]].offset;
2723e87e783SSean Nyekjaer 		*val = offset;
2733e87e783SSean Nyekjaer 		mutex_unlock(&st->lock);
2743e87e783SSean Nyekjaer 		return IIO_VAL_INT;
2753e87e783SSean Nyekjaer 	}
2763e87e783SSean Nyekjaer 	mutex_unlock(&st->lock);
2773e87e783SSean Nyekjaer 
2783e87e783SSean Nyekjaer 	return -EINVAL;
2793e87e783SSean Nyekjaer }
2803e87e783SSean Nyekjaer 
2813e87e783SSean Nyekjaer static int ads8688_write_reg_range(struct iio_dev *indio_dev,
2823e87e783SSean Nyekjaer 				   struct iio_chan_spec const *chan,
2833e87e783SSean Nyekjaer 				   enum ads8688_range range)
2843e87e783SSean Nyekjaer {
2853e87e783SSean Nyekjaer 	unsigned int tmp;
2863e87e783SSean Nyekjaer 	int ret;
2873e87e783SSean Nyekjaer 
2883e87e783SSean Nyekjaer 	tmp = ADS8688_PROG_REG_RANGE_CH(chan->channel);
2893e87e783SSean Nyekjaer 	ret = ads8688_prog_write(indio_dev, tmp, range);
2903e87e783SSean Nyekjaer 
2913e87e783SSean Nyekjaer 	return ret;
2923e87e783SSean Nyekjaer }
2933e87e783SSean Nyekjaer 
2943e87e783SSean Nyekjaer static int ads8688_write_raw(struct iio_dev *indio_dev,
2953e87e783SSean Nyekjaer 			     struct iio_chan_spec const *chan,
2963e87e783SSean Nyekjaer 			     int val, int val2, long mask)
2973e87e783SSean Nyekjaer {
2983e87e783SSean Nyekjaer 	struct ads8688_state *st = iio_priv(indio_dev);
2993e87e783SSean Nyekjaer 	unsigned int scale = 0;
3003e87e783SSean Nyekjaer 	int ret = -EINVAL, i, offset = 0;
3013e87e783SSean Nyekjaer 
3023e87e783SSean Nyekjaer 	mutex_lock(&st->lock);
3033e87e783SSean Nyekjaer 	switch (mask) {
3043e87e783SSean Nyekjaer 	case IIO_CHAN_INFO_SCALE:
3053e87e783SSean Nyekjaer 		/* If the offset is 0 the ±2.5 * VREF mode is not available */
3063e87e783SSean Nyekjaer 		offset = ads8688_range_def[st->range[chan->channel]].offset;
3073e87e783SSean Nyekjaer 		if (offset == 0 && val2 == ads8688_range_def[0].scale * st->vref_mv) {
3083e87e783SSean Nyekjaer 			mutex_unlock(&st->lock);
3093e87e783SSean Nyekjaer 			return -EINVAL;
3103e87e783SSean Nyekjaer 		}
3113e87e783SSean Nyekjaer 
3123e87e783SSean Nyekjaer 		/* Lookup new mode */
3133e87e783SSean Nyekjaer 		for (i = 0; i < ARRAY_SIZE(ads8688_range_def); i++)
3143e87e783SSean Nyekjaer 			if (val2 == ads8688_range_def[i].scale * st->vref_mv &&
3153e87e783SSean Nyekjaer 			    offset == ads8688_range_def[i].offset) {
3163e87e783SSean Nyekjaer 				ret = ads8688_write_reg_range(indio_dev, chan,
3173e87e783SSean Nyekjaer 					ads8688_range_def[i].reg);
3183e87e783SSean Nyekjaer 				break;
3193e87e783SSean Nyekjaer 			}
3203e87e783SSean Nyekjaer 		break;
3213e87e783SSean Nyekjaer 	case IIO_CHAN_INFO_OFFSET:
3223e87e783SSean Nyekjaer 		/*
3233e87e783SSean Nyekjaer 		 * There are only two available offsets:
3243e87e783SSean Nyekjaer 		 * 0 and -(1 << (ADS8688_REALBITS - 1))
3253e87e783SSean Nyekjaer 		 */
3263e87e783SSean Nyekjaer 		if (!(ads8688_range_def[0].offset == val ||
3273e87e783SSean Nyekjaer 		    ads8688_range_def[3].offset == val)) {
3283e87e783SSean Nyekjaer 			mutex_unlock(&st->lock);
3293e87e783SSean Nyekjaer 			return -EINVAL;
3303e87e783SSean Nyekjaer 		}
3313e87e783SSean Nyekjaer 
3323e87e783SSean Nyekjaer 		/*
3333e87e783SSean Nyekjaer 		 * If the device are in ±2.5 * VREF mode, it's not allowed to
3343e87e783SSean Nyekjaer 		 * switch to a mode where the offset is 0
3353e87e783SSean Nyekjaer 		 */
3363e87e783SSean Nyekjaer 		if (val == 0 &&
3373e87e783SSean Nyekjaer 		    st->range[chan->channel] == ADS8688_PLUSMINUS25VREF) {
3383e87e783SSean Nyekjaer 			mutex_unlock(&st->lock);
3393e87e783SSean Nyekjaer 			return -EINVAL;
3403e87e783SSean Nyekjaer 		}
3413e87e783SSean Nyekjaer 
3423e87e783SSean Nyekjaer 		scale = ads8688_range_def[st->range[chan->channel]].scale;
3433e87e783SSean Nyekjaer 
3443e87e783SSean Nyekjaer 		/* Lookup new mode */
3453e87e783SSean Nyekjaer 		for (i = 0; i < ARRAY_SIZE(ads8688_range_def); i++)
3463e87e783SSean Nyekjaer 			if (val == ads8688_range_def[i].offset &&
3473e87e783SSean Nyekjaer 			    scale == ads8688_range_def[i].scale) {
3483e87e783SSean Nyekjaer 				ret = ads8688_write_reg_range(indio_dev, chan,
3493e87e783SSean Nyekjaer 					ads8688_range_def[i].reg);
3503e87e783SSean Nyekjaer 				break;
3513e87e783SSean Nyekjaer 			}
3523e87e783SSean Nyekjaer 		break;
3533e87e783SSean Nyekjaer 	}
3543e87e783SSean Nyekjaer 
3553e87e783SSean Nyekjaer 	if (!ret)
3563e87e783SSean Nyekjaer 		st->range[chan->channel] = ads8688_range_def[i].range;
3573e87e783SSean Nyekjaer 
3583e87e783SSean Nyekjaer 	mutex_unlock(&st->lock);
3593e87e783SSean Nyekjaer 
3603e87e783SSean Nyekjaer 	return ret;
3613e87e783SSean Nyekjaer }
3623e87e783SSean Nyekjaer 
3633e87e783SSean Nyekjaer static int ads8688_write_raw_get_fmt(struct iio_dev *indio_dev,
3643e87e783SSean Nyekjaer 				     struct iio_chan_spec const *chan,
3653e87e783SSean Nyekjaer 				     long mask)
3663e87e783SSean Nyekjaer {
3673e87e783SSean Nyekjaer 	switch (mask) {
3683e87e783SSean Nyekjaer 	case IIO_CHAN_INFO_SCALE:
3693e87e783SSean Nyekjaer 		return IIO_VAL_INT_PLUS_NANO;
3703e87e783SSean Nyekjaer 	case IIO_CHAN_INFO_OFFSET:
3713e87e783SSean Nyekjaer 		return IIO_VAL_INT;
3723e87e783SSean Nyekjaer 	}
3733e87e783SSean Nyekjaer 
3743e87e783SSean Nyekjaer 	return -EINVAL;
3753e87e783SSean Nyekjaer }
3763e87e783SSean Nyekjaer 
3773e87e783SSean Nyekjaer static const struct iio_info ads8688_info = {
3783e87e783SSean Nyekjaer 	.read_raw = &ads8688_read_raw,
3793e87e783SSean Nyekjaer 	.write_raw = &ads8688_write_raw,
3803e87e783SSean Nyekjaer 	.write_raw_get_fmt = &ads8688_write_raw_get_fmt,
3813e87e783SSean Nyekjaer 	.attrs = &ads8688_attribute_group,
3823e87e783SSean Nyekjaer };
3833e87e783SSean Nyekjaer 
384*2a864877SSean Nyekjaer static irqreturn_t ads8688_trigger_handler(int irq, void *p)
385*2a864877SSean Nyekjaer {
386*2a864877SSean Nyekjaer 	struct iio_poll_func *pf = p;
387*2a864877SSean Nyekjaer 	struct iio_dev *indio_dev = pf->indio_dev;
388*2a864877SSean Nyekjaer 	u16 buffer[8];
389*2a864877SSean Nyekjaer 	int i, j = 0;
390*2a864877SSean Nyekjaer 
391*2a864877SSean Nyekjaer 	for (i = 0; i < indio_dev->masklength; i++) {
392*2a864877SSean Nyekjaer 		if (!test_bit(i, indio_dev->active_scan_mask))
393*2a864877SSean Nyekjaer 			continue;
394*2a864877SSean Nyekjaer 		buffer[j] = ads8688_read(indio_dev, i);
395*2a864877SSean Nyekjaer 		j++;
396*2a864877SSean Nyekjaer 	}
397*2a864877SSean Nyekjaer 
398*2a864877SSean Nyekjaer 	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
399*2a864877SSean Nyekjaer 			pf->timestamp);
400*2a864877SSean Nyekjaer 
401*2a864877SSean Nyekjaer 	iio_trigger_notify_done(indio_dev->trig);
402*2a864877SSean Nyekjaer 
403*2a864877SSean Nyekjaer 	return IRQ_HANDLED;
404*2a864877SSean Nyekjaer }
405*2a864877SSean Nyekjaer 
4063e87e783SSean Nyekjaer static const struct ads8688_chip_info ads8688_chip_info_tbl[] = {
4073e87e783SSean Nyekjaer 	[ID_ADS8684] = {
4083e87e783SSean Nyekjaer 		.channels = ads8684_channels,
4093e87e783SSean Nyekjaer 		.num_channels = ARRAY_SIZE(ads8684_channels),
4103e87e783SSean Nyekjaer 	},
4113e87e783SSean Nyekjaer 	[ID_ADS8688] = {
4123e87e783SSean Nyekjaer 		.channels = ads8688_channels,
4133e87e783SSean Nyekjaer 		.num_channels = ARRAY_SIZE(ads8688_channels),
4143e87e783SSean Nyekjaer 	},
4153e87e783SSean Nyekjaer };
4163e87e783SSean Nyekjaer 
4173e87e783SSean Nyekjaer static int ads8688_probe(struct spi_device *spi)
4183e87e783SSean Nyekjaer {
4193e87e783SSean Nyekjaer 	struct ads8688_state *st;
4203e87e783SSean Nyekjaer 	struct iio_dev *indio_dev;
4213e87e783SSean Nyekjaer 	int ret;
4223e87e783SSean Nyekjaer 
4233e87e783SSean Nyekjaer 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
4243e87e783SSean Nyekjaer 	if (indio_dev == NULL)
4253e87e783SSean Nyekjaer 		return -ENOMEM;
4263e87e783SSean Nyekjaer 
4273e87e783SSean Nyekjaer 	st = iio_priv(indio_dev);
4283e87e783SSean Nyekjaer 
4293e87e783SSean Nyekjaer 	st->reg = devm_regulator_get_optional(&spi->dev, "vref");
4303e87e783SSean Nyekjaer 	if (!IS_ERR(st->reg)) {
4313e87e783SSean Nyekjaer 		ret = regulator_enable(st->reg);
4323e87e783SSean Nyekjaer 		if (ret)
4333e87e783SSean Nyekjaer 			return ret;
4343e87e783SSean Nyekjaer 
4353e87e783SSean Nyekjaer 		ret = regulator_get_voltage(st->reg);
4363e87e783SSean Nyekjaer 		if (ret < 0)
437*2a864877SSean Nyekjaer 			goto err_regulator_disable;
4383e87e783SSean Nyekjaer 
4393e87e783SSean Nyekjaer 		st->vref_mv = ret / 1000;
4403e87e783SSean Nyekjaer 	} else {
4413e87e783SSean Nyekjaer 		/* Use internal reference */
4423e87e783SSean Nyekjaer 		st->vref_mv = ADS8688_VREF_MV;
4433e87e783SSean Nyekjaer 	}
4443e87e783SSean Nyekjaer 
4453e87e783SSean Nyekjaer 	st->chip_info =	&ads8688_chip_info_tbl[spi_get_device_id(spi)->driver_data];
4463e87e783SSean Nyekjaer 
4473e87e783SSean Nyekjaer 	spi->mode = SPI_MODE_1;
4483e87e783SSean Nyekjaer 
4493e87e783SSean Nyekjaer 	spi_set_drvdata(spi, indio_dev);
4503e87e783SSean Nyekjaer 
4513e87e783SSean Nyekjaer 	st->spi = spi;
4523e87e783SSean Nyekjaer 
4533e87e783SSean Nyekjaer 	indio_dev->name = spi_get_device_id(spi)->name;
4543e87e783SSean Nyekjaer 	indio_dev->dev.parent = &spi->dev;
455b541eaffSMatt Ranostay 	indio_dev->dev.of_node = spi->dev.of_node;
4563e87e783SSean Nyekjaer 	indio_dev->modes = INDIO_DIRECT_MODE;
4573e87e783SSean Nyekjaer 	indio_dev->channels = st->chip_info->channels;
4583e87e783SSean Nyekjaer 	indio_dev->num_channels = st->chip_info->num_channels;
4593e87e783SSean Nyekjaer 	indio_dev->info = &ads8688_info;
4603e87e783SSean Nyekjaer 
4613e87e783SSean Nyekjaer 	ads8688_reset(indio_dev);
4623e87e783SSean Nyekjaer 
4633e87e783SSean Nyekjaer 	mutex_init(&st->lock);
4643e87e783SSean Nyekjaer 
465*2a864877SSean Nyekjaer 	ret = iio_triggered_buffer_setup(indio_dev, NULL, ads8688_trigger_handler, NULL);
466*2a864877SSean Nyekjaer 	if (ret < 0) {
467*2a864877SSean Nyekjaer 		dev_err(&spi->dev, "iio triggered buffer setup failed\n");
468*2a864877SSean Nyekjaer 		goto err_regulator_disable;
469*2a864877SSean Nyekjaer 	}
470*2a864877SSean Nyekjaer 
4713e87e783SSean Nyekjaer 	ret = iio_device_register(indio_dev);
4723e87e783SSean Nyekjaer 	if (ret)
473*2a864877SSean Nyekjaer 		goto err_buffer_cleanup;
4743e87e783SSean Nyekjaer 
4753e87e783SSean Nyekjaer 	return 0;
4763e87e783SSean Nyekjaer 
477*2a864877SSean Nyekjaer err_buffer_cleanup:
478*2a864877SSean Nyekjaer 	iio_triggered_buffer_cleanup(indio_dev);
479*2a864877SSean Nyekjaer 
480*2a864877SSean Nyekjaer err_regulator_disable:
481fbe84bd4SCrt Mori 	if (!IS_ERR(st->reg))
4823e87e783SSean Nyekjaer 		regulator_disable(st->reg);
4833e87e783SSean Nyekjaer 
4843e87e783SSean Nyekjaer 	return ret;
4853e87e783SSean Nyekjaer }
4863e87e783SSean Nyekjaer 
4873e87e783SSean Nyekjaer static int ads8688_remove(struct spi_device *spi)
4883e87e783SSean Nyekjaer {
4893e87e783SSean Nyekjaer 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
4903e87e783SSean Nyekjaer 	struct ads8688_state *st = iio_priv(indio_dev);
4913e87e783SSean Nyekjaer 
4923e87e783SSean Nyekjaer 	iio_device_unregister(indio_dev);
493*2a864877SSean Nyekjaer 	iio_triggered_buffer_cleanup(indio_dev);
4943e87e783SSean Nyekjaer 
495fbe84bd4SCrt Mori 	if (!IS_ERR(st->reg))
4963e87e783SSean Nyekjaer 		regulator_disable(st->reg);
4973e87e783SSean Nyekjaer 
4983e87e783SSean Nyekjaer 	return 0;
4993e87e783SSean Nyekjaer }
5003e87e783SSean Nyekjaer 
5013e87e783SSean Nyekjaer static const struct spi_device_id ads8688_id[] = {
5023e87e783SSean Nyekjaer 	{"ads8684", ID_ADS8684},
5033e87e783SSean Nyekjaer 	{"ads8688", ID_ADS8688},
5043e87e783SSean Nyekjaer 	{}
5053e87e783SSean Nyekjaer };
5063e87e783SSean Nyekjaer MODULE_DEVICE_TABLE(spi, ads8688_id);
5073e87e783SSean Nyekjaer 
5083e87e783SSean Nyekjaer static const struct of_device_id ads8688_of_match[] = {
5093e87e783SSean Nyekjaer 	{ .compatible = "ti,ads8684" },
5103e87e783SSean Nyekjaer 	{ .compatible = "ti,ads8688" },
5113e87e783SSean Nyekjaer 	{ }
5123e87e783SSean Nyekjaer };
5133e87e783SSean Nyekjaer MODULE_DEVICE_TABLE(of, ads8688_of_match);
5143e87e783SSean Nyekjaer 
5153e87e783SSean Nyekjaer static struct spi_driver ads8688_driver = {
5163e87e783SSean Nyekjaer 	.driver = {
5173e87e783SSean Nyekjaer 		.name	= "ads8688",
5183e87e783SSean Nyekjaer 	},
5193e87e783SSean Nyekjaer 	.probe		= ads8688_probe,
5203e87e783SSean Nyekjaer 	.remove		= ads8688_remove,
5213e87e783SSean Nyekjaer 	.id_table	= ads8688_id,
5223e87e783SSean Nyekjaer };
5233e87e783SSean Nyekjaer module_spi_driver(ads8688_driver);
5243e87e783SSean Nyekjaer 
5253e87e783SSean Nyekjaer MODULE_AUTHOR("Sean Nyekjaer <sean.nyekjaer@prevas.dk>");
5263e87e783SSean Nyekjaer MODULE_DESCRIPTION("Texas Instruments ADS8688 driver");
5273e87e783SSean Nyekjaer MODULE_LICENSE("GPL v2");
528