developer | 3e9ed99 | 2023-10-11 13:59:48 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0
|
| 2 | /*
|
| 3 | * The ZTS8032 is a barometric pressure and temperature sensor.
|
| 4 | * Currently only reading a single temperature is supported by
|
| 5 | * this driver.
|
| 6 | */
|
| 7 |
|
| 8 | #include <linux/i2c.h>
|
| 9 | #include <linux/limits.h>
|
| 10 | #include <linux/math64.h>
|
| 11 | #include <linux/module.h>
|
| 12 | #include <linux/regmap.h>
|
| 13 |
|
| 14 | #include <linux/iio/iio.h>
|
| 15 | #include <linux/iio/sysfs.h>
|
| 16 |
|
| 17 | #define ZTS8032_DEV_NAME "zts8032"
|
| 18 |
|
| 19 | #define ZTS8032_PRS_B0 0x00
|
| 20 | #define ZTS8032_PRS_B1 0x01
|
| 21 | #define ZTS8032_PRS_B2 0x02
|
| 22 | #define ZTS8032_TMP_B0 0x03
|
| 23 | #define ZTS8032_TMP_B1 0x04
|
| 24 | #define ZTS8032_TMP_B2 0x05
|
| 25 | #define ZTS8032_PRS_CFG 0x06
|
| 26 | #define ZTS8032_PRS_RATE_BITS GENMASK(6, 4)
|
| 27 | #define ZTS8032_PRS_PRC_BITS GENMASK(3, 0)
|
| 28 | #define ZTS8032_TMP_CFG 0x07
|
| 29 | #define ZTS8032_TMP_RATE_BITS GENMASK(6, 4)
|
| 30 | #define ZTS8032_TMP_PRC_BITS GENMASK(3, 0)
|
| 31 | #define ZTS8032_TMP_EXT BIT(7)
|
| 32 | #define ZTS8032_MEAS_CFG 0x08
|
| 33 | #define ZTS8032_MEAS_CTRL_BITS GENMASK(2, 0)
|
| 34 | #define ZTS8032_PRS_EN BIT(0)
|
| 35 | #define ZTS8032_TEMP_EN BIT(1)
|
| 36 | #define ZTS8032_BACKGROUND BIT(2)
|
| 37 | #define ZTS8032_PRS_RDY BIT(4)
|
| 38 | #define ZTS8032_TMP_RDY BIT(5)
|
| 39 | #define ZTS8032_SENSOR_RDY BIT(6)
|
| 40 | #define ZTS8032_COEF_RDY BIT(7)
|
| 41 | #define ZTS8032_CFG_REG 0x09
|
| 42 | #define ZTS8032_INT_HL BIT(7)
|
| 43 | #define ZTS8032_TMP_SHIFT_EN BIT(3)
|
| 44 | #define ZTS8032_PRS_SHIFT_EN BIT(4)
|
| 45 | #define ZTS8032_FIFO_EN BIT(5)
|
| 46 | #define ZTS8032_SPI_EN BIT(6)
|
| 47 | #define ZTS8032_RESET 0x0c
|
| 48 | #define ZTS8032_RESET_MAGIC 0x09
|
| 49 | #define ZTS8032_COEF_BASE 0x10
|
| 50 |
|
| 51 | /* Make sure sleep time is <= 20ms for usleep_range */
|
| 52 | #define ZTS8032_POLL_SLEEP_US(t) min(20000, (t) / 8)
|
| 53 | /* Silently handle error in rate value here */
|
| 54 | #define ZTS8032_POLL_TIMEOUT_US(rc) ((rc) <= 0 ? 1000000 : 1000000 / (rc))
|
| 55 |
|
| 56 | #define ZTS8032_PRS_BASE ZTS8032_PRS_B0
|
| 57 | #define ZTS8032_TMP_BASE ZTS8032_TMP_B0
|
| 58 |
|
| 59 | /*
|
| 60 | * These values (defined in the spec) indicate how to scale the raw register
|
| 61 | * values for each level of precision available.
|
| 62 | */
|
| 63 | static const int scale_factors[] = {
|
| 64 | 524288,
|
| 65 | 1572864,
|
| 66 | 3670016,
|
| 67 | 7864320,
|
| 68 | 253952,
|
| 69 | 516096,
|
| 70 | 1040384,
|
| 71 | 2088960,
|
| 72 | };
|
| 73 |
|
| 74 | struct zts8032_data {
|
| 75 | struct i2c_client *client;
|
| 76 | struct regmap *regmap;
|
| 77 | struct mutex lock; /* Lock for sequential HW access functions */
|
| 78 |
|
| 79 | s32 c0, c1;
|
| 80 | s32 c00, c10, c20, c30, c40, c01, c11, c21, c31;
|
| 81 | s32 pressure_raw;
|
| 82 | s32 temp_raw;
|
| 83 | };
|
| 84 |
|
| 85 | static const struct iio_chan_spec zts8032_channels[] = {
|
| 86 | {
|
| 87 | .type = IIO_TEMP,
|
| 88 | .info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
|
| 89 | BIT(IIO_CHAN_INFO_SAMP_FREQ) |
|
| 90 | BIT(IIO_CHAN_INFO_PROCESSED),
|
| 91 | },
|
| 92 | {
|
| 93 | .type = IIO_PRESSURE,
|
| 94 | .info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
|
| 95 | BIT(IIO_CHAN_INFO_SAMP_FREQ) |
|
| 96 | BIT(IIO_CHAN_INFO_PROCESSED),
|
| 97 | },
|
| 98 | };
|
| 99 |
|
| 100 | /* To be called after checking the COEF_RDY bit in MEAS_CFG */
|
| 101 | static int zts8032_get_coefs(struct zts8032_data *data)
|
| 102 | {
|
| 103 | int rc;
|
| 104 | u8 coef[22];
|
| 105 | u32 c0, c1;
|
| 106 | u32 c00, c10, c20, c30, c40, c01, c11, c21, c31;
|
| 107 |
|
| 108 | /* Read all sensor calibration coefficients from the COEF registers. */
|
| 109 | rc = regmap_bulk_read(data->regmap, ZTS8032_COEF_BASE, coef,
|
| 110 | sizeof(coef));
|
| 111 | if (rc < 0)
|
| 112 | return rc;
|
| 113 |
|
| 114 | /*
|
| 115 | * Calculate temperature calibration coefficients c0 and c1. The
|
| 116 | * numbers are 12-bit 2's complement numbers.
|
| 117 | */
|
| 118 | c0 = (coef[0] << 4) | (coef[1] >> 4);
|
| 119 | data->c0 = sign_extend32(c0, 11);
|
| 120 |
|
| 121 | c1 = ((coef[1] & GENMASK(3, 0)) << 8) | coef[2];
|
| 122 | data->c1 = sign_extend32(c1, 11);
|
| 123 |
|
| 124 | /*
|
| 125 | * Calculate pressure calibration coefficients. c00 and c10 are 20 bit
|
| 126 | * 2's complement numbers, while the rest are 16 bit 2's complement
|
| 127 | * numbers.
|
| 128 | */
|
| 129 | c00 = (coef[3] << 12) | (coef[4] << 4) | (coef[5] >> 4);
|
| 130 | data->c00 = sign_extend32(c00, 19);
|
| 131 |
|
| 132 | c10 = ((coef[5] & GENMASK(3, 0)) << 16) | (coef[6] << 8) | coef[7];
|
| 133 | data->c10 = sign_extend32(c10, 19);
|
| 134 |
|
| 135 | c01 = (coef[8] << 8) | coef[9];
|
| 136 | data->c01 = sign_extend32(c01, 15);
|
| 137 |
|
| 138 | c11 = (coef[10] << 8) | coef[11];
|
| 139 | data->c11 = sign_extend32(c11, 15);
|
| 140 |
|
| 141 | c20 = (coef[12] << 8) | coef[13];
|
| 142 | data->c20 = sign_extend32(c20, 15);
|
| 143 |
|
| 144 | c21 = (coef[14] << 8) | coef[15];
|
| 145 | data->c21 = sign_extend32(c21, 15);
|
| 146 |
|
| 147 | c30 = (coef[16] << 8) | coef[17];
|
| 148 | data->c30 = sign_extend32(c30, 15);
|
| 149 |
|
| 150 | c31 = (coef[18] << 8) | coef[19];
|
| 151 | data->c31 = sign_extend32(c31, 15);
|
| 152 |
|
| 153 | c40 = (coef[20] << 8) | coef[21];
|
| 154 | data->c40 = sign_extend32(c40, 15);
|
| 155 |
|
| 156 | return 0;
|
| 157 | }
|
| 158 |
|
| 159 | static int zts8032_get_pres_precision(struct zts8032_data *data)
|
| 160 | {
|
| 161 | int rc;
|
| 162 | int val;
|
| 163 |
|
| 164 | rc = regmap_read(data->regmap, ZTS8032_PRS_CFG, &val);
|
| 165 | if (rc < 0)
|
| 166 | return rc;
|
| 167 |
|
| 168 | return BIT(val & GENMASK(2, 0));
|
| 169 | }
|
| 170 |
|
| 171 | static int zts8032_get_temp_precision(struct zts8032_data *data)
|
| 172 | {
|
| 173 | int rc;
|
| 174 | int val;
|
| 175 |
|
| 176 | rc = regmap_read(data->regmap, ZTS8032_TMP_CFG, &val);
|
| 177 | if (rc < 0)
|
| 178 | return rc;
|
| 179 |
|
| 180 | /*
|
| 181 | * Scale factor is bottom 4 bits of the register, but 1111 is
|
| 182 | * reserved so just grab bottom three
|
| 183 | */
|
| 184 | return BIT(val & GENMASK(2, 0));
|
| 185 | }
|
| 186 |
|
| 187 | /* Called with lock held */
|
| 188 | static int zts8032_set_pres_precision(struct zts8032_data *data, int val)
|
| 189 | {
|
| 190 | int rc;
|
| 191 | u8 shift_en;
|
| 192 |
|
| 193 | if (val < 0 || val > 128)
|
| 194 | return -EINVAL;
|
| 195 |
|
| 196 | shift_en = val >= 16 ? ZTS8032_PRS_SHIFT_EN : 0;
|
| 197 | rc = regmap_write_bits(data->regmap, ZTS8032_CFG_REG,
|
| 198 | ZTS8032_PRS_SHIFT_EN, shift_en);
|
| 199 | if (rc)
|
| 200 | return rc;
|
| 201 |
|
| 202 | return regmap_update_bits(data->regmap, ZTS8032_PRS_CFG,
|
| 203 | ZTS8032_PRS_PRC_BITS, ilog2(val));
|
| 204 | }
|
| 205 |
|
| 206 | /* Called with lock held */
|
| 207 | static int zts8032_set_temp_precision(struct zts8032_data *data, int val)
|
| 208 | {
|
| 209 | int rc;
|
| 210 | u8 shift_en;
|
| 211 |
|
| 212 | if (val < 0 || val > 128)
|
| 213 | return -EINVAL;
|
| 214 |
|
| 215 | shift_en = val >= 16 ? ZTS8032_TMP_SHIFT_EN : 0;
|
| 216 | rc = regmap_write_bits(data->regmap, ZTS8032_CFG_REG,
|
| 217 | ZTS8032_TMP_SHIFT_EN, shift_en);
|
| 218 | if (rc)
|
| 219 | return rc;
|
| 220 |
|
| 221 | return regmap_update_bits(data->regmap, ZTS8032_TMP_CFG,
|
| 222 | ZTS8032_TMP_PRC_BITS, ilog2(val));
|
| 223 | }
|
| 224 |
|
| 225 | /* Called with lock held */
|
| 226 | static int zts8032_set_pres_samp_freq(struct zts8032_data *data, int freq)
|
| 227 | {
|
| 228 | u8 val;
|
| 229 |
|
| 230 | if (freq < 0 || freq > 128)
|
| 231 | return -EINVAL;
|
| 232 |
|
| 233 | val = ilog2(freq) << 4;
|
| 234 |
|
| 235 | return regmap_update_bits(data->regmap, ZTS8032_PRS_CFG,
|
| 236 | ZTS8032_PRS_RATE_BITS, val);
|
| 237 | }
|
| 238 |
|
| 239 | /* Called with lock held */
|
| 240 | static int zts8032_set_temp_samp_freq(struct zts8032_data *data, int freq)
|
| 241 | {
|
| 242 | u8 val;
|
| 243 |
|
| 244 | if (freq < 0 || freq > 128)
|
| 245 | return -EINVAL;
|
| 246 |
|
| 247 | val = ilog2(freq) << 4;
|
| 248 |
|
| 249 | return regmap_update_bits(data->regmap, ZTS8032_TMP_CFG,
|
| 250 | ZTS8032_TMP_RATE_BITS, val);
|
| 251 | }
|
| 252 |
|
| 253 | static int zts8032_get_pres_samp_freq(struct zts8032_data *data)
|
| 254 | {
|
| 255 | int rc;
|
| 256 | int val;
|
| 257 |
|
| 258 | rc = regmap_read(data->regmap, ZTS8032_PRS_CFG, &val);
|
| 259 | if (rc < 0)
|
| 260 | return rc;
|
| 261 |
|
| 262 | return BIT((val & ZTS8032_PRS_RATE_BITS) >> 4);
|
| 263 | }
|
| 264 |
|
| 265 | static int zts8032_get_temp_samp_freq(struct zts8032_data *data)
|
| 266 | {
|
| 267 | int rc;
|
| 268 | int val;
|
| 269 |
|
| 270 | rc = regmap_read(data->regmap, ZTS8032_TMP_CFG, &val);
|
| 271 | if (rc < 0)
|
| 272 | return rc;
|
| 273 |
|
| 274 | return BIT((val & ZTS8032_TMP_RATE_BITS) >> 4);
|
| 275 | }
|
| 276 |
|
| 277 | static int zts8032_get_pres_k(struct zts8032_data *data)
|
| 278 | {
|
| 279 | int rc = zts8032_get_pres_precision(data);
|
| 280 |
|
| 281 | if (rc < 0)
|
| 282 | return rc;
|
| 283 |
|
| 284 | return scale_factors[ilog2(rc)];
|
| 285 | }
|
| 286 |
|
| 287 | static int zts8032_get_temp_k(struct zts8032_data *data)
|
| 288 | {
|
| 289 | int rc = zts8032_get_temp_precision(data);
|
| 290 |
|
| 291 | if (rc < 0)
|
| 292 | return rc;
|
| 293 |
|
| 294 | return scale_factors[ilog2(rc)];
|
| 295 | }
|
| 296 |
|
| 297 | static int zts8032_read_pres_raw(struct zts8032_data *data)
|
| 298 | {
|
| 299 | int rc;
|
| 300 | int rate;
|
| 301 | int ready;
|
| 302 | int timeout;
|
| 303 | s32 raw;
|
| 304 | u8 val[3];
|
| 305 |
|
| 306 | if (mutex_lock_interruptible(&data->lock))
|
| 307 | return -EINTR;
|
| 308 |
|
| 309 | rate = zts8032_get_pres_samp_freq(data);
|
| 310 | timeout = ZTS8032_POLL_TIMEOUT_US(rate);
|
| 311 |
|
| 312 | /* Poll for sensor readiness; base the timeout upon the sample rate. */
|
| 313 | rc = regmap_read_poll_timeout(data->regmap, ZTS8032_MEAS_CFG, ready,
|
| 314 | ready & ZTS8032_PRS_RDY,
|
| 315 | ZTS8032_POLL_SLEEP_US(timeout), timeout);
|
| 316 | if (rc)
|
| 317 | goto done;
|
| 318 |
|
| 319 | rc = regmap_bulk_read(data->regmap, ZTS8032_PRS_BASE, val, sizeof(val));
|
| 320 | if (rc < 0)
|
| 321 | goto done;
|
| 322 |
|
| 323 | raw = (val[0] << 16) | (val[1] << 8) | val[2];
|
| 324 | data->pressure_raw = sign_extend32(raw, 23);
|
| 325 |
|
| 326 | done:
|
| 327 | mutex_unlock(&data->lock);
|
| 328 | return rc;
|
| 329 | }
|
| 330 |
|
| 331 | /* Called with lock held */
|
| 332 | static int zts8032_read_temp_ready(struct zts8032_data *data)
|
| 333 | {
|
| 334 | int rc;
|
| 335 | u8 val[3];
|
| 336 | s32 raw;
|
| 337 |
|
| 338 | rc = regmap_bulk_read(data->regmap, ZTS8032_TMP_BASE, val, sizeof(val));
|
| 339 | if (rc < 0)
|
| 340 | return rc;
|
| 341 |
|
| 342 | raw = (val[0] << 16) | (val[1] << 8) | val[2];
|
| 343 | data->temp_raw = sign_extend32(raw, 23);
|
| 344 |
|
| 345 | return 0;
|
| 346 | }
|
| 347 |
|
| 348 | static int zts8032_read_temp_raw(struct zts8032_data *data)
|
| 349 | {
|
| 350 | int rc;
|
| 351 | int rate;
|
| 352 | int ready;
|
| 353 | int timeout;
|
| 354 |
|
| 355 | if (mutex_lock_interruptible(&data->lock))
|
| 356 | return -EINTR;
|
| 357 |
|
| 358 | rate = zts8032_get_temp_samp_freq(data);
|
| 359 | timeout = ZTS8032_POLL_TIMEOUT_US(rate);
|
| 360 |
|
| 361 | /* Poll for sensor readiness; base the timeout upon the sample rate. */
|
| 362 | rc = regmap_read_poll_timeout(data->regmap, ZTS8032_MEAS_CFG, ready,
|
| 363 | ready & ZTS8032_TMP_RDY,
|
| 364 | ZTS8032_POLL_SLEEP_US(timeout), timeout);
|
| 365 | if (rc < 0)
|
| 366 | goto done;
|
| 367 |
|
| 368 | rc = zts8032_read_temp_ready(data);
|
| 369 |
|
| 370 | done:
|
| 371 | mutex_unlock(&data->lock);
|
| 372 | return rc;
|
| 373 | }
|
| 374 |
|
| 375 | static bool zts8032_is_writeable_reg(struct device *dev, unsigned int reg)
|
| 376 | {
|
| 377 | switch (reg) {
|
| 378 | case ZTS8032_PRS_CFG:
|
| 379 | case ZTS8032_TMP_CFG:
|
| 380 | case ZTS8032_MEAS_CFG:
|
| 381 | case ZTS8032_CFG_REG:
|
| 382 | case ZTS8032_RESET:
|
| 383 | /* No documentation available on the registers below */
|
| 384 | case 0x0e:
|
| 385 | case 0x0f:
|
| 386 | case 0x62:
|
| 387 | return true;
|
| 388 | default:
|
| 389 | return false;
|
| 390 | }
|
| 391 | }
|
| 392 |
|
| 393 | static bool zts8032_is_volatile_reg(struct device *dev, unsigned int reg)
|
| 394 | {
|
| 395 | switch (reg) {
|
| 396 | case ZTS8032_PRS_B0:
|
| 397 | case ZTS8032_PRS_B1:
|
| 398 | case ZTS8032_PRS_B2:
|
| 399 | case ZTS8032_TMP_B0:
|
| 400 | case ZTS8032_TMP_B1:
|
| 401 | case ZTS8032_TMP_B2:
|
| 402 | case ZTS8032_MEAS_CFG:
|
| 403 | case 0x32: /* No documentation available on this register */
|
| 404 | return true;
|
| 405 | default:
|
| 406 | return false;
|
| 407 | }
|
| 408 | }
|
| 409 |
|
| 410 | static int zts8032_write_raw(struct iio_dev *iio,
|
| 411 | struct iio_chan_spec const *chan, int val,
|
| 412 | int val2, long mask)
|
| 413 | {
|
| 414 | int rc;
|
| 415 | struct zts8032_data *data = iio_priv(iio);
|
| 416 |
|
| 417 | if (mutex_lock_interruptible(&data->lock))
|
| 418 | return -EINTR;
|
| 419 |
|
| 420 | switch (mask) {
|
| 421 | case IIO_CHAN_INFO_SAMP_FREQ:
|
| 422 | switch (chan->type) {
|
| 423 | case IIO_PRESSURE:
|
| 424 | rc = zts8032_set_pres_samp_freq(data, val);
|
| 425 | break;
|
| 426 |
|
| 427 | case IIO_TEMP:
|
| 428 | rc = zts8032_set_temp_samp_freq(data, val);
|
| 429 | break;
|
| 430 |
|
| 431 | default:
|
| 432 | rc = -EINVAL;
|
| 433 | break;
|
| 434 | }
|
| 435 | break;
|
| 436 |
|
| 437 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
|
| 438 | switch (chan->type) {
|
| 439 | case IIO_PRESSURE:
|
| 440 | rc = zts8032_set_pres_precision(data, val);
|
| 441 | break;
|
| 442 |
|
| 443 | case IIO_TEMP:
|
| 444 | rc = zts8032_set_temp_precision(data, val);
|
| 445 | break;
|
| 446 |
|
| 447 | default:
|
| 448 | rc = -EINVAL;
|
| 449 | break;
|
| 450 | }
|
| 451 | break;
|
| 452 |
|
| 453 | default:
|
| 454 | rc = -EINVAL;
|
| 455 | break;
|
| 456 | }
|
| 457 |
|
| 458 | mutex_unlock(&data->lock);
|
| 459 | return rc;
|
| 460 | }
|
| 461 |
|
| 462 | static int zts8032_calculate_pressure(struct zts8032_data *data)
|
| 463 | {
|
| 464 | int i;
|
| 465 | int rc;
|
| 466 | int t_ready;
|
| 467 | int kpi = zts8032_get_pres_k(data);
|
| 468 | int kti = zts8032_get_temp_k(data);
|
| 469 | s64 rem = 0ULL;
|
| 470 | s64 pressure = 0ULL;
|
| 471 | s64 p;
|
| 472 | s64 t;
|
| 473 | s64 denoms[9];
|
| 474 | s64 nums[9];
|
| 475 | s64 rems[9];
|
| 476 | s64 kp;
|
| 477 | s64 kt;
|
| 478 |
|
| 479 | if (kpi < 0)
|
| 480 | return kpi;
|
| 481 |
|
| 482 | if (kti < 0)
|
| 483 | return kti;
|
| 484 |
|
| 485 | kp = (s64)kpi;
|
| 486 | kt = (s64)kti;
|
| 487 |
|
| 488 | /* Refresh temp if it's ready, otherwise just use the latest value */
|
| 489 | if (mutex_trylock(&data->lock)) {
|
| 490 | rc = regmap_read(data->regmap, ZTS8032_MEAS_CFG, &t_ready);
|
| 491 | if (rc >= 0 && t_ready & ZTS8032_TMP_RDY)
|
| 492 | zts8032_read_temp_ready(data);
|
| 493 |
|
| 494 | mutex_unlock(&data->lock);
|
| 495 | }
|
| 496 |
|
| 497 | p = (s64)data->pressure_raw;
|
| 498 | t = (s64)data->temp_raw;
|
| 499 |
|
| 500 | /* Section 4.9.1 of the ZTS8032 spec; algebra'd to avoid underflow */
|
| 501 | nums[0] = (s64)data->c00;
|
| 502 | denoms[0] = 1LL;
|
| 503 | nums[1] = p * (s64)data->c10;
|
| 504 | denoms[1] = kp;
|
| 505 | nums[2] = p * p * (s64)data->c20;
|
| 506 | denoms[2] = kp * kp;
|
| 507 | nums[3] = p * p * p * (s64)data->c30;
|
| 508 | denoms[3] = kp * kp * kp;
|
| 509 | nums[4] = p * p * p * p * (s64)data->c40;
|
| 510 | denoms[4] = kp * kp * kp * kp;
|
| 511 | nums[5] = t * (s64)data->c01;
|
| 512 | denoms[5] = kt;
|
| 513 | nums[6] = t * p * (s64)data->c11;
|
| 514 | denoms[6] = kp * kt;
|
| 515 | nums[7] = t * p * p * (s64)data->c21;
|
| 516 | denoms[7] = kp * kp * kt;
|
| 517 | nums[8] = t * p * p * p * (s64)data->c31;
|
| 518 | denoms[8] = kp * kp * kp * kt;
|
| 519 |
|
| 520 | /* Kernel lacks a div64_s64_rem function; denoms are all positive */
|
| 521 | for (i = 0; i < 9; ++i) {
|
| 522 | u64 irem;
|
| 523 |
|
| 524 | if (nums[i] < 0LL) {
|
| 525 | pressure -= div64_u64_rem(-nums[i], denoms[i], &irem);
|
| 526 | rems[i] = -irem;
|
| 527 | } else {
|
| 528 | pressure += div64_u64_rem(nums[i], denoms[i], &irem);
|
| 529 | rems[i] = (s64)irem;
|
| 530 | }
|
| 531 | }
|
| 532 |
|
| 533 | /* Increase precision and calculate the remainder sum */
|
| 534 | for (i = 0; i < 9; ++i)
|
| 535 | rem += div64_s64((s64)rems[i] * 1000000000LL, denoms[i]);
|
| 536 |
|
| 537 | pressure += div_s64(rem, 1000000000LL);
|
| 538 | if (pressure < 0LL)
|
| 539 | return -ERANGE;
|
| 540 |
|
| 541 | return (int)min_t(s64, pressure, INT_MAX);
|
| 542 | }
|
| 543 |
|
| 544 | static int zts8032_read_pressure(struct zts8032_data *data, int *val, int *val2,
|
| 545 | long mask)
|
| 546 | {
|
| 547 | int rc;
|
| 548 |
|
| 549 | switch (mask) {
|
| 550 | case IIO_CHAN_INFO_SAMP_FREQ:
|
| 551 | rc = zts8032_get_pres_samp_freq(data);
|
| 552 | if (rc < 0)
|
| 553 | return rc;
|
| 554 |
|
| 555 | *val = rc;
|
| 556 | return IIO_VAL_INT;
|
| 557 |
|
| 558 | case IIO_CHAN_INFO_PROCESSED:
|
| 559 | rc = zts8032_read_pres_raw(data);
|
| 560 | if (rc)
|
| 561 | return rc;
|
| 562 |
|
| 563 | rc = zts8032_calculate_pressure(data);
|
| 564 | if (rc < 0)
|
| 565 | return rc;
|
| 566 |
|
| 567 | *val = rc;
|
| 568 | *val2 = 1000; /* Convert Pa to KPa per IIO ABI */
|
| 569 | return IIO_VAL_FRACTIONAL;
|
| 570 |
|
| 571 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
|
| 572 | rc = zts8032_get_pres_precision(data);
|
| 573 | if (rc < 0)
|
| 574 | return rc;
|
| 575 |
|
| 576 | *val = rc;
|
| 577 | return IIO_VAL_INT;
|
| 578 |
|
| 579 | default:
|
| 580 | return -EINVAL;
|
| 581 | }
|
| 582 | }
|
| 583 |
|
| 584 | static int zts8032_calculate_temp(struct zts8032_data *data)
|
| 585 | {
|
| 586 | s64 c0;
|
| 587 | s64 t;
|
| 588 | int kt = zts8032_get_temp_k(data);
|
| 589 |
|
| 590 | if (kt < 0)
|
| 591 | return kt;
|
| 592 |
|
| 593 | /* Obtain inverse-scaled offset */
|
| 594 | c0 = div_s64((s64)kt * (s64)data->c0, 2);
|
| 595 |
|
| 596 | /* Add the offset to the unscaled temperature */
|
| 597 | t = c0 + ((s64)data->temp_raw * (s64)data->c1);
|
| 598 |
|
| 599 | /* Convert to milliCelsius and scale the temperature */
|
| 600 | return (int)div_s64(t * 1000LL, kt);
|
| 601 | }
|
| 602 |
|
| 603 | static int zts8032_read_temp(struct zts8032_data *data, int *val, int *val2,
|
| 604 | long mask)
|
| 605 | {
|
| 606 | int rc;
|
| 607 |
|
| 608 | switch (mask) {
|
| 609 | case IIO_CHAN_INFO_SAMP_FREQ:
|
| 610 | rc = zts8032_get_temp_samp_freq(data);
|
| 611 | if (rc < 0)
|
| 612 | return rc;
|
| 613 |
|
| 614 | *val = rc;
|
| 615 | return IIO_VAL_INT;
|
| 616 |
|
| 617 | case IIO_CHAN_INFO_PROCESSED:
|
| 618 | rc = zts8032_read_temp_raw(data);
|
| 619 | if (rc)
|
| 620 | return rc;
|
| 621 |
|
| 622 | rc = zts8032_calculate_temp(data);
|
| 623 | if (rc < 0)
|
| 624 | return rc;
|
| 625 |
|
| 626 | *val = rc;
|
| 627 | return IIO_VAL_INT;
|
| 628 |
|
| 629 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
|
| 630 | rc = zts8032_get_temp_precision(data);
|
| 631 | if (rc < 0)
|
| 632 | return rc;
|
| 633 |
|
| 634 | *val = rc;
|
| 635 | return IIO_VAL_INT;
|
| 636 |
|
| 637 | default:
|
| 638 | return -EINVAL;
|
| 639 | }
|
| 640 | }
|
| 641 |
|
| 642 | static int zts8032_read_raw(struct iio_dev *iio,
|
| 643 | struct iio_chan_spec const *chan,
|
| 644 | int *val, int *val2, long mask)
|
| 645 | {
|
| 646 | struct zts8032_data *data = iio_priv(iio);
|
| 647 |
|
| 648 | switch (chan->type) {
|
| 649 | case IIO_PRESSURE:
|
| 650 | return zts8032_read_pressure(data, val, val2, mask);
|
| 651 |
|
| 652 | case IIO_TEMP:
|
| 653 | return zts8032_read_temp(data, val, val2, mask);
|
| 654 |
|
| 655 | default:
|
| 656 | return -EINVAL;
|
| 657 | }
|
| 658 | }
|
| 659 |
|
| 660 | static void zts8032_reset(void *action_data)
|
| 661 | {
|
| 662 | struct zts8032_data *data = action_data;
|
| 663 |
|
| 664 | regmap_write(data->regmap, ZTS8032_RESET, ZTS8032_RESET_MAGIC);
|
| 665 | }
|
| 666 |
|
| 667 | static const struct regmap_config zts8032_regmap_config = {
|
| 668 | .reg_bits = 8,
|
| 669 | .val_bits = 8,
|
| 670 | .writeable_reg = zts8032_is_writeable_reg,
|
| 671 | .volatile_reg = zts8032_is_volatile_reg,
|
| 672 | .cache_type = REGCACHE_RBTREE,
|
| 673 | .max_register = 0x62, /* No documentation available on this register */
|
| 674 | };
|
| 675 |
|
| 676 | static const struct iio_info zts8032_info = {
|
| 677 | .read_raw = zts8032_read_raw,
|
| 678 | .write_raw = zts8032_write_raw,
|
| 679 | };
|
| 680 |
|
| 681 | /*
|
| 682 | * Some verions of chip will read temperatures in the ~60C range when
|
| 683 | * its actually ~20C. This is the manufacturer recommended workaround
|
| 684 | * to correct the issue. The registers used below are undocumented.
|
| 685 | */
|
| 686 | static int zts8032_temp_workaround(struct zts8032_data *data)
|
| 687 | {
|
| 688 | int rc;
|
| 689 | int reg;
|
| 690 |
|
| 691 | rc = regmap_read(data->regmap, 0x32, ®);
|
| 692 | if (rc < 0)
|
| 693 | return rc;
|
| 694 |
|
| 695 | /*
|
| 696 | * If bit 1 is set then the device is okay, and the workaround does not
|
| 697 | * need to be applied
|
| 698 | */
|
| 699 | if (reg & BIT(1))
|
| 700 | return 0;
|
| 701 |
|
| 702 | rc = regmap_write(data->regmap, 0x0e, 0xA5);
|
| 703 | if (rc < 0)
|
| 704 | return rc;
|
| 705 |
|
| 706 | rc = regmap_write(data->regmap, 0x0f, 0x96);
|
| 707 | if (rc < 0)
|
| 708 | return rc;
|
| 709 |
|
| 710 | rc = regmap_write(data->regmap, 0x62, 0x02);
|
| 711 | if (rc < 0)
|
| 712 | return rc;
|
| 713 |
|
| 714 | rc = regmap_write(data->regmap, 0x0e, 0x00);
|
| 715 | if (rc < 0)
|
| 716 | return rc;
|
| 717 |
|
| 718 | return regmap_write(data->regmap, 0x0f, 0x00);
|
| 719 | }
|
| 720 |
|
| 721 | static int zts8032_probe(struct i2c_client *client,
|
| 722 | const struct i2c_device_id *id)
|
| 723 | {
|
| 724 | struct zts8032_data *data;
|
| 725 | struct iio_dev *iio;
|
| 726 | int rc, ready;
|
| 727 |
|
| 728 | iio = devm_iio_device_alloc(&client->dev, sizeof(*data));
|
| 729 | if (!iio)
|
| 730 | return -ENOMEM;
|
| 731 |
|
| 732 | data = iio_priv(iio);
|
| 733 | data->client = client;
|
| 734 | mutex_init(&data->lock);
|
| 735 |
|
| 736 | iio->name = id->name;
|
| 737 | iio->channels = zts8032_channels;
|
| 738 | iio->num_channels = ARRAY_SIZE(zts8032_channels);
|
| 739 | iio->info = &zts8032_info;
|
| 740 | iio->modes = INDIO_DIRECT_MODE;
|
| 741 |
|
| 742 | data->regmap = devm_regmap_init_i2c(client, &zts8032_regmap_config);
|
| 743 | if (IS_ERR(data->regmap))
|
| 744 | return PTR_ERR(data->regmap);
|
| 745 |
|
| 746 | /* Register to run the device reset when the device is removed */
|
| 747 | rc = devm_add_action_or_reset(&client->dev, zts8032_reset, data);
|
| 748 | if (rc)
|
| 749 | return rc;
|
| 750 |
|
| 751 | /*
|
| 752 | * Set up pressure sensor in single sample, one measurement per second
|
| 753 | * mode
|
| 754 | */
|
| 755 | rc = regmap_write(data->regmap, ZTS8032_PRS_CFG, 0);
|
| 756 |
|
| 757 | /*
|
| 758 | * Set up external (MEMS) temperature sensor in single sample, one
|
| 759 | * measurement per second mode
|
| 760 | */
|
| 761 | rc = regmap_write(data->regmap, ZTS8032_TMP_CFG, ZTS8032_TMP_EXT);
|
| 762 | if (rc < 0)
|
| 763 | return rc;
|
| 764 |
|
| 765 | /* Temp and pressure shifts are disabled when PRC <= 8 */
|
| 766 | rc = regmap_write_bits(data->regmap, ZTS8032_CFG_REG,
|
| 767 | ZTS8032_PRS_SHIFT_EN | ZTS8032_TMP_SHIFT_EN, 0);
|
| 768 | if (rc < 0)
|
| 769 | return rc;
|
| 770 |
|
| 771 | /* MEAS_CFG doesn't update correctly unless first written with 0 */
|
| 772 | rc = regmap_write_bits(data->regmap, ZTS8032_MEAS_CFG,
|
| 773 | ZTS8032_MEAS_CTRL_BITS, 0);
|
| 774 | if (rc < 0)
|
| 775 | return rc;
|
| 776 |
|
| 777 | /* Turn on temperature and pressure measurement in the background */
|
| 778 | rc = regmap_write_bits(data->regmap, ZTS8032_MEAS_CFG,
|
| 779 | ZTS8032_MEAS_CTRL_BITS, ZTS8032_PRS_EN |
|
| 780 | ZTS8032_TEMP_EN | ZTS8032_BACKGROUND);
|
| 781 | if (rc < 0)
|
| 782 | return rc;
|
| 783 |
|
| 784 | /*
|
| 785 | * Calibration coefficients required for reporting temperature.
|
| 786 | * They are available 40ms after the device has started
|
| 787 | */
|
| 788 | rc = regmap_read_poll_timeout(data->regmap, ZTS8032_MEAS_CFG, ready,
|
| 789 | ready & ZTS8032_COEF_RDY, 10000, 40000);
|
| 790 | if (rc < 0)
|
| 791 | return rc;
|
| 792 |
|
| 793 | rc = zts8032_get_coefs(data);
|
| 794 | if (rc < 0)
|
| 795 | return rc;
|
| 796 |
|
| 797 | rc = zts8032_temp_workaround(data);
|
| 798 | if (rc < 0)
|
| 799 | return rc;
|
| 800 |
|
| 801 | rc = devm_iio_device_register(&client->dev, iio);
|
| 802 | if (rc)
|
| 803 | return rc;
|
| 804 |
|
| 805 | i2c_set_clientdata(client, iio);
|
| 806 |
|
| 807 | return 0;
|
| 808 | }
|
| 809 |
|
| 810 | static int zts8032_remove(struct i2c_client *client)
|
| 811 | {
|
| 812 | return 0;
|
| 813 | }
|
| 814 |
|
| 815 | static const struct i2c_device_id zts8032_id[] = {
|
| 816 | { ZTS8032_DEV_NAME, 0 },
|
| 817 | {}
|
| 818 | };
|
| 819 | MODULE_DEVICE_TABLE(i2c, zts8032_id);
|
| 820 |
|
| 821 | static const struct acpi_device_id zts8032_acpi_match[] = {
|
| 822 | { "ZTS8032" },
|
| 823 | {}
|
| 824 | };
|
| 825 | MODULE_DEVICE_TABLE(acpi, zts8032_acpi_match);
|
| 826 |
|
| 827 | static const struct of_device_id zts8032_of_match[] = {
|
| 828 | { .compatible = "zilltek,zts8032", },
|
| 829 | { }
|
| 830 | };
|
| 831 | MODULE_DEVICE_TABLE(of, zts8032_of_match);
|
| 832 |
|
| 833 | static struct i2c_driver zts8032_driver = {
|
| 834 | .driver = {
|
| 835 | .name = ZTS8032_DEV_NAME,
|
| 836 | .acpi_match_table = zts8032_acpi_match,
|
| 837 | .of_match_table = zts8032_of_match,
|
| 838 | },
|
| 839 | .probe = zts8032_probe,
|
| 840 | .remove = zts8032_remove,
|
| 841 | .id_table = zts8032_id,
|
| 842 | };
|
| 843 | module_i2c_driver(zts8032_driver);
|
| 844 |
|
| 845 | MODULE_AUTHOR("Knight Kuo <knight@zilltek.com>");
|
| 846 | MODULE_DESCRIPTION("Zilltek ZTS8032 pressure and temperature sensor");
|
| 847 | MODULE_LICENSE("GPL v2");
|