1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
5 * Copyright (C) 2003-2009 Jean Delvare <jdelvare@suse.de>
7 * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
8 * a sensor chip made by National Semiconductor. It reports up to four
9 * temperatures (its own plus up to three external ones) with a 1 deg
10 * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
11 * from National's website at:
12 * http://www.national.com/pf/LM/LM83.html
13 * Since the datasheet omits to give the chip stepping code, I give it
14 * here: 0x03 (at register 0xff).
16 * Also supports the LM82 temp sensor, which is basically a stripped down
17 * model of the LM83. Datasheet is here:
18 * http://www.national.com/pf/LM/LM82.html
21 #include <linux/bits.h>
22 #include <linux/err.h>
23 #include <linux/i2c.h>
24 #include <linux/init.h>
25 #include <linux/hwmon.h>
26 #include <linux/module.h>
27 #include <linux/regmap.h>
28 #include <linux/slab.h>
32 * Address is selected using 2 three-level pins, resulting in 9 possible
36 static const unsigned short normal_i2c[] = {
37 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
39 enum chips { lm83, lm82 };
43 * Manufacturer ID is 0x01 for National Semiconductor.
46 #define LM83_REG_R_MAN_ID 0xFE
47 #define LM83_REG_R_CHIP_ID 0xFF
48 #define LM83_REG_R_CONFIG 0x03
49 #define LM83_REG_W_CONFIG 0x09
50 #define LM83_REG_R_STATUS1 0x02
51 #define LM83_REG_R_STATUS2 0x35
52 #define LM83_REG_R_LOCAL_TEMP 0x00
53 #define LM83_REG_R_LOCAL_HIGH 0x05
54 #define LM83_REG_W_LOCAL_HIGH 0x0B
55 #define LM83_REG_R_REMOTE1_TEMP 0x30
56 #define LM83_REG_R_REMOTE1_HIGH 0x38
57 #define LM83_REG_W_REMOTE1_HIGH 0x50
58 #define LM83_REG_R_REMOTE2_TEMP 0x01
59 #define LM83_REG_R_REMOTE2_HIGH 0x07
60 #define LM83_REG_W_REMOTE2_HIGH 0x0D
61 #define LM83_REG_R_REMOTE3_TEMP 0x31
62 #define LM83_REG_R_REMOTE3_HIGH 0x3A
63 #define LM83_REG_W_REMOTE3_HIGH 0x52
64 #define LM83_REG_R_TCRIT 0x42
65 #define LM83_REG_W_TCRIT 0x5A
67 static const u8 LM83_REG_TEMP[] = {
68 LM83_REG_R_LOCAL_TEMP,
69 LM83_REG_R_REMOTE1_TEMP,
70 LM83_REG_R_REMOTE2_TEMP,
71 LM83_REG_R_REMOTE3_TEMP,
74 static const u8 LM83_REG_MAX[] = {
75 LM83_REG_R_LOCAL_HIGH,
76 LM83_REG_R_REMOTE1_HIGH,
77 LM83_REG_R_REMOTE2_HIGH,
78 LM83_REG_R_REMOTE3_HIGH,
81 /* alarm and fault registers and bits, indexed by channel */
82 static const u8 LM83_ALARM_REG[] = {
83 LM83_REG_R_STATUS1, LM83_REG_R_STATUS2, LM83_REG_R_STATUS1, LM83_REG_R_STATUS2
86 static const u8 LM83_MAX_ALARM_BIT[] = {
87 BIT(6), BIT(7), BIT(4), BIT(4)
90 static const u8 LM83_CRIT_ALARM_BIT[] = {
91 BIT(0), BIT(0), BIT(1), BIT(1)
94 static const u8 LM83_FAULT_BIT[] = {
95 0, BIT(5), BIT(2), BIT(2)
99 * Client data (each client gets its own)
103 struct regmap *regmap;
109 static int lm83_regmap_reg_read(void *context, unsigned int reg, unsigned int *val)
111 struct i2c_client *client = context;
114 ret = i2c_smbus_read_byte_data(client, reg);
123 * The regmap write function maps read register addresses to write register
124 * addresses. This is necessary for regmap register caching to work.
125 * An alternative would be to clear the regmap cache whenever a register is
126 * written, but that would be much more expensive.
128 static int lm83_regmap_reg_write(void *context, unsigned int reg, unsigned int val)
130 struct i2c_client *client = context;
133 case LM83_REG_R_CONFIG:
134 case LM83_REG_R_LOCAL_HIGH:
135 case LM83_REG_R_REMOTE2_HIGH:
138 case LM83_REG_R_REMOTE1_HIGH:
139 case LM83_REG_R_REMOTE3_HIGH:
140 case LM83_REG_R_TCRIT:
147 return i2c_smbus_write_byte_data(client, reg, val);
150 static bool lm83_regmap_is_volatile(struct device *dev, unsigned int reg)
153 case LM83_REG_R_LOCAL_TEMP:
154 case LM83_REG_R_REMOTE1_TEMP:
155 case LM83_REG_R_REMOTE2_TEMP:
156 case LM83_REG_R_REMOTE3_TEMP:
157 case LM83_REG_R_STATUS1:
158 case LM83_REG_R_STATUS2:
165 static const struct regmap_config lm83_regmap_config = {
168 .cache_type = REGCACHE_RBTREE,
169 .volatile_reg = lm83_regmap_is_volatile,
170 .reg_read = lm83_regmap_reg_read,
171 .reg_write = lm83_regmap_reg_write,
176 static int lm83_temp_read(struct device *dev, u32 attr, int channel, long *val)
178 struct lm83_data *data = dev_get_drvdata(dev);
183 case hwmon_temp_input:
184 err = regmap_read(data->regmap, LM83_REG_TEMP[channel], ®val);
187 *val = (s8)regval * 1000;
190 err = regmap_read(data->regmap, LM83_REG_MAX[channel], ®val);
193 *val = (s8)regval * 1000;
195 case hwmon_temp_crit:
196 err = regmap_read(data->regmap, LM83_REG_R_TCRIT, ®val);
199 *val = (s8)regval * 1000;
201 case hwmon_temp_max_alarm:
202 err = regmap_read(data->regmap, LM83_ALARM_REG[channel], ®val);
205 *val = !!(regval & LM83_MAX_ALARM_BIT[channel]);
207 case hwmon_temp_crit_alarm:
208 err = regmap_read(data->regmap, LM83_ALARM_REG[channel], ®val);
211 *val = !!(regval & LM83_CRIT_ALARM_BIT[channel]);
213 case hwmon_temp_fault:
214 err = regmap_read(data->regmap, LM83_ALARM_REG[channel], ®val);
217 *val = !!(regval & LM83_FAULT_BIT[channel]);
225 static int lm83_temp_write(struct device *dev, u32 attr, int channel, long val)
227 struct lm83_data *data = dev_get_drvdata(dev);
231 regval = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
235 err = regmap_write(data->regmap, LM83_REG_MAX[channel], regval);
239 case hwmon_temp_crit:
240 err = regmap_write(data->regmap, LM83_REG_R_TCRIT, regval);
250 static int lm83_chip_read(struct device *dev, u32 attr, int channel, long *val)
252 struct lm83_data *data = dev_get_drvdata(dev);
257 case hwmon_chip_alarms:
258 err = regmap_read(data->regmap, LM83_REG_R_STATUS1, ®val);
262 err = regmap_read(data->regmap, LM83_REG_R_STATUS2, ®val);
274 static int lm83_read(struct device *dev, enum hwmon_sensor_types type,
275 u32 attr, int channel, long *val)
279 return lm83_chip_read(dev, attr, channel, val);
281 return lm83_temp_read(dev, attr, channel, val);
287 static int lm83_write(struct device *dev, enum hwmon_sensor_types type,
288 u32 attr, int channel, long val)
292 return lm83_temp_write(dev, attr, channel, val);
298 static umode_t lm83_is_visible(const void *_data, enum hwmon_sensor_types type,
299 u32 attr, int channel)
301 const struct lm83_data *data = _data;
304 * LM82 only supports a single external channel, modeled as channel 2.
306 if (data->type == lm82 && (channel == 1 || channel == 3))
311 if (attr == hwmon_chip_alarms)
316 case hwmon_temp_input:
317 case hwmon_temp_max_alarm:
318 case hwmon_temp_crit_alarm:
320 case hwmon_temp_fault:
326 case hwmon_temp_crit:
340 static const struct hwmon_channel_info * const lm83_info[] = {
341 HWMON_CHANNEL_INFO(chip, HWMON_C_ALARMS),
342 HWMON_CHANNEL_INFO(temp,
343 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
344 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM,
345 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
346 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT,
347 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
348 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT,
349 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
350 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT
355 static const struct hwmon_ops lm83_hwmon_ops = {
356 .is_visible = lm83_is_visible,
361 static const struct hwmon_chip_info lm83_chip_info = {
362 .ops = &lm83_hwmon_ops,
366 /* Return 0 if detection is successful, -ENODEV otherwise */
367 static int lm83_detect(struct i2c_client *client,
368 struct i2c_board_info *info)
370 struct i2c_adapter *adapter = client->adapter;
374 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
378 if ((i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1) & 0xA8) ||
379 (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2) & 0x48) ||
380 (i2c_smbus_read_byte_data(client, LM83_REG_R_CONFIG) & 0x41)) {
381 dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n",
387 man_id = i2c_smbus_read_byte_data(client, LM83_REG_R_MAN_ID);
388 if (man_id != 0x01) /* National Semiconductor */
391 chip_id = i2c_smbus_read_byte_data(client, LM83_REG_R_CHIP_ID);
395 * According to the LM82 datasheet dated March 2013, recent
396 * revisions of LM82 have a die revision of 0x03. This was
397 * confirmed with a real chip. Further details in this revision
398 * of the LM82 datasheet strongly suggest that LM82 is just a
399 * repackaged LM83. It is therefore impossible to distinguish
400 * those chips from LM83, and they will be misdetected as LM83.
408 /* identification failed */
409 dev_dbg(&adapter->dev,
410 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
415 strscpy(info->type, name, I2C_NAME_SIZE);
420 static const struct i2c_device_id lm83_id[] = {
425 MODULE_DEVICE_TABLE(i2c, lm83_id);
427 static int lm83_probe(struct i2c_client *client)
429 struct device *dev = &client->dev;
430 struct device *hwmon_dev;
431 struct lm83_data *data;
433 data = devm_kzalloc(dev, sizeof(struct lm83_data), GFP_KERNEL);
437 data->regmap = devm_regmap_init(dev, NULL, client, &lm83_regmap_config);
438 if (IS_ERR(data->regmap))
439 return PTR_ERR(data->regmap);
441 data->type = i2c_match_id(lm83_id, client)->driver_data;
443 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
444 data, &lm83_chip_info, NULL);
445 return PTR_ERR_OR_ZERO(hwmon_dev);
449 * Driver data (common to all clients)
452 static struct i2c_driver lm83_driver = {
453 .class = I2C_CLASS_HWMON,
459 .detect = lm83_detect,
460 .address_list = normal_i2c,
463 module_i2c_driver(lm83_driver);
465 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
466 MODULE_DESCRIPTION("LM83 driver");
467 MODULE_LICENSE("GPL");