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