1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hardware monitoring driver for PMBus devices
5 * Copyright (c) 2010, 2011 Ericsson AB.
6 * Copyright (c) 2012 Guenter Roeck
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>
25 * Number of additional attribute pointers to allocate
26 * with each call to krealloc
28 #define PMBUS_ATTR_ALLOC_SIZE 32
29 #define PMBUS_NAME_SIZE 24
32 struct pmbus_sensor
*next
;
33 char name
[PMBUS_NAME_SIZE
]; /* sysfs sensor name */
34 struct device_attribute attribute
;
35 u8 page
; /* page number */
36 u8 phase
; /* phase number, 0xff for all phases */
37 u16 reg
; /* register */
38 enum pmbus_sensor_classes
class; /* sensor class */
39 bool update
; /* runtime sensor update needed */
40 bool convert
; /* Whether or not to apply linear/vid/direct */
41 int data
; /* Sensor data.
42 Negative if there was a read error */
44 #define to_pmbus_sensor(_attr) \
45 container_of(_attr, struct pmbus_sensor, attribute)
47 struct pmbus_boolean
{
48 char name
[PMBUS_NAME_SIZE
]; /* sysfs boolean name */
49 struct sensor_device_attribute attribute
;
50 struct pmbus_sensor
*s1
;
51 struct pmbus_sensor
*s2
;
53 #define to_pmbus_boolean(_attr) \
54 container_of(_attr, struct pmbus_boolean, attribute)
57 char name
[PMBUS_NAME_SIZE
]; /* sysfs label name */
58 struct device_attribute attribute
;
59 char label
[PMBUS_NAME_SIZE
]; /* label */
61 #define to_pmbus_label(_attr) \
62 container_of(_attr, struct pmbus_label, attribute)
64 /* Macros for converting between sensor index and register/page/status mask */
66 #define PB_STATUS_MASK 0xffff
67 #define PB_REG_SHIFT 16
68 #define PB_REG_MASK 0x3ff
69 #define PB_PAGE_SHIFT 26
70 #define PB_PAGE_MASK 0x3f
72 #define pb_reg_to_index(page, reg, mask) (((page) << PB_PAGE_SHIFT) | \
73 ((reg) << PB_REG_SHIFT) | (mask))
75 #define pb_index_to_page(index) (((index) >> PB_PAGE_SHIFT) & PB_PAGE_MASK)
76 #define pb_index_to_reg(index) (((index) >> PB_REG_SHIFT) & PB_REG_MASK)
77 #define pb_index_to_mask(index) ((index) & PB_STATUS_MASK)
81 struct device
*hwmon_dev
;
83 u32 flags
; /* from platform data */
85 int exponent
[PMBUS_PAGES
];
86 /* linear mode: exponent for output voltages */
88 const struct pmbus_driver_info
*info
;
92 struct attribute_group group
;
93 const struct attribute_group
**groups
;
94 struct dentry
*debugfs
; /* debugfs device directory */
96 struct pmbus_sensor
*sensors
;
98 struct mutex update_lock
;
100 bool has_status_word
; /* device uses STATUS_WORD register */
101 int (*read_status
)(struct i2c_client
*client
, int page
);
103 s16 currpage
; /* current page, -1 for unknown/unset */
104 s16 currphase
; /* current phase, 0xff for all, -1 for unknown/unset */
107 struct pmbus_debugfs_entry
{
108 struct i2c_client
*client
;
113 static const int pmbus_fan_rpm_mask
[] = {
120 static const int pmbus_fan_config_registers
[] = {
127 static const int pmbus_fan_command_registers
[] = {
134 void pmbus_clear_cache(struct i2c_client
*client
)
136 struct pmbus_data
*data
= i2c_get_clientdata(client
);
137 struct pmbus_sensor
*sensor
;
139 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
)
140 sensor
->data
= -ENODATA
;
142 EXPORT_SYMBOL_GPL(pmbus_clear_cache
);
144 int pmbus_set_page(struct i2c_client
*client
, int page
, int phase
)
146 struct pmbus_data
*data
= i2c_get_clientdata(client
);
152 if (!(data
->info
->func
[page
] & PMBUS_PAGE_VIRTUAL
) &&
153 data
->info
->pages
> 1 && page
!= data
->currpage
) {
154 rv
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, page
);
158 rv
= i2c_smbus_read_byte_data(client
, PMBUS_PAGE
);
165 data
->currpage
= page
;
167 if (data
->info
->phases
[page
] && data
->currphase
!= phase
&&
168 !(data
->info
->func
[page
] & PMBUS_PHASE_VIRTUAL
)) {
169 rv
= i2c_smbus_write_byte_data(client
, PMBUS_PHASE
,
174 data
->currphase
= phase
;
178 EXPORT_SYMBOL_GPL(pmbus_set_page
);
180 int pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
184 rv
= pmbus_set_page(client
, page
, 0xff);
188 return i2c_smbus_write_byte(client
, value
);
190 EXPORT_SYMBOL_GPL(pmbus_write_byte
);
193 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
194 * a device specific mapping function exists and calls it if necessary.
196 static int _pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
198 struct pmbus_data
*data
= i2c_get_clientdata(client
);
199 const struct pmbus_driver_info
*info
= data
->info
;
202 if (info
->write_byte
) {
203 status
= info
->write_byte(client
, page
, value
);
204 if (status
!= -ENODATA
)
207 return pmbus_write_byte(client
, page
, value
);
210 int pmbus_write_word_data(struct i2c_client
*client
, int page
, u8 reg
,
215 rv
= pmbus_set_page(client
, page
, 0xff);
219 return i2c_smbus_write_word_data(client
, reg
, word
);
221 EXPORT_SYMBOL_GPL(pmbus_write_word_data
);
224 static int pmbus_write_virt_reg(struct i2c_client
*client
, int page
, int reg
,
232 case PMBUS_VIRT_FAN_TARGET_1
... PMBUS_VIRT_FAN_TARGET_4
:
233 id
= reg
- PMBUS_VIRT_FAN_TARGET_1
;
234 bit
= pmbus_fan_rpm_mask
[id
];
235 rv
= pmbus_update_fan(client
, page
, id
, bit
, bit
, word
);
246 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
247 * a device specific mapping function exists and calls it if necessary.
249 static int _pmbus_write_word_data(struct i2c_client
*client
, int page
, int reg
,
252 struct pmbus_data
*data
= i2c_get_clientdata(client
);
253 const struct pmbus_driver_info
*info
= data
->info
;
256 if (info
->write_word_data
) {
257 status
= info
->write_word_data(client
, page
, reg
, word
);
258 if (status
!= -ENODATA
)
262 if (reg
>= PMBUS_VIRT_BASE
)
263 return pmbus_write_virt_reg(client
, page
, reg
, word
);
265 return pmbus_write_word_data(client
, page
, reg
, word
);
268 int pmbus_update_fan(struct i2c_client
*client
, int page
, int id
,
269 u8 config
, u8 mask
, u16 command
)
275 from
= pmbus_read_byte_data(client
, page
,
276 pmbus_fan_config_registers
[id
]);
280 to
= (from
& ~mask
) | (config
& mask
);
282 rv
= pmbus_write_byte_data(client
, page
,
283 pmbus_fan_config_registers
[id
], to
);
288 return _pmbus_write_word_data(client
, page
,
289 pmbus_fan_command_registers
[id
], command
);
291 EXPORT_SYMBOL_GPL(pmbus_update_fan
);
293 int pmbus_read_word_data(struct i2c_client
*client
, int page
, int phase
, u8 reg
)
297 rv
= pmbus_set_page(client
, page
, phase
);
301 return i2c_smbus_read_word_data(client
, reg
);
303 EXPORT_SYMBOL_GPL(pmbus_read_word_data
);
305 static int pmbus_read_virt_reg(struct i2c_client
*client
, int page
, int reg
)
311 case PMBUS_VIRT_FAN_TARGET_1
... PMBUS_VIRT_FAN_TARGET_4
:
312 id
= reg
- PMBUS_VIRT_FAN_TARGET_1
;
313 rv
= pmbus_get_fan_rate_device(client
, page
, id
, rpm
);
324 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
325 * a device specific mapping function exists and calls it if necessary.
327 static int _pmbus_read_word_data(struct i2c_client
*client
, int page
,
330 struct pmbus_data
*data
= i2c_get_clientdata(client
);
331 const struct pmbus_driver_info
*info
= data
->info
;
334 if (info
->read_word_data
) {
335 status
= info
->read_word_data(client
, page
, phase
, reg
);
336 if (status
!= -ENODATA
)
340 if (reg
>= PMBUS_VIRT_BASE
)
341 return pmbus_read_virt_reg(client
, page
, reg
);
343 return pmbus_read_word_data(client
, page
, phase
, reg
);
346 /* Same as above, but without phase parameter, for use in check functions */
347 static int __pmbus_read_word_data(struct i2c_client
*client
, int page
, int reg
)
349 return _pmbus_read_word_data(client
, page
, 0xff, reg
);
352 int pmbus_read_byte_data(struct i2c_client
*client
, int page
, u8 reg
)
356 rv
= pmbus_set_page(client
, page
, 0xff);
360 return i2c_smbus_read_byte_data(client
, reg
);
362 EXPORT_SYMBOL_GPL(pmbus_read_byte_data
);
364 int pmbus_write_byte_data(struct i2c_client
*client
, int page
, u8 reg
, u8 value
)
368 rv
= pmbus_set_page(client
, page
, 0xff);
372 return i2c_smbus_write_byte_data(client
, reg
, value
);
374 EXPORT_SYMBOL_GPL(pmbus_write_byte_data
);
376 int pmbus_update_byte_data(struct i2c_client
*client
, int page
, u8 reg
,
382 rv
= pmbus_read_byte_data(client
, page
, reg
);
386 tmp
= (rv
& ~mask
) | (value
& mask
);
389 rv
= pmbus_write_byte_data(client
, page
, reg
, tmp
);
393 EXPORT_SYMBOL_GPL(pmbus_update_byte_data
);
396 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
397 * a device specific mapping function exists and calls it if necessary.
399 static int _pmbus_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
401 struct pmbus_data
*data
= i2c_get_clientdata(client
);
402 const struct pmbus_driver_info
*info
= data
->info
;
405 if (info
->read_byte_data
) {
406 status
= info
->read_byte_data(client
, page
, reg
);
407 if (status
!= -ENODATA
)
410 return pmbus_read_byte_data(client
, page
, reg
);
413 static struct pmbus_sensor
*pmbus_find_sensor(struct pmbus_data
*data
, int page
,
416 struct pmbus_sensor
*sensor
;
418 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
) {
419 if (sensor
->page
== page
&& sensor
->reg
== reg
)
423 return ERR_PTR(-EINVAL
);
426 static int pmbus_get_fan_rate(struct i2c_client
*client
, int page
, int id
,
427 enum pmbus_fan_mode mode
,
430 struct pmbus_data
*data
= i2c_get_clientdata(client
);
431 bool want_rpm
, have_rpm
;
432 struct pmbus_sensor
*s
;
436 want_rpm
= (mode
== rpm
);
439 reg
= want_rpm
? PMBUS_VIRT_FAN_TARGET_1
: PMBUS_VIRT_PWM_1
;
440 s
= pmbus_find_sensor(data
, page
, reg
+ id
);
447 config
= pmbus_read_byte_data(client
, page
,
448 pmbus_fan_config_registers
[id
]);
452 have_rpm
= !!(config
& pmbus_fan_rpm_mask
[id
]);
453 if (want_rpm
== have_rpm
)
454 return pmbus_read_word_data(client
, page
, 0xff,
455 pmbus_fan_command_registers
[id
]);
457 /* Can't sensibly map between RPM and PWM, just return zero */
461 int pmbus_get_fan_rate_device(struct i2c_client
*client
, int page
, int id
,
462 enum pmbus_fan_mode mode
)
464 return pmbus_get_fan_rate(client
, page
, id
, mode
, false);
466 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_device
);
468 int pmbus_get_fan_rate_cached(struct i2c_client
*client
, int page
, int id
,
469 enum pmbus_fan_mode mode
)
471 return pmbus_get_fan_rate(client
, page
, id
, mode
, true);
473 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_cached
);
475 static void pmbus_clear_fault_page(struct i2c_client
*client
, int page
)
477 _pmbus_write_byte(client
, page
, PMBUS_CLEAR_FAULTS
);
480 void pmbus_clear_faults(struct i2c_client
*client
)
482 struct pmbus_data
*data
= i2c_get_clientdata(client
);
485 for (i
= 0; i
< data
->info
->pages
; i
++)
486 pmbus_clear_fault_page(client
, i
);
488 EXPORT_SYMBOL_GPL(pmbus_clear_faults
);
490 static int pmbus_check_status_cml(struct i2c_client
*client
)
492 struct pmbus_data
*data
= i2c_get_clientdata(client
);
495 status
= data
->read_status(client
, -1);
496 if (status
< 0 || (status
& PB_STATUS_CML
)) {
497 status2
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
498 if (status2
< 0 || (status2
& PB_CML_FAULT_INVALID_COMMAND
))
504 static bool pmbus_check_register(struct i2c_client
*client
,
505 int (*func
)(struct i2c_client
*client
,
510 struct pmbus_data
*data
= i2c_get_clientdata(client
);
512 rv
= func(client
, page
, reg
);
513 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
514 rv
= pmbus_check_status_cml(client
);
515 pmbus_clear_fault_page(client
, -1);
519 static bool pmbus_check_status_register(struct i2c_client
*client
, int page
)
522 struct pmbus_data
*data
= i2c_get_clientdata(client
);
524 status
= data
->read_status(client
, page
);
525 if (status
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
) &&
526 (status
& PB_STATUS_CML
)) {
527 status
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
528 if (status
< 0 || (status
& PB_CML_FAULT_INVALID_COMMAND
))
532 pmbus_clear_fault_page(client
, -1);
536 bool pmbus_check_byte_register(struct i2c_client
*client
, int page
, int reg
)
538 return pmbus_check_register(client
, _pmbus_read_byte_data
, page
, reg
);
540 EXPORT_SYMBOL_GPL(pmbus_check_byte_register
);
542 bool pmbus_check_word_register(struct i2c_client
*client
, int page
, int reg
)
544 return pmbus_check_register(client
, __pmbus_read_word_data
, page
, reg
);
546 EXPORT_SYMBOL_GPL(pmbus_check_word_register
);
548 const struct pmbus_driver_info
*pmbus_get_driver_info(struct i2c_client
*client
)
550 struct pmbus_data
*data
= i2c_get_clientdata(client
);
554 EXPORT_SYMBOL_GPL(pmbus_get_driver_info
);
556 static int pmbus_get_status(struct i2c_client
*client
, int page
, int reg
)
558 struct pmbus_data
*data
= i2c_get_clientdata(client
);
562 case PMBUS_STATUS_WORD
:
563 status
= data
->read_status(client
, page
);
566 status
= _pmbus_read_byte_data(client
, page
, reg
);
570 pmbus_clear_faults(client
);
574 static void pmbus_update_sensor_data(struct i2c_client
*client
, struct pmbus_sensor
*sensor
)
576 if (sensor
->data
< 0 || sensor
->update
)
577 sensor
->data
= _pmbus_read_word_data(client
, sensor
->page
,
578 sensor
->phase
, sensor
->reg
);
582 * Convert linear sensor values to milli- or micro-units
583 * depending on sensor type.
585 static s64
pmbus_reg2data_linear(struct pmbus_data
*data
,
586 struct pmbus_sensor
*sensor
)
592 if (sensor
->class == PSC_VOLTAGE_OUT
) { /* LINEAR16 */
593 exponent
= data
->exponent
[sensor
->page
];
594 mantissa
= (u16
) sensor
->data
;
595 } else { /* LINEAR11 */
596 exponent
= ((s16
)sensor
->data
) >> 11;
597 mantissa
= ((s16
)((sensor
->data
& 0x7ff) << 5)) >> 5;
602 /* scale result to milli-units for all sensors except fans */
603 if (sensor
->class != PSC_FAN
)
606 /* scale result to micro-units for power sensors */
607 if (sensor
->class == PSC_POWER
)
619 * Convert direct sensor values to milli- or micro-units
620 * depending on sensor type.
622 static s64
pmbus_reg2data_direct(struct pmbus_data
*data
,
623 struct pmbus_sensor
*sensor
)
625 s64 b
, val
= (s16
)sensor
->data
;
628 m
= data
->info
->m
[sensor
->class];
629 b
= data
->info
->b
[sensor
->class];
630 R
= data
->info
->R
[sensor
->class];
635 /* X = 1/m * (Y * 10^-R - b) */
637 /* scale result to milli-units for everything but fans */
638 if (!(sensor
->class == PSC_FAN
|| sensor
->class == PSC_PWM
)) {
643 /* scale result to micro-units for power sensors */
644 if (sensor
->class == PSC_POWER
) {
654 val
= div_s64(val
+ 5LL, 10L); /* round closest */
658 val
= div_s64(val
- b
, m
);
663 * Convert VID sensor values to milli- or micro-units
664 * depending on sensor type.
666 static s64
pmbus_reg2data_vid(struct pmbus_data
*data
,
667 struct pmbus_sensor
*sensor
)
669 long val
= sensor
->data
;
672 switch (data
->info
->vrm_version
[sensor
->page
]) {
674 if (val
>= 0x02 && val
<= 0xb2)
675 rv
= DIV_ROUND_CLOSEST(160000 - (val
- 2) * 625, 100);
679 rv
= 250 + (val
- 1) * 5;
683 rv
= 500 + (val
- 1) * 10;
687 rv
= 200 + (val
- 1) * 10;
690 if (val
>= 0x0 && val
<= 0xd8)
691 rv
= DIV_ROUND_CLOSEST(155000 - val
* 625, 100);
697 static s64
pmbus_reg2data(struct pmbus_data
*data
, struct pmbus_sensor
*sensor
)
701 if (!sensor
->convert
)
704 switch (data
->info
->format
[sensor
->class]) {
706 val
= pmbus_reg2data_direct(data
, sensor
);
709 val
= pmbus_reg2data_vid(data
, sensor
);
713 val
= pmbus_reg2data_linear(data
, sensor
);
719 #define MAX_MANTISSA (1023 * 1000)
720 #define MIN_MANTISSA (511 * 1000)
722 static u16
pmbus_data2reg_linear(struct pmbus_data
*data
,
723 struct pmbus_sensor
*sensor
, s64 val
)
725 s16 exponent
= 0, mantissa
;
726 bool negative
= false;
732 if (sensor
->class == PSC_VOLTAGE_OUT
) {
733 /* LINEAR16 does not support negative voltages */
738 * For a static exponents, we don't have a choice
739 * but to adjust the value to it.
741 if (data
->exponent
[sensor
->page
] < 0)
742 val
<<= -data
->exponent
[sensor
->page
];
744 val
>>= data
->exponent
[sensor
->page
];
745 val
= DIV_ROUND_CLOSEST_ULL(val
, 1000);
746 return clamp_val(val
, 0, 0xffff);
754 /* Power is in uW. Convert to mW before converting. */
755 if (sensor
->class == PSC_POWER
)
756 val
= DIV_ROUND_CLOSEST_ULL(val
, 1000);
759 * For simplicity, convert fan data to milli-units
760 * before calculating the exponent.
762 if (sensor
->class == PSC_FAN
)
765 /* Reduce large mantissa until it fits into 10 bit */
766 while (val
>= MAX_MANTISSA
&& exponent
< 15) {
770 /* Increase small mantissa to improve precision */
771 while (val
< MIN_MANTISSA
&& exponent
> -15) {
776 /* Convert mantissa from milli-units to units */
777 mantissa
= clamp_val(DIV_ROUND_CLOSEST_ULL(val
, 1000), 0, 0x3ff);
781 mantissa
= -mantissa
;
783 /* Convert to 5 bit exponent, 11 bit mantissa */
784 return (mantissa
& 0x7ff) | ((exponent
<< 11) & 0xf800);
787 static u16
pmbus_data2reg_direct(struct pmbus_data
*data
,
788 struct pmbus_sensor
*sensor
, s64 val
)
793 m
= data
->info
->m
[sensor
->class];
794 b
= data
->info
->b
[sensor
->class];
795 R
= data
->info
->R
[sensor
->class];
797 /* Power is in uW. Adjust R and b. */
798 if (sensor
->class == PSC_POWER
) {
803 /* Calculate Y = (m * X + b) * 10^R */
804 if (!(sensor
->class == PSC_FAN
|| sensor
->class == PSC_PWM
)) {
805 R
-= 3; /* Adjust R and b for data in milli-units */
815 val
= div_s64(val
+ 5LL, 10L); /* round closest */
819 return (u16
)clamp_val(val
, S16_MIN
, S16_MAX
);
822 static u16
pmbus_data2reg_vid(struct pmbus_data
*data
,
823 struct pmbus_sensor
*sensor
, s64 val
)
825 val
= clamp_val(val
, 500, 1600);
827 return 2 + DIV_ROUND_CLOSEST_ULL((1600LL - val
) * 100LL, 625);
830 static u16
pmbus_data2reg(struct pmbus_data
*data
,
831 struct pmbus_sensor
*sensor
, s64 val
)
835 if (!sensor
->convert
)
838 switch (data
->info
->format
[sensor
->class]) {
840 regval
= pmbus_data2reg_direct(data
, sensor
, val
);
843 regval
= pmbus_data2reg_vid(data
, sensor
, val
);
847 regval
= pmbus_data2reg_linear(data
, sensor
, val
);
854 * Return boolean calculated from converted data.
855 * <index> defines a status register index and mask.
856 * The mask is in the lower 8 bits, the register index is in bits 8..23.
858 * The associated pmbus_boolean structure contains optional pointers to two
859 * sensor attributes. If specified, those attributes are compared against each
860 * other to determine if a limit has been exceeded.
862 * If the sensor attribute pointers are NULL, the function returns true if
863 * (status[reg] & mask) is true.
865 * If sensor attribute pointers are provided, a comparison against a specified
866 * limit has to be performed to determine the boolean result.
867 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
868 * sensor values referenced by sensor attribute pointers s1 and s2).
870 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
871 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
873 * If a negative value is stored in any of the referenced registers, this value
874 * reflects an error code which will be returned.
876 static int pmbus_get_boolean(struct i2c_client
*client
, struct pmbus_boolean
*b
,
879 struct pmbus_data
*data
= i2c_get_clientdata(client
);
880 struct pmbus_sensor
*s1
= b
->s1
;
881 struct pmbus_sensor
*s2
= b
->s2
;
882 u16 mask
= pb_index_to_mask(index
);
883 u8 page
= pb_index_to_page(index
);
884 u16 reg
= pb_index_to_reg(index
);
888 mutex_lock(&data
->update_lock
);
889 status
= pmbus_get_status(client
, page
, reg
);
896 pmbus_update_sensor_data(client
, s1
);
898 pmbus_update_sensor_data(client
, s2
);
900 regval
= status
& mask
;
913 v1
= pmbus_reg2data(data
, s1
);
914 v2
= pmbus_reg2data(data
, s2
);
915 ret
= !!(regval
&& v1
>= v2
);
920 mutex_unlock(&data
->update_lock
);
924 static ssize_t
pmbus_show_boolean(struct device
*dev
,
925 struct device_attribute
*da
, char *buf
)
927 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
928 struct pmbus_boolean
*boolean
= to_pmbus_boolean(attr
);
929 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
932 val
= pmbus_get_boolean(client
, boolean
, attr
->index
);
935 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
938 static ssize_t
pmbus_show_sensor(struct device
*dev
,
939 struct device_attribute
*devattr
, char *buf
)
941 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
942 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
943 struct pmbus_data
*data
= i2c_get_clientdata(client
);
946 mutex_lock(&data
->update_lock
);
947 pmbus_update_sensor_data(client
, sensor
);
948 if (sensor
->data
< 0)
951 ret
= snprintf(buf
, PAGE_SIZE
, "%lld\n", pmbus_reg2data(data
, sensor
));
952 mutex_unlock(&data
->update_lock
);
956 static ssize_t
pmbus_set_sensor(struct device
*dev
,
957 struct device_attribute
*devattr
,
958 const char *buf
, size_t count
)
960 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
961 struct pmbus_data
*data
= i2c_get_clientdata(client
);
962 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
968 if (kstrtos64(buf
, 10, &val
) < 0)
971 mutex_lock(&data
->update_lock
);
972 regval
= pmbus_data2reg(data
, sensor
, val
);
973 ret
= _pmbus_write_word_data(client
, sensor
->page
, sensor
->reg
, regval
);
977 sensor
->data
= regval
;
978 mutex_unlock(&data
->update_lock
);
982 static ssize_t
pmbus_show_label(struct device
*dev
,
983 struct device_attribute
*da
, char *buf
)
985 struct pmbus_label
*label
= to_pmbus_label(da
);
987 return snprintf(buf
, PAGE_SIZE
, "%s\n", label
->label
);
990 static int pmbus_add_attribute(struct pmbus_data
*data
, struct attribute
*attr
)
992 if (data
->num_attributes
>= data
->max_attributes
- 1) {
993 int new_max_attrs
= data
->max_attributes
+ PMBUS_ATTR_ALLOC_SIZE
;
994 void *new_attrs
= devm_krealloc(data
->dev
, data
->group
.attrs
,
995 new_max_attrs
* sizeof(void *),
999 data
->group
.attrs
= new_attrs
;
1000 data
->max_attributes
= new_max_attrs
;
1003 data
->group
.attrs
[data
->num_attributes
++] = attr
;
1004 data
->group
.attrs
[data
->num_attributes
] = NULL
;
1008 static void pmbus_dev_attr_init(struct device_attribute
*dev_attr
,
1011 ssize_t (*show
)(struct device
*dev
,
1012 struct device_attribute
*attr
,
1014 ssize_t (*store
)(struct device
*dev
,
1015 struct device_attribute
*attr
,
1016 const char *buf
, size_t count
))
1018 sysfs_attr_init(&dev_attr
->attr
);
1019 dev_attr
->attr
.name
= name
;
1020 dev_attr
->attr
.mode
= mode
;
1021 dev_attr
->show
= show
;
1022 dev_attr
->store
= store
;
1025 static void pmbus_attr_init(struct sensor_device_attribute
*a
,
1028 ssize_t (*show
)(struct device
*dev
,
1029 struct device_attribute
*attr
,
1031 ssize_t (*store
)(struct device
*dev
,
1032 struct device_attribute
*attr
,
1033 const char *buf
, size_t count
),
1036 pmbus_dev_attr_init(&a
->dev_attr
, name
, mode
, show
, store
);
1040 static int pmbus_add_boolean(struct pmbus_data
*data
,
1041 const char *name
, const char *type
, int seq
,
1042 struct pmbus_sensor
*s1
,
1043 struct pmbus_sensor
*s2
,
1044 u8 page
, u16 reg
, u16 mask
)
1046 struct pmbus_boolean
*boolean
;
1047 struct sensor_device_attribute
*a
;
1049 if (WARN((s1
&& !s2
) || (!s1
&& s2
), "Bad s1/s2 parameters\n"))
1052 boolean
= devm_kzalloc(data
->dev
, sizeof(*boolean
), GFP_KERNEL
);
1056 a
= &boolean
->attribute
;
1058 snprintf(boolean
->name
, sizeof(boolean
->name
), "%s%d_%s",
1062 pmbus_attr_init(a
, boolean
->name
, 0444, pmbus_show_boolean
, NULL
,
1063 pb_reg_to_index(page
, reg
, mask
));
1065 return pmbus_add_attribute(data
, &a
->dev_attr
.attr
);
1068 static struct pmbus_sensor
*pmbus_add_sensor(struct pmbus_data
*data
,
1069 const char *name
, const char *type
,
1070 int seq
, int page
, int phase
,
1072 enum pmbus_sensor_classes
class,
1073 bool update
, bool readonly
,
1076 struct pmbus_sensor
*sensor
;
1077 struct device_attribute
*a
;
1079 sensor
= devm_kzalloc(data
->dev
, sizeof(*sensor
), GFP_KERNEL
);
1082 a
= &sensor
->attribute
;
1085 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d_%s",
1088 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d",
1091 if (data
->flags
& PMBUS_WRITE_PROTECTED
)
1094 sensor
->page
= page
;
1095 sensor
->phase
= phase
;
1097 sensor
->class = class;
1098 sensor
->update
= update
;
1099 sensor
->convert
= convert
;
1100 sensor
->data
= -ENODATA
;
1101 pmbus_dev_attr_init(a
, sensor
->name
,
1102 readonly
? 0444 : 0644,
1103 pmbus_show_sensor
, pmbus_set_sensor
);
1105 if (pmbus_add_attribute(data
, &a
->attr
))
1108 sensor
->next
= data
->sensors
;
1109 data
->sensors
= sensor
;
1114 static int pmbus_add_label(struct pmbus_data
*data
,
1115 const char *name
, int seq
,
1116 const char *lstring
, int index
, int phase
)
1118 struct pmbus_label
*label
;
1119 struct device_attribute
*a
;
1121 label
= devm_kzalloc(data
->dev
, sizeof(*label
), GFP_KERNEL
);
1125 a
= &label
->attribute
;
1127 snprintf(label
->name
, sizeof(label
->name
), "%s%d_label", name
, seq
);
1130 strncpy(label
->label
, lstring
,
1131 sizeof(label
->label
) - 1);
1133 snprintf(label
->label
, sizeof(label
->label
), "%s.%d",
1137 snprintf(label
->label
, sizeof(label
->label
), "%s%d",
1140 snprintf(label
->label
, sizeof(label
->label
), "%s%d.%d",
1141 lstring
, index
, phase
);
1144 pmbus_dev_attr_init(a
, label
->name
, 0444, pmbus_show_label
, NULL
);
1145 return pmbus_add_attribute(data
, &a
->attr
);
1149 * Search for attributes. Allocate sensors, booleans, and labels as needed.
1153 * The pmbus_limit_attr structure describes a single limit attribute
1154 * and its associated alarm attribute.
1156 struct pmbus_limit_attr
{
1157 u16 reg
; /* Limit register */
1158 u16 sbit
; /* Alarm attribute status bit */
1159 bool update
; /* True if register needs updates */
1160 bool low
; /* True if low limit; for limits with compare
1162 const char *attr
; /* Attribute name */
1163 const char *alarm
; /* Alarm attribute name */
1167 * The pmbus_sensor_attr structure describes one sensor attribute. This
1168 * description includes a reference to the associated limit attributes.
1170 struct pmbus_sensor_attr
{
1171 u16 reg
; /* sensor register */
1172 u16 gbit
; /* generic status bit */
1173 u8 nlimit
; /* # of limit registers */
1174 enum pmbus_sensor_classes
class;/* sensor class */
1175 const char *label
; /* sensor label */
1176 bool paged
; /* true if paged sensor */
1177 bool update
; /* true if update needed */
1178 bool compare
; /* true if compare function needed */
1179 u32 func
; /* sensor mask */
1180 u32 sfunc
; /* sensor status mask */
1181 int sreg
; /* status register */
1182 const struct pmbus_limit_attr
*limit
;/* limit registers */
1186 * Add a set of limit attributes and, if supported, the associated
1188 * returns 0 if no alarm register found, 1 if an alarm register was found,
1191 static int pmbus_add_limit_attrs(struct i2c_client
*client
,
1192 struct pmbus_data
*data
,
1193 const struct pmbus_driver_info
*info
,
1194 const char *name
, int index
, int page
,
1195 struct pmbus_sensor
*base
,
1196 const struct pmbus_sensor_attr
*attr
)
1198 const struct pmbus_limit_attr
*l
= attr
->limit
;
1199 int nlimit
= attr
->nlimit
;
1202 struct pmbus_sensor
*curr
;
1204 for (i
= 0; i
< nlimit
; i
++) {
1205 if (pmbus_check_word_register(client
, page
, l
->reg
)) {
1206 curr
= pmbus_add_sensor(data
, name
, l
->attr
, index
,
1207 page
, 0xff, l
->reg
, attr
->class,
1208 attr
->update
|| l
->update
,
1212 if (l
->sbit
&& (info
->func
[page
] & attr
->sfunc
)) {
1213 ret
= pmbus_add_boolean(data
, name
,
1215 attr
->compare
? l
->low
? curr
: base
1217 attr
->compare
? l
->low
? base
: curr
1219 page
, attr
->sreg
, l
->sbit
);
1230 static int pmbus_add_sensor_attrs_one(struct i2c_client
*client
,
1231 struct pmbus_data
*data
,
1232 const struct pmbus_driver_info
*info
,
1234 int index
, int page
, int phase
,
1235 const struct pmbus_sensor_attr
*attr
,
1238 struct pmbus_sensor
*base
;
1239 bool upper
= !!(attr
->gbit
& 0xff00); /* need to check STATUS_WORD */
1243 ret
= pmbus_add_label(data
, name
, index
, attr
->label
,
1244 paged
? page
+ 1 : 0, phase
);
1248 base
= pmbus_add_sensor(data
, name
, "input", index
, page
, phase
,
1249 attr
->reg
, attr
->class, true, true, true);
1252 /* No limit and alarm attributes for phase specific sensors */
1253 if (attr
->sfunc
&& phase
== 0xff) {
1254 ret
= pmbus_add_limit_attrs(client
, data
, info
, name
,
1255 index
, page
, base
, attr
);
1259 * Add generic alarm attribute only if there are no individual
1260 * alarm attributes, if there is a global alarm bit, and if
1261 * the generic status register (word or byte, depending on
1262 * which global bit is set) for this page is accessible.
1264 if (!ret
&& attr
->gbit
&&
1265 (!upper
|| (upper
&& data
->has_status_word
)) &&
1266 pmbus_check_status_register(client
, page
)) {
1267 ret
= pmbus_add_boolean(data
, name
, "alarm", index
,
1269 page
, PMBUS_STATUS_WORD
,
1278 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info
*info
,
1279 const struct pmbus_sensor_attr
*attr
)
1287 * Some attributes may be present on more than one page despite
1288 * not being marked with the paged attribute. If that is the case,
1289 * then treat the sensor as being paged and add the page suffix to the
1291 * We don't just add the paged attribute to all such attributes, in
1292 * order to maintain the un-suffixed labels in the case where the
1293 * attribute is only on page 0.
1295 for (p
= 1; p
< info
->pages
; p
++) {
1296 if (info
->func
[p
] & attr
->func
)
1302 static int pmbus_add_sensor_attrs(struct i2c_client
*client
,
1303 struct pmbus_data
*data
,
1305 const struct pmbus_sensor_attr
*attrs
,
1308 const struct pmbus_driver_info
*info
= data
->info
;
1313 for (i
= 0; i
< nattrs
; i
++) {
1315 bool paged
= pmbus_sensor_is_paged(info
, attrs
);
1317 pages
= paged
? info
->pages
: 1;
1318 for (page
= 0; page
< pages
; page
++) {
1319 if (!(info
->func
[page
] & attrs
->func
))
1321 ret
= pmbus_add_sensor_attrs_one(client
, data
, info
,
1323 0xff, attrs
, paged
);
1327 if (info
->phases
[page
]) {
1330 for (phase
= 0; phase
< info
->phases
[page
];
1332 if (!(info
->pfunc
[phase
] & attrs
->func
))
1334 ret
= pmbus_add_sensor_attrs_one(client
,
1335 data
, info
, name
, index
, page
,
1336 phase
, attrs
, paged
);
1348 static const struct pmbus_limit_attr vin_limit_attrs
[] = {
1350 .reg
= PMBUS_VIN_UV_WARN_LIMIT
,
1352 .alarm
= "min_alarm",
1353 .sbit
= PB_VOLTAGE_UV_WARNING
,
1355 .reg
= PMBUS_VIN_UV_FAULT_LIMIT
,
1357 .alarm
= "lcrit_alarm",
1358 .sbit
= PB_VOLTAGE_UV_FAULT
,
1360 .reg
= PMBUS_VIN_OV_WARN_LIMIT
,
1362 .alarm
= "max_alarm",
1363 .sbit
= PB_VOLTAGE_OV_WARNING
,
1365 .reg
= PMBUS_VIN_OV_FAULT_LIMIT
,
1367 .alarm
= "crit_alarm",
1368 .sbit
= PB_VOLTAGE_OV_FAULT
,
1370 .reg
= PMBUS_VIRT_READ_VIN_AVG
,
1374 .reg
= PMBUS_VIRT_READ_VIN_MIN
,
1378 .reg
= PMBUS_VIRT_READ_VIN_MAX
,
1382 .reg
= PMBUS_VIRT_RESET_VIN_HISTORY
,
1383 .attr
= "reset_history",
1385 .reg
= PMBUS_MFR_VIN_MIN
,
1386 .attr
= "rated_min",
1388 .reg
= PMBUS_MFR_VIN_MAX
,
1389 .attr
= "rated_max",
1393 static const struct pmbus_limit_attr vmon_limit_attrs
[] = {
1395 .reg
= PMBUS_VIRT_VMON_UV_WARN_LIMIT
,
1397 .alarm
= "min_alarm",
1398 .sbit
= PB_VOLTAGE_UV_WARNING
,
1400 .reg
= PMBUS_VIRT_VMON_UV_FAULT_LIMIT
,
1402 .alarm
= "lcrit_alarm",
1403 .sbit
= PB_VOLTAGE_UV_FAULT
,
1405 .reg
= PMBUS_VIRT_VMON_OV_WARN_LIMIT
,
1407 .alarm
= "max_alarm",
1408 .sbit
= PB_VOLTAGE_OV_WARNING
,
1410 .reg
= PMBUS_VIRT_VMON_OV_FAULT_LIMIT
,
1412 .alarm
= "crit_alarm",
1413 .sbit
= PB_VOLTAGE_OV_FAULT
,
1417 static const struct pmbus_limit_attr vout_limit_attrs
[] = {
1419 .reg
= PMBUS_VOUT_UV_WARN_LIMIT
,
1421 .alarm
= "min_alarm",
1422 .sbit
= PB_VOLTAGE_UV_WARNING
,
1424 .reg
= PMBUS_VOUT_UV_FAULT_LIMIT
,
1426 .alarm
= "lcrit_alarm",
1427 .sbit
= PB_VOLTAGE_UV_FAULT
,
1429 .reg
= PMBUS_VOUT_OV_WARN_LIMIT
,
1431 .alarm
= "max_alarm",
1432 .sbit
= PB_VOLTAGE_OV_WARNING
,
1434 .reg
= PMBUS_VOUT_OV_FAULT_LIMIT
,
1436 .alarm
= "crit_alarm",
1437 .sbit
= PB_VOLTAGE_OV_FAULT
,
1439 .reg
= PMBUS_VIRT_READ_VOUT_AVG
,
1443 .reg
= PMBUS_VIRT_READ_VOUT_MIN
,
1447 .reg
= PMBUS_VIRT_READ_VOUT_MAX
,
1451 .reg
= PMBUS_VIRT_RESET_VOUT_HISTORY
,
1452 .attr
= "reset_history",
1454 .reg
= PMBUS_MFR_VOUT_MIN
,
1455 .attr
= "rated_min",
1457 .reg
= PMBUS_MFR_VOUT_MAX
,
1458 .attr
= "rated_max",
1462 static const struct pmbus_sensor_attr voltage_attributes
[] = {
1464 .reg
= PMBUS_READ_VIN
,
1465 .class = PSC_VOLTAGE_IN
,
1467 .func
= PMBUS_HAVE_VIN
,
1468 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1469 .sreg
= PMBUS_STATUS_INPUT
,
1470 .gbit
= PB_STATUS_VIN_UV
,
1471 .limit
= vin_limit_attrs
,
1472 .nlimit
= ARRAY_SIZE(vin_limit_attrs
),
1474 .reg
= PMBUS_VIRT_READ_VMON
,
1475 .class = PSC_VOLTAGE_IN
,
1477 .func
= PMBUS_HAVE_VMON
,
1478 .sfunc
= PMBUS_HAVE_STATUS_VMON
,
1479 .sreg
= PMBUS_VIRT_STATUS_VMON
,
1480 .limit
= vmon_limit_attrs
,
1481 .nlimit
= ARRAY_SIZE(vmon_limit_attrs
),
1483 .reg
= PMBUS_READ_VCAP
,
1484 .class = PSC_VOLTAGE_IN
,
1486 .func
= PMBUS_HAVE_VCAP
,
1488 .reg
= PMBUS_READ_VOUT
,
1489 .class = PSC_VOLTAGE_OUT
,
1492 .func
= PMBUS_HAVE_VOUT
,
1493 .sfunc
= PMBUS_HAVE_STATUS_VOUT
,
1494 .sreg
= PMBUS_STATUS_VOUT
,
1495 .gbit
= PB_STATUS_VOUT_OV
,
1496 .limit
= vout_limit_attrs
,
1497 .nlimit
= ARRAY_SIZE(vout_limit_attrs
),
1501 /* Current attributes */
1503 static const struct pmbus_limit_attr iin_limit_attrs
[] = {
1505 .reg
= PMBUS_IIN_OC_WARN_LIMIT
,
1507 .alarm
= "max_alarm",
1508 .sbit
= PB_IIN_OC_WARNING
,
1510 .reg
= PMBUS_IIN_OC_FAULT_LIMIT
,
1512 .alarm
= "crit_alarm",
1513 .sbit
= PB_IIN_OC_FAULT
,
1515 .reg
= PMBUS_VIRT_READ_IIN_AVG
,
1519 .reg
= PMBUS_VIRT_READ_IIN_MIN
,
1523 .reg
= PMBUS_VIRT_READ_IIN_MAX
,
1527 .reg
= PMBUS_VIRT_RESET_IIN_HISTORY
,
1528 .attr
= "reset_history",
1530 .reg
= PMBUS_MFR_IIN_MAX
,
1531 .attr
= "rated_max",
1535 static const struct pmbus_limit_attr iout_limit_attrs
[] = {
1537 .reg
= PMBUS_IOUT_OC_WARN_LIMIT
,
1539 .alarm
= "max_alarm",
1540 .sbit
= PB_IOUT_OC_WARNING
,
1542 .reg
= PMBUS_IOUT_UC_FAULT_LIMIT
,
1544 .alarm
= "lcrit_alarm",
1545 .sbit
= PB_IOUT_UC_FAULT
,
1547 .reg
= PMBUS_IOUT_OC_FAULT_LIMIT
,
1549 .alarm
= "crit_alarm",
1550 .sbit
= PB_IOUT_OC_FAULT
,
1552 .reg
= PMBUS_VIRT_READ_IOUT_AVG
,
1556 .reg
= PMBUS_VIRT_READ_IOUT_MIN
,
1560 .reg
= PMBUS_VIRT_READ_IOUT_MAX
,
1564 .reg
= PMBUS_VIRT_RESET_IOUT_HISTORY
,
1565 .attr
= "reset_history",
1567 .reg
= PMBUS_MFR_IOUT_MAX
,
1568 .attr
= "rated_max",
1572 static const struct pmbus_sensor_attr current_attributes
[] = {
1574 .reg
= PMBUS_READ_IIN
,
1575 .class = PSC_CURRENT_IN
,
1577 .func
= PMBUS_HAVE_IIN
,
1578 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1579 .sreg
= PMBUS_STATUS_INPUT
,
1580 .gbit
= PB_STATUS_INPUT
,
1581 .limit
= iin_limit_attrs
,
1582 .nlimit
= ARRAY_SIZE(iin_limit_attrs
),
1584 .reg
= PMBUS_READ_IOUT
,
1585 .class = PSC_CURRENT_OUT
,
1588 .func
= PMBUS_HAVE_IOUT
,
1589 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1590 .sreg
= PMBUS_STATUS_IOUT
,
1591 .gbit
= PB_STATUS_IOUT_OC
,
1592 .limit
= iout_limit_attrs
,
1593 .nlimit
= ARRAY_SIZE(iout_limit_attrs
),
1597 /* Power attributes */
1599 static const struct pmbus_limit_attr pin_limit_attrs
[] = {
1601 .reg
= PMBUS_PIN_OP_WARN_LIMIT
,
1604 .sbit
= PB_PIN_OP_WARNING
,
1606 .reg
= PMBUS_VIRT_READ_PIN_AVG
,
1610 .reg
= PMBUS_VIRT_READ_PIN_MIN
,
1612 .attr
= "input_lowest",
1614 .reg
= PMBUS_VIRT_READ_PIN_MAX
,
1616 .attr
= "input_highest",
1618 .reg
= PMBUS_VIRT_RESET_PIN_HISTORY
,
1619 .attr
= "reset_history",
1621 .reg
= PMBUS_MFR_PIN_MAX
,
1622 .attr
= "rated_max",
1626 static const struct pmbus_limit_attr pout_limit_attrs
[] = {
1628 .reg
= PMBUS_POUT_MAX
,
1630 .alarm
= "cap_alarm",
1631 .sbit
= PB_POWER_LIMITING
,
1633 .reg
= PMBUS_POUT_OP_WARN_LIMIT
,
1635 .alarm
= "max_alarm",
1636 .sbit
= PB_POUT_OP_WARNING
,
1638 .reg
= PMBUS_POUT_OP_FAULT_LIMIT
,
1640 .alarm
= "crit_alarm",
1641 .sbit
= PB_POUT_OP_FAULT
,
1643 .reg
= PMBUS_VIRT_READ_POUT_AVG
,
1647 .reg
= PMBUS_VIRT_READ_POUT_MIN
,
1649 .attr
= "input_lowest",
1651 .reg
= PMBUS_VIRT_READ_POUT_MAX
,
1653 .attr
= "input_highest",
1655 .reg
= PMBUS_VIRT_RESET_POUT_HISTORY
,
1656 .attr
= "reset_history",
1658 .reg
= PMBUS_MFR_POUT_MAX
,
1659 .attr
= "rated_max",
1663 static const struct pmbus_sensor_attr power_attributes
[] = {
1665 .reg
= PMBUS_READ_PIN
,
1668 .func
= PMBUS_HAVE_PIN
,
1669 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1670 .sreg
= PMBUS_STATUS_INPUT
,
1671 .gbit
= PB_STATUS_INPUT
,
1672 .limit
= pin_limit_attrs
,
1673 .nlimit
= ARRAY_SIZE(pin_limit_attrs
),
1675 .reg
= PMBUS_READ_POUT
,
1679 .func
= PMBUS_HAVE_POUT
,
1680 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1681 .sreg
= PMBUS_STATUS_IOUT
,
1682 .limit
= pout_limit_attrs
,
1683 .nlimit
= ARRAY_SIZE(pout_limit_attrs
),
1687 /* Temperature atributes */
1689 static const struct pmbus_limit_attr temp_limit_attrs
[] = {
1691 .reg
= PMBUS_UT_WARN_LIMIT
,
1694 .alarm
= "min_alarm",
1695 .sbit
= PB_TEMP_UT_WARNING
,
1697 .reg
= PMBUS_UT_FAULT_LIMIT
,
1700 .alarm
= "lcrit_alarm",
1701 .sbit
= PB_TEMP_UT_FAULT
,
1703 .reg
= PMBUS_OT_WARN_LIMIT
,
1705 .alarm
= "max_alarm",
1706 .sbit
= PB_TEMP_OT_WARNING
,
1708 .reg
= PMBUS_OT_FAULT_LIMIT
,
1710 .alarm
= "crit_alarm",
1711 .sbit
= PB_TEMP_OT_FAULT
,
1713 .reg
= PMBUS_VIRT_READ_TEMP_MIN
,
1716 .reg
= PMBUS_VIRT_READ_TEMP_AVG
,
1719 .reg
= PMBUS_VIRT_READ_TEMP_MAX
,
1722 .reg
= PMBUS_VIRT_RESET_TEMP_HISTORY
,
1723 .attr
= "reset_history",
1725 .reg
= PMBUS_MFR_MAX_TEMP_1
,
1726 .attr
= "rated_max",
1730 static const struct pmbus_limit_attr temp_limit_attrs2
[] = {
1732 .reg
= PMBUS_UT_WARN_LIMIT
,
1735 .alarm
= "min_alarm",
1736 .sbit
= PB_TEMP_UT_WARNING
,
1738 .reg
= PMBUS_UT_FAULT_LIMIT
,
1741 .alarm
= "lcrit_alarm",
1742 .sbit
= PB_TEMP_UT_FAULT
,
1744 .reg
= PMBUS_OT_WARN_LIMIT
,
1746 .alarm
= "max_alarm",
1747 .sbit
= PB_TEMP_OT_WARNING
,
1749 .reg
= PMBUS_OT_FAULT_LIMIT
,
1751 .alarm
= "crit_alarm",
1752 .sbit
= PB_TEMP_OT_FAULT
,
1754 .reg
= PMBUS_VIRT_READ_TEMP2_MIN
,
1757 .reg
= PMBUS_VIRT_READ_TEMP2_AVG
,
1760 .reg
= PMBUS_VIRT_READ_TEMP2_MAX
,
1763 .reg
= PMBUS_VIRT_RESET_TEMP2_HISTORY
,
1764 .attr
= "reset_history",
1766 .reg
= PMBUS_MFR_MAX_TEMP_2
,
1767 .attr
= "rated_max",
1771 static const struct pmbus_limit_attr temp_limit_attrs3
[] = {
1773 .reg
= PMBUS_UT_WARN_LIMIT
,
1776 .alarm
= "min_alarm",
1777 .sbit
= PB_TEMP_UT_WARNING
,
1779 .reg
= PMBUS_UT_FAULT_LIMIT
,
1782 .alarm
= "lcrit_alarm",
1783 .sbit
= PB_TEMP_UT_FAULT
,
1785 .reg
= PMBUS_OT_WARN_LIMIT
,
1787 .alarm
= "max_alarm",
1788 .sbit
= PB_TEMP_OT_WARNING
,
1790 .reg
= PMBUS_OT_FAULT_LIMIT
,
1792 .alarm
= "crit_alarm",
1793 .sbit
= PB_TEMP_OT_FAULT
,
1795 .reg
= PMBUS_MFR_MAX_TEMP_3
,
1796 .attr
= "rated_max",
1800 static const struct pmbus_sensor_attr temp_attributes
[] = {
1802 .reg
= PMBUS_READ_TEMPERATURE_1
,
1803 .class = PSC_TEMPERATURE
,
1807 .func
= PMBUS_HAVE_TEMP
,
1808 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1809 .sreg
= PMBUS_STATUS_TEMPERATURE
,
1810 .gbit
= PB_STATUS_TEMPERATURE
,
1811 .limit
= temp_limit_attrs
,
1812 .nlimit
= ARRAY_SIZE(temp_limit_attrs
),
1814 .reg
= PMBUS_READ_TEMPERATURE_2
,
1815 .class = PSC_TEMPERATURE
,
1819 .func
= PMBUS_HAVE_TEMP2
,
1820 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1821 .sreg
= PMBUS_STATUS_TEMPERATURE
,
1822 .gbit
= PB_STATUS_TEMPERATURE
,
1823 .limit
= temp_limit_attrs2
,
1824 .nlimit
= ARRAY_SIZE(temp_limit_attrs2
),
1826 .reg
= PMBUS_READ_TEMPERATURE_3
,
1827 .class = PSC_TEMPERATURE
,
1831 .func
= PMBUS_HAVE_TEMP3
,
1832 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1833 .sreg
= PMBUS_STATUS_TEMPERATURE
,
1834 .gbit
= PB_STATUS_TEMPERATURE
,
1835 .limit
= temp_limit_attrs3
,
1836 .nlimit
= ARRAY_SIZE(temp_limit_attrs3
),
1840 static const int pmbus_fan_registers
[] = {
1841 PMBUS_READ_FAN_SPEED_1
,
1842 PMBUS_READ_FAN_SPEED_2
,
1843 PMBUS_READ_FAN_SPEED_3
,
1844 PMBUS_READ_FAN_SPEED_4
1847 static const int pmbus_fan_status_registers
[] = {
1848 PMBUS_STATUS_FAN_12
,
1849 PMBUS_STATUS_FAN_12
,
1850 PMBUS_STATUS_FAN_34
,
1854 static const u32 pmbus_fan_flags
[] = {
1861 static const u32 pmbus_fan_status_flags
[] = {
1862 PMBUS_HAVE_STATUS_FAN12
,
1863 PMBUS_HAVE_STATUS_FAN12
,
1864 PMBUS_HAVE_STATUS_FAN34
,
1865 PMBUS_HAVE_STATUS_FAN34
1870 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */
1871 static int pmbus_add_fan_ctrl(struct i2c_client
*client
,
1872 struct pmbus_data
*data
, int index
, int page
, int id
,
1875 struct pmbus_sensor
*sensor
;
1877 sensor
= pmbus_add_sensor(data
, "fan", "target", index
, page
,
1878 0xff, PMBUS_VIRT_FAN_TARGET_1
+ id
, PSC_FAN
,
1879 false, false, true);
1884 if (!((data
->info
->func
[page
] & PMBUS_HAVE_PWM12
) ||
1885 (data
->info
->func
[page
] & PMBUS_HAVE_PWM34
)))
1888 sensor
= pmbus_add_sensor(data
, "pwm", NULL
, index
, page
,
1889 0xff, PMBUS_VIRT_PWM_1
+ id
, PSC_PWM
,
1890 false, false, true);
1895 sensor
= pmbus_add_sensor(data
, "pwm", "enable", index
, page
,
1896 0xff, PMBUS_VIRT_PWM_ENABLE_1
+ id
, PSC_PWM
,
1897 true, false, false);
1905 static int pmbus_add_fan_attributes(struct i2c_client
*client
,
1906 struct pmbus_data
*data
)
1908 const struct pmbus_driver_info
*info
= data
->info
;
1913 for (page
= 0; page
< info
->pages
; page
++) {
1916 for (f
= 0; f
< ARRAY_SIZE(pmbus_fan_registers
); f
++) {
1919 if (!(info
->func
[page
] & pmbus_fan_flags
[f
]))
1922 if (!pmbus_check_word_register(client
, page
,
1923 pmbus_fan_registers
[f
]))
1927 * Skip fan if not installed.
1928 * Each fan configuration register covers multiple fans,
1929 * so we have to do some magic.
1931 regval
= _pmbus_read_byte_data(client
, page
,
1932 pmbus_fan_config_registers
[f
]);
1934 (!(regval
& (PB_FAN_1_INSTALLED
>> ((f
& 1) * 4)))))
1937 if (pmbus_add_sensor(data
, "fan", "input", index
,
1938 page
, 0xff, pmbus_fan_registers
[f
],
1939 PSC_FAN
, true, true, true) == NULL
)
1943 if (pmbus_check_word_register(client
, page
,
1944 pmbus_fan_command_registers
[f
])) {
1945 ret
= pmbus_add_fan_ctrl(client
, data
, index
,
1952 * Each fan status register covers multiple fans,
1953 * so we have to do some magic.
1955 if ((info
->func
[page
] & pmbus_fan_status_flags
[f
]) &&
1956 pmbus_check_byte_register(client
,
1957 page
, pmbus_fan_status_registers
[f
])) {
1960 if (f
> 1) /* fan 3, 4 */
1961 reg
= PMBUS_STATUS_FAN_34
;
1963 reg
= PMBUS_STATUS_FAN_12
;
1964 ret
= pmbus_add_boolean(data
, "fan",
1965 "alarm", index
, NULL
, NULL
, page
, reg
,
1966 PB_FAN_FAN1_WARNING
>> (f
& 1));
1969 ret
= pmbus_add_boolean(data
, "fan",
1970 "fault", index
, NULL
, NULL
, page
, reg
,
1971 PB_FAN_FAN1_FAULT
>> (f
& 1));
1981 struct pmbus_samples_attr
{
1986 struct pmbus_samples_reg
{
1988 struct pmbus_samples_attr
*attr
;
1989 struct device_attribute dev_attr
;
1992 static struct pmbus_samples_attr pmbus_samples_registers
[] = {
1994 .reg
= PMBUS_VIRT_SAMPLES
,
1997 .reg
= PMBUS_VIRT_IN_SAMPLES
,
1998 .name
= "in_samples",
2000 .reg
= PMBUS_VIRT_CURR_SAMPLES
,
2001 .name
= "curr_samples",
2003 .reg
= PMBUS_VIRT_POWER_SAMPLES
,
2004 .name
= "power_samples",
2006 .reg
= PMBUS_VIRT_TEMP_SAMPLES
,
2007 .name
= "temp_samples",
2011 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr)
2013 static ssize_t
pmbus_show_samples(struct device
*dev
,
2014 struct device_attribute
*devattr
, char *buf
)
2017 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2018 struct pmbus_samples_reg
*reg
= to_samples_reg(devattr
);
2019 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2021 mutex_lock(&data
->update_lock
);
2022 val
= _pmbus_read_word_data(client
, reg
->page
, 0xff, reg
->attr
->reg
);
2023 mutex_unlock(&data
->update_lock
);
2027 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
2030 static ssize_t
pmbus_set_samples(struct device
*dev
,
2031 struct device_attribute
*devattr
,
2032 const char *buf
, size_t count
)
2036 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2037 struct pmbus_samples_reg
*reg
= to_samples_reg(devattr
);
2038 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2040 if (kstrtol(buf
, 0, &val
) < 0)
2043 mutex_lock(&data
->update_lock
);
2044 ret
= _pmbus_write_word_data(client
, reg
->page
, reg
->attr
->reg
, val
);
2045 mutex_unlock(&data
->update_lock
);
2047 return ret
? : count
;
2050 static int pmbus_add_samples_attr(struct pmbus_data
*data
, int page
,
2051 struct pmbus_samples_attr
*attr
)
2053 struct pmbus_samples_reg
*reg
;
2055 reg
= devm_kzalloc(data
->dev
, sizeof(*reg
), GFP_KERNEL
);
2062 pmbus_dev_attr_init(®
->dev_attr
, attr
->name
, 0644,
2063 pmbus_show_samples
, pmbus_set_samples
);
2065 return pmbus_add_attribute(data
, ®
->dev_attr
.attr
);
2068 static int pmbus_add_samples_attributes(struct i2c_client
*client
,
2069 struct pmbus_data
*data
)
2071 const struct pmbus_driver_info
*info
= data
->info
;
2074 if (!(info
->func
[0] & PMBUS_HAVE_SAMPLES
))
2077 for (s
= 0; s
< ARRAY_SIZE(pmbus_samples_registers
); s
++) {
2078 struct pmbus_samples_attr
*attr
;
2081 attr
= &pmbus_samples_registers
[s
];
2082 if (!pmbus_check_word_register(client
, 0, attr
->reg
))
2085 ret
= pmbus_add_samples_attr(data
, 0, attr
);
2093 static int pmbus_find_attributes(struct i2c_client
*client
,
2094 struct pmbus_data
*data
)
2098 /* Voltage sensors */
2099 ret
= pmbus_add_sensor_attrs(client
, data
, "in", voltage_attributes
,
2100 ARRAY_SIZE(voltage_attributes
));
2104 /* Current sensors */
2105 ret
= pmbus_add_sensor_attrs(client
, data
, "curr", current_attributes
,
2106 ARRAY_SIZE(current_attributes
));
2111 ret
= pmbus_add_sensor_attrs(client
, data
, "power", power_attributes
,
2112 ARRAY_SIZE(power_attributes
));
2116 /* Temperature sensors */
2117 ret
= pmbus_add_sensor_attrs(client
, data
, "temp", temp_attributes
,
2118 ARRAY_SIZE(temp_attributes
));
2123 ret
= pmbus_add_fan_attributes(client
, data
);
2127 ret
= pmbus_add_samples_attributes(client
, data
);
2132 * Identify chip parameters.
2133 * This function is called for all chips.
2135 static int pmbus_identify_common(struct i2c_client
*client
,
2136 struct pmbus_data
*data
, int page
)
2140 if (pmbus_check_byte_register(client
, page
, PMBUS_VOUT_MODE
))
2141 vout_mode
= _pmbus_read_byte_data(client
, page
,
2143 if (vout_mode
>= 0 && vout_mode
!= 0xff) {
2145 * Not all chips support the VOUT_MODE command,
2146 * so a failure to read it is not an error.
2148 switch (vout_mode
>> 5) {
2149 case 0: /* linear mode */
2150 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != linear
)
2153 data
->exponent
[page
] = ((s8
)(vout_mode
<< 3)) >> 3;
2155 case 1: /* VID mode */
2156 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != vid
)
2159 case 2: /* direct mode */
2160 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != direct
)
2168 pmbus_clear_fault_page(client
, page
);
2172 static int pmbus_read_status_byte(struct i2c_client
*client
, int page
)
2174 return _pmbus_read_byte_data(client
, page
, PMBUS_STATUS_BYTE
);
2177 static int pmbus_read_status_word(struct i2c_client
*client
, int page
)
2179 return _pmbus_read_word_data(client
, page
, 0xff, PMBUS_STATUS_WORD
);
2182 static int pmbus_init_common(struct i2c_client
*client
, struct pmbus_data
*data
,
2183 struct pmbus_driver_info
*info
)
2185 struct device
*dev
= &client
->dev
;
2189 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
2190 * to use PMBUS_STATUS_BYTE instead if that is the case.
2191 * Bail out if both registers are not supported.
2193 data
->read_status
= pmbus_read_status_word
;
2194 ret
= i2c_smbus_read_word_data(client
, PMBUS_STATUS_WORD
);
2195 if (ret
< 0 || ret
== 0xffff) {
2196 data
->read_status
= pmbus_read_status_byte
;
2197 ret
= i2c_smbus_read_byte_data(client
, PMBUS_STATUS_BYTE
);
2198 if (ret
< 0 || ret
== 0xff) {
2199 dev_err(dev
, "PMBus status register not found\n");
2203 data
->has_status_word
= true;
2206 /* Enable PEC if the controller supports it */
2207 ret
= i2c_smbus_read_byte_data(client
, PMBUS_CAPABILITY
);
2208 if (ret
>= 0 && (ret
& PB_CAPABILITY_ERROR_CHECK
))
2209 client
->flags
|= I2C_CLIENT_PEC
;
2212 * Check if the chip is write protected. If it is, we can not clear
2213 * faults, and we should not try it. Also, in that case, writes into
2214 * limit registers need to be disabled.
2216 ret
= i2c_smbus_read_byte_data(client
, PMBUS_WRITE_PROTECT
);
2217 if (ret
> 0 && (ret
& PB_WP_ANY
))
2218 data
->flags
|= PMBUS_WRITE_PROTECTED
| PMBUS_SKIP_STATUS_CHECK
;
2220 if (data
->info
->pages
)
2221 pmbus_clear_faults(client
);
2223 pmbus_clear_fault_page(client
, -1);
2225 if (info
->identify
) {
2226 ret
= (*info
->identify
)(client
, info
);
2228 dev_err(dev
, "Chip identification failed\n");
2233 if (info
->pages
<= 0 || info
->pages
> PMBUS_PAGES
) {
2234 dev_err(dev
, "Bad number of PMBus pages: %d\n", info
->pages
);
2238 for (page
= 0; page
< info
->pages
; page
++) {
2239 ret
= pmbus_identify_common(client
, data
, page
);
2241 dev_err(dev
, "Failed to identify chip capabilities\n");
2248 #if IS_ENABLED(CONFIG_REGULATOR)
2249 static int pmbus_regulator_is_enabled(struct regulator_dev
*rdev
)
2251 struct device
*dev
= rdev_get_dev(rdev
);
2252 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2253 u8 page
= rdev_get_id(rdev
);
2256 ret
= pmbus_read_byte_data(client
, page
, PMBUS_OPERATION
);
2260 return !!(ret
& PB_OPERATION_CONTROL_ON
);
2263 static int _pmbus_regulator_on_off(struct regulator_dev
*rdev
, bool enable
)
2265 struct device
*dev
= rdev_get_dev(rdev
);
2266 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2267 u8 page
= rdev_get_id(rdev
);
2269 return pmbus_update_byte_data(client
, page
, PMBUS_OPERATION
,
2270 PB_OPERATION_CONTROL_ON
,
2271 enable
? PB_OPERATION_CONTROL_ON
: 0);
2274 static int pmbus_regulator_enable(struct regulator_dev
*rdev
)
2276 return _pmbus_regulator_on_off(rdev
, 1);
2279 static int pmbus_regulator_disable(struct regulator_dev
*rdev
)
2281 return _pmbus_regulator_on_off(rdev
, 0);
2284 const struct regulator_ops pmbus_regulator_ops
= {
2285 .enable
= pmbus_regulator_enable
,
2286 .disable
= pmbus_regulator_disable
,
2287 .is_enabled
= pmbus_regulator_is_enabled
,
2289 EXPORT_SYMBOL_GPL(pmbus_regulator_ops
);
2291 static int pmbus_regulator_register(struct pmbus_data
*data
)
2293 struct device
*dev
= data
->dev
;
2294 const struct pmbus_driver_info
*info
= data
->info
;
2295 const struct pmbus_platform_data
*pdata
= dev_get_platdata(dev
);
2296 struct regulator_dev
*rdev
;
2299 for (i
= 0; i
< info
->num_regulators
; i
++) {
2300 struct regulator_config config
= { };
2303 config
.driver_data
= data
;
2305 if (pdata
&& pdata
->reg_init_data
)
2306 config
.init_data
= &pdata
->reg_init_data
[i
];
2308 rdev
= devm_regulator_register(dev
, &info
->reg_desc
[i
],
2311 dev_err(dev
, "Failed to register %s regulator\n",
2312 info
->reg_desc
[i
].name
);
2313 return PTR_ERR(rdev
);
2320 static int pmbus_regulator_register(struct pmbus_data
*data
)
2326 static struct dentry
*pmbus_debugfs_dir
; /* pmbus debugfs directory */
2328 #if IS_ENABLED(CONFIG_DEBUG_FS)
2329 static int pmbus_debugfs_get(void *data
, u64
*val
)
2332 struct pmbus_debugfs_entry
*entry
= data
;
2334 rc
= _pmbus_read_byte_data(entry
->client
, entry
->page
, entry
->reg
);
2342 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops
, pmbus_debugfs_get
, NULL
,
2345 static int pmbus_debugfs_get_status(void *data
, u64
*val
)
2348 struct pmbus_debugfs_entry
*entry
= data
;
2349 struct pmbus_data
*pdata
= i2c_get_clientdata(entry
->client
);
2351 rc
= pdata
->read_status(entry
->client
, entry
->page
);
2359 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status
, pmbus_debugfs_get_status
,
2360 NULL
, "0x%04llx\n");
2362 static int pmbus_debugfs_get_pec(void *data
, u64
*val
)
2364 struct i2c_client
*client
= data
;
2366 *val
= !!(client
->flags
& I2C_CLIENT_PEC
);
2371 static int pmbus_debugfs_set_pec(void *data
, u64 val
)
2374 struct i2c_client
*client
= data
;
2377 client
->flags
&= ~I2C_CLIENT_PEC
;
2384 rc
= i2c_smbus_read_byte_data(client
, PMBUS_CAPABILITY
);
2388 if (!(rc
& PB_CAPABILITY_ERROR_CHECK
))
2391 client
->flags
|= I2C_CLIENT_PEC
;
2395 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_pec
, pmbus_debugfs_get_pec
,
2396 pmbus_debugfs_set_pec
, "%llu\n");
2398 static void pmbus_remove_debugfs(void *data
)
2400 struct dentry
*entry
= data
;
2402 debugfs_remove_recursive(entry
);
2405 static int pmbus_init_debugfs(struct i2c_client
*client
,
2406 struct pmbus_data
*data
)
2409 char name
[PMBUS_NAME_SIZE
];
2410 struct pmbus_debugfs_entry
*entries
;
2412 if (!pmbus_debugfs_dir
)
2416 * Create the debugfs directory for this device. Use the hwmon device
2417 * name to avoid conflicts (hwmon numbers are globally unique).
2419 data
->debugfs
= debugfs_create_dir(dev_name(data
->hwmon_dev
),
2421 if (IS_ERR_OR_NULL(data
->debugfs
)) {
2422 data
->debugfs
= NULL
;
2426 /* Allocate the max possible entries we need. */
2427 entries
= devm_kcalloc(data
->dev
,
2428 data
->info
->pages
* 10, sizeof(*entries
),
2433 debugfs_create_file("pec", 0664, data
->debugfs
, client
,
2434 &pmbus_debugfs_ops_pec
);
2436 for (i
= 0; i
< data
->info
->pages
; ++i
) {
2437 /* Check accessibility of status register if it's not page 0 */
2438 if (!i
|| pmbus_check_status_register(client
, i
)) {
2439 /* No need to set reg as we have special read op. */
2440 entries
[idx
].client
= client
;
2441 entries
[idx
].page
= i
;
2442 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d", i
);
2443 debugfs_create_file(name
, 0444, data
->debugfs
,
2445 &pmbus_debugfs_ops_status
);
2448 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_VOUT
) {
2449 entries
[idx
].client
= client
;
2450 entries
[idx
].page
= i
;
2451 entries
[idx
].reg
= PMBUS_STATUS_VOUT
;
2452 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_vout", i
);
2453 debugfs_create_file(name
, 0444, data
->debugfs
,
2455 &pmbus_debugfs_ops
);
2458 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_IOUT
) {
2459 entries
[idx
].client
= client
;
2460 entries
[idx
].page
= i
;
2461 entries
[idx
].reg
= PMBUS_STATUS_IOUT
;
2462 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_iout", i
);
2463 debugfs_create_file(name
, 0444, data
->debugfs
,
2465 &pmbus_debugfs_ops
);
2468 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_INPUT
) {
2469 entries
[idx
].client
= client
;
2470 entries
[idx
].page
= i
;
2471 entries
[idx
].reg
= PMBUS_STATUS_INPUT
;
2472 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_input", i
);
2473 debugfs_create_file(name
, 0444, data
->debugfs
,
2475 &pmbus_debugfs_ops
);
2478 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_TEMP
) {
2479 entries
[idx
].client
= client
;
2480 entries
[idx
].page
= i
;
2481 entries
[idx
].reg
= PMBUS_STATUS_TEMPERATURE
;
2482 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_temp", i
);
2483 debugfs_create_file(name
, 0444, data
->debugfs
,
2485 &pmbus_debugfs_ops
);
2488 if (pmbus_check_byte_register(client
, i
, PMBUS_STATUS_CML
)) {
2489 entries
[idx
].client
= client
;
2490 entries
[idx
].page
= i
;
2491 entries
[idx
].reg
= PMBUS_STATUS_CML
;
2492 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_cml", i
);
2493 debugfs_create_file(name
, 0444, data
->debugfs
,
2495 &pmbus_debugfs_ops
);
2498 if (pmbus_check_byte_register(client
, i
, PMBUS_STATUS_OTHER
)) {
2499 entries
[idx
].client
= client
;
2500 entries
[idx
].page
= i
;
2501 entries
[idx
].reg
= PMBUS_STATUS_OTHER
;
2502 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_other", i
);
2503 debugfs_create_file(name
, 0444, data
->debugfs
,
2505 &pmbus_debugfs_ops
);
2508 if (pmbus_check_byte_register(client
, i
,
2509 PMBUS_STATUS_MFR_SPECIFIC
)) {
2510 entries
[idx
].client
= client
;
2511 entries
[idx
].page
= i
;
2512 entries
[idx
].reg
= PMBUS_STATUS_MFR_SPECIFIC
;
2513 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_mfr", i
);
2514 debugfs_create_file(name
, 0444, data
->debugfs
,
2516 &pmbus_debugfs_ops
);
2519 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_FAN12
) {
2520 entries
[idx
].client
= client
;
2521 entries
[idx
].page
= i
;
2522 entries
[idx
].reg
= PMBUS_STATUS_FAN_12
;
2523 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_fan12", i
);
2524 debugfs_create_file(name
, 0444, data
->debugfs
,
2526 &pmbus_debugfs_ops
);
2529 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_FAN34
) {
2530 entries
[idx
].client
= client
;
2531 entries
[idx
].page
= i
;
2532 entries
[idx
].reg
= PMBUS_STATUS_FAN_34
;
2533 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_fan34", i
);
2534 debugfs_create_file(name
, 0444, data
->debugfs
,
2536 &pmbus_debugfs_ops
);
2540 return devm_add_action_or_reset(data
->dev
,
2541 pmbus_remove_debugfs
, data
->debugfs
);
2544 static int pmbus_init_debugfs(struct i2c_client
*client
,
2545 struct pmbus_data
*data
)
2549 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */
2551 int pmbus_do_probe(struct i2c_client
*client
, struct pmbus_driver_info
*info
)
2553 struct device
*dev
= &client
->dev
;
2554 const struct pmbus_platform_data
*pdata
= dev_get_platdata(dev
);
2555 struct pmbus_data
*data
;
2556 size_t groups_num
= 0;
2562 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WRITE_BYTE
2563 | I2C_FUNC_SMBUS_BYTE_DATA
2564 | I2C_FUNC_SMBUS_WORD_DATA
))
2567 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
2572 while (info
->groups
[groups_num
])
2575 data
->groups
= devm_kcalloc(dev
, groups_num
+ 2, sizeof(void *),
2580 i2c_set_clientdata(client
, data
);
2581 mutex_init(&data
->update_lock
);
2585 data
->flags
= pdata
->flags
;
2587 data
->currpage
= -1;
2588 data
->currphase
= -1;
2590 ret
= pmbus_init_common(client
, data
, info
);
2594 ret
= pmbus_find_attributes(client
, data
);
2599 * If there are no attributes, something is wrong.
2600 * Bail out instead of trying to register nothing.
2602 if (!data
->num_attributes
) {
2603 dev_err(dev
, "No attributes found\n");
2607 data
->groups
[0] = &data
->group
;
2608 memcpy(data
->groups
+ 1, info
->groups
, sizeof(void *) * groups_num
);
2609 data
->hwmon_dev
= devm_hwmon_device_register_with_groups(dev
,
2610 client
->name
, data
, data
->groups
);
2611 if (IS_ERR(data
->hwmon_dev
)) {
2612 dev_err(dev
, "Failed to register hwmon device\n");
2613 return PTR_ERR(data
->hwmon_dev
);
2616 ret
= pmbus_regulator_register(data
);
2620 ret
= pmbus_init_debugfs(client
, data
);
2622 dev_warn(dev
, "Failed to register debugfs\n");
2626 EXPORT_SYMBOL_GPL(pmbus_do_probe
);
2628 struct dentry
*pmbus_get_debugfs_dir(struct i2c_client
*client
)
2630 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2632 return data
->debugfs
;
2634 EXPORT_SYMBOL_GPL(pmbus_get_debugfs_dir
);
2636 static int __init
pmbus_core_init(void)
2638 pmbus_debugfs_dir
= debugfs_create_dir("pmbus", NULL
);
2639 if (IS_ERR(pmbus_debugfs_dir
))
2640 pmbus_debugfs_dir
= NULL
;
2645 static void __exit
pmbus_core_exit(void)
2647 debugfs_remove_recursive(pmbus_debugfs_dir
);
2650 module_init(pmbus_core_init
);
2651 module_exit(pmbus_core_exit
);
2653 MODULE_AUTHOR("Guenter Roeck");
2654 MODULE_DESCRIPTION("PMBus core driver");
2655 MODULE_LICENSE("GPL");