Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Hardware monitoring driver for FSP 3Y-Power PSUs
0004  *
0005  * Copyright (c) 2021 Václav Kubernát, CESNET
0006  *
0007  * This driver is mostly reverse engineered with the help of a tool called pmbus_peek written by
0008  * David Brownell (and later adopted by Jan Kundrát). The device has some sort of a timing issue
0009  * when switching pages, details are explained in the code. The driver support is limited. It
0010  * exposes only the values, that have been tested to work correctly. Unsupported values either
0011  * aren't supported by the devices or their encondings are unknown.
0012  */
0013 
0014 #include <linux/delay.h>
0015 #include <linux/i2c.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include "pmbus.h"
0019 
0020 #define YM2151_PAGE_12V_LOG 0x00
0021 #define YM2151_PAGE_12V_REAL    0x00
0022 #define YM2151_PAGE_5VSB_LOG    0x01
0023 #define YM2151_PAGE_5VSB_REAL   0x20
0024 #define YH5151E_PAGE_12V_LOG    0x00
0025 #define YH5151E_PAGE_12V_REAL   0x00
0026 #define YH5151E_PAGE_5V_LOG 0x01
0027 #define YH5151E_PAGE_5V_REAL    0x10
0028 #define YH5151E_PAGE_3V3_LOG    0x02
0029 #define YH5151E_PAGE_3V3_REAL   0x11
0030 
0031 enum chips {
0032     ym2151e,
0033     yh5151e
0034 };
0035 
0036 struct fsp3y_data {
0037     struct pmbus_driver_info info;
0038     int chip;
0039     int page;
0040 
0041     bool vout_linear_11;
0042 };
0043 
0044 #define to_fsp3y_data(x) container_of(x, struct fsp3y_data, info)
0045 
0046 static int page_log_to_page_real(int page_log, enum chips chip)
0047 {
0048     switch (chip) {
0049     case ym2151e:
0050         switch (page_log) {
0051         case YM2151_PAGE_12V_LOG:
0052             return YM2151_PAGE_12V_REAL;
0053         case YM2151_PAGE_5VSB_LOG:
0054             return YM2151_PAGE_5VSB_REAL;
0055         }
0056         return -EINVAL;
0057     case yh5151e:
0058         switch (page_log) {
0059         case YH5151E_PAGE_12V_LOG:
0060             return YH5151E_PAGE_12V_REAL;
0061         case YH5151E_PAGE_5V_LOG:
0062             return YH5151E_PAGE_5V_REAL;
0063         case YH5151E_PAGE_3V3_LOG:
0064             return YH5151E_PAGE_3V3_REAL;
0065         }
0066         return -EINVAL;
0067     }
0068 
0069     return -EINVAL;
0070 }
0071 
0072 static int set_page(struct i2c_client *client, int page_log)
0073 {
0074     const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
0075     struct fsp3y_data *data = to_fsp3y_data(info);
0076     int rv;
0077     int page_real;
0078 
0079     if (page_log < 0)
0080         return 0;
0081 
0082     page_real = page_log_to_page_real(page_log, data->chip);
0083     if (page_real < 0)
0084         return page_real;
0085 
0086     if (data->page != page_real) {
0087         rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page_real);
0088         if (rv < 0)
0089             return rv;
0090 
0091         data->page = page_real;
0092 
0093         /*
0094          * Testing showed that the device has a timing issue. After
0095          * setting a page, it takes a while, before the device actually
0096          * gives the correct values from the correct page. 20 ms was
0097          * tested to be enough to not give wrong values (15 ms wasn't
0098          * enough).
0099          */
0100         usleep_range(20000, 30000);
0101     }
0102 
0103     return 0;
0104 }
0105 
0106 static int fsp3y_read_byte_data(struct i2c_client *client, int page, int reg)
0107 {
0108     const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
0109     struct fsp3y_data *data = to_fsp3y_data(info);
0110     int rv;
0111 
0112     /*
0113      * Inject an exponent for non-compliant YH5151-E.
0114      */
0115     if (data->vout_linear_11 && reg == PMBUS_VOUT_MODE)
0116         return 0x1A;
0117 
0118     rv = set_page(client, page);
0119     if (rv < 0)
0120         return rv;
0121 
0122     return i2c_smbus_read_byte_data(client, reg);
0123 }
0124 
0125 static int fsp3y_read_word_data(struct i2c_client *client, int page, int phase, int reg)
0126 {
0127     const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
0128     struct fsp3y_data *data = to_fsp3y_data(info);
0129     int rv;
0130 
0131     /*
0132      * This masks commands which weren't tested to work correctly. Some of
0133      * the masked commands return 0xFFFF. These would probably get tagged as
0134      * invalid by pmbus_core. Other ones do return values which might be
0135      * useful (that is, they are not 0xFFFF), but their encoding is unknown,
0136      * and so they are unsupported.
0137      */
0138     switch (reg) {
0139     case PMBUS_READ_FAN_SPEED_1:
0140     case PMBUS_READ_IIN:
0141     case PMBUS_READ_IOUT:
0142     case PMBUS_READ_PIN:
0143     case PMBUS_READ_POUT:
0144     case PMBUS_READ_TEMPERATURE_1:
0145     case PMBUS_READ_TEMPERATURE_2:
0146     case PMBUS_READ_TEMPERATURE_3:
0147     case PMBUS_READ_VIN:
0148     case PMBUS_READ_VOUT:
0149     case PMBUS_STATUS_WORD:
0150         break;
0151     default:
0152         return -ENXIO;
0153     }
0154 
0155     rv = set_page(client, page);
0156     if (rv < 0)
0157         return rv;
0158 
0159     rv = i2c_smbus_read_word_data(client, reg);
0160     if (rv < 0)
0161         return rv;
0162 
0163     /*
0164      * Handle YH-5151E non-compliant linear11 vout voltage.
0165      */
0166     if (data->vout_linear_11 && reg == PMBUS_READ_VOUT)
0167         rv = sign_extend32(rv, 10) & 0xffff;
0168 
0169     return rv;
0170 }
0171 
0172 static struct pmbus_driver_info fsp3y_info[] = {
0173     [ym2151e] = {
0174         .pages = 2,
0175         .func[YM2151_PAGE_12V_LOG] =
0176             PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
0177             PMBUS_HAVE_PIN | PMBUS_HAVE_POUT  |
0178             PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 |
0179             PMBUS_HAVE_VIN | PMBUS_HAVE_IIN |
0180             PMBUS_HAVE_FAN12,
0181         .func[YM2151_PAGE_5VSB_LOG] =
0182             PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT,
0183             PMBUS_HAVE_IIN,
0184         .read_word_data = fsp3y_read_word_data,
0185         .read_byte_data = fsp3y_read_byte_data,
0186     },
0187     [yh5151e] = {
0188         .pages = 3,
0189         .func[YH5151E_PAGE_12V_LOG] =
0190             PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
0191             PMBUS_HAVE_POUT  |
0192             PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3,
0193         .func[YH5151E_PAGE_5V_LOG] =
0194             PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
0195             PMBUS_HAVE_POUT,
0196         .func[YH5151E_PAGE_3V3_LOG] =
0197             PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
0198             PMBUS_HAVE_POUT,
0199         .read_word_data = fsp3y_read_word_data,
0200         .read_byte_data = fsp3y_read_byte_data,
0201     }
0202 };
0203 
0204 static int fsp3y_detect(struct i2c_client *client)
0205 {
0206     int rv;
0207     u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
0208 
0209     rv = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
0210     if (rv < 0)
0211         return rv;
0212 
0213     buf[rv] = '\0';
0214 
0215     if (rv == 8) {
0216         if (!strcmp(buf, "YM-2151E"))
0217             return ym2151e;
0218         else if (!strcmp(buf, "YH-5151E"))
0219             return yh5151e;
0220     }
0221 
0222     dev_err(&client->dev, "Unsupported model %.*s\n", rv, buf);
0223     return -ENODEV;
0224 }
0225 
0226 static const struct i2c_device_id fsp3y_id[] = {
0227     {"ym2151e", ym2151e},
0228     {"yh5151e", yh5151e},
0229     { }
0230 };
0231 
0232 static int fsp3y_probe(struct i2c_client *client)
0233 {
0234     struct fsp3y_data *data;
0235     const struct i2c_device_id *id;
0236     int rv;
0237 
0238     data = devm_kzalloc(&client->dev, sizeof(struct fsp3y_data), GFP_KERNEL);
0239     if (!data)
0240         return -ENOMEM;
0241 
0242     data->chip = fsp3y_detect(client);
0243     if (data->chip < 0)
0244         return data->chip;
0245 
0246     id = i2c_match_id(fsp3y_id, client);
0247     if (data->chip != id->driver_data)
0248         dev_warn(&client->dev, "Device mismatch: Configured %s (%d), detected %d\n",
0249              id->name, (int)id->driver_data, data->chip);
0250 
0251     rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
0252     if (rv < 0)
0253         return rv;
0254     data->page = rv;
0255 
0256     data->info = fsp3y_info[data->chip];
0257 
0258     /*
0259      * YH-5151E sometimes reports vout in linear11 and sometimes in
0260      * linear16. This depends on the exact individual piece of hardware. One
0261      * YH-5151E can use linear16 and another might use linear11 instead.
0262      *
0263      * The format can be recognized by reading VOUT_MODE - if it doesn't
0264      * report a valid exponent, then vout uses linear11. Otherwise, the
0265      * device is compliant and uses linear16.
0266      */
0267     data->vout_linear_11 = false;
0268     if (data->chip == yh5151e) {
0269         rv = i2c_smbus_read_byte_data(client, PMBUS_VOUT_MODE);
0270         if (rv < 0)
0271             return rv;
0272 
0273         if (rv == 0xFF)
0274             data->vout_linear_11 = true;
0275     }
0276 
0277     return pmbus_do_probe(client, &data->info);
0278 }
0279 
0280 MODULE_DEVICE_TABLE(i2c, fsp3y_id);
0281 
0282 static struct i2c_driver fsp3y_driver = {
0283     .driver = {
0284            .name = "fsp3y",
0285            },
0286     .probe_new = fsp3y_probe,
0287     .id_table = fsp3y_id
0288 };
0289 
0290 module_i2c_driver(fsp3y_driver);
0291 
0292 MODULE_AUTHOR("Václav Kubernát");
0293 MODULE_DESCRIPTION("PMBus driver for FSP/3Y-Power power supplies");
0294 MODULE_LICENSE("GPL");
0295 MODULE_IMPORT_NS(PMBUS);