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
;
39 /* Ensure natural alignment of buffer elements */
45 u8 data ____cacheline_aligned
;
48 #define MAX1118_CHANNEL(ch) \
50 .type = IIO_VOLTAGE, \
53 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
54 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
63 static const struct iio_chan_spec max1118_channels
[] = {
66 IIO_CHAN_SOFT_TIMESTAMP(2),
69 static int max1118_read(struct spi_device
*spi
, int channel
)
71 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
72 struct max1118
*adc
= iio_priv(indio_dev
);
73 struct spi_transfer xfers
[] = {
75 * To select CH1 for conversion, CNVST pin must be brought high
76 * and low for a second time.
80 .delay
= { /* > CNVST Low Time 100 ns */
82 .unit
= SPI_DELAY_UNIT_USECS
87 * The acquisition interval begins with the falling edge of
88 * CNVST. The total acquisition and conversion process takes
95 .unit
= SPI_DELAY_UNIT_USECS
106 ret
= spi_sync_transfer(spi
, xfers
+ 1, 2);
108 ret
= spi_sync_transfer(spi
, xfers
, 3);
116 static int max1118_get_vref_mV(struct spi_device
*spi
)
118 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
119 struct max1118
*adc
= iio_priv(indio_dev
);
120 const struct spi_device_id
*id
= spi_get_device_id(spi
);
123 switch (id
->driver_data
) {
129 vref_uV
= regulator_get_voltage(adc
->reg
);
132 return vref_uV
/ 1000;
138 static int max1118_read_raw(struct iio_dev
*indio_dev
,
139 struct iio_chan_spec
const *chan
,
140 int *val
, int *val2
, long mask
)
142 struct max1118
*adc
= iio_priv(indio_dev
);
145 case IIO_CHAN_INFO_RAW
:
146 mutex_lock(&adc
->lock
);
147 *val
= max1118_read(adc
->spi
, chan
->channel
);
148 mutex_unlock(&adc
->lock
);
153 case IIO_CHAN_INFO_SCALE
:
154 *val
= max1118_get_vref_mV(adc
->spi
);
159 return IIO_VAL_FRACTIONAL_LOG2
;
165 static const struct iio_info max1118_info
= {
166 .read_raw
= max1118_read_raw
,
169 static irqreturn_t
max1118_trigger_handler(int irq
, void *p
)
171 struct iio_poll_func
*pf
= p
;
172 struct iio_dev
*indio_dev
= pf
->indio_dev
;
173 struct max1118
*adc
= iio_priv(indio_dev
);
177 mutex_lock(&adc
->lock
);
179 for_each_set_bit(scan_index
, indio_dev
->active_scan_mask
,
180 indio_dev
->masklength
) {
181 const struct iio_chan_spec
*scan_chan
=
182 &indio_dev
->channels
[scan_index
];
183 int ret
= max1118_read(adc
->spi
, scan_chan
->channel
);
186 dev_warn(&adc
->spi
->dev
,
187 "failed to get conversion data\n");
191 adc
->scan
.channels
[i
] = ret
;
194 iio_push_to_buffers_with_timestamp(indio_dev
, &adc
->scan
,
195 iio_get_time_ns(indio_dev
));
197 mutex_unlock(&adc
->lock
);
199 iio_trigger_notify_done(indio_dev
->trig
);
204 static int max1118_probe(struct spi_device
*spi
)
206 struct iio_dev
*indio_dev
;
208 const struct spi_device_id
*id
= spi_get_device_id(spi
);
211 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
215 adc
= iio_priv(indio_dev
);
217 mutex_init(&adc
->lock
);
219 if (id
->driver_data
== max1118
) {
220 adc
->reg
= devm_regulator_get(&spi
->dev
, "vref");
221 if (IS_ERR(adc
->reg
)) {
222 dev_err(&spi
->dev
, "failed to get vref regulator\n");
223 return PTR_ERR(adc
->reg
);
225 ret
= regulator_enable(adc
->reg
);
230 spi_set_drvdata(spi
, indio_dev
);
232 indio_dev
->name
= spi_get_device_id(spi
)->name
;
233 indio_dev
->info
= &max1118_info
;
234 indio_dev
->modes
= INDIO_DIRECT_MODE
;
235 indio_dev
->channels
= max1118_channels
;
236 indio_dev
->num_channels
= ARRAY_SIZE(max1118_channels
);
239 * To reinitiate a conversion on CH0, it is necessary to allow for a
240 * conversion to be complete and all of the data to be read out. Once
241 * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
242 * into AutoShutdown mode until the next conversion is initiated.
244 max1118_read(spi
, 0);
246 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
247 max1118_trigger_handler
, NULL
);
249 goto err_reg_disable
;
251 ret
= iio_device_register(indio_dev
);
253 goto err_buffer_cleanup
;
258 iio_triggered_buffer_cleanup(indio_dev
);
260 if (id
->driver_data
== max1118
)
261 regulator_disable(adc
->reg
);
266 static int max1118_remove(struct spi_device
*spi
)
268 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
269 struct max1118
*adc
= iio_priv(indio_dev
);
270 const struct spi_device_id
*id
= spi_get_device_id(spi
);
272 iio_device_unregister(indio_dev
);
273 iio_triggered_buffer_cleanup(indio_dev
);
274 if (id
->driver_data
== max1118
)
275 return regulator_disable(adc
->reg
);
280 static const struct spi_device_id max1118_id
[] = {
281 { "max1117", max1117
},
282 { "max1118", max1118
},
283 { "max1119", max1119
},
286 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
);
296 static struct spi_driver max1118_spi_driver
= {
299 .of_match_table
= max1118_dt_ids
,
301 .probe
= max1118_probe
,
302 .remove
= max1118_remove
,
303 .id_table
= max1118_id
,
305 module_spi_driver(max1118_spi_driver
);
307 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
308 MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
309 MODULE_LICENSE("GPL v2");