hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/iio/adc/ad7476.c
....@@ -12,9 +12,11 @@
1212 #include <linux/sysfs.h>
1313 #include <linux/spi/spi.h>
1414 #include <linux/regulator/consumer.h>
15
+#include <linux/gpio/consumer.h>
1516 #include <linux/err.h>
1617 #include <linux/module.h>
1718 #include <linux/bitops.h>
19
+#include <linux/delay.h>
1820
1921 #include <linux/iio/iio.h>
2022 #include <linux/iio/sysfs.h>
....@@ -27,6 +29,8 @@
2729 struct ad7476_chip_info {
2830 unsigned int int_vref_uv;
2931 struct iio_chan_spec channel[2];
32
+ /* channels used when convst gpio is defined */
33
+ struct iio_chan_spec convst_channel[2];
3034 void (*reset)(struct ad7476_state *);
3135 };
3236
....@@ -34,6 +38,7 @@
3438 struct spi_device *spi;
3539 const struct ad7476_chip_info *chip_info;
3640 struct regulator *reg;
41
+ struct gpio_desc *convst_gpio;
3742 struct spi_transfer xfer;
3843 struct spi_message msg;
3944 /*
....@@ -59,7 +64,21 @@
5964 ID_ADC081S,
6065 ID_ADC101S,
6166 ID_ADC121S,
67
+ ID_ADS7866,
68
+ ID_ADS7867,
69
+ ID_ADS7868,
6270 };
71
+
72
+static void ad7091_convst(struct ad7476_state *st)
73
+{
74
+ if (!st->convst_gpio)
75
+ return;
76
+
77
+ gpiod_set_value(st->convst_gpio, 0);
78
+ udelay(1); /* CONVST pulse width: 10 ns min */
79
+ gpiod_set_value(st->convst_gpio, 1);
80
+ udelay(1); /* Conversion time: 650 ns max */
81
+}
6382
6483 static irqreturn_t ad7476_trigger_handler(int irq, void *p)
6584 {
....@@ -67,6 +86,8 @@
6786 struct iio_dev *indio_dev = pf->indio_dev;
6887 struct ad7476_state *st = iio_priv(indio_dev);
6988 int b_sent;
89
+
90
+ ad7091_convst(st);
7091
7192 b_sent = spi_sync(st->spi, &st->msg);
7293 if (b_sent < 0)
....@@ -89,6 +110,8 @@
89110 static int ad7476_scan_direct(struct ad7476_state *st)
90111 {
91112 int ret;
113
+
114
+ ad7091_convst(st);
92115
93116 ret = spi_sync(st->spi, &st->msg);
94117 if (ret)
....@@ -157,11 +180,17 @@
157180 #define AD7940_CHAN(bits) _AD7476_CHAN((bits), 15 - (bits), \
158181 BIT(IIO_CHAN_INFO_RAW))
159182 #define AD7091R_CHAN(bits) _AD7476_CHAN((bits), 16 - (bits), 0)
183
+#define AD7091R_CONVST_CHAN(bits) _AD7476_CHAN((bits), 16 - (bits), \
184
+ BIT(IIO_CHAN_INFO_RAW))
185
+#define ADS786X_CHAN(bits) _AD7476_CHAN((bits), 12 - (bits), \
186
+ BIT(IIO_CHAN_INFO_RAW))
160187
161188 static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
162189 [ID_AD7091R] = {
163190 .channel[0] = AD7091R_CHAN(12),
164191 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
192
+ .convst_channel[0] = AD7091R_CONVST_CHAN(12),
193
+ .convst_channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
165194 .reset = ad7091_reset,
166195 },
167196 [ID_AD7276] = {
....@@ -209,11 +238,30 @@
209238 .channel[0] = ADC081S_CHAN(12),
210239 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
211240 },
241
+ [ID_ADS7866] = {
242
+ .channel[0] = ADS786X_CHAN(12),
243
+ .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
244
+ },
245
+ [ID_ADS7867] = {
246
+ .channel[0] = ADS786X_CHAN(10),
247
+ .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
248
+ },
249
+ [ID_ADS7868] = {
250
+ .channel[0] = ADS786X_CHAN(8),
251
+ .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
252
+ },
212253 };
213254
214255 static const struct iio_info ad7476_info = {
215256 .read_raw = &ad7476_read_raw,
216257 };
258
+
259
+static void ad7476_reg_disable(void *data)
260
+{
261
+ struct ad7476_state *st = data;
262
+
263
+ regulator_disable(st->reg);
264
+}
217265
218266 static int ad7476_probe(struct spi_device *spi)
219267 {
....@@ -237,18 +285,29 @@
237285 if (ret)
238286 return ret;
239287
288
+ ret = devm_add_action_or_reset(&spi->dev, ad7476_reg_disable,
289
+ st);
290
+ if (ret)
291
+ return ret;
292
+
293
+ st->convst_gpio = devm_gpiod_get_optional(&spi->dev,
294
+ "adi,conversion-start",
295
+ GPIOD_OUT_LOW);
296
+ if (IS_ERR(st->convst_gpio))
297
+ return PTR_ERR(st->convst_gpio);
298
+
240299 spi_set_drvdata(spi, indio_dev);
241300
242301 st->spi = spi;
243302
244
- /* Establish that the iio_dev is a child of the spi device */
245
- indio_dev->dev.parent = &spi->dev;
246
- indio_dev->dev.of_node = spi->dev.of_node;
247303 indio_dev->name = spi_get_device_id(spi)->name;
248304 indio_dev->modes = INDIO_DIRECT_MODE;
249305 indio_dev->channels = st->chip_info->channel;
250306 indio_dev->num_channels = 2;
251307 indio_dev->info = &ad7476_info;
308
+
309
+ if (st->convst_gpio)
310
+ indio_dev->channels = st->chip_info->convst_channel;
252311 /* Setup default message */
253312
254313 st->xfer.rx_buf = &st->data;
....@@ -257,40 +316,19 @@
257316 spi_message_init(&st->msg);
258317 spi_message_add_tail(&st->xfer, &st->msg);
259318
260
- ret = iio_triggered_buffer_setup(indio_dev, NULL,
261
- &ad7476_trigger_handler, NULL);
319
+ ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
320
+ &ad7476_trigger_handler, NULL);
262321 if (ret)
263
- goto error_disable_reg;
322
+ return ret;
264323
265324 if (st->chip_info->reset)
266325 st->chip_info->reset(st);
267326
268
- ret = iio_device_register(indio_dev);
269
- if (ret)
270
- goto error_ring_unregister;
271
- return 0;
272
-
273
-error_ring_unregister:
274
- iio_triggered_buffer_cleanup(indio_dev);
275
-error_disable_reg:
276
- regulator_disable(st->reg);
277
-
278
- return ret;
279
-}
280
-
281
-static int ad7476_remove(struct spi_device *spi)
282
-{
283
- struct iio_dev *indio_dev = spi_get_drvdata(spi);
284
- struct ad7476_state *st = iio_priv(indio_dev);
285
-
286
- iio_device_unregister(indio_dev);
287
- iio_triggered_buffer_cleanup(indio_dev);
288
- regulator_disable(st->reg);
289
-
290
- return 0;
327
+ return devm_iio_device_register(&spi->dev, indio_dev);
291328 }
292329
293330 static const struct spi_device_id ad7476_id[] = {
331
+ {"ad7091", ID_AD7091R},
294332 {"ad7091r", ID_AD7091R},
295333 {"ad7273", ID_AD7277},
296334 {"ad7274", ID_AD7276},
....@@ -314,6 +352,9 @@
314352 {"adc081s", ID_ADC081S},
315353 {"adc101s", ID_ADC101S},
316354 {"adc121s", ID_ADC121S},
355
+ {"ads7866", ID_ADS7866},
356
+ {"ads7867", ID_ADS7867},
357
+ {"ads7868", ID_ADS7868},
317358 {}
318359 };
319360 MODULE_DEVICE_TABLE(spi, ad7476_id);
....@@ -323,11 +364,10 @@
323364 .name = "ad7476",
324365 },
325366 .probe = ad7476_probe,
326
- .remove = ad7476_remove,
327367 .id_table = ad7476_id,
328368 };
329369 module_spi_driver(ad7476_driver);
330370
331
-MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
371
+MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
332372 MODULE_DESCRIPTION("Analog Devices AD7476 and similar 1-channel ADCs");
333373 MODULE_LICENSE("GPL v2");