xref: /openbmc/linux/drivers/iio/adc/max1118.c (revision 11c416e3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * MAX1117/MAX1118/MAX1119 8-bit, dual-channel ADCs driver
4  *
5  * Copyright (c) 2017 Akinobu Mita <akinobu.mita@gmail.com>
6  *
7  * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1117-MAX1119.pdf
8  *
9  * SPI interface connections
10  *
11  * SPI                MAXIM
12  * Master  Direction  MAX1117/8/9
13  * ------  ---------  -----------
14  * nCS        -->     CNVST
15  * SCK        -->     SCLK
16  * MISO       <--     DOUT
17  * ------  ---------  -----------
18  */
19 
20 #include <linux/module.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/spi/spi.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/triggered_buffer.h>
26 #include <linux/iio/trigger_consumer.h>
27 #include <linux/regulator/consumer.h>
28 
29 enum max1118_id {
30 	max1117,
31 	max1118,
32 	max1119,
33 };
34 
35 struct max1118 {
36 	struct spi_device *spi;
37 	struct mutex lock;
38 	struct regulator *reg;
39 
40 	u8 data ____cacheline_aligned;
41 };
42 
43 #define MAX1118_CHANNEL(ch)						\
44 	{								\
45 		.type = IIO_VOLTAGE,					\
46 		.indexed = 1,						\
47 		.channel = (ch),					\
48 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
49 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
50 		.scan_index = ch,					\
51 		.scan_type = {						\
52 			.sign = 'u',					\
53 			.realbits = 8,					\
54 			.storagebits = 8,				\
55 		},							\
56 	}
57 
58 static const struct iio_chan_spec max1118_channels[] = {
59 	MAX1118_CHANNEL(0),
60 	MAX1118_CHANNEL(1),
61 	IIO_CHAN_SOFT_TIMESTAMP(2),
62 };
63 
64 static int max1118_read(struct spi_device *spi, int channel)
65 {
66 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
67 	struct max1118 *adc = iio_priv(indio_dev);
68 	struct spi_transfer xfers[] = {
69 		/*
70 		 * To select CH1 for conversion, CNVST pin must be brought high
71 		 * and low for a second time.
72 		 */
73 		{
74 			.len = 0,
75 			.delay = {	/* > CNVST Low Time 100 ns */
76 				.value = 1,
77 				.unit = SPI_DELAY_UNIT_USECS
78 			},
79 			.cs_change = 1,
80 		},
81 		/*
82 		 * The acquisition interval begins with the falling edge of
83 		 * CNVST.  The total acquisition and conversion process takes
84 		 * <7.5us.
85 		 */
86 		{
87 			.len = 0,
88 			.delay = {
89 				.value = 8,
90 				.unit = SPI_DELAY_UNIT_USECS
91 			},
92 		},
93 		{
94 			.rx_buf = &adc->data,
95 			.len = 1,
96 		},
97 	};
98 	int ret;
99 
100 	if (channel == 0)
101 		ret = spi_sync_transfer(spi, xfers + 1, 2);
102 	else
103 		ret = spi_sync_transfer(spi, xfers, 3);
104 
105 	if (ret)
106 		return ret;
107 
108 	return adc->data;
109 }
110 
111 static int max1118_get_vref_mV(struct spi_device *spi)
112 {
113 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
114 	struct max1118 *adc = iio_priv(indio_dev);
115 	const struct spi_device_id *id = spi_get_device_id(spi);
116 	int vref_uV;
117 
118 	switch (id->driver_data) {
119 	case max1117:
120 		return 2048;
121 	case max1119:
122 		return 4096;
123 	case max1118:
124 		vref_uV = regulator_get_voltage(adc->reg);
125 		if (vref_uV < 0)
126 			return vref_uV;
127 		return vref_uV / 1000;
128 	}
129 
130 	return -ENODEV;
131 }
132 
133 static int max1118_read_raw(struct iio_dev *indio_dev,
134 			struct iio_chan_spec const *chan,
135 			int *val, int *val2, long mask)
136 {
137 	struct max1118 *adc = iio_priv(indio_dev);
138 
139 	switch (mask) {
140 	case IIO_CHAN_INFO_RAW:
141 		mutex_lock(&adc->lock);
142 		*val = max1118_read(adc->spi, chan->channel);
143 		mutex_unlock(&adc->lock);
144 		if (*val < 0)
145 			return *val;
146 
147 		return IIO_VAL_INT;
148 	case IIO_CHAN_INFO_SCALE:
149 		*val = max1118_get_vref_mV(adc->spi);
150 		if (*val < 0)
151 			return *val;
152 		*val2 = 8;
153 
154 		return IIO_VAL_FRACTIONAL_LOG2;
155 	}
156 
157 	return -EINVAL;
158 }
159 
160 static const struct iio_info max1118_info = {
161 	.read_raw = max1118_read_raw,
162 };
163 
164 static irqreturn_t max1118_trigger_handler(int irq, void *p)
165 {
166 	struct iio_poll_func *pf = p;
167 	struct iio_dev *indio_dev = pf->indio_dev;
168 	struct max1118 *adc = iio_priv(indio_dev);
169 	u8 data[16] = { }; /* 2x 8-bit ADC data + padding + 8 bytes timestamp */
170 	int scan_index;
171 	int i = 0;
172 
173 	mutex_lock(&adc->lock);
174 
175 	for_each_set_bit(scan_index, indio_dev->active_scan_mask,
176 			indio_dev->masklength) {
177 		const struct iio_chan_spec *scan_chan =
178 				&indio_dev->channels[scan_index];
179 		int ret = max1118_read(adc->spi, scan_chan->channel);
180 
181 		if (ret < 0) {
182 			dev_warn(&adc->spi->dev,
183 				"failed to get conversion data\n");
184 			goto out;
185 		}
186 
187 		data[i] = ret;
188 		i++;
189 	}
190 	iio_push_to_buffers_with_timestamp(indio_dev, data,
191 					   iio_get_time_ns(indio_dev));
192 out:
193 	mutex_unlock(&adc->lock);
194 
195 	iio_trigger_notify_done(indio_dev->trig);
196 
197 	return IRQ_HANDLED;
198 }
199 
200 static int max1118_probe(struct spi_device *spi)
201 {
202 	struct iio_dev *indio_dev;
203 	struct max1118 *adc;
204 	const struct spi_device_id *id = spi_get_device_id(spi);
205 	int ret;
206 
207 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
208 	if (!indio_dev)
209 		return -ENOMEM;
210 
211 	adc = iio_priv(indio_dev);
212 	adc->spi = spi;
213 	mutex_init(&adc->lock);
214 
215 	if (id->driver_data == max1118) {
216 		adc->reg = devm_regulator_get(&spi->dev, "vref");
217 		if (IS_ERR(adc->reg)) {
218 			dev_err(&spi->dev, "failed to get vref regulator\n");
219 			return PTR_ERR(adc->reg);
220 		}
221 		ret = regulator_enable(adc->reg);
222 		if (ret)
223 			return ret;
224 	}
225 
226 	spi_set_drvdata(spi, indio_dev);
227 
228 	indio_dev->name = spi_get_device_id(spi)->name;
229 	indio_dev->info = &max1118_info;
230 	indio_dev->modes = INDIO_DIRECT_MODE;
231 	indio_dev->channels = max1118_channels;
232 	indio_dev->num_channels = ARRAY_SIZE(max1118_channels);
233 
234 	/*
235 	 * To reinitiate a conversion on CH0, it is necessary to allow for a
236 	 * conversion to be complete and all of the data to be read out.  Once
237 	 * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
238 	 * into AutoShutdown mode until the next conversion is initiated.
239 	 */
240 	max1118_read(spi, 0);
241 
242 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
243 					max1118_trigger_handler, NULL);
244 	if (ret)
245 		goto err_reg_disable;
246 
247 	ret = iio_device_register(indio_dev);
248 	if (ret)
249 		goto err_buffer_cleanup;
250 
251 	return 0;
252 
253 err_buffer_cleanup:
254 	iio_triggered_buffer_cleanup(indio_dev);
255 err_reg_disable:
256 	if (id->driver_data == max1118)
257 		regulator_disable(adc->reg);
258 
259 	return ret;
260 }
261 
262 static int max1118_remove(struct spi_device *spi)
263 {
264 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
265 	struct max1118 *adc = iio_priv(indio_dev);
266 	const struct spi_device_id *id = spi_get_device_id(spi);
267 
268 	iio_device_unregister(indio_dev);
269 	iio_triggered_buffer_cleanup(indio_dev);
270 	if (id->driver_data == max1118)
271 		return regulator_disable(adc->reg);
272 
273 	return 0;
274 }
275 
276 static const struct spi_device_id max1118_id[] = {
277 	{ "max1117", max1117 },
278 	{ "max1118", max1118 },
279 	{ "max1119", max1119 },
280 	{}
281 };
282 MODULE_DEVICE_TABLE(spi, max1118_id);
283 
284 static const struct of_device_id max1118_dt_ids[] = {
285 	{ .compatible = "maxim,max1117" },
286 	{ .compatible = "maxim,max1118" },
287 	{ .compatible = "maxim,max1119" },
288 	{},
289 };
290 MODULE_DEVICE_TABLE(of, max1118_dt_ids);
291 
292 static struct spi_driver max1118_spi_driver = {
293 	.driver = {
294 		.name = "max1118",
295 		.of_match_table = max1118_dt_ids,
296 	},
297 	.probe = max1118_probe,
298 	.remove = max1118_remove,
299 	.id_table = max1118_id,
300 };
301 module_spi_driver(max1118_spi_driver);
302 
303 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
304 MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
305 MODULE_LICENSE("GPL v2");
306