2 * ad2s90.c simple support for the ADI Resolver to Digital Converters: AD2S90
4 * Copyright (c) 2010-2010 Analog Devices Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/types.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/module.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
24 struct spi_device *sdev;
25 u8 rx[2] ____cacheline_aligned;
28 static int ad2s90_read_raw(struct iio_dev *indio_dev,
29 struct iio_chan_spec const *chan,
35 struct ad2s90_state *st = iio_priv(indio_dev);
37 mutex_lock(&st->lock);
38 ret = spi_read(st->sdev, st->rx, 2);
41 *val = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
44 mutex_unlock(&st->lock);
49 static const struct iio_info ad2s90_info = {
50 .read_raw = &ad2s90_read_raw,
51 .driver_module = THIS_MODULE,
54 static const struct iio_chan_spec ad2s90_chan = {
58 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
61 static int ad2s90_probe(struct spi_device *spi)
63 struct iio_dev *indio_dev;
64 struct ad2s90_state *st;
67 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
70 st = iio_priv(indio_dev);
71 spi_set_drvdata(spi, indio_dev);
73 mutex_init(&st->lock);
75 indio_dev->dev.parent = &spi->dev;
76 indio_dev->info = &ad2s90_info;
77 indio_dev->modes = INDIO_DIRECT_MODE;
78 indio_dev->channels = &ad2s90_chan;
79 indio_dev->num_channels = 1;
80 indio_dev->name = spi_get_device_id(spi)->name;
82 ret = iio_device_register(indio_dev);
86 /* need 600ns between CS and the first falling edge of SCLK */
87 spi->max_speed_hz = 830000;
88 spi->mode = SPI_MODE_3;
94 static int ad2s90_remove(struct spi_device *spi)
96 iio_device_unregister(spi_get_drvdata(spi));
101 static const struct spi_device_id ad2s90_id[] = {
105 MODULE_DEVICE_TABLE(spi, ad2s90_id);
107 static struct spi_driver ad2s90_driver = {
110 .owner = THIS_MODULE,
112 .probe = ad2s90_probe,
113 .remove = ad2s90_remove,
114 .id_table = ad2s90_id,
116 module_spi_driver(ad2s90_driver);
119 MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
120 MODULE_LICENSE("GPL v2");