2 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3 * with integrated fan control
4 * Copyright (C) 2004-2008 Jean Delvare <jdelvare@suse.de>
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(clamp_val(val, 0, 127500), 500);
214 return DIV_ROUND_CLOSEST(clamp_val(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);
252 if (time_after(jiffies, next_update) || !data->valid) {
253 if (data->config & 0x04) { /* tachometer enabled */
254 /* order matters for fan1_input */
255 data->fan[0] = i2c_smbus_read_byte_data(client,
256 LM63_REG_TACH_COUNT_LSB) & 0xFC;
257 data->fan[0] |= i2c_smbus_read_byte_data(client,
258 LM63_REG_TACH_COUNT_MSB) << 8;
259 data->fan[1] = (i2c_smbus_read_byte_data(client,
260 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
261 | (i2c_smbus_read_byte_data(client,
262 LM63_REG_TACH_LIMIT_MSB) << 8);
265 data->pwm1_freq = i2c_smbus_read_byte_data(client,
267 if (data->pwm1_freq == 0)
269 data->pwm1[0] = i2c_smbus_read_byte_data(client,
272 data->temp8[0] = i2c_smbus_read_byte_data(client,
273 LM63_REG_LOCAL_TEMP);
274 data->temp8[1] = i2c_smbus_read_byte_data(client,
275 LM63_REG_LOCAL_HIGH);
277 /* order matters for temp2_input */
278 data->temp11[0] = i2c_smbus_read_byte_data(client,
279 LM63_REG_REMOTE_TEMP_MSB) << 8;
280 data->temp11[0] |= i2c_smbus_read_byte_data(client,
281 LM63_REG_REMOTE_TEMP_LSB);
282 data->temp11[1] = (i2c_smbus_read_byte_data(client,
283 LM63_REG_REMOTE_LOW_MSB) << 8)
284 | i2c_smbus_read_byte_data(client,
285 LM63_REG_REMOTE_LOW_LSB);
286 data->temp11[2] = (i2c_smbus_read_byte_data(client,
287 LM63_REG_REMOTE_HIGH_MSB) << 8)
288 | i2c_smbus_read_byte_data(client,
289 LM63_REG_REMOTE_HIGH_LSB);
290 data->temp11[3] = (i2c_smbus_read_byte_data(client,
291 LM63_REG_REMOTE_OFFSET_MSB) << 8)
292 | i2c_smbus_read_byte_data(client,
293 LM63_REG_REMOTE_OFFSET_LSB);
295 if (data->kind == lm96163)
296 data->temp11u = (i2c_smbus_read_byte_data(client,
297 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
298 | i2c_smbus_read_byte_data(client,
299 LM96163_REG_REMOTE_TEMP_U_LSB);
301 data->temp8[2] = i2c_smbus_read_byte_data(client,
302 LM63_REG_REMOTE_TCRIT);
303 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
304 LM63_REG_REMOTE_TCRIT_HYST);
306 data->alarms = i2c_smbus_read_byte_data(client,
307 LM63_REG_ALERT_STATUS) & 0x7F;
309 data->last_updated = jiffies;
313 lm63_update_lut(client);
315 mutex_unlock(&data->update_lock);
321 * Trip points in the lookup table should be in ascending order for both
322 * temperatures and PWM output values.
324 static int lm63_lut_looks_bad(struct i2c_client *client)
326 struct lm63_data *data = i2c_get_clientdata(client);
329 mutex_lock(&data->update_lock);
330 lm63_update_lut(client);
332 for (i = 1; i < data->lut_size; i++) {
333 if (data->pwm1[1 + i - 1] > data->pwm1[1 + i]
334 || data->temp8[3 + i - 1] > data->temp8[3 + i]) {
335 dev_warn(&client->dev,
336 "Lookup table doesn't look sane (check entries %d and %d)\n",
341 mutex_unlock(&data->update_lock);
343 return i == data->lut_size ? 0 : 1;
347 * Sysfs callback functions and files
350 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
353 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
354 struct lm63_data *data = lm63_update_device(dev);
355 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
358 static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
359 const char *buf, size_t count)
361 struct i2c_client *client = to_i2c_client(dev);
362 struct lm63_data *data = i2c_get_clientdata(client);
366 err = kstrtoul(buf, 10, &val);
370 mutex_lock(&data->update_lock);
371 data->fan[1] = FAN_TO_REG(val);
372 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
373 data->fan[1] & 0xFF);
374 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
376 mutex_unlock(&data->update_lock);
380 static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
383 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
384 struct lm63_data *data = lm63_update_device(dev);
385 int nr = attr->index;
388 if (data->pwm_highres)
389 pwm = data->pwm1[nr];
391 pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
392 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
393 (2 * data->pwm1_freq);
395 return sprintf(buf, "%d\n", pwm);
398 static ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr,
399 const char *buf, size_t count)
401 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
402 struct i2c_client *client = to_i2c_client(dev);
403 struct lm63_data *data = i2c_get_clientdata(client);
404 int nr = attr->index;
409 if (!(data->config_fan & 0x20)) /* register is read-only */
412 err = kstrtoul(buf, 10, &val);
416 reg = nr ? LM63_REG_LUT_PWM(nr - 1) : LM63_REG_PWM_VALUE;
417 val = clamp_val(val, 0, 255);
419 mutex_lock(&data->update_lock);
420 data->pwm1[nr] = data->pwm_highres ? val :
421 (val * data->pwm1_freq * 2 + 127) / 255;
422 i2c_smbus_write_byte_data(client, reg, data->pwm1[nr]);
423 mutex_unlock(&data->update_lock);
427 static ssize_t show_pwm1_enable(struct device *dev,
428 struct device_attribute *dummy, char *buf)
430 struct lm63_data *data = lm63_update_device(dev);
431 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
434 static ssize_t set_pwm1_enable(struct device *dev,
435 struct device_attribute *dummy,
436 const char *buf, size_t count)
438 struct i2c_client *client = to_i2c_client(dev);
439 struct lm63_data *data = i2c_get_clientdata(client);
443 err = kstrtoul(buf, 10, &val);
446 if (val < 1 || val > 2)
450 * Only let the user switch to automatic mode if the lookup table
453 if (val == 2 && lm63_lut_looks_bad(client))
456 mutex_lock(&data->update_lock);
457 data->config_fan = i2c_smbus_read_byte_data(client,
458 LM63_REG_CONFIG_FAN);
460 data->config_fan |= 0x20;
462 data->config_fan &= ~0x20;
463 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN,
465 mutex_unlock(&data->update_lock);
470 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
471 * For remote sensor registers temp2_offset has to be considered,
472 * for local sensor it must not.
473 * So we need separate 8bit accessors for local and remote sensor.
475 static ssize_t show_local_temp8(struct device *dev,
476 struct device_attribute *devattr,
479 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
480 struct lm63_data *data = lm63_update_device(dev);
481 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
484 static ssize_t show_remote_temp8(struct device *dev,
485 struct device_attribute *devattr,
488 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
489 struct lm63_data *data = lm63_update_device(dev);
490 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
491 + data->temp2_offset);
494 static ssize_t show_lut_temp(struct device *dev,
495 struct device_attribute *devattr,
498 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
499 struct lm63_data *data = lm63_update_device(dev);
500 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
501 + data->temp2_offset);
504 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
505 const char *buf, size_t count)
507 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
508 struct i2c_client *client = to_i2c_client(dev);
509 struct lm63_data *data = i2c_get_clientdata(client);
510 int nr = attr->index;
516 err = kstrtol(buf, 10, &val);
520 mutex_lock(&data->update_lock);
523 reg = LM63_REG_REMOTE_TCRIT;
524 if (data->remote_unsigned)
525 temp = TEMP8U_TO_REG(val - data->temp2_offset);
527 temp = TEMP8_TO_REG(val - data->temp2_offset);
530 reg = LM63_REG_LOCAL_HIGH;
531 temp = TEMP8_TO_REG(val);
533 default: /* lookup table */
534 reg = LM63_REG_LUT_TEMP(nr - 3);
535 temp = lut_temp_to_reg(data, val);
537 data->temp8[nr] = temp;
538 i2c_smbus_write_byte_data(client, reg, temp);
539 mutex_unlock(&data->update_lock);
543 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
546 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
547 struct lm63_data *data = lm63_update_device(dev);
548 int nr = attr->index;
553 * Use unsigned temperature unless its value is zero.
554 * If it is zero, use signed temperature.
557 temp = TEMP11_FROM_REG(data->temp11u);
559 temp = TEMP11_FROM_REG(data->temp11[nr]);
561 if (data->remote_unsigned && nr == 2)
562 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
564 temp = TEMP11_FROM_REG(data->temp11[nr]);
566 return sprintf(buf, "%d\n", temp + data->temp2_offset);
569 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
570 const char *buf, size_t count)
572 static const u8 reg[6] = {
573 LM63_REG_REMOTE_LOW_MSB,
574 LM63_REG_REMOTE_LOW_LSB,
575 LM63_REG_REMOTE_HIGH_MSB,
576 LM63_REG_REMOTE_HIGH_LSB,
577 LM63_REG_REMOTE_OFFSET_MSB,
578 LM63_REG_REMOTE_OFFSET_LSB,
581 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
582 struct i2c_client *client = to_i2c_client(dev);
583 struct lm63_data *data = i2c_get_clientdata(client);
586 int nr = attr->index;
588 err = kstrtol(buf, 10, &val);
592 mutex_lock(&data->update_lock);
593 if (data->remote_unsigned && nr == 2)
594 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
596 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
598 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
599 data->temp11[nr] >> 8);
600 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
601 data->temp11[nr] & 0xff);
602 mutex_unlock(&data->update_lock);
607 * Hysteresis register holds a relative value, while we want to present
608 * an absolute to user-space
610 static ssize_t show_temp2_crit_hyst(struct device *dev,
611 struct device_attribute *dummy, char *buf)
613 struct lm63_data *data = lm63_update_device(dev);
614 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
616 - TEMP8_FROM_REG(data->temp2_crit_hyst));
619 static ssize_t show_lut_temp_hyst(struct device *dev,
620 struct device_attribute *devattr, char *buf)
622 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
623 struct lm63_data *data = lm63_update_device(dev);
625 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
627 - TEMP8_FROM_REG(data->lut_temp_hyst));
631 * And now the other way around, user-space provides an absolute
632 * hysteresis value and we have to store a relative one
634 static ssize_t set_temp2_crit_hyst(struct device *dev,
635 struct device_attribute *dummy,
636 const char *buf, size_t count)
638 struct i2c_client *client = to_i2c_client(dev);
639 struct lm63_data *data = i2c_get_clientdata(client);
644 err = kstrtol(buf, 10, &val);
648 mutex_lock(&data->update_lock);
649 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
650 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
652 mutex_unlock(&data->update_lock);
657 * Set conversion rate.
658 * client->update_lock must be held when calling this function.
660 static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data,
661 unsigned int interval)
664 unsigned int update_interval;
666 /* Shift calculations to avoid rounding errors */
669 /* find the nearest update rate */
670 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
671 / data->max_convrate_hz;
672 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
673 if (interval >= update_interval * 3 / 4)
676 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
677 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
680 static ssize_t show_update_interval(struct device *dev,
681 struct device_attribute *attr, char *buf)
683 struct lm63_data *data = dev_get_drvdata(dev);
685 return sprintf(buf, "%u\n", data->update_interval);
688 static ssize_t set_update_interval(struct device *dev,
689 struct device_attribute *attr,
690 const char *buf, size_t count)
692 struct i2c_client *client = to_i2c_client(dev);
693 struct lm63_data *data = i2c_get_clientdata(client);
697 err = kstrtoul(buf, 10, &val);
701 mutex_lock(&data->update_lock);
702 lm63_set_convrate(client, data, clamp_val(val, 0, 100000));
703 mutex_unlock(&data->update_lock);
708 static ssize_t show_type(struct device *dev, struct device_attribute *attr,
711 struct i2c_client *client = to_i2c_client(dev);
712 struct lm63_data *data = i2c_get_clientdata(client);
714 return sprintf(buf, data->trutherm ? "1\n" : "2\n");
717 static ssize_t set_type(struct device *dev, struct device_attribute *attr,
718 const char *buf, size_t count)
720 struct i2c_client *client = to_i2c_client(dev);
721 struct lm63_data *data = i2c_get_clientdata(client);
726 ret = kstrtoul(buf, 10, &val);
729 if (val != 1 && val != 2)
732 mutex_lock(&data->update_lock);
733 data->trutherm = val == 1;
734 reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
735 i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
736 reg | (data->trutherm ? 0x02 : 0x00));
738 mutex_unlock(&data->update_lock);
743 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
746 struct lm63_data *data = lm63_update_device(dev);
747 return sprintf(buf, "%u\n", data->alarms);
750 static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
753 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
754 struct lm63_data *data = lm63_update_device(dev);
755 int bitnr = attr->index;
757 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
760 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
761 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
764 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
765 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
766 show_pwm1_enable, set_pwm1_enable);
767 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
768 show_pwm1, set_pwm1, 1);
769 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
770 show_lut_temp, set_temp8, 3);
771 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
772 show_lut_temp_hyst, NULL, 3);
773 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
774 show_pwm1, set_pwm1, 2);
775 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
776 show_lut_temp, set_temp8, 4);
777 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
778 show_lut_temp_hyst, NULL, 4);
779 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
780 show_pwm1, set_pwm1, 3);
781 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
782 show_lut_temp, set_temp8, 5);
783 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
784 show_lut_temp_hyst, NULL, 5);
785 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
786 show_pwm1, set_pwm1, 4);
787 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
788 show_lut_temp, set_temp8, 6);
789 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
790 show_lut_temp_hyst, NULL, 6);
791 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
792 show_pwm1, set_pwm1, 5);
793 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
794 show_lut_temp, set_temp8, 7);
795 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
796 show_lut_temp_hyst, NULL, 7);
797 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
798 show_pwm1, set_pwm1, 6);
799 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
800 show_lut_temp, set_temp8, 8);
801 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
802 show_lut_temp_hyst, NULL, 8);
803 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
804 show_pwm1, set_pwm1, 7);
805 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
806 show_lut_temp, set_temp8, 9);
807 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
808 show_lut_temp_hyst, NULL, 9);
809 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IWUSR | S_IRUGO,
810 show_pwm1, set_pwm1, 8);
811 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IWUSR | S_IRUGO,
812 show_lut_temp, set_temp8, 10);
813 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
814 show_lut_temp_hyst, NULL, 10);
815 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IWUSR | S_IRUGO,
816 show_pwm1, set_pwm1, 9);
817 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IWUSR | S_IRUGO,
818 show_lut_temp, set_temp8, 11);
819 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO,
820 show_lut_temp_hyst, NULL, 11);
821 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IWUSR | S_IRUGO,
822 show_pwm1, set_pwm1, 10);
823 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IWUSR | S_IRUGO,
824 show_lut_temp, set_temp8, 12);
825 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO,
826 show_lut_temp_hyst, NULL, 12);
827 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IWUSR | S_IRUGO,
828 show_pwm1, set_pwm1, 11);
829 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IWUSR | S_IRUGO,
830 show_lut_temp, set_temp8, 13);
831 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO,
832 show_lut_temp_hyst, NULL, 13);
833 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IWUSR | S_IRUGO,
834 show_pwm1, set_pwm1, 12);
835 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IWUSR | S_IRUGO,
836 show_lut_temp, set_temp8, 14);
837 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO,
838 show_lut_temp_hyst, NULL, 14);
840 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
841 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
844 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
845 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
847 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
849 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
851 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
853 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
854 set_temp2_crit_hyst);
856 static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type);
858 /* Individual alarm files */
859 static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
860 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
861 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
862 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
863 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
864 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
865 /* Raw alarm file for compatibility */
866 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
868 static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
869 set_update_interval);
871 static struct attribute *lm63_attributes[] = {
872 &sensor_dev_attr_pwm1.dev_attr.attr,
873 &dev_attr_pwm1_enable.attr,
874 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
875 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
876 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
877 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
878 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
879 &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
880 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
881 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
882 &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
883 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
884 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
885 &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
886 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
887 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
888 &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
889 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
890 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
891 &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
892 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
893 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
894 &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
895 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
896 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
897 &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
899 &sensor_dev_attr_temp1_input.dev_attr.attr,
900 &sensor_dev_attr_temp2_input.dev_attr.attr,
901 &sensor_dev_attr_temp2_min.dev_attr.attr,
902 &sensor_dev_attr_temp1_max.dev_attr.attr,
903 &sensor_dev_attr_temp2_max.dev_attr.attr,
904 &sensor_dev_attr_temp2_offset.dev_attr.attr,
905 &sensor_dev_attr_temp2_crit.dev_attr.attr,
906 &dev_attr_temp2_crit_hyst.attr,
908 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
909 &sensor_dev_attr_temp2_fault.dev_attr.attr,
910 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
911 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
912 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
913 &dev_attr_alarms.attr,
914 &dev_attr_update_interval.attr,
918 static struct attribute *lm63_attributes_temp2_type[] = {
919 &dev_attr_temp2_type.attr,
923 static const struct attribute_group lm63_group_temp2_type = {
924 .attrs = lm63_attributes_temp2_type,
927 static struct attribute *lm63_attributes_extra_lut[] = {
928 &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
929 &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
930 &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
931 &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
932 &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
933 &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
934 &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
935 &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
936 &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
937 &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
938 &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
939 &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
943 static const struct attribute_group lm63_group_extra_lut = {
944 .attrs = lm63_attributes_extra_lut,
948 * On LM63, temp2_crit can be set only once, which should be job
950 * On LM64, temp2_crit can always be set.
951 * On LM96163, temp2_crit can be set if bit 1 of the configuration
954 static umode_t lm63_attribute_mode(struct kobject *kobj,
955 struct attribute *attr, int index)
957 struct device *dev = container_of(kobj, struct device, kobj);
958 struct i2c_client *client = to_i2c_client(dev);
959 struct lm63_data *data = i2c_get_clientdata(client);
961 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
962 && (data->kind == lm64 ||
963 (data->kind == lm96163 && (data->config & 0x02))))
964 return attr->mode | S_IWUSR;
969 static const struct attribute_group lm63_group = {
970 .is_visible = lm63_attribute_mode,
971 .attrs = lm63_attributes,
974 static struct attribute *lm63_attributes_fan1[] = {
975 &sensor_dev_attr_fan1_input.dev_attr.attr,
976 &sensor_dev_attr_fan1_min.dev_attr.attr,
978 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
982 static const struct attribute_group lm63_group_fan1 = {
983 .attrs = lm63_attributes_fan1,
990 /* Return 0 if detection is successful, -ENODEV otherwise */
991 static int lm63_detect(struct i2c_client *client,
992 struct i2c_board_info *info)
994 struct i2c_adapter *adapter = client->adapter;
995 u8 man_id, chip_id, reg_config1, reg_config2;
996 u8 reg_alert_status, reg_alert_mask;
997 int address = client->addr;
999 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1002 man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID);
1003 chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID);
1005 reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
1006 reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2);
1007 reg_alert_status = i2c_smbus_read_byte_data(client,
1008 LM63_REG_ALERT_STATUS);
1009 reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK);
1011 if (man_id != 0x01 /* National Semiconductor */
1012 || (reg_config1 & 0x18) != 0x00
1013 || (reg_config2 & 0xF8) != 0x00
1014 || (reg_alert_status & 0x20) != 0x00
1015 || (reg_alert_mask & 0xA4) != 0xA4) {
1016 dev_dbg(&adapter->dev,
1017 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
1022 if (chip_id == 0x41 && address == 0x4c)
1023 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
1024 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
1025 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
1026 else if (chip_id == 0x49 && address == 0x4c)
1027 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
1035 * Ideally we shouldn't have to initialize anything, since the BIOS
1036 * should have taken care of everything
1038 static void lm63_init_client(struct i2c_client *client)
1040 struct lm63_data *data = i2c_get_clientdata(client);
1041 struct device *dev = &client->dev;
1044 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
1045 data->config_fan = i2c_smbus_read_byte_data(client,
1046 LM63_REG_CONFIG_FAN);
1048 /* Start converting if needed */
1049 if (data->config & 0x40) { /* standby */
1050 dev_dbg(dev, "Switching to operational mode\n");
1051 data->config &= 0xA7;
1052 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
1055 /* Tachometer is always enabled on LM64 */
1056 if (data->kind == lm64)
1057 data->config |= 0x04;
1059 /* We may need pwm1_freq before ever updating the client data */
1060 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
1061 if (data->pwm1_freq == 0)
1062 data->pwm1_freq = 1;
1064 switch (data->kind) {
1067 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
1071 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
1072 data->lut_size = 12;
1074 = i2c_smbus_read_byte_data(client,
1075 LM96163_REG_TRUTHERM) & 0x02;
1078 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
1079 if (unlikely(convrate > LM63_MAX_CONVRATE))
1080 convrate = LM63_MAX_CONVRATE;
1081 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
1085 * For LM96163, check if high resolution PWM
1086 * and unsigned temperature format is enabled.
1088 if (data->kind == lm96163) {
1090 = i2c_smbus_read_byte_data(client,
1091 LM96163_REG_CONFIG_ENHANCED);
1092 if (config_enhanced & 0x20)
1093 data->lut_temp_highres = true;
1094 if ((config_enhanced & 0x10)
1095 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
1096 data->pwm_highres = true;
1097 if (config_enhanced & 0x08)
1098 data->remote_unsigned = true;
1101 /* Show some debug info about the LM63 configuration */
1102 if (data->kind == lm63)
1103 dev_dbg(dev, "Alert/tach pin configured for %s\n",
1104 (data->config & 0x04) ? "tachometer input" :
1106 dev_dbg(dev, "PWM clock %s kHz, output frequency %u Hz\n",
1107 (data->config_fan & 0x08) ? "1.4" : "360",
1108 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
1109 dev_dbg(dev, "PWM output active %s, %s mode\n",
1110 (data->config_fan & 0x10) ? "low" : "high",
1111 (data->config_fan & 0x20) ? "manual" : "auto");
1114 static int lm63_probe(struct i2c_client *client,
1115 const struct i2c_device_id *id)
1117 struct device *dev = &client->dev;
1118 struct lm63_data *data;
1121 data = devm_kzalloc(dev, sizeof(struct lm63_data), GFP_KERNEL);
1125 i2c_set_clientdata(client, data);
1127 mutex_init(&data->update_lock);
1129 /* Set the device type */
1130 data->kind = id->driver_data;
1131 if (data->kind == lm64)
1132 data->temp2_offset = 16000;
1134 /* Initialize chip */
1135 lm63_init_client(client);
1137 /* Register sysfs hooks */
1138 err = sysfs_create_group(&dev->kobj, &lm63_group);
1141 if (data->config & 0x04) { /* tachometer enabled */
1142 err = sysfs_create_group(&dev->kobj, &lm63_group_fan1);
1144 goto exit_remove_files;
1146 if (data->kind == lm96163) {
1147 err = sysfs_create_group(&dev->kobj, &lm63_group_temp2_type);
1149 goto exit_remove_files;
1151 err = sysfs_create_group(&dev->kobj, &lm63_group_extra_lut);
1153 goto exit_remove_files;
1156 data->hwmon_dev = hwmon_device_register(dev);
1157 if (IS_ERR(data->hwmon_dev)) {
1158 err = PTR_ERR(data->hwmon_dev);
1159 goto exit_remove_files;
1165 sysfs_remove_group(&dev->kobj, &lm63_group);
1166 sysfs_remove_group(&dev->kobj, &lm63_group_fan1);
1167 if (data->kind == lm96163) {
1168 sysfs_remove_group(&dev->kobj, &lm63_group_temp2_type);
1169 sysfs_remove_group(&dev->kobj, &lm63_group_extra_lut);
1174 static int lm63_remove(struct i2c_client *client)
1176 struct lm63_data *data = i2c_get_clientdata(client);
1178 hwmon_device_unregister(data->hwmon_dev);
1179 sysfs_remove_group(&client->dev.kobj, &lm63_group);
1180 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
1181 if (data->kind == lm96163) {
1182 sysfs_remove_group(&client->dev.kobj, &lm63_group_temp2_type);
1183 sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut);
1190 * Driver data (common to all clients)
1193 static const struct i2c_device_id lm63_id[] = {
1196 { "lm96163", lm96163 },
1199 MODULE_DEVICE_TABLE(i2c, lm63_id);
1201 static struct i2c_driver lm63_driver = {
1202 .class = I2C_CLASS_HWMON,
1206 .probe = lm63_probe,
1207 .remove = lm63_remove,
1208 .id_table = lm63_id,
1209 .detect = lm63_detect,
1210 .address_list = normal_i2c,
1213 module_i2c_driver(lm63_driver);
1215 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1216 MODULE_DESCRIPTION("LM63 driver");
1217 MODULE_LICENSE("GPL");