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>
25 #define VENDOR_ID_REG 0x7A /* Any bank */
26 #define NUVOTON_ID 0x50
27 #define CHIP_ID_REG 0x7B /* Any bank */
28 #define NCT7904_ID 0xC5
29 #define DEVICE_ID_REG 0x7C /* Any bank */
31 #define BANK_SEL_REG 0xFF
39 #define FANIN_MAX 12 /* Counted from 1 */
40 #define VSEN_MAX 21 /* VSEN1..14, 3VDD, VBAT, V3VSB,
41 LTD (not a voltage), VSEN17..19 */
42 #define FANCTL_MAX 4 /* Counted from 1 */
43 #define TCPU_MAX 8 /* Counted from 1 */
44 #define TEMP_MAX 4 /* Counted from 1 */
46 #define VT_ADC_CTRL0_REG 0x20 /* Bank 0 */
47 #define VT_ADC_CTRL1_REG 0x21 /* Bank 0 */
48 #define VT_ADC_CTRL2_REG 0x22 /* Bank 0 */
49 #define FANIN_CTRL0_REG 0x24
50 #define FANIN_CTRL1_REG 0x25
51 #define DTS_T_CTRL0_REG 0x26
52 #define DTS_T_CTRL1_REG 0x27
53 #define VT_ADC_MD_REG 0x2E
55 #define VSEN1_HV_REG 0x40 /* Bank 0; 2 regs (HV/LV) per sensor */
56 #define TEMP_CH1_HV_REG 0x42 /* Bank 0; same as VSEN2_HV */
57 #define LTD_HV_REG 0x62 /* Bank 0; 2 regs in VSEN range */
58 #define FANIN1_HV_REG 0x80 /* Bank 0; 2 regs (HV/LV) per sensor */
59 #define T_CPU1_HV_REG 0xA0 /* Bank 0; 2 regs (HV/LV) per sensor */
61 #define PRTS_REG 0x03 /* Bank 2 */
62 #define FANCTL1_FMR_REG 0x00 /* Bank 3; 1 reg per channel */
63 #define FANCTL1_OUT_REG 0x10 /* Bank 3; 1 reg per channel */
65 static const unsigned short normal_i2c
[] = {
66 0x2d, 0x2e, I2C_CLIENT_END
70 struct i2c_client
*client
;
71 struct mutex bank_lock
;
76 u8 fan_mode
[FANCTL_MAX
];
79 /* Access functions */
80 static int nct7904_bank_lock(struct nct7904_data
*data
, unsigned bank
)
84 mutex_lock(&data
->bank_lock
);
85 if (data
->bank_sel
== bank
)
87 ret
= i2c_smbus_write_byte_data(data
->client
, BANK_SEL_REG
, bank
);
89 data
->bank_sel
= bank
;
95 static inline void nct7904_bank_release(struct nct7904_data
*data
)
97 mutex_unlock(&data
->bank_lock
);
100 /* Read 1-byte register. Returns unsigned reg or -ERRNO on error. */
101 static int nct7904_read_reg(struct nct7904_data
*data
,
102 unsigned bank
, unsigned reg
)
104 struct i2c_client
*client
= data
->client
;
107 ret
= nct7904_bank_lock(data
, bank
);
109 ret
= i2c_smbus_read_byte_data(client
, reg
);
111 nct7904_bank_release(data
);
116 * Read 2-byte register. Returns register in big-endian format or
119 static int nct7904_read_reg16(struct nct7904_data
*data
,
120 unsigned bank
, unsigned reg
)
122 struct i2c_client
*client
= data
->client
;
125 ret
= nct7904_bank_lock(data
, bank
);
127 ret
= i2c_smbus_read_byte_data(client
, reg
);
130 ret
= i2c_smbus_read_byte_data(client
, reg
+ 1);
136 nct7904_bank_release(data
);
140 /* Write 1-byte register. Returns 0 or -ERRNO on error. */
141 static int nct7904_write_reg(struct nct7904_data
*data
,
142 unsigned bank
, unsigned reg
, u8 val
)
144 struct i2c_client
*client
= data
->client
;
147 ret
= nct7904_bank_lock(data
, bank
);
149 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
151 nct7904_bank_release(data
);
155 static int nct7904_read_fan(struct device
*dev
, u32 attr
, int channel
,
158 struct nct7904_data
*data
= dev_get_drvdata(dev
);
159 unsigned int cnt
, rpm
;
163 case hwmon_fan_input
:
164 ret
= nct7904_read_reg16(data
, BANK_0
,
165 FANIN1_HV_REG
+ channel
* 2);
168 cnt
= ((ret
& 0xff00) >> 3) | (ret
& 0x1f);
180 static umode_t
nct7904_fan_is_visible(const void *_data
, u32 attr
, int channel
)
182 const struct nct7904_data
*data
= _data
;
184 if (attr
== hwmon_fan_input
&& data
->fanin_mask
& (1 << channel
))
189 static u8 nct7904_chan_to_index
[] = {
191 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
195 static int nct7904_read_in(struct device
*dev
, u32 attr
, int channel
,
198 struct nct7904_data
*data
= dev_get_drvdata(dev
);
199 int ret
, volt
, index
;
201 index
= nct7904_chan_to_index
[channel
];
205 ret
= nct7904_read_reg16(data
, BANK_0
,
206 VSEN1_HV_REG
+ index
* 2);
209 volt
= ((ret
& 0xff00) >> 5) | (ret
& 0x7);
211 volt
*= 2; /* 0.002V scale */
213 volt
*= 6; /* 0.006V scale */
221 static umode_t
nct7904_in_is_visible(const void *_data
, u32 attr
, int channel
)
223 const struct nct7904_data
*data
= _data
;
224 int index
= nct7904_chan_to_index
[channel
];
226 if (channel
> 0 && attr
== hwmon_in_input
&&
227 (data
->vsen_mask
& BIT(index
)))
233 static int nct7904_read_temp(struct device
*dev
, u32 attr
, int channel
,
236 struct nct7904_data
*data
= dev_get_drvdata(dev
);
240 case hwmon_temp_input
:
242 ret
= nct7904_read_reg16(data
, BANK_0
, LTD_HV_REG
);
244 ret
= nct7904_read_reg16(data
, BANK_0
,
245 T_CPU1_HV_REG
+ (channel
- 1) * 2);
248 temp
= ((ret
& 0xff00) >> 5) | (ret
& 0x7);
249 *val
= sign_extend32(temp
, 10) * 125;
256 static umode_t
nct7904_temp_is_visible(const void *_data
, u32 attr
, int channel
)
258 const struct nct7904_data
*data
= _data
;
260 if (attr
== hwmon_temp_input
) {
262 if (data
->vsen_mask
& BIT(17))
265 if (data
->tcpu_mask
& BIT(channel
- 1))
273 static int nct7904_read_pwm(struct device
*dev
, u32 attr
, int channel
,
276 struct nct7904_data
*data
= dev_get_drvdata(dev
);
280 case hwmon_pwm_input
:
281 ret
= nct7904_read_reg(data
, BANK_3
, FANCTL1_OUT_REG
+ channel
);
286 case hwmon_pwm_enable
:
287 ret
= nct7904_read_reg(data
, BANK_3
, FANCTL1_FMR_REG
+ channel
);
298 static int nct7904_write_pwm(struct device
*dev
, u32 attr
, int channel
,
301 struct nct7904_data
*data
= dev_get_drvdata(dev
);
305 case hwmon_pwm_input
:
306 if (val
< 0 || val
> 255)
308 ret
= nct7904_write_reg(data
, BANK_3
, FANCTL1_OUT_REG
+ channel
,
311 case hwmon_pwm_enable
:
312 if (val
< 1 || val
> 2 ||
313 (val
== 2 && !data
->fan_mode
[channel
]))
315 ret
= nct7904_write_reg(data
, BANK_3
, FANCTL1_FMR_REG
+ channel
,
316 val
== 2 ? data
->fan_mode
[channel
] : 0);
323 static umode_t
nct7904_pwm_is_visible(const void *_data
, u32 attr
, int channel
)
326 case hwmon_pwm_input
:
327 case hwmon_pwm_enable
:
328 return S_IRUGO
| S_IWUSR
;
334 static int nct7904_read(struct device
*dev
, enum hwmon_sensor_types type
,
335 u32 attr
, int channel
, long *val
)
339 return nct7904_read_in(dev
, attr
, channel
, val
);
341 return nct7904_read_fan(dev
, attr
, channel
, val
);
343 return nct7904_read_pwm(dev
, attr
, channel
, val
);
345 return nct7904_read_temp(dev
, attr
, channel
, val
);
351 static int nct7904_write(struct device
*dev
, enum hwmon_sensor_types type
,
352 u32 attr
, int channel
, long val
)
356 return nct7904_write_pwm(dev
, attr
, channel
, val
);
362 static umode_t
nct7904_is_visible(const void *data
,
363 enum hwmon_sensor_types type
,
364 u32 attr
, int channel
)
368 return nct7904_in_is_visible(data
, attr
, channel
);
370 return nct7904_fan_is_visible(data
, attr
, channel
);
372 return nct7904_pwm_is_visible(data
, attr
, channel
);
374 return nct7904_temp_is_visible(data
, attr
, channel
);
380 /* Return 0 if detection is successful, -ENODEV otherwise */
381 static int nct7904_detect(struct i2c_client
*client
,
382 struct i2c_board_info
*info
)
384 struct i2c_adapter
*adapter
= client
->adapter
;
386 if (!i2c_check_functionality(adapter
,
387 I2C_FUNC_SMBUS_READ_BYTE
|
388 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
))
391 /* Determine the chip type. */
392 if (i2c_smbus_read_byte_data(client
, VENDOR_ID_REG
) != NUVOTON_ID
||
393 i2c_smbus_read_byte_data(client
, CHIP_ID_REG
) != NCT7904_ID
||
394 (i2c_smbus_read_byte_data(client
, DEVICE_ID_REG
) & 0xf0) != 0x50 ||
395 (i2c_smbus_read_byte_data(client
, BANK_SEL_REG
) & 0xf8) != 0x00)
398 strlcpy(info
->type
, "nct7904", I2C_NAME_SIZE
);
403 static const u32 nct7904_in_config
[] = {
404 HWMON_I_INPUT
, /* dummy, skipped in is_visible */
428 static const struct hwmon_channel_info nct7904_in
= {
430 .config
= nct7904_in_config
,
433 static const u32 nct7904_fan_config
[] = {
445 static const struct hwmon_channel_info nct7904_fan
= {
447 .config
= nct7904_fan_config
,
450 static const u32 nct7904_pwm_config
[] = {
451 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
452 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
453 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
454 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
458 static const struct hwmon_channel_info nct7904_pwm
= {
460 .config
= nct7904_pwm_config
,
463 static const u32 nct7904_temp_config
[] = {
476 static const struct hwmon_channel_info nct7904_temp
= {
478 .config
= nct7904_temp_config
,
481 static const struct hwmon_channel_info
*nct7904_info
[] = {
489 static const struct hwmon_ops nct7904_hwmon_ops
= {
490 .is_visible
= nct7904_is_visible
,
491 .read
= nct7904_read
,
492 .write
= nct7904_write
,
495 static const struct hwmon_chip_info nct7904_chip_info
= {
496 .ops
= &nct7904_hwmon_ops
,
497 .info
= nct7904_info
,
500 static int nct7904_probe(struct i2c_client
*client
,
501 const struct i2c_device_id
*id
)
503 struct nct7904_data
*data
;
504 struct device
*hwmon_dev
;
505 struct device
*dev
= &client
->dev
;
509 data
= devm_kzalloc(dev
, sizeof(struct nct7904_data
), GFP_KERNEL
);
513 data
->client
= client
;
514 mutex_init(&data
->bank_lock
);
517 /* Setup sensor groups. */
518 /* FANIN attributes */
519 ret
= nct7904_read_reg16(data
, BANK_0
, FANIN_CTRL0_REG
);
522 data
->fanin_mask
= (ret
>> 8) | ((ret
& 0xff) << 8);
527 * Note: voltage sensors overlap with external temperature
528 * sensors. So, if we ever decide to support the latter
529 * we will have to adjust 'vsen_mask' accordingly.
532 ret
= nct7904_read_reg16(data
, BANK_0
, VT_ADC_CTRL0_REG
);
534 mask
= (ret
>> 8) | ((ret
& 0xff) << 8);
535 ret
= nct7904_read_reg(data
, BANK_0
, VT_ADC_CTRL2_REG
);
538 data
->vsen_mask
= mask
;
540 /* CPU_TEMP attributes */
541 ret
= nct7904_read_reg16(data
, BANK_0
, DTS_T_CTRL0_REG
);
544 data
->tcpu_mask
= ((ret
>> 8) & 0xf) | ((ret
& 0xf) << 4);
546 for (i
= 0; i
< FANCTL_MAX
; i
++) {
547 ret
= nct7904_read_reg(data
, BANK_3
, FANCTL1_FMR_REG
+ i
);
550 data
->fan_mode
[i
] = ret
;
554 devm_hwmon_device_register_with_info(dev
, client
->name
, data
,
555 &nct7904_chip_info
, NULL
);
556 return PTR_ERR_OR_ZERO(hwmon_dev
);
559 static const struct i2c_device_id nct7904_id
[] = {
563 MODULE_DEVICE_TABLE(i2c
, nct7904_id
);
565 static struct i2c_driver nct7904_driver
= {
566 .class = I2C_CLASS_HWMON
,
570 .probe
= nct7904_probe
,
571 .id_table
= nct7904_id
,
572 .detect
= nct7904_detect
,
573 .address_list
= normal_i2c
,
576 module_i2c_driver(nct7904_driver
);
578 MODULE_AUTHOR("Vadim V. Vlasov <vvlasov@dev.rtsoft.ru>");
579 MODULE_DESCRIPTION("Hwmon driver for NUVOTON NCT7904");
580 MODULE_LICENSE("GPL");