1 /* 2 * TI ADC108S102 SPI ADC driver 3 * 4 * Copyright (c) 2013-2015 Intel Corporation. 5 * Copyright (c) 2017 Siemens AG 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms and conditions of the GNU General Public License, 9 * version 2, as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 * more details. 15 * 16 * This IIO device driver is designed to work with the following 17 * analog to digital converters from Texas Instruments: 18 * ADC108S102 19 * ADC128S102 20 * The communication with ADC chip is via the SPI bus (mode 3). 21 */ 22 23 #include <linux/acpi.h> 24 #include <linux/iio/iio.h> 25 #include <linux/iio/buffer.h> 26 #include <linux/iio/types.h> 27 #include <linux/iio/triggered_buffer.h> 28 #include <linux/iio/trigger_consumer.h> 29 #include <linux/interrupt.h> 30 #include <linux/module.h> 31 #include <linux/property.h> 32 #include <linux/regulator/consumer.h> 33 #include <linux/spi/spi.h> 34 35 /* 36 * In case of ACPI, we use the hard-wired 5000 mV of the Galileo and IOT2000 37 * boards as default for the reference pin VA. Device tree users encode that 38 * via the vref-supply regulator. 39 */ 40 #define ADC108S102_VA_MV_ACPI_DEFAULT 5000 41 42 /* 43 * Defining the ADC resolution being 12 bits, we can use the same driver for 44 * both ADC108S102 (10 bits resolution) and ADC128S102 (12 bits resolution) 45 * chips. The ADC108S102 effectively returns a 12-bit result with the 2 46 * least-significant bits unset. 47 */ 48 #define ADC108S102_BITS 12 49 #define ADC108S102_MAX_CHANNELS 8 50 51 /* 52 * 16-bit SPI command format: 53 * [15:14] Ignored 54 * [13:11] 3-bit channel address 55 * [10:0] Ignored 56 */ 57 #define ADC108S102_CMD(ch) ((u16)(ch) << 11) 58 59 /* 60 * 16-bit SPI response format: 61 * [15:12] Zeros 62 * [11:0] 12-bit ADC sample (for ADC108S102, [1:0] will always be 0). 63 */ 64 #define ADC108S102_RES_DATA(res) ((u16)res & GENMASK(11, 0)) 65 66 struct adc108s102_state { 67 struct spi_device *spi; 68 struct regulator *reg; 69 u32 va_millivolt; 70 /* SPI transfer used by triggered buffer handler*/ 71 struct spi_transfer ring_xfer; 72 /* SPI transfer used by direct scan */ 73 struct spi_transfer scan_single_xfer; 74 /* SPI message used by ring_xfer SPI transfer */ 75 struct spi_message ring_msg; 76 /* SPI message used by scan_single_xfer SPI transfer */ 77 struct spi_message scan_single_msg; 78 79 /* 80 * SPI message buffers: 81 * tx_buf: |C0|C1|C2|C3|C4|C5|C6|C7|XX| 82 * rx_buf: |XX|R0|R1|R2|R3|R4|R5|R6|R7|tt|tt|tt|tt| 83 * 84 * tx_buf: 8 channel read commands, plus 1 dummy command 85 * rx_buf: 1 dummy response, 8 channel responses, plus 64-bit timestamp 86 */ 87 __be16 rx_buf[13] ____cacheline_aligned; 88 __be16 tx_buf[9] ____cacheline_aligned; 89 }; 90 91 #define ADC108S102_V_CHAN(index) \ 92 { \ 93 .type = IIO_VOLTAGE, \ 94 .indexed = 1, \ 95 .channel = index, \ 96 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 97 BIT(IIO_CHAN_INFO_SCALE), \ 98 .address = index, \ 99 .scan_index = index, \ 100 .scan_type = { \ 101 .sign = 'u', \ 102 .realbits = ADC108S102_BITS, \ 103 .storagebits = 16, \ 104 .endianness = IIO_BE, \ 105 }, \ 106 } 107 108 static const struct iio_chan_spec adc108s102_channels[] = { 109 ADC108S102_V_CHAN(0), 110 ADC108S102_V_CHAN(1), 111 ADC108S102_V_CHAN(2), 112 ADC108S102_V_CHAN(3), 113 ADC108S102_V_CHAN(4), 114 ADC108S102_V_CHAN(5), 115 ADC108S102_V_CHAN(6), 116 ADC108S102_V_CHAN(7), 117 IIO_CHAN_SOFT_TIMESTAMP(8), 118 }; 119 120 static int adc108s102_update_scan_mode(struct iio_dev *indio_dev, 121 unsigned long const *active_scan_mask) 122 { 123 struct adc108s102_state *st = iio_priv(indio_dev); 124 unsigned int bit, cmds; 125 126 /* 127 * Fill in the first x shorts of tx_buf with the number of channels 128 * enabled for sampling by the triggered buffer. 129 */ 130 cmds = 0; 131 for_each_set_bit(bit, active_scan_mask, ADC108S102_MAX_CHANNELS) 132 st->tx_buf[cmds++] = cpu_to_be16(ADC108S102_CMD(bit)); 133 134 /* One dummy command added, to clock in the last response */ 135 st->tx_buf[cmds++] = 0x00; 136 137 /* build SPI ring message */ 138 st->ring_xfer.tx_buf = &st->tx_buf[0]; 139 st->ring_xfer.rx_buf = &st->rx_buf[0]; 140 st->ring_xfer.len = cmds * sizeof(st->tx_buf[0]); 141 142 spi_message_init_with_transfers(&st->ring_msg, &st->ring_xfer, 1); 143 144 return 0; 145 } 146 147 static irqreturn_t adc108s102_trigger_handler(int irq, void *p) 148 { 149 struct iio_poll_func *pf = p; 150 struct iio_dev *indio_dev = pf->indio_dev; 151 struct adc108s102_state *st = iio_priv(indio_dev); 152 int ret; 153 154 ret = spi_sync(st->spi, &st->ring_msg); 155 if (ret < 0) 156 goto out_notify; 157 158 /* Skip the dummy response in the first slot */ 159 iio_push_to_buffers_with_timestamp(indio_dev, 160 (u8 *)&st->rx_buf[1], 161 iio_get_time_ns(indio_dev)); 162 163 out_notify: 164 iio_trigger_notify_done(indio_dev->trig); 165 166 return IRQ_HANDLED; 167 } 168 169 static int adc108s102_scan_direct(struct adc108s102_state *st, unsigned int ch) 170 { 171 int ret; 172 173 st->tx_buf[0] = cpu_to_be16(ADC108S102_CMD(ch)); 174 ret = spi_sync(st->spi, &st->scan_single_msg); 175 if (ret) 176 return ret; 177 178 /* Skip the dummy response in the first slot */ 179 return be16_to_cpu(st->rx_buf[1]); 180 } 181 182 static int adc108s102_read_raw(struct iio_dev *indio_dev, 183 struct iio_chan_spec const *chan, 184 int *val, int *val2, long m) 185 { 186 struct adc108s102_state *st = iio_priv(indio_dev); 187 int ret; 188 189 switch (m) { 190 case IIO_CHAN_INFO_RAW: 191 ret = iio_device_claim_direct_mode(indio_dev); 192 if (ret) 193 return ret; 194 195 ret = adc108s102_scan_direct(st, chan->address); 196 197 iio_device_release_direct_mode(indio_dev); 198 199 if (ret < 0) 200 return ret; 201 202 *val = ADC108S102_RES_DATA(ret); 203 204 return IIO_VAL_INT; 205 case IIO_CHAN_INFO_SCALE: 206 if (chan->type != IIO_VOLTAGE) 207 break; 208 209 *val = st->va_millivolt; 210 *val2 = chan->scan_type.realbits; 211 212 return IIO_VAL_FRACTIONAL_LOG2; 213 default: 214 break; 215 } 216 217 return -EINVAL; 218 } 219 220 static const struct iio_info adc108s102_info = { 221 .read_raw = &adc108s102_read_raw, 222 .update_scan_mode = &adc108s102_update_scan_mode, 223 .driver_module = THIS_MODULE, 224 }; 225 226 static int adc108s102_probe(struct spi_device *spi) 227 { 228 struct adc108s102_state *st; 229 struct iio_dev *indio_dev; 230 int ret; 231 232 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 233 if (!indio_dev) 234 return -ENOMEM; 235 236 st = iio_priv(indio_dev); 237 238 if (ACPI_COMPANION(&spi->dev)) { 239 st->va_millivolt = ADC108S102_VA_MV_ACPI_DEFAULT; 240 } else { 241 st->reg = devm_regulator_get(&spi->dev, "vref"); 242 if (IS_ERR(st->reg)) 243 return PTR_ERR(st->reg); 244 245 ret = regulator_enable(st->reg); 246 if (ret < 0) { 247 dev_err(&spi->dev, "Cannot enable vref regulator\n"); 248 return ret; 249 } 250 251 ret = regulator_get_voltage(st->reg); 252 if (ret < 0) { 253 dev_err(&spi->dev, "vref get voltage failed\n"); 254 return ret; 255 } 256 257 st->va_millivolt = ret / 1000; 258 } 259 260 spi_set_drvdata(spi, indio_dev); 261 st->spi = spi; 262 263 indio_dev->name = spi->modalias; 264 indio_dev->dev.parent = &spi->dev; 265 indio_dev->modes = INDIO_DIRECT_MODE; 266 indio_dev->channels = adc108s102_channels; 267 indio_dev->num_channels = ARRAY_SIZE(adc108s102_channels); 268 indio_dev->info = &adc108s102_info; 269 270 /* Setup default message */ 271 st->scan_single_xfer.tx_buf = st->tx_buf; 272 st->scan_single_xfer.rx_buf = st->rx_buf; 273 st->scan_single_xfer.len = 2 * sizeof(st->tx_buf[0]); 274 275 spi_message_init_with_transfers(&st->scan_single_msg, 276 &st->scan_single_xfer, 1); 277 278 ret = iio_triggered_buffer_setup(indio_dev, NULL, 279 &adc108s102_trigger_handler, NULL); 280 if (ret) 281 goto error_disable_reg; 282 283 ret = iio_device_register(indio_dev); 284 if (ret) { 285 dev_err(&spi->dev, "Failed to register IIO device\n"); 286 goto error_cleanup_triggered_buffer; 287 } 288 return 0; 289 290 error_cleanup_triggered_buffer: 291 iio_triggered_buffer_cleanup(indio_dev); 292 293 error_disable_reg: 294 regulator_disable(st->reg); 295 296 return ret; 297 } 298 299 static int adc108s102_remove(struct spi_device *spi) 300 { 301 struct iio_dev *indio_dev = spi_get_drvdata(spi); 302 struct adc108s102_state *st = iio_priv(indio_dev); 303 304 iio_device_unregister(indio_dev); 305 iio_triggered_buffer_cleanup(indio_dev); 306 307 regulator_disable(st->reg); 308 309 return 0; 310 } 311 312 #ifdef CONFIG_OF 313 static const struct of_device_id adc108s102_of_match[] = { 314 { .compatible = "ti,adc108s102" }, 315 { } 316 }; 317 MODULE_DEVICE_TABLE(of, adc108s102_of_match); 318 #endif 319 320 #ifdef CONFIG_ACPI 321 static const struct acpi_device_id adc108s102_acpi_ids[] = { 322 { "INT3495", 0 }, 323 { } 324 }; 325 MODULE_DEVICE_TABLE(acpi, adc108s102_acpi_ids); 326 #endif 327 328 static const struct spi_device_id adc108s102_id[] = { 329 { "adc108s102", 0 }, 330 { } 331 }; 332 MODULE_DEVICE_TABLE(spi, adc108s102_id); 333 334 static struct spi_driver adc108s102_driver = { 335 .driver = { 336 .name = "adc108s102", 337 .of_match_table = of_match_ptr(adc108s102_of_match), 338 .acpi_match_table = ACPI_PTR(adc108s102_acpi_ids), 339 }, 340 .probe = adc108s102_probe, 341 .remove = adc108s102_remove, 342 .id_table = adc108s102_id, 343 }; 344 module_spi_driver(adc108s102_driver); 345 346 MODULE_AUTHOR("Bogdan Pricop <bogdan.pricop@emutex.com>"); 347 MODULE_DESCRIPTION("Texas Instruments ADC108S102 and ADC128S102 driver"); 348 MODULE_LICENSE("GPL v2"); 349