xref: /openbmc/linux/drivers/hwmon/pmbus/bpa-rs600.c (revision 1975d167)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Hardware monitoring driver for BluTek BPA-RS600 Power Supplies
4  *
5  * Copyright 2021 Allied Telesis Labs
6  */
7 
8 #include <linux/i2c.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/pmbus.h>
13 #include "pmbus.h"
14 
15 enum chips { bpa_rs600, bpd_rs600 };
16 
bpa_rs600_read_byte_data(struct i2c_client * client,int page,int reg)17 static int bpa_rs600_read_byte_data(struct i2c_client *client, int page, int reg)
18 {
19 	int ret;
20 
21 	if (page > 0)
22 		return -ENXIO;
23 
24 	switch (reg) {
25 	case PMBUS_FAN_CONFIG_12:
26 		/*
27 		 * Two fans are reported in PMBUS_FAN_CONFIG_12 but there is
28 		 * only one fan in the module. Mask out the FAN2 bits.
29 		 */
30 		ret = pmbus_read_byte_data(client, 0, PMBUS_FAN_CONFIG_12);
31 		if (ret >= 0)
32 			ret &= ~(PB_FAN_2_INSTALLED | PB_FAN_2_PULSE_MASK);
33 		break;
34 	default:
35 		ret = -ENODATA;
36 		break;
37 	}
38 
39 	return ret;
40 }
41 
42 /*
43  * The BPA-RS600 violates the PMBus spec. Specifically it treats the
44  * mantissa as unsigned. Deal with this here to allow the PMBus core
45  * to work with correctly encoded data.
46  */
bpa_rs600_read_vin(struct i2c_client * client)47 static int bpa_rs600_read_vin(struct i2c_client *client)
48 {
49 	int ret, exponent, mantissa;
50 
51 	ret = pmbus_read_word_data(client, 0, 0xff, PMBUS_READ_VIN);
52 	if (ret < 0)
53 		return ret;
54 
55 	if (ret & BIT(10)) {
56 		exponent = ret >> 11;
57 		mantissa = ret & 0x7ff;
58 
59 		exponent++;
60 		mantissa >>= 1;
61 
62 		ret = (exponent << 11) | mantissa;
63 	}
64 
65 	return ret;
66 }
67 
68 /*
69  * Firmware V5.70 incorrectly reports 1640W for MFR_PIN_MAX.
70  * Deal with this by returning a sensible value.
71  */
bpa_rs600_read_pin_max(struct i2c_client * client)72 static int bpa_rs600_read_pin_max(struct i2c_client *client)
73 {
74 	int ret;
75 
76 	ret = pmbus_read_word_data(client, 0, 0xff, PMBUS_MFR_PIN_MAX);
77 	if (ret < 0)
78 		return ret;
79 
80 	/* Detect invalid 1640W (linear encoding) */
81 	if (ret == 0x0b34)
82 		/* Report 700W (linear encoding) */
83 		return 0x095e;
84 
85 	return ret;
86 }
87 
bpa_rs600_read_word_data(struct i2c_client * client,int page,int phase,int reg)88 static int bpa_rs600_read_word_data(struct i2c_client *client, int page, int phase, int reg)
89 {
90 	int ret;
91 
92 	if (page > 0)
93 		return -ENXIO;
94 
95 	switch (reg) {
96 	case PMBUS_VIN_UV_WARN_LIMIT:
97 	case PMBUS_VIN_OV_WARN_LIMIT:
98 	case PMBUS_VOUT_UV_WARN_LIMIT:
99 	case PMBUS_VOUT_OV_WARN_LIMIT:
100 	case PMBUS_IIN_OC_WARN_LIMIT:
101 	case PMBUS_IOUT_OC_WARN_LIMIT:
102 	case PMBUS_PIN_OP_WARN_LIMIT:
103 	case PMBUS_POUT_OP_WARN_LIMIT:
104 	case PMBUS_VIN_UV_FAULT_LIMIT:
105 	case PMBUS_VIN_OV_FAULT_LIMIT:
106 	case PMBUS_VOUT_UV_FAULT_LIMIT:
107 	case PMBUS_VOUT_OV_FAULT_LIMIT:
108 		/* These commands return data but it is invalid/un-documented */
109 		ret = -ENXIO;
110 		break;
111 	case PMBUS_READ_VIN:
112 		ret = bpa_rs600_read_vin(client);
113 		break;
114 	case PMBUS_MFR_PIN_MAX:
115 		ret = bpa_rs600_read_pin_max(client);
116 		break;
117 	default:
118 		if (reg >= PMBUS_VIRT_BASE)
119 			ret = -ENXIO;
120 		else
121 			ret = -ENODATA;
122 		break;
123 	}
124 
125 	return ret;
126 }
127 
128 static struct pmbus_driver_info bpa_rs600_info = {
129 	.pages = 1,
130 	.format[PSC_VOLTAGE_IN] = linear,
131 	.format[PSC_VOLTAGE_OUT] = linear,
132 	.format[PSC_CURRENT_IN] = linear,
133 	.format[PSC_CURRENT_OUT] = linear,
134 	.format[PSC_POWER] = linear,
135 	.format[PSC_TEMPERATURE] = linear,
136 	.format[PSC_FAN] = linear,
137 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT |
138 		PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT |
139 		PMBUS_HAVE_PIN | PMBUS_HAVE_POUT |
140 		PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 |
141 		PMBUS_HAVE_FAN12 |
142 		PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
143 		PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP |
144 		PMBUS_HAVE_STATUS_FAN12,
145 	.read_byte_data = bpa_rs600_read_byte_data,
146 	.read_word_data = bpa_rs600_read_word_data,
147 };
148 
149 static const struct i2c_device_id bpa_rs600_id[] = {
150 	{ "bpa-rs600", bpa_rs600 },
151 	{ "bpd-rs600", bpd_rs600 },
152 	{},
153 };
154 MODULE_DEVICE_TABLE(i2c, bpa_rs600_id);
155 
bpa_rs600_probe(struct i2c_client * client)156 static int bpa_rs600_probe(struct i2c_client *client)
157 {
158 	struct device *dev = &client->dev;
159 	u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
160 	int ret;
161 	const struct i2c_device_id *mid;
162 
163 	if (!i2c_check_functionality(client->adapter,
164 				     I2C_FUNC_SMBUS_READ_BYTE_DATA
165 				     | I2C_FUNC_SMBUS_READ_WORD_DATA
166 				     | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
167 		return -ENODEV;
168 
169 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
170 	if (ret < 0) {
171 		dev_err(dev, "Failed to read Manufacturer Model\n");
172 		return ret;
173 	}
174 
175 	for (mid = bpa_rs600_id; mid->name[0]; mid++) {
176 		if (!strncasecmp(buf, mid->name, strlen(mid->name)))
177 			break;
178 	}
179 	if (!mid->name[0]) {
180 		buf[ret] = '\0';
181 		dev_err(dev, "Unsupported Manufacturer Model '%s'\n", buf);
182 		return -ENODEV;
183 	}
184 
185 	return pmbus_do_probe(client, &bpa_rs600_info);
186 }
187 
188 static const struct of_device_id __maybe_unused bpa_rs600_of_match[] = {
189 	{ .compatible = "blutek,bpa-rs600" },
190 	{},
191 };
192 MODULE_DEVICE_TABLE(of, bpa_rs600_of_match);
193 
194 static struct i2c_driver bpa_rs600_driver = {
195 	.driver = {
196 		.name = "bpa-rs600",
197 		.of_match_table = of_match_ptr(bpa_rs600_of_match),
198 	},
199 	.probe = bpa_rs600_probe,
200 	.id_table = bpa_rs600_id,
201 };
202 
203 module_i2c_driver(bpa_rs600_driver);
204 
205 MODULE_AUTHOR("Chris Packham");
206 MODULE_DESCRIPTION("PMBus driver for BluTek BPA-RS600");
207 MODULE_LICENSE("GPL");
208 MODULE_IMPORT_NS(PMBUS);
209