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