1 // SPDX-License-Identifier: GPL-2.0-only 2 // SPDX-FileCopyrightText: 2024 CERN (home.cern) 3 /* 4 * Hardware monitoring driver for Hi-Tron HAC300S PSU. 5 * 6 * NOTE: The HAC300S device does not support the PMBUS_VOUT_MODE register. 7 * On top of that, it returns the Voltage output values in Linear11 which is 8 * not adhering to the PMBus specifications. (PMBus Specification Part II, 9 * Section 7.1-7.3). For that reason the PMBUS_VOUT_MODE register is being faked 10 * and returns the exponent value of the READ_VOUT register. The exponent part 11 * of the VOUT_* registers is being cleared in order to return the mantissa to 12 * the pmbus core. 13 */ 14 15 #include <linux/bitfield.h> 16 #include <linux/bits.h> 17 #include <linux/err.h> 18 #include <linux/i2c.h> 19 #include <linux/init.h> 20 #include <linux/kernel.h> 21 #include <linux/module.h> 22 #include <linux/pmbus.h> 23 24 #include "pmbus.h" 25 26 #define LINEAR11_EXPONENT_MASK GENMASK(15, 11) 27 #define LINEAR11_MANTISSA_MASK GENMASK(10, 0) 28 29 #define to_hac300s_data(x) container_of(x, struct hac300s_data, info) 30 31 struct hac300s_data { 32 struct pmbus_driver_info info; 33 s8 exponent; 34 }; 35 36 static int hac300s_read_byte_data(struct i2c_client *client, int page, int reg) 37 { 38 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 39 struct hac300s_data *data = to_hac300s_data(info); 40 41 if (reg == PMBUS_VOUT_MODE) 42 return data->exponent; 43 44 return -ENODATA; 45 } 46 47 static int hac300s_read_word_data(struct i2c_client *client, int page, 48 int phase, int reg) 49 { 50 int rv; 51 52 switch (reg) { 53 case PMBUS_VOUT_OV_WARN_LIMIT: 54 case PMBUS_VOUT_UV_WARN_LIMIT: 55 case PMBUS_VOUT_OV_FAULT_LIMIT: 56 case PMBUS_VOUT_UV_FAULT_LIMIT: 57 case PMBUS_MFR_VOUT_MAX: 58 case PMBUS_MFR_VOUT_MIN: 59 case PMBUS_READ_VOUT: 60 rv = pmbus_read_word_data(client, page, phase, reg); 61 if (rv < 0) 62 return rv; 63 return FIELD_GET(LINEAR11_MANTISSA_MASK, rv); 64 default: 65 return -ENODATA; 66 } 67 } 68 69 static struct pmbus_driver_info hac300s_info = { 70 .pages = 1, 71 .func[0] = PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | \ 72 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | \ 73 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | \ 74 PMBUS_HAVE_POUT | PMBUS_HAVE_STATUS_TEMP, 75 .read_byte_data = hac300s_read_byte_data, 76 .read_word_data = hac300s_read_word_data, 77 .format[PSC_VOLTAGE_OUT] = linear, 78 }; 79 80 static struct pmbus_platform_data hac300s_pdata = { 81 .flags = PMBUS_NO_CAPABILITY, 82 }; 83 84 static int hac300s_probe(struct i2c_client *client) 85 { 86 struct hac300s_data *data; 87 int rv; 88 89 data = devm_kzalloc(&client->dev, sizeof(struct hac300s_data), GFP_KERNEL); 90 if (!data) 91 return -ENOMEM; 92 93 if (!i2c_check_functionality(client->adapter, 94 I2C_FUNC_SMBUS_READ_BYTE_DATA | 95 I2C_FUNC_SMBUS_READ_WORD_DATA)) 96 return -ENODEV; 97 98 rv = i2c_smbus_read_word_data(client, PMBUS_READ_VOUT); 99 if (rv < 0) 100 return dev_err_probe(&client->dev, rv, "Failed to read vout_mode\n"); 101 102 data->exponent = FIELD_GET(LINEAR11_EXPONENT_MASK, rv); 103 data->info = hac300s_info; 104 client->dev.platform_data = &hac300s_pdata; 105 return pmbus_do_probe(client, &data->info); 106 } 107 108 static const struct of_device_id hac300s_of_match[] = { 109 { .compatible = "hitron,hac300s" }, 110 {} 111 }; 112 MODULE_DEVICE_TABLE(of, hac300s_of_match); 113 114 static const struct i2c_device_id hac300s_id[] = { 115 {"hac300s", 0}, 116 {} 117 }; 118 MODULE_DEVICE_TABLE(i2c, hac300s_id); 119 120 static struct i2c_driver hac300s_driver = { 121 .driver = { 122 .name = "hac300s", 123 .of_match_table = hac300s_of_match, 124 }, 125 .probe = hac300s_probe, 126 .id_table = hac300s_id, 127 128 }; 129 module_i2c_driver(hac300s_driver); 130 131 MODULE_AUTHOR("Vasileios Amoiridis"); 132 MODULE_DESCRIPTION("PMBus driver for Hi-Tron HAC300S PSU"); 133 MODULE_LICENSE("GPL"); 134 MODULE_IMPORT_NS("PMBUS"); 135