xref: /openbmc/linux/drivers/hwmon/pmbus/xdpe12284.c (revision dc6a81c3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Hardware monitoring driver for Infineon Multi-phase Digital VR Controllers
4  *
5  * Copyright (c) 2020 Mellanox Technologies. All rights reserved.
6  */
7 
8 #include <linux/err.h>
9 #include <linux/i2c.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include "pmbus.h"
14 
15 #define XDPE122_PROT_VR12_5MV		0x01 /* VR12.0 mode, 5-mV DAC */
16 #define XDPE122_PROT_VR12_5_10MV	0x02 /* VR12.5 mode, 10-mV DAC */
17 #define XDPE122_PROT_IMVP9_10MV		0x03 /* IMVP9 mode, 10-mV DAC */
18 #define XDPE122_AMD_625MV		0x10 /* AMD mode 6.25mV */
19 #define XDPE122_PAGE_NUM		2
20 
21 static int xdpe122_identify(struct i2c_client *client,
22 			    struct pmbus_driver_info *info)
23 {
24 	u8 vout_params;
25 	int i, ret;
26 
27 	for (i = 0; i < XDPE122_PAGE_NUM; i++) {
28 		/* Read the register with VOUT scaling value.*/
29 		ret = pmbus_read_byte_data(client, i, PMBUS_VOUT_MODE);
30 		if (ret < 0)
31 			return ret;
32 
33 		vout_params = ret & GENMASK(4, 0);
34 
35 		switch (vout_params) {
36 		case XDPE122_PROT_VR12_5_10MV:
37 			info->vrm_version[i] = vr13;
38 			break;
39 		case XDPE122_PROT_VR12_5MV:
40 			info->vrm_version[i] = vr12;
41 			break;
42 		case XDPE122_PROT_IMVP9_10MV:
43 			info->vrm_version[i] = imvp9;
44 			break;
45 		case XDPE122_AMD_625MV:
46 			info->vrm_version[i] = amd625mv;
47 			break;
48 		default:
49 			return -EINVAL;
50 		}
51 	}
52 
53 	return 0;
54 }
55 
56 static struct pmbus_driver_info xdpe122_info = {
57 	.pages = XDPE122_PAGE_NUM,
58 	.format[PSC_VOLTAGE_IN] = linear,
59 	.format[PSC_VOLTAGE_OUT] = vid,
60 	.format[PSC_TEMPERATURE] = linear,
61 	.format[PSC_CURRENT_IN] = linear,
62 	.format[PSC_CURRENT_OUT] = linear,
63 	.format[PSC_POWER] = linear,
64 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
65 		PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
66 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
67 		PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT,
68 	.func[1] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
69 		PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
70 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
71 		PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT,
72 	.identify = xdpe122_identify,
73 };
74 
75 static int xdpe122_probe(struct i2c_client *client,
76 			 const struct i2c_device_id *id)
77 {
78 	struct pmbus_driver_info *info;
79 
80 	info = devm_kmemdup(&client->dev, &xdpe122_info, sizeof(*info),
81 			    GFP_KERNEL);
82 	if (!info)
83 		return -ENOMEM;
84 
85 	return pmbus_do_probe(client, id, info);
86 }
87 
88 static const struct i2c_device_id xdpe122_id[] = {
89 	{"xdpe12254", 0},
90 	{"xdpe12284", 0},
91 	{}
92 };
93 
94 MODULE_DEVICE_TABLE(i2c, xdpe122_id);
95 
96 static const struct of_device_id __maybe_unused xdpe122_of_match[] = {
97 	{.compatible = "infineon, xdpe12254"},
98 	{.compatible = "infineon, xdpe12284"},
99 	{}
100 };
101 MODULE_DEVICE_TABLE(of, xdpe122_of_match);
102 
103 static struct i2c_driver xdpe122_driver = {
104 	.driver = {
105 		.name = "xdpe12284",
106 		.of_match_table = of_match_ptr(xdpe122_of_match),
107 	},
108 	.probe = xdpe122_probe,
109 	.remove = pmbus_do_remove,
110 	.id_table = xdpe122_id,
111 };
112 
113 module_i2c_driver(xdpe122_driver);
114 
115 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
116 MODULE_DESCRIPTION("PMBus driver for Infineon XDPE122 family");
117 MODULE_LICENSE("GPL");
118