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