xref: /openbmc/linux/drivers/hwmon/pmbus/bpa-rs600.c (revision 35267cea)
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 #define BPARS600_MFR_VIN_MIN	0xa0
16 #define BPARS600_MFR_VIN_MAX	0xa1
17 #define BPARS600_MFR_IIN_MAX	0xa2
18 #define BPARS600_MFR_PIN_MAX	0xa3
19 #define BPARS600_MFR_VOUT_MIN	0xa4
20 #define BPARS600_MFR_VOUT_MAX	0xa5
21 #define BPARS600_MFR_IOUT_MAX	0xa6
22 #define BPARS600_MFR_POUT_MAX	0xa7
23 
24 static int bpa_rs600_read_byte_data(struct i2c_client *client, int page, int reg)
25 {
26 	int ret;
27 
28 	if (page > 0)
29 		return -ENXIO;
30 
31 	switch (reg) {
32 	case PMBUS_FAN_CONFIG_12:
33 		/*
34 		 * Two fans are reported in PMBUS_FAN_CONFIG_12 but there is
35 		 * only one fan in the module. Mask out the FAN2 bits.
36 		 */
37 		ret = pmbus_read_byte_data(client, 0, PMBUS_FAN_CONFIG_12);
38 		if (ret >= 0)
39 			ret &= ~(PB_FAN_2_INSTALLED | PB_FAN_2_PULSE_MASK);
40 		break;
41 	default:
42 		ret = -ENODATA;
43 		break;
44 	}
45 
46 	return ret;
47 }
48 
49 /*
50  * The BPA-RS600 violates the PMBus spec. Specifically it treats the
51  * mantissa as unsigned. Deal with this here to allow the PMBus core
52  * to work with correctly encoded data.
53  */
54 static int bpa_rs600_read_vin(struct i2c_client *client)
55 {
56 	int ret, exponent, mantissa;
57 
58 	ret = pmbus_read_word_data(client, 0, 0xff, PMBUS_READ_VIN);
59 	if (ret < 0)
60 		return ret;
61 
62 	if (ret & BIT(10)) {
63 		exponent = ret >> 11;
64 		mantissa = ret & 0x7ff;
65 
66 		exponent++;
67 		mantissa >>= 1;
68 
69 		ret = (exponent << 11) | mantissa;
70 	}
71 
72 	return ret;
73 }
74 
75 static int bpa_rs600_read_word_data(struct i2c_client *client, int page, int phase, int reg)
76 {
77 	int ret;
78 
79 	if (page > 0)
80 		return -ENXIO;
81 
82 	switch (reg) {
83 	case PMBUS_VIN_UV_WARN_LIMIT:
84 		ret = pmbus_read_word_data(client, 0, 0xff, BPARS600_MFR_VIN_MIN);
85 		break;
86 	case PMBUS_VIN_OV_WARN_LIMIT:
87 		ret = pmbus_read_word_data(client, 0, 0xff, BPARS600_MFR_VIN_MAX);
88 		break;
89 	case PMBUS_VOUT_UV_WARN_LIMIT:
90 		ret = pmbus_read_word_data(client, 0, 0xff, BPARS600_MFR_VOUT_MIN);
91 		break;
92 	case PMBUS_VOUT_OV_WARN_LIMIT:
93 		ret = pmbus_read_word_data(client, 0, 0xff, BPARS600_MFR_VOUT_MAX);
94 		break;
95 	case PMBUS_IIN_OC_WARN_LIMIT:
96 		ret = pmbus_read_word_data(client, 0, 0xff, BPARS600_MFR_IIN_MAX);
97 		break;
98 	case PMBUS_IOUT_OC_WARN_LIMIT:
99 		ret = pmbus_read_word_data(client, 0, 0xff, BPARS600_MFR_IOUT_MAX);
100 		break;
101 	case PMBUS_PIN_OP_WARN_LIMIT:
102 		ret = pmbus_read_word_data(client, 0, 0xff, BPARS600_MFR_PIN_MAX);
103 		break;
104 	case PMBUS_POUT_OP_WARN_LIMIT:
105 		ret = pmbus_read_word_data(client, 0, 0xff, BPARS600_MFR_POUT_MAX);
106 		break;
107 	case PMBUS_VIN_UV_FAULT_LIMIT:
108 	case PMBUS_VIN_OV_FAULT_LIMIT:
109 	case PMBUS_VOUT_UV_FAULT_LIMIT:
110 	case PMBUS_VOUT_OV_FAULT_LIMIT:
111 		/* These commands return data but it is invalid/un-documented */
112 		ret = -ENXIO;
113 		break;
114 	case PMBUS_READ_VIN:
115 		ret = bpa_rs600_read_vin(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 int bpa_rs600_probe(struct i2c_client *client)
150 {
151 	struct device *dev = &client->dev;
152 	u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
153 	int ret;
154 
155 	if (!i2c_check_functionality(client->adapter,
156 				     I2C_FUNC_SMBUS_READ_BYTE_DATA
157 				     | I2C_FUNC_SMBUS_READ_WORD_DATA
158 				     | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
159 		return -ENODEV;
160 
161 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
162 	if (ret < 0) {
163 		dev_err(dev, "Failed to read Manufacturer Model\n");
164 		return ret;
165 	}
166 
167 	if (strncmp(buf, "BPA-RS600", 8)) {
168 		buf[ret] = '\0';
169 		dev_err(dev, "Unsupported Manufacturer Model '%s'\n", buf);
170 		return -ENODEV;
171 	}
172 
173 	return pmbus_do_probe(client, &bpa_rs600_info);
174 }
175 
176 static const struct i2c_device_id bpa_rs600_id[] = {
177 	{ "bpars600", 0 },
178 	{},
179 };
180 MODULE_DEVICE_TABLE(i2c, bpa_rs600_id);
181 
182 static const struct of_device_id __maybe_unused bpa_rs600_of_match[] = {
183 	{ .compatible = "blutek,bpa-rs600" },
184 	{},
185 };
186 MODULE_DEVICE_TABLE(of, bpa_rs600_of_match);
187 
188 static struct i2c_driver bpa_rs600_driver = {
189 	.driver = {
190 		.name = "bpa-rs600",
191 		.of_match_table = of_match_ptr(bpa_rs600_of_match),
192 	},
193 	.probe_new = bpa_rs600_probe,
194 	.id_table = bpa_rs600_id,
195 };
196 
197 module_i2c_driver(bpa_rs600_driver);
198 
199 MODULE_AUTHOR("Chris Packham");
200 MODULE_DESCRIPTION("PMBus driver for BluTek BPA-RS600");
201 MODULE_LICENSE("GPL");
202 MODULE_IMPORT_NS(PMBUS);
203