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