File Index
Files and directories
Sign in
FILE

pmbus.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hwmon/pmbus/pmbus.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Hardware monitoring driver for PMBus devices
  4 *
  5 * Copyright (c) 2010, 2011 Ericsson AB.
  6 */
  7
  8#include <linux/kernel.h>
  9#include <linux/module.h>
 10#include <linux/init.h>
 11#include <linux/err.h>
 12#include <linux/slab.h>
 13#include <linux/mutex.h>
 14#include <linux/i2c.h>
 15#include <linux/pmbus.h>
 16#include "pmbus.h"
 17
 18struct pmbus_device_info {
 19	int pages;
 20	u32 flags;
 21};
 22
 23static const struct i2c_device_id pmbus_id[];
 24
 25/*
 26 * Find sensor groups and status registers on each page.
 27 */
 28static void pmbus_find_sensor_groups(struct i2c_client *client,
 29				     struct pmbus_driver_info *info)
 30{
 31	int page;
 32
 33	/* Sensors detected on page 0 only */
 34	if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
 35		info->func[0] |= PMBUS_HAVE_VIN;
 36	if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
 37		info->func[0] |= PMBUS_HAVE_VCAP;
 38	if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
 39		info->func[0] |= PMBUS_HAVE_IIN;
 40	if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
 41		info->func[0] |= PMBUS_HAVE_PIN;
 42	if (info->func[0]
 43	    && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
 44		info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
 45	if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
 46	    pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
 47		info->func[0] |= PMBUS_HAVE_FAN12;
 48		if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
 49			info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
 50	}
 51	if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
 52	    pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
 53		info->func[0] |= PMBUS_HAVE_FAN34;
 54		if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
 55			info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
 56	}
 57	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
 58		info->func[0] |= PMBUS_HAVE_TEMP;
 59	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
 60		info->func[0] |= PMBUS_HAVE_TEMP2;
 61	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
 62		info->func[0] |= PMBUS_HAVE_TEMP3;
 63	if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
 64			     | PMBUS_HAVE_TEMP3)
 65	    && pmbus_check_byte_register(client, 0,
 66					 PMBUS_STATUS_TEMPERATURE))
 67			info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
 68
 69	/* Sensors detected on all pages */
 70	for (page = 0; page < info->pages; page++) {
 71		if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
 72			info->func[page] |= PMBUS_HAVE_VOUT;
 73			if (pmbus_check_byte_register(client, page,
 74						      PMBUS_STATUS_VOUT))
 75				info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
 76		}
 77		if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
 78			info->func[page] |= PMBUS_HAVE_IOUT;
 79			if (pmbus_check_byte_register(client, 0,
 80						      PMBUS_STATUS_IOUT))
 81				info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
 82		}
 83		if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
 84			info->func[page] |= PMBUS_HAVE_POUT;
 85	}
 86}
 87
 88/*
 89 * Identify chip parameters.
 90 */
 91static int pmbus_identify(struct i2c_client *client,
 92			  struct pmbus_driver_info *info)
 93{
 94	int ret = 0;
 95
 96	if (!info->pages) {
 97		/*
 98		 * Check if the PAGE command is supported. If it is,
 99		 * keep setting the page number until it fails or until the
100		 * maximum number of pages has been reached. Assume that
101		 * this is the number of pages supported by the chip.
102		 */
103		if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
104			int page;
105
106			info->pages = PMBUS_PAGES;
107
108			for (page = 1; page < PMBUS_PAGES; page++) {
109				if (pmbus_set_page(client, page, 0xff) < 0)
110					break;
111			}
112			pmbus_set_page(client, 0, 0xff);
113			info->pages = page;
114		} else {
115			info->pages = 1;
116		}
117
118		pmbus_clear_faults(client);
119	}
120
121	if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
122		int vout_mode, i;
123
124		vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
125		if (vout_mode >= 0 && vout_mode != 0xff) {
126			switch (vout_mode >> 5) {
127			case 0:
128				break;
129			case 1:
130				info->format[PSC_VOLTAGE_OUT] = vid;
131				for (i = 0; i < info->pages; i++)
132					info->vrm_version[i] = vr11;
133				break;
134			case 2:
135				info->format[PSC_VOLTAGE_OUT] = direct;
136				break;
137			default:
138				ret = -ENODEV;
139				goto abort;
140			}
141		}
142	}
143
144	/*
145	 * We should check if the COEFFICIENTS register is supported.
146	 * If it is, and the chip is configured for direct mode, we can read
147	 * the coefficients from the chip, one set per group of sensor
148	 * registers.
149	 *
150	 * To do this, we will need access to a chip which actually supports the
151	 * COEFFICIENTS command, since the command is too complex to implement
152	 * without testing it. Until then, abort if a chip configured for direct
153	 * mode was detected.
154	 */
155	if (info->format[PSC_VOLTAGE_OUT] == direct) {
156		ret = -ENODEV;
157		goto abort;
158	}
159
160	/* Try to find sensor groups  */
161	pmbus_find_sensor_groups(client, info);
162abort:
163	return ret;
164}
165
166static int pmbus_probe(struct i2c_client *client)
167{
168	struct pmbus_driver_info *info;
169	struct pmbus_platform_data *pdata = NULL;
170	struct device *dev = &client->dev;
171	struct pmbus_device_info *device_info;
172
173	info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
174	if (!info)
175		return -ENOMEM;
176
177	device_info = (struct pmbus_device_info *)i2c_match_id(pmbus_id, client)->driver_data;
178	if (device_info->flags) {
179		pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data),
180				     GFP_KERNEL);
181		if (!pdata)
182			return -ENOMEM;
183
184		pdata->flags = device_info->flags;
185	}
186
187	info->pages = device_info->pages;
188	info->identify = pmbus_identify;
189	dev->platform_data = pdata;
190
191	return pmbus_do_probe(client, info);
192}
193
194static const struct pmbus_device_info pmbus_info_one = {
195	.pages = 1,
196	.flags = 0
197};
198
199static const struct pmbus_device_info pmbus_info_zero = {
200	.pages = 0,
201	.flags = 0
202};
203
204static const struct pmbus_device_info pmbus_info_one_skip = {
205	.pages = 1,
206	.flags = PMBUS_SKIP_STATUS_CHECK
207};
208
209static const struct pmbus_device_info pmbus_info_one_status = {
210	.pages = 1,
211	.flags = PMBUS_READ_STATUS_AFTER_FAILED_CHECK
212};
213
214/*
215 * Use driver_data to set the number of pages supported by the chip.
216 */
217static const struct i2c_device_id pmbus_id[] = {
218	{"adp4000", (kernel_ulong_t)&pmbus_info_one},
219	{"bmr310", (kernel_ulong_t)&pmbus_info_one_status},
220	{"bmr453", (kernel_ulong_t)&pmbus_info_one},
221	{"bmr454", (kernel_ulong_t)&pmbus_info_one},
222	{"bmr456", (kernel_ulong_t)&pmbus_info_one},
223	{"bmr457", (kernel_ulong_t)&pmbus_info_one},
224	{"bmr458", (kernel_ulong_t)&pmbus_info_one_status},
225	{"bmr480", (kernel_ulong_t)&pmbus_info_one_status},
226	{"bmr490", (kernel_ulong_t)&pmbus_info_one_status},
227	{"bmr491", (kernel_ulong_t)&pmbus_info_one_status},
228	{"bmr492", (kernel_ulong_t)&pmbus_info_one},
229	{"dps460", (kernel_ulong_t)&pmbus_info_one_skip},
230	{"dps650ab", (kernel_ulong_t)&pmbus_info_one_skip},
231	{"dps800", (kernel_ulong_t)&pmbus_info_one_skip},
232	{"max20796", (kernel_ulong_t)&pmbus_info_one},
233	{"mdt040", (kernel_ulong_t)&pmbus_info_one},
234	{"ncp4200", (kernel_ulong_t)&pmbus_info_one},
235	{"ncp4208", (kernel_ulong_t)&pmbus_info_one},
236	{"pdt003", (kernel_ulong_t)&pmbus_info_one},
237	{"pdt006", (kernel_ulong_t)&pmbus_info_one},
238	{"pdt012", (kernel_ulong_t)&pmbus_info_one},
239	{"pmbus", (kernel_ulong_t)&pmbus_info_zero},
240	{"sgd009", (kernel_ulong_t)&pmbus_info_one_skip},
241	{"tps40400", (kernel_ulong_t)&pmbus_info_one},
242	{"tps544b20", (kernel_ulong_t)&pmbus_info_one},
243	{"tps544b25", (kernel_ulong_t)&pmbus_info_one},
244	{"tps544c20", (kernel_ulong_t)&pmbus_info_one},
245	{"tps544c25", (kernel_ulong_t)&pmbus_info_one},
246	{"udt020", (kernel_ulong_t)&pmbus_info_one},
247	{}
248};
249
250MODULE_DEVICE_TABLE(i2c, pmbus_id);
251
252/* This is the driver that will be inserted */
253static struct i2c_driver pmbus_driver = {
254	.driver = {
255		   .name = "pmbus",
256		   },
257	.probe = pmbus_probe,
258	.id_table = pmbus_id,
259};
260
261module_i2c_driver(pmbus_driver);
262
263MODULE_AUTHOR("Guenter Roeck");
264MODULE_DESCRIPTION("Generic PMBus driver");
265MODULE_LICENSE("GPL");
266MODULE_IMPORT_NS("PMBUS");
~/ Read-only file preview.