xref: /linux/drivers/hwmon/lm75.c (revision 582b6e87dcd77ed28b13fd489d2738ffa19b11f3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
4  *	 monitoring
5  * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
6  */
7 
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/slab.h>
12 #include <linux/jiffies.h>
13 #include <linux/i2c.h>
14 #include <linux/i3c/device.h>
15 #include <linux/hwmon.h>
16 #include <linux/err.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/property.h>
19 #include <linux/regmap.h>
20 #include <linux/util_macros.h>
21 #include <linux/regulator/consumer.h>
22 #include "lm75.h"
23 
24 /*
25  * This driver handles the LM75 and compatible digital temperature sensors.
26  */
27 
28 enum lm75_type {		/* keep sorted in alphabetical order */
29 	adt75,
30 	as6200,
31 	at30ts74,
32 	ds1775,
33 	ds75,
34 	ds7505,
35 	g751,
36 	lm75,
37 	lm75a,
38 	lm75b,
39 	max6625,
40 	max6626,
41 	max31725,
42 	mcp980x,
43 	p3t1750,
44 	p3t1755,
45 	pct2075,
46 	stds75,
47 	stlm75,
48 	tcn75,
49 	tmp100,
50 	tmp101,
51 	tmp105,
52 	tmp112,
53 	tmp175,
54 	tmp275,
55 	tmp75,
56 	tmp75b,
57 	tmp75c,
58 	tmp1075,
59 };
60 
61 /**
62  * struct lm75_params - lm75 configuration parameters.
63  * @config_reg_16bits:	Configure register size is 2 bytes.
64  * @set_mask:		Bits to set in configuration register when configuring
65  *			the chip.
66  * @clr_mask:		Bits to clear in configuration register when configuring
67  *			the chip.
68  * @default_resolution:	Default number of bits to represent the temperature
69  *			value.
70  * @resolution_limits:	Limit register resolution. Optional. Should be set if
71  *			the resolution of limit registers does not match the
72  *			resolution of the temperature register.
73  * @resolutions:	List of resolutions associated with sample times.
74  *			Optional. Should be set if num_sample_times is larger
75  *			than 1, and if the resolution changes with sample times.
76  *			If set, number of entries must match num_sample_times.
77  * @default_sample_time:Sample time to be set by default.
78  * @num_sample_times:	Number of possible sample times to be set. Optional.
79  *			Should be set if the number of sample times is larger
80  *			than one.
81  * @sample_times:	All the possible sample times to be set. Mandatory if
82  *			num_sample_times is larger than 1. If set, number of
83  *			entries must match num_sample_times.
84  * @alarm:		Alarm bit is supported.
85  */
86 
87 struct lm75_params {
88 	bool			config_reg_16bits;
89 	u16			set_mask;
90 	u16			clr_mask;
91 	u8			default_resolution;
92 	u8			resolution_limits;
93 	const u8		*resolutions;
94 	unsigned int		default_sample_time;
95 	u8			num_sample_times;
96 	const unsigned int	*sample_times;
97 	bool			alarm;
98 };
99 
100 /* Addresses scanned */
101 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
102 					0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
103 
104 /* The LM75 registers */
105 #define LM75_REG_TEMP		0x00
106 #define LM75_REG_CONF		0x01
107 #define LM75_REG_HYST		0x02
108 #define LM75_REG_MAX		0x03
109 #define PCT2075_REG_IDLE	0x04
110 
111 struct lm75_data {
112 	const char *label;
113 	struct regmap			*regmap;
114 	u16				orig_conf;
115 	u8				resolution;	/* In bits, 9 to 16 */
116 	unsigned int			sample_time;	/* In ms */
117 	enum lm75_type			kind;
118 	const struct lm75_params	*params;
119 	u8				reg_buf[1];
120 	u8				val_buf[3];
121 };
122 
123 /*-----------------------------------------------------------------------*/
124 
125 static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 };
126 
127 #define LM75_ALERT_POLARITY_HIGH_8_BIT	(BIT(2))
128 #define LM75_ALERT_POLARITY_HIGH_16_BIT	(BIT(2) << 8)
129 #define LM75_SAMPLE_CLEAR_MASK		(3 << 5)
130 
131 /* The structure below stores the configuration values of the supported devices.
132  * In case of being supported multiple configurations, the default one must
133  * always be the first element of the array
134  */
135 static const struct lm75_params device_params[] = {
136 	[adt75] = {
137 		.clr_mask = 1 << 5,	/* not one-shot mode */
138 		.default_resolution = 12,
139 		.default_sample_time = MSEC_PER_SEC / 10,
140 	},
141 	[as6200] = {
142 		.config_reg_16bits = true,
143 		.set_mask = 0xC010,	/* 8 sample/s, 4 CF */
144 		.default_resolution = 12,
145 		.default_sample_time = 125,
146 		.num_sample_times = 4,
147 		.sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
148 		.alarm = true,
149 	},
150 	[at30ts74] = {
151 		.set_mask = 3 << 5,	/* 12-bit mode*/
152 		.default_resolution = 12,
153 		.default_sample_time = 200,
154 		.num_sample_times = 4,
155 		.sample_times = (unsigned int []){ 25, 50, 100, 200 },
156 		.resolutions = (u8 []) {9, 10, 11, 12 },
157 	},
158 	[ds1775] = {
159 		.clr_mask = 3 << 5,
160 		.set_mask = 2 << 5,	/* 11-bit mode */
161 		.default_resolution = 11,
162 		.default_sample_time = 500,
163 		.num_sample_times = 4,
164 		.sample_times = (unsigned int []){ 125, 250, 500, 1000 },
165 		.resolutions = (u8 []) {9, 10, 11, 12 },
166 	},
167 	[ds75] = {
168 		.clr_mask = 3 << 5,
169 		.set_mask = 2 << 5,	/* 11-bit mode */
170 		.default_resolution = 11,
171 		.default_sample_time = 600,
172 		.num_sample_times = 4,
173 		.sample_times = (unsigned int []){ 150, 300, 600, 1200 },
174 		.resolutions = (u8 []) {9, 10, 11, 12 },
175 	},
176 	[stds75] = {
177 		.clr_mask = 3 << 5,
178 		.set_mask = 2 << 5,	/* 11-bit mode */
179 		.default_resolution = 11,
180 		.default_sample_time = 600,
181 		.num_sample_times = 4,
182 		.sample_times = (unsigned int []){ 150, 300, 600, 1200 },
183 		.resolutions = (u8 []) {9, 10, 11, 12 },
184 	},
185 	[stlm75] = {
186 		.default_resolution = 9,
187 		.default_sample_time = MSEC_PER_SEC / 6,
188 	},
189 	[ds7505] = {
190 		.set_mask = 3 << 5,	/* 12-bit mode*/
191 		.default_resolution = 12,
192 		.default_sample_time = 200,
193 		.num_sample_times = 4,
194 		.sample_times = (unsigned int []){ 25, 50, 100, 200 },
195 		.resolutions = (u8 []) {9, 10, 11, 12 },
196 	},
197 	[g751] = {
198 		.default_resolution = 9,
199 		.default_sample_time = MSEC_PER_SEC / 10,
200 	},
201 	[lm75] = {
202 		.default_resolution = 9,
203 		.default_sample_time = MSEC_PER_SEC / 10,
204 	},
205 	[lm75a] = {
206 		.default_resolution = 9,
207 		.default_sample_time = MSEC_PER_SEC / 10,
208 	},
209 	[lm75b] = {
210 		.default_resolution = 11,
211 		.default_sample_time = MSEC_PER_SEC / 10,
212 	},
213 	[max6625] = {
214 		.default_resolution = 9,
215 		.default_sample_time = MSEC_PER_SEC / 7,
216 	},
217 	[max6626] = {
218 		.default_resolution = 12,
219 		.default_sample_time = MSEC_PER_SEC / 7,
220 		.resolution_limits = 9,
221 	},
222 	[max31725] = {
223 		.default_resolution = 16,
224 		.default_sample_time = MSEC_PER_SEC / 20,
225 	},
226 	[tcn75] = {
227 		.default_resolution = 9,
228 		.default_sample_time = MSEC_PER_SEC / 18,
229 	},
230 	[p3t1750] = {
231 		.clr_mask = 1 << 1 | 1 << 7,	/* disable SMBAlert and one-shot */
232 		.default_resolution = 12,
233 		.default_sample_time = 55,
234 		.num_sample_times = 4,
235 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
236 	},
237 	[p3t1755] = {
238 		.clr_mask = 1 << 1 | 1 << 7,	/* disable SMBAlert and one-shot */
239 		.default_resolution = 12,
240 		.default_sample_time = 55,
241 		.num_sample_times = 4,
242 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
243 	},
244 	[pct2075] = {
245 		.default_resolution = 11,
246 		.default_sample_time = MSEC_PER_SEC / 10,
247 		.num_sample_times = 31,
248 		.sample_times = (unsigned int []){ 100, 200, 300, 400, 500, 600,
249 		700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700,
250 		1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700,
251 		2800, 2900, 3000, 3100 },
252 	},
253 	[mcp980x] = {
254 		.set_mask = 3 << 5,	/* 12-bit mode */
255 		.clr_mask = 1 << 7,	/* not one-shot mode */
256 		.default_resolution = 12,
257 		.resolution_limits = 9,
258 		.default_sample_time = 240,
259 		.num_sample_times = 4,
260 		.sample_times = (unsigned int []){ 30, 60, 120, 240 },
261 		.resolutions = (u8 []) {9, 10, 11, 12 },
262 	},
263 	[tmp100] = {
264 		.set_mask = 3 << 5,	/* 12-bit mode */
265 		.clr_mask = 1 << 7,	/* not one-shot mode */
266 		.default_resolution = 12,
267 		.default_sample_time = 320,
268 		.num_sample_times = 4,
269 		.sample_times = (unsigned int []){ 40, 80, 160, 320 },
270 		.resolutions = (u8 []) {9, 10, 11, 12 },
271 	},
272 	[tmp101] = {
273 		.set_mask = 3 << 5,	/* 12-bit mode */
274 		.clr_mask = 1 << 7,	/* not one-shot mode */
275 		.default_resolution = 12,
276 		.default_sample_time = 320,
277 		.num_sample_times = 4,
278 		.sample_times = (unsigned int []){ 40, 80, 160, 320 },
279 		.resolutions = (u8 []) {9, 10, 11, 12 },
280 	},
281 	[tmp105] = {
282 		.set_mask = 3 << 5,	/* 12-bit mode */
283 		.clr_mask = 1 << 7,	/* not one-shot mode*/
284 		.default_resolution = 12,
285 		.default_sample_time = 220,
286 		.num_sample_times = 4,
287 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
288 		.resolutions = (u8 []) {9, 10, 11, 12 },
289 	},
290 	[tmp112] = {
291 		.config_reg_16bits = true,
292 		.set_mask = 0xC060,	/* 12-bit mode, 8 samples / second */
293 		.clr_mask = 1 << 7,	/* no one-shot mode*/
294 		.default_resolution = 12,
295 		.default_sample_time = 125,
296 		.num_sample_times = 4,
297 		.sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
298 		.alarm = true,
299 	},
300 	[tmp175] = {
301 		.set_mask = 3 << 5,	/* 12-bit mode */
302 		.clr_mask = 1 << 7,	/* not one-shot mode*/
303 		.default_resolution = 12,
304 		.default_sample_time = 220,
305 		.num_sample_times = 4,
306 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
307 		.resolutions = (u8 []) {9, 10, 11, 12 },
308 	},
309 	[tmp275] = {
310 		.set_mask = 3 << 5,	/* 12-bit mode */
311 		.clr_mask = 1 << 7,	/* not one-shot mode*/
312 		.default_resolution = 12,
313 		.default_sample_time = 220,
314 		.num_sample_times = 4,
315 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
316 		.resolutions = (u8 []) {9, 10, 11, 12 },
317 	},
318 	[tmp75] = {
319 		.set_mask = 3 << 5,	/* 12-bit mode */
320 		.clr_mask = 1 << 7,	/* not one-shot mode*/
321 		.default_resolution = 12,
322 		.default_sample_time = 220,
323 		.num_sample_times = 4,
324 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
325 		.resolutions = (u8 []) {9, 10, 11, 12 },
326 	},
327 	[tmp75b] = { /* not one-shot mode, Conversion rate 37Hz */
328 		.clr_mask = 1 << 7 | 3 << 5,
329 		.default_resolution = 12,
330 		.default_sample_time = MSEC_PER_SEC / 37,
331 		.sample_times = (unsigned int []){ MSEC_PER_SEC / 37,
332 			MSEC_PER_SEC / 18,
333 			MSEC_PER_SEC / 9, MSEC_PER_SEC / 4 },
334 		.num_sample_times = 4,
335 	},
336 	[tmp75c] = {
337 		.clr_mask = 1 << 5,	/*not one-shot mode*/
338 		.default_resolution = 12,
339 		.default_sample_time = MSEC_PER_SEC / 12,
340 	},
341 	[tmp1075] = { /* not one-shot mode, 27.5 ms sample rate */
342 		.clr_mask = 1 << 5 | 1 << 6 | 1 << 7,
343 		.default_resolution = 12,
344 		.default_sample_time = 28,
345 		.num_sample_times = 4,
346 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
347 	}
348 };
349 
350 static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
351 {
352 	return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
353 }
354 
355 static inline int lm75_write_config(struct lm75_data *data, u16 set_mask,
356 				    u16 clr_mask)
357 {
358 	return regmap_update_bits(data->regmap, LM75_REG_CONF,
359 				  clr_mask | set_mask | LM75_SHUTDOWN, set_mask);
360 }
361 
362 static irqreturn_t lm75_alarm_handler(int irq, void *private)
363 {
364 	struct device *hwmon_dev = private;
365 
366 	hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_alarm, 0);
367 	return IRQ_HANDLED;
368 }
369 
370 static int lm75_read_string(struct device *dev, enum hwmon_sensor_types type,
371 			    u32 attr, int channel, const char **str)
372 {
373 	struct lm75_data *data = dev_get_drvdata(dev);
374 
375 	*str = data->label;
376 
377 	return 0;
378 }
379 
380 static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
381 		     u32 attr, int channel, long *val)
382 {
383 	struct lm75_data *data = dev_get_drvdata(dev);
384 	unsigned int regval;
385 	int err, reg;
386 
387 	switch (type) {
388 	case hwmon_chip:
389 		switch (attr) {
390 		case hwmon_chip_update_interval:
391 			*val = data->sample_time;
392 			break;
393 		default:
394 			return -EINVAL;
395 		}
396 		break;
397 	case hwmon_temp:
398 		switch (attr) {
399 		case hwmon_temp_input:
400 			reg = LM75_REG_TEMP;
401 			break;
402 		case hwmon_temp_max:
403 			reg = LM75_REG_MAX;
404 			break;
405 		case hwmon_temp_max_hyst:
406 			reg = LM75_REG_HYST;
407 			break;
408 		case hwmon_temp_alarm:
409 			reg = LM75_REG_CONF;
410 			break;
411 		default:
412 			return -EINVAL;
413 		}
414 		err = regmap_read(data->regmap, reg, &regval);
415 		if (err < 0)
416 			return err;
417 
418 		if (attr == hwmon_temp_alarm) {
419 			switch (data->kind) {
420 			case as6200:
421 			case tmp112:
422 				*val = !!(regval & BIT(13)) == !!(regval & BIT(2));
423 				break;
424 			default:
425 				return -EINVAL;
426 			}
427 		} else {
428 			*val = lm75_reg_to_mc(regval, data->resolution);
429 		}
430 		break;
431 	default:
432 		return -EINVAL;
433 	}
434 	return 0;
435 }
436 
437 static int lm75_write_temp(struct device *dev, u32 attr, long temp)
438 {
439 	struct lm75_data *data = dev_get_drvdata(dev);
440 	u8 resolution;
441 	int reg;
442 
443 	switch (attr) {
444 	case hwmon_temp_max:
445 		reg = LM75_REG_MAX;
446 		break;
447 	case hwmon_temp_max_hyst:
448 		reg = LM75_REG_HYST;
449 		break;
450 	default:
451 		return -EINVAL;
452 	}
453 
454 	/*
455 	 * Resolution of limit registers is assumed to be the same as the
456 	 * temperature input register resolution unless given explicitly.
457 	 */
458 	if (data->params->resolution_limits)
459 		resolution = data->params->resolution_limits;
460 	else
461 		resolution = data->resolution;
462 
463 	temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
464 	temp = DIV_ROUND_CLOSEST(temp  << (resolution - 8),
465 				 1000) << (16 - resolution);
466 
467 	return regmap_write(data->regmap, reg, (u16)temp);
468 }
469 
470 static int lm75_update_interval(struct device *dev, long val)
471 {
472 	struct lm75_data *data = dev_get_drvdata(dev);
473 	u8 index;
474 	s32 err;
475 
476 	index = find_closest(val, data->params->sample_times,
477 			     (int)data->params->num_sample_times);
478 
479 	switch (data->kind) {
480 	default:
481 		err = lm75_write_config(data, lm75_sample_set_masks[index],
482 					LM75_SAMPLE_CLEAR_MASK);
483 		if (err)
484 			return err;
485 
486 		data->sample_time = data->params->sample_times[index];
487 		if (data->params->resolutions)
488 			data->resolution = data->params->resolutions[index];
489 		break;
490 	case tmp112:
491 	case as6200:
492 		err = regmap_update_bits(data->regmap, LM75_REG_CONF,
493 					 0xc000, (3 - index) << 14);
494 		if (err < 0)
495 			return err;
496 		data->sample_time = data->params->sample_times[index];
497 		break;
498 	case pct2075:
499 		err = regmap_write(data->regmap, PCT2075_REG_IDLE, index + 1);
500 		if (err)
501 			return err;
502 		data->sample_time = data->params->sample_times[index];
503 		break;
504 	}
505 	return 0;
506 }
507 
508 static int lm75_write_chip(struct device *dev, u32 attr, long val)
509 {
510 	switch (attr) {
511 	case hwmon_chip_update_interval:
512 		return lm75_update_interval(dev, val);
513 	default:
514 		return -EINVAL;
515 	}
516 	return 0;
517 }
518 
519 static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
520 		      u32 attr, int channel, long val)
521 {
522 	switch (type) {
523 	case hwmon_chip:
524 		return lm75_write_chip(dev, attr, val);
525 	case hwmon_temp:
526 		return lm75_write_temp(dev, attr, val);
527 	default:
528 		return -EINVAL;
529 	}
530 	return 0;
531 }
532 
533 static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
534 			       u32 attr, int channel)
535 {
536 	const struct lm75_data *config_data = data;
537 
538 	switch (type) {
539 	case hwmon_chip:
540 		switch (attr) {
541 		case hwmon_chip_update_interval:
542 			if (config_data->params->num_sample_times > 1)
543 				return 0644;
544 			return 0444;
545 		default:
546 			break;
547 		}
548 		break;
549 	case hwmon_temp:
550 		switch (attr) {
551 		case hwmon_temp_input:
552 			return 0444;
553 		case hwmon_temp_label:
554 		/* Hide label node if label is not provided */
555 			return config_data->label ? 0444 : 0;
556 		case hwmon_temp_max:
557 		case hwmon_temp_max_hyst:
558 			return 0644;
559 		case hwmon_temp_alarm:
560 			if (config_data->params->alarm)
561 				return 0444;
562 			break;
563 		default:
564 			break;
565 		}
566 		break;
567 	default:
568 		break;
569 	}
570 	return 0;
571 }
572 
573 static const struct hwmon_channel_info * const lm75_info[] = {
574 	HWMON_CHANNEL_INFO(chip,
575 			   HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
576 	HWMON_CHANNEL_INFO(temp,
577 			   HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_MAX | HWMON_T_MAX_HYST |
578 			   HWMON_T_ALARM),
579 	NULL
580 };
581 
582 static const struct hwmon_ops lm75_hwmon_ops = {
583 	.is_visible = lm75_is_visible,
584 	.read_string = lm75_read_string,
585 	.read = lm75_read,
586 	.write = lm75_write,
587 };
588 
589 static const struct hwmon_chip_info lm75_chip_info = {
590 	.ops = &lm75_hwmon_ops,
591 	.info = lm75_info,
592 };
593 
594 static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
595 {
596 	return reg != LM75_REG_TEMP;
597 }
598 
599 static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
600 {
601 	return reg == LM75_REG_TEMP || reg == LM75_REG_CONF;
602 }
603 
604 static int lm75_i2c_reg_read(void *context, unsigned int reg, unsigned int *val)
605 {
606 	struct i2c_client *client = context;
607 	struct lm75_data *data = i2c_get_clientdata(client);
608 	int ret;
609 
610 	if (reg == LM75_REG_CONF) {
611 		if (!data->params->config_reg_16bits)
612 			ret = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
613 		else
614 			ret = i2c_smbus_read_word_data(client, LM75_REG_CONF);
615 	} else {
616 		ret = i2c_smbus_read_word_swapped(client, reg);
617 	}
618 	if (ret < 0)
619 		return ret;
620 	*val = ret;
621 	return 0;
622 }
623 
624 static int lm75_i2c_reg_write(void *context, unsigned int reg, unsigned int val)
625 {
626 	struct i2c_client *client = context;
627 	struct lm75_data *data = i2c_get_clientdata(client);
628 
629 	if (reg == PCT2075_REG_IDLE ||
630 	    (reg == LM75_REG_CONF && !data->params->config_reg_16bits))
631 		return i2c_smbus_write_byte_data(client, reg, val);
632 	else if (reg == LM75_REG_CONF)
633 		return i2c_smbus_write_word_data(client, reg, val);
634 	return i2c_smbus_write_word_swapped(client, reg, val);
635 }
636 
637 static const struct regmap_bus lm75_i2c_regmap_bus = {
638 	.reg_read = lm75_i2c_reg_read,
639 	.reg_write = lm75_i2c_reg_write,
640 };
641 
642 static int lm75_i3c_reg_read(void *context, unsigned int reg, unsigned int *val)
643 {
644 	struct i3c_device *i3cdev = context;
645 	struct lm75_data *data = i3cdev_get_drvdata(i3cdev);
646 	struct i3c_xfer xfers[] = {
647 		{
648 			.rnw = false,
649 			.len = 1,
650 			.data.out = data->reg_buf,
651 		},
652 		{
653 			.rnw = true,
654 			.len = 2,
655 			.data.in = data->val_buf,
656 		},
657 	};
658 	int ret;
659 
660 	data->reg_buf[0] = reg;
661 
662 	if (reg == LM75_REG_CONF && !data->params->config_reg_16bits)
663 		xfers[1].len--;
664 
665 	ret = i3c_device_do_xfers(i3cdev, xfers, 2, I3C_SDR);
666 	if (ret < 0)
667 		return ret;
668 
669 	if (reg == LM75_REG_CONF && !data->params->config_reg_16bits)
670 		*val = data->val_buf[0];
671 	else if (reg == LM75_REG_CONF)
672 		*val = data->val_buf[0] | (data->val_buf[1] << 8);
673 	else
674 		*val = data->val_buf[1] | (data->val_buf[0] << 8);
675 
676 	return 0;
677 }
678 
679 static int lm75_i3c_reg_write(void *context, unsigned int reg, unsigned int val)
680 {
681 	struct i3c_device *i3cdev = context;
682 	struct lm75_data *data = i3cdev_get_drvdata(i3cdev);
683 	struct i3c_xfer xfers[] = {
684 		{
685 			.rnw = false,
686 			.len = 3,
687 			.data.out = data->val_buf,
688 		},
689 	};
690 
691 	data->val_buf[0] = reg;
692 
693 	if (reg == PCT2075_REG_IDLE ||
694 	    (reg == LM75_REG_CONF && !data->params->config_reg_16bits)) {
695 		xfers[0].len--;
696 		data->val_buf[1] = val & 0xff;
697 	} else if (reg == LM75_REG_CONF) {
698 		data->val_buf[1] = val & 0xff;
699 		data->val_buf[2] = (val >> 8) & 0xff;
700 	} else {
701 		data->val_buf[1] = (val >> 8) & 0xff;
702 		data->val_buf[2] = val & 0xff;
703 	}
704 
705 	return i3c_device_do_xfers(i3cdev, xfers, 1, I3C_SDR);
706 }
707 
708 static const struct regmap_bus lm75_i3c_regmap_bus = {
709 	.reg_read = lm75_i3c_reg_read,
710 	.reg_write = lm75_i3c_reg_write,
711 };
712 
713 static const struct regmap_config lm75_regmap_config = {
714 	.reg_bits = 8,
715 	.val_bits = 16,
716 	.max_register = PCT2075_REG_IDLE,
717 	.writeable_reg = lm75_is_writeable_reg,
718 	.volatile_reg = lm75_is_volatile_reg,
719 	.val_format_endian = REGMAP_ENDIAN_BIG,
720 	.cache_type = REGCACHE_MAPLE,
721 	.use_single_read = true,
722 	.use_single_write = true,
723 };
724 
725 static void lm75_remove(void *data)
726 {
727 	struct lm75_data *lm75 = data;
728 
729 	regmap_write(lm75->regmap, LM75_REG_CONF, lm75->orig_conf);
730 }
731 
732 static int lm75_generic_probe(struct device *dev, const char *name,
733 			      enum lm75_type kind, int irq, struct regmap *regmap)
734 {
735 	u16 clr_mask, pol_mask, set_mask;
736 	struct device *hwmon_dev;
737 	struct lm75_data *data;
738 	int status, err;
739 
740 	data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
741 	if (!data)
742 		return -ENOMEM;
743 
744 	/* needed by custom regmap callbacks */
745 	dev_set_drvdata(dev, data);
746 
747 	/* Save the connected input label if available */
748 	device_property_read_string(dev, "label", &data->label);
749 
750 	data->kind = kind;
751 	data->regmap = regmap;
752 
753 	err = devm_regulator_get_enable(dev, "vs");
754 	if (err)
755 		return err;
756 
757 	/* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
758 	 * Then tweak to be more precise when appropriate.
759 	 */
760 
761 	data->params = &device_params[data->kind];
762 
763 	/* Save default sample time and resolution*/
764 	data->sample_time = data->params->default_sample_time;
765 	data->resolution = data->params->default_resolution;
766 
767 	/* Cache original configuration */
768 	err = regmap_read(data->regmap, LM75_REG_CONF, &status);
769 	if (err)
770 		return err;
771 	data->orig_conf = status;
772 
773 	/* Enforce polarity active-low (default) or active-high (devicetree) */
774 	if (!data->params->config_reg_16bits)
775 		pol_mask = LM75_ALERT_POLARITY_HIGH_8_BIT;
776 	else
777 		pol_mask = LM75_ALERT_POLARITY_HIGH_16_BIT;
778 
779 	clr_mask = data->params->clr_mask | pol_mask;
780 	set_mask = data->params->set_mask & ~pol_mask;
781 	if (device_property_read_bool(dev, "ti,alert-polarity-active-high"))
782 		set_mask |= pol_mask;
783 
784 	err = lm75_write_config(data, set_mask, clr_mask);
785 	if (err)
786 		return err;
787 
788 	err = devm_add_action_or_reset(dev, lm75_remove, data);
789 	if (err)
790 		return err;
791 
792 	hwmon_dev = devm_hwmon_device_register_with_info(dev, name, data,
793 							 &lm75_chip_info, NULL);
794 	if (IS_ERR(hwmon_dev))
795 		return PTR_ERR(hwmon_dev);
796 
797 	if (irq) {
798 		if (data->params->alarm) {
799 			err = devm_request_threaded_irq(dev,
800 							irq,
801 							NULL,
802 							&lm75_alarm_handler,
803 							IRQF_ONESHOT,
804 							name,
805 							hwmon_dev);
806 			if (err)
807 				return err;
808 		} else {
809 			 /* alarm is only supported for chips with alarm bit */
810 			dev_err(dev, "alarm interrupt is not supported\n");
811 		}
812 	}
813 
814 	dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), name);
815 
816 	return 0;
817 }
818 
819 static int lm75_i2c_probe(struct i2c_client *client)
820 {
821 	struct device *dev = &client->dev;
822 	struct regmap *regmap;
823 
824 	if (!i2c_check_functionality(client->adapter,
825 			I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
826 		return -EOPNOTSUPP;
827 
828 	regmap = devm_regmap_init(dev, &lm75_i2c_regmap_bus, client, &lm75_regmap_config);
829 	if (IS_ERR(regmap))
830 		return PTR_ERR(regmap);
831 
832 	return lm75_generic_probe(dev, client->name, (uintptr_t)i2c_get_match_data(client),
833 				  client->irq, regmap);
834 }
835 
836 static const struct i2c_device_id lm75_i2c_ids[] = {
837 	{ .name = "adt75", .driver_data = adt75 },
838 	{ .name = "as6200", .driver_data = as6200 },
839 	{ .name = "at30ts74", .driver_data = at30ts74 },
840 	{ .name = "ds1775", .driver_data = ds1775 },
841 	{ .name = "ds75", .driver_data = ds75 },
842 	{ .name = "ds7505", .driver_data = ds7505 },
843 	{ .name = "g751", .driver_data = g751 },
844 	{ .name = "lm75", .driver_data = lm75 },
845 	{ .name = "lm75a", .driver_data = lm75a },
846 	{ .name = "lm75b", .driver_data = lm75b },
847 	{ .name = "max6625", .driver_data = max6625 },
848 	{ .name = "max6626", .driver_data = max6626 },
849 	{ .name = "max31725", .driver_data = max31725 },
850 	{ .name = "max31726", .driver_data = max31725 },
851 	{ .name = "mcp980x", .driver_data = mcp980x },
852 	{ .name = "p3t1750", .driver_data = p3t1750 },
853 	{ .name = "p3t1755", .driver_data = p3t1755 },
854 	{ .name = "pct2075", .driver_data = pct2075 },
855 	{ .name = "stds75", .driver_data = stds75 },
856 	{ .name = "stlm75", .driver_data = stlm75 },
857 	{ .name = "tcn75", .driver_data = tcn75 },
858 	{ .name = "tmp100", .driver_data = tmp100 },
859 	{ .name = "tmp101", .driver_data = tmp101 },
860 	{ .name = "tmp105", .driver_data = tmp105 },
861 	{ .name = "tmp112", .driver_data = tmp112 },
862 	{ .name = "tmp175", .driver_data = tmp175 },
863 	{ .name = "tmp275", .driver_data = tmp275 },
864 	{ .name = "tmp75", .driver_data = tmp75 },
865 	{ .name = "tmp75b", .driver_data = tmp75b },
866 	{ .name = "tmp75c", .driver_data = tmp75c },
867 	{ .name = "tmp1075", .driver_data = tmp1075 },
868 	{ /* LIST END */ }
869 };
870 MODULE_DEVICE_TABLE(i2c, lm75_i2c_ids);
871 
872 struct lm75_i3c_device {
873 	enum lm75_type type;
874 	const char *name;
875 };
876 
877 static const struct lm75_i3c_device lm75_i3c_p3t1755 = {
878 	.name = "p3t1755",
879 	.type = p3t1755,
880 };
881 
882 static const struct i3c_device_id lm75_i3c_ids[] = {
883 	I3C_DEVICE(0x011b, 0x152a, &lm75_i3c_p3t1755),
884 	{ /* LIST END */ }
885 };
886 MODULE_DEVICE_TABLE(i3c, lm75_i3c_ids);
887 
888 static int lm75_i3c_probe(struct i3c_device *i3cdev)
889 {
890 	struct device *dev = i3cdev_to_dev(i3cdev);
891 	const struct lm75_i3c_device *id_data;
892 	struct regmap *regmap;
893 
894 	regmap = devm_regmap_init(dev, &lm75_i3c_regmap_bus, i3cdev, &lm75_regmap_config);
895 	if (IS_ERR(regmap))
896 		return PTR_ERR(regmap);
897 
898 	id_data = i3c_device_match_id(i3cdev, lm75_i3c_ids)->data;
899 
900 	return lm75_generic_probe(dev, id_data->name, id_data->type, 0, regmap);
901 }
902 
903 static const struct of_device_id lm75_of_match[] = {
904 	{
905 		.compatible = "adi,adt75",
906 		.data = (void *)adt75
907 	},
908 	{
909 		.compatible = "ams,as6200",
910 		.data = (void *)as6200
911 	},
912 	{
913 		.compatible = "atmel,at30ts74",
914 		.data = (void *)at30ts74
915 	},
916 	{
917 		.compatible = "dallas,ds1775",
918 		.data = (void *)ds1775
919 	},
920 	{
921 		.compatible = "dallas,ds75",
922 		.data = (void *)ds75
923 	},
924 	{
925 		.compatible = "dallas,ds7505",
926 		.data = (void *)ds7505
927 	},
928 	{
929 		.compatible = "gmt,g751",
930 		.data = (void *)g751
931 	},
932 	{
933 		.compatible = "national,lm75",
934 		.data = (void *)lm75
935 	},
936 	{
937 		.compatible = "national,lm75a",
938 		.data = (void *)lm75a
939 	},
940 	{
941 		.compatible = "national,lm75b",
942 		.data = (void *)lm75b
943 	},
944 	{
945 		.compatible = "maxim,max6625",
946 		.data = (void *)max6625
947 	},
948 	{
949 		.compatible = "maxim,max6626",
950 		.data = (void *)max6626
951 	},
952 	{
953 		.compatible = "maxim,max31725",
954 		.data = (void *)max31725
955 	},
956 	{
957 		.compatible = "maxim,max31726",
958 		.data = (void *)max31725
959 	},
960 	{
961 		.compatible = "maxim,mcp980x",
962 		.data = (void *)mcp980x
963 	},
964 	{
965 		.compatible = "nxp,p3t1750",
966 		.data = (void *)p3t1750
967 	},
968 	{
969 		.compatible = "nxp,p3t1755",
970 		.data = (void *)p3t1755
971 	},
972 	{
973 		.compatible = "nxp,pct2075",
974 		.data = (void *)pct2075
975 	},
976 	{
977 		.compatible = "st,stds75",
978 		.data = (void *)stds75
979 	},
980 	{
981 		.compatible = "st,stlm75",
982 		.data = (void *)stlm75
983 	},
984 	{
985 		.compatible = "microchip,tcn75",
986 		.data = (void *)tcn75
987 	},
988 	{
989 		.compatible = "ti,tmp100",
990 		.data = (void *)tmp100
991 	},
992 	{
993 		.compatible = "ti,tmp101",
994 		.data = (void *)tmp101
995 	},
996 	{
997 		.compatible = "ti,tmp105",
998 		.data = (void *)tmp105
999 	},
1000 	{
1001 		.compatible = "ti,tmp112",
1002 		.data = (void *)tmp112
1003 	},
1004 	{
1005 		.compatible = "ti,tmp175",
1006 		.data = (void *)tmp175
1007 	},
1008 	{
1009 		.compatible = "ti,tmp275",
1010 		.data = (void *)tmp275
1011 	},
1012 	{
1013 		.compatible = "ti,tmp75",
1014 		.data = (void *)tmp75
1015 	},
1016 	{
1017 		.compatible = "ti,tmp75b",
1018 		.data = (void *)tmp75b
1019 	},
1020 	{
1021 		.compatible = "ti,tmp75c",
1022 		.data = (void *)tmp75c
1023 	},
1024 	{
1025 		.compatible = "ti,tmp1075",
1026 		.data = (void *)tmp1075
1027 	},
1028 	{ },
1029 };
1030 MODULE_DEVICE_TABLE(of, lm75_of_match);
1031 
1032 #define LM75A_ID 0xA1
1033 
1034 /* Return 0 if detection is successful, -ENODEV otherwise */
1035 static int lm75_detect(struct i2c_client *new_client,
1036 		       struct i2c_board_info *info)
1037 {
1038 	struct i2c_adapter *adapter = new_client->adapter;
1039 	int i;
1040 	int conf, hyst, os;
1041 	bool is_lm75a = 0;
1042 
1043 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
1044 				     I2C_FUNC_SMBUS_WORD_DATA))
1045 		return -ENODEV;
1046 
1047 	/*
1048 	 * Now, we do the remaining detection. There is no identification-
1049 	 * dedicated register so we have to rely on several tricks:
1050 	 * unused bits, registers cycling over 8-address boundaries,
1051 	 * addresses 0x04-0x07 returning the last read value.
1052 	 * The cycling+unused addresses combination is not tested,
1053 	 * since it would significantly slow the detection down and would
1054 	 * hardly add any value.
1055 	 *
1056 	 * The National Semiconductor LM75A is different than earlier
1057 	 * LM75s.  It has an ID byte of 0xaX (where X is the chip
1058 	 * revision, with 1 being the only revision in existence) in
1059 	 * register 7, and unused registers return 0xff rather than the
1060 	 * last read value.
1061 	 *
1062 	 * Note that this function only detects the original National
1063 	 * Semiconductor LM75 and the LM75A. Clones from other vendors
1064 	 * aren't detected, on purpose, because they are typically never
1065 	 * found on PC hardware. They are found on embedded designs where
1066 	 * they can be instantiated explicitly so detection is not needed.
1067 	 * The absence of identification registers on all these clones
1068 	 * would make their exhaustive detection very difficult and weak,
1069 	 * and odds are that the driver would bind to unsupported devices.
1070 	 */
1071 
1072 	/* Unused bits */
1073 	conf = i2c_smbus_read_byte_data(new_client, 1);
1074 	if (conf & 0xe0)
1075 		return -ENODEV;
1076 
1077 	/* First check for LM75A */
1078 	if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
1079 		/*
1080 		 * LM75A returns 0xff on unused registers so
1081 		 * just to be sure we check for that too.
1082 		 */
1083 		if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
1084 		 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
1085 		 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
1086 			return -ENODEV;
1087 		is_lm75a = 1;
1088 		hyst = i2c_smbus_read_byte_data(new_client, 2);
1089 		os = i2c_smbus_read_byte_data(new_client, 3);
1090 	} else { /* Traditional style LM75 detection */
1091 		/* Unused addresses */
1092 		hyst = i2c_smbus_read_byte_data(new_client, 2);
1093 		if (i2c_smbus_read_byte_data(new_client, 4) != hyst
1094 		 || i2c_smbus_read_byte_data(new_client, 5) != hyst
1095 		 || i2c_smbus_read_byte_data(new_client, 6) != hyst
1096 		 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
1097 			return -ENODEV;
1098 		os = i2c_smbus_read_byte_data(new_client, 3);
1099 		if (i2c_smbus_read_byte_data(new_client, 4) != os
1100 		 || i2c_smbus_read_byte_data(new_client, 5) != os
1101 		 || i2c_smbus_read_byte_data(new_client, 6) != os
1102 		 || i2c_smbus_read_byte_data(new_client, 7) != os)
1103 			return -ENODEV;
1104 	}
1105 	/*
1106 	 * It is very unlikely that this is a LM75 if both
1107 	 * hysteresis and temperature limit registers are 0.
1108 	 */
1109 	if (hyst == 0 && os == 0)
1110 		return -ENODEV;
1111 
1112 	/* Addresses cycling */
1113 	for (i = 8; i <= 248; i += 40) {
1114 		if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
1115 		 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
1116 		 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
1117 			return -ENODEV;
1118 		if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
1119 				!= LM75A_ID)
1120 			return -ENODEV;
1121 	}
1122 
1123 	strscpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
1124 
1125 	return 0;
1126 }
1127 
1128 #ifdef CONFIG_PM
1129 static int lm75_suspend(struct device *dev)
1130 {
1131 	struct lm75_data *data = dev_get_drvdata(dev);
1132 
1133 	return regmap_update_bits(data->regmap, LM75_REG_CONF, LM75_SHUTDOWN, LM75_SHUTDOWN);
1134 }
1135 
1136 static int lm75_resume(struct device *dev)
1137 {
1138 	struct lm75_data *data = dev_get_drvdata(dev);
1139 
1140 	return regmap_update_bits(data->regmap, LM75_REG_CONF, LM75_SHUTDOWN, 0);
1141 }
1142 
1143 static const struct dev_pm_ops lm75_dev_pm_ops = {
1144 	.suspend	= lm75_suspend,
1145 	.resume		= lm75_resume,
1146 };
1147 #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
1148 #else
1149 #define LM75_DEV_PM_OPS NULL
1150 #endif /* CONFIG_PM */
1151 
1152 static struct i2c_driver lm75_i2c_driver = {
1153 	.class		= I2C_CLASS_HWMON,
1154 	.driver = {
1155 		.name	= "lm75",
1156 		.of_match_table = lm75_of_match,
1157 		.pm	= LM75_DEV_PM_OPS,
1158 	},
1159 	.probe		= lm75_i2c_probe,
1160 	.id_table	= lm75_i2c_ids,
1161 	.detect		= lm75_detect,
1162 	.address_list	= normal_i2c,
1163 };
1164 
1165 static struct i3c_driver lm75_i3c_driver = {
1166 	.driver = {
1167 		.name = "lm75_i3c",
1168 	},
1169 	.probe = lm75_i3c_probe,
1170 	.id_table = lm75_i3c_ids,
1171 };
1172 
1173 module_i3c_i2c_driver(lm75_i3c_driver, &lm75_i2c_driver)
1174 
1175 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
1176 MODULE_DESCRIPTION("LM75 driver");
1177 MODULE_LICENSE("GPL");
1178