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