1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
4 * with integrated fan control
5 * Copyright (C) 2004-2008 Jean Delvare <jdelvare@suse.de>
6 * Based on the lm90 driver.
8 * The LM63 is a sensor chip made by National Semiconductor. It measures
9 * two temperatures (its own and one external one) and the speed of one
10 * fan, those speed it can additionally control. Complete datasheet can be
11 * obtained from National's website at:
12 * http://www.national.com/pf/LM/LM63.html
14 * The LM63 is basically an LM86 with fan speed monitoring and control
15 * capabilities added. It misses some of the LM86 features though:
16 * - No low limit for local temperature.
17 * - No critical limit for local temperature.
18 * - Critical limit for remote temperature can be changed only once. We
19 * will consider that the critical limit is read-only.
21 * The datasheet isn't very clear about what the tachometer reading is.
22 * I had a explanation from National Semiconductor though. The two lower
23 * bits of the read value have to be masked out. The value is still 16 bit
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/jiffies.h>
31 #include <linux/i2c.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/hwmon.h>
34 #include <linux/err.h>
35 #include <linux/mutex.h>
37 #include <linux/sysfs.h>
38 #include <linux/types.h>
42 * Address is fully defined internally and cannot be changed except for
43 * LM64 which has one pin dedicated to address selection.
44 * LM63 and LM96163 have address 0x4c.
45 * LM64 can have address 0x18 or 0x4e.
48 static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
54 #define LM63_REG_CONFIG1 0x03
55 #define LM63_REG_CONVRATE 0x04
56 #define LM63_REG_CONFIG2 0xBF
57 #define LM63_REG_CONFIG_FAN 0x4A
59 #define LM63_REG_TACH_COUNT_MSB 0x47
60 #define LM63_REG_TACH_COUNT_LSB 0x46
61 #define LM63_REG_TACH_LIMIT_MSB 0x49
62 #define LM63_REG_TACH_LIMIT_LSB 0x48
64 #define LM63_REG_PWM_VALUE 0x4C
65 #define LM63_REG_PWM_FREQ 0x4D
66 #define LM63_REG_LUT_TEMP_HYST 0x4F
67 #define LM63_REG_LUT_TEMP(nr) (0x50 + 2 * (nr))
68 #define LM63_REG_LUT_PWM(nr) (0x51 + 2 * (nr))
70 #define LM63_REG_LOCAL_TEMP 0x00
71 #define LM63_REG_LOCAL_HIGH 0x05
73 #define LM63_REG_REMOTE_TEMP_MSB 0x01
74 #define LM63_REG_REMOTE_TEMP_LSB 0x10
75 #define LM63_REG_REMOTE_OFFSET_MSB 0x11
76 #define LM63_REG_REMOTE_OFFSET_LSB 0x12
77 #define LM63_REG_REMOTE_HIGH_MSB 0x07
78 #define LM63_REG_REMOTE_HIGH_LSB 0x13
79 #define LM63_REG_REMOTE_LOW_MSB 0x08
80 #define LM63_REG_REMOTE_LOW_LSB 0x14
81 #define LM63_REG_REMOTE_TCRIT 0x19
82 #define LM63_REG_REMOTE_TCRIT_HYST 0x21
84 #define LM63_REG_ALERT_STATUS 0x02
85 #define LM63_REG_ALERT_MASK 0x16
87 #define LM63_REG_MAN_ID 0xFE
88 #define LM63_REG_CHIP_ID 0xFF
90 #define LM96163_REG_TRUTHERM 0x30
91 #define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
92 #define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
93 #define LM96163_REG_CONFIG_ENHANCED 0x45
95 #define LM63_MAX_CONVRATE 9
97 #define LM63_MAX_CONVRATE_HZ 32
98 #define LM96163_MAX_CONVRATE_HZ 26
101 * Conversions and various macros
102 * For tachometer counts, the LM63 uses 16-bit values.
103 * For local temperature and high limit, remote critical limit and hysteresis
104 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
105 * For remote temperature, low and high limits, it uses signed 11-bit values
106 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
107 * For LM64 the actual remote diode temperature is 16 degree Celsius higher
108 * than the register reading. Remote temperature setpoints have to be
109 * adapted accordingly.
112 #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
114 #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
115 (5400000 / (val)) & 0xFFFC)
116 #define TEMP8_FROM_REG(reg) ((reg) * 1000)
117 #define TEMP8_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), -128000, \
119 #define TEMP8U_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, \
121 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
122 #define TEMP11_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val((val), -128000, \
124 #define TEMP11U_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val((val), 0, \
126 #define HYST_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, 127000), \
129 #define UPDATE_INTERVAL(max, rate) \
130 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
132 enum chips { lm63, lm64, lm96163 };
135 * Client data (each client gets its own)
139 struct i2c_client *client;
140 struct mutex update_lock;
141 const struct attribute_group *groups[5];
142 bool valid; /* false until following fields are valid */
143 char lut_valid; /* zero until lut fields are valid */
144 unsigned long last_updated; /* in jiffies */
145 unsigned long lut_last_updated; /* in jiffies */
149 int update_interval; /* in milliseconds */
151 int lut_size; /* 8 or 12 */
153 /* registers values */
154 u8 config, config_fan;
155 u16 fan[2]; /* 0: input
158 u8 pwm1[13]; /* 0: current output
159 1-12: lookup table */
160 s8 temp8[15]; /* 0: local input
162 2: remote critical limit
163 3-14: lookup table */
164 s16 temp11[4]; /* 0: remote input
168 u16 temp11u; /* remote input (unsigned) */
173 bool lut_temp_highres;
174 bool remote_unsigned; /* true if unsigned remote upper limits */
178 static inline int temp8_from_reg(struct lm63_data *data, int nr)
180 if (data->remote_unsigned)
181 return TEMP8_FROM_REG((u8)data->temp8[nr]);
182 return TEMP8_FROM_REG(data->temp8[nr]);
185 static inline int lut_temp_from_reg(struct lm63_data *data, int nr)
187 return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000);
190 static inline int lut_temp_to_reg(struct lm63_data *data, long val)
192 val -= data->temp2_offset;
193 if (data->lut_temp_highres)
194 return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127500), 500);
196 return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127000), 1000);
200 * Update the lookup table register cache.
201 * client->update_lock must be held when calling this function.
203 static void lm63_update_lut(struct lm63_data *data)
205 struct i2c_client *client = data->client;
208 if (time_after(jiffies, data->lut_last_updated + 5 * HZ) ||
210 for (i = 0; i < data->lut_size; i++) {
211 data->pwm1[1 + i] = i2c_smbus_read_byte_data(client,
212 LM63_REG_LUT_PWM(i));
213 data->temp8[3 + i] = i2c_smbus_read_byte_data(client,
214 LM63_REG_LUT_TEMP(i));
216 data->lut_temp_hyst = i2c_smbus_read_byte_data(client,
217 LM63_REG_LUT_TEMP_HYST);
219 data->lut_last_updated = jiffies;
224 static struct lm63_data *lm63_update_device(struct device *dev)
226 struct lm63_data *data = dev_get_drvdata(dev);
227 struct i2c_client *client = data->client;
228 unsigned long next_update;
230 mutex_lock(&data->update_lock);
232 next_update = data->last_updated +
233 msecs_to_jiffies(data->update_interval);
234 if (time_after(jiffies, next_update) || !data->valid) {
235 if (data->config & 0x04) { /* tachometer enabled */
236 /* order matters for fan1_input */
237 data->fan[0] = i2c_smbus_read_byte_data(client,
238 LM63_REG_TACH_COUNT_LSB) & 0xFC;
239 data->fan[0] |= i2c_smbus_read_byte_data(client,
240 LM63_REG_TACH_COUNT_MSB) << 8;
241 data->fan[1] = (i2c_smbus_read_byte_data(client,
242 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
243 | (i2c_smbus_read_byte_data(client,
244 LM63_REG_TACH_LIMIT_MSB) << 8);
247 data->pwm1_freq = i2c_smbus_read_byte_data(client,
249 if (data->pwm1_freq == 0)
251 data->pwm1[0] = i2c_smbus_read_byte_data(client,
254 data->temp8[0] = i2c_smbus_read_byte_data(client,
255 LM63_REG_LOCAL_TEMP);
256 data->temp8[1] = i2c_smbus_read_byte_data(client,
257 LM63_REG_LOCAL_HIGH);
259 /* order matters for temp2_input */
260 data->temp11[0] = i2c_smbus_read_byte_data(client,
261 LM63_REG_REMOTE_TEMP_MSB) << 8;
262 data->temp11[0] |= i2c_smbus_read_byte_data(client,
263 LM63_REG_REMOTE_TEMP_LSB);
264 data->temp11[1] = (i2c_smbus_read_byte_data(client,
265 LM63_REG_REMOTE_LOW_MSB) << 8)
266 | i2c_smbus_read_byte_data(client,
267 LM63_REG_REMOTE_LOW_LSB);
268 data->temp11[2] = (i2c_smbus_read_byte_data(client,
269 LM63_REG_REMOTE_HIGH_MSB) << 8)
270 | i2c_smbus_read_byte_data(client,
271 LM63_REG_REMOTE_HIGH_LSB);
272 data->temp11[3] = (i2c_smbus_read_byte_data(client,
273 LM63_REG_REMOTE_OFFSET_MSB) << 8)
274 | i2c_smbus_read_byte_data(client,
275 LM63_REG_REMOTE_OFFSET_LSB);
277 if (data->kind == lm96163)
278 data->temp11u = (i2c_smbus_read_byte_data(client,
279 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
280 | i2c_smbus_read_byte_data(client,
281 LM96163_REG_REMOTE_TEMP_U_LSB);
283 data->temp8[2] = i2c_smbus_read_byte_data(client,
284 LM63_REG_REMOTE_TCRIT);
285 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
286 LM63_REG_REMOTE_TCRIT_HYST);
288 data->alarms = i2c_smbus_read_byte_data(client,
289 LM63_REG_ALERT_STATUS) & 0x7F;
291 data->last_updated = jiffies;
295 lm63_update_lut(data);
297 mutex_unlock(&data->update_lock);
303 * Trip points in the lookup table should be in ascending order for both
304 * temperatures and PWM output values.
306 static int lm63_lut_looks_bad(struct device *dev, struct lm63_data *data)
310 mutex_lock(&data->update_lock);
311 lm63_update_lut(data);
313 for (i = 1; i < data->lut_size; i++) {
314 if (data->pwm1[1 + i - 1] > data->pwm1[1 + i]
315 || data->temp8[3 + i - 1] > data->temp8[3 + i]) {
317 "Lookup table doesn't look sane (check entries %d and %d)\n",
322 mutex_unlock(&data->update_lock);
324 return i == data->lut_size ? 0 : 1;
328 * Sysfs callback functions and files
331 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
334 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
335 struct lm63_data *data = lm63_update_device(dev);
336 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
339 static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
340 const char *buf, size_t count)
342 struct lm63_data *data = dev_get_drvdata(dev);
343 struct i2c_client *client = data->client;
347 err = kstrtoul(buf, 10, &val);
351 mutex_lock(&data->update_lock);
352 data->fan[1] = FAN_TO_REG(val);
353 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
354 data->fan[1] & 0xFF);
355 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
357 mutex_unlock(&data->update_lock);
361 static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
364 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
365 struct lm63_data *data = lm63_update_device(dev);
366 int nr = attr->index;
369 if (data->pwm_highres)
370 pwm = data->pwm1[nr];
372 pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
373 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
374 (2 * data->pwm1_freq);
376 return sprintf(buf, "%d\n", pwm);
379 static ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr,
380 const char *buf, size_t count)
382 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
383 struct lm63_data *data = dev_get_drvdata(dev);
384 struct i2c_client *client = data->client;
385 int nr = attr->index;
390 if (!(data->config_fan & 0x20)) /* register is read-only */
393 err = kstrtoul(buf, 10, &val);
397 reg = nr ? LM63_REG_LUT_PWM(nr - 1) : LM63_REG_PWM_VALUE;
398 val = clamp_val(val, 0, 255);
400 mutex_lock(&data->update_lock);
401 data->pwm1[nr] = data->pwm_highres ? val :
402 (val * data->pwm1_freq * 2 + 127) / 255;
403 i2c_smbus_write_byte_data(client, reg, data->pwm1[nr]);
404 mutex_unlock(&data->update_lock);
408 static ssize_t pwm1_enable_show(struct device *dev,
409 struct device_attribute *dummy, char *buf)
411 struct lm63_data *data = lm63_update_device(dev);
412 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
415 static ssize_t pwm1_enable_store(struct device *dev,
416 struct device_attribute *dummy,
417 const char *buf, size_t count)
419 struct lm63_data *data = dev_get_drvdata(dev);
420 struct i2c_client *client = data->client;
424 err = kstrtoul(buf, 10, &val);
427 if (val < 1 || val > 2)
431 * Only let the user switch to automatic mode if the lookup table
434 if (val == 2 && lm63_lut_looks_bad(dev, data))
437 mutex_lock(&data->update_lock);
438 data->config_fan = i2c_smbus_read_byte_data(client,
439 LM63_REG_CONFIG_FAN);
441 data->config_fan |= 0x20;
443 data->config_fan &= ~0x20;
444 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN,
446 mutex_unlock(&data->update_lock);
451 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
452 * For remote sensor registers temp2_offset has to be considered,
453 * for local sensor it must not.
454 * So we need separate 8bit accessors for local and remote sensor.
456 static ssize_t show_local_temp8(struct device *dev,
457 struct device_attribute *devattr,
460 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
461 struct lm63_data *data = lm63_update_device(dev);
462 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
465 static ssize_t show_remote_temp8(struct device *dev,
466 struct device_attribute *devattr,
469 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
470 struct lm63_data *data = lm63_update_device(dev);
471 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
472 + data->temp2_offset);
475 static ssize_t show_lut_temp(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", lut_temp_from_reg(data, attr->index)
482 + data->temp2_offset);
485 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
486 const char *buf, size_t count)
488 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
489 struct lm63_data *data = dev_get_drvdata(dev);
490 struct i2c_client *client = data->client;
491 int nr = attr->index;
497 err = kstrtol(buf, 10, &val);
501 mutex_lock(&data->update_lock);
504 reg = LM63_REG_REMOTE_TCRIT;
505 if (data->remote_unsigned)
506 temp = TEMP8U_TO_REG(val - data->temp2_offset);
508 temp = TEMP8_TO_REG(val - data->temp2_offset);
511 reg = LM63_REG_LOCAL_HIGH;
512 temp = TEMP8_TO_REG(val);
514 default: /* lookup table */
515 reg = LM63_REG_LUT_TEMP(nr - 3);
516 temp = lut_temp_to_reg(data, val);
518 data->temp8[nr] = temp;
519 i2c_smbus_write_byte_data(client, reg, temp);
520 mutex_unlock(&data->update_lock);
524 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
527 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
528 struct lm63_data *data = lm63_update_device(dev);
529 int nr = attr->index;
534 * Use unsigned temperature unless its value is zero.
535 * If it is zero, use signed temperature.
538 temp = TEMP11_FROM_REG(data->temp11u);
540 temp = TEMP11_FROM_REG(data->temp11[nr]);
542 if (data->remote_unsigned && nr == 2)
543 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
545 temp = TEMP11_FROM_REG(data->temp11[nr]);
547 return sprintf(buf, "%d\n", temp + data->temp2_offset);
550 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
551 const char *buf, size_t count)
553 static const u8 reg[6] = {
554 LM63_REG_REMOTE_LOW_MSB,
555 LM63_REG_REMOTE_LOW_LSB,
556 LM63_REG_REMOTE_HIGH_MSB,
557 LM63_REG_REMOTE_HIGH_LSB,
558 LM63_REG_REMOTE_OFFSET_MSB,
559 LM63_REG_REMOTE_OFFSET_LSB,
562 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
563 struct lm63_data *data = dev_get_drvdata(dev);
564 struct i2c_client *client = data->client;
567 int nr = attr->index;
569 err = kstrtol(buf, 10, &val);
573 mutex_lock(&data->update_lock);
574 if (data->remote_unsigned && nr == 2)
575 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
577 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
579 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
580 data->temp11[nr] >> 8);
581 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
582 data->temp11[nr] & 0xff);
583 mutex_unlock(&data->update_lock);
588 * Hysteresis register holds a relative value, while we want to present
589 * an absolute to user-space
591 static ssize_t temp2_crit_hyst_show(struct device *dev,
592 struct device_attribute *dummy, char *buf)
594 struct lm63_data *data = lm63_update_device(dev);
595 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
597 - TEMP8_FROM_REG(data->temp2_crit_hyst));
600 static ssize_t show_lut_temp_hyst(struct device *dev,
601 struct device_attribute *devattr, char *buf)
603 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
604 struct lm63_data *data = lm63_update_device(dev);
606 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
608 - TEMP8_FROM_REG(data->lut_temp_hyst));
612 * And now the other way around, user-space provides an absolute
613 * hysteresis value and we have to store a relative one
615 static ssize_t temp2_crit_hyst_store(struct device *dev,
616 struct device_attribute *dummy,
617 const char *buf, size_t count)
619 struct lm63_data *data = dev_get_drvdata(dev);
620 struct i2c_client *client = data->client;
625 err = kstrtol(buf, 10, &val);
629 mutex_lock(&data->update_lock);
630 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
631 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
633 mutex_unlock(&data->update_lock);
638 * Set conversion rate.
639 * client->update_lock must be held when calling this function.
641 static void lm63_set_convrate(struct lm63_data *data, unsigned int interval)
643 struct i2c_client *client = data->client;
644 unsigned int update_interval;
647 /* Shift calculations to avoid rounding errors */
650 /* find the nearest update rate */
651 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
652 / data->max_convrate_hz;
653 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
654 if (interval >= update_interval * 3 / 4)
657 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
658 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
661 static ssize_t update_interval_show(struct device *dev,
662 struct device_attribute *attr, char *buf)
664 struct lm63_data *data = dev_get_drvdata(dev);
666 return sprintf(buf, "%u\n", data->update_interval);
669 static ssize_t update_interval_store(struct device *dev,
670 struct device_attribute *attr,
671 const char *buf, size_t count)
673 struct lm63_data *data = dev_get_drvdata(dev);
677 err = kstrtoul(buf, 10, &val);
681 mutex_lock(&data->update_lock);
682 lm63_set_convrate(data, clamp_val(val, 0, 100000));
683 mutex_unlock(&data->update_lock);
688 static ssize_t temp2_type_show(struct device *dev,
689 struct device_attribute *attr, char *buf)
691 struct lm63_data *data = dev_get_drvdata(dev);
693 return sprintf(buf, data->trutherm ? "1\n" : "2\n");
696 static ssize_t temp2_type_store(struct device *dev,
697 struct device_attribute *attr,
698 const char *buf, size_t count)
700 struct lm63_data *data = dev_get_drvdata(dev);
701 struct i2c_client *client = data->client;
706 ret = kstrtoul(buf, 10, &val);
709 if (val != 1 && val != 2)
712 mutex_lock(&data->update_lock);
713 data->trutherm = val == 1;
714 reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
715 i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
716 reg | (data->trutherm ? 0x02 : 0x00));
718 mutex_unlock(&data->update_lock);
723 static ssize_t alarms_show(struct device *dev, struct device_attribute *dummy,
726 struct lm63_data *data = lm63_update_device(dev);
727 return sprintf(buf, "%u\n", data->alarms);
730 static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
733 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
734 struct lm63_data *data = lm63_update_device(dev);
735 int bitnr = attr->index;
737 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
740 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
741 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
744 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
745 static DEVICE_ATTR_RW(pwm1_enable);
746 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
747 show_pwm1, set_pwm1, 1);
748 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
749 show_lut_temp, set_temp8, 3);
750 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
751 show_lut_temp_hyst, NULL, 3);
752 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
753 show_pwm1, set_pwm1, 2);
754 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
755 show_lut_temp, set_temp8, 4);
756 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
757 show_lut_temp_hyst, NULL, 4);
758 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
759 show_pwm1, set_pwm1, 3);
760 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
761 show_lut_temp, set_temp8, 5);
762 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
763 show_lut_temp_hyst, NULL, 5);
764 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
765 show_pwm1, set_pwm1, 4);
766 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
767 show_lut_temp, set_temp8, 6);
768 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
769 show_lut_temp_hyst, NULL, 6);
770 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
771 show_pwm1, set_pwm1, 5);
772 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
773 show_lut_temp, set_temp8, 7);
774 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
775 show_lut_temp_hyst, NULL, 7);
776 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
777 show_pwm1, set_pwm1, 6);
778 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
779 show_lut_temp, set_temp8, 8);
780 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
781 show_lut_temp_hyst, NULL, 8);
782 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
783 show_pwm1, set_pwm1, 7);
784 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
785 show_lut_temp, set_temp8, 9);
786 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
787 show_lut_temp_hyst, NULL, 9);
788 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IWUSR | S_IRUGO,
789 show_pwm1, set_pwm1, 8);
790 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IWUSR | S_IRUGO,
791 show_lut_temp, set_temp8, 10);
792 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
793 show_lut_temp_hyst, NULL, 10);
794 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IWUSR | S_IRUGO,
795 show_pwm1, set_pwm1, 9);
796 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IWUSR | S_IRUGO,
797 show_lut_temp, set_temp8, 11);
798 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO,
799 show_lut_temp_hyst, NULL, 11);
800 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IWUSR | S_IRUGO,
801 show_pwm1, set_pwm1, 10);
802 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IWUSR | S_IRUGO,
803 show_lut_temp, set_temp8, 12);
804 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO,
805 show_lut_temp_hyst, NULL, 12);
806 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IWUSR | S_IRUGO,
807 show_pwm1, set_pwm1, 11);
808 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IWUSR | S_IRUGO,
809 show_lut_temp, set_temp8, 13);
810 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO,
811 show_lut_temp_hyst, NULL, 13);
812 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IWUSR | S_IRUGO,
813 show_pwm1, set_pwm1, 12);
814 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IWUSR | S_IRUGO,
815 show_lut_temp, set_temp8, 14);
816 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO,
817 show_lut_temp_hyst, NULL, 14);
819 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
820 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
823 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
824 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
826 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
828 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
830 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
832 static DEVICE_ATTR_RW(temp2_crit_hyst);
834 static DEVICE_ATTR_RW(temp2_type);
836 /* Individual alarm files */
837 static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
838 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
839 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
840 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
841 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
842 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
843 /* Raw alarm file for compatibility */
844 static DEVICE_ATTR_RO(alarms);
846 static DEVICE_ATTR_RW(update_interval);
848 static struct attribute *lm63_attributes[] = {
849 &sensor_dev_attr_pwm1.dev_attr.attr,
850 &dev_attr_pwm1_enable.attr,
851 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
852 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
853 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
854 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
855 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
856 &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
857 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
858 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
859 &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
860 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
861 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
862 &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
863 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
864 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
865 &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
866 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
867 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
868 &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
869 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
870 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
871 &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
872 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
873 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
874 &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
876 &sensor_dev_attr_temp1_input.dev_attr.attr,
877 &sensor_dev_attr_temp2_input.dev_attr.attr,
878 &sensor_dev_attr_temp2_min.dev_attr.attr,
879 &sensor_dev_attr_temp1_max.dev_attr.attr,
880 &sensor_dev_attr_temp2_max.dev_attr.attr,
881 &sensor_dev_attr_temp2_offset.dev_attr.attr,
882 &sensor_dev_attr_temp2_crit.dev_attr.attr,
883 &dev_attr_temp2_crit_hyst.attr,
885 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
886 &sensor_dev_attr_temp2_fault.dev_attr.attr,
887 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
888 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
889 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
890 &dev_attr_alarms.attr,
891 &dev_attr_update_interval.attr,
895 static struct attribute *lm63_attributes_temp2_type[] = {
896 &dev_attr_temp2_type.attr,
900 static const struct attribute_group lm63_group_temp2_type = {
901 .attrs = lm63_attributes_temp2_type,
904 static struct attribute *lm63_attributes_extra_lut[] = {
905 &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
906 &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
907 &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
908 &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
909 &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
910 &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
911 &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
912 &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
913 &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
914 &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
915 &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
916 &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
920 static const struct attribute_group lm63_group_extra_lut = {
921 .attrs = lm63_attributes_extra_lut,
925 * On LM63, temp2_crit can be set only once, which should be job
927 * On LM64, temp2_crit can always be set.
928 * On LM96163, temp2_crit can be set if bit 1 of the configuration
931 static umode_t lm63_attribute_mode(struct kobject *kobj,
932 struct attribute *attr, int index)
934 struct device *dev = kobj_to_dev(kobj);
935 struct lm63_data *data = dev_get_drvdata(dev);
937 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
938 && (data->kind == lm64 ||
939 (data->kind == lm96163 && (data->config & 0x02))))
940 return attr->mode | S_IWUSR;
945 static const struct attribute_group lm63_group = {
946 .is_visible = lm63_attribute_mode,
947 .attrs = lm63_attributes,
950 static struct attribute *lm63_attributes_fan1[] = {
951 &sensor_dev_attr_fan1_input.dev_attr.attr,
952 &sensor_dev_attr_fan1_min.dev_attr.attr,
954 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
958 static const struct attribute_group lm63_group_fan1 = {
959 .attrs = lm63_attributes_fan1,
966 /* Return 0 if detection is successful, -ENODEV otherwise */
967 static int lm63_detect(struct i2c_client *client,
968 struct i2c_board_info *info)
970 struct i2c_adapter *adapter = client->adapter;
971 u8 man_id, chip_id, reg_config1, reg_config2;
972 u8 reg_alert_status, reg_alert_mask;
973 int address = client->addr;
975 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
978 man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID);
979 chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID);
981 reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
982 reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2);
983 reg_alert_status = i2c_smbus_read_byte_data(client,
984 LM63_REG_ALERT_STATUS);
985 reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK);
987 if (man_id != 0x01 /* National Semiconductor */
988 || (reg_config1 & 0x18) != 0x00
989 || (reg_config2 & 0xF8) != 0x00
990 || (reg_alert_status & 0x20) != 0x00
991 || (reg_alert_mask & 0xA4) != 0xA4) {
992 dev_dbg(&adapter->dev,
993 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
998 if (chip_id == 0x41 && address == 0x4c)
999 strscpy(info->type, "lm63", I2C_NAME_SIZE);
1000 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
1001 strscpy(info->type, "lm64", I2C_NAME_SIZE);
1002 else if (chip_id == 0x49 && address == 0x4c)
1003 strscpy(info->type, "lm96163", I2C_NAME_SIZE);
1011 * Ideally we shouldn't have to initialize anything, since the BIOS
1012 * should have taken care of everything
1014 static void lm63_init_client(struct lm63_data *data)
1016 struct i2c_client *client = data->client;
1017 struct device *dev = &client->dev;
1020 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
1021 data->config_fan = i2c_smbus_read_byte_data(client,
1022 LM63_REG_CONFIG_FAN);
1024 /* Start converting if needed */
1025 if (data->config & 0x40) { /* standby */
1026 dev_dbg(dev, "Switching to operational mode\n");
1027 data->config &= 0xA7;
1028 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
1031 /* Tachometer is always enabled on LM64 */
1032 if (data->kind == lm64)
1033 data->config |= 0x04;
1035 /* We may need pwm1_freq before ever updating the client data */
1036 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
1037 if (data->pwm1_freq == 0)
1038 data->pwm1_freq = 1;
1040 switch (data->kind) {
1043 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
1047 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
1048 data->lut_size = 12;
1050 = i2c_smbus_read_byte_data(client,
1051 LM96163_REG_TRUTHERM) & 0x02;
1054 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
1055 if (unlikely(convrate > LM63_MAX_CONVRATE))
1056 convrate = LM63_MAX_CONVRATE;
1057 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
1061 * For LM96163, check if high resolution PWM
1062 * and unsigned temperature format is enabled.
1064 if (data->kind == lm96163) {
1066 = i2c_smbus_read_byte_data(client,
1067 LM96163_REG_CONFIG_ENHANCED);
1068 if (config_enhanced & 0x20)
1069 data->lut_temp_highres = true;
1070 if ((config_enhanced & 0x10)
1071 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
1072 data->pwm_highres = true;
1073 if (config_enhanced & 0x08)
1074 data->remote_unsigned = true;
1077 /* Show some debug info about the LM63 configuration */
1078 if (data->kind == lm63)
1079 dev_dbg(dev, "Alert/tach pin configured for %s\n",
1080 (data->config & 0x04) ? "tachometer input" :
1082 dev_dbg(dev, "PWM clock %s kHz, output frequency %u Hz\n",
1083 (data->config_fan & 0x08) ? "1.4" : "360",
1084 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
1085 dev_dbg(dev, "PWM output active %s, %s mode\n",
1086 (data->config_fan & 0x10) ? "low" : "high",
1087 (data->config_fan & 0x20) ? "manual" : "auto");
1090 static const struct i2c_device_id lm63_id[];
1092 static int lm63_probe(struct i2c_client *client)
1094 struct device *dev = &client->dev;
1095 struct device *hwmon_dev;
1096 struct lm63_data *data;
1099 data = devm_kzalloc(dev, sizeof(struct lm63_data), GFP_KERNEL);
1103 data->client = client;
1104 mutex_init(&data->update_lock);
1106 /* Set the device type */
1107 if (client->dev.of_node)
1108 data->kind = (uintptr_t)of_device_get_match_data(&client->dev);
1110 data->kind = i2c_match_id(lm63_id, client)->driver_data;
1111 if (data->kind == lm64)
1112 data->temp2_offset = 16000;
1114 /* Initialize chip */
1115 lm63_init_client(data);
1117 /* Register sysfs hooks */
1118 data->groups[groups++] = &lm63_group;
1119 if (data->config & 0x04) /* tachometer enabled */
1120 data->groups[groups++] = &lm63_group_fan1;
1122 if (data->kind == lm96163) {
1123 data->groups[groups++] = &lm63_group_temp2_type;
1124 data->groups[groups++] = &lm63_group_extra_lut;
1127 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1128 data, data->groups);
1129 return PTR_ERR_OR_ZERO(hwmon_dev);
1133 * Driver data (common to all clients)
1136 static const struct i2c_device_id lm63_id[] = {
1139 { "lm96163", lm96163 },
1142 MODULE_DEVICE_TABLE(i2c, lm63_id);
1144 static const struct of_device_id __maybe_unused lm63_of_match[] = {
1146 .compatible = "national,lm63",
1147 .data = (void *)lm63
1150 .compatible = "national,lm64",
1151 .data = (void *)lm64
1154 .compatible = "national,lm96163",
1155 .data = (void *)lm96163
1159 MODULE_DEVICE_TABLE(of, lm63_of_match);
1161 static struct i2c_driver lm63_driver = {
1162 .class = I2C_CLASS_HWMON,
1165 .of_match_table = of_match_ptr(lm63_of_match),
1167 .probe = lm63_probe,
1168 .id_table = lm63_id,
1169 .detect = lm63_detect,
1170 .address_list = normal_i2c,
1173 module_i2c_driver(lm63_driver);
1175 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1176 MODULE_DESCRIPTION("LM63 driver");
1177 MODULE_LICENSE("GPL");