blob: 14cc5af03739363a010213d1061e59daed763ab9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3 * with integrated fan control
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5 * Based on the lm90 driver.
6 *
7 * The LM63 is a sensor chip made by National Semiconductor. It measures
8 * two temperatures (its own and one external one) and the speed of one
9 * fan, those speed it can additionally control. Complete datasheet can be
10 * obtained from National's website at:
11 * http://www.national.com/pf/LM/LM63.html
12 *
13 * The LM63 is basically an LM86 with fan speed monitoring and control
14 * capabilities added. It misses some of the LM86 features though:
15 * - No low limit for local temperature.
16 * - No critical limit for local temperature.
17 * - Critical limit for remote temperature can be changed only once. We
18 * will consider that the critical limit is read-only.
19 *
20 * The datasheet isn't very clear about what the tachometer reading is.
21 * I had a explanation from National Semiconductor though. The two lower
22 * bits of the read value have to be masked out. The value is still 16 bit
23 * in width.
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 */
39
40#include <linux/config.h>
41#include <linux/module.h>
42#include <linux/init.h>
43#include <linux/slab.h>
44#include <linux/jiffies.h>
45#include <linux/i2c.h>
46#include <linux/i2c-sensor.h>
47
48/*
49 * Addresses to scan
50 * Address is fully defined internally and cannot be changed.
51 */
52
53static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
54static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
55
56/*
57 * Insmod parameters
58 */
59
60SENSORS_INSMOD_1(lm63);
61
62/*
63 * The LM63 registers
64 */
65
66#define LM63_REG_CONFIG1 0x03
67#define LM63_REG_CONFIG2 0xBF
68#define LM63_REG_CONFIG_FAN 0x4A
69
70#define LM63_REG_TACH_COUNT_MSB 0x47
71#define LM63_REG_TACH_COUNT_LSB 0x46
72#define LM63_REG_TACH_LIMIT_MSB 0x49
73#define LM63_REG_TACH_LIMIT_LSB 0x48
74
75#define LM63_REG_PWM_VALUE 0x4C
76#define LM63_REG_PWM_FREQ 0x4D
77
78#define LM63_REG_LOCAL_TEMP 0x00
79#define LM63_REG_LOCAL_HIGH 0x05
80
81#define LM63_REG_REMOTE_TEMP_MSB 0x01
82#define LM63_REG_REMOTE_TEMP_LSB 0x10
83#define LM63_REG_REMOTE_OFFSET_MSB 0x11
84#define LM63_REG_REMOTE_OFFSET_LSB 0x12
85#define LM63_REG_REMOTE_HIGH_MSB 0x07
86#define LM63_REG_REMOTE_HIGH_LSB 0x13
87#define LM63_REG_REMOTE_LOW_MSB 0x08
88#define LM63_REG_REMOTE_LOW_LSB 0x14
89#define LM63_REG_REMOTE_TCRIT 0x19
90#define LM63_REG_REMOTE_TCRIT_HYST 0x21
91
92#define LM63_REG_ALERT_STATUS 0x02
93#define LM63_REG_ALERT_MASK 0x16
94
95#define LM63_REG_MAN_ID 0xFE
96#define LM63_REG_CHIP_ID 0xFF
97
98/*
99 * Conversions and various macros
100 * For tachometer counts, the LM63 uses 16-bit values.
101 * For local temperature and high limit, remote critical limit and hysteresis
102 * value, it uses signed 8-bit values with LSB = 1 degree Celcius.
103 * For remote temperature, low and high limits, it uses signed 11-bit values
104 * with LSB = 0.125 degree Celcius, left-justified in 16-bit registers.
105 */
106
107#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
108 5400000 / (reg))
109#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
110 (5400000 / (val)) & 0xFFFC)
111#define TEMP8_FROM_REG(reg) ((reg) * 1000)
112#define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
113 (val) >= 127000 ? 127 : \
114 (val) < 0 ? ((val) - 500) / 1000 : \
115 ((val) + 500) / 1000)
116#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
117#define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
118 (val) >= 127875 ? 0x7FE0 : \
119 (val) < 0 ? ((val) - 62) / 125 * 32 : \
120 ((val) + 62) / 125 * 32)
121#define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
122 (val) >= 127000 ? 127 : \
123 ((val) + 500) / 1000)
124
125/*
126 * Functions declaration
127 */
128
129static int lm63_attach_adapter(struct i2c_adapter *adapter);
130static int lm63_detach_client(struct i2c_client *client);
131
132static struct lm63_data *lm63_update_device(struct device *dev);
133
134static int lm63_detect(struct i2c_adapter *adapter, int address, int kind);
135static void lm63_init_client(struct i2c_client *client);
136
137/*
138 * Driver data (common to all clients)
139 */
140
141static struct i2c_driver lm63_driver = {
142 .owner = THIS_MODULE,
143 .name = "lm63",
144 .flags = I2C_DF_NOTIFY,
145 .attach_adapter = lm63_attach_adapter,
146 .detach_client = lm63_detach_client,
147};
148
149/*
150 * Client data (each client gets its own)
151 */
152
153struct lm63_data {
154 struct i2c_client client;
155 struct semaphore update_lock;
156 char valid; /* zero until following fields are valid */
157 unsigned long last_updated; /* in jiffies */
158
159 /* registers values */
160 u8 config, config_fan;
161 u16 fan1_input;
162 u16 fan1_low;
163 u8 pwm1_freq;
164 u8 pwm1_value;
165 s8 temp1_input;
166 s8 temp1_high;
167 s16 temp2_input;
168 s16 temp2_high;
169 s16 temp2_low;
170 s8 temp2_crit;
171 u8 temp2_crit_hyst;
172 u8 alarms;
173};
174
175/*
176 * Sysfs callback functions and files
177 */
178
179#define show_fan(value) \
180static ssize_t show_##value(struct device *dev, char *buf) \
181{ \
182 struct lm63_data *data = lm63_update_device(dev); \
183 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value)); \
184}
185show_fan(fan1_input);
186show_fan(fan1_low);
187
188static ssize_t set_fan1_low(struct device *dev, const char *buf,
189 size_t count)
190{
191 struct i2c_client *client = to_i2c_client(dev);
192 struct lm63_data *data = i2c_get_clientdata(client);
193 unsigned long val = simple_strtoul(buf, NULL, 10);
194
195 down(&data->update_lock);
196 data->fan1_low = FAN_TO_REG(val);
197 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
198 data->fan1_low & 0xFF);
199 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
200 data->fan1_low >> 8);
201 up(&data->update_lock);
202 return count;
203}
204
205static ssize_t show_pwm1(struct device *dev, char *buf)
206{
207 struct lm63_data *data = lm63_update_device(dev);
208 return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
209 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
210 (2 * data->pwm1_freq));
211}
212
213static ssize_t set_pwm1(struct device *dev, const char *buf, size_t count)
214{
215 struct i2c_client *client = to_i2c_client(dev);
216 struct lm63_data *data = i2c_get_clientdata(client);
217 unsigned long val;
218
219 if (!(data->config_fan & 0x20)) /* register is read-only */
220 return -EPERM;
221
222 val = simple_strtoul(buf, NULL, 10);
223 down(&data->update_lock);
224 data->pwm1_value = val <= 0 ? 0 :
225 val >= 255 ? 2 * data->pwm1_freq :
226 (val * data->pwm1_freq * 2 + 127) / 255;
227 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
228 up(&data->update_lock);
229 return count;
230}
231
232static ssize_t show_pwm1_enable(struct device *dev, char *buf)
233{
234 struct lm63_data *data = lm63_update_device(dev);
235 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
236}
237
238#define show_temp8(value) \
239static ssize_t show_##value(struct device *dev, char *buf) \
240{ \
241 struct lm63_data *data = lm63_update_device(dev); \
242 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->value)); \
243}
244#define show_temp11(value) \
245static ssize_t show_##value(struct device *dev, char *buf) \
246{ \
247 struct lm63_data *data = lm63_update_device(dev); \
248 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->value)); \
249}
250show_temp8(temp1_input);
251show_temp8(temp1_high);
252show_temp11(temp2_input);
253show_temp11(temp2_high);
254show_temp11(temp2_low);
255show_temp8(temp2_crit);
256
257#define set_temp8(value, reg) \
258static ssize_t set_##value(struct device *dev, const char *buf, \
259 size_t count) \
260{ \
261 struct i2c_client *client = to_i2c_client(dev); \
262 struct lm63_data *data = i2c_get_clientdata(client); \
263 long val = simple_strtol(buf, NULL, 10); \
264 \
265 down(&data->update_lock); \
266 data->value = TEMP8_TO_REG(val); \
267 i2c_smbus_write_byte_data(client, reg, data->value); \
268 up(&data->update_lock); \
269 return count; \
270}
271#define set_temp11(value, reg_msb, reg_lsb) \
272static ssize_t set_##value(struct device *dev, const char *buf, \
273 size_t count) \
274{ \
275 struct i2c_client *client = to_i2c_client(dev); \
276 struct lm63_data *data = i2c_get_clientdata(client); \
277 long val = simple_strtol(buf, NULL, 10); \
278 \
279 down(&data->update_lock); \
280 data->value = TEMP11_TO_REG(val); \
281 i2c_smbus_write_byte_data(client, reg_msb, data->value >> 8); \
282 i2c_smbus_write_byte_data(client, reg_lsb, data->value & 0xff); \
283 up(&data->update_lock); \
284 return count; \
285}
286set_temp8(temp1_high, LM63_REG_LOCAL_HIGH);
287set_temp11(temp2_high, LM63_REG_REMOTE_HIGH_MSB, LM63_REG_REMOTE_HIGH_LSB);
288set_temp11(temp2_low, LM63_REG_REMOTE_LOW_MSB, LM63_REG_REMOTE_LOW_LSB);
289
290/* Hysteresis register holds a relative value, while we want to present
291 an absolute to user-space */
292static ssize_t show_temp2_crit_hyst(struct device *dev, char *buf)
293{
294 struct lm63_data *data = lm63_update_device(dev);
295 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp2_crit)
296 - TEMP8_FROM_REG(data->temp2_crit_hyst));
297}
298
299/* And now the other way around, user-space provides an absolute
300 hysteresis value and we have to store a relative one */
301static ssize_t set_temp2_crit_hyst(struct device *dev, const char *buf,
302 size_t count)
303{
304 struct i2c_client *client = to_i2c_client(dev);
305 struct lm63_data *data = i2c_get_clientdata(client);
306 long val = simple_strtol(buf, NULL, 10);
307 long hyst;
308
309 down(&data->update_lock);
310 hyst = TEMP8_FROM_REG(data->temp2_crit) - val;
311 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
312 HYST_TO_REG(hyst));
313 up(&data->update_lock);
314 return count;
315}
316
317static ssize_t show_alarms(struct device *dev, char *buf)
318{
319 struct lm63_data *data = lm63_update_device(dev);
320 return sprintf(buf, "%u\n", data->alarms);
321}
322
323static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan1_input, NULL);
324static DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan1_low,
325 set_fan1_low);
326
327static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
328static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
329
330static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1_input, NULL);
331static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp1_high,
332 set_temp1_high);
333
334static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp2_input, NULL);
335static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp2_low,
336 set_temp2_low);
337static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp2_high,
338 set_temp2_high);
339static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp2_crit, NULL);
340static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
341 set_temp2_crit_hyst);
342
343static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
344
345/*
346 * Real code
347 */
348
349static int lm63_attach_adapter(struct i2c_adapter *adapter)
350{
351 if (!(adapter->class & I2C_CLASS_HWMON))
352 return 0;
353 return i2c_detect(adapter, &addr_data, lm63_detect);
354}
355
356/*
357 * The following function does more than just detection. If detection
358 * succeeds, it also registers the new chip.
359 */
360static int lm63_detect(struct i2c_adapter *adapter, int address, int kind)
361{
362 struct i2c_client *new_client;
363 struct lm63_data *data;
364 int err = 0;
365
366 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
367 goto exit;
368
369 if (!(data = kmalloc(sizeof(struct lm63_data), GFP_KERNEL))) {
370 err = -ENOMEM;
371 goto exit;
372 }
373 memset(data, 0, sizeof(struct lm63_data));
374
375 /* The common I2C client data is placed right before the
376 LM63-specific data. */
377 new_client = &data->client;
378 i2c_set_clientdata(new_client, data);
379 new_client->addr = address;
380 new_client->adapter = adapter;
381 new_client->driver = &lm63_driver;
382 new_client->flags = 0;
383
384 /* Default to an LM63 if forced */
385 if (kind == 0)
386 kind = lm63;
387
388 if (kind < 0) { /* must identify */
389 u8 man_id, chip_id, reg_config1, reg_config2;
390 u8 reg_alert_status, reg_alert_mask;
391
392 man_id = i2c_smbus_read_byte_data(new_client,
393 LM63_REG_MAN_ID);
394 chip_id = i2c_smbus_read_byte_data(new_client,
395 LM63_REG_CHIP_ID);
396 reg_config1 = i2c_smbus_read_byte_data(new_client,
397 LM63_REG_CONFIG1);
398 reg_config2 = i2c_smbus_read_byte_data(new_client,
399 LM63_REG_CONFIG2);
400 reg_alert_status = i2c_smbus_read_byte_data(new_client,
401 LM63_REG_ALERT_STATUS);
402 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
403 LM63_REG_ALERT_MASK);
404
405 if (man_id == 0x01 /* National Semiconductor */
406 && chip_id == 0x41 /* LM63 */
407 && (reg_config1 & 0x18) == 0x00
408 && (reg_config2 & 0xF8) == 0x00
409 && (reg_alert_status & 0x20) == 0x00
410 && (reg_alert_mask & 0xA4) == 0xA4) {
411 kind = lm63;
412 } else { /* failed */
413 dev_dbg(&adapter->dev, "Unsupported chip "
414 "(man_id=0x%02X, chip_id=0x%02X).\n",
415 man_id, chip_id);
416 goto exit_free;
417 }
418 }
419
420 strlcpy(new_client->name, "lm63", I2C_NAME_SIZE);
421 data->valid = 0;
422 init_MUTEX(&data->update_lock);
423
424 /* Tell the I2C layer a new client has arrived */
425 if ((err = i2c_attach_client(new_client)))
426 goto exit_free;
427
428 /* Initialize the LM63 chip */
429 lm63_init_client(new_client);
430
431 /* Register sysfs hooks */
432 if (data->config & 0x04) { /* tachometer enabled */
433 device_create_file(&new_client->dev, &dev_attr_fan1_input);
434 device_create_file(&new_client->dev, &dev_attr_fan1_min);
435 }
436 device_create_file(&new_client->dev, &dev_attr_pwm1);
437 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
438 device_create_file(&new_client->dev, &dev_attr_temp1_input);
439 device_create_file(&new_client->dev, &dev_attr_temp2_input);
440 device_create_file(&new_client->dev, &dev_attr_temp2_min);
441 device_create_file(&new_client->dev, &dev_attr_temp1_max);
442 device_create_file(&new_client->dev, &dev_attr_temp2_max);
443 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
444 device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
445 device_create_file(&new_client->dev, &dev_attr_alarms);
446
447 return 0;
448
449exit_free:
450 kfree(data);
451exit:
452 return err;
453}
454
455/* Idealy we shouldn't have to initialize anything, since the BIOS
456 should have taken care of everything */
457static void lm63_init_client(struct i2c_client *client)
458{
459 struct lm63_data *data = i2c_get_clientdata(client);
460
461 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
462 data->config_fan = i2c_smbus_read_byte_data(client,
463 LM63_REG_CONFIG_FAN);
464
465 /* Start converting if needed */
466 if (data->config & 0x40) { /* standby */
467 dev_dbg(&client->dev, "Switching to operational mode");
468 data->config &= 0xA7;
469 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
470 data->config);
471 }
472
473 /* We may need pwm1_freq before ever updating the client data */
474 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
475 if (data->pwm1_freq == 0)
476 data->pwm1_freq = 1;
477
478 /* Show some debug info about the LM63 configuration */
479 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
480 (data->config & 0x04) ? "tachometer input" :
481 "alert output");
482 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
483 (data->config_fan & 0x08) ? "1.4" : "360",
484 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
485 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
486 (data->config_fan & 0x10) ? "low" : "high",
487 (data->config_fan & 0x20) ? "manual" : "auto");
488}
489
490static int lm63_detach_client(struct i2c_client *client)
491{
492 int err;
493
494 if ((err = i2c_detach_client(client))) {
495 dev_err(&client->dev, "Client deregistration failed, "
496 "client not detached\n");
497 return err;
498 }
499
500 kfree(i2c_get_clientdata(client));
501 return 0;
502}
503
504static struct lm63_data *lm63_update_device(struct device *dev)
505{
506 struct i2c_client *client = to_i2c_client(dev);
507 struct lm63_data *data = i2c_get_clientdata(client);
508
509 down(&data->update_lock);
510
511 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
512 if (data->config & 0x04) { /* tachometer enabled */
513 /* order matters for fan1_input */
514 data->fan1_input = i2c_smbus_read_byte_data(client,
515 LM63_REG_TACH_COUNT_LSB) & 0xFC;
516 data->fan1_input |= i2c_smbus_read_byte_data(client,
517 LM63_REG_TACH_COUNT_MSB) << 8;
518 data->fan1_low = (i2c_smbus_read_byte_data(client,
519 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
520 | (i2c_smbus_read_byte_data(client,
521 LM63_REG_TACH_LIMIT_MSB) << 8);
522 }
523
524 data->pwm1_freq = i2c_smbus_read_byte_data(client,
525 LM63_REG_PWM_FREQ);
526 if (data->pwm1_freq == 0)
527 data->pwm1_freq = 1;
528 data->pwm1_value = i2c_smbus_read_byte_data(client,
529 LM63_REG_PWM_VALUE);
530
531 data->temp1_input = i2c_smbus_read_byte_data(client,
532 LM63_REG_LOCAL_TEMP);
533 data->temp1_high = i2c_smbus_read_byte_data(client,
534 LM63_REG_LOCAL_HIGH);
535
536 /* order matters for temp2_input */
537 data->temp2_input = i2c_smbus_read_byte_data(client,
538 LM63_REG_REMOTE_TEMP_MSB) << 8;
539 data->temp2_input |= i2c_smbus_read_byte_data(client,
540 LM63_REG_REMOTE_TEMP_LSB);
541 data->temp2_high = (i2c_smbus_read_byte_data(client,
542 LM63_REG_REMOTE_HIGH_MSB) << 8)
543 | i2c_smbus_read_byte_data(client,
544 LM63_REG_REMOTE_HIGH_LSB);
545 data->temp2_low = (i2c_smbus_read_byte_data(client,
546 LM63_REG_REMOTE_LOW_MSB) << 8)
547 | i2c_smbus_read_byte_data(client,
548 LM63_REG_REMOTE_LOW_LSB);
549 data->temp2_crit = i2c_smbus_read_byte_data(client,
550 LM63_REG_REMOTE_TCRIT);
551 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
552 LM63_REG_REMOTE_TCRIT_HYST);
553
554 data->alarms = i2c_smbus_read_byte_data(client,
555 LM63_REG_ALERT_STATUS) & 0x7F;
556
557 data->last_updated = jiffies;
558 data->valid = 1;
559 }
560
561 up(&data->update_lock);
562
563 return data;
564}
565
566static int __init sensors_lm63_init(void)
567{
568 return i2c_add_driver(&lm63_driver);
569}
570
571static void __exit sensors_lm63_exit(void)
572{
573 i2c_del_driver(&lm63_driver);
574}
575
576MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
577MODULE_DESCRIPTION("LM63 driver");
578MODULE_LICENSE("GPL");
579
580module_init(sensors_lm63_init);
581module_exit(sensors_lm63_exit);