blob: e59e2d1f080c9ba1ee279fb5f7cfba925672c295 [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 Delvareb6aacdc2008-07-16 19:30:14 +02004 * Copyright (C) 2003-2008 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
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050051static const unsigned short normal_i2c[] = {
52 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * Insmod parameters
56 */
57
Jordan Crouse43cb7eb2006-03-23 16:19:49 +010058I2C_CLIENT_INSMOD_2(lm83, lm82);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
61 * The LM83 registers
62 * Manufacturer ID is 0x01 for National Semiconductor.
63 */
64
65#define LM83_REG_R_MAN_ID 0xFE
66#define LM83_REG_R_CHIP_ID 0xFF
67#define LM83_REG_R_CONFIG 0x03
68#define LM83_REG_W_CONFIG 0x09
69#define LM83_REG_R_STATUS1 0x02
70#define LM83_REG_R_STATUS2 0x35
71#define LM83_REG_R_LOCAL_TEMP 0x00
72#define LM83_REG_R_LOCAL_HIGH 0x05
73#define LM83_REG_W_LOCAL_HIGH 0x0B
74#define LM83_REG_R_REMOTE1_TEMP 0x30
75#define LM83_REG_R_REMOTE1_HIGH 0x38
76#define LM83_REG_W_REMOTE1_HIGH 0x50
77#define LM83_REG_R_REMOTE2_TEMP 0x01
78#define LM83_REG_R_REMOTE2_HIGH 0x07
79#define LM83_REG_W_REMOTE2_HIGH 0x0D
80#define LM83_REG_R_REMOTE3_TEMP 0x31
81#define LM83_REG_R_REMOTE3_HIGH 0x3A
82#define LM83_REG_W_REMOTE3_HIGH 0x52
83#define LM83_REG_R_TCRIT 0x42
84#define LM83_REG_W_TCRIT 0x5A
85
86/*
87 * Conversions and various macros
Steven Cole44bbe872005-05-03 18:21:25 -060088 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90
91#define TEMP_FROM_REG(val) ((val) * 1000)
92#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
93 (val) >= 127000 ? 127 : \
94 (val) < 0 ? ((val) - 500) / 1000 : \
95 ((val) + 500) / 1000)
96
97static const u8 LM83_REG_R_TEMP[] = {
98 LM83_REG_R_LOCAL_TEMP,
99 LM83_REG_R_REMOTE1_TEMP,
100 LM83_REG_R_REMOTE2_TEMP,
Jean Delvare1a86c052005-06-05 21:16:39 +0200101 LM83_REG_R_REMOTE3_TEMP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 LM83_REG_R_LOCAL_HIGH,
103 LM83_REG_R_REMOTE1_HIGH,
104 LM83_REG_R_REMOTE2_HIGH,
Jean Delvare1a86c052005-06-05 21:16:39 +0200105 LM83_REG_R_REMOTE3_HIGH,
106 LM83_REG_R_TCRIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109static const u8 LM83_REG_W_HIGH[] = {
110 LM83_REG_W_LOCAL_HIGH,
111 LM83_REG_W_REMOTE1_HIGH,
112 LM83_REG_W_REMOTE2_HIGH,
Jean Delvare1a86c052005-06-05 21:16:39 +0200113 LM83_REG_W_REMOTE3_HIGH,
114 LM83_REG_W_TCRIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117/*
118 * Functions declaration
119 */
120
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200121static int lm83_detect(struct i2c_client *new_client, int kind,
122 struct i2c_board_info *info);
123static int lm83_probe(struct i2c_client *client,
124 const struct i2c_device_id *id);
125static int lm83_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static struct lm83_data *lm83_update_device(struct device *dev);
127
128/*
129 * Driver data (common to all clients)
130 */
131
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200132static const struct i2c_device_id lm83_id[] = {
133 { "lm83", lm83 },
134 { "lm82", lm82 },
135 { }
136};
137MODULE_DEVICE_TABLE(i2c, lm83_id);
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static struct i2c_driver lm83_driver = {
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200140 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100141 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100142 .name = "lm83",
143 },
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200144 .probe = lm83_probe,
145 .remove = lm83_remove,
146 .id_table = lm83_id,
147 .detect = lm83_detect,
148 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151/*
152 * Client data (each client gets its own)
153 */
154
155struct lm83_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700156 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100157 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 char valid; /* zero until following fields are valid */
159 unsigned long last_updated; /* in jiffies */
160
161 /* registers values */
Jean Delvare1a86c052005-06-05 21:16:39 +0200162 s8 temp[9]; /* 0..3: input 1-4,
163 4..7: high limit 1-4,
164 8 : critical limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 u16 alarms; /* bitvector, combined */
166};
167
168/*
169 * Sysfs stuff
170 */
171
Jean Delvare1a86c052005-06-05 21:16:39 +0200172static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
173 char *buf)
174{
175 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
176 struct lm83_data *data = lm83_update_device(dev);
177 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Jean Delvare1a86c052005-06-05 21:16:39 +0200180static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
181 const char *buf, size_t count)
182{
183 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
184 struct i2c_client *client = to_i2c_client(dev);
185 struct lm83_data *data = i2c_get_clientdata(client);
186 long val = simple_strtol(buf, NULL, 10);
187 int nr = attr->index;
188
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100189 mutex_lock(&data->update_lock);
Jean Delvare1a86c052005-06-05 21:16:39 +0200190 data->temp[nr] = TEMP_TO_REG(val);
191 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
192 data->temp[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100193 mutex_unlock(&data->update_lock);
Jean Delvare1a86c052005-06-05 21:16:39 +0200194 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jean Delvare1a86c052005-06-05 21:16:39 +0200197static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
198 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct lm83_data *data = lm83_update_device(dev);
201 return sprintf(buf, "%d\n", data->alarms);
202}
203
Jean Delvare2d457712006-09-24 20:52:15 +0200204static ssize_t show_alarm(struct device *dev, struct device_attribute
205 *devattr, char *buf)
206{
207 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
208 struct lm83_data *data = lm83_update_device(dev);
209 int bitnr = attr->index;
210
211 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
212}
213
Jean Delvare1a86c052005-06-05 21:16:39 +0200214static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
215static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
216static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
217static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
218static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
219 set_temp, 4);
220static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
221 set_temp, 5);
222static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
223 set_temp, 6);
224static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
225 set_temp, 7);
226static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
227static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
228static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
229 set_temp, 8);
230static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
Jean Delvare2d457712006-09-24 20:52:15 +0200231
232/* Individual alarm files */
233static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
234static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400235static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200236static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
237static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
238static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
239static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
Jean Delvare7817a392007-06-09 10:11:16 -0400240static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10);
Jean Delvare2d457712006-09-24 20:52:15 +0200241static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
Jean Delvare7817a392007-06-09 10:11:16 -0400242static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13);
Jean Delvare2d457712006-09-24 20:52:15 +0200243static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
244/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
246
Jean Delvare0e39e012006-09-24 21:16:40 +0200247static struct attribute *lm83_attributes[] = {
248 &sensor_dev_attr_temp1_input.dev_attr.attr,
249 &sensor_dev_attr_temp3_input.dev_attr.attr,
250 &sensor_dev_attr_temp1_max.dev_attr.attr,
251 &sensor_dev_attr_temp3_max.dev_attr.attr,
252 &sensor_dev_attr_temp1_crit.dev_attr.attr,
253 &sensor_dev_attr_temp3_crit.dev_attr.attr,
254
255 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
256 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400257 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200258 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
259 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
260 &dev_attr_alarms.attr,
261 NULL
262};
263
264static const struct attribute_group lm83_group = {
265 .attrs = lm83_attributes,
266};
267
268static struct attribute *lm83_attributes_opt[] = {
269 &sensor_dev_attr_temp2_input.dev_attr.attr,
270 &sensor_dev_attr_temp4_input.dev_attr.attr,
271 &sensor_dev_attr_temp2_max.dev_attr.attr,
272 &sensor_dev_attr_temp4_max.dev_attr.attr,
273 &sensor_dev_attr_temp2_crit.dev_attr.attr,
274 &sensor_dev_attr_temp4_crit.dev_attr.attr,
275
276 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
277 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400278 &sensor_dev_attr_temp4_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200279 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400280 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200281 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
282 NULL
283};
284
285static const struct attribute_group lm83_group_opt = {
286 .attrs = lm83_attributes_opt,
287};
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*
290 * Real code
291 */
292
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200293/* Return 0 if detection is successful, -ENODEV otherwise */
294static int lm83_detect(struct i2c_client *new_client, int kind,
295 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200297 struct i2c_adapter *adapter = new_client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 const char *name = "";
299
300 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200301 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Now we do the detection and identification. A negative kind
304 * means that the driver was loaded with no force parameter
305 * (default), so we must both detect and identify the chip
306 * (actually there is only one possible kind of chip for now, LM83).
307 * A zero kind means that the driver was loaded with the force
308 * parameter, the detection step shall be skipped. A positive kind
309 * means that the driver was loaded with the force parameter and a
310 * given kind of chip is requested, so both the detection and the
311 * identification steps are skipped. */
312
313 /* Default to an LM83 if forced */
314 if (kind == 0)
315 kind = lm83;
316
317 if (kind < 0) { /* detection */
318 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
319 & 0xA8) != 0x00) ||
320 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
321 & 0x48) != 0x00) ||
322 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
323 & 0x41) != 0x00)) {
324 dev_dbg(&adapter->dev,
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200325 "LM83 detection failed at 0x%02x.\n",
326 new_client->addr);
327 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 }
330
331 if (kind <= 0) { /* identification */
332 u8 man_id, chip_id;
333
334 man_id = i2c_smbus_read_byte_data(new_client,
335 LM83_REG_R_MAN_ID);
336 chip_id = i2c_smbus_read_byte_data(new_client,
337 LM83_REG_R_CHIP_ID);
338
339 if (man_id == 0x01) { /* National Semiconductor */
340 if (chip_id == 0x03) {
341 kind = lm83;
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100342 } else
343 if (chip_id == 0x01) {
344 kind = lm82;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 }
347
348 if (kind <= 0) { /* identification failed */
349 dev_info(&adapter->dev,
350 "Unsupported chip (man_id=0x%02X, "
351 "chip_id=0x%02X).\n", man_id, chip_id);
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200352 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 }
355
356 if (kind == lm83) {
357 name = "lm83";
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100358 } else
359 if (kind == lm82) {
360 name = "lm82";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200363 strlcpy(info->type, name, I2C_NAME_SIZE);
364
365 return 0;
366}
367
368static int lm83_probe(struct i2c_client *new_client,
369 const struct i2c_device_id *id)
370{
371 struct lm83_data *data;
372 int err;
373
374 data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL);
375 if (!data) {
376 err = -ENOMEM;
377 goto exit;
378 }
379
380 i2c_set_clientdata(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100382 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /*
Jean Delvare0e39e012006-09-24 21:16:40 +0200385 * Register sysfs hooks
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100386 * The LM82 can only monitor one external diode which is
387 * at the same register as the LM83 temp3 entry - so we
388 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
389 */
390
Jean Delvare0e39e012006-09-24 21:16:40 +0200391 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group)))
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200392 goto exit_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200394 if (id->driver_data == lm83) {
Jean Delvare0e39e012006-09-24 21:16:40 +0200395 if ((err = sysfs_create_group(&new_client->dev.kobj,
396 &lm83_group_opt)))
397 goto exit_remove_files;
398 }
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100399
Tony Jones1beeffe2007-08-20 13:46:20 -0700400 data->hwmon_dev = hwmon_device_register(&new_client->dev);
401 if (IS_ERR(data->hwmon_dev)) {
402 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200403 goto exit_remove_files;
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100404 }
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407
Jean Delvare0e39e012006-09-24 21:16:40 +0200408exit_remove_files:
409 sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
410 sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411exit_free:
412 kfree(data);
413exit:
414 return err;
415}
416
Jean Delvareb6aacdc2008-07-16 19:30:14 +0200417static int lm83_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400419 struct lm83_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Tony Jones1beeffe2007-08-20 13:46:20 -0700421 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200422 sysfs_remove_group(&client->dev.kobj, &lm83_group);
423 sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400424
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400425 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427}
428
429static struct lm83_data *lm83_update_device(struct device *dev)
430{
431 struct i2c_client *client = to_i2c_client(dev);
432 struct lm83_data *data = i2c_get_clientdata(client);
433
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100434 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
437 int nr;
438
439 dev_dbg(&client->dev, "Updating lm83 data.\n");
Jean Delvare1a86c052005-06-05 21:16:39 +0200440 for (nr = 0; nr < 9; nr++) {
441 data->temp[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 i2c_smbus_read_byte_data(client,
443 LM83_REG_R_TEMP[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 data->alarms =
446 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
447 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
448 << 8);
449
450 data->last_updated = jiffies;
451 data->valid = 1;
452 }
453
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100454 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 return data;
457}
458
459static int __init sensors_lm83_init(void)
460{
461 return i2c_add_driver(&lm83_driver);
462}
463
464static void __exit sensors_lm83_exit(void)
465{
466 i2c_del_driver(&lm83_driver);
467}
468
469MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
470MODULE_DESCRIPTION("LM83 driver");
471MODULE_LICENSE("GPL");
472
473module_init(sensors_lm83_init);
474module_exit(sensors_lm83_exit);