File Index
Files and directories
Sign in
FILE

icp10100.c

File preview with syntax highlighting.

linux-6.19.14/drivers/iio/pressure/icp10100.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2020 InvenSense, Inc.
  4 *
  5 * Driver for InvenSense ICP-1010xx barometric pressure and temperature sensor.
  6 *
  7 * Datasheet:
  8 * http://www.invensense.com/wp-content/uploads/2018/01/DS-000186-ICP-101xx-v1.2.pdf
  9 */
 10
 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>
 23
 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
 30
 31enum icp10100_mode {
 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 */
 36	ICP10100_MODE_NB,
 37};
 38
 39struct icp10100_state {
 40	struct mutex lock;
 41	struct i2c_client *client;
 42	struct regulator *vdd;
 43	enum icp10100_mode mode;
 44	int16_t cal[4];
 45};
 46
 47struct icp10100_command {
 48	__be16 cmd;
 49	unsigned long wait_us;
 50	unsigned long wait_max_us;
 51	size_t response_word_nb;
 52};
 53
 54static const struct icp10100_command icp10100_cmd_soft_reset = {
 55	.cmd = cpu_to_be16(0x805D),
 56	.wait_us = 170,
 57	.wait_max_us = 200,
 58	.response_word_nb = 0,
 59};
 60
 61static const struct icp10100_command icp10100_cmd_read_id = {
 62	.cmd = cpu_to_be16(0xEFC8),
 63	.wait_us = 0,
 64	.response_word_nb = 1,
 65};
 66
 67static const struct icp10100_command icp10100_cmd_read_otp = {
 68	.cmd = cpu_to_be16(0xC7F7),
 69	.wait_us = 0,
 70	.response_word_nb = 1,
 71};
 72
 73static const struct icp10100_command icp10100_cmd_measure[] = {
 74	[ICP10100_MODE_LP] = {
 75		.cmd = cpu_to_be16(0x401A),
 76		.wait_us = 1800,
 77		.wait_max_us = 2000,
 78		.response_word_nb = 3,
 79	},
 80	[ICP10100_MODE_N] = {
 81		.cmd = cpu_to_be16(0x48A3),
 82		.wait_us = 6300,
 83		.wait_max_us = 6500,
 84		.response_word_nb = 3,
 85	},
 86	[ICP10100_MODE_LN] = {
 87		.cmd = cpu_to_be16(0x5059),
 88		.wait_us = 23800,
 89		.wait_max_us = 24000,
 90		.response_word_nb = 3,
 91	},
 92	[ICP10100_MODE_ULN] = {
 93		.cmd = cpu_to_be16(0x58E0),
 94		.wait_us = 94500,
 95		.wait_max_us = 94700,
 96		.response_word_nb = 3,
 97	},
 98};
 99
