Merge branch 'core-objtool-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / drivers / iio / adc / max1118.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * MAX1117/MAX1118/MAX1119 8-bit, dual-channel ADCs driver
4  *
5  * Copyright (c) 2017 Akinobu Mita <akinobu.mita@gmail.com>
6  *
7  * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1117-MAX1119.pdf
8  *
9  * SPI interface connections
10  *
11  * SPI                MAXIM
12  * Master  Direction  MAX1117/8/9
13  * ------  ---------  -----------
14  * nCS        -->     CNVST
15  * SCK        -->     SCLK
16  * MISO       <--     DOUT
17  * ------  ---------  -----------
18  */
19
20 #include <linux/module.h>
21 #include <linux/spi/spi.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/regulator/consumer.h>
27
28 enum max1118_id {
29         max1117,
30         max1118,
31         max1119,
32 };
33
34 struct max1118 {
35         struct spi_device *spi;
36         struct mutex lock;
37         struct regulator *reg;
38
39         u8 data ____cacheline_aligned;
40 };
41
42 #define MAX1118_CHANNEL(ch)                                             \
43         {                                                               \
44                 .type = IIO_VOLTAGE,                                    \
45                 .indexed = 1,                                           \
46                 .channel = (ch),                                        \
47                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
48                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
49                 .scan_index = ch,                                       \
50                 .scan_type = {                                          \
51                         .sign = 'u',                                    \
52                         .realbits = 8,                                  \
53                         .storagebits = 8,                               \
54                 },                                                      \
55         }
56
57 static const struct iio_chan_spec max1118_channels[] = {
58         MAX1118_CHANNEL(0),
59         MAX1118_CHANNEL(1),
60         IIO_CHAN_SOFT_TIMESTAMP(2),
61 };
62
63 static int max1118_read(struct spi_device *spi, int channel)
64 {
65         struct iio_dev *indio_dev = spi_get_drvdata(spi);
66         struct max1118 *adc = iio_priv(indio_dev);
67         struct spi_transfer xfers[] = {
68                 /*
69                  * To select CH1 for conversion, CNVST pin must be brought high
70                  * and low for a second time.
71                  */
72                 {
73                         .len = 0,
74                         .delay = {      /* > CNVST Low Time 100 ns */
75                                 .value = 1,
76                                 .unit = SPI_DELAY_UNIT_USECS
77                         },
78                         .cs_change = 1,
79                 },
80                 /*
81                  * The acquisition interval begins with the falling edge of
82                  * CNVST.  The total acquisition and conversion process takes
83                  * <7.5us.
84                  */
85                 {
86                         .len = 0,
87                         .delay = {
88                                 .value = 8,
89                                 .unit = SPI_DELAY_UNIT_USECS
90                         },
91                 },
92                 {
93                         .rx_buf = &adc->data,
94                         .len = 1,
95                 },
96         };
97         int ret;
98
99         if (channel == 0)
100                 ret = spi_sync_transfer(spi, xfers + 1, 2);
101         else
102                 ret = spi_sync_transfer(spi, xfers, 3);
103
104         if (ret)
105                 return ret;
106
107         return adc->data;
108 }
109
110 static int max1118_get_vref_mV(struct spi_device *spi)
111 {
112         struct iio_dev *indio_dev = spi_get_drvdata(spi);
113         struct max1118 *adc = iio_priv(indio_dev);
114         const struct spi_device_id *id = spi_get_device_id(spi);
115         int vref_uV;
116
117         switch (id->driver_data) {
118         case max1117:
119                 return 2048;
120         case max1119:
121                 return 4096;
122         case max1118:
123                 vref_uV = regulator_get_voltage(adc->reg);
124                 if (vref_uV < 0)
125                         return vref_uV;
126                 return vref_uV / 1000;
127         }
128
129         return -ENODEV;
130 }
131
132 static int max1118_read_raw(struct iio_dev *indio_dev,
133                         struct iio_chan_spec const *chan,
134                         int *val, int *val2, long mask)
135 {
136         struct max1118 *adc = iio_priv(indio_dev);
137
138         switch (mask) {
139         case IIO_CHAN_INFO_RAW:
140                 mutex_lock(&adc->lock);
141                 *val = max1118_read(adc->spi, chan->channel);
142                 mutex_unlock(&adc->lock);
143                 if (*val < 0)
144                         return *val;
145
146                 return IIO_VAL_INT;
147         case IIO_CHAN_INFO_SCALE:
148                 *val = max1118_get_vref_mV(adc->spi);
149                 if (*val < 0)
150                         return *val;
151                 *val2 = 8;
152
153                 return IIO_VAL_FRACTIONAL_LOG2;
154         }
155
156         return -EINVAL;
157 }
158
159 static const struct iio_info max1118_info = {
160         .read_raw = max1118_read_raw,
161 };
162
163 static irqreturn_t max1118_trigger_handler(int irq, void *p)
164 {
165         struct iio_poll_func *pf = p;
166         struct iio_dev *indio_dev = pf->indio_dev;
167         struct max1118 *adc = iio_priv(indio_dev);
168         u8 data[16] = { }; /* 2x 8-bit ADC data + padding + 8 bytes timestamp */
169         int scan_index;
170         int i = 0;
171
172         mutex_lock(&adc->lock);
173
174         for_each_set_bit(scan_index, indio_dev->active_scan_mask,
175                         indio_dev->masklength) {
176                 const struct iio_chan_spec *scan_chan =
177                                 &indio_dev->channels[scan_index];
178                 int ret = max1118_read(adc->spi, scan_chan->channel);
179
180                 if (ret < 0) {
181                         dev_warn(&adc->spi->dev,
182                                 "failed to get conversion data\n");
183                         goto out;
184                 }
185
186                 data[i] = ret;
187                 i++;
188         }
189         iio_push_to_buffers_with_timestamp(indio_dev, data,
190                                            iio_get_time_ns(indio_dev));
191 out:
192         mutex_unlock(&adc->lock);
193
194         iio_trigger_notify_done(indio_dev->trig);
195
196         return IRQ_HANDLED;
197 }
198
199 static int max1118_probe(struct spi_device *spi)
200 {
201         struct iio_dev *indio_dev;
202         struct max1118 *adc;
203         const struct spi_device_id *id = spi_get_device_id(spi);
204         int ret;
205
206         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
207         if (!indio_dev)
208                 return -ENOMEM;
209
210         adc = iio_priv(indio_dev);
211         adc->spi = spi;
212         mutex_init(&adc->lock);
213
214         if (id->driver_data == max1118) {
215                 adc->reg = devm_regulator_get(&spi->dev, "vref");
216                 if (IS_ERR(adc->reg)) {
217                         dev_err(&spi->dev, "failed to get vref regulator\n");
218                         return PTR_ERR(adc->reg);
219                 }
220                 ret = regulator_enable(adc->reg);
221                 if (ret)
222                         return ret;
223         }
224
225         spi_set_drvdata(spi, indio_dev);
226
227         indio_dev->name = spi_get_device_id(spi)->name;
228         indio_dev->dev.parent = &spi->dev;
229         indio_dev->info = &max1118_info;
230         indio_dev->modes = INDIO_DIRECT_MODE;
231         indio_dev->channels = max1118_channels;
232         indio_dev->num_channels = ARRAY_SIZE(max1118_channels);
233
234         /*
235          * To reinitiate a conversion on CH0, it is necessary to allow for a
236          * conversion to be complete and all of the data to be read out.  Once
237          * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
238          * into AutoShutdown mode until the next conversion is initiated.
239          */
240         max1118_read(spi, 0);
241
242         ret = iio_triggered_buffer_setup(indio_dev, NULL,
243                                         max1118_trigger_handler, NULL);
244         if (ret)
245                 goto err_reg_disable;
246
247         ret = iio_device_register(indio_dev);
248         if (ret)
249                 goto err_buffer_cleanup;
250
251         return 0;
252
253 err_buffer_cleanup:
254         iio_triggered_buffer_cleanup(indio_dev);
255 err_reg_disable:
256         if (id->driver_data == max1118)
257                 regulator_disable(adc->reg);
258
259         return ret;
260 }
261
262 static int max1118_remove(struct spi_device *spi)
263 {
264         struct iio_dev *indio_dev = spi_get_drvdata(spi);
265         struct max1118 *adc = iio_priv(indio_dev);
266         const struct spi_device_id *id = spi_get_device_id(spi);
267
268         iio_device_unregister(indio_dev);
269         iio_triggered_buffer_cleanup(indio_dev);
270         if (id->driver_data == max1118)
271                 return regulator_disable(adc->reg);
272
273         return 0;
274 }
275
276 static const struct spi_device_id max1118_id[] = {
277         { "max1117", max1117 },
278         { "max1118", max1118 },
279         { "max1119", max1119 },
280         {}
281 };
282 MODULE_DEVICE_TABLE(spi, max1118_id);
283
284 #ifdef CONFIG_OF
285
286 static const struct of_device_id max1118_dt_ids[] = {
287         { .compatible = "maxim,max1117" },
288         { .compatible = "maxim,max1118" },
289         { .compatible = "maxim,max1119" },
290         {},
291 };
292 MODULE_DEVICE_TABLE(of, max1118_dt_ids);
293
294 #endif
295
296 static struct spi_driver max1118_spi_driver = {
297         .driver = {
298                 .name = "max1118",
299                 .of_match_table = of_match_ptr(max1118_dt_ids),
300         },
301         .probe = max1118_probe,
302         .remove = max1118_remove,
303         .id_table = max1118_id,
304 };
305 module_spi_driver(max1118_spi_driver);
306
307 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
308 MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
309 MODULE_LICENSE("GPL v2");