xref: /openbmc/linux/drivers/hwmon/pmbus/max16601.c (revision 83146efc)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Hardware monitoring driver for Maxim MAX16508, MAX16600, MAX16601,
4  * and MAX16602.
5  *
6  * Implementation notes:
7  *
8  * This chip series supports two rails, VCORE and VSA. Telemetry information
9  * for the two rails is reported in two subsequent I2C addresses. The driver
10  * instantiates a dummy I2C client at the second I2C address to report
11  * information for the VSA rail in a single instance of the driver.
12  * Telemetry for the VSA rail is reported to the PMBus core in PMBus page 2.
13  *
14  * The chip reports input current using two separate methods. The input current
15  * reported with the standard READ_IIN command is derived from the output
16  * current. The first method is reported to the PMBus core with PMBus page 0,
17  * the second method is reported with PMBus page 1.
18  *
19  * The chip supports reading per-phase temperatures and per-phase input/output
20  * currents for VCORE. Telemetry is reported in vendor specific registers.
21  * The driver translates the vendor specific register values to PMBus standard
22  * register values and reports per-phase information in PMBus page 0.
23  *
24  * Copyright 2019, 2020 Google LLC.
25  */
26 
27 #include <linux/bits.h>
28 #include <linux/i2c.h>
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 
33 #include "pmbus.h"
34 
35 enum chips { max16508, max16600, max16601, max16602 };
36 
37 #define REG_DEFAULT_NUM_POP	0xc4
38 #define REG_SETPT_DVID		0xd1
39 #define  DAC_10MV_MODE		BIT(4)
40 #define REG_IOUT_AVG_PK		0xee
41 #define REG_IIN_SENSOR		0xf1
42 #define REG_TOTAL_INPUT_POWER	0xf2
43 #define REG_PHASE_ID		0xf3
44 #define  CORE_RAIL_INDICATOR	BIT(7)
45 #define REG_PHASE_REPORTING	0xf4
46 
47 #define MAX16601_NUM_PHASES	8
48 
49 struct max16601_data {
50 	enum chips id;
51 	struct pmbus_driver_info info;
52 	struct i2c_client *vsa;
53 	int iout_avg_pkg;
54 };
55 
56 #define to_max16601_data(x) container_of(x, struct max16601_data, info)
57 
58 static int max16601_read_byte(struct i2c_client *client, int page, int reg)
59 {
60 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
61 	struct max16601_data *data = to_max16601_data(info);
62 
63 	if (page > 0) {
64 		if (page == 2)	/* VSA */
65 			return i2c_smbus_read_byte_data(data->vsa, reg);
66 		return -EOPNOTSUPP;
67 	}
68 	return -ENODATA;
69 }
70 
71 static int max16601_read_word(struct i2c_client *client, int page, int phase,
72 			      int reg)
73 {
74 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
75 	struct max16601_data *data = to_max16601_data(info);
76 	u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
77 	int ret;
78 
79 	switch (page) {
80 	case 0:		/* VCORE */
81 		if (phase == 0xff)
82 			return -ENODATA;
83 		switch (reg) {
84 		case PMBUS_READ_IIN:
85 		case PMBUS_READ_IOUT:
86 		case PMBUS_READ_TEMPERATURE_1:
87 			ret = i2c_smbus_write_byte_data(client, REG_PHASE_ID,
88 							phase);
89 			if (ret)
90 				return ret;
91 			ret = i2c_smbus_read_block_data(client,
92 							REG_PHASE_REPORTING,
93 							buf);
94 			if (ret < 0)
95 				return ret;
96 			if (ret < 6)
97 				return -EIO;
98 			switch (reg) {
99 			case PMBUS_READ_TEMPERATURE_1:
100 				return buf[1] << 8 | buf[0];
101 			case PMBUS_READ_IOUT:
102 				return buf[3] << 8 | buf[2];
103 			case PMBUS_READ_IIN:
104 				return buf[5] << 8 | buf[4];
105 			default:
106 				break;
107 			}
108 		}
109 		return -EOPNOTSUPP;
110 	case 1:		/* VCORE, read IIN/PIN from sensor element */
111 		switch (reg) {
112 		case PMBUS_READ_IIN:
113 			return i2c_smbus_read_word_data(client, REG_IIN_SENSOR);
114 		case PMBUS_READ_PIN:
115 			return i2c_smbus_read_word_data(client,
116 							REG_TOTAL_INPUT_POWER);
117 		default:
118 			break;
119 		}
120 		return -EOPNOTSUPP;
121 	case 2:		/* VSA */
122 		switch (reg) {
123 		case PMBUS_VIRT_READ_IOUT_MAX:
124 			ret = i2c_smbus_read_word_data(data->vsa,
125 						       REG_IOUT_AVG_PK);
126 			if (ret < 0)
127 				return ret;
128 			if (sign_extend32(ret, 10) >
129 			    sign_extend32(data->iout_avg_pkg, 10))
130 				data->iout_avg_pkg = ret;
131 			return data->iout_avg_pkg;
132 		case PMBUS_VIRT_RESET_IOUT_HISTORY:
133 			return 0;
134 		case PMBUS_IOUT_OC_FAULT_LIMIT:
135 		case PMBUS_IOUT_OC_WARN_LIMIT:
136 		case PMBUS_OT_FAULT_LIMIT:
137 		case PMBUS_OT_WARN_LIMIT:
138 		case PMBUS_READ_IIN:
139 		case PMBUS_READ_IOUT:
140 		case PMBUS_READ_TEMPERATURE_1:
141 		case PMBUS_STATUS_WORD:
142 			return i2c_smbus_read_word_data(data->vsa, reg);
143 		default:
144 			return -EOPNOTSUPP;
145 		}
146 	default:
147 		return -EOPNOTSUPP;
148 	}
149 }
150 
151 static int max16601_write_byte(struct i2c_client *client, int page, u8 reg)
152 {
153 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
154 	struct max16601_data *data = to_max16601_data(info);
155 
156 	if (page == 2) {
157 		if (reg == PMBUS_CLEAR_FAULTS)
158 			return i2c_smbus_write_byte(data->vsa, reg);
159 		return -EOPNOTSUPP;
160 	}
161 	return -ENODATA;
162 }
163 
164 static int max16601_write_word(struct i2c_client *client, int page, int reg,
165 			       u16 value)
166 {
167 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
168 	struct max16601_data *data = to_max16601_data(info);
169 
170 	switch (page) {
171 	case 0:		/* VCORE */
172 		return -ENODATA;
173 	case 1:		/* VCORE IIN/PIN from sensor element */
174 	default:
175 		return -EOPNOTSUPP;
176 	case 2:		/* VSA */
177 		switch (reg) {
178 		case PMBUS_VIRT_RESET_IOUT_HISTORY:
179 			data->iout_avg_pkg = 0xfc00;
180 			return 0;
181 		case PMBUS_IOUT_OC_FAULT_LIMIT:
182 		case PMBUS_IOUT_OC_WARN_LIMIT:
183 		case PMBUS_OT_FAULT_LIMIT:
184 		case PMBUS_OT_WARN_LIMIT:
185 			return i2c_smbus_write_word_data(data->vsa, reg, value);
186 		default:
187 			return -EOPNOTSUPP;
188 		}
189 	}
190 }
191 
192 static int max16601_identify(struct i2c_client *client,
193 			     struct pmbus_driver_info *info)
194 {
195 	struct max16601_data *data = to_max16601_data(info);
196 	int reg;
197 
198 	reg = i2c_smbus_read_byte_data(client, REG_SETPT_DVID);
199 	if (reg < 0)
200 		return reg;
201 	if (reg & DAC_10MV_MODE)
202 		info->vrm_version[0] = vr13;
203 	else
204 		info->vrm_version[0] = vr12;
205 
206 	if (data->id != max16600 && data->id != max16601 && data->id != max16602)
207 		return 0;
208 
209 	reg = i2c_smbus_read_byte_data(client, REG_DEFAULT_NUM_POP);
210 	if (reg < 0)
211 		return reg;
212 
213 	/*
214 	 * If REG_DEFAULT_NUM_POP returns 0, we don't know how many phases
215 	 * are populated. Stick with the default in that case.
216 	 */
217 	reg &= 0x0f;
218 	if (reg && reg <= MAX16601_NUM_PHASES)
219 		info->phases[0] = reg;
220 
221 	return 0;
222 }
223 
224 static struct pmbus_driver_info max16601_info = {
225 	.pages = 3,
226 	.format[PSC_VOLTAGE_IN] = linear,
227 	.format[PSC_VOLTAGE_OUT] = vid,
228 	.format[PSC_CURRENT_IN] = linear,
229 	.format[PSC_CURRENT_OUT] = linear,
230 	.format[PSC_TEMPERATURE] = linear,
231 	.format[PSC_POWER] = linear,
232 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN | PMBUS_HAVE_PIN |
233 		PMBUS_HAVE_STATUS_INPUT |
234 		PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
235 		PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
236 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
237 		PMBUS_HAVE_POUT | PMBUS_PAGE_VIRTUAL | PMBUS_PHASE_VIRTUAL,
238 	.func[1] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN | PMBUS_PAGE_VIRTUAL,
239 	.func[2] = PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT |
240 		PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
241 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_PAGE_VIRTUAL,
242 	.phases[0] = MAX16601_NUM_PHASES,
243 	.pfunc[0] = PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP,
244 	.pfunc[1] = PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT,
245 	.pfunc[2] = PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP,
246 	.pfunc[3] = PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT,
247 	.pfunc[4] = PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP,
248 	.pfunc[5] = PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT,
249 	.pfunc[6] = PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP,
250 	.pfunc[7] = PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT,
251 	.identify = max16601_identify,
252 	.read_byte_data = max16601_read_byte,
253 	.read_word_data = max16601_read_word,
254 	.write_byte = max16601_write_byte,
255 	.write_word_data = max16601_write_word,
256 };
257 
258 static void max16601_remove(void *_data)
259 {
260 	struct max16601_data *data = _data;
261 
262 	i2c_unregister_device(data->vsa);
263 }
264 
265 static const struct i2c_device_id max16601_id[] = {
266 	{"max16508", max16508},
267 	{"max16600", max16600},
268 	{"max16601", max16601},
269 	{"max16602", max16602},
270 	{}
271 };
272 MODULE_DEVICE_TABLE(i2c, max16601_id);
273 
274 static int max16601_get_id(struct i2c_client *client)
275 {
276 	struct device *dev = &client->dev;
277 	u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
278 	enum chips id;
279 	int ret;
280 
281 	ret = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, buf);
282 	if (ret < 0 || ret < 11)
283 		return -ENODEV;
284 
285 	/*
286 	 * PMBUS_IC_DEVICE_ID is expected to return MAX1660[012]y.xx",
287 	 * "MAX16500y.xx".cdxxcccccccccc, or "MAX16508y.xx".
288 	 */
289 	if (!strncmp(buf, "MAX16500", 8) || !strncmp(buf, "MAX16508", 8)) {
290 		id = max16508;
291 	} else if (!strncmp(buf, "MAX16600", 8)) {
292 		id = max16600;
293 	} else if (!strncmp(buf, "MAX16601", 8)) {
294 		id = max16601;
295 	} else if (!strncmp(buf, "MAX16602", 8)) {
296 		id = max16602;
297 	} else {
298 		buf[ret] = '\0';
299 		dev_err(dev, "Unsupported chip '%s'\n", buf);
300 		return -ENODEV;
301 	}
302 	return id;
303 }
304 
305 static int max16601_probe(struct i2c_client *client)
306 {
307 	struct device *dev = &client->dev;
308 	const struct i2c_device_id *id;
309 	struct max16601_data *data;
310 	int ret, chip_id;
311 
312 	if (!i2c_check_functionality(client->adapter,
313 				     I2C_FUNC_SMBUS_READ_BYTE_DATA |
314 				     I2C_FUNC_SMBUS_READ_BLOCK_DATA))
315 		return -ENODEV;
316 
317 	chip_id = max16601_get_id(client);
318 	if (chip_id < 0)
319 		return chip_id;
320 
321 	id = i2c_match_id(max16601_id, client);
322 	if (chip_id != id->driver_data)
323 		dev_warn(&client->dev,
324 			 "Device mismatch: Configured %s (%d), detected %d\n",
325 			 id->name, (int) id->driver_data, chip_id);
326 
327 	ret = i2c_smbus_read_byte_data(client, REG_PHASE_ID);
328 	if (ret < 0)
329 		return ret;
330 	if (!(ret & CORE_RAIL_INDICATOR)) {
331 		dev_err(dev,
332 			"Driver must be instantiated on CORE rail I2C address\n");
333 		return -ENODEV;
334 	}
335 
336 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
337 	if (!data)
338 		return -ENOMEM;
339 
340 	data->id = chip_id;
341 	data->iout_avg_pkg = 0xfc00;
342 	data->vsa = i2c_new_dummy_device(client->adapter, client->addr + 1);
343 	if (IS_ERR(data->vsa)) {
344 		dev_err(dev, "Failed to register VSA client\n");
345 		return PTR_ERR(data->vsa);
346 	}
347 	ret = devm_add_action_or_reset(dev, max16601_remove, data);
348 	if (ret)
349 		return ret;
350 
351 	data->info = max16601_info;
352 
353 	return pmbus_do_probe(client, &data->info);
354 }
355 
356 static struct i2c_driver max16601_driver = {
357 	.driver = {
358 		   .name = "max16601",
359 		   },
360 	.probe = max16601_probe,
361 	.id_table = max16601_id,
362 };
363 
364 module_i2c_driver(max16601_driver);
365 
366 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
367 MODULE_DESCRIPTION("PMBus driver for Maxim MAX16601");
368 MODULE_LICENSE("GPL v2");
369 MODULE_IMPORT_NS(PMBUS);
370