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