0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <linux/module.h>
0019 #include <linux/init.h>
0020 #include <linux/i2c.h>
0021 #include <linux/hwmon.h>
0022 #include <linux/hwmon-sysfs.h>
0023 #include <linux/err.h>
0024 #include <linux/of_device.h>
0025 #include <linux/regmap.h>
0026 #include "nct6775.h"
0027
0028 static int nct6775_i2c_read(void *ctx, unsigned int reg, unsigned int *val)
0029 {
0030 int ret;
0031 u32 tmp;
0032 u8 bank = reg >> 8;
0033 struct nct6775_data *data = ctx;
0034 struct i2c_client *client = data->driver_data;
0035
0036 if (bank != data->bank) {
0037 ret = i2c_smbus_write_byte_data(client, NCT6775_REG_BANK, bank);
0038 if (ret)
0039 return ret;
0040 data->bank = bank;
0041 }
0042
0043 ret = i2c_smbus_read_byte_data(client, reg & 0xff);
0044 if (ret < 0)
0045 return ret;
0046 tmp = ret;
0047
0048 if (nct6775_reg_is_word_sized(data, reg)) {
0049 ret = i2c_smbus_read_byte_data(client, (reg & 0xff) + 1);
0050 if (ret < 0)
0051 return ret;
0052 tmp = (tmp << 8) | ret;
0053 }
0054
0055 *val = tmp;
0056 return 0;
0057 }
0058
0059
0060
0061
0062
0063 static int nct6775_i2c_write(void *ctx, unsigned int reg, unsigned int value)
0064 {
0065 struct nct6775_data *data = ctx;
0066 struct i2c_client *client = data->driver_data;
0067
0068 dev_dbg(&client->dev, "skipping attempted write: %02x -> %03x\n", value, reg);
0069
0070
0071
0072
0073
0074 return 0;
0075 }
0076
0077 static const struct of_device_id __maybe_unused nct6775_i2c_of_match[] = {
0078 { .compatible = "nuvoton,nct6106", .data = (void *)nct6106, },
0079 { .compatible = "nuvoton,nct6116", .data = (void *)nct6116, },
0080 { .compatible = "nuvoton,nct6775", .data = (void *)nct6775, },
0081 { .compatible = "nuvoton,nct6776", .data = (void *)nct6776, },
0082 { .compatible = "nuvoton,nct6779", .data = (void *)nct6779, },
0083 { .compatible = "nuvoton,nct6791", .data = (void *)nct6791, },
0084 { .compatible = "nuvoton,nct6792", .data = (void *)nct6792, },
0085 { .compatible = "nuvoton,nct6793", .data = (void *)nct6793, },
0086 { .compatible = "nuvoton,nct6795", .data = (void *)nct6795, },
0087 { .compatible = "nuvoton,nct6796", .data = (void *)nct6796, },
0088 { .compatible = "nuvoton,nct6797", .data = (void *)nct6797, },
0089 { .compatible = "nuvoton,nct6798", .data = (void *)nct6798, },
0090 { },
0091 };
0092 MODULE_DEVICE_TABLE(of, nct6775_i2c_of_match);
0093
0094 static const struct i2c_device_id nct6775_i2c_id[] = {
0095 { "nct6106", nct6106 },
0096 { "nct6116", nct6116 },
0097 { "nct6775", nct6775 },
0098 { "nct6776", nct6776 },
0099 { "nct6779", nct6779 },
0100 { "nct6791", nct6791 },
0101 { "nct6792", nct6792 },
0102 { "nct6793", nct6793 },
0103 { "nct6795", nct6795 },
0104 { "nct6796", nct6796 },
0105 { "nct6797", nct6797 },
0106 { "nct6798", nct6798 },
0107 { }
0108 };
0109 MODULE_DEVICE_TABLE(i2c, nct6775_i2c_id);
0110
0111 static int nct6775_i2c_probe_init(struct nct6775_data *data)
0112 {
0113 u32 tsi_channel_mask;
0114 struct i2c_client *client = data->driver_data;
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127 data->has_fan = 0x03;
0128 data->has_fan_min = 0x03;
0129 data->has_pwm = 0x03;
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 if (!of_property_read_u32(client->dev.of_node, "nuvoton,tsi-channel-mask",
0140 &tsi_channel_mask))
0141 data->have_tsi_temp |= tsi_channel_mask & GENMASK(NUM_TSI_TEMP - 1, 0);
0142
0143 return 0;
0144 }
0145
0146 static const struct regmap_config nct6775_i2c_regmap_config = {
0147 .reg_bits = 16,
0148 .val_bits = 16,
0149 .reg_read = nct6775_i2c_read,
0150 .reg_write = nct6775_i2c_write,
0151 };
0152
0153 static int nct6775_i2c_probe(struct i2c_client *client)
0154 {
0155 struct nct6775_data *data;
0156 const struct of_device_id *of_id;
0157 const struct i2c_device_id *i2c_id;
0158 struct device *dev = &client->dev;
0159
0160 of_id = of_match_device(nct6775_i2c_of_match, dev);
0161 i2c_id = i2c_match_id(nct6775_i2c_id, client);
0162
0163 if (of_id && (unsigned long)of_id->data != i2c_id->driver_data)
0164 dev_notice(dev, "Device mismatch: %s in device tree, %s detected\n",
0165 of_id->name, i2c_id->name);
0166
0167 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
0168 if (!data)
0169 return -ENOMEM;
0170
0171 data->kind = i2c_id->driver_data;
0172
0173 data->read_only = true;
0174 data->driver_data = client;
0175 data->driver_init = nct6775_i2c_probe_init;
0176
0177 return nct6775_probe(dev, data, &nct6775_i2c_regmap_config);
0178 }
0179
0180 static struct i2c_driver nct6775_i2c_driver = {
0181 .class = I2C_CLASS_HWMON,
0182 .driver = {
0183 .name = "nct6775-i2c",
0184 .of_match_table = of_match_ptr(nct6775_i2c_of_match),
0185 },
0186 .probe_new = nct6775_i2c_probe,
0187 .id_table = nct6775_i2c_id,
0188 };
0189
0190 module_i2c_driver(nct6775_i2c_driver);
0191
0192 MODULE_AUTHOR("Zev Weiss <zev@bewilderbeest.net>");
0193 MODULE_DESCRIPTION("I2C driver for NCT6775F and compatible chips");
0194 MODULE_LICENSE("GPL");
0195 MODULE_IMPORT_NS(HWMON_NCT6775);