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