2 * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
4 * Copyright (C) 2006 Corentin LABBE <clabbe.montjoie@gmail.com>
6 * Based on LM83 Driver by Jean Delvare <jdelvare@suse.de>
8 * Give only processor, motherboard temperatures and fan tachs
9 * Very rare chip please let me know if you use it
11 * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation version 2 of the License
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/jiffies.h>
32 #include <linux/i2c.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/hwmon.h>
35 #include <linux/err.h>
36 #include <linux/mutex.h>
42 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43 0x2e, 0x2f, I2C_CLIENT_END
47 * The ADM1029 registers
48 * Manufacturer ID is 0x41 for Analog Devices
51 #define ADM1029_REG_MAN_ID 0x0D
52 #define ADM1029_REG_CHIP_ID 0x0E
53 #define ADM1029_REG_CONFIG 0x01
54 #define ADM1029_REG_NB_FAN_SUPPORT 0x02
56 #define ADM1029_REG_TEMP_DEVICES_INSTALLED 0x06
58 #define ADM1029_REG_LOCAL_TEMP 0xA0
59 #define ADM1029_REG_REMOTE1_TEMP 0xA1
60 #define ADM1029_REG_REMOTE2_TEMP 0xA2
62 #define ADM1029_REG_LOCAL_TEMP_HIGH 0x90
63 #define ADM1029_REG_REMOTE1_TEMP_HIGH 0x91
64 #define ADM1029_REG_REMOTE2_TEMP_HIGH 0x92
66 #define ADM1029_REG_LOCAL_TEMP_LOW 0x98
67 #define ADM1029_REG_REMOTE1_TEMP_LOW 0x99
68 #define ADM1029_REG_REMOTE2_TEMP_LOW 0x9A
70 #define ADM1029_REG_FAN1 0x70
71 #define ADM1029_REG_FAN2 0x71
73 #define ADM1029_REG_FAN1_MIN 0x78
74 #define ADM1029_REG_FAN2_MIN 0x79
76 #define ADM1029_REG_FAN1_CONFIG 0x68
77 #define ADM1029_REG_FAN2_CONFIG 0x69
79 #define TEMP_FROM_REG(val) ((val) * 1000)
81 #define DIV_FROM_REG(val) (1 << (((val) >> 6) - 1))
83 /* Registers to be checked by adm1029_update_device() */
84 static const u8 ADM1029_REG_TEMP[] = {
85 ADM1029_REG_LOCAL_TEMP,
86 ADM1029_REG_REMOTE1_TEMP,
87 ADM1029_REG_REMOTE2_TEMP,
88 ADM1029_REG_LOCAL_TEMP_HIGH,
89 ADM1029_REG_REMOTE1_TEMP_HIGH,
90 ADM1029_REG_REMOTE2_TEMP_HIGH,
91 ADM1029_REG_LOCAL_TEMP_LOW,
92 ADM1029_REG_REMOTE1_TEMP_LOW,
93 ADM1029_REG_REMOTE2_TEMP_LOW,
96 static const u8 ADM1029_REG_FAN[] = {
100 ADM1029_REG_FAN2_MIN,
103 static const u8 ADM1029_REG_FAN_DIV[] = {
104 ADM1029_REG_FAN1_CONFIG,
105 ADM1029_REG_FAN2_CONFIG,
109 * Functions declaration
112 static int adm1029_probe(struct i2c_client *client,
113 const struct i2c_device_id *id);
114 static int adm1029_detect(struct i2c_client *client,
115 struct i2c_board_info *info);
116 static int adm1029_remove(struct i2c_client *client);
117 static struct adm1029_data *adm1029_update_device(struct device *dev);
118 static int adm1029_init_client(struct i2c_client *client);
121 * Driver data (common to all clients)
124 static const struct i2c_device_id adm1029_id[] = {
128 MODULE_DEVICE_TABLE(i2c, adm1029_id);
130 static struct i2c_driver adm1029_driver = {
131 .class = I2C_CLASS_HWMON,
135 .probe = adm1029_probe,
136 .remove = adm1029_remove,
137 .id_table = adm1029_id,
138 .detect = adm1029_detect,
139 .address_list = normal_i2c,
143 * Client data (each client gets its own)
146 struct adm1029_data {
147 struct device *hwmon_dev;
148 struct mutex update_lock;
149 char valid; /* zero until following fields are valid */
150 unsigned long last_updated; /* in jiffies */
152 /* registers values, signed for temperature, unsigned for other stuff */
153 s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
154 u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
155 u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
163 show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
165 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
166 struct adm1029_data *data = adm1029_update_device(dev);
167 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
171 show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
173 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
174 struct adm1029_data *data = adm1029_update_device(dev);
176 if (data->fan[attr->index] == 0
177 || (data->fan_div[attr->index] & 0xC0) == 0
178 || data->fan[attr->index] == 255) {
179 return sprintf(buf, "0\n");
182 val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
183 / data->fan[attr->index];
184 return sprintf(buf, "%d\n", val);
188 show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
190 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
191 struct adm1029_data *data = adm1029_update_device(dev);
192 if ((data->fan_div[attr->index] & 0xC0) == 0)
193 return sprintf(buf, "0\n");
194 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
197 static ssize_t set_fan_div(struct device *dev,
198 struct device_attribute *devattr, const char *buf, size_t count)
200 struct i2c_client *client = to_i2c_client(dev);
201 struct adm1029_data *data = i2c_get_clientdata(client);
202 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
205 int ret = kstrtol(buf, 10, &val);
209 mutex_lock(&data->update_lock);
211 /*Read actual config */
212 reg = i2c_smbus_read_byte_data(client,
213 ADM1029_REG_FAN_DIV[attr->index]);
226 mutex_unlock(&data->update_lock);
227 dev_err(&client->dev,
228 "fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
232 /* Update the value */
233 reg = (reg & 0x3F) | (val << 6);
235 /* Update the cache */
236 data->fan_div[attr->index] = reg;
239 i2c_smbus_write_byte_data(client,
240 ADM1029_REG_FAN_DIV[attr->index], reg);
241 mutex_unlock(&data->update_lock);
247 * Access rights on sysfs. S_IRUGO: Is Readable by User, Group and Others
248 * S_IWUSR: Is Writable by User.
250 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
251 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
252 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
254 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3);
255 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4);
256 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5);
258 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6);
259 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7);
260 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8);
262 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
263 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
265 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2);
266 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3);
268 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
269 show_fan_div, set_fan_div, 0);
270 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
271 show_fan_div, set_fan_div, 1);
273 static struct attribute *adm1029_attributes[] = {
274 &sensor_dev_attr_temp1_input.dev_attr.attr,
275 &sensor_dev_attr_temp1_min.dev_attr.attr,
276 &sensor_dev_attr_temp1_max.dev_attr.attr,
277 &sensor_dev_attr_temp2_input.dev_attr.attr,
278 &sensor_dev_attr_temp2_min.dev_attr.attr,
279 &sensor_dev_attr_temp2_max.dev_attr.attr,
280 &sensor_dev_attr_temp3_input.dev_attr.attr,
281 &sensor_dev_attr_temp3_min.dev_attr.attr,
282 &sensor_dev_attr_temp3_max.dev_attr.attr,
283 &sensor_dev_attr_fan1_input.dev_attr.attr,
284 &sensor_dev_attr_fan2_input.dev_attr.attr,
285 &sensor_dev_attr_fan1_min.dev_attr.attr,
286 &sensor_dev_attr_fan2_min.dev_attr.attr,
287 &sensor_dev_attr_fan1_div.dev_attr.attr,
288 &sensor_dev_attr_fan2_div.dev_attr.attr,
292 static const struct attribute_group adm1029_group = {
293 .attrs = adm1029_attributes,
300 /* Return 0 if detection is successful, -ENODEV otherwise */
301 static int adm1029_detect(struct i2c_client *client,
302 struct i2c_board_info *info)
304 struct i2c_adapter *adapter = client->adapter;
305 u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
307 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
311 * ADM1029 doesn't have CHIP ID, check just MAN ID
312 * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
313 * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
317 man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
318 chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
319 temp_devices_installed = i2c_smbus_read_byte_data(client,
320 ADM1029_REG_TEMP_DEVICES_INSTALLED);
321 nb_fan_support = i2c_smbus_read_byte_data(client,
322 ADM1029_REG_NB_FAN_SUPPORT);
323 /* 0x41 is Analog Devices */
324 if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01
325 || nb_fan_support != 0x03)
328 if ((chip_id & 0xF0) != 0x00) {
330 * There are no "official" CHIP ID, so actually
331 * we use Major/Minor revision for that
333 pr_info("Unknown major revision %x, please let us know\n",
338 strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
343 static int adm1029_probe(struct i2c_client *client,
344 const struct i2c_device_id *id)
346 struct adm1029_data *data;
349 data = devm_kzalloc(&client->dev, sizeof(struct adm1029_data),
354 i2c_set_clientdata(client, data);
355 mutex_init(&data->update_lock);
358 * Initialize the ADM1029 chip
359 * Check config register
361 if (adm1029_init_client(client) == 0)
364 /* Register sysfs hooks */
365 err = sysfs_create_group(&client->dev.kobj, &adm1029_group);
369 data->hwmon_dev = hwmon_device_register(&client->dev);
370 if (IS_ERR(data->hwmon_dev)) {
371 err = PTR_ERR(data->hwmon_dev);
372 goto exit_remove_files;
378 sysfs_remove_group(&client->dev.kobj, &adm1029_group);
382 static int adm1029_init_client(struct i2c_client *client)
385 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
386 if ((config & 0x10) == 0) {
387 i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
391 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
392 if ((config & 0x10) == 0) {
393 dev_err(&client->dev, "Initialization failed!\n");
399 static int adm1029_remove(struct i2c_client *client)
401 struct adm1029_data *data = i2c_get_clientdata(client);
403 hwmon_device_unregister(data->hwmon_dev);
404 sysfs_remove_group(&client->dev.kobj, &adm1029_group);
410 * function that update the status of the chips (temperature for example)
412 static struct adm1029_data *adm1029_update_device(struct device *dev)
414 struct i2c_client *client = to_i2c_client(dev);
415 struct adm1029_data *data = i2c_get_clientdata(client);
417 mutex_lock(&data->update_lock);
419 * Use the "cache" Luke, don't recheck values
420 * if there are already checked not a long time later
422 if (time_after(jiffies, data->last_updated + HZ * 2)
426 dev_dbg(&client->dev, "Updating adm1029 data\n");
428 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
430 i2c_smbus_read_byte_data(client,
431 ADM1029_REG_TEMP[nr]);
433 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
435 i2c_smbus_read_byte_data(client,
436 ADM1029_REG_FAN[nr]);
438 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
440 i2c_smbus_read_byte_data(client,
441 ADM1029_REG_FAN_DIV[nr]);
444 data->last_updated = jiffies;
448 mutex_unlock(&data->update_lock);
453 module_i2c_driver(adm1029_driver);
455 MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
456 MODULE_DESCRIPTION("adm1029 driver");
457 MODULE_LICENSE("GPL v2");