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 }; 224 225 static int adc108s102_probe(struct spi_device *spi) 226 { 227 struct adc108s102_state *st; 228 struct iio_dev *indio_dev; 229 int ret; 230 231 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 232 if (!indio_dev) 233 return -ENOMEM; 234 235 st = iio_priv(indio_dev); 236 237 if (ACPI_COMPANION(&spi->dev)) { 238 st->va_millivolt = ADC108S102_VA_MV_ACPI_DEFAULT; 239 } else { 240 st->reg = devm_regulator_get(&spi->dev, "vref"); 241 if (IS_ERR(st->reg)) 242 return PTR_ERR(st->reg); 243 244 ret = regulator_enable(st->reg); 245 if (ret < 0) { 246 dev_err(&spi->dev, "Cannot enable vref regulator\n"); 247 return ret; 248 } 249 250 ret = regulator_get_voltage(st->reg); 251 if (ret < 0) { 252 dev_err(&spi->dev, "vref get voltage failed\n"); 253 return ret; 254 } 255 256 st->va_millivolt = ret / 1000; 257 } 258 259 spi_set_drvdata(spi, indio_dev); 260 st->spi = spi; 261 262 indio_dev->name = spi->modalias; 263 indio_dev->dev.parent = &spi->dev; 264 indio_dev->modes = INDIO_DIRECT_MODE; 265 indio_dev->channels = adc108s102_channels; 266 indio_dev->num_channels = ARRAY_SIZE(adc108s102_channels); 267 indio_dev->info = &adc108s102_info; 268 269 /* Setup default message */ 270 st->scan_single_xfer.tx_buf = st->tx_buf; 271 st->scan_single_xfer.rx_buf = st->rx_buf; 272 st->scan_single_xfer.len = 2 * sizeof(st->tx_buf[0]); 273 274 spi_message_init_with_transfers(&st->scan_single_msg, 275 &st->scan_single_xfer, 1); 276 277 ret = iio_triggered_buffer_setup(indio_dev, NULL, 278 &adc108s102_trigger_handler, NULL); 279 if (ret) 280 goto error_disable_reg; 281 282 ret = iio_device_register(indio_dev); 283 if (ret) { 284 dev_err(&spi->dev, "Failed to register IIO device\n"); 285 goto error_cleanup_triggered_buffer; 286 } 287 return 0; 288 289 error_cleanup_triggered_buffer: 290 iio_triggered_buffer_cleanup(indio_dev); 291 292 error_disable_reg: 293 regulator_disable(st->reg); 294 295 return ret; 296 } 297 298 static int adc108s102_remove(struct spi_device *spi) 299 { 300 struct iio_dev *indio_dev = spi_get_drvdata(spi); 301 struct adc108s102_state *st = iio_priv(indio_dev); 302 303 iio_device_unregister(indio_dev); 304 iio_triggered_buffer_cleanup(indio_dev); 305 306 regulator_disable(st->reg); 307 308 return 0; 309 } 310 311 #ifdef CONFIG_OF 312 static const struct of_device_id adc108s102_of_match[] = { 313 { .compatible = "ti,adc108s102" }, 314 { } 315 }; 316 MODULE_DEVICE_TABLE(of, adc108s102_of_match); 317 #endif 318 319 #ifdef CONFIG_ACPI 320 static const struct acpi_device_id adc108s102_acpi_ids[] = { 321 { "INT3495", 0 }, 322 { } 323 }; 324 MODULE_DEVICE_TABLE(acpi, adc108s102_acpi_ids); 325 #endif 326 327 static const struct spi_device_id adc108s102_id[] = { 328 { "adc108s102", 0 }, 329 { } 330 }; 331 MODULE_DEVICE_TABLE(spi, adc108s102_id); 332 333 static struct spi_driver adc108s102_driver = { 334 .driver = { 335 .name = "adc108s102", 336 .of_match_table = of_match_ptr(adc108s102_of_match), 337 .acpi_match_table = ACPI_PTR(adc108s102_acpi_ids), 338 }, 339 .probe = adc108s102_probe, 340 .remove = adc108s102_remove, 341 .id_table = adc108s102_id, 342 }; 343 module_spi_driver(adc108s102_driver); 344 345 MODULE_AUTHOR("Bogdan Pricop <bogdan.pricop@emutex.com>"); 346 MODULE_DESCRIPTION("Texas Instruments ADC108S102 and ADC128S102 driver"); 347 MODULE_LICENSE("GPL v2"); 348