ARM: dts: add 'dr_mode' property to hsotg devices for exynos boards
[linux/fpc-iii.git] / drivers / iio / gyro / bmg160.c
blob60451b32824212a039b8ac9358751faa1af12819
1 /*
2 * BMG160 Gyro Sensor driver
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/acpi.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/pm.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/events.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
32 #define BMG160_DRV_NAME "bmg160"
33 #define BMG160_IRQ_NAME "bmg160_event"
34 #define BMG160_GPIO_NAME "gpio_int"
36 #define BMG160_REG_CHIP_ID 0x00
37 #define BMG160_CHIP_ID_VAL 0x0F
39 #define BMG160_REG_PMU_LPW 0x11
40 #define BMG160_MODE_NORMAL 0x00
41 #define BMG160_MODE_DEEP_SUSPEND 0x20
42 #define BMG160_MODE_SUSPEND 0x80
44 #define BMG160_REG_RANGE 0x0F
46 #define BMG160_RANGE_2000DPS 0
47 #define BMG160_RANGE_1000DPS 1
48 #define BMG160_RANGE_500DPS 2
49 #define BMG160_RANGE_250DPS 3
50 #define BMG160_RANGE_125DPS 4
52 #define BMG160_REG_PMU_BW 0x10
53 #define BMG160_NO_FILTER 0
54 #define BMG160_DEF_BW 100
56 #define BMG160_REG_INT_MAP_0 0x17
57 #define BMG160_INT_MAP_0_BIT_ANY BIT(1)
59 #define BMG160_REG_INT_MAP_1 0x18
60 #define BMG160_INT_MAP_1_BIT_NEW_DATA BIT(0)
62 #define BMG160_REG_INT_RST_LATCH 0x21
63 #define BMG160_INT_MODE_LATCH_RESET 0x80
64 #define BMG160_INT_MODE_LATCH_INT 0x0F
65 #define BMG160_INT_MODE_NON_LATCH_INT 0x00
67 #define BMG160_REG_INT_EN_0 0x15
68 #define BMG160_DATA_ENABLE_INT BIT(7)
70 #define BMG160_REG_INT_EN_1 0x16
71 #define BMG160_INT1_BIT_OD BIT(1)
73 #define BMG160_REG_XOUT_L 0x02
74 #define BMG160_AXIS_TO_REG(axis) (BMG160_REG_XOUT_L + (axis * 2))
76 #define BMG160_REG_SLOPE_THRES 0x1B
77 #define BMG160_SLOPE_THRES_MASK 0x0F
79 #define BMG160_REG_MOTION_INTR 0x1C
80 #define BMG160_INT_MOTION_X BIT(0)
81 #define BMG160_INT_MOTION_Y BIT(1)
82 #define BMG160_INT_MOTION_Z BIT(2)
83 #define BMG160_ANY_DUR_MASK 0x30
84 #define BMG160_ANY_DUR_SHIFT 4
86 #define BMG160_REG_INT_STATUS_2 0x0B
87 #define BMG160_ANY_MOTION_MASK 0x07
88 #define BMG160_ANY_MOTION_BIT_X BIT(0)
89 #define BMG160_ANY_MOTION_BIT_Y BIT(1)
90 #define BMG160_ANY_MOTION_BIT_Z BIT(2)
92 #define BMG160_REG_TEMP 0x08
93 #define BMG160_TEMP_CENTER_VAL 23
95 #define BMG160_MAX_STARTUP_TIME_MS 80
97 #define BMG160_AUTO_SUSPEND_DELAY_MS 2000
99 struct bmg160_data {
100 struct i2c_client *client;
101 struct iio_trigger *dready_trig;
102 struct iio_trigger *motion_trig;
103 struct mutex mutex;
104 s16 buffer[8];
105 u8 bw_bits;
106 u32 dps_range;
107 int ev_enable_state;
108 int slope_thres;
109 bool dready_trigger_on;
110 bool motion_trigger_on;
111 int64_t timestamp;
114 enum bmg160_axis {
115 AXIS_X,
116 AXIS_Y,
117 AXIS_Z,
120 static const struct {
121 int val;
122 int bw_bits;
123 } bmg160_samp_freq_table[] = { {100, 0x07},
124 {200, 0x06},
125 {400, 0x03},
126 {1000, 0x02},
127 {2000, 0x01} };
129 static const struct {
130 int scale;
131 int dps_range;
132 } bmg160_scale_table[] = { { 1065, BMG160_RANGE_2000DPS},
133 { 532, BMG160_RANGE_1000DPS},
134 { 266, BMG160_RANGE_500DPS},
135 { 133, BMG160_RANGE_250DPS},
136 { 66, BMG160_RANGE_125DPS} };
138 static int bmg160_set_mode(struct bmg160_data *data, u8 mode)
140 int ret;
142 ret = i2c_smbus_write_byte_data(data->client,
143 BMG160_REG_PMU_LPW, mode);
144 if (ret < 0) {
145 dev_err(&data->client->dev, "Error writing reg_pmu_lpw\n");
146 return ret;
149 return 0;
152 static int bmg160_convert_freq_to_bit(int val)
154 int i;
156 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
157 if (bmg160_samp_freq_table[i].val == val)
158 return bmg160_samp_freq_table[i].bw_bits;
161 return -EINVAL;
164 static int bmg160_set_bw(struct bmg160_data *data, int val)
166 int ret;
167 int bw_bits;
169 bw_bits = bmg160_convert_freq_to_bit(val);
170 if (bw_bits < 0)
171 return bw_bits;
173 ret = i2c_smbus_write_byte_data(data->client, BMG160_REG_PMU_BW,
174 bw_bits);
175 if (ret < 0) {
176 dev_err(&data->client->dev, "Error writing reg_pmu_bw\n");
177 return ret;
180 data->bw_bits = bw_bits;
182 return 0;
185 static int bmg160_chip_init(struct bmg160_data *data)
187 int ret;
189 ret = i2c_smbus_read_byte_data(data->client, BMG160_REG_CHIP_ID);
190 if (ret < 0) {
191 dev_err(&data->client->dev, "Error reading reg_chip_id\n");
192 return ret;
195 dev_dbg(&data->client->dev, "Chip Id %x\n", ret);
196 if (ret != BMG160_CHIP_ID_VAL) {
197 dev_err(&data->client->dev, "invalid chip %x\n", ret);
198 return -ENODEV;
201 ret = bmg160_set_mode(data, BMG160_MODE_NORMAL);
202 if (ret < 0)
203 return ret;
205 /* Wait upto 500 ms to be ready after changing mode */
206 usleep_range(500, 1000);
208 /* Set Bandwidth */
209 ret = bmg160_set_bw(data, BMG160_DEF_BW);
210 if (ret < 0)
211 return ret;
213 /* Set Default Range */
214 ret = i2c_smbus_write_byte_data(data->client,
215 BMG160_REG_RANGE,
216 BMG160_RANGE_500DPS);
217 if (ret < 0) {
218 dev_err(&data->client->dev, "Error writing reg_range\n");
219 return ret;
221 data->dps_range = BMG160_RANGE_500DPS;
223 ret = i2c_smbus_read_byte_data(data->client, BMG160_REG_SLOPE_THRES);
224 if (ret < 0) {
225 dev_err(&data->client->dev, "Error reading reg_slope_thres\n");
226 return ret;
228 data->slope_thres = ret;
230 /* Set default interrupt mode */
231 ret = i2c_smbus_read_byte_data(data->client, BMG160_REG_INT_EN_1);
232 if (ret < 0) {
233 dev_err(&data->client->dev, "Error reading reg_int_en_1\n");
234 return ret;
236 ret &= ~BMG160_INT1_BIT_OD;
237 ret = i2c_smbus_write_byte_data(data->client,
238 BMG160_REG_INT_EN_1, ret);
239 if (ret < 0) {
240 dev_err(&data->client->dev, "Error writing reg_int_en_1\n");
241 return ret;
244 ret = i2c_smbus_write_byte_data(data->client,
245 BMG160_REG_INT_RST_LATCH,
246 BMG160_INT_MODE_LATCH_INT |
247 BMG160_INT_MODE_LATCH_RESET);
248 if (ret < 0) {
249 dev_err(&data->client->dev,
250 "Error writing reg_motion_intr\n");
251 return ret;
254 return 0;
257 static int bmg160_set_power_state(struct bmg160_data *data, bool on)
259 #ifdef CONFIG_PM
260 int ret;
262 if (on)
263 ret = pm_runtime_get_sync(&data->client->dev);
264 else {
265 pm_runtime_mark_last_busy(&data->client->dev);
266 ret = pm_runtime_put_autosuspend(&data->client->dev);
269 if (ret < 0) {
270 dev_err(&data->client->dev,
271 "Failed: bmg160_set_power_state for %d\n", on);
272 if (on)
273 pm_runtime_put_noidle(&data->client->dev);
275 return ret;
277 #endif
279 return 0;
282 static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data,
283 bool status)
285 int ret;
287 /* Enable/Disable INT_MAP0 mapping */
288 ret = i2c_smbus_read_byte_data(data->client, BMG160_REG_INT_MAP_0);
289 if (ret < 0) {
290 dev_err(&data->client->dev, "Error reading reg_int_map0\n");
291 return ret;
293 if (status)
294 ret |= BMG160_INT_MAP_0_BIT_ANY;
295 else
296 ret &= ~BMG160_INT_MAP_0_BIT_ANY;
298 ret = i2c_smbus_write_byte_data(data->client,
299 BMG160_REG_INT_MAP_0,
300 ret);
301 if (ret < 0) {
302 dev_err(&data->client->dev, "Error writing reg_int_map0\n");
303 return ret;
306 /* Enable/Disable slope interrupts */
307 if (status) {
308 /* Update slope thres */
309 ret = i2c_smbus_write_byte_data(data->client,
310 BMG160_REG_SLOPE_THRES,
311 data->slope_thres);
312 if (ret < 0) {
313 dev_err(&data->client->dev,
314 "Error writing reg_slope_thres\n");
315 return ret;
318 ret = i2c_smbus_write_byte_data(data->client,
319 BMG160_REG_MOTION_INTR,
320 BMG160_INT_MOTION_X |
321 BMG160_INT_MOTION_Y |
322 BMG160_INT_MOTION_Z);
323 if (ret < 0) {
324 dev_err(&data->client->dev,
325 "Error writing reg_motion_intr\n");
326 return ret;
330 * New data interrupt is always non-latched,
331 * which will have higher priority, so no need
332 * to set latched mode, we will be flooded anyway with INTR
334 if (!data->dready_trigger_on) {
335 ret = i2c_smbus_write_byte_data(data->client,
336 BMG160_REG_INT_RST_LATCH,
337 BMG160_INT_MODE_LATCH_INT |
338 BMG160_INT_MODE_LATCH_RESET);
339 if (ret < 0) {
340 dev_err(&data->client->dev,
341 "Error writing reg_rst_latch\n");
342 return ret;
346 ret = i2c_smbus_write_byte_data(data->client,
347 BMG160_REG_INT_EN_0,
348 BMG160_DATA_ENABLE_INT);
350 } else
351 ret = i2c_smbus_write_byte_data(data->client,
352 BMG160_REG_INT_EN_0,
355 if (ret < 0) {
356 dev_err(&data->client->dev, "Error writing reg_int_en0\n");
357 return ret;
360 return 0;
363 static int bmg160_setup_new_data_interrupt(struct bmg160_data *data,
364 bool status)
366 int ret;
368 /* Enable/Disable INT_MAP1 mapping */
369 ret = i2c_smbus_read_byte_data(data->client, BMG160_REG_INT_MAP_1);
370 if (ret < 0) {
371 dev_err(&data->client->dev, "Error reading reg_int_map1\n");
372 return ret;
375 if (status)
376 ret |= BMG160_INT_MAP_1_BIT_NEW_DATA;
377 else
378 ret &= ~BMG160_INT_MAP_1_BIT_NEW_DATA;
380 ret = i2c_smbus_write_byte_data(data->client,
381 BMG160_REG_INT_MAP_1,
382 ret);
383 if (ret < 0) {
384 dev_err(&data->client->dev, "Error writing reg_int_map1\n");
385 return ret;
388 if (status) {
389 ret = i2c_smbus_write_byte_data(data->client,
390 BMG160_REG_INT_RST_LATCH,
391 BMG160_INT_MODE_NON_LATCH_INT |
392 BMG160_INT_MODE_LATCH_RESET);
393 if (ret < 0) {
394 dev_err(&data->client->dev,
395 "Error writing reg_rst_latch\n");
396 return ret;
399 ret = i2c_smbus_write_byte_data(data->client,
400 BMG160_REG_INT_EN_0,
401 BMG160_DATA_ENABLE_INT);
403 } else {
404 /* Restore interrupt mode */
405 ret = i2c_smbus_write_byte_data(data->client,
406 BMG160_REG_INT_RST_LATCH,
407 BMG160_INT_MODE_LATCH_INT |
408 BMG160_INT_MODE_LATCH_RESET);
409 if (ret < 0) {
410 dev_err(&data->client->dev,
411 "Error writing reg_rst_latch\n");
412 return ret;
415 ret = i2c_smbus_write_byte_data(data->client,
416 BMG160_REG_INT_EN_0,
420 if (ret < 0) {
421 dev_err(&data->client->dev, "Error writing reg_int_en0\n");
422 return ret;
425 return 0;
428 static int bmg160_get_bw(struct bmg160_data *data, int *val)
430 int i;
432 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
433 if (bmg160_samp_freq_table[i].bw_bits == data->bw_bits) {
434 *val = bmg160_samp_freq_table[i].val;
435 return IIO_VAL_INT;
439 return -EINVAL;
442 static int bmg160_set_scale(struct bmg160_data *data, int val)
444 int ret, i;
446 for (i = 0; i < ARRAY_SIZE(bmg160_scale_table); ++i) {
447 if (bmg160_scale_table[i].scale == val) {
448 ret = i2c_smbus_write_byte_data(
449 data->client,
450 BMG160_REG_RANGE,
451 bmg160_scale_table[i].dps_range);
452 if (ret < 0) {
453 dev_err(&data->client->dev,
454 "Error writing reg_range\n");
455 return ret;
457 data->dps_range = bmg160_scale_table[i].dps_range;
458 return 0;
462 return -EINVAL;
465 static int bmg160_get_temp(struct bmg160_data *data, int *val)
467 int ret;
469 mutex_lock(&data->mutex);
470 ret = bmg160_set_power_state(data, true);
471 if (ret < 0) {
472 mutex_unlock(&data->mutex);
473 return ret;
476 ret = i2c_smbus_read_byte_data(data->client, BMG160_REG_TEMP);
477 if (ret < 0) {
478 dev_err(&data->client->dev, "Error reading reg_temp\n");
479 bmg160_set_power_state(data, false);
480 mutex_unlock(&data->mutex);
481 return ret;
484 *val = sign_extend32(ret, 7);
485 ret = bmg160_set_power_state(data, false);
486 mutex_unlock(&data->mutex);
487 if (ret < 0)
488 return ret;
490 return IIO_VAL_INT;
493 static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val)
495 int ret;
497 mutex_lock(&data->mutex);
498 ret = bmg160_set_power_state(data, true);
499 if (ret < 0) {
500 mutex_unlock(&data->mutex);
501 return ret;
504 ret = i2c_smbus_read_word_data(data->client, BMG160_AXIS_TO_REG(axis));
505 if (ret < 0) {
506 dev_err(&data->client->dev, "Error reading axis %d\n", axis);
507 bmg160_set_power_state(data, false);
508 mutex_unlock(&data->mutex);
509 return ret;
512 *val = sign_extend32(ret, 15);
513 ret = bmg160_set_power_state(data, false);
514 mutex_unlock(&data->mutex);
515 if (ret < 0)
516 return ret;
518 return IIO_VAL_INT;
521 static int bmg160_read_raw(struct iio_dev *indio_dev,
522 struct iio_chan_spec const *chan,
523 int *val, int *val2, long mask)
525 struct bmg160_data *data = iio_priv(indio_dev);
526 int ret;
528 switch (mask) {
529 case IIO_CHAN_INFO_RAW:
530 switch (chan->type) {
531 case IIO_TEMP:
532 return bmg160_get_temp(data, val);
533 case IIO_ANGL_VEL:
534 if (iio_buffer_enabled(indio_dev))
535 return -EBUSY;
536 else
537 return bmg160_get_axis(data, chan->scan_index,
538 val);
539 default:
540 return -EINVAL;
542 case IIO_CHAN_INFO_OFFSET:
543 if (chan->type == IIO_TEMP) {
544 *val = BMG160_TEMP_CENTER_VAL;
545 return IIO_VAL_INT;
546 } else
547 return -EINVAL;
548 case IIO_CHAN_INFO_SCALE:
549 *val = 0;
550 switch (chan->type) {
551 case IIO_TEMP:
552 *val2 = 500000;
553 return IIO_VAL_INT_PLUS_MICRO;
554 case IIO_ANGL_VEL:
556 int i;
558 for (i = 0; i < ARRAY_SIZE(bmg160_scale_table); ++i) {
559 if (bmg160_scale_table[i].dps_range ==
560 data->dps_range) {
561 *val2 = bmg160_scale_table[i].scale;
562 return IIO_VAL_INT_PLUS_MICRO;
565 return -EINVAL;
567 default:
568 return -EINVAL;
570 case IIO_CHAN_INFO_SAMP_FREQ:
571 *val2 = 0;
572 mutex_lock(&data->mutex);
573 ret = bmg160_get_bw(data, val);
574 mutex_unlock(&data->mutex);
575 return ret;
576 default:
577 return -EINVAL;
581 static int bmg160_write_raw(struct iio_dev *indio_dev,
582 struct iio_chan_spec const *chan,
583 int val, int val2, long mask)
585 struct bmg160_data *data = iio_priv(indio_dev);
586 int ret;
588 switch (mask) {
589 case IIO_CHAN_INFO_SAMP_FREQ:
590 mutex_lock(&data->mutex);
592 * Section 4.2 of spec
593 * In suspend mode, the only supported operations are reading
594 * registers as well as writing to the (0x14) softreset
595 * register. Since we will be in suspend mode by default, change
596 * mode to power on for other writes.
598 ret = bmg160_set_power_state(data, true);
599 if (ret < 0) {
600 mutex_unlock(&data->mutex);
601 return ret;
603 ret = bmg160_set_bw(data, val);
604 if (ret < 0) {
605 bmg160_set_power_state(data, false);
606 mutex_unlock(&data->mutex);
607 return ret;
609 ret = bmg160_set_power_state(data, false);
610 mutex_unlock(&data->mutex);
611 return ret;
612 case IIO_CHAN_INFO_SCALE:
613 if (val)
614 return -EINVAL;
616 mutex_lock(&data->mutex);
617 /* Refer to comments above for the suspend mode ops */
618 ret = bmg160_set_power_state(data, true);
619 if (ret < 0) {
620 mutex_unlock(&data->mutex);
621 return ret;
623 ret = bmg160_set_scale(data, val2);
624 if (ret < 0) {
625 bmg160_set_power_state(data, false);
626 mutex_unlock(&data->mutex);
627 return ret;
629 ret = bmg160_set_power_state(data, false);
630 mutex_unlock(&data->mutex);
631 return ret;
632 default:
633 return -EINVAL;
636 return -EINVAL;
639 static int bmg160_read_event(struct iio_dev *indio_dev,
640 const struct iio_chan_spec *chan,
641 enum iio_event_type type,
642 enum iio_event_direction dir,
643 enum iio_event_info info,
644 int *val, int *val2)
646 struct bmg160_data *data = iio_priv(indio_dev);
648 *val2 = 0;
649 switch (info) {
650 case IIO_EV_INFO_VALUE:
651 *val = data->slope_thres & BMG160_SLOPE_THRES_MASK;
652 break;
653 default:
654 return -EINVAL;
657 return IIO_VAL_INT;
660 static int bmg160_write_event(struct iio_dev *indio_dev,
661 const struct iio_chan_spec *chan,
662 enum iio_event_type type,
663 enum iio_event_direction dir,
664 enum iio_event_info info,
665 int val, int val2)
667 struct bmg160_data *data = iio_priv(indio_dev);
669 switch (info) {
670 case IIO_EV_INFO_VALUE:
671 if (data->ev_enable_state)
672 return -EBUSY;
673 data->slope_thres &= ~BMG160_SLOPE_THRES_MASK;
674 data->slope_thres |= (val & BMG160_SLOPE_THRES_MASK);
675 break;
676 default:
677 return -EINVAL;
680 return 0;
683 static int bmg160_read_event_config(struct iio_dev *indio_dev,
684 const struct iio_chan_spec *chan,
685 enum iio_event_type type,
686 enum iio_event_direction dir)
689 struct bmg160_data *data = iio_priv(indio_dev);
691 return data->ev_enable_state;
694 static int bmg160_write_event_config(struct iio_dev *indio_dev,
695 const struct iio_chan_spec *chan,
696 enum iio_event_type type,
697 enum iio_event_direction dir,
698 int state)
700 struct bmg160_data *data = iio_priv(indio_dev);
701 int ret;
703 if (state && data->ev_enable_state)
704 return 0;
706 mutex_lock(&data->mutex);
708 if (!state && data->motion_trigger_on) {
709 data->ev_enable_state = 0;
710 mutex_unlock(&data->mutex);
711 return 0;
714 * We will expect the enable and disable to do operation in
715 * in reverse order. This will happen here anyway as our
716 * resume operation uses sync mode runtime pm calls, the
717 * suspend operation will be delayed by autosuspend delay
718 * So the disable operation will still happen in reverse of
719 * enable operation. When runtime pm is disabled the mode
720 * is always on so sequence doesn't matter
722 ret = bmg160_set_power_state(data, state);
723 if (ret < 0) {
724 mutex_unlock(&data->mutex);
725 return ret;
728 ret = bmg160_setup_any_motion_interrupt(data, state);
729 if (ret < 0) {
730 bmg160_set_power_state(data, false);
731 mutex_unlock(&data->mutex);
732 return ret;
735 data->ev_enable_state = state;
736 mutex_unlock(&data->mutex);
738 return 0;
741 static int bmg160_validate_trigger(struct iio_dev *indio_dev,
742 struct iio_trigger *trig)
744 struct bmg160_data *data = iio_priv(indio_dev);
746 if (data->dready_trig != trig && data->motion_trig != trig)
747 return -EINVAL;
749 return 0;
752 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 400 1000 2000");
754 static IIO_CONST_ATTR(in_anglvel_scale_available,
755 "0.001065 0.000532 0.000266 0.000133 0.000066");
757 static struct attribute *bmg160_attributes[] = {
758 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
759 &iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
760 NULL,
763 static const struct attribute_group bmg160_attrs_group = {
764 .attrs = bmg160_attributes,
767 static const struct iio_event_spec bmg160_event = {
768 .type = IIO_EV_TYPE_ROC,
769 .dir = IIO_EV_DIR_EITHER,
770 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
771 BIT(IIO_EV_INFO_ENABLE)
774 #define BMG160_CHANNEL(_axis) { \
775 .type = IIO_ANGL_VEL, \
776 .modified = 1, \
777 .channel2 = IIO_MOD_##_axis, \
778 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
779 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
780 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
781 .scan_index = AXIS_##_axis, \
782 .scan_type = { \
783 .sign = 's', \
784 .realbits = 16, \
785 .storagebits = 16, \
786 }, \
787 .event_spec = &bmg160_event, \
788 .num_event_specs = 1 \
791 static const struct iio_chan_spec bmg160_channels[] = {
793 .type = IIO_TEMP,
794 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
795 BIT(IIO_CHAN_INFO_SCALE) |
796 BIT(IIO_CHAN_INFO_OFFSET),
797 .scan_index = -1,
799 BMG160_CHANNEL(X),
800 BMG160_CHANNEL(Y),
801 BMG160_CHANNEL(Z),
802 IIO_CHAN_SOFT_TIMESTAMP(3),
805 static const struct iio_info bmg160_info = {
806 .attrs = &bmg160_attrs_group,
807 .read_raw = bmg160_read_raw,
808 .write_raw = bmg160_write_raw,
809 .read_event_value = bmg160_read_event,
810 .write_event_value = bmg160_write_event,
811 .write_event_config = bmg160_write_event_config,
812 .read_event_config = bmg160_read_event_config,
813 .validate_trigger = bmg160_validate_trigger,
814 .driver_module = THIS_MODULE,
817 static irqreturn_t bmg160_trigger_handler(int irq, void *p)
819 struct iio_poll_func *pf = p;
820 struct iio_dev *indio_dev = pf->indio_dev;
821 struct bmg160_data *data = iio_priv(indio_dev);
822 int bit, ret, i = 0;
824 mutex_lock(&data->mutex);
825 for_each_set_bit(bit, indio_dev->buffer->scan_mask,
826 indio_dev->masklength) {
827 ret = i2c_smbus_read_word_data(data->client,
828 BMG160_AXIS_TO_REG(bit));
829 if (ret < 0) {
830 mutex_unlock(&data->mutex);
831 goto err;
833 data->buffer[i++] = ret;
835 mutex_unlock(&data->mutex);
837 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
838 data->timestamp);
839 err:
840 iio_trigger_notify_done(indio_dev->trig);
842 return IRQ_HANDLED;
845 static int bmg160_trig_try_reen(struct iio_trigger *trig)
847 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
848 struct bmg160_data *data = iio_priv(indio_dev);
849 int ret;
851 /* new data interrupts don't need ack */
852 if (data->dready_trigger_on)
853 return 0;
855 /* Set latched mode interrupt and clear any latched interrupt */
856 ret = i2c_smbus_write_byte_data(data->client,
857 BMG160_REG_INT_RST_LATCH,
858 BMG160_INT_MODE_LATCH_INT |
859 BMG160_INT_MODE_LATCH_RESET);
860 if (ret < 0) {
861 dev_err(&data->client->dev, "Error writing reg_rst_latch\n");
862 return ret;
865 return 0;
868 static int bmg160_data_rdy_trigger_set_state(struct iio_trigger *trig,
869 bool state)
871 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
872 struct bmg160_data *data = iio_priv(indio_dev);
873 int ret;
875 mutex_lock(&data->mutex);
877 if (!state && data->ev_enable_state && data->motion_trigger_on) {
878 data->motion_trigger_on = false;
879 mutex_unlock(&data->mutex);
880 return 0;
884 * Refer to comment in bmg160_write_event_config for
885 * enable/disable operation order
887 ret = bmg160_set_power_state(data, state);
888 if (ret < 0) {
889 mutex_unlock(&data->mutex);
890 return ret;
892 if (data->motion_trig == trig)
893 ret = bmg160_setup_any_motion_interrupt(data, state);
894 else
895 ret = bmg160_setup_new_data_interrupt(data, state);
896 if (ret < 0) {
897 bmg160_set_power_state(data, false);
898 mutex_unlock(&data->mutex);
899 return ret;
901 if (data->motion_trig == trig)
902 data->motion_trigger_on = state;
903 else
904 data->dready_trigger_on = state;
906 mutex_unlock(&data->mutex);
908 return 0;
911 static const struct iio_trigger_ops bmg160_trigger_ops = {
912 .set_trigger_state = bmg160_data_rdy_trigger_set_state,
913 .try_reenable = bmg160_trig_try_reen,
914 .owner = THIS_MODULE,
917 static irqreturn_t bmg160_event_handler(int irq, void *private)
919 struct iio_dev *indio_dev = private;
920 struct bmg160_data *data = iio_priv(indio_dev);
921 int ret;
922 int dir;
924 ret = i2c_smbus_read_byte_data(data->client, BMG160_REG_INT_STATUS_2);
925 if (ret < 0) {
926 dev_err(&data->client->dev, "Error reading reg_int_status2\n");
927 goto ack_intr_status;
930 if (ret & 0x08)
931 dir = IIO_EV_DIR_RISING;
932 else
933 dir = IIO_EV_DIR_FALLING;
935 if (ret & BMG160_ANY_MOTION_BIT_X)
936 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
938 IIO_MOD_X,
939 IIO_EV_TYPE_ROC,
940 dir),
941 data->timestamp);
942 if (ret & BMG160_ANY_MOTION_BIT_Y)
943 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
945 IIO_MOD_Y,
946 IIO_EV_TYPE_ROC,
947 dir),
948 data->timestamp);
949 if (ret & BMG160_ANY_MOTION_BIT_Z)
950 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
952 IIO_MOD_Z,
953 IIO_EV_TYPE_ROC,
954 dir),
955 data->timestamp);
957 ack_intr_status:
958 if (!data->dready_trigger_on) {
959 ret = i2c_smbus_write_byte_data(data->client,
960 BMG160_REG_INT_RST_LATCH,
961 BMG160_INT_MODE_LATCH_INT |
962 BMG160_INT_MODE_LATCH_RESET);
963 if (ret < 0)
964 dev_err(&data->client->dev,
965 "Error writing reg_rst_latch\n");
968 return IRQ_HANDLED;
971 static irqreturn_t bmg160_data_rdy_trig_poll(int irq, void *private)
973 struct iio_dev *indio_dev = private;
974 struct bmg160_data *data = iio_priv(indio_dev);
976 data->timestamp = iio_get_time_ns();
978 if (data->dready_trigger_on)
979 iio_trigger_poll(data->dready_trig);
980 else if (data->motion_trigger_on)
981 iio_trigger_poll(data->motion_trig);
983 if (data->ev_enable_state)
984 return IRQ_WAKE_THREAD;
985 else
986 return IRQ_HANDLED;
990 static int bmg160_gpio_probe(struct i2c_client *client,
991 struct bmg160_data *data)
994 struct device *dev;
995 struct gpio_desc *gpio;
996 int ret;
998 if (!client)
999 return -EINVAL;
1001 dev = &client->dev;
1003 /* data ready gpio interrupt pin */
1004 gpio = devm_gpiod_get_index(dev, BMG160_GPIO_NAME, 0);
1005 if (IS_ERR(gpio)) {
1006 dev_err(dev, "acpi gpio get index failed\n");
1007 return PTR_ERR(gpio);
1010 ret = gpiod_direction_input(gpio);
1011 if (ret)
1012 return ret;
1014 ret = gpiod_to_irq(gpio);
1016 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
1018 return ret;
1021 static const char *bmg160_match_acpi_device(struct device *dev)
1023 const struct acpi_device_id *id;
1025 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1026 if (!id)
1027 return NULL;
1029 return dev_name(dev);
1032 static int bmg160_probe(struct i2c_client *client,
1033 const struct i2c_device_id *id)
1035 struct bmg160_data *data;
1036 struct iio_dev *indio_dev;
1037 int ret;
1038 const char *name = NULL;
1040 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1041 if (!indio_dev)
1042 return -ENOMEM;
1044 data = iio_priv(indio_dev);
1045 i2c_set_clientdata(client, indio_dev);
1046 data->client = client;
1048 ret = bmg160_chip_init(data);
1049 if (ret < 0)
1050 return ret;
1052 mutex_init(&data->mutex);
1054 if (id)
1055 name = id->name;
1057 if (ACPI_HANDLE(&client->dev))
1058 name = bmg160_match_acpi_device(&client->dev);
1060 indio_dev->dev.parent = &client->dev;
1061 indio_dev->channels = bmg160_channels;
1062 indio_dev->num_channels = ARRAY_SIZE(bmg160_channels);
1063 indio_dev->name = name;
1064 indio_dev->modes = INDIO_DIRECT_MODE;
1065 indio_dev->info = &bmg160_info;
1067 if (client->irq <= 0)
1068 client->irq = bmg160_gpio_probe(client, data);
1070 if (client->irq > 0) {
1071 ret = devm_request_threaded_irq(&client->dev,
1072 client->irq,
1073 bmg160_data_rdy_trig_poll,
1074 bmg160_event_handler,
1075 IRQF_TRIGGER_RISING,
1076 BMG160_IRQ_NAME,
1077 indio_dev);
1078 if (ret)
1079 return ret;
1081 data->dready_trig = devm_iio_trigger_alloc(&client->dev,
1082 "%s-dev%d",
1083 indio_dev->name,
1084 indio_dev->id);
1085 if (!data->dready_trig)
1086 return -ENOMEM;
1088 data->motion_trig = devm_iio_trigger_alloc(&client->dev,
1089 "%s-any-motion-dev%d",
1090 indio_dev->name,
1091 indio_dev->id);
1092 if (!data->motion_trig)
1093 return -ENOMEM;
1095 data->dready_trig->dev.parent = &client->dev;
1096 data->dready_trig->ops = &bmg160_trigger_ops;
1097 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
1098 ret = iio_trigger_register(data->dready_trig);
1099 if (ret)
1100 return ret;
1102 data->motion_trig->dev.parent = &client->dev;
1103 data->motion_trig->ops = &bmg160_trigger_ops;
1104 iio_trigger_set_drvdata(data->motion_trig, indio_dev);
1105 ret = iio_trigger_register(data->motion_trig);
1106 if (ret) {
1107 data->motion_trig = NULL;
1108 goto err_trigger_unregister;
1111 ret = iio_triggered_buffer_setup(indio_dev,
1112 NULL,
1113 bmg160_trigger_handler,
1114 NULL);
1115 if (ret < 0) {
1116 dev_err(&client->dev,
1117 "iio triggered buffer setup failed\n");
1118 goto err_trigger_unregister;
1122 ret = iio_device_register(indio_dev);
1123 if (ret < 0) {
1124 dev_err(&client->dev, "unable to register iio device\n");
1125 goto err_buffer_cleanup;
1128 ret = pm_runtime_set_active(&client->dev);
1129 if (ret)
1130 goto err_iio_unregister;
1132 pm_runtime_enable(&client->dev);
1133 pm_runtime_set_autosuspend_delay(&client->dev,
1134 BMG160_AUTO_SUSPEND_DELAY_MS);
1135 pm_runtime_use_autosuspend(&client->dev);
1137 return 0;
1139 err_iio_unregister:
1140 iio_device_unregister(indio_dev);
1141 err_buffer_cleanup:
1142 if (data->dready_trig)
1143 iio_triggered_buffer_cleanup(indio_dev);
1144 err_trigger_unregister:
1145 if (data->dready_trig)
1146 iio_trigger_unregister(data->dready_trig);
1147 if (data->motion_trig)
1148 iio_trigger_unregister(data->motion_trig);
1150 return ret;
1153 static int bmg160_remove(struct i2c_client *client)
1155 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1156 struct bmg160_data *data = iio_priv(indio_dev);
1158 pm_runtime_disable(&client->dev);
1159 pm_runtime_set_suspended(&client->dev);
1160 pm_runtime_put_noidle(&client->dev);
1162 iio_device_unregister(indio_dev);
1164 if (data->dready_trig) {
1165 iio_triggered_buffer_cleanup(indio_dev);
1166 iio_trigger_unregister(data->dready_trig);
1167 iio_trigger_unregister(data->motion_trig);
1170 mutex_lock(&data->mutex);
1171 bmg160_set_mode(data, BMG160_MODE_DEEP_SUSPEND);
1172 mutex_unlock(&data->mutex);
1174 return 0;
1177 #ifdef CONFIG_PM_SLEEP
1178 static int bmg160_suspend(struct device *dev)
1180 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1181 struct bmg160_data *data = iio_priv(indio_dev);
1183 mutex_lock(&data->mutex);
1184 bmg160_set_mode(data, BMG160_MODE_SUSPEND);
1185 mutex_unlock(&data->mutex);
1187 return 0;
1190 static int bmg160_resume(struct device *dev)
1192 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1193 struct bmg160_data *data = iio_priv(indio_dev);
1195 mutex_lock(&data->mutex);
1196 if (data->dready_trigger_on || data->motion_trigger_on ||
1197 data->ev_enable_state)
1198 bmg160_set_mode(data, BMG160_MODE_NORMAL);
1199 mutex_unlock(&data->mutex);
1201 return 0;
1203 #endif
1205 #ifdef CONFIG_PM
1206 static int bmg160_runtime_suspend(struct device *dev)
1208 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1209 struct bmg160_data *data = iio_priv(indio_dev);
1210 int ret;
1212 ret = bmg160_set_mode(data, BMG160_MODE_SUSPEND);
1213 if (ret < 0) {
1214 dev_err(&data->client->dev, "set mode failed\n");
1215 return -EAGAIN;
1218 return 0;
1221 static int bmg160_runtime_resume(struct device *dev)
1223 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1224 struct bmg160_data *data = iio_priv(indio_dev);
1225 int ret;
1227 ret = bmg160_set_mode(data, BMG160_MODE_NORMAL);
1228 if (ret < 0)
1229 return ret;
1231 msleep_interruptible(BMG160_MAX_STARTUP_TIME_MS);
1233 return 0;
1235 #endif
1237 static const struct dev_pm_ops bmg160_pm_ops = {
1238 SET_SYSTEM_SLEEP_PM_OPS(bmg160_suspend, bmg160_resume)
1239 SET_RUNTIME_PM_OPS(bmg160_runtime_suspend,
1240 bmg160_runtime_resume, NULL)
1243 static const struct acpi_device_id bmg160_acpi_match[] = {
1244 {"BMG0160", 0},
1245 {"BMI055B", 0},
1249 MODULE_DEVICE_TABLE(acpi, bmg160_acpi_match);
1251 static const struct i2c_device_id bmg160_id[] = {
1252 {"bmg160", 0},
1253 {"bmi055_gyro", 0},
1257 MODULE_DEVICE_TABLE(i2c, bmg160_id);
1259 static struct i2c_driver bmg160_driver = {
1260 .driver = {
1261 .name = BMG160_DRV_NAME,
1262 .acpi_match_table = ACPI_PTR(bmg160_acpi_match),
1263 .pm = &bmg160_pm_ops,
1265 .probe = bmg160_probe,
1266 .remove = bmg160_remove,
1267 .id_table = bmg160_id,
1269 module_i2c_driver(bmg160_driver);
1271 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1272 MODULE_LICENSE("GPL v2");
1273 MODULE_DESCRIPTION("BMG160 Gyro driver");