1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2020 InvenSense, Inc.
5 * Driver for InvenSense ICP-1010xx barometric pressure and temperature sensor.
8 * http://www.invensense.com/wp-content/uploads/2018/01/DS-000186-ICP-101xx-v1.2.pdf
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/crc8.h>
16 #include <linux/mutex.h>
17 #include <linux/delay.h>
18 #include <linux/log2.h>
19 #include <linux/math64.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/iio/iio.h>
23 #define ICP10100_ID_REG_GET(_reg) ((_reg) & 0x003F)
24 #define ICP10100_ID_REG 0x08
25 #define ICP10100_RESPONSE_WORD_LENGTH 3
26 #define ICP10100_CRC8_WORD_LENGTH 2
27 #define ICP10100_CRC8_POLYNOMIAL 0x31
28 #define ICP10100_CRC8_INIT 0xFF
31 ICP10100_MODE_LP, /* Low power mode: 1x sampling */
32 ICP10100_MODE_N, /* Normal mode: 2x sampling */
33 ICP10100_MODE_LN, /* Low noise mode: 4x sampling */
34 ICP10100_MODE_ULN, /* Ultra low noise mode: 8x sampling */
38 struct icp10100_state {
40 struct i2c_client *client;
41 struct regulator *vdd;
42 enum icp10100_mode mode;
46 struct icp10100_command {
48 unsigned long wait_us;
49 unsigned long wait_max_us;
50 size_t response_word_nb;
53 static const struct icp10100_command icp10100_cmd_soft_reset = {
54 .cmd = cpu_to_be16(0x805D),
57 .response_word_nb = 0,
60 static const struct icp10100_command icp10100_cmd_read_id = {
61 .cmd = cpu_to_be16(0xEFC8),
63 .response_word_nb = 1,
66 static const struct icp10100_command icp10100_cmd_read_otp = {
67 .cmd = cpu_to_be16(0xC7F7),
69 .response_word_nb = 1,
72 static const struct icp10100_command icp10100_cmd_measure[] = {
73 [ICP10100_MODE_LP] = {
74 .cmd = cpu_to_be16(0x401A),
77 .response_word_nb = 3,
80 .cmd = cpu_to_be16(0x48A3),
83 .response_word_nb = 3,
85 [ICP10100_MODE_LN] = {
86 .cmd = cpu_to_be16(0x5059),
89 .response_word_nb = 3,
91 [ICP10100_MODE_ULN] = {
92 .cmd = cpu_to_be16(0x58E0),
95 .response_word_nb = 3,
99 static const uint8_t icp10100_switch_mode_otp[] =
100 {0xC5, 0x95, 0x00, 0x66, 0x9c};
102 DECLARE_CRC8_TABLE(icp10100_crc8_table);
104 static inline int icp10100_i2c_xfer(struct i2c_adapter *adap,
105 struct i2c_msg *msgs, int num)
109 ret = i2c_transfer(adap, msgs, num);
119 static int icp10100_send_cmd(struct icp10100_state *st,
120 const struct icp10100_command *cmd,
121 __be16 *buf, size_t buf_len)
123 size_t size = cmd->response_word_nb * ICP10100_RESPONSE_WORD_LENGTH;
126 uint8_t *buf_ptr = (uint8_t *)buf;
127 struct i2c_msg msgs[2] = {
129 .addr = st->client->addr,
132 .buf = (uint8_t *)&cmd->cmd,
134 .addr = st->client->addr,
144 if (size > sizeof(data))
147 if (cmd->response_word_nb > 0 &&
148 (buf == NULL || buf_len < (cmd->response_word_nb * 2)))
151 dev_dbg(&st->client->dev, "sending cmd %#x\n", be16_to_cpu(cmd->cmd));
153 if (cmd->response_word_nb > 0 && cmd->wait_us == 0) {
154 /* direct command-response without waiting */
155 ret = icp10100_i2c_xfer(st->client->adapter, msgs,
160 /* transfer command write */
161 ret = icp10100_i2c_xfer(st->client->adapter, &msgs[0], 1);
164 if (cmd->wait_us > 0)
165 usleep_range(cmd->wait_us, cmd->wait_max_us);
166 /* transfer response read if needed */
167 if (cmd->response_word_nb > 0) {
168 ret = icp10100_i2c_xfer(st->client->adapter, &msgs[1], 1);
176 /* process read words with crc checking */
177 for (i = 0; i < cmd->response_word_nb; ++i) {
178 ptr = &data[i * ICP10100_RESPONSE_WORD_LENGTH];
179 crc = crc8(icp10100_crc8_table, ptr, ICP10100_CRC8_WORD_LENGTH,
181 if (crc != ptr[ICP10100_CRC8_WORD_LENGTH]) {
182 dev_err(&st->client->dev, "crc error recv=%#x calc=%#x\n",
183 ptr[ICP10100_CRC8_WORD_LENGTH], crc);
193 static int icp10100_read_cal_otp(struct icp10100_state *st)
199 /* switch into OTP read mode */
200 ret = i2c_master_send(st->client, icp10100_switch_mode_otp,
201 ARRAY_SIZE(icp10100_switch_mode_otp));
204 if (ret != ARRAY_SIZE(icp10100_switch_mode_otp))
207 /* read 4 calibration values */
208 for (i = 0; i < 4; ++i) {
209 ret = icp10100_send_cmd(st, &icp10100_cmd_read_otp,
213 st->cal[i] = be16_to_cpu(val);
214 dev_dbg(&st->client->dev, "cal[%d] = %d\n", i, st->cal[i]);
220 static int icp10100_init_chip(struct icp10100_state *st)
226 /* read and check id */
227 ret = icp10100_send_cmd(st, &icp10100_cmd_read_id, &val, sizeof(val));
230 id = ICP10100_ID_REG_GET(be16_to_cpu(val));
231 if (id != ICP10100_ID_REG) {
232 dev_err(&st->client->dev, "invalid id %#x\n", id);
236 /* read calibration data from OTP */
237 ret = icp10100_read_cal_otp(st);
242 return icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
245 static int icp10100_get_measures(struct icp10100_state *st,
246 uint32_t *pressure, uint16_t *temperature)
248 const struct icp10100_command *cmd;
252 pm_runtime_get_sync(&st->client->dev);
254 mutex_lock(&st->lock);
255 cmd = &icp10100_cmd_measure[st->mode];
256 ret = icp10100_send_cmd(st, cmd, measures, sizeof(measures));
257 mutex_unlock(&st->lock);
261 *pressure = (be16_to_cpu(measures[0]) << 8) |
262 (be16_to_cpu(measures[1]) >> 8);
263 *temperature = be16_to_cpu(measures[2]);
265 pm_runtime_mark_last_busy(&st->client->dev);
267 pm_runtime_put_autosuspend(&st->client->dev);
271 static uint32_t icp10100_get_pressure(struct icp10100_state *st,
272 uint32_t raw_pressure, uint16_t raw_temp)
274 static int32_t p_calib[] = {45000, 80000, 105000};
275 static int32_t lut_lower = 3670016;
276 static int32_t lut_upper = 12058624;
277 static int32_t inv_quadr_factor = 16777216;
278 static int32_t offset_factor = 2048;
283 uint32_t pressure_mPa;
285 dev_dbg(&st->client->dev, "raw: pressure = %u, temp = %u\n",
286 raw_pressure, raw_temp);
288 /* compute p_lut values */
289 t = (int32_t)raw_temp - 32768;
291 val1 = (int64_t)st->cal[0] * (int64_t)t_square;
292 p_lut[0] = lut_lower + (int32_t)div_s64(val1, inv_quadr_factor);
293 val1 = (int64_t)st->cal[1] * (int64_t)t_square;
294 p_lut[1] = offset_factor * st->cal[3] +
295 (int32_t)div_s64(val1, inv_quadr_factor);
296 val1 = (int64_t)st->cal[2] * (int64_t)t_square;
297 p_lut[2] = lut_upper + (int32_t)div_s64(val1, inv_quadr_factor);
298 dev_dbg(&st->client->dev, "p_lut = [%d, %d, %d]\n",
299 p_lut[0], p_lut[1], p_lut[2]);
301 /* compute a, b, c factors */
302 val1 = (int64_t)p_lut[0] * (int64_t)p_lut[1] *
303 (int64_t)(p_calib[0] - p_calib[1]) +
304 (int64_t)p_lut[1] * (int64_t)p_lut[2] *
305 (int64_t)(p_calib[1] - p_calib[2]) +
306 (int64_t)p_lut[2] * (int64_t)p_lut[0] *
307 (int64_t)(p_calib[2] - p_calib[0]);
308 val2 = (int64_t)p_lut[2] * (int64_t)(p_calib[0] - p_calib[1]) +
309 (int64_t)p_lut[0] * (int64_t)(p_calib[1] - p_calib[2]) +
310 (int64_t)p_lut[1] * (int64_t)(p_calib[2] - p_calib[0]);
311 c = div64_s64(val1, val2);
312 dev_dbg(&st->client->dev, "val1 = %lld, val2 = %lld, c = %lld\n",
314 val1 = (int64_t)p_calib[0] * (int64_t)p_lut[0] -
315 (int64_t)p_calib[1] * (int64_t)p_lut[1] -
316 (int64_t)(p_calib[1] - p_calib[0]) * c;
317 val2 = (int64_t)p_lut[0] - (int64_t)p_lut[1];
318 a = div64_s64(val1, val2);
319 dev_dbg(&st->client->dev, "val1 = %lld, val2 = %lld, a = %lld\n",
321 b = ((int64_t)p_calib[0] - a) * ((int64_t)p_lut[0] + c);
322 dev_dbg(&st->client->dev, "b = %lld\n", b);
325 * pressure_Pa = a + (b / (c + raw_pressure))
326 * pressure_mPa = 1000 * pressure_Pa
328 pressure_mPa = 1000LL * a + div64_s64(1000LL * b, c + raw_pressure);
333 static int icp10100_read_raw_measures(struct iio_dev *indio_dev,
334 struct iio_chan_spec const *chan,
337 struct icp10100_state *st = iio_priv(indio_dev);
338 uint32_t raw_pressure;
340 uint32_t pressure_mPa;
343 ret = iio_device_claim_direct_mode(indio_dev);
347 ret = icp10100_get_measures(st, &raw_pressure, &raw_temp);
351 switch (chan->type) {
353 pressure_mPa = icp10100_get_pressure(st, raw_pressure,
356 *val = pressure_mPa / 1000000;
357 *val2 = pressure_mPa % 1000000;
358 ret = IIO_VAL_INT_PLUS_MICRO;
370 iio_device_release_direct_mode(indio_dev);
374 static int icp10100_read_raw(struct iio_dev *indio_dev,
375 struct iio_chan_spec const *chan,
376 int *val, int *val2, long mask)
378 struct icp10100_state *st = iio_priv(indio_dev);
381 case IIO_CHAN_INFO_RAW:
382 case IIO_CHAN_INFO_PROCESSED:
383 return icp10100_read_raw_measures(indio_dev, chan, val, val2);
384 case IIO_CHAN_INFO_SCALE:
385 switch (chan->type) {
387 /* 1000 * 175°C / 65536 in m°C */
390 return IIO_VAL_INT_PLUS_MICRO;
395 case IIO_CHAN_INFO_OFFSET:
396 switch (chan->type) {
398 /* 1000 * -45°C in m°C */
405 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
406 mutex_lock(&st->lock);
407 *val = 1 << st->mode;
408 mutex_unlock(&st->lock);
415 static int icp10100_read_avail(struct iio_dev *indio_dev,
416 struct iio_chan_spec const *chan,
417 const int **vals, int *type, int *length,
420 static int oversamplings[] = {1, 2, 4, 8};
423 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
424 *vals = oversamplings;
426 *length = ARRAY_SIZE(oversamplings);
427 return IIO_AVAIL_LIST;
433 static int icp10100_write_raw(struct iio_dev *indio_dev,
434 struct iio_chan_spec const *chan,
435 int val, int val2, long mask)
437 struct icp10100_state *st = iio_priv(indio_dev);
442 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
443 /* oversampling is always positive and a power of 2 */
444 if (val <= 0 || !is_power_of_2(val))
447 if (mode >= ICP10100_MODE_NB)
449 ret = iio_device_claim_direct_mode(indio_dev);
452 mutex_lock(&st->lock);
454 mutex_unlock(&st->lock);
455 iio_device_release_direct_mode(indio_dev);
462 static int icp10100_write_raw_get_fmt(struct iio_dev *indio_dev,
463 struct iio_chan_spec const *chan,
467 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
474 static const struct iio_info icp10100_info = {
475 .read_raw = icp10100_read_raw,
476 .read_avail = icp10100_read_avail,
477 .write_raw = icp10100_write_raw,
478 .write_raw_get_fmt = icp10100_write_raw_get_fmt,
481 static const struct iio_chan_spec icp10100_channels[] = {
483 .type = IIO_PRESSURE,
484 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
485 .info_mask_shared_by_all =
486 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
487 .info_mask_shared_by_all_available =
488 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
491 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
492 BIT(IIO_CHAN_INFO_SCALE) |
493 BIT(IIO_CHAN_INFO_OFFSET),
494 .info_mask_shared_by_all =
495 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
496 .info_mask_shared_by_all_available =
497 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
501 static int icp10100_enable_regulator(struct icp10100_state *st)
505 ret = regulator_enable(st->vdd);
513 static void icp10100_disable_regulator_action(void *data)
515 struct icp10100_state *st = data;
518 ret = regulator_disable(st->vdd);
520 dev_err(&st->client->dev, "error %d disabling vdd\n", ret);
523 static void icp10100_pm_disable(void *data)
525 struct device *dev = data;
527 pm_runtime_put_sync_suspend(dev);
528 pm_runtime_disable(dev);
531 static int icp10100_probe(struct i2c_client *client,
532 const struct i2c_device_id *id)
534 struct iio_dev *indio_dev;
535 struct icp10100_state *st;
538 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
539 dev_err(&client->dev, "plain i2c transactions not supported\n");
543 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
547 i2c_set_clientdata(client, indio_dev);
548 indio_dev->name = client->name;
549 indio_dev->modes = INDIO_DIRECT_MODE;
550 indio_dev->channels = icp10100_channels;
551 indio_dev->num_channels = ARRAY_SIZE(icp10100_channels);
552 indio_dev->info = &icp10100_info;
554 st = iio_priv(indio_dev);
555 mutex_init(&st->lock);
557 st->mode = ICP10100_MODE_N;
559 st->vdd = devm_regulator_get(&client->dev, "vdd");
561 return PTR_ERR(st->vdd);
563 ret = icp10100_enable_regulator(st);
567 ret = devm_add_action_or_reset(&client->dev,
568 icp10100_disable_regulator_action, st);
572 /* has to be done before the first i2c communication */
573 crc8_populate_msb(icp10100_crc8_table, ICP10100_CRC8_POLYNOMIAL);
575 ret = icp10100_init_chip(st);
577 dev_err(&client->dev, "init chip error %d\n", ret);
581 /* enable runtime pm with autosuspend delay of 2s */
582 pm_runtime_get_noresume(&client->dev);
583 pm_runtime_set_active(&client->dev);
584 pm_runtime_enable(&client->dev);
585 pm_runtime_set_autosuspend_delay(&client->dev, 2000);
586 pm_runtime_use_autosuspend(&client->dev);
587 pm_runtime_put(&client->dev);
588 ret = devm_add_action_or_reset(&client->dev, icp10100_pm_disable,
593 return devm_iio_device_register(&client->dev, indio_dev);
596 static int __maybe_unused icp10100_suspend(struct device *dev)
598 struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
601 mutex_lock(&st->lock);
602 ret = regulator_disable(st->vdd);
603 mutex_unlock(&st->lock);
608 static int __maybe_unused icp10100_resume(struct device *dev)
610 struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
613 mutex_lock(&st->lock);
615 ret = icp10100_enable_regulator(st);
620 ret = icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
623 mutex_unlock(&st->lock);
627 static UNIVERSAL_DEV_PM_OPS(icp10100_pm, icp10100_suspend, icp10100_resume,
630 static const struct of_device_id icp10100_of_match[] = {
632 .compatible = "invensense,icp10100",
636 MODULE_DEVICE_TABLE(of, icp10100_of_match);
638 static const struct i2c_device_id icp10100_id[] = {
642 MODULE_DEVICE_TABLE(i2c, icp10100_id);
644 static struct i2c_driver icp10100_driver = {
648 .of_match_table = of_match_ptr(icp10100_of_match),
650 .probe = icp10100_probe,
651 .id_table = icp10100_id,
653 module_i2c_driver(icp10100_driver);
655 MODULE_AUTHOR("InvenSense, Inc.");
656 MODULE_DESCRIPTION("InvenSense icp10100 driver");
657 MODULE_LICENSE("GPL");