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