OSDN Git Service

Merge tag 'printk-for-5.10' of git://git.kernel.org/pub/scm/linux/kernel/git/printk...
[uclinux-h8/linux.git] / drivers / iio / pressure / icp10100.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/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
30 enum icp10100_mode {
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 */
35         ICP10100_MODE_NB,
36 };
37
38 struct 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
46 struct icp10100_command {
47         __be16 cmd;
48         unsigned long wait_us;
49         unsigned long wait_max_us;
50         size_t response_word_nb;
51 };
52
53 static 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
60 static 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
66 static 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
72 static 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
99 static const uint8_t icp10100_switch_mode_otp[] =
100         {0xC5, 0x95, 0x00, 0x66, 0x9c};
101
102 DECLARE_CRC8_TABLE(icp10100_crc8_table);
103
104 static 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
119 static 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                 /* direct command-response without waiting */
155                 ret = icp10100_i2c_xfer(st->client->adapter, msgs,
156                                         ARRAY_SIZE(msgs));
157                 if (ret)
158                         return ret;
159         } else {
160                 /* transfer command write */
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                 /* transfer response read if needed */
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         /* 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,
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
193 static int icp10100_read_cal_otp(struct icp10100_state *st)
194 {
195         __be16 val;
196         int i;
197         int ret;
198
199         /* switch into OTP read mode */
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         /* read 4 calibration values */
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
220 static int icp10100_init_chip(struct icp10100_state *st)
221 {
222         __be16 val;
223         uint16_t id;
224         int ret;
225
226         /* read and check id */
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         /* read calibration data from OTP */
237         ret = icp10100_read_cal_otp(st);
238         if (ret)
239                 return ret;
240
241         /* reset chip */
242         return icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
243 }
244
245 static 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);
266 error_measure:
267         pm_runtime_put_autosuspend(&st->client->dev);
268         return ret;
269 }
270
271 static 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         /* compute p_lut values */
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         /* 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",
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          * pressure_Pa = a + (b / (c + raw_pressure))
326          * pressure_mPa = 1000 * pressure_Pa
327          */
328         pressure_mPa = 1000LL * a + div64_s64(1000LL * b, c + raw_pressure);
329
330         return pressure_mPa;
331 }
332
333 static 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                 /* mPa to kPa */
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
369 error_release:
370         iio_device_release_direct_mode(indio_dev);
371         return ret;
372 }
373
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)
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                         /* 1000 * 175°C / 65536 in m°C */
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                         /* 1000 * -45°C in m°C */
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
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,
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
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)
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                 /* 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                 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
462 static 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
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,
479 };
480
481 static 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
501 static 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
513 static 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
523 static 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
531 static 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->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;
553
554         st = iio_priv(indio_dev);
555         mutex_init(&st->lock);
556         st->client = client;
557         st->mode = ICP10100_MODE_N;
558
559         st->vdd = devm_regulator_get(&client->dev, "vdd");
560         if (IS_ERR(st->vdd))
561                 return PTR_ERR(st->vdd);
562
563         ret = icp10100_enable_regulator(st);
564         if (ret)
565                 return ret;
566
567         ret = devm_add_action_or_reset(&client->dev,
568                                        icp10100_disable_regulator_action, st);
569         if (ret)
570                 return ret;
571
572         /* has to be done before the first i2c communication */
573         crc8_populate_msb(icp10100_crc8_table, ICP10100_CRC8_POLYNOMIAL);
574
575         ret = icp10100_init_chip(st);
576         if (ret) {
577                 dev_err(&client->dev, "init chip error %d\n", ret);
578                 return ret;
579         }
580
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,
589                                        &client->dev);
590         if (ret)
591                 return ret;
592
593         return devm_iio_device_register(&client->dev, indio_dev);
594 }
595
596 static int __maybe_unused icp10100_suspend(struct device *dev)
597 {
598         struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
599         int ret;
600
601         mutex_lock(&st->lock);
602         ret = regulator_disable(st->vdd);
603         mutex_unlock(&st->lock);
604
605         return ret;
606 }
607
608 static int __maybe_unused icp10100_resume(struct device *dev)
609 {
610         struct icp10100_state *st = iio_priv(dev_get_drvdata(dev));
611         int ret;
612
613         mutex_lock(&st->lock);
614
615         ret = icp10100_enable_regulator(st);
616         if (ret)
617                 goto out_unlock;
618
619         /* reset chip */
620         ret = icp10100_send_cmd(st, &icp10100_cmd_soft_reset, NULL, 0);
621
622 out_unlock:
623         mutex_unlock(&st->lock);
624         return ret;
625 }
626
627 static UNIVERSAL_DEV_PM_OPS(icp10100_pm, icp10100_suspend, icp10100_resume,
628                             NULL);
629
630 static const struct of_device_id icp10100_of_match[] = {
631         {
632                 .compatible = "invensense,icp10100",
633         },
634         { }
635 };
636 MODULE_DEVICE_TABLE(of, icp10100_of_match);
637
638 static const struct i2c_device_id icp10100_id[] = {
639         { "icp10100", 0 },
640         { }
641 };
642 MODULE_DEVICE_TABLE(i2c, icp10100_id);
643
644 static struct i2c_driver icp10100_driver = {
645         .driver = {
646                 .name = "icp10100",
647                 .pm = &icp10100_pm,
648                 .of_match_table = of_match_ptr(icp10100_of_match),
649         },
650         .probe = icp10100_probe,
651         .id_table = icp10100_id,
652 };
653 module_i2c_driver(icp10100_driver);
654
655 MODULE_AUTHOR("InvenSense, Inc.");
656 MODULE_DESCRIPTION("InvenSense icp10100 driver");
657 MODULE_LICENSE("GPL");