| 1 |
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (c) 2025 Robert Bosch GmbH. |
| 4 |
*/ |
| 5 |
#include <linux/bitfield.h> |
| 6 |
#include <linux/delay.h> |
| 7 |
#include <linux/interrupt.h> |
| 8 |
#include <linux/irq.h> |
| 9 |
#include <linux/module.h> |
| 10 |
#include <linux/property.h> |
| 11 |
#include <linux/regmap.h> |
| 12 |
#include <linux/string.h> |
| 13 |
#include <linux/units.h> |
| 14 |
|
| 15 |
#include <linux/iio/buffer.h> |
| 16 |
#include <linux/iio/iio.h> |
| 17 |
#include <linux/iio/trigger.h> |
| 18 |
#include <linux/iio/trigger_consumer.h> |
| 19 |
#include <linux/iio/triggered_buffer.h> |
| 20 |
|
| 21 |
#include "smi330.h" |
| 22 |
|
| 23 |
/* Register map */ |
| 24 |
#define SMI330_CHIP_ID_REG 0x00 |
| 25 |
#define SMI330_ERR_REG 0x01 |
| 26 |
#define SMI330_STATUS_REG 0x02 |
| 27 |
#define SMI330_ACCEL_X_REG 0x03 |
| 28 |
#define SMI330_GYRO_X_REG 0x06 |
| 29 |
#define SMI330_TEMP_REG 0x09 |
| 30 |
#define SMI330_INT1_STATUS_REG 0x0D |
| 31 |
#define SMI330_ACCEL_CFG_REG 0x20 |
| 32 |
#define SMI330_GYRO_CFG_REG 0x21 |
| 33 |
#define SMI330_IO_INT_CTRL_REG 0x38 |
| 34 |
#define SMI330_INT_CONF_REG 0x39 |
| 35 |
#define SMI330_INT_MAP1_REG 0x3A |
| 36 |
#define SMI330_INT_MAP2_REG 0x3B |
| 37 |
#define SMI330_CMD_REG 0x7E |
| 38 |
|
| 39 |
/* Register mask */ |
| 40 |
#define SMI330_CHIP_ID_MASK GENMASK(7, 0) |
| 41 |
#define SMI330_ERR_FATAL_MASK BIT(0) |
| 42 |
#define SMI330_ERR_ACC_CONF_MASK BIT(5) |
| 43 |
#define SMI330_ERR_GYR_CONF_MASK BIT(6) |
| 44 |
#define SMI330_STATUS_POR_MASK BIT(0) |
| 45 |
#define SMI330_INT_STATUS_ACC_GYR_DRDY_MASK GENMASK(13, 12) |
| 46 |
#define SMI330_CFG_ODR_MASK GENMASK(3, 0) |
| 47 |
#define SMI330_CFG_RANGE_MASK GENMASK(6, 4) |
| 48 |
#define SMI330_CFG_BW_MASK BIT(7) |
| 49 |
#define SMI330_CFG_AVG_NUM_MASK GENMASK(10, 8) |
| 50 |
#define SMI330_CFG_MODE_MASK GENMASK(14, 12) |
| 51 |
#define SMI330_IO_INT_CTRL_INT1_MASK GENMASK(2, 0) |
| 52 |
#define SMI330_IO_INT_CTRL_INT2_MASK GENMASK(10, 8) |
| 53 |
#define SMI330_INT_CONF_LATCH_MASK BIT(0) |
| 54 |
#define SMI330_INT_MAP2_ACC_DRDY_MASK GENMASK(11, 10) |
| 55 |
#define SMI330_INT_MAP2_GYR_DRDY_MASK GENMASK(9, 8) |
| 56 |
|
| 57 |
/* Register values */ |
| 58 |
#define SMI330_IO_INT_CTRL_LVL BIT(0) |
| 59 |
#define SMI330_IO_INT_CTRL_OD BIT(1) |
| 60 |
#define SMI330_IO_INT_CTRL_EN BIT(2) |
| 61 |
#define SMI330_CMD_SOFT_RESET 0xDEAF |
| 62 |
|
| 63 |
/* T°C = (temp / 512) + 23 */ |
| 64 |
#define SMI330_TEMP_OFFSET 11776 /* 23 * 512 */ |
| 65 |
#define SMI330_TEMP_SCALE 1953125 /* (1 / 512) * 1e9 */ |
| 66 |
|
| 67 |
#define SMI330_CHIP_ID 0x42 |
| 68 |
#define SMI330_SOFT_RESET_DELAY 2000 |
| 69 |
|
| 70 |
#define SMI330_ACCEL_CHANNEL(_axis) { \ |
| 71 |
.type = IIO_ACCEL, \ |
| 72 |
.modified = 1, \ |
| 73 |
.channel2 = IIO_MOD_##_axis, \ |
| 74 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 75 |
.info_mask_shared_by_type = \ |
| 76 |
BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ |
| 77 |
BIT(IIO_CHAN_INFO_SCALE) | \ |
| 78 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | \ |
| 79 |
BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ |
| 80 |
.info_mask_shared_by_type_available = \ |
| 81 |
BIT(IIO_CHAN_INFO_SCALE) | \ |
| 82 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | \ |
| 83 |
BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ |
| 84 |
.info_mask_shared_by_dir_available = \ |
| 85 |
BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 86 |
.scan_index = SMI330_SCAN_ACCEL_##_axis, \ |
| 87 |
.scan_type = { \ |
| 88 |
.sign = 's', \ |
| 89 |
.realbits = 16, \ |
| 90 |
.storagebits = 16, \ |
| 91 |
.endianness = IIO_LE, \ |
| 92 |
}, \ |
| 93 |
} |
| 94 |
|
| 95 |
#define SMI330_GYRO_CHANNEL(_axis) { \ |
| 96 |
.type = IIO_ANGL_VEL, \ |
| 97 |
.modified = 1, \ |
| 98 |
.channel2 = IIO_MOD_##_axis, \ |
| 99 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 100 |
.info_mask_shared_by_type = \ |
| 101 |
BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ |
| 102 |
BIT(IIO_CHAN_INFO_SCALE) | \ |
| 103 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | \ |
| 104 |
BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ |
| 105 |
.info_mask_shared_by_type_available = \ |
| 106 |
BIT(IIO_CHAN_INFO_SCALE) | \ |
| 107 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | \ |
| 108 |
BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ |
| 109 |
.info_mask_shared_by_dir_available = \ |
| 110 |
BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 111 |
.scan_index = SMI330_SCAN_GYRO_##_axis, \ |
| 112 |
.scan_type = { \ |
| 113 |
.sign = 's', \ |
| 114 |
.realbits = 16, \ |
| 115 |
.storagebits = 16, \ |
| 116 |
.endianness = IIO_LE, \ |
| 117 |
}, \ |
| 118 |
} |
| 119 |
|
| 120 |
#define SMI330_TEMP_CHANNEL(_index) { \ |
| 121 |
.type = IIO_TEMP, \ |
| 122 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 123 |
BIT(IIO_CHAN_INFO_OFFSET) | \ |
| 124 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 125 |
.scan_index = _index, \ |
| 126 |
.scan_type = { \ |
| 127 |
.sign = 's', \ |
| 128 |
.realbits = 16, \ |
| 129 |
.storagebits = 16, \ |
| 130 |
.endianness = IIO_LE, \ |
| 131 |
}, \ |
| 132 |
} |
| 133 |
|
| 134 |
enum smi330_accel_range { |
| 135 |
SMI330_ACCEL_RANGE_2G = 0x00, |
| 136 |
SMI330_ACCEL_RANGE_4G = 0x01, |
| 137 |
SMI330_ACCEL_RANGE_8G = 0x02, |
| 138 |
SMI330_ACCEL_RANGE_16G = 0x03 |
| 139 |
}; |
| 140 |
|
| 141 |
enum smi330_gyro_range { |
| 142 |
SMI330_GYRO_RANGE_125 = 0x0, |
| 143 |
SMI330_GYRO_RANGE_250 = 0x01, |
| 144 |
SMI330_GYRO_RANGE_500 = 0x02 |
| 145 |
}; |
| 146 |
|
| 147 |
enum smi330_odr { |
| 148 |
SMI330_ODR_12_5_HZ = 0x05, |
| 149 |
SMI330_ODR_25_HZ = 0x06, |
| 150 |
SMI330_ODR_50_HZ = 0x07, |
| 151 |
SMI330_ODR_100_HZ = 0x08, |
| 152 |
SMI330_ODR_200_HZ = 0x09, |
| 153 |
SMI330_ODR_400_HZ = 0x0A, |
| 154 |
SMI330_ODR_800_HZ = 0x0B, |
| 155 |
SMI330_ODR_1600_HZ = 0x0C, |
| 156 |
SMI330_ODR_3200_HZ = 0x0D, |
| 157 |
SMI330_ODR_6400_HZ = 0x0E |
| 158 |
}; |
| 159 |
|
| 160 |
enum smi330_avg_num { |
| 161 |
SMI330_AVG_NUM_1 = 0x00, |
| 162 |
SMI330_AVG_NUM_2 = 0x01, |
| 163 |
SMI330_AVG_NUM_4 = 0x02, |
| 164 |
SMI330_AVG_NUM_8 = 0x03, |
| 165 |
SMI330_AVG_NUM_16 = 0x04, |
| 166 |
SMI330_AVG_NUM_32 = 0x05, |
| 167 |
SMI330_AVG_NUM_64 = 0x06 |
| 168 |
}; |
| 169 |
|
| 170 |
enum smi330_mode { |
| 171 |
SMI330_MODE_SUSPEND = 0x00, |
| 172 |
SMI330_MODE_GYRO_DRIVE = 0x01, |
| 173 |
SMI330_MODE_LOW_POWER = 0x03, |
| 174 |
SMI330_MODE_NORMAL = 0x04, |
| 175 |
SMI330_MODE_HIGH_PERF = 0x07 |
| 176 |
}; |
| 177 |
|
| 178 |
enum smi330_bw { |
| 179 |
SMI330_BW_2 = 0x00, /* ODR/2 */ |
| 180 |
SMI330_BW_4 = 0x01 /* ODR/4 */ |
| 181 |
}; |
| 182 |
|
| 183 |
enum smi330_operation_mode { |
| 184 |
SMI330_POLLING, |
| 185 |
SMI330_DATA_READY, |
| 186 |
}; |
| 187 |
|
| 188 |
enum smi330_sensor { |
| 189 |
SMI330_ACCEL, |
| 190 |
SMI330_GYRO, |
| 191 |
}; |
| 192 |
|
| 193 |
enum smi330_sensor_conf_select { |
| 194 |
SMI330_ODR, |
| 195 |
SMI330_RANGE, |
| 196 |
SMI330_BW, |
| 197 |
SMI330_AVG_NUM, |
| 198 |
}; |
| 199 |
|
| 200 |
enum smi330_int_out { |
| 201 |
SMI330_INT_DISABLED, |
| 202 |
SMI330_INT_1, |
| 203 |
SMI330_INT_2, |
| 204 |
}; |
| 205 |
|
| 206 |
struct smi330_attributes { |
| 207 |
int *reg_vals; |
| 208 |
int *vals; |
| 209 |
int len; |
| 210 |
int type; |
| 211 |
int mask; |
| 212 |
}; |
| 213 |
|
| 214 |
struct smi330_cfg { |
| 215 |
enum smi330_operation_mode op_mode; |
| 216 |
enum smi330_int_out data_irq; |
| 217 |
}; |
| 218 |
|
| 219 |
struct smi330_data { |
| 220 |
struct regmap *regmap; |
| 221 |
struct smi330_cfg cfg; |
| 222 |
struct iio_trigger *trig; |
| 223 |
IIO_DECLARE_BUFFER_WITH_TS(__le16, buf, SMI330_SCAN_LEN); |
| 224 |
}; |
| 225 |
|
| 226 |
const struct regmap_config smi330_regmap_config = { |
| 227 |
.reg_bits = 8, |
| 228 |
.val_bits = 16, |
| 229 |
.val_format_endian = REGMAP_ENDIAN_LITTLE, |
| 230 |
}; |
| 231 |
EXPORT_SYMBOL_NS_GPL(smi330_regmap_config, "IIO_SMI330"); |
| 232 |
|
| 233 |
static const struct iio_chan_spec smi330_channels[] = { |
| 234 |
SMI330_ACCEL_CHANNEL(X), |
| 235 |
SMI330_ACCEL_CHANNEL(Y), |
| 236 |
SMI330_ACCEL_CHANNEL(Z), |
| 237 |
SMI330_GYRO_CHANNEL(X), |
| 238 |
SMI330_GYRO_CHANNEL(Y), |
| 239 |
SMI330_GYRO_CHANNEL(Z), |
| 240 |
SMI330_TEMP_CHANNEL(-1), /* No buffer support */ |
| 241 |
IIO_CHAN_SOFT_TIMESTAMP(SMI330_SCAN_TIMESTAMP), |
| 242 |
}; |
| 243 |
|
| 244 |
static const unsigned long smi330_avail_scan_masks[] = { |
| 245 |
(BIT(SMI330_SCAN_ACCEL_X) | BIT(SMI330_SCAN_ACCEL_Y) | |
| 246 |
BIT(SMI330_SCAN_ACCEL_Z) | BIT(SMI330_SCAN_GYRO_X) | |
| 247 |
BIT(SMI330_SCAN_GYRO_Y) | BIT(SMI330_SCAN_GYRO_Z)), |
| 248 |
0 |
| 249 |
}; |
| 250 |
|
| 251 |
static const struct smi330_attributes smi330_accel_scale_attr = { |
| 252 |
.reg_vals = (int[]){ SMI330_ACCEL_RANGE_2G, SMI330_ACCEL_RANGE_4G, |
| 253 |
SMI330_ACCEL_RANGE_8G, SMI330_ACCEL_RANGE_16G }, |
| 254 |
.vals = (int[]){ 0, 61035, 0, 122070, 0, 244140, 0, 488281 }, |
| 255 |
.len = 8, |
| 256 |
.type = IIO_VAL_INT_PLUS_NANO, |
| 257 |
.mask = SMI330_CFG_RANGE_MASK |
| 258 |
}; |
| 259 |
|
| 260 |
static const struct smi330_attributes smi330_gyro_scale_attr = { |
| 261 |
.reg_vals = (int[]){ SMI330_GYRO_RANGE_125, SMI330_GYRO_RANGE_250, |
| 262 |
SMI330_GYRO_RANGE_500 }, |
| 263 |
.vals = (int[]){ 0, 3814697, 0, 7629395, 0, 15258789 }, |
| 264 |
.len = 6, |
| 265 |
.type = IIO_VAL_INT_PLUS_NANO, |
| 266 |
.mask = SMI330_CFG_RANGE_MASK |
| 267 |
}; |
| 268 |
|
| 269 |
static const struct smi330_attributes smi330_average_attr = { |
| 270 |
.reg_vals = (int[]){ SMI330_AVG_NUM_1, SMI330_AVG_NUM_2, |
| 271 |
SMI330_AVG_NUM_4, SMI330_AVG_NUM_8, |
| 272 |
SMI330_AVG_NUM_16, SMI330_AVG_NUM_32, |
| 273 |
SMI330_AVG_NUM_64 }, |
| 274 |
.vals = (int[]){ 1, 2, 4, 8, 16, 32, 64 }, |
| 275 |
.len = 7, |
| 276 |
.type = IIO_VAL_INT, |
| 277 |
.mask = SMI330_CFG_AVG_NUM_MASK |
| 278 |
}; |
| 279 |
|
| 280 |
static const struct smi330_attributes smi330_bandwidth_attr = { |
| 281 |
.reg_vals = (int[]){ SMI330_BW_2, SMI330_BW_4 }, |
| 282 |
.vals = (int[]){ 2, 4 }, |
| 283 |
.len = 2, |
| 284 |
.type = IIO_VAL_INT, |
| 285 |
.mask = SMI330_CFG_BW_MASK |
| 286 |
}; |
| 287 |
|
| 288 |
static const struct smi330_attributes smi330_odr_attr = { |
| 289 |
.reg_vals = (int[]){ SMI330_ODR_12_5_HZ, SMI330_ODR_25_HZ, |
| 290 |
SMI330_ODR_50_HZ, SMI330_ODR_100_HZ, |
| 291 |
SMI330_ODR_200_HZ, SMI330_ODR_400_HZ, |
| 292 |
SMI330_ODR_800_HZ, SMI330_ODR_1600_HZ, |
| 293 |
SMI330_ODR_3200_HZ, SMI330_ODR_6400_HZ }, |
| 294 |
.vals = (int[]){ 12, 25, 50, 100, 200, 400, 800, 1600, 3200, 6400 }, |
| 295 |
.len = 10, |
| 296 |
.type = IIO_VAL_INT, |
| 297 |
.mask = SMI330_CFG_ODR_MASK |
| 298 |
}; |
| 299 |
|
| 300 |
static int smi330_get_attributes(enum smi330_sensor_conf_select config, |
| 301 |
enum smi330_sensor sensor, |
| 302 |
const struct smi330_attributes **attr) |
| 303 |
{ |
| 304 |
switch (config) { |
| 305 |
case SMI330_ODR: |
| 306 |
*attr = &smi330_odr_attr; |
| 307 |
return 0; |
| 308 |
case SMI330_RANGE: |
| 309 |
if (sensor == SMI330_ACCEL) |
| 310 |
*attr = &smi330_accel_scale_attr; |
| 311 |
else |
| 312 |
*attr = &smi330_gyro_scale_attr; |
| 313 |
return 0; |
| 314 |
case SMI330_BW: |
| 315 |
*attr = &smi330_bandwidth_attr; |
| 316 |
return 0; |
| 317 |
case SMI330_AVG_NUM: |
| 318 |
*attr = &smi330_average_attr; |
| 319 |
return 0; |
| 320 |
default: |
| 321 |
return -EINVAL; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
static int smi330_get_config_reg(enum smi330_sensor sensor, int *reg) |
| 326 |
{ |
| 327 |
switch (sensor) { |
| 328 |
case SMI330_ACCEL: |
| 329 |
*reg = SMI330_ACCEL_CFG_REG; |
| 330 |
return 0; |
| 331 |
case SMI330_GYRO: |
| 332 |
*reg = SMI330_GYRO_CFG_REG; |
| 333 |
return 0; |
| 334 |
default: |
| 335 |
return -EINVAL; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
static int smi330_get_sensor_config(struct smi330_data *data, |
| 340 |
enum smi330_sensor sensor, |
| 341 |
enum smi330_sensor_conf_select config, |
| 342 |
int *value) |
| 343 |
|
| 344 |
{ |
| 345 |
int ret, reg, reg_val, i; |
| 346 |
const struct smi330_attributes *attr; |
| 347 |
|
| 348 |
ret = smi330_get_config_reg(sensor, ®); |
| 349 |
if (ret) |
| 350 |
return ret; |
| 351 |
|
| 352 |
ret = regmap_read(data->regmap, reg, ®_val); |
| 353 |
if (ret) |
| 354 |
return ret; |
| 355 |
|
| 356 |
ret = smi330_get_attributes(config, sensor, &attr); |
| 357 |
if (ret) |
| 358 |
return ret; |
| 359 |
|
| 360 |
reg_val = field_get(attr->mask, reg_val); |
| 361 |
|
| 362 |
if (attr->type == IIO_VAL_INT) { |
| 363 |
for (i = 0; i < attr->len; i++) { |
| 364 |
if (attr->reg_vals[i] == reg_val) { |
| 365 |
*value = attr->vals[i]; |
| 366 |
return 0; |
| 367 |
} |
| 368 |
} |
| 369 |
} else { |
| 370 |
for (i = 0; i < attr->len / 2; i++) { |
| 371 |
if (attr->reg_vals[i] == reg_val) { |
| 372 |
*value = attr->vals[2 * i + 1]; |
| 373 |
return 0; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
return -EINVAL; |
| 379 |
} |
| 380 |
|
| 381 |
static int smi330_set_sensor_config(struct smi330_data *data, |
| 382 |
enum smi330_sensor sensor, |
| 383 |
enum smi330_sensor_conf_select config, |
| 384 |
int value) |
| 385 |
{ |
| 386 |
int ret, i, reg, reg_val, error; |
| 387 |
const struct smi330_attributes *attr; |
| 388 |
|
| 389 |
ret = smi330_get_attributes(config, sensor, &attr); |
| 390 |
if (ret) |
| 391 |
return ret; |
| 392 |
|
| 393 |
for (i = 0; i < attr->len; i++) { |
| 394 |
if (attr->vals[i] == value) { |
| 395 |
if (attr->type == IIO_VAL_INT) |
| 396 |
reg_val = attr->reg_vals[i]; |
| 397 |
else |
| 398 |
reg_val = attr->reg_vals[i / 2]; |
| 399 |
break; |
| 400 |
} |
| 401 |
} |
| 402 |
if (i == attr->len) |
| 403 |
return -EINVAL; |
| 404 |
|
| 405 |
ret = smi330_get_config_reg(sensor, ®); |
| 406 |
if (ret) |
| 407 |
return ret; |
| 408 |
|
| 409 |
reg_val = field_prep(attr->mask, reg_val); |
| 410 |
ret = regmap_update_bits(data->regmap, reg, attr->mask, reg_val); |
| 411 |
if (ret) |
| 412 |
return ret; |
| 413 |
|
| 414 |
ret = regmap_read(data->regmap, SMI330_ERR_REG, &error); |
| 415 |
if (ret) |
| 416 |
return ret; |
| 417 |
|
| 418 |
if (FIELD_GET(SMI330_ERR_ACC_CONF_MASK, error) || |
| 419 |
FIELD_GET(SMI330_ERR_GYR_CONF_MASK, error)) |
| 420 |
return -EIO; |
| 421 |
|
| 422 |
return 0; |
| 423 |
} |
| 424 |
|
| 425 |
static int smi330_get_data(struct smi330_data *data, int chan_type, int axis, |
| 426 |
int *val) |
| 427 |
{ |
| 428 |
u8 reg; |
| 429 |
int ret, sample; |
| 430 |
|
| 431 |
switch (chan_type) { |
| 432 |
case IIO_ACCEL: |
| 433 |
reg = SMI330_ACCEL_X_REG + (axis - IIO_MOD_X); |
| 434 |
break; |
| 435 |
case IIO_ANGL_VEL: |
| 436 |
reg = SMI330_GYRO_X_REG + (axis - IIO_MOD_X); |
| 437 |
break; |
| 438 |
case IIO_TEMP: |
| 439 |
reg = SMI330_TEMP_REG; |
| 440 |
break; |
| 441 |
default: |
| 442 |
return -EINVAL; |
| 443 |
} |
| 444 |
|
| 445 |
ret = regmap_read(data->regmap, reg, &sample); |
| 446 |
if (ret) |
| 447 |
return ret; |
| 448 |
|
| 449 |
*val = sign_extend32(sample, 15); |
| 450 |
|
| 451 |
return 0; |
| 452 |
} |
| 453 |
|
| 454 |
static int smi330_read_avail(struct iio_dev *indio_dev, |
| 455 |
struct iio_chan_spec const *chan, const int **vals, |
| 456 |
int *type, int *length, long mask) |
| 457 |
{ |
| 458 |
switch (mask) { |
| 459 |
case IIO_CHAN_INFO_SCALE: |
| 460 |
if (chan->type == IIO_ACCEL) { |
| 461 |
*vals = smi330_accel_scale_attr.vals; |
| 462 |
*length = smi330_accel_scale_attr.len; |
| 463 |
*type = smi330_accel_scale_attr.type; |
| 464 |
} else { |
| 465 |
*vals = smi330_gyro_scale_attr.vals; |
| 466 |
*length = smi330_gyro_scale_attr.len; |
| 467 |
*type = smi330_gyro_scale_attr.type; |
| 468 |
} |
| 469 |
return IIO_AVAIL_LIST; |
| 470 |
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 471 |
*vals = smi330_average_attr.vals; |
| 472 |
*length = smi330_average_attr.len; |
| 473 |
*type = smi330_average_attr.type; |
| 474 |
return IIO_AVAIL_LIST; |
| 475 |
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
| 476 |
*vals = smi330_bandwidth_attr.vals; |
| 477 |
*length = smi330_bandwidth_attr.len; |
| 478 |
*type = smi330_bandwidth_attr.type; |
| 479 |
return IIO_AVAIL_LIST; |
| 480 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 481 |
*vals = smi330_odr_attr.vals; |
| 482 |
*length = smi330_odr_attr.len; |
| 483 |
*type = smi330_odr_attr.type; |
| 484 |
return IIO_AVAIL_LIST; |
| 485 |
default: |
| 486 |
return -EINVAL; |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
static int smi330_read_raw(struct iio_dev *indio_dev, |
| 491 |
struct iio_chan_spec const *chan, int *val, |
| 492 |
int *val2, long mask) |
| 493 |
{ |
| 494 |
int ret; |
| 495 |
struct smi330_data *data = iio_priv(indio_dev); |
| 496 |
enum smi330_sensor sensor; |
| 497 |
|
| 498 |
/* valid for all channel types */ |
| 499 |
switch (mask) { |
| 500 |
case IIO_CHAN_INFO_RAW: |
| 501 |
if (!iio_device_claim_direct(indio_dev)) |
| 502 |
return -EBUSY; |
| 503 |
ret = smi330_get_data(data, chan->type, chan->channel2, val); |
| 504 |
iio_device_release_direct(indio_dev); |
| 505 |
return ret ? ret : IIO_VAL_INT; |
| 506 |
default: |
| 507 |
break; |
| 508 |
} |
| 509 |
|
| 510 |
switch (chan->type) { |
| 511 |
case IIO_ACCEL: |
| 512 |
sensor = SMI330_ACCEL; |
| 513 |
break; |
| 514 |
case IIO_ANGL_VEL: |
| 515 |
sensor = SMI330_GYRO; |
| 516 |
break; |
| 517 |
case IIO_TEMP: |
| 518 |
switch (mask) { |
| 519 |
case IIO_CHAN_INFO_SCALE: |
| 520 |
*val = SMI330_TEMP_SCALE / GIGA; |
| 521 |
*val2 = SMI330_TEMP_SCALE % GIGA; |
| 522 |
return IIO_VAL_INT_PLUS_NANO; |
| 523 |
case IIO_CHAN_INFO_OFFSET: |
| 524 |
*val = SMI330_TEMP_OFFSET; |
| 525 |
return IIO_VAL_INT; |
| 526 |
default: |
| 527 |
return -EINVAL; |
| 528 |
} |
| 529 |
default: |
| 530 |
return -EINVAL; |
| 531 |
} |
| 532 |
|
| 533 |
/* valid for acc and gyro channels */ |
| 534 |
switch (mask) { |
| 535 |
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 536 |
ret = smi330_get_sensor_config(data, sensor, SMI330_AVG_NUM, |
| 537 |
val); |
| 538 |
return ret ? ret : IIO_VAL_INT; |
| 539 |
|
| 540 |
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
| 541 |
ret = smi330_get_sensor_config(data, sensor, SMI330_BW, val); |
| 542 |
return ret ? ret : IIO_VAL_INT; |
| 543 |
|
| 544 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 545 |
ret = smi330_get_sensor_config(data, sensor, SMI330_ODR, val); |
| 546 |
return ret ? ret : IIO_VAL_INT; |
| 547 |
|
| 548 |
case IIO_CHAN_INFO_SCALE: |
| 549 |
*val = 0; |
| 550 |
ret = smi330_get_sensor_config(data, sensor, SMI330_RANGE, |
| 551 |
val2); |
| 552 |
return ret ? ret : IIO_VAL_INT_PLUS_NANO; |
| 553 |
|
| 554 |
default: |
| 555 |
return -EINVAL; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
static int smi330_write_raw(struct iio_dev *indio_dev, |
| 560 |
struct iio_chan_spec const *chan, int val, int val2, |
| 561 |
long mask) |
| 562 |
{ |
| 563 |
struct smi330_data *data = iio_priv(indio_dev); |
| 564 |
enum smi330_sensor sensor; |
| 565 |
|
| 566 |
switch (chan->type) { |
| 567 |
case IIO_ACCEL: |
| 568 |
sensor = SMI330_ACCEL; |
| 569 |
break; |
| 570 |
case IIO_ANGL_VEL: |
| 571 |
sensor = SMI330_GYRO; |
| 572 |
break; |
| 573 |
default: |
| 574 |
return -EINVAL; |
| 575 |
} |
| 576 |
|
| 577 |
switch (mask) { |
| 578 |
case IIO_CHAN_INFO_SCALE: |
| 579 |
return smi330_set_sensor_config(data, sensor, SMI330_RANGE, |
| 580 |
val2); |
| 581 |
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 582 |
return smi330_set_sensor_config(data, sensor, SMI330_AVG_NUM, |
| 583 |
val); |
| 584 |
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
| 585 |
return smi330_set_sensor_config(data, sensor, SMI330_BW, val); |
| 586 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 587 |
return smi330_set_sensor_config(data, sensor, SMI330_ODR, val); |
| 588 |
default: |
| 589 |
return -EINVAL; |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
static int smi330_write_raw_get_fmt(struct iio_dev *indio_dev, |
| 594 |
struct iio_chan_spec const *chan, long info) |
| 595 |
{ |
| 596 |
switch (info) { |
| 597 |
case IIO_CHAN_INFO_SCALE: |
| 598 |
return IIO_VAL_INT_PLUS_NANO; |
| 599 |
default: |
| 600 |
return IIO_VAL_INT_PLUS_MICRO; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
static int smi330_soft_reset(struct smi330_data *data) |
| 605 |
{ |
| 606 |
int ret, dummy_byte; |
| 607 |
|
| 608 |
ret = regmap_write(data->regmap, SMI330_CMD_REG, SMI330_CMD_SOFT_RESET); |
| 609 |
if (ret) |
| 610 |
return ret; |
| 611 |
fsleep(SMI330_SOFT_RESET_DELAY); |
| 612 |
|
| 613 |
/* Performing a dummy read after a soft-reset */ |
| 614 |
regmap_read(data->regmap, SMI330_CHIP_ID_REG, &dummy_byte); |
| 615 |
|
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
static irqreturn_t smi330_trigger_handler(int irq, void *p) |
| 620 |
{ |
| 621 |
int ret; |
| 622 |
struct iio_poll_func *pf = p; |
| 623 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 624 |
struct smi330_data *data = iio_priv(indio_dev); |
| 625 |
|
| 626 |
ret = regmap_bulk_read(data->regmap, SMI330_ACCEL_X_REG, data->buf, |
| 627 |
SMI330_SCAN_LEN); |
| 628 |
if (ret) |
| 629 |
goto out; |
| 630 |
|
| 631 |
iio_push_to_buffers_with_timestamp(indio_dev, data->buf, pf->timestamp); |
| 632 |
|
| 633 |
out: |
| 634 |
iio_trigger_notify_done(indio_dev->trig); |
| 635 |
|
| 636 |
return IRQ_HANDLED; |
| 637 |
} |
| 638 |
|
| 639 |
static irqreturn_t smi330_irq_thread_handler(int irq, void *indio_dev_) |
| 640 |
{ |
| 641 |
int ret, int_stat; |
| 642 |
s16 int_status[2] = { 0 }; |
| 643 |
struct iio_dev *indio_dev = indio_dev_; |
| 644 |
struct smi330_data *data = iio_priv(indio_dev); |
| 645 |
|
| 646 |
ret = regmap_bulk_read(data->regmap, SMI330_INT1_STATUS_REG, int_status, 2); |
| 647 |
if (ret) |
| 648 |
return IRQ_NONE; |
| 649 |
|
| 650 |
int_stat = int_status[0] | int_status[1]; |
| 651 |
|
| 652 |
if (FIELD_GET(SMI330_INT_STATUS_ACC_GYR_DRDY_MASK, int_stat)) { |
| 653 |
indio_dev->pollfunc->timestamp = iio_get_time_ns(indio_dev); |
| 654 |
iio_trigger_poll_nested(data->trig); |
| 655 |
} |
| 656 |
|
| 657 |
return IRQ_HANDLED; |
| 658 |
} |
| 659 |
|
| 660 |
static int smi330_set_int_pin_config(struct smi330_data *data, |
| 661 |
enum smi330_int_out irq_num, |
| 662 |
bool active_high, bool open_drain, |
| 663 |
bool latch) |
| 664 |
{ |
| 665 |
int ret, val; |
| 666 |
|
| 667 |
val = active_high ? SMI330_IO_INT_CTRL_LVL : 0; |
| 668 |
val |= open_drain ? SMI330_IO_INT_CTRL_OD : 0; |
| 669 |
val |= SMI330_IO_INT_CTRL_EN; |
| 670 |
|
| 671 |
switch (irq_num) { |
| 672 |
case SMI330_INT_1: |
| 673 |
val = FIELD_PREP(SMI330_IO_INT_CTRL_INT1_MASK, val); |
| 674 |
ret = regmap_update_bits(data->regmap, SMI330_IO_INT_CTRL_REG, |
| 675 |
SMI330_IO_INT_CTRL_INT1_MASK, val); |
| 676 |
if (ret) |
| 677 |
return ret; |
| 678 |
break; |
| 679 |
case SMI330_INT_2: |
| 680 |
val = FIELD_PREP(SMI330_IO_INT_CTRL_INT2_MASK, val); |
| 681 |
ret = regmap_update_bits(data->regmap, SMI330_IO_INT_CTRL_REG, |
| 682 |
SMI330_IO_INT_CTRL_INT2_MASK, val); |
| 683 |
if (ret) |
| 684 |
return ret; |
| 685 |
break; |
| 686 |
default: |
| 687 |
return -EINVAL; |
| 688 |
} |
| 689 |
|
| 690 |
return regmap_update_bits(data->regmap, SMI330_INT_CONF_REG, |
| 691 |
SMI330_INT_CONF_LATCH_MASK, |
| 692 |
FIELD_PREP(SMI330_INT_CONF_LATCH_MASK, |
| 693 |
latch)); |
| 694 |
} |
| 695 |
|
| 696 |
static int smi330_setup_irq(struct device *dev, struct iio_dev *indio_dev, |
| 697 |
int irq, enum smi330_int_out irq_num) |
| 698 |
{ |
| 699 |
int ret, irq_type; |
| 700 |
bool open_drain, active_high, latch; |
| 701 |
struct smi330_data *data = iio_priv(indio_dev); |
| 702 |
struct irq_data *desc; |
| 703 |
|
| 704 |
desc = irq_get_irq_data(irq); |
| 705 |
if (!desc) |
| 706 |
return -EINVAL; |
| 707 |
|
| 708 |
irq_type = irqd_get_trigger_type(desc); |
| 709 |
switch (irq_type) { |
| 710 |
case IRQF_TRIGGER_RISING: |
| 711 |
latch = false; |
| 712 |
active_high = true; |
| 713 |
break; |
| 714 |
case IRQF_TRIGGER_HIGH: |
| 715 |
latch = true; |
| 716 |
active_high = true; |
| 717 |
break; |
| 718 |
case IRQF_TRIGGER_FALLING: |
| 719 |
latch = false; |
| 720 |
active_high = false; |
| 721 |
break; |
| 722 |
case IRQF_TRIGGER_LOW: |
| 723 |
latch = true; |
| 724 |
active_high = false; |
| 725 |
break; |
| 726 |
default: |
| 727 |
return -EINVAL; |
| 728 |
} |
| 729 |
|
| 730 |
open_drain = device_property_read_bool(dev, "drive-open-drain"); |
| 731 |
|
| 732 |
ret = smi330_set_int_pin_config(data, irq_num, active_high, open_drain, |
| 733 |
latch); |
| 734 |
if (ret) |
| 735 |
return ret; |
| 736 |
|
| 737 |
return devm_request_threaded_irq(dev, irq, NULL, |
| 738 |
smi330_irq_thread_handler, |
| 739 |
irq_type | IRQF_ONESHOT, |
| 740 |
indio_dev->name, indio_dev); |
| 741 |
} |
| 742 |
|
| 743 |
static int smi330_register_irq(struct device *dev, struct iio_dev *indio_dev) |
| 744 |
{ |
| 745 |
int ret, irq; |
| 746 |
struct smi330_data *data = iio_priv(indio_dev); |
| 747 |
struct fwnode_handle *fwnode; |
| 748 |
|
| 749 |
fwnode = dev_fwnode(dev); |
| 750 |
if (!fwnode) |
| 751 |
return -ENODEV; |
| 752 |
|
| 753 |
data->cfg.data_irq = SMI330_INT_DISABLED; |
| 754 |
|
| 755 |
irq = fwnode_irq_get_byname(fwnode, "INT1"); |
| 756 |
if (irq > 0) { |
| 757 |
ret = smi330_setup_irq(dev, indio_dev, irq, SMI330_INT_1); |
| 758 |
if (ret) |
| 759 |
return ret; |
| 760 |
data->cfg.data_irq = SMI330_INT_1; |
| 761 |
} else { |
| 762 |
irq = fwnode_irq_get_byname(fwnode, "INT2"); |
| 763 |
if (irq > 0) { |
| 764 |
ret = smi330_setup_irq(dev, indio_dev, irq, |
| 765 |
SMI330_INT_2); |
| 766 |
if (ret) |
| 767 |
return ret; |
| 768 |
data->cfg.data_irq = SMI330_INT_2; |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
return 0; |
| 773 |
} |
| 774 |
|
| 775 |
static int smi330_set_drdy_trigger_state(struct iio_trigger *trig, bool enable) |
| 776 |
{ |
| 777 |
int val; |
| 778 |
struct smi330_data *data = iio_trigger_get_drvdata(trig); |
| 779 |
|
| 780 |
if (enable) |
| 781 |
data->cfg.op_mode = SMI330_DATA_READY; |
| 782 |
else |
| 783 |
data->cfg.op_mode = SMI330_POLLING; |
| 784 |
|
| 785 |
val = FIELD_PREP(SMI330_INT_MAP2_ACC_DRDY_MASK, |
| 786 |
enable ? data->cfg.data_irq : 0); |
| 787 |
val |= FIELD_PREP(SMI330_INT_MAP2_GYR_DRDY_MASK, |
| 788 |
enable ? data->cfg.data_irq : 0); |
| 789 |
return regmap_update_bits(data->regmap, SMI330_INT_MAP2_REG, |
| 790 |
SMI330_INT_MAP2_ACC_DRDY_MASK | |
| 791 |
SMI330_INT_MAP2_GYR_DRDY_MASK, |
| 792 |
val); |
| 793 |
} |
| 794 |
|
| 795 |
static const struct iio_trigger_ops smi330_trigger_ops = { |
| 796 |
.set_trigger_state = &smi330_set_drdy_trigger_state, |
| 797 |
}; |
| 798 |
|
| 799 |
static struct iio_info smi330_info = { |
| 800 |
.read_avail = smi330_read_avail, |
| 801 |
.read_raw = smi330_read_raw, |
| 802 |
.write_raw = smi330_write_raw, |
| 803 |
.write_raw_get_fmt = smi330_write_raw_get_fmt, |
| 804 |
}; |
| 805 |
|
| 806 |
static int smi330_dev_init(struct smi330_data *data) |
| 807 |
{ |
| 808 |
int ret, chip_id, val, mode; |
| 809 |
struct device *dev = regmap_get_device(data->regmap); |
| 810 |
|
| 811 |
ret = regmap_read(data->regmap, SMI330_CHIP_ID_REG, &chip_id); |
| 812 |
if (ret) |
| 813 |
return ret; |
| 814 |
|
| 815 |
chip_id = FIELD_GET(SMI330_CHIP_ID_MASK, chip_id); |
| 816 |
if (chip_id != SMI330_CHIP_ID) |
| 817 |
dev_info(dev, "Unknown chip id: 0x%04x\n", chip_id); |
| 818 |
|
| 819 |
ret = regmap_read(data->regmap, SMI330_ERR_REG, &val); |
| 820 |
if (ret) |
| 821 |
return ret; |
| 822 |
if (FIELD_GET(SMI330_ERR_FATAL_MASK, val)) |
| 823 |
return -ENODEV; |
| 824 |
|
| 825 |
ret = regmap_read(data->regmap, SMI330_STATUS_REG, &val); |
| 826 |
if (ret) |
| 827 |
return ret; |
| 828 |
if (FIELD_GET(SMI330_STATUS_POR_MASK, val) == 0) |
| 829 |
return -ENODEV; |
| 830 |
|
| 831 |
mode = FIELD_PREP(SMI330_CFG_MODE_MASK, SMI330_MODE_NORMAL); |
| 832 |
|
| 833 |
ret = regmap_update_bits(data->regmap, SMI330_ACCEL_CFG_REG, |
| 834 |
SMI330_CFG_MODE_MASK, mode); |
| 835 |
if (ret) |
| 836 |
return ret; |
| 837 |
|
| 838 |
return regmap_update_bits(data->regmap, SMI330_GYRO_CFG_REG, |
| 839 |
SMI330_CFG_MODE_MASK, mode); |
| 840 |
} |
| 841 |
|
| 842 |
int smi330_core_probe(struct device *dev, struct regmap *regmap) |
| 843 |
{ |
| 844 |
int ret; |
| 845 |
struct iio_dev *indio_dev; |
| 846 |
struct smi330_data *data; |
| 847 |
|
| 848 |
indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); |
| 849 |
if (!indio_dev) |
| 850 |
return -ENOMEM; |
| 851 |
|
| 852 |
data = iio_priv(indio_dev); |
| 853 |
data->regmap = regmap; |
| 854 |
|
| 855 |
ret = smi330_soft_reset(data); |
| 856 |
if (ret) |
| 857 |
return dev_err_probe(dev, ret, "Soft reset failed\n"); |
| 858 |
|
| 859 |
indio_dev->channels = smi330_channels; |
| 860 |
indio_dev->num_channels = ARRAY_SIZE(smi330_channels); |
| 861 |
indio_dev->available_scan_masks = smi330_avail_scan_masks; |
| 862 |
indio_dev->name = "smi330"; |
| 863 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 864 |
indio_dev->info = &smi330_info; |
| 865 |
|
| 866 |
data->cfg.op_mode = SMI330_POLLING; |
| 867 |
|
| 868 |
ret = smi330_dev_init(data); |
| 869 |
if (ret) |
| 870 |
return dev_err_probe(dev, ret, "Init failed\n"); |
| 871 |
|
| 872 |
ret = smi330_register_irq(dev, indio_dev); |
| 873 |
if (ret) |
| 874 |
return dev_err_probe(dev, ret, "Register IRQ failed\n"); |
| 875 |
|
| 876 |
if (data->cfg.data_irq != SMI330_INT_DISABLED) { |
| 877 |
data->trig = devm_iio_trigger_alloc(dev, "%s-drdy-trigger", |
| 878 |
indio_dev->name); |
| 879 |
if (!data->trig) |
| 880 |
return -ENOMEM; |
| 881 |
|
| 882 |
data->trig->ops = &smi330_trigger_ops; |
| 883 |
iio_trigger_set_drvdata(data->trig, data); |
| 884 |
|
| 885 |
ret = devm_iio_trigger_register(dev, data->trig); |
| 886 |
if (ret) |
| 887 |
return dev_err_probe(dev, ret, |
| 888 |
"IIO register trigger failed\n"); |
| 889 |
|
| 890 |
/* Set default operation mode to data ready. */ |
| 891 |
indio_dev->trig = iio_trigger_get(data->trig); |
| 892 |
} |
| 893 |
|
| 894 |
ret = devm_iio_triggered_buffer_setup(dev, indio_dev, |
| 895 |
iio_pollfunc_store_time, |
| 896 |
smi330_trigger_handler, NULL); |
| 897 |
if (ret) |
| 898 |
return dev_err_probe(dev, ret, "IIO buffer setup failed\n"); |
| 899 |
|
| 900 |
ret = devm_iio_device_register(dev, indio_dev); |
| 901 |
if (ret) |
| 902 |
return dev_err_probe(dev, ret, "Register IIO device failed\n"); |
| 903 |
|
| 904 |
return 0; |
| 905 |
} |
| 906 |
EXPORT_SYMBOL_NS_GPL(smi330_core_probe, "IIO_SMI330"); |
| 907 |
|
| 908 |
MODULE_AUTHOR("Stefan Gutmann <[email protected]>"); |
| 909 |
MODULE_AUTHOR("Roman Huber <[email protected]>"); |
| 910 |
MODULE_AUTHOR("Filip Andrei <[email protected]>"); |
| 911 |
MODULE_AUTHOR("Drimbarean Avram Andrei <[email protected]>"); |
| 912 |
MODULE_DESCRIPTION("Bosch SMI330 IMU driver"); |
| 913 |
MODULE_LICENSE("Dual BSD/GPL"); |