xref: /linux/drivers/hwmon/pmbus/isl68137.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Hardware monitoring driver for Renesas Digital Multiphase Voltage Regulators
4  *
5  * Copyright (c) 2017 Google Inc
6  * Copyright (c) 2020 Renesas Electronics America
7  *
8  */
9 
10 #include <linux/err.h>
11 #include <linux/hwmon-sysfs.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/string.h>
18 #include <linux/sysfs.h>
19 
20 #include "pmbus.h"
21 
22 #define ISL68137_VOUT_AVS	0x30
23 #define RAA_DMPVR2_READ_VMON	0xc8
24 #define MAX_CHANNELS            4
25 
26 enum chips {
27 	isl68137,
28 	isl68220,
29 	isl68221,
30 	isl68222,
31 	isl68223,
32 	isl68224,
33 	isl68225,
34 	isl68226,
35 	isl68227,
36 	isl68229,
37 	isl68233,
38 	isl68239,
39 	isl69222,
40 	isl69223,
41 	isl69224,
42 	isl69225,
43 	isl69227,
44 	isl69228,
45 	isl69234,
46 	isl69236,
47 	isl69239,
48 	isl69242,
49 	isl69243,
50 	isl69247,
51 	isl69248,
52 	isl69254,
53 	isl69255,
54 	isl69256,
55 	isl69259,
56 	isl69260,
57 	isl69268,
58 	isl69269,
59 	isl69298,
60 	raa228000,
61 	raa228004,
62 	raa228006,
63 	raa228228,
64 	raa229001,
65 	raa229004,
66 };
67 
68 enum variants {
69 	raa_dmpvr1_2rail,
70 	raa_dmpvr2_1rail,
71 	raa_dmpvr2_2rail,
72 	raa_dmpvr2_2rail_nontc,
73 	raa_dmpvr2_3rail,
74 	raa_dmpvr2_hv,
75 };
76 
77 struct isl68137_channel {
78 	u32 vout_voltage_divider[2];
79 };
80 
81 struct isl68137_data {
82 	struct pmbus_driver_info info;
83 	struct isl68137_channel channel[MAX_CHANNELS];
84 };
85 
86 #define to_isl68137_data(x)	container_of(x, struct isl68137_data, info)
87 
88 static const struct i2c_device_id raa_dmpvr_id[];
89 
90 static ssize_t isl68137_avs_enable_show_page(struct i2c_client *client,
91 					     int page,
92 					     char *buf)
93 {
94 	int val = pmbus_read_byte_data(client, page, PMBUS_OPERATION);
95 
96 	return sprintf(buf, "%d\n",
97 		       (val & ISL68137_VOUT_AVS) == ISL68137_VOUT_AVS ? 1 : 0);
98 }
99 
100 static ssize_t isl68137_avs_enable_store_page(struct i2c_client *client,
101 					      int page,
102 					      const char *buf, size_t count)
103 {
104 	int rc, op_val;
105 	bool result;
106 
107 	rc = kstrtobool(buf, &result);
108 	if (rc)
109 		return rc;
110 
111 	op_val = result ? ISL68137_VOUT_AVS : 0;
112 
113 	/*
114 	 * Writes to VOUT setpoint over AVSBus will persist after the VRM is
115 	 * switched to PMBus control. Switching back to AVSBus control
116 	 * restores this persisted setpoint rather than re-initializing to
117 	 * PMBus VOUT_COMMAND. Writing VOUT_COMMAND first over PMBus before
118 	 * enabling AVS control is the workaround.
119 	 */
120 	if (op_val == ISL68137_VOUT_AVS) {
121 		rc = pmbus_read_word_data(client, page, 0xff,
122 					  PMBUS_VOUT_COMMAND);
123 		if (rc < 0)
124 			return rc;
125 
126 		rc = pmbus_write_word_data(client, page, PMBUS_VOUT_COMMAND,
127 					   rc);
128 		if (rc < 0)
129 			return rc;
130 	}
131 
132 	rc = pmbus_update_byte_data(client, page, PMBUS_OPERATION,
133 				    ISL68137_VOUT_AVS, op_val);
134 
135 	return (rc < 0) ? rc : count;
136 }
137 
138 static ssize_t isl68137_avs_enable_show(struct device *dev,
139 					struct device_attribute *devattr,
140 					char *buf)
141 {
142 	struct i2c_client *client = to_i2c_client(dev->parent);
143 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
144 
145 	return isl68137_avs_enable_show_page(client, attr->index, buf);
146 }
147 
148 static ssize_t isl68137_avs_enable_store(struct device *dev,
149 				struct device_attribute *devattr,
150 				const char *buf, size_t count)
151 {
152 	struct i2c_client *client = to_i2c_client(dev->parent);
153 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
154 
155 	return isl68137_avs_enable_store_page(client, attr->index, buf, count);
156 }
157 
158 static SENSOR_DEVICE_ATTR_RW(avs0_enable, isl68137_avs_enable, 0);
159 static SENSOR_DEVICE_ATTR_RW(avs1_enable, isl68137_avs_enable, 1);
160 
161 static struct attribute *enable_attrs[] = {
162 	&sensor_dev_attr_avs0_enable.dev_attr.attr,
163 	&sensor_dev_attr_avs1_enable.dev_attr.attr,
164 	NULL,
165 };
166 
167 static const struct attribute_group enable_group = {
168 	.attrs = enable_attrs,
169 };
170 
171 static const struct attribute_group *isl68137_attribute_groups[] = {
172 	&enable_group,
173 	NULL,
174 };
175 
176 static int raa_dmpvr2_read_word_data(struct i2c_client *client, int page,
177 				     int phase, int reg)
178 {
179 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
180 	const struct isl68137_data *data = to_isl68137_data(info);
181 	int ret;
182 	u64 temp;
183 
184 	switch (reg) {
185 	case PMBUS_VIRT_READ_VMON:
186 		ret = pmbus_read_word_data(client, page, phase,
187 					   RAA_DMPVR2_READ_VMON);
188 		break;
189 	case PMBUS_READ_POUT:
190 	case PMBUS_READ_VOUT:
191 		/*
192 		 * In cases where a voltage divider is attached to the target
193 		 * rail between Vout and the Vsense pin, both Vout and Pout
194 		 * should be scaled by the voltage divider scaling factor.
195 		 * I.e. Vout = Vsense * Rtotal / Rout
196 		 */
197 		ret = pmbus_read_word_data(client, page, phase, reg);
198 		if (ret > 0) {
199 			temp = DIV_U64_ROUND_CLOSEST((u64)ret *
200 				data->channel[page].vout_voltage_divider[1],
201 				data->channel[page].vout_voltage_divider[0]);
202 			ret = clamp_val(temp, 0, 0xffff);
203 		}
204 		break;
205 	default:
206 		ret = -ENODATA;
207 		break;
208 	}
209 
210 	return ret;
211 }
212 
213 static int raa_dmpvr2_write_word_data(struct i2c_client *client, int page,
214 				      int reg, u16 word)
215 {
216 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
217 	const struct isl68137_data *data = to_isl68137_data(info);
218 	int ret;
219 	u64 temp;
220 
221 	switch (reg) {
222 	case PMBUS_VOUT_MAX:
223 	case PMBUS_VOUT_MARGIN_HIGH:
224 	case PMBUS_VOUT_MARGIN_LOW:
225 	case PMBUS_VOUT_OV_FAULT_LIMIT:
226 	case PMBUS_VOUT_UV_FAULT_LIMIT:
227 	case PMBUS_VOUT_COMMAND:
228 		/*
229 		 * In cases where a voltage divider is attached to the target
230 		 * rail between Vout and the Vsense pin, Vout related PMBus
231 		 * commands should be scaled based on the expected voltage
232 		 * at the Vsense pin.
233 		 * I.e. Vsense = Vout * Rout / Rtotal
234 		 */
235 		temp = DIV_U64_ROUND_CLOSEST((u64)word *
236 				data->channel[page].vout_voltage_divider[0],
237 				data->channel[page].vout_voltage_divider[1]);
238 		ret = clamp_val(temp, 0, 0xffff);
239 		break;
240 	default:
241 		ret = -ENODATA;
242 		break;
243 	}
244 	return ret;
245 }
246 
247 static struct pmbus_driver_info raa_dmpvr_info = {
248 	.pages = 3,
249 	.format[PSC_VOLTAGE_IN] = direct,
250 	.format[PSC_VOLTAGE_OUT] = direct,
251 	.format[PSC_CURRENT_IN] = direct,
252 	.format[PSC_CURRENT_OUT] = direct,
253 	.format[PSC_POWER] = direct,
254 	.format[PSC_TEMPERATURE] = direct,
255 	.m[PSC_VOLTAGE_IN] = 1,
256 	.b[PSC_VOLTAGE_IN] = 0,
257 	.R[PSC_VOLTAGE_IN] = 2,
258 	.m[PSC_VOLTAGE_OUT] = 1,
259 	.b[PSC_VOLTAGE_OUT] = 0,
260 	.R[PSC_VOLTAGE_OUT] = 3,
261 	.m[PSC_CURRENT_IN] = 1,
262 	.b[PSC_CURRENT_IN] = 0,
263 	.R[PSC_CURRENT_IN] = 2,
264 	.m[PSC_CURRENT_OUT] = 1,
265 	.b[PSC_CURRENT_OUT] = 0,
266 	.R[PSC_CURRENT_OUT] = 1,
267 	.m[PSC_POWER] = 1,
268 	.b[PSC_POWER] = 0,
269 	.R[PSC_POWER] = 0,
270 	.m[PSC_TEMPERATURE] = 1,
271 	.b[PSC_TEMPERATURE] = 0,
272 	.R[PSC_TEMPERATURE] = 0,
273 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN | PMBUS_HAVE_PIN
274 	    | PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
275 	    | PMBUS_HAVE_TEMP3 | PMBUS_HAVE_STATUS_TEMP
276 	    | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
277 	    | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_POUT
278 		| PMBUS_HAVE_VMON,
279 	.func[1] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT
280 	    | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP3 | PMBUS_HAVE_STATUS_TEMP
281 	    | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_IOUT
282 	    | PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_POUT,
283 	.func[2] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT
284 	    | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP3 | PMBUS_HAVE_STATUS_TEMP
285 	    | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_IOUT
286 	    | PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_POUT,
287 };
288 
289 static int isl68137_probe_child_from_dt(struct device *dev,
290 					struct device_node *child,
291 					struct isl68137_data *data)
292 {
293 	u32 channel, rout, rtotal;
294 	int err;
295 
296 	err = of_property_read_u32(child, "reg", &channel);
297 	if (err) {
298 		dev_err(dev, "missing reg property of %pOFn\n", child);
299 		return err;
300 	}
301 	if (channel >= data->info.pages) {
302 		dev_err(dev, "invalid reg %d of %pOFn\n", channel, child);
303 		return -EINVAL;
304 	}
305 
306 	err = of_property_read_u32_array(child, "vout-voltage-divider",
307 					 data->channel[channel].vout_voltage_divider,
308 					 ARRAY_SIZE(data->channel[channel].vout_voltage_divider));
309 	if (err && err != -EINVAL) {
310 		dev_err(dev,
311 			"malformed vout-voltage-divider value for channel %d\n",
312 			channel);
313 		return err;
314 	}
315 
316 	rout = data->channel[channel].vout_voltage_divider[0];
317 	rtotal = data->channel[channel].vout_voltage_divider[1];
318 	if (rout == 0) {
319 		dev_err(dev,
320 			"Voltage divider output resistance must be greater than 0\n");
321 		return -EINVAL;
322 	}
323 	if (rtotal < rout) {
324 		dev_err(dev,
325 			"Voltage divider total resistance is less than output resistance\n");
326 		return -EINVAL;
327 	}
328 
329 	return 0;
330 }
331 
332 static int isl68137_probe_from_dt(struct device *dev,
333 				  struct isl68137_data *data)
334 {
335 	const struct device_node *np = dev->of_node;
336 	struct device_node *child;
337 	int err;
338 
339 	for_each_child_of_node(np, child) {
340 		if (strcmp(child->name, "channel"))
341 			continue;
342 
343 		err = isl68137_probe_child_from_dt(dev, child, data);
344 		if (err)
345 			return err;
346 	}
347 
348 	return 0;
349 }
350 
351 static int isl68137_probe(struct i2c_client *client)
352 {
353 	struct device *dev = &client->dev;
354 	struct pmbus_driver_info *info;
355 	struct isl68137_data *data;
356 	int i, err;
357 
358 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
359 	if (!data)
360 		return -ENOMEM;
361 
362 	/*
363 	 * Initialize all voltage dividers to Rout=1 and Rtotal=1 to simplify
364 	 * logic in PMBus word read/write functions
365 	 */
366 	for (i = 0; i < MAX_CHANNELS; i++)
367 		memset(data->channel[i].vout_voltage_divider,
368 		       1,
369 		       sizeof(data->channel[i].vout_voltage_divider));
370 
371 	memcpy(&data->info, &raa_dmpvr_info, sizeof(data->info));
372 	info = &data->info;
373 
374 	switch (i2c_match_id(raa_dmpvr_id, client)->driver_data) {
375 	case raa_dmpvr1_2rail:
376 		info->pages = 2;
377 		info->R[PSC_VOLTAGE_IN] = 3;
378 		info->func[0] &= ~PMBUS_HAVE_VMON;
379 		info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
380 		    | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
381 		    | PMBUS_HAVE_POUT;
382 		info->read_word_data = raa_dmpvr2_read_word_data;
383 		info->write_word_data = raa_dmpvr2_write_word_data;
384 		info->groups = isl68137_attribute_groups;
385 		break;
386 	case raa_dmpvr2_1rail:
387 		info->pages = 1;
388 		info->read_word_data = raa_dmpvr2_read_word_data;
389 		info->write_word_data = raa_dmpvr2_write_word_data;
390 		break;
391 	case raa_dmpvr2_2rail_nontc:
392 		info->func[0] &= ~PMBUS_HAVE_TEMP3;
393 		info->func[1] &= ~PMBUS_HAVE_TEMP3;
394 		fallthrough;
395 	case raa_dmpvr2_2rail:
396 		info->pages = 2;
397 		info->read_word_data = raa_dmpvr2_read_word_data;
398 		info->write_word_data = raa_dmpvr2_write_word_data;
399 		break;
400 	case raa_dmpvr2_3rail:
401 		info->read_word_data = raa_dmpvr2_read_word_data;
402 		info->write_word_data = raa_dmpvr2_write_word_data;
403 		break;
404 	case raa_dmpvr2_hv:
405 		info->pages = 1;
406 		info->R[PSC_VOLTAGE_IN] = 1;
407 		info->m[PSC_VOLTAGE_OUT] = 2;
408 		info->R[PSC_VOLTAGE_OUT] = 2;
409 		info->m[PSC_CURRENT_IN] = 2;
410 		info->m[PSC_POWER] = 2;
411 		info->R[PSC_POWER] = -1;
412 		info->read_word_data = raa_dmpvr2_read_word_data;
413 		info->write_word_data = raa_dmpvr2_write_word_data;
414 		break;
415 	default:
416 		return -ENODEV;
417 	}
418 
419 	err = isl68137_probe_from_dt(dev, data);
420 	if (err)
421 		return err;
422 
423 	return pmbus_do_probe(client, info);
424 }
425 
426 static const struct i2c_device_id raa_dmpvr_id[] = {
427 	{"isl68137", raa_dmpvr1_2rail},
428 	{"isl68220", raa_dmpvr2_2rail},
429 	{"isl68221", raa_dmpvr2_3rail},
430 	{"isl68222", raa_dmpvr2_2rail},
431 	{"isl68223", raa_dmpvr2_2rail},
432 	{"isl68224", raa_dmpvr2_3rail},
433 	{"isl68225", raa_dmpvr2_2rail},
434 	{"isl68226", raa_dmpvr2_3rail},
435 	{"isl68227", raa_dmpvr2_1rail},
436 	{"isl68229", raa_dmpvr2_3rail},
437 	{"isl68233", raa_dmpvr2_2rail},
438 	{"isl68239", raa_dmpvr2_3rail},
439 
440 	{"isl69222", raa_dmpvr2_2rail},
441 	{"isl69223", raa_dmpvr2_3rail},
442 	{"isl69224", raa_dmpvr2_2rail},
443 	{"isl69225", raa_dmpvr2_2rail},
444 	{"isl69227", raa_dmpvr2_3rail},
445 	{"isl69228", raa_dmpvr2_3rail},
446 	{"isl69234", raa_dmpvr2_2rail},
447 	{"isl69236", raa_dmpvr2_2rail},
448 	{"isl69239", raa_dmpvr2_3rail},
449 	{"isl69242", raa_dmpvr2_2rail},
450 	{"isl69243", raa_dmpvr2_1rail},
451 	{"isl69247", raa_dmpvr2_2rail},
452 	{"isl69248", raa_dmpvr2_2rail},
453 	{"isl69254", raa_dmpvr2_2rail},
454 	{"isl69255", raa_dmpvr2_2rail},
455 	{"isl69256", raa_dmpvr2_2rail},
456 	{"isl69259", raa_dmpvr2_2rail},
457 	{"isl69260", raa_dmpvr2_2rail},
458 	{"isl69268", raa_dmpvr2_2rail},
459 	{"isl69269", raa_dmpvr2_3rail},
460 	{"isl69298", raa_dmpvr2_2rail},
461 
462 	{"raa228000", raa_dmpvr2_hv},
463 	{"raa228004", raa_dmpvr2_hv},
464 	{"raa228006", raa_dmpvr2_hv},
465 	{"raa228228", raa_dmpvr2_2rail_nontc},
466 	{"raa229001", raa_dmpvr2_2rail},
467 	{"raa229004", raa_dmpvr2_2rail},
468 	{}
469 };
470 
471 MODULE_DEVICE_TABLE(i2c, raa_dmpvr_id);
472 
473 static const struct of_device_id isl68137_of_match[] = {
474 	{ .compatible = "isil,isl68137", .data = (void *)raa_dmpvr1_2rail },
475 	{ .compatible = "renesas,isl68220", .data = (void *)raa_dmpvr2_2rail },
476 	{ .compatible = "renesas,isl68221", .data = (void *)raa_dmpvr2_3rail },
477 	{ .compatible = "renesas,isl68222", .data = (void *)raa_dmpvr2_2rail },
478 	{ .compatible = "renesas,isl68223", .data = (void *)raa_dmpvr2_2rail },
479 	{ .compatible = "renesas,isl68224", .data = (void *)raa_dmpvr2_3rail },
480 	{ .compatible = "renesas,isl68225", .data = (void *)raa_dmpvr2_2rail },
481 	{ .compatible = "renesas,isl68226", .data = (void *)raa_dmpvr2_3rail },
482 	{ .compatible = "renesas,isl68227", .data = (void *)raa_dmpvr2_1rail },
483 	{ .compatible = "renesas,isl68229", .data = (void *)raa_dmpvr2_3rail },
484 	{ .compatible = "renesas,isl68233", .data = (void *)raa_dmpvr2_2rail },
485 	{ .compatible = "renesas,isl68239", .data = (void *)raa_dmpvr2_3rail },
486 
487 	{ .compatible = "renesas,isl69222", .data = (void *)raa_dmpvr2_2rail },
488 	{ .compatible = "renesas,isl69223", .data = (void *)raa_dmpvr2_3rail },
489 	{ .compatible = "renesas,isl69224", .data = (void *)raa_dmpvr2_2rail },
490 	{ .compatible = "renesas,isl69225", .data = (void *)raa_dmpvr2_2rail },
491 	{ .compatible = "renesas,isl69227", .data = (void *)raa_dmpvr2_3rail },
492 	{ .compatible = "renesas,isl69228", .data = (void *)raa_dmpvr2_3rail },
493 	{ .compatible = "renesas,isl69234", .data = (void *)raa_dmpvr2_2rail },
494 	{ .compatible = "renesas,isl69236", .data = (void *)raa_dmpvr2_2rail },
495 	{ .compatible = "renesas,isl69239", .data = (void *)raa_dmpvr2_3rail },
496 	{ .compatible = "renesas,isl69242", .data = (void *)raa_dmpvr2_2rail },
497 	{ .compatible = "renesas,isl69243", .data = (void *)raa_dmpvr2_1rail },
498 	{ .compatible = "renesas,isl69247", .data = (void *)raa_dmpvr2_2rail },
499 	{ .compatible = "renesas,isl69248", .data = (void *)raa_dmpvr2_2rail },
500 	{ .compatible = "renesas,isl69254", .data = (void *)raa_dmpvr2_2rail },
501 	{ .compatible = "renesas,isl69255", .data = (void *)raa_dmpvr2_2rail },
502 	{ .compatible = "renesas,isl69256", .data = (void *)raa_dmpvr2_2rail },
503 	{ .compatible = "renesas,isl69259", .data = (void *)raa_dmpvr2_2rail },
504 	{ .compatible = "isil,isl69260", .data = (void *)raa_dmpvr2_2rail },
505 	{ .compatible = "renesas,isl69268", .data = (void *)raa_dmpvr2_2rail },
506 	{ .compatible = "isil,isl69269", .data = (void *)raa_dmpvr2_3rail },
507 	{ .compatible = "renesas,isl69298", .data = (void *)raa_dmpvr2_2rail },
508 
509 	{ .compatible = "renesas,raa228000", .data = (void *)raa_dmpvr2_hv },
510 	{ .compatible = "renesas,raa228004", .data = (void *)raa_dmpvr2_hv },
511 	{ .compatible = "renesas,raa228006", .data = (void *)raa_dmpvr2_hv },
512 	{ .compatible = "renesas,raa228228", .data = (void *)raa_dmpvr2_2rail_nontc },
513 	{ .compatible = "renesas,raa229001", .data = (void *)raa_dmpvr2_2rail },
514 	{ .compatible = "renesas,raa229004", .data = (void *)raa_dmpvr2_2rail },
515 	{ },
516 };
517 
518 MODULE_DEVICE_TABLE(of, isl68137_of_match);
519 
520 /* This is the driver that will be inserted */
521 static struct i2c_driver isl68137_driver = {
522 	.driver = {
523 		.name = "isl68137",
524 		.of_match_table = isl68137_of_match,
525 	},
526 	.probe = isl68137_probe,
527 	.id_table = raa_dmpvr_id,
528 };
529 
530 module_i2c_driver(isl68137_driver);
531 
532 MODULE_AUTHOR("Maxim Sloyko <maxims@google.com>");
533 MODULE_DESCRIPTION("PMBus driver for Renesas digital multiphase voltage regulators");
534 MODULE_LICENSE("GPL");
535 MODULE_IMPORT_NS(PMBUS);
536