162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Hardware monitoring driver for Maxim MAX8688
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2011 Ericsson AB.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/bitops.h>
962306a36Sopenharmony_ci#include <linux/kernel.h>
1062306a36Sopenharmony_ci#include <linux/module.h>
1162306a36Sopenharmony_ci#include <linux/init.h>
1262306a36Sopenharmony_ci#include <linux/err.h>
1362306a36Sopenharmony_ci#include <linux/i2c.h>
1462306a36Sopenharmony_ci#include "pmbus.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#define MAX8688_MFR_VOUT_PEAK		0xd4
1762306a36Sopenharmony_ci#define MAX8688_MFR_IOUT_PEAK		0xd5
1862306a36Sopenharmony_ci#define MAX8688_MFR_TEMPERATURE_PEAK	0xd6
1962306a36Sopenharmony_ci#define MAX8688_MFG_STATUS		0xd8
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#define MAX8688_STATUS_OC_FAULT		BIT(4)
2262306a36Sopenharmony_ci#define MAX8688_STATUS_OV_FAULT		BIT(5)
2362306a36Sopenharmony_ci#define MAX8688_STATUS_OV_WARNING	BIT(8)
2462306a36Sopenharmony_ci#define MAX8688_STATUS_UV_FAULT		BIT(9)
2562306a36Sopenharmony_ci#define MAX8688_STATUS_UV_WARNING	BIT(10)
2662306a36Sopenharmony_ci#define MAX8688_STATUS_UC_FAULT		BIT(11)
2762306a36Sopenharmony_ci#define MAX8688_STATUS_OC_WARNING	BIT(12)
2862306a36Sopenharmony_ci#define MAX8688_STATUS_OT_FAULT		BIT(13)
2962306a36Sopenharmony_ci#define MAX8688_STATUS_OT_WARNING	BIT(14)
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistatic int max8688_read_word_data(struct i2c_client *client, int page,
3262306a36Sopenharmony_ci				  int phase, int reg)
3362306a36Sopenharmony_ci{
3462306a36Sopenharmony_ci	int ret;
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci	if (page > 0)
3762306a36Sopenharmony_ci		return -ENXIO;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	switch (reg) {
4062306a36Sopenharmony_ci	case PMBUS_VIRT_READ_VOUT_MAX:
4162306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, 0, 0xff,
4262306a36Sopenharmony_ci					   MAX8688_MFR_VOUT_PEAK);
4362306a36Sopenharmony_ci		break;
4462306a36Sopenharmony_ci	case PMBUS_VIRT_READ_IOUT_MAX:
4562306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, 0, 0xff,
4662306a36Sopenharmony_ci					   MAX8688_MFR_IOUT_PEAK);
4762306a36Sopenharmony_ci		break;
4862306a36Sopenharmony_ci	case PMBUS_VIRT_READ_TEMP_MAX:
4962306a36Sopenharmony_ci		ret = pmbus_read_word_data(client, 0, 0xff,
5062306a36Sopenharmony_ci					   MAX8688_MFR_TEMPERATURE_PEAK);
5162306a36Sopenharmony_ci		break;
5262306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_VOUT_HISTORY:
5362306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_IOUT_HISTORY:
5462306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_TEMP_HISTORY:
5562306a36Sopenharmony_ci		ret = 0;
5662306a36Sopenharmony_ci		break;
5762306a36Sopenharmony_ci	default:
5862306a36Sopenharmony_ci		ret = -ENODATA;
5962306a36Sopenharmony_ci		break;
6062306a36Sopenharmony_ci	}
6162306a36Sopenharmony_ci	return ret;
6262306a36Sopenharmony_ci}
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cistatic int max8688_write_word_data(struct i2c_client *client, int page, int reg,
6562306a36Sopenharmony_ci				   u16 word)
6662306a36Sopenharmony_ci{
6762306a36Sopenharmony_ci	int ret;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	switch (reg) {
7062306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_VOUT_HISTORY:
7162306a36Sopenharmony_ci		ret = pmbus_write_word_data(client, 0, MAX8688_MFR_VOUT_PEAK,
7262306a36Sopenharmony_ci					    0);
7362306a36Sopenharmony_ci		break;
7462306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_IOUT_HISTORY:
7562306a36Sopenharmony_ci		ret = pmbus_write_word_data(client, 0, MAX8688_MFR_IOUT_PEAK,
7662306a36Sopenharmony_ci					    0);
7762306a36Sopenharmony_ci		break;
7862306a36Sopenharmony_ci	case PMBUS_VIRT_RESET_TEMP_HISTORY:
7962306a36Sopenharmony_ci		ret = pmbus_write_word_data(client, 0,
8062306a36Sopenharmony_ci					    MAX8688_MFR_TEMPERATURE_PEAK,
8162306a36Sopenharmony_ci					    0xffff);
8262306a36Sopenharmony_ci		break;
8362306a36Sopenharmony_ci	default:
8462306a36Sopenharmony_ci		ret = -ENODATA;
8562306a36Sopenharmony_ci		break;
8662306a36Sopenharmony_ci	}
8762306a36Sopenharmony_ci	return ret;
8862306a36Sopenharmony_ci}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cistatic int max8688_read_byte_data(struct i2c_client *client, int page, int reg)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	int ret = 0;
9362306a36Sopenharmony_ci	int mfg_status;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	if (page > 0)
9662306a36Sopenharmony_ci		return -ENXIO;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	switch (reg) {
9962306a36Sopenharmony_ci	case PMBUS_STATUS_VOUT:
10062306a36Sopenharmony_ci		mfg_status = pmbus_read_word_data(client, 0, 0xff,
10162306a36Sopenharmony_ci						  MAX8688_MFG_STATUS);
10262306a36Sopenharmony_ci		if (mfg_status < 0)
10362306a36Sopenharmony_ci			return mfg_status;
10462306a36Sopenharmony_ci		if (mfg_status & MAX8688_STATUS_UV_WARNING)
10562306a36Sopenharmony_ci			ret |= PB_VOLTAGE_UV_WARNING;
10662306a36Sopenharmony_ci		if (mfg_status & MAX8688_STATUS_UV_FAULT)
10762306a36Sopenharmony_ci			ret |= PB_VOLTAGE_UV_FAULT;
10862306a36Sopenharmony_ci		if (mfg_status & MAX8688_STATUS_OV_WARNING)
10962306a36Sopenharmony_ci			ret |= PB_VOLTAGE_OV_WARNING;
11062306a36Sopenharmony_ci		if (mfg_status & MAX8688_STATUS_OV_FAULT)
11162306a36Sopenharmony_ci			ret |= PB_VOLTAGE_OV_FAULT;
11262306a36Sopenharmony_ci		break;
11362306a36Sopenharmony_ci	case PMBUS_STATUS_IOUT:
11462306a36Sopenharmony_ci		mfg_status = pmbus_read_word_data(client, 0, 0xff,
11562306a36Sopenharmony_ci						  MAX8688_MFG_STATUS);
11662306a36Sopenharmony_ci		if (mfg_status < 0)
11762306a36Sopenharmony_ci			return mfg_status;
11862306a36Sopenharmony_ci		if (mfg_status & MAX8688_STATUS_UC_FAULT)
11962306a36Sopenharmony_ci			ret |= PB_IOUT_UC_FAULT;
12062306a36Sopenharmony_ci		if (mfg_status & MAX8688_STATUS_OC_WARNING)
12162306a36Sopenharmony_ci			ret |= PB_IOUT_OC_WARNING;
12262306a36Sopenharmony_ci		if (mfg_status & MAX8688_STATUS_OC_FAULT)
12362306a36Sopenharmony_ci			ret |= PB_IOUT_OC_FAULT;
12462306a36Sopenharmony_ci		break;
12562306a36Sopenharmony_ci	case PMBUS_STATUS_TEMPERATURE:
12662306a36Sopenharmony_ci		mfg_status = pmbus_read_word_data(client, 0, 0xff,
12762306a36Sopenharmony_ci						  MAX8688_MFG_STATUS);
12862306a36Sopenharmony_ci		if (mfg_status < 0)
12962306a36Sopenharmony_ci			return mfg_status;
13062306a36Sopenharmony_ci		if (mfg_status & MAX8688_STATUS_OT_WARNING)
13162306a36Sopenharmony_ci			ret |= PB_TEMP_OT_WARNING;
13262306a36Sopenharmony_ci		if (mfg_status & MAX8688_STATUS_OT_FAULT)
13362306a36Sopenharmony_ci			ret |= PB_TEMP_OT_FAULT;
13462306a36Sopenharmony_ci		break;
13562306a36Sopenharmony_ci	default:
13662306a36Sopenharmony_ci		ret = -ENODATA;
13762306a36Sopenharmony_ci		break;
13862306a36Sopenharmony_ci	}
13962306a36Sopenharmony_ci	return ret;
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cistatic struct pmbus_driver_info max8688_info = {
14362306a36Sopenharmony_ci	.pages = 1,
14462306a36Sopenharmony_ci	.format[PSC_VOLTAGE_IN] = direct,
14562306a36Sopenharmony_ci	.format[PSC_VOLTAGE_OUT] = direct,
14662306a36Sopenharmony_ci	.format[PSC_TEMPERATURE] = direct,
14762306a36Sopenharmony_ci	.format[PSC_CURRENT_OUT] = direct,
14862306a36Sopenharmony_ci	.m[PSC_VOLTAGE_IN] = 19995,
14962306a36Sopenharmony_ci	.b[PSC_VOLTAGE_IN] = 0,
15062306a36Sopenharmony_ci	.R[PSC_VOLTAGE_IN] = -1,
15162306a36Sopenharmony_ci	.m[PSC_VOLTAGE_OUT] = 19995,
15262306a36Sopenharmony_ci	.b[PSC_VOLTAGE_OUT] = 0,
15362306a36Sopenharmony_ci	.R[PSC_VOLTAGE_OUT] = -1,
15462306a36Sopenharmony_ci	.m[PSC_CURRENT_OUT] = 23109,
15562306a36Sopenharmony_ci	.b[PSC_CURRENT_OUT] = 0,
15662306a36Sopenharmony_ci	.R[PSC_CURRENT_OUT] = -2,
15762306a36Sopenharmony_ci	.m[PSC_TEMPERATURE] = -7612,
15862306a36Sopenharmony_ci	.b[PSC_TEMPERATURE] = 335,
15962306a36Sopenharmony_ci	.R[PSC_TEMPERATURE] = -3,
16062306a36Sopenharmony_ci	.func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP
16162306a36Sopenharmony_ci		| PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
16262306a36Sopenharmony_ci		| PMBUS_HAVE_STATUS_TEMP,
16362306a36Sopenharmony_ci	.read_byte_data = max8688_read_byte_data,
16462306a36Sopenharmony_ci	.read_word_data = max8688_read_word_data,
16562306a36Sopenharmony_ci	.write_word_data = max8688_write_word_data,
16662306a36Sopenharmony_ci};
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_cistatic int max8688_probe(struct i2c_client *client)
16962306a36Sopenharmony_ci{
17062306a36Sopenharmony_ci	return pmbus_do_probe(client, &max8688_info);
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistatic const struct i2c_device_id max8688_id[] = {
17462306a36Sopenharmony_ci	{"max8688", 0},
17562306a36Sopenharmony_ci	{ }
17662306a36Sopenharmony_ci};
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max8688_id);
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci/* This is the driver that will be inserted */
18162306a36Sopenharmony_cistatic struct i2c_driver max8688_driver = {
18262306a36Sopenharmony_ci	.driver = {
18362306a36Sopenharmony_ci		   .name = "max8688",
18462306a36Sopenharmony_ci		   },
18562306a36Sopenharmony_ci	.probe = max8688_probe,
18662306a36Sopenharmony_ci	.id_table = max8688_id,
18762306a36Sopenharmony_ci};
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_cimodule_i2c_driver(max8688_driver);
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ciMODULE_AUTHOR("Guenter Roeck");
19262306a36Sopenharmony_ciMODULE_DESCRIPTION("PMBus driver for Maxim MAX8688");
19362306a36Sopenharmony_ciMODULE_LICENSE("GPL");
19462306a36Sopenharmony_ciMODULE_IMPORT_NS(PMBUS);
195