blob: b11e06f644b19bb14937c2e5cafcc6e52c898182 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
Krzysztof Heltc83c41e2007-07-19 08:00:17 +02003 monitoring
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040027#include <linux/hwmon.h>
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020028#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040029#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32
33/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050034static const unsigned short normal_i2c[] = {
35 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Insmod parameters */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020038I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm,
39 mc1066);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* adm1021 constants specified below */
42
43/* The adm1021 registers */
44/* Read-only */
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020045/* For nr in 0-1 */
46#define ADM1021_REG_TEMP(nr) (nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ADM1021_REG_STATUS 0x02
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020048/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* 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 */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020058#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* limits */
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020064/* 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))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* write-only */
70#define ADM1021_REG_ONESHOT 0x0F
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Initial values */
73
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020074/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070077clearing it. Weird, ey? --Phil */
78
79/* Each client has this additional data */
80struct adm1021_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070081 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 enum chips type;
83
Ingo Molnar9a61bf62006-01-18 23:19:26 +010084 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 char valid; /* !=0 if following fields are valid */
86 unsigned long last_updated; /* In jiffies */
87
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020088 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Jean Delvare65817ed2008-07-16 19:30:08 +0200100static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static void adm1021_init_client(struct i2c_client *client);
Jean Delvare65817ed2008-07-16 19:30:08 +0200105static int adm1021_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static struct adm1021_data *adm1021_update_device(struct device *dev);
107
108/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
Jean Delvare02002962005-09-25 16:26:44 +0200109static int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111
Jean Delvare65817ed2008-07-16 19:30:08 +0200112static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* This is the driver that will be inserted */
126static struct i2c_driver adm1021_driver = {
Jean Delvare65817ed2008-07-16 19:30:08 +0200127 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100128 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100129 .name = "adm1021",
130 },
Jean Delvare65817ed2008-07-16 19:30:08 +0200131 .probe = adm1021_probe,
132 .remove = adm1021_remove,
133 .id_table = adm1021_id,
134 .detect = adm1021_detect,
135 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200138static 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200146
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]);
163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Krzysztof Helt739ba242007-09-09 19:16:03 +0200165static 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
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200173static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200181static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200200static 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;
217}
218
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);
Krzysztof Helt739ba242007-09-09 19:16:03 +0200229static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200237static struct attribute *adm1021_attributes[] = {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200238 &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,
Krzysztof Helt739ba242007-09-09 19:16:03 +0200244 &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,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200249 &dev_attr_alarms.attr,
250 NULL
251};
252
253static const struct attribute_group adm1021_group = {
254 .attrs = adm1021_attributes,
255};
256
Jean Delvare65817ed2008-07-16 19:30:08 +0200257/* Return 0 if detection is successful, -ENODEV otherwise */
258static int adm1021_detect(struct i2c_client *client, int kind,
259 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Jean Delvare65817ed2008-07-16 19:30:08 +0200261 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 const char *type_name = "";
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200264 int conv_rate, status, config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200266 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
267 pr_debug("adm1021: detect failed, "
268 "smbus byte data not supported!\n");
Jean Delvare65817ed2008-07-16 19:30:08 +0200269 return -ENODEV;
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200272 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* Now, we do the remaining detection. */
278 if (kind < 0) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200279 if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
280 || (conv_rate & 0xF8) != 0x00) {
281 pr_debug("adm1021: detect failed, "
282 "chip not detected!\n");
Jean Delvare65817ed2008-07-16 19:30:08 +0200283 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285 }
286
287 /* Determine the chip type. */
288 if (kind <= 0) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200289 i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (i == 0x41)
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200291 if ((i2c_smbus_read_byte_data(client,
292 ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 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) &&
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200301 (i2c_smbus_read_byte_data(client,
302 ADM1021_REG_DEV_ID) == 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 kind = max1617a;
304 else if (i == 0x54)
305 kind = mc1066;
306 /* LM84 Mfr ID in a different place, and it has more unused bits */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200307 else if (conv_rate == 0x00
308 && (kind == 0 /* skip extra detection */
309 || ((config & 0x7F) == 0x00
310 && (status & 0xAB) == 0x00)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 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 }
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200333 pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
Jean Delvare65817ed2008-07-16 19:30:08 +0200334 type_name, i2c_adapter_id(adapter), client->addr);
335 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Jean Delvare65817ed2008-07-16 19:30:08 +0200337 return 0;
338}
339
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;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100355 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /* Initialize the ADM1021 chip */
Jean Delvare65817ed2008-07-16 19:30:08 +0200358 if (data->type != lm84 && !read_only)
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200359 adm1021_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* Register sysfs hooks */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200362 if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
Jean Delvare65817ed2008-07-16 19:30:08 +0200363 goto error1;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200364
Tony Jones1beeffe2007-08-20 13:46:20 -0700365 data->hwmon_dev = hwmon_device_register(&client->dev);
366 if (IS_ERR(data->hwmon_dev)) {
367 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200368 goto error3;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400369 }
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200373error3:
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200374 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375error1:
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 */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200384 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
385 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* Set Conversion rate to 1/sec (this can be tinkered with) */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200387 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Jean Delvare65817ed2008-07-16 19:30:08 +0200390static int adm1021_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400392 struct adm1021_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Tony Jones1beeffe2007-08-20 13:46:20 -0700394 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200395 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400396
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400397 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static 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
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100406 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
409 || !data->valid) {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200410 int i;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 dev_dbg(&client->dev, "Starting adm1021 update\n");
413
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200414 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 }
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200422 data->alarms = i2c_smbus_read_byte_data(client,
423 ADM1021_REG_STATUS) & 0x7c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (data->type == adm1023) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200425 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 data->last_updated = jiffies;
442 data->valid = 1;
443 }
444
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100445 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
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)