xref: /linux/drivers/hwmon/pmbus/pmbus.c (revision cdd5b5a9761fd66d17586e4f4ba6588c70e640ea)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29d2ecfb7SGuenter Roeck /*
39d2ecfb7SGuenter Roeck  * Hardware monitoring driver for PMBus devices
49d2ecfb7SGuenter Roeck  *
59d2ecfb7SGuenter Roeck  * Copyright (c) 2010, 2011 Ericsson AB.
69d2ecfb7SGuenter Roeck  */
79d2ecfb7SGuenter Roeck 
89d2ecfb7SGuenter Roeck #include <linux/kernel.h>
99d2ecfb7SGuenter Roeck #include <linux/module.h>
109d2ecfb7SGuenter Roeck #include <linux/init.h>
119d2ecfb7SGuenter Roeck #include <linux/err.h>
129d2ecfb7SGuenter Roeck #include <linux/slab.h>
139d2ecfb7SGuenter Roeck #include <linux/mutex.h>
149d2ecfb7SGuenter Roeck #include <linux/i2c.h>
154ba1bb12SWolfram Sang #include <linux/pmbus.h>
169d2ecfb7SGuenter Roeck #include "pmbus.h"
179d2ecfb7SGuenter Roeck 
186f4a46f0SXiaoting Liu struct pmbus_device_info {
196f4a46f0SXiaoting Liu 	int pages;
206f4a46f0SXiaoting Liu 	u32 flags;
216f4a46f0SXiaoting Liu };
226f4a46f0SXiaoting Liu 
23dd431939SStephen Kitt static const struct i2c_device_id pmbus_id[];
24dd431939SStephen Kitt 
259d2ecfb7SGuenter Roeck /*
269d2ecfb7SGuenter Roeck  * Find sensor groups and status registers on each page.
279d2ecfb7SGuenter Roeck  */
pmbus_find_sensor_groups(struct i2c_client * client,struct pmbus_driver_info * info)289d2ecfb7SGuenter Roeck static void pmbus_find_sensor_groups(struct i2c_client *client,
299d2ecfb7SGuenter Roeck 				     struct pmbus_driver_info *info)
309d2ecfb7SGuenter Roeck {
319d2ecfb7SGuenter Roeck 	int page;
329d2ecfb7SGuenter Roeck 
339d2ecfb7SGuenter Roeck 	/* Sensors detected on page 0 only */
349d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
359d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_VIN;
369d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
379d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_VCAP;
389d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
399d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_IIN;
409d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
419d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_PIN;
429d2ecfb7SGuenter Roeck 	if (info->func[0]
439d2ecfb7SGuenter Roeck 	    && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
449d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
459d2ecfb7SGuenter Roeck 	if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
469d2ecfb7SGuenter Roeck 	    pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
479d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_FAN12;
489d2ecfb7SGuenter Roeck 		if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
499d2ecfb7SGuenter Roeck 			info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
509d2ecfb7SGuenter Roeck 	}
519d2ecfb7SGuenter Roeck 	if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
529d2ecfb7SGuenter Roeck 	    pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
539d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_FAN34;
549d2ecfb7SGuenter Roeck 		if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
559d2ecfb7SGuenter Roeck 			info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
569d2ecfb7SGuenter Roeck 	}
579d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
589d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_TEMP;
599d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
609d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_TEMP2;
619d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
629d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_TEMP3;
639d2ecfb7SGuenter Roeck 	if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
649d2ecfb7SGuenter Roeck 			     | PMBUS_HAVE_TEMP3)
659d2ecfb7SGuenter Roeck 	    && pmbus_check_byte_register(client, 0,
669d2ecfb7SGuenter Roeck 					 PMBUS_STATUS_TEMPERATURE))
679d2ecfb7SGuenter Roeck 			info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
689d2ecfb7SGuenter Roeck 
699d2ecfb7SGuenter Roeck 	/* Sensors detected on all pages */
709d2ecfb7SGuenter Roeck 	for (page = 0; page < info->pages; page++) {
719d2ecfb7SGuenter Roeck 		if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
729d2ecfb7SGuenter Roeck 			info->func[page] |= PMBUS_HAVE_VOUT;
739d2ecfb7SGuenter Roeck 			if (pmbus_check_byte_register(client, page,
749d2ecfb7SGuenter Roeck 						      PMBUS_STATUS_VOUT))
759d2ecfb7SGuenter Roeck 				info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
769d2ecfb7SGuenter Roeck 		}
779d2ecfb7SGuenter Roeck 		if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
789d2ecfb7SGuenter Roeck 			info->func[page] |= PMBUS_HAVE_IOUT;
799d2ecfb7SGuenter Roeck 			if (pmbus_check_byte_register(client, 0,
809d2ecfb7SGuenter Roeck 						      PMBUS_STATUS_IOUT))
819d2ecfb7SGuenter Roeck 				info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
829d2ecfb7SGuenter Roeck 		}
839d2ecfb7SGuenter Roeck 		if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
849d2ecfb7SGuenter Roeck 			info->func[page] |= PMBUS_HAVE_POUT;
859d2ecfb7SGuenter Roeck 	}
869d2ecfb7SGuenter Roeck }
879d2ecfb7SGuenter Roeck 
889d2ecfb7SGuenter Roeck /*
899d2ecfb7SGuenter Roeck  * Identify chip parameters.
909d2ecfb7SGuenter Roeck  */
pmbus_identify(struct i2c_client * client,struct pmbus_driver_info * info)919d2ecfb7SGuenter Roeck static int pmbus_identify(struct i2c_client *client,
929d2ecfb7SGuenter Roeck 			  struct pmbus_driver_info *info)
939d2ecfb7SGuenter Roeck {
941061d851SGuenter Roeck 	int ret = 0;
951061d851SGuenter Roeck 
969d2ecfb7SGuenter Roeck 	if (!info->pages) {
979d2ecfb7SGuenter Roeck 		/*
989d2ecfb7SGuenter Roeck 		 * Check if the PAGE command is supported. If it is,
999d2ecfb7SGuenter Roeck 		 * keep setting the page number until it fails or until the
1009d2ecfb7SGuenter Roeck 		 * maximum number of pages has been reached. Assume that
1019d2ecfb7SGuenter Roeck 		 * this is the number of pages supported by the chip.
1029d2ecfb7SGuenter Roeck 		 */
1039d2ecfb7SGuenter Roeck 		if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
1049d2ecfb7SGuenter Roeck 			int page;
1059d2ecfb7SGuenter Roeck 
1069d2ecfb7SGuenter Roeck 			for (page = 1; page < PMBUS_PAGES; page++) {
10743f33b6eSGuenter Roeck 				if (pmbus_set_page(client, page, 0xff) < 0)
1089d2ecfb7SGuenter Roeck 					break;
1099d2ecfb7SGuenter Roeck 			}
11043f33b6eSGuenter Roeck 			pmbus_set_page(client, 0, 0xff);
1119d2ecfb7SGuenter Roeck 			info->pages = page;
1129d2ecfb7SGuenter Roeck 		} else {
1139d2ecfb7SGuenter Roeck 			info->pages = 1;
1149d2ecfb7SGuenter Roeck 		}
115e7c6a556SDmitry Bazhenov 
116e7c6a556SDmitry Bazhenov 		pmbus_clear_faults(client);
1179d2ecfb7SGuenter Roeck 	}
1189d2ecfb7SGuenter Roeck 
1191061d851SGuenter Roeck 	if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
120b9fa0a3aSVadim Pasternak 		int vout_mode, i;
1211061d851SGuenter Roeck 
1221061d851SGuenter Roeck 		vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
1231061d851SGuenter Roeck 		if (vout_mode >= 0 && vout_mode != 0xff) {
1241061d851SGuenter Roeck 			switch (vout_mode >> 5) {
1251061d851SGuenter Roeck 			case 0:
1261061d851SGuenter Roeck 				break;
1271061d851SGuenter Roeck 			case 1:
1281061d851SGuenter Roeck 				info->format[PSC_VOLTAGE_OUT] = vid;
129b9fa0a3aSVadim Pasternak 				for (i = 0; i < info->pages; i++)
130b9fa0a3aSVadim Pasternak 					info->vrm_version[i] = vr11;
1311061d851SGuenter Roeck 				break;
1321061d851SGuenter Roeck 			case 2:
1331061d851SGuenter Roeck 				info->format[PSC_VOLTAGE_OUT] = direct;
1341061d851SGuenter Roeck 				break;
1351061d851SGuenter Roeck 			default:
1361061d851SGuenter Roeck 				ret = -ENODEV;
1371061d851SGuenter Roeck 				goto abort;
1381061d851SGuenter Roeck 			}
1391061d851SGuenter Roeck 		}
1401061d851SGuenter Roeck 	}
1411061d851SGuenter Roeck 
1429d2ecfb7SGuenter Roeck 	/*
1439d2ecfb7SGuenter Roeck 	 * We should check if the COEFFICIENTS register is supported.
1449d2ecfb7SGuenter Roeck 	 * If it is, and the chip is configured for direct mode, we can read
1459d2ecfb7SGuenter Roeck 	 * the coefficients from the chip, one set per group of sensor
1469d2ecfb7SGuenter Roeck 	 * registers.
1479d2ecfb7SGuenter Roeck 	 *
1489d2ecfb7SGuenter Roeck 	 * To do this, we will need access to a chip which actually supports the
1499d2ecfb7SGuenter Roeck 	 * COEFFICIENTS command, since the command is too complex to implement
1501061d851SGuenter Roeck 	 * without testing it. Until then, abort if a chip configured for direct
1511061d851SGuenter Roeck 	 * mode was detected.
1529d2ecfb7SGuenter Roeck 	 */
1531061d851SGuenter Roeck 	if (info->format[PSC_VOLTAGE_OUT] == direct) {
1541061d851SGuenter Roeck 		ret = -ENODEV;
1551061d851SGuenter Roeck 		goto abort;
1561061d851SGuenter Roeck 	}
1579d2ecfb7SGuenter Roeck 
1589d2ecfb7SGuenter Roeck 	/* Try to find sensor groups  */
1599d2ecfb7SGuenter Roeck 	pmbus_find_sensor_groups(client, info);
1601061d851SGuenter Roeck abort:
1611061d851SGuenter Roeck 	return ret;
1629d2ecfb7SGuenter Roeck }
1639d2ecfb7SGuenter Roeck 
pmbus_probe(struct i2c_client * client)164dd431939SStephen Kitt static int pmbus_probe(struct i2c_client *client)
1659d2ecfb7SGuenter Roeck {
1669d2ecfb7SGuenter Roeck 	struct pmbus_driver_info *info;
167cc00decfSVadim Pasternak 	struct pmbus_platform_data *pdata = NULL;
168cc00decfSVadim Pasternak 	struct device *dev = &client->dev;
1696f4a46f0SXiaoting Liu 	struct pmbus_device_info *device_info;
1709d2ecfb7SGuenter Roeck 
171cc00decfSVadim Pasternak 	info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
1729d2ecfb7SGuenter Roeck 	if (!info)
1739d2ecfb7SGuenter Roeck 		return -ENOMEM;
1749d2ecfb7SGuenter Roeck 
175dd431939SStephen Kitt 	device_info = (struct pmbus_device_info *)i2c_match_id(pmbus_id, client)->driver_data;
176ea541c18SErik Rosen 	if (device_info->flags) {
177cc00decfSVadim Pasternak 		pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data),
178cc00decfSVadim Pasternak 				     GFP_KERNEL);
179cc00decfSVadim Pasternak 		if (!pdata)
180cc00decfSVadim Pasternak 			return -ENOMEM;
181cc00decfSVadim Pasternak 
182ea541c18SErik Rosen 		pdata->flags = device_info->flags;
183cc00decfSVadim Pasternak 	}
184cc00decfSVadim Pasternak 
1856f4a46f0SXiaoting Liu 	info->pages = device_info->pages;
1869d2ecfb7SGuenter Roeck 	info->identify = pmbus_identify;
187cc00decfSVadim Pasternak 	dev->platform_data = pdata;
1889d2ecfb7SGuenter Roeck 
189dd431939SStephen Kitt 	return pmbus_do_probe(client, info);
1909d2ecfb7SGuenter Roeck }
1919d2ecfb7SGuenter Roeck 
1926f4a46f0SXiaoting Liu static const struct pmbus_device_info pmbus_info_one = {
1936f4a46f0SXiaoting Liu 	.pages = 1,
1946f4a46f0SXiaoting Liu 	.flags = 0
1956f4a46f0SXiaoting Liu };
196ea541c18SErik Rosen 
1976f4a46f0SXiaoting Liu static const struct pmbus_device_info pmbus_info_zero = {
1986f4a46f0SXiaoting Liu 	.pages = 0,
1996f4a46f0SXiaoting Liu 	.flags = 0
2006f4a46f0SXiaoting Liu };
201ea541c18SErik Rosen 
2026f4a46f0SXiaoting Liu static const struct pmbus_device_info pmbus_info_one_skip = {
2036f4a46f0SXiaoting Liu 	.pages = 1,
2046f4a46f0SXiaoting Liu 	.flags = PMBUS_SKIP_STATUS_CHECK
2056f4a46f0SXiaoting Liu };
2066f4a46f0SXiaoting Liu 
207ea541c18SErik Rosen static const struct pmbus_device_info pmbus_info_one_status = {
208ea541c18SErik Rosen 	.pages = 1,
209ea541c18SErik Rosen 	.flags = PMBUS_READ_STATUS_AFTER_FAILED_CHECK
210ea541c18SErik Rosen };
211ea541c18SErik Rosen 
2129d2ecfb7SGuenter Roeck /*
2139d2ecfb7SGuenter Roeck  * Use driver_data to set the number of pages supported by the chip.
2149d2ecfb7SGuenter Roeck  */
2159d2ecfb7SGuenter Roeck static const struct i2c_device_id pmbus_id[] = {
2166f4a46f0SXiaoting Liu 	{"adp4000", (kernel_ulong_t)&pmbus_info_one},
217ea541c18SErik Rosen 	{"bmr310", (kernel_ulong_t)&pmbus_info_one_status},
2186f4a46f0SXiaoting Liu 	{"bmr453", (kernel_ulong_t)&pmbus_info_one},
2196f4a46f0SXiaoting Liu 	{"bmr454", (kernel_ulong_t)&pmbus_info_one},
220ea541c18SErik Rosen 	{"bmr456", (kernel_ulong_t)&pmbus_info_one},
221ea541c18SErik Rosen 	{"bmr457", (kernel_ulong_t)&pmbus_info_one},
222ea541c18SErik Rosen 	{"bmr458", (kernel_ulong_t)&pmbus_info_one_status},
223ea541c18SErik Rosen 	{"bmr480", (kernel_ulong_t)&pmbus_info_one_status},
224ea541c18SErik Rosen 	{"bmr490", (kernel_ulong_t)&pmbus_info_one_status},
225ea541c18SErik Rosen 	{"bmr491", (kernel_ulong_t)&pmbus_info_one_status},
226ea541c18SErik Rosen 	{"bmr492", (kernel_ulong_t)&pmbus_info_one},
2276f4a46f0SXiaoting Liu 	{"dps460", (kernel_ulong_t)&pmbus_info_one_skip},
228705f2c81SXiaoting Liu 	{"dps650ab", (kernel_ulong_t)&pmbus_info_one_skip},
2296f4a46f0SXiaoting Liu 	{"dps800", (kernel_ulong_t)&pmbus_info_one_skip},
2301a1ea120SGuenter Roeck 	{"max20796", (kernel_ulong_t)&pmbus_info_one},
2316f4a46f0SXiaoting Liu 	{"mdt040", (kernel_ulong_t)&pmbus_info_one},
2326f4a46f0SXiaoting Liu 	{"ncp4200", (kernel_ulong_t)&pmbus_info_one},
2336f4a46f0SXiaoting Liu 	{"ncp4208", (kernel_ulong_t)&pmbus_info_one},
2346f4a46f0SXiaoting Liu 	{"pdt003", (kernel_ulong_t)&pmbus_info_one},
2356f4a46f0SXiaoting Liu 	{"pdt006", (kernel_ulong_t)&pmbus_info_one},
2366f4a46f0SXiaoting Liu 	{"pdt012", (kernel_ulong_t)&pmbus_info_one},
2376f4a46f0SXiaoting Liu 	{"pmbus", (kernel_ulong_t)&pmbus_info_zero},
2386f4a46f0SXiaoting Liu 	{"sgd009", (kernel_ulong_t)&pmbus_info_one_skip},
2396f4a46f0SXiaoting Liu 	{"tps40400", (kernel_ulong_t)&pmbus_info_one},
2406f4a46f0SXiaoting Liu 	{"tps544b20", (kernel_ulong_t)&pmbus_info_one},
2416f4a46f0SXiaoting Liu 	{"tps544b25", (kernel_ulong_t)&pmbus_info_one},
2426f4a46f0SXiaoting Liu 	{"tps544c20", (kernel_ulong_t)&pmbus_info_one},
2436f4a46f0SXiaoting Liu 	{"tps544c25", (kernel_ulong_t)&pmbus_info_one},
2446f4a46f0SXiaoting Liu 	{"udt020", (kernel_ulong_t)&pmbus_info_one},
2459d2ecfb7SGuenter Roeck 	{}
2469d2ecfb7SGuenter Roeck };
2479d2ecfb7SGuenter Roeck 
2489d2ecfb7SGuenter Roeck MODULE_DEVICE_TABLE(i2c, pmbus_id);
2499d2ecfb7SGuenter Roeck 
2509d2ecfb7SGuenter Roeck /* This is the driver that will be inserted */
2519d2ecfb7SGuenter Roeck static struct i2c_driver pmbus_driver = {
2529d2ecfb7SGuenter Roeck 	.driver = {
2539d2ecfb7SGuenter Roeck 		   .name = "pmbus",
2549d2ecfb7SGuenter Roeck 		   },
255*1975d167SUwe Kleine-König 	.probe = pmbus_probe,
2569d2ecfb7SGuenter Roeck 	.id_table = pmbus_id,
2579d2ecfb7SGuenter Roeck };
2589d2ecfb7SGuenter Roeck 
259f0967eeaSAxel Lin module_i2c_driver(pmbus_driver);
2609d2ecfb7SGuenter Roeck 
2619d2ecfb7SGuenter Roeck MODULE_AUTHOR("Guenter Roeck");
2629d2ecfb7SGuenter Roeck MODULE_DESCRIPTION("Generic PMBus driver");
2639d2ecfb7SGuenter Roeck MODULE_LICENSE("GPL");
264b94ca77eSGuenter Roeck MODULE_IMPORT_NS(PMBUS);
265