blob: c73331f7782b8af4b91c94c26dd29195ddc45d9a [file] [log] [blame]
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001/*
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00002 * 3-axis accelerometer driver supporting following Bosch-Sensortec chips:
3 * - BMC150
4 * - BMI055
5 * - BMA255
6 * - BMA250E
7 * - BMA222E
8 * - BMA280
9 *
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +010010 * Copyright (c) 2014, Intel Corporation.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms and conditions of the GNU General Public License,
14 * version 2, as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * more details.
20 */
21
22#include <linux/module.h>
23#include <linux/i2c.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/acpi.h>
28#include <linux/gpio/consumer.h>
29#include <linux/pm.h>
30#include <linux/pm_runtime.h>
31#include <linux/iio/iio.h>
32#include <linux/iio/sysfs.h>
33#include <linux/iio/buffer.h>
34#include <linux/iio/events.h>
35#include <linux/iio/trigger.h>
36#include <linux/iio/trigger_consumer.h>
37#include <linux/iio/triggered_buffer.h>
Markus Pargmann4011eda2015-09-21 12:55:13 +020038#include <linux/regmap.h>
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +010039
Markus Pargmann55637c32015-09-21 12:55:15 +020040#include "bmc150-accel.h"
41
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +010042#define BMC150_ACCEL_DRV_NAME "bmc150_accel"
43#define BMC150_ACCEL_IRQ_NAME "bmc150_accel_event"
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +010044
45#define BMC150_ACCEL_REG_CHIP_ID 0x00
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +010046
47#define BMC150_ACCEL_REG_INT_STATUS_2 0x0B
48#define BMC150_ACCEL_ANY_MOTION_MASK 0x07
Srinivas Pandruvada8d5a9782014-10-10 20:35:32 -070049#define BMC150_ACCEL_ANY_MOTION_BIT_X BIT(0)
50#define BMC150_ACCEL_ANY_MOTION_BIT_Y BIT(1)
51#define BMC150_ACCEL_ANY_MOTION_BIT_Z BIT(2)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +010052#define BMC150_ACCEL_ANY_MOTION_BIT_SIGN BIT(3)
53
54#define BMC150_ACCEL_REG_PMU_LPW 0x11
55#define BMC150_ACCEL_PMU_MODE_MASK 0xE0
56#define BMC150_ACCEL_PMU_MODE_SHIFT 5
57#define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_MASK 0x17
58#define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT 1
59
60#define BMC150_ACCEL_REG_PMU_RANGE 0x0F
61
62#define BMC150_ACCEL_DEF_RANGE_2G 0x03
63#define BMC150_ACCEL_DEF_RANGE_4G 0x05
64#define BMC150_ACCEL_DEF_RANGE_8G 0x08
65#define BMC150_ACCEL_DEF_RANGE_16G 0x0C
66
67/* Default BW: 125Hz */
68#define BMC150_ACCEL_REG_PMU_BW 0x10
69#define BMC150_ACCEL_DEF_BW 125
70
71#define BMC150_ACCEL_REG_INT_MAP_0 0x19
72#define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2)
73
74#define BMC150_ACCEL_REG_INT_MAP_1 0x1A
Octavian Purdila3bbec972015-03-22 20:33:40 +020075#define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0)
76#define BMC150_ACCEL_INT_MAP_1_BIT_FWM BIT(1)
77#define BMC150_ACCEL_INT_MAP_1_BIT_FFULL BIT(2)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +010078
79#define BMC150_ACCEL_REG_INT_RST_LATCH 0x21
80#define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80
81#define BMC150_ACCEL_INT_MODE_LATCH_INT 0x0F
82#define BMC150_ACCEL_INT_MODE_NON_LATCH_INT 0x00
83
84#define BMC150_ACCEL_REG_INT_EN_0 0x16
85#define BMC150_ACCEL_INT_EN_BIT_SLP_X BIT(0)
86#define BMC150_ACCEL_INT_EN_BIT_SLP_Y BIT(1)
87#define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2)
88
89#define BMC150_ACCEL_REG_INT_EN_1 0x17
Octavian Purdila3bbec972015-03-22 20:33:40 +020090#define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4)
91#define BMC150_ACCEL_INT_EN_BIT_FFULL_EN BIT(5)
92#define BMC150_ACCEL_INT_EN_BIT_FWM_EN BIT(6)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +010093
94#define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20
95#define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0)
96
97#define BMC150_ACCEL_REG_INT_5 0x27
98#define BMC150_ACCEL_SLOPE_DUR_MASK 0x03
99
100#define BMC150_ACCEL_REG_INT_6 0x28
101#define BMC150_ACCEL_SLOPE_THRES_MASK 0xFF
102
103/* Slope duration in terms of number of samples */
Srinivas Pandruvada9e8e2282014-10-10 20:35:33 -0700104#define BMC150_ACCEL_DEF_SLOPE_DURATION 1
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100105/* in terms of multiples of g's/LSB, based on range */
Srinivas Pandruvada9e8e2282014-10-10 20:35:33 -0700106#define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 1
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100107
108#define BMC150_ACCEL_REG_XOUT_L 0x02
109
110#define BMC150_ACCEL_MAX_STARTUP_TIME_MS 100
111
112/* Sleep Duration values */
113#define BMC150_ACCEL_SLEEP_500_MICRO 0x05
114#define BMC150_ACCEL_SLEEP_1_MS 0x06
115#define BMC150_ACCEL_SLEEP_2_MS 0x07
116#define BMC150_ACCEL_SLEEP_4_MS 0x08
117#define BMC150_ACCEL_SLEEP_6_MS 0x09
118#define BMC150_ACCEL_SLEEP_10_MS 0x0A
119#define BMC150_ACCEL_SLEEP_25_MS 0x0B
120#define BMC150_ACCEL_SLEEP_50_MS 0x0C
121#define BMC150_ACCEL_SLEEP_100_MS 0x0D
122#define BMC150_ACCEL_SLEEP_500_MS 0x0E
123#define BMC150_ACCEL_SLEEP_1_SEC 0x0F
124
125#define BMC150_ACCEL_REG_TEMP 0x08
126#define BMC150_ACCEL_TEMP_CENTER_VAL 24
127
128#define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2))
129#define BMC150_AUTO_SUSPEND_DELAY_MS 2000
130
Octavian Purdila3bbec972015-03-22 20:33:40 +0200131#define BMC150_ACCEL_REG_FIFO_STATUS 0x0E
132#define BMC150_ACCEL_REG_FIFO_CONFIG0 0x30
133#define BMC150_ACCEL_REG_FIFO_CONFIG1 0x3E
134#define BMC150_ACCEL_REG_FIFO_DATA 0x3F
135#define BMC150_ACCEL_FIFO_LENGTH 32
136
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100137enum bmc150_accel_axis {
138 AXIS_X,
139 AXIS_Y,
140 AXIS_Z,
141};
142
143enum bmc150_power_modes {
144 BMC150_ACCEL_SLEEP_MODE_NORMAL,
145 BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND,
146 BMC150_ACCEL_SLEEP_MODE_LPM,
147 BMC150_ACCEL_SLEEP_MODE_SUSPEND = 0x04,
148};
149
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000150struct bmc150_scale_info {
151 int scale;
152 u8 reg_range;
153};
154
155struct bmc150_accel_chip_info {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +0200156 const char *name;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000157 u8 chip_id;
158 const struct iio_chan_spec *channels;
159 int num_channels;
160 const struct bmc150_scale_info scale_table[4];
161};
162
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200163struct bmc150_accel_interrupt {
164 const struct bmc150_accel_interrupt_info *info;
165 atomic_t users;
166};
167
Octavian Purdila7d963212015-03-03 18:17:58 +0200168struct bmc150_accel_trigger {
169 struct bmc150_accel_data *data;
170 struct iio_trigger *indio_trig;
171 int (*setup)(struct bmc150_accel_trigger *t, bool state);
172 int intr;
173 bool enabled;
174};
175
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200176enum bmc150_accel_interrupt_id {
177 BMC150_ACCEL_INT_DATA_READY,
178 BMC150_ACCEL_INT_ANY_MOTION,
179 BMC150_ACCEL_INT_WATERMARK,
180 BMC150_ACCEL_INTERRUPTS,
181};
182
Octavian Purdila7d963212015-03-03 18:17:58 +0200183enum bmc150_accel_trigger_id {
184 BMC150_ACCEL_TRIGGER_DATA_READY,
185 BMC150_ACCEL_TRIGGER_ANY_MOTION,
186 BMC150_ACCEL_TRIGGERS,
187};
188
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100189struct bmc150_accel_data {
Markus Pargmann4011eda2015-09-21 12:55:13 +0200190 struct regmap *regmap;
191 struct device *dev;
Markus Pargmann19c95d62015-09-21 12:55:14 +0200192 int irq;
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200193 struct bmc150_accel_interrupt interrupts[BMC150_ACCEL_INTERRUPTS];
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200194 atomic_t active_intr;
Octavian Purdila7d963212015-03-03 18:17:58 +0200195 struct bmc150_accel_trigger triggers[BMC150_ACCEL_TRIGGERS];
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100196 struct mutex mutex;
Octavian Purdila3bbec972015-03-22 20:33:40 +0200197 u8 fifo_mode, watermark;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100198 s16 buffer[8];
199 u8 bw_bits;
200 u32 slope_dur;
201 u32 slope_thres;
202 u32 range;
203 int ev_enable_state;
Vlad Dogaruc16bff42015-05-12 17:03:24 +0300204 int64_t timestamp, old_timestamp; /* Only used in hw fifo mode. */
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000205 const struct bmc150_accel_chip_info *chip_info;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100206};
207
208static const struct {
209 int val;
210 int val2;
211 u8 bw_bits;
Sathyanarayanan Kuppuswamy0ba8da92015-03-03 18:17:56 +0200212} bmc150_accel_samp_freq_table[] = { {15, 620000, 0x08},
213 {31, 260000, 0x09},
214 {62, 500000, 0x0A},
215 {125, 0, 0x0B},
216 {250, 0, 0x0C},
217 {500, 0, 0x0D},
218 {1000, 0, 0x0E},
219 {2000, 0, 0x0F} };
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100220
221static const struct {
222 int bw_bits;
223 int msec;
224} bmc150_accel_sample_upd_time[] = { {0x08, 64},
225 {0x09, 32},
226 {0x0A, 16},
227 {0x0B, 8},
228 {0x0C, 4},
229 {0x0D, 2},
230 {0x0E, 1},
231 {0x0F, 1} };
232
233static const struct {
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100234 int sleep_dur;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000235 u8 reg_value;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100236} bmc150_accel_sleep_value_table[] = { {0, 0},
237 {500, BMC150_ACCEL_SLEEP_500_MICRO},
238 {1000, BMC150_ACCEL_SLEEP_1_MS},
239 {2000, BMC150_ACCEL_SLEEP_2_MS},
240 {4000, BMC150_ACCEL_SLEEP_4_MS},
241 {6000, BMC150_ACCEL_SLEEP_6_MS},
242 {10000, BMC150_ACCEL_SLEEP_10_MS},
243 {25000, BMC150_ACCEL_SLEEP_25_MS},
244 {50000, BMC150_ACCEL_SLEEP_50_MS},
245 {100000, BMC150_ACCEL_SLEEP_100_MS},
246 {500000, BMC150_ACCEL_SLEEP_500_MS},
247 {1000000, BMC150_ACCEL_SLEEP_1_SEC} };
248
Markus Pargmann4011eda2015-09-21 12:55:13 +0200249static const struct regmap_config bmc150_i2c_regmap_conf = {
250 .reg_bits = 8,
251 .val_bits = 8,
252 .max_register = 0x3f,
253};
254
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100255static int bmc150_accel_set_mode(struct bmc150_accel_data *data,
256 enum bmc150_power_modes mode,
257 int dur_us)
258{
259 int i;
260 int ret;
261 u8 lpw_bits;
262 int dur_val = -1;
263
264 if (dur_us > 0) {
265 for (i = 0; i < ARRAY_SIZE(bmc150_accel_sleep_value_table);
266 ++i) {
267 if (bmc150_accel_sleep_value_table[i].sleep_dur ==
268 dur_us)
269 dur_val =
270 bmc150_accel_sleep_value_table[i].reg_value;
271 }
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200272 } else {
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100273 dur_val = 0;
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200274 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100275
276 if (dur_val < 0)
277 return -EINVAL;
278
279 lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT;
280 lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT);
281
Markus Pargmann19c95d62015-09-21 12:55:14 +0200282 dev_dbg(data->dev, "Set Mode bits %x\n", lpw_bits);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100283
Markus Pargmann4011eda2015-09-21 12:55:13 +0200284 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100285 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200286 dev_err(data->dev, "Error writing reg_pmu_lpw\n");
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100287 return ret;
288 }
289
290 return 0;
291}
292
293static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val,
294 int val2)
295{
296 int i;
297 int ret;
298
299 for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
300 if (bmc150_accel_samp_freq_table[i].val == val &&
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200301 bmc150_accel_samp_freq_table[i].val2 == val2) {
Markus Pargmann4011eda2015-09-21 12:55:13 +0200302 ret = regmap_write(data->regmap,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100303 BMC150_ACCEL_REG_PMU_BW,
304 bmc150_accel_samp_freq_table[i].bw_bits);
305 if (ret < 0)
306 return ret;
307
308 data->bw_bits =
309 bmc150_accel_samp_freq_table[i].bw_bits;
310 return 0;
311 }
312 }
313
314 return -EINVAL;
315}
316
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200317static int bmc150_accel_update_slope(struct bmc150_accel_data *data)
318{
Markus Pargmann4011eda2015-09-21 12:55:13 +0200319 int ret;
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200320
Markus Pargmann4011eda2015-09-21 12:55:13 +0200321 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6,
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200322 data->slope_thres);
323 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200324 dev_err(data->dev, "Error writing reg_int_6\n");
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200325 return ret;
326 }
327
Markus Pargmann4011eda2015-09-21 12:55:13 +0200328 ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5,
329 BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur);
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200330 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200331 dev_err(data->dev, "Error updating reg_int_5\n");
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200332 return ret;
333 }
334
Markus Pargmann19c95d62015-09-21 12:55:14 +0200335 dev_dbg(data->dev, "%s: %x %x\n", __func__, data->slope_thres,
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200336 data->slope_dur);
337
338 return ret;
339}
340
Octavian Purdila7d963212015-03-03 18:17:58 +0200341static int bmc150_accel_any_motion_setup(struct bmc150_accel_trigger *t,
342 bool state)
343{
344 if (state)
345 return bmc150_accel_update_slope(t->data);
346
347 return 0;
348}
349
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100350static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val,
351 int *val2)
352{
353 int i;
354
355 for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
356 if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) {
357 *val = bmc150_accel_samp_freq_table[i].val;
358 *val2 = bmc150_accel_samp_freq_table[i].val2;
359 return IIO_VAL_INT_PLUS_MICRO;
360 }
361 }
362
363 return -EINVAL;
364}
365
Rafael J. Wysocki6f0a13f2014-12-04 01:08:13 +0100366#ifdef CONFIG_PM
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100367static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data)
368{
369 int i;
370
371 for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) {
372 if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits)
373 return bmc150_accel_sample_upd_time[i].msec;
374 }
375
376 return BMC150_ACCEL_MAX_STARTUP_TIME_MS;
377}
378
379static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
380{
381 int ret;
382
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200383 if (on) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200384 ret = pm_runtime_get_sync(data->dev);
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200385 } else {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200386 pm_runtime_mark_last_busy(data->dev);
387 ret = pm_runtime_put_autosuspend(data->dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100388 }
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200389
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100390 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200391 dev_err(data->dev,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100392 "Failed: bmc150_accel_set_power_state for %d\n", on);
Srinivas Pandruvadaaaeecd82014-10-10 20:35:31 -0700393 if (on)
Markus Pargmann19c95d62015-09-21 12:55:14 +0200394 pm_runtime_put_noidle(data->dev);
Srinivas Pandruvadaaaeecd82014-10-10 20:35:31 -0700395
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100396 return ret;
397 }
398
399 return 0;
400}
Laurentiu Palcub31b05c2014-08-29 09:38:00 +0100401#else
402static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
403{
404 return 0;
405}
406#endif
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100407
Octavian Purdila8e22f472015-01-31 02:00:04 +0200408static const struct bmc150_accel_interrupt_info {
409 u8 map_reg;
410 u8 map_bitmask;
411 u8 en_reg;
412 u8 en_bitmask;
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200413} bmc150_accel_interrupts[BMC150_ACCEL_INTERRUPTS] = {
Octavian Purdila8e22f472015-01-31 02:00:04 +0200414 { /* data ready interrupt */
415 .map_reg = BMC150_ACCEL_REG_INT_MAP_1,
416 .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_DATA,
417 .en_reg = BMC150_ACCEL_REG_INT_EN_1,
418 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_DATA_EN,
419 },
420 { /* motion interrupt */
421 .map_reg = BMC150_ACCEL_REG_INT_MAP_0,
422 .map_bitmask = BMC150_ACCEL_INT_MAP_0_BIT_SLOPE,
423 .en_reg = BMC150_ACCEL_REG_INT_EN_0,
424 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_SLP_X |
425 BMC150_ACCEL_INT_EN_BIT_SLP_Y |
426 BMC150_ACCEL_INT_EN_BIT_SLP_Z
427 },
Octavian Purdila3bbec972015-03-22 20:33:40 +0200428 { /* fifo watermark interrupt */
429 .map_reg = BMC150_ACCEL_REG_INT_MAP_1,
430 .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_FWM,
431 .en_reg = BMC150_ACCEL_REG_INT_EN_1,
432 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_FWM_EN,
433 },
Octavian Purdila8e22f472015-01-31 02:00:04 +0200434};
435
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200436static void bmc150_accel_interrupts_setup(struct iio_dev *indio_dev,
437 struct bmc150_accel_data *data)
438{
439 int i;
440
441 for (i = 0; i < BMC150_ACCEL_INTERRUPTS; i++)
442 data->interrupts[i].info = &bmc150_accel_interrupts[i];
443}
444
445static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i,
Octavian Purdila8e22f472015-01-31 02:00:04 +0200446 bool state)
447{
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200448 struct bmc150_accel_interrupt *intr = &data->interrupts[i];
449 const struct bmc150_accel_interrupt_info *info = intr->info;
Octavian Purdila8e22f472015-01-31 02:00:04 +0200450 int ret;
451
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200452 if (state) {
453 if (atomic_inc_return(&intr->users) > 1)
454 return 0;
455 } else {
456 if (atomic_dec_return(&intr->users) > 0)
457 return 0;
458 }
459
Octavian Purdila8e22f472015-01-31 02:00:04 +0200460 /*
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200461 * We will expect the enable and disable to do operation in reverse
462 * order. This will happen here anyway, as our resume operation uses
463 * sync mode runtime pm calls. The suspend operation will be delayed
464 * by autosuspend delay.
465 * So the disable operation will still happen in reverse order of
466 * enable operation. When runtime pm is disabled the mode is always on,
467 * so sequence doesn't matter.
Octavian Purdila8e22f472015-01-31 02:00:04 +0200468 */
469 ret = bmc150_accel_set_power_state(data, state);
470 if (ret < 0)
471 return ret;
472
473 /* map the interrupt to the appropriate pins */
Markus Pargmann4011eda2015-09-21 12:55:13 +0200474 ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask,
475 (state ? info->map_bitmask : 0));
Octavian Purdila8e22f472015-01-31 02:00:04 +0200476 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200477 dev_err(data->dev, "Error updating reg_int_map\n");
Octavian Purdila8e22f472015-01-31 02:00:04 +0200478 goto out_fix_power_state;
479 }
480
481 /* enable/disable the interrupt */
Markus Pargmann4011eda2015-09-21 12:55:13 +0200482 ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask,
483 (state ? info->en_bitmask : 0));
Octavian Purdila8e22f472015-01-31 02:00:04 +0200484 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200485 dev_err(data->dev, "Error updating reg_int_en\n");
Octavian Purdila8e22f472015-01-31 02:00:04 +0200486 goto out_fix_power_state;
487 }
488
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200489 if (state)
490 atomic_inc(&data->active_intr);
491 else
492 atomic_dec(&data->active_intr);
493
Octavian Purdila8e22f472015-01-31 02:00:04 +0200494 return 0;
495
496out_fix_power_state:
497 bmc150_accel_set_power_state(data, false);
498 return ret;
499}
500
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100501static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
502{
503 int ret, i;
504
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000505 for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) {
506 if (data->chip_info->scale_table[i].scale == val) {
Markus Pargmann4011eda2015-09-21 12:55:13 +0200507 ret = regmap_write(data->regmap,
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000508 BMC150_ACCEL_REG_PMU_RANGE,
509 data->chip_info->scale_table[i].reg_range);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100510 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200511 dev_err(data->dev,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100512 "Error writing pmu_range\n");
513 return ret;
514 }
515
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000516 data->range = data->chip_info->scale_table[i].reg_range;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100517 return 0;
518 }
519 }
520
521 return -EINVAL;
522}
523
524static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val)
525{
526 int ret;
Markus Pargmann4011eda2015-09-21 12:55:13 +0200527 unsigned int value;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100528
529 mutex_lock(&data->mutex);
530
Markus Pargmann4011eda2015-09-21 12:55:13 +0200531 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100532 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200533 dev_err(data->dev, "Error reading reg_temp\n");
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100534 mutex_unlock(&data->mutex);
535 return ret;
536 }
Markus Pargmann4011eda2015-09-21 12:55:13 +0200537 *val = sign_extend32(value, 7);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100538
539 mutex_unlock(&data->mutex);
540
541 return IIO_VAL_INT;
542}
543
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000544static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
545 struct iio_chan_spec const *chan,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100546 int *val)
547{
548 int ret;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000549 int axis = chan->scan_index;
Markus Pargmann4011eda2015-09-21 12:55:13 +0200550 unsigned int raw_val;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100551
552 mutex_lock(&data->mutex);
553 ret = bmc150_accel_set_power_state(data, true);
554 if (ret < 0) {
555 mutex_unlock(&data->mutex);
556 return ret;
557 }
558
Markus Pargmann4011eda2015-09-21 12:55:13 +0200559 ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
560 &raw_val, 2);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100561 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200562 dev_err(data->dev, "Error reading axis %d\n", axis);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100563 bmc150_accel_set_power_state(data, false);
564 mutex_unlock(&data->mutex);
565 return ret;
566 }
Markus Pargmann4011eda2015-09-21 12:55:13 +0200567 *val = sign_extend32(raw_val >> chan->scan_type.shift,
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000568 chan->scan_type.realbits - 1);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100569 ret = bmc150_accel_set_power_state(data, false);
570 mutex_unlock(&data->mutex);
571 if (ret < 0)
572 return ret;
573
574 return IIO_VAL_INT;
575}
576
577static int bmc150_accel_read_raw(struct iio_dev *indio_dev,
578 struct iio_chan_spec const *chan,
579 int *val, int *val2, long mask)
580{
581 struct bmc150_accel_data *data = iio_priv(indio_dev);
582 int ret;
583
584 switch (mask) {
585 case IIO_CHAN_INFO_RAW:
586 switch (chan->type) {
587 case IIO_TEMP:
588 return bmc150_accel_get_temp(data, val);
589 case IIO_ACCEL:
590 if (iio_buffer_enabled(indio_dev))
591 return -EBUSY;
592 else
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000593 return bmc150_accel_get_axis(data, chan, val);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100594 default:
595 return -EINVAL;
596 }
597 case IIO_CHAN_INFO_OFFSET:
598 if (chan->type == IIO_TEMP) {
599 *val = BMC150_ACCEL_TEMP_CENTER_VAL;
600 return IIO_VAL_INT;
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200601 } else {
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100602 return -EINVAL;
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200603 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100604 case IIO_CHAN_INFO_SCALE:
605 *val = 0;
606 switch (chan->type) {
607 case IIO_TEMP:
608 *val2 = 500000;
609 return IIO_VAL_INT_PLUS_MICRO;
610 case IIO_ACCEL:
611 {
612 int i;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000613 const struct bmc150_scale_info *si;
614 int st_size = ARRAY_SIZE(data->chip_info->scale_table);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100615
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000616 for (i = 0; i < st_size; ++i) {
617 si = &data->chip_info->scale_table[i];
618 if (si->reg_range == data->range) {
619 *val2 = si->scale;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100620 return IIO_VAL_INT_PLUS_MICRO;
621 }
622 }
623 return -EINVAL;
624 }
625 default:
626 return -EINVAL;
627 }
628 case IIO_CHAN_INFO_SAMP_FREQ:
629 mutex_lock(&data->mutex);
630 ret = bmc150_accel_get_bw(data, val, val2);
631 mutex_unlock(&data->mutex);
632 return ret;
633 default:
634 return -EINVAL;
635 }
636}
637
638static int bmc150_accel_write_raw(struct iio_dev *indio_dev,
639 struct iio_chan_spec const *chan,
640 int val, int val2, long mask)
641{
642 struct bmc150_accel_data *data = iio_priv(indio_dev);
643 int ret;
644
645 switch (mask) {
646 case IIO_CHAN_INFO_SAMP_FREQ:
647 mutex_lock(&data->mutex);
648 ret = bmc150_accel_set_bw(data, val, val2);
649 mutex_unlock(&data->mutex);
650 break;
651 case IIO_CHAN_INFO_SCALE:
652 if (val)
653 return -EINVAL;
654
655 mutex_lock(&data->mutex);
656 ret = bmc150_accel_set_scale(data, val2);
657 mutex_unlock(&data->mutex);
658 return ret;
659 default:
660 ret = -EINVAL;
661 }
662
663 return ret;
664}
665
666static int bmc150_accel_read_event(struct iio_dev *indio_dev,
667 const struct iio_chan_spec *chan,
668 enum iio_event_type type,
669 enum iio_event_direction dir,
670 enum iio_event_info info,
671 int *val, int *val2)
672{
673 struct bmc150_accel_data *data = iio_priv(indio_dev);
674
675 *val2 = 0;
676 switch (info) {
677 case IIO_EV_INFO_VALUE:
678 *val = data->slope_thres;
679 break;
680 case IIO_EV_INFO_PERIOD:
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200681 *val = data->slope_dur;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100682 break;
683 default:
684 return -EINVAL;
685 }
686
687 return IIO_VAL_INT;
688}
689
690static int bmc150_accel_write_event(struct iio_dev *indio_dev,
691 const struct iio_chan_spec *chan,
692 enum iio_event_type type,
693 enum iio_event_direction dir,
694 enum iio_event_info info,
695 int val, int val2)
696{
697 struct bmc150_accel_data *data = iio_priv(indio_dev);
698
699 if (data->ev_enable_state)
700 return -EBUSY;
701
702 switch (info) {
703 case IIO_EV_INFO_VALUE:
Hartmut Knaackfdd15f62015-06-15 23:48:25 +0200704 data->slope_thres = val & BMC150_ACCEL_SLOPE_THRES_MASK;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100705 break;
706 case IIO_EV_INFO_PERIOD:
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200707 data->slope_dur = val & BMC150_ACCEL_SLOPE_DUR_MASK;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100708 break;
709 default:
710 return -EINVAL;
711 }
712
713 return 0;
714}
715
716static int bmc150_accel_read_event_config(struct iio_dev *indio_dev,
717 const struct iio_chan_spec *chan,
718 enum iio_event_type type,
719 enum iio_event_direction dir)
720{
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100721 struct bmc150_accel_data *data = iio_priv(indio_dev);
722
723 return data->ev_enable_state;
724}
725
726static int bmc150_accel_write_event_config(struct iio_dev *indio_dev,
727 const struct iio_chan_spec *chan,
728 enum iio_event_type type,
729 enum iio_event_direction dir,
730 int state)
731{
732 struct bmc150_accel_data *data = iio_priv(indio_dev);
733 int ret;
734
Octavian Purdila14ee64f2015-01-31 02:00:05 +0200735 if (state == data->ev_enable_state)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100736 return 0;
737
738 mutex_lock(&data->mutex);
739
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200740 ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_ANY_MOTION,
741 state);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100742 if (ret < 0) {
743 mutex_unlock(&data->mutex);
744 return ret;
745 }
746
747 data->ev_enable_state = state;
748 mutex_unlock(&data->mutex);
749
750 return 0;
751}
752
753static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev,
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200754 struct iio_trigger *trig)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100755{
756 struct bmc150_accel_data *data = iio_priv(indio_dev);
Octavian Purdila7d963212015-03-03 18:17:58 +0200757 int i;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100758
Octavian Purdila7d963212015-03-03 18:17:58 +0200759 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
760 if (data->triggers[i].indio_trig == trig)
761 return 0;
762 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100763
Octavian Purdila7d963212015-03-03 18:17:58 +0200764 return -EINVAL;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100765}
766
Octavian Purdila3bbec972015-03-22 20:33:40 +0200767static ssize_t bmc150_accel_get_fifo_watermark(struct device *dev,
768 struct device_attribute *attr,
769 char *buf)
770{
771 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
772 struct bmc150_accel_data *data = iio_priv(indio_dev);
773 int wm;
774
775 mutex_lock(&data->mutex);
776 wm = data->watermark;
777 mutex_unlock(&data->mutex);
778
779 return sprintf(buf, "%d\n", wm);
780}
781
782static ssize_t bmc150_accel_get_fifo_state(struct device *dev,
783 struct device_attribute *attr,
784 char *buf)
785{
786 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
787 struct bmc150_accel_data *data = iio_priv(indio_dev);
788 bool state;
789
790 mutex_lock(&data->mutex);
791 state = data->fifo_mode;
792 mutex_unlock(&data->mutex);
793
794 return sprintf(buf, "%d\n", state);
795}
796
797static IIO_CONST_ATTR(hwfifo_watermark_min, "1");
798static IIO_CONST_ATTR(hwfifo_watermark_max,
799 __stringify(BMC150_ACCEL_FIFO_LENGTH));
800static IIO_DEVICE_ATTR(hwfifo_enabled, S_IRUGO,
801 bmc150_accel_get_fifo_state, NULL, 0);
802static IIO_DEVICE_ATTR(hwfifo_watermark, S_IRUGO,
803 bmc150_accel_get_fifo_watermark, NULL, 0);
804
805static const struct attribute *bmc150_accel_fifo_attributes[] = {
806 &iio_const_attr_hwfifo_watermark_min.dev_attr.attr,
807 &iio_const_attr_hwfifo_watermark_max.dev_attr.attr,
808 &iio_dev_attr_hwfifo_watermark.dev_attr.attr,
809 &iio_dev_attr_hwfifo_enabled.dev_attr.attr,
810 NULL,
811};
812
813static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
814{
815 struct bmc150_accel_data *data = iio_priv(indio_dev);
816
817 if (val > BMC150_ACCEL_FIFO_LENGTH)
818 val = BMC150_ACCEL_FIFO_LENGTH;
819
820 mutex_lock(&data->mutex);
821 data->watermark = val;
822 mutex_unlock(&data->mutex);
823
824 return 0;
825}
826
827/*
828 * We must read at least one full frame in one burst, otherwise the rest of the
829 * frame data is discarded.
830 */
Markus Pargmann4011eda2015-09-21 12:55:13 +0200831static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
Octavian Purdila3bbec972015-03-22 20:33:40 +0200832 char *buffer, int samples)
833{
834 int sample_length = 3 * 2;
Markus Pargmann4011eda2015-09-21 12:55:13 +0200835 int ret;
836 int total_length = samples * sample_length;
837 int i;
838 size_t step = regmap_get_raw_read_max(data->regmap);
Octavian Purdila3bbec972015-03-22 20:33:40 +0200839
Markus Pargmann4011eda2015-09-21 12:55:13 +0200840 if (!step || step > total_length)
841 step = total_length;
842 else if (step < total_length)
843 step = sample_length;
Octavian Purdila3bbec972015-03-22 20:33:40 +0200844
Markus Pargmann4011eda2015-09-21 12:55:13 +0200845 /*
846 * Seems we have a bus with size limitation so we have to execute
847 * multiple reads
848 */
849 for (i = 0; i < total_length; i += step) {
850 ret = regmap_raw_read(data->regmap, BMC150_ACCEL_REG_FIFO_DATA,
851 &buffer[i], step);
852 if (ret)
853 break;
Octavian Purdila3bbec972015-03-22 20:33:40 +0200854 }
855
856 if (ret)
Markus Pargmann4011eda2015-09-21 12:55:13 +0200857 dev_err(data->dev, "Error transferring data from fifo in single steps of %zu\n",
858 step);
Octavian Purdila3bbec972015-03-22 20:33:40 +0200859
860 return ret;
861}
862
863static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
864 unsigned samples, bool irq)
865{
866 struct bmc150_accel_data *data = iio_priv(indio_dev);
867 int ret, i;
868 u8 count;
869 u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
870 int64_t tstamp;
871 uint64_t sample_period;
Markus Pargmann4011eda2015-09-21 12:55:13 +0200872 unsigned int val;
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200873
Markus Pargmann4011eda2015-09-21 12:55:13 +0200874 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val);
Octavian Purdila3bbec972015-03-22 20:33:40 +0200875 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200876 dev_err(data->dev, "Error reading reg_fifo_status\n");
Octavian Purdila3bbec972015-03-22 20:33:40 +0200877 return ret;
878 }
879
Markus Pargmann4011eda2015-09-21 12:55:13 +0200880 count = val & 0x7F;
Octavian Purdila3bbec972015-03-22 20:33:40 +0200881
882 if (!count)
883 return 0;
884
885 /*
886 * If we getting called from IRQ handler we know the stored timestamp is
887 * fairly accurate for the last stored sample. Otherwise, if we are
888 * called as a result of a read operation from userspace and hence
889 * before the watermark interrupt was triggered, take a timestamp
890 * now. We can fall anywhere in between two samples so the error in this
891 * case is at most one sample period.
892 */
893 if (!irq) {
894 data->old_timestamp = data->timestamp;
895 data->timestamp = iio_get_time_ns();
896 }
897
898 /*
899 * Approximate timestamps for each of the sample based on the sampling
900 * frequency, timestamp for last sample and number of samples.
901 *
902 * Note that we can't use the current bandwidth settings to compute the
903 * sample period because the sample rate varies with the device
904 * (e.g. between 31.70ms to 32.20ms for a bandwidth of 15.63HZ). That
905 * small variation adds when we store a large number of samples and
906 * creates significant jitter between the last and first samples in
907 * different batches (e.g. 32ms vs 21ms).
908 *
909 * To avoid this issue we compute the actual sample period ourselves
910 * based on the timestamp delta between the last two flush operations.
911 */
912 sample_period = (data->timestamp - data->old_timestamp);
913 do_div(sample_period, count);
914 tstamp = data->timestamp - (count - 1) * sample_period;
915
916 if (samples && count > samples)
917 count = samples;
918
Markus Pargmann4011eda2015-09-21 12:55:13 +0200919 ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count);
Octavian Purdila3bbec972015-03-22 20:33:40 +0200920 if (ret)
921 return ret;
922
923 /*
924 * Ideally we want the IIO core to handle the demux when running in fifo
925 * mode but not when running in triggered buffer mode. Unfortunately
926 * this does not seem to be possible, so stick with driver demux for
927 * now.
928 */
929 for (i = 0; i < count; i++) {
930 u16 sample[8];
931 int j, bit;
932
933 j = 0;
934 for_each_set_bit(bit, indio_dev->active_scan_mask,
935 indio_dev->masklength)
936 memcpy(&sample[j++], &buffer[i * 3 + bit], 2);
937
938 iio_push_to_buffers_with_timestamp(indio_dev, sample, tstamp);
939
940 tstamp += sample_period;
941 }
942
943 return count;
944}
945
946static int bmc150_accel_fifo_flush(struct iio_dev *indio_dev, unsigned samples)
947{
948 struct bmc150_accel_data *data = iio_priv(indio_dev);
949 int ret;
950
951 mutex_lock(&data->mutex);
952 ret = __bmc150_accel_fifo_flush(indio_dev, samples, false);
953 mutex_unlock(&data->mutex);
954
955 return ret;
956}
957
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100958static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
Sathyanarayanan Kuppuswamy0ba8da92015-03-03 18:17:56 +0200959 "15.620000 31.260000 62.50000 125 250 500 1000 2000");
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100960
961static struct attribute *bmc150_accel_attributes[] = {
962 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
963 NULL,
964};
965
966static const struct attribute_group bmc150_accel_attrs_group = {
967 .attrs = bmc150_accel_attributes,
968};
969
970static const struct iio_event_spec bmc150_accel_event = {
971 .type = IIO_EV_TYPE_ROC,
Srinivas Pandruvada11741242014-10-10 20:35:34 -0700972 .dir = IIO_EV_DIR_EITHER,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100973 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
974 BIT(IIO_EV_INFO_ENABLE) |
975 BIT(IIO_EV_INFO_PERIOD)
976};
977
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000978#define BMC150_ACCEL_CHANNEL(_axis, bits) { \
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100979 .type = IIO_ACCEL, \
980 .modified = 1, \
981 .channel2 = IIO_MOD_##_axis, \
982 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
983 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
984 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
985 .scan_index = AXIS_##_axis, \
986 .scan_type = { \
987 .sign = 's', \
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000988 .realbits = (bits), \
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100989 .storagebits = 16, \
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000990 .shift = 16 - (bits), \
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100991 }, \
992 .event_spec = &bmc150_accel_event, \
993 .num_event_specs = 1 \
994}
995
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000996#define BMC150_ACCEL_CHANNELS(bits) { \
997 { \
998 .type = IIO_TEMP, \
999 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1000 BIT(IIO_CHAN_INFO_SCALE) | \
1001 BIT(IIO_CHAN_INFO_OFFSET), \
1002 .scan_index = -1, \
1003 }, \
1004 BMC150_ACCEL_CHANNEL(X, bits), \
1005 BMC150_ACCEL_CHANNEL(Y, bits), \
1006 BMC150_ACCEL_CHANNEL(Z, bits), \
1007 IIO_CHAN_SOFT_TIMESTAMP(3), \
1008}
1009
1010static const struct iio_chan_spec bma222e_accel_channels[] =
1011 BMC150_ACCEL_CHANNELS(8);
1012static const struct iio_chan_spec bma250e_accel_channels[] =
1013 BMC150_ACCEL_CHANNELS(10);
1014static const struct iio_chan_spec bmc150_accel_channels[] =
1015 BMC150_ACCEL_CHANNELS(12);
1016static const struct iio_chan_spec bma280_accel_channels[] =
1017 BMC150_ACCEL_CHANNELS(14);
1018
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001019static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl[] = {
1020 [bmc150] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001021 .name = "BMC150A",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001022 .chip_id = 0xFA,
1023 .channels = bmc150_accel_channels,
1024 .num_channels = ARRAY_SIZE(bmc150_accel_channels),
1025 .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
1026 {19122, BMC150_ACCEL_DEF_RANGE_4G},
1027 {38344, BMC150_ACCEL_DEF_RANGE_8G},
1028 {76590, BMC150_ACCEL_DEF_RANGE_16G} },
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001029 },
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001030 [bmi055] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001031 .name = "BMI055A",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001032 .chip_id = 0xFA,
1033 .channels = bmc150_accel_channels,
1034 .num_channels = ARRAY_SIZE(bmc150_accel_channels),
1035 .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
1036 {19122, BMC150_ACCEL_DEF_RANGE_4G},
1037 {38344, BMC150_ACCEL_DEF_RANGE_8G},
1038 {76590, BMC150_ACCEL_DEF_RANGE_16G} },
1039 },
1040 [bma255] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001041 .name = "BMA0255",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001042 .chip_id = 0xFA,
1043 .channels = bmc150_accel_channels,
1044 .num_channels = ARRAY_SIZE(bmc150_accel_channels),
1045 .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
1046 {19122, BMC150_ACCEL_DEF_RANGE_4G},
1047 {38344, BMC150_ACCEL_DEF_RANGE_8G},
1048 {76590, BMC150_ACCEL_DEF_RANGE_16G} },
1049 },
1050 [bma250e] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001051 .name = "BMA250E",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001052 .chip_id = 0xF9,
1053 .channels = bma250e_accel_channels,
1054 .num_channels = ARRAY_SIZE(bma250e_accel_channels),
1055 .scale_table = { {38344, BMC150_ACCEL_DEF_RANGE_2G},
1056 {76590, BMC150_ACCEL_DEF_RANGE_4G},
1057 {153277, BMC150_ACCEL_DEF_RANGE_8G},
1058 {306457, BMC150_ACCEL_DEF_RANGE_16G} },
1059 },
1060 [bma222e] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001061 .name = "BMA222E",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001062 .chip_id = 0xF8,
1063 .channels = bma222e_accel_channels,
1064 .num_channels = ARRAY_SIZE(bma222e_accel_channels),
1065 .scale_table = { {153277, BMC150_ACCEL_DEF_RANGE_2G},
1066 {306457, BMC150_ACCEL_DEF_RANGE_4G},
1067 {612915, BMC150_ACCEL_DEF_RANGE_8G},
1068 {1225831, BMC150_ACCEL_DEF_RANGE_16G} },
1069 },
1070 [bma280] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001071 .name = "BMA0280",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001072 .chip_id = 0xFB,
1073 .channels = bma280_accel_channels,
1074 .num_channels = ARRAY_SIZE(bma280_accel_channels),
1075 .scale_table = { {2392, BMC150_ACCEL_DEF_RANGE_2G},
1076 {4785, BMC150_ACCEL_DEF_RANGE_4G},
1077 {9581, BMC150_ACCEL_DEF_RANGE_8G},
1078 {19152, BMC150_ACCEL_DEF_RANGE_16G} },
1079 },
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001080};
1081
1082static const struct iio_info bmc150_accel_info = {
1083 .attrs = &bmc150_accel_attrs_group,
1084 .read_raw = bmc150_accel_read_raw,
1085 .write_raw = bmc150_accel_write_raw,
1086 .read_event_value = bmc150_accel_read_event,
1087 .write_event_value = bmc150_accel_write_event,
1088 .write_event_config = bmc150_accel_write_event_config,
1089 .read_event_config = bmc150_accel_read_event_config,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001090 .driver_module = THIS_MODULE,
1091};
1092
Octavian Purdila3bbec972015-03-22 20:33:40 +02001093static const struct iio_info bmc150_accel_info_fifo = {
1094 .attrs = &bmc150_accel_attrs_group,
1095 .read_raw = bmc150_accel_read_raw,
1096 .write_raw = bmc150_accel_write_raw,
1097 .read_event_value = bmc150_accel_read_event,
1098 .write_event_value = bmc150_accel_write_event,
1099 .write_event_config = bmc150_accel_write_event_config,
1100 .read_event_config = bmc150_accel_read_event_config,
1101 .validate_trigger = bmc150_accel_validate_trigger,
1102 .hwfifo_set_watermark = bmc150_accel_set_watermark,
1103 .hwfifo_flush_to_buffer = bmc150_accel_fifo_flush,
1104 .driver_module = THIS_MODULE,
1105};
1106
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001107static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
1108{
1109 struct iio_poll_func *pf = p;
1110 struct iio_dev *indio_dev = pf->indio_dev;
1111 struct bmc150_accel_data *data = iio_priv(indio_dev);
1112 int bit, ret, i = 0;
Markus Pargmann4011eda2015-09-21 12:55:13 +02001113 unsigned int raw_val;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001114
1115 mutex_lock(&data->mutex);
Octavian Purdila70dddee2015-03-02 21:03:05 +02001116 for_each_set_bit(bit, indio_dev->active_scan_mask,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001117 indio_dev->masklength) {
Markus Pargmann4011eda2015-09-21 12:55:13 +02001118 ret = regmap_bulk_read(data->regmap,
1119 BMC150_ACCEL_AXIS_TO_REG(bit), &raw_val,
1120 2);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001121 if (ret < 0) {
1122 mutex_unlock(&data->mutex);
1123 goto err_read;
1124 }
Markus Pargmann4011eda2015-09-21 12:55:13 +02001125 data->buffer[i++] = raw_val;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001126 }
1127 mutex_unlock(&data->mutex);
1128
1129 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001130 pf->timestamp);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001131err_read:
1132 iio_trigger_notify_done(indio_dev->trig);
1133
1134 return IRQ_HANDLED;
1135}
1136
1137static int bmc150_accel_trig_try_reen(struct iio_trigger *trig)
1138{
Octavian Purdila7d963212015-03-03 18:17:58 +02001139 struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1140 struct bmc150_accel_data *data = t->data;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001141 int ret;
1142
1143 /* new data interrupts don't need ack */
Octavian Purdila7d963212015-03-03 18:17:58 +02001144 if (t == &t->data->triggers[BMC150_ACCEL_TRIGGER_DATA_READY])
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001145 return 0;
1146
1147 mutex_lock(&data->mutex);
1148 /* clear any latched interrupt */
Markus Pargmann4011eda2015-09-21 12:55:13 +02001149 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1150 BMC150_ACCEL_INT_MODE_LATCH_INT |
1151 BMC150_ACCEL_INT_MODE_LATCH_RESET);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001152 mutex_unlock(&data->mutex);
1153 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001154 dev_err(data->dev,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001155 "Error writing reg_int_rst_latch\n");
1156 return ret;
1157 }
1158
1159 return 0;
1160}
1161
Octavian Purdila7d963212015-03-03 18:17:58 +02001162static int bmc150_accel_trigger_set_state(struct iio_trigger *trig,
Hartmut Knaacke20008e2015-06-15 23:48:26 +02001163 bool state)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001164{
Octavian Purdila7d963212015-03-03 18:17:58 +02001165 struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1166 struct bmc150_accel_data *data = t->data;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001167 int ret;
1168
1169 mutex_lock(&data->mutex);
1170
Octavian Purdila7d963212015-03-03 18:17:58 +02001171 if (t->enabled == state) {
1172 mutex_unlock(&data->mutex);
1173 return 0;
1174 }
1175
1176 if (t->setup) {
1177 ret = t->setup(t, state);
1178 if (ret < 0) {
Octavian Purdila14ee64f2015-01-31 02:00:05 +02001179 mutex_unlock(&data->mutex);
Octavian Purdila7d963212015-03-03 18:17:58 +02001180 return ret;
Octavian Purdila14ee64f2015-01-31 02:00:05 +02001181 }
1182 }
1183
Octavian Purdila7d963212015-03-03 18:17:58 +02001184 ret = bmc150_accel_set_interrupt(data, t->intr, state);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001185 if (ret < 0) {
1186 mutex_unlock(&data->mutex);
1187 return ret;
1188 }
Octavian Purdila7d963212015-03-03 18:17:58 +02001189
1190 t->enabled = state;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001191
1192 mutex_unlock(&data->mutex);
1193
1194 return ret;
1195}
1196
1197static const struct iio_trigger_ops bmc150_accel_trigger_ops = {
Octavian Purdila7d963212015-03-03 18:17:58 +02001198 .set_trigger_state = bmc150_accel_trigger_set_state,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001199 .try_reenable = bmc150_accel_trig_try_reen,
1200 .owner = THIS_MODULE,
1201};
1202
Octavian Purdila3bbec972015-03-22 20:33:40 +02001203static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001204{
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001205 struct bmc150_accel_data *data = iio_priv(indio_dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001206 int dir;
Octavian Purdila3bbec972015-03-22 20:33:40 +02001207 int ret;
Markus Pargmann4011eda2015-09-21 12:55:13 +02001208 unsigned int val;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001209
Markus Pargmann4011eda2015-09-21 12:55:13 +02001210 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001211 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001212 dev_err(data->dev, "Error reading reg_int_status_2\n");
Octavian Purdila3bbec972015-03-22 20:33:40 +02001213 return ret;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001214 }
1215
Markus Pargmann4011eda2015-09-21 12:55:13 +02001216 if (val & BMC150_ACCEL_ANY_MOTION_BIT_SIGN)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001217 dir = IIO_EV_DIR_FALLING;
1218 else
1219 dir = IIO_EV_DIR_RISING;
1220
Markus Pargmann4011eda2015-09-21 12:55:13 +02001221 if (val & BMC150_ACCEL_ANY_MOTION_BIT_X)
Hartmut Knaacke20008e2015-06-15 23:48:26 +02001222 iio_push_event(indio_dev,
1223 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1224 0,
1225 IIO_MOD_X,
1226 IIO_EV_TYPE_ROC,
1227 dir),
1228 data->timestamp);
1229
Markus Pargmann4011eda2015-09-21 12:55:13 +02001230 if (val & BMC150_ACCEL_ANY_MOTION_BIT_Y)
Hartmut Knaacke20008e2015-06-15 23:48:26 +02001231 iio_push_event(indio_dev,
1232 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1233 0,
1234 IIO_MOD_Y,
1235 IIO_EV_TYPE_ROC,
1236 dir),
1237 data->timestamp);
1238
Markus Pargmann4011eda2015-09-21 12:55:13 +02001239 if (val & BMC150_ACCEL_ANY_MOTION_BIT_Z)
Hartmut Knaacke20008e2015-06-15 23:48:26 +02001240 iio_push_event(indio_dev,
1241 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1242 0,
1243 IIO_MOD_Z,
1244 IIO_EV_TYPE_ROC,
1245 dir),
1246 data->timestamp);
1247
Octavian Purdila3bbec972015-03-22 20:33:40 +02001248 return ret;
1249}
1250
1251static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private)
1252{
1253 struct iio_dev *indio_dev = private;
1254 struct bmc150_accel_data *data = iio_priv(indio_dev);
1255 bool ack = false;
1256 int ret;
1257
1258 mutex_lock(&data->mutex);
1259
1260 if (data->fifo_mode) {
1261 ret = __bmc150_accel_fifo_flush(indio_dev,
1262 BMC150_ACCEL_FIFO_LENGTH, true);
1263 if (ret > 0)
1264 ack = true;
1265 }
1266
1267 if (data->ev_enable_state) {
1268 ret = bmc150_accel_handle_roc_event(indio_dev);
1269 if (ret > 0)
1270 ack = true;
1271 }
1272
1273 if (ack) {
Markus Pargmann4011eda2015-09-21 12:55:13 +02001274 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1275 BMC150_ACCEL_INT_MODE_LATCH_INT |
1276 BMC150_ACCEL_INT_MODE_LATCH_RESET);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001277 if (ret)
Markus Pargmann19c95d62015-09-21 12:55:14 +02001278 dev_err(data->dev, "Error writing reg_int_rst_latch\n");
Hartmut Knaacke20008e2015-06-15 23:48:26 +02001279
Octavian Purdila3bbec972015-03-22 20:33:40 +02001280 ret = IRQ_HANDLED;
1281 } else {
1282 ret = IRQ_NONE;
1283 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001284
Octavian Purdila3bbec972015-03-22 20:33:40 +02001285 mutex_unlock(&data->mutex);
1286
1287 return ret;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001288}
1289
Octavian Purdila3bbec972015-03-22 20:33:40 +02001290static irqreturn_t bmc150_accel_irq_handler(int irq, void *private)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001291{
1292 struct iio_dev *indio_dev = private;
1293 struct bmc150_accel_data *data = iio_priv(indio_dev);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001294 bool ack = false;
Octavian Purdila7d963212015-03-03 18:17:58 +02001295 int i;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001296
Octavian Purdila3bbec972015-03-22 20:33:40 +02001297 data->old_timestamp = data->timestamp;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001298 data->timestamp = iio_get_time_ns();
1299
Octavian Purdila7d963212015-03-03 18:17:58 +02001300 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1301 if (data->triggers[i].enabled) {
1302 iio_trigger_poll(data->triggers[i].indio_trig);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001303 ack = true;
Octavian Purdila7d963212015-03-03 18:17:58 +02001304 break;
1305 }
1306 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001307
Octavian Purdila3bbec972015-03-22 20:33:40 +02001308 if (data->ev_enable_state || data->fifo_mode)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001309 return IRQ_WAKE_THREAD;
Octavian Purdila3bbec972015-03-22 20:33:40 +02001310
1311 if (ack)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001312 return IRQ_HANDLED;
Octavian Purdila3bbec972015-03-22 20:33:40 +02001313
1314 return IRQ_NONE;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001315}
1316
Octavian Purdila7d963212015-03-03 18:17:58 +02001317static const struct {
1318 int intr;
1319 const char *name;
1320 int (*setup)(struct bmc150_accel_trigger *t, bool state);
1321} bmc150_accel_triggers[BMC150_ACCEL_TRIGGERS] = {
1322 {
1323 .intr = 0,
1324 .name = "%s-dev%d",
1325 },
1326 {
1327 .intr = 1,
1328 .name = "%s-any-motion-dev%d",
1329 .setup = bmc150_accel_any_motion_setup,
1330 },
1331};
1332
1333static void bmc150_accel_unregister_triggers(struct bmc150_accel_data *data,
1334 int from)
1335{
1336 int i;
1337
Hartmut Knaack7a1d0d92015-06-15 23:48:24 +02001338 for (i = from; i >= 0; i--) {
Octavian Purdila7d963212015-03-03 18:17:58 +02001339 if (data->triggers[i].indio_trig) {
1340 iio_trigger_unregister(data->triggers[i].indio_trig);
1341 data->triggers[i].indio_trig = NULL;
1342 }
1343 }
1344}
1345
1346static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev,
1347 struct bmc150_accel_data *data)
1348{
1349 int i, ret;
1350
1351 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1352 struct bmc150_accel_trigger *t = &data->triggers[i];
1353
Markus Pargmann19c95d62015-09-21 12:55:14 +02001354 t->indio_trig = devm_iio_trigger_alloc(data->dev,
Octavian Purdila7d963212015-03-03 18:17:58 +02001355 bmc150_accel_triggers[i].name,
1356 indio_dev->name,
1357 indio_dev->id);
1358 if (!t->indio_trig) {
1359 ret = -ENOMEM;
1360 break;
1361 }
1362
Markus Pargmann19c95d62015-09-21 12:55:14 +02001363 t->indio_trig->dev.parent = data->dev;
Octavian Purdila7d963212015-03-03 18:17:58 +02001364 t->indio_trig->ops = &bmc150_accel_trigger_ops;
1365 t->intr = bmc150_accel_triggers[i].intr;
1366 t->data = data;
1367 t->setup = bmc150_accel_triggers[i].setup;
1368 iio_trigger_set_drvdata(t->indio_trig, t);
1369
1370 ret = iio_trigger_register(t->indio_trig);
1371 if (ret)
1372 break;
1373 }
1374
1375 if (ret)
1376 bmc150_accel_unregister_triggers(data, i - 1);
1377
1378 return ret;
1379}
1380
Octavian Purdila3bbec972015-03-22 20:33:40 +02001381#define BMC150_ACCEL_FIFO_MODE_STREAM 0x80
1382#define BMC150_ACCEL_FIFO_MODE_FIFO 0x40
1383#define BMC150_ACCEL_FIFO_MODE_BYPASS 0x00
1384
1385static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data)
1386{
1387 u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1;
1388 int ret;
1389
Markus Pargmann4011eda2015-09-21 12:55:13 +02001390 ret = regmap_write(data->regmap, reg, data->fifo_mode);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001391 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001392 dev_err(data->dev, "Error writing reg_fifo_config1\n");
Octavian Purdila3bbec972015-03-22 20:33:40 +02001393 return ret;
1394 }
1395
1396 if (!data->fifo_mode)
1397 return 0;
1398
Markus Pargmann4011eda2015-09-21 12:55:13 +02001399 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0,
1400 data->watermark);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001401 if (ret < 0)
Markus Pargmann19c95d62015-09-21 12:55:14 +02001402 dev_err(data->dev, "Error writing reg_fifo_config0\n");
Octavian Purdila3bbec972015-03-22 20:33:40 +02001403
1404 return ret;
1405}
1406
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001407static int bmc150_accel_buffer_preenable(struct iio_dev *indio_dev)
1408{
1409 struct bmc150_accel_data *data = iio_priv(indio_dev);
1410
1411 return bmc150_accel_set_power_state(data, true);
1412}
1413
Octavian Purdila3bbec972015-03-22 20:33:40 +02001414static int bmc150_accel_buffer_postenable(struct iio_dev *indio_dev)
1415{
1416 struct bmc150_accel_data *data = iio_priv(indio_dev);
1417 int ret = 0;
1418
1419 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
1420 return iio_triggered_buffer_postenable(indio_dev);
1421
1422 mutex_lock(&data->mutex);
1423
1424 if (!data->watermark)
1425 goto out;
1426
1427 ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
1428 true);
1429 if (ret)
1430 goto out;
1431
1432 data->fifo_mode = BMC150_ACCEL_FIFO_MODE_FIFO;
1433
1434 ret = bmc150_accel_fifo_set_mode(data);
1435 if (ret) {
1436 data->fifo_mode = 0;
1437 bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
1438 false);
1439 }
1440
1441out:
1442 mutex_unlock(&data->mutex);
1443
1444 return ret;
1445}
1446
1447static int bmc150_accel_buffer_predisable(struct iio_dev *indio_dev)
1448{
1449 struct bmc150_accel_data *data = iio_priv(indio_dev);
1450
1451 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
1452 return iio_triggered_buffer_predisable(indio_dev);
1453
1454 mutex_lock(&data->mutex);
1455
1456 if (!data->fifo_mode)
1457 goto out;
1458
1459 bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK, false);
1460 __bmc150_accel_fifo_flush(indio_dev, BMC150_ACCEL_FIFO_LENGTH, false);
1461 data->fifo_mode = 0;
1462 bmc150_accel_fifo_set_mode(data);
1463
1464out:
1465 mutex_unlock(&data->mutex);
1466
1467 return 0;
1468}
1469
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001470static int bmc150_accel_buffer_postdisable(struct iio_dev *indio_dev)
1471{
1472 struct bmc150_accel_data *data = iio_priv(indio_dev);
1473
1474 return bmc150_accel_set_power_state(data, false);
1475}
1476
Octavian Purdila3bbec972015-03-22 20:33:40 +02001477static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops = {
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001478 .preenable = bmc150_accel_buffer_preenable,
Octavian Purdila3bbec972015-03-22 20:33:40 +02001479 .postenable = bmc150_accel_buffer_postenable,
1480 .predisable = bmc150_accel_buffer_predisable,
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001481 .postdisable = bmc150_accel_buffer_postdisable,
Octavian Purdila3bbec972015-03-22 20:33:40 +02001482};
1483
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001484static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
1485{
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001486 int ret, i;
Markus Pargmann4011eda2015-09-21 12:55:13 +02001487 unsigned int val;
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001488
Markus Pargmann4011eda2015-09-21 12:55:13 +02001489 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val);
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001490 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001491 dev_err(data->dev,
Markus Pargmann4011eda2015-09-21 12:55:13 +02001492 "Error: Reading chip id\n");
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001493 return ret;
1494 }
1495
Markus Pargmann19c95d62015-09-21 12:55:14 +02001496 dev_dbg(data->dev, "Chip Id %x\n", val);
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001497 for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) {
Markus Pargmann4011eda2015-09-21 12:55:13 +02001498 if (bmc150_accel_chip_info_tbl[i].chip_id == val) {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001499 data->chip_info = &bmc150_accel_chip_info_tbl[i];
1500 break;
1501 }
1502 }
1503
1504 if (!data->chip_info) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001505 dev_err(data->dev, "Invalid chip %x\n", val);
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001506 return -ENODEV;
1507 }
1508
1509 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1510 if (ret < 0)
1511 return ret;
1512
1513 /* Set Bandwidth */
1514 ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0);
1515 if (ret < 0)
1516 return ret;
1517
1518 /* Set Default Range */
Markus Pargmann4011eda2015-09-21 12:55:13 +02001519 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE,
1520 BMC150_ACCEL_DEF_RANGE_4G);
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001521 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001522 dev_err(data->dev,
Markus Pargmann4011eda2015-09-21 12:55:13 +02001523 "Error writing reg_pmu_range\n");
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001524 return ret;
1525 }
1526
1527 data->range = BMC150_ACCEL_DEF_RANGE_4G;
1528
1529 /* Set default slope duration and thresholds */
1530 data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD;
1531 data->slope_dur = BMC150_ACCEL_DEF_SLOPE_DURATION;
1532 ret = bmc150_accel_update_slope(data);
1533 if (ret < 0)
1534 return ret;
1535
1536 /* Set default as latched interrupts */
Markus Pargmann4011eda2015-09-21 12:55:13 +02001537 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1538 BMC150_ACCEL_INT_MODE_LATCH_INT |
1539 BMC150_ACCEL_INT_MODE_LATCH_RESET);
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001540 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001541 dev_err(data->dev,
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001542 "Error writing reg_int_rst_latch\n");
1543 return ret;
1544 }
1545
1546 return 0;
1547}
1548
Markus Pargmann55637c32015-09-21 12:55:15 +02001549int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
1550 const char *name, bool block_supported)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001551{
1552 struct bmc150_accel_data *data;
1553 struct iio_dev *indio_dev;
1554 int ret;
1555
Markus Pargmann55637c32015-09-21 12:55:15 +02001556 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001557 if (!indio_dev)
1558 return -ENOMEM;
1559
1560 data = iio_priv(indio_dev);
Markus Pargmann55637c32015-09-21 12:55:15 +02001561 dev_set_drvdata(dev, indio_dev);
1562 data->dev = dev;
1563 data->irq = irq;
Markus Pargmann4011eda2015-09-21 12:55:13 +02001564
Markus Pargmann55637c32015-09-21 12:55:15 +02001565 data->regmap = regmap;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001566
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001567 ret = bmc150_accel_chip_init(data);
1568 if (ret < 0)
1569 return ret;
1570
1571 mutex_init(&data->mutex);
1572
Markus Pargmann19c95d62015-09-21 12:55:14 +02001573 indio_dev->dev.parent = dev;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001574 indio_dev->channels = data->chip_info->channels;
1575 indio_dev->num_channels = data->chip_info->num_channels;
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001576 indio_dev->name = name ? name : data->chip_info->name;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001577 indio_dev->modes = INDIO_DIRECT_MODE;
1578 indio_dev->info = &bmc150_accel_info;
1579
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001580 ret = iio_triggered_buffer_setup(indio_dev,
1581 &iio_pollfunc_store_time,
1582 bmc150_accel_trigger_handler,
1583 &bmc150_accel_buffer_ops);
1584 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001585 dev_err(data->dev, "Failed: iio triggered buffer setup\n");
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001586 return ret;
1587 }
1588
Markus Pargmann19c95d62015-09-21 12:55:14 +02001589 if (data->irq > 0) {
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001590 ret = devm_request_threaded_irq(
Markus Pargmann19c95d62015-09-21 12:55:14 +02001591 data->dev, data->irq,
Octavian Purdila3bbec972015-03-22 20:33:40 +02001592 bmc150_accel_irq_handler,
1593 bmc150_accel_irq_thread_handler,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001594 IRQF_TRIGGER_RISING,
1595 BMC150_ACCEL_IRQ_NAME,
1596 indio_dev);
1597 if (ret)
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001598 goto err_buffer_cleanup;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001599
Octavian Purdila8e22f472015-01-31 02:00:04 +02001600 /*
1601 * Set latched mode interrupt. While certain interrupts are
1602 * non-latched regardless of this settings (e.g. new data) we
1603 * want to use latch mode when we can to prevent interrupt
1604 * flooding.
1605 */
Markus Pargmann4011eda2015-09-21 12:55:13 +02001606 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1607 BMC150_ACCEL_INT_MODE_LATCH_RESET);
Octavian Purdila8e22f472015-01-31 02:00:04 +02001608 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001609 dev_err(data->dev, "Error writing reg_int_rst_latch\n");
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001610 goto err_buffer_cleanup;
Octavian Purdila8e22f472015-01-31 02:00:04 +02001611 }
1612
Octavian Purdila3e825ec2015-03-03 18:17:57 +02001613 bmc150_accel_interrupts_setup(indio_dev, data);
1614
Octavian Purdila7d963212015-03-03 18:17:58 +02001615 ret = bmc150_accel_triggers_setup(indio_dev, data);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001616 if (ret)
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001617 goto err_buffer_cleanup;
Octavian Purdila3bbec972015-03-22 20:33:40 +02001618
Markus Pargmann55637c32015-09-21 12:55:15 +02001619 if (block_supported) {
Octavian Purdila3bbec972015-03-22 20:33:40 +02001620 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
1621 indio_dev->info = &bmc150_accel_info_fifo;
1622 indio_dev->buffer->attrs = bmc150_accel_fifo_attributes;
1623 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001624 }
1625
Adriana Reus7d0ead52015-11-05 16:25:29 +02001626 ret = pm_runtime_set_active(dev);
1627 if (ret)
1628 goto err_trigger_unregister;
1629
1630 pm_runtime_enable(dev);
1631 pm_runtime_set_autosuspend_delay(dev, BMC150_AUTO_SUSPEND_DELAY_MS);
1632 pm_runtime_use_autosuspend(dev);
1633
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001634 ret = iio_device_register(indio_dev);
1635 if (ret < 0) {
Markus Pargmann55637c32015-09-21 12:55:15 +02001636 dev_err(dev, "Unable to register iio device\n");
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001637 goto err_trigger_unregister;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001638 }
1639
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001640 return 0;
1641
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001642err_trigger_unregister:
Octavian Purdila7d963212015-03-03 18:17:58 +02001643 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001644err_buffer_cleanup:
1645 iio_triggered_buffer_cleanup(indio_dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001646
1647 return ret;
1648}
Markus Pargmann55637c32015-09-21 12:55:15 +02001649EXPORT_SYMBOL_GPL(bmc150_accel_core_probe);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001650
Markus Pargmann55637c32015-09-21 12:55:15 +02001651int bmc150_accel_core_remove(struct device *dev)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001652{
Markus Pargmann55637c32015-09-21 12:55:15 +02001653 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001654 struct bmc150_accel_data *data = iio_priv(indio_dev);
1655
Adriana Reus7d0ead52015-11-05 16:25:29 +02001656 iio_device_unregister(indio_dev);
1657
Markus Pargmann19c95d62015-09-21 12:55:14 +02001658 pm_runtime_disable(data->dev);
1659 pm_runtime_set_suspended(data->dev);
1660 pm_runtime_put_noidle(data->dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001661
Octavian Purdila7d963212015-03-03 18:17:58 +02001662 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001663
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001664 iio_triggered_buffer_cleanup(indio_dev);
1665
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001666 mutex_lock(&data->mutex);
1667 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0);
1668 mutex_unlock(&data->mutex);
1669
1670 return 0;
1671}
Markus Pargmann55637c32015-09-21 12:55:15 +02001672EXPORT_SYMBOL_GPL(bmc150_accel_core_remove);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001673
1674#ifdef CONFIG_PM_SLEEP
1675static int bmc150_accel_suspend(struct device *dev)
1676{
Markus Pargmann19c95d62015-09-21 12:55:14 +02001677 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001678 struct bmc150_accel_data *data = iio_priv(indio_dev);
1679
1680 mutex_lock(&data->mutex);
1681 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1682 mutex_unlock(&data->mutex);
1683
1684 return 0;
1685}
1686
1687static int bmc150_accel_resume(struct device *dev)
1688{
Markus Pargmann19c95d62015-09-21 12:55:14 +02001689 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001690 struct bmc150_accel_data *data = iio_priv(indio_dev);
1691
1692 mutex_lock(&data->mutex);
Octavian Purdila3e825ec2015-03-03 18:17:57 +02001693 if (atomic_read(&data->active_intr))
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001694 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001695 bmc150_accel_fifo_set_mode(data);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001696 mutex_unlock(&data->mutex);
1697
1698 return 0;
1699}
1700#endif
1701
Rafael J. Wysocki6f0a13f2014-12-04 01:08:13 +01001702#ifdef CONFIG_PM
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001703static int bmc150_accel_runtime_suspend(struct device *dev)
1704{
Markus Pargmann19c95d62015-09-21 12:55:14 +02001705 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001706 struct bmc150_accel_data *data = iio_priv(indio_dev);
Srinivas Pandruvadaaaeecd82014-10-10 20:35:31 -07001707 int ret;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001708
Markus Pargmann19c95d62015-09-21 12:55:14 +02001709 dev_dbg(data->dev, __func__);
Srinivas Pandruvadaaaeecd82014-10-10 20:35:31 -07001710 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1711 if (ret < 0)
1712 return -EAGAIN;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001713
Srinivas Pandruvadaaaeecd82014-10-10 20:35:31 -07001714 return 0;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001715}
1716
1717static int bmc150_accel_runtime_resume(struct device *dev)
1718{
Markus Pargmann19c95d62015-09-21 12:55:14 +02001719 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001720 struct bmc150_accel_data *data = iio_priv(indio_dev);
1721 int ret;
1722 int sleep_val;
1723
Markus Pargmann19c95d62015-09-21 12:55:14 +02001724 dev_dbg(data->dev, __func__);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001725
1726 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1727 if (ret < 0)
1728 return ret;
Octavian Purdila3bbec972015-03-22 20:33:40 +02001729 ret = bmc150_accel_fifo_set_mode(data);
1730 if (ret < 0)
1731 return ret;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001732
1733 sleep_val = bmc150_accel_get_startup_times(data);
1734 if (sleep_val < 20)
1735 usleep_range(sleep_val * 1000, 20000);
1736 else
1737 msleep_interruptible(sleep_val);
1738
1739 return 0;
1740}
1741#endif
1742
Markus Pargmann55637c32015-09-21 12:55:15 +02001743const struct dev_pm_ops bmc150_accel_pm_ops = {
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001744 SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend, bmc150_accel_resume)
1745 SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend,
1746 bmc150_accel_runtime_resume, NULL)
1747};
Markus Pargmann55637c32015-09-21 12:55:15 +02001748EXPORT_SYMBOL_GPL(bmc150_accel_pm_ops);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001749
1750MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1751MODULE_LICENSE("GPL v2");
1752MODULE_DESCRIPTION("BMC150 accelerometer driver");