xref: /openbmc/linux/drivers/hwmon/lm75.c (revision 8f762fe5)
1 /*
2  * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
3  *	 monitoring
4  * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/of_device.h>
30 #include <linux/of.h>
31 #include <linux/regmap.h>
32 #include "lm75.h"
33 
34 
35 /*
36  * This driver handles the LM75 and compatible digital temperature sensors.
37  */
38 
39 enum lm75_type {		/* keep sorted in alphabetical order */
40 	adt75,
41 	ds1775,
42 	ds75,
43 	ds7505,
44 	g751,
45 	lm75,
46 	lm75a,
47 	lm75b,
48 	max6625,
49 	max6626,
50 	max31725,
51 	mcp980x,
52 	stds75,
53 	stlm75,
54 	tcn75,
55 	tmp100,
56 	tmp101,
57 	tmp105,
58 	tmp112,
59 	tmp175,
60 	tmp275,
61 	tmp75,
62 	tmp75b,
63 	tmp75c,
64 };
65 
66 /* Addresses scanned */
67 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
68 					0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
69 
70 /* The LM75 registers */
71 #define LM75_REG_TEMP		0x00
72 #define LM75_REG_CONF		0x01
73 #define LM75_REG_HYST		0x02
74 #define LM75_REG_MAX		0x03
75 
76 /* Each client has this additional data */
77 struct lm75_data {
78 	struct i2c_client	*client;
79 	struct regmap		*regmap;
80 	u8			orig_conf;
81 	u8			resolution;	/* In bits, between 9 and 16 */
82 	u8			resolution_limits;
83 	unsigned int		sample_time;	/* In ms */
84 };
85 
86 /*-----------------------------------------------------------------------*/
87 
88 static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
89 {
90 	return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
91 }
92 
93 static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
94 		     u32 attr, int channel, long *val)
95 {
96 	struct lm75_data *data = dev_get_drvdata(dev);
97 	unsigned int regval;
98 	int err, reg;
99 
100 	switch (type) {
101 	case hwmon_chip:
102 		switch (attr) {
103 		case hwmon_chip_update_interval:
104 			*val = data->sample_time;
105 			break;
106 		default:
107 			return -EINVAL;
108 		}
109 		break;
110 	case hwmon_temp:
111 		switch (attr) {
112 		case hwmon_temp_input:
113 			reg = LM75_REG_TEMP;
114 			break;
115 		case hwmon_temp_max:
116 			reg = LM75_REG_MAX;
117 			break;
118 		case hwmon_temp_max_hyst:
119 			reg = LM75_REG_HYST;
120 			break;
121 		default:
122 			return -EINVAL;
123 		}
124 		err = regmap_read(data->regmap, reg, &regval);
125 		if (err < 0)
126 			return err;
127 
128 		*val = lm75_reg_to_mc(regval, data->resolution);
129 		break;
130 	default:
131 		return -EINVAL;
132 	}
133 	return 0;
134 }
135 
136 static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
137 		      u32 attr, int channel, long temp)
138 {
139 	struct lm75_data *data = dev_get_drvdata(dev);
140 	u8 resolution;
141 	int reg;
142 
143 	if (type != hwmon_temp)
144 		return -EINVAL;
145 
146 	switch (attr) {
147 	case hwmon_temp_max:
148 		reg = LM75_REG_MAX;
149 		break;
150 	case hwmon_temp_max_hyst:
151 		reg = LM75_REG_HYST;
152 		break;
153 	default:
154 		return -EINVAL;
155 	}
156 
157 	/*
158 	 * Resolution of limit registers is assumed to be the same as the
159 	 * temperature input register resolution unless given explicitly.
160 	 */
161 	if (data->resolution_limits)
162 		resolution = data->resolution_limits;
163 	else
164 		resolution = data->resolution;
165 
166 	temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
167 	temp = DIV_ROUND_CLOSEST(temp  << (resolution - 8),
168 				 1000) << (16 - resolution);
169 
170 	return regmap_write(data->regmap, reg, temp);
171 }
172 
173 static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
174 			       u32 attr, int channel)
175 {
176 	switch (type) {
177 	case hwmon_chip:
178 		switch (attr) {
179 		case hwmon_chip_update_interval:
180 			return 0444;
181 		}
182 		break;
183 	case hwmon_temp:
184 		switch (attr) {
185 		case hwmon_temp_input:
186 			return 0444;
187 		case hwmon_temp_max:
188 		case hwmon_temp_max_hyst:
189 			return 0644;
190 		}
191 		break;
192 	default:
193 		break;
194 	}
195 	return 0;
196 }
197 
198 static const struct hwmon_channel_info *lm75_info[] = {
199 	HWMON_CHANNEL_INFO(chip,
200 			   HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
201 	HWMON_CHANNEL_INFO(temp,
202 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
203 	NULL
204 };
205 
206 static const struct hwmon_ops lm75_hwmon_ops = {
207 	.is_visible = lm75_is_visible,
208 	.read = lm75_read,
209 	.write = lm75_write,
210 };
211 
212 static const struct hwmon_chip_info lm75_chip_info = {
213 	.ops = &lm75_hwmon_ops,
214 	.info = lm75_info,
215 };
216 
217 static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
218 {
219 	return reg != LM75_REG_TEMP;
220 }
221 
222 static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
223 {
224 	return reg == LM75_REG_TEMP;
225 }
226 
227 static const struct regmap_config lm75_regmap_config = {
228 	.reg_bits = 8,
229 	.val_bits = 16,
230 	.max_register = LM75_REG_MAX,
231 	.writeable_reg = lm75_is_writeable_reg,
232 	.volatile_reg = lm75_is_volatile_reg,
233 	.val_format_endian = REGMAP_ENDIAN_BIG,
234 	.cache_type = REGCACHE_RBTREE,
235 	.use_single_read = true,
236 	.use_single_write = true,
237 };
238 
239 static void lm75_remove(void *data)
240 {
241 	struct lm75_data *lm75 = data;
242 	struct i2c_client *client = lm75->client;
243 
244 	i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
245 }
246 
247 static int
248 lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
249 {
250 	struct device *dev = &client->dev;
251 	struct device *hwmon_dev;
252 	struct lm75_data *data;
253 	int status, err;
254 	u8 set_mask, clr_mask;
255 	int new;
256 	enum lm75_type kind;
257 
258 	if (client->dev.of_node)
259 		kind = (enum lm75_type)of_device_get_match_data(&client->dev);
260 	else
261 		kind = id->driver_data;
262 
263 	if (!i2c_check_functionality(client->adapter,
264 			I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
265 		return -EIO;
266 
267 	data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
268 	if (!data)
269 		return -ENOMEM;
270 
271 	data->client = client;
272 
273 	data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
274 	if (IS_ERR(data->regmap))
275 		return PTR_ERR(data->regmap);
276 
277 	/* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
278 	 * Then tweak to be more precise when appropriate.
279 	 */
280 	set_mask = 0;
281 	clr_mask = LM75_SHUTDOWN;		/* continuous conversions */
282 
283 	switch (kind) {
284 	case adt75:
285 		clr_mask |= 1 << 5;		/* not one-shot mode */
286 		data->resolution = 12;
287 		data->sample_time = MSEC_PER_SEC / 8;
288 		break;
289 	case ds1775:
290 	case ds75:
291 	case stds75:
292 		clr_mask |= 3 << 5;
293 		set_mask |= 2 << 5;		/* 11-bit mode */
294 		data->resolution = 11;
295 		data->sample_time = MSEC_PER_SEC;
296 		break;
297 	case stlm75:
298 		data->resolution = 9;
299 		data->sample_time = MSEC_PER_SEC / 5;
300 		break;
301 	case ds7505:
302 		set_mask |= 3 << 5;		/* 12-bit mode */
303 		data->resolution = 12;
304 		data->sample_time = MSEC_PER_SEC / 4;
305 		break;
306 	case g751:
307 	case lm75:
308 	case lm75a:
309 		data->resolution = 9;
310 		data->sample_time = MSEC_PER_SEC / 2;
311 		break;
312 	case lm75b:
313 		data->resolution = 11;
314 		data->sample_time = MSEC_PER_SEC / 4;
315 		break;
316 	case max6625:
317 		data->resolution = 9;
318 		data->sample_time = MSEC_PER_SEC / 4;
319 		break;
320 	case max6626:
321 		data->resolution = 12;
322 		data->resolution_limits = 9;
323 		data->sample_time = MSEC_PER_SEC / 4;
324 		break;
325 	case max31725:
326 		data->resolution = 16;
327 		data->sample_time = MSEC_PER_SEC / 8;
328 		break;
329 	case tcn75:
330 		data->resolution = 9;
331 		data->sample_time = MSEC_PER_SEC / 8;
332 		break;
333 	case mcp980x:
334 		data->resolution_limits = 9;
335 		/* fall through */
336 	case tmp100:
337 	case tmp101:
338 		set_mask |= 3 << 5;		/* 12-bit mode */
339 		data->resolution = 12;
340 		data->sample_time = MSEC_PER_SEC;
341 		clr_mask |= 1 << 7;		/* not one-shot mode */
342 		break;
343 	case tmp112:
344 		set_mask |= 3 << 5;		/* 12-bit mode */
345 		clr_mask |= 1 << 7;		/* not one-shot mode */
346 		data->resolution = 12;
347 		data->sample_time = MSEC_PER_SEC / 4;
348 		break;
349 	case tmp105:
350 	case tmp175:
351 	case tmp275:
352 	case tmp75:
353 		set_mask |= 3 << 5;		/* 12-bit mode */
354 		clr_mask |= 1 << 7;		/* not one-shot mode */
355 		data->resolution = 12;
356 		data->sample_time = MSEC_PER_SEC / 2;
357 		break;
358 	case tmp75b:  /* not one-shot mode, Conversion rate 37Hz */
359 		clr_mask |= 1 << 15 | 0x3 << 13;
360 		data->resolution = 12;
361 		data->sample_time = MSEC_PER_SEC / 37;
362 		break;
363 	case tmp75c:
364 		clr_mask |= 1 << 5;		/* not one-shot mode */
365 		data->resolution = 12;
366 		data->sample_time = MSEC_PER_SEC / 4;
367 		break;
368 	}
369 
370 	/* configure as specified */
371 	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
372 	if (status < 0) {
373 		dev_dbg(dev, "Can't read config? %d\n", status);
374 		return status;
375 	}
376 	data->orig_conf = status;
377 	new = status & ~clr_mask;
378 	new |= set_mask;
379 	if (status != new)
380 		i2c_smbus_write_byte_data(client, LM75_REG_CONF, new);
381 
382 	err = devm_add_action_or_reset(dev, lm75_remove, data);
383 	if (err)
384 		return err;
385 
386 	dev_dbg(dev, "Config %02x\n", new);
387 
388 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
389 							 data, &lm75_chip_info,
390 							 NULL);
391 	if (IS_ERR(hwmon_dev))
392 		return PTR_ERR(hwmon_dev);
393 
394 	dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
395 
396 	return 0;
397 }
398 
399 static const struct i2c_device_id lm75_ids[] = {
400 	{ "adt75", adt75, },
401 	{ "ds1775", ds1775, },
402 	{ "ds75", ds75, },
403 	{ "ds7505", ds7505, },
404 	{ "g751", g751, },
405 	{ "lm75", lm75, },
406 	{ "lm75a", lm75a, },
407 	{ "lm75b", lm75b, },
408 	{ "max6625", max6625, },
409 	{ "max6626", max6626, },
410 	{ "max31725", max31725, },
411 	{ "max31726", max31725, },
412 	{ "mcp980x", mcp980x, },
413 	{ "stds75", stds75, },
414 	{ "stlm75", stlm75, },
415 	{ "tcn75", tcn75, },
416 	{ "tmp100", tmp100, },
417 	{ "tmp101", tmp101, },
418 	{ "tmp105", tmp105, },
419 	{ "tmp112", tmp112, },
420 	{ "tmp175", tmp175, },
421 	{ "tmp275", tmp275, },
422 	{ "tmp75", tmp75, },
423 	{ "tmp75b", tmp75b, },
424 	{ "tmp75c", tmp75c, },
425 	{ /* LIST END */ }
426 };
427 MODULE_DEVICE_TABLE(i2c, lm75_ids);
428 
429 static const struct of_device_id __maybe_unused lm75_of_match[] = {
430 	{
431 		.compatible = "adi,adt75",
432 		.data = (void *)adt75
433 	},
434 	{
435 		.compatible = "dallas,ds1775",
436 		.data = (void *)ds1775
437 	},
438 	{
439 		.compatible = "dallas,ds75",
440 		.data = (void *)ds75
441 	},
442 	{
443 		.compatible = "dallas,ds7505",
444 		.data = (void *)ds7505
445 	},
446 	{
447 		.compatible = "gmt,g751",
448 		.data = (void *)g751
449 	},
450 	{
451 		.compatible = "national,lm75",
452 		.data = (void *)lm75
453 	},
454 	{
455 		.compatible = "national,lm75a",
456 		.data = (void *)lm75a
457 	},
458 	{
459 		.compatible = "national,lm75b",
460 		.data = (void *)lm75b
461 	},
462 	{
463 		.compatible = "maxim,max6625",
464 		.data = (void *)max6625
465 	},
466 	{
467 		.compatible = "maxim,max6626",
468 		.data = (void *)max6626
469 	},
470 	{
471 		.compatible = "maxim,max31725",
472 		.data = (void *)max31725
473 	},
474 	{
475 		.compatible = "maxim,max31726",
476 		.data = (void *)max31725
477 	},
478 	{
479 		.compatible = "maxim,mcp980x",
480 		.data = (void *)mcp980x
481 	},
482 	{
483 		.compatible = "st,stds75",
484 		.data = (void *)stds75
485 	},
486 	{
487 		.compatible = "st,stlm75",
488 		.data = (void *)stlm75
489 	},
490 	{
491 		.compatible = "microchip,tcn75",
492 		.data = (void *)tcn75
493 	},
494 	{
495 		.compatible = "ti,tmp100",
496 		.data = (void *)tmp100
497 	},
498 	{
499 		.compatible = "ti,tmp101",
500 		.data = (void *)tmp101
501 	},
502 	{
503 		.compatible = "ti,tmp105",
504 		.data = (void *)tmp105
505 	},
506 	{
507 		.compatible = "ti,tmp112",
508 		.data = (void *)tmp112
509 	},
510 	{
511 		.compatible = "ti,tmp175",
512 		.data = (void *)tmp175
513 	},
514 	{
515 		.compatible = "ti,tmp275",
516 		.data = (void *)tmp275
517 	},
518 	{
519 		.compatible = "ti,tmp75",
520 		.data = (void *)tmp75
521 	},
522 	{
523 		.compatible = "ti,tmp75b",
524 		.data = (void *)tmp75b
525 	},
526 	{
527 		.compatible = "ti,tmp75c",
528 		.data = (void *)tmp75c
529 	},
530 	{ },
531 };
532 MODULE_DEVICE_TABLE(of, lm75_of_match);
533 
534 #define LM75A_ID 0xA1
535 
536 /* Return 0 if detection is successful, -ENODEV otherwise */
537 static int lm75_detect(struct i2c_client *new_client,
538 		       struct i2c_board_info *info)
539 {
540 	struct i2c_adapter *adapter = new_client->adapter;
541 	int i;
542 	int conf, hyst, os;
543 	bool is_lm75a = 0;
544 
545 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
546 				     I2C_FUNC_SMBUS_WORD_DATA))
547 		return -ENODEV;
548 
549 	/*
550 	 * Now, we do the remaining detection. There is no identification-
551 	 * dedicated register so we have to rely on several tricks:
552 	 * unused bits, registers cycling over 8-address boundaries,
553 	 * addresses 0x04-0x07 returning the last read value.
554 	 * The cycling+unused addresses combination is not tested,
555 	 * since it would significantly slow the detection down and would
556 	 * hardly add any value.
557 	 *
558 	 * The National Semiconductor LM75A is different than earlier
559 	 * LM75s.  It has an ID byte of 0xaX (where X is the chip
560 	 * revision, with 1 being the only revision in existence) in
561 	 * register 7, and unused registers return 0xff rather than the
562 	 * last read value.
563 	 *
564 	 * Note that this function only detects the original National
565 	 * Semiconductor LM75 and the LM75A. Clones from other vendors
566 	 * aren't detected, on purpose, because they are typically never
567 	 * found on PC hardware. They are found on embedded designs where
568 	 * they can be instantiated explicitly so detection is not needed.
569 	 * The absence of identification registers on all these clones
570 	 * would make their exhaustive detection very difficult and weak,
571 	 * and odds are that the driver would bind to unsupported devices.
572 	 */
573 
574 	/* Unused bits */
575 	conf = i2c_smbus_read_byte_data(new_client, 1);
576 	if (conf & 0xe0)
577 		return -ENODEV;
578 
579 	/* First check for LM75A */
580 	if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
581 		/* LM75A returns 0xff on unused registers so
582 		   just to be sure we check for that too. */
583 		if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
584 		 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
585 		 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
586 			return -ENODEV;
587 		is_lm75a = 1;
588 		hyst = i2c_smbus_read_byte_data(new_client, 2);
589 		os = i2c_smbus_read_byte_data(new_client, 3);
590 	} else { /* Traditional style LM75 detection */
591 		/* Unused addresses */
592 		hyst = i2c_smbus_read_byte_data(new_client, 2);
593 		if (i2c_smbus_read_byte_data(new_client, 4) != hyst
594 		 || i2c_smbus_read_byte_data(new_client, 5) != hyst
595 		 || i2c_smbus_read_byte_data(new_client, 6) != hyst
596 		 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
597 			return -ENODEV;
598 		os = i2c_smbus_read_byte_data(new_client, 3);
599 		if (i2c_smbus_read_byte_data(new_client, 4) != os
600 		 || i2c_smbus_read_byte_data(new_client, 5) != os
601 		 || i2c_smbus_read_byte_data(new_client, 6) != os
602 		 || i2c_smbus_read_byte_data(new_client, 7) != os)
603 			return -ENODEV;
604 	}
605 	/*
606 	 * It is very unlikely that this is a LM75 if both
607 	 * hysteresis and temperature limit registers are 0.
608 	 */
609 	if (hyst == 0 && os == 0)
610 		return -ENODEV;
611 
612 	/* Addresses cycling */
613 	for (i = 8; i <= 248; i += 40) {
614 		if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
615 		 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
616 		 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
617 			return -ENODEV;
618 		if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
619 				!= LM75A_ID)
620 			return -ENODEV;
621 	}
622 
623 	strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
624 
625 	return 0;
626 }
627 
628 #ifdef CONFIG_PM
629 static int lm75_suspend(struct device *dev)
630 {
631 	int status;
632 	struct i2c_client *client = to_i2c_client(dev);
633 	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
634 	if (status < 0) {
635 		dev_dbg(&client->dev, "Can't read config? %d\n", status);
636 		return status;
637 	}
638 	status = status | LM75_SHUTDOWN;
639 	i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
640 	return 0;
641 }
642 
643 static int lm75_resume(struct device *dev)
644 {
645 	int status;
646 	struct i2c_client *client = to_i2c_client(dev);
647 	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
648 	if (status < 0) {
649 		dev_dbg(&client->dev, "Can't read config? %d\n", status);
650 		return status;
651 	}
652 	status = status & ~LM75_SHUTDOWN;
653 	i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
654 	return 0;
655 }
656 
657 static const struct dev_pm_ops lm75_dev_pm_ops = {
658 	.suspend	= lm75_suspend,
659 	.resume		= lm75_resume,
660 };
661 #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
662 #else
663 #define LM75_DEV_PM_OPS NULL
664 #endif /* CONFIG_PM */
665 
666 static struct i2c_driver lm75_driver = {
667 	.class		= I2C_CLASS_HWMON,
668 	.driver = {
669 		.name	= "lm75",
670 		.of_match_table = of_match_ptr(lm75_of_match),
671 		.pm	= LM75_DEV_PM_OPS,
672 	},
673 	.probe		= lm75_probe,
674 	.id_table	= lm75_ids,
675 	.detect		= lm75_detect,
676 	.address_list	= normal_i2c,
677 };
678 
679 module_i2c_driver(lm75_driver);
680 
681 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
682 MODULE_DESCRIPTION("LM75 driver");
683 MODULE_LICENSE("GPL");
684