Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / iio / adc / max11100.c
blob6cf21758ca66cc13865b34995d7821c3fe2c99a5
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * iio/adc/max11100.c
4 * Maxim max11100 ADC Driver with IIO interface
6 * Copyright (C) 2016-17 Renesas Electronics Corporation
7 * Copyright (C) 2016-17 Jacopo Mondi
8 */
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
26 * Ain = (count * LSB)
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[] = {
42 { /* [0] */
43 .type = IIO_VOLTAGE,
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)
51 int ret;
52 struct max11100_state *state = iio_priv(indio_dev);
54 ret = spi_read(state->spi, state->buffer, sizeof(state->buffer));
55 if (ret) {
56 dev_err(&indio_dev->dev, "SPI transfer failed\n");
57 return ret;
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");
63 return -EINVAL;
66 *val = (state->buffer[1] << 8) | state->buffer[2];
68 return 0;
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)
75 int ret, vref_uv;
76 struct max11100_state *state = iio_priv(indio_dev);
78 switch (info) {
79 case IIO_CHAN_INFO_RAW:
80 ret = max11100_read_single(indio_dev, val);
81 if (ret)
82 return ret;
84 return IIO_VAL_INT;
86 case IIO_CHAN_INFO_SCALE:
87 vref_uv = regulator_get_voltage(state->vref_reg);
88 if (vref_uv < 0)
89 /* dummy regulator "get_voltage" returns -EINVAL */
90 return -EINVAL;
92 *val = vref_uv / 1000;
93 *val2 = MAX11100_LSB_DIV;
94 return IIO_VAL_FRACTIONAL;
97 return -EINVAL;
100 static const struct iio_info max11100_info = {
101 .read_raw = max11100_read_raw,
104 static int max11100_probe(struct spi_device *spi)
106 int ret;
107 struct iio_dev *indio_dev;
108 struct max11100_state *state;
110 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
111 if (!indio_dev)
112 return -ENOMEM;
114 spi_set_drvdata(spi, indio_dev);
116 state = iio_priv(indio_dev);
117 state->spi = spi;
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);
130 if (ret)
131 return ret;
133 ret = iio_device_register(indio_dev);
134 if (ret)
135 goto disable_regulator;
137 return 0;
139 disable_regulator:
140 regulator_disable(state->vref_reg);
142 return ret;
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);
153 return 0;
156 static const struct of_device_id max11100_ids[] = {
157 {.compatible = "maxim,max11100"},
158 { },
160 MODULE_DEVICE_TABLE(of, max11100_ids);
162 static struct spi_driver max11100_driver = {
163 .driver = {
164 .name = "max11100",
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");