staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / iio / adc / max1118.c
blob49db9e9ae625c0fa3d75c6e14900eacb83cbdb92
1 /*
2 * MAX1117/MAX1118/MAX1119 8-bit, dual-channel ADCs driver
4 * Copyright (c) 2017 Akinobu Mita <akinobu.mita@gmail.com>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1117-MAX1119.pdf
12 * SPI interface connections
14 * SPI MAXIM
15 * Master Direction MAX1117/8/9
16 * ------ --------- -----------
17 * nCS --> CNVST
18 * SCK --> SCLK
19 * MISO <-- DOUT
20 * ------ --------- -----------
23 #include <linux/module.h>
24 #include <linux/spi/spi.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/triggered_buffer.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/regulator/consumer.h>
31 enum max1118_id {
32 max1117,
33 max1118,
34 max1119,
37 struct max1118 {
38 struct spi_device *spi;
39 struct mutex lock;
40 struct regulator *reg;
42 u8 data ____cacheline_aligned;
45 #define MAX1118_CHANNEL(ch) \
46 { \
47 .type = IIO_VOLTAGE, \
48 .indexed = 1, \
49 .channel = (ch), \
50 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
51 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
52 .scan_index = ch, \
53 .scan_type = { \
54 .sign = 'u', \
55 .realbits = 8, \
56 .storagebits = 8, \
57 }, \
60 static const struct iio_chan_spec max1118_channels[] = {
61 MAX1118_CHANNEL(0),
62 MAX1118_CHANNEL(1),
63 IIO_CHAN_SOFT_TIMESTAMP(2),
66 static int max1118_read(struct spi_device *spi, int channel)
68 struct iio_dev *indio_dev = spi_get_drvdata(spi);
69 struct max1118 *adc = iio_priv(indio_dev);
70 struct spi_transfer xfers[] = {
72 * To select CH1 for conversion, CNVST pin must be brought high
73 * and low for a second time.
76 .len = 0,
77 .delay_usecs = 1, /* > CNVST Low Time 100 ns */
78 .cs_change = 1,
81 * The acquisition interval begins with the falling edge of
82 * CNVST. The total acquisition and conversion process takes
83 * <7.5us.
86 .len = 0,
87 .delay_usecs = 8,
90 .rx_buf = &adc->data,
91 .len = 1,
94 int ret;
96 if (channel == 0)
97 ret = spi_sync_transfer(spi, xfers + 1, 2);
98 else
99 ret = spi_sync_transfer(spi, xfers, 3);
101 if (ret)
102 return ret;
104 return adc->data;
107 static int max1118_get_vref_mV(struct spi_device *spi)
109 struct iio_dev *indio_dev = spi_get_drvdata(spi);
110 struct max1118 *adc = iio_priv(indio_dev);
111 const struct spi_device_id *id = spi_get_device_id(spi);
112 int vref_uV;
114 switch (id->driver_data) {
115 case max1117:
116 return 2048;
117 case max1119:
118 return 4096;
119 case max1118:
120 vref_uV = regulator_get_voltage(adc->reg);
121 if (vref_uV < 0)
122 return vref_uV;
123 return vref_uV / 1000;
126 return -ENODEV;
129 static int max1118_read_raw(struct iio_dev *indio_dev,
130 struct iio_chan_spec const *chan,
131 int *val, int *val2, long mask)
133 struct max1118 *adc = iio_priv(indio_dev);
135 switch (mask) {
136 case IIO_CHAN_INFO_RAW:
137 mutex_lock(&adc->lock);
138 *val = max1118_read(adc->spi, chan->channel);
139 mutex_unlock(&adc->lock);
140 if (*val < 0)
141 return *val;
143 return IIO_VAL_INT;
144 case IIO_CHAN_INFO_SCALE:
145 *val = max1118_get_vref_mV(adc->spi);
146 if (*val < 0)
147 return *val;
148 *val2 = 8;
150 return IIO_VAL_FRACTIONAL_LOG2;
153 return -EINVAL;
156 static const struct iio_info max1118_info = {
157 .read_raw = max1118_read_raw,
160 static irqreturn_t max1118_trigger_handler(int irq, void *p)
162 struct iio_poll_func *pf = p;
163 struct iio_dev *indio_dev = pf->indio_dev;
164 struct max1118 *adc = iio_priv(indio_dev);
165 u8 data[16] = { }; /* 2x 8-bit ADC data + padding + 8 bytes timestamp */
166 int scan_index;
167 int i = 0;
169 mutex_lock(&adc->lock);
171 for_each_set_bit(scan_index, indio_dev->active_scan_mask,
172 indio_dev->masklength) {
173 const struct iio_chan_spec *scan_chan =
174 &indio_dev->channels[scan_index];
175 int ret = max1118_read(adc->spi, scan_chan->channel);
177 if (ret < 0) {
178 dev_warn(&adc->spi->dev,
179 "failed to get conversion data\n");
180 goto out;
183 data[i] = ret;
184 i++;
186 iio_push_to_buffers_with_timestamp(indio_dev, data,
187 iio_get_time_ns(indio_dev));
188 out:
189 mutex_unlock(&adc->lock);
191 iio_trigger_notify_done(indio_dev->trig);
193 return IRQ_HANDLED;
196 static int max1118_probe(struct spi_device *spi)
198 struct iio_dev *indio_dev;
199 struct max1118 *adc;
200 const struct spi_device_id *id = spi_get_device_id(spi);
201 int ret;
203 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
204 if (!indio_dev)
205 return -ENOMEM;
207 adc = iio_priv(indio_dev);
208 adc->spi = spi;
209 mutex_init(&adc->lock);
211 if (id->driver_data == max1118) {
212 adc->reg = devm_regulator_get(&spi->dev, "vref");
213 if (IS_ERR(adc->reg)) {
214 dev_err(&spi->dev, "failed to get vref regulator\n");
215 return PTR_ERR(adc->reg);
217 ret = regulator_enable(adc->reg);
218 if (ret)
219 return ret;
222 spi_set_drvdata(spi, indio_dev);
224 indio_dev->name = spi_get_device_id(spi)->name;
225 indio_dev->dev.parent = &spi->dev;
226 indio_dev->info = &max1118_info;
227 indio_dev->modes = INDIO_DIRECT_MODE;
228 indio_dev->channels = max1118_channels;
229 indio_dev->num_channels = ARRAY_SIZE(max1118_channels);
232 * To reinitiate a conversion on CH0, it is necessary to allow for a
233 * conversion to be complete and all of the data to be read out. Once
234 * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
235 * into AutoShutdown mode until the next conversion is initiated.
237 max1118_read(spi, 0);
239 ret = iio_triggered_buffer_setup(indio_dev, NULL,
240 max1118_trigger_handler, NULL);
241 if (ret)
242 goto err_reg_disable;
244 ret = iio_device_register(indio_dev);
245 if (ret)
246 goto err_buffer_cleanup;
248 return 0;
250 err_buffer_cleanup:
251 iio_triggered_buffer_cleanup(indio_dev);
252 err_reg_disable:
253 if (id->driver_data == max1118)
254 regulator_disable(adc->reg);
256 return ret;
259 static int max1118_remove(struct spi_device *spi)
261 struct iio_dev *indio_dev = spi_get_drvdata(spi);
262 struct max1118 *adc = iio_priv(indio_dev);
263 const struct spi_device_id *id = spi_get_device_id(spi);
265 iio_device_unregister(indio_dev);
266 iio_triggered_buffer_cleanup(indio_dev);
267 if (id->driver_data == max1118)
268 return regulator_disable(adc->reg);
270 return 0;
273 static const struct spi_device_id max1118_id[] = {
274 { "max1117", max1117 },
275 { "max1118", max1118 },
276 { "max1119", max1119 },
279 MODULE_DEVICE_TABLE(spi, max1118_id);
281 #ifdef CONFIG_OF
283 static const struct of_device_id max1118_dt_ids[] = {
284 { .compatible = "maxim,max1117" },
285 { .compatible = "maxim,max1118" },
286 { .compatible = "maxim,max1119" },
289 MODULE_DEVICE_TABLE(of, max1118_dt_ids);
291 #endif
293 static struct spi_driver max1118_spi_driver = {
294 .driver = {
295 .name = "max1118",
296 .of_match_table = of_match_ptr(max1118_dt_ids),
298 .probe = max1118_probe,
299 .remove = max1118_remove,
300 .id_table = max1118_id,
302 module_spi_driver(max1118_spi_driver);
304 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
305 MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
306 MODULE_LICENSE("GPL v2");