blob: 9ee5e066423bee462f17dd6fc81979b618b25e20 [file] [log] [blame]
Corentin Labbecae2caa2007-02-14 21:15:04 +01001/*
2 * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
3 *
4 * Copyright (C) 2006 Corentin LABBE <corentin.labbe@geomatys.fr>
5 *
6 * Based on LM83 Driver by Jean Delvare <khali@linux-fr.org>
7 *
8 * Give only processor, motherboard temperatures and fan tachs
9 * Very rare chip please let me know if you use it
10 *
11 * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf
12 *
13 *
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
17 *
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.
22 *
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.
26 */
27
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>
37
38/*
39 * Addresses to scan
40 */
41
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050042static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43 0x2e, 0x2f, I2C_CLIENT_END
Corentin Labbecae2caa2007-02-14 21:15:04 +010044};
45
46/*
Corentin Labbecae2caa2007-02-14 21:15:04 +010047 * The ADM1029 registers
48 * Manufacturer ID is 0x41 for Analog Devices
49 */
50
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
55
56#define ADM1029_REG_TEMP_DEVICES_INSTALLED 0x06
57
58#define ADM1029_REG_LOCAL_TEMP 0xA0
59#define ADM1029_REG_REMOTE1_TEMP 0xA1
60#define ADM1029_REG_REMOTE2_TEMP 0xA2
61
62#define ADM1029_REG_LOCAL_TEMP_HIGH 0x90
63#define ADM1029_REG_REMOTE1_TEMP_HIGH 0x91
64#define ADM1029_REG_REMOTE2_TEMP_HIGH 0x92
65
66#define ADM1029_REG_LOCAL_TEMP_LOW 0x98
67#define ADM1029_REG_REMOTE1_TEMP_LOW 0x99
68#define ADM1029_REG_REMOTE2_TEMP_LOW 0x9A
69
70#define ADM1029_REG_FAN1 0x70
71#define ADM1029_REG_FAN2 0x71
72
73#define ADM1029_REG_FAN1_MIN 0x78
74#define ADM1029_REG_FAN2_MIN 0x79
75
76#define ADM1029_REG_FAN1_CONFIG 0x68
77#define ADM1029_REG_FAN2_CONFIG 0x69
78
79#define TEMP_FROM_REG(val) ((val) * 1000)
80
Frans Meulenbroeks08f50902012-01-08 19:34:18 +010081#define DIV_FROM_REG(val) (1 << (((val) >> 6) - 1))
Corentin Labbecae2caa2007-02-14 21:15:04 +010082
83/* Registers to be checked by adm1029_update_device() */
84static 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,
94};
95
96static const u8 ADM1029_REG_FAN[] = {
97 ADM1029_REG_FAN1,
98 ADM1029_REG_FAN2,
99 ADM1029_REG_FAN1_MIN,
100 ADM1029_REG_FAN2_MIN,
101};
102
103static const u8 ADM1029_REG_FAN_DIV[] = {
104 ADM1029_REG_FAN1_CONFIG,
105 ADM1029_REG_FAN2_CONFIG,
106};
107
108/*
109 * Functions declaration
110 */
111
Jean Delvare9c97fb42008-07-16 19:30:09 +0200112static int adm1029_probe(struct i2c_client *client,
113 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100114static int adm1029_detect(struct i2c_client *client,
Jean Delvare9c97fb42008-07-16 19:30:09 +0200115 struct i2c_board_info *info);
116static int adm1029_remove(struct i2c_client *client);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100117static struct adm1029_data *adm1029_update_device(struct device *dev);
118static int adm1029_init_client(struct i2c_client *client);
119
120/*
121 * Driver data (common to all clients)
122 */
123
Jean Delvare9c97fb42008-07-16 19:30:09 +0200124static const struct i2c_device_id adm1029_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100125 { "adm1029", 0 },
Jean Delvare9c97fb42008-07-16 19:30:09 +0200126 { }
127};
128MODULE_DEVICE_TABLE(i2c, adm1029_id);
129
Corentin Labbecae2caa2007-02-14 21:15:04 +0100130static struct i2c_driver adm1029_driver = {
Jean Delvare9c97fb42008-07-16 19:30:09 +0200131 .class = I2C_CLASS_HWMON,
Corentin Labbecae2caa2007-02-14 21:15:04 +0100132 .driver = {
133 .name = "adm1029",
134 },
Jean Delvare9c97fb42008-07-16 19:30:09 +0200135 .probe = adm1029_probe,
136 .remove = adm1029_remove,
137 .id_table = adm1029_id,
138 .detect = adm1029_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100139 .address_list = normal_i2c,
Corentin Labbecae2caa2007-02-14 21:15:04 +0100140};
141
142/*
143 * Client data (each client gets its own)
144 */
145
146struct adm1029_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700147 struct device *hwmon_dev;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100148 struct mutex update_lock;
149 char valid; /* zero until following fields are valid */
150 unsigned long last_updated; /* in jiffies */
151
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)];
156};
157
158/*
159 * Sysfs stuff
160 */
161
162static ssize_t
163show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
164{
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]));
168}
169
170static ssize_t
171show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
172{
173 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
174 struct adm1029_data *data = adm1029_update_device(dev);
175 u16 val;
Corentin Labbe366716e2008-10-17 17:51:20 +0200176 if (data->fan[attr->index] == 0
177 || (data->fan_div[attr->index] & 0xC0) == 0
Corentin Labbecae2caa2007-02-14 21:15:04 +0100178 || data->fan[attr->index] == 255) {
179 return sprintf(buf, "0\n");
180 }
181
182 val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
183 / data->fan[attr->index];
184 return sprintf(buf, "%d\n", val);
185}
186
187static ssize_t
188show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
189{
190 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
191 struct adm1029_data *data = adm1029_update_device(dev);
Corentin Labbe366716e2008-10-17 17:51:20 +0200192 if ((data->fan_div[attr->index] & 0xC0) == 0)
Corentin Labbecae2caa2007-02-14 21:15:04 +0100193 return sprintf(buf, "0\n");
194 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
195}
196
197static ssize_t set_fan_div(struct device *dev,
198 struct device_attribute *devattr, const char *buf, size_t count)
199{
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);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100203 u8 reg;
Frans Meulenbroeks08f50902012-01-08 19:34:18 +0100204 long val;
205 int ret = kstrtol(buf, 10, &val);
206 if (ret < 0)
207 return ret;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100208
209 mutex_lock(&data->update_lock);
210
211 /*Read actual config */
212 reg = i2c_smbus_read_byte_data(client,
213 ADM1029_REG_FAN_DIV[attr->index]);
214
215 switch (val) {
216 case 1:
217 val = 1;
218 break;
219 case 2:
220 val = 2;
221 break;
222 case 4:
223 val = 3;
224 break;
225 default:
226 mutex_unlock(&data->update_lock);
Guenter Roeckb55f3752013-01-10 10:01:24 -0800227 dev_err(&client->dev,
228 "fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
229 val);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100230 return -EINVAL;
231 }
232 /* Update the value */
233 reg = (reg & 0x3F) | (val << 6);
234
235 /* Write value */
236 i2c_smbus_write_byte_data(client,
237 ADM1029_REG_FAN_DIV[attr->index], reg);
238 mutex_unlock(&data->update_lock);
239
240 return count;
241}
242
243/*
Guenter Roeck94b991d2012-01-19 11:02:14 -0800244 * Access rights on sysfs. S_IRUGO: Is Readable by User, Group and Others
245 * S_IWUSR: Is Writable by User.
246 */
Corentin Labbecae2caa2007-02-14 21:15:04 +0100247static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
248static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
249static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
250
251static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3);
252static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4);
253static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5);
254
255static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6);
256static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7);
257static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8);
258
259static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
260static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
261
262static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2);
263static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3);
264
265static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
266 show_fan_div, set_fan_div, 0);
267static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
268 show_fan_div, set_fan_div, 1);
269
270static struct attribute *adm1029_attributes[] = {
271 &sensor_dev_attr_temp1_input.dev_attr.attr,
272 &sensor_dev_attr_temp1_min.dev_attr.attr,
273 &sensor_dev_attr_temp1_max.dev_attr.attr,
274 &sensor_dev_attr_temp2_input.dev_attr.attr,
275 &sensor_dev_attr_temp2_min.dev_attr.attr,
276 &sensor_dev_attr_temp2_max.dev_attr.attr,
277 &sensor_dev_attr_temp3_input.dev_attr.attr,
278 &sensor_dev_attr_temp3_min.dev_attr.attr,
279 &sensor_dev_attr_temp3_max.dev_attr.attr,
280 &sensor_dev_attr_fan1_input.dev_attr.attr,
281 &sensor_dev_attr_fan2_input.dev_attr.attr,
282 &sensor_dev_attr_fan1_min.dev_attr.attr,
283 &sensor_dev_attr_fan2_min.dev_attr.attr,
284 &sensor_dev_attr_fan1_div.dev_attr.attr,
285 &sensor_dev_attr_fan2_div.dev_attr.attr,
286 NULL
287};
288
289static const struct attribute_group adm1029_group = {
290 .attrs = adm1029_attributes,
291};
292
293/*
294 * Real code
295 */
296
Jean Delvare9c97fb42008-07-16 19:30:09 +0200297/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100298static int adm1029_detect(struct i2c_client *client,
Jean Delvare9c97fb42008-07-16 19:30:09 +0200299 struct i2c_board_info *info)
Corentin Labbecae2caa2007-02-14 21:15:04 +0100300{
Jean Delvare9c97fb42008-07-16 19:30:09 +0200301 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100302 u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100303
Corentin Labbecae2caa2007-02-14 21:15:04 +0100304 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9c97fb42008-07-16 19:30:09 +0200305 return -ENODEV;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100306
Guenter Roeck94b991d2012-01-19 11:02:14 -0800307 /*
308 * ADM1029 doesn't have CHIP ID, check just MAN ID
Corentin Labbecae2caa2007-02-14 21:15:04 +0100309 * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
310 * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
311 * documented
312 */
313
Jean Delvare52df6442009-12-09 20:35:57 +0100314 man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
315 chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
316 temp_devices_installed = i2c_smbus_read_byte_data(client,
Corentin Labbecae2caa2007-02-14 21:15:04 +0100317 ADM1029_REG_TEMP_DEVICES_INSTALLED);
Jean Delvare52df6442009-12-09 20:35:57 +0100318 nb_fan_support = i2c_smbus_read_byte_data(client,
Corentin Labbecae2caa2007-02-14 21:15:04 +0100319 ADM1029_REG_NB_FAN_SUPPORT);
Jean Delvare52df6442009-12-09 20:35:57 +0100320 /* 0x41 is Analog Devices */
321 if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01
322 || nb_fan_support != 0x03)
323 return -ENODEV;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100324
Jean Delvare52df6442009-12-09 20:35:57 +0100325 if ((chip_id & 0xF0) != 0x00) {
Guenter Roeck94b991d2012-01-19 11:02:14 -0800326 /*
327 * There are no "official" CHIP ID, so actually
328 * we use Major/Minor revision for that
329 */
Guenter Roeckb55f3752013-01-10 10:01:24 -0800330 pr_info("Unknown major revision %x, please let us know\n",
331 chip_id);
Jean Delvare52df6442009-12-09 20:35:57 +0100332 return -ENODEV;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100333 }
Jean Delvare52df6442009-12-09 20:35:57 +0100334
Jean Delvare9c97fb42008-07-16 19:30:09 +0200335 strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100336
Jean Delvare9c97fb42008-07-16 19:30:09 +0200337 return 0;
338}
339
340static int adm1029_probe(struct i2c_client *client,
341 const struct i2c_device_id *id)
342{
343 struct adm1029_data *data;
344 int err;
345
Guenter Roeck08940b82012-06-02 09:57:59 -0700346 data = devm_kzalloc(&client->dev, sizeof(struct adm1029_data),
347 GFP_KERNEL);
348 if (!data)
349 return -ENOMEM;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100350
Jean Delvare9c97fb42008-07-16 19:30:09 +0200351 i2c_set_clientdata(client, data);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100352 mutex_init(&data->update_lock);
353
Corentin Labbecae2caa2007-02-14 21:15:04 +0100354 /*
355 * Initialize the ADM1029 chip
356 * Check config register
357 */
Guenter Roeck08940b82012-06-02 09:57:59 -0700358 if (adm1029_init_client(client) == 0)
359 return -ENODEV;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100360
361 /* Register sysfs hooks */
Frans Meulenbroeks08f50902012-01-08 19:34:18 +0100362 err = sysfs_create_group(&client->dev.kobj, &adm1029_group);
363 if (err)
Guenter Roeck08940b82012-06-02 09:57:59 -0700364 return err;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100365
Tony Jones1beeffe2007-08-20 13:46:20 -0700366 data->hwmon_dev = hwmon_device_register(&client->dev);
367 if (IS_ERR(data->hwmon_dev)) {
368 err = PTR_ERR(data->hwmon_dev);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100369 goto exit_remove_files;
370 }
371
372 return 0;
373
374 exit_remove_files:
375 sysfs_remove_group(&client->dev.kobj, &adm1029_group);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100376 return err;
377}
378
379static int adm1029_init_client(struct i2c_client *client)
380{
381 u8 config;
382 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
383 if ((config & 0x10) == 0) {
384 i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
385 config | 0x10);
386 }
387 /* recheck config */
388 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
389 if ((config & 0x10) == 0) {
390 dev_err(&client->dev, "Initialization failed!\n");
391 return 0;
392 }
393 return 1;
394}
395
Jean Delvare9c97fb42008-07-16 19:30:09 +0200396static int adm1029_remove(struct i2c_client *client)
Corentin Labbecae2caa2007-02-14 21:15:04 +0100397{
398 struct adm1029_data *data = i2c_get_clientdata(client);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100399
Tony Jones1beeffe2007-08-20 13:46:20 -0700400 hwmon_device_unregister(data->hwmon_dev);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100401 sysfs_remove_group(&client->dev.kobj, &adm1029_group);
402
Corentin Labbecae2caa2007-02-14 21:15:04 +0100403 return 0;
404}
405
406/*
Guenter Roeck94b991d2012-01-19 11:02:14 -0800407 * function that update the status of the chips (temperature for example)
408 */
Corentin Labbecae2caa2007-02-14 21:15:04 +0100409static struct adm1029_data *adm1029_update_device(struct device *dev)
410{
411 struct i2c_client *client = to_i2c_client(dev);
412 struct adm1029_data *data = i2c_get_clientdata(client);
413
414 mutex_lock(&data->update_lock);
415 /*
416 * Use the "cache" Luke, don't recheck values
417 * if there are already checked not a long time later
418 */
419 if (time_after(jiffies, data->last_updated + HZ * 2)
420 || !data->valid) {
421 int nr;
422
423 dev_dbg(&client->dev, "Updating adm1029 data\n");
424
425 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
426 data->temp[nr] =
427 i2c_smbus_read_byte_data(client,
428 ADM1029_REG_TEMP[nr]);
429 }
430 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
431 data->fan[nr] =
432 i2c_smbus_read_byte_data(client,
433 ADM1029_REG_FAN[nr]);
434 }
435 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
436 data->fan_div[nr] =
437 i2c_smbus_read_byte_data(client,
438 ADM1029_REG_FAN_DIV[nr]);
439 }
440
441 data->last_updated = jiffies;
442 data->valid = 1;
443 }
444
445 mutex_unlock(&data->update_lock);
446
447 return data;
448}
449
Axel Linf0967ee2012-01-20 15:38:18 +0800450module_i2c_driver(adm1029_driver);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100451
452MODULE_AUTHOR("Corentin LABBE <corentin.labbe@geomatys.fr>");
453MODULE_DESCRIPTION("adm1029 driver");
454MODULE_LICENSE("GPL v2");