2 * Bosch BMC150 three-axis magnetic field sensor driver
4 * Copyright (c) 2015, Intel Corporation.
6 * This code is based on bmm050_api.c authored by contact@bosch.sensortec.com:
8 * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/acpi.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
30 #include <linux/iio/buffer.h>
31 #include <linux/iio/events.h>
32 #include <linux/iio/trigger.h>
33 #include <linux/iio/trigger_consumer.h>
34 #include <linux/iio/triggered_buffer.h>
35 #include <linux/regmap.h>
37 #include "bmc150_magn.h"
39 #define BMC150_MAGN_DRV_NAME "bmc150_magn"
40 #define BMC150_MAGN_IRQ_NAME "bmc150_magn_event"
42 #define BMC150_MAGN_REG_CHIP_ID 0x40
43 #define BMC150_MAGN_CHIP_ID_VAL 0x32
45 #define BMC150_MAGN_REG_X_L 0x42
46 #define BMC150_MAGN_REG_X_M 0x43
47 #define BMC150_MAGN_REG_Y_L 0x44
48 #define BMC150_MAGN_REG_Y_M 0x45
49 #define BMC150_MAGN_SHIFT_XY_L 3
50 #define BMC150_MAGN_REG_Z_L 0x46
51 #define BMC150_MAGN_REG_Z_M 0x47
52 #define BMC150_MAGN_SHIFT_Z_L 1
53 #define BMC150_MAGN_REG_RHALL_L 0x48
54 #define BMC150_MAGN_REG_RHALL_M 0x49
55 #define BMC150_MAGN_SHIFT_RHALL_L 2
57 #define BMC150_MAGN_REG_INT_STATUS 0x4A
59 #define BMC150_MAGN_REG_POWER 0x4B
60 #define BMC150_MAGN_MASK_POWER_CTL BIT(0)
62 #define BMC150_MAGN_REG_OPMODE_ODR 0x4C
63 #define BMC150_MAGN_MASK_OPMODE GENMASK(2, 1)
64 #define BMC150_MAGN_SHIFT_OPMODE 1
65 #define BMC150_MAGN_MODE_NORMAL 0x00
66 #define BMC150_MAGN_MODE_FORCED 0x01
67 #define BMC150_MAGN_MODE_SLEEP 0x03
68 #define BMC150_MAGN_MASK_ODR GENMASK(5, 3)
69 #define BMC150_MAGN_SHIFT_ODR 3
71 #define BMC150_MAGN_REG_INT 0x4D
73 #define BMC150_MAGN_REG_INT_DRDY 0x4E
74 #define BMC150_MAGN_MASK_DRDY_EN BIT(7)
75 #define BMC150_MAGN_SHIFT_DRDY_EN 7
76 #define BMC150_MAGN_MASK_DRDY_INT3 BIT(6)
77 #define BMC150_MAGN_MASK_DRDY_Z_EN BIT(5)
78 #define BMC150_MAGN_MASK_DRDY_Y_EN BIT(4)
79 #define BMC150_MAGN_MASK_DRDY_X_EN BIT(3)
80 #define BMC150_MAGN_MASK_DRDY_DR_POLARITY BIT(2)
81 #define BMC150_MAGN_MASK_DRDY_LATCHING BIT(1)
82 #define BMC150_MAGN_MASK_DRDY_INT3_POLARITY BIT(0)
84 #define BMC150_MAGN_REG_LOW_THRESH 0x4F
85 #define BMC150_MAGN_REG_HIGH_THRESH 0x50
86 #define BMC150_MAGN_REG_REP_XY 0x51
87 #define BMC150_MAGN_REG_REP_Z 0x52
88 #define BMC150_MAGN_REG_REP_DATAMASK GENMASK(7, 0)
90 #define BMC150_MAGN_REG_TRIM_START 0x5D
91 #define BMC150_MAGN_REG_TRIM_END 0x71
93 #define BMC150_MAGN_XY_OVERFLOW_VAL -4096
94 #define BMC150_MAGN_Z_OVERFLOW_VAL -16384
96 /* Time from SUSPEND to SLEEP */
97 #define BMC150_MAGN_START_UP_TIME_MS 3
99 #define BMC150_MAGN_AUTO_SUSPEND_DELAY_MS 2000
101 #define BMC150_MAGN_REGVAL_TO_REPXY(regval) (((regval) * 2) + 1)
102 #define BMC150_MAGN_REGVAL_TO_REPZ(regval) ((regval) + 1)
103 #define BMC150_MAGN_REPXY_TO_REGVAL(rep) (((rep) - 1) / 2)
104 #define BMC150_MAGN_REPZ_TO_REGVAL(rep) ((rep) - 1)
106 enum bmc150_magn_axis
{
111 AXIS_XYZ_MAX
= RHALL
,
115 enum bmc150_magn_power_modes
{
116 BMC150_MAGN_POWER_MODE_SUSPEND
,
117 BMC150_MAGN_POWER_MODE_SLEEP
,
118 BMC150_MAGN_POWER_MODE_NORMAL
,
121 struct bmc150_magn_trim_regs
{
138 struct bmc150_magn_data
{
141 * 1. Protect this structure.
142 * 2. Serialize sequences that power on/off the device and access HW.
145 struct regmap
*regmap
;
146 /* 4 x 32 bits for x, y z, 4 bytes align, 64 bits timestamp */
148 struct iio_trigger
*dready_trig
;
149 bool dready_trigger_on
;
154 static const struct {
157 } bmc150_magn_samp_freq_table
[] = { {2, 0x01},
166 enum bmc150_magn_presets
{
169 ENHANCED_REGULAR_PRESET
,
173 static const struct bmc150_magn_preset
{
177 } bmc150_magn_presets_table
[] = {
178 [LOW_POWER_PRESET
] = {3, 3, 10},
179 [REGULAR_PRESET
] = {9, 15, 10},
180 [ENHANCED_REGULAR_PRESET
] = {15, 27, 10},
181 [HIGH_ACCURACY_PRESET
] = {47, 83, 20},
184 #define BMC150_MAGN_DEFAULT_PRESET REGULAR_PRESET
186 static bool bmc150_magn_is_writeable_reg(struct device
*dev
, unsigned int reg
)
189 case BMC150_MAGN_REG_POWER
:
190 case BMC150_MAGN_REG_OPMODE_ODR
:
191 case BMC150_MAGN_REG_INT
:
192 case BMC150_MAGN_REG_INT_DRDY
:
193 case BMC150_MAGN_REG_LOW_THRESH
:
194 case BMC150_MAGN_REG_HIGH_THRESH
:
195 case BMC150_MAGN_REG_REP_XY
:
196 case BMC150_MAGN_REG_REP_Z
:
203 static bool bmc150_magn_is_volatile_reg(struct device
*dev
, unsigned int reg
)
206 case BMC150_MAGN_REG_X_L
:
207 case BMC150_MAGN_REG_X_M
:
208 case BMC150_MAGN_REG_Y_L
:
209 case BMC150_MAGN_REG_Y_M
:
210 case BMC150_MAGN_REG_Z_L
:
211 case BMC150_MAGN_REG_Z_M
:
212 case BMC150_MAGN_REG_RHALL_L
:
213 case BMC150_MAGN_REG_RHALL_M
:
214 case BMC150_MAGN_REG_INT_STATUS
:
221 const struct regmap_config bmc150_magn_regmap_config
= {
225 .max_register
= BMC150_MAGN_REG_TRIM_END
,
226 .cache_type
= REGCACHE_RBTREE
,
228 .writeable_reg
= bmc150_magn_is_writeable_reg
,
229 .volatile_reg
= bmc150_magn_is_volatile_reg
,
231 EXPORT_SYMBOL(bmc150_magn_regmap_config
);
233 static int bmc150_magn_set_power_mode(struct bmc150_magn_data
*data
,
234 enum bmc150_magn_power_modes mode
,
240 case BMC150_MAGN_POWER_MODE_SUSPEND
:
241 ret
= regmap_update_bits(data
->regmap
, BMC150_MAGN_REG_POWER
,
242 BMC150_MAGN_MASK_POWER_CTL
, !state
);
245 usleep_range(BMC150_MAGN_START_UP_TIME_MS
* 1000, 20000);
247 case BMC150_MAGN_POWER_MODE_SLEEP
:
248 return regmap_update_bits(data
->regmap
,
249 BMC150_MAGN_REG_OPMODE_ODR
,
250 BMC150_MAGN_MASK_OPMODE
,
251 BMC150_MAGN_MODE_SLEEP
<<
252 BMC150_MAGN_SHIFT_OPMODE
);
253 case BMC150_MAGN_POWER_MODE_NORMAL
:
254 return regmap_update_bits(data
->regmap
,
255 BMC150_MAGN_REG_OPMODE_ODR
,
256 BMC150_MAGN_MASK_OPMODE
,
257 BMC150_MAGN_MODE_NORMAL
<<
258 BMC150_MAGN_SHIFT_OPMODE
);
264 static int bmc150_magn_set_power_state(struct bmc150_magn_data
*data
, bool on
)
270 ret
= pm_runtime_get_sync(data
->dev
);
272 pm_runtime_mark_last_busy(data
->dev
);
273 ret
= pm_runtime_put_autosuspend(data
->dev
);
278 "failed to change power state to %d\n", on
);
280 pm_runtime_put_noidle(data
->dev
);
289 static int bmc150_magn_get_odr(struct bmc150_magn_data
*data
, int *val
)
294 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_OPMODE_ODR
, ®_val
);
297 odr_val
= (reg_val
& BMC150_MAGN_MASK_ODR
) >> BMC150_MAGN_SHIFT_ODR
;
299 for (i
= 0; i
< ARRAY_SIZE(bmc150_magn_samp_freq_table
); i
++)
300 if (bmc150_magn_samp_freq_table
[i
].reg_val
== odr_val
) {
301 *val
= bmc150_magn_samp_freq_table
[i
].freq
;
308 static int bmc150_magn_set_odr(struct bmc150_magn_data
*data
, int val
)
313 for (i
= 0; i
< ARRAY_SIZE(bmc150_magn_samp_freq_table
); i
++) {
314 if (bmc150_magn_samp_freq_table
[i
].freq
== val
) {
315 ret
= regmap_update_bits(data
->regmap
,
316 BMC150_MAGN_REG_OPMODE_ODR
,
317 BMC150_MAGN_MASK_ODR
,
318 bmc150_magn_samp_freq_table
[i
].
320 BMC150_MAGN_SHIFT_ODR
);
330 static int bmc150_magn_set_max_odr(struct bmc150_magn_data
*data
, int rep_xy
,
333 int ret
, reg_val
, max_odr
;
336 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_REP_XY
,
340 rep_xy
= BMC150_MAGN_REGVAL_TO_REPXY(reg_val
);
343 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_REP_Z
,
347 rep_z
= BMC150_MAGN_REGVAL_TO_REPZ(reg_val
);
350 ret
= bmc150_magn_get_odr(data
, &odr
);
354 /* the maximum selectable read-out frequency from datasheet */
355 max_odr
= 1000000 / (145 * rep_xy
+ 500 * rep_z
+ 980);
358 "Can't set oversampling with sampling freq %d\n",
362 data
->max_odr
= max_odr
;
367 static s32
bmc150_magn_compensate_x(struct bmc150_magn_trim_regs
*tregs
, s16 x
,
371 u16 xyz1
= le16_to_cpu(tregs
->xyz1
);
373 if (x
== BMC150_MAGN_XY_OVERFLOW_VAL
)
379 val
= ((s16
)(((u16
)((((s32
)xyz1
) << 14) / rhall
)) - ((u16
)0x4000)));
380 val
= ((s16
)((((s32
)x
) * ((((((((s32
)tregs
->xy2
) * ((((s32
)val
) *
381 ((s32
)val
)) >> 7)) + (((s32
)val
) *
382 ((s32
)(((s16
)tregs
->xy1
) << 7)))) >> 9) + ((s32
)0x100000)) *
383 ((s32
)(((s16
)tregs
->x2
) + ((s16
)0xA0)))) >> 12)) >> 13)) +
384 (((s16
)tregs
->x1
) << 3);
389 static s32
bmc150_magn_compensate_y(struct bmc150_magn_trim_regs
*tregs
, s16 y
,
393 u16 xyz1
= le16_to_cpu(tregs
->xyz1
);
395 if (y
== BMC150_MAGN_XY_OVERFLOW_VAL
)
401 val
= ((s16
)(((u16
)((((s32
)xyz1
) << 14) / rhall
)) - ((u16
)0x4000)));
402 val
= ((s16
)((((s32
)y
) * ((((((((s32
)tregs
->xy2
) * ((((s32
)val
) *
403 ((s32
)val
)) >> 7)) + (((s32
)val
) *
404 ((s32
)(((s16
)tregs
->xy1
) << 7)))) >> 9) + ((s32
)0x100000)) *
405 ((s32
)(((s16
)tregs
->y2
) + ((s16
)0xA0)))) >> 12)) >> 13)) +
406 (((s16
)tregs
->y1
) << 3);
411 static s32
bmc150_magn_compensate_z(struct bmc150_magn_trim_regs
*tregs
, s16 z
,
415 u16 xyz1
= le16_to_cpu(tregs
->xyz1
);
416 u16 z1
= le16_to_cpu(tregs
->z1
);
417 s16 z2
= le16_to_cpu(tregs
->z2
);
418 s16 z3
= le16_to_cpu(tregs
->z3
);
419 s16 z4
= le16_to_cpu(tregs
->z4
);
421 if (z
== BMC150_MAGN_Z_OVERFLOW_VAL
)
424 val
= (((((s32
)(z
- z4
)) << 15) - ((((s32
)z3
) * ((s32
)(((s16
)rhall
) -
425 ((s16
)xyz1
)))) >> 2)) / (z2
+ ((s16
)(((((s32
)z1
) *
426 ((((s16
)rhall
) << 1))) + (1 << 15)) >> 16))));
431 static int bmc150_magn_read_xyz(struct bmc150_magn_data
*data
, s32
*buffer
)
434 __le16 values
[AXIS_XYZR_MAX
];
435 s16 raw_x
, raw_y
, raw_z
;
437 struct bmc150_magn_trim_regs tregs
;
439 ret
= regmap_bulk_read(data
->regmap
, BMC150_MAGN_REG_X_L
,
440 values
, sizeof(values
));
444 raw_x
= (s16
)le16_to_cpu(values
[AXIS_X
]) >> BMC150_MAGN_SHIFT_XY_L
;
445 raw_y
= (s16
)le16_to_cpu(values
[AXIS_Y
]) >> BMC150_MAGN_SHIFT_XY_L
;
446 raw_z
= (s16
)le16_to_cpu(values
[AXIS_Z
]) >> BMC150_MAGN_SHIFT_Z_L
;
447 rhall
= le16_to_cpu(values
[RHALL
]) >> BMC150_MAGN_SHIFT_RHALL_L
;
449 ret
= regmap_bulk_read(data
->regmap
, BMC150_MAGN_REG_TRIM_START
,
450 &tregs
, sizeof(tregs
));
454 buffer
[AXIS_X
] = bmc150_magn_compensate_x(&tregs
, raw_x
, rhall
);
455 buffer
[AXIS_Y
] = bmc150_magn_compensate_y(&tregs
, raw_y
, rhall
);
456 buffer
[AXIS_Z
] = bmc150_magn_compensate_z(&tregs
, raw_z
, rhall
);
461 static int bmc150_magn_read_raw(struct iio_dev
*indio_dev
,
462 struct iio_chan_spec
const *chan
,
463 int *val
, int *val2
, long mask
)
465 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
467 s32 values
[AXIS_XYZ_MAX
];
470 case IIO_CHAN_INFO_RAW
:
471 if (iio_buffer_enabled(indio_dev
))
473 mutex_lock(&data
->mutex
);
475 ret
= bmc150_magn_set_power_state(data
, true);
477 mutex_unlock(&data
->mutex
);
481 ret
= bmc150_magn_read_xyz(data
, values
);
483 bmc150_magn_set_power_state(data
, false);
484 mutex_unlock(&data
->mutex
);
487 *val
= values
[chan
->scan_index
];
489 ret
= bmc150_magn_set_power_state(data
, false);
491 mutex_unlock(&data
->mutex
);
495 mutex_unlock(&data
->mutex
);
497 case IIO_CHAN_INFO_SCALE
:
499 * The API/driver performs an off-chip temperature
500 * compensation and outputs x/y/z magnetic field data in
501 * 16 LSB/uT to the upper application layer.
505 return IIO_VAL_INT_PLUS_MICRO
;
506 case IIO_CHAN_INFO_SAMP_FREQ
:
507 ret
= bmc150_magn_get_odr(data
, val
);
511 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
512 switch (chan
->channel2
) {
515 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_REP_XY
,
519 *val
= BMC150_MAGN_REGVAL_TO_REPXY(tmp
);
522 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_REP_Z
,
526 *val
= BMC150_MAGN_REGVAL_TO_REPZ(tmp
);
536 static int bmc150_magn_write_raw(struct iio_dev
*indio_dev
,
537 struct iio_chan_spec
const *chan
,
538 int val
, int val2
, long mask
)
540 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
544 case IIO_CHAN_INFO_SAMP_FREQ
:
545 if (val
> data
->max_odr
)
547 mutex_lock(&data
->mutex
);
548 ret
= bmc150_magn_set_odr(data
, val
);
549 mutex_unlock(&data
->mutex
);
551 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
552 switch (chan
->channel2
) {
555 if (val
< 1 || val
> 511)
557 mutex_lock(&data
->mutex
);
558 ret
= bmc150_magn_set_max_odr(data
, val
, 0, 0);
560 mutex_unlock(&data
->mutex
);
563 ret
= regmap_update_bits(data
->regmap
,
564 BMC150_MAGN_REG_REP_XY
,
565 BMC150_MAGN_REG_REP_DATAMASK
,
566 BMC150_MAGN_REPXY_TO_REGVAL
568 mutex_unlock(&data
->mutex
);
571 if (val
< 1 || val
> 256)
573 mutex_lock(&data
->mutex
);
574 ret
= bmc150_magn_set_max_odr(data
, 0, val
, 0);
576 mutex_unlock(&data
->mutex
);
579 ret
= regmap_update_bits(data
->regmap
,
580 BMC150_MAGN_REG_REP_Z
,
581 BMC150_MAGN_REG_REP_DATAMASK
,
582 BMC150_MAGN_REPZ_TO_REGVAL
584 mutex_unlock(&data
->mutex
);
594 static ssize_t
bmc150_magn_show_samp_freq_avail(struct device
*dev
,
595 struct device_attribute
*attr
,
598 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
599 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
603 for (i
= 0; i
< ARRAY_SIZE(bmc150_magn_samp_freq_table
); i
++) {
604 if (bmc150_magn_samp_freq_table
[i
].freq
> data
->max_odr
)
606 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ",
607 bmc150_magn_samp_freq_table
[i
].freq
);
609 /* replace last space with a newline */
615 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail
);
617 static struct attribute
*bmc150_magn_attributes
[] = {
618 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
622 static const struct attribute_group bmc150_magn_attrs_group
= {
623 .attrs
= bmc150_magn_attributes
,
626 #define BMC150_MAGN_CHANNEL(_axis) { \
629 .channel2 = IIO_MOD_##_axis, \
630 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
631 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
632 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
633 BIT(IIO_CHAN_INFO_SCALE), \
634 .scan_index = AXIS_##_axis, \
639 .endianness = IIO_LE \
643 static const struct iio_chan_spec bmc150_magn_channels
[] = {
644 BMC150_MAGN_CHANNEL(X
),
645 BMC150_MAGN_CHANNEL(Y
),
646 BMC150_MAGN_CHANNEL(Z
),
647 IIO_CHAN_SOFT_TIMESTAMP(3),
650 static const struct iio_info bmc150_magn_info
= {
651 .attrs
= &bmc150_magn_attrs_group
,
652 .read_raw
= bmc150_magn_read_raw
,
653 .write_raw
= bmc150_magn_write_raw
,
654 .driver_module
= THIS_MODULE
,
657 static const unsigned long bmc150_magn_scan_masks
[] = {
658 BIT(AXIS_X
) | BIT(AXIS_Y
) | BIT(AXIS_Z
),
661 static irqreturn_t
bmc150_magn_trigger_handler(int irq
, void *p
)
663 struct iio_poll_func
*pf
= p
;
664 struct iio_dev
*indio_dev
= pf
->indio_dev
;
665 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
668 mutex_lock(&data
->mutex
);
669 ret
= bmc150_magn_read_xyz(data
, data
->buffer
);
673 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
677 mutex_unlock(&data
->mutex
);
678 iio_trigger_notify_done(indio_dev
->trig
);
683 static int bmc150_magn_init(struct bmc150_magn_data
*data
)
686 struct bmc150_magn_preset preset
;
688 ret
= bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SUSPEND
,
692 "Failed to bring up device from suspend mode\n");
696 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_CHIP_ID
, &chip_id
);
698 dev_err(data
->dev
, "Failed reading chip id\n");
701 if (chip_id
!= BMC150_MAGN_CHIP_ID_VAL
) {
702 dev_err(data
->dev
, "Invalid chip id 0x%x\n", chip_id
);
706 dev_dbg(data
->dev
, "Chip id %x\n", chip_id
);
708 preset
= bmc150_magn_presets_table
[BMC150_MAGN_DEFAULT_PRESET
];
709 ret
= bmc150_magn_set_odr(data
, preset
.odr
);
711 dev_err(data
->dev
, "Failed to set ODR to %d\n",
716 ret
= regmap_write(data
->regmap
, BMC150_MAGN_REG_REP_XY
,
717 BMC150_MAGN_REPXY_TO_REGVAL(preset
.rep_xy
));
719 dev_err(data
->dev
, "Failed to set REP XY to %d\n",
724 ret
= regmap_write(data
->regmap
, BMC150_MAGN_REG_REP_Z
,
725 BMC150_MAGN_REPZ_TO_REGVAL(preset
.rep_z
));
727 dev_err(data
->dev
, "Failed to set REP Z to %d\n",
732 ret
= bmc150_magn_set_max_odr(data
, preset
.rep_xy
, preset
.rep_z
,
737 ret
= bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_NORMAL
,
740 dev_err(data
->dev
, "Failed to power on device\n");
747 bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SUSPEND
, true);
751 static int bmc150_magn_reset_intr(struct bmc150_magn_data
*data
)
756 * Data Ready (DRDY) is always cleared after
757 * readout of data registers ends.
759 return regmap_read(data
->regmap
, BMC150_MAGN_REG_X_L
, &tmp
);
762 static int bmc150_magn_trig_try_reen(struct iio_trigger
*trig
)
764 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
765 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
768 if (!data
->dready_trigger_on
)
771 mutex_lock(&data
->mutex
);
772 ret
= bmc150_magn_reset_intr(data
);
773 mutex_unlock(&data
->mutex
);
778 static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
781 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
782 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
785 mutex_lock(&data
->mutex
);
786 if (state
== data
->dready_trigger_on
)
789 ret
= regmap_update_bits(data
->regmap
, BMC150_MAGN_REG_INT_DRDY
,
790 BMC150_MAGN_MASK_DRDY_EN
,
791 state
<< BMC150_MAGN_SHIFT_DRDY_EN
);
795 data
->dready_trigger_on
= state
;
798 ret
= bmc150_magn_reset_intr(data
);
802 mutex_unlock(&data
->mutex
);
807 mutex_unlock(&data
->mutex
);
811 static const struct iio_trigger_ops bmc150_magn_trigger_ops
= {
812 .set_trigger_state
= bmc150_magn_data_rdy_trigger_set_state
,
813 .try_reenable
= bmc150_magn_trig_try_reen
,
814 .owner
= THIS_MODULE
,
817 static int bmc150_magn_buffer_preenable(struct iio_dev
*indio_dev
)
819 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
821 return bmc150_magn_set_power_state(data
, true);
824 static int bmc150_magn_buffer_postdisable(struct iio_dev
*indio_dev
)
826 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
828 return bmc150_magn_set_power_state(data
, false);
831 static const struct iio_buffer_setup_ops bmc150_magn_buffer_setup_ops
= {
832 .preenable
= bmc150_magn_buffer_preenable
,
833 .postenable
= iio_triggered_buffer_postenable
,
834 .predisable
= iio_triggered_buffer_predisable
,
835 .postdisable
= bmc150_magn_buffer_postdisable
,
838 static const char *bmc150_magn_match_acpi_device(struct device
*dev
)
840 const struct acpi_device_id
*id
;
842 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
846 return dev_name(dev
);
849 int bmc150_magn_probe(struct device
*dev
, struct regmap
*regmap
,
850 int irq
, const char *name
)
852 struct bmc150_magn_data
*data
;
853 struct iio_dev
*indio_dev
;
856 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
860 data
= iio_priv(indio_dev
);
861 dev_set_drvdata(dev
, indio_dev
);
862 data
->regmap
= regmap
;
866 if (!name
&& ACPI_HANDLE(dev
))
867 name
= bmc150_magn_match_acpi_device(dev
);
869 mutex_init(&data
->mutex
);
871 ret
= bmc150_magn_init(data
);
875 indio_dev
->dev
.parent
= dev
;
876 indio_dev
->channels
= bmc150_magn_channels
;
877 indio_dev
->num_channels
= ARRAY_SIZE(bmc150_magn_channels
);
878 indio_dev
->available_scan_masks
= bmc150_magn_scan_masks
;
879 indio_dev
->name
= name
;
880 indio_dev
->modes
= INDIO_DIRECT_MODE
;
881 indio_dev
->info
= &bmc150_magn_info
;
884 data
->dready_trig
= devm_iio_trigger_alloc(dev
,
888 if (!data
->dready_trig
) {
890 dev_err(dev
, "iio trigger alloc failed\n");
894 data
->dready_trig
->dev
.parent
= dev
;
895 data
->dready_trig
->ops
= &bmc150_magn_trigger_ops
;
896 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
897 ret
= iio_trigger_register(data
->dready_trig
);
899 dev_err(dev
, "iio trigger register failed\n");
903 ret
= request_threaded_irq(irq
,
904 iio_trigger_generic_data_rdy_poll
,
906 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
907 BMC150_MAGN_IRQ_NAME
,
910 dev_err(dev
, "request irq %d failed\n", irq
);
911 goto err_trigger_unregister
;
915 ret
= iio_triggered_buffer_setup(indio_dev
,
916 iio_pollfunc_store_time
,
917 bmc150_magn_trigger_handler
,
918 &bmc150_magn_buffer_setup_ops
);
920 dev_err(dev
, "iio triggered buffer setup failed\n");
924 ret
= pm_runtime_set_active(dev
);
926 goto err_buffer_cleanup
;
928 pm_runtime_enable(dev
);
929 pm_runtime_set_autosuspend_delay(dev
,
930 BMC150_MAGN_AUTO_SUSPEND_DELAY_MS
);
931 pm_runtime_use_autosuspend(dev
);
933 ret
= iio_device_register(indio_dev
);
935 dev_err(dev
, "unable to register iio device\n");
936 goto err_buffer_cleanup
;
939 dev_dbg(dev
, "Registered device %s\n", name
);
943 iio_triggered_buffer_cleanup(indio_dev
);
946 free_irq(irq
, data
->dready_trig
);
947 err_trigger_unregister
:
948 if (data
->dready_trig
)
949 iio_trigger_unregister(data
->dready_trig
);
951 bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SUSPEND
, true);
954 EXPORT_SYMBOL(bmc150_magn_probe
);
956 int bmc150_magn_remove(struct device
*dev
)
958 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
959 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
961 iio_device_unregister(indio_dev
);
963 pm_runtime_disable(dev
);
964 pm_runtime_set_suspended(dev
);
965 pm_runtime_put_noidle(dev
);
967 iio_triggered_buffer_cleanup(indio_dev
);
970 free_irq(data
->irq
, data
->dready_trig
);
972 if (data
->dready_trig
)
973 iio_trigger_unregister(data
->dready_trig
);
975 mutex_lock(&data
->mutex
);
976 bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SUSPEND
, true);
977 mutex_unlock(&data
->mutex
);
981 EXPORT_SYMBOL(bmc150_magn_remove
);
984 static int bmc150_magn_runtime_suspend(struct device
*dev
)
986 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
987 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
990 mutex_lock(&data
->mutex
);
991 ret
= bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SLEEP
,
993 mutex_unlock(&data
->mutex
);
995 dev_err(dev
, "powering off device failed\n");
1002 * Should be called with data->mutex held.
1004 static int bmc150_magn_runtime_resume(struct device
*dev
)
1006 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1007 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
1009 return bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_NORMAL
,
1014 #ifdef CONFIG_PM_SLEEP
1015 static int bmc150_magn_suspend(struct device
*dev
)
1017 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1018 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
1021 mutex_lock(&data
->mutex
);
1022 ret
= bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SLEEP
,
1024 mutex_unlock(&data
->mutex
);
1029 static int bmc150_magn_resume(struct device
*dev
)
1031 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1032 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
1035 mutex_lock(&data
->mutex
);
1036 ret
= bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_NORMAL
,
1038 mutex_unlock(&data
->mutex
);
1044 const struct dev_pm_ops bmc150_magn_pm_ops
= {
1045 SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend
, bmc150_magn_resume
)
1046 SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend
,
1047 bmc150_magn_runtime_resume
, NULL
)
1049 EXPORT_SYMBOL(bmc150_magn_pm_ops
);
1051 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1052 MODULE_LICENSE("GPL v2");
1053 MODULE_DESCRIPTION("BMC150 magnetometer core driver");