2010-10-12 00:05:37 +08:00
|
|
|
/*
|
|
|
|
* AD7466/7/8 AD7476/5/7/8 (A) SPI ADC driver
|
|
|
|
*
|
|
|
|
* Copyright 2010 Analog Devices Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the GPL-2 or later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/sysfs.h>
|
|
|
|
#include <linux/spi/spi.h>
|
|
|
|
#include <linux/regulator/consumer.h>
|
|
|
|
#include <linux/err.h>
|
2011-07-04 03:49:50 +08:00
|
|
|
#include <linux/module.h>
|
2010-10-12 00:05:37 +08:00
|
|
|
|
2012-04-25 22:54:58 +08:00
|
|
|
#include <linux/iio/iio.h>
|
|
|
|
#include <linux/iio/sysfs.h>
|
|
|
|
#include <linux/iio/buffer.h>
|
2012-09-10 16:34:00 +08:00
|
|
|
#include <linux/iio/trigger_consumer.h>
|
|
|
|
#include <linux/iio/triggered_buffer.h>
|
2010-10-12 00:05:37 +08:00
|
|
|
|
2012-09-10 16:34:00 +08:00
|
|
|
#define RES_MASK(bits) ((1 << (bits)) - 1)
|
|
|
|
|
2012-09-17 20:26:00 +08:00
|
|
|
struct ad7476_state;
|
|
|
|
|
2012-09-10 16:34:00 +08:00
|
|
|
struct ad7476_chip_info {
|
|
|
|
unsigned int int_vref_uv;
|
|
|
|
struct iio_chan_spec channel[2];
|
2012-09-17 20:26:00 +08:00
|
|
|
void (*reset)(struct ad7476_state *);
|
2012-09-10 16:34:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ad7476_state {
|
|
|
|
struct spi_device *spi;
|
|
|
|
const struct ad7476_chip_info *chip_info;
|
|
|
|
struct regulator *reg;
|
|
|
|
struct spi_transfer xfer;
|
|
|
|
struct spi_message msg;
|
|
|
|
/*
|
|
|
|
* DMA (thus cache coherency maintenance) requires the
|
|
|
|
* transfer buffers to live in their own cache lines.
|
|
|
|
* Make the buffer large enough for one 16 bit sample and one 64 bit
|
|
|
|
* aligned 64 bit timestamp.
|
|
|
|
*/
|
|
|
|
unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)]
|
|
|
|
____cacheline_aligned;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ad7476_supported_device_ids {
|
2012-09-17 20:26:00 +08:00
|
|
|
ID_AD7091R,
|
2012-09-10 17:33:00 +08:00
|
|
|
ID_AD7276,
|
|
|
|
ID_AD7277,
|
|
|
|
ID_AD7278,
|
2012-09-10 16:34:00 +08:00
|
|
|
ID_AD7466,
|
|
|
|
ID_AD7467,
|
|
|
|
ID_AD7468,
|
2012-09-10 16:34:00 +08:00
|
|
|
ID_AD7495,
|
|
|
|
ID_AD7940,
|
2012-09-10 16:34:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static irqreturn_t ad7476_trigger_handler(int irq, void *p)
|
|
|
|
{
|
|
|
|
struct iio_poll_func *pf = p;
|
|
|
|
struct iio_dev *indio_dev = pf->indio_dev;
|
|
|
|
struct ad7476_state *st = iio_priv(indio_dev);
|
|
|
|
s64 time_ns;
|
|
|
|
int b_sent;
|
|
|
|
|
|
|
|
b_sent = spi_sync(st->spi, &st->msg);
|
|
|
|
if (b_sent < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
time_ns = iio_get_time_ns();
|
|
|
|
|
|
|
|
if (indio_dev->scan_timestamp)
|
|
|
|
((s64 *)st->data)[1] = time_ns;
|
|
|
|
|
2012-07-01 03:06:00 +08:00
|
|
|
iio_push_to_buffers(indio_dev, st->data);
|
2012-09-10 16:34:00 +08:00
|
|
|
done:
|
|
|
|
iio_trigger_notify_done(indio_dev->trig);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
2010-10-12 00:05:37 +08:00
|
|
|
|
2012-09-17 20:26:00 +08:00
|
|
|
static void ad7091_reset(struct ad7476_state *st)
|
|
|
|
{
|
|
|
|
/* Any transfers with 8 scl cycles will reset the device */
|
|
|
|
spi_read(st->spi, st->data, 1);
|
|
|
|
}
|
|
|
|
|
2010-10-12 00:05:37 +08:00
|
|
|
static int ad7476_scan_direct(struct ad7476_state *st)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2010-10-12 00:05:56 +08:00
|
|
|
ret = spi_sync(st->spi, &st->msg);
|
2010-10-12 00:05:37 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2012-09-10 16:34:00 +08:00
|
|
|
return be16_to_cpup((__be16 *)st->data);
|
2010-10-12 00:05:37 +08:00
|
|
|
}
|
|
|
|
|
2011-10-07 00:14:37 +08:00
|
|
|
static int ad7476_read_raw(struct iio_dev *indio_dev,
|
2011-05-18 21:41:24 +08:00
|
|
|
struct iio_chan_spec const *chan,
|
|
|
|
int *val,
|
|
|
|
int *val2,
|
|
|
|
long m)
|
2010-10-12 00:05:37 +08:00
|
|
|
{
|
|
|
|
int ret;
|
2011-10-07 00:14:37 +08:00
|
|
|
struct ad7476_state *st = iio_priv(indio_dev);
|
2012-09-10 16:34:00 +08:00
|
|
|
int scale_uv;
|
2011-05-18 21:41:24 +08:00
|
|
|
|
|
|
|
switch (m) {
|
2012-04-16 00:41:18 +08:00
|
|
|
case IIO_CHAN_INFO_RAW:
|
2011-10-07 00:14:37 +08:00
|
|
|
mutex_lock(&indio_dev->mlock);
|
|
|
|
if (iio_buffer_enabled(indio_dev))
|
2011-12-06 06:18:20 +08:00
|
|
|
ret = -EBUSY;
|
2011-05-18 21:41:24 +08:00
|
|
|
else
|
|
|
|
ret = ad7476_scan_direct(st);
|
2011-10-07 00:14:37 +08:00
|
|
|
mutex_unlock(&indio_dev->mlock);
|
2011-05-18 21:41:24 +08:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
*val = (ret >> st->chip_info->channel[0].scan_type.shift) &
|
|
|
|
RES_MASK(st->chip_info->channel[0].scan_type.realbits);
|
|
|
|
return IIO_VAL_INT;
|
2011-10-27 00:41:36 +08:00
|
|
|
case IIO_CHAN_INFO_SCALE:
|
2012-09-10 16:34:00 +08:00
|
|
|
if (!st->chip_info->int_vref_uv) {
|
|
|
|
scale_uv = regulator_get_voltage(st->reg);
|
|
|
|
if (scale_uv < 0)
|
|
|
|
return scale_uv;
|
|
|
|
} else {
|
|
|
|
scale_uv = st->chip_info->int_vref_uv;
|
|
|
|
}
|
|
|
|
scale_uv >>= chan->scan_type.realbits;
|
|
|
|
*val = scale_uv / 1000;
|
|
|
|
*val2 = (scale_uv % 1000) * 1000;
|
2011-05-18 21:41:24 +08:00
|
|
|
return IIO_VAL_INT_PLUS_MICRO;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
2010-10-12 00:05:37 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 20:26:00 +08:00
|
|
|
#define _AD7476_CHAN(bits, _shift, _info_mask) \
|
2012-04-13 17:42:55 +08:00
|
|
|
{ \
|
|
|
|
.type = IIO_VOLTAGE, \
|
|
|
|
.indexed = 1, \
|
2012-09-17 20:26:00 +08:00
|
|
|
.info_mask = _info_mask | \
|
2012-04-16 00:41:18 +08:00
|
|
|
IIO_CHAN_INFO_SCALE_SHARED_BIT, \
|
2012-04-13 17:42:55 +08:00
|
|
|
.scan_type = { \
|
|
|
|
.sign = 'u', \
|
2012-09-10 16:34:00 +08:00
|
|
|
.realbits = (bits), \
|
2012-04-13 17:42:55 +08:00
|
|
|
.storagebits = 16, \
|
2012-09-10 16:34:00 +08:00
|
|
|
.shift = (_shift), \
|
|
|
|
.endianness = IIO_BE, \
|
2012-04-13 17:42:55 +08:00
|
|
|
}, \
|
|
|
|
}
|
|
|
|
|
2012-09-17 20:26:00 +08:00
|
|
|
#define AD7476_CHAN(bits) _AD7476_CHAN((bits), 13 - (bits), \
|
|
|
|
IIO_CHAN_INFO_RAW_SEPARATE_BIT)
|
|
|
|
#define AD7940_CHAN(bits) _AD7476_CHAN((bits), 15 - (bits), \
|
|
|
|
IIO_CHAN_INFO_RAW_SEPARATE_BIT)
|
|
|
|
#define AD7091R_CHAN(bits) _AD7476_CHAN((bits), 16 - (bits), 0)
|
2012-09-10 16:34:00 +08:00
|
|
|
|
2010-10-12 00:05:37 +08:00
|
|
|
static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
|
2012-09-17 20:26:00 +08:00
|
|
|
[ID_AD7091R] = {
|
|
|
|
.channel[0] = AD7091R_CHAN(12),
|
|
|
|
.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
|
|
|
|
.reset = ad7091_reset,
|
|
|
|
},
|
2012-09-10 17:33:00 +08:00
|
|
|
[ID_AD7276] = {
|
|
|
|
.channel[0] = AD7940_CHAN(12),
|
|
|
|
.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
|
|
|
|
},
|
|
|
|
[ID_AD7277] = {
|
|
|
|
.channel[0] = AD7940_CHAN(10),
|
|
|
|
.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
|
|
|
|
},
|
|
|
|
[ID_AD7278] = {
|
|
|
|
.channel[0] = AD7940_CHAN(8),
|
|
|
|
.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
|
|
|
|
},
|
2010-10-12 00:05:37 +08:00
|
|
|
[ID_AD7466] = {
|
2012-04-13 17:42:55 +08:00
|
|
|
.channel[0] = AD7476_CHAN(12),
|
2011-05-18 21:41:24 +08:00
|
|
|
.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
|
2010-10-12 00:05:37 +08:00
|
|
|
},
|
|
|
|
[ID_AD7467] = {
|
2012-04-13 17:42:55 +08:00
|
|
|
.channel[0] = AD7476_CHAN(10),
|
2011-05-18 21:41:24 +08:00
|
|
|
.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
|
2010-10-12 00:05:37 +08:00
|
|
|
},
|
|
|
|
[ID_AD7468] = {
|
2012-04-13 17:42:55 +08:00
|
|
|
.channel[0] = AD7476_CHAN(8),
|
2011-05-18 21:41:24 +08:00
|
|
|
.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
|
2010-10-12 00:05:37 +08:00
|
|
|
},
|
|
|
|
[ID_AD7495] = {
|
2012-04-13 17:42:55 +08:00
|
|
|
.channel[0] = AD7476_CHAN(12),
|
2011-05-18 21:41:24 +08:00
|
|
|
.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
|
2012-09-10 16:34:00 +08:00
|
|
|
.int_vref_uv = 2500000,
|
2010-10-12 00:05:37 +08:00
|
|
|
},
|
2012-09-10 16:34:00 +08:00
|
|
|
[ID_AD7940] = {
|
|
|
|
.channel[0] = AD7940_CHAN(14),
|
|
|
|
.channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
|
|
|
|
},
|
2010-10-12 00:05:37 +08:00
|
|
|
};
|
|
|
|
|
2011-05-18 21:42:37 +08:00
|
|
|
static const struct iio_info ad7476_info = {
|
|
|
|
.driver_module = THIS_MODULE,
|
|
|
|
.read_raw = &ad7476_read_raw,
|
|
|
|
};
|
|
|
|
|
2012-12-22 05:21:43 +08:00
|
|
|
static int ad7476_probe(struct spi_device *spi)
|
2010-10-12 00:05:37 +08:00
|
|
|
{
|
|
|
|
struct ad7476_state *st;
|
2011-06-27 20:07:18 +08:00
|
|
|
struct iio_dev *indio_dev;
|
2012-09-10 16:34:00 +08:00
|
|
|
int ret;
|
2010-10-12 00:05:37 +08:00
|
|
|
|
2012-04-26 19:35:01 +08:00
|
|
|
indio_dev = iio_device_alloc(sizeof(*st));
|
2011-06-27 20:07:18 +08:00
|
|
|
if (indio_dev == NULL) {
|
2010-10-12 00:05:37 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error_ret;
|
|
|
|
}
|
2011-06-27 20:07:18 +08:00
|
|
|
st = iio_priv(indio_dev);
|
2010-10-12 00:05:37 +08:00
|
|
|
st->chip_info =
|
|
|
|
&ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data];
|
|
|
|
|
2012-09-10 16:34:00 +08:00
|
|
|
st->reg = regulator_get(&spi->dev, "vcc");
|
|
|
|
if (IS_ERR(st->reg)) {
|
|
|
|
ret = PTR_ERR(st->reg);
|
|
|
|
goto error_free_dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = regulator_enable(st->reg);
|
|
|
|
if (ret)
|
|
|
|
goto error_put_reg;
|
2010-10-12 00:05:37 +08:00
|
|
|
|
2011-07-13 20:42:47 +08:00
|
|
|
spi_set_drvdata(spi, indio_dev);
|
2010-10-12 00:05:37 +08:00
|
|
|
|
|
|
|
st->spi = spi;
|
|
|
|
|
2011-01-29 23:27:17 +08:00
|
|
|
/* Establish that the iio_dev is a child of the spi device */
|
2011-06-27 20:07:18 +08:00
|
|
|
indio_dev->dev.parent = &spi->dev;
|
|
|
|
indio_dev->name = spi_get_device_id(spi)->name;
|
|
|
|
indio_dev->modes = INDIO_DIRECT_MODE;
|
|
|
|
indio_dev->channels = st->chip_info->channel;
|
|
|
|
indio_dev->num_channels = 2;
|
|
|
|
indio_dev->info = &ad7476_info;
|
2010-10-12 00:05:37 +08:00
|
|
|
/* Setup default message */
|
|
|
|
|
|
|
|
st->xfer.rx_buf = &st->data;
|
2011-05-18 21:41:24 +08:00
|
|
|
st->xfer.len = st->chip_info->channel[0].scan_type.storagebits / 8;
|
2010-10-12 00:05:37 +08:00
|
|
|
|
|
|
|
spi_message_init(&st->msg);
|
|
|
|
spi_message_add_tail(&st->xfer, &st->msg);
|
|
|
|
|
2012-09-10 16:34:00 +08:00
|
|
|
ret = iio_triggered_buffer_setup(indio_dev, NULL,
|
|
|
|
&ad7476_trigger_handler, NULL);
|
2010-10-12 00:05:37 +08:00
|
|
|
if (ret)
|
2011-06-27 20:07:18 +08:00
|
|
|
goto error_disable_reg;
|
2010-10-12 00:05:37 +08:00
|
|
|
|
2012-09-17 20:26:00 +08:00
|
|
|
if (st->chip_info->reset)
|
|
|
|
st->chip_info->reset(st);
|
|
|
|
|
2011-09-03 00:14:40 +08:00
|
|
|
ret = iio_device_register(indio_dev);
|
|
|
|
if (ret)
|
|
|
|
goto error_ring_unregister;
|
2010-10-12 00:05:37 +08:00
|
|
|
return 0;
|
|
|
|
|
2011-09-03 00:14:40 +08:00
|
|
|
error_ring_unregister:
|
2012-09-10 16:34:00 +08:00
|
|
|
iio_triggered_buffer_cleanup(indio_dev);
|
2010-10-12 00:05:37 +08:00
|
|
|
error_disable_reg:
|
2012-09-10 16:34:00 +08:00
|
|
|
regulator_disable(st->reg);
|
2010-10-12 00:05:37 +08:00
|
|
|
error_put_reg:
|
2012-09-10 16:34:00 +08:00
|
|
|
regulator_put(st->reg);
|
|
|
|
error_free_dev:
|
2012-04-26 19:35:01 +08:00
|
|
|
iio_device_free(indio_dev);
|
2011-09-03 00:14:40 +08:00
|
|
|
|
2010-10-12 00:05:37 +08:00
|
|
|
error_ret:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-22 05:21:43 +08:00
|
|
|
static int ad7476_remove(struct spi_device *spi)
|
2010-10-12 00:05:37 +08:00
|
|
|
{
|
2011-06-27 20:07:18 +08:00
|
|
|
struct iio_dev *indio_dev = spi_get_drvdata(spi);
|
|
|
|
struct ad7476_state *st = iio_priv(indio_dev);
|
|
|
|
|
2011-10-14 21:46:58 +08:00
|
|
|
iio_device_unregister(indio_dev);
|
2012-09-10 16:34:00 +08:00
|
|
|
iio_triggered_buffer_cleanup(indio_dev);
|
2012-09-10 16:34:00 +08:00
|
|
|
regulator_disable(st->reg);
|
|
|
|
regulator_put(st->reg);
|
2012-04-26 19:35:01 +08:00
|
|
|
iio_device_free(indio_dev);
|
2011-06-27 20:07:18 +08:00
|
|
|
|
2010-10-12 00:05:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct spi_device_id ad7476_id[] = {
|
2012-09-17 20:26:00 +08:00
|
|
|
{"ad7091r", ID_AD7091R},
|
2012-09-10 17:33:00 +08:00
|
|
|
{"ad7273", ID_AD7277},
|
|
|
|
{"ad7274", ID_AD7276},
|
|
|
|
{"ad7276", ID_AD7276},
|
|
|
|
{"ad7277", ID_AD7277},
|
|
|
|
{"ad7278", ID_AD7278},
|
2010-10-12 00:05:37 +08:00
|
|
|
{"ad7466", ID_AD7466},
|
|
|
|
{"ad7467", ID_AD7467},
|
|
|
|
{"ad7468", ID_AD7468},
|
2012-09-10 16:34:00 +08:00
|
|
|
{"ad7475", ID_AD7466},
|
|
|
|
{"ad7476", ID_AD7466},
|
|
|
|
{"ad7476a", ID_AD7466},
|
|
|
|
{"ad7477", ID_AD7467},
|
|
|
|
{"ad7477a", ID_AD7467},
|
|
|
|
{"ad7478", ID_AD7468},
|
|
|
|
{"ad7478a", ID_AD7468},
|
2010-10-12 00:05:37 +08:00
|
|
|
{"ad7495", ID_AD7495},
|
2012-09-10 16:34:00 +08:00
|
|
|
{"ad7910", ID_AD7467},
|
|
|
|
{"ad7920", ID_AD7466},
|
2012-09-10 16:34:00 +08:00
|
|
|
{"ad7940", ID_AD7940},
|
2010-10-12 00:05:37 +08:00
|
|
|
{}
|
|
|
|
};
|
2011-11-16 15:53:31 +08:00
|
|
|
MODULE_DEVICE_TABLE(spi, ad7476_id);
|
2010-10-12 00:05:37 +08:00
|
|
|
|
|
|
|
static struct spi_driver ad7476_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "ad7476",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
.probe = ad7476_probe,
|
2012-12-22 05:21:43 +08:00
|
|
|
.remove = ad7476_remove,
|
2010-10-12 00:05:37 +08:00
|
|
|
.id_table = ad7476_id,
|
|
|
|
};
|
2011-11-16 17:13:39 +08:00
|
|
|
module_spi_driver(ad7476_driver);
|
2010-10-12 00:05:37 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
|
2012-09-10 16:34:00 +08:00
|
|
|
MODULE_DESCRIPTION("Analog Devices AD7476 and similar 1-channel ADCs");
|
2010-10-12 00:05:37 +08:00
|
|
|
MODULE_LICENSE("GPL v2");
|