2 * TI tlc4541 ADC Driver
4 * Copyright (C) 2017 Phil Reid
6 * Datasheets can be found here:
7 * http://www.ti.com/lit/gpn/tlc3541
8 * http://www.ti.com/lit/gpn/tlc4541
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * The tlc4541 requires 24 clock cycles to start a transfer.
15 * Conversion then takes 2.94us to complete before data is ready
16 * Data is returned MSB first.
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/interrupt.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/trigger_consumer.h>
27 #include <linux/iio/triggered_buffer.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/slab.h>
32 #include <linux/spi/spi.h>
33 #include <linux/sysfs.h>
35 struct tlc4541_state
{
36 struct spi_device
*spi
;
37 struct regulator
*reg
;
38 struct spi_transfer scan_single_xfer
[3];
39 struct spi_message scan_single_msg
;
42 * DMA (thus cache coherency maintenance) requires the
43 * transfer buffers to live in their own cache lines.
44 * 2 bytes data + 6 bytes padding + 8 bytes timestamp when
45 * call iio_push_to_buffers_with_timestamp.
47 __be16 rx_buf
[8] ____cacheline_aligned
;
50 struct tlc4541_chip_info
{
51 const struct iio_chan_spec
*channels
;
52 unsigned int num_channels
;
60 #define TLC4541_V_CHAN(bits, bitshift) { \
61 .type = IIO_VOLTAGE, \
62 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
63 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
68 .shift = (bitshift), \
69 .endianness = IIO_BE, \
73 #define DECLARE_TLC4541_CHANNELS(name, bits, bitshift) \
74 const struct iio_chan_spec name ## _channels[] = { \
75 TLC4541_V_CHAN(bits, bitshift), \
76 IIO_CHAN_SOFT_TIMESTAMP(1), \
79 static DECLARE_TLC4541_CHANNELS(tlc3541
, 14, 2);
80 static DECLARE_TLC4541_CHANNELS(tlc4541
, 16, 0);
82 static const struct tlc4541_chip_info tlc4541_chip_info
[] = {
84 .channels
= tlc3541_channels
,
85 .num_channels
= ARRAY_SIZE(tlc3541_channels
),
88 .channels
= tlc4541_channels
,
89 .num_channels
= ARRAY_SIZE(tlc4541_channels
),
93 static irqreturn_t
tlc4541_trigger_handler(int irq
, void *p
)
95 struct iio_poll_func
*pf
= p
;
96 struct iio_dev
*indio_dev
= pf
->indio_dev
;
97 struct tlc4541_state
*st
= iio_priv(indio_dev
);
100 ret
= spi_sync(st
->spi
, &st
->scan_single_msg
);
104 iio_push_to_buffers_with_timestamp(indio_dev
, st
->rx_buf
,
105 iio_get_time_ns(indio_dev
));
108 iio_trigger_notify_done(indio_dev
->trig
);
112 static int tlc4541_get_range(struct tlc4541_state
*st
)
116 vref
= regulator_get_voltage(st
->reg
);
125 static int tlc4541_read_raw(struct iio_dev
*indio_dev
,
126 struct iio_chan_spec
const *chan
,
132 struct tlc4541_state
*st
= iio_priv(indio_dev
);
135 case IIO_CHAN_INFO_RAW
:
136 ret
= iio_device_claim_direct_mode(indio_dev
);
139 ret
= spi_sync(st
->spi
, &st
->scan_single_msg
);
140 iio_device_release_direct_mode(indio_dev
);
143 *val
= be16_to_cpu(st
->rx_buf
[0]);
144 *val
= *val
>> chan
->scan_type
.shift
;
145 *val
&= GENMASK(chan
->scan_type
.realbits
- 1, 0);
147 case IIO_CHAN_INFO_SCALE
:
148 ret
= tlc4541_get_range(st
);
152 *val2
= chan
->scan_type
.realbits
;
153 return IIO_VAL_FRACTIONAL_LOG2
;
158 static const struct iio_info tlc4541_info
= {
159 .read_raw
= &tlc4541_read_raw
,
162 static int tlc4541_probe(struct spi_device
*spi
)
164 struct tlc4541_state
*st
;
165 struct iio_dev
*indio_dev
;
166 const struct tlc4541_chip_info
*info
;
168 int8_t device_init
= 0;
170 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
171 if (indio_dev
== NULL
)
174 st
= iio_priv(indio_dev
);
176 spi_set_drvdata(spi
, indio_dev
);
180 info
= &tlc4541_chip_info
[spi_get_device_id(spi
)->driver_data
];
182 indio_dev
->name
= spi_get_device_id(spi
)->name
;
183 indio_dev
->dev
.parent
= &spi
->dev
;
184 indio_dev
->modes
= INDIO_DIRECT_MODE
;
185 indio_dev
->channels
= info
->channels
;
186 indio_dev
->num_channels
= info
->num_channels
;
187 indio_dev
->info
= &tlc4541_info
;
190 spi_write(spi
, &device_init
, 1);
192 /* Setup default message */
193 st
->scan_single_xfer
[0].rx_buf
= &st
->rx_buf
[0];
194 st
->scan_single_xfer
[0].len
= 3;
195 st
->scan_single_xfer
[1].delay_usecs
= 3;
196 st
->scan_single_xfer
[2].rx_buf
= &st
->rx_buf
[0];
197 st
->scan_single_xfer
[2].len
= 2;
199 spi_message_init_with_transfers(&st
->scan_single_msg
,
200 st
->scan_single_xfer
, 3);
202 st
->reg
= devm_regulator_get(&spi
->dev
, "vref");
204 return PTR_ERR(st
->reg
);
206 ret
= regulator_enable(st
->reg
);
210 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
211 &tlc4541_trigger_handler
, NULL
);
213 goto error_disable_reg
;
215 ret
= iio_device_register(indio_dev
);
217 goto error_cleanup_buffer
;
221 error_cleanup_buffer
:
222 iio_triggered_buffer_cleanup(indio_dev
);
224 regulator_disable(st
->reg
);
229 static int tlc4541_remove(struct spi_device
*spi
)
231 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
232 struct tlc4541_state
*st
= iio_priv(indio_dev
);
234 iio_device_unregister(indio_dev
);
235 iio_triggered_buffer_cleanup(indio_dev
);
236 regulator_disable(st
->reg
);
242 static const struct of_device_id tlc4541_dt_ids
[] = {
243 { .compatible
= "ti,tlc3541", },
244 { .compatible
= "ti,tlc4541", },
247 MODULE_DEVICE_TABLE(of
, tlc4541_dt_ids
);
250 static const struct spi_device_id tlc4541_id
[] = {
251 {"tlc3541", TLC3541
},
252 {"tlc4541", TLC4541
},
255 MODULE_DEVICE_TABLE(spi
, tlc4541_id
);
257 static struct spi_driver tlc4541_driver
= {
260 .of_match_table
= of_match_ptr(tlc4541_dt_ids
),
262 .probe
= tlc4541_probe
,
263 .remove
= tlc4541_remove
,
264 .id_table
= tlc4541_id
,
266 module_spi_driver(tlc4541_driver
);
268 MODULE_AUTHOR("Phil Reid <preid@electromag.com.au>");
269 MODULE_DESCRIPTION("Texas Instruments TLC4541 ADC");
270 MODULE_LICENSE("GPL v2");