1 // SPDX-License-Identifier: GPL-2.0-only
3 * MAX1117/MAX1118/MAX1119 8-bit, dual-channel ADCs driver
5 * Copyright (c) 2017 Akinobu Mita <akinobu.mita@gmail.com>
7 * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1117-MAX1119.pdf
9 * SPI interface connections
12 * Master Direction MAX1117/8/9
13 * ------ --------- -----------
17 * ------ --------- -----------
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>
36 struct spi_device *spi;
38 struct regulator *reg;
40 u8 data ____cacheline_aligned;
43 #define MAX1118_CHANNEL(ch) \
45 .type = IIO_VOLTAGE, \
48 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
49 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
58 static const struct iio_chan_spec max1118_channels[] = {
61 IIO_CHAN_SOFT_TIMESTAMP(2),
64 static int max1118_read(struct spi_device *spi, int channel)
66 struct iio_dev *indio_dev = spi_get_drvdata(spi);
67 struct max1118 *adc = iio_priv(indio_dev);
68 struct spi_transfer xfers[] = {
70 * To select CH1 for conversion, CNVST pin must be brought high
71 * and low for a second time.
75 .delay = { /* > CNVST Low Time 100 ns */
77 .unit = SPI_DELAY_UNIT_USECS
82 * The acquisition interval begins with the falling edge of
83 * CNVST. The total acquisition and conversion process takes
90 .unit = SPI_DELAY_UNIT_USECS
101 ret = spi_sync_transfer(spi, xfers + 1, 2);
103 ret = spi_sync_transfer(spi, xfers, 3);
111 static int max1118_get_vref_mV(struct spi_device *spi)
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);
118 switch (id->driver_data) {
124 vref_uV = regulator_get_voltage(adc->reg);
127 return vref_uV / 1000;
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)
137 struct max1118 *adc = iio_priv(indio_dev);
140 case IIO_CHAN_INFO_RAW:
141 mutex_lock(&adc->lock);
142 *val = max1118_read(adc->spi, chan->channel);
143 mutex_unlock(&adc->lock);
148 case IIO_CHAN_INFO_SCALE:
149 *val = max1118_get_vref_mV(adc->spi);
154 return IIO_VAL_FRACTIONAL_LOG2;
160 static const struct iio_info max1118_info = {
161 .read_raw = max1118_read_raw,
164 static irqreturn_t max1118_trigger_handler(int irq, void *p)
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 */
173 mutex_lock(&adc->lock);
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);
182 dev_warn(&adc->spi->dev,
183 "failed to get conversion data\n");
190 iio_push_to_buffers_with_timestamp(indio_dev, data,
191 iio_get_time_ns(indio_dev));
193 mutex_unlock(&adc->lock);
195 iio_trigger_notify_done(indio_dev->trig);
200 static int max1118_probe(struct spi_device *spi)
202 struct iio_dev *indio_dev;
204 const struct spi_device_id *id = spi_get_device_id(spi);
207 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
211 adc = iio_priv(indio_dev);
213 mutex_init(&adc->lock);
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);
221 ret = regulator_enable(adc->reg);
226 spi_set_drvdata(spi, indio_dev);
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);
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.
240 max1118_read(spi, 0);
242 ret = iio_triggered_buffer_setup(indio_dev, NULL,
243 max1118_trigger_handler, NULL);
245 goto err_reg_disable;
247 ret = iio_device_register(indio_dev);
249 goto err_buffer_cleanup;
254 iio_triggered_buffer_cleanup(indio_dev);
256 if (id->driver_data == max1118)
257 regulator_disable(adc->reg);
262 static int max1118_remove(struct spi_device *spi)
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);
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);
276 static const struct spi_device_id max1118_id[] = {
277 { "max1117", max1117 },
278 { "max1118", max1118 },
279 { "max1119", max1119 },
282 MODULE_DEVICE_TABLE(spi, max1118_id);
284 static const struct of_device_id max1118_dt_ids[] = {
285 { .compatible = "maxim,max1117" },
286 { .compatible = "maxim,max1118" },
287 { .compatible = "maxim,max1119" },
290 MODULE_DEVICE_TABLE(of, max1118_dt_ids);
292 static struct spi_driver max1118_spi_driver = {
295 .of_match_table = max1118_dt_ids,
297 .probe = max1118_probe,
298 .remove = max1118_remove,
299 .id_table = max1118_id,
301 module_spi_driver(max1118_spi_driver);
303 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
304 MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
305 MODULE_LICENSE("GPL v2");