1 /* sensor_bmm150.h - header file for BMM150 Geomagnetic sensor driver */ 2 3 /* 4 * Copyright (c) 2017 Intel Corporation 5 * 6 * SPDX-License-Identifier: Apache-2.0 7 */ 8 9 #ifndef ZEPHYR_DRIVERS_SENSOR_BMM150_BMM150_H_ 10 #define ZEPHYR_DRIVERS_SENSOR_BMM150_BMM150_H_ 11 12 #include <zephyr/types.h> 13 #include <zephyr/device.h> 14 #include <zephyr/devicetree.h> 15 #include <zephyr/drivers/spi.h> 16 #include <zephyr/drivers/i2c.h> 17 18 #define DT_DRV_COMPAT bosch_bmm150 19 20 #define BMM150_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 21 #define BMM150_BUS_I2C DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) 22 23 union bmm150_bus { 24 #if BMM150_BUS_SPI 25 struct spi_dt_spec spi; 26 #endif 27 #if BMM150_BUS_I2C 28 struct i2c_dt_spec i2c; 29 #endif 30 }; 31 32 typedef int (*bmm150_bus_check_fn)(const union bmm150_bus *bus); 33 typedef int (*bmm150_reg_read_fn)(const union bmm150_bus *bus, 34 uint8_t start, uint8_t *buf, int size); 35 typedef int (*bmm150_reg_write_fn)(const union bmm150_bus *bus, 36 uint8_t reg, uint8_t val); 37 38 struct bmm150_bus_io { 39 bmm150_bus_check_fn check; 40 bmm150_reg_read_fn read; 41 bmm150_reg_write_fn write; 42 }; 43 44 #if BMM150_BUS_SPI 45 #define BMM150_SPI_OPERATION (SPI_WORD_SET(8) | SPI_TRANSFER_MSB | \ 46 SPI_MODE_CPOL | SPI_MODE_CPHA) 47 extern const struct bmm150_bus_io bmm150_bus_io_spi; 48 #endif 49 50 #if BMM150_BUS_I2C 51 extern const struct bmm150_bus_io bmm150_bus_io_i2c; 52 #endif 53 54 #include <zephyr/types.h> 55 #include <zephyr/drivers/i2c.h> 56 #include <stdint.h> 57 #include <zephyr/sys/util.h> 58 59 #include <zephyr/kernel.h> 60 #include <zephyr/device.h> 61 #include <zephyr/drivers/sensor.h> 62 #include <zephyr/sys/byteorder.h> 63 #include <zephyr/sys/__assert.h> 64 #include <zephyr/drivers/gpio.h> 65 66 #define BMM150_REG_CHIP_ID 0x40 67 #define BMM150_CHIP_ID_VAL 0x32 68 69 #define BMM150_REG_X_L 0x42 70 #define BMM150_REG_X_M 0x43 71 #define BMM150_REG_Y_L 0x44 72 #define BMM150_REG_Y_M 0x45 73 #define BMM150_SHIFT_XY_L 3 74 #define BMM150_REG_Z_L 0x46 75 #define BMM150_REG_Z_M 0x47 76 #define BMM150_SHIFT_Z_L 1 77 #define BMM150_REG_RHALL_L 0x48 78 #define BMM150_REG_RHALL_M 0x49 79 #define BMM150_SHIFT_RHALL_L 2 80 81 #define BMM150_REG_INT_STATUS 0x4A 82 83 #define BMM150_REG_POWER 0x4B 84 #define BMM150_MASK_POWER_CTL BIT(0) 85 #define BMM150_MASK_SOFT_RESET (BIT(1) | BIT(7)) 86 #define BMM150_SOFT_RESET 0x81 87 88 #define BMM150_REG_OPMODE_ODR 0x4C 89 #define BMM150_MASK_OPMODE (BIT(2) | BIT(1)) 90 #define BMM150_SHIFT_OPMODE 1 91 #define BMM150_MODE_NORMAL 0x00 92 #define BMM150_MODE_FORCED 0x01 93 #define BMM150_MODE_SLEEP 0x03 94 #define BMM150_MASK_ODR (BIT(5) | BIT(4) | BIT(3)) 95 #define BMM150_SHIFT_ODR 3 96 97 #define BMM150_REG_LOW_THRESH 0x4F 98 #define BMM150_REG_HIGH_THRESH 0x50 99 #define BMM150_REG_REP_XY 0x51 100 #define BMM150_REG_REP_Z 0x52 101 #define BMM150_REG_REP_DATAMASK 0xFF 102 103 #define BMM150_REG_TRIM_START 0x5D 104 #define BMM150_REG_TRIM_END 0x71 105 106 #define BMM150_XY_OVERFLOW_VAL -4096 107 #define BMM150_Z_OVERFLOW_VAL -16384 108 109 #define BMM150_REGVAL_TO_REPXY(regval) (((regval) * 2) + 1) 110 #define BMM150_REGVAL_TO_REPZ(regval) ((regval) + 1) 111 #define BMM150_REPXY_TO_REGVAL(rep) (((rep) - 1) / 2) 112 #define BMM150_REPZ_TO_REGVAL(rep) BMM150_REPXY_TO_REGVAL(rep) 113 114 #define BMM150_REG_INT 0x4D 115 116 #define BMM150_REG_INT_DRDY 0x4E 117 #define BMM150_MASK_DRDY_EN BIT(7) 118 #define BMM150_SHIFT_DRDY_EN 7 119 #define BMM150_DRDY_INT3 BIT(6) 120 #define BMM150_MASK_DRDY_Z_EN BIT(5) 121 #define BMM150_MASK_DRDY_Y_EN BIT(4) 122 #define BMM150_MASK_DRDY_X_EN BIT(3) 123 #define BMM150_MASK_DRDY_DR_POLARITY BIT(2) 124 #define BMM150_SHIFT_DRDY_DR_POLARITY 2 125 #define BMM150_MASK_DRDY_LATCHING BIT(1) 126 #define BMM150_MASK_DRDY_INT3_POLARITY BIT(0) 127 128 #if defined(CONFIG_BMM150_SAMPLING_REP_XY) || \ 129 defined(CONFIG_BMM150_SAMPLING_REP_Z) 130 #define BMM150_SET_ATTR_REP 131 #endif 132 133 #if defined(CONFIG_BMM150_SAMPLING_RATE_RUNTIME) || \ 134 defined(BMM150_MAGN_SET_ATTR_REP) 135 #define BMM150_MAGN_SET_ATTR 136 #endif 137 138 struct bmm150_trim_regs { 139 int8_t x1; 140 int8_t y1; 141 uint16_t reserved1; 142 uint8_t reserved2; 143 int16_t z4; 144 int8_t x2; 145 int8_t y2; 146 uint16_t reserved3; 147 int16_t z2; 148 uint16_t z1; 149 uint16_t xyz1; 150 int16_t z3; 151 int8_t xy2; 152 uint8_t xy1; 153 } __packed; 154 155 struct bmm150_config { 156 union bmm150_bus bus; 157 const struct bmm150_bus_io *bus_io; 158 }; 159 160 struct bmm150_data { 161 struct k_sem sem; 162 struct bmm150_trim_regs tregs; 163 int rep_xy, rep_z, odr, max_odr; 164 int sample_x, sample_y, sample_z; 165 }; 166 167 enum bmm150_power_modes { 168 BMM150_POWER_MODE_SUSPEND, 169 BMM150_POWER_MODE_SLEEP, 170 BMM150_POWER_MODE_NORMAL 171 }; 172 173 enum bmm150_axis { 174 BMM150_AXIS_X, 175 BMM150_AXIS_Y, 176 BMM150_AXIS_Z, 177 BMM150_RHALL, 178 BMM150_AXIS_XYZ_MAX = BMM150_RHALL, 179 BMM150_AXIS_XYZR_MAX 180 }; 181 182 enum bmm150_presets { 183 BMM150_LOW_POWER_PRESET, 184 BMM150_REGULAR_PRESET, 185 BMM150_ENHANCED_REGULAR_PRESET, 186 BMM150_HIGH_ACCURACY_PRESET 187 }; 188 189 #if defined(CONFIG_BMM150_PRESET_LOW_POWER) 190 #define BMM150_DEFAULT_PRESET BMM150_LOW_POWER_PRESET 191 #elif defined(CONFIG_BMM150_PRESET_REGULAR) 192 #define BMM150_DEFAULT_PRESET BMM150_REGULAR_PRESET 193 #elif defined(CONFIG_BMM150_PRESET_ENHANCED_REGULAR) 194 #define BMM150_DEFAULT_PRESET BMM150_ENHANCED_REGULAR_PRESET 195 #elif defined(CONFIG_BMM150_PRESET_HIGH_ACCURACY) 196 #define BMM150_DEFAULT_PRESET BMM150_HIGH_ACCURACY_PRESET 197 #endif 198 199 int bmm150_reg_update_byte(const struct device *dev, uint8_t reg, 200 uint8_t mask, uint8_t value); 201 202 #endif /* __SENSOR_BMM150_H__ */ 203