1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments ADS7950 SPI ADC driver
5 * Copyright 2016 David Lechner <david@lechnology.com>
7 * Based on iio/ad7923.c:
8 * Copyright 2011 Analog Devices Inc
9 * Copyright 2012 CS Systemes d'Information
11 * And also on hwmon/ads79xx.c
12 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
16 #include <linux/acpi.h>
17 #include <linux/bitops.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/spi/spi.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/triggered_buffer.h>
34 * In case of ACPI, we use the 5000 mV as default for the reference pin.
35 * Device tree users encode that via the vref-supply regulator.
37 #define TI_ADS7950_VA_MV_ACPI_DEFAULT 5000
39 #define TI_ADS7950_CR_MANUAL BIT(12)
40 #define TI_ADS7950_CR_WRITE BIT(11)
41 #define TI_ADS7950_CR_CHAN(ch) ((ch) << 7)
42 #define TI_ADS7950_CR_RANGE_5V BIT(6)
44 #define TI_ADS7950_MAX_CHAN 16
46 #define TI_ADS7950_TIMESTAMP_SIZE (sizeof(int64_t) / sizeof(__be16))
48 /* val = value, dec = left shift, bits = number of bits of the mask */
49 #define TI_ADS7950_EXTRACT(val, dec, bits) \
50 (((val) >> (dec)) & ((1 << (bits)) - 1))
52 struct ti_ads7950_state
{
53 struct spi_device
*spi
;
54 struct spi_transfer ring_xfer
;
55 struct spi_transfer scan_single_xfer
[3];
56 struct spi_message ring_msg
;
57 struct spi_message scan_single_msg
;
59 struct regulator
*reg
;
62 unsigned int settings
;
65 * DMA (thus cache coherency maintenance) requires the
66 * transfer buffers to live in their own cache lines.
68 u16 rx_buf
[TI_ADS7950_MAX_CHAN
+ 2 + TI_ADS7950_TIMESTAMP_SIZE
]
69 ____cacheline_aligned
;
70 u16 tx_buf
[TI_ADS7950_MAX_CHAN
+ 2];
76 struct ti_ads7950_chip_info
{
77 const struct iio_chan_spec
*channels
;
78 unsigned int num_channels
;
96 #define TI_ADS7950_V_CHAN(index, bits) \
98 .type = IIO_VOLTAGE, \
101 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
102 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
104 .datasheet_name = "CH##index", \
105 .scan_index = index, \
110 .shift = 12 - (bits), \
111 .endianness = IIO_CPU, \
115 #define DECLARE_TI_ADS7950_4_CHANNELS(name, bits) \
116 const struct iio_chan_spec name ## _channels[] = { \
117 TI_ADS7950_V_CHAN(0, bits), \
118 TI_ADS7950_V_CHAN(1, bits), \
119 TI_ADS7950_V_CHAN(2, bits), \
120 TI_ADS7950_V_CHAN(3, bits), \
121 IIO_CHAN_SOFT_TIMESTAMP(4), \
124 #define DECLARE_TI_ADS7950_8_CHANNELS(name, bits) \
125 const struct iio_chan_spec name ## _channels[] = { \
126 TI_ADS7950_V_CHAN(0, bits), \
127 TI_ADS7950_V_CHAN(1, bits), \
128 TI_ADS7950_V_CHAN(2, bits), \
129 TI_ADS7950_V_CHAN(3, bits), \
130 TI_ADS7950_V_CHAN(4, bits), \
131 TI_ADS7950_V_CHAN(5, bits), \
132 TI_ADS7950_V_CHAN(6, bits), \
133 TI_ADS7950_V_CHAN(7, bits), \
134 IIO_CHAN_SOFT_TIMESTAMP(8), \
137 #define DECLARE_TI_ADS7950_12_CHANNELS(name, bits) \
138 const struct iio_chan_spec name ## _channels[] = { \
139 TI_ADS7950_V_CHAN(0, bits), \
140 TI_ADS7950_V_CHAN(1, bits), \
141 TI_ADS7950_V_CHAN(2, bits), \
142 TI_ADS7950_V_CHAN(3, bits), \
143 TI_ADS7950_V_CHAN(4, bits), \
144 TI_ADS7950_V_CHAN(5, bits), \
145 TI_ADS7950_V_CHAN(6, bits), \
146 TI_ADS7950_V_CHAN(7, bits), \
147 TI_ADS7950_V_CHAN(8, bits), \
148 TI_ADS7950_V_CHAN(9, bits), \
149 TI_ADS7950_V_CHAN(10, bits), \
150 TI_ADS7950_V_CHAN(11, bits), \
151 IIO_CHAN_SOFT_TIMESTAMP(12), \
154 #define DECLARE_TI_ADS7950_16_CHANNELS(name, bits) \
155 const struct iio_chan_spec name ## _channels[] = { \
156 TI_ADS7950_V_CHAN(0, bits), \
157 TI_ADS7950_V_CHAN(1, bits), \
158 TI_ADS7950_V_CHAN(2, bits), \
159 TI_ADS7950_V_CHAN(3, bits), \
160 TI_ADS7950_V_CHAN(4, bits), \
161 TI_ADS7950_V_CHAN(5, bits), \
162 TI_ADS7950_V_CHAN(6, bits), \
163 TI_ADS7950_V_CHAN(7, bits), \
164 TI_ADS7950_V_CHAN(8, bits), \
165 TI_ADS7950_V_CHAN(9, bits), \
166 TI_ADS7950_V_CHAN(10, bits), \
167 TI_ADS7950_V_CHAN(11, bits), \
168 TI_ADS7950_V_CHAN(12, bits), \
169 TI_ADS7950_V_CHAN(13, bits), \
170 TI_ADS7950_V_CHAN(14, bits), \
171 TI_ADS7950_V_CHAN(15, bits), \
172 IIO_CHAN_SOFT_TIMESTAMP(16), \
175 static DECLARE_TI_ADS7950_4_CHANNELS(ti_ads7950
, 12);
176 static DECLARE_TI_ADS7950_8_CHANNELS(ti_ads7951
, 12);
177 static DECLARE_TI_ADS7950_12_CHANNELS(ti_ads7952
, 12);
178 static DECLARE_TI_ADS7950_16_CHANNELS(ti_ads7953
, 12);
179 static DECLARE_TI_ADS7950_4_CHANNELS(ti_ads7954
, 10);
180 static DECLARE_TI_ADS7950_8_CHANNELS(ti_ads7955
, 10);
181 static DECLARE_TI_ADS7950_12_CHANNELS(ti_ads7956
, 10);
182 static DECLARE_TI_ADS7950_16_CHANNELS(ti_ads7957
, 10);
183 static DECLARE_TI_ADS7950_4_CHANNELS(ti_ads7958
, 8);
184 static DECLARE_TI_ADS7950_8_CHANNELS(ti_ads7959
, 8);
185 static DECLARE_TI_ADS7950_12_CHANNELS(ti_ads7960
, 8);
186 static DECLARE_TI_ADS7950_16_CHANNELS(ti_ads7961
, 8);
188 static const struct ti_ads7950_chip_info ti_ads7950_chip_info
[] = {
190 .channels
= ti_ads7950_channels
,
191 .num_channels
= ARRAY_SIZE(ti_ads7950_channels
),
194 .channels
= ti_ads7951_channels
,
195 .num_channels
= ARRAY_SIZE(ti_ads7951_channels
),
198 .channels
= ti_ads7952_channels
,
199 .num_channels
= ARRAY_SIZE(ti_ads7952_channels
),
202 .channels
= ti_ads7953_channels
,
203 .num_channels
= ARRAY_SIZE(ti_ads7953_channels
),
206 .channels
= ti_ads7954_channels
,
207 .num_channels
= ARRAY_SIZE(ti_ads7954_channels
),
210 .channels
= ti_ads7955_channels
,
211 .num_channels
= ARRAY_SIZE(ti_ads7955_channels
),
214 .channels
= ti_ads7956_channels
,
215 .num_channels
= ARRAY_SIZE(ti_ads7956_channels
),
218 .channels
= ti_ads7957_channels
,
219 .num_channels
= ARRAY_SIZE(ti_ads7957_channels
),
222 .channels
= ti_ads7958_channels
,
223 .num_channels
= ARRAY_SIZE(ti_ads7958_channels
),
226 .channels
= ti_ads7959_channels
,
227 .num_channels
= ARRAY_SIZE(ti_ads7959_channels
),
230 .channels
= ti_ads7960_channels
,
231 .num_channels
= ARRAY_SIZE(ti_ads7960_channels
),
234 .channels
= ti_ads7961_channels
,
235 .num_channels
= ARRAY_SIZE(ti_ads7961_channels
),
240 * ti_ads7950_update_scan_mode() setup the spi transfer buffer for the new
243 static int ti_ads7950_update_scan_mode(struct iio_dev
*indio_dev
,
244 const unsigned long *active_scan_mask
)
246 struct ti_ads7950_state
*st
= iio_priv(indio_dev
);
250 for_each_set_bit(i
, active_scan_mask
, indio_dev
->num_channels
) {
251 cmd
= TI_ADS7950_CR_WRITE
| TI_ADS7950_CR_CHAN(i
) | st
->settings
;
252 st
->tx_buf
[len
++] = cmd
;
255 /* Data for the 1st channel is not returned until the 3rd transfer */
256 st
->tx_buf
[len
++] = 0;
257 st
->tx_buf
[len
++] = 0;
259 st
->ring_xfer
.len
= len
* 2;
264 static irqreturn_t
ti_ads7950_trigger_handler(int irq
, void *p
)
266 struct iio_poll_func
*pf
= p
;
267 struct iio_dev
*indio_dev
= pf
->indio_dev
;
268 struct ti_ads7950_state
*st
= iio_priv(indio_dev
);
271 ret
= spi_sync(st
->spi
, &st
->ring_msg
);
275 iio_push_to_buffers_with_timestamp(indio_dev
, &st
->rx_buf
[2],
276 iio_get_time_ns(indio_dev
));
279 iio_trigger_notify_done(indio_dev
->trig
);
284 static int ti_ads7950_scan_direct(struct iio_dev
*indio_dev
, unsigned int ch
)
286 struct ti_ads7950_state
*st
= iio_priv(indio_dev
);
289 mutex_lock(&indio_dev
->mlock
);
291 cmd
= TI_ADS7950_CR_WRITE
| TI_ADS7950_CR_CHAN(ch
) | st
->settings
;
294 ret
= spi_sync(st
->spi
, &st
->scan_single_msg
);
301 mutex_unlock(&indio_dev
->mlock
);
306 static int ti_ads7950_get_range(struct ti_ads7950_state
*st
)
313 vref
= regulator_get_voltage(st
->reg
);
320 if (st
->settings
& TI_ADS7950_CR_RANGE_5V
)
326 static int ti_ads7950_read_raw(struct iio_dev
*indio_dev
,
327 struct iio_chan_spec
const *chan
,
328 int *val
, int *val2
, long m
)
330 struct ti_ads7950_state
*st
= iio_priv(indio_dev
);
334 case IIO_CHAN_INFO_RAW
:
335 ret
= ti_ads7950_scan_direct(indio_dev
, chan
->address
);
339 if (chan
->address
!= TI_ADS7950_EXTRACT(ret
, 12, 4))
342 *val
= TI_ADS7950_EXTRACT(ret
, chan
->scan_type
.shift
,
343 chan
->scan_type
.realbits
);
346 case IIO_CHAN_INFO_SCALE
:
347 ret
= ti_ads7950_get_range(st
);
352 *val2
= (1 << chan
->scan_type
.realbits
) - 1;
354 return IIO_VAL_FRACTIONAL
;
360 static const struct iio_info ti_ads7950_info
= {
361 .read_raw
= &ti_ads7950_read_raw
,
362 .update_scan_mode
= ti_ads7950_update_scan_mode
,
365 static int ti_ads7950_probe(struct spi_device
*spi
)
367 struct ti_ads7950_state
*st
;
368 struct iio_dev
*indio_dev
;
369 const struct ti_ads7950_chip_info
*info
;
372 spi
->bits_per_word
= 16;
373 spi
->mode
|= SPI_CS_WORD
;
374 ret
= spi_setup(spi
);
376 dev_err(&spi
->dev
, "Error in spi setup\n");
380 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
384 st
= iio_priv(indio_dev
);
386 spi_set_drvdata(spi
, indio_dev
);
389 st
->settings
= TI_ADS7950_CR_MANUAL
| TI_ADS7950_CR_RANGE_5V
;
391 info
= &ti_ads7950_chip_info
[spi_get_device_id(spi
)->driver_data
];
393 indio_dev
->name
= spi_get_device_id(spi
)->name
;
394 indio_dev
->dev
.parent
= &spi
->dev
;
395 indio_dev
->modes
= INDIO_DIRECT_MODE
;
396 indio_dev
->channels
= info
->channels
;
397 indio_dev
->num_channels
= info
->num_channels
;
398 indio_dev
->info
= &ti_ads7950_info
;
400 /* build spi ring message */
401 spi_message_init(&st
->ring_msg
);
403 st
->ring_xfer
.tx_buf
= &st
->tx_buf
[0];
404 st
->ring_xfer
.rx_buf
= &st
->rx_buf
[0];
405 /* len will be set later */
406 st
->ring_xfer
.cs_change
= true;
408 spi_message_add_tail(&st
->ring_xfer
, &st
->ring_msg
);
411 * Setup default message. The sample is read at the end of the first
412 * transfer, then it takes one full cycle to convert the sample and one
413 * more cycle to send the value. The conversion process is driven by
414 * the SPI clock, which is why we have 3 transfers. The middle one is
415 * just dummy data sent while the chip is converting the sample that
416 * was read at the end of the first transfer.
419 st
->scan_single_xfer
[0].tx_buf
= &st
->single_tx
;
420 st
->scan_single_xfer
[0].len
= 2;
421 st
->scan_single_xfer
[0].cs_change
= 1;
422 st
->scan_single_xfer
[1].tx_buf
= &st
->single_tx
;
423 st
->scan_single_xfer
[1].len
= 2;
424 st
->scan_single_xfer
[1].cs_change
= 1;
425 st
->scan_single_xfer
[2].rx_buf
= &st
->single_rx
;
426 st
->scan_single_xfer
[2].len
= 2;
428 spi_message_init_with_transfers(&st
->scan_single_msg
,
429 st
->scan_single_xfer
, 3);
431 /* Use hard coded value for reference voltage in ACPI case */
432 if (ACPI_COMPANION(&spi
->dev
))
433 st
->vref_mv
= TI_ADS7950_VA_MV_ACPI_DEFAULT
;
435 st
->reg
= devm_regulator_get(&spi
->dev
, "vref");
436 if (IS_ERR(st
->reg
)) {
437 dev_err(&spi
->dev
, "Failed get get regulator \"vref\"\n");
438 return PTR_ERR(st
->reg
);
441 ret
= regulator_enable(st
->reg
);
443 dev_err(&spi
->dev
, "Failed to enable regulator \"vref\"\n");
447 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
448 &ti_ads7950_trigger_handler
, NULL
);
450 dev_err(&spi
->dev
, "Failed to setup triggered buffer\n");
451 goto error_disable_reg
;
454 ret
= iio_device_register(indio_dev
);
456 dev_err(&spi
->dev
, "Failed to register iio device\n");
457 goto error_cleanup_ring
;
463 iio_triggered_buffer_cleanup(indio_dev
);
465 regulator_disable(st
->reg
);
470 static int ti_ads7950_remove(struct spi_device
*spi
)
472 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
473 struct ti_ads7950_state
*st
= iio_priv(indio_dev
);
475 iio_device_unregister(indio_dev
);
476 iio_triggered_buffer_cleanup(indio_dev
);
477 regulator_disable(st
->reg
);
482 static const struct spi_device_id ti_ads7950_id
[] = {
483 { "ads7950", TI_ADS7950
},
484 { "ads7951", TI_ADS7951
},
485 { "ads7952", TI_ADS7952
},
486 { "ads7953", TI_ADS7953
},
487 { "ads7954", TI_ADS7954
},
488 { "ads7955", TI_ADS7955
},
489 { "ads7956", TI_ADS7956
},
490 { "ads7957", TI_ADS7957
},
491 { "ads7958", TI_ADS7958
},
492 { "ads7959", TI_ADS7959
},
493 { "ads7960", TI_ADS7960
},
494 { "ads7961", TI_ADS7961
},
497 MODULE_DEVICE_TABLE(spi
, ti_ads7950_id
);
499 static const struct of_device_id ads7950_of_table
[] = {
500 { .compatible
= "ti,ads7950", .data
= &ti_ads7950_chip_info
[TI_ADS7950
] },
501 { .compatible
= "ti,ads7951", .data
= &ti_ads7950_chip_info
[TI_ADS7951
] },
502 { .compatible
= "ti,ads7952", .data
= &ti_ads7950_chip_info
[TI_ADS7952
] },
503 { .compatible
= "ti,ads7953", .data
= &ti_ads7950_chip_info
[TI_ADS7953
] },
504 { .compatible
= "ti,ads7954", .data
= &ti_ads7950_chip_info
[TI_ADS7954
] },
505 { .compatible
= "ti,ads7955", .data
= &ti_ads7950_chip_info
[TI_ADS7955
] },
506 { .compatible
= "ti,ads7956", .data
= &ti_ads7950_chip_info
[TI_ADS7956
] },
507 { .compatible
= "ti,ads7957", .data
= &ti_ads7950_chip_info
[TI_ADS7957
] },
508 { .compatible
= "ti,ads7958", .data
= &ti_ads7950_chip_info
[TI_ADS7958
] },
509 { .compatible
= "ti,ads7959", .data
= &ti_ads7950_chip_info
[TI_ADS7959
] },
510 { .compatible
= "ti,ads7960", .data
= &ti_ads7950_chip_info
[TI_ADS7960
] },
511 { .compatible
= "ti,ads7961", .data
= &ti_ads7950_chip_info
[TI_ADS7961
] },
514 MODULE_DEVICE_TABLE(of
, ads7950_of_table
);
516 static struct spi_driver ti_ads7950_driver
= {
519 .of_match_table
= ads7950_of_table
,
521 .probe
= ti_ads7950_probe
,
522 .remove
= ti_ads7950_remove
,
523 .id_table
= ti_ads7950_id
,
525 module_spi_driver(ti_ads7950_driver
);
527 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
528 MODULE_DESCRIPTION("TI TI_ADS7950 ADC");
529 MODULE_LICENSE("GPL v2");