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/mutex.h>
16 #include <linux/spi/spi.h>
17 #include <linux/sysfs.h>
18 #include <linux/types.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
23 #define DRV_NAME "ad2s1200"
25 /* input clock on serial interface */
26 #define AD2S1200_HZ 8192000
27 /* clock period in nano second */
28 #define AD2S1200_TSCLK (1000000000 / AD2S1200_HZ)
31 * struct ad2s1200_state - driver instance specific data.
32 * @lock: protects both the GPIO pins and the rx buffer.
34 * @sample: GPIO pin SAMPLE.
35 * @rdvel: GPIO pin RDVEL.
36 * @rx: buffer for spi transfers.
38 struct ad2s1200_state {
40 struct spi_device *sdev;
41 struct gpio_desc *sample;
42 struct gpio_desc *rdvel;
43 __be16 rx ____cacheline_aligned;
46 static int ad2s1200_read_raw(struct iio_dev *indio_dev,
47 struct iio_chan_spec const *chan,
52 struct ad2s1200_state *st = iio_priv(indio_dev);
56 case IIO_CHAN_INFO_SCALE:
59 /* 2 * Pi / (2^12 - 1) ~= 0.001534355 */
62 return IIO_VAL_INT_PLUS_NANO;
64 /* 2 * Pi ~= 6.283185 */
67 return IIO_VAL_INT_PLUS_MICRO;
72 case IIO_CHAN_INFO_RAW:
73 mutex_lock(&st->lock);
74 gpiod_set_value(st->sample, 0);
76 /* delay (6 * AD2S1200_TSCLK + 20) nano seconds */
78 gpiod_set_value(st->sample, 1);
79 gpiod_set_value(st->rdvel, !!(chan->type == IIO_ANGL));
81 ret = spi_read(st->sdev, &st->rx, 2);
83 mutex_unlock(&st->lock);
89 *val = be16_to_cpup(&st->rx) >> 4;
92 *val = sign_extend32(be16_to_cpup(&st->rx) >> 4, 11);
95 mutex_unlock(&st->lock);
99 /* delay (2 * AD2S1200_TSCLK + 20) ns for sample pulse */
101 mutex_unlock(&st->lock);
111 static const struct iio_chan_spec ad2s1200_channels[] = {
116 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
117 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
119 .type = IIO_ANGL_VEL,
122 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
123 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
127 static const struct iio_info ad2s1200_info = {
128 .read_raw = ad2s1200_read_raw,
131 static int ad2s1200_probe(struct spi_device *spi)
133 struct ad2s1200_state *st;
134 struct iio_dev *indio_dev;
137 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
141 spi_set_drvdata(spi, indio_dev);
142 st = iio_priv(indio_dev);
143 mutex_init(&st->lock);
146 st->sample = devm_gpiod_get(&spi->dev, "adi,sample", GPIOD_OUT_LOW);
147 if (IS_ERR(st->sample)) {
148 dev_err(&spi->dev, "Failed to claim SAMPLE gpio: err=%ld\n",
149 PTR_ERR(st->sample));
150 return PTR_ERR(st->sample);
153 st->rdvel = devm_gpiod_get(&spi->dev, "adi,rdvel", GPIOD_OUT_LOW);
154 if (IS_ERR(st->rdvel)) {
155 dev_err(&spi->dev, "Failed to claim RDVEL gpio: err=%ld\n",
157 return PTR_ERR(st->rdvel);
160 indio_dev->dev.parent = &spi->dev;
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 = of_match_ptr(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");