mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / iio / accel / bmc150-accel-core.c
blob197e693e7e7b795e9002e10430fd3f41399f96a7
1 /*
2 * 3-axis accelerometer driver supporting following Bosch-Sensortec chips:
3 * - BMC150
4 * - BMI055
5 * - BMA255
6 * - BMA250E
7 * - BMA222E
8 * - BMA280
10 * Copyright (c) 2014, Intel Corporation.
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.
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.
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/pm.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/iio/iio.h>
31 #include <linux/iio/sysfs.h>
32 #include <linux/iio/buffer.h>
33 #include <linux/iio/events.h>
34 #include <linux/iio/trigger.h>
35 #include <linux/iio/trigger_consumer.h>
36 #include <linux/iio/triggered_buffer.h>
37 #include <linux/regmap.h>
39 #include "bmc150-accel.h"
41 #define BMC150_ACCEL_DRV_NAME "bmc150_accel"
42 #define BMC150_ACCEL_IRQ_NAME "bmc150_accel_event"
44 #define BMC150_ACCEL_REG_CHIP_ID 0x00
46 #define BMC150_ACCEL_REG_INT_STATUS_2 0x0B
47 #define BMC150_ACCEL_ANY_MOTION_MASK 0x07
48 #define BMC150_ACCEL_ANY_MOTION_BIT_X BIT(0)
49 #define BMC150_ACCEL_ANY_MOTION_BIT_Y BIT(1)
50 #define BMC150_ACCEL_ANY_MOTION_BIT_Z BIT(2)
51 #define BMC150_ACCEL_ANY_MOTION_BIT_SIGN BIT(3)
53 #define BMC150_ACCEL_REG_PMU_LPW 0x11
54 #define BMC150_ACCEL_PMU_MODE_MASK 0xE0
55 #define BMC150_ACCEL_PMU_MODE_SHIFT 5
56 #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_MASK 0x17
57 #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT 1
59 #define BMC150_ACCEL_REG_PMU_RANGE 0x0F
61 #define BMC150_ACCEL_DEF_RANGE_2G 0x03
62 #define BMC150_ACCEL_DEF_RANGE_4G 0x05
63 #define BMC150_ACCEL_DEF_RANGE_8G 0x08
64 #define BMC150_ACCEL_DEF_RANGE_16G 0x0C
66 /* Default BW: 125Hz */
67 #define BMC150_ACCEL_REG_PMU_BW 0x10
68 #define BMC150_ACCEL_DEF_BW 125
70 #define BMC150_ACCEL_REG_INT_MAP_0 0x19
71 #define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2)
73 #define BMC150_ACCEL_REG_INT_MAP_1 0x1A
74 #define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0)
75 #define BMC150_ACCEL_INT_MAP_1_BIT_FWM BIT(1)
76 #define BMC150_ACCEL_INT_MAP_1_BIT_FFULL BIT(2)
78 #define BMC150_ACCEL_REG_INT_RST_LATCH 0x21
79 #define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80
80 #define BMC150_ACCEL_INT_MODE_LATCH_INT 0x0F
81 #define BMC150_ACCEL_INT_MODE_NON_LATCH_INT 0x00
83 #define BMC150_ACCEL_REG_INT_EN_0 0x16
84 #define BMC150_ACCEL_INT_EN_BIT_SLP_X BIT(0)
85 #define BMC150_ACCEL_INT_EN_BIT_SLP_Y BIT(1)
86 #define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2)
88 #define BMC150_ACCEL_REG_INT_EN_1 0x17
89 #define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4)
90 #define BMC150_ACCEL_INT_EN_BIT_FFULL_EN BIT(5)
91 #define BMC150_ACCEL_INT_EN_BIT_FWM_EN BIT(6)
93 #define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20
94 #define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0)
96 #define BMC150_ACCEL_REG_INT_5 0x27
97 #define BMC150_ACCEL_SLOPE_DUR_MASK 0x03
99 #define BMC150_ACCEL_REG_INT_6 0x28
100 #define BMC150_ACCEL_SLOPE_THRES_MASK 0xFF
102 /* Slope duration in terms of number of samples */
103 #define BMC150_ACCEL_DEF_SLOPE_DURATION 1
104 /* in terms of multiples of g's/LSB, based on range */
105 #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 1
107 #define BMC150_ACCEL_REG_XOUT_L 0x02
109 #define BMC150_ACCEL_MAX_STARTUP_TIME_MS 100
111 /* Sleep Duration values */
112 #define BMC150_ACCEL_SLEEP_500_MICRO 0x05
113 #define BMC150_ACCEL_SLEEP_1_MS 0x06
114 #define BMC150_ACCEL_SLEEP_2_MS 0x07
115 #define BMC150_ACCEL_SLEEP_4_MS 0x08
116 #define BMC150_ACCEL_SLEEP_6_MS 0x09
117 #define BMC150_ACCEL_SLEEP_10_MS 0x0A
118 #define BMC150_ACCEL_SLEEP_25_MS 0x0B
119 #define BMC150_ACCEL_SLEEP_50_MS 0x0C
120 #define BMC150_ACCEL_SLEEP_100_MS 0x0D
121 #define BMC150_ACCEL_SLEEP_500_MS 0x0E
122 #define BMC150_ACCEL_SLEEP_1_SEC 0x0F
124 #define BMC150_ACCEL_REG_TEMP 0x08
125 #define BMC150_ACCEL_TEMP_CENTER_VAL 24
127 #define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2))
128 #define BMC150_AUTO_SUSPEND_DELAY_MS 2000
130 #define BMC150_ACCEL_REG_FIFO_STATUS 0x0E
131 #define BMC150_ACCEL_REG_FIFO_CONFIG0 0x30
132 #define BMC150_ACCEL_REG_FIFO_CONFIG1 0x3E
133 #define BMC150_ACCEL_REG_FIFO_DATA 0x3F
134 #define BMC150_ACCEL_FIFO_LENGTH 32
136 enum bmc150_accel_axis {
137 AXIS_X,
138 AXIS_Y,
139 AXIS_Z,
140 AXIS_MAX,
143 enum 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,
150 struct bmc150_scale_info {
151 int scale;
152 u8 reg_range;
155 struct bmc150_accel_chip_info {
156 const char *name;
157 u8 chip_id;
158 const struct iio_chan_spec *channels;
159 int num_channels;
160 const struct bmc150_scale_info scale_table[4];
163 struct bmc150_accel_interrupt {
164 const struct bmc150_accel_interrupt_info *info;
165 atomic_t users;
168 struct 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;
176 enum 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,
183 enum bmc150_accel_trigger_id {
184 BMC150_ACCEL_TRIGGER_DATA_READY,
185 BMC150_ACCEL_TRIGGER_ANY_MOTION,
186 BMC150_ACCEL_TRIGGERS,
189 struct bmc150_accel_data {
190 struct regmap *regmap;
191 int irq;
192 struct bmc150_accel_interrupt interrupts[BMC150_ACCEL_INTERRUPTS];
193 atomic_t active_intr;
194 struct bmc150_accel_trigger triggers[BMC150_ACCEL_TRIGGERS];
195 struct mutex mutex;
196 u8 fifo_mode, watermark;
197 s16 buffer[8];
198 u8 bw_bits;
199 u32 slope_dur;
200 u32 slope_thres;
201 u32 range;
202 int ev_enable_state;
203 int64_t timestamp, old_timestamp; /* Only used in hw fifo mode. */
204 const struct bmc150_accel_chip_info *chip_info;
207 static const struct {
208 int val;
209 int val2;
210 u8 bw_bits;
211 } bmc150_accel_samp_freq_table[] = { {15, 620000, 0x08},
212 {31, 260000, 0x09},
213 {62, 500000, 0x0A},
214 {125, 0, 0x0B},
215 {250, 0, 0x0C},
216 {500, 0, 0x0D},
217 {1000, 0, 0x0E},
218 {2000, 0, 0x0F} };
220 static const struct {
221 int bw_bits;
222 int msec;
223 } bmc150_accel_sample_upd_time[] = { {0x08, 64},
224 {0x09, 32},
225 {0x0A, 16},
226 {0x0B, 8},
227 {0x0C, 4},
228 {0x0D, 2},
229 {0x0E, 1},
230 {0x0F, 1} };
232 static const struct {
233 int sleep_dur;
234 u8 reg_value;
235 } bmc150_accel_sleep_value_table[] = { {0, 0},
236 {500, BMC150_ACCEL_SLEEP_500_MICRO},
237 {1000, BMC150_ACCEL_SLEEP_1_MS},
238 {2000, BMC150_ACCEL_SLEEP_2_MS},
239 {4000, BMC150_ACCEL_SLEEP_4_MS},
240 {6000, BMC150_ACCEL_SLEEP_6_MS},
241 {10000, BMC150_ACCEL_SLEEP_10_MS},
242 {25000, BMC150_ACCEL_SLEEP_25_MS},
243 {50000, BMC150_ACCEL_SLEEP_50_MS},
244 {100000, BMC150_ACCEL_SLEEP_100_MS},
245 {500000, BMC150_ACCEL_SLEEP_500_MS},
246 {1000000, BMC150_ACCEL_SLEEP_1_SEC} };
248 const struct regmap_config bmc150_regmap_conf = {
249 .reg_bits = 8,
250 .val_bits = 8,
251 .max_register = 0x3f,
253 EXPORT_SYMBOL_GPL(bmc150_regmap_conf);
255 static int bmc150_accel_set_mode(struct bmc150_accel_data *data,
256 enum bmc150_power_modes mode,
257 int dur_us)
259 struct device *dev = regmap_get_device(data->regmap);
260 int i;
261 int ret;
262 u8 lpw_bits;
263 int dur_val = -1;
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;
273 } else {
274 dur_val = 0;
277 if (dur_val < 0)
278 return -EINVAL;
280 lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT;
281 lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT);
283 dev_dbg(dev, "Set Mode bits %x\n", lpw_bits);
285 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits);
286 if (ret < 0) {
287 dev_err(dev, "Error writing reg_pmu_lpw\n");
288 return ret;
291 return 0;
294 static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val,
295 int val2)
297 int i;
298 int ret;
300 for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
301 if (bmc150_accel_samp_freq_table[i].val == val &&
302 bmc150_accel_samp_freq_table[i].val2 == val2) {
303 ret = regmap_write(data->regmap,
304 BMC150_ACCEL_REG_PMU_BW,
305 bmc150_accel_samp_freq_table[i].bw_bits);
306 if (ret < 0)
307 return ret;
309 data->bw_bits =
310 bmc150_accel_samp_freq_table[i].bw_bits;
311 return 0;
315 return -EINVAL;
318 static int bmc150_accel_update_slope(struct bmc150_accel_data *data)
320 struct device *dev = regmap_get_device(data->regmap);
321 int ret;
323 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6,
324 data->slope_thres);
325 if (ret < 0) {
326 dev_err(dev, "Error writing reg_int_6\n");
327 return ret;
330 ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5,
331 BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur);
332 if (ret < 0) {
333 dev_err(dev, "Error updating reg_int_5\n");
334 return ret;
337 dev_dbg(dev, "%s: %x %x\n", __func__, data->slope_thres,
338 data->slope_dur);
340 return ret;
343 static int bmc150_accel_any_motion_setup(struct bmc150_accel_trigger *t,
344 bool state)
346 if (state)
347 return bmc150_accel_update_slope(t->data);
349 return 0;
352 static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val,
353 int *val2)
355 int i;
357 for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
358 if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) {
359 *val = bmc150_accel_samp_freq_table[i].val;
360 *val2 = bmc150_accel_samp_freq_table[i].val2;
361 return IIO_VAL_INT_PLUS_MICRO;
365 return -EINVAL;
368 #ifdef CONFIG_PM
369 static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data)
371 int i;
373 for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) {
374 if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits)
375 return bmc150_accel_sample_upd_time[i].msec;
378 return BMC150_ACCEL_MAX_STARTUP_TIME_MS;
381 static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
383 struct device *dev = regmap_get_device(data->regmap);
384 int ret;
386 if (on) {
387 ret = pm_runtime_get_sync(dev);
388 } else {
389 pm_runtime_mark_last_busy(dev);
390 ret = pm_runtime_put_autosuspend(dev);
393 if (ret < 0) {
394 dev_err(dev,
395 "Failed: bmc150_accel_set_power_state for %d\n", on);
396 if (on)
397 pm_runtime_put_noidle(dev);
399 return ret;
402 return 0;
404 #else
405 static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
407 return 0;
409 #endif
411 static const struct bmc150_accel_interrupt_info {
412 u8 map_reg;
413 u8 map_bitmask;
414 u8 en_reg;
415 u8 en_bitmask;
416 } bmc150_accel_interrupts[BMC150_ACCEL_INTERRUPTS] = {
417 { /* data ready interrupt */
418 .map_reg = BMC150_ACCEL_REG_INT_MAP_1,
419 .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_DATA,
420 .en_reg = BMC150_ACCEL_REG_INT_EN_1,
421 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_DATA_EN,
423 { /* motion interrupt */
424 .map_reg = BMC150_ACCEL_REG_INT_MAP_0,
425 .map_bitmask = BMC150_ACCEL_INT_MAP_0_BIT_SLOPE,
426 .en_reg = BMC150_ACCEL_REG_INT_EN_0,
427 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_SLP_X |
428 BMC150_ACCEL_INT_EN_BIT_SLP_Y |
429 BMC150_ACCEL_INT_EN_BIT_SLP_Z
431 { /* fifo watermark interrupt */
432 .map_reg = BMC150_ACCEL_REG_INT_MAP_1,
433 .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_FWM,
434 .en_reg = BMC150_ACCEL_REG_INT_EN_1,
435 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_FWM_EN,
439 static void bmc150_accel_interrupts_setup(struct iio_dev *indio_dev,
440 struct bmc150_accel_data *data)
442 int i;
444 for (i = 0; i < BMC150_ACCEL_INTERRUPTS; i++)
445 data->interrupts[i].info = &bmc150_accel_interrupts[i];
448 static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i,
449 bool state)
451 struct device *dev = regmap_get_device(data->regmap);
452 struct bmc150_accel_interrupt *intr = &data->interrupts[i];
453 const struct bmc150_accel_interrupt_info *info = intr->info;
454 int ret;
456 if (state) {
457 if (atomic_inc_return(&intr->users) > 1)
458 return 0;
459 } else {
460 if (atomic_dec_return(&intr->users) > 0)
461 return 0;
465 * We will expect the enable and disable to do operation in reverse
466 * order. This will happen here anyway, as our resume operation uses
467 * sync mode runtime pm calls. The suspend operation will be delayed
468 * by autosuspend delay.
469 * So the disable operation will still happen in reverse order of
470 * enable operation. When runtime pm is disabled the mode is always on,
471 * so sequence doesn't matter.
473 ret = bmc150_accel_set_power_state(data, state);
474 if (ret < 0)
475 return ret;
477 /* map the interrupt to the appropriate pins */
478 ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask,
479 (state ? info->map_bitmask : 0));
480 if (ret < 0) {
481 dev_err(dev, "Error updating reg_int_map\n");
482 goto out_fix_power_state;
485 /* enable/disable the interrupt */
486 ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask,
487 (state ? info->en_bitmask : 0));
488 if (ret < 0) {
489 dev_err(dev, "Error updating reg_int_en\n");
490 goto out_fix_power_state;
493 if (state)
494 atomic_inc(&data->active_intr);
495 else
496 atomic_dec(&data->active_intr);
498 return 0;
500 out_fix_power_state:
501 bmc150_accel_set_power_state(data, false);
502 return ret;
505 static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
507 struct device *dev = regmap_get_device(data->regmap);
508 int ret, i;
510 for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) {
511 if (data->chip_info->scale_table[i].scale == val) {
512 ret = regmap_write(data->regmap,
513 BMC150_ACCEL_REG_PMU_RANGE,
514 data->chip_info->scale_table[i].reg_range);
515 if (ret < 0) {
516 dev_err(dev, "Error writing pmu_range\n");
517 return ret;
520 data->range = data->chip_info->scale_table[i].reg_range;
521 return 0;
525 return -EINVAL;
528 static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val)
530 struct device *dev = regmap_get_device(data->regmap);
531 int ret;
532 unsigned int value;
534 mutex_lock(&data->mutex);
536 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value);
537 if (ret < 0) {
538 dev_err(dev, "Error reading reg_temp\n");
539 mutex_unlock(&data->mutex);
540 return ret;
542 *val = sign_extend32(value, 7);
544 mutex_unlock(&data->mutex);
546 return IIO_VAL_INT;
549 static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
550 struct iio_chan_spec const *chan,
551 int *val)
553 struct device *dev = regmap_get_device(data->regmap);
554 int ret;
555 int axis = chan->scan_index;
556 __le16 raw_val;
558 mutex_lock(&data->mutex);
559 ret = bmc150_accel_set_power_state(data, true);
560 if (ret < 0) {
561 mutex_unlock(&data->mutex);
562 return ret;
565 ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
566 &raw_val, sizeof(raw_val));
567 if (ret < 0) {
568 dev_err(dev, "Error reading axis %d\n", axis);
569 bmc150_accel_set_power_state(data, false);
570 mutex_unlock(&data->mutex);
571 return ret;
573 *val = sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift,
574 chan->scan_type.realbits - 1);
575 ret = bmc150_accel_set_power_state(data, false);
576 mutex_unlock(&data->mutex);
577 if (ret < 0)
578 return ret;
580 return IIO_VAL_INT;
583 static int bmc150_accel_read_raw(struct iio_dev *indio_dev,
584 struct iio_chan_spec const *chan,
585 int *val, int *val2, long mask)
587 struct bmc150_accel_data *data = iio_priv(indio_dev);
588 int ret;
590 switch (mask) {
591 case IIO_CHAN_INFO_RAW:
592 switch (chan->type) {
593 case IIO_TEMP:
594 return bmc150_accel_get_temp(data, val);
595 case IIO_ACCEL:
596 if (iio_buffer_enabled(indio_dev))
597 return -EBUSY;
598 else
599 return bmc150_accel_get_axis(data, chan, val);
600 default:
601 return -EINVAL;
603 case IIO_CHAN_INFO_OFFSET:
604 if (chan->type == IIO_TEMP) {
605 *val = BMC150_ACCEL_TEMP_CENTER_VAL;
606 return IIO_VAL_INT;
607 } else {
608 return -EINVAL;
610 case IIO_CHAN_INFO_SCALE:
611 *val = 0;
612 switch (chan->type) {
613 case IIO_TEMP:
614 *val2 = 500000;
615 return IIO_VAL_INT_PLUS_MICRO;
616 case IIO_ACCEL:
618 int i;
619 const struct bmc150_scale_info *si;
620 int st_size = ARRAY_SIZE(data->chip_info->scale_table);
622 for (i = 0; i < st_size; ++i) {
623 si = &data->chip_info->scale_table[i];
624 if (si->reg_range == data->range) {
625 *val2 = si->scale;
626 return IIO_VAL_INT_PLUS_MICRO;
629 return -EINVAL;
631 default:
632 return -EINVAL;
634 case IIO_CHAN_INFO_SAMP_FREQ:
635 mutex_lock(&data->mutex);
636 ret = bmc150_accel_get_bw(data, val, val2);
637 mutex_unlock(&data->mutex);
638 return ret;
639 default:
640 return -EINVAL;
644 static int bmc150_accel_write_raw(struct iio_dev *indio_dev,
645 struct iio_chan_spec const *chan,
646 int val, int val2, long mask)
648 struct bmc150_accel_data *data = iio_priv(indio_dev);
649 int ret;
651 switch (mask) {
652 case IIO_CHAN_INFO_SAMP_FREQ:
653 mutex_lock(&data->mutex);
654 ret = bmc150_accel_set_bw(data, val, val2);
655 mutex_unlock(&data->mutex);
656 break;
657 case IIO_CHAN_INFO_SCALE:
658 if (val)
659 return -EINVAL;
661 mutex_lock(&data->mutex);
662 ret = bmc150_accel_set_scale(data, val2);
663 mutex_unlock(&data->mutex);
664 return ret;
665 default:
666 ret = -EINVAL;
669 return ret;
672 static int bmc150_accel_read_event(struct iio_dev *indio_dev,
673 const struct iio_chan_spec *chan,
674 enum iio_event_type type,
675 enum iio_event_direction dir,
676 enum iio_event_info info,
677 int *val, int *val2)
679 struct bmc150_accel_data *data = iio_priv(indio_dev);
681 *val2 = 0;
682 switch (info) {
683 case IIO_EV_INFO_VALUE:
684 *val = data->slope_thres;
685 break;
686 case IIO_EV_INFO_PERIOD:
687 *val = data->slope_dur;
688 break;
689 default:
690 return -EINVAL;
693 return IIO_VAL_INT;
696 static int bmc150_accel_write_event(struct iio_dev *indio_dev,
697 const struct iio_chan_spec *chan,
698 enum iio_event_type type,
699 enum iio_event_direction dir,
700 enum iio_event_info info,
701 int val, int val2)
703 struct bmc150_accel_data *data = iio_priv(indio_dev);
705 if (data->ev_enable_state)
706 return -EBUSY;
708 switch (info) {
709 case IIO_EV_INFO_VALUE:
710 data->slope_thres = val & BMC150_ACCEL_SLOPE_THRES_MASK;
711 break;
712 case IIO_EV_INFO_PERIOD:
713 data->slope_dur = val & BMC150_ACCEL_SLOPE_DUR_MASK;
714 break;
715 default:
716 return -EINVAL;
719 return 0;
722 static int bmc150_accel_read_event_config(struct iio_dev *indio_dev,
723 const struct iio_chan_spec *chan,
724 enum iio_event_type type,
725 enum iio_event_direction dir)
727 struct bmc150_accel_data *data = iio_priv(indio_dev);
729 return data->ev_enable_state;
732 static int bmc150_accel_write_event_config(struct iio_dev *indio_dev,
733 const struct iio_chan_spec *chan,
734 enum iio_event_type type,
735 enum iio_event_direction dir,
736 int state)
738 struct bmc150_accel_data *data = iio_priv(indio_dev);
739 int ret;
741 if (state == data->ev_enable_state)
742 return 0;
744 mutex_lock(&data->mutex);
746 ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_ANY_MOTION,
747 state);
748 if (ret < 0) {
749 mutex_unlock(&data->mutex);
750 return ret;
753 data->ev_enable_state = state;
754 mutex_unlock(&data->mutex);
756 return 0;
759 static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev,
760 struct iio_trigger *trig)
762 struct bmc150_accel_data *data = iio_priv(indio_dev);
763 int i;
765 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
766 if (data->triggers[i].indio_trig == trig)
767 return 0;
770 return -EINVAL;
773 static ssize_t bmc150_accel_get_fifo_watermark(struct device *dev,
774 struct device_attribute *attr,
775 char *buf)
777 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
778 struct bmc150_accel_data *data = iio_priv(indio_dev);
779 int wm;
781 mutex_lock(&data->mutex);
782 wm = data->watermark;
783 mutex_unlock(&data->mutex);
785 return sprintf(buf, "%d\n", wm);
788 static ssize_t bmc150_accel_get_fifo_state(struct device *dev,
789 struct device_attribute *attr,
790 char *buf)
792 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
793 struct bmc150_accel_data *data = iio_priv(indio_dev);
794 bool state;
796 mutex_lock(&data->mutex);
797 state = data->fifo_mode;
798 mutex_unlock(&data->mutex);
800 return sprintf(buf, "%d\n", state);
803 static IIO_CONST_ATTR(hwfifo_watermark_min, "1");
804 static IIO_CONST_ATTR(hwfifo_watermark_max,
805 __stringify(BMC150_ACCEL_FIFO_LENGTH));
806 static IIO_DEVICE_ATTR(hwfifo_enabled, S_IRUGO,
807 bmc150_accel_get_fifo_state, NULL, 0);
808 static IIO_DEVICE_ATTR(hwfifo_watermark, S_IRUGO,
809 bmc150_accel_get_fifo_watermark, NULL, 0);
811 static const struct attribute *bmc150_accel_fifo_attributes[] = {
812 &iio_const_attr_hwfifo_watermark_min.dev_attr.attr,
813 &iio_const_attr_hwfifo_watermark_max.dev_attr.attr,
814 &iio_dev_attr_hwfifo_watermark.dev_attr.attr,
815 &iio_dev_attr_hwfifo_enabled.dev_attr.attr,
816 NULL,
819 static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
821 struct bmc150_accel_data *data = iio_priv(indio_dev);
823 if (val > BMC150_ACCEL_FIFO_LENGTH)
824 val = BMC150_ACCEL_FIFO_LENGTH;
826 mutex_lock(&data->mutex);
827 data->watermark = val;
828 mutex_unlock(&data->mutex);
830 return 0;
834 * We must read at least one full frame in one burst, otherwise the rest of the
835 * frame data is discarded.
837 static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
838 char *buffer, int samples)
840 struct device *dev = regmap_get_device(data->regmap);
841 int sample_length = 3 * 2;
842 int ret;
843 int total_length = samples * sample_length;
844 int i;
845 size_t step = regmap_get_raw_read_max(data->regmap);
847 if (!step || step > total_length)
848 step = total_length;
849 else if (step < total_length)
850 step = sample_length;
853 * Seems we have a bus with size limitation so we have to execute
854 * multiple reads
856 for (i = 0; i < total_length; i += step) {
857 ret = regmap_raw_read(data->regmap, BMC150_ACCEL_REG_FIFO_DATA,
858 &buffer[i], step);
859 if (ret)
860 break;
863 if (ret)
864 dev_err(dev,
865 "Error transferring data from fifo in single steps of %zu\n",
866 step);
868 return ret;
871 static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
872 unsigned samples, bool irq)
874 struct bmc150_accel_data *data = iio_priv(indio_dev);
875 struct device *dev = regmap_get_device(data->regmap);
876 int ret, i;
877 u8 count;
878 u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
879 int64_t tstamp;
880 uint64_t sample_period;
881 unsigned int val;
883 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val);
884 if (ret < 0) {
885 dev_err(dev, "Error reading reg_fifo_status\n");
886 return ret;
889 count = val & 0x7F;
891 if (!count)
892 return 0;
895 * If we getting called from IRQ handler we know the stored timestamp is
896 * fairly accurate for the last stored sample. Otherwise, if we are
897 * called as a result of a read operation from userspace and hence
898 * before the watermark interrupt was triggered, take a timestamp
899 * now. We can fall anywhere in between two samples so the error in this
900 * case is at most one sample period.
902 if (!irq) {
903 data->old_timestamp = data->timestamp;
904 data->timestamp = iio_get_time_ns();
908 * Approximate timestamps for each of the sample based on the sampling
909 * frequency, timestamp for last sample and number of samples.
911 * Note that we can't use the current bandwidth settings to compute the
912 * sample period because the sample rate varies with the device
913 * (e.g. between 31.70ms to 32.20ms for a bandwidth of 15.63HZ). That
914 * small variation adds when we store a large number of samples and
915 * creates significant jitter between the last and first samples in
916 * different batches (e.g. 32ms vs 21ms).
918 * To avoid this issue we compute the actual sample period ourselves
919 * based on the timestamp delta between the last two flush operations.
921 sample_period = (data->timestamp - data->old_timestamp);
922 do_div(sample_period, count);
923 tstamp = data->timestamp - (count - 1) * sample_period;
925 if (samples && count > samples)
926 count = samples;
928 ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count);
929 if (ret)
930 return ret;
933 * Ideally we want the IIO core to handle the demux when running in fifo
934 * mode but not when running in triggered buffer mode. Unfortunately
935 * this does not seem to be possible, so stick with driver demux for
936 * now.
938 for (i = 0; i < count; i++) {
939 u16 sample[8];
940 int j, bit;
942 j = 0;
943 for_each_set_bit(bit, indio_dev->active_scan_mask,
944 indio_dev->masklength)
945 memcpy(&sample[j++], &buffer[i * 3 + bit], 2);
947 iio_push_to_buffers_with_timestamp(indio_dev, sample, tstamp);
949 tstamp += sample_period;
952 return count;
955 static int bmc150_accel_fifo_flush(struct iio_dev *indio_dev, unsigned samples)
957 struct bmc150_accel_data *data = iio_priv(indio_dev);
958 int ret;
960 mutex_lock(&data->mutex);
961 ret = __bmc150_accel_fifo_flush(indio_dev, samples, false);
962 mutex_unlock(&data->mutex);
964 return ret;
967 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
968 "15.620000 31.260000 62.50000 125 250 500 1000 2000");
970 static struct attribute *bmc150_accel_attributes[] = {
971 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
972 NULL,
975 static const struct attribute_group bmc150_accel_attrs_group = {
976 .attrs = bmc150_accel_attributes,
979 static const struct iio_event_spec bmc150_accel_event = {
980 .type = IIO_EV_TYPE_ROC,
981 .dir = IIO_EV_DIR_EITHER,
982 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
983 BIT(IIO_EV_INFO_ENABLE) |
984 BIT(IIO_EV_INFO_PERIOD)
987 #define BMC150_ACCEL_CHANNEL(_axis, bits) { \
988 .type = IIO_ACCEL, \
989 .modified = 1, \
990 .channel2 = IIO_MOD_##_axis, \
991 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
992 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
993 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
994 .scan_index = AXIS_##_axis, \
995 .scan_type = { \
996 .sign = 's', \
997 .realbits = (bits), \
998 .storagebits = 16, \
999 .shift = 16 - (bits), \
1000 .endianness = IIO_LE, \
1001 }, \
1002 .event_spec = &bmc150_accel_event, \
1003 .num_event_specs = 1 \
1006 #define BMC150_ACCEL_CHANNELS(bits) { \
1008 .type = IIO_TEMP, \
1009 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1010 BIT(IIO_CHAN_INFO_SCALE) | \
1011 BIT(IIO_CHAN_INFO_OFFSET), \
1012 .scan_index = -1, \
1013 }, \
1014 BMC150_ACCEL_CHANNEL(X, bits), \
1015 BMC150_ACCEL_CHANNEL(Y, bits), \
1016 BMC150_ACCEL_CHANNEL(Z, bits), \
1017 IIO_CHAN_SOFT_TIMESTAMP(3), \
1020 static const struct iio_chan_spec bma222e_accel_channels[] =
1021 BMC150_ACCEL_CHANNELS(8);
1022 static const struct iio_chan_spec bma250e_accel_channels[] =
1023 BMC150_ACCEL_CHANNELS(10);
1024 static const struct iio_chan_spec bmc150_accel_channels[] =
1025 BMC150_ACCEL_CHANNELS(12);
1026 static const struct iio_chan_spec bma280_accel_channels[] =
1027 BMC150_ACCEL_CHANNELS(14);
1029 static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl[] = {
1030 [bmc150] = {
1031 .name = "BMC150A",
1032 .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} },
1040 [bmi055] = {
1041 .name = "BMI055A",
1042 .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} },
1050 [bma255] = {
1051 .name = "BMA0255",
1052 .chip_id = 0xFA,
1053 .channels = bmc150_accel_channels,
1054 .num_channels = ARRAY_SIZE(bmc150_accel_channels),
1055 .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
1056 {19122, BMC150_ACCEL_DEF_RANGE_4G},
1057 {38344, BMC150_ACCEL_DEF_RANGE_8G},
1058 {76590, BMC150_ACCEL_DEF_RANGE_16G} },
1060 [bma250e] = {
1061 .name = "BMA250E",
1062 .chip_id = 0xF9,
1063 .channels = bma250e_accel_channels,
1064 .num_channels = ARRAY_SIZE(bma250e_accel_channels),
1065 .scale_table = { {38344, BMC150_ACCEL_DEF_RANGE_2G},
1066 {76590, BMC150_ACCEL_DEF_RANGE_4G},
1067 {153277, BMC150_ACCEL_DEF_RANGE_8G},
1068 {306457, BMC150_ACCEL_DEF_RANGE_16G} },
1070 [bma222e] = {
1071 .name = "BMA222E",
1072 .chip_id = 0xF8,
1073 .channels = bma222e_accel_channels,
1074 .num_channels = ARRAY_SIZE(bma222e_accel_channels),
1075 .scale_table = { {153277, BMC150_ACCEL_DEF_RANGE_2G},
1076 {306457, BMC150_ACCEL_DEF_RANGE_4G},
1077 {612915, BMC150_ACCEL_DEF_RANGE_8G},
1078 {1225831, BMC150_ACCEL_DEF_RANGE_16G} },
1080 [bma280] = {
1081 .name = "BMA0280",
1082 .chip_id = 0xFB,
1083 .channels = bma280_accel_channels,
1084 .num_channels = ARRAY_SIZE(bma280_accel_channels),
1085 .scale_table = { {2392, BMC150_ACCEL_DEF_RANGE_2G},
1086 {4785, BMC150_ACCEL_DEF_RANGE_4G},
1087 {9581, BMC150_ACCEL_DEF_RANGE_8G},
1088 {19152, BMC150_ACCEL_DEF_RANGE_16G} },
1092 static const struct iio_info bmc150_accel_info = {
1093 .attrs = &bmc150_accel_attrs_group,
1094 .read_raw = bmc150_accel_read_raw,
1095 .write_raw = bmc150_accel_write_raw,
1096 .read_event_value = bmc150_accel_read_event,
1097 .write_event_value = bmc150_accel_write_event,
1098 .write_event_config = bmc150_accel_write_event_config,
1099 .read_event_config = bmc150_accel_read_event_config,
1100 .driver_module = THIS_MODULE,
1103 static const struct iio_info bmc150_accel_info_fifo = {
1104 .attrs = &bmc150_accel_attrs_group,
1105 .read_raw = bmc150_accel_read_raw,
1106 .write_raw = bmc150_accel_write_raw,
1107 .read_event_value = bmc150_accel_read_event,
1108 .write_event_value = bmc150_accel_write_event,
1109 .write_event_config = bmc150_accel_write_event_config,
1110 .read_event_config = bmc150_accel_read_event_config,
1111 .validate_trigger = bmc150_accel_validate_trigger,
1112 .hwfifo_set_watermark = bmc150_accel_set_watermark,
1113 .hwfifo_flush_to_buffer = bmc150_accel_fifo_flush,
1114 .driver_module = THIS_MODULE,
1117 static const unsigned long bmc150_accel_scan_masks[] = {
1118 BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
1121 static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
1123 struct iio_poll_func *pf = p;
1124 struct iio_dev *indio_dev = pf->indio_dev;
1125 struct bmc150_accel_data *data = iio_priv(indio_dev);
1126 int ret;
1128 mutex_lock(&data->mutex);
1129 ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L,
1130 data->buffer, AXIS_MAX * 2);
1131 mutex_unlock(&data->mutex);
1132 if (ret < 0)
1133 goto err_read;
1135 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
1136 pf->timestamp);
1137 err_read:
1138 iio_trigger_notify_done(indio_dev->trig);
1140 return IRQ_HANDLED;
1143 static int bmc150_accel_trig_try_reen(struct iio_trigger *trig)
1145 struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1146 struct bmc150_accel_data *data = t->data;
1147 struct device *dev = regmap_get_device(data->regmap);
1148 int ret;
1150 /* new data interrupts don't need ack */
1151 if (t == &t->data->triggers[BMC150_ACCEL_TRIGGER_DATA_READY])
1152 return 0;
1154 mutex_lock(&data->mutex);
1155 /* clear any latched interrupt */
1156 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1157 BMC150_ACCEL_INT_MODE_LATCH_INT |
1158 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1159 mutex_unlock(&data->mutex);
1160 if (ret < 0) {
1161 dev_err(dev, "Error writing reg_int_rst_latch\n");
1162 return ret;
1165 return 0;
1168 static int bmc150_accel_trigger_set_state(struct iio_trigger *trig,
1169 bool state)
1171 struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1172 struct bmc150_accel_data *data = t->data;
1173 int ret;
1175 mutex_lock(&data->mutex);
1177 if (t->enabled == state) {
1178 mutex_unlock(&data->mutex);
1179 return 0;
1182 if (t->setup) {
1183 ret = t->setup(t, state);
1184 if (ret < 0) {
1185 mutex_unlock(&data->mutex);
1186 return ret;
1190 ret = bmc150_accel_set_interrupt(data, t->intr, state);
1191 if (ret < 0) {
1192 mutex_unlock(&data->mutex);
1193 return ret;
1196 t->enabled = state;
1198 mutex_unlock(&data->mutex);
1200 return ret;
1203 static const struct iio_trigger_ops bmc150_accel_trigger_ops = {
1204 .set_trigger_state = bmc150_accel_trigger_set_state,
1205 .try_reenable = bmc150_accel_trig_try_reen,
1206 .owner = THIS_MODULE,
1209 static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev)
1211 struct bmc150_accel_data *data = iio_priv(indio_dev);
1212 struct device *dev = regmap_get_device(data->regmap);
1213 int dir;
1214 int ret;
1215 unsigned int val;
1217 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val);
1218 if (ret < 0) {
1219 dev_err(dev, "Error reading reg_int_status_2\n");
1220 return ret;
1223 if (val & BMC150_ACCEL_ANY_MOTION_BIT_SIGN)
1224 dir = IIO_EV_DIR_FALLING;
1225 else
1226 dir = IIO_EV_DIR_RISING;
1228 if (val & BMC150_ACCEL_ANY_MOTION_BIT_X)
1229 iio_push_event(indio_dev,
1230 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1232 IIO_MOD_X,
1233 IIO_EV_TYPE_ROC,
1234 dir),
1235 data->timestamp);
1237 if (val & BMC150_ACCEL_ANY_MOTION_BIT_Y)
1238 iio_push_event(indio_dev,
1239 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1241 IIO_MOD_Y,
1242 IIO_EV_TYPE_ROC,
1243 dir),
1244 data->timestamp);
1246 if (val & BMC150_ACCEL_ANY_MOTION_BIT_Z)
1247 iio_push_event(indio_dev,
1248 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1250 IIO_MOD_Z,
1251 IIO_EV_TYPE_ROC,
1252 dir),
1253 data->timestamp);
1255 return ret;
1258 static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private)
1260 struct iio_dev *indio_dev = private;
1261 struct bmc150_accel_data *data = iio_priv(indio_dev);
1262 struct device *dev = regmap_get_device(data->regmap);
1263 bool ack = false;
1264 int ret;
1266 mutex_lock(&data->mutex);
1268 if (data->fifo_mode) {
1269 ret = __bmc150_accel_fifo_flush(indio_dev,
1270 BMC150_ACCEL_FIFO_LENGTH, true);
1271 if (ret > 0)
1272 ack = true;
1275 if (data->ev_enable_state) {
1276 ret = bmc150_accel_handle_roc_event(indio_dev);
1277 if (ret > 0)
1278 ack = true;
1281 if (ack) {
1282 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1283 BMC150_ACCEL_INT_MODE_LATCH_INT |
1284 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1285 if (ret)
1286 dev_err(dev, "Error writing reg_int_rst_latch\n");
1288 ret = IRQ_HANDLED;
1289 } else {
1290 ret = IRQ_NONE;
1293 mutex_unlock(&data->mutex);
1295 return ret;
1298 static irqreturn_t bmc150_accel_irq_handler(int irq, void *private)
1300 struct iio_dev *indio_dev = private;
1301 struct bmc150_accel_data *data = iio_priv(indio_dev);
1302 bool ack = false;
1303 int i;
1305 data->old_timestamp = data->timestamp;
1306 data->timestamp = iio_get_time_ns();
1308 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1309 if (data->triggers[i].enabled) {
1310 iio_trigger_poll(data->triggers[i].indio_trig);
1311 ack = true;
1312 break;
1316 if (data->ev_enable_state || data->fifo_mode)
1317 return IRQ_WAKE_THREAD;
1319 if (ack)
1320 return IRQ_HANDLED;
1322 return IRQ_NONE;
1325 static const struct {
1326 int intr;
1327 const char *name;
1328 int (*setup)(struct bmc150_accel_trigger *t, bool state);
1329 } bmc150_accel_triggers[BMC150_ACCEL_TRIGGERS] = {
1331 .intr = 0,
1332 .name = "%s-dev%d",
1335 .intr = 1,
1336 .name = "%s-any-motion-dev%d",
1337 .setup = bmc150_accel_any_motion_setup,
1341 static void bmc150_accel_unregister_triggers(struct bmc150_accel_data *data,
1342 int from)
1344 int i;
1346 for (i = from; i >= 0; i--) {
1347 if (data->triggers[i].indio_trig) {
1348 iio_trigger_unregister(data->triggers[i].indio_trig);
1349 data->triggers[i].indio_trig = NULL;
1354 static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev,
1355 struct bmc150_accel_data *data)
1357 struct device *dev = regmap_get_device(data->regmap);
1358 int i, ret;
1360 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1361 struct bmc150_accel_trigger *t = &data->triggers[i];
1363 t->indio_trig = devm_iio_trigger_alloc(dev,
1364 bmc150_accel_triggers[i].name,
1365 indio_dev->name,
1366 indio_dev->id);
1367 if (!t->indio_trig) {
1368 ret = -ENOMEM;
1369 break;
1372 t->indio_trig->dev.parent = dev;
1373 t->indio_trig->ops = &bmc150_accel_trigger_ops;
1374 t->intr = bmc150_accel_triggers[i].intr;
1375 t->data = data;
1376 t->setup = bmc150_accel_triggers[i].setup;
1377 iio_trigger_set_drvdata(t->indio_trig, t);
1379 ret = iio_trigger_register(t->indio_trig);
1380 if (ret)
1381 break;
1384 if (ret)
1385 bmc150_accel_unregister_triggers(data, i - 1);
1387 return ret;
1390 #define BMC150_ACCEL_FIFO_MODE_STREAM 0x80
1391 #define BMC150_ACCEL_FIFO_MODE_FIFO 0x40
1392 #define BMC150_ACCEL_FIFO_MODE_BYPASS 0x00
1394 static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data)
1396 struct device *dev = regmap_get_device(data->regmap);
1397 u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1;
1398 int ret;
1400 ret = regmap_write(data->regmap, reg, data->fifo_mode);
1401 if (ret < 0) {
1402 dev_err(dev, "Error writing reg_fifo_config1\n");
1403 return ret;
1406 if (!data->fifo_mode)
1407 return 0;
1409 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0,
1410 data->watermark);
1411 if (ret < 0)
1412 dev_err(dev, "Error writing reg_fifo_config0\n");
1414 return ret;
1417 static int bmc150_accel_buffer_preenable(struct iio_dev *indio_dev)
1419 struct bmc150_accel_data *data = iio_priv(indio_dev);
1421 return bmc150_accel_set_power_state(data, true);
1424 static int bmc150_accel_buffer_postenable(struct iio_dev *indio_dev)
1426 struct bmc150_accel_data *data = iio_priv(indio_dev);
1427 int ret = 0;
1429 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
1430 return iio_triggered_buffer_postenable(indio_dev);
1432 mutex_lock(&data->mutex);
1434 if (!data->watermark)
1435 goto out;
1437 ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
1438 true);
1439 if (ret)
1440 goto out;
1442 data->fifo_mode = BMC150_ACCEL_FIFO_MODE_FIFO;
1444 ret = bmc150_accel_fifo_set_mode(data);
1445 if (ret) {
1446 data->fifo_mode = 0;
1447 bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
1448 false);
1451 out:
1452 mutex_unlock(&data->mutex);
1454 return ret;
1457 static int bmc150_accel_buffer_predisable(struct iio_dev *indio_dev)
1459 struct bmc150_accel_data *data = iio_priv(indio_dev);
1461 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
1462 return iio_triggered_buffer_predisable(indio_dev);
1464 mutex_lock(&data->mutex);
1466 if (!data->fifo_mode)
1467 goto out;
1469 bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK, false);
1470 __bmc150_accel_fifo_flush(indio_dev, BMC150_ACCEL_FIFO_LENGTH, false);
1471 data->fifo_mode = 0;
1472 bmc150_accel_fifo_set_mode(data);
1474 out:
1475 mutex_unlock(&data->mutex);
1477 return 0;
1480 static int bmc150_accel_buffer_postdisable(struct iio_dev *indio_dev)
1482 struct bmc150_accel_data *data = iio_priv(indio_dev);
1484 return bmc150_accel_set_power_state(data, false);
1487 static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops = {
1488 .preenable = bmc150_accel_buffer_preenable,
1489 .postenable = bmc150_accel_buffer_postenable,
1490 .predisable = bmc150_accel_buffer_predisable,
1491 .postdisable = bmc150_accel_buffer_postdisable,
1494 static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
1496 struct device *dev = regmap_get_device(data->regmap);
1497 int ret, i;
1498 unsigned int val;
1500 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val);
1501 if (ret < 0) {
1502 dev_err(dev, "Error: Reading chip id\n");
1503 return ret;
1506 dev_dbg(dev, "Chip Id %x\n", val);
1507 for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) {
1508 if (bmc150_accel_chip_info_tbl[i].chip_id == val) {
1509 data->chip_info = &bmc150_accel_chip_info_tbl[i];
1510 break;
1514 if (!data->chip_info) {
1515 dev_err(dev, "Invalid chip %x\n", val);
1516 return -ENODEV;
1519 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1520 if (ret < 0)
1521 return ret;
1523 /* Set Bandwidth */
1524 ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0);
1525 if (ret < 0)
1526 return ret;
1528 /* Set Default Range */
1529 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE,
1530 BMC150_ACCEL_DEF_RANGE_4G);
1531 if (ret < 0) {
1532 dev_err(dev, "Error writing reg_pmu_range\n");
1533 return ret;
1536 data->range = BMC150_ACCEL_DEF_RANGE_4G;
1538 /* Set default slope duration and thresholds */
1539 data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD;
1540 data->slope_dur = BMC150_ACCEL_DEF_SLOPE_DURATION;
1541 ret = bmc150_accel_update_slope(data);
1542 if (ret < 0)
1543 return ret;
1545 /* Set default as latched interrupts */
1546 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1547 BMC150_ACCEL_INT_MODE_LATCH_INT |
1548 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1549 if (ret < 0) {
1550 dev_err(dev, "Error writing reg_int_rst_latch\n");
1551 return ret;
1554 return 0;
1557 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
1558 const char *name, bool block_supported)
1560 struct bmc150_accel_data *data;
1561 struct iio_dev *indio_dev;
1562 int ret;
1564 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1565 if (!indio_dev)
1566 return -ENOMEM;
1568 data = iio_priv(indio_dev);
1569 dev_set_drvdata(dev, indio_dev);
1570 data->irq = irq;
1572 data->regmap = regmap;
1574 ret = bmc150_accel_chip_init(data);
1575 if (ret < 0)
1576 return ret;
1578 mutex_init(&data->mutex);
1580 indio_dev->dev.parent = dev;
1581 indio_dev->channels = data->chip_info->channels;
1582 indio_dev->num_channels = data->chip_info->num_channels;
1583 indio_dev->name = name ? name : data->chip_info->name;
1584 indio_dev->available_scan_masks = bmc150_accel_scan_masks;
1585 indio_dev->modes = INDIO_DIRECT_MODE;
1586 indio_dev->info = &bmc150_accel_info;
1588 ret = iio_triggered_buffer_setup(indio_dev,
1589 &iio_pollfunc_store_time,
1590 bmc150_accel_trigger_handler,
1591 &bmc150_accel_buffer_ops);
1592 if (ret < 0) {
1593 dev_err(dev, "Failed: iio triggered buffer setup\n");
1594 return ret;
1597 if (data->irq > 0) {
1598 ret = devm_request_threaded_irq(
1599 dev, data->irq,
1600 bmc150_accel_irq_handler,
1601 bmc150_accel_irq_thread_handler,
1602 IRQF_TRIGGER_RISING,
1603 BMC150_ACCEL_IRQ_NAME,
1604 indio_dev);
1605 if (ret)
1606 goto err_buffer_cleanup;
1609 * Set latched mode interrupt. While certain interrupts are
1610 * non-latched regardless of this settings (e.g. new data) we
1611 * want to use latch mode when we can to prevent interrupt
1612 * flooding.
1614 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1615 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1616 if (ret < 0) {
1617 dev_err(dev, "Error writing reg_int_rst_latch\n");
1618 goto err_buffer_cleanup;
1621 bmc150_accel_interrupts_setup(indio_dev, data);
1623 ret = bmc150_accel_triggers_setup(indio_dev, data);
1624 if (ret)
1625 goto err_buffer_cleanup;
1627 if (block_supported) {
1628 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
1629 indio_dev->info = &bmc150_accel_info_fifo;
1630 indio_dev->buffer->attrs = bmc150_accel_fifo_attributes;
1634 ret = pm_runtime_set_active(dev);
1635 if (ret)
1636 goto err_trigger_unregister;
1638 pm_runtime_enable(dev);
1639 pm_runtime_set_autosuspend_delay(dev, BMC150_AUTO_SUSPEND_DELAY_MS);
1640 pm_runtime_use_autosuspend(dev);
1642 ret = iio_device_register(indio_dev);
1643 if (ret < 0) {
1644 dev_err(dev, "Unable to register iio device\n");
1645 goto err_trigger_unregister;
1648 return 0;
1650 err_trigger_unregister:
1651 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
1652 err_buffer_cleanup:
1653 iio_triggered_buffer_cleanup(indio_dev);
1655 return ret;
1657 EXPORT_SYMBOL_GPL(bmc150_accel_core_probe);
1659 int bmc150_accel_core_remove(struct device *dev)
1661 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1662 struct bmc150_accel_data *data = iio_priv(indio_dev);
1664 iio_device_unregister(indio_dev);
1666 pm_runtime_disable(dev);
1667 pm_runtime_set_suspended(dev);
1668 pm_runtime_put_noidle(dev);
1670 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
1672 iio_triggered_buffer_cleanup(indio_dev);
1674 mutex_lock(&data->mutex);
1675 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0);
1676 mutex_unlock(&data->mutex);
1678 return 0;
1680 EXPORT_SYMBOL_GPL(bmc150_accel_core_remove);
1682 #ifdef CONFIG_PM_SLEEP
1683 static int bmc150_accel_suspend(struct device *dev)
1685 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1686 struct bmc150_accel_data *data = iio_priv(indio_dev);
1688 mutex_lock(&data->mutex);
1689 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1690 mutex_unlock(&data->mutex);
1692 return 0;
1695 static int bmc150_accel_resume(struct device *dev)
1697 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1698 struct bmc150_accel_data *data = iio_priv(indio_dev);
1700 mutex_lock(&data->mutex);
1701 if (atomic_read(&data->active_intr))
1702 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1703 bmc150_accel_fifo_set_mode(data);
1704 mutex_unlock(&data->mutex);
1706 return 0;
1708 #endif
1710 #ifdef CONFIG_PM
1711 static int bmc150_accel_runtime_suspend(struct device *dev)
1713 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1714 struct bmc150_accel_data *data = iio_priv(indio_dev);
1715 int ret;
1717 dev_dbg(dev, __func__);
1718 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1719 if (ret < 0)
1720 return -EAGAIN;
1722 return 0;
1725 static int bmc150_accel_runtime_resume(struct device *dev)
1727 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1728 struct bmc150_accel_data *data = iio_priv(indio_dev);
1729 int ret;
1730 int sleep_val;
1732 dev_dbg(dev, __func__);
1734 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1735 if (ret < 0)
1736 return ret;
1737 ret = bmc150_accel_fifo_set_mode(data);
1738 if (ret < 0)
1739 return ret;
1741 sleep_val = bmc150_accel_get_startup_times(data);
1742 if (sleep_val < 20)
1743 usleep_range(sleep_val * 1000, 20000);
1744 else
1745 msleep_interruptible(sleep_val);
1747 return 0;
1749 #endif
1751 const struct dev_pm_ops bmc150_accel_pm_ops = {
1752 SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend, bmc150_accel_resume)
1753 SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend,
1754 bmc150_accel_runtime_resume, NULL)
1756 EXPORT_SYMBOL_GPL(bmc150_accel_pm_ops);
1758 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1759 MODULE_LICENSE("GPL v2");
1760 MODULE_DESCRIPTION("BMC150 accelerometer driver");