1 // SPDX-License-Identifier: GPL-2.0
3 * mlx90632.c - Melexis MLX90632 contactless IR temperature sensor
5 * Copyright (c) 2017 Melexis <cmo@melexis.com>
7 * Driver for the Melexis MLX90632 I2C 16-bit IR thermopile sensor
9 #include <linux/bitfield.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/iopoll.h>
16 #include <linux/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/limits.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/module.h>
21 #include <linux/math64.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
29 /* Memory sections addresses */
30 #define MLX90632_ADDR_RAM 0x4000 /* Start address of ram */
31 #define MLX90632_ADDR_EEPROM 0x2480 /* Start address of user eeprom */
33 /* EEPROM addresses - used at startup */
34 #define MLX90632_EE_CTRL 0x24d4 /* Control register initial value */
35 #define MLX90632_EE_I2C_ADDR 0x24d5 /* I2C address register initial value */
36 #define MLX90632_EE_VERSION 0x240b /* EEPROM version reg address */
37 #define MLX90632_EE_P_R 0x240c /* P_R calibration register 32bit */
38 #define MLX90632_EE_P_G 0x240e /* P_G calibration register 32bit */
39 #define MLX90632_EE_P_T 0x2410 /* P_T calibration register 32bit */
40 #define MLX90632_EE_P_O 0x2412 /* P_O calibration register 32bit */
41 #define MLX90632_EE_Aa 0x2414 /* Aa calibration register 32bit */
42 #define MLX90632_EE_Ab 0x2416 /* Ab calibration register 32bit */
43 #define MLX90632_EE_Ba 0x2418 /* Ba calibration register 32bit */
44 #define MLX90632_EE_Bb 0x241a /* Bb calibration register 32bit */
45 #define MLX90632_EE_Ca 0x241c /* Ca calibration register 32bit */
46 #define MLX90632_EE_Cb 0x241e /* Cb calibration register 32bit */
47 #define MLX90632_EE_Da 0x2420 /* Da calibration register 32bit */
48 #define MLX90632_EE_Db 0x2422 /* Db calibration register 32bit */
49 #define MLX90632_EE_Ea 0x2424 /* Ea calibration register 32bit */
50 #define MLX90632_EE_Eb 0x2426 /* Eb calibration register 32bit */
51 #define MLX90632_EE_Fa 0x2428 /* Fa calibration register 32bit */
52 #define MLX90632_EE_Fb 0x242a /* Fb calibration register 32bit */
53 #define MLX90632_EE_Ga 0x242c /* Ga calibration register 32bit */
55 #define MLX90632_EE_Gb 0x242e /* Gb calibration register 16bit */
56 #define MLX90632_EE_Ka 0x242f /* Ka calibration register 16bit */
58 #define MLX90632_EE_Ha 0x2481 /* Ha customer calib value reg 16bit */
59 #define MLX90632_EE_Hb 0x2482 /* Hb customer calib value reg 16bit */
61 #define MLX90632_EE_MEDICAL_MEAS1 0x24E1 /* Medical measurement 1 16bit */
62 #define MLX90632_EE_MEDICAL_MEAS2 0x24E2 /* Medical measurement 2 16bit */
63 #define MLX90632_EE_EXTENDED_MEAS1 0x24F1 /* Extended measurement 1 16bit */
64 #define MLX90632_EE_EXTENDED_MEAS2 0x24F2 /* Extended measurement 2 16bit */
65 #define MLX90632_EE_EXTENDED_MEAS3 0x24F3 /* Extended measurement 3 16bit */
67 /* Register addresses - volatile */
68 #define MLX90632_REG_I2C_ADDR 0x3000 /* Chip I2C address register */
70 /* Control register address - volatile */
71 #define MLX90632_REG_CONTROL 0x3001 /* Control Register address */
72 #define MLX90632_CFG_PWR_MASK GENMASK(2, 1) /* PowerMode Mask */
73 #define MLX90632_CFG_MTYP_MASK GENMASK(8, 4) /* Meas select Mask */
74 #define MLX90632_CFG_SOB_MASK BIT(11)
76 /* PowerModes statuses */
77 #define MLX90632_PWR_STATUS(ctrl_val) (ctrl_val << 1)
78 #define MLX90632_PWR_STATUS_HALT MLX90632_PWR_STATUS(0) /* hold */
79 #define MLX90632_PWR_STATUS_SLEEP_STEP MLX90632_PWR_STATUS(1) /* sleep step */
80 #define MLX90632_PWR_STATUS_STEP MLX90632_PWR_STATUS(2) /* step */
81 #define MLX90632_PWR_STATUS_CONTINUOUS MLX90632_PWR_STATUS(3) /* continuous */
83 #define MLX90632_EE_RR GENMASK(10, 8) /* Only Refresh Rate bits */
84 #define MLX90632_REFRESH_RATE(ee_val) FIELD_GET(MLX90632_EE_RR, ee_val)
85 /* Extract Refresh Rate from ee register */
86 #define MLX90632_REFRESH_RATE_STATUS(refresh_rate) (refresh_rate << 8)
88 /* Measurement types */
89 #define MLX90632_MTYP_MEDICAL 0
90 #define MLX90632_MTYP_EXTENDED 17
92 /* Measurement type select*/
93 #define MLX90632_MTYP_STATUS(ctrl_val) (ctrl_val << 4)
94 #define MLX90632_MTYP_STATUS_MEDICAL MLX90632_MTYP_STATUS(MLX90632_MTYP_MEDICAL)
95 #define MLX90632_MTYP_STATUS_EXTENDED MLX90632_MTYP_STATUS(MLX90632_MTYP_EXTENDED)
97 /* I2C command register - volatile */
98 #define MLX90632_REG_I2C_CMD 0x3005 /* I2C command Register address */
100 /* Device status register - volatile */
101 #define MLX90632_REG_STATUS 0x3fff /* Device status register */
102 #define MLX90632_STAT_BUSY BIT(10) /* Device busy indicator */
103 #define MLX90632_STAT_EE_BUSY BIT(9) /* EEPROM busy indicator */
104 #define MLX90632_STAT_BRST BIT(8) /* Brown out reset indicator */
105 #define MLX90632_STAT_CYCLE_POS GENMASK(6, 2) /* Data position */
106 #define MLX90632_STAT_DATA_RDY BIT(0) /* Data ready indicator */
108 /* RAM_MEAS address-es for each channel */
109 #define MLX90632_RAM_1(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num)
110 #define MLX90632_RAM_2(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 1)
111 #define MLX90632_RAM_3(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 2)
113 /* Name important RAM_MEAS channels */
114 #define MLX90632_RAM_DSP5_EXTENDED_AMBIENT_1 MLX90632_RAM_3(17)
115 #define MLX90632_RAM_DSP5_EXTENDED_AMBIENT_2 MLX90632_RAM_3(18)
116 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_1 MLX90632_RAM_1(17)
117 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_2 MLX90632_RAM_2(17)
118 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_3 MLX90632_RAM_1(18)
119 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_4 MLX90632_RAM_2(18)
120 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_5 MLX90632_RAM_1(19)
121 #define MLX90632_RAM_DSP5_EXTENDED_OBJECT_6 MLX90632_RAM_2(19)
123 /* Magic constants */
124 #define MLX90632_ID_MEDICAL 0x0105 /* EEPROM DSPv5 Medical device id */
125 #define MLX90632_ID_CONSUMER 0x0205 /* EEPROM DSPv5 Consumer device id */
126 #define MLX90632_ID_EXTENDED 0x0505 /* EEPROM DSPv5 Extended range device id */
127 #define MLX90632_ID_MASK GENMASK(14, 0) /* DSP version and device ID in EE_VERSION */
128 #define MLX90632_DSP_VERSION 5 /* DSP version */
129 #define MLX90632_DSP_MASK GENMASK(7, 0) /* DSP version in EE_VERSION */
130 #define MLX90632_RESET_CMD 0x0006 /* Reset sensor (address or global) */
131 #define MLX90632_REF_12 12LL /* ResCtrlRef value of Ch 1 or Ch 2 */
132 #define MLX90632_REF_3 12LL /* ResCtrlRef value of Channel 3 */
133 #define MLX90632_MAX_MEAS_NUM 31 /* Maximum measurements in list */
134 #define MLX90632_SLEEP_DELAY_MS 6000 /* Autosleep delay */
135 #define MLX90632_EXTENDED_LIMIT 27000 /* Extended mode raw value limit */
136 #define MLX90632_MEAS_MAX_TIME 2000 /* Max measurement time in ms for the lowest refresh rate */
139 * struct mlx90632_data - private data for the MLX90632 device
140 * @client: I2C client of the device
141 * @lock: Internal mutex for multiple reads for single measurement
142 * @regmap: Regmap of the device
143 * @emissivity: Object emissivity from 0 to 1000 where 1000 = 1.
144 * @mtyp: Measurement type physical sensor configuration for extended range
146 * @object_ambient_temperature: Ambient temperature at object (might differ of
147 * the ambient temperature of sensor.
148 * @regulator: Regulator of the device
149 * @powerstatus: Current POWER status of the device
150 * @interaction_ts: Timestamp of the last temperature read that is used
151 * for power management in jiffies
153 struct mlx90632_data
{
154 struct i2c_client
*client
;
156 struct regmap
*regmap
;
159 u32 object_ambient_temperature
;
160 struct regulator
*regulator
;
162 unsigned long interaction_ts
;
165 static const struct regmap_range mlx90632_volatile_reg_range
[] = {
166 regmap_reg_range(MLX90632_REG_I2C_ADDR
, MLX90632_REG_CONTROL
),
167 regmap_reg_range(MLX90632_REG_I2C_CMD
, MLX90632_REG_I2C_CMD
),
168 regmap_reg_range(MLX90632_REG_STATUS
, MLX90632_REG_STATUS
),
169 regmap_reg_range(MLX90632_RAM_1(0),
170 MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM
)),
173 static const struct regmap_access_table mlx90632_volatile_regs_tbl
= {
174 .yes_ranges
= mlx90632_volatile_reg_range
,
175 .n_yes_ranges
= ARRAY_SIZE(mlx90632_volatile_reg_range
),
178 static const struct regmap_range mlx90632_read_reg_range
[] = {
179 regmap_reg_range(MLX90632_EE_VERSION
, MLX90632_EE_Ka
),
180 regmap_reg_range(MLX90632_EE_CTRL
, MLX90632_EE_I2C_ADDR
),
181 regmap_reg_range(MLX90632_EE_Ha
, MLX90632_EE_Hb
),
182 regmap_reg_range(MLX90632_EE_MEDICAL_MEAS1
, MLX90632_EE_MEDICAL_MEAS2
),
183 regmap_reg_range(MLX90632_EE_EXTENDED_MEAS1
, MLX90632_EE_EXTENDED_MEAS3
),
184 regmap_reg_range(MLX90632_REG_I2C_ADDR
, MLX90632_REG_CONTROL
),
185 regmap_reg_range(MLX90632_REG_I2C_CMD
, MLX90632_REG_I2C_CMD
),
186 regmap_reg_range(MLX90632_REG_STATUS
, MLX90632_REG_STATUS
),
187 regmap_reg_range(MLX90632_RAM_1(0),
188 MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM
)),
191 static const struct regmap_access_table mlx90632_readable_regs_tbl
= {
192 .yes_ranges
= mlx90632_read_reg_range
,
193 .n_yes_ranges
= ARRAY_SIZE(mlx90632_read_reg_range
),
196 static const struct regmap_range mlx90632_no_write_reg_range
[] = {
197 regmap_reg_range(MLX90632_EE_VERSION
, MLX90632_EE_Ka
),
198 regmap_reg_range(MLX90632_RAM_1(0),
199 MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM
)),
202 static const struct regmap_access_table mlx90632_writeable_regs_tbl
= {
203 .no_ranges
= mlx90632_no_write_reg_range
,
204 .n_no_ranges
= ARRAY_SIZE(mlx90632_no_write_reg_range
),
207 static const struct regmap_config mlx90632_regmap
= {
211 .volatile_table
= &mlx90632_volatile_regs_tbl
,
212 .rd_table
= &mlx90632_readable_regs_tbl
,
213 .wr_table
= &mlx90632_writeable_regs_tbl
,
215 .use_single_read
= true,
216 .use_single_write
= true,
217 .reg_format_endian
= REGMAP_ENDIAN_BIG
,
218 .val_format_endian
= REGMAP_ENDIAN_BIG
,
219 .cache_type
= REGCACHE_RBTREE
,
222 static int mlx90632_pwr_set_sleep_step(struct regmap
*regmap
)
224 struct mlx90632_data
*data
=
225 iio_priv(dev_get_drvdata(regmap_get_device(regmap
)));
228 if (data
->powerstatus
== MLX90632_PWR_STATUS_SLEEP_STEP
)
231 ret
= regmap_write_bits(regmap
, MLX90632_REG_CONTROL
, MLX90632_CFG_PWR_MASK
,
232 MLX90632_PWR_STATUS_SLEEP_STEP
);
236 data
->powerstatus
= MLX90632_PWR_STATUS_SLEEP_STEP
;
240 static int mlx90632_pwr_continuous(struct regmap
*regmap
)
242 struct mlx90632_data
*data
=
243 iio_priv(dev_get_drvdata(regmap_get_device(regmap
)));
246 if (data
->powerstatus
== MLX90632_PWR_STATUS_CONTINUOUS
)
249 ret
= regmap_write_bits(regmap
, MLX90632_REG_CONTROL
, MLX90632_CFG_PWR_MASK
,
250 MLX90632_PWR_STATUS_CONTINUOUS
);
254 data
->powerstatus
= MLX90632_PWR_STATUS_CONTINUOUS
;
259 * mlx90632_reset_delay() - Give the mlx90632 some time to reset properly
260 * If this is not done, the following I2C command(s) will not be accepted.
262 static void mlx90632_reset_delay(void)
264 usleep_range(150, 200);
267 static int mlx90632_get_measurement_time(struct regmap
*regmap
, u16 meas
)
272 ret
= regmap_read(regmap
, meas
, ®
);
276 return MLX90632_MEAS_MAX_TIME
>> FIELD_GET(MLX90632_EE_RR
, reg
);
279 static int mlx90632_calculate_dataset_ready_time(struct mlx90632_data
*data
)
281 unsigned int refresh_time
;
284 if (data
->mtyp
== MLX90632_MTYP_MEDICAL
) {
285 ret
= mlx90632_get_measurement_time(data
->regmap
,
286 MLX90632_EE_MEDICAL_MEAS1
);
292 ret
= mlx90632_get_measurement_time(data
->regmap
,
293 MLX90632_EE_MEDICAL_MEAS2
);
299 ret
= mlx90632_get_measurement_time(data
->regmap
,
300 MLX90632_EE_EXTENDED_MEAS1
);
306 ret
= mlx90632_get_measurement_time(data
->regmap
,
307 MLX90632_EE_EXTENDED_MEAS2
);
313 ret
= mlx90632_get_measurement_time(data
->regmap
,
314 MLX90632_EE_EXTENDED_MEAS3
);
325 * mlx90632_perform_measurement() - Trigger and retrieve current measurement cycle
326 * @data: pointer to mlx90632_data object containing regmap information
328 * Perform a measurement and return latest measurement cycle position reported
329 * by sensor. This is a blocking function for 500ms, as that is default sensor
332 static int mlx90632_perform_measurement(struct mlx90632_data
*data
)
334 unsigned int reg_status
;
337 ret
= regmap_clear_bits(data
->regmap
, MLX90632_REG_STATUS
,
338 MLX90632_STAT_DATA_RDY
);
342 ret
= regmap_read_poll_timeout(data
->regmap
, MLX90632_REG_STATUS
, reg_status
,
343 !(reg_status
& MLX90632_STAT_DATA_RDY
), 10000,
347 dev_err(&data
->client
->dev
, "data not ready");
351 return (reg_status
& MLX90632_STAT_CYCLE_POS
) >> 2;
355 * mlx90632_perform_measurement_burst() - Trigger and retrieve current measurement
356 * cycle in step sleep mode
357 * @data: pointer to mlx90632_data object containing regmap information
359 * Perform a measurement and return 2 as measurement cycle position reported
360 * by sensor. This is a blocking function for amount dependent on the sensor
363 static int mlx90632_perform_measurement_burst(struct mlx90632_data
*data
)
365 unsigned int reg_status
;
368 ret
= regmap_write_bits(data
->regmap
, MLX90632_REG_CONTROL
,
369 MLX90632_CFG_SOB_MASK
, MLX90632_CFG_SOB_MASK
);
373 ret
= mlx90632_calculate_dataset_ready_time(data
);
377 msleep(ret
); /* Wait minimum time for dataset to be ready */
379 ret
= regmap_read_poll_timeout(data
->regmap
, MLX90632_REG_STATUS
,
381 (reg_status
& MLX90632_STAT_BUSY
) == 0,
384 dev_err(&data
->client
->dev
, "data not ready");
391 static int mlx90632_set_meas_type(struct mlx90632_data
*data
, u8 type
)
393 int current_powerstatus
;
396 if (data
->mtyp
== type
)
399 current_powerstatus
= data
->powerstatus
;
400 ret
= mlx90632_pwr_continuous(data
->regmap
);
404 ret
= regmap_write(data
->regmap
, MLX90632_REG_I2C_CMD
, MLX90632_RESET_CMD
);
408 mlx90632_reset_delay();
410 ret
= regmap_update_bits(data
->regmap
, MLX90632_REG_CONTROL
,
411 (MLX90632_CFG_MTYP_MASK
| MLX90632_CFG_PWR_MASK
),
412 (MLX90632_MTYP_STATUS(type
) | MLX90632_PWR_STATUS_HALT
));
417 data
->powerstatus
= MLX90632_PWR_STATUS_HALT
;
419 if (current_powerstatus
== MLX90632_PWR_STATUS_SLEEP_STEP
)
420 return mlx90632_pwr_set_sleep_step(data
->regmap
);
422 return mlx90632_pwr_continuous(data
->regmap
);
425 static int mlx90632_channel_new_select(int perform_ret
, uint8_t *channel_new
,
426 uint8_t *channel_old
)
428 switch (perform_ret
) {
444 static int mlx90632_read_ambient_raw(struct regmap
*regmap
,
445 s16
*ambient_new_raw
, s16
*ambient_old_raw
)
447 unsigned int read_tmp
;
450 ret
= regmap_read(regmap
, MLX90632_RAM_3(1), &read_tmp
);
453 *ambient_new_raw
= (s16
)read_tmp
;
455 ret
= regmap_read(regmap
, MLX90632_RAM_3(2), &read_tmp
);
458 *ambient_old_raw
= (s16
)read_tmp
;
463 static int mlx90632_read_object_raw(struct regmap
*regmap
,
464 int perform_measurement_ret
,
465 s16
*object_new_raw
, s16
*object_old_raw
)
467 unsigned int read_tmp
;
473 ret
= mlx90632_channel_new_select(perform_measurement_ret
, &channel
,
478 ret
= regmap_read(regmap
, MLX90632_RAM_2(channel
), &read_tmp
);
482 read
= (s16
)read_tmp
;
484 ret
= regmap_read(regmap
, MLX90632_RAM_1(channel
), &read_tmp
);
487 *object_new_raw
= (read
+ (s16
)read_tmp
) / 2;
489 ret
= regmap_read(regmap
, MLX90632_RAM_2(channel_old
), &read_tmp
);
492 read
= (s16
)read_tmp
;
494 ret
= regmap_read(regmap
, MLX90632_RAM_1(channel_old
), &read_tmp
);
497 *object_old_raw
= (read
+ (s16
)read_tmp
) / 2;
502 static int mlx90632_read_all_channel(struct mlx90632_data
*data
,
503 s16
*ambient_new_raw
, s16
*ambient_old_raw
,
504 s16
*object_new_raw
, s16
*object_old_raw
)
509 mutex_lock(&data
->lock
);
510 ret
= mlx90632_set_meas_type(data
, MLX90632_MTYP_MEDICAL
);
514 switch (data
->powerstatus
) {
515 case MLX90632_PWR_STATUS_CONTINUOUS
:
516 ret
= mlx90632_perform_measurement(data
);
521 case MLX90632_PWR_STATUS_SLEEP_STEP
:
522 ret
= mlx90632_perform_measurement_burst(data
);
532 measurement
= ret
; /* If we came here ret holds the measurement position */
534 ret
= mlx90632_read_ambient_raw(data
->regmap
, ambient_new_raw
,
539 ret
= mlx90632_read_object_raw(data
->regmap
, measurement
,
540 object_new_raw
, object_old_raw
);
542 mutex_unlock(&data
->lock
);
546 static int mlx90632_read_ambient_raw_extended(struct regmap
*regmap
,
547 s16
*ambient_new_raw
, s16
*ambient_old_raw
)
549 unsigned int read_tmp
;
552 ret
= regmap_read(regmap
, MLX90632_RAM_DSP5_EXTENDED_AMBIENT_1
, &read_tmp
);
555 *ambient_new_raw
= (s16
)read_tmp
;
557 ret
= regmap_read(regmap
, MLX90632_RAM_DSP5_EXTENDED_AMBIENT_2
, &read_tmp
);
560 *ambient_old_raw
= (s16
)read_tmp
;
565 static int mlx90632_read_object_raw_extended(struct regmap
*regmap
, s16
*object_new_raw
)
567 unsigned int read_tmp
;
571 ret
= regmap_read(regmap
, MLX90632_RAM_DSP5_EXTENDED_OBJECT_1
, &read_tmp
);
574 read
= (s16
)read_tmp
;
576 ret
= regmap_read(regmap
, MLX90632_RAM_DSP5_EXTENDED_OBJECT_2
, &read_tmp
);
579 read
= read
- (s16
)read_tmp
;
581 ret
= regmap_read(regmap
, MLX90632_RAM_DSP5_EXTENDED_OBJECT_3
, &read_tmp
);
584 read
= read
- (s16
)read_tmp
;
586 ret
= regmap_read(regmap
, MLX90632_RAM_DSP5_EXTENDED_OBJECT_4
, &read_tmp
);
589 read
= (read
+ (s16
)read_tmp
) / 2;
591 ret
= regmap_read(regmap
, MLX90632_RAM_DSP5_EXTENDED_OBJECT_5
, &read_tmp
);
594 read
= read
+ (s16
)read_tmp
;
596 ret
= regmap_read(regmap
, MLX90632_RAM_DSP5_EXTENDED_OBJECT_6
, &read_tmp
);
599 read
= read
+ (s16
)read_tmp
;
601 if (read
> S16_MAX
|| read
< S16_MIN
)
604 *object_new_raw
= read
;
609 static int mlx90632_read_all_channel_extended(struct mlx90632_data
*data
, s16
*object_new_raw
,
610 s16
*ambient_new_raw
, s16
*ambient_old_raw
)
614 mutex_lock(&data
->lock
);
615 ret
= mlx90632_set_meas_type(data
, MLX90632_MTYP_EXTENDED
);
619 switch (data
->powerstatus
) {
620 case MLX90632_PWR_STATUS_CONTINUOUS
:
621 ret
= read_poll_timeout(mlx90632_perform_measurement
, meas
, meas
== 19,
622 50000, 800000, false, data
);
626 case MLX90632_PWR_STATUS_SLEEP_STEP
:
627 ret
= mlx90632_perform_measurement_burst(data
);
636 ret
= mlx90632_read_object_raw_extended(data
->regmap
, object_new_raw
);
640 ret
= mlx90632_read_ambient_raw_extended(data
->regmap
, ambient_new_raw
, ambient_old_raw
);
643 mutex_unlock(&data
->lock
);
647 static int mlx90632_read_ee_register(struct regmap
*regmap
, u16 reg_lsb
,
654 ret
= regmap_read(regmap
, reg_lsb
, &read
);
660 ret
= regmap_read(regmap
, reg_lsb
+ 1, &read
);
664 *reg_value
= (read
<< 16) | (value
& 0xffff);
669 static s64
mlx90632_preprocess_temp_amb(s16 ambient_new_raw
,
670 s16 ambient_old_raw
, s16 Gb
)
674 kGb
= ((s64
)Gb
* 1000LL) >> 10ULL;
675 VR_Ta
= (s64
)ambient_old_raw
* 1000000LL +
676 kGb
* div64_s64(((s64
)ambient_new_raw
* 1000LL),
679 div64_s64(((s64
)ambient_new_raw
* 1000000000000LL),
680 (MLX90632_REF_3
)), VR_Ta
);
681 return div64_s64(tmp
<< 19ULL, 1000LL);
684 static s64
mlx90632_preprocess_temp_obj(s16 object_new_raw
, s16 object_old_raw
,
686 s16 ambient_old_raw
, s16 Ka
)
690 kKa
= ((s64
)Ka
* 1000LL) >> 10ULL;
691 VR_IR
= (s64
)ambient_old_raw
* 1000000LL +
692 kKa
* div64_s64(((s64
)ambient_new_raw
* 1000LL),
695 div64_s64(((s64
)((object_new_raw
+ object_old_raw
) / 2)
696 * 1000000000000LL), (MLX90632_REF_12
)),
698 return div64_s64((tmp
<< 19ULL), 1000LL);
701 static s64
mlx90632_preprocess_temp_obj_extended(s16 object_new_raw
, s16 ambient_new_raw
,
702 s16 ambient_old_raw
, s16 Ka
)
706 kKa
= ((s64
)Ka
* 1000LL) >> 10ULL;
707 VR_IR
= (s64
)ambient_old_raw
* 1000000LL +
708 kKa
* div64_s64((s64
)ambient_new_raw
* 1000LL,
711 div64_s64((s64
) object_new_raw
* 1000000000000LL, MLX90632_REF_12
),
713 return div64_s64(tmp
<< 19ULL, 1000LL);
716 static s32
mlx90632_calc_temp_ambient(s16 ambient_new_raw
, s16 ambient_old_raw
,
717 s32 P_T
, s32 P_R
, s32 P_G
, s32 P_O
, s16 Gb
)
719 s64 Asub
, Bsub
, Ablock
, Bblock
, Cblock
, AMB
, sum
;
721 AMB
= mlx90632_preprocess_temp_amb(ambient_new_raw
, ambient_old_raw
,
723 Asub
= ((s64
)P_T
* 10000000000LL) >> 44ULL;
724 Bsub
= AMB
- (((s64
)P_R
* 1000LL) >> 8ULL);
725 Ablock
= Asub
* (Bsub
* Bsub
);
726 Bblock
= (div64_s64(Bsub
* 10000000LL, P_G
)) << 20ULL;
727 Cblock
= ((s64
)P_O
* 10000000000LL) >> 8ULL;
729 sum
= div64_s64(Ablock
, 1000000LL) + Bblock
+ Cblock
;
731 return div64_s64(sum
, 10000000LL);
734 static s32
mlx90632_calc_temp_object_iteration(s32 prev_object_temp
, s64 object
,
735 s64 TAdut
, s64 TAdut4
, s32 Fa
, s32 Fb
,
736 s32 Ga
, s16 Ha
, s16 Hb
,
739 s64 calcedKsTO
, calcedKsTA
, ir_Alpha
, Alpha_corr
;
740 s64 Ha_customer
, Hb_customer
;
742 Ha_customer
= ((s64
)Ha
* 1000000LL) >> 14ULL;
743 Hb_customer
= ((s64
)Hb
* 100) >> 10ULL;
745 calcedKsTO
= ((s64
)((s64
)Ga
* (prev_object_temp
- 25 * 1000LL)
747 calcedKsTA
= ((s64
)(Fb
* (TAdut
- 25 * 1000000LL))) >> 36LL;
748 Alpha_corr
= div64_s64((((s64
)(Fa
* 10000000000LL) >> 46LL)
749 * Ha_customer
), 1000LL);
750 Alpha_corr
*= ((s64
)(1 * 1000000LL + calcedKsTO
+ calcedKsTA
));
751 Alpha_corr
= emissivity
* div64_s64(Alpha_corr
, 100000LL);
752 Alpha_corr
= div64_s64(Alpha_corr
, 1000LL);
753 ir_Alpha
= div64_s64((s64
)object
* 10000000LL, Alpha_corr
);
755 return (int_sqrt64(int_sqrt64(ir_Alpha
* 1000000000000LL + TAdut4
))
756 - 27315 - Hb_customer
) * 10;
759 static s64
mlx90632_calc_ta4(s64 TAdut
, s64 scale
)
761 return (div64_s64(TAdut
, scale
) + 27315) *
762 (div64_s64(TAdut
, scale
) + 27315) *
763 (div64_s64(TAdut
, scale
) + 27315) *
764 (div64_s64(TAdut
, scale
) + 27315);
767 static s32
mlx90632_calc_temp_object(s64 object
, s64 ambient
, s32 Ea
, s32 Eb
,
768 s32 Fa
, s32 Fb
, s32 Ga
, s16 Ha
, s16 Hb
,
771 s64 kTA
, kTA0
, TAdut
, TAdut4
;
775 kTA
= (Ea
* 1000LL) >> 16LL;
776 kTA0
= (Eb
* 1000LL) >> 8LL;
777 TAdut
= div64_s64(((ambient
- kTA0
) * 1000000LL), kTA
) + 25 * 1000000LL;
778 TAdut4
= mlx90632_calc_ta4(TAdut
, 10000LL);
780 /* Iterations of calculation as described in datasheet */
781 for (i
= 0; i
< 5; ++i
) {
782 temp
= mlx90632_calc_temp_object_iteration(temp
, object
, TAdut
, TAdut4
,
789 static s32
mlx90632_calc_temp_object_extended(s64 object
, s64 ambient
, s64 reflected
,
790 s32 Ea
, s32 Eb
, s32 Fa
, s32 Fb
, s32 Ga
,
791 s16 Ha
, s16 Hb
, u16 tmp_emi
)
793 s64 kTA
, kTA0
, TAdut
, TAdut4
, Tr4
, TaTr4
;
797 kTA
= (Ea
* 1000LL) >> 16LL;
798 kTA0
= (Eb
* 1000LL) >> 8LL;
799 TAdut
= div64_s64((ambient
- kTA0
) * 1000000LL, kTA
) + 25 * 1000000LL;
800 Tr4
= mlx90632_calc_ta4(reflected
, 10);
801 TAdut4
= mlx90632_calc_ta4(TAdut
, 10000LL);
802 TaTr4
= Tr4
- div64_s64(Tr4
- TAdut4
, tmp_emi
) * 1000;
804 /* Iterations of calculation as described in datasheet */
805 for (i
= 0; i
< 5; ++i
) {
806 temp
= mlx90632_calc_temp_object_iteration(temp
, object
, TAdut
, TaTr4
,
807 Fa
/ 2, Fb
, Ga
, Ha
, Hb
,
814 static int mlx90632_calc_object_dsp105(struct mlx90632_data
*data
, int *val
)
816 s16 ambient_new_raw
, ambient_old_raw
, object_new_raw
, object_old_raw
;
817 s32 Ea
, Eb
, Fa
, Fb
, Ga
;
818 unsigned int read_tmp
;
823 ret
= mlx90632_read_ee_register(data
->regmap
, MLX90632_EE_Ea
, &Ea
);
826 ret
= mlx90632_read_ee_register(data
->regmap
, MLX90632_EE_Eb
, &Eb
);
829 ret
= mlx90632_read_ee_register(data
->regmap
, MLX90632_EE_Fa
, &Fa
);
832 ret
= mlx90632_read_ee_register(data
->regmap
, MLX90632_EE_Fb
, &Fb
);
835 ret
= mlx90632_read_ee_register(data
->regmap
, MLX90632_EE_Ga
, &Ga
);
838 ret
= regmap_read(data
->regmap
, MLX90632_EE_Ha
, &read_tmp
);
842 ret
= regmap_read(data
->regmap
, MLX90632_EE_Hb
, &read_tmp
);
846 ret
= regmap_read(data
->regmap
, MLX90632_EE_Gb
, &read_tmp
);
850 ret
= regmap_read(data
->regmap
, MLX90632_EE_Ka
, &read_tmp
);
855 ret
= mlx90632_read_all_channel(data
,
856 &ambient_new_raw
, &ambient_old_raw
,
857 &object_new_raw
, &object_old_raw
);
861 if (object_new_raw
> MLX90632_EXTENDED_LIMIT
&&
862 data
->mtyp
== MLX90632_MTYP_EXTENDED
) {
863 ret
= mlx90632_read_all_channel_extended(data
, &object_new_raw
,
864 &ambient_new_raw
, &ambient_old_raw
);
868 /* Use extended mode calculations */
869 ambient
= mlx90632_preprocess_temp_amb(ambient_new_raw
,
870 ambient_old_raw
, Gb
);
871 object
= mlx90632_preprocess_temp_obj_extended(object_new_raw
,
873 ambient_old_raw
, Ka
);
874 *val
= mlx90632_calc_temp_object_extended(object
, ambient
,
875 data
->object_ambient_temperature
,
877 Ha
, Hb
, data
->emissivity
);
881 ambient
= mlx90632_preprocess_temp_amb(ambient_new_raw
,
882 ambient_old_raw
, Gb
);
883 object
= mlx90632_preprocess_temp_obj(object_new_raw
,
886 ambient_old_raw
, Ka
);
888 *val
= mlx90632_calc_temp_object(object
, ambient
, Ea
, Eb
, Fa
, Fb
, Ga
,
889 Ha
, Hb
, data
->emissivity
);
893 static int mlx90632_calc_ambient_dsp105(struct mlx90632_data
*data
, int *val
)
895 s16 ambient_new_raw
, ambient_old_raw
;
896 unsigned int read_tmp
;
901 ret
= mlx90632_read_ee_register(data
->regmap
, MLX90632_EE_P_R
, &PR
);
904 ret
= mlx90632_read_ee_register(data
->regmap
, MLX90632_EE_P_G
, &PG
);
907 ret
= mlx90632_read_ee_register(data
->regmap
, MLX90632_EE_P_T
, &PT
);
910 ret
= mlx90632_read_ee_register(data
->regmap
, MLX90632_EE_P_O
, &PO
);
913 ret
= regmap_read(data
->regmap
, MLX90632_EE_Gb
, &read_tmp
);
918 ret
= mlx90632_read_ambient_raw(data
->regmap
, &ambient_new_raw
,
922 *val
= mlx90632_calc_temp_ambient(ambient_new_raw
, ambient_old_raw
,
927 static int mlx90632_get_refresh_rate(struct mlx90632_data
*data
,
933 ret
= regmap_read(data
->regmap
, MLX90632_EE_MEDICAL_MEAS1
, &meas1
);
937 *refresh_rate
= MLX90632_REFRESH_RATE(meas1
);
942 static const int mlx90632_freqs
[][2] = {
954 * mlx90632_pm_interraction_wakeup() - Measure time between user interactions to change powermode
955 * @data: pointer to mlx90632_data object containing interaction_ts information
957 * Switch to continuous mode when interaction is faster than MLX90632_MEAS_MAX_TIME. Update the
958 * interaction_ts for each function call with the jiffies to enable measurement between function
959 * calls. Initial value of the interaction_ts needs to be set before this function call.
961 static int mlx90632_pm_interraction_wakeup(struct mlx90632_data
*data
)
967 if (time_in_range(now
, data
->interaction_ts
,
968 data
->interaction_ts
+
969 msecs_to_jiffies(MLX90632_MEAS_MAX_TIME
+ 100))) {
970 if (data
->powerstatus
== MLX90632_PWR_STATUS_SLEEP_STEP
) {
971 ret
= mlx90632_pwr_continuous(data
->regmap
);
977 data
->interaction_ts
= now
;
982 static int mlx90632_read_raw(struct iio_dev
*indio_dev
,
983 struct iio_chan_spec
const *channel
, int *val
,
984 int *val2
, long mask
)
986 struct mlx90632_data
*data
= iio_priv(indio_dev
);
990 pm_runtime_get_sync(&data
->client
->dev
);
991 ret
= mlx90632_pm_interraction_wakeup(data
);
993 goto mlx90632_read_raw_pm
;
996 case IIO_CHAN_INFO_PROCESSED
:
997 switch (channel
->channel2
) {
998 case IIO_MOD_TEMP_AMBIENT
:
999 ret
= mlx90632_calc_ambient_dsp105(data
, val
);
1001 goto mlx90632_read_raw_pm
;
1005 case IIO_MOD_TEMP_OBJECT
:
1006 ret
= mlx90632_calc_object_dsp105(data
, val
);
1008 goto mlx90632_read_raw_pm
;
1017 case IIO_CHAN_INFO_CALIBEMISSIVITY
:
1018 if (data
->emissivity
== 1000) {
1023 *val2
= data
->emissivity
* 1000;
1025 ret
= IIO_VAL_INT_PLUS_MICRO
;
1027 case IIO_CHAN_INFO_CALIBAMBIENT
:
1028 *val
= data
->object_ambient_temperature
;
1031 case IIO_CHAN_INFO_SAMP_FREQ
:
1032 ret
= mlx90632_get_refresh_rate(data
, &cr
);
1034 goto mlx90632_read_raw_pm
;
1036 *val
= mlx90632_freqs
[cr
][0];
1037 *val2
= mlx90632_freqs
[cr
][1];
1038 ret
= IIO_VAL_INT_PLUS_MICRO
;
1045 mlx90632_read_raw_pm
:
1046 pm_runtime_mark_last_busy(&data
->client
->dev
);
1047 pm_runtime_put_autosuspend(&data
->client
->dev
);
1051 static int mlx90632_write_raw(struct iio_dev
*indio_dev
,
1052 struct iio_chan_spec
const *channel
, int val
,
1053 int val2
, long mask
)
1055 struct mlx90632_data
*data
= iio_priv(indio_dev
);
1058 case IIO_CHAN_INFO_CALIBEMISSIVITY
:
1059 /* Confirm we are within 0 and 1.0 */
1060 if (val
< 0 || val2
< 0 || val
> 1 ||
1061 (val
== 1 && val2
!= 0))
1063 data
->emissivity
= val
* 1000 + val2
/ 1000;
1065 case IIO_CHAN_INFO_CALIBAMBIENT
:
1066 data
->object_ambient_temperature
= val
;
1073 static int mlx90632_read_avail(struct iio_dev
*indio_dev
,
1074 struct iio_chan_spec
const *chan
,
1075 const int **vals
, int *type
, int *length
,
1079 case IIO_CHAN_INFO_SAMP_FREQ
:
1080 *vals
= (int *)mlx90632_freqs
;
1081 *type
= IIO_VAL_INT_PLUS_MICRO
;
1082 *length
= 2 * ARRAY_SIZE(mlx90632_freqs
);
1083 return IIO_AVAIL_LIST
;
1089 static const struct iio_chan_spec mlx90632_channels
[] = {
1093 .channel2
= IIO_MOD_TEMP_AMBIENT
,
1094 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1095 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
1096 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
1101 .channel2
= IIO_MOD_TEMP_OBJECT
,
1102 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
1103 BIT(IIO_CHAN_INFO_CALIBEMISSIVITY
) | BIT(IIO_CHAN_INFO_CALIBAMBIENT
),
1104 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
1105 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
1109 static const struct iio_info mlx90632_info
= {
1110 .read_raw
= mlx90632_read_raw
,
1111 .write_raw
= mlx90632_write_raw
,
1112 .read_avail
= mlx90632_read_avail
,
1115 static void mlx90632_sleep(void *_data
)
1117 struct mlx90632_data
*data
= _data
;
1119 mlx90632_pwr_set_sleep_step(data
->regmap
);
1122 static int mlx90632_suspend(struct mlx90632_data
*data
)
1124 regcache_mark_dirty(data
->regmap
);
1126 dev_dbg(&data
->client
->dev
, "Requesting suspend");
1127 return mlx90632_pwr_set_sleep_step(data
->regmap
);
1130 static int mlx90632_wakeup(struct mlx90632_data
*data
)
1134 ret
= regcache_sync(data
->regmap
);
1136 dev_err(&data
->client
->dev
,
1137 "Failed to sync regmap registers: %d\n", ret
);
1141 dev_dbg(&data
->client
->dev
, "Requesting wake-up\n");
1142 return mlx90632_pwr_continuous(data
->regmap
);
1145 static void mlx90632_disable_regulator(void *_data
)
1147 struct mlx90632_data
*data
= _data
;
1150 ret
= regulator_disable(data
->regulator
);
1152 dev_err(regmap_get_device(data
->regmap
),
1153 "Failed to disable power regulator: %d\n", ret
);
1156 static int mlx90632_enable_regulator(struct mlx90632_data
*data
)
1160 ret
= regulator_enable(data
->regulator
);
1162 dev_err(regmap_get_device(data
->regmap
), "Failed to enable power regulator!\n");
1166 mlx90632_reset_delay();
1171 static int mlx90632_probe(struct i2c_client
*client
)
1173 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
1174 struct mlx90632_data
*mlx90632
;
1175 struct iio_dev
*indio_dev
;
1176 struct regmap
*regmap
;
1180 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*mlx90632
));
1182 dev_err(&client
->dev
, "Failed to allocate device\n");
1186 regmap
= devm_regmap_init_i2c(client
, &mlx90632_regmap
);
1187 if (IS_ERR(regmap
)) {
1188 ret
= PTR_ERR(regmap
);
1189 dev_err(&client
->dev
, "Failed to allocate regmap: %d\n", ret
);
1193 mlx90632
= iio_priv(indio_dev
);
1194 i2c_set_clientdata(client
, indio_dev
);
1195 mlx90632
->client
= client
;
1196 mlx90632
->regmap
= regmap
;
1197 mlx90632
->mtyp
= MLX90632_MTYP_MEDICAL
;
1198 mlx90632
->powerstatus
= MLX90632_PWR_STATUS_HALT
;
1200 mutex_init(&mlx90632
->lock
);
1201 indio_dev
->name
= id
->name
;
1202 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1203 indio_dev
->info
= &mlx90632_info
;
1204 indio_dev
->channels
= mlx90632_channels
;
1205 indio_dev
->num_channels
= ARRAY_SIZE(mlx90632_channels
);
1207 mlx90632
->regulator
= devm_regulator_get(&client
->dev
, "vdd");
1208 if (IS_ERR(mlx90632
->regulator
))
1209 return dev_err_probe(&client
->dev
, PTR_ERR(mlx90632
->regulator
),
1210 "failed to get vdd regulator");
1212 ret
= mlx90632_enable_regulator(mlx90632
);
1216 ret
= devm_add_action_or_reset(&client
->dev
, mlx90632_disable_regulator
,
1219 dev_err(&client
->dev
, "Failed to setup regulator cleanup action %d\n",
1224 ret
= mlx90632_wakeup(mlx90632
);
1226 dev_err(&client
->dev
, "Wakeup failed: %d\n", ret
);
1230 ret
= devm_add_action_or_reset(&client
->dev
, mlx90632_sleep
, mlx90632
);
1232 dev_err(&client
->dev
, "Failed to setup low power cleanup action %d\n",
1237 ret
= regmap_read(mlx90632
->regmap
, MLX90632_EE_VERSION
, &read
);
1239 dev_err(&client
->dev
, "read of version failed: %d\n", ret
);
1242 read
= read
& MLX90632_ID_MASK
;
1243 if (read
== MLX90632_ID_MEDICAL
) {
1244 dev_dbg(&client
->dev
,
1245 "Detected Medical EEPROM calibration %x\n", read
);
1246 } else if (read
== MLX90632_ID_CONSUMER
) {
1247 dev_dbg(&client
->dev
,
1248 "Detected Consumer EEPROM calibration %x\n", read
);
1249 } else if (read
== MLX90632_ID_EXTENDED
) {
1250 dev_dbg(&client
->dev
,
1251 "Detected Extended range EEPROM calibration %x\n", read
);
1252 mlx90632
->mtyp
= MLX90632_MTYP_EXTENDED
;
1253 } else if ((read
& MLX90632_DSP_MASK
) == MLX90632_DSP_VERSION
) {
1254 dev_dbg(&client
->dev
,
1255 "Detected Unknown EEPROM calibration %x\n", read
);
1257 dev_err(&client
->dev
,
1258 "Wrong DSP version %x (expected %x)\n",
1259 read
, MLX90632_DSP_VERSION
);
1260 return -EPROTONOSUPPORT
;
1263 mlx90632
->emissivity
= 1000;
1264 mlx90632
->object_ambient_temperature
= 25000; /* 25 degrees milliCelsius */
1265 mlx90632
->interaction_ts
= jiffies
; /* Set initial value */
1267 pm_runtime_get_noresume(&client
->dev
);
1268 pm_runtime_set_active(&client
->dev
);
1270 ret
= devm_pm_runtime_enable(&client
->dev
);
1274 pm_runtime_set_autosuspend_delay(&client
->dev
, MLX90632_SLEEP_DELAY_MS
);
1275 pm_runtime_use_autosuspend(&client
->dev
);
1276 pm_runtime_put_autosuspend(&client
->dev
);
1278 return devm_iio_device_register(&client
->dev
, indio_dev
);
1281 static const struct i2c_device_id mlx90632_id
[] = {
1285 MODULE_DEVICE_TABLE(i2c
, mlx90632_id
);
1287 static const struct of_device_id mlx90632_of_match
[] = {
1288 { .compatible
= "melexis,mlx90632" },
1291 MODULE_DEVICE_TABLE(of
, mlx90632_of_match
);
1293 static int mlx90632_pm_suspend(struct device
*dev
)
1295 struct mlx90632_data
*data
= iio_priv(dev_get_drvdata(dev
));
1298 ret
= mlx90632_suspend(data
);
1302 ret
= regulator_disable(data
->regulator
);
1304 dev_err(regmap_get_device(data
->regmap
),
1305 "Failed to disable power regulator: %d\n", ret
);
1310 static int mlx90632_pm_resume(struct device
*dev
)
1312 struct mlx90632_data
*data
= iio_priv(dev_get_drvdata(dev
));
1315 ret
= mlx90632_enable_regulator(data
);
1319 return mlx90632_wakeup(data
);
1322 static int mlx90632_pm_runtime_suspend(struct device
*dev
)
1324 struct mlx90632_data
*data
= iio_priv(dev_get_drvdata(dev
));
1326 return mlx90632_pwr_set_sleep_step(data
->regmap
);
1329 static const struct dev_pm_ops mlx90632_pm_ops
= {
1330 SYSTEM_SLEEP_PM_OPS(mlx90632_pm_suspend
, mlx90632_pm_resume
)
1331 RUNTIME_PM_OPS(mlx90632_pm_runtime_suspend
, NULL
, NULL
)
1334 static struct i2c_driver mlx90632_driver
= {
1337 .of_match_table
= mlx90632_of_match
,
1338 .pm
= pm_ptr(&mlx90632_pm_ops
),
1340 .probe
= mlx90632_probe
,
1341 .id_table
= mlx90632_id
,
1343 module_i2c_driver(mlx90632_driver
);
1345 MODULE_AUTHOR("Crt Mori <cmo@melexis.com>");
1346 MODULE_DESCRIPTION("Melexis MLX90632 contactless Infra Red temperature sensor driver");
1347 MODULE_LICENSE("GPL v2");