iio: Remove superfluous of_node assignments
[platform/kernel/linux-rpi.git] / drivers / iio / adc / max11100.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * iio/adc/max11100.c
4  * Maxim max11100 ADC Driver with IIO interface
5  *
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/module.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/spi/spi.h>
14
15 #include <linux/iio/iio.h>
16 #include <linux/iio/driver.h>
17
18 /*
19  * LSB is the ADC single digital step
20  * 1 LSB = (vref_mv / 2 ^ 16)
21  *
22  * LSB is used to calculate analog voltage value
23  * from the number of ADC steps count
24  *
25  * Ain = (count * LSB)
26  */
27 #define MAX11100_LSB_DIV                (1 << 16)
28
29 struct max11100_state {
30         struct regulator *vref_reg;
31         struct spi_device *spi;
32
33         /*
34          * DMA (thus cache coherency maintenance) requires the
35          * transfer buffers to live in their own cache lines.
36          */
37         u8 buffer[3] ____cacheline_aligned;
38 };
39
40 static const struct iio_chan_spec max11100_channels[] = {
41         { /* [0] */
42                 .type = IIO_VOLTAGE,
43                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
44                                       BIT(IIO_CHAN_INFO_SCALE),
45         },
46 };
47
48 static int max11100_read_single(struct iio_dev *indio_dev, int *val)
49 {
50         int ret;
51         struct max11100_state *state = iio_priv(indio_dev);
52
53         ret = spi_read(state->spi, state->buffer, sizeof(state->buffer));
54         if (ret) {
55                 dev_err(&indio_dev->dev, "SPI transfer failed\n");
56                 return ret;
57         }
58
59         /* the first 8 bits sent out from ADC must be 0s */
60         if (state->buffer[0]) {
61                 dev_err(&indio_dev->dev, "Invalid value: buffer[0] != 0\n");
62                 return -EINVAL;
63         }
64
65         *val = (state->buffer[1] << 8) | state->buffer[2];
66
67         return 0;
68 }
69
70 static int max11100_read_raw(struct iio_dev *indio_dev,
71                              struct iio_chan_spec const *chan,
72                              int *val, int *val2, long info)
73 {
74         int ret, vref_uv;
75         struct max11100_state *state = iio_priv(indio_dev);
76
77         switch (info) {
78         case IIO_CHAN_INFO_RAW:
79                 ret = max11100_read_single(indio_dev, val);
80                 if (ret)
81                         return ret;
82
83                 return IIO_VAL_INT;
84
85         case IIO_CHAN_INFO_SCALE:
86                 vref_uv = regulator_get_voltage(state->vref_reg);
87                 if (vref_uv < 0)
88                         /* dummy regulator "get_voltage" returns -EINVAL */
89                         return -EINVAL;
90
91                 *val =  vref_uv / 1000;
92                 *val2 = MAX11100_LSB_DIV;
93                 return IIO_VAL_FRACTIONAL;
94         }
95
96         return -EINVAL;
97 }
98
99 static const struct iio_info max11100_info = {
100         .read_raw = max11100_read_raw,
101 };
102
103 static int max11100_probe(struct spi_device *spi)
104 {
105         int ret;
106         struct iio_dev *indio_dev;
107         struct max11100_state *state;
108
109         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
110         if (!indio_dev)
111                 return -ENOMEM;
112
113         spi_set_drvdata(spi, indio_dev);
114
115         state = iio_priv(indio_dev);
116         state->spi = spi;
117
118         indio_dev->name = "max11100";
119         indio_dev->info = &max11100_info;
120         indio_dev->modes = INDIO_DIRECT_MODE;
121         indio_dev->channels = max11100_channels;
122         indio_dev->num_channels = ARRAY_SIZE(max11100_channels);
123
124         state->vref_reg = devm_regulator_get(&spi->dev, "vref");
125         if (IS_ERR(state->vref_reg))
126                 return PTR_ERR(state->vref_reg);
127
128         ret = regulator_enable(state->vref_reg);
129         if (ret)
130                 return ret;
131
132         ret = iio_device_register(indio_dev);
133         if (ret)
134                 goto disable_regulator;
135
136         return 0;
137
138 disable_regulator:
139         regulator_disable(state->vref_reg);
140
141         return ret;
142 }
143
144 static int max11100_remove(struct spi_device *spi)
145 {
146         struct iio_dev *indio_dev = spi_get_drvdata(spi);
147         struct max11100_state *state = iio_priv(indio_dev);
148
149         iio_device_unregister(indio_dev);
150         regulator_disable(state->vref_reg);
151
152         return 0;
153 }
154
155 static const struct of_device_id max11100_ids[] = {
156         {.compatible = "maxim,max11100"},
157         { },
158 };
159 MODULE_DEVICE_TABLE(of, max11100_ids);
160
161 static struct spi_driver max11100_driver = {
162         .driver = {
163                 .name   = "max11100",
164                 .of_match_table = of_match_ptr(max11100_ids),
165         },
166         .probe          = max11100_probe,
167         .remove         = max11100_remove,
168 };
169
170 module_spi_driver(max11100_driver);
171
172 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
173 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
174 MODULE_LICENSE("GPL v2");