drivers/hwmon/pmbus/mp2985.c

Source file repositories/reference/linux-study-clean/drivers/hwmon/pmbus/mp2985.c

File Facts

System
Linux kernel
Corpus path
drivers/hwmon/pmbus/mp2985.c
Extension
.c
Size
11081 bytes
Lines
403
Domain
Driver Families
Bucket
drivers/hwmon
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct mp2985_data {
	struct pmbus_driver_info info;
	int vout_scale[MP2985_PAGE_NUM];
	int vid_offset[MP2985_PAGE_NUM];
};

#define to_mp2985_data(x) container_of(x, struct mp2985_data, info)

static u16 mp2985_linear_exp_transfer(u16 word, u16 expect_exponent)
{
	s16 exponent, mantissa, target_exponent;

	exponent = ((s16)word) >> 11;
	mantissa = ((s16)((word & 0x7ff) << 5)) >> 5;
	target_exponent = (s16)((expect_exponent & 0x1f) << 11) >> 11;

	/*
	 * The MP2985 does not support negtive limit value, if a negtive
	 * limit value is written, the limit value will become to 0. And
	 * the maximum positive limit value is limitted to 0x3FF.
	 */
	if (mantissa < 0) {
		mantissa = 0;
	} else {
		if (exponent > target_exponent) {
			mantissa = (1023 >> (exponent - target_exponent)) >= mantissa ?
						mantissa << (exponent - target_exponent) :
						0x3FF;
		} else {
			mantissa = clamp_val(mantissa >> (target_exponent - exponent),
					     0, 0x3FF);
		}
	}

	return mantissa | ((expect_exponent << 11) & 0xf800);
}

static int mp2985_read_byte_data(struct i2c_client *client, int page, int reg)
{
	int ret;

	switch (reg) {
	case PMBUS_VOUT_MODE:
		/*
		 * The MP2985 does not follow standard PMBus protocol completely,
		 * and the calculation of vout in this driver is based on direct
		 * format. As a result, the format of vout is enforced to direct.
		 */
		ret = PB_VOUT_MODE_DIRECT;
		break;
	default:
		ret = -ENODATA;
		break;
	}

	return ret;
}

static int mp2985_read_word_data(struct i2c_client *client, int page, int phase,
				 int reg)
{
	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
	struct mp2985_data *data = to_mp2985_data(info);
	int ret;

	switch (reg) {
	case PMBUS_READ_VOUT:
		ret = pmbus_read_word_data(client, page, phase, reg);
		if (ret < 0)
			return ret;

		/*
		 * The MP2985 supports three vout mode, direct, linear11 and vid mode.
		 * In vid mode, the MP2985 vout telemetry has 49 vid step offset, but
		 * PMBUS_VOUT_OV_FAULT_LIMIT and PMBUS_VOUT_UV_FAULT_LIMIT do not take
		 * this into consideration, their resolution are 1.953125mV/LSB, as a
		 * result, format[PSC_VOLTAGE_OUT] can not be set to vid mode directly.
		 * Adding extra vid_offset variable for vout telemetry.
		 */
		ret = clamp_val(DIV_ROUND_CLOSEST(((ret & GENMASK(11, 0)) +
									data->vid_offset[page]) *
							data->vout_scale[page], MP2985_VOUT_DIV),
							0, 0x7FFF);
		break;
	case PMBUS_READ_IIN:
		/*
		 * The MP2985 has standard PMBUS_READ_IIN register(0x89), but this is
		 * not used to read the input current of per rail. The input current
		 * is read through the vender redefined register READ_IIN_EST(0x8E).
		 */

Annotation

Implementation Notes