blob: 654c0f73464d3f502f566d72e71626dd664914ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
Jean Delvare2d457712006-09-24 20:52:15 +02004 * Copyright (C) 2003-2006 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
7 * a sensor chip made by National Semiconductor. It reports up to four
8 * temperatures (its own plus up to three external ones) with a 1 deg
9 * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
10 * from National's website at:
11 * http://www.national.com/pf/LM/LM83.html
12 * Since the datasheet omits to give the chip stepping code, I give it
13 * here: 0x03 (at register 0xff).
14 *
Jordan Crouse43cb7eb2006-03-23 16:19:49 +010015 * Also supports the LM82 temp sensor, which is basically a stripped down
16 * model of the LM83. Datasheet is here:
17 * http://www.national.com/pf/LM/LM82.html
18 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/jiffies.h>
38#include <linux/i2c.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020039#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040040#include <linux/hwmon.h>
41#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010042#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020043#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * Addresses to scan
47 * Address is selected using 2 three-level pins, resulting in 9 possible
48 * addresses.
49 */
50
51static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
52 0x29, 0x2a, 0x2b,
53 0x4c, 0x4d, 0x4e,
54 I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * Insmod parameters
58 */
59
Jordan Crouse43cb7eb2006-03-23 16:19:49 +010060I2C_CLIENT_INSMOD_2(lm83, lm82);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
63 * The LM83 registers
64 * Manufacturer ID is 0x01 for National Semiconductor.
65 */
66
67#define LM83_REG_R_MAN_ID 0xFE
68#define LM83_REG_R_CHIP_ID 0xFF
69#define LM83_REG_R_CONFIG 0x03
70#define LM83_REG_W_CONFIG 0x09
71#define LM83_REG_R_STATUS1 0x02
72#define LM83_REG_R_STATUS2 0x35
73#define LM83_REG_R_LOCAL_TEMP 0x00
74#define LM83_REG_R_LOCAL_HIGH 0x05
75#define LM83_REG_W_LOCAL_HIGH 0x0B
76#define LM83_REG_R_REMOTE1_TEMP 0x30
77#define LM83_REG_R_REMOTE1_HIGH 0x38
78#define LM83_REG_W_REMOTE1_HIGH 0x50
79#define LM83_REG_R_REMOTE2_TEMP 0x01
80#define LM83_REG_R_REMOTE2_HIGH 0x07
81#define LM83_REG_W_REMOTE2_HIGH 0x0D
82#define LM83_REG_R_REMOTE3_TEMP 0x31
83#define LM83_REG_R_REMOTE3_HIGH 0x3A
84#define LM83_REG_W_REMOTE3_HIGH 0x52
85#define LM83_REG_R_TCRIT 0x42
86#define LM83_REG_W_TCRIT 0x5A
87
88/*
89 * Conversions and various macros
Steven Cole44bbe872005-05-03 18:21:25 -060090 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
92
93#define TEMP_FROM_REG(val) ((val) * 1000)
94#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
95 (val) >= 127000 ? 127 : \
96 (val) < 0 ? ((val) - 500) / 1000 : \
97 ((val) + 500) / 1000)
98
99static const u8 LM83_REG_R_TEMP[] = {
100 LM83_REG_R_LOCAL_TEMP,
101 LM83_REG_R_REMOTE1_TEMP,
102 LM83_REG_R_REMOTE2_TEMP,
Jean Delvare1a86c052005-06-05 21:16:39 +0200103 LM83_REG_R_REMOTE3_TEMP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 LM83_REG_R_LOCAL_HIGH,
105 LM83_REG_R_REMOTE1_HIGH,
106 LM83_REG_R_REMOTE2_HIGH,
Jean Delvare1a86c052005-06-05 21:16:39 +0200107 LM83_REG_R_REMOTE3_HIGH,
108 LM83_REG_R_TCRIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111static const u8 LM83_REG_W_HIGH[] = {
112 LM83_REG_W_LOCAL_HIGH,
113 LM83_REG_W_REMOTE1_HIGH,
114 LM83_REG_W_REMOTE2_HIGH,
Jean Delvare1a86c052005-06-05 21:16:39 +0200115 LM83_REG_W_REMOTE3_HIGH,
116 LM83_REG_W_TCRIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119/*
120 * Functions declaration
121 */
122
123static int lm83_attach_adapter(struct i2c_adapter *adapter);
124static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
125static int lm83_detach_client(struct i2c_client *client);
126static struct lm83_data *lm83_update_device(struct device *dev);
127
128/*
129 * Driver data (common to all clients)
130 */
131
132static struct i2c_driver lm83_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100133 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100134 .name = "lm83",
135 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .id = I2C_DRIVERID_LM83,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .attach_adapter = lm83_attach_adapter,
138 .detach_client = lm83_detach_client,
139};
140
141/*
142 * Client data (each client gets its own)
143 */
144
145struct lm83_data {
146 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400147 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100148 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 char valid; /* zero until following fields are valid */
150 unsigned long last_updated; /* in jiffies */
151
152 /* registers values */
Jean Delvare1a86c052005-06-05 21:16:39 +0200153 s8 temp[9]; /* 0..3: input 1-4,
154 4..7: high limit 1-4,
155 8 : critical limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 u16 alarms; /* bitvector, combined */
157};
158
159/*
160 * Sysfs stuff
161 */
162
Jean Delvare1a86c052005-06-05 21:16:39 +0200163static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
164 char *buf)
165{
166 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
167 struct lm83_data *data = lm83_update_device(dev);
168 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jean Delvare1a86c052005-06-05 21:16:39 +0200171static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
172 const char *buf, size_t count)
173{
174 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
175 struct i2c_client *client = to_i2c_client(dev);
176 struct lm83_data *data = i2c_get_clientdata(client);
177 long val = simple_strtol(buf, NULL, 10);
178 int nr = attr->index;
179
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100180 mutex_lock(&data->update_lock);
Jean Delvare1a86c052005-06-05 21:16:39 +0200181 data->temp[nr] = TEMP_TO_REG(val);
182 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
183 data->temp[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100184 mutex_unlock(&data->update_lock);
Jean Delvare1a86c052005-06-05 21:16:39 +0200185 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Jean Delvare1a86c052005-06-05 21:16:39 +0200188static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
189 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct lm83_data *data = lm83_update_device(dev);
192 return sprintf(buf, "%d\n", data->alarms);
193}
194
Jean Delvare2d457712006-09-24 20:52:15 +0200195static ssize_t show_alarm(struct device *dev, struct device_attribute
196 *devattr, char *buf)
197{
198 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
199 struct lm83_data *data = lm83_update_device(dev);
200 int bitnr = attr->index;
201
202 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
203}
204
Jean Delvare1a86c052005-06-05 21:16:39 +0200205static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
206static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
207static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
208static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
209static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
210 set_temp, 4);
211static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
212 set_temp, 5);
213static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
214 set_temp, 6);
215static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
216 set_temp, 7);
217static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
218static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
219static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
220 set_temp, 8);
221static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
Jean Delvare2d457712006-09-24 20:52:15 +0200222
223/* Individual alarm files */
224static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
225static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400226static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200227static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
228static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
229static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
230static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
Jean Delvare7817a392007-06-09 10:11:16 -0400231static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10);
Jean Delvare2d457712006-09-24 20:52:15 +0200232static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
Jean Delvare7817a392007-06-09 10:11:16 -0400233static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13);
Jean Delvare2d457712006-09-24 20:52:15 +0200234static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
235/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
237
Jean Delvare0e39e012006-09-24 21:16:40 +0200238static struct attribute *lm83_attributes[] = {
239 &sensor_dev_attr_temp1_input.dev_attr.attr,
240 &sensor_dev_attr_temp3_input.dev_attr.attr,
241 &sensor_dev_attr_temp1_max.dev_attr.attr,
242 &sensor_dev_attr_temp3_max.dev_attr.attr,
243 &sensor_dev_attr_temp1_crit.dev_attr.attr,
244 &sensor_dev_attr_temp3_crit.dev_attr.attr,
245
246 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
247 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400248 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200249 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
250 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
251 &dev_attr_alarms.attr,
252 NULL
253};
254
255static const struct attribute_group lm83_group = {
256 .attrs = lm83_attributes,
257};
258
259static struct attribute *lm83_attributes_opt[] = {
260 &sensor_dev_attr_temp2_input.dev_attr.attr,
261 &sensor_dev_attr_temp4_input.dev_attr.attr,
262 &sensor_dev_attr_temp2_max.dev_attr.attr,
263 &sensor_dev_attr_temp4_max.dev_attr.attr,
264 &sensor_dev_attr_temp2_crit.dev_attr.attr,
265 &sensor_dev_attr_temp4_crit.dev_attr.attr,
266
267 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
268 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400269 &sensor_dev_attr_temp4_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200270 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400271 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200272 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
273 NULL
274};
275
276static const struct attribute_group lm83_group_opt = {
277 .attrs = lm83_attributes_opt,
278};
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
281 * Real code
282 */
283
284static int lm83_attach_adapter(struct i2c_adapter *adapter)
285{
286 if (!(adapter->class & I2C_CLASS_HWMON))
287 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200288 return i2c_probe(adapter, &addr_data, lm83_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/*
292 * The following function does more than just detection. If detection
293 * succeeds, it also registers the new chip.
294 */
295static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
296{
297 struct i2c_client *new_client;
298 struct lm83_data *data;
299 int err = 0;
300 const char *name = "";
301
302 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
303 goto exit;
304
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200305 if (!(data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 err = -ENOMEM;
307 goto exit;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* The common I2C client data is placed right after the
311 * LM83-specific data. */
312 new_client = &data->client;
313 i2c_set_clientdata(new_client, data);
314 new_client->addr = address;
315 new_client->adapter = adapter;
316 new_client->driver = &lm83_driver;
317 new_client->flags = 0;
318
319 /* Now we do the detection and identification. A negative kind
320 * means that the driver was loaded with no force parameter
321 * (default), so we must both detect and identify the chip
322 * (actually there is only one possible kind of chip for now, LM83).
323 * A zero kind means that the driver was loaded with the force
324 * parameter, the detection step shall be skipped. A positive kind
325 * means that the driver was loaded with the force parameter and a
326 * given kind of chip is requested, so both the detection and the
327 * identification steps are skipped. */
328
329 /* Default to an LM83 if forced */
330 if (kind == 0)
331 kind = lm83;
332
333 if (kind < 0) { /* detection */
334 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
335 & 0xA8) != 0x00) ||
336 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
337 & 0x48) != 0x00) ||
338 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
339 & 0x41) != 0x00)) {
340 dev_dbg(&adapter->dev,
341 "LM83 detection failed at 0x%02x.\n", address);
342 goto exit_free;
343 }
344 }
345
346 if (kind <= 0) { /* identification */
347 u8 man_id, chip_id;
348
349 man_id = i2c_smbus_read_byte_data(new_client,
350 LM83_REG_R_MAN_ID);
351 chip_id = i2c_smbus_read_byte_data(new_client,
352 LM83_REG_R_CHIP_ID);
353
354 if (man_id == 0x01) { /* National Semiconductor */
355 if (chip_id == 0x03) {
356 kind = lm83;
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100357 } else
358 if (chip_id == 0x01) {
359 kind = lm82;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 }
362
363 if (kind <= 0) { /* identification failed */
364 dev_info(&adapter->dev,
365 "Unsupported chip (man_id=0x%02X, "
366 "chip_id=0x%02X).\n", man_id, chip_id);
367 goto exit_free;
368 }
369 }
370
371 if (kind == lm83) {
372 name = "lm83";
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100373 } else
374 if (kind == lm82) {
375 name = "lm82";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 /* We can fill in the remaining client fields */
379 strlcpy(new_client->name, name, I2C_NAME_SIZE);
380 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100381 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* Tell the I2C layer a new client has arrived */
384 if ((err = i2c_attach_client(new_client)))
385 goto exit_free;
386
387 /*
Jean Delvare0e39e012006-09-24 21:16:40 +0200388 * Register sysfs hooks
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100389 * The LM82 can only monitor one external diode which is
390 * at the same register as the LM83 temp3 entry - so we
391 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
392 */
393
Jean Delvare0e39e012006-09-24 21:16:40 +0200394 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group)))
395 goto exit_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100397 if (kind == lm83) {
Jean Delvare0e39e012006-09-24 21:16:40 +0200398 if ((err = sysfs_create_group(&new_client->dev.kobj,
399 &lm83_group_opt)))
400 goto exit_remove_files;
401 }
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100402
Jean Delvare0e39e012006-09-24 21:16:40 +0200403 data->class_dev = hwmon_device_register(&new_client->dev);
404 if (IS_ERR(data->class_dev)) {
405 err = PTR_ERR(data->class_dev);
406 goto exit_remove_files;
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100407 }
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 0;
410
Jean Delvare0e39e012006-09-24 21:16:40 +0200411exit_remove_files:
412 sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
413 sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400414exit_detach:
415 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416exit_free:
417 kfree(data);
418exit:
419 return err;
420}
421
422static int lm83_detach_client(struct i2c_client *client)
423{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400424 struct lm83_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 int err;
426
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400427 hwmon_device_unregister(data->class_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200428 sysfs_remove_group(&client->dev.kobj, &lm83_group);
429 sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400430
Jean Delvare7bef5592005-07-27 22:14:49 +0200431 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400434 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return 0;
436}
437
438static struct lm83_data *lm83_update_device(struct device *dev)
439{
440 struct i2c_client *client = to_i2c_client(dev);
441 struct lm83_data *data = i2c_get_clientdata(client);
442
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100443 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
446 int nr;
447
448 dev_dbg(&client->dev, "Updating lm83 data.\n");
Jean Delvare1a86c052005-06-05 21:16:39 +0200449 for (nr = 0; nr < 9; nr++) {
450 data->temp[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 i2c_smbus_read_byte_data(client,
452 LM83_REG_R_TEMP[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 data->alarms =
455 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
456 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
457 << 8);
458
459 data->last_updated = jiffies;
460 data->valid = 1;
461 }
462
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100463 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 return data;
466}
467
468static int __init sensors_lm83_init(void)
469{
470 return i2c_add_driver(&lm83_driver);
471}
472
473static void __exit sensors_lm83_exit(void)
474{
475 i2c_del_driver(&lm83_driver);
476}
477
478MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
479MODULE_DESCRIPTION("LM83 driver");
480MODULE_LICENSE("GPL");
481
482module_init(sensors_lm83_init);
483module_exit(sensors_lm83_exit);