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