blob: cb753a43533cdd85170f2a8156ce94c07dc3d524 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jonathan Cameron574fb252009-08-18 18:06:25 +01002/*
3 * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI
4 *
Jonathan Cameron0f8c9622012-09-02 21:34:59 +01005 * Copyright (c) 2009 Jonathan Cameron <jic23@kernel.org>
Jonathan Cameron574fb252009-08-18 18:06:25 +01006 *
7 * See industrialio/accels/sca3000.h for comments.
8 */
9
10#include <linux/interrupt.h>
Jonathan Cameron574fb252009-08-18 18:06:25 +010011#include <linux/fs.h>
12#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Jonathan Cameron574fb252009-08-18 18:06:25 +010014#include <linux/kernel.h>
15#include <linux/spi/spi.h>
16#include <linux/sysfs.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040017#include <linux/module.h>
Jonathan Cameronced5c032016-10-08 17:39:02 +010018#include <linux/uaccess.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010019#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
21#include <linux/iio/events.h>
22#include <linux/iio/buffer.h>
Jonathan Cameron152a6a82016-10-08 17:39:06 +010023#include <linux/iio/kfifo_buf.h>
Jonathan Cameron574fb252009-08-18 18:06:25 +010024
Jonathan Cameronced5c032016-10-08 17:39:02 +010025#define SCA3000_WRITE_REG(a) (((a) << 2) | 0x02)
26#define SCA3000_READ_REG(a) ((a) << 2)
27
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010028#define SCA3000_REG_REVID_ADDR 0x00
29#define SCA3000_REG_REVID_MAJOR_MASK GENMASK(8, 4)
30#define SCA3000_REG_REVID_MINOR_MASK GENMASK(3, 0)
Jonathan Cameronced5c032016-10-08 17:39:02 +010031
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010032#define SCA3000_REG_STATUS_ADDR 0x02
33#define SCA3000_LOCKED BIT(5)
34#define SCA3000_EEPROM_CS_ERROR BIT(1)
35#define SCA3000_SPI_FRAME_ERROR BIT(0)
Reno Farnesi893bd0e2017-06-26 21:45:22 -040036
Jonathan Cameronced5c032016-10-08 17:39:02 +010037/* All reads done using register decrement so no need to directly access LSBs */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010038#define SCA3000_REG_X_MSB_ADDR 0x05
39#define SCA3000_REG_Y_MSB_ADDR 0x07
40#define SCA3000_REG_Z_MSB_ADDR 0x09
Jonathan Cameronced5c032016-10-08 17:39:02 +010041
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010042#define SCA3000_REG_RING_OUT_ADDR 0x0f
Jonathan Cameronced5c032016-10-08 17:39:02 +010043
44/* Temp read untested - the e05 doesn't have the sensor */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010045#define SCA3000_REG_TEMP_MSB_ADDR 0x13
Jonathan Cameronced5c032016-10-08 17:39:02 +010046
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010047#define SCA3000_REG_MODE_ADDR 0x14
48#define SCA3000_MODE_PROT_MASK 0x28
49#define SCA3000_REG_MODE_RING_BUF_ENABLE BIT(7)
50#define SCA3000_REG_MODE_RING_BUF_8BIT BIT(6)
Jonathan Cameronced5c032016-10-08 17:39:02 +010051
Jonathan Cameronced5c032016-10-08 17:39:02 +010052/*
53 * Free fall detection triggers an interrupt if the acceleration
54 * is below a threshold for equivalent of 25cm drop
55 */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010056#define SCA3000_REG_MODE_FREE_FALL_DETECT BIT(4)
57#define SCA3000_REG_MODE_MEAS_MODE_NORMAL 0x00
58#define SCA3000_REG_MODE_MEAS_MODE_OP_1 0x01
59#define SCA3000_REG_MODE_MEAS_MODE_OP_2 0x02
Jonathan Cameronced5c032016-10-08 17:39:02 +010060
61/*
62 * In motion detection mode the accelerations are band pass filtered
63 * (approx 1 - 25Hz) and then a programmable threshold used to trigger
64 * and interrupt.
65 */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010066#define SCA3000_REG_MODE_MEAS_MODE_MOT_DET 0x03
67#define SCA3000_REG_MODE_MODE_MASK 0x03
Jonathan Cameronced5c032016-10-08 17:39:02 +010068
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010069#define SCA3000_REG_BUF_COUNT_ADDR 0x15
Jonathan Cameronced5c032016-10-08 17:39:02 +010070
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010071#define SCA3000_REG_INT_STATUS_ADDR 0x16
72#define SCA3000_REG_INT_STATUS_THREE_QUARTERS BIT(7)
73#define SCA3000_REG_INT_STATUS_HALF BIT(6)
Reno Farnesi893bd0e2017-06-26 21:45:22 -040074
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010075#define SCA3000_INT_STATUS_FREE_FALL BIT(3)
76#define SCA3000_INT_STATUS_Y_TRIGGER BIT(2)
77#define SCA3000_INT_STATUS_X_TRIGGER BIT(1)
78#define SCA3000_INT_STATUS_Z_TRIGGER BIT(0)
Jonathan Cameronced5c032016-10-08 17:39:02 +010079
80/* Used to allow access to multiplexed registers */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010081#define SCA3000_REG_CTRL_SEL_ADDR 0x18
Jonathan Cameronced5c032016-10-08 17:39:02 +010082/* Only available for SCA3000-D03 and SCA3000-D01 */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010083#define SCA3000_REG_CTRL_SEL_I2C_DISABLE 0x01
84#define SCA3000_REG_CTRL_SEL_MD_CTRL 0x02
85#define SCA3000_REG_CTRL_SEL_MD_Y_TH 0x03
86#define SCA3000_REG_CTRL_SEL_MD_X_TH 0x04
87#define SCA3000_REG_CTRL_SEL_MD_Z_TH 0x05
Jonathan Cameronced5c032016-10-08 17:39:02 +010088/*
89 * BE VERY CAREFUL WITH THIS, IF 3 BITS ARE NOT SET the device
90 * will not function
91 */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +010092#define SCA3000_REG_CTRL_SEL_OUT_CTRL 0x0B
93
94#define SCA3000_REG_OUT_CTRL_PROT_MASK 0xE0
95#define SCA3000_REG_OUT_CTRL_BUF_X_EN 0x10
96#define SCA3000_REG_OUT_CTRL_BUF_Y_EN 0x08
97#define SCA3000_REG_OUT_CTRL_BUF_Z_EN 0x04
98#define SCA3000_REG_OUT_CTRL_BUF_DIV_MASK 0x03
99#define SCA3000_REG_OUT_CTRL_BUF_DIV_4 0x02
100#define SCA3000_REG_OUT_CTRL_BUF_DIV_2 0x01
101
Jonathan Cameronced5c032016-10-08 17:39:02 +0100102
103/*
104 * Control which motion detector interrupts are on.
105 * For now only OR combinations are supported.
106 */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100107#define SCA3000_MD_CTRL_PROT_MASK 0xC0
108#define SCA3000_MD_CTRL_OR_Y BIT(0)
109#define SCA3000_MD_CTRL_OR_X BIT(1)
110#define SCA3000_MD_CTRL_OR_Z BIT(2)
Jonathan Cameronced5c032016-10-08 17:39:02 +0100111/* Currently unsupported */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100112#define SCA3000_MD_CTRL_AND_Y BIT(3)
113#define SCA3000_MD_CTRL_AND_X BIT(4)
Christophe JAILLET80343f52019-07-21 12:53:53 +0200114#define SCA3000_MD_CTRL_AND_Z BIT(5)
Jonathan Cameronced5c032016-10-08 17:39:02 +0100115
116/*
117 * Some control registers of complex access methods requiring this register to
118 * be used to remove a lock.
119 */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100120#define SCA3000_REG_UNLOCK_ADDR 0x1e
Jonathan Cameronced5c032016-10-08 17:39:02 +0100121
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100122#define SCA3000_REG_INT_MASK_ADDR 0x21
123#define SCA3000_REG_INT_MASK_PROT_MASK 0x1C
Reno Farnesi893bd0e2017-06-26 21:45:22 -0400124
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100125#define SCA3000_REG_INT_MASK_RING_THREE_QUARTER BIT(7)
126#define SCA3000_REG_INT_MASK_RING_HALF BIT(6)
Jonathan Cameronced5c032016-10-08 17:39:02 +0100127
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100128#define SCA3000_REG_INT_MASK_ALL_INTS 0x02
129#define SCA3000_REG_INT_MASK_ACTIVE_HIGH 0x01
130#define SCA3000_REG_INT_MASK_ACTIVE_LOW 0x00
Jonathan Cameronced5c032016-10-08 17:39:02 +0100131/* Values of multiplexed registers (write to ctrl_data after select) */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100132#define SCA3000_REG_CTRL_DATA_ADDR 0x22
Jonathan Cameronced5c032016-10-08 17:39:02 +0100133
134/*
135 * Measurement modes available on some sca3000 series chips. Code assumes others
136 * may become available in the future.
137 *
138 * Bypass - Bypass the low-pass filter in the signal channel so as to increase
139 * signal bandwidth.
140 *
141 * Narrow - Narrow low-pass filtering of the signal channel and half output
142 * data rate by decimation.
143 *
144 * Wide - Widen low-pass filtering of signal channel to increase bandwidth
145 */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100146#define SCA3000_OP_MODE_BYPASS 0x01
147#define SCA3000_OP_MODE_NARROW 0x02
148#define SCA3000_OP_MODE_WIDE 0x04
Jonathan Cameronced5c032016-10-08 17:39:02 +0100149#define SCA3000_MAX_TX 6
150#define SCA3000_MAX_RX 2
151
152/**
153 * struct sca3000_state - device instance state information
154 * @us: the associated spi device
155 * @info: chip variant information
Jonathan Cameronced5c032016-10-08 17:39:02 +0100156 * @last_timestamp: the timestamp of the last event
157 * @mo_det_use_count: reference counter for the motion detection unit
158 * @lock: lock used to protect elements of sca3000_state
159 * and the underlying device state.
Jonathan Cameronced5c032016-10-08 17:39:02 +0100160 * @tx: dma-able transmit buffer
161 * @rx: dma-able receive buffer
162 **/
163struct sca3000_state {
164 struct spi_device *us;
165 const struct sca3000_chip_info *info;
Jonathan Cameronced5c032016-10-08 17:39:02 +0100166 s64 last_timestamp;
167 int mo_det_use_count;
168 struct mutex lock;
Jonathan Cameronced5c032016-10-08 17:39:02 +0100169 /* Can these share a cacheline ? */
Jonathan Cameron152a6a82016-10-08 17:39:06 +0100170 u8 rx[384] ____cacheline_aligned;
Jonathan Cameronced5c032016-10-08 17:39:02 +0100171 u8 tx[6] ____cacheline_aligned;
172};
173
174/**
175 * struct sca3000_chip_info - model dependent parameters
176 * @scale: scale * 10^-6
177 * @temp_output: some devices have temperature sensors.
178 * @measurement_mode_freq: normal mode sampling frequency
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100179 * @measurement_mode_3db_freq: 3db cutoff frequency of the low pass filter for
180 * the normal measurement mode.
Jonathan Cameronced5c032016-10-08 17:39:02 +0100181 * @option_mode_1: first optional mode. Not all models have one
182 * @option_mode_1_freq: option mode 1 sampling frequency
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100183 * @option_mode_1_3db_freq: 3db cutoff frequency of the low pass filter for
184 * the first option mode.
Jonathan Cameronced5c032016-10-08 17:39:02 +0100185 * @option_mode_2: second optional mode. Not all chips have one
186 * @option_mode_2_freq: option mode 2 sampling frequency
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100187 * @option_mode_2_3db_freq: 3db cutoff frequency of the low pass filter for
188 * the second option mode.
Lee Jonesfb37b5f2020-07-17 17:55:24 +0100189 * @mot_det_mult_xz: Bit wise multipliers to calculate the threshold
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100190 * for motion detection in the x and z axis.
Lee Jonesfb37b5f2020-07-17 17:55:24 +0100191 * @mot_det_mult_y: Bit wise multipliers to calculate the threshold
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100192 * for motion detection in the y axis.
Jonathan Cameronced5c032016-10-08 17:39:02 +0100193 *
194 * This structure is used to hold information about the functionality of a given
195 * sca3000 variant.
196 **/
197struct sca3000_chip_info {
198 unsigned int scale;
199 bool temp_output;
200 int measurement_mode_freq;
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100201 int measurement_mode_3db_freq;
Jonathan Cameronced5c032016-10-08 17:39:02 +0100202 int option_mode_1;
203 int option_mode_1_freq;
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100204 int option_mode_1_3db_freq;
Jonathan Cameronced5c032016-10-08 17:39:02 +0100205 int option_mode_2;
206 int option_mode_2_freq;
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100207 int option_mode_2_3db_freq;
Jonathan Cameronced5c032016-10-08 17:39:02 +0100208 int mot_det_mult_xz[6];
209 int mot_det_mult_y[7];
210};
Jonathan Cameron574fb252009-08-18 18:06:25 +0100211
212enum sca3000_variant {
213 d01,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100214 e02,
215 e04,
216 e05,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100217};
218
Peter Meerwald5262d8f2014-01-13 21:28:00 +0000219/*
220 * Note where option modes are not defined, the chip simply does not
Jonathan Cameron574fb252009-08-18 18:06:25 +0100221 * support any.
222 * Other chips in the sca3000 series use i2c and are not included here.
223 *
224 * Some of these devices are only listed in the family data sheet and
225 * do not actually appear to be available.
226 */
227static const struct sca3000_chip_info sca3000_spi_chip_info_tbl[] = {
Jonathan Cameron845bd122011-05-18 14:41:44 +0100228 [d01] = {
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100229 .scale = 7357,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100230 .temp_output = true,
231 .measurement_mode_freq = 250,
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100232 .measurement_mode_3db_freq = 45,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100233 .option_mode_1 = SCA3000_OP_MODE_BYPASS,
234 .option_mode_1_freq = 250,
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100235 .option_mode_1_3db_freq = 70,
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100236 .mot_det_mult_xz = {50, 100, 200, 350, 650, 1300},
237 .mot_det_mult_y = {50, 100, 150, 250, 450, 850, 1750},
Jonathan Cameron845bd122011-05-18 14:41:44 +0100238 },
239 [e02] = {
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100240 .scale = 9810,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100241 .measurement_mode_freq = 125,
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100242 .measurement_mode_3db_freq = 40,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100243 .option_mode_1 = SCA3000_OP_MODE_NARROW,
244 .option_mode_1_freq = 63,
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100245 .option_mode_1_3db_freq = 11,
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100246 .mot_det_mult_xz = {100, 150, 300, 550, 1050, 2050},
247 .mot_det_mult_y = {50, 100, 200, 350, 700, 1350, 2700},
Jonathan Cameron845bd122011-05-18 14:41:44 +0100248 },
249 [e04] = {
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100250 .scale = 19620,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100251 .measurement_mode_freq = 100,
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100252 .measurement_mode_3db_freq = 38,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100253 .option_mode_1 = SCA3000_OP_MODE_NARROW,
254 .option_mode_1_freq = 50,
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100255 .option_mode_1_3db_freq = 9,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100256 .option_mode_2 = SCA3000_OP_MODE_WIDE,
257 .option_mode_2_freq = 400,
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100258 .option_mode_2_3db_freq = 70,
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100259 .mot_det_mult_xz = {200, 300, 600, 1100, 2100, 4100},
260 .mot_det_mult_y = {100, 200, 400, 7000, 1400, 2700, 54000},
Jonathan Cameron845bd122011-05-18 14:41:44 +0100261 },
262 [e05] = {
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100263 .scale = 61313,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100264 .measurement_mode_freq = 200,
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100265 .measurement_mode_3db_freq = 60,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100266 .option_mode_1 = SCA3000_OP_MODE_NARROW,
267 .option_mode_1_freq = 50,
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100268 .option_mode_1_3db_freq = 9,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100269 .option_mode_2 = SCA3000_OP_MODE_WIDE,
270 .option_mode_2_freq = 400,
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100271 .option_mode_2_3db_freq = 75,
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100272 .mot_det_mult_xz = {600, 900, 1700, 3200, 6100, 11900},
273 .mot_det_mult_y = {300, 600, 1200, 2000, 4100, 7800, 15600},
Jonathan Cameron574fb252009-08-18 18:06:25 +0100274 },
275};
276
Jonathan Cameronced5c032016-10-08 17:39:02 +0100277static int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
Jonathan Cameron574fb252009-08-18 18:06:25 +0100278{
Jonathan Cameron574fb252009-08-18 18:06:25 +0100279 st->tx[0] = SCA3000_WRITE_REG(address);
280 st->tx[1] = val;
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100281 return spi_write(st->us, st->tx, 2);
282}
283
Jonathan Cameronced5c032016-10-08 17:39:02 +0100284static int sca3000_read_data_short(struct sca3000_state *st,
Jonathan Cameron389c5832016-10-08 17:39:16 +0100285 u8 reg_address_high,
286 int len)
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100287{
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100288 struct spi_transfer xfer[2] = {
289 {
290 .len = 1,
291 .tx_buf = st->tx,
292 }, {
293 .len = len,
294 .rx_buf = st->rx,
295 }
296 };
297 st->tx[0] = SCA3000_READ_REG(reg_address_high);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100298
Lars-Peter Clausenad6c46b2013-01-09 17:31:00 +0000299 return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
Jonathan Cameron574fb252009-08-18 18:06:25 +0100300}
301
Jonathan Cameron574fb252009-08-18 18:06:25 +0100302/**
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100303 * sca3000_reg_lock_on() - test if the ctrl register lock is on
304 * @st: Driver specific device instance data.
Jonathan Cameron574fb252009-08-18 18:06:25 +0100305 *
306 * Lock must be held.
307 **/
308static int sca3000_reg_lock_on(struct sca3000_state *st)
309{
Jonathan Cameron574fb252009-08-18 18:06:25 +0100310 int ret;
311
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100312 ret = sca3000_read_data_short(st, SCA3000_REG_STATUS_ADDR, 1);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100313 if (ret < 0)
314 return ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +0100315
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100316 return !(st->rx[0] & SCA3000_LOCKED);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100317}
318
319/**
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100320 * __sca3000_unlock_reg_lock() - unlock the control registers
321 * @st: Driver specific device instance data.
Jonathan Cameron574fb252009-08-18 18:06:25 +0100322 *
323 * Note the device does not appear to support doing this in a single transfer.
324 * This should only ever be used as part of ctrl reg read.
325 * Lock must be held before calling this
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100326 */
Jonathan Cameron574fb252009-08-18 18:06:25 +0100327static int __sca3000_unlock_reg_lock(struct sca3000_state *st)
328{
Jonathan Cameron574fb252009-08-18 18:06:25 +0100329 struct spi_transfer xfer[3] = {
330 {
Jonathan Cameron574fb252009-08-18 18:06:25 +0100331 .len = 2,
332 .cs_change = 1,
333 .tx_buf = st->tx,
334 }, {
Jonathan Cameron574fb252009-08-18 18:06:25 +0100335 .len = 2,
336 .cs_change = 1,
337 .tx_buf = st->tx + 2,
338 }, {
Jonathan Cameron574fb252009-08-18 18:06:25 +0100339 .len = 2,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100340 .tx_buf = st->tx + 4,
341 },
342 };
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100343 st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100344 st->tx[1] = 0x00;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100345 st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100346 st->tx[3] = 0x50;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100347 st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100348 st->tx[5] = 0xA0;
Jonathan Cameron574fb252009-08-18 18:06:25 +0100349
Lars-Peter Clausenad6c46b2013-01-09 17:31:00 +0000350 return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
Jonathan Cameron574fb252009-08-18 18:06:25 +0100351}
352
353/**
Jonathan Camerond7f1c0c2021-03-14 16:46:54 +0000354 * sca3000_write_ctrl_reg() - write to a lock protect ctrl register
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100355 * @st: Driver specific device instance data.
Jonathan Cameron574fb252009-08-18 18:06:25 +0100356 * @sel: selects which registers we wish to write to
357 * @val: the value to be written
358 *
359 * Certain control registers are protected against overwriting by the lock
360 * register and use a shared write address. This function allows writing of
361 * these registers.
362 * Lock must be held.
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100363 */
Jonathan Cameron574fb252009-08-18 18:06:25 +0100364static int sca3000_write_ctrl_reg(struct sca3000_state *st,
Ioana Ciornei89ea25c2015-10-29 01:01:50 +0200365 u8 sel,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100366 uint8_t val)
367{
Jonathan Cameron574fb252009-08-18 18:06:25 +0100368 int ret;
369
370 ret = sca3000_reg_lock_on(st);
371 if (ret < 0)
372 goto error_ret;
373 if (ret) {
374 ret = __sca3000_unlock_reg_lock(st);
375 if (ret)
376 goto error_ret;
377 }
378
379 /* Set the control select register */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100380 ret = sca3000_write_reg(st, SCA3000_REG_CTRL_SEL_ADDR, sel);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100381 if (ret)
382 goto error_ret;
383
384 /* Write the actual value into the register */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100385 ret = sca3000_write_reg(st, SCA3000_REG_CTRL_DATA_ADDR, val);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100386
387error_ret:
388 return ret;
389}
390
Jonathan Cameron574fb252009-08-18 18:06:25 +0100391/**
Jonathan Camerond7f1c0c2021-03-14 16:46:54 +0000392 * sca3000_read_ctrl_reg() - read from lock protected control register.
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100393 * @st: Driver specific device instance data.
394 * @ctrl_reg: Which ctrl register do we want to read.
Jonathan Cameron574fb252009-08-18 18:06:25 +0100395 *
396 * Lock must be held.
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100397 */
Jonathan Cameron574fb252009-08-18 18:06:25 +0100398static int sca3000_read_ctrl_reg(struct sca3000_state *st,
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100399 u8 ctrl_reg)
Jonathan Cameron574fb252009-08-18 18:06:25 +0100400{
401 int ret;
402
403 ret = sca3000_reg_lock_on(st);
404 if (ret < 0)
405 goto error_ret;
406 if (ret) {
407 ret = __sca3000_unlock_reg_lock(st);
408 if (ret)
409 goto error_ret;
410 }
411 /* Set the control select register */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100412 ret = sca3000_write_reg(st, SCA3000_REG_CTRL_SEL_ADDR, ctrl_reg);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100413 if (ret)
414 goto error_ret;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100415 ret = sca3000_read_data_short(st, SCA3000_REG_CTRL_DATA_ADDR, 1);
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100416 if (ret)
417 goto error_ret;
Janani Ravichandran911568b2016-02-18 16:22:29 -0500418 return st->rx[0];
Jonathan Cameron574fb252009-08-18 18:06:25 +0100419error_ret:
420 return ret;
421}
422
Jonathan Cameron574fb252009-08-18 18:06:25 +0100423/**
Jonathan Camerond7f1c0c2021-03-14 16:46:54 +0000424 * sca3000_print_rev() - sysfs interface to read the chip revision number
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100425 * @indio_dev: Device instance specific generic IIO data.
426 * Driver specific device instance data can be obtained via
427 * via iio_priv(indio_dev)
428 */
Jonathan Cameron7ab9fa0052016-10-08 17:39:14 +0100429static int sca3000_print_rev(struct iio_dev *indio_dev)
Jonathan Cameron574fb252009-08-18 18:06:25 +0100430{
Jonathan Cameron7ab9fa0052016-10-08 17:39:14 +0100431 int ret;
Jonathan Cameron2579a0d2011-10-06 17:14:36 +0100432 struct sca3000_state *st = iio_priv(indio_dev);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100433
Jonathan Cameron574fb252009-08-18 18:06:25 +0100434 mutex_lock(&st->lock);
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100435 ret = sca3000_read_data_short(st, SCA3000_REG_REVID_ADDR, 1);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100436 if (ret < 0)
437 goto error_ret;
Jonathan Cameron7ab9fa0052016-10-08 17:39:14 +0100438 dev_info(&indio_dev->dev,
439 "sca3000 revision major=%lu, minor=%lu\n",
440 st->rx[0] & SCA3000_REG_REVID_MAJOR_MASK,
441 st->rx[0] & SCA3000_REG_REVID_MINOR_MASK);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100442error_ret:
443 mutex_unlock(&st->lock);
444
Jonathan Cameron7ab9fa0052016-10-08 17:39:14 +0100445 return ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +0100446}
447
Jonathan Cameron626f9712016-10-08 17:39:12 +0100448static ssize_t
449sca3000_show_available_3db_freqs(struct device *dev,
450 struct device_attribute *attr,
451 char *buf)
452{
453 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
454 struct sca3000_state *st = iio_priv(indio_dev);
455 int len;
456
457 len = sprintf(buf, "%d", st->info->measurement_mode_3db_freq);
458 if (st->info->option_mode_1)
459 len += sprintf(buf + len, " %d",
460 st->info->option_mode_1_3db_freq);
461 if (st->info->option_mode_2)
462 len += sprintf(buf + len, " %d",
463 st->info->option_mode_2_3db_freq);
464 len += sprintf(buf + len, "\n");
465
466 return len;
467}
Jonathan Cameron574fb252009-08-18 18:06:25 +0100468
Jonathan Cameron626f9712016-10-08 17:39:12 +0100469static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available,
470 S_IRUGO, sca3000_show_available_3db_freqs,
471 NULL, 0);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100472
Lars-Peter Clausen129c3f62013-10-07 15:11:00 +0100473static const struct iio_event_spec sca3000_event = {
474 .type = IIO_EV_TYPE_MAG,
475 .dir = IIO_EV_DIR_RISING,
476 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
477};
Jonathan Cameron574fb252009-08-18 18:06:25 +0100478
Jonathan Cameron152a6a82016-10-08 17:39:06 +0100479/*
480 * Note the hack in the number of bits to pretend we have 2 more than
481 * we do in the fifo.
482 */
Jonathan Cameron691a4ca2012-04-13 10:42:53 +0100483#define SCA3000_CHAN(index, mod) \
484 { \
485 .type = IIO_ACCEL, \
486 .modified = 1, \
487 .channel2 = mod, \
Jonathan Camerona8b21c52013-02-27 19:36:45 +0000488 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100489 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |\
490 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),\
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200491 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
Jonathan Cameron691a4ca2012-04-13 10:42:53 +0100492 .address = index, \
493 .scan_index = index, \
494 .scan_type = { \
495 .sign = 's', \
Jonathan Cameron152a6a82016-10-08 17:39:06 +0100496 .realbits = 13, \
Jonathan Cameron691a4ca2012-04-13 10:42:53 +0100497 .storagebits = 16, \
Jonathan Cameron152a6a82016-10-08 17:39:06 +0100498 .shift = 3, \
499 .endianness = IIO_BE, \
Jonathan Cameron691a4ca2012-04-13 10:42:53 +0100500 }, \
Lars-Peter Clausen129c3f62013-10-07 15:11:00 +0100501 .event_spec = &sca3000_event, \
502 .num_event_specs = 1, \
Clifton Barnesa5211b02016-08-22 22:45:05 -0400503 }
Jonathan Cameron691a4ca2012-04-13 10:42:53 +0100504
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100505static const struct iio_event_spec sca3000_freefall_event_spec = {
506 .type = IIO_EV_TYPE_MAG,
507 .dir = IIO_EV_DIR_FALLING,
508 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
509 BIT(IIO_EV_INFO_PERIOD),
510};
511
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100512static const struct iio_chan_spec sca3000_channels[] = {
Jonathan Cameron691a4ca2012-04-13 10:42:53 +0100513 SCA3000_CHAN(0, IIO_MOD_X),
514 SCA3000_CHAN(1, IIO_MOD_Y),
515 SCA3000_CHAN(2, IIO_MOD_Z),
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100516 {
517 .type = IIO_ACCEL,
518 .modified = 1,
519 .channel2 = IIO_MOD_X_AND_Y_AND_Z,
520 .scan_index = -1, /* Fake channel */
521 .event_spec = &sca3000_freefall_event_spec,
522 .num_event_specs = 1,
523 },
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100524};
525
Peter Meerwaldbb0090e2014-01-13 21:28:00 +0000526static const struct iio_chan_spec sca3000_channels_with_temp[] = {
527 SCA3000_CHAN(0, IIO_MOD_X),
528 SCA3000_CHAN(1, IIO_MOD_Y),
529 SCA3000_CHAN(2, IIO_MOD_Z),
530 {
531 .type = IIO_TEMP,
532 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
533 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
534 BIT(IIO_CHAN_INFO_OFFSET),
Lars-Peter Clausen131e97d2014-11-26 18:55:10 +0100535 /* No buffer support */
536 .scan_index = -1,
Peter Meerwaldbb0090e2014-01-13 21:28:00 +0000537 },
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100538 {
539 .type = IIO_ACCEL,
540 .modified = 1,
541 .channel2 = IIO_MOD_X_AND_Y_AND_Z,
542 .scan_index = -1, /* Fake channel */
543 .event_spec = &sca3000_freefall_event_spec,
544 .num_event_specs = 1,
545 },
Peter Meerwaldbb0090e2014-01-13 21:28:00 +0000546};
547
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100548static u8 sca3000_addresses[3][3] = {
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100549 [0] = {SCA3000_REG_X_MSB_ADDR, SCA3000_REG_CTRL_SEL_MD_X_TH,
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100550 SCA3000_MD_CTRL_OR_X},
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100551 [1] = {SCA3000_REG_Y_MSB_ADDR, SCA3000_REG_CTRL_SEL_MD_Y_TH,
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100552 SCA3000_MD_CTRL_OR_Y},
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100553 [2] = {SCA3000_REG_Z_MSB_ADDR, SCA3000_REG_CTRL_SEL_MD_Z_TH,
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100554 SCA3000_MD_CTRL_OR_Z},
555};
556
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200557/**
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100558 * __sca3000_get_base_freq() - obtain mode specific base frequency
559 * @st: Private driver specific device instance specific state.
560 * @info: chip type specific information.
561 * @base_freq: Base frequency for the current measurement mode.
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200562 *
563 * lock must be held
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100564 */
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200565static inline int __sca3000_get_base_freq(struct sca3000_state *st,
566 const struct sca3000_chip_info *info,
567 int *base_freq)
568{
569 int ret;
570
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100571 ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200572 if (ret)
573 goto error_ret;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100574 switch (SCA3000_REG_MODE_MODE_MASK & st->rx[0]) {
575 case SCA3000_REG_MODE_MEAS_MODE_NORMAL:
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200576 *base_freq = info->measurement_mode_freq;
577 break;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100578 case SCA3000_REG_MODE_MEAS_MODE_OP_1:
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200579 *base_freq = info->option_mode_1_freq;
580 break;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100581 case SCA3000_REG_MODE_MEAS_MODE_OP_2:
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200582 *base_freq = info->option_mode_2_freq;
583 break;
Arnd Bergmanna1427af2016-09-22 11:43:42 +0200584 default:
585 ret = -EINVAL;
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200586 }
587error_ret:
588 return ret;
589}
590
591/**
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100592 * sca3000_read_raw_samp_freq() - read_raw handler for IIO_CHAN_INFO_SAMP_FREQ
593 * @st: Private driver specific device instance specific state.
594 * @val: The frequency read back.
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200595 *
596 * lock must be held
597 **/
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100598static int sca3000_read_raw_samp_freq(struct sca3000_state *st, int *val)
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200599{
600 int ret;
601
602 ret = __sca3000_get_base_freq(st, st->info, val);
603 if (ret)
604 return ret;
605
606 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
607 if (ret < 0)
608 return ret;
609
610 if (*val > 0) {
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100611 ret &= SCA3000_REG_OUT_CTRL_BUF_DIV_MASK;
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200612 switch (ret) {
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100613 case SCA3000_REG_OUT_CTRL_BUF_DIV_2:
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200614 *val /= 2;
615 break;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100616 case SCA3000_REG_OUT_CTRL_BUF_DIV_4:
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200617 *val /= 4;
618 break;
619 }
620 }
621
622 return 0;
623}
624
625/**
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100626 * sca3000_write_raw_samp_freq() - write_raw handler for IIO_CHAN_INFO_SAMP_FREQ
627 * @st: Private driver specific device instance specific state.
628 * @val: The frequency desired.
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200629 *
630 * lock must be held
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100631 */
632static int sca3000_write_raw_samp_freq(struct sca3000_state *st, int val)
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200633{
634 int ret, base_freq, ctrlval;
635
636 ret = __sca3000_get_base_freq(st, st->info, &base_freq);
637 if (ret)
638 return ret;
639
640 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
641 if (ret < 0)
642 return ret;
643
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100644 ctrlval = ret & ~SCA3000_REG_OUT_CTRL_BUF_DIV_MASK;
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200645
646 if (val == base_freq / 2)
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100647 ctrlval |= SCA3000_REG_OUT_CTRL_BUF_DIV_2;
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200648 if (val == base_freq / 4)
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100649 ctrlval |= SCA3000_REG_OUT_CTRL_BUF_DIV_4;
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200650 else if (val != base_freq)
651 return -EINVAL;
652
653 return sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
654 ctrlval);
655}
656
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100657static int sca3000_read_3db_freq(struct sca3000_state *st, int *val)
658{
659 int ret;
660
661 ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
662 if (ret)
663 return ret;
664
665 /* mask bottom 2 bits - only ones that are relevant */
666 st->rx[0] &= SCA3000_REG_MODE_MODE_MASK;
667 switch (st->rx[0]) {
668 case SCA3000_REG_MODE_MEAS_MODE_NORMAL:
669 *val = st->info->measurement_mode_3db_freq;
670 return IIO_VAL_INT;
671 case SCA3000_REG_MODE_MEAS_MODE_MOT_DET:
672 return -EBUSY;
673 case SCA3000_REG_MODE_MEAS_MODE_OP_1:
674 *val = st->info->option_mode_1_3db_freq;
675 return IIO_VAL_INT;
676 case SCA3000_REG_MODE_MEAS_MODE_OP_2:
677 *val = st->info->option_mode_2_3db_freq;
678 return IIO_VAL_INT;
679 default:
680 return -EINVAL;
681 }
682}
683
Jonathan Cameron626f9712016-10-08 17:39:12 +0100684static int sca3000_write_3db_freq(struct sca3000_state *st, int val)
685{
686 int ret;
687 int mode;
688
689 if (val == st->info->measurement_mode_3db_freq)
690 mode = SCA3000_REG_MODE_MEAS_MODE_NORMAL;
691 else if (st->info->option_mode_1 &&
692 (val == st->info->option_mode_1_3db_freq))
693 mode = SCA3000_REG_MODE_MEAS_MODE_OP_1;
694 else if (st->info->option_mode_2 &&
695 (val == st->info->option_mode_2_3db_freq))
696 mode = SCA3000_REG_MODE_MEAS_MODE_OP_2;
697 else
698 return -EINVAL;
699 ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
700 if (ret)
701 return ret;
702
703 st->rx[0] &= ~SCA3000_REG_MODE_MODE_MASK;
704 st->rx[0] |= (mode & SCA3000_REG_MODE_MODE_MASK);
705
706 return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, st->rx[0]);
707}
708
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100709static int sca3000_read_raw(struct iio_dev *indio_dev,
710 struct iio_chan_spec const *chan,
711 int *val,
712 int *val2,
713 long mask)
714{
Jonathan Cameron83f04222011-06-27 13:07:16 +0100715 struct sca3000_state *st = iio_priv(indio_dev);
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100716 int ret;
717 u8 address;
718
719 switch (mask) {
Jonathan Cameron31313fc2012-04-15 17:41:17 +0100720 case IIO_CHAN_INFO_RAW:
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100721 mutex_lock(&st->lock);
Peter Meerwaldbb0090e2014-01-13 21:28:00 +0000722 if (chan->type == IIO_ACCEL) {
723 if (st->mo_det_use_count) {
724 mutex_unlock(&st->lock);
725 return -EBUSY;
726 }
727 address = sca3000_addresses[chan->address][0];
728 ret = sca3000_read_data_short(st, address, 2);
729 if (ret < 0) {
730 mutex_unlock(&st->lock);
731 return ret;
732 }
733 *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
Ioana Ciornei1abe0c92015-10-29 01:01:48 +0200734 *val = ((*val) << (sizeof(*val) * 8 - 13)) >>
735 (sizeof(*val) * 8 - 13);
Peter Meerwaldbb0090e2014-01-13 21:28:00 +0000736 } else {
737 /* get the temperature when available */
738 ret = sca3000_read_data_short(st,
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100739 SCA3000_REG_TEMP_MSB_ADDR,
Ioana Ciornei252b1d82015-10-29 01:01:49 +0200740 2);
Peter Meerwaldbb0090e2014-01-13 21:28:00 +0000741 if (ret < 0) {
742 mutex_unlock(&st->lock);
743 return ret;
744 }
Murilo Opsfelder Araujo2f29c162014-07-08 01:22:00 +0100745 *val = ((st->rx[0] & 0x3F) << 3) |
746 ((st->rx[1] & 0xE0) >> 5);
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100747 }
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100748 mutex_unlock(&st->lock);
749 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100750 case IIO_CHAN_INFO_SCALE:
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100751 *val = 0;
752 if (chan->type == IIO_ACCEL)
753 *val2 = st->info->scale;
754 else /* temperature */
755 *val2 = 555556;
756 return IIO_VAL_INT_PLUS_MICRO;
Peter Meerwaldbb0090e2014-01-13 21:28:00 +0000757 case IIO_CHAN_INFO_OFFSET:
758 *val = -214;
759 *val2 = 600000;
760 return IIO_VAL_INT_PLUS_MICRO;
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200761 case IIO_CHAN_INFO_SAMP_FREQ:
762 mutex_lock(&st->lock);
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100763 ret = sca3000_read_raw_samp_freq(st, val);
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200764 mutex_unlock(&st->lock);
765 return ret ? ret : IIO_VAL_INT;
Jonathan Cameron085fe1b2016-10-08 17:39:10 +0100766 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
767 mutex_lock(&st->lock);
768 ret = sca3000_read_3db_freq(st, val);
769 mutex_unlock(&st->lock);
770 return ret;
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100771 default:
772 return -EINVAL;
773 }
774}
Jonathan Cameron574fb252009-08-18 18:06:25 +0100775
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200776static int sca3000_write_raw(struct iio_dev *indio_dev,
777 struct iio_chan_spec const *chan,
778 int val, int val2, long mask)
779{
780 struct sca3000_state *st = iio_priv(indio_dev);
781 int ret;
782
783 switch (mask) {
784 case IIO_CHAN_INFO_SAMP_FREQ:
785 if (val2)
786 return -EINVAL;
787 mutex_lock(&st->lock);
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100788 ret = sca3000_write_raw_samp_freq(st, val);
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200789 mutex_unlock(&st->lock);
790 return ret;
Jonathan Cameron626f9712016-10-08 17:39:12 +0100791 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
792 if (val2)
793 return -EINVAL;
794 mutex_lock(&st->lock);
795 ret = sca3000_write_3db_freq(st, val);
796 mutex_unlock(&st->lock);
Gustavo A. R. Silvac5b974b2018-07-07 12:44:01 -0500797 return ret;
Ico Doornekampe0f3fc92016-09-13 21:14:14 +0200798 default:
799 return -EINVAL;
800 }
801
802 return ret;
803}
804
Jonathan Cameron574fb252009-08-18 18:06:25 +0100805/**
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100806 * sca3000_read_av_freq() - sysfs function to get available frequencies
807 * @dev: Device structure for this device.
808 * @attr: Description of the attribute.
809 * @buf: Incoming string
Jonathan Cameron574fb252009-08-18 18:06:25 +0100810 *
811 * The later modes are only relevant to the ring buffer - and depend on current
812 * mode. Note that data sheet gives rather wide tolerances for these so integer
813 * division will give good enough answer and not all chips have them specified
814 * at all.
815 **/
816static ssize_t sca3000_read_av_freq(struct device *dev,
Ioana Ciornei252b1d82015-10-29 01:01:49 +0200817 struct device_attribute *attr,
818 char *buf)
Jonathan Cameron574fb252009-08-18 18:06:25 +0100819{
Lars-Peter Clausen4b522ce2012-05-12 15:39:43 +0200820 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron83f04222011-06-27 13:07:16 +0100821 struct sca3000_state *st = iio_priv(indio_dev);
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100822 int len = 0, ret, val;
823
Jonathan Cameron574fb252009-08-18 18:06:25 +0100824 mutex_lock(&st->lock);
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100825 ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100826 val = st->rx[0];
Jonathan Cameron574fb252009-08-18 18:06:25 +0100827 mutex_unlock(&st->lock);
828 if (ret)
829 goto error_ret;
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100830
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100831 switch (val & SCA3000_REG_MODE_MODE_MASK) {
832 case SCA3000_REG_MODE_MEAS_MODE_NORMAL:
Jonathan Cameron574fb252009-08-18 18:06:25 +0100833 len += sprintf(buf + len, "%d %d %d\n",
834 st->info->measurement_mode_freq,
Ioana Ciornei1abe0c92015-10-29 01:01:48 +0200835 st->info->measurement_mode_freq / 2,
836 st->info->measurement_mode_freq / 4);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100837 break;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100838 case SCA3000_REG_MODE_MEAS_MODE_OP_1:
Jonathan Cameron574fb252009-08-18 18:06:25 +0100839 len += sprintf(buf + len, "%d %d %d\n",
840 st->info->option_mode_1_freq,
Ioana Ciornei1abe0c92015-10-29 01:01:48 +0200841 st->info->option_mode_1_freq / 2,
842 st->info->option_mode_1_freq / 4);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100843 break;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +0100844 case SCA3000_REG_MODE_MEAS_MODE_OP_2:
Jonathan Cameron574fb252009-08-18 18:06:25 +0100845 len += sprintf(buf + len, "%d %d %d\n",
846 st->info->option_mode_2_freq,
Ioana Ciornei1abe0c92015-10-29 01:01:48 +0200847 st->info->option_mode_2_freq / 2,
848 st->info->option_mode_2_freq / 4);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100849 break;
Mike Frysingerc608cb02010-05-23 03:10:30 -0400850 }
Jonathan Cameron574fb252009-08-18 18:06:25 +0100851 return len;
852error_ret:
853 return ret;
854}
Ioana Ciornei4a613ad2015-10-29 01:01:51 +0200855
Peter Meerwald5262d8f2014-01-13 21:28:00 +0000856/*
857 * Should only really be registered if ring buffer support is compiled in.
Jonathan Cameron574fb252009-08-18 18:06:25 +0100858 * Does no harm however and doing it right would add a fair bit of complexity
859 */
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100860static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sca3000_read_av_freq);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100861
Lee Jonesfb37b5f2020-07-17 17:55:24 +0100862/*
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100863 * sca3000_read_event_value() - query of a threshold or period
Lee Jonesfb37b5f2020-07-17 17:55:24 +0100864 */
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100865static int sca3000_read_event_value(struct iio_dev *indio_dev,
866 const struct iio_chan_spec *chan,
867 enum iio_event_type type,
868 enum iio_event_direction dir,
869 enum iio_event_info info,
870 int *val, int *val2)
Jonathan Cameron574fb252009-08-18 18:06:25 +0100871{
Jonathan Cameron83f04222011-06-27 13:07:16 +0100872 struct sca3000_state *st = iio_priv(indio_dev);
Dan Carpenteraabcbfe2019-06-21 12:18:28 +0300873 long ret;
874 int i;
Jonathan Cameron74bdc942016-10-08 17:39:11 +0100875
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100876 switch (info) {
877 case IIO_EV_INFO_VALUE:
878 mutex_lock(&st->lock);
Jonathan Cameron74bdc942016-10-08 17:39:11 +0100879 ret = sca3000_read_ctrl_reg(st,
880 sca3000_addresses[chan->address][1]);
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100881 mutex_unlock(&st->lock);
882 if (ret < 0)
883 return ret;
884 *val = 0;
Jonathan Cameron74bdc942016-10-08 17:39:11 +0100885 if (chan->channel2 == IIO_MOD_Y)
Dan Carpenteraabcbfe2019-06-21 12:18:28 +0300886 for_each_set_bit(i, &ret,
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100887 ARRAY_SIZE(st->info->mot_det_mult_y))
888 *val += st->info->mot_det_mult_y[i];
889 else
Dan Carpenteraabcbfe2019-06-21 12:18:28 +0300890 for_each_set_bit(i, &ret,
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100891 ARRAY_SIZE(st->info->mot_det_mult_xz))
892 *val += st->info->mot_det_mult_xz[i];
Murilo Opsfelder Araujod7b79512014-07-08 01:22:00 +0100893
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100894 return IIO_VAL_INT;
895 case IIO_EV_INFO_PERIOD:
896 *val = 0;
897 *val2 = 226000;
898 return IIO_VAL_INT_PLUS_MICRO;
899 default:
900 return -EINVAL;
901 }
Jonathan Cameron574fb252009-08-18 18:06:25 +0100902}
903
904/**
Jonathan Camerond7f1c0c2021-03-14 16:46:54 +0000905 * sca3000_write_event_value() - control of threshold and period
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100906 * @indio_dev: Device instance specific IIO information.
907 * @chan: Description of the channel for which the event is being
908 * configured.
909 * @type: The type of event being configured, here magnitude rising
910 * as everything else is read only.
911 * @dir: Direction of the event (here rising)
912 * @info: What information about the event are we configuring.
913 * Here the threshold only.
914 * @val: Integer part of the value being written..
915 * @val2: Non integer part of the value being written. Here always 0.
916 */
Jonathan Cameron45ae5562016-10-08 17:39:08 +0100917static int sca3000_write_event_value(struct iio_dev *indio_dev,
918 const struct iio_chan_spec *chan,
919 enum iio_event_type type,
920 enum iio_event_direction dir,
921 enum iio_event_info info,
922 int val, int val2)
Jonathan Cameron574fb252009-08-18 18:06:25 +0100923{
Jonathan Cameron83f04222011-06-27 13:07:16 +0100924 struct sca3000_state *st = iio_priv(indio_dev);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100925 int ret;
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100926 int i;
927 u8 nonlinear = 0;
Jonathan Cameron574fb252009-08-18 18:06:25 +0100928
Jonathan Cameron74bdc942016-10-08 17:39:11 +0100929 if (chan->channel2 == IIO_MOD_Y) {
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100930 i = ARRAY_SIZE(st->info->mot_det_mult_y);
931 while (i > 0)
932 if (val >= st->info->mot_det_mult_y[--i]) {
933 nonlinear |= (1 << i);
934 val -= st->info->mot_det_mult_y[i];
935 }
936 } else {
937 i = ARRAY_SIZE(st->info->mot_det_mult_xz);
938 while (i > 0)
939 if (val >= st->info->mot_det_mult_xz[--i]) {
940 nonlinear |= (1 << i);
941 val -= st->info->mot_det_mult_xz[i];
942 }
943 }
944
Jonathan Cameron574fb252009-08-18 18:06:25 +0100945 mutex_lock(&st->lock);
Jonathan Cameron74bdc942016-10-08 17:39:11 +0100946 ret = sca3000_write_ctrl_reg(st,
947 sca3000_addresses[chan->address][1],
948 nonlinear);
Jonathan Cameron574fb252009-08-18 18:06:25 +0100949 mutex_unlock(&st->lock);
950
Jonathan Cameron25888dc2011-05-18 14:41:01 +0100951 return ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +0100952}
953
Jonathan Cameron574fb252009-08-18 18:06:25 +0100954static struct attribute *sca3000_attributes[] = {
Jonathan Cameron626f9712016-10-08 17:39:12 +0100955 &iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.dev_attr.attr,
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100956 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
Jonathan Cameron574fb252009-08-18 18:06:25 +0100957 NULL,
958};
959
Jonathan Cameron574fb252009-08-18 18:06:25 +0100960static const struct attribute_group sca3000_attribute_group = {
961 .attrs = sca3000_attributes,
962};
963
Jonathan Cameron152a6a82016-10-08 17:39:06 +0100964static int sca3000_read_data(struct sca3000_state *st,
965 u8 reg_address_high,
966 u8 *rx,
967 int len)
968{
969 int ret;
970 struct spi_transfer xfer[2] = {
971 {
972 .len = 1,
973 .tx_buf = st->tx,
974 }, {
975 .len = len,
976 .rx_buf = rx,
977 }
978 };
979
980 st->tx[0] = SCA3000_READ_REG(reg_address_high);
981 ret = spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
982 if (ret) {
Christophe JAILLET928edef2020-05-06 05:52:06 +0200983 dev_err(&st->us->dev, "problem reading register\n");
Jonathan Cameron152a6a82016-10-08 17:39:06 +0100984 return ret;
985 }
986
987 return 0;
988}
989
Jonathan Cameron574fb252009-08-18 18:06:25 +0100990/**
Jonathan Cameron2ccf6142016-10-08 17:39:17 +0100991 * sca3000_ring_int_process() - ring specific interrupt handling.
992 * @val: Value of the interrupt status register.
993 * @indio_dev: Device instance specific IIO device structure.
994 */
Jonathan Cameron152a6a82016-10-08 17:39:06 +0100995static void sca3000_ring_int_process(u8 val, struct iio_dev *indio_dev)
Jonathan Cameronced5c032016-10-08 17:39:02 +0100996{
Jonathan Cameron152a6a82016-10-08 17:39:06 +0100997 struct sca3000_state *st = iio_priv(indio_dev);
998 int ret, i, num_available;
999
1000 mutex_lock(&st->lock);
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001001
1002 if (val & SCA3000_REG_INT_STATUS_HALF) {
1003 ret = sca3000_read_data_short(st, SCA3000_REG_BUF_COUNT_ADDR,
Jonathan Cameron152a6a82016-10-08 17:39:06 +01001004 1);
1005 if (ret)
1006 goto error_ret;
1007 num_available = st->rx[0];
1008 /*
1009 * num_available is the total number of samples available
1010 * i.e. number of time points * number of channels.
1011 */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001012 ret = sca3000_read_data(st, SCA3000_REG_RING_OUT_ADDR, st->rx,
Jonathan Cameron152a6a82016-10-08 17:39:06 +01001013 num_available * 2);
1014 if (ret)
1015 goto error_ret;
1016 for (i = 0; i < num_available / 3; i++) {
1017 /*
1018 * Dirty hack to cover for 11 bit in fifo, 13 bit
1019 * direct reading.
1020 *
1021 * In theory the bottom two bits are undefined.
1022 * In reality they appear to always be 0.
1023 */
1024 iio_push_to_buffers(indio_dev, st->rx + i * 3 * 2);
1025 }
Jonathan Cameronced5c032016-10-08 17:39:02 +01001026 }
Jonathan Cameron152a6a82016-10-08 17:39:06 +01001027error_ret:
1028 mutex_unlock(&st->lock);
Jonathan Cameronced5c032016-10-08 17:39:02 +01001029}
1030
1031/**
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001032 * sca3000_event_handler() - handling ring and non ring events
Jonathan Cameron2ccf6142016-10-08 17:39:17 +01001033 * @irq: The irq being handled.
1034 * @private: struct iio_device pointer for the device.
Jonathan Cameron574fb252009-08-18 18:06:25 +01001035 *
Peter Meerwald5262d8f2014-01-13 21:28:00 +00001036 * Ring related interrupt handler. Depending on event, push to
1037 * the ring buffer event chrdev or the event one.
1038 *
Jonathan Cameron574fb252009-08-18 18:06:25 +01001039 * This function is complicated by the fact that the devices can signify ring
1040 * and non ring events via the same interrupt line and they can only
1041 * be distinguished via a read of the relevant status register.
Jonathan Cameron2ccf6142016-10-08 17:39:17 +01001042 */
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001043static irqreturn_t sca3000_event_handler(int irq, void *private)
Jonathan Cameron574fb252009-08-18 18:06:25 +01001044{
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001045 struct iio_dev *indio_dev = private;
Jonathan Cameron83f04222011-06-27 13:07:16 +01001046 struct sca3000_state *st = iio_priv(indio_dev);
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001047 int ret, val;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001048 s64 last_timestamp = iio_get_time_ns(indio_dev);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001049
Peter Meerwald5262d8f2014-01-13 21:28:00 +00001050 /*
1051 * Could lead if badly timed to an extra read of status reg,
Jonathan Cameron574fb252009-08-18 18:06:25 +01001052 * but ensures no interrupt is missed.
1053 */
Jonathan Cameron574fb252009-08-18 18:06:25 +01001054 mutex_lock(&st->lock);
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001055 ret = sca3000_read_data_short(st, SCA3000_REG_INT_STATUS_ADDR, 1);
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001056 val = st->rx[0];
Jonathan Cameron574fb252009-08-18 18:06:25 +01001057 mutex_unlock(&st->lock);
1058 if (ret)
1059 goto done;
1060
Jonathan Cameron152a6a82016-10-08 17:39:06 +01001061 sca3000_ring_int_process(val, indio_dev);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001062
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001063 if (val & SCA3000_INT_STATUS_FREE_FALL)
Jonathan Cameron5aa96182011-08-30 12:41:06 +01001064 iio_push_event(indio_dev,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +01001065 IIO_MOD_EVENT_CODE(IIO_ACCEL,
Jonathan Cameronde9fe322010-10-08 12:14:03 +01001066 0,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +01001067 IIO_MOD_X_AND_Y_AND_Z,
Jonathan Cameronde9fe322010-10-08 12:14:03 +01001068 IIO_EV_TYPE_MAG,
1069 IIO_EV_DIR_FALLING),
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001070 last_timestamp);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001071
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001072 if (val & SCA3000_INT_STATUS_Y_TRIGGER)
Jonathan Cameron5aa96182011-08-30 12:41:06 +01001073 iio_push_event(indio_dev,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +01001074 IIO_MOD_EVENT_CODE(IIO_ACCEL,
Jonathan Cameronde9fe322010-10-08 12:14:03 +01001075 0,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +01001076 IIO_MOD_Y,
Jonathan Cameronde9fe322010-10-08 12:14:03 +01001077 IIO_EV_TYPE_MAG,
1078 IIO_EV_DIR_RISING),
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001079 last_timestamp);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001080
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001081 if (val & SCA3000_INT_STATUS_X_TRIGGER)
Jonathan Cameron5aa96182011-08-30 12:41:06 +01001082 iio_push_event(indio_dev,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +01001083 IIO_MOD_EVENT_CODE(IIO_ACCEL,
Jonathan Cameronde9fe322010-10-08 12:14:03 +01001084 0,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +01001085 IIO_MOD_X,
Jonathan Cameronde9fe322010-10-08 12:14:03 +01001086 IIO_EV_TYPE_MAG,
1087 IIO_EV_DIR_RISING),
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001088 last_timestamp);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001089
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001090 if (val & SCA3000_INT_STATUS_Z_TRIGGER)
Jonathan Cameron5aa96182011-08-30 12:41:06 +01001091 iio_push_event(indio_dev,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +01001092 IIO_MOD_EVENT_CODE(IIO_ACCEL,
Jonathan Cameronde9fe322010-10-08 12:14:03 +01001093 0,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +01001094 IIO_MOD_Z,
Jonathan Cameronde9fe322010-10-08 12:14:03 +01001095 IIO_EV_TYPE_MAG,
1096 IIO_EV_DIR_RISING),
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001097 last_timestamp);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001098
1099done:
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001100 return IRQ_HANDLED;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001101}
1102
Lee Jonesfb37b5f2020-07-17 17:55:24 +01001103/*
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001104 * sca3000_read_event_config() what events are enabled
Lee Jonesfb37b5f2020-07-17 17:55:24 +01001105 */
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001106static int sca3000_read_event_config(struct iio_dev *indio_dev,
Lars-Peter Clausen129c3f62013-10-07 15:11:00 +01001107 const struct iio_chan_spec *chan,
1108 enum iio_event_type type,
1109 enum iio_event_direction dir)
Jonathan Cameron574fb252009-08-18 18:06:25 +01001110{
Jonathan Cameron83f04222011-06-27 13:07:16 +01001111 struct sca3000_state *st = iio_priv(indio_dev);
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001112 int ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001113 /* read current value of mode register */
1114 mutex_lock(&st->lock);
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001115
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001116 ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001117 if (ret)
1118 goto error_ret;
1119
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001120 switch (chan->channel2) {
1121 case IIO_MOD_X_AND_Y_AND_Z:
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001122 ret = !!(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT);
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001123 break;
1124 case IIO_MOD_X:
1125 case IIO_MOD_Y:
1126 case IIO_MOD_Z:
1127 /*
1128 * Motion detection mode cannot run at the same time as
1129 * acceleration data being read.
1130 */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001131 if ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK)
1132 != SCA3000_REG_MODE_MEAS_MODE_MOT_DET) {
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001133 ret = 0;
1134 } else {
1135 ret = sca3000_read_ctrl_reg(st,
1136 SCA3000_REG_CTRL_SEL_MD_CTRL);
1137 if (ret < 0)
1138 goto error_ret;
1139 /* only supporting logical or's for now */
Jonathan Cameron74bdc942016-10-08 17:39:11 +01001140 ret = !!(ret & sca3000_addresses[chan->address][2]);
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001141 }
1142 break;
1143 default:
1144 ret = -EINVAL;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001145 }
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001146
Jonathan Cameron574fb252009-08-18 18:06:25 +01001147error_ret:
1148 mutex_unlock(&st->lock);
1149
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001150 return ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001151}
Ioana Ciornei4a613ad2015-10-29 01:01:51 +02001152
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001153static int sca3000_freefall_set_state(struct iio_dev *indio_dev, int state)
Jonathan Cameron574fb252009-08-18 18:06:25 +01001154{
Jonathan Cameron83f04222011-06-27 13:07:16 +01001155 struct sca3000_state *st = iio_priv(indio_dev);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001156 int ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001157
1158 /* read current value of mode register */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001159 ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001160 if (ret)
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001161 return ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001162
Peter Meerwald5262d8f2014-01-13 21:28:00 +00001163 /* if off and should be on */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001164 if (state && !(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT))
1165 return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR,
1166 st->rx[0] | SCA3000_REG_MODE_FREE_FALL_DETECT);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001167 /* if on and should be off */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001168 else if (!state && (st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT))
1169 return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR,
1170 st->rx[0] & ~SCA3000_REG_MODE_FREE_FALL_DETECT);
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001171 else
1172 return 0;
1173}
Jonathan Cameron574fb252009-08-18 18:06:25 +01001174
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001175static int sca3000_motion_detect_set_state(struct iio_dev *indio_dev, int axis,
1176 int state)
1177{
1178 struct sca3000_state *st = iio_priv(indio_dev);
1179 int ret, ctrlval;
1180
1181 /*
1182 * First read the motion detector config to find out if
1183 * this axis is on
1184 */
1185 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
1186 if (ret < 0)
1187 return ret;
1188 ctrlval = ret;
1189 /* if off and should be on */
1190 if (state && !(ctrlval & sca3000_addresses[axis][2])) {
1191 ret = sca3000_write_ctrl_reg(st,
1192 SCA3000_REG_CTRL_SEL_MD_CTRL,
1193 ctrlval |
1194 sca3000_addresses[axis][2]);
1195 if (ret)
1196 return ret;
1197 st->mo_det_use_count++;
1198 } else if (!state && (ctrlval & sca3000_addresses[axis][2])) {
1199 ret = sca3000_write_ctrl_reg(st,
1200 SCA3000_REG_CTRL_SEL_MD_CTRL,
1201 ctrlval &
1202 ~(sca3000_addresses[axis][2]));
1203 if (ret)
1204 return ret;
1205 st->mo_det_use_count--;
1206 }
1207
1208 /* read current value of mode register */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001209 ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001210 if (ret)
1211 return ret;
1212 /* if off and should be on */
1213 if ((st->mo_det_use_count) &&
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001214 ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK)
1215 != SCA3000_REG_MODE_MEAS_MODE_MOT_DET))
1216 return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR,
1217 (st->rx[0] & ~SCA3000_REG_MODE_MODE_MASK)
1218 | SCA3000_REG_MODE_MEAS_MODE_MOT_DET);
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001219 /* if on and should be off */
1220 else if (!(st->mo_det_use_count) &&
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001221 ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK)
1222 == SCA3000_REG_MODE_MEAS_MODE_MOT_DET))
1223 return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR,
1224 st->rx[0] & SCA3000_REG_MODE_MODE_MASK);
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001225 else
1226 return 0;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001227}
1228
1229/**
Jonathan Cameron2ccf6142016-10-08 17:39:17 +01001230 * sca3000_write_event_config() - simple on off control for motion detector
1231 * @indio_dev: IIO device instance specific structure. Data specific to this
1232 * particular driver may be accessed via iio_priv(indio_dev).
1233 * @chan: Description of the channel whose event we are configuring.
1234 * @type: The type of event.
1235 * @dir: The direction of the event.
1236 * @state: Desired state of event being configured.
Jonathan Cameron574fb252009-08-18 18:06:25 +01001237 *
1238 * This is a per axis control, but enabling any will result in the
1239 * motion detector unit being enabled.
1240 * N.B. enabling motion detector stops normal data acquisition.
1241 * There is a complexity in knowing which mode to return to when
1242 * this mode is disabled. Currently normal mode is assumed.
1243 **/
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001244static int sca3000_write_event_config(struct iio_dev *indio_dev,
Lars-Peter Clausen129c3f62013-10-07 15:11:00 +01001245 const struct iio_chan_spec *chan,
1246 enum iio_event_type type,
1247 enum iio_event_direction dir,
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001248 int state)
Jonathan Cameron574fb252009-08-18 18:06:25 +01001249{
Jonathan Cameron83f04222011-06-27 13:07:16 +01001250 struct sca3000_state *st = iio_priv(indio_dev);
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001251 int ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001252
1253 mutex_lock(&st->lock);
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001254 switch (chan->channel2) {
1255 case IIO_MOD_X_AND_Y_AND_Z:
1256 ret = sca3000_freefall_set_state(indio_dev, state);
1257 break;
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001258
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001259 case IIO_MOD_X:
1260 case IIO_MOD_Y:
1261 case IIO_MOD_Z:
Jonathan Cameron74bdc942016-10-08 17:39:11 +01001262 ret = sca3000_motion_detect_set_state(indio_dev,
1263 chan->address,
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001264 state);
1265 break;
1266 default:
1267 ret = -EINVAL;
1268 break;
1269 }
Jonathan Cameron574fb252009-08-18 18:06:25 +01001270 mutex_unlock(&st->lock);
1271
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001272 return ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001273}
1274
Jonathan Cameronced5c032016-10-08 17:39:02 +01001275static inline
1276int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state)
1277{
1278 struct sca3000_state *st = iio_priv(indio_dev);
1279 int ret;
1280
1281 mutex_lock(&st->lock);
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001282 ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
Jonathan Cameronced5c032016-10-08 17:39:02 +01001283 if (ret)
1284 goto error_ret;
1285 if (state) {
1286 dev_info(&indio_dev->dev, "supposedly enabling ring buffer\n");
1287 ret = sca3000_write_reg(st,
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001288 SCA3000_REG_MODE_ADDR,
1289 (st->rx[0] | SCA3000_REG_MODE_RING_BUF_ENABLE));
Jonathan Cameronced5c032016-10-08 17:39:02 +01001290 } else
1291 ret = sca3000_write_reg(st,
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001292 SCA3000_REG_MODE_ADDR,
1293 (st->rx[0] & ~SCA3000_REG_MODE_RING_BUF_ENABLE));
Jonathan Cameronced5c032016-10-08 17:39:02 +01001294error_ret:
1295 mutex_unlock(&st->lock);
1296
1297 return ret;
1298}
1299
1300/**
Jonathan Cameron2ccf6142016-10-08 17:39:17 +01001301 * sca3000_hw_ring_preenable() - hw ring buffer preenable function
1302 * @indio_dev: structure representing the IIO device. Device instance
1303 * specific state can be accessed via iio_priv(indio_dev).
Jonathan Cameronced5c032016-10-08 17:39:02 +01001304 *
1305 * Very simple enable function as the chip will allows normal reads
1306 * during ring buffer operation so as long as it is indeed running
1307 * before we notify the core, the precise ordering does not matter.
Jonathan Cameron2ccf6142016-10-08 17:39:17 +01001308 */
Jonathan Cameronced5c032016-10-08 17:39:02 +01001309static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
1310{
Jonathan Cameronc19a0252016-10-08 17:39:05 +01001311 int ret;
1312 struct sca3000_state *st = iio_priv(indio_dev);
1313
Jonathan Cameronc19a0252016-10-08 17:39:05 +01001314 mutex_lock(&st->lock);
1315
1316 /* Enable the 50% full interrupt */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001317 ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1);
Jonathan Cameronc19a0252016-10-08 17:39:05 +01001318 if (ret)
1319 goto error_unlock;
1320 ret = sca3000_write_reg(st,
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001321 SCA3000_REG_INT_MASK_ADDR,
1322 st->rx[0] | SCA3000_REG_INT_MASK_RING_HALF);
Jonathan Cameronc19a0252016-10-08 17:39:05 +01001323 if (ret)
1324 goto error_unlock;
1325
1326 mutex_unlock(&st->lock);
1327
Jonathan Cameronced5c032016-10-08 17:39:02 +01001328 return __sca3000_hw_ring_state_set(indio_dev, 1);
Jonathan Cameronc19a0252016-10-08 17:39:05 +01001329
1330error_unlock:
1331 mutex_unlock(&st->lock);
1332
1333 return ret;
Jonathan Cameronced5c032016-10-08 17:39:02 +01001334}
1335
1336static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev)
1337{
Jonathan Cameronc19a0252016-10-08 17:39:05 +01001338 int ret;
1339 struct sca3000_state *st = iio_priv(indio_dev);
1340
1341 ret = __sca3000_hw_ring_state_set(indio_dev, 0);
1342 if (ret)
1343 return ret;
1344
1345 /* Disable the 50% full interrupt */
1346 mutex_lock(&st->lock);
1347
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001348 ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1);
Jonathan Cameronc19a0252016-10-08 17:39:05 +01001349 if (ret)
1350 goto unlock;
1351 ret = sca3000_write_reg(st,
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001352 SCA3000_REG_INT_MASK_ADDR,
1353 st->rx[0] & ~SCA3000_REG_INT_MASK_RING_HALF);
Jonathan Cameronc19a0252016-10-08 17:39:05 +01001354unlock:
1355 mutex_unlock(&st->lock);
1356 return ret;
Jonathan Cameronced5c032016-10-08 17:39:02 +01001357}
1358
1359static const struct iio_buffer_setup_ops sca3000_ring_setup_ops = {
1360 .preenable = &sca3000_hw_ring_preenable,
1361 .postdisable = &sca3000_hw_ring_postdisable,
1362};
1363
Jonathan Cameron574fb252009-08-18 18:06:25 +01001364/**
Jonathan Cameron2ccf6142016-10-08 17:39:17 +01001365 * sca3000_clean_setup() - get the device into a predictable state
1366 * @st: Device instance specific private data structure
Jonathan Cameron574fb252009-08-18 18:06:25 +01001367 *
1368 * Devices use flash memory to store many of the register values
1369 * and hence can come up in somewhat unpredictable states.
1370 * Hence reset everything on driver load.
Jonathan Cameron2ccf6142016-10-08 17:39:17 +01001371 */
Jonathan Cameron574fb252009-08-18 18:06:25 +01001372static int sca3000_clean_setup(struct sca3000_state *st)
1373{
1374 int ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001375
1376 mutex_lock(&st->lock);
1377 /* Ensure all interrupts have been acknowledged */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001378 ret = sca3000_read_data_short(st, SCA3000_REG_INT_STATUS_ADDR, 1);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001379 if (ret)
1380 goto error_ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001381
1382 /* Turn off all motion detection channels */
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001383 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL);
1384 if (ret < 0)
Jonathan Cameron574fb252009-08-18 18:06:25 +01001385 goto error_ret;
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001386 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_MD_CTRL,
1387 ret & SCA3000_MD_CTRL_PROT_MASK);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001388 if (ret)
1389 goto error_ret;
1390
1391 /* Disable ring buffer */
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001392 ret = sca3000_read_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL);
Luis de Bethencourtaea7b1d2016-06-22 20:43:31 +01001393 if (ret < 0)
1394 goto error_ret;
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001395 ret = sca3000_write_ctrl_reg(st, SCA3000_REG_CTRL_SEL_OUT_CTRL,
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001396 (ret & SCA3000_REG_OUT_CTRL_PROT_MASK)
1397 | SCA3000_REG_OUT_CTRL_BUF_X_EN
1398 | SCA3000_REG_OUT_CTRL_BUF_Y_EN
1399 | SCA3000_REG_OUT_CTRL_BUF_Z_EN
1400 | SCA3000_REG_OUT_CTRL_BUF_DIV_4);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001401 if (ret)
1402 goto error_ret;
1403 /* Enable interrupts, relevant to mode and set up as active low */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001404 ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001405 if (ret)
1406 goto error_ret;
1407 ret = sca3000_write_reg(st,
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001408 SCA3000_REG_INT_MASK_ADDR,
1409 (ret & SCA3000_REG_INT_MASK_PROT_MASK)
1410 | SCA3000_REG_INT_MASK_ACTIVE_LOW);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001411 if (ret)
1412 goto error_ret;
Peter Meerwald5262d8f2014-01-13 21:28:00 +00001413 /*
1414 * Select normal measurement mode, free fall off, ring off
1415 * Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5
1416 * as that occurs in one of the example on the datasheet
1417 */
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001418 ret = sca3000_read_data_short(st, SCA3000_REG_MODE_ADDR, 1);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001419 if (ret)
1420 goto error_ret;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001421 ret = sca3000_write_reg(st, SCA3000_REG_MODE_ADDR,
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001422 (st->rx[0] & SCA3000_MODE_PROT_MASK));
Jonathan Cameron574fb252009-08-18 18:06:25 +01001423
1424error_ret:
1425 mutex_unlock(&st->lock);
1426 return ret;
1427}
1428
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001429static const struct iio_info sca3000_info = {
1430 .attrs = &sca3000_attribute_group,
1431 .read_raw = &sca3000_read_raw,
Ico Doornekampe0f3fc92016-09-13 21:14:14 +02001432 .write_raw = &sca3000_write_raw,
Jonathan Cameron45ae5562016-10-08 17:39:08 +01001433 .read_event_value = &sca3000_read_event_value,
1434 .write_event_value = &sca3000_write_event_value,
Lars-Peter Clausencb955852013-12-07 10:45:00 +00001435 .read_event_config = &sca3000_read_event_config,
1436 .write_event_config = &sca3000_write_event_config,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001437};
1438
Bill Pemberton4ae1c612012-11-19 13:21:57 -05001439static int sca3000_probe(struct spi_device *spi)
Jonathan Cameron574fb252009-08-18 18:06:25 +01001440{
Jonathan Camerond2fffd62011-10-14 14:46:58 +01001441 int ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001442 struct sca3000_state *st;
Jonathan Cameron83f04222011-06-27 13:07:16 +01001443 struct iio_dev *indio_dev;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001444
Sachin Kamat0189d932013-08-24 19:48:00 +01001445 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1446 if (!indio_dev)
1447 return -ENOMEM;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001448
Dan Carpenter03bda052011-06-29 22:50:48 +03001449 st = iio_priv(indio_dev);
Jonathan Cameron83f04222011-06-27 13:07:16 +01001450 spi_set_drvdata(spi, indio_dev);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001451 st->us = spi;
1452 mutex_init(&st->lock);
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001453 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi)
1454 ->driver_data];
Jonathan Cameron574fb252009-08-18 18:06:25 +01001455
Jonathan Cameron83f04222011-06-27 13:07:16 +01001456 indio_dev->name = spi_get_device_id(spi)->name;
Peter Meerwaldbb0090e2014-01-13 21:28:00 +00001457 indio_dev->info = &sca3000_info;
1458 if (st->info->temp_output) {
1459 indio_dev->channels = sca3000_channels_with_temp;
1460 indio_dev->num_channels =
1461 ARRAY_SIZE(sca3000_channels_with_temp);
1462 } else {
1463 indio_dev->channels = sca3000_channels;
1464 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels);
1465 }
Jonathan Cameron83f04222011-06-27 13:07:16 +01001466 indio_dev->modes = INDIO_DIRECT_MODE;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001467
Alexandru Ardeleane03ed892021-02-15 12:40:23 +02001468 ret = devm_iio_kfifo_buffer_setup(&spi->dev, indio_dev,
1469 INDIO_BUFFER_SOFTWARE,
1470 &sca3000_ring_setup_ops);
Christophe JAILLETda2d5442018-04-08 21:44:15 +02001471 if (ret)
1472 return ret;
Jonathan Camerond2fffd62011-10-14 14:46:58 +01001473
Jonathan Cameron3e2c96e2011-10-12 10:57:33 +01001474 if (spi->irq) {
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001475 ret = request_threaded_irq(spi->irq,
1476 NULL,
1477 &sca3000_event_handler,
Lars-Peter Clausena91aff12012-07-02 10:54:45 +02001478 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001479 "sca3000",
Jonathan Cameron83f04222011-06-27 13:07:16 +01001480 indio_dev);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001481 if (ret)
Jonathan Cameron9a4936d2016-10-08 17:39:15 +01001482 return ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001483 }
Jonathan Cameron574fb252009-08-18 18:06:25 +01001484 ret = sca3000_clean_setup(st);
1485 if (ret)
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001486 goto error_free_irq;
Jonathan Cameron7ab9fa0052016-10-08 17:39:14 +01001487
1488 ret = sca3000_print_rev(indio_dev);
1489 if (ret)
1490 goto error_free_irq;
1491
Jonathan Cameron9a4936d2016-10-08 17:39:15 +01001492 return iio_device_register(indio_dev);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001493
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001494error_free_irq:
Jonathan Cameron3e2c96e2011-10-12 10:57:33 +01001495 if (spi->irq)
Jonathan Cameron83f04222011-06-27 13:07:16 +01001496 free_irq(spi->irq, indio_dev);
Jonathan Cameron9a4936d2016-10-08 17:39:15 +01001497
Jonathan Cameron574fb252009-08-18 18:06:25 +01001498 return ret;
1499}
1500
1501static int sca3000_stop_all_interrupts(struct sca3000_state *st)
1502{
1503 int ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001504
1505 mutex_lock(&st->lock);
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001506 ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001507 if (ret)
1508 goto error_ret;
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001509 ret = sca3000_write_reg(st, SCA3000_REG_INT_MASK_ADDR,
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001510 (st->rx[0] &
Jonathan Cameron9bc11d32016-10-08 17:39:09 +01001511 ~(SCA3000_REG_INT_MASK_RING_THREE_QUARTER |
1512 SCA3000_REG_INT_MASK_RING_HALF |
1513 SCA3000_REG_INT_MASK_ALL_INTS)));
Jonathan Cameron574fb252009-08-18 18:06:25 +01001514error_ret:
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001515 mutex_unlock(&st->lock);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001516 return ret;
Jonathan Cameron574fb252009-08-18 18:06:25 +01001517}
1518
Bill Pemberton447d4f22012-11-19 13:26:37 -05001519static int sca3000_remove(struct spi_device *spi)
Jonathan Cameron574fb252009-08-18 18:06:25 +01001520{
Jonathan Cameron83f04222011-06-27 13:07:16 +01001521 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1522 struct sca3000_state *st = iio_priv(indio_dev);
Lars-Peter Clausen67ad4e02012-09-22 09:56:00 +01001523
Jonathan Cameron9a4936d2016-10-08 17:39:15 +01001524 iio_device_unregister(indio_dev);
1525
Peter Meerwald5262d8f2014-01-13 21:28:00 +00001526 /* Must ensure no interrupts can be generated after this! */
Lars-Peter Clausen67ad4e02012-09-22 09:56:00 +01001527 sca3000_stop_all_interrupts(st);
Jonathan Cameron3e2c96e2011-10-12 10:57:33 +01001528 if (spi->irq)
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001529 free_irq(spi->irq, indio_dev);
Jonathan Cameron9a4936d2016-10-08 17:39:15 +01001530
Jonathan Cameron574fb252009-08-18 18:06:25 +01001531 return 0;
1532}
1533
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001534static const struct spi_device_id sca3000_id[] = {
1535 {"sca3000_d01", d01},
1536 {"sca3000_e02", e02},
1537 {"sca3000_e04", e04},
1538 {"sca3000_e05", e05},
1539 {}
1540};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +01001541MODULE_DEVICE_TABLE(spi, sca3000_id);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001542
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001543static struct spi_driver sca3000_driver = {
1544 .driver = {
1545 .name = "sca3000",
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001546 },
1547 .probe = sca3000_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -05001548 .remove = sca3000_remove,
Jonathan Cameron25888dc2011-05-18 14:41:01 +01001549 .id_table = sca3000_id,
1550};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +01001551module_spi_driver(sca3000_driver);
Jonathan Cameron574fb252009-08-18 18:06:25 +01001552
Jonathan Cameron0f8c9622012-09-02 21:34:59 +01001553MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron574fb252009-08-18 18:06:25 +01001554MODULE_DESCRIPTION("VTI SCA3000 Series Accelerometers SPI driver");
1555MODULE_LICENSE("GPL v2");