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