Crt Mori | c87742a | 2018-01-11 11:20:23 +0100 | [diff] [blame] | 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/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/math64.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/pm_runtime.h> |
| 18 | #include <linux/regmap.h> |
| 19 | |
| 20 | #include <linux/iio/iio.h> |
| 21 | #include <linux/iio/sysfs.h> |
| 22 | |
| 23 | /* Memory sections addresses */ |
| 24 | #define MLX90632_ADDR_RAM 0x4000 /* Start address of ram */ |
| 25 | #define MLX90632_ADDR_EEPROM 0x2480 /* Start address of user eeprom */ |
| 26 | |
| 27 | /* EEPROM addresses - used at startup */ |
| 28 | #define MLX90632_EE_CTRL 0x24d4 /* Control register initial value */ |
| 29 | #define MLX90632_EE_I2C_ADDR 0x24d5 /* I2C address register initial value */ |
| 30 | #define MLX90632_EE_VERSION 0x240b /* EEPROM version reg address */ |
| 31 | #define MLX90632_EE_P_R 0x240c /* P_R calibration register 32bit */ |
| 32 | #define MLX90632_EE_P_G 0x240e /* P_G calibration register 32bit */ |
| 33 | #define MLX90632_EE_P_T 0x2410 /* P_T calibration register 32bit */ |
| 34 | #define MLX90632_EE_P_O 0x2412 /* P_O calibration register 32bit */ |
| 35 | #define MLX90632_EE_Aa 0x2414 /* Aa calibration register 32bit */ |
| 36 | #define MLX90632_EE_Ab 0x2416 /* Ab calibration register 32bit */ |
| 37 | #define MLX90632_EE_Ba 0x2418 /* Ba calibration register 32bit */ |
| 38 | #define MLX90632_EE_Bb 0x241a /* Bb calibration register 32bit */ |
| 39 | #define MLX90632_EE_Ca 0x241c /* Ca calibration register 32bit */ |
| 40 | #define MLX90632_EE_Cb 0x241e /* Cb calibration register 32bit */ |
| 41 | #define MLX90632_EE_Da 0x2420 /* Da calibration register 32bit */ |
| 42 | #define MLX90632_EE_Db 0x2422 /* Db calibration register 32bit */ |
| 43 | #define MLX90632_EE_Ea 0x2424 /* Ea calibration register 32bit */ |
| 44 | #define MLX90632_EE_Eb 0x2426 /* Eb calibration register 32bit */ |
| 45 | #define MLX90632_EE_Fa 0x2428 /* Fa calibration register 32bit */ |
| 46 | #define MLX90632_EE_Fb 0x242a /* Fb calibration register 32bit */ |
| 47 | #define MLX90632_EE_Ga 0x242c /* Ga calibration register 32bit */ |
| 48 | |
| 49 | #define MLX90632_EE_Gb 0x242e /* Gb calibration register 16bit */ |
| 50 | #define MLX90632_EE_Ka 0x242f /* Ka calibration register 16bit */ |
| 51 | |
| 52 | #define MLX90632_EE_Ha 0x2481 /* Ha customer calib value reg 16bit */ |
| 53 | #define MLX90632_EE_Hb 0x2482 /* Hb customer calib value reg 16bit */ |
| 54 | |
| 55 | /* Register addresses - volatile */ |
| 56 | #define MLX90632_REG_I2C_ADDR 0x3000 /* Chip I2C address register */ |
| 57 | |
| 58 | /* Control register address - volatile */ |
| 59 | #define MLX90632_REG_CONTROL 0x3001 /* Control Register address */ |
| 60 | #define MLX90632_CFG_PWR_MASK GENMASK(2, 1) /* PowerMode Mask */ |
| 61 | /* PowerModes statuses */ |
| 62 | #define MLX90632_PWR_STATUS(ctrl_val) (ctrl_val << 1) |
| 63 | #define MLX90632_PWR_STATUS_HALT MLX90632_PWR_STATUS(0) /* hold */ |
| 64 | #define MLX90632_PWR_STATUS_SLEEP_STEP MLX90632_PWR_STATUS(1) /* sleep step*/ |
| 65 | #define MLX90632_PWR_STATUS_STEP MLX90632_PWR_STATUS(2) /* step */ |
| 66 | #define MLX90632_PWR_STATUS_CONTINUOUS MLX90632_PWR_STATUS(3) /* continuous*/ |
| 67 | |
| 68 | /* Device status register - volatile */ |
| 69 | #define MLX90632_REG_STATUS 0x3fff /* Device status register */ |
| 70 | #define MLX90632_STAT_BUSY BIT(10) /* Device busy indicator */ |
| 71 | #define MLX90632_STAT_EE_BUSY BIT(9) /* EEPROM busy indicator */ |
| 72 | #define MLX90632_STAT_BRST BIT(8) /* Brown out reset indicator */ |
| 73 | #define MLX90632_STAT_CYCLE_POS GENMASK(6, 2) /* Data position */ |
| 74 | #define MLX90632_STAT_DATA_RDY BIT(0) /* Data ready indicator */ |
| 75 | |
| 76 | /* RAM_MEAS address-es for each channel */ |
| 77 | #define MLX90632_RAM_1(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num) |
| 78 | #define MLX90632_RAM_2(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 1) |
| 79 | #define MLX90632_RAM_3(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 2) |
| 80 | |
| 81 | /* Magic constants */ |
| 82 | #define MLX90632_ID_MEDICAL 0x0105 /* EEPROM DSPv5 Medical device id */ |
| 83 | #define MLX90632_ID_CONSUMER 0x0205 /* EEPROM DSPv5 Consumer device id */ |
Crt Mori | 389fc70 | 2019-05-23 14:07:22 +0200 | [diff] [blame] | 84 | #define MLX90632_DSP_VERSION 5 /* DSP version */ |
| 85 | #define MLX90632_DSP_MASK GENMASK(7, 0) /* DSP version in EE_VERSION */ |
Crt Mori | c87742a | 2018-01-11 11:20:23 +0100 | [diff] [blame] | 86 | #define MLX90632_RESET_CMD 0x0006 /* Reset sensor (address or global) */ |
| 87 | #define MLX90632_REF_12 12LL /**< ResCtrlRef value of Ch 1 or Ch 2 */ |
| 88 | #define MLX90632_REF_3 12LL /**< ResCtrlRef value of Channel 3 */ |
| 89 | #define MLX90632_MAX_MEAS_NUM 31 /**< Maximum measurements in list */ |
| 90 | #define MLX90632_SLEEP_DELAY_MS 3000 /**< Autosleep delay */ |
| 91 | |
| 92 | struct mlx90632_data { |
| 93 | struct i2c_client *client; |
| 94 | struct mutex lock; /* Multiple reads for single measurement */ |
| 95 | struct regmap *regmap; |
| 96 | u16 emissivity; |
| 97 | }; |
| 98 | |
| 99 | static const struct regmap_range mlx90632_volatile_reg_range[] = { |
| 100 | regmap_reg_range(MLX90632_REG_I2C_ADDR, MLX90632_REG_CONTROL), |
| 101 | regmap_reg_range(MLX90632_REG_STATUS, MLX90632_REG_STATUS), |
| 102 | regmap_reg_range(MLX90632_RAM_1(0), |
| 103 | MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), |
| 104 | }; |
| 105 | |
| 106 | static const struct regmap_access_table mlx90632_volatile_regs_tbl = { |
| 107 | .yes_ranges = mlx90632_volatile_reg_range, |
| 108 | .n_yes_ranges = ARRAY_SIZE(mlx90632_volatile_reg_range), |
| 109 | }; |
| 110 | |
| 111 | static const struct regmap_range mlx90632_read_reg_range[] = { |
| 112 | regmap_reg_range(MLX90632_EE_VERSION, MLX90632_EE_Ka), |
| 113 | regmap_reg_range(MLX90632_EE_CTRL, MLX90632_EE_I2C_ADDR), |
| 114 | regmap_reg_range(MLX90632_EE_Ha, MLX90632_EE_Hb), |
| 115 | regmap_reg_range(MLX90632_REG_I2C_ADDR, MLX90632_REG_CONTROL), |
| 116 | regmap_reg_range(MLX90632_REG_STATUS, MLX90632_REG_STATUS), |
| 117 | regmap_reg_range(MLX90632_RAM_1(0), |
| 118 | MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), |
| 119 | }; |
| 120 | |
| 121 | static const struct regmap_access_table mlx90632_readable_regs_tbl = { |
| 122 | .yes_ranges = mlx90632_read_reg_range, |
| 123 | .n_yes_ranges = ARRAY_SIZE(mlx90632_read_reg_range), |
| 124 | }; |
| 125 | |
| 126 | static const struct regmap_range mlx90632_no_write_reg_range[] = { |
| 127 | regmap_reg_range(MLX90632_EE_VERSION, MLX90632_EE_Ka), |
| 128 | regmap_reg_range(MLX90632_RAM_1(0), |
| 129 | MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)), |
| 130 | }; |
| 131 | |
| 132 | static const struct regmap_access_table mlx90632_writeable_regs_tbl = { |
| 133 | .no_ranges = mlx90632_no_write_reg_range, |
| 134 | .n_no_ranges = ARRAY_SIZE(mlx90632_no_write_reg_range), |
| 135 | }; |
| 136 | |
| 137 | static const struct regmap_config mlx90632_regmap = { |
| 138 | .reg_bits = 16, |
| 139 | .val_bits = 16, |
| 140 | |
| 141 | .volatile_table = &mlx90632_volatile_regs_tbl, |
| 142 | .rd_table = &mlx90632_readable_regs_tbl, |
| 143 | .wr_table = &mlx90632_writeable_regs_tbl, |
| 144 | |
David Frey | 1c96a2f | 2018-09-01 09:50:41 -0700 | [diff] [blame] | 145 | .use_single_read = true, |
| 146 | .use_single_write = true, |
Crt Mori | c87742a | 2018-01-11 11:20:23 +0100 | [diff] [blame] | 147 | .reg_format_endian = REGMAP_ENDIAN_BIG, |
| 148 | .val_format_endian = REGMAP_ENDIAN_BIG, |
| 149 | .cache_type = REGCACHE_RBTREE, |
| 150 | }; |
| 151 | |
| 152 | static s32 mlx90632_pwr_set_sleep_step(struct regmap *regmap) |
| 153 | { |
| 154 | return regmap_update_bits(regmap, MLX90632_REG_CONTROL, |
| 155 | MLX90632_CFG_PWR_MASK, |
| 156 | MLX90632_PWR_STATUS_SLEEP_STEP); |
| 157 | } |
| 158 | |
| 159 | static s32 mlx90632_pwr_continuous(struct regmap *regmap) |
| 160 | { |
| 161 | return regmap_update_bits(regmap, MLX90632_REG_CONTROL, |
| 162 | MLX90632_CFG_PWR_MASK, |
| 163 | MLX90632_PWR_STATUS_CONTINUOUS); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * mlx90632_perform_measurement - Trigger and retrieve current measurement cycle |
| 168 | * @*data: pointer to mlx90632_data object containing regmap information |
| 169 | * |
| 170 | * Perform a measurement and return latest measurement cycle position reported |
| 171 | * by sensor. This is a blocking function for 500ms, as that is default sensor |
| 172 | * refresh rate. |
| 173 | */ |
| 174 | static int mlx90632_perform_measurement(struct mlx90632_data *data) |
| 175 | { |
| 176 | int ret, tries = 100; |
| 177 | unsigned int reg_status; |
| 178 | |
| 179 | ret = regmap_update_bits(data->regmap, MLX90632_REG_STATUS, |
| 180 | MLX90632_STAT_DATA_RDY, 0); |
| 181 | if (ret < 0) |
| 182 | return ret; |
| 183 | |
| 184 | while (tries-- > 0) { |
| 185 | ret = regmap_read(data->regmap, MLX90632_REG_STATUS, |
| 186 | ®_status); |
| 187 | if (ret < 0) |
| 188 | return ret; |
| 189 | if (reg_status & MLX90632_STAT_DATA_RDY) |
| 190 | break; |
| 191 | usleep_range(10000, 11000); |
| 192 | } |
| 193 | |
| 194 | if (tries < 0) { |
| 195 | dev_err(&data->client->dev, "data not ready"); |
| 196 | return -ETIMEDOUT; |
| 197 | } |
| 198 | |
| 199 | return (reg_status & MLX90632_STAT_CYCLE_POS) >> 2; |
| 200 | } |
| 201 | |
| 202 | static int mlx90632_channel_new_select(int perform_ret, uint8_t *channel_new, |
| 203 | uint8_t *channel_old) |
| 204 | { |
| 205 | switch (perform_ret) { |
| 206 | case 1: |
| 207 | *channel_new = 1; |
| 208 | *channel_old = 2; |
| 209 | break; |
| 210 | case 2: |
| 211 | *channel_new = 2; |
| 212 | *channel_old = 1; |
| 213 | break; |
| 214 | default: |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int mlx90632_read_ambient_raw(struct regmap *regmap, |
| 222 | s16 *ambient_new_raw, s16 *ambient_old_raw) |
| 223 | { |
| 224 | int ret; |
| 225 | unsigned int read_tmp; |
| 226 | |
| 227 | ret = regmap_read(regmap, MLX90632_RAM_3(1), &read_tmp); |
| 228 | if (ret < 0) |
| 229 | return ret; |
| 230 | *ambient_new_raw = (s16)read_tmp; |
| 231 | |
| 232 | ret = regmap_read(regmap, MLX90632_RAM_3(2), &read_tmp); |
| 233 | if (ret < 0) |
| 234 | return ret; |
| 235 | *ambient_old_raw = (s16)read_tmp; |
| 236 | |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | static int mlx90632_read_object_raw(struct regmap *regmap, |
| 241 | int perform_measurement_ret, |
| 242 | s16 *object_new_raw, s16 *object_old_raw) |
| 243 | { |
| 244 | int ret; |
| 245 | unsigned int read_tmp; |
| 246 | s16 read; |
| 247 | u8 channel = 0; |
| 248 | u8 channel_old = 0; |
| 249 | |
| 250 | ret = mlx90632_channel_new_select(perform_measurement_ret, &channel, |
| 251 | &channel_old); |
| 252 | if (ret != 0) |
| 253 | return ret; |
| 254 | |
| 255 | ret = regmap_read(regmap, MLX90632_RAM_2(channel), &read_tmp); |
| 256 | if (ret < 0) |
| 257 | return ret; |
| 258 | |
| 259 | read = (s16)read_tmp; |
| 260 | |
| 261 | ret = regmap_read(regmap, MLX90632_RAM_1(channel), &read_tmp); |
| 262 | if (ret < 0) |
| 263 | return ret; |
| 264 | *object_new_raw = (read + (s16)read_tmp) / 2; |
| 265 | |
| 266 | ret = regmap_read(regmap, MLX90632_RAM_2(channel_old), &read_tmp); |
| 267 | if (ret < 0) |
| 268 | return ret; |
| 269 | read = (s16)read_tmp; |
| 270 | |
| 271 | ret = regmap_read(regmap, MLX90632_RAM_1(channel_old), &read_tmp); |
| 272 | if (ret < 0) |
| 273 | return ret; |
| 274 | *object_old_raw = (read + (s16)read_tmp) / 2; |
| 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | static int mlx90632_read_all_channel(struct mlx90632_data *data, |
| 280 | s16 *ambient_new_raw, s16 *ambient_old_raw, |
| 281 | s16 *object_new_raw, s16 *object_old_raw) |
| 282 | { |
| 283 | s32 ret, measurement; |
| 284 | |
| 285 | mutex_lock(&data->lock); |
| 286 | measurement = mlx90632_perform_measurement(data); |
| 287 | if (measurement < 0) { |
| 288 | ret = measurement; |
| 289 | goto read_unlock; |
| 290 | } |
| 291 | ret = mlx90632_read_ambient_raw(data->regmap, ambient_new_raw, |
| 292 | ambient_old_raw); |
| 293 | if (ret < 0) |
| 294 | goto read_unlock; |
| 295 | |
| 296 | ret = mlx90632_read_object_raw(data->regmap, measurement, |
| 297 | object_new_raw, object_old_raw); |
| 298 | read_unlock: |
| 299 | mutex_unlock(&data->lock); |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | static int mlx90632_read_ee_register(struct regmap *regmap, u16 reg_lsb, |
| 304 | s32 *reg_value) |
| 305 | { |
| 306 | s32 ret; |
| 307 | unsigned int read; |
| 308 | u32 value; |
| 309 | |
| 310 | ret = regmap_read(regmap, reg_lsb, &read); |
| 311 | if (ret < 0) |
| 312 | return ret; |
| 313 | |
| 314 | value = read; |
| 315 | |
| 316 | ret = regmap_read(regmap, reg_lsb + 1, &read); |
| 317 | if (ret < 0) |
| 318 | return ret; |
| 319 | |
| 320 | *reg_value = (read << 16) | (value & 0xffff); |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static s64 mlx90632_preprocess_temp_amb(s16 ambient_new_raw, |
| 326 | s16 ambient_old_raw, s16 Gb) |
| 327 | { |
| 328 | s64 VR_Ta, kGb, tmp; |
| 329 | |
| 330 | kGb = ((s64)Gb * 1000LL) >> 10ULL; |
| 331 | VR_Ta = (s64)ambient_old_raw * 1000000LL + |
| 332 | kGb * div64_s64(((s64)ambient_new_raw * 1000LL), |
| 333 | (MLX90632_REF_3)); |
| 334 | tmp = div64_s64( |
| 335 | div64_s64(((s64)ambient_new_raw * 1000000000000LL), |
| 336 | (MLX90632_REF_3)), VR_Ta); |
| 337 | return div64_s64(tmp << 19ULL, 1000LL); |
| 338 | } |
| 339 | |
| 340 | static s64 mlx90632_preprocess_temp_obj(s16 object_new_raw, s16 object_old_raw, |
| 341 | s16 ambient_new_raw, |
| 342 | s16 ambient_old_raw, s16 Ka) |
| 343 | { |
| 344 | s64 VR_IR, kKa, tmp; |
| 345 | |
| 346 | kKa = ((s64)Ka * 1000LL) >> 10ULL; |
| 347 | VR_IR = (s64)ambient_old_raw * 1000000LL + |
| 348 | kKa * div64_s64(((s64)ambient_new_raw * 1000LL), |
| 349 | (MLX90632_REF_3)); |
| 350 | tmp = div64_s64( |
| 351 | div64_s64(((s64)((object_new_raw + object_old_raw) / 2) |
| 352 | * 1000000000000LL), (MLX90632_REF_12)), |
| 353 | VR_IR); |
| 354 | return div64_s64((tmp << 19ULL), 1000LL); |
| 355 | } |
| 356 | |
| 357 | static s32 mlx90632_calc_temp_ambient(s16 ambient_new_raw, s16 ambient_old_raw, |
| 358 | s32 P_T, s32 P_R, s32 P_G, s32 P_O, |
| 359 | s16 Gb) |
| 360 | { |
| 361 | s64 Asub, Bsub, Ablock, Bblock, Cblock, AMB, sum; |
| 362 | |
| 363 | AMB = mlx90632_preprocess_temp_amb(ambient_new_raw, ambient_old_raw, |
| 364 | Gb); |
| 365 | Asub = ((s64)P_T * 10000000000LL) >> 44ULL; |
| 366 | Bsub = AMB - (((s64)P_R * 1000LL) >> 8ULL); |
| 367 | Ablock = Asub * (Bsub * Bsub); |
| 368 | Bblock = (div64_s64(Bsub * 10000000LL, P_G)) << 20ULL; |
| 369 | Cblock = ((s64)P_O * 10000000000LL) >> 8ULL; |
| 370 | |
| 371 | sum = div64_s64(Ablock, 1000000LL) + Bblock + Cblock; |
| 372 | |
| 373 | return div64_s64(sum, 10000000LL); |
| 374 | } |
| 375 | |
| 376 | static s32 mlx90632_calc_temp_object_iteration(s32 prev_object_temp, s64 object, |
| 377 | s64 TAdut, s32 Fa, s32 Fb, |
| 378 | s32 Ga, s16 Ha, s16 Hb, |
| 379 | u16 emissivity) |
| 380 | { |
| 381 | s64 calcedKsTO, calcedKsTA, ir_Alpha, TAdut4, Alpha_corr; |
| 382 | s64 Ha_customer, Hb_customer; |
| 383 | |
| 384 | Ha_customer = ((s64)Ha * 1000000LL) >> 14ULL; |
| 385 | Hb_customer = ((s64)Hb * 100) >> 10ULL; |
| 386 | |
| 387 | calcedKsTO = ((s64)((s64)Ga * (prev_object_temp - 25 * 1000LL) |
| 388 | * 1000LL)) >> 36LL; |
| 389 | calcedKsTA = ((s64)(Fb * (TAdut - 25 * 1000000LL))) >> 36LL; |
| 390 | Alpha_corr = div64_s64((((s64)(Fa * 10000000000LL) >> 46LL) |
| 391 | * Ha_customer), 1000LL); |
| 392 | Alpha_corr *= ((s64)(1 * 1000000LL + calcedKsTO + calcedKsTA)); |
| 393 | Alpha_corr = emissivity * div64_s64(Alpha_corr, 100000LL); |
| 394 | Alpha_corr = div64_s64(Alpha_corr, 1000LL); |
| 395 | ir_Alpha = div64_s64((s64)object * 10000000LL, Alpha_corr); |
| 396 | TAdut4 = (div64_s64(TAdut, 10000LL) + 27315) * |
| 397 | (div64_s64(TAdut, 10000LL) + 27315) * |
| 398 | (div64_s64(TAdut, 10000LL) + 27315) * |
| 399 | (div64_s64(TAdut, 10000LL) + 27315); |
| 400 | |
| 401 | return (int_sqrt64(int_sqrt64(ir_Alpha * 1000000000000LL + TAdut4)) |
| 402 | - 27315 - Hb_customer) * 10; |
| 403 | } |
| 404 | |
| 405 | static s32 mlx90632_calc_temp_object(s64 object, s64 ambient, s32 Ea, s32 Eb, |
| 406 | s32 Fa, s32 Fb, s32 Ga, s16 Ha, s16 Hb, |
| 407 | u16 tmp_emi) |
| 408 | { |
| 409 | s64 kTA, kTA0, TAdut; |
| 410 | s64 temp = 25000; |
| 411 | s8 i; |
| 412 | |
| 413 | kTA = (Ea * 1000LL) >> 16LL; |
| 414 | kTA0 = (Eb * 1000LL) >> 8LL; |
| 415 | TAdut = div64_s64(((ambient - kTA0) * 1000000LL), kTA) + 25 * 1000000LL; |
| 416 | |
| 417 | /* Iterations of calculation as described in datasheet */ |
| 418 | for (i = 0; i < 5; ++i) { |
| 419 | temp = mlx90632_calc_temp_object_iteration(temp, object, TAdut, |
| 420 | Fa, Fb, Ga, Ha, Hb, |
| 421 | tmp_emi); |
| 422 | } |
| 423 | return temp; |
| 424 | } |
| 425 | |
| 426 | static int mlx90632_calc_object_dsp105(struct mlx90632_data *data, int *val) |
| 427 | { |
| 428 | s32 ret; |
| 429 | s32 Ea, Eb, Fa, Fb, Ga; |
| 430 | unsigned int read_tmp; |
| 431 | s16 Ha, Hb, Gb, Ka; |
| 432 | s16 ambient_new_raw, ambient_old_raw, object_new_raw, object_old_raw; |
| 433 | s64 object, ambient; |
| 434 | |
| 435 | ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Ea, &Ea); |
| 436 | if (ret < 0) |
| 437 | return ret; |
| 438 | ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Eb, &Eb); |
| 439 | if (ret < 0) |
| 440 | return ret; |
| 441 | ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Fa, &Fa); |
| 442 | if (ret < 0) |
| 443 | return ret; |
| 444 | ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Fb, &Fb); |
| 445 | if (ret < 0) |
| 446 | return ret; |
| 447 | ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Ga, &Ga); |
| 448 | if (ret < 0) |
| 449 | return ret; |
| 450 | ret = regmap_read(data->regmap, MLX90632_EE_Ha, &read_tmp); |
| 451 | if (ret < 0) |
| 452 | return ret; |
| 453 | Ha = (s16)read_tmp; |
| 454 | ret = regmap_read(data->regmap, MLX90632_EE_Hb, &read_tmp); |
| 455 | if (ret < 0) |
| 456 | return ret; |
| 457 | Hb = (s16)read_tmp; |
| 458 | ret = regmap_read(data->regmap, MLX90632_EE_Gb, &read_tmp); |
| 459 | if (ret < 0) |
| 460 | return ret; |
| 461 | Gb = (s16)read_tmp; |
| 462 | ret = regmap_read(data->regmap, MLX90632_EE_Ka, &read_tmp); |
| 463 | if (ret < 0) |
| 464 | return ret; |
| 465 | Ka = (s16)read_tmp; |
| 466 | |
| 467 | ret = mlx90632_read_all_channel(data, |
| 468 | &ambient_new_raw, &ambient_old_raw, |
| 469 | &object_new_raw, &object_old_raw); |
| 470 | if (ret < 0) |
| 471 | return ret; |
| 472 | |
| 473 | ambient = mlx90632_preprocess_temp_amb(ambient_new_raw, |
| 474 | ambient_old_raw, Gb); |
| 475 | object = mlx90632_preprocess_temp_obj(object_new_raw, |
| 476 | object_old_raw, |
| 477 | ambient_new_raw, |
| 478 | ambient_old_raw, Ka); |
| 479 | |
| 480 | *val = mlx90632_calc_temp_object(object, ambient, Ea, Eb, Fa, Fb, Ga, |
| 481 | Ha, Hb, data->emissivity); |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | static int mlx90632_calc_ambient_dsp105(struct mlx90632_data *data, int *val) |
| 486 | { |
| 487 | s32 ret; |
| 488 | unsigned int read_tmp; |
| 489 | s32 PT, PR, PG, PO; |
| 490 | s16 Gb; |
| 491 | s16 ambient_new_raw, ambient_old_raw; |
| 492 | |
| 493 | ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_R, &PR); |
| 494 | if (ret < 0) |
| 495 | return ret; |
| 496 | ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_G, &PG); |
| 497 | if (ret < 0) |
| 498 | return ret; |
| 499 | ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_T, &PT); |
| 500 | if (ret < 0) |
| 501 | return ret; |
| 502 | ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_O, &PO); |
| 503 | if (ret < 0) |
| 504 | return ret; |
| 505 | ret = regmap_read(data->regmap, MLX90632_EE_Gb, &read_tmp); |
| 506 | if (ret < 0) |
| 507 | return ret; |
| 508 | Gb = (s16)read_tmp; |
| 509 | |
| 510 | ret = mlx90632_read_ambient_raw(data->regmap, &ambient_new_raw, |
| 511 | &ambient_old_raw); |
Dan Carpenter | c50fade | 2018-02-22 12:14:53 +0300 | [diff] [blame] | 512 | if (ret < 0) |
| 513 | return ret; |
Crt Mori | c87742a | 2018-01-11 11:20:23 +0100 | [diff] [blame] | 514 | *val = mlx90632_calc_temp_ambient(ambient_new_raw, ambient_old_raw, |
| 515 | PT, PR, PG, PO, Gb); |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | static int mlx90632_read_raw(struct iio_dev *indio_dev, |
| 520 | struct iio_chan_spec const *channel, int *val, |
| 521 | int *val2, long mask) |
| 522 | { |
| 523 | struct mlx90632_data *data = iio_priv(indio_dev); |
| 524 | int ret; |
| 525 | |
| 526 | switch (mask) { |
| 527 | case IIO_CHAN_INFO_PROCESSED: |
| 528 | switch (channel->channel2) { |
| 529 | case IIO_MOD_TEMP_AMBIENT: |
| 530 | ret = mlx90632_calc_ambient_dsp105(data, val); |
| 531 | if (ret < 0) |
| 532 | return ret; |
| 533 | return IIO_VAL_INT; |
| 534 | case IIO_MOD_TEMP_OBJECT: |
| 535 | ret = mlx90632_calc_object_dsp105(data, val); |
| 536 | if (ret < 0) |
| 537 | return ret; |
| 538 | return IIO_VAL_INT; |
| 539 | default: |
| 540 | return -EINVAL; |
| 541 | } |
| 542 | case IIO_CHAN_INFO_CALIBEMISSIVITY: |
| 543 | if (data->emissivity == 1000) { |
| 544 | *val = 1; |
| 545 | *val2 = 0; |
| 546 | } else { |
| 547 | *val = 0; |
| 548 | *val2 = data->emissivity * 1000; |
| 549 | } |
| 550 | return IIO_VAL_INT_PLUS_MICRO; |
| 551 | |
| 552 | default: |
| 553 | return -EINVAL; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | static int mlx90632_write_raw(struct iio_dev *indio_dev, |
| 558 | struct iio_chan_spec const *channel, int val, |
| 559 | int val2, long mask) |
| 560 | { |
| 561 | struct mlx90632_data *data = iio_priv(indio_dev); |
| 562 | |
| 563 | switch (mask) { |
| 564 | case IIO_CHAN_INFO_CALIBEMISSIVITY: |
| 565 | /* Confirm we are within 0 and 1.0 */ |
| 566 | if (val < 0 || val2 < 0 || val > 1 || |
| 567 | (val == 1 && val2 != 0)) |
| 568 | return -EINVAL; |
| 569 | data->emissivity = val * 1000 + val2 / 1000; |
| 570 | return 0; |
| 571 | default: |
| 572 | return -EINVAL; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | static const struct iio_chan_spec mlx90632_channels[] = { |
| 577 | { |
| 578 | .type = IIO_TEMP, |
| 579 | .modified = 1, |
| 580 | .channel2 = IIO_MOD_TEMP_AMBIENT, |
| 581 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
| 582 | }, |
| 583 | { |
| 584 | .type = IIO_TEMP, |
| 585 | .modified = 1, |
| 586 | .channel2 = IIO_MOD_TEMP_OBJECT, |
| 587 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | |
| 588 | BIT(IIO_CHAN_INFO_CALIBEMISSIVITY), |
| 589 | }, |
| 590 | }; |
| 591 | |
| 592 | static const struct iio_info mlx90632_info = { |
| 593 | .read_raw = mlx90632_read_raw, |
| 594 | .write_raw = mlx90632_write_raw, |
| 595 | }; |
| 596 | |
| 597 | static int mlx90632_sleep(struct mlx90632_data *data) |
| 598 | { |
| 599 | regcache_mark_dirty(data->regmap); |
| 600 | |
| 601 | dev_dbg(&data->client->dev, "Requesting sleep"); |
| 602 | return mlx90632_pwr_set_sleep_step(data->regmap); |
| 603 | } |
| 604 | |
| 605 | static int mlx90632_wakeup(struct mlx90632_data *data) |
| 606 | { |
| 607 | int ret; |
| 608 | |
| 609 | ret = regcache_sync(data->regmap); |
| 610 | if (ret < 0) { |
| 611 | dev_err(&data->client->dev, |
| 612 | "Failed to sync regmap registers: %d\n", ret); |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | dev_dbg(&data->client->dev, "Requesting wake-up\n"); |
| 617 | return mlx90632_pwr_continuous(data->regmap); |
| 618 | } |
| 619 | |
| 620 | static int mlx90632_probe(struct i2c_client *client, |
| 621 | const struct i2c_device_id *id) |
| 622 | { |
| 623 | struct iio_dev *indio_dev; |
| 624 | struct mlx90632_data *mlx90632; |
| 625 | struct regmap *regmap; |
| 626 | int ret; |
| 627 | unsigned int read; |
| 628 | |
| 629 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mlx90632)); |
| 630 | if (!indio_dev) { |
| 631 | dev_err(&client->dev, "Failed to allocate device\n"); |
| 632 | return -ENOMEM; |
| 633 | } |
| 634 | |
| 635 | regmap = devm_regmap_init_i2c(client, &mlx90632_regmap); |
| 636 | if (IS_ERR(regmap)) { |
| 637 | ret = PTR_ERR(regmap); |
| 638 | dev_err(&client->dev, "Failed to allocate regmap: %d\n", ret); |
| 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | mlx90632 = iio_priv(indio_dev); |
| 643 | i2c_set_clientdata(client, indio_dev); |
| 644 | mlx90632->client = client; |
| 645 | mlx90632->regmap = regmap; |
| 646 | |
| 647 | mutex_init(&mlx90632->lock); |
| 648 | indio_dev->dev.parent = &client->dev; |
| 649 | indio_dev->name = id->name; |
| 650 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 651 | indio_dev->info = &mlx90632_info; |
| 652 | indio_dev->channels = mlx90632_channels; |
| 653 | indio_dev->num_channels = ARRAY_SIZE(mlx90632_channels); |
| 654 | |
| 655 | ret = mlx90632_wakeup(mlx90632); |
| 656 | if (ret < 0) { |
| 657 | dev_err(&client->dev, "Wakeup failed: %d\n", ret); |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | ret = regmap_read(mlx90632->regmap, MLX90632_EE_VERSION, &read); |
| 662 | if (ret < 0) { |
| 663 | dev_err(&client->dev, "read of version failed: %d\n", ret); |
| 664 | return ret; |
| 665 | } |
| 666 | if (read == MLX90632_ID_MEDICAL) { |
| 667 | dev_dbg(&client->dev, |
| 668 | "Detected Medical EEPROM calibration %x\n", read); |
| 669 | } else if (read == MLX90632_ID_CONSUMER) { |
| 670 | dev_dbg(&client->dev, |
| 671 | "Detected Consumer EEPROM calibration %x\n", read); |
Crt Mori | 389fc70 | 2019-05-23 14:07:22 +0200 | [diff] [blame] | 672 | } else if ((read & MLX90632_DSP_MASK) == MLX90632_DSP_VERSION) { |
| 673 | dev_dbg(&client->dev, |
| 674 | "Detected Unknown EEPROM calibration %x\n", read); |
Crt Mori | c87742a | 2018-01-11 11:20:23 +0100 | [diff] [blame] | 675 | } else { |
| 676 | dev_err(&client->dev, |
Crt Mori | 389fc70 | 2019-05-23 14:07:22 +0200 | [diff] [blame] | 677 | "Wrong DSP version %x (expected %x)\n", |
| 678 | read, MLX90632_DSP_VERSION); |
Crt Mori | c87742a | 2018-01-11 11:20:23 +0100 | [diff] [blame] | 679 | return -EPROTONOSUPPORT; |
| 680 | } |
| 681 | |
| 682 | mlx90632->emissivity = 1000; |
| 683 | |
| 684 | pm_runtime_disable(&client->dev); |
| 685 | ret = pm_runtime_set_active(&client->dev); |
| 686 | if (ret < 0) { |
| 687 | mlx90632_sleep(mlx90632); |
| 688 | return ret; |
| 689 | } |
| 690 | pm_runtime_enable(&client->dev); |
| 691 | pm_runtime_set_autosuspend_delay(&client->dev, MLX90632_SLEEP_DELAY_MS); |
| 692 | pm_runtime_use_autosuspend(&client->dev); |
| 693 | |
| 694 | return iio_device_register(indio_dev); |
| 695 | } |
| 696 | |
| 697 | static int mlx90632_remove(struct i2c_client *client) |
| 698 | { |
| 699 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 700 | struct mlx90632_data *data = iio_priv(indio_dev); |
| 701 | |
| 702 | iio_device_unregister(indio_dev); |
| 703 | |
| 704 | pm_runtime_disable(&client->dev); |
| 705 | pm_runtime_set_suspended(&client->dev); |
| 706 | pm_runtime_put_noidle(&client->dev); |
| 707 | |
| 708 | mlx90632_sleep(data); |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | static const struct i2c_device_id mlx90632_id[] = { |
| 714 | { "mlx90632", 0 }, |
| 715 | { } |
| 716 | }; |
| 717 | MODULE_DEVICE_TABLE(i2c, mlx90632_id); |
| 718 | |
| 719 | static const struct of_device_id mlx90632_of_match[] = { |
| 720 | { .compatible = "melexis,mlx90632" }, |
| 721 | { } |
| 722 | }; |
| 723 | MODULE_DEVICE_TABLE(of, mlx90632_of_match); |
| 724 | |
| 725 | static int __maybe_unused mlx90632_pm_suspend(struct device *dev) |
| 726 | { |
| 727 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 728 | struct mlx90632_data *data = iio_priv(indio_dev); |
| 729 | |
| 730 | return mlx90632_sleep(data); |
| 731 | } |
| 732 | |
| 733 | static int __maybe_unused mlx90632_pm_resume(struct device *dev) |
| 734 | { |
| 735 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 736 | struct mlx90632_data *data = iio_priv(indio_dev); |
| 737 | |
| 738 | return mlx90632_wakeup(data); |
| 739 | } |
| 740 | |
| 741 | static UNIVERSAL_DEV_PM_OPS(mlx90632_pm_ops, mlx90632_pm_suspend, |
| 742 | mlx90632_pm_resume, NULL); |
| 743 | |
| 744 | static struct i2c_driver mlx90632_driver = { |
| 745 | .driver = { |
| 746 | .name = "mlx90632", |
| 747 | .of_match_table = mlx90632_of_match, |
| 748 | .pm = &mlx90632_pm_ops, |
| 749 | }, |
| 750 | .probe = mlx90632_probe, |
| 751 | .remove = mlx90632_remove, |
| 752 | .id_table = mlx90632_id, |
| 753 | }; |
| 754 | module_i2c_driver(mlx90632_driver); |
| 755 | |
| 756 | MODULE_AUTHOR("Crt Mori <cmo@melexis.com>"); |
| 757 | MODULE_DESCRIPTION("Melexis MLX90632 contactless Infra Red temperature sensor driver"); |
| 758 | MODULE_LICENSE("GPL v2"); |