1
2
3
4
5
6
7
8
9
10
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
18#include "../iio.h"
19#include "../sysfs.h"
20
21#define DRV_NAME "ad2s90"
22
23struct ad2s90_state {
24 struct mutex lock;
25 struct iio_dev *idev;
26 struct spi_device *sdev;
27 u8 rx[2];
28 u8 tx[2];
29};
30
31static ssize_t ad2s90_show_angular(struct device *dev,
32 struct device_attribute *attr, char *buf)
33{
34 struct spi_message msg;
35 struct spi_transfer xfer;
36 int ret;
37 ssize_t len = 0;
38 u16 val;
39 struct iio_dev *idev = dev_get_drvdata(dev);
40 struct ad2s90_state *st = idev->dev_data;
41
42 xfer.len = 1;
43 xfer.tx_buf = st->tx;
44 xfer.rx_buf = st->rx;
45 mutex_lock(&st->lock);
46
47 spi_message_init(&msg);
48 spi_message_add_tail(&xfer, &msg);
49 ret = spi_sync(st->sdev, &msg);
50 if (ret)
51 goto error_ret;
52 val = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
53 len = sprintf(buf, "%d\n", val);
54error_ret:
55 mutex_unlock(&st->lock);
56
57 return ret ? ret : len;
58}
59
60#define IIO_DEV_ATTR_SIMPLE_RESOLVER(_show) \
61 IIO_DEVICE_ATTR(angular, S_IRUGO, _show, NULL, 0)
62
63static IIO_CONST_ATTR(description,
64 "Low Cost, Complete 12-Bit Resolver-to-Digital Converter");
65static IIO_DEV_ATTR_SIMPLE_RESOLVER(ad2s90_show_angular);
66
67static struct attribute *ad2s90_attributes[] = {
68 &iio_const_attr_description.dev_attr.attr,
69 &iio_dev_attr_angular.dev_attr.attr,
70 NULL,
71};
72
73static const struct attribute_group ad2s90_attribute_group = {
74 .name = DRV_NAME,
75 .attrs = ad2s90_attributes,
76};
77
78static int __devinit ad2s90_probe(struct spi_device *spi)
79{
80 struct ad2s90_state *st;
81 int ret = 0;
82
83 st = kzalloc(sizeof(*st), GFP_KERNEL);
84 if (st == NULL) {
85 ret = -ENOMEM;
86 goto error_ret;
87 }
88 spi_set_drvdata(spi, st);
89
90 mutex_init(&st->lock);
91 st->sdev = spi;
92
93 st->idev = iio_allocate_device();
94 if (st->idev == NULL) {
95 ret = -ENOMEM;
96 goto error_free_st;
97 }
98 st->idev->dev.parent = &spi->dev;
99 st->idev->num_interrupt_lines = 0;
100 st->idev->event_attrs = NULL;
101
102 st->idev->attrs = &ad2s90_attribute_group;
103 st->idev->dev_data = (void *)(st);
104 st->idev->driver_module = THIS_MODULE;
105 st->idev->modes = INDIO_DIRECT_MODE;
106
107 ret = iio_device_register(st->idev);
108 if (ret)
109 goto error_free_dev;
110
111
112 spi->max_speed_hz = 830000;
113 spi->mode = SPI_MODE_3;
114 spi_setup(spi);
115
116 return 0;
117
118error_free_dev:
119 iio_free_device(st->idev);
120error_free_st:
121 kfree(st);
122error_ret:
123 return ret;
124}
125
126static int __devexit ad2s90_remove(struct spi_device *spi)
127{
128 struct ad2s90_state *st = spi_get_drvdata(spi);
129
130 iio_device_unregister(st->idev);
131 kfree(st);
132
133 return 0;
134}
135
136static struct spi_driver ad2s90_driver = {
137 .driver = {
138 .name = DRV_NAME,
139 .owner = THIS_MODULE,
140 },
141 .probe = ad2s90_probe,
142 .remove = __devexit_p(ad2s90_remove),
143};
144
145static __init int ad2s90_spi_init(void)
146{
147 return spi_register_driver(&ad2s90_driver);
148}
149module_init(ad2s90_spi_init);
150
151static __exit void ad2s90_spi_exit(void)
152{
153 spi_unregister_driver(&ad2s90_driver);
154}
155module_exit(ad2s90_spi_exit);
156
157MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
158MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
159MODULE_LICENSE("GPL v2");
160