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