1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hardware monitoring driver for the STPDDC60 controller
5 * Copyright (c) 2021 Flextronics International Sweden AB.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/pmbus.h>
16 #define STPDDC60_MFR_READ_VOUT 0xd2
17 #define STPDDC60_MFR_OV_LIMIT_OFFSET 0xe5
18 #define STPDDC60_MFR_UV_LIMIT_OFFSET 0xe6
20 static const struct i2c_device_id stpddc60_id[] = {
25 MODULE_DEVICE_TABLE(i2c, stpddc60_id);
27 static struct pmbus_driver_info stpddc60_info = {
29 .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
30 | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
31 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
32 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
37 * Calculate the closest absolute offset between commanded vout value
38 * and limit value in steps of 50mv in the range 0 (50mv) to 7 (400mv).
39 * Return 0 if the upper limit is lower than vout or if the lower limit
40 * is higher than vout.
42 static u8 stpddc60_get_offset(int vout, u16 limit, bool over)
47 v = 250 + (vout - 1) * 5; /* Convert VID to mv */
48 l = (limit * 1000L) >> 8; /* Convert LINEAR to mv */
53 offset = DIV_ROUND_CLOSEST(abs(l - v), 50);
58 return clamp_val(offset, 0, 7);
62 * Adjust the linear format word to use the given fixed exponent.
64 static u16 stpddc60_adjust_linear(u16 word, s16 fixed)
68 e = ((s16)word) >> 11;
69 m = ((s16)((word & 0x7ff) << 5)) >> 5;
77 return clamp_val(m, 0, 0x3ff) | ((fixed << 11) & 0xf800);
81 * The VOUT_COMMAND register uses the VID format but the vout alarm limit
82 * registers use the LINEAR format so we override VOUT_MODE here to force
83 * LINEAR format for all registers.
85 static int stpddc60_read_byte_data(struct i2c_client *client, int page, int reg)
105 * The vout related registers return values in LINEAR11 format when LINEAR16
106 * is expected. Clear the top 5 bits to set the exponent part to zero to
107 * convert the value to LINEAR16 format.
109 static int stpddc60_read_word_data(struct i2c_client *client, int page,
118 case PMBUS_READ_VOUT:
119 ret = pmbus_read_word_data(client, page, phase,
120 STPDDC60_MFR_READ_VOUT);
125 case PMBUS_VOUT_OV_FAULT_LIMIT:
126 case PMBUS_VOUT_UV_FAULT_LIMIT:
127 ret = pmbus_read_word_data(client, page, phase, reg);
141 * The vout under- and over-voltage limits are set as an offset relative to
142 * the commanded vout voltage. The vin, iout, pout and temp limits must use
143 * the same fixed exponent the chip uses to encode the data when read.
145 static int stpddc60_write_word_data(struct i2c_client *client, int page,
155 case PMBUS_VOUT_OV_FAULT_LIMIT:
156 ret = pmbus_read_word_data(client, page, 0xff,
160 offset = stpddc60_get_offset(ret, word, true);
161 ret = pmbus_write_byte_data(client, page,
162 STPDDC60_MFR_OV_LIMIT_OFFSET,
165 case PMBUS_VOUT_UV_FAULT_LIMIT:
166 ret = pmbus_read_word_data(client, page, 0xff,
170 offset = stpddc60_get_offset(ret, word, false);
171 ret = pmbus_write_byte_data(client, page,
172 STPDDC60_MFR_UV_LIMIT_OFFSET,
175 case PMBUS_VIN_OV_FAULT_LIMIT:
176 case PMBUS_VIN_UV_FAULT_LIMIT:
177 case PMBUS_OT_FAULT_LIMIT:
178 case PMBUS_OT_WARN_LIMIT:
179 case PMBUS_IOUT_OC_FAULT_LIMIT:
180 case PMBUS_IOUT_OC_WARN_LIMIT:
181 case PMBUS_POUT_OP_FAULT_LIMIT:
182 ret = pmbus_read_word_data(client, page, 0xff, reg);
185 word = stpddc60_adjust_linear(word, ret >> 11);
186 ret = pmbus_write_word_data(client, page, reg, word);
196 static int stpddc60_probe(struct i2c_client *client)
199 u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
200 const struct i2c_device_id *mid;
201 struct pmbus_driver_info *info = &stpddc60_info;
203 if (!i2c_check_functionality(client->adapter,
204 I2C_FUNC_SMBUS_READ_BYTE_DATA
205 | I2C_FUNC_SMBUS_BLOCK_DATA))
208 status = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, device_id);
210 dev_err(&client->dev, "Failed to read Manufacturer Model\n");
213 for (mid = stpddc60_id; mid->name[0]; mid++) {
214 if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
218 dev_err(&client->dev, "Unsupported device\n");
222 info->read_byte_data = stpddc60_read_byte_data;
223 info->read_word_data = stpddc60_read_word_data;
224 info->write_word_data = stpddc60_write_word_data;
226 status = pmbus_do_probe(client, info);
230 pmbus_set_update(client, PMBUS_VOUT_OV_FAULT_LIMIT, true);
231 pmbus_set_update(client, PMBUS_VOUT_UV_FAULT_LIMIT, true);
236 static struct i2c_driver stpddc60_driver = {
240 .probe_new = stpddc60_probe,
241 .id_table = stpddc60_id,
244 module_i2c_driver(stpddc60_driver);
246 MODULE_AUTHOR("Erik Rosen <erik.rosen@metormote.com>");
247 MODULE_DESCRIPTION("PMBus driver for ST STPDDC60");
248 MODULE_LICENSE("GPL");
249 MODULE_IMPORT_NS(PMBUS);