Commit | Line | Data |
---|---|---|
1b2f99e1 BS |
1 | /* |
2 | * ADIS16080/100 Yaw Rate Gyroscope with SPI driver | |
3 | * | |
4 | * Copyright 2010 Analog Devices Inc. | |
5 | * | |
6 | * Licensed under the GPL-2 or later. | |
7 | */ | |
1b2f99e1 BS |
8 | #include <linux/gpio.h> |
9 | #include <linux/delay.h> | |
10 | #include <linux/mutex.h> | |
11 | #include <linux/device.h> | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/spi/spi.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/sysfs.h> | |
1b2f99e1 BS |
16 | |
17 | #include "../iio.h" | |
18 | #include "../sysfs.h" | |
19 | #include "gyro.h" | |
20 | #include "../adc/adc.h" | |
21 | ||
35d2b6f9 JC |
22 | #define ADIS16080_DIN_GYRO (0 << 10) /* Gyroscope output */ |
23 | #define ADIS16080_DIN_TEMP (1 << 10) /* Temperature output */ | |
24 | #define ADIS16080_DIN_AIN1 (2 << 10) | |
25 | #define ADIS16080_DIN_AIN2 (3 << 10) | |
26 | ||
27 | /* | |
28 | * 1: Write contents on DIN to control register. | |
29 | * 0: No changes to control register. | |
30 | */ | |
31 | ||
32 | #define ADIS16080_DIN_WRITE (1 << 15) | |
33 | ||
34 | /** | |
35 | * struct adis16080_state - device instance specific data | |
36 | * @us: actual spi_device to write data | |
37 | * @indio_dev: industrial I/O device structure | |
25985edc | 38 | * @buf: transmit or receive buffer |
35d2b6f9 JC |
39 | * @buf_lock: mutex to protect tx and rx |
40 | **/ | |
41 | struct adis16080_state { | |
42 | struct spi_device *us; | |
43 | struct iio_dev *indio_dev; | |
44 | struct mutex buf_lock; | |
45 | ||
46 | u8 buf[2] ____cacheline_aligned; | |
47 | }; | |
1b2f99e1 | 48 | |
8d016b43 | 49 | static int adis16080_spi_write(struct device *dev, |
1b2f99e1 BS |
50 | u16 val) |
51 | { | |
52 | int ret; | |
53 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
54 | struct adis16080_state *st = iio_dev_get_devdata(indio_dev); | |
55 | ||
56 | mutex_lock(&st->buf_lock); | |
35d2b6f9 JC |
57 | st->buf[0] = val >> 8; |
58 | st->buf[1] = val; | |
1b2f99e1 | 59 | |
35d2b6f9 | 60 | ret = spi_write(st->us, st->buf, 2); |
1b2f99e1 BS |
61 | mutex_unlock(&st->buf_lock); |
62 | ||
63 | return ret; | |
64 | } | |
65 | ||
8d016b43 | 66 | static int adis16080_spi_read(struct device *dev, |
1b2f99e1 BS |
67 | u16 *val) |
68 | { | |
69 | int ret; | |
70 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
71 | struct adis16080_state *st = iio_dev_get_devdata(indio_dev); | |
72 | ||
73 | mutex_lock(&st->buf_lock); | |
74 | ||
35d2b6f9 | 75 | ret = spi_read(st->us, st->buf, 2); |
1b2f99e1 BS |
76 | |
77 | if (ret == 0) | |
35d2b6f9 | 78 | *val = ((st->buf[0] & 0xF) << 8) | st->buf[1]; |
1b2f99e1 BS |
79 | mutex_unlock(&st->buf_lock); |
80 | ||
81 | return ret; | |
82 | } | |
83 | ||
84 | static ssize_t adis16080_read(struct device *dev, | |
85 | struct device_attribute *attr, | |
86 | char *buf) | |
87 | { | |
35d2b6f9 | 88 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
1b2f99e1 | 89 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
8d016b43 | 90 | u16 val = 0; |
1b2f99e1 BS |
91 | ssize_t ret; |
92 | ||
93 | /* Take the iio_dev status lock */ | |
94 | mutex_lock(&indio_dev->mlock); | |
35d2b6f9 JC |
95 | ret = adis16080_spi_write(dev, |
96 | this_attr->address | ADIS16080_DIN_WRITE); | |
97 | if (ret < 0) | |
98 | goto error_ret; | |
1b2f99e1 | 99 | ret = adis16080_spi_read(dev, &val); |
35d2b6f9 | 100 | error_ret: |
1b2f99e1 BS |
101 | mutex_unlock(&indio_dev->mlock); |
102 | ||
103 | if (ret == 0) | |
104 | return sprintf(buf, "%d\n", val); | |
105 | else | |
106 | return ret; | |
107 | } | |
35d2b6f9 JC |
108 | static IIO_DEV_ATTR_GYRO_Z(adis16080_read, ADIS16080_DIN_GYRO); |
109 | static IIO_DEVICE_ATTR(temp_raw, S_IRUGO, adis16080_read, NULL, | |
110 | ADIS16080_DIN_TEMP); | |
111 | static IIO_DEV_ATTR_IN_RAW(0, adis16080_read, ADIS16080_DIN_AIN1); | |
112 | static IIO_DEV_ATTR_IN_RAW(1, adis16080_read, ADIS16080_DIN_AIN2); | |
1b2f99e1 BS |
113 | static IIO_CONST_ATTR(name, "adis16080"); |
114 | ||
1b2f99e1 | 115 | static struct attribute *adis16080_attributes[] = { |
35d2b6f9 JC |
116 | &iio_dev_attr_gyro_z_raw.dev_attr.attr, |
117 | &iio_dev_attr_temp_raw.dev_attr.attr, | |
118 | &iio_dev_attr_in0_raw.dev_attr.attr, | |
119 | &iio_dev_attr_in1_raw.dev_attr.attr, | |
1b2f99e1 BS |
120 | &iio_const_attr_name.dev_attr.attr, |
121 | NULL | |
122 | }; | |
123 | ||
124 | static const struct attribute_group adis16080_attribute_group = { | |
125 | .attrs = adis16080_attributes, | |
126 | }; | |
127 | ||
128 | static int __devinit adis16080_probe(struct spi_device *spi) | |
129 | { | |
130 | int ret, regdone = 0; | |
131 | struct adis16080_state *st = kzalloc(sizeof *st, GFP_KERNEL); | |
132 | if (!st) { | |
133 | ret = -ENOMEM; | |
134 | goto error_ret; | |
135 | } | |
136 | /* this is only used for removal purposes */ | |
137 | spi_set_drvdata(spi, st); | |
138 | ||
139 | /* Allocate the comms buffers */ | |
1b2f99e1 BS |
140 | st->us = spi; |
141 | mutex_init(&st->buf_lock); | |
142 | /* setup the industrialio driver allocated elements */ | |
143 | st->indio_dev = iio_allocate_device(); | |
144 | if (st->indio_dev == NULL) { | |
145 | ret = -ENOMEM; | |
35d2b6f9 | 146 | goto error_free_st; |
1b2f99e1 BS |
147 | } |
148 | ||
149 | st->indio_dev->dev.parent = &spi->dev; | |
1b2f99e1 BS |
150 | st->indio_dev->attrs = &adis16080_attribute_group; |
151 | st->indio_dev->dev_data = (void *)(st); | |
152 | st->indio_dev->driver_module = THIS_MODULE; | |
153 | st->indio_dev->modes = INDIO_DIRECT_MODE; | |
154 | ||
1b2f99e1 BS |
155 | ret = iio_device_register(st->indio_dev); |
156 | if (ret) | |
8d016b43 | 157 | goto error_free_dev; |
1b2f99e1 BS |
158 | regdone = 1; |
159 | ||
1b2f99e1 BS |
160 | return 0; |
161 | ||
1b2f99e1 BS |
162 | error_free_dev: |
163 | if (regdone) | |
164 | iio_device_unregister(st->indio_dev); | |
165 | else | |
166 | iio_free_device(st->indio_dev); | |
1b2f99e1 BS |
167 | error_free_st: |
168 | kfree(st); | |
169 | error_ret: | |
170 | return ret; | |
171 | } | |
172 | ||
173 | /* fixme, confirm ordering in this function */ | |
174 | static int adis16080_remove(struct spi_device *spi) | |
175 | { | |
176 | struct adis16080_state *st = spi_get_drvdata(spi); | |
177 | struct iio_dev *indio_dev = st->indio_dev; | |
178 | ||
1b2f99e1 | 179 | iio_device_unregister(indio_dev); |
1b2f99e1 BS |
180 | kfree(st); |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
185 | static struct spi_driver adis16080_driver = { | |
186 | .driver = { | |
187 | .name = "adis16080", | |
188 | .owner = THIS_MODULE, | |
189 | }, | |
190 | .probe = adis16080_probe, | |
191 | .remove = __devexit_p(adis16080_remove), | |
192 | }; | |
193 | ||
194 | static __init int adis16080_init(void) | |
195 | { | |
196 | return spi_register_driver(&adis16080_driver); | |
197 | } | |
198 | module_init(adis16080_init); | |
199 | ||
200 | static __exit void adis16080_exit(void) | |
201 | { | |
202 | spi_unregister_driver(&adis16080_driver); | |
203 | } | |
204 | module_exit(adis16080_exit); | |
205 | ||
206 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); | |
8d016b43 | 207 | MODULE_DESCRIPTION("Analog Devices ADIS16080/100 Yaw Rate Gyroscope Driver"); |
1b2f99e1 | 208 | MODULE_LICENSE("GPL v2"); |