treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157
[sfrench/cifs-2.6.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 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->dev.parent = &spi->dev;
119         indio_dev->dev.of_node = spi->dev.of_node;
120         indio_dev->name = "max11100";
121         indio_dev->info = &max11100_info;
122         indio_dev->modes = INDIO_DIRECT_MODE;
123         indio_dev->channels = max11100_channels;
124         indio_dev->num_channels = ARRAY_SIZE(max11100_channels);
125
126         state->vref_reg = devm_regulator_get(&spi->dev, "vref");
127         if (IS_ERR(state->vref_reg))
128                 return PTR_ERR(state->vref_reg);
129
130         ret = regulator_enable(state->vref_reg);
131         if (ret)
132                 return ret;
133
134         ret = iio_device_register(indio_dev);
135         if (ret)
136                 goto disable_regulator;
137
138         return 0;
139
140 disable_regulator:
141         regulator_disable(state->vref_reg);
142
143         return ret;
144 }
145
146 static int max11100_remove(struct spi_device *spi)
147 {
148         struct iio_dev *indio_dev = spi_get_drvdata(spi);
149         struct max11100_state *state = iio_priv(indio_dev);
150
151         iio_device_unregister(indio_dev);
152         regulator_disable(state->vref_reg);
153
154         return 0;
155 }
156
157 static const struct of_device_id max11100_ids[] = {
158         {.compatible = "maxim,max11100"},
159         { },
160 };
161 MODULE_DEVICE_TABLE(of, max11100_ids);
162
163 static struct spi_driver max11100_driver = {
164         .driver = {
165                 .name   = "max11100",
166                 .of_match_table = of_match_ptr(max11100_ids),
167         },
168         .probe          = max11100_probe,
169         .remove         = max11100_remove,
170 };
171
172 module_spi_driver(max11100_driver);
173
174 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
175 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
176 MODULE_LICENSE("GPL v2");