xref: /linux/drivers/iio/adc/ti-adc128s052.c (revision 889600e21e3be388a6817c2a0dac0411df860751)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com>
4  *
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
10  */
11 
12 #include <linux/cleanup.h>
13 #include <linux/err.h>
14 #include <linux/iio/iio.h>
15 #include <linux/module.h>
16 #include <linux/property.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/spi/spi.h>
19 
20 struct adc128_configuration {
21 	const struct iio_chan_spec	*channels;
22 	u8				num_channels;
23 	const char			*refname;
24 	int				num_other_regulators;
25 	const char * const		(*other_regulators)[];
26 };
27 
28 struct adc128 {
29 	struct spi_device *spi;
30 
31 	/*
32 	 * Serialize the SPI 'write-channel + read data' accesses and protect
33 	 * the shared buffer.
34 	 */
35 	struct mutex lock;
36 	int vref_mv;
37 	union {
38 		__be16 buffer16;
39 		u8 buffer[2];
40 	} __aligned(IIO_DMA_MINALIGN);
41 };
42 
adc128_adc_conversion(struct adc128 * adc,u8 channel)43 static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
44 {
45 	int ret;
46 
47 	guard(mutex)(&adc->lock);
48 
49 	adc->buffer[0] = channel << 3;
50 	adc->buffer[1] = 0;
51 
52 	ret = spi_write(adc->spi, &adc->buffer, sizeof(adc->buffer));
53 	if (ret < 0)
54 		return ret;
55 
56 	ret = spi_read(adc->spi, &adc->buffer16, sizeof(adc->buffer16));
57 	if (ret < 0)
58 		return ret;
59 
60 	return be16_to_cpu(adc->buffer16) & 0xFFF;
61 }
62 
adc128_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int * val,int * val2,long mask)63 static int adc128_read_raw(struct iio_dev *indio_dev,
64 			   struct iio_chan_spec const *channel, int *val,
65 			   int *val2, long mask)
66 {
67 	struct adc128 *adc = iio_priv(indio_dev);
68 	int ret;
69 
70 	switch (mask) {
71 	case IIO_CHAN_INFO_RAW:
72 
73 		ret = adc128_adc_conversion(adc, channel->channel);
74 		if (ret < 0)
75 			return ret;
76 
77 		*val = ret;
78 		return IIO_VAL_INT;
79 
80 	case IIO_CHAN_INFO_SCALE:
81 
82 		*val = adc->vref_mv;
83 		*val2 = 12;
84 		return IIO_VAL_FRACTIONAL_LOG2;
85 
86 	default:
87 		return -EINVAL;
88 	}
89 
90 }
91 
92 #define ADC128_VOLTAGE_CHANNEL(num)	\
93 	{ \
94 		.type = IIO_VOLTAGE, \
95 		.indexed = 1, \
96 		.channel = (num), \
97 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
98 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
99 	}
100 
101 static const struct iio_chan_spec simple_1chan_adc_channels[] = {
102 	ADC128_VOLTAGE_CHANNEL(0),
103 };
104 
105 static const struct iio_chan_spec simple_2chan_adc_channels[] = {
106 	ADC128_VOLTAGE_CHANNEL(0),
107 	ADC128_VOLTAGE_CHANNEL(1),
108 };
109 
110 static const struct iio_chan_spec simple_4chan_adc_channels[] = {
111 	ADC128_VOLTAGE_CHANNEL(0),
112 	ADC128_VOLTAGE_CHANNEL(1),
113 	ADC128_VOLTAGE_CHANNEL(2),
114 	ADC128_VOLTAGE_CHANNEL(3),
115 };
116 
117 static const struct iio_chan_spec simple_8chan_adc_channels[] = {
118 	ADC128_VOLTAGE_CHANNEL(0),
119 	ADC128_VOLTAGE_CHANNEL(1),
120 	ADC128_VOLTAGE_CHANNEL(2),
121 	ADC128_VOLTAGE_CHANNEL(3),
122 	ADC128_VOLTAGE_CHANNEL(4),
123 	ADC128_VOLTAGE_CHANNEL(5),
124 	ADC128_VOLTAGE_CHANNEL(6),
125 	ADC128_VOLTAGE_CHANNEL(7),
126 };
127 
128 static const char * const bd79104_regulators[] = { "iovdd" };
129 
130 static const struct adc128_configuration adc122s_config = {
131 	.channels = simple_2chan_adc_channels,
132 	.num_channels = ARRAY_SIZE(simple_2chan_adc_channels),
133 	.refname = "vref",
134 };
135 
136 static const struct adc128_configuration adc124s_config = {
137 	.channels = simple_4chan_adc_channels,
138 	.num_channels = ARRAY_SIZE(simple_4chan_adc_channels),
139 	.refname = "vref",
140 };
141 
142 static const struct adc128_configuration adc128s_config = {
143 	.channels = simple_8chan_adc_channels,
144 	.num_channels = ARRAY_SIZE(simple_8chan_adc_channels),
145 	.refname = "vref",
146 };
147 
148 static const struct adc128_configuration bd79100_config = {
149 	.channels = simple_1chan_adc_channels,
150 	.num_channels = ARRAY_SIZE(simple_1chan_adc_channels),
151 	.refname = "vdd",
152 	.other_regulators = &bd79104_regulators,
153 	.num_other_regulators = 1,
154 };
155 
156 static const struct adc128_configuration bd79101_config = {
157 	.channels = simple_2chan_adc_channels,
158 	.num_channels = ARRAY_SIZE(simple_2chan_adc_channels),
159 	.refname = "vdd",
160 	.other_regulators = &bd79104_regulators,
161 	.num_other_regulators = 1,
162 };
163 
164 static const struct adc128_configuration bd79102_config = {
165 	.channels = simple_4chan_adc_channels,
166 	.num_channels = ARRAY_SIZE(simple_4chan_adc_channels),
167 	.refname = "vdd",
168 	.other_regulators = &bd79104_regulators,
169 	.num_other_regulators = 1,
170 };
171 
172 static const struct adc128_configuration bd79104_config = {
173 	.channels = simple_8chan_adc_channels,
174 	.num_channels = ARRAY_SIZE(simple_8chan_adc_channels),
175 	.refname = "vdd",
176 	.other_regulators = &bd79104_regulators,
177 	.num_other_regulators = 1,
178 };
179 
180 static const struct iio_info adc128_info = {
181 	.read_raw = adc128_read_raw,
182 };
183 
adc128_probe(struct spi_device * spi)184 static int adc128_probe(struct spi_device *spi)
185 {
186 	const struct adc128_configuration *config;
187 	struct iio_dev *indio_dev;
188 	struct adc128 *adc;
189 	int ret;
190 
191 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
192 	if (!indio_dev)
193 		return -ENOMEM;
194 
195 	adc = iio_priv(indio_dev);
196 	adc->spi = spi;
197 
198 	indio_dev->name = spi_get_device_id(spi)->name;
199 	indio_dev->modes = INDIO_DIRECT_MODE;
200 	indio_dev->info = &adc128_info;
201 
202 	config = spi_get_device_match_data(spi);
203 
204 	indio_dev->channels = config->channels;
205 	indio_dev->num_channels = config->num_channels;
206 
207 	ret = devm_regulator_get_enable_read_voltage(&spi->dev,
208 						     config->refname);
209 	if (ret < 0)
210 		return dev_err_probe(&spi->dev, ret,
211 				     "failed to read '%s' voltage",
212 				     config->refname);
213 
214 	adc->vref_mv = ret / 1000;
215 
216 	if (config->num_other_regulators) {
217 		ret = devm_regulator_bulk_get_enable(&spi->dev,
218 						config->num_other_regulators,
219 						*config->other_regulators);
220 		if (ret)
221 			return dev_err_probe(&spi->dev, ret,
222 					     "Failed to enable regulators\n");
223 	}
224 
225 	ret = devm_mutex_init(&spi->dev, &adc->lock);
226 	if (ret)
227 		return ret;
228 
229 	return devm_iio_device_register(&spi->dev, indio_dev);
230 }
231 
232 static const struct of_device_id adc128_of_match[] = {
233 	{ .compatible = "ti,adc128s052", .data = &adc128s_config },
234 	{ .compatible = "ti,adc122s021", .data = &adc122s_config },
235 	{ .compatible = "ti,adc122s051", .data = &adc122s_config },
236 	{ .compatible = "ti,adc122s101", .data = &adc122s_config },
237 	{ .compatible = "ti,adc124s021", .data = &adc124s_config },
238 	{ .compatible = "ti,adc124s051", .data = &adc124s_config },
239 	{ .compatible = "ti,adc124s101", .data = &adc124s_config },
240 	{ .compatible = "rohm,bd79100", .data = &bd79100_config },
241 	{ .compatible = "rohm,bd79101", .data = &bd79101_config },
242 	{ .compatible = "rohm,bd79102", .data = &bd79102_config },
243 	{ .compatible = "rohm,bd79103", .data = &bd79104_config },
244 	{ .compatible = "rohm,bd79104", .data = &bd79104_config },
245 	{ }
246 };
247 MODULE_DEVICE_TABLE(of, adc128_of_match);
248 
249 static const struct spi_device_id adc128_id[] = {
250 	{ .name = "adc128s052", .driver_data = (kernel_ulong_t)&adc128s_config },
251 	{ .name = "adc122s021", .driver_data = (kernel_ulong_t)&adc122s_config },
252 	{ .name = "adc122s051", .driver_data = (kernel_ulong_t)&adc122s_config },
253 	{ .name = "adc122s101", .driver_data = (kernel_ulong_t)&adc122s_config },
254 	{ .name = "adc124s021", .driver_data = (kernel_ulong_t)&adc124s_config },
255 	{ .name = "adc124s051", .driver_data = (kernel_ulong_t)&adc124s_config },
256 	{ .name = "adc124s101", .driver_data = (kernel_ulong_t)&adc124s_config },
257 	{ .name = "bd79100", .driver_data = (kernel_ulong_t)&bd79100_config },
258 	{ .name = "bd79101", .driver_data = (kernel_ulong_t)&bd79101_config },
259 	{ .name = "bd79102", .driver_data = (kernel_ulong_t)&bd79102_config },
260 	{ .name = "bd79103", .driver_data = (kernel_ulong_t)&bd79104_config },
261 	{ .name = "bd79104", .driver_data = (kernel_ulong_t)&bd79104_config },
262 	{ }
263 };
264 MODULE_DEVICE_TABLE(spi, adc128_id);
265 
266 static const struct acpi_device_id adc128_acpi_match[] = {
267 	{ "AANT1280", (kernel_ulong_t)&adc124s_config },
268 	{ }
269 };
270 MODULE_DEVICE_TABLE(acpi, adc128_acpi_match);
271 
272 static struct spi_driver adc128_driver = {
273 	.driver = {
274 		.name = "adc128s052",
275 		.of_match_table = adc128_of_match,
276 		.acpi_match_table = adc128_acpi_match,
277 	},
278 	.probe = adc128_probe,
279 	.id_table = adc128_id,
280 };
281 module_spi_driver(adc128_driver);
282 
283 MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
284 MODULE_DESCRIPTION("Texas Instruments ADC128S052");
285 MODULE_LICENSE("GPL v2");
286