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