1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com>
5 * Driver for Texas Instruments' ADC128S052, ADC122S021 and ADC124S021 ADC chip.
6 * Datasheets can be found here:
7 * https://www.ti.com/lit/ds/symlink/adc128s052.pdf
8 * https://www.ti.com/lit/ds/symlink/adc122s021.pdf
9 * https://www.ti.com/lit/ds/symlink/adc124s021.pdf
12 #include <linux/err.h>
13 #include <linux/iio/iio.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.h>
16 #include <linux/property.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/spi/spi.h>
20 struct adc128_configuration
{
21 const struct iio_chan_spec
*channels
;
26 struct spi_device
*spi
;
28 struct regulator
*reg
;
31 u8 buffer
[2] __aligned(IIO_DMA_MINALIGN
);
34 static int adc128_adc_conversion(struct adc128
*adc
, u8 channel
)
38 mutex_lock(&adc
->lock
);
40 adc
->buffer
[0] = channel
<< 3;
43 ret
= spi_write(adc
->spi
, &adc
->buffer
, 2);
45 mutex_unlock(&adc
->lock
);
49 ret
= spi_read(adc
->spi
, &adc
->buffer
, 2);
51 mutex_unlock(&adc
->lock
);
56 return ((adc
->buffer
[0] << 8 | adc
->buffer
[1]) & 0xFFF);
59 static int adc128_read_raw(struct iio_dev
*indio_dev
,
60 struct iio_chan_spec
const *channel
, int *val
,
63 struct adc128
*adc
= iio_priv(indio_dev
);
67 case IIO_CHAN_INFO_RAW
:
69 ret
= adc128_adc_conversion(adc
, channel
->channel
);
76 case IIO_CHAN_INFO_SCALE
:
78 ret
= regulator_get_voltage(adc
->reg
);
84 return IIO_VAL_FRACTIONAL_LOG2
;
92 #define ADC128_VOLTAGE_CHANNEL(num) \
94 .type = IIO_VOLTAGE, \
97 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
98 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
101 static const struct iio_chan_spec adc128s052_channels
[] = {
102 ADC128_VOLTAGE_CHANNEL(0),
103 ADC128_VOLTAGE_CHANNEL(1),
104 ADC128_VOLTAGE_CHANNEL(2),
105 ADC128_VOLTAGE_CHANNEL(3),
106 ADC128_VOLTAGE_CHANNEL(4),
107 ADC128_VOLTAGE_CHANNEL(5),
108 ADC128_VOLTAGE_CHANNEL(6),
109 ADC128_VOLTAGE_CHANNEL(7),
112 static const struct iio_chan_spec adc122s021_channels
[] = {
113 ADC128_VOLTAGE_CHANNEL(0),
114 ADC128_VOLTAGE_CHANNEL(1),
117 static const struct iio_chan_spec adc124s021_channels
[] = {
118 ADC128_VOLTAGE_CHANNEL(0),
119 ADC128_VOLTAGE_CHANNEL(1),
120 ADC128_VOLTAGE_CHANNEL(2),
121 ADC128_VOLTAGE_CHANNEL(3),
124 static const struct adc128_configuration adc128_config
[] = {
125 { adc128s052_channels
, ARRAY_SIZE(adc128s052_channels
) },
126 { adc122s021_channels
, ARRAY_SIZE(adc122s021_channels
) },
127 { adc124s021_channels
, ARRAY_SIZE(adc124s021_channels
) },
130 static const struct iio_info adc128_info
= {
131 .read_raw
= adc128_read_raw
,
134 static void adc128_disable_regulator(void *reg
)
136 regulator_disable(reg
);
139 static int adc128_probe(struct spi_device
*spi
)
141 const struct adc128_configuration
*config
;
142 struct iio_dev
*indio_dev
;
146 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
150 adc
= iio_priv(indio_dev
);
153 indio_dev
->name
= spi_get_device_id(spi
)->name
;
154 indio_dev
->modes
= INDIO_DIRECT_MODE
;
155 indio_dev
->info
= &adc128_info
;
157 config
= spi_get_device_match_data(spi
);
159 indio_dev
->channels
= config
->channels
;
160 indio_dev
->num_channels
= config
->num_channels
;
162 adc
->reg
= devm_regulator_get(&spi
->dev
, "vref");
163 if (IS_ERR(adc
->reg
))
164 return PTR_ERR(adc
->reg
);
166 ret
= regulator_enable(adc
->reg
);
169 ret
= devm_add_action_or_reset(&spi
->dev
, adc128_disable_regulator
,
174 mutex_init(&adc
->lock
);
176 return devm_iio_device_register(&spi
->dev
, indio_dev
);
179 static const struct of_device_id adc128_of_match
[] = {
180 { .compatible
= "ti,adc128s052", .data
= &adc128_config
[0] },
181 { .compatible
= "ti,adc122s021", .data
= &adc128_config
[1] },
182 { .compatible
= "ti,adc122s051", .data
= &adc128_config
[1] },
183 { .compatible
= "ti,adc122s101", .data
= &adc128_config
[1] },
184 { .compatible
= "ti,adc124s021", .data
= &adc128_config
[2] },
185 { .compatible
= "ti,adc124s051", .data
= &adc128_config
[2] },
186 { .compatible
= "ti,adc124s101", .data
= &adc128_config
[2] },
189 MODULE_DEVICE_TABLE(of
, adc128_of_match
);
191 static const struct spi_device_id adc128_id
[] = {
192 { "adc128s052", (kernel_ulong_t
)&adc128_config
[0] },
193 { "adc122s021", (kernel_ulong_t
)&adc128_config
[1] },
194 { "adc122s051", (kernel_ulong_t
)&adc128_config
[1] },
195 { "adc122s101", (kernel_ulong_t
)&adc128_config
[1] },
196 { "adc124s021", (kernel_ulong_t
)&adc128_config
[2] },
197 { "adc124s051", (kernel_ulong_t
)&adc128_config
[2] },
198 { "adc124s101", (kernel_ulong_t
)&adc128_config
[2] },
201 MODULE_DEVICE_TABLE(spi
, adc128_id
);
203 static const struct acpi_device_id adc128_acpi_match
[] = {
204 { "AANT1280", (kernel_ulong_t
)&adc128_config
[2] },
207 MODULE_DEVICE_TABLE(acpi
, adc128_acpi_match
);
209 static struct spi_driver adc128_driver
= {
211 .name
= "adc128s052",
212 .of_match_table
= adc128_of_match
,
213 .acpi_match_table
= adc128_acpi_match
,
215 .probe
= adc128_probe
,
216 .id_table
= adc128_id
,
218 module_spi_driver(adc128_driver
);
220 MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
221 MODULE_DESCRIPTION("Texas Instruments ADC128S052");
222 MODULE_LICENSE("GPL v2");