drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / iio / adc / ti-tlc4541.c
blob08de997584fd0177d30f704fd2d7350770955582
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * TI tlc4541 ADC Driver
5 * Copyright (C) 2017 Phil Reid
7 * Datasheets can be found here:
8 * https://www.ti.com/lit/gpn/tlc3541
9 * https://www.ti.com/lit/gpn/tlc4541
11 * The tlc4541 requires 24 clock cycles to start a transfer.
12 * Conversion then takes 2.94us to complete before data is ready
13 * Data is returned MSB first.
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/interrupt.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/mod_devicetable.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30 #include <linux/spi/spi.h>
31 #include <linux/sysfs.h>
33 struct tlc4541_state {
34 struct spi_device *spi;
35 struct regulator *reg;
36 struct spi_transfer scan_single_xfer[3];
37 struct spi_message scan_single_msg;
40 * DMA (thus cache coherency maintenance) may require the
41 * transfer buffers to live in their own cache lines.
42 * 2 bytes data + 6 bytes padding + 8 bytes timestamp when
43 * call iio_push_to_buffers_with_timestamp.
45 __be16 rx_buf[8] __aligned(IIO_DMA_MINALIGN);
48 struct tlc4541_chip_info {
49 const struct iio_chan_spec *channels;
50 unsigned int num_channels;
53 enum tlc4541_id {
54 TLC3541,
55 TLC4541,
58 #define TLC4541_V_CHAN(bits, bitshift) { \
59 .type = IIO_VOLTAGE, \
60 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
61 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
62 .scan_type = { \
63 .sign = 'u', \
64 .realbits = (bits), \
65 .storagebits = 16, \
66 .shift = (bitshift), \
67 .endianness = IIO_BE, \
68 }, \
71 #define DECLARE_TLC4541_CHANNELS(name, bits, bitshift) \
72 const struct iio_chan_spec name ## _channels[] = { \
73 TLC4541_V_CHAN(bits, bitshift), \
74 IIO_CHAN_SOFT_TIMESTAMP(1), \
77 static DECLARE_TLC4541_CHANNELS(tlc3541, 14, 2);
78 static DECLARE_TLC4541_CHANNELS(tlc4541, 16, 0);
80 static const struct tlc4541_chip_info tlc4541_chip_info[] = {
81 [TLC3541] = {
82 .channels = tlc3541_channels,
83 .num_channels = ARRAY_SIZE(tlc3541_channels),
85 [TLC4541] = {
86 .channels = tlc4541_channels,
87 .num_channels = ARRAY_SIZE(tlc4541_channels),
91 static irqreturn_t tlc4541_trigger_handler(int irq, void *p)
93 struct iio_poll_func *pf = p;
94 struct iio_dev *indio_dev = pf->indio_dev;
95 struct tlc4541_state *st = iio_priv(indio_dev);
96 int ret;
98 ret = spi_sync(st->spi, &st->scan_single_msg);
99 if (ret < 0)
100 goto done;
102 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
103 iio_get_time_ns(indio_dev));
105 done:
106 iio_trigger_notify_done(indio_dev->trig);
107 return IRQ_HANDLED;
110 static int tlc4541_get_range(struct tlc4541_state *st)
112 int vref;
114 vref = regulator_get_voltage(st->reg);
115 if (vref < 0)
116 return vref;
118 vref /= 1000;
120 return vref;
123 static int tlc4541_read_raw(struct iio_dev *indio_dev,
124 struct iio_chan_spec const *chan,
125 int *val,
126 int *val2,
127 long m)
129 int ret = 0;
130 struct tlc4541_state *st = iio_priv(indio_dev);
132 switch (m) {
133 case IIO_CHAN_INFO_RAW:
134 ret = iio_device_claim_direct_mode(indio_dev);
135 if (ret)
136 return ret;
137 ret = spi_sync(st->spi, &st->scan_single_msg);
138 iio_device_release_direct_mode(indio_dev);
139 if (ret < 0)
140 return ret;
141 *val = be16_to_cpu(st->rx_buf[0]);
142 *val = *val >> chan->scan_type.shift;
143 *val &= GENMASK(chan->scan_type.realbits - 1, 0);
144 return IIO_VAL_INT;
145 case IIO_CHAN_INFO_SCALE:
146 ret = tlc4541_get_range(st);
147 if (ret < 0)
148 return ret;
149 *val = ret;
150 *val2 = chan->scan_type.realbits;
151 return IIO_VAL_FRACTIONAL_LOG2;
153 return -EINVAL;
156 static const struct iio_info tlc4541_info = {
157 .read_raw = &tlc4541_read_raw,
160 static int tlc4541_probe(struct spi_device *spi)
162 struct tlc4541_state *st;
163 struct iio_dev *indio_dev;
164 const struct tlc4541_chip_info *info;
165 int ret;
166 int8_t device_init = 0;
168 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
169 if (indio_dev == NULL)
170 return -ENOMEM;
172 st = iio_priv(indio_dev);
174 spi_set_drvdata(spi, indio_dev);
176 st->spi = spi;
178 info = &tlc4541_chip_info[spi_get_device_id(spi)->driver_data];
180 indio_dev->name = spi_get_device_id(spi)->name;
181 indio_dev->modes = INDIO_DIRECT_MODE;
182 indio_dev->channels = info->channels;
183 indio_dev->num_channels = info->num_channels;
184 indio_dev->info = &tlc4541_info;
186 /* perform reset */
187 spi_write(spi, &device_init, 1);
189 /* Setup default message */
190 st->scan_single_xfer[0].rx_buf = &st->rx_buf[0];
191 st->scan_single_xfer[0].len = 3;
192 st->scan_single_xfer[1].delay.value = 3;
193 st->scan_single_xfer[1].delay.unit = SPI_DELAY_UNIT_NSECS;
194 st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
195 st->scan_single_xfer[2].len = 2;
197 spi_message_init_with_transfers(&st->scan_single_msg,
198 st->scan_single_xfer, 3);
200 st->reg = devm_regulator_get(&spi->dev, "vref");
201 if (IS_ERR(st->reg))
202 return PTR_ERR(st->reg);
204 ret = regulator_enable(st->reg);
205 if (ret)
206 return ret;
208 ret = iio_triggered_buffer_setup(indio_dev, NULL,
209 &tlc4541_trigger_handler, NULL);
210 if (ret)
211 goto error_disable_reg;
213 ret = iio_device_register(indio_dev);
214 if (ret)
215 goto error_cleanup_buffer;
217 return 0;
219 error_cleanup_buffer:
220 iio_triggered_buffer_cleanup(indio_dev);
221 error_disable_reg:
222 regulator_disable(st->reg);
224 return ret;
227 static void tlc4541_remove(struct spi_device *spi)
229 struct iio_dev *indio_dev = spi_get_drvdata(spi);
230 struct tlc4541_state *st = iio_priv(indio_dev);
232 iio_device_unregister(indio_dev);
233 iio_triggered_buffer_cleanup(indio_dev);
234 regulator_disable(st->reg);
237 static const struct of_device_id tlc4541_dt_ids[] = {
238 { .compatible = "ti,tlc3541", },
239 { .compatible = "ti,tlc4541", },
242 MODULE_DEVICE_TABLE(of, tlc4541_dt_ids);
244 static const struct spi_device_id tlc4541_id[] = {
245 { "tlc3541", TLC3541 },
246 { "tlc4541", TLC4541 },
249 MODULE_DEVICE_TABLE(spi, tlc4541_id);
251 static struct spi_driver tlc4541_driver = {
252 .driver = {
253 .name = "tlc4541",
254 .of_match_table = tlc4541_dt_ids,
256 .probe = tlc4541_probe,
257 .remove = tlc4541_remove,
258 .id_table = tlc4541_id,
260 module_spi_driver(tlc4541_driver);
262 MODULE_AUTHOR("Phil Reid <preid@electromag.com.au>");
263 MODULE_DESCRIPTION("Texas Instruments TLC4541 ADC");
264 MODULE_LICENSE("GPL v2");