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_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 */
89 
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;
95 };
96 
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)),
102 };
103 
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),
107 };
108 
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)),
117 };
118 
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),
122 };
123 
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)),
128 };
129 
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),
133 };
134 
135 static const struct regmap_config mlx90632_regmap = {
136 	.reg_bits = 16,
137 	.val_bits = 16,
138 
139 	.volatile_table = &mlx90632_volatile_regs_tbl,
140 	.rd_table = &mlx90632_readable_regs_tbl,
141 	.wr_table = &mlx90632_writeable_regs_tbl,
142 
143 	.use_single_rw = true,
144 	.reg_format_endian = REGMAP_ENDIAN_BIG,
145 	.val_format_endian = REGMAP_ENDIAN_BIG,
146 	.cache_type = REGCACHE_RBTREE,
147 };
148 
mlx90632_pwr_set_sleep_step(struct regmap * regmap)149 static s32 mlx90632_pwr_set_sleep_step(struct regmap *regmap)
150 {
151 	return regmap_update_bits(regmap, MLX90632_REG_CONTROL,
152 				  MLX90632_CFG_PWR_MASK,
153 				  MLX90632_PWR_STATUS_SLEEP_STEP);
154 }
155 
mlx90632_pwr_continuous(struct regmap * regmap)156 static s32 mlx90632_pwr_continuous(struct regmap *regmap)
157 {
158 	return regmap_update_bits(regmap, MLX90632_REG_CONTROL,
159 				  MLX90632_CFG_PWR_MASK,
160 				  MLX90632_PWR_STATUS_CONTINUOUS);
161 }
162 
163 /**
164  * mlx90632_perform_measurement - Trigger and retrieve current measurement cycle
165  * @*data: pointer to mlx90632_data object containing regmap information
166  *
167  * Perform a measurement and return latest measurement cycle position reported
168  * by sensor. This is a blocking function for 500ms, as that is default sensor
169  * refresh rate.
170  */
mlx90632_perform_measurement(struct mlx90632_data * data)171 static int mlx90632_perform_measurement(struct mlx90632_data *data)
172 {
173 	int ret, tries = 100;
174 	unsigned int reg_status;
175 
176 	ret = regmap_update_bits(data->regmap, MLX90632_REG_STATUS,
177 				 MLX90632_STAT_DATA_RDY, 0);
178 	if (ret < 0)
179 		return ret;
180 
181 	while (tries-- > 0) {
182 		ret = regmap_read(data->regmap, MLX90632_REG_STATUS,
183 				  &reg_status);
184 		if (ret < 0)
185 			return ret;
186 		if (reg_status & MLX90632_STAT_DATA_RDY)
187 			break;
188 		usleep_range(10000, 11000);
189 	}
190 
191 	if (tries < 0) {
192 		dev_err(&data->client->dev, "data not ready");
193 		return -ETIMEDOUT;
194 	}
195 
196 	return (reg_status & MLX90632_STAT_CYCLE_POS) >> 2;
197 }
198 
mlx90632_channel_new_select(int perform_ret,uint8_t * channel_new,uint8_t * channel_old)199 static int mlx90632_channel_new_select(int perform_ret, uint8_t *channel_new,
200 				       uint8_t *channel_old)
201 {
202 	switch (perform_ret) {
203 	case 1:
204 		*channel_new = 1;
205 		*channel_old = 2;
206 		break;
207 	case 2:
208 		*channel_new = 2;
209 		*channel_old = 1;
210 		break;
211 	default:
212 		return -EINVAL;
213 	}
214 
215 	return 0;
216 }
217 
mlx90632_read_ambient_raw(struct regmap * regmap,s16 * ambient_new_raw,s16 * ambient_old_raw)218 static int mlx90632_read_ambient_raw(struct regmap *regmap,
219 				     s16 *ambient_new_raw, s16 *ambient_old_raw)
220 {
221 	int ret;
222 	unsigned int read_tmp;
223 
224 	ret = regmap_read(regmap, MLX90632_RAM_3(1), &read_tmp);
225 	if (ret < 0)
226 		return ret;
227 	*ambient_new_raw = (s16)read_tmp;
228 
229 	ret = regmap_read(regmap, MLX90632_RAM_3(2), &read_tmp);
230 	if (ret < 0)
231 		return ret;
232 	*ambient_old_raw = (s16)read_tmp;
233 
234 	return ret;
235 }
236 
mlx90632_read_object_raw(struct regmap * regmap,int perform_measurement_ret,s16 * object_new_raw,s16 * object_old_raw)237 static int mlx90632_read_object_raw(struct regmap *regmap,
238 				    int perform_measurement_ret,
239 				    s16 *object_new_raw, s16 *object_old_raw)
240 {
241 	int ret;
242 	unsigned int read_tmp;
243 	s16 read;
244 	u8 channel = 0;
245 	u8 channel_old = 0;
246 
247 	ret = mlx90632_channel_new_select(perform_measurement_ret, &channel,
248 					  &channel_old);
249 	if (ret != 0)
250 		return ret;
251 
252 	ret = regmap_read(regmap, MLX90632_RAM_2(channel), &read_tmp);
253 	if (ret < 0)
254 		return ret;
255 
256 	read = (s16)read_tmp;
257 
258 	ret = regmap_read(regmap, MLX90632_RAM_1(channel), &read_tmp);
259 	if (ret < 0)
260 		return ret;
261 	*object_new_raw = (read + (s16)read_tmp) / 2;
262 
263 	ret = regmap_read(regmap, MLX90632_RAM_2(channel_old), &read_tmp);
264 	if (ret < 0)
265 		return ret;
266 	read = (s16)read_tmp;
267 
268 	ret = regmap_read(regmap, MLX90632_RAM_1(channel_old), &read_tmp);
269 	if (ret < 0)
270 		return ret;
271 	*object_old_raw = (read + (s16)read_tmp) / 2;
272 
273 	return ret;
274 }
275 
mlx90632_read_all_channel(struct mlx90632_data * data,s16 * ambient_new_raw,s16 * ambient_old_raw,s16 * object_new_raw,s16 * object_old_raw)276 static int mlx90632_read_all_channel(struct mlx90632_data *data,
277 				     s16 *ambient_new_raw, s16 *ambient_old_raw,
278 				     s16 *object_new_raw, s16 *object_old_raw)
279 {
280 	s32 ret, measurement;
281 
282 	mutex_lock(&data->lock);
283 	measurement = mlx90632_perform_measurement(data);
284 	if (measurement < 0) {
285 		ret = measurement;
286 		goto read_unlock;
287 	}
288 	ret = mlx90632_read_ambient_raw(data->regmap, ambient_new_raw,
289 					ambient_old_raw);
290 	if (ret < 0)
291 		goto read_unlock;
292 
293 	ret = mlx90632_read_object_raw(data->regmap, measurement,
294 				       object_new_raw, object_old_raw);
295 read_unlock:
296 	mutex_unlock(&data->lock);
297 	return ret;
298 }
299 
mlx90632_read_ee_register(struct regmap * regmap,u16 reg_lsb,s32 * reg_value)300 static int mlx90632_read_ee_register(struct regmap *regmap, u16 reg_lsb,
301 				     s32 *reg_value)
302 {
303 	s32 ret;
304 	unsigned int read;
305 	u32 value;
306 
307 	ret = regmap_read(regmap, reg_lsb, &read);
308 	if (ret < 0)
309 		return ret;
310 
311 	value = read;
312 
313 	ret = regmap_read(regmap, reg_lsb + 1, &read);
314 	if (ret < 0)
315 		return ret;
316 
317 	*reg_value = (read << 16) | (value & 0xffff);
318 
319 	return 0;
320 }
321 
mlx90632_preprocess_temp_amb(s16 ambient_new_raw,s16 ambient_old_raw,s16 Gb)322 static s64 mlx90632_preprocess_temp_amb(s16 ambient_new_raw,
323 					s16 ambient_old_raw, s16 Gb)
324 {
325 	s64 VR_Ta, kGb, tmp;
326 
327 	kGb = ((s64)Gb * 1000LL) >> 10ULL;
328 	VR_Ta = (s64)ambient_old_raw * 1000000LL +
329 		kGb * div64_s64(((s64)ambient_new_raw * 1000LL),
330 			(MLX90632_REF_3));
331 	tmp = div64_s64(
332 			 div64_s64(((s64)ambient_new_raw * 1000000000000LL),
333 				   (MLX90632_REF_3)), VR_Ta);
334 	return div64_s64(tmp << 19ULL, 1000LL);
335 }
336 
mlx90632_preprocess_temp_obj(s16 object_new_raw,s16 object_old_raw,s16 ambient_new_raw,s16 ambient_old_raw,s16 Ka)337 static s64 mlx90632_preprocess_temp_obj(s16 object_new_raw, s16 object_old_raw,
338 					s16 ambient_new_raw,
339 					s16 ambient_old_raw, s16 Ka)
340 {
341 	s64 VR_IR, kKa, tmp;
342 
343 	kKa = ((s64)Ka * 1000LL) >> 10ULL;
344 	VR_IR = (s64)ambient_old_raw * 1000000LL +
345 		kKa * div64_s64(((s64)ambient_new_raw * 1000LL),
346 			(MLX90632_REF_3));
347 	tmp = div64_s64(
348 			div64_s64(((s64)((object_new_raw + object_old_raw) / 2)
349 				   * 1000000000000LL), (MLX90632_REF_12)),
350 			VR_IR);
351 	return div64_s64((tmp << 19ULL), 1000LL);
352 }
353 
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)354 static s32 mlx90632_calc_temp_ambient(s16 ambient_new_raw, s16 ambient_old_raw,
355 				      s32 P_T, s32 P_R, s32 P_G, s32 P_O,
356 				      s16 Gb)
357 {
358 	s64 Asub, Bsub, Ablock, Bblock, Cblock, AMB, sum;
359 
360 	AMB = mlx90632_preprocess_temp_amb(ambient_new_raw, ambient_old_raw,
361 					   Gb);
362 	Asub = ((s64)P_T * 10000000000LL) >> 44ULL;
363 	Bsub = AMB - (((s64)P_R * 1000LL) >> 8ULL);
364 	Ablock = Asub * (Bsub * Bsub);
365 	Bblock = (div64_s64(Bsub * 10000000LL, P_G)) << 20ULL;
366 	Cblock = ((s64)P_O * 10000000000LL) >> 8ULL;
367 
368 	sum = div64_s64(Ablock, 1000000LL) + Bblock + Cblock;
369 
370 	return div64_s64(sum, 10000000LL);
371 }
372 
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)373 static s32 mlx90632_calc_temp_object_iteration(s32 prev_object_temp, s64 object,
374 					       s64 TAdut, s32 Fa, s32 Fb,
375 					       s32 Ga, s16 Ha, s16 Hb,
376 					       u16 emissivity)
377 {
378 	s64 calcedKsTO, calcedKsTA, ir_Alpha, TAdut4, Alpha_corr;
379 	s64 Ha_customer, Hb_customer;
380 
381 	Ha_customer = ((s64)Ha * 1000000LL) >> 14ULL;
382 	Hb_customer = ((s64)Hb * 100) >> 10ULL;
383 
384 	calcedKsTO = ((s64)((s64)Ga * (prev_object_temp - 25 * 1000LL)
385 			     * 1000LL)) >> 36LL;
386 	calcedKsTA = ((s64)(Fb * (TAdut - 25 * 1000000LL))) >> 36LL;
387 	Alpha_corr = div64_s64((((s64)(Fa * 10000000000LL) >> 46LL)
388 				* Ha_customer), 1000LL);
389 	Alpha_corr *= ((s64)(1 * 1000000LL + calcedKsTO + calcedKsTA));
390 	Alpha_corr = emissivity * div64_s64(Alpha_corr, 100000LL);
391 	Alpha_corr = div64_s64(Alpha_corr, 1000LL);
392 	ir_Alpha = div64_s64((s64)object * 10000000LL, Alpha_corr);
393 	TAdut4 = (div64_s64(TAdut, 10000LL) + 27315) *
394 		(div64_s64(TAdut, 10000LL) + 27315) *
395 		(div64_s64(TAdut, 10000LL)  + 27315) *
396 		(div64_s64(TAdut, 10000LL) + 27315);
397 
398 	return (int_sqrt64(int_sqrt64(ir_Alpha * 1000000000000LL + TAdut4))
399 		- 27315 - Hb_customer) * 10;
400 }
401 
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)402 static s32 mlx90632_calc_temp_object(s64 object, s64 ambient, s32 Ea, s32 Eb,
403 				     s32 Fa, s32 Fb, s32 Ga, s16 Ha, s16 Hb,
404 				     u16 tmp_emi)
405 {
406 	s64 kTA, kTA0, TAdut;
407 	s64 temp = 25000;
408 	s8 i;
409 
410 	kTA = (Ea * 1000LL) >> 16LL;
411 	kTA0 = (Eb * 1000LL) >> 8LL;
412 	TAdut = div64_s64(((ambient - kTA0) * 1000000LL), kTA) + 25 * 1000000LL;
413 
414 	/* Iterations of calculation as described in datasheet */
415 	for (i = 0; i < 5; ++i) {
416 		temp = mlx90632_calc_temp_object_iteration(temp, object, TAdut,
417 							   Fa, Fb, Ga, Ha, Hb,
418 							   tmp_emi);
419 	}
420 	return temp;
421 }
422 
mlx90632_calc_object_dsp105(struct mlx90632_data * data,int * val)423 static int mlx90632_calc_object_dsp105(struct mlx90632_data *data, int *val)
424 {
425 	s32 ret;
426 	s32 Ea, Eb, Fa, Fb, Ga;
427 	unsigned int read_tmp;
428 	s16 Ha, Hb, Gb, Ka;
429 	s16 ambient_new_raw, ambient_old_raw, object_new_raw, object_old_raw;
430 	s64 object, ambient;
431 
432 	ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Ea, &Ea);
433 	if (ret < 0)
434 		return ret;
435 	ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Eb, &Eb);
436 	if (ret < 0)
437 		return ret;
438 	ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Fa, &Fa);
439 	if (ret < 0)
440 		return ret;
441 	ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Fb, &Fb);
442 	if (ret < 0)
443 		return ret;
444 	ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Ga, &Ga);
445 	if (ret < 0)
446 		return ret;
447 	ret = regmap_read(data->regmap, MLX90632_EE_Ha, &read_tmp);
448 	if (ret < 0)
449 		return ret;
450 	Ha = (s16)read_tmp;
451 	ret = regmap_read(data->regmap, MLX90632_EE_Hb, &read_tmp);
452 	if (ret < 0)
453 		return ret;
454 	Hb = (s16)read_tmp;
455 	ret = regmap_read(data->regmap, MLX90632_EE_Gb, &read_tmp);
456 	if (ret < 0)
457 		return ret;
458 	Gb = (s16)read_tmp;
459 	ret = regmap_read(data->regmap, MLX90632_EE_Ka, &read_tmp);
460 	if (ret < 0)
461 		return ret;
462 	Ka = (s16)read_tmp;
463 
464 	ret = mlx90632_read_all_channel(data,
465 					&ambient_new_raw, &ambient_old_raw,
466 					&object_new_raw, &object_old_raw);
467 	if (ret < 0)
468 		return ret;
469 
470 	ambient = mlx90632_preprocess_temp_amb(ambient_new_raw,
471 					       ambient_old_raw, Gb);
472 	object = mlx90632_preprocess_temp_obj(object_new_raw,
473 					      object_old_raw,
474 					      ambient_new_raw,
475 					      ambient_old_raw, Ka);
476 
477 	*val = mlx90632_calc_temp_object(object, ambient, Ea, Eb, Fa, Fb, Ga,
478 					 Ha, Hb, data->emissivity);
479 	return 0;
480 }
481 
mlx90632_calc_ambient_dsp105(struct mlx90632_data * data,int * val)482 static int mlx90632_calc_ambient_dsp105(struct mlx90632_data *data, int *val)
483 {
484 	s32 ret;
485 	unsigned int read_tmp;
486 	s32 PT, PR, PG, PO;
487 	s16 Gb;
488 	s16 ambient_new_raw, ambient_old_raw;
489 
490 	ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_R, &PR);
491 	if (ret < 0)
492 		return ret;
493 	ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_G, &PG);
494 	if (ret < 0)
495 		return ret;
496 	ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_T, &PT);
497 	if (ret < 0)
498 		return ret;
499 	ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_O, &PO);
500 	if (ret < 0)
501 		return ret;
502 	ret = regmap_read(data->regmap, MLX90632_EE_Gb, &read_tmp);
503 	if (ret < 0)
504 		return ret;
505 	Gb = (s16)read_tmp;
506 
507 	ret = mlx90632_read_ambient_raw(data->regmap, &ambient_new_raw,
508 					&ambient_old_raw);
509 	if (ret < 0)
510 		return ret;
511 	*val = mlx90632_calc_temp_ambient(ambient_new_raw, ambient_old_raw,
512 					  PT, PR, PG, PO, Gb);
513 	return ret;
514 }
515 
mlx90632_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int * val,int * val2,long mask)516 static int mlx90632_read_raw(struct iio_dev *indio_dev,
517 			     struct iio_chan_spec const *channel, int *val,
518 			     int *val2, long mask)
519 {
520 	struct mlx90632_data *data = iio_priv(indio_dev);
521 	int ret;
522 
523 	switch (mask) {
524 	case IIO_CHAN_INFO_PROCESSED:
525 		switch (channel->channel2) {
526 		case IIO_MOD_TEMP_AMBIENT:
527 			ret = mlx90632_calc_ambient_dsp105(data, val);
528 			if (ret < 0)
529 				return ret;
530 			return IIO_VAL_INT;
531 		case IIO_MOD_TEMP_OBJECT:
532 			ret = mlx90632_calc_object_dsp105(data, val);
533 			if (ret < 0)
534 				return ret;
535 			return IIO_VAL_INT;
536 		default:
537 			return -EINVAL;
538 		}
539 	case IIO_CHAN_INFO_CALIBEMISSIVITY:
540 		if (data->emissivity == 1000) {
541 			*val = 1;
542 			*val2 = 0;
543 		} else {
544 			*val = 0;
545 			*val2 = data->emissivity * 1000;
546 		}
547 		return IIO_VAL_INT_PLUS_MICRO;
548 
549 	default:
550 		return -EINVAL;
551 	}
552 }
553 
mlx90632_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int val,int val2,long mask)554 static int mlx90632_write_raw(struct iio_dev *indio_dev,
555 			      struct iio_chan_spec const *channel, int val,
556 			      int val2, long mask)
557 {
558 	struct mlx90632_data *data = iio_priv(indio_dev);
559 
560 	switch (mask) {
561 	case IIO_CHAN_INFO_CALIBEMISSIVITY:
562 		/* Confirm we are within 0 and 1.0 */
563 		if (val < 0 || val2 < 0 || val > 1 ||
564 		    (val == 1 && val2 != 0))
565 			return -EINVAL;
566 		data->emissivity = val * 1000 + val2 / 1000;
567 		return 0;
568 	default:
569 		return -EINVAL;
570 	}
571 }
572 
573 static const struct iio_chan_spec mlx90632_channels[] = {
574 	{
575 		.type = IIO_TEMP,
576 		.modified = 1,
577 		.channel2 = IIO_MOD_TEMP_AMBIENT,
578 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
579 	},
580 	{
581 		.type = IIO_TEMP,
582 		.modified = 1,
583 		.channel2 = IIO_MOD_TEMP_OBJECT,
584 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
585 			BIT(IIO_CHAN_INFO_CALIBEMISSIVITY),
586 	},
587 };
588 
589 static const struct iio_info mlx90632_info = {
590 	.read_raw = mlx90632_read_raw,
591 	.write_raw = mlx90632_write_raw,
592 };
593 
mlx90632_sleep(struct mlx90632_data * data)594 static int mlx90632_sleep(struct mlx90632_data *data)
595 {
596 	regcache_mark_dirty(data->regmap);
597 
598 	dev_dbg(&data->client->dev, "Requesting sleep");
599 	return mlx90632_pwr_set_sleep_step(data->regmap);
600 }
601 
mlx90632_wakeup(struct mlx90632_data * data)602 static int mlx90632_wakeup(struct mlx90632_data *data)
603 {
604 	int ret;
605 
606 	ret = regcache_sync(data->regmap);
607 	if (ret < 0) {
608 		dev_err(&data->client->dev,
609 			"Failed to sync regmap registers: %d\n", ret);
610 		return ret;
611 	}
612 
613 	dev_dbg(&data->client->dev, "Requesting wake-up\n");
614 	return mlx90632_pwr_continuous(data->regmap);
615 }
616 
mlx90632_probe(struct i2c_client * client,const struct i2c_device_id * id)617 static int mlx90632_probe(struct i2c_client *client,
618 			  const struct i2c_device_id *id)
619 {
620 	struct iio_dev *indio_dev;
621 	struct mlx90632_data *mlx90632;
622 	struct regmap *regmap;
623 	int ret;
624 	unsigned int read;
625 
626 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mlx90632));
627 	if (!indio_dev) {
628 		dev_err(&client->dev, "Failed to allocate device\n");
629 		return -ENOMEM;
630 	}
631 
632 	regmap = devm_regmap_init_i2c(client, &mlx90632_regmap);
633 	if (IS_ERR(regmap)) {
634 		ret = PTR_ERR(regmap);
635 		dev_err(&client->dev, "Failed to allocate regmap: %d\n", ret);
636 		return ret;
637 	}
638 
639 	mlx90632 = iio_priv(indio_dev);
640 	i2c_set_clientdata(client, indio_dev);
641 	mlx90632->client = client;
642 	mlx90632->regmap = regmap;
643 
644 	mutex_init(&mlx90632->lock);
645 	indio_dev->dev.parent = &client->dev;
646 	indio_dev->name = id->name;
647 	indio_dev->modes = INDIO_DIRECT_MODE;
648 	indio_dev->info = &mlx90632_info;
649 	indio_dev->channels = mlx90632_channels;
650 	indio_dev->num_channels = ARRAY_SIZE(mlx90632_channels);
651 
652 	ret = mlx90632_wakeup(mlx90632);
653 	if (ret < 0) {
654 		dev_err(&client->dev, "Wakeup failed: %d\n", ret);
655 		return ret;
656 	}
657 
658 	ret = regmap_read(mlx90632->regmap, MLX90632_EE_VERSION, &read);
659 	if (ret < 0) {
660 		dev_err(&client->dev, "read of version failed: %d\n", ret);
661 		return ret;
662 	}
663 	if (read == MLX90632_ID_MEDICAL) {
664 		dev_dbg(&client->dev,
665 			"Detected Medical EEPROM calibration %x\n", read);
666 	} else if (read == MLX90632_ID_CONSUMER) {
667 		dev_dbg(&client->dev,
668 			"Detected Consumer EEPROM calibration %x\n", read);
669 	} else {
670 		dev_err(&client->dev,
671 			"EEPROM version mismatch %x (expected %x or %x)\n",
672 			read, MLX90632_ID_CONSUMER, MLX90632_ID_MEDICAL);
673 		return -EPROTONOSUPPORT;
674 	}
675 
676 	mlx90632->emissivity = 1000;
677 
678 	pm_runtime_disable(&client->dev);
679 	ret = pm_runtime_set_active(&client->dev);
680 	if (ret < 0) {
681 		mlx90632_sleep(mlx90632);
682 		return ret;
683 	}
684 	pm_runtime_enable(&client->dev);
685 	pm_runtime_set_autosuspend_delay(&client->dev, MLX90632_SLEEP_DELAY_MS);
686 	pm_runtime_use_autosuspend(&client->dev);
687 
688 	return iio_device_register(indio_dev);
689 }
690 
mlx90632_remove(struct i2c_client * client)691 static int mlx90632_remove(struct i2c_client *client)
692 {
693 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
694 	struct mlx90632_data *data = iio_priv(indio_dev);
695 
696 	iio_device_unregister(indio_dev);
697 
698 	pm_runtime_disable(&client->dev);
699 	pm_runtime_set_suspended(&client->dev);
700 	pm_runtime_put_noidle(&client->dev);
701 
702 	mlx90632_sleep(data);
703 
704 	return 0;
705 }
706 
707 static const struct i2c_device_id mlx90632_id[] = {
708 	{ "mlx90632", 0 },
709 	{ }
710 };
711 MODULE_DEVICE_TABLE(i2c, mlx90632_id);
712 
713 static const struct of_device_id mlx90632_of_match[] = {
714 	{ .compatible = "melexis,mlx90632" },
715 	{ }
716 };
717 MODULE_DEVICE_TABLE(of, mlx90632_of_match);
718 
mlx90632_pm_suspend(struct device * dev)719 static int __maybe_unused mlx90632_pm_suspend(struct device *dev)
720 {
721 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
722 	struct mlx90632_data *data = iio_priv(indio_dev);
723 
724 	return mlx90632_sleep(data);
725 }
726 
mlx90632_pm_resume(struct device * dev)727 static int __maybe_unused mlx90632_pm_resume(struct device *dev)
728 {
729 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
730 	struct mlx90632_data *data = iio_priv(indio_dev);
731 
732 	return mlx90632_wakeup(data);
733 }
734 
735 static UNIVERSAL_DEV_PM_OPS(mlx90632_pm_ops, mlx90632_pm_suspend,
736 			    mlx90632_pm_resume, NULL);
737 
738 static struct i2c_driver mlx90632_driver = {
739 	.driver = {
740 		.name	= "mlx90632",
741 		.of_match_table = mlx90632_of_match,
742 		.pm	= &mlx90632_pm_ops,
743 	},
744 	.probe = mlx90632_probe,
745 	.remove = mlx90632_remove,
746 	.id_table = mlx90632_id,
747 };
748 module_i2c_driver(mlx90632_driver);
749 
750 MODULE_AUTHOR("Crt Mori <cmo@melexis.com>");
751 MODULE_DESCRIPTION("Melexis MLX90632 contactless Infra Red temperature sensor driver");
752 MODULE_LICENSE("GPL v2");
753