2 * nct7904.c - driver for Nuvoton NCT7904D.
4 * Copyright (c) 2015 Kontron
5 * Author: Vadim V. Vlasov <vvlasov@dev.rtsoft.ru>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/device.h>
20 #include <linux/init.h>
21 #include <linux/i2c.h>
22 #include <linux/mutex.h>
23 #include <linux/hwmon.h>
24 #include <linux/hwmon-sysfs.h>
26 #define VENDOR_ID_REG 0x7A /* Any bank */
27 #define NUVOTON_ID 0x50
28 #define CHIP_ID_REG 0x7B /* Any bank */
29 #define NCT7904_ID 0xC5
30 #define DEVICE_ID_REG 0x7C /* Any bank */
32 #define BANK_SEL_REG 0xFF
40 #define FANIN_MAX 12 /* Counted from 1 */
41 #define VSEN_MAX 21 /* VSEN1..14, 3VDD, VBAT, V3VSB,
42 LTD (not a voltage), VSEN17..19 */
43 #define FANCTL_MAX 4 /* Counted from 1 */
44 #define TCPU_MAX 8 /* Counted from 1 */
45 #define TEMP_MAX 4 /* Counted from 1 */
47 #define VT_ADC_CTRL0_REG 0x20 /* Bank 0 */
48 #define VT_ADC_CTRL1_REG 0x21 /* Bank 0 */
49 #define VT_ADC_CTRL2_REG 0x22 /* Bank 0 */
50 #define FANIN_CTRL0_REG 0x24
51 #define FANIN_CTRL1_REG 0x25
52 #define DTS_T_CTRL0_REG 0x26
53 #define DTS_T_CTRL1_REG 0x27
54 #define VT_ADC_MD_REG 0x2E
56 #define VSEN1_HV_REG 0x40 /* Bank 0; 2 regs (HV/LV) per sensor */
57 #define TEMP_CH1_HV_REG 0x42 /* Bank 0; same as VSEN2_HV */
58 #define LTD_HV_REG 0x62 /* Bank 0; 2 regs in VSEN range */
59 #define FANIN1_HV_REG 0x80 /* Bank 0; 2 regs (HV/LV) per sensor */
60 #define T_CPU1_HV_REG 0xA0 /* Bank 0; 2 regs (HV/LV) per sensor */
62 #define PRTS_REG 0x03 /* Bank 2 */
63 #define FANCTL1_FMR_REG 0x00 /* Bank 3; 1 reg per channel */
64 #define FANCTL1_OUT_REG 0x10 /* Bank 3; 1 reg per channel */
66 static const unsigned short normal_i2c[] = {
67 0x2d, 0x2e, I2C_CLIENT_END
71 struct i2c_client *client;
72 struct mutex bank_lock;
77 u8 fan_mode[FANCTL_MAX];
80 /* Access functions */
81 static int nct7904_bank_lock(struct nct7904_data *data, unsigned bank)
85 mutex_lock(&data->bank_lock);
86 if (data->bank_sel == bank)
88 ret = i2c_smbus_write_byte_data(data->client, BANK_SEL_REG, bank);
90 data->bank_sel = bank;
96 static inline void nct7904_bank_release(struct nct7904_data *data)
98 mutex_unlock(&data->bank_lock);
101 /* Read 1-byte register. Returns unsigned reg or -ERRNO on error. */
102 static int nct7904_read_reg(struct nct7904_data *data,
103 unsigned bank, unsigned reg)
105 struct i2c_client *client = data->client;
108 ret = nct7904_bank_lock(data, bank);
110 ret = i2c_smbus_read_byte_data(client, reg);
112 nct7904_bank_release(data);
117 * Read 2-byte register. Returns register in big-endian format or
120 static int nct7904_read_reg16(struct nct7904_data *data,
121 unsigned bank, unsigned reg)
123 struct i2c_client *client = data->client;
126 ret = nct7904_bank_lock(data, bank);
128 ret = i2c_smbus_read_byte_data(client, reg);
131 ret = i2c_smbus_read_byte_data(client, reg + 1);
137 nct7904_bank_release(data);
141 /* Write 1-byte register. Returns 0 or -ERRNO on error. */
142 static int nct7904_write_reg(struct nct7904_data *data,
143 unsigned bank, unsigned reg, u8 val)
145 struct i2c_client *client = data->client;
148 ret = nct7904_bank_lock(data, bank);
150 ret = i2c_smbus_write_byte_data(client, reg, val);
152 nct7904_bank_release(data);
157 static ssize_t show_fan(struct device *dev,
158 struct device_attribute *devattr, char *buf)
160 int index = to_sensor_dev_attr(devattr)->index;
161 struct nct7904_data *data = dev_get_drvdata(dev);
165 ret = nct7904_read_reg16(data, BANK_0, FANIN1_HV_REG + index * 2);
168 cnt = ((ret & 0xff00) >> 3) | (ret & 0x1f);
173 return sprintf(buf, "%u\n", rpm);
176 static umode_t nct7904_fanin_is_visible(struct kobject *kobj,
177 struct attribute *a, int n)
179 struct device *dev = container_of(kobj, struct device, kobj);
180 struct nct7904_data *data = dev_get_drvdata(dev);
182 if (data->fanin_mask & (1 << n))
187 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
188 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
189 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
190 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
191 static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4);
192 static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan, NULL, 5);
193 static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan, NULL, 6);
194 static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan, NULL, 7);
195 static SENSOR_DEVICE_ATTR(fan9_input, S_IRUGO, show_fan, NULL, 8);
196 static SENSOR_DEVICE_ATTR(fan10_input, S_IRUGO, show_fan, NULL, 9);
197 static SENSOR_DEVICE_ATTR(fan11_input, S_IRUGO, show_fan, NULL, 10);
198 static SENSOR_DEVICE_ATTR(fan12_input, S_IRUGO, show_fan, NULL, 11);
200 static struct attribute *nct7904_fanin_attrs[] = {
201 &sensor_dev_attr_fan1_input.dev_attr.attr,
202 &sensor_dev_attr_fan2_input.dev_attr.attr,
203 &sensor_dev_attr_fan3_input.dev_attr.attr,
204 &sensor_dev_attr_fan4_input.dev_attr.attr,
205 &sensor_dev_attr_fan5_input.dev_attr.attr,
206 &sensor_dev_attr_fan6_input.dev_attr.attr,
207 &sensor_dev_attr_fan7_input.dev_attr.attr,
208 &sensor_dev_attr_fan8_input.dev_attr.attr,
209 &sensor_dev_attr_fan9_input.dev_attr.attr,
210 &sensor_dev_attr_fan10_input.dev_attr.attr,
211 &sensor_dev_attr_fan11_input.dev_attr.attr,
212 &sensor_dev_attr_fan12_input.dev_attr.attr,
216 static const struct attribute_group nct7904_fanin_group = {
217 .attrs = nct7904_fanin_attrs,
218 .is_visible = nct7904_fanin_is_visible,
222 static ssize_t show_voltage(struct device *dev,
223 struct device_attribute *devattr, char *buf)
225 int index = to_sensor_dev_attr(devattr)->index;
226 struct nct7904_data *data = dev_get_drvdata(dev);
230 ret = nct7904_read_reg16(data, BANK_0, VSEN1_HV_REG + index * 2);
233 volt = ((ret & 0xff00) >> 5) | (ret & 0x7);
235 volt *= 2; /* 0.002V scale */
237 volt *= 6; /* 0.006V scale */
239 return sprintf(buf, "%d\n", volt);
242 static ssize_t show_ltemp(struct device *dev,
243 struct device_attribute *devattr, char *buf)
245 struct nct7904_data *data = dev_get_drvdata(dev);
249 ret = nct7904_read_reg16(data, BANK_0, LTD_HV_REG);
252 temp = ((ret & 0xff00) >> 5) | (ret & 0x7);
253 temp = sign_extend32(temp, 10) * 125;
255 return sprintf(buf, "%d\n", temp);
258 static umode_t nct7904_vsen_is_visible(struct kobject *kobj,
259 struct attribute *a, int n)
261 struct device *dev = container_of(kobj, struct device, kobj);
262 struct nct7904_data *data = dev_get_drvdata(dev);
264 if (data->vsen_mask & (1 << n))
269 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_voltage, NULL, 0);
270 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_voltage, NULL, 1);
271 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_voltage, NULL, 2);
272 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_voltage, NULL, 3);
273 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_voltage, NULL, 4);
274 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_voltage, NULL, 5);
275 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_voltage, NULL, 6);
276 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_voltage, NULL, 7);
277 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_voltage, NULL, 8);
278 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_voltage, NULL, 9);
279 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_voltage, NULL, 10);
280 static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_voltage, NULL, 11);
281 static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_voltage, NULL, 12);
282 static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, show_voltage, NULL, 13);
284 * Next 3 voltage sensors have specific names in the Nuvoton doc
285 * (3VDD, VBAT, 3VSB) but we use vacant numbers for them.
287 static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, show_voltage, NULL, 14);
288 static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_voltage, NULL, 15);
289 static SENSOR_DEVICE_ATTR(in20_input, S_IRUGO, show_voltage, NULL, 16);
290 /* This is not a voltage, but a local temperature sensor. */
291 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_ltemp, NULL, 0);
292 static SENSOR_DEVICE_ATTR(in17_input, S_IRUGO, show_voltage, NULL, 18);
293 static SENSOR_DEVICE_ATTR(in18_input, S_IRUGO, show_voltage, NULL, 19);
294 static SENSOR_DEVICE_ATTR(in19_input, S_IRUGO, show_voltage, NULL, 20);
296 static struct attribute *nct7904_vsen_attrs[] = {
297 &sensor_dev_attr_in1_input.dev_attr.attr,
298 &sensor_dev_attr_in2_input.dev_attr.attr,
299 &sensor_dev_attr_in3_input.dev_attr.attr,
300 &sensor_dev_attr_in4_input.dev_attr.attr,
301 &sensor_dev_attr_in5_input.dev_attr.attr,
302 &sensor_dev_attr_in6_input.dev_attr.attr,
303 &sensor_dev_attr_in7_input.dev_attr.attr,
304 &sensor_dev_attr_in8_input.dev_attr.attr,
305 &sensor_dev_attr_in9_input.dev_attr.attr,
306 &sensor_dev_attr_in10_input.dev_attr.attr,
307 &sensor_dev_attr_in11_input.dev_attr.attr,
308 &sensor_dev_attr_in12_input.dev_attr.attr,
309 &sensor_dev_attr_in13_input.dev_attr.attr,
310 &sensor_dev_attr_in14_input.dev_attr.attr,
311 &sensor_dev_attr_in15_input.dev_attr.attr,
312 &sensor_dev_attr_in16_input.dev_attr.attr,
313 &sensor_dev_attr_in20_input.dev_attr.attr,
314 &sensor_dev_attr_temp1_input.dev_attr.attr,
315 &sensor_dev_attr_in17_input.dev_attr.attr,
316 &sensor_dev_attr_in18_input.dev_attr.attr,
317 &sensor_dev_attr_in19_input.dev_attr.attr,
321 static const struct attribute_group nct7904_vsen_group = {
322 .attrs = nct7904_vsen_attrs,
323 .is_visible = nct7904_vsen_is_visible,
327 static ssize_t show_tcpu(struct device *dev,
328 struct device_attribute *devattr, char *buf)
330 int index = to_sensor_dev_attr(devattr)->index;
331 struct nct7904_data *data = dev_get_drvdata(dev);
335 ret = nct7904_read_reg16(data, BANK_0, T_CPU1_HV_REG + index * 2);
339 temp = ((ret & 0xff00) >> 5) | (ret & 0x7);
340 temp = sign_extend32(temp, 10) * 125;
341 return sprintf(buf, "%d\n", temp);
344 static umode_t nct7904_tcpu_is_visible(struct kobject *kobj,
345 struct attribute *a, int n)
347 struct device *dev = container_of(kobj, struct device, kobj);
348 struct nct7904_data *data = dev_get_drvdata(dev);
350 if (data->tcpu_mask & (1 << n))
355 /* "temp1_input" reserved for local temp */
356 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_tcpu, NULL, 0);
357 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_tcpu, NULL, 1);
358 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_tcpu, NULL, 2);
359 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_tcpu, NULL, 3);
360 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_tcpu, NULL, 4);
361 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_tcpu, NULL, 5);
362 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_tcpu, NULL, 6);
363 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_tcpu, NULL, 7);
365 static struct attribute *nct7904_tcpu_attrs[] = {
366 &sensor_dev_attr_temp2_input.dev_attr.attr,
367 &sensor_dev_attr_temp3_input.dev_attr.attr,
368 &sensor_dev_attr_temp4_input.dev_attr.attr,
369 &sensor_dev_attr_temp5_input.dev_attr.attr,
370 &sensor_dev_attr_temp6_input.dev_attr.attr,
371 &sensor_dev_attr_temp7_input.dev_attr.attr,
372 &sensor_dev_attr_temp8_input.dev_attr.attr,
373 &sensor_dev_attr_temp9_input.dev_attr.attr,
377 static const struct attribute_group nct7904_tcpu_group = {
378 .attrs = nct7904_tcpu_attrs,
379 .is_visible = nct7904_tcpu_is_visible,
383 static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
384 const char *buf, size_t count)
386 int index = to_sensor_dev_attr(devattr)->index;
387 struct nct7904_data *data = dev_get_drvdata(dev);
391 if (kstrtoul(buf, 10, &val) < 0)
396 ret = nct7904_write_reg(data, BANK_3, FANCTL1_OUT_REG + index, val);
398 return ret ? ret : count;
401 static ssize_t show_pwm(struct device *dev,
402 struct device_attribute *devattr, char *buf)
404 int index = to_sensor_dev_attr(devattr)->index;
405 struct nct7904_data *data = dev_get_drvdata(dev);
408 val = nct7904_read_reg(data, BANK_3, FANCTL1_OUT_REG + index);
412 return sprintf(buf, "%d\n", val);
415 static ssize_t store_enable(struct device *dev,
416 struct device_attribute *devattr,
417 const char *buf, size_t count)
419 int index = to_sensor_dev_attr(devattr)->index;
420 struct nct7904_data *data = dev_get_drvdata(dev);
424 if (kstrtoul(buf, 10, &val) < 0)
426 if (val < 1 || val > 2 || (val == 2 && !data->fan_mode[index]))
429 ret = nct7904_write_reg(data, BANK_3, FANCTL1_FMR_REG + index,
430 val == 2 ? data->fan_mode[index] : 0);
432 return ret ? ret : count;
435 /* Return 1 for manual mode or 2 for SmartFan mode */
436 static ssize_t show_enable(struct device *dev,
437 struct device_attribute *devattr, char *buf)
439 int index = to_sensor_dev_attr(devattr)->index;
440 struct nct7904_data *data = dev_get_drvdata(dev);
443 val = nct7904_read_reg(data, BANK_3, FANCTL1_FMR_REG + index);
447 return sprintf(buf, "%d\n", val ? 2 : 1);
450 /* 2 attributes per channel: pwm and mode */
451 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR,
452 show_pwm, store_pwm, 0);
453 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
454 show_enable, store_enable, 0);
455 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
456 show_pwm, store_pwm, 1);
457 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
458 show_enable, store_enable, 1);
459 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR,
460 show_pwm, store_pwm, 2);
461 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
462 show_enable, store_enable, 2);
463 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR,
464 show_pwm, store_pwm, 3);
465 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
466 show_enable, store_enable, 3);
468 static struct attribute *nct7904_fanctl_attrs[] = {
469 &sensor_dev_attr_pwm1.dev_attr.attr,
470 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
471 &sensor_dev_attr_pwm2.dev_attr.attr,
472 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
473 &sensor_dev_attr_pwm3.dev_attr.attr,
474 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
475 &sensor_dev_attr_pwm4.dev_attr.attr,
476 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
480 static const struct attribute_group nct7904_fanctl_group = {
481 .attrs = nct7904_fanctl_attrs,
484 static const struct attribute_group *nct7904_groups[] = {
485 &nct7904_fanin_group,
488 &nct7904_fanctl_group,
492 /* Return 0 if detection is successful, -ENODEV otherwise */
493 static int nct7904_detect(struct i2c_client *client,
494 struct i2c_board_info *info)
496 struct i2c_adapter *adapter = client->adapter;
498 if (!i2c_check_functionality(adapter,
499 I2C_FUNC_SMBUS_READ_BYTE |
500 I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
503 /* Determine the chip type. */
504 if (i2c_smbus_read_byte_data(client, VENDOR_ID_REG) != NUVOTON_ID ||
505 i2c_smbus_read_byte_data(client, CHIP_ID_REG) != NCT7904_ID ||
506 (i2c_smbus_read_byte_data(client, DEVICE_ID_REG) & 0xf0) != 0x50 ||
507 (i2c_smbus_read_byte_data(client, BANK_SEL_REG) & 0xf8) != 0x00)
510 strlcpy(info->type, "nct7904", I2C_NAME_SIZE);
515 static int nct7904_probe(struct i2c_client *client,
516 const struct i2c_device_id *id)
518 struct nct7904_data *data;
519 struct device *hwmon_dev;
520 struct device *dev = &client->dev;
524 data = devm_kzalloc(dev, sizeof(struct nct7904_data), GFP_KERNEL);
528 data->client = client;
529 mutex_init(&data->bank_lock);
532 /* Setup sensor groups. */
533 /* FANIN attributes */
534 ret = nct7904_read_reg16(data, BANK_0, FANIN_CTRL0_REG);
537 data->fanin_mask = (ret >> 8) | ((ret & 0xff) << 8);
542 * Note: voltage sensors overlap with external temperature
543 * sensors. So, if we ever decide to support the latter
544 * we will have to adjust 'vsen_mask' accordingly.
547 ret = nct7904_read_reg16(data, BANK_0, VT_ADC_CTRL0_REG);
549 mask = (ret >> 8) | ((ret & 0xff) << 8);
550 ret = nct7904_read_reg(data, BANK_0, VT_ADC_CTRL2_REG);
553 data->vsen_mask = mask;
555 /* CPU_TEMP attributes */
556 ret = nct7904_read_reg16(data, BANK_0, DTS_T_CTRL0_REG);
559 data->tcpu_mask = ((ret >> 8) & 0xf) | ((ret & 0xf) << 4);
561 for (i = 0; i < FANCTL_MAX; i++) {
562 ret = nct7904_read_reg(data, BANK_3, FANCTL1_FMR_REG + i);
565 data->fan_mode[i] = ret;
569 devm_hwmon_device_register_with_groups(dev, client->name, data,
571 return PTR_ERR_OR_ZERO(hwmon_dev);
574 static const struct i2c_device_id nct7904_id[] = {
578 MODULE_DEVICE_TABLE(i2c, nct7904_id);
580 static struct i2c_driver nct7904_driver = {
581 .class = I2C_CLASS_HWMON,
585 .probe = nct7904_probe,
586 .id_table = nct7904_id,
587 .detect = nct7904_detect,
588 .address_list = normal_i2c,
591 module_i2c_driver(nct7904_driver);
593 MODULE_AUTHOR("Vadim V. Vlasov <vvlasov@dev.rtsoft.ru>");
594 MODULE_DESCRIPTION("Hwmon driver for NUVOTON NCT7904");
595 MODULE_LICENSE("GPL");