Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2017 Axis Communications AB
0004  *
0005  * Driver for Texas Instruments' ADC084S021 ADC chip.
0006  * Datasheets can be found here:
0007  * https://www.ti.com/lit/ds/symlink/adc084s021.pdf
0008  */
0009 
0010 #include <linux/err.h>
0011 #include <linux/spi/spi.h>
0012 #include <linux/module.h>
0013 #include <linux/mod_devicetable.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/iio/iio.h>
0016 #include <linux/iio/buffer.h>
0017 #include <linux/iio/triggered_buffer.h>
0018 #include <linux/iio/trigger_consumer.h>
0019 #include <linux/regulator/consumer.h>
0020 
0021 #define ADC084S021_DRIVER_NAME "adc084s021"
0022 
0023 struct adc084s021 {
0024     struct spi_device *spi;
0025     struct spi_message message;
0026     struct spi_transfer spi_trans;
0027     struct regulator *reg;
0028     struct mutex lock;
0029     /* Buffer used to align data */
0030     struct {
0031         __be16 channels[4];
0032         s64 ts __aligned(8);
0033     } scan;
0034     /*
0035      * DMA (thus cache coherency maintenance) may require the
0036      * transfer buffers to live in their own cache line.
0037      */
0038     u16 tx_buf[4] __aligned(IIO_DMA_MINALIGN);
0039     __be16 rx_buf[5]; /* First 16-bits are trash */
0040 };
0041 
0042 #define ADC084S021_VOLTAGE_CHANNEL(num)                  \
0043     {                                                      \
0044         .type = IIO_VOLTAGE,                                 \
0045         .channel = (num),                                    \
0046         .indexed = 1,                                        \
0047         .scan_index = (num),                                 \
0048         .scan_type = {                                       \
0049             .sign = 'u',                                       \
0050             .realbits = 8,                                     \
0051             .storagebits = 16,                                 \
0052             .shift = 4,                                        \
0053             .endianness = IIO_BE,                              \
0054         },                                                   \
0055         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),        \
0056         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
0057     }
0058 
0059 static const struct iio_chan_spec adc084s021_channels[] = {
0060     ADC084S021_VOLTAGE_CHANNEL(0),
0061     ADC084S021_VOLTAGE_CHANNEL(1),
0062     ADC084S021_VOLTAGE_CHANNEL(2),
0063     ADC084S021_VOLTAGE_CHANNEL(3),
0064     IIO_CHAN_SOFT_TIMESTAMP(4),
0065 };
0066 
0067 /**
0068  * adc084s021_adc_conversion() - Read an ADC channel and return its value.
0069  *
0070  * @adc: The ADC SPI data.
0071  * @data: Buffer for converted data.
0072  */
0073 static int adc084s021_adc_conversion(struct adc084s021 *adc, __be16 *data)
0074 {
0075     int n_words = (adc->spi_trans.len >> 1) - 1; /* Discard first word */
0076     int ret, i = 0;
0077 
0078     /* Do the transfer */
0079     ret = spi_sync(adc->spi, &adc->message);
0080     if (ret < 0)
0081         return ret;
0082 
0083     for (; i < n_words; i++)
0084         *(data + i) = adc->rx_buf[i + 1];
0085 
0086     return ret;
0087 }
0088 
0089 static int adc084s021_read_raw(struct iio_dev *indio_dev,
0090                struct iio_chan_spec const *channel, int *val,
0091                int *val2, long mask)
0092 {
0093     struct adc084s021 *adc = iio_priv(indio_dev);
0094     int ret;
0095     __be16 be_val;
0096 
0097     switch (mask) {
0098     case IIO_CHAN_INFO_RAW:
0099         ret = iio_device_claim_direct_mode(indio_dev);
0100         if (ret < 0)
0101             return ret;
0102 
0103         ret = regulator_enable(adc->reg);
0104         if (ret) {
0105             iio_device_release_direct_mode(indio_dev);
0106             return ret;
0107         }
0108 
0109         adc->tx_buf[0] = channel->channel << 3;
0110         ret = adc084s021_adc_conversion(adc, &be_val);
0111         iio_device_release_direct_mode(indio_dev);
0112         regulator_disable(adc->reg);
0113         if (ret < 0)
0114             return ret;
0115 
0116         *val = be16_to_cpu(be_val);
0117         *val = (*val >> channel->scan_type.shift) & 0xff;
0118 
0119         return IIO_VAL_INT;
0120     case IIO_CHAN_INFO_SCALE:
0121         ret = regulator_enable(adc->reg);
0122         if (ret)
0123             return ret;
0124 
0125         ret = regulator_get_voltage(adc->reg);
0126         regulator_disable(adc->reg);
0127         if (ret < 0)
0128             return ret;
0129 
0130         *val = ret / 1000;
0131 
0132         return IIO_VAL_INT;
0133     default:
0134         return -EINVAL;
0135     }
0136 }
0137 
0138 /**
0139  * adc084s021_buffer_trigger_handler() - Read ADC channels and push to buffer.
0140  *
0141  * @irq: The interrupt number (not used).
0142  * @pollfunc: Pointer to the poll func.
0143  */
0144 static irqreturn_t adc084s021_buffer_trigger_handler(int irq, void *pollfunc)
0145 {
0146     struct iio_poll_func *pf = pollfunc;
0147     struct iio_dev *indio_dev = pf->indio_dev;
0148     struct adc084s021 *adc = iio_priv(indio_dev);
0149 
0150     mutex_lock(&adc->lock);
0151 
0152     if (adc084s021_adc_conversion(adc, adc->scan.channels) < 0)
0153         dev_err(&adc->spi->dev, "Failed to read data\n");
0154 
0155     iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
0156                        iio_get_time_ns(indio_dev));
0157     mutex_unlock(&adc->lock);
0158     iio_trigger_notify_done(indio_dev->trig);
0159 
0160     return IRQ_HANDLED;
0161 }
0162 
0163 static int adc084s021_buffer_preenable(struct iio_dev *indio_dev)
0164 {
0165     struct adc084s021 *adc = iio_priv(indio_dev);
0166     int scan_index;
0167     int i = 0;
0168 
0169     for_each_set_bit(scan_index, indio_dev->active_scan_mask,
0170              indio_dev->masklength) {
0171         const struct iio_chan_spec *channel =
0172             &indio_dev->channels[scan_index];
0173         adc->tx_buf[i++] = channel->channel << 3;
0174     }
0175     adc->spi_trans.len = 2 + (i * sizeof(__be16)); /* Trash + channels */
0176 
0177     return regulator_enable(adc->reg);
0178 }
0179 
0180 static int adc084s021_buffer_postdisable(struct iio_dev *indio_dev)
0181 {
0182     struct adc084s021 *adc = iio_priv(indio_dev);
0183 
0184     adc->spi_trans.len = 4; /* Trash + single channel */
0185 
0186     return regulator_disable(adc->reg);
0187 }
0188 
0189 static const struct iio_info adc084s021_info = {
0190     .read_raw = adc084s021_read_raw,
0191 };
0192 
0193 static const struct iio_buffer_setup_ops adc084s021_buffer_setup_ops = {
0194     .preenable = adc084s021_buffer_preenable,
0195     .postdisable = adc084s021_buffer_postdisable,
0196 };
0197 
0198 static int adc084s021_probe(struct spi_device *spi)
0199 {
0200     struct iio_dev *indio_dev;
0201     struct adc084s021 *adc;
0202     int ret;
0203 
0204     indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
0205     if (!indio_dev) {
0206         dev_err(&spi->dev, "Failed to allocate IIO device\n");
0207         return -ENOMEM;
0208     }
0209 
0210     adc = iio_priv(indio_dev);
0211     adc->spi = spi;
0212 
0213     /* Initiate the Industrial I/O device */
0214     indio_dev->name = spi_get_device_id(spi)->name;
0215     indio_dev->modes = INDIO_DIRECT_MODE;
0216     indio_dev->info = &adc084s021_info;
0217     indio_dev->channels = adc084s021_channels;
0218     indio_dev->num_channels = ARRAY_SIZE(adc084s021_channels);
0219 
0220     /* Create SPI transfer for channel reads */
0221     adc->spi_trans.tx_buf = adc->tx_buf;
0222     adc->spi_trans.rx_buf = adc->rx_buf;
0223     adc->spi_trans.len = 4; /* Trash + single channel */
0224     spi_message_init_with_transfers(&adc->message, &adc->spi_trans, 1);
0225 
0226     adc->reg = devm_regulator_get(&spi->dev, "vref");
0227     if (IS_ERR(adc->reg))
0228         return PTR_ERR(adc->reg);
0229 
0230     mutex_init(&adc->lock);
0231 
0232     /* Setup triggered buffer with pollfunction */
0233     ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
0234                         adc084s021_buffer_trigger_handler,
0235                         &adc084s021_buffer_setup_ops);
0236     if (ret) {
0237         dev_err(&spi->dev, "Failed to setup triggered buffer\n");
0238         return ret;
0239     }
0240 
0241     return devm_iio_device_register(&spi->dev, indio_dev);
0242 }
0243 
0244 static const struct of_device_id adc084s021_of_match[] = {
0245     { .compatible = "ti,adc084s021", },
0246     {},
0247 };
0248 MODULE_DEVICE_TABLE(of, adc084s021_of_match);
0249 
0250 static const struct spi_device_id adc084s021_id[] = {
0251     { ADC084S021_DRIVER_NAME, 0 },
0252     {}
0253 };
0254 MODULE_DEVICE_TABLE(spi, adc084s021_id);
0255 
0256 static struct spi_driver adc084s021_driver = {
0257     .driver = {
0258         .name = ADC084S021_DRIVER_NAME,
0259         .of_match_table = adc084s021_of_match,
0260     },
0261     .probe = adc084s021_probe,
0262     .id_table = adc084s021_id,
0263 };
0264 module_spi_driver(adc084s021_driver);
0265 
0266 MODULE_AUTHOR("MÃ¥rten Lindahl <martenli@axis.com>");
0267 MODULE_DESCRIPTION("Texas Instruments ADC084S021");
0268 MODULE_LICENSE("GPL v2");
0269 MODULE_VERSION("1.0");