1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
5 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
6 * Philip Edelbrock <phil@netroedge.com>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/jiffies.h>
13 #include <linux/i2c.h>
14 #include <linux/hwmon.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
20 /* Addresses to scan */
21 static const unsigned short normal_i2c[] = {
22 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
25 adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066 };
27 /* adm1021 constants specified below */
29 /* The adm1021 registers */
32 #define ADM1021_REG_TEMP(nr) (nr)
33 #define ADM1021_REG_STATUS 0x02
34 /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
35 #define ADM1021_REG_MAN_ID 0xFE
36 /* ADM1021 = 0x0X, ADM1023 = 0x3X */
37 #define ADM1021_REG_DEV_ID 0xFF
38 /* These use different addresses for reading/writing */
39 #define ADM1021_REG_CONFIG_R 0x03
40 #define ADM1021_REG_CONFIG_W 0x09
41 #define ADM1021_REG_CONV_RATE_R 0x04
42 #define ADM1021_REG_CONV_RATE_W 0x0A
43 /* These are for the ADM1023's additional precision on the remote temp sensor */
44 #define ADM1023_REG_REM_TEMP_PREC 0x10
45 #define ADM1023_REG_REM_OFFSET 0x11
46 #define ADM1023_REG_REM_OFFSET_PREC 0x12
47 #define ADM1023_REG_REM_TOS_PREC 0x13
48 #define ADM1023_REG_REM_THYST_PREC 0x14
51 #define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr))
52 #define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr))
53 #define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr))
54 #define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr))
56 #define ADM1021_REG_ONESHOT 0x0F
61 * Note: Even though I left the low and high limits named os and hyst,
62 * they don't quite work like a thermostat the way the LM75 does. I.e.,
63 * a lower temp than THYST actually triggers an alarm instead of
64 * clearing it. Weird, ey? --Phil
67 /* Each client has this additional data */
69 struct i2c_client *client;
72 const struct attribute_group *groups[3];
74 struct mutex update_lock;
75 bool valid; /* true if following fields are valid */
76 char low_power; /* !=0 if device in low power mode */
77 unsigned long last_updated; /* In jiffies */
79 int temp_max[2]; /* Register values */
83 /* Special values for ADM1023 only */
84 u8 remote_temp_offset;
85 u8 remote_temp_offset_prec;
88 /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
89 static bool read_only;
91 static struct adm1021_data *adm1021_update_device(struct device *dev)
93 struct adm1021_data *data = dev_get_drvdata(dev);
94 struct i2c_client *client = data->client;
96 mutex_lock(&data->update_lock);
98 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
102 dev_dbg(dev, "Starting adm1021 update\n");
104 for (i = 0; i < 2; i++) {
105 data->temp[i] = 1000 *
106 (s8) i2c_smbus_read_byte_data(
107 client, ADM1021_REG_TEMP(i));
108 data->temp_max[i] = 1000 *
109 (s8) i2c_smbus_read_byte_data(
110 client, ADM1021_REG_TOS_R(i));
111 if (data->type != lm84) {
112 data->temp_min[i] = 1000 *
113 (s8) i2c_smbus_read_byte_data(client,
114 ADM1021_REG_THYST_R(i));
117 data->alarms = i2c_smbus_read_byte_data(client,
118 ADM1021_REG_STATUS) & 0x7c;
119 if (data->type == adm1023) {
121 * The ADM1023 provides 3 extra bits of precision for
122 * the remote sensor in extra registers.
124 data->temp[1] += 125 * (i2c_smbus_read_byte_data(
125 client, ADM1023_REG_REM_TEMP_PREC) >> 5);
126 data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
127 client, ADM1023_REG_REM_TOS_PREC) >> 5);
128 data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
129 client, ADM1023_REG_REM_THYST_PREC) >> 5);
130 data->remote_temp_offset =
131 i2c_smbus_read_byte_data(client,
132 ADM1023_REG_REM_OFFSET);
133 data->remote_temp_offset_prec =
134 i2c_smbus_read_byte_data(client,
135 ADM1023_REG_REM_OFFSET_PREC);
137 data->last_updated = jiffies;
141 mutex_unlock(&data->update_lock);
146 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
149 int index = to_sensor_dev_attr(devattr)->index;
150 struct adm1021_data *data = adm1021_update_device(dev);
152 return sprintf(buf, "%d\n", data->temp[index]);
155 static ssize_t temp_max_show(struct device *dev,
156 struct device_attribute *devattr, char *buf)
158 int index = to_sensor_dev_attr(devattr)->index;
159 struct adm1021_data *data = adm1021_update_device(dev);
161 return sprintf(buf, "%d\n", data->temp_max[index]);
164 static ssize_t temp_min_show(struct device *dev,
165 struct device_attribute *devattr, char *buf)
167 int index = to_sensor_dev_attr(devattr)->index;
168 struct adm1021_data *data = adm1021_update_device(dev);
170 return sprintf(buf, "%d\n", data->temp_min[index]);
173 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
176 int index = to_sensor_dev_attr(attr)->index;
177 struct adm1021_data *data = adm1021_update_device(dev);
178 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
181 static ssize_t alarms_show(struct device *dev,
182 struct device_attribute *attr,
185 struct adm1021_data *data = adm1021_update_device(dev);
186 return sprintf(buf, "%u\n", data->alarms);
189 static ssize_t temp_max_store(struct device *dev,
190 struct device_attribute *devattr,
191 const char *buf, size_t count)
193 int index = to_sensor_dev_attr(devattr)->index;
194 struct adm1021_data *data = dev_get_drvdata(dev);
195 struct i2c_client *client = data->client;
199 err = kstrtol(buf, 10, &temp);
204 mutex_lock(&data->update_lock);
205 reg_val = clamp_val(temp, -128, 127);
206 data->temp_max[index] = reg_val * 1000;
208 i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
210 mutex_unlock(&data->update_lock);
215 static ssize_t temp_min_store(struct device *dev,
216 struct device_attribute *devattr,
217 const char *buf, size_t count)
219 int index = to_sensor_dev_attr(devattr)->index;
220 struct adm1021_data *data = dev_get_drvdata(dev);
221 struct i2c_client *client = data->client;
225 err = kstrtol(buf, 10, &temp);
230 mutex_lock(&data->update_lock);
231 reg_val = clamp_val(temp, -128, 127);
232 data->temp_min[index] = reg_val * 1000;
234 i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
236 mutex_unlock(&data->update_lock);
241 static ssize_t low_power_show(struct device *dev,
242 struct device_attribute *devattr, char *buf)
244 struct adm1021_data *data = adm1021_update_device(dev);
245 return sprintf(buf, "%d\n", data->low_power);
248 static ssize_t low_power_store(struct device *dev,
249 struct device_attribute *devattr,
250 const char *buf, size_t count)
252 struct adm1021_data *data = dev_get_drvdata(dev);
253 struct i2c_client *client = data->client;
258 err = kstrtoul(buf, 10, &val);
261 low_power = val != 0;
263 mutex_lock(&data->update_lock);
264 if (low_power != data->low_power) {
265 int config = i2c_smbus_read_byte_data(
266 client, ADM1021_REG_CONFIG_R);
267 data->low_power = low_power;
268 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
269 (config & 0xBF) | (low_power << 6));
271 mutex_unlock(&data->update_lock);
277 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
278 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
279 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
280 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
281 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
282 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
283 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6);
284 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 5);
285 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4);
286 static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, alarm, 3);
287 static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2);
289 static DEVICE_ATTR_RO(alarms);
290 static DEVICE_ATTR_RW(low_power);
292 static struct attribute *adm1021_attributes[] = {
293 &sensor_dev_attr_temp1_max.dev_attr.attr,
294 &sensor_dev_attr_temp1_input.dev_attr.attr,
295 &sensor_dev_attr_temp2_max.dev_attr.attr,
296 &sensor_dev_attr_temp2_input.dev_attr.attr,
297 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
298 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
299 &sensor_dev_attr_temp2_fault.dev_attr.attr,
300 &dev_attr_alarms.attr,
301 &dev_attr_low_power.attr,
305 static const struct attribute_group adm1021_group = {
306 .attrs = adm1021_attributes,
309 static struct attribute *adm1021_min_attributes[] = {
310 &sensor_dev_attr_temp1_min.dev_attr.attr,
311 &sensor_dev_attr_temp2_min.dev_attr.attr,
312 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
313 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
317 static const struct attribute_group adm1021_min_group = {
318 .attrs = adm1021_min_attributes,
321 /* Return 0 if detection is successful, -ENODEV otherwise */
322 static int adm1021_detect(struct i2c_client *client,
323 struct i2c_board_info *info)
325 struct i2c_adapter *adapter = client->adapter;
326 const char *type_name;
327 int reg, conv_rate, status, config, man_id, dev_id;
329 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
330 pr_debug("detect failed, smbus byte data not supported!\n");
334 status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
335 conv_rate = i2c_smbus_read_byte_data(client,
336 ADM1021_REG_CONV_RATE_R);
337 config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
339 /* Check unused bits */
340 if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
341 pr_debug("detect failed, chip not detected!\n");
345 /* Determine the chip type. */
346 man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
347 dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
349 if (man_id < 0 || dev_id < 0)
352 if (man_id == 0x4d && dev_id == 0x01) {
354 * dev_id 0x01 matches MAX6680, MAX6695, MAX6696, and possibly
355 * others. Read register which is unsupported on MAX1617 but
356 * exists on all those chips and compare with the dev_id
357 * register. If it matches, it may be a MAX1617A.
359 reg = i2c_smbus_read_byte_data(client,
360 ADM1023_REG_REM_TEMP_PREC);
363 type_name = "max1617a";
364 } else if (man_id == 0x41) {
365 if ((dev_id & 0xF0) == 0x30)
366 type_name = "adm1023";
367 else if ((dev_id & 0xF0) == 0x00)
368 type_name = "adm1021";
371 } else if (man_id == 0x49)
372 type_name = "thmc10";
373 else if (man_id == 0x23)
374 type_name = "gl523sm";
375 else if (man_id == 0x54)
376 type_name = "mc1066";
378 int lte, rte, lhi, rhi, llo, rlo;
380 /* extra checks for LM84 and MAX1617 to avoid misdetections */
382 llo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(0));
383 rlo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(1));
385 /* fail if any of the additional register reads failed */
386 if (llo < 0 || rlo < 0)
389 lte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(0));
390 rte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(1));
391 lhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(0));
392 rhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(1));
395 * Fail for negative temperatures and negative high limits.
396 * This check also catches read errors on the tested registers.
398 if ((s8)lte < 0 || (s8)rte < 0 || (s8)lhi < 0 || (s8)rhi < 0)
401 /* fail if all registers hold the same value */
402 if (lte == rte && lte == lhi && lte == rhi && lte == llo
407 * LM84 Mfr ID is in a different place,
408 * and it has more unused bits. Registers at 0xfe and 0xff
409 * are undefined and return the most recently read value,
410 * here the value of the configuration register.
412 if (conv_rate == 0x00
413 && man_id == config && dev_id == config
414 && (config & 0x7F) == 0x00
415 && (status & 0xAB) == 0x00) {
418 if ((config & 0x3f) || (status & 0x03))
420 /* fail if low limits are larger than high limits */
421 if ((s8)llo > lhi || (s8)rlo > rhi)
423 type_name = "max1617";
427 pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n",
428 type_name, i2c_adapter_id(adapter), client->addr);
429 strscpy(info->type, type_name, I2C_NAME_SIZE);
434 static void adm1021_init_client(struct i2c_client *client)
436 /* Enable ADC and disable suspend mode */
437 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
438 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
439 /* Set Conversion rate to 1/sec (this can be tinkered with) */
440 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
443 static const struct i2c_device_id adm1021_id[];
445 static int adm1021_probe(struct i2c_client *client)
447 struct device *dev = &client->dev;
448 struct adm1021_data *data;
449 struct device *hwmon_dev;
451 data = devm_kzalloc(dev, sizeof(struct adm1021_data), GFP_KERNEL);
455 data->client = client;
456 data->type = i2c_match_id(adm1021_id, client)->driver_data;
457 mutex_init(&data->update_lock);
459 /* Initialize the ADM1021 chip */
460 if (data->type != lm84 && !read_only)
461 adm1021_init_client(client);
463 data->groups[0] = &adm1021_group;
464 if (data->type != lm84)
465 data->groups[1] = &adm1021_min_group;
467 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
470 return PTR_ERR_OR_ZERO(hwmon_dev);
473 static const struct i2c_device_id adm1021_id[] = {
474 { "adm1021", adm1021 },
475 { "adm1023", adm1023 },
476 { "max1617", max1617 },
477 { "max1617a", max1617a },
478 { "thmc10", thmc10 },
480 { "gl523sm", gl523sm },
481 { "mc1066", mc1066 },
484 MODULE_DEVICE_TABLE(i2c, adm1021_id);
486 static struct i2c_driver adm1021_driver = {
487 .class = I2C_CLASS_HWMON,
491 .probe_new = adm1021_probe,
492 .id_table = adm1021_id,
493 .detect = adm1021_detect,
494 .address_list = normal_i2c,
497 module_i2c_driver(adm1021_driver);
499 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
500 "Philip Edelbrock <phil@netroedge.com>");
501 MODULE_DESCRIPTION("adm1021 driver");
502 MODULE_LICENSE("GPL");
504 module_param(read_only, bool, 0);
505 MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");