]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/staging/iio/accel/sca3000_core.c
staging: iio: accel: change uint8_t to u8
[mirror_ubuntu-artful-kernel.git] / drivers / staging / iio / accel / sca3000_core.c
1 /*
2 * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 *
8 * Copyright (c) 2009 Jonathan Cameron <jic23@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
172 int ret;
173
174 ret = sca3000_reg_lock_on(st);
175 if (ret < 0)
176 goto error_ret;
177 if (ret) {
178 ret = __sca3000_unlock_reg_lock(st);
179 if (ret)
180 goto error_ret;
181 }
182
183 /* Set the control select register */
184 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_SEL, sel);
185 if (ret)
186 goto error_ret;
187
188 /* Write the actual value into the register */
189 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_CTRL_DATA, val);
190
191 error_ret:
192 return ret;
193 }
194
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 /**
227 * sca3000_show_rev() - sysfs interface to read the chip revision number
228 **/
229 static ssize_t sca3000_show_rev(struct device *dev,
230 struct device_attribute *attr,
231 char *buf)
232 {
233 int len = 0, ret;
234 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
235 struct sca3000_state *st = iio_priv(indio_dev);
236
237 mutex_lock(&st->lock);
238 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_REVID, 1);
239 if (ret < 0)
240 goto error_ret;
241 len += sprintf(buf + len,
242 "major=%d, minor=%d\n",
243 st->rx[0] & SCA3000_REVID_MAJOR_MASK,
244 st->rx[0] & SCA3000_REVID_MINOR_MASK);
245 error_ret:
246 mutex_unlock(&st->lock);
247
248 return ret ? ret : len;
249 }
250
251 /**
252 * sca3000_show_available_measurement_modes() display available modes
253 *
254 * This is all read from chip specific data in the driver. Not all
255 * of the sca3000 series support modes other than normal.
256 **/
257 static ssize_t
258 sca3000_show_available_measurement_modes(struct device *dev,
259 struct device_attribute *attr,
260 char *buf)
261 {
262 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
263 struct sca3000_state *st = iio_priv(indio_dev);
264 int len = 0;
265
266 len += sprintf(buf + len, "0 - normal mode");
267 switch (st->info->option_mode_1) {
268 case SCA3000_OP_MODE_NARROW:
269 len += sprintf(buf + len, ", 1 - narrow mode");
270 break;
271 case SCA3000_OP_MODE_BYPASS:
272 len += sprintf(buf + len, ", 1 - bypass mode");
273 break;
274 }
275 switch (st->info->option_mode_2) {
276 case SCA3000_OP_MODE_WIDE:
277 len += sprintf(buf + len, ", 2 - wide mode");
278 break;
279 }
280 /* always supported */
281 len += sprintf(buf + len, " 3 - motion detection\n");
282
283 return len;
284 }
285
286 /**
287 * sca3000_show_measurement_mode() sysfs read of current mode
288 **/
289 static ssize_t
290 sca3000_show_measurement_mode(struct device *dev,
291 struct device_attribute *attr,
292 char *buf)
293 {
294 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
295 struct sca3000_state *st = iio_priv(indio_dev);
296 int len = 0, ret;
297
298 mutex_lock(&st->lock);
299 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
300 if (ret)
301 goto error_ret;
302 /* mask bottom 2 bits - only ones that are relevant */
303 st->rx[0] &= 0x03;
304 switch (st->rx[0]) {
305 case SCA3000_MEAS_MODE_NORMAL:
306 len += sprintf(buf + len, "0 - normal mode\n");
307 break;
308 case SCA3000_MEAS_MODE_MOT_DET:
309 len += sprintf(buf + len, "3 - motion detection\n");
310 break;
311 case SCA3000_MEAS_MODE_OP_1:
312 switch (st->info->option_mode_1) {
313 case SCA3000_OP_MODE_NARROW:
314 len += sprintf(buf + len, "1 - narrow mode\n");
315 break;
316 case SCA3000_OP_MODE_BYPASS:
317 len += sprintf(buf + len, "1 - bypass mode\n");
318 break;
319 }
320 break;
321 case SCA3000_MEAS_MODE_OP_2:
322 switch (st->info->option_mode_2) {
323 case SCA3000_OP_MODE_WIDE:
324 len += sprintf(buf + len, "2 - wide mode\n");
325 break;
326 }
327 break;
328 }
329
330 error_ret:
331 mutex_unlock(&st->lock);
332
333 return ret ? ret : len;
334 }
335
336 /**
337 * sca3000_store_measurement_mode() set the current mode
338 **/
339 static ssize_t
340 sca3000_store_measurement_mode(struct device *dev,
341 struct device_attribute *attr,
342 const char *buf,
343 size_t len)
344 {
345 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
346 struct sca3000_state *st = iio_priv(indio_dev);
347 int ret;
348 u8 mask = 0x03;
349 u8 val;
350
351 mutex_lock(&st->lock);
352 ret = kstrtou8(buf, 10, &val);
353 if (ret)
354 goto error_ret;
355 if (val > 3) {
356 ret = -EINVAL;
357 goto error_ret;
358 }
359 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
360 if (ret)
361 goto error_ret;
362 st->rx[0] &= ~mask;
363 st->rx[0] |= (val & mask);
364 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, st->rx[0]);
365 if (ret)
366 goto error_ret;
367 mutex_unlock(&st->lock);
368
369 return len;
370
371 error_ret:
372 mutex_unlock(&st->lock);
373
374 return ret;
375 }
376
377
378 /*
379 * Not even vaguely standard attributes so defined here rather than
380 * in the relevant IIO core headers
381 */
382 static IIO_DEVICE_ATTR(measurement_mode_available, S_IRUGO,
383 sca3000_show_available_measurement_modes,
384 NULL, 0);
385
386 static IIO_DEVICE_ATTR(measurement_mode, S_IRUGO | S_IWUSR,
387 sca3000_show_measurement_mode,
388 sca3000_store_measurement_mode,
389 0);
390
391 /* More standard attributes */
392
393 static IIO_DEVICE_ATTR(revision, S_IRUGO, sca3000_show_rev, NULL, 0);
394
395 static const struct iio_event_spec sca3000_event = {
396 .type = IIO_EV_TYPE_MAG,
397 .dir = IIO_EV_DIR_RISING,
398 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
399 };
400
401 #define SCA3000_CHAN(index, mod) \
402 { \
403 .type = IIO_ACCEL, \
404 .modified = 1, \
405 .channel2 = mod, \
406 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
407 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
408 .address = index, \
409 .scan_index = index, \
410 .scan_type = { \
411 .sign = 's', \
412 .realbits = 11, \
413 .storagebits = 16, \
414 .shift = 5, \
415 }, \
416 .event_spec = &sca3000_event, \
417 .num_event_specs = 1, \
418 }
419
420 static const struct iio_chan_spec sca3000_channels[] = {
421 SCA3000_CHAN(0, IIO_MOD_X),
422 SCA3000_CHAN(1, IIO_MOD_Y),
423 SCA3000_CHAN(2, IIO_MOD_Z),
424 };
425
426 static const struct iio_chan_spec sca3000_channels_with_temp[] = {
427 SCA3000_CHAN(0, IIO_MOD_X),
428 SCA3000_CHAN(1, IIO_MOD_Y),
429 SCA3000_CHAN(2, IIO_MOD_Z),
430 {
431 .type = IIO_TEMP,
432 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
433 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
434 BIT(IIO_CHAN_INFO_OFFSET),
435 /* No buffer support */
436 .scan_index = -1,
437 },
438 };
439
440 static u8 sca3000_addresses[3][3] = {
441 [0] = {SCA3000_REG_ADDR_X_MSB, SCA3000_REG_CTRL_SEL_MD_X_TH,
442 SCA3000_MD_CTRL_OR_X},
443 [1] = {SCA3000_REG_ADDR_Y_MSB, SCA3000_REG_CTRL_SEL_MD_Y_TH,
444 SCA3000_MD_CTRL_OR_Y},
445 [2] = {SCA3000_REG_ADDR_Z_MSB, SCA3000_REG_CTRL_SEL_MD_Z_TH,
446 SCA3000_MD_CTRL_OR_Z},
447 };
448
449 static int sca3000_read_raw(struct iio_dev *indio_dev,
450 struct iio_chan_spec const *chan,
451 int *val,
452 int *val2,
453 long mask)
454 {
455 struct sca3000_state *st = iio_priv(indio_dev);
456 int ret;
457 u8 address;
458
459 switch (mask) {
460 case IIO_CHAN_INFO_RAW:
461 mutex_lock(&st->lock);
462 if (chan->type == IIO_ACCEL) {
463 if (st->mo_det_use_count) {
464 mutex_unlock(&st->lock);
465 return -EBUSY;
466 }
467 address = sca3000_addresses[chan->address][0];
468 ret = sca3000_read_data_short(st, address, 2);
469 if (ret < 0) {
470 mutex_unlock(&st->lock);
471 return ret;
472 }
473 *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
474 *val = ((*val) << (sizeof(*val) * 8 - 13)) >>
475 (sizeof(*val) * 8 - 13);
476 } else {
477 /* get the temperature when available */
478 ret = sca3000_read_data_short(st,
479 SCA3000_REG_ADDR_TEMP_MSB,
480 2);
481 if (ret < 0) {
482 mutex_unlock(&st->lock);
483 return ret;
484 }
485 *val = ((st->rx[0] & 0x3F) << 3) |
486 ((st->rx[1] & 0xE0) >> 5);
487 }
488 mutex_unlock(&st->lock);
489 return IIO_VAL_INT;
490 case IIO_CHAN_INFO_SCALE:
491 *val = 0;
492 if (chan->type == IIO_ACCEL)
493 *val2 = st->info->scale;
494 else /* temperature */
495 *val2 = 555556;
496 return IIO_VAL_INT_PLUS_MICRO;
497 case IIO_CHAN_INFO_OFFSET:
498 *val = -214;
499 *val2 = 600000;
500 return IIO_VAL_INT_PLUS_MICRO;
501 default:
502 return -EINVAL;
503 }
504 }
505
506 /**
507 * sca3000_read_av_freq() sysfs function to get available frequencies
508 *
509 * The later modes are only relevant to the ring buffer - and depend on current
510 * mode. Note that data sheet gives rather wide tolerances for these so integer
511 * division will give good enough answer and not all chips have them specified
512 * at all.
513 **/
514 static ssize_t sca3000_read_av_freq(struct device *dev,
515 struct device_attribute *attr,
516 char *buf)
517 {
518 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
519 struct sca3000_state *st = iio_priv(indio_dev);
520 int len = 0, ret, val;
521
522 mutex_lock(&st->lock);
523 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
524 val = st->rx[0];
525 mutex_unlock(&st->lock);
526 if (ret)
527 goto error_ret;
528
529 switch (val & 0x03) {
530 case SCA3000_MEAS_MODE_NORMAL:
531 len += sprintf(buf + len, "%d %d %d\n",
532 st->info->measurement_mode_freq,
533 st->info->measurement_mode_freq / 2,
534 st->info->measurement_mode_freq / 4);
535 break;
536 case SCA3000_MEAS_MODE_OP_1:
537 len += sprintf(buf + len, "%d %d %d\n",
538 st->info->option_mode_1_freq,
539 st->info->option_mode_1_freq / 2,
540 st->info->option_mode_1_freq / 4);
541 break;
542 case SCA3000_MEAS_MODE_OP_2:
543 len += sprintf(buf + len, "%d %d %d\n",
544 st->info->option_mode_2_freq,
545 st->info->option_mode_2_freq / 2,
546 st->info->option_mode_2_freq / 4);
547 break;
548 }
549 return len;
550 error_ret:
551 return ret;
552 }
553 /**
554 * __sca3000_get_base_freq() obtain mode specific base frequency
555 *
556 * lock must be held
557 **/
558 static inline int __sca3000_get_base_freq(struct sca3000_state *st,
559 const struct sca3000_chip_info *info,
560 int *base_freq)
561 {
562 int ret;
563
564 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
565 if (ret)
566 goto error_ret;
567 switch (0x03 & st->rx[0]) {
568 case SCA3000_MEAS_MODE_NORMAL:
569 *base_freq = info->measurement_mode_freq;
570 break;
571 case SCA3000_MEAS_MODE_OP_1:
572 *base_freq = info->option_mode_1_freq;
573 break;
574 case SCA3000_MEAS_MODE_OP_2:
575 *base_freq = info->option_mode_2_freq;
576 break;
577 }
578 error_ret:
579 return ret;
580 }
581
582 /**
583 * sca3000_read_frequency() sysfs interface to get the current frequency
584 **/
585 static ssize_t sca3000_read_frequency(struct device *dev,
586 struct device_attribute *attr,
587 char *buf)
588 {
589 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
590 struct sca3000_state *st = iio_priv(indio_dev);
591 int ret, len = 0, base_freq = 0, val;
592
593 mutex_lock(&st->lock);
594 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
595 if (ret)
596 goto error_ret_mut;
597 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
598 mutex_unlock(&st->lock);
599 if (ret)
600 goto error_ret;
601 val = ret;
602 if (base_freq > 0)
603 switch (val & 0x03) {
604 case 0x00:
605 case 0x03:
606 len = sprintf(buf, "%d\n", base_freq);
607 break;
608 case 0x01:
609 len = sprintf(buf, "%d\n", base_freq / 2);
610 break;
611 case 0x02:
612 len = sprintf(buf, "%d\n", base_freq / 4);
613 break;
614 }
615
616 return len;
617 error_ret_mut:
618 mutex_unlock(&st->lock);
619 error_ret:
620 return ret;
621 }
622
623 /**
624 * sca3000_set_frequency() sysfs interface to set the current frequency
625 **/
626 static ssize_t sca3000_set_frequency(struct device *dev,
627 struct device_attribute *attr,
628 const char *buf,
629 size_t len)
630 {
631 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
632 struct sca3000_state *st = iio_priv(indio_dev);
633 int ret, base_freq = 0;
634 int ctrlval;
635 int val;
636
637 ret = kstrtoint(buf, 10, &val);
638 if (ret)
639 return ret;
640
641 mutex_lock(&st->lock);
642 /* What mode are we in? */
643 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
644 if (ret)
645 goto error_free_lock;
646
647 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
648 if (ret < 0)
649 goto error_free_lock;
650 ctrlval = ret;
651 /* clear the bits */
652 ctrlval &= ~0x03;
653
654 if (val == base_freq / 2) {
655 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_2;
656 } else if (val == base_freq / 4) {
657 ctrlval |= SCA3000_OUT_CTRL_BUF_DIV_4;
658 } else if (val != base_freq) {
659 ret = -EINVAL;
660 goto error_free_lock;
661 }
662 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
663 ctrlval);
664 error_free_lock:
665 mutex_unlock(&st->lock);
666
667 return ret ? ret : len;
668 }
669
670 /*
671 * Should only really be registered if ring buffer support is compiled in.
672 * Does no harm however and doing it right would add a fair bit of complexity
673 */
674 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq);
675
676 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
677 sca3000_read_frequency,
678 sca3000_set_frequency);
679
680 /**
681 * sca3000_read_thresh() - query of a threshold
682 **/
683 static int sca3000_read_thresh(struct iio_dev *indio_dev,
684 const struct iio_chan_spec *chan,
685 enum iio_event_type type,
686 enum iio_event_direction dir,
687 enum iio_event_info info,
688 int *val, int *val2)
689 {
690 int ret, i;
691 struct sca3000_state *st = iio_priv(indio_dev);
692 int num = chan->channel2;
693
694 mutex_lock(&st->lock);
695 ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]);
696 mutex_unlock(&st->lock);
697 if (ret < 0)
698 return ret;
699 *val = 0;
700 if (num == 1)
701 for_each_set_bit(i, (unsigned long *)&ret,
702 ARRAY_SIZE(st->info->mot_det_mult_y))
703 *val += st->info->mot_det_mult_y[i];
704 else
705 for_each_set_bit(i, (unsigned long *)&ret,
706 ARRAY_SIZE(st->info->mot_det_mult_xz))
707 *val += st->info->mot_det_mult_xz[i];
708
709 return IIO_VAL_INT;
710 }
711
712 /**
713 * sca3000_write_thresh() control of threshold
714 **/
715 static int sca3000_write_thresh(struct iio_dev *indio_dev,
716 const struct iio_chan_spec *chan,
717 enum iio_event_type type,
718 enum iio_event_direction dir,
719 enum iio_event_info info,
720 int val, int val2)
721 {
722 struct sca3000_state *st = iio_priv(indio_dev);
723 int num = chan->channel2;
724 int ret;
725 int i;
726 u8 nonlinear = 0;
727
728 if (num == 1) {
729 i = ARRAY_SIZE(st->info->mot_det_mult_y);
730 while (i > 0)
731 if (val >= st->info->mot_det_mult_y[--i]) {
732 nonlinear |= (1 << i);
733 val -= st->info->mot_det_mult_y[i];
734 }
735 } else {
736 i = ARRAY_SIZE(st->info->mot_det_mult_xz);
737 while (i > 0)
738 if (val >= st->info->mot_det_mult_xz[--i]) {
739 nonlinear |= (1 << i);
740 val -= st->info->mot_det_mult_xz[i];
741 }
742 }
743
744 mutex_lock(&st->lock);
745 ret = sca3000_write_ctrl_reg(st, sca3000_addresses[num][1], nonlinear);
746 mutex_unlock(&st->lock);
747
748 return ret;
749 }
750
751 static struct attribute *sca3000_attributes[] = {
752 &iio_dev_attr_revision.dev_attr.attr,
753 &iio_dev_attr_measurement_mode_available.dev_attr.attr,
754 &iio_dev_attr_measurement_mode.dev_attr.attr,
755 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
756 &iio_dev_attr_sampling_frequency.dev_attr.attr,
757 NULL,
758 };
759
760 static const struct attribute_group sca3000_attribute_group = {
761 .attrs = sca3000_attributes,
762 };
763
764 /**
765 * sca3000_event_handler() - handling ring and non ring events
766 *
767 * Ring related interrupt handler. Depending on event, push to
768 * the ring buffer event chrdev or the event one.
769 *
770 * This function is complicated by the fact that the devices can signify ring
771 * and non ring events via the same interrupt line and they can only
772 * be distinguished via a read of the relevant status register.
773 **/
774 static irqreturn_t sca3000_event_handler(int irq, void *private)
775 {
776 struct iio_dev *indio_dev = private;
777 struct sca3000_state *st = iio_priv(indio_dev);
778 int ret, val;
779 s64 last_timestamp = iio_get_time_ns();
780
781 /*
782 * Could lead if badly timed to an extra read of status reg,
783 * but ensures no interrupt is missed.
784 */
785 mutex_lock(&st->lock);
786 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
787 val = st->rx[0];
788 mutex_unlock(&st->lock);
789 if (ret)
790 goto done;
791
792 sca3000_ring_int_process(val, indio_dev->buffer);
793
794 if (val & SCA3000_INT_STATUS_FREE_FALL)
795 iio_push_event(indio_dev,
796 IIO_MOD_EVENT_CODE(IIO_ACCEL,
797 0,
798 IIO_MOD_X_AND_Y_AND_Z,
799 IIO_EV_TYPE_MAG,
800 IIO_EV_DIR_FALLING),
801 last_timestamp);
802
803 if (val & SCA3000_INT_STATUS_Y_TRIGGER)
804 iio_push_event(indio_dev,
805 IIO_MOD_EVENT_CODE(IIO_ACCEL,
806 0,
807 IIO_MOD_Y,
808 IIO_EV_TYPE_MAG,
809 IIO_EV_DIR_RISING),
810 last_timestamp);
811
812 if (val & SCA3000_INT_STATUS_X_TRIGGER)
813 iio_push_event(indio_dev,
814 IIO_MOD_EVENT_CODE(IIO_ACCEL,
815 0,
816 IIO_MOD_X,
817 IIO_EV_TYPE_MAG,
818 IIO_EV_DIR_RISING),
819 last_timestamp);
820
821 if (val & SCA3000_INT_STATUS_Z_TRIGGER)
822 iio_push_event(indio_dev,
823 IIO_MOD_EVENT_CODE(IIO_ACCEL,
824 0,
825 IIO_MOD_Z,
826 IIO_EV_TYPE_MAG,
827 IIO_EV_DIR_RISING),
828 last_timestamp);
829
830 done:
831 return IRQ_HANDLED;
832 }
833
834 /**
835 * sca3000_read_event_config() what events are enabled
836 **/
837 static int sca3000_read_event_config(struct iio_dev *indio_dev,
838 const struct iio_chan_spec *chan,
839 enum iio_event_type type,
840 enum iio_event_direction dir)
841 {
842 struct sca3000_state *st = iio_priv(indio_dev);
843 int ret;
844 u8 protect_mask = 0x03;
845 int num = chan->channel2;
846
847 /* read current value of mode register */
848 mutex_lock(&st->lock);
849 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
850 if (ret)
851 goto error_ret;
852
853 if ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET)
854 ret = 0;
855 else {
856 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
857 if (ret < 0)
858 goto error_ret;
859 /* only supporting logical or's for now */
860 ret = !!(ret & sca3000_addresses[num][2]);
861 }
862 error_ret:
863 mutex_unlock(&st->lock);
864
865 return ret;
866 }
867 /**
868 * sca3000_query_free_fall_mode() is free fall mode enabled
869 **/
870 static ssize_t sca3000_query_free_fall_mode(struct device *dev,
871 struct device_attribute *attr,
872 char *buf)
873 {
874 int ret;
875 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
876 struct sca3000_state *st = iio_priv(indio_dev);
877 int val;
878
879 mutex_lock(&st->lock);
880 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
881 val = st->rx[0];
882 mutex_unlock(&st->lock);
883 if (ret < 0)
884 return ret;
885 return sprintf(buf, "%d\n", !!(val & SCA3000_FREE_FALL_DETECT));
886 }
887
888 /**
889 * sca3000_set_free_fall_mode() simple on off control for free fall int
890 *
891 * In these chips the free fall detector should send an interrupt if
892 * the device falls more than 25cm. This has not been tested due
893 * to fragile wiring.
894 **/
895 static ssize_t sca3000_set_free_fall_mode(struct device *dev,
896 struct device_attribute *attr,
897 const char *buf,
898 size_t len)
899 {
900 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
901 struct sca3000_state *st = iio_priv(indio_dev);
902 u8 val;
903 int ret;
904 u8 protect_mask = SCA3000_FREE_FALL_DETECT;
905
906 mutex_lock(&st->lock);
907 ret = kstrtou8(buf, 10, &val);
908 if (ret)
909 goto error_ret;
910
911 /* read current value of mode register */
912 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
913 if (ret)
914 goto error_ret;
915
916 /* if off and should be on */
917 if (val && !(st->rx[0] & protect_mask))
918 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
919 (st->rx[0] | SCA3000_FREE_FALL_DETECT));
920 /* if on and should be off */
921 else if (!val && (st->rx[0] & protect_mask))
922 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
923 (st->rx[0] & ~protect_mask));
924 error_ret:
925 mutex_unlock(&st->lock);
926
927 return ret ? ret : len;
928 }
929
930 /**
931 * sca3000_write_event_config() simple on off control for motion detector
932 *
933 * This is a per axis control, but enabling any will result in the
934 * motion detector unit being enabled.
935 * N.B. enabling motion detector stops normal data acquisition.
936 * There is a complexity in knowing which mode to return to when
937 * this mode is disabled. Currently normal mode is assumed.
938 **/
939 static int sca3000_write_event_config(struct iio_dev *indio_dev,
940 const struct iio_chan_spec *chan,
941 enum iio_event_type type,
942 enum iio_event_direction dir,
943 int state)
944 {
945 struct sca3000_state *st = iio_priv(indio_dev);
946 int ret, ctrlval;
947 u8 protect_mask = 0x03;
948 int num = chan->channel2;
949
950 mutex_lock(&st->lock);
951 /*
952 * First read the motion detector config to find out if
953 * this axis is on
954 */
955 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
956 if (ret < 0)
957 goto exit_point;
958 ctrlval = ret;
959 /* if off and should be on */
960 if (state && !(ctrlval & sca3000_addresses[num][2])) {
961 ret = sca3000_write_ctrl_reg(st,
962 SCA3000_REG_CTRL_SEL_MD_CTRL,
963 ctrlval |
964 sca3000_addresses[num][2]);
965 if (ret)
966 goto exit_point;
967 st->mo_det_use_count++;
968 } else if (!state && (ctrlval & sca3000_addresses[num][2])) {
969 ret = sca3000_write_ctrl_reg(st,
970 SCA3000_REG_CTRL_SEL_MD_CTRL,
971 ctrlval &
972 ~(sca3000_addresses[num][2]));
973 if (ret)
974 goto exit_point;
975 st->mo_det_use_count--;
976 }
977
978 /* read current value of mode register */
979 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
980 if (ret)
981 goto exit_point;
982 /* if off and should be on */
983 if ((st->mo_det_use_count)
984 && ((st->rx[0] & protect_mask) != SCA3000_MEAS_MODE_MOT_DET))
985 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
986 (st->rx[0] & ~protect_mask)
987 | SCA3000_MEAS_MODE_MOT_DET);
988 /* if on and should be off */
989 else if (!(st->mo_det_use_count)
990 && ((st->rx[0] & protect_mask) == SCA3000_MEAS_MODE_MOT_DET))
991 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
992 (st->rx[0] & ~protect_mask));
993 exit_point:
994 mutex_unlock(&st->lock);
995
996 return ret;
997 }
998
999 /* Free fall detector related event attribute */
1000 static IIO_DEVICE_ATTR_NAMED(accel_xayaz_mag_falling_en,
1001 in_accel_x & y & z_mag_falling_en,
1002 S_IRUGO | S_IWUSR,
1003 sca3000_query_free_fall_mode,
1004 sca3000_set_free_fall_mode,
1005 0);
1006
1007 static IIO_CONST_ATTR_NAMED(accel_xayaz_mag_falling_period,
1008 in_accel_x & y & z_mag_falling_period,
1009 "0.226");
1010
1011 static struct attribute *sca3000_event_attributes[] = {
1012 &iio_dev_attr_accel_xayaz_mag_falling_en.dev_attr.attr,
1013 &iio_const_attr_accel_xayaz_mag_falling_period.dev_attr.attr,
1014 NULL,
1015 };
1016
1017 static struct attribute_group sca3000_event_attribute_group = {
1018 .attrs = sca3000_event_attributes,
1019 .name = "events",
1020 };
1021
1022 /**
1023 * sca3000_clean_setup() get the device into a predictable state
1024 *
1025 * Devices use flash memory to store many of the register values
1026 * and hence can come up in somewhat unpredictable states.
1027 * Hence reset everything on driver load.
1028 **/
1029 static int sca3000_clean_setup(struct sca3000_state *st)
1030 {
1031 int ret;
1032
1033 mutex_lock(&st->lock);
1034 /* Ensure all interrupts have been acknowledged */
1035 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_STATUS, 1);
1036 if (ret)
1037 goto error_ret;
1038
1039 /* Turn off all motion detection channels */
1040 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
1041 if (ret < 0)
1042 goto error_ret;
1043 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL,
1044 ret & SCA3000_MD_CTRL_PROT_MASK);
1045 if (ret)
1046 goto error_ret;
1047
1048 /* Disable ring buffer */
1049 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
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 .event_attrs = &sca3000_event_attribute_group,
1089 .read_event_value = &sca3000_read_thresh,
1090 .write_event_value = &sca3000_write_thresh,
1091 .read_event_config = &sca3000_read_event_config,
1092 .write_event_config = &sca3000_write_event_config,
1093 .driver_module = THIS_MODULE,
1094 };
1095
1096 static int sca3000_probe(struct spi_device *spi)
1097 {
1098 int ret;
1099 struct sca3000_state *st;
1100 struct iio_dev *indio_dev;
1101
1102 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1103 if (!indio_dev)
1104 return -ENOMEM;
1105
1106 st = iio_priv(indio_dev);
1107 spi_set_drvdata(spi, indio_dev);
1108 st->us = spi;
1109 mutex_init(&st->lock);
1110 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
1111 ->driver_data];
1112
1113 indio_dev->dev.parent = &spi->dev;
1114 indio_dev->name = spi_get_device_id(spi)->name;
1115 indio_dev->info = &sca3000_info;
1116 if (st->info->temp_output) {
1117 indio_dev->channels = sca3000_channels_with_temp;
1118 indio_dev->num_channels =
1119 ARRAY_SIZE(sca3000_channels_with_temp);
1120 } else {
1121 indio_dev->channels = sca3000_channels;
1122 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels);
1123 }
1124 indio_dev->modes = INDIO_DIRECT_MODE;
1125
1126 sca3000_configure_ring(indio_dev);
1127 ret = iio_device_register(indio_dev);
1128 if (ret < 0)
1129 return ret;
1130
1131 if (spi->irq) {
1132 ret = request_threaded_irq(spi->irq,
1133 NULL,
1134 &sca3000_event_handler,
1135 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1136 "sca3000",
1137 indio_dev);
1138 if (ret)
1139 goto error_unregister_dev;
1140 }
1141 sca3000_register_ring_funcs(indio_dev);
1142 ret = sca3000_clean_setup(st);
1143 if (ret)
1144 goto error_free_irq;
1145 return 0;
1146
1147 error_free_irq:
1148 if (spi->irq)
1149 free_irq(spi->irq, indio_dev);
1150 error_unregister_dev:
1151 iio_device_unregister(indio_dev);
1152 return ret;
1153 }
1154
1155 static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1156 {
1157 int ret;
1158
1159 mutex_lock(&st->lock);
1160 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
1161 if (ret)
1162 goto error_ret;
1163 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_INT_MASK,
1164 (st->rx[0] &
1165 ~(SCA3000_INT_MASK_RING_THREE_QUARTER |
1166 SCA3000_INT_MASK_RING_HALF |
1167 SCA3000_INT_MASK_ALL_INTS)));
1168 error_ret:
1169 mutex_unlock(&st->lock);
1170 return ret;
1171 }
1172
1173 static int sca3000_remove(struct spi_device *spi)
1174 {
1175 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1176 struct sca3000_state *st = iio_priv(indio_dev);
1177
1178 /* Must ensure no interrupts can be generated after this! */
1179 sca3000_stop_all_interrupts(st);
1180 if (spi->irq)
1181 free_irq(spi->irq, indio_dev);
1182 iio_device_unregister(indio_dev);
1183 sca3000_unconfigure_ring(indio_dev);
1184
1185 return 0;
1186 }
1187
1188 static const struct spi_device_id sca3000_id[] = {
1189 {"sca3000_d01", d01},
1190 {"sca3000_e02", e02},
1191 {"sca3000_e04", e04},
1192 {"sca3000_e05", e05},
1193 {}
1194 };
1195 MODULE_DEVICE_TABLE(spi, sca3000_id);
1196
1197 static struct spi_driver sca3000_driver = {
1198 .driver = {
1199 .name = "sca3000",
1200 .owner = THIS_MODULE,
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");