2 * MAX1117/MAX1118/MAX1119 8-bit, dual-channel ADCs driver
4 * Copyright (c) 2017 Akinobu Mita <akinobu.mita@gmail.com>
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.
10 * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1117-MAX1119.pdf
12 * SPI interface connections
15 * Master Direction MAX1117/8/9
16 * ------ --------- -----------
20 * ------ --------- -----------
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>
38 struct spi_device *spi;
40 struct regulator *reg;
41 /* Ensure natural alignment of buffer elements */
47 u8 data ____cacheline_aligned;
50 #define MAX1118_CHANNEL(ch) \
52 .type = IIO_VOLTAGE, \
55 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
56 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
65 static const struct iio_chan_spec max1118_channels[] = {
68 IIO_CHAN_SOFT_TIMESTAMP(2),
71 static int max1118_read(struct spi_device *spi, int channel)
73 struct iio_dev *indio_dev = spi_get_drvdata(spi);
74 struct max1118 *adc = iio_priv(indio_dev);
75 struct spi_transfer xfers[] = {
77 * To select CH1 for conversion, CNVST pin must be brought high
78 * and low for a second time.
82 .delay_usecs = 1, /* > CNVST Low Time 100 ns */
86 * The acquisition interval begins with the falling edge of
87 * CNVST. The total acquisition and conversion process takes
102 ret = spi_sync_transfer(spi, xfers + 1, 2);
104 ret = spi_sync_transfer(spi, xfers, 3);
112 static int max1118_get_vref_mV(struct spi_device *spi)
114 struct iio_dev *indio_dev = spi_get_drvdata(spi);
115 struct max1118 *adc = iio_priv(indio_dev);
116 const struct spi_device_id *id = spi_get_device_id(spi);
119 switch (id->driver_data) {
125 vref_uV = regulator_get_voltage(adc->reg);
128 return vref_uV / 1000;
134 static int max1118_read_raw(struct iio_dev *indio_dev,
135 struct iio_chan_spec const *chan,
136 int *val, int *val2, long mask)
138 struct max1118 *adc = iio_priv(indio_dev);
141 case IIO_CHAN_INFO_RAW:
142 mutex_lock(&adc->lock);
143 *val = max1118_read(adc->spi, chan->channel);
144 mutex_unlock(&adc->lock);
149 case IIO_CHAN_INFO_SCALE:
150 *val = max1118_get_vref_mV(adc->spi);
155 return IIO_VAL_FRACTIONAL_LOG2;
161 static const struct iio_info max1118_info = {
162 .read_raw = max1118_read_raw,
165 static irqreturn_t max1118_trigger_handler(int irq, void *p)
167 struct iio_poll_func *pf = p;
168 struct iio_dev *indio_dev = pf->indio_dev;
169 struct max1118 *adc = iio_priv(indio_dev);
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");
187 adc->scan.channels[i] = ret;
190 iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
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->dev.parent = &spi->dev;
230 indio_dev->info = &max1118_info;
231 indio_dev->modes = INDIO_DIRECT_MODE;
232 indio_dev->channels = max1118_channels;
233 indio_dev->num_channels = ARRAY_SIZE(max1118_channels);
236 * To reinitiate a conversion on CH0, it is necessary to allow for a
237 * conversion to be complete and all of the data to be read out. Once
238 * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
239 * into AutoShutdown mode until the next conversion is initiated.
241 max1118_read(spi, 0);
243 ret = iio_triggered_buffer_setup(indio_dev, NULL,
244 max1118_trigger_handler, NULL);
246 goto err_reg_disable;
248 ret = iio_device_register(indio_dev);
250 goto err_buffer_cleanup;
255 iio_triggered_buffer_cleanup(indio_dev);
257 if (id->driver_data == max1118)
258 regulator_disable(adc->reg);
263 static int max1118_remove(struct spi_device *spi)
265 struct iio_dev *indio_dev = spi_get_drvdata(spi);
266 struct max1118 *adc = iio_priv(indio_dev);
267 const struct spi_device_id *id = spi_get_device_id(spi);
269 iio_device_unregister(indio_dev);
270 iio_triggered_buffer_cleanup(indio_dev);
271 if (id->driver_data == max1118)
272 return regulator_disable(adc->reg);
277 static const struct spi_device_id max1118_id[] = {
278 { "max1117", max1117 },
279 { "max1118", max1118 },
280 { "max1119", max1119 },
283 MODULE_DEVICE_TABLE(spi, max1118_id);
287 static const struct of_device_id max1118_dt_ids[] = {
288 { .compatible = "maxim,max1117" },
289 { .compatible = "maxim,max1118" },
290 { .compatible = "maxim,max1119" },
293 MODULE_DEVICE_TABLE(of, max1118_dt_ids);
297 static struct spi_driver max1118_spi_driver = {
300 .of_match_table = of_match_ptr(max1118_dt_ids),
302 .probe = max1118_probe,
303 .remove = max1118_remove,
304 .id_table = max1118_id,
306 module_spi_driver(max1118_spi_driver);
308 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
309 MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
310 MODULE_LICENSE("GPL v2");