2 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3 * with integrated fan control
4 * Copyright (C) 2004-2008 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/hwmon-sysfs.h>
46 #include <linux/hwmon.h>
47 #include <linux/err.h>
48 #include <linux/mutex.h>
49 #include <linux/sysfs.h>
50 #include <linux/types.h>
54 * Address is fully defined internally and cannot be changed.
57 static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
63 #define LM63_REG_CONFIG1 0x03
64 #define LM63_REG_CONVRATE 0x04
65 #define LM63_REG_CONFIG2 0xBF
66 #define LM63_REG_CONFIG_FAN 0x4A
68 #define LM63_REG_TACH_COUNT_MSB 0x47
69 #define LM63_REG_TACH_COUNT_LSB 0x46
70 #define LM63_REG_TACH_LIMIT_MSB 0x49
71 #define LM63_REG_TACH_LIMIT_LSB 0x48
73 #define LM63_REG_PWM_VALUE 0x4C
74 #define LM63_REG_PWM_FREQ 0x4D
76 #define LM63_REG_LOCAL_TEMP 0x00
77 #define LM63_REG_LOCAL_HIGH 0x05
79 #define LM63_REG_REMOTE_TEMP_MSB 0x01
80 #define LM63_REG_REMOTE_TEMP_LSB 0x10
81 #define LM63_REG_REMOTE_OFFSET_MSB 0x11
82 #define LM63_REG_REMOTE_OFFSET_LSB 0x12
83 #define LM63_REG_REMOTE_HIGH_MSB 0x07
84 #define LM63_REG_REMOTE_HIGH_LSB 0x13
85 #define LM63_REG_REMOTE_LOW_MSB 0x08
86 #define LM63_REG_REMOTE_LOW_LSB 0x14
87 #define LM63_REG_REMOTE_TCRIT 0x19
88 #define LM63_REG_REMOTE_TCRIT_HYST 0x21
90 #define LM63_REG_ALERT_STATUS 0x02
91 #define LM63_REG_ALERT_MASK 0x16
93 #define LM63_REG_MAN_ID 0xFE
94 #define LM63_REG_CHIP_ID 0xFF
96 #define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
97 #define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
98 #define LM96163_REG_CONFIG_ENHANCED 0x45
100 #define LM63_MAX_CONVRATE 9
102 #define LM63_MAX_CONVRATE_HZ 32
103 #define LM96163_MAX_CONVRATE_HZ 26
106 * Conversions and various macros
107 * For tachometer counts, the LM63 uses 16-bit values.
108 * For local temperature and high limit, remote critical limit and hysteresis
109 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
110 * For remote temperature, low and high limits, it uses signed 11-bit values
111 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
112 * For LM64 the actual remote diode temperature is 16 degree Celsius higher
113 * than the register reading. Remote temperature setpoints have to be
114 * adapted accordingly.
117 #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
119 #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
120 (5400000 / (val)) & 0xFFFC)
121 #define TEMP8_FROM_REG(reg) ((reg) * 1000)
122 #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
123 (val) >= 127000 ? 127 : \
124 (val) < 0 ? ((val) - 500) / 1000 : \
125 ((val) + 500) / 1000)
126 #define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \
127 (val) >= 255000 ? 255 : \
128 ((val) + 500) / 1000)
129 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
130 #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
131 (val) >= 127875 ? 0x7FE0 : \
132 (val) < 0 ? ((val) - 62) / 125 * 32 : \
133 ((val) + 62) / 125 * 32)
134 #define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \
135 (val) >= 255875 ? 0xFFE0 : \
136 ((val) + 62) / 125 * 32)
137 #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
138 (val) >= 127000 ? 127 : \
139 ((val) + 500) / 1000)
141 #define UPDATE_INTERVAL(max, rate) \
142 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
145 * Functions declaration
148 static int lm63_probe(struct i2c_client *client,
149 const struct i2c_device_id *id);
150 static int lm63_remove(struct i2c_client *client);
152 static struct lm63_data *lm63_update_device(struct device *dev);
154 static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info);
155 static void lm63_init_client(struct i2c_client *client);
157 enum chips { lm63, lm64, lm96163 };
160 * Driver data (common to all clients)
163 static const struct i2c_device_id lm63_id[] = {
166 { "lm96163", lm96163 },
169 MODULE_DEVICE_TABLE(i2c, lm63_id);
171 static struct i2c_driver lm63_driver = {
172 .class = I2C_CLASS_HWMON,
177 .remove = lm63_remove,
179 .detect = lm63_detect,
180 .address_list = normal_i2c,
184 * Client data (each client gets its own)
188 struct device *hwmon_dev;
189 struct mutex update_lock;
190 char valid; /* zero until following fields are valid */
191 unsigned long last_updated; /* in jiffies */
195 int update_interval; /* in milliseconds */
198 /* registers values */
199 u8 config, config_fan;
200 u16 fan[2]; /* 0: input
204 s8 temp8[3]; /* 0: local input
206 2: remote critical limit */
207 s16 temp11[4]; /* 0: remote input
211 u16 temp11u; /* remote input (unsigned) */
215 bool remote_unsigned; /* true if unsigned remote upper limits */
218 static inline int temp8_from_reg(struct lm63_data *data, int nr)
220 if (data->remote_unsigned)
221 return TEMP8_FROM_REG((u8)data->temp8[nr]);
222 return TEMP8_FROM_REG(data->temp8[nr]);
226 * Sysfs callback functions and files
229 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
232 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
233 struct lm63_data *data = lm63_update_device(dev);
234 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
237 static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
238 const char *buf, size_t count)
240 struct i2c_client *client = to_i2c_client(dev);
241 struct lm63_data *data = i2c_get_clientdata(client);
245 err = kstrtoul(buf, 10, &val);
249 mutex_lock(&data->update_lock);
250 data->fan[1] = FAN_TO_REG(val);
251 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
252 data->fan[1] & 0xFF);
253 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
255 mutex_unlock(&data->update_lock);
259 static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
262 struct lm63_data *data = lm63_update_device(dev);
265 if (data->pwm_highres)
266 pwm = data->pwm1_value;
268 pwm = data->pwm1_value >= 2 * data->pwm1_freq ?
269 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
270 (2 * data->pwm1_freq);
272 return sprintf(buf, "%d\n", pwm);
275 static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
276 const char *buf, size_t count)
278 struct i2c_client *client = to_i2c_client(dev);
279 struct lm63_data *data = i2c_get_clientdata(client);
283 if (!(data->config_fan & 0x20)) /* register is read-only */
286 err = kstrtoul(buf, 10, &val);
290 val = SENSORS_LIMIT(val, 0, 255);
291 mutex_lock(&data->update_lock);
292 data->pwm1_value = data->pwm_highres ? val :
293 (val * data->pwm1_freq * 2 + 127) / 255;
294 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
295 mutex_unlock(&data->update_lock);
299 static ssize_t show_pwm1_enable(struct device *dev,
300 struct device_attribute *dummy, char *buf)
302 struct lm63_data *data = lm63_update_device(dev);
303 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
307 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
308 * For remote sensor registers temp2_offset has to be considered,
309 * for local sensor it must not.
310 * So we need separate 8bit accessors for local and remote sensor.
312 static ssize_t show_local_temp8(struct device *dev,
313 struct device_attribute *devattr,
316 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
317 struct lm63_data *data = lm63_update_device(dev);
318 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
321 static ssize_t show_remote_temp8(struct device *dev,
322 struct device_attribute *devattr,
325 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
326 struct lm63_data *data = lm63_update_device(dev);
327 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
328 + data->temp2_offset);
331 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
332 const char *buf, size_t count)
334 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
335 struct i2c_client *client = to_i2c_client(dev);
336 struct lm63_data *data = i2c_get_clientdata(client);
337 int nr = attr->index;
338 int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH;
343 err = kstrtol(buf, 10, &val);
347 mutex_lock(&data->update_lock);
349 if (data->remote_unsigned)
350 temp = TEMP8U_TO_REG(val - data->temp2_offset);
352 temp = TEMP8_TO_REG(val - data->temp2_offset);
354 temp = TEMP8_TO_REG(val);
356 data->temp8[nr] = temp;
357 i2c_smbus_write_byte_data(client, reg, temp);
358 mutex_unlock(&data->update_lock);
362 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
365 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
366 struct lm63_data *data = lm63_update_device(dev);
367 int nr = attr->index;
372 * Use unsigned temperature unless its value is zero.
373 * If it is zero, use signed temperature.
376 temp = TEMP11_FROM_REG(data->temp11u);
378 temp = TEMP11_FROM_REG(data->temp11[nr]);
380 if (data->remote_unsigned && nr == 2)
381 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
383 temp = TEMP11_FROM_REG(data->temp11[nr]);
385 return sprintf(buf, "%d\n", temp + data->temp2_offset);
388 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
389 const char *buf, size_t count)
391 static const u8 reg[6] = {
392 LM63_REG_REMOTE_LOW_MSB,
393 LM63_REG_REMOTE_LOW_LSB,
394 LM63_REG_REMOTE_HIGH_MSB,
395 LM63_REG_REMOTE_HIGH_LSB,
396 LM63_REG_REMOTE_OFFSET_MSB,
397 LM63_REG_REMOTE_OFFSET_LSB,
400 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
401 struct i2c_client *client = to_i2c_client(dev);
402 struct lm63_data *data = i2c_get_clientdata(client);
405 int nr = attr->index;
407 err = kstrtol(buf, 10, &val);
411 mutex_lock(&data->update_lock);
412 if (data->remote_unsigned && nr == 2)
413 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
415 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
417 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
418 data->temp11[nr] >> 8);
419 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
420 data->temp11[nr] & 0xff);
421 mutex_unlock(&data->update_lock);
426 * Hysteresis register holds a relative value, while we want to present
427 * an absolute to user-space
429 static ssize_t show_temp2_crit_hyst(struct device *dev,
430 struct device_attribute *dummy, char *buf)
432 struct lm63_data *data = lm63_update_device(dev);
433 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
435 - TEMP8_FROM_REG(data->temp2_crit_hyst));
439 * And now the other way around, user-space provides an absolute
440 * hysteresis value and we have to store a relative one
442 static ssize_t set_temp2_crit_hyst(struct device *dev,
443 struct device_attribute *dummy,
444 const char *buf, size_t count)
446 struct i2c_client *client = to_i2c_client(dev);
447 struct lm63_data *data = i2c_get_clientdata(client);
452 err = kstrtol(buf, 10, &val);
456 mutex_lock(&data->update_lock);
457 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
458 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
460 mutex_unlock(&data->update_lock);
465 * Set conversion rate.
466 * client->update_lock must be held when calling this function.
468 static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data,
469 unsigned int interval)
472 unsigned int update_interval;
474 /* Shift calculations to avoid rounding errors */
477 /* find the nearest update rate */
478 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
479 / data->max_convrate_hz;
480 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
481 if (interval >= update_interval * 3 / 4)
484 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
485 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
488 static ssize_t show_update_interval(struct device *dev,
489 struct device_attribute *attr, char *buf)
491 struct lm63_data *data = dev_get_drvdata(dev);
493 return sprintf(buf, "%u\n", data->update_interval);
496 static ssize_t set_update_interval(struct device *dev,
497 struct device_attribute *attr,
498 const char *buf, size_t count)
500 struct i2c_client *client = to_i2c_client(dev);
501 struct lm63_data *data = i2c_get_clientdata(client);
505 err = kstrtoul(buf, 10, &val);
509 mutex_lock(&data->update_lock);
510 lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000));
511 mutex_unlock(&data->update_lock);
516 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
519 struct lm63_data *data = lm63_update_device(dev);
520 return sprintf(buf, "%u\n", data->alarms);
523 static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
526 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
527 struct lm63_data *data = lm63_update_device(dev);
528 int bitnr = attr->index;
530 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
533 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
534 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
537 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
538 static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
540 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
541 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
544 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
545 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
547 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
549 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
551 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
553 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
554 set_temp2_crit_hyst);
556 /* Individual alarm files */
557 static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
558 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
559 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
560 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
561 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
562 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
563 /* Raw alarm file for compatibility */
564 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
566 static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
567 set_update_interval);
569 static struct attribute *lm63_attributes[] = {
571 &dev_attr_pwm1_enable.attr,
572 &sensor_dev_attr_temp1_input.dev_attr.attr,
573 &sensor_dev_attr_temp2_input.dev_attr.attr,
574 &sensor_dev_attr_temp2_min.dev_attr.attr,
575 &sensor_dev_attr_temp1_max.dev_attr.attr,
576 &sensor_dev_attr_temp2_max.dev_attr.attr,
577 &sensor_dev_attr_temp2_offset.dev_attr.attr,
578 &sensor_dev_attr_temp2_crit.dev_attr.attr,
579 &dev_attr_temp2_crit_hyst.attr,
581 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
582 &sensor_dev_attr_temp2_fault.dev_attr.attr,
583 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
584 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
585 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
586 &dev_attr_alarms.attr,
587 &dev_attr_update_interval.attr,
592 * On LM63, temp2_crit can be set only once, which should be job
594 * On LM64, temp2_crit can always be set.
595 * On LM96163, temp2_crit can be set if bit 1 of the configuration
598 static umode_t lm63_attribute_mode(struct kobject *kobj,
599 struct attribute *attr, int index)
601 struct device *dev = container_of(kobj, struct device, kobj);
602 struct i2c_client *client = to_i2c_client(dev);
603 struct lm63_data *data = i2c_get_clientdata(client);
605 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
606 && (data->kind == lm64 ||
607 (data->kind == lm96163 && (data->config & 0x02))))
608 return attr->mode | S_IWUSR;
613 static const struct attribute_group lm63_group = {
614 .is_visible = lm63_attribute_mode,
615 .attrs = lm63_attributes,
618 static struct attribute *lm63_attributes_fan1[] = {
619 &sensor_dev_attr_fan1_input.dev_attr.attr,
620 &sensor_dev_attr_fan1_min.dev_attr.attr,
622 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
626 static const struct attribute_group lm63_group_fan1 = {
627 .attrs = lm63_attributes_fan1,
634 /* Return 0 if detection is successful, -ENODEV otherwise */
635 static int lm63_detect(struct i2c_client *new_client,
636 struct i2c_board_info *info)
638 struct i2c_adapter *adapter = new_client->adapter;
639 u8 man_id, chip_id, reg_config1, reg_config2;
640 u8 reg_alert_status, reg_alert_mask;
641 int address = new_client->addr;
643 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
646 man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
647 chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
649 reg_config1 = i2c_smbus_read_byte_data(new_client,
651 reg_config2 = i2c_smbus_read_byte_data(new_client,
653 reg_alert_status = i2c_smbus_read_byte_data(new_client,
654 LM63_REG_ALERT_STATUS);
655 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
656 LM63_REG_ALERT_MASK);
658 if (man_id != 0x01 /* National Semiconductor */
659 || (reg_config1 & 0x18) != 0x00
660 || (reg_config2 & 0xF8) != 0x00
661 || (reg_alert_status & 0x20) != 0x00
662 || (reg_alert_mask & 0xA4) != 0xA4) {
663 dev_dbg(&adapter->dev,
664 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
669 if (chip_id == 0x41 && address == 0x4c)
670 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
671 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
672 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
673 else if (chip_id == 0x49 && address == 0x4c)
674 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
681 static int lm63_probe(struct i2c_client *new_client,
682 const struct i2c_device_id *id)
684 struct lm63_data *data;
687 data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
693 i2c_set_clientdata(new_client, data);
695 mutex_init(&data->update_lock);
697 /* Set the device type */
698 data->kind = id->driver_data;
699 if (data->kind == lm64)
700 data->temp2_offset = 16000;
702 /* Initialize chip */
703 lm63_init_client(new_client);
705 /* Register sysfs hooks */
706 err = sysfs_create_group(&new_client->dev.kobj, &lm63_group);
709 if (data->config & 0x04) { /* tachometer enabled */
710 err = sysfs_create_group(&new_client->dev.kobj,
713 goto exit_remove_files;
716 data->hwmon_dev = hwmon_device_register(&new_client->dev);
717 if (IS_ERR(data->hwmon_dev)) {
718 err = PTR_ERR(data->hwmon_dev);
719 goto exit_remove_files;
725 sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
726 sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
734 * Ideally we shouldn't have to initialize anything, since the BIOS
735 * should have taken care of everything
737 static void lm63_init_client(struct i2c_client *client)
739 struct lm63_data *data = i2c_get_clientdata(client);
742 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
743 data->config_fan = i2c_smbus_read_byte_data(client,
744 LM63_REG_CONFIG_FAN);
746 /* Start converting if needed */
747 if (data->config & 0x40) { /* standby */
748 dev_dbg(&client->dev, "Switching to operational mode\n");
749 data->config &= 0xA7;
750 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
754 /* We may need pwm1_freq before ever updating the client data */
755 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
756 if (data->pwm1_freq == 0)
759 switch (data->kind) {
762 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
765 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
768 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
769 if (unlikely(convrate > LM63_MAX_CONVRATE))
770 convrate = LM63_MAX_CONVRATE;
771 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
775 * For LM96163, check if high resolution PWM
776 * and unsigned temperature format is enabled.
778 if (data->kind == lm96163) {
780 = i2c_smbus_read_byte_data(client,
781 LM96163_REG_CONFIG_ENHANCED);
782 if ((config_enhanced & 0x10)
783 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
784 data->pwm_highres = true;
785 if (config_enhanced & 0x08)
786 data->remote_unsigned = true;
789 /* Show some debug info about the LM63 configuration */
790 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
791 (data->config & 0x04) ? "tachometer input" :
793 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
794 (data->config_fan & 0x08) ? "1.4" : "360",
795 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
796 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
797 (data->config_fan & 0x10) ? "low" : "high",
798 (data->config_fan & 0x20) ? "manual" : "auto");
801 static int lm63_remove(struct i2c_client *client)
803 struct lm63_data *data = i2c_get_clientdata(client);
805 hwmon_device_unregister(data->hwmon_dev);
806 sysfs_remove_group(&client->dev.kobj, &lm63_group);
807 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
813 static struct lm63_data *lm63_update_device(struct device *dev)
815 struct i2c_client *client = to_i2c_client(dev);
816 struct lm63_data *data = i2c_get_clientdata(client);
817 unsigned long next_update;
819 mutex_lock(&data->update_lock);
821 next_update = data->last_updated
822 + msecs_to_jiffies(data->update_interval) + 1;
824 if (time_after(jiffies, next_update) || !data->valid) {
825 if (data->config & 0x04) { /* tachometer enabled */
826 /* order matters for fan1_input */
827 data->fan[0] = i2c_smbus_read_byte_data(client,
828 LM63_REG_TACH_COUNT_LSB) & 0xFC;
829 data->fan[0] |= i2c_smbus_read_byte_data(client,
830 LM63_REG_TACH_COUNT_MSB) << 8;
831 data->fan[1] = (i2c_smbus_read_byte_data(client,
832 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
833 | (i2c_smbus_read_byte_data(client,
834 LM63_REG_TACH_LIMIT_MSB) << 8);
837 data->pwm1_freq = i2c_smbus_read_byte_data(client,
839 if (data->pwm1_freq == 0)
841 data->pwm1_value = i2c_smbus_read_byte_data(client,
844 data->temp8[0] = i2c_smbus_read_byte_data(client,
845 LM63_REG_LOCAL_TEMP);
846 data->temp8[1] = i2c_smbus_read_byte_data(client,
847 LM63_REG_LOCAL_HIGH);
849 /* order matters for temp2_input */
850 data->temp11[0] = i2c_smbus_read_byte_data(client,
851 LM63_REG_REMOTE_TEMP_MSB) << 8;
852 data->temp11[0] |= i2c_smbus_read_byte_data(client,
853 LM63_REG_REMOTE_TEMP_LSB);
854 data->temp11[1] = (i2c_smbus_read_byte_data(client,
855 LM63_REG_REMOTE_LOW_MSB) << 8)
856 | i2c_smbus_read_byte_data(client,
857 LM63_REG_REMOTE_LOW_LSB);
858 data->temp11[2] = (i2c_smbus_read_byte_data(client,
859 LM63_REG_REMOTE_HIGH_MSB) << 8)
860 | i2c_smbus_read_byte_data(client,
861 LM63_REG_REMOTE_HIGH_LSB);
862 data->temp11[3] = (i2c_smbus_read_byte_data(client,
863 LM63_REG_REMOTE_OFFSET_MSB) << 8)
864 | i2c_smbus_read_byte_data(client,
865 LM63_REG_REMOTE_OFFSET_LSB);
867 if (data->kind == lm96163)
868 data->temp11u = (i2c_smbus_read_byte_data(client,
869 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
870 | i2c_smbus_read_byte_data(client,
871 LM96163_REG_REMOTE_TEMP_U_LSB);
873 data->temp8[2] = i2c_smbus_read_byte_data(client,
874 LM63_REG_REMOTE_TCRIT);
875 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
876 LM63_REG_REMOTE_TCRIT_HYST);
878 data->alarms = i2c_smbus_read_byte_data(client,
879 LM63_REG_ALERT_STATUS) & 0x7F;
881 data->last_updated = jiffies;
885 mutex_unlock(&data->update_lock);
890 static int __init sensors_lm63_init(void)
892 return i2c_add_driver(&lm63_driver);
895 static void __exit sensors_lm63_exit(void)
897 i2c_del_driver(&lm63_driver);
900 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
901 MODULE_DESCRIPTION("LM63 driver");
902 MODULE_LICENSE("GPL");
904 module_init(sensors_lm63_init);
905 module_exit(sensors_lm63_exit);