162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Hardware monitoring driver for Analog Devices LT7182S
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2022 Guenter Roeck
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/bits.h>
1062306a36Sopenharmony_ci#include <linux/err.h>
1162306a36Sopenharmony_ci#include <linux/i2c.h>
1262306a36Sopenharmony_ci#include <linux/init.h>
1362306a36Sopenharmony_ci#include <linux/kernel.h>
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <linux/of.h>
1662306a36Sopenharmony_ci#include "pmbus.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define LT7182S_NUM_PAGES	2
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define MFR_READ_EXTVCC		0xcd
2162306a36Sopenharmony_ci#define MFR_READ_ITH		0xce
2262306a36Sopenharmony_ci#define MFR_CONFIG_ALL_LT7182S	0xd1
2362306a36Sopenharmony_ci#define MFR_IOUT_PEAK		0xd7
2462306a36Sopenharmony_ci#define MFR_ADC_CONTROL_LT7182S 0xd8
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define MFR_DEBUG_TELEMETRY	BIT(0)
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define MFR_VOUT_PEAK		0xdd
2962306a36Sopenharmony_ci#define MFR_VIN_PEAK		0xde
3062306a36Sopenharmony_ci#define MFR_TEMPERATURE_1_PEAK	0xdf
3162306a36Sopenharmony_ci#define MFR_CLEAR_PEAKS		0xe3
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define MFR_CONFIG_IEEE		BIT(8)
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistatic int lt7182s_read_word_data(struct i2c_client *client, int page, int phase, int reg)
3662306a36Sopenharmony_ci{
3762306a36Sopenharmony_ci	int ret;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	switch (reg) {
4062306a36Sopenharmony_ci	case PMBUS_VIRT_READ_VMON:
4162306a36Sopenharmony_ci		if (page == 0 || page == 1)
4262306a36Sopenharmony_ci			ret = pmbus_read_word_data(client, page, phase, MFR_READ_ITH);
4362306a36Sopenharmony_ci		else
4462306a36Sopenharmony_ci			ret = pmbus_read_word_data(client, 0, phase, MFR_READ_EXTVCC);
4562306a36Sopenharmony_ci		break;
4662306a36Sopenharmony_ci	case PMBUS_VIRT_READ_IOUT_MAX:
4762306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, page, phase, MFR_IOUT_PEAK);
4862306a36Sopenharmony_ci		break;
4962306a36Sopenharmony_ci	case PMBUS_VIRT_READ_VOUT_MAX:
5062306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, page, phase, MFR_VOUT_PEAK);
5162306a36Sopenharmony_ci		break;
5262306a36Sopenharmony_ci	case PMBUS_VIRT_READ_VIN_MAX:
5362306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, page, phase, MFR_VIN_PEAK);
5462306a36Sopenharmony_ci		break;
5562306a36Sopenharmony_ci	case PMBUS_VIRT_READ_TEMP_MAX:
5662306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, page, phase, MFR_TEMPERATURE_1_PEAK);
5762306a36Sopenharmony_ci		break;
5862306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_VIN_HISTORY:
5962306a36Sopenharmony_ci		ret = (page == 0) ? 0 : -ENODATA;
6062306a36Sopenharmony_ci		break;
6162306a36Sopenharmony_ci	default:
6262306a36Sopenharmony_ci		ret = -ENODATA;
6362306a36Sopenharmony_ci		break;
6462306a36Sopenharmony_ci	}
6562306a36Sopenharmony_ci	return ret;
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_cistatic int lt7182s_write_word_data(struct i2c_client *client, int page, int reg, u16 word)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	int ret;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	switch (reg) {
7362306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_VIN_HISTORY:
7462306a36Sopenharmony_ci		ret = pmbus_write_byte(client, 0, MFR_CLEAR_PEAKS);
7562306a36Sopenharmony_ci		break;
7662306a36Sopenharmony_ci	default:
7762306a36Sopenharmony_ci		ret = -ENODATA;
7862306a36Sopenharmony_ci		break;
7962306a36Sopenharmony_ci	}
8062306a36Sopenharmony_ci	return ret;
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic struct pmbus_driver_info lt7182s_info = {
8462306a36Sopenharmony_ci	.pages = LT7182S_NUM_PAGES,
8562306a36Sopenharmony_ci	.format[PSC_VOLTAGE_IN] = linear,
8662306a36Sopenharmony_ci	.format[PSC_VOLTAGE_OUT] = linear,
8762306a36Sopenharmony_ci	.format[PSC_CURRENT_IN] = linear,
8862306a36Sopenharmony_ci	.format[PSC_CURRENT_OUT] = linear,
8962306a36Sopenharmony_ci	.format[PSC_TEMPERATURE] = linear,
9062306a36Sopenharmony_ci	.format[PSC_POWER] = linear,
9162306a36Sopenharmony_ci	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT |
9262306a36Sopenharmony_ci	  PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_POUT |
9362306a36Sopenharmony_ci	  PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
9462306a36Sopenharmony_ci	  PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP,
9562306a36Sopenharmony_ci	.func[1] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT |
9662306a36Sopenharmony_ci	  PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_POUT |
9762306a36Sopenharmony_ci	  PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
9862306a36Sopenharmony_ci	  PMBUS_HAVE_STATUS_INPUT,
9962306a36Sopenharmony_ci	.read_word_data = lt7182s_read_word_data,
10062306a36Sopenharmony_ci	.write_word_data = lt7182s_write_word_data,
10162306a36Sopenharmony_ci};
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cistatic int lt7182s_probe(struct i2c_client *client)
10462306a36Sopenharmony_ci{
10562306a36Sopenharmony_ci	struct device *dev = &client->dev;
10662306a36Sopenharmony_ci	struct pmbus_driver_info *info;
10762306a36Sopenharmony_ci	u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
10862306a36Sopenharmony_ci	int ret;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	if (!i2c_check_functionality(client->adapter,
11162306a36Sopenharmony_ci				     I2C_FUNC_SMBUS_READ_BYTE_DATA |
11262306a36Sopenharmony_ci				     I2C_FUNC_SMBUS_READ_WORD_DATA |
11362306a36Sopenharmony_ci				     I2C_FUNC_SMBUS_READ_BLOCK_DATA))
11462306a36Sopenharmony_ci		return -ENODEV;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
11762306a36Sopenharmony_ci	if (ret < 0) {
11862306a36Sopenharmony_ci		dev_err(dev, "Failed to read PMBUS_MFR_ID\n");
11962306a36Sopenharmony_ci		return ret;
12062306a36Sopenharmony_ci	}
12162306a36Sopenharmony_ci	if (ret != 3 || strncmp(buf, "ADI", 3)) {
12262306a36Sopenharmony_ci		buf[ret] = '\0';
12362306a36Sopenharmony_ci		dev_err(dev, "Manufacturer '%s' not supported\n", buf);
12462306a36Sopenharmony_ci		return -ENODEV;
12562306a36Sopenharmony_ci	}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
12862306a36Sopenharmony_ci	if (ret < 0) {
12962306a36Sopenharmony_ci		dev_err(dev, "Failed to read PMBUS_MFR_MODEL\n");
13062306a36Sopenharmony_ci		return ret;
13162306a36Sopenharmony_ci	}
13262306a36Sopenharmony_ci	if (ret != 7 || strncmp(buf, "LT7182S", 7)) {
13362306a36Sopenharmony_ci		buf[ret] = '\0';
13462306a36Sopenharmony_ci		dev_err(dev, "Model '%s' not supported\n", buf);
13562306a36Sopenharmony_ci		return -ENODEV;
13662306a36Sopenharmony_ci	}
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	info = devm_kmemdup(dev, &lt7182s_info,
13962306a36Sopenharmony_ci			    sizeof(struct pmbus_driver_info), GFP_KERNEL);
14062306a36Sopenharmony_ci	if (!info)
14162306a36Sopenharmony_ci		return -ENOMEM;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	/* Set data format to IEEE754 if configured */
14462306a36Sopenharmony_ci	ret = i2c_smbus_read_word_data(client, MFR_CONFIG_ALL_LT7182S);
14562306a36Sopenharmony_ci	if (ret < 0)
14662306a36Sopenharmony_ci		return ret;
14762306a36Sopenharmony_ci	if (ret & MFR_CONFIG_IEEE) {
14862306a36Sopenharmony_ci		info->format[PSC_VOLTAGE_IN] = ieee754;
14962306a36Sopenharmony_ci		info->format[PSC_VOLTAGE_OUT] = ieee754;
15062306a36Sopenharmony_ci		info->format[PSC_CURRENT_IN] = ieee754;
15162306a36Sopenharmony_ci		info->format[PSC_CURRENT_OUT] = ieee754;
15262306a36Sopenharmony_ci		info->format[PSC_TEMPERATURE] = ieee754;
15362306a36Sopenharmony_ci		info->format[PSC_POWER] = ieee754;
15462306a36Sopenharmony_ci	}
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	/* Enable VMON output if configured */
15762306a36Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, MFR_ADC_CONTROL_LT7182S);
15862306a36Sopenharmony_ci	if (ret < 0)
15962306a36Sopenharmony_ci		return ret;
16062306a36Sopenharmony_ci	if (ret & MFR_DEBUG_TELEMETRY) {
16162306a36Sopenharmony_ci		info->pages = 3;
16262306a36Sopenharmony_ci		info->func[0] |= PMBUS_HAVE_VMON;
16362306a36Sopenharmony_ci		info->func[1] |= PMBUS_HAVE_VMON;
16462306a36Sopenharmony_ci		info->func[2] = PMBUS_HAVE_VMON;
16562306a36Sopenharmony_ci	}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	return pmbus_do_probe(client, info);
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistatic const struct i2c_device_id lt7182s_id[] = {
17162306a36Sopenharmony_ci	{ "lt7182s", 0 },
17262306a36Sopenharmony_ci	{}
17362306a36Sopenharmony_ci};
17462306a36Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, lt7182s_id);
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_cistatic const struct of_device_id __maybe_unused lt7182s_of_match[] = {
17762306a36Sopenharmony_ci	{ .compatible = "adi,lt7182s" },
17862306a36Sopenharmony_ci	{}
17962306a36Sopenharmony_ci};
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic struct i2c_driver lt7182s_driver = {
18262306a36Sopenharmony_ci	.driver = {
18362306a36Sopenharmony_ci		.name = "lt7182s",
18462306a36Sopenharmony_ci		.of_match_table = of_match_ptr(lt7182s_of_match),
18562306a36Sopenharmony_ci	},
18662306a36Sopenharmony_ci	.probe = lt7182s_probe,
18762306a36Sopenharmony_ci	.id_table = lt7182s_id,
18862306a36Sopenharmony_ci};
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_cimodule_i2c_driver(lt7182s_driver);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciMODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
19362306a36Sopenharmony_ciMODULE_DESCRIPTION("PMBus driver for Analog Devices LT7182S");
19462306a36Sopenharmony_ciMODULE_LICENSE("GPL");
19562306a36Sopenharmony_ciMODULE_IMPORT_NS(PMBUS);
196