xref: /openbmc/linux/drivers/hwmon/pmbus/pmbus_core.c (revision e953aeaa)
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 s64 pmbus_reg2data_linear(struct pmbus_data *data,
632 				 struct pmbus_sensor *sensor)
633 {
634 	s16 exponent;
635 	s32 mantissa;
636 	s64 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 * 1000LL;
651 
652 	/* scale result to micro-units for power sensors */
653 	if (sensor->class == PSC_POWER)
654 		val = val * 1000LL;
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 s64 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 val;
706 }
707 
708 /*
709  * Convert VID sensor values to milli- or micro-units
710  * depending on sensor type.
711  */
712 static s64 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 s64 pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
744 {
745 	s64 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, s64 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_ULL(val, 1000);
792 		return clamp_val(val, 0, 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_ULL(val, 1000);
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 * 1000LL;
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 = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff);
824 
825 	/* restore sign */
826 	if (negative)
827 		mantissa = -mantissa;
828 
829 	/* Convert to 5 bit exponent, 11 bit mantissa */
830 	return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
831 }
832 
833 static u16 pmbus_data2reg_direct(struct pmbus_data *data,
834 				 struct pmbus_sensor *sensor, s64 val)
835 {
836 	s64 b;
837 	s32 m, R;
838 
839 	m = data->info->m[sensor->class];
840 	b = data->info->b[sensor->class];
841 	R = data->info->R[sensor->class];
842 
843 	/* Power is in uW. Adjust R and b. */
844 	if (sensor->class == PSC_POWER) {
845 		R -= 3;
846 		b *= 1000;
847 	}
848 
849 	/* Calculate Y = (m * X + b) * 10^R */
850 	if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) {
851 		R -= 3;		/* Adjust R and b for data in milli-units */
852 		b *= 1000;
853 	}
854 	val = val * m + b;
855 
856 	while (R > 0) {
857 		val *= 10;
858 		R--;
859 	}
860 	while (R < 0) {
861 		val = div_s64(val + 5LL, 10L);  /* round closest */
862 		R++;
863 	}
864 
865 	return (u16)clamp_val(val, S16_MIN, S16_MAX);
866 }
867 
868 static u16 pmbus_data2reg_vid(struct pmbus_data *data,
869 			      struct pmbus_sensor *sensor, s64 val)
870 {
871 	val = clamp_val(val, 500, 1600);
872 
873 	return 2 + DIV_ROUND_CLOSEST_ULL((1600LL - val) * 100LL, 625);
874 }
875 
876 static u16 pmbus_data2reg(struct pmbus_data *data,
877 			  struct pmbus_sensor *sensor, s64 val)
878 {
879 	u16 regval;
880 
881 	if (!sensor->convert)
882 		return val;
883 
884 	switch (data->info->format[sensor->class]) {
885 	case direct:
886 		regval = pmbus_data2reg_direct(data, sensor, val);
887 		break;
888 	case vid:
889 		regval = pmbus_data2reg_vid(data, sensor, val);
890 		break;
891 	case linear:
892 	default:
893 		regval = pmbus_data2reg_linear(data, sensor, val);
894 		break;
895 	}
896 	return regval;
897 }
898 
899 /*
900  * Return boolean calculated from converted data.
901  * <index> defines a status register index and mask.
902  * The mask is in the lower 8 bits, the register index is in bits 8..23.
903  *
904  * The associated pmbus_boolean structure contains optional pointers to two
905  * sensor attributes. If specified, those attributes are compared against each
906  * other to determine if a limit has been exceeded.
907  *
908  * If the sensor attribute pointers are NULL, the function returns true if
909  * (status[reg] & mask) is true.
910  *
911  * If sensor attribute pointers are provided, a comparison against a specified
912  * limit has to be performed to determine the boolean result.
913  * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
914  * sensor values referenced by sensor attribute pointers s1 and s2).
915  *
916  * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
917  * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
918  *
919  * If a negative value is stored in any of the referenced registers, this value
920  * reflects an error code which will be returned.
921  */
922 static int pmbus_get_boolean(struct pmbus_data *data, struct pmbus_boolean *b,
923 			     int index)
924 {
925 	struct pmbus_sensor *s1 = b->s1;
926 	struct pmbus_sensor *s2 = b->s2;
927 	u16 reg = (index >> 16) & 0xffff;
928 	u16 mask = index & 0xffff;
929 	int ret, status;
930 	u16 regval;
931 
932 	status = data->status[reg];
933 	if (status < 0)
934 		return status;
935 
936 	regval = status & mask;
937 	if (!s1 && !s2) {
938 		ret = !!regval;
939 	} else if (!s1 || !s2) {
940 		WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b, s1, s2);
941 		return 0;
942 	} else {
943 		s64 v1, v2;
944 
945 		if (s1->data < 0)
946 			return s1->data;
947 		if (s2->data < 0)
948 			return s2->data;
949 
950 		v1 = pmbus_reg2data(data, s1);
951 		v2 = pmbus_reg2data(data, s2);
952 		ret = !!(regval && v1 >= v2);
953 	}
954 	return ret;
955 }
956 
957 static ssize_t pmbus_show_boolean(struct device *dev,
958 				  struct device_attribute *da, char *buf)
959 {
960 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
961 	struct pmbus_boolean *boolean = to_pmbus_boolean(attr);
962 	struct pmbus_data *data = pmbus_update_device(dev);
963 	int val;
964 
965 	val = pmbus_get_boolean(data, boolean, attr->index);
966 	if (val < 0)
967 		return val;
968 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
969 }
970 
971 static ssize_t pmbus_show_sensor(struct device *dev,
972 				 struct device_attribute *devattr, char *buf)
973 {
974 	struct pmbus_data *data = pmbus_update_device(dev);
975 	struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
976 
977 	if (sensor->data < 0)
978 		return sensor->data;
979 
980 	return snprintf(buf, PAGE_SIZE, "%lld\n", pmbus_reg2data(data, sensor));
981 }
982 
983 static ssize_t pmbus_set_sensor(struct device *dev,
984 				struct device_attribute *devattr,
985 				const char *buf, size_t count)
986 {
987 	struct i2c_client *client = to_i2c_client(dev->parent);
988 	struct pmbus_data *data = i2c_get_clientdata(client);
989 	struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
990 	ssize_t rv = count;
991 	s64 val;
992 	int ret;
993 	u16 regval;
994 
995 	if (kstrtos64(buf, 10, &val) < 0)
996 		return -EINVAL;
997 
998 	mutex_lock(&data->update_lock);
999 	regval = pmbus_data2reg(data, sensor, val);
1000 	ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
1001 	if (ret < 0)
1002 		rv = ret;
1003 	else
1004 		sensor->data = regval;
1005 	mutex_unlock(&data->update_lock);
1006 	return rv;
1007 }
1008 
1009 static ssize_t pmbus_show_label(struct device *dev,
1010 				struct device_attribute *da, char *buf)
1011 {
1012 	struct pmbus_label *label = to_pmbus_label(da);
1013 
1014 	return snprintf(buf, PAGE_SIZE, "%s\n", label->label);
1015 }
1016 
1017 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
1018 {
1019 	if (data->num_attributes >= data->max_attributes - 1) {
1020 		int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
1021 		void *new_attrs = krealloc(data->group.attrs,
1022 					   new_max_attrs * sizeof(void *),
1023 					   GFP_KERNEL);
1024 		if (!new_attrs)
1025 			return -ENOMEM;
1026 		data->group.attrs = new_attrs;
1027 		data->max_attributes = new_max_attrs;
1028 	}
1029 
1030 	data->group.attrs[data->num_attributes++] = attr;
1031 	data->group.attrs[data->num_attributes] = NULL;
1032 	return 0;
1033 }
1034 
1035 static void pmbus_dev_attr_init(struct device_attribute *dev_attr,
1036 				const char *name,
1037 				umode_t mode,
1038 				ssize_t (*show)(struct device *dev,
1039 						struct device_attribute *attr,
1040 						char *buf),
1041 				ssize_t (*store)(struct device *dev,
1042 						 struct device_attribute *attr,
1043 						 const char *buf, size_t count))
1044 {
1045 	sysfs_attr_init(&dev_attr->attr);
1046 	dev_attr->attr.name = name;
1047 	dev_attr->attr.mode = mode;
1048 	dev_attr->show = show;
1049 	dev_attr->store = store;
1050 }
1051 
1052 static void pmbus_attr_init(struct sensor_device_attribute *a,
1053 			    const char *name,
1054 			    umode_t mode,
1055 			    ssize_t (*show)(struct device *dev,
1056 					    struct device_attribute *attr,
1057 					    char *buf),
1058 			    ssize_t (*store)(struct device *dev,
1059 					     struct device_attribute *attr,
1060 					     const char *buf, size_t count),
1061 			    int idx)
1062 {
1063 	pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store);
1064 	a->index = idx;
1065 }
1066 
1067 static int pmbus_add_boolean(struct pmbus_data *data,
1068 			     const char *name, const char *type, int seq,
1069 			     struct pmbus_sensor *s1,
1070 			     struct pmbus_sensor *s2,
1071 			     u16 reg, u16 mask)
1072 {
1073 	struct pmbus_boolean *boolean;
1074 	struct sensor_device_attribute *a;
1075 
1076 	boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL);
1077 	if (!boolean)
1078 		return -ENOMEM;
1079 
1080 	a = &boolean->attribute;
1081 
1082 	snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
1083 		 name, seq, type);
1084 	boolean->s1 = s1;
1085 	boolean->s2 = s2;
1086 	pmbus_attr_init(a, boolean->name, 0444, pmbus_show_boolean, NULL,
1087 			(reg << 16) | mask);
1088 
1089 	return pmbus_add_attribute(data, &a->dev_attr.attr);
1090 }
1091 
1092 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
1093 					     const char *name, const char *type,
1094 					     int seq, int page, int phase,
1095 					     int reg,
1096 					     enum pmbus_sensor_classes class,
1097 					     bool update, bool readonly,
1098 					     bool convert)
1099 {
1100 	struct pmbus_sensor *sensor;
1101 	struct device_attribute *a;
1102 
1103 	sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL);
1104 	if (!sensor)
1105 		return NULL;
1106 	a = &sensor->attribute;
1107 
1108 	if (type)
1109 		snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
1110 			 name, seq, type);
1111 	else
1112 		snprintf(sensor->name, sizeof(sensor->name), "%s%d",
1113 			 name, seq);
1114 
1115 	if (data->flags & PMBUS_WRITE_PROTECTED)
1116 		readonly = true;
1117 
1118 	sensor->page = page;
1119 	sensor->phase = phase;
1120 	sensor->reg = reg;
1121 	sensor->class = class;
1122 	sensor->update = update;
1123 	sensor->convert = convert;
1124 	pmbus_dev_attr_init(a, sensor->name,
1125 			    readonly ? 0444 : 0644,
1126 			    pmbus_show_sensor, pmbus_set_sensor);
1127 
1128 	if (pmbus_add_attribute(data, &a->attr))
1129 		return NULL;
1130 
1131 	sensor->next = data->sensors;
1132 	data->sensors = sensor;
1133 
1134 	return sensor;
1135 }
1136 
1137 static int pmbus_add_label(struct pmbus_data *data,
1138 			   const char *name, int seq,
1139 			   const char *lstring, int index, int phase)
1140 {
1141 	struct pmbus_label *label;
1142 	struct device_attribute *a;
1143 
1144 	label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL);
1145 	if (!label)
1146 		return -ENOMEM;
1147 
1148 	a = &label->attribute;
1149 
1150 	snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
1151 	if (!index) {
1152 		if (phase == 0xff)
1153 			strncpy(label->label, lstring,
1154 				sizeof(label->label) - 1);
1155 		else
1156 			snprintf(label->label, sizeof(label->label), "%s.%d",
1157 				 lstring, phase);
1158 	} else {
1159 		if (phase == 0xff)
1160 			snprintf(label->label, sizeof(label->label), "%s%d",
1161 				 lstring, index);
1162 		else
1163 			snprintf(label->label, sizeof(label->label), "%s%d.%d",
1164 				 lstring, index, phase);
1165 	}
1166 
1167 	pmbus_dev_attr_init(a, label->name, 0444, pmbus_show_label, NULL);
1168 	return pmbus_add_attribute(data, &a->attr);
1169 }
1170 
1171 /*
1172  * Search for attributes. Allocate sensors, booleans, and labels as needed.
1173  */
1174 
1175 /*
1176  * The pmbus_limit_attr structure describes a single limit attribute
1177  * and its associated alarm attribute.
1178  */
1179 struct pmbus_limit_attr {
1180 	u16 reg;		/* Limit register */
1181 	u16 sbit;		/* Alarm attribute status bit */
1182 	bool update;		/* True if register needs updates */
1183 	bool low;		/* True if low limit; for limits with compare
1184 				   functions only */
1185 	const char *attr;	/* Attribute name */
1186 	const char *alarm;	/* Alarm attribute name */
1187 };
1188 
1189 /*
1190  * The pmbus_sensor_attr structure describes one sensor attribute. This
1191  * description includes a reference to the associated limit attributes.
1192  */
1193 struct pmbus_sensor_attr {
1194 	u16 reg;			/* sensor register */
1195 	u16 gbit;			/* generic status bit */
1196 	u8 nlimit;			/* # of limit registers */
1197 	enum pmbus_sensor_classes class;/* sensor class */
1198 	const char *label;		/* sensor label */
1199 	bool paged;			/* true if paged sensor */
1200 	bool update;			/* true if update needed */
1201 	bool compare;			/* true if compare function needed */
1202 	u32 func;			/* sensor mask */
1203 	u32 sfunc;			/* sensor status mask */
1204 	int sbase;			/* status base register */
1205 	const struct pmbus_limit_attr *limit;/* limit registers */
1206 };
1207 
1208 /*
1209  * Add a set of limit attributes and, if supported, the associated
1210  * alarm attributes.
1211  * returns 0 if no alarm register found, 1 if an alarm register was found,
1212  * < 0 on errors.
1213  */
1214 static int pmbus_add_limit_attrs(struct i2c_client *client,
1215 				 struct pmbus_data *data,
1216 				 const struct pmbus_driver_info *info,
1217 				 const char *name, int index, int page,
1218 				 struct pmbus_sensor *base,
1219 				 const struct pmbus_sensor_attr *attr)
1220 {
1221 	const struct pmbus_limit_attr *l = attr->limit;
1222 	int nlimit = attr->nlimit;
1223 	int have_alarm = 0;
1224 	int i, ret;
1225 	struct pmbus_sensor *curr;
1226 
1227 	for (i = 0; i < nlimit; i++) {
1228 		if (pmbus_check_word_register(client, page, l->reg)) {
1229 			curr = pmbus_add_sensor(data, name, l->attr, index,
1230 						page, 0xff, l->reg, attr->class,
1231 						attr->update || l->update,
1232 						false, true);
1233 			if (!curr)
1234 				return -ENOMEM;
1235 			if (l->sbit && (info->func[page] & attr->sfunc)) {
1236 				ret = pmbus_add_boolean(data, name,
1237 					l->alarm, index,
1238 					attr->compare ?  l->low ? curr : base
1239 						      : NULL,
1240 					attr->compare ? l->low ? base : curr
1241 						      : NULL,
1242 					attr->sbase + page, l->sbit);
1243 				if (ret)
1244 					return ret;
1245 				have_alarm = 1;
1246 			}
1247 		}
1248 		l++;
1249 	}
1250 	return have_alarm;
1251 }
1252 
1253 static int pmbus_add_sensor_attrs_one(struct i2c_client *client,
1254 				      struct pmbus_data *data,
1255 				      const struct pmbus_driver_info *info,
1256 				      const char *name,
1257 				      int index, int page, int phase,
1258 				      const struct pmbus_sensor_attr *attr,
1259 				      bool paged)
1260 {
1261 	struct pmbus_sensor *base;
1262 	bool upper = !!(attr->gbit & 0xff00);	/* need to check STATUS_WORD */
1263 	int ret;
1264 
1265 	if (attr->label) {
1266 		ret = pmbus_add_label(data, name, index, attr->label,
1267 				      paged ? page + 1 : 0, phase);
1268 		if (ret)
1269 			return ret;
1270 	}
1271 	base = pmbus_add_sensor(data, name, "input", index, page, phase,
1272 				attr->reg, attr->class, true, true, true);
1273 	if (!base)
1274 		return -ENOMEM;
1275 	/* No limit and alarm attributes for phase specific sensors */
1276 	if (attr->sfunc && phase == 0xff) {
1277 		ret = pmbus_add_limit_attrs(client, data, info, name,
1278 					    index, page, base, attr);
1279 		if (ret < 0)
1280 			return ret;
1281 		/*
1282 		 * Add generic alarm attribute only if there are no individual
1283 		 * alarm attributes, if there is a global alarm bit, and if
1284 		 * the generic status register (word or byte, depending on
1285 		 * which global bit is set) for this page is accessible.
1286 		 */
1287 		if (!ret && attr->gbit &&
1288 		    (!upper || (upper && data->has_status_word)) &&
1289 		    pmbus_check_status_register(client, page)) {
1290 			ret = pmbus_add_boolean(data, name, "alarm", index,
1291 						NULL, NULL,
1292 						PB_STATUS_BASE + page,
1293 						attr->gbit);
1294 			if (ret)
1295 				return ret;
1296 		}
1297 	}
1298 	return 0;
1299 }
1300 
1301 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info *info,
1302 				  const struct pmbus_sensor_attr *attr)
1303 {
1304 	int p;
1305 
1306 	if (attr->paged)
1307 		return true;
1308 
1309 	/*
1310 	 * Some attributes may be present on more than one page despite
1311 	 * not being marked with the paged attribute. If that is the case,
1312 	 * then treat the sensor as being paged and add the page suffix to the
1313 	 * attribute name.
1314 	 * We don't just add the paged attribute to all such attributes, in
1315 	 * order to maintain the un-suffixed labels in the case where the
1316 	 * attribute is only on page 0.
1317 	 */
1318 	for (p = 1; p < info->pages; p++) {
1319 		if (info->func[p] & attr->func)
1320 			return true;
1321 	}
1322 	return false;
1323 }
1324 
1325 static int pmbus_add_sensor_attrs(struct i2c_client *client,
1326 				  struct pmbus_data *data,
1327 				  const char *name,
1328 				  const struct pmbus_sensor_attr *attrs,
1329 				  int nattrs)
1330 {
1331 	const struct pmbus_driver_info *info = data->info;
1332 	int index, i;
1333 	int ret;
1334 
1335 	index = 1;
1336 	for (i = 0; i < nattrs; i++) {
1337 		int page, pages;
1338 		bool paged = pmbus_sensor_is_paged(info, attrs);
1339 
1340 		pages = paged ? info->pages : 1;
1341 		for (page = 0; page < pages; page++) {
1342 			if (!(info->func[page] & attrs->func))
1343 				continue;
1344 			ret = pmbus_add_sensor_attrs_one(client, data, info,
1345 							 name, index, page,
1346 							 0xff, attrs, paged);
1347 			if (ret)
1348 				return ret;
1349 			index++;
1350 			if (info->phases[page]) {
1351 				int phase;
1352 
1353 				for (phase = 0; phase < info->phases[page];
1354 				     phase++) {
1355 					if (!(info->pfunc[phase] & attrs->func))
1356 						continue;
1357 					ret = pmbus_add_sensor_attrs_one(client,
1358 						data, info, name, index, page,
1359 						phase, attrs, paged);
1360 					if (ret)
1361 						return ret;
1362 					index++;
1363 				}
1364 			}
1365 		}
1366 		attrs++;
1367 	}
1368 	return 0;
1369 }
1370 
1371 static const struct pmbus_limit_attr vin_limit_attrs[] = {
1372 	{
1373 		.reg = PMBUS_VIN_UV_WARN_LIMIT,
1374 		.attr = "min",
1375 		.alarm = "min_alarm",
1376 		.sbit = PB_VOLTAGE_UV_WARNING,
1377 	}, {
1378 		.reg = PMBUS_VIN_UV_FAULT_LIMIT,
1379 		.attr = "lcrit",
1380 		.alarm = "lcrit_alarm",
1381 		.sbit = PB_VOLTAGE_UV_FAULT,
1382 	}, {
1383 		.reg = PMBUS_VIN_OV_WARN_LIMIT,
1384 		.attr = "max",
1385 		.alarm = "max_alarm",
1386 		.sbit = PB_VOLTAGE_OV_WARNING,
1387 	}, {
1388 		.reg = PMBUS_VIN_OV_FAULT_LIMIT,
1389 		.attr = "crit",
1390 		.alarm = "crit_alarm",
1391 		.sbit = PB_VOLTAGE_OV_FAULT,
1392 	}, {
1393 		.reg = PMBUS_VIRT_READ_VIN_AVG,
1394 		.update = true,
1395 		.attr = "average",
1396 	}, {
1397 		.reg = PMBUS_VIRT_READ_VIN_MIN,
1398 		.update = true,
1399 		.attr = "lowest",
1400 	}, {
1401 		.reg = PMBUS_VIRT_READ_VIN_MAX,
1402 		.update = true,
1403 		.attr = "highest",
1404 	}, {
1405 		.reg = PMBUS_VIRT_RESET_VIN_HISTORY,
1406 		.attr = "reset_history",
1407 	},
1408 };
1409 
1410 static const struct pmbus_limit_attr vmon_limit_attrs[] = {
1411 	{
1412 		.reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT,
1413 		.attr = "min",
1414 		.alarm = "min_alarm",
1415 		.sbit = PB_VOLTAGE_UV_WARNING,
1416 	}, {
1417 		.reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT,
1418 		.attr = "lcrit",
1419 		.alarm = "lcrit_alarm",
1420 		.sbit = PB_VOLTAGE_UV_FAULT,
1421 	}, {
1422 		.reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT,
1423 		.attr = "max",
1424 		.alarm = "max_alarm",
1425 		.sbit = PB_VOLTAGE_OV_WARNING,
1426 	}, {
1427 		.reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT,
1428 		.attr = "crit",
1429 		.alarm = "crit_alarm",
1430 		.sbit = PB_VOLTAGE_OV_FAULT,
1431 	}
1432 };
1433 
1434 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1435 	{
1436 		.reg = PMBUS_VOUT_UV_WARN_LIMIT,
1437 		.attr = "min",
1438 		.alarm = "min_alarm",
1439 		.sbit = PB_VOLTAGE_UV_WARNING,
1440 	}, {
1441 		.reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1442 		.attr = "lcrit",
1443 		.alarm = "lcrit_alarm",
1444 		.sbit = PB_VOLTAGE_UV_FAULT,
1445 	}, {
1446 		.reg = PMBUS_VOUT_OV_WARN_LIMIT,
1447 		.attr = "max",
1448 		.alarm = "max_alarm",
1449 		.sbit = PB_VOLTAGE_OV_WARNING,
1450 	}, {
1451 		.reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1452 		.attr = "crit",
1453 		.alarm = "crit_alarm",
1454 		.sbit = PB_VOLTAGE_OV_FAULT,
1455 	}, {
1456 		.reg = PMBUS_VIRT_READ_VOUT_AVG,
1457 		.update = true,
1458 		.attr = "average",
1459 	}, {
1460 		.reg = PMBUS_VIRT_READ_VOUT_MIN,
1461 		.update = true,
1462 		.attr = "lowest",
1463 	}, {
1464 		.reg = PMBUS_VIRT_READ_VOUT_MAX,
1465 		.update = true,
1466 		.attr = "highest",
1467 	}, {
1468 		.reg = PMBUS_VIRT_RESET_VOUT_HISTORY,
1469 		.attr = "reset_history",
1470 	}
1471 };
1472 
1473 static const struct pmbus_sensor_attr voltage_attributes[] = {
1474 	{
1475 		.reg = PMBUS_READ_VIN,
1476 		.class = PSC_VOLTAGE_IN,
1477 		.label = "vin",
1478 		.func = PMBUS_HAVE_VIN,
1479 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1480 		.sbase = PB_STATUS_INPUT_BASE,
1481 		.gbit = PB_STATUS_VIN_UV,
1482 		.limit = vin_limit_attrs,
1483 		.nlimit = ARRAY_SIZE(vin_limit_attrs),
1484 	}, {
1485 		.reg = PMBUS_VIRT_READ_VMON,
1486 		.class = PSC_VOLTAGE_IN,
1487 		.label = "vmon",
1488 		.func = PMBUS_HAVE_VMON,
1489 		.sfunc = PMBUS_HAVE_STATUS_VMON,
1490 		.sbase = PB_STATUS_VMON_BASE,
1491 		.limit = vmon_limit_attrs,
1492 		.nlimit = ARRAY_SIZE(vmon_limit_attrs),
1493 	}, {
1494 		.reg = PMBUS_READ_VCAP,
1495 		.class = PSC_VOLTAGE_IN,
1496 		.label = "vcap",
1497 		.func = PMBUS_HAVE_VCAP,
1498 	}, {
1499 		.reg = PMBUS_READ_VOUT,
1500 		.class = PSC_VOLTAGE_OUT,
1501 		.label = "vout",
1502 		.paged = true,
1503 		.func = PMBUS_HAVE_VOUT,
1504 		.sfunc = PMBUS_HAVE_STATUS_VOUT,
1505 		.sbase = PB_STATUS_VOUT_BASE,
1506 		.gbit = PB_STATUS_VOUT_OV,
1507 		.limit = vout_limit_attrs,
1508 		.nlimit = ARRAY_SIZE(vout_limit_attrs),
1509 	}
1510 };
1511 
1512 /* Current attributes */
1513 
1514 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1515 	{
1516 		.reg = PMBUS_IIN_OC_WARN_LIMIT,
1517 		.attr = "max",
1518 		.alarm = "max_alarm",
1519 		.sbit = PB_IIN_OC_WARNING,
1520 	}, {
1521 		.reg = PMBUS_IIN_OC_FAULT_LIMIT,
1522 		.attr = "crit",
1523 		.alarm = "crit_alarm",
1524 		.sbit = PB_IIN_OC_FAULT,
1525 	}, {
1526 		.reg = PMBUS_VIRT_READ_IIN_AVG,
1527 		.update = true,
1528 		.attr = "average",
1529 	}, {
1530 		.reg = PMBUS_VIRT_READ_IIN_MIN,
1531 		.update = true,
1532 		.attr = "lowest",
1533 	}, {
1534 		.reg = PMBUS_VIRT_READ_IIN_MAX,
1535 		.update = true,
1536 		.attr = "highest",
1537 	}, {
1538 		.reg = PMBUS_VIRT_RESET_IIN_HISTORY,
1539 		.attr = "reset_history",
1540 	}
1541 };
1542 
1543 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1544 	{
1545 		.reg = PMBUS_IOUT_OC_WARN_LIMIT,
1546 		.attr = "max",
1547 		.alarm = "max_alarm",
1548 		.sbit = PB_IOUT_OC_WARNING,
1549 	}, {
1550 		.reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1551 		.attr = "lcrit",
1552 		.alarm = "lcrit_alarm",
1553 		.sbit = PB_IOUT_UC_FAULT,
1554 	}, {
1555 		.reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1556 		.attr = "crit",
1557 		.alarm = "crit_alarm",
1558 		.sbit = PB_IOUT_OC_FAULT,
1559 	}, {
1560 		.reg = PMBUS_VIRT_READ_IOUT_AVG,
1561 		.update = true,
1562 		.attr = "average",
1563 	}, {
1564 		.reg = PMBUS_VIRT_READ_IOUT_MIN,
1565 		.update = true,
1566 		.attr = "lowest",
1567 	}, {
1568 		.reg = PMBUS_VIRT_READ_IOUT_MAX,
1569 		.update = true,
1570 		.attr = "highest",
1571 	}, {
1572 		.reg = PMBUS_VIRT_RESET_IOUT_HISTORY,
1573 		.attr = "reset_history",
1574 	}
1575 };
1576 
1577 static const struct pmbus_sensor_attr current_attributes[] = {
1578 	{
1579 		.reg = PMBUS_READ_IIN,
1580 		.class = PSC_CURRENT_IN,
1581 		.label = "iin",
1582 		.func = PMBUS_HAVE_IIN,
1583 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1584 		.sbase = PB_STATUS_INPUT_BASE,
1585 		.gbit = PB_STATUS_INPUT,
1586 		.limit = iin_limit_attrs,
1587 		.nlimit = ARRAY_SIZE(iin_limit_attrs),
1588 	}, {
1589 		.reg = PMBUS_READ_IOUT,
1590 		.class = PSC_CURRENT_OUT,
1591 		.label = "iout",
1592 		.paged = true,
1593 		.func = PMBUS_HAVE_IOUT,
1594 		.sfunc = PMBUS_HAVE_STATUS_IOUT,
1595 		.sbase = PB_STATUS_IOUT_BASE,
1596 		.gbit = PB_STATUS_IOUT_OC,
1597 		.limit = iout_limit_attrs,
1598 		.nlimit = ARRAY_SIZE(iout_limit_attrs),
1599 	}
1600 };
1601 
1602 /* Power attributes */
1603 
1604 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1605 	{
1606 		.reg = PMBUS_PIN_OP_WARN_LIMIT,
1607 		.attr = "max",
1608 		.alarm = "alarm",
1609 		.sbit = PB_PIN_OP_WARNING,
1610 	}, {
1611 		.reg = PMBUS_VIRT_READ_PIN_AVG,
1612 		.update = true,
1613 		.attr = "average",
1614 	}, {
1615 		.reg = PMBUS_VIRT_READ_PIN_MIN,
1616 		.update = true,
1617 		.attr = "input_lowest",
1618 	}, {
1619 		.reg = PMBUS_VIRT_READ_PIN_MAX,
1620 		.update = true,
1621 		.attr = "input_highest",
1622 	}, {
1623 		.reg = PMBUS_VIRT_RESET_PIN_HISTORY,
1624 		.attr = "reset_history",
1625 	}
1626 };
1627 
1628 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1629 	{
1630 		.reg = PMBUS_POUT_MAX,
1631 		.attr = "cap",
1632 		.alarm = "cap_alarm",
1633 		.sbit = PB_POWER_LIMITING,
1634 	}, {
1635 		.reg = PMBUS_POUT_OP_WARN_LIMIT,
1636 		.attr = "max",
1637 		.alarm = "max_alarm",
1638 		.sbit = PB_POUT_OP_WARNING,
1639 	}, {
1640 		.reg = PMBUS_POUT_OP_FAULT_LIMIT,
1641 		.attr = "crit",
1642 		.alarm = "crit_alarm",
1643 		.sbit = PB_POUT_OP_FAULT,
1644 	}, {
1645 		.reg = PMBUS_VIRT_READ_POUT_AVG,
1646 		.update = true,
1647 		.attr = "average",
1648 	}, {
1649 		.reg = PMBUS_VIRT_READ_POUT_MIN,
1650 		.update = true,
1651 		.attr = "input_lowest",
1652 	}, {
1653 		.reg = PMBUS_VIRT_READ_POUT_MAX,
1654 		.update = true,
1655 		.attr = "input_highest",
1656 	}, {
1657 		.reg = PMBUS_VIRT_RESET_POUT_HISTORY,
1658 		.attr = "reset_history",
1659 	}
1660 };
1661 
1662 static const struct pmbus_sensor_attr power_attributes[] = {
1663 	{
1664 		.reg = PMBUS_READ_PIN,
1665 		.class = PSC_POWER,
1666 		.label = "pin",
1667 		.func = PMBUS_HAVE_PIN,
1668 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1669 		.sbase = PB_STATUS_INPUT_BASE,
1670 		.gbit = PB_STATUS_INPUT,
1671 		.limit = pin_limit_attrs,
1672 		.nlimit = ARRAY_SIZE(pin_limit_attrs),
1673 	}, {
1674 		.reg = PMBUS_READ_POUT,
1675 		.class = PSC_POWER,
1676 		.label = "pout",
1677 		.paged = true,
1678 		.func = PMBUS_HAVE_POUT,
1679 		.sfunc = PMBUS_HAVE_STATUS_IOUT,
1680 		.sbase = PB_STATUS_IOUT_BASE,
1681 		.limit = pout_limit_attrs,
1682 		.nlimit = ARRAY_SIZE(pout_limit_attrs),
1683 	}
1684 };
1685 
1686 /* Temperature atributes */
1687 
1688 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1689 	{
1690 		.reg = PMBUS_UT_WARN_LIMIT,
1691 		.low = true,
1692 		.attr = "min",
1693 		.alarm = "min_alarm",
1694 		.sbit = PB_TEMP_UT_WARNING,
1695 	}, {
1696 		.reg = PMBUS_UT_FAULT_LIMIT,
1697 		.low = true,
1698 		.attr = "lcrit",
1699 		.alarm = "lcrit_alarm",
1700 		.sbit = PB_TEMP_UT_FAULT,
1701 	}, {
1702 		.reg = PMBUS_OT_WARN_LIMIT,
1703 		.attr = "max",
1704 		.alarm = "max_alarm",
1705 		.sbit = PB_TEMP_OT_WARNING,
1706 	}, {
1707 		.reg = PMBUS_OT_FAULT_LIMIT,
1708 		.attr = "crit",
1709 		.alarm = "crit_alarm",
1710 		.sbit = PB_TEMP_OT_FAULT,
1711 	}, {
1712 		.reg = PMBUS_VIRT_READ_TEMP_MIN,
1713 		.attr = "lowest",
1714 	}, {
1715 		.reg = PMBUS_VIRT_READ_TEMP_AVG,
1716 		.attr = "average",
1717 	}, {
1718 		.reg = PMBUS_VIRT_READ_TEMP_MAX,
1719 		.attr = "highest",
1720 	}, {
1721 		.reg = PMBUS_VIRT_RESET_TEMP_HISTORY,
1722 		.attr = "reset_history",
1723 	}
1724 };
1725 
1726 static const struct pmbus_limit_attr temp_limit_attrs2[] = {
1727 	{
1728 		.reg = PMBUS_UT_WARN_LIMIT,
1729 		.low = true,
1730 		.attr = "min",
1731 		.alarm = "min_alarm",
1732 		.sbit = PB_TEMP_UT_WARNING,
1733 	}, {
1734 		.reg = PMBUS_UT_FAULT_LIMIT,
1735 		.low = true,
1736 		.attr = "lcrit",
1737 		.alarm = "lcrit_alarm",
1738 		.sbit = PB_TEMP_UT_FAULT,
1739 	}, {
1740 		.reg = PMBUS_OT_WARN_LIMIT,
1741 		.attr = "max",
1742 		.alarm = "max_alarm",
1743 		.sbit = PB_TEMP_OT_WARNING,
1744 	}, {
1745 		.reg = PMBUS_OT_FAULT_LIMIT,
1746 		.attr = "crit",
1747 		.alarm = "crit_alarm",
1748 		.sbit = PB_TEMP_OT_FAULT,
1749 	}, {
1750 		.reg = PMBUS_VIRT_READ_TEMP2_MIN,
1751 		.attr = "lowest",
1752 	}, {
1753 		.reg = PMBUS_VIRT_READ_TEMP2_AVG,
1754 		.attr = "average",
1755 	}, {
1756 		.reg = PMBUS_VIRT_READ_TEMP2_MAX,
1757 		.attr = "highest",
1758 	}, {
1759 		.reg = PMBUS_VIRT_RESET_TEMP2_HISTORY,
1760 		.attr = "reset_history",
1761 	}
1762 };
1763 
1764 static const struct pmbus_limit_attr temp_limit_attrs3[] = {
1765 	{
1766 		.reg = PMBUS_UT_WARN_LIMIT,
1767 		.low = true,
1768 		.attr = "min",
1769 		.alarm = "min_alarm",
1770 		.sbit = PB_TEMP_UT_WARNING,
1771 	}, {
1772 		.reg = PMBUS_UT_FAULT_LIMIT,
1773 		.low = true,
1774 		.attr = "lcrit",
1775 		.alarm = "lcrit_alarm",
1776 		.sbit = PB_TEMP_UT_FAULT,
1777 	}, {
1778 		.reg = PMBUS_OT_WARN_LIMIT,
1779 		.attr = "max",
1780 		.alarm = "max_alarm",
1781 		.sbit = PB_TEMP_OT_WARNING,
1782 	}, {
1783 		.reg = PMBUS_OT_FAULT_LIMIT,
1784 		.attr = "crit",
1785 		.alarm = "crit_alarm",
1786 		.sbit = PB_TEMP_OT_FAULT,
1787 	}
1788 };
1789 
1790 static const struct pmbus_sensor_attr temp_attributes[] = {
1791 	{
1792 		.reg = PMBUS_READ_TEMPERATURE_1,
1793 		.class = PSC_TEMPERATURE,
1794 		.paged = true,
1795 		.update = true,
1796 		.compare = true,
1797 		.func = PMBUS_HAVE_TEMP,
1798 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1799 		.sbase = PB_STATUS_TEMP_BASE,
1800 		.gbit = PB_STATUS_TEMPERATURE,
1801 		.limit = temp_limit_attrs,
1802 		.nlimit = ARRAY_SIZE(temp_limit_attrs),
1803 	}, {
1804 		.reg = PMBUS_READ_TEMPERATURE_2,
1805 		.class = PSC_TEMPERATURE,
1806 		.paged = true,
1807 		.update = true,
1808 		.compare = true,
1809 		.func = PMBUS_HAVE_TEMP2,
1810 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1811 		.sbase = PB_STATUS_TEMP_BASE,
1812 		.gbit = PB_STATUS_TEMPERATURE,
1813 		.limit = temp_limit_attrs2,
1814 		.nlimit = ARRAY_SIZE(temp_limit_attrs2),
1815 	}, {
1816 		.reg = PMBUS_READ_TEMPERATURE_3,
1817 		.class = PSC_TEMPERATURE,
1818 		.paged = true,
1819 		.update = true,
1820 		.compare = true,
1821 		.func = PMBUS_HAVE_TEMP3,
1822 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1823 		.sbase = PB_STATUS_TEMP_BASE,
1824 		.gbit = PB_STATUS_TEMPERATURE,
1825 		.limit = temp_limit_attrs3,
1826 		.nlimit = ARRAY_SIZE(temp_limit_attrs3),
1827 	}
1828 };
1829 
1830 static const int pmbus_fan_registers[] = {
1831 	PMBUS_READ_FAN_SPEED_1,
1832 	PMBUS_READ_FAN_SPEED_2,
1833 	PMBUS_READ_FAN_SPEED_3,
1834 	PMBUS_READ_FAN_SPEED_4
1835 };
1836 
1837 static const int pmbus_fan_status_registers[] = {
1838 	PMBUS_STATUS_FAN_12,
1839 	PMBUS_STATUS_FAN_12,
1840 	PMBUS_STATUS_FAN_34,
1841 	PMBUS_STATUS_FAN_34
1842 };
1843 
1844 static const u32 pmbus_fan_flags[] = {
1845 	PMBUS_HAVE_FAN12,
1846 	PMBUS_HAVE_FAN12,
1847 	PMBUS_HAVE_FAN34,
1848 	PMBUS_HAVE_FAN34
1849 };
1850 
1851 static const u32 pmbus_fan_status_flags[] = {
1852 	PMBUS_HAVE_STATUS_FAN12,
1853 	PMBUS_HAVE_STATUS_FAN12,
1854 	PMBUS_HAVE_STATUS_FAN34,
1855 	PMBUS_HAVE_STATUS_FAN34
1856 };
1857 
1858 /* Fans */
1859 
1860 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */
1861 static int pmbus_add_fan_ctrl(struct i2c_client *client,
1862 		struct pmbus_data *data, int index, int page, int id,
1863 		u8 config)
1864 {
1865 	struct pmbus_sensor *sensor;
1866 
1867 	sensor = pmbus_add_sensor(data, "fan", "target", index, page,
1868 				  0xff, PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN,
1869 				  false, false, true);
1870 
1871 	if (!sensor)
1872 		return -ENOMEM;
1873 
1874 	if (!((data->info->func[page] & PMBUS_HAVE_PWM12) ||
1875 			(data->info->func[page] & PMBUS_HAVE_PWM34)))
1876 		return 0;
1877 
1878 	sensor = pmbus_add_sensor(data, "pwm", NULL, index, page,
1879 				  0xff, PMBUS_VIRT_PWM_1 + id, PSC_PWM,
1880 				  false, false, true);
1881 
1882 	if (!sensor)
1883 		return -ENOMEM;
1884 
1885 	sensor = pmbus_add_sensor(data, "pwm", "enable", index, page,
1886 				  0xff, PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM,
1887 				  true, false, false);
1888 
1889 	if (!sensor)
1890 		return -ENOMEM;
1891 
1892 	return 0;
1893 }
1894 
1895 static int pmbus_add_fan_attributes(struct i2c_client *client,
1896 				    struct pmbus_data *data)
1897 {
1898 	const struct pmbus_driver_info *info = data->info;
1899 	int index = 1;
1900 	int page;
1901 	int ret;
1902 
1903 	for (page = 0; page < info->pages; page++) {
1904 		int f;
1905 
1906 		for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1907 			int regval;
1908 
1909 			if (!(info->func[page] & pmbus_fan_flags[f]))
1910 				break;
1911 
1912 			if (!pmbus_check_word_register(client, page,
1913 						       pmbus_fan_registers[f]))
1914 				break;
1915 
1916 			/*
1917 			 * Skip fan if not installed.
1918 			 * Each fan configuration register covers multiple fans,
1919 			 * so we have to do some magic.
1920 			 */
1921 			regval = _pmbus_read_byte_data(client, page,
1922 				pmbus_fan_config_registers[f]);
1923 			if (regval < 0 ||
1924 			    (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1925 				continue;
1926 
1927 			if (pmbus_add_sensor(data, "fan", "input", index,
1928 					     page, 0xff, pmbus_fan_registers[f],
1929 					     PSC_FAN, true, true, true) == NULL)
1930 				return -ENOMEM;
1931 
1932 			/* Fan control */
1933 			if (pmbus_check_word_register(client, page,
1934 					pmbus_fan_command_registers[f])) {
1935 				ret = pmbus_add_fan_ctrl(client, data, index,
1936 							 page, f, regval);
1937 				if (ret < 0)
1938 					return ret;
1939 			}
1940 
1941 			/*
1942 			 * Each fan status register covers multiple fans,
1943 			 * so we have to do some magic.
1944 			 */
1945 			if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1946 			    pmbus_check_byte_register(client,
1947 					page, pmbus_fan_status_registers[f])) {
1948 				int base;
1949 
1950 				if (f > 1)	/* fan 3, 4 */
1951 					base = PB_STATUS_FAN34_BASE + page;
1952 				else
1953 					base = PB_STATUS_FAN_BASE + page;
1954 				ret = pmbus_add_boolean(data, "fan",
1955 					"alarm", index, NULL, NULL, base,
1956 					PB_FAN_FAN1_WARNING >> (f & 1));
1957 				if (ret)
1958 					return ret;
1959 				ret = pmbus_add_boolean(data, "fan",
1960 					"fault", index, NULL, NULL, base,
1961 					PB_FAN_FAN1_FAULT >> (f & 1));
1962 				if (ret)
1963 					return ret;
1964 			}
1965 			index++;
1966 		}
1967 	}
1968 	return 0;
1969 }
1970 
1971 struct pmbus_samples_attr {
1972 	int reg;
1973 	char *name;
1974 };
1975 
1976 struct pmbus_samples_reg {
1977 	int page;
1978 	struct pmbus_samples_attr *attr;
1979 	struct device_attribute dev_attr;
1980 };
1981 
1982 static struct pmbus_samples_attr pmbus_samples_registers[] = {
1983 	{
1984 		.reg = PMBUS_VIRT_SAMPLES,
1985 		.name = "samples",
1986 	}, {
1987 		.reg = PMBUS_VIRT_IN_SAMPLES,
1988 		.name = "in_samples",
1989 	}, {
1990 		.reg = PMBUS_VIRT_CURR_SAMPLES,
1991 		.name = "curr_samples",
1992 	}, {
1993 		.reg = PMBUS_VIRT_POWER_SAMPLES,
1994 		.name = "power_samples",
1995 	}, {
1996 		.reg = PMBUS_VIRT_TEMP_SAMPLES,
1997 		.name = "temp_samples",
1998 	}
1999 };
2000 
2001 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr)
2002 
2003 static ssize_t pmbus_show_samples(struct device *dev,
2004 				  struct device_attribute *devattr, char *buf)
2005 {
2006 	int val;
2007 	struct i2c_client *client = to_i2c_client(dev->parent);
2008 	struct pmbus_samples_reg *reg = to_samples_reg(devattr);
2009 
2010 	val = _pmbus_read_word_data(client, reg->page, 0xff, reg->attr->reg);
2011 	if (val < 0)
2012 		return val;
2013 
2014 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
2015 }
2016 
2017 static ssize_t pmbus_set_samples(struct device *dev,
2018 				 struct device_attribute *devattr,
2019 				 const char *buf, size_t count)
2020 {
2021 	int ret;
2022 	long val;
2023 	struct i2c_client *client = to_i2c_client(dev->parent);
2024 	struct pmbus_samples_reg *reg = to_samples_reg(devattr);
2025 	struct pmbus_data *data = i2c_get_clientdata(client);
2026 
2027 	if (kstrtol(buf, 0, &val) < 0)
2028 		return -EINVAL;
2029 
2030 	mutex_lock(&data->update_lock);
2031 	ret = _pmbus_write_word_data(client, reg->page, reg->attr->reg, val);
2032 	mutex_unlock(&data->update_lock);
2033 
2034 	return ret ? : count;
2035 }
2036 
2037 static int pmbus_add_samples_attr(struct pmbus_data *data, int page,
2038 				  struct pmbus_samples_attr *attr)
2039 {
2040 	struct pmbus_samples_reg *reg;
2041 
2042 	reg = devm_kzalloc(data->dev, sizeof(*reg), GFP_KERNEL);
2043 	if (!reg)
2044 		return -ENOMEM;
2045 
2046 	reg->attr = attr;
2047 	reg->page = page;
2048 
2049 	pmbus_dev_attr_init(&reg->dev_attr, attr->name, 0644,
2050 			    pmbus_show_samples, pmbus_set_samples);
2051 
2052 	return pmbus_add_attribute(data, &reg->dev_attr.attr);
2053 }
2054 
2055 static int pmbus_add_samples_attributes(struct i2c_client *client,
2056 					struct pmbus_data *data)
2057 {
2058 	const struct pmbus_driver_info *info = data->info;
2059 	int s;
2060 
2061 	if (!(info->func[0] & PMBUS_HAVE_SAMPLES))
2062 		return 0;
2063 
2064 	for (s = 0; s < ARRAY_SIZE(pmbus_samples_registers); s++) {
2065 		struct pmbus_samples_attr *attr;
2066 		int ret;
2067 
2068 		attr = &pmbus_samples_registers[s];
2069 		if (!pmbus_check_word_register(client, 0, attr->reg))
2070 			continue;
2071 
2072 		ret = pmbus_add_samples_attr(data, 0, attr);
2073 		if (ret)
2074 			return ret;
2075 	}
2076 
2077 	return 0;
2078 }
2079 
2080 static int pmbus_find_attributes(struct i2c_client *client,
2081 				 struct pmbus_data *data)
2082 {
2083 	int ret;
2084 
2085 	/* Voltage sensors */
2086 	ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
2087 				     ARRAY_SIZE(voltage_attributes));
2088 	if (ret)
2089 		return ret;
2090 
2091 	/* Current sensors */
2092 	ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
2093 				     ARRAY_SIZE(current_attributes));
2094 	if (ret)
2095 		return ret;
2096 
2097 	/* Power sensors */
2098 	ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes,
2099 				     ARRAY_SIZE(power_attributes));
2100 	if (ret)
2101 		return ret;
2102 
2103 	/* Temperature sensors */
2104 	ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
2105 				     ARRAY_SIZE(temp_attributes));
2106 	if (ret)
2107 		return ret;
2108 
2109 	/* Fans */
2110 	ret = pmbus_add_fan_attributes(client, data);
2111 	if (ret)
2112 		return ret;
2113 
2114 	ret = pmbus_add_samples_attributes(client, data);
2115 	return ret;
2116 }
2117 
2118 /*
2119  * Identify chip parameters.
2120  * This function is called for all chips.
2121  */
2122 static int pmbus_identify_common(struct i2c_client *client,
2123 				 struct pmbus_data *data, int page)
2124 {
2125 	int vout_mode = -1;
2126 
2127 	if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE))
2128 		vout_mode = _pmbus_read_byte_data(client, page,
2129 						  PMBUS_VOUT_MODE);
2130 	if (vout_mode >= 0 && vout_mode != 0xff) {
2131 		/*
2132 		 * Not all chips support the VOUT_MODE command,
2133 		 * so a failure to read it is not an error.
2134 		 */
2135 		switch (vout_mode >> 5) {
2136 		case 0:	/* linear mode      */
2137 			if (data->info->format[PSC_VOLTAGE_OUT] != linear)
2138 				return -ENODEV;
2139 
2140 			data->exponent[page] = ((s8)(vout_mode << 3)) >> 3;
2141 			break;
2142 		case 1: /* VID mode         */
2143 			if (data->info->format[PSC_VOLTAGE_OUT] != vid)
2144 				return -ENODEV;
2145 			break;
2146 		case 2:	/* direct mode      */
2147 			if (data->info->format[PSC_VOLTAGE_OUT] != direct)
2148 				return -ENODEV;
2149 			break;
2150 		default:
2151 			return -ENODEV;
2152 		}
2153 	}
2154 
2155 	pmbus_clear_fault_page(client, page);
2156 	return 0;
2157 }
2158 
2159 static int pmbus_read_status_byte(struct i2c_client *client, int page)
2160 {
2161 	return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
2162 }
2163 
2164 static int pmbus_read_status_word(struct i2c_client *client, int page)
2165 {
2166 	return _pmbus_read_word_data(client, page, 0xff, PMBUS_STATUS_WORD);
2167 }
2168 
2169 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
2170 			     struct pmbus_driver_info *info)
2171 {
2172 	struct device *dev = &client->dev;
2173 	int page, ret;
2174 
2175 	/*
2176 	 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
2177 	 * to use PMBUS_STATUS_BYTE instead if that is the case.
2178 	 * Bail out if both registers are not supported.
2179 	 */
2180 	data->read_status = pmbus_read_status_word;
2181 	ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD);
2182 	if (ret < 0 || ret == 0xffff) {
2183 		data->read_status = pmbus_read_status_byte;
2184 		ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE);
2185 		if (ret < 0 || ret == 0xff) {
2186 			dev_err(dev, "PMBus status register not found\n");
2187 			return -ENODEV;
2188 		}
2189 	} else {
2190 		data->has_status_word = true;
2191 	}
2192 
2193 	/* Enable PEC if the controller supports it */
2194 	ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
2195 	if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK))
2196 		client->flags |= I2C_CLIENT_PEC;
2197 
2198 	/*
2199 	 * Check if the chip is write protected. If it is, we can not clear
2200 	 * faults, and we should not try it. Also, in that case, writes into
2201 	 * limit registers need to be disabled.
2202 	 */
2203 	ret = i2c_smbus_read_byte_data(client, PMBUS_WRITE_PROTECT);
2204 	if (ret > 0 && (ret & PB_WP_ANY))
2205 		data->flags |= PMBUS_WRITE_PROTECTED | PMBUS_SKIP_STATUS_CHECK;
2206 
2207 	if (data->info->pages)
2208 		pmbus_clear_faults(client);
2209 	else
2210 		pmbus_clear_fault_page(client, -1);
2211 
2212 	if (info->identify) {
2213 		ret = (*info->identify)(client, info);
2214 		if (ret < 0) {
2215 			dev_err(dev, "Chip identification failed\n");
2216 			return ret;
2217 		}
2218 	}
2219 
2220 	if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
2221 		dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages);
2222 		return -ENODEV;
2223 	}
2224 
2225 	for (page = 0; page < info->pages; page++) {
2226 		ret = pmbus_identify_common(client, data, page);
2227 		if (ret < 0) {
2228 			dev_err(dev, "Failed to identify chip capabilities\n");
2229 			return ret;
2230 		}
2231 	}
2232 	return 0;
2233 }
2234 
2235 #if IS_ENABLED(CONFIG_REGULATOR)
2236 static int pmbus_regulator_is_enabled(struct regulator_dev *rdev)
2237 {
2238 	struct device *dev = rdev_get_dev(rdev);
2239 	struct i2c_client *client = to_i2c_client(dev->parent);
2240 	u8 page = rdev_get_id(rdev);
2241 	int ret;
2242 
2243 	ret = pmbus_read_byte_data(client, page, PMBUS_OPERATION);
2244 	if (ret < 0)
2245 		return ret;
2246 
2247 	return !!(ret & PB_OPERATION_CONTROL_ON);
2248 }
2249 
2250 static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable)
2251 {
2252 	struct device *dev = rdev_get_dev(rdev);
2253 	struct i2c_client *client = to_i2c_client(dev->parent);
2254 	u8 page = rdev_get_id(rdev);
2255 
2256 	return pmbus_update_byte_data(client, page, PMBUS_OPERATION,
2257 				      PB_OPERATION_CONTROL_ON,
2258 				      enable ? PB_OPERATION_CONTROL_ON : 0);
2259 }
2260 
2261 static int pmbus_regulator_enable(struct regulator_dev *rdev)
2262 {
2263 	return _pmbus_regulator_on_off(rdev, 1);
2264 }
2265 
2266 static int pmbus_regulator_disable(struct regulator_dev *rdev)
2267 {
2268 	return _pmbus_regulator_on_off(rdev, 0);
2269 }
2270 
2271 const struct regulator_ops pmbus_regulator_ops = {
2272 	.enable = pmbus_regulator_enable,
2273 	.disable = pmbus_regulator_disable,
2274 	.is_enabled = pmbus_regulator_is_enabled,
2275 };
2276 EXPORT_SYMBOL_GPL(pmbus_regulator_ops);
2277 
2278 static int pmbus_regulator_register(struct pmbus_data *data)
2279 {
2280 	struct device *dev = data->dev;
2281 	const struct pmbus_driver_info *info = data->info;
2282 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
2283 	struct regulator_dev *rdev;
2284 	int i;
2285 
2286 	for (i = 0; i < info->num_regulators; i++) {
2287 		struct regulator_config config = { };
2288 
2289 		config.dev = dev;
2290 		config.driver_data = data;
2291 
2292 		if (pdata && pdata->reg_init_data)
2293 			config.init_data = &pdata->reg_init_data[i];
2294 
2295 		rdev = devm_regulator_register(dev, &info->reg_desc[i],
2296 					       &config);
2297 		if (IS_ERR(rdev)) {
2298 			dev_err(dev, "Failed to register %s regulator\n",
2299 				info->reg_desc[i].name);
2300 			return PTR_ERR(rdev);
2301 		}
2302 	}
2303 
2304 	return 0;
2305 }
2306 #else
2307 static int pmbus_regulator_register(struct pmbus_data *data)
2308 {
2309 	return 0;
2310 }
2311 #endif
2312 
2313 static struct dentry *pmbus_debugfs_dir;	/* pmbus debugfs directory */
2314 
2315 #if IS_ENABLED(CONFIG_DEBUG_FS)
2316 static int pmbus_debugfs_get(void *data, u64 *val)
2317 {
2318 	int rc;
2319 	struct pmbus_debugfs_entry *entry = data;
2320 
2321 	rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg);
2322 	if (rc < 0)
2323 		return rc;
2324 
2325 	*val = rc;
2326 
2327 	return 0;
2328 }
2329 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL,
2330 			 "0x%02llx\n");
2331 
2332 static int pmbus_debugfs_get_status(void *data, u64 *val)
2333 {
2334 	int rc;
2335 	struct pmbus_debugfs_entry *entry = data;
2336 	struct pmbus_data *pdata = i2c_get_clientdata(entry->client);
2337 
2338 	rc = pdata->read_status(entry->client, entry->page);
2339 	if (rc < 0)
2340 		return rc;
2341 
2342 	*val = rc;
2343 
2344 	return 0;
2345 }
2346 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status,
2347 			 NULL, "0x%04llx\n");
2348 
2349 static int pmbus_init_debugfs(struct i2c_client *client,
2350 			      struct pmbus_data *data)
2351 {
2352 	int i, idx = 0;
2353 	char name[PMBUS_NAME_SIZE];
2354 	struct pmbus_debugfs_entry *entries;
2355 
2356 	if (!pmbus_debugfs_dir)
2357 		return -ENODEV;
2358 
2359 	/*
2360 	 * Create the debugfs directory for this device. Use the hwmon device
2361 	 * name to avoid conflicts (hwmon numbers are globally unique).
2362 	 */
2363 	data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev),
2364 					   pmbus_debugfs_dir);
2365 	if (IS_ERR_OR_NULL(data->debugfs)) {
2366 		data->debugfs = NULL;
2367 		return -ENODEV;
2368 	}
2369 
2370 	/* Allocate the max possible entries we need. */
2371 	entries = devm_kcalloc(data->dev,
2372 			       data->info->pages * 10, sizeof(*entries),
2373 			       GFP_KERNEL);
2374 	if (!entries)
2375 		return -ENOMEM;
2376 
2377 	for (i = 0; i < data->info->pages; ++i) {
2378 		/* Check accessibility of status register if it's not page 0 */
2379 		if (!i || pmbus_check_status_register(client, i)) {
2380 			/* No need to set reg as we have special read op. */
2381 			entries[idx].client = client;
2382 			entries[idx].page = i;
2383 			scnprintf(name, PMBUS_NAME_SIZE, "status%d", i);
2384 			debugfs_create_file(name, 0444, data->debugfs,
2385 					    &entries[idx++],
2386 					    &pmbus_debugfs_ops_status);
2387 		}
2388 
2389 		if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) {
2390 			entries[idx].client = client;
2391 			entries[idx].page = i;
2392 			entries[idx].reg = PMBUS_STATUS_VOUT;
2393 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i);
2394 			debugfs_create_file(name, 0444, data->debugfs,
2395 					    &entries[idx++],
2396 					    &pmbus_debugfs_ops);
2397 		}
2398 
2399 		if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) {
2400 			entries[idx].client = client;
2401 			entries[idx].page = i;
2402 			entries[idx].reg = PMBUS_STATUS_IOUT;
2403 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i);
2404 			debugfs_create_file(name, 0444, data->debugfs,
2405 					    &entries[idx++],
2406 					    &pmbus_debugfs_ops);
2407 		}
2408 
2409 		if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) {
2410 			entries[idx].client = client;
2411 			entries[idx].page = i;
2412 			entries[idx].reg = PMBUS_STATUS_INPUT;
2413 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i);
2414 			debugfs_create_file(name, 0444, data->debugfs,
2415 					    &entries[idx++],
2416 					    &pmbus_debugfs_ops);
2417 		}
2418 
2419 		if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) {
2420 			entries[idx].client = client;
2421 			entries[idx].page = i;
2422 			entries[idx].reg = PMBUS_STATUS_TEMPERATURE;
2423 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i);
2424 			debugfs_create_file(name, 0444, data->debugfs,
2425 					    &entries[idx++],
2426 					    &pmbus_debugfs_ops);
2427 		}
2428 
2429 		if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) {
2430 			entries[idx].client = client;
2431 			entries[idx].page = i;
2432 			entries[idx].reg = PMBUS_STATUS_CML;
2433 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i);
2434 			debugfs_create_file(name, 0444, data->debugfs,
2435 					    &entries[idx++],
2436 					    &pmbus_debugfs_ops);
2437 		}
2438 
2439 		if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) {
2440 			entries[idx].client = client;
2441 			entries[idx].page = i;
2442 			entries[idx].reg = PMBUS_STATUS_OTHER;
2443 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i);
2444 			debugfs_create_file(name, 0444, data->debugfs,
2445 					    &entries[idx++],
2446 					    &pmbus_debugfs_ops);
2447 		}
2448 
2449 		if (pmbus_check_byte_register(client, i,
2450 					      PMBUS_STATUS_MFR_SPECIFIC)) {
2451 			entries[idx].client = client;
2452 			entries[idx].page = i;
2453 			entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC;
2454 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i);
2455 			debugfs_create_file(name, 0444, data->debugfs,
2456 					    &entries[idx++],
2457 					    &pmbus_debugfs_ops);
2458 		}
2459 
2460 		if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) {
2461 			entries[idx].client = client;
2462 			entries[idx].page = i;
2463 			entries[idx].reg = PMBUS_STATUS_FAN_12;
2464 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i);
2465 			debugfs_create_file(name, 0444, data->debugfs,
2466 					    &entries[idx++],
2467 					    &pmbus_debugfs_ops);
2468 		}
2469 
2470 		if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) {
2471 			entries[idx].client = client;
2472 			entries[idx].page = i;
2473 			entries[idx].reg = PMBUS_STATUS_FAN_34;
2474 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i);
2475 			debugfs_create_file(name, 0444, data->debugfs,
2476 					    &entries[idx++],
2477 					    &pmbus_debugfs_ops);
2478 		}
2479 	}
2480 
2481 	return 0;
2482 }
2483 #else
2484 static int pmbus_init_debugfs(struct i2c_client *client,
2485 			      struct pmbus_data *data)
2486 {
2487 	return 0;
2488 }
2489 #endif	/* IS_ENABLED(CONFIG_DEBUG_FS) */
2490 
2491 int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
2492 		   struct pmbus_driver_info *info)
2493 {
2494 	struct device *dev = &client->dev;
2495 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
2496 	struct pmbus_data *data;
2497 	size_t groups_num = 0;
2498 	int ret;
2499 
2500 	if (!info)
2501 		return -ENODEV;
2502 
2503 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
2504 				     | I2C_FUNC_SMBUS_BYTE_DATA
2505 				     | I2C_FUNC_SMBUS_WORD_DATA))
2506 		return -ENODEV;
2507 
2508 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
2509 	if (!data)
2510 		return -ENOMEM;
2511 
2512 	if (info->groups)
2513 		while (info->groups[groups_num])
2514 			groups_num++;
2515 
2516 	data->groups = devm_kcalloc(dev, groups_num + 2, sizeof(void *),
2517 				    GFP_KERNEL);
2518 	if (!data->groups)
2519 		return -ENOMEM;
2520 
2521 	i2c_set_clientdata(client, data);
2522 	mutex_init(&data->update_lock);
2523 	data->dev = dev;
2524 
2525 	if (pdata)
2526 		data->flags = pdata->flags;
2527 	data->info = info;
2528 	data->currpage = -1;
2529 	data->currphase = -1;
2530 
2531 	ret = pmbus_init_common(client, data, info);
2532 	if (ret < 0)
2533 		return ret;
2534 
2535 	ret = pmbus_find_attributes(client, data);
2536 	if (ret)
2537 		goto out_kfree;
2538 
2539 	/*
2540 	 * If there are no attributes, something is wrong.
2541 	 * Bail out instead of trying to register nothing.
2542 	 */
2543 	if (!data->num_attributes) {
2544 		dev_err(dev, "No attributes found\n");
2545 		ret = -ENODEV;
2546 		goto out_kfree;
2547 	}
2548 
2549 	data->groups[0] = &data->group;
2550 	memcpy(data->groups + 1, info->groups, sizeof(void *) * groups_num);
2551 	data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
2552 							    data, data->groups);
2553 	if (IS_ERR(data->hwmon_dev)) {
2554 		ret = PTR_ERR(data->hwmon_dev);
2555 		dev_err(dev, "Failed to register hwmon device\n");
2556 		goto out_kfree;
2557 	}
2558 
2559 	ret = pmbus_regulator_register(data);
2560 	if (ret)
2561 		goto out_unregister;
2562 
2563 	ret = pmbus_init_debugfs(client, data);
2564 	if (ret)
2565 		dev_warn(dev, "Failed to register debugfs\n");
2566 
2567 	return 0;
2568 
2569 out_unregister:
2570 	hwmon_device_unregister(data->hwmon_dev);
2571 out_kfree:
2572 	kfree(data->group.attrs);
2573 	return ret;
2574 }
2575 EXPORT_SYMBOL_GPL(pmbus_do_probe);
2576 
2577 int pmbus_do_remove(struct i2c_client *client)
2578 {
2579 	struct pmbus_data *data = i2c_get_clientdata(client);
2580 
2581 	debugfs_remove_recursive(data->debugfs);
2582 
2583 	hwmon_device_unregister(data->hwmon_dev);
2584 	kfree(data->group.attrs);
2585 	return 0;
2586 }
2587 EXPORT_SYMBOL_GPL(pmbus_do_remove);
2588 
2589 struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client)
2590 {
2591 	struct pmbus_data *data = i2c_get_clientdata(client);
2592 
2593 	return data->debugfs;
2594 }
2595 EXPORT_SYMBOL_GPL(pmbus_get_debugfs_dir);
2596 
2597 static int __init pmbus_core_init(void)
2598 {
2599 	pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL);
2600 	if (IS_ERR(pmbus_debugfs_dir))
2601 		pmbus_debugfs_dir = NULL;
2602 
2603 	return 0;
2604 }
2605 
2606 static void __exit pmbus_core_exit(void)
2607 {
2608 	debugfs_remove_recursive(pmbus_debugfs_dir);
2609 }
2610 
2611 module_init(pmbus_core_init);
2612 module_exit(pmbus_core_exit);
2613 
2614 MODULE_AUTHOR("Guenter Roeck");
2615 MODULE_DESCRIPTION("PMBus core driver");
2616 MODULE_LICENSE("GPL");
2617