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