1 // SPDX-License-Identifier: GPL-2.0
3 * nct6775-i2c - I2C driver for the hardware monitoring functionality of
4 * Nuvoton NCT677x Super-I/O chips
6 * Copyright (C) 2022 Zev Weiss <zev@bewilderbeest.net>
8 * This driver interacts with the chip via it's "back door" i2c interface, as
9 * is often exposed to a BMC. Because the host may still be operating the
10 * chip via the ("front door") LPC interface, this driver cannot assume that
11 * it actually has full control of the chip, and in particular must avoid
12 * making any changes that could confuse the host's LPC usage of it. It thus
13 * operates in a strictly read-only fashion, with the only exception being the
14 * bank-select register (which seems, thankfully, to be replicated for the i2c
15 * interface so it doesn't affect the LPC interface).
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21 #include <linux/hwmon.h>
22 #include <linux/hwmon-sysfs.h>
23 #include <linux/err.h>
25 #include <linux/regmap.h>
28 static int nct6775_i2c_read(void *ctx
, unsigned int reg
, unsigned int *val
)
33 struct nct6775_data
*data
= ctx
;
34 struct i2c_client
*client
= data
->driver_data
;
36 if (bank
!= data
->bank
) {
37 ret
= i2c_smbus_write_byte_data(client
, NCT6775_REG_BANK
, bank
);
43 ret
= i2c_smbus_read_byte_data(client
, reg
& 0xff);
48 if (nct6775_reg_is_word_sized(data
, reg
)) {
49 ret
= i2c_smbus_read_byte_data(client
, (reg
& 0xff) + 1);
52 tmp
= (tmp
<< 8) | ret
;
60 * The write operation is a dummy so as not to disturb anything being done
61 * with the chip via LPC.
63 static int nct6775_i2c_write(void *ctx
, unsigned int reg
, unsigned int value
)
65 struct nct6775_data
*data
= ctx
;
66 struct i2c_client
*client
= data
->driver_data
;
68 dev_dbg(&client
->dev
, "skipping attempted write: %02x -> %03x\n", value
, reg
);
71 * This is a lie, but writing anything but the bank-select register is
72 * something this driver shouldn't be doing.
77 static const struct of_device_id __maybe_unused nct6775_i2c_of_match
[] = {
78 { .compatible
= "nuvoton,nct6106", .data
= (void *)nct6106
, },
79 { .compatible
= "nuvoton,nct6116", .data
= (void *)nct6116
, },
80 { .compatible
= "nuvoton,nct6775", .data
= (void *)nct6775
, },
81 { .compatible
= "nuvoton,nct6776", .data
= (void *)nct6776
, },
82 { .compatible
= "nuvoton,nct6779", .data
= (void *)nct6779
, },
83 { .compatible
= "nuvoton,nct6791", .data
= (void *)nct6791
, },
84 { .compatible
= "nuvoton,nct6792", .data
= (void *)nct6792
, },
85 { .compatible
= "nuvoton,nct6793", .data
= (void *)nct6793
, },
86 { .compatible
= "nuvoton,nct6795", .data
= (void *)nct6795
, },
87 { .compatible
= "nuvoton,nct6796", .data
= (void *)nct6796
, },
88 { .compatible
= "nuvoton,nct6797", .data
= (void *)nct6797
, },
89 { .compatible
= "nuvoton,nct6798", .data
= (void *)nct6798
, },
90 { .compatible
= "nuvoton,nct6799", .data
= (void *)nct6799
, },
93 MODULE_DEVICE_TABLE(of
, nct6775_i2c_of_match
);
95 static const struct i2c_device_id nct6775_i2c_id
[] = {
96 { "nct6106", nct6106
},
97 { "nct6116", nct6116
},
98 { "nct6775", nct6775
},
99 { "nct6776", nct6776
},
100 { "nct6779", nct6779
},
101 { "nct6791", nct6791
},
102 { "nct6792", nct6792
},
103 { "nct6793", nct6793
},
104 { "nct6795", nct6795
},
105 { "nct6796", nct6796
},
106 { "nct6797", nct6797
},
107 { "nct6798", nct6798
},
108 { "nct6799", nct6799
},
111 MODULE_DEVICE_TABLE(i2c
, nct6775_i2c_id
);
113 static int nct6775_i2c_probe_init(struct nct6775_data
*data
)
115 u32 tsi_channel_mask
;
116 struct i2c_client
*client
= data
->driver_data
;
119 * The i2c interface doesn't provide access to the control registers
120 * needed to determine the presence of other fans, but fans 1 and 2
121 * are (in principle) always there.
123 * In practice this is perhaps a little silly, because the system
124 * using this driver is mostly likely a BMC, and hence probably has
125 * totally separate fan tachs & pwms of its own that are actually
126 * controlling/monitoring the fans -- these are thus unlikely to be
127 * doing anything actually useful.
129 data
->has_fan
= 0x03;
130 data
->has_fan_min
= 0x03;
131 data
->has_pwm
= 0x03;
134 * Because on a BMC this driver may be bound very shortly after power
135 * is first applied to the device, the automatic TSI channel detection
136 * in nct6775_probe() (which has already been run at this point) may
137 * not find anything if a channel hasn't yet produced a temperature
138 * reading. Augment whatever was found via autodetection (if
139 * anything) with the channels DT says should be active.
141 if (!of_property_read_u32(client
->dev
.of_node
, "nuvoton,tsi-channel-mask",
143 data
->have_tsi_temp
|= tsi_channel_mask
& GENMASK(NUM_TSI_TEMP
- 1, 0);
148 static const struct regmap_config nct6775_i2c_regmap_config
= {
151 .reg_read
= nct6775_i2c_read
,
152 .reg_write
= nct6775_i2c_write
,
155 static int nct6775_i2c_probe(struct i2c_client
*client
)
157 struct nct6775_data
*data
;
158 struct device
*dev
= &client
->dev
;
160 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
164 data
->kind
= (enum kinds
)(uintptr_t)i2c_get_match_data(client
);
165 data
->read_only
= true;
166 data
->driver_data
= client
;
167 data
->driver_init
= nct6775_i2c_probe_init
;
169 return nct6775_probe(dev
, data
, &nct6775_i2c_regmap_config
);
172 static struct i2c_driver nct6775_i2c_driver
= {
173 .class = I2C_CLASS_HWMON
,
175 .name
= "nct6775-i2c",
176 .of_match_table
= of_match_ptr(nct6775_i2c_of_match
),
178 .probe
= nct6775_i2c_probe
,
179 .id_table
= nct6775_i2c_id
,
182 module_i2c_driver(nct6775_i2c_driver
);
184 MODULE_AUTHOR("Zev Weiss <zev@bewilderbeest.net>");
185 MODULE_DESCRIPTION("I2C driver for NCT6775F and compatible chips");
186 MODULE_LICENSE("GPL");
187 MODULE_IMPORT_NS("HWMON_NCT6775");