xref: /linux/drivers/hwmon/pmbus/ltc4286.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
10c459759SDelphine CC Chiu // SPDX-License-Identifier: GPL-2.0-or-later
20c459759SDelphine CC Chiu 
30c459759SDelphine CC Chiu #include <linux/err.h>
40c459759SDelphine CC Chiu #include <linux/i2c.h>
50c459759SDelphine CC Chiu #include <linux/init.h>
60c459759SDelphine CC Chiu #include <linux/kernel.h>
70c459759SDelphine CC Chiu #include <linux/module.h>
80c459759SDelphine CC Chiu #include <linux/pmbus.h>
90c459759SDelphine CC Chiu #include "pmbus.h"
100c459759SDelphine CC Chiu 
110c459759SDelphine CC Chiu /* LTC4286 register */
120c459759SDelphine CC Chiu #define LTC4286_MFR_CONFIG1	0xF2
130c459759SDelphine CC Chiu 
140c459759SDelphine CC Chiu /* LTC4286 configuration */
150c459759SDelphine CC Chiu #define VRANGE_SELECT_BIT	BIT(1)
160c459759SDelphine CC Chiu 
170c459759SDelphine CC Chiu #define LTC4286_MFR_ID_SIZE	3
180c459759SDelphine CC Chiu 
190c459759SDelphine CC Chiu /*
200c459759SDelphine CC Chiu  * Initialize the MBR as default settings which is referred to LTC4286 datasheet
210c459759SDelphine CC Chiu  * (March 22, 2022 version) table 3 page 16
220c459759SDelphine CC Chiu  */
230c459759SDelphine CC Chiu static struct pmbus_driver_info ltc4286_info = {
240c459759SDelphine CC Chiu 	.pages = 1,
250c459759SDelphine CC Chiu 	.format[PSC_VOLTAGE_IN] = direct,
260c459759SDelphine CC Chiu 	.format[PSC_VOLTAGE_OUT] = direct,
270c459759SDelphine CC Chiu 	.format[PSC_CURRENT_OUT] = direct,
280c459759SDelphine CC Chiu 	.format[PSC_POWER] = direct,
290c459759SDelphine CC Chiu 	.format[PSC_TEMPERATURE] = direct,
300c459759SDelphine CC Chiu 	.m[PSC_VOLTAGE_IN] = 32,
310c459759SDelphine CC Chiu 	.b[PSC_VOLTAGE_IN] = 0,
320c459759SDelphine CC Chiu 	.R[PSC_VOLTAGE_IN] = 1,
330c459759SDelphine CC Chiu 	.m[PSC_VOLTAGE_OUT] = 32,
340c459759SDelphine CC Chiu 	.b[PSC_VOLTAGE_OUT] = 0,
350c459759SDelphine CC Chiu 	.R[PSC_VOLTAGE_OUT] = 1,
360c459759SDelphine CC Chiu 	.m[PSC_CURRENT_OUT] = 1024,
370c459759SDelphine CC Chiu 	.b[PSC_CURRENT_OUT] = 0,
380c459759SDelphine CC Chiu 	/*
390c459759SDelphine CC Chiu 	 * The rsense value used in MBR formula in LTC4286 datasheet should be ohm unit.
400c459759SDelphine CC Chiu 	 * However, the rsense value that user input is micro ohm.
410c459759SDelphine CC Chiu 	 * Thus, the MBR setting which involves rsense should be shifted by 6 digits.
420c459759SDelphine CC Chiu 	 */
430c459759SDelphine CC Chiu 	.R[PSC_CURRENT_OUT] = 3 - 6,
440c459759SDelphine CC Chiu 	.m[PSC_POWER] = 1,
450c459759SDelphine CC Chiu 	.b[PSC_POWER] = 0,
460c459759SDelphine CC Chiu 	/*
470c459759SDelphine CC Chiu 	 * The rsense value used in MBR formula in LTC4286 datasheet should be ohm unit.
480c459759SDelphine CC Chiu 	 * However, the rsense value that user input is micro ohm.
490c459759SDelphine CC Chiu 	 * Thus, the MBR setting which involves rsense should be shifted by 6 digits.
500c459759SDelphine CC Chiu 	 */
510c459759SDelphine CC Chiu 	.R[PSC_POWER] = 4 - 6,
520c459759SDelphine CC Chiu 	.m[PSC_TEMPERATURE] = 1,
530c459759SDelphine CC Chiu 	.b[PSC_TEMPERATURE] = 273,
540c459759SDelphine CC Chiu 	.R[PSC_TEMPERATURE] = 0,
550c459759SDelphine CC Chiu 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
560c459759SDelphine CC Chiu 		   PMBUS_HAVE_PIN | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_VOUT |
570c459759SDelphine CC Chiu 		   PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_STATUS_TEMP,
580c459759SDelphine CC Chiu };
590c459759SDelphine CC Chiu 
600c459759SDelphine CC Chiu static const struct i2c_device_id ltc4286_id[] = {
61*04e3bb0bSUwe Kleine-König 	{ "ltc4286", },
62*04e3bb0bSUwe Kleine-König 	{ "ltc4287", },
630c459759SDelphine CC Chiu 	{}
640c459759SDelphine CC Chiu };
650c459759SDelphine CC Chiu MODULE_DEVICE_TABLE(i2c, ltc4286_id);
660c459759SDelphine CC Chiu 
ltc4286_probe(struct i2c_client * client)670c459759SDelphine CC Chiu static int ltc4286_probe(struct i2c_client *client)
680c459759SDelphine CC Chiu {
690c459759SDelphine CC Chiu 	int ret;
700c459759SDelphine CC Chiu 	const struct i2c_device_id *mid;
710c459759SDelphine CC Chiu 	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
720c459759SDelphine CC Chiu 	struct pmbus_driver_info *info;
730c459759SDelphine CC Chiu 	u32 rsense;
740c459759SDelphine CC Chiu 	int vrange_nval, vrange_oval;
750c459759SDelphine CC Chiu 
760c459759SDelphine CC Chiu 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
770c459759SDelphine CC Chiu 	if (ret < 0) {
780c459759SDelphine CC Chiu 		return dev_err_probe(&client->dev, ret,
790c459759SDelphine CC Chiu 				     "Failed to read manufacturer id\n");
800c459759SDelphine CC Chiu 	}
810c459759SDelphine CC Chiu 
820c459759SDelphine CC Chiu 	/*
830c459759SDelphine CC Chiu 	 * Refer to ltc4286 datasheet page 20
840c459759SDelphine CC Chiu 	 * the manufacturer id is LTC
850c459759SDelphine CC Chiu 	 */
860c459759SDelphine CC Chiu 	if (ret != LTC4286_MFR_ID_SIZE ||
870c459759SDelphine CC Chiu 	    strncmp(block_buffer, "LTC", LTC4286_MFR_ID_SIZE)) {
880c459759SDelphine CC Chiu 		return dev_err_probe(&client->dev, -ENODEV,
890c459759SDelphine CC Chiu 				     "Manufacturer id mismatch\n");
900c459759SDelphine CC Chiu 	}
910c459759SDelphine CC Chiu 
920c459759SDelphine CC Chiu 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
930c459759SDelphine CC Chiu 	if (ret < 0) {
940c459759SDelphine CC Chiu 		return dev_err_probe(&client->dev, ret,
950c459759SDelphine CC Chiu 				     "Failed to read manufacturer model\n");
960c459759SDelphine CC Chiu 	}
970c459759SDelphine CC Chiu 
980c459759SDelphine CC Chiu 	for (mid = ltc4286_id; mid->name[0]; mid++) {
990c459759SDelphine CC Chiu 		if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
1000c459759SDelphine CC Chiu 			break;
1010c459759SDelphine CC Chiu 	}
1020c459759SDelphine CC Chiu 	if (!mid->name[0])
1030c459759SDelphine CC Chiu 		return dev_err_probe(&client->dev, -ENODEV,
1040c459759SDelphine CC Chiu 				     "Unsupported device\n");
1050c459759SDelphine CC Chiu 
1060c459759SDelphine CC Chiu 	if (of_property_read_u32(client->dev.of_node,
1070c459759SDelphine CC Chiu 				 "shunt-resistor-micro-ohms", &rsense))
1080c459759SDelphine CC Chiu 		rsense = 300; /* 0.3 mOhm if not set via DT */
1090c459759SDelphine CC Chiu 
1100c459759SDelphine CC Chiu 	if (rsense == 0)
1110c459759SDelphine CC Chiu 		return -EINVAL;
1120c459759SDelphine CC Chiu 
1130c459759SDelphine CC Chiu 	/* Check for the latter MBR value won't overflow */
1140c459759SDelphine CC Chiu 	if (rsense > (INT_MAX / 1024))
1150c459759SDelphine CC Chiu 		return -EINVAL;
1160c459759SDelphine CC Chiu 
1170c459759SDelphine CC Chiu 	info = devm_kmemdup(&client->dev, &ltc4286_info, sizeof(*info),
1180c459759SDelphine CC Chiu 			    GFP_KERNEL);
1190c459759SDelphine CC Chiu 	if (!info)
1200c459759SDelphine CC Chiu 		return -ENOMEM;
1210c459759SDelphine CC Chiu 
1220c459759SDelphine CC Chiu 	/* Check MFR1 CONFIG register bit 1 VRANGE_SELECT before driver loading */
1230c459759SDelphine CC Chiu 	vrange_oval = i2c_smbus_read_word_data(client, LTC4286_MFR_CONFIG1);
1240c459759SDelphine CC Chiu 	if (vrange_oval < 0)
1250c459759SDelphine CC Chiu 		return dev_err_probe(&client->dev, vrange_oval,
1260c459759SDelphine CC Chiu 				     "Failed to read manufacturer configuration one\n");
1270c459759SDelphine CC Chiu 	vrange_nval = vrange_oval;
1280c459759SDelphine CC Chiu 
1290c459759SDelphine CC Chiu 	if (device_property_read_bool(&client->dev, "adi,vrange-low-enable")) {
1300c459759SDelphine CC Chiu 		vrange_nval &=
1310c459759SDelphine CC Chiu 			~VRANGE_SELECT_BIT; /* VRANGE_SELECT = 0, 25.6 volts */
1320c459759SDelphine CC Chiu 
1330c459759SDelphine CC Chiu 		info->m[PSC_VOLTAGE_IN] = 128;
1340c459759SDelphine CC Chiu 		info->m[PSC_VOLTAGE_OUT] = 128;
1350c459759SDelphine CC Chiu 		info->m[PSC_POWER] = 4 * rsense;
1360c459759SDelphine CC Chiu 	} else {
1370c459759SDelphine CC Chiu 		vrange_nval |=
1380c459759SDelphine CC Chiu 			VRANGE_SELECT_BIT; /* VRANGE_SELECT = 1, 102.4 volts */
1390c459759SDelphine CC Chiu 
1400c459759SDelphine CC Chiu 		info->m[PSC_POWER] = rsense;
1410c459759SDelphine CC Chiu 	}
1420c459759SDelphine CC Chiu 	if (vrange_nval != vrange_oval) {
1430c459759SDelphine CC Chiu 		/* Set MFR1 CONFIG register bit 1 VRANGE_SELECT */
1440c459759SDelphine CC Chiu 		ret = i2c_smbus_write_word_data(client, LTC4286_MFR_CONFIG1,
1450c459759SDelphine CC Chiu 						vrange_nval);
1460c459759SDelphine CC Chiu 		if (ret < 0)
1470c459759SDelphine CC Chiu 			return dev_err_probe(&client->dev, ret,
1480c459759SDelphine CC Chiu 					     "Failed to set vrange\n");
1490c459759SDelphine CC Chiu 	}
1500c459759SDelphine CC Chiu 
1510c459759SDelphine CC Chiu 	info->m[PSC_CURRENT_OUT] = 1024 * rsense;
1520c459759SDelphine CC Chiu 
1530c459759SDelphine CC Chiu 	return pmbus_do_probe(client, info);
1540c459759SDelphine CC Chiu }
1550c459759SDelphine CC Chiu 
1560c459759SDelphine CC Chiu static const struct of_device_id ltc4286_of_match[] = {
1570c459759SDelphine CC Chiu 	{ .compatible = "lltc,ltc4286" },
1580c459759SDelphine CC Chiu 	{ .compatible = "lltc,ltc4287" },
1590c459759SDelphine CC Chiu 	{}
1600c459759SDelphine CC Chiu };
1610c459759SDelphine CC Chiu 
1620c459759SDelphine CC Chiu static struct i2c_driver ltc4286_driver = {
1630c459759SDelphine CC Chiu 	.driver = {
1640c459759SDelphine CC Chiu 		.name = "ltc4286",
1650c459759SDelphine CC Chiu 		.of_match_table = ltc4286_of_match,
1660c459759SDelphine CC Chiu 	},
1670c459759SDelphine CC Chiu 	.probe = ltc4286_probe,
1680c459759SDelphine CC Chiu 	.id_table = ltc4286_id,
1690c459759SDelphine CC Chiu };
1700c459759SDelphine CC Chiu 
1710c459759SDelphine CC Chiu module_i2c_driver(ltc4286_driver);
1720c459759SDelphine CC Chiu 
1730c459759SDelphine CC Chiu MODULE_AUTHOR("Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>");
1740c459759SDelphine CC Chiu MODULE_DESCRIPTION("PMBUS driver for LTC4286 and compatibles");
1750c459759SDelphine CC Chiu MODULE_LICENSE("GPL");
176