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