File Index
Files and directories
Sign in
FILE

mpq8785.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hwmon/pmbus/mpq8785.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Driver for MPS MPQ8785 Step-Down Converter
  4 */
  5
  6#include <linux/i2c.h>
  7#include <linux/bitops.h>
  8#include <linux/module.h>
  9#include <linux/property.h>
 10#include <linux/of_device.h>
 11#include "pmbus.h"
 12
 13#define MPM82504_READ_TEMPERATURE_1_SIGN_POS	9
 14
 15enum chips { mpm3695, mpm3695_25, mpm82504, mpq8785 };
 16
 17static u16 voltage_scale_loop_max_val[] = {
 18	[mpm3695] = GENMASK(9, 0),
 19	[mpm3695_25] = GENMASK(11, 0),
 20	[mpm82504] = GENMASK(9, 0),
 21	[mpq8785] = GENMASK(10, 0),
 22};
 23
 24static int mpq8785_identify(struct i2c_client *client,
 25			    struct pmbus_driver_info *info)
 26{
 27	int vout_mode;
 28
 29	vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
 30	if (vout_mode < 0 || vout_mode == 0xff)
 31		return vout_mode < 0 ? vout_mode : -ENODEV;
 32	switch (vout_mode >> 5) {
 33	case 0:
 34		info->format[PSC_VOLTAGE_OUT] = linear;
 35		break;
 36	case 1:
 37	case 2:
 38		info->format[PSC_VOLTAGE_OUT] = direct;
 39		info->m[PSC_VOLTAGE_OUT] = 64;
 40		info->b[PSC_VOLTAGE_OUT] = 0;
 41		info->R[PSC_VOLTAGE_OUT] = 1;
 42		break;
 43	default:
 44		return -ENODEV;
 45	}
 46
 47	return 0;
 48};
 49
 50static int mpq8785_read_byte_data(struct i2c_client *client, int page, int reg)
 51{
 52	int ret;
 53
 54	switch (reg) {
 55	case PMBUS_VOUT_MODE:
 56		ret = pmbus_read_byte_data(client, page, reg);
 57		if (ret < 0)
 58			return ret;
 59
 60		if ((ret >> 5) == 1) {
 61			/*
 62			 * The MPQ8785 chip reports VOUT_MODE as VID mode, but the driver
 63			 * treats VID as direct mode. Without this, identification would fail
 64			 * due to mode mismatch.
 65			 * This override ensures the reported mode matches the driver
 66			 * configuration, allowing successful initialization.
 67			 */
 68			return PB_VOUT_MODE_DIRECT;
 69		}
 70
 71		return ret;
 72	default:
 73		return -ENODATA;
 74	}
 75}
 76
 77static int mpm82504_read_word_data(struct i2c_client *client, int page,
 78				   int phase, int reg)
 79{
 80	int ret;
 81
 82	ret = pmbus_read_word_data(client, page, phase, reg);
 83
 84	if (ret < 0 || reg != PMBUS_READ_TEMPERATURE_1)
 85		return ret;
 86
 87	/* Fix PMBUS_READ_TEMPERATURE_1 signedness */
 88	return sign_extend32(ret, MPM82504_READ_TEMPERATURE_1_SIGN_POS) & 0xffff;
 89}
 90
 91static struct pmbus_driver_info mpq8785_info = {
 92	.pages = 1,
 93	.format[PSC_VOLTAGE_IN] = direct,
 94	.format[PSC_CURRENT_OUT] = direct,
 95	.format[PSC_TEMPERATURE] = direct,
 96	.m[PSC_VOLTAGE_IN] = 4,
 97	.b[PSC_VOLTAGE_IN] = 0,
 98	.R[PSC_VOLTAGE_IN] = 1,
 99	.m[PSC_CURRENT_OUT] = 16,
100	.b[PSC_CURRENT_OUT] = 0,
101	.R[PSC_CURRENT_OUT] = 0,
102	.m[PSC_TEMPERATURE] = 1,
103	.b[PSC_TEMPERATURE] = 0,
104	.R[PSC_TEMPERATURE] = 0,
105	.func[0] =
106		PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT |
107		PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
108		PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
109		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
110};
111
112static const struct i2c_device_id mpq8785_id[] = {
113	{ "mpm3695", mpm3695 },
114	{ "mpm3695-25", mpm3695_25 },
115	{ "mpm82504", mpm82504 },
116	{ "mpq8785", mpq8785 },
117	{ },
118};
119MODULE_DEVICE_TABLE(i2c, mpq8785_id);
120
121static const struct of_device_id __maybe_unused mpq8785_of_match[] = {
122	{ .compatible = "mps,mpm3695", .data = (void *)mpm3695 },
123	{ .compatible = "mps,mpm3695-25", .data = (void *)mpm3695_25 },
124	{ .compatible = "mps,mpm82504", .data = (void *)mpm82504 },
125	{ .compatible = "mps,mpq8785", .data = (void *)mpq8785 },
126	{}
127};
128MODULE_DEVICE_TABLE(of, mpq8785_of_match);
129
130static int mpq8785_probe(struct i2c_client *client)
131{
132	struct device *dev = &client->dev;
133	struct pmbus_driver_info *info;
134	enum chips chip_id;
135	u32 voltage_scale;
136	int ret;
137
138	info = devm_kmemdup(dev, &mpq8785_info, sizeof(*info), GFP_KERNEL);
139	if (!info)
140		return -ENOMEM;
141
142	if (dev->of_node)
143		chip_id = (kernel_ulong_t)of_device_get_match_data(dev);
144	else
145		chip_id = (kernel_ulong_t)i2c_get_match_data(client);
146
147	switch (chip_id) {
148	case mpm3695:
149	case mpm3695_25:
150	case mpm82504:
151		info->format[PSC_VOLTAGE_OUT] = direct;
152		info->m[PSC_VOLTAGE_OUT] = 8;
153		info->b[PSC_VOLTAGE_OUT] = 0;
154		info->R[PSC_VOLTAGE_OUT] = 2;
155		info->read_word_data = mpm82504_read_word_data;
156		break;
157	case mpq8785:
158		info->identify = mpq8785_identify;
159		info->read_byte_data = mpq8785_read_byte_data;
160		break;
161	default:
162		return -ENODEV;
163	}
164
165	if (!device_property_read_u32(dev, "mps,vout-fb-divider-ratio-permille",
166				      &voltage_scale)) {
167		if (voltage_scale > voltage_scale_loop_max_val[chip_id])
168			return -EINVAL;
169
170		ret = i2c_smbus_write_word_data(client, PMBUS_VOUT_SCALE_LOOP,
171						voltage_scale);
172		if (ret)
173			return ret;
174	}
175
176	return pmbus_do_probe(client, info);
177};
178
179static struct i2c_driver mpq8785_driver = {
180	.driver = {
181		   .name = "mpq8785",
182		   .of_match_table = of_match_ptr(mpq8785_of_match),
183	},
184	.probe = mpq8785_probe,
185	.id_table = mpq8785_id,
186};
187
188module_i2c_driver(mpq8785_driver);
189
190MODULE_AUTHOR("Charles Hsu <ythsu0511@gmail.com>");
191MODULE_DESCRIPTION("PMBus driver for MPS MPQ8785");
192MODULE_LICENSE("GPL");
193MODULE_IMPORT_NS("PMBUS");
~/ Read-only file preview.