18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * mlx90632.c - Melexis MLX90632 contactless IR temperature sensor 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2017 Melexis <cmo@melexis.com> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Driver for the Melexis MLX90632 I2C 16-bit IR thermopile sensor 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci#include <linux/delay.h> 108c2ecf20Sopenharmony_ci#include <linux/err.h> 118c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 128c2ecf20Sopenharmony_ci#include <linux/i2c.h> 138c2ecf20Sopenharmony_ci#include <linux/iopoll.h> 148c2ecf20Sopenharmony_ci#include <linux/kernel.h> 158c2ecf20Sopenharmony_ci#include <linux/limits.h> 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/math64.h> 188c2ecf20Sopenharmony_ci#include <linux/of.h> 198c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 208c2ecf20Sopenharmony_ci#include <linux/regmap.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 238c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* Memory sections addresses */ 268c2ecf20Sopenharmony_ci#define MLX90632_ADDR_RAM 0x4000 /* Start address of ram */ 278c2ecf20Sopenharmony_ci#define MLX90632_ADDR_EEPROM 0x2480 /* Start address of user eeprom */ 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* EEPROM addresses - used at startup */ 308c2ecf20Sopenharmony_ci#define MLX90632_EE_CTRL 0x24d4 /* Control register initial value */ 318c2ecf20Sopenharmony_ci#define MLX90632_EE_I2C_ADDR 0x24d5 /* I2C address register initial value */ 328c2ecf20Sopenharmony_ci#define MLX90632_EE_VERSION 0x240b /* EEPROM version reg address */ 338c2ecf20Sopenharmony_ci#define MLX90632_EE_P_R 0x240c /* P_R calibration register 32bit */ 348c2ecf20Sopenharmony_ci#define MLX90632_EE_P_G 0x240e /* P_G calibration register 32bit */ 358c2ecf20Sopenharmony_ci#define MLX90632_EE_P_T 0x2410 /* P_T calibration register 32bit */ 368c2ecf20Sopenharmony_ci#define MLX90632_EE_P_O 0x2412 /* P_O calibration register 32bit */ 378c2ecf20Sopenharmony_ci#define MLX90632_EE_Aa 0x2414 /* Aa calibration register 32bit */ 388c2ecf20Sopenharmony_ci#define MLX90632_EE_Ab 0x2416 /* Ab calibration register 32bit */ 398c2ecf20Sopenharmony_ci#define MLX90632_EE_Ba 0x2418 /* Ba calibration register 32bit */ 408c2ecf20Sopenharmony_ci#define MLX90632_EE_Bb 0x241a /* Bb calibration register 32bit */ 418c2ecf20Sopenharmony_ci#define MLX90632_EE_Ca 0x241c /* Ca calibration register 32bit */ 428c2ecf20Sopenharmony_ci#define MLX90632_EE_Cb 0x241e /* Cb calibration register 32bit */ 438c2ecf20Sopenharmony_ci#define MLX90632_EE_Da 0x2420 /* Da calibration register 32bit */ 448c2ecf20Sopenharmony_ci#define MLX90632_EE_Db 0x2422 /* Db calibration register 32bit */ 458c2ecf20Sopenharmony_ci#define MLX90632_EE_Ea 0x2424 /* Ea calibration register 32bit */ 468c2ecf20Sopenharmony_ci#define MLX90632_EE_Eb 0x2426 /* Eb calibration register 32bit */ 478c2ecf20Sopenharmony_ci#define MLX90632_EE_Fa 0x2428 /* Fa calibration register 32bit */ 488c2ecf20Sopenharmony_ci#define MLX90632_EE_Fb 0x242a /* Fb calibration register 32bit */ 498c2ecf20Sopenharmony_ci#define MLX90632_EE_Ga 0x242c /* Ga calibration register 32bit */ 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci#define MLX90632_EE_Gb 0x242e /* Gb calibration register 16bit */ 528c2ecf20Sopenharmony_ci#define MLX90632_EE_Ka 0x242f /* Ka calibration register 16bit */ 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#define MLX90632_EE_Ha 0x2481 /* Ha customer calib value reg 16bit */ 558c2ecf20Sopenharmony_ci#define MLX90632_EE_Hb 0x2482 /* Hb customer calib value reg 16bit */ 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* Register addresses - volatile */ 588c2ecf20Sopenharmony_ci#define MLX90632_REG_I2C_ADDR 0x3000 /* Chip I2C address register */ 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/* Control register address - volatile */ 618c2ecf20Sopenharmony_ci#define MLX90632_REG_CONTROL 0x3001 /* Control Register address */ 628c2ecf20Sopenharmony_ci#define MLX90632_CFG_PWR_MASK GENMASK(2, 1) /* PowerMode Mask */ 638c2ecf20Sopenharmony_ci#define MLX90632_CFG_MTYP_MASK GENMASK(8, 4) /* Meas select Mask */ 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/* PowerModes statuses */ 668c2ecf20Sopenharmony_ci#define MLX90632_PWR_STATUS(ctrl_val) (ctrl_val << 1) 678c2ecf20Sopenharmony_ci#define MLX90632_PWR_STATUS_HALT MLX90632_PWR_STATUS(0) /* hold */ 688c2ecf20Sopenharmony_ci#define MLX90632_PWR_STATUS_SLEEP_STEP MLX90632_PWR_STATUS(1) /* sleep step*/ 698c2ecf20Sopenharmony_ci#define MLX90632_PWR_STATUS_STEP MLX90632_PWR_STATUS(2) /* step */ 708c2ecf20Sopenharmony_ci#define MLX90632_PWR_STATUS_CONTINUOUS MLX90632_PWR_STATUS(3) /* continuous*/ 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci/* Measurement types */ 738c2ecf20Sopenharmony_ci#define MLX90632_MTYP_MEDICAL 0 748c2ecf20Sopenharmony_ci#define MLX90632_MTYP_EXTENDED 17 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/* Measurement type select*/ 778c2ecf20Sopenharmony_ci#define MLX90632_MTYP_STATUS(ctrl_val) (ctrl_val << 4) 788c2ecf20Sopenharmony_ci#define MLX90632_MTYP_STATUS_MEDICAL MLX90632_MTYP_STATUS(MLX90632_MTYP_MEDICAL) 798c2ecf20Sopenharmony_ci#define MLX90632_MTYP_STATUS_EXTENDED MLX90632_MTYP_STATUS(MLX90632_MTYP_EXTENDED) 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci/* I2C command register - volatile */ 828c2ecf20Sopenharmony_ci#define MLX90632_REG_I2C_CMD 0x3005 /* I2C command Register address */ 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci/* Device status register - volatile */ 858c2ecf20Sopenharmony_ci#define MLX90632_REG_STATUS 0x3fff /* Device status register */ 868c2ecf20Sopenharmony_ci#define MLX90632_STAT_BUSY BIT(10) /* Device busy indicator */ 878c2ecf20Sopenharmony_ci#define MLX90632_STAT_EE_BUSY BIT(9) /* EEPROM busy indicator */ 888c2ecf20Sopenharmony_ci#define MLX90632_STAT_BRST BIT(8) /* Brown out reset indicator */ 898c2ecf20Sopenharmony_ci#define MLX90632_STAT_CYCLE_POS GENMASK(6, 2) /* Data position */ 908c2ecf20Sopenharmony_ci#define MLX90632_STAT_DATA_RDY BIT(0) /* Data ready indicator */ 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci/* RAM_MEAS address-es for each channel */ 938c2ecf20Sopenharmony_ci#define MLX90632_RAM_1(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num) 948c2ecf20Sopenharmony_ci#define MLX90632_RAM_2(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 1) 958c2ecf20Sopenharmony_ci#define MLX90632_RAM_3(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 2) 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/* Name important RAM_MEAS channels */ 988c2ecf20Sopenharmony_ci#define MLX90632_RAM_DSP5_EXTENDED_AMBIENT_1 MLX90632_RAM_3(17) 998c2ecf20Sopenharmony_ci#define MLX90632_RAM_DSP5_EXTENDED_AMBIENT_2 MLX90632_RAM_3(18) 1008c2ecf20Sopenharmony_ci#define MLX90632_RAM_DSP5_EXTENDED_OBJECT_1 MLX90632_RAM_1(17) 1018c2ecf20Sopenharmony_ci#define MLX90632_RAM_DSP5_EXTENDED_OBJECT_2 MLX90632_RAM_2(17) 1028c2ecf20Sopenharmony_ci#define MLX90632_RAM_DSP5_EXTENDED_OBJECT_3 MLX90632_RAM_1(18) 1038c2ecf20Sopenharmony_ci#define MLX90632_RAM_DSP5_EXTENDED_OBJECT_4 MLX90632_RAM_2(18) 1048c2ecf20Sopenharmony_ci#define MLX90632_RAM_DSP5_EXTENDED_OBJECT_5 MLX90632_RAM_1(19) 1058c2ecf20Sopenharmony_ci#define MLX90632_RAM_DSP5_EXTENDED_OBJECT_6 MLX90632_RAM_2(19) 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci/* Magic constants */ 1088c2ecf20Sopenharmony_ci#define MLX90632_ID_MEDICAL 0x0105 /* EEPROM DSPv5 Medical device id */ 1098c2ecf20Sopenharmony_ci#define MLX90632_ID_CONSUMER 0x0205 /* EEPROM DSPv5 Consumer device id */ 1108c2ecf20Sopenharmony_ci#define MLX90632_ID_EXTENDED 0x0505 /* EEPROM DSPv5 Extended range device id */ 1118c2ecf20Sopenharmony_ci#define MLX90632_ID_MASK GENMASK(14, 0) /* DSP version and device ID in EE_VERSION */ 1128c2ecf20Sopenharmony_ci#define MLX90632_DSP_VERSION 5 /* DSP version */ 1138c2ecf20Sopenharmony_ci#define MLX90632_DSP_MASK GENMASK(7, 0) /* DSP version in EE_VERSION */ 1148c2ecf20Sopenharmony_ci#define MLX90632_RESET_CMD 0x0006 /* Reset sensor (address or global) */ 1158c2ecf20Sopenharmony_ci#define MLX90632_REF_12 12LL /* ResCtrlRef value of Ch 1 or Ch 2 */ 1168c2ecf20Sopenharmony_ci#define MLX90632_REF_3 12LL /* ResCtrlRef value of Channel 3 */ 1178c2ecf20Sopenharmony_ci#define MLX90632_MAX_MEAS_NUM 31 /* Maximum measurements in list */ 1188c2ecf20Sopenharmony_ci#define MLX90632_SLEEP_DELAY_MS 3000 /* Autosleep delay */ 1198c2ecf20Sopenharmony_ci#define MLX90632_EXTENDED_LIMIT 27000 /* Extended mode raw value limit */ 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci/** 1228c2ecf20Sopenharmony_ci * struct mlx90632_data - private data for the MLX90632 device 1238c2ecf20Sopenharmony_ci * @client: I2C client of the device 1248c2ecf20Sopenharmony_ci * @lock: Internal mutex for multiple reads for single measurement 1258c2ecf20Sopenharmony_ci * @regmap: Regmap of the device 1268c2ecf20Sopenharmony_ci * @emissivity: Object emissivity from 0 to 1000 where 1000 = 1. 1278c2ecf20Sopenharmony_ci * @mtyp: Measurement type physical sensor configuration for extended range 1288c2ecf20Sopenharmony_ci * calculations 1298c2ecf20Sopenharmony_ci * @object_ambient_temperature: Ambient temperature at object (might differ of 1308c2ecf20Sopenharmony_ci * the ambient temperature of sensor. 1318c2ecf20Sopenharmony_ci */ 1328c2ecf20Sopenharmony_cistruct mlx90632_data { 1338c2ecf20Sopenharmony_ci struct i2c_client *client; 1348c2ecf20Sopenharmony_ci struct mutex lock; 1358c2ecf20Sopenharmony_ci struct regmap *regmap; 1368c2ecf20Sopenharmony_ci u16 emissivity; 1378c2ecf20Sopenharmony_ci u8 mtyp; 1388c2ecf20Sopenharmony_ci u32 object_ambient_temperature; 1398c2ecf20Sopenharmony_ci}; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistatic const struct regmap_range mlx90632_volatile_reg_range[] = { 1428c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_REG_I2C_ADDR, MLX90632_REG_CONTROL), 1438c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_REG_I2C_CMD, MLX90632_REG_I2C_CMD), 1448c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_REG_STATUS, MLX90632_REG_STATUS), 1458c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_RAM_1(0), 1468c2ecf20Sopenharmony_ci MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), 1478c2ecf20Sopenharmony_ci}; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic const struct regmap_access_table mlx90632_volatile_regs_tbl = { 1508c2ecf20Sopenharmony_ci .yes_ranges = mlx90632_volatile_reg_range, 1518c2ecf20Sopenharmony_ci .n_yes_ranges = ARRAY_SIZE(mlx90632_volatile_reg_range), 1528c2ecf20Sopenharmony_ci}; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_cistatic const struct regmap_range mlx90632_read_reg_range[] = { 1558c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_EE_VERSION, MLX90632_EE_Ka), 1568c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_EE_CTRL, MLX90632_EE_I2C_ADDR), 1578c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_EE_Ha, MLX90632_EE_Hb), 1588c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_REG_I2C_ADDR, MLX90632_REG_CONTROL), 1598c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_REG_I2C_CMD, MLX90632_REG_I2C_CMD), 1608c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_REG_STATUS, MLX90632_REG_STATUS), 1618c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_RAM_1(0), 1628c2ecf20Sopenharmony_ci MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), 1638c2ecf20Sopenharmony_ci}; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistatic const struct regmap_access_table mlx90632_readable_regs_tbl = { 1668c2ecf20Sopenharmony_ci .yes_ranges = mlx90632_read_reg_range, 1678c2ecf20Sopenharmony_ci .n_yes_ranges = ARRAY_SIZE(mlx90632_read_reg_range), 1688c2ecf20Sopenharmony_ci}; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_cistatic const struct regmap_range mlx90632_no_write_reg_range[] = { 1718c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_EE_VERSION, MLX90632_EE_Ka), 1728c2ecf20Sopenharmony_ci regmap_reg_range(MLX90632_RAM_1(0), 1738c2ecf20Sopenharmony_ci MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), 1748c2ecf20Sopenharmony_ci}; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic const struct regmap_access_table mlx90632_writeable_regs_tbl = { 1778c2ecf20Sopenharmony_ci .no_ranges = mlx90632_no_write_reg_range, 1788c2ecf20Sopenharmony_ci .n_no_ranges = ARRAY_SIZE(mlx90632_no_write_reg_range), 1798c2ecf20Sopenharmony_ci}; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_cistatic const struct regmap_config mlx90632_regmap = { 1828c2ecf20Sopenharmony_ci .reg_bits = 16, 1838c2ecf20Sopenharmony_ci .val_bits = 16, 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci .volatile_table = &mlx90632_volatile_regs_tbl, 1868c2ecf20Sopenharmony_ci .rd_table = &mlx90632_readable_regs_tbl, 1878c2ecf20Sopenharmony_ci .wr_table = &mlx90632_writeable_regs_tbl, 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci .use_single_read = true, 1908c2ecf20Sopenharmony_ci .use_single_write = true, 1918c2ecf20Sopenharmony_ci .reg_format_endian = REGMAP_ENDIAN_BIG, 1928c2ecf20Sopenharmony_ci .val_format_endian = REGMAP_ENDIAN_BIG, 1938c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 1948c2ecf20Sopenharmony_ci}; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_cistatic s32 mlx90632_pwr_set_sleep_step(struct regmap *regmap) 1978c2ecf20Sopenharmony_ci{ 1988c2ecf20Sopenharmony_ci return regmap_update_bits(regmap, MLX90632_REG_CONTROL, 1998c2ecf20Sopenharmony_ci MLX90632_CFG_PWR_MASK, 2008c2ecf20Sopenharmony_ci MLX90632_PWR_STATUS_SLEEP_STEP); 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_cistatic s32 mlx90632_pwr_continuous(struct regmap *regmap) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci return regmap_update_bits(regmap, MLX90632_REG_CONTROL, 2068c2ecf20Sopenharmony_ci MLX90632_CFG_PWR_MASK, 2078c2ecf20Sopenharmony_ci MLX90632_PWR_STATUS_CONTINUOUS); 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci/** 2118c2ecf20Sopenharmony_ci * mlx90632_perform_measurement() - Trigger and retrieve current measurement cycle 2128c2ecf20Sopenharmony_ci * @data: pointer to mlx90632_data object containing regmap information 2138c2ecf20Sopenharmony_ci * 2148c2ecf20Sopenharmony_ci * Perform a measurement and return latest measurement cycle position reported 2158c2ecf20Sopenharmony_ci * by sensor. This is a blocking function for 500ms, as that is default sensor 2168c2ecf20Sopenharmony_ci * refresh rate. 2178c2ecf20Sopenharmony_ci */ 2188c2ecf20Sopenharmony_cistatic int mlx90632_perform_measurement(struct mlx90632_data *data) 2198c2ecf20Sopenharmony_ci{ 2208c2ecf20Sopenharmony_ci unsigned int reg_status; 2218c2ecf20Sopenharmony_ci int ret; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, MLX90632_REG_STATUS, 2248c2ecf20Sopenharmony_ci MLX90632_STAT_DATA_RDY, 0); 2258c2ecf20Sopenharmony_ci if (ret < 0) 2268c2ecf20Sopenharmony_ci return ret; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci ret = regmap_read_poll_timeout(data->regmap, MLX90632_REG_STATUS, reg_status, 2298c2ecf20Sopenharmony_ci !(reg_status & MLX90632_STAT_DATA_RDY), 10000, 2308c2ecf20Sopenharmony_ci 100 * 10000); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci if (ret < 0) { 2338c2ecf20Sopenharmony_ci dev_err(&data->client->dev, "data not ready"); 2348c2ecf20Sopenharmony_ci return -ETIMEDOUT; 2358c2ecf20Sopenharmony_ci } 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci return (reg_status & MLX90632_STAT_CYCLE_POS) >> 2; 2388c2ecf20Sopenharmony_ci} 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_cistatic int mlx90632_set_meas_type(struct regmap *regmap, u8 type) 2418c2ecf20Sopenharmony_ci{ 2428c2ecf20Sopenharmony_ci int ret; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci if ((type != MLX90632_MTYP_MEDICAL) && (type != MLX90632_MTYP_EXTENDED)) 2458c2ecf20Sopenharmony_ci return -EINVAL; 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci ret = regmap_write(regmap, MLX90632_REG_I2C_CMD, MLX90632_RESET_CMD); 2488c2ecf20Sopenharmony_ci if (ret < 0) 2498c2ecf20Sopenharmony_ci return ret; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci /* 2528c2ecf20Sopenharmony_ci * Give the mlx90632 some time to reset properly before sending a new I2C command 2538c2ecf20Sopenharmony_ci * if this is not done, the following I2C command(s) will not be accepted. 2548c2ecf20Sopenharmony_ci */ 2558c2ecf20Sopenharmony_ci usleep_range(150, 200); 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci ret = regmap_write_bits(regmap, MLX90632_REG_CONTROL, 2588c2ecf20Sopenharmony_ci (MLX90632_CFG_MTYP_MASK | MLX90632_CFG_PWR_MASK), 2598c2ecf20Sopenharmony_ci (MLX90632_MTYP_STATUS(type) | MLX90632_PWR_STATUS_HALT)); 2608c2ecf20Sopenharmony_ci if (ret < 0) 2618c2ecf20Sopenharmony_ci return ret; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci return mlx90632_pwr_continuous(regmap); 2648c2ecf20Sopenharmony_ci} 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_cistatic int mlx90632_channel_new_select(int perform_ret, uint8_t *channel_new, 2678c2ecf20Sopenharmony_ci uint8_t *channel_old) 2688c2ecf20Sopenharmony_ci{ 2698c2ecf20Sopenharmony_ci switch (perform_ret) { 2708c2ecf20Sopenharmony_ci case 1: 2718c2ecf20Sopenharmony_ci *channel_new = 1; 2728c2ecf20Sopenharmony_ci *channel_old = 2; 2738c2ecf20Sopenharmony_ci break; 2748c2ecf20Sopenharmony_ci case 2: 2758c2ecf20Sopenharmony_ci *channel_new = 2; 2768c2ecf20Sopenharmony_ci *channel_old = 1; 2778c2ecf20Sopenharmony_ci break; 2788c2ecf20Sopenharmony_ci default: 2798c2ecf20Sopenharmony_ci return -EINVAL; 2808c2ecf20Sopenharmony_ci } 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci return 0; 2838c2ecf20Sopenharmony_ci} 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_cistatic int mlx90632_read_ambient_raw(struct regmap *regmap, 2868c2ecf20Sopenharmony_ci s16 *ambient_new_raw, s16 *ambient_old_raw) 2878c2ecf20Sopenharmony_ci{ 2888c2ecf20Sopenharmony_ci int ret; 2898c2ecf20Sopenharmony_ci unsigned int read_tmp; 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_3(1), &read_tmp); 2928c2ecf20Sopenharmony_ci if (ret < 0) 2938c2ecf20Sopenharmony_ci return ret; 2948c2ecf20Sopenharmony_ci *ambient_new_raw = (s16)read_tmp; 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_3(2), &read_tmp); 2978c2ecf20Sopenharmony_ci if (ret < 0) 2988c2ecf20Sopenharmony_ci return ret; 2998c2ecf20Sopenharmony_ci *ambient_old_raw = (s16)read_tmp; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci return ret; 3028c2ecf20Sopenharmony_ci} 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_cistatic int mlx90632_read_object_raw(struct regmap *regmap, 3058c2ecf20Sopenharmony_ci int perform_measurement_ret, 3068c2ecf20Sopenharmony_ci s16 *object_new_raw, s16 *object_old_raw) 3078c2ecf20Sopenharmony_ci{ 3088c2ecf20Sopenharmony_ci int ret; 3098c2ecf20Sopenharmony_ci unsigned int read_tmp; 3108c2ecf20Sopenharmony_ci s16 read; 3118c2ecf20Sopenharmony_ci u8 channel = 0; 3128c2ecf20Sopenharmony_ci u8 channel_old = 0; 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci ret = mlx90632_channel_new_select(perform_measurement_ret, &channel, 3158c2ecf20Sopenharmony_ci &channel_old); 3168c2ecf20Sopenharmony_ci if (ret != 0) 3178c2ecf20Sopenharmony_ci return ret; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_2(channel), &read_tmp); 3208c2ecf20Sopenharmony_ci if (ret < 0) 3218c2ecf20Sopenharmony_ci return ret; 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci read = (s16)read_tmp; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_1(channel), &read_tmp); 3268c2ecf20Sopenharmony_ci if (ret < 0) 3278c2ecf20Sopenharmony_ci return ret; 3288c2ecf20Sopenharmony_ci *object_new_raw = (read + (s16)read_tmp) / 2; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_2(channel_old), &read_tmp); 3318c2ecf20Sopenharmony_ci if (ret < 0) 3328c2ecf20Sopenharmony_ci return ret; 3338c2ecf20Sopenharmony_ci read = (s16)read_tmp; 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_1(channel_old), &read_tmp); 3368c2ecf20Sopenharmony_ci if (ret < 0) 3378c2ecf20Sopenharmony_ci return ret; 3388c2ecf20Sopenharmony_ci *object_old_raw = (read + (s16)read_tmp) / 2; 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci return ret; 3418c2ecf20Sopenharmony_ci} 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_cistatic int mlx90632_read_all_channel(struct mlx90632_data *data, 3448c2ecf20Sopenharmony_ci s16 *ambient_new_raw, s16 *ambient_old_raw, 3458c2ecf20Sopenharmony_ci s16 *object_new_raw, s16 *object_old_raw) 3468c2ecf20Sopenharmony_ci{ 3478c2ecf20Sopenharmony_ci s32 ret, measurement; 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 3508c2ecf20Sopenharmony_ci measurement = mlx90632_perform_measurement(data); 3518c2ecf20Sopenharmony_ci if (measurement < 0) { 3528c2ecf20Sopenharmony_ci ret = measurement; 3538c2ecf20Sopenharmony_ci goto read_unlock; 3548c2ecf20Sopenharmony_ci } 3558c2ecf20Sopenharmony_ci ret = mlx90632_read_ambient_raw(data->regmap, ambient_new_raw, 3568c2ecf20Sopenharmony_ci ambient_old_raw); 3578c2ecf20Sopenharmony_ci if (ret < 0) 3588c2ecf20Sopenharmony_ci goto read_unlock; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci ret = mlx90632_read_object_raw(data->regmap, measurement, 3618c2ecf20Sopenharmony_ci object_new_raw, object_old_raw); 3628c2ecf20Sopenharmony_ciread_unlock: 3638c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 3648c2ecf20Sopenharmony_ci return ret; 3658c2ecf20Sopenharmony_ci} 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_cistatic int mlx90632_read_ambient_raw_extended(struct regmap *regmap, 3688c2ecf20Sopenharmony_ci s16 *ambient_new_raw, s16 *ambient_old_raw) 3698c2ecf20Sopenharmony_ci{ 3708c2ecf20Sopenharmony_ci unsigned int read_tmp; 3718c2ecf20Sopenharmony_ci int ret; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_AMBIENT_1, &read_tmp); 3748c2ecf20Sopenharmony_ci if (ret < 0) 3758c2ecf20Sopenharmony_ci return ret; 3768c2ecf20Sopenharmony_ci *ambient_new_raw = (s16)read_tmp; 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_AMBIENT_2, &read_tmp); 3798c2ecf20Sopenharmony_ci if (ret < 0) 3808c2ecf20Sopenharmony_ci return ret; 3818c2ecf20Sopenharmony_ci *ambient_old_raw = (s16)read_tmp; 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci return 0; 3848c2ecf20Sopenharmony_ci} 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_cistatic int mlx90632_read_object_raw_extended(struct regmap *regmap, s16 *object_new_raw) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci unsigned int read_tmp; 3898c2ecf20Sopenharmony_ci s32 read; 3908c2ecf20Sopenharmony_ci int ret; 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_1, &read_tmp); 3938c2ecf20Sopenharmony_ci if (ret < 0) 3948c2ecf20Sopenharmony_ci return ret; 3958c2ecf20Sopenharmony_ci read = (s16)read_tmp; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_2, &read_tmp); 3988c2ecf20Sopenharmony_ci if (ret < 0) 3998c2ecf20Sopenharmony_ci return ret; 4008c2ecf20Sopenharmony_ci read = read - (s16)read_tmp; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_3, &read_tmp); 4038c2ecf20Sopenharmony_ci if (ret < 0) 4048c2ecf20Sopenharmony_ci return ret; 4058c2ecf20Sopenharmony_ci read = read - (s16)read_tmp; 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_4, &read_tmp); 4088c2ecf20Sopenharmony_ci if (ret < 0) 4098c2ecf20Sopenharmony_ci return ret; 4108c2ecf20Sopenharmony_ci read = (read + (s16)read_tmp) / 2; 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_5, &read_tmp); 4138c2ecf20Sopenharmony_ci if (ret < 0) 4148c2ecf20Sopenharmony_ci return ret; 4158c2ecf20Sopenharmony_ci read = read + (s16)read_tmp; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci ret = regmap_read(regmap, MLX90632_RAM_DSP5_EXTENDED_OBJECT_6, &read_tmp); 4188c2ecf20Sopenharmony_ci if (ret < 0) 4198c2ecf20Sopenharmony_ci return ret; 4208c2ecf20Sopenharmony_ci read = read + (s16)read_tmp; 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci if (read > S16_MAX || read < S16_MIN) 4238c2ecf20Sopenharmony_ci return -ERANGE; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci *object_new_raw = read; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci return 0; 4288c2ecf20Sopenharmony_ci} 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_cistatic int mlx90632_read_all_channel_extended(struct mlx90632_data *data, s16 *object_new_raw, 4318c2ecf20Sopenharmony_ci s16 *ambient_new_raw, s16 *ambient_old_raw) 4328c2ecf20Sopenharmony_ci{ 4338c2ecf20Sopenharmony_ci s32 ret, meas; 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 4368c2ecf20Sopenharmony_ci ret = mlx90632_set_meas_type(data->regmap, MLX90632_MTYP_EXTENDED); 4378c2ecf20Sopenharmony_ci if (ret < 0) 4388c2ecf20Sopenharmony_ci goto read_unlock; 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_ci ret = read_poll_timeout(mlx90632_perform_measurement, meas, meas == 19, 4418c2ecf20Sopenharmony_ci 50000, 800000, false, data); 4428c2ecf20Sopenharmony_ci if (ret != 0) 4438c2ecf20Sopenharmony_ci goto read_unlock; 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci ret = mlx90632_read_object_raw_extended(data->regmap, object_new_raw); 4468c2ecf20Sopenharmony_ci if (ret < 0) 4478c2ecf20Sopenharmony_ci goto read_unlock; 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci ret = mlx90632_read_ambient_raw_extended(data->regmap, ambient_new_raw, ambient_old_raw); 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ciread_unlock: 4528c2ecf20Sopenharmony_ci (void) mlx90632_set_meas_type(data->regmap, MLX90632_MTYP_MEDICAL); 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 4558c2ecf20Sopenharmony_ci return ret; 4568c2ecf20Sopenharmony_ci} 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_cistatic int mlx90632_read_ee_register(struct regmap *regmap, u16 reg_lsb, 4598c2ecf20Sopenharmony_ci s32 *reg_value) 4608c2ecf20Sopenharmony_ci{ 4618c2ecf20Sopenharmony_ci s32 ret; 4628c2ecf20Sopenharmony_ci unsigned int read; 4638c2ecf20Sopenharmony_ci u32 value; 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci ret = regmap_read(regmap, reg_lsb, &read); 4668c2ecf20Sopenharmony_ci if (ret < 0) 4678c2ecf20Sopenharmony_ci return ret; 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci value = read; 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci ret = regmap_read(regmap, reg_lsb + 1, &read); 4728c2ecf20Sopenharmony_ci if (ret < 0) 4738c2ecf20Sopenharmony_ci return ret; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci *reg_value = (read << 16) | (value & 0xffff); 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci return 0; 4788c2ecf20Sopenharmony_ci} 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_cistatic s64 mlx90632_preprocess_temp_amb(s16 ambient_new_raw, 4818c2ecf20Sopenharmony_ci s16 ambient_old_raw, s16 Gb) 4828c2ecf20Sopenharmony_ci{ 4838c2ecf20Sopenharmony_ci s64 VR_Ta, kGb, tmp; 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci kGb = ((s64)Gb * 1000LL) >> 10ULL; 4868c2ecf20Sopenharmony_ci VR_Ta = (s64)ambient_old_raw * 1000000LL + 4878c2ecf20Sopenharmony_ci kGb * div64_s64(((s64)ambient_new_raw * 1000LL), 4888c2ecf20Sopenharmony_ci (MLX90632_REF_3)); 4898c2ecf20Sopenharmony_ci tmp = div64_s64( 4908c2ecf20Sopenharmony_ci div64_s64(((s64)ambient_new_raw * 1000000000000LL), 4918c2ecf20Sopenharmony_ci (MLX90632_REF_3)), VR_Ta); 4928c2ecf20Sopenharmony_ci return div64_s64(tmp << 19ULL, 1000LL); 4938c2ecf20Sopenharmony_ci} 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_cistatic s64 mlx90632_preprocess_temp_obj(s16 object_new_raw, s16 object_old_raw, 4968c2ecf20Sopenharmony_ci s16 ambient_new_raw, 4978c2ecf20Sopenharmony_ci s16 ambient_old_raw, s16 Ka) 4988c2ecf20Sopenharmony_ci{ 4998c2ecf20Sopenharmony_ci s64 VR_IR, kKa, tmp; 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci kKa = ((s64)Ka * 1000LL) >> 10ULL; 5028c2ecf20Sopenharmony_ci VR_IR = (s64)ambient_old_raw * 1000000LL + 5038c2ecf20Sopenharmony_ci kKa * div64_s64(((s64)ambient_new_raw * 1000LL), 5048c2ecf20Sopenharmony_ci (MLX90632_REF_3)); 5058c2ecf20Sopenharmony_ci tmp = div64_s64( 5068c2ecf20Sopenharmony_ci div64_s64(((s64)((object_new_raw + object_old_raw) / 2) 5078c2ecf20Sopenharmony_ci * 1000000000000LL), (MLX90632_REF_12)), 5088c2ecf20Sopenharmony_ci VR_IR); 5098c2ecf20Sopenharmony_ci return div64_s64((tmp << 19ULL), 1000LL); 5108c2ecf20Sopenharmony_ci} 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_cistatic s64 mlx90632_preprocess_temp_obj_extended(s16 object_new_raw, s16 ambient_new_raw, 5138c2ecf20Sopenharmony_ci s16 ambient_old_raw, s16 Ka) 5148c2ecf20Sopenharmony_ci{ 5158c2ecf20Sopenharmony_ci s64 VR_IR, kKa, tmp; 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci kKa = ((s64)Ka * 1000LL) >> 10ULL; 5188c2ecf20Sopenharmony_ci VR_IR = (s64)ambient_old_raw * 1000000LL + 5198c2ecf20Sopenharmony_ci kKa * div64_s64((s64)ambient_new_raw * 1000LL, 5208c2ecf20Sopenharmony_ci MLX90632_REF_3); 5218c2ecf20Sopenharmony_ci tmp = div64_s64( 5228c2ecf20Sopenharmony_ci div64_s64((s64) object_new_raw * 1000000000000LL, MLX90632_REF_12), 5238c2ecf20Sopenharmony_ci VR_IR); 5248c2ecf20Sopenharmony_ci return div64_s64(tmp << 19ULL, 1000LL); 5258c2ecf20Sopenharmony_ci} 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_cistatic s32 mlx90632_calc_temp_ambient(s16 ambient_new_raw, s16 ambient_old_raw, 5288c2ecf20Sopenharmony_ci s32 P_T, s32 P_R, s32 P_G, s32 P_O, s16 Gb) 5298c2ecf20Sopenharmony_ci{ 5308c2ecf20Sopenharmony_ci s64 Asub, Bsub, Ablock, Bblock, Cblock, AMB, sum; 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci AMB = mlx90632_preprocess_temp_amb(ambient_new_raw, ambient_old_raw, 5338c2ecf20Sopenharmony_ci Gb); 5348c2ecf20Sopenharmony_ci Asub = ((s64)P_T * 10000000000LL) >> 44ULL; 5358c2ecf20Sopenharmony_ci Bsub = AMB - (((s64)P_R * 1000LL) >> 8ULL); 5368c2ecf20Sopenharmony_ci Ablock = Asub * (Bsub * Bsub); 5378c2ecf20Sopenharmony_ci Bblock = (div64_s64(Bsub * 10000000LL, P_G)) << 20ULL; 5388c2ecf20Sopenharmony_ci Cblock = ((s64)P_O * 10000000000LL) >> 8ULL; 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci sum = div64_s64(Ablock, 1000000LL) + Bblock + Cblock; 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci return div64_s64(sum, 10000000LL); 5438c2ecf20Sopenharmony_ci} 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_cistatic s32 mlx90632_calc_temp_object_iteration(s32 prev_object_temp, s64 object, 5468c2ecf20Sopenharmony_ci s64 TAdut, s64 TAdut4, s32 Fa, s32 Fb, 5478c2ecf20Sopenharmony_ci s32 Ga, s16 Ha, s16 Hb, 5488c2ecf20Sopenharmony_ci u16 emissivity) 5498c2ecf20Sopenharmony_ci{ 5508c2ecf20Sopenharmony_ci s64 calcedKsTO, calcedKsTA, ir_Alpha, Alpha_corr; 5518c2ecf20Sopenharmony_ci s64 Ha_customer, Hb_customer; 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci Ha_customer = ((s64)Ha * 1000000LL) >> 14ULL; 5548c2ecf20Sopenharmony_ci Hb_customer = ((s64)Hb * 100) >> 10ULL; 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci calcedKsTO = ((s64)((s64)Ga * (prev_object_temp - 25 * 1000LL) 5578c2ecf20Sopenharmony_ci * 1000LL)) >> 36LL; 5588c2ecf20Sopenharmony_ci calcedKsTA = ((s64)(Fb * (TAdut - 25 * 1000000LL))) >> 36LL; 5598c2ecf20Sopenharmony_ci Alpha_corr = div64_s64((((s64)(Fa * 10000000000LL) >> 46LL) 5608c2ecf20Sopenharmony_ci * Ha_customer), 1000LL); 5618c2ecf20Sopenharmony_ci Alpha_corr *= ((s64)(1 * 1000000LL + calcedKsTO + calcedKsTA)); 5628c2ecf20Sopenharmony_ci Alpha_corr = emissivity * div64_s64(Alpha_corr, 100000LL); 5638c2ecf20Sopenharmony_ci Alpha_corr = div64_s64(Alpha_corr, 1000LL); 5648c2ecf20Sopenharmony_ci ir_Alpha = div64_s64((s64)object * 10000000LL, Alpha_corr); 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci return (int_sqrt64(int_sqrt64(ir_Alpha * 1000000000000LL + TAdut4)) 5678c2ecf20Sopenharmony_ci - 27315 - Hb_customer) * 10; 5688c2ecf20Sopenharmony_ci} 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_cistatic s64 mlx90632_calc_ta4(s64 TAdut, s64 scale) 5718c2ecf20Sopenharmony_ci{ 5728c2ecf20Sopenharmony_ci return (div64_s64(TAdut, scale) + 27315) * 5738c2ecf20Sopenharmony_ci (div64_s64(TAdut, scale) + 27315) * 5748c2ecf20Sopenharmony_ci (div64_s64(TAdut, scale) + 27315) * 5758c2ecf20Sopenharmony_ci (div64_s64(TAdut, scale) + 27315); 5768c2ecf20Sopenharmony_ci} 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_cistatic s32 mlx90632_calc_temp_object(s64 object, s64 ambient, s32 Ea, s32 Eb, 5798c2ecf20Sopenharmony_ci s32 Fa, s32 Fb, s32 Ga, s16 Ha, s16 Hb, 5808c2ecf20Sopenharmony_ci u16 tmp_emi) 5818c2ecf20Sopenharmony_ci{ 5828c2ecf20Sopenharmony_ci s64 kTA, kTA0, TAdut, TAdut4; 5838c2ecf20Sopenharmony_ci s64 temp = 25000; 5848c2ecf20Sopenharmony_ci s8 i; 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci kTA = (Ea * 1000LL) >> 16LL; 5878c2ecf20Sopenharmony_ci kTA0 = (Eb * 1000LL) >> 8LL; 5888c2ecf20Sopenharmony_ci TAdut = div64_s64(((ambient - kTA0) * 1000000LL), kTA) + 25 * 1000000LL; 5898c2ecf20Sopenharmony_ci TAdut4 = mlx90632_calc_ta4(TAdut, 10000LL); 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_ci /* Iterations of calculation as described in datasheet */ 5928c2ecf20Sopenharmony_ci for (i = 0; i < 5; ++i) { 5938c2ecf20Sopenharmony_ci temp = mlx90632_calc_temp_object_iteration(temp, object, TAdut, TAdut4, 5948c2ecf20Sopenharmony_ci Fa, Fb, Ga, Ha, Hb, 5958c2ecf20Sopenharmony_ci tmp_emi); 5968c2ecf20Sopenharmony_ci } 5978c2ecf20Sopenharmony_ci return temp; 5988c2ecf20Sopenharmony_ci} 5998c2ecf20Sopenharmony_ci 6008c2ecf20Sopenharmony_cistatic s32 mlx90632_calc_temp_object_extended(s64 object, s64 ambient, s64 reflected, 6018c2ecf20Sopenharmony_ci s32 Ea, s32 Eb, s32 Fa, s32 Fb, s32 Ga, 6028c2ecf20Sopenharmony_ci s16 Ha, s16 Hb, u16 tmp_emi) 6038c2ecf20Sopenharmony_ci{ 6048c2ecf20Sopenharmony_ci s64 kTA, kTA0, TAdut, TAdut4, Tr4, TaTr4; 6058c2ecf20Sopenharmony_ci s64 temp = 25000; 6068c2ecf20Sopenharmony_ci s8 i; 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci kTA = (Ea * 1000LL) >> 16LL; 6098c2ecf20Sopenharmony_ci kTA0 = (Eb * 1000LL) >> 8LL; 6108c2ecf20Sopenharmony_ci TAdut = div64_s64((ambient - kTA0) * 1000000LL, kTA) + 25 * 1000000LL; 6118c2ecf20Sopenharmony_ci Tr4 = mlx90632_calc_ta4(reflected, 10); 6128c2ecf20Sopenharmony_ci TAdut4 = mlx90632_calc_ta4(TAdut, 10000LL); 6138c2ecf20Sopenharmony_ci TaTr4 = Tr4 - div64_s64(Tr4 - TAdut4, tmp_emi) * 1000; 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci /* Iterations of calculation as described in datasheet */ 6168c2ecf20Sopenharmony_ci for (i = 0; i < 5; ++i) { 6178c2ecf20Sopenharmony_ci temp = mlx90632_calc_temp_object_iteration(temp, object, TAdut, TaTr4, 6188c2ecf20Sopenharmony_ci Fa / 2, Fb, Ga, Ha, Hb, 6198c2ecf20Sopenharmony_ci tmp_emi); 6208c2ecf20Sopenharmony_ci } 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci return temp; 6238c2ecf20Sopenharmony_ci} 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_cistatic int mlx90632_calc_object_dsp105(struct mlx90632_data *data, int *val) 6268c2ecf20Sopenharmony_ci{ 6278c2ecf20Sopenharmony_ci s32 ret; 6288c2ecf20Sopenharmony_ci s32 Ea, Eb, Fa, Fb, Ga; 6298c2ecf20Sopenharmony_ci unsigned int read_tmp; 6308c2ecf20Sopenharmony_ci s16 Ha, Hb, Gb, Ka; 6318c2ecf20Sopenharmony_ci s16 ambient_new_raw, ambient_old_raw, object_new_raw, object_old_raw; 6328c2ecf20Sopenharmony_ci s64 object, ambient; 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Ea, &Ea); 6358c2ecf20Sopenharmony_ci if (ret < 0) 6368c2ecf20Sopenharmony_ci return ret; 6378c2ecf20Sopenharmony_ci ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Eb, &Eb); 6388c2ecf20Sopenharmony_ci if (ret < 0) 6398c2ecf20Sopenharmony_ci return ret; 6408c2ecf20Sopenharmony_ci ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Fa, &Fa); 6418c2ecf20Sopenharmony_ci if (ret < 0) 6428c2ecf20Sopenharmony_ci return ret; 6438c2ecf20Sopenharmony_ci ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Fb, &Fb); 6448c2ecf20Sopenharmony_ci if (ret < 0) 6458c2ecf20Sopenharmony_ci return ret; 6468c2ecf20Sopenharmony_ci ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Ga, &Ga); 6478c2ecf20Sopenharmony_ci if (ret < 0) 6488c2ecf20Sopenharmony_ci return ret; 6498c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MLX90632_EE_Ha, &read_tmp); 6508c2ecf20Sopenharmony_ci if (ret < 0) 6518c2ecf20Sopenharmony_ci return ret; 6528c2ecf20Sopenharmony_ci Ha = (s16)read_tmp; 6538c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MLX90632_EE_Hb, &read_tmp); 6548c2ecf20Sopenharmony_ci if (ret < 0) 6558c2ecf20Sopenharmony_ci return ret; 6568c2ecf20Sopenharmony_ci Hb = (s16)read_tmp; 6578c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MLX90632_EE_Gb, &read_tmp); 6588c2ecf20Sopenharmony_ci if (ret < 0) 6598c2ecf20Sopenharmony_ci return ret; 6608c2ecf20Sopenharmony_ci Gb = (s16)read_tmp; 6618c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MLX90632_EE_Ka, &read_tmp); 6628c2ecf20Sopenharmony_ci if (ret < 0) 6638c2ecf20Sopenharmony_ci return ret; 6648c2ecf20Sopenharmony_ci Ka = (s16)read_tmp; 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci ret = mlx90632_read_all_channel(data, 6678c2ecf20Sopenharmony_ci &ambient_new_raw, &ambient_old_raw, 6688c2ecf20Sopenharmony_ci &object_new_raw, &object_old_raw); 6698c2ecf20Sopenharmony_ci if (ret < 0) 6708c2ecf20Sopenharmony_ci return ret; 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_ci if (object_new_raw > MLX90632_EXTENDED_LIMIT && 6738c2ecf20Sopenharmony_ci data->mtyp == MLX90632_MTYP_EXTENDED) { 6748c2ecf20Sopenharmony_ci ret = mlx90632_read_all_channel_extended(data, &object_new_raw, 6758c2ecf20Sopenharmony_ci &ambient_new_raw, &ambient_old_raw); 6768c2ecf20Sopenharmony_ci if (ret < 0) 6778c2ecf20Sopenharmony_ci return ret; 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ci /* Use extended mode calculations */ 6808c2ecf20Sopenharmony_ci ambient = mlx90632_preprocess_temp_amb(ambient_new_raw, 6818c2ecf20Sopenharmony_ci ambient_old_raw, Gb); 6828c2ecf20Sopenharmony_ci object = mlx90632_preprocess_temp_obj_extended(object_new_raw, 6838c2ecf20Sopenharmony_ci ambient_new_raw, 6848c2ecf20Sopenharmony_ci ambient_old_raw, Ka); 6858c2ecf20Sopenharmony_ci *val = mlx90632_calc_temp_object_extended(object, ambient, 6868c2ecf20Sopenharmony_ci data->object_ambient_temperature, 6878c2ecf20Sopenharmony_ci Ea, Eb, Fa, Fb, Ga, 6888c2ecf20Sopenharmony_ci Ha, Hb, data->emissivity); 6898c2ecf20Sopenharmony_ci return 0; 6908c2ecf20Sopenharmony_ci } 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci ambient = mlx90632_preprocess_temp_amb(ambient_new_raw, 6938c2ecf20Sopenharmony_ci ambient_old_raw, Gb); 6948c2ecf20Sopenharmony_ci object = mlx90632_preprocess_temp_obj(object_new_raw, 6958c2ecf20Sopenharmony_ci object_old_raw, 6968c2ecf20Sopenharmony_ci ambient_new_raw, 6978c2ecf20Sopenharmony_ci ambient_old_raw, Ka); 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_ci *val = mlx90632_calc_temp_object(object, ambient, Ea, Eb, Fa, Fb, Ga, 7008c2ecf20Sopenharmony_ci Ha, Hb, data->emissivity); 7018c2ecf20Sopenharmony_ci return 0; 7028c2ecf20Sopenharmony_ci} 7038c2ecf20Sopenharmony_ci 7048c2ecf20Sopenharmony_cistatic int mlx90632_calc_ambient_dsp105(struct mlx90632_data *data, int *val) 7058c2ecf20Sopenharmony_ci{ 7068c2ecf20Sopenharmony_ci s32 ret; 7078c2ecf20Sopenharmony_ci unsigned int read_tmp; 7088c2ecf20Sopenharmony_ci s32 PT, PR, PG, PO; 7098c2ecf20Sopenharmony_ci s16 Gb; 7108c2ecf20Sopenharmony_ci s16 ambient_new_raw, ambient_old_raw; 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_ci ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_R, &PR); 7138c2ecf20Sopenharmony_ci if (ret < 0) 7148c2ecf20Sopenharmony_ci return ret; 7158c2ecf20Sopenharmony_ci ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_G, &PG); 7168c2ecf20Sopenharmony_ci if (ret < 0) 7178c2ecf20Sopenharmony_ci return ret; 7188c2ecf20Sopenharmony_ci ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_T, &PT); 7198c2ecf20Sopenharmony_ci if (ret < 0) 7208c2ecf20Sopenharmony_ci return ret; 7218c2ecf20Sopenharmony_ci ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_O, &PO); 7228c2ecf20Sopenharmony_ci if (ret < 0) 7238c2ecf20Sopenharmony_ci return ret; 7248c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MLX90632_EE_Gb, &read_tmp); 7258c2ecf20Sopenharmony_ci if (ret < 0) 7268c2ecf20Sopenharmony_ci return ret; 7278c2ecf20Sopenharmony_ci Gb = (s16)read_tmp; 7288c2ecf20Sopenharmony_ci 7298c2ecf20Sopenharmony_ci ret = mlx90632_read_ambient_raw(data->regmap, &ambient_new_raw, 7308c2ecf20Sopenharmony_ci &ambient_old_raw); 7318c2ecf20Sopenharmony_ci if (ret < 0) 7328c2ecf20Sopenharmony_ci return ret; 7338c2ecf20Sopenharmony_ci *val = mlx90632_calc_temp_ambient(ambient_new_raw, ambient_old_raw, 7348c2ecf20Sopenharmony_ci PT, PR, PG, PO, Gb); 7358c2ecf20Sopenharmony_ci return ret; 7368c2ecf20Sopenharmony_ci} 7378c2ecf20Sopenharmony_ci 7388c2ecf20Sopenharmony_cistatic int mlx90632_read_raw(struct iio_dev *indio_dev, 7398c2ecf20Sopenharmony_ci struct iio_chan_spec const *channel, int *val, 7408c2ecf20Sopenharmony_ci int *val2, long mask) 7418c2ecf20Sopenharmony_ci{ 7428c2ecf20Sopenharmony_ci struct mlx90632_data *data = iio_priv(indio_dev); 7438c2ecf20Sopenharmony_ci int ret; 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_ci switch (mask) { 7468c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_PROCESSED: 7478c2ecf20Sopenharmony_ci switch (channel->channel2) { 7488c2ecf20Sopenharmony_ci case IIO_MOD_TEMP_AMBIENT: 7498c2ecf20Sopenharmony_ci ret = mlx90632_calc_ambient_dsp105(data, val); 7508c2ecf20Sopenharmony_ci if (ret < 0) 7518c2ecf20Sopenharmony_ci return ret; 7528c2ecf20Sopenharmony_ci return IIO_VAL_INT; 7538c2ecf20Sopenharmony_ci case IIO_MOD_TEMP_OBJECT: 7548c2ecf20Sopenharmony_ci ret = mlx90632_calc_object_dsp105(data, val); 7558c2ecf20Sopenharmony_ci if (ret < 0) 7568c2ecf20Sopenharmony_ci return ret; 7578c2ecf20Sopenharmony_ci return IIO_VAL_INT; 7588c2ecf20Sopenharmony_ci default: 7598c2ecf20Sopenharmony_ci return -EINVAL; 7608c2ecf20Sopenharmony_ci } 7618c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_CALIBEMISSIVITY: 7628c2ecf20Sopenharmony_ci if (data->emissivity == 1000) { 7638c2ecf20Sopenharmony_ci *val = 1; 7648c2ecf20Sopenharmony_ci *val2 = 0; 7658c2ecf20Sopenharmony_ci } else { 7668c2ecf20Sopenharmony_ci *val = 0; 7678c2ecf20Sopenharmony_ci *val2 = data->emissivity * 1000; 7688c2ecf20Sopenharmony_ci } 7698c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 7708c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_CALIBAMBIENT: 7718c2ecf20Sopenharmony_ci *val = data->object_ambient_temperature; 7728c2ecf20Sopenharmony_ci return IIO_VAL_INT; 7738c2ecf20Sopenharmony_ci default: 7748c2ecf20Sopenharmony_ci return -EINVAL; 7758c2ecf20Sopenharmony_ci } 7768c2ecf20Sopenharmony_ci} 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_cistatic int mlx90632_write_raw(struct iio_dev *indio_dev, 7798c2ecf20Sopenharmony_ci struct iio_chan_spec const *channel, int val, 7808c2ecf20Sopenharmony_ci int val2, long mask) 7818c2ecf20Sopenharmony_ci{ 7828c2ecf20Sopenharmony_ci struct mlx90632_data *data = iio_priv(indio_dev); 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci switch (mask) { 7858c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_CALIBEMISSIVITY: 7868c2ecf20Sopenharmony_ci /* Confirm we are within 0 and 1.0 */ 7878c2ecf20Sopenharmony_ci if (val < 0 || val2 < 0 || val > 1 || 7888c2ecf20Sopenharmony_ci (val == 1 && val2 != 0)) 7898c2ecf20Sopenharmony_ci return -EINVAL; 7908c2ecf20Sopenharmony_ci data->emissivity = val * 1000 + val2 / 1000; 7918c2ecf20Sopenharmony_ci return 0; 7928c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_CALIBAMBIENT: 7938c2ecf20Sopenharmony_ci data->object_ambient_temperature = val; 7948c2ecf20Sopenharmony_ci return 0; 7958c2ecf20Sopenharmony_ci default: 7968c2ecf20Sopenharmony_ci return -EINVAL; 7978c2ecf20Sopenharmony_ci } 7988c2ecf20Sopenharmony_ci} 7998c2ecf20Sopenharmony_ci 8008c2ecf20Sopenharmony_cistatic const struct iio_chan_spec mlx90632_channels[] = { 8018c2ecf20Sopenharmony_ci { 8028c2ecf20Sopenharmony_ci .type = IIO_TEMP, 8038c2ecf20Sopenharmony_ci .modified = 1, 8048c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_TEMP_AMBIENT, 8058c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 8068c2ecf20Sopenharmony_ci }, 8078c2ecf20Sopenharmony_ci { 8088c2ecf20Sopenharmony_ci .type = IIO_TEMP, 8098c2ecf20Sopenharmony_ci .modified = 1, 8108c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_TEMP_OBJECT, 8118c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 8128c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_CALIBEMISSIVITY) | BIT(IIO_CHAN_INFO_CALIBAMBIENT), 8138c2ecf20Sopenharmony_ci }, 8148c2ecf20Sopenharmony_ci}; 8158c2ecf20Sopenharmony_ci 8168c2ecf20Sopenharmony_cistatic const struct iio_info mlx90632_info = { 8178c2ecf20Sopenharmony_ci .read_raw = mlx90632_read_raw, 8188c2ecf20Sopenharmony_ci .write_raw = mlx90632_write_raw, 8198c2ecf20Sopenharmony_ci}; 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_cistatic int mlx90632_sleep(struct mlx90632_data *data) 8228c2ecf20Sopenharmony_ci{ 8238c2ecf20Sopenharmony_ci regcache_mark_dirty(data->regmap); 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ci dev_dbg(&data->client->dev, "Requesting sleep"); 8268c2ecf20Sopenharmony_ci return mlx90632_pwr_set_sleep_step(data->regmap); 8278c2ecf20Sopenharmony_ci} 8288c2ecf20Sopenharmony_ci 8298c2ecf20Sopenharmony_cistatic int mlx90632_wakeup(struct mlx90632_data *data) 8308c2ecf20Sopenharmony_ci{ 8318c2ecf20Sopenharmony_ci int ret; 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_ci ret = regcache_sync(data->regmap); 8348c2ecf20Sopenharmony_ci if (ret < 0) { 8358c2ecf20Sopenharmony_ci dev_err(&data->client->dev, 8368c2ecf20Sopenharmony_ci "Failed to sync regmap registers: %d\n", ret); 8378c2ecf20Sopenharmony_ci return ret; 8388c2ecf20Sopenharmony_ci } 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci dev_dbg(&data->client->dev, "Requesting wake-up\n"); 8418c2ecf20Sopenharmony_ci return mlx90632_pwr_continuous(data->regmap); 8428c2ecf20Sopenharmony_ci} 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_cistatic int mlx90632_probe(struct i2c_client *client, 8458c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 8468c2ecf20Sopenharmony_ci{ 8478c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 8488c2ecf20Sopenharmony_ci struct mlx90632_data *mlx90632; 8498c2ecf20Sopenharmony_ci struct regmap *regmap; 8508c2ecf20Sopenharmony_ci int ret; 8518c2ecf20Sopenharmony_ci unsigned int read; 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mlx90632)); 8548c2ecf20Sopenharmony_ci if (!indio_dev) { 8558c2ecf20Sopenharmony_ci dev_err(&client->dev, "Failed to allocate device\n"); 8568c2ecf20Sopenharmony_ci return -ENOMEM; 8578c2ecf20Sopenharmony_ci } 8588c2ecf20Sopenharmony_ci 8598c2ecf20Sopenharmony_ci regmap = devm_regmap_init_i2c(client, &mlx90632_regmap); 8608c2ecf20Sopenharmony_ci if (IS_ERR(regmap)) { 8618c2ecf20Sopenharmony_ci ret = PTR_ERR(regmap); 8628c2ecf20Sopenharmony_ci dev_err(&client->dev, "Failed to allocate regmap: %d\n", ret); 8638c2ecf20Sopenharmony_ci return ret; 8648c2ecf20Sopenharmony_ci } 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci mlx90632 = iio_priv(indio_dev); 8678c2ecf20Sopenharmony_ci i2c_set_clientdata(client, indio_dev); 8688c2ecf20Sopenharmony_ci mlx90632->client = client; 8698c2ecf20Sopenharmony_ci mlx90632->regmap = regmap; 8708c2ecf20Sopenharmony_ci mlx90632->mtyp = MLX90632_MTYP_MEDICAL; 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_ci mutex_init(&mlx90632->lock); 8738c2ecf20Sopenharmony_ci indio_dev->name = id->name; 8748c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 8758c2ecf20Sopenharmony_ci indio_dev->info = &mlx90632_info; 8768c2ecf20Sopenharmony_ci indio_dev->channels = mlx90632_channels; 8778c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(mlx90632_channels); 8788c2ecf20Sopenharmony_ci 8798c2ecf20Sopenharmony_ci ret = mlx90632_wakeup(mlx90632); 8808c2ecf20Sopenharmony_ci if (ret < 0) { 8818c2ecf20Sopenharmony_ci dev_err(&client->dev, "Wakeup failed: %d\n", ret); 8828c2ecf20Sopenharmony_ci return ret; 8838c2ecf20Sopenharmony_ci } 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci ret = regmap_read(mlx90632->regmap, MLX90632_EE_VERSION, &read); 8868c2ecf20Sopenharmony_ci if (ret < 0) { 8878c2ecf20Sopenharmony_ci dev_err(&client->dev, "read of version failed: %d\n", ret); 8888c2ecf20Sopenharmony_ci return ret; 8898c2ecf20Sopenharmony_ci } 8908c2ecf20Sopenharmony_ci read = read & MLX90632_ID_MASK; 8918c2ecf20Sopenharmony_ci if (read == MLX90632_ID_MEDICAL) { 8928c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 8938c2ecf20Sopenharmony_ci "Detected Medical EEPROM calibration %x\n", read); 8948c2ecf20Sopenharmony_ci } else if (read == MLX90632_ID_CONSUMER) { 8958c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 8968c2ecf20Sopenharmony_ci "Detected Consumer EEPROM calibration %x\n", read); 8978c2ecf20Sopenharmony_ci } else if (read == MLX90632_ID_EXTENDED) { 8988c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 8998c2ecf20Sopenharmony_ci "Detected Extended range EEPROM calibration %x\n", read); 9008c2ecf20Sopenharmony_ci mlx90632->mtyp = MLX90632_MTYP_EXTENDED; 9018c2ecf20Sopenharmony_ci } else if ((read & MLX90632_DSP_MASK) == MLX90632_DSP_VERSION) { 9028c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 9038c2ecf20Sopenharmony_ci "Detected Unknown EEPROM calibration %x\n", read); 9048c2ecf20Sopenharmony_ci } else { 9058c2ecf20Sopenharmony_ci dev_err(&client->dev, 9068c2ecf20Sopenharmony_ci "Wrong DSP version %x (expected %x)\n", 9078c2ecf20Sopenharmony_ci read, MLX90632_DSP_VERSION); 9088c2ecf20Sopenharmony_ci return -EPROTONOSUPPORT; 9098c2ecf20Sopenharmony_ci } 9108c2ecf20Sopenharmony_ci 9118c2ecf20Sopenharmony_ci mlx90632->emissivity = 1000; 9128c2ecf20Sopenharmony_ci mlx90632->object_ambient_temperature = 25000; /* 25 degrees milliCelsius */ 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci pm_runtime_disable(&client->dev); 9158c2ecf20Sopenharmony_ci ret = pm_runtime_set_active(&client->dev); 9168c2ecf20Sopenharmony_ci if (ret < 0) { 9178c2ecf20Sopenharmony_ci mlx90632_sleep(mlx90632); 9188c2ecf20Sopenharmony_ci return ret; 9198c2ecf20Sopenharmony_ci } 9208c2ecf20Sopenharmony_ci pm_runtime_enable(&client->dev); 9218c2ecf20Sopenharmony_ci pm_runtime_set_autosuspend_delay(&client->dev, MLX90632_SLEEP_DELAY_MS); 9228c2ecf20Sopenharmony_ci pm_runtime_use_autosuspend(&client->dev); 9238c2ecf20Sopenharmony_ci 9248c2ecf20Sopenharmony_ci return iio_device_register(indio_dev); 9258c2ecf20Sopenharmony_ci} 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_cistatic int mlx90632_remove(struct i2c_client *client) 9288c2ecf20Sopenharmony_ci{ 9298c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(client); 9308c2ecf20Sopenharmony_ci struct mlx90632_data *data = iio_priv(indio_dev); 9318c2ecf20Sopenharmony_ci 9328c2ecf20Sopenharmony_ci iio_device_unregister(indio_dev); 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_ci pm_runtime_disable(&client->dev); 9358c2ecf20Sopenharmony_ci pm_runtime_set_suspended(&client->dev); 9368c2ecf20Sopenharmony_ci pm_runtime_put_noidle(&client->dev); 9378c2ecf20Sopenharmony_ci 9388c2ecf20Sopenharmony_ci mlx90632_sleep(data); 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_ci return 0; 9418c2ecf20Sopenharmony_ci} 9428c2ecf20Sopenharmony_ci 9438c2ecf20Sopenharmony_cistatic const struct i2c_device_id mlx90632_id[] = { 9448c2ecf20Sopenharmony_ci { "mlx90632", 0 }, 9458c2ecf20Sopenharmony_ci { } 9468c2ecf20Sopenharmony_ci}; 9478c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, mlx90632_id); 9488c2ecf20Sopenharmony_ci 9498c2ecf20Sopenharmony_cistatic const struct of_device_id mlx90632_of_match[] = { 9508c2ecf20Sopenharmony_ci { .compatible = "melexis,mlx90632" }, 9518c2ecf20Sopenharmony_ci { } 9528c2ecf20Sopenharmony_ci}; 9538c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mlx90632_of_match); 9548c2ecf20Sopenharmony_ci 9558c2ecf20Sopenharmony_cistatic int __maybe_unused mlx90632_pm_suspend(struct device *dev) 9568c2ecf20Sopenharmony_ci{ 9578c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 9588c2ecf20Sopenharmony_ci struct mlx90632_data *data = iio_priv(indio_dev); 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci return mlx90632_sleep(data); 9618c2ecf20Sopenharmony_ci} 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_cistatic int __maybe_unused mlx90632_pm_resume(struct device *dev) 9648c2ecf20Sopenharmony_ci{ 9658c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 9668c2ecf20Sopenharmony_ci struct mlx90632_data *data = iio_priv(indio_dev); 9678c2ecf20Sopenharmony_ci 9688c2ecf20Sopenharmony_ci return mlx90632_wakeup(data); 9698c2ecf20Sopenharmony_ci} 9708c2ecf20Sopenharmony_ci 9718c2ecf20Sopenharmony_cistatic UNIVERSAL_DEV_PM_OPS(mlx90632_pm_ops, mlx90632_pm_suspend, 9728c2ecf20Sopenharmony_ci mlx90632_pm_resume, NULL); 9738c2ecf20Sopenharmony_ci 9748c2ecf20Sopenharmony_cistatic struct i2c_driver mlx90632_driver = { 9758c2ecf20Sopenharmony_ci .driver = { 9768c2ecf20Sopenharmony_ci .name = "mlx90632", 9778c2ecf20Sopenharmony_ci .of_match_table = mlx90632_of_match, 9788c2ecf20Sopenharmony_ci .pm = &mlx90632_pm_ops, 9798c2ecf20Sopenharmony_ci }, 9808c2ecf20Sopenharmony_ci .probe = mlx90632_probe, 9818c2ecf20Sopenharmony_ci .remove = mlx90632_remove, 9828c2ecf20Sopenharmony_ci .id_table = mlx90632_id, 9838c2ecf20Sopenharmony_ci}; 9848c2ecf20Sopenharmony_cimodule_i2c_driver(mlx90632_driver); 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_ciMODULE_AUTHOR("Crt Mori <cmo@melexis.com>"); 9878c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Melexis MLX90632 contactless Infra Red temperature sensor driver"); 9888c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 989