xref: /openbmc/linux/drivers/hwmon/pmbus/pmbus_core.c (revision 68198dca)
1 /*
2  * Hardware monitoring driver for PMBus devices
3  *
4  * Copyright (c) 2010, 2011 Ericsson AB.
5  * Copyright (c) 2012 Guenter Roeck
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <linux/debugfs.h>
23 #include <linux/kernel.h>
24 #include <linux/math64.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/err.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/jiffies.h>
33 #include <linux/pmbus.h>
34 #include <linux/regulator/driver.h>
35 #include <linux/regulator/machine.h>
36 #include "pmbus.h"
37 
38 /*
39  * Number of additional attribute pointers to allocate
40  * with each call to krealloc
41  */
42 #define PMBUS_ATTR_ALLOC_SIZE	32
43 
44 /*
45  * Index into status register array, per status register group
46  */
47 #define PB_STATUS_BASE		0
48 #define PB_STATUS_VOUT_BASE	(PB_STATUS_BASE + PMBUS_PAGES)
49 #define PB_STATUS_IOUT_BASE	(PB_STATUS_VOUT_BASE + PMBUS_PAGES)
50 #define PB_STATUS_FAN_BASE	(PB_STATUS_IOUT_BASE + PMBUS_PAGES)
51 #define PB_STATUS_FAN34_BASE	(PB_STATUS_FAN_BASE + PMBUS_PAGES)
52 #define PB_STATUS_TEMP_BASE	(PB_STATUS_FAN34_BASE + PMBUS_PAGES)
53 #define PB_STATUS_INPUT_BASE	(PB_STATUS_TEMP_BASE + PMBUS_PAGES)
54 #define PB_STATUS_VMON_BASE	(PB_STATUS_INPUT_BASE + 1)
55 
56 #define PB_NUM_STATUS_REG	(PB_STATUS_VMON_BASE + 1)
57 
58 #define PMBUS_NAME_SIZE		24
59 
60 struct pmbus_sensor {
61 	struct pmbus_sensor *next;
62 	char name[PMBUS_NAME_SIZE];	/* sysfs sensor name */
63 	struct device_attribute attribute;
64 	u8 page;		/* page number */
65 	u16 reg;		/* register */
66 	enum pmbus_sensor_classes class;	/* sensor class */
67 	bool update;		/* runtime sensor update needed */
68 	int data;		/* Sensor data.
69 				   Negative if there was a read error */
70 };
71 #define to_pmbus_sensor(_attr) \
72 	container_of(_attr, struct pmbus_sensor, attribute)
73 
74 struct pmbus_boolean {
75 	char name[PMBUS_NAME_SIZE];	/* sysfs boolean name */
76 	struct sensor_device_attribute attribute;
77 	struct pmbus_sensor *s1;
78 	struct pmbus_sensor *s2;
79 };
80 #define to_pmbus_boolean(_attr) \
81 	container_of(_attr, struct pmbus_boolean, attribute)
82 
83 struct pmbus_label {
84 	char name[PMBUS_NAME_SIZE];	/* sysfs label name */
85 	struct device_attribute attribute;
86 	char label[PMBUS_NAME_SIZE];	/* label */
87 };
88 #define to_pmbus_label(_attr) \
89 	container_of(_attr, struct pmbus_label, attribute)
90 
91 struct pmbus_data {
92 	struct device *dev;
93 	struct device *hwmon_dev;
94 
95 	u32 flags;		/* from platform data */
96 
97 	int exponent[PMBUS_PAGES];
98 				/* linear mode: exponent for output voltages */
99 
100 	const struct pmbus_driver_info *info;
101 
102 	int max_attributes;
103 	int num_attributes;
104 	struct attribute_group group;
105 	const struct attribute_group *groups[2];
106 	struct dentry *debugfs;		/* debugfs device directory */
107 
108 	struct pmbus_sensor *sensors;
109 
110 	struct mutex update_lock;
111 	bool valid;
112 	unsigned long last_updated;	/* in jiffies */
113 
114 	/*
115 	 * A single status register covers multiple attributes,
116 	 * so we keep them all together.
117 	 */
118 	u16 status[PB_NUM_STATUS_REG];
119 
120 	bool has_status_word;		/* device uses STATUS_WORD register */
121 	int (*read_status)(struct i2c_client *client, int page);
122 
123 	u8 currpage;
124 };
125 
126 struct pmbus_debugfs_entry {
127 	struct i2c_client *client;
128 	u8 page;
129 	u8 reg;
130 };
131 
132 void pmbus_clear_cache(struct i2c_client *client)
133 {
134 	struct pmbus_data *data = i2c_get_clientdata(client);
135 
136 	data->valid = false;
137 }
138 EXPORT_SYMBOL_GPL(pmbus_clear_cache);
139 
140 int pmbus_set_page(struct i2c_client *client, int page)
141 {
142 	struct pmbus_data *data = i2c_get_clientdata(client);
143 	int rv = 0;
144 	int newpage;
145 
146 	if (page >= 0 && page != data->currpage) {
147 		rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
148 		newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
149 		if (newpage != page)
150 			rv = -EIO;
151 		else
152 			data->currpage = page;
153 	}
154 	return rv;
155 }
156 EXPORT_SYMBOL_GPL(pmbus_set_page);
157 
158 int pmbus_write_byte(struct i2c_client *client, int page, u8 value)
159 {
160 	int rv;
161 
162 	rv = pmbus_set_page(client, page);
163 	if (rv < 0)
164 		return rv;
165 
166 	return i2c_smbus_write_byte(client, value);
167 }
168 EXPORT_SYMBOL_GPL(pmbus_write_byte);
169 
170 /*
171  * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
172  * a device specific mapping function exists and calls it if necessary.
173  */
174 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value)
175 {
176 	struct pmbus_data *data = i2c_get_clientdata(client);
177 	const struct pmbus_driver_info *info = data->info;
178 	int status;
179 
180 	if (info->write_byte) {
181 		status = info->write_byte(client, page, value);
182 		if (status != -ENODATA)
183 			return status;
184 	}
185 	return pmbus_write_byte(client, page, value);
186 }
187 
188 int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg,
189 			  u16 word)
190 {
191 	int rv;
192 
193 	rv = pmbus_set_page(client, page);
194 	if (rv < 0)
195 		return rv;
196 
197 	return i2c_smbus_write_word_data(client, reg, word);
198 }
199 EXPORT_SYMBOL_GPL(pmbus_write_word_data);
200 
201 /*
202  * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
203  * a device specific mapping function exists and calls it if necessary.
204  */
205 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg,
206 				  u16 word)
207 {
208 	struct pmbus_data *data = i2c_get_clientdata(client);
209 	const struct pmbus_driver_info *info = data->info;
210 	int status;
211 
212 	if (info->write_word_data) {
213 		status = info->write_word_data(client, page, reg, word);
214 		if (status != -ENODATA)
215 			return status;
216 	}
217 	if (reg >= PMBUS_VIRT_BASE)
218 		return -ENXIO;
219 	return pmbus_write_word_data(client, page, reg, word);
220 }
221 
222 int pmbus_read_word_data(struct i2c_client *client, int page, u8 reg)
223 {
224 	int rv;
225 
226 	rv = pmbus_set_page(client, page);
227 	if (rv < 0)
228 		return rv;
229 
230 	return i2c_smbus_read_word_data(client, reg);
231 }
232 EXPORT_SYMBOL_GPL(pmbus_read_word_data);
233 
234 /*
235  * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
236  * a device specific mapping function exists and calls it if necessary.
237  */
238 static int _pmbus_read_word_data(struct i2c_client *client, int page, int reg)
239 {
240 	struct pmbus_data *data = i2c_get_clientdata(client);
241 	const struct pmbus_driver_info *info = data->info;
242 	int status;
243 
244 	if (info->read_word_data) {
245 		status = info->read_word_data(client, page, reg);
246 		if (status != -ENODATA)
247 			return status;
248 	}
249 	if (reg >= PMBUS_VIRT_BASE)
250 		return -ENXIO;
251 	return pmbus_read_word_data(client, page, reg);
252 }
253 
254 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg)
255 {
256 	int rv;
257 
258 	rv = pmbus_set_page(client, page);
259 	if (rv < 0)
260 		return rv;
261 
262 	return i2c_smbus_read_byte_data(client, reg);
263 }
264 EXPORT_SYMBOL_GPL(pmbus_read_byte_data);
265 
266 int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value)
267 {
268 	int rv;
269 
270 	rv = pmbus_set_page(client, page);
271 	if (rv < 0)
272 		return rv;
273 
274 	return i2c_smbus_write_byte_data(client, reg, value);
275 }
276 EXPORT_SYMBOL_GPL(pmbus_write_byte_data);
277 
278 int pmbus_update_byte_data(struct i2c_client *client, int page, u8 reg,
279 			   u8 mask, u8 value)
280 {
281 	unsigned int tmp;
282 	int rv;
283 
284 	rv = pmbus_read_byte_data(client, page, reg);
285 	if (rv < 0)
286 		return rv;
287 
288 	tmp = (rv & ~mask) | (value & mask);
289 
290 	if (tmp != rv)
291 		rv = pmbus_write_byte_data(client, page, reg, tmp);
292 
293 	return rv;
294 }
295 EXPORT_SYMBOL_GPL(pmbus_update_byte_data);
296 
297 /*
298  * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
299  * a device specific mapping function exists and calls it if necessary.
300  */
301 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
302 {
303 	struct pmbus_data *data = i2c_get_clientdata(client);
304 	const struct pmbus_driver_info *info = data->info;
305 	int status;
306 
307 	if (info->read_byte_data) {
308 		status = info->read_byte_data(client, page, reg);
309 		if (status != -ENODATA)
310 			return status;
311 	}
312 	return pmbus_read_byte_data(client, page, reg);
313 }
314 
315 static void pmbus_clear_fault_page(struct i2c_client *client, int page)
316 {
317 	_pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
318 }
319 
320 void pmbus_clear_faults(struct i2c_client *client)
321 {
322 	struct pmbus_data *data = i2c_get_clientdata(client);
323 	int i;
324 
325 	for (i = 0; i < data->info->pages; i++)
326 		pmbus_clear_fault_page(client, i);
327 }
328 EXPORT_SYMBOL_GPL(pmbus_clear_faults);
329 
330 static int pmbus_check_status_cml(struct i2c_client *client)
331 {
332 	struct pmbus_data *data = i2c_get_clientdata(client);
333 	int status, status2;
334 
335 	status = data->read_status(client, -1);
336 	if (status < 0 || (status & PB_STATUS_CML)) {
337 		status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
338 		if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
339 			return -EIO;
340 	}
341 	return 0;
342 }
343 
344 static bool pmbus_check_register(struct i2c_client *client,
345 				 int (*func)(struct i2c_client *client,
346 					     int page, int reg),
347 				 int page, int reg)
348 {
349 	int rv;
350 	struct pmbus_data *data = i2c_get_clientdata(client);
351 
352 	rv = func(client, page, reg);
353 	if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
354 		rv = pmbus_check_status_cml(client);
355 	pmbus_clear_fault_page(client, -1);
356 	return rv >= 0;
357 }
358 
359 static bool pmbus_check_status_register(struct i2c_client *client, int page)
360 {
361 	int status;
362 	struct pmbus_data *data = i2c_get_clientdata(client);
363 
364 	status = data->read_status(client, page);
365 	if (status >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK) &&
366 	    (status & PB_STATUS_CML)) {
367 		status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
368 		if (status < 0 || (status & PB_CML_FAULT_INVALID_COMMAND))
369 			status = -EIO;
370 	}
371 
372 	pmbus_clear_fault_page(client, -1);
373 	return status >= 0;
374 }
375 
376 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
377 {
378 	return pmbus_check_register(client, _pmbus_read_byte_data, page, reg);
379 }
380 EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
381 
382 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
383 {
384 	return pmbus_check_register(client, _pmbus_read_word_data, page, reg);
385 }
386 EXPORT_SYMBOL_GPL(pmbus_check_word_register);
387 
388 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
389 {
390 	struct pmbus_data *data = i2c_get_clientdata(client);
391 
392 	return data->info;
393 }
394 EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
395 
396 static struct _pmbus_status {
397 	u32 func;
398 	u16 base;
399 	u16 reg;
400 } pmbus_status[] = {
401 	{ PMBUS_HAVE_STATUS_VOUT, PB_STATUS_VOUT_BASE, PMBUS_STATUS_VOUT },
402 	{ PMBUS_HAVE_STATUS_IOUT, PB_STATUS_IOUT_BASE, PMBUS_STATUS_IOUT },
403 	{ PMBUS_HAVE_STATUS_TEMP, PB_STATUS_TEMP_BASE,
404 	  PMBUS_STATUS_TEMPERATURE },
405 	{ PMBUS_HAVE_STATUS_FAN12, PB_STATUS_FAN_BASE, PMBUS_STATUS_FAN_12 },
406 	{ PMBUS_HAVE_STATUS_FAN34, PB_STATUS_FAN34_BASE, PMBUS_STATUS_FAN_34 },
407 };
408 
409 static struct pmbus_data *pmbus_update_device(struct device *dev)
410 {
411 	struct i2c_client *client = to_i2c_client(dev->parent);
412 	struct pmbus_data *data = i2c_get_clientdata(client);
413 	const struct pmbus_driver_info *info = data->info;
414 	struct pmbus_sensor *sensor;
415 
416 	mutex_lock(&data->update_lock);
417 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
418 		int i, j;
419 
420 		for (i = 0; i < info->pages; i++) {
421 			data->status[PB_STATUS_BASE + i]
422 			    = data->read_status(client, i);
423 			for (j = 0; j < ARRAY_SIZE(pmbus_status); j++) {
424 				struct _pmbus_status *s = &pmbus_status[j];
425 
426 				if (!(info->func[i] & s->func))
427 					continue;
428 				data->status[s->base + i]
429 					= _pmbus_read_byte_data(client, i,
430 								s->reg);
431 			}
432 		}
433 
434 		if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
435 			data->status[PB_STATUS_INPUT_BASE]
436 			  = _pmbus_read_byte_data(client, 0,
437 						  PMBUS_STATUS_INPUT);
438 
439 		if (info->func[0] & PMBUS_HAVE_STATUS_VMON)
440 			data->status[PB_STATUS_VMON_BASE]
441 			  = _pmbus_read_byte_data(client, 0,
442 						  PMBUS_VIRT_STATUS_VMON);
443 
444 		for (sensor = data->sensors; sensor; sensor = sensor->next) {
445 			if (!data->valid || sensor->update)
446 				sensor->data
447 				    = _pmbus_read_word_data(client,
448 							    sensor->page,
449 							    sensor->reg);
450 		}
451 		pmbus_clear_faults(client);
452 		data->last_updated = jiffies;
453 		data->valid = 1;
454 	}
455 	mutex_unlock(&data->update_lock);
456 	return data;
457 }
458 
459 /*
460  * Convert linear sensor values to milli- or micro-units
461  * depending on sensor type.
462  */
463 static long pmbus_reg2data_linear(struct pmbus_data *data,
464 				  struct pmbus_sensor *sensor)
465 {
466 	s16 exponent;
467 	s32 mantissa;
468 	long val;
469 
470 	if (sensor->class == PSC_VOLTAGE_OUT) {	/* LINEAR16 */
471 		exponent = data->exponent[sensor->page];
472 		mantissa = (u16) sensor->data;
473 	} else {				/* LINEAR11 */
474 		exponent = ((s16)sensor->data) >> 11;
475 		mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5;
476 	}
477 
478 	val = mantissa;
479 
480 	/* scale result to milli-units for all sensors except fans */
481 	if (sensor->class != PSC_FAN)
482 		val = val * 1000L;
483 
484 	/* scale result to micro-units for power sensors */
485 	if (sensor->class == PSC_POWER)
486 		val = val * 1000L;
487 
488 	if (exponent >= 0)
489 		val <<= exponent;
490 	else
491 		val >>= -exponent;
492 
493 	return val;
494 }
495 
496 /*
497  * Convert direct sensor values to milli- or micro-units
498  * depending on sensor type.
499  */
500 static long pmbus_reg2data_direct(struct pmbus_data *data,
501 				  struct pmbus_sensor *sensor)
502 {
503 	s64 b, val = (s16)sensor->data;
504 	s32 m, R;
505 
506 	m = data->info->m[sensor->class];
507 	b = data->info->b[sensor->class];
508 	R = data->info->R[sensor->class];
509 
510 	if (m == 0)
511 		return 0;
512 
513 	/* X = 1/m * (Y * 10^-R - b) */
514 	R = -R;
515 	/* scale result to milli-units for everything but fans */
516 	if (sensor->class != PSC_FAN) {
517 		R += 3;
518 		b *= 1000;
519 	}
520 
521 	/* scale result to micro-units for power sensors */
522 	if (sensor->class == PSC_POWER) {
523 		R += 3;
524 		b *= 1000;
525 	}
526 
527 	while (R > 0) {
528 		val *= 10;
529 		R--;
530 	}
531 	while (R < 0) {
532 		val = div_s64(val + 5LL, 10L);  /* round closest */
533 		R++;
534 	}
535 
536 	val = div_s64(val - b, m);
537 	return clamp_val(val, LONG_MIN, LONG_MAX);
538 }
539 
540 /*
541  * Convert VID sensor values to milli- or micro-units
542  * depending on sensor type.
543  */
544 static long pmbus_reg2data_vid(struct pmbus_data *data,
545 			       struct pmbus_sensor *sensor)
546 {
547 	long val = sensor->data;
548 	long rv = 0;
549 
550 	switch (data->info->vrm_version) {
551 	case vr11:
552 		if (val >= 0x02 && val <= 0xb2)
553 			rv = DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100);
554 		break;
555 	case vr12:
556 		if (val >= 0x01)
557 			rv = 250 + (val - 1) * 5;
558 		break;
559 	case vr13:
560 		if (val >= 0x01)
561 			rv = 500 + (val - 1) * 10;
562 		break;
563 	}
564 	return rv;
565 }
566 
567 static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
568 {
569 	long val;
570 
571 	switch (data->info->format[sensor->class]) {
572 	case direct:
573 		val = pmbus_reg2data_direct(data, sensor);
574 		break;
575 	case vid:
576 		val = pmbus_reg2data_vid(data, sensor);
577 		break;
578 	case linear:
579 	default:
580 		val = pmbus_reg2data_linear(data, sensor);
581 		break;
582 	}
583 	return val;
584 }
585 
586 #define MAX_MANTISSA	(1023 * 1000)
587 #define MIN_MANTISSA	(511 * 1000)
588 
589 static u16 pmbus_data2reg_linear(struct pmbus_data *data,
590 				 struct pmbus_sensor *sensor, long val)
591 {
592 	s16 exponent = 0, mantissa;
593 	bool negative = false;
594 
595 	/* simple case */
596 	if (val == 0)
597 		return 0;
598 
599 	if (sensor->class == PSC_VOLTAGE_OUT) {
600 		/* LINEAR16 does not support negative voltages */
601 		if (val < 0)
602 			return 0;
603 
604 		/*
605 		 * For a static exponents, we don't have a choice
606 		 * but to adjust the value to it.
607 		 */
608 		if (data->exponent[sensor->page] < 0)
609 			val <<= -data->exponent[sensor->page];
610 		else
611 			val >>= data->exponent[sensor->page];
612 		val = DIV_ROUND_CLOSEST(val, 1000);
613 		return val & 0xffff;
614 	}
615 
616 	if (val < 0) {
617 		negative = true;
618 		val = -val;
619 	}
620 
621 	/* Power is in uW. Convert to mW before converting. */
622 	if (sensor->class == PSC_POWER)
623 		val = DIV_ROUND_CLOSEST(val, 1000L);
624 
625 	/*
626 	 * For simplicity, convert fan data to milli-units
627 	 * before calculating the exponent.
628 	 */
629 	if (sensor->class == PSC_FAN)
630 		val = val * 1000;
631 
632 	/* Reduce large mantissa until it fits into 10 bit */
633 	while (val >= MAX_MANTISSA && exponent < 15) {
634 		exponent++;
635 		val >>= 1;
636 	}
637 	/* Increase small mantissa to improve precision */
638 	while (val < MIN_MANTISSA && exponent > -15) {
639 		exponent--;
640 		val <<= 1;
641 	}
642 
643 	/* Convert mantissa from milli-units to units */
644 	mantissa = DIV_ROUND_CLOSEST(val, 1000);
645 
646 	/* Ensure that resulting number is within range */
647 	if (mantissa > 0x3ff)
648 		mantissa = 0x3ff;
649 
650 	/* restore sign */
651 	if (negative)
652 		mantissa = -mantissa;
653 
654 	/* Convert to 5 bit exponent, 11 bit mantissa */
655 	return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
656 }
657 
658 static u16 pmbus_data2reg_direct(struct pmbus_data *data,
659 				 struct pmbus_sensor *sensor, long val)
660 {
661 	s64 b, val64 = val;
662 	s32 m, R;
663 
664 	m = data->info->m[sensor->class];
665 	b = data->info->b[sensor->class];
666 	R = data->info->R[sensor->class];
667 
668 	/* Power is in uW. Adjust R and b. */
669 	if (sensor->class == PSC_POWER) {
670 		R -= 3;
671 		b *= 1000;
672 	}
673 
674 	/* Calculate Y = (m * X + b) * 10^R */
675 	if (sensor->class != PSC_FAN) {
676 		R -= 3;		/* Adjust R and b for data in milli-units */
677 		b *= 1000;
678 	}
679 	val64 = val64 * m + b;
680 
681 	while (R > 0) {
682 		val64 *= 10;
683 		R--;
684 	}
685 	while (R < 0) {
686 		val64 = div_s64(val64 + 5LL, 10L);  /* round closest */
687 		R++;
688 	}
689 
690 	return (u16)clamp_val(val64, S16_MIN, S16_MAX);
691 }
692 
693 static u16 pmbus_data2reg_vid(struct pmbus_data *data,
694 			      struct pmbus_sensor *sensor, long val)
695 {
696 	val = clamp_val(val, 500, 1600);
697 
698 	return 2 + DIV_ROUND_CLOSEST((1600 - val) * 100, 625);
699 }
700 
701 static u16 pmbus_data2reg(struct pmbus_data *data,
702 			  struct pmbus_sensor *sensor, long val)
703 {
704 	u16 regval;
705 
706 	switch (data->info->format[sensor->class]) {
707 	case direct:
708 		regval = pmbus_data2reg_direct(data, sensor, val);
709 		break;
710 	case vid:
711 		regval = pmbus_data2reg_vid(data, sensor, val);
712 		break;
713 	case linear:
714 	default:
715 		regval = pmbus_data2reg_linear(data, sensor, val);
716 		break;
717 	}
718 	return regval;
719 }
720 
721 /*
722  * Return boolean calculated from converted data.
723  * <index> defines a status register index and mask.
724  * The mask is in the lower 8 bits, the register index is in bits 8..23.
725  *
726  * The associated pmbus_boolean structure contains optional pointers to two
727  * sensor attributes. If specified, those attributes are compared against each
728  * other to determine if a limit has been exceeded.
729  *
730  * If the sensor attribute pointers are NULL, the function returns true if
731  * (status[reg] & mask) is true.
732  *
733  * If sensor attribute pointers are provided, a comparison against a specified
734  * limit has to be performed to determine the boolean result.
735  * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
736  * sensor values referenced by sensor attribute pointers s1 and s2).
737  *
738  * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
739  * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
740  *
741  * If a negative value is stored in any of the referenced registers, this value
742  * reflects an error code which will be returned.
743  */
744 static int pmbus_get_boolean(struct pmbus_data *data, struct pmbus_boolean *b,
745 			     int index)
746 {
747 	struct pmbus_sensor *s1 = b->s1;
748 	struct pmbus_sensor *s2 = b->s2;
749 	u16 reg = (index >> 16) & 0xffff;
750 	u16 mask = index & 0xffff;
751 	int ret, status;
752 	u16 regval;
753 
754 	status = data->status[reg];
755 	if (status < 0)
756 		return status;
757 
758 	regval = status & mask;
759 	if (!s1 && !s2) {
760 		ret = !!regval;
761 	} else if (!s1 || !s2) {
762 		WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b, s1, s2);
763 		return 0;
764 	} else {
765 		long v1, v2;
766 
767 		if (s1->data < 0)
768 			return s1->data;
769 		if (s2->data < 0)
770 			return s2->data;
771 
772 		v1 = pmbus_reg2data(data, s1);
773 		v2 = pmbus_reg2data(data, s2);
774 		ret = !!(regval && v1 >= v2);
775 	}
776 	return ret;
777 }
778 
779 static ssize_t pmbus_show_boolean(struct device *dev,
780 				  struct device_attribute *da, char *buf)
781 {
782 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
783 	struct pmbus_boolean *boolean = to_pmbus_boolean(attr);
784 	struct pmbus_data *data = pmbus_update_device(dev);
785 	int val;
786 
787 	val = pmbus_get_boolean(data, boolean, attr->index);
788 	if (val < 0)
789 		return val;
790 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
791 }
792 
793 static ssize_t pmbus_show_sensor(struct device *dev,
794 				 struct device_attribute *devattr, char *buf)
795 {
796 	struct pmbus_data *data = pmbus_update_device(dev);
797 	struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
798 
799 	if (sensor->data < 0)
800 		return sensor->data;
801 
802 	return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor));
803 }
804 
805 static ssize_t pmbus_set_sensor(struct device *dev,
806 				struct device_attribute *devattr,
807 				const char *buf, size_t count)
808 {
809 	struct i2c_client *client = to_i2c_client(dev->parent);
810 	struct pmbus_data *data = i2c_get_clientdata(client);
811 	struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
812 	ssize_t rv = count;
813 	long val = 0;
814 	int ret;
815 	u16 regval;
816 
817 	if (kstrtol(buf, 10, &val) < 0)
818 		return -EINVAL;
819 
820 	mutex_lock(&data->update_lock);
821 	regval = pmbus_data2reg(data, sensor, val);
822 	ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
823 	if (ret < 0)
824 		rv = ret;
825 	else
826 		sensor->data = regval;
827 	mutex_unlock(&data->update_lock);
828 	return rv;
829 }
830 
831 static ssize_t pmbus_show_label(struct device *dev,
832 				struct device_attribute *da, char *buf)
833 {
834 	struct pmbus_label *label = to_pmbus_label(da);
835 
836 	return snprintf(buf, PAGE_SIZE, "%s\n", label->label);
837 }
838 
839 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
840 {
841 	if (data->num_attributes >= data->max_attributes - 1) {
842 		int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
843 		void *new_attrs = krealloc(data->group.attrs,
844 					   new_max_attrs * sizeof(void *),
845 					   GFP_KERNEL);
846 		if (!new_attrs)
847 			return -ENOMEM;
848 		data->group.attrs = new_attrs;
849 		data->max_attributes = new_max_attrs;
850 	}
851 
852 	data->group.attrs[data->num_attributes++] = attr;
853 	data->group.attrs[data->num_attributes] = NULL;
854 	return 0;
855 }
856 
857 static void pmbus_dev_attr_init(struct device_attribute *dev_attr,
858 				const char *name,
859 				umode_t mode,
860 				ssize_t (*show)(struct device *dev,
861 						struct device_attribute *attr,
862 						char *buf),
863 				ssize_t (*store)(struct device *dev,
864 						 struct device_attribute *attr,
865 						 const char *buf, size_t count))
866 {
867 	sysfs_attr_init(&dev_attr->attr);
868 	dev_attr->attr.name = name;
869 	dev_attr->attr.mode = mode;
870 	dev_attr->show = show;
871 	dev_attr->store = store;
872 }
873 
874 static void pmbus_attr_init(struct sensor_device_attribute *a,
875 			    const char *name,
876 			    umode_t mode,
877 			    ssize_t (*show)(struct device *dev,
878 					    struct device_attribute *attr,
879 					    char *buf),
880 			    ssize_t (*store)(struct device *dev,
881 					     struct device_attribute *attr,
882 					     const char *buf, size_t count),
883 			    int idx)
884 {
885 	pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store);
886 	a->index = idx;
887 }
888 
889 static int pmbus_add_boolean(struct pmbus_data *data,
890 			     const char *name, const char *type, int seq,
891 			     struct pmbus_sensor *s1,
892 			     struct pmbus_sensor *s2,
893 			     u16 reg, u16 mask)
894 {
895 	struct pmbus_boolean *boolean;
896 	struct sensor_device_attribute *a;
897 
898 	boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL);
899 	if (!boolean)
900 		return -ENOMEM;
901 
902 	a = &boolean->attribute;
903 
904 	snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
905 		 name, seq, type);
906 	boolean->s1 = s1;
907 	boolean->s2 = s2;
908 	pmbus_attr_init(a, boolean->name, S_IRUGO, pmbus_show_boolean, NULL,
909 			(reg << 16) | mask);
910 
911 	return pmbus_add_attribute(data, &a->dev_attr.attr);
912 }
913 
914 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
915 					     const char *name, const char *type,
916 					     int seq, int page, int reg,
917 					     enum pmbus_sensor_classes class,
918 					     bool update, bool readonly)
919 {
920 	struct pmbus_sensor *sensor;
921 	struct device_attribute *a;
922 
923 	sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL);
924 	if (!sensor)
925 		return NULL;
926 	a = &sensor->attribute;
927 
928 	snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
929 		 name, seq, type);
930 	sensor->page = page;
931 	sensor->reg = reg;
932 	sensor->class = class;
933 	sensor->update = update;
934 	pmbus_dev_attr_init(a, sensor->name,
935 			    readonly ? S_IRUGO : S_IRUGO | S_IWUSR,
936 			    pmbus_show_sensor, pmbus_set_sensor);
937 
938 	if (pmbus_add_attribute(data, &a->attr))
939 		return NULL;
940 
941 	sensor->next = data->sensors;
942 	data->sensors = sensor;
943 
944 	return sensor;
945 }
946 
947 static int pmbus_add_label(struct pmbus_data *data,
948 			   const char *name, int seq,
949 			   const char *lstring, int index)
950 {
951 	struct pmbus_label *label;
952 	struct device_attribute *a;
953 
954 	label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL);
955 	if (!label)
956 		return -ENOMEM;
957 
958 	a = &label->attribute;
959 
960 	snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
961 	if (!index)
962 		strncpy(label->label, lstring, sizeof(label->label) - 1);
963 	else
964 		snprintf(label->label, sizeof(label->label), "%s%d", lstring,
965 			 index);
966 
967 	pmbus_dev_attr_init(a, label->name, S_IRUGO, pmbus_show_label, NULL);
968 	return pmbus_add_attribute(data, &a->attr);
969 }
970 
971 /*
972  * Search for attributes. Allocate sensors, booleans, and labels as needed.
973  */
974 
975 /*
976  * The pmbus_limit_attr structure describes a single limit attribute
977  * and its associated alarm attribute.
978  */
979 struct pmbus_limit_attr {
980 	u16 reg;		/* Limit register */
981 	u16 sbit;		/* Alarm attribute status bit */
982 	bool update;		/* True if register needs updates */
983 	bool low;		/* True if low limit; for limits with compare
984 				   functions only */
985 	const char *attr;	/* Attribute name */
986 	const char *alarm;	/* Alarm attribute name */
987 };
988 
989 /*
990  * The pmbus_sensor_attr structure describes one sensor attribute. This
991  * description includes a reference to the associated limit attributes.
992  */
993 struct pmbus_sensor_attr {
994 	u16 reg;			/* sensor register */
995 	u16 gbit;			/* generic status bit */
996 	u8 nlimit;			/* # of limit registers */
997 	enum pmbus_sensor_classes class;/* sensor class */
998 	const char *label;		/* sensor label */
999 	bool paged;			/* true if paged sensor */
1000 	bool update;			/* true if update needed */
1001 	bool compare;			/* true if compare function needed */
1002 	u32 func;			/* sensor mask */
1003 	u32 sfunc;			/* sensor status mask */
1004 	int sbase;			/* status base register */
1005 	const struct pmbus_limit_attr *limit;/* limit registers */
1006 };
1007 
1008 /*
1009  * Add a set of limit attributes and, if supported, the associated
1010  * alarm attributes.
1011  * returns 0 if no alarm register found, 1 if an alarm register was found,
1012  * < 0 on errors.
1013  */
1014 static int pmbus_add_limit_attrs(struct i2c_client *client,
1015 				 struct pmbus_data *data,
1016 				 const struct pmbus_driver_info *info,
1017 				 const char *name, int index, int page,
1018 				 struct pmbus_sensor *base,
1019 				 const struct pmbus_sensor_attr *attr)
1020 {
1021 	const struct pmbus_limit_attr *l = attr->limit;
1022 	int nlimit = attr->nlimit;
1023 	int have_alarm = 0;
1024 	int i, ret;
1025 	struct pmbus_sensor *curr;
1026 
1027 	for (i = 0; i < nlimit; i++) {
1028 		if (pmbus_check_word_register(client, page, l->reg)) {
1029 			curr = pmbus_add_sensor(data, name, l->attr, index,
1030 						page, l->reg, attr->class,
1031 						attr->update || l->update,
1032 						false);
1033 			if (!curr)
1034 				return -ENOMEM;
1035 			if (l->sbit && (info->func[page] & attr->sfunc)) {
1036 				ret = pmbus_add_boolean(data, name,
1037 					l->alarm, index,
1038 					attr->compare ?  l->low ? curr : base
1039 						      : NULL,
1040 					attr->compare ? l->low ? base : curr
1041 						      : NULL,
1042 					attr->sbase + page, l->sbit);
1043 				if (ret)
1044 					return ret;
1045 				have_alarm = 1;
1046 			}
1047 		}
1048 		l++;
1049 	}
1050 	return have_alarm;
1051 }
1052 
1053 static int pmbus_add_sensor_attrs_one(struct i2c_client *client,
1054 				      struct pmbus_data *data,
1055 				      const struct pmbus_driver_info *info,
1056 				      const char *name,
1057 				      int index, int page,
1058 				      const struct pmbus_sensor_attr *attr)
1059 {
1060 	struct pmbus_sensor *base;
1061 	bool upper = !!(attr->gbit & 0xff00);	/* need to check STATUS_WORD */
1062 	int ret;
1063 
1064 	if (attr->label) {
1065 		ret = pmbus_add_label(data, name, index, attr->label,
1066 				      attr->paged ? page + 1 : 0);
1067 		if (ret)
1068 			return ret;
1069 	}
1070 	base = pmbus_add_sensor(data, name, "input", index, page, attr->reg,
1071 				attr->class, true, true);
1072 	if (!base)
1073 		return -ENOMEM;
1074 	if (attr->sfunc) {
1075 		ret = pmbus_add_limit_attrs(client, data, info, name,
1076 					    index, page, base, attr);
1077 		if (ret < 0)
1078 			return ret;
1079 		/*
1080 		 * Add generic alarm attribute only if there are no individual
1081 		 * alarm attributes, if there is a global alarm bit, and if
1082 		 * the generic status register (word or byte, depending on
1083 		 * which global bit is set) for this page is accessible.
1084 		 */
1085 		if (!ret && attr->gbit &&
1086 		    (!upper || (upper && data->has_status_word)) &&
1087 		    pmbus_check_status_register(client, page)) {
1088 			ret = pmbus_add_boolean(data, name, "alarm", index,
1089 						NULL, NULL,
1090 						PB_STATUS_BASE + page,
1091 						attr->gbit);
1092 			if (ret)
1093 				return ret;
1094 		}
1095 	}
1096 	return 0;
1097 }
1098 
1099 static int pmbus_add_sensor_attrs(struct i2c_client *client,
1100 				  struct pmbus_data *data,
1101 				  const char *name,
1102 				  const struct pmbus_sensor_attr *attrs,
1103 				  int nattrs)
1104 {
1105 	const struct pmbus_driver_info *info = data->info;
1106 	int index, i;
1107 	int ret;
1108 
1109 	index = 1;
1110 	for (i = 0; i < nattrs; i++) {
1111 		int page, pages;
1112 
1113 		pages = attrs->paged ? info->pages : 1;
1114 		for (page = 0; page < pages; page++) {
1115 			if (!(info->func[page] & attrs->func))
1116 				continue;
1117 			ret = pmbus_add_sensor_attrs_one(client, data, info,
1118 							 name, index, page,
1119 							 attrs);
1120 			if (ret)
1121 				return ret;
1122 			index++;
1123 		}
1124 		attrs++;
1125 	}
1126 	return 0;
1127 }
1128 
1129 static const struct pmbus_limit_attr vin_limit_attrs[] = {
1130 	{
1131 		.reg = PMBUS_VIN_UV_WARN_LIMIT,
1132 		.attr = "min",
1133 		.alarm = "min_alarm",
1134 		.sbit = PB_VOLTAGE_UV_WARNING,
1135 	}, {
1136 		.reg = PMBUS_VIN_UV_FAULT_LIMIT,
1137 		.attr = "lcrit",
1138 		.alarm = "lcrit_alarm",
1139 		.sbit = PB_VOLTAGE_UV_FAULT,
1140 	}, {
1141 		.reg = PMBUS_VIN_OV_WARN_LIMIT,
1142 		.attr = "max",
1143 		.alarm = "max_alarm",
1144 		.sbit = PB_VOLTAGE_OV_WARNING,
1145 	}, {
1146 		.reg = PMBUS_VIN_OV_FAULT_LIMIT,
1147 		.attr = "crit",
1148 		.alarm = "crit_alarm",
1149 		.sbit = PB_VOLTAGE_OV_FAULT,
1150 	}, {
1151 		.reg = PMBUS_VIRT_READ_VIN_AVG,
1152 		.update = true,
1153 		.attr = "average",
1154 	}, {
1155 		.reg = PMBUS_VIRT_READ_VIN_MIN,
1156 		.update = true,
1157 		.attr = "lowest",
1158 	}, {
1159 		.reg = PMBUS_VIRT_READ_VIN_MAX,
1160 		.update = true,
1161 		.attr = "highest",
1162 	}, {
1163 		.reg = PMBUS_VIRT_RESET_VIN_HISTORY,
1164 		.attr = "reset_history",
1165 	},
1166 };
1167 
1168 static const struct pmbus_limit_attr vmon_limit_attrs[] = {
1169 	{
1170 		.reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT,
1171 		.attr = "min",
1172 		.alarm = "min_alarm",
1173 		.sbit = PB_VOLTAGE_UV_WARNING,
1174 	}, {
1175 		.reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT,
1176 		.attr = "lcrit",
1177 		.alarm = "lcrit_alarm",
1178 		.sbit = PB_VOLTAGE_UV_FAULT,
1179 	}, {
1180 		.reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT,
1181 		.attr = "max",
1182 		.alarm = "max_alarm",
1183 		.sbit = PB_VOLTAGE_OV_WARNING,
1184 	}, {
1185 		.reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT,
1186 		.attr = "crit",
1187 		.alarm = "crit_alarm",
1188 		.sbit = PB_VOLTAGE_OV_FAULT,
1189 	}
1190 };
1191 
1192 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1193 	{
1194 		.reg = PMBUS_VOUT_UV_WARN_LIMIT,
1195 		.attr = "min",
1196 		.alarm = "min_alarm",
1197 		.sbit = PB_VOLTAGE_UV_WARNING,
1198 	}, {
1199 		.reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1200 		.attr = "lcrit",
1201 		.alarm = "lcrit_alarm",
1202 		.sbit = PB_VOLTAGE_UV_FAULT,
1203 	}, {
1204 		.reg = PMBUS_VOUT_OV_WARN_LIMIT,
1205 		.attr = "max",
1206 		.alarm = "max_alarm",
1207 		.sbit = PB_VOLTAGE_OV_WARNING,
1208 	}, {
1209 		.reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1210 		.attr = "crit",
1211 		.alarm = "crit_alarm",
1212 		.sbit = PB_VOLTAGE_OV_FAULT,
1213 	}, {
1214 		.reg = PMBUS_VIRT_READ_VOUT_AVG,
1215 		.update = true,
1216 		.attr = "average",
1217 	}, {
1218 		.reg = PMBUS_VIRT_READ_VOUT_MIN,
1219 		.update = true,
1220 		.attr = "lowest",
1221 	}, {
1222 		.reg = PMBUS_VIRT_READ_VOUT_MAX,
1223 		.update = true,
1224 		.attr = "highest",
1225 	}, {
1226 		.reg = PMBUS_VIRT_RESET_VOUT_HISTORY,
1227 		.attr = "reset_history",
1228 	}
1229 };
1230 
1231 static const struct pmbus_sensor_attr voltage_attributes[] = {
1232 	{
1233 		.reg = PMBUS_READ_VIN,
1234 		.class = PSC_VOLTAGE_IN,
1235 		.label = "vin",
1236 		.func = PMBUS_HAVE_VIN,
1237 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1238 		.sbase = PB_STATUS_INPUT_BASE,
1239 		.gbit = PB_STATUS_VIN_UV,
1240 		.limit = vin_limit_attrs,
1241 		.nlimit = ARRAY_SIZE(vin_limit_attrs),
1242 	}, {
1243 		.reg = PMBUS_VIRT_READ_VMON,
1244 		.class = PSC_VOLTAGE_IN,
1245 		.label = "vmon",
1246 		.func = PMBUS_HAVE_VMON,
1247 		.sfunc = PMBUS_HAVE_STATUS_VMON,
1248 		.sbase = PB_STATUS_VMON_BASE,
1249 		.limit = vmon_limit_attrs,
1250 		.nlimit = ARRAY_SIZE(vmon_limit_attrs),
1251 	}, {
1252 		.reg = PMBUS_READ_VCAP,
1253 		.class = PSC_VOLTAGE_IN,
1254 		.label = "vcap",
1255 		.func = PMBUS_HAVE_VCAP,
1256 	}, {
1257 		.reg = PMBUS_READ_VOUT,
1258 		.class = PSC_VOLTAGE_OUT,
1259 		.label = "vout",
1260 		.paged = true,
1261 		.func = PMBUS_HAVE_VOUT,
1262 		.sfunc = PMBUS_HAVE_STATUS_VOUT,
1263 		.sbase = PB_STATUS_VOUT_BASE,
1264 		.gbit = PB_STATUS_VOUT_OV,
1265 		.limit = vout_limit_attrs,
1266 		.nlimit = ARRAY_SIZE(vout_limit_attrs),
1267 	}
1268 };
1269 
1270 /* Current attributes */
1271 
1272 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1273 	{
1274 		.reg = PMBUS_IIN_OC_WARN_LIMIT,
1275 		.attr = "max",
1276 		.alarm = "max_alarm",
1277 		.sbit = PB_IIN_OC_WARNING,
1278 	}, {
1279 		.reg = PMBUS_IIN_OC_FAULT_LIMIT,
1280 		.attr = "crit",
1281 		.alarm = "crit_alarm",
1282 		.sbit = PB_IIN_OC_FAULT,
1283 	}, {
1284 		.reg = PMBUS_VIRT_READ_IIN_AVG,
1285 		.update = true,
1286 		.attr = "average",
1287 	}, {
1288 		.reg = PMBUS_VIRT_READ_IIN_MIN,
1289 		.update = true,
1290 		.attr = "lowest",
1291 	}, {
1292 		.reg = PMBUS_VIRT_READ_IIN_MAX,
1293 		.update = true,
1294 		.attr = "highest",
1295 	}, {
1296 		.reg = PMBUS_VIRT_RESET_IIN_HISTORY,
1297 		.attr = "reset_history",
1298 	}
1299 };
1300 
1301 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1302 	{
1303 		.reg = PMBUS_IOUT_OC_WARN_LIMIT,
1304 		.attr = "max",
1305 		.alarm = "max_alarm",
1306 		.sbit = PB_IOUT_OC_WARNING,
1307 	}, {
1308 		.reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1309 		.attr = "lcrit",
1310 		.alarm = "lcrit_alarm",
1311 		.sbit = PB_IOUT_UC_FAULT,
1312 	}, {
1313 		.reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1314 		.attr = "crit",
1315 		.alarm = "crit_alarm",
1316 		.sbit = PB_IOUT_OC_FAULT,
1317 	}, {
1318 		.reg = PMBUS_VIRT_READ_IOUT_AVG,
1319 		.update = true,
1320 		.attr = "average",
1321 	}, {
1322 		.reg = PMBUS_VIRT_READ_IOUT_MIN,
1323 		.update = true,
1324 		.attr = "lowest",
1325 	}, {
1326 		.reg = PMBUS_VIRT_READ_IOUT_MAX,
1327 		.update = true,
1328 		.attr = "highest",
1329 	}, {
1330 		.reg = PMBUS_VIRT_RESET_IOUT_HISTORY,
1331 		.attr = "reset_history",
1332 	}
1333 };
1334 
1335 static const struct pmbus_sensor_attr current_attributes[] = {
1336 	{
1337 		.reg = PMBUS_READ_IIN,
1338 		.class = PSC_CURRENT_IN,
1339 		.label = "iin",
1340 		.func = PMBUS_HAVE_IIN,
1341 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1342 		.sbase = PB_STATUS_INPUT_BASE,
1343 		.gbit = PB_STATUS_INPUT,
1344 		.limit = iin_limit_attrs,
1345 		.nlimit = ARRAY_SIZE(iin_limit_attrs),
1346 	}, {
1347 		.reg = PMBUS_READ_IOUT,
1348 		.class = PSC_CURRENT_OUT,
1349 		.label = "iout",
1350 		.paged = true,
1351 		.func = PMBUS_HAVE_IOUT,
1352 		.sfunc = PMBUS_HAVE_STATUS_IOUT,
1353 		.sbase = PB_STATUS_IOUT_BASE,
1354 		.gbit = PB_STATUS_IOUT_OC,
1355 		.limit = iout_limit_attrs,
1356 		.nlimit = ARRAY_SIZE(iout_limit_attrs),
1357 	}
1358 };
1359 
1360 /* Power attributes */
1361 
1362 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1363 	{
1364 		.reg = PMBUS_PIN_OP_WARN_LIMIT,
1365 		.attr = "max",
1366 		.alarm = "alarm",
1367 		.sbit = PB_PIN_OP_WARNING,
1368 	}, {
1369 		.reg = PMBUS_VIRT_READ_PIN_AVG,
1370 		.update = true,
1371 		.attr = "average",
1372 	}, {
1373 		.reg = PMBUS_VIRT_READ_PIN_MIN,
1374 		.update = true,
1375 		.attr = "input_lowest",
1376 	}, {
1377 		.reg = PMBUS_VIRT_READ_PIN_MAX,
1378 		.update = true,
1379 		.attr = "input_highest",
1380 	}, {
1381 		.reg = PMBUS_VIRT_RESET_PIN_HISTORY,
1382 		.attr = "reset_history",
1383 	}
1384 };
1385 
1386 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1387 	{
1388 		.reg = PMBUS_POUT_MAX,
1389 		.attr = "cap",
1390 		.alarm = "cap_alarm",
1391 		.sbit = PB_POWER_LIMITING,
1392 	}, {
1393 		.reg = PMBUS_POUT_OP_WARN_LIMIT,
1394 		.attr = "max",
1395 		.alarm = "max_alarm",
1396 		.sbit = PB_POUT_OP_WARNING,
1397 	}, {
1398 		.reg = PMBUS_POUT_OP_FAULT_LIMIT,
1399 		.attr = "crit",
1400 		.alarm = "crit_alarm",
1401 		.sbit = PB_POUT_OP_FAULT,
1402 	}, {
1403 		.reg = PMBUS_VIRT_READ_POUT_AVG,
1404 		.update = true,
1405 		.attr = "average",
1406 	}, {
1407 		.reg = PMBUS_VIRT_READ_POUT_MIN,
1408 		.update = true,
1409 		.attr = "input_lowest",
1410 	}, {
1411 		.reg = PMBUS_VIRT_READ_POUT_MAX,
1412 		.update = true,
1413 		.attr = "input_highest",
1414 	}, {
1415 		.reg = PMBUS_VIRT_RESET_POUT_HISTORY,
1416 		.attr = "reset_history",
1417 	}
1418 };
1419 
1420 static const struct pmbus_sensor_attr power_attributes[] = {
1421 	{
1422 		.reg = PMBUS_READ_PIN,
1423 		.class = PSC_POWER,
1424 		.label = "pin",
1425 		.func = PMBUS_HAVE_PIN,
1426 		.sfunc = PMBUS_HAVE_STATUS_INPUT,
1427 		.sbase = PB_STATUS_INPUT_BASE,
1428 		.gbit = PB_STATUS_INPUT,
1429 		.limit = pin_limit_attrs,
1430 		.nlimit = ARRAY_SIZE(pin_limit_attrs),
1431 	}, {
1432 		.reg = PMBUS_READ_POUT,
1433 		.class = PSC_POWER,
1434 		.label = "pout",
1435 		.paged = true,
1436 		.func = PMBUS_HAVE_POUT,
1437 		.sfunc = PMBUS_HAVE_STATUS_IOUT,
1438 		.sbase = PB_STATUS_IOUT_BASE,
1439 		.limit = pout_limit_attrs,
1440 		.nlimit = ARRAY_SIZE(pout_limit_attrs),
1441 	}
1442 };
1443 
1444 /* Temperature atributes */
1445 
1446 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1447 	{
1448 		.reg = PMBUS_UT_WARN_LIMIT,
1449 		.low = true,
1450 		.attr = "min",
1451 		.alarm = "min_alarm",
1452 		.sbit = PB_TEMP_UT_WARNING,
1453 	}, {
1454 		.reg = PMBUS_UT_FAULT_LIMIT,
1455 		.low = true,
1456 		.attr = "lcrit",
1457 		.alarm = "lcrit_alarm",
1458 		.sbit = PB_TEMP_UT_FAULT,
1459 	}, {
1460 		.reg = PMBUS_OT_WARN_LIMIT,
1461 		.attr = "max",
1462 		.alarm = "max_alarm",
1463 		.sbit = PB_TEMP_OT_WARNING,
1464 	}, {
1465 		.reg = PMBUS_OT_FAULT_LIMIT,
1466 		.attr = "crit",
1467 		.alarm = "crit_alarm",
1468 		.sbit = PB_TEMP_OT_FAULT,
1469 	}, {
1470 		.reg = PMBUS_VIRT_READ_TEMP_MIN,
1471 		.attr = "lowest",
1472 	}, {
1473 		.reg = PMBUS_VIRT_READ_TEMP_AVG,
1474 		.attr = "average",
1475 	}, {
1476 		.reg = PMBUS_VIRT_READ_TEMP_MAX,
1477 		.attr = "highest",
1478 	}, {
1479 		.reg = PMBUS_VIRT_RESET_TEMP_HISTORY,
1480 		.attr = "reset_history",
1481 	}
1482 };
1483 
1484 static const struct pmbus_limit_attr temp_limit_attrs2[] = {
1485 	{
1486 		.reg = PMBUS_UT_WARN_LIMIT,
1487 		.low = true,
1488 		.attr = "min",
1489 		.alarm = "min_alarm",
1490 		.sbit = PB_TEMP_UT_WARNING,
1491 	}, {
1492 		.reg = PMBUS_UT_FAULT_LIMIT,
1493 		.low = true,
1494 		.attr = "lcrit",
1495 		.alarm = "lcrit_alarm",
1496 		.sbit = PB_TEMP_UT_FAULT,
1497 	}, {
1498 		.reg = PMBUS_OT_WARN_LIMIT,
1499 		.attr = "max",
1500 		.alarm = "max_alarm",
1501 		.sbit = PB_TEMP_OT_WARNING,
1502 	}, {
1503 		.reg = PMBUS_OT_FAULT_LIMIT,
1504 		.attr = "crit",
1505 		.alarm = "crit_alarm",
1506 		.sbit = PB_TEMP_OT_FAULT,
1507 	}, {
1508 		.reg = PMBUS_VIRT_READ_TEMP2_MIN,
1509 		.attr = "lowest",
1510 	}, {
1511 		.reg = PMBUS_VIRT_READ_TEMP2_AVG,
1512 		.attr = "average",
1513 	}, {
1514 		.reg = PMBUS_VIRT_READ_TEMP2_MAX,
1515 		.attr = "highest",
1516 	}, {
1517 		.reg = PMBUS_VIRT_RESET_TEMP2_HISTORY,
1518 		.attr = "reset_history",
1519 	}
1520 };
1521 
1522 static const struct pmbus_limit_attr temp_limit_attrs3[] = {
1523 	{
1524 		.reg = PMBUS_UT_WARN_LIMIT,
1525 		.low = true,
1526 		.attr = "min",
1527 		.alarm = "min_alarm",
1528 		.sbit = PB_TEMP_UT_WARNING,
1529 	}, {
1530 		.reg = PMBUS_UT_FAULT_LIMIT,
1531 		.low = true,
1532 		.attr = "lcrit",
1533 		.alarm = "lcrit_alarm",
1534 		.sbit = PB_TEMP_UT_FAULT,
1535 	}, {
1536 		.reg = PMBUS_OT_WARN_LIMIT,
1537 		.attr = "max",
1538 		.alarm = "max_alarm",
1539 		.sbit = PB_TEMP_OT_WARNING,
1540 	}, {
1541 		.reg = PMBUS_OT_FAULT_LIMIT,
1542 		.attr = "crit",
1543 		.alarm = "crit_alarm",
1544 		.sbit = PB_TEMP_OT_FAULT,
1545 	}
1546 };
1547 
1548 static const struct pmbus_sensor_attr temp_attributes[] = {
1549 	{
1550 		.reg = PMBUS_READ_TEMPERATURE_1,
1551 		.class = PSC_TEMPERATURE,
1552 		.paged = true,
1553 		.update = true,
1554 		.compare = true,
1555 		.func = PMBUS_HAVE_TEMP,
1556 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1557 		.sbase = PB_STATUS_TEMP_BASE,
1558 		.gbit = PB_STATUS_TEMPERATURE,
1559 		.limit = temp_limit_attrs,
1560 		.nlimit = ARRAY_SIZE(temp_limit_attrs),
1561 	}, {
1562 		.reg = PMBUS_READ_TEMPERATURE_2,
1563 		.class = PSC_TEMPERATURE,
1564 		.paged = true,
1565 		.update = true,
1566 		.compare = true,
1567 		.func = PMBUS_HAVE_TEMP2,
1568 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1569 		.sbase = PB_STATUS_TEMP_BASE,
1570 		.gbit = PB_STATUS_TEMPERATURE,
1571 		.limit = temp_limit_attrs2,
1572 		.nlimit = ARRAY_SIZE(temp_limit_attrs2),
1573 	}, {
1574 		.reg = PMBUS_READ_TEMPERATURE_3,
1575 		.class = PSC_TEMPERATURE,
1576 		.paged = true,
1577 		.update = true,
1578 		.compare = true,
1579 		.func = PMBUS_HAVE_TEMP3,
1580 		.sfunc = PMBUS_HAVE_STATUS_TEMP,
1581 		.sbase = PB_STATUS_TEMP_BASE,
1582 		.gbit = PB_STATUS_TEMPERATURE,
1583 		.limit = temp_limit_attrs3,
1584 		.nlimit = ARRAY_SIZE(temp_limit_attrs3),
1585 	}
1586 };
1587 
1588 static const int pmbus_fan_registers[] = {
1589 	PMBUS_READ_FAN_SPEED_1,
1590 	PMBUS_READ_FAN_SPEED_2,
1591 	PMBUS_READ_FAN_SPEED_3,
1592 	PMBUS_READ_FAN_SPEED_4
1593 };
1594 
1595 static const int pmbus_fan_config_registers[] = {
1596 	PMBUS_FAN_CONFIG_12,
1597 	PMBUS_FAN_CONFIG_12,
1598 	PMBUS_FAN_CONFIG_34,
1599 	PMBUS_FAN_CONFIG_34
1600 };
1601 
1602 static const int pmbus_fan_status_registers[] = {
1603 	PMBUS_STATUS_FAN_12,
1604 	PMBUS_STATUS_FAN_12,
1605 	PMBUS_STATUS_FAN_34,
1606 	PMBUS_STATUS_FAN_34
1607 };
1608 
1609 static const u32 pmbus_fan_flags[] = {
1610 	PMBUS_HAVE_FAN12,
1611 	PMBUS_HAVE_FAN12,
1612 	PMBUS_HAVE_FAN34,
1613 	PMBUS_HAVE_FAN34
1614 };
1615 
1616 static const u32 pmbus_fan_status_flags[] = {
1617 	PMBUS_HAVE_STATUS_FAN12,
1618 	PMBUS_HAVE_STATUS_FAN12,
1619 	PMBUS_HAVE_STATUS_FAN34,
1620 	PMBUS_HAVE_STATUS_FAN34
1621 };
1622 
1623 /* Fans */
1624 static int pmbus_add_fan_attributes(struct i2c_client *client,
1625 				    struct pmbus_data *data)
1626 {
1627 	const struct pmbus_driver_info *info = data->info;
1628 	int index = 1;
1629 	int page;
1630 	int ret;
1631 
1632 	for (page = 0; page < info->pages; page++) {
1633 		int f;
1634 
1635 		for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1636 			int regval;
1637 
1638 			if (!(info->func[page] & pmbus_fan_flags[f]))
1639 				break;
1640 
1641 			if (!pmbus_check_word_register(client, page,
1642 						       pmbus_fan_registers[f]))
1643 				break;
1644 
1645 			/*
1646 			 * Skip fan if not installed.
1647 			 * Each fan configuration register covers multiple fans,
1648 			 * so we have to do some magic.
1649 			 */
1650 			regval = _pmbus_read_byte_data(client, page,
1651 				pmbus_fan_config_registers[f]);
1652 			if (regval < 0 ||
1653 			    (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1654 				continue;
1655 
1656 			if (pmbus_add_sensor(data, "fan", "input", index,
1657 					     page, pmbus_fan_registers[f],
1658 					     PSC_FAN, true, true) == NULL)
1659 				return -ENOMEM;
1660 
1661 			/*
1662 			 * Each fan status register covers multiple fans,
1663 			 * so we have to do some magic.
1664 			 */
1665 			if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1666 			    pmbus_check_byte_register(client,
1667 					page, pmbus_fan_status_registers[f])) {
1668 				int base;
1669 
1670 				if (f > 1)	/* fan 3, 4 */
1671 					base = PB_STATUS_FAN34_BASE + page;
1672 				else
1673 					base = PB_STATUS_FAN_BASE + page;
1674 				ret = pmbus_add_boolean(data, "fan",
1675 					"alarm", index, NULL, NULL, base,
1676 					PB_FAN_FAN1_WARNING >> (f & 1));
1677 				if (ret)
1678 					return ret;
1679 				ret = pmbus_add_boolean(data, "fan",
1680 					"fault", index, NULL, NULL, base,
1681 					PB_FAN_FAN1_FAULT >> (f & 1));
1682 				if (ret)
1683 					return ret;
1684 			}
1685 			index++;
1686 		}
1687 	}
1688 	return 0;
1689 }
1690 
1691 static int pmbus_find_attributes(struct i2c_client *client,
1692 				 struct pmbus_data *data)
1693 {
1694 	int ret;
1695 
1696 	/* Voltage sensors */
1697 	ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
1698 				     ARRAY_SIZE(voltage_attributes));
1699 	if (ret)
1700 		return ret;
1701 
1702 	/* Current sensors */
1703 	ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
1704 				     ARRAY_SIZE(current_attributes));
1705 	if (ret)
1706 		return ret;
1707 
1708 	/* Power sensors */
1709 	ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes,
1710 				     ARRAY_SIZE(power_attributes));
1711 	if (ret)
1712 		return ret;
1713 
1714 	/* Temperature sensors */
1715 	ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
1716 				     ARRAY_SIZE(temp_attributes));
1717 	if (ret)
1718 		return ret;
1719 
1720 	/* Fans */
1721 	ret = pmbus_add_fan_attributes(client, data);
1722 	return ret;
1723 }
1724 
1725 /*
1726  * Identify chip parameters.
1727  * This function is called for all chips.
1728  */
1729 static int pmbus_identify_common(struct i2c_client *client,
1730 				 struct pmbus_data *data, int page)
1731 {
1732 	int vout_mode = -1;
1733 
1734 	if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE))
1735 		vout_mode = _pmbus_read_byte_data(client, page,
1736 						  PMBUS_VOUT_MODE);
1737 	if (vout_mode >= 0 && vout_mode != 0xff) {
1738 		/*
1739 		 * Not all chips support the VOUT_MODE command,
1740 		 * so a failure to read it is not an error.
1741 		 */
1742 		switch (vout_mode >> 5) {
1743 		case 0:	/* linear mode      */
1744 			if (data->info->format[PSC_VOLTAGE_OUT] != linear)
1745 				return -ENODEV;
1746 
1747 			data->exponent[page] = ((s8)(vout_mode << 3)) >> 3;
1748 			break;
1749 		case 1: /* VID mode         */
1750 			if (data->info->format[PSC_VOLTAGE_OUT] != vid)
1751 				return -ENODEV;
1752 			break;
1753 		case 2:	/* direct mode      */
1754 			if (data->info->format[PSC_VOLTAGE_OUT] != direct)
1755 				return -ENODEV;
1756 			break;
1757 		default:
1758 			return -ENODEV;
1759 		}
1760 	}
1761 
1762 	pmbus_clear_fault_page(client, page);
1763 	return 0;
1764 }
1765 
1766 static int pmbus_read_status_byte(struct i2c_client *client, int page)
1767 {
1768 	return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
1769 }
1770 
1771 static int pmbus_read_status_word(struct i2c_client *client, int page)
1772 {
1773 	return _pmbus_read_word_data(client, page, PMBUS_STATUS_WORD);
1774 }
1775 
1776 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
1777 			     struct pmbus_driver_info *info)
1778 {
1779 	struct device *dev = &client->dev;
1780 	int page, ret;
1781 
1782 	/*
1783 	 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
1784 	 * to use PMBUS_STATUS_BYTE instead if that is the case.
1785 	 * Bail out if both registers are not supported.
1786 	 */
1787 	data->read_status = pmbus_read_status_word;
1788 	ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD);
1789 	if (ret < 0 || ret == 0xffff) {
1790 		data->read_status = pmbus_read_status_byte;
1791 		ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE);
1792 		if (ret < 0 || ret == 0xff) {
1793 			dev_err(dev, "PMBus status register not found\n");
1794 			return -ENODEV;
1795 		}
1796 	} else {
1797 		data->has_status_word = true;
1798 	}
1799 
1800 	/* Enable PEC if the controller supports it */
1801 	ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
1802 	if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK))
1803 		client->flags |= I2C_CLIENT_PEC;
1804 
1805 	pmbus_clear_faults(client);
1806 
1807 	if (info->identify) {
1808 		ret = (*info->identify)(client, info);
1809 		if (ret < 0) {
1810 			dev_err(dev, "Chip identification failed\n");
1811 			return ret;
1812 		}
1813 	}
1814 
1815 	if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
1816 		dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages);
1817 		return -ENODEV;
1818 	}
1819 
1820 	for (page = 0; page < info->pages; page++) {
1821 		ret = pmbus_identify_common(client, data, page);
1822 		if (ret < 0) {
1823 			dev_err(dev, "Failed to identify chip capabilities\n");
1824 			return ret;
1825 		}
1826 	}
1827 	return 0;
1828 }
1829 
1830 #if IS_ENABLED(CONFIG_REGULATOR)
1831 static int pmbus_regulator_is_enabled(struct regulator_dev *rdev)
1832 {
1833 	struct device *dev = rdev_get_dev(rdev);
1834 	struct i2c_client *client = to_i2c_client(dev->parent);
1835 	u8 page = rdev_get_id(rdev);
1836 	int ret;
1837 
1838 	ret = pmbus_read_byte_data(client, page, PMBUS_OPERATION);
1839 	if (ret < 0)
1840 		return ret;
1841 
1842 	return !!(ret & PB_OPERATION_CONTROL_ON);
1843 }
1844 
1845 static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable)
1846 {
1847 	struct device *dev = rdev_get_dev(rdev);
1848 	struct i2c_client *client = to_i2c_client(dev->parent);
1849 	u8 page = rdev_get_id(rdev);
1850 
1851 	return pmbus_update_byte_data(client, page, PMBUS_OPERATION,
1852 				      PB_OPERATION_CONTROL_ON,
1853 				      enable ? PB_OPERATION_CONTROL_ON : 0);
1854 }
1855 
1856 static int pmbus_regulator_enable(struct regulator_dev *rdev)
1857 {
1858 	return _pmbus_regulator_on_off(rdev, 1);
1859 }
1860 
1861 static int pmbus_regulator_disable(struct regulator_dev *rdev)
1862 {
1863 	return _pmbus_regulator_on_off(rdev, 0);
1864 }
1865 
1866 const struct regulator_ops pmbus_regulator_ops = {
1867 	.enable = pmbus_regulator_enable,
1868 	.disable = pmbus_regulator_disable,
1869 	.is_enabled = pmbus_regulator_is_enabled,
1870 };
1871 EXPORT_SYMBOL_GPL(pmbus_regulator_ops);
1872 
1873 static int pmbus_regulator_register(struct pmbus_data *data)
1874 {
1875 	struct device *dev = data->dev;
1876 	const struct pmbus_driver_info *info = data->info;
1877 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
1878 	struct regulator_dev *rdev;
1879 	int i;
1880 
1881 	for (i = 0; i < info->num_regulators; i++) {
1882 		struct regulator_config config = { };
1883 
1884 		config.dev = dev;
1885 		config.driver_data = data;
1886 
1887 		if (pdata && pdata->reg_init_data)
1888 			config.init_data = &pdata->reg_init_data[i];
1889 
1890 		rdev = devm_regulator_register(dev, &info->reg_desc[i],
1891 					       &config);
1892 		if (IS_ERR(rdev)) {
1893 			dev_err(dev, "Failed to register %s regulator\n",
1894 				info->reg_desc[i].name);
1895 			return PTR_ERR(rdev);
1896 		}
1897 	}
1898 
1899 	return 0;
1900 }
1901 #else
1902 static int pmbus_regulator_register(struct pmbus_data *data)
1903 {
1904 	return 0;
1905 }
1906 #endif
1907 
1908 static struct dentry *pmbus_debugfs_dir;	/* pmbus debugfs directory */
1909 
1910 #if IS_ENABLED(CONFIG_DEBUG_FS)
1911 static int pmbus_debugfs_get(void *data, u64 *val)
1912 {
1913 	int rc;
1914 	struct pmbus_debugfs_entry *entry = data;
1915 
1916 	rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg);
1917 	if (rc < 0)
1918 		return rc;
1919 
1920 	*val = rc;
1921 
1922 	return 0;
1923 }
1924 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL,
1925 			 "0x%02llx\n");
1926 
1927 static int pmbus_debugfs_get_status(void *data, u64 *val)
1928 {
1929 	int rc;
1930 	struct pmbus_debugfs_entry *entry = data;
1931 	struct pmbus_data *pdata = i2c_get_clientdata(entry->client);
1932 
1933 	rc = pdata->read_status(entry->client, entry->page);
1934 	if (rc < 0)
1935 		return rc;
1936 
1937 	*val = rc;
1938 
1939 	return 0;
1940 }
1941 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status,
1942 			 NULL, "0x%04llx\n");
1943 
1944 static int pmbus_init_debugfs(struct i2c_client *client,
1945 			      struct pmbus_data *data)
1946 {
1947 	int i, idx = 0;
1948 	char name[PMBUS_NAME_SIZE];
1949 	struct pmbus_debugfs_entry *entries;
1950 
1951 	if (!pmbus_debugfs_dir)
1952 		return -ENODEV;
1953 
1954 	/*
1955 	 * Create the debugfs directory for this device. Use the hwmon device
1956 	 * name to avoid conflicts (hwmon numbers are globally unique).
1957 	 */
1958 	data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev),
1959 					   pmbus_debugfs_dir);
1960 	if (IS_ERR_OR_NULL(data->debugfs)) {
1961 		data->debugfs = NULL;
1962 		return -ENODEV;
1963 	}
1964 
1965 	/* Allocate the max possible entries we need. */
1966 	entries = devm_kzalloc(data->dev,
1967 			       sizeof(*entries) * (data->info->pages * 10),
1968 			       GFP_KERNEL);
1969 	if (!entries)
1970 		return -ENOMEM;
1971 
1972 	for (i = 0; i < data->info->pages; ++i) {
1973 		/* Check accessibility of status register if it's not page 0 */
1974 		if (!i || pmbus_check_status_register(client, i)) {
1975 			/* No need to set reg as we have special read op. */
1976 			entries[idx].client = client;
1977 			entries[idx].page = i;
1978 			scnprintf(name, PMBUS_NAME_SIZE, "status%d", i);
1979 			debugfs_create_file(name, 0444, data->debugfs,
1980 					    &entries[idx++],
1981 					    &pmbus_debugfs_ops_status);
1982 		}
1983 
1984 		if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) {
1985 			entries[idx].client = client;
1986 			entries[idx].page = i;
1987 			entries[idx].reg = PMBUS_STATUS_VOUT;
1988 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i);
1989 			debugfs_create_file(name, 0444, data->debugfs,
1990 					    &entries[idx++],
1991 					    &pmbus_debugfs_ops);
1992 		}
1993 
1994 		if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) {
1995 			entries[idx].client = client;
1996 			entries[idx].page = i;
1997 			entries[idx].reg = PMBUS_STATUS_IOUT;
1998 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i);
1999 			debugfs_create_file(name, 0444, data->debugfs,
2000 					    &entries[idx++],
2001 					    &pmbus_debugfs_ops);
2002 		}
2003 
2004 		if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) {
2005 			entries[idx].client = client;
2006 			entries[idx].page = i;
2007 			entries[idx].reg = PMBUS_STATUS_INPUT;
2008 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i);
2009 			debugfs_create_file(name, 0444, data->debugfs,
2010 					    &entries[idx++],
2011 					    &pmbus_debugfs_ops);
2012 		}
2013 
2014 		if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) {
2015 			entries[idx].client = client;
2016 			entries[idx].page = i;
2017 			entries[idx].reg = PMBUS_STATUS_TEMPERATURE;
2018 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i);
2019 			debugfs_create_file(name, 0444, data->debugfs,
2020 					    &entries[idx++],
2021 					    &pmbus_debugfs_ops);
2022 		}
2023 
2024 		if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) {
2025 			entries[idx].client = client;
2026 			entries[idx].page = i;
2027 			entries[idx].reg = PMBUS_STATUS_CML;
2028 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i);
2029 			debugfs_create_file(name, 0444, data->debugfs,
2030 					    &entries[idx++],
2031 					    &pmbus_debugfs_ops);
2032 		}
2033 
2034 		if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) {
2035 			entries[idx].client = client;
2036 			entries[idx].page = i;
2037 			entries[idx].reg = PMBUS_STATUS_OTHER;
2038 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i);
2039 			debugfs_create_file(name, 0444, data->debugfs,
2040 					    &entries[idx++],
2041 					    &pmbus_debugfs_ops);
2042 		}
2043 
2044 		if (pmbus_check_byte_register(client, i,
2045 					      PMBUS_STATUS_MFR_SPECIFIC)) {
2046 			entries[idx].client = client;
2047 			entries[idx].page = i;
2048 			entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC;
2049 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i);
2050 			debugfs_create_file(name, 0444, data->debugfs,
2051 					    &entries[idx++],
2052 					    &pmbus_debugfs_ops);
2053 		}
2054 
2055 		if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) {
2056 			entries[idx].client = client;
2057 			entries[idx].page = i;
2058 			entries[idx].reg = PMBUS_STATUS_FAN_12;
2059 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i);
2060 			debugfs_create_file(name, 0444, data->debugfs,
2061 					    &entries[idx++],
2062 					    &pmbus_debugfs_ops);
2063 		}
2064 
2065 		if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) {
2066 			entries[idx].client = client;
2067 			entries[idx].page = i;
2068 			entries[idx].reg = PMBUS_STATUS_FAN_34;
2069 			scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i);
2070 			debugfs_create_file(name, 0444, data->debugfs,
2071 					    &entries[idx++],
2072 					    &pmbus_debugfs_ops);
2073 		}
2074 	}
2075 
2076 	return 0;
2077 }
2078 #else
2079 static int pmbus_init_debugfs(struct i2c_client *client,
2080 			      struct pmbus_data *data)
2081 {
2082 	return 0;
2083 }
2084 #endif	/* IS_ENABLED(CONFIG_DEBUG_FS) */
2085 
2086 int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
2087 		   struct pmbus_driver_info *info)
2088 {
2089 	struct device *dev = &client->dev;
2090 	const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
2091 	struct pmbus_data *data;
2092 	int ret;
2093 
2094 	if (!info)
2095 		return -ENODEV;
2096 
2097 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
2098 				     | I2C_FUNC_SMBUS_BYTE_DATA
2099 				     | I2C_FUNC_SMBUS_WORD_DATA))
2100 		return -ENODEV;
2101 
2102 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
2103 	if (!data)
2104 		return -ENOMEM;
2105 
2106 	i2c_set_clientdata(client, data);
2107 	mutex_init(&data->update_lock);
2108 	data->dev = dev;
2109 
2110 	if (pdata)
2111 		data->flags = pdata->flags;
2112 	data->info = info;
2113 
2114 	ret = pmbus_init_common(client, data, info);
2115 	if (ret < 0)
2116 		return ret;
2117 
2118 	ret = pmbus_find_attributes(client, data);
2119 	if (ret)
2120 		goto out_kfree;
2121 
2122 	/*
2123 	 * If there are no attributes, something is wrong.
2124 	 * Bail out instead of trying to register nothing.
2125 	 */
2126 	if (!data->num_attributes) {
2127 		dev_err(dev, "No attributes found\n");
2128 		ret = -ENODEV;
2129 		goto out_kfree;
2130 	}
2131 
2132 	data->groups[0] = &data->group;
2133 	data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
2134 							    data, data->groups);
2135 	if (IS_ERR(data->hwmon_dev)) {
2136 		ret = PTR_ERR(data->hwmon_dev);
2137 		dev_err(dev, "Failed to register hwmon device\n");
2138 		goto out_kfree;
2139 	}
2140 
2141 	ret = pmbus_regulator_register(data);
2142 	if (ret)
2143 		goto out_unregister;
2144 
2145 	ret = pmbus_init_debugfs(client, data);
2146 	if (ret)
2147 		dev_warn(dev, "Failed to register debugfs\n");
2148 
2149 	return 0;
2150 
2151 out_unregister:
2152 	hwmon_device_unregister(data->hwmon_dev);
2153 out_kfree:
2154 	kfree(data->group.attrs);
2155 	return ret;
2156 }
2157 EXPORT_SYMBOL_GPL(pmbus_do_probe);
2158 
2159 int pmbus_do_remove(struct i2c_client *client)
2160 {
2161 	struct pmbus_data *data = i2c_get_clientdata(client);
2162 
2163 	debugfs_remove_recursive(data->debugfs);
2164 
2165 	hwmon_device_unregister(data->hwmon_dev);
2166 	kfree(data->group.attrs);
2167 	return 0;
2168 }
2169 EXPORT_SYMBOL_GPL(pmbus_do_remove);
2170 
2171 static int __init pmbus_core_init(void)
2172 {
2173 	pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL);
2174 	if (IS_ERR(pmbus_debugfs_dir))
2175 		pmbus_debugfs_dir = NULL;
2176 
2177 	return 0;
2178 }
2179 
2180 static void __exit pmbus_core_exit(void)
2181 {
2182 	debugfs_remove_recursive(pmbus_debugfs_dir);
2183 }
2184 
2185 module_init(pmbus_core_init);
2186 module_exit(pmbus_core_exit);
2187 
2188 MODULE_AUTHOR("Guenter Roeck");
2189 MODULE_DESCRIPTION("PMBus core driver");
2190 MODULE_LICENSE("GPL");
2191