1465c811fSDan Robertson // SPDX-License-Identifier: GPL-2.0-only
2465c811fSDan Robertson /*
3465c811fSDan Robertson * Core IIO driver for Bosch BMA400 triaxial acceleration sensor.
4465c811fSDan Robertson *
5465c811fSDan Robertson * Copyright 2019 Dan Robertson <dan@dlrobertson.com>
6465c811fSDan Robertson *
7465c811fSDan Robertson * TODO:
8465c811fSDan Robertson * - Support for power management
9465c811fSDan Robertson * - Support events and interrupts
10465c811fSDan Robertson * - Create channel for step count
11465c811fSDan Robertson * - Create channel for sensor time
12465c811fSDan Robertson */
13465c811fSDan Robertson
14ffe0ab6aSJagath Jog J #include <linux/bitfield.h>
15465c811fSDan Robertson #include <linux/bitops.h>
16f44c314fSKrzysztof Kozlowski #include <linux/cleanup.h>
17465c811fSDan Robertson #include <linux/device.h>
18465c811fSDan Robertson #include <linux/kernel.h>
19465c811fSDan Robertson #include <linux/module.h>
20465c811fSDan Robertson #include <linux/mutex.h>
21465c811fSDan Robertson #include <linux/regmap.h>
223cf7ded1SDan Robertson #include <linux/regulator/consumer.h>
23d221de60SJagath Jog J #include <linux/slab.h>
24d221de60SJagath Jog J
255f60d5f6SAl Viro #include <linux/unaligned.h>
26465c811fSDan Robertson
271bd2dc6eSJagath Jog J #include <linux/iio/iio.h>
28ffe0ab6aSJagath Jog J #include <linux/iio/buffer.h>
29d024af5bSJagath Jog J #include <linux/iio/events.h>
30961db2daSJagath Jog J #include <linux/iio/sysfs.h>
31ffe0ab6aSJagath Jog J #include <linux/iio/trigger.h>
32ffe0ab6aSJagath Jog J #include <linux/iio/trigger_consumer.h>
33ffe0ab6aSJagath Jog J #include <linux/iio/triggered_buffer.h>
341bd2dc6eSJagath Jog J
35465c811fSDan Robertson #include "bma400.h"
36465c811fSDan Robertson
37465c811fSDan Robertson /*
38465c811fSDan Robertson * The G-range selection may be one of 2g, 4g, 8, or 16g. The scale may
39465c811fSDan Robertson * be selected with the acc_range bits of the ACC_CONFIG1 register.
40465c811fSDan Robertson * NB: This buffer is populated in the device init.
41465c811fSDan Robertson */
42465c811fSDan Robertson static int bma400_scales[8];
43465c811fSDan Robertson
44465c811fSDan Robertson /*
45465c811fSDan Robertson * See the ACC_CONFIG1 section of the datasheet.
46465c811fSDan Robertson * NB: This buffer is populated in the device init.
47465c811fSDan Robertson */
48465c811fSDan Robertson static int bma400_sample_freqs[14];
49465c811fSDan Robertson
50465c811fSDan Robertson static const int bma400_osr_range[] = { 0, 1, 3 };
51465c811fSDan Robertson
52961db2daSJagath Jog J static int tap_reset_timeout[BMA400_TAP_TIM_LIST_LEN] = {
53961db2daSJagath Jog J 300000,
54961db2daSJagath Jog J 400000,
55961db2daSJagath Jog J 500000,
56961db2daSJagath Jog J 600000
57961db2daSJagath Jog J };
58961db2daSJagath Jog J
59961db2daSJagath Jog J static int tap_max2min_time[BMA400_TAP_TIM_LIST_LEN] = {
60961db2daSJagath Jog J 30000,
61961db2daSJagath Jog J 45000,
62961db2daSJagath Jog J 60000,
63961db2daSJagath Jog J 90000
64961db2daSJagath Jog J };
65961db2daSJagath Jog J
66961db2daSJagath Jog J static int double_tap2_min_delay[BMA400_TAP_TIM_LIST_LEN] = {
67961db2daSJagath Jog J 20000,
68961db2daSJagath Jog J 40000,
69961db2daSJagath Jog J 60000,
70961db2daSJagath Jog J 80000
71961db2daSJagath Jog J };
72961db2daSJagath Jog J
73465c811fSDan Robertson /* See the ACC_CONFIG0 section of the datasheet */
74465c811fSDan Robertson enum bma400_power_mode {
75465c811fSDan Robertson POWER_MODE_SLEEP = 0x00,
76465c811fSDan Robertson POWER_MODE_LOW = 0x01,
77465c811fSDan Robertson POWER_MODE_NORMAL = 0x02,
78465c811fSDan Robertson POWER_MODE_INVALID = 0x03,
79465c811fSDan Robertson };
80465c811fSDan Robertson
81ffe0ab6aSJagath Jog J enum bma400_scan {
82ffe0ab6aSJagath Jog J BMA400_ACCL_X,
83ffe0ab6aSJagath Jog J BMA400_ACCL_Y,
84ffe0ab6aSJagath Jog J BMA400_ACCL_Z,
85ffe0ab6aSJagath Jog J BMA400_TEMP,
86ffe0ab6aSJagath Jog J };
87ffe0ab6aSJagath Jog J
88465c811fSDan Robertson struct bma400_sample_freq {
89465c811fSDan Robertson int hz;
90465c811fSDan Robertson int uhz;
91465c811fSDan Robertson };
92465c811fSDan Robertson
9300ee658fSJagath Jog J enum bma400_activity {
9400ee658fSJagath Jog J BMA400_STILL,
9500ee658fSJagath Jog J BMA400_WALKING,
9600ee658fSJagath Jog J BMA400_RUNNING,
9700ee658fSJagath Jog J };
9800ee658fSJagath Jog J
99465c811fSDan Robertson struct bma400_data {
100465c811fSDan Robertson struct device *dev;
101465c811fSDan Robertson struct regmap *regmap;
102465c811fSDan Robertson struct mutex mutex; /* data register lock */
103465c811fSDan Robertson struct iio_mount_matrix orientation;
104465c811fSDan Robertson enum bma400_power_mode power_mode;
105465c811fSDan Robertson struct bma400_sample_freq sample_freq;
106465c811fSDan Robertson int oversampling_ratio;
107465c811fSDan Robertson int scale;
108ffe0ab6aSJagath Jog J struct iio_trigger *trig;
109d221de60SJagath Jog J int steps_enabled;
110d024af5bSJagath Jog J bool step_event_en;
11100ee658fSJagath Jog J bool activity_event_en;
1123cf122c2SJagath Jog J unsigned int generic_event_en;
113961db2daSJagath Jog J unsigned int tap_event_en_bitmask;
114ffe0ab6aSJagath Jog J /* Correct time stamp alignment */
115ffe0ab6aSJagath Jog J struct {
116ffe0ab6aSJagath Jog J __le16 buff[3];
117ffe0ab6aSJagath Jog J u8 temperature;
118ffe0ab6aSJagath Jog J s64 ts __aligned(8);
11912c4efe3SJonathan Cameron } buffer __aligned(IIO_DMA_MINALIGN);
120ffe0ab6aSJagath Jog J __le16 status;
1213cf122c2SJagath Jog J __be16 duration;
122465c811fSDan Robertson };
123465c811fSDan Robertson
bma400_is_writable_reg(struct device * dev,unsigned int reg)124465c811fSDan Robertson static bool bma400_is_writable_reg(struct device *dev, unsigned int reg)
125465c811fSDan Robertson {
126465c811fSDan Robertson switch (reg) {
127465c811fSDan Robertson case BMA400_CHIP_ID_REG:
128465c811fSDan Robertson case BMA400_ERR_REG:
129465c811fSDan Robertson case BMA400_STATUS_REG:
130465c811fSDan Robertson case BMA400_X_AXIS_LSB_REG:
131465c811fSDan Robertson case BMA400_X_AXIS_MSB_REG:
132465c811fSDan Robertson case BMA400_Y_AXIS_LSB_REG:
133465c811fSDan Robertson case BMA400_Y_AXIS_MSB_REG:
134465c811fSDan Robertson case BMA400_Z_AXIS_LSB_REG:
135465c811fSDan Robertson case BMA400_Z_AXIS_MSB_REG:
136465c811fSDan Robertson case BMA400_SENSOR_TIME0:
137465c811fSDan Robertson case BMA400_SENSOR_TIME1:
138465c811fSDan Robertson case BMA400_SENSOR_TIME2:
139465c811fSDan Robertson case BMA400_EVENT_REG:
140465c811fSDan Robertson case BMA400_INT_STAT0_REG:
141465c811fSDan Robertson case BMA400_INT_STAT1_REG:
142465c811fSDan Robertson case BMA400_INT_STAT2_REG:
143465c811fSDan Robertson case BMA400_TEMP_DATA_REG:
144465c811fSDan Robertson case BMA400_FIFO_LENGTH0_REG:
145465c811fSDan Robertson case BMA400_FIFO_LENGTH1_REG:
146465c811fSDan Robertson case BMA400_FIFO_DATA_REG:
147465c811fSDan Robertson case BMA400_STEP_CNT0_REG:
148465c811fSDan Robertson case BMA400_STEP_CNT1_REG:
149465c811fSDan Robertson case BMA400_STEP_CNT3_REG:
150465c811fSDan Robertson case BMA400_STEP_STAT_REG:
151465c811fSDan Robertson return false;
152465c811fSDan Robertson default:
153465c811fSDan Robertson return true;
154465c811fSDan Robertson }
155465c811fSDan Robertson }
156465c811fSDan Robertson
bma400_is_volatile_reg(struct device * dev,unsigned int reg)157465c811fSDan Robertson static bool bma400_is_volatile_reg(struct device *dev, unsigned int reg)
158465c811fSDan Robertson {
159465c811fSDan Robertson switch (reg) {
160465c811fSDan Robertson case BMA400_ERR_REG:
161465c811fSDan Robertson case BMA400_STATUS_REG:
162465c811fSDan Robertson case BMA400_X_AXIS_LSB_REG:
163465c811fSDan Robertson case BMA400_X_AXIS_MSB_REG:
164465c811fSDan Robertson case BMA400_Y_AXIS_LSB_REG:
165465c811fSDan Robertson case BMA400_Y_AXIS_MSB_REG:
166465c811fSDan Robertson case BMA400_Z_AXIS_LSB_REG:
167465c811fSDan Robertson case BMA400_Z_AXIS_MSB_REG:
168465c811fSDan Robertson case BMA400_SENSOR_TIME0:
169465c811fSDan Robertson case BMA400_SENSOR_TIME1:
170465c811fSDan Robertson case BMA400_SENSOR_TIME2:
171465c811fSDan Robertson case BMA400_EVENT_REG:
172465c811fSDan Robertson case BMA400_INT_STAT0_REG:
173465c811fSDan Robertson case BMA400_INT_STAT1_REG:
174465c811fSDan Robertson case BMA400_INT_STAT2_REG:
175465c811fSDan Robertson case BMA400_TEMP_DATA_REG:
176465c811fSDan Robertson case BMA400_FIFO_LENGTH0_REG:
177465c811fSDan Robertson case BMA400_FIFO_LENGTH1_REG:
178465c811fSDan Robertson case BMA400_FIFO_DATA_REG:
179465c811fSDan Robertson case BMA400_STEP_CNT0_REG:
180465c811fSDan Robertson case BMA400_STEP_CNT1_REG:
181465c811fSDan Robertson case BMA400_STEP_CNT3_REG:
182465c811fSDan Robertson case BMA400_STEP_STAT_REG:
183465c811fSDan Robertson return true;
184465c811fSDan Robertson default:
185465c811fSDan Robertson return false;
186465c811fSDan Robertson }
187465c811fSDan Robertson }
188465c811fSDan Robertson
189465c811fSDan Robertson const struct regmap_config bma400_regmap_config = {
190465c811fSDan Robertson .reg_bits = 8,
191465c811fSDan Robertson .val_bits = 8,
192465c811fSDan Robertson .max_register = BMA400_CMD_REG,
193465c811fSDan Robertson .cache_type = REGCACHE_RBTREE,
194465c811fSDan Robertson .writeable_reg = bma400_is_writable_reg,
195465c811fSDan Robertson .volatile_reg = bma400_is_volatile_reg,
196465c811fSDan Robertson };
197f9c02c94SJonathan Cameron EXPORT_SYMBOL_NS(bma400_regmap_config, IIO_BMA400);
198465c811fSDan Robertson
199465c811fSDan Robertson static const struct iio_mount_matrix *
bma400_accel_get_mount_matrix(const struct iio_dev * indio_dev,const struct iio_chan_spec * chan)200465c811fSDan Robertson bma400_accel_get_mount_matrix(const struct iio_dev *indio_dev,
201465c811fSDan Robertson const struct iio_chan_spec *chan)
202465c811fSDan Robertson {
203465c811fSDan Robertson struct bma400_data *data = iio_priv(indio_dev);
204465c811fSDan Robertson
205465c811fSDan Robertson return &data->orientation;
206465c811fSDan Robertson }
207465c811fSDan Robertson
208465c811fSDan Robertson static const struct iio_chan_spec_ext_info bma400_ext_info[] = {
209465c811fSDan Robertson IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bma400_accel_get_mount_matrix),
210465c811fSDan Robertson { }
211465c811fSDan Robertson };
212465c811fSDan Robertson
213d024af5bSJagath Jog J static const struct iio_event_spec bma400_step_detect_event = {
214d024af5bSJagath Jog J .type = IIO_EV_TYPE_CHANGE,
215d024af5bSJagath Jog J .dir = IIO_EV_DIR_NONE,
216d024af5bSJagath Jog J .mask_separate = BIT(IIO_EV_INFO_ENABLE),
217d024af5bSJagath Jog J };
218d024af5bSJagath Jog J
21900ee658fSJagath Jog J static const struct iio_event_spec bma400_activity_event = {
22000ee658fSJagath Jog J .type = IIO_EV_TYPE_CHANGE,
22100ee658fSJagath Jog J .dir = IIO_EV_DIR_NONE,
22200ee658fSJagath Jog J .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE),
22300ee658fSJagath Jog J };
22400ee658fSJagath Jog J
2253cf122c2SJagath Jog J static const struct iio_event_spec bma400_accel_event[] = {
2263cf122c2SJagath Jog J {
2273cf122c2SJagath Jog J .type = IIO_EV_TYPE_MAG,
2283cf122c2SJagath Jog J .dir = IIO_EV_DIR_FALLING,
2293cf122c2SJagath Jog J .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
2303cf122c2SJagath Jog J BIT(IIO_EV_INFO_PERIOD) |
2313cf122c2SJagath Jog J BIT(IIO_EV_INFO_HYSTERESIS) |
2323cf122c2SJagath Jog J BIT(IIO_EV_INFO_ENABLE),
2333cf122c2SJagath Jog J },
2343cf122c2SJagath Jog J {
2353cf122c2SJagath Jog J .type = IIO_EV_TYPE_MAG,
2363cf122c2SJagath Jog J .dir = IIO_EV_DIR_RISING,
2373cf122c2SJagath Jog J .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
2383cf122c2SJagath Jog J BIT(IIO_EV_INFO_PERIOD) |
2393cf122c2SJagath Jog J BIT(IIO_EV_INFO_HYSTERESIS) |
2403cf122c2SJagath Jog J BIT(IIO_EV_INFO_ENABLE),
2413cf122c2SJagath Jog J },
242961db2daSJagath Jog J {
243961db2daSJagath Jog J .type = IIO_EV_TYPE_GESTURE,
244961db2daSJagath Jog J .dir = IIO_EV_DIR_SINGLETAP,
245961db2daSJagath Jog J .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
246961db2daSJagath Jog J BIT(IIO_EV_INFO_ENABLE) |
247961db2daSJagath Jog J BIT(IIO_EV_INFO_RESET_TIMEOUT),
248961db2daSJagath Jog J },
249961db2daSJagath Jog J {
250961db2daSJagath Jog J .type = IIO_EV_TYPE_GESTURE,
251961db2daSJagath Jog J .dir = IIO_EV_DIR_DOUBLETAP,
252961db2daSJagath Jog J .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
253961db2daSJagath Jog J BIT(IIO_EV_INFO_ENABLE) |
254961db2daSJagath Jog J BIT(IIO_EV_INFO_RESET_TIMEOUT) |
255961db2daSJagath Jog J BIT(IIO_EV_INFO_TAP2_MIN_DELAY),
256961db2daSJagath Jog J },
257961db2daSJagath Jog J };
258961db2daSJagath Jog J
usec_to_tapreg_raw(int usec,const int * time_list)259961db2daSJagath Jog J static int usec_to_tapreg_raw(int usec, const int *time_list)
260961db2daSJagath Jog J {
261961db2daSJagath Jog J int index;
262961db2daSJagath Jog J
263961db2daSJagath Jog J for (index = 0; index < BMA400_TAP_TIM_LIST_LEN; index++) {
264961db2daSJagath Jog J if (usec == time_list[index])
265961db2daSJagath Jog J return index;
266961db2daSJagath Jog J }
267961db2daSJagath Jog J return -EINVAL;
268961db2daSJagath Jog J }
269961db2daSJagath Jog J
in_accel_gesture_tap_maxtomin_time_show(struct device * dev,struct device_attribute * attr,char * buf)270961db2daSJagath Jog J static ssize_t in_accel_gesture_tap_maxtomin_time_show(struct device *dev,
271961db2daSJagath Jog J struct device_attribute *attr,
272961db2daSJagath Jog J char *buf)
273961db2daSJagath Jog J {
274961db2daSJagath Jog J struct iio_dev *indio_dev = dev_to_iio_dev(dev);
275961db2daSJagath Jog J struct bma400_data *data = iio_priv(indio_dev);
276961db2daSJagath Jog J int ret, reg_val, raw, vals[2];
277961db2daSJagath Jog J
278961db2daSJagath Jog J ret = regmap_read(data->regmap, BMA400_TAP_CONFIG1, ®_val);
279961db2daSJagath Jog J if (ret)
280961db2daSJagath Jog J return ret;
281961db2daSJagath Jog J
282961db2daSJagath Jog J raw = FIELD_GET(BMA400_TAP_TICSTH_MSK, reg_val);
283961db2daSJagath Jog J vals[0] = 0;
284961db2daSJagath Jog J vals[1] = tap_max2min_time[raw];
285961db2daSJagath Jog J
286961db2daSJagath Jog J return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, vals);
287961db2daSJagath Jog J }
288961db2daSJagath Jog J
in_accel_gesture_tap_maxtomin_time_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)289961db2daSJagath Jog J static ssize_t in_accel_gesture_tap_maxtomin_time_store(struct device *dev,
290961db2daSJagath Jog J struct device_attribute *attr,
291961db2daSJagath Jog J const char *buf, size_t len)
292961db2daSJagath Jog J {
293961db2daSJagath Jog J struct iio_dev *indio_dev = dev_to_iio_dev(dev);
294961db2daSJagath Jog J struct bma400_data *data = iio_priv(indio_dev);
295961db2daSJagath Jog J int ret, val_int, val_fract, raw;
296961db2daSJagath Jog J
297961db2daSJagath Jog J ret = iio_str_to_fixpoint(buf, 100000, &val_int, &val_fract);
298961db2daSJagath Jog J if (ret)
299961db2daSJagath Jog J return ret;
300961db2daSJagath Jog J
301961db2daSJagath Jog J raw = usec_to_tapreg_raw(val_fract, tap_max2min_time);
302961db2daSJagath Jog J if (raw < 0)
303961db2daSJagath Jog J return -EINVAL;
304961db2daSJagath Jog J
305961db2daSJagath Jog J ret = regmap_update_bits(data->regmap, BMA400_TAP_CONFIG1,
306961db2daSJagath Jog J BMA400_TAP_TICSTH_MSK,
307961db2daSJagath Jog J FIELD_PREP(BMA400_TAP_TICSTH_MSK, raw));
308961db2daSJagath Jog J if (ret)
309961db2daSJagath Jog J return ret;
310961db2daSJagath Jog J
311961db2daSJagath Jog J return len;
312961db2daSJagath Jog J }
313961db2daSJagath Jog J
314961db2daSJagath Jog J static IIO_DEVICE_ATTR_RW(in_accel_gesture_tap_maxtomin_time, 0);
315961db2daSJagath Jog J
316961db2daSJagath Jog J /*
317961db2daSJagath Jog J * Tap interrupts works with 200 Hz input data rate and the time based tap
318961db2daSJagath Jog J * controls are in the terms of data samples so the below calculation is
319961db2daSJagath Jog J * used to convert the configuration values into seconds.
320961db2daSJagath Jog J * e.g.:
321961db2daSJagath Jog J * 60 data samples * 0.005 ms = 0.3 seconds.
322961db2daSJagath Jog J * 80 data samples * 0.005 ms = 0.4 seconds.
323961db2daSJagath Jog J */
324961db2daSJagath Jog J
325961db2daSJagath Jog J /* quiet configuration values in seconds */
326961db2daSJagath Jog J static IIO_CONST_ATTR(in_accel_gesture_tap_reset_timeout_available,
327961db2daSJagath Jog J "0.3 0.4 0.5 0.6");
328961db2daSJagath Jog J
329961db2daSJagath Jog J /* tics_th configuration values in seconds */
330961db2daSJagath Jog J static IIO_CONST_ATTR(in_accel_gesture_tap_maxtomin_time_available,
331961db2daSJagath Jog J "0.03 0.045 0.06 0.09");
332961db2daSJagath Jog J
333961db2daSJagath Jog J /* quiet_dt configuration values in seconds */
334961db2daSJagath Jog J static IIO_CONST_ATTR(in_accel_gesture_doubletap_tap2_min_delay_available,
335961db2daSJagath Jog J "0.02 0.04 0.06 0.08");
336961db2daSJagath Jog J
337961db2daSJagath Jog J /* List of sensitivity values available to configure tap interrupts */
338961db2daSJagath Jog J static IIO_CONST_ATTR(in_accel_gesture_tap_value_available, "0 1 2 3 4 5 6 7");
339961db2daSJagath Jog J
340961db2daSJagath Jog J static struct attribute *bma400_event_attributes[] = {
341961db2daSJagath Jog J &iio_const_attr_in_accel_gesture_tap_value_available.dev_attr.attr,
342961db2daSJagath Jog J &iio_const_attr_in_accel_gesture_tap_reset_timeout_available.dev_attr.attr,
343961db2daSJagath Jog J &iio_const_attr_in_accel_gesture_tap_maxtomin_time_available.dev_attr.attr,
344961db2daSJagath Jog J &iio_const_attr_in_accel_gesture_doubletap_tap2_min_delay_available.dev_attr.attr,
345961db2daSJagath Jog J &iio_dev_attr_in_accel_gesture_tap_maxtomin_time.dev_attr.attr,
346961db2daSJagath Jog J NULL
347961db2daSJagath Jog J };
348961db2daSJagath Jog J
349961db2daSJagath Jog J static const struct attribute_group bma400_event_attribute_group = {
350961db2daSJagath Jog J .attrs = bma400_event_attributes,
3513cf122c2SJagath Jog J };
3523cf122c2SJagath Jog J
353ffe0ab6aSJagath Jog J #define BMA400_ACC_CHANNEL(_index, _axis) { \
354465c811fSDan Robertson .type = IIO_ACCEL, \
355465c811fSDan Robertson .modified = 1, \
356465c811fSDan Robertson .channel2 = IIO_MOD_##_axis, \
357465c811fSDan Robertson .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
358465c811fSDan Robertson .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
359465c811fSDan Robertson BIT(IIO_CHAN_INFO_SCALE) | \
360465c811fSDan Robertson BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
361465c811fSDan Robertson .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
362465c811fSDan Robertson BIT(IIO_CHAN_INFO_SCALE) | \
363465c811fSDan Robertson BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
364465c811fSDan Robertson .ext_info = bma400_ext_info, \
365ffe0ab6aSJagath Jog J .scan_index = _index, \
366ffe0ab6aSJagath Jog J .scan_type = { \
367ffe0ab6aSJagath Jog J .sign = 's', \
368ffe0ab6aSJagath Jog J .realbits = 12, \
369ffe0ab6aSJagath Jog J .storagebits = 16, \
370ffe0ab6aSJagath Jog J .endianness = IIO_LE, \
371ffe0ab6aSJagath Jog J }, \
3723cf122c2SJagath Jog J .event_spec = bma400_accel_event, \
3733cf122c2SJagath Jog J .num_event_specs = ARRAY_SIZE(bma400_accel_event) \
374465c811fSDan Robertson }
375465c811fSDan Robertson
37600ee658fSJagath Jog J #define BMA400_ACTIVITY_CHANNEL(_chan2) { \
37700ee658fSJagath Jog J .type = IIO_ACTIVITY, \
37800ee658fSJagath Jog J .modified = 1, \
37900ee658fSJagath Jog J .channel2 = _chan2, \
38000ee658fSJagath Jog J .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
38100ee658fSJagath Jog J .scan_index = -1, /* No buffer support */ \
38200ee658fSJagath Jog J .event_spec = &bma400_activity_event, \
38300ee658fSJagath Jog J .num_event_specs = 1, \
38400ee658fSJagath Jog J }
38500ee658fSJagath Jog J
386465c811fSDan Robertson static const struct iio_chan_spec bma400_channels[] = {
387ffe0ab6aSJagath Jog J BMA400_ACC_CHANNEL(0, X),
388ffe0ab6aSJagath Jog J BMA400_ACC_CHANNEL(1, Y),
389ffe0ab6aSJagath Jog J BMA400_ACC_CHANNEL(2, Z),
390465c811fSDan Robertson {
391465c811fSDan Robertson .type = IIO_TEMP,
392465c811fSDan Robertson .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
393465c811fSDan Robertson .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),
394ffe0ab6aSJagath Jog J .scan_index = 3,
395ffe0ab6aSJagath Jog J .scan_type = {
396ffe0ab6aSJagath Jog J .sign = 's',
397ffe0ab6aSJagath Jog J .realbits = 8,
398ffe0ab6aSJagath Jog J .storagebits = 8,
399ffe0ab6aSJagath Jog J .endianness = IIO_LE,
400465c811fSDan Robertson },
401ffe0ab6aSJagath Jog J },
402d221de60SJagath Jog J {
403d221de60SJagath Jog J .type = IIO_STEPS,
404d221de60SJagath Jog J .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
405d221de60SJagath Jog J BIT(IIO_CHAN_INFO_ENABLE),
406d221de60SJagath Jog J .scan_index = -1, /* No buffer support */
407d024af5bSJagath Jog J .event_spec = &bma400_step_detect_event,
408d024af5bSJagath Jog J .num_event_specs = 1,
409d221de60SJagath Jog J },
41000ee658fSJagath Jog J BMA400_ACTIVITY_CHANNEL(IIO_MOD_STILL),
41100ee658fSJagath Jog J BMA400_ACTIVITY_CHANNEL(IIO_MOD_WALKING),
41200ee658fSJagath Jog J BMA400_ACTIVITY_CHANNEL(IIO_MOD_RUNNING),
413ffe0ab6aSJagath Jog J IIO_CHAN_SOFT_TIMESTAMP(4),
414465c811fSDan Robertson };
415465c811fSDan Robertson
bma400_get_temp_reg(struct bma400_data * data,int * val,int * val2)416465c811fSDan Robertson static int bma400_get_temp_reg(struct bma400_data *data, int *val, int *val2)
417465c811fSDan Robertson {
418465c811fSDan Robertson unsigned int raw_temp;
419465c811fSDan Robertson int host_temp;
420465c811fSDan Robertson int ret;
421465c811fSDan Robertson
422465c811fSDan Robertson if (data->power_mode == POWER_MODE_SLEEP)
423465c811fSDan Robertson return -EBUSY;
424465c811fSDan Robertson
425465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_TEMP_DATA_REG, &raw_temp);
426465c811fSDan Robertson if (ret)
427465c811fSDan Robertson return ret;
428465c811fSDan Robertson
429465c811fSDan Robertson host_temp = sign_extend32(raw_temp, 7);
430465c811fSDan Robertson /*
431465c811fSDan Robertson * The formula for the TEMP_DATA register in the datasheet
432465c811fSDan Robertson * is: x * 0.5 + 23
433465c811fSDan Robertson */
434465c811fSDan Robertson *val = (host_temp >> 1) + 23;
435465c811fSDan Robertson *val2 = (host_temp & 0x1) * 500000;
436465c811fSDan Robertson return IIO_VAL_INT_PLUS_MICRO;
437465c811fSDan Robertson }
438465c811fSDan Robertson
bma400_get_accel_reg(struct bma400_data * data,const struct iio_chan_spec * chan,int * val)439465c811fSDan Robertson static int bma400_get_accel_reg(struct bma400_data *data,
440465c811fSDan Robertson const struct iio_chan_spec *chan,
441465c811fSDan Robertson int *val)
442465c811fSDan Robertson {
443465c811fSDan Robertson __le16 raw_accel;
444465c811fSDan Robertson int lsb_reg;
445465c811fSDan Robertson int ret;
446465c811fSDan Robertson
447465c811fSDan Robertson if (data->power_mode == POWER_MODE_SLEEP)
448465c811fSDan Robertson return -EBUSY;
449465c811fSDan Robertson
450465c811fSDan Robertson switch (chan->channel2) {
451465c811fSDan Robertson case IIO_MOD_X:
452465c811fSDan Robertson lsb_reg = BMA400_X_AXIS_LSB_REG;
453465c811fSDan Robertson break;
454465c811fSDan Robertson case IIO_MOD_Y:
455465c811fSDan Robertson lsb_reg = BMA400_Y_AXIS_LSB_REG;
456465c811fSDan Robertson break;
457465c811fSDan Robertson case IIO_MOD_Z:
458465c811fSDan Robertson lsb_reg = BMA400_Z_AXIS_LSB_REG;
459465c811fSDan Robertson break;
460465c811fSDan Robertson default:
461465c811fSDan Robertson dev_err(data->dev, "invalid axis channel modifier\n");
462465c811fSDan Robertson return -EINVAL;
463465c811fSDan Robertson }
464465c811fSDan Robertson
465465c811fSDan Robertson /* bulk read two registers, with the base being the LSB register */
466465c811fSDan Robertson ret = regmap_bulk_read(data->regmap, lsb_reg, &raw_accel,
467465c811fSDan Robertson sizeof(raw_accel));
468465c811fSDan Robertson if (ret)
469465c811fSDan Robertson return ret;
470465c811fSDan Robertson
471465c811fSDan Robertson *val = sign_extend32(le16_to_cpu(raw_accel), 11);
472465c811fSDan Robertson return IIO_VAL_INT;
473465c811fSDan Robertson }
474465c811fSDan Robertson
bma400_output_data_rate_from_raw(int raw,unsigned int * val,unsigned int * val2)475465c811fSDan Robertson static void bma400_output_data_rate_from_raw(int raw, unsigned int *val,
476465c811fSDan Robertson unsigned int *val2)
477465c811fSDan Robertson {
478465c811fSDan Robertson *val = BMA400_ACC_ODR_MAX_HZ >> (BMA400_ACC_ODR_MAX_RAW - raw);
479465c811fSDan Robertson if (raw > BMA400_ACC_ODR_MIN_RAW)
480465c811fSDan Robertson *val2 = 0;
481465c811fSDan Robertson else
482465c811fSDan Robertson *val2 = 500000;
483465c811fSDan Robertson }
484465c811fSDan Robertson
bma400_get_accel_output_data_rate(struct bma400_data * data)485465c811fSDan Robertson static int bma400_get_accel_output_data_rate(struct bma400_data *data)
486465c811fSDan Robertson {
487465c811fSDan Robertson unsigned int val;
488465c811fSDan Robertson unsigned int odr;
489465c811fSDan Robertson int ret;
490465c811fSDan Robertson
491465c811fSDan Robertson switch (data->power_mode) {
492465c811fSDan Robertson case POWER_MODE_LOW:
493465c811fSDan Robertson /*
494465c811fSDan Robertson * Runs at a fixed rate in low-power mode. See section 4.3
495465c811fSDan Robertson * in the datasheet.
496465c811fSDan Robertson */
497465c811fSDan Robertson bma400_output_data_rate_from_raw(BMA400_ACC_ODR_LP_RAW,
498465c811fSDan Robertson &data->sample_freq.hz,
499465c811fSDan Robertson &data->sample_freq.uhz);
500465c811fSDan Robertson return 0;
501465c811fSDan Robertson case POWER_MODE_NORMAL:
502465c811fSDan Robertson /*
503465c811fSDan Robertson * In normal mode the ODR can be found in the ACC_CONFIG1
504465c811fSDan Robertson * register.
505465c811fSDan Robertson */
506465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_ACC_CONFIG1_REG, &val);
507465c811fSDan Robertson if (ret)
508465c811fSDan Robertson goto error;
509465c811fSDan Robertson
510465c811fSDan Robertson odr = val & BMA400_ACC_ODR_MASK;
511465c811fSDan Robertson if (odr < BMA400_ACC_ODR_MIN_RAW ||
512465c811fSDan Robertson odr > BMA400_ACC_ODR_MAX_RAW) {
513465c811fSDan Robertson ret = -EINVAL;
514465c811fSDan Robertson goto error;
515465c811fSDan Robertson }
516465c811fSDan Robertson
517465c811fSDan Robertson bma400_output_data_rate_from_raw(odr, &data->sample_freq.hz,
518465c811fSDan Robertson &data->sample_freq.uhz);
519465c811fSDan Robertson return 0;
520465c811fSDan Robertson case POWER_MODE_SLEEP:
521465c811fSDan Robertson data->sample_freq.hz = 0;
522465c811fSDan Robertson data->sample_freq.uhz = 0;
523465c811fSDan Robertson return 0;
524465c811fSDan Robertson default:
525465c811fSDan Robertson ret = 0;
526465c811fSDan Robertson goto error;
527465c811fSDan Robertson }
528465c811fSDan Robertson error:
529465c811fSDan Robertson data->sample_freq.hz = -1;
530465c811fSDan Robertson data->sample_freq.uhz = -1;
531465c811fSDan Robertson return ret;
532465c811fSDan Robertson }
533465c811fSDan Robertson
bma400_set_accel_output_data_rate(struct bma400_data * data,int hz,int uhz)534465c811fSDan Robertson static int bma400_set_accel_output_data_rate(struct bma400_data *data,
535465c811fSDan Robertson int hz, int uhz)
536465c811fSDan Robertson {
537465c811fSDan Robertson unsigned int idx;
538465c811fSDan Robertson unsigned int odr;
539465c811fSDan Robertson unsigned int val;
540465c811fSDan Robertson int ret;
541465c811fSDan Robertson
542465c811fSDan Robertson if (hz >= BMA400_ACC_ODR_MIN_WHOLE_HZ) {
543465c811fSDan Robertson if (uhz || hz > BMA400_ACC_ODR_MAX_HZ)
544465c811fSDan Robertson return -EINVAL;
545465c811fSDan Robertson
546465c811fSDan Robertson /* Note this works because MIN_WHOLE_HZ is odd */
547465c811fSDan Robertson idx = __ffs(hz);
548465c811fSDan Robertson
549465c811fSDan Robertson if (hz >> idx != BMA400_ACC_ODR_MIN_WHOLE_HZ)
550465c811fSDan Robertson return -EINVAL;
551465c811fSDan Robertson
552465c811fSDan Robertson idx += BMA400_ACC_ODR_MIN_RAW + 1;
553465c811fSDan Robertson } else if (hz == BMA400_ACC_ODR_MIN_HZ && uhz == 500000) {
554465c811fSDan Robertson idx = BMA400_ACC_ODR_MIN_RAW;
555465c811fSDan Robertson } else {
556465c811fSDan Robertson return -EINVAL;
557465c811fSDan Robertson }
558465c811fSDan Robertson
559465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_ACC_CONFIG1_REG, &val);
560465c811fSDan Robertson if (ret)
561465c811fSDan Robertson return ret;
562465c811fSDan Robertson
563465c811fSDan Robertson /* preserve the range and normal mode osr */
564465c811fSDan Robertson odr = (~BMA400_ACC_ODR_MASK & val) | idx;
565465c811fSDan Robertson
566465c811fSDan Robertson ret = regmap_write(data->regmap, BMA400_ACC_CONFIG1_REG, odr);
567465c811fSDan Robertson if (ret)
568465c811fSDan Robertson return ret;
569465c811fSDan Robertson
570465c811fSDan Robertson bma400_output_data_rate_from_raw(idx, &data->sample_freq.hz,
571465c811fSDan Robertson &data->sample_freq.uhz);
572465c811fSDan Robertson return 0;
573465c811fSDan Robertson }
574465c811fSDan Robertson
bma400_get_accel_oversampling_ratio(struct bma400_data * data)575465c811fSDan Robertson static int bma400_get_accel_oversampling_ratio(struct bma400_data *data)
576465c811fSDan Robertson {
577465c811fSDan Robertson unsigned int val;
578465c811fSDan Robertson unsigned int osr;
579465c811fSDan Robertson int ret;
580465c811fSDan Robertson
581465c811fSDan Robertson /*
582465c811fSDan Robertson * The oversampling ratio is stored in a different register
583465c811fSDan Robertson * based on the power-mode. In normal mode the OSR is stored
584465c811fSDan Robertson * in ACC_CONFIG1. In low-power mode it is stored in
585465c811fSDan Robertson * ACC_CONFIG0.
586465c811fSDan Robertson */
587465c811fSDan Robertson switch (data->power_mode) {
588465c811fSDan Robertson case POWER_MODE_LOW:
589465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_ACC_CONFIG0_REG, &val);
590465c811fSDan Robertson if (ret) {
591465c811fSDan Robertson data->oversampling_ratio = -1;
592465c811fSDan Robertson return ret;
593465c811fSDan Robertson }
594465c811fSDan Robertson
595465c811fSDan Robertson osr = (val & BMA400_LP_OSR_MASK) >> BMA400_LP_OSR_SHIFT;
596465c811fSDan Robertson
597465c811fSDan Robertson data->oversampling_ratio = osr;
598465c811fSDan Robertson return 0;
599465c811fSDan Robertson case POWER_MODE_NORMAL:
600465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_ACC_CONFIG1_REG, &val);
601465c811fSDan Robertson if (ret) {
602465c811fSDan Robertson data->oversampling_ratio = -1;
603465c811fSDan Robertson return ret;
604465c811fSDan Robertson }
605465c811fSDan Robertson
606465c811fSDan Robertson osr = (val & BMA400_NP_OSR_MASK) >> BMA400_NP_OSR_SHIFT;
607465c811fSDan Robertson
608465c811fSDan Robertson data->oversampling_ratio = osr;
609465c811fSDan Robertson return 0;
610465c811fSDan Robertson case POWER_MODE_SLEEP:
611465c811fSDan Robertson data->oversampling_ratio = 0;
612465c811fSDan Robertson return 0;
613465c811fSDan Robertson default:
614465c811fSDan Robertson data->oversampling_ratio = -1;
615465c811fSDan Robertson return -EINVAL;
616465c811fSDan Robertson }
617465c811fSDan Robertson }
618465c811fSDan Robertson
bma400_set_accel_oversampling_ratio(struct bma400_data * data,int val)619465c811fSDan Robertson static int bma400_set_accel_oversampling_ratio(struct bma400_data *data,
620465c811fSDan Robertson int val)
621465c811fSDan Robertson {
622465c811fSDan Robertson unsigned int acc_config;
623465c811fSDan Robertson int ret;
624465c811fSDan Robertson
625465c811fSDan Robertson if (val & ~BMA400_TWO_BITS_MASK)
626465c811fSDan Robertson return -EINVAL;
627465c811fSDan Robertson
628465c811fSDan Robertson /*
629465c811fSDan Robertson * The oversampling ratio is stored in a different register
630465c811fSDan Robertson * based on the power-mode.
631465c811fSDan Robertson */
632465c811fSDan Robertson switch (data->power_mode) {
633465c811fSDan Robertson case POWER_MODE_LOW:
634465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_ACC_CONFIG0_REG,
635465c811fSDan Robertson &acc_config);
636465c811fSDan Robertson if (ret)
637465c811fSDan Robertson return ret;
638465c811fSDan Robertson
639465c811fSDan Robertson ret = regmap_write(data->regmap, BMA400_ACC_CONFIG0_REG,
640465c811fSDan Robertson (acc_config & ~BMA400_LP_OSR_MASK) |
641465c811fSDan Robertson (val << BMA400_LP_OSR_SHIFT));
642465c811fSDan Robertson if (ret) {
643465c811fSDan Robertson dev_err(data->dev, "Failed to write out OSR\n");
644465c811fSDan Robertson return ret;
645465c811fSDan Robertson }
646465c811fSDan Robertson
647465c811fSDan Robertson data->oversampling_ratio = val;
648465c811fSDan Robertson return 0;
649465c811fSDan Robertson case POWER_MODE_NORMAL:
650465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_ACC_CONFIG1_REG,
651465c811fSDan Robertson &acc_config);
652465c811fSDan Robertson if (ret)
653465c811fSDan Robertson return ret;
654465c811fSDan Robertson
655465c811fSDan Robertson ret = regmap_write(data->regmap, BMA400_ACC_CONFIG1_REG,
656465c811fSDan Robertson (acc_config & ~BMA400_NP_OSR_MASK) |
657465c811fSDan Robertson (val << BMA400_NP_OSR_SHIFT));
658465c811fSDan Robertson if (ret) {
659465c811fSDan Robertson dev_err(data->dev, "Failed to write out OSR\n");
660465c811fSDan Robertson return ret;
661465c811fSDan Robertson }
662465c811fSDan Robertson
663465c811fSDan Robertson data->oversampling_ratio = val;
664465c811fSDan Robertson return 0;
665465c811fSDan Robertson default:
666465c811fSDan Robertson return -EINVAL;
667465c811fSDan Robertson }
668465c811fSDan Robertson return ret;
669465c811fSDan Robertson }
670465c811fSDan Robertson
bma400_accel_scale_to_raw(struct bma400_data * data,unsigned int val)671465c811fSDan Robertson static int bma400_accel_scale_to_raw(struct bma400_data *data,
672465c811fSDan Robertson unsigned int val)
673465c811fSDan Robertson {
674465c811fSDan Robertson int raw;
675465c811fSDan Robertson
676465c811fSDan Robertson if (val == 0)
677465c811fSDan Robertson return -EINVAL;
678465c811fSDan Robertson
679465c811fSDan Robertson /* Note this works because BMA400_SCALE_MIN is odd */
680465c811fSDan Robertson raw = __ffs(val);
681465c811fSDan Robertson
682465c811fSDan Robertson if (val >> raw != BMA400_SCALE_MIN)
683465c811fSDan Robertson return -EINVAL;
684465c811fSDan Robertson
685465c811fSDan Robertson return raw;
686465c811fSDan Robertson }
687465c811fSDan Robertson
bma400_get_accel_scale(struct bma400_data * data)688465c811fSDan Robertson static int bma400_get_accel_scale(struct bma400_data *data)
689465c811fSDan Robertson {
690465c811fSDan Robertson unsigned int raw_scale;
691465c811fSDan Robertson unsigned int val;
692465c811fSDan Robertson int ret;
693465c811fSDan Robertson
694465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_ACC_CONFIG1_REG, &val);
695465c811fSDan Robertson if (ret)
696465c811fSDan Robertson return ret;
697465c811fSDan Robertson
698465c811fSDan Robertson raw_scale = (val & BMA400_ACC_SCALE_MASK) >> BMA400_SCALE_SHIFT;
699465c811fSDan Robertson if (raw_scale > BMA400_TWO_BITS_MASK)
700465c811fSDan Robertson return -EINVAL;
701465c811fSDan Robertson
702465c811fSDan Robertson data->scale = BMA400_SCALE_MIN << raw_scale;
703465c811fSDan Robertson
704465c811fSDan Robertson return 0;
705465c811fSDan Robertson }
706465c811fSDan Robertson
bma400_set_accel_scale(struct bma400_data * data,unsigned int val)707465c811fSDan Robertson static int bma400_set_accel_scale(struct bma400_data *data, unsigned int val)
708465c811fSDan Robertson {
709465c811fSDan Robertson unsigned int acc_config;
710465c811fSDan Robertson int raw;
711465c811fSDan Robertson int ret;
712465c811fSDan Robertson
713465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_ACC_CONFIG1_REG, &acc_config);
714465c811fSDan Robertson if (ret)
715465c811fSDan Robertson return ret;
716465c811fSDan Robertson
717465c811fSDan Robertson raw = bma400_accel_scale_to_raw(data, val);
718465c811fSDan Robertson if (raw < 0)
719465c811fSDan Robertson return raw;
720465c811fSDan Robertson
721465c811fSDan Robertson ret = regmap_write(data->regmap, BMA400_ACC_CONFIG1_REG,
722465c811fSDan Robertson (acc_config & ~BMA400_ACC_SCALE_MASK) |
723465c811fSDan Robertson (raw << BMA400_SCALE_SHIFT));
724465c811fSDan Robertson if (ret)
725465c811fSDan Robertson return ret;
726465c811fSDan Robertson
727465c811fSDan Robertson data->scale = val;
728465c811fSDan Robertson return 0;
729465c811fSDan Robertson }
730465c811fSDan Robertson
bma400_get_power_mode(struct bma400_data * data)731465c811fSDan Robertson static int bma400_get_power_mode(struct bma400_data *data)
732465c811fSDan Robertson {
733465c811fSDan Robertson unsigned int val;
734465c811fSDan Robertson int ret;
735465c811fSDan Robertson
736465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_STATUS_REG, &val);
737465c811fSDan Robertson if (ret) {
738465c811fSDan Robertson dev_err(data->dev, "Failed to read status register\n");
739465c811fSDan Robertson return ret;
740465c811fSDan Robertson }
741465c811fSDan Robertson
742465c811fSDan Robertson data->power_mode = (val >> 1) & BMA400_TWO_BITS_MASK;
743465c811fSDan Robertson return 0;
744465c811fSDan Robertson }
745465c811fSDan Robertson
bma400_set_power_mode(struct bma400_data * data,enum bma400_power_mode mode)746465c811fSDan Robertson static int bma400_set_power_mode(struct bma400_data *data,
747465c811fSDan Robertson enum bma400_power_mode mode)
748465c811fSDan Robertson {
749465c811fSDan Robertson unsigned int val;
750465c811fSDan Robertson int ret;
751465c811fSDan Robertson
752465c811fSDan Robertson ret = regmap_read(data->regmap, BMA400_ACC_CONFIG0_REG, &val);
753465c811fSDan Robertson if (ret)
754465c811fSDan Robertson return ret;
755465c811fSDan Robertson
756465c811fSDan Robertson if (data->power_mode == mode)
757465c811fSDan Robertson return 0;
758465c811fSDan Robertson
759465c811fSDan Robertson if (mode == POWER_MODE_INVALID)
760465c811fSDan Robertson return -EINVAL;
761465c811fSDan Robertson
762465c811fSDan Robertson /* Preserve the low-power oversample ratio etc */
763465c811fSDan Robertson ret = regmap_write(data->regmap, BMA400_ACC_CONFIG0_REG,
764465c811fSDan Robertson mode | (val & ~BMA400_TWO_BITS_MASK));
765465c811fSDan Robertson if (ret) {
766465c811fSDan Robertson dev_err(data->dev, "Failed to write to power-mode\n");
767465c811fSDan Robertson return ret;
768465c811fSDan Robertson }
769465c811fSDan Robertson
770465c811fSDan Robertson data->power_mode = mode;
771465c811fSDan Robertson
772465c811fSDan Robertson /*
773465c811fSDan Robertson * Update our cached osr and odr based on the new
774465c811fSDan Robertson * power-mode.
775465c811fSDan Robertson */
776465c811fSDan Robertson bma400_get_accel_output_data_rate(data);
777465c811fSDan Robertson bma400_get_accel_oversampling_ratio(data);
778465c811fSDan Robertson return 0;
779465c811fSDan Robertson }
780465c811fSDan Robertson
bma400_enable_steps(struct bma400_data * data,int val)781d221de60SJagath Jog J static int bma400_enable_steps(struct bma400_data *data, int val)
782d221de60SJagath Jog J {
783d221de60SJagath Jog J int ret;
784d221de60SJagath Jog J
785d221de60SJagath Jog J if (data->steps_enabled == val)
786d221de60SJagath Jog J return 0;
787d221de60SJagath Jog J
788d221de60SJagath Jog J ret = regmap_update_bits(data->regmap, BMA400_INT_CONFIG1_REG,
789d221de60SJagath Jog J BMA400_STEP_INT_MSK,
790d221de60SJagath Jog J FIELD_PREP(BMA400_STEP_INT_MSK, val ? 1 : 0));
791d221de60SJagath Jog J if (ret)
792d221de60SJagath Jog J return ret;
793d221de60SJagath Jog J data->steps_enabled = val;
794d221de60SJagath Jog J return ret;
795d221de60SJagath Jog J }
796d221de60SJagath Jog J
bma400_get_steps_reg(struct bma400_data * data,int * val)797d221de60SJagath Jog J static int bma400_get_steps_reg(struct bma400_data *data, int *val)
798d221de60SJagath Jog J {
799d221de60SJagath Jog J int ret;
800d221de60SJagath Jog J
801f44c314fSKrzysztof Kozlowski u8 *steps_raw __free(kfree) = kmalloc(BMA400_STEP_RAW_LEN, GFP_KERNEL);
802d221de60SJagath Jog J if (!steps_raw)
803d221de60SJagath Jog J return -ENOMEM;
804d221de60SJagath Jog J
805d221de60SJagath Jog J ret = regmap_bulk_read(data->regmap, BMA400_STEP_CNT0_REG,
806d221de60SJagath Jog J steps_raw, BMA400_STEP_RAW_LEN);
807f44c314fSKrzysztof Kozlowski if (ret)
808d221de60SJagath Jog J return ret;
809f44c314fSKrzysztof Kozlowski
810d221de60SJagath Jog J *val = get_unaligned_le24(steps_raw);
811f44c314fSKrzysztof Kozlowski
812d221de60SJagath Jog J return IIO_VAL_INT;
813d221de60SJagath Jog J }
814d221de60SJagath Jog J
bma400_init_tables(void)815465c811fSDan Robertson static void bma400_init_tables(void)
816465c811fSDan Robertson {
817465c811fSDan Robertson int raw;
818465c811fSDan Robertson int i;
819465c811fSDan Robertson
820465c811fSDan Robertson for (i = 0; i + 1 < ARRAY_SIZE(bma400_sample_freqs); i += 2) {
821465c811fSDan Robertson raw = (i / 2) + 5;
822465c811fSDan Robertson bma400_output_data_rate_from_raw(raw, &bma400_sample_freqs[i],
823465c811fSDan Robertson &bma400_sample_freqs[i + 1]);
824465c811fSDan Robertson }
825465c811fSDan Robertson
826465c811fSDan Robertson for (i = 0; i + 1 < ARRAY_SIZE(bma400_scales); i += 2) {
827465c811fSDan Robertson raw = i / 2;
828465c811fSDan Robertson bma400_scales[i] = 0;
829465c811fSDan Robertson bma400_scales[i + 1] = BMA400_SCALE_MIN << raw;
830465c811fSDan Robertson }
831465c811fSDan Robertson }
832465c811fSDan Robertson
bma400_power_disable(void * data_ptr)83312c99f85SJagath Jog J static void bma400_power_disable(void *data_ptr)
83412c99f85SJagath Jog J {
83512c99f85SJagath Jog J struct bma400_data *data = data_ptr;
83612c99f85SJagath Jog J int ret;
83712c99f85SJagath Jog J
83812c99f85SJagath Jog J mutex_lock(&data->mutex);
83912c99f85SJagath Jog J ret = bma400_set_power_mode(data, POWER_MODE_SLEEP);
84012c99f85SJagath Jog J mutex_unlock(&data->mutex);
84112c99f85SJagath Jog J if (ret)
84212c99f85SJagath Jog J dev_warn(data->dev, "Failed to put device into sleep mode (%pe)\n",
84312c99f85SJagath Jog J ERR_PTR(ret));
84412c99f85SJagath Jog J }
84512c99f85SJagath Jog J
bma400_act_to_mod(enum bma400_activity activity)84600ee658fSJagath Jog J static enum iio_modifier bma400_act_to_mod(enum bma400_activity activity)
84700ee658fSJagath Jog J {
84800ee658fSJagath Jog J switch (activity) {
84900ee658fSJagath Jog J case BMA400_STILL:
85000ee658fSJagath Jog J return IIO_MOD_STILL;
85100ee658fSJagath Jog J case BMA400_WALKING:
85200ee658fSJagath Jog J return IIO_MOD_WALKING;
85300ee658fSJagath Jog J case BMA400_RUNNING:
85400ee658fSJagath Jog J return IIO_MOD_RUNNING;
85500ee658fSJagath Jog J default:
85600ee658fSJagath Jog J return IIO_NO_MOD;
85700ee658fSJagath Jog J }
85800ee658fSJagath Jog J }
85900ee658fSJagath Jog J
bma400_init(struct bma400_data * data)860465c811fSDan Robertson static int bma400_init(struct bma400_data *data)
861465c811fSDan Robertson {
8624da9438dSJonathan Cameron static const char * const regulator_names[] = { "vdd", "vddio" };
863465c811fSDan Robertson unsigned int val;
864465c811fSDan Robertson int ret;
865465c811fSDan Robertson
8664da9438dSJonathan Cameron ret = devm_regulator_bulk_get_enable(data->dev,
8674da9438dSJonathan Cameron ARRAY_SIZE(regulator_names),
8684da9438dSJonathan Cameron regulator_names);
8692a22b40aSYuan Can if (ret)
870b4d32eb3SChristophe JAILLET return dev_err_probe(data->dev, ret, "Failed to get regulators\n");
8713cf7ded1SDan Robertson
87257572cacSJonathan Cameron /* Try to read chip_id register. It must return 0x90. */
87357572cacSJonathan Cameron ret = regmap_read(data->regmap, BMA400_CHIP_ID_REG, &val);
87457572cacSJonathan Cameron if (ret) {
87557572cacSJonathan Cameron dev_err(data->dev, "Failed to read chip id register\n");
87657572cacSJonathan Cameron return ret;
87757572cacSJonathan Cameron }
87857572cacSJonathan Cameron
87957572cacSJonathan Cameron if (val != BMA400_ID_REG_VAL) {
88057572cacSJonathan Cameron dev_err(data->dev, "Chip ID mismatch\n");
88157572cacSJonathan Cameron return -ENODEV;
88257572cacSJonathan Cameron }
88357572cacSJonathan Cameron
884465c811fSDan Robertson ret = bma400_get_power_mode(data);
885465c811fSDan Robertson if (ret) {
886465c811fSDan Robertson dev_err(data->dev, "Failed to get the initial power-mode\n");
88712c99f85SJagath Jog J return ret;
888465c811fSDan Robertson }
889465c811fSDan Robertson
890465c811fSDan Robertson if (data->power_mode != POWER_MODE_NORMAL) {
891465c811fSDan Robertson ret = bma400_set_power_mode(data, POWER_MODE_NORMAL);
892465c811fSDan Robertson if (ret) {
893465c811fSDan Robertson dev_err(data->dev, "Failed to wake up the device\n");
89412c99f85SJagath Jog J return ret;
895465c811fSDan Robertson }
896465c811fSDan Robertson /*
897465c811fSDan Robertson * TODO: The datasheet waits 1500us here in the example, but
898465c811fSDan Robertson * lists 2/ODR as the wakeup time.
899465c811fSDan Robertson */
900465c811fSDan Robertson usleep_range(1500, 2000);
901465c811fSDan Robertson }
902465c811fSDan Robertson
90312c99f85SJagath Jog J ret = devm_add_action_or_reset(data->dev, bma400_power_disable, data);
90412c99f85SJagath Jog J if (ret)
90512c99f85SJagath Jog J return ret;
90612c99f85SJagath Jog J
907465c811fSDan Robertson bma400_init_tables();
908465c811fSDan Robertson
909465c811fSDan Robertson ret = bma400_get_accel_output_data_rate(data);
910465c811fSDan Robertson if (ret)
91112c99f85SJagath Jog J return ret;
912465c811fSDan Robertson
913465c811fSDan Robertson ret = bma400_get_accel_oversampling_ratio(data);
914465c811fSDan Robertson if (ret)
91512c99f85SJagath Jog J return ret;
916465c811fSDan Robertson
917465c811fSDan Robertson ret = bma400_get_accel_scale(data);
918465c811fSDan Robertson if (ret)
91912c99f85SJagath Jog J return ret;
920465c811fSDan Robertson
921ffe0ab6aSJagath Jog J /* Configure INT1 pin to open drain */
922ffe0ab6aSJagath Jog J ret = regmap_write(data->regmap, BMA400_INT_IO_CTRL_REG, 0x06);
923ffe0ab6aSJagath Jog J if (ret)
924ffe0ab6aSJagath Jog J return ret;
925465c811fSDan Robertson /*
926465c811fSDan Robertson * Once the interrupt engine is supported we might use the
927465c811fSDan Robertson * data_src_reg, but for now ensure this is set to the
928465c811fSDan Robertson * variable ODR filter selectable by the sample frequency
929465c811fSDan Robertson * channel.
930465c811fSDan Robertson */
931465c811fSDan Robertson return regmap_write(data->regmap, BMA400_ACC_CONFIG2_REG, 0x00);
932465c811fSDan Robertson }
933465c811fSDan Robertson
bma400_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)934465c811fSDan Robertson static int bma400_read_raw(struct iio_dev *indio_dev,
935465c811fSDan Robertson struct iio_chan_spec const *chan, int *val,
936465c811fSDan Robertson int *val2, long mask)
937465c811fSDan Robertson {
938465c811fSDan Robertson struct bma400_data *data = iio_priv(indio_dev);
93900ee658fSJagath Jog J unsigned int activity;
940465c811fSDan Robertson int ret;
941465c811fSDan Robertson
942465c811fSDan Robertson switch (mask) {
943465c811fSDan Robertson case IIO_CHAN_INFO_PROCESSED:
944d221de60SJagath Jog J switch (chan->type) {
945d221de60SJagath Jog J case IIO_TEMP:
946465c811fSDan Robertson mutex_lock(&data->mutex);
947465c811fSDan Robertson ret = bma400_get_temp_reg(data, val, val2);
948465c811fSDan Robertson mutex_unlock(&data->mutex);
949465c811fSDan Robertson return ret;
950d221de60SJagath Jog J case IIO_STEPS:
951d221de60SJagath Jog J return bma400_get_steps_reg(data, val);
95200ee658fSJagath Jog J case IIO_ACTIVITY:
95300ee658fSJagath Jog J ret = regmap_read(data->regmap, BMA400_STEP_STAT_REG,
95400ee658fSJagath Jog J &activity);
95500ee658fSJagath Jog J if (ret)
95600ee658fSJagath Jog J return ret;
95700ee658fSJagath Jog J /*
95800ee658fSJagath Jog J * The device does not support confidence value levels,
95900ee658fSJagath Jog J * so we will always have 100% for current activity and
96000ee658fSJagath Jog J * 0% for the others.
96100ee658fSJagath Jog J */
96200ee658fSJagath Jog J if (chan->channel2 == bma400_act_to_mod(activity))
96300ee658fSJagath Jog J *val = 100;
96400ee658fSJagath Jog J else
96500ee658fSJagath Jog J *val = 0;
96600ee658fSJagath Jog J return IIO_VAL_INT;
967d221de60SJagath Jog J default:
968d221de60SJagath Jog J return -EINVAL;
969d221de60SJagath Jog J }
970465c811fSDan Robertson case IIO_CHAN_INFO_RAW:
971465c811fSDan Robertson mutex_lock(&data->mutex);
972465c811fSDan Robertson ret = bma400_get_accel_reg(data, chan, val);
973465c811fSDan Robertson mutex_unlock(&data->mutex);
974465c811fSDan Robertson return ret;
975465c811fSDan Robertson case IIO_CHAN_INFO_SAMP_FREQ:
976465c811fSDan Robertson switch (chan->type) {
977465c811fSDan Robertson case IIO_ACCEL:
978465c811fSDan Robertson if (data->sample_freq.hz < 0)
979465c811fSDan Robertson return -EINVAL;
980465c811fSDan Robertson
981465c811fSDan Robertson *val = data->sample_freq.hz;
982465c811fSDan Robertson *val2 = data->sample_freq.uhz;
983465c811fSDan Robertson return IIO_VAL_INT_PLUS_MICRO;
984465c811fSDan Robertson case IIO_TEMP:
985465c811fSDan Robertson /*
986465c811fSDan Robertson * Runs at a fixed sampling frequency. See Section 4.4
987465c811fSDan Robertson * of the datasheet.
988465c811fSDan Robertson */
989465c811fSDan Robertson *val = 6;
990465c811fSDan Robertson *val2 = 250000;
991465c811fSDan Robertson return IIO_VAL_INT_PLUS_MICRO;
992465c811fSDan Robertson default:
993465c811fSDan Robertson return -EINVAL;
994465c811fSDan Robertson }
995465c811fSDan Robertson case IIO_CHAN_INFO_SCALE:
996465c811fSDan Robertson *val = 0;
997465c811fSDan Robertson *val2 = data->scale;
998465c811fSDan Robertson return IIO_VAL_INT_PLUS_MICRO;
999465c811fSDan Robertson case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1000465c811fSDan Robertson /*
1001465c811fSDan Robertson * TODO: We could avoid this logic and returning -EINVAL here if
1002465c811fSDan Robertson * we set both the low-power and normal mode OSR registers when
1003465c811fSDan Robertson * we configure the device.
1004465c811fSDan Robertson */
1005465c811fSDan Robertson if (data->oversampling_ratio < 0)
1006465c811fSDan Robertson return -EINVAL;
1007465c811fSDan Robertson
1008465c811fSDan Robertson *val = data->oversampling_ratio;
1009465c811fSDan Robertson return IIO_VAL_INT;
1010d221de60SJagath Jog J case IIO_CHAN_INFO_ENABLE:
1011d221de60SJagath Jog J *val = data->steps_enabled;
1012d221de60SJagath Jog J return IIO_VAL_INT;
1013465c811fSDan Robertson default:
1014465c811fSDan Robertson return -EINVAL;
1015465c811fSDan Robertson }
1016465c811fSDan Robertson }
1017465c811fSDan Robertson
bma400_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)1018465c811fSDan Robertson static int bma400_read_avail(struct iio_dev *indio_dev,
1019465c811fSDan Robertson struct iio_chan_spec const *chan,
1020465c811fSDan Robertson const int **vals, int *type, int *length,
1021465c811fSDan Robertson long mask)
1022465c811fSDan Robertson {
1023465c811fSDan Robertson switch (mask) {
1024465c811fSDan Robertson case IIO_CHAN_INFO_SCALE:
1025465c811fSDan Robertson *type = IIO_VAL_INT_PLUS_MICRO;
1026465c811fSDan Robertson *vals = bma400_scales;
1027465c811fSDan Robertson *length = ARRAY_SIZE(bma400_scales);
1028465c811fSDan Robertson return IIO_AVAIL_LIST;
1029465c811fSDan Robertson case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1030465c811fSDan Robertson *type = IIO_VAL_INT;
1031465c811fSDan Robertson *vals = bma400_osr_range;
1032465c811fSDan Robertson *length = ARRAY_SIZE(bma400_osr_range);
1033465c811fSDan Robertson return IIO_AVAIL_RANGE;
1034465c811fSDan Robertson case IIO_CHAN_INFO_SAMP_FREQ:
1035465c811fSDan Robertson *type = IIO_VAL_INT_PLUS_MICRO;
1036465c811fSDan Robertson *vals = bma400_sample_freqs;
1037465c811fSDan Robertson *length = ARRAY_SIZE(bma400_sample_freqs);
1038465c811fSDan Robertson return IIO_AVAIL_LIST;
1039465c811fSDan Robertson default:
1040465c811fSDan Robertson return -EINVAL;
1041465c811fSDan Robertson }
1042465c811fSDan Robertson }
1043465c811fSDan Robertson
bma400_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)1044465c811fSDan Robertson static int bma400_write_raw(struct iio_dev *indio_dev,
1045465c811fSDan Robertson struct iio_chan_spec const *chan, int val, int val2,
1046465c811fSDan Robertson long mask)
1047465c811fSDan Robertson {
1048465c811fSDan Robertson struct bma400_data *data = iio_priv(indio_dev);
1049465c811fSDan Robertson int ret;
1050465c811fSDan Robertson
1051465c811fSDan Robertson switch (mask) {
1052465c811fSDan Robertson case IIO_CHAN_INFO_SAMP_FREQ:
1053465c811fSDan Robertson /*
1054465c811fSDan Robertson * The sample frequency is readonly for the temperature
1055465c811fSDan Robertson * register and a fixed value in low-power mode.
1056465c811fSDan Robertson */
1057465c811fSDan Robertson if (chan->type != IIO_ACCEL)
1058465c811fSDan Robertson return -EINVAL;
1059465c811fSDan Robertson
1060465c811fSDan Robertson mutex_lock(&data->mutex);
1061465c811fSDan Robertson ret = bma400_set_accel_output_data_rate(data, val, val2);
1062465c811fSDan Robertson mutex_unlock(&data->mutex);
1063465c811fSDan Robertson return ret;
1064465c811fSDan Robertson case IIO_CHAN_INFO_SCALE:
106598496ccdSDan Carpenter if (val != 0 ||
106698496ccdSDan Carpenter val2 < BMA400_SCALE_MIN || val2 > BMA400_SCALE_MAX)
1067465c811fSDan Robertson return -EINVAL;
1068465c811fSDan Robertson
1069465c811fSDan Robertson mutex_lock(&data->mutex);
1070465c811fSDan Robertson ret = bma400_set_accel_scale(data, val2);
1071465c811fSDan Robertson mutex_unlock(&data->mutex);
1072465c811fSDan Robertson return ret;
1073465c811fSDan Robertson case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1074465c811fSDan Robertson mutex_lock(&data->mutex);
1075465c811fSDan Robertson ret = bma400_set_accel_oversampling_ratio(data, val);
1076465c811fSDan Robertson mutex_unlock(&data->mutex);
1077465c811fSDan Robertson return ret;
1078d221de60SJagath Jog J case IIO_CHAN_INFO_ENABLE:
1079d221de60SJagath Jog J mutex_lock(&data->mutex);
1080d221de60SJagath Jog J ret = bma400_enable_steps(data, val);
1081d221de60SJagath Jog J mutex_unlock(&data->mutex);
1082d221de60SJagath Jog J return ret;
1083465c811fSDan Robertson default:
1084465c811fSDan Robertson return -EINVAL;
1085465c811fSDan Robertson }
1086465c811fSDan Robertson }
1087465c811fSDan Robertson
bma400_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)1088465c811fSDan Robertson static int bma400_write_raw_get_fmt(struct iio_dev *indio_dev,
1089465c811fSDan Robertson struct iio_chan_spec const *chan,
1090465c811fSDan Robertson long mask)
1091465c811fSDan Robertson {
1092465c811fSDan Robertson switch (mask) {
1093465c811fSDan Robertson case IIO_CHAN_INFO_SAMP_FREQ:
1094465c811fSDan Robertson return IIO_VAL_INT_PLUS_MICRO;
1095465c811fSDan Robertson case IIO_CHAN_INFO_SCALE:
1096465c811fSDan Robertson return IIO_VAL_INT_PLUS_MICRO;
1097465c811fSDan Robertson case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1098465c811fSDan Robertson return IIO_VAL_INT;
1099d221de60SJagath Jog J case IIO_CHAN_INFO_ENABLE:
1100d221de60SJagath Jog J return IIO_VAL_INT;
1101465c811fSDan Robertson default:
1102465c811fSDan Robertson return -EINVAL;
1103465c811fSDan Robertson }
1104465c811fSDan Robertson }
1105465c811fSDan Robertson
bma400_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)1106d024af5bSJagath Jog J static int bma400_read_event_config(struct iio_dev *indio_dev,
1107d024af5bSJagath Jog J const struct iio_chan_spec *chan,
1108d024af5bSJagath Jog J enum iio_event_type type,
1109d024af5bSJagath Jog J enum iio_event_direction dir)
1110d024af5bSJagath Jog J {
1111d024af5bSJagath Jog J struct bma400_data *data = iio_priv(indio_dev);
1112d024af5bSJagath Jog J
1113d024af5bSJagath Jog J switch (chan->type) {
11143cf122c2SJagath Jog J case IIO_ACCEL:
11153cf122c2SJagath Jog J switch (dir) {
11163cf122c2SJagath Jog J case IIO_EV_DIR_RISING:
11173cf122c2SJagath Jog J return FIELD_GET(BMA400_INT_GEN1_MSK,
11183cf122c2SJagath Jog J data->generic_event_en);
11193cf122c2SJagath Jog J case IIO_EV_DIR_FALLING:
11203cf122c2SJagath Jog J return FIELD_GET(BMA400_INT_GEN2_MSK,
11213cf122c2SJagath Jog J data->generic_event_en);
1122961db2daSJagath Jog J case IIO_EV_DIR_SINGLETAP:
1123961db2daSJagath Jog J return FIELD_GET(BMA400_S_TAP_MSK,
1124961db2daSJagath Jog J data->tap_event_en_bitmask);
1125961db2daSJagath Jog J case IIO_EV_DIR_DOUBLETAP:
1126961db2daSJagath Jog J return FIELD_GET(BMA400_D_TAP_MSK,
1127961db2daSJagath Jog J data->tap_event_en_bitmask);
11283cf122c2SJagath Jog J default:
11293cf122c2SJagath Jog J return -EINVAL;
11303cf122c2SJagath Jog J }
1131d024af5bSJagath Jog J case IIO_STEPS:
1132d024af5bSJagath Jog J return data->step_event_en;
113300ee658fSJagath Jog J case IIO_ACTIVITY:
113400ee658fSJagath Jog J return data->activity_event_en;
1135d024af5bSJagath Jog J default:
1136d024af5bSJagath Jog J return -EINVAL;
1137d024af5bSJagath Jog J }
1138d024af5bSJagath Jog J }
1139d024af5bSJagath Jog J
bma400_steps_event_enable(struct bma400_data * data,int state)1140d024af5bSJagath Jog J static int bma400_steps_event_enable(struct bma400_data *data, int state)
1141d024af5bSJagath Jog J {
1142d024af5bSJagath Jog J int ret;
1143d024af5bSJagath Jog J
1144d024af5bSJagath Jog J ret = bma400_enable_steps(data, 1);
1145d024af5bSJagath Jog J if (ret)
1146d024af5bSJagath Jog J return ret;
1147d024af5bSJagath Jog J
1148d024af5bSJagath Jog J ret = regmap_update_bits(data->regmap, BMA400_INT12_MAP_REG,
1149d024af5bSJagath Jog J BMA400_STEP_INT_MSK,
1150d024af5bSJagath Jog J FIELD_PREP(BMA400_STEP_INT_MSK,
1151d024af5bSJagath Jog J state));
1152d024af5bSJagath Jog J if (ret)
1153d024af5bSJagath Jog J return ret;
1154d024af5bSJagath Jog J data->step_event_en = state;
1155d024af5bSJagath Jog J return 0;
1156d024af5bSJagath Jog J }
1157d024af5bSJagath Jog J
bma400_activity_event_en(struct bma400_data * data,enum iio_event_direction dir,int state)11583cf122c2SJagath Jog J static int bma400_activity_event_en(struct bma400_data *data,
11593cf122c2SJagath Jog J enum iio_event_direction dir,
11603cf122c2SJagath Jog J int state)
11613cf122c2SJagath Jog J {
11626ee2a705SJonathan Cameron int ret, reg, msk, value;
11636ee2a705SJonathan Cameron int field_value = 0;
11643cf122c2SJagath Jog J
11653cf122c2SJagath Jog J switch (dir) {
11663cf122c2SJagath Jog J case IIO_EV_DIR_RISING:
11673cf122c2SJagath Jog J reg = BMA400_GEN1INT_CONFIG0;
11683cf122c2SJagath Jog J msk = BMA400_INT_GEN1_MSK;
11693cf122c2SJagath Jog J value = 2;
11703cf122c2SJagath Jog J set_mask_bits(&field_value, BMA400_INT_GEN1_MSK,
11713cf122c2SJagath Jog J FIELD_PREP(BMA400_INT_GEN1_MSK, state));
11723cf122c2SJagath Jog J break;
11733cf122c2SJagath Jog J case IIO_EV_DIR_FALLING:
11743cf122c2SJagath Jog J reg = BMA400_GEN2INT_CONFIG0;
11753cf122c2SJagath Jog J msk = BMA400_INT_GEN2_MSK;
11763cf122c2SJagath Jog J value = 0;
11773cf122c2SJagath Jog J set_mask_bits(&field_value, BMA400_INT_GEN2_MSK,
11783cf122c2SJagath Jog J FIELD_PREP(BMA400_INT_GEN2_MSK, state));
11793cf122c2SJagath Jog J break;
11803cf122c2SJagath Jog J default:
11813cf122c2SJagath Jog J return -EINVAL;
11823cf122c2SJagath Jog J }
11833cf122c2SJagath Jog J
11843cf122c2SJagath Jog J /* Enabling all axis for interrupt evaluation */
11853cf122c2SJagath Jog J ret = regmap_write(data->regmap, reg, 0xF8);
11863cf122c2SJagath Jog J if (ret)
11873cf122c2SJagath Jog J return ret;
11883cf122c2SJagath Jog J
11893cf122c2SJagath Jog J /* OR combination of all axis for interrupt evaluation */
11903cf122c2SJagath Jog J ret = regmap_write(data->regmap, reg + BMA400_GEN_CONFIG1_OFF, value);
11913cf122c2SJagath Jog J if (ret)
11923cf122c2SJagath Jog J return ret;
11933cf122c2SJagath Jog J
11943cf122c2SJagath Jog J /* Initial value to avoid interrupts while enabling*/
11953cf122c2SJagath Jog J ret = regmap_write(data->regmap, reg + BMA400_GEN_CONFIG2_OFF, 0x0A);
11963cf122c2SJagath Jog J if (ret)
11973cf122c2SJagath Jog J return ret;
11983cf122c2SJagath Jog J
11993cf122c2SJagath Jog J /* Initial duration value to avoid interrupts while enabling*/
12003cf122c2SJagath Jog J ret = regmap_write(data->regmap, reg + BMA400_GEN_CONFIG31_OFF, 0x0F);
12013cf122c2SJagath Jog J if (ret)
12023cf122c2SJagath Jog J return ret;
12033cf122c2SJagath Jog J
12043cf122c2SJagath Jog J ret = regmap_update_bits(data->regmap, BMA400_INT1_MAP_REG, msk,
12053cf122c2SJagath Jog J field_value);
12063cf122c2SJagath Jog J if (ret)
12073cf122c2SJagath Jog J return ret;
12083cf122c2SJagath Jog J
12093cf122c2SJagath Jog J ret = regmap_update_bits(data->regmap, BMA400_INT_CONFIG0_REG, msk,
12103cf122c2SJagath Jog J field_value);
12113cf122c2SJagath Jog J if (ret)
12123cf122c2SJagath Jog J return ret;
12133cf122c2SJagath Jog J
12143cf122c2SJagath Jog J set_mask_bits(&data->generic_event_en, msk, field_value);
12153cf122c2SJagath Jog J return 0;
12163cf122c2SJagath Jog J }
12173cf122c2SJagath Jog J
bma400_tap_event_en(struct bma400_data * data,enum iio_event_direction dir,int state)1218961db2daSJagath Jog J static int bma400_tap_event_en(struct bma400_data *data,
1219961db2daSJagath Jog J enum iio_event_direction dir, int state)
1220961db2daSJagath Jog J {
1221*db9795a4SMikhail Lobanov unsigned int mask;
1222*db9795a4SMikhail Lobanov unsigned int field_value = 0;
1223961db2daSJagath Jog J int ret;
1224961db2daSJagath Jog J
1225961db2daSJagath Jog J /*
1226961db2daSJagath Jog J * Tap interrupts can be configured only in normal mode.
1227961db2daSJagath Jog J * See table in section 4.3 "Power modes - performance modes" of
1228961db2daSJagath Jog J * datasheet v1.2.
1229961db2daSJagath Jog J */
1230961db2daSJagath Jog J if (data->power_mode != POWER_MODE_NORMAL)
1231961db2daSJagath Jog J return -EINVAL;
1232961db2daSJagath Jog J
1233961db2daSJagath Jog J /*
1234961db2daSJagath Jog J * Tap interrupts are operating with a data rate of 200Hz.
1235961db2daSJagath Jog J * See section 4.7 "Tap sensing interrupt" in datasheet v1.2.
1236961db2daSJagath Jog J */
1237961db2daSJagath Jog J if (data->sample_freq.hz != 200 && state) {
1238961db2daSJagath Jog J dev_err(data->dev, "Invalid data rate for tap interrupts.\n");
1239961db2daSJagath Jog J return -EINVAL;
1240961db2daSJagath Jog J }
1241961db2daSJagath Jog J
1242961db2daSJagath Jog J ret = regmap_update_bits(data->regmap, BMA400_INT12_MAP_REG,
1243961db2daSJagath Jog J BMA400_S_TAP_MSK,
1244961db2daSJagath Jog J FIELD_PREP(BMA400_S_TAP_MSK, state));
1245961db2daSJagath Jog J if (ret)
1246961db2daSJagath Jog J return ret;
1247961db2daSJagath Jog J
1248961db2daSJagath Jog J switch (dir) {
1249961db2daSJagath Jog J case IIO_EV_DIR_SINGLETAP:
1250961db2daSJagath Jog J mask = BMA400_S_TAP_MSK;
1251961db2daSJagath Jog J set_mask_bits(&field_value, BMA400_S_TAP_MSK,
1252961db2daSJagath Jog J FIELD_PREP(BMA400_S_TAP_MSK, state));
1253961db2daSJagath Jog J break;
1254961db2daSJagath Jog J case IIO_EV_DIR_DOUBLETAP:
1255961db2daSJagath Jog J mask = BMA400_D_TAP_MSK;
1256961db2daSJagath Jog J set_mask_bits(&field_value, BMA400_D_TAP_MSK,
1257961db2daSJagath Jog J FIELD_PREP(BMA400_D_TAP_MSK, state));
1258961db2daSJagath Jog J break;
1259961db2daSJagath Jog J default:
1260961db2daSJagath Jog J return -EINVAL;
1261961db2daSJagath Jog J }
1262961db2daSJagath Jog J
1263961db2daSJagath Jog J ret = regmap_update_bits(data->regmap, BMA400_INT_CONFIG1_REG, mask,
1264961db2daSJagath Jog J field_value);
1265961db2daSJagath Jog J if (ret)
1266961db2daSJagath Jog J return ret;
1267961db2daSJagath Jog J
1268961db2daSJagath Jog J set_mask_bits(&data->tap_event_en_bitmask, mask, field_value);
1269961db2daSJagath Jog J
1270961db2daSJagath Jog J return 0;
1271961db2daSJagath Jog J }
1272961db2daSJagath Jog J
bma400_disable_adv_interrupt(struct bma400_data * data)1273961db2daSJagath Jog J static int bma400_disable_adv_interrupt(struct bma400_data *data)
1274961db2daSJagath Jog J {
1275961db2daSJagath Jog J int ret;
1276961db2daSJagath Jog J
1277961db2daSJagath Jog J ret = regmap_write(data->regmap, BMA400_INT_CONFIG0_REG, 0);
1278961db2daSJagath Jog J if (ret)
1279961db2daSJagath Jog J return ret;
1280961db2daSJagath Jog J
1281961db2daSJagath Jog J ret = regmap_write(data->regmap, BMA400_INT_CONFIG1_REG, 0);
1282961db2daSJagath Jog J if (ret)
1283961db2daSJagath Jog J return ret;
1284961db2daSJagath Jog J
1285961db2daSJagath Jog J data->tap_event_en_bitmask = 0;
1286961db2daSJagath Jog J data->generic_event_en = 0;
1287961db2daSJagath Jog J data->step_event_en = false;
1288961db2daSJagath Jog J data->activity_event_en = false;
1289961db2daSJagath Jog J
1290961db2daSJagath Jog J return 0;
1291961db2daSJagath Jog J }
1292961db2daSJagath Jog J
bma400_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)1293d024af5bSJagath Jog J static int bma400_write_event_config(struct iio_dev *indio_dev,
1294d024af5bSJagath Jog J const struct iio_chan_spec *chan,
1295d024af5bSJagath Jog J enum iio_event_type type,
1296d024af5bSJagath Jog J enum iio_event_direction dir, int state)
1297d024af5bSJagath Jog J {
1298d024af5bSJagath Jog J struct bma400_data *data = iio_priv(indio_dev);
1299d024af5bSJagath Jog J int ret;
1300d024af5bSJagath Jog J
1301d024af5bSJagath Jog J switch (chan->type) {
13023cf122c2SJagath Jog J case IIO_ACCEL:
1303961db2daSJagath Jog J switch (type) {
1304961db2daSJagath Jog J case IIO_EV_TYPE_MAG:
13053cf122c2SJagath Jog J mutex_lock(&data->mutex);
13063cf122c2SJagath Jog J ret = bma400_activity_event_en(data, dir, state);
13073cf122c2SJagath Jog J mutex_unlock(&data->mutex);
13083cf122c2SJagath Jog J return ret;
1309961db2daSJagath Jog J case IIO_EV_TYPE_GESTURE:
1310961db2daSJagath Jog J mutex_lock(&data->mutex);
1311961db2daSJagath Jog J ret = bma400_tap_event_en(data, dir, state);
1312961db2daSJagath Jog J mutex_unlock(&data->mutex);
1313961db2daSJagath Jog J return ret;
1314961db2daSJagath Jog J default:
1315961db2daSJagath Jog J return -EINVAL;
1316961db2daSJagath Jog J }
1317d024af5bSJagath Jog J case IIO_STEPS:
1318d024af5bSJagath Jog J mutex_lock(&data->mutex);
1319d024af5bSJagath Jog J ret = bma400_steps_event_enable(data, state);
1320d024af5bSJagath Jog J mutex_unlock(&data->mutex);
1321d024af5bSJagath Jog J return ret;
132200ee658fSJagath Jog J case IIO_ACTIVITY:
132300ee658fSJagath Jog J mutex_lock(&data->mutex);
132400ee658fSJagath Jog J if (!data->step_event_en) {
132500ee658fSJagath Jog J ret = bma400_steps_event_enable(data, true);
132600ee658fSJagath Jog J if (ret) {
132700ee658fSJagath Jog J mutex_unlock(&data->mutex);
132800ee658fSJagath Jog J return ret;
132900ee658fSJagath Jog J }
133000ee658fSJagath Jog J }
133100ee658fSJagath Jog J data->activity_event_en = state;
133200ee658fSJagath Jog J mutex_unlock(&data->mutex);
133300ee658fSJagath Jog J return 0;
1334d024af5bSJagath Jog J default:
1335d024af5bSJagath Jog J return -EINVAL;
1336d024af5bSJagath Jog J }
1337d024af5bSJagath Jog J }
1338d024af5bSJagath Jog J
get_gen_config_reg(enum iio_event_direction dir)13393cf122c2SJagath Jog J static int get_gen_config_reg(enum iio_event_direction dir)
13403cf122c2SJagath Jog J {
13413cf122c2SJagath Jog J switch (dir) {
13423cf122c2SJagath Jog J case IIO_EV_DIR_FALLING:
13433cf122c2SJagath Jog J return BMA400_GEN2INT_CONFIG0;
13443cf122c2SJagath Jog J case IIO_EV_DIR_RISING:
13453cf122c2SJagath Jog J return BMA400_GEN1INT_CONFIG0;
13463cf122c2SJagath Jog J default:
13473cf122c2SJagath Jog J return -EINVAL;
13483cf122c2SJagath Jog J }
13493cf122c2SJagath Jog J }
13503cf122c2SJagath Jog J
bma400_read_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)13513cf122c2SJagath Jog J static int bma400_read_event_value(struct iio_dev *indio_dev,
13523cf122c2SJagath Jog J const struct iio_chan_spec *chan,
13533cf122c2SJagath Jog J enum iio_event_type type,
13543cf122c2SJagath Jog J enum iio_event_direction dir,
13553cf122c2SJagath Jog J enum iio_event_info info,
13563cf122c2SJagath Jog J int *val, int *val2)
13573cf122c2SJagath Jog J {
13583cf122c2SJagath Jog J struct bma400_data *data = iio_priv(indio_dev);
1359961db2daSJagath Jog J int ret, reg, reg_val, raw;
13603cf122c2SJagath Jog J
1361961db2daSJagath Jog J if (chan->type != IIO_ACCEL)
1362961db2daSJagath Jog J return -EINVAL;
1363961db2daSJagath Jog J
1364961db2daSJagath Jog J switch (type) {
1365961db2daSJagath Jog J case IIO_EV_TYPE_MAG:
13663cf122c2SJagath Jog J reg = get_gen_config_reg(dir);
13673cf122c2SJagath Jog J if (reg < 0)
13683cf122c2SJagath Jog J return -EINVAL;
13693cf122c2SJagath Jog J
13703cf122c2SJagath Jog J *val2 = 0;
13713cf122c2SJagath Jog J switch (info) {
13723cf122c2SJagath Jog J case IIO_EV_INFO_VALUE:
13733cf122c2SJagath Jog J ret = regmap_read(data->regmap,
13743cf122c2SJagath Jog J reg + BMA400_GEN_CONFIG2_OFF,
13753cf122c2SJagath Jog J val);
13763cf122c2SJagath Jog J if (ret)
13773cf122c2SJagath Jog J return ret;
13783cf122c2SJagath Jog J return IIO_VAL_INT;
13793cf122c2SJagath Jog J case IIO_EV_INFO_PERIOD:
13803cf122c2SJagath Jog J mutex_lock(&data->mutex);
13813cf122c2SJagath Jog J ret = regmap_bulk_read(data->regmap,
13823cf122c2SJagath Jog J reg + BMA400_GEN_CONFIG3_OFF,
13833cf122c2SJagath Jog J &data->duration,
13843cf122c2SJagath Jog J sizeof(data->duration));
13853cf122c2SJagath Jog J if (ret) {
13863cf122c2SJagath Jog J mutex_unlock(&data->mutex);
13873cf122c2SJagath Jog J return ret;
13883cf122c2SJagath Jog J }
13893cf122c2SJagath Jog J *val = be16_to_cpu(data->duration);
13903cf122c2SJagath Jog J mutex_unlock(&data->mutex);
13913cf122c2SJagath Jog J return IIO_VAL_INT;
13923cf122c2SJagath Jog J case IIO_EV_INFO_HYSTERESIS:
13933cf122c2SJagath Jog J ret = regmap_read(data->regmap, reg, val);
13943cf122c2SJagath Jog J if (ret)
13953cf122c2SJagath Jog J return ret;
13963cf122c2SJagath Jog J *val = FIELD_GET(BMA400_GEN_HYST_MSK, *val);
13973cf122c2SJagath Jog J return IIO_VAL_INT;
13983cf122c2SJagath Jog J default:
13993cf122c2SJagath Jog J return -EINVAL;
14003cf122c2SJagath Jog J }
1401961db2daSJagath Jog J case IIO_EV_TYPE_GESTURE:
1402961db2daSJagath Jog J switch (info) {
1403961db2daSJagath Jog J case IIO_EV_INFO_VALUE:
1404961db2daSJagath Jog J ret = regmap_read(data->regmap, BMA400_TAP_CONFIG,
1405961db2daSJagath Jog J ®_val);
1406961db2daSJagath Jog J if (ret)
1407961db2daSJagath Jog J return ret;
1408961db2daSJagath Jog J
1409961db2daSJagath Jog J *val = FIELD_GET(BMA400_TAP_SEN_MSK, reg_val);
1410961db2daSJagath Jog J return IIO_VAL_INT;
1411961db2daSJagath Jog J case IIO_EV_INFO_RESET_TIMEOUT:
1412961db2daSJagath Jog J ret = regmap_read(data->regmap, BMA400_TAP_CONFIG1,
1413961db2daSJagath Jog J ®_val);
1414961db2daSJagath Jog J if (ret)
1415961db2daSJagath Jog J return ret;
1416961db2daSJagath Jog J
1417961db2daSJagath Jog J raw = FIELD_GET(BMA400_TAP_QUIET_MSK, reg_val);
1418961db2daSJagath Jog J *val = 0;
1419961db2daSJagath Jog J *val2 = tap_reset_timeout[raw];
1420961db2daSJagath Jog J return IIO_VAL_INT_PLUS_MICRO;
1421961db2daSJagath Jog J case IIO_EV_INFO_TAP2_MIN_DELAY:
1422961db2daSJagath Jog J ret = regmap_read(data->regmap, BMA400_TAP_CONFIG1,
1423961db2daSJagath Jog J ®_val);
1424961db2daSJagath Jog J if (ret)
1425961db2daSJagath Jog J return ret;
1426961db2daSJagath Jog J
1427961db2daSJagath Jog J raw = FIELD_GET(BMA400_TAP_QUIETDT_MSK, reg_val);
1428961db2daSJagath Jog J *val = 0;
1429961db2daSJagath Jog J *val2 = double_tap2_min_delay[raw];
1430961db2daSJagath Jog J return IIO_VAL_INT_PLUS_MICRO;
1431961db2daSJagath Jog J default:
1432961db2daSJagath Jog J return -EINVAL;
1433961db2daSJagath Jog J }
14343cf122c2SJagath Jog J default:
14353cf122c2SJagath Jog J return -EINVAL;
14363cf122c2SJagath Jog J }
14373cf122c2SJagath Jog J }
14383cf122c2SJagath Jog J
bma400_write_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)14393cf122c2SJagath Jog J static int bma400_write_event_value(struct iio_dev *indio_dev,
14403cf122c2SJagath Jog J const struct iio_chan_spec *chan,
14413cf122c2SJagath Jog J enum iio_event_type type,
14423cf122c2SJagath Jog J enum iio_event_direction dir,
14433cf122c2SJagath Jog J enum iio_event_info info,
14443cf122c2SJagath Jog J int val, int val2)
14453cf122c2SJagath Jog J {
14463cf122c2SJagath Jog J struct bma400_data *data = iio_priv(indio_dev);
1447961db2daSJagath Jog J int reg, ret, raw;
14483cf122c2SJagath Jog J
1449961db2daSJagath Jog J if (chan->type != IIO_ACCEL)
1450961db2daSJagath Jog J return -EINVAL;
1451961db2daSJagath Jog J
1452961db2daSJagath Jog J switch (type) {
1453961db2daSJagath Jog J case IIO_EV_TYPE_MAG:
14543cf122c2SJagath Jog J reg = get_gen_config_reg(dir);
14553cf122c2SJagath Jog J if (reg < 0)
14563cf122c2SJagath Jog J return -EINVAL;
14573cf122c2SJagath Jog J
14583cf122c2SJagath Jog J switch (info) {
14593cf122c2SJagath Jog J case IIO_EV_INFO_VALUE:
14603cf122c2SJagath Jog J if (val < 1 || val > 255)
14613cf122c2SJagath Jog J return -EINVAL;
14623cf122c2SJagath Jog J
14633cf122c2SJagath Jog J return regmap_write(data->regmap,
14643cf122c2SJagath Jog J reg + BMA400_GEN_CONFIG2_OFF,
14653cf122c2SJagath Jog J val);
14663cf122c2SJagath Jog J case IIO_EV_INFO_PERIOD:
14673cf122c2SJagath Jog J if (val < 1 || val > 65535)
14683cf122c2SJagath Jog J return -EINVAL;
14693cf122c2SJagath Jog J
14703cf122c2SJagath Jog J mutex_lock(&data->mutex);
14713cf122c2SJagath Jog J put_unaligned_be16(val, &data->duration);
14723cf122c2SJagath Jog J ret = regmap_bulk_write(data->regmap,
14733cf122c2SJagath Jog J reg + BMA400_GEN_CONFIG3_OFF,
14743cf122c2SJagath Jog J &data->duration,
14753cf122c2SJagath Jog J sizeof(data->duration));
14763cf122c2SJagath Jog J mutex_unlock(&data->mutex);
14773cf122c2SJagath Jog J return ret;
14783cf122c2SJagath Jog J case IIO_EV_INFO_HYSTERESIS:
14793cf122c2SJagath Jog J if (val < 0 || val > 3)
14803cf122c2SJagath Jog J return -EINVAL;
14813cf122c2SJagath Jog J
14823cf122c2SJagath Jog J return regmap_update_bits(data->regmap, reg,
14833cf122c2SJagath Jog J BMA400_GEN_HYST_MSK,
14843cf122c2SJagath Jog J FIELD_PREP(BMA400_GEN_HYST_MSK,
14853cf122c2SJagath Jog J val));
14863cf122c2SJagath Jog J default:
14873cf122c2SJagath Jog J return -EINVAL;
14883cf122c2SJagath Jog J }
1489961db2daSJagath Jog J case IIO_EV_TYPE_GESTURE:
1490961db2daSJagath Jog J switch (info) {
1491961db2daSJagath Jog J case IIO_EV_INFO_VALUE:
1492961db2daSJagath Jog J if (val < 0 || val > 7)
1493961db2daSJagath Jog J return -EINVAL;
1494961db2daSJagath Jog J
1495961db2daSJagath Jog J return regmap_update_bits(data->regmap,
1496961db2daSJagath Jog J BMA400_TAP_CONFIG,
1497961db2daSJagath Jog J BMA400_TAP_SEN_MSK,
1498961db2daSJagath Jog J FIELD_PREP(BMA400_TAP_SEN_MSK,
1499961db2daSJagath Jog J val));
1500961db2daSJagath Jog J case IIO_EV_INFO_RESET_TIMEOUT:
1501961db2daSJagath Jog J raw = usec_to_tapreg_raw(val2, tap_reset_timeout);
1502961db2daSJagath Jog J if (raw < 0)
1503961db2daSJagath Jog J return -EINVAL;
1504961db2daSJagath Jog J
1505961db2daSJagath Jog J return regmap_update_bits(data->regmap,
1506961db2daSJagath Jog J BMA400_TAP_CONFIG1,
1507961db2daSJagath Jog J BMA400_TAP_QUIET_MSK,
1508961db2daSJagath Jog J FIELD_PREP(BMA400_TAP_QUIET_MSK,
1509961db2daSJagath Jog J raw));
1510961db2daSJagath Jog J case IIO_EV_INFO_TAP2_MIN_DELAY:
1511961db2daSJagath Jog J raw = usec_to_tapreg_raw(val2, double_tap2_min_delay);
1512961db2daSJagath Jog J if (raw < 0)
1513961db2daSJagath Jog J return -EINVAL;
1514961db2daSJagath Jog J
1515961db2daSJagath Jog J return regmap_update_bits(data->regmap,
1516961db2daSJagath Jog J BMA400_TAP_CONFIG1,
1517961db2daSJagath Jog J BMA400_TAP_QUIETDT_MSK,
1518961db2daSJagath Jog J FIELD_PREP(BMA400_TAP_QUIETDT_MSK,
1519961db2daSJagath Jog J raw));
1520961db2daSJagath Jog J default:
1521961db2daSJagath Jog J return -EINVAL;
1522961db2daSJagath Jog J }
15233cf122c2SJagath Jog J default:
15243cf122c2SJagath Jog J return -EINVAL;
15253cf122c2SJagath Jog J }
15263cf122c2SJagath Jog J }
15273cf122c2SJagath Jog J
bma400_data_rdy_trigger_set_state(struct iio_trigger * trig,bool state)1528ffe0ab6aSJagath Jog J static int bma400_data_rdy_trigger_set_state(struct iio_trigger *trig,
1529ffe0ab6aSJagath Jog J bool state)
1530ffe0ab6aSJagath Jog J {
1531ffe0ab6aSJagath Jog J struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1532ffe0ab6aSJagath Jog J struct bma400_data *data = iio_priv(indio_dev);
1533ffe0ab6aSJagath Jog J int ret;
1534ffe0ab6aSJagath Jog J
1535ffe0ab6aSJagath Jog J ret = regmap_update_bits(data->regmap, BMA400_INT_CONFIG0_REG,
1536ffe0ab6aSJagath Jog J BMA400_INT_DRDY_MSK,
1537ffe0ab6aSJagath Jog J FIELD_PREP(BMA400_INT_DRDY_MSK, state));
1538ffe0ab6aSJagath Jog J if (ret)
1539ffe0ab6aSJagath Jog J return ret;
1540ffe0ab6aSJagath Jog J
1541ffe0ab6aSJagath Jog J return regmap_update_bits(data->regmap, BMA400_INT1_MAP_REG,
1542ffe0ab6aSJagath Jog J BMA400_INT_DRDY_MSK,
1543ffe0ab6aSJagath Jog J FIELD_PREP(BMA400_INT_DRDY_MSK, state));
1544ffe0ab6aSJagath Jog J }
1545ffe0ab6aSJagath Jog J
1546ffe0ab6aSJagath Jog J static const unsigned long bma400_avail_scan_masks[] = {
1547ffe0ab6aSJagath Jog J BIT(BMA400_ACCL_X) | BIT(BMA400_ACCL_Y) | BIT(BMA400_ACCL_Z),
1548ffe0ab6aSJagath Jog J BIT(BMA400_ACCL_X) | BIT(BMA400_ACCL_Y) | BIT(BMA400_ACCL_Z)
1549ffe0ab6aSJagath Jog J | BIT(BMA400_TEMP),
1550ffe0ab6aSJagath Jog J 0
1551ffe0ab6aSJagath Jog J };
1552ffe0ab6aSJagath Jog J
1553465c811fSDan Robertson static const struct iio_info bma400_info = {
1554465c811fSDan Robertson .read_raw = bma400_read_raw,
1555465c811fSDan Robertson .read_avail = bma400_read_avail,
1556465c811fSDan Robertson .write_raw = bma400_write_raw,
1557465c811fSDan Robertson .write_raw_get_fmt = bma400_write_raw_get_fmt,
1558d024af5bSJagath Jog J .read_event_config = bma400_read_event_config,
1559d024af5bSJagath Jog J .write_event_config = bma400_write_event_config,
15603cf122c2SJagath Jog J .write_event_value = bma400_write_event_value,
15613cf122c2SJagath Jog J .read_event_value = bma400_read_event_value,
1562961db2daSJagath Jog J .event_attrs = &bma400_event_attribute_group,
1563465c811fSDan Robertson };
1564465c811fSDan Robertson
1565ffe0ab6aSJagath Jog J static const struct iio_trigger_ops bma400_trigger_ops = {
1566ffe0ab6aSJagath Jog J .set_trigger_state = &bma400_data_rdy_trigger_set_state,
1567ffe0ab6aSJagath Jog J .validate_device = &iio_trigger_validate_own_device,
1568ffe0ab6aSJagath Jog J };
1569ffe0ab6aSJagath Jog J
bma400_trigger_handler(int irq,void * p)1570ffe0ab6aSJagath Jog J static irqreturn_t bma400_trigger_handler(int irq, void *p)
1571ffe0ab6aSJagath Jog J {
1572ffe0ab6aSJagath Jog J struct iio_poll_func *pf = p;
1573ffe0ab6aSJagath Jog J struct iio_dev *indio_dev = pf->indio_dev;
1574ffe0ab6aSJagath Jog J struct bma400_data *data = iio_priv(indio_dev);
1575ffe0ab6aSJagath Jog J int ret, temp;
1576ffe0ab6aSJagath Jog J
1577ffe0ab6aSJagath Jog J /* Lock to protect the data->buffer */
1578ffe0ab6aSJagath Jog J mutex_lock(&data->mutex);
1579ffe0ab6aSJagath Jog J
1580ffe0ab6aSJagath Jog J /* bulk read six registers, with the base being the LSB register */
1581ffe0ab6aSJagath Jog J ret = regmap_bulk_read(data->regmap, BMA400_X_AXIS_LSB_REG,
1582ffe0ab6aSJagath Jog J &data->buffer.buff, sizeof(data->buffer.buff));
1583ffe0ab6aSJagath Jog J if (ret)
1584ffe0ab6aSJagath Jog J goto unlock_err;
1585ffe0ab6aSJagath Jog J
1586ffe0ab6aSJagath Jog J if (test_bit(BMA400_TEMP, indio_dev->active_scan_mask)) {
1587ffe0ab6aSJagath Jog J ret = regmap_read(data->regmap, BMA400_TEMP_DATA_REG, &temp);
1588ffe0ab6aSJagath Jog J if (ret)
1589ffe0ab6aSJagath Jog J goto unlock_err;
1590ffe0ab6aSJagath Jog J
1591ffe0ab6aSJagath Jog J data->buffer.temperature = temp;
1592ffe0ab6aSJagath Jog J }
1593ffe0ab6aSJagath Jog J
1594ffe0ab6aSJagath Jog J iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
1595ffe0ab6aSJagath Jog J iio_get_time_ns(indio_dev));
1596ffe0ab6aSJagath Jog J
1597ffe0ab6aSJagath Jog J mutex_unlock(&data->mutex);
1598ffe0ab6aSJagath Jog J iio_trigger_notify_done(indio_dev->trig);
1599ffe0ab6aSJagath Jog J return IRQ_HANDLED;
1600ffe0ab6aSJagath Jog J
1601ffe0ab6aSJagath Jog J unlock_err:
1602ffe0ab6aSJagath Jog J mutex_unlock(&data->mutex);
1603ffe0ab6aSJagath Jog J return IRQ_NONE;
1604ffe0ab6aSJagath Jog J }
1605ffe0ab6aSJagath Jog J
bma400_interrupt(int irq,void * private)1606ffe0ab6aSJagath Jog J static irqreturn_t bma400_interrupt(int irq, void *private)
1607ffe0ab6aSJagath Jog J {
1608ffe0ab6aSJagath Jog J struct iio_dev *indio_dev = private;
1609ffe0ab6aSJagath Jog J struct bma400_data *data = iio_priv(indio_dev);
1610d024af5bSJagath Jog J s64 timestamp = iio_get_time_ns(indio_dev);
16113cf122c2SJagath Jog J unsigned int act, ev_dir = IIO_EV_DIR_NONE;
1612ffe0ab6aSJagath Jog J int ret;
1613ffe0ab6aSJagath Jog J
1614ffe0ab6aSJagath Jog J /* Lock to protect the data->status */
1615ffe0ab6aSJagath Jog J mutex_lock(&data->mutex);
1616ffe0ab6aSJagath Jog J ret = regmap_bulk_read(data->regmap, BMA400_INT_STAT0_REG,
1617ffe0ab6aSJagath Jog J &data->status,
1618ffe0ab6aSJagath Jog J sizeof(data->status));
1619ffe0ab6aSJagath Jog J /*
1620ffe0ab6aSJagath Jog J * if none of the bit is set in the status register then it is
1621ffe0ab6aSJagath Jog J * spurious interrupt.
1622ffe0ab6aSJagath Jog J */
1623ffe0ab6aSJagath Jog J if (ret || !data->status)
1624ffe0ab6aSJagath Jog J goto unlock_err;
1625ffe0ab6aSJagath Jog J
1626961db2daSJagath Jog J /*
1627961db2daSJagath Jog J * Disable all advance interrupts if interrupt engine overrun occurs.
1628961db2daSJagath Jog J * See section 4.7 "Interrupt engine overrun" in datasheet v1.2.
1629961db2daSJagath Jog J */
1630961db2daSJagath Jog J if (FIELD_GET(BMA400_INT_ENG_OVRUN_MSK, le16_to_cpu(data->status))) {
1631961db2daSJagath Jog J bma400_disable_adv_interrupt(data);
1632961db2daSJagath Jog J dev_err(data->dev, "Interrupt engine overrun\n");
1633961db2daSJagath Jog J goto unlock_err;
1634961db2daSJagath Jog J }
1635961db2daSJagath Jog J
1636961db2daSJagath Jog J if (FIELD_GET(BMA400_INT_S_TAP_MSK, le16_to_cpu(data->status)))
1637961db2daSJagath Jog J iio_push_event(indio_dev,
1638961db2daSJagath Jog J IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
1639961db2daSJagath Jog J IIO_MOD_X_OR_Y_OR_Z,
1640961db2daSJagath Jog J IIO_EV_TYPE_GESTURE,
1641961db2daSJagath Jog J IIO_EV_DIR_SINGLETAP),
1642961db2daSJagath Jog J timestamp);
1643961db2daSJagath Jog J
1644961db2daSJagath Jog J if (FIELD_GET(BMA400_INT_D_TAP_MSK, le16_to_cpu(data->status)))
1645961db2daSJagath Jog J iio_push_event(indio_dev,
1646961db2daSJagath Jog J IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
1647961db2daSJagath Jog J IIO_MOD_X_OR_Y_OR_Z,
1648961db2daSJagath Jog J IIO_EV_TYPE_GESTURE,
1649961db2daSJagath Jog J IIO_EV_DIR_DOUBLETAP),
1650961db2daSJagath Jog J timestamp);
1651961db2daSJagath Jog J
16523cf122c2SJagath Jog J if (FIELD_GET(BMA400_INT_GEN1_MSK, le16_to_cpu(data->status)))
16533cf122c2SJagath Jog J ev_dir = IIO_EV_DIR_RISING;
16543cf122c2SJagath Jog J
16553cf122c2SJagath Jog J if (FIELD_GET(BMA400_INT_GEN2_MSK, le16_to_cpu(data->status)))
16563cf122c2SJagath Jog J ev_dir = IIO_EV_DIR_FALLING;
16573cf122c2SJagath Jog J
16583cf122c2SJagath Jog J if (ev_dir != IIO_EV_DIR_NONE) {
16593cf122c2SJagath Jog J iio_push_event(indio_dev,
16603cf122c2SJagath Jog J IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
16613cf122c2SJagath Jog J IIO_MOD_X_OR_Y_OR_Z,
16623cf122c2SJagath Jog J IIO_EV_TYPE_MAG, ev_dir),
16633cf122c2SJagath Jog J timestamp);
16643cf122c2SJagath Jog J }
16653cf122c2SJagath Jog J
1666d024af5bSJagath Jog J if (FIELD_GET(BMA400_STEP_STAT_MASK, le16_to_cpu(data->status))) {
1667d024af5bSJagath Jog J iio_push_event(indio_dev,
1668d024af5bSJagath Jog J IIO_MOD_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD,
1669d024af5bSJagath Jog J IIO_EV_TYPE_CHANGE,
1670d024af5bSJagath Jog J IIO_EV_DIR_NONE),
1671d024af5bSJagath Jog J timestamp);
167200ee658fSJagath Jog J
167300ee658fSJagath Jog J if (data->activity_event_en) {
167400ee658fSJagath Jog J ret = regmap_read(data->regmap, BMA400_STEP_STAT_REG,
167500ee658fSJagath Jog J &act);
167600ee658fSJagath Jog J if (ret)
167700ee658fSJagath Jog J goto unlock_err;
167800ee658fSJagath Jog J
167900ee658fSJagath Jog J iio_push_event(indio_dev,
168000ee658fSJagath Jog J IIO_MOD_EVENT_CODE(IIO_ACTIVITY, 0,
168100ee658fSJagath Jog J bma400_act_to_mod(act),
168200ee658fSJagath Jog J IIO_EV_TYPE_CHANGE,
168300ee658fSJagath Jog J IIO_EV_DIR_NONE),
168400ee658fSJagath Jog J timestamp);
168500ee658fSJagath Jog J }
1686d024af5bSJagath Jog J }
1687d024af5bSJagath Jog J
1688ffe0ab6aSJagath Jog J if (FIELD_GET(BMA400_INT_DRDY_MSK, le16_to_cpu(data->status))) {
1689ffe0ab6aSJagath Jog J mutex_unlock(&data->mutex);
1690f700e55eSMehdi Djait iio_trigger_poll_nested(data->trig);
1691ffe0ab6aSJagath Jog J return IRQ_HANDLED;
1692ffe0ab6aSJagath Jog J }
1693ffe0ab6aSJagath Jog J
1694d024af5bSJagath Jog J mutex_unlock(&data->mutex);
1695d024af5bSJagath Jog J return IRQ_HANDLED;
1696d024af5bSJagath Jog J
1697ffe0ab6aSJagath Jog J unlock_err:
1698ffe0ab6aSJagath Jog J mutex_unlock(&data->mutex);
1699ffe0ab6aSJagath Jog J return IRQ_NONE;
1700ffe0ab6aSJagath Jog J }
1701ffe0ab6aSJagath Jog J
bma400_probe(struct device * dev,struct regmap * regmap,int irq,const char * name)1702ffe0ab6aSJagath Jog J int bma400_probe(struct device *dev, struct regmap *regmap, int irq,
1703ffe0ab6aSJagath Jog J const char *name)
1704465c811fSDan Robertson {
1705465c811fSDan Robertson struct iio_dev *indio_dev;
1706465c811fSDan Robertson struct bma400_data *data;
1707465c811fSDan Robertson int ret;
1708465c811fSDan Robertson
1709465c811fSDan Robertson indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1710465c811fSDan Robertson if (!indio_dev)
1711465c811fSDan Robertson return -ENOMEM;
1712465c811fSDan Robertson
1713465c811fSDan Robertson data = iio_priv(indio_dev);
1714465c811fSDan Robertson data->regmap = regmap;
1715465c811fSDan Robertson data->dev = dev;
1716465c811fSDan Robertson
1717465c811fSDan Robertson ret = bma400_init(data);
1718465c811fSDan Robertson if (ret)
1719465c811fSDan Robertson return ret;
1720465c811fSDan Robertson
1721b892770aSAndy Shevchenko ret = iio_read_mount_matrix(dev, &data->orientation);
1722465c811fSDan Robertson if (ret)
1723465c811fSDan Robertson return ret;
1724465c811fSDan Robertson
1725465c811fSDan Robertson mutex_init(&data->mutex);
1726465c811fSDan Robertson indio_dev->name = name;
1727465c811fSDan Robertson indio_dev->info = &bma400_info;
1728465c811fSDan Robertson indio_dev->channels = bma400_channels;
1729465c811fSDan Robertson indio_dev->num_channels = ARRAY_SIZE(bma400_channels);
1730ffe0ab6aSJagath Jog J indio_dev->available_scan_masks = bma400_avail_scan_masks;
1731465c811fSDan Robertson indio_dev->modes = INDIO_DIRECT_MODE;
1732465c811fSDan Robertson
1733ffe0ab6aSJagath Jog J if (irq > 0) {
1734ffe0ab6aSJagath Jog J data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
1735ffe0ab6aSJagath Jog J indio_dev->name,
1736ffe0ab6aSJagath Jog J iio_device_id(indio_dev));
1737ffe0ab6aSJagath Jog J if (!data->trig)
1738ffe0ab6aSJagath Jog J return -ENOMEM;
1739ffe0ab6aSJagath Jog J
1740ffe0ab6aSJagath Jog J data->trig->ops = &bma400_trigger_ops;
1741ffe0ab6aSJagath Jog J iio_trigger_set_drvdata(data->trig, indio_dev);
1742ffe0ab6aSJagath Jog J
1743ffe0ab6aSJagath Jog J ret = devm_iio_trigger_register(data->dev, data->trig);
1744ffe0ab6aSJagath Jog J if (ret)
1745ffe0ab6aSJagath Jog J return dev_err_probe(data->dev, ret,
1746ffe0ab6aSJagath Jog J "iio trigger register fail\n");
1747ffe0ab6aSJagath Jog J
1748ffe0ab6aSJagath Jog J indio_dev->trig = iio_trigger_get(data->trig);
1749ffe0ab6aSJagath Jog J ret = devm_request_threaded_irq(dev, irq, NULL,
1750ffe0ab6aSJagath Jog J &bma400_interrupt,
1751ffe0ab6aSJagath Jog J IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1752ffe0ab6aSJagath Jog J indio_dev->name, indio_dev);
1753ffe0ab6aSJagath Jog J if (ret)
1754ffe0ab6aSJagath Jog J return dev_err_probe(data->dev, ret,
1755ffe0ab6aSJagath Jog J "request irq %d failed\n", irq);
1756ffe0ab6aSJagath Jog J }
1757ffe0ab6aSJagath Jog J
1758ffe0ab6aSJagath Jog J ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
1759ffe0ab6aSJagath Jog J &bma400_trigger_handler, NULL);
1760ffe0ab6aSJagath Jog J if (ret)
1761ffe0ab6aSJagath Jog J return dev_err_probe(data->dev, ret,
1762ffe0ab6aSJagath Jog J "iio triggered buffer setup failed\n");
1763ffe0ab6aSJagath Jog J
176412c99f85SJagath Jog J return devm_iio_device_register(dev, indio_dev);
1765465c811fSDan Robertson }
1766f9c02c94SJonathan Cameron EXPORT_SYMBOL_NS(bma400_probe, IIO_BMA400);
1767465c811fSDan Robertson
1768465c811fSDan Robertson MODULE_AUTHOR("Dan Robertson <dan@dlrobertson.com>");
1769961db2daSJagath Jog J MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
1770465c811fSDan Robertson MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor core");
1771465c811fSDan Robertson MODULE_LICENSE("GPL");
1772