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/jiffies.h>
20 #include <linux/pmbus.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
26 * Number of additional attribute pointers to allocate
27 * with each call to krealloc
29 #define PMBUS_ATTR_ALLOC_SIZE 32
32 * Index into status register array, per status register group
34 #define PB_STATUS_BASE 0
35 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
36 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
37 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
38 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
39 #define PB_STATUS_TEMP_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
40 #define PB_STATUS_INPUT_BASE (PB_STATUS_TEMP_BASE + PMBUS_PAGES)
41 #define PB_STATUS_VMON_BASE (PB_STATUS_INPUT_BASE + 1)
43 #define PB_NUM_STATUS_REG (PB_STATUS_VMON_BASE + 1)
45 #define PMBUS_NAME_SIZE 24
48 struct pmbus_sensor
*next
;
49 char name
[PMBUS_NAME_SIZE
]; /* sysfs sensor name */
50 struct device_attribute attribute
;
51 u8 page
; /* page number */
52 u16 reg
; /* register */
53 enum pmbus_sensor_classes
class; /* sensor class */
54 bool update
; /* runtime sensor update needed */
55 bool convert
; /* Whether or not to apply linear/vid/direct */
56 int data
; /* Sensor data.
57 Negative if there was a read error */
59 #define to_pmbus_sensor(_attr) \
60 container_of(_attr, struct pmbus_sensor, attribute)
62 struct pmbus_boolean
{
63 char name
[PMBUS_NAME_SIZE
]; /* sysfs boolean name */
64 struct sensor_device_attribute attribute
;
65 struct pmbus_sensor
*s1
;
66 struct pmbus_sensor
*s2
;
68 #define to_pmbus_boolean(_attr) \
69 container_of(_attr, struct pmbus_boolean, attribute)
72 char name
[PMBUS_NAME_SIZE
]; /* sysfs label name */
73 struct device_attribute attribute
;
74 char label
[PMBUS_NAME_SIZE
]; /* label */
76 #define to_pmbus_label(_attr) \
77 container_of(_attr, struct pmbus_label, attribute)
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 unsigned long last_updated
; /* in jiffies */
103 * A single status register covers multiple attributes,
104 * so we keep them all together.
106 u16 status
[PB_NUM_STATUS_REG
];
108 bool has_status_word
; /* device uses STATUS_WORD register */
109 int (*read_status
)(struct i2c_client
*client
, int page
);
114 struct pmbus_debugfs_entry
{
115 struct i2c_client
*client
;
120 static const int pmbus_fan_rpm_mask
[] = {
127 static const int pmbus_fan_config_registers
[] = {
134 static const int pmbus_fan_command_registers
[] = {
141 void pmbus_clear_cache(struct i2c_client
*client
)
143 struct pmbus_data
*data
= i2c_get_clientdata(client
);
147 EXPORT_SYMBOL_GPL(pmbus_clear_cache
);
149 int pmbus_set_page(struct i2c_client
*client
, int page
)
151 struct pmbus_data
*data
= i2c_get_clientdata(client
);
154 if (page
< 0 || page
== data
->currpage
)
157 if (!(data
->info
->func
[page
] & PMBUS_PAGE_VIRTUAL
)) {
158 rv
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, page
);
162 rv
= i2c_smbus_read_byte_data(client
, PMBUS_PAGE
);
170 data
->currpage
= page
;
174 EXPORT_SYMBOL_GPL(pmbus_set_page
);
176 int pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
180 rv
= pmbus_set_page(client
, page
);
184 return i2c_smbus_write_byte(client
, value
);
186 EXPORT_SYMBOL_GPL(pmbus_write_byte
);
189 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
190 * a device specific mapping function exists and calls it if necessary.
192 static int _pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
194 struct pmbus_data
*data
= i2c_get_clientdata(client
);
195 const struct pmbus_driver_info
*info
= data
->info
;
198 if (info
->write_byte
) {
199 status
= info
->write_byte(client
, page
, value
);
200 if (status
!= -ENODATA
)
203 return pmbus_write_byte(client
, page
, value
);
206 int pmbus_write_word_data(struct i2c_client
*client
, int page
, u8 reg
,
211 rv
= pmbus_set_page(client
, page
);
215 return i2c_smbus_write_word_data(client
, reg
, word
);
217 EXPORT_SYMBOL_GPL(pmbus_write_word_data
);
220 static int pmbus_write_virt_reg(struct i2c_client
*client
, int page
, int reg
,
228 case PMBUS_VIRT_FAN_TARGET_1
... PMBUS_VIRT_FAN_TARGET_4
:
229 id
= reg
- PMBUS_VIRT_FAN_TARGET_1
;
230 bit
= pmbus_fan_rpm_mask
[id
];
231 rv
= pmbus_update_fan(client
, page
, id
, bit
, bit
, word
);
242 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
243 * a device specific mapping function exists and calls it if necessary.
245 static int _pmbus_write_word_data(struct i2c_client
*client
, int page
, int reg
,
248 struct pmbus_data
*data
= i2c_get_clientdata(client
);
249 const struct pmbus_driver_info
*info
= data
->info
;
252 if (info
->write_word_data
) {
253 status
= info
->write_word_data(client
, page
, reg
, word
);
254 if (status
!= -ENODATA
)
258 if (reg
>= PMBUS_VIRT_BASE
)
259 return pmbus_write_virt_reg(client
, page
, reg
, word
);
261 return pmbus_write_word_data(client
, page
, reg
, word
);
264 int pmbus_update_fan(struct i2c_client
*client
, int page
, int id
,
265 u8 config
, u8 mask
, u16 command
)
271 from
= pmbus_read_byte_data(client
, page
,
272 pmbus_fan_config_registers
[id
]);
276 to
= (from
& ~mask
) | (config
& mask
);
278 rv
= pmbus_write_byte_data(client
, page
,
279 pmbus_fan_config_registers
[id
], to
);
284 return _pmbus_write_word_data(client
, page
,
285 pmbus_fan_command_registers
[id
], command
);
287 EXPORT_SYMBOL_GPL(pmbus_update_fan
);
289 int pmbus_read_word_data(struct i2c_client
*client
, int page
, u8 reg
)
293 rv
= pmbus_set_page(client
, page
);
297 return i2c_smbus_read_word_data(client
, reg
);
299 EXPORT_SYMBOL_GPL(pmbus_read_word_data
);
301 static int pmbus_read_virt_reg(struct i2c_client
*client
, int page
, int reg
)
307 case PMBUS_VIRT_FAN_TARGET_1
... PMBUS_VIRT_FAN_TARGET_4
:
308 id
= reg
- PMBUS_VIRT_FAN_TARGET_1
;
309 rv
= pmbus_get_fan_rate_device(client
, page
, id
, rpm
);
320 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
321 * a device specific mapping function exists and calls it if necessary.
323 static int _pmbus_read_word_data(struct i2c_client
*client
, int page
, int reg
)
325 struct pmbus_data
*data
= i2c_get_clientdata(client
);
326 const struct pmbus_driver_info
*info
= data
->info
;
329 if (info
->read_word_data
) {
330 status
= info
->read_word_data(client
, page
, reg
);
331 if (status
!= -ENODATA
)
335 if (reg
>= PMBUS_VIRT_BASE
)
336 return pmbus_read_virt_reg(client
, page
, reg
);
338 return pmbus_read_word_data(client
, page
, reg
);
341 int pmbus_read_byte_data(struct i2c_client
*client
, int page
, u8 reg
)
345 rv
= pmbus_set_page(client
, page
);
349 return i2c_smbus_read_byte_data(client
, reg
);
351 EXPORT_SYMBOL_GPL(pmbus_read_byte_data
);
353 int pmbus_write_byte_data(struct i2c_client
*client
, int page
, u8 reg
, u8 value
)
357 rv
= pmbus_set_page(client
, page
);
361 return i2c_smbus_write_byte_data(client
, reg
, value
);
363 EXPORT_SYMBOL_GPL(pmbus_write_byte_data
);
365 int pmbus_update_byte_data(struct i2c_client
*client
, int page
, u8 reg
,
371 rv
= pmbus_read_byte_data(client
, page
, reg
);
375 tmp
= (rv
& ~mask
) | (value
& mask
);
378 rv
= pmbus_write_byte_data(client
, page
, reg
, tmp
);
382 EXPORT_SYMBOL_GPL(pmbus_update_byte_data
);
385 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
386 * a device specific mapping function exists and calls it if necessary.
388 static int _pmbus_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
390 struct pmbus_data
*data
= i2c_get_clientdata(client
);
391 const struct pmbus_driver_info
*info
= data
->info
;
394 if (info
->read_byte_data
) {
395 status
= info
->read_byte_data(client
, page
, reg
);
396 if (status
!= -ENODATA
)
399 return pmbus_read_byte_data(client
, page
, reg
);
402 static struct pmbus_sensor
*pmbus_find_sensor(struct pmbus_data
*data
, int page
,
405 struct pmbus_sensor
*sensor
;
407 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
) {
408 if (sensor
->page
== page
&& sensor
->reg
== reg
)
412 return ERR_PTR(-EINVAL
);
415 static int pmbus_get_fan_rate(struct i2c_client
*client
, int page
, int id
,
416 enum pmbus_fan_mode mode
,
419 struct pmbus_data
*data
= i2c_get_clientdata(client
);
420 bool want_rpm
, have_rpm
;
421 struct pmbus_sensor
*s
;
425 want_rpm
= (mode
== rpm
);
428 reg
= want_rpm
? PMBUS_VIRT_FAN_TARGET_1
: PMBUS_VIRT_PWM_1
;
429 s
= pmbus_find_sensor(data
, page
, reg
+ id
);
436 config
= pmbus_read_byte_data(client
, page
,
437 pmbus_fan_config_registers
[id
]);
441 have_rpm
= !!(config
& pmbus_fan_rpm_mask
[id
]);
442 if (want_rpm
== have_rpm
)
443 return pmbus_read_word_data(client
, page
,
444 pmbus_fan_command_registers
[id
]);
446 /* Can't sensibly map between RPM and PWM, just return zero */
450 int pmbus_get_fan_rate_device(struct i2c_client
*client
, int page
, int id
,
451 enum pmbus_fan_mode mode
)
453 return pmbus_get_fan_rate(client
, page
, id
, mode
, false);
455 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_device
);
457 int pmbus_get_fan_rate_cached(struct i2c_client
*client
, int page
, int id
,
458 enum pmbus_fan_mode mode
)
460 return pmbus_get_fan_rate(client
, page
, id
, mode
, true);
462 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_cached
);
464 static void pmbus_clear_fault_page(struct i2c_client
*client
, int page
)
466 _pmbus_write_byte(client
, page
, PMBUS_CLEAR_FAULTS
);
469 void pmbus_clear_faults(struct i2c_client
*client
)
471 struct pmbus_data
*data
= i2c_get_clientdata(client
);
474 for (i
= 0; i
< data
->info
->pages
; i
++)
475 pmbus_clear_fault_page(client
, i
);
477 EXPORT_SYMBOL_GPL(pmbus_clear_faults
);
479 static int pmbus_check_status_cml(struct i2c_client
*client
)
481 struct pmbus_data
*data
= i2c_get_clientdata(client
);
484 status
= data
->read_status(client
, -1);
485 if (status
< 0 || (status
& PB_STATUS_CML
)) {
486 status2
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
487 if (status2
< 0 || (status2
& PB_CML_FAULT_INVALID_COMMAND
))
493 static bool pmbus_check_register(struct i2c_client
*client
,
494 int (*func
)(struct i2c_client
*client
,
499 struct pmbus_data
*data
= i2c_get_clientdata(client
);
501 rv
= func(client
, page
, reg
);
502 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
503 rv
= pmbus_check_status_cml(client
);
504 pmbus_clear_fault_page(client
, -1);
508 static bool pmbus_check_status_register(struct i2c_client
*client
, int page
)
511 struct pmbus_data
*data
= i2c_get_clientdata(client
);
513 status
= data
->read_status(client
, page
);
514 if (status
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
) &&
515 (status
& PB_STATUS_CML
)) {
516 status
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
517 if (status
< 0 || (status
& PB_CML_FAULT_INVALID_COMMAND
))
521 pmbus_clear_fault_page(client
, -1);
525 bool pmbus_check_byte_register(struct i2c_client
*client
, int page
, int reg
)
527 return pmbus_check_register(client
, _pmbus_read_byte_data
, page
, reg
);
529 EXPORT_SYMBOL_GPL(pmbus_check_byte_register
);
531 bool pmbus_check_word_register(struct i2c_client
*client
, int page
, int reg
)
533 return pmbus_check_register(client
, _pmbus_read_word_data
, page
, reg
);
535 EXPORT_SYMBOL_GPL(pmbus_check_word_register
);
537 const struct pmbus_driver_info
*pmbus_get_driver_info(struct i2c_client
*client
)
539 struct pmbus_data
*data
= i2c_get_clientdata(client
);
543 EXPORT_SYMBOL_GPL(pmbus_get_driver_info
);
545 static struct _pmbus_status
{
550 { PMBUS_HAVE_STATUS_VOUT
, PB_STATUS_VOUT_BASE
, PMBUS_STATUS_VOUT
},
551 { PMBUS_HAVE_STATUS_IOUT
, PB_STATUS_IOUT_BASE
, PMBUS_STATUS_IOUT
},
552 { PMBUS_HAVE_STATUS_TEMP
, PB_STATUS_TEMP_BASE
,
553 PMBUS_STATUS_TEMPERATURE
},
554 { PMBUS_HAVE_STATUS_FAN12
, PB_STATUS_FAN_BASE
, PMBUS_STATUS_FAN_12
},
555 { PMBUS_HAVE_STATUS_FAN34
, PB_STATUS_FAN34_BASE
, PMBUS_STATUS_FAN_34
},
558 static struct pmbus_data
*pmbus_update_device(struct device
*dev
)
560 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
561 struct pmbus_data
*data
= i2c_get_clientdata(client
);
562 const struct pmbus_driver_info
*info
= data
->info
;
563 struct pmbus_sensor
*sensor
;
565 mutex_lock(&data
->update_lock
);
566 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
569 for (i
= 0; i
< info
->pages
; i
++) {
570 data
->status
[PB_STATUS_BASE
+ i
]
571 = data
->read_status(client
, i
);
572 for (j
= 0; j
< ARRAY_SIZE(pmbus_status
); j
++) {
573 struct _pmbus_status
*s
= &pmbus_status
[j
];
575 if (!(info
->func
[i
] & s
->func
))
577 data
->status
[s
->base
+ i
]
578 = _pmbus_read_byte_data(client
, i
,
583 if (info
->func
[0] & PMBUS_HAVE_STATUS_INPUT
)
584 data
->status
[PB_STATUS_INPUT_BASE
]
585 = _pmbus_read_byte_data(client
, 0,
588 if (info
->func
[0] & PMBUS_HAVE_STATUS_VMON
)
589 data
->status
[PB_STATUS_VMON_BASE
]
590 = _pmbus_read_byte_data(client
, 0,
591 PMBUS_VIRT_STATUS_VMON
);
593 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
) {
594 if (!data
->valid
|| sensor
->update
)
596 = _pmbus_read_word_data(client
,
600 pmbus_clear_faults(client
);
601 data
->last_updated
= jiffies
;
604 mutex_unlock(&data
->update_lock
);
609 * Convert linear sensor values to milli- or micro-units
610 * depending on sensor type.
612 static long pmbus_reg2data_linear(struct pmbus_data
*data
,
613 struct pmbus_sensor
*sensor
)
619 if (sensor
->class == PSC_VOLTAGE_OUT
) { /* LINEAR16 */
620 exponent
= data
->exponent
[sensor
->page
];
621 mantissa
= (u16
) sensor
->data
;
622 } else { /* LINEAR11 */
623 exponent
= ((s16
)sensor
->data
) >> 11;
624 mantissa
= ((s16
)((sensor
->data
& 0x7ff) << 5)) >> 5;
629 /* scale result to milli-units for all sensors except fans */
630 if (sensor
->class != PSC_FAN
)
633 /* scale result to micro-units for power sensors */
634 if (sensor
->class == PSC_POWER
)
646 * Convert direct sensor values to milli- or micro-units
647 * depending on sensor type.
649 static long pmbus_reg2data_direct(struct pmbus_data
*data
,
650 struct pmbus_sensor
*sensor
)
652 s64 b
, val
= (s16
)sensor
->data
;
655 m
= data
->info
->m
[sensor
->class];
656 b
= data
->info
->b
[sensor
->class];
657 R
= data
->info
->R
[sensor
->class];
662 /* X = 1/m * (Y * 10^-R - b) */
664 /* scale result to milli-units for everything but fans */
665 if (!(sensor
->class == PSC_FAN
|| sensor
->class == PSC_PWM
)) {
670 /* scale result to micro-units for power sensors */
671 if (sensor
->class == PSC_POWER
) {
681 val
= div_s64(val
+ 5LL, 10L); /* round closest */
685 val
= div_s64(val
- b
, m
);
686 return clamp_val(val
, LONG_MIN
, LONG_MAX
);
690 * Convert VID sensor values to milli- or micro-units
691 * depending on sensor type.
693 static long pmbus_reg2data_vid(struct pmbus_data
*data
,
694 struct pmbus_sensor
*sensor
)
696 long val
= sensor
->data
;
699 switch (data
->info
->vrm_version
[sensor
->page
]) {
701 if (val
>= 0x02 && val
<= 0xb2)
702 rv
= DIV_ROUND_CLOSEST(160000 - (val
- 2) * 625, 100);
706 rv
= 250 + (val
- 1) * 5;
710 rv
= 500 + (val
- 1) * 10;
714 rv
= 200 + (val
- 1) * 10;
717 if (val
>= 0x0 && val
<= 0xd8)
718 rv
= DIV_ROUND_CLOSEST(155000 - val
* 625, 100);
724 static long pmbus_reg2data(struct pmbus_data
*data
, struct pmbus_sensor
*sensor
)
728 if (!sensor
->convert
)
731 switch (data
->info
->format
[sensor
->class]) {
733 val
= pmbus_reg2data_direct(data
, sensor
);
736 val
= pmbus_reg2data_vid(data
, sensor
);
740 val
= pmbus_reg2data_linear(data
, sensor
);
746 #define MAX_MANTISSA (1023 * 1000)
747 #define MIN_MANTISSA (511 * 1000)
749 static u16
pmbus_data2reg_linear(struct pmbus_data
*data
,
750 struct pmbus_sensor
*sensor
, long val
)
752 s16 exponent
= 0, mantissa
;
753 bool negative
= false;
759 if (sensor
->class == PSC_VOLTAGE_OUT
) {
760 /* LINEAR16 does not support negative voltages */
765 * For a static exponents, we don't have a choice
766 * but to adjust the value to it.
768 if (data
->exponent
[sensor
->page
] < 0)
769 val
<<= -data
->exponent
[sensor
->page
];
771 val
>>= data
->exponent
[sensor
->page
];
772 val
= DIV_ROUND_CLOSEST(val
, 1000);
781 /* Power is in uW. Convert to mW before converting. */
782 if (sensor
->class == PSC_POWER
)
783 val
= DIV_ROUND_CLOSEST(val
, 1000L);
786 * For simplicity, convert fan data to milli-units
787 * before calculating the exponent.
789 if (sensor
->class == PSC_FAN
)
792 /* Reduce large mantissa until it fits into 10 bit */
793 while (val
>= MAX_MANTISSA
&& exponent
< 15) {
797 /* Increase small mantissa to improve precision */
798 while (val
< MIN_MANTISSA
&& exponent
> -15) {
803 /* Convert mantissa from milli-units to units */
804 mantissa
= DIV_ROUND_CLOSEST(val
, 1000);
806 /* Ensure that resulting number is within range */
807 if (mantissa
> 0x3ff)
812 mantissa
= -mantissa
;
814 /* Convert to 5 bit exponent, 11 bit mantissa */
815 return (mantissa
& 0x7ff) | ((exponent
<< 11) & 0xf800);
818 static u16
pmbus_data2reg_direct(struct pmbus_data
*data
,
819 struct pmbus_sensor
*sensor
, long val
)
824 m
= data
->info
->m
[sensor
->class];
825 b
= data
->info
->b
[sensor
->class];
826 R
= data
->info
->R
[sensor
->class];
828 /* Power is in uW. Adjust R and b. */
829 if (sensor
->class == PSC_POWER
) {
834 /* Calculate Y = (m * X + b) * 10^R */
835 if (!(sensor
->class == PSC_FAN
|| sensor
->class == PSC_PWM
)) {
836 R
-= 3; /* Adjust R and b for data in milli-units */
839 val64
= val64
* m
+ b
;
846 val64
= div_s64(val64
+ 5LL, 10L); /* round closest */
850 return (u16
)clamp_val(val64
, S16_MIN
, S16_MAX
);
853 static u16
pmbus_data2reg_vid(struct pmbus_data
*data
,
854 struct pmbus_sensor
*sensor
, long val
)
856 val
= clamp_val(val
, 500, 1600);
858 return 2 + DIV_ROUND_CLOSEST((1600 - val
) * 100, 625);
861 static u16
pmbus_data2reg(struct pmbus_data
*data
,
862 struct pmbus_sensor
*sensor
, long val
)
866 if (!sensor
->convert
)
869 switch (data
->info
->format
[sensor
->class]) {
871 regval
= pmbus_data2reg_direct(data
, sensor
, val
);
874 regval
= pmbus_data2reg_vid(data
, sensor
, val
);
878 regval
= pmbus_data2reg_linear(data
, sensor
, val
);
885 * Return boolean calculated from converted data.
886 * <index> defines a status register index and mask.
887 * The mask is in the lower 8 bits, the register index is in bits 8..23.
889 * The associated pmbus_boolean structure contains optional pointers to two
890 * sensor attributes. If specified, those attributes are compared against each
891 * other to determine if a limit has been exceeded.
893 * If the sensor attribute pointers are NULL, the function returns true if
894 * (status[reg] & mask) is true.
896 * If sensor attribute pointers are provided, a comparison against a specified
897 * limit has to be performed to determine the boolean result.
898 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
899 * sensor values referenced by sensor attribute pointers s1 and s2).
901 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
902 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
904 * If a negative value is stored in any of the referenced registers, this value
905 * reflects an error code which will be returned.
907 static int pmbus_get_boolean(struct pmbus_data
*data
, struct pmbus_boolean
*b
,
910 struct pmbus_sensor
*s1
= b
->s1
;
911 struct pmbus_sensor
*s2
= b
->s2
;
912 u16 reg
= (index
>> 16) & 0xffff;
913 u16 mask
= index
& 0xffff;
917 status
= data
->status
[reg
];
921 regval
= status
& mask
;
924 } else if (!s1
|| !s2
) {
925 WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b
, s1
, s2
);
935 v1
= pmbus_reg2data(data
, s1
);
936 v2
= pmbus_reg2data(data
, s2
);
937 ret
= !!(regval
&& v1
>= v2
);
942 static ssize_t
pmbus_show_boolean(struct device
*dev
,
943 struct device_attribute
*da
, char *buf
)
945 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
946 struct pmbus_boolean
*boolean
= to_pmbus_boolean(attr
);
947 struct pmbus_data
*data
= pmbus_update_device(dev
);
950 val
= pmbus_get_boolean(data
, boolean
, attr
->index
);
953 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
956 static ssize_t
pmbus_show_sensor(struct device
*dev
,
957 struct device_attribute
*devattr
, char *buf
)
959 struct pmbus_data
*data
= pmbus_update_device(dev
);
960 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
962 if (sensor
->data
< 0)
965 return snprintf(buf
, PAGE_SIZE
, "%ld\n", pmbus_reg2data(data
, sensor
));
968 static ssize_t
pmbus_set_sensor(struct device
*dev
,
969 struct device_attribute
*devattr
,
970 const char *buf
, size_t count
)
972 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
973 struct pmbus_data
*data
= i2c_get_clientdata(client
);
974 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
980 if (kstrtol(buf
, 10, &val
) < 0)
983 mutex_lock(&data
->update_lock
);
984 regval
= pmbus_data2reg(data
, sensor
, val
);
985 ret
= _pmbus_write_word_data(client
, sensor
->page
, sensor
->reg
, regval
);
989 sensor
->data
= regval
;
990 mutex_unlock(&data
->update_lock
);
994 static ssize_t
pmbus_show_label(struct device
*dev
,
995 struct device_attribute
*da
, char *buf
)
997 struct pmbus_label
*label
= to_pmbus_label(da
);
999 return snprintf(buf
, PAGE_SIZE
, "%s\n", label
->label
);
1002 static int pmbus_add_attribute(struct pmbus_data
*data
, struct attribute
*attr
)
1004 if (data
->num_attributes
>= data
->max_attributes
- 1) {
1005 int new_max_attrs
= data
->max_attributes
+ PMBUS_ATTR_ALLOC_SIZE
;
1006 void *new_attrs
= krealloc(data
->group
.attrs
,
1007 new_max_attrs
* sizeof(void *),
1011 data
->group
.attrs
= new_attrs
;
1012 data
->max_attributes
= new_max_attrs
;
1015 data
->group
.attrs
[data
->num_attributes
++] = attr
;
1016 data
->group
.attrs
[data
->num_attributes
] = NULL
;
1020 static void pmbus_dev_attr_init(struct device_attribute
*dev_attr
,
1023 ssize_t (*show
)(struct device
*dev
,
1024 struct device_attribute
*attr
,
1026 ssize_t (*store
)(struct device
*dev
,
1027 struct device_attribute
*attr
,
1028 const char *buf
, size_t count
))
1030 sysfs_attr_init(&dev_attr
->attr
);
1031 dev_attr
->attr
.name
= name
;
1032 dev_attr
->attr
.mode
= mode
;
1033 dev_attr
->show
= show
;
1034 dev_attr
->store
= store
;
1037 static void pmbus_attr_init(struct sensor_device_attribute
*a
,
1040 ssize_t (*show
)(struct device
*dev
,
1041 struct device_attribute
*attr
,
1043 ssize_t (*store
)(struct device
*dev
,
1044 struct device_attribute
*attr
,
1045 const char *buf
, size_t count
),
1048 pmbus_dev_attr_init(&a
->dev_attr
, name
, mode
, show
, store
);
1052 static int pmbus_add_boolean(struct pmbus_data
*data
,
1053 const char *name
, const char *type
, int seq
,
1054 struct pmbus_sensor
*s1
,
1055 struct pmbus_sensor
*s2
,
1058 struct pmbus_boolean
*boolean
;
1059 struct sensor_device_attribute
*a
;
1061 boolean
= devm_kzalloc(data
->dev
, sizeof(*boolean
), GFP_KERNEL
);
1065 a
= &boolean
->attribute
;
1067 snprintf(boolean
->name
, sizeof(boolean
->name
), "%s%d_%s",
1071 pmbus_attr_init(a
, boolean
->name
, 0444, pmbus_show_boolean
, NULL
,
1072 (reg
<< 16) | mask
);
1074 return pmbus_add_attribute(data
, &a
->dev_attr
.attr
);
1077 static struct pmbus_sensor
*pmbus_add_sensor(struct pmbus_data
*data
,
1078 const char *name
, const char *type
,
1079 int seq
, int page
, int reg
,
1080 enum pmbus_sensor_classes
class,
1081 bool update
, bool readonly
,
1084 struct pmbus_sensor
*sensor
;
1085 struct device_attribute
*a
;
1087 sensor
= devm_kzalloc(data
->dev
, sizeof(*sensor
), GFP_KERNEL
);
1090 a
= &sensor
->attribute
;
1093 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d_%s",
1096 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d",
1099 if (data
->flags
& PMBUS_WRITE_PROTECTED
)
1102 sensor
->page
= page
;
1104 sensor
->class = class;
1105 sensor
->update
= update
;
1106 sensor
->convert
= convert
;
1107 pmbus_dev_attr_init(a
, sensor
->name
,
1108 readonly
? 0444 : 0644,
1109 pmbus_show_sensor
, pmbus_set_sensor
);
1111 if (pmbus_add_attribute(data
, &a
->attr
))
1114 sensor
->next
= data
->sensors
;
1115 data
->sensors
= sensor
;
1120 static int pmbus_add_label(struct pmbus_data
*data
,
1121 const char *name
, int seq
,
1122 const char *lstring
, int index
)
1124 struct pmbus_label
*label
;
1125 struct device_attribute
*a
;
1127 label
= devm_kzalloc(data
->dev
, sizeof(*label
), GFP_KERNEL
);
1131 a
= &label
->attribute
;
1133 snprintf(label
->name
, sizeof(label
->name
), "%s%d_label", name
, seq
);
1135 strncpy(label
->label
, lstring
, sizeof(label
->label
) - 1);
1137 snprintf(label
->label
, sizeof(label
->label
), "%s%d", lstring
,
1140 pmbus_dev_attr_init(a
, label
->name
, 0444, pmbus_show_label
, NULL
);
1141 return pmbus_add_attribute(data
, &a
->attr
);
1145 * Search for attributes. Allocate sensors, booleans, and labels as needed.
1149 * The pmbus_limit_attr structure describes a single limit attribute
1150 * and its associated alarm attribute.
1152 struct pmbus_limit_attr
{
1153 u16 reg
; /* Limit register */
1154 u16 sbit
; /* Alarm attribute status bit */
1155 bool update
; /* True if register needs updates */
1156 bool low
; /* True if low limit; for limits with compare
1158 const char *attr
; /* Attribute name */
1159 const char *alarm
; /* Alarm attribute name */
1163 * The pmbus_sensor_attr structure describes one sensor attribute. This
1164 * description includes a reference to the associated limit attributes.
1166 struct pmbus_sensor_attr
{
1167 u16 reg
; /* sensor register */
1168 u16 gbit
; /* generic status bit */
1169 u8 nlimit
; /* # of limit registers */
1170 enum pmbus_sensor_classes
class;/* sensor class */
1171 const char *label
; /* sensor label */
1172 bool paged
; /* true if paged sensor */
1173 bool update
; /* true if update needed */
1174 bool compare
; /* true if compare function needed */
1175 u32 func
; /* sensor mask */
1176 u32 sfunc
; /* sensor status mask */
1177 int sbase
; /* status base register */
1178 const struct pmbus_limit_attr
*limit
;/* limit registers */
1182 * Add a set of limit attributes and, if supported, the associated
1184 * returns 0 if no alarm register found, 1 if an alarm register was found,
1187 static int pmbus_add_limit_attrs(struct i2c_client
*client
,
1188 struct pmbus_data
*data
,
1189 const struct pmbus_driver_info
*info
,
1190 const char *name
, int index
, int page
,
1191 struct pmbus_sensor
*base
,
1192 const struct pmbus_sensor_attr
*attr
)
1194 const struct pmbus_limit_attr
*l
= attr
->limit
;
1195 int nlimit
= attr
->nlimit
;
1198 struct pmbus_sensor
*curr
;
1200 for (i
= 0; i
< nlimit
; i
++) {
1201 if (pmbus_check_word_register(client
, page
, l
->reg
)) {
1202 curr
= pmbus_add_sensor(data
, name
, l
->attr
, index
,
1203 page
, l
->reg
, attr
->class,
1204 attr
->update
|| l
->update
,
1208 if (l
->sbit
&& (info
->func
[page
] & attr
->sfunc
)) {
1209 ret
= pmbus_add_boolean(data
, name
,
1211 attr
->compare
? l
->low
? curr
: base
1213 attr
->compare
? l
->low
? base
: curr
1215 attr
->sbase
+ page
, l
->sbit
);
1226 static int pmbus_add_sensor_attrs_one(struct i2c_client
*client
,
1227 struct pmbus_data
*data
,
1228 const struct pmbus_driver_info
*info
,
1230 int index
, int page
,
1231 const struct pmbus_sensor_attr
*attr
,
1234 struct pmbus_sensor
*base
;
1235 bool upper
= !!(attr
->gbit
& 0xff00); /* need to check STATUS_WORD */
1239 ret
= pmbus_add_label(data
, name
, index
, attr
->label
,
1240 paged
? page
+ 1 : 0);
1244 base
= pmbus_add_sensor(data
, name
, "input", index
, page
, attr
->reg
,
1245 attr
->class, true, true, true);
1249 ret
= pmbus_add_limit_attrs(client
, data
, info
, name
,
1250 index
, page
, base
, attr
);
1254 * Add generic alarm attribute only if there are no individual
1255 * alarm attributes, if there is a global alarm bit, and if
1256 * the generic status register (word or byte, depending on
1257 * which global bit is set) for this page is accessible.
1259 if (!ret
&& attr
->gbit
&&
1260 (!upper
|| (upper
&& data
->has_status_word
)) &&
1261 pmbus_check_status_register(client
, page
)) {
1262 ret
= pmbus_add_boolean(data
, name
, "alarm", index
,
1264 PB_STATUS_BASE
+ page
,
1273 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info
*info
,
1274 const struct pmbus_sensor_attr
*attr
)
1282 * Some attributes may be present on more than one page despite
1283 * not being marked with the paged attribute. If that is the case,
1284 * then treat the sensor as being paged and add the page suffix to the
1286 * We don't just add the paged attribute to all such attributes, in
1287 * order to maintain the un-suffixed labels in the case where the
1288 * attribute is only on page 0.
1290 for (p
= 1; p
< info
->pages
; p
++) {
1291 if (info
->func
[p
] & attr
->func
)
1297 static int pmbus_add_sensor_attrs(struct i2c_client
*client
,
1298 struct pmbus_data
*data
,
1300 const struct pmbus_sensor_attr
*attrs
,
1303 const struct pmbus_driver_info
*info
= data
->info
;
1308 for (i
= 0; i
< nattrs
; i
++) {
1310 bool paged
= pmbus_sensor_is_paged(info
, attrs
);
1312 pages
= paged
? info
->pages
: 1;
1313 for (page
= 0; page
< pages
; page
++) {
1314 if (!(info
->func
[page
] & attrs
->func
))
1316 ret
= pmbus_add_sensor_attrs_one(client
, data
, info
,
1328 static const struct pmbus_limit_attr vin_limit_attrs
[] = {
1330 .reg
= PMBUS_VIN_UV_WARN_LIMIT
,
1332 .alarm
= "min_alarm",
1333 .sbit
= PB_VOLTAGE_UV_WARNING
,
1335 .reg
= PMBUS_VIN_UV_FAULT_LIMIT
,
1337 .alarm
= "lcrit_alarm",
1338 .sbit
= PB_VOLTAGE_UV_FAULT
,
1340 .reg
= PMBUS_VIN_OV_WARN_LIMIT
,
1342 .alarm
= "max_alarm",
1343 .sbit
= PB_VOLTAGE_OV_WARNING
,
1345 .reg
= PMBUS_VIN_OV_FAULT_LIMIT
,
1347 .alarm
= "crit_alarm",
1348 .sbit
= PB_VOLTAGE_OV_FAULT
,
1350 .reg
= PMBUS_VIRT_READ_VIN_AVG
,
1354 .reg
= PMBUS_VIRT_READ_VIN_MIN
,
1358 .reg
= PMBUS_VIRT_READ_VIN_MAX
,
1362 .reg
= PMBUS_VIRT_RESET_VIN_HISTORY
,
1363 .attr
= "reset_history",
1367 static const struct pmbus_limit_attr vmon_limit_attrs
[] = {
1369 .reg
= PMBUS_VIRT_VMON_UV_WARN_LIMIT
,
1371 .alarm
= "min_alarm",
1372 .sbit
= PB_VOLTAGE_UV_WARNING
,
1374 .reg
= PMBUS_VIRT_VMON_UV_FAULT_LIMIT
,
1376 .alarm
= "lcrit_alarm",
1377 .sbit
= PB_VOLTAGE_UV_FAULT
,
1379 .reg
= PMBUS_VIRT_VMON_OV_WARN_LIMIT
,
1381 .alarm
= "max_alarm",
1382 .sbit
= PB_VOLTAGE_OV_WARNING
,
1384 .reg
= PMBUS_VIRT_VMON_OV_FAULT_LIMIT
,
1386 .alarm
= "crit_alarm",
1387 .sbit
= PB_VOLTAGE_OV_FAULT
,
1391 static const struct pmbus_limit_attr vout_limit_attrs
[] = {
1393 .reg
= PMBUS_VOUT_UV_WARN_LIMIT
,
1395 .alarm
= "min_alarm",
1396 .sbit
= PB_VOLTAGE_UV_WARNING
,
1398 .reg
= PMBUS_VOUT_UV_FAULT_LIMIT
,
1400 .alarm
= "lcrit_alarm",
1401 .sbit
= PB_VOLTAGE_UV_FAULT
,
1403 .reg
= PMBUS_VOUT_OV_WARN_LIMIT
,
1405 .alarm
= "max_alarm",
1406 .sbit
= PB_VOLTAGE_OV_WARNING
,
1408 .reg
= PMBUS_VOUT_OV_FAULT_LIMIT
,
1410 .alarm
= "crit_alarm",
1411 .sbit
= PB_VOLTAGE_OV_FAULT
,
1413 .reg
= PMBUS_VIRT_READ_VOUT_AVG
,
1417 .reg
= PMBUS_VIRT_READ_VOUT_MIN
,
1421 .reg
= PMBUS_VIRT_READ_VOUT_MAX
,
1425 .reg
= PMBUS_VIRT_RESET_VOUT_HISTORY
,
1426 .attr
= "reset_history",
1430 static const struct pmbus_sensor_attr voltage_attributes
[] = {
1432 .reg
= PMBUS_READ_VIN
,
1433 .class = PSC_VOLTAGE_IN
,
1435 .func
= PMBUS_HAVE_VIN
,
1436 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1437 .sbase
= PB_STATUS_INPUT_BASE
,
1438 .gbit
= PB_STATUS_VIN_UV
,
1439 .limit
= vin_limit_attrs
,
1440 .nlimit
= ARRAY_SIZE(vin_limit_attrs
),
1442 .reg
= PMBUS_VIRT_READ_VMON
,
1443 .class = PSC_VOLTAGE_IN
,
1445 .func
= PMBUS_HAVE_VMON
,
1446 .sfunc
= PMBUS_HAVE_STATUS_VMON
,
1447 .sbase
= PB_STATUS_VMON_BASE
,
1448 .limit
= vmon_limit_attrs
,
1449 .nlimit
= ARRAY_SIZE(vmon_limit_attrs
),
1451 .reg
= PMBUS_READ_VCAP
,
1452 .class = PSC_VOLTAGE_IN
,
1454 .func
= PMBUS_HAVE_VCAP
,
1456 .reg
= PMBUS_READ_VOUT
,
1457 .class = PSC_VOLTAGE_OUT
,
1460 .func
= PMBUS_HAVE_VOUT
,
1461 .sfunc
= PMBUS_HAVE_STATUS_VOUT
,
1462 .sbase
= PB_STATUS_VOUT_BASE
,
1463 .gbit
= PB_STATUS_VOUT_OV
,
1464 .limit
= vout_limit_attrs
,
1465 .nlimit
= ARRAY_SIZE(vout_limit_attrs
),
1469 /* Current attributes */
1471 static const struct pmbus_limit_attr iin_limit_attrs
[] = {
1473 .reg
= PMBUS_IIN_OC_WARN_LIMIT
,
1475 .alarm
= "max_alarm",
1476 .sbit
= PB_IIN_OC_WARNING
,
1478 .reg
= PMBUS_IIN_OC_FAULT_LIMIT
,
1480 .alarm
= "crit_alarm",
1481 .sbit
= PB_IIN_OC_FAULT
,
1483 .reg
= PMBUS_VIRT_READ_IIN_AVG
,
1487 .reg
= PMBUS_VIRT_READ_IIN_MIN
,
1491 .reg
= PMBUS_VIRT_READ_IIN_MAX
,
1495 .reg
= PMBUS_VIRT_RESET_IIN_HISTORY
,
1496 .attr
= "reset_history",
1500 static const struct pmbus_limit_attr iout_limit_attrs
[] = {
1502 .reg
= PMBUS_IOUT_OC_WARN_LIMIT
,
1504 .alarm
= "max_alarm",
1505 .sbit
= PB_IOUT_OC_WARNING
,
1507 .reg
= PMBUS_IOUT_UC_FAULT_LIMIT
,
1509 .alarm
= "lcrit_alarm",
1510 .sbit
= PB_IOUT_UC_FAULT
,
1512 .reg
= PMBUS_IOUT_OC_FAULT_LIMIT
,
1514 .alarm
= "crit_alarm",
1515 .sbit
= PB_IOUT_OC_FAULT
,
1517 .reg
= PMBUS_VIRT_READ_IOUT_AVG
,
1521 .reg
= PMBUS_VIRT_READ_IOUT_MIN
,
1525 .reg
= PMBUS_VIRT_READ_IOUT_MAX
,
1529 .reg
= PMBUS_VIRT_RESET_IOUT_HISTORY
,
1530 .attr
= "reset_history",
1534 static const struct pmbus_sensor_attr current_attributes
[] = {
1536 .reg
= PMBUS_READ_IIN
,
1537 .class = PSC_CURRENT_IN
,
1539 .func
= PMBUS_HAVE_IIN
,
1540 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1541 .sbase
= PB_STATUS_INPUT_BASE
,
1542 .gbit
= PB_STATUS_INPUT
,
1543 .limit
= iin_limit_attrs
,
1544 .nlimit
= ARRAY_SIZE(iin_limit_attrs
),
1546 .reg
= PMBUS_READ_IOUT
,
1547 .class = PSC_CURRENT_OUT
,
1550 .func
= PMBUS_HAVE_IOUT
,
1551 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1552 .sbase
= PB_STATUS_IOUT_BASE
,
1553 .gbit
= PB_STATUS_IOUT_OC
,
1554 .limit
= iout_limit_attrs
,
1555 .nlimit
= ARRAY_SIZE(iout_limit_attrs
),
1559 /* Power attributes */
1561 static const struct pmbus_limit_attr pin_limit_attrs
[] = {
1563 .reg
= PMBUS_PIN_OP_WARN_LIMIT
,
1566 .sbit
= PB_PIN_OP_WARNING
,
1568 .reg
= PMBUS_VIRT_READ_PIN_AVG
,
1572 .reg
= PMBUS_VIRT_READ_PIN_MIN
,
1574 .attr
= "input_lowest",
1576 .reg
= PMBUS_VIRT_READ_PIN_MAX
,
1578 .attr
= "input_highest",
1580 .reg
= PMBUS_VIRT_RESET_PIN_HISTORY
,
1581 .attr
= "reset_history",
1585 static const struct pmbus_limit_attr pout_limit_attrs
[] = {
1587 .reg
= PMBUS_POUT_MAX
,
1589 .alarm
= "cap_alarm",
1590 .sbit
= PB_POWER_LIMITING
,
1592 .reg
= PMBUS_POUT_OP_WARN_LIMIT
,
1594 .alarm
= "max_alarm",
1595 .sbit
= PB_POUT_OP_WARNING
,
1597 .reg
= PMBUS_POUT_OP_FAULT_LIMIT
,
1599 .alarm
= "crit_alarm",
1600 .sbit
= PB_POUT_OP_FAULT
,
1602 .reg
= PMBUS_VIRT_READ_POUT_AVG
,
1606 .reg
= PMBUS_VIRT_READ_POUT_MIN
,
1608 .attr
= "input_lowest",
1610 .reg
= PMBUS_VIRT_READ_POUT_MAX
,
1612 .attr
= "input_highest",
1614 .reg
= PMBUS_VIRT_RESET_POUT_HISTORY
,
1615 .attr
= "reset_history",
1619 static const struct pmbus_sensor_attr power_attributes
[] = {
1621 .reg
= PMBUS_READ_PIN
,
1624 .func
= PMBUS_HAVE_PIN
,
1625 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1626 .sbase
= PB_STATUS_INPUT_BASE
,
1627 .gbit
= PB_STATUS_INPUT
,
1628 .limit
= pin_limit_attrs
,
1629 .nlimit
= ARRAY_SIZE(pin_limit_attrs
),
1631 .reg
= PMBUS_READ_POUT
,
1635 .func
= PMBUS_HAVE_POUT
,
1636 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1637 .sbase
= PB_STATUS_IOUT_BASE
,
1638 .limit
= pout_limit_attrs
,
1639 .nlimit
= ARRAY_SIZE(pout_limit_attrs
),
1643 /* Temperature atributes */
1645 static const struct pmbus_limit_attr temp_limit_attrs
[] = {
1647 .reg
= PMBUS_UT_WARN_LIMIT
,
1650 .alarm
= "min_alarm",
1651 .sbit
= PB_TEMP_UT_WARNING
,
1653 .reg
= PMBUS_UT_FAULT_LIMIT
,
1656 .alarm
= "lcrit_alarm",
1657 .sbit
= PB_TEMP_UT_FAULT
,
1659 .reg
= PMBUS_OT_WARN_LIMIT
,
1661 .alarm
= "max_alarm",
1662 .sbit
= PB_TEMP_OT_WARNING
,
1664 .reg
= PMBUS_OT_FAULT_LIMIT
,
1666 .alarm
= "crit_alarm",
1667 .sbit
= PB_TEMP_OT_FAULT
,
1669 .reg
= PMBUS_VIRT_READ_TEMP_MIN
,
1672 .reg
= PMBUS_VIRT_READ_TEMP_AVG
,
1675 .reg
= PMBUS_VIRT_READ_TEMP_MAX
,
1678 .reg
= PMBUS_VIRT_RESET_TEMP_HISTORY
,
1679 .attr
= "reset_history",
1683 static const struct pmbus_limit_attr temp_limit_attrs2
[] = {
1685 .reg
= PMBUS_UT_WARN_LIMIT
,
1688 .alarm
= "min_alarm",
1689 .sbit
= PB_TEMP_UT_WARNING
,
1691 .reg
= PMBUS_UT_FAULT_LIMIT
,
1694 .alarm
= "lcrit_alarm",
1695 .sbit
= PB_TEMP_UT_FAULT
,
1697 .reg
= PMBUS_OT_WARN_LIMIT
,
1699 .alarm
= "max_alarm",
1700 .sbit
= PB_TEMP_OT_WARNING
,
1702 .reg
= PMBUS_OT_FAULT_LIMIT
,
1704 .alarm
= "crit_alarm",
1705 .sbit
= PB_TEMP_OT_FAULT
,
1707 .reg
= PMBUS_VIRT_READ_TEMP2_MIN
,
1710 .reg
= PMBUS_VIRT_READ_TEMP2_AVG
,
1713 .reg
= PMBUS_VIRT_READ_TEMP2_MAX
,
1716 .reg
= PMBUS_VIRT_RESET_TEMP2_HISTORY
,
1717 .attr
= "reset_history",
1721 static const struct pmbus_limit_attr temp_limit_attrs3
[] = {
1723 .reg
= PMBUS_UT_WARN_LIMIT
,
1726 .alarm
= "min_alarm",
1727 .sbit
= PB_TEMP_UT_WARNING
,
1729 .reg
= PMBUS_UT_FAULT_LIMIT
,
1732 .alarm
= "lcrit_alarm",
1733 .sbit
= PB_TEMP_UT_FAULT
,
1735 .reg
= PMBUS_OT_WARN_LIMIT
,
1737 .alarm
= "max_alarm",
1738 .sbit
= PB_TEMP_OT_WARNING
,
1740 .reg
= PMBUS_OT_FAULT_LIMIT
,
1742 .alarm
= "crit_alarm",
1743 .sbit
= PB_TEMP_OT_FAULT
,
1747 static const struct pmbus_sensor_attr temp_attributes
[] = {
1749 .reg
= PMBUS_READ_TEMPERATURE_1
,
1750 .class = PSC_TEMPERATURE
,
1754 .func
= PMBUS_HAVE_TEMP
,
1755 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1756 .sbase
= PB_STATUS_TEMP_BASE
,
1757 .gbit
= PB_STATUS_TEMPERATURE
,
1758 .limit
= temp_limit_attrs
,
1759 .nlimit
= ARRAY_SIZE(temp_limit_attrs
),
1761 .reg
= PMBUS_READ_TEMPERATURE_2
,
1762 .class = PSC_TEMPERATURE
,
1766 .func
= PMBUS_HAVE_TEMP2
,
1767 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1768 .sbase
= PB_STATUS_TEMP_BASE
,
1769 .gbit
= PB_STATUS_TEMPERATURE
,
1770 .limit
= temp_limit_attrs2
,
1771 .nlimit
= ARRAY_SIZE(temp_limit_attrs2
),
1773 .reg
= PMBUS_READ_TEMPERATURE_3
,
1774 .class = PSC_TEMPERATURE
,
1778 .func
= PMBUS_HAVE_TEMP3
,
1779 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1780 .sbase
= PB_STATUS_TEMP_BASE
,
1781 .gbit
= PB_STATUS_TEMPERATURE
,
1782 .limit
= temp_limit_attrs3
,
1783 .nlimit
= ARRAY_SIZE(temp_limit_attrs3
),
1787 static const int pmbus_fan_registers
[] = {
1788 PMBUS_READ_FAN_SPEED_1
,
1789 PMBUS_READ_FAN_SPEED_2
,
1790 PMBUS_READ_FAN_SPEED_3
,
1791 PMBUS_READ_FAN_SPEED_4
1794 static const int pmbus_fan_status_registers
[] = {
1795 PMBUS_STATUS_FAN_12
,
1796 PMBUS_STATUS_FAN_12
,
1797 PMBUS_STATUS_FAN_34
,
1801 static const u32 pmbus_fan_flags
[] = {
1808 static const u32 pmbus_fan_status_flags
[] = {
1809 PMBUS_HAVE_STATUS_FAN12
,
1810 PMBUS_HAVE_STATUS_FAN12
,
1811 PMBUS_HAVE_STATUS_FAN34
,
1812 PMBUS_HAVE_STATUS_FAN34
1817 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */
1818 static int pmbus_add_fan_ctrl(struct i2c_client
*client
,
1819 struct pmbus_data
*data
, int index
, int page
, int id
,
1822 struct pmbus_sensor
*sensor
;
1824 sensor
= pmbus_add_sensor(data
, "fan", "target", index
, page
,
1825 PMBUS_VIRT_FAN_TARGET_1
+ id
, PSC_FAN
,
1826 false, false, true);
1831 if (!((data
->info
->func
[page
] & PMBUS_HAVE_PWM12
) ||
1832 (data
->info
->func
[page
] & PMBUS_HAVE_PWM34
)))
1835 sensor
= pmbus_add_sensor(data
, "pwm", NULL
, index
, page
,
1836 PMBUS_VIRT_PWM_1
+ id
, PSC_PWM
,
1837 false, false, true);
1842 sensor
= pmbus_add_sensor(data
, "pwm", "enable", index
, page
,
1843 PMBUS_VIRT_PWM_ENABLE_1
+ id
, PSC_PWM
,
1844 true, false, false);
1852 static int pmbus_add_fan_attributes(struct i2c_client
*client
,
1853 struct pmbus_data
*data
)
1855 const struct pmbus_driver_info
*info
= data
->info
;
1860 for (page
= 0; page
< info
->pages
; page
++) {
1863 for (f
= 0; f
< ARRAY_SIZE(pmbus_fan_registers
); f
++) {
1866 if (!(info
->func
[page
] & pmbus_fan_flags
[f
]))
1869 if (!pmbus_check_word_register(client
, page
,
1870 pmbus_fan_registers
[f
]))
1874 * Skip fan if not installed.
1875 * Each fan configuration register covers multiple fans,
1876 * so we have to do some magic.
1878 regval
= _pmbus_read_byte_data(client
, page
,
1879 pmbus_fan_config_registers
[f
]);
1881 (!(regval
& (PB_FAN_1_INSTALLED
>> ((f
& 1) * 4)))))
1884 if (pmbus_add_sensor(data
, "fan", "input", index
,
1885 page
, pmbus_fan_registers
[f
],
1886 PSC_FAN
, true, true, true) == NULL
)
1890 if (pmbus_check_word_register(client
, page
,
1891 pmbus_fan_command_registers
[f
])) {
1892 ret
= pmbus_add_fan_ctrl(client
, data
, index
,
1899 * Each fan status register covers multiple fans,
1900 * so we have to do some magic.
1902 if ((info
->func
[page
] & pmbus_fan_status_flags
[f
]) &&
1903 pmbus_check_byte_register(client
,
1904 page
, pmbus_fan_status_registers
[f
])) {
1907 if (f
> 1) /* fan 3, 4 */
1908 base
= PB_STATUS_FAN34_BASE
+ page
;
1910 base
= PB_STATUS_FAN_BASE
+ page
;
1911 ret
= pmbus_add_boolean(data
, "fan",
1912 "alarm", index
, NULL
, NULL
, base
,
1913 PB_FAN_FAN1_WARNING
>> (f
& 1));
1916 ret
= pmbus_add_boolean(data
, "fan",
1917 "fault", index
, NULL
, NULL
, base
,
1918 PB_FAN_FAN1_FAULT
>> (f
& 1));
1928 struct pmbus_samples_attr
{
1933 struct pmbus_samples_reg
{
1935 struct pmbus_samples_attr
*attr
;
1936 struct device_attribute dev_attr
;
1939 static struct pmbus_samples_attr pmbus_samples_registers
[] = {
1941 .reg
= PMBUS_VIRT_SAMPLES
,
1944 .reg
= PMBUS_VIRT_IN_SAMPLES
,
1945 .name
= "in_samples",
1947 .reg
= PMBUS_VIRT_CURR_SAMPLES
,
1948 .name
= "curr_samples",
1950 .reg
= PMBUS_VIRT_POWER_SAMPLES
,
1951 .name
= "power_samples",
1953 .reg
= PMBUS_VIRT_TEMP_SAMPLES
,
1954 .name
= "temp_samples",
1958 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr)
1960 static ssize_t
pmbus_show_samples(struct device
*dev
,
1961 struct device_attribute
*devattr
, char *buf
)
1964 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
1965 struct pmbus_samples_reg
*reg
= to_samples_reg(devattr
);
1967 val
= _pmbus_read_word_data(client
, reg
->page
, reg
->attr
->reg
);
1971 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
1974 static ssize_t
pmbus_set_samples(struct device
*dev
,
1975 struct device_attribute
*devattr
,
1976 const char *buf
, size_t count
)
1980 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
1981 struct pmbus_samples_reg
*reg
= to_samples_reg(devattr
);
1982 struct pmbus_data
*data
= i2c_get_clientdata(client
);
1984 if (kstrtol(buf
, 0, &val
) < 0)
1987 mutex_lock(&data
->update_lock
);
1988 ret
= _pmbus_write_word_data(client
, reg
->page
, reg
->attr
->reg
, val
);
1989 mutex_unlock(&data
->update_lock
);
1991 return ret
? : count
;
1994 static int pmbus_add_samples_attr(struct pmbus_data
*data
, int page
,
1995 struct pmbus_samples_attr
*attr
)
1997 struct pmbus_samples_reg
*reg
;
1999 reg
= devm_kzalloc(data
->dev
, sizeof(*reg
), GFP_KERNEL
);
2006 pmbus_dev_attr_init(®
->dev_attr
, attr
->name
, 0644,
2007 pmbus_show_samples
, pmbus_set_samples
);
2009 return pmbus_add_attribute(data
, ®
->dev_attr
.attr
);
2012 static int pmbus_add_samples_attributes(struct i2c_client
*client
,
2013 struct pmbus_data
*data
)
2015 const struct pmbus_driver_info
*info
= data
->info
;
2018 if (!(info
->func
[0] & PMBUS_HAVE_SAMPLES
))
2021 for (s
= 0; s
< ARRAY_SIZE(pmbus_samples_registers
); s
++) {
2022 struct pmbus_samples_attr
*attr
;
2025 attr
= &pmbus_samples_registers
[s
];
2026 if (!pmbus_check_word_register(client
, 0, attr
->reg
))
2029 ret
= pmbus_add_samples_attr(data
, 0, attr
);
2037 static int pmbus_find_attributes(struct i2c_client
*client
,
2038 struct pmbus_data
*data
)
2042 /* Voltage sensors */
2043 ret
= pmbus_add_sensor_attrs(client
, data
, "in", voltage_attributes
,
2044 ARRAY_SIZE(voltage_attributes
));
2048 /* Current sensors */
2049 ret
= pmbus_add_sensor_attrs(client
, data
, "curr", current_attributes
,
2050 ARRAY_SIZE(current_attributes
));
2055 ret
= pmbus_add_sensor_attrs(client
, data
, "power", power_attributes
,
2056 ARRAY_SIZE(power_attributes
));
2060 /* Temperature sensors */
2061 ret
= pmbus_add_sensor_attrs(client
, data
, "temp", temp_attributes
,
2062 ARRAY_SIZE(temp_attributes
));
2067 ret
= pmbus_add_fan_attributes(client
, data
);
2071 ret
= pmbus_add_samples_attributes(client
, data
);
2076 * Identify chip parameters.
2077 * This function is called for all chips.
2079 static int pmbus_identify_common(struct i2c_client
*client
,
2080 struct pmbus_data
*data
, int page
)
2084 if (pmbus_check_byte_register(client
, page
, PMBUS_VOUT_MODE
))
2085 vout_mode
= _pmbus_read_byte_data(client
, page
,
2087 if (vout_mode
>= 0 && vout_mode
!= 0xff) {
2089 * Not all chips support the VOUT_MODE command,
2090 * so a failure to read it is not an error.
2092 switch (vout_mode
>> 5) {
2093 case 0: /* linear mode */
2094 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != linear
)
2097 data
->exponent
[page
] = ((s8
)(vout_mode
<< 3)) >> 3;
2099 case 1: /* VID mode */
2100 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != vid
)
2103 case 2: /* direct mode */
2104 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != direct
)
2112 pmbus_clear_fault_page(client
, page
);
2116 static int pmbus_read_status_byte(struct i2c_client
*client
, int page
)
2118 return _pmbus_read_byte_data(client
, page
, PMBUS_STATUS_BYTE
);
2121 static int pmbus_read_status_word(struct i2c_client
*client
, int page
)
2123 return _pmbus_read_word_data(client
, page
, PMBUS_STATUS_WORD
);
2126 static int pmbus_init_common(struct i2c_client
*client
, struct pmbus_data
*data
,
2127 struct pmbus_driver_info
*info
)
2129 struct device
*dev
= &client
->dev
;
2133 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
2134 * to use PMBUS_STATUS_BYTE instead if that is the case.
2135 * Bail out if both registers are not supported.
2137 data
->read_status
= pmbus_read_status_word
;
2138 ret
= i2c_smbus_read_word_data(client
, PMBUS_STATUS_WORD
);
2139 if (ret
< 0 || ret
== 0xffff) {
2140 data
->read_status
= pmbus_read_status_byte
;
2141 ret
= i2c_smbus_read_byte_data(client
, PMBUS_STATUS_BYTE
);
2142 if (ret
< 0 || ret
== 0xff) {
2143 dev_err(dev
, "PMBus status register not found\n");
2147 data
->has_status_word
= true;
2150 /* Enable PEC if the controller supports it */
2151 ret
= i2c_smbus_read_byte_data(client
, PMBUS_CAPABILITY
);
2152 if (ret
>= 0 && (ret
& PB_CAPABILITY_ERROR_CHECK
))
2153 client
->flags
|= I2C_CLIENT_PEC
;
2156 * Check if the chip is write protected. If it is, we can not clear
2157 * faults, and we should not try it. Also, in that case, writes into
2158 * limit registers need to be disabled.
2160 ret
= i2c_smbus_read_byte_data(client
, PMBUS_WRITE_PROTECT
);
2161 if (ret
> 0 && (ret
& PB_WP_ANY
))
2162 data
->flags
|= PMBUS_WRITE_PROTECTED
| PMBUS_SKIP_STATUS_CHECK
;
2164 if (data
->info
->pages
)
2165 pmbus_clear_faults(client
);
2167 pmbus_clear_fault_page(client
, -1);
2169 if (info
->identify
) {
2170 ret
= (*info
->identify
)(client
, info
);
2172 dev_err(dev
, "Chip identification failed\n");
2177 if (info
->pages
<= 0 || info
->pages
> PMBUS_PAGES
) {
2178 dev_err(dev
, "Bad number of PMBus pages: %d\n", info
->pages
);
2182 for (page
= 0; page
< info
->pages
; page
++) {
2183 ret
= pmbus_identify_common(client
, data
, page
);
2185 dev_err(dev
, "Failed to identify chip capabilities\n");
2192 #if IS_ENABLED(CONFIG_REGULATOR)
2193 static int pmbus_regulator_is_enabled(struct regulator_dev
*rdev
)
2195 struct device
*dev
= rdev_get_dev(rdev
);
2196 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2197 u8 page
= rdev_get_id(rdev
);
2200 ret
= pmbus_read_byte_data(client
, page
, PMBUS_OPERATION
);
2204 return !!(ret
& PB_OPERATION_CONTROL_ON
);
2207 static int _pmbus_regulator_on_off(struct regulator_dev
*rdev
, bool enable
)
2209 struct device
*dev
= rdev_get_dev(rdev
);
2210 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2211 u8 page
= rdev_get_id(rdev
);
2213 return pmbus_update_byte_data(client
, page
, PMBUS_OPERATION
,
2214 PB_OPERATION_CONTROL_ON
,
2215 enable
? PB_OPERATION_CONTROL_ON
: 0);
2218 static int pmbus_regulator_enable(struct regulator_dev
*rdev
)
2220 return _pmbus_regulator_on_off(rdev
, 1);
2223 static int pmbus_regulator_disable(struct regulator_dev
*rdev
)
2225 return _pmbus_regulator_on_off(rdev
, 0);
2228 const struct regulator_ops pmbus_regulator_ops
= {
2229 .enable
= pmbus_regulator_enable
,
2230 .disable
= pmbus_regulator_disable
,
2231 .is_enabled
= pmbus_regulator_is_enabled
,
2233 EXPORT_SYMBOL_GPL(pmbus_regulator_ops
);
2235 static int pmbus_regulator_register(struct pmbus_data
*data
)
2237 struct device
*dev
= data
->dev
;
2238 const struct pmbus_driver_info
*info
= data
->info
;
2239 const struct pmbus_platform_data
*pdata
= dev_get_platdata(dev
);
2240 struct regulator_dev
*rdev
;
2243 for (i
= 0; i
< info
->num_regulators
; i
++) {
2244 struct regulator_config config
= { };
2247 config
.driver_data
= data
;
2249 if (pdata
&& pdata
->reg_init_data
)
2250 config
.init_data
= &pdata
->reg_init_data
[i
];
2252 rdev
= devm_regulator_register(dev
, &info
->reg_desc
[i
],
2255 dev_err(dev
, "Failed to register %s regulator\n",
2256 info
->reg_desc
[i
].name
);
2257 return PTR_ERR(rdev
);
2264 static int pmbus_regulator_register(struct pmbus_data
*data
)
2270 static struct dentry
*pmbus_debugfs_dir
; /* pmbus debugfs directory */
2272 #if IS_ENABLED(CONFIG_DEBUG_FS)
2273 static int pmbus_debugfs_get(void *data
, u64
*val
)
2276 struct pmbus_debugfs_entry
*entry
= data
;
2278 rc
= _pmbus_read_byte_data(entry
->client
, entry
->page
, entry
->reg
);
2286 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops
, pmbus_debugfs_get
, NULL
,
2289 static int pmbus_debugfs_get_status(void *data
, u64
*val
)
2292 struct pmbus_debugfs_entry
*entry
= data
;
2293 struct pmbus_data
*pdata
= i2c_get_clientdata(entry
->client
);
2295 rc
= pdata
->read_status(entry
->client
, entry
->page
);
2303 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status
, pmbus_debugfs_get_status
,
2304 NULL
, "0x%04llx\n");
2306 static int pmbus_init_debugfs(struct i2c_client
*client
,
2307 struct pmbus_data
*data
)
2310 char name
[PMBUS_NAME_SIZE
];
2311 struct pmbus_debugfs_entry
*entries
;
2313 if (!pmbus_debugfs_dir
)
2317 * Create the debugfs directory for this device. Use the hwmon device
2318 * name to avoid conflicts (hwmon numbers are globally unique).
2320 data
->debugfs
= debugfs_create_dir(dev_name(data
->hwmon_dev
),
2322 if (IS_ERR_OR_NULL(data
->debugfs
)) {
2323 data
->debugfs
= NULL
;
2327 /* Allocate the max possible entries we need. */
2328 entries
= devm_kcalloc(data
->dev
,
2329 data
->info
->pages
* 10, sizeof(*entries
),
2334 for (i
= 0; i
< data
->info
->pages
; ++i
) {
2335 /* Check accessibility of status register if it's not page 0 */
2336 if (!i
|| pmbus_check_status_register(client
, i
)) {
2337 /* No need to set reg as we have special read op. */
2338 entries
[idx
].client
= client
;
2339 entries
[idx
].page
= i
;
2340 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d", i
);
2341 debugfs_create_file(name
, 0444, data
->debugfs
,
2343 &pmbus_debugfs_ops_status
);
2346 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_VOUT
) {
2347 entries
[idx
].client
= client
;
2348 entries
[idx
].page
= i
;
2349 entries
[idx
].reg
= PMBUS_STATUS_VOUT
;
2350 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_vout", i
);
2351 debugfs_create_file(name
, 0444, data
->debugfs
,
2353 &pmbus_debugfs_ops
);
2356 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_IOUT
) {
2357 entries
[idx
].client
= client
;
2358 entries
[idx
].page
= i
;
2359 entries
[idx
].reg
= PMBUS_STATUS_IOUT
;
2360 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_iout", i
);
2361 debugfs_create_file(name
, 0444, data
->debugfs
,
2363 &pmbus_debugfs_ops
);
2366 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_INPUT
) {
2367 entries
[idx
].client
= client
;
2368 entries
[idx
].page
= i
;
2369 entries
[idx
].reg
= PMBUS_STATUS_INPUT
;
2370 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_input", i
);
2371 debugfs_create_file(name
, 0444, data
->debugfs
,
2373 &pmbus_debugfs_ops
);
2376 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_TEMP
) {
2377 entries
[idx
].client
= client
;
2378 entries
[idx
].page
= i
;
2379 entries
[idx
].reg
= PMBUS_STATUS_TEMPERATURE
;
2380 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_temp", i
);
2381 debugfs_create_file(name
, 0444, data
->debugfs
,
2383 &pmbus_debugfs_ops
);
2386 if (pmbus_check_byte_register(client
, i
, PMBUS_STATUS_CML
)) {
2387 entries
[idx
].client
= client
;
2388 entries
[idx
].page
= i
;
2389 entries
[idx
].reg
= PMBUS_STATUS_CML
;
2390 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_cml", i
);
2391 debugfs_create_file(name
, 0444, data
->debugfs
,
2393 &pmbus_debugfs_ops
);
2396 if (pmbus_check_byte_register(client
, i
, PMBUS_STATUS_OTHER
)) {
2397 entries
[idx
].client
= client
;
2398 entries
[idx
].page
= i
;
2399 entries
[idx
].reg
= PMBUS_STATUS_OTHER
;
2400 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_other", i
);
2401 debugfs_create_file(name
, 0444, data
->debugfs
,
2403 &pmbus_debugfs_ops
);
2406 if (pmbus_check_byte_register(client
, i
,
2407 PMBUS_STATUS_MFR_SPECIFIC
)) {
2408 entries
[idx
].client
= client
;
2409 entries
[idx
].page
= i
;
2410 entries
[idx
].reg
= PMBUS_STATUS_MFR_SPECIFIC
;
2411 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_mfr", i
);
2412 debugfs_create_file(name
, 0444, data
->debugfs
,
2414 &pmbus_debugfs_ops
);
2417 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_FAN12
) {
2418 entries
[idx
].client
= client
;
2419 entries
[idx
].page
= i
;
2420 entries
[idx
].reg
= PMBUS_STATUS_FAN_12
;
2421 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_fan12", i
);
2422 debugfs_create_file(name
, 0444, data
->debugfs
,
2424 &pmbus_debugfs_ops
);
2427 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_FAN34
) {
2428 entries
[idx
].client
= client
;
2429 entries
[idx
].page
= i
;
2430 entries
[idx
].reg
= PMBUS_STATUS_FAN_34
;
2431 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_fan34", i
);
2432 debugfs_create_file(name
, 0444, data
->debugfs
,
2434 &pmbus_debugfs_ops
);
2441 static int pmbus_init_debugfs(struct i2c_client
*client
,
2442 struct pmbus_data
*data
)
2446 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */
2448 int pmbus_do_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
,
2449 struct pmbus_driver_info
*info
)
2451 struct device
*dev
= &client
->dev
;
2452 const struct pmbus_platform_data
*pdata
= dev_get_platdata(dev
);
2453 struct pmbus_data
*data
;
2454 size_t groups_num
= 0;
2460 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WRITE_BYTE
2461 | I2C_FUNC_SMBUS_BYTE_DATA
2462 | I2C_FUNC_SMBUS_WORD_DATA
))
2465 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
2470 while (info
->groups
[groups_num
])
2473 data
->groups
= devm_kcalloc(dev
, groups_num
+ 2, sizeof(void *),
2478 i2c_set_clientdata(client
, data
);
2479 mutex_init(&data
->update_lock
);
2483 data
->flags
= pdata
->flags
;
2486 ret
= pmbus_init_common(client
, data
, info
);
2490 ret
= pmbus_find_attributes(client
, data
);
2495 * If there are no attributes, something is wrong.
2496 * Bail out instead of trying to register nothing.
2498 if (!data
->num_attributes
) {
2499 dev_err(dev
, "No attributes found\n");
2504 data
->groups
[0] = &data
->group
;
2505 memcpy(data
->groups
+ 1, info
->groups
, sizeof(void *) * groups_num
);
2506 data
->hwmon_dev
= hwmon_device_register_with_groups(dev
, client
->name
,
2507 data
, data
->groups
);
2508 if (IS_ERR(data
->hwmon_dev
)) {
2509 ret
= PTR_ERR(data
->hwmon_dev
);
2510 dev_err(dev
, "Failed to register hwmon device\n");
2514 ret
= pmbus_regulator_register(data
);
2516 goto out_unregister
;
2518 ret
= pmbus_init_debugfs(client
, data
);
2520 dev_warn(dev
, "Failed to register debugfs\n");
2525 hwmon_device_unregister(data
->hwmon_dev
);
2527 kfree(data
->group
.attrs
);
2530 EXPORT_SYMBOL_GPL(pmbus_do_probe
);
2532 int pmbus_do_remove(struct i2c_client
*client
)
2534 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2536 debugfs_remove_recursive(data
->debugfs
);
2538 hwmon_device_unregister(data
->hwmon_dev
);
2539 kfree(data
->group
.attrs
);
2542 EXPORT_SYMBOL_GPL(pmbus_do_remove
);
2544 struct dentry
*pmbus_get_debugfs_dir(struct i2c_client
*client
)
2546 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2548 return data
->debugfs
;
2550 EXPORT_SYMBOL_GPL(pmbus_get_debugfs_dir
);
2552 static int __init
pmbus_core_init(void)
2554 pmbus_debugfs_dir
= debugfs_create_dir("pmbus", NULL
);
2555 if (IS_ERR(pmbus_debugfs_dir
))
2556 pmbus_debugfs_dir
= NULL
;
2561 static void __exit
pmbus_core_exit(void)
2563 debugfs_remove_recursive(pmbus_debugfs_dir
);
2566 module_init(pmbus_core_init
);
2567 module_exit(pmbus_core_exit
);
2569 MODULE_AUTHOR("Guenter Roeck");
2570 MODULE_DESCRIPTION("PMBus core driver");
2571 MODULE_LICENSE("GPL");