2 * ad2s1200.c simple support for the ADI Resolver to Digital Converters:
5 * Copyright (c) 2018-2018 David Veenstra <davidjulianveenstra@gmail.com>
6 * Copyright (c) 2010-2010 Analog Devices Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/bitops.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/gpio.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/spi/spi.h>
21 #include <linux/sysfs.h>
22 #include <linux/types.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
27 #define DRV_NAME "ad2s1200"
29 /* input clock on serial interface */
30 #define AD2S1200_HZ 8192000
31 /* clock period in nano second */
32 #define AD2S1200_TSCLK (1000000000 / AD2S1200_HZ)
35 * struct ad2s1200_state - driver instance specific data.
36 * @lock: protects both the GPIO pins and the rx buffer.
38 * @sample: GPIO pin SAMPLE.
39 * @rdvel: GPIO pin RDVEL.
40 * @rx: buffer for spi transfers.
42 struct ad2s1200_state
{
44 struct spi_device
*sdev
;
45 struct gpio_desc
*sample
;
46 struct gpio_desc
*rdvel
;
47 __be16 rx ____cacheline_aligned
;
50 static int ad2s1200_read_raw(struct iio_dev
*indio_dev
,
51 struct iio_chan_spec
const *chan
,
56 struct ad2s1200_state
*st
= iio_priv(indio_dev
);
60 case IIO_CHAN_INFO_SCALE
:
63 /* 2 * Pi / (2^12 - 1) ~= 0.001534355 */
66 return IIO_VAL_INT_PLUS_NANO
;
68 /* 2 * Pi ~= 6.283185 */
71 return IIO_VAL_INT_PLUS_MICRO
;
76 case IIO_CHAN_INFO_RAW
:
77 mutex_lock(&st
->lock
);
78 gpiod_set_value(st
->sample
, 0);
80 /* delay (6 * AD2S1200_TSCLK + 20) nano seconds */
82 gpiod_set_value(st
->sample
, 1);
83 gpiod_set_value(st
->rdvel
, !!(chan
->type
== IIO_ANGL
));
85 ret
= spi_read(st
->sdev
, &st
->rx
, 2);
87 mutex_unlock(&st
->lock
);
93 *val
= be16_to_cpup(&st
->rx
) >> 4;
96 *val
= sign_extend32(be16_to_cpup(&st
->rx
) >> 4, 11);
99 mutex_unlock(&st
->lock
);
103 /* delay (2 * AD2S1200_TSCLK + 20) ns for sample pulse */
105 mutex_unlock(&st
->lock
);
115 static const struct iio_chan_spec ad2s1200_channels
[] = {
120 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
121 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
123 .type
= IIO_ANGL_VEL
,
126 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
127 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
131 static const struct iio_info ad2s1200_info
= {
132 .read_raw
= ad2s1200_read_raw
,
135 static int ad2s1200_probe(struct spi_device
*spi
)
137 struct ad2s1200_state
*st
;
138 struct iio_dev
*indio_dev
;
141 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
145 spi_set_drvdata(spi
, indio_dev
);
146 st
= iio_priv(indio_dev
);
147 mutex_init(&st
->lock
);
150 st
->sample
= devm_gpiod_get(&spi
->dev
, "adi,sample", GPIOD_OUT_LOW
);
151 if (IS_ERR(st
->sample
)) {
152 dev_err(&spi
->dev
, "Failed to claim SAMPLE gpio: err=%ld\n",
153 PTR_ERR(st
->sample
));
154 return PTR_ERR(st
->sample
);
157 st
->rdvel
= devm_gpiod_get(&spi
->dev
, "adi,rdvel", GPIOD_OUT_LOW
);
158 if (IS_ERR(st
->rdvel
)) {
159 dev_err(&spi
->dev
, "Failed to claim RDVEL gpio: err=%ld\n",
161 return PTR_ERR(st
->rdvel
);
164 indio_dev
->dev
.parent
= &spi
->dev
;
165 indio_dev
->info
= &ad2s1200_info
;
166 indio_dev
->modes
= INDIO_DIRECT_MODE
;
167 indio_dev
->channels
= ad2s1200_channels
;
168 indio_dev
->num_channels
= ARRAY_SIZE(ad2s1200_channels
);
169 indio_dev
->name
= spi_get_device_id(spi
)->name
;
171 spi
->max_speed_hz
= AD2S1200_HZ
;
172 spi
->mode
= SPI_MODE_3
;
173 ret
= spi_setup(spi
);
176 dev_err(&spi
->dev
, "spi_setup failed!\n");
180 return devm_iio_device_register(&spi
->dev
, indio_dev
);
183 static const struct of_device_id ad2s1200_of_match
[] = {
184 { .compatible
= "adi,ad2s1200", },
185 { .compatible
= "adi,ad2s1205", },
188 MODULE_DEVICE_TABLE(of
, ad2s1200_of_match
);
190 static const struct spi_device_id ad2s1200_id
[] = {
195 MODULE_DEVICE_TABLE(spi
, ad2s1200_id
);
197 static struct spi_driver ad2s1200_driver
= {
200 .of_match_table
= of_match_ptr(ad2s1200_of_match
),
202 .probe
= ad2s1200_probe
,
203 .id_table
= ad2s1200_id
,
205 module_spi_driver(ad2s1200_driver
);
207 MODULE_AUTHOR("David Veenstra <davidjulianveenstra@gmail.com>");
208 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
209 MODULE_DESCRIPTION("Analog Devices AD2S1200/1205 Resolver to Digital SPI driver");
210 MODULE_LICENSE("GPL v2");