Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / resolver / ad2s90.c
blob166e2414ac8559390ffa584f98241baa51f680d2
1 /*
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>
18 #include "../iio.h"
19 #include "../sysfs.h"
21 #define DRV_NAME "ad2s90"
23 struct ad2s90_state {
24 struct mutex lock;
25 struct iio_dev *idev;
26 struct spi_device *sdev;
27 u8 rx[2] ____cacheline_aligned;
30 static ssize_t ad2s90_show_angular(struct device *dev,
31 struct device_attribute *attr, char *buf)
33 int ret;
34 ssize_t len = 0;
35 u16 val;
36 struct ad2s90_state *st = iio_priv(dev_get_drvdata(dev));
38 mutex_lock(&st->lock);
39 ret = spi_read(st->sdev, st->rx, 2);
40 if (ret)
41 goto error_ret;
42 val = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
43 len = sprintf(buf, "%d\n", val);
44 error_ret:
45 mutex_unlock(&st->lock);
47 return ret ? ret : len;
50 #define IIO_DEV_ATTR_SIMPLE_RESOLVER(_show) \
51 IIO_DEVICE_ATTR(angular, S_IRUGO, _show, NULL, 0)
53 static IIO_DEV_ATTR_SIMPLE_RESOLVER(ad2s90_show_angular);
55 static struct attribute *ad2s90_attributes[] = {
56 &iio_dev_attr_angular.dev_attr.attr,
57 NULL,
60 static const struct attribute_group ad2s90_attribute_group = {
61 .name = DRV_NAME,
62 .attrs = ad2s90_attributes,
65 static const struct iio_info ad2s90_info = {
66 .attrs = &ad2s90_attribute_group,
67 .driver_module = THIS_MODULE,
70 static int __devinit ad2s90_probe(struct spi_device *spi)
72 struct iio_dev *indio_dev;
73 struct ad2s90_state *st;
74 int ret = 0;
76 indio_dev = iio_allocate_device(sizeof(*st));
77 if (indio_dev == NULL) {
78 ret = -ENOMEM;
79 goto error_ret;
81 st = iio_priv(indio_dev);
82 spi_set_drvdata(spi, indio_dev);
84 mutex_init(&st->lock);
85 st->sdev = spi;
86 indio_dev->dev.parent = &spi->dev;
87 indio_dev->info = &ad2s90_info;
88 indio_dev->modes = INDIO_DIRECT_MODE;
90 ret = iio_device_register(st->idev);
91 if (ret)
92 goto error_free_dev;
94 /* need 600ns between CS and the first falling edge of SCLK */
95 spi->max_speed_hz = 830000;
96 spi->mode = SPI_MODE_3;
97 spi_setup(spi);
99 return 0;
101 error_free_dev:
102 iio_free_device(st->idev);
103 error_ret:
104 return ret;
107 static int __devexit ad2s90_remove(struct spi_device *spi)
109 iio_device_unregister(spi_get_drvdata(spi));
111 return 0;
114 static struct spi_driver ad2s90_driver = {
115 .driver = {
116 .name = DRV_NAME,
117 .owner = THIS_MODULE,
119 .probe = ad2s90_probe,
120 .remove = __devexit_p(ad2s90_remove),
123 static __init int ad2s90_spi_init(void)
125 return spi_register_driver(&ad2s90_driver);
127 module_init(ad2s90_spi_init);
129 static __exit void ad2s90_spi_exit(void)
131 spi_unregister_driver(&ad2s90_driver);
133 module_exit(ad2s90_spi_exit);
135 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
136 MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
137 MODULE_LICENSE("GPL v2");