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