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