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