1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Hardware monitoring driver for FSP 3Y-Power PSUs 4 * 5 * Copyright (c) 2021 Václav Kubernát, CESNET 6 * 7 * This driver is mostly reverse engineered with the help of a tool called pmbus_peek written by 8 * David Brownell (and later adopted by Jan Kundrát). The device has some sort of a timing issue 9 * when switching pages, details are explained in the code. The driver support is limited. It 10 * exposes only the values, that have been tested to work correctly. Unsupported values either 11 * aren't supported by the devices or their encondings are unknown. 12 */ 13 14 #include <linux/delay.h> 15 #include <linux/i2c.h> 16 #include <linux/kernel.h> 17 #include <linux/module.h> 18 #include "pmbus.h" 19 20 #define YM2151_PAGE_12V_LOG 0x00 21 #define YM2151_PAGE_12V_REAL 0x00 22 #define YM2151_PAGE_5VSB_LOG 0x01 23 #define YM2151_PAGE_5VSB_REAL 0x20 24 #define YH5151E_PAGE_12V_LOG 0x00 25 #define YH5151E_PAGE_12V_REAL 0x00 26 #define YH5151E_PAGE_5V_LOG 0x01 27 #define YH5151E_PAGE_5V_REAL 0x10 28 #define YH5151E_PAGE_3V3_LOG 0x02 29 #define YH5151E_PAGE_3V3_REAL 0x11 30 31 enum chips { 32 ym2151e, 33 yh5151e 34 }; 35 36 struct fsp3y_data { 37 struct pmbus_driver_info info; 38 int chip; 39 int page; 40 }; 41 42 #define to_fsp3y_data(x) container_of(x, struct fsp3y_data, info) 43 44 static int page_log_to_page_real(int page_log, enum chips chip) 45 { 46 switch (chip) { 47 case ym2151e: 48 switch (page_log) { 49 case YM2151_PAGE_12V_LOG: 50 return YM2151_PAGE_12V_REAL; 51 case YM2151_PAGE_5VSB_LOG: 52 return YM2151_PAGE_5VSB_REAL; 53 } 54 return -EINVAL; 55 case yh5151e: 56 switch (page_log) { 57 case YH5151E_PAGE_12V_LOG: 58 return YH5151E_PAGE_12V_REAL; 59 case YH5151E_PAGE_5V_LOG: 60 return YH5151E_PAGE_5V_LOG; 61 case YH5151E_PAGE_3V3_LOG: 62 return YH5151E_PAGE_3V3_REAL; 63 } 64 return -EINVAL; 65 } 66 67 return -EINVAL; 68 } 69 70 static int set_page(struct i2c_client *client, int page_log) 71 { 72 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 73 struct fsp3y_data *data = to_fsp3y_data(info); 74 int rv; 75 int page_real; 76 77 if (page_log < 0) 78 return 0; 79 80 page_real = page_log_to_page_real(page_log, data->chip); 81 if (page_real < 0) 82 return page_real; 83 84 if (data->page != page_real) { 85 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page_real); 86 if (rv < 0) 87 return rv; 88 89 data->page = page_real; 90 91 /* 92 * Testing showed that the device has a timing issue. After 93 * setting a page, it takes a while, before the device actually 94 * gives the correct values from the correct page. 20 ms was 95 * tested to be enough to not give wrong values (15 ms wasn't 96 * enough). 97 */ 98 usleep_range(20000, 30000); 99 } 100 101 return 0; 102 } 103 104 static int fsp3y_read_byte_data(struct i2c_client *client, int page, int reg) 105 { 106 int rv; 107 108 rv = set_page(client, page); 109 if (rv < 0) 110 return rv; 111 112 return i2c_smbus_read_byte_data(client, reg); 113 } 114 115 static int fsp3y_read_word_data(struct i2c_client *client, int page, int phase, int reg) 116 { 117 int rv; 118 119 /* 120 * This masks commands which weren't tested to work correctly. Some of 121 * the masked commands return 0xFFFF. These would probably get tagged as 122 * invalid by pmbus_core. Other ones do return values which might be 123 * useful (that is, they are not 0xFFFF), but their encoding is unknown, 124 * and so they are unsupported. 125 */ 126 switch (reg) { 127 case PMBUS_READ_FAN_SPEED_1: 128 case PMBUS_READ_IIN: 129 case PMBUS_READ_IOUT: 130 case PMBUS_READ_PIN: 131 case PMBUS_READ_POUT: 132 case PMBUS_READ_TEMPERATURE_1: 133 case PMBUS_READ_TEMPERATURE_2: 134 case PMBUS_READ_TEMPERATURE_3: 135 case PMBUS_READ_VIN: 136 case PMBUS_READ_VOUT: 137 case PMBUS_STATUS_WORD: 138 break; 139 default: 140 return -ENXIO; 141 } 142 143 rv = set_page(client, page); 144 if (rv < 0) 145 return rv; 146 147 return i2c_smbus_read_word_data(client, reg); 148 } 149 150 static struct pmbus_driver_info fsp3y_info[] = { 151 [ym2151e] = { 152 .pages = 2, 153 .func[YM2151_PAGE_12V_LOG] = 154 PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | 155 PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | 156 PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | 157 PMBUS_HAVE_VIN | PMBUS_HAVE_IIN | 158 PMBUS_HAVE_FAN12, 159 .func[YM2151_PAGE_5VSB_LOG] = 160 PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT, 161 PMBUS_HAVE_IIN, 162 .read_word_data = fsp3y_read_word_data, 163 .read_byte_data = fsp3y_read_byte_data, 164 }, 165 [yh5151e] = { 166 .pages = 3, 167 .func[YH5151E_PAGE_12V_LOG] = 168 PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | 169 PMBUS_HAVE_POUT | 170 PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3, 171 .func[YH5151E_PAGE_5V_LOG] = 172 PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | 173 PMBUS_HAVE_POUT, 174 .func[YH5151E_PAGE_3V3_LOG] = 175 PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | 176 PMBUS_HAVE_POUT, 177 .read_word_data = fsp3y_read_word_data, 178 .read_byte_data = fsp3y_read_byte_data, 179 } 180 }; 181 182 static int fsp3y_detect(struct i2c_client *client) 183 { 184 int rv; 185 u8 buf[I2C_SMBUS_BLOCK_MAX + 1]; 186 187 rv = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf); 188 if (rv < 0) 189 return rv; 190 191 buf[rv] = '\0'; 192 193 if (rv == 8) { 194 if (!strcmp(buf, "YM-2151E")) 195 return ym2151e; 196 else if (!strcmp(buf, "YH-5151E")) 197 return yh5151e; 198 } 199 200 dev_err(&client->dev, "Unsupported model %.*s\n", rv, buf); 201 return -ENODEV; 202 } 203 204 static const struct i2c_device_id fsp3y_id[] = { 205 {"ym2151e", ym2151e}, 206 {"yh5151e", yh5151e}, 207 { } 208 }; 209 210 static int fsp3y_probe(struct i2c_client *client) 211 { 212 struct fsp3y_data *data; 213 const struct i2c_device_id *id; 214 int rv; 215 216 data = devm_kzalloc(&client->dev, sizeof(struct fsp3y_data), GFP_KERNEL); 217 if (!data) 218 return -ENOMEM; 219 220 data->chip = fsp3y_detect(client); 221 if (data->chip < 0) 222 return data->chip; 223 224 id = i2c_match_id(fsp3y_id, client); 225 if (data->chip != id->driver_data) 226 dev_warn(&client->dev, "Device mismatch: Configured %s (%d), detected %d\n", 227 id->name, (int)id->driver_data, data->chip); 228 229 rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE); 230 if (rv < 0) 231 return rv; 232 data->page = rv; 233 234 data->info = fsp3y_info[data->chip]; 235 236 return pmbus_do_probe(client, &data->info); 237 } 238 239 MODULE_DEVICE_TABLE(i2c, fsp3y_id); 240 241 static struct i2c_driver fsp3y_driver = { 242 .driver = { 243 .name = "fsp3y", 244 }, 245 .probe_new = fsp3y_probe, 246 .id_table = fsp3y_id 247 }; 248 249 module_i2c_driver(fsp3y_driver); 250 251 MODULE_AUTHOR("Václav Kubernát"); 252 MODULE_DESCRIPTION("PMBus driver for FSP/3Y-Power power supplies"); 253 MODULE_LICENSE("GPL"); 254 MODULE_IMPORT_NS(PMBUS); 255