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