cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

max20730.c (22826B)


      1// SPDX-License-Identifier: GPL-2.0-or-later
      2/*
      3 * Driver for MAX20710, MAX20730, MAX20734, and MAX20743 Integrated,
      4 * Step-Down Switching Regulators
      5 *
      6 * Copyright 2019 Google LLC.
      7 * Copyright 2020 Maxim Integrated
      8 */
      9
     10#include <linux/bits.h>
     11#include <linux/debugfs.h>
     12#include <linux/err.h>
     13#include <linux/i2c.h>
     14#include <linux/init.h>
     15#include <linux/kernel.h>
     16#include <linux/module.h>
     17#include <linux/mutex.h>
     18#include <linux/of_device.h>
     19#include <linux/pmbus.h>
     20#include <linux/util_macros.h>
     21#include "pmbus.h"
     22
     23enum chips {
     24	max20710,
     25	max20730,
     26	max20734,
     27	max20743
     28};
     29
     30enum {
     31	MAX20730_DEBUGFS_VOUT_MIN = 0,
     32	MAX20730_DEBUGFS_FREQUENCY,
     33	MAX20730_DEBUGFS_PG_DELAY,
     34	MAX20730_DEBUGFS_INTERNAL_GAIN,
     35	MAX20730_DEBUGFS_BOOT_VOLTAGE,
     36	MAX20730_DEBUGFS_OUT_V_RAMP_RATE,
     37	MAX20730_DEBUGFS_OC_PROTECT_MODE,
     38	MAX20730_DEBUGFS_SS_TIMING,
     39	MAX20730_DEBUGFS_IMAX,
     40	MAX20730_DEBUGFS_OPERATION,
     41	MAX20730_DEBUGFS_ON_OFF_CONFIG,
     42	MAX20730_DEBUGFS_SMBALERT_MASK,
     43	MAX20730_DEBUGFS_VOUT_MODE,
     44	MAX20730_DEBUGFS_VOUT_COMMAND,
     45	MAX20730_DEBUGFS_VOUT_MAX,
     46	MAX20730_DEBUGFS_NUM_ENTRIES
     47};
     48
     49struct max20730_data {
     50	enum chips id;
     51	struct pmbus_driver_info info;
     52	struct mutex lock;	/* Used to protect against parallel writes */
     53	u16 mfr_devset1;
     54	u16 mfr_devset2;
     55	u16 mfr_voutmin;
     56	u32 vout_voltage_divider[2];
     57};
     58
     59#define to_max20730_data(x)  container_of(x, struct max20730_data, info)
     60
     61#define VOLT_FROM_REG(val)	DIV_ROUND_CLOSEST((val), 1 << 9)
     62
     63#define PMBUS_SMB_ALERT_MASK	0x1B
     64
     65#define MAX20730_MFR_VOUT_MIN	0xd1
     66#define MAX20730_MFR_DEVSET1	0xd2
     67#define MAX20730_MFR_DEVSET2	0xd3
     68
     69#define MAX20730_MFR_VOUT_MIN_MASK		GENMASK(9, 0)
     70#define MAX20730_MFR_VOUT_MIN_BIT_POS		0
     71
     72#define MAX20730_MFR_DEVSET1_RGAIN_MASK		(BIT(13) | BIT(14))
     73#define MAX20730_MFR_DEVSET1_OTP_MASK		(BIT(11) | BIT(12))
     74#define MAX20730_MFR_DEVSET1_VBOOT_MASK		(BIT(8) | BIT(9))
     75#define MAX20730_MFR_DEVSET1_OCP_MASK		(BIT(5) | BIT(6))
     76#define MAX20730_MFR_DEVSET1_FSW_MASK		GENMASK(4, 2)
     77#define MAX20730_MFR_DEVSET1_TSTAT_MASK		(BIT(0) | BIT(1))
     78
     79#define MAX20730_MFR_DEVSET1_RGAIN_BIT_POS	13
     80#define MAX20730_MFR_DEVSET1_OTP_BIT_POS	11
     81#define MAX20730_MFR_DEVSET1_VBOOT_BIT_POS	8
     82#define MAX20730_MFR_DEVSET1_OCP_BIT_POS	5
     83#define MAX20730_MFR_DEVSET1_FSW_BIT_POS	2
     84#define MAX20730_MFR_DEVSET1_TSTAT_BIT_POS	0
     85
     86#define MAX20730_MFR_DEVSET2_IMAX_MASK		GENMASK(10, 8)
     87#define MAX20730_MFR_DEVSET2_VRATE		(BIT(6) | BIT(7))
     88#define MAX20730_MFR_DEVSET2_OCPM_MASK		BIT(5)
     89#define MAX20730_MFR_DEVSET2_SS_MASK		(BIT(0) | BIT(1))
     90
     91#define MAX20730_MFR_DEVSET2_IMAX_BIT_POS	8
     92#define MAX20730_MFR_DEVSET2_VRATE_BIT_POS	6
     93#define MAX20730_MFR_DEVSET2_OCPM_BIT_POS	5
     94#define MAX20730_MFR_DEVSET2_SS_BIT_POS		0
     95
     96#define DEBUG_FS_DATA_MAX			16
     97
     98struct max20730_debugfs_data {
     99	struct i2c_client *client;
    100	int debugfs_entries[MAX20730_DEBUGFS_NUM_ENTRIES];
    101};
    102
    103#define to_psu(x, y) container_of((x), \
    104			struct max20730_debugfs_data, debugfs_entries[(y)])
    105
    106#ifdef CONFIG_DEBUG_FS
    107static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
    108				     size_t count, loff_t *ppos)
    109{
    110	int ret, len;
    111	int *idxp = file->private_data;
    112	int idx = *idxp;
    113	struct max20730_debugfs_data *psu = to_psu(idxp, idx);
    114	const struct pmbus_driver_info *info;
    115	const struct max20730_data *data;
    116	char tbuf[DEBUG_FS_DATA_MAX] = { 0 };
    117	u16 val;
    118
    119	info = pmbus_get_driver_info(psu->client);
    120	data = to_max20730_data(info);
    121
    122	switch (idx) {
    123	case MAX20730_DEBUGFS_VOUT_MIN:
    124		ret = VOLT_FROM_REG(data->mfr_voutmin * 10000);
    125		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d.%d\n",
    126				ret / 10000, ret % 10000);
    127		break;
    128	case MAX20730_DEBUGFS_FREQUENCY:
    129		val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_FSW_MASK)
    130			>> MAX20730_MFR_DEVSET1_FSW_BIT_POS;
    131
    132		if (val == 0)
    133			ret = 400;
    134		else if (val == 1)
    135			ret = 500;
    136		else if (val == 2 || val == 3)
    137			ret = 600;
    138		else if (val == 4)
    139			ret = 700;
    140		else if (val == 5)
    141			ret = 800;
    142		else
    143			ret = 900;
    144		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
    145		break;
    146	case MAX20730_DEBUGFS_PG_DELAY:
    147		val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_TSTAT_MASK)
    148			>> MAX20730_MFR_DEVSET1_TSTAT_BIT_POS;
    149
    150		if (val == 0)
    151			len = strlcpy(tbuf, "2000\n", DEBUG_FS_DATA_MAX);
    152		else if (val == 1)
    153			len = strlcpy(tbuf, "125\n", DEBUG_FS_DATA_MAX);
    154		else if (val == 2)
    155			len = strlcpy(tbuf, "62.5\n", DEBUG_FS_DATA_MAX);
    156		else
    157			len = strlcpy(tbuf, "32\n", DEBUG_FS_DATA_MAX);
    158		break;
    159	case MAX20730_DEBUGFS_INTERNAL_GAIN:
    160		val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_RGAIN_MASK)
    161			>> MAX20730_MFR_DEVSET1_RGAIN_BIT_POS;
    162
    163		if (data->id == max20734) {
    164			/* AN6209 */
    165			if (val == 0)
    166				len = strlcpy(tbuf, "0.8\n", DEBUG_FS_DATA_MAX);
    167			else if (val == 1)
    168				len = strlcpy(tbuf, "3.2\n", DEBUG_FS_DATA_MAX);
    169			else if (val == 2)
    170				len = strlcpy(tbuf, "1.6\n", DEBUG_FS_DATA_MAX);
    171			else
    172				len = strlcpy(tbuf, "6.4\n", DEBUG_FS_DATA_MAX);
    173		} else if (data->id == max20730 || data->id == max20710) {
    174			/* AN6042 or AN6140 */
    175			if (val == 0)
    176				len = strlcpy(tbuf, "0.9\n", DEBUG_FS_DATA_MAX);
    177			else if (val == 1)
    178				len = strlcpy(tbuf, "3.6\n", DEBUG_FS_DATA_MAX);
    179			else if (val == 2)
    180				len = strlcpy(tbuf, "1.8\n", DEBUG_FS_DATA_MAX);
    181			else
    182				len = strlcpy(tbuf, "7.2\n", DEBUG_FS_DATA_MAX);
    183		} else if (data->id == max20743) {
    184			/* AN6042 */
    185			if (val == 0)
    186				len = strlcpy(tbuf, "0.45\n", DEBUG_FS_DATA_MAX);
    187			else if (val == 1)
    188				len = strlcpy(tbuf, "1.8\n", DEBUG_FS_DATA_MAX);
    189			else if (val == 2)
    190				len = strlcpy(tbuf, "0.9\n", DEBUG_FS_DATA_MAX);
    191			else
    192				len = strlcpy(tbuf, "3.6\n", DEBUG_FS_DATA_MAX);
    193		} else {
    194			len = strlcpy(tbuf, "Not supported\n", DEBUG_FS_DATA_MAX);
    195		}
    196		break;
    197	case MAX20730_DEBUGFS_BOOT_VOLTAGE:
    198		val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_VBOOT_MASK)
    199			>> MAX20730_MFR_DEVSET1_VBOOT_BIT_POS;
    200
    201		if (val == 0)
    202			len = strlcpy(tbuf, "0.6484\n", DEBUG_FS_DATA_MAX);
    203		else if (val == 1)
    204			len = strlcpy(tbuf, "0.8984\n", DEBUG_FS_DATA_MAX);
    205		else if (val == 2)
    206			len = strlcpy(tbuf, "1.0\n", DEBUG_FS_DATA_MAX);
    207		else
    208			len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
    209		break;
    210	case MAX20730_DEBUGFS_OUT_V_RAMP_RATE:
    211		val = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_VRATE)
    212			>> MAX20730_MFR_DEVSET2_VRATE_BIT_POS;
    213
    214		if (val == 0)
    215			len = strlcpy(tbuf, "4\n", DEBUG_FS_DATA_MAX);
    216		else if (val == 1)
    217			len = strlcpy(tbuf, "2\n", DEBUG_FS_DATA_MAX);
    218		else if (val == 2)
    219			len = strlcpy(tbuf, "1\n", DEBUG_FS_DATA_MAX);
    220		else
    221			len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
    222		break;
    223	case MAX20730_DEBUGFS_OC_PROTECT_MODE:
    224		ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_OCPM_MASK)
    225			>> MAX20730_MFR_DEVSET2_OCPM_BIT_POS;
    226		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
    227		break;
    228	case MAX20730_DEBUGFS_SS_TIMING:
    229		val = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_SS_MASK)
    230			>> MAX20730_MFR_DEVSET2_SS_BIT_POS;
    231
    232		if (val == 0)
    233			len = strlcpy(tbuf, "0.75\n", DEBUG_FS_DATA_MAX);
    234		else if (val == 1)
    235			len = strlcpy(tbuf, "1.5\n", DEBUG_FS_DATA_MAX);
    236		else if (val == 2)
    237			len = strlcpy(tbuf, "3\n", DEBUG_FS_DATA_MAX);
    238		else
    239			len = strlcpy(tbuf, "6\n", DEBUG_FS_DATA_MAX);
    240		break;
    241	case MAX20730_DEBUGFS_IMAX:
    242		ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_IMAX_MASK)
    243			>> MAX20730_MFR_DEVSET2_IMAX_BIT_POS;
    244		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
    245		break;
    246	case MAX20730_DEBUGFS_OPERATION:
    247		ret = i2c_smbus_read_byte_data(psu->client, PMBUS_OPERATION);
    248		if (ret < 0)
    249			return ret;
    250		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
    251		break;
    252	case MAX20730_DEBUGFS_ON_OFF_CONFIG:
    253		ret = i2c_smbus_read_byte_data(psu->client, PMBUS_ON_OFF_CONFIG);
    254		if (ret < 0)
    255			return ret;
    256		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
    257		break;
    258	case MAX20730_DEBUGFS_SMBALERT_MASK:
    259		ret = i2c_smbus_read_word_data(psu->client,
    260					       PMBUS_SMB_ALERT_MASK);
    261		if (ret < 0)
    262			return ret;
    263		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
    264		break;
    265	case MAX20730_DEBUGFS_VOUT_MODE:
    266		ret = i2c_smbus_read_byte_data(psu->client, PMBUS_VOUT_MODE);
    267		if (ret < 0)
    268			return ret;
    269		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
    270		break;
    271	case MAX20730_DEBUGFS_VOUT_COMMAND:
    272		ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_COMMAND);
    273		if (ret < 0)
    274			return ret;
    275
    276		ret = VOLT_FROM_REG(ret * 10000);
    277		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
    278				"%d.%d\n", ret / 10000, ret % 10000);
    279		break;
    280	case MAX20730_DEBUGFS_VOUT_MAX:
    281		ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_MAX);
    282		if (ret < 0)
    283			return ret;
    284
    285		ret = VOLT_FROM_REG(ret * 10000);
    286		len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
    287				"%d.%d\n", ret / 10000, ret % 10000);
    288		break;
    289	default:
    290		len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
    291	}
    292
    293	return simple_read_from_buffer(buf, count, ppos, tbuf, len);
    294}
    295
    296static const struct file_operations max20730_fops = {
    297	.llseek = noop_llseek,
    298	.read = max20730_debugfs_read,
    299	.write = NULL,
    300	.open = simple_open,
    301};
    302
    303static int max20730_init_debugfs(struct i2c_client *client,
    304				 struct max20730_data *data)
    305{
    306	int ret, i;
    307	struct dentry *debugfs;
    308	struct dentry *max20730_dir;
    309	struct max20730_debugfs_data *psu;
    310
    311	ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET2);
    312	if (ret < 0)
    313		return ret;
    314	data->mfr_devset2 = ret;
    315
    316	ret = i2c_smbus_read_word_data(client, MAX20730_MFR_VOUT_MIN);
    317	if (ret < 0)
    318		return ret;
    319	data->mfr_voutmin = ret;
    320
    321	psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
    322	if (!psu)
    323		return -ENOMEM;
    324	psu->client = client;
    325
    326	debugfs = pmbus_get_debugfs_dir(client);
    327	if (!debugfs)
    328		return -ENOENT;
    329
    330	max20730_dir = debugfs_create_dir(client->name, debugfs);
    331
    332	for (i = 0; i < MAX20730_DEBUGFS_NUM_ENTRIES; ++i)
    333		psu->debugfs_entries[i] = i;
    334
    335	debugfs_create_file("vout_min", 0444, max20730_dir,
    336			    &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MIN],
    337			    &max20730_fops);
    338	debugfs_create_file("frequency", 0444, max20730_dir,
    339			    &psu->debugfs_entries[MAX20730_DEBUGFS_FREQUENCY],
    340			    &max20730_fops);
    341	debugfs_create_file("power_good_delay", 0444, max20730_dir,
    342			    &psu->debugfs_entries[MAX20730_DEBUGFS_PG_DELAY],
    343			    &max20730_fops);
    344	debugfs_create_file("internal_gain", 0444, max20730_dir,
    345			    &psu->debugfs_entries[MAX20730_DEBUGFS_INTERNAL_GAIN],
    346			    &max20730_fops);
    347	debugfs_create_file("boot_voltage", 0444, max20730_dir,
    348			    &psu->debugfs_entries[MAX20730_DEBUGFS_BOOT_VOLTAGE],
    349			    &max20730_fops);
    350	debugfs_create_file("out_voltage_ramp_rate", 0444, max20730_dir,
    351			    &psu->debugfs_entries[MAX20730_DEBUGFS_OUT_V_RAMP_RATE],
    352			    &max20730_fops);
    353	debugfs_create_file("oc_protection_mode", 0444, max20730_dir,
    354			    &psu->debugfs_entries[MAX20730_DEBUGFS_OC_PROTECT_MODE],
    355			    &max20730_fops);
    356	debugfs_create_file("soft_start_timing", 0444, max20730_dir,
    357			    &psu->debugfs_entries[MAX20730_DEBUGFS_SS_TIMING],
    358			    &max20730_fops);
    359	debugfs_create_file("imax", 0444, max20730_dir,
    360			    &psu->debugfs_entries[MAX20730_DEBUGFS_IMAX],
    361			    &max20730_fops);
    362	debugfs_create_file("operation", 0444, max20730_dir,
    363			    &psu->debugfs_entries[MAX20730_DEBUGFS_OPERATION],
    364			    &max20730_fops);
    365	debugfs_create_file("on_off_config", 0444, max20730_dir,
    366			    &psu->debugfs_entries[MAX20730_DEBUGFS_ON_OFF_CONFIG],
    367			    &max20730_fops);
    368	debugfs_create_file("smbalert_mask", 0444, max20730_dir,
    369			    &psu->debugfs_entries[MAX20730_DEBUGFS_SMBALERT_MASK],
    370			    &max20730_fops);
    371	debugfs_create_file("vout_mode", 0444, max20730_dir,
    372			    &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MODE],
    373			    &max20730_fops);
    374	debugfs_create_file("vout_command", 0444, max20730_dir,
    375			    &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_COMMAND],
    376			    &max20730_fops);
    377	debugfs_create_file("vout_max", 0444, max20730_dir,
    378			    &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MAX],
    379			    &max20730_fops);
    380
    381	return 0;
    382}
    383#else
    384static int max20730_init_debugfs(struct i2c_client *client,
    385				 struct max20730_data *data)
    386{
    387	return 0;
    388}
    389#endif /* CONFIG_DEBUG_FS */
    390
    391static const struct i2c_device_id max20730_id[];
    392
    393/*
    394 * Convert discreet value to direct data format. Strictly speaking, all passed
    395 * values are constants, so we could do that calculation manually. On the
    396 * downside, that would make the driver more difficult to maintain, so lets
    397 * use this approach.
    398 */
    399static u16 val_to_direct(int v, enum pmbus_sensor_classes class,
    400			 const struct pmbus_driver_info *info)
    401{
    402	int R = info->R[class] - 3;	/* take milli-units into account */
    403	int b = info->b[class] * 1000;
    404	long d;
    405
    406	d = v * info->m[class] + b;
    407	/*
    408	 * R < 0 is true for all callers, so we don't need to bother
    409	 * about the R > 0 case.
    410	 */
    411	while (R < 0) {
    412		d = DIV_ROUND_CLOSEST(d, 10);
    413		R++;
    414	}
    415	return (u16)d;
    416}
    417
    418static long direct_to_val(u16 w, enum pmbus_sensor_classes class,
    419			  const struct pmbus_driver_info *info)
    420{
    421	int R = info->R[class] - 3;
    422	int b = info->b[class] * 1000;
    423	int m = info->m[class];
    424	long d = (s16)w;
    425
    426	if (m == 0)
    427		return 0;
    428
    429	while (R < 0) {
    430		d *= 10;
    431		R++;
    432	}
    433	d = (d - b) / m;
    434	return d;
    435}
    436
    437static u32 max_current[][5] = {
    438	[max20710] = { 6200, 8000, 9700, 11600 },
    439	[max20730] = { 13000, 16600, 20100, 23600 },
    440	[max20734] = { 21000, 27000, 32000, 38000 },
    441	[max20743] = { 18900, 24100, 29200, 34100 },
    442};
    443
    444static int max20730_read_word_data(struct i2c_client *client, int page,
    445				   int phase, int reg)
    446{
    447	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
    448	const struct max20730_data *data = to_max20730_data(info);
    449	int ret = 0;
    450	u32 max_c;
    451
    452	switch (reg) {
    453	case PMBUS_OT_FAULT_LIMIT:
    454		switch ((data->mfr_devset1 >> 11) & 0x3) {
    455		case 0x0:
    456			ret = val_to_direct(150000, PSC_TEMPERATURE, info);
    457			break;
    458		case 0x1:
    459			ret = val_to_direct(130000, PSC_TEMPERATURE, info);
    460			break;
    461		default:
    462			ret = -ENODATA;
    463			break;
    464		}
    465		break;
    466	case PMBUS_IOUT_OC_FAULT_LIMIT:
    467		max_c = max_current[data->id][(data->mfr_devset1 >> 5) & 0x3];
    468		ret = val_to_direct(max_c, PSC_CURRENT_OUT, info);
    469		break;
    470	case PMBUS_READ_VOUT:
    471		ret = pmbus_read_word_data(client, page, phase, reg);
    472		if (ret > 0 && data->vout_voltage_divider[0] && data->vout_voltage_divider[1]) {
    473			u64 temp = DIV_ROUND_CLOSEST_ULL((u64)ret * data->vout_voltage_divider[1],
    474							 data->vout_voltage_divider[0]);
    475			ret = clamp_val(temp, 0, 0xffff);
    476		}
    477		break;
    478	default:
    479		ret = -ENODATA;
    480		break;
    481	}
    482	return ret;
    483}
    484
    485static int max20730_write_word_data(struct i2c_client *client, int page,
    486				    int reg, u16 word)
    487{
    488	struct pmbus_driver_info *info;
    489	struct max20730_data *data;
    490	u16 devset1;
    491	int ret = 0;
    492	int idx;
    493
    494	info = (struct pmbus_driver_info *)pmbus_get_driver_info(client);
    495	data = to_max20730_data(info);
    496
    497	mutex_lock(&data->lock);
    498	devset1 = data->mfr_devset1;
    499
    500	switch (reg) {
    501	case PMBUS_OT_FAULT_LIMIT:
    502		devset1 &= ~(BIT(11) | BIT(12));
    503		if (direct_to_val(word, PSC_TEMPERATURE, info) < 140000)
    504			devset1 |= BIT(11);
    505		break;
    506	case PMBUS_IOUT_OC_FAULT_LIMIT:
    507		devset1 &= ~(BIT(5) | BIT(6));
    508
    509		idx = find_closest(direct_to_val(word, PSC_CURRENT_OUT, info),
    510				   max_current[data->id], 4);
    511		devset1 |= (idx << 5);
    512		break;
    513	default:
    514		ret = -ENODATA;
    515		break;
    516	}
    517
    518	if (!ret && devset1 != data->mfr_devset1) {
    519		ret = i2c_smbus_write_word_data(client, MAX20730_MFR_DEVSET1,
    520						devset1);
    521		if (!ret) {
    522			data->mfr_devset1 = devset1;
    523			pmbus_clear_cache(client);
    524		}
    525	}
    526	mutex_unlock(&data->lock);
    527	return ret;
    528}
    529
    530static const struct pmbus_driver_info max20730_info[] = {
    531	[max20710] = {
    532		.pages = 1,
    533		.read_word_data = max20730_read_word_data,
    534		.write_word_data = max20730_write_word_data,
    535
    536		/* Source : Maxim AN6140 and AN6042 */
    537		.format[PSC_TEMPERATURE] = direct,
    538		.m[PSC_TEMPERATURE] = 21,
    539		.b[PSC_TEMPERATURE] = 5887,
    540		.R[PSC_TEMPERATURE] = -1,
    541
    542		.format[PSC_VOLTAGE_IN] = direct,
    543		.m[PSC_VOLTAGE_IN] = 3609,
    544		.b[PSC_VOLTAGE_IN] = 0,
    545		.R[PSC_VOLTAGE_IN] = -2,
    546
    547		.format[PSC_CURRENT_OUT] = direct,
    548		.m[PSC_CURRENT_OUT] = 153,
    549		.b[PSC_CURRENT_OUT] = 4976,
    550		.R[PSC_CURRENT_OUT] = -1,
    551
    552		.format[PSC_VOLTAGE_OUT] = linear,
    553
    554		.func[0] = PMBUS_HAVE_VIN |
    555			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
    556			PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
    557			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
    558			PMBUS_HAVE_STATUS_INPUT,
    559	},
    560	[max20730] = {
    561		.pages = 1,
    562		.read_word_data = max20730_read_word_data,
    563		.write_word_data = max20730_write_word_data,
    564
    565		/* Source : Maxim AN6042 */
    566		.format[PSC_TEMPERATURE] = direct,
    567		.m[PSC_TEMPERATURE] = 21,
    568		.b[PSC_TEMPERATURE] = 5887,
    569		.R[PSC_TEMPERATURE] = -1,
    570
    571		.format[PSC_VOLTAGE_IN] = direct,
    572		.m[PSC_VOLTAGE_IN] = 3609,
    573		.b[PSC_VOLTAGE_IN] = 0,
    574		.R[PSC_VOLTAGE_IN] = -2,
    575
    576		/*
    577		 * Values in the datasheet are adjusted for temperature and
    578		 * for the relationship between Vin and Vout.
    579		 * Unfortunately, the data sheet suggests that Vout measurement
    580		 * may be scaled with a resistor array. This is indeed the case
    581		 * at least on the evaulation boards. As a result, any in-driver
    582		 * adjustments would either be wrong or require elaborate means
    583		 * to configure the scaling. Instead of doing that, just report
    584		 * raw values and let userspace handle adjustments.
    585		 */
    586		.format[PSC_CURRENT_OUT] = direct,
    587		.m[PSC_CURRENT_OUT] = 153,
    588		.b[PSC_CURRENT_OUT] = 4976,
    589		.R[PSC_CURRENT_OUT] = -1,
    590
    591		.format[PSC_VOLTAGE_OUT] = linear,
    592
    593		.func[0] = PMBUS_HAVE_VIN |
    594			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
    595			PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
    596			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
    597			PMBUS_HAVE_STATUS_INPUT,
    598	},
    599	[max20734] = {
    600		.pages = 1,
    601		.read_word_data = max20730_read_word_data,
    602		.write_word_data = max20730_write_word_data,
    603
    604		/* Source : Maxim AN6209 */
    605		.format[PSC_TEMPERATURE] = direct,
    606		.m[PSC_TEMPERATURE] = 21,
    607		.b[PSC_TEMPERATURE] = 5887,
    608		.R[PSC_TEMPERATURE] = -1,
    609
    610		.format[PSC_VOLTAGE_IN] = direct,
    611		.m[PSC_VOLTAGE_IN] = 3592,
    612		.b[PSC_VOLTAGE_IN] = 0,
    613		.R[PSC_VOLTAGE_IN] = -2,
    614
    615		.format[PSC_CURRENT_OUT] = direct,
    616		.m[PSC_CURRENT_OUT] = 111,
    617		.b[PSC_CURRENT_OUT] = 3461,
    618		.R[PSC_CURRENT_OUT] = -1,
    619
    620		.format[PSC_VOLTAGE_OUT] = linear,
    621
    622		.func[0] = PMBUS_HAVE_VIN |
    623			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
    624			PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
    625			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
    626			PMBUS_HAVE_STATUS_INPUT,
    627	},
    628	[max20743] = {
    629		.pages = 1,
    630		.read_word_data = max20730_read_word_data,
    631		.write_word_data = max20730_write_word_data,
    632
    633		/* Source : Maxim AN6042 */
    634		.format[PSC_TEMPERATURE] = direct,
    635		.m[PSC_TEMPERATURE] = 21,
    636		.b[PSC_TEMPERATURE] = 5887,
    637		.R[PSC_TEMPERATURE] = -1,
    638
    639		.format[PSC_VOLTAGE_IN] = direct,
    640		.m[PSC_VOLTAGE_IN] = 3597,
    641		.b[PSC_VOLTAGE_IN] = 0,
    642		.R[PSC_VOLTAGE_IN] = -2,
    643
    644		.format[PSC_CURRENT_OUT] = direct,
    645		.m[PSC_CURRENT_OUT] = 95,
    646		.b[PSC_CURRENT_OUT] = 5014,
    647		.R[PSC_CURRENT_OUT] = -1,
    648
    649		.format[PSC_VOLTAGE_OUT] = linear,
    650
    651		.func[0] = PMBUS_HAVE_VIN |
    652			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
    653			PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
    654			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
    655			PMBUS_HAVE_STATUS_INPUT,
    656	},
    657};
    658
    659static int max20730_probe(struct i2c_client *client)
    660{
    661	struct device *dev = &client->dev;
    662	u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
    663	struct max20730_data *data;
    664	enum chips chip_id;
    665	int ret;
    666
    667	if (!i2c_check_functionality(client->adapter,
    668				     I2C_FUNC_SMBUS_READ_BYTE_DATA |
    669				     I2C_FUNC_SMBUS_READ_WORD_DATA |
    670				     I2C_FUNC_SMBUS_BLOCK_DATA))
    671		return -ENODEV;
    672
    673	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
    674	if (ret < 0) {
    675		dev_err(&client->dev, "Failed to read Manufacturer ID\n");
    676		return ret;
    677	}
    678	if (ret != 5 || strncmp(buf, "MAXIM", 5)) {
    679		buf[ret] = '\0';
    680		dev_err(dev, "Unsupported Manufacturer ID '%s'\n", buf);
    681		return -ENODEV;
    682	}
    683
    684	/*
    685	 * The chips support reading PMBUS_MFR_MODEL. On both MAX20730
    686	 * and MAX20734, reading it returns M20743. Presumably that is
    687	 * the reason why the command is not documented. Unfortunately,
    688	 * that means that there is no reliable means to detect the chip.
    689	 * However, we can at least detect the chip series. Compare
    690	 * the returned value against 'M20743' and bail out if there is
    691	 * a mismatch. If that doesn't work for all chips, we may have
    692	 * to remove this check.
    693	 */
    694	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
    695	if (ret < 0) {
    696		dev_err(dev, "Failed to read Manufacturer Model\n");
    697		return ret;
    698	}
    699	if (ret != 6 || strncmp(buf, "M20743", 6)) {
    700		buf[ret] = '\0';
    701		dev_err(dev, "Unsupported Manufacturer Model '%s'\n", buf);
    702		return -ENODEV;
    703	}
    704
    705	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_REVISION, buf);
    706	if (ret < 0) {
    707		dev_err(dev, "Failed to read Manufacturer Revision\n");
    708		return ret;
    709	}
    710	if (ret != 1 || buf[0] != 'F') {
    711		buf[ret] = '\0';
    712		dev_err(dev, "Unsupported Manufacturer Revision '%s'\n", buf);
    713		return -ENODEV;
    714	}
    715
    716	if (client->dev.of_node)
    717		chip_id = (enum chips)of_device_get_match_data(dev);
    718	else
    719		chip_id = i2c_match_id(max20730_id, client)->driver_data;
    720
    721	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
    722	if (!data)
    723		return -ENOMEM;
    724	data->id = chip_id;
    725	mutex_init(&data->lock);
    726	memcpy(&data->info, &max20730_info[chip_id], sizeof(data->info));
    727	if (of_property_read_u32_array(client->dev.of_node, "vout-voltage-divider",
    728				       data->vout_voltage_divider,
    729				       ARRAY_SIZE(data->vout_voltage_divider)) != 0)
    730		memset(data->vout_voltage_divider, 0, sizeof(data->vout_voltage_divider));
    731	if (data->vout_voltage_divider[1] < data->vout_voltage_divider[0]) {
    732		dev_err(dev,
    733			"The total resistance of voltage divider is less than output resistance\n");
    734		return -EINVAL;
    735	}
    736
    737	ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET1);
    738	if (ret < 0)
    739		return ret;
    740	data->mfr_devset1 = ret;
    741
    742	ret = pmbus_do_probe(client, &data->info);
    743	if (ret < 0)
    744		return ret;
    745
    746	ret = max20730_init_debugfs(client, data);
    747	if (ret)
    748		dev_warn(dev, "Failed to register debugfs: %d\n",
    749			 ret);
    750
    751	return 0;
    752}
    753
    754static const struct i2c_device_id max20730_id[] = {
    755	{ "max20710", max20710 },
    756	{ "max20730", max20730 },
    757	{ "max20734", max20734 },
    758	{ "max20743", max20743 },
    759	{ },
    760};
    761
    762MODULE_DEVICE_TABLE(i2c, max20730_id);
    763
    764static const struct of_device_id max20730_of_match[] = {
    765	{ .compatible = "maxim,max20710", .data = (void *)max20710 },
    766	{ .compatible = "maxim,max20730", .data = (void *)max20730 },
    767	{ .compatible = "maxim,max20734", .data = (void *)max20734 },
    768	{ .compatible = "maxim,max20743", .data = (void *)max20743 },
    769	{ },
    770};
    771
    772MODULE_DEVICE_TABLE(of, max20730_of_match);
    773
    774static struct i2c_driver max20730_driver = {
    775	.driver = {
    776		.name = "max20730",
    777		.of_match_table = max20730_of_match,
    778	},
    779	.probe_new = max20730_probe,
    780	.id_table = max20730_id,
    781};
    782
    783module_i2c_driver(max20730_driver);
    784
    785MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
    786MODULE_DESCRIPTION("PMBus driver for Maxim MAX20710 / MAX20730 / MAX20734 / MAX20743");
    787MODULE_LICENSE("GPL");
    788MODULE_IMPORT_NS(PMBUS);