100static const uint8_t icp10100_switch_mode_otp[] =
101	{0xC5, 0x95, 0x00, 0x66, 0x9c};
102
103DECLARE_CRC8_TABLE(icp10100_crc8_table);
104
105static inline int icp10100_i2c_xfer(struct i2c_adapter *adap,
106				    struct i2c_msg *msgs, int num)
107{
108	int ret;
109
110	ret = i2c_transfer(adap, msgs, num);
111	if (ret < 0)
112		return ret;
113
114	if (ret != num)
115		return -EIO;
116
117	return 0;
118}
119
120static int icp10100_send_cmd(struct icp10100_state *st,
121			     const struct icp10100_command *cmd,
122			     __be16 *buf, size_t buf_len)
123{
124	size_t size = cmd->response_word_nb * ICP10100_RESPONSE_WORD_LENGTH;
125	uint8_t data[16];
126	uint8_t *ptr;
127	uint8_t *buf_ptr = (uint8_t *)buf;
128	struct i2c_msg msgs[2] = {
129		{
130			.addr = st->client->addr,
131			.flags = 0,
132			.len = 2,
133			.buf = (uint8_t *)&cmd->cmd,
134		}, {
135			.addr = st->client->addr,
136			.flags = I2C_M_RD,
137			.len = size,
138			.buf = data,
139		},
140	};
141	uint8_t crc;
142	unsigned int i;
143	int ret;
144
145	if (size > sizeof(data))
146		return -EINVAL;
147
148	if (cmd->response_word_nb > 0 &&
149			(buf == NULL || buf_len < (cmd->response_word_nb * 2)))
150		return -EINVAL;
151
152	dev_dbg(&st->client->dev, "sending cmd %#x\n", be16_to_cpu(cmd->cmd));
153
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,
157					ARRAY_SIZE(msgs));
158		if (ret)
159			return ret;
160	} else {
161		/* transfer command write */
162		ret = icp10100_i2c_xfer(st->client->adapter, &msgs[0], 1);
163		if (ret)
164			return ret;
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);
170			if (ret)
171				return ret;
172		} else {
173			return 0;
174		}
175	}
176
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,
181			   ICP10100_CRC8_INIT);
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);
185			return -EIO;
186		}
187		*buf_ptr++ = ptr[0];
188		*buf_ptr++ = ptr[1];
189	}
190
191	return 0;
192}
193
194static int icp10100_read_cal_otp(struct icp10100_state *st)
195{
196	__be16 val;
197	int i;
198	int ret;
199
200	/* switch into OTP read mode */
201	ret = i2c_master_send(st->client, icp10100_switch_mode_otp,
202			      ARRAY_SIZE(icp10100_switch_mode_otp));
203	if (ret < 0)
204		return ret;
205	if (ret != ARRAY_SIZE(icp10100_switch_mode_otp))
206		return -EIO;
207
208	/* read 4 calibration values */
209	for (i = 0; i < 4; ++i) {
210		ret = icp10100_send_cmd(st, &icp10100_cmd_read_otp,
211					&val, sizeof(val));
212		if (ret)
213			return ret;
214		st->cal[i] = be16_to_cpu(val);
215		dev_dbg(&st->client->dev, "cal[%d] = %d\n", i, st->cal[i]);
216	}
217
218	return 0;
219}
220
221static int icp10100_init_chip(struct icp10100_state *st)
222{
223	__be16 val;
224	uint16_t id;
225	int ret;
226
227	/* read and check id */
228	ret = icp10100_send_cmd(st, &icp10100_cmd_read_id, &val, sizeof(val));
229	if (ret)
230		return ret;
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);
234		return -ENODEV;
235	}
236
237	/* read calibration data from OTP */
238	ret = icp10100_read_cal_otp(st);
239	if (ret)
240		return ret;
241
242	/* reset chip */
243	return icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
244}
245
246static int icp10100_get_measures(struct icp10100_state *st,
247				uint32_t *pressure, uint16_t *temperature)
248{
249	const struct icp10100_command *cmd;
250	__be16 measures[3];
251	int ret;
252
253	ret = pm_runtime_resume_and_get(&st->client->dev);
254	if (ret < 0)
255		return ret;
256
257	mutex_lock(&st->lock);
258	cmd = &icp10100_cmd_measure[st->mode];
259	ret = icp10100_send_cmd(st, cmd, measures, sizeof(measures));
260	mutex_unlock(&st->lock);
261	if (ret)
262		goto error_measure;
263
264	*pressure = (be16_to_cpu(measures[0]) << 8) |
265			(be16_to_cpu(measures[1]) >> 8);
266	*temperature = be16_to_cpu(measures[2]);
267
268error_measure:
269	pm_runtime_put_autosuspend(&st->client->dev);
270	return ret;
271}
272
273static uint32_t icp10100_get_pressure(struct icp10100_state *st,
274				      uint32_t raw_pressure, uint16_t raw_temp)
275{
276	static int32_t p_calib[] = {45000, 80000, 105000};
277	static int32_t lut_lower = 3670016;
278	static int32_t lut_upper = 12058624;
279	static int32_t inv_quadr_factor = 16777216;
280	static int32_t offset_factor = 2048;
281	int64_t val1, val2;
282	int32_t p_lut[3];
283	int32_t t, t_square;
284	int64_t a, b, c;
285	uint32_t pressure_mPa;
286
287	dev_dbg(&st->client->dev, "raw: pressure = %u, temp = %u\n",
288		raw_pressure, raw_temp);
289
290	/* compute p_lut values */
291	t = (int32_t)raw_temp - 32768;
292	t_square = t * t;
293	val1 = (int64_t)st->cal[0] * (int64_t)t_square;
294	p_lut[0] = lut_lower + (int32_t)div_s64(val1, inv_quadr_factor);
295	val1 = (int64_t)st->cal[1] * (int64_t)t_square;
296	p_lut[1] = offset_factor * st->cal[3] +
297			(int32_t)div_s64(val1, inv_quadr_factor);
298	val1 = (int64_t)st->cal[2] * (int64_t)t_square;
299	p_lut[2] = lut_upper + (int32_t)div_s64(val1, inv_quadr_factor);
300	dev_dbg(&st->client->dev, "p_lut = [%d, %d, %d]\n",
301		p_lut[0], p_lut[1], p_lut[2]);
302
303	/* compute a, b, c factors */
304	val1 = (int64_t)p_lut[0] * (int64_t)p_lut[1] *
305			(int64_t)(p_calib[0] - p_calib[1]) +
306		(int64_t)p_lut[1] * (int64_t)p_lut[2] *
307			(int64_t)(p_calib[1] - p_calib[2]) +
308		(int64_t)p_lut[2] * (int64_t)p_lut[0] *
309			(int64_t)(p_calib[2] - p_calib[0]);
310	val2 = (int64_t)p_lut[2] * (int64_t)(p_calib[0] - p_calib[1]) +
311		(int64_t)p_lut[0] * (int64_t)(p_calib[1] - p_calib[2]) +
312		(int64_t)p_lut[1] * (int64_t)(p_calib[2] - p_calib[0]);
313	c = div64_s64(val1, val2);
314	dev_dbg(&st->client->dev, "val1 = %lld, val2 = %lld, c = %lld\n",
315		val1, val2, c);
316	val1 = (int64_t)p_calib[0] * (int64_t)p_lut[0] -
317		(int64_t)p_calib[1] * (int64_t)p_lut[1] -
318		(int64_t)(p_calib[1] - p_calib[0]) * c;
319	val2 = (int64_t)p_lut[0] - (int64_t)p_lut[1];
320	a = div64_s64(val1, val2);
321	dev_dbg(&st->client->dev, "val1 = %lld, val2 = %lld, a = %lld\n",
322		val1, val2, a);
323	b = ((int64_t)p_calib[0] - a) * ((int64_t)p_lut[0] + c);
324	dev_dbg(&st->client->dev, "b = %lld\n", b);
325
326	/*
327	 * pressure_Pa = a + (b / (c + raw_pressure))
328	 * pressure_mPa = 1000 * pressure_Pa
329	 */
330	pressure_mPa = 1000LL * a + div64_s64(1000LL * b, c + raw_pressure);
331
332	return pressure_mPa;
333}
334
335static int icp10100_read_raw_measures(struct iio_dev *indio_dev,
336				      struct iio_chan_spec const *chan,
337				      int *val, int *val2)
338{
339	struct icp10100_state *st = iio_priv(indio_dev);
340	uint32_t raw_pressure;
341	uint16_t raw_temp;
342	uint32_t pressure_mPa;
343	int ret;
344
345	if (!iio_device_claim_direct(indio_dev))
346		return -EBUSY;
347
348	ret = icp10100_get_measures(st, &raw_pressure, &raw_temp);
349	if (ret)
350		goto error_release;
351
352	switch (chan->type) {
353	case IIO_PRESSURE:
354		pressure_mPa = icp10100_get_pressure(st, raw_pressure,
355						     raw_temp);
356		/* mPa to kPa */
357		*val = pressure_mPa / 1000000;
358		*val2 = pressure_mPa % 1000000;
359		ret = IIO_VAL_INT_PLUS_MICRO;
360		break;
361	case IIO_TEMP:
362		*val = raw_temp;
363		ret = IIO_VAL_INT;
364		break;
365	default:
366		ret = -EINVAL;
367		break;
368	}
369
370error_release:
371	iio_device_release_direct(indio_dev);
372	return ret;
373}
374
375static int icp10100_read_raw(struct iio_dev *indio_dev,
376			     struct iio_chan_spec const *chan,
377			     int *val, int *val2, long mask)
378{
379	struct icp10100_state *st = iio_priv(indio_dev);
380
381	switch (mask) {
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) {
387		case IIO_TEMP:
388			/* 1000 * 175°C / 65536 in m°C */
389			*val = 2;
390			*val2 = 670288;
391			return IIO_VAL_INT_PLUS_MICRO;
392		default:
393			return -EINVAL;
394		}
395		break;
396	case IIO_CHAN_INFO_OFFSET:
397		switch (chan->type) {
398		case IIO_TEMP:
399			/* 1000 * -45°C in m°C */
400			*val = -45000;
401			return IIO_VAL_INT;
402		default:
403			return -EINVAL;
404		}
405		break;
406	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
407		mutex_lock(&st->lock);
408		*val = 1 << st->mode;
409		mutex_unlock(&st->lock);
410		return IIO_VAL_INT;
411	default:
412		return -EINVAL;
413	}
414}
415
416static int icp10100_read_avail(struct iio_dev *indio_dev,
417			       struct iio_chan_spec const *chan,
418			       const int **vals, int *type, int *length,
419			       long mask)
420{
421	static int oversamplings[] = {1, 2, 4, 8};
422
423	switch (mask) {
424	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
425		*vals = oversamplings;
426		*type = IIO_VAL_INT;
427		*length = ARRAY_SIZE(oversamplings);
428		return IIO_AVAIL_LIST;
429	default:
430		return -EINVAL;
431	}
432}
433
434static int icp10100_write_raw(struct iio_dev *indio_dev,
435			      struct iio_chan_spec const *chan,
436			      int val, int val2, long mask)
437{
438	struct icp10100_state *st = iio_priv(indio_dev);
439	unsigned int mode;
440
441	switch (mask) {
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))
445			return -EINVAL;
446		mode = ilog2(val);
447		if (mode >= ICP10100_MODE_NB)
448			return -EINVAL;
449		if (!iio_device_claim_direct(indio_dev))
450			return -EBUSY;
451		mutex_lock(&st->lock);
452		st->mode = mode;
453		mutex_unlock(&st->lock);
454		iio_device_release_direct(indio_dev);
455		return 0;
456	default:
457		return -EINVAL;
458	}
459}
460
461static int icp10100_write_raw_get_fmt(struct iio_dev *indio_dev,
462				      struct iio_chan_spec const *chan,
463				      long mask)
464{
465	switch (mask) {
466	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
467		return IIO_VAL_INT;
468	default:
469		return -EINVAL;
470	}
471}
472
473static const struct iio_info icp10100_info = {
474	.read_raw = icp10100_read_raw,
475	.read_avail = icp10100_read_avail,
476	.write_raw = icp10100_write_raw,
477	.write_raw_get_fmt = icp10100_write_raw_get_fmt,
478};
479
480static const struct iio_chan_spec icp10100_channels[] = {
481	{
482		.type = IIO_PRESSURE,
483		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
484		.info_mask_shared_by_all =
485			BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
486		.info_mask_shared_by_all_available =
487			BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
488	}, {
489		.type = IIO_TEMP,
490		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
491			BIT(IIO_CHAN_INFO_SCALE) |
492			BIT(IIO_CHAN_INFO_OFFSET),
493		.info_mask_shared_by_all =
494			BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
495		.info_mask_shared_by_all_available =
496			BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
497	},
498};
499
500static int icp10100_enable_regulator(struct icp10100_state *st)
501{
502	int ret;
503
504	ret = regulator_enable(st->vdd);
505	if (ret)
506		return ret;
507	msleep(100);
508
509	return 0;
510}
511
512static void icp10100_disable_regulator_action(void *data)
513{
514	struct icp10100_state *st = data;
515	int ret;
516
517	ret = regulator_disable(st->vdd);
518	if (ret)
519		dev_err(&st->client->dev, "error %d disabling vdd\n", ret);
520}
521
522static void icp10100_pm_disable(void *data)
523{
524	struct device *dev = data;
525
526	pm_runtime_disable(dev);
527}
528
529static int icp10100_probe(struct i2c_client *client)
530{
531	struct iio_dev *indio_dev;
532	struct icp10100_state *st;
533	int ret;
534
535	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
536		dev_err(&client->dev, "plain i2c transactions not supported\n");
537		return -ENODEV;
538	}
539
540	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
541	if (!indio_dev)
542		return -ENOMEM;
543
544	i2c_set_clientdata(client, indio_dev);
545	indio_dev->name = client->name;
546	indio_dev->modes = INDIO_DIRECT_MODE;
547	indio_dev->channels = icp10100_channels;
548	indio_dev->num_channels = ARRAY_SIZE(icp10100_channels);
549	indio_dev->info = &icp10100_info;
550
551	st = iio_priv(indio_dev);
552	mutex_init(&st->lock);
553	st->client = client;
554	st->mode = ICP10100_MODE_N;
555
556	st->vdd = devm_regulator_get(&client->dev, "vdd");
557	if (IS_ERR(st->vdd))
558		return PTR_ERR(st->vdd);
559
560	ret = icp10100_enable_regulator(st);
561	if (ret)
562		return ret;
563
564	ret = devm_add_action_or_reset(&client->dev,
565				       icp10100_disable_regulator_action, st);
566	if (ret)
567		return ret;
568
569	/* has to be done before the first i2c communication */
570	crc8_populate_msb(icp10100_crc8_table, ICP10100_CRC8_POLYNOMIAL);
571
572	ret = icp10100_init_chip(st);
573	if (ret) {
574		dev_err(&client->dev, "init chip error %d\n", ret);
575		return ret;
576	}
577
578	/* enable runtime pm with autosuspend delay of 2s */
579	pm_runtime_get_noresume(&client->dev);
580	pm_runtime_set_active(&client->dev);
581	pm_runtime_enable(&client->dev);
582	pm_runtime_set_autosuspend_delay(&client->dev, 2000);
583	pm_runtime_use_autosuspend(&client->dev);
584	pm_runtime_put(&client->dev);
585	ret = devm_add_action_or_reset(&client->dev, icp10100_pm_disable,
586				       &client->dev);
587	if (ret)
588		return ret;
589
590	return devm_iio_device_register(&client->dev, indio_dev);
591}
592
593static int icp10100_suspend(struct device *dev)
594{
595	struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
596	int ret;
597
598	mutex_lock(&st->lock);
599	ret = regulator_disable(st->vdd);
600	mutex_unlock(&st->lock);
601
602	return ret;
603}
604
605static int icp10100_resume(struct device *dev)
606{
607	struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
608	int ret;
609
610	mutex_lock(&st->lock);
611
612	ret = icp10100_enable_regulator(st);
613	if (ret)
614		goto out_unlock;
615
616	/* reset chip */
617	ret = icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
618
619out_unlock:
620	mutex_unlock(&st->lock);
621	return ret;
622}
623
624static DEFINE_RUNTIME_DEV_PM_OPS(icp10100_pm, icp10100_suspend, icp10100_resume,
625				 NULL);
626
627static const struct of_device_id icp10100_of_match[] = {
628	{
629		.compatible = "invensense,icp10100",
630	},
631	{ }
632};
633MODULE_DEVICE_TABLE(of, icp10100_of_match);
634
635static const struct i2c_device_id icp10100_id[] = {
636	{ "icp10100" },
637	{ }
638};
639MODULE_DEVICE_TABLE(i2c, icp10100_id);
640
641static struct i2c_driver icp10100_driver = {
642	.driver = {
643		.name = "icp10100",
644		.pm = pm_ptr(&icp10100_pm),
645		.of_match_table = icp10100_of_match,
646	},
647	.probe = icp10100_probe,
648	.id_table = icp10100_id,
649};
650module_i2c_driver(icp10100_driver);
651
652MODULE_AUTHOR("InvenSense, Inc.");
653MODULE_DESCRIPTION("InvenSense icp10100 driver");
654MODULE_LICENSE("GPL");
~/ Read-only file preview.