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,
163 .driver_module = THIS_MODULE,
166 static irqreturn_t max1118_trigger_handler(int irq, void *p)
168 struct iio_poll_func *pf = p;
169 struct iio_dev *indio_dev = pf->indio_dev;
170 struct max1118 *adc = iio_priv(indio_dev);
174 mutex_lock(&adc->lock);
176 for_each_set_bit(scan_index, indio_dev->active_scan_mask,
177 indio_dev->masklength) {
178 const struct iio_chan_spec *scan_chan =
179 &indio_dev->channels[scan_index];
180 int ret = max1118_read(adc->spi, scan_chan->channel);
183 dev_warn(&adc->spi->dev,
184 "failed to get conversion data\n");
188 adc->scan.channels[i] = ret;
191 iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
192 iio_get_time_ns(indio_dev));
194 mutex_unlock(&adc->lock);
196 iio_trigger_notify_done(indio_dev->trig);
201 static int max1118_probe(struct spi_device *spi)
203 struct iio_dev *indio_dev;
205 const struct spi_device_id *id = spi_get_device_id(spi);
208 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
212 adc = iio_priv(indio_dev);
214 mutex_init(&adc->lock);
216 if (id->driver_data == max1118) {
217 adc->reg = devm_regulator_get(&spi->dev, "vref");
218 if (IS_ERR(adc->reg)) {
219 dev_err(&spi->dev, "failed to get vref regulator\n");
220 return PTR_ERR(adc->reg);
222 ret = regulator_enable(adc->reg);
227 spi_set_drvdata(spi, indio_dev);
229 indio_dev->name = spi_get_device_id(spi)->name;
230 indio_dev->dev.parent = &spi->dev;
231 indio_dev->info = &max1118_info;
232 indio_dev->modes = INDIO_DIRECT_MODE;
233 indio_dev->channels = max1118_channels;
234 indio_dev->num_channels = ARRAY_SIZE(max1118_channels);
237 * To reinitiate a conversion on CH0, it is necessary to allow for a
238 * conversion to be complete and all of the data to be read out. Once
239 * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
240 * into AutoShutdown mode until the next conversion is initiated.
242 max1118_read(spi, 0);
244 ret = iio_triggered_buffer_setup(indio_dev, NULL,
245 max1118_trigger_handler, NULL);
247 goto err_reg_disable;
249 ret = iio_device_register(indio_dev);
251 goto err_buffer_cleanup;
256 iio_triggered_buffer_cleanup(indio_dev);
258 if (id->driver_data == max1118)
259 regulator_disable(adc->reg);
264 static int max1118_remove(struct spi_device *spi)
266 struct iio_dev *indio_dev = spi_get_drvdata(spi);
267 struct max1118 *adc = iio_priv(indio_dev);
268 const struct spi_device_id *id = spi_get_device_id(spi);
270 iio_device_unregister(indio_dev);
271 iio_triggered_buffer_cleanup(indio_dev);
272 if (id->driver_data == max1118)
273 return regulator_disable(adc->reg);
278 static const struct spi_device_id max1118_id[] = {
279 { "max1117", max1117 },
280 { "max1118", max1118 },
281 { "max1119", max1119 },
284 MODULE_DEVICE_TABLE(spi, max1118_id);
288 static const struct of_device_id max1118_dt_ids[] = {
289 { .compatible = "maxim,max1117" },
290 { .compatible = "maxim,max1118" },
291 { .compatible = "maxim,max1119" },
294 MODULE_DEVICE_TABLE(of, max1118_dt_ids);
298 static struct spi_driver max1118_spi_driver = {
301 .of_match_table = of_match_ptr(max1118_dt_ids),
303 .probe = max1118_probe,
304 .remove = max1118_remove,
305 .id_table = max1118_id,
307 module_spi_driver(max1118_spi_driver);
309 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
310 MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
311 MODULE_LICENSE("GPL v2");