xref: /openbmc/linux/drivers/hwmon/pmbus/pmbus_core.c (revision 5abbeebd)
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 <linux/of.h>
23 #include <linux/thermal.h>
24 #include "pmbus.h"
25 
26 /*
27  * Number of additional attribute pointers to allocate
28  * with each call to krealloc
29  */
30 #define PMBUS_ATTR_ALLOC_SIZE	32
31 #define PMBUS_NAME_SIZE		24
32 
33 struct pmbus_sensor {
34 	struct pmbus_sensor *next;
35 	char name[PMBUS_NAME_SIZE];	/* sysfs sensor name */
36 	struct device_attribute attribute;
37 	u8 page;		/* page number */
38 	u8 phase;		/* phase number, 0xff for all phases */
39 	u16 reg;		/* register */
40 	enum pmbus_sensor_classes class;	/* sensor class */
41 	bool update;		/* runtime sensor update needed */
42 	bool convert;		/* Whether or not to apply linear/vid/direct */
43 	int data;		/* Sensor data.
44 				   Negative if there was a read error */
45 };
46 #define to_pmbus_sensor(_attr) \
47 	container_of(_attr, struct pmbus_sensor, attribute)
48 
49 struct pmbus_boolean {
50 	char name[PMBUS_NAME_SIZE];	/* sysfs boolean name */
51 	struct sensor_device_attribute attribute;
52 	struct pmbus_sensor *s1;
53 	struct pmbus_sensor *s2;
54 };
55 #define to_pmbus_boolean(_attr) \
56 	container_of(_attr, struct pmbus_boolean, attribute)
57 
58 struct pmbus_label {
59 	char name[PMBUS_NAME_SIZE];	/* sysfs label name */
60 	struct device_attribute attribute;
61 	char label[PMBUS_NAME_SIZE];	/* label */
62 };
63 #define to_pmbus_label(_attr) \
64 	container_of(_attr, struct pmbus_label, attribute)
65 
66 /* Macros for converting between sensor index and register/page/status mask */
67 
68 #define PB_STATUS_MASK	0xffff
69 #define PB_REG_SHIFT	16
70 #define PB_REG_MASK	0x3ff
71 #define PB_PAGE_SHIFT	26
72 #define PB_PAGE_MASK	0x3f
73 
74 #define pb_reg_to_index(page, reg, mask)	(((page) << PB_PAGE_SHIFT) | \
75 						 ((reg) << PB_REG_SHIFT) | (mask))
76 
77 #define pb_index_to_page(index)			(((index) >> PB_PAGE_SHIFT) & PB_PAGE_MASK)
78 #define pb_index_to_reg(index)			(((index) >> PB_REG_SHIFT) & PB_REG_MASK)
79 #define pb_index_to_mask(index)			((index) & PB_STATUS_MASK)
80 
81 struct pmbus_data {
82 	struct device *dev;
83 	struct device *hwmon_dev;
84 
85 	u32 flags;		/* from platform data */
86 
87 	int exponent[PMBUS_PAGES];
88 				/* linear mode: exponent for output voltages */
89 
90 	const struct pmbus_driver_info *info;
91 
92 	int max_attributes;
93 	int num_attributes;
94 	struct attribute_group group;
95 	const struct attribute_group **groups;
96 	struct dentry *debugfs;		/* debugfs device directory */
97 
98 	struct pmbus_sensor *sensors;
99 
100 	struct mutex update_lock;
101 
102 	bool has_status_word;		/* device uses STATUS_WORD register */
103 	int (*read_status)(struct i2c_client *client, int page);
104 
105 	s16 currpage;	/* current page, -1 for unknown/unset */
106 	s16 currphase;	/* current phase, 0xff for all, -1 for unknown/unset */
107 
108 	int vout_low[PMBUS_PAGES];	/* voltage low margin */
109 	int vout_high[PMBUS_PAGES];	/* voltage high margin */
110 };
111 
112 struct pmbus_debugfs_entry {
113 	struct i2c_client *client;
114 	u8 page;
115 	u8 reg;
116 };
117 
118 static const int pmbus_fan_rpm_mask[] = {
119 	PB_FAN_1_RPM,
120 	PB_FAN_2_RPM,
121 	PB_FAN_1_RPM,
122 	PB_FAN_2_RPM,
123 };
124 
125 static const int pmbus_fan_config_registers[] = {
126 	PMBUS_FAN_CONFIG_12,
127 	PMBUS_FAN_CONFIG_12,
128 	PMBUS_FAN_CONFIG_34,
129 	PMBUS_FAN_CONFIG_34
130 };
131 
132 static const int pmbus_fan_command_registers[] = {
133 	PMBUS_FAN_COMMAND_1,
134 	PMBUS_FAN_COMMAND_2,
135 	PMBUS_FAN_COMMAND_3,
136 	PMBUS_FAN_COMMAND_4,
137 };
138 
139 void pmbus_clear_cache(struct i2c_client *client)
140 {
141 	struct pmbus_data *data = i2c_get_clientdata(client);
142 	struct pmbus_sensor *sensor;
143 
144 	for (sensor = data->sensors; sensor; sensor = sensor->next)
145 		sensor->data = -ENODATA;
146 }
147 EXPORT_SYMBOL_NS_GPL(pmbus_clear_cache, PMBUS);
148 
149 void pmbus_set_update(struct i2c_client *client, u8 reg, bool update)
150 {
151 	struct pmbus_data *data = i2c_get_clientdata(client);
152 	struct pmbus_sensor *sensor;
153 
154 	for (sensor = data->sensors; sensor; sensor = sensor->next)
155 		if (sensor->reg == reg)
156 			sensor->update = update;
157 }
158 EXPORT_SYMBOL_NS_GPL(pmbus_set_update, PMBUS);
159 
160 int pmbus_set_page(struct i2c_client *client, int page, int phase)
161 {
162 	struct pmbus_data *data = i2c_get_clientdata(client);
163 	int rv;
164 
165 	if (page < 0)
166 		return 0;
167 
168 	if (!(data->info->func[page] & PMBUS_PAGE_VIRTUAL) &&
169 	    data->info->pages > 1 && page != data->currpage) {
170 		rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
171 		if (rv < 0)
172 			return rv;
173 
174 		rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
175 		if (rv < 0)
176 			return rv;
177 
178 		if (rv != page)
179 			return -EIO;
180 	}
181 	data->currpage = page;
182 
183 	if (data->info->phases[page] && data->currphase != phase &&
184 	    !(data->info->func[page] & PMBUS_PHASE_VIRTUAL)) {
185 		rv = i2c_smbus_write_byte_data(client, PMBUS_PHASE,
186 					       phase);
187 		if (rv)
188 			return rv;
189 	}
190 	data->currphase = phase;
191 
192 	return 0;
193 }
194 EXPORT_SYMBOL_NS_GPL(pmbus_set_page, PMBUS);
195 
196 int pmbus_write_byte(struct i2c_client *client, int page, u8 value)
197 {
198 	int rv;
199 
200 	rv = pmbus_set_page(client, page, 0xff);
201 	if (rv < 0)
202 		return rv;
203 
204 	return i2c_smbus_write_byte(client, value);
205 }
206 EXPORT_SYMBOL_NS_GPL(pmbus_write_byte, PMBUS);
207 
208 /*
209  * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
210  * a device specific mapping function exists and calls it if necessary.
211  */
212 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value)
213 {
214 	struct pmbus_data *data = i2c_get_clientdata(client);
215 	const struct pmbus_driver_info *info = data->info;
216 	int status;
217 
218 	if (info->write_byte) {
219 		status = info->write_byte(client, page, value);
220 		if (status != -ENODATA)
221 			return status;
222 	}
223 	return pmbus_write_byte(client, page, value);
224 }
225 
226 int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg,
227 			  u16 word)
228 {
229 	int rv;
230 
231 	rv = pmbus_set_page(client, page, 0xff);
232 	if (rv < 0)
233 		return rv;
234 
235 	return i2c_smbus_write_word_data(client, reg, word);
236 }
237 EXPORT_SYMBOL_NS_GPL(pmbus_write_word_data, PMBUS);
238 
239 
240 static int pmbus_write_virt_reg(struct i2c_client *client, int page, int reg,
241 				u16 word)
242 {
243 	int bit;
244 	int id;
245 	int rv;
246 
247 	switch (reg) {
248 	case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4:
249 		id = reg - PMBUS_VIRT_FAN_TARGET_1;
250 		bit = pmbus_fan_rpm_mask[id];
251 		rv = pmbus_update_fan(client, page, id, bit, bit, word);
252 		break;
253 	default:
254 		rv = -ENXIO;
255 		break;
256 	}
257 
258 	return rv;
259 }
260 
261 /*
262  * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
263  * a device specific mapping function exists and calls it if necessary.
264  */
265 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg,
266 				  u16 word)
267 {
268 	struct pmbus_data *data = i2c_get_clientdata(client);
269 	const struct pmbus_driver_info *info = data->info;
270 	int status;
271 
272 	if (info->write_word_data) {
273 		status = info->write_word_data(client, page, reg, word);
274 		if (status != -ENODATA)
275 			return status;
276 	}
277 
278 	if (reg >= PMBUS_VIRT_BASE)
279 		return pmbus_write_virt_reg(client, page, reg, word);
280 
281 	return pmbus_write_word_data(client, page, reg, word);
282 }
283 
284 /*
285  * _pmbus_write_byte_data() is similar to pmbus_write_byte_data(), but checks if
286  * a device specific mapping function exists and calls it if necessary.
287  */
288 static int _pmbus_write_byte_data(struct i2c_client *client, int page, int reg, u8 value)
289 {
290 	struct pmbus_data *data = i2c_get_clientdata(client);
291 	const struct pmbus_driver_info *info = data->info;
292 	int status;
293 
294 	if (info->write_byte_data) {
295 		status = info->write_byte_data(client, page, reg, value);
296 		if (status != -ENODATA)
297 			return status;
298 	}
299 	return pmbus_write_byte_data(client, page, reg, value);
300 }
301 
302 /*
303  * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
304  * a device specific mapping function exists and calls it if necessary.
305  */
306 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
307 {
308 	struct pmbus_data *data = i2c_get_clientdata(client);
309 	const struct pmbus_driver_info *info = data->info;
310 	int status;
311 
312 	if (info->read_byte_data) {
313 		status = info->read_byte_data(client, page, reg);
314 		if (status != -ENODATA)
315 			return status;
316 	}
317 	return pmbus_read_byte_data(client, page, reg);
318 }
319 
320 int pmbus_update_fan(struct i2c_client *client, int page, int id,
321 		     u8 config, u8 mask, u16 command)
322 {
323 	int from;
324 	int rv;
325 	u8 to;
326 
327 	from = _pmbus_read_byte_data(client, page,
328 				    pmbus_fan_config_registers[id]);
329 	if (from < 0)
330 		return from;
331 
332 	to = (from & ~mask) | (config & mask);
333 	if (to != from) {
334 		rv = _pmbus_write_byte_data(client, page,
335 					   pmbus_fan_config_registers[id], to);
336 		if (rv < 0)
337 			return rv;
338 	}
339 
340 	return _pmbus_write_word_data(client, page,
341 				      pmbus_fan_command_registers[id], command);
342 }
343 EXPORT_SYMBOL_NS_GPL(pmbus_update_fan, PMBUS);
344 
345 int pmbus_read_word_data(struct i2c_client *client, int page, int phase, u8 reg)
346 {
347 	int rv;
348 
349 	rv = pmbus_set_page(client, page, phase);
350 	if (rv < 0)
351 		return rv;
352 
353 	return i2c_smbus_read_word_data(client, reg);
354 }
355 EXPORT_SYMBOL_NS_GPL(pmbus_read_word_data, PMBUS);
356 
357 static int pmbus_read_virt_reg(struct i2c_client *client, int page, int reg)
358 {
359 	int rv;
360 	int id;
361 
362 	switch (reg) {
363 	case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4:
364 		id = reg - PMBUS_VIRT_FAN_TARGET_1;
365 		rv = pmbus_get_fan_rate_device(client, page, id, rpm);
366 		break;
367 	default:
368 		rv = -ENXIO;
369 		break;
370 	}
371 
372 	return rv;
373 }
374 
375 /*
376  * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
377  * a device specific mapping function exists and calls it if necessary.
378  */
379 static int _pmbus_read_word_data(struct i2c_client *client, int page,
380 				 int phase, int reg)
381 {
382 	struct pmbus_data *data = i2c_get_clientdata(client);
383 	const struct pmbus_driver_info *info = data->info;
384 	int status;
385 
386 	if (info->read_word_data) {
387 		status = info->read_word_data(client, page, phase, reg);
388 		if (status != -ENODATA)
389 			return status;
390 	}
391 
392 	if (reg >= PMBUS_VIRT_BASE)
393 		return pmbus_read_virt_reg(client, page, reg);
394 
395 	return pmbus_read_word_data(client, page, phase, reg);
396 }
397 
398 /* Same as above, but without phase parameter, for use in check functions */
399 static int __pmbus_read_word_data(struct i2c_client *client, int page, int reg)
400 {
401 	return _pmbus_read_word_data(client, page, 0xff, reg);
402 }
403 
404 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg)
405 {
406 	int rv;
407 
408 	rv = pmbus_set_page(client, page, 0xff);
409 	if (rv < 0)
410 		return rv;
411 
412 	return i2c_smbus_read_byte_data(client, reg);
413 }
414 EXPORT_SYMBOL_NS_GPL(pmbus_read_byte_data, PMBUS);
415 
416 int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value)
417 {
418 	int rv;
419 
420 	rv = pmbus_set_page(client, page, 0xff);
421 	if (rv < 0)
422 		return rv;
423 
424 	return i2c_smbus_write_byte_data(client, reg, value);
425 }
426 EXPORT_SYMBOL_NS_GPL(pmbus_write_byte_data, PMBUS);
427 
428 int pmbus_update_byte_data(struct i2c_client *client, int page, u8 reg,
429 			   u8 mask, u8 value)
430 {
431 	unsigned int tmp;
432 	int rv;
433 
434 	rv = _pmbus_read_byte_data(client, page, reg);
435 	if (rv < 0)
436 		return rv;
437 
438 	tmp = (rv & ~mask) | (value & mask);
439 
440 	if (tmp != rv)
441 		rv = _pmbus_write_byte_data(client, page, reg, tmp);
442 
443 	return rv;
444 }
445 EXPORT_SYMBOL_NS_GPL(pmbus_update_byte_data, PMBUS);
446 
447 static int pmbus_read_block_data(struct i2c_client *client, int page, u8 reg,
448 				 char *data_buf)
449 {
450 	int rv;
451 
452 	rv = pmbus_set_page(client, page, 0xff);
453 	if (rv < 0)
454 		return rv;
455 
456 	return i2c_smbus_read_block_data(client, reg, data_buf);
457 }
458 
459 static struct pmbus_sensor *pmbus_find_sensor(struct pmbus_data *data, int page,
460 					      int reg)
461 {
462 	struct pmbus_sensor *sensor;
463 
464 	for (sensor = data->sensors; sensor; sensor = sensor->next) {
465 		if (sensor->page == page && sensor->reg == reg)
466 			return sensor;
467 	}
468 
469 	return ERR_PTR(-EINVAL);
470 }
471 
472 static int pmbus_get_fan_rate(struct i2c_client *client, int page, int id,
473 			      enum pmbus_fan_mode mode,
474 			      bool from_cache)
475 {
476 	struct pmbus_data *data = i2c_get_clientdata(client);
477 	bool want_rpm, have_rpm;
478 	struct pmbus_sensor *s;
479 	int config;
480 	int reg;
481 
482 	want_rpm = (mode == rpm);
483 
484 	if (from_cache) {
485 		reg = want_rpm ? PMBUS_VIRT_FAN_TARGET_1 : PMBUS_VIRT_PWM_1;
486 		s = pmbus_find_sensor(data, page, reg + id);
487 		if (IS_ERR(s))
488 			return PTR_ERR(s);
489 
490 		return s->data;
491 	}
492 
493 	config = _pmbus_read_byte_data(client, page,
494 				      pmbus_fan_config_registers[id]);
495 	if (config < 0)
496 		return config;
497 
498 	have_rpm = !!(config & pmbus_fan_rpm_mask[id]);
499 	if (want_rpm == have_rpm)
500 		return pmbus_read_word_data(client, page, 0xff,
501 					    pmbus_fan_command_registers[id]);
502 
503 	/* Can't sensibly map between RPM and PWM, just return zero */
504 	return 0;
505 }
506 
507 int pmbus_get_fan_rate_device(struct i2c_client *client, int page, int id,
508 			      enum pmbus_fan_mode mode)
509 {
510 	return pmbus_get_fan_rate(client, page, id, mode, false);
511 }
512 EXPORT_SYMBOL_NS_GPL(pmbus_get_fan_rate_device, PMBUS);
513 
514 int pmbus_get_fan_rate_cached(struct i2c_client *client, int page, int id,
515 			      enum pmbus_fan_mode mode)
516 {
517 	return pmbus_get_fan_rate(client, page, id, mode, true);
518 }
519 EXPORT_SYMBOL_NS_GPL(pmbus_get_fan_rate_cached, PMBUS);
520 
521 static void pmbus_clear_fault_page(struct i2c_client *client, int page)
522 {
523 	_pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
524 }
525 
526 void pmbus_clear_faults(struct i2c_client *client)
527 {
528 	struct pmbus_data *data = i2c_get_clientdata(client);
529 	int i;
530 
531 	for (i = 0; i < data->info->pages; i++)
532 		pmbus_clear_fault_page(client, i);
533 }
534 EXPORT_SYMBOL_NS_GPL(pmbus_clear_faults, PMBUS);
535 
536 static int pmbus_check_status_cml(struct i2c_client *client)
537 {
538 	struct pmbus_data *data = i2c_get_clientdata(client);
539 	int status, status2;
540 
541 	status = data->read_status(client, -1);
542 	if (status < 0 || (status & PB_STATUS_CML)) {
543 		status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
544 		if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
545 			return -EIO;
546 	}
547 	return 0;
548 }
549 
550 static bool pmbus_check_register(struct i2c_client *client,
551 				 int (*func)(struct i2c_client *client,
552 					     int page, int reg),
553 				 int page, int reg)
554 {
555 	int rv;
556 	struct pmbus_data *data = i2c_get_clientdata(client);
557 
558 	rv = func(client, page, reg);
559 	if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
560 		rv = pmbus_check_status_cml(client);
561 	if (rv < 0 && (data->flags & PMBUS_READ_STATUS_AFTER_FAILED_CHECK))
562 		data->read_status(client, -1);
563 	pmbus_clear_fault_page(client, -1);
564 	return rv >= 0;
565 }
566 
567 static bool pmbus_check_status_register(struct i2c_client *client, int page)
568 {
569 	int status;
570 	struct pmbus_data *data = i2c_get_clientdata(client);
571 
572 	status = data->read_status(client, page);
573 	if (status >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK) &&
574 	    (status & PB_STATUS_CML)) {
575 		status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
576 		if (status < 0 || (status & PB_CML_FAULT_INVALID_COMMAND))
577 			status = -EIO;
578 	}
579 
580 	pmbus_clear_fault_page(client, -1);
581 	return status >= 0;
582 }
583 
584 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
585 {
586 	return pmbus_check_register(client, _pmbus_read_byte_data, page, reg);
587 }
588 EXPORT_SYMBOL_NS_GPL(pmbus_check_byte_register, PMBUS);
589 
590 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
591 {
592 	return pmbus_check_register(client, __pmbus_read_word_data, page, reg);
593 }
594 EXPORT_SYMBOL_NS_GPL(pmbus_check_word_register, PMBUS);
595 
596 static bool __maybe_unused pmbus_check_block_register(struct i2c_client *client,
597 						      int page, int reg)
598 {
599 	int rv;
600 	struct pmbus_data *data = i2c_get_clientdata(client);
601 	char data_buf[I2C_SMBUS_BLOCK_MAX + 2];
602 
603 	rv = pmbus_read_block_data(client, page, reg, data_buf);
604 	if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
605 		rv = pmbus_check_status_cml(client);
606 	if (rv < 0 && (data->flags & PMBUS_READ_STATUS_AFTER_FAILED_CHECK))
607 		data->read_status(client, -1);
608 	pmbus_clear_fault_page(client, -1);
609 	return rv >= 0;
610 }
611 
612 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
613 {
614 	struct pmbus_data *data = i2c_get_clientdata(client);
615 
616 	return data->info;
617 }
618 EXPORT_SYMBOL_NS_GPL(pmbus_get_driver_info, PMBUS);
619 
620 static int pmbus_get_status(struct i2c_client *client, int page, int reg)
621 {
622 	struct pmbus_data *data = i2c_get_clientdata(client);
623 	int status;
624 
625 	switch (reg) {
626 	case PMBUS_STATUS_WORD:
627 		status = data->read_status(client, page);
628 		break;
629 	default:
630 		status = _pmbus_read_byte_data(client, page, reg);
631 		break;
632 	}
633 	if (status < 0)
634 		pmbus_clear_faults(client);
635 	return status;
636 }
637 
638 static void pmbus_update_sensor_data(struct i2c_client *client, struct pmbus_sensor *sensor)
639 {
640 	if (sensor->data < 0 || sensor->update)
641 		sensor->data = _pmbus_read_word_data(client, sensor->page,
642 						     sensor->phase, sensor->reg);
643 }
644 
645 /*
646  * Convert ieee754 sensor values to milli- or micro-units
647  * depending on sensor type.
648  *
649  * ieee754 data format:
650  *	bit 15:		sign
651  *	bit 10..14:	exponent
652  *	bit 0..9:	mantissa
653  * exponent=0:
654  *	v=(−1)^signbit * 2^(−14) * 0.significantbits
655  * exponent=1..30:
656  *	v=(−1)^signbit * 2^(exponent - 15) * 1.significantbits
657  * exponent=31:
658  *	v=NaN
659  *
660  * Add the number mantissa bits into the calculations for simplicity.
661  * To do that, add '10' to the exponent. By doing that, we can just add
662  * 0x400 to normal values and get the expected result.
663  */
664 static long pmbus_reg2data_ieee754(struct pmbus_data *data,
665 				   struct pmbus_sensor *sensor)
666 {
667 	int exponent;
668 	bool sign;
669 	long val;
670 
671 	/* only support half precision for now */
672 	sign = sensor->data & 0x8000;
673 	exponent = (sensor->data >> 10) & 0x1f;
674 	val = sensor->data & 0x3ff;
675 
676 	if (exponent == 0) {			/* subnormal */
677 		exponent = -(14 + 10);
678 	} else if (exponent ==  0x1f) {		/* NaN, convert to min/max */
679 		exponent = 0;
680 		val = 65504;
681 	} else {
682 		exponent -= (15 + 10);		/* normal */
683 		val |= 0x400;
684 	}
685 
686 	/* scale result to milli-units for all sensors except fans */
687 	if (sensor->class != PSC_FAN)
688 		val = val * 1000L;
689 
690 	/* scale result to micro-units for power sensors */
691 	if (sensor->class == PSC_POWER)
692 		val = val * 1000L;
693 
694 	if (exponent >= 0)
695 		val <<= exponent;
696 	else
697 		val >>= -exponent;
698 
699 	if (sign)
700 		val = -val;
701 
702 	return val;
703 }
704 
705 /*
706  * Convert linear sensor values to milli- or micro-units
707  * depending on sensor type.
708  */
709 static s64 pmbus_reg2data_linear(struct pmbus_data *data,
710 				 struct pmbus_sensor *sensor)
711 {
712 	s16 exponent;
713 	s32 mantissa;
714 	s64 val;
715 
716 	if (sensor->class == PSC_VOLTAGE_OUT) {	/* LINEAR16 */
717 		exponent = data->exponent[sensor->page];
718 		mantissa = (u16) sensor->data;
719 	} else {				/* LINEAR11 */
720 		exponent = ((s16)sensor->data) >> 11;
721 		mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5;
722 	}
723 
724 	val = mantissa;
725 
726 	/* scale result to milli-units for all sensors except fans */
727 	if (sensor->class != PSC_FAN)
728 		val = val * 1000LL;
729 
730 	/* scale result to micro-units for power sensors */
731 	if (sensor->class == PSC_POWER)
732 		val = val * 1000LL;
733 
734 	if (exponent >= 0)
735 		val <<= exponent;
736 	else
737 		val >>= -exponent;
738 
739 	return val;
740 }
741 
742 /*
743  * Convert direct sensor values to milli- or micro-units
744  * depending on sensor type.
745  */
746 static s64 pmbus_reg2data_direct(struct pmbus_data *data,
747 				 struct pmbus_sensor *sensor)
748 {
749 	s64 b, val = (s16)sensor->data;
750 	s32 m, R;
751 
752 	m = data->info->m[sensor->class];
753 	b = data->info->b[sensor->class];
754 	R = data->info->R[sensor->class];
755 
756 	if (m == 0)
757 		return 0;
758 
759 	/* X = 1/m * (Y * 10^-R - b) */
760 	R = -R;
761 	/* scale result to milli-units for everything but fans */
762 	if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) {
763 		R += 3;
764 		b *= 1000;
765 	}
766 
767 	/* scale result to micro-units for power sensors */
768 	if (sensor->class == PSC_POWER) {
769 		R += 3;
770 		b *= 1000;
771 	}
772 
773 	while (R > 0) {
774 		val *= 10;
775 		R--;
776 	}
777 	while (R < 0) {
778 		val = div_s64(val + 5LL, 10L);  /* round closest */
779 		R++;
780 	}
781 
782 	val = div_s64(val - b, m);
783 	return val;
784 }
785 
786 /*
787  * Convert VID sensor values to milli- or micro-units
788  * depending on sensor type.
789  */
790 static s64 pmbus_reg2data_vid(struct pmbus_data *data,
791 			      struct pmbus_sensor *sensor)
792 {
793 	long val = sensor->data;
794 	long rv = 0;
795 
796 	switch (data->info->vrm_version[sensor->page]) {
797 	case vr11:
798 		if (val >= 0x02 && val <= 0xb2)
799 			rv = DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100);
800 		break;
801 	case vr12:
802 		if (val >= 0x01)
803 			rv = 250 + (val - 1) * 5;
804 		break;
805 	case vr13:
806 		if (val >= 0x01)
807 			rv = 500 + (val - 1) * 10;
808 		break;
809 	case imvp9:
810 		if (val >= 0x01)
811 			rv = 200 + (val - 1) * 10;
812 		break;
813 	case amd625mv:
814 		if (val >= 0x0 && val <= 0xd8)
815 			rv = DIV_ROUND_CLOSEST(155000 - val * 625, 100);
816 		break;
817 	}
818 	return rv;
819 }
820 
821 static s64 pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
822 {
823 	s64 val;
824 
825 	if (!sensor->convert)
826 		return sensor->data;
827 
828 	switch (data->info->format[sensor->class]) {
829 	case direct:
830 		val = pmbus_reg2data_direct(data, sensor);
831 		break;
832 	case vid:
833 		val = pmbus_reg2data_vid(data, sensor);
834 		break;
835 	case ieee754:
836 		val = pmbus_reg2data_ieee754(data, sensor);
837 		break;
838 	case linear:
839 	default:
840 		val = pmbus_reg2data_linear(data, sensor);
841 		break;
842 	}
843 	return val;
844 }
845 
846 #define MAX_IEEE_MANTISSA	(0x7ff * 1000)
847 #define MIN_IEEE_MANTISSA	(0x400 * 1000)
848 
849 static u16 pmbus_data2reg_ieee754(struct pmbus_data *data,
850 				  struct pmbus_sensor *sensor, long val)
851 {
852 	u16 exponent = (15 + 10);
853 	long mantissa;
854 	u16 sign = 0;
855 
856 	/* simple case */
857 	if (val == 0)
858 		return 0;
859 
860 	if (val < 0) {
861 		sign = 0x8000;
862 		val = -val;
863 	}
864 
865 	/* Power is in uW. Convert to mW before converting. */
866 	if (sensor->class == PSC_POWER)
867 		val = DIV_ROUND_CLOSEST(val, 1000L);
868 
869 	/*
870 	 * For simplicity, convert fan data to milli-units
871 	 * before calculating the exponent.
872 	 */
873 	if (sensor->class == PSC_FAN)
874 		val = val * 1000;
875 
876 	/* Reduce large mantissa until it fits into 10 bit */
877 	while (val > MAX_IEEE_MANTISSA && exponent < 30) {
878 		exponent++;
879 		val >>= 1;
880 	}
881 	/*
882 	 * Increase small mantissa to generate valid 'normal'
883 	 * number
884 	 */
885 	while (val < MIN_IEEE_MANTISSA && exponent > 1) {
886 		exponent--;
887 		val <<= 1;
888 	}
889 
890 	/* Convert mantissa from milli-units to units */
891 	mantissa = DIV_ROUND_CLOSEST(val, 1000);
892 
893 	/*
894 	 * Ensure that the resulting number is within range.
895 	 * Valid range is 0x400..0x7ff, where bit 10 reflects
896 	 * the implied high bit in normalized ieee754 numbers.
897 	 * Set the range to 0x400..0x7ff to reflect this.
898 	 * The upper bit is then removed by the mask against
899 	 * 0x3ff in the final assignment.
900 	 */
901 	if (mantissa > 0x7ff)
902 		mantissa = 0x7ff;
903 	else if (mantissa < 0x400)
904 		mantissa = 0x400;
905 
906 	/* Convert to sign, 5 bit exponent, 10 bit mantissa */
907 	return sign | (mantissa & 0x3ff) | ((exponent << 10) & 0x7c00);
908 }
909 
910 #define MAX_LIN_MANTISSA	(1023 * 1000)
911 #define MIN_LIN_MANTISSA	(511 * 1000)
912 
913 static u16 pmbus_data2reg_linear(struct pmbus_data *data,
914 				 struct pmbus_sensor *sensor, s64 val)
915 {
916 	s16 exponent = 0, mantissa;
917 	bool negative = false;
918 
919 	/* simple case */
920 	if (val == 0)
921 		return 0;
922 
923 	if (sensor->class == PSC_VOLTAGE_OUT) {
924 		/* LINEAR16 does not support negative voltages */
925 		if (val < 0)
926 			return 0;
927 
928 		/*
929 		 * For a static exponents, we don't have a choice
930 		 * but to adjust the value to it.
931 		 */
932 		if (data->exponent[sensor->page] < 0)
933 			val <<= -data->exponent[sensor->page];
934 		else
935 			val >>= data->exponent[sensor->page];
936 		val = DIV_ROUND_CLOSEST_ULL(val, 1000);
937 		return clamp_val(val, 0, 0xffff);
938 	}
939 
940 	if (val < 0) {
941 		negative = true;
942 		val = -val;
943 	}
944 
945 	/* Power is in uW. Convert to mW before converting. */
946 	if (sensor->class == PSC_POWER)
947 		val = DIV_ROUND_CLOSEST_ULL(val, 1000);
948 
949 	/*
950 	 * For simplicity, convert fan data to milli-units
951 	 * before calculating the exponent.
952 	 */
953 	if (sensor->class == PSC_FAN)
954 		val = val * 1000LL;
955 
956 	/* Reduce large mantissa until it fits into 10 bit */
957 	while (val >= MAX_LIN_MANTISSA && exponent < 15) {
958 		exponent++;
959 		val >>= 1;
960 	}
961 	/* Increase small mantissa to improve precision */
962 	while (val < MIN_LIN_MANTISSA && exponent > -15) {
963 		exponent--;
964 		val <<= 1;
965 	}
966 
967 	/* Convert mantissa from milli-units to units */
968 	mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff);
969 
970 	/* restore sign */
971 	if (negative)
972 		mantissa = -mantissa;
973 
974 	/* Convert to 5 bit exponent, 11 bit mantissa */
975 	return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
976 }
977 
978 static u16 pmbus_data2reg_direct(struct pmbus_data *data,
979 				 struct pmbus_sensor *sensor, s64 val)
980 {
981 	s64 b;
982 	s32 m, R;
983 
984 	m = data->info->m[sensor->class];
985 	b = data->info->b[sensor->class];
986 	R = data->info->R[sensor->class];
987 
988 	/* Power is in uW. Adjust R and b. */
989 	if (sensor->class == PSC_POWER) {
990 		R -= 3;
991 		b *= 1000;
992 	}
993 
994 	/* Calculate Y = (m * X + b) * 10^R */
995 	if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) {
996 		R -= 3;		/* Adjust R and b for data in milli-units */
997 		b *= 1000;
998 	}
999 	val = val * m + b;
1000 
1001 	while (R > 0) {
1002 		val *= 10;
1003 		R--;
1004 	}
1005 	while (R < 0) {
1006 		val = div_s64(val + 5LL, 10L);  /* round closest */
1007 		R++;
1008 	}
1009 
1010 	return (u16)clamp_val(val, S16_MIN, S16_MAX);
1011 }
1012 
1013 static u16 pmbus_data2reg_vid(struct pmbus_data *data,
1014 			      struct pmbus_sensor *sensor, s64 val)
1015 {
1016 	val = clamp_val(val, 500, 1600);
1017 
1018 	return 2 + DIV_ROUND_CLOSEST_ULL((1600LL - val) * 100LL, 625);
1019 }
1020 
1021 static u16 pmbus_data2reg(struct pmbus_data *data,
1022 			  struct pmbus_sensor *sensor, s64 val)
1023 {
1024 	u16 regval;
1025 
1026 	if (!sensor->convert)
1027 		return val;
1028 
1029 	switch (data->info->format[sensor->class]) {
1030 	case direct:
1031 		regval = pmbus_data2reg_direct(data, sensor, val);
1032 		break;
1033 	case vid:
1034 		regval = pmbus_data2reg_vid(data, sensor, val);
1035 		break;
1036 	case ieee754:
1037 		regval = pmbus_data2reg_ieee754(data, sensor, val);
1038 		break;
1039 	case linear:
1040 	default:
1041 		regval = pmbus_data2reg_linear(data, sensor, val);
1042 		break;
1043 	}
1044 	return regval;
1045 }
1046 
1047 /*
1048  * Return boolean calculated from converted data.
1049  * <index> defines a status register index and mask.
1050  * The mask is in the lower 8 bits, the register index is in bits 8..23.
1051  *
1052  * The associated pmbus_boolean structure contains optional pointers to two
1053  * sensor attributes. If specified, those attributes are compared against each
1054  * other to determine if a limit has been exceeded.
1055  *
1056  * If the sensor attribute pointers are NULL, the function returns true if
1057  * (status[reg] & mask) is true.
1058  *
1059  * If sensor attribute pointers are provided, a comparison against a specified
1060  * limit has to be performed to determine the boolean result.
1061  * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
1062  * sensor values referenced by sensor attribute pointers s1 and s2).
1063  *
1064  * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
1065  * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
1066  *
1067  * If a negative value is stored in any of the referenced registers, this value
1068  * reflects an error code which will be returned.
1069  */
1070 static int pmbus_get_boolean(struct i2c_client *client, struct pmbus_boolean *b,
1071 			     int index)
1072 {
1073 	struct pmbus_data *data = i2c_get_clientdata(client);
1074 	struct pmbus_sensor *s1 = b->s1;
1075 	struct pmbus_sensor *s2 = b->s2;
1076 	u16 mask = pb_index_to_mask(index);
1077 	u8 page = pb_index_to_page(index);
1078 	u16 reg = pb_index_to_reg(index);
1079 	int ret, status;
1080 	u16 regval;
1081 
1082 	mutex_lock(&data->update_lock);
1083 	status = pmbus_get_status(client, page, reg);
1084 	if (status < 0) {
1085 		ret = status;
1086 		goto unlock;
1087 	}
1088 
1089 	if (s1)
1090 		pmbus_update_sensor_data(client, s1);
1091 	if (s2)
1092 		pmbus_update_sensor_data(client, s2);
1093 
1094 	regval = status & mask;
1095 	if (regval) {
1096 		ret = _pmbus_write_byte_data(client, page, reg, regval);
1097 		if (ret)
1098 			goto unlock;
1099 	}
1100 	if (s1 && s2) {
1101 		s64 v1, v2;
1102 
1103 		if (s1->data < 0) {
1104 			ret = s1->data;
1105 			goto unlock;
1106 		}
1107 		if (s2->data < 0) {
1108 			ret = s2->data;
1109 			goto unlock;
1110 		}
1111 
1112 		v1 = pmbus_reg2data(data, s1);
1113 		v2 = pmbus_reg2data(data, s2);
1114 		ret = !!(regval && v1 >= v2);
1115 	} else {
1116 		ret = !!regval;
1117 	}
1118 unlock:
1119 	mutex_unlock(&data->update_lock);
1120 	return ret;
1121 }
1122 
1123 static ssize_t pmbus_show_boolean(struct device *dev,
1124 				  struct device_attribute *da, char *buf)
1125 {
1126 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
1127 	struct pmbus_boolean *boolean = to_pmbus_boolean(attr);
1128 	struct i2c_client *client = to_i2c_client(dev->parent);
1129 	int val;
1130 
1131 	val = pmbus_get_boolean(client, boolean, attr->index);
1132 	if (val < 0)
1133 		return val;
1134 	return sysfs_emit(buf, "%d\n", val);
1135 }
1136 
1137 static ssize_t pmbus_show_sensor(struct device *dev,
1138 				 struct device_attribute *devattr, char *buf)
1139 {
1140 	struct i2c_client *client = to_i2c_client(dev->parent);
1141 	struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
1142 	struct pmbus_data *data = i2c_get_clientdata(client);
1143 	ssize_t ret;
1144 
1145 	mutex_lock(&data->update_lock);
1146 	pmbus_update_sensor_data(client, sensor);
1147 	if (sensor->data < 0)
1148 		ret = sensor->data;
1149 	else
1150 		ret = sysfs_emit(buf, "%lld\n", pmbus_reg2data(data, sensor));
1151 	mutex_unlock(&data->update_lock);
1152 	return ret;
1153 }
1154 
1155 static ssize_t pmbus_set_sensor(struct device *dev,
1156 				struct device_attribute *devattr,
1157 				const char *buf, size_t count)
1158 {
1159 	struct i2c_client *client = to_i2c_client(dev->parent);
1160 	struct pmbus_data *data = i2c_get_clientdata(client);
1161 	struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
1162 	ssize_t rv = count;
1163 	s64 val;
1164 	int ret;
1165 	u16 regval;
1166 
1167 	if (kstrtos64(buf, 10, &val) < 0)
1168 		return -EINVAL;
1169 
1170 	mutex_lock(&data->update_lock);
1171 	regval = pmbus_data2reg(data, sensor, val);
1172 	ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
1173 	if (ret < 0)
1174 		rv = ret;
1175 	else
1176 		sensor->data = -ENODATA;
1177 	mutex_unlock(&data->update_lock);
1178 	return rv;
1179 }
1180 
1181 static ssize_t pmbus_show_label(struct device *dev,
1182 				struct device_attribute *da, char *buf)
1183 {
1184 	struct pmbus_label *label = to_pmbus_label(da);
1185 
1186 	return sysfs_emit(buf, "%s\n", label->label);
1187 }
1188 
1189 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
1190 {
1191 	if (data->num_attributes >= data->max_attributes - 1) {
1192 		int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
1193 		void *new_attrs = devm_krealloc(data->dev, data->group.attrs,
1194 						new_max_attrs * sizeof(void *),
1195 						GFP_KERNEL);
1196 		if (!new_attrs)
1197 			return -ENOMEM;
1198 		data->group.attrs = new_attrs;
1199 		data->max_attributes = new_max_attrs;
1200 	}
1201 
1202 	data->group.attrs[data->num_attributes++] = attr;
1203 	data->group.attrs[data->num_attributes] = NULL;
1204 	return 0;
1205 }
1206 
1207 static void pmbus_dev_attr_init(struct device_attribute *dev_attr,
1208 				const char *name,
1209 				umode_t mode,
1210 				ssize_t (*show)(struct device *dev,
1211 						struct device_attribute *attr,
1212 						char *buf),
1213 				ssize_t (*store)(struct device *dev,
1214 						 struct device_attribute *attr,
1215 						 const char *buf, size_t count))
1216 {
1217 	sysfs_attr_init(&dev_attr->attr);
1218 	dev_attr->attr.name = name;
1219 	dev_attr->attr.mode = mode;
1220 	dev_attr->show = show;
1221 	dev_attr->store = store;
1222 }
1223 
1224 static void pmbus_attr_init(struct sensor_device_attribute *a,
1225 			    const char *name,
1226 			    umode_t mode,
1227 			    ssize_t (*show)(struct device *dev,
1228 					    struct device_attribute *attr,
1229 					    char *buf),
1230 			    ssize_t (*store)(struct device *dev,
1231 					     struct device_attribute *attr,
1232 					     const char *buf, size_t count),
1233 			    int idx)
1234 {
1235 	pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store);
1236 	a->index = idx;
1237 }
1238 
1239 static int pmbus_add_boolean(struct pmbus_data *data,
1240 			     const char *name, const char *type, int seq,
1241 			     struct pmbus_sensor *s1,
1242 			     struct pmbus_sensor *s2,
1243 			     u8 page, u16 reg, u16 mask)
1244 {
1245 	struct pmbus_boolean *boolean;
1246 	struct sensor_device_attribute *a;
1247 
1248 	if (WARN((s1 && !s2) || (!s1 && s2), "Bad s1/s2 parameters\n"))
1249 		return -EINVAL;
1250 
1251 	boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL);
1252 	if (!boolean)
1253 		return -ENOMEM;
1254 
1255 	a = &boolean->attribute;
1256 
1257 	snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
1258 		 name, seq, type);
1259 	boolean->s1 = s1;
1260 	boolean->s2 = s2;
1261 	pmbus_attr_init(a, boolean->name, 0444, pmbus_show_boolean, NULL,
1262 			pb_reg_to_index(page, reg, mask));
1263 
1264 	return pmbus_add_attribute(data, &a->dev_attr.attr);
1265 }
1266 
1267 /* of thermal for pmbus temperature sensors */
1268 struct pmbus_thermal_data {
1269 	struct pmbus_data *pmbus_data;
1270 	struct pmbus_sensor *sensor;
1271 };
1272 
1273 static int pmbus_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
1274 {
1275 	struct pmbus_thermal_data *tdata = tz->devdata;
1276 	struct pmbus_sensor *sensor = tdata->sensor;
1277 	struct pmbus_data *pmbus_data = tdata->pmbus_data;
1278 	struct i2c_client *client = to_i2c_client(pmbus_data->dev);
1279 	struct device *dev = pmbus_data->hwmon_dev;
1280 	int ret = 0;
1281 
1282 	if (!dev) {
1283 		/* May not even get to hwmon yet */
1284 		*temp = 0;
1285 		return 0;
1286 	}
1287 
1288 	mutex_lock(&pmbus_data->update_lock);
1289 	pmbus_update_sensor_data(client, sensor);
1290 	if (sensor->data < 0)
1291 		ret = sensor->data;
1292 	else
1293 		*temp = (int)pmbus_reg2data(pmbus_data, sensor);
1294 	mutex_unlock(&pmbus_data->update_lock);
1295 
1296 	return ret;
1297 }
1298 
1299 static const struct thermal_zone_device_ops pmbus_thermal_ops = {
1300 	.get_temp = pmbus_thermal_get_temp,
1301 };
1302 
1303 static int pmbus_thermal_add_sensor(struct pmbus_data *pmbus_data,
1304 				    struct pmbus_sensor *sensor, int index)
1305 {
1306 	struct device *dev = pmbus_data->dev;
1307 	struct pmbus_thermal_data *tdata;
1308 	struct thermal_zone_device *tzd;
1309 
1310 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
1311 	if (!tdata)
1312 		return -ENOMEM;
1313 
1314 	tdata->sensor = sensor;
1315 	tdata->pmbus_data = pmbus_data;
1316 
1317 	tzd = devm_thermal_of_zone_register(dev, index, tdata,
1318 					    &pmbus_thermal_ops);
1319 	/*
1320 	 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
1321 	 * so ignore that error but forward any other error.
1322 	 */
1323 	if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
1324 		return PTR_ERR(tzd);
1325 
1326 	return 0;
1327 }
1328 
1329 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
1330 					     const char *name, const char *type,
1331 					     int seq, int page, int phase,
1332 					     int reg,
1333 					     enum pmbus_sensor_classes class,
1334 					     bool update, bool readonly,
1335 					     bool convert)
1336 {
1337 	struct pmbus_sensor *sensor;
1338 	struct device_attribute *a;
1339 
1340 	sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL);
1341 	if (!sensor)
1342 		return NULL;
1343 	a = &sensor->attribute;
1344 
1345 	if (type)
1346 		snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
1347 			 name, seq, type);
1348 	else
1349 		snprintf(sensor->name, sizeof(sensor->name), "%s%d",
1350 			 name, seq);
1351 
1352 	if (data->flags & PMBUS_WRITE_PROTECTED)
1353 		readonly = true;
1354 
1355 	sensor->page = page;
1356 	sensor->phase = phase;
1357 	sensor->reg = reg;
1358 	sensor->class = class;
1359 	sensor->update = update;
1360 	sensor->convert = convert;
1361 	sensor->data = -ENODATA;
1362 	pmbus_dev_attr_init(a, sensor->name,
1363 			    readonly ? 0444 : 0644,
1364 			    pmbus_show_sensor, pmbus_set_sensor);
1365 
1366 	if (pmbus_add_attribute(data, &a->attr))
1367 		return NULL;
1368 
1369 	sensor->next = data->sensors;
1370 	data->sensors = sensor;
1371 
1372 	/* temperature sensors with _input values are registered with thermal */
1373 	if (class == PSC_TEMPERATURE && strcmp(type, "input") == 0)
1374 		pmbus_thermal_add_sensor(data, sensor, seq);
1375 
1376 	return sensor;
1377 }
1378 
1379 static int pmbus_add_label(struct pmbus_data *data,
1380 			   const char *name, int seq,
1381 			   const char *lstring, int index, int phase)
1382 {
1383 	struct pmbus_label *label;
1384 	struct device_attribute *a;
1385 
1386 	label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL);
1387 	if (!label)
1388 		return -ENOMEM;
1389 
1390 	a = &label->attribute;
1391 
1392 	snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
1393 	if (!index) {
1394 		if (phase == 0xff)
1395 			strncpy(label->label, lstring,
1396 				sizeof(label->label) - 1);
1397 		else
1398 			snprintf(label->label, sizeof(label->label), "%s.%d",
1399 				 lstring, phase);
1400 	} else {
1401 		if (phase == 0xff)
1402 			snprintf(label->label, sizeof(label->label), "%s%d",
1403 				 lstring, index);
1404 		else
1405 			snprintf(label->label, sizeof(label->label), "%s%d.%d",
1406 				 lstring, index, phase);
1407 	}
1408 
1409 	pmbus_dev_attr_init(a, label->name, 0444, pmbus_show_label, NULL);
1410 	return pmbus_add_attribute(data, &a->attr);
1411 }
1412 
1413 /*
1414  * Search for attributes. Allocate sensors, booleans, and labels as needed.
1415  */
1416 
1417 /*
1418  * The pmbus_limit_attr structure describes a single limit attribute
1419  * and its associated alarm attribute.
1420  */
1421 struct pmbus_limit_attr {
1422 	u16 reg;		/* Limit register */
1423 	u16 sbit;		/* Alarm attribute status bit */
1424 	bool update;		/* True if register needs updates */
1425 	bool low;		/* True if low limit; for limits with compare
1426 				   functions only */
1427 	const char *attr;	/* Attribute name */
1428 	const char *alarm;	/* Alarm attribute name */
1429 };
1430 
1431 /*
1432  * The pmbus_sensor_attr structure describes one sensor attribute. This
1433  * description includes a reference to the associated limit attributes.
1434  */
1435 struct pmbus_sensor_attr {
1436 	u16 reg;			/* sensor register */
1437 	u16 gbit;			/* generic status bit */
1438 	u8 nlimit;			/* # of limit registers */
1439 	enum pmbus_sensor_classes class;/* sensor class */
1440 	const char *label;		/* sensor label */
1441 	bool paged;			/* true if paged sensor */
1442 	bool update;			/* true if update needed */
1443 	bool compare;			/* true if compare function needed */
1444 	u32 func;			/* sensor mask */
1445 	u32 sfunc;			/* sensor status mask */
1446 	int sreg;			/* status register */
1447 	const struct pmbus_limit_attr *limit;/* limit registers */
1448 };
1449 
1450 /*
1451  * Add a set of limit attributes and, if supported, the associated
1452  * alarm attributes.
1453  * returns 0 if no alarm register found, 1 if an alarm register was found,
1454  * < 0 on errors.
1455  */
1456 static int pmbus_add_limit_attrs(struct i2c_client *client,
1457 				 struct pmbus_data *data,
1458 				 const struct pmbus_driver_info *info,
1459 				 const char *name, int index, int page,
1460 				 struct pmbus_sensor *base,
1461 				 const struct pmbus_sensor_attr *attr)
1462 {
1463 	const struct pmbus_limit_attr *l = attr->limit;
1464 	int nlimit = attr->nlimit;
1465 	int have_alarm = 0;
1466 	int i, ret;
1467 	struct pmbus_sensor *curr;
1468 
1469 	for (i = 0; i < nlimit; i++) {
1470 		if (pmbus_check_word_register(client, page, l->reg)) {
1471 			curr = pmbus_add_sensor(data, name, l->attr, index,
1472 						page, 0xff, l->reg, attr->class,
1473 						attr->update || l->update,
1474 						false, true);
1475 			if (!curr)
1476 				return -ENOMEM;
1477 			if (l->sbit && (info->func[page] & attr->sfunc)) {
1478 				ret = pmbus_add_boolean(data, name,
1479 					l->alarm, index,
1480 					attr->compare ?  l->low ? curr : base
1481 						      : NULL,
1482 					attr->compare ? l->low ? base : curr
1483 						      : NULL,
1484 					page, attr->sreg, l->sbit);
1485 				if (ret)
1486 					return ret;
1487 				have_alarm = 1;
1488 			}
1489 		}
1490 		l++;
1491 	}
1492 	return have_alarm;
1493 }
1494 
1495 static int pmbus_add_sensor_attrs_one(struct i2c_client *client,
1496 				      struct pmbus_data *data,
1497 				      const struct pmbus_driver_info *info,
1498 				      const char *name,
1499 				      int index, int page, int phase,
1500 				      const struct pmbus_sensor_attr *attr,
1501 				      bool paged)
1502 {
1503 	struct pmbus_sensor *base;
1504 	bool upper = !!(attr->gbit & 0xff00);	/* need to check STATUS_WORD */
1505 	int ret;
1506 
1507 	if (attr->label) {
1508 		ret = pmbus_add_label(data, name, index, attr->label,
1509 				      paged ? page + 1 : 0, phase);
1510 		if (ret)
1511 			return ret;
1512 	}
1513 	base = pmbus_add_sensor(data, name, "input", index, page, phase,
1514 				attr->reg, attr->class, true, true, true);
1515 	if (!base)
1516 		return -ENOMEM;
1517 	/* No limit and alarm attributes for phase specific sensors */
1518 	if (attr->sfunc && phase == 0xff) {
1519 		ret = pmbus_add_limit_attrs(client, data, info, name,
1520 					    index, page, base, attr);
1521 		if (ret < 0)
1522 			return ret;
1523 		/*
1524 		 * Add generic alarm attribute only if there are no individual
1525 		 * alarm attributes, if there is a global alarm bit, and if
1526 		 * the generic status register (word or byte, depending on
1527 		 * which global bit is set) for this page is accessible.
1528 		 */
1529 		if (!ret && attr->gbit &&
1530 		    (!upper || data->has_status_word) &&
1531 		    pmbus_check_status_register(client, page)) {
1532 			ret = pmbus_add_boolean(data, name, "alarm", index,
1533 						NULL, NULL,
1534 						page, PMBUS_STATUS_WORD,
1535 						attr->gbit);
1536 			if (ret)
1537 				return ret;
1538 		}
1539 	}
1540 	return 0;
1541 }
1542 
1543 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info *info,
1544 				  const struct pmbus_sensor_attr *attr)
1545 {
1546 	int p;
1547 
1548 	if (attr->paged)
1549 		return true;
1550 
1551 	/*
1552 	 * Some attributes may be present on more than one page despite
1553 	 * not being marked with the paged attribute. If that is the case,
1554 	 * then treat the sensor as being paged and add the page suffix to the
1555 	 * attribute name.
1556 	 * We don't just add the paged attribute to all such attributes, in
1557 	 * order to maintain the un-suffixed labels in the case where the
1558 	 * attribute is only on page 0.
1559 	 */
1560 	for (p = 1; p < info->pages; p++) {
1561 		if (info->func[p] & attr->func)
1562 			return true;
1563 	}
1564 	return false;
1565 }
1566 
1567 static int pmbus_add_sensor_attrs(struct i2c_client *client,
1568 				  struct pmbus_data *data,
1569 				  const char *name,
1570 				  const struct pmbus_sensor_attr *attrs,
1571 				  int nattrs)
1572 {
1573 	const struct pmbus_driver_info *info = data->info;
1574 	int index, i;
1575 	int ret;
1576 
1577 	index = 1;
1578 	for (i = 0; i < nattrs; i++) {
1579 		int page, pages;
1580 		bool paged = pmbus_sensor_is_paged(info, attrs);
1581 
1582 		pages = paged ? info->pages : 1;
1583 		for (page = 0; page < pages; page++) {
1584 			if (info->func[page] & attrs->func) {
1585 				ret = pmbus_add_sensor_attrs_one(client, data, info,
1586 								 name, index, page,
1587 								 0xff, attrs, paged);
1588 				if (ret)
1589 					return ret;
1590 				index++;
1591 			}
1592 			if (info->phases[page]) {
1593 				int phase;
1594 
1595 				for (phase = 0; phase < info->phases[page];
1596 				     phase++) {
1597 					if (!(info->pfunc[phase] & attrs->func))
1598 						continue;
1599 					ret = pmbus_add_sensor_attrs_one(client,
1600 						data, info, name, index, page,
1601 						phase, attrs, paged);
1602 					if (ret)
1603 						return ret;
1604 					index++;
1605 				}
1606 			}
1607 		}
1608 		attrs++;
1609 	}
1610 	return 0;
1611 }
1612 
1613 static const struct pmbus_limit_attr vin_limit_attrs[] = {
1614 	{
1615 		.reg = PMBUS_VIN_UV_WARN_LIMIT,
1616 		.attr = "min",
1617 		.alarm = "min_alarm",
1618 		.sbit = PB_VOLTAGE_UV_WARNING,
1619 	}, {
1620 		.reg = PMBUS_VIN_UV_FAULT_LIMIT,
1621 		.attr = "lcrit",
1622 		.alarm = "lcrit_alarm",
1623 		.sbit = PB_VOLTAGE_UV_FAULT | PB_VOLTAGE_VIN_OFF,
1624 	}, {
1625 		.reg = PMBUS_VIN_OV_WARN_LIMIT,
1626 		.attr = "max",
1627 		.alarm = "max_alarm",
1628 		.sbit = PB_VOLTAGE_OV_WARNING,
1629 	}, {
1630 		.reg = PMBUS_VIN_OV_FAULT_LIMIT,
1631 		.attr = "crit",
1632 		.alarm = "crit_alarm",
1633 		.sbit = PB_VOLTAGE_OV_FAULT,
1634 	}, {
1635 		.reg = PMBUS_VIRT_READ_VIN_AVG,
1636 		.update = true,
1637 		.attr = "average",
1638 	}, {
1639 		.reg = PMBUS_VIRT_READ_VIN_MIN,
1640 		.update = true,
1641 		.attr = "lowest",
1642 	}, {
1643 		.reg = PMBUS_VIRT_READ_VIN_MAX,
1644 		.update = true,
1645 		.attr = "highest",
1646 	}, {
1647 		.reg = PMBUS_VIRT_RESET_VIN_HISTORY,
1648 		.attr = "reset_history",
1649 	}, {
1650 		.reg = PMBUS_MFR_VIN_MIN,
1651 		.attr = "rated_min",
1652 	}, {
1653 		.reg = PMBUS_MFR_VIN_MAX,
1654 		.attr = "rated_max",
1655 	},
1656 };
1657 
1658 static const struct pmbus_limit_attr vmon_limit_attrs[] = {
1659 	{
1660 		.reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT,
1661 		.attr = "min",
1662 		.alarm = "min_alarm",
1663 		.sbit = PB_VOLTAGE_UV_WARNING,
1664 	}, {
1665 		.reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT,
1666 		.attr = "lcrit",
1667 		.alarm = "lcrit_alarm",
1668 		.sbit = PB_VOLTAGE_UV_FAULT,
1669 	}, {
1670 		.reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT,
1671 		.attr = "max",
1672 		.alarm = "max_alarm",
1673 		.sbit = PB_VOLTAGE_OV_WARNING,
1674 	}, {
1675 		.reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT,
1676 		.attr = "crit",
1677 		.alarm = "crit_alarm",
1678 		.sbit = PB_VOLTAGE_OV_FAULT,
1679 	}
1680 };
1681 
1682 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1683 	{
1684 		.reg = PMBUS_VOUT_UV_WARN_LIMIT,
1685 		.attr = "min",
1686 		.alarm = "min_alarm",
1687 		.sbit = PB_VOLTAGE_UV_WARNING,
1688 	}, {
1689 		.reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1690 		.attr = "lcrit",
1691 		.alarm = "lcrit_alarm",
1692 		.sbit = PB_VOLTAGE_UV_FAULT,
1693 	}, {
1694 		.reg = PMBUS_VOUT_OV_WARN_LIMIT,
1695 		.attr = "max",
1696 		.alarm = "max_alarm",
1697 		.sbit = PB_VOLTAGE_OV_WARNING,
1698 	}, {
1699 		.reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1700 		.attr = "crit",
1701 		.alarm = "crit_alarm",
1702 		.sbit = PB_VOLTAGE_OV_FAULT,
1703 	}, {
1704 		.reg = PMBUS_VIRT_READ_VOUT_AVG,
1705 		.update = true,
1706 		.attr = "average",
1707 	}, {
1708 		.reg = PMBUS_VIRT_READ_VOUT_MIN,
1709 		.update = true,
1710 		.attr = "lowest",
1711 	}, {
1712 		.reg = PMBUS_VIRT_READ_VOUT_MAX,
1713 		.update = true,
1714 		.attr = "highest",
1715 	}, {
1716 		.reg = PMBUS_VIRT_RESET_VOUT_HISTORY,
1717 		.attr = "reset_history",
1718 	}, {
1719 		.reg = PMBUS_MFR_VOUT_MIN,
1720 		.attr = "rated_min",
1721 	}, {
1722 		.reg = PMBUS_MFR_VOUT_MAX,
1723 		.attr = "rated_max",
1724 	},
1725 };
1726 
1727 static const struct pmbus_sensor_attr voltage_attributes[] = {
1728 	{
1729 		.reg = PMBUS_READ_VIN,
1730 		.class = PSC_VOLTAGE_IN,
1731 		.label = "vin",
1732 		.func = PMBUS_HAVE_VIN,
1733 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1734 		.sreg = PMBUS_STATUS_INPUT,
1735 		.gbit = PB_STATUS_VIN_UV,
1736 		.limit = vin_limit_attrs,
1737 		.nlimit = ARRAY_SIZE(vin_limit_attrs),
1738 	}, {
1739 		.reg = PMBUS_VIRT_READ_VMON,
1740 		.class = PSC_VOLTAGE_IN,
1741 		.label = "vmon",
1742 		.func = PMBUS_HAVE_VMON,
1743 		.sfunc = PMBUS_HAVE_STATUS_VMON,
1744 		.sreg = PMBUS_VIRT_STATUS_VMON,
1745 		.limit = vmon_limit_attrs,
1746 		.nlimit = ARRAY_SIZE(vmon_limit_attrs),
1747 	}, {
1748 		.reg = PMBUS_READ_VCAP,
1749 		.class = PSC_VOLTAGE_IN,
1750 		.label = "vcap",
1751 		.func = PMBUS_HAVE_VCAP,
1752 	}, {
1753 		.reg = PMBUS_READ_VOUT,
1754 		.class = PSC_VOLTAGE_OUT,
1755 		.label = "vout",
1756 		.paged = true,
1757 		.func = PMBUS_HAVE_VOUT,
1758 		.sfunc = PMBUS_HAVE_STATUS_VOUT,
1759 		.sreg = PMBUS_STATUS_VOUT,
1760 		.gbit = PB_STATUS_VOUT_OV,
1761 		.limit = vout_limit_attrs,
1762 		.nlimit = ARRAY_SIZE(vout_limit_attrs),
1763 	}
1764 };
1765 
1766 /* Current attributes */
1767 
1768 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1769 	{
1770 		.reg = PMBUS_IIN_OC_WARN_LIMIT,
1771 		.attr = "max",
1772 		.alarm = "max_alarm",
1773 		.sbit = PB_IIN_OC_WARNING,
1774 	}, {
1775 		.reg = PMBUS_IIN_OC_FAULT_LIMIT,
1776 		.attr = "crit",
1777 		.alarm = "crit_alarm",
1778 		.sbit = PB_IIN_OC_FAULT,
1779 	}, {
1780 		.reg = PMBUS_VIRT_READ_IIN_AVG,
1781 		.update = true,
1782 		.attr = "average",
1783 	}, {
1784 		.reg = PMBUS_VIRT_READ_IIN_MIN,
1785 		.update = true,
1786 		.attr = "lowest",
1787 	}, {
1788 		.reg = PMBUS_VIRT_READ_IIN_MAX,
1789 		.update = true,
1790 		.attr = "highest",
1791 	}, {
1792 		.reg = PMBUS_VIRT_RESET_IIN_HISTORY,
1793 		.attr = "reset_history",
1794 	}, {
1795 		.reg = PMBUS_MFR_IIN_MAX,
1796 		.attr = "rated_max",
1797 	},
1798 };
1799 
1800 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1801 	{
1802 		.reg = PMBUS_IOUT_OC_WARN_LIMIT,
1803 		.attr = "max",
1804 		.alarm = "max_alarm",
1805 		.sbit = PB_IOUT_OC_WARNING,
1806 	}, {
1807 		.reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1808 		.attr = "lcrit",
1809 		.alarm = "lcrit_alarm",
1810 		.sbit = PB_IOUT_UC_FAULT,
1811 	}, {
1812 		.reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1813 		.attr = "crit",
1814 		.alarm = "crit_alarm",
1815 		.sbit = PB_IOUT_OC_FAULT,
1816 	}, {
1817 		.reg = PMBUS_VIRT_READ_IOUT_AVG,
1818 		.update = true,
1819 		.attr = "average",
1820 	}, {
1821 		.reg = PMBUS_VIRT_READ_IOUT_MIN,
1822 		.update = true,
1823 		.attr = "lowest",
1824 	}, {
1825 		.reg = PMBUS_VIRT_READ_IOUT_MAX,
1826 		.update = true,
1827 		.attr = "highest",
1828 	}, {
1829 		.reg = PMBUS_VIRT_RESET_IOUT_HISTORY,
1830 		.attr = "reset_history",
1831 	}, {
1832 		.reg = PMBUS_MFR_IOUT_MAX,
1833 		.attr = "rated_max",
1834 	},
1835 };
1836 
1837 static const struct pmbus_sensor_attr current_attributes[] = {
1838 	{
1839 		.reg = PMBUS_READ_IIN,
1840 		.class = PSC_CURRENT_IN,
1841 		.label = "iin",
1842 		.func = PMBUS_HAVE_IIN,
1843 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1844 		.sreg = PMBUS_STATUS_INPUT,
1845 		.gbit = PB_STATUS_INPUT,
1846 		.limit = iin_limit_attrs,
1847 		.nlimit = ARRAY_SIZE(iin_limit_attrs),
1848 	}, {
1849 		.reg = PMBUS_READ_IOUT,
1850 		.class = PSC_CURRENT_OUT,
1851 		.label = "iout",
1852 		.paged = true,
1853 		.func = PMBUS_HAVE_IOUT,
1854 		.sfunc = PMBUS_HAVE_STATUS_IOUT,
1855 		.sreg = PMBUS_STATUS_IOUT,
1856 		.gbit = PB_STATUS_IOUT_OC,
1857 		.limit = iout_limit_attrs,
1858 		.nlimit = ARRAY_SIZE(iout_limit_attrs),
1859 	}
1860 };
1861 
1862 /* Power attributes */
1863 
1864 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1865 	{
1866 		.reg = PMBUS_PIN_OP_WARN_LIMIT,
1867 		.attr = "max",
1868 		.alarm = "alarm",
1869 		.sbit = PB_PIN_OP_WARNING,
1870 	}, {
1871 		.reg = PMBUS_VIRT_READ_PIN_AVG,
1872 		.update = true,
1873 		.attr = "average",
1874 	}, {
1875 		.reg = PMBUS_VIRT_READ_PIN_MIN,
1876 		.update = true,
1877 		.attr = "input_lowest",
1878 	}, {
1879 		.reg = PMBUS_VIRT_READ_PIN_MAX,
1880 		.update = true,
1881 		.attr = "input_highest",
1882 	}, {
1883 		.reg = PMBUS_VIRT_RESET_PIN_HISTORY,
1884 		.attr = "reset_history",
1885 	}, {
1886 		.reg = PMBUS_MFR_PIN_MAX,
1887 		.attr = "rated_max",
1888 	},
1889 };
1890 
1891 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1892 	{
1893 		.reg = PMBUS_POUT_MAX,
1894 		.attr = "cap",
1895 		.alarm = "cap_alarm",
1896 		.sbit = PB_POWER_LIMITING,
1897 	}, {
1898 		.reg = PMBUS_POUT_OP_WARN_LIMIT,
1899 		.attr = "max",
1900 		.alarm = "max_alarm",
1901 		.sbit = PB_POUT_OP_WARNING,
1902 	}, {
1903 		.reg = PMBUS_POUT_OP_FAULT_LIMIT,
1904 		.attr = "crit",
1905 		.alarm = "crit_alarm",
1906 		.sbit = PB_POUT_OP_FAULT,
1907 	}, {
1908 		.reg = PMBUS_VIRT_READ_POUT_AVG,
1909 		.update = true,
1910 		.attr = "average",
1911 	}, {
1912 		.reg = PMBUS_VIRT_READ_POUT_MIN,
1913 		.update = true,
1914 		.attr = "input_lowest",
1915 	}, {
1916 		.reg = PMBUS_VIRT_READ_POUT_MAX,
1917 		.update = true,
1918 		.attr = "input_highest",
1919 	}, {
1920 		.reg = PMBUS_VIRT_RESET_POUT_HISTORY,
1921 		.attr = "reset_history",
1922 	}, {
1923 		.reg = PMBUS_MFR_POUT_MAX,
1924 		.attr = "rated_max",
1925 	},
1926 };
1927 
1928 static const struct pmbus_sensor_attr power_attributes[] = {
1929 	{
1930 		.reg = PMBUS_READ_PIN,
1931 		.class = PSC_POWER,
1932 		.label = "pin",
1933 		.func = PMBUS_HAVE_PIN,
1934 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1935 		.sreg = PMBUS_STATUS_INPUT,
1936 		.gbit = PB_STATUS_INPUT,
1937 		.limit = pin_limit_attrs,
1938 		.nlimit = ARRAY_SIZE(pin_limit_attrs),
1939 	}, {
1940 		.reg = PMBUS_READ_POUT,
1941 		.class = PSC_POWER,
1942 		.label = "pout",
1943 		.paged = true,
1944 		.func = PMBUS_HAVE_POUT,
1945 		.sfunc = PMBUS_HAVE_STATUS_IOUT,
1946 		.sreg = PMBUS_STATUS_IOUT,
1947 		.limit = pout_limit_attrs,
1948 		.nlimit = ARRAY_SIZE(pout_limit_attrs),
1949 	}
1950 };
1951 
1952 /* Temperature atributes */
1953 
1954 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1955 	{
1956 		.reg = PMBUS_UT_WARN_LIMIT,
1957 		.low = true,
1958 		.attr = "min",
1959 		.alarm = "min_alarm",
1960 		.sbit = PB_TEMP_UT_WARNING,
1961 	}, {
1962 		.reg = PMBUS_UT_FAULT_LIMIT,
1963 		.low = true,
1964 		.attr = "lcrit",
1965 		.alarm = "lcrit_alarm",
1966 		.sbit = PB_TEMP_UT_FAULT,
1967 	}, {
1968 		.reg = PMBUS_OT_WARN_LIMIT,
1969 		.attr = "max",
1970 		.alarm = "max_alarm",
1971 		.sbit = PB_TEMP_OT_WARNING,
1972 	}, {
1973 		.reg = PMBUS_OT_FAULT_LIMIT,
1974 		.attr = "crit",
1975 		.alarm = "crit_alarm",
1976 		.sbit = PB_TEMP_OT_FAULT,
1977 	}, {
1978 		.reg = PMBUS_VIRT_READ_TEMP_MIN,
1979 		.attr = "lowest",
1980 	}, {
1981 		.reg = PMBUS_VIRT_READ_TEMP_AVG,
1982 		.attr = "average",
1983 	}, {
1984 		.reg = PMBUS_VIRT_READ_TEMP_MAX,
1985 		.attr = "highest",
1986 	}, {
1987 		.reg = PMBUS_VIRT_RESET_TEMP_HISTORY,
1988 		.attr = "reset_history",
1989 	}, {
1990 		.reg = PMBUS_MFR_MAX_TEMP_1,
1991 		.attr = "rated_max",
1992 	},
1993 };
1994 
1995 static const struct pmbus_limit_attr temp_limit_attrs2[] = {
1996 	{
1997 		.reg = PMBUS_UT_WARN_LIMIT,
1998 		.low = true,
1999 		.attr = "min",
2000 		.alarm = "min_alarm",
2001 		.sbit = PB_TEMP_UT_WARNING,
2002 	}, {
2003 		.reg = PMBUS_UT_FAULT_LIMIT,
2004 		.low = true,
2005 		.attr = "lcrit",
2006 		.alarm = "lcrit_alarm",
2007 		.sbit = PB_TEMP_UT_FAULT,
2008 	}, {
2009 		.reg = PMBUS_OT_WARN_LIMIT,
2010 		.attr = "max",
2011 		.alarm = "max_alarm",
2012 		.sbit = PB_TEMP_OT_WARNING,
2013 	}, {
2014 		.reg = PMBUS_OT_FAULT_LIMIT,
2015 		.attr = "crit",
2016 		.alarm = "crit_alarm",
2017 		.sbit = PB_TEMP_OT_FAULT,
2018 	}, {
2019 		.reg = PMBUS_VIRT_READ_TEMP2_MIN,
2020 		.attr = "lowest",
2021 	}, {
2022 		.reg = PMBUS_VIRT_READ_TEMP2_AVG,
2023 		.attr = "average",
2024 	}, {
2025 		.reg = PMBUS_VIRT_READ_TEMP2_MAX,
2026 		.attr = "highest",
2027 	}, {
2028 		.reg = PMBUS_VIRT_RESET_TEMP2_HISTORY,
2029 		.attr = "reset_history",
2030 	}, {
2031 		.reg = PMBUS_MFR_MAX_TEMP_2,
2032 		.attr = "rated_max",
2033 	},
2034 };
2035 
2036 static const struct pmbus_limit_attr temp_limit_attrs3[] = {
2037 	{
2038 		.reg = PMBUS_UT_WARN_LIMIT,
2039 		.low = true,
2040 		.attr = "min",
2041 		.alarm = "min_alarm",
2042 		.sbit = PB_TEMP_UT_WARNING,
2043 	}, {
2044 		.reg = PMBUS_UT_FAULT_LIMIT,
2045 		.low = true,
2046 		.attr = "lcrit",
2047 		.alarm = "lcrit_alarm",
2048 		.sbit = PB_TEMP_UT_FAULT,
2049 	}, {
2050 		.reg = PMBUS_OT_WARN_LIMIT,
2051 		.attr = "max",
2052 		.alarm = "max_alarm",
2053 		.sbit = PB_TEMP_OT_WARNING,
2054 	}, {
2055 		.reg = PMBUS_OT_FAULT_LIMIT,
2056 		.attr = "crit",
2057 		.alarm = "crit_alarm",
2058 		.sbit = PB_TEMP_OT_FAULT,
2059 	}, {
2060 		.reg = PMBUS_MFR_MAX_TEMP_3,
2061 		.attr = "rated_max",
2062 	},
2063 };
2064 
2065 static const struct pmbus_sensor_attr temp_attributes[] = {
2066 	{
2067 		.reg = PMBUS_READ_TEMPERATURE_1,
2068 		.class = PSC_TEMPERATURE,
2069 		.paged = true,
2070 		.update = true,
2071 		.compare = true,
2072 		.func = PMBUS_HAVE_TEMP,
2073 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
2074 		.sreg = PMBUS_STATUS_TEMPERATURE,
2075 		.gbit = PB_STATUS_TEMPERATURE,
2076 		.limit = temp_limit_attrs,
2077 		.nlimit = ARRAY_SIZE(temp_limit_attrs),
2078 	}, {
2079 		.reg = PMBUS_READ_TEMPERATURE_2,
2080 		.class = PSC_TEMPERATURE,
2081 		.paged = true,
2082 		.update = true,
2083 		.compare = true,
2084 		.func = PMBUS_HAVE_TEMP2,
2085 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
2086 		.sreg = PMBUS_STATUS_TEMPERATURE,
2087 		.gbit = PB_STATUS_TEMPERATURE,
2088 		.limit = temp_limit_attrs2,
2089 		.nlimit = ARRAY_SIZE(temp_limit_attrs2),
2090 	}, {
2091 		.reg = PMBUS_READ_TEMPERATURE_3,
2092 		.class = PSC_TEMPERATURE,
2093 		.paged = true,
2094 		.update = true,
2095 		.compare = true,
2096 		.func = PMBUS_HAVE_TEMP3,
2097 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
2098 		.sreg = PMBUS_STATUS_TEMPERATURE,
2099 		.gbit = PB_STATUS_TEMPERATURE,
2100 		.limit = temp_limit_attrs3,
2101 		.nlimit = ARRAY_SIZE(temp_limit_attrs3),
2102 	}
2103 };
2104 
2105 static const int pmbus_fan_registers[] = {
2106 	PMBUS_READ_FAN_SPEED_1,
2107 	PMBUS_READ_FAN_SPEED_2,
2108 	PMBUS_READ_FAN_SPEED_3,
2109 	PMBUS_READ_FAN_SPEED_4
2110 };
2111 
2112 static const int pmbus_fan_status_registers[] = {
2113 	PMBUS_STATUS_FAN_12,
2114 	PMBUS_STATUS_FAN_12,
2115 	PMBUS_STATUS_FAN_34,
2116 	PMBUS_STATUS_FAN_34
2117 };
2118 
2119 static const u32 pmbus_fan_flags[] = {
2120 	PMBUS_HAVE_FAN12,
2121 	PMBUS_HAVE_FAN12,
2122 	PMBUS_HAVE_FAN34,
2123 	PMBUS_HAVE_FAN34
2124 };
2125 
2126 static const u32 pmbus_fan_status_flags[] = {
2127 	PMBUS_HAVE_STATUS_FAN12,
2128 	PMBUS_HAVE_STATUS_FAN12,
2129 	PMBUS_HAVE_STATUS_FAN34,
2130 	PMBUS_HAVE_STATUS_FAN34
2131 };
2132 
2133 /* Fans */
2134 
2135 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */
2136 static int pmbus_add_fan_ctrl(struct i2c_client *client,
2137 		struct pmbus_data *data, int index, int page, int id,
2138 		u8 config)
2139 {
2140 	struct pmbus_sensor *sensor;
2141 
2142 	sensor = pmbus_add_sensor(data, "fan", "target", index, page,
2143 				  0xff, PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN,
2144 				  false, false, true);
2145 
2146 	if (!sensor)
2147 		return -ENOMEM;
2148 
2149 	if (!((data->info->func[page] & PMBUS_HAVE_PWM12) ||
2150 			(data->info->func[page] & PMBUS_HAVE_PWM34)))
2151 		return 0;
2152 
2153 	sensor = pmbus_add_sensor(data, "pwm", NULL, index, page,
2154 				  0xff, PMBUS_VIRT_PWM_1 + id, PSC_PWM,
2155 				  false, false, true);
2156 
2157 	if (!sensor)
2158 		return -ENOMEM;
2159 
2160 	sensor = pmbus_add_sensor(data, "pwm", "enable", index, page,
2161 				  0xff, PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM,
2162 				  true, false, false);
2163 
2164 	if (!sensor)
2165 		return -ENOMEM;
2166 
2167 	return 0;
2168 }
2169 
2170 static int pmbus_add_fan_attributes(struct i2c_client *client,
2171 				    struct pmbus_data *data)
2172 {
2173 	const struct pmbus_driver_info *info = data->info;
2174 	int index = 1;
2175 	int page;
2176 	int ret;
2177 
2178 	for (page = 0; page < info->pages; page++) {
2179 		int f;
2180 
2181 		for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
2182 			int regval;
2183 
2184 			if (!(info->func[page] & pmbus_fan_flags[f]))
2185 				break;
2186 
2187 			if (!pmbus_check_word_register(client, page,
2188 						       pmbus_fan_registers[f]))
2189 				break;
2190 
2191 			/*
2192 			 * Skip fan if not installed.
2193 			 * Each fan configuration register covers multiple fans,
2194 			 * so we have to do some magic.
2195 			 */
2196 			regval = _pmbus_read_byte_data(client, page,
2197 				pmbus_fan_config_registers[f]);
2198 			if (regval < 0 ||
2199 			    (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
2200 				continue;
2201 
2202 			if (pmbus_add_sensor(data, "fan", "input", index,
2203 					     page, 0xff, pmbus_fan_registers[f],
2204 					     PSC_FAN, true, true, true) == NULL)
2205 				return -ENOMEM;
2206 
2207 			/* Fan control */
2208 			if (pmbus_check_word_register(client, page,
2209 					pmbus_fan_command_registers[f])) {
2210 				ret = pmbus_add_fan_ctrl(client, data, index,
2211 							 page, f, regval);
2212 				if (ret < 0)
2213 					return ret;
2214 			}
2215 
2216 			/*
2217 			 * Each fan status register covers multiple fans,
2218 			 * so we have to do some magic.
2219 			 */
2220 			if ((info->func[page] & pmbus_fan_status_flags[f]) &&
2221 			    pmbus_check_byte_register(client,
2222 					page, pmbus_fan_status_registers[f])) {
2223 				int reg;
2224 
2225 				if (f > 1)	/* fan 3, 4 */
2226 					reg = PMBUS_STATUS_FAN_34;
2227 				else
2228 					reg = PMBUS_STATUS_FAN_12;
2229 				ret = pmbus_add_boolean(data, "fan",
2230 					"alarm", index, NULL, NULL, page, reg,
2231 					PB_FAN_FAN1_WARNING >> (f & 1));
2232 				if (ret)
2233 					return ret;
2234 				ret = pmbus_add_boolean(data, "fan",
2235 					"fault", index, NULL, NULL, page, reg,
2236 					PB_FAN_FAN1_FAULT >> (f & 1));
2237 				if (ret)
2238 					return ret;
2239 			}
2240 			index++;
2241 		}
2242 	}
2243 	return 0;
2244 }
2245 
2246 struct pmbus_samples_attr {
2247 	int reg;
2248 	char *name;
2249 };
2250 
2251 struct pmbus_samples_reg {
2252 	int page;
2253 	struct pmbus_samples_attr *attr;
2254 	struct device_attribute dev_attr;
2255 };
2256 
2257 static struct pmbus_samples_attr pmbus_samples_registers[] = {
2258 	{
2259 		.reg = PMBUS_VIRT_SAMPLES,
2260 		.name = "samples",
2261 	}, {
2262 		.reg = PMBUS_VIRT_IN_SAMPLES,
2263 		.name = "in_samples",
2264 	}, {
2265 		.reg = PMBUS_VIRT_CURR_SAMPLES,
2266 		.name = "curr_samples",
2267 	}, {
2268 		.reg = PMBUS_VIRT_POWER_SAMPLES,
2269 		.name = "power_samples",
2270 	}, {
2271 		.reg = PMBUS_VIRT_TEMP_SAMPLES,
2272 		.name = "temp_samples",
2273 	}
2274 };
2275 
2276 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr)
2277 
2278 static ssize_t pmbus_show_samples(struct device *dev,
2279 				  struct device_attribute *devattr, char *buf)
2280 {
2281 	int val;
2282 	struct i2c_client *client = to_i2c_client(dev->parent);
2283 	struct pmbus_samples_reg *reg = to_samples_reg(devattr);
2284 	struct pmbus_data *data = i2c_get_clientdata(client);
2285 
2286 	mutex_lock(&data->update_lock);
2287 	val = _pmbus_read_word_data(client, reg->page, 0xff, reg->attr->reg);
2288 	mutex_unlock(&data->update_lock);
2289 	if (val < 0)
2290 		return val;
2291 
2292 	return sysfs_emit(buf, "%d\n", val);
2293 }
2294 
2295 static ssize_t pmbus_set_samples(struct device *dev,
2296 				 struct device_attribute *devattr,
2297 				 const char *buf, size_t count)
2298 {
2299 	int ret;
2300 	long val;
2301 	struct i2c_client *client = to_i2c_client(dev->parent);
2302 	struct pmbus_samples_reg *reg = to_samples_reg(devattr);
2303 	struct pmbus_data *data = i2c_get_clientdata(client);
2304 
2305 	if (kstrtol(buf, 0, &val) < 0)
2306 		return -EINVAL;
2307 
2308 	mutex_lock(&data->update_lock);
2309 	ret = _pmbus_write_word_data(client, reg->page, reg->attr->reg, val);
2310 	mutex_unlock(&data->update_lock);
2311 
2312 	return ret ? : count;
2313 }
2314 
2315 static int pmbus_add_samples_attr(struct pmbus_data *data, int page,
2316 				  struct pmbus_samples_attr *attr)
2317 {
2318 	struct pmbus_samples_reg *reg;
2319 
2320 	reg = devm_kzalloc(data->dev, sizeof(*reg), GFP_KERNEL);
2321 	if (!reg)
2322 		return -ENOMEM;
2323 
2324 	reg->attr = attr;
2325 	reg->page = page;
2326 
2327 	pmbus_dev_attr_init(&reg->dev_attr, attr->name, 0644,
2328 			    pmbus_show_samples, pmbus_set_samples);
2329 
2330 	return pmbus_add_attribute(data, &reg->dev_attr.attr);
2331 }
2332 
2333 static int pmbus_add_samples_attributes(struct i2c_client *client,
2334 					struct pmbus_data *data)
2335 {
2336 	const struct pmbus_driver_info *info = data->info;
2337 	int s;
2338 
2339 	if (!(info->func[0] & PMBUS_HAVE_SAMPLES))
2340 		return 0;
2341 
2342 	for (s = 0; s < ARRAY_SIZE(pmbus_samples_registers); s++) {
2343 		struct pmbus_samples_attr *attr;
2344 		int ret;
2345 
2346 		attr = &pmbus_samples_registers[s];
2347 		if (!pmbus_check_word_register(client, 0, attr->reg))
2348 			continue;
2349 
2350 		ret = pmbus_add_samples_attr(data, 0, attr);
2351 		if (ret)
2352 			return ret;
2353 	}
2354 
2355 	return 0;
2356 }
2357 
2358 static int pmbus_find_attributes(struct i2c_client *client,
2359 				 struct pmbus_data *data)
2360 {
2361 	int ret;
2362 
2363 	/* Voltage sensors */
2364 	ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
2365 				     ARRAY_SIZE(voltage_attributes));
2366 	if (ret)
2367 		return ret;
2368 
2369 	/* Current sensors */
2370 	ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
2371 				     ARRAY_SIZE(current_attributes));
2372 	if (ret)
2373 		return ret;
2374 
2375 	/* Power sensors */
2376 	ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes,
2377 				     ARRAY_SIZE(power_attributes));
2378 	if (ret)
2379 		return ret;
2380 
2381 	/* Temperature sensors */
2382 	ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
2383 				     ARRAY_SIZE(temp_attributes));
2384 	if (ret)
2385 		return ret;
2386 
2387 	/* Fans */
2388 	ret = pmbus_add_fan_attributes(client, data);
2389 	if (ret)
2390 		return ret;
2391 
2392 	ret = pmbus_add_samples_attributes(client, data);
2393 	return ret;
2394 }
2395 
2396 /*
2397  * The pmbus_class_attr_map structure maps one sensor class to
2398  * it's corresponding sensor attributes array.
2399  */
2400 struct pmbus_class_attr_map {
2401 	enum pmbus_sensor_classes class;
2402 	int nattr;
2403 	const struct pmbus_sensor_attr *attr;
2404 };
2405 
2406 static const struct pmbus_class_attr_map class_attr_map[] = {
2407 	{
2408 		.class = PSC_VOLTAGE_IN,
2409 		.attr = voltage_attributes,
2410 		.nattr = ARRAY_SIZE(voltage_attributes),
2411 	}, {
2412 		.class = PSC_VOLTAGE_OUT,
2413 		.attr = voltage_attributes,
2414 		.nattr = ARRAY_SIZE(voltage_attributes),
2415 	}, {
2416 		.class = PSC_CURRENT_IN,
2417 		.attr = current_attributes,
2418 		.nattr = ARRAY_SIZE(current_attributes),
2419 	}, {
2420 		.class = PSC_CURRENT_OUT,
2421 		.attr = current_attributes,
2422 		.nattr = ARRAY_SIZE(current_attributes),
2423 	}, {
2424 		.class = PSC_POWER,
2425 		.attr = power_attributes,
2426 		.nattr = ARRAY_SIZE(power_attributes),
2427 	}, {
2428 		.class = PSC_TEMPERATURE,
2429 		.attr = temp_attributes,
2430 		.nattr = ARRAY_SIZE(temp_attributes),
2431 	}
2432 };
2433 
2434 /*
2435  * Read the coefficients for direct mode.
2436  */
2437 static int pmbus_read_coefficients(struct i2c_client *client,
2438 				   struct pmbus_driver_info *info,
2439 				   const struct pmbus_sensor_attr *attr)
2440 {
2441 	int rv;
2442 	union i2c_smbus_data data;
2443 	enum pmbus_sensor_classes class = attr->class;
2444 	s8 R;
2445 	s16 m, b;
2446 
2447 	data.block[0] = 2;
2448 	data.block[1] = attr->reg;
2449 	data.block[2] = 0x01;
2450 
2451 	rv = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2452 			    I2C_SMBUS_WRITE, PMBUS_COEFFICIENTS,
2453 			    I2C_SMBUS_BLOCK_PROC_CALL, &data);
2454 
2455 	if (rv < 0)
2456 		return rv;
2457 
2458 	if (data.block[0] != 5)
2459 		return -EIO;
2460 
2461 	m = data.block[1] | (data.block[2] << 8);
2462 	b = data.block[3] | (data.block[4] << 8);
2463 	R = data.block[5];
2464 	info->m[class] = m;
2465 	info->b[class] = b;
2466 	info->R[class] = R;
2467 
2468 	return rv;
2469 }
2470 
2471 static int pmbus_init_coefficients(struct i2c_client *client,
2472 				   struct pmbus_driver_info *info)
2473 {
2474 	int i, n, ret = -EINVAL;
2475 	const struct pmbus_class_attr_map *map;
2476 	const struct pmbus_sensor_attr *attr;
2477 
2478 	for (i = 0; i < ARRAY_SIZE(class_attr_map); i++) {
2479 		map = &class_attr_map[i];
2480 		if (info->format[map->class] != direct)
2481 			continue;
2482 		for (n = 0; n < map->nattr; n++) {
2483 			attr = &map->attr[n];
2484 			if (map->class != attr->class)
2485 				continue;
2486 			ret = pmbus_read_coefficients(client, info, attr);
2487 			if (ret >= 0)
2488 				break;
2489 		}
2490 		if (ret < 0) {
2491 			dev_err(&client->dev,
2492 				"No coefficients found for sensor class %d\n",
2493 				map->class);
2494 			return -EINVAL;
2495 		}
2496 	}
2497 
2498 	return 0;
2499 }
2500 
2501 /*
2502  * Identify chip parameters.
2503  * This function is called for all chips.
2504  */
2505 static int pmbus_identify_common(struct i2c_client *client,
2506 				 struct pmbus_data *data, int page)
2507 {
2508 	int vout_mode = -1;
2509 
2510 	if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE))
2511 		vout_mode = _pmbus_read_byte_data(client, page,
2512 						  PMBUS_VOUT_MODE);
2513 	if (vout_mode >= 0 && vout_mode != 0xff) {
2514 		/*
2515 		 * Not all chips support the VOUT_MODE command,
2516 		 * so a failure to read it is not an error.
2517 		 */
2518 		switch (vout_mode >> 5) {
2519 		case 0:	/* linear mode      */
2520 			if (data->info->format[PSC_VOLTAGE_OUT] != linear)
2521 				return -ENODEV;
2522 
2523 			data->exponent[page] = ((s8)(vout_mode << 3)) >> 3;
2524 			break;
2525 		case 1: /* VID mode         */
2526 			if (data->info->format[PSC_VOLTAGE_OUT] != vid)
2527 				return -ENODEV;
2528 			break;
2529 		case 2:	/* direct mode      */
2530 			if (data->info->format[PSC_VOLTAGE_OUT] != direct)
2531 				return -ENODEV;
2532 			break;
2533 		case 3:	/* ieee 754 half precision */
2534 			if (data->info->format[PSC_VOLTAGE_OUT] != ieee754)
2535 				return -ENODEV;
2536 			break;
2537 		default:
2538 			return -ENODEV;
2539 		}
2540 	}
2541 
2542 	pmbus_clear_fault_page(client, page);
2543 	return 0;
2544 }
2545 
2546 static int pmbus_read_status_byte(struct i2c_client *client, int page)
2547 {
2548 	return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
2549 }
2550 
2551 static int pmbus_read_status_word(struct i2c_client *client, int page)
2552 {
2553 	return _pmbus_read_word_data(client, page, 0xff, PMBUS_STATUS_WORD);
2554 }
2555 
2556 /* PEC attribute support */
2557 
2558 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
2559 			char *buf)
2560 {
2561 	struct i2c_client *client = to_i2c_client(dev);
2562 
2563 	return sysfs_emit(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
2564 }
2565 
2566 static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
2567 			 const char *buf, size_t count)
2568 {
2569 	struct i2c_client *client = to_i2c_client(dev);
2570 	bool enable;
2571 	int err;
2572 
2573 	err = kstrtobool(buf, &enable);
2574 	if (err < 0)
2575 		return err;
2576 
2577 	if (enable)
2578 		client->flags |= I2C_CLIENT_PEC;
2579 	else
2580 		client->flags &= ~I2C_CLIENT_PEC;
2581 
2582 	return count;
2583 }
2584 
2585 static DEVICE_ATTR_RW(pec);
2586 
2587 static void pmbus_remove_pec(void *dev)
2588 {
2589 	device_remove_file(dev, &dev_attr_pec);
2590 }
2591 
2592 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
2593 			     struct pmbus_driver_info *info)
2594 {
2595 	struct device *dev = &client->dev;
2596 	int page, ret;
2597 
2598 	/*
2599 	 * Figure out if PEC is enabled before accessing any other register.
2600 	 * Make sure PEC is disabled, will be enabled later if needed.
2601 	 */
2602 	client->flags &= ~I2C_CLIENT_PEC;
2603 
2604 	/* Enable PEC if the controller and bus supports it */
2605 	if (!(data->flags & PMBUS_NO_CAPABILITY)) {
2606 		ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
2607 		if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK)) {
2608 			if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC))
2609 				client->flags |= I2C_CLIENT_PEC;
2610 		}
2611 	}
2612 
2613 	/*
2614 	 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
2615 	 * to use PMBUS_STATUS_BYTE instead if that is the case.
2616 	 * Bail out if both registers are not supported.
2617 	 */
2618 	data->read_status = pmbus_read_status_word;
2619 	ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD);
2620 	if (ret < 0 || ret == 0xffff) {
2621 		data->read_status = pmbus_read_status_byte;
2622 		ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE);
2623 		if (ret < 0 || ret == 0xff) {
2624 			dev_err(dev, "PMBus status register not found\n");
2625 			return -ENODEV;
2626 		}
2627 	} else {
2628 		data->has_status_word = true;
2629 	}
2630 
2631 	/*
2632 	 * Check if the chip is write protected. If it is, we can not clear
2633 	 * faults, and we should not try it. Also, in that case, writes into
2634 	 * limit registers need to be disabled.
2635 	 */
2636 	if (!(data->flags & PMBUS_NO_WRITE_PROTECT)) {
2637 		ret = i2c_smbus_read_byte_data(client, PMBUS_WRITE_PROTECT);
2638 		if (ret > 0 && (ret & PB_WP_ANY))
2639 			data->flags |= PMBUS_WRITE_PROTECTED | PMBUS_SKIP_STATUS_CHECK;
2640 	}
2641 
2642 	if (data->info->pages)
2643 		pmbus_clear_faults(client);
2644 	else
2645 		pmbus_clear_fault_page(client, -1);
2646 
2647 	if (info->identify) {
2648 		ret = (*info->identify)(client, info);
2649 		if (ret < 0) {
2650 			dev_err(dev, "Chip identification failed\n");
2651 			return ret;
2652 		}
2653 	}
2654 
2655 	if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
2656 		dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages);
2657 		return -ENODEV;
2658 	}
2659 
2660 	for (page = 0; page < info->pages; page++) {
2661 		ret = pmbus_identify_common(client, data, page);
2662 		if (ret < 0) {
2663 			dev_err(dev, "Failed to identify chip capabilities\n");
2664 			return ret;
2665 		}
2666 	}
2667 
2668 	if (data->flags & PMBUS_USE_COEFFICIENTS_CMD) {
2669 		if (!i2c_check_functionality(client->adapter,
2670 					     I2C_FUNC_SMBUS_BLOCK_PROC_CALL))
2671 			return -ENODEV;
2672 
2673 		ret = pmbus_init_coefficients(client, info);
2674 		if (ret < 0)
2675 			return ret;
2676 	}
2677 
2678 	if (client->flags & I2C_CLIENT_PEC) {
2679 		/*
2680 		 * If I2C_CLIENT_PEC is set here, both the I2C adapter and the
2681 		 * chip support PEC. Add 'pec' attribute to client device to let
2682 		 * the user control it.
2683 		 */
2684 		ret = device_create_file(dev, &dev_attr_pec);
2685 		if (ret)
2686 			return ret;
2687 		ret = devm_add_action_or_reset(dev, pmbus_remove_pec, dev);
2688 		if (ret)
2689 			return ret;
2690 	}
2691 
2692 	return 0;
2693 }
2694 
2695 #if IS_ENABLED(CONFIG_REGULATOR)
2696 static int pmbus_regulator_is_enabled(struct regulator_dev *rdev)
2697 {
2698 	struct device *dev = rdev_get_dev(rdev);
2699 	struct i2c_client *client = to_i2c_client(dev->parent);
2700 	struct pmbus_data *data = i2c_get_clientdata(client);
2701 	u8 page = rdev_get_id(rdev);
2702 	int ret;
2703 
2704 	mutex_lock(&data->update_lock);
2705 	ret = _pmbus_read_byte_data(client, page, PMBUS_OPERATION);
2706 	mutex_unlock(&data->update_lock);
2707 
2708 	if (ret < 0)
2709 		return ret;
2710 
2711 	return !!(ret & PB_OPERATION_CONTROL_ON);
2712 }
2713 
2714 static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable)
2715 {
2716 	struct device *dev = rdev_get_dev(rdev);
2717 	struct i2c_client *client = to_i2c_client(dev->parent);
2718 	struct pmbus_data *data = i2c_get_clientdata(client);
2719 	u8 page = rdev_get_id(rdev);
2720 	int ret;
2721 
2722 	mutex_lock(&data->update_lock);
2723 	ret = pmbus_update_byte_data(client, page, PMBUS_OPERATION,
2724 				     PB_OPERATION_CONTROL_ON,
2725 				     enable ? PB_OPERATION_CONTROL_ON : 0);
2726 	mutex_unlock(&data->update_lock);
2727 
2728 	return ret;
2729 }
2730 
2731 static int pmbus_regulator_enable(struct regulator_dev *rdev)
2732 {
2733 	return _pmbus_regulator_on_off(rdev, 1);
2734 }
2735 
2736 static int pmbus_regulator_disable(struct regulator_dev *rdev)
2737 {
2738 	return _pmbus_regulator_on_off(rdev, 0);
2739 }
2740 
2741 /* A PMBus status flag and the corresponding REGULATOR_ERROR_* flag */
2742 struct pmbus_regulator_status_assoc {
2743 	int pflag, rflag;
2744 };
2745 
2746 /* PMBus->regulator bit mappings for a PMBus status register */
2747 struct pmbus_regulator_status_category {
2748 	int func;
2749 	int reg;
2750 	const struct pmbus_regulator_status_assoc *bits; /* zero-terminated */
2751 };
2752 
2753 static const struct pmbus_regulator_status_category pmbus_regulator_flag_map[] = {
2754 	{
2755 		.func = PMBUS_HAVE_STATUS_VOUT,
2756 		.reg = PMBUS_STATUS_VOUT,
2757 		.bits = (const struct pmbus_regulator_status_assoc[]) {
2758 			{ PB_VOLTAGE_UV_WARNING, REGULATOR_ERROR_UNDER_VOLTAGE_WARN },
2759 			{ PB_VOLTAGE_UV_FAULT,   REGULATOR_ERROR_UNDER_VOLTAGE },
2760 			{ PB_VOLTAGE_OV_WARNING, REGULATOR_ERROR_OVER_VOLTAGE_WARN },
2761 			{ PB_VOLTAGE_OV_FAULT,   REGULATOR_ERROR_REGULATION_OUT },
2762 			{ },
2763 		},
2764 	}, {
2765 		.func = PMBUS_HAVE_STATUS_IOUT,
2766 		.reg = PMBUS_STATUS_IOUT,
2767 		.bits = (const struct pmbus_regulator_status_assoc[]) {
2768 			{ PB_IOUT_OC_WARNING,    REGULATOR_ERROR_OVER_CURRENT_WARN },
2769 			{ PB_IOUT_OC_FAULT,      REGULATOR_ERROR_OVER_CURRENT },
2770 			{ PB_IOUT_OC_LV_FAULT,   REGULATOR_ERROR_OVER_CURRENT },
2771 			{ },
2772 		},
2773 	}, {
2774 		.func = PMBUS_HAVE_STATUS_TEMP,
2775 		.reg = PMBUS_STATUS_TEMPERATURE,
2776 		.bits = (const struct pmbus_regulator_status_assoc[]) {
2777 			{ PB_TEMP_OT_WARNING,    REGULATOR_ERROR_OVER_TEMP_WARN },
2778 			{ PB_TEMP_OT_FAULT,      REGULATOR_ERROR_OVER_TEMP },
2779 			{ },
2780 		},
2781 	},
2782 };
2783 
2784 static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags)
2785 {
2786 	int i, status;
2787 	const struct pmbus_regulator_status_category *cat;
2788 	const struct pmbus_regulator_status_assoc *bit;
2789 	struct device *dev = rdev_get_dev(rdev);
2790 	struct i2c_client *client = to_i2c_client(dev->parent);
2791 	struct pmbus_data *data = i2c_get_clientdata(client);
2792 	u8 page = rdev_get_id(rdev);
2793 	int func = data->info->func[page];
2794 
2795 	*flags = 0;
2796 
2797 	mutex_lock(&data->update_lock);
2798 
2799 	for (i = 0; i < ARRAY_SIZE(pmbus_regulator_flag_map); i++) {
2800 		cat = &pmbus_regulator_flag_map[i];
2801 		if (!(func & cat->func))
2802 			continue;
2803 
2804 		status = _pmbus_read_byte_data(client, page, cat->reg);
2805 		if (status < 0) {
2806 			mutex_unlock(&data->update_lock);
2807 			return status;
2808 		}
2809 
2810 		for (bit = cat->bits; bit->pflag; bit++) {
2811 			if (status & bit->pflag)
2812 				*flags |= bit->rflag;
2813 		}
2814 	}
2815 
2816 	/*
2817 	 * Map what bits of STATUS_{WORD,BYTE} we can to REGULATOR_ERROR_*
2818 	 * bits.  Some of the other bits are tempting (especially for cases
2819 	 * where we don't have the relevant PMBUS_HAVE_STATUS_*
2820 	 * functionality), but there's an unfortunate ambiguity in that
2821 	 * they're defined as indicating a fault *or* a warning, so we can't
2822 	 * easily determine whether to report REGULATOR_ERROR_<foo> or
2823 	 * REGULATOR_ERROR_<foo>_WARN.
2824 	 */
2825 	status = pmbus_get_status(client, page, PMBUS_STATUS_WORD);
2826 	mutex_unlock(&data->update_lock);
2827 	if (status < 0)
2828 		return status;
2829 
2830 	if (pmbus_regulator_is_enabled(rdev)) {
2831 		if (status & PB_STATUS_OFF)
2832 			*flags |= REGULATOR_ERROR_FAIL;
2833 
2834 		if (status & PB_STATUS_POWER_GOOD_N)
2835 			*flags |= REGULATOR_ERROR_REGULATION_OUT;
2836 	}
2837 	/*
2838 	 * Unlike most other status bits, PB_STATUS_{IOUT_OC,VOUT_OV} are
2839 	 * defined strictly as fault indicators (not warnings).
2840 	 */
2841 	if (status & PB_STATUS_IOUT_OC)
2842 		*flags |= REGULATOR_ERROR_OVER_CURRENT;
2843 	if (status & PB_STATUS_VOUT_OV)
2844 		*flags |= REGULATOR_ERROR_REGULATION_OUT;
2845 
2846 	/*
2847 	 * If we haven't discovered any thermal faults or warnings via
2848 	 * PMBUS_STATUS_TEMPERATURE, map PB_STATUS_TEMPERATURE to a warning as
2849 	 * a (conservative) best-effort interpretation.
2850 	 */
2851 	if (!(*flags & (REGULATOR_ERROR_OVER_TEMP | REGULATOR_ERROR_OVER_TEMP_WARN)) &&
2852 	    (status & PB_STATUS_TEMPERATURE))
2853 		*flags |= REGULATOR_ERROR_OVER_TEMP_WARN;
2854 
2855 	return 0;
2856 }
2857 
2858 static int pmbus_regulator_get_status(struct regulator_dev *rdev)
2859 {
2860 	struct device *dev = rdev_get_dev(rdev);
2861 	struct i2c_client *client = to_i2c_client(dev->parent);
2862 	struct pmbus_data *data = i2c_get_clientdata(client);
2863 	u8 page = rdev_get_id(rdev);
2864 	int status, ret;
2865 
2866 	mutex_lock(&data->update_lock);
2867 	status = pmbus_get_status(client, page, PMBUS_STATUS_WORD);
2868 	if (status < 0) {
2869 		ret = status;
2870 		goto unlock;
2871 	}
2872 
2873 	if (status & PB_STATUS_OFF) {
2874 		ret = REGULATOR_STATUS_OFF;
2875 		goto unlock;
2876 	}
2877 
2878 	/* If regulator is ON & reports power good then return ON */
2879 	if (!(status & PB_STATUS_POWER_GOOD_N)) {
2880 		ret = REGULATOR_STATUS_ON;
2881 		goto unlock;
2882 	}
2883 
2884 	ret = pmbus_regulator_get_error_flags(rdev, &status);
2885 	if (ret)
2886 		goto unlock;
2887 
2888 	if (status & (REGULATOR_ERROR_UNDER_VOLTAGE | REGULATOR_ERROR_OVER_CURRENT |
2889 	   REGULATOR_ERROR_REGULATION_OUT | REGULATOR_ERROR_FAIL | REGULATOR_ERROR_OVER_TEMP)) {
2890 		ret = REGULATOR_STATUS_ERROR;
2891 		goto unlock;
2892 	}
2893 
2894 	ret = REGULATOR_STATUS_UNDEFINED;
2895 
2896 unlock:
2897 	mutex_unlock(&data->update_lock);
2898 	return ret;
2899 }
2900 
2901 static int pmbus_regulator_get_low_margin(struct i2c_client *client, int page)
2902 {
2903 	struct pmbus_data *data = i2c_get_clientdata(client);
2904 	struct pmbus_sensor s = {
2905 		.page = page,
2906 		.class = PSC_VOLTAGE_OUT,
2907 		.convert = true,
2908 		.data = -1,
2909 	};
2910 
2911 	if (data->vout_low[page] < 0) {
2912 		if (pmbus_check_word_register(client, page, PMBUS_MFR_VOUT_MIN))
2913 			s.data = _pmbus_read_word_data(client, page, 0xff,
2914 						       PMBUS_MFR_VOUT_MIN);
2915 		if (s.data < 0) {
2916 			s.data = _pmbus_read_word_data(client, page, 0xff,
2917 						       PMBUS_VOUT_MARGIN_LOW);
2918 			if (s.data < 0)
2919 				return s.data;
2920 		}
2921 		data->vout_low[page] = pmbus_reg2data(data, &s);
2922 	}
2923 
2924 	return data->vout_low[page];
2925 }
2926 
2927 static int pmbus_regulator_get_high_margin(struct i2c_client *client, int page)
2928 {
2929 	struct pmbus_data *data = i2c_get_clientdata(client);
2930 	struct pmbus_sensor s = {
2931 		.page = page,
2932 		.class = PSC_VOLTAGE_OUT,
2933 		.convert = true,
2934 		.data = -1,
2935 	};
2936 
2937 	if (data->vout_high[page] < 0) {
2938 		if (pmbus_check_word_register(client, page, PMBUS_MFR_VOUT_MAX))
2939 			s.data = _pmbus_read_word_data(client, page, 0xff,
2940 						       PMBUS_MFR_VOUT_MAX);
2941 		if (s.data < 0) {
2942 			s.data = _pmbus_read_word_data(client, page, 0xff,
2943 						       PMBUS_VOUT_MARGIN_HIGH);
2944 			if (s.data < 0)
2945 				return s.data;
2946 		}
2947 		data->vout_high[page] = pmbus_reg2data(data, &s);
2948 	}
2949 
2950 	return data->vout_high[page];
2951 }
2952 
2953 static int pmbus_regulator_get_voltage(struct regulator_dev *rdev)
2954 {
2955 	struct device *dev = rdev_get_dev(rdev);
2956 	struct i2c_client *client = to_i2c_client(dev->parent);
2957 	struct pmbus_data *data = i2c_get_clientdata(client);
2958 	struct pmbus_sensor s = {
2959 		.page = rdev_get_id(rdev),
2960 		.class = PSC_VOLTAGE_OUT,
2961 		.convert = true,
2962 	};
2963 
2964 	s.data = _pmbus_read_word_data(client, s.page, 0xff, PMBUS_READ_VOUT);
2965 	if (s.data < 0)
2966 		return s.data;
2967 
2968 	return (int)pmbus_reg2data(data, &s) * 1000; /* unit is uV */
2969 }
2970 
2971 static int pmbus_regulator_set_voltage(struct regulator_dev *rdev, int min_uv,
2972 				       int max_uv, unsigned int *selector)
2973 {
2974 	struct device *dev = rdev_get_dev(rdev);
2975 	struct i2c_client *client = to_i2c_client(dev->parent);
2976 	struct pmbus_data *data = i2c_get_clientdata(client);
2977 	struct pmbus_sensor s = {
2978 		.page = rdev_get_id(rdev),
2979 		.class = PSC_VOLTAGE_OUT,
2980 		.convert = true,
2981 		.data = -1,
2982 	};
2983 	int val = DIV_ROUND_CLOSEST(min_uv, 1000); /* convert to mV */
2984 	int low, high;
2985 
2986 	*selector = 0;
2987 
2988 	low = pmbus_regulator_get_low_margin(client, s.page);
2989 	if (low < 0)
2990 		return low;
2991 
2992 	high = pmbus_regulator_get_high_margin(client, s.page);
2993 	if (high < 0)
2994 		return high;
2995 
2996 	/* Make sure we are within margins */
2997 	if (low > val)
2998 		val = low;
2999 	if (high < val)
3000 		val = high;
3001 
3002 	val = pmbus_data2reg(data, &s, val);
3003 
3004 	return _pmbus_write_word_data(client, s.page, PMBUS_VOUT_COMMAND, (u16)val);
3005 }
3006 
3007 static int pmbus_regulator_list_voltage(struct regulator_dev *rdev,
3008 					 unsigned int selector)
3009 {
3010 	struct device *dev = rdev_get_dev(rdev);
3011 	struct i2c_client *client = to_i2c_client(dev->parent);
3012 	int val, low, high;
3013 
3014 	if (selector >= rdev->desc->n_voltages ||
3015 	    selector < rdev->desc->linear_min_sel)
3016 		return -EINVAL;
3017 
3018 	selector -= rdev->desc->linear_min_sel;
3019 	val = DIV_ROUND_CLOSEST(rdev->desc->min_uV +
3020 				(rdev->desc->uV_step * selector), 1000); /* convert to mV */
3021 
3022 	low = pmbus_regulator_get_low_margin(client, rdev_get_id(rdev));
3023 	if (low < 0)
3024 		return low;
3025 
3026 	high = pmbus_regulator_get_high_margin(client, rdev_get_id(rdev));
3027 	if (high < 0)
3028 		return high;
3029 
3030 	if (val >= low && val <= high)
3031 		return val * 1000; /* unit is uV */
3032 
3033 	return 0;
3034 }
3035 
3036 const struct regulator_ops pmbus_regulator_ops = {
3037 	.enable = pmbus_regulator_enable,
3038 	.disable = pmbus_regulator_disable,
3039 	.is_enabled = pmbus_regulator_is_enabled,
3040 	.get_error_flags = pmbus_regulator_get_error_flags,
3041 	.get_status = pmbus_regulator_get_status,
3042 	.get_voltage = pmbus_regulator_get_voltage,
3043 	.set_voltage = pmbus_regulator_set_voltage,
3044 	.list_voltage = pmbus_regulator_list_voltage,
3045 };
3046 EXPORT_SYMBOL_NS_GPL(pmbus_regulator_ops, PMBUS);
3047 
3048 static int pmbus_regulator_register(struct pmbus_data *data)
3049 {
3050 	struct device *dev = data->dev;
3051 	const struct pmbus_driver_info *info = data->info;
3052 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
3053 	struct regulator_dev *rdev;
3054 	int i;
3055 
3056 	for (i = 0; i < info->num_regulators; i++) {
3057 		struct regulator_config config = { };
3058 
3059 		config.dev = dev;
3060 		config.driver_data = data;
3061 
3062 		if (pdata && pdata->reg_init_data)
3063 			config.init_data = &pdata->reg_init_data[i];
3064 
3065 		rdev = devm_regulator_register(dev, &info->reg_desc[i],
3066 					       &config);
3067 		if (IS_ERR(rdev))
3068 			return dev_err_probe(dev, PTR_ERR(rdev),
3069 					     "Failed to register %s regulator\n",
3070 					     info->reg_desc[i].name);
3071 	}
3072 
3073 	return 0;
3074 }
3075 #else
3076 static int pmbus_regulator_register(struct pmbus_data *data)
3077 {
3078 	return 0;
3079 }
3080 #endif
3081 
3082 static struct dentry *pmbus_debugfs_dir;	/* pmbus debugfs directory */
3083 
3084 #if IS_ENABLED(CONFIG_DEBUG_FS)
3085 static int pmbus_debugfs_get(void *data, u64 *val)
3086 {
3087 	int rc;
3088 	struct pmbus_debugfs_entry *entry = data;
3089 
3090 	rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg);
3091 	if (rc < 0)
3092 		return rc;
3093 
3094 	*val = rc;
3095 
3096 	return 0;
3097 }
3098 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL,
3099 			 "0x%02llx\n");
3100 
3101 static int pmbus_debugfs_get_status(void *data, u64 *val)
3102 {
3103 	int rc;
3104 	struct pmbus_debugfs_entry *entry = data;
3105 	struct pmbus_data *pdata = i2c_get_clientdata(entry->client);
3106 
3107 	rc = pdata->read_status(entry->client, entry->page);
3108 	if (rc < 0)
3109 		return rc;
3110 
3111 	*val = rc;
3112 
3113 	return 0;
3114 }
3115 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status,
3116 			 NULL, "0x%04llx\n");
3117 
3118 static ssize_t pmbus_debugfs_mfr_read(struct file *file, char __user *buf,
3119 				       size_t count, loff_t *ppos)
3120 {
3121 	int rc;
3122 	struct pmbus_debugfs_entry *entry = file->private_data;
3123 	char data[I2C_SMBUS_BLOCK_MAX + 2] = { 0 };
3124 
3125 	rc = pmbus_read_block_data(entry->client, entry->page, entry->reg,
3126 				   data);
3127 	if (rc < 0)
3128 		return rc;
3129 
3130 	/* Add newline at the end of a read data */
3131 	data[rc] = '\n';
3132 
3133 	/* Include newline into the length */
3134 	rc += 1;
3135 
3136 	return simple_read_from_buffer(buf, count, ppos, data, rc);
3137 }
3138 
3139 static const struct file_operations pmbus_debugfs_ops_mfr = {
3140 	.llseek = noop_llseek,
3141 	.read = pmbus_debugfs_mfr_read,
3142 	.write = NULL,
3143 	.open = simple_open,
3144 };
3145 
3146 static void pmbus_remove_debugfs(void *data)
3147 {
3148 	struct dentry *entry = data;
3149 
3150 	debugfs_remove_recursive(entry);
3151 }
3152 
3153 static int pmbus_init_debugfs(struct i2c_client *client,
3154 			      struct pmbus_data *data)
3155 {
3156 	int i, idx = 0;
3157 	char name[PMBUS_NAME_SIZE];
3158 	struct pmbus_debugfs_entry *entries;
3159 
3160 	if (!pmbus_debugfs_dir)
3161 		return -ENODEV;
3162 
3163 	/*
3164 	 * Create the debugfs directory for this device. Use the hwmon device
3165 	 * name to avoid conflicts (hwmon numbers are globally unique).
3166 	 */
3167 	data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev),
3168 					   pmbus_debugfs_dir);
3169 	if (IS_ERR_OR_NULL(data->debugfs)) {
3170 		data->debugfs = NULL;
3171 		return -ENODEV;
3172 	}
3173 
3174 	/*
3175 	 * Allocate the max possible entries we need.
3176 	 * 6 entries device-specific
3177 	 * 10 entries page-specific
3178 	 */
3179 	entries = devm_kcalloc(data->dev,
3180 			       6 + data->info->pages * 10, sizeof(*entries),
3181 			       GFP_KERNEL);
3182 	if (!entries)
3183 		return -ENOMEM;
3184 
3185 	/*
3186 	 * Add device-specific entries.
3187 	 * Please note that the PMBUS standard allows all registers to be
3188 	 * page-specific.
3189 	 * To reduce the number of debugfs entries for devices with many pages
3190 	 * assume that values of the following registers are the same for all
3191 	 * pages and report values only for page 0.
3192 	 */
3193 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_ID)) {
3194 		entries[idx].client = client;
3195 		entries[idx].page = 0;
3196 		entries[idx].reg = PMBUS_MFR_ID;
3197 		debugfs_create_file("mfr_id", 0444, data->debugfs,
3198 				    &entries[idx++],
3199 				    &pmbus_debugfs_ops_mfr);
3200 	}
3201 
3202 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_MODEL)) {
3203 		entries[idx].client = client;
3204 		entries[idx].page = 0;
3205 		entries[idx].reg = PMBUS_MFR_MODEL;
3206 		debugfs_create_file("mfr_model", 0444, data->debugfs,
3207 				    &entries[idx++],
3208 				    &pmbus_debugfs_ops_mfr);
3209 	}
3210 
3211 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_REVISION)) {
3212 		entries[idx].client = client;
3213 		entries[idx].page = 0;
3214 		entries[idx].reg = PMBUS_MFR_REVISION;
3215 		debugfs_create_file("mfr_revision", 0444, data->debugfs,
3216 				    &entries[idx++],
3217 				    &pmbus_debugfs_ops_mfr);
3218 	}
3219 
3220 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_LOCATION)) {
3221 		entries[idx].client = client;
3222 		entries[idx].page = 0;
3223 		entries[idx].reg = PMBUS_MFR_LOCATION;
3224 		debugfs_create_file("mfr_location", 0444, data->debugfs,
3225 				    &entries[idx++],
3226 				    &pmbus_debugfs_ops_mfr);
3227 	}
3228 
3229 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_DATE)) {
3230 		entries[idx].client = client;
3231 		entries[idx].page = 0;
3232 		entries[idx].reg = PMBUS_MFR_DATE;
3233 		debugfs_create_file("mfr_date", 0444, data->debugfs,
3234 				    &entries[idx++],
3235 				    &pmbus_debugfs_ops_mfr);
3236 	}
3237 
3238 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_SERIAL)) {
3239 		entries[idx].client = client;
3240 		entries[idx].page = 0;
3241 		entries[idx].reg = PMBUS_MFR_SERIAL;
3242 		debugfs_create_file("mfr_serial", 0444, data->debugfs,
3243 				    &entries[idx++],
3244 				    &pmbus_debugfs_ops_mfr);
3245 	}
3246 
3247 	/* Add page specific entries */
3248 	for (i = 0; i < data->info->pages; ++i) {
3249 		/* Check accessibility of status register if it's not page 0 */
3250 		if (!i || pmbus_check_status_register(client, i)) {
3251 			/* No need to set reg as we have special read op. */
3252 			entries[idx].client = client;
3253 			entries[idx].page = i;
3254 			scnprintf(name, PMBUS_NAME_SIZE, "status%d", i);
3255 			debugfs_create_file(name, 0444, data->debugfs,
3256 					    &entries[idx++],
3257 					    &pmbus_debugfs_ops_status);
3258 		}
3259 
3260 		if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) {
3261 			entries[idx].client = client;
3262 			entries[idx].page = i;
3263 			entries[idx].reg = PMBUS_STATUS_VOUT;
3264 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i);
3265 			debugfs_create_file(name, 0444, data->debugfs,
3266 					    &entries[idx++],
3267 					    &pmbus_debugfs_ops);
3268 		}
3269 
3270 		if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) {
3271 			entries[idx].client = client;
3272 			entries[idx].page = i;
3273 			entries[idx].reg = PMBUS_STATUS_IOUT;
3274 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i);
3275 			debugfs_create_file(name, 0444, data->debugfs,
3276 					    &entries[idx++],
3277 					    &pmbus_debugfs_ops);
3278 		}
3279 
3280 		if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) {
3281 			entries[idx].client = client;
3282 			entries[idx].page = i;
3283 			entries[idx].reg = PMBUS_STATUS_INPUT;
3284 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i);
3285 			debugfs_create_file(name, 0444, data->debugfs,
3286 					    &entries[idx++],
3287 					    &pmbus_debugfs_ops);
3288 		}
3289 
3290 		if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) {
3291 			entries[idx].client = client;
3292 			entries[idx].page = i;
3293 			entries[idx].reg = PMBUS_STATUS_TEMPERATURE;
3294 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i);
3295 			debugfs_create_file(name, 0444, data->debugfs,
3296 					    &entries[idx++],
3297 					    &pmbus_debugfs_ops);
3298 		}
3299 
3300 		if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) {
3301 			entries[idx].client = client;
3302 			entries[idx].page = i;
3303 			entries[idx].reg = PMBUS_STATUS_CML;
3304 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i);
3305 			debugfs_create_file(name, 0444, data->debugfs,
3306 					    &entries[idx++],
3307 					    &pmbus_debugfs_ops);
3308 		}
3309 
3310 		if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) {
3311 			entries[idx].client = client;
3312 			entries[idx].page = i;
3313 			entries[idx].reg = PMBUS_STATUS_OTHER;
3314 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i);
3315 			debugfs_create_file(name, 0444, data->debugfs,
3316 					    &entries[idx++],
3317 					    &pmbus_debugfs_ops);
3318 		}
3319 
3320 		if (pmbus_check_byte_register(client, i,
3321 					      PMBUS_STATUS_MFR_SPECIFIC)) {
3322 			entries[idx].client = client;
3323 			entries[idx].page = i;
3324 			entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC;
3325 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i);
3326 			debugfs_create_file(name, 0444, data->debugfs,
3327 					    &entries[idx++],
3328 					    &pmbus_debugfs_ops);
3329 		}
3330 
3331 		if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) {
3332 			entries[idx].client = client;
3333 			entries[idx].page = i;
3334 			entries[idx].reg = PMBUS_STATUS_FAN_12;
3335 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i);
3336 			debugfs_create_file(name, 0444, data->debugfs,
3337 					    &entries[idx++],
3338 					    &pmbus_debugfs_ops);
3339 		}
3340 
3341 		if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) {
3342 			entries[idx].client = client;
3343 			entries[idx].page = i;
3344 			entries[idx].reg = PMBUS_STATUS_FAN_34;
3345 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i);
3346 			debugfs_create_file(name, 0444, data->debugfs,
3347 					    &entries[idx++],
3348 					    &pmbus_debugfs_ops);
3349 		}
3350 	}
3351 
3352 	return devm_add_action_or_reset(data->dev,
3353 					pmbus_remove_debugfs, data->debugfs);
3354 }
3355 #else
3356 static int pmbus_init_debugfs(struct i2c_client *client,
3357 			      struct pmbus_data *data)
3358 {
3359 	return 0;
3360 }
3361 #endif	/* IS_ENABLED(CONFIG_DEBUG_FS) */
3362 
3363 int pmbus_do_probe(struct i2c_client *client, struct pmbus_driver_info *info)
3364 {
3365 	struct device *dev = &client->dev;
3366 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
3367 	struct pmbus_data *data;
3368 	size_t groups_num = 0;
3369 	int ret;
3370 	int i;
3371 	char *name;
3372 
3373 	if (!info)
3374 		return -ENODEV;
3375 
3376 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
3377 				     | I2C_FUNC_SMBUS_BYTE_DATA
3378 				     | I2C_FUNC_SMBUS_WORD_DATA))
3379 		return -ENODEV;
3380 
3381 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
3382 	if (!data)
3383 		return -ENOMEM;
3384 
3385 	if (info->groups)
3386 		while (info->groups[groups_num])
3387 			groups_num++;
3388 
3389 	data->groups = devm_kcalloc(dev, groups_num + 2, sizeof(void *),
3390 				    GFP_KERNEL);
3391 	if (!data->groups)
3392 		return -ENOMEM;
3393 
3394 	i2c_set_clientdata(client, data);
3395 	mutex_init(&data->update_lock);
3396 	data->dev = dev;
3397 
3398 	if (pdata)
3399 		data->flags = pdata->flags;
3400 	data->info = info;
3401 	data->currpage = -1;
3402 	data->currphase = -1;
3403 
3404 	for (i = 0; i < ARRAY_SIZE(data->vout_low); i++) {
3405 		data->vout_low[i] = -1;
3406 		data->vout_high[i] = -1;
3407 	}
3408 
3409 	ret = pmbus_init_common(client, data, info);
3410 	if (ret < 0)
3411 		return ret;
3412 
3413 	ret = pmbus_find_attributes(client, data);
3414 	if (ret)
3415 		return ret;
3416 
3417 	/*
3418 	 * If there are no attributes, something is wrong.
3419 	 * Bail out instead of trying to register nothing.
3420 	 */
3421 	if (!data->num_attributes) {
3422 		dev_err(dev, "No attributes found\n");
3423 		return -ENODEV;
3424 	}
3425 
3426 	name = devm_kstrdup(dev, client->name, GFP_KERNEL);
3427 	if (!name)
3428 		return -ENOMEM;
3429 	strreplace(name, '-', '_');
3430 
3431 	data->groups[0] = &data->group;
3432 	memcpy(data->groups + 1, info->groups, sizeof(void *) * groups_num);
3433 	data->hwmon_dev = devm_hwmon_device_register_with_groups(dev,
3434 					name, data, data->groups);
3435 	if (IS_ERR(data->hwmon_dev)) {
3436 		dev_err(dev, "Failed to register hwmon device\n");
3437 		return PTR_ERR(data->hwmon_dev);
3438 	}
3439 
3440 	ret = pmbus_regulator_register(data);
3441 	if (ret)
3442 		return ret;
3443 
3444 	ret = pmbus_init_debugfs(client, data);
3445 	if (ret)
3446 		dev_warn(dev, "Failed to register debugfs\n");
3447 
3448 	return 0;
3449 }
3450 EXPORT_SYMBOL_NS_GPL(pmbus_do_probe, PMBUS);
3451 
3452 struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client)
3453 {
3454 	struct pmbus_data *data = i2c_get_clientdata(client);
3455 
3456 	return data->debugfs;
3457 }
3458 EXPORT_SYMBOL_NS_GPL(pmbus_get_debugfs_dir, PMBUS);
3459 
3460 static int __init pmbus_core_init(void)
3461 {
3462 	pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL);
3463 	if (IS_ERR(pmbus_debugfs_dir))
3464 		pmbus_debugfs_dir = NULL;
3465 
3466 	return 0;
3467 }
3468 
3469 static void __exit pmbus_core_exit(void)
3470 {
3471 	debugfs_remove_recursive(pmbus_debugfs_dir);
3472 }
3473 
3474 module_init(pmbus_core_init);
3475 module_exit(pmbus_core_exit);
3476 
3477 MODULE_AUTHOR("Guenter Roeck");
3478 MODULE_DESCRIPTION("PMBus core driver");
3479 MODULE_LICENSE("GPL");
3480