0001
0002
0003
0004
0005
0006 #include <linux/kernel.h>
0007 #include <linux/device.h>
0008 #include <linux/mutex.h>
0009 #include <linux/pm_runtime.h>
0010 #include <linux/regmap.h>
0011 #include <linux/delay.h>
0012 #include <linux/math64.h>
0013 #include <linux/iio/iio.h>
0014 #include <linux/iio/buffer.h>
0015 #include <linux/iio/kfifo_buf.h>
0016
0017 #include "inv_icm42600.h"
0018 #include "inv_icm42600_temp.h"
0019 #include "inv_icm42600_buffer.h"
0020 #include "inv_icm42600_timestamp.h"
0021
0022 #define INV_ICM42600_GYRO_CHAN(_modifier, _index, _ext_info) \
0023 { \
0024 .type = IIO_ANGL_VEL, \
0025 .modified = 1, \
0026 .channel2 = _modifier, \
0027 .info_mask_separate = \
0028 BIT(IIO_CHAN_INFO_RAW) | \
0029 BIT(IIO_CHAN_INFO_CALIBBIAS), \
0030 .info_mask_shared_by_type = \
0031 BIT(IIO_CHAN_INFO_SCALE), \
0032 .info_mask_shared_by_type_available = \
0033 BIT(IIO_CHAN_INFO_SCALE) | \
0034 BIT(IIO_CHAN_INFO_CALIBBIAS), \
0035 .info_mask_shared_by_all = \
0036 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
0037 .info_mask_shared_by_all_available = \
0038 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
0039 .scan_index = _index, \
0040 .scan_type = { \
0041 .sign = 's', \
0042 .realbits = 16, \
0043 .storagebits = 16, \
0044 .endianness = IIO_BE, \
0045 }, \
0046 .ext_info = _ext_info, \
0047 }
0048
0049 enum inv_icm42600_gyro_scan {
0050 INV_ICM42600_GYRO_SCAN_X,
0051 INV_ICM42600_GYRO_SCAN_Y,
0052 INV_ICM42600_GYRO_SCAN_Z,
0053 INV_ICM42600_GYRO_SCAN_TEMP,
0054 INV_ICM42600_GYRO_SCAN_TIMESTAMP,
0055 };
0056
0057 static const struct iio_chan_spec_ext_info inv_icm42600_gyro_ext_infos[] = {
0058 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, inv_icm42600_get_mount_matrix),
0059 {},
0060 };
0061
0062 static const struct iio_chan_spec inv_icm42600_gyro_channels[] = {
0063 INV_ICM42600_GYRO_CHAN(IIO_MOD_X, INV_ICM42600_GYRO_SCAN_X,
0064 inv_icm42600_gyro_ext_infos),
0065 INV_ICM42600_GYRO_CHAN(IIO_MOD_Y, INV_ICM42600_GYRO_SCAN_Y,
0066 inv_icm42600_gyro_ext_infos),
0067 INV_ICM42600_GYRO_CHAN(IIO_MOD_Z, INV_ICM42600_GYRO_SCAN_Z,
0068 inv_icm42600_gyro_ext_infos),
0069 INV_ICM42600_TEMP_CHAN(INV_ICM42600_GYRO_SCAN_TEMP),
0070 IIO_CHAN_SOFT_TIMESTAMP(INV_ICM42600_GYRO_SCAN_TIMESTAMP),
0071 };
0072
0073
0074
0075
0076
0077 struct inv_icm42600_gyro_buffer {
0078 struct inv_icm42600_fifo_sensor_data gyro;
0079 int16_t temp;
0080 int64_t timestamp __aligned(8);
0081 };
0082
0083 #define INV_ICM42600_SCAN_MASK_GYRO_3AXIS \
0084 (BIT(INV_ICM42600_GYRO_SCAN_X) | \
0085 BIT(INV_ICM42600_GYRO_SCAN_Y) | \
0086 BIT(INV_ICM42600_GYRO_SCAN_Z))
0087
0088 #define INV_ICM42600_SCAN_MASK_TEMP BIT(INV_ICM42600_GYRO_SCAN_TEMP)
0089
0090 static const unsigned long inv_icm42600_gyro_scan_masks[] = {
0091
0092 INV_ICM42600_SCAN_MASK_GYRO_3AXIS | INV_ICM42600_SCAN_MASK_TEMP,
0093 0,
0094 };
0095
0096
0097 static int inv_icm42600_gyro_update_scan_mode(struct iio_dev *indio_dev,
0098 const unsigned long *scan_mask)
0099 {
0100 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
0101 struct inv_icm42600_timestamp *ts = iio_priv(indio_dev);
0102 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
0103 unsigned int fifo_en = 0;
0104 unsigned int sleep_gyro = 0;
0105 unsigned int sleep_temp = 0;
0106 unsigned int sleep;
0107 int ret;
0108
0109 mutex_lock(&st->lock);
0110
0111 if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
0112
0113 ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
0114 if (ret)
0115 goto out_unlock;
0116 fifo_en |= INV_ICM42600_SENSOR_TEMP;
0117 }
0118
0119 if (*scan_mask & INV_ICM42600_SCAN_MASK_GYRO_3AXIS) {
0120
0121 conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
0122 ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_gyro);
0123 if (ret)
0124 goto out_unlock;
0125 fifo_en |= INV_ICM42600_SENSOR_GYRO;
0126 }
0127
0128
0129 inv_icm42600_timestamp_apply_odr(ts, 0, 0, 0);
0130 ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
0131 if (ret)
0132 goto out_unlock;
0133
0134 ret = inv_icm42600_buffer_update_watermark(st);
0135
0136 out_unlock:
0137 mutex_unlock(&st->lock);
0138
0139 if (sleep_gyro > sleep_temp)
0140 sleep = sleep_gyro;
0141 else
0142 sleep = sleep_temp;
0143 if (sleep)
0144 msleep(sleep);
0145 return ret;
0146 }
0147
0148 static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
0149 struct iio_chan_spec const *chan,
0150 int16_t *val)
0151 {
0152 struct device *dev = regmap_get_device(st->map);
0153 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
0154 unsigned int reg;
0155 __be16 *data;
0156 int ret;
0157
0158 if (chan->type != IIO_ANGL_VEL)
0159 return -EINVAL;
0160
0161 switch (chan->channel2) {
0162 case IIO_MOD_X:
0163 reg = INV_ICM42600_REG_GYRO_DATA_X;
0164 break;
0165 case IIO_MOD_Y:
0166 reg = INV_ICM42600_REG_GYRO_DATA_Y;
0167 break;
0168 case IIO_MOD_Z:
0169 reg = INV_ICM42600_REG_GYRO_DATA_Z;
0170 break;
0171 default:
0172 return -EINVAL;
0173 }
0174
0175 pm_runtime_get_sync(dev);
0176 mutex_lock(&st->lock);
0177
0178
0179 conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
0180 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
0181 if (ret)
0182 goto exit;
0183
0184
0185 data = (__be16 *)&st->buffer[0];
0186 ret = regmap_bulk_read(st->map, reg, data, sizeof(*data));
0187 if (ret)
0188 goto exit;
0189
0190 *val = (int16_t)be16_to_cpup(data);
0191 if (*val == INV_ICM42600_DATA_INVALID)
0192 ret = -EINVAL;
0193 exit:
0194 mutex_unlock(&st->lock);
0195 pm_runtime_mark_last_busy(dev);
0196 pm_runtime_put_autosuspend(dev);
0197 return ret;
0198 }
0199
0200
0201 static const int inv_icm42600_gyro_scale[] = {
0202
0203 [2 * INV_ICM42600_GYRO_FS_2000DPS] = 0,
0204 [2 * INV_ICM42600_GYRO_FS_2000DPS + 1] = 1065264,
0205
0206 [2 * INV_ICM42600_GYRO_FS_1000DPS] = 0,
0207 [2 * INV_ICM42600_GYRO_FS_1000DPS + 1] = 532632,
0208
0209 [2 * INV_ICM42600_GYRO_FS_500DPS] = 0,
0210 [2 * INV_ICM42600_GYRO_FS_500DPS + 1] = 266316,
0211
0212 [2 * INV_ICM42600_GYRO_FS_250DPS] = 0,
0213 [2 * INV_ICM42600_GYRO_FS_250DPS + 1] = 133158,
0214
0215 [2 * INV_ICM42600_GYRO_FS_125DPS] = 0,
0216 [2 * INV_ICM42600_GYRO_FS_125DPS + 1] = 66579,
0217
0218 [2 * INV_ICM42600_GYRO_FS_62_5DPS] = 0,
0219 [2 * INV_ICM42600_GYRO_FS_62_5DPS + 1] = 33290,
0220
0221 [2 * INV_ICM42600_GYRO_FS_31_25DPS] = 0,
0222 [2 * INV_ICM42600_GYRO_FS_31_25DPS + 1] = 16645,
0223
0224 [2 * INV_ICM42600_GYRO_FS_15_625DPS] = 0,
0225 [2 * INV_ICM42600_GYRO_FS_15_625DPS + 1] = 8322,
0226 };
0227
0228 static int inv_icm42600_gyro_read_scale(struct inv_icm42600_state *st,
0229 int *val, int *val2)
0230 {
0231 unsigned int idx;
0232
0233 idx = st->conf.gyro.fs;
0234
0235 *val = inv_icm42600_gyro_scale[2 * idx];
0236 *val2 = inv_icm42600_gyro_scale[2 * idx + 1];
0237 return IIO_VAL_INT_PLUS_NANO;
0238 }
0239
0240 static int inv_icm42600_gyro_write_scale(struct inv_icm42600_state *st,
0241 int val, int val2)
0242 {
0243 struct device *dev = regmap_get_device(st->map);
0244 unsigned int idx;
0245 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
0246 int ret;
0247
0248 for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_scale); idx += 2) {
0249 if (val == inv_icm42600_gyro_scale[idx] &&
0250 val2 == inv_icm42600_gyro_scale[idx + 1])
0251 break;
0252 }
0253 if (idx >= ARRAY_SIZE(inv_icm42600_gyro_scale))
0254 return -EINVAL;
0255
0256 conf.fs = idx / 2;
0257
0258 pm_runtime_get_sync(dev);
0259 mutex_lock(&st->lock);
0260
0261 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
0262
0263 mutex_unlock(&st->lock);
0264 pm_runtime_mark_last_busy(dev);
0265 pm_runtime_put_autosuspend(dev);
0266
0267 return ret;
0268 }
0269
0270
0271 static const int inv_icm42600_gyro_odr[] = {
0272
0273 12, 500000,
0274
0275 25, 0,
0276
0277 50, 0,
0278
0279 100, 0,
0280
0281 200, 0,
0282
0283 1000, 0,
0284
0285 2000, 0,
0286
0287 4000, 0,
0288 };
0289
0290 static const int inv_icm42600_gyro_odr_conv[] = {
0291 INV_ICM42600_ODR_12_5HZ,
0292 INV_ICM42600_ODR_25HZ,
0293 INV_ICM42600_ODR_50HZ,
0294 INV_ICM42600_ODR_100HZ,
0295 INV_ICM42600_ODR_200HZ,
0296 INV_ICM42600_ODR_1KHZ_LN,
0297 INV_ICM42600_ODR_2KHZ_LN,
0298 INV_ICM42600_ODR_4KHZ_LN,
0299 };
0300
0301 static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state *st,
0302 int *val, int *val2)
0303 {
0304 unsigned int odr;
0305 unsigned int i;
0306
0307 odr = st->conf.gyro.odr;
0308
0309 for (i = 0; i < ARRAY_SIZE(inv_icm42600_gyro_odr_conv); ++i) {
0310 if (inv_icm42600_gyro_odr_conv[i] == odr)
0311 break;
0312 }
0313 if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv))
0314 return -EINVAL;
0315
0316 *val = inv_icm42600_gyro_odr[2 * i];
0317 *val2 = inv_icm42600_gyro_odr[2 * i + 1];
0318
0319 return IIO_VAL_INT_PLUS_MICRO;
0320 }
0321
0322 static int inv_icm42600_gyro_write_odr(struct iio_dev *indio_dev,
0323 int val, int val2)
0324 {
0325 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
0326 struct inv_icm42600_timestamp *ts = iio_priv(indio_dev);
0327 struct device *dev = regmap_get_device(st->map);
0328 unsigned int idx;
0329 struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
0330 int ret;
0331
0332 for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_odr); idx += 2) {
0333 if (val == inv_icm42600_gyro_odr[idx] &&
0334 val2 == inv_icm42600_gyro_odr[idx + 1])
0335 break;
0336 }
0337 if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr))
0338 return -EINVAL;
0339
0340 conf.odr = inv_icm42600_gyro_odr_conv[idx / 2];
0341
0342 pm_runtime_get_sync(dev);
0343 mutex_lock(&st->lock);
0344
0345 ret = inv_icm42600_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr),
0346 iio_buffer_enabled(indio_dev));
0347 if (ret)
0348 goto out_unlock;
0349
0350 ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
0351 if (ret)
0352 goto out_unlock;
0353 inv_icm42600_buffer_update_fifo_period(st);
0354 inv_icm42600_buffer_update_watermark(st);
0355
0356 out_unlock:
0357 mutex_unlock(&st->lock);
0358 pm_runtime_mark_last_busy(dev);
0359 pm_runtime_put_autosuspend(dev);
0360
0361 return ret;
0362 }
0363
0364
0365
0366
0367
0368 static int inv_icm42600_gyro_calibbias[] = {
0369 -1, 117010721,
0370 0, 545415,
0371 1, 116465306,
0372 };
0373
0374 static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state *st,
0375 struct iio_chan_spec const *chan,
0376 int *val, int *val2)
0377 {
0378 struct device *dev = regmap_get_device(st->map);
0379 int64_t val64;
0380 int32_t bias;
0381 unsigned int reg;
0382 int16_t offset;
0383 uint8_t data[2];
0384 int ret;
0385
0386 if (chan->type != IIO_ANGL_VEL)
0387 return -EINVAL;
0388
0389 switch (chan->channel2) {
0390 case IIO_MOD_X:
0391 reg = INV_ICM42600_REG_OFFSET_USER0;
0392 break;
0393 case IIO_MOD_Y:
0394 reg = INV_ICM42600_REG_OFFSET_USER1;
0395 break;
0396 case IIO_MOD_Z:
0397 reg = INV_ICM42600_REG_OFFSET_USER3;
0398 break;
0399 default:
0400 return -EINVAL;
0401 }
0402
0403 pm_runtime_get_sync(dev);
0404 mutex_lock(&st->lock);
0405
0406 ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data));
0407 memcpy(data, st->buffer, sizeof(data));
0408
0409 mutex_unlock(&st->lock);
0410 pm_runtime_mark_last_busy(dev);
0411 pm_runtime_put_autosuspend(dev);
0412 if (ret)
0413 return ret;
0414
0415
0416 switch (chan->channel2) {
0417 case IIO_MOD_X:
0418 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
0419 break;
0420 case IIO_MOD_Y:
0421 offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
0422 break;
0423 case IIO_MOD_Z:
0424 offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
0425 break;
0426 default:
0427 return -EINVAL;
0428 }
0429
0430
0431
0432
0433
0434
0435
0436
0437 val64 = (int64_t)offset * 64LL * 3141592653LL;
0438
0439 if (val64 >= 0)
0440 val64 += 2048 * 180 / 2;
0441 else
0442 val64 -= 2048 * 180 / 2;
0443 bias = div_s64(val64, 2048 * 180);
0444 *val = bias / 1000000000L;
0445 *val2 = bias % 1000000000L;
0446
0447 return IIO_VAL_INT_PLUS_NANO;
0448 }
0449
0450 static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state *st,
0451 struct iio_chan_spec const *chan,
0452 int val, int val2)
0453 {
0454 struct device *dev = regmap_get_device(st->map);
0455 int64_t val64, min, max;
0456 unsigned int reg, regval;
0457 int16_t offset;
0458 int ret;
0459
0460 if (chan->type != IIO_ANGL_VEL)
0461 return -EINVAL;
0462
0463 switch (chan->channel2) {
0464 case IIO_MOD_X:
0465 reg = INV_ICM42600_REG_OFFSET_USER0;
0466 break;
0467 case IIO_MOD_Y:
0468 reg = INV_ICM42600_REG_OFFSET_USER1;
0469 break;
0470 case IIO_MOD_Z:
0471 reg = INV_ICM42600_REG_OFFSET_USER3;
0472 break;
0473 default:
0474 return -EINVAL;
0475 }
0476
0477
0478 min = (int64_t)inv_icm42600_gyro_calibbias[0] * 1000000000LL +
0479 (int64_t)inv_icm42600_gyro_calibbias[1];
0480 max = (int64_t)inv_icm42600_gyro_calibbias[4] * 1000000000LL +
0481 (int64_t)inv_icm42600_gyro_calibbias[5];
0482 val64 = (int64_t)val * 1000000000LL + (int64_t)val2;
0483 if (val64 < min || val64 > max)
0484 return -EINVAL;
0485
0486
0487
0488
0489
0490
0491
0492
0493 val64 = val64 * 180LL * 2048LL;
0494
0495 if (val64 >= 0)
0496 val64 += 3141592653LL * 64LL / 2LL;
0497 else
0498 val64 -= 3141592653LL * 64LL / 2LL;
0499 offset = div64_s64(val64, 3141592653LL * 64LL);
0500
0501
0502 if (offset < -2048)
0503 offset = -2048;
0504 else if (offset > 2047)
0505 offset = 2047;
0506
0507 pm_runtime_get_sync(dev);
0508 mutex_lock(&st->lock);
0509
0510 switch (chan->channel2) {
0511 case IIO_MOD_X:
0512
0513 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
0514 ®val);
0515 if (ret)
0516 goto out_unlock;
0517 st->buffer[0] = offset & 0xFF;
0518 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
0519 break;
0520 case IIO_MOD_Y:
0521
0522 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
0523 ®val);
0524 if (ret)
0525 goto out_unlock;
0526 st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
0527 st->buffer[1] = offset & 0xFF;
0528 break;
0529 case IIO_MOD_Z:
0530
0531 ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4,
0532 ®val);
0533 if (ret)
0534 goto out_unlock;
0535 st->buffer[0] = offset & 0xFF;
0536 st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
0537 break;
0538 default:
0539 ret = -EINVAL;
0540 goto out_unlock;
0541 }
0542
0543 ret = regmap_bulk_write(st->map, reg, st->buffer, 2);
0544
0545 out_unlock:
0546 mutex_unlock(&st->lock);
0547 pm_runtime_mark_last_busy(dev);
0548 pm_runtime_put_autosuspend(dev);
0549 return ret;
0550 }
0551
0552 static int inv_icm42600_gyro_read_raw(struct iio_dev *indio_dev,
0553 struct iio_chan_spec const *chan,
0554 int *val, int *val2, long mask)
0555 {
0556 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
0557 int16_t data;
0558 int ret;
0559
0560 switch (chan->type) {
0561 case IIO_ANGL_VEL:
0562 break;
0563 case IIO_TEMP:
0564 return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask);
0565 default:
0566 return -EINVAL;
0567 }
0568
0569 switch (mask) {
0570 case IIO_CHAN_INFO_RAW:
0571 ret = iio_device_claim_direct_mode(indio_dev);
0572 if (ret)
0573 return ret;
0574 ret = inv_icm42600_gyro_read_sensor(st, chan, &data);
0575 iio_device_release_direct_mode(indio_dev);
0576 if (ret)
0577 return ret;
0578 *val = data;
0579 return IIO_VAL_INT;
0580 case IIO_CHAN_INFO_SCALE:
0581 return inv_icm42600_gyro_read_scale(st, val, val2);
0582 case IIO_CHAN_INFO_SAMP_FREQ:
0583 return inv_icm42600_gyro_read_odr(st, val, val2);
0584 case IIO_CHAN_INFO_CALIBBIAS:
0585 return inv_icm42600_gyro_read_offset(st, chan, val, val2);
0586 default:
0587 return -EINVAL;
0588 }
0589 }
0590
0591 static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev,
0592 struct iio_chan_spec const *chan,
0593 const int **vals,
0594 int *type, int *length, long mask)
0595 {
0596 if (chan->type != IIO_ANGL_VEL)
0597 return -EINVAL;
0598
0599 switch (mask) {
0600 case IIO_CHAN_INFO_SCALE:
0601 *vals = inv_icm42600_gyro_scale;
0602 *type = IIO_VAL_INT_PLUS_NANO;
0603 *length = ARRAY_SIZE(inv_icm42600_gyro_scale);
0604 return IIO_AVAIL_LIST;
0605 case IIO_CHAN_INFO_SAMP_FREQ:
0606 *vals = inv_icm42600_gyro_odr;
0607 *type = IIO_VAL_INT_PLUS_MICRO;
0608 *length = ARRAY_SIZE(inv_icm42600_gyro_odr);
0609 return IIO_AVAIL_LIST;
0610 case IIO_CHAN_INFO_CALIBBIAS:
0611 *vals = inv_icm42600_gyro_calibbias;
0612 *type = IIO_VAL_INT_PLUS_NANO;
0613 return IIO_AVAIL_RANGE;
0614 default:
0615 return -EINVAL;
0616 }
0617 }
0618
0619 static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev,
0620 struct iio_chan_spec const *chan,
0621 int val, int val2, long mask)
0622 {
0623 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
0624 int ret;
0625
0626 if (chan->type != IIO_ANGL_VEL)
0627 return -EINVAL;
0628
0629 switch (mask) {
0630 case IIO_CHAN_INFO_SCALE:
0631 ret = iio_device_claim_direct_mode(indio_dev);
0632 if (ret)
0633 return ret;
0634 ret = inv_icm42600_gyro_write_scale(st, val, val2);
0635 iio_device_release_direct_mode(indio_dev);
0636 return ret;
0637 case IIO_CHAN_INFO_SAMP_FREQ:
0638 return inv_icm42600_gyro_write_odr(indio_dev, val, val2);
0639 case IIO_CHAN_INFO_CALIBBIAS:
0640 ret = iio_device_claim_direct_mode(indio_dev);
0641 if (ret)
0642 return ret;
0643 ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
0644 iio_device_release_direct_mode(indio_dev);
0645 return ret;
0646 default:
0647 return -EINVAL;
0648 }
0649 }
0650
0651 static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
0652 struct iio_chan_spec const *chan,
0653 long mask)
0654 {
0655 if (chan->type != IIO_ANGL_VEL)
0656 return -EINVAL;
0657
0658 switch (mask) {
0659 case IIO_CHAN_INFO_SCALE:
0660 return IIO_VAL_INT_PLUS_NANO;
0661 case IIO_CHAN_INFO_SAMP_FREQ:
0662 return IIO_VAL_INT_PLUS_MICRO;
0663 case IIO_CHAN_INFO_CALIBBIAS:
0664 return IIO_VAL_INT_PLUS_NANO;
0665 default:
0666 return -EINVAL;
0667 }
0668 }
0669
0670 static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
0671 unsigned int val)
0672 {
0673 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
0674 int ret;
0675
0676 mutex_lock(&st->lock);
0677
0678 st->fifo.watermark.gyro = val;
0679 ret = inv_icm42600_buffer_update_watermark(st);
0680
0681 mutex_unlock(&st->lock);
0682
0683 return ret;
0684 }
0685
0686 static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
0687 unsigned int count)
0688 {
0689 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
0690 int ret;
0691
0692 if (count == 0)
0693 return 0;
0694
0695 mutex_lock(&st->lock);
0696
0697 ret = inv_icm42600_buffer_hwfifo_flush(st, count);
0698 if (!ret)
0699 ret = st->fifo.nb.gyro;
0700
0701 mutex_unlock(&st->lock);
0702
0703 return ret;
0704 }
0705
0706 static const struct iio_info inv_icm42600_gyro_info = {
0707 .read_raw = inv_icm42600_gyro_read_raw,
0708 .read_avail = inv_icm42600_gyro_read_avail,
0709 .write_raw = inv_icm42600_gyro_write_raw,
0710 .write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt,
0711 .debugfs_reg_access = inv_icm42600_debugfs_reg,
0712 .update_scan_mode = inv_icm42600_gyro_update_scan_mode,
0713 .hwfifo_set_watermark = inv_icm42600_gyro_hwfifo_set_watermark,
0714 .hwfifo_flush_to_buffer = inv_icm42600_gyro_hwfifo_flush,
0715 };
0716
0717 struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
0718 {
0719 struct device *dev = regmap_get_device(st->map);
0720 const char *name;
0721 struct inv_icm42600_timestamp *ts;
0722 struct iio_dev *indio_dev;
0723 int ret;
0724
0725 name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
0726 if (!name)
0727 return ERR_PTR(-ENOMEM);
0728
0729 indio_dev = devm_iio_device_alloc(dev, sizeof(*ts));
0730 if (!indio_dev)
0731 return ERR_PTR(-ENOMEM);
0732
0733 ts = iio_priv(indio_dev);
0734 inv_icm42600_timestamp_init(ts, inv_icm42600_odr_to_period(st->conf.gyro.odr));
0735
0736 iio_device_set_drvdata(indio_dev, st);
0737 indio_dev->name = name;
0738 indio_dev->info = &inv_icm42600_gyro_info;
0739 indio_dev->modes = INDIO_DIRECT_MODE;
0740 indio_dev->channels = inv_icm42600_gyro_channels;
0741 indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels);
0742 indio_dev->available_scan_masks = inv_icm42600_gyro_scan_masks;
0743 indio_dev->setup_ops = &inv_icm42600_buffer_ops;
0744
0745 ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
0746 &inv_icm42600_buffer_ops);
0747 if (ret)
0748 return ERR_PTR(ret);
0749
0750 ret = devm_iio_device_register(dev, indio_dev);
0751 if (ret)
0752 return ERR_PTR(ret);
0753
0754 return indio_dev;
0755 }
0756
0757 int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev)
0758 {
0759 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
0760 struct inv_icm42600_timestamp *ts = iio_priv(indio_dev);
0761 ssize_t i, size;
0762 unsigned int no;
0763 const void *accel, *gyro, *timestamp;
0764 const int8_t *temp;
0765 unsigned int odr;
0766 int64_t ts_val;
0767 struct inv_icm42600_gyro_buffer buffer;
0768
0769
0770 for (i = 0, no = 0; i < st->fifo.count; i += size, ++no) {
0771 size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
0772 &accel, &gyro, &temp, ×tamp, &odr);
0773
0774 if (size <= 0)
0775 return size;
0776
0777
0778 if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro))
0779 continue;
0780
0781
0782 if (odr & INV_ICM42600_SENSOR_GYRO)
0783 inv_icm42600_timestamp_apply_odr(ts, st->fifo.period,
0784 st->fifo.nb.total, no);
0785
0786
0787 memset(&buffer, 0, sizeof(buffer));
0788 memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro));
0789
0790 buffer.temp = temp ? (*temp * 64) : 0;
0791 ts_val = inv_icm42600_timestamp_pop(ts);
0792 iio_push_to_buffers_with_timestamp(indio_dev, &buffer, ts_val);
0793 }
0794
0795 return 0;
0796 }