0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/device.h>
0012 #include <linux/module.h>
0013 #include <linux/mod_devicetable.h>
0014 #include <linux/i2c.h>
0015 #include <linux/pm_runtime.h>
0016 #include <linux/crc8.h>
0017 #include <linux/mutex.h>
0018 #include <linux/delay.h>
0019 #include <linux/log2.h>
0020 #include <linux/math64.h>
0021 #include <linux/regulator/consumer.h>
0022 #include <linux/iio/iio.h>
0023
0024 #define ICP10100_ID_REG_GET(_reg) ((_reg) & 0x003F)
0025 #define ICP10100_ID_REG 0x08
0026 #define ICP10100_RESPONSE_WORD_LENGTH 3
0027 #define ICP10100_CRC8_WORD_LENGTH 2
0028 #define ICP10100_CRC8_POLYNOMIAL 0x31
0029 #define ICP10100_CRC8_INIT 0xFF
0030
0031 enum icp10100_mode {
0032 ICP10100_MODE_LP,
0033 ICP10100_MODE_N,
0034 ICP10100_MODE_LN,
0035 ICP10100_MODE_ULN,
0036 ICP10100_MODE_NB,
0037 };
0038
0039 struct icp10100_state {
0040 struct mutex lock;
0041 struct i2c_client *client;
0042 struct regulator *vdd;
0043 enum icp10100_mode mode;
0044 int16_t cal[4];
0045 };
0046
0047 struct icp10100_command {
0048 __be16 cmd;
0049 unsigned long wait_us;
0050 unsigned long wait_max_us;
0051 size_t response_word_nb;
0052 };
0053
0054 static const struct icp10100_command icp10100_cmd_soft_reset = {
0055 .cmd = cpu_to_be16(0x805D),
0056 .wait_us = 170,
0057 .wait_max_us = 200,
0058 .response_word_nb = 0,
0059 };
0060
0061 static const struct icp10100_command icp10100_cmd_read_id = {
0062 .cmd = cpu_to_be16(0xEFC8),
0063 .wait_us = 0,
0064 .response_word_nb = 1,
0065 };
0066
0067 static const struct icp10100_command icp10100_cmd_read_otp = {
0068 .cmd = cpu_to_be16(0xC7F7),
0069 .wait_us = 0,
0070 .response_word_nb = 1,
0071 };
0072
0073 static const struct icp10100_command icp10100_cmd_measure[] = {
0074 [ICP10100_MODE_LP] = {
0075 .cmd = cpu_to_be16(0x401A),
0076 .wait_us = 1800,
0077 .wait_max_us = 2000,
0078 .response_word_nb = 3,
0079 },
0080 [ICP10100_MODE_N] = {
0081 .cmd = cpu_to_be16(0x48A3),
0082 .wait_us = 6300,
0083 .wait_max_us = 6500,
0084 .response_word_nb = 3,
0085 },
0086 [ICP10100_MODE_LN] = {
0087 .cmd = cpu_to_be16(0x5059),
0088 .wait_us = 23800,
0089 .wait_max_us = 24000,
0090 .response_word_nb = 3,
0091 },
0092 [ICP10100_MODE_ULN] = {
0093 .cmd = cpu_to_be16(0x58E0),
0094 .wait_us = 94500,
0095 .wait_max_us = 94700,
0096 .response_word_nb = 3,
0097 },
0098 };
0099
0100 static const uint8_t icp10100_switch_mode_otp[] =
0101 {0xC5, 0x95, 0x00, 0x66, 0x9c};
0102
0103 DECLARE_CRC8_TABLE(icp10100_crc8_table);
0104
0105 static inline int icp10100_i2c_xfer(struct i2c_adapter *adap,
0106 struct i2c_msg *msgs, int num)
0107 {
0108 int ret;
0109
0110 ret = i2c_transfer(adap, msgs, num);
0111 if (ret < 0)
0112 return ret;
0113
0114 if (ret != num)
0115 return -EIO;
0116
0117 return 0;
0118 }
0119
0120 static int icp10100_send_cmd(struct icp10100_state *st,
0121 const struct icp10100_command *cmd,
0122 __be16 *buf, size_t buf_len)
0123 {
0124 size_t size = cmd->response_word_nb * ICP10100_RESPONSE_WORD_LENGTH;
0125 uint8_t data[16];
0126 uint8_t *ptr;
0127 uint8_t *buf_ptr = (uint8_t *)buf;
0128 struct i2c_msg msgs[2] = {
0129 {
0130 .addr = st->client->addr,
0131 .flags = 0,
0132 .len = 2,
0133 .buf = (uint8_t *)&cmd->cmd,
0134 }, {
0135 .addr = st->client->addr,
0136 .flags = I2C_M_RD,
0137 .len = size,
0138 .buf = data,
0139 },
0140 };
0141 uint8_t crc;
0142 unsigned int i;
0143 int ret;
0144
0145 if (size > sizeof(data))
0146 return -EINVAL;
0147
0148 if (cmd->response_word_nb > 0 &&
0149 (buf == NULL || buf_len < (cmd->response_word_nb * 2)))
0150 return -EINVAL;
0151
0152 dev_dbg(&st->client->dev, "sending cmd %#x\n", be16_to_cpu(cmd->cmd));
0153
0154 if (cmd->response_word_nb > 0 && cmd->wait_us == 0) {
0155
0156 ret = icp10100_i2c_xfer(st->client->adapter, msgs,
0157 ARRAY_SIZE(msgs));
0158 if (ret)
0159 return ret;
0160 } else {
0161
0162 ret = icp10100_i2c_xfer(st->client->adapter, &msgs[0], 1);
0163 if (ret)
0164 return ret;
0165 if (cmd->wait_us > 0)
0166 usleep_range(cmd->wait_us, cmd->wait_max_us);
0167
0168 if (cmd->response_word_nb > 0) {
0169 ret = icp10100_i2c_xfer(st->client->adapter, &msgs[1], 1);
0170 if (ret)
0171 return ret;
0172 } else {
0173 return 0;
0174 }
0175 }
0176
0177
0178 for (i = 0; i < cmd->response_word_nb; ++i) {
0179 ptr = &data[i * ICP10100_RESPONSE_WORD_LENGTH];
0180 crc = crc8(icp10100_crc8_table, ptr, ICP10100_CRC8_WORD_LENGTH,
0181 ICP10100_CRC8_INIT);
0182 if (crc != ptr[ICP10100_CRC8_WORD_LENGTH]) {
0183 dev_err(&st->client->dev, "crc error recv=%#x calc=%#x\n",
0184 ptr[ICP10100_CRC8_WORD_LENGTH], crc);
0185 return -EIO;
0186 }
0187 *buf_ptr++ = ptr[0];
0188 *buf_ptr++ = ptr[1];
0189 }
0190
0191 return 0;
0192 }
0193
0194 static int icp10100_read_cal_otp(struct icp10100_state *st)
0195 {
0196 __be16 val;
0197 int i;
0198 int ret;
0199
0200
0201 ret = i2c_master_send(st->client, icp10100_switch_mode_otp,
0202 ARRAY_SIZE(icp10100_switch_mode_otp));
0203 if (ret < 0)
0204 return ret;
0205 if (ret != ARRAY_SIZE(icp10100_switch_mode_otp))
0206 return -EIO;
0207
0208
0209 for (i = 0; i < 4; ++i) {
0210 ret = icp10100_send_cmd(st, &icp10100_cmd_read_otp,
0211 &val, sizeof(val));
0212 if (ret)
0213 return ret;
0214 st->cal[i] = be16_to_cpu(val);
0215 dev_dbg(&st->client->dev, "cal[%d] = %d\n", i, st->cal[i]);
0216 }
0217
0218 return 0;
0219 }
0220
0221 static int icp10100_init_chip(struct icp10100_state *st)
0222 {
0223 __be16 val;
0224 uint16_t id;
0225 int ret;
0226
0227
0228 ret = icp10100_send_cmd(st, &icp10100_cmd_read_id, &val, sizeof(val));
0229 if (ret)
0230 return ret;
0231 id = ICP10100_ID_REG_GET(be16_to_cpu(val));
0232 if (id != ICP10100_ID_REG) {
0233 dev_err(&st->client->dev, "invalid id %#x\n", id);
0234 return -ENODEV;
0235 }
0236
0237
0238 ret = icp10100_read_cal_otp(st);
0239 if (ret)
0240 return ret;
0241
0242
0243 return icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
0244 }
0245
0246 static int icp10100_get_measures(struct icp10100_state *st,
0247 uint32_t *pressure, uint16_t *temperature)
0248 {
0249 const struct icp10100_command *cmd;
0250 __be16 measures[3];
0251 int ret;
0252
0253 ret = pm_runtime_resume_and_get(&st->client->dev);
0254 if (ret < 0)
0255 return ret;
0256
0257 mutex_lock(&st->lock);
0258 cmd = &icp10100_cmd_measure[st->mode];
0259 ret = icp10100_send_cmd(st, cmd, measures, sizeof(measures));
0260 mutex_unlock(&st->lock);
0261 if (ret)
0262 goto error_measure;
0263
0264 *pressure = (be16_to_cpu(measures[0]) << 8) |
0265 (be16_to_cpu(measures[1]) >> 8);
0266 *temperature = be16_to_cpu(measures[2]);
0267
0268 pm_runtime_mark_last_busy(&st->client->dev);
0269 error_measure:
0270 pm_runtime_put_autosuspend(&st->client->dev);
0271 return ret;
0272 }
0273
0274 static uint32_t icp10100_get_pressure(struct icp10100_state *st,
0275 uint32_t raw_pressure, uint16_t raw_temp)
0276 {
0277 static int32_t p_calib[] = {45000, 80000, 105000};
0278 static int32_t lut_lower = 3670016;
0279 static int32_t lut_upper = 12058624;
0280 static int32_t inv_quadr_factor = 16777216;
0281 static int32_t offset_factor = 2048;
0282 int64_t val1, val2;
0283 int32_t p_lut[3];
0284 int32_t t, t_square;
0285 int64_t a, b, c;
0286 uint32_t pressure_mPa;
0287
0288 dev_dbg(&st->client->dev, "raw: pressure = %u, temp = %u\n",
0289 raw_pressure, raw_temp);
0290
0291
0292 t = (int32_t)raw_temp - 32768;
0293 t_square = t * t;
0294 val1 = (int64_t)st->cal[0] * (int64_t)t_square;
0295 p_lut[0] = lut_lower + (int32_t)div_s64(val1, inv_quadr_factor);
0296 val1 = (int64_t)st->cal[1] * (int64_t)t_square;
0297 p_lut[1] = offset_factor * st->cal[3] +
0298 (int32_t)div_s64(val1, inv_quadr_factor);
0299 val1 = (int64_t)st->cal[2] * (int64_t)t_square;
0300 p_lut[2] = lut_upper + (int32_t)div_s64(val1, inv_quadr_factor);
0301 dev_dbg(&st->client->dev, "p_lut = [%d, %d, %d]\n",
0302 p_lut[0], p_lut[1], p_lut[2]);
0303
0304
0305 val1 = (int64_t)p_lut[0] * (int64_t)p_lut[1] *
0306 (int64_t)(p_calib[0] - p_calib[1]) +
0307 (int64_t)p_lut[1] * (int64_t)p_lut[2] *
0308 (int64_t)(p_calib[1] - p_calib[2]) +
0309 (int64_t)p_lut[2] * (int64_t)p_lut[0] *
0310 (int64_t)(p_calib[2] - p_calib[0]);
0311 val2 = (int64_t)p_lut[2] * (int64_t)(p_calib[0] - p_calib[1]) +
0312 (int64_t)p_lut[0] * (int64_t)(p_calib[1] - p_calib[2]) +
0313 (int64_t)p_lut[1] * (int64_t)(p_calib[2] - p_calib[0]);
0314 c = div64_s64(val1, val2);
0315 dev_dbg(&st->client->dev, "val1 = %lld, val2 = %lld, c = %lld\n",
0316 val1, val2, c);
0317 val1 = (int64_t)p_calib[0] * (int64_t)p_lut[0] -
0318 (int64_t)p_calib[1] * (int64_t)p_lut[1] -
0319 (int64_t)(p_calib[1] - p_calib[0]) * c;
0320 val2 = (int64_t)p_lut[0] - (int64_t)p_lut[1];
0321 a = div64_s64(val1, val2);
0322 dev_dbg(&st->client->dev, "val1 = %lld, val2 = %lld, a = %lld\n",
0323 val1, val2, a);
0324 b = ((int64_t)p_calib[0] - a) * ((int64_t)p_lut[0] + c);
0325 dev_dbg(&st->client->dev, "b = %lld\n", b);
0326
0327
0328
0329
0330
0331 pressure_mPa = 1000LL * a + div64_s64(1000LL * b, c + raw_pressure);
0332
0333 return pressure_mPa;
0334 }
0335
0336 static int icp10100_read_raw_measures(struct iio_dev *indio_dev,
0337 struct iio_chan_spec const *chan,
0338 int *val, int *val2)
0339 {
0340 struct icp10100_state *st = iio_priv(indio_dev);
0341 uint32_t raw_pressure;
0342 uint16_t raw_temp;
0343 uint32_t pressure_mPa;
0344 int ret;
0345
0346 ret = iio_device_claim_direct_mode(indio_dev);
0347 if (ret)
0348 return ret;
0349
0350 ret = icp10100_get_measures(st, &raw_pressure, &raw_temp);
0351 if (ret)
0352 goto error_release;
0353
0354 switch (chan->type) {
0355 case IIO_PRESSURE:
0356 pressure_mPa = icp10100_get_pressure(st, raw_pressure,
0357 raw_temp);
0358
0359 *val = pressure_mPa / 1000000;
0360 *val2 = pressure_mPa % 1000000;
0361 ret = IIO_VAL_INT_PLUS_MICRO;
0362 break;
0363 case IIO_TEMP:
0364 *val = raw_temp;
0365 ret = IIO_VAL_INT;
0366 break;
0367 default:
0368 ret = -EINVAL;
0369 break;
0370 }
0371
0372 error_release:
0373 iio_device_release_direct_mode(indio_dev);
0374 return ret;
0375 }
0376
0377 static int icp10100_read_raw(struct iio_dev *indio_dev,
0378 struct iio_chan_spec const *chan,
0379 int *val, int *val2, long mask)
0380 {
0381 struct icp10100_state *st = iio_priv(indio_dev);
0382
0383 switch (mask) {
0384 case IIO_CHAN_INFO_RAW:
0385 case IIO_CHAN_INFO_PROCESSED:
0386 return icp10100_read_raw_measures(indio_dev, chan, val, val2);
0387 case IIO_CHAN_INFO_SCALE:
0388 switch (chan->type) {
0389 case IIO_TEMP:
0390
0391 *val = 2;
0392 *val2 = 670288;
0393 return IIO_VAL_INT_PLUS_MICRO;
0394 default:
0395 return -EINVAL;
0396 }
0397 break;
0398 case IIO_CHAN_INFO_OFFSET:
0399 switch (chan->type) {
0400 case IIO_TEMP:
0401
0402 *val = -45000;
0403 return IIO_VAL_INT;
0404 default:
0405 return -EINVAL;
0406 }
0407 break;
0408 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
0409 mutex_lock(&st->lock);
0410 *val = 1 << st->mode;
0411 mutex_unlock(&st->lock);
0412 return IIO_VAL_INT;
0413 default:
0414 return -EINVAL;
0415 }
0416 }
0417
0418 static int icp10100_read_avail(struct iio_dev *indio_dev,
0419 struct iio_chan_spec const *chan,
0420 const int **vals, int *type, int *length,
0421 long mask)
0422 {
0423 static int oversamplings[] = {1, 2, 4, 8};
0424
0425 switch (mask) {
0426 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
0427 *vals = oversamplings;
0428 *type = IIO_VAL_INT;
0429 *length = ARRAY_SIZE(oversamplings);
0430 return IIO_AVAIL_LIST;
0431 default:
0432 return -EINVAL;
0433 }
0434 }
0435
0436 static int icp10100_write_raw(struct iio_dev *indio_dev,
0437 struct iio_chan_spec const *chan,
0438 int val, int val2, long mask)
0439 {
0440 struct icp10100_state *st = iio_priv(indio_dev);
0441 unsigned int mode;
0442 int ret;
0443
0444 switch (mask) {
0445 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
0446
0447 if (val <= 0 || !is_power_of_2(val))
0448 return -EINVAL;
0449 mode = ilog2(val);
0450 if (mode >= ICP10100_MODE_NB)
0451 return -EINVAL;
0452 ret = iio_device_claim_direct_mode(indio_dev);
0453 if (ret)
0454 return ret;
0455 mutex_lock(&st->lock);
0456 st->mode = mode;
0457 mutex_unlock(&st->lock);
0458 iio_device_release_direct_mode(indio_dev);
0459 return 0;
0460 default:
0461 return -EINVAL;
0462 }
0463 }
0464
0465 static int icp10100_write_raw_get_fmt(struct iio_dev *indio_dev,
0466 struct iio_chan_spec const *chan,
0467 long mask)
0468 {
0469 switch (mask) {
0470 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
0471 return IIO_VAL_INT;
0472 default:
0473 return -EINVAL;
0474 }
0475 }
0476
0477 static const struct iio_info icp10100_info = {
0478 .read_raw = icp10100_read_raw,
0479 .read_avail = icp10100_read_avail,
0480 .write_raw = icp10100_write_raw,
0481 .write_raw_get_fmt = icp10100_write_raw_get_fmt,
0482 };
0483
0484 static const struct iio_chan_spec icp10100_channels[] = {
0485 {
0486 .type = IIO_PRESSURE,
0487 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
0488 .info_mask_shared_by_all =
0489 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
0490 .info_mask_shared_by_all_available =
0491 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
0492 }, {
0493 .type = IIO_TEMP,
0494 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0495 BIT(IIO_CHAN_INFO_SCALE) |
0496 BIT(IIO_CHAN_INFO_OFFSET),
0497 .info_mask_shared_by_all =
0498 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
0499 .info_mask_shared_by_all_available =
0500 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
0501 },
0502 };
0503
0504 static int icp10100_enable_regulator(struct icp10100_state *st)
0505 {
0506 int ret;
0507
0508 ret = regulator_enable(st->vdd);
0509 if (ret)
0510 return ret;
0511 msleep(100);
0512
0513 return 0;
0514 }
0515
0516 static void icp10100_disable_regulator_action(void *data)
0517 {
0518 struct icp10100_state *st = data;
0519 int ret;
0520
0521 ret = regulator_disable(st->vdd);
0522 if (ret)
0523 dev_err(&st->client->dev, "error %d disabling vdd\n", ret);
0524 }
0525
0526 static void icp10100_pm_disable(void *data)
0527 {
0528 struct device *dev = data;
0529
0530 pm_runtime_disable(dev);
0531 }
0532
0533 static int icp10100_probe(struct i2c_client *client,
0534 const struct i2c_device_id *id)
0535 {
0536 struct iio_dev *indio_dev;
0537 struct icp10100_state *st;
0538 int ret;
0539
0540 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0541 dev_err(&client->dev, "plain i2c transactions not supported\n");
0542 return -ENODEV;
0543 }
0544
0545 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
0546 if (!indio_dev)
0547 return -ENOMEM;
0548
0549 i2c_set_clientdata(client, indio_dev);
0550 indio_dev->name = client->name;
0551 indio_dev->modes = INDIO_DIRECT_MODE;
0552 indio_dev->channels = icp10100_channels;
0553 indio_dev->num_channels = ARRAY_SIZE(icp10100_channels);
0554 indio_dev->info = &icp10100_info;
0555
0556 st = iio_priv(indio_dev);
0557 mutex_init(&st->lock);
0558 st->client = client;
0559 st->mode = ICP10100_MODE_N;
0560
0561 st->vdd = devm_regulator_get(&client->dev, "vdd");
0562 if (IS_ERR(st->vdd))
0563 return PTR_ERR(st->vdd);
0564
0565 ret = icp10100_enable_regulator(st);
0566 if (ret)
0567 return ret;
0568
0569 ret = devm_add_action_or_reset(&client->dev,
0570 icp10100_disable_regulator_action, st);
0571 if (ret)
0572 return ret;
0573
0574
0575 crc8_populate_msb(icp10100_crc8_table, ICP10100_CRC8_POLYNOMIAL);
0576
0577 ret = icp10100_init_chip(st);
0578 if (ret) {
0579 dev_err(&client->dev, "init chip error %d\n", ret);
0580 return ret;
0581 }
0582
0583
0584 pm_runtime_get_noresume(&client->dev);
0585 pm_runtime_set_active(&client->dev);
0586 pm_runtime_enable(&client->dev);
0587 pm_runtime_set_autosuspend_delay(&client->dev, 2000);
0588 pm_runtime_use_autosuspend(&client->dev);
0589 pm_runtime_put(&client->dev);
0590 ret = devm_add_action_or_reset(&client->dev, icp10100_pm_disable,
0591 &client->dev);
0592 if (ret)
0593 return ret;
0594
0595 return devm_iio_device_register(&client->dev, indio_dev);
0596 }
0597
0598 static int __maybe_unused icp10100_suspend(struct device *dev)
0599 {
0600 struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
0601 int ret;
0602
0603 mutex_lock(&st->lock);
0604 ret = regulator_disable(st->vdd);
0605 mutex_unlock(&st->lock);
0606
0607 return ret;
0608 }
0609
0610 static int __maybe_unused icp10100_resume(struct device *dev)
0611 {
0612 struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
0613 int ret;
0614
0615 mutex_lock(&st->lock);
0616
0617 ret = icp10100_enable_regulator(st);
0618 if (ret)
0619 goto out_unlock;
0620
0621
0622 ret = icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
0623
0624 out_unlock:
0625 mutex_unlock(&st->lock);
0626 return ret;
0627 }
0628
0629 static UNIVERSAL_DEV_PM_OPS(icp10100_pm, icp10100_suspend, icp10100_resume,
0630 NULL);
0631
0632 static const struct of_device_id icp10100_of_match[] = {
0633 {
0634 .compatible = "invensense,icp10100",
0635 },
0636 { }
0637 };
0638 MODULE_DEVICE_TABLE(of, icp10100_of_match);
0639
0640 static const struct i2c_device_id icp10100_id[] = {
0641 { "icp10100", 0 },
0642 { }
0643 };
0644 MODULE_DEVICE_TABLE(i2c, icp10100_id);
0645
0646 static struct i2c_driver icp10100_driver = {
0647 .driver = {
0648 .name = "icp10100",
0649 .pm = &icp10100_pm,
0650 .of_match_table = icp10100_of_match,
0651 },
0652 .probe = icp10100_probe,
0653 .id_table = icp10100_id,
0654 };
0655 module_i2c_driver(icp10100_driver);
0656
0657 MODULE_AUTHOR("InvenSense, Inc.");
0658 MODULE_DESCRIPTION("InvenSense icp10100 driver");
0659 MODULE_LICENSE("GPL");