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