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