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 int64_t timestamp __aligned(8); 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 if (sleep_gyro > sleep_temp) 135 sleep = sleep_gyro; 136 else 137 sleep = sleep_temp; 138 if (sleep) 139 msleep(sleep); 140 return ret; 141 } 142 143 static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st, 144 struct iio_chan_spec const *chan, 145 int16_t *val) 146 { 147 struct device *dev = regmap_get_device(st->map); 148 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT; 149 unsigned int reg; 150 __be16 *data; 151 int ret; 152 153 if (chan->type != IIO_ANGL_VEL) 154 return -EINVAL; 155 156 switch (chan->channel2) { 157 case IIO_MOD_X: 158 reg = INV_ICM42600_REG_GYRO_DATA_X; 159 break; 160 case IIO_MOD_Y: 161 reg = INV_ICM42600_REG_GYRO_DATA_Y; 162 break; 163 case IIO_MOD_Z: 164 reg = INV_ICM42600_REG_GYRO_DATA_Z; 165 break; 166 default: 167 return -EINVAL; 168 } 169 170 pm_runtime_get_sync(dev); 171 mutex_lock(&st->lock); 172 173 /* enable gyro sensor */ 174 conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE; 175 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL); 176 if (ret) 177 goto exit; 178 179 /* read gyro register data */ 180 data = (__be16 *)&st->buffer[0]; 181 ret = regmap_bulk_read(st->map, reg, data, sizeof(*data)); 182 if (ret) 183 goto exit; 184 185 *val = (int16_t)be16_to_cpup(data); 186 if (*val == INV_ICM42600_DATA_INVALID) 187 ret = -EINVAL; 188 exit: 189 mutex_unlock(&st->lock); 190 pm_runtime_mark_last_busy(dev); 191 pm_runtime_put_autosuspend(dev); 192 return ret; 193 } 194 195 /* IIO format int + nano */ 196 static const int inv_icm42600_gyro_scale[] = { 197 /* +/- 2000dps => 0.001065264 rad/s */ 198 [2 * INV_ICM42600_GYRO_FS_2000DPS] = 0, 199 [2 * INV_ICM42600_GYRO_FS_2000DPS + 1] = 1065264, 200 /* +/- 1000dps => 0.000532632 rad/s */ 201 [2 * INV_ICM42600_GYRO_FS_1000DPS] = 0, 202 [2 * INV_ICM42600_GYRO_FS_1000DPS + 1] = 532632, 203 /* +/- 500dps => 0.000266316 rad/s */ 204 [2 * INV_ICM42600_GYRO_FS_500DPS] = 0, 205 [2 * INV_ICM42600_GYRO_FS_500DPS + 1] = 266316, 206 /* +/- 250dps => 0.000133158 rad/s */ 207 [2 * INV_ICM42600_GYRO_FS_250DPS] = 0, 208 [2 * INV_ICM42600_GYRO_FS_250DPS + 1] = 133158, 209 /* +/- 125dps => 0.000066579 rad/s */ 210 [2 * INV_ICM42600_GYRO_FS_125DPS] = 0, 211 [2 * INV_ICM42600_GYRO_FS_125DPS + 1] = 66579, 212 /* +/- 62.5dps => 0.000033290 rad/s */ 213 [2 * INV_ICM42600_GYRO_FS_62_5DPS] = 0, 214 [2 * INV_ICM42600_GYRO_FS_62_5DPS + 1] = 33290, 215 /* +/- 31.25dps => 0.000016645 rad/s */ 216 [2 * INV_ICM42600_GYRO_FS_31_25DPS] = 0, 217 [2 * INV_ICM42600_GYRO_FS_31_25DPS + 1] = 16645, 218 /* +/- 15.625dps => 0.000008322 rad/s */ 219 [2 * INV_ICM42600_GYRO_FS_15_625DPS] = 0, 220 [2 * INV_ICM42600_GYRO_FS_15_625DPS + 1] = 8322, 221 }; 222 223 static int inv_icm42600_gyro_read_scale(struct inv_icm42600_state *st, 224 int *val, int *val2) 225 { 226 unsigned int idx; 227 228 idx = st->conf.gyro.fs; 229 230 *val = inv_icm42600_gyro_scale[2 * idx]; 231 *val2 = inv_icm42600_gyro_scale[2 * idx + 1]; 232 return IIO_VAL_INT_PLUS_NANO; 233 } 234 235 static int inv_icm42600_gyro_write_scale(struct inv_icm42600_state *st, 236 int val, int val2) 237 { 238 struct device *dev = regmap_get_device(st->map); 239 unsigned int idx; 240 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT; 241 int ret; 242 243 for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_scale); idx += 2) { 244 if (val == inv_icm42600_gyro_scale[idx] && 245 val2 == inv_icm42600_gyro_scale[idx + 1]) 246 break; 247 } 248 if (idx >= ARRAY_SIZE(inv_icm42600_gyro_scale)) 249 return -EINVAL; 250 251 conf.fs = idx / 2; 252 253 pm_runtime_get_sync(dev); 254 mutex_lock(&st->lock); 255 256 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL); 257 258 mutex_unlock(&st->lock); 259 pm_runtime_mark_last_busy(dev); 260 pm_runtime_put_autosuspend(dev); 261 262 return ret; 263 } 264 265 /* IIO format int + micro */ 266 static const int inv_icm42600_gyro_odr[] = { 267 /* 12.5Hz */ 268 12, 500000, 269 /* 25Hz */ 270 25, 0, 271 /* 50Hz */ 272 50, 0, 273 /* 100Hz */ 274 100, 0, 275 /* 200Hz */ 276 200, 0, 277 /* 1kHz */ 278 1000, 0, 279 /* 2kHz */ 280 2000, 0, 281 /* 4kHz */ 282 4000, 0, 283 }; 284 285 static const int inv_icm42600_gyro_odr_conv[] = { 286 INV_ICM42600_ODR_12_5HZ, 287 INV_ICM42600_ODR_25HZ, 288 INV_ICM42600_ODR_50HZ, 289 INV_ICM42600_ODR_100HZ, 290 INV_ICM42600_ODR_200HZ, 291 INV_ICM42600_ODR_1KHZ_LN, 292 INV_ICM42600_ODR_2KHZ_LN, 293 INV_ICM42600_ODR_4KHZ_LN, 294 }; 295 296 static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state *st, 297 int *val, int *val2) 298 { 299 unsigned int odr; 300 unsigned int i; 301 302 odr = st->conf.gyro.odr; 303 304 for (i = 0; i < ARRAY_SIZE(inv_icm42600_gyro_odr_conv); ++i) { 305 if (inv_icm42600_gyro_odr_conv[i] == odr) 306 break; 307 } 308 if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv)) 309 return -EINVAL; 310 311 *val = inv_icm42600_gyro_odr[2 * i]; 312 *val2 = inv_icm42600_gyro_odr[2 * i + 1]; 313 314 return IIO_VAL_INT_PLUS_MICRO; 315 } 316 317 static int inv_icm42600_gyro_write_odr(struct iio_dev *indio_dev, 318 int val, int val2) 319 { 320 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 321 struct inv_sensors_timestamp *ts = iio_priv(indio_dev); 322 struct device *dev = regmap_get_device(st->map); 323 unsigned int idx; 324 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT; 325 int ret; 326 327 for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_odr); idx += 2) { 328 if (val == inv_icm42600_gyro_odr[idx] && 329 val2 == inv_icm42600_gyro_odr[idx + 1]) 330 break; 331 } 332 if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr)) 333 return -EINVAL; 334 335 conf.odr = inv_icm42600_gyro_odr_conv[idx / 2]; 336 337 pm_runtime_get_sync(dev); 338 mutex_lock(&st->lock); 339 340 ret = inv_sensors_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr), 341 iio_buffer_enabled(indio_dev)); 342 if (ret) 343 goto out_unlock; 344 345 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL); 346 if (ret) 347 goto out_unlock; 348 inv_icm42600_buffer_update_fifo_period(st); 349 inv_icm42600_buffer_update_watermark(st); 350 351 out_unlock: 352 mutex_unlock(&st->lock); 353 pm_runtime_mark_last_busy(dev); 354 pm_runtime_put_autosuspend(dev); 355 356 return ret; 357 } 358 359 /* 360 * Calibration bias values, IIO range format int + nano. 361 * Value is limited to +/-64dps coded on 12 bits signed. Step is 1/32 dps. 362 */ 363 static int inv_icm42600_gyro_calibbias[] = { 364 -1, 117010721, /* min: -1.117010721 rad/s */ 365 0, 545415, /* step: 0.000545415 rad/s */ 366 1, 116465306, /* max: 1.116465306 rad/s */ 367 }; 368 369 static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state *st, 370 struct iio_chan_spec const *chan, 371 int *val, int *val2) 372 { 373 struct device *dev = regmap_get_device(st->map); 374 int64_t val64; 375 int32_t bias; 376 unsigned int reg; 377 int16_t offset; 378 uint8_t data[2]; 379 int ret; 380 381 if (chan->type != IIO_ANGL_VEL) 382 return -EINVAL; 383 384 switch (chan->channel2) { 385 case IIO_MOD_X: 386 reg = INV_ICM42600_REG_OFFSET_USER0; 387 break; 388 case IIO_MOD_Y: 389 reg = INV_ICM42600_REG_OFFSET_USER1; 390 break; 391 case IIO_MOD_Z: 392 reg = INV_ICM42600_REG_OFFSET_USER3; 393 break; 394 default: 395 return -EINVAL; 396 } 397 398 pm_runtime_get_sync(dev); 399 mutex_lock(&st->lock); 400 401 ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data)); 402 memcpy(data, st->buffer, sizeof(data)); 403 404 mutex_unlock(&st->lock); 405 pm_runtime_mark_last_busy(dev); 406 pm_runtime_put_autosuspend(dev); 407 if (ret) 408 return ret; 409 410 /* 12 bits signed value */ 411 switch (chan->channel2) { 412 case IIO_MOD_X: 413 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11); 414 break; 415 case IIO_MOD_Y: 416 offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11); 417 break; 418 case IIO_MOD_Z: 419 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11); 420 break; 421 default: 422 return -EINVAL; 423 } 424 425 /* 426 * convert raw offset to dps then to rad/s 427 * 12 bits signed raw max 64 to dps: 64 / 2048 428 * dps to rad: Pi / 180 429 * result in nano (1000000000) 430 * (offset * 64 * Pi * 1000000000) / (2048 * 180) 431 */ 432 val64 = (int64_t)offset * 64LL * 3141592653LL; 433 /* for rounding, add + or - divisor (2048 * 180) divided by 2 */ 434 if (val64 >= 0) 435 val64 += 2048 * 180 / 2; 436 else 437 val64 -= 2048 * 180 / 2; 438 bias = div_s64(val64, 2048 * 180); 439 *val = bias / 1000000000L; 440 *val2 = bias % 1000000000L; 441 442 return IIO_VAL_INT_PLUS_NANO; 443 } 444 445 static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state *st, 446 struct iio_chan_spec const *chan, 447 int val, int val2) 448 { 449 struct device *dev = regmap_get_device(st->map); 450 int64_t val64, min, max; 451 unsigned int reg, regval; 452 int16_t offset; 453 int ret; 454 455 if (chan->type != IIO_ANGL_VEL) 456 return -EINVAL; 457 458 switch (chan->channel2) { 459 case IIO_MOD_X: 460 reg = INV_ICM42600_REG_OFFSET_USER0; 461 break; 462 case IIO_MOD_Y: 463 reg = INV_ICM42600_REG_OFFSET_USER1; 464 break; 465 case IIO_MOD_Z: 466 reg = INV_ICM42600_REG_OFFSET_USER3; 467 break; 468 default: 469 return -EINVAL; 470 } 471 472 /* inv_icm42600_gyro_calibbias: min - step - max in nano */ 473 min = (int64_t)inv_icm42600_gyro_calibbias[0] * 1000000000LL + 474 (int64_t)inv_icm42600_gyro_calibbias[1]; 475 max = (int64_t)inv_icm42600_gyro_calibbias[4] * 1000000000LL + 476 (int64_t)inv_icm42600_gyro_calibbias[5]; 477 val64 = (int64_t)val * 1000000000LL + (int64_t)val2; 478 if (val64 < min || val64 > max) 479 return -EINVAL; 480 481 /* 482 * convert rad/s to dps then to raw value 483 * rad to dps: 180 / Pi 484 * dps to raw 12 bits signed, max 64: 2048 / 64 485 * val in nano (1000000000) 486 * val * 180 * 2048 / (Pi * 1000000000 * 64) 487 */ 488 val64 = val64 * 180LL * 2048LL; 489 /* for rounding, add + or - divisor (3141592653 * 64) divided by 2 */ 490 if (val64 >= 0) 491 val64 += 3141592653LL * 64LL / 2LL; 492 else 493 val64 -= 3141592653LL * 64LL / 2LL; 494 offset = div64_s64(val64, 3141592653LL * 64LL); 495 496 /* clamp value limited to 12 bits signed */ 497 if (offset < -2048) 498 offset = -2048; 499 else if (offset > 2047) 500 offset = 2047; 501 502 pm_runtime_get_sync(dev); 503 mutex_lock(&st->lock); 504 505 switch (chan->channel2) { 506 case IIO_MOD_X: 507 /* OFFSET_USER1 register is shared */ 508 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1, 509 ®val); 510 if (ret) 511 goto out_unlock; 512 st->buffer[0] = offset & 0xFF; 513 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8); 514 break; 515 case IIO_MOD_Y: 516 /* OFFSET_USER1 register is shared */ 517 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1, 518 ®val); 519 if (ret) 520 goto out_unlock; 521 st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F); 522 st->buffer[1] = offset & 0xFF; 523 break; 524 case IIO_MOD_Z: 525 /* OFFSET_USER4 register is shared */ 526 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4, 527 ®val); 528 if (ret) 529 goto out_unlock; 530 st->buffer[0] = offset & 0xFF; 531 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8); 532 break; 533 default: 534 ret = -EINVAL; 535 goto out_unlock; 536 } 537 538 ret = regmap_bulk_write(st->map, reg, st->buffer, 2); 539 540 out_unlock: 541 mutex_unlock(&st->lock); 542 pm_runtime_mark_last_busy(dev); 543 pm_runtime_put_autosuspend(dev); 544 return ret; 545 } 546 547 static int inv_icm42600_gyro_read_raw(struct iio_dev *indio_dev, 548 struct iio_chan_spec const *chan, 549 int *val, int *val2, long mask) 550 { 551 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 552 int16_t data; 553 int ret; 554 555 switch (chan->type) { 556 case IIO_ANGL_VEL: 557 break; 558 case IIO_TEMP: 559 return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask); 560 default: 561 return -EINVAL; 562 } 563 564 switch (mask) { 565 case IIO_CHAN_INFO_RAW: 566 ret = iio_device_claim_direct_mode(indio_dev); 567 if (ret) 568 return ret; 569 ret = inv_icm42600_gyro_read_sensor(st, chan, &data); 570 iio_device_release_direct_mode(indio_dev); 571 if (ret) 572 return ret; 573 *val = data; 574 return IIO_VAL_INT; 575 case IIO_CHAN_INFO_SCALE: 576 return inv_icm42600_gyro_read_scale(st, val, val2); 577 case IIO_CHAN_INFO_SAMP_FREQ: 578 return inv_icm42600_gyro_read_odr(st, val, val2); 579 case IIO_CHAN_INFO_CALIBBIAS: 580 return inv_icm42600_gyro_read_offset(st, chan, val, val2); 581 default: 582 return -EINVAL; 583 } 584 } 585 586 static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev, 587 struct iio_chan_spec const *chan, 588 const int **vals, 589 int *type, int *length, long mask) 590 { 591 if (chan->type != IIO_ANGL_VEL) 592 return -EINVAL; 593 594 switch (mask) { 595 case IIO_CHAN_INFO_SCALE: 596 *vals = inv_icm42600_gyro_scale; 597 *type = IIO_VAL_INT_PLUS_NANO; 598 *length = ARRAY_SIZE(inv_icm42600_gyro_scale); 599 return IIO_AVAIL_LIST; 600 case IIO_CHAN_INFO_SAMP_FREQ: 601 *vals = inv_icm42600_gyro_odr; 602 *type = IIO_VAL_INT_PLUS_MICRO; 603 *length = ARRAY_SIZE(inv_icm42600_gyro_odr); 604 return IIO_AVAIL_LIST; 605 case IIO_CHAN_INFO_CALIBBIAS: 606 *vals = inv_icm42600_gyro_calibbias; 607 *type = IIO_VAL_INT_PLUS_NANO; 608 return IIO_AVAIL_RANGE; 609 default: 610 return -EINVAL; 611 } 612 } 613 614 static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev, 615 struct iio_chan_spec const *chan, 616 int val, int val2, long mask) 617 { 618 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 619 int ret; 620 621 if (chan->type != IIO_ANGL_VEL) 622 return -EINVAL; 623 624 switch (mask) { 625 case IIO_CHAN_INFO_SCALE: 626 ret = iio_device_claim_direct_mode(indio_dev); 627 if (ret) 628 return ret; 629 ret = inv_icm42600_gyro_write_scale(st, val, val2); 630 iio_device_release_direct_mode(indio_dev); 631 return ret; 632 case IIO_CHAN_INFO_SAMP_FREQ: 633 return inv_icm42600_gyro_write_odr(indio_dev, val, val2); 634 case IIO_CHAN_INFO_CALIBBIAS: 635 ret = iio_device_claim_direct_mode(indio_dev); 636 if (ret) 637 return ret; 638 ret = inv_icm42600_gyro_write_offset(st, chan, val, val2); 639 iio_device_release_direct_mode(indio_dev); 640 return ret; 641 default: 642 return -EINVAL; 643 } 644 } 645 646 static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev, 647 struct iio_chan_spec const *chan, 648 long mask) 649 { 650 if (chan->type != IIO_ANGL_VEL) 651 return -EINVAL; 652 653 switch (mask) { 654 case IIO_CHAN_INFO_SCALE: 655 return IIO_VAL_INT_PLUS_NANO; 656 case IIO_CHAN_INFO_SAMP_FREQ: 657 return IIO_VAL_INT_PLUS_MICRO; 658 case IIO_CHAN_INFO_CALIBBIAS: 659 return IIO_VAL_INT_PLUS_NANO; 660 default: 661 return -EINVAL; 662 } 663 } 664 665 static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev, 666 unsigned int val) 667 { 668 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 669 int ret; 670 671 mutex_lock(&st->lock); 672 673 st->fifo.watermark.gyro = val; 674 ret = inv_icm42600_buffer_update_watermark(st); 675 676 mutex_unlock(&st->lock); 677 678 return ret; 679 } 680 681 static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev, 682 unsigned int count) 683 { 684 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 685 int ret; 686 687 if (count == 0) 688 return 0; 689 690 mutex_lock(&st->lock); 691 692 ret = inv_icm42600_buffer_hwfifo_flush(st, count); 693 if (!ret) 694 ret = st->fifo.nb.gyro; 695 696 mutex_unlock(&st->lock); 697 698 return ret; 699 } 700 701 static const struct iio_info inv_icm42600_gyro_info = { 702 .read_raw = inv_icm42600_gyro_read_raw, 703 .read_avail = inv_icm42600_gyro_read_avail, 704 .write_raw = inv_icm42600_gyro_write_raw, 705 .write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt, 706 .debugfs_reg_access = inv_icm42600_debugfs_reg, 707 .update_scan_mode = inv_icm42600_gyro_update_scan_mode, 708 .hwfifo_set_watermark = inv_icm42600_gyro_hwfifo_set_watermark, 709 .hwfifo_flush_to_buffer = inv_icm42600_gyro_hwfifo_flush, 710 }; 711 712 struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st) 713 { 714 struct device *dev = regmap_get_device(st->map); 715 const char *name; 716 struct inv_sensors_timestamp_chip ts_chip; 717 struct inv_sensors_timestamp *ts; 718 struct iio_dev *indio_dev; 719 int ret; 720 721 name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name); 722 if (!name) 723 return ERR_PTR(-ENOMEM); 724 725 indio_dev = devm_iio_device_alloc(dev, sizeof(*ts)); 726 if (!indio_dev) 727 return ERR_PTR(-ENOMEM); 728 729 /* 730 * clock period is 32kHz (31250ns) 731 * jitter is +/- 2% (20 per mille) 732 */ 733 ts_chip.clock_period = 31250; 734 ts_chip.jitter = 20; 735 ts_chip.init_period = inv_icm42600_odr_to_period(st->conf.accel.odr); 736 ts = iio_priv(indio_dev); 737 inv_sensors_timestamp_init(ts, &ts_chip); 738 739 iio_device_set_drvdata(indio_dev, st); 740 indio_dev->name = name; 741 indio_dev->info = &inv_icm42600_gyro_info; 742 indio_dev->modes = INDIO_DIRECT_MODE; 743 indio_dev->channels = inv_icm42600_gyro_channels; 744 indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels); 745 indio_dev->available_scan_masks = inv_icm42600_gyro_scan_masks; 746 indio_dev->setup_ops = &inv_icm42600_buffer_ops; 747 748 ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, 749 &inv_icm42600_buffer_ops); 750 if (ret) 751 return ERR_PTR(ret); 752 753 ret = devm_iio_device_register(dev, indio_dev); 754 if (ret) 755 return ERR_PTR(ret); 756 757 return indio_dev; 758 } 759 760 int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev) 761 { 762 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev); 763 struct inv_sensors_timestamp *ts = iio_priv(indio_dev); 764 ssize_t i, size; 765 unsigned int no; 766 const void *accel, *gyro, *timestamp; 767 const int8_t *temp; 768 unsigned int odr; 769 int64_t ts_val; 770 struct inv_icm42600_gyro_buffer buffer; 771 772 /* parse all fifo packets */ 773 for (i = 0, no = 0; i < st->fifo.count; i += size, ++no) { 774 size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i], 775 &accel, &gyro, &temp, ×tamp, &odr); 776 /* quit if error or FIFO is empty */ 777 if (size <= 0) 778 return size; 779 780 /* skip packet if no gyro data or data is invalid */ 781 if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro)) 782 continue; 783 784 /* update odr */ 785 if (odr & INV_ICM42600_SENSOR_GYRO) 786 inv_sensors_timestamp_apply_odr(ts, st->fifo.period, 787 st->fifo.nb.total, no); 788 789 /* buffer is copied to userspace, zeroing it to avoid any data leak */ 790 memset(&buffer, 0, sizeof(buffer)); 791 memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro)); 792 /* convert 8 bits FIFO temperature in high resolution format */ 793 buffer.temp = temp ? (*temp * 64) : 0; 794 ts_val = inv_sensors_timestamp_pop(ts); 795 iio_push_to_buffers_with_timestamp(indio_dev, &buffer, ts_val); 796 } 797 798 return 0; 799 } 800