e27e3b7d1003aeef43285765b55e85e7763d52fa
[safe/jmp/linux-2.6] / drivers / staging / iio / accel / sca3000_core.c
1 /*
2  * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  *
8  * Copyright (c) 2009 Jonathan Cameron <jic23@cam.ac.uk>
9  *
10  * See industrialio/accels/sca3000.h for comments.
11  */
12
13 #include <linux/interrupt.h>
14 #include <linux/gpio.h>
15 #include <linux/fs.h>
16 #include <linux/device.h>
17 #include <linux/kernel.h>
18 #include <linux/spi/spi.h>
19 #include <linux/sysfs.h>
20 #include "../iio.h"
21 #include "../sysfs.h"
22 #include "../ring_generic.h"
23
24 #include "accel.h"
25 #include "sca3000.h"
26
27 enum sca3000_variant {
28         d01,
29         d03,
30         e02,
31         e04,
32         e05,
33         l01,
34 };
35
36 /* Note where option modes are not defined, the chip simply does not
37  * support any.
38  * Other chips in the sca3000 series use i2c and are not included here.
39  *
40  * Some of these devices are only listed in the family data sheet and
41  * do not actually appear to be available.
42  */
43 static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = {
44         {
45                 .name = "sca3000-d01",
46                 .temp_output = true,
47                 .measurement_mode_freq = 250,
48                 .option_mode_1 = SCA3000_OP_MODE_BYPASS,
49                 .option_mode_1_freq = 250,
50         }, {
51                 /* No data sheet available - may be the same as the 3100-d03?*/
52                 .name = "sca3000-d03",
53                 .temp_output = true,
54         }, {
55                 .name = "sca3000-e02",
56                 .measurement_mode_freq = 125,
57                 .option_mode_1 = SCA3000_OP_MODE_NARROW,
58                 .option_mode_1_freq = 63,
59         }, {
60                 .name = "sca3000-e04",
61                 .measurement_mode_freq = 100,
62                 .option_mode_1 = SCA3000_OP_MODE_NARROW,
63                 .option_mode_1_freq = 50,
64                 .option_mode_2 = SCA3000_OP_MODE_WIDE,
65                 .option_mode_2_freq = 400,
66         }, {
67                 .name = "sca3000-e05",
68                 .measurement_mode_freq = 200,
69                 .option_mode_1 = SCA3000_OP_MODE_NARROW,
70                 .option_mode_1_freq = 50,
71                 .option_mode_2 = SCA3000_OP_MODE_WIDE,
72                 .option_mode_2_freq = 400,
73         }, {
74                 /* No data sheet available.
75                  * Frequencies are unknown.
76                  */
77                 .name = "sca3000-l01",
78                 .temp_output = true,
79                 .option_mode_1 = SCA3000_OP_MODE_BYPASS,
80         },
81 };
82
83
84 int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
85 {
86         struct spi_transfer xfer = {
87                 .bits_per_word = 8,
88                 .len = 2,
89                 .cs_change = 1,
90                 .tx_buf = st->tx,
91         };
92         struct spi_message msg;
93
94         st->tx[0] = SCA3000_WRITE_REG(address);
95         st->tx[1] = val;
96         spi_message_init(&msg);
97         spi_message_add_tail(&xfer, &msg);
98
99         return spi_sync(st->us, &msg);
100 }
101
102 int sca3000_read_data(struct sca3000_state *st,
103                       uint8_t reg_address_high,
104                       u8 **rx_p,
105                       int len)
106 {
107         int ret;
108         struct spi_message msg;
109         struct spi_transfer xfer = {
110                 .bits_per_word = 8,
111                 .len = len + 1,
112                 .cs_change = 1,
113                 .tx_buf = st->tx,
114         };
115
116         *rx_p = kmalloc(len + 1, GFP_KERNEL);
117         if (*rx_p == NULL) {
118                 ret = -ENOMEM;
119                 goto error_ret;
120         }
121         xfer.rx_buf = *rx_p;
122         st->tx[0] = SCA3000_READ_REG(reg_address_high);
123         spi_message_init(&msg);
124         spi_message_add_tail(&xfer, &msg);
125
126         ret = spi_sync(st->us, &msg);
127
128         if (ret) {
129                 dev_err(get_device(&st->us->dev), "problem reading register");
130                 goto error_free_rx;
131         }
132
133         return 0;
134 error_free_rx:
135         kfree(*rx_p);
136 error_ret:
137         return ret;
138
139 }
140 /**
141  * sca3000_reg_lock_on() test if the ctrl register lock is on
142  *
143  * Lock must be held.
144  **/
145 static int sca3000_reg_lock_on(struct sca3000_state *st)
146 {
147         u8 *rx;
148         int ret;
149
150         ret = sca3000_read_data(st, SCA3000_REG_ADDR_STATUS, &rx, 1);
151
152         if (ret < 0)
153                 return ret;
154         ret = !(rx[1] & SCA3000_LOCKED);
155         kfree(rx);
156
157         return ret;
158 }
159
160 /**
161  * __sca3000_unlock_reg_lock() unlock the control registers
162  *
163  * Note the device does not appear to support doing this in a single transfer.
164  * This should only ever be used as part of ctrl reg read.
165  * Lock must be held before calling this
166  **/
167 static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
168 {
169         struct spi_message msg;
170         struct spi_transfer xfer[3] = {
171                 {
172                         .bits_per_word = 8,
173                         .len = 2,
174                         .cs_change = 1,
175                         .tx_buf = st->tx,
176                 }, {
177                         .bits_per_word = 8,
178                         .len = 2,
179                         .cs_change = 1,
180                         .tx_buf = st->tx + 2,
181                 }, {
182                         .bits_per_word = 8,
183                         .len = 2,
184                         .cs_change = 1,
185                         .tx_buf = st->tx + 4,
186                 },
187         };
188         st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
189         st->tx[1] = 0x00;
190         st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
191         st->tx[3] = 0x50;
192         st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
193         st->tx[5] = 0xA0;
194         spi_message_init(&msg);
195         spi_message_add_tail(&xfer[0], &msg);
196         spi_message_add_tail(&xfer[1], &msg);
197         spi_message_add_tail(&xfer[2], &msg);
198
199         return spi_sync(st->us, &msg);
200 }
201
202 /**
203  * sca3000_write_ctrl_reg() write to a lock protect ctrl register
204  * @sel: selects which registers we wish to write to
205  * @val: the value to be written
206  *
207  * Certain control registers are protected against overwriting by the lock
208  * register and use a shared write address. This function allows writing of
209  * these registers.
210  * Lock must be held.
211  **/
212 static int sca3000_write_ctrl_reg(struct sca3000_state *st,
213                                   uint8_t sel,
214                                   uint8_t val)
215 {
216
217         int ret;
218
219         ret = sca3000_reg_lock_on(st);
220         if (ret < 0)
221                 goto error_ret;
222         if (ret) {
223                 ret = __sca3000_unlock_reg_lock(st);
224                 if (ret)
225                         goto error_ret;
226         }
227
228         /* Set the control select register */
229         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel);
230         if (ret)
231                 goto error_ret;
232
233         /* Write the actual value into the register */
234         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val);
235
236 error_ret:
237         return ret;
238 }
239
240 /* Crucial that lock is called before calling this */
241 /**
242  * sca3000_read_ctrl_reg() read from lock protected control register.
243  *
244  * Lock must be held.
245  **/
246 static int sca3000_read_ctrl_reg(struct sca3000_state *st,
247                                  u8 ctrl_reg,
248                                  u8 **rx_p)
249 {
250         int ret;
251
252         ret = sca3000_reg_lock_on(st);
253         if (ret < 0)
254                 goto error_ret;
255         if (ret) {
256                 ret = __sca3000_unlock_reg_lock(st);
257                 if (ret)
258                         goto error_ret;
259         }
260         /* Set the control select register */
261         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg);
262         if (ret)
263                 goto error_ret;
264         ret = sca3000_read_data(st, SCA3000_REG_ADDR_CTRL_DATA, rx_p, 1);
265
266 error_ret:
267         return ret;
268 }
269
270 #ifdef SCA3000_DEBUG
271 /**
272  * sca3000_check_status() check the status register
273  *
274  * Only used for debugging purposes
275  **/
276 static int sca3000_check_status(struct device *dev)
277 {
278         u8 *rx;
279         int ret;
280         struct iio_dev *indio_dev = dev_get_drvdata(dev);
281         struct sca3000_state *st = indio_dev->dev_data;
282
283         mutex_lock(&st->lock);
284         ret = sca3000_read_data(st, SCA3000_REG_ADDR_STATUS, &rx, 1);
285         if (ret < 0)
286                 goto error_ret;
287         if (rx[1] & SCA3000_EEPROM_CS_ERROR)
288                 dev_err(dev, "eeprom error \n");
289         if (rx[1] & SCA3000_SPI_FRAME_ERROR)
290                 dev_err(dev, "Previous SPI Frame was corrupt\n");
291         kfree(rx);
292
293 error_ret:
294         mutex_unlock(&st->lock);
295         return ret;
296 }
297 #endif /* SCA3000_DEBUG */
298
299 /**
300  * sca3000_read_13bit_signed() sysfs interface to read 13 bit signed registers
301  *
302  * These are described as signed 12 bit on the data sheet, which appears
303  * to be a conventional 2's complement 13 bit.
304  **/
305 static ssize_t sca3000_read_13bit_signed(struct device *dev,
306                                          struct device_attribute *attr,
307                                          char *buf)
308 {
309         int len = 0, ret;
310         int val;
311         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
312         u8 *rx;
313         struct iio_dev *indio_dev = dev_get_drvdata(dev);
314         struct sca3000_state *st = indio_dev->dev_data;
315
316         mutex_lock(&st->lock);
317         ret = sca3000_read_data(st, this_attr->address, &rx, 2);
318         if (ret < 0)
319                 goto error_ret;
320         val = sca3000_13bit_convert(rx[1], rx[2]);
321         len += sprintf(buf + len, "%d\n", val);
322         kfree(rx);
323 error_ret:
324         mutex_unlock(&st->lock);
325
326         return ret ? ret : len;
327 }
328
329
330 static ssize_t sca3000_show_name(struct device *dev,
331                                  struct device_attribute *attr,
332                                  char *buf)
333 {
334         struct iio_dev *dev_info = dev_get_drvdata(dev);
335         struct sca3000_state *st = dev_info->dev_data;
336         return sprintf(buf, "%s\n", st->info->name);
337 }
338 /**
339  * sca3000_show_reg() - sysfs interface to read the chip revision number
340  **/
341 static ssize_t sca3000_show_rev(struct device *dev,
342                                 struct device_attribute *attr,
343                                 char *buf)
344 {
345         int len = 0, ret;
346         struct iio_dev *dev_info = dev_get_drvdata(dev);
347         struct sca3000_state *st = dev_info->dev_data;
348
349         u8 *rx;
350
351         mutex_lock(&st->lock);
352         ret = sca3000_read_data(st, SCA3000_REG_ADDR_REVID, &rx, 1);
353         if (ret < 0)
354                 goto error_ret;
355         len += sprintf(buf + len,
356                        "major=%d, minor=%d\n",
357                        rx[1] & SCA3000_REVID_MAJOR_MASK,
358                        rx[1] & SCA3000_REVID_MINOR_MASK);
359         kfree(rx);
360
361 error_ret:
362         mutex_unlock(&st->lock);
363
364         return ret ? ret : len;
365 }
366
367 /**
368  * sca3000_show_available_measurement_modes() display available modes
369  *
370  * This is all read from chip specific data in the driver. Not all
371  * of the sca3000 series support modes other than normal.
372  **/
373 static ssize_t
374 sca3000_show_available_measurement_modes(struct device *dev,
375                                          struct device_attribute *attr,
376                                          char *buf)
377 {
378         struct iio_dev *dev_info = dev_get_drvdata(dev);
379         struct sca3000_state *st = dev_info->dev_data;
380         int len = 0;
381
382         len += sprintf(buf + len, "0 - normal mode");
383         switch (st->info->option_mode_1) {
384         case SCA3000_OP_MODE_NARROW:
385                 len += sprintf(buf + len, ", 1 - narrow mode");
386                 break;
387         case SCA3000_OP_MODE_BYPASS:
388                 len += sprintf(buf + len, ", 1 - bypass mode");
389                 break;
390         };
391         switch (st->info->option_mode_2) {
392         case SCA3000_OP_MODE_WIDE:
393                 len += sprintf(buf + len, ", 2 - wide mode");
394                 break;
395         }
396         /* always supported */
397         len += sprintf(buf + len, " 3 - motion detection \n");
398
399         return len;
400 }
401
402 /**
403  * sca3000_show_measurmenet_mode() sysfs read of current mode
404  **/
405 static ssize_t
406 sca3000_show_measurement_mode(struct device *dev,
407                               struct device_attribute *attr,
408                               char *buf)
409 {
410         struct iio_dev *dev_info = dev_get_drvdata(dev);
411         struct sca3000_state *st = dev_info->dev_data;
412         int len = 0, ret;
413         u8 *rx;
414
415         mutex_lock(&st->lock);
416         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
417         if (ret)
418                 goto error_ret;
419         /* mask bottom 2 bits - only ones that are relevant */
420         rx[1] &= 0x03;
421         switch (rx[1]) {
422         case SCA3000_MEAS_MODE_NORMAL:
423                 len += sprintf(buf + len, "0 - normal mode\n");
424                 break;
425         case SCA3000_MEAS_MODE_MOT_DET:
426                 len += sprintf(buf + len, "3 - motion detection\n");
427                 break;
428         case SCA3000_MEAS_MODE_OP_1:
429                 switch (st->info->option_mode_1) {
430                 case SCA3000_OP_MODE_NARROW:
431                         len += sprintf(buf + len, "1 - narrow mode\n");
432                         break;
433                 case SCA3000_OP_MODE_BYPASS:
434                         len += sprintf(buf + len, "1 - bypass mode\n");
435                         break;
436                 };
437                 break;
438         case SCA3000_MEAS_MODE_OP_2:
439                 switch (st->info->option_mode_2) {
440                 case SCA3000_OP_MODE_WIDE:
441                         len += sprintf(buf + len, "2 - wide mode\n");
442                         break;
443                 }
444                 break;
445         };
446
447 error_ret:
448         mutex_unlock(&st->lock);
449
450         return ret ? ret : len;
451 }
452
453 /**
454  * sca3000_store_measurement_mode() set the current mode
455  **/
456 static ssize_t
457 sca3000_store_measurement_mode(struct device *dev,
458                                struct device_attribute *attr,
459                                const char *buf,
460                                size_t len)
461 {
462         struct iio_dev *dev_info = dev_get_drvdata(dev);
463         struct sca3000_state *st = dev_info->dev_data;
464         int ret;
465         u8 *rx;
466         int mask = 0x03;
467         long val;
468
469         mutex_lock(&st->lock);
470         ret = strict_strtol(buf, 10, &val);
471         if (ret)
472                 goto error_ret;
473         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
474         if (ret)
475                 goto error_ret;
476         rx[1] &= ~mask;
477         rx[1] |= (val & mask);
478         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, rx[1]);
479         if (ret)
480                 goto error_free_rx;
481         mutex_unlock(&st->lock);
482
483         return len;
484
485 error_free_rx:
486         kfree(rx);
487 error_ret:
488         mutex_unlock(&st->lock);
489
490         return ret;
491 }
492
493
494 /* Not even vaguely standard attributes so defined here rather than
495  * in the relevant IIO core headers
496  */
497 static IIO_DEVICE_ATTR(available_measurement_modes, S_IRUGO,
498                        sca3000_show_available_measurement_modes,
499                        NULL, 0);
500
501 static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
502                        sca3000_show_measurement_mode,
503                        sca3000_store_measurement_mode,
504                        0);
505
506 /* More standard attributes */
507
508 static IIO_DEV_ATTR_NAME(sca3000_show_name);
509 static IIO_DEV_ATTR_REV(sca3000_show_rev);
510
511 static IIO_DEV_ATTR_ACCEL_X(sca3000_read_13bit_signed,
512                             SCA3000_REG_ADDR_X_MSB);
513 static IIO_DEV_ATTR_ACCEL_Y(sca3000_read_13bit_signed,
514                             SCA3000_REG_ADDR_Y_MSB);
515 static IIO_DEV_ATTR_ACCEL_Z(sca3000_read_13bit_signed,
516                             SCA3000_REG_ADDR_Z_MSB);
517
518
519 /**
520  * sca3000_read_av_freq() sysfs function to get available frequencies
521  *
522  * The later modes are only relevant to the ring buffer - and depend on current
523  * mode. Note that data sheet gives rather wide tolerances for these so integer
524  * division will give good enough answer and not all chips have them specified
525  * at all.
526  **/
527 static ssize_t sca3000_read_av_freq(struct device *dev,
528                              struct device_attribute *attr,
529                              char *buf)
530 {
531         struct iio_dev *indio_dev = dev_get_drvdata(dev);
532         struct sca3000_state *st = indio_dev->dev_data;
533         int len = 0, ret;
534         u8 *rx;
535         mutex_lock(&st->lock);
536         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
537         mutex_unlock(&st->lock);
538         if (ret)
539                 goto error_ret;
540         rx[1] &= 0x03;
541         switch (rx[1]) {
542         case SCA3000_MEAS_MODE_NORMAL:
543                 len += sprintf(buf + len, "%d %d %d\n",
544                                st->info->measurement_mode_freq,
545                                st->info->measurement_mode_freq/2,
546                                st->info->measurement_mode_freq/4);
547                 break;
548         case SCA3000_MEAS_MODE_OP_1:
549                 len += sprintf(buf + len, "%d %d %d\n",
550                                st->info->option_mode_1_freq,
551                                st->info->option_mode_1_freq/2,
552                                st->info->option_mode_1_freq/4);
553                 break;
554         case SCA3000_MEAS_MODE_OP_2:
555                 len += sprintf(buf + len, "%d %d %d\n",
556                                st->info->option_mode_2_freq,
557                                st->info->option_mode_2_freq/2,
558                                st->info->option_mode_2_freq/4);
559                 break;
560         };
561         kfree(rx);
562         return len;
563 error_ret:
564         return ret;
565 }
566 /**
567  * __sca3000_get_base_frequency() obtain mode specific base frequency
568  *
569  * lock must be held
570  **/
571 static inline int __sca3000_get_base_freq(struct sca3000_state *st,
572                                           const struct sca3000_chip_info *info,
573                                           int *base_freq)
574 {
575         int ret;
576         u8 *rx;
577
578         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
579         if (ret)
580                 goto error_ret;
581         switch (0x03 & rx[1]) {
582         case SCA3000_MEAS_MODE_NORMAL:
583                 *base_freq = info->measurement_mode_freq;
584                 break;
585         case SCA3000_MEAS_MODE_OP_1:
586                 *base_freq = info->option_mode_1_freq;
587                 break;
588         case SCA3000_MEAS_MODE_OP_2:
589                 *base_freq = info->option_mode_2_freq;
590                 break;
591         };
592         kfree(rx);
593 error_ret:
594         return ret;
595 }
596
597 /**
598  * sca3000_read_frequency() sysfs interface to get the current frequency
599  **/
600 static ssize_t sca3000_read_frequency(struct device *dev,
601                                struct device_attribute *attr,
602                                char *buf)
603 {
604         struct iio_dev *indio_dev = dev_get_drvdata(dev);
605         struct sca3000_state *st = indio_dev->dev_data;
606         int ret, len = 0, base_freq = 0;
607         u8 *rx;
608         mutex_lock(&st->lock);
609         ret = __sca3000_get_base_freq(st, st->info, &base_freq);
610         if (ret)
611                 goto error_ret_mut;
612         ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, &rx);
613         mutex_unlock(&st->lock);
614         if (ret)
615                 goto error_ret;
616         if (base_freq > 0)
617                 switch (rx[1]&0x03) {
618                 case 0x00:
619                 case 0x03:
620                         len = sprintf(buf, "%d\n", base_freq);
621                         break;
622                 case 0x01:
623                         len = sprintf(buf, "%d\n", base_freq/2);
624                         break;
625                 case 0x02:
626                         len = sprintf(buf, "%d\n", base_freq/4);
627                         break;
628         };
629                         kfree(rx);
630         return len;
631 error_ret_mut:
632         mutex_unlock(&st->lock);
633 error_ret:
634         return ret;
635 }
636
637 /**
638  * sca3000_set_frequency() sysfs interface to set the current frequency
639  **/
640 static ssize_t sca3000_set_frequency(struct device *dev,
641                               struct device_attribute *attr,
642                               const char *buf,
643                               size_t len)
644 {
645         struct iio_dev *indio_dev = dev_get_drvdata(dev);
646         struct sca3000_state *st = indio_dev->dev_data;
647         int ret, base_freq = 0;
648         u8 *rx;
649         long val;
650
651         ret = strict_strtol(buf, 10, &val);
652         if (ret)
653                 return ret;
654
655         mutex_lock(&st->lock);
656         /* What mode are we in? */
657         ret = __sca3000_get_base_freq(st, st->info, &base_freq);
658         if (ret)
659                 goto error_free_lock;
660
661         ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, &rx);
662         if (ret)
663                 goto error_free_lock;
664         /* clear the bits */
665         rx[1] &= ~0x03;
666
667         if (val == base_freq/2) {
668                 rx[1] |= SCA3000_OUT_CTRL_BUF_DIV_2;
669         } else if (val == base_freq/4) {
670                 rx[1] |= SCA3000_OUT_CTRL_BUF_DIV_4;
671         } else if (val != base_freq) {
672                 ret = -EINVAL;
673                 goto error_free_lock;
674         }
675         ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL, rx[1]);
676 error_free_lock:
677         mutex_unlock(&st->lock);
678
679         return ret ? ret : len;
680 }
681
682 /* Should only really be registered if ring buffer support is compiled in.
683  * Does no harm however and doing it right would add a fair bit of complexity
684  */
685 static IIO_DEV_ATTR_AVAIL_SAMP_FREQ(sca3000_read_av_freq);
686
687 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
688                               sca3000_read_frequency,
689                               sca3000_set_frequency);
690
691
692 /**
693  * sca3000_read_temp() sysfs interface to get the temperature when available
694  *
695 * The alignment of data in here is downright odd. See data sheet.
696 * Converting this into a meaningful value is left to inline functions in
697 * userspace part of header.
698 **/
699 static ssize_t sca3000_read_temp(struct device *dev,
700                                  struct device_attribute *attr,
701                                  char *buf)
702 {
703         struct iio_dev *indio_dev = dev_get_drvdata(dev);
704         struct sca3000_state *st = indio_dev->dev_data;
705         int len = 0, ret;
706         int val;
707         u8 *rx;
708         ret = sca3000_read_data(st, SCA3000_REG_ADDR_TEMP_MSB, &rx, 2);
709         if (ret < 0)
710                 goto error_ret;
711         val = ((rx[1]&0x3F) << 3) | ((rx[2] & 0xE0) >> 5);
712         len += sprintf(buf + len, "%d\n", val);
713         kfree(rx);
714
715         return len;
716
717 error_ret:
718         return ret;
719 }
720 static IIO_DEV_ATTR_TEMP(sca3000_read_temp);
721
722 /**
723  * sca3000_show_thresh() sysfs query of a theshold
724  **/
725 static ssize_t sca3000_show_thresh(struct device *dev,
726                                    struct device_attribute *attr,
727                                    char *buf)
728 {
729         struct iio_dev *indio_dev = dev_get_drvdata(dev);
730         struct sca3000_state *st = indio_dev->dev_data;
731         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
732         int len = 0, ret;
733         u8 *rx;
734
735         mutex_lock(&st->lock);
736         ret = sca3000_read_ctrl_reg(st,
737                                     this_attr->address,
738                                     &rx);
739         mutex_unlock(&st->lock);
740         if (ret)
741                 return ret;
742         len += sprintf(buf + len, "%d\n", rx[1]);
743         kfree(rx);
744
745         return len;
746 }
747
748 /**
749  * sca3000_write_thresh() sysfs control of threshold
750  **/
751 static ssize_t sca3000_write_thresh(struct device *dev,
752                                     struct device_attribute *attr,
753                                     const char *buf,
754                                     size_t len)
755 {
756         struct iio_dev *indio_dev = dev_get_drvdata(dev);
757         struct sca3000_state *st = indio_dev->dev_data;
758         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
759         int ret;
760         long val;
761
762         ret = strict_strtol(buf, 10, &val);
763         if (ret)
764                 return ret;
765         mutex_lock(&st->lock);
766         ret = sca3000_write_ctrl_reg(st, this_attr->address, val);
767         mutex_unlock(&st->lock);
768
769         return ret ? ret : len;
770 }
771
772 static IIO_DEV_ATTR_ACCEL_THRESH_X(S_IRUGO | S_IWUSR,
773                                    sca3000_show_thresh,
774                                    sca3000_write_thresh,
775                                    SCA3000_REG_CTRL_SEL_MD_X_TH);
776 static IIO_DEV_ATTR_ACCEL_THRESH_Y(S_IRUGO | S_IWUSR,
777                                    sca3000_show_thresh,
778                                    sca3000_write_thresh,
779                                    SCA3000_REG_CTRL_SEL_MD_Y_TH);
780 static IIO_DEV_ATTR_ACCEL_THRESH_Z(S_IRUGO | S_IWUSR,
781                                    sca3000_show_thresh,
782                                    sca3000_write_thresh,
783                                    SCA3000_REG_CTRL_SEL_MD_Z_TH);
784
785 static struct attribute *sca3000_attributes[] = {
786         &iio_dev_attr_name.dev_attr.attr,
787         &iio_dev_attr_revision.dev_attr.attr,
788         &iio_dev_attr_accel_x.dev_attr.attr,
789         &iio_dev_attr_accel_y.dev_attr.attr,
790         &iio_dev_attr_accel_z.dev_attr.attr,
791         &iio_dev_attr_thresh_accel_x.dev_attr.attr,
792         &iio_dev_attr_thresh_accel_y.dev_attr.attr,
793         &iio_dev_attr_thresh_accel_z.dev_attr.attr,
794         &iio_dev_attr_available_measurement_modes.dev_attr.attr,
795         &iio_dev_attr_measurement_mode.dev_attr.attr,
796         &iio_dev_attr_available_sampling_frequency.dev_attr.attr,
797         &iio_dev_attr_sampling_frequency.dev_attr.attr,
798         NULL,
799 };
800
801 static struct attribute *sca3000_attributes_with_temp[] = {
802         &iio_dev_attr_name.dev_attr.attr,
803         &iio_dev_attr_revision.dev_attr.attr,
804         &iio_dev_attr_accel_x.dev_attr.attr,
805         &iio_dev_attr_accel_y.dev_attr.attr,
806         &iio_dev_attr_accel_z.dev_attr.attr,
807         &iio_dev_attr_thresh_accel_x.dev_attr.attr,
808         &iio_dev_attr_thresh_accel_y.dev_attr.attr,
809         &iio_dev_attr_thresh_accel_z.dev_attr.attr,
810         &iio_dev_attr_available_measurement_modes.dev_attr.attr,
811         &iio_dev_attr_measurement_mode.dev_attr.attr,
812         &iio_dev_attr_available_sampling_frequency.dev_attr.attr,
813         &iio_dev_attr_sampling_frequency.dev_attr.attr,
814         /* Only present if temp sensor is */
815         &iio_dev_attr_temp.dev_attr.attr,
816         NULL,
817 };
818
819 static const struct attribute_group sca3000_attribute_group = {
820         .attrs = sca3000_attributes,
821 };
822
823 static const struct attribute_group sca3000_attribute_group_with_temp = {
824         .attrs = sca3000_attributes_with_temp,
825 };
826
827 /* RING RELATED interrupt handler */
828 /* depending on event, push to the ring buffer event chrdev or the event one */
829
830 /**
831  * sca3000_interrupt_handler_bh() - handling ring and non ring events
832  *
833  * This function is complicated by the fact that the devices can signify ring
834  * and non ring events via the same interrupt line and they can only
835  * be distinguished via a read of the relevant status register.
836  **/
837 static void sca3000_interrupt_handler_bh(struct work_struct *work_s)
838 {
839         struct sca3000_state *st
840                 = container_of(work_s, struct sca3000_state,
841                                interrupt_handler_ws);
842         u8 *rx;
843         int ret;
844
845         /* Could lead if badly timed to an extra read of status reg,
846          * but ensures no interrupt is missed.
847          */
848         enable_irq(st->us->irq);
849         mutex_lock(&st->lock);
850         ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_STATUS,
851                                 &rx, 1);
852         mutex_unlock(&st->lock);
853         if (ret)
854                 goto done;
855
856         sca3000_ring_int_process(rx[1], st->indio_dev->ring);
857
858         if (rx[1] & SCA3000_INT_STATUS_FREE_FALL)
859                 iio_push_event(st->indio_dev, 0,
860                                IIO_EVENT_CODE_FREE_FALL,
861                                st->last_timestamp);
862
863         if (rx[1] & SCA3000_INT_STATUS_Y_TRIGGER)
864                 iio_push_event(st->indio_dev, 0,
865                                IIO_EVENT_CODE_ACCEL_Y_HIGH,
866                                st->last_timestamp);
867
868         if (rx[1] & SCA3000_INT_STATUS_X_TRIGGER)
869                 iio_push_event(st->indio_dev, 0,
870                                IIO_EVENT_CODE_ACCEL_X_HIGH,
871                                st->last_timestamp);
872
873         if (rx[1] & SCA3000_INT_STATUS_Z_TRIGGER)
874                 iio_push_event(st->indio_dev, 0,
875                                IIO_EVENT_CODE_ACCEL_Z_HIGH,
876                                st->last_timestamp);
877
878 done:
879         kfree(rx);
880         return;
881 }
882
883 /**
884  * sca3000_handler_th() handles all interrupt events from device
885  *
886  * These devices deploy unified interrupt status registers meaning
887  * all interrupts must be handled together
888  **/
889 static int sca3000_handler_th(struct iio_dev *dev_info,
890                               int index,
891                               s64 timestamp,
892                               int no_test)
893 {
894         struct sca3000_state *st = dev_info->dev_data;
895
896         st->last_timestamp = timestamp;
897         schedule_work(&st->interrupt_handler_ws);
898
899         return 0;
900 }
901
902 /**
903  * sca3000_query_mo_det() is motion detection enabled for this axis
904  *
905  * First queries if motion detection is enabled and then if this axis is
906  * on.
907  **/
908 static ssize_t sca3000_query_mo_det(struct device *dev,
909                                     struct device_attribute *attr,
910                                     char *buf)
911 {
912         struct iio_dev *indio_dev = dev_get_drvdata(dev);
913         struct sca3000_state *st = indio_dev->dev_data;
914         struct iio_event_attr *this_attr = to_iio_event_attr(attr);
915         int ret, len = 0;
916         u8 *rx;
917         u8 protect_mask = 0x03;
918
919         /* read current value of mode register */
920         mutex_lock(&st->lock);
921         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
922         if (ret)
923                 goto error_ret;
924
925         if ((rx[1]&protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
926                 len += sprintf(buf + len, "0\n");
927         else {
928                 kfree(rx);
929                 ret = sca3000_read_ctrl_reg(st,
930                                             SCA3000_REG_CTRL_SEL_MD_CTRL,
931                                             &rx);
932                 if (ret)
933                         goto error_ret;
934                 /* only supporting logical or's for now */
935                 len += sprintf(buf + len, "%d\n",
936                                (rx[1] & this_attr->mask) ? 1 : 0);
937         }
938         kfree(rx);
939 error_ret:
940         mutex_unlock(&st->lock);
941
942         return ret ? ret : len;
943 }
944 /**
945  * sca3000_query_free_fall_mode() is free fall mode enabled
946  **/
947 static ssize_t sca3000_query_free_fall_mode(struct device *dev,
948                                             struct device_attribute *attr,
949                                             char *buf)
950 {
951         int ret, len;
952         u8 *rx;
953         struct iio_dev *indio_dev = dev_get_drvdata(dev);
954         struct sca3000_state *st = indio_dev->dev_data;
955
956         mutex_lock(&st->lock);
957         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
958         mutex_unlock(&st->lock);
959         if (ret)
960                 return ret;
961         len = sprintf(buf, "%d\n",
962                       !!(rx[1] & SCA3000_FREE_FALL_DETECT));
963         kfree(rx);
964
965         return len;
966 }
967 /**
968  * sca3000_query_ring_int() is the hardware ring status interrupt enabled
969  **/
970 static ssize_t sca3000_query_ring_int(struct device *dev,
971                                       struct device_attribute *attr,
972                                       char *buf)
973 {
974         struct iio_event_attr *this_attr = to_iio_event_attr(attr);
975         int ret, len;
976         u8 *rx;
977         struct iio_dev *indio_dev = dev_get_drvdata(dev);
978         struct sca3000_state *st = indio_dev->dev_data;
979         mutex_lock(&st->lock);
980         ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
981         mutex_unlock(&st->lock);
982         if (ret)
983                 return ret;
984         len = sprintf(buf, "%d\n", (rx[1] & this_attr->mask) ? 1 : 0);
985         kfree(rx);
986
987         return len;
988 }
989 /**
990  * sca3000_set_ring_int() set state of ring status interrupt
991  **/
992 static ssize_t sca3000_set_ring_int(struct device *dev,
993                                       struct device_attribute *attr,
994                                       const char *buf,
995                                       size_t len)
996 {
997         struct iio_dev *indio_dev = dev_get_drvdata(dev);
998         struct sca3000_state *st = indio_dev->dev_data;
999         struct iio_event_attr *this_attr = to_iio_event_attr(attr);
1000
1001         long val;
1002         int ret;
1003         u8 *rx;
1004
1005         mutex_lock(&st->lock);
1006         ret = strict_strtol(buf, 10, &val);
1007         if (ret)
1008                 goto error_ret;
1009         ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
1010         if (ret)
1011                 goto error_ret;
1012         if (val)
1013                 ret = sca3000_write_reg(st,
1014                                         SCA3000_REG_ADDR_INT_MASK,
1015                                         rx[1] | this_attr->mask);
1016         else
1017                 ret = sca3000_write_reg(st,
1018                                         SCA3000_REG_ADDR_INT_MASK,
1019                                         rx[1] & ~this_attr->mask);
1020         kfree(rx);
1021 error_ret:
1022         mutex_unlock(&st->lock);
1023
1024         return ret ? ret : len;
1025 }
1026
1027 /**
1028  * sca3000_set_free_fall_mode() simple on off control for free fall int
1029  *
1030  * In these chips the free fall detector should send an interrupt if
1031  * the device falls more than 25cm.  This has not been tested due
1032  * to fragile wiring.
1033  **/
1034
1035 static ssize_t sca3000_set_free_fall_mode(struct device *dev,
1036                                           struct device_attribute *attr,
1037                                           const char *buf,
1038                                           size_t len)
1039 {
1040         struct iio_dev *indio_dev = dev_get_drvdata(dev);
1041         struct sca3000_state *st = indio_dev->dev_data;
1042         long val;
1043         int ret;
1044         u8 *rx;
1045         u8 protect_mask = SCA3000_FREE_FALL_DETECT;
1046
1047         mutex_lock(&st->lock);
1048         ret = strict_strtol(buf, 10, &val);
1049         if (ret)
1050                 goto error_ret;
1051
1052         /* read current value of mode register */
1053         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
1054         if (ret)
1055                 goto error_ret;
1056
1057         /*if off and should be on*/
1058         if (val && !(rx[1] & protect_mask))
1059                 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1060                                         (rx[1] | SCA3000_FREE_FALL_DETECT));
1061         /* if on and should be off */
1062         else if (!val && (rx[1]&protect_mask))
1063                 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1064                                         (rx[1] & ~protect_mask));
1065
1066         kfree(rx);
1067 error_ret:
1068         mutex_unlock(&st->lock);
1069
1070         return ret ? ret : len;
1071 }
1072
1073 /**
1074  * sca3000_set_mo_det() simple on off control for motion detector
1075  *
1076  * This is a per axis control, but enabling any will result in the
1077  * motion detector unit being enabled.
1078  * N.B. enabling motion detector stops normal data acquisition.
1079  * There is a complexity in knowing which mode to return to when
1080  * this mode is disabled.  Currently normal mode is assumed.
1081  **/
1082 static ssize_t sca3000_set_mo_det(struct device *dev,
1083                                   struct device_attribute *attr,
1084                                   const char *buf,
1085                                   size_t len)
1086 {
1087         struct iio_dev *indio_dev = dev_get_drvdata(dev);
1088         struct sca3000_state *st = indio_dev->dev_data;
1089         struct iio_event_attr *this_attr = to_iio_event_attr(attr);
1090         long val;
1091         int ret;
1092         u8 *rx;
1093         u8 protect_mask = 0x03;
1094         ret = strict_strtol(buf, 10, &val);
1095         if (ret)
1096                 return ret;
1097
1098         mutex_lock(&st->lock);
1099         /* First read the motion detector config to find out if
1100          * this axis is on*/
1101         ret = sca3000_read_ctrl_reg(st,
1102                                     SCA3000_REG_CTRL_SEL_MD_CTRL,
1103                                     &rx);
1104         if (ret)
1105                 goto exit_point;
1106         /* Off and should be on */
1107         if (val && !(rx[1] & this_attr->mask)) {
1108                 ret = sca3000_write_ctrl_reg(st,
1109                                              SCA3000_REG_CTRL_SEL_MD_CTRL,
1110                                              rx[1] | this_attr->mask);
1111                 if (ret)
1112                         goto exit_point_free_rx;
1113                 st->mo_det_use_count++;
1114         } else if (!val && (rx[1]&this_attr->mask)) {
1115                 ret = sca3000_write_ctrl_reg(st,
1116                                              SCA3000_REG_CTRL_SEL_MD_CTRL,
1117                                              rx[1] & ~(this_attr->mask));
1118                 if (ret)
1119                         goto exit_point_free_rx;
1120                 st->mo_det_use_count--;
1121         } else /* relies on clean state for device on boot */
1122                 goto exit_point_free_rx;
1123         kfree(rx);
1124         /* read current value of mode register */
1125         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
1126         if (ret)
1127                 goto exit_point;
1128         /*if off and should be on*/
1129         if ((st->mo_det_use_count)
1130             && ((rx[1]&protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
1131                 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1132                                         (rx[1] & ~protect_mask)
1133                                         | SCA3000_MEAS_MODE_MOT_DET);
1134         /* if on and should be off */
1135         else if (!(st->mo_det_use_count)
1136                  && ((rx[1]&protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
1137                 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1138                                         (rx[1] & ~protect_mask));
1139 exit_point_free_rx:
1140         kfree(rx);
1141 exit_point:
1142         mutex_unlock(&st->lock);
1143
1144         return ret ? ret : len;
1145 }
1146
1147 /* Shared event handler for all events as single event status register */
1148 IIO_EVENT_SH(all, &sca3000_handler_th);
1149
1150 /* Free fall detector related event attribute */
1151 IIO_EVENT_ATTR_FREE_FALL_DETECT_SH(iio_event_all,
1152                                    sca3000_query_free_fall_mode,
1153                                    sca3000_set_free_fall_mode,
1154                                    0)
1155
1156 /* Motion detector related event attributes */
1157 IIO_EVENT_ATTR_ACCEL_X_HIGH_SH(iio_event_all,
1158                                sca3000_query_mo_det,
1159                                sca3000_set_mo_det,
1160                                SCA3000_MD_CTRL_OR_X);
1161
1162 IIO_EVENT_ATTR_ACCEL_Y_HIGH_SH(iio_event_all,
1163                                sca3000_query_mo_det,
1164                                sca3000_set_mo_det,
1165                                SCA3000_MD_CTRL_OR_Y);
1166
1167 IIO_EVENT_ATTR_ACCEL_Z_HIGH_SH(iio_event_all,
1168                                sca3000_query_mo_det,
1169                                sca3000_set_mo_det,
1170                                SCA3000_MD_CTRL_OR_Z);
1171
1172 /* Hardware ring buffer related event attributes */
1173 IIO_EVENT_ATTR_RING_50_FULL_SH(iio_event_all,
1174                                sca3000_query_ring_int,
1175                                sca3000_set_ring_int,
1176                                SCA3000_INT_MASK_RING_HALF);
1177
1178 IIO_EVENT_ATTR_RING_75_FULL_SH(iio_event_all,
1179                                sca3000_query_ring_int,
1180                                sca3000_set_ring_int,
1181                                SCA3000_INT_MASK_RING_THREE_QUARTER);
1182
1183 static struct attribute *sca3000_event_attributes[] = {
1184         &iio_event_attr_free_fall.dev_attr.attr,
1185         &iio_event_attr_accel_x_high.dev_attr.attr,
1186         &iio_event_attr_accel_y_high.dev_attr.attr,
1187         &iio_event_attr_accel_z_high.dev_attr.attr,
1188         &iio_event_attr_ring_50_full.dev_attr.attr,
1189         &iio_event_attr_ring_75_full.dev_attr.attr,
1190         NULL,
1191 };
1192
1193 static struct attribute_group sca3000_event_attribute_group = {
1194         .attrs = sca3000_event_attributes,
1195 };
1196
1197 /**
1198  * sca3000_clean_setup() get the device into a predictable state
1199  *
1200  * Devices use flash memory to store many of the register values
1201  * and hence can come up in somewhat unpredictable states.
1202  * Hence reset everything on driver load.
1203   **/
1204 static int sca3000_clean_setup(struct sca3000_state *st)
1205 {
1206         int ret;
1207         u8 *rx;
1208
1209         mutex_lock(&st->lock);
1210         /* Ensure all interrupts have been acknowledged */
1211         ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_STATUS, &rx, 1);
1212         if (ret)
1213                 goto error_ret;
1214         kfree(rx);
1215
1216         /* Turn off all motion detection channels */
1217         ret = sca3000_read_ctrl_reg(st,
1218                                     SCA3000_REG_CTRL_SEL_MD_CTRL,
1219                                     &rx);
1220         if (ret)
1221                 goto error_ret;
1222         ret = sca3000_write_ctrl_reg(st,
1223                                      SCA3000_REG_CTRL_SEL_MD_CTRL,
1224                                      rx[1] & SCA3000_MD_CTRL_PROT_MASK);
1225         kfree(rx);
1226         if (ret)
1227                 goto error_ret;
1228
1229         /* Disable ring buffer */
1230         sca3000_read_ctrl_reg(st,
1231                               SCA3000_REG_CTRL_SEL_OUT_CTRL,
1232                               &rx);
1233         /* Frequency of ring buffer sampling deliberately restricted to make
1234          * debugging easier - add control of this later */
1235         ret = sca3000_write_ctrl_reg(st,
1236                                      SCA3000_REG_CTRL_SEL_OUT_CTRL,
1237                                      (rx[1] & SCA3000_OUT_CTRL_PROT_MASK)
1238                                      | SCA3000_OUT_CTRL_BUF_X_EN
1239                                      | SCA3000_OUT_CTRL_BUF_Y_EN
1240                                      | SCA3000_OUT_CTRL_BUF_Z_EN
1241                                      | SCA3000_OUT_CTRL_BUF_DIV_4);
1242         kfree(rx);
1243
1244         if (ret)
1245                 goto error_ret;
1246         /* Enable interrupts, relevant to mode and set up as active low */
1247         ret = sca3000_read_data(st,
1248                           SCA3000_REG_ADDR_INT_MASK,
1249                           &rx, 1);
1250         if (ret)
1251                 goto error_ret;
1252         ret = sca3000_write_reg(st,
1253                                 SCA3000_REG_ADDR_INT_MASK,
1254                                 (rx[1] & SCA3000_INT_MASK_PROT_MASK)
1255                                 | SCA3000_INT_MASK_ACTIVE_LOW);
1256         kfree(rx);
1257         if (ret)
1258                 goto error_ret;
1259         /* Select normal measurement mode, free fall off, ring off */
1260         /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1261          * as that occurs in one of the example on the datasheet */
1262         ret = sca3000_read_data(st,
1263                           SCA3000_REG_ADDR_MODE,
1264                           &rx, 1);
1265         if (ret)
1266                 goto error_ret;
1267         ret = sca3000_write_reg(st,
1268                                 SCA3000_REG_ADDR_MODE,
1269                                 (rx[1] & SCA3000_MODE_PROT_MASK));
1270         kfree(rx);
1271         st->bpse = 11;
1272
1273 error_ret:
1274         mutex_unlock(&st->lock);
1275         return ret;
1276 }
1277
1278 static int __devinit __sca3000_probe(struct spi_device *spi,
1279                                      enum sca3000_variant variant)
1280 {
1281         int ret, regdone = 0;
1282         struct sca3000_state *st;
1283
1284         st = kzalloc(sizeof(struct sca3000_state), GFP_KERNEL);
1285         if (st == NULL) {
1286                 ret = -ENOMEM;
1287                 goto error_ret;
1288         }
1289         spi_set_drvdata(spi, st);
1290
1291         st->tx = kmalloc(sizeof(*st->tx)*6, GFP_KERNEL);
1292         if (st->tx == NULL) {
1293                 ret = -ENOMEM;
1294                 goto error_clear_st;
1295         }
1296         st->rx = kmalloc(sizeof(*st->rx)*3, GFP_KERNEL);
1297         if (st->rx == NULL) {
1298                 ret = -ENOMEM;
1299                 goto error_free_tx;
1300         }
1301         st->us = spi;
1302         mutex_init(&st->lock);
1303         st->info = &sca3000_spi_chip_info_tbl[variant];
1304
1305         st->indio_dev = iio_allocate_device();
1306         if (st->indio_dev == NULL) {
1307                 ret = -ENOMEM;
1308                 goto error_free_rx;
1309         }
1310
1311         st->indio_dev->dev.parent = &spi->dev;
1312         st->indio_dev->num_interrupt_lines = 1;
1313         st->indio_dev->event_attrs = &sca3000_event_attribute_group;
1314         if (st->info->temp_output)
1315                 st->indio_dev->attrs = &sca3000_attribute_group_with_temp;
1316         else
1317                 st->indio_dev->attrs = &sca3000_attribute_group;
1318         st->indio_dev->dev_data = (void *)(st);
1319         st->indio_dev->modes = INDIO_DIRECT_MODE;
1320
1321         sca3000_configure_ring(st->indio_dev);
1322
1323         ret = iio_device_register(st->indio_dev);
1324         if (ret < 0)
1325                 goto error_free_dev;
1326         regdone = 1;
1327         ret = iio_ring_buffer_register(st->indio_dev->ring);
1328         if (ret < 0)
1329                 goto error_unregister_dev;
1330         if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
1331                 INIT_WORK(&st->interrupt_handler_ws,
1332                           sca3000_interrupt_handler_bh);
1333                 ret = iio_register_interrupt_line(spi->irq,
1334                                                   st->indio_dev,
1335                                                   0,
1336                                                   IRQF_TRIGGER_FALLING,
1337                                                   "sca3000");
1338                 if (ret)
1339                         goto error_unregister_ring;
1340                 /* RFC
1341                  * Probably a common situation.  All interrupts need an ack
1342                  * and there is only one handler so the complicated list system
1343                  * is overkill.  At very least a simpler registration method
1344                  * might be worthwhile.
1345                  */
1346                 iio_add_event_to_list(iio_event_attr_accel_z_high.listel,
1347                                             &st->indio_dev
1348                                             ->interrupts[0]->ev_list);
1349         }
1350         sca3000_register_ring_funcs(st->indio_dev);
1351         ret = sca3000_clean_setup(st);
1352         if (ret)
1353                 goto error_unregister_interrupt_line;
1354         return 0;
1355
1356 error_unregister_interrupt_line:
1357         if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
1358                 iio_unregister_interrupt_line(st->indio_dev, 0);
1359 error_unregister_ring:
1360         iio_ring_buffer_unregister(st->indio_dev->ring);
1361 error_unregister_dev:
1362 error_free_dev:
1363         if (regdone)
1364                 iio_device_unregister(st->indio_dev);
1365         else
1366                 iio_free_device(st->indio_dev);
1367 error_free_rx:
1368         kfree(st->rx);
1369 error_free_tx:
1370         kfree(st->tx);
1371 error_clear_st:
1372         kfree(st);
1373 error_ret:
1374         return ret;
1375 }
1376
1377 static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1378 {
1379         int ret;
1380         u8 *rx;
1381
1382         mutex_lock(&st->lock);
1383         ret = sca3000_read_data(st, SCA3000_REG_ADDR_INT_MASK, &rx, 1);
1384         if (ret)
1385                 goto error_ret;
1386         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK,
1387                                 (rx[1] & ~(SCA3000_INT_MASK_RING_THREE_QUARTER
1388                                            | SCA3000_INT_MASK_RING_HALF
1389                                            | SCA3000_INT_MASK_ALL_INTS)));
1390 error_ret:
1391         kfree(rx);
1392         return ret;
1393
1394 }
1395
1396 static int sca3000_remove(struct spi_device *spi)
1397 {
1398         struct sca3000_state *st =  spi_get_drvdata(spi);
1399         struct iio_dev *indio_dev = st->indio_dev;
1400         int ret;
1401         /* Must ensure no interrupts can be generated after this!*/
1402         ret = sca3000_stop_all_interrupts(st);
1403         if (ret)
1404                 return ret;
1405         if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
1406                 iio_unregister_interrupt_line(indio_dev, 0);
1407         iio_ring_buffer_unregister(indio_dev->ring);
1408         sca3000_unconfigure_ring(indio_dev);
1409         iio_device_unregister(indio_dev);
1410
1411         kfree(st->tx);
1412         kfree(st->rx);
1413         kfree(st);
1414
1415         return 0;
1416 }
1417
1418 /* These macros save on an awful lot of repeated code */
1419 #define SCA3000_VARIANT_PROBE(_name)                            \
1420         static int __devinit                                    \
1421         sca3000_##_name##_probe(struct spi_device *spi)         \
1422         {                                                       \
1423                 return __sca3000_probe(spi, _name);             \
1424         }
1425
1426 #define SCA3000_VARIANT_SPI_DRIVER(_name)                       \
1427         struct spi_driver sca3000_##_name##_driver = {          \
1428                 .driver = {                                     \
1429                         .name = "sca3000_" #_name,              \
1430                         .owner = THIS_MODULE,                   \
1431                 },                                              \
1432                 .probe = sca3000_##_name##_probe,               \
1433                 .remove = __devexit_p(sca3000_remove),          \
1434         }
1435
1436 SCA3000_VARIANT_PROBE(d01);
1437 static SCA3000_VARIANT_SPI_DRIVER(d01);
1438
1439 SCA3000_VARIANT_PROBE(d03);
1440 static SCA3000_VARIANT_SPI_DRIVER(d03);
1441
1442 SCA3000_VARIANT_PROBE(e02);
1443 static SCA3000_VARIANT_SPI_DRIVER(e02);
1444
1445 SCA3000_VARIANT_PROBE(e04);
1446 static SCA3000_VARIANT_SPI_DRIVER(e04);
1447
1448 SCA3000_VARIANT_PROBE(e05);
1449 static SCA3000_VARIANT_SPI_DRIVER(e05);
1450
1451 SCA3000_VARIANT_PROBE(l01);
1452 static SCA3000_VARIANT_SPI_DRIVER(l01);
1453
1454 static __init int sca3000_init(void)
1455 {
1456         int ret;
1457
1458         ret = spi_register_driver(&sca3000_d01_driver);
1459         if (ret)
1460                 goto error_ret;
1461         ret = spi_register_driver(&sca3000_d03_driver);
1462         if (ret)
1463                 goto error_unreg_d01;
1464         ret = spi_register_driver(&sca3000_e02_driver);
1465         if (ret)
1466                 goto error_unreg_d03;
1467         ret = spi_register_driver(&sca3000_e04_driver);
1468         if (ret)
1469                 goto error_unreg_e02;
1470         ret = spi_register_driver(&sca3000_e05_driver);
1471         if (ret)
1472                 goto error_unreg_e04;
1473         ret = spi_register_driver(&sca3000_l01_driver);
1474         if (ret)
1475                 goto error_unreg_e05;
1476
1477         return 0;
1478
1479 error_unreg_e05:
1480         spi_unregister_driver(&sca3000_e05_driver);
1481 error_unreg_e04:
1482         spi_unregister_driver(&sca3000_e04_driver);
1483 error_unreg_e02:
1484         spi_unregister_driver(&sca3000_e02_driver);
1485 error_unreg_d03:
1486         spi_unregister_driver(&sca3000_d03_driver);
1487 error_unreg_d01:
1488         spi_unregister_driver(&sca3000_d01_driver);
1489 error_ret:
1490
1491         return ret;
1492 }
1493
1494 static __exit void sca3000_exit(void)
1495 {
1496         spi_unregister_driver(&sca3000_l01_driver);
1497         spi_unregister_driver(&sca3000_e05_driver);
1498         spi_unregister_driver(&sca3000_e04_driver);
1499         spi_unregister_driver(&sca3000_e02_driver);
1500         spi_unregister_driver(&sca3000_d03_driver);
1501         spi_unregister_driver(&sca3000_d01_driver);
1502 }
1503
1504 module_init(sca3000_init);
1505 module_exit(sca3000_exit);
1506
1507 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1508 MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1509 MODULE_LICENSE("GPL v2");