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