1 // SPDX-License-Identifier: GPL-2.0-only 2 /** 3 * Copyright (C) 2017 Axis Communications AB 4 * 5 * Driver for Texas Instruments' ADC084S021 ADC chip. 6 * Datasheets can be found here: 7 * https://www.ti.com/lit/ds/symlink/adc084s021.pdf 8 */ 9 10 #include <linux/err.h> 11 #include <linux/spi/spi.h> 12 #include <linux/module.h> 13 #include <linux/mod_devicetable.h> 14 #include <linux/interrupt.h> 15 #include <linux/iio/iio.h> 16 #include <linux/iio/buffer.h> 17 #include <linux/iio/triggered_buffer.h> 18 #include <linux/iio/trigger_consumer.h> 19 #include <linux/regulator/consumer.h> 20 21 #define ADC084S021_DRIVER_NAME "adc084s021" 22 23 struct adc084s021 { 24 struct spi_device *spi; 25 struct spi_message message; 26 struct spi_transfer spi_trans; 27 struct regulator *reg; 28 struct mutex lock; 29 /* 30 * DMA (thus cache coherency maintenance) requires the 31 * transfer buffers to live in their own cache line. 32 */ 33 u16 tx_buf[4] ____cacheline_aligned; 34 __be16 rx_buf[5]; /* First 16-bits are trash */ 35 }; 36 37 #define ADC084S021_VOLTAGE_CHANNEL(num) \ 38 { \ 39 .type = IIO_VOLTAGE, \ 40 .channel = (num), \ 41 .indexed = 1, \ 42 .scan_index = (num), \ 43 .scan_type = { \ 44 .sign = 'u', \ 45 .realbits = 8, \ 46 .storagebits = 16, \ 47 .shift = 4, \ 48 .endianness = IIO_BE, \ 49 }, \ 50 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 51 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\ 52 } 53 54 static const struct iio_chan_spec adc084s021_channels[] = { 55 ADC084S021_VOLTAGE_CHANNEL(0), 56 ADC084S021_VOLTAGE_CHANNEL(1), 57 ADC084S021_VOLTAGE_CHANNEL(2), 58 ADC084S021_VOLTAGE_CHANNEL(3), 59 IIO_CHAN_SOFT_TIMESTAMP(4), 60 }; 61 62 /** 63 * Read an ADC channel and return its value. 64 * 65 * @adc: The ADC SPI data. 66 * @data: Buffer for converted data. 67 */ 68 static int adc084s021_adc_conversion(struct adc084s021 *adc, void *data) 69 { 70 int n_words = (adc->spi_trans.len >> 1) - 1; /* Discard first word */ 71 int ret, i = 0; 72 u16 *p = data; 73 74 /* Do the transfer */ 75 ret = spi_sync(adc->spi, &adc->message); 76 if (ret < 0) 77 return ret; 78 79 for (; i < n_words; i++) 80 *(p + i) = adc->rx_buf[i + 1]; 81 82 return ret; 83 } 84 85 static int adc084s021_read_raw(struct iio_dev *indio_dev, 86 struct iio_chan_spec const *channel, int *val, 87 int *val2, long mask) 88 { 89 struct adc084s021 *adc = iio_priv(indio_dev); 90 int ret; 91 92 switch (mask) { 93 case IIO_CHAN_INFO_RAW: 94 ret = iio_device_claim_direct_mode(indio_dev); 95 if (ret < 0) 96 return ret; 97 98 ret = regulator_enable(adc->reg); 99 if (ret) { 100 iio_device_release_direct_mode(indio_dev); 101 return ret; 102 } 103 104 adc->tx_buf[0] = channel->channel << 3; 105 ret = adc084s021_adc_conversion(adc, val); 106 iio_device_release_direct_mode(indio_dev); 107 regulator_disable(adc->reg); 108 if (ret < 0) 109 return ret; 110 111 *val = be16_to_cpu(*val); 112 *val = (*val >> channel->scan_type.shift) & 0xff; 113 114 return IIO_VAL_INT; 115 case IIO_CHAN_INFO_SCALE: 116 ret = regulator_enable(adc->reg); 117 if (ret) 118 return ret; 119 120 ret = regulator_get_voltage(adc->reg); 121 regulator_disable(adc->reg); 122 if (ret < 0) 123 return ret; 124 125 *val = ret / 1000; 126 127 return IIO_VAL_INT; 128 default: 129 return -EINVAL; 130 } 131 } 132 133 /** 134 * Read enabled ADC channels and push data to the buffer. 135 * 136 * @irq: The interrupt number (not used). 137 * @pollfunc: Pointer to the poll func. 138 */ 139 static irqreturn_t adc084s021_buffer_trigger_handler(int irq, void *pollfunc) 140 { 141 struct iio_poll_func *pf = pollfunc; 142 struct iio_dev *indio_dev = pf->indio_dev; 143 struct adc084s021 *adc = iio_priv(indio_dev); 144 __be16 data[8] = {0}; /* 4 * 16-bit words of data + 8 bytes timestamp */ 145 146 mutex_lock(&adc->lock); 147 148 if (adc084s021_adc_conversion(adc, &data) < 0) 149 dev_err(&adc->spi->dev, "Failed to read data\n"); 150 151 iio_push_to_buffers_with_timestamp(indio_dev, data, 152 iio_get_time_ns(indio_dev)); 153 mutex_unlock(&adc->lock); 154 iio_trigger_notify_done(indio_dev->trig); 155 156 return IRQ_HANDLED; 157 } 158 159 static int adc084s021_buffer_preenable(struct iio_dev *indio_dev) 160 { 161 struct adc084s021 *adc = iio_priv(indio_dev); 162 int scan_index; 163 int i = 0; 164 165 for_each_set_bit(scan_index, indio_dev->active_scan_mask, 166 indio_dev->masklength) { 167 const struct iio_chan_spec *channel = 168 &indio_dev->channels[scan_index]; 169 adc->tx_buf[i++] = channel->channel << 3; 170 } 171 adc->spi_trans.len = 2 + (i * sizeof(__be16)); /* Trash + channels */ 172 173 return regulator_enable(adc->reg); 174 } 175 176 static int adc084s021_buffer_postdisable(struct iio_dev *indio_dev) 177 { 178 struct adc084s021 *adc = iio_priv(indio_dev); 179 180 adc->spi_trans.len = 4; /* Trash + single channel */ 181 182 return regulator_disable(adc->reg); 183 } 184 185 static const struct iio_info adc084s021_info = { 186 .read_raw = adc084s021_read_raw, 187 }; 188 189 static const struct iio_buffer_setup_ops adc084s021_buffer_setup_ops = { 190 .preenable = adc084s021_buffer_preenable, 191 .postdisable = adc084s021_buffer_postdisable, 192 }; 193 194 static int adc084s021_probe(struct spi_device *spi) 195 { 196 struct iio_dev *indio_dev; 197 struct adc084s021 *adc; 198 int ret; 199 200 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); 201 if (!indio_dev) { 202 dev_err(&spi->dev, "Failed to allocate IIO device\n"); 203 return -ENOMEM; 204 } 205 206 adc = iio_priv(indio_dev); 207 adc->spi = spi; 208 209 /* Connect the SPI device and the iio dev */ 210 spi_set_drvdata(spi, indio_dev); 211 212 /* Initiate the Industrial I/O device */ 213 indio_dev->name = spi_get_device_id(spi)->name; 214 indio_dev->modes = INDIO_DIRECT_MODE; 215 indio_dev->info = &adc084s021_info; 216 indio_dev->channels = adc084s021_channels; 217 indio_dev->num_channels = ARRAY_SIZE(adc084s021_channels); 218 219 /* Create SPI transfer for channel reads */ 220 adc->spi_trans.tx_buf = adc->tx_buf; 221 adc->spi_trans.rx_buf = adc->rx_buf; 222 adc->spi_trans.len = 4; /* Trash + single channel */ 223 spi_message_init_with_transfers(&adc->message, &adc->spi_trans, 1); 224 225 adc->reg = devm_regulator_get(&spi->dev, "vref"); 226 if (IS_ERR(adc->reg)) 227 return PTR_ERR(adc->reg); 228 229 mutex_init(&adc->lock); 230 231 /* Setup triggered buffer with pollfunction */ 232 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL, 233 adc084s021_buffer_trigger_handler, 234 &adc084s021_buffer_setup_ops); 235 if (ret) { 236 dev_err(&spi->dev, "Failed to setup triggered buffer\n"); 237 return ret; 238 } 239 240 return devm_iio_device_register(&spi->dev, indio_dev); 241 } 242 243 static const struct of_device_id adc084s021_of_match[] = { 244 { .compatible = "ti,adc084s021", }, 245 {}, 246 }; 247 MODULE_DEVICE_TABLE(of, adc084s021_of_match); 248 249 static const struct spi_device_id adc084s021_id[] = { 250 { ADC084S021_DRIVER_NAME, 0}, 251 {} 252 }; 253 MODULE_DEVICE_TABLE(spi, adc084s021_id); 254 255 static struct spi_driver adc084s021_driver = { 256 .driver = { 257 .name = ADC084S021_DRIVER_NAME, 258 .of_match_table = adc084s021_of_match, 259 }, 260 .probe = adc084s021_probe, 261 .id_table = adc084s021_id, 262 }; 263 module_spi_driver(adc084s021_driver); 264 265 MODULE_AUTHOR("Mårten Lindahl <martenli@axis.com>"); 266 MODULE_DESCRIPTION("Texas Instruments ADC084S021"); 267 MODULE_LICENSE("GPL v2"); 268 MODULE_VERSION("1.0"); 269