Merge branch 'for-3.2' of git://linux-nfs.org/~bfields/linux
[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/fs.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/kernel.h>
18 #include <linux/spi/spi.h>
19 #include <linux/sysfs.h>
20 #include <linux/module.h>
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "../buffer_generic.h"
24
25 #include "sca3000.h"
26
27 enum sca3000_variant {
28         d01,
29         e02,
30         e04,
31         e05,
32 };
33
34 /* Note where option modes are not defined, the chip simply does not
35  * support any.
36  * Other chips in the sca3000 series use i2c and are not included here.
37  *
38  * Some of these devices are only listed in the family data sheet and
39  * do not actually appear to be available.
40  */
41 static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = {
42         [d01] = {
43                 .scale = 7357,
44                 .temp_output = true,
45                 .measurement_mode_freq = 250,
46                 .option_mode_1 = SCA3000_OP_MODE_BYPASS,
47                 .option_mode_1_freq = 250,
48                 .mot_det_mult_xz = {50, 100, 200, 350, 650, 1300},
49                 .mot_det_mult_y = {50, 100, 150, 250, 450, 850, 1750},
50         },
51         [e02] = {
52                 .scale = 9810,
53                 .measurement_mode_freq = 125,
54                 .option_mode_1 = SCA3000_OP_MODE_NARROW,
55                 .option_mode_1_freq = 63,
56                 .mot_det_mult_xz = {100, 150, 300, 550, 1050, 2050},
57                 .mot_det_mult_y = {50, 100, 200, 350, 700, 1350, 2700},
58         },
59         [e04] = {
60                 .scale = 19620,
61                 .measurement_mode_freq = 100,
62                 .option_mode_1 = SCA3000_OP_MODE_NARROW,
63                 .option_mode_1_freq = 50,
64                 .option_mode_2 = SCA3000_OP_MODE_WIDE,
65                 .option_mode_2_freq = 400,
66                 .mot_det_mult_xz = {200, 300, 600, 1100, 2100, 4100},
67                 .mot_det_mult_y = {100, 200, 400, 7000, 1400, 2700, 54000},
68         },
69         [e05] = {
70                 .scale = 61313,
71                 .measurement_mode_freq = 200,
72                 .option_mode_1 = SCA3000_OP_MODE_NARROW,
73                 .option_mode_1_freq = 50,
74                 .option_mode_2 = SCA3000_OP_MODE_WIDE,
75                 .option_mode_2_freq = 400,
76                 .mot_det_mult_xz = {600, 900, 1700, 3200, 6100, 11900},
77                 .mot_det_mult_y = {300, 600, 1200, 2000, 4100, 7800, 15600},
78         },
79 };
80
81 int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
82 {
83         st->tx[0] = SCA3000_WRITE_REG(address);
84         st->tx[1] = val;
85         return spi_write(st->us, st->tx, 2);
86 }
87
88 int sca3000_read_data_short(struct sca3000_state *st,
89                             uint8_t reg_address_high,
90                             int len)
91 {
92         struct spi_message msg;
93         struct spi_transfer xfer[2] = {
94                 {
95                         .len = 1,
96                         .tx_buf = st->tx,
97                 }, {
98                         .len = len,
99                         .rx_buf = st->rx,
100                 }
101         };
102         st->tx[0] = SCA3000_READ_REG(reg_address_high);
103         spi_message_init(&msg);
104         spi_message_add_tail(&xfer[0], &msg);
105         spi_message_add_tail(&xfer[1], &msg);
106
107         return spi_sync(st->us, &msg);
108 }
109
110 /**
111  * sca3000_reg_lock_on() test if the ctrl register lock is on
112  *
113  * Lock must be held.
114  **/
115 static int sca3000_reg_lock_on(struct sca3000_state *st)
116 {
117         int ret;
118
119         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1);
120         if (ret < 0)
121                 return ret;
122
123         return !(st->rx[0] & SCA3000_LOCKED);
124 }
125
126 /**
127  * __sca3000_unlock_reg_lock() unlock the control registers
128  *
129  * Note the device does not appear to support doing this in a single transfer.
130  * This should only ever be used as part of ctrl reg read.
131  * Lock must be held before calling this
132  **/
133 static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
134 {
135         struct spi_message msg;
136         struct spi_transfer xfer[3] = {
137                 {
138                         .len = 2,
139                         .cs_change = 1,
140                         .tx_buf = st->tx,
141                 }, {
142                         .len = 2,
143                         .cs_change = 1,
144                         .tx_buf = st->tx + 2,
145                 }, {
146                         .len = 2,
147                         .tx_buf = st->tx + 4,
148                 },
149         };
150         st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
151         st->tx[1] = 0x00;
152         st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
153         st->tx[3] = 0x50;
154         st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_ADDR_UNLOCK);
155         st->tx[5] = 0xA0;
156         spi_message_init(&msg);
157         spi_message_add_tail(&xfer[0], &msg);
158         spi_message_add_tail(&xfer[1], &msg);
159         spi_message_add_tail(&xfer[2], &msg);
160
161         return spi_sync(st->us, &msg);
162 }
163
164 /**
165  * sca3000_write_ctrl_reg() write to a lock protect ctrl register
166  * @sel: selects which registers we wish to write to
167  * @val: the value to be written
168  *
169  * Certain control registers are protected against overwriting by the lock
170  * register and use a shared write address. This function allows writing of
171  * these registers.
172  * Lock must be held.
173  **/
174 static int sca3000_write_ctrl_reg(struct sca3000_state *st,
175                                   uint8_t sel,
176                                   uint8_t val)
177 {
178
179         int ret;
180
181         ret = sca3000_reg_lock_on(st);
182         if (ret < 0)
183                 goto error_ret;
184         if (ret) {
185                 ret = __sca3000_unlock_reg_lock(st);
186                 if (ret)
187                         goto error_ret;
188         }
189
190         /* Set the control select register */
191         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel);
192         if (ret)
193                 goto error_ret;
194
195         /* Write the actual value into the register */
196         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val);
197
198 error_ret:
199         return ret;
200 }
201
202 /* Crucial that lock is called before calling this */
203 /**
204  * sca3000_read_ctrl_reg() read from lock protected control register.
205  *
206  * Lock must be held.
207  **/
208 static int sca3000_read_ctrl_reg(struct sca3000_state *st,
209                                  u8 ctrl_reg)
210 {
211         int ret;
212
213         ret = sca3000_reg_lock_on(st);
214         if (ret < 0)
215                 goto error_ret;
216         if (ret) {
217                 ret = __sca3000_unlock_reg_lock(st);
218                 if (ret)
219                         goto error_ret;
220         }
221         /* Set the control select register */
222         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, ctrl_reg);
223         if (ret)
224                 goto error_ret;
225         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_CTRL_DATA, 1);
226         if (ret)
227                 goto error_ret;
228         else
229                 return st->rx[0];
230 error_ret:
231         return ret;
232 }
233
234 #ifdef SCA3000_DEBUG
235 /**
236  * sca3000_check_status() check the status register
237  *
238  * Only used for debugging purposes
239  **/
240 static int sca3000_check_status(struct device *dev)
241 {
242         int ret;
243         struct iio_dev *indio_dev = dev_get_drvdata(dev);
244         struct sca3000_state *st = iio_priv(indio_dev);
245
246         mutex_lock(&st->lock);
247         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_STATUS, 1);
248         if (ret < 0)
249                 goto error_ret;
250         if (st->rx[0] & SCA3000_EEPROM_CS_ERROR)
251                 dev_err(dev, "eeprom error\n");
252         if (st->rx[0] & SCA3000_SPI_FRAME_ERROR)
253                 dev_err(dev, "Previous SPI Frame was corrupt\n");
254
255 error_ret:
256         mutex_unlock(&st->lock);
257         return ret;
258 }
259 #endif /* SCA3000_DEBUG */
260
261
262 /**
263  * sca3000_show_reg() - sysfs interface to read the chip revision number
264  **/
265 static ssize_t sca3000_show_rev(struct device *dev,
266                                 struct device_attribute *attr,
267                                 char *buf)
268 {
269         int len = 0, ret;
270         struct iio_dev *indio_dev = dev_get_drvdata(dev);
271         struct sca3000_state *st = iio_priv(indio_dev);
272
273         mutex_lock(&st->lock);
274         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_REVID, 1);
275         if (ret < 0)
276                 goto error_ret;
277         len += sprintf(buf + len,
278                        "major=%d, minor=%d\n",
279                        st->rx[0] & SCA3000_REVID_MAJOR_MASK,
280                        st->rx[0] & SCA3000_REVID_MINOR_MASK);
281 error_ret:
282         mutex_unlock(&st->lock);
283
284         return ret ? ret : len;
285 }
286
287 /**
288  * sca3000_show_available_measurement_modes() display available modes
289  *
290  * This is all read from chip specific data in the driver. Not all
291  * of the sca3000 series support modes other than normal.
292  **/
293 static ssize_t
294 sca3000_show_available_measurement_modes(struct device *dev,
295                                          struct device_attribute *attr,
296                                          char *buf)
297 {
298         struct iio_dev *indio_dev = dev_get_drvdata(dev);
299         struct sca3000_state *st = iio_priv(indio_dev);
300         int len = 0;
301
302         len += sprintf(buf + len, "0 - normal mode");
303         switch (st->info->option_mode_1) {
304         case SCA3000_OP_MODE_NARROW:
305                 len += sprintf(buf + len, ", 1 - narrow mode");
306                 break;
307         case SCA3000_OP_MODE_BYPASS:
308                 len += sprintf(buf + len, ", 1 - bypass mode");
309                 break;
310         }
311         switch (st->info->option_mode_2) {
312         case SCA3000_OP_MODE_WIDE:
313                 len += sprintf(buf + len, ", 2 - wide mode");
314                 break;
315         }
316         /* always supported */
317         len += sprintf(buf + len, " 3 - motion detection\n");
318
319         return len;
320 }
321
322 /**
323  * sca3000_show_measurmenet_mode() sysfs read of current mode
324  **/
325 static ssize_t
326 sca3000_show_measurement_mode(struct device *dev,
327                               struct device_attribute *attr,
328                               char *buf)
329 {
330         struct iio_dev *indio_dev = dev_get_drvdata(dev);
331         struct sca3000_state *st = iio_priv(indio_dev);
332         int len = 0, ret;
333
334         mutex_lock(&st->lock);
335         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
336         if (ret)
337                 goto error_ret;
338         /* mask bottom 2 bits - only ones that are relevant */
339         st->rx[0] &= 0x03;
340         switch (st->rx[0]) {
341         case SCA3000_MEAS_MODE_NORMAL:
342                 len += sprintf(buf + len, "0 - normal mode\n");
343                 break;
344         case SCA3000_MEAS_MODE_MOT_DET:
345                 len += sprintf(buf + len, "3 - motion detection\n");
346                 break;
347         case SCA3000_MEAS_MODE_OP_1:
348                 switch (st->info->option_mode_1) {
349                 case SCA3000_OP_MODE_NARROW:
350                         len += sprintf(buf + len, "1 - narrow mode\n");
351                         break;
352                 case SCA3000_OP_MODE_BYPASS:
353                         len += sprintf(buf + len, "1 - bypass mode\n");
354                         break;
355                 }
356                 break;
357         case SCA3000_MEAS_MODE_OP_2:
358                 switch (st->info->option_mode_2) {
359                 case SCA3000_OP_MODE_WIDE:
360                         len += sprintf(buf + len, "2 - wide mode\n");
361                         break;
362                 }
363                 break;
364         }
365
366 error_ret:
367         mutex_unlock(&st->lock);
368
369         return ret ? ret : len;
370 }
371
372 /**
373  * sca3000_store_measurement_mode() set the current mode
374  **/
375 static ssize_t
376 sca3000_store_measurement_mode(struct device *dev,
377                                struct device_attribute *attr,
378                                const char *buf,
379                                size_t len)
380 {
381         struct iio_dev *indio_dev = dev_get_drvdata(dev);
382         struct sca3000_state *st = iio_priv(indio_dev);
383         int ret;
384         int mask = 0x03;
385         long val;
386
387         mutex_lock(&st->lock);
388         ret = strict_strtol(buf, 10, &val);
389         if (ret)
390                 goto error_ret;
391         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
392         if (ret)
393                 goto error_ret;
394         st->rx[0] &= ~mask;
395         st->rx[0] |= (val & mask);
396         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, st->rx[0]);
397         if (ret)
398                 goto error_ret;
399         mutex_unlock(&st->lock);
400
401         return len;
402
403 error_ret:
404         mutex_unlock(&st->lock);
405
406         return ret;
407 }
408
409
410 /* Not even vaguely standard attributes so defined here rather than
411  * in the relevant IIO core headers
412  */
413 static IIO_DEVICE_ATTR(measurement_mode_available, S_IRUGO,
414                        sca3000_show_available_measurement_modes,
415                        NULL, 0);
416
417 static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
418                        sca3000_show_measurement_mode,
419                        sca3000_store_measurement_mode,
420                        0);
421
422 /* More standard attributes */
423
424 static IIO_DEVICE_ATTR(revision, S_IRUGO, sca3000_show_rev, NULL, 0);
425
426 #define SCA3000_INFO_MASK                       \
427         (1 << IIO_CHAN_INFO_SCALE_SHARED)
428 #define SCA3000_EVENT_MASK                                      \
429         (IIO_EV_BIT(IIO_EV_TYPE_MAG, IIO_EV_DIR_RISING))
430
431 static struct iio_chan_spec sca3000_channels[] = {
432         IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X, SCA3000_INFO_MASK,
433                  0, 0, IIO_ST('s', 11, 16, 5), SCA3000_EVENT_MASK),
434         IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y, SCA3000_INFO_MASK,
435                  1, 1, IIO_ST('s', 11, 16, 5), SCA3000_EVENT_MASK),
436         IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Z, SCA3000_INFO_MASK,
437                  2, 2, IIO_ST('s', 11, 16, 5), SCA3000_EVENT_MASK),
438 };
439
440 static u8 sca3000_addresses[3][3] = {
441         [0] = {SCA3000_REG_ADDR_X_MSB, SCA3000_REG_CTRL_SEL_MD_X_TH,
442                SCA3000_MD_CTRL_OR_X},
443         [1] = {SCA3000_REG_ADDR_Y_MSB, SCA3000_REG_CTRL_SEL_MD_Y_TH,
444                SCA3000_MD_CTRL_OR_Y},
445         [2] = {SCA3000_REG_ADDR_Z_MSB, SCA3000_REG_CTRL_SEL_MD_Z_TH,
446                SCA3000_MD_CTRL_OR_Z},
447 };
448
449 static int sca3000_read_raw(struct iio_dev *indio_dev,
450                             struct iio_chan_spec const *chan,
451                             int *val,
452                             int *val2,
453                             long mask)
454 {
455         struct sca3000_state *st = iio_priv(indio_dev);
456         int ret;
457         u8 address;
458
459         switch (mask) {
460         case 0:
461                 mutex_lock(&st->lock);
462                 if (st->mo_det_use_count) {
463                         mutex_unlock(&st->lock);
464                         return -EBUSY;
465                 }
466                 address = sca3000_addresses[chan->address][0];
467                 ret = sca3000_read_data_short(st, address, 2);
468                 if (ret < 0) {
469                         mutex_unlock(&st->lock);
470                         return ret;
471                 }
472                 *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
473                 *val = ((*val) << (sizeof(*val)*8 - 13)) >>
474                         (sizeof(*val)*8 - 13);
475                 mutex_unlock(&st->lock);
476                 return IIO_VAL_INT;
477         case (1 << IIO_CHAN_INFO_SCALE_SHARED):
478                 *val = 0;
479                 if (chan->type == IIO_ACCEL)
480                         *val2 = st->info->scale;
481                 else /* temperature */
482                         *val2 = 555556;
483                 return IIO_VAL_INT_PLUS_MICRO;
484         default:
485                 return -EINVAL;
486         }
487 }
488
489 /**
490  * sca3000_read_av_freq() sysfs function to get available frequencies
491  *
492  * The later modes are only relevant to the ring buffer - and depend on current
493  * mode. Note that data sheet gives rather wide tolerances for these so integer
494  * division will give good enough answer and not all chips have them specified
495  * at all.
496  **/
497 static ssize_t sca3000_read_av_freq(struct device *dev,
498                              struct device_attribute *attr,
499                              char *buf)
500 {
501         struct iio_dev *indio_dev = dev_get_drvdata(dev);
502         struct sca3000_state *st = iio_priv(indio_dev);
503         int len = 0, ret, val;
504
505         mutex_lock(&st->lock);
506         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
507         val = st->rx[0];
508         mutex_unlock(&st->lock);
509         if (ret)
510                 goto error_ret;
511
512         switch (val & 0x03) {
513         case SCA3000_MEAS_MODE_NORMAL:
514                 len += sprintf(buf + len, "%d %d %d\n",
515                                st->info->measurement_mode_freq,
516                                st->info->measurement_mode_freq/2,
517                                st->info->measurement_mode_freq/4);
518                 break;
519         case SCA3000_MEAS_MODE_OP_1:
520                 len += sprintf(buf + len, "%d %d %d\n",
521                                st->info->option_mode_1_freq,
522                                st->info->option_mode_1_freq/2,
523                                st->info->option_mode_1_freq/4);
524                 break;
525         case SCA3000_MEAS_MODE_OP_2:
526                 len += sprintf(buf + len, "%d %d %d\n",
527                                st->info->option_mode_2_freq,
528                                st->info->option_mode_2_freq/2,
529                                st->info->option_mode_2_freq/4);
530                 break;
531         }
532         return len;
533 error_ret:
534         return ret;
535 }
536 /**
537  * __sca3000_get_base_frequency() obtain mode specific base frequency
538  *
539  * lock must be held
540  **/
541 static inline int __sca3000_get_base_freq(struct sca3000_state *st,
542                                           const struct sca3000_chip_info *info,
543                                           int *base_freq)
544 {
545         int ret;
546
547         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
548         if (ret)
549                 goto error_ret;
550         switch (0x03 & st->rx[0]) {
551         case SCA3000_MEAS_MODE_NORMAL:
552                 *base_freq = info->measurement_mode_freq;
553                 break;
554         case SCA3000_MEAS_MODE_OP_1:
555                 *base_freq = info->option_mode_1_freq;
556                 break;
557         case SCA3000_MEAS_MODE_OP_2:
558                 *base_freq = info->option_mode_2_freq;
559                 break;
560         }
561 error_ret:
562         return ret;
563 }
564
565 /**
566  * sca3000_read_frequency() sysfs interface to get the current frequency
567  **/
568 static ssize_t sca3000_read_frequency(struct device *dev,
569                                struct device_attribute *attr,
570                                char *buf)
571 {
572         struct iio_dev *indio_dev = dev_get_drvdata(dev);
573         struct sca3000_state *st = iio_priv(indio_dev);
574         int ret, len = 0, base_freq = 0, val;
575
576         mutex_lock(&st->lock);
577         ret = __sca3000_get_base_freq(st, st->info, &base_freq);
578         if (ret)
579                 goto error_ret_mut;
580         ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
581         mutex_unlock(&st->lock);
582         if (ret)
583                 goto error_ret;
584         val = ret;
585         if (base_freq > 0)
586                 switch (val & 0x03) {
587                 case 0x00:
588                 case 0x03:
589                         len = sprintf(buf, "%d\n", base_freq);
590                         break;
591                 case 0x01:
592                         len = sprintf(buf, "%d\n", base_freq/2);
593                         break;
594                 case 0x02:
595                         len = sprintf(buf, "%d\n", base_freq/4);
596                         break;
597         }
598
599         return len;
600 error_ret_mut:
601         mutex_unlock(&st->lock);
602 error_ret:
603         return ret;
604 }
605
606 /**
607  * sca3000_set_frequency() sysfs interface to set the current frequency
608  **/
609 static ssize_t sca3000_set_frequency(struct device *dev,
610                               struct device_attribute *attr,
611                               const char *buf,
612                               size_t len)
613 {
614         struct iio_dev *indio_dev = dev_get_drvdata(dev);
615         struct sca3000_state *st = iio_priv(indio_dev);
616         int ret, base_freq = 0;
617         int ctrlval;
618         long val;
619
620         ret = strict_strtol(buf, 10, &val);
621         if (ret)
622                 return ret;
623
624         mutex_lock(&st->lock);
625         /* What mode are we in? */
626         ret = __sca3000_get_base_freq(st, st->info, &base_freq);
627         if (ret)
628                 goto error_free_lock;
629
630         ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
631         if (ret < 0)
632                 goto error_free_lock;
633         ctrlval = ret;
634         /* clear the bits */
635         ctrlval &= ~0x03;
636
637         if (val == base_freq/2) {
638                 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_2;
639         } else if (val == base_freq/4) {
640                 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_4;
641         } else if (val != base_freq) {
642                 ret = -EINVAL;
643                 goto error_free_lock;
644         }
645         ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
646                                      ctrlval);
647 error_free_lock:
648         mutex_unlock(&st->lock);
649
650         return ret ? ret : len;
651 }
652
653 /* Should only really be registered if ring buffer support is compiled in.
654  * Does no harm however and doing it right would add a fair bit of complexity
655  */
656 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq);
657
658 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
659                               sca3000_read_frequency,
660                               sca3000_set_frequency);
661
662
663 /**
664  * sca3000_read_temp() sysfs interface to get the temperature when available
665  *
666 * The alignment of data in here is downright odd. See data sheet.
667 * Converting this into a meaningful value is left to inline functions in
668 * userspace part of header.
669 **/
670 static ssize_t sca3000_read_temp(struct device *dev,
671                                  struct device_attribute *attr,
672                                  char *buf)
673 {
674         struct iio_dev *indio_dev = dev_get_drvdata(dev);
675         struct sca3000_state *st = iio_priv(indio_dev);
676         int ret;
677         int val;
678         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_TEMP_MSB, 2);
679         if (ret < 0)
680                 goto error_ret;
681         val = ((st->rx[0] & 0x3F) << 3) | ((st->rx[1] & 0xE0) >> 5);
682
683         return sprintf(buf, "%d\n", val);
684
685 error_ret:
686         return ret;
687 }
688 static IIO_DEV_ATTR_TEMP_RAW(sca3000_read_temp);
689
690 static IIO_CONST_ATTR_TEMP_SCALE("0.555556");
691 static IIO_CONST_ATTR_TEMP_OFFSET("-214.6");
692
693 /**
694  * sca3000_read_thresh() - query of a threshold
695  **/
696 static int sca3000_read_thresh(struct iio_dev *indio_dev,
697                                u64 e,
698                                int *val)
699 {
700         int ret, i;
701         struct sca3000_state *st = iio_priv(indio_dev);
702         int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
703         mutex_lock(&st->lock);
704         ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]);
705         mutex_unlock(&st->lock);
706         if (ret < 0)
707                 return ret;
708         *val = 0;
709         if (num == 1)
710                 for_each_set_bit(i, (unsigned long *)&ret,
711                                  ARRAY_SIZE(st->info->mot_det_mult_y))
712                         *val += st->info->mot_det_mult_y[i];
713         else
714                 for_each_set_bit(i, (unsigned long *)&ret,
715                                  ARRAY_SIZE(st->info->mot_det_mult_xz))
716                         *val += st->info->mot_det_mult_xz[i];
717
718         return 0;
719 }
720
721 /**
722  * sca3000_write_thresh() control of threshold
723  **/
724 static int sca3000_write_thresh(struct iio_dev *indio_dev,
725                                 u64 e,
726                                 int val)
727 {
728         struct sca3000_state *st = iio_priv(indio_dev);
729         int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
730         int ret;
731         int i;
732         u8 nonlinear = 0;
733
734         if (num == 1) {
735                 i = ARRAY_SIZE(st->info->mot_det_mult_y);
736                 while (i > 0)
737                         if (val >= st->info->mot_det_mult_y[--i]) {
738                                 nonlinear |= (1 << i);
739                                 val -= st->info->mot_det_mult_y[i];
740                         }
741         } else {
742                 i = ARRAY_SIZE(st->info->mot_det_mult_xz);
743                 while (i > 0)
744                         if (val >= st->info->mot_det_mult_xz[--i]) {
745                                 nonlinear |= (1 << i);
746                                 val -= st->info->mot_det_mult_xz[i];
747                         }
748         }
749
750         mutex_lock(&st->lock);
751         ret = sca3000_write_ctrl_reg(st, sca3000_addresses[num][1], nonlinear);
752         mutex_unlock(&st->lock);
753
754         return ret;
755 }
756
757 static struct attribute *sca3000_attributes[] = {
758         &iio_dev_attr_revision.dev_attr.attr,
759         &iio_dev_attr_measurement_mode_available.dev_attr.attr,
760         &iio_dev_attr_measurement_mode.dev_attr.attr,
761         &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
762         &iio_dev_attr_sampling_frequency.dev_attr.attr,
763         NULL,
764 };
765
766 static struct attribute *sca3000_attributes_with_temp[] = {
767         &iio_dev_attr_revision.dev_attr.attr,
768         &iio_dev_attr_measurement_mode_available.dev_attr.attr,
769         &iio_dev_attr_measurement_mode.dev_attr.attr,
770         &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
771         &iio_dev_attr_sampling_frequency.dev_attr.attr,
772         /* Only present if temp sensor is */
773         &iio_dev_attr_in_temp_raw.dev_attr.attr,
774         &iio_const_attr_in_temp_offset.dev_attr.attr,
775         &iio_const_attr_in_temp_scale.dev_attr.attr,
776         NULL,
777 };
778
779 static const struct attribute_group sca3000_attribute_group = {
780         .attrs = sca3000_attributes,
781 };
782
783 static const struct attribute_group sca3000_attribute_group_with_temp = {
784         .attrs = sca3000_attributes_with_temp,
785 };
786
787 /* RING RELATED interrupt handler */
788 /* depending on event, push to the ring buffer event chrdev or the event one */
789
790 /**
791  * sca3000_event_handler() - handling ring and non ring events
792  *
793  * This function is complicated by the fact that the devices can signify ring
794  * and non ring events via the same interrupt line and they can only
795  * be distinguished via a read of the relevant status register.
796  **/
797 static irqreturn_t sca3000_event_handler(int irq, void *private)
798 {
799         struct iio_dev *indio_dev = private;
800         struct sca3000_state *st = iio_priv(indio_dev);
801         int ret, val;
802         s64 last_timestamp = iio_get_time_ns();
803
804         /* Could lead if badly timed to an extra read of status reg,
805          * but ensures no interrupt is missed.
806          */
807         mutex_lock(&st->lock);
808         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
809         val = st->rx[0];
810         mutex_unlock(&st->lock);
811         if (ret)
812                 goto done;
813
814         sca3000_ring_int_process(val, indio_dev->buffer);
815
816         if (val & SCA3000_INT_STATUS_FREE_FALL)
817                 iio_push_event(indio_dev,
818                                IIO_MOD_EVENT_CODE(IIO_ACCEL,
819                                                   0,
820                                                   IIO_MOD_X_AND_Y_AND_Z,
821                                                   IIO_EV_TYPE_MAG,
822                                                   IIO_EV_DIR_FALLING),
823                                last_timestamp);
824
825         if (val & SCA3000_INT_STATUS_Y_TRIGGER)
826                 iio_push_event(indio_dev,
827                                IIO_MOD_EVENT_CODE(IIO_ACCEL,
828                                                   0,
829                                                   IIO_MOD_Y,
830                                                   IIO_EV_TYPE_MAG,
831                                                   IIO_EV_DIR_RISING),
832                                last_timestamp);
833
834         if (val & SCA3000_INT_STATUS_X_TRIGGER)
835                 iio_push_event(indio_dev,
836                                IIO_MOD_EVENT_CODE(IIO_ACCEL,
837                                                   0,
838                                                   IIO_MOD_X,
839                                                   IIO_EV_TYPE_MAG,
840                                                   IIO_EV_DIR_RISING),
841                                last_timestamp);
842
843         if (val & SCA3000_INT_STATUS_Z_TRIGGER)
844                 iio_push_event(indio_dev,
845                                IIO_MOD_EVENT_CODE(IIO_ACCEL,
846                                                   0,
847                                                   IIO_MOD_Z,
848                                                   IIO_EV_TYPE_MAG,
849                                                   IIO_EV_DIR_RISING),
850                                last_timestamp);
851
852 done:
853         return IRQ_HANDLED;
854 }
855
856 /**
857  * sca3000_read_event_config() what events are enabled
858  **/
859 static int sca3000_read_event_config(struct iio_dev *indio_dev,
860                                      u64 e)
861 {
862         struct sca3000_state *st = iio_priv(indio_dev);
863         int ret;
864         u8 protect_mask = 0x03;
865         int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
866
867         /* read current value of mode register */
868         mutex_lock(&st->lock);
869         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
870         if (ret)
871                 goto error_ret;
872
873         if ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
874                 ret = 0;
875         else {
876                 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
877                 if (ret < 0)
878                         goto error_ret;
879                 /* only supporting logical or's for now */
880                 ret = !!(ret & sca3000_addresses[num][2]);
881         }
882 error_ret:
883         mutex_unlock(&st->lock);
884
885         return ret;
886 }
887 /**
888  * sca3000_query_free_fall_mode() is free fall mode enabled
889  **/
890 static ssize_t sca3000_query_free_fall_mode(struct device *dev,
891                                             struct device_attribute *attr,
892                                             char *buf)
893 {
894         int ret, len;
895         struct iio_dev *indio_dev = dev_get_drvdata(dev);
896         struct sca3000_state *st = iio_priv(indio_dev);
897         int val;
898
899         mutex_lock(&st->lock);
900         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
901         val = st->rx[0];
902         mutex_unlock(&st->lock);
903         if (ret < 0)
904                 return ret;
905         len = sprintf(buf, "%d\n",
906                       !!(val & SCA3000_FREE_FALL_DETECT));
907         return len;
908 }
909
910 /**
911  * sca3000_set_free_fall_mode() simple on off control for free fall int
912  *
913  * In these chips the free fall detector should send an interrupt if
914  * the device falls more than 25cm.  This has not been tested due
915  * to fragile wiring.
916  **/
917
918 static ssize_t sca3000_set_free_fall_mode(struct device *dev,
919                                           struct device_attribute *attr,
920                                           const char *buf,
921                                           size_t len)
922 {
923         struct iio_dev *indio_dev = dev_get_drvdata(dev);
924         struct sca3000_state *st = iio_priv(indio_dev);
925         long val;
926         int ret;
927         u8 protect_mask = SCA3000_FREE_FALL_DETECT;
928
929         mutex_lock(&st->lock);
930         ret = strict_strtol(buf, 10, &val);
931         if (ret)
932                 goto error_ret;
933
934         /* read current value of mode register */
935         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
936         if (ret)
937                 goto error_ret;
938
939         /*if off and should be on*/
940         if (val && !(st->rx[0] & protect_mask))
941                 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
942                                         (st->rx[0] | SCA3000_FREE_FALL_DETECT));
943         /* if on and should be off */
944         else if (!val && (st->rx[0] & protect_mask))
945                 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
946                                         (st->rx[0] & ~protect_mask));
947 error_ret:
948         mutex_unlock(&st->lock);
949
950         return ret ? ret : len;
951 }
952
953 /**
954  * sca3000_set_mo_det() simple on off control for motion detector
955  *
956  * This is a per axis control, but enabling any will result in the
957  * motion detector unit being enabled.
958  * N.B. enabling motion detector stops normal data acquisition.
959  * There is a complexity in knowing which mode to return to when
960  * this mode is disabled.  Currently normal mode is assumed.
961  **/
962 static int sca3000_write_event_config(struct iio_dev *indio_dev,
963                                       u64 e,
964                                       int state)
965 {
966         struct sca3000_state *st = iio_priv(indio_dev);
967         int ret, ctrlval;
968         u8 protect_mask = 0x03;
969         int num = IIO_EVENT_CODE_EXTRACT_MODIFIER(e);
970
971         mutex_lock(&st->lock);
972         /* First read the motion detector config to find out if
973          * this axis is on*/
974         ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
975         if (ret < 0)
976                 goto exit_point;
977         ctrlval = ret;
978         /* Off and should be on */
979         if (state && !(ctrlval & sca3000_addresses[num][2])) {
980                 ret = sca3000_write_ctrl_reg(st,
981                                              SCA3000_REG_CTRL_SEL_MD_CTRL,
982                                              ctrlval |
983                                              sca3000_addresses[num][2]);
984                 if (ret)
985                         goto exit_point;
986                 st->mo_det_use_count++;
987         } else if (!state && (ctrlval & sca3000_addresses[num][2])) {
988                 ret = sca3000_write_ctrl_reg(st,
989                                              SCA3000_REG_CTRL_SEL_MD_CTRL,
990                                              ctrlval &
991                                              ~(sca3000_addresses[num][2]));
992                 if (ret)
993                         goto exit_point;
994                 st->mo_det_use_count--;
995         }
996
997         /* read current value of mode register */
998         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
999         if (ret)
1000                 goto exit_point;
1001         /*if off and should be on*/
1002         if ((st->mo_det_use_count)
1003             && ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
1004                 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1005                                         (st->rx[0] & ~protect_mask)
1006                                         | SCA3000_MEAS_MODE_MOT_DET);
1007         /* if on and should be off */
1008         else if (!(st->mo_det_use_count)
1009                  && ((st->rx[0] & protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
1010                 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1011                                         (st->rx[0] & ~protect_mask));
1012 exit_point:
1013         mutex_unlock(&st->lock);
1014
1015         return ret;
1016 }
1017
1018 /* Free fall detector related event attribute */
1019 static IIO_DEVICE_ATTR_NAMED(accel_xayaz_mag_falling_en,
1020                              in_accel_x&y&z_mag_falling_en,
1021                              S_IRUGO | S_IWUSR,
1022                              sca3000_query_free_fall_mode,
1023                              sca3000_set_free_fall_mode,
1024                              0);
1025
1026 static IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period,
1027                             in_accel_x&y&z_mag_falling_period,
1028                             "0.226");
1029
1030 static struct attribute *sca3000_event_attributes[] = {
1031         &iio_dev_attr_accel_xayaz_mag_falling_en.dev_attr.attr,
1032         &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr,
1033         NULL,
1034 };
1035
1036 static struct attribute_group sca3000_event_attribute_group = {
1037         .attrs = sca3000_event_attributes,
1038         .name = "events",
1039 };
1040
1041 /**
1042  * sca3000_clean_setup() get the device into a predictable state
1043  *
1044  * Devices use flash memory to store many of the register values
1045  * and hence can come up in somewhat unpredictable states.
1046  * Hence reset everything on driver load.
1047   **/
1048 static int sca3000_clean_setup(struct sca3000_state *st)
1049 {
1050         int ret;
1051
1052         mutex_lock(&st->lock);
1053         /* Ensure all interrupts have been acknowledged */
1054         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
1055         if (ret)
1056                 goto error_ret;
1057
1058         /* Turn off all motion detection channels */
1059         ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
1060         if (ret < 0)
1061                 goto error_ret;
1062         ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL,
1063                                      ret & SCA3000_MD_CTRL_PROT_MASK);
1064         if (ret)
1065                 goto error_ret;
1066
1067         /* Disable ring buffer */
1068         ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
1069         ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
1070                                      (ret & SCA3000_OUT_CTRL_PROT_MASK)
1071                                      | SCA3000_OUT_CTRL_BUF_X_EN
1072                                      | SCA3000_OUT_CTRL_BUF_Y_EN
1073                                      | SCA3000_OUT_CTRL_BUF_Z_EN
1074                                      | SCA3000_OUT_CTRL_BUF_DIV_4);
1075         if (ret)
1076                 goto error_ret;
1077         /* Enable interrupts, relevant to mode and set up as active low */
1078         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1079         if (ret)
1080                 goto error_ret;
1081         ret = sca3000_write_reg(st,
1082                                 SCA3000_REG_ADDR_INT_MASK,
1083                                 (ret & SCA3000_INT_MASK_PROT_MASK)
1084                                 | SCA3000_INT_MASK_ACTIVE_LOW);
1085         if (ret)
1086                 goto error_ret;
1087         /* Select normal measurement mode, free fall off, ring off */
1088         /* Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1089          * as that occurs in one of the example on the datasheet */
1090         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
1091         if (ret)
1092                 goto error_ret;
1093         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
1094                                 (st->rx[0] & SCA3000_MODE_PROT_MASK));
1095         st->bpse = 11;
1096
1097 error_ret:
1098         mutex_unlock(&st->lock);
1099         return ret;
1100 }
1101
1102 static const struct iio_info sca3000_info = {
1103         .attrs = &sca3000_attribute_group,
1104         .read_raw = &sca3000_read_raw,
1105         .event_attrs = &sca3000_event_attribute_group,
1106         .read_event_value = &sca3000_read_thresh,
1107         .write_event_value = &sca3000_write_thresh,
1108         .read_event_config = &sca3000_read_event_config,
1109         .write_event_config = &sca3000_write_event_config,
1110         .driver_module = THIS_MODULE,
1111 };
1112
1113 static const struct iio_info sca3000_info_with_temp = {
1114         .attrs = &sca3000_attribute_group_with_temp,
1115         .read_raw = &sca3000_read_raw,
1116         .read_event_value = &sca3000_read_thresh,
1117         .write_event_value = &sca3000_write_thresh,
1118         .read_event_config = &sca3000_read_event_config,
1119         .write_event_config = &sca3000_write_event_config,
1120         .driver_module = THIS_MODULE,
1121 };
1122
1123 static int __devinit sca3000_probe(struct spi_device *spi)
1124 {
1125         int ret;
1126         struct sca3000_state *st;
1127         struct iio_dev *indio_dev;
1128
1129         indio_dev = iio_allocate_device(sizeof(*st));
1130         if (indio_dev == NULL) {
1131                 ret = -ENOMEM;
1132                 goto error_ret;
1133         }
1134
1135         st = iio_priv(indio_dev);
1136         spi_set_drvdata(spi, indio_dev);
1137         st->us = spi;
1138         mutex_init(&st->lock);
1139         st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
1140                                               ->driver_data];
1141
1142         indio_dev->dev.parent = &spi->dev;
1143         indio_dev->name = spi_get_device_id(spi)->name;
1144         if (st->info->temp_output)
1145                 indio_dev->info = &sca3000_info_with_temp;
1146         else {
1147                 indio_dev->info = &sca3000_info;
1148                 indio_dev->channels = sca3000_channels;
1149                 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels);
1150         }
1151         indio_dev->modes = INDIO_DIRECT_MODE;
1152
1153         sca3000_configure_ring(indio_dev);
1154         ret = iio_device_register(indio_dev);
1155         if (ret < 0)
1156                 goto error_free_dev;
1157
1158         ret = iio_buffer_register(indio_dev,
1159                                   sca3000_channels,
1160                                   ARRAY_SIZE(sca3000_channels));
1161         if (ret < 0)
1162                 goto error_unregister_dev;
1163         if (indio_dev->buffer) {
1164                 iio_scan_mask_set(indio_dev->buffer, 0);
1165                 iio_scan_mask_set(indio_dev->buffer, 1);
1166                 iio_scan_mask_set(indio_dev->buffer, 2);
1167         }
1168
1169         if (spi->irq) {
1170                 ret = request_threaded_irq(spi->irq,
1171                                            NULL,
1172                                            &sca3000_event_handler,
1173                                            IRQF_TRIGGER_FALLING,
1174                                            "sca3000",
1175                                            indio_dev);
1176                 if (ret)
1177                         goto error_unregister_ring;
1178         }
1179         sca3000_register_ring_funcs(indio_dev);
1180         ret = sca3000_clean_setup(st);
1181         if (ret)
1182                 goto error_free_irq;
1183         return 0;
1184
1185 error_free_irq:
1186         if (spi->irq)
1187                 free_irq(spi->irq, indio_dev);
1188 error_unregister_ring:
1189         iio_buffer_unregister(indio_dev);
1190 error_unregister_dev:
1191         iio_device_unregister(indio_dev);
1192 error_free_dev:
1193         iio_free_device(indio_dev);
1194
1195 error_ret:
1196         return ret;
1197 }
1198
1199 static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1200 {
1201         int ret;
1202
1203         mutex_lock(&st->lock);
1204         ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1205         if (ret)
1206                 goto error_ret;
1207         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK,
1208                                 (st->rx[0] &
1209                                  ~(SCA3000_INT_MASK_RING_THREE_QUARTER |
1210                                    SCA3000_INT_MASK_RING_HALF |
1211                                    SCA3000_INT_MASK_ALL_INTS)));
1212 error_ret:
1213         mutex_unlock(&st->lock);
1214         return ret;
1215 }
1216
1217 static int sca3000_remove(struct spi_device *spi)
1218 {
1219         struct iio_dev *indio_dev = spi_get_drvdata(spi);
1220         struct sca3000_state *st = iio_priv(indio_dev);
1221         int ret;
1222         /* Must ensure no interrupts can be generated after this!*/
1223         ret = sca3000_stop_all_interrupts(st);
1224         if (ret)
1225                 return ret;
1226         if (spi->irq)
1227                 free_irq(spi->irq, indio_dev);
1228         iio_device_unregister(indio_dev);
1229         iio_buffer_unregister(indio_dev);
1230         sca3000_unconfigure_ring(indio_dev);
1231         iio_free_device(indio_dev);
1232
1233         return 0;
1234 }
1235
1236 static const struct spi_device_id sca3000_id[] = {
1237         {"sca3000_d01", d01},
1238         {"sca3000_e02", e02},
1239         {"sca3000_e04", e04},
1240         {"sca3000_e05", e05},
1241         {}
1242 };
1243
1244 static struct spi_driver sca3000_driver = {
1245         .driver = {
1246                 .name = "sca3000",
1247                 .owner = THIS_MODULE,
1248         },
1249         .probe = sca3000_probe,
1250         .remove = __devexit_p(sca3000_remove),
1251         .id_table = sca3000_id,
1252 };
1253
1254 static __init int sca3000_init(void)
1255 {
1256         return spi_register_driver(&sca3000_driver);
1257 }
1258 module_init(sca3000_init);
1259
1260 static __exit void sca3000_exit(void)
1261 {
1262         spi_unregister_driver(&sca3000_driver);
1263 }
1264 module_exit(sca3000_exit);
1265
1266 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1267 MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1268 MODULE_LICENSE("GPL v2");