xref: /openbmc/linux/drivers/hwmon/pmbus/pmbus_core.c (revision dc6a81c3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Hardware monitoring driver for PMBus devices
4  *
5  * Copyright (c) 2010, 2011 Ericsson AB.
6  * Copyright (c) 2012 Guenter Roeck
7  */
8 
9 #include <linux/debugfs.h>
10 #include <linux/kernel.h>
11 #include <linux/math64.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/jiffies.h>
20 #include <linux/pmbus.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include "pmbus.h"
24 
25 /*
26  * Number of additional attribute pointers to allocate
27  * with each call to krealloc
28  */
29 #define PMBUS_ATTR_ALLOC_SIZE	32
30 
31 /*
32  * Index into status register array, per status register group
33  */
34 #define PB_STATUS_BASE		0
35 #define PB_STATUS_VOUT_BASE	(PB_STATUS_BASE + PMBUS_PAGES)
36 #define PB_STATUS_IOUT_BASE	(PB_STATUS_VOUT_BASE + PMBUS_PAGES)
37 #define PB_STATUS_FAN_BASE	(PB_STATUS_IOUT_BASE + PMBUS_PAGES)
38 #define PB_STATUS_FAN34_BASE	(PB_STATUS_FAN_BASE + PMBUS_PAGES)
39 #define PB_STATUS_TEMP_BASE	(PB_STATUS_FAN34_BASE + PMBUS_PAGES)
40 #define PB_STATUS_INPUT_BASE	(PB_STATUS_TEMP_BASE + PMBUS_PAGES)
41 #define PB_STATUS_VMON_BASE	(PB_STATUS_INPUT_BASE + 1)
42 
43 #define PB_NUM_STATUS_REG	(PB_STATUS_VMON_BASE + 1)
44 
45 #define PMBUS_NAME_SIZE		24
46 
47 struct pmbus_sensor {
48 	struct pmbus_sensor *next;
49 	char name[PMBUS_NAME_SIZE];	/* sysfs sensor name */
50 	struct device_attribute attribute;
51 	u8 page;		/* page number */
52 	u16 reg;		/* register */
53 	enum pmbus_sensor_classes class;	/* sensor class */
54 	bool update;		/* runtime sensor update needed */
55 	bool convert;		/* Whether or not to apply linear/vid/direct */
56 	int data;		/* Sensor data.
57 				   Negative if there was a read error */
58 };
59 #define to_pmbus_sensor(_attr) \
60 	container_of(_attr, struct pmbus_sensor, attribute)
61 
62 struct pmbus_boolean {
63 	char name[PMBUS_NAME_SIZE];	/* sysfs boolean name */
64 	struct sensor_device_attribute attribute;
65 	struct pmbus_sensor *s1;
66 	struct pmbus_sensor *s2;
67 };
68 #define to_pmbus_boolean(_attr) \
69 	container_of(_attr, struct pmbus_boolean, attribute)
70 
71 struct pmbus_label {
72 	char name[PMBUS_NAME_SIZE];	/* sysfs label name */
73 	struct device_attribute attribute;
74 	char label[PMBUS_NAME_SIZE];	/* label */
75 };
76 #define to_pmbus_label(_attr) \
77 	container_of(_attr, struct pmbus_label, attribute)
78 
79 struct pmbus_data {
80 	struct device *dev;
81 	struct device *hwmon_dev;
82 
83 	u32 flags;		/* from platform data */
84 
85 	int exponent[PMBUS_PAGES];
86 				/* linear mode: exponent for output voltages */
87 
88 	const struct pmbus_driver_info *info;
89 
90 	int max_attributes;
91 	int num_attributes;
92 	struct attribute_group group;
93 	const struct attribute_group **groups;
94 	struct dentry *debugfs;		/* debugfs device directory */
95 
96 	struct pmbus_sensor *sensors;
97 
98 	struct mutex update_lock;
99 	bool valid;
100 	unsigned long last_updated;	/* in jiffies */
101 
102 	/*
103 	 * A single status register covers multiple attributes,
104 	 * so we keep them all together.
105 	 */
106 	u16 status[PB_NUM_STATUS_REG];
107 
108 	bool has_status_word;		/* device uses STATUS_WORD register */
109 	int (*read_status)(struct i2c_client *client, int page);
110 
111 	u8 currpage;
112 };
113 
114 struct pmbus_debugfs_entry {
115 	struct i2c_client *client;
116 	u8 page;
117 	u8 reg;
118 };
119 
120 static const int pmbus_fan_rpm_mask[] = {
121 	PB_FAN_1_RPM,
122 	PB_FAN_2_RPM,
123 	PB_FAN_1_RPM,
124 	PB_FAN_2_RPM,
125 };
126 
127 static const int pmbus_fan_config_registers[] = {
128 	PMBUS_FAN_CONFIG_12,
129 	PMBUS_FAN_CONFIG_12,
130 	PMBUS_FAN_CONFIG_34,
131 	PMBUS_FAN_CONFIG_34
132 };
133 
134 static const int pmbus_fan_command_registers[] = {
135 	PMBUS_FAN_COMMAND_1,
136 	PMBUS_FAN_COMMAND_2,
137 	PMBUS_FAN_COMMAND_3,
138 	PMBUS_FAN_COMMAND_4,
139 };
140 
141 void pmbus_clear_cache(struct i2c_client *client)
142 {
143 	struct pmbus_data *data = i2c_get_clientdata(client);
144 
145 	data->valid = false;
146 }
147 EXPORT_SYMBOL_GPL(pmbus_clear_cache);
148 
149 int pmbus_set_page(struct i2c_client *client, int page)
150 {
151 	struct pmbus_data *data = i2c_get_clientdata(client);
152 	int rv;
153 
154 	if (page < 0 || page == data->currpage)
155 		return 0;
156 
157 	if (!(data->info->func[page] & PMBUS_PAGE_VIRTUAL)) {
158 		rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
159 		if (rv < 0)
160 			return rv;
161 
162 		rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
163 		if (rv < 0)
164 			return rv;
165 
166 		if (rv != page)
167 			return -EIO;
168 	}
169 
170 	data->currpage = page;
171 
172 	return 0;
173 }
174 EXPORT_SYMBOL_GPL(pmbus_set_page);
175 
176 int pmbus_write_byte(struct i2c_client *client, int page, u8 value)
177 {
178 	int rv;
179 
180 	rv = pmbus_set_page(client, page);
181 	if (rv < 0)
182 		return rv;
183 
184 	return i2c_smbus_write_byte(client, value);
185 }
186 EXPORT_SYMBOL_GPL(pmbus_write_byte);
187 
188 /*
189  * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
190  * a device specific mapping function exists and calls it if necessary.
191  */
192 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value)
193 {
194 	struct pmbus_data *data = i2c_get_clientdata(client);
195 	const struct pmbus_driver_info *info = data->info;
196 	int status;
197 
198 	if (info->write_byte) {
199 		status = info->write_byte(client, page, value);
200 		if (status != -ENODATA)
201 			return status;
202 	}
203 	return pmbus_write_byte(client, page, value);
204 }
205 
206 int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg,
207 			  u16 word)
208 {
209 	int rv;
210 
211 	rv = pmbus_set_page(client, page);
212 	if (rv < 0)
213 		return rv;
214 
215 	return i2c_smbus_write_word_data(client, reg, word);
216 }
217 EXPORT_SYMBOL_GPL(pmbus_write_word_data);
218 
219 
220 static int pmbus_write_virt_reg(struct i2c_client *client, int page, int reg,
221 				u16 word)
222 {
223 	int bit;
224 	int id;
225 	int rv;
226 
227 	switch (reg) {
228 	case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4:
229 		id = reg - PMBUS_VIRT_FAN_TARGET_1;
230 		bit = pmbus_fan_rpm_mask[id];
231 		rv = pmbus_update_fan(client, page, id, bit, bit, word);
232 		break;
233 	default:
234 		rv = -ENXIO;
235 		break;
236 	}
237 
238 	return rv;
239 }
240 
241 /*
242  * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
243  * a device specific mapping function exists and calls it if necessary.
244  */
245 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg,
246 				  u16 word)
247 {
248 	struct pmbus_data *data = i2c_get_clientdata(client);
249 	const struct pmbus_driver_info *info = data->info;
250 	int status;
251 
252 	if (info->write_word_data) {
253 		status = info->write_word_data(client, page, reg, word);
254 		if (status != -ENODATA)
255 			return status;
256 	}
257 
258 	if (reg >= PMBUS_VIRT_BASE)
259 		return pmbus_write_virt_reg(client, page, reg, word);
260 
261 	return pmbus_write_word_data(client, page, reg, word);
262 }
263 
264 int pmbus_update_fan(struct i2c_client *client, int page, int id,
265 		     u8 config, u8 mask, u16 command)
266 {
267 	int from;
268 	int rv;
269 	u8 to;
270 
271 	from = pmbus_read_byte_data(client, page,
272 				    pmbus_fan_config_registers[id]);
273 	if (from < 0)
274 		return from;
275 
276 	to = (from & ~mask) | (config & mask);
277 	if (to != from) {
278 		rv = pmbus_write_byte_data(client, page,
279 					   pmbus_fan_config_registers[id], to);
280 		if (rv < 0)
281 			return rv;
282 	}
283 
284 	return _pmbus_write_word_data(client, page,
285 				      pmbus_fan_command_registers[id], command);
286 }
287 EXPORT_SYMBOL_GPL(pmbus_update_fan);
288 
289 int pmbus_read_word_data(struct i2c_client *client, int page, u8 reg)
290 {
291 	int rv;
292 
293 	rv = pmbus_set_page(client, page);
294 	if (rv < 0)
295 		return rv;
296 
297 	return i2c_smbus_read_word_data(client, reg);
298 }
299 EXPORT_SYMBOL_GPL(pmbus_read_word_data);
300 
301 static int pmbus_read_virt_reg(struct i2c_client *client, int page, int reg)
302 {
303 	int rv;
304 	int id;
305 
306 	switch (reg) {
307 	case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4:
308 		id = reg - PMBUS_VIRT_FAN_TARGET_1;
309 		rv = pmbus_get_fan_rate_device(client, page, id, rpm);
310 		break;
311 	default:
312 		rv = -ENXIO;
313 		break;
314 	}
315 
316 	return rv;
317 }
318 
319 /*
320  * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
321  * a device specific mapping function exists and calls it if necessary.
322  */
323 static int _pmbus_read_word_data(struct i2c_client *client, int page, int reg)
324 {
325 	struct pmbus_data *data = i2c_get_clientdata(client);
326 	const struct pmbus_driver_info *info = data->info;
327 	int status;
328 
329 	if (info->read_word_data) {
330 		status = info->read_word_data(client, page, reg);
331 		if (status != -ENODATA)
332 			return status;
333 	}
334 
335 	if (reg >= PMBUS_VIRT_BASE)
336 		return pmbus_read_virt_reg(client, page, reg);
337 
338 	return pmbus_read_word_data(client, page, reg);
339 }
340 
341 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg)
342 {
343 	int rv;
344 
345 	rv = pmbus_set_page(client, page);
346 	if (rv < 0)
347 		return rv;
348 
349 	return i2c_smbus_read_byte_data(client, reg);
350 }
351 EXPORT_SYMBOL_GPL(pmbus_read_byte_data);
352 
353 int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value)
354 {
355 	int rv;
356 
357 	rv = pmbus_set_page(client, page);
358 	if (rv < 0)
359 		return rv;
360 
361 	return i2c_smbus_write_byte_data(client, reg, value);
362 }
363 EXPORT_SYMBOL_GPL(pmbus_write_byte_data);
364 
365 int pmbus_update_byte_data(struct i2c_client *client, int page, u8 reg,
366 			   u8 mask, u8 value)
367 {
368 	unsigned int tmp;
369 	int rv;
370 
371 	rv = pmbus_read_byte_data(client, page, reg);
372 	if (rv < 0)
373 		return rv;
374 
375 	tmp = (rv & ~mask) | (value & mask);
376 
377 	if (tmp != rv)
378 		rv = pmbus_write_byte_data(client, page, reg, tmp);
379 
380 	return rv;
381 }
382 EXPORT_SYMBOL_GPL(pmbus_update_byte_data);
383 
384 /*
385  * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
386  * a device specific mapping function exists and calls it if necessary.
387  */
388 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
389 {
390 	struct pmbus_data *data = i2c_get_clientdata(client);
391 	const struct pmbus_driver_info *info = data->info;
392 	int status;
393 
394 	if (info->read_byte_data) {
395 		status = info->read_byte_data(client, page, reg);
396 		if (status != -ENODATA)
397 			return status;
398 	}
399 	return pmbus_read_byte_data(client, page, reg);
400 }
401 
402 static struct pmbus_sensor *pmbus_find_sensor(struct pmbus_data *data, int page,
403 					      int reg)
404 {
405 	struct pmbus_sensor *sensor;
406 
407 	for (sensor = data->sensors; sensor; sensor = sensor->next) {
408 		if (sensor->page == page && sensor->reg == reg)
409 			return sensor;
410 	}
411 
412 	return ERR_PTR(-EINVAL);
413 }
414 
415 static int pmbus_get_fan_rate(struct i2c_client *client, int page, int id,
416 			      enum pmbus_fan_mode mode,
417 			      bool from_cache)
418 {
419 	struct pmbus_data *data = i2c_get_clientdata(client);
420 	bool want_rpm, have_rpm;
421 	struct pmbus_sensor *s;
422 	int config;
423 	int reg;
424 
425 	want_rpm = (mode == rpm);
426 
427 	if (from_cache) {
428 		reg = want_rpm ? PMBUS_VIRT_FAN_TARGET_1 : PMBUS_VIRT_PWM_1;
429 		s = pmbus_find_sensor(data, page, reg + id);
430 		if (IS_ERR(s))
431 			return PTR_ERR(s);
432 
433 		return s->data;
434 	}
435 
436 	config = pmbus_read_byte_data(client, page,
437 				      pmbus_fan_config_registers[id]);
438 	if (config < 0)
439 		return config;
440 
441 	have_rpm = !!(config & pmbus_fan_rpm_mask[id]);
442 	if (want_rpm == have_rpm)
443 		return pmbus_read_word_data(client, page,
444 					    pmbus_fan_command_registers[id]);
445 
446 	/* Can't sensibly map between RPM and PWM, just return zero */
447 	return 0;
448 }
449 
450 int pmbus_get_fan_rate_device(struct i2c_client *client, int page, int id,
451 			      enum pmbus_fan_mode mode)
452 {
453 	return pmbus_get_fan_rate(client, page, id, mode, false);
454 }
455 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_device);
456 
457 int pmbus_get_fan_rate_cached(struct i2c_client *client, int page, int id,
458 			      enum pmbus_fan_mode mode)
459 {
460 	return pmbus_get_fan_rate(client, page, id, mode, true);
461 }
462 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_cached);
463 
464 static void pmbus_clear_fault_page(struct i2c_client *client, int page)
465 {
466 	_pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
467 }
468 
469 void pmbus_clear_faults(struct i2c_client *client)
470 {
471 	struct pmbus_data *data = i2c_get_clientdata(client);
472 	int i;
473 
474 	for (i = 0; i < data->info->pages; i++)
475 		pmbus_clear_fault_page(client, i);
476 }
477 EXPORT_SYMBOL_GPL(pmbus_clear_faults);
478 
479 static int pmbus_check_status_cml(struct i2c_client *client)
480 {
481 	struct pmbus_data *data = i2c_get_clientdata(client);
482 	int status, status2;
483 
484 	status = data->read_status(client, -1);
485 	if (status < 0 || (status & PB_STATUS_CML)) {
486 		status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
487 		if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
488 			return -EIO;
489 	}
490 	return 0;
491 }
492 
493 static bool pmbus_check_register(struct i2c_client *client,
494 				 int (*func)(struct i2c_client *client,
495 					     int page, int reg),
496 				 int page, int reg)
497 {
498 	int rv;
499 	struct pmbus_data *data = i2c_get_clientdata(client);
500 
501 	rv = func(client, page, reg);
502 	if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
503 		rv = pmbus_check_status_cml(client);
504 	pmbus_clear_fault_page(client, -1);
505 	return rv >= 0;
506 }
507 
508 static bool pmbus_check_status_register(struct i2c_client *client, int page)
509 {
510 	int status;
511 	struct pmbus_data *data = i2c_get_clientdata(client);
512 
513 	status = data->read_status(client, page);
514 	if (status >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK) &&
515 	    (status & PB_STATUS_CML)) {
516 		status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
517 		if (status < 0 || (status & PB_CML_FAULT_INVALID_COMMAND))
518 			status = -EIO;
519 	}
520 
521 	pmbus_clear_fault_page(client, -1);
522 	return status >= 0;
523 }
524 
525 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
526 {
527 	return pmbus_check_register(client, _pmbus_read_byte_data, page, reg);
528 }
529 EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
530 
531 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
532 {
533 	return pmbus_check_register(client, _pmbus_read_word_data, page, reg);
534 }
535 EXPORT_SYMBOL_GPL(pmbus_check_word_register);
536 
537 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
538 {
539 	struct pmbus_data *data = i2c_get_clientdata(client);
540 
541 	return data->info;
542 }
543 EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
544 
545 static struct _pmbus_status {
546 	u32 func;
547 	u16 base;
548 	u16 reg;
549 } pmbus_status[] = {
550 	{ PMBUS_HAVE_STATUS_VOUT, PB_STATUS_VOUT_BASE, PMBUS_STATUS_VOUT },
551 	{ PMBUS_HAVE_STATUS_IOUT, PB_STATUS_IOUT_BASE, PMBUS_STATUS_IOUT },
552 	{ PMBUS_HAVE_STATUS_TEMP, PB_STATUS_TEMP_BASE,
553 	  PMBUS_STATUS_TEMPERATURE },
554 	{ PMBUS_HAVE_STATUS_FAN12, PB_STATUS_FAN_BASE, PMBUS_STATUS_FAN_12 },
555 	{ PMBUS_HAVE_STATUS_FAN34, PB_STATUS_FAN34_BASE, PMBUS_STATUS_FAN_34 },
556 };
557 
558 static struct pmbus_data *pmbus_update_device(struct device *dev)
559 {
560 	struct i2c_client *client = to_i2c_client(dev->parent);
561 	struct pmbus_data *data = i2c_get_clientdata(client);
562 	const struct pmbus_driver_info *info = data->info;
563 	struct pmbus_sensor *sensor;
564 
565 	mutex_lock(&data->update_lock);
566 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
567 		int i, j;
568 
569 		for (i = 0; i < info->pages; i++) {
570 			data->status[PB_STATUS_BASE + i]
571 			    = data->read_status(client, i);
572 			for (j = 0; j < ARRAY_SIZE(pmbus_status); j++) {
573 				struct _pmbus_status *s = &pmbus_status[j];
574 
575 				if (!(info->func[i] & s->func))
576 					continue;
577 				data->status[s->base + i]
578 					= _pmbus_read_byte_data(client, i,
579 								s->reg);
580 			}
581 		}
582 
583 		if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
584 			data->status[PB_STATUS_INPUT_BASE]
585 			  = _pmbus_read_byte_data(client, 0,
586 						  PMBUS_STATUS_INPUT);
587 
588 		if (info->func[0] & PMBUS_HAVE_STATUS_VMON)
589 			data->status[PB_STATUS_VMON_BASE]
590 			  = _pmbus_read_byte_data(client, 0,
591 						  PMBUS_VIRT_STATUS_VMON);
592 
593 		for (sensor = data->sensors; sensor; sensor = sensor->next) {
594 			if (!data->valid || sensor->update)
595 				sensor->data
596 				    = _pmbus_read_word_data(client,
597 							    sensor->page,
598 							    sensor->reg);
599 		}
600 		pmbus_clear_faults(client);
601 		data->last_updated = jiffies;
602 		data->valid = 1;
603 	}
604 	mutex_unlock(&data->update_lock);
605 	return data;
606 }
607 
608 /*
609  * Convert linear sensor values to milli- or micro-units
610  * depending on sensor type.
611  */
612 static long pmbus_reg2data_linear(struct pmbus_data *data,
613 				  struct pmbus_sensor *sensor)
614 {
615 	s16 exponent;
616 	s32 mantissa;
617 	long val;
618 
619 	if (sensor->class == PSC_VOLTAGE_OUT) {	/* LINEAR16 */
620 		exponent = data->exponent[sensor->page];
621 		mantissa = (u16) sensor->data;
622 	} else {				/* LINEAR11 */
623 		exponent = ((s16)sensor->data) >> 11;
624 		mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5;
625 	}
626 
627 	val = mantissa;
628 
629 	/* scale result to milli-units for all sensors except fans */
630 	if (sensor->class != PSC_FAN)
631 		val = val * 1000L;
632 
633 	/* scale result to micro-units for power sensors */
634 	if (sensor->class == PSC_POWER)
635 		val = val * 1000L;
636 
637 	if (exponent >= 0)
638 		val <<= exponent;
639 	else
640 		val >>= -exponent;
641 
642 	return val;
643 }
644 
645 /*
646  * Convert direct sensor values to milli- or micro-units
647  * depending on sensor type.
648  */
649 static long pmbus_reg2data_direct(struct pmbus_data *data,
650 				  struct pmbus_sensor *sensor)
651 {
652 	s64 b, val = (s16)sensor->data;
653 	s32 m, R;
654 
655 	m = data->info->m[sensor->class];
656 	b = data->info->b[sensor->class];
657 	R = data->info->R[sensor->class];
658 
659 	if (m == 0)
660 		return 0;
661 
662 	/* X = 1/m * (Y * 10^-R - b) */
663 	R = -R;
664 	/* scale result to milli-units for everything but fans */
665 	if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) {
666 		R += 3;
667 		b *= 1000;
668 	}
669 
670 	/* scale result to micro-units for power sensors */
671 	if (sensor->class == PSC_POWER) {
672 		R += 3;
673 		b *= 1000;
674 	}
675 
676 	while (R > 0) {
677 		val *= 10;
678 		R--;
679 	}
680 	while (R < 0) {
681 		val = div_s64(val + 5LL, 10L);  /* round closest */
682 		R++;
683 	}
684 
685 	val = div_s64(val - b, m);
686 	return clamp_val(val, LONG_MIN, LONG_MAX);
687 }
688 
689 /*
690  * Convert VID sensor values to milli- or micro-units
691  * depending on sensor type.
692  */
693 static long pmbus_reg2data_vid(struct pmbus_data *data,
694 			       struct pmbus_sensor *sensor)
695 {
696 	long val = sensor->data;
697 	long rv = 0;
698 
699 	switch (data->info->vrm_version[sensor->page]) {
700 	case vr11:
701 		if (val >= 0x02 && val <= 0xb2)
702 			rv = DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100);
703 		break;
704 	case vr12:
705 		if (val >= 0x01)
706 			rv = 250 + (val - 1) * 5;
707 		break;
708 	case vr13:
709 		if (val >= 0x01)
710 			rv = 500 + (val - 1) * 10;
711 		break;
712 	case imvp9:
713 		if (val >= 0x01)
714 			rv = 200 + (val - 1) * 10;
715 		break;
716 	case amd625mv:
717 		if (val >= 0x0 && val <= 0xd8)
718 			rv = DIV_ROUND_CLOSEST(155000 - val * 625, 100);
719 		break;
720 	}
721 	return rv;
722 }
723 
724 static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
725 {
726 	long val;
727 
728 	if (!sensor->convert)
729 		return sensor->data;
730 
731 	switch (data->info->format[sensor->class]) {
732 	case direct:
733 		val = pmbus_reg2data_direct(data, sensor);
734 		break;
735 	case vid:
736 		val = pmbus_reg2data_vid(data, sensor);
737 		break;
738 	case linear:
739 	default:
740 		val = pmbus_reg2data_linear(data, sensor);
741 		break;
742 	}
743 	return val;
744 }
745 
746 #define MAX_MANTISSA	(1023 * 1000)
747 #define MIN_MANTISSA	(511 * 1000)
748 
749 static u16 pmbus_data2reg_linear(struct pmbus_data *data,
750 				 struct pmbus_sensor *sensor, long val)
751 {
752 	s16 exponent = 0, mantissa;
753 	bool negative = false;
754 
755 	/* simple case */
756 	if (val == 0)
757 		return 0;
758 
759 	if (sensor->class == PSC_VOLTAGE_OUT) {
760 		/* LINEAR16 does not support negative voltages */
761 		if (val < 0)
762 			return 0;
763 
764 		/*
765 		 * For a static exponents, we don't have a choice
766 		 * but to adjust the value to it.
767 		 */
768 		if (data->exponent[sensor->page] < 0)
769 			val <<= -data->exponent[sensor->page];
770 		else
771 			val >>= data->exponent[sensor->page];
772 		val = DIV_ROUND_CLOSEST(val, 1000);
773 		return val & 0xffff;
774 	}
775 
776 	if (val < 0) {
777 		negative = true;
778 		val = -val;
779 	}
780 
781 	/* Power is in uW. Convert to mW before converting. */
782 	if (sensor->class == PSC_POWER)
783 		val = DIV_ROUND_CLOSEST(val, 1000L);
784 
785 	/*
786 	 * For simplicity, convert fan data to milli-units
787 	 * before calculating the exponent.
788 	 */
789 	if (sensor->class == PSC_FAN)
790 		val = val * 1000;
791 
792 	/* Reduce large mantissa until it fits into 10 bit */
793 	while (val >= MAX_MANTISSA && exponent < 15) {
794 		exponent++;
795 		val >>= 1;
796 	}
797 	/* Increase small mantissa to improve precision */
798 	while (val < MIN_MANTISSA && exponent > -15) {
799 		exponent--;
800 		val <<= 1;
801 	}
802 
803 	/* Convert mantissa from milli-units to units */
804 	mantissa = DIV_ROUND_CLOSEST(val, 1000);
805 
806 	/* Ensure that resulting number is within range */
807 	if (mantissa > 0x3ff)
808 		mantissa = 0x3ff;
809 
810 	/* restore sign */
811 	if (negative)
812 		mantissa = -mantissa;
813 
814 	/* Convert to 5 bit exponent, 11 bit mantissa */
815 	return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
816 }
817 
818 static u16 pmbus_data2reg_direct(struct pmbus_data *data,
819 				 struct pmbus_sensor *sensor, long val)
820 {
821 	s64 b, val64 = val;
822 	s32 m, R;
823 
824 	m = data->info->m[sensor->class];
825 	b = data->info->b[sensor->class];
826 	R = data->info->R[sensor->class];
827 
828 	/* Power is in uW. Adjust R and b. */
829 	if (sensor->class == PSC_POWER) {
830 		R -= 3;
831 		b *= 1000;
832 	}
833 
834 	/* Calculate Y = (m * X + b) * 10^R */
835 	if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) {
836 		R -= 3;		/* Adjust R and b for data in milli-units */
837 		b *= 1000;
838 	}
839 	val64 = val64 * m + b;
840 
841 	while (R > 0) {
842 		val64 *= 10;
843 		R--;
844 	}
845 	while (R < 0) {
846 		val64 = div_s64(val64 + 5LL, 10L);  /* round closest */
847 		R++;
848 	}
849 
850 	return (u16)clamp_val(val64, S16_MIN, S16_MAX);
851 }
852 
853 static u16 pmbus_data2reg_vid(struct pmbus_data *data,
854 			      struct pmbus_sensor *sensor, long val)
855 {
856 	val = clamp_val(val, 500, 1600);
857 
858 	return 2 + DIV_ROUND_CLOSEST((1600 - val) * 100, 625);
859 }
860 
861 static u16 pmbus_data2reg(struct pmbus_data *data,
862 			  struct pmbus_sensor *sensor, long val)
863 {
864 	u16 regval;
865 
866 	if (!sensor->convert)
867 		return val;
868 
869 	switch (data->info->format[sensor->class]) {
870 	case direct:
871 		regval = pmbus_data2reg_direct(data, sensor, val);
872 		break;
873 	case vid:
874 		regval = pmbus_data2reg_vid(data, sensor, val);
875 		break;
876 	case linear:
877 	default:
878 		regval = pmbus_data2reg_linear(data, sensor, val);
879 		break;
880 	}
881 	return regval;
882 }
883 
884 /*
885  * Return boolean calculated from converted data.
886  * <index> defines a status register index and mask.
887  * The mask is in the lower 8 bits, the register index is in bits 8..23.
888  *
889  * The associated pmbus_boolean structure contains optional pointers to two
890  * sensor attributes. If specified, those attributes are compared against each
891  * other to determine if a limit has been exceeded.
892  *
893  * If the sensor attribute pointers are NULL, the function returns true if
894  * (status[reg] & mask) is true.
895  *
896  * If sensor attribute pointers are provided, a comparison against a specified
897  * limit has to be performed to determine the boolean result.
898  * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
899  * sensor values referenced by sensor attribute pointers s1 and s2).
900  *
901  * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
902  * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
903  *
904  * If a negative value is stored in any of the referenced registers, this value
905  * reflects an error code which will be returned.
906  */
907 static int pmbus_get_boolean(struct pmbus_data *data, struct pmbus_boolean *b,
908 			     int index)
909 {
910 	struct pmbus_sensor *s1 = b->s1;
911 	struct pmbus_sensor *s2 = b->s2;
912 	u16 reg = (index >> 16) & 0xffff;
913 	u16 mask = index & 0xffff;
914 	int ret, status;
915 	u16 regval;
916 
917 	status = data->status[reg];
918 	if (status < 0)
919 		return status;
920 
921 	regval = status & mask;
922 	if (!s1 && !s2) {
923 		ret = !!regval;
924 	} else if (!s1 || !s2) {
925 		WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b, s1, s2);
926 		return 0;
927 	} else {
928 		long v1, v2;
929 
930 		if (s1->data < 0)
931 			return s1->data;
932 		if (s2->data < 0)
933 			return s2->data;
934 
935 		v1 = pmbus_reg2data(data, s1);
936 		v2 = pmbus_reg2data(data, s2);
937 		ret = !!(regval && v1 >= v2);
938 	}
939 	return ret;
940 }
941 
942 static ssize_t pmbus_show_boolean(struct device *dev,
943 				  struct device_attribute *da, char *buf)
944 {
945 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
946 	struct pmbus_boolean *boolean = to_pmbus_boolean(attr);
947 	struct pmbus_data *data = pmbus_update_device(dev);
948 	int val;
949 
950 	val = pmbus_get_boolean(data, boolean, attr->index);
951 	if (val < 0)
952 		return val;
953 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
954 }
955 
956 static ssize_t pmbus_show_sensor(struct device *dev,
957 				 struct device_attribute *devattr, char *buf)
958 {
959 	struct pmbus_data *data = pmbus_update_device(dev);
960 	struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
961 
962 	if (sensor->data < 0)
963 		return sensor->data;
964 
965 	return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor));
966 }
967 
968 static ssize_t pmbus_set_sensor(struct device *dev,
969 				struct device_attribute *devattr,
970 				const char *buf, size_t count)
971 {
972 	struct i2c_client *client = to_i2c_client(dev->parent);
973 	struct pmbus_data *data = i2c_get_clientdata(client);
974 	struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
975 	ssize_t rv = count;
976 	long val = 0;
977 	int ret;
978 	u16 regval;
979 
980 	if (kstrtol(buf, 10, &val) < 0)
981 		return -EINVAL;
982 
983 	mutex_lock(&data->update_lock);
984 	regval = pmbus_data2reg(data, sensor, val);
985 	ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
986 	if (ret < 0)
987 		rv = ret;
988 	else
989 		sensor->data = regval;
990 	mutex_unlock(&data->update_lock);
991 	return rv;
992 }
993 
994 static ssize_t pmbus_show_label(struct device *dev,
995 				struct device_attribute *da, char *buf)
996 {
997 	struct pmbus_label *label = to_pmbus_label(da);
998 
999 	return snprintf(buf, PAGE_SIZE, "%s\n", label->label);
1000 }
1001 
1002 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
1003 {
1004 	if (data->num_attributes >= data->max_attributes - 1) {
1005 		int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
1006 		void *new_attrs = krealloc(data->group.attrs,
1007 					   new_max_attrs * sizeof(void *),
1008 					   GFP_KERNEL);
1009 		if (!new_attrs)
1010 			return -ENOMEM;
1011 		data->group.attrs = new_attrs;
1012 		data->max_attributes = new_max_attrs;
1013 	}
1014 
1015 	data->group.attrs[data->num_attributes++] = attr;
1016 	data->group.attrs[data->num_attributes] = NULL;
1017 	return 0;
1018 }
1019 
1020 static void pmbus_dev_attr_init(struct device_attribute *dev_attr,
1021 				const char *name,
1022 				umode_t mode,
1023 				ssize_t (*show)(struct device *dev,
1024 						struct device_attribute *attr,
1025 						char *buf),
1026 				ssize_t (*store)(struct device *dev,
1027 						 struct device_attribute *attr,
1028 						 const char *buf, size_t count))
1029 {
1030 	sysfs_attr_init(&dev_attr->attr);
1031 	dev_attr->attr.name = name;
1032 	dev_attr->attr.mode = mode;
1033 	dev_attr->show = show;
1034 	dev_attr->store = store;
1035 }
1036 
1037 static void pmbus_attr_init(struct sensor_device_attribute *a,
1038 			    const char *name,
1039 			    umode_t mode,
1040 			    ssize_t (*show)(struct device *dev,
1041 					    struct device_attribute *attr,
1042 					    char *buf),
1043 			    ssize_t (*store)(struct device *dev,
1044 					     struct device_attribute *attr,
1045 					     const char *buf, size_t count),
1046 			    int idx)
1047 {
1048 	pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store);
1049 	a->index = idx;
1050 }
1051 
1052 static int pmbus_add_boolean(struct pmbus_data *data,
1053 			     const char *name, const char *type, int seq,
1054 			     struct pmbus_sensor *s1,
1055 			     struct pmbus_sensor *s2,
1056 			     u16 reg, u16 mask)
1057 {
1058 	struct pmbus_boolean *boolean;
1059 	struct sensor_device_attribute *a;
1060 
1061 	boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL);
1062 	if (!boolean)
1063 		return -ENOMEM;
1064 
1065 	a = &boolean->attribute;
1066 
1067 	snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
1068 		 name, seq, type);
1069 	boolean->s1 = s1;
1070 	boolean->s2 = s2;
1071 	pmbus_attr_init(a, boolean->name, 0444, pmbus_show_boolean, NULL,
1072 			(reg << 16) | mask);
1073 
1074 	return pmbus_add_attribute(data, &a->dev_attr.attr);
1075 }
1076 
1077 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
1078 					     const char *name, const char *type,
1079 					     int seq, int page, int reg,
1080 					     enum pmbus_sensor_classes class,
1081 					     bool update, bool readonly,
1082 					     bool convert)
1083 {
1084 	struct pmbus_sensor *sensor;
1085 	struct device_attribute *a;
1086 
1087 	sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL);
1088 	if (!sensor)
1089 		return NULL;
1090 	a = &sensor->attribute;
1091 
1092 	if (type)
1093 		snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
1094 			 name, seq, type);
1095 	else
1096 		snprintf(sensor->name, sizeof(sensor->name), "%s%d",
1097 			 name, seq);
1098 
1099 	if (data->flags & PMBUS_WRITE_PROTECTED)
1100 		readonly = true;
1101 
1102 	sensor->page = page;
1103 	sensor->reg = reg;
1104 	sensor->class = class;
1105 	sensor->update = update;
1106 	sensor->convert = convert;
1107 	pmbus_dev_attr_init(a, sensor->name,
1108 			    readonly ? 0444 : 0644,
1109 			    pmbus_show_sensor, pmbus_set_sensor);
1110 
1111 	if (pmbus_add_attribute(data, &a->attr))
1112 		return NULL;
1113 
1114 	sensor->next = data->sensors;
1115 	data->sensors = sensor;
1116 
1117 	return sensor;
1118 }
1119 
1120 static int pmbus_add_label(struct pmbus_data *data,
1121 			   const char *name, int seq,
1122 			   const char *lstring, int index)
1123 {
1124 	struct pmbus_label *label;
1125 	struct device_attribute *a;
1126 
1127 	label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL);
1128 	if (!label)
1129 		return -ENOMEM;
1130 
1131 	a = &label->attribute;
1132 
1133 	snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
1134 	if (!index)
1135 		strncpy(label->label, lstring, sizeof(label->label) - 1);
1136 	else
1137 		snprintf(label->label, sizeof(label->label), "%s%d", lstring,
1138 			 index);
1139 
1140 	pmbus_dev_attr_init(a, label->name, 0444, pmbus_show_label, NULL);
1141 	return pmbus_add_attribute(data, &a->attr);
1142 }
1143 
1144 /*
1145  * Search for attributes. Allocate sensors, booleans, and labels as needed.
1146  */
1147 
1148 /*
1149  * The pmbus_limit_attr structure describes a single limit attribute
1150  * and its associated alarm attribute.
1151  */
1152 struct pmbus_limit_attr {
1153 	u16 reg;		/* Limit register */
1154 	u16 sbit;		/* Alarm attribute status bit */
1155 	bool update;		/* True if register needs updates */
1156 	bool low;		/* True if low limit; for limits with compare
1157 				   functions only */
1158 	const char *attr;	/* Attribute name */
1159 	const char *alarm;	/* Alarm attribute name */
1160 };
1161 
1162 /*
1163  * The pmbus_sensor_attr structure describes one sensor attribute. This
1164  * description includes a reference to the associated limit attributes.
1165  */
1166 struct pmbus_sensor_attr {
1167 	u16 reg;			/* sensor register */
1168 	u16 gbit;			/* generic status bit */
1169 	u8 nlimit;			/* # of limit registers */
1170 	enum pmbus_sensor_classes class;/* sensor class */
1171 	const char *label;		/* sensor label */
1172 	bool paged;			/* true if paged sensor */
1173 	bool update;			/* true if update needed */
1174 	bool compare;			/* true if compare function needed */
1175 	u32 func;			/* sensor mask */
1176 	u32 sfunc;			/* sensor status mask */
1177 	int sbase;			/* status base register */
1178 	const struct pmbus_limit_attr *limit;/* limit registers */
1179 };
1180 
1181 /*
1182  * Add a set of limit attributes and, if supported, the associated
1183  * alarm attributes.
1184  * returns 0 if no alarm register found, 1 if an alarm register was found,
1185  * < 0 on errors.
1186  */
1187 static int pmbus_add_limit_attrs(struct i2c_client *client,
1188 				 struct pmbus_data *data,
1189 				 const struct pmbus_driver_info *info,
1190 				 const char *name, int index, int page,
1191 				 struct pmbus_sensor *base,
1192 				 const struct pmbus_sensor_attr *attr)
1193 {
1194 	const struct pmbus_limit_attr *l = attr->limit;
1195 	int nlimit = attr->nlimit;
1196 	int have_alarm = 0;
1197 	int i, ret;
1198 	struct pmbus_sensor *curr;
1199 
1200 	for (i = 0; i < nlimit; i++) {
1201 		if (pmbus_check_word_register(client, page, l->reg)) {
1202 			curr = pmbus_add_sensor(data, name, l->attr, index,
1203 						page, l->reg, attr->class,
1204 						attr->update || l->update,
1205 						false, true);
1206 			if (!curr)
1207 				return -ENOMEM;
1208 			if (l->sbit && (info->func[page] & attr->sfunc)) {
1209 				ret = pmbus_add_boolean(data, name,
1210 					l->alarm, index,
1211 					attr->compare ?  l->low ? curr : base
1212 						      : NULL,
1213 					attr->compare ? l->low ? base : curr
1214 						      : NULL,
1215 					attr->sbase + page, l->sbit);
1216 				if (ret)
1217 					return ret;
1218 				have_alarm = 1;
1219 			}
1220 		}
1221 		l++;
1222 	}
1223 	return have_alarm;
1224 }
1225 
1226 static int pmbus_add_sensor_attrs_one(struct i2c_client *client,
1227 				      struct pmbus_data *data,
1228 				      const struct pmbus_driver_info *info,
1229 				      const char *name,
1230 				      int index, int page,
1231 				      const struct pmbus_sensor_attr *attr,
1232 				      bool paged)
1233 {
1234 	struct pmbus_sensor *base;
1235 	bool upper = !!(attr->gbit & 0xff00);	/* need to check STATUS_WORD */
1236 	int ret;
1237 
1238 	if (attr->label) {
1239 		ret = pmbus_add_label(data, name, index, attr->label,
1240 				      paged ? page + 1 : 0);
1241 		if (ret)
1242 			return ret;
1243 	}
1244 	base = pmbus_add_sensor(data, name, "input", index, page, attr->reg,
1245 				attr->class, true, true, true);
1246 	if (!base)
1247 		return -ENOMEM;
1248 	if (attr->sfunc) {
1249 		ret = pmbus_add_limit_attrs(client, data, info, name,
1250 					    index, page, base, attr);
1251 		if (ret < 0)
1252 			return ret;
1253 		/*
1254 		 * Add generic alarm attribute only if there are no individual
1255 		 * alarm attributes, if there is a global alarm bit, and if
1256 		 * the generic status register (word or byte, depending on
1257 		 * which global bit is set) for this page is accessible.
1258 		 */
1259 		if (!ret && attr->gbit &&
1260 		    (!upper || (upper && data->has_status_word)) &&
1261 		    pmbus_check_status_register(client, page)) {
1262 			ret = pmbus_add_boolean(data, name, "alarm", index,
1263 						NULL, NULL,
1264 						PB_STATUS_BASE + page,
1265 						attr->gbit);
1266 			if (ret)
1267 				return ret;
1268 		}
1269 	}
1270 	return 0;
1271 }
1272 
1273 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info *info,
1274 				  const struct pmbus_sensor_attr *attr)
1275 {
1276 	int p;
1277 
1278 	if (attr->paged)
1279 		return true;
1280 
1281 	/*
1282 	 * Some attributes may be present on more than one page despite
1283 	 * not being marked with the paged attribute. If that is the case,
1284 	 * then treat the sensor as being paged and add the page suffix to the
1285 	 * attribute name.
1286 	 * We don't just add the paged attribute to all such attributes, in
1287 	 * order to maintain the un-suffixed labels in the case where the
1288 	 * attribute is only on page 0.
1289 	 */
1290 	for (p = 1; p < info->pages; p++) {
1291 		if (info->func[p] & attr->func)
1292 			return true;
1293 	}
1294 	return false;
1295 }
1296 
1297 static int pmbus_add_sensor_attrs(struct i2c_client *client,
1298 				  struct pmbus_data *data,
1299 				  const char *name,
1300 				  const struct pmbus_sensor_attr *attrs,
1301 				  int nattrs)
1302 {
1303 	const struct pmbus_driver_info *info = data->info;
1304 	int index, i;
1305 	int ret;
1306 
1307 	index = 1;
1308 	for (i = 0; i < nattrs; i++) {
1309 		int page, pages;
1310 		bool paged = pmbus_sensor_is_paged(info, attrs);
1311 
1312 		pages = paged ? info->pages : 1;
1313 		for (page = 0; page < pages; page++) {
1314 			if (!(info->func[page] & attrs->func))
1315 				continue;
1316 			ret = pmbus_add_sensor_attrs_one(client, data, info,
1317 							 name, index, page,
1318 							 attrs, paged);
1319 			if (ret)
1320 				return ret;
1321 			index++;
1322 		}
1323 		attrs++;
1324 	}
1325 	return 0;
1326 }
1327 
1328 static const struct pmbus_limit_attr vin_limit_attrs[] = {
1329 	{
1330 		.reg = PMBUS_VIN_UV_WARN_LIMIT,
1331 		.attr = "min",
1332 		.alarm = "min_alarm",
1333 		.sbit = PB_VOLTAGE_UV_WARNING,
1334 	}, {
1335 		.reg = PMBUS_VIN_UV_FAULT_LIMIT,
1336 		.attr = "lcrit",
1337 		.alarm = "lcrit_alarm",
1338 		.sbit = PB_VOLTAGE_UV_FAULT,
1339 	}, {
1340 		.reg = PMBUS_VIN_OV_WARN_LIMIT,
1341 		.attr = "max",
1342 		.alarm = "max_alarm",
1343 		.sbit = PB_VOLTAGE_OV_WARNING,
1344 	}, {
1345 		.reg = PMBUS_VIN_OV_FAULT_LIMIT,
1346 		.attr = "crit",
1347 		.alarm = "crit_alarm",
1348 		.sbit = PB_VOLTAGE_OV_FAULT,
1349 	}, {
1350 		.reg = PMBUS_VIRT_READ_VIN_AVG,
1351 		.update = true,
1352 		.attr = "average",
1353 	}, {
1354 		.reg = PMBUS_VIRT_READ_VIN_MIN,
1355 		.update = true,
1356 		.attr = "lowest",
1357 	}, {
1358 		.reg = PMBUS_VIRT_READ_VIN_MAX,
1359 		.update = true,
1360 		.attr = "highest",
1361 	}, {
1362 		.reg = PMBUS_VIRT_RESET_VIN_HISTORY,
1363 		.attr = "reset_history",
1364 	},
1365 };
1366 
1367 static const struct pmbus_limit_attr vmon_limit_attrs[] = {
1368 	{
1369 		.reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT,
1370 		.attr = "min",
1371 		.alarm = "min_alarm",
1372 		.sbit = PB_VOLTAGE_UV_WARNING,
1373 	}, {
1374 		.reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT,
1375 		.attr = "lcrit",
1376 		.alarm = "lcrit_alarm",
1377 		.sbit = PB_VOLTAGE_UV_FAULT,
1378 	}, {
1379 		.reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT,
1380 		.attr = "max",
1381 		.alarm = "max_alarm",
1382 		.sbit = PB_VOLTAGE_OV_WARNING,
1383 	}, {
1384 		.reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT,
1385 		.attr = "crit",
1386 		.alarm = "crit_alarm",
1387 		.sbit = PB_VOLTAGE_OV_FAULT,
1388 	}
1389 };
1390 
1391 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1392 	{
1393 		.reg = PMBUS_VOUT_UV_WARN_LIMIT,
1394 		.attr = "min",
1395 		.alarm = "min_alarm",
1396 		.sbit = PB_VOLTAGE_UV_WARNING,
1397 	}, {
1398 		.reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1399 		.attr = "lcrit",
1400 		.alarm = "lcrit_alarm",
1401 		.sbit = PB_VOLTAGE_UV_FAULT,
1402 	}, {
1403 		.reg = PMBUS_VOUT_OV_WARN_LIMIT,
1404 		.attr = "max",
1405 		.alarm = "max_alarm",
1406 		.sbit = PB_VOLTAGE_OV_WARNING,
1407 	}, {
1408 		.reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1409 		.attr = "crit",
1410 		.alarm = "crit_alarm",
1411 		.sbit = PB_VOLTAGE_OV_FAULT,
1412 	}, {
1413 		.reg = PMBUS_VIRT_READ_VOUT_AVG,
1414 		.update = true,
1415 		.attr = "average",
1416 	}, {
1417 		.reg = PMBUS_VIRT_READ_VOUT_MIN,
1418 		.update = true,
1419 		.attr = "lowest",
1420 	}, {
1421 		.reg = PMBUS_VIRT_READ_VOUT_MAX,
1422 		.update = true,
1423 		.attr = "highest",
1424 	}, {
1425 		.reg = PMBUS_VIRT_RESET_VOUT_HISTORY,
1426 		.attr = "reset_history",
1427 	}
1428 };
1429 
1430 static const struct pmbus_sensor_attr voltage_attributes[] = {
1431 	{
1432 		.reg = PMBUS_READ_VIN,
1433 		.class = PSC_VOLTAGE_IN,
1434 		.label = "vin",
1435 		.func = PMBUS_HAVE_VIN,
1436 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1437 		.sbase = PB_STATUS_INPUT_BASE,
1438 		.gbit = PB_STATUS_VIN_UV,
1439 		.limit = vin_limit_attrs,
1440 		.nlimit = ARRAY_SIZE(vin_limit_attrs),
1441 	}, {
1442 		.reg = PMBUS_VIRT_READ_VMON,
1443 		.class = PSC_VOLTAGE_IN,
1444 		.label = "vmon",
1445 		.func = PMBUS_HAVE_VMON,
1446 		.sfunc = PMBUS_HAVE_STATUS_VMON,
1447 		.sbase = PB_STATUS_VMON_BASE,
1448 		.limit = vmon_limit_attrs,
1449 		.nlimit = ARRAY_SIZE(vmon_limit_attrs),
1450 	}, {
1451 		.reg = PMBUS_READ_VCAP,
1452 		.class = PSC_VOLTAGE_IN,
1453 		.label = "vcap",
1454 		.func = PMBUS_HAVE_VCAP,
1455 	}, {
1456 		.reg = PMBUS_READ_VOUT,
1457 		.class = PSC_VOLTAGE_OUT,
1458 		.label = "vout",
1459 		.paged = true,
1460 		.func = PMBUS_HAVE_VOUT,
1461 		.sfunc = PMBUS_HAVE_STATUS_VOUT,
1462 		.sbase = PB_STATUS_VOUT_BASE,
1463 		.gbit = PB_STATUS_VOUT_OV,
1464 		.limit = vout_limit_attrs,
1465 		.nlimit = ARRAY_SIZE(vout_limit_attrs),
1466 	}
1467 };
1468 
1469 /* Current attributes */
1470 
1471 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1472 	{
1473 		.reg = PMBUS_IIN_OC_WARN_LIMIT,
1474 		.attr = "max",
1475 		.alarm = "max_alarm",
1476 		.sbit = PB_IIN_OC_WARNING,
1477 	}, {
1478 		.reg = PMBUS_IIN_OC_FAULT_LIMIT,
1479 		.attr = "crit",
1480 		.alarm = "crit_alarm",
1481 		.sbit = PB_IIN_OC_FAULT,
1482 	}, {
1483 		.reg = PMBUS_VIRT_READ_IIN_AVG,
1484 		.update = true,
1485 		.attr = "average",
1486 	}, {
1487 		.reg = PMBUS_VIRT_READ_IIN_MIN,
1488 		.update = true,
1489 		.attr = "lowest",
1490 	}, {
1491 		.reg = PMBUS_VIRT_READ_IIN_MAX,
1492 		.update = true,
1493 		.attr = "highest",
1494 	}, {
1495 		.reg = PMBUS_VIRT_RESET_IIN_HISTORY,
1496 		.attr = "reset_history",
1497 	}
1498 };
1499 
1500 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1501 	{
1502 		.reg = PMBUS_IOUT_OC_WARN_LIMIT,
1503 		.attr = "max",
1504 		.alarm = "max_alarm",
1505 		.sbit = PB_IOUT_OC_WARNING,
1506 	}, {
1507 		.reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1508 		.attr = "lcrit",
1509 		.alarm = "lcrit_alarm",
1510 		.sbit = PB_IOUT_UC_FAULT,
1511 	}, {
1512 		.reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1513 		.attr = "crit",
1514 		.alarm = "crit_alarm",
1515 		.sbit = PB_IOUT_OC_FAULT,
1516 	}, {
1517 		.reg = PMBUS_VIRT_READ_IOUT_AVG,
1518 		.update = true,
1519 		.attr = "average",
1520 	}, {
1521 		.reg = PMBUS_VIRT_READ_IOUT_MIN,
1522 		.update = true,
1523 		.attr = "lowest",
1524 	}, {
1525 		.reg = PMBUS_VIRT_READ_IOUT_MAX,
1526 		.update = true,
1527 		.attr = "highest",
1528 	}, {
1529 		.reg = PMBUS_VIRT_RESET_IOUT_HISTORY,
1530 		.attr = "reset_history",
1531 	}
1532 };
1533 
1534 static const struct pmbus_sensor_attr current_attributes[] = {
1535 	{
1536 		.reg = PMBUS_READ_IIN,
1537 		.class = PSC_CURRENT_IN,
1538 		.label = "iin",
1539 		.func = PMBUS_HAVE_IIN,
1540 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1541 		.sbase = PB_STATUS_INPUT_BASE,
1542 		.gbit = PB_STATUS_INPUT,
1543 		.limit = iin_limit_attrs,
1544 		.nlimit = ARRAY_SIZE(iin_limit_attrs),
1545 	}, {
1546 		.reg = PMBUS_READ_IOUT,
1547 		.class = PSC_CURRENT_OUT,
1548 		.label = "iout",
1549 		.paged = true,
1550 		.func = PMBUS_HAVE_IOUT,
1551 		.sfunc = PMBUS_HAVE_STATUS_IOUT,
1552 		.sbase = PB_STATUS_IOUT_BASE,
1553 		.gbit = PB_STATUS_IOUT_OC,
1554 		.limit = iout_limit_attrs,
1555 		.nlimit = ARRAY_SIZE(iout_limit_attrs),
1556 	}
1557 };
1558 
1559 /* Power attributes */
1560 
1561 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1562 	{
1563 		.reg = PMBUS_PIN_OP_WARN_LIMIT,
1564 		.attr = "max",
1565 		.alarm = "alarm",
1566 		.sbit = PB_PIN_OP_WARNING,
1567 	}, {
1568 		.reg = PMBUS_VIRT_READ_PIN_AVG,
1569 		.update = true,
1570 		.attr = "average",
1571 	}, {
1572 		.reg = PMBUS_VIRT_READ_PIN_MIN,
1573 		.update = true,
1574 		.attr = "input_lowest",
1575 	}, {
1576 		.reg = PMBUS_VIRT_READ_PIN_MAX,
1577 		.update = true,
1578 		.attr = "input_highest",
1579 	}, {
1580 		.reg = PMBUS_VIRT_RESET_PIN_HISTORY,
1581 		.attr = "reset_history",
1582 	}
1583 };
1584 
1585 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1586 	{
1587 		.reg = PMBUS_POUT_MAX,
1588 		.attr = "cap",
1589 		.alarm = "cap_alarm",
1590 		.sbit = PB_POWER_LIMITING,
1591 	}, {
1592 		.reg = PMBUS_POUT_OP_WARN_LIMIT,
1593 		.attr = "max",
1594 		.alarm = "max_alarm",
1595 		.sbit = PB_POUT_OP_WARNING,
1596 	}, {
1597 		.reg = PMBUS_POUT_OP_FAULT_LIMIT,
1598 		.attr = "crit",
1599 		.alarm = "crit_alarm",
1600 		.sbit = PB_POUT_OP_FAULT,
1601 	}, {
1602 		.reg = PMBUS_VIRT_READ_POUT_AVG,
1603 		.update = true,
1604 		.attr = "average",
1605 	}, {
1606 		.reg = PMBUS_VIRT_READ_POUT_MIN,
1607 		.update = true,
1608 		.attr = "input_lowest",
1609 	}, {
1610 		.reg = PMBUS_VIRT_READ_POUT_MAX,
1611 		.update = true,
1612 		.attr = "input_highest",
1613 	}, {
1614 		.reg = PMBUS_VIRT_RESET_POUT_HISTORY,
1615 		.attr = "reset_history",
1616 	}
1617 };
1618 
1619 static const struct pmbus_sensor_attr power_attributes[] = {
1620 	{
1621 		.reg = PMBUS_READ_PIN,
1622 		.class = PSC_POWER,
1623 		.label = "pin",
1624 		.func = PMBUS_HAVE_PIN,
1625 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1626 		.sbase = PB_STATUS_INPUT_BASE,
1627 		.gbit = PB_STATUS_INPUT,
1628 		.limit = pin_limit_attrs,
1629 		.nlimit = ARRAY_SIZE(pin_limit_attrs),
1630 	}, {
1631 		.reg = PMBUS_READ_POUT,
1632 		.class = PSC_POWER,
1633 		.label = "pout",
1634 		.paged = true,
1635 		.func = PMBUS_HAVE_POUT,
1636 		.sfunc = PMBUS_HAVE_STATUS_IOUT,
1637 		.sbase = PB_STATUS_IOUT_BASE,
1638 		.limit = pout_limit_attrs,
1639 		.nlimit = ARRAY_SIZE(pout_limit_attrs),
1640 	}
1641 };
1642 
1643 /* Temperature atributes */
1644 
1645 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1646 	{
1647 		.reg = PMBUS_UT_WARN_LIMIT,
1648 		.low = true,
1649 		.attr = "min",
1650 		.alarm = "min_alarm",
1651 		.sbit = PB_TEMP_UT_WARNING,
1652 	}, {
1653 		.reg = PMBUS_UT_FAULT_LIMIT,
1654 		.low = true,
1655 		.attr = "lcrit",
1656 		.alarm = "lcrit_alarm",
1657 		.sbit = PB_TEMP_UT_FAULT,
1658 	}, {
1659 		.reg = PMBUS_OT_WARN_LIMIT,
1660 		.attr = "max",
1661 		.alarm = "max_alarm",
1662 		.sbit = PB_TEMP_OT_WARNING,
1663 	}, {
1664 		.reg = PMBUS_OT_FAULT_LIMIT,
1665 		.attr = "crit",
1666 		.alarm = "crit_alarm",
1667 		.sbit = PB_TEMP_OT_FAULT,
1668 	}, {
1669 		.reg = PMBUS_VIRT_READ_TEMP_MIN,
1670 		.attr = "lowest",
1671 	}, {
1672 		.reg = PMBUS_VIRT_READ_TEMP_AVG,
1673 		.attr = "average",
1674 	}, {
1675 		.reg = PMBUS_VIRT_READ_TEMP_MAX,
1676 		.attr = "highest",
1677 	}, {
1678 		.reg = PMBUS_VIRT_RESET_TEMP_HISTORY,
1679 		.attr = "reset_history",
1680 	}
1681 };
1682 
1683 static const struct pmbus_limit_attr temp_limit_attrs2[] = {
1684 	{
1685 		.reg = PMBUS_UT_WARN_LIMIT,
1686 		.low = true,
1687 		.attr = "min",
1688 		.alarm = "min_alarm",
1689 		.sbit = PB_TEMP_UT_WARNING,
1690 	}, {
1691 		.reg = PMBUS_UT_FAULT_LIMIT,
1692 		.low = true,
1693 		.attr = "lcrit",
1694 		.alarm = "lcrit_alarm",
1695 		.sbit = PB_TEMP_UT_FAULT,
1696 	}, {
1697 		.reg = PMBUS_OT_WARN_LIMIT,
1698 		.attr = "max",
1699 		.alarm = "max_alarm",
1700 		.sbit = PB_TEMP_OT_WARNING,
1701 	}, {
1702 		.reg = PMBUS_OT_FAULT_LIMIT,
1703 		.attr = "crit",
1704 		.alarm = "crit_alarm",
1705 		.sbit = PB_TEMP_OT_FAULT,
1706 	}, {
1707 		.reg = PMBUS_VIRT_READ_TEMP2_MIN,
1708 		.attr = "lowest",
1709 	}, {
1710 		.reg = PMBUS_VIRT_READ_TEMP2_AVG,
1711 		.attr = "average",
1712 	}, {
1713 		.reg = PMBUS_VIRT_READ_TEMP2_MAX,
1714 		.attr = "highest",
1715 	}, {
1716 		.reg = PMBUS_VIRT_RESET_TEMP2_HISTORY,
1717 		.attr = "reset_history",
1718 	}
1719 };
1720 
1721 static const struct pmbus_limit_attr temp_limit_attrs3[] = {
1722 	{
1723 		.reg = PMBUS_UT_WARN_LIMIT,
1724 		.low = true,
1725 		.attr = "min",
1726 		.alarm = "min_alarm",
1727 		.sbit = PB_TEMP_UT_WARNING,
1728 	}, {
1729 		.reg = PMBUS_UT_FAULT_LIMIT,
1730 		.low = true,
1731 		.attr = "lcrit",
1732 		.alarm = "lcrit_alarm",
1733 		.sbit = PB_TEMP_UT_FAULT,
1734 	}, {
1735 		.reg = PMBUS_OT_WARN_LIMIT,
1736 		.attr = "max",
1737 		.alarm = "max_alarm",
1738 		.sbit = PB_TEMP_OT_WARNING,
1739 	}, {
1740 		.reg = PMBUS_OT_FAULT_LIMIT,
1741 		.attr = "crit",
1742 		.alarm = "crit_alarm",
1743 		.sbit = PB_TEMP_OT_FAULT,
1744 	}
1745 };
1746 
1747 static const struct pmbus_sensor_attr temp_attributes[] = {
1748 	{
1749 		.reg = PMBUS_READ_TEMPERATURE_1,
1750 		.class = PSC_TEMPERATURE,
1751 		.paged = true,
1752 		.update = true,
1753 		.compare = true,
1754 		.func = PMBUS_HAVE_TEMP,
1755 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1756 		.sbase = PB_STATUS_TEMP_BASE,
1757 		.gbit = PB_STATUS_TEMPERATURE,
1758 		.limit = temp_limit_attrs,
1759 		.nlimit = ARRAY_SIZE(temp_limit_attrs),
1760 	}, {
1761 		.reg = PMBUS_READ_TEMPERATURE_2,
1762 		.class = PSC_TEMPERATURE,
1763 		.paged = true,
1764 		.update = true,
1765 		.compare = true,
1766 		.func = PMBUS_HAVE_TEMP2,
1767 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1768 		.sbase = PB_STATUS_TEMP_BASE,
1769 		.gbit = PB_STATUS_TEMPERATURE,
1770 		.limit = temp_limit_attrs2,
1771 		.nlimit = ARRAY_SIZE(temp_limit_attrs2),
1772 	}, {
1773 		.reg = PMBUS_READ_TEMPERATURE_3,
1774 		.class = PSC_TEMPERATURE,
1775 		.paged = true,
1776 		.update = true,
1777 		.compare = true,
1778 		.func = PMBUS_HAVE_TEMP3,
1779 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1780 		.sbase = PB_STATUS_TEMP_BASE,
1781 		.gbit = PB_STATUS_TEMPERATURE,
1782 		.limit = temp_limit_attrs3,
1783 		.nlimit = ARRAY_SIZE(temp_limit_attrs3),
1784 	}
1785 };
1786 
1787 static const int pmbus_fan_registers[] = {
1788 	PMBUS_READ_FAN_SPEED_1,
1789 	PMBUS_READ_FAN_SPEED_2,
1790 	PMBUS_READ_FAN_SPEED_3,
1791 	PMBUS_READ_FAN_SPEED_4
1792 };
1793 
1794 static const int pmbus_fan_status_registers[] = {
1795 	PMBUS_STATUS_FAN_12,
1796 	PMBUS_STATUS_FAN_12,
1797 	PMBUS_STATUS_FAN_34,
1798 	PMBUS_STATUS_FAN_34
1799 };
1800 
1801 static const u32 pmbus_fan_flags[] = {
1802 	PMBUS_HAVE_FAN12,
1803 	PMBUS_HAVE_FAN12,
1804 	PMBUS_HAVE_FAN34,
1805 	PMBUS_HAVE_FAN34
1806 };
1807 
1808 static const u32 pmbus_fan_status_flags[] = {
1809 	PMBUS_HAVE_STATUS_FAN12,
1810 	PMBUS_HAVE_STATUS_FAN12,
1811 	PMBUS_HAVE_STATUS_FAN34,
1812 	PMBUS_HAVE_STATUS_FAN34
1813 };
1814 
1815 /* Fans */
1816 
1817 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */
1818 static int pmbus_add_fan_ctrl(struct i2c_client *client,
1819 		struct pmbus_data *data, int index, int page, int id,
1820 		u8 config)
1821 {
1822 	struct pmbus_sensor *sensor;
1823 
1824 	sensor = pmbus_add_sensor(data, "fan", "target", index, page,
1825 				  PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN,
1826 				  false, false, true);
1827 
1828 	if (!sensor)
1829 		return -ENOMEM;
1830 
1831 	if (!((data->info->func[page] & PMBUS_HAVE_PWM12) ||
1832 			(data->info->func[page] & PMBUS_HAVE_PWM34)))
1833 		return 0;
1834 
1835 	sensor = pmbus_add_sensor(data, "pwm", NULL, index, page,
1836 				  PMBUS_VIRT_PWM_1 + id, PSC_PWM,
1837 				  false, false, true);
1838 
1839 	if (!sensor)
1840 		return -ENOMEM;
1841 
1842 	sensor = pmbus_add_sensor(data, "pwm", "enable", index, page,
1843 				  PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM,
1844 				  true, false, false);
1845 
1846 	if (!sensor)
1847 		return -ENOMEM;
1848 
1849 	return 0;
1850 }
1851 
1852 static int pmbus_add_fan_attributes(struct i2c_client *client,
1853 				    struct pmbus_data *data)
1854 {
1855 	const struct pmbus_driver_info *info = data->info;
1856 	int index = 1;
1857 	int page;
1858 	int ret;
1859 
1860 	for (page = 0; page < info->pages; page++) {
1861 		int f;
1862 
1863 		for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1864 			int regval;
1865 
1866 			if (!(info->func[page] & pmbus_fan_flags[f]))
1867 				break;
1868 
1869 			if (!pmbus_check_word_register(client, page,
1870 						       pmbus_fan_registers[f]))
1871 				break;
1872 
1873 			/*
1874 			 * Skip fan if not installed.
1875 			 * Each fan configuration register covers multiple fans,
1876 			 * so we have to do some magic.
1877 			 */
1878 			regval = _pmbus_read_byte_data(client, page,
1879 				pmbus_fan_config_registers[f]);
1880 			if (regval < 0 ||
1881 			    (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1882 				continue;
1883 
1884 			if (pmbus_add_sensor(data, "fan", "input", index,
1885 					     page, pmbus_fan_registers[f],
1886 					     PSC_FAN, true, true, true) == NULL)
1887 				return -ENOMEM;
1888 
1889 			/* Fan control */
1890 			if (pmbus_check_word_register(client, page,
1891 					pmbus_fan_command_registers[f])) {
1892 				ret = pmbus_add_fan_ctrl(client, data, index,
1893 							 page, f, regval);
1894 				if (ret < 0)
1895 					return ret;
1896 			}
1897 
1898 			/*
1899 			 * Each fan status register covers multiple fans,
1900 			 * so we have to do some magic.
1901 			 */
1902 			if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1903 			    pmbus_check_byte_register(client,
1904 					page, pmbus_fan_status_registers[f])) {
1905 				int base;
1906 
1907 				if (f > 1)	/* fan 3, 4 */
1908 					base = PB_STATUS_FAN34_BASE + page;
1909 				else
1910 					base = PB_STATUS_FAN_BASE + page;
1911 				ret = pmbus_add_boolean(data, "fan",
1912 					"alarm", index, NULL, NULL, base,
1913 					PB_FAN_FAN1_WARNING >> (f & 1));
1914 				if (ret)
1915 					return ret;
1916 				ret = pmbus_add_boolean(data, "fan",
1917 					"fault", index, NULL, NULL, base,
1918 					PB_FAN_FAN1_FAULT >> (f & 1));
1919 				if (ret)
1920 					return ret;
1921 			}
1922 			index++;
1923 		}
1924 	}
1925 	return 0;
1926 }
1927 
1928 struct pmbus_samples_attr {
1929 	int reg;
1930 	char *name;
1931 };
1932 
1933 struct pmbus_samples_reg {
1934 	int page;
1935 	struct pmbus_samples_attr *attr;
1936 	struct device_attribute dev_attr;
1937 };
1938 
1939 static struct pmbus_samples_attr pmbus_samples_registers[] = {
1940 	{
1941 		.reg = PMBUS_VIRT_SAMPLES,
1942 		.name = "samples",
1943 	}, {
1944 		.reg = PMBUS_VIRT_IN_SAMPLES,
1945 		.name = "in_samples",
1946 	}, {
1947 		.reg = PMBUS_VIRT_CURR_SAMPLES,
1948 		.name = "curr_samples",
1949 	}, {
1950 		.reg = PMBUS_VIRT_POWER_SAMPLES,
1951 		.name = "power_samples",
1952 	}, {
1953 		.reg = PMBUS_VIRT_TEMP_SAMPLES,
1954 		.name = "temp_samples",
1955 	}
1956 };
1957 
1958 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr)
1959 
1960 static ssize_t pmbus_show_samples(struct device *dev,
1961 				  struct device_attribute *devattr, char *buf)
1962 {
1963 	int val;
1964 	struct i2c_client *client = to_i2c_client(dev->parent);
1965 	struct pmbus_samples_reg *reg = to_samples_reg(devattr);
1966 
1967 	val = _pmbus_read_word_data(client, reg->page, reg->attr->reg);
1968 	if (val < 0)
1969 		return val;
1970 
1971 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
1972 }
1973 
1974 static ssize_t pmbus_set_samples(struct device *dev,
1975 				 struct device_attribute *devattr,
1976 				 const char *buf, size_t count)
1977 {
1978 	int ret;
1979 	long val;
1980 	struct i2c_client *client = to_i2c_client(dev->parent);
1981 	struct pmbus_samples_reg *reg = to_samples_reg(devattr);
1982 	struct pmbus_data *data = i2c_get_clientdata(client);
1983 
1984 	if (kstrtol(buf, 0, &val) < 0)
1985 		return -EINVAL;
1986 
1987 	mutex_lock(&data->update_lock);
1988 	ret = _pmbus_write_word_data(client, reg->page, reg->attr->reg, val);
1989 	mutex_unlock(&data->update_lock);
1990 
1991 	return ret ? : count;
1992 }
1993 
1994 static int pmbus_add_samples_attr(struct pmbus_data *data, int page,
1995 				  struct pmbus_samples_attr *attr)
1996 {
1997 	struct pmbus_samples_reg *reg;
1998 
1999 	reg = devm_kzalloc(data->dev, sizeof(*reg), GFP_KERNEL);
2000 	if (!reg)
2001 		return -ENOMEM;
2002 
2003 	reg->attr = attr;
2004 	reg->page = page;
2005 
2006 	pmbus_dev_attr_init(&reg->dev_attr, attr->name, 0644,
2007 			    pmbus_show_samples, pmbus_set_samples);
2008 
2009 	return pmbus_add_attribute(data, &reg->dev_attr.attr);
2010 }
2011 
2012 static int pmbus_add_samples_attributes(struct i2c_client *client,
2013 					struct pmbus_data *data)
2014 {
2015 	const struct pmbus_driver_info *info = data->info;
2016 	int s;
2017 
2018 	if (!(info->func[0] & PMBUS_HAVE_SAMPLES))
2019 		return 0;
2020 
2021 	for (s = 0; s < ARRAY_SIZE(pmbus_samples_registers); s++) {
2022 		struct pmbus_samples_attr *attr;
2023 		int ret;
2024 
2025 		attr = &pmbus_samples_registers[s];
2026 		if (!pmbus_check_word_register(client, 0, attr->reg))
2027 			continue;
2028 
2029 		ret = pmbus_add_samples_attr(data, 0, attr);
2030 		if (ret)
2031 			return ret;
2032 	}
2033 
2034 	return 0;
2035 }
2036 
2037 static int pmbus_find_attributes(struct i2c_client *client,
2038 				 struct pmbus_data *data)
2039 {
2040 	int ret;
2041 
2042 	/* Voltage sensors */
2043 	ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
2044 				     ARRAY_SIZE(voltage_attributes));
2045 	if (ret)
2046 		return ret;
2047 
2048 	/* Current sensors */
2049 	ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
2050 				     ARRAY_SIZE(current_attributes));
2051 	if (ret)
2052 		return ret;
2053 
2054 	/* Power sensors */
2055 	ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes,
2056 				     ARRAY_SIZE(power_attributes));
2057 	if (ret)
2058 		return ret;
2059 
2060 	/* Temperature sensors */
2061 	ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
2062 				     ARRAY_SIZE(temp_attributes));
2063 	if (ret)
2064 		return ret;
2065 
2066 	/* Fans */
2067 	ret = pmbus_add_fan_attributes(client, data);
2068 	if (ret)
2069 		return ret;
2070 
2071 	ret = pmbus_add_samples_attributes(client, data);
2072 	return ret;
2073 }
2074 
2075 /*
2076  * Identify chip parameters.
2077  * This function is called for all chips.
2078  */
2079 static int pmbus_identify_common(struct i2c_client *client,
2080 				 struct pmbus_data *data, int page)
2081 {
2082 	int vout_mode = -1;
2083 
2084 	if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE))
2085 		vout_mode = _pmbus_read_byte_data(client, page,
2086 						  PMBUS_VOUT_MODE);
2087 	if (vout_mode >= 0 && vout_mode != 0xff) {
2088 		/*
2089 		 * Not all chips support the VOUT_MODE command,
2090 		 * so a failure to read it is not an error.
2091 		 */
2092 		switch (vout_mode >> 5) {
2093 		case 0:	/* linear mode      */
2094 			if (data->info->format[PSC_VOLTAGE_OUT] != linear)
2095 				return -ENODEV;
2096 
2097 			data->exponent[page] = ((s8)(vout_mode << 3)) >> 3;
2098 			break;
2099 		case 1: /* VID mode         */
2100 			if (data->info->format[PSC_VOLTAGE_OUT] != vid)
2101 				return -ENODEV;
2102 			break;
2103 		case 2:	/* direct mode      */
2104 			if (data->info->format[PSC_VOLTAGE_OUT] != direct)
2105 				return -ENODEV;
2106 			break;
2107 		default:
2108 			return -ENODEV;
2109 		}
2110 	}
2111 
2112 	pmbus_clear_fault_page(client, page);
2113 	return 0;
2114 }
2115 
2116 static int pmbus_read_status_byte(struct i2c_client *client, int page)
2117 {
2118 	return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
2119 }
2120 
2121 static int pmbus_read_status_word(struct i2c_client *client, int page)
2122 {
2123 	return _pmbus_read_word_data(client, page, PMBUS_STATUS_WORD);
2124 }
2125 
2126 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
2127 			     struct pmbus_driver_info *info)
2128 {
2129 	struct device *dev = &client->dev;
2130 	int page, ret;
2131 
2132 	/*
2133 	 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
2134 	 * to use PMBUS_STATUS_BYTE instead if that is the case.
2135 	 * Bail out if both registers are not supported.
2136 	 */
2137 	data->read_status = pmbus_read_status_word;
2138 	ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD);
2139 	if (ret < 0 || ret == 0xffff) {
2140 		data->read_status = pmbus_read_status_byte;
2141 		ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE);
2142 		if (ret < 0 || ret == 0xff) {
2143 			dev_err(dev, "PMBus status register not found\n");
2144 			return -ENODEV;
2145 		}
2146 	} else {
2147 		data->has_status_word = true;
2148 	}
2149 
2150 	/* Enable PEC if the controller supports it */
2151 	ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
2152 	if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK))
2153 		client->flags |= I2C_CLIENT_PEC;
2154 
2155 	/*
2156 	 * Check if the chip is write protected. If it is, we can not clear
2157 	 * faults, and we should not try it. Also, in that case, writes into
2158 	 * limit registers need to be disabled.
2159 	 */
2160 	ret = i2c_smbus_read_byte_data(client, PMBUS_WRITE_PROTECT);
2161 	if (ret > 0 && (ret & PB_WP_ANY))
2162 		data->flags |= PMBUS_WRITE_PROTECTED | PMBUS_SKIP_STATUS_CHECK;
2163 
2164 	if (data->info->pages)
2165 		pmbus_clear_faults(client);
2166 	else
2167 		pmbus_clear_fault_page(client, -1);
2168 
2169 	if (info->identify) {
2170 		ret = (*info->identify)(client, info);
2171 		if (ret < 0) {
2172 			dev_err(dev, "Chip identification failed\n");
2173 			return ret;
2174 		}
2175 	}
2176 
2177 	if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
2178 		dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages);
2179 		return -ENODEV;
2180 	}
2181 
2182 	for (page = 0; page < info->pages; page++) {
2183 		ret = pmbus_identify_common(client, data, page);
2184 		if (ret < 0) {
2185 			dev_err(dev, "Failed to identify chip capabilities\n");
2186 			return ret;
2187 		}
2188 	}
2189 	return 0;
2190 }
2191 
2192 #if IS_ENABLED(CONFIG_REGULATOR)
2193 static int pmbus_regulator_is_enabled(struct regulator_dev *rdev)
2194 {
2195 	struct device *dev = rdev_get_dev(rdev);
2196 	struct i2c_client *client = to_i2c_client(dev->parent);
2197 	u8 page = rdev_get_id(rdev);
2198 	int ret;
2199 
2200 	ret = pmbus_read_byte_data(client, page, PMBUS_OPERATION);
2201 	if (ret < 0)
2202 		return ret;
2203 
2204 	return !!(ret & PB_OPERATION_CONTROL_ON);
2205 }
2206 
2207 static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable)
2208 {
2209 	struct device *dev = rdev_get_dev(rdev);
2210 	struct i2c_client *client = to_i2c_client(dev->parent);
2211 	u8 page = rdev_get_id(rdev);
2212 
2213 	return pmbus_update_byte_data(client, page, PMBUS_OPERATION,
2214 				      PB_OPERATION_CONTROL_ON,
2215 				      enable ? PB_OPERATION_CONTROL_ON : 0);
2216 }
2217 
2218 static int pmbus_regulator_enable(struct regulator_dev *rdev)
2219 {
2220 	return _pmbus_regulator_on_off(rdev, 1);
2221 }
2222 
2223 static int pmbus_regulator_disable(struct regulator_dev *rdev)
2224 {
2225 	return _pmbus_regulator_on_off(rdev, 0);
2226 }
2227 
2228 const struct regulator_ops pmbus_regulator_ops = {
2229 	.enable = pmbus_regulator_enable,
2230 	.disable = pmbus_regulator_disable,
2231 	.is_enabled = pmbus_regulator_is_enabled,
2232 };
2233 EXPORT_SYMBOL_GPL(pmbus_regulator_ops);
2234 
2235 static int pmbus_regulator_register(struct pmbus_data *data)
2236 {
2237 	struct device *dev = data->dev;
2238 	const struct pmbus_driver_info *info = data->info;
2239 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
2240 	struct regulator_dev *rdev;
2241 	int i;
2242 
2243 	for (i = 0; i < info->num_regulators; i++) {
2244 		struct regulator_config config = { };
2245 
2246 		config.dev = dev;
2247 		config.driver_data = data;
2248 
2249 		if (pdata && pdata->reg_init_data)
2250 			config.init_data = &pdata->reg_init_data[i];
2251 
2252 		rdev = devm_regulator_register(dev, &info->reg_desc[i],
2253 					       &config);
2254 		if (IS_ERR(rdev)) {
2255 			dev_err(dev, "Failed to register %s regulator\n",
2256 				info->reg_desc[i].name);
2257 			return PTR_ERR(rdev);
2258 		}
2259 	}
2260 
2261 	return 0;
2262 }
2263 #else
2264 static int pmbus_regulator_register(struct pmbus_data *data)
2265 {
2266 	return 0;
2267 }
2268 #endif
2269 
2270 static struct dentry *pmbus_debugfs_dir;	/* pmbus debugfs directory */
2271 
2272 #if IS_ENABLED(CONFIG_DEBUG_FS)
2273 static int pmbus_debugfs_get(void *data, u64 *val)
2274 {
2275 	int rc;
2276 	struct pmbus_debugfs_entry *entry = data;
2277 
2278 	rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg);
2279 	if (rc < 0)
2280 		return rc;
2281 
2282 	*val = rc;
2283 
2284 	return 0;
2285 }
2286 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL,
2287 			 "0x%02llx\n");
2288 
2289 static int pmbus_debugfs_get_status(void *data, u64 *val)
2290 {
2291 	int rc;
2292 	struct pmbus_debugfs_entry *entry = data;
2293 	struct pmbus_data *pdata = i2c_get_clientdata(entry->client);
2294 
2295 	rc = pdata->read_status(entry->client, entry->page);
2296 	if (rc < 0)
2297 		return rc;
2298 
2299 	*val = rc;
2300 
2301 	return 0;
2302 }
2303 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status,
2304 			 NULL, "0x%04llx\n");
2305 
2306 static int pmbus_init_debugfs(struct i2c_client *client,
2307 			      struct pmbus_data *data)
2308 {
2309 	int i, idx = 0;
2310 	char name[PMBUS_NAME_SIZE];
2311 	struct pmbus_debugfs_entry *entries;
2312 
2313 	if (!pmbus_debugfs_dir)
2314 		return -ENODEV;
2315 
2316 	/*
2317 	 * Create the debugfs directory for this device. Use the hwmon device
2318 	 * name to avoid conflicts (hwmon numbers are globally unique).
2319 	 */
2320 	data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev),
2321 					   pmbus_debugfs_dir);
2322 	if (IS_ERR_OR_NULL(data->debugfs)) {
2323 		data->debugfs = NULL;
2324 		return -ENODEV;
2325 	}
2326 
2327 	/* Allocate the max possible entries we need. */
2328 	entries = devm_kcalloc(data->dev,
2329 			       data->info->pages * 10, sizeof(*entries),
2330 			       GFP_KERNEL);
2331 	if (!entries)
2332 		return -ENOMEM;
2333 
2334 	for (i = 0; i < data->info->pages; ++i) {
2335 		/* Check accessibility of status register if it's not page 0 */
2336 		if (!i || pmbus_check_status_register(client, i)) {
2337 			/* No need to set reg as we have special read op. */
2338 			entries[idx].client = client;
2339 			entries[idx].page = i;
2340 			scnprintf(name, PMBUS_NAME_SIZE, "status%d", i);
2341 			debugfs_create_file(name, 0444, data->debugfs,
2342 					    &entries[idx++],
2343 					    &pmbus_debugfs_ops_status);
2344 		}
2345 
2346 		if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) {
2347 			entries[idx].client = client;
2348 			entries[idx].page = i;
2349 			entries[idx].reg = PMBUS_STATUS_VOUT;
2350 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i);
2351 			debugfs_create_file(name, 0444, data->debugfs,
2352 					    &entries[idx++],
2353 					    &pmbus_debugfs_ops);
2354 		}
2355 
2356 		if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) {
2357 			entries[idx].client = client;
2358 			entries[idx].page = i;
2359 			entries[idx].reg = PMBUS_STATUS_IOUT;
2360 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i);
2361 			debugfs_create_file(name, 0444, data->debugfs,
2362 					    &entries[idx++],
2363 					    &pmbus_debugfs_ops);
2364 		}
2365 
2366 		if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) {
2367 			entries[idx].client = client;
2368 			entries[idx].page = i;
2369 			entries[idx].reg = PMBUS_STATUS_INPUT;
2370 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i);
2371 			debugfs_create_file(name, 0444, data->debugfs,
2372 					    &entries[idx++],
2373 					    &pmbus_debugfs_ops);
2374 		}
2375 
2376 		if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) {
2377 			entries[idx].client = client;
2378 			entries[idx].page = i;
2379 			entries[idx].reg = PMBUS_STATUS_TEMPERATURE;
2380 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i);
2381 			debugfs_create_file(name, 0444, data->debugfs,
2382 					    &entries[idx++],
2383 					    &pmbus_debugfs_ops);
2384 		}
2385 
2386 		if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) {
2387 			entries[idx].client = client;
2388 			entries[idx].page = i;
2389 			entries[idx].reg = PMBUS_STATUS_CML;
2390 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i);
2391 			debugfs_create_file(name, 0444, data->debugfs,
2392 					    &entries[idx++],
2393 					    &pmbus_debugfs_ops);
2394 		}
2395 
2396 		if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) {
2397 			entries[idx].client = client;
2398 			entries[idx].page = i;
2399 			entries[idx].reg = PMBUS_STATUS_OTHER;
2400 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i);
2401 			debugfs_create_file(name, 0444, data->debugfs,
2402 					    &entries[idx++],
2403 					    &pmbus_debugfs_ops);
2404 		}
2405 
2406 		if (pmbus_check_byte_register(client, i,
2407 					      PMBUS_STATUS_MFR_SPECIFIC)) {
2408 			entries[idx].client = client;
2409 			entries[idx].page = i;
2410 			entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC;
2411 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i);
2412 			debugfs_create_file(name, 0444, data->debugfs,
2413 					    &entries[idx++],
2414 					    &pmbus_debugfs_ops);
2415 		}
2416 
2417 		if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) {
2418 			entries[idx].client = client;
2419 			entries[idx].page = i;
2420 			entries[idx].reg = PMBUS_STATUS_FAN_12;
2421 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i);
2422 			debugfs_create_file(name, 0444, data->debugfs,
2423 					    &entries[idx++],
2424 					    &pmbus_debugfs_ops);
2425 		}
2426 
2427 		if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) {
2428 			entries[idx].client = client;
2429 			entries[idx].page = i;
2430 			entries[idx].reg = PMBUS_STATUS_FAN_34;
2431 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i);
2432 			debugfs_create_file(name, 0444, data->debugfs,
2433 					    &entries[idx++],
2434 					    &pmbus_debugfs_ops);
2435 		}
2436 	}
2437 
2438 	return 0;
2439 }
2440 #else
2441 static int pmbus_init_debugfs(struct i2c_client *client,
2442 			      struct pmbus_data *data)
2443 {
2444 	return 0;
2445 }
2446 #endif	/* IS_ENABLED(CONFIG_DEBUG_FS) */
2447 
2448 int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
2449 		   struct pmbus_driver_info *info)
2450 {
2451 	struct device *dev = &client->dev;
2452 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
2453 	struct pmbus_data *data;
2454 	size_t groups_num = 0;
2455 	int ret;
2456 
2457 	if (!info)
2458 		return -ENODEV;
2459 
2460 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
2461 				     | I2C_FUNC_SMBUS_BYTE_DATA
2462 				     | I2C_FUNC_SMBUS_WORD_DATA))
2463 		return -ENODEV;
2464 
2465 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
2466 	if (!data)
2467 		return -ENOMEM;
2468 
2469 	if (info->groups)
2470 		while (info->groups[groups_num])
2471 			groups_num++;
2472 
2473 	data->groups = devm_kcalloc(dev, groups_num + 2, sizeof(void *),
2474 				    GFP_KERNEL);
2475 	if (!data->groups)
2476 		return -ENOMEM;
2477 
2478 	i2c_set_clientdata(client, data);
2479 	mutex_init(&data->update_lock);
2480 	data->dev = dev;
2481 
2482 	if (pdata)
2483 		data->flags = pdata->flags;
2484 	data->info = info;
2485 
2486 	ret = pmbus_init_common(client, data, info);
2487 	if (ret < 0)
2488 		return ret;
2489 
2490 	ret = pmbus_find_attributes(client, data);
2491 	if (ret)
2492 		goto out_kfree;
2493 
2494 	/*
2495 	 * If there are no attributes, something is wrong.
2496 	 * Bail out instead of trying to register nothing.
2497 	 */
2498 	if (!data->num_attributes) {
2499 		dev_err(dev, "No attributes found\n");
2500 		ret = -ENODEV;
2501 		goto out_kfree;
2502 	}
2503 
2504 	data->groups[0] = &data->group;
2505 	memcpy(data->groups + 1, info->groups, sizeof(void *) * groups_num);
2506 	data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
2507 							    data, data->groups);
2508 	if (IS_ERR(data->hwmon_dev)) {
2509 		ret = PTR_ERR(data->hwmon_dev);
2510 		dev_err(dev, "Failed to register hwmon device\n");
2511 		goto out_kfree;
2512 	}
2513 
2514 	ret = pmbus_regulator_register(data);
2515 	if (ret)
2516 		goto out_unregister;
2517 
2518 	ret = pmbus_init_debugfs(client, data);
2519 	if (ret)
2520 		dev_warn(dev, "Failed to register debugfs\n");
2521 
2522 	return 0;
2523 
2524 out_unregister:
2525 	hwmon_device_unregister(data->hwmon_dev);
2526 out_kfree:
2527 	kfree(data->group.attrs);
2528 	return ret;
2529 }
2530 EXPORT_SYMBOL_GPL(pmbus_do_probe);
2531 
2532 int pmbus_do_remove(struct i2c_client *client)
2533 {
2534 	struct pmbus_data *data = i2c_get_clientdata(client);
2535 
2536 	debugfs_remove_recursive(data->debugfs);
2537 
2538 	hwmon_device_unregister(data->hwmon_dev);
2539 	kfree(data->group.attrs);
2540 	return 0;
2541 }
2542 EXPORT_SYMBOL_GPL(pmbus_do_remove);
2543 
2544 struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client)
2545 {
2546 	struct pmbus_data *data = i2c_get_clientdata(client);
2547 
2548 	return data->debugfs;
2549 }
2550 EXPORT_SYMBOL_GPL(pmbus_get_debugfs_dir);
2551 
2552 static int __init pmbus_core_init(void)
2553 {
2554 	pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL);
2555 	if (IS_ERR(pmbus_debugfs_dir))
2556 		pmbus_debugfs_dir = NULL;
2557 
2558 	return 0;
2559 }
2560 
2561 static void __exit pmbus_core_exit(void)
2562 {
2563 	debugfs_remove_recursive(pmbus_debugfs_dir);
2564 }
2565 
2566 module_init(pmbus_core_init);
2567 module_exit(pmbus_core_exit);
2568 
2569 MODULE_AUTHOR("Guenter Roeck");
2570 MODULE_DESCRIPTION("PMBus core driver");
2571 MODULE_LICENSE("GPL");
2572