Merge branch 'for-paul' of git://gitorious.org/linux-omap-dss2/linux
[deliverable/linux.git] / drivers / staging / iio / resolver / ad2s90.c
1 /*
2 * ad2s90.c simple support for the ADI Resolver to Digital Converters: AD2S90
3 *
4 * Copyright (c) 2010-2010 Analog Devices Inc.
5 *
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.
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
23 struct 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
31 static 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);
54 error_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
63 static IIO_CONST_ATTR(description,
64 "Low Cost, Complete 12-Bit Resolver-to-Digital Converter");
65 static IIO_DEV_ATTR_SIMPLE_RESOLVER(ad2s90_show_angular);
66
67 static struct attribute *ad2s90_attributes[] = {
68 &iio_const_attr_description.dev_attr.attr,
69 &iio_dev_attr_angular.dev_attr.attr,
70 NULL,
71 };
72
73 static const struct attribute_group ad2s90_attribute_group = {
74 .name = DRV_NAME,
75 .attrs = ad2s90_attributes,
76 };
77
78 static const struct iio_info ad2s90_info = {
79 .attrs = &ad2s90_attribute_group,
80 .driver_module = THIS_MODULE,
81 };
82
83 static int __devinit ad2s90_probe(struct spi_device *spi)
84 {
85 struct ad2s90_state *st;
86 int ret = 0;
87
88 st = kzalloc(sizeof(*st), GFP_KERNEL);
89 if (st == NULL) {
90 ret = -ENOMEM;
91 goto error_ret;
92 }
93 spi_set_drvdata(spi, st);
94
95 mutex_init(&st->lock);
96 st->sdev = spi;
97
98 st->idev = iio_allocate_device(0);
99 if (st->idev == NULL) {
100 ret = -ENOMEM;
101 goto error_free_st;
102 }
103 st->idev->dev.parent = &spi->dev;
104
105 st->idev->info = &ad2s90_info;
106 st->idev->dev_data = (void *)(st);
107 st->idev->modes = INDIO_DIRECT_MODE;
108
109 ret = iio_device_register(st->idev);
110 if (ret)
111 goto error_free_dev;
112
113 /* need 600ns between CS and the first falling edge of SCLK */
114 spi->max_speed_hz = 830000;
115 spi->mode = SPI_MODE_3;
116 spi_setup(spi);
117
118 return 0;
119
120 error_free_dev:
121 iio_free_device(st->idev);
122 error_free_st:
123 kfree(st);
124 error_ret:
125 return ret;
126 }
127
128 static int __devexit ad2s90_remove(struct spi_device *spi)
129 {
130 struct ad2s90_state *st = spi_get_drvdata(spi);
131
132 iio_device_unregister(st->idev);
133 kfree(st);
134
135 return 0;
136 }
137
138 static struct spi_driver ad2s90_driver = {
139 .driver = {
140 .name = DRV_NAME,
141 .owner = THIS_MODULE,
142 },
143 .probe = ad2s90_probe,
144 .remove = __devexit_p(ad2s90_remove),
145 };
146
147 static __init int ad2s90_spi_init(void)
148 {
149 return spi_register_driver(&ad2s90_driver);
150 }
151 module_init(ad2s90_spi_init);
152
153 static __exit void ad2s90_spi_exit(void)
154 {
155 spi_unregister_driver(&ad2s90_driver);
156 }
157 module_exit(ad2s90_spi_exit);
158
159 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
160 MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
161 MODULE_LICENSE("GPL v2");
This page took 0.039115 seconds and 6 git commands to generate.