blob: b96be772e498c82528657ae7ea969fb2c8268da5 [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 */
34static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
35 0x29, 0x2a, 0x2b,
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020036 0x4c, 0x4d, 0x4e,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* Insmod parameters */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020040I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm,
41 mc1066);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* adm1021 constants specified below */
44
45/* The adm1021 registers */
46/* Read-only */
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020047/* For nr in 0-1 */
48#define ADM1021_REG_TEMP(nr) (nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define ADM1021_REG_STATUS 0x02
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020050/* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
51#define ADM1021_REG_MAN_ID 0xFE
52/* ADM1021 = 0x0X, ADM1023 = 0x3X */
53#define ADM1021_REG_DEV_ID 0xFF
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* These use different addresses for reading/writing */
55#define ADM1021_REG_CONFIG_R 0x03
56#define ADM1021_REG_CONFIG_W 0x09
57#define ADM1021_REG_CONV_RATE_R 0x04
58#define ADM1021_REG_CONV_RATE_W 0x0A
59/* These are for the ADM1023's additional precision on the remote temp sensor */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020060#define ADM1023_REG_REM_TEMP_PREC 0x10
61#define ADM1023_REG_REM_OFFSET 0x11
62#define ADM1023_REG_REM_OFFSET_PREC 0x12
63#define ADM1023_REG_REM_TOS_PREC 0x13
64#define ADM1023_REG_REM_THYST_PREC 0x14
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* limits */
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020066/* For nr in 0-1 */
67#define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr))
68#define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr))
69#define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr))
70#define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* write-only */
72#define ADM1021_REG_ONESHOT 0x0F
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* Initial values */
75
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020076/* Note: Even though I left the low and high limits named os and hyst,
77they don't quite work like a thermostat the way the LM75 does. I.e.,
78a lower temp than THYST actually triggers an alarm instead of
Linus Torvalds1da177e2005-04-16 15:20:36 -070079clearing it. Weird, ey? --Phil */
80
81/* Each client has this additional data */
82struct adm1021_data {
83 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -070084 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 enum chips type;
86
Ingo Molnar9a61bf62006-01-18 23:19:26 +010087 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 char valid; /* !=0 if following fields are valid */
89 unsigned long last_updated; /* In jiffies */
90
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020091 s8 temp_max[2]; /* Register values */
92 s8 temp_min[2];
93 s8 temp[2];
94 u8 alarms;
95 /* Special values for ADM1023 only */
96 u8 remote_temp_prec;
97 u8 remote_temp_os_prec;
98 u8 remote_temp_hyst_prec;
99 u8 remote_temp_offset;
100 u8 remote_temp_offset_prec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103static int adm1021_attach_adapter(struct i2c_adapter *adapter);
104static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
105static void adm1021_init_client(struct i2c_client *client);
106static int adm1021_detach_client(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static struct adm1021_data *adm1021_update_device(struct device *dev);
108
109/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
Jean Delvare02002962005-09-25 16:26:44 +0200110static int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112
113/* This is the driver that will be inserted */
114static struct i2c_driver adm1021_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100115 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100116 .name = "adm1021",
117 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .attach_adapter = adm1021_attach_adapter,
119 .detach_client = adm1021_detach_client,
120};
121
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200122static ssize_t show_temp(struct device *dev,
123 struct device_attribute *devattr, char *buf)
124{
125 int index = to_sensor_dev_attr(devattr)->index;
126 struct adm1021_data *data = adm1021_update_device(dev);
127
128 return sprintf(buf, "%d\n", 1000 * data->temp[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200130
131static ssize_t show_temp_max(struct device *dev,
132 struct device_attribute *devattr, char *buf)
133{
134 int index = to_sensor_dev_attr(devattr)->index;
135 struct adm1021_data *data = adm1021_update_device(dev);
136
137 return sprintf(buf, "%d\n", 1000 * data->temp_max[index]);
138}
139
140static ssize_t show_temp_min(struct device *dev,
141 struct device_attribute *devattr, char *buf)
142{
143 int index = to_sensor_dev_attr(devattr)->index;
144 struct adm1021_data *data = adm1021_update_device(dev);
145
146 return sprintf(buf, "%d\n", 1000 * data->temp_min[index]);
147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Krzysztof Helt739ba242007-09-09 19:16:03 +0200149static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
150 char *buf)
151{
152 int index = to_sensor_dev_attr(attr)->index;
153 struct adm1021_data *data = adm1021_update_device(dev);
154 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
155}
156
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200157static ssize_t show_alarms(struct device *dev,
158 struct device_attribute *attr,
159 char *buf)
160{
161 struct adm1021_data *data = adm1021_update_device(dev);
162 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200165static ssize_t set_temp_max(struct device *dev,
166 struct device_attribute *devattr,
167 const char *buf, size_t count)
168{
169 int index = to_sensor_dev_attr(devattr)->index;
170 struct i2c_client *client = to_i2c_client(dev);
171 struct adm1021_data *data = i2c_get_clientdata(client);
172 long temp = simple_strtol(buf, NULL, 10) / 1000;
173
174 mutex_lock(&data->update_lock);
175 data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
176 if (!read_only)
177 i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
178 data->temp_max[index]);
179 mutex_unlock(&data->update_lock);
180
181 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200184static ssize_t set_temp_min(struct device *dev,
185 struct device_attribute *devattr,
186 const char *buf, size_t count)
187{
188 int index = to_sensor_dev_attr(devattr)->index;
189 struct i2c_client *client = to_i2c_client(dev);
190 struct adm1021_data *data = i2c_get_clientdata(client);
191 long temp = simple_strtol(buf, NULL, 10) / 1000;
192
193 mutex_lock(&data->update_lock);
194 data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
195 if (!read_only)
196 i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
197 data->temp_min[index]);
198 mutex_unlock(&data->update_lock);
199
200 return count;
201}
202
203static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
204static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
205 set_temp_max, 0);
206static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
207 set_temp_min, 0);
208static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
209static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
210 set_temp_max, 1);
211static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
212 set_temp_min, 1);
Krzysztof Helt739ba242007-09-09 19:16:03 +0200213static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
214static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
215static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
216static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
217static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static int adm1021_attach_adapter(struct i2c_adapter *adapter)
222{
223 if (!(adapter->class & I2C_CLASS_HWMON))
224 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200225 return i2c_probe(adapter, &addr_data, adm1021_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200228static struct attribute *adm1021_attributes[] = {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200229 &sensor_dev_attr_temp1_max.dev_attr.attr,
230 &sensor_dev_attr_temp1_min.dev_attr.attr,
231 &sensor_dev_attr_temp1_input.dev_attr.attr,
232 &sensor_dev_attr_temp2_max.dev_attr.attr,
233 &sensor_dev_attr_temp2_min.dev_attr.attr,
234 &sensor_dev_attr_temp2_input.dev_attr.attr,
Krzysztof Helt739ba242007-09-09 19:16:03 +0200235 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
236 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
237 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
238 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
239 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200240 &dev_attr_alarms.attr,
241 NULL
242};
243
244static const struct attribute_group adm1021_group = {
245 .attrs = adm1021_attributes,
246};
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
249{
250 int i;
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200251 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct adm1021_data *data;
253 int err = 0;
254 const char *type_name = "";
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200255 int conv_rate, status, config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200257 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
258 pr_debug("adm1021: detect failed, "
259 "smbus byte data not supported!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto error0;
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* OK. For now, we presume we have a valid client. We now create the
264 client structure, even though we cannot fill it completely yet.
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200265 But it allows us to access adm1021 register values. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200267 if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200268 pr_debug("adm1021: detect failed, kzalloc failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 err = -ENOMEM;
270 goto error0;
271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200273 client = &data->client;
274 i2c_set_clientdata(client, data);
275 client->addr = address;
276 client->adapter = adapter;
277 client->driver = &adm1021_driver;
278 status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
279 conv_rate = i2c_smbus_read_byte_data(client,
280 ADM1021_REG_CONV_RATE_R);
281 config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* Now, we do the remaining detection. */
284 if (kind < 0) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200285 if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
286 || (conv_rate & 0xF8) != 0x00) {
287 pr_debug("adm1021: detect failed, "
288 "chip not detected!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 err = -ENODEV;
290 goto error1;
291 }
292 }
293
294 /* Determine the chip type. */
295 if (kind <= 0) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200296 i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (i == 0x41)
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200298 if ((i2c_smbus_read_byte_data(client,
299 ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 kind = adm1023;
301 else
302 kind = adm1021;
303 else if (i == 0x49)
304 kind = thmc10;
305 else if (i == 0x23)
306 kind = gl523sm;
307 else if ((i == 0x4d) &&
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200308 (i2c_smbus_read_byte_data(client,
309 ADM1021_REG_DEV_ID) == 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 kind = max1617a;
311 else if (i == 0x54)
312 kind = mc1066;
313 /* LM84 Mfr ID in a different place, and it has more unused bits */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200314 else if (conv_rate == 0x00
315 && (kind == 0 /* skip extra detection */
316 || ((config & 0x7F) == 0x00
317 && (status & 0xAB) == 0x00)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 kind = lm84;
319 else
320 kind = max1617;
321 }
322
323 if (kind == max1617) {
324 type_name = "max1617";
325 } else if (kind == max1617a) {
326 type_name = "max1617a";
327 } else if (kind == adm1021) {
328 type_name = "adm1021";
329 } else if (kind == adm1023) {
330 type_name = "adm1023";
331 } else if (kind == thmc10) {
332 type_name = "thmc10";
333 } else if (kind == lm84) {
334 type_name = "lm84";
335 } else if (kind == gl523sm) {
336 type_name = "gl523sm";
337 } else if (kind == mc1066) {
338 type_name = "mc1066";
339 }
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200340 pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
341 type_name, i2c_adapter_id(adapter), address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200343 /* Fill in the remaining client fields */
344 strlcpy(client->name, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 data->type = kind;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100346 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* Tell the I2C layer a new client has arrived */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200349 if ((err = i2c_attach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 goto error1;
351
352 /* Initialize the ADM1021 chip */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200353 if (kind != lm84 && !read_only)
354 adm1021_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* Register sysfs hooks */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200357 if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200358 goto error2;
359
Tony Jones1beeffe2007-08-20 13:46:20 -0700360 data->hwmon_dev = hwmon_device_register(&client->dev);
361 if (IS_ERR(data->hwmon_dev)) {
362 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200363 goto error3;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400364 }
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200368error3:
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200369 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400370error2:
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200371 i2c_detach_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372error1:
373 kfree(data);
374error0:
375 return err;
376}
377
378static void adm1021_init_client(struct i2c_client *client)
379{
380 /* Enable ADC and disable suspend mode */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200381 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
382 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Set Conversion rate to 1/sec (this can be tinkered with) */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200384 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387static int adm1021_detach_client(struct i2c_client *client)
388{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400389 struct adm1021_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int err;
391
Tony Jones1beeffe2007-08-20 13:46:20 -0700392 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200393 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400394
Jean Delvare7bef5592005-07-27 22:14:49 +0200395 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400398 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0;
400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static struct adm1021_data *adm1021_update_device(struct device *dev)
403{
404 struct i2c_client *client = to_i2c_client(dev);
405 struct adm1021_data *data = i2c_get_clientdata(client);
406
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100407 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
410 || !data->valid) {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200411 int i;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 dev_dbg(&client->dev, "Starting adm1021 update\n");
414
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200415 for (i = 0; i < 2; i++) {
416 data->temp[i] = i2c_smbus_read_byte_data(client,
417 ADM1021_REG_TEMP(i));
418 data->temp_max[i] = i2c_smbus_read_byte_data(client,
419 ADM1021_REG_TOS_R(i));
420 data->temp_min[i] = i2c_smbus_read_byte_data(client,
421 ADM1021_REG_THYST_R(i));
422 }
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200423 data->alarms = i2c_smbus_read_byte_data(client,
424 ADM1021_REG_STATUS) & 0x7c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (data->type == adm1023) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200426 data->remote_temp_prec =
427 i2c_smbus_read_byte_data(client,
428 ADM1023_REG_REM_TEMP_PREC);
429 data->remote_temp_os_prec =
430 i2c_smbus_read_byte_data(client,
431 ADM1023_REG_REM_TOS_PREC);
432 data->remote_temp_hyst_prec =
433 i2c_smbus_read_byte_data(client,
434 ADM1023_REG_REM_THYST_PREC);
435 data->remote_temp_offset =
436 i2c_smbus_read_byte_data(client,
437 ADM1023_REG_REM_OFFSET);
438 data->remote_temp_offset_prec =
439 i2c_smbus_read_byte_data(client,
440 ADM1023_REG_REM_OFFSET_PREC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 data->last_updated = jiffies;
443 data->valid = 1;
444 }
445
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100446 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 return data;
449}
450
451static int __init sensors_adm1021_init(void)
452{
453 return i2c_add_driver(&adm1021_driver);
454}
455
456static void __exit sensors_adm1021_exit(void)
457{
458 i2c_del_driver(&adm1021_driver);
459}
460
461MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
462 "Philip Edelbrock <phil@netroedge.com>");
463MODULE_DESCRIPTION("adm1021 driver");
464MODULE_LICENSE("GPL");
465
466module_param(read_only, bool, 0);
467MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
468
469module_init(sensors_adm1021_init)
470module_exit(sensors_adm1021_exit)