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/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/math64.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/i2c.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/pmbus.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
24 #include <linux/thermal.h>
28 * Number of additional attribute pointers to allocate
29 * with each call to krealloc
31 #define PMBUS_ATTR_ALLOC_SIZE 32
32 #define PMBUS_NAME_SIZE 24
35 struct pmbus_sensor
*next
;
36 char name
[PMBUS_NAME_SIZE
]; /* sysfs sensor name */
37 struct device_attribute attribute
;
38 u8 page
; /* page number */
39 u8 phase
; /* phase number, 0xff for all phases */
40 u16 reg
; /* register */
41 enum pmbus_sensor_classes
class; /* sensor class */
42 bool update
; /* runtime sensor update needed */
43 bool convert
; /* Whether or not to apply linear/vid/direct */
44 int data
; /* Sensor data.
45 Negative if there was a read error */
47 #define to_pmbus_sensor(_attr) \
48 container_of(_attr, struct pmbus_sensor, attribute)
50 struct pmbus_boolean
{
51 char name
[PMBUS_NAME_SIZE
]; /* sysfs boolean name */
52 struct sensor_device_attribute attribute
;
53 struct pmbus_sensor
*s1
;
54 struct pmbus_sensor
*s2
;
56 #define to_pmbus_boolean(_attr) \
57 container_of(_attr, struct pmbus_boolean, attribute)
60 char name
[PMBUS_NAME_SIZE
]; /* sysfs label name */
61 struct device_attribute attribute
;
62 char label
[PMBUS_NAME_SIZE
]; /* label */
64 #define to_pmbus_label(_attr) \
65 container_of(_attr, struct pmbus_label, attribute)
67 /* Macros for converting between sensor index and register/page/status mask */
69 #define PB_STATUS_MASK 0xffff
70 #define PB_REG_SHIFT 16
71 #define PB_REG_MASK 0x3ff
72 #define PB_PAGE_SHIFT 26
73 #define PB_PAGE_MASK 0x3f
75 #define pb_reg_to_index(page, reg, mask) (((page) << PB_PAGE_SHIFT) | \
76 ((reg) << PB_REG_SHIFT) | (mask))
78 #define pb_index_to_page(index) (((index) >> PB_PAGE_SHIFT) & PB_PAGE_MASK)
79 #define pb_index_to_reg(index) (((index) >> PB_REG_SHIFT) & PB_REG_MASK)
80 #define pb_index_to_mask(index) ((index) & PB_STATUS_MASK)
84 struct device
*hwmon_dev
;
85 struct regulator_dev
**rdevs
;
87 u32 flags
; /* from platform data */
89 u8 revision
; /* The PMBus revision the device is compliant with */
91 int exponent
[PMBUS_PAGES
];
92 /* linear mode: exponent for output voltages */
94 const struct pmbus_driver_info
*info
;
98 struct attribute_group group
;
99 const struct attribute_group
**groups
;
100 struct dentry
*debugfs
; /* debugfs device directory */
102 struct pmbus_sensor
*sensors
;
104 struct mutex update_lock
;
106 bool has_status_word
; /* device uses STATUS_WORD register */
107 int (*read_status
)(struct i2c_client
*client
, int page
);
109 s16 currpage
; /* current page, -1 for unknown/unset */
110 s16 currphase
; /* current phase, 0xff for all, -1 for unknown/unset */
112 int vout_low
[PMBUS_PAGES
]; /* voltage low margin */
113 int vout_high
[PMBUS_PAGES
]; /* voltage high margin */
114 ktime_t write_time
; /* Last SMBUS write timestamp */
115 ktime_t access_time
; /* Last SMBUS access timestamp */
118 struct pmbus_debugfs_entry
{
119 struct i2c_client
*client
;
124 static const int pmbus_fan_rpm_mask
[] = {
131 static const int pmbus_fan_config_registers
[] = {
138 static const int pmbus_fan_command_registers
[] = {
145 void pmbus_clear_cache(struct i2c_client
*client
)
147 struct pmbus_data
*data
= i2c_get_clientdata(client
);
148 struct pmbus_sensor
*sensor
;
150 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
)
151 sensor
->data
= -ENODATA
;
153 EXPORT_SYMBOL_NS_GPL(pmbus_clear_cache
, "PMBUS");
155 void pmbus_set_update(struct i2c_client
*client
, u8 reg
, bool update
)
157 struct pmbus_data
*data
= i2c_get_clientdata(client
);
158 struct pmbus_sensor
*sensor
;
160 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
)
161 if (sensor
->reg
== reg
)
162 sensor
->update
= update
;
164 EXPORT_SYMBOL_NS_GPL(pmbus_set_update
, "PMBUS");
166 /* Some chips need a delay between accesses. */
167 static void pmbus_wait(struct i2c_client
*client
)
169 struct pmbus_data
*data
= i2c_get_clientdata(client
);
170 const struct pmbus_driver_info
*info
= data
->info
;
173 if (info
->access_delay
) {
174 delta
= ktime_us_delta(ktime_get(), data
->access_time
);
176 if (delta
< info
->access_delay
)
177 fsleep(info
->access_delay
- delta
);
178 } else if (info
->write_delay
) {
179 delta
= ktime_us_delta(ktime_get(), data
->write_time
);
181 if (delta
< info
->write_delay
)
182 fsleep(info
->write_delay
- delta
);
186 /* Sets the last accessed timestamp for pmbus_wait */
187 static void pmbus_update_ts(struct i2c_client
*client
, bool write_op
)
189 struct pmbus_data
*data
= i2c_get_clientdata(client
);
190 const struct pmbus_driver_info
*info
= data
->info
;
192 if (info
->access_delay
) {
193 data
->access_time
= ktime_get();
194 } else if (info
->write_delay
&& write_op
) {
195 data
->write_time
= ktime_get();
199 int pmbus_set_page(struct i2c_client
*client
, int page
, int phase
)
201 struct pmbus_data
*data
= i2c_get_clientdata(client
);
207 if (!(data
->info
->func
[page
] & PMBUS_PAGE_VIRTUAL
) &&
208 data
->info
->pages
> 1 && page
!= data
->currpage
) {
210 rv
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, page
);
211 pmbus_update_ts(client
, true);
216 rv
= i2c_smbus_read_byte_data(client
, PMBUS_PAGE
);
217 pmbus_update_ts(client
, false);
224 data
->currpage
= page
;
226 if (data
->info
->phases
[page
] && data
->currphase
!= phase
&&
227 !(data
->info
->func
[page
] & PMBUS_PHASE_VIRTUAL
)) {
229 rv
= i2c_smbus_write_byte_data(client
, PMBUS_PHASE
,
231 pmbus_update_ts(client
, true);
235 data
->currphase
= phase
;
239 EXPORT_SYMBOL_NS_GPL(pmbus_set_page
, "PMBUS");
241 int pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
245 rv
= pmbus_set_page(client
, page
, 0xff);
250 rv
= i2c_smbus_write_byte(client
, value
);
251 pmbus_update_ts(client
, true);
255 EXPORT_SYMBOL_NS_GPL(pmbus_write_byte
, "PMBUS");
258 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
259 * a device specific mapping function exists and calls it if necessary.
261 static int _pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
263 struct pmbus_data
*data
= i2c_get_clientdata(client
);
264 const struct pmbus_driver_info
*info
= data
->info
;
267 if (info
->write_byte
) {
268 status
= info
->write_byte(client
, page
, value
);
269 if (status
!= -ENODATA
)
272 return pmbus_write_byte(client
, page
, value
);
275 int pmbus_write_word_data(struct i2c_client
*client
, int page
, u8 reg
,
280 rv
= pmbus_set_page(client
, page
, 0xff);
285 rv
= i2c_smbus_write_word_data(client
, reg
, word
);
286 pmbus_update_ts(client
, true);
290 EXPORT_SYMBOL_NS_GPL(pmbus_write_word_data
, "PMBUS");
293 static int pmbus_write_virt_reg(struct i2c_client
*client
, int page
, int reg
,
301 case PMBUS_VIRT_FAN_TARGET_1
... PMBUS_VIRT_FAN_TARGET_4
:
302 id
= reg
- PMBUS_VIRT_FAN_TARGET_1
;
303 bit
= pmbus_fan_rpm_mask
[id
];
304 rv
= pmbus_update_fan(client
, page
, id
, bit
, bit
, word
);
315 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
316 * a device specific mapping function exists and calls it if necessary.
318 static int _pmbus_write_word_data(struct i2c_client
*client
, int page
, int reg
,
321 struct pmbus_data
*data
= i2c_get_clientdata(client
);
322 const struct pmbus_driver_info
*info
= data
->info
;
325 if (info
->write_word_data
) {
326 status
= info
->write_word_data(client
, page
, reg
, word
);
327 if (status
!= -ENODATA
)
331 if (reg
>= PMBUS_VIRT_BASE
)
332 return pmbus_write_virt_reg(client
, page
, reg
, word
);
334 return pmbus_write_word_data(client
, page
, reg
, word
);
338 * _pmbus_write_byte_data() is similar to pmbus_write_byte_data(), but checks if
339 * a device specific mapping function exists and calls it if necessary.
341 static int _pmbus_write_byte_data(struct i2c_client
*client
, int page
, int reg
, u8 value
)
343 struct pmbus_data
*data
= i2c_get_clientdata(client
);
344 const struct pmbus_driver_info
*info
= data
->info
;
347 if (info
->write_byte_data
) {
348 status
= info
->write_byte_data(client
, page
, reg
, value
);
349 if (status
!= -ENODATA
)
352 return pmbus_write_byte_data(client
, page
, reg
, value
);
356 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
357 * a device specific mapping function exists and calls it if necessary.
359 static int _pmbus_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
361 struct pmbus_data
*data
= i2c_get_clientdata(client
);
362 const struct pmbus_driver_info
*info
= data
->info
;
365 if (info
->read_byte_data
) {
366 status
= info
->read_byte_data(client
, page
, reg
);
367 if (status
!= -ENODATA
)
370 return pmbus_read_byte_data(client
, page
, reg
);
373 int pmbus_update_fan(struct i2c_client
*client
, int page
, int id
,
374 u8 config
, u8 mask
, u16 command
)
380 from
= _pmbus_read_byte_data(client
, page
,
381 pmbus_fan_config_registers
[id
]);
385 to
= (from
& ~mask
) | (config
& mask
);
387 rv
= _pmbus_write_byte_data(client
, page
,
388 pmbus_fan_config_registers
[id
], to
);
393 return _pmbus_write_word_data(client
, page
,
394 pmbus_fan_command_registers
[id
], command
);
396 EXPORT_SYMBOL_NS_GPL(pmbus_update_fan
, "PMBUS");
398 int pmbus_read_word_data(struct i2c_client
*client
, int page
, int phase
, u8 reg
)
402 rv
= pmbus_set_page(client
, page
, phase
);
407 rv
= i2c_smbus_read_word_data(client
, reg
);
408 pmbus_update_ts(client
, false);
412 EXPORT_SYMBOL_NS_GPL(pmbus_read_word_data
, "PMBUS");
414 static int pmbus_read_virt_reg(struct i2c_client
*client
, int page
, int reg
)
420 case PMBUS_VIRT_FAN_TARGET_1
... PMBUS_VIRT_FAN_TARGET_4
:
421 id
= reg
- PMBUS_VIRT_FAN_TARGET_1
;
422 rv
= pmbus_get_fan_rate_device(client
, page
, id
, rpm
);
433 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
434 * a device specific mapping function exists and calls it if necessary.
436 static int _pmbus_read_word_data(struct i2c_client
*client
, int page
,
439 struct pmbus_data
*data
= i2c_get_clientdata(client
);
440 const struct pmbus_driver_info
*info
= data
->info
;
443 if (info
->read_word_data
) {
444 status
= info
->read_word_data(client
, page
, phase
, reg
);
445 if (status
!= -ENODATA
)
449 if (reg
>= PMBUS_VIRT_BASE
)
450 return pmbus_read_virt_reg(client
, page
, reg
);
452 return pmbus_read_word_data(client
, page
, phase
, reg
);
455 /* Same as above, but without phase parameter, for use in check functions */
456 static int __pmbus_read_word_data(struct i2c_client
*client
, int page
, int reg
)
458 return _pmbus_read_word_data(client
, page
, 0xff, reg
);
461 int pmbus_read_byte_data(struct i2c_client
*client
, int page
, u8 reg
)
465 rv
= pmbus_set_page(client
, page
, 0xff);
470 rv
= i2c_smbus_read_byte_data(client
, reg
);
471 pmbus_update_ts(client
, false);
475 EXPORT_SYMBOL_NS_GPL(pmbus_read_byte_data
, "PMBUS");
477 int pmbus_write_byte_data(struct i2c_client
*client
, int page
, u8 reg
, u8 value
)
481 rv
= pmbus_set_page(client
, page
, 0xff);
486 rv
= i2c_smbus_write_byte_data(client
, reg
, value
);
487 pmbus_update_ts(client
, true);
491 EXPORT_SYMBOL_NS_GPL(pmbus_write_byte_data
, "PMBUS");
493 int pmbus_update_byte_data(struct i2c_client
*client
, int page
, u8 reg
,
499 rv
= _pmbus_read_byte_data(client
, page
, reg
);
503 tmp
= (rv
& ~mask
) | (value
& mask
);
506 rv
= _pmbus_write_byte_data(client
, page
, reg
, tmp
);
510 EXPORT_SYMBOL_NS_GPL(pmbus_update_byte_data
, "PMBUS");
512 static int pmbus_read_block_data(struct i2c_client
*client
, int page
, u8 reg
,
517 rv
= pmbus_set_page(client
, page
, 0xff);
522 rv
= i2c_smbus_read_block_data(client
, reg
, data_buf
);
523 pmbus_update_ts(client
, false);
528 static struct pmbus_sensor
*pmbus_find_sensor(struct pmbus_data
*data
, int page
,
531 struct pmbus_sensor
*sensor
;
533 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
) {
534 if (sensor
->page
== page
&& sensor
->reg
== reg
)
538 return ERR_PTR(-EINVAL
);
541 static int pmbus_get_fan_rate(struct i2c_client
*client
, int page
, int id
,
542 enum pmbus_fan_mode mode
,
545 struct pmbus_data
*data
= i2c_get_clientdata(client
);
546 bool want_rpm
, have_rpm
;
547 struct pmbus_sensor
*s
;
551 want_rpm
= (mode
== rpm
);
554 reg
= want_rpm
? PMBUS_VIRT_FAN_TARGET_1
: PMBUS_VIRT_PWM_1
;
555 s
= pmbus_find_sensor(data
, page
, reg
+ id
);
562 config
= _pmbus_read_byte_data(client
, page
,
563 pmbus_fan_config_registers
[id
]);
567 have_rpm
= !!(config
& pmbus_fan_rpm_mask
[id
]);
568 if (want_rpm
== have_rpm
)
569 return pmbus_read_word_data(client
, page
, 0xff,
570 pmbus_fan_command_registers
[id
]);
572 /* Can't sensibly map between RPM and PWM, just return zero */
576 int pmbus_get_fan_rate_device(struct i2c_client
*client
, int page
, int id
,
577 enum pmbus_fan_mode mode
)
579 return pmbus_get_fan_rate(client
, page
, id
, mode
, false);
581 EXPORT_SYMBOL_NS_GPL(pmbus_get_fan_rate_device
, "PMBUS");
583 int pmbus_get_fan_rate_cached(struct i2c_client
*client
, int page
, int id
,
584 enum pmbus_fan_mode mode
)
586 return pmbus_get_fan_rate(client
, page
, id
, mode
, true);
588 EXPORT_SYMBOL_NS_GPL(pmbus_get_fan_rate_cached
, "PMBUS");
590 static void pmbus_clear_fault_page(struct i2c_client
*client
, int page
)
592 _pmbus_write_byte(client
, page
, PMBUS_CLEAR_FAULTS
);
595 void pmbus_clear_faults(struct i2c_client
*client
)
597 struct pmbus_data
*data
= i2c_get_clientdata(client
);
600 for (i
= 0; i
< data
->info
->pages
; i
++)
601 pmbus_clear_fault_page(client
, i
);
603 EXPORT_SYMBOL_NS_GPL(pmbus_clear_faults
, "PMBUS");
605 static int pmbus_check_status_cml(struct i2c_client
*client
)
607 struct pmbus_data
*data
= i2c_get_clientdata(client
);
610 status
= data
->read_status(client
, -1);
611 if (status
< 0 || (status
& PB_STATUS_CML
)) {
612 status2
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
613 if (status2
< 0 || (status2
& PB_CML_FAULT_INVALID_COMMAND
))
619 static bool pmbus_check_register(struct i2c_client
*client
,
620 int (*func
)(struct i2c_client
*client
,
625 struct pmbus_data
*data
= i2c_get_clientdata(client
);
627 rv
= func(client
, page
, reg
);
628 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
629 rv
= pmbus_check_status_cml(client
);
630 if (rv
< 0 && (data
->flags
& PMBUS_READ_STATUS_AFTER_FAILED_CHECK
))
631 data
->read_status(client
, -1);
632 if (reg
< PMBUS_VIRT_BASE
)
633 pmbus_clear_fault_page(client
, -1);
637 static bool pmbus_check_status_register(struct i2c_client
*client
, int page
)
640 struct pmbus_data
*data
= i2c_get_clientdata(client
);
642 status
= data
->read_status(client
, page
);
643 if (status
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
) &&
644 (status
& PB_STATUS_CML
)) {
645 status
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
646 if (status
< 0 || (status
& PB_CML_FAULT_INVALID_COMMAND
))
650 pmbus_clear_fault_page(client
, -1);
654 bool pmbus_check_byte_register(struct i2c_client
*client
, int page
, int reg
)
656 return pmbus_check_register(client
, _pmbus_read_byte_data
, page
, reg
);
658 EXPORT_SYMBOL_NS_GPL(pmbus_check_byte_register
, "PMBUS");
660 bool pmbus_check_word_register(struct i2c_client
*client
, int page
, int reg
)
662 return pmbus_check_register(client
, __pmbus_read_word_data
, page
, reg
);
664 EXPORT_SYMBOL_NS_GPL(pmbus_check_word_register
, "PMBUS");
666 static bool __maybe_unused
pmbus_check_block_register(struct i2c_client
*client
,
670 struct pmbus_data
*data
= i2c_get_clientdata(client
);
671 char data_buf
[I2C_SMBUS_BLOCK_MAX
+ 2];
673 rv
= pmbus_read_block_data(client
, page
, reg
, data_buf
);
674 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
675 rv
= pmbus_check_status_cml(client
);
676 if (rv
< 0 && (data
->flags
& PMBUS_READ_STATUS_AFTER_FAILED_CHECK
))
677 data
->read_status(client
, -1);
678 pmbus_clear_fault_page(client
, -1);
682 const struct pmbus_driver_info
*pmbus_get_driver_info(struct i2c_client
*client
)
684 struct pmbus_data
*data
= i2c_get_clientdata(client
);
688 EXPORT_SYMBOL_NS_GPL(pmbus_get_driver_info
, "PMBUS");
690 static int pmbus_get_status(struct i2c_client
*client
, int page
, int reg
)
692 struct pmbus_data
*data
= i2c_get_clientdata(client
);
696 case PMBUS_STATUS_WORD
:
697 status
= data
->read_status(client
, page
);
700 status
= _pmbus_read_byte_data(client
, page
, reg
);
704 pmbus_clear_faults(client
);
708 static void pmbus_update_sensor_data(struct i2c_client
*client
, struct pmbus_sensor
*sensor
)
710 if (sensor
->data
< 0 || sensor
->update
)
711 sensor
->data
= _pmbus_read_word_data(client
, sensor
->page
,
712 sensor
->phase
, sensor
->reg
);
716 * Convert ieee754 sensor values to milli- or micro-units
717 * depending on sensor type.
719 * ieee754 data format:
721 * bit 10..14: exponent
724 * v=(−1)^signbit * 2^(−14) * 0.significantbits
726 * v=(−1)^signbit * 2^(exponent - 15) * 1.significantbits
730 * Add the number mantissa bits into the calculations for simplicity.
731 * To do that, add '10' to the exponent. By doing that, we can just add
732 * 0x400 to normal values and get the expected result.
734 static long pmbus_reg2data_ieee754(struct pmbus_data
*data
,
735 struct pmbus_sensor
*sensor
)
741 /* only support half precision for now */
742 sign
= sensor
->data
& 0x8000;
743 exponent
= (sensor
->data
>> 10) & 0x1f;
744 val
= sensor
->data
& 0x3ff;
746 if (exponent
== 0) { /* subnormal */
747 exponent
= -(14 + 10);
748 } else if (exponent
== 0x1f) { /* NaN, convert to min/max */
752 exponent
-= (15 + 10); /* normal */
756 /* scale result to milli-units for all sensors except fans */
757 if (sensor
->class != PSC_FAN
)
760 /* scale result to micro-units for power sensors */
761 if (sensor
->class == PSC_POWER
)
776 * Convert linear sensor values to milli- or micro-units
777 * depending on sensor type.
779 static s64
pmbus_reg2data_linear(struct pmbus_data
*data
,
780 struct pmbus_sensor
*sensor
)
786 if (sensor
->class == PSC_VOLTAGE_OUT
) { /* LINEAR16 */
787 exponent
= data
->exponent
[sensor
->page
];
788 mantissa
= (u16
) sensor
->data
;
789 } else { /* LINEAR11 */
790 exponent
= ((s16
)sensor
->data
) >> 11;
791 mantissa
= ((s16
)((sensor
->data
& 0x7ff) << 5)) >> 5;
796 /* scale result to milli-units for all sensors except fans */
797 if (sensor
->class != PSC_FAN
)
800 /* scale result to micro-units for power sensors */
801 if (sensor
->class == PSC_POWER
)
813 * Convert direct sensor values to milli- or micro-units
814 * depending on sensor type.
816 static s64
pmbus_reg2data_direct(struct pmbus_data
*data
,
817 struct pmbus_sensor
*sensor
)
819 s64 b
, val
= (s16
)sensor
->data
;
822 m
= data
->info
->m
[sensor
->class];
823 b
= data
->info
->b
[sensor
->class];
824 R
= data
->info
->R
[sensor
->class];
829 /* X = 1/m * (Y * 10^-R - b) */
831 /* scale result to milli-units for everything but fans */
832 if (!(sensor
->class == PSC_FAN
|| sensor
->class == PSC_PWM
)) {
837 /* scale result to micro-units for power sensors */
838 if (sensor
->class == PSC_POWER
) {
848 val
= div_s64(val
+ 5LL, 10L); /* round closest */
852 val
= div_s64(val
- b
, m
);
857 * Convert VID sensor values to milli- or micro-units
858 * depending on sensor type.
860 static s64
pmbus_reg2data_vid(struct pmbus_data
*data
,
861 struct pmbus_sensor
*sensor
)
863 long val
= sensor
->data
;
866 switch (data
->info
->vrm_version
[sensor
->page
]) {
868 if (val
>= 0x02 && val
<= 0xb2)
869 rv
= DIV_ROUND_CLOSEST(160000 - (val
- 2) * 625, 100);
873 rv
= 250 + (val
- 1) * 5;
877 rv
= 500 + (val
- 1) * 10;
881 rv
= 200 + (val
- 1) * 10;
884 if (val
>= 0x0 && val
<= 0xd8)
885 rv
= DIV_ROUND_CLOSEST(155000 - val
* 625, 100);
891 static s64
pmbus_reg2data(struct pmbus_data
*data
, struct pmbus_sensor
*sensor
)
895 if (!sensor
->convert
)
898 switch (data
->info
->format
[sensor
->class]) {
900 val
= pmbus_reg2data_direct(data
, sensor
);
903 val
= pmbus_reg2data_vid(data
, sensor
);
906 val
= pmbus_reg2data_ieee754(data
, sensor
);
910 val
= pmbus_reg2data_linear(data
, sensor
);
916 #define MAX_IEEE_MANTISSA (0x7ff * 1000)
917 #define MIN_IEEE_MANTISSA (0x400 * 1000)
919 static u16
pmbus_data2reg_ieee754(struct pmbus_data
*data
,
920 struct pmbus_sensor
*sensor
, long val
)
922 u16 exponent
= (15 + 10);
935 /* Power is in uW. Convert to mW before converting. */
936 if (sensor
->class == PSC_POWER
)
937 val
= DIV_ROUND_CLOSEST(val
, 1000L);
940 * For simplicity, convert fan data to milli-units
941 * before calculating the exponent.
943 if (sensor
->class == PSC_FAN
)
946 /* Reduce large mantissa until it fits into 10 bit */
947 while (val
> MAX_IEEE_MANTISSA
&& exponent
< 30) {
952 * Increase small mantissa to generate valid 'normal'
955 while (val
< MIN_IEEE_MANTISSA
&& exponent
> 1) {
960 /* Convert mantissa from milli-units to units */
961 mantissa
= DIV_ROUND_CLOSEST(val
, 1000);
964 * Ensure that the resulting number is within range.
965 * Valid range is 0x400..0x7ff, where bit 10 reflects
966 * the implied high bit in normalized ieee754 numbers.
967 * Set the range to 0x400..0x7ff to reflect this.
968 * The upper bit is then removed by the mask against
969 * 0x3ff in the final assignment.
971 if (mantissa
> 0x7ff)
973 else if (mantissa
< 0x400)
976 /* Convert to sign, 5 bit exponent, 10 bit mantissa */
977 return sign
| (mantissa
& 0x3ff) | ((exponent
<< 10) & 0x7c00);
980 #define MAX_LIN_MANTISSA (1023 * 1000)
981 #define MIN_LIN_MANTISSA (511 * 1000)
983 static u16
pmbus_data2reg_linear(struct pmbus_data
*data
,
984 struct pmbus_sensor
*sensor
, s64 val
)
986 s16 exponent
= 0, mantissa
;
987 bool negative
= false;
993 if (sensor
->class == PSC_VOLTAGE_OUT
) {
994 /* LINEAR16 does not support negative voltages */
999 * For a static exponents, we don't have a choice
1000 * but to adjust the value to it.
1002 if (data
->exponent
[sensor
->page
] < 0)
1003 val
<<= -data
->exponent
[sensor
->page
];
1005 val
>>= data
->exponent
[sensor
->page
];
1006 val
= DIV_ROUND_CLOSEST_ULL(val
, 1000);
1007 return clamp_val(val
, 0, 0xffff);
1015 /* Power is in uW. Convert to mW before converting. */
1016 if (sensor
->class == PSC_POWER
)
1017 val
= DIV_ROUND_CLOSEST_ULL(val
, 1000);
1020 * For simplicity, convert fan data to milli-units
1021 * before calculating the exponent.
1023 if (sensor
->class == PSC_FAN
)
1026 /* Reduce large mantissa until it fits into 10 bit */
1027 while (val
>= MAX_LIN_MANTISSA
&& exponent
< 15) {
1031 /* Increase small mantissa to improve precision */
1032 while (val
< MIN_LIN_MANTISSA
&& exponent
> -15) {
1037 /* Convert mantissa from milli-units to units */
1038 mantissa
= clamp_val(DIV_ROUND_CLOSEST_ULL(val
, 1000), 0, 0x3ff);
1042 mantissa
= -mantissa
;
1044 /* Convert to 5 bit exponent, 11 bit mantissa */
1045 return (mantissa
& 0x7ff) | ((exponent
<< 11) & 0xf800);
1048 static u16
pmbus_data2reg_direct(struct pmbus_data
*data
,
1049 struct pmbus_sensor
*sensor
, s64 val
)
1054 m
= data
->info
->m
[sensor
->class];
1055 b
= data
->info
->b
[sensor
->class];
1056 R
= data
->info
->R
[sensor
->class];
1058 /* Power is in uW. Adjust R and b. */
1059 if (sensor
->class == PSC_POWER
) {
1064 /* Calculate Y = (m * X + b) * 10^R */
1065 if (!(sensor
->class == PSC_FAN
|| sensor
->class == PSC_PWM
)) {
1066 R
-= 3; /* Adjust R and b for data in milli-units */
1076 val
= div_s64(val
+ 5LL, 10L); /* round closest */
1080 return (u16
)clamp_val(val
, S16_MIN
, S16_MAX
);
1083 static u16
pmbus_data2reg_vid(struct pmbus_data
*data
,
1084 struct pmbus_sensor
*sensor
, s64 val
)
1086 val
= clamp_val(val
, 500, 1600);
1088 return 2 + DIV_ROUND_CLOSEST_ULL((1600LL - val
) * 100LL, 625);
1091 static u16
pmbus_data2reg(struct pmbus_data
*data
,
1092 struct pmbus_sensor
*sensor
, s64 val
)
1096 if (!sensor
->convert
)
1099 switch (data
->info
->format
[sensor
->class]) {
1101 regval
= pmbus_data2reg_direct(data
, sensor
, val
);
1104 regval
= pmbus_data2reg_vid(data
, sensor
, val
);
1107 regval
= pmbus_data2reg_ieee754(data
, sensor
, val
);
1111 regval
= pmbus_data2reg_linear(data
, sensor
, val
);
1118 * Return boolean calculated from converted data.
1119 * <index> defines a status register index and mask.
1120 * The mask is in the lower 8 bits, the register index is in bits 8..23.
1122 * The associated pmbus_boolean structure contains optional pointers to two
1123 * sensor attributes. If specified, those attributes are compared against each
1124 * other to determine if a limit has been exceeded.
1126 * If the sensor attribute pointers are NULL, the function returns true if
1127 * (status[reg] & mask) is true.
1129 * If sensor attribute pointers are provided, a comparison against a specified
1130 * limit has to be performed to determine the boolean result.
1131 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
1132 * sensor values referenced by sensor attribute pointers s1 and s2).
1134 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
1135 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
1137 * If a negative value is stored in any of the referenced registers, this value
1138 * reflects an error code which will be returned.
1140 static int pmbus_get_boolean(struct i2c_client
*client
, struct pmbus_boolean
*b
,
1143 struct pmbus_data
*data
= i2c_get_clientdata(client
);
1144 struct pmbus_sensor
*s1
= b
->s1
;
1145 struct pmbus_sensor
*s2
= b
->s2
;
1146 u16 mask
= pb_index_to_mask(index
);
1147 u8 page
= pb_index_to_page(index
);
1148 u16 reg
= pb_index_to_reg(index
);
1152 mutex_lock(&data
->update_lock
);
1153 status
= pmbus_get_status(client
, page
, reg
);
1160 pmbus_update_sensor_data(client
, s1
);
1162 pmbus_update_sensor_data(client
, s2
);
1164 regval
= status
& mask
;
1166 if (data
->revision
>= PMBUS_REV_12
) {
1167 ret
= _pmbus_write_byte_data(client
, page
, reg
, regval
);
1171 pmbus_clear_fault_page(client
, page
);
1187 v1
= pmbus_reg2data(data
, s1
);
1188 v2
= pmbus_reg2data(data
, s2
);
1189 ret
= !!(regval
&& v1
>= v2
);
1194 mutex_unlock(&data
->update_lock
);
1198 static ssize_t
pmbus_show_boolean(struct device
*dev
,
1199 struct device_attribute
*da
, char *buf
)
1201 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
1202 struct pmbus_boolean
*boolean
= to_pmbus_boolean(attr
);
1203 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
1206 val
= pmbus_get_boolean(client
, boolean
, attr
->index
);
1209 return sysfs_emit(buf
, "%d\n", val
);
1212 static ssize_t
pmbus_show_sensor(struct device
*dev
,
1213 struct device_attribute
*devattr
, char *buf
)
1215 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
1216 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
1217 struct pmbus_data
*data
= i2c_get_clientdata(client
);
1220 mutex_lock(&data
->update_lock
);
1221 pmbus_update_sensor_data(client
, sensor
);
1222 if (sensor
->data
< 0)
1225 ret
= sysfs_emit(buf
, "%lld\n", pmbus_reg2data(data
, sensor
));
1226 mutex_unlock(&data
->update_lock
);
1230 static ssize_t
pmbus_set_sensor(struct device
*dev
,
1231 struct device_attribute
*devattr
,
1232 const char *buf
, size_t count
)
1234 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
1235 struct pmbus_data
*data
= i2c_get_clientdata(client
);
1236 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
1242 if (kstrtos64(buf
, 10, &val
) < 0)
1245 mutex_lock(&data
->update_lock
);
1246 regval
= pmbus_data2reg(data
, sensor
, val
);
1247 ret
= _pmbus_write_word_data(client
, sensor
->page
, sensor
->reg
, regval
);
1251 sensor
->data
= -ENODATA
;
1252 mutex_unlock(&data
->update_lock
);
1256 static ssize_t
pmbus_show_label(struct device
*dev
,
1257 struct device_attribute
*da
, char *buf
)
1259 struct pmbus_label
*label
= to_pmbus_label(da
);
1261 return sysfs_emit(buf
, "%s\n", label
->label
);
1264 static int pmbus_add_attribute(struct pmbus_data
*data
, struct attribute
*attr
)
1266 if (data
->num_attributes
>= data
->max_attributes
- 1) {
1267 int new_max_attrs
= data
->max_attributes
+ PMBUS_ATTR_ALLOC_SIZE
;
1268 void *new_attrs
= devm_krealloc_array(data
->dev
, data
->group
.attrs
,
1269 new_max_attrs
, sizeof(void *),
1273 data
->group
.attrs
= new_attrs
;
1274 data
->max_attributes
= new_max_attrs
;
1277 data
->group
.attrs
[data
->num_attributes
++] = attr
;
1278 data
->group
.attrs
[data
->num_attributes
] = NULL
;
1282 static void pmbus_dev_attr_init(struct device_attribute
*dev_attr
,
1285 ssize_t (*show
)(struct device
*dev
,
1286 struct device_attribute
*attr
,
1288 ssize_t (*store
)(struct device
*dev
,
1289 struct device_attribute
*attr
,
1290 const char *buf
, size_t count
))
1292 sysfs_attr_init(&dev_attr
->attr
);
1293 dev_attr
->attr
.name
= name
;
1294 dev_attr
->attr
.mode
= mode
;
1295 dev_attr
->show
= show
;
1296 dev_attr
->store
= store
;
1299 static void pmbus_attr_init(struct sensor_device_attribute
*a
,
1302 ssize_t (*show
)(struct device
*dev
,
1303 struct device_attribute
*attr
,
1305 ssize_t (*store
)(struct device
*dev
,
1306 struct device_attribute
*attr
,
1307 const char *buf
, size_t count
),
1310 pmbus_dev_attr_init(&a
->dev_attr
, name
, mode
, show
, store
);
1314 static int pmbus_add_boolean(struct pmbus_data
*data
,
1315 const char *name
, const char *type
, int seq
,
1316 struct pmbus_sensor
*s1
,
1317 struct pmbus_sensor
*s2
,
1318 u8 page
, u16 reg
, u16 mask
)
1320 struct pmbus_boolean
*boolean
;
1321 struct sensor_device_attribute
*a
;
1323 if (WARN((s1
&& !s2
) || (!s1
&& s2
), "Bad s1/s2 parameters\n"))
1326 boolean
= devm_kzalloc(data
->dev
, sizeof(*boolean
), GFP_KERNEL
);
1330 a
= &boolean
->attribute
;
1332 snprintf(boolean
->name
, sizeof(boolean
->name
), "%s%d_%s",
1336 pmbus_attr_init(a
, boolean
->name
, 0444, pmbus_show_boolean
, NULL
,
1337 pb_reg_to_index(page
, reg
, mask
));
1339 return pmbus_add_attribute(data
, &a
->dev_attr
.attr
);
1342 /* of thermal for pmbus temperature sensors */
1343 struct pmbus_thermal_data
{
1344 struct pmbus_data
*pmbus_data
;
1345 struct pmbus_sensor
*sensor
;
1348 static int pmbus_thermal_get_temp(struct thermal_zone_device
*tz
, int *temp
)
1350 struct pmbus_thermal_data
*tdata
= thermal_zone_device_priv(tz
);
1351 struct pmbus_sensor
*sensor
= tdata
->sensor
;
1352 struct pmbus_data
*pmbus_data
= tdata
->pmbus_data
;
1353 struct i2c_client
*client
= to_i2c_client(pmbus_data
->dev
);
1354 struct device
*dev
= pmbus_data
->hwmon_dev
;
1358 /* May not even get to hwmon yet */
1363 mutex_lock(&pmbus_data
->update_lock
);
1364 pmbus_update_sensor_data(client
, sensor
);
1365 if (sensor
->data
< 0)
1368 *temp
= (int)pmbus_reg2data(pmbus_data
, sensor
);
1369 mutex_unlock(&pmbus_data
->update_lock
);
1374 static const struct thermal_zone_device_ops pmbus_thermal_ops
= {
1375 .get_temp
= pmbus_thermal_get_temp
,
1378 static int pmbus_thermal_add_sensor(struct pmbus_data
*pmbus_data
,
1379 struct pmbus_sensor
*sensor
, int index
)
1381 struct device
*dev
= pmbus_data
->dev
;
1382 struct pmbus_thermal_data
*tdata
;
1383 struct thermal_zone_device
*tzd
;
1385 tdata
= devm_kzalloc(dev
, sizeof(*tdata
), GFP_KERNEL
);
1389 tdata
->sensor
= sensor
;
1390 tdata
->pmbus_data
= pmbus_data
;
1392 tzd
= devm_thermal_of_zone_register(dev
, index
, tdata
,
1393 &pmbus_thermal_ops
);
1395 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
1396 * so ignore that error but forward any other error.
1398 if (IS_ERR(tzd
) && (PTR_ERR(tzd
) != -ENODEV
))
1399 return PTR_ERR(tzd
);
1404 static struct pmbus_sensor
*pmbus_add_sensor(struct pmbus_data
*data
,
1405 const char *name
, const char *type
,
1406 int seq
, int page
, int phase
,
1408 enum pmbus_sensor_classes
class,
1409 bool update
, bool readonly
,
1412 struct pmbus_sensor
*sensor
;
1413 struct device_attribute
*a
;
1415 sensor
= devm_kzalloc(data
->dev
, sizeof(*sensor
), GFP_KERNEL
);
1418 a
= &sensor
->attribute
;
1421 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d_%s",
1424 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d",
1427 if (data
->flags
& PMBUS_WRITE_PROTECTED
)
1430 sensor
->page
= page
;
1431 sensor
->phase
= phase
;
1433 sensor
->class = class;
1434 sensor
->update
= update
;
1435 sensor
->convert
= convert
;
1436 sensor
->data
= -ENODATA
;
1437 pmbus_dev_attr_init(a
, sensor
->name
,
1438 readonly
? 0444 : 0644,
1439 pmbus_show_sensor
, pmbus_set_sensor
);
1441 if (pmbus_add_attribute(data
, &a
->attr
))
1444 sensor
->next
= data
->sensors
;
1445 data
->sensors
= sensor
;
1447 /* temperature sensors with _input values are registered with thermal */
1448 if (class == PSC_TEMPERATURE
&& strcmp(type
, "input") == 0)
1449 pmbus_thermal_add_sensor(data
, sensor
, seq
);
1454 static int pmbus_add_label(struct pmbus_data
*data
,
1455 const char *name
, int seq
,
1456 const char *lstring
, int index
, int phase
)
1458 struct pmbus_label
*label
;
1459 struct device_attribute
*a
;
1461 label
= devm_kzalloc(data
->dev
, sizeof(*label
), GFP_KERNEL
);
1465 a
= &label
->attribute
;
1467 snprintf(label
->name
, sizeof(label
->name
), "%s%d_label", name
, seq
);
1470 strncpy(label
->label
, lstring
,
1471 sizeof(label
->label
) - 1);
1473 snprintf(label
->label
, sizeof(label
->label
), "%s.%d",
1477 snprintf(label
->label
, sizeof(label
->label
), "%s%d",
1480 snprintf(label
->label
, sizeof(label
->label
), "%s%d.%d",
1481 lstring
, index
, phase
);
1484 pmbus_dev_attr_init(a
, label
->name
, 0444, pmbus_show_label
, NULL
);
1485 return pmbus_add_attribute(data
, &a
->attr
);
1489 * Search for attributes. Allocate sensors, booleans, and labels as needed.
1493 * The pmbus_limit_attr structure describes a single limit attribute
1494 * and its associated alarm attribute.
1496 struct pmbus_limit_attr
{
1497 u16 reg
; /* Limit register */
1498 u16 sbit
; /* Alarm attribute status bit */
1499 bool update
; /* True if register needs updates */
1500 bool low
; /* True if low limit; for limits with compare
1502 const char *attr
; /* Attribute name */
1503 const char *alarm
; /* Alarm attribute name */
1507 * The pmbus_sensor_attr structure describes one sensor attribute. This
1508 * description includes a reference to the associated limit attributes.
1510 struct pmbus_sensor_attr
{
1511 u16 reg
; /* sensor register */
1512 u16 gbit
; /* generic status bit */
1513 u8 nlimit
; /* # of limit registers */
1514 enum pmbus_sensor_classes
class;/* sensor class */
1515 const char *label
; /* sensor label */
1516 bool paged
; /* true if paged sensor */
1517 bool update
; /* true if update needed */
1518 bool compare
; /* true if compare function needed */
1519 u32 func
; /* sensor mask */
1520 u32 sfunc
; /* sensor status mask */
1521 int sreg
; /* status register */
1522 const struct pmbus_limit_attr
*limit
;/* limit registers */
1526 * Add a set of limit attributes and, if supported, the associated
1528 * returns 0 if no alarm register found, 1 if an alarm register was found,
1531 static int pmbus_add_limit_attrs(struct i2c_client
*client
,
1532 struct pmbus_data
*data
,
1533 const struct pmbus_driver_info
*info
,
1534 const char *name
, int index
, int page
,
1535 struct pmbus_sensor
*base
,
1536 const struct pmbus_sensor_attr
*attr
)
1538 const struct pmbus_limit_attr
*l
= attr
->limit
;
1539 int nlimit
= attr
->nlimit
;
1542 struct pmbus_sensor
*curr
;
1544 for (i
= 0; i
< nlimit
; i
++) {
1545 if (pmbus_check_word_register(client
, page
, l
->reg
)) {
1546 curr
= pmbus_add_sensor(data
, name
, l
->attr
, index
,
1547 page
, 0xff, l
->reg
, attr
->class,
1548 attr
->update
|| l
->update
,
1552 if (l
->sbit
&& (info
->func
[page
] & attr
->sfunc
)) {
1553 ret
= pmbus_add_boolean(data
, name
,
1555 attr
->compare
? l
->low
? curr
: base
1557 attr
->compare
? l
->low
? base
: curr
1559 page
, attr
->sreg
, l
->sbit
);
1570 static int pmbus_add_sensor_attrs_one(struct i2c_client
*client
,
1571 struct pmbus_data
*data
,
1572 const struct pmbus_driver_info
*info
,
1574 int index
, int page
, int phase
,
1575 const struct pmbus_sensor_attr
*attr
,
1578 struct pmbus_sensor
*base
;
1579 bool upper
= !!(attr
->gbit
& 0xff00); /* need to check STATUS_WORD */
1583 ret
= pmbus_add_label(data
, name
, index
, attr
->label
,
1584 paged
? page
+ 1 : 0, phase
);
1588 base
= pmbus_add_sensor(data
, name
, "input", index
, page
, phase
,
1589 attr
->reg
, attr
->class, true, true, true);
1592 /* No limit and alarm attributes for phase specific sensors */
1593 if (attr
->sfunc
&& phase
== 0xff) {
1594 ret
= pmbus_add_limit_attrs(client
, data
, info
, name
,
1595 index
, page
, base
, attr
);
1599 * Add generic alarm attribute only if there are no individual
1600 * alarm attributes, if there is a global alarm bit, and if
1601 * the generic status register (word or byte, depending on
1602 * which global bit is set) for this page is accessible.
1604 if (!ret
&& attr
->gbit
&&
1605 (!upper
|| data
->has_status_word
) &&
1606 pmbus_check_status_register(client
, page
)) {
1607 ret
= pmbus_add_boolean(data
, name
, "alarm", index
,
1609 page
, PMBUS_STATUS_WORD
,
1618 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info
*info
,
1619 const struct pmbus_sensor_attr
*attr
)
1627 * Some attributes may be present on more than one page despite
1628 * not being marked with the paged attribute. If that is the case,
1629 * then treat the sensor as being paged and add the page suffix to the
1631 * We don't just add the paged attribute to all such attributes, in
1632 * order to maintain the un-suffixed labels in the case where the
1633 * attribute is only on page 0.
1635 for (p
= 1; p
< info
->pages
; p
++) {
1636 if (info
->func
[p
] & attr
->func
)
1642 static int pmbus_add_sensor_attrs(struct i2c_client
*client
,
1643 struct pmbus_data
*data
,
1645 const struct pmbus_sensor_attr
*attrs
,
1648 const struct pmbus_driver_info
*info
= data
->info
;
1653 for (i
= 0; i
< nattrs
; i
++) {
1655 bool paged
= pmbus_sensor_is_paged(info
, attrs
);
1657 pages
= paged
? info
->pages
: 1;
1658 for (page
= 0; page
< pages
; page
++) {
1659 if (info
->func
[page
] & attrs
->func
) {
1660 ret
= pmbus_add_sensor_attrs_one(client
, data
, info
,
1662 0xff, attrs
, paged
);
1667 if (info
->phases
[page
]) {
1670 for (phase
= 0; phase
< info
->phases
[page
];
1672 if (!(info
->pfunc
[phase
] & attrs
->func
))
1674 ret
= pmbus_add_sensor_attrs_one(client
,
1675 data
, info
, name
, index
, page
,
1676 phase
, attrs
, paged
);
1688 static const struct pmbus_limit_attr vin_limit_attrs
[] = {
1690 .reg
= PMBUS_VIN_UV_WARN_LIMIT
,
1692 .alarm
= "min_alarm",
1693 .sbit
= PB_VOLTAGE_UV_WARNING
,
1695 .reg
= PMBUS_VIN_UV_FAULT_LIMIT
,
1697 .alarm
= "lcrit_alarm",
1698 .sbit
= PB_VOLTAGE_UV_FAULT
| PB_VOLTAGE_VIN_OFF
,
1700 .reg
= PMBUS_VIN_OV_WARN_LIMIT
,
1702 .alarm
= "max_alarm",
1703 .sbit
= PB_VOLTAGE_OV_WARNING
,
1705 .reg
= PMBUS_VIN_OV_FAULT_LIMIT
,
1707 .alarm
= "crit_alarm",
1708 .sbit
= PB_VOLTAGE_OV_FAULT
,
1710 .reg
= PMBUS_VIRT_READ_VIN_AVG
,
1714 .reg
= PMBUS_VIRT_READ_VIN_MIN
,
1718 .reg
= PMBUS_VIRT_READ_VIN_MAX
,
1722 .reg
= PMBUS_VIRT_RESET_VIN_HISTORY
,
1723 .attr
= "reset_history",
1725 .reg
= PMBUS_MFR_VIN_MIN
,
1726 .attr
= "rated_min",
1728 .reg
= PMBUS_MFR_VIN_MAX
,
1729 .attr
= "rated_max",
1733 static const struct pmbus_limit_attr vmon_limit_attrs
[] = {
1735 .reg
= PMBUS_VIRT_VMON_UV_WARN_LIMIT
,
1737 .alarm
= "min_alarm",
1738 .sbit
= PB_VOLTAGE_UV_WARNING
,
1740 .reg
= PMBUS_VIRT_VMON_UV_FAULT_LIMIT
,
1742 .alarm
= "lcrit_alarm",
1743 .sbit
= PB_VOLTAGE_UV_FAULT
,
1745 .reg
= PMBUS_VIRT_VMON_OV_WARN_LIMIT
,
1747 .alarm
= "max_alarm",
1748 .sbit
= PB_VOLTAGE_OV_WARNING
,
1750 .reg
= PMBUS_VIRT_VMON_OV_FAULT_LIMIT
,
1752 .alarm
= "crit_alarm",
1753 .sbit
= PB_VOLTAGE_OV_FAULT
,
1757 static const struct pmbus_limit_attr vout_limit_attrs
[] = {
1759 .reg
= PMBUS_VOUT_UV_WARN_LIMIT
,
1761 .alarm
= "min_alarm",
1762 .sbit
= PB_VOLTAGE_UV_WARNING
,
1764 .reg
= PMBUS_VOUT_UV_FAULT_LIMIT
,
1766 .alarm
= "lcrit_alarm",
1767 .sbit
= PB_VOLTAGE_UV_FAULT
,
1769 .reg
= PMBUS_VOUT_OV_WARN_LIMIT
,
1771 .alarm
= "max_alarm",
1772 .sbit
= PB_VOLTAGE_OV_WARNING
,
1774 .reg
= PMBUS_VOUT_OV_FAULT_LIMIT
,
1776 .alarm
= "crit_alarm",
1777 .sbit
= PB_VOLTAGE_OV_FAULT
,
1779 .reg
= PMBUS_VIRT_READ_VOUT_AVG
,
1783 .reg
= PMBUS_VIRT_READ_VOUT_MIN
,
1787 .reg
= PMBUS_VIRT_READ_VOUT_MAX
,
1791 .reg
= PMBUS_VIRT_RESET_VOUT_HISTORY
,
1792 .attr
= "reset_history",
1794 .reg
= PMBUS_MFR_VOUT_MIN
,
1795 .attr
= "rated_min",
1797 .reg
= PMBUS_MFR_VOUT_MAX
,
1798 .attr
= "rated_max",
1802 static const struct pmbus_sensor_attr voltage_attributes
[] = {
1804 .reg
= PMBUS_READ_VIN
,
1805 .class = PSC_VOLTAGE_IN
,
1807 .func
= PMBUS_HAVE_VIN
,
1808 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1809 .sreg
= PMBUS_STATUS_INPUT
,
1810 .gbit
= PB_STATUS_VIN_UV
,
1811 .limit
= vin_limit_attrs
,
1812 .nlimit
= ARRAY_SIZE(vin_limit_attrs
),
1814 .reg
= PMBUS_VIRT_READ_VMON
,
1815 .class = PSC_VOLTAGE_IN
,
1817 .func
= PMBUS_HAVE_VMON
,
1818 .sfunc
= PMBUS_HAVE_STATUS_VMON
,
1819 .sreg
= PMBUS_VIRT_STATUS_VMON
,
1820 .limit
= vmon_limit_attrs
,
1821 .nlimit
= ARRAY_SIZE(vmon_limit_attrs
),
1823 .reg
= PMBUS_READ_VCAP
,
1824 .class = PSC_VOLTAGE_IN
,
1826 .func
= PMBUS_HAVE_VCAP
,
1828 .reg
= PMBUS_READ_VOUT
,
1829 .class = PSC_VOLTAGE_OUT
,
1832 .func
= PMBUS_HAVE_VOUT
,
1833 .sfunc
= PMBUS_HAVE_STATUS_VOUT
,
1834 .sreg
= PMBUS_STATUS_VOUT
,
1835 .gbit
= PB_STATUS_VOUT_OV
,
1836 .limit
= vout_limit_attrs
,
1837 .nlimit
= ARRAY_SIZE(vout_limit_attrs
),
1841 /* Current attributes */
1843 static const struct pmbus_limit_attr iin_limit_attrs
[] = {
1845 .reg
= PMBUS_IIN_OC_WARN_LIMIT
,
1847 .alarm
= "max_alarm",
1848 .sbit
= PB_IIN_OC_WARNING
,
1850 .reg
= PMBUS_IIN_OC_FAULT_LIMIT
,
1852 .alarm
= "crit_alarm",
1853 .sbit
= PB_IIN_OC_FAULT
,
1855 .reg
= PMBUS_VIRT_READ_IIN_AVG
,
1859 .reg
= PMBUS_VIRT_READ_IIN_MIN
,
1863 .reg
= PMBUS_VIRT_READ_IIN_MAX
,
1867 .reg
= PMBUS_VIRT_RESET_IIN_HISTORY
,
1868 .attr
= "reset_history",
1870 .reg
= PMBUS_MFR_IIN_MAX
,
1871 .attr
= "rated_max",
1875 static const struct pmbus_limit_attr iout_limit_attrs
[] = {
1877 .reg
= PMBUS_IOUT_OC_WARN_LIMIT
,
1879 .alarm
= "max_alarm",
1880 .sbit
= PB_IOUT_OC_WARNING
,
1882 .reg
= PMBUS_IOUT_UC_FAULT_LIMIT
,
1884 .alarm
= "lcrit_alarm",
1885 .sbit
= PB_IOUT_UC_FAULT
,
1887 .reg
= PMBUS_IOUT_OC_FAULT_LIMIT
,
1889 .alarm
= "crit_alarm",
1890 .sbit
= PB_IOUT_OC_FAULT
,
1892 .reg
= PMBUS_VIRT_READ_IOUT_AVG
,
1896 .reg
= PMBUS_VIRT_READ_IOUT_MIN
,
1900 .reg
= PMBUS_VIRT_READ_IOUT_MAX
,
1904 .reg
= PMBUS_VIRT_RESET_IOUT_HISTORY
,
1905 .attr
= "reset_history",
1907 .reg
= PMBUS_MFR_IOUT_MAX
,
1908 .attr
= "rated_max",
1912 static const struct pmbus_sensor_attr current_attributes
[] = {
1914 .reg
= PMBUS_READ_IIN
,
1915 .class = PSC_CURRENT_IN
,
1917 .func
= PMBUS_HAVE_IIN
,
1918 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1919 .sreg
= PMBUS_STATUS_INPUT
,
1920 .gbit
= PB_STATUS_INPUT
,
1921 .limit
= iin_limit_attrs
,
1922 .nlimit
= ARRAY_SIZE(iin_limit_attrs
),
1924 .reg
= PMBUS_READ_IOUT
,
1925 .class = PSC_CURRENT_OUT
,
1928 .func
= PMBUS_HAVE_IOUT
,
1929 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1930 .sreg
= PMBUS_STATUS_IOUT
,
1931 .gbit
= PB_STATUS_IOUT_OC
,
1932 .limit
= iout_limit_attrs
,
1933 .nlimit
= ARRAY_SIZE(iout_limit_attrs
),
1937 /* Power attributes */
1939 static const struct pmbus_limit_attr pin_limit_attrs
[] = {
1941 .reg
= PMBUS_PIN_OP_WARN_LIMIT
,
1944 .sbit
= PB_PIN_OP_WARNING
,
1946 .reg
= PMBUS_VIRT_READ_PIN_AVG
,
1950 .reg
= PMBUS_VIRT_READ_PIN_MIN
,
1952 .attr
= "input_lowest",
1954 .reg
= PMBUS_VIRT_READ_PIN_MAX
,
1956 .attr
= "input_highest",
1958 .reg
= PMBUS_VIRT_RESET_PIN_HISTORY
,
1959 .attr
= "reset_history",
1961 .reg
= PMBUS_MFR_PIN_MAX
,
1962 .attr
= "rated_max",
1966 static const struct pmbus_limit_attr pout_limit_attrs
[] = {
1968 .reg
= PMBUS_POUT_MAX
,
1970 .alarm
= "cap_alarm",
1971 .sbit
= PB_POWER_LIMITING
,
1973 .reg
= PMBUS_POUT_OP_WARN_LIMIT
,
1975 .alarm
= "max_alarm",
1976 .sbit
= PB_POUT_OP_WARNING
,
1978 .reg
= PMBUS_POUT_OP_FAULT_LIMIT
,
1980 .alarm
= "crit_alarm",
1981 .sbit
= PB_POUT_OP_FAULT
,
1983 .reg
= PMBUS_VIRT_READ_POUT_AVG
,
1987 .reg
= PMBUS_VIRT_READ_POUT_MIN
,
1989 .attr
= "input_lowest",
1991 .reg
= PMBUS_VIRT_READ_POUT_MAX
,
1993 .attr
= "input_highest",
1995 .reg
= PMBUS_VIRT_RESET_POUT_HISTORY
,
1996 .attr
= "reset_history",
1998 .reg
= PMBUS_MFR_POUT_MAX
,
1999 .attr
= "rated_max",
2003 static const struct pmbus_sensor_attr power_attributes
[] = {
2005 .reg
= PMBUS_READ_PIN
,
2008 .func
= PMBUS_HAVE_PIN
,
2009 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
2010 .sreg
= PMBUS_STATUS_INPUT
,
2011 .gbit
= PB_STATUS_INPUT
,
2012 .limit
= pin_limit_attrs
,
2013 .nlimit
= ARRAY_SIZE(pin_limit_attrs
),
2015 .reg
= PMBUS_READ_POUT
,
2019 .func
= PMBUS_HAVE_POUT
,
2020 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
2021 .sreg
= PMBUS_STATUS_IOUT
,
2022 .limit
= pout_limit_attrs
,
2023 .nlimit
= ARRAY_SIZE(pout_limit_attrs
),
2027 /* Temperature atributes */
2029 static const struct pmbus_limit_attr temp_limit_attrs
[] = {
2031 .reg
= PMBUS_UT_WARN_LIMIT
,
2034 .alarm
= "min_alarm",
2035 .sbit
= PB_TEMP_UT_WARNING
,
2037 .reg
= PMBUS_UT_FAULT_LIMIT
,
2040 .alarm
= "lcrit_alarm",
2041 .sbit
= PB_TEMP_UT_FAULT
,
2043 .reg
= PMBUS_OT_WARN_LIMIT
,
2045 .alarm
= "max_alarm",
2046 .sbit
= PB_TEMP_OT_WARNING
,
2048 .reg
= PMBUS_OT_FAULT_LIMIT
,
2050 .alarm
= "crit_alarm",
2051 .sbit
= PB_TEMP_OT_FAULT
,
2053 .reg
= PMBUS_VIRT_READ_TEMP_MIN
,
2056 .reg
= PMBUS_VIRT_READ_TEMP_AVG
,
2059 .reg
= PMBUS_VIRT_READ_TEMP_MAX
,
2062 .reg
= PMBUS_VIRT_RESET_TEMP_HISTORY
,
2063 .attr
= "reset_history",
2065 .reg
= PMBUS_MFR_MAX_TEMP_1
,
2066 .attr
= "rated_max",
2070 static const struct pmbus_limit_attr temp_limit_attrs2
[] = {
2072 .reg
= PMBUS_UT_WARN_LIMIT
,
2075 .alarm
= "min_alarm",
2076 .sbit
= PB_TEMP_UT_WARNING
,
2078 .reg
= PMBUS_UT_FAULT_LIMIT
,
2081 .alarm
= "lcrit_alarm",
2082 .sbit
= PB_TEMP_UT_FAULT
,
2084 .reg
= PMBUS_OT_WARN_LIMIT
,
2086 .alarm
= "max_alarm",
2087 .sbit
= PB_TEMP_OT_WARNING
,
2089 .reg
= PMBUS_OT_FAULT_LIMIT
,
2091 .alarm
= "crit_alarm",
2092 .sbit
= PB_TEMP_OT_FAULT
,
2094 .reg
= PMBUS_VIRT_READ_TEMP2_MIN
,
2097 .reg
= PMBUS_VIRT_READ_TEMP2_AVG
,
2100 .reg
= PMBUS_VIRT_READ_TEMP2_MAX
,
2103 .reg
= PMBUS_VIRT_RESET_TEMP2_HISTORY
,
2104 .attr
= "reset_history",
2106 .reg
= PMBUS_MFR_MAX_TEMP_2
,
2107 .attr
= "rated_max",
2111 static const struct pmbus_limit_attr temp_limit_attrs3
[] = {
2113 .reg
= PMBUS_UT_WARN_LIMIT
,
2116 .alarm
= "min_alarm",
2117 .sbit
= PB_TEMP_UT_WARNING
,
2119 .reg
= PMBUS_UT_FAULT_LIMIT
,
2122 .alarm
= "lcrit_alarm",
2123 .sbit
= PB_TEMP_UT_FAULT
,
2125 .reg
= PMBUS_OT_WARN_LIMIT
,
2127 .alarm
= "max_alarm",
2128 .sbit
= PB_TEMP_OT_WARNING
,
2130 .reg
= PMBUS_OT_FAULT_LIMIT
,
2132 .alarm
= "crit_alarm",
2133 .sbit
= PB_TEMP_OT_FAULT
,
2135 .reg
= PMBUS_MFR_MAX_TEMP_3
,
2136 .attr
= "rated_max",
2140 static const struct pmbus_sensor_attr temp_attributes
[] = {
2142 .reg
= PMBUS_READ_TEMPERATURE_1
,
2143 .class = PSC_TEMPERATURE
,
2147 .func
= PMBUS_HAVE_TEMP
,
2148 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
2149 .sreg
= PMBUS_STATUS_TEMPERATURE
,
2150 .gbit
= PB_STATUS_TEMPERATURE
,
2151 .limit
= temp_limit_attrs
,
2152 .nlimit
= ARRAY_SIZE(temp_limit_attrs
),
2154 .reg
= PMBUS_READ_TEMPERATURE_2
,
2155 .class = PSC_TEMPERATURE
,
2159 .func
= PMBUS_HAVE_TEMP2
,
2160 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
2161 .sreg
= PMBUS_STATUS_TEMPERATURE
,
2162 .gbit
= PB_STATUS_TEMPERATURE
,
2163 .limit
= temp_limit_attrs2
,
2164 .nlimit
= ARRAY_SIZE(temp_limit_attrs2
),
2166 .reg
= PMBUS_READ_TEMPERATURE_3
,
2167 .class = PSC_TEMPERATURE
,
2171 .func
= PMBUS_HAVE_TEMP3
,
2172 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
2173 .sreg
= PMBUS_STATUS_TEMPERATURE
,
2174 .gbit
= PB_STATUS_TEMPERATURE
,
2175 .limit
= temp_limit_attrs3
,
2176 .nlimit
= ARRAY_SIZE(temp_limit_attrs3
),
2180 static const int pmbus_fan_registers
[] = {
2181 PMBUS_READ_FAN_SPEED_1
,
2182 PMBUS_READ_FAN_SPEED_2
,
2183 PMBUS_READ_FAN_SPEED_3
,
2184 PMBUS_READ_FAN_SPEED_4
2187 static const int pmbus_fan_status_registers
[] = {
2188 PMBUS_STATUS_FAN_12
,
2189 PMBUS_STATUS_FAN_12
,
2190 PMBUS_STATUS_FAN_34
,
2194 static const u32 pmbus_fan_flags
[] = {
2201 static const u32 pmbus_fan_status_flags
[] = {
2202 PMBUS_HAVE_STATUS_FAN12
,
2203 PMBUS_HAVE_STATUS_FAN12
,
2204 PMBUS_HAVE_STATUS_FAN34
,
2205 PMBUS_HAVE_STATUS_FAN34
2210 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */
2211 static int pmbus_add_fan_ctrl(struct i2c_client
*client
,
2212 struct pmbus_data
*data
, int index
, int page
, int id
,
2215 struct pmbus_sensor
*sensor
;
2217 sensor
= pmbus_add_sensor(data
, "fan", "target", index
, page
,
2218 0xff, PMBUS_VIRT_FAN_TARGET_1
+ id
, PSC_FAN
,
2219 false, false, true);
2224 if (!((data
->info
->func
[page
] & PMBUS_HAVE_PWM12
) ||
2225 (data
->info
->func
[page
] & PMBUS_HAVE_PWM34
)))
2228 sensor
= pmbus_add_sensor(data
, "pwm", NULL
, index
, page
,
2229 0xff, PMBUS_VIRT_PWM_1
+ id
, PSC_PWM
,
2230 false, false, true);
2235 sensor
= pmbus_add_sensor(data
, "pwm", "enable", index
, page
,
2236 0xff, PMBUS_VIRT_PWM_ENABLE_1
+ id
, PSC_PWM
,
2237 true, false, false);
2245 static int pmbus_add_fan_attributes(struct i2c_client
*client
,
2246 struct pmbus_data
*data
)
2248 const struct pmbus_driver_info
*info
= data
->info
;
2253 for (page
= 0; page
< info
->pages
; page
++) {
2256 for (f
= 0; f
< ARRAY_SIZE(pmbus_fan_registers
); f
++) {
2259 if (!(info
->func
[page
] & pmbus_fan_flags
[f
]))
2262 if (!pmbus_check_word_register(client
, page
,
2263 pmbus_fan_registers
[f
]))
2267 * Skip fan if not installed.
2268 * Each fan configuration register covers multiple fans,
2269 * so we have to do some magic.
2271 regval
= _pmbus_read_byte_data(client
, page
,
2272 pmbus_fan_config_registers
[f
]);
2274 (!(regval
& (PB_FAN_1_INSTALLED
>> ((f
& 1) * 4)))))
2277 if (pmbus_add_sensor(data
, "fan", "input", index
,
2278 page
, 0xff, pmbus_fan_registers
[f
],
2279 PSC_FAN
, true, true, true) == NULL
)
2283 if (pmbus_check_word_register(client
, page
,
2284 pmbus_fan_command_registers
[f
])) {
2285 ret
= pmbus_add_fan_ctrl(client
, data
, index
,
2292 * Each fan status register covers multiple fans,
2293 * so we have to do some magic.
2295 if ((info
->func
[page
] & pmbus_fan_status_flags
[f
]) &&
2296 pmbus_check_byte_register(client
,
2297 page
, pmbus_fan_status_registers
[f
])) {
2300 if (f
> 1) /* fan 3, 4 */
2301 reg
= PMBUS_STATUS_FAN_34
;
2303 reg
= PMBUS_STATUS_FAN_12
;
2304 ret
= pmbus_add_boolean(data
, "fan",
2305 "alarm", index
, NULL
, NULL
, page
, reg
,
2306 PB_FAN_FAN1_WARNING
>> (f
& 1));
2309 ret
= pmbus_add_boolean(data
, "fan",
2310 "fault", index
, NULL
, NULL
, page
, reg
,
2311 PB_FAN_FAN1_FAULT
>> (f
& 1));
2321 struct pmbus_samples_attr
{
2326 struct pmbus_samples_reg
{
2328 struct pmbus_samples_attr
*attr
;
2329 struct device_attribute dev_attr
;
2332 static struct pmbus_samples_attr pmbus_samples_registers
[] = {
2334 .reg
= PMBUS_VIRT_SAMPLES
,
2337 .reg
= PMBUS_VIRT_IN_SAMPLES
,
2338 .name
= "in_samples",
2340 .reg
= PMBUS_VIRT_CURR_SAMPLES
,
2341 .name
= "curr_samples",
2343 .reg
= PMBUS_VIRT_POWER_SAMPLES
,
2344 .name
= "power_samples",
2346 .reg
= PMBUS_VIRT_TEMP_SAMPLES
,
2347 .name
= "temp_samples",
2351 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr)
2353 static ssize_t
pmbus_show_samples(struct device
*dev
,
2354 struct device_attribute
*devattr
, char *buf
)
2357 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2358 struct pmbus_samples_reg
*reg
= to_samples_reg(devattr
);
2359 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2361 mutex_lock(&data
->update_lock
);
2362 val
= _pmbus_read_word_data(client
, reg
->page
, 0xff, reg
->attr
->reg
);
2363 mutex_unlock(&data
->update_lock
);
2367 return sysfs_emit(buf
, "%d\n", val
);
2370 static ssize_t
pmbus_set_samples(struct device
*dev
,
2371 struct device_attribute
*devattr
,
2372 const char *buf
, size_t count
)
2376 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2377 struct pmbus_samples_reg
*reg
= to_samples_reg(devattr
);
2378 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2380 if (kstrtol(buf
, 0, &val
) < 0)
2383 mutex_lock(&data
->update_lock
);
2384 ret
= _pmbus_write_word_data(client
, reg
->page
, reg
->attr
->reg
, val
);
2385 mutex_unlock(&data
->update_lock
);
2387 return ret
? : count
;
2390 static int pmbus_add_samples_attr(struct pmbus_data
*data
, int page
,
2391 struct pmbus_samples_attr
*attr
)
2393 struct pmbus_samples_reg
*reg
;
2395 reg
= devm_kzalloc(data
->dev
, sizeof(*reg
), GFP_KERNEL
);
2402 pmbus_dev_attr_init(®
->dev_attr
, attr
->name
, 0644,
2403 pmbus_show_samples
, pmbus_set_samples
);
2405 return pmbus_add_attribute(data
, ®
->dev_attr
.attr
);
2408 static int pmbus_add_samples_attributes(struct i2c_client
*client
,
2409 struct pmbus_data
*data
)
2411 const struct pmbus_driver_info
*info
= data
->info
;
2414 if (!(info
->func
[0] & PMBUS_HAVE_SAMPLES
))
2417 for (s
= 0; s
< ARRAY_SIZE(pmbus_samples_registers
); s
++) {
2418 struct pmbus_samples_attr
*attr
;
2421 attr
= &pmbus_samples_registers
[s
];
2422 if (!pmbus_check_word_register(client
, 0, attr
->reg
))
2425 ret
= pmbus_add_samples_attr(data
, 0, attr
);
2433 static int pmbus_find_attributes(struct i2c_client
*client
,
2434 struct pmbus_data
*data
)
2438 /* Voltage sensors */
2439 ret
= pmbus_add_sensor_attrs(client
, data
, "in", voltage_attributes
,
2440 ARRAY_SIZE(voltage_attributes
));
2444 /* Current sensors */
2445 ret
= pmbus_add_sensor_attrs(client
, data
, "curr", current_attributes
,
2446 ARRAY_SIZE(current_attributes
));
2451 ret
= pmbus_add_sensor_attrs(client
, data
, "power", power_attributes
,
2452 ARRAY_SIZE(power_attributes
));
2456 /* Temperature sensors */
2457 ret
= pmbus_add_sensor_attrs(client
, data
, "temp", temp_attributes
,
2458 ARRAY_SIZE(temp_attributes
));
2463 ret
= pmbus_add_fan_attributes(client
, data
);
2467 ret
= pmbus_add_samples_attributes(client
, data
);
2472 * The pmbus_class_attr_map structure maps one sensor class to
2473 * it's corresponding sensor attributes array.
2475 struct pmbus_class_attr_map
{
2476 enum pmbus_sensor_classes
class;
2478 const struct pmbus_sensor_attr
*attr
;
2481 static const struct pmbus_class_attr_map class_attr_map
[] = {
2483 .class = PSC_VOLTAGE_IN
,
2484 .attr
= voltage_attributes
,
2485 .nattr
= ARRAY_SIZE(voltage_attributes
),
2487 .class = PSC_VOLTAGE_OUT
,
2488 .attr
= voltage_attributes
,
2489 .nattr
= ARRAY_SIZE(voltage_attributes
),
2491 .class = PSC_CURRENT_IN
,
2492 .attr
= current_attributes
,
2493 .nattr
= ARRAY_SIZE(current_attributes
),
2495 .class = PSC_CURRENT_OUT
,
2496 .attr
= current_attributes
,
2497 .nattr
= ARRAY_SIZE(current_attributes
),
2500 .attr
= power_attributes
,
2501 .nattr
= ARRAY_SIZE(power_attributes
),
2503 .class = PSC_TEMPERATURE
,
2504 .attr
= temp_attributes
,
2505 .nattr
= ARRAY_SIZE(temp_attributes
),
2510 * Read the coefficients for direct mode.
2512 static int pmbus_read_coefficients(struct i2c_client
*client
,
2513 struct pmbus_driver_info
*info
,
2514 const struct pmbus_sensor_attr
*attr
)
2517 union i2c_smbus_data data
;
2518 enum pmbus_sensor_classes
class = attr
->class;
2523 data
.block
[1] = attr
->reg
;
2524 data
.block
[2] = 0x01;
2527 rv
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2528 I2C_SMBUS_WRITE
, PMBUS_COEFFICIENTS
,
2529 I2C_SMBUS_BLOCK_PROC_CALL
, &data
);
2530 pmbus_update_ts(client
, true);
2535 if (data
.block
[0] != 5)
2538 m
= data
.block
[1] | (data
.block
[2] << 8);
2539 b
= data
.block
[3] | (data
.block
[4] << 8);
2548 static int pmbus_init_coefficients(struct i2c_client
*client
,
2549 struct pmbus_driver_info
*info
)
2551 int i
, n
, ret
= -EINVAL
;
2552 const struct pmbus_class_attr_map
*map
;
2553 const struct pmbus_sensor_attr
*attr
;
2555 for (i
= 0; i
< ARRAY_SIZE(class_attr_map
); i
++) {
2556 map
= &class_attr_map
[i
];
2557 if (info
->format
[map
->class] != direct
)
2559 for (n
= 0; n
< map
->nattr
; n
++) {
2560 attr
= &map
->attr
[n
];
2561 if (map
->class != attr
->class)
2563 ret
= pmbus_read_coefficients(client
, info
, attr
);
2568 dev_err(&client
->dev
,
2569 "No coefficients found for sensor class %d\n",
2579 * Identify chip parameters.
2580 * This function is called for all chips.
2582 static int pmbus_identify_common(struct i2c_client
*client
,
2583 struct pmbus_data
*data
, int page
)
2587 if (pmbus_check_byte_register(client
, page
, PMBUS_VOUT_MODE
))
2588 vout_mode
= _pmbus_read_byte_data(client
, page
,
2590 if (vout_mode
>= 0 && vout_mode
!= 0xff) {
2592 * Not all chips support the VOUT_MODE command,
2593 * so a failure to read it is not an error.
2595 switch (vout_mode
>> 5) {
2596 case 0: /* linear mode */
2597 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != linear
)
2600 data
->exponent
[page
] = ((s8
)(vout_mode
<< 3)) >> 3;
2602 case 1: /* VID mode */
2603 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != vid
)
2606 case 2: /* direct mode */
2607 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != direct
)
2610 case 3: /* ieee 754 half precision */
2611 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != ieee754
)
2622 static int pmbus_read_status_byte(struct i2c_client
*client
, int page
)
2624 return _pmbus_read_byte_data(client
, page
, PMBUS_STATUS_BYTE
);
2627 static int pmbus_read_status_word(struct i2c_client
*client
, int page
)
2629 return _pmbus_read_word_data(client
, page
, 0xff, PMBUS_STATUS_WORD
);
2632 /* PEC attribute support */
2634 static ssize_t
pec_show(struct device
*dev
, struct device_attribute
*dummy
,
2637 struct i2c_client
*client
= to_i2c_client(dev
);
2639 return sysfs_emit(buf
, "%d\n", !!(client
->flags
& I2C_CLIENT_PEC
));
2642 static ssize_t
pec_store(struct device
*dev
, struct device_attribute
*dummy
,
2643 const char *buf
, size_t count
)
2645 struct i2c_client
*client
= to_i2c_client(dev
);
2649 err
= kstrtobool(buf
, &enable
);
2654 client
->flags
|= I2C_CLIENT_PEC
;
2656 client
->flags
&= ~I2C_CLIENT_PEC
;
2661 static DEVICE_ATTR_RW(pec
);
2663 static void pmbus_remove_pec(void *dev
)
2665 device_remove_file(dev
, &dev_attr_pec
);
2668 static int pmbus_init_common(struct i2c_client
*client
, struct pmbus_data
*data
,
2669 struct pmbus_driver_info
*info
)
2671 struct device
*dev
= &client
->dev
;
2675 * Figure out if PEC is enabled before accessing any other register.
2676 * Make sure PEC is disabled, will be enabled later if needed.
2678 client
->flags
&= ~I2C_CLIENT_PEC
;
2680 /* Enable PEC if the controller and bus supports it */
2681 if (!(data
->flags
& PMBUS_NO_CAPABILITY
)) {
2683 ret
= i2c_smbus_read_byte_data(client
, PMBUS_CAPABILITY
);
2684 pmbus_update_ts(client
, false);
2686 if (ret
>= 0 && (ret
& PB_CAPABILITY_ERROR_CHECK
)) {
2687 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_PEC
))
2688 client
->flags
|= I2C_CLIENT_PEC
;
2693 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
2694 * to use PMBUS_STATUS_BYTE instead if that is the case.
2695 * Bail out if both registers are not supported.
2697 data
->read_status
= pmbus_read_status_word
;
2699 ret
= i2c_smbus_read_word_data(client
, PMBUS_STATUS_WORD
);
2700 pmbus_update_ts(client
, false);
2702 if (ret
< 0 || ret
== 0xffff) {
2703 data
->read_status
= pmbus_read_status_byte
;
2705 ret
= i2c_smbus_read_byte_data(client
, PMBUS_STATUS_BYTE
);
2706 pmbus_update_ts(client
, false);
2708 if (ret
< 0 || ret
== 0xff) {
2709 dev_err(dev
, "PMBus status register not found\n");
2713 data
->has_status_word
= true;
2717 * Check if the chip is write protected. If it is, we can not clear
2718 * faults, and we should not try it. Also, in that case, writes into
2719 * limit registers need to be disabled.
2721 if (!(data
->flags
& PMBUS_NO_WRITE_PROTECT
)) {
2722 ret
= _pmbus_read_byte_data(client
, -1, PMBUS_WRITE_PROTECT
);
2724 if (ret
> 0 && (ret
& PB_WP_ANY
))
2725 data
->flags
|= PMBUS_WRITE_PROTECTED
| PMBUS_SKIP_STATUS_CHECK
;
2728 ret
= i2c_smbus_read_byte_data(client
, PMBUS_REVISION
);
2730 data
->revision
= ret
;
2732 if (data
->info
->pages
)
2733 pmbus_clear_faults(client
);
2735 pmbus_clear_fault_page(client
, -1);
2737 if (info
->identify
) {
2738 ret
= (*info
->identify
)(client
, info
);
2740 dev_err(dev
, "Chip identification failed\n");
2745 if (info
->pages
<= 0 || info
->pages
> PMBUS_PAGES
) {
2746 dev_err(dev
, "Bad number of PMBus pages: %d\n", info
->pages
);
2750 for (page
= 0; page
< info
->pages
; page
++) {
2751 ret
= pmbus_identify_common(client
, data
, page
);
2753 dev_err(dev
, "Failed to identify chip capabilities\n");
2758 if (data
->flags
& PMBUS_USE_COEFFICIENTS_CMD
) {
2759 if (!i2c_check_functionality(client
->adapter
,
2760 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
))
2763 ret
= pmbus_init_coefficients(client
, info
);
2768 if (client
->flags
& I2C_CLIENT_PEC
) {
2770 * If I2C_CLIENT_PEC is set here, both the I2C adapter and the
2771 * chip support PEC. Add 'pec' attribute to client device to let
2772 * the user control it.
2774 ret
= device_create_file(dev
, &dev_attr_pec
);
2777 ret
= devm_add_action_or_reset(dev
, pmbus_remove_pec
, dev
);
2785 /* A PMBus status flag and the corresponding REGULATOR_ERROR_* and REGULATOR_EVENTS_* flag */
2786 struct pmbus_status_assoc
{
2787 int pflag
, rflag
, eflag
;
2790 /* PMBus->regulator bit mappings for a PMBus status register */
2791 struct pmbus_status_category
{
2794 const struct pmbus_status_assoc
*bits
; /* zero-terminated */
2797 static const struct pmbus_status_category __maybe_unused pmbus_status_flag_map
[] = {
2799 .func
= PMBUS_HAVE_STATUS_VOUT
,
2800 .reg
= PMBUS_STATUS_VOUT
,
2801 .bits
= (const struct pmbus_status_assoc
[]) {
2802 { PB_VOLTAGE_UV_WARNING
, REGULATOR_ERROR_UNDER_VOLTAGE_WARN
,
2803 REGULATOR_EVENT_UNDER_VOLTAGE_WARN
},
2804 { PB_VOLTAGE_UV_FAULT
, REGULATOR_ERROR_UNDER_VOLTAGE
,
2805 REGULATOR_EVENT_UNDER_VOLTAGE
},
2806 { PB_VOLTAGE_OV_WARNING
, REGULATOR_ERROR_OVER_VOLTAGE_WARN
,
2807 REGULATOR_EVENT_OVER_VOLTAGE_WARN
},
2808 { PB_VOLTAGE_OV_FAULT
, REGULATOR_ERROR_REGULATION_OUT
,
2809 REGULATOR_EVENT_OVER_VOLTAGE_WARN
},
2813 .func
= PMBUS_HAVE_STATUS_IOUT
,
2814 .reg
= PMBUS_STATUS_IOUT
,
2815 .bits
= (const struct pmbus_status_assoc
[]) {
2816 { PB_IOUT_OC_WARNING
, REGULATOR_ERROR_OVER_CURRENT_WARN
,
2817 REGULATOR_EVENT_OVER_CURRENT_WARN
},
2818 { PB_IOUT_OC_FAULT
, REGULATOR_ERROR_OVER_CURRENT
,
2819 REGULATOR_EVENT_OVER_CURRENT
},
2820 { PB_IOUT_OC_LV_FAULT
, REGULATOR_ERROR_OVER_CURRENT
,
2821 REGULATOR_EVENT_OVER_CURRENT
},
2825 .func
= PMBUS_HAVE_STATUS_TEMP
,
2826 .reg
= PMBUS_STATUS_TEMPERATURE
,
2827 .bits
= (const struct pmbus_status_assoc
[]) {
2828 { PB_TEMP_OT_WARNING
, REGULATOR_ERROR_OVER_TEMP_WARN
,
2829 REGULATOR_EVENT_OVER_TEMP_WARN
},
2830 { PB_TEMP_OT_FAULT
, REGULATOR_ERROR_OVER_TEMP
,
2831 REGULATOR_EVENT_OVER_TEMP
},
2837 static int _pmbus_is_enabled(struct i2c_client
*client
, u8 page
)
2841 ret
= _pmbus_read_byte_data(client
, page
, PMBUS_OPERATION
);
2846 return !!(ret
& PB_OPERATION_CONTROL_ON
);
2849 static int __maybe_unused
pmbus_is_enabled(struct i2c_client
*client
, u8 page
)
2851 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2854 mutex_lock(&data
->update_lock
);
2855 ret
= _pmbus_is_enabled(client
, page
);
2856 mutex_unlock(&data
->update_lock
);
2861 #define to_dev_attr(_dev_attr) \
2862 container_of(_dev_attr, struct device_attribute, attr)
2864 static void pmbus_notify(struct pmbus_data
*data
, int page
, int reg
, int flags
)
2868 for (i
= 0; i
< data
->num_attributes
; i
++) {
2869 struct device_attribute
*da
= to_dev_attr(data
->group
.attrs
[i
]);
2870 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
2871 int index
= attr
->index
;
2872 u16 smask
= pb_index_to_mask(index
);
2873 u8 spage
= pb_index_to_page(index
);
2874 u16 sreg
= pb_index_to_reg(index
);
2876 if (reg
== sreg
&& page
== spage
&& (smask
& flags
)) {
2877 dev_dbg(data
->dev
, "sysfs notify: %s", da
->attr
.name
);
2878 sysfs_notify(&data
->dev
->kobj
, NULL
, da
->attr
.name
);
2879 kobject_uevent(&data
->dev
->kobj
, KOBJ_CHANGE
);
2888 static int _pmbus_get_flags(struct pmbus_data
*data
, u8 page
, unsigned int *flags
,
2889 unsigned int *event
, bool notify
)
2892 const struct pmbus_status_category
*cat
;
2893 const struct pmbus_status_assoc
*bit
;
2894 struct device
*dev
= data
->dev
;
2895 struct i2c_client
*client
= to_i2c_client(dev
);
2896 int func
= data
->info
->func
[page
];
2901 for (i
= 0; i
< ARRAY_SIZE(pmbus_status_flag_map
); i
++) {
2902 cat
= &pmbus_status_flag_map
[i
];
2903 if (!(func
& cat
->func
))
2906 status
= _pmbus_read_byte_data(client
, page
, cat
->reg
);
2910 for (bit
= cat
->bits
; bit
->pflag
; bit
++)
2911 if (status
& bit
->pflag
) {
2912 *flags
|= bit
->rflag
;
2913 *event
|= bit
->eflag
;
2916 if (notify
&& status
)
2917 pmbus_notify(data
, page
, cat
->reg
, status
);
2922 * Map what bits of STATUS_{WORD,BYTE} we can to REGULATOR_ERROR_*
2923 * bits. Some of the other bits are tempting (especially for cases
2924 * where we don't have the relevant PMBUS_HAVE_STATUS_*
2925 * functionality), but there's an unfortunate ambiguity in that
2926 * they're defined as indicating a fault *or* a warning, so we can't
2927 * easily determine whether to report REGULATOR_ERROR_<foo> or
2928 * REGULATOR_ERROR_<foo>_WARN.
2930 status
= pmbus_get_status(client
, page
, PMBUS_STATUS_WORD
);
2934 if (_pmbus_is_enabled(client
, page
)) {
2935 if (status
& PB_STATUS_OFF
) {
2936 *flags
|= REGULATOR_ERROR_FAIL
;
2937 *event
|= REGULATOR_EVENT_FAIL
;
2940 if (status
& PB_STATUS_POWER_GOOD_N
) {
2941 *flags
|= REGULATOR_ERROR_REGULATION_OUT
;
2942 *event
|= REGULATOR_EVENT_REGULATION_OUT
;
2946 * Unlike most other status bits, PB_STATUS_{IOUT_OC,VOUT_OV} are
2947 * defined strictly as fault indicators (not warnings).
2949 if (status
& PB_STATUS_IOUT_OC
) {
2950 *flags
|= REGULATOR_ERROR_OVER_CURRENT
;
2951 *event
|= REGULATOR_EVENT_OVER_CURRENT
;
2953 if (status
& PB_STATUS_VOUT_OV
) {
2954 *flags
|= REGULATOR_ERROR_REGULATION_OUT
;
2955 *event
|= REGULATOR_EVENT_FAIL
;
2959 * If we haven't discovered any thermal faults or warnings via
2960 * PMBUS_STATUS_TEMPERATURE, map PB_STATUS_TEMPERATURE to a warning as
2961 * a (conservative) best-effort interpretation.
2963 if (!(*flags
& (REGULATOR_ERROR_OVER_TEMP
| REGULATOR_ERROR_OVER_TEMP_WARN
)) &&
2964 (status
& PB_STATUS_TEMPERATURE
)) {
2965 *flags
|= REGULATOR_ERROR_OVER_TEMP_WARN
;
2966 *event
|= REGULATOR_EVENT_OVER_TEMP_WARN
;
2973 static int __maybe_unused
pmbus_get_flags(struct pmbus_data
*data
, u8 page
, unsigned int *flags
,
2974 unsigned int *event
, bool notify
)
2978 mutex_lock(&data
->update_lock
);
2979 ret
= _pmbus_get_flags(data
, page
, flags
, event
, notify
);
2980 mutex_unlock(&data
->update_lock
);
2985 #if IS_ENABLED(CONFIG_REGULATOR)
2986 static int pmbus_regulator_is_enabled(struct regulator_dev
*rdev
)
2988 struct device
*dev
= rdev_get_dev(rdev
);
2989 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2991 return pmbus_is_enabled(client
, rdev_get_id(rdev
));
2994 static int _pmbus_regulator_on_off(struct regulator_dev
*rdev
, bool enable
)
2996 struct device
*dev
= rdev_get_dev(rdev
);
2997 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2998 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2999 u8 page
= rdev_get_id(rdev
);
3002 mutex_lock(&data
->update_lock
);
3003 ret
= pmbus_update_byte_data(client
, page
, PMBUS_OPERATION
,
3004 PB_OPERATION_CONTROL_ON
,
3005 enable
? PB_OPERATION_CONTROL_ON
: 0);
3006 mutex_unlock(&data
->update_lock
);
3011 static int pmbus_regulator_enable(struct regulator_dev
*rdev
)
3013 return _pmbus_regulator_on_off(rdev
, 1);
3016 static int pmbus_regulator_disable(struct regulator_dev
*rdev
)
3018 return _pmbus_regulator_on_off(rdev
, 0);
3021 static int pmbus_regulator_get_error_flags(struct regulator_dev
*rdev
, unsigned int *flags
)
3023 struct device
*dev
= rdev_get_dev(rdev
);
3024 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
3025 struct pmbus_data
*data
= i2c_get_clientdata(client
);
3028 return pmbus_get_flags(data
, rdev_get_id(rdev
), flags
, &event
, false);
3031 static int pmbus_regulator_get_status(struct regulator_dev
*rdev
)
3033 struct device
*dev
= rdev_get_dev(rdev
);
3034 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
3035 struct pmbus_data
*data
= i2c_get_clientdata(client
);
3036 u8 page
= rdev_get_id(rdev
);
3040 mutex_lock(&data
->update_lock
);
3041 status
= pmbus_get_status(client
, page
, PMBUS_STATUS_WORD
);
3047 if (status
& PB_STATUS_OFF
) {
3048 ret
= REGULATOR_STATUS_OFF
;
3052 /* If regulator is ON & reports power good then return ON */
3053 if (!(status
& PB_STATUS_POWER_GOOD_N
)) {
3054 ret
= REGULATOR_STATUS_ON
;
3058 ret
= _pmbus_get_flags(data
, rdev_get_id(rdev
), &status
, &event
, false);
3062 if (status
& (REGULATOR_ERROR_UNDER_VOLTAGE
| REGULATOR_ERROR_OVER_CURRENT
|
3063 REGULATOR_ERROR_REGULATION_OUT
| REGULATOR_ERROR_FAIL
| REGULATOR_ERROR_OVER_TEMP
)) {
3064 ret
= REGULATOR_STATUS_ERROR
;
3068 ret
= REGULATOR_STATUS_UNDEFINED
;
3071 mutex_unlock(&data
->update_lock
);
3075 static int pmbus_regulator_get_low_margin(struct i2c_client
*client
, int page
)
3077 struct pmbus_data
*data
= i2c_get_clientdata(client
);
3078 struct pmbus_sensor s
= {
3080 .class = PSC_VOLTAGE_OUT
,
3085 if (data
->vout_low
[page
] < 0) {
3086 if (pmbus_check_word_register(client
, page
, PMBUS_MFR_VOUT_MIN
))
3087 s
.data
= _pmbus_read_word_data(client
, page
, 0xff,
3088 PMBUS_MFR_VOUT_MIN
);
3090 s
.data
= _pmbus_read_word_data(client
, page
, 0xff,
3091 PMBUS_VOUT_MARGIN_LOW
);
3095 data
->vout_low
[page
] = pmbus_reg2data(data
, &s
);
3098 return data
->vout_low
[page
];
3101 static int pmbus_regulator_get_high_margin(struct i2c_client
*client
, int page
)
3103 struct pmbus_data
*data
= i2c_get_clientdata(client
);
3104 struct pmbus_sensor s
= {
3106 .class = PSC_VOLTAGE_OUT
,
3111 if (data
->vout_high
[page
] < 0) {
3112 if (pmbus_check_word_register(client
, page
, PMBUS_MFR_VOUT_MAX
))
3113 s
.data
= _pmbus_read_word_data(client
, page
, 0xff,
3114 PMBUS_MFR_VOUT_MAX
);
3116 s
.data
= _pmbus_read_word_data(client
, page
, 0xff,
3117 PMBUS_VOUT_MARGIN_HIGH
);
3121 data
->vout_high
[page
] = pmbus_reg2data(data
, &s
);
3124 return data
->vout_high
[page
];
3127 static int pmbus_regulator_get_voltage(struct regulator_dev
*rdev
)
3129 struct device
*dev
= rdev_get_dev(rdev
);
3130 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
3131 struct pmbus_data
*data
= i2c_get_clientdata(client
);
3132 struct pmbus_sensor s
= {
3133 .page
= rdev_get_id(rdev
),
3134 .class = PSC_VOLTAGE_OUT
,
3138 s
.data
= _pmbus_read_word_data(client
, s
.page
, 0xff, PMBUS_READ_VOUT
);
3142 return (int)pmbus_reg2data(data
, &s
) * 1000; /* unit is uV */
3145 static int pmbus_regulator_set_voltage(struct regulator_dev
*rdev
, int min_uv
,
3146 int max_uv
, unsigned int *selector
)
3148 struct device
*dev
= rdev_get_dev(rdev
);
3149 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
3150 struct pmbus_data
*data
= i2c_get_clientdata(client
);
3151 struct pmbus_sensor s
= {
3152 .page
= rdev_get_id(rdev
),
3153 .class = PSC_VOLTAGE_OUT
,
3157 int val
= DIV_ROUND_CLOSEST(min_uv
, 1000); /* convert to mV */
3162 low
= pmbus_regulator_get_low_margin(client
, s
.page
);
3166 high
= pmbus_regulator_get_high_margin(client
, s
.page
);
3170 /* Make sure we are within margins */
3176 val
= pmbus_data2reg(data
, &s
, val
);
3178 return _pmbus_write_word_data(client
, s
.page
, PMBUS_VOUT_COMMAND
, (u16
)val
);
3181 static int pmbus_regulator_list_voltage(struct regulator_dev
*rdev
,
3182 unsigned int selector
)
3184 struct device
*dev
= rdev_get_dev(rdev
);
3185 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
3188 if (selector
>= rdev
->desc
->n_voltages
||
3189 selector
< rdev
->desc
->linear_min_sel
)
3192 selector
-= rdev
->desc
->linear_min_sel
;
3193 val
= DIV_ROUND_CLOSEST(rdev
->desc
->min_uV
+
3194 (rdev
->desc
->uV_step
* selector
), 1000); /* convert to mV */
3196 low
= pmbus_regulator_get_low_margin(client
, rdev_get_id(rdev
));
3200 high
= pmbus_regulator_get_high_margin(client
, rdev_get_id(rdev
));
3204 if (val
>= low
&& val
<= high
)
3205 return val
* 1000; /* unit is uV */
3210 const struct regulator_ops pmbus_regulator_ops
= {
3211 .enable
= pmbus_regulator_enable
,
3212 .disable
= pmbus_regulator_disable
,
3213 .is_enabled
= pmbus_regulator_is_enabled
,
3214 .get_error_flags
= pmbus_regulator_get_error_flags
,
3215 .get_status
= pmbus_regulator_get_status
,
3216 .get_voltage
= pmbus_regulator_get_voltage
,
3217 .set_voltage
= pmbus_regulator_set_voltage
,
3218 .list_voltage
= pmbus_regulator_list_voltage
,
3220 EXPORT_SYMBOL_NS_GPL(pmbus_regulator_ops
, "PMBUS");
3222 static int pmbus_regulator_register(struct pmbus_data
*data
)
3224 struct device
*dev
= data
->dev
;
3225 const struct pmbus_driver_info
*info
= data
->info
;
3226 const struct pmbus_platform_data
*pdata
= dev_get_platdata(dev
);
3229 data
->rdevs
= devm_kzalloc(dev
, sizeof(struct regulator_dev
*) * info
->num_regulators
,
3234 for (i
= 0; i
< info
->num_regulators
; i
++) {
3235 struct regulator_config config
= { };
3238 config
.driver_data
= data
;
3240 if (pdata
&& pdata
->reg_init_data
)
3241 config
.init_data
= &pdata
->reg_init_data
[i
];
3243 data
->rdevs
[i
] = devm_regulator_register(dev
, &info
->reg_desc
[i
],
3245 if (IS_ERR(data
->rdevs
[i
]))
3246 return dev_err_probe(dev
, PTR_ERR(data
->rdevs
[i
]),
3247 "Failed to register %s regulator\n",
3248 info
->reg_desc
[i
].name
);
3254 static int pmbus_regulator_notify(struct pmbus_data
*data
, int page
, int event
)
3258 for (j
= 0; j
< data
->info
->num_regulators
; j
++) {
3259 if (page
== rdev_get_id(data
->rdevs
[j
])) {
3260 regulator_notifier_call_chain(data
->rdevs
[j
], event
, NULL
);
3267 static int pmbus_regulator_register(struct pmbus_data
*data
)
3272 static int pmbus_regulator_notify(struct pmbus_data
*data
, int page
, int event
)
3278 static int pmbus_write_smbalert_mask(struct i2c_client
*client
, u8 page
, u8 reg
, u8 val
)
3282 ret
= _pmbus_write_word_data(client
, page
, PMBUS_SMBALERT_MASK
, reg
| (val
<< 8));
3285 * Clear fault systematically in case writing PMBUS_SMBALERT_MASK
3286 * is not supported by the chip.
3288 pmbus_clear_fault_page(client
, page
);
3293 static irqreturn_t
pmbus_fault_handler(int irq
, void *pdata
)
3295 struct pmbus_data
*data
= pdata
;
3296 struct i2c_client
*client
= to_i2c_client(data
->dev
);
3298 int i
, status
, event
;
3299 mutex_lock(&data
->update_lock
);
3300 for (i
= 0; i
< data
->info
->pages
; i
++) {
3301 _pmbus_get_flags(data
, i
, &status
, &event
, true);
3304 pmbus_regulator_notify(data
, i
, event
);
3307 pmbus_clear_faults(client
);
3308 mutex_unlock(&data
->update_lock
);
3313 static int pmbus_irq_setup(struct i2c_client
*client
, struct pmbus_data
*data
)
3315 struct device
*dev
= &client
->dev
;
3316 const struct pmbus_status_category
*cat
;
3317 const struct pmbus_status_assoc
*bit
;
3318 int i
, j
, err
, func
;
3321 static const u8 misc_status
[] = {PMBUS_STATUS_CML
, PMBUS_STATUS_OTHER
,
3322 PMBUS_STATUS_MFR_SPECIFIC
, PMBUS_STATUS_FAN_12
,
3323 PMBUS_STATUS_FAN_34
};
3328 for (i
= 0; i
< data
->info
->pages
; i
++) {
3329 func
= data
->info
->func
[i
];
3331 for (j
= 0; j
< ARRAY_SIZE(pmbus_status_flag_map
); j
++) {
3332 cat
= &pmbus_status_flag_map
[j
];
3333 if (!(func
& cat
->func
))
3336 for (bit
= cat
->bits
; bit
->pflag
; bit
++)
3339 err
= pmbus_write_smbalert_mask(client
, i
, cat
->reg
, ~mask
);
3341 dev_dbg_once(dev
, "Failed to set smbalert for reg 0x%02x\n",
3345 for (j
= 0; j
< ARRAY_SIZE(misc_status
); j
++)
3346 pmbus_write_smbalert_mask(client
, i
, misc_status
[j
], 0xff);
3349 /* Register notifiers */
3350 err
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, pmbus_fault_handler
,
3351 IRQF_ONESHOT
, "pmbus-irq", data
);
3353 dev_err(dev
, "failed to request an irq %d\n", err
);
3360 static struct dentry
*pmbus_debugfs_dir
; /* pmbus debugfs directory */
3362 #if IS_ENABLED(CONFIG_DEBUG_FS)
3363 static int pmbus_debugfs_get(void *data
, u64
*val
)
3366 struct pmbus_debugfs_entry
*entry
= data
;
3367 struct pmbus_data
*pdata
= i2c_get_clientdata(entry
->client
);
3369 rc
= mutex_lock_interruptible(&pdata
->update_lock
);
3372 rc
= _pmbus_read_byte_data(entry
->client
, entry
->page
, entry
->reg
);
3373 mutex_unlock(&pdata
->update_lock
);
3381 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops
, pmbus_debugfs_get
, NULL
,
3384 static int pmbus_debugfs_get_status(void *data
, u64
*val
)
3387 struct pmbus_debugfs_entry
*entry
= data
;
3388 struct pmbus_data
*pdata
= i2c_get_clientdata(entry
->client
);
3390 rc
= mutex_lock_interruptible(&pdata
->update_lock
);
3393 rc
= pdata
->read_status(entry
->client
, entry
->page
);
3394 mutex_unlock(&pdata
->update_lock
);
3402 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status
, pmbus_debugfs_get_status
,
3403 NULL
, "0x%04llx\n");
3405 static ssize_t
pmbus_debugfs_mfr_read(struct file
*file
, char __user
*buf
,
3406 size_t count
, loff_t
*ppos
)
3409 struct pmbus_debugfs_entry
*entry
= file
->private_data
;
3410 struct pmbus_data
*pdata
= i2c_get_clientdata(entry
->client
);
3411 char data
[I2C_SMBUS_BLOCK_MAX
+ 2] = { 0 };
3413 rc
= mutex_lock_interruptible(&pdata
->update_lock
);
3416 rc
= pmbus_read_block_data(entry
->client
, entry
->page
, entry
->reg
,
3418 mutex_unlock(&pdata
->update_lock
);
3422 /* Add newline at the end of a read data */
3425 /* Include newline into the length */
3428 return simple_read_from_buffer(buf
, count
, ppos
, data
, rc
);
3431 static const struct file_operations pmbus_debugfs_ops_mfr
= {
3432 .llseek
= noop_llseek
,
3433 .read
= pmbus_debugfs_mfr_read
,
3435 .open
= simple_open
,
3438 static void pmbus_remove_debugfs(void *data
)
3440 struct dentry
*entry
= data
;
3442 debugfs_remove_recursive(entry
);
3445 static int pmbus_init_debugfs(struct i2c_client
*client
,
3446 struct pmbus_data
*data
)
3449 char name
[PMBUS_NAME_SIZE
];
3450 struct pmbus_debugfs_entry
*entries
;
3452 if (!pmbus_debugfs_dir
)
3456 * Create the debugfs directory for this device. Use the hwmon device
3457 * name to avoid conflicts (hwmon numbers are globally unique).
3459 data
->debugfs
= debugfs_create_dir(dev_name(data
->hwmon_dev
),
3461 if (IS_ERR_OR_NULL(data
->debugfs
)) {
3462 data
->debugfs
= NULL
;
3467 * Allocate the max possible entries we need.
3468 * 6 entries device-specific
3469 * 10 entries page-specific
3471 entries
= devm_kcalloc(data
->dev
,
3472 6 + data
->info
->pages
* 10, sizeof(*entries
),
3478 * Add device-specific entries.
3479 * Please note that the PMBUS standard allows all registers to be
3481 * To reduce the number of debugfs entries for devices with many pages
3482 * assume that values of the following registers are the same for all
3483 * pages and report values only for page 0.
3485 if (pmbus_check_block_register(client
, 0, PMBUS_MFR_ID
)) {
3486 entries
[idx
].client
= client
;
3487 entries
[idx
].page
= 0;
3488 entries
[idx
].reg
= PMBUS_MFR_ID
;
3489 debugfs_create_file("mfr_id", 0444, data
->debugfs
,
3491 &pmbus_debugfs_ops_mfr
);
3494 if (pmbus_check_block_register(client
, 0, PMBUS_MFR_MODEL
)) {
3495 entries
[idx
].client
= client
;
3496 entries
[idx
].page
= 0;
3497 entries
[idx
].reg
= PMBUS_MFR_MODEL
;
3498 debugfs_create_file("mfr_model", 0444, data
->debugfs
,
3500 &pmbus_debugfs_ops_mfr
);
3503 if (pmbus_check_block_register(client
, 0, PMBUS_MFR_REVISION
)) {
3504 entries
[idx
].client
= client
;
3505 entries
[idx
].page
= 0;
3506 entries
[idx
].reg
= PMBUS_MFR_REVISION
;
3507 debugfs_create_file("mfr_revision", 0444, data
->debugfs
,
3509 &pmbus_debugfs_ops_mfr
);
3512 if (pmbus_check_block_register(client
, 0, PMBUS_MFR_LOCATION
)) {
3513 entries
[idx
].client
= client
;
3514 entries
[idx
].page
= 0;
3515 entries
[idx
].reg
= PMBUS_MFR_LOCATION
;
3516 debugfs_create_file("mfr_location", 0444, data
->debugfs
,
3518 &pmbus_debugfs_ops_mfr
);
3521 if (pmbus_check_block_register(client
, 0, PMBUS_MFR_DATE
)) {
3522 entries
[idx
].client
= client
;
3523 entries
[idx
].page
= 0;
3524 entries
[idx
].reg
= PMBUS_MFR_DATE
;
3525 debugfs_create_file("mfr_date", 0444, data
->debugfs
,
3527 &pmbus_debugfs_ops_mfr
);
3530 if (pmbus_check_block_register(client
, 0, PMBUS_MFR_SERIAL
)) {
3531 entries
[idx
].client
= client
;
3532 entries
[idx
].page
= 0;
3533 entries
[idx
].reg
= PMBUS_MFR_SERIAL
;
3534 debugfs_create_file("mfr_serial", 0444, data
->debugfs
,
3536 &pmbus_debugfs_ops_mfr
);
3539 /* Add page specific entries */
3540 for (i
= 0; i
< data
->info
->pages
; ++i
) {
3541 /* Check accessibility of status register if it's not page 0 */
3542 if (!i
|| pmbus_check_status_register(client
, i
)) {
3543 /* No need to set reg as we have special read op. */
3544 entries
[idx
].client
= client
;
3545 entries
[idx
].page
= i
;
3546 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d", i
);
3547 debugfs_create_file(name
, 0444, data
->debugfs
,
3549 &pmbus_debugfs_ops_status
);
3552 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_VOUT
) {
3553 entries
[idx
].client
= client
;
3554 entries
[idx
].page
= i
;
3555 entries
[idx
].reg
= PMBUS_STATUS_VOUT
;
3556 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_vout", i
);
3557 debugfs_create_file(name
, 0444, data
->debugfs
,
3559 &pmbus_debugfs_ops
);
3562 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_IOUT
) {
3563 entries
[idx
].client
= client
;
3564 entries
[idx
].page
= i
;
3565 entries
[idx
].reg
= PMBUS_STATUS_IOUT
;
3566 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_iout", i
);
3567 debugfs_create_file(name
, 0444, data
->debugfs
,
3569 &pmbus_debugfs_ops
);
3572 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_INPUT
) {
3573 entries
[idx
].client
= client
;
3574 entries
[idx
].page
= i
;
3575 entries
[idx
].reg
= PMBUS_STATUS_INPUT
;
3576 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_input", i
);
3577 debugfs_create_file(name
, 0444, data
->debugfs
,
3579 &pmbus_debugfs_ops
);
3582 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_TEMP
) {
3583 entries
[idx
].client
= client
;
3584 entries
[idx
].page
= i
;
3585 entries
[idx
].reg
= PMBUS_STATUS_TEMPERATURE
;
3586 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_temp", i
);
3587 debugfs_create_file(name
, 0444, data
->debugfs
,
3589 &pmbus_debugfs_ops
);
3592 if (pmbus_check_byte_register(client
, i
, PMBUS_STATUS_CML
)) {
3593 entries
[idx
].client
= client
;
3594 entries
[idx
].page
= i
;
3595 entries
[idx
].reg
= PMBUS_STATUS_CML
;
3596 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_cml", i
);
3597 debugfs_create_file(name
, 0444, data
->debugfs
,
3599 &pmbus_debugfs_ops
);
3602 if (pmbus_check_byte_register(client
, i
, PMBUS_STATUS_OTHER
)) {
3603 entries
[idx
].client
= client
;
3604 entries
[idx
].page
= i
;
3605 entries
[idx
].reg
= PMBUS_STATUS_OTHER
;
3606 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_other", i
);
3607 debugfs_create_file(name
, 0444, data
->debugfs
,
3609 &pmbus_debugfs_ops
);
3612 if (pmbus_check_byte_register(client
, i
,
3613 PMBUS_STATUS_MFR_SPECIFIC
)) {
3614 entries
[idx
].client
= client
;
3615 entries
[idx
].page
= i
;
3616 entries
[idx
].reg
= PMBUS_STATUS_MFR_SPECIFIC
;
3617 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_mfr", i
);
3618 debugfs_create_file(name
, 0444, data
->debugfs
,
3620 &pmbus_debugfs_ops
);
3623 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_FAN12
) {
3624 entries
[idx
].client
= client
;
3625 entries
[idx
].page
= i
;
3626 entries
[idx
].reg
= PMBUS_STATUS_FAN_12
;
3627 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_fan12", i
);
3628 debugfs_create_file(name
, 0444, data
->debugfs
,
3630 &pmbus_debugfs_ops
);
3633 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_FAN34
) {
3634 entries
[idx
].client
= client
;
3635 entries
[idx
].page
= i
;
3636 entries
[idx
].reg
= PMBUS_STATUS_FAN_34
;
3637 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_fan34", i
);
3638 debugfs_create_file(name
, 0444, data
->debugfs
,
3640 &pmbus_debugfs_ops
);
3644 return devm_add_action_or_reset(data
->dev
,
3645 pmbus_remove_debugfs
, data
->debugfs
);
3648 static int pmbus_init_debugfs(struct i2c_client
*client
,
3649 struct pmbus_data
*data
)
3653 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */
3655 int pmbus_do_probe(struct i2c_client
*client
, struct pmbus_driver_info
*info
)
3657 struct device
*dev
= &client
->dev
;
3658 const struct pmbus_platform_data
*pdata
= dev_get_platdata(dev
);
3659 struct pmbus_data
*data
;
3660 size_t groups_num
= 0;
3668 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WRITE_BYTE
3669 | I2C_FUNC_SMBUS_BYTE_DATA
3670 | I2C_FUNC_SMBUS_WORD_DATA
))
3673 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
3678 while (info
->groups
[groups_num
])
3681 data
->groups
= devm_kcalloc(dev
, groups_num
+ 2, sizeof(void *),
3686 i2c_set_clientdata(client
, data
);
3687 mutex_init(&data
->update_lock
);
3691 data
->flags
= pdata
->flags
;
3693 data
->currpage
= -1;
3694 data
->currphase
= -1;
3696 for (i
= 0; i
< ARRAY_SIZE(data
->vout_low
); i
++) {
3697 data
->vout_low
[i
] = -1;
3698 data
->vout_high
[i
] = -1;
3701 ret
= pmbus_init_common(client
, data
, info
);
3705 ret
= pmbus_find_attributes(client
, data
);
3710 * If there are no attributes, something is wrong.
3711 * Bail out instead of trying to register nothing.
3713 if (!data
->num_attributes
) {
3714 dev_err(dev
, "No attributes found\n");
3718 name
= devm_kstrdup(dev
, client
->name
, GFP_KERNEL
);
3721 strreplace(name
, '-', '_');
3723 data
->groups
[0] = &data
->group
;
3724 memcpy(data
->groups
+ 1, info
->groups
, sizeof(void *) * groups_num
);
3725 data
->hwmon_dev
= devm_hwmon_device_register_with_groups(dev
,
3726 name
, data
, data
->groups
);
3727 if (IS_ERR(data
->hwmon_dev
)) {
3728 dev_err(dev
, "Failed to register hwmon device\n");
3729 return PTR_ERR(data
->hwmon_dev
);
3732 ret
= pmbus_regulator_register(data
);
3736 ret
= pmbus_irq_setup(client
, data
);
3740 ret
= pmbus_init_debugfs(client
, data
);
3742 dev_warn(dev
, "Failed to register debugfs\n");
3746 EXPORT_SYMBOL_NS_GPL(pmbus_do_probe
, "PMBUS");
3748 struct dentry
*pmbus_get_debugfs_dir(struct i2c_client
*client
)
3750 struct pmbus_data
*data
= i2c_get_clientdata(client
);
3752 return data
->debugfs
;
3754 EXPORT_SYMBOL_NS_GPL(pmbus_get_debugfs_dir
, "PMBUS");
3756 int pmbus_lock_interruptible(struct i2c_client
*client
)
3758 struct pmbus_data
*data
= i2c_get_clientdata(client
);
3760 return mutex_lock_interruptible(&data
->update_lock
);
3762 EXPORT_SYMBOL_NS_GPL(pmbus_lock_interruptible
, "PMBUS");
3764 void pmbus_unlock(struct i2c_client
*client
)
3766 struct pmbus_data
*data
= i2c_get_clientdata(client
);
3768 mutex_unlock(&data
->update_lock
);
3770 EXPORT_SYMBOL_NS_GPL(pmbus_unlock
, "PMBUS");
3772 static int __init
pmbus_core_init(void)
3774 pmbus_debugfs_dir
= debugfs_create_dir("pmbus", NULL
);
3775 if (IS_ERR(pmbus_debugfs_dir
))
3776 pmbus_debugfs_dir
= NULL
;
3781 static void __exit
pmbus_core_exit(void)
3783 debugfs_remove_recursive(pmbus_debugfs_dir
);
3786 module_init(pmbus_core_init
);
3787 module_exit(pmbus_core_exit
);
3789 MODULE_AUTHOR("Guenter Roeck");
3790 MODULE_DESCRIPTION("PMBus core driver");
3791 MODULE_LICENSE("GPL");