2 * 3-axis accelerometer driver supporting following Bosch-Sensortec chips:
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
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>
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>
39 #define BMC150_ACCEL_DRV_NAME "bmc150_accel"
40 #define BMC150_ACCEL_IRQ_NAME "bmc150_accel_event"
41 #define BMC150_ACCEL_GPIO_NAME "bmc150_accel_int"
43 #define BMC150_ACCEL_REG_CHIP_ID 0x00
45 #define BMC150_ACCEL_REG_INT_STATUS_2 0x0B
46 #define BMC150_ACCEL_ANY_MOTION_MASK 0x07
47 #define BMC150_ACCEL_ANY_MOTION_BIT_SIGN BIT(3)
49 #define BMC150_ACCEL_REG_PMU_LPW 0x11
50 #define BMC150_ACCEL_PMU_MODE_MASK 0xE0
51 #define BMC150_ACCEL_PMU_MODE_SHIFT 5
52 #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_MASK 0x17
53 #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT 1
55 #define BMC150_ACCEL_REG_PMU_RANGE 0x0F
57 #define BMC150_ACCEL_DEF_RANGE_2G 0x03
58 #define BMC150_ACCEL_DEF_RANGE_4G 0x05
59 #define BMC150_ACCEL_DEF_RANGE_8G 0x08
60 #define BMC150_ACCEL_DEF_RANGE_16G 0x0C
62 /* Default BW: 125Hz */
63 #define BMC150_ACCEL_REG_PMU_BW 0x10
64 #define BMC150_ACCEL_DEF_BW 125
66 #define BMC150_ACCEL_REG_INT_MAP_0 0x19
67 #define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2)
69 #define BMC150_ACCEL_REG_INT_MAP_1 0x1A
70 #define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0)
72 #define BMC150_ACCEL_REG_INT_RST_LATCH 0x21
73 #define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80
74 #define BMC150_ACCEL_INT_MODE_LATCH_INT 0x0F
75 #define BMC150_ACCEL_INT_MODE_NON_LATCH_INT 0x00
77 #define BMC150_ACCEL_REG_INT_EN_0 0x16
78 #define BMC150_ACCEL_INT_EN_BIT_SLP_X BIT(0)
79 #define BMC150_ACCEL_INT_EN_BIT_SLP_Y BIT(1)
80 #define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2)
82 #define BMC150_ACCEL_REG_INT_EN_1 0x17
83 #define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4)
85 #define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20
86 #define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0)
88 #define BMC150_ACCEL_REG_INT_5 0x27
89 #define BMC150_ACCEL_SLOPE_DUR_MASK 0x03
91 #define BMC150_ACCEL_REG_INT_6 0x28
92 #define BMC150_ACCEL_SLOPE_THRES_MASK 0xFF
94 /* Slope duration in terms of number of samples */
95 #define BMC150_ACCEL_DEF_SLOPE_DURATION 2
96 /* in terms of multiples of g's/LSB, based on range */
97 #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 5
99 #define BMC150_ACCEL_REG_XOUT_L 0x02
101 #define BMC150_ACCEL_MAX_STARTUP_TIME_MS 100
103 /* Sleep Duration values */
104 #define BMC150_ACCEL_SLEEP_500_MICRO 0x05
105 #define BMC150_ACCEL_SLEEP_1_MS 0x06
106 #define BMC150_ACCEL_SLEEP_2_MS 0x07
107 #define BMC150_ACCEL_SLEEP_4_MS 0x08
108 #define BMC150_ACCEL_SLEEP_6_MS 0x09
109 #define BMC150_ACCEL_SLEEP_10_MS 0x0A
110 #define BMC150_ACCEL_SLEEP_25_MS 0x0B
111 #define BMC150_ACCEL_SLEEP_50_MS 0x0C
112 #define BMC150_ACCEL_SLEEP_100_MS 0x0D
113 #define BMC150_ACCEL_SLEEP_500_MS 0x0E
114 #define BMC150_ACCEL_SLEEP_1_SEC 0x0F
116 #define BMC150_ACCEL_REG_TEMP 0x08
117 #define BMC150_ACCEL_TEMP_CENTER_VAL 24
119 #define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2))
120 #define BMC150_AUTO_SUSPEND_DELAY_MS 2000
122 enum bmc150_accel_axis {
128 enum bmc150_power_modes {
129 BMC150_ACCEL_SLEEP_MODE_NORMAL,
130 BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND,
131 BMC150_ACCEL_SLEEP_MODE_LPM,
132 BMC150_ACCEL_SLEEP_MODE_SUSPEND = 0x04,
135 struct bmc150_scale_info {
140 struct bmc150_accel_chip_info {
142 const struct iio_chan_spec *channels;
144 const struct bmc150_scale_info scale_table[4];
147 struct bmc150_accel_data {
148 struct i2c_client *client;
149 struct iio_trigger *dready_trig;
150 struct iio_trigger *motion_trig;
158 bool dready_trigger_on;
159 bool motion_trigger_on;
161 const struct bmc150_accel_chip_info *chip_info;
164 static const struct {
168 } bmc150_accel_samp_freq_table[] = { {7, 810000, 0x08},
177 static const struct {
180 } bmc150_accel_sample_upd_time[] = { {0x08, 64},
189 static const struct {
192 } bmc150_accel_sleep_value_table[] = { {0, 0},
193 {500, BMC150_ACCEL_SLEEP_500_MICRO},
194 {1000, BMC150_ACCEL_SLEEP_1_MS},
195 {2000, BMC150_ACCEL_SLEEP_2_MS},
196 {4000, BMC150_ACCEL_SLEEP_4_MS},
197 {6000, BMC150_ACCEL_SLEEP_6_MS},
198 {10000, BMC150_ACCEL_SLEEP_10_MS},
199 {25000, BMC150_ACCEL_SLEEP_25_MS},
200 {50000, BMC150_ACCEL_SLEEP_50_MS},
201 {100000, BMC150_ACCEL_SLEEP_100_MS},
202 {500000, BMC150_ACCEL_SLEEP_500_MS},
203 {1000000, BMC150_ACCEL_SLEEP_1_SEC} };
206 static int bmc150_accel_set_mode(struct bmc150_accel_data *data,
207 enum bmc150_power_modes mode,
216 for (i = 0; i < ARRAY_SIZE(bmc150_accel_sleep_value_table);
218 if (bmc150_accel_sleep_value_table[i].sleep_dur ==
221 bmc150_accel_sleep_value_table[i].reg_value;
229 lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT;
230 lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT);
232 dev_dbg(&data->client->dev, "Set Mode bits %x\n", lpw_bits);
234 ret = i2c_smbus_write_byte_data(data->client,
235 BMC150_ACCEL_REG_PMU_LPW, lpw_bits);
237 dev_err(&data->client->dev, "Error writing reg_pmu_lpw\n");
244 static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val,
250 for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
251 if (bmc150_accel_samp_freq_table[i].val == val &&
252 bmc150_accel_samp_freq_table[i].val2 == val2) {
253 ret = i2c_smbus_write_byte_data(
255 BMC150_ACCEL_REG_PMU_BW,
256 bmc150_accel_samp_freq_table[i].bw_bits);
261 bmc150_accel_samp_freq_table[i].bw_bits;
269 static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
273 ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_CHIP_ID);
275 dev_err(&data->client->dev,
276 "Error: Reading chip id\n");
280 dev_dbg(&data->client->dev, "Chip Id %x\n", ret);
281 if (ret != data->chip_info->chip_id) {
282 dev_err(&data->client->dev, "Invalid chip %x\n", ret);
286 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
291 ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0);
295 /* Set Default Range */
296 ret = i2c_smbus_write_byte_data(data->client,
297 BMC150_ACCEL_REG_PMU_RANGE,
298 BMC150_ACCEL_DEF_RANGE_4G);
300 dev_err(&data->client->dev,
301 "Error writing reg_pmu_range\n");
305 data->range = BMC150_ACCEL_DEF_RANGE_4G;
307 /* Set default slope duration */
308 ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_INT_5);
310 dev_err(&data->client->dev, "Error reading reg_int_5\n");
313 data->slope_dur |= BMC150_ACCEL_DEF_SLOPE_DURATION;
314 ret = i2c_smbus_write_byte_data(data->client,
315 BMC150_ACCEL_REG_INT_5,
318 dev_err(&data->client->dev, "Error writing reg_int_5\n");
321 dev_dbg(&data->client->dev, "slope_dur %x\n", data->slope_dur);
323 /* Set default slope thresholds */
324 ret = i2c_smbus_write_byte_data(data->client,
325 BMC150_ACCEL_REG_INT_6,
326 BMC150_ACCEL_DEF_SLOPE_THRESHOLD);
328 dev_err(&data->client->dev, "Error writing reg_int_6\n");
331 data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD;
332 dev_dbg(&data->client->dev, "slope_thres %x\n", data->slope_thres);
334 /* Set default as latched interrupts */
335 ret = i2c_smbus_write_byte_data(data->client,
336 BMC150_ACCEL_REG_INT_RST_LATCH,
337 BMC150_ACCEL_INT_MODE_LATCH_INT |
338 BMC150_ACCEL_INT_MODE_LATCH_RESET);
340 dev_err(&data->client->dev,
341 "Error writing reg_int_rst_latch\n");
348 static int bmc150_accel_setup_any_motion_interrupt(
349 struct bmc150_accel_data *data,
354 /* Enable/Disable INT1 mapping */
355 ret = i2c_smbus_read_byte_data(data->client,
356 BMC150_ACCEL_REG_INT_MAP_0);
358 dev_err(&data->client->dev, "Error reading reg_int_map_0\n");
362 ret |= BMC150_ACCEL_INT_MAP_0_BIT_SLOPE;
364 ret &= ~BMC150_ACCEL_INT_MAP_0_BIT_SLOPE;
366 ret = i2c_smbus_write_byte_data(data->client,
367 BMC150_ACCEL_REG_INT_MAP_0,
370 dev_err(&data->client->dev, "Error writing reg_int_map_0\n");
375 /* Set slope duration (no of samples) */
376 ret = i2c_smbus_write_byte_data(data->client,
377 BMC150_ACCEL_REG_INT_5,
380 dev_err(&data->client->dev, "Error write reg_int_5\n");
384 /* Set slope thresholds */
385 ret = i2c_smbus_write_byte_data(data->client,
386 BMC150_ACCEL_REG_INT_6,
389 dev_err(&data->client->dev, "Error write reg_int_6\n");
394 * New data interrupt is always non-latched,
395 * which will have higher priority, so no need
396 * to set latched mode, we will be flooded anyway with INTR
398 if (!data->dready_trigger_on) {
399 ret = i2c_smbus_write_byte_data(data->client,
400 BMC150_ACCEL_REG_INT_RST_LATCH,
401 BMC150_ACCEL_INT_MODE_LATCH_INT |
402 BMC150_ACCEL_INT_MODE_LATCH_RESET);
404 dev_err(&data->client->dev,
405 "Error writing reg_int_rst_latch\n");
410 ret = i2c_smbus_write_byte_data(data->client,
411 BMC150_ACCEL_REG_INT_EN_0,
412 BMC150_ACCEL_INT_EN_BIT_SLP_X |
413 BMC150_ACCEL_INT_EN_BIT_SLP_Y |
414 BMC150_ACCEL_INT_EN_BIT_SLP_Z);
416 ret = i2c_smbus_write_byte_data(data->client,
417 BMC150_ACCEL_REG_INT_EN_0,
421 dev_err(&data->client->dev, "Error writing reg_int_en_0\n");
428 static int bmc150_accel_setup_new_data_interrupt(struct bmc150_accel_data *data,
433 /* Enable/Disable INT1 mapping */
434 ret = i2c_smbus_read_byte_data(data->client,
435 BMC150_ACCEL_REG_INT_MAP_1);
437 dev_err(&data->client->dev, "Error reading reg_int_map_1\n");
441 ret |= BMC150_ACCEL_INT_MAP_1_BIT_DATA;
443 ret &= ~BMC150_ACCEL_INT_MAP_1_BIT_DATA;
445 ret = i2c_smbus_write_byte_data(data->client,
446 BMC150_ACCEL_REG_INT_MAP_1,
449 dev_err(&data->client->dev, "Error writing reg_int_map_1\n");
455 * Set non latched mode interrupt and clear any latched
458 ret = i2c_smbus_write_byte_data(data->client,
459 BMC150_ACCEL_REG_INT_RST_LATCH,
460 BMC150_ACCEL_INT_MODE_NON_LATCH_INT |
461 BMC150_ACCEL_INT_MODE_LATCH_RESET);
463 dev_err(&data->client->dev,
464 "Error writing reg_int_rst_latch\n");
468 ret = i2c_smbus_write_byte_data(data->client,
469 BMC150_ACCEL_REG_INT_EN_1,
470 BMC150_ACCEL_INT_EN_BIT_DATA_EN);
473 /* Restore default interrupt mode */
474 ret = i2c_smbus_write_byte_data(data->client,
475 BMC150_ACCEL_REG_INT_RST_LATCH,
476 BMC150_ACCEL_INT_MODE_LATCH_INT |
477 BMC150_ACCEL_INT_MODE_LATCH_RESET);
479 dev_err(&data->client->dev,
480 "Error writing reg_int_rst_latch\n");
484 ret = i2c_smbus_write_byte_data(data->client,
485 BMC150_ACCEL_REG_INT_EN_1,
490 dev_err(&data->client->dev, "Error writing reg_int_en_1\n");
497 static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val,
502 for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
503 if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) {
504 *val = bmc150_accel_samp_freq_table[i].val;
505 *val2 = bmc150_accel_samp_freq_table[i].val2;
506 return IIO_VAL_INT_PLUS_MICRO;
513 #ifdef CONFIG_PM_RUNTIME
514 static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data)
518 for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) {
519 if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits)
520 return bmc150_accel_sample_upd_time[i].msec;
523 return BMC150_ACCEL_MAX_STARTUP_TIME_MS;
526 static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
531 ret = pm_runtime_get_sync(&data->client->dev);
533 pm_runtime_mark_last_busy(&data->client->dev);
534 ret = pm_runtime_put_autosuspend(&data->client->dev);
537 dev_err(&data->client->dev,
538 "Failed: bmc150_accel_set_power_state for %d\n", on);
540 pm_runtime_put_noidle(&data->client->dev);
548 static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
554 static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
558 for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) {
559 if (data->chip_info->scale_table[i].scale == val) {
560 ret = i2c_smbus_write_byte_data(
562 BMC150_ACCEL_REG_PMU_RANGE,
563 data->chip_info->scale_table[i].reg_range);
565 dev_err(&data->client->dev,
566 "Error writing pmu_range\n");
570 data->range = data->chip_info->scale_table[i].reg_range;
578 static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val)
582 mutex_lock(&data->mutex);
584 ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_TEMP);
586 dev_err(&data->client->dev, "Error reading reg_temp\n");
587 mutex_unlock(&data->mutex);
590 *val = sign_extend32(ret, 7);
592 mutex_unlock(&data->mutex);
597 static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
598 struct iio_chan_spec const *chan,
602 int axis = chan->scan_index;
604 mutex_lock(&data->mutex);
605 ret = bmc150_accel_set_power_state(data, true);
607 mutex_unlock(&data->mutex);
611 ret = i2c_smbus_read_word_data(data->client,
612 BMC150_ACCEL_AXIS_TO_REG(axis));
614 dev_err(&data->client->dev, "Error reading axis %d\n", axis);
615 bmc150_accel_set_power_state(data, false);
616 mutex_unlock(&data->mutex);
619 *val = sign_extend32(ret >> chan->scan_type.shift,
620 chan->scan_type.realbits - 1);
621 ret = bmc150_accel_set_power_state(data, false);
622 mutex_unlock(&data->mutex);
629 static int bmc150_accel_read_raw(struct iio_dev *indio_dev,
630 struct iio_chan_spec const *chan,
631 int *val, int *val2, long mask)
633 struct bmc150_accel_data *data = iio_priv(indio_dev);
637 case IIO_CHAN_INFO_RAW:
638 switch (chan->type) {
640 return bmc150_accel_get_temp(data, val);
642 if (iio_buffer_enabled(indio_dev))
645 return bmc150_accel_get_axis(data, chan, val);
649 case IIO_CHAN_INFO_OFFSET:
650 if (chan->type == IIO_TEMP) {
651 *val = BMC150_ACCEL_TEMP_CENTER_VAL;
655 case IIO_CHAN_INFO_SCALE:
657 switch (chan->type) {
660 return IIO_VAL_INT_PLUS_MICRO;
664 const struct bmc150_scale_info *si;
665 int st_size = ARRAY_SIZE(data->chip_info->scale_table);
667 for (i = 0; i < st_size; ++i) {
668 si = &data->chip_info->scale_table[i];
669 if (si->reg_range == data->range) {
671 return IIO_VAL_INT_PLUS_MICRO;
679 case IIO_CHAN_INFO_SAMP_FREQ:
680 mutex_lock(&data->mutex);
681 ret = bmc150_accel_get_bw(data, val, val2);
682 mutex_unlock(&data->mutex);
689 static int bmc150_accel_write_raw(struct iio_dev *indio_dev,
690 struct iio_chan_spec const *chan,
691 int val, int val2, long mask)
693 struct bmc150_accel_data *data = iio_priv(indio_dev);
697 case IIO_CHAN_INFO_SAMP_FREQ:
698 mutex_lock(&data->mutex);
699 ret = bmc150_accel_set_bw(data, val, val2);
700 mutex_unlock(&data->mutex);
702 case IIO_CHAN_INFO_SCALE:
706 mutex_lock(&data->mutex);
707 ret = bmc150_accel_set_scale(data, val2);
708 mutex_unlock(&data->mutex);
717 static int bmc150_accel_read_event(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 enum iio_event_info info,
724 struct bmc150_accel_data *data = iio_priv(indio_dev);
728 case IIO_EV_INFO_VALUE:
729 *val = data->slope_thres;
731 case IIO_EV_INFO_PERIOD:
732 *val = data->slope_dur & BMC150_ACCEL_SLOPE_DUR_MASK;
741 static int bmc150_accel_write_event(struct iio_dev *indio_dev,
742 const struct iio_chan_spec *chan,
743 enum iio_event_type type,
744 enum iio_event_direction dir,
745 enum iio_event_info info,
748 struct bmc150_accel_data *data = iio_priv(indio_dev);
750 if (data->ev_enable_state)
754 case IIO_EV_INFO_VALUE:
755 data->slope_thres = val;
757 case IIO_EV_INFO_PERIOD:
758 data->slope_dur &= ~BMC150_ACCEL_SLOPE_DUR_MASK;
759 data->slope_dur |= val & BMC150_ACCEL_SLOPE_DUR_MASK;
768 static int bmc150_accel_read_event_config(struct iio_dev *indio_dev,
769 const struct iio_chan_spec *chan,
770 enum iio_event_type type,
771 enum iio_event_direction dir)
774 struct bmc150_accel_data *data = iio_priv(indio_dev);
776 return data->ev_enable_state;
779 static int bmc150_accel_write_event_config(struct iio_dev *indio_dev,
780 const struct iio_chan_spec *chan,
781 enum iio_event_type type,
782 enum iio_event_direction dir,
785 struct bmc150_accel_data *data = iio_priv(indio_dev);
788 if (state && data->ev_enable_state)
791 mutex_lock(&data->mutex);
793 if (!state && data->motion_trigger_on) {
794 data->ev_enable_state = 0;
795 mutex_unlock(&data->mutex);
800 * We will expect the enable and disable to do operation in
801 * in reverse order. This will happen here anyway as our
802 * resume operation uses sync mode runtime pm calls, the
803 * suspend operation will be delayed by autosuspend delay
804 * So the disable operation will still happen in reverse of
805 * enable operation. When runtime pm is disabled the mode
806 * is always on so sequence doesn't matter
809 ret = bmc150_accel_set_power_state(data, state);
811 mutex_unlock(&data->mutex);
815 ret = bmc150_accel_setup_any_motion_interrupt(data, state);
817 bmc150_accel_set_power_state(data, false);
818 mutex_unlock(&data->mutex);
822 data->ev_enable_state = state;
823 mutex_unlock(&data->mutex);
828 static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev,
829 struct iio_trigger *trig)
831 struct bmc150_accel_data *data = iio_priv(indio_dev);
833 if (data->dready_trig != trig && data->motion_trig != trig)
839 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
840 "7.810000 15.630000 31.250000 62.500000 125 250 500 1000");
842 static struct attribute *bmc150_accel_attributes[] = {
843 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
847 static const struct attribute_group bmc150_accel_attrs_group = {
848 .attrs = bmc150_accel_attributes,
851 static const struct iio_event_spec bmc150_accel_event = {
852 .type = IIO_EV_TYPE_ROC,
853 .dir = IIO_EV_DIR_RISING | IIO_EV_DIR_FALLING,
854 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
855 BIT(IIO_EV_INFO_ENABLE) |
856 BIT(IIO_EV_INFO_PERIOD)
859 #define BMC150_ACCEL_CHANNEL(_axis, bits) { \
862 .channel2 = IIO_MOD_##_axis, \
863 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
864 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
865 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
866 .scan_index = AXIS_##_axis, \
869 .realbits = (bits), \
871 .shift = 16 - (bits), \
873 .event_spec = &bmc150_accel_event, \
874 .num_event_specs = 1 \
877 #define BMC150_ACCEL_CHANNELS(bits) { \
880 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
881 BIT(IIO_CHAN_INFO_SCALE) | \
882 BIT(IIO_CHAN_INFO_OFFSET), \
885 BMC150_ACCEL_CHANNEL(X, bits), \
886 BMC150_ACCEL_CHANNEL(Y, bits), \
887 BMC150_ACCEL_CHANNEL(Z, bits), \
888 IIO_CHAN_SOFT_TIMESTAMP(3), \
891 static const struct iio_chan_spec bma222e_accel_channels[] =
892 BMC150_ACCEL_CHANNELS(8);
893 static const struct iio_chan_spec bma250e_accel_channels[] =
894 BMC150_ACCEL_CHANNELS(10);
895 static const struct iio_chan_spec bmc150_accel_channels[] =
896 BMC150_ACCEL_CHANNELS(12);
897 static const struct iio_chan_spec bma280_accel_channels[] =
898 BMC150_ACCEL_CHANNELS(14);
909 static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl[] = {
912 .channels = bmc150_accel_channels,
913 .num_channels = ARRAY_SIZE(bmc150_accel_channels),
914 .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
915 {19122, BMC150_ACCEL_DEF_RANGE_4G},
916 {38344, BMC150_ACCEL_DEF_RANGE_8G},
917 {76590, BMC150_ACCEL_DEF_RANGE_16G} },
921 .channels = bmc150_accel_channels,
922 .num_channels = ARRAY_SIZE(bmc150_accel_channels),
923 .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
924 {19122, BMC150_ACCEL_DEF_RANGE_4G},
925 {38344, BMC150_ACCEL_DEF_RANGE_8G},
926 {76590, BMC150_ACCEL_DEF_RANGE_16G} },
930 .channels = bmc150_accel_channels,
931 .num_channels = ARRAY_SIZE(bmc150_accel_channels),
932 .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G},
933 {19122, BMC150_ACCEL_DEF_RANGE_4G},
934 {38344, BMC150_ACCEL_DEF_RANGE_8G},
935 {76590, BMC150_ACCEL_DEF_RANGE_16G} },
939 .channels = bma250e_accel_channels,
940 .num_channels = ARRAY_SIZE(bma250e_accel_channels),
941 .scale_table = { {38344, BMC150_ACCEL_DEF_RANGE_2G},
942 {76590, BMC150_ACCEL_DEF_RANGE_4G},
943 {153277, BMC150_ACCEL_DEF_RANGE_8G},
944 {306457, BMC150_ACCEL_DEF_RANGE_16G} },
948 .channels = bma222e_accel_channels,
949 .num_channels = ARRAY_SIZE(bma222e_accel_channels),
950 .scale_table = { {153277, BMC150_ACCEL_DEF_RANGE_2G},
951 {306457, BMC150_ACCEL_DEF_RANGE_4G},
952 {612915, BMC150_ACCEL_DEF_RANGE_8G},
953 {1225831, BMC150_ACCEL_DEF_RANGE_16G} },
957 .channels = bma280_accel_channels,
958 .num_channels = ARRAY_SIZE(bma280_accel_channels),
959 .scale_table = { {2392, BMC150_ACCEL_DEF_RANGE_2G},
960 {4785, BMC150_ACCEL_DEF_RANGE_4G},
961 {9581, BMC150_ACCEL_DEF_RANGE_8G},
962 {19152, BMC150_ACCEL_DEF_RANGE_16G} },
966 static const struct iio_info bmc150_accel_info = {
967 .attrs = &bmc150_accel_attrs_group,
968 .read_raw = bmc150_accel_read_raw,
969 .write_raw = bmc150_accel_write_raw,
970 .read_event_value = bmc150_accel_read_event,
971 .write_event_value = bmc150_accel_write_event,
972 .write_event_config = bmc150_accel_write_event_config,
973 .read_event_config = bmc150_accel_read_event_config,
974 .validate_trigger = bmc150_accel_validate_trigger,
975 .driver_module = THIS_MODULE,
978 static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
980 struct iio_poll_func *pf = p;
981 struct iio_dev *indio_dev = pf->indio_dev;
982 struct bmc150_accel_data *data = iio_priv(indio_dev);
985 mutex_lock(&data->mutex);
986 for_each_set_bit(bit, indio_dev->buffer->scan_mask,
987 indio_dev->masklength) {
988 ret = i2c_smbus_read_word_data(data->client,
989 BMC150_ACCEL_AXIS_TO_REG(bit));
991 mutex_unlock(&data->mutex);
994 data->buffer[i++] = ret;
996 mutex_unlock(&data->mutex);
998 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
1001 iio_trigger_notify_done(indio_dev->trig);
1006 static int bmc150_accel_trig_try_reen(struct iio_trigger *trig)
1008 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1009 struct bmc150_accel_data *data = iio_priv(indio_dev);
1012 /* new data interrupts don't need ack */
1013 if (data->dready_trigger_on)
1016 mutex_lock(&data->mutex);
1017 /* clear any latched interrupt */
1018 ret = i2c_smbus_write_byte_data(data->client,
1019 BMC150_ACCEL_REG_INT_RST_LATCH,
1020 BMC150_ACCEL_INT_MODE_LATCH_INT |
1021 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1022 mutex_unlock(&data->mutex);
1024 dev_err(&data->client->dev,
1025 "Error writing reg_int_rst_latch\n");
1032 static int bmc150_accel_data_rdy_trigger_set_state(struct iio_trigger *trig,
1035 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1036 struct bmc150_accel_data *data = iio_priv(indio_dev);
1039 mutex_lock(&data->mutex);
1041 if (!state && data->ev_enable_state && data->motion_trigger_on) {
1042 data->motion_trigger_on = false;
1043 mutex_unlock(&data->mutex);
1048 * Refer to comment in bmc150_accel_write_event_config for
1049 * enable/disable operation order
1051 ret = bmc150_accel_set_power_state(data, state);
1053 mutex_unlock(&data->mutex);
1056 if (data->motion_trig == trig)
1057 ret = bmc150_accel_setup_any_motion_interrupt(data, state);
1059 ret = bmc150_accel_setup_new_data_interrupt(data, state);
1061 bmc150_accel_set_power_state(data, false);
1062 mutex_unlock(&data->mutex);
1065 if (data->motion_trig == trig)
1066 data->motion_trigger_on = state;
1068 data->dready_trigger_on = state;
1070 mutex_unlock(&data->mutex);
1075 static const struct iio_trigger_ops bmc150_accel_trigger_ops = {
1076 .set_trigger_state = bmc150_accel_data_rdy_trigger_set_state,
1077 .try_reenable = bmc150_accel_trig_try_reen,
1078 .owner = THIS_MODULE,
1081 static irqreturn_t bmc150_accel_event_handler(int irq, void *private)
1083 struct iio_dev *indio_dev = private;
1084 struct bmc150_accel_data *data = iio_priv(indio_dev);
1088 ret = i2c_smbus_read_byte_data(data->client,
1089 BMC150_ACCEL_REG_INT_STATUS_2);
1091 dev_err(&data->client->dev, "Error reading reg_int_status_2\n");
1092 goto ack_intr_status;
1095 if (ret & BMC150_ACCEL_ANY_MOTION_BIT_SIGN)
1096 dir = IIO_EV_DIR_FALLING;
1098 dir = IIO_EV_DIR_RISING;
1100 if (ret & BMC150_ACCEL_ANY_MOTION_MASK)
1101 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ACCEL,
1103 IIO_MOD_X_OR_Y_OR_Z,
1108 if (!data->dready_trigger_on)
1109 ret = i2c_smbus_write_byte_data(data->client,
1110 BMC150_ACCEL_REG_INT_RST_LATCH,
1111 BMC150_ACCEL_INT_MODE_LATCH_INT |
1112 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1117 static irqreturn_t bmc150_accel_data_rdy_trig_poll(int irq, void *private)
1119 struct iio_dev *indio_dev = private;
1120 struct bmc150_accel_data *data = iio_priv(indio_dev);
1122 data->timestamp = iio_get_time_ns();
1124 if (data->dready_trigger_on)
1125 iio_trigger_poll(data->dready_trig);
1126 else if (data->motion_trigger_on)
1127 iio_trigger_poll(data->motion_trig);
1129 if (data->ev_enable_state)
1130 return IRQ_WAKE_THREAD;
1135 static const char *bmc150_accel_match_acpi_device(struct device *dev, int *data)
1137 const struct acpi_device_id *id;
1139 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1144 *data = (int) id->driver_data;
1146 return dev_name(dev);
1149 static int bmc150_accel_gpio_probe(struct i2c_client *client,
1150 struct bmc150_accel_data *data)
1153 struct gpio_desc *gpio;
1161 /* data ready gpio interrupt pin */
1162 gpio = devm_gpiod_get_index(dev, BMC150_ACCEL_GPIO_NAME, 0);
1164 dev_err(dev, "Failed: gpio get index\n");
1165 return PTR_ERR(gpio);
1168 ret = gpiod_direction_input(gpio);
1172 ret = gpiod_to_irq(gpio);
1174 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
1179 static int bmc150_accel_probe(struct i2c_client *client,
1180 const struct i2c_device_id *id)
1182 struct bmc150_accel_data *data;
1183 struct iio_dev *indio_dev;
1185 const char *name = NULL;
1188 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1192 data = iio_priv(indio_dev);
1193 i2c_set_clientdata(client, indio_dev);
1194 data->client = client;
1198 chip_id = id->driver_data;
1201 if (ACPI_HANDLE(&client->dev))
1202 name = bmc150_accel_match_acpi_device(&client->dev, &chip_id);
1204 data->chip_info = &bmc150_accel_chip_info_tbl[chip_id];
1206 ret = bmc150_accel_chip_init(data);
1210 mutex_init(&data->mutex);
1212 indio_dev->dev.parent = &client->dev;
1213 indio_dev->channels = data->chip_info->channels;
1214 indio_dev->num_channels = data->chip_info->num_channels;
1215 indio_dev->name = name;
1216 indio_dev->modes = INDIO_DIRECT_MODE;
1217 indio_dev->info = &bmc150_accel_info;
1219 if (client->irq < 0)
1220 client->irq = bmc150_accel_gpio_probe(client, data);
1222 if (client->irq >= 0) {
1223 ret = devm_request_threaded_irq(
1224 &client->dev, client->irq,
1225 bmc150_accel_data_rdy_trig_poll,
1226 bmc150_accel_event_handler,
1227 IRQF_TRIGGER_RISING,
1228 BMC150_ACCEL_IRQ_NAME,
1233 data->dready_trig = devm_iio_trigger_alloc(&client->dev,
1237 if (!data->dready_trig)
1240 data->motion_trig = devm_iio_trigger_alloc(&client->dev,
1241 "%s-any-motion-dev%d",
1244 if (!data->motion_trig)
1247 data->dready_trig->dev.parent = &client->dev;
1248 data->dready_trig->ops = &bmc150_accel_trigger_ops;
1249 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
1250 ret = iio_trigger_register(data->dready_trig);
1254 data->motion_trig->dev.parent = &client->dev;
1255 data->motion_trig->ops = &bmc150_accel_trigger_ops;
1256 iio_trigger_set_drvdata(data->motion_trig, indio_dev);
1257 ret = iio_trigger_register(data->motion_trig);
1259 data->motion_trig = NULL;
1260 goto err_trigger_unregister;
1263 ret = iio_triggered_buffer_setup(indio_dev,
1264 &iio_pollfunc_store_time,
1265 bmc150_accel_trigger_handler,
1268 dev_err(&client->dev,
1269 "Failed: iio triggered buffer setup\n");
1270 goto err_trigger_unregister;
1274 ret = iio_device_register(indio_dev);
1276 dev_err(&client->dev, "Unable to register iio device\n");
1277 goto err_buffer_cleanup;
1280 ret = pm_runtime_set_active(&client->dev);
1282 goto err_iio_unregister;
1284 pm_runtime_enable(&client->dev);
1285 pm_runtime_set_autosuspend_delay(&client->dev,
1286 BMC150_AUTO_SUSPEND_DELAY_MS);
1287 pm_runtime_use_autosuspend(&client->dev);
1292 iio_device_unregister(indio_dev);
1294 if (data->dready_trig)
1295 iio_triggered_buffer_cleanup(indio_dev);
1296 err_trigger_unregister:
1297 if (data->dready_trig)
1298 iio_trigger_unregister(data->dready_trig);
1299 if (data->motion_trig)
1300 iio_trigger_unregister(data->motion_trig);
1305 static int bmc150_accel_remove(struct i2c_client *client)
1307 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1308 struct bmc150_accel_data *data = iio_priv(indio_dev);
1310 pm_runtime_disable(&client->dev);
1311 pm_runtime_set_suspended(&client->dev);
1312 pm_runtime_put_noidle(&client->dev);
1314 iio_device_unregister(indio_dev);
1316 if (data->dready_trig) {
1317 iio_triggered_buffer_cleanup(indio_dev);
1318 iio_trigger_unregister(data->dready_trig);
1319 iio_trigger_unregister(data->motion_trig);
1322 mutex_lock(&data->mutex);
1323 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0);
1324 mutex_unlock(&data->mutex);
1329 #ifdef CONFIG_PM_SLEEP
1330 static int bmc150_accel_suspend(struct device *dev)
1332 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1333 struct bmc150_accel_data *data = iio_priv(indio_dev);
1335 mutex_lock(&data->mutex);
1336 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1337 mutex_unlock(&data->mutex);
1342 static int bmc150_accel_resume(struct device *dev)
1344 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1345 struct bmc150_accel_data *data = iio_priv(indio_dev);
1347 mutex_lock(&data->mutex);
1348 if (data->dready_trigger_on || data->motion_trigger_on ||
1349 data->ev_enable_state)
1350 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1351 mutex_unlock(&data->mutex);
1357 #ifdef CONFIG_PM_RUNTIME
1358 static int bmc150_accel_runtime_suspend(struct device *dev)
1360 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1361 struct bmc150_accel_data *data = iio_priv(indio_dev);
1364 dev_dbg(&data->client->dev, __func__);
1365 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1372 static int bmc150_accel_runtime_resume(struct device *dev)
1374 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1375 struct bmc150_accel_data *data = iio_priv(indio_dev);
1379 dev_dbg(&data->client->dev, __func__);
1381 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1385 sleep_val = bmc150_accel_get_startup_times(data);
1387 usleep_range(sleep_val * 1000, 20000);
1389 msleep_interruptible(sleep_val);
1395 static const struct dev_pm_ops bmc150_accel_pm_ops = {
1396 SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend, bmc150_accel_resume)
1397 SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend,
1398 bmc150_accel_runtime_resume, NULL)
1401 static const struct acpi_device_id bmc150_accel_acpi_match[] = {
1402 {"BSBA0150", bmc150},
1403 {"BMC150A", bmc150},
1404 {"BMI055A", bmi055},
1405 {"BMA0255", bma255},
1406 {"BMA250E", bma250e},
1407 {"BMA222E", bma222e},
1408 {"BMA0280", bma280},
1411 MODULE_DEVICE_TABLE(acpi, bmc150_accel_acpi_match);
1413 static const struct i2c_device_id bmc150_accel_id[] = {
1414 {"bmc150_accel", bmc150},
1415 {"bmi055_accel", bmi055},
1417 {"bma250e", bma250e},
1418 {"bma222e", bma222e},
1423 MODULE_DEVICE_TABLE(i2c, bmc150_accel_id);
1425 static struct i2c_driver bmc150_accel_driver = {
1427 .name = BMC150_ACCEL_DRV_NAME,
1428 .acpi_match_table = ACPI_PTR(bmc150_accel_acpi_match),
1429 .pm = &bmc150_accel_pm_ops,
1431 .probe = bmc150_accel_probe,
1432 .remove = bmc150_accel_remove,
1433 .id_table = bmc150_accel_id,
1435 module_i2c_driver(bmc150_accel_driver);
1437 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1438 MODULE_LICENSE("GPL v2");
1439 MODULE_DESCRIPTION("BMC150 accelerometer driver");