1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ad28d315SDavid Veenstra /*
3ad28d315SDavid Veenstra * ad2s1200.c simple support for the ADI Resolver to Digital Converters:
4ad28d315SDavid Veenstra * AD2S1200/1205
5ad28d315SDavid Veenstra *
6ad28d315SDavid Veenstra * Copyright (c) 2018-2018 David Veenstra <davidjulianveenstra@gmail.com>
7ad28d315SDavid Veenstra * Copyright (c) 2010-2010 Analog Devices Inc.
8ad28d315SDavid Veenstra */
9ad28d315SDavid Veenstra
10ad28d315SDavid Veenstra #include <linux/bitops.h>
11ad28d315SDavid Veenstra #include <linux/delay.h>
12ad28d315SDavid Veenstra #include <linux/device.h>
13ad28d315SDavid Veenstra #include <linux/gpio/consumer.h>
14ad28d315SDavid Veenstra #include <linux/module.h>
15184ac728SJonathan Cameron #include <linux/mod_devicetable.h>
16ad28d315SDavid Veenstra #include <linux/mutex.h>
17ad28d315SDavid Veenstra #include <linux/spi/spi.h>
18ad28d315SDavid Veenstra #include <linux/sysfs.h>
19ad28d315SDavid Veenstra #include <linux/types.h>
20ad28d315SDavid Veenstra
21ad28d315SDavid Veenstra #include <linux/iio/iio.h>
22ad28d315SDavid Veenstra #include <linux/iio/sysfs.h>
23ad28d315SDavid Veenstra
24ad28d315SDavid Veenstra #define DRV_NAME "ad2s1200"
25ad28d315SDavid Veenstra
26ad28d315SDavid Veenstra /* input clock on serial interface */
27ad28d315SDavid Veenstra #define AD2S1200_HZ 8192000
28ad28d315SDavid Veenstra /* clock period in nano second */
29ad28d315SDavid Veenstra #define AD2S1200_TSCLK (1000000000 / AD2S1200_HZ)
30ad28d315SDavid Veenstra
31ad28d315SDavid Veenstra /**
32ad28d315SDavid Veenstra * struct ad2s1200_state - driver instance specific data.
33ad28d315SDavid Veenstra * @lock: protects both the GPIO pins and the rx buffer.
34ad28d315SDavid Veenstra * @sdev: spi device.
35ad28d315SDavid Veenstra * @sample: GPIO pin SAMPLE.
36ad28d315SDavid Veenstra * @rdvel: GPIO pin RDVEL.
37ad28d315SDavid Veenstra * @rx: buffer for spi transfers.
38ad28d315SDavid Veenstra */
39ad28d315SDavid Veenstra struct ad2s1200_state {
40ad28d315SDavid Veenstra struct mutex lock;
41ad28d315SDavid Veenstra struct spi_device *sdev;
42ad28d315SDavid Veenstra struct gpio_desc *sample;
43ad28d315SDavid Veenstra struct gpio_desc *rdvel;
44*37882314SJonathan Cameron __be16 rx __aligned(IIO_DMA_MINALIGN);
45ad28d315SDavid Veenstra };
46ad28d315SDavid Veenstra
ad2s1200_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)47ad28d315SDavid Veenstra static int ad2s1200_read_raw(struct iio_dev *indio_dev,
48ad28d315SDavid Veenstra struct iio_chan_spec const *chan,
49ad28d315SDavid Veenstra int *val,
50ad28d315SDavid Veenstra int *val2,
51ad28d315SDavid Veenstra long m)
52ad28d315SDavid Veenstra {
53ad28d315SDavid Veenstra struct ad2s1200_state *st = iio_priv(indio_dev);
54ad28d315SDavid Veenstra int ret;
55ad28d315SDavid Veenstra
56ad28d315SDavid Veenstra switch (m) {
57ad28d315SDavid Veenstra case IIO_CHAN_INFO_SCALE:
58ad28d315SDavid Veenstra switch (chan->type) {
59ad28d315SDavid Veenstra case IIO_ANGL:
60ad28d315SDavid Veenstra /* 2 * Pi / (2^12 - 1) ~= 0.001534355 */
61ad28d315SDavid Veenstra *val = 0;
62ad28d315SDavid Veenstra *val2 = 1534355;
63ad28d315SDavid Veenstra return IIO_VAL_INT_PLUS_NANO;
64ad28d315SDavid Veenstra case IIO_ANGL_VEL:
65ad28d315SDavid Veenstra /* 2 * Pi ~= 6.283185 */
66ad28d315SDavid Veenstra *val = 6;
67ad28d315SDavid Veenstra *val2 = 283185;
68ad28d315SDavid Veenstra return IIO_VAL_INT_PLUS_MICRO;
69ad28d315SDavid Veenstra default:
70ad28d315SDavid Veenstra return -EINVAL;
71ad28d315SDavid Veenstra }
72ad28d315SDavid Veenstra break;
73ad28d315SDavid Veenstra case IIO_CHAN_INFO_RAW:
74ad28d315SDavid Veenstra mutex_lock(&st->lock);
75ad28d315SDavid Veenstra gpiod_set_value(st->sample, 0);
76ad28d315SDavid Veenstra
77ad28d315SDavid Veenstra /* delay (6 * AD2S1200_TSCLK + 20) nano seconds */
78ad28d315SDavid Veenstra udelay(1);
79ad28d315SDavid Veenstra gpiod_set_value(st->sample, 1);
80ad28d315SDavid Veenstra gpiod_set_value(st->rdvel, !!(chan->type == IIO_ANGL));
81ad28d315SDavid Veenstra
82ad28d315SDavid Veenstra ret = spi_read(st->sdev, &st->rx, 2);
83ad28d315SDavid Veenstra if (ret < 0) {
84ad28d315SDavid Veenstra mutex_unlock(&st->lock);
85ad28d315SDavid Veenstra return ret;
86ad28d315SDavid Veenstra }
87ad28d315SDavid Veenstra
88ad28d315SDavid Veenstra switch (chan->type) {
89ad28d315SDavid Veenstra case IIO_ANGL:
90ad28d315SDavid Veenstra *val = be16_to_cpup(&st->rx) >> 4;
91ad28d315SDavid Veenstra break;
92ad28d315SDavid Veenstra case IIO_ANGL_VEL:
93ad28d315SDavid Veenstra *val = sign_extend32(be16_to_cpup(&st->rx) >> 4, 11);
94ad28d315SDavid Veenstra break;
95ad28d315SDavid Veenstra default:
96ad28d315SDavid Veenstra mutex_unlock(&st->lock);
97ad28d315SDavid Veenstra return -EINVAL;
98ad28d315SDavid Veenstra }
99ad28d315SDavid Veenstra
100ad28d315SDavid Veenstra /* delay (2 * AD2S1200_TSCLK + 20) ns for sample pulse */
101ad28d315SDavid Veenstra udelay(1);
102ad28d315SDavid Veenstra mutex_unlock(&st->lock);
103ad28d315SDavid Veenstra
104ad28d315SDavid Veenstra return IIO_VAL_INT;
105ad28d315SDavid Veenstra default:
106ad28d315SDavid Veenstra break;
107ad28d315SDavid Veenstra }
108ad28d315SDavid Veenstra
109ad28d315SDavid Veenstra return -EINVAL;
110ad28d315SDavid Veenstra }
111ad28d315SDavid Veenstra
112ad28d315SDavid Veenstra static const struct iio_chan_spec ad2s1200_channels[] = {
113ad28d315SDavid Veenstra {
114ad28d315SDavid Veenstra .type = IIO_ANGL,
115ad28d315SDavid Veenstra .indexed = 1,
116ad28d315SDavid Veenstra .channel = 0,
117ad28d315SDavid Veenstra .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
118ad28d315SDavid Veenstra .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
119ad28d315SDavid Veenstra }, {
120ad28d315SDavid Veenstra .type = IIO_ANGL_VEL,
121ad28d315SDavid Veenstra .indexed = 1,
122ad28d315SDavid Veenstra .channel = 0,
123ad28d315SDavid Veenstra .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
124ad28d315SDavid Veenstra .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
125ad28d315SDavid Veenstra }
126ad28d315SDavid Veenstra };
127ad28d315SDavid Veenstra
128ad28d315SDavid Veenstra static const struct iio_info ad2s1200_info = {
129ad28d315SDavid Veenstra .read_raw = ad2s1200_read_raw,
130ad28d315SDavid Veenstra };
131ad28d315SDavid Veenstra
ad2s1200_probe(struct spi_device * spi)132ad28d315SDavid Veenstra static int ad2s1200_probe(struct spi_device *spi)
133ad28d315SDavid Veenstra {
134ad28d315SDavid Veenstra struct ad2s1200_state *st;
135ad28d315SDavid Veenstra struct iio_dev *indio_dev;
136ad28d315SDavid Veenstra int ret;
137ad28d315SDavid Veenstra
138ad28d315SDavid Veenstra indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
139ad28d315SDavid Veenstra if (!indio_dev)
140ad28d315SDavid Veenstra return -ENOMEM;
141ad28d315SDavid Veenstra
142ad28d315SDavid Veenstra spi_set_drvdata(spi, indio_dev);
143ad28d315SDavid Veenstra st = iio_priv(indio_dev);
144ad28d315SDavid Veenstra mutex_init(&st->lock);
145ad28d315SDavid Veenstra st->sdev = spi;
146ad28d315SDavid Veenstra
147ad28d315SDavid Veenstra st->sample = devm_gpiod_get(&spi->dev, "adi,sample", GPIOD_OUT_LOW);
148ad28d315SDavid Veenstra if (IS_ERR(st->sample)) {
149ad28d315SDavid Veenstra dev_err(&spi->dev, "Failed to claim SAMPLE gpio: err=%ld\n",
150ad28d315SDavid Veenstra PTR_ERR(st->sample));
151ad28d315SDavid Veenstra return PTR_ERR(st->sample);
152ad28d315SDavid Veenstra }
153ad28d315SDavid Veenstra
154ad28d315SDavid Veenstra st->rdvel = devm_gpiod_get(&spi->dev, "adi,rdvel", GPIOD_OUT_LOW);
155ad28d315SDavid Veenstra if (IS_ERR(st->rdvel)) {
156ad28d315SDavid Veenstra dev_err(&spi->dev, "Failed to claim RDVEL gpio: err=%ld\n",
157ad28d315SDavid Veenstra PTR_ERR(st->rdvel));
158ad28d315SDavid Veenstra return PTR_ERR(st->rdvel);
159ad28d315SDavid Veenstra }
160ad28d315SDavid Veenstra
161ad28d315SDavid Veenstra indio_dev->info = &ad2s1200_info;
162ad28d315SDavid Veenstra indio_dev->modes = INDIO_DIRECT_MODE;
163ad28d315SDavid Veenstra indio_dev->channels = ad2s1200_channels;
164ad28d315SDavid Veenstra indio_dev->num_channels = ARRAY_SIZE(ad2s1200_channels);
165ad28d315SDavid Veenstra indio_dev->name = spi_get_device_id(spi)->name;
166ad28d315SDavid Veenstra
167ad28d315SDavid Veenstra spi->max_speed_hz = AD2S1200_HZ;
168ad28d315SDavid Veenstra spi->mode = SPI_MODE_3;
169ad28d315SDavid Veenstra ret = spi_setup(spi);
170ad28d315SDavid Veenstra
171ad28d315SDavid Veenstra if (ret < 0) {
172ad28d315SDavid Veenstra dev_err(&spi->dev, "spi_setup failed!\n");
173ad28d315SDavid Veenstra return ret;
174ad28d315SDavid Veenstra }
175ad28d315SDavid Veenstra
176ad28d315SDavid Veenstra return devm_iio_device_register(&spi->dev, indio_dev);
177ad28d315SDavid Veenstra }
178ad28d315SDavid Veenstra
179ad28d315SDavid Veenstra static const struct of_device_id ad2s1200_of_match[] = {
180ad28d315SDavid Veenstra { .compatible = "adi,ad2s1200", },
181ad28d315SDavid Veenstra { .compatible = "adi,ad2s1205", },
182ad28d315SDavid Veenstra { }
183ad28d315SDavid Veenstra };
184ad28d315SDavid Veenstra MODULE_DEVICE_TABLE(of, ad2s1200_of_match);
185ad28d315SDavid Veenstra
186ad28d315SDavid Veenstra static const struct spi_device_id ad2s1200_id[] = {
187ad28d315SDavid Veenstra { "ad2s1200" },
188ad28d315SDavid Veenstra { "ad2s1205" },
189ad28d315SDavid Veenstra {}
190ad28d315SDavid Veenstra };
191ad28d315SDavid Veenstra MODULE_DEVICE_TABLE(spi, ad2s1200_id);
192ad28d315SDavid Veenstra
193ad28d315SDavid Veenstra static struct spi_driver ad2s1200_driver = {
194ad28d315SDavid Veenstra .driver = {
195ad28d315SDavid Veenstra .name = DRV_NAME,
196184ac728SJonathan Cameron .of_match_table = ad2s1200_of_match,
197ad28d315SDavid Veenstra },
198ad28d315SDavid Veenstra .probe = ad2s1200_probe,
199ad28d315SDavid Veenstra .id_table = ad2s1200_id,
200ad28d315SDavid Veenstra };
201ad28d315SDavid Veenstra module_spi_driver(ad2s1200_driver);
202ad28d315SDavid Veenstra
203ad28d315SDavid Veenstra MODULE_AUTHOR("David Veenstra <davidjulianveenstra@gmail.com>");
204ad28d315SDavid Veenstra MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
205ad28d315SDavid Veenstra MODULE_DESCRIPTION("Analog Devices AD2S1200/1205 Resolver to Digital SPI driver");
206ad28d315SDavid Veenstra MODULE_LICENSE("GPL v2");
207