drm/client: Fix drm client endless Kconfig loop
[drm/drm-misc.git] / drivers / iio / adc / ad7292.c
bloba398973f313d8a21a8bf298c9ce356ce616fb340
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Analog Devices AD7292 SPI ADC driver
5 * Copyright 2019 Analog Devices Inc.
6 */
8 #include <linux/bitfield.h>
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/property.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/spi/spi.h>
16 #include <linux/iio/iio.h>
18 #define ADI_VENDOR_ID 0x0018
20 #define AD7292_INTERNAL_REF_MV 1250
22 /* AD7292 registers definition */
23 #define AD7292_REG_VENDOR_ID 0x00
24 #define AD7292_REG_CONF_BANK 0x05
25 #define AD7292_REG_CONV_COMM 0x0E
26 #define AD7292_REG_ADC_CH(x) (0x10 + (x))
28 /* AD7292 configuration bank subregisters definition */
29 #define AD7292_BANK_REG_VIN_RNG0 0x10
30 #define AD7292_BANK_REG_VIN_RNG1 0x11
31 #define AD7292_BANK_REG_SAMP_MODE 0x12
33 #define AD7292_RD_FLAG_MSK(x) (BIT(7) | ((x) & 0x3F))
35 /* AD7292_REG_ADC_CONVERSION */
36 #define AD7292_ADC_DATA_MASK GENMASK(15, 6)
37 #define AD7292_ADC_DATA(x) FIELD_GET(AD7292_ADC_DATA_MASK, x)
39 /* AD7292_CHANNEL_SAMPLING_MODE */
40 #define AD7292_CH_SAMP_MODE(reg, ch) (((reg) >> 8) & BIT(ch))
42 /* AD7292_CHANNEL_VIN_RANGE */
43 #define AD7292_CH_VIN_RANGE(reg, ch) ((reg) & BIT(ch))
45 #define AD7292_VOLTAGE_CHAN(_chan) \
46 { \
47 .type = IIO_VOLTAGE, \
48 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
49 BIT(IIO_CHAN_INFO_SCALE), \
50 .indexed = 1, \
51 .channel = _chan, \
54 static const struct iio_chan_spec ad7292_channels[] = {
55 AD7292_VOLTAGE_CHAN(0),
56 AD7292_VOLTAGE_CHAN(1),
57 AD7292_VOLTAGE_CHAN(2),
58 AD7292_VOLTAGE_CHAN(3),
59 AD7292_VOLTAGE_CHAN(4),
60 AD7292_VOLTAGE_CHAN(5),
61 AD7292_VOLTAGE_CHAN(6),
62 AD7292_VOLTAGE_CHAN(7)
65 static const struct iio_chan_spec ad7292_channels_diff[] = {
67 .type = IIO_VOLTAGE,
68 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
69 .indexed = 1,
70 .differential = 1,
71 .channel = 0,
72 .channel2 = 1,
74 AD7292_VOLTAGE_CHAN(2),
75 AD7292_VOLTAGE_CHAN(3),
76 AD7292_VOLTAGE_CHAN(4),
77 AD7292_VOLTAGE_CHAN(5),
78 AD7292_VOLTAGE_CHAN(6),
79 AD7292_VOLTAGE_CHAN(7)
82 struct ad7292_state {
83 struct spi_device *spi;
84 unsigned short vref_mv;
86 __be16 d16 __aligned(IIO_DMA_MINALIGN);
87 u8 d8[2];
90 static int ad7292_spi_reg_read(struct ad7292_state *st, unsigned int addr)
92 int ret;
94 st->d8[0] = AD7292_RD_FLAG_MSK(addr);
96 ret = spi_write_then_read(st->spi, st->d8, 1, &st->d16, 2);
97 if (ret < 0)
98 return ret;
100 return be16_to_cpu(st->d16);
103 static int ad7292_spi_subreg_read(struct ad7292_state *st, unsigned int addr,
104 unsigned int sub_addr, unsigned int len)
106 unsigned int shift = 16 - (8 * len);
107 int ret;
109 st->d8[0] = AD7292_RD_FLAG_MSK(addr);
110 st->d8[1] = sub_addr;
112 ret = spi_write_then_read(st->spi, st->d8, 2, &st->d16, len);
113 if (ret < 0)
114 return ret;
116 return (be16_to_cpu(st->d16) >> shift);
119 static int ad7292_single_conversion(struct ad7292_state *st,
120 unsigned int chan_addr)
122 int ret;
124 struct spi_transfer t[] = {
126 .tx_buf = &st->d8,
127 .len = 4,
128 .delay = {
129 .value = 6,
130 .unit = SPI_DELAY_UNIT_USECS
132 }, {
133 .rx_buf = &st->d16,
134 .len = 2,
138 st->d8[0] = chan_addr;
139 st->d8[1] = AD7292_RD_FLAG_MSK(AD7292_REG_CONV_COMM);
141 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
143 if (ret < 0)
144 return ret;
146 return be16_to_cpu(st->d16);
149 static int ad7292_vin_range_multiplier(struct ad7292_state *st, int channel)
151 int samp_mode, range0, range1, factor = 1;
154 * Every AD7292 ADC channel may have its input range adjusted according
155 * to the settings at the ADC sampling mode and VIN range subregisters.
156 * For a given channel, the minimum input range is equal to Vref, and it
157 * may be increased by a multiplier factor of 2 or 4 according to the
158 * following rule:
159 * If channel is being sampled with respect to AGND:
160 * factor = 4 if VIN range0 and VIN range1 equal 0
161 * factor = 2 if only one of VIN ranges equal 1
162 * factor = 1 if both VIN range0 and VIN range1 equal 1
163 * If channel is being sampled with respect to AVDD:
164 * factor = 4 if VIN range0 and VIN range1 equal 0
165 * Behavior is undefined if any of VIN range doesn't equal 0
168 samp_mode = ad7292_spi_subreg_read(st, AD7292_REG_CONF_BANK,
169 AD7292_BANK_REG_SAMP_MODE, 2);
171 if (samp_mode < 0)
172 return samp_mode;
174 range0 = ad7292_spi_subreg_read(st, AD7292_REG_CONF_BANK,
175 AD7292_BANK_REG_VIN_RNG0, 2);
177 if (range0 < 0)
178 return range0;
180 range1 = ad7292_spi_subreg_read(st, AD7292_REG_CONF_BANK,
181 AD7292_BANK_REG_VIN_RNG1, 2);
183 if (range1 < 0)
184 return range1;
186 if (AD7292_CH_SAMP_MODE(samp_mode, channel)) {
187 /* Sampling with respect to AGND */
188 if (!AD7292_CH_VIN_RANGE(range0, channel))
189 factor *= 2;
191 if (!AD7292_CH_VIN_RANGE(range1, channel))
192 factor *= 2;
194 } else {
195 /* Sampling with respect to AVDD */
196 if (AD7292_CH_VIN_RANGE(range0, channel) ||
197 AD7292_CH_VIN_RANGE(range1, channel))
198 return -EPERM;
200 factor = 4;
203 return factor;
206 static int ad7292_read_raw(struct iio_dev *indio_dev,
207 const struct iio_chan_spec *chan,
208 int *val, int *val2, long info)
210 struct ad7292_state *st = iio_priv(indio_dev);
211 unsigned int ch_addr;
212 int ret;
214 switch (info) {
215 case IIO_CHAN_INFO_RAW:
216 ch_addr = AD7292_REG_ADC_CH(chan->channel);
217 ret = ad7292_single_conversion(st, ch_addr);
218 if (ret < 0)
219 return ret;
221 *val = AD7292_ADC_DATA(ret);
223 return IIO_VAL_INT;
224 case IIO_CHAN_INFO_SCALE:
226 * To convert a raw value to standard units, the IIO defines
227 * this formula: Scaled value = (raw + offset) * scale.
228 * For the scale to be a correct multiplier for (raw + offset),
229 * it must be calculated as the input range divided by the
230 * number of possible distinct input values. Given the ADC data
231 * is 10 bit long, it may assume 2^10 distinct values.
232 * Hence, scale = range / 2^10. The IIO_VAL_FRACTIONAL_LOG2
233 * return type indicates to the IIO API to divide *val by 2 to
234 * the power of *val2 when returning from read_raw.
237 ret = ad7292_vin_range_multiplier(st, chan->channel);
238 if (ret < 0)
239 return ret;
241 *val = st->vref_mv * ret;
242 *val2 = 10;
243 return IIO_VAL_FRACTIONAL_LOG2;
244 default:
245 break;
247 return -EINVAL;
250 static const struct iio_info ad7292_info = {
251 .read_raw = ad7292_read_raw,
254 static int ad7292_probe(struct spi_device *spi)
256 struct ad7292_state *st;
257 struct iio_dev *indio_dev;
258 bool diff_channels = false;
259 int ret;
261 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
262 if (!indio_dev)
263 return -ENOMEM;
265 st = iio_priv(indio_dev);
266 st->spi = spi;
268 ret = ad7292_spi_reg_read(st, AD7292_REG_VENDOR_ID);
269 if (ret != ADI_VENDOR_ID) {
270 dev_err(&spi->dev, "Wrong vendor id 0x%x\n", ret);
271 return -EINVAL;
274 ret = devm_regulator_get_enable_read_voltage(&spi->dev, "vref");
275 if (ret < 0 && ret != -ENODEV)
276 return ret;
278 st->vref_mv = ret == -ENODEV ? AD7292_INTERNAL_REF_MV : ret / 1000;
280 indio_dev->name = spi_get_device_id(spi)->name;
281 indio_dev->modes = INDIO_DIRECT_MODE;
282 indio_dev->info = &ad7292_info;
284 device_for_each_child_node_scoped(&spi->dev, child) {
285 diff_channels = fwnode_property_read_bool(child,
286 "diff-channels");
287 if (diff_channels)
288 break;
291 if (diff_channels) {
292 indio_dev->num_channels = ARRAY_SIZE(ad7292_channels_diff);
293 indio_dev->channels = ad7292_channels_diff;
294 } else {
295 indio_dev->num_channels = ARRAY_SIZE(ad7292_channels);
296 indio_dev->channels = ad7292_channels;
299 return devm_iio_device_register(&spi->dev, indio_dev);
302 static const struct spi_device_id ad7292_id_table[] = {
303 { "ad7292", 0 },
306 MODULE_DEVICE_TABLE(spi, ad7292_id_table);
308 static const struct of_device_id ad7292_of_match[] = {
309 { .compatible = "adi,ad7292" },
312 MODULE_DEVICE_TABLE(of, ad7292_of_match);
314 static struct spi_driver ad7292_driver = {
315 .driver = {
316 .name = "ad7292",
317 .of_match_table = ad7292_of_match,
319 .probe = ad7292_probe,
320 .id_table = ad7292_id_table,
322 module_spi_driver(ad7292_driver);
324 MODULE_AUTHOR("Marcelo Schmitt <marcelo.schmitt1@gmail.com>");
325 MODULE_DESCRIPTION("Analog Devices AD7292 ADC driver");
326 MODULE_LICENSE("GPL v2");