1 // SPDX-License-Identifier: GPL-2.0
4 * Maxim max11100 ADC Driver with IIO interface
6 * Copyright (C) 2016-17 Renesas Electronics Corporation
7 * Copyright (C) 2016-17 Jacopo Mondi
9 #include <linux/delay.h>
10 #include <linux/kernel.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/spi/spi.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/driver.h>
20 * LSB is the ADC single digital step
21 * 1 LSB = (vref_mv / 2 ^ 16)
23 * LSB is used to calculate analog voltage value
24 * from the number of ADC steps count
28 #define MAX11100_LSB_DIV (1 << 16)
30 struct max11100_state
{
31 struct regulator
*vref_reg
;
32 struct spi_device
*spi
;
35 * DMA (thus cache coherency maintenance) requires the
36 * transfer buffers to live in their own cache lines.
38 u8 buffer
[3] ____cacheline_aligned
;
41 static const struct iio_chan_spec max11100_channels
[] = {
44 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
45 BIT(IIO_CHAN_INFO_SCALE
),
49 static int max11100_read_single(struct iio_dev
*indio_dev
, int *val
)
52 struct max11100_state
*state
= iio_priv(indio_dev
);
54 ret
= spi_read(state
->spi
, state
->buffer
, sizeof(state
->buffer
));
56 dev_err(&indio_dev
->dev
, "SPI transfer failed\n");
60 /* the first 8 bits sent out from ADC must be 0s */
61 if (state
->buffer
[0]) {
62 dev_err(&indio_dev
->dev
, "Invalid value: buffer[0] != 0\n");
66 *val
= (state
->buffer
[1] << 8) | state
->buffer
[2];
71 static int max11100_read_raw(struct iio_dev
*indio_dev
,
72 struct iio_chan_spec
const *chan
,
73 int *val
, int *val2
, long info
)
76 struct max11100_state
*state
= iio_priv(indio_dev
);
79 case IIO_CHAN_INFO_RAW
:
80 ret
= max11100_read_single(indio_dev
, val
);
86 case IIO_CHAN_INFO_SCALE
:
87 vref_uv
= regulator_get_voltage(state
->vref_reg
);
89 /* dummy regulator "get_voltage" returns -EINVAL */
92 *val
= vref_uv
/ 1000;
93 *val2
= MAX11100_LSB_DIV
;
94 return IIO_VAL_FRACTIONAL
;
100 static const struct iio_info max11100_info
= {
101 .read_raw
= max11100_read_raw
,
104 static int max11100_probe(struct spi_device
*spi
)
107 struct iio_dev
*indio_dev
;
108 struct max11100_state
*state
;
110 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*state
));
114 spi_set_drvdata(spi
, indio_dev
);
116 state
= iio_priv(indio_dev
);
119 indio_dev
->name
= "max11100";
120 indio_dev
->info
= &max11100_info
;
121 indio_dev
->modes
= INDIO_DIRECT_MODE
;
122 indio_dev
->channels
= max11100_channels
;
123 indio_dev
->num_channels
= ARRAY_SIZE(max11100_channels
);
125 state
->vref_reg
= devm_regulator_get(&spi
->dev
, "vref");
126 if (IS_ERR(state
->vref_reg
))
127 return PTR_ERR(state
->vref_reg
);
129 ret
= regulator_enable(state
->vref_reg
);
133 ret
= iio_device_register(indio_dev
);
135 goto disable_regulator
;
140 regulator_disable(state
->vref_reg
);
145 static int max11100_remove(struct spi_device
*spi
)
147 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
148 struct max11100_state
*state
= iio_priv(indio_dev
);
150 iio_device_unregister(indio_dev
);
151 regulator_disable(state
->vref_reg
);
156 static const struct of_device_id max11100_ids
[] = {
157 {.compatible
= "maxim,max11100"},
160 MODULE_DEVICE_TABLE(of
, max11100_ids
);
162 static struct spi_driver max11100_driver
= {
165 .of_match_table
= max11100_ids
,
167 .probe
= max11100_probe
,
168 .remove
= max11100_remove
,
171 module_spi_driver(max11100_driver
);
173 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
174 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
175 MODULE_LICENSE("GPL v2");