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
__aligned(IIO_DMA_MINALIGN
);
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 iio_dev
*indio_dev
, int channel
)
71 struct max1118
*adc
= iio_priv(indio_dev
);
72 struct spi_transfer xfers
[] = {
74 * To select CH1 for conversion, CNVST pin must be brought high
75 * and low for a second time.
79 .delay
= { /* > CNVST Low Time 100 ns */
81 .unit
= SPI_DELAY_UNIT_USECS
86 * The acquisition interval begins with the falling edge of
87 * CNVST. The total acquisition and conversion process takes
94 .unit
= SPI_DELAY_UNIT_USECS
105 ret
= spi_sync_transfer(adc
->spi
, xfers
+ 1, 2);
107 ret
= spi_sync_transfer(adc
->spi
, xfers
, 3);
115 static int max1118_get_vref_mV(struct iio_dev
*indio_dev
)
117 struct max1118
*adc
= iio_priv(indio_dev
);
118 const struct spi_device_id
*id
= spi_get_device_id(adc
->spi
);
121 switch (id
->driver_data
) {
127 vref_uV
= regulator_get_voltage(adc
->reg
);
130 return vref_uV
/ 1000;
136 static int max1118_read_raw(struct iio_dev
*indio_dev
,
137 struct iio_chan_spec
const *chan
,
138 int *val
, int *val2
, long mask
)
140 struct max1118
*adc
= iio_priv(indio_dev
);
143 case IIO_CHAN_INFO_RAW
:
144 mutex_lock(&adc
->lock
);
145 *val
= max1118_read(indio_dev
, chan
->channel
);
146 mutex_unlock(&adc
->lock
);
151 case IIO_CHAN_INFO_SCALE
:
152 *val
= max1118_get_vref_mV(indio_dev
);
157 return IIO_VAL_FRACTIONAL_LOG2
;
163 static const struct iio_info max1118_info
= {
164 .read_raw
= max1118_read_raw
,
167 static irqreturn_t
max1118_trigger_handler(int irq
, void *p
)
169 struct iio_poll_func
*pf
= p
;
170 struct iio_dev
*indio_dev
= pf
->indio_dev
;
171 struct max1118
*adc
= iio_priv(indio_dev
);
175 mutex_lock(&adc
->lock
);
177 iio_for_each_active_channel(indio_dev
, scan_index
) {
178 const struct iio_chan_spec
*scan_chan
=
179 &indio_dev
->channels
[scan_index
];
180 int ret
= max1118_read(indio_dev
, 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 void max1118_reg_disable(void *reg
)
203 regulator_disable(reg
);
206 static int max1118_probe(struct spi_device
*spi
)
208 struct iio_dev
*indio_dev
;
210 const struct spi_device_id
*id
= spi_get_device_id(spi
);
213 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
217 adc
= iio_priv(indio_dev
);
219 mutex_init(&adc
->lock
);
221 if (id
->driver_data
== max1118
) {
222 adc
->reg
= devm_regulator_get(&spi
->dev
, "vref");
223 if (IS_ERR(adc
->reg
))
224 return dev_err_probe(&spi
->dev
, PTR_ERR(adc
->reg
),
225 "failed to get vref regulator\n");
226 ret
= regulator_enable(adc
->reg
);
230 ret
= devm_add_action_or_reset(&spi
->dev
, max1118_reg_disable
,
237 indio_dev
->name
= spi_get_device_id(spi
)->name
;
238 indio_dev
->info
= &max1118_info
;
239 indio_dev
->modes
= INDIO_DIRECT_MODE
;
240 indio_dev
->channels
= max1118_channels
;
241 indio_dev
->num_channels
= ARRAY_SIZE(max1118_channels
);
244 * To reinitiate a conversion on CH0, it is necessary to allow for a
245 * conversion to be complete and all of the data to be read out. Once
246 * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
247 * into AutoShutdown mode until the next conversion is initiated.
249 max1118_read(indio_dev
, 0);
251 ret
= devm_iio_triggered_buffer_setup(&spi
->dev
, indio_dev
, NULL
,
252 max1118_trigger_handler
, NULL
);
256 return devm_iio_device_register(&spi
->dev
, indio_dev
);
259 static const struct spi_device_id max1118_id
[] = {
260 { "max1117", max1117
},
261 { "max1118", max1118
},
262 { "max1119", max1119
},
265 MODULE_DEVICE_TABLE(spi
, max1118_id
);
267 static const struct of_device_id max1118_dt_ids
[] = {
268 { .compatible
= "maxim,max1117" },
269 { .compatible
= "maxim,max1118" },
270 { .compatible
= "maxim,max1119" },
273 MODULE_DEVICE_TABLE(of
, max1118_dt_ids
);
275 static struct spi_driver max1118_spi_driver
= {
278 .of_match_table
= max1118_dt_ids
,
280 .probe
= max1118_probe
,
281 .id_table
= max1118_id
,
283 module_spi_driver(max1118_spi_driver
);
285 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
286 MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
287 MODULE_LICENSE("GPL v2");