Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Hardware monitoring driver for BluTek BPA-RS600 Power Supplies
0004  *
0005  * Copyright 2021 Allied Telesis Labs
0006  */
0007 
0008 #include <linux/i2c.h>
0009 #include <linux/init.h>
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/pmbus.h>
0013 #include "pmbus.h"
0014 
0015 enum chips { bpa_rs600, bpd_rs600 };
0016 
0017 static int bpa_rs600_read_byte_data(struct i2c_client *client, int page, int reg)
0018 {
0019     int ret;
0020 
0021     if (page > 0)
0022         return -ENXIO;
0023 
0024     switch (reg) {
0025     case PMBUS_FAN_CONFIG_12:
0026         /*
0027          * Two fans are reported in PMBUS_FAN_CONFIG_12 but there is
0028          * only one fan in the module. Mask out the FAN2 bits.
0029          */
0030         ret = pmbus_read_byte_data(client, 0, PMBUS_FAN_CONFIG_12);
0031         if (ret >= 0)
0032             ret &= ~(PB_FAN_2_INSTALLED | PB_FAN_2_PULSE_MASK);
0033         break;
0034     default:
0035         ret = -ENODATA;
0036         break;
0037     }
0038 
0039     return ret;
0040 }
0041 
0042 /*
0043  * The BPA-RS600 violates the PMBus spec. Specifically it treats the
0044  * mantissa as unsigned. Deal with this here to allow the PMBus core
0045  * to work with correctly encoded data.
0046  */
0047 static int bpa_rs600_read_vin(struct i2c_client *client)
0048 {
0049     int ret, exponent, mantissa;
0050 
0051     ret = pmbus_read_word_data(client, 0, 0xff, PMBUS_READ_VIN);
0052     if (ret < 0)
0053         return ret;
0054 
0055     if (ret & BIT(10)) {
0056         exponent = ret >> 11;
0057         mantissa = ret & 0x7ff;
0058 
0059         exponent++;
0060         mantissa >>= 1;
0061 
0062         ret = (exponent << 11) | mantissa;
0063     }
0064 
0065     return ret;
0066 }
0067 
0068 /*
0069  * Firmware V5.70 incorrectly reports 1640W for MFR_PIN_MAX.
0070  * Deal with this by returning a sensible value.
0071  */
0072 static int bpa_rs600_read_pin_max(struct i2c_client *client)
0073 {
0074     int ret;
0075 
0076     ret = pmbus_read_word_data(client, 0, 0xff, PMBUS_MFR_PIN_MAX);
0077     if (ret < 0)
0078         return ret;
0079 
0080     /* Detect invalid 1640W (linear encoding) */
0081     if (ret == 0x0b34)
0082         /* Report 700W (linear encoding) */
0083         return 0x095e;
0084 
0085     return ret;
0086 }
0087 
0088 static int bpa_rs600_read_word_data(struct i2c_client *client, int page, int phase, int reg)
0089 {
0090     int ret;
0091 
0092     if (page > 0)
0093         return -ENXIO;
0094 
0095     switch (reg) {
0096     case PMBUS_VIN_UV_WARN_LIMIT:
0097     case PMBUS_VIN_OV_WARN_LIMIT:
0098     case PMBUS_VOUT_UV_WARN_LIMIT:
0099     case PMBUS_VOUT_OV_WARN_LIMIT:
0100     case PMBUS_IIN_OC_WARN_LIMIT:
0101     case PMBUS_IOUT_OC_WARN_LIMIT:
0102     case PMBUS_PIN_OP_WARN_LIMIT:
0103     case PMBUS_POUT_OP_WARN_LIMIT:
0104     case PMBUS_VIN_UV_FAULT_LIMIT:
0105     case PMBUS_VIN_OV_FAULT_LIMIT:
0106     case PMBUS_VOUT_UV_FAULT_LIMIT:
0107     case PMBUS_VOUT_OV_FAULT_LIMIT:
0108         /* These commands return data but it is invalid/un-documented */
0109         ret = -ENXIO;
0110         break;
0111     case PMBUS_READ_VIN:
0112         ret = bpa_rs600_read_vin(client);
0113         break;
0114     case PMBUS_MFR_PIN_MAX:
0115         ret = bpa_rs600_read_pin_max(client);
0116         break;
0117     default:
0118         if (reg >= PMBUS_VIRT_BASE)
0119             ret = -ENXIO;
0120         else
0121             ret = -ENODATA;
0122         break;
0123     }
0124 
0125     return ret;
0126 }
0127 
0128 static struct pmbus_driver_info bpa_rs600_info = {
0129     .pages = 1,
0130     .format[PSC_VOLTAGE_IN] = linear,
0131     .format[PSC_VOLTAGE_OUT] = linear,
0132     .format[PSC_CURRENT_IN] = linear,
0133     .format[PSC_CURRENT_OUT] = linear,
0134     .format[PSC_POWER] = linear,
0135     .format[PSC_TEMPERATURE] = linear,
0136     .format[PSC_FAN] = linear,
0137     .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT |
0138         PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT |
0139         PMBUS_HAVE_PIN | PMBUS_HAVE_POUT |
0140         PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 |
0141         PMBUS_HAVE_FAN12 |
0142         PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
0143         PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP |
0144         PMBUS_HAVE_STATUS_FAN12,
0145     .read_byte_data = bpa_rs600_read_byte_data,
0146     .read_word_data = bpa_rs600_read_word_data,
0147 };
0148 
0149 static const struct i2c_device_id bpa_rs600_id[] = {
0150     { "bpa-rs600", bpa_rs600 },
0151     { "bpd-rs600", bpd_rs600 },
0152     {},
0153 };
0154 MODULE_DEVICE_TABLE(i2c, bpa_rs600_id);
0155 
0156 static int bpa_rs600_probe(struct i2c_client *client)
0157 {
0158     struct device *dev = &client->dev;
0159     u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
0160     int ret;
0161     const struct i2c_device_id *mid;
0162 
0163     if (!i2c_check_functionality(client->adapter,
0164                      I2C_FUNC_SMBUS_READ_BYTE_DATA
0165                      | I2C_FUNC_SMBUS_READ_WORD_DATA
0166                      | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
0167         return -ENODEV;
0168 
0169     ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
0170     if (ret < 0) {
0171         dev_err(dev, "Failed to read Manufacturer Model\n");
0172         return ret;
0173     }
0174 
0175     for (mid = bpa_rs600_id; mid->name[0]; mid++) {
0176         if (!strncasecmp(buf, mid->name, strlen(mid->name)))
0177             break;
0178     }
0179     if (!mid->name[0]) {
0180         buf[ret] = '\0';
0181         dev_err(dev, "Unsupported Manufacturer Model '%s'\n", buf);
0182         return -ENODEV;
0183     }
0184 
0185     return pmbus_do_probe(client, &bpa_rs600_info);
0186 }
0187 
0188 static const struct of_device_id __maybe_unused bpa_rs600_of_match[] = {
0189     { .compatible = "blutek,bpa-rs600" },
0190     {},
0191 };
0192 MODULE_DEVICE_TABLE(of, bpa_rs600_of_match);
0193 
0194 static struct i2c_driver bpa_rs600_driver = {
0195     .driver = {
0196         .name = "bpa-rs600",
0197         .of_match_table = of_match_ptr(bpa_rs600_of_match),
0198     },
0199     .probe_new = bpa_rs600_probe,
0200     .id_table = bpa_rs600_id,
0201 };
0202 
0203 module_i2c_driver(bpa_rs600_driver);
0204 
0205 MODULE_AUTHOR("Chris Packham");
0206 MODULE_DESCRIPTION("PMBus driver for BluTek BPA-RS600");
0207 MODULE_LICENSE("GPL");
0208 MODULE_IMPORT_NS(PMBUS);