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 except for
55 * LM64 which has one pin dedicated to address selection.
56 * LM63 and LM96163 have address 0x4c.
57 * LM64 can have address 0x18 or 0x4e.
60 static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
66 #define LM63_REG_CONFIG1 0x03
67 #define LM63_REG_CONVRATE 0x04
68 #define LM63_REG_CONFIG2 0xBF
69 #define LM63_REG_CONFIG_FAN 0x4A
71 #define LM63_REG_TACH_COUNT_MSB 0x47
72 #define LM63_REG_TACH_COUNT_LSB 0x46
73 #define LM63_REG_TACH_LIMIT_MSB 0x49
74 #define LM63_REG_TACH_LIMIT_LSB 0x48
76 #define LM63_REG_PWM_VALUE 0x4C
77 #define LM63_REG_PWM_FREQ 0x4D
78 #define LM63_REG_LUT_TEMP_HYST 0x4F
79 #define LM63_REG_LUT_TEMP(nr) (0x50 + 2 * (nr))
80 #define LM63_REG_LUT_PWM(nr) (0x51 + 2 * (nr))
82 #define LM63_REG_LOCAL_TEMP 0x00
83 #define LM63_REG_LOCAL_HIGH 0x05
85 #define LM63_REG_REMOTE_TEMP_MSB 0x01
86 #define LM63_REG_REMOTE_TEMP_LSB 0x10
87 #define LM63_REG_REMOTE_OFFSET_MSB 0x11
88 #define LM63_REG_REMOTE_OFFSET_LSB 0x12
89 #define LM63_REG_REMOTE_HIGH_MSB 0x07
90 #define LM63_REG_REMOTE_HIGH_LSB 0x13
91 #define LM63_REG_REMOTE_LOW_MSB 0x08
92 #define LM63_REG_REMOTE_LOW_LSB 0x14
93 #define LM63_REG_REMOTE_TCRIT 0x19
94 #define LM63_REG_REMOTE_TCRIT_HYST 0x21
96 #define LM63_REG_ALERT_STATUS 0x02
97 #define LM63_REG_ALERT_MASK 0x16
99 #define LM63_REG_MAN_ID 0xFE
100 #define LM63_REG_CHIP_ID 0xFF
102 #define LM96163_REG_TRUTHERM 0x30
103 #define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
104 #define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
105 #define LM96163_REG_CONFIG_ENHANCED 0x45
107 #define LM63_MAX_CONVRATE 9
109 #define LM63_MAX_CONVRATE_HZ 32
110 #define LM96163_MAX_CONVRATE_HZ 26
113 * Conversions and various macros
114 * For tachometer counts, the LM63 uses 16-bit values.
115 * For local temperature and high limit, remote critical limit and hysteresis
116 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
117 * For remote temperature, low and high limits, it uses signed 11-bit values
118 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
119 * For LM64 the actual remote diode temperature is 16 degree Celsius higher
120 * than the register reading. Remote temperature setpoints have to be
121 * adapted accordingly.
124 #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
126 #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
127 (5400000 / (val)) & 0xFFFC)
128 #define TEMP8_FROM_REG(reg) ((reg) * 1000)
129 #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
130 (val) >= 127000 ? 127 : \
131 (val) < 0 ? ((val) - 500) / 1000 : \
132 ((val) + 500) / 1000)
133 #define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \
134 (val) >= 255000 ? 255 : \
135 ((val) + 500) / 1000)
136 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
137 #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
138 (val) >= 127875 ? 0x7FE0 : \
139 (val) < 0 ? ((val) - 62) / 125 * 32 : \
140 ((val) + 62) / 125 * 32)
141 #define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \
142 (val) >= 255875 ? 0xFFE0 : \
143 ((val) + 62) / 125 * 32)
144 #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
145 (val) >= 127000 ? 127 : \
146 ((val) + 500) / 1000)
148 #define UPDATE_INTERVAL(max, rate) \
149 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
151 enum chips { lm63, lm64, lm96163 };
154 * Client data (each client gets its own)
158 struct device *hwmon_dev;
159 struct mutex update_lock;
160 char valid; /* zero until following fields are valid */
161 char lut_valid; /* zero until lut fields are valid */
162 unsigned long last_updated; /* in jiffies */
163 unsigned long lut_last_updated; /* in jiffies */
167 int update_interval; /* in milliseconds */
169 int lut_size; /* 8 or 12 */
171 /* registers values */
172 u8 config, config_fan;
173 u16 fan[2]; /* 0: input
176 u8 pwm1[13]; /* 0: current output
177 1-12: lookup table */
178 s8 temp8[15]; /* 0: local input
180 2: remote critical limit
181 3-14: lookup table */
182 s16 temp11[4]; /* 0: remote input
186 u16 temp11u; /* remote input (unsigned) */
191 bool lut_temp_highres;
192 bool remote_unsigned; /* true if unsigned remote upper limits */
196 static inline int temp8_from_reg(struct lm63_data *data, int nr)
198 if (data->remote_unsigned)
199 return TEMP8_FROM_REG((u8)data->temp8[nr]);
200 return TEMP8_FROM_REG(data->temp8[nr]);
203 static inline int lut_temp_from_reg(struct lm63_data *data, int nr)
205 return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000);
208 static inline int lut_temp_to_reg(struct lm63_data *data, long val)
210 val -= data->temp2_offset;
211 if (data->lut_temp_highres)
212 return DIV_ROUND_CLOSEST(SENSORS_LIMIT(val, 0, 127500), 500);
214 return DIV_ROUND_CLOSEST(SENSORS_LIMIT(val, 0, 127000), 1000);
218 * Update the lookup table register cache.
219 * client->update_lock must be held when calling this function.
221 static void lm63_update_lut(struct i2c_client *client)
223 struct lm63_data *data = i2c_get_clientdata(client);
226 if (time_after(jiffies, data->lut_last_updated + 5 * HZ) ||
228 for (i = 0; i < data->lut_size; i++) {
229 data->pwm1[1 + i] = i2c_smbus_read_byte_data(client,
230 LM63_REG_LUT_PWM(i));
231 data->temp8[3 + i] = i2c_smbus_read_byte_data(client,
232 LM63_REG_LUT_TEMP(i));
234 data->lut_temp_hyst = i2c_smbus_read_byte_data(client,
235 LM63_REG_LUT_TEMP_HYST);
237 data->lut_last_updated = jiffies;
242 static struct lm63_data *lm63_update_device(struct device *dev)
244 struct i2c_client *client = to_i2c_client(dev);
245 struct lm63_data *data = i2c_get_clientdata(client);
246 unsigned long next_update;
248 mutex_lock(&data->update_lock);
250 next_update = data->last_updated
251 + msecs_to_jiffies(data->update_interval) + 1;
253 if (time_after(jiffies, next_update) || !data->valid) {
254 if (data->config & 0x04) { /* tachometer enabled */
255 /* order matters for fan1_input */
256 data->fan[0] = i2c_smbus_read_byte_data(client,
257 LM63_REG_TACH_COUNT_LSB) & 0xFC;
258 data->fan[0] |= i2c_smbus_read_byte_data(client,
259 LM63_REG_TACH_COUNT_MSB) << 8;
260 data->fan[1] = (i2c_smbus_read_byte_data(client,
261 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
262 | (i2c_smbus_read_byte_data(client,
263 LM63_REG_TACH_LIMIT_MSB) << 8);
266 data->pwm1_freq = i2c_smbus_read_byte_data(client,
268 if (data->pwm1_freq == 0)
270 data->pwm1[0] = i2c_smbus_read_byte_data(client,
273 data->temp8[0] = i2c_smbus_read_byte_data(client,
274 LM63_REG_LOCAL_TEMP);
275 data->temp8[1] = i2c_smbus_read_byte_data(client,
276 LM63_REG_LOCAL_HIGH);
278 /* order matters for temp2_input */
279 data->temp11[0] = i2c_smbus_read_byte_data(client,
280 LM63_REG_REMOTE_TEMP_MSB) << 8;
281 data->temp11[0] |= i2c_smbus_read_byte_data(client,
282 LM63_REG_REMOTE_TEMP_LSB);
283 data->temp11[1] = (i2c_smbus_read_byte_data(client,
284 LM63_REG_REMOTE_LOW_MSB) << 8)
285 | i2c_smbus_read_byte_data(client,
286 LM63_REG_REMOTE_LOW_LSB);
287 data->temp11[2] = (i2c_smbus_read_byte_data(client,
288 LM63_REG_REMOTE_HIGH_MSB) << 8)
289 | i2c_smbus_read_byte_data(client,
290 LM63_REG_REMOTE_HIGH_LSB);
291 data->temp11[3] = (i2c_smbus_read_byte_data(client,
292 LM63_REG_REMOTE_OFFSET_MSB) << 8)
293 | i2c_smbus_read_byte_data(client,
294 LM63_REG_REMOTE_OFFSET_LSB);
296 if (data->kind == lm96163)
297 data->temp11u = (i2c_smbus_read_byte_data(client,
298 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
299 | i2c_smbus_read_byte_data(client,
300 LM96163_REG_REMOTE_TEMP_U_LSB);
302 data->temp8[2] = i2c_smbus_read_byte_data(client,
303 LM63_REG_REMOTE_TCRIT);
304 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
305 LM63_REG_REMOTE_TCRIT_HYST);
307 data->alarms = i2c_smbus_read_byte_data(client,
308 LM63_REG_ALERT_STATUS) & 0x7F;
310 data->last_updated = jiffies;
314 lm63_update_lut(client);
316 mutex_unlock(&data->update_lock);
322 * Trip points in the lookup table should be in ascending order for both
323 * temperatures and PWM output values.
325 static int lm63_lut_looks_bad(struct i2c_client *client)
327 struct lm63_data *data = i2c_get_clientdata(client);
330 mutex_lock(&data->update_lock);
331 lm63_update_lut(client);
333 for (i = 1; i < data->lut_size; i++) {
334 if (data->pwm1[1 + i - 1] > data->pwm1[1 + i]
335 || data->temp8[3 + i - 1] > data->temp8[3 + i]) {
336 dev_warn(&client->dev,
337 "Lookup table doesn't look sane (check entries %d and %d)\n",
342 mutex_unlock(&data->update_lock);
344 return i == data->lut_size ? 0 : 1;
348 * Sysfs callback functions and files
351 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
354 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
355 struct lm63_data *data = lm63_update_device(dev);
356 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
359 static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
360 const char *buf, size_t count)
362 struct i2c_client *client = to_i2c_client(dev);
363 struct lm63_data *data = i2c_get_clientdata(client);
367 err = kstrtoul(buf, 10, &val);
371 mutex_lock(&data->update_lock);
372 data->fan[1] = FAN_TO_REG(val);
373 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
374 data->fan[1] & 0xFF);
375 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
377 mutex_unlock(&data->update_lock);
381 static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
384 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
385 struct lm63_data *data = lm63_update_device(dev);
386 int nr = attr->index;
389 if (data->pwm_highres)
390 pwm = data->pwm1[nr];
392 pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
393 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
394 (2 * data->pwm1_freq);
396 return sprintf(buf, "%d\n", pwm);
399 static ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr,
400 const char *buf, size_t count)
402 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
403 struct i2c_client *client = to_i2c_client(dev);
404 struct lm63_data *data = i2c_get_clientdata(client);
405 int nr = attr->index;
410 if (!(data->config_fan & 0x20)) /* register is read-only */
413 err = kstrtoul(buf, 10, &val);
417 reg = nr ? LM63_REG_LUT_PWM(nr - 1) : LM63_REG_PWM_VALUE;
418 val = SENSORS_LIMIT(val, 0, 255);
420 mutex_lock(&data->update_lock);
421 data->pwm1[nr] = data->pwm_highres ? val :
422 (val * data->pwm1_freq * 2 + 127) / 255;
423 i2c_smbus_write_byte_data(client, reg, data->pwm1[nr]);
424 mutex_unlock(&data->update_lock);
428 static ssize_t show_pwm1_enable(struct device *dev,
429 struct device_attribute *dummy, char *buf)
431 struct lm63_data *data = lm63_update_device(dev);
432 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
435 static ssize_t set_pwm1_enable(struct device *dev,
436 struct device_attribute *dummy,
437 const char *buf, size_t count)
439 struct i2c_client *client = to_i2c_client(dev);
440 struct lm63_data *data = i2c_get_clientdata(client);
444 err = kstrtoul(buf, 10, &val);
447 if (val < 1 || val > 2)
451 * Only let the user switch to automatic mode if the lookup table
454 if (val == 2 && lm63_lut_looks_bad(client))
457 mutex_lock(&data->update_lock);
458 data->config_fan = i2c_smbus_read_byte_data(client,
459 LM63_REG_CONFIG_FAN);
461 data->config_fan |= 0x20;
463 data->config_fan &= ~0x20;
464 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN,
466 mutex_unlock(&data->update_lock);
471 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
472 * For remote sensor registers temp2_offset has to be considered,
473 * for local sensor it must not.
474 * So we need separate 8bit accessors for local and remote sensor.
476 static ssize_t show_local_temp8(struct device *dev,
477 struct device_attribute *devattr,
480 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
481 struct lm63_data *data = lm63_update_device(dev);
482 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
485 static ssize_t show_remote_temp8(struct device *dev,
486 struct device_attribute *devattr,
489 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
490 struct lm63_data *data = lm63_update_device(dev);
491 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
492 + data->temp2_offset);
495 static ssize_t show_lut_temp(struct device *dev,
496 struct device_attribute *devattr,
499 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
500 struct lm63_data *data = lm63_update_device(dev);
501 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
502 + data->temp2_offset);
505 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
506 const char *buf, size_t count)
508 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
509 struct i2c_client *client = to_i2c_client(dev);
510 struct lm63_data *data = i2c_get_clientdata(client);
511 int nr = attr->index;
517 err = kstrtol(buf, 10, &val);
521 mutex_lock(&data->update_lock);
524 reg = LM63_REG_REMOTE_TCRIT;
525 if (data->remote_unsigned)
526 temp = TEMP8U_TO_REG(val - data->temp2_offset);
528 temp = TEMP8_TO_REG(val - data->temp2_offset);
531 reg = LM63_REG_LOCAL_HIGH;
532 temp = TEMP8_TO_REG(val);
534 default: /* lookup table */
535 reg = LM63_REG_LUT_TEMP(nr - 3);
536 temp = lut_temp_to_reg(data, val);
538 data->temp8[nr] = temp;
539 i2c_smbus_write_byte_data(client, reg, temp);
540 mutex_unlock(&data->update_lock);
544 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
547 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
548 struct lm63_data *data = lm63_update_device(dev);
549 int nr = attr->index;
554 * Use unsigned temperature unless its value is zero.
555 * If it is zero, use signed temperature.
558 temp = TEMP11_FROM_REG(data->temp11u);
560 temp = TEMP11_FROM_REG(data->temp11[nr]);
562 if (data->remote_unsigned && nr == 2)
563 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
565 temp = TEMP11_FROM_REG(data->temp11[nr]);
567 return sprintf(buf, "%d\n", temp + data->temp2_offset);
570 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
571 const char *buf, size_t count)
573 static const u8 reg[6] = {
574 LM63_REG_REMOTE_LOW_MSB,
575 LM63_REG_REMOTE_LOW_LSB,
576 LM63_REG_REMOTE_HIGH_MSB,
577 LM63_REG_REMOTE_HIGH_LSB,
578 LM63_REG_REMOTE_OFFSET_MSB,
579 LM63_REG_REMOTE_OFFSET_LSB,
582 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
583 struct i2c_client *client = to_i2c_client(dev);
584 struct lm63_data *data = i2c_get_clientdata(client);
587 int nr = attr->index;
589 err = kstrtol(buf, 10, &val);
593 mutex_lock(&data->update_lock);
594 if (data->remote_unsigned && nr == 2)
595 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
597 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
599 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
600 data->temp11[nr] >> 8);
601 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
602 data->temp11[nr] & 0xff);
603 mutex_unlock(&data->update_lock);
608 * Hysteresis register holds a relative value, while we want to present
609 * an absolute to user-space
611 static ssize_t show_temp2_crit_hyst(struct device *dev,
612 struct device_attribute *dummy, char *buf)
614 struct lm63_data *data = lm63_update_device(dev);
615 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
617 - TEMP8_FROM_REG(data->temp2_crit_hyst));
620 static ssize_t show_lut_temp_hyst(struct device *dev,
621 struct device_attribute *devattr, char *buf)
623 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
624 struct lm63_data *data = lm63_update_device(dev);
626 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
628 - TEMP8_FROM_REG(data->lut_temp_hyst));
632 * And now the other way around, user-space provides an absolute
633 * hysteresis value and we have to store a relative one
635 static ssize_t set_temp2_crit_hyst(struct device *dev,
636 struct device_attribute *dummy,
637 const char *buf, size_t count)
639 struct i2c_client *client = to_i2c_client(dev);
640 struct lm63_data *data = i2c_get_clientdata(client);
645 err = kstrtol(buf, 10, &val);
649 mutex_lock(&data->update_lock);
650 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
651 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
653 mutex_unlock(&data->update_lock);
658 * Set conversion rate.
659 * client->update_lock must be held when calling this function.
661 static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data,
662 unsigned int interval)
665 unsigned int update_interval;
667 /* Shift calculations to avoid rounding errors */
670 /* find the nearest update rate */
671 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
672 / data->max_convrate_hz;
673 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
674 if (interval >= update_interval * 3 / 4)
677 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
678 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
681 static ssize_t show_update_interval(struct device *dev,
682 struct device_attribute *attr, char *buf)
684 struct lm63_data *data = dev_get_drvdata(dev);
686 return sprintf(buf, "%u\n", data->update_interval);
689 static ssize_t set_update_interval(struct device *dev,
690 struct device_attribute *attr,
691 const char *buf, size_t count)
693 struct i2c_client *client = to_i2c_client(dev);
694 struct lm63_data *data = i2c_get_clientdata(client);
698 err = kstrtoul(buf, 10, &val);
702 mutex_lock(&data->update_lock);
703 lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000));
704 mutex_unlock(&data->update_lock);
709 static ssize_t show_type(struct device *dev, struct device_attribute *attr,
712 struct i2c_client *client = to_i2c_client(dev);
713 struct lm63_data *data = i2c_get_clientdata(client);
715 return sprintf(buf, data->trutherm ? "1\n" : "2\n");
718 static ssize_t set_type(struct device *dev, struct device_attribute *attr,
719 const char *buf, size_t count)
721 struct i2c_client *client = to_i2c_client(dev);
722 struct lm63_data *data = i2c_get_clientdata(client);
727 ret = kstrtoul(buf, 10, &val);
730 if (val != 1 && val != 2)
733 mutex_lock(&data->update_lock);
734 data->trutherm = val == 1;
735 reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
736 i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
737 reg | (data->trutherm ? 0x02 : 0x00));
739 mutex_unlock(&data->update_lock);
744 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
747 struct lm63_data *data = lm63_update_device(dev);
748 return sprintf(buf, "%u\n", data->alarms);
751 static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
754 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
755 struct lm63_data *data = lm63_update_device(dev);
756 int bitnr = attr->index;
758 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
761 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
762 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
765 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
766 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
767 show_pwm1_enable, set_pwm1_enable);
768 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
769 show_pwm1, set_pwm1, 1);
770 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
771 show_lut_temp, set_temp8, 3);
772 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
773 show_lut_temp_hyst, NULL, 3);
774 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
775 show_pwm1, set_pwm1, 2);
776 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
777 show_lut_temp, set_temp8, 4);
778 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
779 show_lut_temp_hyst, NULL, 4);
780 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
781 show_pwm1, set_pwm1, 3);
782 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
783 show_lut_temp, set_temp8, 5);
784 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
785 show_lut_temp_hyst, NULL, 5);
786 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
787 show_pwm1, set_pwm1, 4);
788 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
789 show_lut_temp, set_temp8, 6);
790 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
791 show_lut_temp_hyst, NULL, 6);
792 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
793 show_pwm1, set_pwm1, 5);
794 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
795 show_lut_temp, set_temp8, 7);
796 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
797 show_lut_temp_hyst, NULL, 7);
798 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
799 show_pwm1, set_pwm1, 6);
800 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
801 show_lut_temp, set_temp8, 8);
802 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
803 show_lut_temp_hyst, NULL, 8);
804 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
805 show_pwm1, set_pwm1, 7);
806 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
807 show_lut_temp, set_temp8, 9);
808 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
809 show_lut_temp_hyst, NULL, 9);
810 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IWUSR | S_IRUGO,
811 show_pwm1, set_pwm1, 8);
812 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IWUSR | S_IRUGO,
813 show_lut_temp, set_temp8, 10);
814 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
815 show_lut_temp_hyst, NULL, 10);
816 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IWUSR | S_IRUGO,
817 show_pwm1, set_pwm1, 9);
818 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IWUSR | S_IRUGO,
819 show_lut_temp, set_temp8, 11);
820 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO,
821 show_lut_temp_hyst, NULL, 11);
822 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IWUSR | S_IRUGO,
823 show_pwm1, set_pwm1, 10);
824 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IWUSR | S_IRUGO,
825 show_lut_temp, set_temp8, 12);
826 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO,
827 show_lut_temp_hyst, NULL, 12);
828 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IWUSR | S_IRUGO,
829 show_pwm1, set_pwm1, 11);
830 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IWUSR | S_IRUGO,
831 show_lut_temp, set_temp8, 13);
832 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO,
833 show_lut_temp_hyst, NULL, 13);
834 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IWUSR | S_IRUGO,
835 show_pwm1, set_pwm1, 12);
836 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IWUSR | S_IRUGO,
837 show_lut_temp, set_temp8, 14);
838 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO,
839 show_lut_temp_hyst, NULL, 14);
841 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
842 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
845 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
846 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
848 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
850 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
852 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
854 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
855 set_temp2_crit_hyst);
857 static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type);
859 /* Individual alarm files */
860 static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
861 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
862 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
863 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
864 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
865 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
866 /* Raw alarm file for compatibility */
867 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
869 static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
870 set_update_interval);
872 static struct attribute *lm63_attributes[] = {
873 &sensor_dev_attr_pwm1.dev_attr.attr,
874 &dev_attr_pwm1_enable.attr,
875 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
876 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
877 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
878 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
879 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
880 &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
881 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
882 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
883 &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
884 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
885 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
886 &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
887 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
888 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
889 &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
890 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
891 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
892 &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
893 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
894 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
895 &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
896 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
897 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
898 &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
900 &sensor_dev_attr_temp1_input.dev_attr.attr,
901 &sensor_dev_attr_temp2_input.dev_attr.attr,
902 &sensor_dev_attr_temp2_min.dev_attr.attr,
903 &sensor_dev_attr_temp1_max.dev_attr.attr,
904 &sensor_dev_attr_temp2_max.dev_attr.attr,
905 &sensor_dev_attr_temp2_offset.dev_attr.attr,
906 &sensor_dev_attr_temp2_crit.dev_attr.attr,
907 &dev_attr_temp2_crit_hyst.attr,
909 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
910 &sensor_dev_attr_temp2_fault.dev_attr.attr,
911 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
912 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
913 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
914 &dev_attr_alarms.attr,
915 &dev_attr_update_interval.attr,
919 static struct attribute *lm63_attributes_extra_lut[] = {
920 &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
921 &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
922 &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
923 &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
924 &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
925 &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
926 &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
927 &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
928 &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
929 &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
930 &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
931 &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
935 static const struct attribute_group lm63_group_extra_lut = {
936 .attrs = lm63_attributes_extra_lut,
940 * On LM63, temp2_crit can be set only once, which should be job
942 * On LM64, temp2_crit can always be set.
943 * On LM96163, temp2_crit can be set if bit 1 of the configuration
946 static umode_t lm63_attribute_mode(struct kobject *kobj,
947 struct attribute *attr, int index)
949 struct device *dev = container_of(kobj, struct device, kobj);
950 struct i2c_client *client = to_i2c_client(dev);
951 struct lm63_data *data = i2c_get_clientdata(client);
953 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
954 && (data->kind == lm64 ||
955 (data->kind == lm96163 && (data->config & 0x02))))
956 return attr->mode | S_IWUSR;
961 static const struct attribute_group lm63_group = {
962 .is_visible = lm63_attribute_mode,
963 .attrs = lm63_attributes,
966 static struct attribute *lm63_attributes_fan1[] = {
967 &sensor_dev_attr_fan1_input.dev_attr.attr,
968 &sensor_dev_attr_fan1_min.dev_attr.attr,
970 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
974 static const struct attribute_group lm63_group_fan1 = {
975 .attrs = lm63_attributes_fan1,
982 /* Return 0 if detection is successful, -ENODEV otherwise */
983 static int lm63_detect(struct i2c_client *client,
984 struct i2c_board_info *info)
986 struct i2c_adapter *adapter = client->adapter;
987 u8 man_id, chip_id, reg_config1, reg_config2;
988 u8 reg_alert_status, reg_alert_mask;
989 int address = client->addr;
991 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
994 man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID);
995 chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID);
997 reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
998 reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2);
999 reg_alert_status = i2c_smbus_read_byte_data(client,
1000 LM63_REG_ALERT_STATUS);
1001 reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK);
1003 if (man_id != 0x01 /* National Semiconductor */
1004 || (reg_config1 & 0x18) != 0x00
1005 || (reg_config2 & 0xF8) != 0x00
1006 || (reg_alert_status & 0x20) != 0x00
1007 || (reg_alert_mask & 0xA4) != 0xA4) {
1008 dev_dbg(&adapter->dev,
1009 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
1014 if (chip_id == 0x41 && address == 0x4c)
1015 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
1016 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
1017 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
1018 else if (chip_id == 0x49 && address == 0x4c)
1019 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
1027 * Ideally we shouldn't have to initialize anything, since the BIOS
1028 * should have taken care of everything
1030 static void lm63_init_client(struct i2c_client *client)
1032 struct lm63_data *data = i2c_get_clientdata(client);
1035 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
1036 data->config_fan = i2c_smbus_read_byte_data(client,
1037 LM63_REG_CONFIG_FAN);
1039 /* Start converting if needed */
1040 if (data->config & 0x40) { /* standby */
1041 dev_dbg(&client->dev, "Switching to operational mode\n");
1042 data->config &= 0xA7;
1043 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
1046 /* Tachometer is always enabled on LM64 */
1047 if (data->kind == lm64)
1048 data->config |= 0x04;
1050 /* We may need pwm1_freq before ever updating the client data */
1051 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
1052 if (data->pwm1_freq == 0)
1053 data->pwm1_freq = 1;
1055 switch (data->kind) {
1058 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
1062 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
1063 data->lut_size = 12;
1065 = i2c_smbus_read_byte_data(client,
1066 LM96163_REG_TRUTHERM) & 0x02;
1069 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
1070 if (unlikely(convrate > LM63_MAX_CONVRATE))
1071 convrate = LM63_MAX_CONVRATE;
1072 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
1076 * For LM96163, check if high resolution PWM
1077 * and unsigned temperature format is enabled.
1079 if (data->kind == lm96163) {
1081 = i2c_smbus_read_byte_data(client,
1082 LM96163_REG_CONFIG_ENHANCED);
1083 if (config_enhanced & 0x20)
1084 data->lut_temp_highres = true;
1085 if ((config_enhanced & 0x10)
1086 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
1087 data->pwm_highres = true;
1088 if (config_enhanced & 0x08)
1089 data->remote_unsigned = true;
1092 /* Show some debug info about the LM63 configuration */
1093 if (data->kind == lm63)
1094 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
1095 (data->config & 0x04) ? "tachometer input" :
1097 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
1098 (data->config_fan & 0x08) ? "1.4" : "360",
1099 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
1100 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
1101 (data->config_fan & 0x10) ? "low" : "high",
1102 (data->config_fan & 0x20) ? "manual" : "auto");
1105 static int lm63_probe(struct i2c_client *client,
1106 const struct i2c_device_id *id)
1108 struct lm63_data *data;
1111 data = devm_kzalloc(&client->dev, sizeof(struct lm63_data), GFP_KERNEL);
1115 i2c_set_clientdata(client, data);
1117 mutex_init(&data->update_lock);
1119 /* Set the device type */
1120 data->kind = id->driver_data;
1121 if (data->kind == lm64)
1122 data->temp2_offset = 16000;
1124 /* Initialize chip */
1125 lm63_init_client(client);
1127 /* Register sysfs hooks */
1128 err = sysfs_create_group(&client->dev.kobj, &lm63_group);
1131 if (data->config & 0x04) { /* tachometer enabled */
1132 err = sysfs_create_group(&client->dev.kobj, &lm63_group_fan1);
1134 goto exit_remove_files;
1136 if (data->kind == lm96163) {
1137 err = device_create_file(&client->dev, &dev_attr_temp2_type);
1139 goto exit_remove_files;
1141 err = sysfs_create_group(&client->dev.kobj,
1142 &lm63_group_extra_lut);
1144 goto exit_remove_files;
1147 data->hwmon_dev = hwmon_device_register(&client->dev);
1148 if (IS_ERR(data->hwmon_dev)) {
1149 err = PTR_ERR(data->hwmon_dev);
1150 goto exit_remove_files;
1156 sysfs_remove_group(&client->dev.kobj, &lm63_group);
1157 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
1158 if (data->kind == lm96163) {
1159 device_remove_file(&client->dev, &dev_attr_temp2_type);
1160 sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut);
1165 static int lm63_remove(struct i2c_client *client)
1167 struct lm63_data *data = i2c_get_clientdata(client);
1169 hwmon_device_unregister(data->hwmon_dev);
1170 sysfs_remove_group(&client->dev.kobj, &lm63_group);
1171 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
1172 if (data->kind == lm96163) {
1173 device_remove_file(&client->dev, &dev_attr_temp2_type);
1174 sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut);
1181 * Driver data (common to all clients)
1184 static const struct i2c_device_id lm63_id[] = {
1187 { "lm96163", lm96163 },
1190 MODULE_DEVICE_TABLE(i2c, lm63_id);
1192 static struct i2c_driver lm63_driver = {
1193 .class = I2C_CLASS_HWMON,
1197 .probe = lm63_probe,
1198 .remove = lm63_remove,
1199 .id_table = lm63_id,
1200 .detect = lm63_detect,
1201 .address_list = normal_i2c,
1204 module_i2c_driver(lm63_driver);
1206 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1207 MODULE_DESCRIPTION("LM63 driver");
1208 MODULE_LICENSE("GPL");