Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * TI tlc4541 ADC Driver
0004  *
0005  * Copyright (C) 2017 Phil Reid
0006  *
0007  * Datasheets can be found here:
0008  * https://www.ti.com/lit/gpn/tlc3541
0009  * https://www.ti.com/lit/gpn/tlc4541
0010  *
0011  * The tlc4541 requires 24 clock cycles to start a transfer.
0012  * Conversion then takes 2.94us to complete before data is ready
0013  * Data is returned MSB first.
0014  */
0015 
0016 #include <linux/delay.h>
0017 #include <linux/device.h>
0018 #include <linux/err.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/iio/iio.h>
0021 #include <linux/iio/sysfs.h>
0022 #include <linux/iio/buffer.h>
0023 #include <linux/iio/trigger_consumer.h>
0024 #include <linux/iio/triggered_buffer.h>
0025 #include <linux/kernel.h>
0026 #include <linux/module.h>
0027 #include <linux/mod_devicetable.h>
0028 #include <linux/regulator/consumer.h>
0029 #include <linux/slab.h>
0030 #include <linux/spi/spi.h>
0031 #include <linux/sysfs.h>
0032 
0033 struct tlc4541_state {
0034     struct spi_device               *spi;
0035     struct regulator                *reg;
0036     struct spi_transfer             scan_single_xfer[3];
0037     struct spi_message              scan_single_msg;
0038 
0039     /*
0040      * DMA (thus cache coherency maintenance) may require the
0041      * transfer buffers to live in their own cache lines.
0042      * 2 bytes data + 6 bytes padding + 8 bytes timestamp when
0043      * call iio_push_to_buffers_with_timestamp.
0044      */
0045     __be16                          rx_buf[8] __aligned(IIO_DMA_MINALIGN);
0046 };
0047 
0048 struct tlc4541_chip_info {
0049     const struct iio_chan_spec *channels;
0050     unsigned int num_channels;
0051 };
0052 
0053 enum tlc4541_id {
0054     TLC3541,
0055     TLC4541,
0056 };
0057 
0058 #define TLC4541_V_CHAN(bits, bitshift) {                              \
0059         .type = IIO_VOLTAGE,                                  \
0060         .info_mask_separate       = BIT(IIO_CHAN_INFO_RAW),   \
0061         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
0062         .scan_type = {                                        \
0063             .sign = 'u',                                  \
0064             .realbits = (bits),                           \
0065             .storagebits = 16,                            \
0066             .shift = (bitshift),                          \
0067             .endianness = IIO_BE,                         \
0068         },                                                    \
0069     }
0070 
0071 #define DECLARE_TLC4541_CHANNELS(name, bits, bitshift) \
0072 const struct iio_chan_spec name ## _channels[] = { \
0073     TLC4541_V_CHAN(bits, bitshift), \
0074     IIO_CHAN_SOFT_TIMESTAMP(1), \
0075 }
0076 
0077 static DECLARE_TLC4541_CHANNELS(tlc3541, 14, 2);
0078 static DECLARE_TLC4541_CHANNELS(tlc4541, 16, 0);
0079 
0080 static const struct tlc4541_chip_info tlc4541_chip_info[] = {
0081     [TLC3541] = {
0082         .channels = tlc3541_channels,
0083         .num_channels = ARRAY_SIZE(tlc3541_channels),
0084     },
0085     [TLC4541] = {
0086         .channels = tlc4541_channels,
0087         .num_channels = ARRAY_SIZE(tlc4541_channels),
0088     },
0089 };
0090 
0091 static irqreturn_t tlc4541_trigger_handler(int irq, void *p)
0092 {
0093     struct iio_poll_func *pf = p;
0094     struct iio_dev *indio_dev = pf->indio_dev;
0095     struct tlc4541_state *st = iio_priv(indio_dev);
0096     int ret;
0097 
0098     ret = spi_sync(st->spi, &st->scan_single_msg);
0099     if (ret < 0)
0100         goto done;
0101 
0102     iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
0103                        iio_get_time_ns(indio_dev));
0104 
0105 done:
0106     iio_trigger_notify_done(indio_dev->trig);
0107     return IRQ_HANDLED;
0108 }
0109 
0110 static int tlc4541_get_range(struct tlc4541_state *st)
0111 {
0112     int vref;
0113 
0114     vref = regulator_get_voltage(st->reg);
0115     if (vref < 0)
0116         return vref;
0117 
0118     vref /= 1000;
0119 
0120     return vref;
0121 }
0122 
0123 static int tlc4541_read_raw(struct iio_dev *indio_dev,
0124                 struct iio_chan_spec const *chan,
0125                 int *val,
0126                 int *val2,
0127                 long m)
0128 {
0129     int ret = 0;
0130     struct tlc4541_state *st = iio_priv(indio_dev);
0131 
0132     switch (m) {
0133     case IIO_CHAN_INFO_RAW:
0134         ret = iio_device_claim_direct_mode(indio_dev);
0135         if (ret)
0136             return ret;
0137         ret = spi_sync(st->spi, &st->scan_single_msg);
0138         iio_device_release_direct_mode(indio_dev);
0139         if (ret < 0)
0140             return ret;
0141         *val = be16_to_cpu(st->rx_buf[0]);
0142         *val = *val >> chan->scan_type.shift;
0143         *val &= GENMASK(chan->scan_type.realbits - 1, 0);
0144         return IIO_VAL_INT;
0145     case IIO_CHAN_INFO_SCALE:
0146         ret = tlc4541_get_range(st);
0147         if (ret < 0)
0148             return ret;
0149         *val = ret;
0150         *val2 = chan->scan_type.realbits;
0151         return IIO_VAL_FRACTIONAL_LOG2;
0152     }
0153     return -EINVAL;
0154 }
0155 
0156 static const struct iio_info tlc4541_info = {
0157     .read_raw = &tlc4541_read_raw,
0158 };
0159 
0160 static int tlc4541_probe(struct spi_device *spi)
0161 {
0162     struct tlc4541_state *st;
0163     struct iio_dev *indio_dev;
0164     const struct tlc4541_chip_info *info;
0165     int ret;
0166     int8_t device_init = 0;
0167 
0168     indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
0169     if (indio_dev == NULL)
0170         return -ENOMEM;
0171 
0172     st = iio_priv(indio_dev);
0173 
0174     spi_set_drvdata(spi, indio_dev);
0175 
0176     st->spi = spi;
0177 
0178     info = &tlc4541_chip_info[spi_get_device_id(spi)->driver_data];
0179 
0180     indio_dev->name = spi_get_device_id(spi)->name;
0181     indio_dev->modes = INDIO_DIRECT_MODE;
0182     indio_dev->channels = info->channels;
0183     indio_dev->num_channels = info->num_channels;
0184     indio_dev->info = &tlc4541_info;
0185 
0186     /* perform reset */
0187     spi_write(spi, &device_init, 1);
0188 
0189     /* Setup default message */
0190     st->scan_single_xfer[0].rx_buf = &st->rx_buf[0];
0191     st->scan_single_xfer[0].len = 3;
0192     st->scan_single_xfer[1].delay.value = 3;
0193     st->scan_single_xfer[1].delay.unit = SPI_DELAY_UNIT_NSECS;
0194     st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
0195     st->scan_single_xfer[2].len = 2;
0196 
0197     spi_message_init_with_transfers(&st->scan_single_msg,
0198                     st->scan_single_xfer, 3);
0199 
0200     st->reg = devm_regulator_get(&spi->dev, "vref");
0201     if (IS_ERR(st->reg))
0202         return PTR_ERR(st->reg);
0203 
0204     ret = regulator_enable(st->reg);
0205     if (ret)
0206         return ret;
0207 
0208     ret = iio_triggered_buffer_setup(indio_dev, NULL,
0209             &tlc4541_trigger_handler, NULL);
0210     if (ret)
0211         goto error_disable_reg;
0212 
0213     ret = iio_device_register(indio_dev);
0214     if (ret)
0215         goto error_cleanup_buffer;
0216 
0217     return 0;
0218 
0219 error_cleanup_buffer:
0220     iio_triggered_buffer_cleanup(indio_dev);
0221 error_disable_reg:
0222     regulator_disable(st->reg);
0223 
0224     return ret;
0225 }
0226 
0227 static void tlc4541_remove(struct spi_device *spi)
0228 {
0229     struct iio_dev *indio_dev = spi_get_drvdata(spi);
0230     struct tlc4541_state *st = iio_priv(indio_dev);
0231 
0232     iio_device_unregister(indio_dev);
0233     iio_triggered_buffer_cleanup(indio_dev);
0234     regulator_disable(st->reg);
0235 }
0236 
0237 static const struct of_device_id tlc4541_dt_ids[] = {
0238     { .compatible = "ti,tlc3541", },
0239     { .compatible = "ti,tlc4541", },
0240     {}
0241 };
0242 MODULE_DEVICE_TABLE(of, tlc4541_dt_ids);
0243 
0244 static const struct spi_device_id tlc4541_id[] = {
0245     {"tlc3541", TLC3541},
0246     {"tlc4541", TLC4541},
0247     {}
0248 };
0249 MODULE_DEVICE_TABLE(spi, tlc4541_id);
0250 
0251 static struct spi_driver tlc4541_driver = {
0252     .driver = {
0253         .name   = "tlc4541",
0254         .of_match_table = tlc4541_dt_ids,
0255     },
0256     .probe          = tlc4541_probe,
0257     .remove         = tlc4541_remove,
0258     .id_table       = tlc4541_id,
0259 };
0260 module_spi_driver(tlc4541_driver);
0261 
0262 MODULE_AUTHOR("Phil Reid <preid@electromag.com.au>");
0263 MODULE_DESCRIPTION("Texas Instruments TLC4541 ADC");
0264 MODULE_LICENSE("GPL v2");