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
;
42 u8 data ____cacheline_aligned
;
45 #define MAX1118_CHANNEL(ch) \
47 .type = IIO_VOLTAGE, \
50 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
51 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
60 static const struct iio_chan_spec max1118_channels
[] = {
63 IIO_CHAN_SOFT_TIMESTAMP(2),
66 static int max1118_read(struct spi_device
*spi
, int channel
)
68 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
69 struct max1118
*adc
= iio_priv(indio_dev
);
70 struct spi_transfer xfers
[] = {
72 * To select CH1 for conversion, CNVST pin must be brought high
73 * and low for a second time.
77 .delay_usecs
= 1, /* > CNVST Low Time 100 ns */
81 * The acquisition interval begins with the falling edge of
82 * CNVST. The total acquisition and conversion process takes
97 ret
= spi_sync_transfer(spi
, xfers
+ 1, 2);
99 ret
= spi_sync_transfer(spi
, xfers
, 3);
107 static int max1118_get_vref_mV(struct spi_device
*spi
)
109 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
110 struct max1118
*adc
= iio_priv(indio_dev
);
111 const struct spi_device_id
*id
= spi_get_device_id(spi
);
114 switch (id
->driver_data
) {
120 vref_uV
= regulator_get_voltage(adc
->reg
);
123 return vref_uV
/ 1000;
129 static int max1118_read_raw(struct iio_dev
*indio_dev
,
130 struct iio_chan_spec
const *chan
,
131 int *val
, int *val2
, long mask
)
133 struct max1118
*adc
= iio_priv(indio_dev
);
136 case IIO_CHAN_INFO_RAW
:
137 mutex_lock(&adc
->lock
);
138 *val
= max1118_read(adc
->spi
, chan
->channel
);
139 mutex_unlock(&adc
->lock
);
144 case IIO_CHAN_INFO_SCALE
:
145 *val
= max1118_get_vref_mV(adc
->spi
);
150 return IIO_VAL_FRACTIONAL_LOG2
;
156 static const struct iio_info max1118_info
= {
157 .read_raw
= max1118_read_raw
,
160 static irqreturn_t
max1118_trigger_handler(int irq
, void *p
)
162 struct iio_poll_func
*pf
= p
;
163 struct iio_dev
*indio_dev
= pf
->indio_dev
;
164 struct max1118
*adc
= iio_priv(indio_dev
);
165 u8 data
[16] = { }; /* 2x 8-bit ADC data + padding + 8 bytes timestamp */
169 mutex_lock(&adc
->lock
);
171 for_each_set_bit(scan_index
, indio_dev
->active_scan_mask
,
172 indio_dev
->masklength
) {
173 const struct iio_chan_spec
*scan_chan
=
174 &indio_dev
->channels
[scan_index
];
175 int ret
= max1118_read(adc
->spi
, scan_chan
->channel
);
178 dev_warn(&adc
->spi
->dev
,
179 "failed to get conversion data\n");
186 iio_push_to_buffers_with_timestamp(indio_dev
, data
,
187 iio_get_time_ns(indio_dev
));
189 mutex_unlock(&adc
->lock
);
191 iio_trigger_notify_done(indio_dev
->trig
);
196 static int max1118_probe(struct spi_device
*spi
)
198 struct iio_dev
*indio_dev
;
200 const struct spi_device_id
*id
= spi_get_device_id(spi
);
203 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
207 adc
= iio_priv(indio_dev
);
209 mutex_init(&adc
->lock
);
211 if (id
->driver_data
== max1118
) {
212 adc
->reg
= devm_regulator_get(&spi
->dev
, "vref");
213 if (IS_ERR(adc
->reg
)) {
214 dev_err(&spi
->dev
, "failed to get vref regulator\n");
215 return PTR_ERR(adc
->reg
);
217 ret
= regulator_enable(adc
->reg
);
222 spi_set_drvdata(spi
, indio_dev
);
224 indio_dev
->name
= spi_get_device_id(spi
)->name
;
225 indio_dev
->dev
.parent
= &spi
->dev
;
226 indio_dev
->info
= &max1118_info
;
227 indio_dev
->modes
= INDIO_DIRECT_MODE
;
228 indio_dev
->channels
= max1118_channels
;
229 indio_dev
->num_channels
= ARRAY_SIZE(max1118_channels
);
232 * To reinitiate a conversion on CH0, it is necessary to allow for a
233 * conversion to be complete and all of the data to be read out. Once
234 * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
235 * into AutoShutdown mode until the next conversion is initiated.
237 max1118_read(spi
, 0);
239 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
240 max1118_trigger_handler
, NULL
);
242 goto err_reg_disable
;
244 ret
= iio_device_register(indio_dev
);
246 goto err_buffer_cleanup
;
251 iio_triggered_buffer_cleanup(indio_dev
);
253 if (id
->driver_data
== max1118
)
254 regulator_disable(adc
->reg
);
259 static int max1118_remove(struct spi_device
*spi
)
261 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
262 struct max1118
*adc
= iio_priv(indio_dev
);
263 const struct spi_device_id
*id
= spi_get_device_id(spi
);
265 iio_device_unregister(indio_dev
);
266 iio_triggered_buffer_cleanup(indio_dev
);
267 if (id
->driver_data
== max1118
)
268 return regulator_disable(adc
->reg
);
273 static const struct spi_device_id max1118_id
[] = {
274 { "max1117", max1117
},
275 { "max1118", max1118
},
276 { "max1119", max1119
},
279 MODULE_DEVICE_TABLE(spi
, max1118_id
);
283 static const struct of_device_id max1118_dt_ids
[] = {
284 { .compatible
= "maxim,max1117" },
285 { .compatible
= "maxim,max1118" },
286 { .compatible
= "maxim,max1119" },
289 MODULE_DEVICE_TABLE(of
, max1118_dt_ids
);
293 static struct spi_driver max1118_spi_driver
= {
296 .of_match_table
= of_match_ptr(max1118_dt_ids
),
298 .probe
= max1118_probe
,
299 .remove
= max1118_remove
,
300 .id_table
= max1118_id
,
302 module_spi_driver(max1118_spi_driver
);
304 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
305 MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
306 MODULE_LICENSE("GPL v2");