blob: 442dc17cdb77fc71fa2026ab62a70a479891005b [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
4 * Copyright (C) 2003 Jean Delvare <khali@linux-fr.org>
5 *
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>
36
37/*
38 * Addresses to scan
39 * Address is selected using 2 three-level pins, resulting in 9 possible
40 * addresses.
41 */
42
43static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
44 0x29, 0x2a, 0x2b,
45 0x4c, 0x4d, 0x4e,
46 I2C_CLIENT_END };
47static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
48
49/*
50 * Insmod parameters
51 */
52
53SENSORS_INSMOD_1(lm83);
54
55/*
56 * The LM83 registers
57 * Manufacturer ID is 0x01 for National Semiconductor.
58 */
59
60#define LM83_REG_R_MAN_ID 0xFE
61#define LM83_REG_R_CHIP_ID 0xFF
62#define LM83_REG_R_CONFIG 0x03
63#define LM83_REG_W_CONFIG 0x09
64#define LM83_REG_R_STATUS1 0x02
65#define LM83_REG_R_STATUS2 0x35
66#define LM83_REG_R_LOCAL_TEMP 0x00
67#define LM83_REG_R_LOCAL_HIGH 0x05
68#define LM83_REG_W_LOCAL_HIGH 0x0B
69#define LM83_REG_R_REMOTE1_TEMP 0x30
70#define LM83_REG_R_REMOTE1_HIGH 0x38
71#define LM83_REG_W_REMOTE1_HIGH 0x50
72#define LM83_REG_R_REMOTE2_TEMP 0x01
73#define LM83_REG_R_REMOTE2_HIGH 0x07
74#define LM83_REG_W_REMOTE2_HIGH 0x0D
75#define LM83_REG_R_REMOTE3_TEMP 0x31
76#define LM83_REG_R_REMOTE3_HIGH 0x3A
77#define LM83_REG_W_REMOTE3_HIGH 0x52
78#define LM83_REG_R_TCRIT 0x42
79#define LM83_REG_W_TCRIT 0x5A
80
81/*
82 * Conversions and various macros
Steven Cole44bbe872005-05-03 18:21:25 -060083 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
85
86#define TEMP_FROM_REG(val) ((val) * 1000)
87#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
88 (val) >= 127000 ? 127 : \
89 (val) < 0 ? ((val) - 500) / 1000 : \
90 ((val) + 500) / 1000)
91
92static const u8 LM83_REG_R_TEMP[] = {
93 LM83_REG_R_LOCAL_TEMP,
94 LM83_REG_R_REMOTE1_TEMP,
95 LM83_REG_R_REMOTE2_TEMP,
96 LM83_REG_R_REMOTE3_TEMP
97};
98
99static const u8 LM83_REG_R_HIGH[] = {
100 LM83_REG_R_LOCAL_HIGH,
101 LM83_REG_R_REMOTE1_HIGH,
102 LM83_REG_R_REMOTE2_HIGH,
103 LM83_REG_R_REMOTE3_HIGH
104};
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,
110 LM83_REG_W_REMOTE3_HIGH
111};
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;
141 struct semaphore update_lock;
142 char valid; /* zero until following fields are valid */
143 unsigned long last_updated; /* in jiffies */
144
145 /* registers values */
146 s8 temp_input[4];
147 s8 temp_high[4];
148 s8 temp_crit;
149 u16 alarms; /* bitvector, combined */
150};
151
152/*
153 * Sysfs stuff
154 */
155
156#define show_temp(suffix, value) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400157static ssize_t show_temp_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{ \
159 struct lm83_data *data = lm83_update_device(dev); \
160 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
161}
162show_temp(input1, temp_input[0]);
163show_temp(input2, temp_input[1]);
164show_temp(input3, temp_input[2]);
165show_temp(input4, temp_input[3]);
166show_temp(high1, temp_high[0]);
167show_temp(high2, temp_high[1]);
168show_temp(high3, temp_high[2]);
169show_temp(high4, temp_high[3]);
170show_temp(crit, temp_crit);
171
172#define set_temp(suffix, value, reg) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400173static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 size_t count) \
175{ \
176 struct i2c_client *client = to_i2c_client(dev); \
177 struct lm83_data *data = i2c_get_clientdata(client); \
178 long val = simple_strtol(buf, NULL, 10); \
179 \
180 down(&data->update_lock); \
181 data->value = TEMP_TO_REG(val); \
182 i2c_smbus_write_byte_data(client, reg, data->value); \
183 up(&data->update_lock); \
184 return count; \
185}
186set_temp(high1, temp_high[0], LM83_REG_W_LOCAL_HIGH);
187set_temp(high2, temp_high[1], LM83_REG_W_REMOTE1_HIGH);
188set_temp(high3, temp_high[2], LM83_REG_W_REMOTE2_HIGH);
189set_temp(high4, temp_high[3], LM83_REG_W_REMOTE3_HIGH);
190set_temp(crit, temp_crit, LM83_REG_W_TCRIT);
191
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400192static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct lm83_data *data = lm83_update_device(dev);
195 return sprintf(buf, "%d\n", data->alarms);
196}
197
198static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
199static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
200static DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input3, NULL);
201static DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input4, NULL);
202static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_high1,
203 set_temp_high1);
204static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
205 set_temp_high2);
206static DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_high3,
207 set_temp_high3);
208static DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_high4,
209 set_temp_high4);
210static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL);
211static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL);
212static DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp_crit,
213 set_temp_crit);
214static DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL);
215static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
216
217/*
218 * Real code
219 */
220
221static int lm83_attach_adapter(struct i2c_adapter *adapter)
222{
223 if (!(adapter->class & I2C_CLASS_HWMON))
224 return 0;
225 return i2c_detect(adapter, &addr_data, lm83_detect);
226}
227
228/*
229 * The following function does more than just detection. If detection
230 * succeeds, it also registers the new chip.
231 */
232static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
233{
234 struct i2c_client *new_client;
235 struct lm83_data *data;
236 int err = 0;
237 const char *name = "";
238
239 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
240 goto exit;
241
242 if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
243 err = -ENOMEM;
244 goto exit;
245 }
246 memset(data, 0, sizeof(struct lm83_data));
247
248 /* The common I2C client data is placed right after the
249 * LM83-specific data. */
250 new_client = &data->client;
251 i2c_set_clientdata(new_client, data);
252 new_client->addr = address;
253 new_client->adapter = adapter;
254 new_client->driver = &lm83_driver;
255 new_client->flags = 0;
256
257 /* Now we do the detection and identification. A negative kind
258 * means that the driver was loaded with no force parameter
259 * (default), so we must both detect and identify the chip
260 * (actually there is only one possible kind of chip for now, LM83).
261 * A zero kind means that the driver was loaded with the force
262 * parameter, the detection step shall be skipped. A positive kind
263 * means that the driver was loaded with the force parameter and a
264 * given kind of chip is requested, so both the detection and the
265 * identification steps are skipped. */
266
267 /* Default to an LM83 if forced */
268 if (kind == 0)
269 kind = lm83;
270
271 if (kind < 0) { /* detection */
272 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
273 & 0xA8) != 0x00) ||
274 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
275 & 0x48) != 0x00) ||
276 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
277 & 0x41) != 0x00)) {
278 dev_dbg(&adapter->dev,
279 "LM83 detection failed at 0x%02x.\n", address);
280 goto exit_free;
281 }
282 }
283
284 if (kind <= 0) { /* identification */
285 u8 man_id, chip_id;
286
287 man_id = i2c_smbus_read_byte_data(new_client,
288 LM83_REG_R_MAN_ID);
289 chip_id = i2c_smbus_read_byte_data(new_client,
290 LM83_REG_R_CHIP_ID);
291
292 if (man_id == 0x01) { /* National Semiconductor */
293 if (chip_id == 0x03) {
294 kind = lm83;
295 }
296 }
297
298 if (kind <= 0) { /* identification failed */
299 dev_info(&adapter->dev,
300 "Unsupported chip (man_id=0x%02X, "
301 "chip_id=0x%02X).\n", man_id, chip_id);
302 goto exit_free;
303 }
304 }
305
306 if (kind == lm83) {
307 name = "lm83";
308 }
309
310 /* We can fill in the remaining client fields */
311 strlcpy(new_client->name, name, I2C_NAME_SIZE);
312 data->valid = 0;
313 init_MUTEX(&data->update_lock);
314
315 /* Tell the I2C layer a new client has arrived */
316 if ((err = i2c_attach_client(new_client)))
317 goto exit_free;
318
319 /*
320 * Initialize the LM83 chip
321 * (Nothing to do for this one.)
322 */
323
324 /* Register sysfs hooks */
325 device_create_file(&new_client->dev, &dev_attr_temp1_input);
326 device_create_file(&new_client->dev, &dev_attr_temp2_input);
327 device_create_file(&new_client->dev, &dev_attr_temp3_input);
328 device_create_file(&new_client->dev, &dev_attr_temp4_input);
329 device_create_file(&new_client->dev, &dev_attr_temp1_max);
330 device_create_file(&new_client->dev, &dev_attr_temp2_max);
331 device_create_file(&new_client->dev, &dev_attr_temp3_max);
332 device_create_file(&new_client->dev, &dev_attr_temp4_max);
333 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
334 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
335 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
336 device_create_file(&new_client->dev, &dev_attr_temp4_crit);
337 device_create_file(&new_client->dev, &dev_attr_alarms);
338
339 return 0;
340
341exit_free:
342 kfree(data);
343exit:
344 return err;
345}
346
347static int lm83_detach_client(struct i2c_client *client)
348{
349 int err;
350
351 if ((err = i2c_detach_client(client))) {
352 dev_err(&client->dev,
353 "Client deregistration failed, client not detached.\n");
354 return err;
355 }
356
357 kfree(i2c_get_clientdata(client));
358 return 0;
359}
360
361static struct lm83_data *lm83_update_device(struct device *dev)
362{
363 struct i2c_client *client = to_i2c_client(dev);
364 struct lm83_data *data = i2c_get_clientdata(client);
365
366 down(&data->update_lock);
367
368 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
369 int nr;
370
371 dev_dbg(&client->dev, "Updating lm83 data.\n");
372 for (nr = 0; nr < 4 ; nr++) {
373 data->temp_input[nr] =
374 i2c_smbus_read_byte_data(client,
375 LM83_REG_R_TEMP[nr]);
376 data->temp_high[nr] =
377 i2c_smbus_read_byte_data(client,
378 LM83_REG_R_HIGH[nr]);
379 }
380 data->temp_crit =
381 i2c_smbus_read_byte_data(client, LM83_REG_R_TCRIT);
382 data->alarms =
383 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
384 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
385 << 8);
386
387 data->last_updated = jiffies;
388 data->valid = 1;
389 }
390
391 up(&data->update_lock);
392
393 return data;
394}
395
396static int __init sensors_lm83_init(void)
397{
398 return i2c_add_driver(&lm83_driver);
399}
400
401static void __exit sensors_lm83_exit(void)
402{
403 i2c_del_driver(&lm83_driver);
404}
405
406MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
407MODULE_DESCRIPTION("LM83 driver");
408MODULE_LICENSE("GPL");
409
410module_init(sensors_lm83_init);
411module_exit(sensors_lm83_exit);