xref: /openbmc/linux/drivers/hwmon/lm75.c (revision 35267cea)
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/slab.h>
11 #include <linux/jiffies.h>
12 #include <linux/i2c.h>
13 #include <linux/hwmon.h>
14 #include <linux/hwmon-sysfs.h>
15 #include <linux/err.h>
16 #include <linux/of_device.h>
17 #include <linux/of.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 	ds1775,
30 	ds75,
31 	ds7505,
32 	g751,
33 	lm75,
34 	lm75a,
35 	lm75b,
36 	max6625,
37 	max6626,
38 	max31725,
39 	mcp980x,
40 	pct2075,
41 	stds75,
42 	stlm75,
43 	tcn75,
44 	tmp100,
45 	tmp101,
46 	tmp105,
47 	tmp112,
48 	tmp175,
49 	tmp275,
50 	tmp75,
51 	tmp75b,
52 	tmp75c,
53 	tmp1075,
54 };
55 
56 /**
57  * struct lm75_params - lm75 configuration parameters.
58  * @set_mask:		Bits to set in configuration register when configuring
59  *			the chip.
60  * @clr_mask:		Bits to clear in configuration register when configuring
61  *			the chip.
62  * @default_resolution:	Default number of bits to represent the temperature
63  *			value.
64  * @resolution_limits:	Limit register resolution. Optional. Should be set if
65  *			the resolution of limit registers does not match the
66  *			resolution of the temperature register.
67  * @resolutions:	List of resolutions associated with sample times.
68  *			Optional. Should be set if num_sample_times is larger
69  *			than 1, and if the resolution changes with sample times.
70  *			If set, number of entries must match num_sample_times.
71  * @default_sample_time:Sample time to be set by default.
72  * @num_sample_times:	Number of possible sample times to be set. Optional.
73  *			Should be set if the number of sample times is larger
74  *			than one.
75  * @sample_times:	All the possible sample times to be set. Mandatory if
76  *			num_sample_times is larger than 1. If set, number of
77  *			entries must match num_sample_times.
78  */
79 
80 struct lm75_params {
81 	u8			set_mask;
82 	u8			clr_mask;
83 	u8			default_resolution;
84 	u8			resolution_limits;
85 	const u8		*resolutions;
86 	unsigned int		default_sample_time;
87 	u8			num_sample_times;
88 	const unsigned int	*sample_times;
89 };
90 
91 /* Addresses scanned */
92 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
93 					0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
94 
95 /* The LM75 registers */
96 #define LM75_REG_TEMP		0x00
97 #define LM75_REG_CONF		0x01
98 #define LM75_REG_HYST		0x02
99 #define LM75_REG_MAX		0x03
100 #define PCT2075_REG_IDLE	0x04
101 
102 /* Each client has this additional data */
103 struct lm75_data {
104 	struct i2c_client		*client;
105 	struct regmap			*regmap;
106 	struct regulator		*vs;
107 	u8				orig_conf;
108 	u8				current_conf;
109 	u8				resolution;	/* In bits, 9 to 16 */
110 	unsigned int			sample_time;	/* In ms */
111 	enum lm75_type			kind;
112 	const struct lm75_params	*params;
113 };
114 
115 /*-----------------------------------------------------------------------*/
116 
117 static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 };
118 
119 #define LM75_SAMPLE_CLEAR_MASK	(3 << 5)
120 
121 /* The structure below stores the configuration values of the supported devices.
122  * In case of being supported multiple configurations, the default one must
123  * always be the first element of the array
124  */
125 static const struct lm75_params device_params[] = {
126 	[adt75] = {
127 		.clr_mask = 1 << 5,	/* not one-shot mode */
128 		.default_resolution = 12,
129 		.default_sample_time = MSEC_PER_SEC / 10,
130 	},
131 	[ds1775] = {
132 		.clr_mask = 3 << 5,
133 		.set_mask = 2 << 5,	/* 11-bit mode */
134 		.default_resolution = 11,
135 		.default_sample_time = 500,
136 		.num_sample_times = 4,
137 		.sample_times = (unsigned int []){ 125, 250, 500, 1000 },
138 		.resolutions = (u8 []) {9, 10, 11, 12 },
139 	},
140 	[ds75] = {
141 		.clr_mask = 3 << 5,
142 		.set_mask = 2 << 5,	/* 11-bit mode */
143 		.default_resolution = 11,
144 		.default_sample_time = 600,
145 		.num_sample_times = 4,
146 		.sample_times = (unsigned int []){ 150, 300, 600, 1200 },
147 		.resolutions = (u8 []) {9, 10, 11, 12 },
148 	},
149 	[stds75] = {
150 		.clr_mask = 3 << 5,
151 		.set_mask = 2 << 5,	/* 11-bit mode */
152 		.default_resolution = 11,
153 		.default_sample_time = 600,
154 		.num_sample_times = 4,
155 		.sample_times = (unsigned int []){ 150, 300, 600, 1200 },
156 		.resolutions = (u8 []) {9, 10, 11, 12 },
157 	},
158 	[stlm75] = {
159 		.default_resolution = 9,
160 		.default_sample_time = MSEC_PER_SEC / 6,
161 	},
162 	[ds7505] = {
163 		.set_mask = 3 << 5,	/* 12-bit mode*/
164 		.default_resolution = 12,
165 		.default_sample_time = 200,
166 		.num_sample_times = 4,
167 		.sample_times = (unsigned int []){ 25, 50, 100, 200 },
168 		.resolutions = (u8 []) {9, 10, 11, 12 },
169 	},
170 	[g751] = {
171 		.default_resolution = 9,
172 		.default_sample_time = MSEC_PER_SEC / 10,
173 	},
174 	[lm75] = {
175 		.default_resolution = 9,
176 		.default_sample_time = MSEC_PER_SEC / 10,
177 	},
178 	[lm75a] = {
179 		.default_resolution = 9,
180 		.default_sample_time = MSEC_PER_SEC / 10,
181 	},
182 	[lm75b] = {
183 		.default_resolution = 11,
184 		.default_sample_time = MSEC_PER_SEC / 10,
185 	},
186 	[max6625] = {
187 		.default_resolution = 9,
188 		.default_sample_time = MSEC_PER_SEC / 7,
189 	},
190 	[max6626] = {
191 		.default_resolution = 12,
192 		.default_sample_time = MSEC_PER_SEC / 7,
193 		.resolution_limits = 9,
194 	},
195 	[max31725] = {
196 		.default_resolution = 16,
197 		.default_sample_time = MSEC_PER_SEC / 20,
198 	},
199 	[tcn75] = {
200 		.default_resolution = 9,
201 		.default_sample_time = MSEC_PER_SEC / 18,
202 	},
203 	[pct2075] = {
204 		.default_resolution = 11,
205 		.default_sample_time = MSEC_PER_SEC / 10,
206 		.num_sample_times = 31,
207 		.sample_times = (unsigned int []){ 100, 200, 300, 400, 500, 600,
208 		700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700,
209 		1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700,
210 		2800, 2900, 3000, 3100 },
211 	},
212 	[mcp980x] = {
213 		.set_mask = 3 << 5,	/* 12-bit mode */
214 		.clr_mask = 1 << 7,	/* not one-shot mode */
215 		.default_resolution = 12,
216 		.resolution_limits = 9,
217 		.default_sample_time = 240,
218 		.num_sample_times = 4,
219 		.sample_times = (unsigned int []){ 30, 60, 120, 240 },
220 		.resolutions = (u8 []) {9, 10, 11, 12 },
221 	},
222 	[tmp100] = {
223 		.set_mask = 3 << 5,	/* 12-bit mode */
224 		.clr_mask = 1 << 7,	/* not one-shot mode */
225 		.default_resolution = 12,
226 		.default_sample_time = 320,
227 		.num_sample_times = 4,
228 		.sample_times = (unsigned int []){ 40, 80, 160, 320 },
229 		.resolutions = (u8 []) {9, 10, 11, 12 },
230 	},
231 	[tmp101] = {
232 		.set_mask = 3 << 5,	/* 12-bit mode */
233 		.clr_mask = 1 << 7,	/* not one-shot mode */
234 		.default_resolution = 12,
235 		.default_sample_time = 320,
236 		.num_sample_times = 4,
237 		.sample_times = (unsigned int []){ 40, 80, 160, 320 },
238 		.resolutions = (u8 []) {9, 10, 11, 12 },
239 	},
240 	[tmp105] = {
241 		.set_mask = 3 << 5,	/* 12-bit mode */
242 		.clr_mask = 1 << 7,	/* not one-shot mode*/
243 		.default_resolution = 12,
244 		.default_sample_time = 220,
245 		.num_sample_times = 4,
246 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
247 		.resolutions = (u8 []) {9, 10, 11, 12 },
248 	},
249 	[tmp112] = {
250 		.set_mask = 3 << 5,	/* 8 samples / second */
251 		.clr_mask = 1 << 7,	/* no one-shot mode*/
252 		.default_resolution = 12,
253 		.default_sample_time = 125,
254 		.num_sample_times = 4,
255 		.sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
256 	},
257 	[tmp175] = {
258 		.set_mask = 3 << 5,	/* 12-bit mode */
259 		.clr_mask = 1 << 7,	/* not one-shot mode*/
260 		.default_resolution = 12,
261 		.default_sample_time = 220,
262 		.num_sample_times = 4,
263 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
264 		.resolutions = (u8 []) {9, 10, 11, 12 },
265 	},
266 	[tmp275] = {
267 		.set_mask = 3 << 5,	/* 12-bit mode */
268 		.clr_mask = 1 << 7,	/* not one-shot mode*/
269 		.default_resolution = 12,
270 		.default_sample_time = 220,
271 		.num_sample_times = 4,
272 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
273 		.resolutions = (u8 []) {9, 10, 11, 12 },
274 	},
275 	[tmp75] = {
276 		.set_mask = 3 << 5,	/* 12-bit mode */
277 		.clr_mask = 1 << 7,	/* not one-shot mode*/
278 		.default_resolution = 12,
279 		.default_sample_time = 220,
280 		.num_sample_times = 4,
281 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
282 		.resolutions = (u8 []) {9, 10, 11, 12 },
283 	},
284 	[tmp75b] = { /* not one-shot mode, Conversion rate 37Hz */
285 		.clr_mask = 1 << 7 | 3 << 5,
286 		.default_resolution = 12,
287 		.default_sample_time = MSEC_PER_SEC / 37,
288 		.sample_times = (unsigned int []){ MSEC_PER_SEC / 37,
289 			MSEC_PER_SEC / 18,
290 			MSEC_PER_SEC / 9, MSEC_PER_SEC / 4 },
291 		.num_sample_times = 4,
292 	},
293 	[tmp75c] = {
294 		.clr_mask = 1 << 5,	/*not one-shot mode*/
295 		.default_resolution = 12,
296 		.default_sample_time = MSEC_PER_SEC / 12,
297 	},
298 	[tmp1075] = { /* not one-shot mode, 27.5 ms sample rate */
299 		.clr_mask = 1 << 5 | 1 << 6 | 1 << 7,
300 		.default_resolution = 12,
301 		.default_sample_time = 28,
302 		.num_sample_times = 4,
303 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
304 	}
305 };
306 
307 static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
308 {
309 	return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
310 }
311 
312 static int lm75_write_config(struct lm75_data *data, u8 set_mask,
313 			     u8 clr_mask)
314 {
315 	u8 value;
316 
317 	clr_mask |= LM75_SHUTDOWN;
318 	value = data->current_conf & ~clr_mask;
319 	value |= set_mask;
320 
321 	if (data->current_conf != value) {
322 		s32 err;
323 
324 		err = i2c_smbus_write_byte_data(data->client, LM75_REG_CONF,
325 						value);
326 		if (err)
327 			return err;
328 		data->current_conf = value;
329 	}
330 	return 0;
331 }
332 
333 static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
334 		     u32 attr, int channel, long *val)
335 {
336 	struct lm75_data *data = dev_get_drvdata(dev);
337 	unsigned int regval;
338 	int err, reg;
339 
340 	switch (type) {
341 	case hwmon_chip:
342 		switch (attr) {
343 		case hwmon_chip_update_interval:
344 			*val = data->sample_time;
345 			break;
346 		default:
347 			return -EINVAL;
348 		}
349 		break;
350 	case hwmon_temp:
351 		switch (attr) {
352 		case hwmon_temp_input:
353 			reg = LM75_REG_TEMP;
354 			break;
355 		case hwmon_temp_max:
356 			reg = LM75_REG_MAX;
357 			break;
358 		case hwmon_temp_max_hyst:
359 			reg = LM75_REG_HYST;
360 			break;
361 		default:
362 			return -EINVAL;
363 		}
364 		err = regmap_read(data->regmap, reg, &regval);
365 		if (err < 0)
366 			return err;
367 
368 		*val = lm75_reg_to_mc(regval, data->resolution);
369 		break;
370 	default:
371 		return -EINVAL;
372 	}
373 	return 0;
374 }
375 
376 static int lm75_write_temp(struct device *dev, u32 attr, long temp)
377 {
378 	struct lm75_data *data = dev_get_drvdata(dev);
379 	u8 resolution;
380 	int reg;
381 
382 	switch (attr) {
383 	case hwmon_temp_max:
384 		reg = LM75_REG_MAX;
385 		break;
386 	case hwmon_temp_max_hyst:
387 		reg = LM75_REG_HYST;
388 		break;
389 	default:
390 		return -EINVAL;
391 	}
392 
393 	/*
394 	 * Resolution of limit registers is assumed to be the same as the
395 	 * temperature input register resolution unless given explicitly.
396 	 */
397 	if (data->params->resolution_limits)
398 		resolution = data->params->resolution_limits;
399 	else
400 		resolution = data->resolution;
401 
402 	temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
403 	temp = DIV_ROUND_CLOSEST(temp  << (resolution - 8),
404 				 1000) << (16 - resolution);
405 
406 	return regmap_write(data->regmap, reg, (u16)temp);
407 }
408 
409 static int lm75_update_interval(struct device *dev, long val)
410 {
411 	struct lm75_data *data = dev_get_drvdata(dev);
412 	unsigned int reg;
413 	u8 index;
414 	s32 err;
415 
416 	index = find_closest(val, data->params->sample_times,
417 			     (int)data->params->num_sample_times);
418 
419 	switch (data->kind) {
420 	default:
421 		err = lm75_write_config(data, lm75_sample_set_masks[index],
422 					LM75_SAMPLE_CLEAR_MASK);
423 		if (err)
424 			return err;
425 
426 		data->sample_time = data->params->sample_times[index];
427 		if (data->params->resolutions)
428 			data->resolution = data->params->resolutions[index];
429 		break;
430 	case tmp112:
431 		err = regmap_read(data->regmap, LM75_REG_CONF, &reg);
432 		if (err < 0)
433 			return err;
434 		reg &= ~0x00c0;
435 		reg |= (3 - index) << 6;
436 		err = regmap_write(data->regmap, LM75_REG_CONF, reg);
437 		if (err < 0)
438 			return err;
439 		data->sample_time = data->params->sample_times[index];
440 		break;
441 	case pct2075:
442 		err = i2c_smbus_write_byte_data(data->client, PCT2075_REG_IDLE,
443 						index + 1);
444 		if (err)
445 			return err;
446 		data->sample_time = data->params->sample_times[index];
447 		break;
448 	}
449 	return 0;
450 }
451 
452 static int lm75_write_chip(struct device *dev, u32 attr, long val)
453 {
454 	switch (attr) {
455 	case hwmon_chip_update_interval:
456 		return lm75_update_interval(dev, val);
457 	default:
458 		return -EINVAL;
459 	}
460 	return 0;
461 }
462 
463 static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
464 		      u32 attr, int channel, long val)
465 {
466 	switch (type) {
467 	case hwmon_chip:
468 		return lm75_write_chip(dev, attr, val);
469 	case hwmon_temp:
470 		return lm75_write_temp(dev, attr, val);
471 	default:
472 		return -EINVAL;
473 	}
474 	return 0;
475 }
476 
477 static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
478 			       u32 attr, int channel)
479 {
480 	const struct lm75_data *config_data = data;
481 
482 	switch (type) {
483 	case hwmon_chip:
484 		switch (attr) {
485 		case hwmon_chip_update_interval:
486 			if (config_data->params->num_sample_times > 1)
487 				return 0644;
488 			return 0444;
489 		}
490 		break;
491 	case hwmon_temp:
492 		switch (attr) {
493 		case hwmon_temp_input:
494 			return 0444;
495 		case hwmon_temp_max:
496 		case hwmon_temp_max_hyst:
497 			return 0644;
498 		}
499 		break;
500 	default:
501 		break;
502 	}
503 	return 0;
504 }
505 
506 static const struct hwmon_channel_info *lm75_info[] = {
507 	HWMON_CHANNEL_INFO(chip,
508 			   HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
509 	HWMON_CHANNEL_INFO(temp,
510 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
511 	NULL
512 };
513 
514 static const struct hwmon_ops lm75_hwmon_ops = {
515 	.is_visible = lm75_is_visible,
516 	.read = lm75_read,
517 	.write = lm75_write,
518 };
519 
520 static const struct hwmon_chip_info lm75_chip_info = {
521 	.ops = &lm75_hwmon_ops,
522 	.info = lm75_info,
523 };
524 
525 static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
526 {
527 	return reg != LM75_REG_TEMP;
528 }
529 
530 static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
531 {
532 	return reg == LM75_REG_TEMP || reg == LM75_REG_CONF;
533 }
534 
535 static const struct regmap_config lm75_regmap_config = {
536 	.reg_bits = 8,
537 	.val_bits = 16,
538 	.max_register = PCT2075_REG_IDLE,
539 	.writeable_reg = lm75_is_writeable_reg,
540 	.volatile_reg = lm75_is_volatile_reg,
541 	.val_format_endian = REGMAP_ENDIAN_BIG,
542 	.cache_type = REGCACHE_RBTREE,
543 	.use_single_read = true,
544 	.use_single_write = true,
545 };
546 
547 static void lm75_disable_regulator(void *data)
548 {
549 	struct lm75_data *lm75 = data;
550 
551 	regulator_disable(lm75->vs);
552 }
553 
554 static void lm75_remove(void *data)
555 {
556 	struct lm75_data *lm75 = data;
557 	struct i2c_client *client = lm75->client;
558 
559 	i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
560 }
561 
562 static const struct i2c_device_id lm75_ids[];
563 
564 static int lm75_probe(struct i2c_client *client)
565 {
566 	struct device *dev = &client->dev;
567 	struct device *hwmon_dev;
568 	struct lm75_data *data;
569 	int status, err;
570 	enum lm75_type kind;
571 
572 	if (client->dev.of_node)
573 		kind = (enum lm75_type)of_device_get_match_data(&client->dev);
574 	else
575 		kind = i2c_match_id(lm75_ids, client)->driver_data;
576 
577 	if (!i2c_check_functionality(client->adapter,
578 			I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
579 		return -EIO;
580 
581 	data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
582 	if (!data)
583 		return -ENOMEM;
584 
585 	data->client = client;
586 	data->kind = kind;
587 
588 	data->vs = devm_regulator_get(dev, "vs");
589 	if (IS_ERR(data->vs))
590 		return PTR_ERR(data->vs);
591 
592 	data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
593 	if (IS_ERR(data->regmap))
594 		return PTR_ERR(data->regmap);
595 
596 	/* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
597 	 * Then tweak to be more precise when appropriate.
598 	 */
599 
600 	data->params = &device_params[data->kind];
601 
602 	/* Save default sample time and resolution*/
603 	data->sample_time = data->params->default_sample_time;
604 	data->resolution = data->params->default_resolution;
605 
606 	/* Enable the power */
607 	err = regulator_enable(data->vs);
608 	if (err) {
609 		dev_err(dev, "failed to enable regulator: %d\n", err);
610 		return err;
611 	}
612 
613 	err = devm_add_action_or_reset(dev, lm75_disable_regulator, data);
614 	if (err)
615 		return err;
616 
617 	/* Cache original configuration */
618 	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
619 	if (status < 0) {
620 		dev_dbg(dev, "Can't read config? %d\n", status);
621 		return status;
622 	}
623 	data->orig_conf = status;
624 	data->current_conf = status;
625 
626 	err = lm75_write_config(data, data->params->set_mask,
627 				data->params->clr_mask);
628 	if (err)
629 		return err;
630 
631 	err = devm_add_action_or_reset(dev, lm75_remove, data);
632 	if (err)
633 		return err;
634 
635 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
636 							 data, &lm75_chip_info,
637 							 NULL);
638 	if (IS_ERR(hwmon_dev))
639 		return PTR_ERR(hwmon_dev);
640 
641 	dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
642 
643 	return 0;
644 }
645 
646 static const struct i2c_device_id lm75_ids[] = {
647 	{ "adt75", adt75, },
648 	{ "ds1775", ds1775, },
649 	{ "ds75", ds75, },
650 	{ "ds7505", ds7505, },
651 	{ "g751", g751, },
652 	{ "lm75", lm75, },
653 	{ "lm75a", lm75a, },
654 	{ "lm75b", lm75b, },
655 	{ "max6625", max6625, },
656 	{ "max6626", max6626, },
657 	{ "max31725", max31725, },
658 	{ "max31726", max31725, },
659 	{ "mcp980x", mcp980x, },
660 	{ "pct2075", pct2075, },
661 	{ "stds75", stds75, },
662 	{ "stlm75", stlm75, },
663 	{ "tcn75", tcn75, },
664 	{ "tmp100", tmp100, },
665 	{ "tmp101", tmp101, },
666 	{ "tmp105", tmp105, },
667 	{ "tmp112", tmp112, },
668 	{ "tmp175", tmp175, },
669 	{ "tmp275", tmp275, },
670 	{ "tmp75", tmp75, },
671 	{ "tmp75b", tmp75b, },
672 	{ "tmp75c", tmp75c, },
673 	{ "tmp1075", tmp1075, },
674 	{ /* LIST END */ }
675 };
676 MODULE_DEVICE_TABLE(i2c, lm75_ids);
677 
678 static const struct of_device_id __maybe_unused lm75_of_match[] = {
679 	{
680 		.compatible = "adi,adt75",
681 		.data = (void *)adt75
682 	},
683 	{
684 		.compatible = "dallas,ds1775",
685 		.data = (void *)ds1775
686 	},
687 	{
688 		.compatible = "dallas,ds75",
689 		.data = (void *)ds75
690 	},
691 	{
692 		.compatible = "dallas,ds7505",
693 		.data = (void *)ds7505
694 	},
695 	{
696 		.compatible = "gmt,g751",
697 		.data = (void *)g751
698 	},
699 	{
700 		.compatible = "national,lm75",
701 		.data = (void *)lm75
702 	},
703 	{
704 		.compatible = "national,lm75a",
705 		.data = (void *)lm75a
706 	},
707 	{
708 		.compatible = "national,lm75b",
709 		.data = (void *)lm75b
710 	},
711 	{
712 		.compatible = "maxim,max6625",
713 		.data = (void *)max6625
714 	},
715 	{
716 		.compatible = "maxim,max6626",
717 		.data = (void *)max6626
718 	},
719 	{
720 		.compatible = "maxim,max31725",
721 		.data = (void *)max31725
722 	},
723 	{
724 		.compatible = "maxim,max31726",
725 		.data = (void *)max31725
726 	},
727 	{
728 		.compatible = "maxim,mcp980x",
729 		.data = (void *)mcp980x
730 	},
731 	{
732 		.compatible = "nxp,pct2075",
733 		.data = (void *)pct2075
734 	},
735 	{
736 		.compatible = "st,stds75",
737 		.data = (void *)stds75
738 	},
739 	{
740 		.compatible = "st,stlm75",
741 		.data = (void *)stlm75
742 	},
743 	{
744 		.compatible = "microchip,tcn75",
745 		.data = (void *)tcn75
746 	},
747 	{
748 		.compatible = "ti,tmp100",
749 		.data = (void *)tmp100
750 	},
751 	{
752 		.compatible = "ti,tmp101",
753 		.data = (void *)tmp101
754 	},
755 	{
756 		.compatible = "ti,tmp105",
757 		.data = (void *)tmp105
758 	},
759 	{
760 		.compatible = "ti,tmp112",
761 		.data = (void *)tmp112
762 	},
763 	{
764 		.compatible = "ti,tmp175",
765 		.data = (void *)tmp175
766 	},
767 	{
768 		.compatible = "ti,tmp275",
769 		.data = (void *)tmp275
770 	},
771 	{
772 		.compatible = "ti,tmp75",
773 		.data = (void *)tmp75
774 	},
775 	{
776 		.compatible = "ti,tmp75b",
777 		.data = (void *)tmp75b
778 	},
779 	{
780 		.compatible = "ti,tmp75c",
781 		.data = (void *)tmp75c
782 	},
783 	{
784 		.compatible = "ti,tmp1075",
785 		.data = (void *)tmp1075
786 	},
787 	{ },
788 };
789 MODULE_DEVICE_TABLE(of, lm75_of_match);
790 
791 #define LM75A_ID 0xA1
792 
793 /* Return 0 if detection is successful, -ENODEV otherwise */
794 static int lm75_detect(struct i2c_client *new_client,
795 		       struct i2c_board_info *info)
796 {
797 	struct i2c_adapter *adapter = new_client->adapter;
798 	int i;
799 	int conf, hyst, os;
800 	bool is_lm75a = 0;
801 
802 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
803 				     I2C_FUNC_SMBUS_WORD_DATA))
804 		return -ENODEV;
805 
806 	/*
807 	 * Now, we do the remaining detection. There is no identification-
808 	 * dedicated register so we have to rely on several tricks:
809 	 * unused bits, registers cycling over 8-address boundaries,
810 	 * addresses 0x04-0x07 returning the last read value.
811 	 * The cycling+unused addresses combination is not tested,
812 	 * since it would significantly slow the detection down and would
813 	 * hardly add any value.
814 	 *
815 	 * The National Semiconductor LM75A is different than earlier
816 	 * LM75s.  It has an ID byte of 0xaX (where X is the chip
817 	 * revision, with 1 being the only revision in existence) in
818 	 * register 7, and unused registers return 0xff rather than the
819 	 * last read value.
820 	 *
821 	 * Note that this function only detects the original National
822 	 * Semiconductor LM75 and the LM75A. Clones from other vendors
823 	 * aren't detected, on purpose, because they are typically never
824 	 * found on PC hardware. They are found on embedded designs where
825 	 * they can be instantiated explicitly so detection is not needed.
826 	 * The absence of identification registers on all these clones
827 	 * would make their exhaustive detection very difficult and weak,
828 	 * and odds are that the driver would bind to unsupported devices.
829 	 */
830 
831 	/* Unused bits */
832 	conf = i2c_smbus_read_byte_data(new_client, 1);
833 	if (conf & 0xe0)
834 		return -ENODEV;
835 
836 	/* First check for LM75A */
837 	if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
838 		/*
839 		 * LM75A returns 0xff on unused registers so
840 		 * just to be sure we check for that too.
841 		 */
842 		if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
843 		 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
844 		 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
845 			return -ENODEV;
846 		is_lm75a = 1;
847 		hyst = i2c_smbus_read_byte_data(new_client, 2);
848 		os = i2c_smbus_read_byte_data(new_client, 3);
849 	} else { /* Traditional style LM75 detection */
850 		/* Unused addresses */
851 		hyst = i2c_smbus_read_byte_data(new_client, 2);
852 		if (i2c_smbus_read_byte_data(new_client, 4) != hyst
853 		 || i2c_smbus_read_byte_data(new_client, 5) != hyst
854 		 || i2c_smbus_read_byte_data(new_client, 6) != hyst
855 		 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
856 			return -ENODEV;
857 		os = i2c_smbus_read_byte_data(new_client, 3);
858 		if (i2c_smbus_read_byte_data(new_client, 4) != os
859 		 || i2c_smbus_read_byte_data(new_client, 5) != os
860 		 || i2c_smbus_read_byte_data(new_client, 6) != os
861 		 || i2c_smbus_read_byte_data(new_client, 7) != os)
862 			return -ENODEV;
863 	}
864 	/*
865 	 * It is very unlikely that this is a LM75 if both
866 	 * hysteresis and temperature limit registers are 0.
867 	 */
868 	if (hyst == 0 && os == 0)
869 		return -ENODEV;
870 
871 	/* Addresses cycling */
872 	for (i = 8; i <= 248; i += 40) {
873 		if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
874 		 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
875 		 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
876 			return -ENODEV;
877 		if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
878 				!= LM75A_ID)
879 			return -ENODEV;
880 	}
881 
882 	strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
883 
884 	return 0;
885 }
886 
887 #ifdef CONFIG_PM
888 static int lm75_suspend(struct device *dev)
889 {
890 	int status;
891 	struct i2c_client *client = to_i2c_client(dev);
892 
893 	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
894 	if (status < 0) {
895 		dev_dbg(&client->dev, "Can't read config? %d\n", status);
896 		return status;
897 	}
898 	status = status | LM75_SHUTDOWN;
899 	i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
900 	return 0;
901 }
902 
903 static int lm75_resume(struct device *dev)
904 {
905 	int status;
906 	struct i2c_client *client = to_i2c_client(dev);
907 
908 	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
909 	if (status < 0) {
910 		dev_dbg(&client->dev, "Can't read config? %d\n", status);
911 		return status;
912 	}
913 	status = status & ~LM75_SHUTDOWN;
914 	i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
915 	return 0;
916 }
917 
918 static const struct dev_pm_ops lm75_dev_pm_ops = {
919 	.suspend	= lm75_suspend,
920 	.resume		= lm75_resume,
921 };
922 #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
923 #else
924 #define LM75_DEV_PM_OPS NULL
925 #endif /* CONFIG_PM */
926 
927 static struct i2c_driver lm75_driver = {
928 	.class		= I2C_CLASS_HWMON,
929 	.driver = {
930 		.name	= "lm75",
931 		.of_match_table = of_match_ptr(lm75_of_match),
932 		.pm	= LM75_DEV_PM_OPS,
933 	},
934 	.probe_new	= lm75_probe,
935 	.id_table	= lm75_ids,
936 	.detect		= lm75_detect,
937 	.address_list	= normal_i2c,
938 };
939 
940 module_i2c_driver(lm75_driver);
941 
942 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
943 MODULE_DESCRIPTION("LM75 driver");
944 MODULE_LICENSE("GPL");
945