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