Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6-block.git] / drivers / hwmon / adm1021.c
CommitLineData
1da177e4
LT
1/*
2 adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
c83c41e4 3 monitoring
1da177e4
LT
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
1da177e4
LT
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
943b0830 27#include <linux/hwmon.h>
a8d6646e 28#include <linux/hwmon-sysfs.h>
943b0830 29#include <linux/err.h>
9a61bf63 30#include <linux/mutex.h>
1da177e4
LT
31
32
33/* Addresses to scan */
25e9c86d
MH
34static const unsigned short normal_i2c[] = {
35 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
1da177e4
LT
36
37/* Insmod parameters */
c83c41e4
KH
38I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm,
39 mc1066);
1da177e4
LT
40
41/* adm1021 constants specified below */
42
43/* The adm1021 registers */
44/* Read-only */
a8d6646e
KH
45/* For nr in 0-1 */
46#define ADM1021_REG_TEMP(nr) (nr)
1da177e4 47#define ADM1021_REG_STATUS 0x02
c83c41e4
KH
48/* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
49#define ADM1021_REG_MAN_ID 0xFE
50/* ADM1021 = 0x0X, ADM1023 = 0x3X */
51#define ADM1021_REG_DEV_ID 0xFF
1da177e4
LT
52/* These use different addresses for reading/writing */
53#define ADM1021_REG_CONFIG_R 0x03
54#define ADM1021_REG_CONFIG_W 0x09
55#define ADM1021_REG_CONV_RATE_R 0x04
56#define ADM1021_REG_CONV_RATE_W 0x0A
57/* These are for the ADM1023's additional precision on the remote temp sensor */
c83c41e4
KH
58#define ADM1023_REG_REM_TEMP_PREC 0x10
59#define ADM1023_REG_REM_OFFSET 0x11
60#define ADM1023_REG_REM_OFFSET_PREC 0x12
61#define ADM1023_REG_REM_TOS_PREC 0x13
62#define ADM1023_REG_REM_THYST_PREC 0x14
1da177e4 63/* limits */
a8d6646e
KH
64/* For nr in 0-1 */
65#define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr))
66#define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr))
67#define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr))
68#define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr))
1da177e4
LT
69/* write-only */
70#define ADM1021_REG_ONESHOT 0x0F
71
1da177e4
LT
72/* Initial values */
73
c83c41e4
KH
74/* Note: Even though I left the low and high limits named os and hyst,
75they don't quite work like a thermostat the way the LM75 does. I.e.,
76a lower temp than THYST actually triggers an alarm instead of
1da177e4
LT
77clearing it. Weird, ey? --Phil */
78
79/* Each client has this additional data */
80struct adm1021_data {
1beeffe4 81 struct device *hwmon_dev;
1da177e4
LT
82 enum chips type;
83
9a61bf63 84 struct mutex update_lock;
1da177e4
LT
85 char valid; /* !=0 if following fields are valid */
86 unsigned long last_updated; /* In jiffies */
87
a8d6646e
KH
88 s8 temp_max[2]; /* Register values */
89 s8 temp_min[2];
90 s8 temp[2];
91 u8 alarms;
92 /* Special values for ADM1023 only */
93 u8 remote_temp_prec;
94 u8 remote_temp_os_prec;
95 u8 remote_temp_hyst_prec;
96 u8 remote_temp_offset;
97 u8 remote_temp_offset_prec;
1da177e4
LT
98};
99
65817ed8
JD
100static int adm1021_probe(struct i2c_client *client,
101 const struct i2c_device_id *id);
102static int adm1021_detect(struct i2c_client *client, int kind,
103 struct i2c_board_info *info);
1da177e4 104static void adm1021_init_client(struct i2c_client *client);
65817ed8 105static int adm1021_remove(struct i2c_client *client);
1da177e4
LT
106static struct adm1021_data *adm1021_update_device(struct device *dev);
107
108/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
02002963 109static int read_only;
1da177e4
LT
110
111
65817ed8
JD
112static const struct i2c_device_id adm1021_id[] = {
113 { "adm1021", adm1021 },
114 { "adm1023", adm1023 },
115 { "max1617", max1617 },
116 { "max1617a", max1617a },
117 { "thmc10", thmc10 },
118 { "lm84", lm84 },
119 { "gl523sm", gl523sm },
120 { "mc1066", mc1066 },
121 { }
122};
123MODULE_DEVICE_TABLE(i2c, adm1021_id);
124
1da177e4
LT
125/* This is the driver that will be inserted */
126static struct i2c_driver adm1021_driver = {
65817ed8 127 .class = I2C_CLASS_HWMON,
cdaf7934 128 .driver = {
cdaf7934
LR
129 .name = "adm1021",
130 },
65817ed8
JD
131 .probe = adm1021_probe,
132 .remove = adm1021_remove,
133 .id_table = adm1021_id,
134 .detect = adm1021_detect,
135 .address_data = &addr_data,
1da177e4
LT
136};
137
a8d6646e
KH
138static ssize_t show_temp(struct device *dev,
139 struct device_attribute *devattr, char *buf)
140{
141 int index = to_sensor_dev_attr(devattr)->index;
142 struct adm1021_data *data = adm1021_update_device(dev);
143
144 return sprintf(buf, "%d\n", 1000 * data->temp[index]);
145}
146
147static ssize_t show_temp_max(struct device *dev,
148 struct device_attribute *devattr, char *buf)
149{
150 int index = to_sensor_dev_attr(devattr)->index;
151 struct adm1021_data *data = adm1021_update_device(dev);
152
153 return sprintf(buf, "%d\n", 1000 * data->temp_max[index]);
154}
155
156static ssize_t show_temp_min(struct device *dev,
157 struct device_attribute *devattr, char *buf)
158{
159 int index = to_sensor_dev_attr(devattr)->index;
160 struct adm1021_data *data = adm1021_update_device(dev);
161
162 return sprintf(buf, "%d\n", 1000 * data->temp_min[index]);
1da177e4 163}
1da177e4 164
739ba248
KH
165static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
166 char *buf)
167{
168 int index = to_sensor_dev_attr(attr)->index;
169 struct adm1021_data *data = adm1021_update_device(dev);
170 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
171}
172
c83c41e4
KH
173static ssize_t show_alarms(struct device *dev,
174 struct device_attribute *attr,
175 char *buf)
176{
177 struct adm1021_data *data = adm1021_update_device(dev);
178 return sprintf(buf, "%u\n", data->alarms);
1da177e4 179}
c83c41e4 180
a8d6646e
KH
181static ssize_t set_temp_max(struct device *dev,
182 struct device_attribute *devattr,
183 const char *buf, size_t count)
184{
185 int index = to_sensor_dev_attr(devattr)->index;
186 struct i2c_client *client = to_i2c_client(dev);
187 struct adm1021_data *data = i2c_get_clientdata(client);
188 long temp = simple_strtol(buf, NULL, 10) / 1000;
189
190 mutex_lock(&data->update_lock);
191 data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
192 if (!read_only)
193 i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
194 data->temp_max[index]);
195 mutex_unlock(&data->update_lock);
196
197 return count;
198}
199
200static ssize_t set_temp_min(struct device *dev,
201 struct device_attribute *devattr,
202 const char *buf, size_t count)
203{
204 int index = to_sensor_dev_attr(devattr)->index;
205 struct i2c_client *client = to_i2c_client(dev);
206 struct adm1021_data *data = i2c_get_clientdata(client);
207 long temp = simple_strtol(buf, NULL, 10) / 1000;
208
209 mutex_lock(&data->update_lock);
210 data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
211 if (!read_only)
212 i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
213 data->temp_min[index]);
214 mutex_unlock(&data->update_lock);
215
216 return count;
1da177e4 217}
1da177e4 218
a8d6646e
KH
219static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
220static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
221 set_temp_max, 0);
222static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
223 set_temp_min, 0);
224static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
225static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
226 set_temp_max, 1);
227static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
228 set_temp_min, 1);
739ba248
KH
229static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
230static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
231static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
232static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
233static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
234
a8d6646e 235static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1da177e4 236
681c6f7a 237static struct attribute *adm1021_attributes[] = {
a8d6646e
KH
238 &sensor_dev_attr_temp1_max.dev_attr.attr,
239 &sensor_dev_attr_temp1_min.dev_attr.attr,
240 &sensor_dev_attr_temp1_input.dev_attr.attr,
241 &sensor_dev_attr_temp2_max.dev_attr.attr,
242 &sensor_dev_attr_temp2_min.dev_attr.attr,
243 &sensor_dev_attr_temp2_input.dev_attr.attr,
739ba248
KH
244 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
245 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
246 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
247 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
248 &sensor_dev_attr_temp2_fault.dev_attr.attr,
681c6f7a
MH
249 &dev_attr_alarms.attr,
250 NULL
251};
252
253static const struct attribute_group adm1021_group = {
254 .attrs = adm1021_attributes,
255};
256
65817ed8
JD
257/* Return 0 if detection is successful, -ENODEV otherwise */
258static int adm1021_detect(struct i2c_client *client, int kind,
259 struct i2c_board_info *info)
1da177e4 260{
65817ed8 261 struct i2c_adapter *adapter = client->adapter;
1da177e4 262 int i;
1da177e4 263 const char *type_name = "";
c83c41e4 264 int conv_rate, status, config;
1da177e4 265
c83c41e4
KH
266 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
267 pr_debug("adm1021: detect failed, "
268 "smbus byte data not supported!\n");
65817ed8 269 return -ENODEV;
1da177e4 270 }
1da177e4 271
c83c41e4
KH
272 status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
273 conv_rate = i2c_smbus_read_byte_data(client,
274 ADM1021_REG_CONV_RATE_R);
275 config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
1da177e4
LT
276
277 /* Now, we do the remaining detection. */
278 if (kind < 0) {
c83c41e4
KH
279 if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
280 || (conv_rate & 0xF8) != 0x00) {
281 pr_debug("adm1021: detect failed, "
282 "chip not detected!\n");
65817ed8 283 return -ENODEV;
1da177e4
LT
284 }
285 }
286
287 /* Determine the chip type. */
288 if (kind <= 0) {
c83c41e4 289 i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
1da177e4 290 if (i == 0x41)
c83c41e4
KH
291 if ((i2c_smbus_read_byte_data(client,
292 ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
1da177e4
LT
293 kind = adm1023;
294 else
295 kind = adm1021;
296 else if (i == 0x49)
297 kind = thmc10;
298 else if (i == 0x23)
299 kind = gl523sm;
300 else if ((i == 0x4d) &&
c83c41e4
KH
301 (i2c_smbus_read_byte_data(client,
302 ADM1021_REG_DEV_ID) == 0x01))
1da177e4
LT
303 kind = max1617a;
304 else if (i == 0x54)
305 kind = mc1066;
306 /* LM84 Mfr ID in a different place, and it has more unused bits */
c83c41e4
KH
307 else if (conv_rate == 0x00
308 && (kind == 0 /* skip extra detection */
309 || ((config & 0x7F) == 0x00
310 && (status & 0xAB) == 0x00)))
1da177e4
LT
311 kind = lm84;
312 else
313 kind = max1617;
314 }
315
316 if (kind == max1617) {
317 type_name = "max1617";
318 } else if (kind == max1617a) {
319 type_name = "max1617a";
320 } else if (kind == adm1021) {
321 type_name = "adm1021";
322 } else if (kind == adm1023) {
323 type_name = "adm1023";
324 } else if (kind == thmc10) {
325 type_name = "thmc10";
326 } else if (kind == lm84) {
327 type_name = "lm84";
328 } else if (kind == gl523sm) {
329 type_name = "gl523sm";
330 } else if (kind == mc1066) {
331 type_name = "mc1066";
332 }
c83c41e4 333 pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
65817ed8
JD
334 type_name, i2c_adapter_id(adapter), client->addr);
335 strlcpy(info->type, type_name, I2C_NAME_SIZE);
1da177e4 336
65817ed8
JD
337 return 0;
338}
1da177e4 339
65817ed8
JD
340static int adm1021_probe(struct i2c_client *client,
341 const struct i2c_device_id *id)
342{
343 struct adm1021_data *data;
344 int err;
345
346 data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL);
347 if (!data) {
348 pr_debug("adm1021: detect failed, kzalloc failed!\n");
349 err = -ENOMEM;
350 goto error0;
351 }
352
353 i2c_set_clientdata(client, data);
354 data->type = id->driver_data;
355 mutex_init(&data->update_lock);
1da177e4
LT
356
357 /* Initialize the ADM1021 chip */
65817ed8 358 if (data->type != lm84 && !read_only)
c83c41e4 359 adm1021_init_client(client);
1da177e4
LT
360
361 /* Register sysfs hooks */
c83c41e4 362 if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
65817ed8 363 goto error1;
681c6f7a 364
1beeffe4
TJ
365 data->hwmon_dev = hwmon_device_register(&client->dev);
366 if (IS_ERR(data->hwmon_dev)) {
367 err = PTR_ERR(data->hwmon_dev);
681c6f7a 368 goto error3;
943b0830
MH
369 }
370
1da177e4
LT
371 return 0;
372
681c6f7a 373error3:
c83c41e4 374 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
1da177e4
LT
375error1:
376 kfree(data);
377error0:
378 return err;
379}
380
381static void adm1021_init_client(struct i2c_client *client)
382{
383 /* Enable ADC and disable suspend mode */
c83c41e4
KH
384 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
385 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
1da177e4 386 /* Set Conversion rate to 1/sec (this can be tinkered with) */
c83c41e4 387 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
1da177e4
LT
388}
389
65817ed8 390static int adm1021_remove(struct i2c_client *client)
1da177e4 391{
943b0830 392 struct adm1021_data *data = i2c_get_clientdata(client);
1da177e4 393
1beeffe4 394 hwmon_device_unregister(data->hwmon_dev);
681c6f7a 395 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
943b0830 396
943b0830 397 kfree(data);
1da177e4
LT
398 return 0;
399}
400
1da177e4
LT
401static struct adm1021_data *adm1021_update_device(struct device *dev)
402{
403 struct i2c_client *client = to_i2c_client(dev);
404 struct adm1021_data *data = i2c_get_clientdata(client);
405
9a61bf63 406 mutex_lock(&data->update_lock);
1da177e4
LT
407
408 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
409 || !data->valid) {
a8d6646e
KH
410 int i;
411
1da177e4
LT
412 dev_dbg(&client->dev, "Starting adm1021 update\n");
413
a8d6646e
KH
414 for (i = 0; i < 2; i++) {
415 data->temp[i] = i2c_smbus_read_byte_data(client,
416 ADM1021_REG_TEMP(i));
417 data->temp_max[i] = i2c_smbus_read_byte_data(client,
418 ADM1021_REG_TOS_R(i));
419 data->temp_min[i] = i2c_smbus_read_byte_data(client,
420 ADM1021_REG_THYST_R(i));
421 }
c83c41e4
KH
422 data->alarms = i2c_smbus_read_byte_data(client,
423 ADM1021_REG_STATUS) & 0x7c;
1da177e4 424 if (data->type == adm1023) {
c83c41e4
KH
425 data->remote_temp_prec =
426 i2c_smbus_read_byte_data(client,
427 ADM1023_REG_REM_TEMP_PREC);
428 data->remote_temp_os_prec =
429 i2c_smbus_read_byte_data(client,
430 ADM1023_REG_REM_TOS_PREC);
431 data->remote_temp_hyst_prec =
432 i2c_smbus_read_byte_data(client,
433 ADM1023_REG_REM_THYST_PREC);
434 data->remote_temp_offset =
435 i2c_smbus_read_byte_data(client,
436 ADM1023_REG_REM_OFFSET);
437 data->remote_temp_offset_prec =
438 i2c_smbus_read_byte_data(client,
439 ADM1023_REG_REM_OFFSET_PREC);
1da177e4
LT
440 }
441 data->last_updated = jiffies;
442 data->valid = 1;
443 }
444
9a61bf63 445 mutex_unlock(&data->update_lock);
1da177e4
LT
446
447 return data;
448}
449
450static int __init sensors_adm1021_init(void)
451{
452 return i2c_add_driver(&adm1021_driver);
453}
454
455static void __exit sensors_adm1021_exit(void)
456{
457 i2c_del_driver(&adm1021_driver);
458}
459
460MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
461 "Philip Edelbrock <phil@netroedge.com>");
462MODULE_DESCRIPTION("adm1021 driver");
463MODULE_LICENSE("GPL");
464
465module_param(read_only, bool, 0);
466MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
467
468module_init(sensors_adm1021_init)
469module_exit(sensors_adm1021_exit)