xref: /linux/drivers/hwmon/pmbus/pmbus.c (revision 4ba1bb12cf21f4ee4681aee939c4d9d82d6f49f2)
19d2ecfb7SGuenter Roeck /*
29d2ecfb7SGuenter Roeck  * Hardware monitoring driver for PMBus devices
39d2ecfb7SGuenter Roeck  *
49d2ecfb7SGuenter Roeck  * Copyright (c) 2010, 2011 Ericsson AB.
59d2ecfb7SGuenter Roeck  *
69d2ecfb7SGuenter Roeck  * This program is free software; you can redistribute it and/or modify
79d2ecfb7SGuenter Roeck  * it under the terms of the GNU General Public License as published by
89d2ecfb7SGuenter Roeck  * the Free Software Foundation; either version 2 of the License, or
99d2ecfb7SGuenter Roeck  * (at your option) any later version.
109d2ecfb7SGuenter Roeck  *
119d2ecfb7SGuenter Roeck  * This program is distributed in the hope that it will be useful,
129d2ecfb7SGuenter Roeck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
139d2ecfb7SGuenter Roeck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
149d2ecfb7SGuenter Roeck  * GNU General Public License for more details.
159d2ecfb7SGuenter Roeck  *
169d2ecfb7SGuenter Roeck  * You should have received a copy of the GNU General Public License
179d2ecfb7SGuenter Roeck  * along with this program; if not, write to the Free Software
189d2ecfb7SGuenter Roeck  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
199d2ecfb7SGuenter Roeck  */
209d2ecfb7SGuenter Roeck 
219d2ecfb7SGuenter Roeck #include <linux/kernel.h>
229d2ecfb7SGuenter Roeck #include <linux/module.h>
239d2ecfb7SGuenter Roeck #include <linux/init.h>
249d2ecfb7SGuenter Roeck #include <linux/err.h>
259d2ecfb7SGuenter Roeck #include <linux/slab.h>
269d2ecfb7SGuenter Roeck #include <linux/mutex.h>
279d2ecfb7SGuenter Roeck #include <linux/i2c.h>
28*4ba1bb12SWolfram Sang #include <linux/pmbus.h>
299d2ecfb7SGuenter Roeck #include "pmbus.h"
309d2ecfb7SGuenter Roeck 
319d2ecfb7SGuenter Roeck /*
329d2ecfb7SGuenter Roeck  * Find sensor groups and status registers on each page.
339d2ecfb7SGuenter Roeck  */
349d2ecfb7SGuenter Roeck static void pmbus_find_sensor_groups(struct i2c_client *client,
359d2ecfb7SGuenter Roeck 				     struct pmbus_driver_info *info)
369d2ecfb7SGuenter Roeck {
379d2ecfb7SGuenter Roeck 	int page;
389d2ecfb7SGuenter Roeck 
399d2ecfb7SGuenter Roeck 	/* Sensors detected on page 0 only */
409d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
419d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_VIN;
429d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
439d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_VCAP;
449d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
459d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_IIN;
469d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
479d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_PIN;
489d2ecfb7SGuenter Roeck 	if (info->func[0]
499d2ecfb7SGuenter Roeck 	    && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
509d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
519d2ecfb7SGuenter Roeck 	if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
529d2ecfb7SGuenter Roeck 	    pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
539d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_FAN12;
549d2ecfb7SGuenter Roeck 		if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
559d2ecfb7SGuenter Roeck 			info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
569d2ecfb7SGuenter Roeck 	}
579d2ecfb7SGuenter Roeck 	if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
589d2ecfb7SGuenter Roeck 	    pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
599d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_FAN34;
609d2ecfb7SGuenter Roeck 		if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
619d2ecfb7SGuenter Roeck 			info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
629d2ecfb7SGuenter Roeck 	}
639d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
649d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_TEMP;
659d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
669d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_TEMP2;
679d2ecfb7SGuenter Roeck 	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
689d2ecfb7SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_TEMP3;
699d2ecfb7SGuenter Roeck 	if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
709d2ecfb7SGuenter Roeck 			     | PMBUS_HAVE_TEMP3)
719d2ecfb7SGuenter Roeck 	    && pmbus_check_byte_register(client, 0,
729d2ecfb7SGuenter Roeck 					 PMBUS_STATUS_TEMPERATURE))
739d2ecfb7SGuenter Roeck 			info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
749d2ecfb7SGuenter Roeck 
759d2ecfb7SGuenter Roeck 	/* Sensors detected on all pages */
769d2ecfb7SGuenter Roeck 	for (page = 0; page < info->pages; page++) {
779d2ecfb7SGuenter Roeck 		if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
789d2ecfb7SGuenter Roeck 			info->func[page] |= PMBUS_HAVE_VOUT;
799d2ecfb7SGuenter Roeck 			if (pmbus_check_byte_register(client, page,
809d2ecfb7SGuenter Roeck 						      PMBUS_STATUS_VOUT))
819d2ecfb7SGuenter Roeck 				info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
829d2ecfb7SGuenter Roeck 		}
839d2ecfb7SGuenter Roeck 		if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
849d2ecfb7SGuenter Roeck 			info->func[page] |= PMBUS_HAVE_IOUT;
859d2ecfb7SGuenter Roeck 			if (pmbus_check_byte_register(client, 0,
869d2ecfb7SGuenter Roeck 						      PMBUS_STATUS_IOUT))
879d2ecfb7SGuenter Roeck 				info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
889d2ecfb7SGuenter Roeck 		}
899d2ecfb7SGuenter Roeck 		if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
909d2ecfb7SGuenter Roeck 			info->func[page] |= PMBUS_HAVE_POUT;
919d2ecfb7SGuenter Roeck 	}
929d2ecfb7SGuenter Roeck }
939d2ecfb7SGuenter Roeck 
949d2ecfb7SGuenter Roeck /*
959d2ecfb7SGuenter Roeck  * Identify chip parameters.
969d2ecfb7SGuenter Roeck  */
979d2ecfb7SGuenter Roeck static int pmbus_identify(struct i2c_client *client,
989d2ecfb7SGuenter Roeck 			  struct pmbus_driver_info *info)
999d2ecfb7SGuenter Roeck {
1001061d851SGuenter Roeck 	int ret = 0;
1011061d851SGuenter Roeck 
1029d2ecfb7SGuenter Roeck 	if (!info->pages) {
1039d2ecfb7SGuenter Roeck 		/*
1049d2ecfb7SGuenter Roeck 		 * Check if the PAGE command is supported. If it is,
1059d2ecfb7SGuenter Roeck 		 * keep setting the page number until it fails or until the
1069d2ecfb7SGuenter Roeck 		 * maximum number of pages has been reached. Assume that
1079d2ecfb7SGuenter Roeck 		 * this is the number of pages supported by the chip.
1089d2ecfb7SGuenter Roeck 		 */
1099d2ecfb7SGuenter Roeck 		if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
1109d2ecfb7SGuenter Roeck 			int page;
1119d2ecfb7SGuenter Roeck 
1129d2ecfb7SGuenter Roeck 			for (page = 1; page < PMBUS_PAGES; page++) {
1139d2ecfb7SGuenter Roeck 				if (pmbus_set_page(client, page) < 0)
1149d2ecfb7SGuenter Roeck 					break;
1159d2ecfb7SGuenter Roeck 			}
1169d2ecfb7SGuenter Roeck 			pmbus_set_page(client, 0);
1179d2ecfb7SGuenter Roeck 			info->pages = page;
1189d2ecfb7SGuenter Roeck 		} else {
1199d2ecfb7SGuenter Roeck 			info->pages = 1;
1209d2ecfb7SGuenter Roeck 		}
1219d2ecfb7SGuenter Roeck 	}
1229d2ecfb7SGuenter Roeck 
1231061d851SGuenter Roeck 	if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
1241061d851SGuenter Roeck 		int vout_mode;
1251061d851SGuenter Roeck 
1261061d851SGuenter Roeck 		vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
1271061d851SGuenter Roeck 		if (vout_mode >= 0 && vout_mode != 0xff) {
1281061d851SGuenter Roeck 			switch (vout_mode >> 5) {
1291061d851SGuenter Roeck 			case 0:
1301061d851SGuenter Roeck 				break;
1311061d851SGuenter Roeck 			case 1:
1321061d851SGuenter Roeck 				info->format[PSC_VOLTAGE_OUT] = vid;
133068c2270SGuenter Roeck 				info->vrm_version = vr11;
1341061d851SGuenter Roeck 				break;
1351061d851SGuenter Roeck 			case 2:
1361061d851SGuenter Roeck 				info->format[PSC_VOLTAGE_OUT] = direct;
1371061d851SGuenter Roeck 				break;
1381061d851SGuenter Roeck 			default:
1391061d851SGuenter Roeck 				ret = -ENODEV;
1401061d851SGuenter Roeck 				goto abort;
1411061d851SGuenter Roeck 			}
1421061d851SGuenter Roeck 		}
1431061d851SGuenter Roeck 	}
1441061d851SGuenter Roeck 
1459d2ecfb7SGuenter Roeck 	/*
1469d2ecfb7SGuenter Roeck 	 * We should check if the COEFFICIENTS register is supported.
1479d2ecfb7SGuenter Roeck 	 * If it is, and the chip is configured for direct mode, we can read
1489d2ecfb7SGuenter Roeck 	 * the coefficients from the chip, one set per group of sensor
1499d2ecfb7SGuenter Roeck 	 * registers.
1509d2ecfb7SGuenter Roeck 	 *
1519d2ecfb7SGuenter Roeck 	 * To do this, we will need access to a chip which actually supports the
1529d2ecfb7SGuenter Roeck 	 * COEFFICIENTS command, since the command is too complex to implement
1531061d851SGuenter Roeck 	 * without testing it. Until then, abort if a chip configured for direct
1541061d851SGuenter Roeck 	 * mode was detected.
1559d2ecfb7SGuenter Roeck 	 */
1561061d851SGuenter Roeck 	if (info->format[PSC_VOLTAGE_OUT] == direct) {
1571061d851SGuenter Roeck 		ret = -ENODEV;
1581061d851SGuenter Roeck 		goto abort;
1591061d851SGuenter Roeck 	}
1609d2ecfb7SGuenter Roeck 
1619d2ecfb7SGuenter Roeck 	/* Try to find sensor groups  */
1629d2ecfb7SGuenter Roeck 	pmbus_find_sensor_groups(client, info);
1631061d851SGuenter Roeck abort:
1641061d851SGuenter Roeck 	return ret;
1659d2ecfb7SGuenter Roeck }
1669d2ecfb7SGuenter Roeck 
1679d2ecfb7SGuenter Roeck static int pmbus_probe(struct i2c_client *client,
1689d2ecfb7SGuenter Roeck 		       const struct i2c_device_id *id)
1699d2ecfb7SGuenter Roeck {
1709d2ecfb7SGuenter Roeck 	struct pmbus_driver_info *info;
171cc00decfSVadim Pasternak 	struct pmbus_platform_data *pdata = NULL;
172cc00decfSVadim Pasternak 	struct device *dev = &client->dev;
1739d2ecfb7SGuenter Roeck 
174cc00decfSVadim Pasternak 	info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
1759d2ecfb7SGuenter Roeck 	if (!info)
1769d2ecfb7SGuenter Roeck 		return -ENOMEM;
1779d2ecfb7SGuenter Roeck 
178cc00decfSVadim Pasternak 	if (!strcmp(id->name, "dps460") || !strcmp(id->name, "dps800") ||
179cc00decfSVadim Pasternak 	    !strcmp(id->name, "sgd009")) {
180cc00decfSVadim Pasternak 		pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data),
181cc00decfSVadim Pasternak 				     GFP_KERNEL);
182cc00decfSVadim Pasternak 		if (!pdata)
183cc00decfSVadim Pasternak 			return -ENOMEM;
184cc00decfSVadim Pasternak 
185cc00decfSVadim Pasternak 		pdata->flags = PMBUS_SKIP_STATUS_CHECK;
186cc00decfSVadim Pasternak 	}
187cc00decfSVadim Pasternak 
1889d2ecfb7SGuenter Roeck 	info->pages = id->driver_data;
1899d2ecfb7SGuenter Roeck 	info->identify = pmbus_identify;
190cc00decfSVadim Pasternak 	dev->platform_data = pdata;
1919d2ecfb7SGuenter Roeck 
1928b313ca7SGuenter Roeck 	return pmbus_do_probe(client, id, info);
1939d2ecfb7SGuenter Roeck }
1949d2ecfb7SGuenter Roeck 
1959d2ecfb7SGuenter Roeck /*
1969d2ecfb7SGuenter Roeck  * Use driver_data to set the number of pages supported by the chip.
1979d2ecfb7SGuenter Roeck  */
1989d2ecfb7SGuenter Roeck static const struct i2c_device_id pmbus_id[] = {
199e0455e38SGuenter Roeck 	{"adp4000", 1},
2009d2ecfb7SGuenter Roeck 	{"bmr453", 1},
2019d2ecfb7SGuenter Roeck 	{"bmr454", 1},
202cc00decfSVadim Pasternak 	{"dps460", 1},
203cc00decfSVadim Pasternak 	{"dps800", 1},
20421633409SGuenter Roeck 	{"mdt040", 1},
205e0455e38SGuenter Roeck 	{"ncp4200", 1},
206e0455e38SGuenter Roeck 	{"ncp4208", 1},
207bc365a7fSGuenter Roeck 	{"pdt003", 1},
208bc365a7fSGuenter Roeck 	{"pdt006", 1},
209bc365a7fSGuenter Roeck 	{"pdt012", 1},
2109d2ecfb7SGuenter Roeck 	{"pmbus", 0},
211cc00decfSVadim Pasternak 	{"sgd009", 1},
212c5f35c9dSGuenter Roeck 	{"tps40400", 1},
213cfca3789SGuenter Roeck 	{"tps544b20", 1},
214cfca3789SGuenter Roeck 	{"tps544b25", 1},
215cfca3789SGuenter Roeck 	{"tps544c20", 1},
216cfca3789SGuenter Roeck 	{"tps544c25", 1},
217bc365a7fSGuenter Roeck 	{"udt020", 1},
2189d2ecfb7SGuenter Roeck 	{}
2199d2ecfb7SGuenter Roeck };
2209d2ecfb7SGuenter Roeck 
2219d2ecfb7SGuenter Roeck MODULE_DEVICE_TABLE(i2c, pmbus_id);
2229d2ecfb7SGuenter Roeck 
2239d2ecfb7SGuenter Roeck /* This is the driver that will be inserted */
2249d2ecfb7SGuenter Roeck static struct i2c_driver pmbus_driver = {
2259d2ecfb7SGuenter Roeck 	.driver = {
2269d2ecfb7SGuenter Roeck 		   .name = "pmbus",
2279d2ecfb7SGuenter Roeck 		   },
2289d2ecfb7SGuenter Roeck 	.probe = pmbus_probe,
229dd285ad7SGuenter Roeck 	.remove = pmbus_do_remove,
2309d2ecfb7SGuenter Roeck 	.id_table = pmbus_id,
2319d2ecfb7SGuenter Roeck };
2329d2ecfb7SGuenter Roeck 
233f0967eeaSAxel Lin module_i2c_driver(pmbus_driver);
2349d2ecfb7SGuenter Roeck 
2359d2ecfb7SGuenter Roeck MODULE_AUTHOR("Guenter Roeck");
2369d2ecfb7SGuenter Roeck MODULE_DESCRIPTION("Generic PMBus driver");
2379d2ecfb7SGuenter Roeck MODULE_LICENSE("GPL");
238