1
2
3
4
5
6
7
8
9
10
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>
22
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
29
30enum icp10100_mode {
31 ICP10100_MODE_LP,
32 ICP10100_MODE_N,
33 ICP10100_MODE_LN,
34 ICP10100_MODE_ULN,
35 ICP10100_MODE_NB,
36};
37
38struct icp10100_state {
39 struct mutex lock;
40 struct i2c_client *client;
41 struct regulator *vdd;
42 enum icp10100_mode mode;
43 int16_t cal[4];
44};
45
46struct icp10100_command {
47 __be16 cmd;
48 unsigned long wait_us;
49 unsigned long wait_max_us;
50 size_t response_word_nb;
51};
52
53static const struct icp10100_command icp10100_cmd_soft_reset = {
54 .cmd = cpu_to_be16(0x805D),
55 .wait_us = 170,
56 .wait_max_us = 200,
57 .response_word_nb = 0,
58};
59
60static const struct icp10100_command icp10100_cmd_read_id = {
61 .cmd = cpu_to_be16(0xEFC8),
62 .wait_us = 0,
63 .response_word_nb = 1,
64};
65
66static const struct icp10100_command icp10100_cmd_read_otp = {
67 .cmd = cpu_to_be16(0xC7F7),
68 .wait_us = 0,
69 .response_word_nb = 1,
70};
71
72static const struct icp10100_command icp10100_cmd_measure[] = {
73 [ICP10100_MODE_LP] = {
74 .cmd = cpu_to_be16(0x401A),
75 .wait_us = 1800,
76 .wait_max_us = 2000,
77 .response_word_nb = 3,
78 },
79 [ICP10100_MODE_N] = {
80 .cmd = cpu_to_be16(0x48A3),
81 .wait_us = 6300,
82 .wait_max_us = 6500,
83 .response_word_nb = 3,
84 },
85 [ICP10100_MODE_LN] = {
86 .cmd = cpu_to_be16(0x5059),
87 .wait_us = 23800,
88 .wait_max_us = 24000,
89 .response_word_nb = 3,
90 },
91 [ICP10100_MODE_ULN] = {
92 .cmd = cpu_to_be16(0x58E0),
93 .wait_us = 94500,
94 .wait_max_us = 94700,
95 .response_word_nb = 3,
96 },
97};
98
99static const uint8_t icp10100_switch_mode_otp[] =
100 {0xC5, 0x95, 0x00, 0x66, 0x9c};
101
102DECLARE_CRC8_TABLE(icp10100_crc8_table);
103
104static inline int icp10100_i2c_xfer(struct i2c_adapter *adap,
105 struct i2c_msg *msgs, int num)
106{
107 int ret;
108
109 ret = i2c_transfer(adap, msgs, num);
110 if (ret < 0)
111 return ret;
112
113 if (ret != num)
114 return -EIO;
115
116 return 0;
117}
118
119static int icp10100_send_cmd(struct icp10100_state *st,
120 const struct icp10100_command *cmd,
121 __be16 *buf, size_t buf_len)
122{
123 size_t size = cmd->response_word_nb * ICP10100_RESPONSE_WORD_LENGTH;
124 uint8_t data[16];
125 uint8_t *ptr;
126 uint8_t *buf_ptr = (uint8_t *)buf;
127 struct i2c_msg msgs[2] = {
128 {
129 .addr = st->client->addr,
130 .flags = 0,
131 .len = 2,
132 .buf = (uint8_t *)&cmd->cmd,
133 }, {
134 .addr = st->client->addr,
135 .flags = I2C_M_RD,
136 .len = size,
137 .buf = data,
138 },
139 };
140 uint8_t crc;
141 unsigned int i;
142 int ret;
143
144 if (size > sizeof(data))
145 return -EINVAL;
146
147 if (cmd->response_word_nb > 0 &&
148 (buf == NULL || buf_len < (cmd->response_word_nb * 2)))
149 return -EINVAL;
150
151 dev_dbg(&st->client->dev, "sending cmd %#x\n", be16_to_cpu(cmd->cmd));
152
153 if (cmd->response_word_nb > 0 && cmd->wait_us == 0) {
154
155 ret = icp10100_i2c_xfer(st->client->adapter, msgs,
156 ARRAY_SIZE(msgs));
157 if (ret)
158 return ret;
159 } else {
160
161 ret = icp10100_i2c_xfer(st->client->adapter, &msgs[0], 1);
162 if (ret)
163 return ret;
164 if (cmd->wait_us > 0)
165 usleep_range(cmd->wait_us, cmd->wait_max_us);
166
167 if (cmd->response_word_nb > 0) {
168 ret = icp10100_i2c_xfer(st->client->adapter, &msgs[1], 1);
169 if (ret)
170 return ret;
171 } else {
172 return 0;
173 }
174 }
175
176
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,
180 ICP10100_CRC8_INIT);
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);
184 return -EIO;
185 }
186 *buf_ptr++ = ptr[0];
187 *buf_ptr++ = ptr[1];
188 }
189
190 return 0;
191}
192
193static int icp10100_read_cal_otp(struct icp10100_state *st)
194{
195 __be16 val;
196 int i;
197 int ret;
198
199
200 ret = i2c_master_send(st->client, icp10100_switch_mode_otp,
201 ARRAY_SIZE(icp10100_switch_mode_otp));
202 if (ret < 0)
203 return ret;
204 if (ret != ARRAY_SIZE(icp10100_switch_mode_otp))
205 return -EIO;
206
207
208 for (i = 0; i < 4; ++i) {
209 ret = icp10100_send_cmd(st, &icp10100_cmd_read_otp,
210 &val, sizeof(val));
211 if (ret)
212 return ret;
213 st->cal[i] = be16_to_cpu(val);
214 dev_dbg(&st->client->dev, "cal[%d] = %d\n", i, st->cal[i]);
215 }
216
217 return 0;
218}
219
220static int icp10100_init_chip(struct icp10100_state *st)
221{
222 __be16 val;
223 uint16_t id;
224 int ret;
225
226
227 ret = icp10100_send_cmd(st, &icp10100_cmd_read_id, &val, sizeof(val));
228 if (ret)
229 return ret;
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);
233 return -ENODEV;
234 }
235
236
237 ret = icp10100_read_cal_otp(st);
238 if (ret)
239 return ret;
240
241
242 return icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
243}
244
245static int icp10100_get_measures(struct icp10100_state *st,
246 uint32_t *pressure, uint16_t *temperature)
247{
248 const struct icp10100_command *cmd;
249 __be16 measures[3];
250 int ret;
251
252 pm_runtime_get_sync(&st->client->dev);
253
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);
258 if (ret)
259 goto error_measure;
260
261 *pressure = (be16_to_cpu(measures[0]) << 8) |
262 (be16_to_cpu(measures[1]) >> 8);
263 *temperature = be16_to_cpu(measures[2]);
264
265 pm_runtime_mark_last_busy(&st->client->dev);
266error_measure:
267 pm_runtime_put_autosuspend(&st->client->dev);
268 return ret;
269}
270
271static uint32_t icp10100_get_pressure(struct icp10100_state *st,
272 uint32_t raw_pressure, uint16_t raw_temp)
273{
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;
279 int64_t val1, val2;
280 int32_t p_lut[3];
281 int32_t t, t_square;
282 int64_t a, b, c;
283 uint32_t pressure_mPa;
284
285 dev_dbg(&st->client->dev, "raw: pressure = %u, temp = %u\n",
286 raw_pressure, raw_temp);
287
288
289 t = (int32_t)raw_temp - 32768;
290 t_square = t * t;
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]);
300
301
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",
313 val1, val2, c);
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",
320 val1, val2, a);
321 b = ((int64_t)p_calib[0] - a) * ((int64_t)p_lut[0] + c);
322 dev_dbg(&st->client->dev, "b = %lld\n", b);
323
324
325
326
327
328 pressure_mPa = 1000LL * a + div64_s64(1000LL * b, c + raw_pressure);
329
330 return pressure_mPa;
331}
332
333static int icp10100_read_raw_measures(struct iio_dev *indio_dev,
334 struct iio_chan_spec const *chan,
335 int *val, int *val2)
336{
337 struct icp10100_state *st = iio_priv(indio_dev);
338 uint32_t raw_pressure;
339 uint16_t raw_temp;
340 uint32_t pressure_mPa;
341 int ret;
342
343 ret = iio_device_claim_direct_mode(indio_dev);
344 if (ret)
345 return ret;
346
347 ret = icp10100_get_measures(st, &raw_pressure, &raw_temp);
348 if (ret)
349 goto error_release;
350
351 switch (chan->type) {
352 case IIO_PRESSURE:
353 pressure_mPa = icp10100_get_pressure(st, raw_pressure,
354 raw_temp);
355
356 *val = pressure_mPa / 1000000;
357 *val2 = pressure_mPa % 1000000;
358 ret = IIO_VAL_INT_PLUS_MICRO;
359 break;
360 case IIO_TEMP:
361 *val = raw_temp;
362 ret = IIO_VAL_INT;
363 break;
364 default:
365 ret = -EINVAL;
366 break;
367 }
368
369error_release:
370 iio_device_release_direct_mode(indio_dev);
371 return ret;
372}
373
374static int icp10100_read_raw(struct iio_dev *indio_dev,
375 struct iio_chan_spec const *chan,
376 int *val, int *val2, long mask)
377{
378 struct icp10100_state *st = iio_priv(indio_dev);
379
380 switch (mask) {
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) {
386 case IIO_TEMP:
387
388 *val = 2;
389 *val2 = 670288;
390 return IIO_VAL_INT_PLUS_MICRO;
391 default:
392 return -EINVAL;
393 }
394 break;
395 case IIO_CHAN_INFO_OFFSET:
396 switch (chan->type) {
397 case IIO_TEMP:
398
399 *val = -45000;
400 return IIO_VAL_INT;
401 default:
402 return -EINVAL;
403 }
404 break;
405 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
406 mutex_lock(&st->lock);
407 *val = 1 << st->mode;
408 mutex_unlock(&st->lock);
409 return IIO_VAL_INT;
410 default:
411 return -EINVAL;
412 }
413}
414
415static int icp10100_read_avail(struct iio_dev *indio_dev,
416 struct iio_chan_spec const *chan,
417 const int **vals, int *type, int *length,
418 long mask)
419{
420 static int oversamplings[] = {1, 2, 4, 8};
421
422 switch (mask) {
423 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
424 *vals = oversamplings;
425 *type = IIO_VAL_INT;
426 *length = ARRAY_SIZE(oversamplings);
427 return IIO_AVAIL_LIST;
428 default:
429 return -EINVAL;
430 }
431}
432
433static int icp10100_write_raw(struct iio_dev *indio_dev,
434 struct iio_chan_spec const *chan,
435 int val, int val2, long mask)
436{
437 struct icp10100_state *st = iio_priv(indio_dev);
438 unsigned int mode;
439 int ret;
440
441 switch (mask) {
442 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
443
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 ret = iio_device_claim_direct_mode(indio_dev);
450 if (ret)
451 return ret;
452 mutex_lock(&st->lock);
453 st->mode = mode;
454 mutex_unlock(&st->lock);
455 iio_device_release_direct_mode(indio_dev);
456 return 0;
457 default:
458 return -EINVAL;
459 }
460}
461
462static int icp10100_write_raw_get_fmt(struct iio_dev *indio_dev,
463 struct iio_chan_spec const *chan,
464 long mask)
465{
466 switch (mask) {
467 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
468 return IIO_VAL_INT;
469 default:
470 return -EINVAL;
471 }
472}
473
474static 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,
479};
480
481static const struct iio_chan_spec icp10100_channels[] = {
482 {
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),
489 }, {
490 .type = IIO_TEMP,
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),
498 },
499};
500
501static int icp10100_enable_regulator(struct icp10100_state *st)
502{
503 int ret;
504
505 ret = regulator_enable(st->vdd);
506 if (ret)
507 return ret;
508 msleep(100);
509
510 return 0;
511}
512
513static void icp10100_disable_regulator_action(void *data)
514{
515 struct icp10100_state *st = data;
516 int ret;
517
518 ret = regulator_disable(st->vdd);
519 if (ret)
520 dev_err(&st->client->dev, "error %d disabling vdd\n", ret);
521}
522
523static void icp10100_pm_disable(void *data)
524{
525 struct device *dev = data;
526
527 pm_runtime_put_sync_suspend(dev);
528 pm_runtime_disable(dev);
529}
530
531static int icp10100_probe(struct i2c_client *client,
532 const struct i2c_device_id *id)
533{
534 struct iio_dev *indio_dev;
535 struct icp10100_state *st;
536 int ret;
537
538 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
539 dev_err(&client->dev, "plain i2c transactions not supported\n");
540 return -ENODEV;
541 }
542
543 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
544 if (!indio_dev)
545 return -ENOMEM;
546
547 i2c_set_clientdata(client, indio_dev);
548 indio_dev->dev.parent = &client->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;
554
555 st = iio_priv(indio_dev);
556 mutex_init(&st->lock);
557 st->client = client;
558 st->mode = ICP10100_MODE_N;
559
560 st->vdd = devm_regulator_get(&client->dev, "vdd");
561 if (IS_ERR(st->vdd))
562 return PTR_ERR(st->vdd);
563
564 ret = icp10100_enable_regulator(st);
565 if (ret)
566 return ret;
567
568 ret = devm_add_action_or_reset(&client->dev,
569 icp10100_disable_regulator_action, st);
570 if (ret)
571 return ret;
572
573
574 crc8_populate_msb(icp10100_crc8_table, ICP10100_CRC8_POLYNOMIAL);
575
576 ret = icp10100_init_chip(st);
577 if (ret) {
578 dev_err(&client->dev, "init chip error %d\n", ret);
579 return ret;
580 }
581
582
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,
590 &client->dev);
591 if (ret)
592 return ret;
593
594 return devm_iio_device_register(&client->dev, indio_dev);
595}
596
597static int __maybe_unused icp10100_suspend(struct device *dev)
598{
599 struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
600 int ret;
601
602 mutex_lock(&st->lock);
603 ret = regulator_disable(st->vdd);
604 mutex_unlock(&st->lock);
605
606 return ret;
607}
608
609static int __maybe_unused icp10100_resume(struct device *dev)
610{
611 struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
612 int ret;
613
614 mutex_lock(&st->lock);
615
616 ret = icp10100_enable_regulator(st);
617 if (ret)
618 goto out_unlock;
619
620
621 ret = icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
622
623out_unlock:
624 mutex_unlock(&st->lock);
625 return ret;
626}
627
628static UNIVERSAL_DEV_PM_OPS(icp10100_pm, icp10100_suspend, icp10100_resume,
629 NULL);
630
631static const struct of_device_id icp10100_of_match[] = {
632 {
633 .compatible = "invensense,icp10100",
634 },
635 { }
636};
637MODULE_DEVICE_TABLE(of, icp10100_of_match);
638
639static const struct i2c_device_id icp10100_id[] = {
640 { "icp10100", 0 },
641 { }
642};
643MODULE_DEVICE_TABLE(i2c, icp10100_id);
644
645static struct i2c_driver icp10100_driver = {
646 .driver = {
647 .name = "icp10100",
648 .pm = &icp10100_pm,
649 .of_match_table = of_match_ptr(icp10100_of_match),
650 },
651 .probe = icp10100_probe,
652 .id_table = icp10100_id,
653};
654module_i2c_driver(icp10100_driver);
655
656MODULE_AUTHOR("InvenSense, Inc.");
657MODULE_DESCRIPTION("InvenSense icp10100 driver");
658MODULE_LICENSE("GPL");
659