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