Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 2 | /* |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 3 | * ad2s1200.c simple support for the ADI Resolver to Digital Converters: |
| 4 | * AD2S1200/1205 |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 5 | * |
David Veenstra | 9c3e81ab | 2018-05-18 20:23:25 +0200 | [diff] [blame] | 6 | * Copyright (c) 2018-2018 David Veenstra <davidjulianveenstra@gmail.com> |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 7 | * Copyright (c) 2010-2010 Analog Devices Inc. |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 8 | */ |
David Veenstra | d99066e | 2018-04-20 21:29:52 +0200 | [diff] [blame] | 9 | |
David Veenstra | 972c0c3 | 2018-04-20 21:28:52 +0200 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 11 | #include <linux/delay.h> |
David Veenstra | 972c0c3 | 2018-04-20 21:28:52 +0200 | [diff] [blame] | 12 | #include <linux/device.h> |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 13 | #include <linux/gpio.h> |
David Veenstra | 5af93e6 | 2018-05-18 20:21:56 +0200 | [diff] [blame] | 14 | #include <linux/gpio/consumer.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
David Veenstra | 972c0c3 | 2018-04-20 21:28:52 +0200 | [diff] [blame] | 16 | #include <linux/mutex.h> |
| 17 | #include <linux/spi/spi.h> |
| 18 | #include <linux/sysfs.h> |
| 19 | #include <linux/types.h> |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 20 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 21 | #include <linux/iio/iio.h> |
| 22 | #include <linux/iio/sysfs.h> |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 23 | |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 24 | #define DRV_NAME "ad2s1200" |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 25 | |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 26 | /* input clock on serial interface */ |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 27 | #define AD2S1200_HZ 8192000 |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 28 | /* clock period in nano second */ |
Eva Rachel Retuya | cdd6dee | 2016-02-18 18:59:34 +0800 | [diff] [blame] | 29 | #define AD2S1200_TSCLK (1000000000 / AD2S1200_HZ) |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 30 | |
David Veenstra | bc683b4 | 2018-05-18 20:21:34 +0200 | [diff] [blame] | 31 | /** |
| 32 | * struct ad2s1200_state - driver instance specific data. |
| 33 | * @lock: protects both the GPIO pins and the rx buffer. |
| 34 | * @sdev: spi device. |
| 35 | * @sample: GPIO pin SAMPLE. |
| 36 | * @rdvel: GPIO pin RDVEL. |
| 37 | * @rx: buffer for spi transfers. |
| 38 | */ |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 39 | struct ad2s1200_state { |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 40 | struct mutex lock; |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 41 | struct spi_device *sdev; |
David Veenstra | 5af93e6 | 2018-05-18 20:21:56 +0200 | [diff] [blame] | 42 | struct gpio_desc *sample; |
| 43 | struct gpio_desc *rdvel; |
David Veenstra | 0bd3d33 | 2018-04-23 00:03:03 +0200 | [diff] [blame] | 44 | __be16 rx ____cacheline_aligned; |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 45 | }; |
| 46 | |
Jonathan Cameron | 9c5ed82 | 2011-10-05 15:28:01 +0100 | [diff] [blame] | 47 | static int ad2s1200_read_raw(struct iio_dev *indio_dev, |
Eva Rachel Retuya | ae6394a | 2016-02-18 18:59:33 +0800 | [diff] [blame] | 48 | struct iio_chan_spec const *chan, |
| 49 | int *val, |
| 50 | int *val2, |
| 51 | long m) |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 52 | { |
David Veenstra | 0598225 | 2018-04-20 21:29:08 +0200 | [diff] [blame] | 53 | struct ad2s1200_state *st = iio_priv(indio_dev); |
David Veenstra | c1b6a7d | 2018-05-18 20:21:23 +0200 | [diff] [blame] | 54 | int ret; |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 55 | |
David Veenstra | d3e6ed8 | 2018-05-18 20:23:01 +0200 | [diff] [blame] | 56 | switch (m) { |
| 57 | case IIO_CHAN_INFO_SCALE: |
| 58 | switch (chan->type) { |
David Veenstra | f9db737 | 2018-05-18 20:23:14 +0200 | [diff] [blame] | 59 | case IIO_ANGL: |
| 60 | /* 2 * Pi / (2^12 - 1) ~= 0.001534355 */ |
| 61 | *val = 0; |
| 62 | *val2 = 1534355; |
| 63 | return IIO_VAL_INT_PLUS_NANO; |
David Veenstra | d3e6ed8 | 2018-05-18 20:23:01 +0200 | [diff] [blame] | 64 | case IIO_ANGL_VEL: |
| 65 | /* 2 * Pi ~= 6.283185 */ |
| 66 | *val = 6; |
| 67 | *val2 = 283185; |
| 68 | return IIO_VAL_INT_PLUS_MICRO; |
| 69 | default: |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | break; |
| 73 | case IIO_CHAN_INFO_RAW: |
| 74 | mutex_lock(&st->lock); |
| 75 | gpiod_set_value(st->sample, 0); |
David Veenstra | d99066e | 2018-04-20 21:29:52 +0200 | [diff] [blame] | 76 | |
David Veenstra | d3e6ed8 | 2018-05-18 20:23:01 +0200 | [diff] [blame] | 77 | /* delay (6 * AD2S1200_TSCLK + 20) nano seconds */ |
| 78 | udelay(1); |
| 79 | gpiod_set_value(st->sample, 1); |
| 80 | gpiod_set_value(st->rdvel, !!(chan->type == IIO_ANGL)); |
David Veenstra | d99066e | 2018-04-20 21:29:52 +0200 | [diff] [blame] | 81 | |
David Veenstra | d3e6ed8 | 2018-05-18 20:23:01 +0200 | [diff] [blame] | 82 | ret = spi_read(st->sdev, &st->rx, 2); |
| 83 | if (ret < 0) { |
| 84 | mutex_unlock(&st->lock); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | switch (chan->type) { |
| 89 | case IIO_ANGL: |
| 90 | *val = be16_to_cpup(&st->rx) >> 4; |
| 91 | break; |
| 92 | case IIO_ANGL_VEL: |
| 93 | *val = sign_extend32(be16_to_cpup(&st->rx) >> 4, 11); |
| 94 | break; |
| 95 | default: |
| 96 | mutex_unlock(&st->lock); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | |
| 100 | /* delay (2 * AD2S1200_TSCLK + 20) ns for sample pulse */ |
| 101 | udelay(1); |
Jonathan Cameron | 9c5ed82 | 2011-10-05 15:28:01 +0100 | [diff] [blame] | 102 | mutex_unlock(&st->lock); |
Jonathan Cameron | 9c5ed82 | 2011-10-05 15:28:01 +0100 | [diff] [blame] | 103 | |
David Veenstra | d3e6ed8 | 2018-05-18 20:23:01 +0200 | [diff] [blame] | 104 | return IIO_VAL_INT; |
Jonathan Cameron | 9c5ed82 | 2011-10-05 15:28:01 +0100 | [diff] [blame] | 105 | default: |
David Veenstra | d3e6ed8 | 2018-05-18 20:23:01 +0200 | [diff] [blame] | 106 | break; |
Jonathan Cameron | 9c5ed82 | 2011-10-05 15:28:01 +0100 | [diff] [blame] | 107 | } |
David Veenstra | d99066e | 2018-04-20 21:29:52 +0200 | [diff] [blame] | 108 | |
David Veenstra | d3e6ed8 | 2018-05-18 20:23:01 +0200 | [diff] [blame] | 109 | return -EINVAL; |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Jonathan Cameron | 9c5ed82 | 2011-10-05 15:28:01 +0100 | [diff] [blame] | 112 | static const struct iio_chan_spec ad2s1200_channels[] = { |
| 113 | { |
| 114 | .type = IIO_ANGL, |
| 115 | .indexed = 1, |
| 116 | .channel = 0, |
Jonathan Cameron | 910b51f | 2013-02-27 19:41:20 +0000 | [diff] [blame] | 117 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
David Veenstra | f9db737 | 2018-05-18 20:23:14 +0200 | [diff] [blame] | 118 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
Jonathan Cameron | 9c5ed82 | 2011-10-05 15:28:01 +0100 | [diff] [blame] | 119 | }, { |
| 120 | .type = IIO_ANGL_VEL, |
| 121 | .indexed = 1, |
| 122 | .channel = 0, |
Jonathan Cameron | 910b51f | 2013-02-27 19:41:20 +0000 | [diff] [blame] | 123 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
David Veenstra | d3e6ed8 | 2018-05-18 20:23:01 +0200 | [diff] [blame] | 124 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
Jonathan Cameron | 9c5ed82 | 2011-10-05 15:28:01 +0100 | [diff] [blame] | 125 | } |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 126 | }; |
| 127 | |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 128 | static const struct iio_info ad2s1200_info = { |
simran singhal | 67cba02 | 2017-03-11 19:56:43 +0530 | [diff] [blame] | 129 | .read_raw = ad2s1200_read_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 130 | }; |
| 131 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 132 | static int ad2s1200_probe(struct spi_device *spi) |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 133 | { |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 134 | struct ad2s1200_state *st; |
Jonathan Cameron | 8f2bd83 | 2011-06-27 13:07:52 +0100 | [diff] [blame] | 135 | struct iio_dev *indio_dev; |
David Veenstra | 1946980 | 2018-05-18 20:22:32 +0200 | [diff] [blame] | 136 | int ret; |
David Veenstra | d99066e | 2018-04-20 21:29:52 +0200 | [diff] [blame] | 137 | |
Sachin Kamat | 8d1b0b4 | 2013-09-11 10:55:00 +0100 | [diff] [blame] | 138 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); |
| 139 | if (!indio_dev) |
| 140 | return -ENOMEM; |
David Veenstra | d99066e | 2018-04-20 21:29:52 +0200 | [diff] [blame] | 141 | |
Jonathan Cameron | 8f2bd83 | 2011-06-27 13:07:52 +0100 | [diff] [blame] | 142 | spi_set_drvdata(spi, indio_dev); |
| 143 | st = iio_priv(indio_dev); |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 144 | mutex_init(&st->lock); |
| 145 | st->sdev = spi; |
David Veenstra | 1946980 | 2018-05-18 20:22:32 +0200 | [diff] [blame] | 146 | |
| 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); |
| 152 | } |
| 153 | |
| 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", |
| 157 | PTR_ERR(st->rdvel)); |
| 158 | return PTR_ERR(st->rdvel); |
| 159 | } |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 160 | |
Jonathan Cameron | 8f2bd83 | 2011-06-27 13:07:52 +0100 | [diff] [blame] | 161 | indio_dev->dev.parent = &spi->dev; |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 162 | indio_dev->info = &ad2s1200_info; |
Jonathan Cameron | 8f2bd83 | 2011-06-27 13:07:52 +0100 | [diff] [blame] | 163 | indio_dev->modes = INDIO_DIRECT_MODE; |
Jonathan Cameron | 9c5ed82 | 2011-10-05 15:28:01 +0100 | [diff] [blame] | 164 | indio_dev->channels = ad2s1200_channels; |
| 165 | indio_dev->num_channels = ARRAY_SIZE(ad2s1200_channels); |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 166 | indio_dev->name = spi_get_device_id(spi)->name; |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 167 | |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 168 | spi->max_speed_hz = AD2S1200_HZ; |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 169 | spi->mode = SPI_MODE_3; |
David Veenstra | 94cdefa | 2018-05-18 20:21:44 +0200 | [diff] [blame] | 170 | ret = spi_setup(spi); |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 171 | |
David Veenstra | 94cdefa | 2018-05-18 20:21:44 +0200 | [diff] [blame] | 172 | if (ret < 0) { |
| 173 | dev_err(&spi->dev, "spi_setup failed!\n"); |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | return devm_iio_device_register(&spi->dev, indio_dev); |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 178 | } |
| 179 | |
David Veenstra | 2363c01 | 2018-05-18 20:22:41 +0200 | [diff] [blame] | 180 | static const struct of_device_id ad2s1200_of_match[] = { |
| 181 | { .compatible = "adi,ad2s1200", }, |
| 182 | { .compatible = "adi,ad2s1205", }, |
| 183 | { } |
| 184 | }; |
| 185 | MODULE_DEVICE_TABLE(of, ad2s1200_of_match); |
| 186 | |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 187 | static const struct spi_device_id ad2s1200_id[] = { |
| 188 | { "ad2s1200" }, |
| 189 | { "ad2s1205" }, |
| 190 | {} |
| 191 | }; |
Lars-Peter Clausen | 55e4390 | 2011-11-16 08:53:31 +0100 | [diff] [blame] | 192 | MODULE_DEVICE_TABLE(spi, ad2s1200_id); |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 193 | |
| 194 | static struct spi_driver ad2s1200_driver = { |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 195 | .driver = { |
| 196 | .name = DRV_NAME, |
David Veenstra | 2363c01 | 2018-05-18 20:22:41 +0200 | [diff] [blame] | 197 | .of_match_table = of_match_ptr(ad2s1200_of_match), |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 198 | }, |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 199 | .probe = ad2s1200_probe, |
Jonathan Cameron | 10e4a52 | 2011-10-05 15:28:02 +0100 | [diff] [blame] | 200 | .id_table = ad2s1200_id, |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 201 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 202 | module_spi_driver(ad2s1200_driver); |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 203 | |
David Veenstra | 9c3e81ab | 2018-05-18 20:23:25 +0200 | [diff] [blame] | 204 | MODULE_AUTHOR("David Veenstra <davidjulianveenstra@gmail.com>"); |
Graf Yang | ffd7a62 | 2010-10-27 21:44:20 -0400 | [diff] [blame] | 205 | MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>"); |
| 206 | MODULE_DESCRIPTION("Analog Devices AD2S1200/1205 Resolver to Digital SPI driver"); |
| 207 | MODULE_LICENSE("GPL v2"); |