1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2020 Invensense, Inc. 4 */ 5 6 #include <linux/kernel.h> 7 #include <linux/device.h> 8 #include <linux/mutex.h> 9 #include <linux/pm_runtime.h> 10 #include <linux/regmap.h> 11 #include <linux/delay.h> 12 #include <linux/math64.h> 13 14 #include <linux/iio/buffer.h> 15 #include <linux/iio/common/inv_sensors_timestamp.h> 16 #include <linux/iio/iio.h> 17 #include <linux/iio/kfifo_buf.h> 18 19 #include "inv_icm42600.h" 20 #include "inv_icm42600_temp.h" 21 #include "inv_icm42600_buffer.h" 22 23 #define INV_ICM42600_GYRO_CHAN(_modifier, _index, _ext_info) \ 24 { \ 25 .type = IIO_ANGL_VEL, \ 26 .modified = 1, \ 27 .channel2 = _modifier, \ 28 .info_mask_separate = \ 29 BIT(IIO_CHAN_INFO_RAW) | \ 30 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 31 .info_mask_shared_by_type = \ 32 BIT(IIO_CHAN_INFO_SCALE), \ 33 .info_mask_shared_by_type_available = \ 34 BIT(IIO_CHAN_INFO_SCALE) | \ 35 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 36 .info_mask_shared_by_all = \ 37 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 38 .info_mask_shared_by_all_available = \ 39 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 40 .scan_index = _index, \ 41 .scan_type = { \ 42 .sign = 's', \ 43 .realbits = 16, \ 44 .storagebits = 16, \ 45 .endianness = IIO_BE, \ 46 }, \ 47 .ext_info = _ext_info, \ 48 } 49 50 enum inv_icm42600_gyro_scan { 51 INV_ICM42600_GYRO_SCAN_X, 52 INV_ICM42600_GYRO_SCAN_Y, 53 INV_ICM42600_GYRO_SCAN_Z, 54 INV_ICM42600_GYRO_SCAN_TEMP, 55 INV_ICM42600_GYRO_SCAN_TIMESTAMP, 56 }; 57 58 static const struct iio_chan_spec_ext_info inv_icm42600_gyro_ext_infos[] = { 59 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, inv_icm42600_get_mount_matrix), 60 { } 61 }; 62 63 static const struct iio_chan_spec inv_icm42600_gyro_channels[] = { 64 INV_ICM42600_GYRO_CHAN(IIO_MOD_X, INV_ICM42600_GYRO_SCAN_X, 65 inv_icm42600_gyro_ext_infos), 66 INV_ICM42600_GYRO_CHAN(IIO_MOD_Y, INV_ICM42600_GYRO_SCAN_Y, 67 inv_icm42600_gyro_ext_infos), 68 INV_ICM42600_GYRO_CHAN(IIO_MOD_Z, INV_ICM42600_GYRO_SCAN_Z, 69 inv_icm42600_gyro_ext_infos), 70 INV_ICM42600_TEMP_CHAN(INV_ICM42600_GYRO_SCAN_TEMP), 71 IIO_CHAN_SOFT_TIMESTAMP(INV_ICM42600_GYRO_SCAN_TIMESTAMP), 72 }; 73 74 /* 75 * IIO buffer data: size must be a power of 2 and timestamp aligned 76 * 16 bytes: 6 bytes angular velocity, 2 bytes temperature, 8 bytes timestamp 77 */ 78 struct inv_icm42600_gyro_buffer { 79 struct inv_icm42600_fifo_sensor_data gyro; 80 int16_t temp; 81 aligned_s64 timestamp; 82 }; 83 84 #define INV_ICM42600_SCAN_MASK_GYRO_3AXIS \ 85 (BIT(INV_ICM42600_GYRO_SCAN_X) | \ 86 BIT(INV_ICM42600_GYRO_SCAN_Y) | \ 87 BIT(INV_ICM42600_GYRO_SCAN_Z)) 88 89 #define INV_ICM42600_SCAN_MASK_TEMP BIT(INV_ICM42600_GYRO_SCAN_TEMP) 90 91 static const unsigned long inv_icm42600_gyro_scan_masks[] = { 92 /* 3-axis gyro + temperature */ 93 INV_ICM42600_SCAN_MASK_GYRO_3AXIS | INV_ICM42600_SCAN_MASK_TEMP, 94 0, 95 }; 96 97 /* enable gyroscope sensor and FIFO write */ 98 static int inv_icm42600_gyro_update_scan_mode(struct iio_dev *indio_dev, 99 const unsigned long *scan_mask) 100 { 101 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 102 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT; 103 unsigned int fifo_en = 0; 104 unsigned int sleep_gyro = 0; 105 unsigned int sleep_temp = 0; 106 unsigned int sleep; 107 int ret; 108 109 mutex_lock(&st->lock); 110 111 if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) { 112 /* enable temp sensor */ 113 ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp); 114 if (ret) 115 goto out_unlock; 116 fifo_en |= INV_ICM42600_SENSOR_TEMP; 117 } 118 119 if (*scan_mask & INV_ICM42600_SCAN_MASK_GYRO_3AXIS) { 120 /* enable gyro sensor */ 121 conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE; 122 ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_gyro); 123 if (ret) 124 goto out_unlock; 125 fifo_en |= INV_ICM42600_SENSOR_GYRO; 126 } 127 128 /* update data FIFO write */ 129 ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en); 130 131 out_unlock: 132 mutex_unlock(&st->lock); 133 /* sleep maximum required time */ 134 sleep = max(sleep_gyro, sleep_temp); 135 if (sleep) 136 msleep(sleep); 137 return ret; 138 } 139 140 static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st, 141 struct iio_chan_spec const *chan, 142 int16_t *val) 143 { 144 struct device *dev = regmap_get_device(st->map); 145 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT; 146 unsigned int reg; 147 __be16 *data; 148 int ret; 149 150 if (chan->type != IIO_ANGL_VEL) 151 return -EINVAL; 152 153 switch (chan->channel2) { 154 case IIO_MOD_X: 155 reg = INV_ICM42600_REG_GYRO_DATA_X; 156 break; 157 case IIO_MOD_Y: 158 reg = INV_ICM42600_REG_GYRO_DATA_Y; 159 break; 160 case IIO_MOD_Z: 161 reg = INV_ICM42600_REG_GYRO_DATA_Z; 162 break; 163 default: 164 return -EINVAL; 165 } 166 167 pm_runtime_get_sync(dev); 168 mutex_lock(&st->lock); 169 170 /* enable gyro sensor */ 171 conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE; 172 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL); 173 if (ret) 174 goto exit; 175 176 /* read gyro register data */ 177 data = (__be16 *)&st->buffer[0]; 178 ret = regmap_bulk_read(st->map, reg, data, sizeof(*data)); 179 if (ret) 180 goto exit; 181 182 *val = (int16_t)be16_to_cpup(data); 183 if (*val == INV_ICM42600_DATA_INVALID) 184 ret = -EINVAL; 185 exit: 186 mutex_unlock(&st->lock); 187 pm_runtime_mark_last_busy(dev); 188 pm_runtime_put_autosuspend(dev); 189 return ret; 190 } 191 192 /* IIO format int + nano */ 193 static const int inv_icm42600_gyro_scale[] = { 194 /* +/- 2000dps => 0.001065264 rad/s */ 195 [2 * INV_ICM42600_GYRO_FS_2000DPS] = 0, 196 [2 * INV_ICM42600_GYRO_FS_2000DPS + 1] = 1065264, 197 /* +/- 1000dps => 0.000532632 rad/s */ 198 [2 * INV_ICM42600_GYRO_FS_1000DPS] = 0, 199 [2 * INV_ICM42600_GYRO_FS_1000DPS + 1] = 532632, 200 /* +/- 500dps => 0.000266316 rad/s */ 201 [2 * INV_ICM42600_GYRO_FS_500DPS] = 0, 202 [2 * INV_ICM42600_GYRO_FS_500DPS + 1] = 266316, 203 /* +/- 250dps => 0.000133158 rad/s */ 204 [2 * INV_ICM42600_GYRO_FS_250DPS] = 0, 205 [2 * INV_ICM42600_GYRO_FS_250DPS + 1] = 133158, 206 /* +/- 125dps => 0.000066579 rad/s */ 207 [2 * INV_ICM42600_GYRO_FS_125DPS] = 0, 208 [2 * INV_ICM42600_GYRO_FS_125DPS + 1] = 66579, 209 /* +/- 62.5dps => 0.000033290 rad/s */ 210 [2 * INV_ICM42600_GYRO_FS_62_5DPS] = 0, 211 [2 * INV_ICM42600_GYRO_FS_62_5DPS + 1] = 33290, 212 /* +/- 31.25dps => 0.000016645 rad/s */ 213 [2 * INV_ICM42600_GYRO_FS_31_25DPS] = 0, 214 [2 * INV_ICM42600_GYRO_FS_31_25DPS + 1] = 16645, 215 /* +/- 15.625dps => 0.000008322 rad/s */ 216 [2 * INV_ICM42600_GYRO_FS_15_625DPS] = 0, 217 [2 * INV_ICM42600_GYRO_FS_15_625DPS + 1] = 8322, 218 }; 219 static const int inv_icm42686_gyro_scale[] = { 220 /* +/- 4000dps => 0.002130529 rad/s */ 221 [2 * INV_ICM42686_GYRO_FS_4000DPS] = 0, 222 [2 * INV_ICM42686_GYRO_FS_4000DPS + 1] = 2130529, 223 /* +/- 2000dps => 0.001065264 rad/s */ 224 [2 * INV_ICM42686_GYRO_FS_2000DPS] = 0, 225 [2 * INV_ICM42686_GYRO_FS_2000DPS + 1] = 1065264, 226 /* +/- 1000dps => 0.000532632 rad/s */ 227 [2 * INV_ICM42686_GYRO_FS_1000DPS] = 0, 228 [2 * INV_ICM42686_GYRO_FS_1000DPS + 1] = 532632, 229 /* +/- 500dps => 0.000266316 rad/s */ 230 [2 * INV_ICM42686_GYRO_FS_500DPS] = 0, 231 [2 * INV_ICM42686_GYRO_FS_500DPS + 1] = 266316, 232 /* +/- 250dps => 0.000133158 rad/s */ 233 [2 * INV_ICM42686_GYRO_FS_250DPS] = 0, 234 [2 * INV_ICM42686_GYRO_FS_250DPS + 1] = 133158, 235 /* +/- 125dps => 0.000066579 rad/s */ 236 [2 * INV_ICM42686_GYRO_FS_125DPS] = 0, 237 [2 * INV_ICM42686_GYRO_FS_125DPS + 1] = 66579, 238 /* +/- 62.5dps => 0.000033290 rad/s */ 239 [2 * INV_ICM42686_GYRO_FS_62_5DPS] = 0, 240 [2 * INV_ICM42686_GYRO_FS_62_5DPS + 1] = 33290, 241 /* +/- 31.25dps => 0.000016645 rad/s */ 242 [2 * INV_ICM42686_GYRO_FS_31_25DPS] = 0, 243 [2 * INV_ICM42686_GYRO_FS_31_25DPS + 1] = 16645, 244 }; 245 246 static int inv_icm42600_gyro_read_scale(struct iio_dev *indio_dev, 247 int *val, int *val2) 248 { 249 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 250 struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev); 251 unsigned int idx; 252 253 idx = st->conf.gyro.fs; 254 255 *val = gyro_st->scales[2 * idx]; 256 *val2 = gyro_st->scales[2 * idx + 1]; 257 return IIO_VAL_INT_PLUS_NANO; 258 } 259 260 static int inv_icm42600_gyro_write_scale(struct iio_dev *indio_dev, 261 int val, int val2) 262 { 263 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 264 struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev); 265 struct device *dev = regmap_get_device(st->map); 266 unsigned int idx; 267 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT; 268 int ret; 269 270 for (idx = 0; idx < gyro_st->scales_len; idx += 2) { 271 if (val == gyro_st->scales[idx] && 272 val2 == gyro_st->scales[idx + 1]) 273 break; 274 } 275 if (idx >= gyro_st->scales_len) 276 return -EINVAL; 277 278 conf.fs = idx / 2; 279 280 pm_runtime_get_sync(dev); 281 mutex_lock(&st->lock); 282 283 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL); 284 285 mutex_unlock(&st->lock); 286 pm_runtime_mark_last_busy(dev); 287 pm_runtime_put_autosuspend(dev); 288 289 return ret; 290 } 291 292 /* IIO format int + micro */ 293 static const int inv_icm42600_gyro_odr[] = { 294 /* 12.5Hz */ 295 12, 500000, 296 /* 25Hz */ 297 25, 0, 298 /* 50Hz */ 299 50, 0, 300 /* 100Hz */ 301 100, 0, 302 /* 200Hz */ 303 200, 0, 304 /* 1kHz */ 305 1000, 0, 306 /* 2kHz */ 307 2000, 0, 308 /* 4kHz */ 309 4000, 0, 310 }; 311 312 static const int inv_icm42600_gyro_odr_conv[] = { 313 INV_ICM42600_ODR_12_5HZ, 314 INV_ICM42600_ODR_25HZ, 315 INV_ICM42600_ODR_50HZ, 316 INV_ICM42600_ODR_100HZ, 317 INV_ICM42600_ODR_200HZ, 318 INV_ICM42600_ODR_1KHZ_LN, 319 INV_ICM42600_ODR_2KHZ_LN, 320 INV_ICM42600_ODR_4KHZ_LN, 321 }; 322 323 static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state *st, 324 int *val, int *val2) 325 { 326 unsigned int odr; 327 unsigned int i; 328 329 odr = st->conf.gyro.odr; 330 331 for (i = 0; i < ARRAY_SIZE(inv_icm42600_gyro_odr_conv); ++i) { 332 if (inv_icm42600_gyro_odr_conv[i] == odr) 333 break; 334 } 335 if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv)) 336 return -EINVAL; 337 338 *val = inv_icm42600_gyro_odr[2 * i]; 339 *val2 = inv_icm42600_gyro_odr[2 * i + 1]; 340 341 return IIO_VAL_INT_PLUS_MICRO; 342 } 343 344 static int inv_icm42600_gyro_write_odr(struct iio_dev *indio_dev, 345 int val, int val2) 346 { 347 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 348 struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev); 349 struct inv_sensors_timestamp *ts = &gyro_st->ts; 350 struct device *dev = regmap_get_device(st->map); 351 unsigned int idx; 352 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT; 353 int ret; 354 355 for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_odr); idx += 2) { 356 if (val == inv_icm42600_gyro_odr[idx] && 357 val2 == inv_icm42600_gyro_odr[idx + 1]) 358 break; 359 } 360 if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr)) 361 return -EINVAL; 362 363 conf.odr = inv_icm42600_gyro_odr_conv[idx / 2]; 364 365 pm_runtime_get_sync(dev); 366 mutex_lock(&st->lock); 367 368 ret = inv_sensors_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr), 369 iio_buffer_enabled(indio_dev)); 370 if (ret) 371 goto out_unlock; 372 373 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL); 374 if (ret) 375 goto out_unlock; 376 inv_icm42600_buffer_update_fifo_period(st); 377 inv_icm42600_buffer_update_watermark(st); 378 379 out_unlock: 380 mutex_unlock(&st->lock); 381 pm_runtime_mark_last_busy(dev); 382 pm_runtime_put_autosuspend(dev); 383 384 return ret; 385 } 386 387 /* 388 * Calibration bias values, IIO range format int + nano. 389 * Value is limited to +/-64dps coded on 12 bits signed. Step is 1/32 dps. 390 */ 391 static int inv_icm42600_gyro_calibbias[] = { 392 -1, 117010721, /* min: -1.117010721 rad/s */ 393 0, 545415, /* step: 0.000545415 rad/s */ 394 1, 116465306, /* max: 1.116465306 rad/s */ 395 }; 396 397 static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state *st, 398 struct iio_chan_spec const *chan, 399 int *val, int *val2) 400 { 401 struct device *dev = regmap_get_device(st->map); 402 int64_t val64; 403 int32_t bias; 404 unsigned int reg; 405 int16_t offset; 406 uint8_t data[2]; 407 int ret; 408 409 if (chan->type != IIO_ANGL_VEL) 410 return -EINVAL; 411 412 switch (chan->channel2) { 413 case IIO_MOD_X: 414 reg = INV_ICM42600_REG_OFFSET_USER0; 415 break; 416 case IIO_MOD_Y: 417 reg = INV_ICM42600_REG_OFFSET_USER1; 418 break; 419 case IIO_MOD_Z: 420 reg = INV_ICM42600_REG_OFFSET_USER3; 421 break; 422 default: 423 return -EINVAL; 424 } 425 426 pm_runtime_get_sync(dev); 427 mutex_lock(&st->lock); 428 429 ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data)); 430 memcpy(data, st->buffer, sizeof(data)); 431 432 mutex_unlock(&st->lock); 433 pm_runtime_mark_last_busy(dev); 434 pm_runtime_put_autosuspend(dev); 435 if (ret) 436 return ret; 437 438 /* 12 bits signed value */ 439 switch (chan->channel2) { 440 case IIO_MOD_X: 441 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11); 442 break; 443 case IIO_MOD_Y: 444 offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11); 445 break; 446 case IIO_MOD_Z: 447 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11); 448 break; 449 default: 450 return -EINVAL; 451 } 452 453 /* 454 * convert raw offset to dps then to rad/s 455 * 12 bits signed raw max 64 to dps: 64 / 2048 456 * dps to rad: Pi / 180 457 * result in nano (1000000000) 458 * (offset * 64 * Pi * 1000000000) / (2048 * 180) 459 */ 460 val64 = (int64_t)offset * 64LL * 3141592653LL; 461 /* for rounding, add + or - divisor (2048 * 180) divided by 2 */ 462 if (val64 >= 0) 463 val64 += 2048 * 180 / 2; 464 else 465 val64 -= 2048 * 180 / 2; 466 bias = div_s64(val64, 2048 * 180); 467 *val = bias / 1000000000L; 468 *val2 = bias % 1000000000L; 469 470 return IIO_VAL_INT_PLUS_NANO; 471 } 472 473 static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state *st, 474 struct iio_chan_spec const *chan, 475 int val, int val2) 476 { 477 struct device *dev = regmap_get_device(st->map); 478 int64_t val64, min, max; 479 unsigned int reg, regval; 480 int16_t offset; 481 int ret; 482 483 if (chan->type != IIO_ANGL_VEL) 484 return -EINVAL; 485 486 switch (chan->channel2) { 487 case IIO_MOD_X: 488 reg = INV_ICM42600_REG_OFFSET_USER0; 489 break; 490 case IIO_MOD_Y: 491 reg = INV_ICM42600_REG_OFFSET_USER1; 492 break; 493 case IIO_MOD_Z: 494 reg = INV_ICM42600_REG_OFFSET_USER3; 495 break; 496 default: 497 return -EINVAL; 498 } 499 500 /* inv_icm42600_gyro_calibbias: min - step - max in nano */ 501 min = (int64_t)inv_icm42600_gyro_calibbias[0] * 1000000000LL + 502 (int64_t)inv_icm42600_gyro_calibbias[1]; 503 max = (int64_t)inv_icm42600_gyro_calibbias[4] * 1000000000LL + 504 (int64_t)inv_icm42600_gyro_calibbias[5]; 505 val64 = (int64_t)val * 1000000000LL + (int64_t)val2; 506 if (val64 < min || val64 > max) 507 return -EINVAL; 508 509 /* 510 * convert rad/s to dps then to raw value 511 * rad to dps: 180 / Pi 512 * dps to raw 12 bits signed, max 64: 2048 / 64 513 * val in nano (1000000000) 514 * val * 180 * 2048 / (Pi * 1000000000 * 64) 515 */ 516 val64 = val64 * 180LL * 2048LL; 517 /* for rounding, add + or - divisor (3141592653 * 64) divided by 2 */ 518 if (val64 >= 0) 519 val64 += 3141592653LL * 64LL / 2LL; 520 else 521 val64 -= 3141592653LL * 64LL / 2LL; 522 offset = div64_s64(val64, 3141592653LL * 64LL); 523 524 /* clamp value limited to 12 bits signed */ 525 if (offset < -2048) 526 offset = -2048; 527 else if (offset > 2047) 528 offset = 2047; 529 530 pm_runtime_get_sync(dev); 531 mutex_lock(&st->lock); 532 533 switch (chan->channel2) { 534 case IIO_MOD_X: 535 /* OFFSET_USER1 register is shared */ 536 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1, 537 ®val); 538 if (ret) 539 goto out_unlock; 540 st->buffer[0] = offset & 0xFF; 541 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8); 542 break; 543 case IIO_MOD_Y: 544 /* OFFSET_USER1 register is shared */ 545 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1, 546 ®val); 547 if (ret) 548 goto out_unlock; 549 st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F); 550 st->buffer[1] = offset & 0xFF; 551 break; 552 case IIO_MOD_Z: 553 /* OFFSET_USER4 register is shared */ 554 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4, 555 ®val); 556 if (ret) 557 goto out_unlock; 558 st->buffer[0] = offset & 0xFF; 559 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8); 560 break; 561 default: 562 ret = -EINVAL; 563 goto out_unlock; 564 } 565 566 ret = regmap_bulk_write(st->map, reg, st->buffer, 2); 567 568 out_unlock: 569 mutex_unlock(&st->lock); 570 pm_runtime_mark_last_busy(dev); 571 pm_runtime_put_autosuspend(dev); 572 return ret; 573 } 574 575 static int inv_icm42600_gyro_read_raw(struct iio_dev *indio_dev, 576 struct iio_chan_spec const *chan, 577 int *val, int *val2, long mask) 578 { 579 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 580 int16_t data; 581 int ret; 582 583 switch (chan->type) { 584 case IIO_ANGL_VEL: 585 break; 586 case IIO_TEMP: 587 return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask); 588 default: 589 return -EINVAL; 590 } 591 592 switch (mask) { 593 case IIO_CHAN_INFO_RAW: 594 if (!iio_device_claim_direct(indio_dev)) 595 return -EBUSY; 596 ret = inv_icm42600_gyro_read_sensor(st, chan, &data); 597 iio_device_release_direct(indio_dev); 598 if (ret) 599 return ret; 600 *val = data; 601 return IIO_VAL_INT; 602 case IIO_CHAN_INFO_SCALE: 603 return inv_icm42600_gyro_read_scale(indio_dev, val, val2); 604 case IIO_CHAN_INFO_SAMP_FREQ: 605 return inv_icm42600_gyro_read_odr(st, val, val2); 606 case IIO_CHAN_INFO_CALIBBIAS: 607 return inv_icm42600_gyro_read_offset(st, chan, val, val2); 608 default: 609 return -EINVAL; 610 } 611 } 612 613 static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev, 614 struct iio_chan_spec const *chan, 615 const int **vals, 616 int *type, int *length, long mask) 617 { 618 struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev); 619 620 if (chan->type != IIO_ANGL_VEL) 621 return -EINVAL; 622 623 switch (mask) { 624 case IIO_CHAN_INFO_SCALE: 625 *vals = gyro_st->scales; 626 *type = IIO_VAL_INT_PLUS_NANO; 627 *length = gyro_st->scales_len; 628 return IIO_AVAIL_LIST; 629 case IIO_CHAN_INFO_SAMP_FREQ: 630 *vals = inv_icm42600_gyro_odr; 631 *type = IIO_VAL_INT_PLUS_MICRO; 632 *length = ARRAY_SIZE(inv_icm42600_gyro_odr); 633 return IIO_AVAIL_LIST; 634 case IIO_CHAN_INFO_CALIBBIAS: 635 *vals = inv_icm42600_gyro_calibbias; 636 *type = IIO_VAL_INT_PLUS_NANO; 637 return IIO_AVAIL_RANGE; 638 default: 639 return -EINVAL; 640 } 641 } 642 643 static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev, 644 struct iio_chan_spec const *chan, 645 int val, int val2, long mask) 646 { 647 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 648 int ret; 649 650 if (chan->type != IIO_ANGL_VEL) 651 return -EINVAL; 652 653 switch (mask) { 654 case IIO_CHAN_INFO_SCALE: 655 if (!iio_device_claim_direct(indio_dev)) 656 return -EBUSY; 657 ret = inv_icm42600_gyro_write_scale(indio_dev, val, val2); 658 iio_device_release_direct(indio_dev); 659 return ret; 660 case IIO_CHAN_INFO_SAMP_FREQ: 661 return inv_icm42600_gyro_write_odr(indio_dev, val, val2); 662 case IIO_CHAN_INFO_CALIBBIAS: 663 if (!iio_device_claim_direct(indio_dev)) 664 return -EBUSY; 665 ret = inv_icm42600_gyro_write_offset(st, chan, val, val2); 666 iio_device_release_direct(indio_dev); 667 return ret; 668 default: 669 return -EINVAL; 670 } 671 } 672 673 static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev, 674 struct iio_chan_spec const *chan, 675 long mask) 676 { 677 if (chan->type != IIO_ANGL_VEL) 678 return -EINVAL; 679 680 switch (mask) { 681 case IIO_CHAN_INFO_SCALE: 682 return IIO_VAL_INT_PLUS_NANO; 683 case IIO_CHAN_INFO_SAMP_FREQ: 684 return IIO_VAL_INT_PLUS_MICRO; 685 case IIO_CHAN_INFO_CALIBBIAS: 686 return IIO_VAL_INT_PLUS_NANO; 687 default: 688 return -EINVAL; 689 } 690 } 691 692 static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev, 693 unsigned int val) 694 { 695 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 696 int ret; 697 698 mutex_lock(&st->lock); 699 700 st->fifo.watermark.gyro = val; 701 ret = inv_icm42600_buffer_update_watermark(st); 702 703 mutex_unlock(&st->lock); 704 705 return ret; 706 } 707 708 static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev, 709 unsigned int count) 710 { 711 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 712 int ret; 713 714 if (count == 0) 715 return 0; 716 717 mutex_lock(&st->lock); 718 719 ret = inv_icm42600_buffer_hwfifo_flush(st, count); 720 if (!ret) 721 ret = st->fifo.nb.gyro; 722 723 mutex_unlock(&st->lock); 724 725 return ret; 726 } 727 728 static const struct iio_info inv_icm42600_gyro_info = { 729 .read_raw = inv_icm42600_gyro_read_raw, 730 .read_avail = inv_icm42600_gyro_read_avail, 731 .write_raw = inv_icm42600_gyro_write_raw, 732 .write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt, 733 .debugfs_reg_access = inv_icm42600_debugfs_reg, 734 .update_scan_mode = inv_icm42600_gyro_update_scan_mode, 735 .hwfifo_set_watermark = inv_icm42600_gyro_hwfifo_set_watermark, 736 .hwfifo_flush_to_buffer = inv_icm42600_gyro_hwfifo_flush, 737 }; 738 739 struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st) 740 { 741 struct device *dev = regmap_get_device(st->map); 742 const char *name; 743 struct inv_icm42600_sensor_state *gyro_st; 744 struct inv_sensors_timestamp_chip ts_chip; 745 struct iio_dev *indio_dev; 746 int ret; 747 748 name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name); 749 if (!name) 750 return ERR_PTR(-ENOMEM); 751 752 indio_dev = devm_iio_device_alloc(dev, sizeof(*gyro_st)); 753 if (!indio_dev) 754 return ERR_PTR(-ENOMEM); 755 gyro_st = iio_priv(indio_dev); 756 757 switch (st->chip) { 758 case INV_CHIP_ICM42686: 759 gyro_st->scales = inv_icm42686_gyro_scale; 760 gyro_st->scales_len = ARRAY_SIZE(inv_icm42686_gyro_scale); 761 break; 762 default: 763 gyro_st->scales = inv_icm42600_gyro_scale; 764 gyro_st->scales_len = ARRAY_SIZE(inv_icm42600_gyro_scale); 765 break; 766 } 767 768 /* 769 * clock period is 32kHz (31250ns) 770 * jitter is +/- 2% (20 per mille) 771 */ 772 ts_chip.clock_period = 31250; 773 ts_chip.jitter = 20; 774 ts_chip.init_period = inv_icm42600_odr_to_period(st->conf.accel.odr); 775 inv_sensors_timestamp_init(&gyro_st->ts, &ts_chip); 776 777 iio_device_set_drvdata(indio_dev, st); 778 indio_dev->name = name; 779 indio_dev->info = &inv_icm42600_gyro_info; 780 indio_dev->modes = INDIO_DIRECT_MODE; 781 indio_dev->channels = inv_icm42600_gyro_channels; 782 indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels); 783 indio_dev->available_scan_masks = inv_icm42600_gyro_scan_masks; 784 indio_dev->setup_ops = &inv_icm42600_buffer_ops; 785 786 ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, 787 &inv_icm42600_buffer_ops); 788 if (ret) 789 return ERR_PTR(ret); 790 791 ret = devm_iio_device_register(dev, indio_dev); 792 if (ret) 793 return ERR_PTR(ret); 794 795 return indio_dev; 796 } 797 798 int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev) 799 { 800 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 801 struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev); 802 struct inv_sensors_timestamp *ts = &gyro_st->ts; 803 ssize_t i, size; 804 unsigned int no; 805 const void *accel, *gyro, *timestamp; 806 const int8_t *temp; 807 unsigned int odr; 808 int64_t ts_val; 809 struct inv_icm42600_gyro_buffer buffer; 810 811 /* parse all fifo packets */ 812 for (i = 0, no = 0; i < st->fifo.count; i += size, ++no) { 813 size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i], 814 &accel, &gyro, &temp, ×tamp, &odr); 815 /* quit if error or FIFO is empty */ 816 if (size <= 0) 817 return size; 818 819 /* skip packet if no gyro data or data is invalid */ 820 if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro)) 821 continue; 822 823 /* update odr */ 824 if (odr & INV_ICM42600_SENSOR_GYRO) 825 inv_sensors_timestamp_apply_odr(ts, st->fifo.period, 826 st->fifo.nb.total, no); 827 828 /* buffer is copied to userspace, zeroing it to avoid any data leak */ 829 memset(&buffer, 0, sizeof(buffer)); 830 memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro)); 831 /* convert 8 bits FIFO temperature in high resolution format */ 832 buffer.temp = temp ? (*temp * 64) : 0; 833 ts_val = inv_sensors_timestamp_pop(ts); 834 iio_push_to_buffers_with_timestamp(indio_dev, &buffer, ts_val); 835 } 836 837 return 0; 838 } 839