xref: /openbmc/linux/drivers/hwmon/pmbus/pmbus_core.c (revision f59a3ee6)
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) && (status & PB_STATUS_OFF))
2831 		*flags |= REGULATOR_ERROR_FAIL;
2832 
2833 	/*
2834 	 * Unlike most other status bits, PB_STATUS_{IOUT_OC,VOUT_OV} are
2835 	 * defined strictly as fault indicators (not warnings).
2836 	 */
2837 	if (status & PB_STATUS_IOUT_OC)
2838 		*flags |= REGULATOR_ERROR_OVER_CURRENT;
2839 	if (status & PB_STATUS_VOUT_OV)
2840 		*flags |= REGULATOR_ERROR_REGULATION_OUT;
2841 
2842 	/*
2843 	 * If we haven't discovered any thermal faults or warnings via
2844 	 * PMBUS_STATUS_TEMPERATURE, map PB_STATUS_TEMPERATURE to a warning as
2845 	 * a (conservative) best-effort interpretation.
2846 	 */
2847 	if (!(*flags & (REGULATOR_ERROR_OVER_TEMP | REGULATOR_ERROR_OVER_TEMP_WARN)) &&
2848 	    (status & PB_STATUS_TEMPERATURE))
2849 		*flags |= REGULATOR_ERROR_OVER_TEMP_WARN;
2850 
2851 	return 0;
2852 }
2853 
2854 static int pmbus_regulator_get_low_margin(struct i2c_client *client, int page)
2855 {
2856 	struct pmbus_data *data = i2c_get_clientdata(client);
2857 	struct pmbus_sensor s = {
2858 		.page = page,
2859 		.class = PSC_VOLTAGE_OUT,
2860 		.convert = true,
2861 		.data = -1,
2862 	};
2863 
2864 	if (data->vout_low[page] < 0) {
2865 		if (pmbus_check_word_register(client, page, PMBUS_MFR_VOUT_MIN))
2866 			s.data = _pmbus_read_word_data(client, page, 0xff,
2867 						       PMBUS_MFR_VOUT_MIN);
2868 		if (s.data < 0) {
2869 			s.data = _pmbus_read_word_data(client, page, 0xff,
2870 						       PMBUS_VOUT_MARGIN_LOW);
2871 			if (s.data < 0)
2872 				return s.data;
2873 		}
2874 		data->vout_low[page] = pmbus_reg2data(data, &s);
2875 	}
2876 
2877 	return data->vout_low[page];
2878 }
2879 
2880 static int pmbus_regulator_get_high_margin(struct i2c_client *client, int page)
2881 {
2882 	struct pmbus_data *data = i2c_get_clientdata(client);
2883 	struct pmbus_sensor s = {
2884 		.page = page,
2885 		.class = PSC_VOLTAGE_OUT,
2886 		.convert = true,
2887 		.data = -1,
2888 	};
2889 
2890 	if (data->vout_high[page] < 0) {
2891 		if (pmbus_check_word_register(client, page, PMBUS_MFR_VOUT_MAX))
2892 			s.data = _pmbus_read_word_data(client, page, 0xff,
2893 						       PMBUS_MFR_VOUT_MAX);
2894 		if (s.data < 0) {
2895 			s.data = _pmbus_read_word_data(client, page, 0xff,
2896 						       PMBUS_VOUT_MARGIN_HIGH);
2897 			if (s.data < 0)
2898 				return s.data;
2899 		}
2900 		data->vout_high[page] = pmbus_reg2data(data, &s);
2901 	}
2902 
2903 	return data->vout_high[page];
2904 }
2905 
2906 static int pmbus_regulator_get_voltage(struct regulator_dev *rdev)
2907 {
2908 	struct device *dev = rdev_get_dev(rdev);
2909 	struct i2c_client *client = to_i2c_client(dev->parent);
2910 	struct pmbus_data *data = i2c_get_clientdata(client);
2911 	struct pmbus_sensor s = {
2912 		.page = rdev_get_id(rdev),
2913 		.class = PSC_VOLTAGE_OUT,
2914 		.convert = true,
2915 	};
2916 
2917 	s.data = _pmbus_read_word_data(client, s.page, 0xff, PMBUS_READ_VOUT);
2918 	if (s.data < 0)
2919 		return s.data;
2920 
2921 	return (int)pmbus_reg2data(data, &s) * 1000; /* unit is uV */
2922 }
2923 
2924 static int pmbus_regulator_set_voltage(struct regulator_dev *rdev, int min_uv,
2925 				       int max_uv, unsigned int *selector)
2926 {
2927 	struct device *dev = rdev_get_dev(rdev);
2928 	struct i2c_client *client = to_i2c_client(dev->parent);
2929 	struct pmbus_data *data = i2c_get_clientdata(client);
2930 	struct pmbus_sensor s = {
2931 		.page = rdev_get_id(rdev),
2932 		.class = PSC_VOLTAGE_OUT,
2933 		.convert = true,
2934 		.data = -1,
2935 	};
2936 	int val = DIV_ROUND_CLOSEST(min_uv, 1000); /* convert to mV */
2937 	int low, high;
2938 
2939 	*selector = 0;
2940 
2941 	low = pmbus_regulator_get_low_margin(client, s.page);
2942 	if (low < 0)
2943 		return low;
2944 
2945 	high = pmbus_regulator_get_high_margin(client, s.page);
2946 	if (high < 0)
2947 		return high;
2948 
2949 	/* Make sure we are within margins */
2950 	if (low > val)
2951 		val = low;
2952 	if (high < val)
2953 		val = high;
2954 
2955 	val = pmbus_data2reg(data, &s, val);
2956 
2957 	return _pmbus_write_word_data(client, s.page, PMBUS_VOUT_COMMAND, (u16)val);
2958 }
2959 
2960 static int pmbus_regulator_list_voltage(struct regulator_dev *rdev,
2961 					 unsigned int selector)
2962 {
2963 	struct device *dev = rdev_get_dev(rdev);
2964 	struct i2c_client *client = to_i2c_client(dev->parent);
2965 	int val, low, high;
2966 
2967 	if (selector >= rdev->desc->n_voltages ||
2968 	    selector < rdev->desc->linear_min_sel)
2969 		return -EINVAL;
2970 
2971 	selector -= rdev->desc->linear_min_sel;
2972 	val = DIV_ROUND_CLOSEST(rdev->desc->min_uV +
2973 				(rdev->desc->uV_step * selector), 1000); /* convert to mV */
2974 
2975 	low = pmbus_regulator_get_low_margin(client, rdev_get_id(rdev));
2976 	if (low < 0)
2977 		return low;
2978 
2979 	high = pmbus_regulator_get_high_margin(client, rdev_get_id(rdev));
2980 	if (high < 0)
2981 		return high;
2982 
2983 	if (val >= low && val <= high)
2984 		return val * 1000; /* unit is uV */
2985 
2986 	return 0;
2987 }
2988 
2989 const struct regulator_ops pmbus_regulator_ops = {
2990 	.enable = pmbus_regulator_enable,
2991 	.disable = pmbus_regulator_disable,
2992 	.is_enabled = pmbus_regulator_is_enabled,
2993 	.get_error_flags = pmbus_regulator_get_error_flags,
2994 	.get_voltage = pmbus_regulator_get_voltage,
2995 	.set_voltage = pmbus_regulator_set_voltage,
2996 	.list_voltage = pmbus_regulator_list_voltage,
2997 };
2998 EXPORT_SYMBOL_NS_GPL(pmbus_regulator_ops, PMBUS);
2999 
3000 static int pmbus_regulator_register(struct pmbus_data *data)
3001 {
3002 	struct device *dev = data->dev;
3003 	const struct pmbus_driver_info *info = data->info;
3004 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
3005 	struct regulator_dev *rdev;
3006 	int i;
3007 
3008 	for (i = 0; i < info->num_regulators; i++) {
3009 		struct regulator_config config = { };
3010 
3011 		config.dev = dev;
3012 		config.driver_data = data;
3013 
3014 		if (pdata && pdata->reg_init_data)
3015 			config.init_data = &pdata->reg_init_data[i];
3016 
3017 		rdev = devm_regulator_register(dev, &info->reg_desc[i],
3018 					       &config);
3019 		if (IS_ERR(rdev))
3020 			return dev_err_probe(dev, PTR_ERR(rdev),
3021 					     "Failed to register %s regulator\n",
3022 					     info->reg_desc[i].name);
3023 	}
3024 
3025 	return 0;
3026 }
3027 #else
3028 static int pmbus_regulator_register(struct pmbus_data *data)
3029 {
3030 	return 0;
3031 }
3032 #endif
3033 
3034 static struct dentry *pmbus_debugfs_dir;	/* pmbus debugfs directory */
3035 
3036 #if IS_ENABLED(CONFIG_DEBUG_FS)
3037 static int pmbus_debugfs_get(void *data, u64 *val)
3038 {
3039 	int rc;
3040 	struct pmbus_debugfs_entry *entry = data;
3041 
3042 	rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg);
3043 	if (rc < 0)
3044 		return rc;
3045 
3046 	*val = rc;
3047 
3048 	return 0;
3049 }
3050 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL,
3051 			 "0x%02llx\n");
3052 
3053 static int pmbus_debugfs_get_status(void *data, u64 *val)
3054 {
3055 	int rc;
3056 	struct pmbus_debugfs_entry *entry = data;
3057 	struct pmbus_data *pdata = i2c_get_clientdata(entry->client);
3058 
3059 	rc = pdata->read_status(entry->client, entry->page);
3060 	if (rc < 0)
3061 		return rc;
3062 
3063 	*val = rc;
3064 
3065 	return 0;
3066 }
3067 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status,
3068 			 NULL, "0x%04llx\n");
3069 
3070 static ssize_t pmbus_debugfs_mfr_read(struct file *file, char __user *buf,
3071 				       size_t count, loff_t *ppos)
3072 {
3073 	int rc;
3074 	struct pmbus_debugfs_entry *entry = file->private_data;
3075 	char data[I2C_SMBUS_BLOCK_MAX + 2] = { 0 };
3076 
3077 	rc = pmbus_read_block_data(entry->client, entry->page, entry->reg,
3078 				   data);
3079 	if (rc < 0)
3080 		return rc;
3081 
3082 	/* Add newline at the end of a read data */
3083 	data[rc] = '\n';
3084 
3085 	/* Include newline into the length */
3086 	rc += 1;
3087 
3088 	return simple_read_from_buffer(buf, count, ppos, data, rc);
3089 }
3090 
3091 static const struct file_operations pmbus_debugfs_ops_mfr = {
3092 	.llseek = noop_llseek,
3093 	.read = pmbus_debugfs_mfr_read,
3094 	.write = NULL,
3095 	.open = simple_open,
3096 };
3097 
3098 static void pmbus_remove_debugfs(void *data)
3099 {
3100 	struct dentry *entry = data;
3101 
3102 	debugfs_remove_recursive(entry);
3103 }
3104 
3105 static int pmbus_init_debugfs(struct i2c_client *client,
3106 			      struct pmbus_data *data)
3107 {
3108 	int i, idx = 0;
3109 	char name[PMBUS_NAME_SIZE];
3110 	struct pmbus_debugfs_entry *entries;
3111 
3112 	if (!pmbus_debugfs_dir)
3113 		return -ENODEV;
3114 
3115 	/*
3116 	 * Create the debugfs directory for this device. Use the hwmon device
3117 	 * name to avoid conflicts (hwmon numbers are globally unique).
3118 	 */
3119 	data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev),
3120 					   pmbus_debugfs_dir);
3121 	if (IS_ERR_OR_NULL(data->debugfs)) {
3122 		data->debugfs = NULL;
3123 		return -ENODEV;
3124 	}
3125 
3126 	/*
3127 	 * Allocate the max possible entries we need.
3128 	 * 6 entries device-specific
3129 	 * 10 entries page-specific
3130 	 */
3131 	entries = devm_kcalloc(data->dev,
3132 			       6 + data->info->pages * 10, sizeof(*entries),
3133 			       GFP_KERNEL);
3134 	if (!entries)
3135 		return -ENOMEM;
3136 
3137 	/*
3138 	 * Add device-specific entries.
3139 	 * Please note that the PMBUS standard allows all registers to be
3140 	 * page-specific.
3141 	 * To reduce the number of debugfs entries for devices with many pages
3142 	 * assume that values of the following registers are the same for all
3143 	 * pages and report values only for page 0.
3144 	 */
3145 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_ID)) {
3146 		entries[idx].client = client;
3147 		entries[idx].page = 0;
3148 		entries[idx].reg = PMBUS_MFR_ID;
3149 		debugfs_create_file("mfr_id", 0444, data->debugfs,
3150 				    &entries[idx++],
3151 				    &pmbus_debugfs_ops_mfr);
3152 	}
3153 
3154 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_MODEL)) {
3155 		entries[idx].client = client;
3156 		entries[idx].page = 0;
3157 		entries[idx].reg = PMBUS_MFR_MODEL;
3158 		debugfs_create_file("mfr_model", 0444, data->debugfs,
3159 				    &entries[idx++],
3160 				    &pmbus_debugfs_ops_mfr);
3161 	}
3162 
3163 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_REVISION)) {
3164 		entries[idx].client = client;
3165 		entries[idx].page = 0;
3166 		entries[idx].reg = PMBUS_MFR_REVISION;
3167 		debugfs_create_file("mfr_revision", 0444, data->debugfs,
3168 				    &entries[idx++],
3169 				    &pmbus_debugfs_ops_mfr);
3170 	}
3171 
3172 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_LOCATION)) {
3173 		entries[idx].client = client;
3174 		entries[idx].page = 0;
3175 		entries[idx].reg = PMBUS_MFR_LOCATION;
3176 		debugfs_create_file("mfr_location", 0444, data->debugfs,
3177 				    &entries[idx++],
3178 				    &pmbus_debugfs_ops_mfr);
3179 	}
3180 
3181 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_DATE)) {
3182 		entries[idx].client = client;
3183 		entries[idx].page = 0;
3184 		entries[idx].reg = PMBUS_MFR_DATE;
3185 		debugfs_create_file("mfr_date", 0444, data->debugfs,
3186 				    &entries[idx++],
3187 				    &pmbus_debugfs_ops_mfr);
3188 	}
3189 
3190 	if (pmbus_check_block_register(client, 0, PMBUS_MFR_SERIAL)) {
3191 		entries[idx].client = client;
3192 		entries[idx].page = 0;
3193 		entries[idx].reg = PMBUS_MFR_SERIAL;
3194 		debugfs_create_file("mfr_serial", 0444, data->debugfs,
3195 				    &entries[idx++],
3196 				    &pmbus_debugfs_ops_mfr);
3197 	}
3198 
3199 	/* Add page specific entries */
3200 	for (i = 0; i < data->info->pages; ++i) {
3201 		/* Check accessibility of status register if it's not page 0 */
3202 		if (!i || pmbus_check_status_register(client, i)) {
3203 			/* No need to set reg as we have special read op. */
3204 			entries[idx].client = client;
3205 			entries[idx].page = i;
3206 			scnprintf(name, PMBUS_NAME_SIZE, "status%d", i);
3207 			debugfs_create_file(name, 0444, data->debugfs,
3208 					    &entries[idx++],
3209 					    &pmbus_debugfs_ops_status);
3210 		}
3211 
3212 		if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) {
3213 			entries[idx].client = client;
3214 			entries[idx].page = i;
3215 			entries[idx].reg = PMBUS_STATUS_VOUT;
3216 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i);
3217 			debugfs_create_file(name, 0444, data->debugfs,
3218 					    &entries[idx++],
3219 					    &pmbus_debugfs_ops);
3220 		}
3221 
3222 		if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) {
3223 			entries[idx].client = client;
3224 			entries[idx].page = i;
3225 			entries[idx].reg = PMBUS_STATUS_IOUT;
3226 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i);
3227 			debugfs_create_file(name, 0444, data->debugfs,
3228 					    &entries[idx++],
3229 					    &pmbus_debugfs_ops);
3230 		}
3231 
3232 		if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) {
3233 			entries[idx].client = client;
3234 			entries[idx].page = i;
3235 			entries[idx].reg = PMBUS_STATUS_INPUT;
3236 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i);
3237 			debugfs_create_file(name, 0444, data->debugfs,
3238 					    &entries[idx++],
3239 					    &pmbus_debugfs_ops);
3240 		}
3241 
3242 		if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) {
3243 			entries[idx].client = client;
3244 			entries[idx].page = i;
3245 			entries[idx].reg = PMBUS_STATUS_TEMPERATURE;
3246 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i);
3247 			debugfs_create_file(name, 0444, data->debugfs,
3248 					    &entries[idx++],
3249 					    &pmbus_debugfs_ops);
3250 		}
3251 
3252 		if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) {
3253 			entries[idx].client = client;
3254 			entries[idx].page = i;
3255 			entries[idx].reg = PMBUS_STATUS_CML;
3256 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i);
3257 			debugfs_create_file(name, 0444, data->debugfs,
3258 					    &entries[idx++],
3259 					    &pmbus_debugfs_ops);
3260 		}
3261 
3262 		if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) {
3263 			entries[idx].client = client;
3264 			entries[idx].page = i;
3265 			entries[idx].reg = PMBUS_STATUS_OTHER;
3266 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i);
3267 			debugfs_create_file(name, 0444, data->debugfs,
3268 					    &entries[idx++],
3269 					    &pmbus_debugfs_ops);
3270 		}
3271 
3272 		if (pmbus_check_byte_register(client, i,
3273 					      PMBUS_STATUS_MFR_SPECIFIC)) {
3274 			entries[idx].client = client;
3275 			entries[idx].page = i;
3276 			entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC;
3277 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i);
3278 			debugfs_create_file(name, 0444, data->debugfs,
3279 					    &entries[idx++],
3280 					    &pmbus_debugfs_ops);
3281 		}
3282 
3283 		if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) {
3284 			entries[idx].client = client;
3285 			entries[idx].page = i;
3286 			entries[idx].reg = PMBUS_STATUS_FAN_12;
3287 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i);
3288 			debugfs_create_file(name, 0444, data->debugfs,
3289 					    &entries[idx++],
3290 					    &pmbus_debugfs_ops);
3291 		}
3292 
3293 		if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) {
3294 			entries[idx].client = client;
3295 			entries[idx].page = i;
3296 			entries[idx].reg = PMBUS_STATUS_FAN_34;
3297 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i);
3298 			debugfs_create_file(name, 0444, data->debugfs,
3299 					    &entries[idx++],
3300 					    &pmbus_debugfs_ops);
3301 		}
3302 	}
3303 
3304 	return devm_add_action_or_reset(data->dev,
3305 					pmbus_remove_debugfs, data->debugfs);
3306 }
3307 #else
3308 static int pmbus_init_debugfs(struct i2c_client *client,
3309 			      struct pmbus_data *data)
3310 {
3311 	return 0;
3312 }
3313 #endif	/* IS_ENABLED(CONFIG_DEBUG_FS) */
3314 
3315 int pmbus_do_probe(struct i2c_client *client, struct pmbus_driver_info *info)
3316 {
3317 	struct device *dev = &client->dev;
3318 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
3319 	struct pmbus_data *data;
3320 	size_t groups_num = 0;
3321 	int ret;
3322 	int i;
3323 	char *name;
3324 
3325 	if (!info)
3326 		return -ENODEV;
3327 
3328 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
3329 				     | I2C_FUNC_SMBUS_BYTE_DATA
3330 				     | I2C_FUNC_SMBUS_WORD_DATA))
3331 		return -ENODEV;
3332 
3333 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
3334 	if (!data)
3335 		return -ENOMEM;
3336 
3337 	if (info->groups)
3338 		while (info->groups[groups_num])
3339 			groups_num++;
3340 
3341 	data->groups = devm_kcalloc(dev, groups_num + 2, sizeof(void *),
3342 				    GFP_KERNEL);
3343 	if (!data->groups)
3344 		return -ENOMEM;
3345 
3346 	i2c_set_clientdata(client, data);
3347 	mutex_init(&data->update_lock);
3348 	data->dev = dev;
3349 
3350 	if (pdata)
3351 		data->flags = pdata->flags;
3352 	data->info = info;
3353 	data->currpage = -1;
3354 	data->currphase = -1;
3355 
3356 	for (i = 0; i < ARRAY_SIZE(data->vout_low); i++) {
3357 		data->vout_low[i] = -1;
3358 		data->vout_high[i] = -1;
3359 	}
3360 
3361 	ret = pmbus_init_common(client, data, info);
3362 	if (ret < 0)
3363 		return ret;
3364 
3365 	ret = pmbus_find_attributes(client, data);
3366 	if (ret)
3367 		return ret;
3368 
3369 	/*
3370 	 * If there are no attributes, something is wrong.
3371 	 * Bail out instead of trying to register nothing.
3372 	 */
3373 	if (!data->num_attributes) {
3374 		dev_err(dev, "No attributes found\n");
3375 		return -ENODEV;
3376 	}
3377 
3378 	name = devm_kstrdup(dev, client->name, GFP_KERNEL);
3379 	if (!name)
3380 		return -ENOMEM;
3381 	strreplace(name, '-', '_');
3382 
3383 	data->groups[0] = &data->group;
3384 	memcpy(data->groups + 1, info->groups, sizeof(void *) * groups_num);
3385 	data->hwmon_dev = devm_hwmon_device_register_with_groups(dev,
3386 					name, data, data->groups);
3387 	if (IS_ERR(data->hwmon_dev)) {
3388 		dev_err(dev, "Failed to register hwmon device\n");
3389 		return PTR_ERR(data->hwmon_dev);
3390 	}
3391 
3392 	ret = pmbus_regulator_register(data);
3393 	if (ret)
3394 		return ret;
3395 
3396 	ret = pmbus_init_debugfs(client, data);
3397 	if (ret)
3398 		dev_warn(dev, "Failed to register debugfs\n");
3399 
3400 	return 0;
3401 }
3402 EXPORT_SYMBOL_NS_GPL(pmbus_do_probe, PMBUS);
3403 
3404 struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client)
3405 {
3406 	struct pmbus_data *data = i2c_get_clientdata(client);
3407 
3408 	return data->debugfs;
3409 }
3410 EXPORT_SYMBOL_NS_GPL(pmbus_get_debugfs_dir, PMBUS);
3411 
3412 static int __init pmbus_core_init(void)
3413 {
3414 	pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL);
3415 	if (IS_ERR(pmbus_debugfs_dir))
3416 		pmbus_debugfs_dir = NULL;
3417 
3418 	return 0;
3419 }
3420 
3421 static void __exit pmbus_core_exit(void)
3422 {
3423 	debugfs_remove_recursive(pmbus_debugfs_dir);
3424 }
3425 
3426 module_init(pmbus_core_init);
3427 module_exit(pmbus_core_exit);
3428 
3429 MODULE_AUTHOR("Guenter Roeck");
3430 MODULE_DESCRIPTION("PMBus core driver");
3431 MODULE_LICENSE("GPL");
3432