blob: ecbf69484bf53ad8a65aa515583c11cc4a138e9a [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 {
81 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -070082 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 enum chips type;
84
Ingo Molnar9a61bf62006-01-18 23:19:26 +010085 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 char valid; /* !=0 if following fields are valid */
87 unsigned long last_updated; /* In jiffies */
88
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020089 s8 temp_max[2]; /* Register values */
90 s8 temp_min[2];
91 s8 temp[2];
92 u8 alarms;
93 /* Special values for ADM1023 only */
94 u8 remote_temp_prec;
95 u8 remote_temp_os_prec;
96 u8 remote_temp_hyst_prec;
97 u8 remote_temp_offset;
98 u8 remote_temp_offset_prec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
101static int adm1021_attach_adapter(struct i2c_adapter *adapter);
102static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
103static void adm1021_init_client(struct i2c_client *client);
104static int adm1021_detach_client(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static struct adm1021_data *adm1021_update_device(struct device *dev);
106
107/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
Jean Delvare02002962005-09-25 16:26:44 +0200108static int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110
111/* This is the driver that will be inserted */
112static struct i2c_driver adm1021_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100113 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100114 .name = "adm1021",
115 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .attach_adapter = adm1021_attach_adapter,
117 .detach_client = adm1021_detach_client,
118};
119
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200120static ssize_t show_temp(struct device *dev,
121 struct device_attribute *devattr, char *buf)
122{
123 int index = to_sensor_dev_attr(devattr)->index;
124 struct adm1021_data *data = adm1021_update_device(dev);
125
126 return sprintf(buf, "%d\n", 1000 * data->temp[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200128
129static ssize_t show_temp_max(struct device *dev,
130 struct device_attribute *devattr, char *buf)
131{
132 int index = to_sensor_dev_attr(devattr)->index;
133 struct adm1021_data *data = adm1021_update_device(dev);
134
135 return sprintf(buf, "%d\n", 1000 * data->temp_max[index]);
136}
137
138static ssize_t show_temp_min(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_min[index]);
145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Krzysztof Helt739ba242007-09-09 19:16:03 +0200147static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
148 char *buf)
149{
150 int index = to_sensor_dev_attr(attr)->index;
151 struct adm1021_data *data = adm1021_update_device(dev);
152 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
153}
154
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200155static ssize_t show_alarms(struct device *dev,
156 struct device_attribute *attr,
157 char *buf)
158{
159 struct adm1021_data *data = adm1021_update_device(dev);
160 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200163static ssize_t set_temp_max(struct device *dev,
164 struct device_attribute *devattr,
165 const char *buf, size_t count)
166{
167 int index = to_sensor_dev_attr(devattr)->index;
168 struct i2c_client *client = to_i2c_client(dev);
169 struct adm1021_data *data = i2c_get_clientdata(client);
170 long temp = simple_strtol(buf, NULL, 10) / 1000;
171
172 mutex_lock(&data->update_lock);
173 data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
174 if (!read_only)
175 i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
176 data->temp_max[index]);
177 mutex_unlock(&data->update_lock);
178
179 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200182static ssize_t set_temp_min(struct device *dev,
183 struct device_attribute *devattr,
184 const char *buf, size_t count)
185{
186 int index = to_sensor_dev_attr(devattr)->index;
187 struct i2c_client *client = to_i2c_client(dev);
188 struct adm1021_data *data = i2c_get_clientdata(client);
189 long temp = simple_strtol(buf, NULL, 10) / 1000;
190
191 mutex_lock(&data->update_lock);
192 data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
193 if (!read_only)
194 i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
195 data->temp_min[index]);
196 mutex_unlock(&data->update_lock);
197
198 return count;
199}
200
201static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
202static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
203 set_temp_max, 0);
204static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
205 set_temp_min, 0);
206static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
207static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
208 set_temp_max, 1);
209static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
210 set_temp_min, 1);
Krzysztof Helt739ba242007-09-09 19:16:03 +0200211static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
212static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
213static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
214static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
215static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static int adm1021_attach_adapter(struct i2c_adapter *adapter)
220{
221 if (!(adapter->class & I2C_CLASS_HWMON))
222 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200223 return i2c_probe(adapter, &addr_data, adm1021_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200226static struct attribute *adm1021_attributes[] = {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200227 &sensor_dev_attr_temp1_max.dev_attr.attr,
228 &sensor_dev_attr_temp1_min.dev_attr.attr,
229 &sensor_dev_attr_temp1_input.dev_attr.attr,
230 &sensor_dev_attr_temp2_max.dev_attr.attr,
231 &sensor_dev_attr_temp2_min.dev_attr.attr,
232 &sensor_dev_attr_temp2_input.dev_attr.attr,
Krzysztof Helt739ba242007-09-09 19:16:03 +0200233 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
234 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
235 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
236 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
237 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200238 &dev_attr_alarms.attr,
239 NULL
240};
241
242static const struct attribute_group adm1021_group = {
243 .attrs = adm1021_attributes,
244};
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
247{
248 int i;
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200249 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct adm1021_data *data;
251 int err = 0;
252 const char *type_name = "";
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200253 int conv_rate, status, config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200255 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
256 pr_debug("adm1021: detect failed, "
257 "smbus byte data not supported!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto error0;
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* OK. For now, we presume we have a valid client. We now create the
262 client structure, even though we cannot fill it completely yet.
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200263 But it allows us to access adm1021 register values. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200265 if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200266 pr_debug("adm1021: detect failed, kzalloc failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 err = -ENOMEM;
268 goto error0;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200271 client = &data->client;
272 i2c_set_clientdata(client, data);
273 client->addr = address;
274 client->adapter = adapter;
275 client->driver = &adm1021_driver;
276 status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
277 conv_rate = i2c_smbus_read_byte_data(client,
278 ADM1021_REG_CONV_RATE_R);
279 config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* Now, we do the remaining detection. */
282 if (kind < 0) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200283 if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
284 || (conv_rate & 0xF8) != 0x00) {
285 pr_debug("adm1021: detect failed, "
286 "chip not detected!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 err = -ENODEV;
288 goto error1;
289 }
290 }
291
292 /* Determine the chip type. */
293 if (kind <= 0) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200294 i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (i == 0x41)
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200296 if ((i2c_smbus_read_byte_data(client,
297 ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 kind = adm1023;
299 else
300 kind = adm1021;
301 else if (i == 0x49)
302 kind = thmc10;
303 else if (i == 0x23)
304 kind = gl523sm;
305 else if ((i == 0x4d) &&
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200306 (i2c_smbus_read_byte_data(client,
307 ADM1021_REG_DEV_ID) == 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 kind = max1617a;
309 else if (i == 0x54)
310 kind = mc1066;
311 /* LM84 Mfr ID in a different place, and it has more unused bits */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200312 else if (conv_rate == 0x00
313 && (kind == 0 /* skip extra detection */
314 || ((config & 0x7F) == 0x00
315 && (status & 0xAB) == 0x00)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 kind = lm84;
317 else
318 kind = max1617;
319 }
320
321 if (kind == max1617) {
322 type_name = "max1617";
323 } else if (kind == max1617a) {
324 type_name = "max1617a";
325 } else if (kind == adm1021) {
326 type_name = "adm1021";
327 } else if (kind == adm1023) {
328 type_name = "adm1023";
329 } else if (kind == thmc10) {
330 type_name = "thmc10";
331 } else if (kind == lm84) {
332 type_name = "lm84";
333 } else if (kind == gl523sm) {
334 type_name = "gl523sm";
335 } else if (kind == mc1066) {
336 type_name = "mc1066";
337 }
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200338 pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
339 type_name, i2c_adapter_id(adapter), address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200341 /* Fill in the remaining client fields */
342 strlcpy(client->name, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 data->type = kind;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100344 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* Tell the I2C layer a new client has arrived */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200347 if ((err = i2c_attach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 goto error1;
349
350 /* Initialize the ADM1021 chip */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200351 if (kind != lm84 && !read_only)
352 adm1021_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* Register sysfs hooks */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200355 if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200356 goto error2;
357
Tony Jones1beeffe2007-08-20 13:46:20 -0700358 data->hwmon_dev = hwmon_device_register(&client->dev);
359 if (IS_ERR(data->hwmon_dev)) {
360 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200361 goto error3;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400362 }
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200366error3:
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200367 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400368error2:
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200369 i2c_detach_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370error1:
371 kfree(data);
372error0:
373 return err;
374}
375
376static void adm1021_init_client(struct i2c_client *client)
377{
378 /* Enable ADC and disable suspend mode */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200379 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
380 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /* Set Conversion rate to 1/sec (this can be tinkered with) */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200382 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385static int adm1021_detach_client(struct i2c_client *client)
386{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400387 struct adm1021_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int err;
389
Tony Jones1beeffe2007-08-20 13:46:20 -0700390 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200391 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400392
Jean Delvare7bef5592005-07-27 22:14:49 +0200393 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400396 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return 0;
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400static struct adm1021_data *adm1021_update_device(struct device *dev)
401{
402 struct i2c_client *client = to_i2c_client(dev);
403 struct adm1021_data *data = i2c_get_clientdata(client);
404
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100405 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
408 || !data->valid) {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200409 int i;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 dev_dbg(&client->dev, "Starting adm1021 update\n");
412
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200413 for (i = 0; i < 2; i++) {
414 data->temp[i] = i2c_smbus_read_byte_data(client,
415 ADM1021_REG_TEMP(i));
416 data->temp_max[i] = i2c_smbus_read_byte_data(client,
417 ADM1021_REG_TOS_R(i));
418 data->temp_min[i] = i2c_smbus_read_byte_data(client,
419 ADM1021_REG_THYST_R(i));
420 }
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200421 data->alarms = i2c_smbus_read_byte_data(client,
422 ADM1021_REG_STATUS) & 0x7c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (data->type == adm1023) {
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200424 data->remote_temp_prec =
425 i2c_smbus_read_byte_data(client,
426 ADM1023_REG_REM_TEMP_PREC);
427 data->remote_temp_os_prec =
428 i2c_smbus_read_byte_data(client,
429 ADM1023_REG_REM_TOS_PREC);
430 data->remote_temp_hyst_prec =
431 i2c_smbus_read_byte_data(client,
432 ADM1023_REG_REM_THYST_PREC);
433 data->remote_temp_offset =
434 i2c_smbus_read_byte_data(client,
435 ADM1023_REG_REM_OFFSET);
436 data->remote_temp_offset_prec =
437 i2c_smbus_read_byte_data(client,
438 ADM1023_REG_REM_OFFSET_PREC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 data->last_updated = jiffies;
441 data->valid = 1;
442 }
443
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100444 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 return data;
447}
448
449static int __init sensors_adm1021_init(void)
450{
451 return i2c_add_driver(&adm1021_driver);
452}
453
454static void __exit sensors_adm1021_exit(void)
455{
456 i2c_del_driver(&adm1021_driver);
457}
458
459MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
460 "Philip Edelbrock <phil@netroedge.com>");
461MODULE_DESCRIPTION("adm1021 driver");
462MODULE_LICENSE("GPL");
463
464module_param(read_only, bool, 0);
465MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
466
467module_init(sensors_adm1021_init)
468module_exit(sensors_adm1021_exit)