1 // SPDX-License-Identifier: GPL-2.0
3 * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
5 * Copyright (C) 2006 Corentin LABBE <clabbe.montjoie@gmail.com>
7 * Based on LM83 Driver by Jean Delvare <jdelvare@suse.de>
9 * Give only processor, motherboard temperatures and fan tachs
10 * Very rare chip please let me know if you use it
12 * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation version 2 of the License
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/i2c.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/hwmon.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
39 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
40 0x2e, 0x2f, I2C_CLIENT_END
44 * The ADM1029 registers
45 * Manufacturer ID is 0x41 for Analog Devices
48 #define ADM1029_REG_MAN_ID 0x0D
49 #define ADM1029_REG_CHIP_ID 0x0E
50 #define ADM1029_REG_CONFIG 0x01
51 #define ADM1029_REG_NB_FAN_SUPPORT 0x02
53 #define ADM1029_REG_TEMP_DEVICES_INSTALLED 0x06
55 #define ADM1029_REG_LOCAL_TEMP 0xA0
56 #define ADM1029_REG_REMOTE1_TEMP 0xA1
57 #define ADM1029_REG_REMOTE2_TEMP 0xA2
59 #define ADM1029_REG_LOCAL_TEMP_HIGH 0x90
60 #define ADM1029_REG_REMOTE1_TEMP_HIGH 0x91
61 #define ADM1029_REG_REMOTE2_TEMP_HIGH 0x92
63 #define ADM1029_REG_LOCAL_TEMP_LOW 0x98
64 #define ADM1029_REG_REMOTE1_TEMP_LOW 0x99
65 #define ADM1029_REG_REMOTE2_TEMP_LOW 0x9A
67 #define ADM1029_REG_FAN1 0x70
68 #define ADM1029_REG_FAN2 0x71
70 #define ADM1029_REG_FAN1_MIN 0x78
71 #define ADM1029_REG_FAN2_MIN 0x79
73 #define ADM1029_REG_FAN1_CONFIG 0x68
74 #define ADM1029_REG_FAN2_CONFIG 0x69
76 #define TEMP_FROM_REG(val) ((val) * 1000)
78 #define DIV_FROM_REG(val) (1 << (((val) >> 6) - 1))
80 /* Registers to be checked by adm1029_update_device() */
81 static const u8 ADM1029_REG_TEMP[] = {
82 ADM1029_REG_LOCAL_TEMP,
83 ADM1029_REG_REMOTE1_TEMP,
84 ADM1029_REG_REMOTE2_TEMP,
85 ADM1029_REG_LOCAL_TEMP_HIGH,
86 ADM1029_REG_REMOTE1_TEMP_HIGH,
87 ADM1029_REG_REMOTE2_TEMP_HIGH,
88 ADM1029_REG_LOCAL_TEMP_LOW,
89 ADM1029_REG_REMOTE1_TEMP_LOW,
90 ADM1029_REG_REMOTE2_TEMP_LOW,
93 static const u8 ADM1029_REG_FAN[] = {
100 static const u8 ADM1029_REG_FAN_DIV[] = {
101 ADM1029_REG_FAN1_CONFIG,
102 ADM1029_REG_FAN2_CONFIG,
106 * Client data (each client gets its own)
109 struct adm1029_data {
110 struct i2c_client *client;
111 struct mutex update_lock; /* protect register access */
112 char valid; /* zero until following fields are valid */
113 unsigned long last_updated; /* in jiffies */
115 /* registers values, signed for temperature, unsigned for other stuff */
116 s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
117 u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
118 u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
122 * function that update the status of the chips (temperature for example)
124 static struct adm1029_data *adm1029_update_device(struct device *dev)
126 struct adm1029_data *data = dev_get_drvdata(dev);
127 struct i2c_client *client = data->client;
129 mutex_lock(&data->update_lock);
131 * Use the "cache" Luke, don't recheck values
132 * if there are already checked not a long time later
134 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
137 dev_dbg(&client->dev, "Updating adm1029 data\n");
139 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
141 i2c_smbus_read_byte_data(client,
142 ADM1029_REG_TEMP[nr]);
144 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
146 i2c_smbus_read_byte_data(client,
147 ADM1029_REG_FAN[nr]);
149 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
151 i2c_smbus_read_byte_data(client,
152 ADM1029_REG_FAN_DIV[nr]);
155 data->last_updated = jiffies;
159 mutex_unlock(&data->update_lock);
169 show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
171 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
172 struct adm1029_data *data = adm1029_update_device(dev);
174 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
178 show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
180 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
181 struct adm1029_data *data = adm1029_update_device(dev);
184 if (data->fan[attr->index] == 0 ||
185 (data->fan_div[attr->index] & 0xC0) == 0 ||
186 data->fan[attr->index] == 255) {
187 return sprintf(buf, "0\n");
190 val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
191 / data->fan[attr->index];
192 return sprintf(buf, "%d\n", val);
196 show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
198 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
199 struct adm1029_data *data = adm1029_update_device(dev);
201 if ((data->fan_div[attr->index] & 0xC0) == 0)
202 return sprintf(buf, "0\n");
203 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
206 static ssize_t set_fan_div(struct device *dev, struct device_attribute *devattr,
207 const char *buf, size_t count)
209 struct adm1029_data *data = dev_get_drvdata(dev);
210 struct i2c_client *client = data->client;
211 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
214 int ret = kstrtol(buf, 10, &val);
219 mutex_lock(&data->update_lock);
221 /*Read actual config */
222 reg = i2c_smbus_read_byte_data(client,
223 ADM1029_REG_FAN_DIV[attr->index]);
236 mutex_unlock(&data->update_lock);
237 dev_err(&client->dev,
238 "fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
242 /* Update the value */
243 reg = (reg & 0x3F) | (val << 6);
245 /* Update the cache */
246 data->fan_div[attr->index] = reg;
249 i2c_smbus_write_byte_data(client,
250 ADM1029_REG_FAN_DIV[attr->index], reg);
251 mutex_unlock(&data->update_lock);
256 /* Access rights on sysfs. */
257 static SENSOR_DEVICE_ATTR(temp1_input, 0444, show_temp, NULL, 0);
258 static SENSOR_DEVICE_ATTR(temp2_input, 0444, show_temp, NULL, 1);
259 static SENSOR_DEVICE_ATTR(temp3_input, 0444, show_temp, NULL, 2);
261 static SENSOR_DEVICE_ATTR(temp1_max, 0444, show_temp, NULL, 3);
262 static SENSOR_DEVICE_ATTR(temp2_max, 0444, show_temp, NULL, 4);
263 static SENSOR_DEVICE_ATTR(temp3_max, 0444, show_temp, NULL, 5);
265 static SENSOR_DEVICE_ATTR(temp1_min, 0444, show_temp, NULL, 6);
266 static SENSOR_DEVICE_ATTR(temp2_min, 0444, show_temp, NULL, 7);
267 static SENSOR_DEVICE_ATTR(temp3_min, 0444, show_temp, NULL, 8);
269 static SENSOR_DEVICE_ATTR(fan1_input, 0444, show_fan, NULL, 0);
270 static SENSOR_DEVICE_ATTR(fan2_input, 0444, show_fan, NULL, 1);
272 static SENSOR_DEVICE_ATTR(fan1_min, 0444, show_fan, NULL, 2);
273 static SENSOR_DEVICE_ATTR(fan2_min, 0444, show_fan, NULL, 3);
275 static SENSOR_DEVICE_ATTR(fan1_div, 0644, show_fan_div, set_fan_div, 0);
276 static SENSOR_DEVICE_ATTR(fan2_div, 0644, show_fan_div, set_fan_div, 1);
278 static struct attribute *adm1029_attrs[] = {
279 &sensor_dev_attr_temp1_input.dev_attr.attr,
280 &sensor_dev_attr_temp1_min.dev_attr.attr,
281 &sensor_dev_attr_temp1_max.dev_attr.attr,
282 &sensor_dev_attr_temp2_input.dev_attr.attr,
283 &sensor_dev_attr_temp2_min.dev_attr.attr,
284 &sensor_dev_attr_temp2_max.dev_attr.attr,
285 &sensor_dev_attr_temp3_input.dev_attr.attr,
286 &sensor_dev_attr_temp3_min.dev_attr.attr,
287 &sensor_dev_attr_temp3_max.dev_attr.attr,
288 &sensor_dev_attr_fan1_input.dev_attr.attr,
289 &sensor_dev_attr_fan2_input.dev_attr.attr,
290 &sensor_dev_attr_fan1_min.dev_attr.attr,
291 &sensor_dev_attr_fan2_min.dev_attr.attr,
292 &sensor_dev_attr_fan1_div.dev_attr.attr,
293 &sensor_dev_attr_fan2_div.dev_attr.attr,
297 ATTRIBUTE_GROUPS(adm1029);
303 /* Return 0 if detection is successful, -ENODEV otherwise */
304 static int adm1029_detect(struct i2c_client *client,
305 struct i2c_board_info *info)
307 struct i2c_adapter *adapter = client->adapter;
308 u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
310 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
314 * ADM1029 doesn't have CHIP ID, check just MAN ID
315 * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
316 * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
320 man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
321 chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
322 temp_devices_installed = i2c_smbus_read_byte_data(client,
323 ADM1029_REG_TEMP_DEVICES_INSTALLED);
324 nb_fan_support = i2c_smbus_read_byte_data(client,
325 ADM1029_REG_NB_FAN_SUPPORT);
326 /* 0x41 is Analog Devices */
327 if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01 ||
328 nb_fan_support != 0x03)
331 if ((chip_id & 0xF0) != 0x00) {
333 * There are no "official" CHIP ID, so actually
334 * we use Major/Minor revision for that
336 pr_info("Unknown major revision %x, please let us know\n",
341 strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
346 static int adm1029_init_client(struct i2c_client *client)
350 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
351 if ((config & 0x10) == 0) {
352 i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
356 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
357 if ((config & 0x10) == 0) {
358 dev_err(&client->dev, "Initialization failed!\n");
364 static int adm1029_probe(struct i2c_client *client,
365 const struct i2c_device_id *id)
367 struct device *dev = &client->dev;
368 struct adm1029_data *data;
369 struct device *hwmon_dev;
371 data = devm_kzalloc(dev, sizeof(struct adm1029_data), GFP_KERNEL);
375 data->client = client;
376 mutex_init(&data->update_lock);
379 * Initialize the ADM1029 chip
380 * Check config register
382 if (adm1029_init_client(client) == 0)
385 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
388 return PTR_ERR_OR_ZERO(hwmon_dev);
391 static const struct i2c_device_id adm1029_id[] = {
395 MODULE_DEVICE_TABLE(i2c, adm1029_id);
397 static struct i2c_driver adm1029_driver = {
398 .class = I2C_CLASS_HWMON,
402 .probe = adm1029_probe,
403 .id_table = adm1029_id,
404 .detect = adm1029_detect,
405 .address_list = normal_i2c,
408 module_i2c_driver(adm1029_driver);
410 MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
411 MODULE_DESCRIPTION("adm1029 driver");
412 MODULE_LICENSE("GPL v2");