xref: /openbmc/linux/drivers/hwmon/pmbus/pmbus_core.c (revision 12eb4683)
1 /*
2  * Hardware monitoring driver for PMBus devices
3  *
4  * Copyright (c) 2010, 2011 Ericsson AB.
5  * Copyright (c) 2012 Guenter Roeck
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/err.h>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
28 #include <linux/hwmon.h>
29 #include <linux/hwmon-sysfs.h>
30 #include <linux/jiffies.h>
31 #include <linux/i2c/pmbus.h>
32 #include "pmbus.h"
33 
34 /*
35  * Number of additional attribute pointers to allocate
36  * with each call to krealloc
37  */
38 #define PMBUS_ATTR_ALLOC_SIZE	32
39 
40 /*
41  * Index into status register array, per status register group
42  */
43 #define PB_STATUS_BASE		0
44 #define PB_STATUS_VOUT_BASE	(PB_STATUS_BASE + PMBUS_PAGES)
45 #define PB_STATUS_IOUT_BASE	(PB_STATUS_VOUT_BASE + PMBUS_PAGES)
46 #define PB_STATUS_FAN_BASE	(PB_STATUS_IOUT_BASE + PMBUS_PAGES)
47 #define PB_STATUS_FAN34_BASE	(PB_STATUS_FAN_BASE + PMBUS_PAGES)
48 #define PB_STATUS_TEMP_BASE	(PB_STATUS_FAN34_BASE + PMBUS_PAGES)
49 #define PB_STATUS_INPUT_BASE	(PB_STATUS_TEMP_BASE + PMBUS_PAGES)
50 #define PB_STATUS_VMON_BASE	(PB_STATUS_INPUT_BASE + 1)
51 
52 #define PB_NUM_STATUS_REG	(PB_STATUS_VMON_BASE + 1)
53 
54 #define PMBUS_NAME_SIZE		24
55 
56 struct pmbus_sensor {
57 	struct pmbus_sensor *next;
58 	char name[PMBUS_NAME_SIZE];	/* sysfs sensor name */
59 	struct device_attribute attribute;
60 	u8 page;		/* page number */
61 	u16 reg;		/* register */
62 	enum pmbus_sensor_classes class;	/* sensor class */
63 	bool update;		/* runtime sensor update needed */
64 	int data;		/* Sensor data.
65 				   Negative if there was a read error */
66 };
67 #define to_pmbus_sensor(_attr) \
68 	container_of(_attr, struct pmbus_sensor, attribute)
69 
70 struct pmbus_boolean {
71 	char name[PMBUS_NAME_SIZE];	/* sysfs boolean name */
72 	struct sensor_device_attribute attribute;
73 	struct pmbus_sensor *s1;
74 	struct pmbus_sensor *s2;
75 };
76 #define to_pmbus_boolean(_attr) \
77 	container_of(_attr, struct pmbus_boolean, attribute)
78 
79 struct pmbus_label {
80 	char name[PMBUS_NAME_SIZE];	/* sysfs label name */
81 	struct device_attribute attribute;
82 	char label[PMBUS_NAME_SIZE];	/* label */
83 };
84 #define to_pmbus_label(_attr) \
85 	container_of(_attr, struct pmbus_label, attribute)
86 
87 struct pmbus_data {
88 	struct device *dev;
89 	struct device *hwmon_dev;
90 
91 	u32 flags;		/* from platform data */
92 
93 	int exponent;		/* linear mode: exponent for output voltages */
94 
95 	const struct pmbus_driver_info *info;
96 
97 	int max_attributes;
98 	int num_attributes;
99 	struct attribute_group group;
100 	const struct attribute_group *groups[2];
101 
102 	struct pmbus_sensor *sensors;
103 
104 	struct mutex update_lock;
105 	bool valid;
106 	unsigned long last_updated;	/* in jiffies */
107 
108 	/*
109 	 * A single status register covers multiple attributes,
110 	 * so we keep them all together.
111 	 */
112 	u8 status[PB_NUM_STATUS_REG];
113 	u8 status_register;
114 
115 	u8 currpage;
116 };
117 
118 void pmbus_clear_cache(struct i2c_client *client)
119 {
120 	struct pmbus_data *data = i2c_get_clientdata(client);
121 
122 	data->valid = false;
123 }
124 EXPORT_SYMBOL_GPL(pmbus_clear_cache);
125 
126 int pmbus_set_page(struct i2c_client *client, u8 page)
127 {
128 	struct pmbus_data *data = i2c_get_clientdata(client);
129 	int rv = 0;
130 	int newpage;
131 
132 	if (page != data->currpage) {
133 		rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
134 		newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
135 		if (newpage != page)
136 			rv = -EIO;
137 		else
138 			data->currpage = page;
139 	}
140 	return rv;
141 }
142 EXPORT_SYMBOL_GPL(pmbus_set_page);
143 
144 int pmbus_write_byte(struct i2c_client *client, int page, u8 value)
145 {
146 	int rv;
147 
148 	if (page >= 0) {
149 		rv = pmbus_set_page(client, page);
150 		if (rv < 0)
151 			return rv;
152 	}
153 
154 	return i2c_smbus_write_byte(client, value);
155 }
156 EXPORT_SYMBOL_GPL(pmbus_write_byte);
157 
158 /*
159  * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
160  * a device specific mapping function exists and calls it if necessary.
161  */
162 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value)
163 {
164 	struct pmbus_data *data = i2c_get_clientdata(client);
165 	const struct pmbus_driver_info *info = data->info;
166 	int status;
167 
168 	if (info->write_byte) {
169 		status = info->write_byte(client, page, value);
170 		if (status != -ENODATA)
171 			return status;
172 	}
173 	return pmbus_write_byte(client, page, value);
174 }
175 
176 int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg, u16 word)
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_word_data(client, reg, word);
185 }
186 EXPORT_SYMBOL_GPL(pmbus_write_word_data);
187 
188 /*
189  * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
190  * a device specific mapping function exists and calls it if necessary.
191  */
192 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg,
193 				  u16 word)
194 {
195 	struct pmbus_data *data = i2c_get_clientdata(client);
196 	const struct pmbus_driver_info *info = data->info;
197 	int status;
198 
199 	if (info->write_word_data) {
200 		status = info->write_word_data(client, page, reg, word);
201 		if (status != -ENODATA)
202 			return status;
203 	}
204 	if (reg >= PMBUS_VIRT_BASE)
205 		return -ENXIO;
206 	return pmbus_write_word_data(client, page, reg, word);
207 }
208 
209 int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg)
210 {
211 	int rv;
212 
213 	rv = pmbus_set_page(client, page);
214 	if (rv < 0)
215 		return rv;
216 
217 	return i2c_smbus_read_word_data(client, reg);
218 }
219 EXPORT_SYMBOL_GPL(pmbus_read_word_data);
220 
221 /*
222  * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
223  * a device specific mapping function exists and calls it if necessary.
224  */
225 static int _pmbus_read_word_data(struct i2c_client *client, int page, int reg)
226 {
227 	struct pmbus_data *data = i2c_get_clientdata(client);
228 	const struct pmbus_driver_info *info = data->info;
229 	int status;
230 
231 	if (info->read_word_data) {
232 		status = info->read_word_data(client, page, reg);
233 		if (status != -ENODATA)
234 			return status;
235 	}
236 	if (reg >= PMBUS_VIRT_BASE)
237 		return -ENXIO;
238 	return pmbus_read_word_data(client, page, reg);
239 }
240 
241 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg)
242 {
243 	int rv;
244 
245 	if (page >= 0) {
246 		rv = pmbus_set_page(client, page);
247 		if (rv < 0)
248 			return rv;
249 	}
250 
251 	return i2c_smbus_read_byte_data(client, reg);
252 }
253 EXPORT_SYMBOL_GPL(pmbus_read_byte_data);
254 
255 /*
256  * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
257  * a device specific mapping function exists and calls it if necessary.
258  */
259 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
260 {
261 	struct pmbus_data *data = i2c_get_clientdata(client);
262 	const struct pmbus_driver_info *info = data->info;
263 	int status;
264 
265 	if (info->read_byte_data) {
266 		status = info->read_byte_data(client, page, reg);
267 		if (status != -ENODATA)
268 			return status;
269 	}
270 	return pmbus_read_byte_data(client, page, reg);
271 }
272 
273 static void pmbus_clear_fault_page(struct i2c_client *client, int page)
274 {
275 	_pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
276 }
277 
278 void pmbus_clear_faults(struct i2c_client *client)
279 {
280 	struct pmbus_data *data = i2c_get_clientdata(client);
281 	int i;
282 
283 	for (i = 0; i < data->info->pages; i++)
284 		pmbus_clear_fault_page(client, i);
285 }
286 EXPORT_SYMBOL_GPL(pmbus_clear_faults);
287 
288 static int pmbus_check_status_cml(struct i2c_client *client)
289 {
290 	struct pmbus_data *data = i2c_get_clientdata(client);
291 	int status, status2;
292 
293 	status = _pmbus_read_byte_data(client, -1, data->status_register);
294 	if (status < 0 || (status & PB_STATUS_CML)) {
295 		status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
296 		if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
297 			return -EIO;
298 	}
299 	return 0;
300 }
301 
302 static bool pmbus_check_register(struct i2c_client *client,
303 				 int (*func)(struct i2c_client *client,
304 					     int page, int reg),
305 				 int page, int reg)
306 {
307 	int rv;
308 	struct pmbus_data *data = i2c_get_clientdata(client);
309 
310 	rv = func(client, page, reg);
311 	if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
312 		rv = pmbus_check_status_cml(client);
313 	pmbus_clear_fault_page(client, -1);
314 	return rv >= 0;
315 }
316 
317 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
318 {
319 	return pmbus_check_register(client, _pmbus_read_byte_data, page, reg);
320 }
321 EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
322 
323 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
324 {
325 	return pmbus_check_register(client, _pmbus_read_word_data, page, reg);
326 }
327 EXPORT_SYMBOL_GPL(pmbus_check_word_register);
328 
329 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
330 {
331 	struct pmbus_data *data = i2c_get_clientdata(client);
332 
333 	return data->info;
334 }
335 EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
336 
337 static struct _pmbus_status {
338 	u32 func;
339 	u16 base;
340 	u16 reg;
341 } pmbus_status[] = {
342 	{ PMBUS_HAVE_STATUS_VOUT, PB_STATUS_VOUT_BASE, PMBUS_STATUS_VOUT },
343 	{ PMBUS_HAVE_STATUS_IOUT, PB_STATUS_IOUT_BASE, PMBUS_STATUS_IOUT },
344 	{ PMBUS_HAVE_STATUS_TEMP, PB_STATUS_TEMP_BASE,
345 	  PMBUS_STATUS_TEMPERATURE },
346 	{ PMBUS_HAVE_STATUS_FAN12, PB_STATUS_FAN_BASE, PMBUS_STATUS_FAN_12 },
347 	{ PMBUS_HAVE_STATUS_FAN34, PB_STATUS_FAN34_BASE, PMBUS_STATUS_FAN_34 },
348 };
349 
350 static struct pmbus_data *pmbus_update_device(struct device *dev)
351 {
352 	struct i2c_client *client = to_i2c_client(dev->parent);
353 	struct pmbus_data *data = i2c_get_clientdata(client);
354 	const struct pmbus_driver_info *info = data->info;
355 	struct pmbus_sensor *sensor;
356 
357 	mutex_lock(&data->update_lock);
358 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
359 		int i, j;
360 
361 		for (i = 0; i < info->pages; i++) {
362 			data->status[PB_STATUS_BASE + i]
363 			    = _pmbus_read_byte_data(client, i,
364 						    data->status_register);
365 			for (j = 0; j < ARRAY_SIZE(pmbus_status); j++) {
366 				struct _pmbus_status *s = &pmbus_status[j];
367 
368 				if (!(info->func[i] & s->func))
369 					continue;
370 				data->status[s->base + i]
371 					= _pmbus_read_byte_data(client, i,
372 								s->reg);
373 			}
374 		}
375 
376 		if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
377 			data->status[PB_STATUS_INPUT_BASE]
378 			  = _pmbus_read_byte_data(client, 0,
379 						  PMBUS_STATUS_INPUT);
380 
381 		if (info->func[0] & PMBUS_HAVE_STATUS_VMON)
382 			data->status[PB_STATUS_VMON_BASE]
383 			  = _pmbus_read_byte_data(client, 0,
384 						  PMBUS_VIRT_STATUS_VMON);
385 
386 		for (sensor = data->sensors; sensor; sensor = sensor->next) {
387 			if (!data->valid || sensor->update)
388 				sensor->data
389 				    = _pmbus_read_word_data(client,
390 							    sensor->page,
391 							    sensor->reg);
392 		}
393 		pmbus_clear_faults(client);
394 		data->last_updated = jiffies;
395 		data->valid = 1;
396 	}
397 	mutex_unlock(&data->update_lock);
398 	return data;
399 }
400 
401 /*
402  * Convert linear sensor values to milli- or micro-units
403  * depending on sensor type.
404  */
405 static long pmbus_reg2data_linear(struct pmbus_data *data,
406 				  struct pmbus_sensor *sensor)
407 {
408 	s16 exponent;
409 	s32 mantissa;
410 	long val;
411 
412 	if (sensor->class == PSC_VOLTAGE_OUT) {	/* LINEAR16 */
413 		exponent = data->exponent;
414 		mantissa = (u16) sensor->data;
415 	} else {				/* LINEAR11 */
416 		exponent = ((s16)sensor->data) >> 11;
417 		mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5;
418 	}
419 
420 	val = mantissa;
421 
422 	/* scale result to milli-units for all sensors except fans */
423 	if (sensor->class != PSC_FAN)
424 		val = val * 1000L;
425 
426 	/* scale result to micro-units for power sensors */
427 	if (sensor->class == PSC_POWER)
428 		val = val * 1000L;
429 
430 	if (exponent >= 0)
431 		val <<= exponent;
432 	else
433 		val >>= -exponent;
434 
435 	return val;
436 }
437 
438 /*
439  * Convert direct sensor values to milli- or micro-units
440  * depending on sensor type.
441  */
442 static long pmbus_reg2data_direct(struct pmbus_data *data,
443 				  struct pmbus_sensor *sensor)
444 {
445 	long val = (s16) sensor->data;
446 	long m, b, R;
447 
448 	m = data->info->m[sensor->class];
449 	b = data->info->b[sensor->class];
450 	R = data->info->R[sensor->class];
451 
452 	if (m == 0)
453 		return 0;
454 
455 	/* X = 1/m * (Y * 10^-R - b) */
456 	R = -R;
457 	/* scale result to milli-units for everything but fans */
458 	if (sensor->class != PSC_FAN) {
459 		R += 3;
460 		b *= 1000;
461 	}
462 
463 	/* scale result to micro-units for power sensors */
464 	if (sensor->class == PSC_POWER) {
465 		R += 3;
466 		b *= 1000;
467 	}
468 
469 	while (R > 0) {
470 		val *= 10;
471 		R--;
472 	}
473 	while (R < 0) {
474 		val = DIV_ROUND_CLOSEST(val, 10);
475 		R++;
476 	}
477 
478 	return (val - b) / m;
479 }
480 
481 /*
482  * Convert VID sensor values to milli- or micro-units
483  * depending on sensor type.
484  * We currently only support VR11.
485  */
486 static long pmbus_reg2data_vid(struct pmbus_data *data,
487 			       struct pmbus_sensor *sensor)
488 {
489 	long val = sensor->data;
490 
491 	if (val < 0x02 || val > 0xb2)
492 		return 0;
493 	return DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100);
494 }
495 
496 static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
497 {
498 	long val;
499 
500 	switch (data->info->format[sensor->class]) {
501 	case direct:
502 		val = pmbus_reg2data_direct(data, sensor);
503 		break;
504 	case vid:
505 		val = pmbus_reg2data_vid(data, sensor);
506 		break;
507 	case linear:
508 	default:
509 		val = pmbus_reg2data_linear(data, sensor);
510 		break;
511 	}
512 	return val;
513 }
514 
515 #define MAX_MANTISSA	(1023 * 1000)
516 #define MIN_MANTISSA	(511 * 1000)
517 
518 static u16 pmbus_data2reg_linear(struct pmbus_data *data,
519 				 enum pmbus_sensor_classes class, long val)
520 {
521 	s16 exponent = 0, mantissa;
522 	bool negative = false;
523 
524 	/* simple case */
525 	if (val == 0)
526 		return 0;
527 
528 	if (class == PSC_VOLTAGE_OUT) {
529 		/* LINEAR16 does not support negative voltages */
530 		if (val < 0)
531 			return 0;
532 
533 		/*
534 		 * For a static exponents, we don't have a choice
535 		 * but to adjust the value to it.
536 		 */
537 		if (data->exponent < 0)
538 			val <<= -data->exponent;
539 		else
540 			val >>= data->exponent;
541 		val = DIV_ROUND_CLOSEST(val, 1000);
542 		return val & 0xffff;
543 	}
544 
545 	if (val < 0) {
546 		negative = true;
547 		val = -val;
548 	}
549 
550 	/* Power is in uW. Convert to mW before converting. */
551 	if (class == PSC_POWER)
552 		val = DIV_ROUND_CLOSEST(val, 1000L);
553 
554 	/*
555 	 * For simplicity, convert fan data to milli-units
556 	 * before calculating the exponent.
557 	 */
558 	if (class == PSC_FAN)
559 		val = val * 1000;
560 
561 	/* Reduce large mantissa until it fits into 10 bit */
562 	while (val >= MAX_MANTISSA && exponent < 15) {
563 		exponent++;
564 		val >>= 1;
565 	}
566 	/* Increase small mantissa to improve precision */
567 	while (val < MIN_MANTISSA && exponent > -15) {
568 		exponent--;
569 		val <<= 1;
570 	}
571 
572 	/* Convert mantissa from milli-units to units */
573 	mantissa = DIV_ROUND_CLOSEST(val, 1000);
574 
575 	/* Ensure that resulting number is within range */
576 	if (mantissa > 0x3ff)
577 		mantissa = 0x3ff;
578 
579 	/* restore sign */
580 	if (negative)
581 		mantissa = -mantissa;
582 
583 	/* Convert to 5 bit exponent, 11 bit mantissa */
584 	return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
585 }
586 
587 static u16 pmbus_data2reg_direct(struct pmbus_data *data,
588 				 enum pmbus_sensor_classes class, long val)
589 {
590 	long m, b, R;
591 
592 	m = data->info->m[class];
593 	b = data->info->b[class];
594 	R = data->info->R[class];
595 
596 	/* Power is in uW. Adjust R and b. */
597 	if (class == PSC_POWER) {
598 		R -= 3;
599 		b *= 1000;
600 	}
601 
602 	/* Calculate Y = (m * X + b) * 10^R */
603 	if (class != PSC_FAN) {
604 		R -= 3;		/* Adjust R and b for data in milli-units */
605 		b *= 1000;
606 	}
607 	val = val * m + b;
608 
609 	while (R > 0) {
610 		val *= 10;
611 		R--;
612 	}
613 	while (R < 0) {
614 		val = DIV_ROUND_CLOSEST(val, 10);
615 		R++;
616 	}
617 
618 	return val;
619 }
620 
621 static u16 pmbus_data2reg_vid(struct pmbus_data *data,
622 			      enum pmbus_sensor_classes class, long val)
623 {
624 	val = clamp_val(val, 500, 1600);
625 
626 	return 2 + DIV_ROUND_CLOSEST((1600 - val) * 100, 625);
627 }
628 
629 static u16 pmbus_data2reg(struct pmbus_data *data,
630 			  enum pmbus_sensor_classes class, long val)
631 {
632 	u16 regval;
633 
634 	switch (data->info->format[class]) {
635 	case direct:
636 		regval = pmbus_data2reg_direct(data, class, val);
637 		break;
638 	case vid:
639 		regval = pmbus_data2reg_vid(data, class, val);
640 		break;
641 	case linear:
642 	default:
643 		regval = pmbus_data2reg_linear(data, class, val);
644 		break;
645 	}
646 	return regval;
647 }
648 
649 /*
650  * Return boolean calculated from converted data.
651  * <index> defines a status register index and mask.
652  * The mask is in the lower 8 bits, the register index is in bits 8..23.
653  *
654  * The associated pmbus_boolean structure contains optional pointers to two
655  * sensor attributes. If specified, those attributes are compared against each
656  * other to determine if a limit has been exceeded.
657  *
658  * If the sensor attribute pointers are NULL, the function returns true if
659  * (status[reg] & mask) is true.
660  *
661  * If sensor attribute pointers are provided, a comparison against a specified
662  * limit has to be performed to determine the boolean result.
663  * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
664  * sensor values referenced by sensor attribute pointers s1 and s2).
665  *
666  * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
667  * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
668  *
669  * If a negative value is stored in any of the referenced registers, this value
670  * reflects an error code which will be returned.
671  */
672 static int pmbus_get_boolean(struct pmbus_data *data, struct pmbus_boolean *b,
673 			     int index)
674 {
675 	struct pmbus_sensor *s1 = b->s1;
676 	struct pmbus_sensor *s2 = b->s2;
677 	u16 reg = (index >> 8) & 0xffff;
678 	u8 mask = index & 0xff;
679 	int ret, status;
680 	u8 regval;
681 
682 	status = data->status[reg];
683 	if (status < 0)
684 		return status;
685 
686 	regval = status & mask;
687 	if (!s1 && !s2) {
688 		ret = !!regval;
689 	} else if (!s1 || !s2) {
690 		WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b, s1, s2);
691 		return 0;
692 	} else {
693 		long v1, v2;
694 
695 		if (s1->data < 0)
696 			return s1->data;
697 		if (s2->data < 0)
698 			return s2->data;
699 
700 		v1 = pmbus_reg2data(data, s1);
701 		v2 = pmbus_reg2data(data, s2);
702 		ret = !!(regval && v1 >= v2);
703 	}
704 	return ret;
705 }
706 
707 static ssize_t pmbus_show_boolean(struct device *dev,
708 				  struct device_attribute *da, char *buf)
709 {
710 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
711 	struct pmbus_boolean *boolean = to_pmbus_boolean(attr);
712 	struct pmbus_data *data = pmbus_update_device(dev);
713 	int val;
714 
715 	val = pmbus_get_boolean(data, boolean, attr->index);
716 	if (val < 0)
717 		return val;
718 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
719 }
720 
721 static ssize_t pmbus_show_sensor(struct device *dev,
722 				 struct device_attribute *devattr, char *buf)
723 {
724 	struct pmbus_data *data = pmbus_update_device(dev);
725 	struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
726 
727 	if (sensor->data < 0)
728 		return sensor->data;
729 
730 	return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor));
731 }
732 
733 static ssize_t pmbus_set_sensor(struct device *dev,
734 				struct device_attribute *devattr,
735 				const char *buf, size_t count)
736 {
737 	struct i2c_client *client = to_i2c_client(dev->parent);
738 	struct pmbus_data *data = i2c_get_clientdata(client);
739 	struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
740 	ssize_t rv = count;
741 	long val = 0;
742 	int ret;
743 	u16 regval;
744 
745 	if (kstrtol(buf, 10, &val) < 0)
746 		return -EINVAL;
747 
748 	mutex_lock(&data->update_lock);
749 	regval = pmbus_data2reg(data, sensor->class, val);
750 	ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
751 	if (ret < 0)
752 		rv = ret;
753 	else
754 		sensor->data = regval;
755 	mutex_unlock(&data->update_lock);
756 	return rv;
757 }
758 
759 static ssize_t pmbus_show_label(struct device *dev,
760 				struct device_attribute *da, char *buf)
761 {
762 	struct pmbus_label *label = to_pmbus_label(da);
763 
764 	return snprintf(buf, PAGE_SIZE, "%s\n", label->label);
765 }
766 
767 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
768 {
769 	if (data->num_attributes >= data->max_attributes - 1) {
770 		int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
771 		void *new_attrs = krealloc(data->group.attrs,
772 					   new_max_attrs * sizeof(void *),
773 					   GFP_KERNEL);
774 		if (!new_attrs)
775 			return -ENOMEM;
776 		data->group.attrs = new_attrs;
777 		data->max_attributes = new_max_attrs;
778 	}
779 
780 	data->group.attrs[data->num_attributes++] = attr;
781 	data->group.attrs[data->num_attributes] = NULL;
782 	return 0;
783 }
784 
785 static void pmbus_dev_attr_init(struct device_attribute *dev_attr,
786 				const char *name,
787 				umode_t mode,
788 				ssize_t (*show)(struct device *dev,
789 						struct device_attribute *attr,
790 						char *buf),
791 				ssize_t (*store)(struct device *dev,
792 						 struct device_attribute *attr,
793 						 const char *buf, size_t count))
794 {
795 	sysfs_attr_init(&dev_attr->attr);
796 	dev_attr->attr.name = name;
797 	dev_attr->attr.mode = mode;
798 	dev_attr->show = show;
799 	dev_attr->store = store;
800 }
801 
802 static void pmbus_attr_init(struct sensor_device_attribute *a,
803 			    const char *name,
804 			    umode_t mode,
805 			    ssize_t (*show)(struct device *dev,
806 					    struct device_attribute *attr,
807 					    char *buf),
808 			    ssize_t (*store)(struct device *dev,
809 					     struct device_attribute *attr,
810 					     const char *buf, size_t count),
811 			    int idx)
812 {
813 	pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store);
814 	a->index = idx;
815 }
816 
817 static int pmbus_add_boolean(struct pmbus_data *data,
818 			     const char *name, const char *type, int seq,
819 			     struct pmbus_sensor *s1,
820 			     struct pmbus_sensor *s2,
821 			     u16 reg, u8 mask)
822 {
823 	struct pmbus_boolean *boolean;
824 	struct sensor_device_attribute *a;
825 
826 	boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL);
827 	if (!boolean)
828 		return -ENOMEM;
829 
830 	a = &boolean->attribute;
831 
832 	snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
833 		 name, seq, type);
834 	boolean->s1 = s1;
835 	boolean->s2 = s2;
836 	pmbus_attr_init(a, boolean->name, S_IRUGO, pmbus_show_boolean, NULL,
837 			(reg << 8) | mask);
838 
839 	return pmbus_add_attribute(data, &a->dev_attr.attr);
840 }
841 
842 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
843 					     const char *name, const char *type,
844 					     int seq, int page, int reg,
845 					     enum pmbus_sensor_classes class,
846 					     bool update, bool readonly)
847 {
848 	struct pmbus_sensor *sensor;
849 	struct device_attribute *a;
850 
851 	sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL);
852 	if (!sensor)
853 		return NULL;
854 	a = &sensor->attribute;
855 
856 	snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
857 		 name, seq, type);
858 	sensor->page = page;
859 	sensor->reg = reg;
860 	sensor->class = class;
861 	sensor->update = update;
862 	pmbus_dev_attr_init(a, sensor->name,
863 			    readonly ? S_IRUGO : S_IRUGO | S_IWUSR,
864 			    pmbus_show_sensor, pmbus_set_sensor);
865 
866 	if (pmbus_add_attribute(data, &a->attr))
867 		return NULL;
868 
869 	sensor->next = data->sensors;
870 	data->sensors = sensor;
871 
872 	return sensor;
873 }
874 
875 static int pmbus_add_label(struct pmbus_data *data,
876 			   const char *name, int seq,
877 			   const char *lstring, int index)
878 {
879 	struct pmbus_label *label;
880 	struct device_attribute *a;
881 
882 	label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL);
883 	if (!label)
884 		return -ENOMEM;
885 
886 	a = &label->attribute;
887 
888 	snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
889 	if (!index)
890 		strncpy(label->label, lstring, sizeof(label->label) - 1);
891 	else
892 		snprintf(label->label, sizeof(label->label), "%s%d", lstring,
893 			 index);
894 
895 	pmbus_dev_attr_init(a, label->name, S_IRUGO, pmbus_show_label, NULL);
896 	return pmbus_add_attribute(data, &a->attr);
897 }
898 
899 /*
900  * Search for attributes. Allocate sensors, booleans, and labels as needed.
901  */
902 
903 /*
904  * The pmbus_limit_attr structure describes a single limit attribute
905  * and its associated alarm attribute.
906  */
907 struct pmbus_limit_attr {
908 	u16 reg;		/* Limit register */
909 	u16 sbit;		/* Alarm attribute status bit */
910 	bool update;		/* True if register needs updates */
911 	bool low;		/* True if low limit; for limits with compare
912 				   functions only */
913 	const char *attr;	/* Attribute name */
914 	const char *alarm;	/* Alarm attribute name */
915 };
916 
917 /*
918  * The pmbus_sensor_attr structure describes one sensor attribute. This
919  * description includes a reference to the associated limit attributes.
920  */
921 struct pmbus_sensor_attr {
922 	u16 reg;			/* sensor register */
923 	u8 gbit;			/* generic status bit */
924 	u8 nlimit;			/* # of limit registers */
925 	enum pmbus_sensor_classes class;/* sensor class */
926 	const char *label;		/* sensor label */
927 	bool paged;			/* true if paged sensor */
928 	bool update;			/* true if update needed */
929 	bool compare;			/* true if compare function needed */
930 	u32 func;			/* sensor mask */
931 	u32 sfunc;			/* sensor status mask */
932 	int sbase;			/* status base register */
933 	const struct pmbus_limit_attr *limit;/* limit registers */
934 };
935 
936 /*
937  * Add a set of limit attributes and, if supported, the associated
938  * alarm attributes.
939  * returns 0 if no alarm register found, 1 if an alarm register was found,
940  * < 0 on errors.
941  */
942 static int pmbus_add_limit_attrs(struct i2c_client *client,
943 				 struct pmbus_data *data,
944 				 const struct pmbus_driver_info *info,
945 				 const char *name, int index, int page,
946 				 struct pmbus_sensor *base,
947 				 const struct pmbus_sensor_attr *attr)
948 {
949 	const struct pmbus_limit_attr *l = attr->limit;
950 	int nlimit = attr->nlimit;
951 	int have_alarm = 0;
952 	int i, ret;
953 	struct pmbus_sensor *curr;
954 
955 	for (i = 0; i < nlimit; i++) {
956 		if (pmbus_check_word_register(client, page, l->reg)) {
957 			curr = pmbus_add_sensor(data, name, l->attr, index,
958 						page, l->reg, attr->class,
959 						attr->update || l->update,
960 						false);
961 			if (!curr)
962 				return -ENOMEM;
963 			if (l->sbit && (info->func[page] & attr->sfunc)) {
964 				ret = pmbus_add_boolean(data, name,
965 					l->alarm, index,
966 					attr->compare ?  l->low ? curr : base
967 						      : NULL,
968 					attr->compare ? l->low ? base : curr
969 						      : NULL,
970 					attr->sbase + page, l->sbit);
971 				if (ret)
972 					return ret;
973 				have_alarm = 1;
974 			}
975 		}
976 		l++;
977 	}
978 	return have_alarm;
979 }
980 
981 static int pmbus_add_sensor_attrs_one(struct i2c_client *client,
982 				      struct pmbus_data *data,
983 				      const struct pmbus_driver_info *info,
984 				      const char *name,
985 				      int index, int page,
986 				      const struct pmbus_sensor_attr *attr)
987 {
988 	struct pmbus_sensor *base;
989 	int ret;
990 
991 	if (attr->label) {
992 		ret = pmbus_add_label(data, name, index, attr->label,
993 				      attr->paged ? page + 1 : 0);
994 		if (ret)
995 			return ret;
996 	}
997 	base = pmbus_add_sensor(data, name, "input", index, page, attr->reg,
998 				attr->class, true, true);
999 	if (!base)
1000 		return -ENOMEM;
1001 	if (attr->sfunc) {
1002 		ret = pmbus_add_limit_attrs(client, data, info, name,
1003 					    index, page, base, attr);
1004 		if (ret < 0)
1005 			return ret;
1006 		/*
1007 		 * Add generic alarm attribute only if there are no individual
1008 		 * alarm attributes, if there is a global alarm bit, and if
1009 		 * the generic status register for this page is accessible.
1010 		 */
1011 		if (!ret && attr->gbit &&
1012 		    pmbus_check_byte_register(client, page,
1013 					      data->status_register)) {
1014 			ret = pmbus_add_boolean(data, name, "alarm", index,
1015 						NULL, NULL,
1016 						PB_STATUS_BASE + page,
1017 						attr->gbit);
1018 			if (ret)
1019 				return ret;
1020 		}
1021 	}
1022 	return 0;
1023 }
1024 
1025 static int pmbus_add_sensor_attrs(struct i2c_client *client,
1026 				  struct pmbus_data *data,
1027 				  const char *name,
1028 				  const struct pmbus_sensor_attr *attrs,
1029 				  int nattrs)
1030 {
1031 	const struct pmbus_driver_info *info = data->info;
1032 	int index, i;
1033 	int ret;
1034 
1035 	index = 1;
1036 	for (i = 0; i < nattrs; i++) {
1037 		int page, pages;
1038 
1039 		pages = attrs->paged ? info->pages : 1;
1040 		for (page = 0; page < pages; page++) {
1041 			if (!(info->func[page] & attrs->func))
1042 				continue;
1043 			ret = pmbus_add_sensor_attrs_one(client, data, info,
1044 							 name, index, page,
1045 							 attrs);
1046 			if (ret)
1047 				return ret;
1048 			index++;
1049 		}
1050 		attrs++;
1051 	}
1052 	return 0;
1053 }
1054 
1055 static const struct pmbus_limit_attr vin_limit_attrs[] = {
1056 	{
1057 		.reg = PMBUS_VIN_UV_WARN_LIMIT,
1058 		.attr = "min",
1059 		.alarm = "min_alarm",
1060 		.sbit = PB_VOLTAGE_UV_WARNING,
1061 	}, {
1062 		.reg = PMBUS_VIN_UV_FAULT_LIMIT,
1063 		.attr = "lcrit",
1064 		.alarm = "lcrit_alarm",
1065 		.sbit = PB_VOLTAGE_UV_FAULT,
1066 	}, {
1067 		.reg = PMBUS_VIN_OV_WARN_LIMIT,
1068 		.attr = "max",
1069 		.alarm = "max_alarm",
1070 		.sbit = PB_VOLTAGE_OV_WARNING,
1071 	}, {
1072 		.reg = PMBUS_VIN_OV_FAULT_LIMIT,
1073 		.attr = "crit",
1074 		.alarm = "crit_alarm",
1075 		.sbit = PB_VOLTAGE_OV_FAULT,
1076 	}, {
1077 		.reg = PMBUS_VIRT_READ_VIN_AVG,
1078 		.update = true,
1079 		.attr = "average",
1080 	}, {
1081 		.reg = PMBUS_VIRT_READ_VIN_MIN,
1082 		.update = true,
1083 		.attr = "lowest",
1084 	}, {
1085 		.reg = PMBUS_VIRT_READ_VIN_MAX,
1086 		.update = true,
1087 		.attr = "highest",
1088 	}, {
1089 		.reg = PMBUS_VIRT_RESET_VIN_HISTORY,
1090 		.attr = "reset_history",
1091 	},
1092 };
1093 
1094 static const struct pmbus_limit_attr vmon_limit_attrs[] = {
1095 	{
1096 		.reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT,
1097 		.attr = "min",
1098 		.alarm = "min_alarm",
1099 		.sbit = PB_VOLTAGE_UV_WARNING,
1100 	}, {
1101 		.reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT,
1102 		.attr = "lcrit",
1103 		.alarm = "lcrit_alarm",
1104 		.sbit = PB_VOLTAGE_UV_FAULT,
1105 	}, {
1106 		.reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT,
1107 		.attr = "max",
1108 		.alarm = "max_alarm",
1109 		.sbit = PB_VOLTAGE_OV_WARNING,
1110 	}, {
1111 		.reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT,
1112 		.attr = "crit",
1113 		.alarm = "crit_alarm",
1114 		.sbit = PB_VOLTAGE_OV_FAULT,
1115 	}
1116 };
1117 
1118 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1119 	{
1120 		.reg = PMBUS_VOUT_UV_WARN_LIMIT,
1121 		.attr = "min",
1122 		.alarm = "min_alarm",
1123 		.sbit = PB_VOLTAGE_UV_WARNING,
1124 	}, {
1125 		.reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1126 		.attr = "lcrit",
1127 		.alarm = "lcrit_alarm",
1128 		.sbit = PB_VOLTAGE_UV_FAULT,
1129 	}, {
1130 		.reg = PMBUS_VOUT_OV_WARN_LIMIT,
1131 		.attr = "max",
1132 		.alarm = "max_alarm",
1133 		.sbit = PB_VOLTAGE_OV_WARNING,
1134 	}, {
1135 		.reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1136 		.attr = "crit",
1137 		.alarm = "crit_alarm",
1138 		.sbit = PB_VOLTAGE_OV_FAULT,
1139 	}, {
1140 		.reg = PMBUS_VIRT_READ_VOUT_AVG,
1141 		.update = true,
1142 		.attr = "average",
1143 	}, {
1144 		.reg = PMBUS_VIRT_READ_VOUT_MIN,
1145 		.update = true,
1146 		.attr = "lowest",
1147 	}, {
1148 		.reg = PMBUS_VIRT_READ_VOUT_MAX,
1149 		.update = true,
1150 		.attr = "highest",
1151 	}, {
1152 		.reg = PMBUS_VIRT_RESET_VOUT_HISTORY,
1153 		.attr = "reset_history",
1154 	}
1155 };
1156 
1157 static const struct pmbus_sensor_attr voltage_attributes[] = {
1158 	{
1159 		.reg = PMBUS_READ_VIN,
1160 		.class = PSC_VOLTAGE_IN,
1161 		.label = "vin",
1162 		.func = PMBUS_HAVE_VIN,
1163 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1164 		.sbase = PB_STATUS_INPUT_BASE,
1165 		.gbit = PB_STATUS_VIN_UV,
1166 		.limit = vin_limit_attrs,
1167 		.nlimit = ARRAY_SIZE(vin_limit_attrs),
1168 	}, {
1169 		.reg = PMBUS_VIRT_READ_VMON,
1170 		.class = PSC_VOLTAGE_IN,
1171 		.label = "vmon",
1172 		.func = PMBUS_HAVE_VMON,
1173 		.sfunc = PMBUS_HAVE_STATUS_VMON,
1174 		.sbase = PB_STATUS_VMON_BASE,
1175 		.limit = vmon_limit_attrs,
1176 		.nlimit = ARRAY_SIZE(vmon_limit_attrs),
1177 	}, {
1178 		.reg = PMBUS_READ_VCAP,
1179 		.class = PSC_VOLTAGE_IN,
1180 		.label = "vcap",
1181 		.func = PMBUS_HAVE_VCAP,
1182 	}, {
1183 		.reg = PMBUS_READ_VOUT,
1184 		.class = PSC_VOLTAGE_OUT,
1185 		.label = "vout",
1186 		.paged = true,
1187 		.func = PMBUS_HAVE_VOUT,
1188 		.sfunc = PMBUS_HAVE_STATUS_VOUT,
1189 		.sbase = PB_STATUS_VOUT_BASE,
1190 		.gbit = PB_STATUS_VOUT_OV,
1191 		.limit = vout_limit_attrs,
1192 		.nlimit = ARRAY_SIZE(vout_limit_attrs),
1193 	}
1194 };
1195 
1196 /* Current attributes */
1197 
1198 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1199 	{
1200 		.reg = PMBUS_IIN_OC_WARN_LIMIT,
1201 		.attr = "max",
1202 		.alarm = "max_alarm",
1203 		.sbit = PB_IIN_OC_WARNING,
1204 	}, {
1205 		.reg = PMBUS_IIN_OC_FAULT_LIMIT,
1206 		.attr = "crit",
1207 		.alarm = "crit_alarm",
1208 		.sbit = PB_IIN_OC_FAULT,
1209 	}, {
1210 		.reg = PMBUS_VIRT_READ_IIN_AVG,
1211 		.update = true,
1212 		.attr = "average",
1213 	}, {
1214 		.reg = PMBUS_VIRT_READ_IIN_MIN,
1215 		.update = true,
1216 		.attr = "lowest",
1217 	}, {
1218 		.reg = PMBUS_VIRT_READ_IIN_MAX,
1219 		.update = true,
1220 		.attr = "highest",
1221 	}, {
1222 		.reg = PMBUS_VIRT_RESET_IIN_HISTORY,
1223 		.attr = "reset_history",
1224 	}
1225 };
1226 
1227 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1228 	{
1229 		.reg = PMBUS_IOUT_OC_WARN_LIMIT,
1230 		.attr = "max",
1231 		.alarm = "max_alarm",
1232 		.sbit = PB_IOUT_OC_WARNING,
1233 	}, {
1234 		.reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1235 		.attr = "lcrit",
1236 		.alarm = "lcrit_alarm",
1237 		.sbit = PB_IOUT_UC_FAULT,
1238 	}, {
1239 		.reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1240 		.attr = "crit",
1241 		.alarm = "crit_alarm",
1242 		.sbit = PB_IOUT_OC_FAULT,
1243 	}, {
1244 		.reg = PMBUS_VIRT_READ_IOUT_AVG,
1245 		.update = true,
1246 		.attr = "average",
1247 	}, {
1248 		.reg = PMBUS_VIRT_READ_IOUT_MIN,
1249 		.update = true,
1250 		.attr = "lowest",
1251 	}, {
1252 		.reg = PMBUS_VIRT_READ_IOUT_MAX,
1253 		.update = true,
1254 		.attr = "highest",
1255 	}, {
1256 		.reg = PMBUS_VIRT_RESET_IOUT_HISTORY,
1257 		.attr = "reset_history",
1258 	}
1259 };
1260 
1261 static const struct pmbus_sensor_attr current_attributes[] = {
1262 	{
1263 		.reg = PMBUS_READ_IIN,
1264 		.class = PSC_CURRENT_IN,
1265 		.label = "iin",
1266 		.func = PMBUS_HAVE_IIN,
1267 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1268 		.sbase = PB_STATUS_INPUT_BASE,
1269 		.limit = iin_limit_attrs,
1270 		.nlimit = ARRAY_SIZE(iin_limit_attrs),
1271 	}, {
1272 		.reg = PMBUS_READ_IOUT,
1273 		.class = PSC_CURRENT_OUT,
1274 		.label = "iout",
1275 		.paged = true,
1276 		.func = PMBUS_HAVE_IOUT,
1277 		.sfunc = PMBUS_HAVE_STATUS_IOUT,
1278 		.sbase = PB_STATUS_IOUT_BASE,
1279 		.gbit = PB_STATUS_IOUT_OC,
1280 		.limit = iout_limit_attrs,
1281 		.nlimit = ARRAY_SIZE(iout_limit_attrs),
1282 	}
1283 };
1284 
1285 /* Power attributes */
1286 
1287 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1288 	{
1289 		.reg = PMBUS_PIN_OP_WARN_LIMIT,
1290 		.attr = "max",
1291 		.alarm = "alarm",
1292 		.sbit = PB_PIN_OP_WARNING,
1293 	}, {
1294 		.reg = PMBUS_VIRT_READ_PIN_AVG,
1295 		.update = true,
1296 		.attr = "average",
1297 	}, {
1298 		.reg = PMBUS_VIRT_READ_PIN_MAX,
1299 		.update = true,
1300 		.attr = "input_highest",
1301 	}, {
1302 		.reg = PMBUS_VIRT_RESET_PIN_HISTORY,
1303 		.attr = "reset_history",
1304 	}
1305 };
1306 
1307 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1308 	{
1309 		.reg = PMBUS_POUT_MAX,
1310 		.attr = "cap",
1311 		.alarm = "cap_alarm",
1312 		.sbit = PB_POWER_LIMITING,
1313 	}, {
1314 		.reg = PMBUS_POUT_OP_WARN_LIMIT,
1315 		.attr = "max",
1316 		.alarm = "max_alarm",
1317 		.sbit = PB_POUT_OP_WARNING,
1318 	}, {
1319 		.reg = PMBUS_POUT_OP_FAULT_LIMIT,
1320 		.attr = "crit",
1321 		.alarm = "crit_alarm",
1322 		.sbit = PB_POUT_OP_FAULT,
1323 	}, {
1324 		.reg = PMBUS_VIRT_READ_POUT_AVG,
1325 		.update = true,
1326 		.attr = "average",
1327 	}, {
1328 		.reg = PMBUS_VIRT_READ_POUT_MAX,
1329 		.update = true,
1330 		.attr = "input_highest",
1331 	}, {
1332 		.reg = PMBUS_VIRT_RESET_POUT_HISTORY,
1333 		.attr = "reset_history",
1334 	}
1335 };
1336 
1337 static const struct pmbus_sensor_attr power_attributes[] = {
1338 	{
1339 		.reg = PMBUS_READ_PIN,
1340 		.class = PSC_POWER,
1341 		.label = "pin",
1342 		.func = PMBUS_HAVE_PIN,
1343 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1344 		.sbase = PB_STATUS_INPUT_BASE,
1345 		.limit = pin_limit_attrs,
1346 		.nlimit = ARRAY_SIZE(pin_limit_attrs),
1347 	}, {
1348 		.reg = PMBUS_READ_POUT,
1349 		.class = PSC_POWER,
1350 		.label = "pout",
1351 		.paged = true,
1352 		.func = PMBUS_HAVE_POUT,
1353 		.sfunc = PMBUS_HAVE_STATUS_IOUT,
1354 		.sbase = PB_STATUS_IOUT_BASE,
1355 		.limit = pout_limit_attrs,
1356 		.nlimit = ARRAY_SIZE(pout_limit_attrs),
1357 	}
1358 };
1359 
1360 /* Temperature atributes */
1361 
1362 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1363 	{
1364 		.reg = PMBUS_UT_WARN_LIMIT,
1365 		.low = true,
1366 		.attr = "min",
1367 		.alarm = "min_alarm",
1368 		.sbit = PB_TEMP_UT_WARNING,
1369 	}, {
1370 		.reg = PMBUS_UT_FAULT_LIMIT,
1371 		.low = true,
1372 		.attr = "lcrit",
1373 		.alarm = "lcrit_alarm",
1374 		.sbit = PB_TEMP_UT_FAULT,
1375 	}, {
1376 		.reg = PMBUS_OT_WARN_LIMIT,
1377 		.attr = "max",
1378 		.alarm = "max_alarm",
1379 		.sbit = PB_TEMP_OT_WARNING,
1380 	}, {
1381 		.reg = PMBUS_OT_FAULT_LIMIT,
1382 		.attr = "crit",
1383 		.alarm = "crit_alarm",
1384 		.sbit = PB_TEMP_OT_FAULT,
1385 	}, {
1386 		.reg = PMBUS_VIRT_READ_TEMP_MIN,
1387 		.attr = "lowest",
1388 	}, {
1389 		.reg = PMBUS_VIRT_READ_TEMP_AVG,
1390 		.attr = "average",
1391 	}, {
1392 		.reg = PMBUS_VIRT_READ_TEMP_MAX,
1393 		.attr = "highest",
1394 	}, {
1395 		.reg = PMBUS_VIRT_RESET_TEMP_HISTORY,
1396 		.attr = "reset_history",
1397 	}
1398 };
1399 
1400 static const struct pmbus_limit_attr temp_limit_attrs2[] = {
1401 	{
1402 		.reg = PMBUS_UT_WARN_LIMIT,
1403 		.low = true,
1404 		.attr = "min",
1405 		.alarm = "min_alarm",
1406 		.sbit = PB_TEMP_UT_WARNING,
1407 	}, {
1408 		.reg = PMBUS_UT_FAULT_LIMIT,
1409 		.low = true,
1410 		.attr = "lcrit",
1411 		.alarm = "lcrit_alarm",
1412 		.sbit = PB_TEMP_UT_FAULT,
1413 	}, {
1414 		.reg = PMBUS_OT_WARN_LIMIT,
1415 		.attr = "max",
1416 		.alarm = "max_alarm",
1417 		.sbit = PB_TEMP_OT_WARNING,
1418 	}, {
1419 		.reg = PMBUS_OT_FAULT_LIMIT,
1420 		.attr = "crit",
1421 		.alarm = "crit_alarm",
1422 		.sbit = PB_TEMP_OT_FAULT,
1423 	}, {
1424 		.reg = PMBUS_VIRT_READ_TEMP2_MIN,
1425 		.attr = "lowest",
1426 	}, {
1427 		.reg = PMBUS_VIRT_READ_TEMP2_AVG,
1428 		.attr = "average",
1429 	}, {
1430 		.reg = PMBUS_VIRT_READ_TEMP2_MAX,
1431 		.attr = "highest",
1432 	}, {
1433 		.reg = PMBUS_VIRT_RESET_TEMP2_HISTORY,
1434 		.attr = "reset_history",
1435 	}
1436 };
1437 
1438 static const struct pmbus_limit_attr temp_limit_attrs3[] = {
1439 	{
1440 		.reg = PMBUS_UT_WARN_LIMIT,
1441 		.low = true,
1442 		.attr = "min",
1443 		.alarm = "min_alarm",
1444 		.sbit = PB_TEMP_UT_WARNING,
1445 	}, {
1446 		.reg = PMBUS_UT_FAULT_LIMIT,
1447 		.low = true,
1448 		.attr = "lcrit",
1449 		.alarm = "lcrit_alarm",
1450 		.sbit = PB_TEMP_UT_FAULT,
1451 	}, {
1452 		.reg = PMBUS_OT_WARN_LIMIT,
1453 		.attr = "max",
1454 		.alarm = "max_alarm",
1455 		.sbit = PB_TEMP_OT_WARNING,
1456 	}, {
1457 		.reg = PMBUS_OT_FAULT_LIMIT,
1458 		.attr = "crit",
1459 		.alarm = "crit_alarm",
1460 		.sbit = PB_TEMP_OT_FAULT,
1461 	}
1462 };
1463 
1464 static const struct pmbus_sensor_attr temp_attributes[] = {
1465 	{
1466 		.reg = PMBUS_READ_TEMPERATURE_1,
1467 		.class = PSC_TEMPERATURE,
1468 		.paged = true,
1469 		.update = true,
1470 		.compare = true,
1471 		.func = PMBUS_HAVE_TEMP,
1472 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1473 		.sbase = PB_STATUS_TEMP_BASE,
1474 		.gbit = PB_STATUS_TEMPERATURE,
1475 		.limit = temp_limit_attrs,
1476 		.nlimit = ARRAY_SIZE(temp_limit_attrs),
1477 	}, {
1478 		.reg = PMBUS_READ_TEMPERATURE_2,
1479 		.class = PSC_TEMPERATURE,
1480 		.paged = true,
1481 		.update = true,
1482 		.compare = true,
1483 		.func = PMBUS_HAVE_TEMP2,
1484 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1485 		.sbase = PB_STATUS_TEMP_BASE,
1486 		.gbit = PB_STATUS_TEMPERATURE,
1487 		.limit = temp_limit_attrs2,
1488 		.nlimit = ARRAY_SIZE(temp_limit_attrs2),
1489 	}, {
1490 		.reg = PMBUS_READ_TEMPERATURE_3,
1491 		.class = PSC_TEMPERATURE,
1492 		.paged = true,
1493 		.update = true,
1494 		.compare = true,
1495 		.func = PMBUS_HAVE_TEMP3,
1496 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1497 		.sbase = PB_STATUS_TEMP_BASE,
1498 		.gbit = PB_STATUS_TEMPERATURE,
1499 		.limit = temp_limit_attrs3,
1500 		.nlimit = ARRAY_SIZE(temp_limit_attrs3),
1501 	}
1502 };
1503 
1504 static const int pmbus_fan_registers[] = {
1505 	PMBUS_READ_FAN_SPEED_1,
1506 	PMBUS_READ_FAN_SPEED_2,
1507 	PMBUS_READ_FAN_SPEED_3,
1508 	PMBUS_READ_FAN_SPEED_4
1509 };
1510 
1511 static const int pmbus_fan_config_registers[] = {
1512 	PMBUS_FAN_CONFIG_12,
1513 	PMBUS_FAN_CONFIG_12,
1514 	PMBUS_FAN_CONFIG_34,
1515 	PMBUS_FAN_CONFIG_34
1516 };
1517 
1518 static const int pmbus_fan_status_registers[] = {
1519 	PMBUS_STATUS_FAN_12,
1520 	PMBUS_STATUS_FAN_12,
1521 	PMBUS_STATUS_FAN_34,
1522 	PMBUS_STATUS_FAN_34
1523 };
1524 
1525 static const u32 pmbus_fan_flags[] = {
1526 	PMBUS_HAVE_FAN12,
1527 	PMBUS_HAVE_FAN12,
1528 	PMBUS_HAVE_FAN34,
1529 	PMBUS_HAVE_FAN34
1530 };
1531 
1532 static const u32 pmbus_fan_status_flags[] = {
1533 	PMBUS_HAVE_STATUS_FAN12,
1534 	PMBUS_HAVE_STATUS_FAN12,
1535 	PMBUS_HAVE_STATUS_FAN34,
1536 	PMBUS_HAVE_STATUS_FAN34
1537 };
1538 
1539 /* Fans */
1540 static int pmbus_add_fan_attributes(struct i2c_client *client,
1541 				    struct pmbus_data *data)
1542 {
1543 	const struct pmbus_driver_info *info = data->info;
1544 	int index = 1;
1545 	int page;
1546 	int ret;
1547 
1548 	for (page = 0; page < info->pages; page++) {
1549 		int f;
1550 
1551 		for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1552 			int regval;
1553 
1554 			if (!(info->func[page] & pmbus_fan_flags[f]))
1555 				break;
1556 
1557 			if (!pmbus_check_word_register(client, page,
1558 						       pmbus_fan_registers[f]))
1559 				break;
1560 
1561 			/*
1562 			 * Skip fan if not installed.
1563 			 * Each fan configuration register covers multiple fans,
1564 			 * so we have to do some magic.
1565 			 */
1566 			regval = _pmbus_read_byte_data(client, page,
1567 				pmbus_fan_config_registers[f]);
1568 			if (regval < 0 ||
1569 			    (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1570 				continue;
1571 
1572 			if (pmbus_add_sensor(data, "fan", "input", index,
1573 					     page, pmbus_fan_registers[f],
1574 					     PSC_FAN, true, true) == NULL)
1575 				return -ENOMEM;
1576 
1577 			/*
1578 			 * Each fan status register covers multiple fans,
1579 			 * so we have to do some magic.
1580 			 */
1581 			if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1582 			    pmbus_check_byte_register(client,
1583 					page, pmbus_fan_status_registers[f])) {
1584 				int base;
1585 
1586 				if (f > 1)	/* fan 3, 4 */
1587 					base = PB_STATUS_FAN34_BASE + page;
1588 				else
1589 					base = PB_STATUS_FAN_BASE + page;
1590 				ret = pmbus_add_boolean(data, "fan",
1591 					"alarm", index, NULL, NULL, base,
1592 					PB_FAN_FAN1_WARNING >> (f & 1));
1593 				if (ret)
1594 					return ret;
1595 				ret = pmbus_add_boolean(data, "fan",
1596 					"fault", index, NULL, NULL, base,
1597 					PB_FAN_FAN1_FAULT >> (f & 1));
1598 				if (ret)
1599 					return ret;
1600 			}
1601 			index++;
1602 		}
1603 	}
1604 	return 0;
1605 }
1606 
1607 static int pmbus_find_attributes(struct i2c_client *client,
1608 				 struct pmbus_data *data)
1609 {
1610 	int ret;
1611 
1612 	/* Voltage sensors */
1613 	ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
1614 				     ARRAY_SIZE(voltage_attributes));
1615 	if (ret)
1616 		return ret;
1617 
1618 	/* Current sensors */
1619 	ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
1620 				     ARRAY_SIZE(current_attributes));
1621 	if (ret)
1622 		return ret;
1623 
1624 	/* Power sensors */
1625 	ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes,
1626 				     ARRAY_SIZE(power_attributes));
1627 	if (ret)
1628 		return ret;
1629 
1630 	/* Temperature sensors */
1631 	ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
1632 				     ARRAY_SIZE(temp_attributes));
1633 	if (ret)
1634 		return ret;
1635 
1636 	/* Fans */
1637 	ret = pmbus_add_fan_attributes(client, data);
1638 	return ret;
1639 }
1640 
1641 /*
1642  * Identify chip parameters.
1643  * This function is called for all chips.
1644  */
1645 static int pmbus_identify_common(struct i2c_client *client,
1646 				 struct pmbus_data *data)
1647 {
1648 	int vout_mode = -1;
1649 
1650 	if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE))
1651 		vout_mode = _pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
1652 	if (vout_mode >= 0 && vout_mode != 0xff) {
1653 		/*
1654 		 * Not all chips support the VOUT_MODE command,
1655 		 * so a failure to read it is not an error.
1656 		 */
1657 		switch (vout_mode >> 5) {
1658 		case 0:	/* linear mode      */
1659 			if (data->info->format[PSC_VOLTAGE_OUT] != linear)
1660 				return -ENODEV;
1661 
1662 			data->exponent = ((s8)(vout_mode << 3)) >> 3;
1663 			break;
1664 		case 1: /* VID mode         */
1665 			if (data->info->format[PSC_VOLTAGE_OUT] != vid)
1666 				return -ENODEV;
1667 			break;
1668 		case 2:	/* direct mode      */
1669 			if (data->info->format[PSC_VOLTAGE_OUT] != direct)
1670 				return -ENODEV;
1671 			break;
1672 		default:
1673 			return -ENODEV;
1674 		}
1675 	}
1676 
1677 	pmbus_clear_fault_page(client, 0);
1678 	return 0;
1679 }
1680 
1681 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
1682 			     struct pmbus_driver_info *info)
1683 {
1684 	struct device *dev = &client->dev;
1685 	int ret;
1686 
1687 	/*
1688 	 * Some PMBus chips don't support PMBUS_STATUS_BYTE, so try
1689 	 * to use PMBUS_STATUS_WORD instead if that is the case.
1690 	 * Bail out if both registers are not supported.
1691 	 */
1692 	data->status_register = PMBUS_STATUS_BYTE;
1693 	ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE);
1694 	if (ret < 0 || ret == 0xff) {
1695 		data->status_register = PMBUS_STATUS_WORD;
1696 		ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD);
1697 		if (ret < 0 || ret == 0xffff) {
1698 			dev_err(dev, "PMBus status register not found\n");
1699 			return -ENODEV;
1700 		}
1701 	}
1702 
1703 	pmbus_clear_faults(client);
1704 
1705 	if (info->identify) {
1706 		ret = (*info->identify)(client, info);
1707 		if (ret < 0) {
1708 			dev_err(dev, "Chip identification failed\n");
1709 			return ret;
1710 		}
1711 	}
1712 
1713 	if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
1714 		dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages);
1715 		return -ENODEV;
1716 	}
1717 
1718 	ret = pmbus_identify_common(client, data);
1719 	if (ret < 0) {
1720 		dev_err(dev, "Failed to identify chip capabilities\n");
1721 		return ret;
1722 	}
1723 	return 0;
1724 }
1725 
1726 int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
1727 		   struct pmbus_driver_info *info)
1728 {
1729 	struct device *dev = &client->dev;
1730 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
1731 	struct pmbus_data *data;
1732 	int ret;
1733 
1734 	if (!info)
1735 		return -ENODEV;
1736 
1737 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
1738 				     | I2C_FUNC_SMBUS_BYTE_DATA
1739 				     | I2C_FUNC_SMBUS_WORD_DATA))
1740 		return -ENODEV;
1741 
1742 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1743 	if (!data)
1744 		return -ENOMEM;
1745 
1746 	i2c_set_clientdata(client, data);
1747 	mutex_init(&data->update_lock);
1748 	data->dev = dev;
1749 
1750 	if (pdata)
1751 		data->flags = pdata->flags;
1752 	data->info = info;
1753 
1754 	ret = pmbus_init_common(client, data, info);
1755 	if (ret < 0)
1756 		return ret;
1757 
1758 	ret = pmbus_find_attributes(client, data);
1759 	if (ret)
1760 		goto out_kfree;
1761 
1762 	/*
1763 	 * If there are no attributes, something is wrong.
1764 	 * Bail out instead of trying to register nothing.
1765 	 */
1766 	if (!data->num_attributes) {
1767 		dev_err(dev, "No attributes found\n");
1768 		ret = -ENODEV;
1769 		goto out_kfree;
1770 	}
1771 
1772 	data->groups[0] = &data->group;
1773 	data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
1774 							    data, data->groups);
1775 	if (IS_ERR(data->hwmon_dev)) {
1776 		ret = PTR_ERR(data->hwmon_dev);
1777 		dev_err(dev, "Failed to register hwmon device\n");
1778 		goto out_kfree;
1779 	}
1780 	return 0;
1781 
1782 out_kfree:
1783 	kfree(data->group.attrs);
1784 	return ret;
1785 }
1786 EXPORT_SYMBOL_GPL(pmbus_do_probe);
1787 
1788 int pmbus_do_remove(struct i2c_client *client)
1789 {
1790 	struct pmbus_data *data = i2c_get_clientdata(client);
1791 	hwmon_device_unregister(data->hwmon_dev);
1792 	kfree(data->group.attrs);
1793 	return 0;
1794 }
1795 EXPORT_SYMBOL_GPL(pmbus_do_remove);
1796 
1797 MODULE_AUTHOR("Guenter Roeck");
1798 MODULE_DESCRIPTION("PMBus core driver");
1799 MODULE_LICENSE("GPL");
1800