1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * mlx90632.c - Melexis MLX90632 contactless IR temperature sensor 4 * 5 * Copyright (c) 2017 Melexis <cmo@melexis.com> 6 * 7 * Driver for the Melexis MLX90632 I2C 16-bit IR thermopile sensor 8 */ 9 #include <linux/delay.h> 10 #include <linux/err.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/i2c.h> 13 #include <linux/iopoll.h> 14 #include <linux/kernel.h> 15 #include <linux/limits.h> 16 #include <linux/mod_devicetable.h> 17 #include <linux/module.h> 18 #include <linux/math64.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/regmap.h> 21 #include <linux/regulator/consumer.h> 22 23 #include <linux/iio/iio.h> 24 #include <linux/iio/sysfs.h> 25 26 /* Memory sections addresses */ 27 #define MLX90632_ADDR_RAM 0x4000 /* Start address of ram */ 28 #define MLX90632_ADDR_EEPROM 0x2480 /* Start address of user eeprom */ 29 30 /* EEPROM addresses - used at startup */ 31 #define MLX90632_EE_CTRL 0x24d4 /* Control register initial value */ 32 #define MLX90632_EE_I2C_ADDR 0x24d5 /* I2C address register initial value */ 33 #define MLX90632_EE_VERSION 0x240b /* EEPROM version reg address */ 34 #define MLX90632_EE_P_R 0x240c /* P_R calibration register 32bit */ 35 #define MLX90632_EE_P_G 0x240e /* P_G calibration register 32bit */ 36 #define MLX90632_EE_P_T 0x2410 /* P_T calibration register 32bit */ 37 #define MLX90632_EE_P_O 0x2412 /* P_O calibration register 32bit */ 38 #define MLX90632_EE_Aa 0x2414 /* Aa calibration register 32bit */ 39 #define MLX90632_EE_Ab 0x2416 /* Ab calibration register 32bit */ 40 #define MLX90632_EE_Ba 0x2418 /* Ba calibration register 32bit */ 41 #define MLX90632_EE_Bb 0x241a /* Bb calibration register 32bit */ 42 #define MLX90632_EE_Ca 0x241c /* Ca calibration register 32bit */ 43 #define MLX90632_EE_Cb 0x241e /* Cb calibration register 32bit */ 44 #define MLX90632_EE_Da 0x2420 /* Da calibration register 32bit */ 45 #define MLX90632_EE_Db 0x2422 /* Db calibration register 32bit */ 46 #define MLX90632_EE_Ea 0x2424 /* Ea calibration register 32bit */ 47 #define MLX90632_EE_Eb 0x2426 /* Eb calibration register 32bit */ 48 #define MLX90632_EE_Fa 0x2428 /* Fa calibration register 32bit */ 49 #define MLX90632_EE_Fb 0x242a /* Fb calibration register 32bit */ 50 #define MLX90632_EE_Ga 0x242c /* Ga calibration register 32bit */ 51 52 #define MLX90632_EE_Gb 0x242e /* Gb calibration register 16bit */ 53 #define MLX90632_EE_Ka 0x242f /* Ka calibration register 16bit */ 54 55 #define MLX90632_EE_Ha 0x2481 /* Ha customer calib value reg 16bit */ 56 #define MLX90632_EE_Hb 0x2482 /* Hb customer calib value reg 16bit */ 57 58 /* Register addresses - volatile */ 59 #define MLX90632_REG_I2C_ADDR 0x3000 /* Chip I2C address register */ 60 61 /* Control register address - volatile */ 62 #define MLX90632_REG_CONTROL 0x3001 /* Control Register address */ 63 #define MLX90632_CFG_PWR_MASK GENMASK(2, 1) /* PowerMode Mask */ 64 #define MLX90632_CFG_MTYP_MASK GENMASK(8, 4) /* Meas select Mask */ 65 66 /* PowerModes statuses */ 67 #define MLX90632_PWR_STATUS(ctrl_val) (ctrl_val << 1) 68 #define MLX90632_PWR_STATUS_HALT MLX90632_PWR_STATUS(0) /* hold */ 69 #define MLX90632_PWR_STATUS_SLEEP_STEP MLX90632_PWR_STATUS(1) /* sleep step*/ 70 #define MLX90632_PWR_STATUS_STEP MLX90632_PWR_STATUS(2) /* step */ 71 #define MLX90632_PWR_STATUS_CONTINUOUS MLX90632_PWR_STATUS(3) /* continuous*/ 72 73 /* Measurement types */ 74 #define MLX90632_MTYP_MEDICAL 0 75 #define MLX90632_MTYP_EXTENDED 17 76 77 /* Measurement type select*/ 78 #define MLX90632_MTYP_STATUS(ctrl_val) (ctrl_val << 4) 79 #define MLX90632_MTYP_STATUS_MEDICAL MLX90632_MTYP_STATUS(MLX90632_MTYP_MEDICAL) 80 #define MLX90632_MTYP_STATUS_EXTENDED MLX90632_MTYP_STATUS(MLX90632_MTYP_EXTENDED) 81 82 /* I2C command register - volatile */ 83 #define MLX90632_REG_I2C_CMD 0x3005 /* I2C command Register address */ 84 85 /* Device status register - volatile */ 86 #define MLX90632_REG_STATUS 0x3fff /* Device status register */ 87 #define MLX90632_STAT_BUSY BIT(10) /* Device busy indicator */ 88 #define MLX90632_STAT_EE_BUSY BIT(9) /* EEPROM busy indicator */ 89 #define MLX90632_STAT_BRST BIT(8) /* Brown out reset indicator */ 90 #define MLX90632_STAT_CYCLE_POS GENMASK(6, 2) /* Data position */ 91 #define MLX90632_STAT_DATA_RDY BIT(0) /* Data ready indicator */ 92 93 /* RAM_MEAS address-es for each channel */ 94 #define MLX90632_RAM_1(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num) 95 #define MLX90632_RAM_2(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 1) 96 #define MLX90632_RAM_3(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 2) 97 98 /* Name important RAM_MEAS channels */ 99 #define MLX90632_RAM_DSP5_EXTENDED_AMBIENT_1 MLX90632_RAM_3(17) 100 #define MLX90632_RAM_DSP5_EXTENDED_AMBIENT_2 MLX90632_RAM_3(18) 101 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_1 MLX90632_RAM_1(17) 102 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_2 MLX90632_RAM_2(17) 103 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_3 MLX90632_RAM_1(18) 104 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_4 MLX90632_RAM_2(18) 105 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_5 MLX90632_RAM_1(19) 106 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_6 MLX90632_RAM_2(19) 107 108 /* Magic constants */ 109 #define MLX90632_ID_MEDICAL 0x0105 /* EEPROM DSPv5 Medical device id */ 110 #define MLX90632_ID_CONSUMER 0x0205 /* EEPROM DSPv5 Consumer device id */ 111 #define MLX90632_ID_EXTENDED 0x0505 /* EEPROM DSPv5 Extended range device id */ 112 #define MLX90632_ID_MASK GENMASK(14, 0) /* DSP version and device ID in EE_VERSION */ 113 #define MLX90632_DSP_VERSION 5 /* DSP version */ 114 #define MLX90632_DSP_MASK GENMASK(7, 0) /* DSP version in EE_VERSION */ 115 #define MLX90632_RESET_CMD 0x0006 /* Reset sensor (address or global) */ 116 #define MLX90632_REF_12 12LL /* ResCtrlRef value of Ch 1 or Ch 2 */ 117 #define MLX90632_REF_3 12LL /* ResCtrlRef value of Channel 3 */ 118 #define MLX90632_MAX_MEAS_NUM 31 /* Maximum measurements in list */ 119 #define MLX90632_SLEEP_DELAY_MS 3000 /* Autosleep delay */ 120 #define MLX90632_EXTENDED_LIMIT 27000 /* Extended mode raw value limit */ 121 122 /** 123 * struct mlx90632_data - private data for the MLX90632 device 124 * @client: I2C client of the device 125 * @lock: Internal mutex for multiple reads for single measurement 126 * @regmap: Regmap of the device 127 * @emissivity: Object emissivity from 0 to 1000 where 1000 = 1. 128 * @mtyp: Measurement type physical sensor configuration for extended range 129 * calculations 130 * @object_ambient_temperature: Ambient temperature at object (might differ of 131 * the ambient temperature of sensor. 132 * @regulator: Regulator of the device 133 */ 134 struct mlx90632_data { 135 struct i2c_client *client; 136 struct mutex lock; 137 struct regmap *regmap; 138 u16 emissivity; 139 u8 mtyp; 140 u32 object_ambient_temperature; 141 struct regulator *regulator; 142 }; 143 144 static const struct regmap_range mlx90632_volatile_reg_range[] = { 145 regmap_reg_range(MLX90632_REG_I2C_ADDR, MLX90632_REG_CONTROL), 146 regmap_reg_range(MLX90632_REG_I2C_CMD, MLX90632_REG_I2C_CMD), 147 regmap_reg_range(MLX90632_REG_STATUS, MLX90632_REG_STATUS), 148 regmap_reg_range(MLX90632_RAM_1(0), 149 MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), 150 }; 151 152 static const struct regmap_access_table mlx90632_volatile_regs_tbl = { 153 .yes_ranges = mlx90632_volatile_reg_range, 154 .n_yes_ranges = ARRAY_SIZE(mlx90632_volatile_reg_range), 155 }; 156 157 static const struct regmap_range mlx90632_read_reg_range[] = { 158 regmap_reg_range(MLX90632_EE_VERSION, MLX90632_EE_Ka), 159 regmap_reg_range(MLX90632_EE_CTRL, MLX90632_EE_I2C_ADDR), 160 regmap_reg_range(MLX90632_EE_Ha, MLX90632_EE_Hb), 161 regmap_reg_range(MLX90632_REG_I2C_ADDR, MLX90632_REG_CONTROL), 162 regmap_reg_range(MLX90632_REG_I2C_CMD, MLX90632_REG_I2C_CMD), 163 regmap_reg_range(MLX90632_REG_STATUS, MLX90632_REG_STATUS), 164 regmap_reg_range(MLX90632_RAM_1(0), 165 MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), 166 }; 167 168 static const struct regmap_access_table mlx90632_readable_regs_tbl = { 169 .yes_ranges = mlx90632_read_reg_range, 170 .n_yes_ranges = ARRAY_SIZE(mlx90632_read_reg_range), 171 }; 172 173 static const struct regmap_range mlx90632_no_write_reg_range[] = { 174 regmap_reg_range(MLX90632_EE_VERSION, MLX90632_EE_Ka), 175 regmap_reg_range(MLX90632_RAM_1(0), 176 MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), 177 }; 178 179 static const struct regmap_access_table mlx90632_writeable_regs_tbl = { 180 .no_ranges = mlx90632_no_write_reg_range, 181 .n_no_ranges = ARRAY_SIZE(mlx90632_no_write_reg_range), 182 }; 183 184 static const struct regmap_config mlx90632_regmap = { 185 .reg_bits = 16, 186 .val_bits = 16, 187 188 .volatile_table = &mlx90632_volatile_regs_tbl, 189 .rd_table = &mlx90632_readable_regs_tbl, 190 .wr_table = &mlx90632_writeable_regs_tbl, 191 192 .use_single_read = true, 193 .use_single_write = true, 194 .reg_format_endian = REGMAP_ENDIAN_BIG, 195 .val_format_endian = REGMAP_ENDIAN_BIG, 196 .cache_type = REGCACHE_RBTREE, 197 }; 198 199 static s32 mlx90632_pwr_set_sleep_step(struct regmap *regmap) 200 { 201 return regmap_update_bits(regmap, MLX90632_REG_CONTROL, 202 MLX90632_CFG_PWR_MASK, 203 MLX90632_PWR_STATUS_SLEEP_STEP); 204 } 205 206 static s32 mlx90632_pwr_continuous(struct regmap *regmap) 207 { 208 return regmap_update_bits(regmap, MLX90632_REG_CONTROL, 209 MLX90632_CFG_PWR_MASK, 210 MLX90632_PWR_STATUS_CONTINUOUS); 211 } 212 213 /** 214 * mlx90632_reset_delay() - Give the mlx90632 some time to reset properly 215 * If this is not done, the following I2C command(s) will not be accepted. 216 */ 217 static void mlx90632_reset_delay(void) 218 { 219 usleep_range(150, 200); 220 } 221 222 /** 223 * mlx90632_perform_measurement() - Trigger and retrieve current measurement cycle 224 * @data: pointer to mlx90632_data object containing regmap information 225 * 226 * Perform a measurement and return latest measurement cycle position reported 227 * by sensor. This is a blocking function for 500ms, as that is default sensor 228 * refresh rate. 229 */ 230 static int mlx90632_perform_measurement(struct mlx90632_data *data) 231 { 232 unsigned int reg_status; 233 int ret; 234 235 ret = regmap_update_bits(data->regmap, MLX90632_REG_STATUS, 236 MLX90632_STAT_DATA_RDY, 0); 237 if (ret < 0) 238 return ret; 239 240 ret = regmap_read_poll_timeout(data->regmap, MLX90632_REG_STATUS, reg_status, 241 !(reg_status & MLX90632_STAT_DATA_RDY), 10000, 242 100 * 10000); 243 244 if (ret < 0) { 245 dev_err(&data->client->dev, "data not ready"); 246 return -ETIMEDOUT; 247 } 248 249 return (reg_status & MLX90632_STAT_CYCLE_POS) >> 2; 250 } 251 252 static int mlx90632_set_meas_type(struct regmap *regmap, u8 type) 253 { 254 int ret; 255 256 if ((type != MLX90632_MTYP_MEDICAL) && (type != MLX90632_MTYP_EXTENDED)) 257 return -EINVAL; 258 259 ret = regmap_write(regmap, MLX90632_REG_I2C_CMD, MLX90632_RESET_CMD); 260 if (ret < 0) 261 return ret; 262 263 mlx90632_reset_delay(); 264 265 ret = regmap_write_bits(regmap, MLX90632_REG_CONTROL, 266 (MLX90632_CFG_MTYP_MASK | MLX90632_CFG_PWR_MASK), 267 (MLX90632_MTYP_STATUS(type) | MLX90632_PWR_STATUS_HALT)); 268 if (ret < 0) 269 return ret; 270 271 return mlx90632_pwr_continuous(regmap); 272 } 273 274 static int mlx90632_channel_new_select(int perform_ret, uint8_t *channel_new, 275 uint8_t *channel_old) 276 { 277 switch (perform_ret) { 278 case 1: 279 *channel_new = 1; 280 *channel_old = 2; 281 break; 282 case 2: 283 *channel_new = 2; 284 *channel_old = 1; 285 break; 286 default: 287 return -EINVAL; 288 } 289 290 return 0; 291 } 292 293 static int mlx90632_read_ambient_raw(struct regmap *regmap, 294 s16 *ambient_new_raw, s16 *ambient_old_raw) 295 { 296 int ret; 297 unsigned int read_tmp; 298 299 ret = regmap_read(regmap, MLX90632_RAM_3(1), &read_tmp); 300 if (ret < 0) 301 return ret; 302 *ambient_new_raw = (s16)read_tmp; 303 304 ret = regmap_read(regmap, MLX90632_RAM_3(2), &read_tmp); 305 if (ret < 0) 306 return ret; 307 *ambient_old_raw = (s16)read_tmp; 308 309 return ret; 310 } 311 312 static int mlx90632_read_object_raw(struct regmap *regmap, 313 int perform_measurement_ret, 314 s16 *object_new_raw, s16 *object_old_raw) 315 { 316 int ret; 317 unsigned int read_tmp; 318 s16 read; 319 u8 channel = 0; 320 u8 channel_old = 0; 321 322 ret = mlx90632_channel_new_select(perform_measurement_ret, &channel, 323 &channel_old); 324 if (ret != 0) 325 return ret; 326 327 ret = regmap_read(regmap, MLX90632_RAM_2(channel), &read_tmp); 328 if (ret < 0) 329 return ret; 330 331 read = (s16)read_tmp; 332 333 ret = regmap_read(regmap, MLX90632_RAM_1(channel), &read_tmp); 334 if (ret < 0) 335 return ret; 336 *object_new_raw = (read + (s16)read_tmp) / 2; 337 338 ret = regmap_read(regmap, MLX90632_RAM_2(channel_old), &read_tmp); 339 if (ret < 0) 340 return ret; 341 read = (s16)read_tmp; 342 343 ret = regmap_read(regmap, MLX90632_RAM_1(channel_old), &read_tmp); 344 if (ret < 0) 345 return ret; 346 *object_old_raw = (read + (s16)read_tmp) / 2; 347 348 return ret; 349 } 350 351 static int mlx90632_read_all_channel(struct mlx90632_data *data, 352 s16 *ambient_new_raw, s16 *ambient_old_raw, 353 s16 *object_new_raw, s16 *object_old_raw) 354 { 355 s32 ret, measurement; 356 357 mutex_lock(&data->lock); 358 measurement = mlx90632_perform_measurement(data); 359 if (measurement < 0) { 360 ret = measurement; 361 goto read_unlock; 362 } 363 ret = mlx90632_read_ambient_raw(data->regmap, ambient_new_raw, 364 ambient_old_raw); 365 if (ret < 0) 366 goto read_unlock; 367 368 ret = mlx90632_read_object_raw(data->regmap, measurement, 369 object_new_raw, object_old_raw); 370 read_unlock: 371 mutex_unlock(&data->lock); 372 return ret; 373 } 374 375 static int mlx90632_read_ambient_raw_extended(struct regmap *regmap, 376 s16 *ambient_new_raw, s16 *ambient_old_raw) 377 { 378 unsigned int read_tmp; 379 int ret; 380 381 ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_AMBIENT_1, &read_tmp); 382 if (ret < 0) 383 return ret; 384 *ambient_new_raw = (s16)read_tmp; 385 386 ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_AMBIENT_2, &read_tmp); 387 if (ret < 0) 388 return ret; 389 *ambient_old_raw = (s16)read_tmp; 390 391 return 0; 392 } 393 394 static int mlx90632_read_object_raw_extended(struct regmap *regmap, s16 *object_new_raw) 395 { 396 unsigned int read_tmp; 397 s32 read; 398 int ret; 399 400 ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_1, &read_tmp); 401 if (ret < 0) 402 return ret; 403 read = (s16)read_tmp; 404 405 ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_2, &read_tmp); 406 if (ret < 0) 407 return ret; 408 read = read - (s16)read_tmp; 409 410 ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_3, &read_tmp); 411 if (ret < 0) 412 return ret; 413 read = read - (s16)read_tmp; 414 415 ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_4, &read_tmp); 416 if (ret < 0) 417 return ret; 418 read = (read + (s16)read_tmp) / 2; 419 420 ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_5, &read_tmp); 421 if (ret < 0) 422 return ret; 423 read = read + (s16)read_tmp; 424 425 ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_6, &read_tmp); 426 if (ret < 0) 427 return ret; 428 read = read + (s16)read_tmp; 429 430 if (read > S16_MAX || read < S16_MIN) 431 return -ERANGE; 432 433 *object_new_raw = read; 434 435 return 0; 436 } 437 438 static int mlx90632_read_all_channel_extended(struct mlx90632_data *data, s16 *object_new_raw, 439 s16 *ambient_new_raw, s16 *ambient_old_raw) 440 { 441 s32 ret, meas; 442 443 mutex_lock(&data->lock); 444 ret = mlx90632_set_meas_type(data->regmap, MLX90632_MTYP_EXTENDED); 445 if (ret < 0) 446 goto read_unlock; 447 448 ret = read_poll_timeout(mlx90632_perform_measurement, meas, meas == 19, 449 50000, 800000, false, data); 450 if (ret != 0) 451 goto read_unlock; 452 453 ret = mlx90632_read_object_raw_extended(data->regmap, object_new_raw); 454 if (ret < 0) 455 goto read_unlock; 456 457 ret = mlx90632_read_ambient_raw_extended(data->regmap, ambient_new_raw, ambient_old_raw); 458 459 read_unlock: 460 (void) mlx90632_set_meas_type(data->regmap, MLX90632_MTYP_MEDICAL); 461 462 mutex_unlock(&data->lock); 463 return ret; 464 } 465 466 static int mlx90632_read_ee_register(struct regmap *regmap, u16 reg_lsb, 467 s32 *reg_value) 468 { 469 s32 ret; 470 unsigned int read; 471 u32 value; 472 473 ret = regmap_read(regmap, reg_lsb, &read); 474 if (ret < 0) 475 return ret; 476 477 value = read; 478 479 ret = regmap_read(regmap, reg_lsb + 1, &read); 480 if (ret < 0) 481 return ret; 482 483 *reg_value = (read << 16) | (value & 0xffff); 484 485 return 0; 486 } 487 488 static s64 mlx90632_preprocess_temp_amb(s16 ambient_new_raw, 489 s16 ambient_old_raw, s16 Gb) 490 { 491 s64 VR_Ta, kGb, tmp; 492 493 kGb = ((s64)Gb * 1000LL) >> 10ULL; 494 VR_Ta = (s64)ambient_old_raw * 1000000LL + 495 kGb * div64_s64(((s64)ambient_new_raw * 1000LL), 496 (MLX90632_REF_3)); 497 tmp = div64_s64( 498 div64_s64(((s64)ambient_new_raw * 1000000000000LL), 499 (MLX90632_REF_3)), VR_Ta); 500 return div64_s64(tmp << 19ULL, 1000LL); 501 } 502 503 static s64 mlx90632_preprocess_temp_obj(s16 object_new_raw, s16 object_old_raw, 504 s16 ambient_new_raw, 505 s16 ambient_old_raw, s16 Ka) 506 { 507 s64 VR_IR, kKa, tmp; 508 509 kKa = ((s64)Ka * 1000LL) >> 10ULL; 510 VR_IR = (s64)ambient_old_raw * 1000000LL + 511 kKa * div64_s64(((s64)ambient_new_raw * 1000LL), 512 (MLX90632_REF_3)); 513 tmp = div64_s64( 514 div64_s64(((s64)((object_new_raw + object_old_raw) / 2) 515 * 1000000000000LL), (MLX90632_REF_12)), 516 VR_IR); 517 return div64_s64((tmp << 19ULL), 1000LL); 518 } 519 520 static s64 mlx90632_preprocess_temp_obj_extended(s16 object_new_raw, s16 ambient_new_raw, 521 s16 ambient_old_raw, s16 Ka) 522 { 523 s64 VR_IR, kKa, tmp; 524 525 kKa = ((s64)Ka * 1000LL) >> 10ULL; 526 VR_IR = (s64)ambient_old_raw * 1000000LL + 527 kKa * div64_s64((s64)ambient_new_raw * 1000LL, 528 MLX90632_REF_3); 529 tmp = div64_s64( 530 div64_s64((s64) object_new_raw * 1000000000000LL, MLX90632_REF_12), 531 VR_IR); 532 return div64_s64(tmp << 19ULL, 1000LL); 533 } 534 535 static s32 mlx90632_calc_temp_ambient(s16 ambient_new_raw, s16 ambient_old_raw, 536 s32 P_T, s32 P_R, s32 P_G, s32 P_O, s16 Gb) 537 { 538 s64 Asub, Bsub, Ablock, Bblock, Cblock, AMB, sum; 539 540 AMB = mlx90632_preprocess_temp_amb(ambient_new_raw, ambient_old_raw, 541 Gb); 542 Asub = ((s64)P_T * 10000000000LL) >> 44ULL; 543 Bsub = AMB - (((s64)P_R * 1000LL) >> 8ULL); 544 Ablock = Asub * (Bsub * Bsub); 545 Bblock = (div64_s64(Bsub * 10000000LL, P_G)) << 20ULL; 546 Cblock = ((s64)P_O * 10000000000LL) >> 8ULL; 547 548 sum = div64_s64(Ablock, 1000000LL) + Bblock + Cblock; 549 550 return div64_s64(sum, 10000000LL); 551 } 552 553 static s32 mlx90632_calc_temp_object_iteration(s32 prev_object_temp, s64 object, 554 s64 TAdut, s64 TAdut4, s32 Fa, s32 Fb, 555 s32 Ga, s16 Ha, s16 Hb, 556 u16 emissivity) 557 { 558 s64 calcedKsTO, calcedKsTA, ir_Alpha, Alpha_corr; 559 s64 Ha_customer, Hb_customer; 560 561 Ha_customer = ((s64)Ha * 1000000LL) >> 14ULL; 562 Hb_customer = ((s64)Hb * 100) >> 10ULL; 563 564 calcedKsTO = ((s64)((s64)Ga * (prev_object_temp - 25 * 1000LL) 565 * 1000LL)) >> 36LL; 566 calcedKsTA = ((s64)(Fb * (TAdut - 25 * 1000000LL))) >> 36LL; 567 Alpha_corr = div64_s64((((s64)(Fa * 10000000000LL) >> 46LL) 568 * Ha_customer), 1000LL); 569 Alpha_corr *= ((s64)(1 * 1000000LL + calcedKsTO + calcedKsTA)); 570 Alpha_corr = emissivity * div64_s64(Alpha_corr, 100000LL); 571 Alpha_corr = div64_s64(Alpha_corr, 1000LL); 572 ir_Alpha = div64_s64((s64)object * 10000000LL, Alpha_corr); 573 574 return (int_sqrt64(int_sqrt64(ir_Alpha * 1000000000000LL + TAdut4)) 575 - 27315 - Hb_customer) * 10; 576 } 577 578 static s64 mlx90632_calc_ta4(s64 TAdut, s64 scale) 579 { 580 return (div64_s64(TAdut, scale) + 27315) * 581 (div64_s64(TAdut, scale) + 27315) * 582 (div64_s64(TAdut, scale) + 27315) * 583 (div64_s64(TAdut, scale) + 27315); 584 } 585 586 static s32 mlx90632_calc_temp_object(s64 object, s64 ambient, s32 Ea, s32 Eb, 587 s32 Fa, s32 Fb, s32 Ga, s16 Ha, s16 Hb, 588 u16 tmp_emi) 589 { 590 s64 kTA, kTA0, TAdut, TAdut4; 591 s64 temp = 25000; 592 s8 i; 593 594 kTA = (Ea * 1000LL) >> 16LL; 595 kTA0 = (Eb * 1000LL) >> 8LL; 596 TAdut = div64_s64(((ambient - kTA0) * 1000000LL), kTA) + 25 * 1000000LL; 597 TAdut4 = mlx90632_calc_ta4(TAdut, 10000LL); 598 599 /* Iterations of calculation as described in datasheet */ 600 for (i = 0; i < 5; ++i) { 601 temp = mlx90632_calc_temp_object_iteration(temp, object, TAdut, TAdut4, 602 Fa, Fb, Ga, Ha, Hb, 603 tmp_emi); 604 } 605 return temp; 606 } 607 608 static s32 mlx90632_calc_temp_object_extended(s64 object, s64 ambient, s64 reflected, 609 s32 Ea, s32 Eb, s32 Fa, s32 Fb, s32 Ga, 610 s16 Ha, s16 Hb, u16 tmp_emi) 611 { 612 s64 kTA, kTA0, TAdut, TAdut4, Tr4, TaTr4; 613 s64 temp = 25000; 614 s8 i; 615 616 kTA = (Ea * 1000LL) >> 16LL; 617 kTA0 = (Eb * 1000LL) >> 8LL; 618 TAdut = div64_s64((ambient - kTA0) * 1000000LL, kTA) + 25 * 1000000LL; 619 Tr4 = mlx90632_calc_ta4(reflected, 10); 620 TAdut4 = mlx90632_calc_ta4(TAdut, 10000LL); 621 TaTr4 = Tr4 - div64_s64(Tr4 - TAdut4, tmp_emi) * 1000; 622 623 /* Iterations of calculation as described in datasheet */ 624 for (i = 0; i < 5; ++i) { 625 temp = mlx90632_calc_temp_object_iteration(temp, object, TAdut, TaTr4, 626 Fa / 2, Fb, Ga, Ha, Hb, 627 tmp_emi); 628 } 629 630 return temp; 631 } 632 633 static int mlx90632_calc_object_dsp105(struct mlx90632_data *data, int *val) 634 { 635 s32 ret; 636 s32 Ea, Eb, Fa, Fb, Ga; 637 unsigned int read_tmp; 638 s16 Ha, Hb, Gb, Ka; 639 s16 ambient_new_raw, ambient_old_raw, object_new_raw, object_old_raw; 640 s64 object, ambient; 641 642 ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Ea, &Ea); 643 if (ret < 0) 644 return ret; 645 ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Eb, &Eb); 646 if (ret < 0) 647 return ret; 648 ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Fa, &Fa); 649 if (ret < 0) 650 return ret; 651 ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Fb, &Fb); 652 if (ret < 0) 653 return ret; 654 ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Ga, &Ga); 655 if (ret < 0) 656 return ret; 657 ret = regmap_read(data->regmap, MLX90632_EE_Ha, &read_tmp); 658 if (ret < 0) 659 return ret; 660 Ha = (s16)read_tmp; 661 ret = regmap_read(data->regmap, MLX90632_EE_Hb, &read_tmp); 662 if (ret < 0) 663 return ret; 664 Hb = (s16)read_tmp; 665 ret = regmap_read(data->regmap, MLX90632_EE_Gb, &read_tmp); 666 if (ret < 0) 667 return ret; 668 Gb = (s16)read_tmp; 669 ret = regmap_read(data->regmap, MLX90632_EE_Ka, &read_tmp); 670 if (ret < 0) 671 return ret; 672 Ka = (s16)read_tmp; 673 674 ret = mlx90632_read_all_channel(data, 675 &ambient_new_raw, &ambient_old_raw, 676 &object_new_raw, &object_old_raw); 677 if (ret < 0) 678 return ret; 679 680 if (object_new_raw > MLX90632_EXTENDED_LIMIT && 681 data->mtyp == MLX90632_MTYP_EXTENDED) { 682 ret = mlx90632_read_all_channel_extended(data, &object_new_raw, 683 &ambient_new_raw, &ambient_old_raw); 684 if (ret < 0) 685 return ret; 686 687 /* Use extended mode calculations */ 688 ambient = mlx90632_preprocess_temp_amb(ambient_new_raw, 689 ambient_old_raw, Gb); 690 object = mlx90632_preprocess_temp_obj_extended(object_new_raw, 691 ambient_new_raw, 692 ambient_old_raw, Ka); 693 *val = mlx90632_calc_temp_object_extended(object, ambient, 694 data->object_ambient_temperature, 695 Ea, Eb, Fa, Fb, Ga, 696 Ha, Hb, data->emissivity); 697 return 0; 698 } 699 700 ambient = mlx90632_preprocess_temp_amb(ambient_new_raw, 701 ambient_old_raw, Gb); 702 object = mlx90632_preprocess_temp_obj(object_new_raw, 703 object_old_raw, 704 ambient_new_raw, 705 ambient_old_raw, Ka); 706 707 *val = mlx90632_calc_temp_object(object, ambient, Ea, Eb, Fa, Fb, Ga, 708 Ha, Hb, data->emissivity); 709 return 0; 710 } 711 712 static int mlx90632_calc_ambient_dsp105(struct mlx90632_data *data, int *val) 713 { 714 s32 ret; 715 unsigned int read_tmp; 716 s32 PT, PR, PG, PO; 717 s16 Gb; 718 s16 ambient_new_raw, ambient_old_raw; 719 720 ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_R, &PR); 721 if (ret < 0) 722 return ret; 723 ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_G, &PG); 724 if (ret < 0) 725 return ret; 726 ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_T, &PT); 727 if (ret < 0) 728 return ret; 729 ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_O, &PO); 730 if (ret < 0) 731 return ret; 732 ret = regmap_read(data->regmap, MLX90632_EE_Gb, &read_tmp); 733 if (ret < 0) 734 return ret; 735 Gb = (s16)read_tmp; 736 737 ret = mlx90632_read_ambient_raw(data->regmap, &ambient_new_raw, 738 &ambient_old_raw); 739 if (ret < 0) 740 return ret; 741 *val = mlx90632_calc_temp_ambient(ambient_new_raw, ambient_old_raw, 742 PT, PR, PG, PO, Gb); 743 return ret; 744 } 745 746 static int mlx90632_read_raw(struct iio_dev *indio_dev, 747 struct iio_chan_spec const *channel, int *val, 748 int *val2, long mask) 749 { 750 struct mlx90632_data *data = iio_priv(indio_dev); 751 int ret; 752 753 switch (mask) { 754 case IIO_CHAN_INFO_PROCESSED: 755 switch (channel->channel2) { 756 case IIO_MOD_TEMP_AMBIENT: 757 ret = mlx90632_calc_ambient_dsp105(data, val); 758 if (ret < 0) 759 return ret; 760 return IIO_VAL_INT; 761 case IIO_MOD_TEMP_OBJECT: 762 ret = mlx90632_calc_object_dsp105(data, val); 763 if (ret < 0) 764 return ret; 765 return IIO_VAL_INT; 766 default: 767 return -EINVAL; 768 } 769 case IIO_CHAN_INFO_CALIBEMISSIVITY: 770 if (data->emissivity == 1000) { 771 *val = 1; 772 *val2 = 0; 773 } else { 774 *val = 0; 775 *val2 = data->emissivity * 1000; 776 } 777 return IIO_VAL_INT_PLUS_MICRO; 778 case IIO_CHAN_INFO_CALIBAMBIENT: 779 *val = data->object_ambient_temperature; 780 return IIO_VAL_INT; 781 default: 782 return -EINVAL; 783 } 784 } 785 786 static int mlx90632_write_raw(struct iio_dev *indio_dev, 787 struct iio_chan_spec const *channel, int val, 788 int val2, long mask) 789 { 790 struct mlx90632_data *data = iio_priv(indio_dev); 791 792 switch (mask) { 793 case IIO_CHAN_INFO_CALIBEMISSIVITY: 794 /* Confirm we are within 0 and 1.0 */ 795 if (val < 0 || val2 < 0 || val > 1 || 796 (val == 1 && val2 != 0)) 797 return -EINVAL; 798 data->emissivity = val * 1000 + val2 / 1000; 799 return 0; 800 case IIO_CHAN_INFO_CALIBAMBIENT: 801 data->object_ambient_temperature = val; 802 return 0; 803 default: 804 return -EINVAL; 805 } 806 } 807 808 static const struct iio_chan_spec mlx90632_channels[] = { 809 { 810 .type = IIO_TEMP, 811 .modified = 1, 812 .channel2 = IIO_MOD_TEMP_AMBIENT, 813 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 814 }, 815 { 816 .type = IIO_TEMP, 817 .modified = 1, 818 .channel2 = IIO_MOD_TEMP_OBJECT, 819 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 820 BIT(IIO_CHAN_INFO_CALIBEMISSIVITY) | BIT(IIO_CHAN_INFO_CALIBAMBIENT), 821 }, 822 }; 823 824 static const struct iio_info mlx90632_info = { 825 .read_raw = mlx90632_read_raw, 826 .write_raw = mlx90632_write_raw, 827 }; 828 829 static int mlx90632_sleep(struct mlx90632_data *data) 830 { 831 regcache_mark_dirty(data->regmap); 832 833 dev_dbg(&data->client->dev, "Requesting sleep"); 834 return mlx90632_pwr_set_sleep_step(data->regmap); 835 } 836 837 static int mlx90632_wakeup(struct mlx90632_data *data) 838 { 839 int ret; 840 841 ret = regcache_sync(data->regmap); 842 if (ret < 0) { 843 dev_err(&data->client->dev, 844 "Failed to sync regmap registers: %d\n", ret); 845 return ret; 846 } 847 848 dev_dbg(&data->client->dev, "Requesting wake-up\n"); 849 return mlx90632_pwr_continuous(data->regmap); 850 } 851 852 static void mlx90632_disable_regulator(void *_data) 853 { 854 struct mlx90632_data *data = _data; 855 int ret; 856 857 ret = regulator_disable(data->regulator); 858 if (ret < 0) 859 dev_err(regmap_get_device(data->regmap), 860 "Failed to disable power regulator: %d\n", ret); 861 } 862 863 static int mlx90632_enable_regulator(struct mlx90632_data *data) 864 { 865 int ret; 866 867 ret = regulator_enable(data->regulator); 868 if (ret < 0) { 869 dev_err(regmap_get_device(data->regmap), "Failed to enable power regulator!\n"); 870 return ret; 871 } 872 873 mlx90632_reset_delay(); 874 875 return ret; 876 } 877 878 static int mlx90632_probe(struct i2c_client *client, 879 const struct i2c_device_id *id) 880 { 881 struct iio_dev *indio_dev; 882 struct mlx90632_data *mlx90632; 883 struct regmap *regmap; 884 int ret; 885 unsigned int read; 886 887 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mlx90632)); 888 if (!indio_dev) { 889 dev_err(&client->dev, "Failed to allocate device\n"); 890 return -ENOMEM; 891 } 892 893 regmap = devm_regmap_init_i2c(client, &mlx90632_regmap); 894 if (IS_ERR(regmap)) { 895 ret = PTR_ERR(regmap); 896 dev_err(&client->dev, "Failed to allocate regmap: %d\n", ret); 897 return ret; 898 } 899 900 mlx90632 = iio_priv(indio_dev); 901 i2c_set_clientdata(client, indio_dev); 902 mlx90632->client = client; 903 mlx90632->regmap = regmap; 904 mlx90632->mtyp = MLX90632_MTYP_MEDICAL; 905 906 mutex_init(&mlx90632->lock); 907 indio_dev->name = id->name; 908 indio_dev->modes = INDIO_DIRECT_MODE; 909 indio_dev->info = &mlx90632_info; 910 indio_dev->channels = mlx90632_channels; 911 indio_dev->num_channels = ARRAY_SIZE(mlx90632_channels); 912 913 mlx90632->regulator = devm_regulator_get(&client->dev, "vdd"); 914 if (IS_ERR(mlx90632->regulator)) 915 return dev_err_probe(&client->dev, PTR_ERR(mlx90632->regulator), 916 "failed to get vdd regulator"); 917 918 ret = mlx90632_enable_regulator(mlx90632); 919 if (ret < 0) 920 return ret; 921 922 ret = devm_add_action_or_reset(&client->dev, mlx90632_disable_regulator, 923 mlx90632); 924 if (ret < 0) { 925 dev_err(&client->dev, "Failed to setup regulator cleanup action %d\n", 926 ret); 927 return ret; 928 } 929 930 ret = mlx90632_wakeup(mlx90632); 931 if (ret < 0) { 932 dev_err(&client->dev, "Wakeup failed: %d\n", ret); 933 return ret; 934 } 935 936 ret = regmap_read(mlx90632->regmap, MLX90632_EE_VERSION, &read); 937 if (ret < 0) { 938 dev_err(&client->dev, "read of version failed: %d\n", ret); 939 return ret; 940 } 941 read = read & MLX90632_ID_MASK; 942 if (read == MLX90632_ID_MEDICAL) { 943 dev_dbg(&client->dev, 944 "Detected Medical EEPROM calibration %x\n", read); 945 } else if (read == MLX90632_ID_CONSUMER) { 946 dev_dbg(&client->dev, 947 "Detected Consumer EEPROM calibration %x\n", read); 948 } else if (read == MLX90632_ID_EXTENDED) { 949 dev_dbg(&client->dev, 950 "Detected Extended range EEPROM calibration %x\n", read); 951 mlx90632->mtyp = MLX90632_MTYP_EXTENDED; 952 } else if ((read & MLX90632_DSP_MASK) == MLX90632_DSP_VERSION) { 953 dev_dbg(&client->dev, 954 "Detected Unknown EEPROM calibration %x\n", read); 955 } else { 956 dev_err(&client->dev, 957 "Wrong DSP version %x (expected %x)\n", 958 read, MLX90632_DSP_VERSION); 959 return -EPROTONOSUPPORT; 960 } 961 962 mlx90632->emissivity = 1000; 963 mlx90632->object_ambient_temperature = 25000; /* 25 degrees milliCelsius */ 964 965 pm_runtime_disable(&client->dev); 966 ret = pm_runtime_set_active(&client->dev); 967 if (ret < 0) { 968 mlx90632_sleep(mlx90632); 969 return ret; 970 } 971 pm_runtime_enable(&client->dev); 972 pm_runtime_set_autosuspend_delay(&client->dev, MLX90632_SLEEP_DELAY_MS); 973 pm_runtime_use_autosuspend(&client->dev); 974 975 return iio_device_register(indio_dev); 976 } 977 978 static void mlx90632_remove(struct i2c_client *client) 979 { 980 struct iio_dev *indio_dev = i2c_get_clientdata(client); 981 struct mlx90632_data *data = iio_priv(indio_dev); 982 983 iio_device_unregister(indio_dev); 984 985 pm_runtime_disable(&client->dev); 986 pm_runtime_set_suspended(&client->dev); 987 pm_runtime_put_noidle(&client->dev); 988 989 mlx90632_sleep(data); 990 } 991 992 static const struct i2c_device_id mlx90632_id[] = { 993 { "mlx90632", 0 }, 994 { } 995 }; 996 MODULE_DEVICE_TABLE(i2c, mlx90632_id); 997 998 static const struct of_device_id mlx90632_of_match[] = { 999 { .compatible = "melexis,mlx90632" }, 1000 { } 1001 }; 1002 MODULE_DEVICE_TABLE(of, mlx90632_of_match); 1003 1004 static int __maybe_unused mlx90632_pm_suspend(struct device *dev) 1005 { 1006 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1007 struct mlx90632_data *data = iio_priv(indio_dev); 1008 1009 return mlx90632_sleep(data); 1010 } 1011 1012 static int __maybe_unused mlx90632_pm_resume(struct device *dev) 1013 { 1014 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1015 struct mlx90632_data *data = iio_priv(indio_dev); 1016 1017 return mlx90632_wakeup(data); 1018 } 1019 1020 static UNIVERSAL_DEV_PM_OPS(mlx90632_pm_ops, mlx90632_pm_suspend, 1021 mlx90632_pm_resume, NULL); 1022 1023 static struct i2c_driver mlx90632_driver = { 1024 .driver = { 1025 .name = "mlx90632", 1026 .of_match_table = mlx90632_of_match, 1027 .pm = &mlx90632_pm_ops, 1028 }, 1029 .probe = mlx90632_probe, 1030 .remove = mlx90632_remove, 1031 .id_table = mlx90632_id, 1032 }; 1033 module_i2c_driver(mlx90632_driver); 1034 1035 MODULE_AUTHOR("Crt Mori <cmo@melexis.com>"); 1036 MODULE_DESCRIPTION("Melexis MLX90632 contactless Infra Red temperature sensor driver"); 1037 MODULE_LICENSE("GPL v2"); 1038