1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23e87e783SSean Nyekjaer /*
33e87e783SSean Nyekjaer * Copyright (C) 2015 Prevas A/S
43e87e783SSean Nyekjaer */
53e87e783SSean Nyekjaer
63e87e783SSean Nyekjaer #include <linux/device.h>
73e87e783SSean Nyekjaer #include <linux/kernel.h>
83e87e783SSean Nyekjaer #include <linux/slab.h>
93e87e783SSean Nyekjaer #include <linux/sysfs.h>
103e87e783SSean Nyekjaer #include <linux/spi/spi.h>
113e87e783SSean Nyekjaer #include <linux/regulator/consumer.h>
123e87e783SSean Nyekjaer #include <linux/err.h>
133e87e783SSean Nyekjaer #include <linux/module.h>
143e87e783SSean Nyekjaer #include <linux/of.h>
153e87e783SSean Nyekjaer
163e87e783SSean Nyekjaer #include <linux/iio/iio.h>
172a864877SSean Nyekjaer #include <linux/iio/buffer.h>
182a864877SSean Nyekjaer #include <linux/iio/trigger_consumer.h>
192a864877SSean Nyekjaer #include <linux/iio/triggered_buffer.h>
203e87e783SSean Nyekjaer #include <linux/iio/sysfs.h>
213e87e783SSean Nyekjaer
223e87e783SSean Nyekjaer #define ADS8688_CMD_REG(x) (x << 8)
233e87e783SSean Nyekjaer #define ADS8688_CMD_REG_NOOP 0x00
243e87e783SSean Nyekjaer #define ADS8688_CMD_REG_RST 0x85
253e87e783SSean Nyekjaer #define ADS8688_CMD_REG_MAN_CH(chan) (0xC0 | (4 * chan))
263e87e783SSean Nyekjaer #define ADS8688_CMD_DONT_CARE_BITS 16
273e87e783SSean Nyekjaer
283e87e783SSean Nyekjaer #define ADS8688_PROG_REG(x) (x << 9)
293e87e783SSean Nyekjaer #define ADS8688_PROG_REG_RANGE_CH(chan) (0x05 + chan)
303e87e783SSean Nyekjaer #define ADS8688_PROG_WR_BIT BIT(8)
313e87e783SSean Nyekjaer #define ADS8688_PROG_DONT_CARE_BITS 8
323e87e783SSean Nyekjaer
333e87e783SSean Nyekjaer #define ADS8688_REG_PLUSMINUS25VREF 0
343e87e783SSean Nyekjaer #define ADS8688_REG_PLUSMINUS125VREF 1
353e87e783SSean Nyekjaer #define ADS8688_REG_PLUSMINUS0625VREF 2
363e87e783SSean Nyekjaer #define ADS8688_REG_PLUS25VREF 5
373e87e783SSean Nyekjaer #define ADS8688_REG_PLUS125VREF 6
383e87e783SSean Nyekjaer
393e87e783SSean Nyekjaer #define ADS8688_VREF_MV 4096
403e87e783SSean Nyekjaer #define ADS8688_REALBITS 16
41f214ff52SDan Murphy #define ADS8688_MAX_CHANNELS 8
423e87e783SSean Nyekjaer
433e87e783SSean Nyekjaer /*
443e87e783SSean Nyekjaer * enum ads8688_range - ADS8688 reference voltage range
453e87e783SSean Nyekjaer * @ADS8688_PLUSMINUS25VREF: Device is configured for input range ±2.5 * VREF
463e87e783SSean Nyekjaer * @ADS8688_PLUSMINUS125VREF: Device is configured for input range ±1.25 * VREF
473e87e783SSean Nyekjaer * @ADS8688_PLUSMINUS0625VREF: Device is configured for input range ±0.625 * VREF
483e87e783SSean Nyekjaer * @ADS8688_PLUS25VREF: Device is configured for input range 0 - 2.5 * VREF
493e87e783SSean Nyekjaer * @ADS8688_PLUS125VREF: Device is configured for input range 0 - 1.25 * VREF
503e87e783SSean Nyekjaer */
513e87e783SSean Nyekjaer enum ads8688_range {
523e87e783SSean Nyekjaer ADS8688_PLUSMINUS25VREF,
533e87e783SSean Nyekjaer ADS8688_PLUSMINUS125VREF,
543e87e783SSean Nyekjaer ADS8688_PLUSMINUS0625VREF,
553e87e783SSean Nyekjaer ADS8688_PLUS25VREF,
563e87e783SSean Nyekjaer ADS8688_PLUS125VREF,
573e87e783SSean Nyekjaer };
583e87e783SSean Nyekjaer
593e87e783SSean Nyekjaer struct ads8688_chip_info {
603e87e783SSean Nyekjaer const struct iio_chan_spec *channels;
613e87e783SSean Nyekjaer unsigned int num_channels;
623e87e783SSean Nyekjaer };
633e87e783SSean Nyekjaer
643e87e783SSean Nyekjaer struct ads8688_state {
653e87e783SSean Nyekjaer struct mutex lock;
663e87e783SSean Nyekjaer const struct ads8688_chip_info *chip_info;
673e87e783SSean Nyekjaer struct spi_device *spi;
683e87e783SSean Nyekjaer struct regulator *reg;
693e87e783SSean Nyekjaer unsigned int vref_mv;
703e87e783SSean Nyekjaer enum ads8688_range range[8];
713e87e783SSean Nyekjaer union {
723e87e783SSean Nyekjaer __be32 d32;
733e87e783SSean Nyekjaer u8 d8[4];
74a2105d87SJonathan Cameron } data[2] __aligned(IIO_DMA_MINALIGN);
753e87e783SSean Nyekjaer };
763e87e783SSean Nyekjaer
773e87e783SSean Nyekjaer enum ads8688_id {
783e87e783SSean Nyekjaer ID_ADS8684,
793e87e783SSean Nyekjaer ID_ADS8688,
803e87e783SSean Nyekjaer };
813e87e783SSean Nyekjaer
823e87e783SSean Nyekjaer struct ads8688_ranges {
833e87e783SSean Nyekjaer enum ads8688_range range;
843e87e783SSean Nyekjaer unsigned int scale;
853e87e783SSean Nyekjaer int offset;
863e87e783SSean Nyekjaer u8 reg;
873e87e783SSean Nyekjaer };
883e87e783SSean Nyekjaer
893e87e783SSean Nyekjaer static const struct ads8688_ranges ads8688_range_def[5] = {
903e87e783SSean Nyekjaer {
913e87e783SSean Nyekjaer .range = ADS8688_PLUSMINUS25VREF,
923e87e783SSean Nyekjaer .scale = 76295,
933e87e783SSean Nyekjaer .offset = -(1 << (ADS8688_REALBITS - 1)),
943e87e783SSean Nyekjaer .reg = ADS8688_REG_PLUSMINUS25VREF,
953e87e783SSean Nyekjaer }, {
963e87e783SSean Nyekjaer .range = ADS8688_PLUSMINUS125VREF,
973e87e783SSean Nyekjaer .scale = 38148,
983e87e783SSean Nyekjaer .offset = -(1 << (ADS8688_REALBITS - 1)),
993e87e783SSean Nyekjaer .reg = ADS8688_REG_PLUSMINUS125VREF,
1003e87e783SSean Nyekjaer }, {
1013e87e783SSean Nyekjaer .range = ADS8688_PLUSMINUS0625VREF,
1023e87e783SSean Nyekjaer .scale = 19074,
1033e87e783SSean Nyekjaer .offset = -(1 << (ADS8688_REALBITS - 1)),
1043e87e783SSean Nyekjaer .reg = ADS8688_REG_PLUSMINUS0625VREF,
1053e87e783SSean Nyekjaer }, {
1063e87e783SSean Nyekjaer .range = ADS8688_PLUS25VREF,
1073e87e783SSean Nyekjaer .scale = 38148,
1083e87e783SSean Nyekjaer .offset = 0,
1093e87e783SSean Nyekjaer .reg = ADS8688_REG_PLUS25VREF,
1103e87e783SSean Nyekjaer }, {
1113e87e783SSean Nyekjaer .range = ADS8688_PLUS125VREF,
1123e87e783SSean Nyekjaer .scale = 19074,
1133e87e783SSean Nyekjaer .offset = 0,
1143e87e783SSean Nyekjaer .reg = ADS8688_REG_PLUS125VREF,
1153e87e783SSean Nyekjaer }
1163e87e783SSean Nyekjaer };
1173e87e783SSean Nyekjaer
ads8688_show_scales(struct device * dev,struct device_attribute * attr,char * buf)1183e87e783SSean Nyekjaer static ssize_t ads8688_show_scales(struct device *dev,
1193e87e783SSean Nyekjaer struct device_attribute *attr, char *buf)
1203e87e783SSean Nyekjaer {
1213e87e783SSean Nyekjaer struct ads8688_state *st = iio_priv(dev_to_iio_dev(dev));
1223e87e783SSean Nyekjaer
1233e87e783SSean Nyekjaer return sprintf(buf, "0.%09u 0.%09u 0.%09u\n",
1243e87e783SSean Nyekjaer ads8688_range_def[0].scale * st->vref_mv,
1253e87e783SSean Nyekjaer ads8688_range_def[1].scale * st->vref_mv,
1263e87e783SSean Nyekjaer ads8688_range_def[2].scale * st->vref_mv);
1273e87e783SSean Nyekjaer }
1283e87e783SSean Nyekjaer
ads8688_show_offsets(struct device * dev,struct device_attribute * attr,char * buf)1293e87e783SSean Nyekjaer static ssize_t ads8688_show_offsets(struct device *dev,
1303e87e783SSean Nyekjaer struct device_attribute *attr, char *buf)
1313e87e783SSean Nyekjaer {
1323e87e783SSean Nyekjaer return sprintf(buf, "%d %d\n", ads8688_range_def[0].offset,
1333e87e783SSean Nyekjaer ads8688_range_def[3].offset);
1343e87e783SSean Nyekjaer }
1353e87e783SSean Nyekjaer
1363e87e783SSean Nyekjaer static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO,
1373e87e783SSean Nyekjaer ads8688_show_scales, NULL, 0);
1383e87e783SSean Nyekjaer static IIO_DEVICE_ATTR(in_voltage_offset_available, S_IRUGO,
1393e87e783SSean Nyekjaer ads8688_show_offsets, NULL, 0);
1403e87e783SSean Nyekjaer
1413e87e783SSean Nyekjaer static struct attribute *ads8688_attributes[] = {
1423e87e783SSean Nyekjaer &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
1433e87e783SSean Nyekjaer &iio_dev_attr_in_voltage_offset_available.dev_attr.attr,
1443e87e783SSean Nyekjaer NULL,
1453e87e783SSean Nyekjaer };
1463e87e783SSean Nyekjaer
1473e87e783SSean Nyekjaer static const struct attribute_group ads8688_attribute_group = {
1483e87e783SSean Nyekjaer .attrs = ads8688_attributes,
1493e87e783SSean Nyekjaer };
1503e87e783SSean Nyekjaer
1513e87e783SSean Nyekjaer #define ADS8688_CHAN(index) \
1523e87e783SSean Nyekjaer { \
1533e87e783SSean Nyekjaer .type = IIO_VOLTAGE, \
1543e87e783SSean Nyekjaer .indexed = 1, \
1553e87e783SSean Nyekjaer .channel = index, \
1563e87e783SSean Nyekjaer .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
1573e87e783SSean Nyekjaer | BIT(IIO_CHAN_INFO_SCALE) \
1583e87e783SSean Nyekjaer | BIT(IIO_CHAN_INFO_OFFSET), \
1592a864877SSean Nyekjaer .scan_index = index, \
1602a864877SSean Nyekjaer .scan_type = { \
1612a864877SSean Nyekjaer .sign = 'u', \
1622a864877SSean Nyekjaer .realbits = 16, \
1632a864877SSean Nyekjaer .storagebits = 16, \
1642a864877SSean Nyekjaer .endianness = IIO_BE, \
1652a864877SSean Nyekjaer }, \
1663e87e783SSean Nyekjaer }
1673e87e783SSean Nyekjaer
1683e87e783SSean Nyekjaer static const struct iio_chan_spec ads8684_channels[] = {
1693e87e783SSean Nyekjaer ADS8688_CHAN(0),
1703e87e783SSean Nyekjaer ADS8688_CHAN(1),
1713e87e783SSean Nyekjaer ADS8688_CHAN(2),
1723e87e783SSean Nyekjaer ADS8688_CHAN(3),
1733e87e783SSean Nyekjaer };
1743e87e783SSean Nyekjaer
1753e87e783SSean Nyekjaer static const struct iio_chan_spec ads8688_channels[] = {
1763e87e783SSean Nyekjaer ADS8688_CHAN(0),
1773e87e783SSean Nyekjaer ADS8688_CHAN(1),
1783e87e783SSean Nyekjaer ADS8688_CHAN(2),
1793e87e783SSean Nyekjaer ADS8688_CHAN(3),
1803e87e783SSean Nyekjaer ADS8688_CHAN(4),
1813e87e783SSean Nyekjaer ADS8688_CHAN(5),
1823e87e783SSean Nyekjaer ADS8688_CHAN(6),
1833e87e783SSean Nyekjaer ADS8688_CHAN(7),
1843e87e783SSean Nyekjaer };
1853e87e783SSean Nyekjaer
ads8688_prog_write(struct iio_dev * indio_dev,unsigned int addr,unsigned int val)1863e87e783SSean Nyekjaer static int ads8688_prog_write(struct iio_dev *indio_dev, unsigned int addr,
1873e87e783SSean Nyekjaer unsigned int val)
1883e87e783SSean Nyekjaer {
1893e87e783SSean Nyekjaer struct ads8688_state *st = iio_priv(indio_dev);
1903e87e783SSean Nyekjaer u32 tmp;
1913e87e783SSean Nyekjaer
1923e87e783SSean Nyekjaer tmp = ADS8688_PROG_REG(addr) | ADS8688_PROG_WR_BIT | val;
1933e87e783SSean Nyekjaer tmp <<= ADS8688_PROG_DONT_CARE_BITS;
1943e87e783SSean Nyekjaer st->data[0].d32 = cpu_to_be32(tmp);
1953e87e783SSean Nyekjaer
1963e87e783SSean Nyekjaer return spi_write(st->spi, &st->data[0].d8[1], 3);
1973e87e783SSean Nyekjaer }
1983e87e783SSean Nyekjaer
ads8688_reset(struct iio_dev * indio_dev)1993e87e783SSean Nyekjaer static int ads8688_reset(struct iio_dev *indio_dev)
2003e87e783SSean Nyekjaer {
2013e87e783SSean Nyekjaer struct ads8688_state *st = iio_priv(indio_dev);
2023e87e783SSean Nyekjaer u32 tmp;
2033e87e783SSean Nyekjaer
2043e87e783SSean Nyekjaer tmp = ADS8688_CMD_REG(ADS8688_CMD_REG_RST);
2053e87e783SSean Nyekjaer tmp <<= ADS8688_CMD_DONT_CARE_BITS;
2063e87e783SSean Nyekjaer st->data[0].d32 = cpu_to_be32(tmp);
2073e87e783SSean Nyekjaer
2083e87e783SSean Nyekjaer return spi_write(st->spi, &st->data[0].d8[0], 4);
2093e87e783SSean Nyekjaer }
2103e87e783SSean Nyekjaer
ads8688_read(struct iio_dev * indio_dev,unsigned int chan)2113e87e783SSean Nyekjaer static int ads8688_read(struct iio_dev *indio_dev, unsigned int chan)
2123e87e783SSean Nyekjaer {
2133e87e783SSean Nyekjaer struct ads8688_state *st = iio_priv(indio_dev);
2143e87e783SSean Nyekjaer int ret;
2153e87e783SSean Nyekjaer u32 tmp;
2163e87e783SSean Nyekjaer struct spi_transfer t[] = {
2173e87e783SSean Nyekjaer {
2183e87e783SSean Nyekjaer .tx_buf = &st->data[0].d8[0],
2193e87e783SSean Nyekjaer .len = 4,
2203e87e783SSean Nyekjaer .cs_change = 1,
2213e87e783SSean Nyekjaer }, {
2223e87e783SSean Nyekjaer .tx_buf = &st->data[1].d8[0],
2233e87e783SSean Nyekjaer .rx_buf = &st->data[1].d8[0],
2243e87e783SSean Nyekjaer .len = 4,
2253e87e783SSean Nyekjaer },
2263e87e783SSean Nyekjaer };
2273e87e783SSean Nyekjaer
2283e87e783SSean Nyekjaer tmp = ADS8688_CMD_REG(ADS8688_CMD_REG_MAN_CH(chan));
2293e87e783SSean Nyekjaer tmp <<= ADS8688_CMD_DONT_CARE_BITS;
2303e87e783SSean Nyekjaer st->data[0].d32 = cpu_to_be32(tmp);
2313e87e783SSean Nyekjaer
2323e87e783SSean Nyekjaer tmp = ADS8688_CMD_REG(ADS8688_CMD_REG_NOOP);
2333e87e783SSean Nyekjaer tmp <<= ADS8688_CMD_DONT_CARE_BITS;
2343e87e783SSean Nyekjaer st->data[1].d32 = cpu_to_be32(tmp);
2353e87e783SSean Nyekjaer
2363e87e783SSean Nyekjaer ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
2373e87e783SSean Nyekjaer if (ret < 0)
2383e87e783SSean Nyekjaer return ret;
2393e87e783SSean Nyekjaer
2403e87e783SSean Nyekjaer return be32_to_cpu(st->data[1].d32) & 0xffff;
2413e87e783SSean Nyekjaer }
2423e87e783SSean Nyekjaer
ads8688_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)2433e87e783SSean Nyekjaer static int ads8688_read_raw(struct iio_dev *indio_dev,
2443e87e783SSean Nyekjaer struct iio_chan_spec const *chan,
2453e87e783SSean Nyekjaer int *val, int *val2, long m)
2463e87e783SSean Nyekjaer {
2473e87e783SSean Nyekjaer int ret, offset;
2483e87e783SSean Nyekjaer unsigned long scale_mv;
2493e87e783SSean Nyekjaer
2503e87e783SSean Nyekjaer struct ads8688_state *st = iio_priv(indio_dev);
2513e87e783SSean Nyekjaer
2523e87e783SSean Nyekjaer mutex_lock(&st->lock);
2533e87e783SSean Nyekjaer switch (m) {
2543e87e783SSean Nyekjaer case IIO_CHAN_INFO_RAW:
2553e87e783SSean Nyekjaer ret = ads8688_read(indio_dev, chan->channel);
2563e87e783SSean Nyekjaer mutex_unlock(&st->lock);
2573e87e783SSean Nyekjaer if (ret < 0)
2583e87e783SSean Nyekjaer return ret;
2593e87e783SSean Nyekjaer *val = ret;
2603e87e783SSean Nyekjaer return IIO_VAL_INT;
2613e87e783SSean Nyekjaer case IIO_CHAN_INFO_SCALE:
2623e87e783SSean Nyekjaer scale_mv = st->vref_mv;
2633e87e783SSean Nyekjaer scale_mv *= ads8688_range_def[st->range[chan->channel]].scale;
2643e87e783SSean Nyekjaer *val = 0;
2653e87e783SSean Nyekjaer *val2 = scale_mv;
2663e87e783SSean Nyekjaer mutex_unlock(&st->lock);
2673e87e783SSean Nyekjaer return IIO_VAL_INT_PLUS_NANO;
2683e87e783SSean Nyekjaer case IIO_CHAN_INFO_OFFSET:
2693e87e783SSean Nyekjaer offset = ads8688_range_def[st->range[chan->channel]].offset;
2703e87e783SSean Nyekjaer *val = offset;
2713e87e783SSean Nyekjaer mutex_unlock(&st->lock);
2723e87e783SSean Nyekjaer return IIO_VAL_INT;
2733e87e783SSean Nyekjaer }
2743e87e783SSean Nyekjaer mutex_unlock(&st->lock);
2753e87e783SSean Nyekjaer
2763e87e783SSean Nyekjaer return -EINVAL;
2773e87e783SSean Nyekjaer }
2783e87e783SSean Nyekjaer
ads8688_write_reg_range(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,enum ads8688_range range)2793e87e783SSean Nyekjaer static int ads8688_write_reg_range(struct iio_dev *indio_dev,
2803e87e783SSean Nyekjaer struct iio_chan_spec const *chan,
2813e87e783SSean Nyekjaer enum ads8688_range range)
2823e87e783SSean Nyekjaer {
2833e87e783SSean Nyekjaer unsigned int tmp;
2843e87e783SSean Nyekjaer
2853e87e783SSean Nyekjaer tmp = ADS8688_PROG_REG_RANGE_CH(chan->channel);
2863e87e783SSean Nyekjaer
2878a457852SMinghao Chi return ads8688_prog_write(indio_dev, tmp, range);
2883e87e783SSean Nyekjaer }
2893e87e783SSean Nyekjaer
ads8688_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)2903e87e783SSean Nyekjaer static int ads8688_write_raw(struct iio_dev *indio_dev,
2913e87e783SSean Nyekjaer struct iio_chan_spec const *chan,
2923e87e783SSean Nyekjaer int val, int val2, long mask)
2933e87e783SSean Nyekjaer {
2943e87e783SSean Nyekjaer struct ads8688_state *st = iio_priv(indio_dev);
2953e87e783SSean Nyekjaer unsigned int scale = 0;
2963e87e783SSean Nyekjaer int ret = -EINVAL, i, offset = 0;
2973e87e783SSean Nyekjaer
2983e87e783SSean Nyekjaer mutex_lock(&st->lock);
2993e87e783SSean Nyekjaer switch (mask) {
3003e87e783SSean Nyekjaer case IIO_CHAN_INFO_SCALE:
3013e87e783SSean Nyekjaer /* If the offset is 0 the ±2.5 * VREF mode is not available */
3023e87e783SSean Nyekjaer offset = ads8688_range_def[st->range[chan->channel]].offset;
3033e87e783SSean Nyekjaer if (offset == 0 && val2 == ads8688_range_def[0].scale * st->vref_mv) {
3043e87e783SSean Nyekjaer mutex_unlock(&st->lock);
3053e87e783SSean Nyekjaer return -EINVAL;
3063e87e783SSean Nyekjaer }
3073e87e783SSean Nyekjaer
3083e87e783SSean Nyekjaer /* Lookup new mode */
3093e87e783SSean Nyekjaer for (i = 0; i < ARRAY_SIZE(ads8688_range_def); i++)
3103e87e783SSean Nyekjaer if (val2 == ads8688_range_def[i].scale * st->vref_mv &&
3113e87e783SSean Nyekjaer offset == ads8688_range_def[i].offset) {
3123e87e783SSean Nyekjaer ret = ads8688_write_reg_range(indio_dev, chan,
3133e87e783SSean Nyekjaer ads8688_range_def[i].reg);
3143e87e783SSean Nyekjaer break;
3153e87e783SSean Nyekjaer }
3163e87e783SSean Nyekjaer break;
3173e87e783SSean Nyekjaer case IIO_CHAN_INFO_OFFSET:
3183e87e783SSean Nyekjaer /*
3193e87e783SSean Nyekjaer * There are only two available offsets:
3203e87e783SSean Nyekjaer * 0 and -(1 << (ADS8688_REALBITS - 1))
3213e87e783SSean Nyekjaer */
3223e87e783SSean Nyekjaer if (!(ads8688_range_def[0].offset == val ||
3233e87e783SSean Nyekjaer ads8688_range_def[3].offset == val)) {
3243e87e783SSean Nyekjaer mutex_unlock(&st->lock);
3253e87e783SSean Nyekjaer return -EINVAL;
3263e87e783SSean Nyekjaer }
3273e87e783SSean Nyekjaer
3283e87e783SSean Nyekjaer /*
3293e87e783SSean Nyekjaer * If the device are in ±2.5 * VREF mode, it's not allowed to
3303e87e783SSean Nyekjaer * switch to a mode where the offset is 0
3313e87e783SSean Nyekjaer */
3323e87e783SSean Nyekjaer if (val == 0 &&
3333e87e783SSean Nyekjaer st->range[chan->channel] == ADS8688_PLUSMINUS25VREF) {
3343e87e783SSean Nyekjaer mutex_unlock(&st->lock);
3353e87e783SSean Nyekjaer return -EINVAL;
3363e87e783SSean Nyekjaer }
3373e87e783SSean Nyekjaer
3383e87e783SSean Nyekjaer scale = ads8688_range_def[st->range[chan->channel]].scale;
3393e87e783SSean Nyekjaer
3403e87e783SSean Nyekjaer /* Lookup new mode */
3413e87e783SSean Nyekjaer for (i = 0; i < ARRAY_SIZE(ads8688_range_def); i++)
3423e87e783SSean Nyekjaer if (val == ads8688_range_def[i].offset &&
3433e87e783SSean Nyekjaer scale == ads8688_range_def[i].scale) {
3443e87e783SSean Nyekjaer ret = ads8688_write_reg_range(indio_dev, chan,
3453e87e783SSean Nyekjaer ads8688_range_def[i].reg);
3463e87e783SSean Nyekjaer break;
3473e87e783SSean Nyekjaer }
3483e87e783SSean Nyekjaer break;
3493e87e783SSean Nyekjaer }
3503e87e783SSean Nyekjaer
3513e87e783SSean Nyekjaer if (!ret)
3523e87e783SSean Nyekjaer st->range[chan->channel] = ads8688_range_def[i].range;
3533e87e783SSean Nyekjaer
3543e87e783SSean Nyekjaer mutex_unlock(&st->lock);
3553e87e783SSean Nyekjaer
3563e87e783SSean Nyekjaer return ret;
3573e87e783SSean Nyekjaer }
3583e87e783SSean Nyekjaer
ads8688_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)3593e87e783SSean Nyekjaer static int ads8688_write_raw_get_fmt(struct iio_dev *indio_dev,
3603e87e783SSean Nyekjaer struct iio_chan_spec const *chan,
3613e87e783SSean Nyekjaer long mask)
3623e87e783SSean Nyekjaer {
3633e87e783SSean Nyekjaer switch (mask) {
3643e87e783SSean Nyekjaer case IIO_CHAN_INFO_SCALE:
3653e87e783SSean Nyekjaer return IIO_VAL_INT_PLUS_NANO;
3663e87e783SSean Nyekjaer case IIO_CHAN_INFO_OFFSET:
3673e87e783SSean Nyekjaer return IIO_VAL_INT;
3683e87e783SSean Nyekjaer }
3693e87e783SSean Nyekjaer
3703e87e783SSean Nyekjaer return -EINVAL;
3713e87e783SSean Nyekjaer }
3723e87e783SSean Nyekjaer
3733e87e783SSean Nyekjaer static const struct iio_info ads8688_info = {
3743e87e783SSean Nyekjaer .read_raw = &ads8688_read_raw,
3753e87e783SSean Nyekjaer .write_raw = &ads8688_write_raw,
3763e87e783SSean Nyekjaer .write_raw_get_fmt = &ads8688_write_raw_get_fmt,
3773e87e783SSean Nyekjaer .attrs = &ads8688_attribute_group,
3783e87e783SSean Nyekjaer };
3793e87e783SSean Nyekjaer
ads8688_trigger_handler(int irq,void * p)3802a864877SSean Nyekjaer static irqreturn_t ads8688_trigger_handler(int irq, void *p)
3812a864877SSean Nyekjaer {
3822a864877SSean Nyekjaer struct iio_poll_func *pf = p;
3832a864877SSean Nyekjaer struct iio_dev *indio_dev = pf->indio_dev;
38461fa5dfaSJonathan Cameron /* Ensure naturally aligned timestamp */
385*455df95eSJavier Carrasco u16 buffer[ADS8688_MAX_CHANNELS + sizeof(s64)/sizeof(u16)] __aligned(8) = { };
3862a864877SSean Nyekjaer int i, j = 0;
3872a864877SSean Nyekjaer
3882a864877SSean Nyekjaer for (i = 0; i < indio_dev->masklength; i++) {
3892a864877SSean Nyekjaer if (!test_bit(i, indio_dev->active_scan_mask))
3902a864877SSean Nyekjaer continue;
3912a864877SSean Nyekjaer buffer[j] = ads8688_read(indio_dev, i);
3922a864877SSean Nyekjaer j++;
3932a864877SSean Nyekjaer }
3942a864877SSean Nyekjaer
3952a864877SSean Nyekjaer iio_push_to_buffers_with_timestamp(indio_dev, buffer,
396e6d12298SSean Nyekjaer iio_get_time_ns(indio_dev));
3972a864877SSean Nyekjaer
3982a864877SSean Nyekjaer iio_trigger_notify_done(indio_dev->trig);
3992a864877SSean Nyekjaer
4002a864877SSean Nyekjaer return IRQ_HANDLED;
4012a864877SSean Nyekjaer }
4022a864877SSean Nyekjaer
4033e87e783SSean Nyekjaer static const struct ads8688_chip_info ads8688_chip_info_tbl[] = {
4043e87e783SSean Nyekjaer [ID_ADS8684] = {
4053e87e783SSean Nyekjaer .channels = ads8684_channels,
4063e87e783SSean Nyekjaer .num_channels = ARRAY_SIZE(ads8684_channels),
4073e87e783SSean Nyekjaer },
4083e87e783SSean Nyekjaer [ID_ADS8688] = {
4093e87e783SSean Nyekjaer .channels = ads8688_channels,
4103e87e783SSean Nyekjaer .num_channels = ARRAY_SIZE(ads8688_channels),
4113e87e783SSean Nyekjaer },
4123e87e783SSean Nyekjaer };
4133e87e783SSean Nyekjaer
ads8688_probe(struct spi_device * spi)4143e87e783SSean Nyekjaer static int ads8688_probe(struct spi_device *spi)
4153e87e783SSean Nyekjaer {
4163e87e783SSean Nyekjaer struct ads8688_state *st;
4173e87e783SSean Nyekjaer struct iio_dev *indio_dev;
4183e87e783SSean Nyekjaer int ret;
4193e87e783SSean Nyekjaer
4203e87e783SSean Nyekjaer indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
4213e87e783SSean Nyekjaer if (indio_dev == NULL)
4223e87e783SSean Nyekjaer return -ENOMEM;
4233e87e783SSean Nyekjaer
4243e87e783SSean Nyekjaer st = iio_priv(indio_dev);
4253e87e783SSean Nyekjaer
4263e87e783SSean Nyekjaer st->reg = devm_regulator_get_optional(&spi->dev, "vref");
4273e87e783SSean Nyekjaer if (!IS_ERR(st->reg)) {
4283e87e783SSean Nyekjaer ret = regulator_enable(st->reg);
4293e87e783SSean Nyekjaer if (ret)
4303e87e783SSean Nyekjaer return ret;
4313e87e783SSean Nyekjaer
4323e87e783SSean Nyekjaer ret = regulator_get_voltage(st->reg);
4333e87e783SSean Nyekjaer if (ret < 0)
4342a864877SSean Nyekjaer goto err_regulator_disable;
4353e87e783SSean Nyekjaer
4363e87e783SSean Nyekjaer st->vref_mv = ret / 1000;
4373e87e783SSean Nyekjaer } else {
4383e87e783SSean Nyekjaer /* Use internal reference */
4393e87e783SSean Nyekjaer st->vref_mv = ADS8688_VREF_MV;
4403e87e783SSean Nyekjaer }
4413e87e783SSean Nyekjaer
4423e87e783SSean Nyekjaer st->chip_info = &ads8688_chip_info_tbl[spi_get_device_id(spi)->driver_data];
4433e87e783SSean Nyekjaer
4443e87e783SSean Nyekjaer spi->mode = SPI_MODE_1;
4453e87e783SSean Nyekjaer
4463e87e783SSean Nyekjaer spi_set_drvdata(spi, indio_dev);
4473e87e783SSean Nyekjaer
4483e87e783SSean Nyekjaer st->spi = spi;
4493e87e783SSean Nyekjaer
4503e87e783SSean Nyekjaer indio_dev->name = spi_get_device_id(spi)->name;
4513e87e783SSean Nyekjaer indio_dev->modes = INDIO_DIRECT_MODE;
4523e87e783SSean Nyekjaer indio_dev->channels = st->chip_info->channels;
4533e87e783SSean Nyekjaer indio_dev->num_channels = st->chip_info->num_channels;
4543e87e783SSean Nyekjaer indio_dev->info = &ads8688_info;
4553e87e783SSean Nyekjaer
4563e87e783SSean Nyekjaer ads8688_reset(indio_dev);
4573e87e783SSean Nyekjaer
4583e87e783SSean Nyekjaer mutex_init(&st->lock);
4593e87e783SSean Nyekjaer
4602a864877SSean Nyekjaer ret = iio_triggered_buffer_setup(indio_dev, NULL, ads8688_trigger_handler, NULL);
4612a864877SSean Nyekjaer if (ret < 0) {
4622a864877SSean Nyekjaer dev_err(&spi->dev, "iio triggered buffer setup failed\n");
4632a864877SSean Nyekjaer goto err_regulator_disable;
4642a864877SSean Nyekjaer }
4652a864877SSean Nyekjaer
4663e87e783SSean Nyekjaer ret = iio_device_register(indio_dev);
4673e87e783SSean Nyekjaer if (ret)
4682a864877SSean Nyekjaer goto err_buffer_cleanup;
4693e87e783SSean Nyekjaer
4703e87e783SSean Nyekjaer return 0;
4713e87e783SSean Nyekjaer
4722a864877SSean Nyekjaer err_buffer_cleanup:
4732a864877SSean Nyekjaer iio_triggered_buffer_cleanup(indio_dev);
4742a864877SSean Nyekjaer
4752a864877SSean Nyekjaer err_regulator_disable:
476fbe84bd4SCrt Mori if (!IS_ERR(st->reg))
4773e87e783SSean Nyekjaer regulator_disable(st->reg);
4783e87e783SSean Nyekjaer
4793e87e783SSean Nyekjaer return ret;
4803e87e783SSean Nyekjaer }
4813e87e783SSean Nyekjaer
ads8688_remove(struct spi_device * spi)482a0386bbaSUwe Kleine-König static void ads8688_remove(struct spi_device *spi)
4833e87e783SSean Nyekjaer {
4843e87e783SSean Nyekjaer struct iio_dev *indio_dev = spi_get_drvdata(spi);
4853e87e783SSean Nyekjaer struct ads8688_state *st = iio_priv(indio_dev);
4863e87e783SSean Nyekjaer
4873e87e783SSean Nyekjaer iio_device_unregister(indio_dev);
4882a864877SSean Nyekjaer iio_triggered_buffer_cleanup(indio_dev);
4893e87e783SSean Nyekjaer
490fbe84bd4SCrt Mori if (!IS_ERR(st->reg))
4913e87e783SSean Nyekjaer regulator_disable(st->reg);
4923e87e783SSean Nyekjaer }
4933e87e783SSean Nyekjaer
4943e87e783SSean Nyekjaer static const struct spi_device_id ads8688_id[] = {
4953e87e783SSean Nyekjaer {"ads8684", ID_ADS8684},
4963e87e783SSean Nyekjaer {"ads8688", ID_ADS8688},
4973e87e783SSean Nyekjaer {}
4983e87e783SSean Nyekjaer };
4993e87e783SSean Nyekjaer MODULE_DEVICE_TABLE(spi, ads8688_id);
5003e87e783SSean Nyekjaer
5013e87e783SSean Nyekjaer static const struct of_device_id ads8688_of_match[] = {
5023e87e783SSean Nyekjaer { .compatible = "ti,ads8684" },
5033e87e783SSean Nyekjaer { .compatible = "ti,ads8688" },
5043e87e783SSean Nyekjaer { }
5053e87e783SSean Nyekjaer };
5063e87e783SSean Nyekjaer MODULE_DEVICE_TABLE(of, ads8688_of_match);
5073e87e783SSean Nyekjaer
5083e87e783SSean Nyekjaer static struct spi_driver ads8688_driver = {
5093e87e783SSean Nyekjaer .driver = {
5103e87e783SSean Nyekjaer .name = "ads8688",
511eda75f82SKrzysztof Kozlowski .of_match_table = ads8688_of_match,
5123e87e783SSean Nyekjaer },
5133e87e783SSean Nyekjaer .probe = ads8688_probe,
5143e87e783SSean Nyekjaer .remove = ads8688_remove,
5153e87e783SSean Nyekjaer .id_table = ads8688_id,
5163e87e783SSean Nyekjaer };
5173e87e783SSean Nyekjaer module_spi_driver(ads8688_driver);
5183e87e783SSean Nyekjaer
519bcccd7abSSean Nyekjaer MODULE_AUTHOR("Sean Nyekjaer <sean@geanix.dk>");
5203e87e783SSean Nyekjaer MODULE_DESCRIPTION("Texas Instruments ADS8688 driver");
5213e87e783SSean Nyekjaer MODULE_LICENSE("GPL v2");
522