1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
5 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
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>
18 #include <linux/regmap.h>
23 * This driver handles the LM75 and compatible digital temperature sensors.
26 enum lm75_type { /* keep sorted in alphabetical order */
53 /* Addresses scanned */
54 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
55 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
57 /* The LM75 registers */
58 #define LM75_REG_TEMP 0x00
59 #define LM75_REG_CONF 0x01
60 #define LM75_REG_HYST 0x02
61 #define LM75_REG_MAX 0x03
63 /* Each client has this additional data */
65 struct i2c_client *client;
66 struct regmap *regmap;
68 u8 resolution; /* In bits, between 9 and 16 */
70 unsigned int sample_time; /* In ms */
73 /*-----------------------------------------------------------------------*/
75 static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
77 return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
80 static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
81 u32 attr, int channel, long *val)
83 struct lm75_data *data = dev_get_drvdata(dev);
90 case hwmon_chip_update_interval:
91 *val = data->sample_time;
99 case hwmon_temp_input:
105 case hwmon_temp_max_hyst:
111 err = regmap_read(data->regmap, reg, ®val);
115 *val = lm75_reg_to_mc(regval, data->resolution);
123 static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
124 u32 attr, int channel, long temp)
126 struct lm75_data *data = dev_get_drvdata(dev);
130 if (type != hwmon_temp)
137 case hwmon_temp_max_hyst:
145 * Resolution of limit registers is assumed to be the same as the
146 * temperature input register resolution unless given explicitly.
148 if (data->resolution_limits)
149 resolution = data->resolution_limits;
151 resolution = data->resolution;
153 temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
154 temp = DIV_ROUND_CLOSEST(temp << (resolution - 8),
155 1000) << (16 - resolution);
157 return regmap_write(data->regmap, reg, temp);
160 static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
161 u32 attr, int channel)
166 case hwmon_chip_update_interval:
172 case hwmon_temp_input:
175 case hwmon_temp_max_hyst:
185 static const struct hwmon_channel_info *lm75_info[] = {
186 HWMON_CHANNEL_INFO(chip,
187 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
188 HWMON_CHANNEL_INFO(temp,
189 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
193 static const struct hwmon_ops lm75_hwmon_ops = {
194 .is_visible = lm75_is_visible,
199 static const struct hwmon_chip_info lm75_chip_info = {
200 .ops = &lm75_hwmon_ops,
204 static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
206 return reg != LM75_REG_TEMP;
209 static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
211 return reg == LM75_REG_TEMP;
214 static const struct regmap_config lm75_regmap_config = {
217 .max_register = LM75_REG_MAX,
218 .writeable_reg = lm75_is_writeable_reg,
219 .volatile_reg = lm75_is_volatile_reg,
220 .val_format_endian = REGMAP_ENDIAN_BIG,
221 .cache_type = REGCACHE_RBTREE,
222 .use_single_read = true,
223 .use_single_write = true,
226 static void lm75_remove(void *data)
228 struct lm75_data *lm75 = data;
229 struct i2c_client *client = lm75->client;
231 i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
235 lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
237 struct device *dev = &client->dev;
238 struct device *hwmon_dev;
239 struct lm75_data *data;
241 u8 set_mask, clr_mask;
245 if (client->dev.of_node)
246 kind = (enum lm75_type)of_device_get_match_data(&client->dev);
248 kind = id->driver_data;
250 if (!i2c_check_functionality(client->adapter,
251 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
254 data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
258 data->client = client;
260 data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
261 if (IS_ERR(data->regmap))
262 return PTR_ERR(data->regmap);
264 /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
265 * Then tweak to be more precise when appropriate.
268 clr_mask = LM75_SHUTDOWN; /* continuous conversions */
272 clr_mask |= 1 << 5; /* not one-shot mode */
273 data->resolution = 12;
274 data->sample_time = MSEC_PER_SEC / 8;
280 set_mask |= 2 << 5; /* 11-bit mode */
281 data->resolution = 11;
282 data->sample_time = MSEC_PER_SEC;
285 data->resolution = 9;
286 data->sample_time = MSEC_PER_SEC / 5;
289 set_mask |= 3 << 5; /* 12-bit mode */
290 data->resolution = 12;
291 data->sample_time = MSEC_PER_SEC / 4;
296 data->resolution = 9;
297 data->sample_time = MSEC_PER_SEC / 2;
300 data->resolution = 11;
301 data->sample_time = MSEC_PER_SEC / 4;
304 data->resolution = 9;
305 data->sample_time = MSEC_PER_SEC / 4;
308 data->resolution = 12;
309 data->resolution_limits = 9;
310 data->sample_time = MSEC_PER_SEC / 4;
313 data->resolution = 16;
314 data->sample_time = MSEC_PER_SEC / 8;
317 data->resolution = 9;
318 data->sample_time = MSEC_PER_SEC / 8;
321 data->resolution_limits = 9;
325 set_mask |= 3 << 5; /* 12-bit mode */
326 data->resolution = 12;
327 data->sample_time = MSEC_PER_SEC;
328 clr_mask |= 1 << 7; /* not one-shot mode */
331 set_mask |= 3 << 5; /* 12-bit mode */
332 clr_mask |= 1 << 7; /* not one-shot mode */
333 data->resolution = 12;
334 data->sample_time = MSEC_PER_SEC / 4;
340 set_mask |= 3 << 5; /* 12-bit mode */
341 clr_mask |= 1 << 7; /* not one-shot mode */
342 data->resolution = 12;
343 data->sample_time = MSEC_PER_SEC / 2;
345 case tmp75b: /* not one-shot mode, Conversion rate 37Hz */
346 clr_mask |= 1 << 15 | 0x3 << 13;
347 data->resolution = 12;
348 data->sample_time = MSEC_PER_SEC / 37;
351 clr_mask |= 1 << 5; /* not one-shot mode */
352 data->resolution = 12;
353 data->sample_time = MSEC_PER_SEC / 4;
357 /* configure as specified */
358 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
360 dev_dbg(dev, "Can't read config? %d\n", status);
363 data->orig_conf = status;
364 new = status & ~clr_mask;
367 i2c_smbus_write_byte_data(client, LM75_REG_CONF, new);
369 err = devm_add_action_or_reset(dev, lm75_remove, data);
373 dev_dbg(dev, "Config %02x\n", new);
375 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
376 data, &lm75_chip_info,
378 if (IS_ERR(hwmon_dev))
379 return PTR_ERR(hwmon_dev);
381 dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
386 static const struct i2c_device_id lm75_ids[] = {
388 { "ds1775", ds1775, },
390 { "ds7505", ds7505, },
395 { "max6625", max6625, },
396 { "max6626", max6626, },
397 { "max31725", max31725, },
398 { "max31726", max31725, },
399 { "mcp980x", mcp980x, },
400 { "stds75", stds75, },
401 { "stlm75", stlm75, },
403 { "tmp100", tmp100, },
404 { "tmp101", tmp101, },
405 { "tmp105", tmp105, },
406 { "tmp112", tmp112, },
407 { "tmp175", tmp175, },
408 { "tmp275", tmp275, },
410 { "tmp75b", tmp75b, },
411 { "tmp75c", tmp75c, },
414 MODULE_DEVICE_TABLE(i2c, lm75_ids);
416 static const struct of_device_id __maybe_unused lm75_of_match[] = {
418 .compatible = "adi,adt75",
419 .data = (void *)adt75
422 .compatible = "dallas,ds1775",
423 .data = (void *)ds1775
426 .compatible = "dallas,ds75",
430 .compatible = "dallas,ds7505",
431 .data = (void *)ds7505
434 .compatible = "gmt,g751",
438 .compatible = "national,lm75",
442 .compatible = "national,lm75a",
443 .data = (void *)lm75a
446 .compatible = "national,lm75b",
447 .data = (void *)lm75b
450 .compatible = "maxim,max6625",
451 .data = (void *)max6625
454 .compatible = "maxim,max6626",
455 .data = (void *)max6626
458 .compatible = "maxim,max31725",
459 .data = (void *)max31725
462 .compatible = "maxim,max31726",
463 .data = (void *)max31725
466 .compatible = "maxim,mcp980x",
467 .data = (void *)mcp980x
470 .compatible = "st,stds75",
471 .data = (void *)stds75
474 .compatible = "st,stlm75",
475 .data = (void *)stlm75
478 .compatible = "microchip,tcn75",
479 .data = (void *)tcn75
482 .compatible = "ti,tmp100",
483 .data = (void *)tmp100
486 .compatible = "ti,tmp101",
487 .data = (void *)tmp101
490 .compatible = "ti,tmp105",
491 .data = (void *)tmp105
494 .compatible = "ti,tmp112",
495 .data = (void *)tmp112
498 .compatible = "ti,tmp175",
499 .data = (void *)tmp175
502 .compatible = "ti,tmp275",
503 .data = (void *)tmp275
506 .compatible = "ti,tmp75",
507 .data = (void *)tmp75
510 .compatible = "ti,tmp75b",
511 .data = (void *)tmp75b
514 .compatible = "ti,tmp75c",
515 .data = (void *)tmp75c
519 MODULE_DEVICE_TABLE(of, lm75_of_match);
521 #define LM75A_ID 0xA1
523 /* Return 0 if detection is successful, -ENODEV otherwise */
524 static int lm75_detect(struct i2c_client *new_client,
525 struct i2c_board_info *info)
527 struct i2c_adapter *adapter = new_client->adapter;
532 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
533 I2C_FUNC_SMBUS_WORD_DATA))
537 * Now, we do the remaining detection. There is no identification-
538 * dedicated register so we have to rely on several tricks:
539 * unused bits, registers cycling over 8-address boundaries,
540 * addresses 0x04-0x07 returning the last read value.
541 * The cycling+unused addresses combination is not tested,
542 * since it would significantly slow the detection down and would
543 * hardly add any value.
545 * The National Semiconductor LM75A is different than earlier
546 * LM75s. It has an ID byte of 0xaX (where X is the chip
547 * revision, with 1 being the only revision in existence) in
548 * register 7, and unused registers return 0xff rather than the
551 * Note that this function only detects the original National
552 * Semiconductor LM75 and the LM75A. Clones from other vendors
553 * aren't detected, on purpose, because they are typically never
554 * found on PC hardware. They are found on embedded designs where
555 * they can be instantiated explicitly so detection is not needed.
556 * The absence of identification registers on all these clones
557 * would make their exhaustive detection very difficult and weak,
558 * and odds are that the driver would bind to unsupported devices.
562 conf = i2c_smbus_read_byte_data(new_client, 1);
566 /* First check for LM75A */
567 if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
568 /* LM75A returns 0xff on unused registers so
569 just to be sure we check for that too. */
570 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
571 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
572 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
575 hyst = i2c_smbus_read_byte_data(new_client, 2);
576 os = i2c_smbus_read_byte_data(new_client, 3);
577 } else { /* Traditional style LM75 detection */
578 /* Unused addresses */
579 hyst = i2c_smbus_read_byte_data(new_client, 2);
580 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
581 || i2c_smbus_read_byte_data(new_client, 5) != hyst
582 || i2c_smbus_read_byte_data(new_client, 6) != hyst
583 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
585 os = i2c_smbus_read_byte_data(new_client, 3);
586 if (i2c_smbus_read_byte_data(new_client, 4) != os
587 || i2c_smbus_read_byte_data(new_client, 5) != os
588 || i2c_smbus_read_byte_data(new_client, 6) != os
589 || i2c_smbus_read_byte_data(new_client, 7) != os)
593 * It is very unlikely that this is a LM75 if both
594 * hysteresis and temperature limit registers are 0.
596 if (hyst == 0 && os == 0)
599 /* Addresses cycling */
600 for (i = 8; i <= 248; i += 40) {
601 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
602 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
603 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
605 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
610 strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
616 static int lm75_suspend(struct device *dev)
619 struct i2c_client *client = to_i2c_client(dev);
620 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
622 dev_dbg(&client->dev, "Can't read config? %d\n", status);
625 status = status | LM75_SHUTDOWN;
626 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
630 static int lm75_resume(struct device *dev)
633 struct i2c_client *client = to_i2c_client(dev);
634 status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
636 dev_dbg(&client->dev, "Can't read config? %d\n", status);
639 status = status & ~LM75_SHUTDOWN;
640 i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
644 static const struct dev_pm_ops lm75_dev_pm_ops = {
645 .suspend = lm75_suspend,
646 .resume = lm75_resume,
648 #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
650 #define LM75_DEV_PM_OPS NULL
651 #endif /* CONFIG_PM */
653 static struct i2c_driver lm75_driver = {
654 .class = I2C_CLASS_HWMON,
657 .of_match_table = of_match_ptr(lm75_of_match),
658 .pm = LM75_DEV_PM_OPS,
661 .id_table = lm75_ids,
662 .detect = lm75_detect,
663 .address_list = normal_i2c,
666 module_i2c_driver(lm75_driver);
668 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
669 MODULE_DESCRIPTION("LM75 driver");
670 MODULE_LICENSE("GPL");