Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[deliverable/linux.git] / drivers / staging / iio / gyro / adxrs450_core.c
1 /*
2 * ADXRS450/ADXRS453 Digital Output Gyroscope Driver
3 *
4 * Copyright 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/delay.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20
21 #include "../iio.h"
22 #include "../sysfs.h"
23
24 #include "adxrs450.h"
25
26 /**
27 * adxrs450_spi_read_reg_16() - read 2 bytes from a register pair
28 * @dev: device associated with child of actual iio_dev
29 * @reg_address: the address of the lower of the two registers,which should be an even address,
30 * Second register's address is reg_address + 1.
31 * @val: somewhere to pass back the value read
32 **/
33 static int adxrs450_spi_read_reg_16(struct iio_dev *indio_dev,
34 u8 reg_address,
35 u16 *val)
36 {
37 struct adxrs450_state *st = iio_priv(indio_dev);
38 int ret;
39
40 mutex_lock(&st->buf_lock);
41 st->tx[0] = ADXRS450_READ_DATA | (reg_address >> 7);
42 st->tx[1] = reg_address << 1;
43 st->tx[2] = 0;
44 st->tx[3] = 0;
45
46 if (!(hweight32(be32_to_cpu(*(u32 *)st->tx)) & 1))
47 st->tx[3] |= ADXRS450_P;
48
49 ret = spi_write(st->us, st->tx, 4);
50 if (ret) {
51 dev_err(&st->us->dev, "problem while reading 16 bit register 0x%02x\n",
52 reg_address);
53 goto error_ret;
54 }
55 ret = spi_read(st->us, st->rx, 4);
56 if (ret) {
57 dev_err(&st->us->dev, "problem while reading 16 bit register 0x%02x\n",
58 reg_address);
59 goto error_ret;
60 }
61
62 *val = (be32_to_cpu(*(u32 *)st->rx) >> 5) & 0xFFFF;
63
64 error_ret:
65 mutex_unlock(&st->buf_lock);
66 return ret;
67 }
68
69 /**
70 * adxrs450_spi_write_reg_16() - write 2 bytes data to a register pair
71 * @dev: device associated with child of actual actual iio_dev
72 * @reg_address: the address of the lower of the two registers,which should be an even address,
73 * Second register's address is reg_address + 1.
74 * @val: value to be written.
75 **/
76 static int adxrs450_spi_write_reg_16(struct iio_dev *indio_dev,
77 u8 reg_address,
78 u16 val)
79 {
80 struct adxrs450_state *st = iio_priv(indio_dev);
81 int ret;
82
83 mutex_lock(&st->buf_lock);
84 st->tx[0] = ADXRS450_WRITE_DATA | reg_address >> 7;
85 st->tx[1] = reg_address << 1 | val >> 15;
86 st->tx[2] = val >> 7;
87 st->tx[3] = val << 1;
88
89 if (!(hweight32(be32_to_cpu(*(u32 *)st->tx)) & 1))
90 st->tx[3] |= ADXRS450_P;
91
92 ret = spi_write(st->us, st->tx, 4);
93 if (ret)
94 dev_err(&st->us->dev, "problem while writing 16 bit register 0x%02x\n",
95 reg_address);
96 msleep(1); /* enforce sequential transfer delay 0.1ms */
97 mutex_unlock(&st->buf_lock);
98 return ret;
99 }
100
101 /**
102 * adxrs450_spi_sensor_data() - read 2 bytes sensor data
103 * @dev: device associated with child of actual iio_dev
104 * @val: somewhere to pass back the value read
105 **/
106 static int adxrs450_spi_sensor_data(struct iio_dev *indio_dev, s16 *val)
107 {
108 struct adxrs450_state *st = iio_priv(indio_dev);
109 int ret;
110
111 mutex_lock(&st->buf_lock);
112 st->tx[0] = ADXRS450_SENSOR_DATA;
113 st->tx[1] = 0;
114 st->tx[2] = 0;
115 st->tx[3] = 0;
116
117 ret = spi_write(st->us, st->tx, 4);
118 if (ret) {
119 dev_err(&st->us->dev, "Problem while reading sensor data\n");
120 goto error_ret;
121 }
122
123 ret = spi_read(st->us, st->rx, 4);
124 if (ret) {
125 dev_err(&st->us->dev, "Problem while reading sensor data\n");
126 goto error_ret;
127 }
128
129 *val = (be32_to_cpu(*(u32 *)st->rx) >> 10) & 0xFFFF;
130
131 error_ret:
132 mutex_unlock(&st->buf_lock);
133 return ret;
134 }
135
136 /**
137 * adxrs450_spi_initial() - use for initializing procedure.
138 * @st: device instance specific data
139 * @val: somewhere to pass back the value read
140 **/
141 static int adxrs450_spi_initial(struct adxrs450_state *st,
142 u32 *val, char chk)
143 {
144 struct spi_message msg;
145 int ret;
146 struct spi_transfer xfers = {
147 .tx_buf = st->tx,
148 .rx_buf = st->rx,
149 .bits_per_word = 8,
150 .len = 4,
151 };
152
153 mutex_lock(&st->buf_lock);
154 st->tx[0] = ADXRS450_SENSOR_DATA;
155 st->tx[1] = 0;
156 st->tx[2] = 0;
157 st->tx[3] = 0;
158 if (chk)
159 st->tx[3] |= (ADXRS450_CHK | ADXRS450_P);
160 spi_message_init(&msg);
161 spi_message_add_tail(&xfers, &msg);
162 ret = spi_sync(st->us, &msg);
163 if (ret) {
164 dev_err(&st->us->dev, "Problem while reading initializing data\n");
165 goto error_ret;
166 }
167
168 *val = be32_to_cpu(*(u32 *)st->rx);
169
170 error_ret:
171 mutex_unlock(&st->buf_lock);
172 return ret;
173 }
174
175 /* Recommended Startup Sequence by spec */
176 static int adxrs450_initial_setup(struct iio_dev *indio_dev)
177 {
178 u32 t;
179 u16 data;
180 int ret;
181 struct adxrs450_state *st = iio_priv(indio_dev);
182
183 msleep(ADXRS450_STARTUP_DELAY*2);
184 ret = adxrs450_spi_initial(st, &t, 1);
185 if (ret)
186 return ret;
187 if (t != 0x01)
188 dev_warn(&st->us->dev, "The initial power on response "
189 "is not correct! Restart without reset?\n");
190
191 msleep(ADXRS450_STARTUP_DELAY);
192 ret = adxrs450_spi_initial(st, &t, 0);
193 if (ret)
194 return ret;
195
196 msleep(ADXRS450_STARTUP_DELAY);
197 ret = adxrs450_spi_initial(st, &t, 0);
198 if (ret)
199 return ret;
200 if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
201 dev_err(&st->us->dev, "The second response is not correct!\n");
202 return -EIO;
203
204 }
205 ret = adxrs450_spi_initial(st, &t, 0);
206 if (ret)
207 return ret;
208 if (((t & 0xff) | 0x01) != 0xff || ADXRS450_GET_ST(t) != 2) {
209 dev_err(&st->us->dev, "The third response is not correct!\n");
210 return -EIO;
211
212 }
213 ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_FAULT1, &data);
214 if (ret)
215 return ret;
216 if (data & 0x0fff) {
217 dev_err(&st->us->dev, "The device is not in normal status!\n");
218 return -EINVAL;
219 }
220 ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_PID1, &data);
221 if (ret)
222 return ret;
223 dev_info(&st->us->dev, "The Part ID is 0x%x\n", data);
224
225 ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_SNL, &data);
226 if (ret)
227 return ret;
228 t = data;
229 ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_SNH, &data);
230 if (ret)
231 return ret;
232 t |= data << 16;
233 dev_info(&st->us->dev, "The Serial Number is 0x%x\n", t);
234
235 return 0;
236 }
237
238 static int adxrs450_write_raw(struct iio_dev *indio_dev,
239 struct iio_chan_spec const *chan,
240 int val,
241 int val2,
242 long mask)
243 {
244 int ret;
245 switch (mask) {
246 case IIO_CHAN_INFO_CALIBBIAS:
247 ret = adxrs450_spi_write_reg_16(indio_dev,
248 ADXRS450_DNC1,
249 val & 0x3FF);
250 break;
251 default:
252 ret = -EINVAL;
253 break;
254 }
255 return ret;
256 }
257
258 static int adxrs450_read_raw(struct iio_dev *indio_dev,
259 struct iio_chan_spec const *chan,
260 int *val,
261 int *val2,
262 long mask)
263 {
264 int ret;
265 s16 t;
266
267 switch (mask) {
268 case 0:
269 switch (chan->type) {
270 case IIO_ANGL_VEL:
271 ret = adxrs450_spi_sensor_data(indio_dev, &t);
272 if (ret)
273 break;
274 *val = t;
275 ret = IIO_VAL_INT;
276 break;
277 case IIO_TEMP:
278 ret = adxrs450_spi_read_reg_16(indio_dev,
279 ADXRS450_TEMP1, &t);
280 if (ret)
281 break;
282 *val = (t >> 6) + 225;
283 ret = IIO_VAL_INT;
284 break;
285 default:
286 ret = -EINVAL;
287 break;
288 }
289 break;
290 case IIO_CHAN_INFO_SCALE:
291 switch (chan->type) {
292 case IIO_ANGL_VEL:
293 *val = 0;
294 *val2 = 218166;
295 return IIO_VAL_INT_PLUS_NANO;
296 case IIO_TEMP:
297 *val = 200;
298 *val2 = 0;
299 return IIO_VAL_INT;
300 default:
301 return -EINVAL;
302 }
303 break;
304 case IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW:
305 ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_QUAD1, &t);
306 if (ret)
307 break;
308 *val = t;
309 ret = IIO_VAL_INT;
310 break;
311 case IIO_CHAN_INFO_CALIBBIAS:
312 ret = adxrs450_spi_read_reg_16(indio_dev, ADXRS450_DNC1, &t);
313 if (ret)
314 break;
315 *val = t;
316 ret = IIO_VAL_INT;
317 break;
318 default:
319 ret = -EINVAL;
320 break;
321 }
322
323 return ret;
324 }
325
326 static const struct iio_chan_spec adxrs450_channels[2][2] = {
327 [ID_ADXRS450] = {
328 {
329 .type = IIO_ANGL_VEL,
330 .modified = 1,
331 .channel2 = IIO_MOD_Z,
332 .info_mask = IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT |
333 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE_BIT |
334 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
335 }, {
336 .type = IIO_TEMP,
337 .indexed = 1,
338 .channel = 0,
339 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
340 }
341 },
342 [ID_ADXRS453] = {
343 {
344 .type = IIO_ANGL_VEL,
345 .modified = 1,
346 .channel2 = IIO_MOD_Z,
347 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
348 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE_BIT,
349 }, {
350 .type = IIO_TEMP,
351 .indexed = 1,
352 .channel = 0,
353 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
354 }
355 },
356 };
357
358 static const struct iio_info adxrs450_info = {
359 .driver_module = THIS_MODULE,
360 .read_raw = &adxrs450_read_raw,
361 .write_raw = &adxrs450_write_raw,
362 };
363
364 static int __devinit adxrs450_probe(struct spi_device *spi)
365 {
366 int ret;
367 struct adxrs450_state *st;
368 struct iio_dev *indio_dev;
369
370 /* setup the industrialio driver allocated elements */
371 indio_dev = iio_allocate_device(sizeof(*st));
372 if (indio_dev == NULL) {
373 ret = -ENOMEM;
374 goto error_ret;
375 }
376 st = iio_priv(indio_dev);
377 st->us = spi;
378 mutex_init(&st->buf_lock);
379 /* This is only used for removal purposes */
380 spi_set_drvdata(spi, indio_dev);
381
382 indio_dev->dev.parent = &spi->dev;
383 indio_dev->info = &adxrs450_info;
384 indio_dev->modes = INDIO_DIRECT_MODE;
385 indio_dev->channels =
386 adxrs450_channels[spi_get_device_id(spi)->driver_data];
387 indio_dev->num_channels = ARRAY_SIZE(adxrs450_channels);
388 indio_dev->name = spi->dev.driver->name;
389
390 ret = iio_device_register(indio_dev);
391 if (ret)
392 goto error_free_dev;
393
394 /* Get the device into a sane initial state */
395 ret = adxrs450_initial_setup(indio_dev);
396 if (ret)
397 goto error_initial;
398 return 0;
399 error_initial:
400 iio_device_unregister(indio_dev);
401 error_free_dev:
402 iio_free_device(indio_dev);
403
404 error_ret:
405 return ret;
406 }
407
408 static int adxrs450_remove(struct spi_device *spi)
409 {
410 iio_device_unregister(spi_get_drvdata(spi));
411 iio_free_device(spi_get_drvdata(spi));
412
413 return 0;
414 }
415
416 static const struct spi_device_id adxrs450_id[] = {
417 {"adxrs450", ID_ADXRS450},
418 {"adxrs453", ID_ADXRS453},
419 {}
420 };
421 MODULE_DEVICE_TABLE(spi, adxrs450_id);
422
423 static struct spi_driver adxrs450_driver = {
424 .driver = {
425 .name = "adxrs450",
426 .owner = THIS_MODULE,
427 },
428 .probe = adxrs450_probe,
429 .remove = __devexit_p(adxrs450_remove),
430 .id_table = adxrs450_id,
431 };
432 module_spi_driver(adxrs450_driver);
433
434 MODULE_AUTHOR("Cliff Cai <cliff.cai@xxxxxxxxxx>");
435 MODULE_DESCRIPTION("Analog Devices ADXRS450/ADXRS453 Gyroscope SPI driver");
436 MODULE_LICENSE("GPL v2");
This page took 0.060156 seconds and 5 git commands to generate.