blob: 5b78d19693e8d693ec173475f2426837bff809a7 [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 Delvare1a86c052005-06-05 21:16:39 +02004 * Copyright (C) 2003-2005 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 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/slab.h>
33#include <linux/jiffies.h>
34#include <linux/i2c.h>
35#include <linux/i2c-sensor.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020036#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040037#include <linux/hwmon.h>
38#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Addresses to scan
42 * Address is selected using 2 three-level pins, resulting in 9 possible
43 * addresses.
44 */
45
46static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
47 0x29, 0x2a, 0x2b,
48 0x4c, 0x4d, 0x4e,
49 I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * Insmod parameters
53 */
54
55SENSORS_INSMOD_1(lm83);
56
57/*
58 * The LM83 registers
59 * Manufacturer ID is 0x01 for National Semiconductor.
60 */
61
62#define LM83_REG_R_MAN_ID 0xFE
63#define LM83_REG_R_CHIP_ID 0xFF
64#define LM83_REG_R_CONFIG 0x03
65#define LM83_REG_W_CONFIG 0x09
66#define LM83_REG_R_STATUS1 0x02
67#define LM83_REG_R_STATUS2 0x35
68#define LM83_REG_R_LOCAL_TEMP 0x00
69#define LM83_REG_R_LOCAL_HIGH 0x05
70#define LM83_REG_W_LOCAL_HIGH 0x0B
71#define LM83_REG_R_REMOTE1_TEMP 0x30
72#define LM83_REG_R_REMOTE1_HIGH 0x38
73#define LM83_REG_W_REMOTE1_HIGH 0x50
74#define LM83_REG_R_REMOTE2_TEMP 0x01
75#define LM83_REG_R_REMOTE2_HIGH 0x07
76#define LM83_REG_W_REMOTE2_HIGH 0x0D
77#define LM83_REG_R_REMOTE3_TEMP 0x31
78#define LM83_REG_R_REMOTE3_HIGH 0x3A
79#define LM83_REG_W_REMOTE3_HIGH 0x52
80#define LM83_REG_R_TCRIT 0x42
81#define LM83_REG_W_TCRIT 0x5A
82
83/*
84 * Conversions and various macros
Steven Cole44bbe872005-05-03 18:21:25 -060085 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87
88#define TEMP_FROM_REG(val) ((val) * 1000)
89#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
90 (val) >= 127000 ? 127 : \
91 (val) < 0 ? ((val) - 500) / 1000 : \
92 ((val) + 500) / 1000)
93
94static const u8 LM83_REG_R_TEMP[] = {
95 LM83_REG_R_LOCAL_TEMP,
96 LM83_REG_R_REMOTE1_TEMP,
97 LM83_REG_R_REMOTE2_TEMP,
Jean Delvare1a86c052005-06-05 21:16:39 +020098 LM83_REG_R_REMOTE3_TEMP,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 LM83_REG_R_LOCAL_HIGH,
100 LM83_REG_R_REMOTE1_HIGH,
101 LM83_REG_R_REMOTE2_HIGH,
Jean Delvare1a86c052005-06-05 21:16:39 +0200102 LM83_REG_R_REMOTE3_HIGH,
103 LM83_REG_R_TCRIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106static const u8 LM83_REG_W_HIGH[] = {
107 LM83_REG_W_LOCAL_HIGH,
108 LM83_REG_W_REMOTE1_HIGH,
109 LM83_REG_W_REMOTE2_HIGH,
Jean Delvare1a86c052005-06-05 21:16:39 +0200110 LM83_REG_W_REMOTE3_HIGH,
111 LM83_REG_W_TCRIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
114/*
115 * Functions declaration
116 */
117
118static int lm83_attach_adapter(struct i2c_adapter *adapter);
119static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
120static int lm83_detach_client(struct i2c_client *client);
121static struct lm83_data *lm83_update_device(struct device *dev);
122
123/*
124 * Driver data (common to all clients)
125 */
126
127static struct i2c_driver lm83_driver = {
128 .owner = THIS_MODULE,
129 .name = "lm83",
130 .id = I2C_DRIVERID_LM83,
131 .flags = I2C_DF_NOTIFY,
132 .attach_adapter = lm83_attach_adapter,
133 .detach_client = lm83_detach_client,
134};
135
136/*
137 * Client data (each client gets its own)
138 */
139
140struct lm83_data {
141 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400142 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct semaphore update_lock;
144 char valid; /* zero until following fields are valid */
145 unsigned long last_updated; /* in jiffies */
146
147 /* registers values */
Jean Delvare1a86c052005-06-05 21:16:39 +0200148 s8 temp[9]; /* 0..3: input 1-4,
149 4..7: high limit 1-4,
150 8 : critical limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 u16 alarms; /* bitvector, combined */
152};
153
154/*
155 * Sysfs stuff
156 */
157
Jean Delvare1a86c052005-06-05 21:16:39 +0200158static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
159 char *buf)
160{
161 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
162 struct lm83_data *data = lm83_update_device(dev);
163 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jean Delvare1a86c052005-06-05 21:16:39 +0200166static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
167 const char *buf, size_t count)
168{
169 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
170 struct i2c_client *client = to_i2c_client(dev);
171 struct lm83_data *data = i2c_get_clientdata(client);
172 long val = simple_strtol(buf, NULL, 10);
173 int nr = attr->index;
174
175 down(&data->update_lock);
176 data->temp[nr] = TEMP_TO_REG(val);
177 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
178 data->temp[nr]);
179 up(&data->update_lock);
180 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jean Delvare1a86c052005-06-05 21:16:39 +0200183static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
184 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct lm83_data *data = lm83_update_device(dev);
187 return sprintf(buf, "%d\n", data->alarms);
188}
189
Jean Delvare1a86c052005-06-05 21:16:39 +0200190static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
191static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
192static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
193static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
194static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
195 set_temp, 4);
196static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
197 set_temp, 5);
198static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
199 set_temp, 6);
200static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
201 set_temp, 7);
202static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
203static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
204static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
205 set_temp, 8);
206static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
208
209/*
210 * Real code
211 */
212
213static int lm83_attach_adapter(struct i2c_adapter *adapter)
214{
215 if (!(adapter->class & I2C_CLASS_HWMON))
216 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200217 return i2c_probe(adapter, &addr_data, lm83_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220/*
221 * The following function does more than just detection. If detection
222 * succeeds, it also registers the new chip.
223 */
224static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
225{
226 struct i2c_client *new_client;
227 struct lm83_data *data;
228 int err = 0;
229 const char *name = "";
230
231 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
232 goto exit;
233
234 if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
235 err = -ENOMEM;
236 goto exit;
237 }
238 memset(data, 0, sizeof(struct lm83_data));
239
240 /* The common I2C client data is placed right after the
241 * LM83-specific data. */
242 new_client = &data->client;
243 i2c_set_clientdata(new_client, data);
244 new_client->addr = address;
245 new_client->adapter = adapter;
246 new_client->driver = &lm83_driver;
247 new_client->flags = 0;
248
249 /* Now we do the detection and identification. A negative kind
250 * means that the driver was loaded with no force parameter
251 * (default), so we must both detect and identify the chip
252 * (actually there is only one possible kind of chip for now, LM83).
253 * A zero kind means that the driver was loaded with the force
254 * parameter, the detection step shall be skipped. A positive kind
255 * means that the driver was loaded with the force parameter and a
256 * given kind of chip is requested, so both the detection and the
257 * identification steps are skipped. */
258
259 /* Default to an LM83 if forced */
260 if (kind == 0)
261 kind = lm83;
262
263 if (kind < 0) { /* detection */
264 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
265 & 0xA8) != 0x00) ||
266 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
267 & 0x48) != 0x00) ||
268 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
269 & 0x41) != 0x00)) {
270 dev_dbg(&adapter->dev,
271 "LM83 detection failed at 0x%02x.\n", address);
272 goto exit_free;
273 }
274 }
275
276 if (kind <= 0) { /* identification */
277 u8 man_id, chip_id;
278
279 man_id = i2c_smbus_read_byte_data(new_client,
280 LM83_REG_R_MAN_ID);
281 chip_id = i2c_smbus_read_byte_data(new_client,
282 LM83_REG_R_CHIP_ID);
283
284 if (man_id == 0x01) { /* National Semiconductor */
285 if (chip_id == 0x03) {
286 kind = lm83;
287 }
288 }
289
290 if (kind <= 0) { /* identification failed */
291 dev_info(&adapter->dev,
292 "Unsupported chip (man_id=0x%02X, "
293 "chip_id=0x%02X).\n", man_id, chip_id);
294 goto exit_free;
295 }
296 }
297
298 if (kind == lm83) {
299 name = "lm83";
300 }
301
302 /* We can fill in the remaining client fields */
303 strlcpy(new_client->name, name, I2C_NAME_SIZE);
304 data->valid = 0;
305 init_MUTEX(&data->update_lock);
306
307 /* Tell the I2C layer a new client has arrived */
308 if ((err = i2c_attach_client(new_client)))
309 goto exit_free;
310
311 /*
312 * Initialize the LM83 chip
313 * (Nothing to do for this one.)
314 */
315
316 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400317 data->class_dev = hwmon_device_register(&new_client->dev);
318 if (IS_ERR(data->class_dev)) {
319 err = PTR_ERR(data->class_dev);
320 goto exit_detach;
321 }
322
Jean Delvare1a86c052005-06-05 21:16:39 +0200323 device_create_file(&new_client->dev,
324 &sensor_dev_attr_temp1_input.dev_attr);
325 device_create_file(&new_client->dev,
326 &sensor_dev_attr_temp2_input.dev_attr);
327 device_create_file(&new_client->dev,
328 &sensor_dev_attr_temp3_input.dev_attr);
329 device_create_file(&new_client->dev,
330 &sensor_dev_attr_temp4_input.dev_attr);
331 device_create_file(&new_client->dev,
332 &sensor_dev_attr_temp1_max.dev_attr);
333 device_create_file(&new_client->dev,
334 &sensor_dev_attr_temp2_max.dev_attr);
335 device_create_file(&new_client->dev,
336 &sensor_dev_attr_temp3_max.dev_attr);
337 device_create_file(&new_client->dev,
338 &sensor_dev_attr_temp4_max.dev_attr);
339 device_create_file(&new_client->dev,
340 &sensor_dev_attr_temp1_crit.dev_attr);
341 device_create_file(&new_client->dev,
342 &sensor_dev_attr_temp2_crit.dev_attr);
343 device_create_file(&new_client->dev,
344 &sensor_dev_attr_temp3_crit.dev_attr);
345 device_create_file(&new_client->dev,
346 &sensor_dev_attr_temp4_crit.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 device_create_file(&new_client->dev, &dev_attr_alarms);
348
349 return 0;
350
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400351exit_detach:
352 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353exit_free:
354 kfree(data);
355exit:
356 return err;
357}
358
359static int lm83_detach_client(struct i2c_client *client)
360{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400361 struct lm83_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int err;
363
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400364 hwmon_device_unregister(data->class_dev);
365
Jean Delvare7bef5592005-07-27 22:14:49 +0200366 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400369 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
372
373static struct lm83_data *lm83_update_device(struct device *dev)
374{
375 struct i2c_client *client = to_i2c_client(dev);
376 struct lm83_data *data = i2c_get_clientdata(client);
377
378 down(&data->update_lock);
379
380 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
381 int nr;
382
383 dev_dbg(&client->dev, "Updating lm83 data.\n");
Jean Delvare1a86c052005-06-05 21:16:39 +0200384 for (nr = 0; nr < 9; nr++) {
385 data->temp[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 i2c_smbus_read_byte_data(client,
387 LM83_REG_R_TEMP[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 data->alarms =
390 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
391 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
392 << 8);
393
394 data->last_updated = jiffies;
395 data->valid = 1;
396 }
397
398 up(&data->update_lock);
399
400 return data;
401}
402
403static int __init sensors_lm83_init(void)
404{
405 return i2c_add_driver(&lm83_driver);
406}
407
408static void __exit sensors_lm83_exit(void)
409{
410 i2c_del_driver(&lm83_driver);
411}
412
413MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
414MODULE_DESCRIPTION("LM83 driver");
415MODULE_LICENSE("GPL");
416
417module_init(sensors_lm83_init);
418module_exit(sensors_lm83_exit);