blob: cc52366ad4ad5d858d1ac869d5f8712552153ce6 [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,
Irina Tirdea23e758b2016-03-24 11:29:26 +0200141 AXIS_MAX,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100142};
143
144enum bmc150_power_modes {
145 BMC150_ACCEL_SLEEP_MODE_NORMAL,
146 BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND,
147 BMC150_ACCEL_SLEEP_MODE_LPM,
148 BMC150_ACCEL_SLEEP_MODE_SUSPEND = 0x04,
149};
150
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000151struct bmc150_scale_info {
152 int scale;
153 u8 reg_range;
154};
155
156struct bmc150_accel_chip_info {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +0200157 const char *name;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000158 u8 chip_id;
159 const struct iio_chan_spec *channels;
160 int num_channels;
161 const struct bmc150_scale_info scale_table[4];
162};
163
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200164struct bmc150_accel_interrupt {
165 const struct bmc150_accel_interrupt_info *info;
166 atomic_t users;
167};
168
Octavian Purdila7d963212015-03-03 18:17:58 +0200169struct bmc150_accel_trigger {
170 struct bmc150_accel_data *data;
171 struct iio_trigger *indio_trig;
172 int (*setup)(struct bmc150_accel_trigger *t, bool state);
173 int intr;
174 bool enabled;
175};
176
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200177enum bmc150_accel_interrupt_id {
178 BMC150_ACCEL_INT_DATA_READY,
179 BMC150_ACCEL_INT_ANY_MOTION,
180 BMC150_ACCEL_INT_WATERMARK,
181 BMC150_ACCEL_INTERRUPTS,
182};
183
Octavian Purdila7d963212015-03-03 18:17:58 +0200184enum bmc150_accel_trigger_id {
185 BMC150_ACCEL_TRIGGER_DATA_READY,
186 BMC150_ACCEL_TRIGGER_ANY_MOTION,
187 BMC150_ACCEL_TRIGGERS,
188};
189
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100190struct bmc150_accel_data {
Markus Pargmann4011eda2015-09-21 12:55:13 +0200191 struct regmap *regmap;
192 struct device *dev;
Markus Pargmann19c95d62015-09-21 12:55:14 +0200193 int irq;
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200194 struct bmc150_accel_interrupt interrupts[BMC150_ACCEL_INTERRUPTS];
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200195 atomic_t active_intr;
Octavian Purdila7d963212015-03-03 18:17:58 +0200196 struct bmc150_accel_trigger triggers[BMC150_ACCEL_TRIGGERS];
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100197 struct mutex mutex;
Octavian Purdila3bbec972015-03-22 20:33:40 +0200198 u8 fifo_mode, watermark;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100199 s16 buffer[8];
200 u8 bw_bits;
201 u32 slope_dur;
202 u32 slope_thres;
203 u32 range;
204 int ev_enable_state;
Vlad Dogaruc16bff42015-05-12 17:03:24 +0300205 int64_t timestamp, old_timestamp; /* Only used in hw fifo mode. */
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000206 const struct bmc150_accel_chip_info *chip_info;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100207};
208
209static const struct {
210 int val;
211 int val2;
212 u8 bw_bits;
Sathyanarayanan Kuppuswamy0ba8da92015-03-03 18:17:56 +0200213} bmc150_accel_samp_freq_table[] = { {15, 620000, 0x08},
214 {31, 260000, 0x09},
215 {62, 500000, 0x0A},
216 {125, 0, 0x0B},
217 {250, 0, 0x0C},
218 {500, 0, 0x0D},
219 {1000, 0, 0x0E},
220 {2000, 0, 0x0F} };
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100221
222static const struct {
223 int bw_bits;
224 int msec;
225} bmc150_accel_sample_upd_time[] = { {0x08, 64},
226 {0x09, 32},
227 {0x0A, 16},
228 {0x0B, 8},
229 {0x0C, 4},
230 {0x0D, 2},
231 {0x0E, 1},
232 {0x0F, 1} };
233
234static const struct {
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100235 int sleep_dur;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000236 u8 reg_value;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100237} bmc150_accel_sleep_value_table[] = { {0, 0},
238 {500, BMC150_ACCEL_SLEEP_500_MICRO},
239 {1000, BMC150_ACCEL_SLEEP_1_MS},
240 {2000, BMC150_ACCEL_SLEEP_2_MS},
241 {4000, BMC150_ACCEL_SLEEP_4_MS},
242 {6000, BMC150_ACCEL_SLEEP_6_MS},
243 {10000, BMC150_ACCEL_SLEEP_10_MS},
244 {25000, BMC150_ACCEL_SLEEP_25_MS},
245 {50000, BMC150_ACCEL_SLEEP_50_MS},
246 {100000, BMC150_ACCEL_SLEEP_100_MS},
247 {500000, BMC150_ACCEL_SLEEP_500_MS},
248 {1000000, BMC150_ACCEL_SLEEP_1_SEC} };
249
Markus Pargmann4011eda2015-09-21 12:55:13 +0200250static const struct regmap_config bmc150_i2c_regmap_conf = {
251 .reg_bits = 8,
252 .val_bits = 8,
253 .max_register = 0x3f,
254};
255
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100256static int bmc150_accel_set_mode(struct bmc150_accel_data *data,
257 enum bmc150_power_modes mode,
258 int dur_us)
259{
260 int i;
261 int ret;
262 u8 lpw_bits;
263 int dur_val = -1;
264
265 if (dur_us > 0) {
266 for (i = 0; i < ARRAY_SIZE(bmc150_accel_sleep_value_table);
267 ++i) {
268 if (bmc150_accel_sleep_value_table[i].sleep_dur ==
269 dur_us)
270 dur_val =
271 bmc150_accel_sleep_value_table[i].reg_value;
272 }
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200273 } else {
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100274 dur_val = 0;
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200275 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100276
277 if (dur_val < 0)
278 return -EINVAL;
279
280 lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT;
281 lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT);
282
Markus Pargmann19c95d62015-09-21 12:55:14 +0200283 dev_dbg(data->dev, "Set Mode bits %x\n", lpw_bits);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100284
Markus Pargmann4011eda2015-09-21 12:55:13 +0200285 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100286 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200287 dev_err(data->dev, "Error writing reg_pmu_lpw\n");
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100288 return ret;
289 }
290
291 return 0;
292}
293
294static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val,
295 int val2)
296{
297 int i;
298 int ret;
299
300 for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
301 if (bmc150_accel_samp_freq_table[i].val == val &&
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200302 bmc150_accel_samp_freq_table[i].val2 == val2) {
Markus Pargmann4011eda2015-09-21 12:55:13 +0200303 ret = regmap_write(data->regmap,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100304 BMC150_ACCEL_REG_PMU_BW,
305 bmc150_accel_samp_freq_table[i].bw_bits);
306 if (ret < 0)
307 return ret;
308
309 data->bw_bits =
310 bmc150_accel_samp_freq_table[i].bw_bits;
311 return 0;
312 }
313 }
314
315 return -EINVAL;
316}
317
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200318static int bmc150_accel_update_slope(struct bmc150_accel_data *data)
319{
Markus Pargmann4011eda2015-09-21 12:55:13 +0200320 int ret;
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200321
Markus Pargmann4011eda2015-09-21 12:55:13 +0200322 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6,
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200323 data->slope_thres);
324 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200325 dev_err(data->dev, "Error writing reg_int_6\n");
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200326 return ret;
327 }
328
Markus Pargmann4011eda2015-09-21 12:55:13 +0200329 ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5,
330 BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur);
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200331 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200332 dev_err(data->dev, "Error updating reg_int_5\n");
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200333 return ret;
334 }
335
Markus Pargmann19c95d62015-09-21 12:55:14 +0200336 dev_dbg(data->dev, "%s: %x %x\n", __func__, data->slope_thres,
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200337 data->slope_dur);
338
339 return ret;
340}
341
Octavian Purdila7d963212015-03-03 18:17:58 +0200342static int bmc150_accel_any_motion_setup(struct bmc150_accel_trigger *t,
343 bool state)
344{
345 if (state)
346 return bmc150_accel_update_slope(t->data);
347
348 return 0;
349}
350
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100351static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val,
352 int *val2)
353{
354 int i;
355
356 for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
357 if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) {
358 *val = bmc150_accel_samp_freq_table[i].val;
359 *val2 = bmc150_accel_samp_freq_table[i].val2;
360 return IIO_VAL_INT_PLUS_MICRO;
361 }
362 }
363
364 return -EINVAL;
365}
366
Rafael J. Wysocki6f0a13f2014-12-04 01:08:13 +0100367#ifdef CONFIG_PM
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100368static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data)
369{
370 int i;
371
372 for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) {
373 if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits)
374 return bmc150_accel_sample_upd_time[i].msec;
375 }
376
377 return BMC150_ACCEL_MAX_STARTUP_TIME_MS;
378}
379
380static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
381{
382 int ret;
383
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200384 if (on) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200385 ret = pm_runtime_get_sync(data->dev);
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200386 } else {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200387 pm_runtime_mark_last_busy(data->dev);
388 ret = pm_runtime_put_autosuspend(data->dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100389 }
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200390
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100391 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200392 dev_err(data->dev,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100393 "Failed: bmc150_accel_set_power_state for %d\n", on);
Srinivas Pandruvadaaaeecd82014-10-10 20:35:31 -0700394 if (on)
Markus Pargmann19c95d62015-09-21 12:55:14 +0200395 pm_runtime_put_noidle(data->dev);
Srinivas Pandruvadaaaeecd82014-10-10 20:35:31 -0700396
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100397 return ret;
398 }
399
400 return 0;
401}
Laurentiu Palcub31b05c2014-08-29 09:38:00 +0100402#else
403static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
404{
405 return 0;
406}
407#endif
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100408
Octavian Purdila8e22f472015-01-31 02:00:04 +0200409static const struct bmc150_accel_interrupt_info {
410 u8 map_reg;
411 u8 map_bitmask;
412 u8 en_reg;
413 u8 en_bitmask;
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200414} bmc150_accel_interrupts[BMC150_ACCEL_INTERRUPTS] = {
Octavian Purdila8e22f472015-01-31 02:00:04 +0200415 { /* data ready interrupt */
416 .map_reg = BMC150_ACCEL_REG_INT_MAP_1,
417 .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_DATA,
418 .en_reg = BMC150_ACCEL_REG_INT_EN_1,
419 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_DATA_EN,
420 },
421 { /* motion interrupt */
422 .map_reg = BMC150_ACCEL_REG_INT_MAP_0,
423 .map_bitmask = BMC150_ACCEL_INT_MAP_0_BIT_SLOPE,
424 .en_reg = BMC150_ACCEL_REG_INT_EN_0,
425 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_SLP_X |
426 BMC150_ACCEL_INT_EN_BIT_SLP_Y |
427 BMC150_ACCEL_INT_EN_BIT_SLP_Z
428 },
Octavian Purdila3bbec972015-03-22 20:33:40 +0200429 { /* fifo watermark interrupt */
430 .map_reg = BMC150_ACCEL_REG_INT_MAP_1,
431 .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_FWM,
432 .en_reg = BMC150_ACCEL_REG_INT_EN_1,
433 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_FWM_EN,
434 },
Octavian Purdila8e22f472015-01-31 02:00:04 +0200435};
436
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200437static void bmc150_accel_interrupts_setup(struct iio_dev *indio_dev,
438 struct bmc150_accel_data *data)
439{
440 int i;
441
442 for (i = 0; i < BMC150_ACCEL_INTERRUPTS; i++)
443 data->interrupts[i].info = &bmc150_accel_interrupts[i];
444}
445
446static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i,
Octavian Purdila8e22f472015-01-31 02:00:04 +0200447 bool state)
448{
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200449 struct bmc150_accel_interrupt *intr = &data->interrupts[i];
450 const struct bmc150_accel_interrupt_info *info = intr->info;
Octavian Purdila8e22f472015-01-31 02:00:04 +0200451 int ret;
452
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200453 if (state) {
454 if (atomic_inc_return(&intr->users) > 1)
455 return 0;
456 } else {
457 if (atomic_dec_return(&intr->users) > 0)
458 return 0;
459 }
460
Octavian Purdila8e22f472015-01-31 02:00:04 +0200461 /*
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200462 * We will expect the enable and disable to do operation in reverse
463 * order. This will happen here anyway, as our resume operation uses
464 * sync mode runtime pm calls. The suspend operation will be delayed
465 * by autosuspend delay.
466 * So the disable operation will still happen in reverse order of
467 * enable operation. When runtime pm is disabled the mode is always on,
468 * so sequence doesn't matter.
Octavian Purdila8e22f472015-01-31 02:00:04 +0200469 */
470 ret = bmc150_accel_set_power_state(data, state);
471 if (ret < 0)
472 return ret;
473
474 /* map the interrupt to the appropriate pins */
Markus Pargmann4011eda2015-09-21 12:55:13 +0200475 ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask,
476 (state ? info->map_bitmask : 0));
Octavian Purdila8e22f472015-01-31 02:00:04 +0200477 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200478 dev_err(data->dev, "Error updating reg_int_map\n");
Octavian Purdila8e22f472015-01-31 02:00:04 +0200479 goto out_fix_power_state;
480 }
481
482 /* enable/disable the interrupt */
Markus Pargmann4011eda2015-09-21 12:55:13 +0200483 ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask,
484 (state ? info->en_bitmask : 0));
Octavian Purdila8e22f472015-01-31 02:00:04 +0200485 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200486 dev_err(data->dev, "Error updating reg_int_en\n");
Octavian Purdila8e22f472015-01-31 02:00:04 +0200487 goto out_fix_power_state;
488 }
489
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200490 if (state)
491 atomic_inc(&data->active_intr);
492 else
493 atomic_dec(&data->active_intr);
494
Octavian Purdila8e22f472015-01-31 02:00:04 +0200495 return 0;
496
497out_fix_power_state:
498 bmc150_accel_set_power_state(data, false);
499 return ret;
500}
501
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100502static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
503{
504 int ret, i;
505
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000506 for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) {
507 if (data->chip_info->scale_table[i].scale == val) {
Markus Pargmann4011eda2015-09-21 12:55:13 +0200508 ret = regmap_write(data->regmap,
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000509 BMC150_ACCEL_REG_PMU_RANGE,
510 data->chip_info->scale_table[i].reg_range);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100511 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200512 dev_err(data->dev,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100513 "Error writing pmu_range\n");
514 return ret;
515 }
516
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000517 data->range = data->chip_info->scale_table[i].reg_range;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100518 return 0;
519 }
520 }
521
522 return -EINVAL;
523}
524
525static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val)
526{
527 int ret;
Markus Pargmann4011eda2015-09-21 12:55:13 +0200528 unsigned int value;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100529
530 mutex_lock(&data->mutex);
531
Markus Pargmann4011eda2015-09-21 12:55:13 +0200532 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100533 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200534 dev_err(data->dev, "Error reading reg_temp\n");
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100535 mutex_unlock(&data->mutex);
536 return ret;
537 }
Markus Pargmann4011eda2015-09-21 12:55:13 +0200538 *val = sign_extend32(value, 7);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100539
540 mutex_unlock(&data->mutex);
541
542 return IIO_VAL_INT;
543}
544
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000545static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
546 struct iio_chan_spec const *chan,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100547 int *val)
548{
549 int ret;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000550 int axis = chan->scan_index;
Markus Pargmann4011eda2015-09-21 12:55:13 +0200551 unsigned int raw_val;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100552
553 mutex_lock(&data->mutex);
554 ret = bmc150_accel_set_power_state(data, true);
555 if (ret < 0) {
556 mutex_unlock(&data->mutex);
557 return ret;
558 }
559
Markus Pargmann4011eda2015-09-21 12:55:13 +0200560 ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
561 &raw_val, 2);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100562 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200563 dev_err(data->dev, "Error reading axis %d\n", axis);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100564 bmc150_accel_set_power_state(data, false);
565 mutex_unlock(&data->mutex);
566 return ret;
567 }
Markus Pargmann4011eda2015-09-21 12:55:13 +0200568 *val = sign_extend32(raw_val >> chan->scan_type.shift,
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000569 chan->scan_type.realbits - 1);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100570 ret = bmc150_accel_set_power_state(data, false);
571 mutex_unlock(&data->mutex);
572 if (ret < 0)
573 return ret;
574
575 return IIO_VAL_INT;
576}
577
578static int bmc150_accel_read_raw(struct iio_dev *indio_dev,
579 struct iio_chan_spec const *chan,
580 int *val, int *val2, long mask)
581{
582 struct bmc150_accel_data *data = iio_priv(indio_dev);
583 int ret;
584
585 switch (mask) {
586 case IIO_CHAN_INFO_RAW:
587 switch (chan->type) {
588 case IIO_TEMP:
589 return bmc150_accel_get_temp(data, val);
590 case IIO_ACCEL:
591 if (iio_buffer_enabled(indio_dev))
592 return -EBUSY;
593 else
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000594 return bmc150_accel_get_axis(data, chan, val);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100595 default:
596 return -EINVAL;
597 }
598 case IIO_CHAN_INFO_OFFSET:
599 if (chan->type == IIO_TEMP) {
600 *val = BMC150_ACCEL_TEMP_CENTER_VAL;
601 return IIO_VAL_INT;
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200602 } else {
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100603 return -EINVAL;
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200604 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100605 case IIO_CHAN_INFO_SCALE:
606 *val = 0;
607 switch (chan->type) {
608 case IIO_TEMP:
609 *val2 = 500000;
610 return IIO_VAL_INT_PLUS_MICRO;
611 case IIO_ACCEL:
612 {
613 int i;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000614 const struct bmc150_scale_info *si;
615 int st_size = ARRAY_SIZE(data->chip_info->scale_table);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100616
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000617 for (i = 0; i < st_size; ++i) {
618 si = &data->chip_info->scale_table[i];
619 if (si->reg_range == data->range) {
620 *val2 = si->scale;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100621 return IIO_VAL_INT_PLUS_MICRO;
622 }
623 }
624 return -EINVAL;
625 }
626 default:
627 return -EINVAL;
628 }
629 case IIO_CHAN_INFO_SAMP_FREQ:
630 mutex_lock(&data->mutex);
631 ret = bmc150_accel_get_bw(data, val, val2);
632 mutex_unlock(&data->mutex);
633 return ret;
634 default:
635 return -EINVAL;
636 }
637}
638
639static int bmc150_accel_write_raw(struct iio_dev *indio_dev,
640 struct iio_chan_spec const *chan,
641 int val, int val2, long mask)
642{
643 struct bmc150_accel_data *data = iio_priv(indio_dev);
644 int ret;
645
646 switch (mask) {
647 case IIO_CHAN_INFO_SAMP_FREQ:
648 mutex_lock(&data->mutex);
649 ret = bmc150_accel_set_bw(data, val, val2);
650 mutex_unlock(&data->mutex);
651 break;
652 case IIO_CHAN_INFO_SCALE:
653 if (val)
654 return -EINVAL;
655
656 mutex_lock(&data->mutex);
657 ret = bmc150_accel_set_scale(data, val2);
658 mutex_unlock(&data->mutex);
659 return ret;
660 default:
661 ret = -EINVAL;
662 }
663
664 return ret;
665}
666
667static int bmc150_accel_read_event(struct iio_dev *indio_dev,
668 const struct iio_chan_spec *chan,
669 enum iio_event_type type,
670 enum iio_event_direction dir,
671 enum iio_event_info info,
672 int *val, int *val2)
673{
674 struct bmc150_accel_data *data = iio_priv(indio_dev);
675
676 *val2 = 0;
677 switch (info) {
678 case IIO_EV_INFO_VALUE:
679 *val = data->slope_thres;
680 break;
681 case IIO_EV_INFO_PERIOD:
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200682 *val = data->slope_dur;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100683 break;
684 default:
685 return -EINVAL;
686 }
687
688 return IIO_VAL_INT;
689}
690
691static int bmc150_accel_write_event(struct iio_dev *indio_dev,
692 const struct iio_chan_spec *chan,
693 enum iio_event_type type,
694 enum iio_event_direction dir,
695 enum iio_event_info info,
696 int val, int val2)
697{
698 struct bmc150_accel_data *data = iio_priv(indio_dev);
699
700 if (data->ev_enable_state)
701 return -EBUSY;
702
703 switch (info) {
704 case IIO_EV_INFO_VALUE:
Hartmut Knaackfdd15f62015-06-15 23:48:25 +0200705 data->slope_thres = val & BMC150_ACCEL_SLOPE_THRES_MASK;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100706 break;
707 case IIO_EV_INFO_PERIOD:
Octavian Purdila802a3ae2015-01-31 02:00:03 +0200708 data->slope_dur = val & BMC150_ACCEL_SLOPE_DUR_MASK;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100709 break;
710 default:
711 return -EINVAL;
712 }
713
714 return 0;
715}
716
717static int bmc150_accel_read_event_config(struct iio_dev *indio_dev,
718 const struct iio_chan_spec *chan,
719 enum iio_event_type type,
720 enum iio_event_direction dir)
721{
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100722 struct bmc150_accel_data *data = iio_priv(indio_dev);
723
724 return data->ev_enable_state;
725}
726
727static int bmc150_accel_write_event_config(struct iio_dev *indio_dev,
728 const struct iio_chan_spec *chan,
729 enum iio_event_type type,
730 enum iio_event_direction dir,
731 int state)
732{
733 struct bmc150_accel_data *data = iio_priv(indio_dev);
734 int ret;
735
Octavian Purdila14ee64f2015-01-31 02:00:05 +0200736 if (state == data->ev_enable_state)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100737 return 0;
738
739 mutex_lock(&data->mutex);
740
Octavian Purdila3e825ec2015-03-03 18:17:57 +0200741 ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_ANY_MOTION,
742 state);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100743 if (ret < 0) {
744 mutex_unlock(&data->mutex);
745 return ret;
746 }
747
748 data->ev_enable_state = state;
749 mutex_unlock(&data->mutex);
750
751 return 0;
752}
753
754static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev,
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200755 struct iio_trigger *trig)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100756{
757 struct bmc150_accel_data *data = iio_priv(indio_dev);
Octavian Purdila7d963212015-03-03 18:17:58 +0200758 int i;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100759
Octavian Purdila7d963212015-03-03 18:17:58 +0200760 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
761 if (data->triggers[i].indio_trig == trig)
762 return 0;
763 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100764
Octavian Purdila7d963212015-03-03 18:17:58 +0200765 return -EINVAL;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100766}
767
Octavian Purdila3bbec972015-03-22 20:33:40 +0200768static ssize_t bmc150_accel_get_fifo_watermark(struct device *dev,
769 struct device_attribute *attr,
770 char *buf)
771{
772 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
773 struct bmc150_accel_data *data = iio_priv(indio_dev);
774 int wm;
775
776 mutex_lock(&data->mutex);
777 wm = data->watermark;
778 mutex_unlock(&data->mutex);
779
780 return sprintf(buf, "%d\n", wm);
781}
782
783static ssize_t bmc150_accel_get_fifo_state(struct device *dev,
784 struct device_attribute *attr,
785 char *buf)
786{
787 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
788 struct bmc150_accel_data *data = iio_priv(indio_dev);
789 bool state;
790
791 mutex_lock(&data->mutex);
792 state = data->fifo_mode;
793 mutex_unlock(&data->mutex);
794
795 return sprintf(buf, "%d\n", state);
796}
797
798static IIO_CONST_ATTR(hwfifo_watermark_min, "1");
799static IIO_CONST_ATTR(hwfifo_watermark_max,
800 __stringify(BMC150_ACCEL_FIFO_LENGTH));
801static IIO_DEVICE_ATTR(hwfifo_enabled, S_IRUGO,
802 bmc150_accel_get_fifo_state, NULL, 0);
803static IIO_DEVICE_ATTR(hwfifo_watermark, S_IRUGO,
804 bmc150_accel_get_fifo_watermark, NULL, 0);
805
806static const struct attribute *bmc150_accel_fifo_attributes[] = {
807 &iio_const_attr_hwfifo_watermark_min.dev_attr.attr,
808 &iio_const_attr_hwfifo_watermark_max.dev_attr.attr,
809 &iio_dev_attr_hwfifo_watermark.dev_attr.attr,
810 &iio_dev_attr_hwfifo_enabled.dev_attr.attr,
811 NULL,
812};
813
814static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
815{
816 struct bmc150_accel_data *data = iio_priv(indio_dev);
817
818 if (val > BMC150_ACCEL_FIFO_LENGTH)
819 val = BMC150_ACCEL_FIFO_LENGTH;
820
821 mutex_lock(&data->mutex);
822 data->watermark = val;
823 mutex_unlock(&data->mutex);
824
825 return 0;
826}
827
828/*
829 * We must read at least one full frame in one burst, otherwise the rest of the
830 * frame data is discarded.
831 */
Markus Pargmann4011eda2015-09-21 12:55:13 +0200832static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
Octavian Purdila3bbec972015-03-22 20:33:40 +0200833 char *buffer, int samples)
834{
835 int sample_length = 3 * 2;
Markus Pargmann4011eda2015-09-21 12:55:13 +0200836 int ret;
837 int total_length = samples * sample_length;
838 int i;
839 size_t step = regmap_get_raw_read_max(data->regmap);
Octavian Purdila3bbec972015-03-22 20:33:40 +0200840
Markus Pargmann4011eda2015-09-21 12:55:13 +0200841 if (!step || step > total_length)
842 step = total_length;
843 else if (step < total_length)
844 step = sample_length;
Octavian Purdila3bbec972015-03-22 20:33:40 +0200845
Markus Pargmann4011eda2015-09-21 12:55:13 +0200846 /*
847 * Seems we have a bus with size limitation so we have to execute
848 * multiple reads
849 */
850 for (i = 0; i < total_length; i += step) {
851 ret = regmap_raw_read(data->regmap, BMC150_ACCEL_REG_FIFO_DATA,
852 &buffer[i], step);
853 if (ret)
854 break;
Octavian Purdila3bbec972015-03-22 20:33:40 +0200855 }
856
857 if (ret)
Markus Pargmann4011eda2015-09-21 12:55:13 +0200858 dev_err(data->dev, "Error transferring data from fifo in single steps of %zu\n",
859 step);
Octavian Purdila3bbec972015-03-22 20:33:40 +0200860
861 return ret;
862}
863
864static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
865 unsigned samples, bool irq)
866{
867 struct bmc150_accel_data *data = iio_priv(indio_dev);
868 int ret, i;
869 u8 count;
870 u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
871 int64_t tstamp;
872 uint64_t sample_period;
Markus Pargmann4011eda2015-09-21 12:55:13 +0200873 unsigned int val;
Hartmut Knaacke20008e2015-06-15 23:48:26 +0200874
Markus Pargmann4011eda2015-09-21 12:55:13 +0200875 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val);
Octavian Purdila3bbec972015-03-22 20:33:40 +0200876 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +0200877 dev_err(data->dev, "Error reading reg_fifo_status\n");
Octavian Purdila3bbec972015-03-22 20:33:40 +0200878 return ret;
879 }
880
Markus Pargmann4011eda2015-09-21 12:55:13 +0200881 count = val & 0x7F;
Octavian Purdila3bbec972015-03-22 20:33:40 +0200882
883 if (!count)
884 return 0;
885
886 /*
887 * If we getting called from IRQ handler we know the stored timestamp is
888 * fairly accurate for the last stored sample. Otherwise, if we are
889 * called as a result of a read operation from userspace and hence
890 * before the watermark interrupt was triggered, take a timestamp
891 * now. We can fall anywhere in between two samples so the error in this
892 * case is at most one sample period.
893 */
894 if (!irq) {
895 data->old_timestamp = data->timestamp;
896 data->timestamp = iio_get_time_ns();
897 }
898
899 /*
900 * Approximate timestamps for each of the sample based on the sampling
901 * frequency, timestamp for last sample and number of samples.
902 *
903 * Note that we can't use the current bandwidth settings to compute the
904 * sample period because the sample rate varies with the device
905 * (e.g. between 31.70ms to 32.20ms for a bandwidth of 15.63HZ). That
906 * small variation adds when we store a large number of samples and
907 * creates significant jitter between the last and first samples in
908 * different batches (e.g. 32ms vs 21ms).
909 *
910 * To avoid this issue we compute the actual sample period ourselves
911 * based on the timestamp delta between the last two flush operations.
912 */
913 sample_period = (data->timestamp - data->old_timestamp);
914 do_div(sample_period, count);
915 tstamp = data->timestamp - (count - 1) * sample_period;
916
917 if (samples && count > samples)
918 count = samples;
919
Markus Pargmann4011eda2015-09-21 12:55:13 +0200920 ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count);
Octavian Purdila3bbec972015-03-22 20:33:40 +0200921 if (ret)
922 return ret;
923
924 /*
925 * Ideally we want the IIO core to handle the demux when running in fifo
926 * mode but not when running in triggered buffer mode. Unfortunately
927 * this does not seem to be possible, so stick with driver demux for
928 * now.
929 */
930 for (i = 0; i < count; i++) {
931 u16 sample[8];
932 int j, bit;
933
934 j = 0;
935 for_each_set_bit(bit, indio_dev->active_scan_mask,
936 indio_dev->masklength)
937 memcpy(&sample[j++], &buffer[i * 3 + bit], 2);
938
939 iio_push_to_buffers_with_timestamp(indio_dev, sample, tstamp);
940
941 tstamp += sample_period;
942 }
943
944 return count;
945}
946
947static int bmc150_accel_fifo_flush(struct iio_dev *indio_dev, unsigned samples)
948{
949 struct bmc150_accel_data *data = iio_priv(indio_dev);
950 int ret;
951
952 mutex_lock(&data->mutex);
953 ret = __bmc150_accel_fifo_flush(indio_dev, samples, false);
954 mutex_unlock(&data->mutex);
955
956 return ret;
957}
958
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100959static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
Sathyanarayanan Kuppuswamy0ba8da92015-03-03 18:17:56 +0200960 "15.620000 31.260000 62.50000 125 250 500 1000 2000");
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100961
962static struct attribute *bmc150_accel_attributes[] = {
963 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
964 NULL,
965};
966
967static const struct attribute_group bmc150_accel_attrs_group = {
968 .attrs = bmc150_accel_attributes,
969};
970
971static const struct iio_event_spec bmc150_accel_event = {
972 .type = IIO_EV_TYPE_ROC,
Srinivas Pandruvada11741242014-10-10 20:35:34 -0700973 .dir = IIO_EV_DIR_EITHER,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100974 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
975 BIT(IIO_EV_INFO_ENABLE) |
976 BIT(IIO_EV_INFO_PERIOD)
977};
978
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000979#define BMC150_ACCEL_CHANNEL(_axis, bits) { \
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100980 .type = IIO_ACCEL, \
981 .modified = 1, \
982 .channel2 = IIO_MOD_##_axis, \
983 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
984 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
985 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
986 .scan_index = AXIS_##_axis, \
987 .scan_type = { \
988 .sign = 's', \
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000989 .realbits = (bits), \
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100990 .storagebits = 16, \
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000991 .shift = 16 - (bits), \
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +0100992 }, \
993 .event_spec = &bmc150_accel_event, \
994 .num_event_specs = 1 \
995}
996
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +0000997#define BMC150_ACCEL_CHANNELS(bits) { \
998 { \
999 .type = IIO_TEMP, \
1000 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1001 BIT(IIO_CHAN_INFO_SCALE) | \
1002 BIT(IIO_CHAN_INFO_OFFSET), \
1003 .scan_index = -1, \
1004 }, \
1005 BMC150_ACCEL_CHANNEL(X, bits), \
1006 BMC150_ACCEL_CHANNEL(Y, bits), \
1007 BMC150_ACCEL_CHANNEL(Z, bits), \
1008 IIO_CHAN_SOFT_TIMESTAMP(3), \
1009}
1010
1011static const struct iio_chan_spec bma222e_accel_channels[] =
1012 BMC150_ACCEL_CHANNELS(8);
1013static const struct iio_chan_spec bma250e_accel_channels[] =
1014 BMC150_ACCEL_CHANNELS(10);
1015static const struct iio_chan_spec bmc150_accel_channels[] =
1016 BMC150_ACCEL_CHANNELS(12);
1017static const struct iio_chan_spec bma280_accel_channels[] =
1018 BMC150_ACCEL_CHANNELS(14);
1019
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001020static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl[] = {
1021 [bmc150] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001022 .name = "BMC150A",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001023 .chip_id = 0xFA,
1024 .channels = bmc150_accel_channels,
1025 .num_channels = ARRAY_SIZE(bmc150_accel_channels),
1026 .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
1027 {19122, BMC150_ACCEL_DEF_RANGE_4G},
1028 {38344, BMC150_ACCEL_DEF_RANGE_8G},
1029 {76590, BMC150_ACCEL_DEF_RANGE_16G} },
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001030 },
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001031 [bmi055] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001032 .name = "BMI055A",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001033 .chip_id = 0xFA,
1034 .channels = bmc150_accel_channels,
1035 .num_channels = ARRAY_SIZE(bmc150_accel_channels),
1036 .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
1037 {19122, BMC150_ACCEL_DEF_RANGE_4G},
1038 {38344, BMC150_ACCEL_DEF_RANGE_8G},
1039 {76590, BMC150_ACCEL_DEF_RANGE_16G} },
1040 },
1041 [bma255] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001042 .name = "BMA0255",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001043 .chip_id = 0xFA,
1044 .channels = bmc150_accel_channels,
1045 .num_channels = ARRAY_SIZE(bmc150_accel_channels),
1046 .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
1047 {19122, BMC150_ACCEL_DEF_RANGE_4G},
1048 {38344, BMC150_ACCEL_DEF_RANGE_8G},
1049 {76590, BMC150_ACCEL_DEF_RANGE_16G} },
1050 },
1051 [bma250e] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001052 .name = "BMA250E",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001053 .chip_id = 0xF9,
1054 .channels = bma250e_accel_channels,
1055 .num_channels = ARRAY_SIZE(bma250e_accel_channels),
1056 .scale_table = { {38344, BMC150_ACCEL_DEF_RANGE_2G},
1057 {76590, BMC150_ACCEL_DEF_RANGE_4G},
1058 {153277, BMC150_ACCEL_DEF_RANGE_8G},
1059 {306457, BMC150_ACCEL_DEF_RANGE_16G} },
1060 },
1061 [bma222e] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001062 .name = "BMA222E",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001063 .chip_id = 0xF8,
1064 .channels = bma222e_accel_channels,
1065 .num_channels = ARRAY_SIZE(bma222e_accel_channels),
1066 .scale_table = { {153277, BMC150_ACCEL_DEF_RANGE_2G},
1067 {306457, BMC150_ACCEL_DEF_RANGE_4G},
1068 {612915, BMC150_ACCEL_DEF_RANGE_8G},
1069 {1225831, BMC150_ACCEL_DEF_RANGE_16G} },
1070 },
1071 [bma280] = {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001072 .name = "BMA0280",
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001073 .chip_id = 0xFB,
1074 .channels = bma280_accel_channels,
1075 .num_channels = ARRAY_SIZE(bma280_accel_channels),
1076 .scale_table = { {2392, BMC150_ACCEL_DEF_RANGE_2G},
1077 {4785, BMC150_ACCEL_DEF_RANGE_4G},
1078 {9581, BMC150_ACCEL_DEF_RANGE_8G},
1079 {19152, BMC150_ACCEL_DEF_RANGE_16G} },
1080 },
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001081};
1082
1083static const struct iio_info bmc150_accel_info = {
1084 .attrs = &bmc150_accel_attrs_group,
1085 .read_raw = bmc150_accel_read_raw,
1086 .write_raw = bmc150_accel_write_raw,
1087 .read_event_value = bmc150_accel_read_event,
1088 .write_event_value = bmc150_accel_write_event,
1089 .write_event_config = bmc150_accel_write_event_config,
1090 .read_event_config = bmc150_accel_read_event_config,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001091 .driver_module = THIS_MODULE,
1092};
1093
Octavian Purdila3bbec972015-03-22 20:33:40 +02001094static const struct iio_info bmc150_accel_info_fifo = {
1095 .attrs = &bmc150_accel_attrs_group,
1096 .read_raw = bmc150_accel_read_raw,
1097 .write_raw = bmc150_accel_write_raw,
1098 .read_event_value = bmc150_accel_read_event,
1099 .write_event_value = bmc150_accel_write_event,
1100 .write_event_config = bmc150_accel_write_event_config,
1101 .read_event_config = bmc150_accel_read_event_config,
1102 .validate_trigger = bmc150_accel_validate_trigger,
1103 .hwfifo_set_watermark = bmc150_accel_set_watermark,
1104 .hwfifo_flush_to_buffer = bmc150_accel_fifo_flush,
1105 .driver_module = THIS_MODULE,
1106};
1107
Irina Tirdea23e758b2016-03-24 11:29:26 +02001108static const unsigned long bmc150_accel_scan_masks[] = {
1109 BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
1110 0};
1111
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001112static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
1113{
1114 struct iio_poll_func *pf = p;
1115 struct iio_dev *indio_dev = pf->indio_dev;
1116 struct bmc150_accel_data *data = iio_priv(indio_dev);
1117 int bit, ret, i = 0;
Markus Pargmann4011eda2015-09-21 12:55:13 +02001118 unsigned int raw_val;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001119
1120 mutex_lock(&data->mutex);
Irina Tirdea23e758b2016-03-24 11:29:26 +02001121 for (bit = 0; bit < AXIS_MAX; bit++) {
Markus Pargmann4011eda2015-09-21 12:55:13 +02001122 ret = regmap_bulk_read(data->regmap,
1123 BMC150_ACCEL_AXIS_TO_REG(bit), &raw_val,
1124 2);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001125 if (ret < 0) {
1126 mutex_unlock(&data->mutex);
1127 goto err_read;
1128 }
Markus Pargmann4011eda2015-09-21 12:55:13 +02001129 data->buffer[i++] = raw_val;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001130 }
1131 mutex_unlock(&data->mutex);
1132
1133 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001134 pf->timestamp);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001135err_read:
1136 iio_trigger_notify_done(indio_dev->trig);
1137
1138 return IRQ_HANDLED;
1139}
1140
1141static int bmc150_accel_trig_try_reen(struct iio_trigger *trig)
1142{
Octavian Purdila7d963212015-03-03 18:17:58 +02001143 struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1144 struct bmc150_accel_data *data = t->data;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001145 int ret;
1146
1147 /* new data interrupts don't need ack */
Octavian Purdila7d963212015-03-03 18:17:58 +02001148 if (t == &t->data->triggers[BMC150_ACCEL_TRIGGER_DATA_READY])
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001149 return 0;
1150
1151 mutex_lock(&data->mutex);
1152 /* clear any latched interrupt */
Markus Pargmann4011eda2015-09-21 12:55:13 +02001153 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1154 BMC150_ACCEL_INT_MODE_LATCH_INT |
1155 BMC150_ACCEL_INT_MODE_LATCH_RESET);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001156 mutex_unlock(&data->mutex);
1157 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001158 dev_err(data->dev,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001159 "Error writing reg_int_rst_latch\n");
1160 return ret;
1161 }
1162
1163 return 0;
1164}
1165
Octavian Purdila7d963212015-03-03 18:17:58 +02001166static int bmc150_accel_trigger_set_state(struct iio_trigger *trig,
Hartmut Knaacke20008e2015-06-15 23:48:26 +02001167 bool state)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001168{
Octavian Purdila7d963212015-03-03 18:17:58 +02001169 struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1170 struct bmc150_accel_data *data = t->data;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001171 int ret;
1172
1173 mutex_lock(&data->mutex);
1174
Octavian Purdila7d963212015-03-03 18:17:58 +02001175 if (t->enabled == state) {
1176 mutex_unlock(&data->mutex);
1177 return 0;
1178 }
1179
1180 if (t->setup) {
1181 ret = t->setup(t, state);
1182 if (ret < 0) {
Octavian Purdila14ee64f2015-01-31 02:00:05 +02001183 mutex_unlock(&data->mutex);
Octavian Purdila7d963212015-03-03 18:17:58 +02001184 return ret;
Octavian Purdila14ee64f2015-01-31 02:00:05 +02001185 }
1186 }
1187
Octavian Purdila7d963212015-03-03 18:17:58 +02001188 ret = bmc150_accel_set_interrupt(data, t->intr, state);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001189 if (ret < 0) {
1190 mutex_unlock(&data->mutex);
1191 return ret;
1192 }
Octavian Purdila7d963212015-03-03 18:17:58 +02001193
1194 t->enabled = state;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001195
1196 mutex_unlock(&data->mutex);
1197
1198 return ret;
1199}
1200
1201static const struct iio_trigger_ops bmc150_accel_trigger_ops = {
Octavian Purdila7d963212015-03-03 18:17:58 +02001202 .set_trigger_state = bmc150_accel_trigger_set_state,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001203 .try_reenable = bmc150_accel_trig_try_reen,
1204 .owner = THIS_MODULE,
1205};
1206
Octavian Purdila3bbec972015-03-22 20:33:40 +02001207static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001208{
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001209 struct bmc150_accel_data *data = iio_priv(indio_dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001210 int dir;
Octavian Purdila3bbec972015-03-22 20:33:40 +02001211 int ret;
Markus Pargmann4011eda2015-09-21 12:55:13 +02001212 unsigned int val;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001213
Markus Pargmann4011eda2015-09-21 12:55:13 +02001214 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001215 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001216 dev_err(data->dev, "Error reading reg_int_status_2\n");
Octavian Purdila3bbec972015-03-22 20:33:40 +02001217 return ret;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001218 }
1219
Markus Pargmann4011eda2015-09-21 12:55:13 +02001220 if (val & BMC150_ACCEL_ANY_MOTION_BIT_SIGN)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001221 dir = IIO_EV_DIR_FALLING;
1222 else
1223 dir = IIO_EV_DIR_RISING;
1224
Markus Pargmann4011eda2015-09-21 12:55:13 +02001225 if (val & BMC150_ACCEL_ANY_MOTION_BIT_X)
Hartmut Knaacke20008e2015-06-15 23:48:26 +02001226 iio_push_event(indio_dev,
1227 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1228 0,
1229 IIO_MOD_X,
1230 IIO_EV_TYPE_ROC,
1231 dir),
1232 data->timestamp);
1233
Markus Pargmann4011eda2015-09-21 12:55:13 +02001234 if (val & BMC150_ACCEL_ANY_MOTION_BIT_Y)
Hartmut Knaacke20008e2015-06-15 23:48:26 +02001235 iio_push_event(indio_dev,
1236 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1237 0,
1238 IIO_MOD_Y,
1239 IIO_EV_TYPE_ROC,
1240 dir),
1241 data->timestamp);
1242
Markus Pargmann4011eda2015-09-21 12:55:13 +02001243 if (val & BMC150_ACCEL_ANY_MOTION_BIT_Z)
Hartmut Knaacke20008e2015-06-15 23:48:26 +02001244 iio_push_event(indio_dev,
1245 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1246 0,
1247 IIO_MOD_Z,
1248 IIO_EV_TYPE_ROC,
1249 dir),
1250 data->timestamp);
1251
Octavian Purdila3bbec972015-03-22 20:33:40 +02001252 return ret;
1253}
1254
1255static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private)
1256{
1257 struct iio_dev *indio_dev = private;
1258 struct bmc150_accel_data *data = iio_priv(indio_dev);
1259 bool ack = false;
1260 int ret;
1261
1262 mutex_lock(&data->mutex);
1263
1264 if (data->fifo_mode) {
1265 ret = __bmc150_accel_fifo_flush(indio_dev,
1266 BMC150_ACCEL_FIFO_LENGTH, true);
1267 if (ret > 0)
1268 ack = true;
1269 }
1270
1271 if (data->ev_enable_state) {
1272 ret = bmc150_accel_handle_roc_event(indio_dev);
1273 if (ret > 0)
1274 ack = true;
1275 }
1276
1277 if (ack) {
Markus Pargmann4011eda2015-09-21 12:55:13 +02001278 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1279 BMC150_ACCEL_INT_MODE_LATCH_INT |
1280 BMC150_ACCEL_INT_MODE_LATCH_RESET);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001281 if (ret)
Markus Pargmann19c95d62015-09-21 12:55:14 +02001282 dev_err(data->dev, "Error writing reg_int_rst_latch\n");
Hartmut Knaacke20008e2015-06-15 23:48:26 +02001283
Octavian Purdila3bbec972015-03-22 20:33:40 +02001284 ret = IRQ_HANDLED;
1285 } else {
1286 ret = IRQ_NONE;
1287 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001288
Octavian Purdila3bbec972015-03-22 20:33:40 +02001289 mutex_unlock(&data->mutex);
1290
1291 return ret;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001292}
1293
Octavian Purdila3bbec972015-03-22 20:33:40 +02001294static irqreturn_t bmc150_accel_irq_handler(int irq, void *private)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001295{
1296 struct iio_dev *indio_dev = private;
1297 struct bmc150_accel_data *data = iio_priv(indio_dev);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001298 bool ack = false;
Octavian Purdila7d963212015-03-03 18:17:58 +02001299 int i;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001300
Octavian Purdila3bbec972015-03-22 20:33:40 +02001301 data->old_timestamp = data->timestamp;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001302 data->timestamp = iio_get_time_ns();
1303
Octavian Purdila7d963212015-03-03 18:17:58 +02001304 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1305 if (data->triggers[i].enabled) {
1306 iio_trigger_poll(data->triggers[i].indio_trig);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001307 ack = true;
Octavian Purdila7d963212015-03-03 18:17:58 +02001308 break;
1309 }
1310 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001311
Octavian Purdila3bbec972015-03-22 20:33:40 +02001312 if (data->ev_enable_state || data->fifo_mode)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001313 return IRQ_WAKE_THREAD;
Octavian Purdila3bbec972015-03-22 20:33:40 +02001314
1315 if (ack)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001316 return IRQ_HANDLED;
Octavian Purdila3bbec972015-03-22 20:33:40 +02001317
1318 return IRQ_NONE;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001319}
1320
Octavian Purdila7d963212015-03-03 18:17:58 +02001321static const struct {
1322 int intr;
1323 const char *name;
1324 int (*setup)(struct bmc150_accel_trigger *t, bool state);
1325} bmc150_accel_triggers[BMC150_ACCEL_TRIGGERS] = {
1326 {
1327 .intr = 0,
1328 .name = "%s-dev%d",
1329 },
1330 {
1331 .intr = 1,
1332 .name = "%s-any-motion-dev%d",
1333 .setup = bmc150_accel_any_motion_setup,
1334 },
1335};
1336
1337static void bmc150_accel_unregister_triggers(struct bmc150_accel_data *data,
1338 int from)
1339{
1340 int i;
1341
Hartmut Knaack7a1d0d92015-06-15 23:48:24 +02001342 for (i = from; i >= 0; i--) {
Octavian Purdila7d963212015-03-03 18:17:58 +02001343 if (data->triggers[i].indio_trig) {
1344 iio_trigger_unregister(data->triggers[i].indio_trig);
1345 data->triggers[i].indio_trig = NULL;
1346 }
1347 }
1348}
1349
1350static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev,
1351 struct bmc150_accel_data *data)
1352{
1353 int i, ret;
1354
1355 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1356 struct bmc150_accel_trigger *t = &data->triggers[i];
1357
Markus Pargmann19c95d62015-09-21 12:55:14 +02001358 t->indio_trig = devm_iio_trigger_alloc(data->dev,
Octavian Purdila7d963212015-03-03 18:17:58 +02001359 bmc150_accel_triggers[i].name,
1360 indio_dev->name,
1361 indio_dev->id);
1362 if (!t->indio_trig) {
1363 ret = -ENOMEM;
1364 break;
1365 }
1366
Markus Pargmann19c95d62015-09-21 12:55:14 +02001367 t->indio_trig->dev.parent = data->dev;
Octavian Purdila7d963212015-03-03 18:17:58 +02001368 t->indio_trig->ops = &bmc150_accel_trigger_ops;
1369 t->intr = bmc150_accel_triggers[i].intr;
1370 t->data = data;
1371 t->setup = bmc150_accel_triggers[i].setup;
1372 iio_trigger_set_drvdata(t->indio_trig, t);
1373
1374 ret = iio_trigger_register(t->indio_trig);
1375 if (ret)
1376 break;
1377 }
1378
1379 if (ret)
1380 bmc150_accel_unregister_triggers(data, i - 1);
1381
1382 return ret;
1383}
1384
Octavian Purdila3bbec972015-03-22 20:33:40 +02001385#define BMC150_ACCEL_FIFO_MODE_STREAM 0x80
1386#define BMC150_ACCEL_FIFO_MODE_FIFO 0x40
1387#define BMC150_ACCEL_FIFO_MODE_BYPASS 0x00
1388
1389static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data)
1390{
1391 u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1;
1392 int ret;
1393
Markus Pargmann4011eda2015-09-21 12:55:13 +02001394 ret = regmap_write(data->regmap, reg, data->fifo_mode);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001395 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001396 dev_err(data->dev, "Error writing reg_fifo_config1\n");
Octavian Purdila3bbec972015-03-22 20:33:40 +02001397 return ret;
1398 }
1399
1400 if (!data->fifo_mode)
1401 return 0;
1402
Markus Pargmann4011eda2015-09-21 12:55:13 +02001403 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0,
1404 data->watermark);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001405 if (ret < 0)
Markus Pargmann19c95d62015-09-21 12:55:14 +02001406 dev_err(data->dev, "Error writing reg_fifo_config0\n");
Octavian Purdila3bbec972015-03-22 20:33:40 +02001407
1408 return ret;
1409}
1410
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001411static int bmc150_accel_buffer_preenable(struct iio_dev *indio_dev)
1412{
1413 struct bmc150_accel_data *data = iio_priv(indio_dev);
1414
1415 return bmc150_accel_set_power_state(data, true);
1416}
1417
Octavian Purdila3bbec972015-03-22 20:33:40 +02001418static int bmc150_accel_buffer_postenable(struct iio_dev *indio_dev)
1419{
1420 struct bmc150_accel_data *data = iio_priv(indio_dev);
1421 int ret = 0;
1422
1423 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
1424 return iio_triggered_buffer_postenable(indio_dev);
1425
1426 mutex_lock(&data->mutex);
1427
1428 if (!data->watermark)
1429 goto out;
1430
1431 ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
1432 true);
1433 if (ret)
1434 goto out;
1435
1436 data->fifo_mode = BMC150_ACCEL_FIFO_MODE_FIFO;
1437
1438 ret = bmc150_accel_fifo_set_mode(data);
1439 if (ret) {
1440 data->fifo_mode = 0;
1441 bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
1442 false);
1443 }
1444
1445out:
1446 mutex_unlock(&data->mutex);
1447
1448 return ret;
1449}
1450
1451static int bmc150_accel_buffer_predisable(struct iio_dev *indio_dev)
1452{
1453 struct bmc150_accel_data *data = iio_priv(indio_dev);
1454
1455 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
1456 return iio_triggered_buffer_predisable(indio_dev);
1457
1458 mutex_lock(&data->mutex);
1459
1460 if (!data->fifo_mode)
1461 goto out;
1462
1463 bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK, false);
1464 __bmc150_accel_fifo_flush(indio_dev, BMC150_ACCEL_FIFO_LENGTH, false);
1465 data->fifo_mode = 0;
1466 bmc150_accel_fifo_set_mode(data);
1467
1468out:
1469 mutex_unlock(&data->mutex);
1470
1471 return 0;
1472}
1473
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001474static int bmc150_accel_buffer_postdisable(struct iio_dev *indio_dev)
1475{
1476 struct bmc150_accel_data *data = iio_priv(indio_dev);
1477
1478 return bmc150_accel_set_power_state(data, false);
1479}
1480
Octavian Purdila3bbec972015-03-22 20:33:40 +02001481static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops = {
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001482 .preenable = bmc150_accel_buffer_preenable,
Octavian Purdila3bbec972015-03-22 20:33:40 +02001483 .postenable = bmc150_accel_buffer_postenable,
1484 .predisable = bmc150_accel_buffer_predisable,
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001485 .postdisable = bmc150_accel_buffer_postdisable,
Octavian Purdila3bbec972015-03-22 20:33:40 +02001486};
1487
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001488static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
1489{
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001490 int ret, i;
Markus Pargmann4011eda2015-09-21 12:55:13 +02001491 unsigned int val;
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001492
Markus Pargmann4011eda2015-09-21 12:55:13 +02001493 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val);
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001494 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001495 dev_err(data->dev,
Markus Pargmann4011eda2015-09-21 12:55:13 +02001496 "Error: Reading chip id\n");
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001497 return ret;
1498 }
1499
Markus Pargmann19c95d62015-09-21 12:55:14 +02001500 dev_dbg(data->dev, "Chip Id %x\n", val);
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001501 for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) {
Markus Pargmann4011eda2015-09-21 12:55:13 +02001502 if (bmc150_accel_chip_info_tbl[i].chip_id == val) {
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001503 data->chip_info = &bmc150_accel_chip_info_tbl[i];
1504 break;
1505 }
1506 }
1507
1508 if (!data->chip_info) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001509 dev_err(data->dev, "Invalid chip %x\n", val);
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001510 return -ENODEV;
1511 }
1512
1513 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1514 if (ret < 0)
1515 return ret;
1516
1517 /* Set Bandwidth */
1518 ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0);
1519 if (ret < 0)
1520 return ret;
1521
1522 /* Set Default Range */
Markus Pargmann4011eda2015-09-21 12:55:13 +02001523 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE,
1524 BMC150_ACCEL_DEF_RANGE_4G);
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001525 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001526 dev_err(data->dev,
Markus Pargmann4011eda2015-09-21 12:55:13 +02001527 "Error writing reg_pmu_range\n");
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001528 return ret;
1529 }
1530
1531 data->range = BMC150_ACCEL_DEF_RANGE_4G;
1532
1533 /* Set default slope duration and thresholds */
1534 data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD;
1535 data->slope_dur = BMC150_ACCEL_DEF_SLOPE_DURATION;
1536 ret = bmc150_accel_update_slope(data);
1537 if (ret < 0)
1538 return ret;
1539
1540 /* Set default as latched interrupts */
Markus Pargmann4011eda2015-09-21 12:55:13 +02001541 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1542 BMC150_ACCEL_INT_MODE_LATCH_INT |
1543 BMC150_ACCEL_INT_MODE_LATCH_RESET);
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001544 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001545 dev_err(data->dev,
Bastien Nocerac4eaab72015-07-23 17:20:59 +02001546 "Error writing reg_int_rst_latch\n");
1547 return ret;
1548 }
1549
1550 return 0;
1551}
1552
Markus Pargmann55637c32015-09-21 12:55:15 +02001553int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
1554 const char *name, bool block_supported)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001555{
1556 struct bmc150_accel_data *data;
1557 struct iio_dev *indio_dev;
1558 int ret;
1559
Markus Pargmann55637c32015-09-21 12:55:15 +02001560 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001561 if (!indio_dev)
1562 return -ENOMEM;
1563
1564 data = iio_priv(indio_dev);
Markus Pargmann55637c32015-09-21 12:55:15 +02001565 dev_set_drvdata(dev, indio_dev);
1566 data->dev = dev;
1567 data->irq = irq;
Markus Pargmann4011eda2015-09-21 12:55:13 +02001568
Markus Pargmann55637c32015-09-21 12:55:15 +02001569 data->regmap = regmap;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001570
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001571 ret = bmc150_accel_chip_init(data);
1572 if (ret < 0)
1573 return ret;
1574
1575 mutex_init(&data->mutex);
1576
Markus Pargmann19c95d62015-09-21 12:55:14 +02001577 indio_dev->dev.parent = dev;
Laurentiu Palcu8ecbb3c2014-02-09 10:30:00 +00001578 indio_dev->channels = data->chip_info->channels;
1579 indio_dev->num_channels = data->chip_info->num_channels;
Bastien Nocera0ad4bf32015-07-23 17:21:07 +02001580 indio_dev->name = name ? name : data->chip_info->name;
Irina Tirdea23e758b2016-03-24 11:29:26 +02001581 indio_dev->available_scan_masks = bmc150_accel_scan_masks;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001582 indio_dev->modes = INDIO_DIRECT_MODE;
1583 indio_dev->info = &bmc150_accel_info;
1584
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001585 ret = iio_triggered_buffer_setup(indio_dev,
1586 &iio_pollfunc_store_time,
1587 bmc150_accel_trigger_handler,
1588 &bmc150_accel_buffer_ops);
1589 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001590 dev_err(data->dev, "Failed: iio triggered buffer setup\n");
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001591 return ret;
1592 }
1593
Markus Pargmann19c95d62015-09-21 12:55:14 +02001594 if (data->irq > 0) {
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001595 ret = devm_request_threaded_irq(
Markus Pargmann19c95d62015-09-21 12:55:14 +02001596 data->dev, data->irq,
Octavian Purdila3bbec972015-03-22 20:33:40 +02001597 bmc150_accel_irq_handler,
1598 bmc150_accel_irq_thread_handler,
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001599 IRQF_TRIGGER_RISING,
1600 BMC150_ACCEL_IRQ_NAME,
1601 indio_dev);
1602 if (ret)
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001603 goto err_buffer_cleanup;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001604
Octavian Purdila8e22f472015-01-31 02:00:04 +02001605 /*
1606 * Set latched mode interrupt. While certain interrupts are
1607 * non-latched regardless of this settings (e.g. new data) we
1608 * want to use latch mode when we can to prevent interrupt
1609 * flooding.
1610 */
Markus Pargmann4011eda2015-09-21 12:55:13 +02001611 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1612 BMC150_ACCEL_INT_MODE_LATCH_RESET);
Octavian Purdila8e22f472015-01-31 02:00:04 +02001613 if (ret < 0) {
Markus Pargmann19c95d62015-09-21 12:55:14 +02001614 dev_err(data->dev, "Error writing reg_int_rst_latch\n");
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001615 goto err_buffer_cleanup;
Octavian Purdila8e22f472015-01-31 02:00:04 +02001616 }
1617
Octavian Purdila3e825ec2015-03-03 18:17:57 +02001618 bmc150_accel_interrupts_setup(indio_dev, data);
1619
Octavian Purdila7d963212015-03-03 18:17:58 +02001620 ret = bmc150_accel_triggers_setup(indio_dev, data);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001621 if (ret)
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001622 goto err_buffer_cleanup;
Octavian Purdila3bbec972015-03-22 20:33:40 +02001623
Markus Pargmann55637c32015-09-21 12:55:15 +02001624 if (block_supported) {
Octavian Purdila3bbec972015-03-22 20:33:40 +02001625 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
1626 indio_dev->info = &bmc150_accel_info_fifo;
1627 indio_dev->buffer->attrs = bmc150_accel_fifo_attributes;
1628 }
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001629 }
1630
Adriana Reus7d0ead52015-11-05 16:25:29 +02001631 ret = pm_runtime_set_active(dev);
1632 if (ret)
1633 goto err_trigger_unregister;
1634
1635 pm_runtime_enable(dev);
1636 pm_runtime_set_autosuspend_delay(dev, BMC150_AUTO_SUSPEND_DELAY_MS);
1637 pm_runtime_use_autosuspend(dev);
1638
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001639 ret = iio_device_register(indio_dev);
1640 if (ret < 0) {
Markus Pargmann55637c32015-09-21 12:55:15 +02001641 dev_err(dev, "Unable to register iio device\n");
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001642 goto err_trigger_unregister;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001643 }
1644
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001645 return 0;
1646
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001647err_trigger_unregister:
Octavian Purdila7d963212015-03-03 18:17:58 +02001648 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001649err_buffer_cleanup:
1650 iio_triggered_buffer_cleanup(indio_dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001651
1652 return ret;
1653}
Markus Pargmann55637c32015-09-21 12:55:15 +02001654EXPORT_SYMBOL_GPL(bmc150_accel_core_probe);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001655
Markus Pargmann55637c32015-09-21 12:55:15 +02001656int bmc150_accel_core_remove(struct device *dev)
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001657{
Markus Pargmann55637c32015-09-21 12:55:15 +02001658 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001659 struct bmc150_accel_data *data = iio_priv(indio_dev);
1660
Adriana Reus7d0ead52015-11-05 16:25:29 +02001661 iio_device_unregister(indio_dev);
1662
Markus Pargmann19c95d62015-09-21 12:55:14 +02001663 pm_runtime_disable(data->dev);
1664 pm_runtime_set_suspended(data->dev);
1665 pm_runtime_put_noidle(data->dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001666
Octavian Purdila7d963212015-03-03 18:17:58 +02001667 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001668
Vlad Dogaruc16bff42015-05-12 17:03:24 +03001669 iio_triggered_buffer_cleanup(indio_dev);
1670
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001671 mutex_lock(&data->mutex);
1672 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0);
1673 mutex_unlock(&data->mutex);
1674
1675 return 0;
1676}
Markus Pargmann55637c32015-09-21 12:55:15 +02001677EXPORT_SYMBOL_GPL(bmc150_accel_core_remove);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001678
1679#ifdef CONFIG_PM_SLEEP
1680static int bmc150_accel_suspend(struct device *dev)
1681{
Markus Pargmann19c95d62015-09-21 12:55:14 +02001682 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001683 struct bmc150_accel_data *data = iio_priv(indio_dev);
1684
1685 mutex_lock(&data->mutex);
1686 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1687 mutex_unlock(&data->mutex);
1688
1689 return 0;
1690}
1691
1692static int bmc150_accel_resume(struct device *dev)
1693{
Markus Pargmann19c95d62015-09-21 12:55:14 +02001694 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001695 struct bmc150_accel_data *data = iio_priv(indio_dev);
1696
1697 mutex_lock(&data->mutex);
Octavian Purdila3e825ec2015-03-03 18:17:57 +02001698 if (atomic_read(&data->active_intr))
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001699 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
Octavian Purdila3bbec972015-03-22 20:33:40 +02001700 bmc150_accel_fifo_set_mode(data);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001701 mutex_unlock(&data->mutex);
1702
1703 return 0;
1704}
1705#endif
1706
Rafael J. Wysocki6f0a13f2014-12-04 01:08:13 +01001707#ifdef CONFIG_PM
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001708static int bmc150_accel_runtime_suspend(struct device *dev)
1709{
Markus Pargmann19c95d62015-09-21 12:55:14 +02001710 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001711 struct bmc150_accel_data *data = iio_priv(indio_dev);
Srinivas Pandruvadaaaeecd82014-10-10 20:35:31 -07001712 int ret;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001713
Markus Pargmann19c95d62015-09-21 12:55:14 +02001714 dev_dbg(data->dev, __func__);
Srinivas Pandruvadaaaeecd82014-10-10 20:35:31 -07001715 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1716 if (ret < 0)
1717 return -EAGAIN;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001718
Srinivas Pandruvadaaaeecd82014-10-10 20:35:31 -07001719 return 0;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001720}
1721
1722static int bmc150_accel_runtime_resume(struct device *dev)
1723{
Markus Pargmann19c95d62015-09-21 12:55:14 +02001724 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001725 struct bmc150_accel_data *data = iio_priv(indio_dev);
1726 int ret;
1727 int sleep_val;
1728
Markus Pargmann19c95d62015-09-21 12:55:14 +02001729 dev_dbg(data->dev, __func__);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001730
1731 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1732 if (ret < 0)
1733 return ret;
Octavian Purdila3bbec972015-03-22 20:33:40 +02001734 ret = bmc150_accel_fifo_set_mode(data);
1735 if (ret < 0)
1736 return ret;
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001737
1738 sleep_val = bmc150_accel_get_startup_times(data);
1739 if (sleep_val < 20)
1740 usleep_range(sleep_val * 1000, 20000);
1741 else
1742 msleep_interruptible(sleep_val);
1743
1744 return 0;
1745}
1746#endif
1747
Markus Pargmann55637c32015-09-21 12:55:15 +02001748const struct dev_pm_ops bmc150_accel_pm_ops = {
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001749 SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend, bmc150_accel_resume)
1750 SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend,
1751 bmc150_accel_runtime_resume, NULL)
1752};
Markus Pargmann55637c32015-09-21 12:55:15 +02001753EXPORT_SYMBOL_GPL(bmc150_accel_pm_ops);
Srinivas Pandruvadabd7fe5b2014-05-08 22:57:00 +01001754
1755MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1756MODULE_LICENSE("GPL v2");
1757MODULE_DESCRIPTION("BMC150 accelerometer driver");