18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for MAX20710, MAX20730, MAX20734, and MAX20743 Integrated,
48c2ecf20Sopenharmony_ci * Step-Down Switching Regulators
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright 2019 Google LLC.
78c2ecf20Sopenharmony_ci * Copyright 2020 Maxim Integrated
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/bits.h>
118c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
128c2ecf20Sopenharmony_ci#include <linux/err.h>
138c2ecf20Sopenharmony_ci#include <linux/i2c.h>
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/kernel.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/mutex.h>
188c2ecf20Sopenharmony_ci#include <linux/of_device.h>
198c2ecf20Sopenharmony_ci#include <linux/pmbus.h>
208c2ecf20Sopenharmony_ci#include <linux/util_macros.h>
218c2ecf20Sopenharmony_ci#include "pmbus.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cienum chips {
248c2ecf20Sopenharmony_ci	max20710,
258c2ecf20Sopenharmony_ci	max20730,
268c2ecf20Sopenharmony_ci	max20734,
278c2ecf20Sopenharmony_ci	max20743
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cienum {
318c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_VOUT_MIN = 0,
328c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_FREQUENCY,
338c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_PG_DELAY,
348c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_INTERNAL_GAIN,
358c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_BOOT_VOLTAGE,
368c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_OUT_V_RAMP_RATE,
378c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_OC_PROTECT_MODE,
388c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_SS_TIMING,
398c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_IMAX,
408c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_OPERATION,
418c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_ON_OFF_CONFIG,
428c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_SMBALERT_MASK,
438c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_VOUT_MODE,
448c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_VOUT_COMMAND,
458c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_VOUT_MAX,
468c2ecf20Sopenharmony_ci	MAX20730_DEBUGFS_NUM_ENTRIES
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistruct max20730_data {
508c2ecf20Sopenharmony_ci	enum chips id;
518c2ecf20Sopenharmony_ci	struct pmbus_driver_info info;
528c2ecf20Sopenharmony_ci	struct mutex lock;	/* Used to protect against parallel writes */
538c2ecf20Sopenharmony_ci	u16 mfr_devset1;
548c2ecf20Sopenharmony_ci	u16 mfr_devset2;
558c2ecf20Sopenharmony_ci	u16 mfr_voutmin;
568c2ecf20Sopenharmony_ci	u32 vout_voltage_divider[2];
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define to_max20730_data(x)  container_of(x, struct max20730_data, info)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define VOLT_FROM_REG(val)	DIV_ROUND_CLOSEST((val), 1 << 9)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define PMBUS_SMB_ALERT_MASK	0x1B
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define MAX20730_MFR_VOUT_MIN	0xd1
668c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1	0xd2
678c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET2	0xd3
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define MAX20730_MFR_VOUT_MIN_MASK		GENMASK(9, 0)
708c2ecf20Sopenharmony_ci#define MAX20730_MFR_VOUT_MIN_BIT_POS		0
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_RGAIN_MASK		(BIT(13) | BIT(14))
738c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_OTP_MASK		(BIT(11) | BIT(12))
748c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_VBOOT_MASK		(BIT(8) | BIT(9))
758c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_OCP_MASK		(BIT(5) | BIT(6))
768c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_FSW_MASK		GENMASK(4, 2)
778c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_TSTAT_MASK		(BIT(0) | BIT(1))
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_RGAIN_BIT_POS	13
808c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_OTP_BIT_POS	11
818c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_VBOOT_BIT_POS	8
828c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_OCP_BIT_POS	5
838c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_FSW_BIT_POS	2
848c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET1_TSTAT_BIT_POS	0
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET2_IMAX_MASK		GENMASK(10, 8)
878c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET2_VRATE		(BIT(6) | BIT(7))
888c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET2_OCPM_MASK		BIT(5)
898c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET2_SS_MASK		(BIT(0) | BIT(1))
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET2_IMAX_BIT_POS	8
928c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET2_VRATE_BIT_POS	6
938c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET2_OCPM_BIT_POS	5
948c2ecf20Sopenharmony_ci#define MAX20730_MFR_DEVSET2_SS_BIT_POS		0
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define DEBUG_FS_DATA_MAX			16
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistruct max20730_debugfs_data {
998c2ecf20Sopenharmony_ci	struct i2c_client *client;
1008c2ecf20Sopenharmony_ci	int debugfs_entries[MAX20730_DEBUGFS_NUM_ENTRIES];
1018c2ecf20Sopenharmony_ci};
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define to_psu(x, y) container_of((x), \
1048c2ecf20Sopenharmony_ci			struct max20730_debugfs_data, debugfs_entries[(y)])
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
1078c2ecf20Sopenharmony_cistatic ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
1088c2ecf20Sopenharmony_ci				     size_t count, loff_t *ppos)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	int ret, len;
1118c2ecf20Sopenharmony_ci	int *idxp = file->private_data;
1128c2ecf20Sopenharmony_ci	int idx = *idxp;
1138c2ecf20Sopenharmony_ci	struct max20730_debugfs_data *psu = to_psu(idxp, idx);
1148c2ecf20Sopenharmony_ci	const struct pmbus_driver_info *info;
1158c2ecf20Sopenharmony_ci	const struct max20730_data *data;
1168c2ecf20Sopenharmony_ci	char tbuf[DEBUG_FS_DATA_MAX] = { 0 };
1178c2ecf20Sopenharmony_ci	u16 val;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	info = pmbus_get_driver_info(psu->client);
1208c2ecf20Sopenharmony_ci	data = to_max20730_data(info);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	switch (idx) {
1238c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_VOUT_MIN:
1248c2ecf20Sopenharmony_ci		ret = VOLT_FROM_REG(data->mfr_voutmin * 10000);
1258c2ecf20Sopenharmony_ci		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d.%d\n",
1268c2ecf20Sopenharmony_ci				ret / 10000, ret % 10000);
1278c2ecf20Sopenharmony_ci		break;
1288c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_FREQUENCY:
1298c2ecf20Sopenharmony_ci		val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_FSW_MASK)
1308c2ecf20Sopenharmony_ci			>> MAX20730_MFR_DEVSET1_FSW_BIT_POS;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci		if (val == 0)
1338c2ecf20Sopenharmony_ci			ret = 400;
1348c2ecf20Sopenharmony_ci		else if (val == 1)
1358c2ecf20Sopenharmony_ci			ret = 500;
1368c2ecf20Sopenharmony_ci		else if (val == 2 || val == 3)
1378c2ecf20Sopenharmony_ci			ret = 600;
1388c2ecf20Sopenharmony_ci		else if (val == 4)
1398c2ecf20Sopenharmony_ci			ret = 700;
1408c2ecf20Sopenharmony_ci		else if (val == 5)
1418c2ecf20Sopenharmony_ci			ret = 800;
1428c2ecf20Sopenharmony_ci		else
1438c2ecf20Sopenharmony_ci			ret = 900;
1448c2ecf20Sopenharmony_ci		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
1458c2ecf20Sopenharmony_ci		break;
1468c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_PG_DELAY:
1478c2ecf20Sopenharmony_ci		val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_TSTAT_MASK)
1488c2ecf20Sopenharmony_ci			>> MAX20730_MFR_DEVSET1_TSTAT_BIT_POS;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci		if (val == 0)
1518c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "2000\n", DEBUG_FS_DATA_MAX);
1528c2ecf20Sopenharmony_ci		else if (val == 1)
1538c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "125\n", DEBUG_FS_DATA_MAX);
1548c2ecf20Sopenharmony_ci		else if (val == 2)
1558c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "62.5\n", DEBUG_FS_DATA_MAX);
1568c2ecf20Sopenharmony_ci		else
1578c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "32\n", DEBUG_FS_DATA_MAX);
1588c2ecf20Sopenharmony_ci		break;
1598c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_INTERNAL_GAIN:
1608c2ecf20Sopenharmony_ci		val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_RGAIN_MASK)
1618c2ecf20Sopenharmony_ci			>> MAX20730_MFR_DEVSET1_RGAIN_BIT_POS;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci		if (data->id == max20734) {
1648c2ecf20Sopenharmony_ci			/* AN6209 */
1658c2ecf20Sopenharmony_ci			if (val == 0)
1668c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "0.8\n", DEBUG_FS_DATA_MAX);
1678c2ecf20Sopenharmony_ci			else if (val == 1)
1688c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "3.2\n", DEBUG_FS_DATA_MAX);
1698c2ecf20Sopenharmony_ci			else if (val == 2)
1708c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "1.6\n", DEBUG_FS_DATA_MAX);
1718c2ecf20Sopenharmony_ci			else
1728c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "6.4\n", DEBUG_FS_DATA_MAX);
1738c2ecf20Sopenharmony_ci		} else if (data->id == max20730 || data->id == max20710) {
1748c2ecf20Sopenharmony_ci			/* AN6042 or AN6140 */
1758c2ecf20Sopenharmony_ci			if (val == 0)
1768c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "0.9\n", DEBUG_FS_DATA_MAX);
1778c2ecf20Sopenharmony_ci			else if (val == 1)
1788c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "3.6\n", DEBUG_FS_DATA_MAX);
1798c2ecf20Sopenharmony_ci			else if (val == 2)
1808c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "1.8\n", DEBUG_FS_DATA_MAX);
1818c2ecf20Sopenharmony_ci			else
1828c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "7.2\n", DEBUG_FS_DATA_MAX);
1838c2ecf20Sopenharmony_ci		} else if (data->id == max20743) {
1848c2ecf20Sopenharmony_ci			/* AN6042 */
1858c2ecf20Sopenharmony_ci			if (val == 0)
1868c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "0.45\n", DEBUG_FS_DATA_MAX);
1878c2ecf20Sopenharmony_ci			else if (val == 1)
1888c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "1.8\n", DEBUG_FS_DATA_MAX);
1898c2ecf20Sopenharmony_ci			else if (val == 2)
1908c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "0.9\n", DEBUG_FS_DATA_MAX);
1918c2ecf20Sopenharmony_ci			else
1928c2ecf20Sopenharmony_ci				len = strlcpy(tbuf, "3.6\n", DEBUG_FS_DATA_MAX);
1938c2ecf20Sopenharmony_ci		} else {
1948c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "Not supported\n", DEBUG_FS_DATA_MAX);
1958c2ecf20Sopenharmony_ci		}
1968c2ecf20Sopenharmony_ci		break;
1978c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_BOOT_VOLTAGE:
1988c2ecf20Sopenharmony_ci		val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_VBOOT_MASK)
1998c2ecf20Sopenharmony_ci			>> MAX20730_MFR_DEVSET1_VBOOT_BIT_POS;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci		if (val == 0)
2028c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "0.6484\n", DEBUG_FS_DATA_MAX);
2038c2ecf20Sopenharmony_ci		else if (val == 1)
2048c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "0.8984\n", DEBUG_FS_DATA_MAX);
2058c2ecf20Sopenharmony_ci		else if (val == 2)
2068c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "1.0\n", DEBUG_FS_DATA_MAX);
2078c2ecf20Sopenharmony_ci		else
2088c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
2098c2ecf20Sopenharmony_ci		break;
2108c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_OUT_V_RAMP_RATE:
2118c2ecf20Sopenharmony_ci		val = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_VRATE)
2128c2ecf20Sopenharmony_ci			>> MAX20730_MFR_DEVSET2_VRATE_BIT_POS;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci		if (val == 0)
2158c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "4\n", DEBUG_FS_DATA_MAX);
2168c2ecf20Sopenharmony_ci		else if (val == 1)
2178c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "2\n", DEBUG_FS_DATA_MAX);
2188c2ecf20Sopenharmony_ci		else if (val == 2)
2198c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "1\n", DEBUG_FS_DATA_MAX);
2208c2ecf20Sopenharmony_ci		else
2218c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
2228c2ecf20Sopenharmony_ci		break;
2238c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_OC_PROTECT_MODE:
2248c2ecf20Sopenharmony_ci		ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_OCPM_MASK)
2258c2ecf20Sopenharmony_ci			>> MAX20730_MFR_DEVSET2_OCPM_BIT_POS;
2268c2ecf20Sopenharmony_ci		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
2278c2ecf20Sopenharmony_ci		break;
2288c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_SS_TIMING:
2298c2ecf20Sopenharmony_ci		val = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_SS_MASK)
2308c2ecf20Sopenharmony_ci			>> MAX20730_MFR_DEVSET2_SS_BIT_POS;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci		if (val == 0)
2338c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "0.75\n", DEBUG_FS_DATA_MAX);
2348c2ecf20Sopenharmony_ci		else if (val == 1)
2358c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "1.5\n", DEBUG_FS_DATA_MAX);
2368c2ecf20Sopenharmony_ci		else if (val == 2)
2378c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "3\n", DEBUG_FS_DATA_MAX);
2388c2ecf20Sopenharmony_ci		else
2398c2ecf20Sopenharmony_ci			len = strlcpy(tbuf, "6\n", DEBUG_FS_DATA_MAX);
2408c2ecf20Sopenharmony_ci		break;
2418c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_IMAX:
2428c2ecf20Sopenharmony_ci		ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_IMAX_MASK)
2438c2ecf20Sopenharmony_ci			>> MAX20730_MFR_DEVSET2_IMAX_BIT_POS;
2448c2ecf20Sopenharmony_ci		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
2458c2ecf20Sopenharmony_ci		break;
2468c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_OPERATION:
2478c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_byte_data(psu->client, PMBUS_OPERATION);
2488c2ecf20Sopenharmony_ci		if (ret < 0)
2498c2ecf20Sopenharmony_ci			return ret;
2508c2ecf20Sopenharmony_ci		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
2518c2ecf20Sopenharmony_ci		break;
2528c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_ON_OFF_CONFIG:
2538c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_byte_data(psu->client, PMBUS_ON_OFF_CONFIG);
2548c2ecf20Sopenharmony_ci		if (ret < 0)
2558c2ecf20Sopenharmony_ci			return ret;
2568c2ecf20Sopenharmony_ci		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
2578c2ecf20Sopenharmony_ci		break;
2588c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_SMBALERT_MASK:
2598c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_word_data(psu->client,
2608c2ecf20Sopenharmony_ci					       PMBUS_SMB_ALERT_MASK);
2618c2ecf20Sopenharmony_ci		if (ret < 0)
2628c2ecf20Sopenharmony_ci			return ret;
2638c2ecf20Sopenharmony_ci		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
2648c2ecf20Sopenharmony_ci		break;
2658c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_VOUT_MODE:
2668c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_byte_data(psu->client, PMBUS_VOUT_MODE);
2678c2ecf20Sopenharmony_ci		if (ret < 0)
2688c2ecf20Sopenharmony_ci			return ret;
2698c2ecf20Sopenharmony_ci		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
2708c2ecf20Sopenharmony_ci		break;
2718c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_VOUT_COMMAND:
2728c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_COMMAND);
2738c2ecf20Sopenharmony_ci		if (ret < 0)
2748c2ecf20Sopenharmony_ci			return ret;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci		ret = VOLT_FROM_REG(ret * 10000);
2778c2ecf20Sopenharmony_ci		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
2788c2ecf20Sopenharmony_ci				"%d.%d\n", ret / 10000, ret % 10000);
2798c2ecf20Sopenharmony_ci		break;
2808c2ecf20Sopenharmony_ci	case MAX20730_DEBUGFS_VOUT_MAX:
2818c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_MAX);
2828c2ecf20Sopenharmony_ci		if (ret < 0)
2838c2ecf20Sopenharmony_ci			return ret;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci		ret = VOLT_FROM_REG(ret * 10000);
2868c2ecf20Sopenharmony_ci		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
2878c2ecf20Sopenharmony_ci				"%d.%d\n", ret / 10000, ret % 10000);
2888c2ecf20Sopenharmony_ci		break;
2898c2ecf20Sopenharmony_ci	default:
2908c2ecf20Sopenharmony_ci		len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
2918c2ecf20Sopenharmony_ci	}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	return simple_read_from_buffer(buf, count, ppos, tbuf, len);
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic const struct file_operations max20730_fops = {
2978c2ecf20Sopenharmony_ci	.llseek = noop_llseek,
2988c2ecf20Sopenharmony_ci	.read = max20730_debugfs_read,
2998c2ecf20Sopenharmony_ci	.write = NULL,
3008c2ecf20Sopenharmony_ci	.open = simple_open,
3018c2ecf20Sopenharmony_ci};
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic int max20730_init_debugfs(struct i2c_client *client,
3048c2ecf20Sopenharmony_ci				 struct max20730_data *data)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	int ret, i;
3078c2ecf20Sopenharmony_ci	struct dentry *debugfs;
3088c2ecf20Sopenharmony_ci	struct dentry *max20730_dir;
3098c2ecf20Sopenharmony_ci	struct max20730_debugfs_data *psu;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET2);
3128c2ecf20Sopenharmony_ci	if (ret < 0)
3138c2ecf20Sopenharmony_ci		return ret;
3148c2ecf20Sopenharmony_ci	data->mfr_devset2 = ret;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_word_data(client, MAX20730_MFR_VOUT_MIN);
3178c2ecf20Sopenharmony_ci	if (ret < 0)
3188c2ecf20Sopenharmony_ci		return ret;
3198c2ecf20Sopenharmony_ci	data->mfr_voutmin = ret;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
3228c2ecf20Sopenharmony_ci	if (!psu)
3238c2ecf20Sopenharmony_ci		return -ENOMEM;
3248c2ecf20Sopenharmony_ci	psu->client = client;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	debugfs = pmbus_get_debugfs_dir(client);
3278c2ecf20Sopenharmony_ci	if (!debugfs)
3288c2ecf20Sopenharmony_ci		return -ENOENT;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	max20730_dir = debugfs_create_dir(client->name, debugfs);
3318c2ecf20Sopenharmony_ci	if (!max20730_dir)
3328c2ecf20Sopenharmony_ci		return -ENOENT;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	for (i = 0; i < MAX20730_DEBUGFS_NUM_ENTRIES; ++i)
3358c2ecf20Sopenharmony_ci		psu->debugfs_entries[i] = i;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	debugfs_create_file("vout_min", 0444, max20730_dir,
3388c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MIN],
3398c2ecf20Sopenharmony_ci			    &max20730_fops);
3408c2ecf20Sopenharmony_ci	debugfs_create_file("frequency", 0444, max20730_dir,
3418c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_FREQUENCY],
3428c2ecf20Sopenharmony_ci			    &max20730_fops);
3438c2ecf20Sopenharmony_ci	debugfs_create_file("power_good_delay", 0444, max20730_dir,
3448c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_PG_DELAY],
3458c2ecf20Sopenharmony_ci			    &max20730_fops);
3468c2ecf20Sopenharmony_ci	debugfs_create_file("internal_gain", 0444, max20730_dir,
3478c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_INTERNAL_GAIN],
3488c2ecf20Sopenharmony_ci			    &max20730_fops);
3498c2ecf20Sopenharmony_ci	debugfs_create_file("boot_voltage", 0444, max20730_dir,
3508c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_BOOT_VOLTAGE],
3518c2ecf20Sopenharmony_ci			    &max20730_fops);
3528c2ecf20Sopenharmony_ci	debugfs_create_file("out_voltage_ramp_rate", 0444, max20730_dir,
3538c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_OUT_V_RAMP_RATE],
3548c2ecf20Sopenharmony_ci			    &max20730_fops);
3558c2ecf20Sopenharmony_ci	debugfs_create_file("oc_protection_mode", 0444, max20730_dir,
3568c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_OC_PROTECT_MODE],
3578c2ecf20Sopenharmony_ci			    &max20730_fops);
3588c2ecf20Sopenharmony_ci	debugfs_create_file("soft_start_timing", 0444, max20730_dir,
3598c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_SS_TIMING],
3608c2ecf20Sopenharmony_ci			    &max20730_fops);
3618c2ecf20Sopenharmony_ci	debugfs_create_file("imax", 0444, max20730_dir,
3628c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_IMAX],
3638c2ecf20Sopenharmony_ci			    &max20730_fops);
3648c2ecf20Sopenharmony_ci	debugfs_create_file("operation", 0444, max20730_dir,
3658c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_OPERATION],
3668c2ecf20Sopenharmony_ci			    &max20730_fops);
3678c2ecf20Sopenharmony_ci	debugfs_create_file("on_off_config", 0444, max20730_dir,
3688c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_ON_OFF_CONFIG],
3698c2ecf20Sopenharmony_ci			    &max20730_fops);
3708c2ecf20Sopenharmony_ci	debugfs_create_file("smbalert_mask", 0444, max20730_dir,
3718c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_SMBALERT_MASK],
3728c2ecf20Sopenharmony_ci			    &max20730_fops);
3738c2ecf20Sopenharmony_ci	debugfs_create_file("vout_mode", 0444, max20730_dir,
3748c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MODE],
3758c2ecf20Sopenharmony_ci			    &max20730_fops);
3768c2ecf20Sopenharmony_ci	debugfs_create_file("vout_command", 0444, max20730_dir,
3778c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_COMMAND],
3788c2ecf20Sopenharmony_ci			    &max20730_fops);
3798c2ecf20Sopenharmony_ci	debugfs_create_file("vout_max", 0444, max20730_dir,
3808c2ecf20Sopenharmony_ci			    &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MAX],
3818c2ecf20Sopenharmony_ci			    &max20730_fops);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	return 0;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci#else
3868c2ecf20Sopenharmony_cistatic int max20730_init_debugfs(struct i2c_client *client,
3878c2ecf20Sopenharmony_ci				 struct max20730_data *data)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	return 0;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci#endif /* CONFIG_DEBUG_FS */
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_cistatic const struct i2c_device_id max20730_id[];
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci/*
3968c2ecf20Sopenharmony_ci * Convert discreet value to direct data format. Strictly speaking, all passed
3978c2ecf20Sopenharmony_ci * values are constants, so we could do that calculation manually. On the
3988c2ecf20Sopenharmony_ci * downside, that would make the driver more difficult to maintain, so lets
3998c2ecf20Sopenharmony_ci * use this approach.
4008c2ecf20Sopenharmony_ci */
4018c2ecf20Sopenharmony_cistatic u16 val_to_direct(int v, enum pmbus_sensor_classes class,
4028c2ecf20Sopenharmony_ci			 const struct pmbus_driver_info *info)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	int R = info->R[class] - 3;	/* take milli-units into account */
4058c2ecf20Sopenharmony_ci	int b = info->b[class] * 1000;
4068c2ecf20Sopenharmony_ci	long d;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	d = v * info->m[class] + b;
4098c2ecf20Sopenharmony_ci	/*
4108c2ecf20Sopenharmony_ci	 * R < 0 is true for all callers, so we don't need to bother
4118c2ecf20Sopenharmony_ci	 * about the R > 0 case.
4128c2ecf20Sopenharmony_ci	 */
4138c2ecf20Sopenharmony_ci	while (R < 0) {
4148c2ecf20Sopenharmony_ci		d = DIV_ROUND_CLOSEST(d, 10);
4158c2ecf20Sopenharmony_ci		R++;
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci	return (u16)d;
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistatic long direct_to_val(u16 w, enum pmbus_sensor_classes class,
4218c2ecf20Sopenharmony_ci			  const struct pmbus_driver_info *info)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	int R = info->R[class] - 3;
4248c2ecf20Sopenharmony_ci	int b = info->b[class] * 1000;
4258c2ecf20Sopenharmony_ci	int m = info->m[class];
4268c2ecf20Sopenharmony_ci	long d = (s16)w;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (m == 0)
4298c2ecf20Sopenharmony_ci		return 0;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	while (R < 0) {
4328c2ecf20Sopenharmony_ci		d *= 10;
4338c2ecf20Sopenharmony_ci		R++;
4348c2ecf20Sopenharmony_ci	}
4358c2ecf20Sopenharmony_ci	d = (d - b) / m;
4368c2ecf20Sopenharmony_ci	return d;
4378c2ecf20Sopenharmony_ci}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_cistatic u32 max_current[][5] = {
4408c2ecf20Sopenharmony_ci	[max20710] = { 6200, 8000, 9700, 11600 },
4418c2ecf20Sopenharmony_ci	[max20730] = { 13000, 16600, 20100, 23600 },
4428c2ecf20Sopenharmony_ci	[max20734] = { 21000, 27000, 32000, 38000 },
4438c2ecf20Sopenharmony_ci	[max20743] = { 18900, 24100, 29200, 34100 },
4448c2ecf20Sopenharmony_ci};
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic int max20730_read_word_data(struct i2c_client *client, int page,
4478c2ecf20Sopenharmony_ci				   int phase, int reg)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
4508c2ecf20Sopenharmony_ci	const struct max20730_data *data = to_max20730_data(info);
4518c2ecf20Sopenharmony_ci	int ret = 0;
4528c2ecf20Sopenharmony_ci	u32 max_c;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	switch (reg) {
4558c2ecf20Sopenharmony_ci	case PMBUS_OT_FAULT_LIMIT:
4568c2ecf20Sopenharmony_ci		switch ((data->mfr_devset1 >> 11) & 0x3) {
4578c2ecf20Sopenharmony_ci		case 0x0:
4588c2ecf20Sopenharmony_ci			ret = val_to_direct(150000, PSC_TEMPERATURE, info);
4598c2ecf20Sopenharmony_ci			break;
4608c2ecf20Sopenharmony_ci		case 0x1:
4618c2ecf20Sopenharmony_ci			ret = val_to_direct(130000, PSC_TEMPERATURE, info);
4628c2ecf20Sopenharmony_ci			break;
4638c2ecf20Sopenharmony_ci		default:
4648c2ecf20Sopenharmony_ci			ret = -ENODATA;
4658c2ecf20Sopenharmony_ci			break;
4668c2ecf20Sopenharmony_ci		}
4678c2ecf20Sopenharmony_ci		break;
4688c2ecf20Sopenharmony_ci	case PMBUS_IOUT_OC_FAULT_LIMIT:
4698c2ecf20Sopenharmony_ci		max_c = max_current[data->id][(data->mfr_devset1 >> 5) & 0x3];
4708c2ecf20Sopenharmony_ci		ret = val_to_direct(max_c, PSC_CURRENT_OUT, info);
4718c2ecf20Sopenharmony_ci		break;
4728c2ecf20Sopenharmony_ci	case PMBUS_READ_VOUT:
4738c2ecf20Sopenharmony_ci		ret = pmbus_read_word_data(client, page, phase, reg);
4748c2ecf20Sopenharmony_ci		if (ret > 0 && data->vout_voltage_divider[0] && data->vout_voltage_divider[1]) {
4758c2ecf20Sopenharmony_ci			u64 temp = DIV_ROUND_CLOSEST_ULL((u64)ret * data->vout_voltage_divider[1],
4768c2ecf20Sopenharmony_ci							 data->vout_voltage_divider[0]);
4778c2ecf20Sopenharmony_ci			ret = clamp_val(temp, 0, 0xffff);
4788c2ecf20Sopenharmony_ci		}
4798c2ecf20Sopenharmony_ci		break;
4808c2ecf20Sopenharmony_ci	default:
4818c2ecf20Sopenharmony_ci		ret = -ENODATA;
4828c2ecf20Sopenharmony_ci		break;
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci	return ret;
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic int max20730_write_word_data(struct i2c_client *client, int page,
4888c2ecf20Sopenharmony_ci				    int reg, u16 word)
4898c2ecf20Sopenharmony_ci{
4908c2ecf20Sopenharmony_ci	struct pmbus_driver_info *info;
4918c2ecf20Sopenharmony_ci	struct max20730_data *data;
4928c2ecf20Sopenharmony_ci	u16 devset1;
4938c2ecf20Sopenharmony_ci	int ret = 0;
4948c2ecf20Sopenharmony_ci	int idx;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	info = (struct pmbus_driver_info *)pmbus_get_driver_info(client);
4978c2ecf20Sopenharmony_ci	data = to_max20730_data(info);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
5008c2ecf20Sopenharmony_ci	devset1 = data->mfr_devset1;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	switch (reg) {
5038c2ecf20Sopenharmony_ci	case PMBUS_OT_FAULT_LIMIT:
5048c2ecf20Sopenharmony_ci		devset1 &= ~(BIT(11) | BIT(12));
5058c2ecf20Sopenharmony_ci		if (direct_to_val(word, PSC_TEMPERATURE, info) < 140000)
5068c2ecf20Sopenharmony_ci			devset1 |= BIT(11);
5078c2ecf20Sopenharmony_ci		break;
5088c2ecf20Sopenharmony_ci	case PMBUS_IOUT_OC_FAULT_LIMIT:
5098c2ecf20Sopenharmony_ci		devset1 &= ~(BIT(5) | BIT(6));
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci		idx = find_closest(direct_to_val(word, PSC_CURRENT_OUT, info),
5128c2ecf20Sopenharmony_ci				   max_current[data->id], 4);
5138c2ecf20Sopenharmony_ci		devset1 |= (idx << 5);
5148c2ecf20Sopenharmony_ci		break;
5158c2ecf20Sopenharmony_ci	default:
5168c2ecf20Sopenharmony_ci		ret = -ENODATA;
5178c2ecf20Sopenharmony_ci		break;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	if (!ret && devset1 != data->mfr_devset1) {
5218c2ecf20Sopenharmony_ci		ret = i2c_smbus_write_word_data(client, MAX20730_MFR_DEVSET1,
5228c2ecf20Sopenharmony_ci						devset1);
5238c2ecf20Sopenharmony_ci		if (!ret) {
5248c2ecf20Sopenharmony_ci			data->mfr_devset1 = devset1;
5258c2ecf20Sopenharmony_ci			pmbus_clear_cache(client);
5268c2ecf20Sopenharmony_ci		}
5278c2ecf20Sopenharmony_ci	}
5288c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
5298c2ecf20Sopenharmony_ci	return ret;
5308c2ecf20Sopenharmony_ci}
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_cistatic const struct pmbus_driver_info max20730_info[] = {
5338c2ecf20Sopenharmony_ci	[max20710] = {
5348c2ecf20Sopenharmony_ci		.pages = 1,
5358c2ecf20Sopenharmony_ci		.read_word_data = max20730_read_word_data,
5368c2ecf20Sopenharmony_ci		.write_word_data = max20730_write_word_data,
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci		/* Source : Maxim AN6140 and AN6042 */
5398c2ecf20Sopenharmony_ci		.format[PSC_TEMPERATURE] = direct,
5408c2ecf20Sopenharmony_ci		.m[PSC_TEMPERATURE] = 21,
5418c2ecf20Sopenharmony_ci		.b[PSC_TEMPERATURE] = 5887,
5428c2ecf20Sopenharmony_ci		.R[PSC_TEMPERATURE] = -1,
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci		.format[PSC_VOLTAGE_IN] = direct,
5458c2ecf20Sopenharmony_ci		.m[PSC_VOLTAGE_IN] = 3609,
5468c2ecf20Sopenharmony_ci		.b[PSC_VOLTAGE_IN] = 0,
5478c2ecf20Sopenharmony_ci		.R[PSC_VOLTAGE_IN] = -2,
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci		.format[PSC_CURRENT_OUT] = direct,
5508c2ecf20Sopenharmony_ci		.m[PSC_CURRENT_OUT] = 153,
5518c2ecf20Sopenharmony_ci		.b[PSC_CURRENT_OUT] = 4976,
5528c2ecf20Sopenharmony_ci		.R[PSC_CURRENT_OUT] = -1,
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci		.format[PSC_VOLTAGE_OUT] = linear,
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci		.func[0] = PMBUS_HAVE_VIN |
5578c2ecf20Sopenharmony_ci			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
5588c2ecf20Sopenharmony_ci			PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
5598c2ecf20Sopenharmony_ci			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
5608c2ecf20Sopenharmony_ci			PMBUS_HAVE_STATUS_INPUT,
5618c2ecf20Sopenharmony_ci	},
5628c2ecf20Sopenharmony_ci	[max20730] = {
5638c2ecf20Sopenharmony_ci		.pages = 1,
5648c2ecf20Sopenharmony_ci		.read_word_data = max20730_read_word_data,
5658c2ecf20Sopenharmony_ci		.write_word_data = max20730_write_word_data,
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci		/* Source : Maxim AN6042 */
5688c2ecf20Sopenharmony_ci		.format[PSC_TEMPERATURE] = direct,
5698c2ecf20Sopenharmony_ci		.m[PSC_TEMPERATURE] = 21,
5708c2ecf20Sopenharmony_ci		.b[PSC_TEMPERATURE] = 5887,
5718c2ecf20Sopenharmony_ci		.R[PSC_TEMPERATURE] = -1,
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci		.format[PSC_VOLTAGE_IN] = direct,
5748c2ecf20Sopenharmony_ci		.m[PSC_VOLTAGE_IN] = 3609,
5758c2ecf20Sopenharmony_ci		.b[PSC_VOLTAGE_IN] = 0,
5768c2ecf20Sopenharmony_ci		.R[PSC_VOLTAGE_IN] = -2,
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci		/*
5798c2ecf20Sopenharmony_ci		 * Values in the datasheet are adjusted for temperature and
5808c2ecf20Sopenharmony_ci		 * for the relationship between Vin and Vout.
5818c2ecf20Sopenharmony_ci		 * Unfortunately, the data sheet suggests that Vout measurement
5828c2ecf20Sopenharmony_ci		 * may be scaled with a resistor array. This is indeed the case
5838c2ecf20Sopenharmony_ci		 * at least on the evaulation boards. As a result, any in-driver
5848c2ecf20Sopenharmony_ci		 * adjustments would either be wrong or require elaborate means
5858c2ecf20Sopenharmony_ci		 * to configure the scaling. Instead of doing that, just report
5868c2ecf20Sopenharmony_ci		 * raw values and let userspace handle adjustments.
5878c2ecf20Sopenharmony_ci		 */
5888c2ecf20Sopenharmony_ci		.format[PSC_CURRENT_OUT] = direct,
5898c2ecf20Sopenharmony_ci		.m[PSC_CURRENT_OUT] = 153,
5908c2ecf20Sopenharmony_ci		.b[PSC_CURRENT_OUT] = 4976,
5918c2ecf20Sopenharmony_ci		.R[PSC_CURRENT_OUT] = -1,
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci		.format[PSC_VOLTAGE_OUT] = linear,
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci		.func[0] = PMBUS_HAVE_VIN |
5968c2ecf20Sopenharmony_ci			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
5978c2ecf20Sopenharmony_ci			PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
5988c2ecf20Sopenharmony_ci			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
5998c2ecf20Sopenharmony_ci			PMBUS_HAVE_STATUS_INPUT,
6008c2ecf20Sopenharmony_ci	},
6018c2ecf20Sopenharmony_ci	[max20734] = {
6028c2ecf20Sopenharmony_ci		.pages = 1,
6038c2ecf20Sopenharmony_ci		.read_word_data = max20730_read_word_data,
6048c2ecf20Sopenharmony_ci		.write_word_data = max20730_write_word_data,
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci		/* Source : Maxim AN6209 */
6078c2ecf20Sopenharmony_ci		.format[PSC_TEMPERATURE] = direct,
6088c2ecf20Sopenharmony_ci		.m[PSC_TEMPERATURE] = 21,
6098c2ecf20Sopenharmony_ci		.b[PSC_TEMPERATURE] = 5887,
6108c2ecf20Sopenharmony_ci		.R[PSC_TEMPERATURE] = -1,
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci		.format[PSC_VOLTAGE_IN] = direct,
6138c2ecf20Sopenharmony_ci		.m[PSC_VOLTAGE_IN] = 3592,
6148c2ecf20Sopenharmony_ci		.b[PSC_VOLTAGE_IN] = 0,
6158c2ecf20Sopenharmony_ci		.R[PSC_VOLTAGE_IN] = -2,
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci		.format[PSC_CURRENT_OUT] = direct,
6188c2ecf20Sopenharmony_ci		.m[PSC_CURRENT_OUT] = 111,
6198c2ecf20Sopenharmony_ci		.b[PSC_CURRENT_OUT] = 3461,
6208c2ecf20Sopenharmony_ci		.R[PSC_CURRENT_OUT] = -1,
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci		.format[PSC_VOLTAGE_OUT] = linear,
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci		.func[0] = PMBUS_HAVE_VIN |
6258c2ecf20Sopenharmony_ci			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
6268c2ecf20Sopenharmony_ci			PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
6278c2ecf20Sopenharmony_ci			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
6288c2ecf20Sopenharmony_ci			PMBUS_HAVE_STATUS_INPUT,
6298c2ecf20Sopenharmony_ci	},
6308c2ecf20Sopenharmony_ci	[max20743] = {
6318c2ecf20Sopenharmony_ci		.pages = 1,
6328c2ecf20Sopenharmony_ci		.read_word_data = max20730_read_word_data,
6338c2ecf20Sopenharmony_ci		.write_word_data = max20730_write_word_data,
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci		/* Source : Maxim AN6042 */
6368c2ecf20Sopenharmony_ci		.format[PSC_TEMPERATURE] = direct,
6378c2ecf20Sopenharmony_ci		.m[PSC_TEMPERATURE] = 21,
6388c2ecf20Sopenharmony_ci		.b[PSC_TEMPERATURE] = 5887,
6398c2ecf20Sopenharmony_ci		.R[PSC_TEMPERATURE] = -1,
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci		.format[PSC_VOLTAGE_IN] = direct,
6428c2ecf20Sopenharmony_ci		.m[PSC_VOLTAGE_IN] = 3597,
6438c2ecf20Sopenharmony_ci		.b[PSC_VOLTAGE_IN] = 0,
6448c2ecf20Sopenharmony_ci		.R[PSC_VOLTAGE_IN] = -2,
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci		.format[PSC_CURRENT_OUT] = direct,
6478c2ecf20Sopenharmony_ci		.m[PSC_CURRENT_OUT] = 95,
6488c2ecf20Sopenharmony_ci		.b[PSC_CURRENT_OUT] = 5014,
6498c2ecf20Sopenharmony_ci		.R[PSC_CURRENT_OUT] = -1,
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci		.format[PSC_VOLTAGE_OUT] = linear,
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci		.func[0] = PMBUS_HAVE_VIN |
6548c2ecf20Sopenharmony_ci			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
6558c2ecf20Sopenharmony_ci			PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
6568c2ecf20Sopenharmony_ci			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
6578c2ecf20Sopenharmony_ci			PMBUS_HAVE_STATUS_INPUT,
6588c2ecf20Sopenharmony_ci	},
6598c2ecf20Sopenharmony_ci};
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic int max20730_probe(struct i2c_client *client)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
6648c2ecf20Sopenharmony_ci	u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
6658c2ecf20Sopenharmony_ci	struct max20730_data *data;
6668c2ecf20Sopenharmony_ci	enum chips chip_id;
6678c2ecf20Sopenharmony_ci	int ret;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter,
6708c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_READ_BYTE_DATA |
6718c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_READ_WORD_DATA |
6728c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_BLOCK_DATA))
6738c2ecf20Sopenharmony_ci		return -ENODEV;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
6768c2ecf20Sopenharmony_ci	if (ret < 0) {
6778c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to read Manufacturer ID\n");
6788c2ecf20Sopenharmony_ci		return ret;
6798c2ecf20Sopenharmony_ci	}
6808c2ecf20Sopenharmony_ci	if (ret != 5 || strncmp(buf, "MAXIM", 5)) {
6818c2ecf20Sopenharmony_ci		buf[ret] = '\0';
6828c2ecf20Sopenharmony_ci		dev_err(dev, "Unsupported Manufacturer ID '%s'\n", buf);
6838c2ecf20Sopenharmony_ci		return -ENODEV;
6848c2ecf20Sopenharmony_ci	}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	/*
6878c2ecf20Sopenharmony_ci	 * The chips support reading PMBUS_MFR_MODEL. On both MAX20730
6888c2ecf20Sopenharmony_ci	 * and MAX20734, reading it returns M20743. Presumably that is
6898c2ecf20Sopenharmony_ci	 * the reason why the command is not documented. Unfortunately,
6908c2ecf20Sopenharmony_ci	 * that means that there is no reliable means to detect the chip.
6918c2ecf20Sopenharmony_ci	 * However, we can at least detect the chip series. Compare
6928c2ecf20Sopenharmony_ci	 * the returned value against 'M20743' and bail out if there is
6938c2ecf20Sopenharmony_ci	 * a mismatch. If that doesn't work for all chips, we may have
6948c2ecf20Sopenharmony_ci	 * to remove this check.
6958c2ecf20Sopenharmony_ci	 */
6968c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
6978c2ecf20Sopenharmony_ci	if (ret < 0) {
6988c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to read Manufacturer Model\n");
6998c2ecf20Sopenharmony_ci		return ret;
7008c2ecf20Sopenharmony_ci	}
7018c2ecf20Sopenharmony_ci	if (ret != 6 || strncmp(buf, "M20743", 6)) {
7028c2ecf20Sopenharmony_ci		buf[ret] = '\0';
7038c2ecf20Sopenharmony_ci		dev_err(dev, "Unsupported Manufacturer Model '%s'\n", buf);
7048c2ecf20Sopenharmony_ci		return -ENODEV;
7058c2ecf20Sopenharmony_ci	}
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_REVISION, buf);
7088c2ecf20Sopenharmony_ci	if (ret < 0) {
7098c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to read Manufacturer Revision\n");
7108c2ecf20Sopenharmony_ci		return ret;
7118c2ecf20Sopenharmony_ci	}
7128c2ecf20Sopenharmony_ci	if (ret != 1 || buf[0] != 'F') {
7138c2ecf20Sopenharmony_ci		buf[ret] = '\0';
7148c2ecf20Sopenharmony_ci		dev_err(dev, "Unsupported Manufacturer Revision '%s'\n", buf);
7158c2ecf20Sopenharmony_ci		return -ENODEV;
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	if (client->dev.of_node)
7198c2ecf20Sopenharmony_ci		chip_id = (enum chips)of_device_get_match_data(dev);
7208c2ecf20Sopenharmony_ci	else
7218c2ecf20Sopenharmony_ci		chip_id = i2c_match_id(max20730_id, client)->driver_data;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
7248c2ecf20Sopenharmony_ci	if (!data)
7258c2ecf20Sopenharmony_ci		return -ENOMEM;
7268c2ecf20Sopenharmony_ci	data->id = chip_id;
7278c2ecf20Sopenharmony_ci	mutex_init(&data->lock);
7288c2ecf20Sopenharmony_ci	memcpy(&data->info, &max20730_info[chip_id], sizeof(data->info));
7298c2ecf20Sopenharmony_ci	if (of_property_read_u32_array(client->dev.of_node, "vout-voltage-divider",
7308c2ecf20Sopenharmony_ci				       data->vout_voltage_divider,
7318c2ecf20Sopenharmony_ci				       ARRAY_SIZE(data->vout_voltage_divider)) != 0)
7328c2ecf20Sopenharmony_ci		memset(data->vout_voltage_divider, 0, sizeof(data->vout_voltage_divider));
7338c2ecf20Sopenharmony_ci	if (data->vout_voltage_divider[1] < data->vout_voltage_divider[0]) {
7348c2ecf20Sopenharmony_ci		dev_err(dev,
7358c2ecf20Sopenharmony_ci			"The total resistance of voltage divider is less than output resistance\n");
7368c2ecf20Sopenharmony_ci		return -EINVAL;
7378c2ecf20Sopenharmony_ci	}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET1);
7408c2ecf20Sopenharmony_ci	if (ret < 0)
7418c2ecf20Sopenharmony_ci		return ret;
7428c2ecf20Sopenharmony_ci	data->mfr_devset1 = ret;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	ret = pmbus_do_probe(client, &data->info);
7458c2ecf20Sopenharmony_ci	if (ret < 0)
7468c2ecf20Sopenharmony_ci		return ret;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	ret = max20730_init_debugfs(client, data);
7498c2ecf20Sopenharmony_ci	if (ret)
7508c2ecf20Sopenharmony_ci		dev_warn(dev, "Failed to register debugfs: %d\n",
7518c2ecf20Sopenharmony_ci			 ret);
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	return 0;
7548c2ecf20Sopenharmony_ci}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_cistatic const struct i2c_device_id max20730_id[] = {
7578c2ecf20Sopenharmony_ci	{ "max20710", max20710 },
7588c2ecf20Sopenharmony_ci	{ "max20730", max20730 },
7598c2ecf20Sopenharmony_ci	{ "max20734", max20734 },
7608c2ecf20Sopenharmony_ci	{ "max20743", max20743 },
7618c2ecf20Sopenharmony_ci	{ },
7628c2ecf20Sopenharmony_ci};
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max20730_id);
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_cistatic const struct of_device_id max20730_of_match[] = {
7678c2ecf20Sopenharmony_ci	{ .compatible = "maxim,max20710", .data = (void *)max20710 },
7688c2ecf20Sopenharmony_ci	{ .compatible = "maxim,max20730", .data = (void *)max20730 },
7698c2ecf20Sopenharmony_ci	{ .compatible = "maxim,max20734", .data = (void *)max20734 },
7708c2ecf20Sopenharmony_ci	{ .compatible = "maxim,max20743", .data = (void *)max20743 },
7718c2ecf20Sopenharmony_ci	{ },
7728c2ecf20Sopenharmony_ci};
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, max20730_of_match);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic struct i2c_driver max20730_driver = {
7778c2ecf20Sopenharmony_ci	.driver = {
7788c2ecf20Sopenharmony_ci		.name = "max20730",
7798c2ecf20Sopenharmony_ci		.of_match_table = max20730_of_match,
7808c2ecf20Sopenharmony_ci	},
7818c2ecf20Sopenharmony_ci	.probe_new = max20730_probe,
7828c2ecf20Sopenharmony_ci	.remove = pmbus_do_remove,
7838c2ecf20Sopenharmony_ci	.id_table = max20730_id,
7848c2ecf20Sopenharmony_ci};
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_cimodule_i2c_driver(max20730_driver);
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ciMODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
7898c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PMBus driver for Maxim MAX20710 / MAX20730 / MAX20734 / MAX20743");
7908c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
791