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