xref: /linux/drivers/hwmon/pmbus/xdp720.c (revision b8ea7da314c2efcb9c2f559ed65b7a36c869d68e)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Hardware monitoring driver for Infineon XDP720 / XDP730 Digital
4  * eFuse Controllers.
5  *
6  * Both parts share the same PMBus register map and direct-format
7  * coefficients; they differ in the GIMON gain step exposed via
8  * the TELEMETRY_AVG register and in the VDD_VIN pin number.
9  *
10  * Copyright (c) 2026 Infineon Technologies. All rights reserved.
11  */
12 
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/of_device.h>
18 #include <linux/bitops.h>
19 #include <linux/math64.h>
20 #include <linux/property.h>
21 #include <linux/regulator/consumer.h>
22 #include "pmbus.h"
23 
24 /*
25  * The IMON resistor required to generate the system overcurrent protection.
26  * Arbitrary default Rimon value: 2k Ohm
27  */
28 #define XDP720_DEFAULT_RIMON 2000000000U /* 2k ohm */
29 #define XDP720_TELEMETRY_AVG 0xE9
30 #define XDP720_TELEMETRY_AVG_GIMON BIT(10) /* high/low GIMON select */
31 
32 /* Chip identifiers carried in OF match-data and i2c_device_id->driver_data. */
33 enum xdp720_chip_id {
34 	CHIP_XDP720 = 0,
35 	CHIP_XDP730,
36 };
37 
38 struct xdp720_data {
39 	enum xdp720_chip_id	 id;
40 	struct pmbus_driver_info info;
41 };
42 
43 static const struct pmbus_driver_info xdp720_info = {
44 	.pages = 1,
45 	.format[PSC_VOLTAGE_IN] = direct,
46 	.format[PSC_VOLTAGE_OUT] = direct,
47 	.format[PSC_CURRENT_OUT] = direct,
48 	.format[PSC_POWER] = direct,
49 	.format[PSC_TEMPERATURE] = direct,
50 
51 	.m[PSC_VOLTAGE_IN] = 4653,
52 	.b[PSC_VOLTAGE_IN] = 0,
53 	.R[PSC_VOLTAGE_IN] = -2,
54 	.m[PSC_VOLTAGE_OUT] = 4653,
55 	.b[PSC_VOLTAGE_OUT] = 0,
56 	.R[PSC_VOLTAGE_OUT] = -2,
57 	/*
58 	 * Current and Power measurement depends on the RIMON (kOhm) and
59 	 * GIMON(microA/A) values.
60 	 */
61 	.m[PSC_CURRENT_OUT] = 24668,
62 	.b[PSC_CURRENT_OUT] = 0,
63 	.R[PSC_CURRENT_OUT] = -4,
64 	.m[PSC_POWER] = 4486,
65 	.b[PSC_POWER] = 0,
66 	.R[PSC_POWER] = -1,
67 	.m[PSC_TEMPERATURE] = 54,
68 	.b[PSC_TEMPERATURE] = 22521,
69 	.R[PSC_TEMPERATURE] = -1,
70 
71 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_PIN |
72 		   PMBUS_HAVE_TEMP | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_INPUT |
73 		   PMBUS_HAVE_STATUS_TEMP,
74 };
75 
76 static int xdp720_probe(struct i2c_client *client)
77 {
78 	struct xdp720_data *data;
79 	int ret;
80 	u32 rimon;
81 	int gimon;
82 
83 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
84 	if (!data)
85 		return -ENOMEM;
86 
87 	data->id = (enum xdp720_chip_id)(uintptr_t)i2c_get_match_data(client);
88 	data->info = xdp720_info;
89 
90 	ret = devm_regulator_get_enable(&client->dev, "vdd-vin");
91 	if (ret)
92 		return dev_err_probe(&client->dev, ret,
93 			"failed to enable vdd-vin supply\n");
94 
95 	ret = i2c_smbus_read_word_data(client, XDP720_TELEMETRY_AVG);
96 	if (ret < 0) {
97 		dev_err(&client->dev, "Can't get TELEMETRY_AVG\n");
98 		return ret;
99 	}
100 
101 	/* Bit 10 of TELEMETRY_AVG selects the GIMON gain step in microA/A */
102 	switch (data->id) {
103 	case CHIP_XDP720:
104 		gimon = (ret & XDP720_TELEMETRY_AVG_GIMON) ? 18200 : 9100;
105 		dev_info(&client->dev, "Initialised XDP720 instance\n");
106 		break;
107 	case CHIP_XDP730:
108 		gimon = (ret & XDP720_TELEMETRY_AVG_GIMON) ? 20000 : 10000;
109 		dev_info(&client->dev, "Initialised XDP730 instance\n");
110 		break;
111 	default:
112 		return -EINVAL;
113 	}
114 
115 	if (device_property_read_u32(&client->dev,
116 				     "infineon,rimon-micro-ohms", &rimon))
117 		rimon = XDP720_DEFAULT_RIMON;	/* Default if not in FW */
118 	if (rimon == 0)
119 		return -EINVAL;
120 
121 	/* Adapt the current and power scale for each instance */
122 	data->info.m[PSC_CURRENT_OUT] = DIV64_U64_ROUND_CLOSEST((u64)
123 		data->info.m[PSC_CURRENT_OUT] * rimon * gimon,
124 		1000000000000ULL);
125 	data->info.m[PSC_POWER] = DIV64_U64_ROUND_CLOSEST((u64)
126 		data->info.m[PSC_POWER] * rimon * gimon,
127 		1000000000000000ULL);
128 
129 	return pmbus_do_probe(client, &data->info);
130 }
131 
132 static const struct of_device_id xdp720_of_match[] = {
133 	{ .compatible = "infineon,xdp720", .data = (void *)CHIP_XDP720 },
134 	{ .compatible = "infineon,xdp730", .data = (void *)CHIP_XDP730 },
135 	{}
136 };
137 MODULE_DEVICE_TABLE(of, xdp720_of_match);
138 
139 static const struct i2c_device_id xdp720_id[] = {
140 	{ .name = "xdp720", .driver_data = CHIP_XDP720 },
141 	{ .name = "xdp730", .driver_data = CHIP_XDP730 },
142 	{ }
143 };
144 MODULE_DEVICE_TABLE(i2c, xdp720_id);
145 
146 static struct i2c_driver xdp720_driver = {
147 	.driver = {
148 		   .name = "xdp720",
149 		   .of_match_table = xdp720_of_match,
150 	},
151 	.probe = xdp720_probe,
152 	.id_table = xdp720_id,
153 };
154 
155 module_i2c_driver(xdp720_driver);
156 
157 MODULE_AUTHOR("Ashish Yadav <ashish.yadav@infineon.com>");
158 MODULE_DESCRIPTION("PMBus driver for Infineon XDP720/XDP730 Digital eFuse Controllers");
159 MODULE_LICENSE("GPL");
160 MODULE_IMPORT_NS("PMBUS");
161