xref: /openbmc/linux/drivers/iio/adc/ti-adc128s052.c (revision c2d3cf36)
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/acpi.h>
13 #include <linux/err.h>
14 #include <linux/spi/spi.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/iio/iio.h>
18 #include <linux/property.h>
19 #include <linux/regulator/consumer.h>
20 
21 struct adc128_configuration {
22 	const struct iio_chan_spec	*channels;
23 	u8				num_channels;
24 };
25 
26 struct adc128 {
27 	struct spi_device *spi;
28 
29 	struct regulator *reg;
30 	struct mutex lock;
31 
32 	u8 buffer[2] __aligned(IIO_DMA_MINALIGN);
33 };
34 
35 static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
36 {
37 	int ret;
38 
39 	mutex_lock(&adc->lock);
40 
41 	adc->buffer[0] = channel << 3;
42 	adc->buffer[1] = 0;
43 
44 	ret = spi_write(adc->spi, &adc->buffer, 2);
45 	if (ret < 0) {
46 		mutex_unlock(&adc->lock);
47 		return ret;
48 	}
49 
50 	ret = spi_read(adc->spi, &adc->buffer, 2);
51 
52 	mutex_unlock(&adc->lock);
53 
54 	if (ret < 0)
55 		return ret;
56 
57 	return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF);
58 }
59 
60 static int adc128_read_raw(struct iio_dev *indio_dev,
61 			   struct iio_chan_spec const *channel, int *val,
62 			   int *val2, long mask)
63 {
64 	struct adc128 *adc = iio_priv(indio_dev);
65 	int ret;
66 
67 	switch (mask) {
68 	case IIO_CHAN_INFO_RAW:
69 
70 		ret = adc128_adc_conversion(adc, channel->channel);
71 		if (ret < 0)
72 			return ret;
73 
74 		*val = ret;
75 		return IIO_VAL_INT;
76 
77 	case IIO_CHAN_INFO_SCALE:
78 
79 		ret = regulator_get_voltage(adc->reg);
80 		if (ret < 0)
81 			return ret;
82 
83 		*val = ret / 1000;
84 		*val2 = 12;
85 		return IIO_VAL_FRACTIONAL_LOG2;
86 
87 	default:
88 		return -EINVAL;
89 	}
90 
91 }
92 
93 #define ADC128_VOLTAGE_CHANNEL(num)	\
94 	{ \
95 		.type = IIO_VOLTAGE, \
96 		.indexed = 1, \
97 		.channel = (num), \
98 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
99 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
100 	}
101 
102 static const struct iio_chan_spec adc128s052_channels[] = {
103 	ADC128_VOLTAGE_CHANNEL(0),
104 	ADC128_VOLTAGE_CHANNEL(1),
105 	ADC128_VOLTAGE_CHANNEL(2),
106 	ADC128_VOLTAGE_CHANNEL(3),
107 	ADC128_VOLTAGE_CHANNEL(4),
108 	ADC128_VOLTAGE_CHANNEL(5),
109 	ADC128_VOLTAGE_CHANNEL(6),
110 	ADC128_VOLTAGE_CHANNEL(7),
111 };
112 
113 static const struct iio_chan_spec adc122s021_channels[] = {
114 	ADC128_VOLTAGE_CHANNEL(0),
115 	ADC128_VOLTAGE_CHANNEL(1),
116 };
117 
118 static const struct iio_chan_spec adc124s021_channels[] = {
119 	ADC128_VOLTAGE_CHANNEL(0),
120 	ADC128_VOLTAGE_CHANNEL(1),
121 	ADC128_VOLTAGE_CHANNEL(2),
122 	ADC128_VOLTAGE_CHANNEL(3),
123 };
124 
125 static const struct adc128_configuration adc128_config[] = {
126 	{ adc128s052_channels, ARRAY_SIZE(adc128s052_channels) },
127 	{ adc122s021_channels, ARRAY_SIZE(adc122s021_channels) },
128 	{ adc124s021_channels, ARRAY_SIZE(adc124s021_channels) },
129 };
130 
131 static const struct iio_info adc128_info = {
132 	.read_raw = adc128_read_raw,
133 };
134 
135 static void adc128_disable_regulator(void *reg)
136 {
137 	regulator_disable(reg);
138 }
139 
140 static int adc128_probe(struct spi_device *spi)
141 {
142 	struct iio_dev *indio_dev;
143 	unsigned int config;
144 	struct adc128 *adc;
145 	int ret;
146 
147 	if (dev_fwnode(&spi->dev))
148 		config = (unsigned long) device_get_match_data(&spi->dev);
149 	else
150 		config = spi_get_device_id(spi)->driver_data;
151 
152 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
153 	if (!indio_dev)
154 		return -ENOMEM;
155 
156 	adc = iio_priv(indio_dev);
157 	adc->spi = spi;
158 
159 	indio_dev->name = spi_get_device_id(spi)->name;
160 	indio_dev->modes = INDIO_DIRECT_MODE;
161 	indio_dev->info = &adc128_info;
162 
163 	indio_dev->channels = adc128_config[config].channels;
164 	indio_dev->num_channels = adc128_config[config].num_channels;
165 
166 	adc->reg = devm_regulator_get(&spi->dev, "vref");
167 	if (IS_ERR(adc->reg))
168 		return PTR_ERR(adc->reg);
169 
170 	ret = regulator_enable(adc->reg);
171 	if (ret < 0)
172 		return ret;
173 	ret = devm_add_action_or_reset(&spi->dev, adc128_disable_regulator,
174 				       adc->reg);
175 	if (ret)
176 		return ret;
177 
178 	mutex_init(&adc->lock);
179 
180 	return devm_iio_device_register(&spi->dev, indio_dev);
181 }
182 
183 static const struct of_device_id adc128_of_match[] = {
184 	{ .compatible = "ti,adc128s052", .data = (void*)0L, },
185 	{ .compatible = "ti,adc122s021", .data = (void*)1L, },
186 	{ .compatible = "ti,adc122s051", .data = (void*)1L, },
187 	{ .compatible = "ti,adc122s101", .data = (void*)1L, },
188 	{ .compatible = "ti,adc124s021", .data = (void*)2L, },
189 	{ .compatible = "ti,adc124s051", .data = (void*)2L, },
190 	{ .compatible = "ti,adc124s101", .data = (void*)2L, },
191 	{ /* sentinel */ },
192 };
193 MODULE_DEVICE_TABLE(of, adc128_of_match);
194 
195 static const struct spi_device_id adc128_id[] = {
196 	{ "adc128s052", 0 },	/* index into adc128_config */
197 	{ "adc122s021",	1 },
198 	{ "adc122s051",	1 },
199 	{ "adc122s101",	1 },
200 	{ "adc124s021", 2 },
201 	{ "adc124s051", 2 },
202 	{ "adc124s101", 2 },
203 	{ }
204 };
205 MODULE_DEVICE_TABLE(spi, adc128_id);
206 
207 #ifdef CONFIG_ACPI
208 static const struct acpi_device_id adc128_acpi_match[] = {
209 	{ "AANT1280", 2 }, /* ADC124S021 compatible ACPI ID */
210 	{ }
211 };
212 MODULE_DEVICE_TABLE(acpi, adc128_acpi_match);
213 #endif
214 
215 static struct spi_driver adc128_driver = {
216 	.driver = {
217 		.name = "adc128s052",
218 		.of_match_table = adc128_of_match,
219 		.acpi_match_table = ACPI_PTR(adc128_acpi_match),
220 	},
221 	.probe = adc128_probe,
222 	.id_table = adc128_id,
223 };
224 module_spi_driver(adc128_driver);
225 
226 MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
227 MODULE_DESCRIPTION("Texas Instruments ADC128S052");
228 MODULE_LICENSE("GPL v2");
229