1 // SPDX-License-Identifier: GPL-2.0-or-later 2 3 #include <linux/err.h> 4 #include <linux/i2c.h> 5 #include <linux/kernel.h> 6 #include <linux/module.h> 7 #include "pmbus.h" 8 9 #define LX1308_MFR_IOUT_OCP3_FAULT 0xBE 10 #define LX1308_MFR_IOUT_OCP3_WARN 0xBF 11 12 /* 13 * Decode a Linear11-encoded word to an integer value. 14 * Linear11 format: bits[15:11] = signed 5-bit exponent, 15 * bits[10:0] = signed 11-bit mantissa. Result = mant * 2^exp. 16 */ 17 static inline int linear11_to_int(u16 word) 18 { 19 s16 exp = ((s16)word) >> 11; 20 s16 mant = ((s16)((word & 0x7ff) << 5)) >> 5; 21 22 return (exp >= 0) ? (int)((u32)mant << exp) : (mant >> -exp); 23 } 24 25 static int lx1308_read_word_data(struct i2c_client *client, int page, 26 int phase, int reg) 27 { 28 int ret; 29 30 if (page > 0) 31 return -ENXIO; 32 33 switch (reg) { 34 /* 35 * The LX1308 OCP3 registers (slow OCP, 60ms delay) use a 36 * manufacturer-specific U8.0 format. Read the byte value N and present 37 * it as a Linear11 word with exponent 0. 38 */ 39 case PMBUS_IOUT_OC_FAULT_LIMIT: 40 ret = i2c_smbus_read_byte_data(client, LX1308_MFR_IOUT_OCP3_FAULT); 41 if (ret < 0) 42 break; 43 ret &= 0x7FF; 44 break; 45 46 case PMBUS_IOUT_OC_WARN_LIMIT: 47 ret = i2c_smbus_read_byte_data(client, LX1308_MFR_IOUT_OCP3_WARN); 48 if (ret < 0) 49 break; 50 ret &= 0x7FF; 51 break; 52 53 /* 54 * The following registers are not implemented by the LX1308. Return 55 * -ENXIO to suppress the corresponding sysfs attributes. 56 */ 57 case PMBUS_IIN_OC_WARN_LIMIT: 58 case PMBUS_IIN_OC_FAULT_LIMIT: 59 case PMBUS_IOUT_UC_FAULT_LIMIT: 60 case PMBUS_PIN_OP_WARN_LIMIT: 61 case PMBUS_POUT_OP_WARN_LIMIT: 62 case PMBUS_UT_WARN_LIMIT: 63 case PMBUS_UT_FAULT_LIMIT: 64 case PMBUS_MFR_IIN_MAX: 65 case PMBUS_MFR_IOUT_MAX: 66 case PMBUS_MFR_VIN_MIN: 67 case PMBUS_MFR_VIN_MAX: 68 case PMBUS_MFR_VOUT_MIN: 69 case PMBUS_MFR_VOUT_MAX: 70 case PMBUS_MFR_PIN_MAX: 71 case PMBUS_MFR_POUT_MAX: 72 case PMBUS_MFR_MAX_TEMP_1: 73 ret = -ENXIO; 74 break; 75 76 default: 77 ret = -ENODATA; 78 break; 79 } 80 81 return ret; 82 } 83 84 static int lx1308_write_word_data(struct i2c_client *client, int page, 85 int reg, u16 word) 86 { 87 int ret; 88 89 if (page > 0) 90 return -ENXIO; 91 92 switch (reg) { 93 case PMBUS_IOUT_OC_FAULT_LIMIT: 94 /* 95 * Decode Linear11 word from pmbus_core back to a plain integer 96 * and write as the U8.0 byte the device expects. 97 */ 98 ret = i2c_smbus_write_byte_data(client, LX1308_MFR_IOUT_OCP3_FAULT, 99 clamp_val(linear11_to_int(word), 0, 255)); 100 break; 101 102 case PMBUS_IOUT_OC_WARN_LIMIT: 103 ret = i2c_smbus_write_byte_data(client, LX1308_MFR_IOUT_OCP3_WARN, 104 clamp_val(linear11_to_int(word), 0, 255)); 105 break; 106 107 default: 108 ret = -ENODATA; 109 break; 110 } 111 112 return ret; 113 } 114 115 static struct pmbus_driver_info lx1308_info = { 116 .pages = 1, 117 .format[PSC_VOLTAGE_IN] = linear, 118 .format[PSC_VOLTAGE_OUT] = linear, 119 .format[PSC_CURRENT_IN] = linear, 120 .format[PSC_CURRENT_OUT] = linear, 121 .format[PSC_POWER] = linear, 122 .format[PSC_TEMPERATURE] = linear, 123 124 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT 125 | PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT 126 | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT 127 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP 128 | PMBUS_HAVE_STATUS_INPUT, 129 130 .read_word_data = lx1308_read_word_data, 131 .write_word_data = lx1308_write_word_data, 132 }; 133 134 static const struct of_device_id lx1308_of_match[] = { 135 { .compatible = "luxshare,lx1308" }, 136 { } 137 }; 138 139 MODULE_DEVICE_TABLE(of, lx1308_of_match); 140 141 static const struct i2c_device_id lx1308_id[] = { 142 { "lx1308" }, 143 { } 144 }; 145 146 MODULE_DEVICE_TABLE(i2c, lx1308_id); 147 148 static int lx1308_probe(struct i2c_client *client) 149 { 150 u8 buf[I2C_SMBUS_BLOCK_MAX + 1]; 151 const struct i2c_device_id *mid; 152 int ret; 153 154 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf); 155 if (ret < 0) 156 return dev_err_probe(&client->dev, ret, 157 "Failed to read manufacturer id\n"); 158 buf[ret] = '\0'; 159 160 if (ret != 12 || strncmp(buf, "LUXSHARE", 8)) 161 return dev_err_probe(&client->dev, -ENODEV, 162 "Unsupported Manufacturer ID '%s'\n", buf); 163 164 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf); 165 if (ret < 0) 166 return dev_err_probe(&client->dev, ret, 167 "Failed to read Manufacturer Model\n"); 168 buf[ret] = '\0'; 169 170 for (mid = lx1308_id; mid->name[0]; mid++) { 171 if (!strncasecmp(mid->name, buf, strlen(mid->name))) 172 break; 173 } 174 if (!mid->name[0]) 175 return dev_err_probe(&client->dev, -ENODEV, 176 "Unsupported Manufacturer Model '%s'\n", buf); 177 178 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_REVISION, buf); 179 if (ret < 0) 180 return dev_err_probe(&client->dev, ret, 181 "Failed to read Manufacturer Revision\n"); 182 buf[ret] = '\0'; 183 184 if (ret != 12 || buf[0] != 'V') 185 return dev_err_probe(&client->dev, -ENODEV, 186 "Unsupported Manufacturer Revision '%s'\n", buf); 187 return pmbus_do_probe(client, &lx1308_info); 188 } 189 190 static struct i2c_driver lx1308_driver = { 191 .driver = { 192 .name = "lx1308", 193 .of_match_table = lx1308_of_match, 194 }, 195 .probe = lx1308_probe, 196 .id_table = lx1308_id, 197 }; 198 199 module_i2c_driver(lx1308_driver); 200 201 MODULE_AUTHOR("Brian Chiang <chiang.brian@inventec.com>"); 202 MODULE_DESCRIPTION("PMBus driver for Luxshare LX1308"); 203 MODULE_LICENSE("GPL"); 204 MODULE_IMPORT_NS("PMBUS"); 205