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