staging:iio: rework of attribute registration.
[deliverable/linux.git] / drivers / staging / iio / accel / adis16203_core.c
CommitLineData
f11ba4f5
BS
1/*
2 * ADIS16203 Programmable Digital Vibration Sensor driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
f11ba4f5
BS
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>
99c97852 16#include <linux/module.h>
f11ba4f5
BS
17
18#include "../iio.h"
19#include "../sysfs.h"
d37b24b3 20#include "../ring_generic.h"
f11ba4f5
BS
21
22#include "adis16203.h"
23
24#define DRIVER_NAME "adis16203"
25
f11ba4f5
BS
26/**
27 * adis16203_spi_write_reg_8() - write single byte to a register
d37b24b3 28 * @indio_dev: iio device associated with child of actual device
f11ba4f5
BS
29 * @reg_address: the address of the register to be written
30 * @val: the value to write
31 **/
d37b24b3
JC
32static int adis16203_spi_write_reg_8(struct iio_dev *indio_dev,
33 u8 reg_address,
34 u8 val)
f11ba4f5
BS
35{
36 int ret;
6a0d8090 37 struct adis16203_state *st = iio_priv(indio_dev);
f11ba4f5
BS
38
39 mutex_lock(&st->buf_lock);
40 st->tx[0] = ADIS16203_WRITE_REG(reg_address);
41 st->tx[1] = val;
42
43 ret = spi_write(st->us, st->tx, 2);
44 mutex_unlock(&st->buf_lock);
45
46 return ret;
47}
48
49/**
50 * adis16203_spi_write_reg_16() - write 2 bytes to a pair of registers
d37b24b3 51 * @indio_dev: iio device associated with child of actual device
f11ba4f5
BS
52 * @reg_address: the address of the lower of the two registers. Second register
53 * is assumed to have address one greater.
54 * @val: value to be written
55 **/
d37b24b3
JC
56static int adis16203_spi_write_reg_16(struct iio_dev *indio_dev,
57 u8 lower_reg_address,
58 u16 value)
f11ba4f5
BS
59{
60 int ret;
61 struct spi_message msg;
6a0d8090 62 struct adis16203_state *st = iio_priv(indio_dev);
f11ba4f5
BS
63 struct spi_transfer xfers[] = {
64 {
65 .tx_buf = st->tx,
66 .bits_per_word = 8,
67 .len = 2,
68 .cs_change = 1,
69 }, {
70 .tx_buf = st->tx + 2,
71 .bits_per_word = 8,
72 .len = 2,
f11ba4f5
BS
73 },
74 };
75
76 mutex_lock(&st->buf_lock);
77 st->tx[0] = ADIS16203_WRITE_REG(lower_reg_address);
78 st->tx[1] = value & 0xFF;
79 st->tx[2] = ADIS16203_WRITE_REG(lower_reg_address + 1);
80 st->tx[3] = (value >> 8) & 0xFF;
81
82 spi_message_init(&msg);
83 spi_message_add_tail(&xfers[0], &msg);
84 spi_message_add_tail(&xfers[1], &msg);
85 ret = spi_sync(st->us, &msg);
86 mutex_unlock(&st->buf_lock);
87
88 return ret;
89}
90
91/**
92 * adis16203_spi_read_reg_16() - read 2 bytes from a 16-bit register
d37b24b3 93 * @indio_dev: iio device associated with child of actual device
f11ba4f5
BS
94 * @reg_address: the address of the lower of the two registers. Second register
95 * is assumed to have address one greater.
96 * @val: somewhere to pass back the value read
97 **/
d37b24b3 98static int adis16203_spi_read_reg_16(struct iio_dev *indio_dev,
f11ba4f5
BS
99 u8 lower_reg_address,
100 u16 *val)
101{
102 struct spi_message msg;
6a0d8090 103 struct adis16203_state *st = iio_priv(indio_dev);
f11ba4f5
BS
104 int ret;
105 struct spi_transfer xfers[] = {
106 {
107 .tx_buf = st->tx,
108 .bits_per_word = 8,
109 .len = 2,
110 .cs_change = 1,
111 .delay_usecs = 20,
112 }, {
113 .rx_buf = st->rx,
114 .bits_per_word = 8,
115 .len = 2,
f11ba4f5
BS
116 .delay_usecs = 20,
117 },
118 };
119
120 mutex_lock(&st->buf_lock);
121 st->tx[0] = ADIS16203_READ_REG(lower_reg_address);
122 st->tx[1] = 0;
123
124 spi_message_init(&msg);
125 spi_message_add_tail(&xfers[0], &msg);
126 spi_message_add_tail(&xfers[1], &msg);
127 ret = spi_sync(st->us, &msg);
128 if (ret) {
129 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
130 lower_reg_address);
131 goto error_ret;
132 }
133 *val = (st->rx[0] << 8) | st->rx[1];
134
135error_ret:
136 mutex_unlock(&st->buf_lock);
137 return ret;
138}
139
d37b24b3 140static int adis16203_check_status(struct iio_dev *indio_dev)
f11ba4f5 141{
d37b24b3 142 u16 status;
f11ba4f5 143 int ret;
f11ba4f5 144
d37b24b3
JC
145 ret = adis16203_spi_read_reg_16(indio_dev,
146 ADIS16203_DIAG_STAT,
147 &status);
148 if (ret < 0) {
149 dev_err(&indio_dev->dev, "Reading status failed\n");
f11ba4f5 150 goto error_ret;
f11ba4f5 151 }
d37b24b3 152 ret = status & 0x1F;
f11ba4f5 153
d37b24b3
JC
154 if (status & ADIS16203_DIAG_STAT_SELFTEST_FAIL)
155 dev_err(&indio_dev->dev, "Self test failure\n");
156 if (status & ADIS16203_DIAG_STAT_SPI_FAIL)
157 dev_err(&indio_dev->dev, "SPI failure\n");
158 if (status & ADIS16203_DIAG_STAT_FLASH_UPT)
159 dev_err(&indio_dev->dev, "Flash update failed\n");
160 if (status & ADIS16203_DIAG_STAT_POWER_HIGH)
161 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
162 if (status & ADIS16203_DIAG_STAT_POWER_LOW)
163 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
f11ba4f5
BS
164
165error_ret:
d37b24b3 166 return ret;
f11ba4f5
BS
167}
168
d37b24b3 169static int adis16203_reset(struct iio_dev *indio_dev)
f11ba4f5
BS
170{
171 int ret;
d37b24b3 172 ret = adis16203_spi_write_reg_8(indio_dev,
f11ba4f5
BS
173 ADIS16203_GLOB_CMD,
174 ADIS16203_GLOB_CMD_SW_RESET);
175 if (ret)
d37b24b3 176 dev_err(&indio_dev->dev, "problem resetting device");
f11ba4f5
BS
177
178 return ret;
179}
180
181static ssize_t adis16203_write_reset(struct device *dev,
182 struct device_attribute *attr,
183 const char *buf, size_t len)
184{
d37b24b3 185 struct iio_dev *indio_dev = dev_get_drvdata(dev);
f11ba4f5
BS
186 if (len < 1)
187 return -EINVAL;
188 switch (buf[0]) {
189 case '1':
190 case 'y':
191 case 'Y':
d37b24b3 192 return adis16203_reset(indio_dev);
f11ba4f5
BS
193 }
194 return -EINVAL;
195}
196
d37b24b3 197int adis16203_set_irq(struct iio_dev *indio_dev, bool enable)
f11ba4f5
BS
198{
199 int ret = 0;
200 u16 msc;
201
d37b24b3 202 ret = adis16203_spi_read_reg_16(indio_dev, ADIS16203_MSC_CTRL, &msc);
f11ba4f5
BS
203 if (ret)
204 goto error_ret;
205
206 msc |= ADIS16203_MSC_CTRL_ACTIVE_HIGH;
207 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_DIO1;
208 if (enable)
209 msc |= ADIS16203_MSC_CTRL_DATA_RDY_EN;
210 else
211 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_EN;
212
d37b24b3 213 ret = adis16203_spi_write_reg_16(indio_dev, ADIS16203_MSC_CTRL, msc);
f11ba4f5
BS
214
215error_ret:
216 return ret;
217}
218
d37b24b3 219static int adis16203_self_test(struct iio_dev *indio_dev)
f11ba4f5
BS
220{
221 int ret;
d37b24b3 222 ret = adis16203_spi_write_reg_16(indio_dev,
f11ba4f5
BS
223 ADIS16203_MSC_CTRL,
224 ADIS16203_MSC_CTRL_SELF_TEST_EN);
225 if (ret) {
d37b24b3 226 dev_err(&indio_dev->dev, "problem starting self test");
f11ba4f5
BS
227 goto err_ret;
228 }
229
d37b24b3 230 adis16203_check_status(indio_dev);
f11ba4f5
BS
231
232err_ret:
233 return ret;
234}
235
d37b24b3 236static int adis16203_initial_setup(struct iio_dev *indio_dev)
f11ba4f5
BS
237{
238 int ret;
f11ba4f5
BS
239
240 /* Disable IRQ */
d37b24b3 241 ret = adis16203_set_irq(indio_dev, false);
f11ba4f5 242 if (ret) {
d37b24b3 243 dev_err(&indio_dev->dev, "disable irq failed");
f11ba4f5
BS
244 goto err_ret;
245 }
246
247 /* Do self test */
d37b24b3 248 ret = adis16203_self_test(indio_dev);
f11ba4f5 249 if (ret) {
d37b24b3 250 dev_err(&indio_dev->dev, "self test failure");
f11ba4f5
BS
251 goto err_ret;
252 }
253
254 /* Read status register to check the result */
d37b24b3 255 ret = adis16203_check_status(indio_dev);
f11ba4f5 256 if (ret) {
d37b24b3
JC
257 adis16203_reset(indio_dev);
258 dev_err(&indio_dev->dev, "device not playing ball -> reset");
f11ba4f5 259 msleep(ADIS16203_STARTUP_DELAY);
d37b24b3 260 ret = adis16203_check_status(indio_dev);
f11ba4f5 261 if (ret) {
d37b24b3 262 dev_err(&indio_dev->dev, "giving up");
f11ba4f5
BS
263 goto err_ret;
264 }
265 }
266
f11ba4f5
BS
267err_ret:
268 return ret;
269}
270
d37b24b3
JC
271enum adis16203_chan {
272 in_supply,
273 in_aux,
274 incli_x,
275 incli_y,
276 temp,
277};
278
279static u8 adis16203_addresses[5][2] = {
280 [in_supply] = { ADIS16203_SUPPLY_OUT },
281 [in_aux] = { ADIS16203_AUX_ADC },
282 [incli_x] = { ADIS16203_XINCL_OUT, ADIS16203_INCL_NULL},
283 [incli_y] = { ADIS16203_YINCL_OUT },
284 [temp] = { ADIS16203_TEMP_OUT }
285};
286
287static int adis16203_write_raw(struct iio_dev *indio_dev,
288 struct iio_chan_spec const *chan,
289 int val,
290 int val2,
291 long mask)
292{
293 /* currently only one writable parameter which keeps this simple */
294 u8 addr = adis16203_addresses[chan->address][1];
295 return adis16203_spi_write_reg_16(indio_dev, addr, val & 0x3FFF);
296}
297
298static int adis16203_read_raw(struct iio_dev *indio_dev,
299 struct iio_chan_spec const *chan,
300 int *val, int *val2,
301 long mask)
302{
303 int ret;
304 int bits;
305 u8 addr;
306 s16 val16;
307 switch (mask) {
308 case 0:
309 mutex_lock(&indio_dev->mlock);
310 addr = adis16203_addresses[chan->address][0];
311 ret = adis16203_spi_read_reg_16(indio_dev, addr, &val16);
8f896155
DC
312 if (ret) {
313 mutex_unlock(&indio_dev->mlock);
d37b24b3 314 return ret;
8f896155 315 }
d37b24b3
JC
316
317 if (val16 & ADIS16203_ERROR_ACTIVE) {
318 ret = adis16203_check_status(indio_dev);
8f896155
DC
319 if (ret) {
320 mutex_unlock(&indio_dev->mlock);
d37b24b3 321 return ret;
8f896155 322 }
d37b24b3
JC
323 }
324 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
325 if (chan->scan_type.sign == 's')
326 val16 = (s16)(val16 <<
327 (16 - chan->scan_type.realbits)) >>
328 (16 - chan->scan_type.realbits);
329 *val = val16;
330 mutex_unlock(&indio_dev->mlock);
331 return IIO_VAL_INT;
332 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
333 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
334 switch (chan->type) {
335 case IIO_IN:
336 *val = 0;
337 if (chan->channel == 0)
338 *val2 = 1220;
339 else
340 *val2 = 610;
341 return IIO_VAL_INT_PLUS_MICRO;
342 case IIO_TEMP:
343 *val = 0;
344 *val2 = -470000;
345 return IIO_VAL_INT_PLUS_MICRO;
346 case IIO_INCLI:
347 *val = 0;
348 *val2 = 25000;
349 return IIO_VAL_INT_PLUS_MICRO;
350 default:
351 return -EINVAL;
352 }
353 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
354 *val = 25;
355 return IIO_VAL_INT;
356 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
357 bits = 14;
358 mutex_lock(&indio_dev->mlock);
359 addr = adis16203_addresses[chan->address][1];
360 ret = adis16203_spi_read_reg_16(indio_dev, addr, &val16);
361 if (ret) {
362 mutex_unlock(&indio_dev->mlock);
363 return ret;
364 }
365 val16 &= (1 << bits) - 1;
366 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
367 *val = val16;
368 mutex_unlock(&indio_dev->mlock);
369 return IIO_VAL_INT;
370 default:
371 return -EINVAL;
372 }
373}
374
375static struct iio_chan_spec adis16203_channels[] = {
376 IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0,
377 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
378 in_supply, ADIS16203_SCAN_SUPPLY,
379 IIO_ST('u', 12, 16, 0), 0),
380 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
381 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
382 in_aux, ADIS16203_SCAN_AUX_ADC,
383 IIO_ST('u', 12, 16, 0), 0),
384 IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_X,
385 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
386 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
387 incli_x, ADIS16203_SCAN_INCLI_X,
388 IIO_ST('s', 14, 16, 0), 0),
389 /* Fixme: Not what it appears to be - see data sheet */
390 IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_Y,
391 (1 << IIO_CHAN_INFO_SCALE_SHARED),
392 incli_y, ADIS16203_SCAN_INCLI_Y,
393 IIO_ST('s', 14, 16, 0), 0),
394 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
395 (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
396 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE),
397 temp, ADIS16203_SCAN_TEMP,
398 IIO_ST('u', 12, 16, 0), 0),
399 IIO_CHAN_SOFT_TIMESTAMP(5),
400};
f11ba4f5
BS
401
402static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16203_write_reset, 0);
403
f11ba4f5 404static struct attribute *adis16203_attributes[] = {
f11ba4f5 405 &iio_dev_attr_reset.dev_attr.attr,
f11ba4f5
BS
406 NULL
407};
408
409static const struct attribute_group adis16203_attribute_group = {
410 .attrs = adis16203_attributes,
411};
412
6fe8135f
JC
413static const struct iio_info adis16203_info = {
414 .attrs = &adis16203_attribute_group,
415 .read_raw = &adis16203_read_raw,
416 .write_raw = &adis16203_write_raw,
417 .driver_module = THIS_MODULE,
418};
419
f11ba4f5
BS
420static int __devinit adis16203_probe(struct spi_device *spi)
421{
26d25ae3 422 int ret;
6a0d8090
JC
423 struct iio_dev *indio_dev;
424 struct adis16203_state *st;
f11ba4f5 425
6a0d8090
JC
426 /* setup the industrialio driver allocated elements */
427 indio_dev = iio_allocate_device(sizeof(*st));
428 if (indio_dev == NULL) {
f11ba4f5 429 ret = -ENOMEM;
6a0d8090 430 goto error_ret;
f11ba4f5 431 }
6a0d8090
JC
432 st = iio_priv(indio_dev);
433 /* this is only used for removal purposes */
434 spi_set_drvdata(spi, indio_dev);
f11ba4f5
BS
435 st->us = spi;
436 mutex_init(&st->buf_lock);
6a0d8090
JC
437
438 indio_dev->name = spi->dev.driver->name;
439 indio_dev->dev.parent = &spi->dev;
440 indio_dev->channels = adis16203_channels;
441 indio_dev->num_channels = ARRAY_SIZE(adis16203_channels);
442 indio_dev->info = &adis16203_info;
443 indio_dev->modes = INDIO_DIRECT_MODE;
444
445 ret = adis16203_configure_ring(indio_dev);
f11ba4f5
BS
446 if (ret)
447 goto error_free_dev;
448
c009f7e4
JC
449 ret = iio_ring_buffer_register(indio_dev,
450 adis16203_channels,
451 ARRAY_SIZE(adis16203_channels));
f11ba4f5
BS
452 if (ret) {
453 printk(KERN_ERR "failed to initialize the ring\n");
454 goto error_unreg_ring_funcs;
455 }
456
457 if (spi->irq) {
6a0d8090 458 ret = adis16203_probe_trigger(indio_dev);
f11ba4f5 459 if (ret)
9e558ff5 460 goto error_uninitialize_ring;
f11ba4f5
BS
461 }
462
463 /* Get the device into a sane initial state */
6a0d8090 464 ret = adis16203_initial_setup(indio_dev);
f11ba4f5
BS
465 if (ret)
466 goto error_remove_trigger;
26d25ae3
JC
467
468 ret = iio_device_register(indio_dev);
469 if (ret)
470 goto error_remove_trigger;
471
f11ba4f5
BS
472 return 0;
473
474error_remove_trigger:
6a0d8090 475 adis16203_remove_trigger(indio_dev);
f11ba4f5 476error_uninitialize_ring:
1aa04278 477 iio_ring_buffer_unregister(indio_dev);
f11ba4f5 478error_unreg_ring_funcs:
6a0d8090 479 adis16203_unconfigure_ring(indio_dev);
f11ba4f5 480error_free_dev:
26d25ae3 481 iio_free_device(indio_dev);
f11ba4f5
BS
482error_ret:
483 return ret;
484}
485
486static int adis16203_remove(struct spi_device *spi)
487{
6a0d8090 488 struct iio_dev *indio_dev = spi_get_drvdata(spi);
f11ba4f5 489
f11ba4f5 490 adis16203_remove_trigger(indio_dev);
1aa04278 491 iio_ring_buffer_unregister(indio_dev);
f11ba4f5 492 adis16203_unconfigure_ring(indio_dev);
3e394407 493 iio_device_unregister(indio_dev);
f11ba4f5
BS
494
495 return 0;
496}
497
498static struct spi_driver adis16203_driver = {
499 .driver = {
500 .name = "adis16203",
501 .owner = THIS_MODULE,
502 },
503 .probe = adis16203_probe,
504 .remove = __devexit_p(adis16203_remove),
505};
506
507static __init int adis16203_init(void)
508{
509 return spi_register_driver(&adis16203_driver);
510}
511module_init(adis16203_init);
512
513static __exit void adis16203_exit(void)
514{
515 spi_unregister_driver(&adis16203_driver);
516}
517module_exit(adis16203_exit);
518
519MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
520MODULE_DESCRIPTION("Analog Devices ADIS16203 Programmable Digital Vibration Sensor driver");
521MODULE_LICENSE("GPL v2");
This page took 0.13424 seconds and 5 git commands to generate.