1
2
3
4
5
6
7
8
9#include <linux/delay.h>
10#include <linux/err.h>
11#include <linux/gpio/consumer.h>
12#include <linux/i2c.h>
13#include <linux/iopoll.h>
14#include <linux/kernel.h>
15#include <linux/limits.h>
16#include <linux/module.h>
17#include <linux/math64.h>
18#include <linux/of.h>
19#include <linux/pm_runtime.h>
20#include <linux/regmap.h>
21
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24
25
26#define MLX90632_ADDR_RAM 0x4000
27#define MLX90632_ADDR_EEPROM 0x2480
28
29
30#define MLX90632_EE_CTRL 0x24d4
31#define MLX90632_EE_I2C_ADDR 0x24d5
32#define MLX90632_EE_VERSION 0x240b
33#define MLX90632_EE_P_R 0x240c
34#define MLX90632_EE_P_G 0x240e
35#define MLX90632_EE_P_T 0x2410
36#define MLX90632_EE_P_O 0x2412
37#define MLX90632_EE_Aa 0x2414
38#define MLX90632_EE_Ab 0x2416
39#define MLX90632_EE_Ba 0x2418
40#define MLX90632_EE_Bb 0x241a
41#define MLX90632_EE_Ca 0x241c
42#define MLX90632_EE_Cb 0x241e
43#define MLX90632_EE_Da 0x2420
44#define MLX90632_EE_Db 0x2422
45#define MLX90632_EE_Ea 0x2424
46#define MLX90632_EE_Eb 0x2426
47#define MLX90632_EE_Fa 0x2428
48#define MLX90632_EE_Fb 0x242a
49#define MLX90632_EE_Ga 0x242c
50
51#define MLX90632_EE_Gb 0x242e
52#define MLX90632_EE_Ka 0x242f
53
54#define MLX90632_EE_Ha 0x2481
55#define MLX90632_EE_Hb 0x2482
56
57
58#define MLX90632_REG_I2C_ADDR 0x3000
59
60
61#define MLX90632_REG_CONTROL 0x3001
62#define MLX90632_CFG_PWR_MASK GENMASK(2, 1)
63#define MLX90632_CFG_MTYP_MASK GENMASK(8, 4)
64
65
66#define MLX90632_PWR_STATUS(ctrl_val) (ctrl_val << 1)
67#define MLX90632_PWR_STATUS_HALT MLX90632_PWR_STATUS(0)
68#define MLX90632_PWR_STATUS_SLEEP_STEP MLX90632_PWR_STATUS(1)
69#define MLX90632_PWR_STATUS_STEP MLX90632_PWR_STATUS(2)
70#define MLX90632_PWR_STATUS_CONTINUOUS MLX90632_PWR_STATUS(3)
71
72
73#define MLX90632_MTYP_MEDICAL 0
74#define MLX90632_MTYP_EXTENDED 17
75
76
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
82#define MLX90632_REG_I2C_CMD 0x3005
83
84
85#define MLX90632_REG_STATUS 0x3fff
86#define MLX90632_STAT_BUSY BIT(10)
87#define MLX90632_STAT_EE_BUSY BIT(9)
88#define MLX90632_STAT_BRST BIT(8)
89#define MLX90632_STAT_CYCLE_POS GENMASK(6, 2)
90#define MLX90632_STAT_DATA_RDY BIT(0)
91
92
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
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
108#define MLX90632_ID_MEDICAL 0x0105
109#define MLX90632_ID_CONSUMER 0x0205
110#define MLX90632_ID_EXTENDED 0x0505
111#define MLX90632_ID_MASK GENMASK(14, 0)
112#define MLX90632_DSP_VERSION 5
113#define MLX90632_DSP_MASK GENMASK(7, 0)
114#define MLX90632_RESET_CMD 0x0006
115#define MLX90632_REF_12 12LL
116#define MLX90632_REF_3 12LL
117#define MLX90632_MAX_MEAS_NUM 31
118#define MLX90632_SLEEP_DELAY_MS 3000
119#define MLX90632_EXTENDED_LIMIT 27000
120
121
122
123
124
125
126
127
128
129
130
131
132struct 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
141static 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
149static 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
154static 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
165static 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
170static 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
176static 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
181static 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
196static 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
203static 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
212
213
214
215
216
217
218static 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
240static 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
253
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
266static 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
285static 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
304static 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
343static 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);
362read_unlock:
363 mutex_unlock(&data->lock);
364 return ret;
365}
366
367static 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
386static 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
430static 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
451read_unlock:
452 (void) mlx90632_set_meas_type(data->regmap, MLX90632_MTYP_MEDICAL);
453
454 mutex_unlock(&data->lock);
455 return ret;
456}
457
458static 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
480static 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
495static 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
512static 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
527static 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
545static 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
570static 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
578static 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
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
600static 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
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
625static 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
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
704static 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
738static 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
778static 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
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
800static 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
816static const struct iio_info mlx90632_info = {
817 .read_raw = mlx90632_read_raw,
818 .write_raw = mlx90632_write_raw,
819};
820
821static 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
829static 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
844static 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;
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
927static 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
943static const struct i2c_device_id mlx90632_id[] = {
944 { "mlx90632", 0 },
945 { }
946};
947MODULE_DEVICE_TABLE(i2c, mlx90632_id);
948
949static const struct of_device_id mlx90632_of_match[] = {
950 { .compatible = "melexis,mlx90632" },
951 { }
952};
953MODULE_DEVICE_TABLE(of, mlx90632_of_match);
954
955static 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
963static 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
971static UNIVERSAL_DEV_PM_OPS(mlx90632_pm_ops, mlx90632_pm_suspend,
972 mlx90632_pm_resume, NULL);
973
974static 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};
984module_i2c_driver(mlx90632_driver);
985
986MODULE_AUTHOR("Crt Mori <cmo@melexis.com>");
987MODULE_DESCRIPTION("Melexis MLX90632 contactless Infra Red temperature sensor driver");
988MODULE_LICENSE("GPL v2");
989