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/mod_devicetable.h>
14 #include <linux/i2c.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/crc8.h>
17 #include <linux/mutex.h>
18 #include <linux/delay.h>
19 #include <linux/log2.h>
20 #include <linux/math64.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/iio/iio.h>
24 #define ICP10100_ID_REG_GET(_reg) ((_reg) & 0x003F)
25 #define ICP10100_ID_REG 0x08
26 #define ICP10100_RESPONSE_WORD_LENGTH 3
27 #define ICP10100_CRC8_WORD_LENGTH 2
28 #define ICP10100_CRC8_POLYNOMIAL 0x31
29 #define ICP10100_CRC8_INIT 0xFF
32 ICP10100_MODE_LP, /* Low power mode: 1x sampling */
33 ICP10100_MODE_N, /* Normal mode: 2x sampling */
34 ICP10100_MODE_LN, /* Low noise mode: 4x sampling */
35 ICP10100_MODE_ULN, /* Ultra low noise mode: 8x sampling */
39 struct icp10100_state {
41 struct i2c_client *client;
42 struct regulator *vdd;
43 enum icp10100_mode mode;
47 struct icp10100_command {
49 unsigned long wait_us;
50 unsigned long wait_max_us;
51 size_t response_word_nb;
54 static const struct icp10100_command icp10100_cmd_soft_reset = {
55 .cmd = cpu_to_be16(0x805D),
58 .response_word_nb = 0,
61 static const struct icp10100_command icp10100_cmd_read_id = {
62 .cmd = cpu_to_be16(0xEFC8),
64 .response_word_nb = 1,
67 static const struct icp10100_command icp10100_cmd_read_otp = {
68 .cmd = cpu_to_be16(0xC7F7),
70 .response_word_nb = 1,
73 static const struct icp10100_command icp10100_cmd_measure[] = {
74 [ICP10100_MODE_LP] = {
75 .cmd = cpu_to_be16(0x401A),
78 .response_word_nb = 3,
81 .cmd = cpu_to_be16(0x48A3),
84 .response_word_nb = 3,
86 [ICP10100_MODE_LN] = {
87 .cmd = cpu_to_be16(0x5059),
90 .response_word_nb = 3,
92 [ICP10100_MODE_ULN] = {
93 .cmd = cpu_to_be16(0x58E0),
96 .response_word_nb = 3,
100 static const uint8_t icp10100_switch_mode_otp[] =
101 {0xC5, 0x95, 0x00, 0x66, 0x9c};
103 DECLARE_CRC8_TABLE(icp10100_crc8_table);
105 static inline int icp10100_i2c_xfer(struct i2c_adapter *adap,
106 struct i2c_msg *msgs, int num)
110 ret = i2c_transfer(adap, msgs, num);
120 static int icp10100_send_cmd(struct icp10100_state *st,
121 const struct icp10100_command *cmd,
122 __be16 *buf, size_t buf_len)
124 size_t size = cmd->response_word_nb * ICP10100_RESPONSE_WORD_LENGTH;
127 uint8_t *buf_ptr = (uint8_t *)buf;
128 struct i2c_msg msgs[2] = {
130 .addr = st->client->addr,
133 .buf = (uint8_t *)&cmd->cmd,
135 .addr = st->client->addr,
145 if (size > sizeof(data))
148 if (cmd->response_word_nb > 0 &&
149 (buf == NULL || buf_len < (cmd->response_word_nb * 2)))
152 dev_dbg(&st->client->dev, "sending cmd %#x\n", be16_to_cpu(cmd->cmd));
154 if (cmd->response_word_nb > 0 && cmd->wait_us == 0) {
155 /* direct command-response without waiting */
156 ret = icp10100_i2c_xfer(st->client->adapter, msgs,
161 /* transfer command write */
162 ret = icp10100_i2c_xfer(st->client->adapter, &msgs[0], 1);
165 if (cmd->wait_us > 0)
166 usleep_range(cmd->wait_us, cmd->wait_max_us);
167 /* transfer response read if needed */
168 if (cmd->response_word_nb > 0) {
169 ret = icp10100_i2c_xfer(st->client->adapter, &msgs[1], 1);
177 /* process read words with crc checking */
178 for (i = 0; i < cmd->response_word_nb; ++i) {
179 ptr = &data[i * ICP10100_RESPONSE_WORD_LENGTH];
180 crc = crc8(icp10100_crc8_table, ptr, ICP10100_CRC8_WORD_LENGTH,
182 if (crc != ptr[ICP10100_CRC8_WORD_LENGTH]) {
183 dev_err(&st->client->dev, "crc error recv=%#x calc=%#x\n",
184 ptr[ICP10100_CRC8_WORD_LENGTH], crc);
194 static int icp10100_read_cal_otp(struct icp10100_state *st)
200 /* switch into OTP read mode */
201 ret = i2c_master_send(st->client, icp10100_switch_mode_otp,
202 ARRAY_SIZE(icp10100_switch_mode_otp));
205 if (ret != ARRAY_SIZE(icp10100_switch_mode_otp))
208 /* read 4 calibration values */
209 for (i = 0; i < 4; ++i) {
210 ret = icp10100_send_cmd(st, &icp10100_cmd_read_otp,
214 st->cal[i] = be16_to_cpu(val);
215 dev_dbg(&st->client->dev, "cal[%d] = %d\n", i, st->cal[i]);
221 static int icp10100_init_chip(struct icp10100_state *st)
227 /* read and check id */
228 ret = icp10100_send_cmd(st, &icp10100_cmd_read_id, &val, sizeof(val));
231 id = ICP10100_ID_REG_GET(be16_to_cpu(val));
232 if (id != ICP10100_ID_REG) {
233 dev_err(&st->client->dev, "invalid id %#x\n", id);
237 /* read calibration data from OTP */
238 ret = icp10100_read_cal_otp(st);
243 return icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
246 static int icp10100_get_measures(struct icp10100_state *st,
247 uint32_t *pressure, uint16_t *temperature)
249 const struct icp10100_command *cmd;
253 pm_runtime_get_sync(&st->client->dev);
255 mutex_lock(&st->lock);
256 cmd = &icp10100_cmd_measure[st->mode];
257 ret = icp10100_send_cmd(st, cmd, measures, sizeof(measures));
258 mutex_unlock(&st->lock);
262 *pressure = (be16_to_cpu(measures[0]) << 8) |
263 (be16_to_cpu(measures[1]) >> 8);
264 *temperature = be16_to_cpu(measures[2]);
266 pm_runtime_mark_last_busy(&st->client->dev);
268 pm_runtime_put_autosuspend(&st->client->dev);
272 static uint32_t icp10100_get_pressure(struct icp10100_state *st,
273 uint32_t raw_pressure, uint16_t raw_temp)
275 static int32_t p_calib[] = {45000, 80000, 105000};
276 static int32_t lut_lower = 3670016;
277 static int32_t lut_upper = 12058624;
278 static int32_t inv_quadr_factor = 16777216;
279 static int32_t offset_factor = 2048;
284 uint32_t pressure_mPa;
286 dev_dbg(&st->client->dev, "raw: pressure = %u, temp = %u\n",
287 raw_pressure, raw_temp);
289 /* compute p_lut values */
290 t = (int32_t)raw_temp - 32768;
292 val1 = (int64_t)st->cal[0] * (int64_t)t_square;
293 p_lut[0] = lut_lower + (int32_t)div_s64(val1, inv_quadr_factor);
294 val1 = (int64_t)st->cal[1] * (int64_t)t_square;
295 p_lut[1] = offset_factor * st->cal[3] +
296 (int32_t)div_s64(val1, inv_quadr_factor);
297 val1 = (int64_t)st->cal[2] * (int64_t)t_square;
298 p_lut[2] = lut_upper + (int32_t)div_s64(val1, inv_quadr_factor);
299 dev_dbg(&st->client->dev, "p_lut = [%d, %d, %d]\n",
300 p_lut[0], p_lut[1], p_lut[2]);
302 /* compute a, b, c factors */
303 val1 = (int64_t)p_lut[0] * (int64_t)p_lut[1] *
304 (int64_t)(p_calib[0] - p_calib[1]) +
305 (int64_t)p_lut[1] * (int64_t)p_lut[2] *
306 (int64_t)(p_calib[1] - p_calib[2]) +
307 (int64_t)p_lut[2] * (int64_t)p_lut[0] *
308 (int64_t)(p_calib[2] - p_calib[0]);
309 val2 = (int64_t)p_lut[2] * (int64_t)(p_calib[0] - p_calib[1]) +
310 (int64_t)p_lut[0] * (int64_t)(p_calib[1] - p_calib[2]) +
311 (int64_t)p_lut[1] * (int64_t)(p_calib[2] - p_calib[0]);
312 c = div64_s64(val1, val2);
313 dev_dbg(&st->client->dev, "val1 = %lld, val2 = %lld, c = %lld\n",
315 val1 = (int64_t)p_calib[0] * (int64_t)p_lut[0] -
316 (int64_t)p_calib[1] * (int64_t)p_lut[1] -
317 (int64_t)(p_calib[1] - p_calib[0]) * c;
318 val2 = (int64_t)p_lut[0] - (int64_t)p_lut[1];
319 a = div64_s64(val1, val2);
320 dev_dbg(&st->client->dev, "val1 = %lld, val2 = %lld, a = %lld\n",
322 b = ((int64_t)p_calib[0] - a) * ((int64_t)p_lut[0] + c);
323 dev_dbg(&st->client->dev, "b = %lld\n", b);
326 * pressure_Pa = a + (b / (c + raw_pressure))
327 * pressure_mPa = 1000 * pressure_Pa
329 pressure_mPa = 1000LL * a + div64_s64(1000LL * b, c + raw_pressure);
334 static int icp10100_read_raw_measures(struct iio_dev *indio_dev,
335 struct iio_chan_spec const *chan,
338 struct icp10100_state *st = iio_priv(indio_dev);
339 uint32_t raw_pressure;
341 uint32_t pressure_mPa;
344 ret = iio_device_claim_direct_mode(indio_dev);
348 ret = icp10100_get_measures(st, &raw_pressure, &raw_temp);
352 switch (chan->type) {
354 pressure_mPa = icp10100_get_pressure(st, raw_pressure,
357 *val = pressure_mPa / 1000000;
358 *val2 = pressure_mPa % 1000000;
359 ret = IIO_VAL_INT_PLUS_MICRO;
371 iio_device_release_direct_mode(indio_dev);
375 static int icp10100_read_raw(struct iio_dev *indio_dev,
376 struct iio_chan_spec const *chan,
377 int *val, int *val2, long mask)
379 struct icp10100_state *st = iio_priv(indio_dev);
382 case IIO_CHAN_INFO_RAW:
383 case IIO_CHAN_INFO_PROCESSED:
384 return icp10100_read_raw_measures(indio_dev, chan, val, val2);
385 case IIO_CHAN_INFO_SCALE:
386 switch (chan->type) {
388 /* 1000 * 175°C / 65536 in m°C */
391 return IIO_VAL_INT_PLUS_MICRO;
396 case IIO_CHAN_INFO_OFFSET:
397 switch (chan->type) {
399 /* 1000 * -45°C in m°C */
406 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
407 mutex_lock(&st->lock);
408 *val = 1 << st->mode;
409 mutex_unlock(&st->lock);
416 static int icp10100_read_avail(struct iio_dev *indio_dev,
417 struct iio_chan_spec const *chan,
418 const int **vals, int *type, int *length,
421 static int oversamplings[] = {1, 2, 4, 8};
424 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
425 *vals = oversamplings;
427 *length = ARRAY_SIZE(oversamplings);
428 return IIO_AVAIL_LIST;
434 static int icp10100_write_raw(struct iio_dev *indio_dev,
435 struct iio_chan_spec const *chan,
436 int val, int val2, long mask)
438 struct icp10100_state *st = iio_priv(indio_dev);
443 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
444 /* oversampling is always positive and a power of 2 */
445 if (val <= 0 || !is_power_of_2(val))
448 if (mode >= ICP10100_MODE_NB)
450 ret = iio_device_claim_direct_mode(indio_dev);
453 mutex_lock(&st->lock);
455 mutex_unlock(&st->lock);
456 iio_device_release_direct_mode(indio_dev);
463 static int icp10100_write_raw_get_fmt(struct iio_dev *indio_dev,
464 struct iio_chan_spec const *chan,
468 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
475 static const struct iio_info icp10100_info = {
476 .read_raw = icp10100_read_raw,
477 .read_avail = icp10100_read_avail,
478 .write_raw = icp10100_write_raw,
479 .write_raw_get_fmt = icp10100_write_raw_get_fmt,
482 static const struct iio_chan_spec icp10100_channels[] = {
484 .type = IIO_PRESSURE,
485 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
486 .info_mask_shared_by_all =
487 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
488 .info_mask_shared_by_all_available =
489 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
492 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
493 BIT(IIO_CHAN_INFO_SCALE) |
494 BIT(IIO_CHAN_INFO_OFFSET),
495 .info_mask_shared_by_all =
496 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
497 .info_mask_shared_by_all_available =
498 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
502 static int icp10100_enable_regulator(struct icp10100_state *st)
506 ret = regulator_enable(st->vdd);
514 static void icp10100_disable_regulator_action(void *data)
516 struct icp10100_state *st = data;
519 ret = regulator_disable(st->vdd);
521 dev_err(&st->client->dev, "error %d disabling vdd\n", ret);
524 static void icp10100_pm_disable(void *data)
526 struct device *dev = data;
528 pm_runtime_put_sync_suspend(dev);
529 pm_runtime_disable(dev);
532 static int icp10100_probe(struct i2c_client *client,
533 const struct i2c_device_id *id)
535 struct iio_dev *indio_dev;
536 struct icp10100_state *st;
539 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
540 dev_err(&client->dev, "plain i2c transactions not supported\n");
544 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
548 i2c_set_clientdata(client, indio_dev);
549 indio_dev->name = client->name;
550 indio_dev->modes = INDIO_DIRECT_MODE;
551 indio_dev->channels = icp10100_channels;
552 indio_dev->num_channels = ARRAY_SIZE(icp10100_channels);
553 indio_dev->info = &icp10100_info;
555 st = iio_priv(indio_dev);
556 mutex_init(&st->lock);
558 st->mode = ICP10100_MODE_N;
560 st->vdd = devm_regulator_get(&client->dev, "vdd");
562 return PTR_ERR(st->vdd);
564 ret = icp10100_enable_regulator(st);
568 ret = devm_add_action_or_reset(&client->dev,
569 icp10100_disable_regulator_action, st);
573 /* has to be done before the first i2c communication */
574 crc8_populate_msb(icp10100_crc8_table, ICP10100_CRC8_POLYNOMIAL);
576 ret = icp10100_init_chip(st);
578 dev_err(&client->dev, "init chip error %d\n", ret);
582 /* enable runtime pm with autosuspend delay of 2s */
583 pm_runtime_get_noresume(&client->dev);
584 pm_runtime_set_active(&client->dev);
585 pm_runtime_enable(&client->dev);
586 pm_runtime_set_autosuspend_delay(&client->dev, 2000);
587 pm_runtime_use_autosuspend(&client->dev);
588 pm_runtime_put(&client->dev);
589 ret = devm_add_action_or_reset(&client->dev, icp10100_pm_disable,
594 return devm_iio_device_register(&client->dev, indio_dev);
597 static int __maybe_unused icp10100_suspend(struct device *dev)
599 struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
602 mutex_lock(&st->lock);
603 ret = regulator_disable(st->vdd);
604 mutex_unlock(&st->lock);
609 static int __maybe_unused icp10100_resume(struct device *dev)
611 struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
614 mutex_lock(&st->lock);
616 ret = icp10100_enable_regulator(st);
621 ret = icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
624 mutex_unlock(&st->lock);
628 static UNIVERSAL_DEV_PM_OPS(icp10100_pm, icp10100_suspend, icp10100_resume,
631 static const struct of_device_id icp10100_of_match[] = {
633 .compatible = "invensense,icp10100",
637 MODULE_DEVICE_TABLE(of, icp10100_of_match);
639 static const struct i2c_device_id icp10100_id[] = {
643 MODULE_DEVICE_TABLE(i2c, icp10100_id);
645 static struct i2c_driver icp10100_driver = {
649 .of_match_table = icp10100_of_match,
651 .probe = icp10100_probe,
652 .id_table = icp10100_id,
654 module_i2c_driver(icp10100_driver);
656 MODULE_AUTHOR("InvenSense, Inc.");
657 MODULE_DESCRIPTION("InvenSense icp10100 driver");
658 MODULE_LICENSE("GPL");