18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Hardware monitoring driver for Infineon PXE1610
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2019 Facebook Inc
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/err.h>
108c2ecf20Sopenharmony_ci#include <linux/i2c.h>
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include "pmbus.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define PXE1610_NUM_PAGES 3
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* Identify chip parameters. */
198c2ecf20Sopenharmony_cistatic int pxe1610_identify(struct i2c_client *client,
208c2ecf20Sopenharmony_ci			     struct pmbus_driver_info *info)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	int i;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	for (i = 0; i < PXE1610_NUM_PAGES; i++) {
258c2ecf20Sopenharmony_ci		if (pmbus_check_byte_register(client, i, PMBUS_VOUT_MODE)) {
268c2ecf20Sopenharmony_ci			u8 vout_mode;
278c2ecf20Sopenharmony_ci			int ret;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci			/* Read the register with VOUT scaling value.*/
308c2ecf20Sopenharmony_ci			ret = pmbus_read_byte_data(client, i, PMBUS_VOUT_MODE);
318c2ecf20Sopenharmony_ci			if (ret < 0)
328c2ecf20Sopenharmony_ci				return ret;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci			vout_mode = ret & GENMASK(4, 0);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci			switch (vout_mode) {
378c2ecf20Sopenharmony_ci			case 1:
388c2ecf20Sopenharmony_ci				info->vrm_version[i] = vr12;
398c2ecf20Sopenharmony_ci				break;
408c2ecf20Sopenharmony_ci			case 2:
418c2ecf20Sopenharmony_ci				info->vrm_version[i] = vr13;
428c2ecf20Sopenharmony_ci				break;
438c2ecf20Sopenharmony_ci			default:
448c2ecf20Sopenharmony_ci				/*
458c2ecf20Sopenharmony_ci				 * If prior pages are available limit operation
468c2ecf20Sopenharmony_ci				 * to them
478c2ecf20Sopenharmony_ci				 */
488c2ecf20Sopenharmony_ci				if (i != 0) {
498c2ecf20Sopenharmony_ci					info->pages = i;
508c2ecf20Sopenharmony_ci					return 0;
518c2ecf20Sopenharmony_ci				}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci				return -ENODEV;
548c2ecf20Sopenharmony_ci			}
558c2ecf20Sopenharmony_ci		}
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	return 0;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic struct pmbus_driver_info pxe1610_info = {
628c2ecf20Sopenharmony_ci	.pages = PXE1610_NUM_PAGES,
638c2ecf20Sopenharmony_ci	.format[PSC_VOLTAGE_IN] = linear,
648c2ecf20Sopenharmony_ci	.format[PSC_VOLTAGE_OUT] = vid,
658c2ecf20Sopenharmony_ci	.format[PSC_CURRENT_IN] = linear,
668c2ecf20Sopenharmony_ci	.format[PSC_CURRENT_OUT] = linear,
678c2ecf20Sopenharmony_ci	.format[PSC_TEMPERATURE] = linear,
688c2ecf20Sopenharmony_ci	.format[PSC_POWER] = linear,
698c2ecf20Sopenharmony_ci	.func[0] = PMBUS_HAVE_VIN
708c2ecf20Sopenharmony_ci		| PMBUS_HAVE_VOUT | PMBUS_HAVE_IIN
718c2ecf20Sopenharmony_ci		| PMBUS_HAVE_IOUT | PMBUS_HAVE_PIN
728c2ecf20Sopenharmony_ci		| PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
738c2ecf20Sopenharmony_ci		| PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
748c2ecf20Sopenharmony_ci		| PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP,
758c2ecf20Sopenharmony_ci	.func[1] = PMBUS_HAVE_VIN
768c2ecf20Sopenharmony_ci		| PMBUS_HAVE_VOUT | PMBUS_HAVE_IIN
778c2ecf20Sopenharmony_ci		| PMBUS_HAVE_IOUT | PMBUS_HAVE_PIN
788c2ecf20Sopenharmony_ci		| PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
798c2ecf20Sopenharmony_ci		| PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
808c2ecf20Sopenharmony_ci		| PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP,
818c2ecf20Sopenharmony_ci	.func[2] = PMBUS_HAVE_VIN
828c2ecf20Sopenharmony_ci		| PMBUS_HAVE_VOUT | PMBUS_HAVE_IIN
838c2ecf20Sopenharmony_ci		| PMBUS_HAVE_IOUT | PMBUS_HAVE_PIN
848c2ecf20Sopenharmony_ci		| PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
858c2ecf20Sopenharmony_ci		| PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
868c2ecf20Sopenharmony_ci		| PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP,
878c2ecf20Sopenharmony_ci	.identify = pxe1610_identify,
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic int pxe1610_probe(struct i2c_client *client)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct pmbus_driver_info *info;
938c2ecf20Sopenharmony_ci	u8 buf[I2C_SMBUS_BLOCK_MAX];
948c2ecf20Sopenharmony_ci	int ret;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(
978c2ecf20Sopenharmony_ci			client->adapter,
988c2ecf20Sopenharmony_ci			I2C_FUNC_SMBUS_READ_BYTE_DATA
998c2ecf20Sopenharmony_ci			| I2C_FUNC_SMBUS_READ_WORD_DATA
1008c2ecf20Sopenharmony_ci			| I2C_FUNC_SMBUS_READ_BLOCK_DATA))
1018c2ecf20Sopenharmony_ci		return -ENODEV;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/*
1048c2ecf20Sopenharmony_ci	 * By default this device doesn't boot to page 0, so set page 0
1058c2ecf20Sopenharmony_ci	 * to access all pmbus registers.
1068c2ecf20Sopenharmony_ci	 */
1078c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/* Read Manufacturer id */
1108c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
1118c2ecf20Sopenharmony_ci	if (ret < 0) {
1128c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to read PMBUS_MFR_ID\n");
1138c2ecf20Sopenharmony_ci		return ret;
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci	if (ret != 2 || strncmp(buf, "XP", 2)) {
1168c2ecf20Sopenharmony_ci		dev_err(&client->dev, "MFR_ID unrecognized\n");
1178c2ecf20Sopenharmony_ci		return -ENODEV;
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	info = devm_kmemdup(&client->dev, &pxe1610_info,
1218c2ecf20Sopenharmony_ci			    sizeof(struct pmbus_driver_info),
1228c2ecf20Sopenharmony_ci			    GFP_KERNEL);
1238c2ecf20Sopenharmony_ci	if (!info)
1248c2ecf20Sopenharmony_ci		return -ENOMEM;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	return pmbus_do_probe(client, info);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic const struct i2c_device_id pxe1610_id[] = {
1308c2ecf20Sopenharmony_ci	{"pxe1610", 0},
1318c2ecf20Sopenharmony_ci	{"pxe1110", 0},
1328c2ecf20Sopenharmony_ci	{"pxm1310", 0},
1338c2ecf20Sopenharmony_ci	{}
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, pxe1610_id);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic struct i2c_driver pxe1610_driver = {
1398c2ecf20Sopenharmony_ci	.driver = {
1408c2ecf20Sopenharmony_ci			.name = "pxe1610",
1418c2ecf20Sopenharmony_ci			},
1428c2ecf20Sopenharmony_ci	.probe_new = pxe1610_probe,
1438c2ecf20Sopenharmony_ci	.remove = pmbus_do_remove,
1448c2ecf20Sopenharmony_ci	.id_table = pxe1610_id,
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cimodule_i2c_driver(pxe1610_driver);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vijay Khemka <vijaykhemka@fb.com>");
1508c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PMBus driver for Infineon PXE1610, PXE1110 and PXM1310");
1518c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
152