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>
15 #include <linux/unaligned.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/driver.h>
21 * LSB is the ADC single digital step
22 * 1 LSB = (vref_mv / 2 ^ 16)
24 * LSB is used to calculate analog voltage value
25 * from the number of ADC steps count
29 #define MAX11100_LSB_DIV (1 << 16)
31 struct max11100_state
{
32 struct regulator
*vref_reg
;
33 struct spi_device
*spi
;
36 * DMA (thus cache coherency maintenance) may require the
37 * transfer buffers to live in their own cache lines.
39 u8 buffer
[3] __aligned(IIO_DMA_MINALIGN
);
42 static const struct iio_chan_spec max11100_channels
[] = {
45 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
46 BIT(IIO_CHAN_INFO_SCALE
),
50 static int max11100_read_single(struct iio_dev
*indio_dev
, int *val
)
53 struct max11100_state
*state
= iio_priv(indio_dev
);
55 ret
= spi_read(state
->spi
, state
->buffer
, sizeof(state
->buffer
));
57 dev_err(&indio_dev
->dev
, "SPI transfer failed\n");
61 /* the first 8 bits sent out from ADC must be 0s */
62 if (state
->buffer
[0]) {
63 dev_err(&indio_dev
->dev
, "Invalid value: buffer[0] != 0\n");
67 *val
= get_unaligned_be16(&state
->buffer
[1]);
72 static int max11100_read_raw(struct iio_dev
*indio_dev
,
73 struct iio_chan_spec
const *chan
,
74 int *val
, int *val2
, long info
)
77 struct max11100_state
*state
= iio_priv(indio_dev
);
80 case IIO_CHAN_INFO_RAW
:
81 ret
= max11100_read_single(indio_dev
, val
);
87 case IIO_CHAN_INFO_SCALE
:
88 vref_uv
= regulator_get_voltage(state
->vref_reg
);
90 /* dummy regulator "get_voltage" returns -EINVAL */
93 *val
= vref_uv
/ 1000;
94 *val2
= MAX11100_LSB_DIV
;
95 return IIO_VAL_FRACTIONAL
;
101 static const struct iio_info max11100_info
= {
102 .read_raw
= max11100_read_raw
,
105 static void max11100_regulator_disable(void *reg
)
107 regulator_disable(reg
);
110 static int max11100_probe(struct spi_device
*spi
)
113 struct iio_dev
*indio_dev
;
114 struct max11100_state
*state
;
116 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*state
));
120 state
= iio_priv(indio_dev
);
123 indio_dev
->name
= "max11100";
124 indio_dev
->info
= &max11100_info
;
125 indio_dev
->modes
= INDIO_DIRECT_MODE
;
126 indio_dev
->channels
= max11100_channels
;
127 indio_dev
->num_channels
= ARRAY_SIZE(max11100_channels
);
129 state
->vref_reg
= devm_regulator_get(&spi
->dev
, "vref");
130 if (IS_ERR(state
->vref_reg
))
131 return PTR_ERR(state
->vref_reg
);
133 ret
= regulator_enable(state
->vref_reg
);
137 ret
= devm_add_action_or_reset(&spi
->dev
, max11100_regulator_disable
,
142 return devm_iio_device_register(&spi
->dev
, indio_dev
);
145 static const struct of_device_id max11100_ids
[] = {
146 { .compatible
= "maxim,max11100" },
149 MODULE_DEVICE_TABLE(of
, max11100_ids
);
151 static struct spi_driver max11100_driver
= {
154 .of_match_table
= max11100_ids
,
156 .probe
= max11100_probe
,
159 module_spi_driver(max11100_driver
);
161 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
162 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
163 MODULE_LICENSE("GPL v2");