Merge branches 'fixes', 'pgt-next' and 'versatile' into devel
[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 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 /* need 600ns between CS and the first falling edge of SCLK */
112 spi->max_speed_hz = 830000;
113 spi->mode = SPI_MODE_3;
114 spi_setup(spi);
115
116 return 0;
117
118 error_free_dev:
119 iio_free_device(st->idev);
120 error_free_st:
121 kfree(st);
122 error_ret:
123 return ret;
124 }
125
126 static 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
136 static 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
145 static __init int ad2s90_spi_init(void)
146 {
147 return spi_register_driver(&ad2s90_driver);
148 }
149 module_init(ad2s90_spi_init);
150
151 static __exit void ad2s90_spi_exit(void)
152 {
153 spi_unregister_driver(&ad2s90_driver);
154 }
155 module_exit(ad2s90_spi_exit);
156
157 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
158 MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
159 MODULE_LICENSE("GPL v2");
This page took 0.038703 seconds and 6 git commands to generate.