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