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