1 // SPDX-License-Identifier: GPL-2.0-only
3 * ad2s1200.c simple support for the ADI Resolver to Digital Converters:
7 * Copyright (c) 2010-2010 Analog Devices Inc.
10 #include <linux/bitops.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/mutex.h>
17 #include <linux/spi/spi.h>
18 #include <linux/sysfs.h>
19 #include <linux/types.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
24 #define DRV_NAME "ad2s1200"
26 /* input clock on serial interface */
27 #define AD2S1200_HZ 8192000
28 /* clock period in nano second */
29 #define AD2S1200_TSCLK (1000000000 / AD2S1200_HZ)
32 * struct ad2s1200_state - driver instance specific data.
33 * @lock: protects both the GPIO pins and the rx buffer.
35 * @sample: GPIO pin SAMPLE.
36 * @rdvel: GPIO pin RDVEL.
37 * @rx: buffer for spi transfers.
39 struct ad2s1200_state {
41 struct spi_device *sdev;
42 struct gpio_desc *sample;
43 struct gpio_desc *rdvel;
44 __be16 rx __aligned(IIO_DMA_MINALIGN);
47 static int ad2s1200_read_raw(struct iio_dev *indio_dev,
48 struct iio_chan_spec const *chan,
53 struct ad2s1200_state *st = iio_priv(indio_dev);
57 case IIO_CHAN_INFO_SCALE:
60 /* 2 * Pi / (2^12 - 1) ~= 0.001534355 */
63 return IIO_VAL_INT_PLUS_NANO;
65 /* 2 * Pi ~= 6.283185 */
68 return IIO_VAL_INT_PLUS_MICRO;
73 case IIO_CHAN_INFO_RAW:
74 mutex_lock(&st->lock);
75 gpiod_set_value(st->sample, 0);
77 /* delay (6 * AD2S1200_TSCLK + 20) nano seconds */
79 gpiod_set_value(st->sample, 1);
80 gpiod_set_value(st->rdvel, !!(chan->type == IIO_ANGL));
82 ret = spi_read(st->sdev, &st->rx, 2);
84 mutex_unlock(&st->lock);
90 *val = be16_to_cpup(&st->rx) >> 4;
93 *val = sign_extend32(be16_to_cpup(&st->rx) >> 4, 11);
96 mutex_unlock(&st->lock);
100 /* delay (2 * AD2S1200_TSCLK + 20) ns for sample pulse */
102 mutex_unlock(&st->lock);
112 static const struct iio_chan_spec ad2s1200_channels[] = {
117 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
118 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
120 .type = IIO_ANGL_VEL,
123 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
124 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
128 static const struct iio_info ad2s1200_info = {
129 .read_raw = ad2s1200_read_raw,
132 static int ad2s1200_probe(struct spi_device *spi)
134 struct ad2s1200_state *st;
135 struct iio_dev *indio_dev;
138 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
142 spi_set_drvdata(spi, indio_dev);
143 st = iio_priv(indio_dev);
144 mutex_init(&st->lock);
147 st->sample = devm_gpiod_get(&spi->dev, "adi,sample", GPIOD_OUT_LOW);
148 if (IS_ERR(st->sample)) {
149 dev_err(&spi->dev, "Failed to claim SAMPLE gpio: err=%ld\n",
150 PTR_ERR(st->sample));
151 return PTR_ERR(st->sample);
154 st->rdvel = devm_gpiod_get(&spi->dev, "adi,rdvel", GPIOD_OUT_LOW);
155 if (IS_ERR(st->rdvel)) {
156 dev_err(&spi->dev, "Failed to claim RDVEL gpio: err=%ld\n",
158 return PTR_ERR(st->rdvel);
161 indio_dev->info = &ad2s1200_info;
162 indio_dev->modes = INDIO_DIRECT_MODE;
163 indio_dev->channels = ad2s1200_channels;
164 indio_dev->num_channels = ARRAY_SIZE(ad2s1200_channels);
165 indio_dev->name = spi_get_device_id(spi)->name;
167 spi->max_speed_hz = AD2S1200_HZ;
168 spi->mode = SPI_MODE_3;
169 ret = spi_setup(spi);
172 dev_err(&spi->dev, "spi_setup failed!\n");
176 return devm_iio_device_register(&spi->dev, indio_dev);
179 static const struct of_device_id ad2s1200_of_match[] = {
180 { .compatible = "adi,ad2s1200", },
181 { .compatible = "adi,ad2s1205", },
184 MODULE_DEVICE_TABLE(of, ad2s1200_of_match);
186 static const struct spi_device_id ad2s1200_id[] = {
191 MODULE_DEVICE_TABLE(spi, ad2s1200_id);
193 static struct spi_driver ad2s1200_driver = {
196 .of_match_table = ad2s1200_of_match,
198 .probe = ad2s1200_probe,
199 .id_table = ad2s1200_id,
201 module_spi_driver(ad2s1200_driver);
205 MODULE_DESCRIPTION("Analog Devices AD2S1200/1205 Resolver to Digital SPI driver");
206 MODULE_LICENSE("GPL v2");