blob: 2137d7879df668f1f91e46d94a77ed8a2adca043 [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 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * Addresses to scan
46 * Address is selected using 2 three-level pins, resulting in 9 possible
47 * addresses.
48 */
49
50static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
51 0x29, 0x2a, 0x2b,
52 0x4c, 0x4d, 0x4e,
53 I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * Insmod parameters
57 */
58
Jordan Crouse43cb7eb2006-03-23 16:19:49 +010059I2C_CLIENT_INSMOD_2(lm83, lm82);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
62 * The LM83 registers
63 * Manufacturer ID is 0x01 for National Semiconductor.
64 */
65
66#define LM83_REG_R_MAN_ID 0xFE
67#define LM83_REG_R_CHIP_ID 0xFF
68#define LM83_REG_R_CONFIG 0x03
69#define LM83_REG_W_CONFIG 0x09
70#define LM83_REG_R_STATUS1 0x02
71#define LM83_REG_R_STATUS2 0x35
72#define LM83_REG_R_LOCAL_TEMP 0x00
73#define LM83_REG_R_LOCAL_HIGH 0x05
74#define LM83_REG_W_LOCAL_HIGH 0x0B
75#define LM83_REG_R_REMOTE1_TEMP 0x30
76#define LM83_REG_R_REMOTE1_HIGH 0x38
77#define LM83_REG_W_REMOTE1_HIGH 0x50
78#define LM83_REG_R_REMOTE2_TEMP 0x01
79#define LM83_REG_R_REMOTE2_HIGH 0x07
80#define LM83_REG_W_REMOTE2_HIGH 0x0D
81#define LM83_REG_R_REMOTE3_TEMP 0x31
82#define LM83_REG_R_REMOTE3_HIGH 0x3A
83#define LM83_REG_W_REMOTE3_HIGH 0x52
84#define LM83_REG_R_TCRIT 0x42
85#define LM83_REG_W_TCRIT 0x5A
86
87/*
88 * Conversions and various macros
Steven Cole44bbe872005-05-03 18:21:25 -060089 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91
92#define TEMP_FROM_REG(val) ((val) * 1000)
93#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
94 (val) >= 127000 ? 127 : \
95 (val) < 0 ? ((val) - 500) / 1000 : \
96 ((val) + 500) / 1000)
97
98static const u8 LM83_REG_R_TEMP[] = {
99 LM83_REG_R_LOCAL_TEMP,
100 LM83_REG_R_REMOTE1_TEMP,
101 LM83_REG_R_REMOTE2_TEMP,
Jean Delvare1a86c052005-06-05 21:16:39 +0200102 LM83_REG_R_REMOTE3_TEMP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 LM83_REG_R_LOCAL_HIGH,
104 LM83_REG_R_REMOTE1_HIGH,
105 LM83_REG_R_REMOTE2_HIGH,
Jean Delvare1a86c052005-06-05 21:16:39 +0200106 LM83_REG_R_REMOTE3_HIGH,
107 LM83_REG_R_TCRIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110static const u8 LM83_REG_W_HIGH[] = {
111 LM83_REG_W_LOCAL_HIGH,
112 LM83_REG_W_REMOTE1_HIGH,
113 LM83_REG_W_REMOTE2_HIGH,
Jean Delvare1a86c052005-06-05 21:16:39 +0200114 LM83_REG_W_REMOTE3_HIGH,
115 LM83_REG_W_TCRIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118/*
119 * Functions declaration
120 */
121
122static int lm83_attach_adapter(struct i2c_adapter *adapter);
123static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
124static int lm83_detach_client(struct i2c_client *client);
125static struct lm83_data *lm83_update_device(struct device *dev);
126
127/*
128 * Driver data (common to all clients)
129 */
130
131static struct i2c_driver lm83_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100132 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100133 .name = "lm83",
134 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .id = I2C_DRIVERID_LM83,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .attach_adapter = lm83_attach_adapter,
137 .detach_client = lm83_detach_client,
138};
139
140/*
141 * Client data (each client gets its own)
142 */
143
144struct lm83_data {
145 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400146 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100147 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 char valid; /* zero until following fields are valid */
149 unsigned long last_updated; /* in jiffies */
150
151 /* registers values */
Jean Delvare1a86c052005-06-05 21:16:39 +0200152 s8 temp[9]; /* 0..3: input 1-4,
153 4..7: high limit 1-4,
154 8 : critical limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 u16 alarms; /* bitvector, combined */
156};
157
158/*
159 * Sysfs stuff
160 */
161
Jean Delvare1a86c052005-06-05 21:16:39 +0200162static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
163 char *buf)
164{
165 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
166 struct lm83_data *data = lm83_update_device(dev);
167 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jean Delvare1a86c052005-06-05 21:16:39 +0200170static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
171 const char *buf, size_t count)
172{
173 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
174 struct i2c_client *client = to_i2c_client(dev);
175 struct lm83_data *data = i2c_get_clientdata(client);
176 long val = simple_strtol(buf, NULL, 10);
177 int nr = attr->index;
178
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100179 mutex_lock(&data->update_lock);
Jean Delvare1a86c052005-06-05 21:16:39 +0200180 data->temp[nr] = TEMP_TO_REG(val);
181 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
182 data->temp[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100183 mutex_unlock(&data->update_lock);
Jean Delvare1a86c052005-06-05 21:16:39 +0200184 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Jean Delvare1a86c052005-06-05 21:16:39 +0200187static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
188 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct lm83_data *data = lm83_update_device(dev);
191 return sprintf(buf, "%d\n", data->alarms);
192}
193
Jean Delvare1a86c052005-06-05 21:16:39 +0200194static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
195static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
196static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
197static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
198static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
199 set_temp, 4);
200static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
201 set_temp, 5);
202static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
203 set_temp, 6);
204static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
205 set_temp, 7);
206static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
207static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
208static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
209 set_temp, 8);
210static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
212
213/*
214 * Real code
215 */
216
217static int lm83_attach_adapter(struct i2c_adapter *adapter)
218{
219 if (!(adapter->class & I2C_CLASS_HWMON))
220 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200221 return i2c_probe(adapter, &addr_data, lm83_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*
225 * The following function does more than just detection. If detection
226 * succeeds, it also registers the new chip.
227 */
228static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
229{
230 struct i2c_client *new_client;
231 struct lm83_data *data;
232 int err = 0;
233 const char *name = "";
234
235 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
236 goto exit;
237
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200238 if (!(data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 err = -ENOMEM;
240 goto exit;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* The common I2C client data is placed right after the
244 * LM83-specific data. */
245 new_client = &data->client;
246 i2c_set_clientdata(new_client, data);
247 new_client->addr = address;
248 new_client->adapter = adapter;
249 new_client->driver = &lm83_driver;
250 new_client->flags = 0;
251
252 /* Now we do the detection and identification. A negative kind
253 * means that the driver was loaded with no force parameter
254 * (default), so we must both detect and identify the chip
255 * (actually there is only one possible kind of chip for now, LM83).
256 * A zero kind means that the driver was loaded with the force
257 * parameter, the detection step shall be skipped. A positive kind
258 * means that the driver was loaded with the force parameter and a
259 * given kind of chip is requested, so both the detection and the
260 * identification steps are skipped. */
261
262 /* Default to an LM83 if forced */
263 if (kind == 0)
264 kind = lm83;
265
266 if (kind < 0) { /* detection */
267 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
268 & 0xA8) != 0x00) ||
269 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
270 & 0x48) != 0x00) ||
271 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
272 & 0x41) != 0x00)) {
273 dev_dbg(&adapter->dev,
274 "LM83 detection failed at 0x%02x.\n", address);
275 goto exit_free;
276 }
277 }
278
279 if (kind <= 0) { /* identification */
280 u8 man_id, chip_id;
281
282 man_id = i2c_smbus_read_byte_data(new_client,
283 LM83_REG_R_MAN_ID);
284 chip_id = i2c_smbus_read_byte_data(new_client,
285 LM83_REG_R_CHIP_ID);
286
287 if (man_id == 0x01) { /* National Semiconductor */
288 if (chip_id == 0x03) {
289 kind = lm83;
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100290 } else
291 if (chip_id == 0x01) {
292 kind = lm82;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 }
295
296 if (kind <= 0) { /* identification failed */
297 dev_info(&adapter->dev,
298 "Unsupported chip (man_id=0x%02X, "
299 "chip_id=0x%02X).\n", man_id, chip_id);
300 goto exit_free;
301 }
302 }
303
304 if (kind == lm83) {
305 name = "lm83";
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100306 } else
307 if (kind == lm82) {
308 name = "lm82";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 /* We can fill in the remaining client fields */
312 strlcpy(new_client->name, name, I2C_NAME_SIZE);
313 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100314 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /* Tell the I2C layer a new client has arrived */
317 if ((err = i2c_attach_client(new_client)))
318 goto exit_free;
319
320 /*
321 * Initialize the LM83 chip
322 * (Nothing to do for this one.)
323 */
324
325 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400326 data->class_dev = hwmon_device_register(&new_client->dev);
327 if (IS_ERR(data->class_dev)) {
328 err = PTR_ERR(data->class_dev);
329 goto exit_detach;
330 }
331
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100332 /*
333 * The LM82 can only monitor one external diode which is
334 * at the same register as the LM83 temp3 entry - so we
335 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
336 */
337
Jean Delvare1a86c052005-06-05 21:16:39 +0200338 device_create_file(&new_client->dev,
339 &sensor_dev_attr_temp1_input.dev_attr);
340 device_create_file(&new_client->dev,
Jean Delvare1a86c052005-06-05 21:16:39 +0200341 &sensor_dev_attr_temp3_input.dev_attr);
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100342
Jean Delvare1a86c052005-06-05 21:16:39 +0200343 device_create_file(&new_client->dev,
344 &sensor_dev_attr_temp1_max.dev_attr);
345 device_create_file(&new_client->dev,
Jean Delvare1a86c052005-06-05 21:16:39 +0200346 &sensor_dev_attr_temp3_max.dev_attr);
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100347
Jean Delvare1a86c052005-06-05 21:16:39 +0200348 device_create_file(&new_client->dev,
349 &sensor_dev_attr_temp1_crit.dev_attr);
350 device_create_file(&new_client->dev,
Jean Delvare1a86c052005-06-05 21:16:39 +0200351 &sensor_dev_attr_temp3_crit.dev_attr);
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 device_create_file(&new_client->dev, &dev_attr_alarms);
354
Jordan Crouse43cb7eb2006-03-23 16:19:49 +0100355 if (kind == lm83) {
356 device_create_file(&new_client->dev,
357 &sensor_dev_attr_temp2_input.dev_attr);
358 device_create_file(&new_client->dev,
359 &sensor_dev_attr_temp4_input.dev_attr);
360
361 device_create_file(&new_client->dev,
362 &sensor_dev_attr_temp2_max.dev_attr);
363 device_create_file(&new_client->dev,
364 &sensor_dev_attr_temp4_max.dev_attr);
365
366 device_create_file(&new_client->dev,
367 &sensor_dev_attr_temp2_crit.dev_attr);
368 device_create_file(&new_client->dev,
369 &sensor_dev_attr_temp4_crit.dev_attr);
370 }
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
373
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400374exit_detach:
375 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376exit_free:
377 kfree(data);
378exit:
379 return err;
380}
381
382static int lm83_detach_client(struct i2c_client *client)
383{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400384 struct lm83_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 int err;
386
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400387 hwmon_device_unregister(data->class_dev);
388
Jean Delvare7bef5592005-07-27 22:14:49 +0200389 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400392 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return 0;
394}
395
396static struct lm83_data *lm83_update_device(struct device *dev)
397{
398 struct i2c_client *client = to_i2c_client(dev);
399 struct lm83_data *data = i2c_get_clientdata(client);
400
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100401 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
404 int nr;
405
406 dev_dbg(&client->dev, "Updating lm83 data.\n");
Jean Delvare1a86c052005-06-05 21:16:39 +0200407 for (nr = 0; nr < 9; nr++) {
408 data->temp[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 i2c_smbus_read_byte_data(client,
410 LM83_REG_R_TEMP[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 data->alarms =
413 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
414 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
415 << 8);
416
417 data->last_updated = jiffies;
418 data->valid = 1;
419 }
420
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100421 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 return data;
424}
425
426static int __init sensors_lm83_init(void)
427{
428 return i2c_add_driver(&lm83_driver);
429}
430
431static void __exit sensors_lm83_exit(void)
432{
433 i2c_del_driver(&lm83_driver);
434}
435
436MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
437MODULE_DESCRIPTION("LM83 driver");
438MODULE_LICENSE("GPL");
439
440module_init(sensors_lm83_init);
441module_exit(sensors_lm83_exit);