162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Hardware monitoring driver for LTC3815
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2015 Linear Technology
662306a36Sopenharmony_ci * Copyright (c) 2015 Guenter Roeck
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/err.h>
1062306a36Sopenharmony_ci#include <linux/i2c.h>
1162306a36Sopenharmony_ci#include <linux/init.h>
1262306a36Sopenharmony_ci#include <linux/jiffies.h>
1362306a36Sopenharmony_ci#include <linux/kernel.h>
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include "pmbus.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define LTC3815_MFR_IOUT_PEAK	0xd7
1862306a36Sopenharmony_ci#define LTC3815_MFR_VOUT_PEAK	0xdd
1962306a36Sopenharmony_ci#define LTC3815_MFR_VIN_PEAK	0xde
2062306a36Sopenharmony_ci#define LTC3815_MFR_TEMP_PEAK	0xdf
2162306a36Sopenharmony_ci#define LTC3815_MFR_IIN_PEAK	0xe1
2262306a36Sopenharmony_ci#define LTC3815_MFR_SPECIAL_ID	0xe7
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define LTC3815_ID		0x8000
2562306a36Sopenharmony_ci#define LTC3815_ID_MASK		0xff00
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic int ltc3815_read_byte_data(struct i2c_client *client, int page, int reg)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	int ret;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	switch (reg) {
3262306a36Sopenharmony_ci	case PMBUS_VOUT_MODE:
3362306a36Sopenharmony_ci		/*
3462306a36Sopenharmony_ci		 * The chip returns 0x3e, suggesting VID mode with manufacturer
3562306a36Sopenharmony_ci		 * specific VID codes. Since the output voltage is reported
3662306a36Sopenharmony_ci		 * with a LSB of 0.5mV, override and report direct mode with
3762306a36Sopenharmony_ci		 * appropriate coefficients.
3862306a36Sopenharmony_ci		 */
3962306a36Sopenharmony_ci		ret = 0x40;
4062306a36Sopenharmony_ci		break;
4162306a36Sopenharmony_ci	default:
4262306a36Sopenharmony_ci		ret = -ENODATA;
4362306a36Sopenharmony_ci		break;
4462306a36Sopenharmony_ci	}
4562306a36Sopenharmony_ci	return ret;
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic int ltc3815_write_byte(struct i2c_client *client, int page, u8 reg)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	int ret;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	switch (reg) {
5362306a36Sopenharmony_ci	case PMBUS_CLEAR_FAULTS:
5462306a36Sopenharmony_ci		/*
5562306a36Sopenharmony_ci		 * LTC3815 does not support the CLEAR_FAULTS command.
5662306a36Sopenharmony_ci		 * Emulate it by clearing the status register.
5762306a36Sopenharmony_ci		 */
5862306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, 0, 0xff, PMBUS_STATUS_WORD);
5962306a36Sopenharmony_ci		if (ret > 0) {
6062306a36Sopenharmony_ci			pmbus_write_word_data(client, 0, PMBUS_STATUS_WORD,
6162306a36Sopenharmony_ci					      ret);
6262306a36Sopenharmony_ci			ret = 0;
6362306a36Sopenharmony_ci		}
6462306a36Sopenharmony_ci		break;
6562306a36Sopenharmony_ci	default:
6662306a36Sopenharmony_ci		ret = -ENODATA;
6762306a36Sopenharmony_ci		break;
6862306a36Sopenharmony_ci	}
6962306a36Sopenharmony_ci	return ret;
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistatic int ltc3815_read_word_data(struct i2c_client *client, int page,
7362306a36Sopenharmony_ci				  int phase, int reg)
7462306a36Sopenharmony_ci{
7562306a36Sopenharmony_ci	int ret;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	switch (reg) {
7862306a36Sopenharmony_ci	case PMBUS_VIRT_READ_VIN_MAX:
7962306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, page, phase,
8062306a36Sopenharmony_ci					   LTC3815_MFR_VIN_PEAK);
8162306a36Sopenharmony_ci		break;
8262306a36Sopenharmony_ci	case PMBUS_VIRT_READ_VOUT_MAX:
8362306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, page, phase,
8462306a36Sopenharmony_ci					   LTC3815_MFR_VOUT_PEAK);
8562306a36Sopenharmony_ci		break;
8662306a36Sopenharmony_ci	case PMBUS_VIRT_READ_TEMP_MAX:
8762306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, page, phase,
8862306a36Sopenharmony_ci					   LTC3815_MFR_TEMP_PEAK);
8962306a36Sopenharmony_ci		break;
9062306a36Sopenharmony_ci	case PMBUS_VIRT_READ_IOUT_MAX:
9162306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, page, phase,
9262306a36Sopenharmony_ci					   LTC3815_MFR_IOUT_PEAK);
9362306a36Sopenharmony_ci		break;
9462306a36Sopenharmony_ci	case PMBUS_VIRT_READ_IIN_MAX:
9562306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, page, phase,
9662306a36Sopenharmony_ci					   LTC3815_MFR_IIN_PEAK);
9762306a36Sopenharmony_ci		break;
9862306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_VOUT_HISTORY:
9962306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_VIN_HISTORY:
10062306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_TEMP_HISTORY:
10162306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_IOUT_HISTORY:
10262306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_IIN_HISTORY:
10362306a36Sopenharmony_ci		ret = 0;
10462306a36Sopenharmony_ci		break;
10562306a36Sopenharmony_ci	default:
10662306a36Sopenharmony_ci		ret = -ENODATA;
10762306a36Sopenharmony_ci		break;
10862306a36Sopenharmony_ci	}
10962306a36Sopenharmony_ci	return ret;
11062306a36Sopenharmony_ci}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_cistatic int ltc3815_write_word_data(struct i2c_client *client, int page,
11362306a36Sopenharmony_ci				   int reg, u16 word)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	int ret;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	switch (reg) {
11862306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_IIN_HISTORY:
11962306a36Sopenharmony_ci		ret = pmbus_write_word_data(client, page,
12062306a36Sopenharmony_ci					    LTC3815_MFR_IIN_PEAK, 0);
12162306a36Sopenharmony_ci		break;
12262306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_IOUT_HISTORY:
12362306a36Sopenharmony_ci		ret = pmbus_write_word_data(client, page,
12462306a36Sopenharmony_ci					    LTC3815_MFR_IOUT_PEAK, 0);
12562306a36Sopenharmony_ci		break;
12662306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_VOUT_HISTORY:
12762306a36Sopenharmony_ci		ret = pmbus_write_word_data(client, page,
12862306a36Sopenharmony_ci					    LTC3815_MFR_VOUT_PEAK, 0);
12962306a36Sopenharmony_ci		break;
13062306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_VIN_HISTORY:
13162306a36Sopenharmony_ci		ret = pmbus_write_word_data(client, page,
13262306a36Sopenharmony_ci					    LTC3815_MFR_VIN_PEAK, 0);
13362306a36Sopenharmony_ci		break;
13462306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_TEMP_HISTORY:
13562306a36Sopenharmony_ci		ret = pmbus_write_word_data(client, page,
13662306a36Sopenharmony_ci					    LTC3815_MFR_TEMP_PEAK, 0);
13762306a36Sopenharmony_ci		break;
13862306a36Sopenharmony_ci	default:
13962306a36Sopenharmony_ci		ret = -ENODATA;
14062306a36Sopenharmony_ci		break;
14162306a36Sopenharmony_ci	}
14262306a36Sopenharmony_ci	return ret;
14362306a36Sopenharmony_ci}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_cistatic const struct i2c_device_id ltc3815_id[] = {
14662306a36Sopenharmony_ci	{"ltc3815", 0},
14762306a36Sopenharmony_ci	{ }
14862306a36Sopenharmony_ci};
14962306a36Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ltc3815_id);
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_cistatic struct pmbus_driver_info ltc3815_info = {
15262306a36Sopenharmony_ci	.pages = 1,
15362306a36Sopenharmony_ci	.format[PSC_VOLTAGE_IN] = direct,
15462306a36Sopenharmony_ci	.format[PSC_VOLTAGE_OUT] = direct,
15562306a36Sopenharmony_ci	.format[PSC_CURRENT_IN] = direct,
15662306a36Sopenharmony_ci	.format[PSC_CURRENT_OUT] = direct,
15762306a36Sopenharmony_ci	.format[PSC_TEMPERATURE] = direct,
15862306a36Sopenharmony_ci	.m[PSC_VOLTAGE_IN] = 250,
15962306a36Sopenharmony_ci	.b[PSC_VOLTAGE_IN] = 0,
16062306a36Sopenharmony_ci	.R[PSC_VOLTAGE_IN] = 0,
16162306a36Sopenharmony_ci	.m[PSC_VOLTAGE_OUT] = 2,
16262306a36Sopenharmony_ci	.b[PSC_VOLTAGE_OUT] = 0,
16362306a36Sopenharmony_ci	.R[PSC_VOLTAGE_OUT] = 3,
16462306a36Sopenharmony_ci	.m[PSC_CURRENT_IN] = 1,
16562306a36Sopenharmony_ci	.b[PSC_CURRENT_IN] = 0,
16662306a36Sopenharmony_ci	.R[PSC_CURRENT_IN] = 2,
16762306a36Sopenharmony_ci	.m[PSC_CURRENT_OUT] = 1,
16862306a36Sopenharmony_ci	.b[PSC_CURRENT_OUT] = 0,
16962306a36Sopenharmony_ci	.R[PSC_CURRENT_OUT] = 2,
17062306a36Sopenharmony_ci	.m[PSC_TEMPERATURE] = 1,
17162306a36Sopenharmony_ci	.b[PSC_TEMPERATURE] = 0,
17262306a36Sopenharmony_ci	.R[PSC_TEMPERATURE] = 0,
17362306a36Sopenharmony_ci	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN | PMBUS_HAVE_VOUT |
17462306a36Sopenharmony_ci		PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP,
17562306a36Sopenharmony_ci	.read_byte_data = ltc3815_read_byte_data,
17662306a36Sopenharmony_ci	.read_word_data = ltc3815_read_word_data,
17762306a36Sopenharmony_ci	.write_byte = ltc3815_write_byte,
17862306a36Sopenharmony_ci	.write_word_data = ltc3815_write_word_data,
17962306a36Sopenharmony_ci};
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic int ltc3815_probe(struct i2c_client *client)
18262306a36Sopenharmony_ci{
18362306a36Sopenharmony_ci	int chip_id;
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci	if (!i2c_check_functionality(client->adapter,
18662306a36Sopenharmony_ci				     I2C_FUNC_SMBUS_READ_WORD_DATA))
18762306a36Sopenharmony_ci		return -ENODEV;
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	chip_id = i2c_smbus_read_word_data(client, LTC3815_MFR_SPECIAL_ID);
19062306a36Sopenharmony_ci	if (chip_id < 0)
19162306a36Sopenharmony_ci		return chip_id;
19262306a36Sopenharmony_ci	if ((chip_id & LTC3815_ID_MASK) != LTC3815_ID)
19362306a36Sopenharmony_ci		return -ENODEV;
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	return pmbus_do_probe(client, &ltc3815_info);
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_cistatic struct i2c_driver ltc3815_driver = {
19962306a36Sopenharmony_ci	.driver = {
20062306a36Sopenharmony_ci		   .name = "ltc3815",
20162306a36Sopenharmony_ci		   },
20262306a36Sopenharmony_ci	.probe = ltc3815_probe,
20362306a36Sopenharmony_ci	.id_table = ltc3815_id,
20462306a36Sopenharmony_ci};
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cimodule_i2c_driver(ltc3815_driver);
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ciMODULE_AUTHOR("Guenter Roeck");
20962306a36Sopenharmony_ciMODULE_DESCRIPTION("PMBus driver for LTC3815");
21062306a36Sopenharmony_ciMODULE_LICENSE("GPL");
21162306a36Sopenharmony_ciMODULE_IMPORT_NS(PMBUS);
212