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