blob: d74b2c20c719376eea2c8231938f66be28a5f9d8 [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>
Jean Delvare10c08f82005-06-06 19:34:45 +020035#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040036#include <linux/hwmon.h>
37#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Addresses to scan
41 * Address is selected using 2 three-level pins, resulting in 9 possible
42 * addresses.
43 */
44
45static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
46 0x29, 0x2a, 0x2b,
47 0x4c, 0x4d, 0x4e,
48 I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * Insmod parameters
52 */
53
Jean Delvaref4b50262005-07-31 21:49:03 +020054I2C_CLIENT_INSMOD_1(lm83);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * The LM83 registers
58 * Manufacturer ID is 0x01 for National Semiconductor.
59 */
60
61#define LM83_REG_R_MAN_ID 0xFE
62#define LM83_REG_R_CHIP_ID 0xFF
63#define LM83_REG_R_CONFIG 0x03
64#define LM83_REG_W_CONFIG 0x09
65#define LM83_REG_R_STATUS1 0x02
66#define LM83_REG_R_STATUS2 0x35
67#define LM83_REG_R_LOCAL_TEMP 0x00
68#define LM83_REG_R_LOCAL_HIGH 0x05
69#define LM83_REG_W_LOCAL_HIGH 0x0B
70#define LM83_REG_R_REMOTE1_TEMP 0x30
71#define LM83_REG_R_REMOTE1_HIGH 0x38
72#define LM83_REG_W_REMOTE1_HIGH 0x50
73#define LM83_REG_R_REMOTE2_TEMP 0x01
74#define LM83_REG_R_REMOTE2_HIGH 0x07
75#define LM83_REG_W_REMOTE2_HIGH 0x0D
76#define LM83_REG_R_REMOTE3_TEMP 0x31
77#define LM83_REG_R_REMOTE3_HIGH 0x3A
78#define LM83_REG_W_REMOTE3_HIGH 0x52
79#define LM83_REG_R_TCRIT 0x42
80#define LM83_REG_W_TCRIT 0x5A
81
82/*
83 * Conversions and various macros
Steven Cole44bbe872005-05-03 18:21:25 -060084 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
86
87#define TEMP_FROM_REG(val) ((val) * 1000)
88#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
89 (val) >= 127000 ? 127 : \
90 (val) < 0 ? ((val) - 500) / 1000 : \
91 ((val) + 500) / 1000)
92
93static const u8 LM83_REG_R_TEMP[] = {
94 LM83_REG_R_LOCAL_TEMP,
95 LM83_REG_R_REMOTE1_TEMP,
96 LM83_REG_R_REMOTE2_TEMP,
Jean Delvare1a86c052005-06-05 21:16:39 +020097 LM83_REG_R_REMOTE3_TEMP,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 LM83_REG_R_LOCAL_HIGH,
99 LM83_REG_R_REMOTE1_HIGH,
100 LM83_REG_R_REMOTE2_HIGH,
Jean Delvare1a86c052005-06-05 21:16:39 +0200101 LM83_REG_R_REMOTE3_HIGH,
102 LM83_REG_R_TCRIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105static const u8 LM83_REG_W_HIGH[] = {
106 LM83_REG_W_LOCAL_HIGH,
107 LM83_REG_W_REMOTE1_HIGH,
108 LM83_REG_W_REMOTE2_HIGH,
Jean Delvare1a86c052005-06-05 21:16:39 +0200109 LM83_REG_W_REMOTE3_HIGH,
110 LM83_REG_W_TCRIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113/*
114 * Functions declaration
115 */
116
117static int lm83_attach_adapter(struct i2c_adapter *adapter);
118static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
119static int lm83_detach_client(struct i2c_client *client);
120static struct lm83_data *lm83_update_device(struct device *dev);
121
122/*
123 * Driver data (common to all clients)
124 */
125
126static struct i2c_driver lm83_driver = {
127 .owner = THIS_MODULE,
128 .name = "lm83",
129 .id = I2C_DRIVERID_LM83,
130 .flags = I2C_DF_NOTIFY,
131 .attach_adapter = lm83_attach_adapter,
132 .detach_client = lm83_detach_client,
133};
134
135/*
136 * Client data (each client gets its own)
137 */
138
139struct lm83_data {
140 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400141 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct semaphore update_lock;
143 char valid; /* zero until following fields are valid */
144 unsigned long last_updated; /* in jiffies */
145
146 /* registers values */
Jean Delvare1a86c052005-06-05 21:16:39 +0200147 s8 temp[9]; /* 0..3: input 1-4,
148 4..7: high limit 1-4,
149 8 : critical limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 u16 alarms; /* bitvector, combined */
151};
152
153/*
154 * Sysfs stuff
155 */
156
Jean Delvare1a86c052005-06-05 21:16:39 +0200157static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
158 char *buf)
159{
160 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
161 struct lm83_data *data = lm83_update_device(dev);
162 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jean Delvare1a86c052005-06-05 21:16:39 +0200165static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
166 const char *buf, size_t count)
167{
168 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
169 struct i2c_client *client = to_i2c_client(dev);
170 struct lm83_data *data = i2c_get_clientdata(client);
171 long val = simple_strtol(buf, NULL, 10);
172 int nr = attr->index;
173
174 down(&data->update_lock);
175 data->temp[nr] = TEMP_TO_REG(val);
176 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
177 data->temp[nr]);
178 up(&data->update_lock);
179 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Jean Delvare1a86c052005-06-05 21:16:39 +0200182static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
183 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct lm83_data *data = lm83_update_device(dev);
186 return sprintf(buf, "%d\n", data->alarms);
187}
188
Jean Delvare1a86c052005-06-05 21:16:39 +0200189static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
190static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
191static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
192static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
193static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
194 set_temp, 4);
195static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
196 set_temp, 5);
197static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
198 set_temp, 6);
199static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
200 set_temp, 7);
201static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
202static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
203static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
204 set_temp, 8);
205static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
207
208/*
209 * Real code
210 */
211
212static int lm83_attach_adapter(struct i2c_adapter *adapter)
213{
214 if (!(adapter->class & I2C_CLASS_HWMON))
215 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200216 return i2c_probe(adapter, &addr_data, lm83_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219/*
220 * The following function does more than just detection. If detection
221 * succeeds, it also registers the new chip.
222 */
223static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
224{
225 struct i2c_client *new_client;
226 struct lm83_data *data;
227 int err = 0;
228 const char *name = "";
229
230 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
231 goto exit;
232
233 if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
234 err = -ENOMEM;
235 goto exit;
236 }
237 memset(data, 0, sizeof(struct lm83_data));
238
239 /* The common I2C client data is placed right after the
240 * LM83-specific data. */
241 new_client = &data->client;
242 i2c_set_clientdata(new_client, data);
243 new_client->addr = address;
244 new_client->adapter = adapter;
245 new_client->driver = &lm83_driver;
246 new_client->flags = 0;
247
248 /* Now we do the detection and identification. A negative kind
249 * means that the driver was loaded with no force parameter
250 * (default), so we must both detect and identify the chip
251 * (actually there is only one possible kind of chip for now, LM83).
252 * A zero kind means that the driver was loaded with the force
253 * parameter, the detection step shall be skipped. A positive kind
254 * means that the driver was loaded with the force parameter and a
255 * given kind of chip is requested, so both the detection and the
256 * identification steps are skipped. */
257
258 /* Default to an LM83 if forced */
259 if (kind == 0)
260 kind = lm83;
261
262 if (kind < 0) { /* detection */
263 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
264 & 0xA8) != 0x00) ||
265 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
266 & 0x48) != 0x00) ||
267 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
268 & 0x41) != 0x00)) {
269 dev_dbg(&adapter->dev,
270 "LM83 detection failed at 0x%02x.\n", address);
271 goto exit_free;
272 }
273 }
274
275 if (kind <= 0) { /* identification */
276 u8 man_id, chip_id;
277
278 man_id = i2c_smbus_read_byte_data(new_client,
279 LM83_REG_R_MAN_ID);
280 chip_id = i2c_smbus_read_byte_data(new_client,
281 LM83_REG_R_CHIP_ID);
282
283 if (man_id == 0x01) { /* National Semiconductor */
284 if (chip_id == 0x03) {
285 kind = lm83;
286 }
287 }
288
289 if (kind <= 0) { /* identification failed */
290 dev_info(&adapter->dev,
291 "Unsupported chip (man_id=0x%02X, "
292 "chip_id=0x%02X).\n", man_id, chip_id);
293 goto exit_free;
294 }
295 }
296
297 if (kind == lm83) {
298 name = "lm83";
299 }
300
301 /* We can fill in the remaining client fields */
302 strlcpy(new_client->name, name, I2C_NAME_SIZE);
303 data->valid = 0;
304 init_MUTEX(&data->update_lock);
305
306 /* Tell the I2C layer a new client has arrived */
307 if ((err = i2c_attach_client(new_client)))
308 goto exit_free;
309
310 /*
311 * Initialize the LM83 chip
312 * (Nothing to do for this one.)
313 */
314
315 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400316 data->class_dev = hwmon_device_register(&new_client->dev);
317 if (IS_ERR(data->class_dev)) {
318 err = PTR_ERR(data->class_dev);
319 goto exit_detach;
320 }
321
Jean Delvare1a86c052005-06-05 21:16:39 +0200322 device_create_file(&new_client->dev,
323 &sensor_dev_attr_temp1_input.dev_attr);
324 device_create_file(&new_client->dev,
325 &sensor_dev_attr_temp2_input.dev_attr);
326 device_create_file(&new_client->dev,
327 &sensor_dev_attr_temp3_input.dev_attr);
328 device_create_file(&new_client->dev,
329 &sensor_dev_attr_temp4_input.dev_attr);
330 device_create_file(&new_client->dev,
331 &sensor_dev_attr_temp1_max.dev_attr);
332 device_create_file(&new_client->dev,
333 &sensor_dev_attr_temp2_max.dev_attr);
334 device_create_file(&new_client->dev,
335 &sensor_dev_attr_temp3_max.dev_attr);
336 device_create_file(&new_client->dev,
337 &sensor_dev_attr_temp4_max.dev_attr);
338 device_create_file(&new_client->dev,
339 &sensor_dev_attr_temp1_crit.dev_attr);
340 device_create_file(&new_client->dev,
341 &sensor_dev_attr_temp2_crit.dev_attr);
342 device_create_file(&new_client->dev,
343 &sensor_dev_attr_temp3_crit.dev_attr);
344 device_create_file(&new_client->dev,
345 &sensor_dev_attr_temp4_crit.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 device_create_file(&new_client->dev, &dev_attr_alarms);
347
348 return 0;
349
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400350exit_detach:
351 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352exit_free:
353 kfree(data);
354exit:
355 return err;
356}
357
358static int lm83_detach_client(struct i2c_client *client)
359{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400360 struct lm83_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int err;
362
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400363 hwmon_device_unregister(data->class_dev);
364
Jean Delvare7bef5592005-07-27 22:14:49 +0200365 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400368 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
371
372static struct lm83_data *lm83_update_device(struct device *dev)
373{
374 struct i2c_client *client = to_i2c_client(dev);
375 struct lm83_data *data = i2c_get_clientdata(client);
376
377 down(&data->update_lock);
378
379 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
380 int nr;
381
382 dev_dbg(&client->dev, "Updating lm83 data.\n");
Jean Delvare1a86c052005-06-05 21:16:39 +0200383 for (nr = 0; nr < 9; nr++) {
384 data->temp[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 i2c_smbus_read_byte_data(client,
386 LM83_REG_R_TEMP[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 data->alarms =
389 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
390 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
391 << 8);
392
393 data->last_updated = jiffies;
394 data->valid = 1;
395 }
396
397 up(&data->update_lock);
398
399 return data;
400}
401
402static int __init sensors_lm83_init(void)
403{
404 return i2c_add_driver(&lm83_driver);
405}
406
407static void __exit sensors_lm83_exit(void)
408{
409 i2c_del_driver(&lm83_driver);
410}
411
412MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
413MODULE_DESCRIPTION("LM83 driver");
414MODULE_LICENSE("GPL");
415
416module_init(sensors_lm83_init);
417module_exit(sensors_lm83_exit);