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