Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * nct6775-i2c - I2C driver for the hardware monitoring functionality of
0004  *           Nuvoton NCT677x Super-I/O chips
0005  *
0006  * Copyright (C) 2022 Zev Weiss <zev@bewilderbeest.net>
0007  *
0008  * This driver interacts with the chip via it's "back door" i2c interface, as
0009  * is often exposed to a BMC.  Because the host may still be operating the
0010  * chip via the ("front door") LPC interface, this driver cannot assume that
0011  * it actually has full control of the chip, and in particular must avoid
0012  * making any changes that could confuse the host's LPC usage of it.  It thus
0013  * operates in a strictly read-only fashion, with the only exception being the
0014  * bank-select register (which seems, thankfully, to be replicated for the i2c
0015  * interface so it doesn't affect the LPC interface).
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  * The write operation is a dummy so as not to disturb anything being done
0061  * with the chip via LPC.
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      * This is a lie, but writing anything but the bank-select register is
0072      * something this driver shouldn't be doing.
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      * The i2c interface doesn't provide access to the control registers
0118      * needed to determine the presence of other fans, but fans 1 and 2
0119      * are (in principle) always there.
0120      *
0121      * In practice this is perhaps a little silly, because the system
0122      * using this driver is mostly likely a BMC, and hence probably has
0123      * totally separate fan tachs & pwms of its own that are actually
0124      * controlling/monitoring the fans -- these are thus unlikely to be
0125      * doing anything actually useful.
0126      */
0127     data->has_fan = 0x03;
0128     data->has_fan_min = 0x03;
0129     data->has_pwm = 0x03;
0130 
0131     /*
0132      * Because on a BMC this driver may be bound very shortly after power
0133      * is first applied to the device, the automatic TSI channel detection
0134      * in nct6775_probe() (which has already been run at this point) may
0135      * not find anything if a channel hasn't yet produced a temperature
0136      * reading.  Augment whatever was found via autodetection (if
0137      * anything) with the channels DT says should be active.
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);