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.
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
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.
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
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.
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.
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.
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/jiffies.h>
44 #include <linux/i2c.h>
45 #include <linux/i2c-sensor.h>
49 * Address is fully defined internally and cannot be changed.
52 static unsigned short normal_i2c
[] = { 0x4c, I2C_CLIENT_END
};
53 static unsigned int normal_isa
[] = { I2C_CLIENT_ISA_END
};
59 SENSORS_INSMOD_1(lm63
);
65 #define LM63_REG_CONFIG1 0x03
66 #define LM63_REG_CONFIG2 0xBF
67 #define LM63_REG_CONFIG_FAN 0x4A
69 #define LM63_REG_TACH_COUNT_MSB 0x47
70 #define LM63_REG_TACH_COUNT_LSB 0x46
71 #define LM63_REG_TACH_LIMIT_MSB 0x49
72 #define LM63_REG_TACH_LIMIT_LSB 0x48
74 #define LM63_REG_PWM_VALUE 0x4C
75 #define LM63_REG_PWM_FREQ 0x4D
77 #define LM63_REG_LOCAL_TEMP 0x00
78 #define LM63_REG_LOCAL_HIGH 0x05
80 #define LM63_REG_REMOTE_TEMP_MSB 0x01
81 #define LM63_REG_REMOTE_TEMP_LSB 0x10
82 #define LM63_REG_REMOTE_OFFSET_MSB 0x11
83 #define LM63_REG_REMOTE_OFFSET_LSB 0x12
84 #define LM63_REG_REMOTE_HIGH_MSB 0x07
85 #define LM63_REG_REMOTE_HIGH_LSB 0x13
86 #define LM63_REG_REMOTE_LOW_MSB 0x08
87 #define LM63_REG_REMOTE_LOW_LSB 0x14
88 #define LM63_REG_REMOTE_TCRIT 0x19
89 #define LM63_REG_REMOTE_TCRIT_HYST 0x21
91 #define LM63_REG_ALERT_STATUS 0x02
92 #define LM63_REG_ALERT_MASK 0x16
94 #define LM63_REG_MAN_ID 0xFE
95 #define LM63_REG_CHIP_ID 0xFF
98 * Conversions and various macros
99 * For tachometer counts, the LM63 uses 16-bit values.
100 * For local temperature and high limit, remote critical limit and hysteresis
101 * value, it uses signed 8-bit values with LSB = 1 degree Celcius.
102 * For remote temperature, low and high limits, it uses signed 11-bit values
103 * with LSB = 0.125 degree Celcius, left-justified in 16-bit registers.
106 #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
108 #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
109 (5400000 / (val)) & 0xFFFC)
110 #define TEMP8_FROM_REG(reg) ((reg) * 1000)
111 #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
112 (val) >= 127000 ? 127 : \
113 (val) < 0 ? ((val) - 500) / 1000 : \
114 ((val) + 500) / 1000)
115 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
116 #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
117 (val) >= 127875 ? 0x7FE0 : \
118 (val) < 0 ? ((val) - 62) / 125 * 32 : \
119 ((val) + 62) / 125 * 32)
120 #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
121 (val) >= 127000 ? 127 : \
122 ((val) + 500) / 1000)
125 * Functions declaration
128 static int lm63_attach_adapter(struct i2c_adapter
*adapter
);
129 static int lm63_detach_client(struct i2c_client
*client
);
131 static struct lm63_data
*lm63_update_device(struct device
*dev
);
133 static int lm63_detect(struct i2c_adapter
*adapter
, int address
, int kind
);
134 static void lm63_init_client(struct i2c_client
*client
);
137 * Driver data (common to all clients)
140 static struct i2c_driver lm63_driver
= {
141 .owner
= THIS_MODULE
,
143 .flags
= I2C_DF_NOTIFY
,
144 .attach_adapter
= lm63_attach_adapter
,
145 .detach_client
= lm63_detach_client
,
149 * Client data (each client gets its own)
153 struct i2c_client client
;
154 struct semaphore update_lock
;
155 char valid
; /* zero until following fields are valid */
156 unsigned long last_updated
; /* in jiffies */
158 /* registers values */
159 u8 config
, config_fan
;
175 * Sysfs callback functions and files
178 #define show_fan(value) \
179 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
181 struct lm63_data *data = lm63_update_device(dev); \
182 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value)); \
184 show_fan(fan1_input
);
187 static ssize_t
set_fan1_low(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
190 struct i2c_client
*client
= to_i2c_client(dev
);
191 struct lm63_data
*data
= i2c_get_clientdata(client
);
192 unsigned long val
= simple_strtoul(buf
, NULL
, 10);
194 down(&data
->update_lock
);
195 data
->fan1_low
= FAN_TO_REG(val
);
196 i2c_smbus_write_byte_data(client
, LM63_REG_TACH_LIMIT_LSB
,
197 data
->fan1_low
& 0xFF);
198 i2c_smbus_write_byte_data(client
, LM63_REG_TACH_LIMIT_MSB
,
199 data
->fan1_low
>> 8);
200 up(&data
->update_lock
);
204 static ssize_t
show_pwm1(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
206 struct lm63_data
*data
= lm63_update_device(dev
);
207 return sprintf(buf
, "%d\n", data
->pwm1_value
>= 2 * data
->pwm1_freq
?
208 255 : (data
->pwm1_value
* 255 + data
->pwm1_freq
) /
209 (2 * data
->pwm1_freq
));
212 static ssize_t
set_pwm1(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
214 struct i2c_client
*client
= to_i2c_client(dev
);
215 struct lm63_data
*data
= i2c_get_clientdata(client
);
218 if (!(data
->config_fan
& 0x20)) /* register is read-only */
221 val
= simple_strtoul(buf
, NULL
, 10);
222 down(&data
->update_lock
);
223 data
->pwm1_value
= val
<= 0 ? 0 :
224 val
>= 255 ? 2 * data
->pwm1_freq
:
225 (val
* data
->pwm1_freq
* 2 + 127) / 255;
226 i2c_smbus_write_byte_data(client
, LM63_REG_PWM_VALUE
, data
->pwm1_value
);
227 up(&data
->update_lock
);
231 static ssize_t
show_pwm1_enable(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
233 struct lm63_data
*data
= lm63_update_device(dev
);
234 return sprintf(buf
, "%d\n", data
->config_fan
& 0x20 ? 1 : 2);
237 #define show_temp8(value) \
238 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
240 struct lm63_data *data = lm63_update_device(dev); \
241 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->value)); \
243 #define show_temp11(value) \
244 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
246 struct lm63_data *data = lm63_update_device(dev); \
247 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->value)); \
249 show_temp8(temp1_input
);
250 show_temp8(temp1_high
);
251 show_temp11(temp2_input
);
252 show_temp11(temp2_high
);
253 show_temp11(temp2_low
);
254 show_temp8(temp2_crit
);
256 #define set_temp8(value, reg) \
257 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \
260 struct i2c_client *client = to_i2c_client(dev); \
261 struct lm63_data *data = i2c_get_clientdata(client); \
262 long val = simple_strtol(buf, NULL, 10); \
264 down(&data->update_lock); \
265 data->value = TEMP8_TO_REG(val); \
266 i2c_smbus_write_byte_data(client, reg, data->value); \
267 up(&data->update_lock); \
270 #define set_temp11(value, reg_msb, reg_lsb) \
271 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \
274 struct i2c_client *client = to_i2c_client(dev); \
275 struct lm63_data *data = i2c_get_clientdata(client); \
276 long val = simple_strtol(buf, NULL, 10); \
278 down(&data->update_lock); \
279 data->value = TEMP11_TO_REG(val); \
280 i2c_smbus_write_byte_data(client, reg_msb, data->value >> 8); \
281 i2c_smbus_write_byte_data(client, reg_lsb, data->value & 0xff); \
282 up(&data->update_lock); \
285 set_temp8(temp1_high
, LM63_REG_LOCAL_HIGH
);
286 set_temp11(temp2_high
, LM63_REG_REMOTE_HIGH_MSB
, LM63_REG_REMOTE_HIGH_LSB
);
287 set_temp11(temp2_low
, LM63_REG_REMOTE_LOW_MSB
, LM63_REG_REMOTE_LOW_LSB
);
289 /* Hysteresis register holds a relative value, while we want to present
290 an absolute to user-space */
291 static ssize_t
show_temp2_crit_hyst(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
293 struct lm63_data
*data
= lm63_update_device(dev
);
294 return sprintf(buf
, "%d\n", TEMP8_FROM_REG(data
->temp2_crit
)
295 - TEMP8_FROM_REG(data
->temp2_crit_hyst
));
298 /* And now the other way around, user-space provides an absolute
299 hysteresis value and we have to store a relative one */
300 static ssize_t
set_temp2_crit_hyst(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
303 struct i2c_client
*client
= to_i2c_client(dev
);
304 struct lm63_data
*data
= i2c_get_clientdata(client
);
305 long val
= simple_strtol(buf
, NULL
, 10);
308 down(&data
->update_lock
);
309 hyst
= TEMP8_FROM_REG(data
->temp2_crit
) - val
;
310 i2c_smbus_write_byte_data(client
, LM63_REG_REMOTE_TCRIT_HYST
,
312 up(&data
->update_lock
);
316 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
318 struct lm63_data
*data
= lm63_update_device(dev
);
319 return sprintf(buf
, "%u\n", data
->alarms
);
322 static DEVICE_ATTR(fan1_input
, S_IRUGO
, show_fan1_input
, NULL
);
323 static DEVICE_ATTR(fan1_min
, S_IWUSR
| S_IRUGO
, show_fan1_low
,
326 static DEVICE_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, show_pwm1
, set_pwm1
);
327 static DEVICE_ATTR(pwm1_enable
, S_IRUGO
, show_pwm1_enable
, NULL
);
329 static DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp1_input
, NULL
);
330 static DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
, show_temp1_high
,
333 static DEVICE_ATTR(temp2_input
, S_IRUGO
, show_temp2_input
, NULL
);
334 static DEVICE_ATTR(temp2_min
, S_IWUSR
| S_IRUGO
, show_temp2_low
,
336 static DEVICE_ATTR(temp2_max
, S_IWUSR
| S_IRUGO
, show_temp2_high
,
338 static DEVICE_ATTR(temp2_crit
, S_IRUGO
, show_temp2_crit
, NULL
);
339 static DEVICE_ATTR(temp2_crit_hyst
, S_IWUSR
| S_IRUGO
, show_temp2_crit_hyst
,
340 set_temp2_crit_hyst
);
342 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
348 static int lm63_attach_adapter(struct i2c_adapter
*adapter
)
350 if (!(adapter
->class & I2C_CLASS_HWMON
))
352 return i2c_detect(adapter
, &addr_data
, lm63_detect
);
356 * The following function does more than just detection. If detection
357 * succeeds, it also registers the new chip.
359 static int lm63_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
361 struct i2c_client
*new_client
;
362 struct lm63_data
*data
;
365 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
368 if (!(data
= kmalloc(sizeof(struct lm63_data
), GFP_KERNEL
))) {
372 memset(data
, 0, sizeof(struct lm63_data
));
374 /* The common I2C client data is placed right before the
375 LM63-specific data. */
376 new_client
= &data
->client
;
377 i2c_set_clientdata(new_client
, data
);
378 new_client
->addr
= address
;
379 new_client
->adapter
= adapter
;
380 new_client
->driver
= &lm63_driver
;
381 new_client
->flags
= 0;
383 /* Default to an LM63 if forced */
387 if (kind
< 0) { /* must identify */
388 u8 man_id
, chip_id
, reg_config1
, reg_config2
;
389 u8 reg_alert_status
, reg_alert_mask
;
391 man_id
= i2c_smbus_read_byte_data(new_client
,
393 chip_id
= i2c_smbus_read_byte_data(new_client
,
395 reg_config1
= i2c_smbus_read_byte_data(new_client
,
397 reg_config2
= i2c_smbus_read_byte_data(new_client
,
399 reg_alert_status
= i2c_smbus_read_byte_data(new_client
,
400 LM63_REG_ALERT_STATUS
);
401 reg_alert_mask
= i2c_smbus_read_byte_data(new_client
,
402 LM63_REG_ALERT_MASK
);
404 if (man_id
== 0x01 /* National Semiconductor */
405 && chip_id
== 0x41 /* LM63 */
406 && (reg_config1
& 0x18) == 0x00
407 && (reg_config2
& 0xF8) == 0x00
408 && (reg_alert_status
& 0x20) == 0x00
409 && (reg_alert_mask
& 0xA4) == 0xA4) {
411 } else { /* failed */
412 dev_dbg(&adapter
->dev
, "Unsupported chip "
413 "(man_id=0x%02X, chip_id=0x%02X).\n",
419 strlcpy(new_client
->name
, "lm63", I2C_NAME_SIZE
);
421 init_MUTEX(&data
->update_lock
);
423 /* Tell the I2C layer a new client has arrived */
424 if ((err
= i2c_attach_client(new_client
)))
427 /* Initialize the LM63 chip */
428 lm63_init_client(new_client
);
430 /* Register sysfs hooks */
431 if (data
->config
& 0x04) { /* tachometer enabled */
432 device_create_file(&new_client
->dev
, &dev_attr_fan1_input
);
433 device_create_file(&new_client
->dev
, &dev_attr_fan1_min
);
435 device_create_file(&new_client
->dev
, &dev_attr_pwm1
);
436 device_create_file(&new_client
->dev
, &dev_attr_pwm1_enable
);
437 device_create_file(&new_client
->dev
, &dev_attr_temp1_input
);
438 device_create_file(&new_client
->dev
, &dev_attr_temp2_input
);
439 device_create_file(&new_client
->dev
, &dev_attr_temp2_min
);
440 device_create_file(&new_client
->dev
, &dev_attr_temp1_max
);
441 device_create_file(&new_client
->dev
, &dev_attr_temp2_max
);
442 device_create_file(&new_client
->dev
, &dev_attr_temp2_crit
);
443 device_create_file(&new_client
->dev
, &dev_attr_temp2_crit_hyst
);
444 device_create_file(&new_client
->dev
, &dev_attr_alarms
);
454 /* Idealy we shouldn't have to initialize anything, since the BIOS
455 should have taken care of everything */
456 static void lm63_init_client(struct i2c_client
*client
)
458 struct lm63_data
*data
= i2c_get_clientdata(client
);
460 data
->config
= i2c_smbus_read_byte_data(client
, LM63_REG_CONFIG1
);
461 data
->config_fan
= i2c_smbus_read_byte_data(client
,
462 LM63_REG_CONFIG_FAN
);
464 /* Start converting if needed */
465 if (data
->config
& 0x40) { /* standby */
466 dev_dbg(&client
->dev
, "Switching to operational mode");
467 data
->config
&= 0xA7;
468 i2c_smbus_write_byte_data(client
, LM63_REG_CONFIG1
,
472 /* We may need pwm1_freq before ever updating the client data */
473 data
->pwm1_freq
= i2c_smbus_read_byte_data(client
, LM63_REG_PWM_FREQ
);
474 if (data
->pwm1_freq
== 0)
477 /* Show some debug info about the LM63 configuration */
478 dev_dbg(&client
->dev
, "Alert/tach pin configured for %s\n",
479 (data
->config
& 0x04) ? "tachometer input" :
481 dev_dbg(&client
->dev
, "PWM clock %s kHz, output frequency %u Hz\n",
482 (data
->config_fan
& 0x08) ? "1.4" : "360",
483 ((data
->config_fan
& 0x08) ? 700 : 180000) / data
->pwm1_freq
);
484 dev_dbg(&client
->dev
, "PWM output active %s, %s mode\n",
485 (data
->config_fan
& 0x10) ? "low" : "high",
486 (data
->config_fan
& 0x20) ? "manual" : "auto");
489 static int lm63_detach_client(struct i2c_client
*client
)
493 if ((err
= i2c_detach_client(client
))) {
494 dev_err(&client
->dev
, "Client deregistration failed, "
495 "client not detached\n");
499 kfree(i2c_get_clientdata(client
));
503 static struct lm63_data
*lm63_update_device(struct device
*dev
)
505 struct i2c_client
*client
= to_i2c_client(dev
);
506 struct lm63_data
*data
= i2c_get_clientdata(client
);
508 down(&data
->update_lock
);
510 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
511 if (data
->config
& 0x04) { /* tachometer enabled */
512 /* order matters for fan1_input */
513 data
->fan1_input
= i2c_smbus_read_byte_data(client
,
514 LM63_REG_TACH_COUNT_LSB
) & 0xFC;
515 data
->fan1_input
|= i2c_smbus_read_byte_data(client
,
516 LM63_REG_TACH_COUNT_MSB
) << 8;
517 data
->fan1_low
= (i2c_smbus_read_byte_data(client
,
518 LM63_REG_TACH_LIMIT_LSB
) & 0xFC)
519 | (i2c_smbus_read_byte_data(client
,
520 LM63_REG_TACH_LIMIT_MSB
) << 8);
523 data
->pwm1_freq
= i2c_smbus_read_byte_data(client
,
525 if (data
->pwm1_freq
== 0)
527 data
->pwm1_value
= i2c_smbus_read_byte_data(client
,
530 data
->temp1_input
= i2c_smbus_read_byte_data(client
,
531 LM63_REG_LOCAL_TEMP
);
532 data
->temp1_high
= i2c_smbus_read_byte_data(client
,
533 LM63_REG_LOCAL_HIGH
);
535 /* order matters for temp2_input */
536 data
->temp2_input
= i2c_smbus_read_byte_data(client
,
537 LM63_REG_REMOTE_TEMP_MSB
) << 8;
538 data
->temp2_input
|= i2c_smbus_read_byte_data(client
,
539 LM63_REG_REMOTE_TEMP_LSB
);
540 data
->temp2_high
= (i2c_smbus_read_byte_data(client
,
541 LM63_REG_REMOTE_HIGH_MSB
) << 8)
542 | i2c_smbus_read_byte_data(client
,
543 LM63_REG_REMOTE_HIGH_LSB
);
544 data
->temp2_low
= (i2c_smbus_read_byte_data(client
,
545 LM63_REG_REMOTE_LOW_MSB
) << 8)
546 | i2c_smbus_read_byte_data(client
,
547 LM63_REG_REMOTE_LOW_LSB
);
548 data
->temp2_crit
= i2c_smbus_read_byte_data(client
,
549 LM63_REG_REMOTE_TCRIT
);
550 data
->temp2_crit_hyst
= i2c_smbus_read_byte_data(client
,
551 LM63_REG_REMOTE_TCRIT_HYST
);
553 data
->alarms
= i2c_smbus_read_byte_data(client
,
554 LM63_REG_ALERT_STATUS
) & 0x7F;
556 data
->last_updated
= jiffies
;
560 up(&data
->update_lock
);
565 static int __init
sensors_lm63_init(void)
567 return i2c_add_driver(&lm63_driver
);
570 static void __exit
sensors_lm63_exit(void)
572 i2c_del_driver(&lm63_driver
);
575 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
576 MODULE_DESCRIPTION("LM63 driver");
577 MODULE_LICENSE("GPL");
579 module_init(sensors_lm63_init
);
580 module_exit(sensors_lm63_exit
);