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>
26 #include <linux/gpio/consumer.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/sysfs.h>
31 #include <linux/iio/buffer.h>
32 #include <linux/iio/events.h>
33 #include <linux/iio/trigger.h>
34 #include <linux/iio/trigger_consumer.h>
35 #include <linux/iio/triggered_buffer.h>
36 #include <linux/regmap.h>
38 #define BMC150_MAGN_DRV_NAME "bmc150_magn"
39 #define BMC150_MAGN_IRQ_NAME "bmc150_magn_event"
40 #define BMC150_MAGN_GPIO_INT "interrupt"
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
89 #define BMC150_MAGN_REG_TRIM_START 0x5D
90 #define BMC150_MAGN_REG_TRIM_END 0x71
92 #define BMC150_MAGN_XY_OVERFLOW_VAL -4096
93 #define BMC150_MAGN_Z_OVERFLOW_VAL -16384
95 /* Time from SUSPEND to SLEEP */
96 #define BMC150_MAGN_START_UP_TIME_MS 3
98 #define BMC150_MAGN_AUTO_SUSPEND_DELAY_MS 2000
100 #define BMC150_MAGN_REGVAL_TO_REPXY(regval) (((regval) * 2) + 1)
101 #define BMC150_MAGN_REGVAL_TO_REPZ(regval) ((regval) + 1)
102 #define BMC150_MAGN_REPXY_TO_REGVAL(rep) (((rep) - 1) / 2)
103 #define BMC150_MAGN_REPZ_TO_REGVAL(rep) ((rep) - 1)
105 enum bmc150_magn_axis
{
110 AXIS_XYZ_MAX
= RHALL
,
114 enum bmc150_magn_power_modes
{
115 BMC150_MAGN_POWER_MODE_SUSPEND
,
116 BMC150_MAGN_POWER_MODE_SLEEP
,
117 BMC150_MAGN_POWER_MODE_NORMAL
,
120 struct bmc150_magn_trim_regs
{
137 struct bmc150_magn_data
{
138 struct i2c_client
*client
;
140 * 1. Protect this structure.
141 * 2. Serialize sequences that power on/off the device and access HW.
144 struct regmap
*regmap
;
145 /* 4 x 32 bits for x, y z, 4 bytes align, 64 bits timestamp */
147 struct iio_trigger
*dready_trig
;
148 bool dready_trigger_on
;
152 static const struct {
155 } bmc150_magn_samp_freq_table
[] = { {2, 0x01},
164 enum bmc150_magn_presets
{
167 ENHANCED_REGULAR_PRESET
,
171 static const struct bmc150_magn_preset
{
175 } bmc150_magn_presets_table
[] = {
176 [LOW_POWER_PRESET
] = {3, 3, 10},
177 [REGULAR_PRESET
] = {9, 15, 10},
178 [ENHANCED_REGULAR_PRESET
] = {15, 27, 10},
179 [HIGH_ACCURACY_PRESET
] = {47, 83, 20},
182 #define BMC150_MAGN_DEFAULT_PRESET REGULAR_PRESET
184 static bool bmc150_magn_is_writeable_reg(struct device
*dev
, unsigned int reg
)
187 case BMC150_MAGN_REG_POWER
:
188 case BMC150_MAGN_REG_OPMODE_ODR
:
189 case BMC150_MAGN_REG_INT
:
190 case BMC150_MAGN_REG_INT_DRDY
:
191 case BMC150_MAGN_REG_LOW_THRESH
:
192 case BMC150_MAGN_REG_HIGH_THRESH
:
193 case BMC150_MAGN_REG_REP_XY
:
194 case BMC150_MAGN_REG_REP_Z
:
201 static bool bmc150_magn_is_volatile_reg(struct device
*dev
, unsigned int reg
)
204 case BMC150_MAGN_REG_X_L
:
205 case BMC150_MAGN_REG_X_M
:
206 case BMC150_MAGN_REG_Y_L
:
207 case BMC150_MAGN_REG_Y_M
:
208 case BMC150_MAGN_REG_Z_L
:
209 case BMC150_MAGN_REG_Z_M
:
210 case BMC150_MAGN_REG_RHALL_L
:
211 case BMC150_MAGN_REG_RHALL_M
:
212 case BMC150_MAGN_REG_INT_STATUS
:
219 static const struct regmap_config bmc150_magn_regmap_config
= {
223 .max_register
= BMC150_MAGN_REG_TRIM_END
,
224 .cache_type
= REGCACHE_RBTREE
,
226 .writeable_reg
= bmc150_magn_is_writeable_reg
,
227 .volatile_reg
= bmc150_magn_is_volatile_reg
,
230 static int bmc150_magn_set_power_mode(struct bmc150_magn_data
*data
,
231 enum bmc150_magn_power_modes mode
,
237 case BMC150_MAGN_POWER_MODE_SUSPEND
:
238 ret
= regmap_update_bits(data
->regmap
, BMC150_MAGN_REG_POWER
,
239 BMC150_MAGN_MASK_POWER_CTL
, !state
);
242 usleep_range(BMC150_MAGN_START_UP_TIME_MS
* 1000, 20000);
244 case BMC150_MAGN_POWER_MODE_SLEEP
:
245 return regmap_update_bits(data
->regmap
,
246 BMC150_MAGN_REG_OPMODE_ODR
,
247 BMC150_MAGN_MASK_OPMODE
,
248 BMC150_MAGN_MODE_SLEEP
<<
249 BMC150_MAGN_SHIFT_OPMODE
);
250 case BMC150_MAGN_POWER_MODE_NORMAL
:
251 return regmap_update_bits(data
->regmap
,
252 BMC150_MAGN_REG_OPMODE_ODR
,
253 BMC150_MAGN_MASK_OPMODE
,
254 BMC150_MAGN_MODE_NORMAL
<<
255 BMC150_MAGN_SHIFT_OPMODE
);
261 static int bmc150_magn_set_power_state(struct bmc150_magn_data
*data
, bool on
)
267 ret
= pm_runtime_get_sync(&data
->client
->dev
);
269 pm_runtime_mark_last_busy(&data
->client
->dev
);
270 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
274 dev_err(&data
->client
->dev
,
275 "failed to change power state to %d\n", on
);
277 pm_runtime_put_noidle(&data
->client
->dev
);
286 static int bmc150_magn_get_odr(struct bmc150_magn_data
*data
, int *val
)
291 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_OPMODE_ODR
, ®_val
);
294 odr_val
= (reg_val
& BMC150_MAGN_MASK_ODR
) >> BMC150_MAGN_SHIFT_ODR
;
296 for (i
= 0; i
< ARRAY_SIZE(bmc150_magn_samp_freq_table
); i
++)
297 if (bmc150_magn_samp_freq_table
[i
].reg_val
== odr_val
) {
298 *val
= bmc150_magn_samp_freq_table
[i
].freq
;
305 static int bmc150_magn_set_odr(struct bmc150_magn_data
*data
, int val
)
310 for (i
= 0; i
< ARRAY_SIZE(bmc150_magn_samp_freq_table
); i
++) {
311 if (bmc150_magn_samp_freq_table
[i
].freq
== val
) {
312 ret
= regmap_update_bits(data
->regmap
,
313 BMC150_MAGN_REG_OPMODE_ODR
,
314 BMC150_MAGN_MASK_ODR
,
315 bmc150_magn_samp_freq_table
[i
].
317 BMC150_MAGN_SHIFT_ODR
);
327 static int bmc150_magn_set_max_odr(struct bmc150_magn_data
*data
, int rep_xy
,
330 int ret
, reg_val
, max_odr
;
333 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_REP_XY
,
337 rep_xy
= BMC150_MAGN_REGVAL_TO_REPXY(reg_val
);
340 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_REP_Z
,
344 rep_z
= BMC150_MAGN_REGVAL_TO_REPZ(reg_val
);
347 ret
= bmc150_magn_get_odr(data
, &odr
);
351 /* the maximum selectable read-out frequency from datasheet */
352 max_odr
= 1000000 / (145 * rep_xy
+ 500 * rep_z
+ 980);
354 dev_err(&data
->client
->dev
,
355 "Can't set oversampling with sampling freq %d\n",
359 data
->max_odr
= max_odr
;
364 static s32
bmc150_magn_compensate_x(struct bmc150_magn_trim_regs
*tregs
, s16 x
,
368 u16 xyz1
= le16_to_cpu(tregs
->xyz1
);
370 if (x
== BMC150_MAGN_XY_OVERFLOW_VAL
)
376 val
= ((s16
)(((u16
)((((s32
)xyz1
) << 14) / rhall
)) - ((u16
)0x4000)));
377 val
= ((s16
)((((s32
)x
) * ((((((((s32
)tregs
->xy2
) * ((((s32
)val
) *
378 ((s32
)val
)) >> 7)) + (((s32
)val
) *
379 ((s32
)(((s16
)tregs
->xy1
) << 7)))) >> 9) + ((s32
)0x100000)) *
380 ((s32
)(((s16
)tregs
->x2
) + ((s16
)0xA0)))) >> 12)) >> 13)) +
381 (((s16
)tregs
->x1
) << 3);
386 static s32
bmc150_magn_compensate_y(struct bmc150_magn_trim_regs
*tregs
, s16 y
,
390 u16 xyz1
= le16_to_cpu(tregs
->xyz1
);
392 if (y
== BMC150_MAGN_XY_OVERFLOW_VAL
)
398 val
= ((s16
)(((u16
)((((s32
)xyz1
) << 14) / rhall
)) - ((u16
)0x4000)));
399 val
= ((s16
)((((s32
)y
) * ((((((((s32
)tregs
->xy2
) * ((((s32
)val
) *
400 ((s32
)val
)) >> 7)) + (((s32
)val
) *
401 ((s32
)(((s16
)tregs
->xy1
) << 7)))) >> 9) + ((s32
)0x100000)) *
402 ((s32
)(((s16
)tregs
->y2
) + ((s16
)0xA0)))) >> 12)) >> 13)) +
403 (((s16
)tregs
->y1
) << 3);
408 static s32
bmc150_magn_compensate_z(struct bmc150_magn_trim_regs
*tregs
, s16 z
,
412 u16 xyz1
= le16_to_cpu(tregs
->xyz1
);
413 u16 z1
= le16_to_cpu(tregs
->z1
);
414 s16 z2
= le16_to_cpu(tregs
->z2
);
415 s16 z3
= le16_to_cpu(tregs
->z3
);
416 s16 z4
= le16_to_cpu(tregs
->z4
);
418 if (z
== BMC150_MAGN_Z_OVERFLOW_VAL
)
421 val
= (((((s32
)(z
- z4
)) << 15) - ((((s32
)z3
) * ((s32
)(((s16
)rhall
) -
422 ((s16
)xyz1
)))) >> 2)) / (z2
+ ((s16
)(((((s32
)z1
) *
423 ((((s16
)rhall
) << 1))) + (1 << 15)) >> 16))));
428 static int bmc150_magn_read_xyz(struct bmc150_magn_data
*data
, s32
*buffer
)
431 __le16 values
[AXIS_XYZR_MAX
];
432 s16 raw_x
, raw_y
, raw_z
;
434 struct bmc150_magn_trim_regs tregs
;
436 ret
= regmap_bulk_read(data
->regmap
, BMC150_MAGN_REG_X_L
,
437 values
, sizeof(values
));
441 raw_x
= (s16
)le16_to_cpu(values
[AXIS_X
]) >> BMC150_MAGN_SHIFT_XY_L
;
442 raw_y
= (s16
)le16_to_cpu(values
[AXIS_Y
]) >> BMC150_MAGN_SHIFT_XY_L
;
443 raw_z
= (s16
)le16_to_cpu(values
[AXIS_Z
]) >> BMC150_MAGN_SHIFT_Z_L
;
444 rhall
= le16_to_cpu(values
[RHALL
]) >> BMC150_MAGN_SHIFT_RHALL_L
;
446 ret
= regmap_bulk_read(data
->regmap
, BMC150_MAGN_REG_TRIM_START
,
447 &tregs
, sizeof(tregs
));
451 buffer
[AXIS_X
] = bmc150_magn_compensate_x(&tregs
, raw_x
, rhall
);
452 buffer
[AXIS_Y
] = bmc150_magn_compensate_y(&tregs
, raw_y
, rhall
);
453 buffer
[AXIS_Z
] = bmc150_magn_compensate_z(&tregs
, raw_z
, rhall
);
458 static int bmc150_magn_read_raw(struct iio_dev
*indio_dev
,
459 struct iio_chan_spec
const *chan
,
460 int *val
, int *val2
, long mask
)
462 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
464 s32 values
[AXIS_XYZ_MAX
];
467 case IIO_CHAN_INFO_RAW
:
468 if (iio_buffer_enabled(indio_dev
))
470 mutex_lock(&data
->mutex
);
472 ret
= bmc150_magn_set_power_state(data
, true);
474 mutex_unlock(&data
->mutex
);
478 ret
= bmc150_magn_read_xyz(data
, values
);
480 bmc150_magn_set_power_state(data
, false);
481 mutex_unlock(&data
->mutex
);
484 *val
= values
[chan
->scan_index
];
486 ret
= bmc150_magn_set_power_state(data
, false);
488 mutex_unlock(&data
->mutex
);
492 mutex_unlock(&data
->mutex
);
494 case IIO_CHAN_INFO_SCALE
:
496 * The API/driver performs an off-chip temperature
497 * compensation and outputs x/y/z magnetic field data in
498 * 16 LSB/uT to the upper application layer.
502 return IIO_VAL_INT_PLUS_MICRO
;
503 case IIO_CHAN_INFO_SAMP_FREQ
:
504 ret
= bmc150_magn_get_odr(data
, val
);
508 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
509 switch (chan
->channel2
) {
512 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_REP_XY
,
516 *val
= BMC150_MAGN_REGVAL_TO_REPXY(tmp
);
519 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_REP_Z
,
523 *val
= BMC150_MAGN_REGVAL_TO_REPZ(tmp
);
533 static int bmc150_magn_write_raw(struct iio_dev
*indio_dev
,
534 struct iio_chan_spec
const *chan
,
535 int val
, int val2
, long mask
)
537 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
541 case IIO_CHAN_INFO_SAMP_FREQ
:
542 if (val
> data
->max_odr
)
544 mutex_lock(&data
->mutex
);
545 ret
= bmc150_magn_set_odr(data
, val
);
546 mutex_unlock(&data
->mutex
);
548 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
549 switch (chan
->channel2
) {
552 if (val
< 1 || val
> 511)
554 mutex_lock(&data
->mutex
);
555 ret
= bmc150_magn_set_max_odr(data
, val
, 0, 0);
557 mutex_unlock(&data
->mutex
);
560 ret
= regmap_update_bits(data
->regmap
,
561 BMC150_MAGN_REG_REP_XY
,
563 BMC150_MAGN_REPXY_TO_REGVAL
565 mutex_unlock(&data
->mutex
);
568 if (val
< 1 || val
> 256)
570 mutex_lock(&data
->mutex
);
571 ret
= bmc150_magn_set_max_odr(data
, 0, val
, 0);
573 mutex_unlock(&data
->mutex
);
576 ret
= regmap_update_bits(data
->regmap
,
577 BMC150_MAGN_REG_REP_Z
,
579 BMC150_MAGN_REPZ_TO_REGVAL
581 mutex_unlock(&data
->mutex
);
591 static int bmc150_magn_validate_trigger(struct iio_dev
*indio_dev
,
592 struct iio_trigger
*trig
)
594 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
596 if (data
->dready_trig
!= trig
)
602 static ssize_t
bmc150_magn_show_samp_freq_avail(struct device
*dev
,
603 struct device_attribute
*attr
,
606 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
607 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
611 for (i
= 0; i
< ARRAY_SIZE(bmc150_magn_samp_freq_table
); i
++) {
612 if (bmc150_magn_samp_freq_table
[i
].freq
> data
->max_odr
)
614 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ",
615 bmc150_magn_samp_freq_table
[i
].freq
);
617 /* replace last space with a newline */
623 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail
);
625 static struct attribute
*bmc150_magn_attributes
[] = {
626 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
630 static const struct attribute_group bmc150_magn_attrs_group
= {
631 .attrs
= bmc150_magn_attributes
,
634 #define BMC150_MAGN_CHANNEL(_axis) { \
637 .channel2 = IIO_MOD_##_axis, \
638 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
639 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
640 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
641 BIT(IIO_CHAN_INFO_SCALE), \
642 .scan_index = AXIS_##_axis, \
647 .endianness = IIO_LE \
651 static const struct iio_chan_spec bmc150_magn_channels
[] = {
652 BMC150_MAGN_CHANNEL(X
),
653 BMC150_MAGN_CHANNEL(Y
),
654 BMC150_MAGN_CHANNEL(Z
),
655 IIO_CHAN_SOFT_TIMESTAMP(3),
658 static const struct iio_info bmc150_magn_info
= {
659 .attrs
= &bmc150_magn_attrs_group
,
660 .read_raw
= bmc150_magn_read_raw
,
661 .write_raw
= bmc150_magn_write_raw
,
662 .validate_trigger
= bmc150_magn_validate_trigger
,
663 .driver_module
= THIS_MODULE
,
666 static const unsigned long bmc150_magn_scan_masks
[] = {0x07, 0};
668 static irqreturn_t
bmc150_magn_trigger_handler(int irq
, void *p
)
670 struct iio_poll_func
*pf
= p
;
671 struct iio_dev
*indio_dev
= pf
->indio_dev
;
672 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
675 mutex_lock(&data
->mutex
);
676 ret
= bmc150_magn_read_xyz(data
, data
->buffer
);
677 mutex_unlock(&data
->mutex
);
681 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
685 iio_trigger_notify_done(data
->dready_trig
);
690 static int bmc150_magn_init(struct bmc150_magn_data
*data
)
693 struct bmc150_magn_preset preset
;
695 ret
= bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SUSPEND
,
698 dev_err(&data
->client
->dev
,
699 "Failed to bring up device from suspend mode\n");
703 ret
= regmap_read(data
->regmap
, BMC150_MAGN_REG_CHIP_ID
, &chip_id
);
705 dev_err(&data
->client
->dev
, "Failed reading chip id\n");
708 if (chip_id
!= BMC150_MAGN_CHIP_ID_VAL
) {
709 dev_err(&data
->client
->dev
, "Invalid chip id 0x%x\n", ret
);
713 dev_dbg(&data
->client
->dev
, "Chip id %x\n", ret
);
715 preset
= bmc150_magn_presets_table
[BMC150_MAGN_DEFAULT_PRESET
];
716 ret
= bmc150_magn_set_odr(data
, preset
.odr
);
718 dev_err(&data
->client
->dev
, "Failed to set ODR to %d\n",
723 ret
= regmap_write(data
->regmap
, BMC150_MAGN_REG_REP_XY
,
724 BMC150_MAGN_REPXY_TO_REGVAL(preset
.rep_xy
));
726 dev_err(&data
->client
->dev
, "Failed to set REP XY to %d\n",
731 ret
= regmap_write(data
->regmap
, BMC150_MAGN_REG_REP_Z
,
732 BMC150_MAGN_REPZ_TO_REGVAL(preset
.rep_z
));
734 dev_err(&data
->client
->dev
, "Failed to set REP Z to %d\n",
739 ret
= bmc150_magn_set_max_odr(data
, preset
.rep_xy
, preset
.rep_z
,
744 ret
= bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_NORMAL
,
747 dev_err(&data
->client
->dev
, "Failed to power on device\n");
754 bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SUSPEND
, true);
758 static int bmc150_magn_reset_intr(struct bmc150_magn_data
*data
)
763 * Data Ready (DRDY) is always cleared after
764 * readout of data registers ends.
766 return regmap_read(data
->regmap
, BMC150_MAGN_REG_X_L
, &tmp
);
769 static int bmc150_magn_trig_try_reen(struct iio_trigger
*trig
)
771 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
772 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
775 if (!data
->dready_trigger_on
)
778 mutex_lock(&data
->mutex
);
779 ret
= bmc150_magn_reset_intr(data
);
780 mutex_unlock(&data
->mutex
);
785 static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
788 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
789 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
792 mutex_lock(&data
->mutex
);
793 if (state
== data
->dready_trigger_on
)
796 ret
= bmc150_magn_set_power_state(data
, state
);
800 ret
= regmap_update_bits(data
->regmap
, BMC150_MAGN_REG_INT_DRDY
,
801 BMC150_MAGN_MASK_DRDY_EN
,
802 state
<< BMC150_MAGN_SHIFT_DRDY_EN
);
806 data
->dready_trigger_on
= state
;
809 ret
= bmc150_magn_reset_intr(data
);
813 mutex_unlock(&data
->mutex
);
818 bmc150_magn_set_power_state(data
, false);
820 mutex_unlock(&data
->mutex
);
824 static const struct iio_trigger_ops bmc150_magn_trigger_ops
= {
825 .set_trigger_state
= bmc150_magn_data_rdy_trigger_set_state
,
826 .try_reenable
= bmc150_magn_trig_try_reen
,
827 .owner
= THIS_MODULE
,
830 static int bmc150_magn_gpio_probe(struct i2c_client
*client
)
833 struct gpio_desc
*gpio
;
841 /* data ready GPIO interrupt pin */
842 gpio
= devm_gpiod_get_index(dev
, BMC150_MAGN_GPIO_INT
, 0, GPIOD_IN
);
844 dev_err(dev
, "ACPI GPIO get index failed\n");
845 return PTR_ERR(gpio
);
848 ret
= gpiod_to_irq(gpio
);
850 dev_dbg(dev
, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio
), ret
);
855 static const char *bmc150_magn_match_acpi_device(struct device
*dev
)
857 const struct acpi_device_id
*id
;
859 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
863 return dev_name(dev
);
866 static int bmc150_magn_probe(struct i2c_client
*client
,
867 const struct i2c_device_id
*id
)
869 struct bmc150_magn_data
*data
;
870 struct iio_dev
*indio_dev
;
871 const char *name
= NULL
;
874 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
878 data
= iio_priv(indio_dev
);
879 i2c_set_clientdata(client
, indio_dev
);
880 data
->client
= client
;
884 else if (ACPI_HANDLE(&client
->dev
))
885 name
= bmc150_magn_match_acpi_device(&client
->dev
);
889 mutex_init(&data
->mutex
);
890 data
->regmap
= devm_regmap_init_i2c(client
, &bmc150_magn_regmap_config
);
891 if (IS_ERR(data
->regmap
)) {
892 dev_err(&client
->dev
, "Failed to allocate register map\n");
893 return PTR_ERR(data
->regmap
);
896 ret
= bmc150_magn_init(data
);
900 indio_dev
->dev
.parent
= &client
->dev
;
901 indio_dev
->channels
= bmc150_magn_channels
;
902 indio_dev
->num_channels
= ARRAY_SIZE(bmc150_magn_channels
);
903 indio_dev
->available_scan_masks
= bmc150_magn_scan_masks
;
904 indio_dev
->name
= name
;
905 indio_dev
->modes
= INDIO_DIRECT_MODE
;
906 indio_dev
->info
= &bmc150_magn_info
;
908 if (client
->irq
<= 0)
909 client
->irq
= bmc150_magn_gpio_probe(client
);
911 if (client
->irq
> 0) {
912 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
916 if (!data
->dready_trig
) {
918 dev_err(&client
->dev
, "iio trigger alloc failed\n");
922 data
->dready_trig
->dev
.parent
= &client
->dev
;
923 data
->dready_trig
->ops
= &bmc150_magn_trigger_ops
;
924 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
925 ret
= iio_trigger_register(data
->dready_trig
);
927 dev_err(&client
->dev
, "iio trigger register failed\n");
931 ret
= iio_triggered_buffer_setup(indio_dev
,
932 &iio_pollfunc_store_time
,
933 bmc150_magn_trigger_handler
,
936 dev_err(&client
->dev
,
937 "iio triggered buffer setup failed\n");
938 goto err_trigger_unregister
;
941 ret
= request_threaded_irq(client
->irq
,
942 iio_trigger_generic_data_rdy_poll
,
944 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
945 BMC150_MAGN_IRQ_NAME
,
948 dev_err(&client
->dev
, "request irq %d failed\n",
950 goto err_buffer_cleanup
;
954 ret
= iio_device_register(indio_dev
);
956 dev_err(&client
->dev
, "unable to register iio device\n");
960 ret
= pm_runtime_set_active(&client
->dev
);
962 goto err_iio_unregister
;
964 pm_runtime_enable(&client
->dev
);
965 pm_runtime_set_autosuspend_delay(&client
->dev
,
966 BMC150_MAGN_AUTO_SUSPEND_DELAY_MS
);
967 pm_runtime_use_autosuspend(&client
->dev
);
969 dev_dbg(&indio_dev
->dev
, "Registered device %s\n", name
);
974 iio_device_unregister(indio_dev
);
977 free_irq(client
->irq
, data
->dready_trig
);
979 if (data
->dready_trig
)
980 iio_triggered_buffer_cleanup(indio_dev
);
981 err_trigger_unregister
:
982 if (data
->dready_trig
)
983 iio_trigger_unregister(data
->dready_trig
);
985 bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SUSPEND
, true);
989 static int bmc150_magn_remove(struct i2c_client
*client
)
991 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
992 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
994 pm_runtime_disable(&client
->dev
);
995 pm_runtime_set_suspended(&client
->dev
);
996 pm_runtime_put_noidle(&client
->dev
);
998 iio_device_unregister(indio_dev
);
1000 if (client
->irq
> 0)
1001 free_irq(data
->client
->irq
, data
->dready_trig
);
1003 if (data
->dready_trig
) {
1004 iio_triggered_buffer_cleanup(indio_dev
);
1005 iio_trigger_unregister(data
->dready_trig
);
1008 mutex_lock(&data
->mutex
);
1009 bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SUSPEND
, true);
1010 mutex_unlock(&data
->mutex
);
1016 static int bmc150_magn_runtime_suspend(struct device
*dev
)
1018 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1019 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
1022 mutex_lock(&data
->mutex
);
1023 ret
= bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SLEEP
,
1025 mutex_unlock(&data
->mutex
);
1027 dev_err(&data
->client
->dev
, "powering off device failed\n");
1033 static int bmc150_magn_runtime_resume(struct device
*dev
)
1035 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1036 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
1038 return bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_NORMAL
,
1043 #ifdef CONFIG_PM_SLEEP
1044 static int bmc150_magn_suspend(struct device
*dev
)
1046 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1047 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
1050 mutex_lock(&data
->mutex
);
1051 ret
= bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_SLEEP
,
1053 mutex_unlock(&data
->mutex
);
1058 static int bmc150_magn_resume(struct device
*dev
)
1060 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1061 struct bmc150_magn_data
*data
= iio_priv(indio_dev
);
1064 mutex_lock(&data
->mutex
);
1065 ret
= bmc150_magn_set_power_mode(data
, BMC150_MAGN_POWER_MODE_NORMAL
,
1067 mutex_unlock(&data
->mutex
);
1073 static const struct dev_pm_ops bmc150_magn_pm_ops
= {
1074 SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend
, bmc150_magn_resume
)
1075 SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend
,
1076 bmc150_magn_runtime_resume
, NULL
)
1079 static const struct acpi_device_id bmc150_magn_acpi_match
[] = {
1083 MODULE_DEVICE_TABLE(acpi
, bmc150_magn_acpi_match
);
1085 static const struct i2c_device_id bmc150_magn_id
[] = {
1089 MODULE_DEVICE_TABLE(i2c
, bmc150_magn_id
);
1091 static struct i2c_driver bmc150_magn_driver
= {
1093 .name
= BMC150_MAGN_DRV_NAME
,
1094 .acpi_match_table
= ACPI_PTR(bmc150_magn_acpi_match
),
1095 .pm
= &bmc150_magn_pm_ops
,
1097 .probe
= bmc150_magn_probe
,
1098 .remove
= bmc150_magn_remove
,
1099 .id_table
= bmc150_magn_id
,
1101 module_i2c_driver(bmc150_magn_driver
);
1103 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1104 MODULE_LICENSE("GPL v2");
1105 MODULE_DESCRIPTION("BMC150 magnetometer driver");