2 * Hardware monitoring driver for PMBus devices
4 * Copyright (c) 2010, 2011 Ericsson AB.
5 * Copyright (c) 2012 Guenter Roeck
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/debugfs.h>
23 #include <linux/kernel.h>
24 #include <linux/math64.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/err.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/jiffies.h>
33 #include <linux/pmbus.h>
34 #include <linux/regulator/driver.h>
35 #include <linux/regulator/machine.h>
39 * Number of additional attribute pointers to allocate
40 * with each call to krealloc
42 #define PMBUS_ATTR_ALLOC_SIZE 32
45 * Index into status register array, per status register group
47 #define PB_STATUS_BASE 0
48 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
49 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
50 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
51 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
52 #define PB_STATUS_TEMP_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
53 #define PB_STATUS_INPUT_BASE (PB_STATUS_TEMP_BASE + PMBUS_PAGES)
54 #define PB_STATUS_VMON_BASE (PB_STATUS_INPUT_BASE + 1)
56 #define PB_NUM_STATUS_REG (PB_STATUS_VMON_BASE + 1)
58 #define PMBUS_NAME_SIZE 24
61 struct pmbus_sensor
*next
;
62 char name
[PMBUS_NAME_SIZE
]; /* sysfs sensor name */
63 struct device_attribute attribute
;
64 u8 page
; /* page number */
65 u16 reg
; /* register */
66 enum pmbus_sensor_classes
class; /* sensor class */
67 bool update
; /* runtime sensor update needed */
68 bool convert
; /* Whether or not to apply linear/vid/direct */
69 int data
; /* Sensor data.
70 Negative if there was a read error */
72 #define to_pmbus_sensor(_attr) \
73 container_of(_attr, struct pmbus_sensor, attribute)
75 struct pmbus_boolean
{
76 char name
[PMBUS_NAME_SIZE
]; /* sysfs boolean name */
77 struct sensor_device_attribute attribute
;
78 struct pmbus_sensor
*s1
;
79 struct pmbus_sensor
*s2
;
81 #define to_pmbus_boolean(_attr) \
82 container_of(_attr, struct pmbus_boolean, attribute)
85 char name
[PMBUS_NAME_SIZE
]; /* sysfs label name */
86 struct device_attribute attribute
;
87 char label
[PMBUS_NAME_SIZE
]; /* label */
89 #define to_pmbus_label(_attr) \
90 container_of(_attr, struct pmbus_label, attribute)
94 struct device
*hwmon_dev
;
96 u32 flags
; /* from platform data */
98 int exponent
[PMBUS_PAGES
];
99 /* linear mode: exponent for output voltages */
101 const struct pmbus_driver_info
*info
;
105 struct attribute_group group
;
106 const struct attribute_group
*groups
[2];
107 struct dentry
*debugfs
; /* debugfs device directory */
109 struct pmbus_sensor
*sensors
;
111 struct mutex update_lock
;
113 unsigned long last_updated
; /* in jiffies */
116 * A single status register covers multiple attributes,
117 * so we keep them all together.
119 u16 status
[PB_NUM_STATUS_REG
];
121 bool has_status_word
; /* device uses STATUS_WORD register */
122 int (*read_status
)(struct i2c_client
*client
, int page
);
127 struct pmbus_debugfs_entry
{
128 struct i2c_client
*client
;
133 static const int pmbus_fan_rpm_mask
[] = {
140 static const int pmbus_fan_config_registers
[] = {
147 static const int pmbus_fan_command_registers
[] = {
154 void pmbus_clear_cache(struct i2c_client
*client
)
156 struct pmbus_data
*data
= i2c_get_clientdata(client
);
160 EXPORT_SYMBOL_GPL(pmbus_clear_cache
);
162 int pmbus_set_page(struct i2c_client
*client
, int page
)
164 struct pmbus_data
*data
= i2c_get_clientdata(client
);
167 if (page
< 0 || page
== data
->currpage
)
170 if (!(data
->info
->func
[page
] & PMBUS_PAGE_VIRTUAL
)) {
171 rv
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, page
);
175 rv
= i2c_smbus_read_byte_data(client
, PMBUS_PAGE
);
183 data
->currpage
= page
;
187 EXPORT_SYMBOL_GPL(pmbus_set_page
);
189 int pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
193 rv
= pmbus_set_page(client
, page
);
197 return i2c_smbus_write_byte(client
, value
);
199 EXPORT_SYMBOL_GPL(pmbus_write_byte
);
202 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
203 * a device specific mapping function exists and calls it if necessary.
205 static int _pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
207 struct pmbus_data
*data
= i2c_get_clientdata(client
);
208 const struct pmbus_driver_info
*info
= data
->info
;
211 if (info
->write_byte
) {
212 status
= info
->write_byte(client
, page
, value
);
213 if (status
!= -ENODATA
)
216 return pmbus_write_byte(client
, page
, value
);
219 int pmbus_write_word_data(struct i2c_client
*client
, int page
, u8 reg
,
224 rv
= pmbus_set_page(client
, page
);
228 return i2c_smbus_write_word_data(client
, reg
, word
);
230 EXPORT_SYMBOL_GPL(pmbus_write_word_data
);
233 static int pmbus_write_virt_reg(struct i2c_client
*client
, int page
, int reg
,
241 case PMBUS_VIRT_FAN_TARGET_1
... PMBUS_VIRT_FAN_TARGET_4
:
242 id
= reg
- PMBUS_VIRT_FAN_TARGET_1
;
243 bit
= pmbus_fan_rpm_mask
[id
];
244 rv
= pmbus_update_fan(client
, page
, id
, bit
, bit
, word
);
255 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
256 * a device specific mapping function exists and calls it if necessary.
258 static int _pmbus_write_word_data(struct i2c_client
*client
, int page
, int reg
,
261 struct pmbus_data
*data
= i2c_get_clientdata(client
);
262 const struct pmbus_driver_info
*info
= data
->info
;
265 if (info
->write_word_data
) {
266 status
= info
->write_word_data(client
, page
, reg
, word
);
267 if (status
!= -ENODATA
)
271 if (reg
>= PMBUS_VIRT_BASE
)
272 return pmbus_write_virt_reg(client
, page
, reg
, word
);
274 return pmbus_write_word_data(client
, page
, reg
, word
);
277 int pmbus_update_fan(struct i2c_client
*client
, int page
, int id
,
278 u8 config
, u8 mask
, u16 command
)
284 from
= pmbus_read_byte_data(client
, page
,
285 pmbus_fan_config_registers
[id
]);
289 to
= (from
& ~mask
) | (config
& mask
);
291 rv
= pmbus_write_byte_data(client
, page
,
292 pmbus_fan_config_registers
[id
], to
);
297 return _pmbus_write_word_data(client
, page
,
298 pmbus_fan_command_registers
[id
], command
);
300 EXPORT_SYMBOL_GPL(pmbus_update_fan
);
302 int pmbus_read_word_data(struct i2c_client
*client
, int page
, u8 reg
)
306 rv
= pmbus_set_page(client
, page
);
310 return i2c_smbus_read_word_data(client
, reg
);
312 EXPORT_SYMBOL_GPL(pmbus_read_word_data
);
314 static int pmbus_read_virt_reg(struct i2c_client
*client
, int page
, int reg
)
320 case PMBUS_VIRT_FAN_TARGET_1
... PMBUS_VIRT_FAN_TARGET_4
:
321 id
= reg
- PMBUS_VIRT_FAN_TARGET_1
;
322 rv
= pmbus_get_fan_rate_device(client
, page
, id
, rpm
);
333 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
334 * a device specific mapping function exists and calls it if necessary.
336 static int _pmbus_read_word_data(struct i2c_client
*client
, int page
, int reg
)
338 struct pmbus_data
*data
= i2c_get_clientdata(client
);
339 const struct pmbus_driver_info
*info
= data
->info
;
342 if (info
->read_word_data
) {
343 status
= info
->read_word_data(client
, page
, reg
);
344 if (status
!= -ENODATA
)
348 if (reg
>= PMBUS_VIRT_BASE
)
349 return pmbus_read_virt_reg(client
, page
, reg
);
351 return pmbus_read_word_data(client
, page
, reg
);
354 int pmbus_read_byte_data(struct i2c_client
*client
, int page
, u8 reg
)
358 rv
= pmbus_set_page(client
, page
);
362 return i2c_smbus_read_byte_data(client
, reg
);
364 EXPORT_SYMBOL_GPL(pmbus_read_byte_data
);
366 int pmbus_write_byte_data(struct i2c_client
*client
, int page
, u8 reg
, u8 value
)
370 rv
= pmbus_set_page(client
, page
);
374 return i2c_smbus_write_byte_data(client
, reg
, value
);
376 EXPORT_SYMBOL_GPL(pmbus_write_byte_data
);
378 int pmbus_update_byte_data(struct i2c_client
*client
, int page
, u8 reg
,
384 rv
= pmbus_read_byte_data(client
, page
, reg
);
388 tmp
= (rv
& ~mask
) | (value
& mask
);
391 rv
= pmbus_write_byte_data(client
, page
, reg
, tmp
);
395 EXPORT_SYMBOL_GPL(pmbus_update_byte_data
);
398 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
399 * a device specific mapping function exists and calls it if necessary.
401 static int _pmbus_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
403 struct pmbus_data
*data
= i2c_get_clientdata(client
);
404 const struct pmbus_driver_info
*info
= data
->info
;
407 if (info
->read_byte_data
) {
408 status
= info
->read_byte_data(client
, page
, reg
);
409 if (status
!= -ENODATA
)
412 return pmbus_read_byte_data(client
, page
, reg
);
415 static struct pmbus_sensor
*pmbus_find_sensor(struct pmbus_data
*data
, int page
,
418 struct pmbus_sensor
*sensor
;
420 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
) {
421 if (sensor
->page
== page
&& sensor
->reg
== reg
)
425 return ERR_PTR(-EINVAL
);
428 static int pmbus_get_fan_rate(struct i2c_client
*client
, int page
, int id
,
429 enum pmbus_fan_mode mode
,
432 struct pmbus_data
*data
= i2c_get_clientdata(client
);
433 bool want_rpm
, have_rpm
;
434 struct pmbus_sensor
*s
;
438 want_rpm
= (mode
== rpm
);
441 reg
= want_rpm
? PMBUS_VIRT_FAN_TARGET_1
: PMBUS_VIRT_PWM_1
;
442 s
= pmbus_find_sensor(data
, page
, reg
+ id
);
449 config
= pmbus_read_byte_data(client
, page
,
450 pmbus_fan_config_registers
[id
]);
454 have_rpm
= !!(config
& pmbus_fan_rpm_mask
[id
]);
455 if (want_rpm
== have_rpm
)
456 return pmbus_read_word_data(client
, page
,
457 pmbus_fan_command_registers
[id
]);
459 /* Can't sensibly map between RPM and PWM, just return zero */
463 int pmbus_get_fan_rate_device(struct i2c_client
*client
, int page
, int id
,
464 enum pmbus_fan_mode mode
)
466 return pmbus_get_fan_rate(client
, page
, id
, mode
, false);
468 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_device
);
470 int pmbus_get_fan_rate_cached(struct i2c_client
*client
, int page
, int id
,
471 enum pmbus_fan_mode mode
)
473 return pmbus_get_fan_rate(client
, page
, id
, mode
, true);
475 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_cached
);
477 static void pmbus_clear_fault_page(struct i2c_client
*client
, int page
)
479 _pmbus_write_byte(client
, page
, PMBUS_CLEAR_FAULTS
);
482 void pmbus_clear_faults(struct i2c_client
*client
)
484 struct pmbus_data
*data
= i2c_get_clientdata(client
);
487 for (i
= 0; i
< data
->info
->pages
; i
++)
488 pmbus_clear_fault_page(client
, i
);
490 EXPORT_SYMBOL_GPL(pmbus_clear_faults
);
492 static int pmbus_check_status_cml(struct i2c_client
*client
)
494 struct pmbus_data
*data
= i2c_get_clientdata(client
);
497 status
= data
->read_status(client
, -1);
498 if (status
< 0 || (status
& PB_STATUS_CML
)) {
499 status2
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
500 if (status2
< 0 || (status2
& PB_CML_FAULT_INVALID_COMMAND
))
506 static bool pmbus_check_register(struct i2c_client
*client
,
507 int (*func
)(struct i2c_client
*client
,
512 struct pmbus_data
*data
= i2c_get_clientdata(client
);
514 rv
= func(client
, page
, reg
);
515 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
516 rv
= pmbus_check_status_cml(client
);
517 pmbus_clear_fault_page(client
, -1);
521 static bool pmbus_check_status_register(struct i2c_client
*client
, int page
)
524 struct pmbus_data
*data
= i2c_get_clientdata(client
);
526 status
= data
->read_status(client
, page
);
527 if (status
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
) &&
528 (status
& PB_STATUS_CML
)) {
529 status
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
530 if (status
< 0 || (status
& PB_CML_FAULT_INVALID_COMMAND
))
534 pmbus_clear_fault_page(client
, -1);
538 bool pmbus_check_byte_register(struct i2c_client
*client
, int page
, int reg
)
540 return pmbus_check_register(client
, _pmbus_read_byte_data
, page
, reg
);
542 EXPORT_SYMBOL_GPL(pmbus_check_byte_register
);
544 bool pmbus_check_word_register(struct i2c_client
*client
, int page
, int reg
)
546 return pmbus_check_register(client
, _pmbus_read_word_data
, page
, reg
);
548 EXPORT_SYMBOL_GPL(pmbus_check_word_register
);
550 const struct pmbus_driver_info
*pmbus_get_driver_info(struct i2c_client
*client
)
552 struct pmbus_data
*data
= i2c_get_clientdata(client
);
556 EXPORT_SYMBOL_GPL(pmbus_get_driver_info
);
558 static struct _pmbus_status
{
563 { PMBUS_HAVE_STATUS_VOUT
, PB_STATUS_VOUT_BASE
, PMBUS_STATUS_VOUT
},
564 { PMBUS_HAVE_STATUS_IOUT
, PB_STATUS_IOUT_BASE
, PMBUS_STATUS_IOUT
},
565 { PMBUS_HAVE_STATUS_TEMP
, PB_STATUS_TEMP_BASE
,
566 PMBUS_STATUS_TEMPERATURE
},
567 { PMBUS_HAVE_STATUS_FAN12
, PB_STATUS_FAN_BASE
, PMBUS_STATUS_FAN_12
},
568 { PMBUS_HAVE_STATUS_FAN34
, PB_STATUS_FAN34_BASE
, PMBUS_STATUS_FAN_34
},
571 static struct pmbus_data
*pmbus_update_device(struct device
*dev
)
573 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
574 struct pmbus_data
*data
= i2c_get_clientdata(client
);
575 const struct pmbus_driver_info
*info
= data
->info
;
576 struct pmbus_sensor
*sensor
;
578 mutex_lock(&data
->update_lock
);
579 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
582 for (i
= 0; i
< info
->pages
; i
++) {
583 data
->status
[PB_STATUS_BASE
+ i
]
584 = data
->read_status(client
, i
);
585 for (j
= 0; j
< ARRAY_SIZE(pmbus_status
); j
++) {
586 struct _pmbus_status
*s
= &pmbus_status
[j
];
588 if (!(info
->func
[i
] & s
->func
))
590 data
->status
[s
->base
+ i
]
591 = _pmbus_read_byte_data(client
, i
,
596 if (info
->func
[0] & PMBUS_HAVE_STATUS_INPUT
)
597 data
->status
[PB_STATUS_INPUT_BASE
]
598 = _pmbus_read_byte_data(client
, 0,
601 if (info
->func
[0] & PMBUS_HAVE_STATUS_VMON
)
602 data
->status
[PB_STATUS_VMON_BASE
]
603 = _pmbus_read_byte_data(client
, 0,
604 PMBUS_VIRT_STATUS_VMON
);
606 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
) {
607 if (!data
->valid
|| sensor
->update
)
609 = _pmbus_read_word_data(client
,
613 pmbus_clear_faults(client
);
614 data
->last_updated
= jiffies
;
617 mutex_unlock(&data
->update_lock
);
622 * Convert linear sensor values to milli- or micro-units
623 * depending on sensor type.
625 static long pmbus_reg2data_linear(struct pmbus_data
*data
,
626 struct pmbus_sensor
*sensor
)
632 if (sensor
->class == PSC_VOLTAGE_OUT
) { /* LINEAR16 */
633 exponent
= data
->exponent
[sensor
->page
];
634 mantissa
= (u16
) sensor
->data
;
635 } else { /* LINEAR11 */
636 exponent
= ((s16
)sensor
->data
) >> 11;
637 mantissa
= ((s16
)((sensor
->data
& 0x7ff) << 5)) >> 5;
642 /* scale result to milli-units for all sensors except fans */
643 if (sensor
->class != PSC_FAN
)
646 /* scale result to micro-units for power sensors */
647 if (sensor
->class == PSC_POWER
)
659 * Convert direct sensor values to milli- or micro-units
660 * depending on sensor type.
662 static long pmbus_reg2data_direct(struct pmbus_data
*data
,
663 struct pmbus_sensor
*sensor
)
665 s64 b
, val
= (s16
)sensor
->data
;
668 m
= data
->info
->m
[sensor
->class];
669 b
= data
->info
->b
[sensor
->class];
670 R
= data
->info
->R
[sensor
->class];
675 /* X = 1/m * (Y * 10^-R - b) */
677 /* scale result to milli-units for everything but fans */
678 if (!(sensor
->class == PSC_FAN
|| sensor
->class == PSC_PWM
)) {
683 /* scale result to micro-units for power sensors */
684 if (sensor
->class == PSC_POWER
) {
694 val
= div_s64(val
+ 5LL, 10L); /* round closest */
698 val
= div_s64(val
- b
, m
);
699 return clamp_val(val
, LONG_MIN
, LONG_MAX
);
703 * Convert VID sensor values to milli- or micro-units
704 * depending on sensor type.
706 static long pmbus_reg2data_vid(struct pmbus_data
*data
,
707 struct pmbus_sensor
*sensor
)
709 long val
= sensor
->data
;
712 switch (data
->info
->vrm_version
) {
714 if (val
>= 0x02 && val
<= 0xb2)
715 rv
= DIV_ROUND_CLOSEST(160000 - (val
- 2) * 625, 100);
719 rv
= 250 + (val
- 1) * 5;
723 rv
= 500 + (val
- 1) * 10;
729 static long pmbus_reg2data(struct pmbus_data
*data
, struct pmbus_sensor
*sensor
)
733 if (!sensor
->convert
)
736 switch (data
->info
->format
[sensor
->class]) {
738 val
= pmbus_reg2data_direct(data
, sensor
);
741 val
= pmbus_reg2data_vid(data
, sensor
);
745 val
= pmbus_reg2data_linear(data
, sensor
);
751 #define MAX_MANTISSA (1023 * 1000)
752 #define MIN_MANTISSA (511 * 1000)
754 static u16
pmbus_data2reg_linear(struct pmbus_data
*data
,
755 struct pmbus_sensor
*sensor
, long val
)
757 s16 exponent
= 0, mantissa
;
758 bool negative
= false;
764 if (sensor
->class == PSC_VOLTAGE_OUT
) {
765 /* LINEAR16 does not support negative voltages */
770 * For a static exponents, we don't have a choice
771 * but to adjust the value to it.
773 if (data
->exponent
[sensor
->page
] < 0)
774 val
<<= -data
->exponent
[sensor
->page
];
776 val
>>= data
->exponent
[sensor
->page
];
777 val
= DIV_ROUND_CLOSEST(val
, 1000);
786 /* Power is in uW. Convert to mW before converting. */
787 if (sensor
->class == PSC_POWER
)
788 val
= DIV_ROUND_CLOSEST(val
, 1000L);
791 * For simplicity, convert fan data to milli-units
792 * before calculating the exponent.
794 if (sensor
->class == PSC_FAN
)
797 /* Reduce large mantissa until it fits into 10 bit */
798 while (val
>= MAX_MANTISSA
&& exponent
< 15) {
802 /* Increase small mantissa to improve precision */
803 while (val
< MIN_MANTISSA
&& exponent
> -15) {
808 /* Convert mantissa from milli-units to units */
809 mantissa
= DIV_ROUND_CLOSEST(val
, 1000);
811 /* Ensure that resulting number is within range */
812 if (mantissa
> 0x3ff)
817 mantissa
= -mantissa
;
819 /* Convert to 5 bit exponent, 11 bit mantissa */
820 return (mantissa
& 0x7ff) | ((exponent
<< 11) & 0xf800);
823 static u16
pmbus_data2reg_direct(struct pmbus_data
*data
,
824 struct pmbus_sensor
*sensor
, long val
)
829 m
= data
->info
->m
[sensor
->class];
830 b
= data
->info
->b
[sensor
->class];
831 R
= data
->info
->R
[sensor
->class];
833 /* Power is in uW. Adjust R and b. */
834 if (sensor
->class == PSC_POWER
) {
839 /* Calculate Y = (m * X + b) * 10^R */
840 if (!(sensor
->class == PSC_FAN
|| sensor
->class == PSC_PWM
)) {
841 R
-= 3; /* Adjust R and b for data in milli-units */
844 val64
= val64
* m
+ b
;
851 val64
= div_s64(val64
+ 5LL, 10L); /* round closest */
855 return (u16
)clamp_val(val64
, S16_MIN
, S16_MAX
);
858 static u16
pmbus_data2reg_vid(struct pmbus_data
*data
,
859 struct pmbus_sensor
*sensor
, long val
)
861 val
= clamp_val(val
, 500, 1600);
863 return 2 + DIV_ROUND_CLOSEST((1600 - val
) * 100, 625);
866 static u16
pmbus_data2reg(struct pmbus_data
*data
,
867 struct pmbus_sensor
*sensor
, long val
)
871 if (!sensor
->convert
)
874 switch (data
->info
->format
[sensor
->class]) {
876 regval
= pmbus_data2reg_direct(data
, sensor
, val
);
879 regval
= pmbus_data2reg_vid(data
, sensor
, val
);
883 regval
= pmbus_data2reg_linear(data
, sensor
, val
);
890 * Return boolean calculated from converted data.
891 * <index> defines a status register index and mask.
892 * The mask is in the lower 8 bits, the register index is in bits 8..23.
894 * The associated pmbus_boolean structure contains optional pointers to two
895 * sensor attributes. If specified, those attributes are compared against each
896 * other to determine if a limit has been exceeded.
898 * If the sensor attribute pointers are NULL, the function returns true if
899 * (status[reg] & mask) is true.
901 * If sensor attribute pointers are provided, a comparison against a specified
902 * limit has to be performed to determine the boolean result.
903 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
904 * sensor values referenced by sensor attribute pointers s1 and s2).
906 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
907 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
909 * If a negative value is stored in any of the referenced registers, this value
910 * reflects an error code which will be returned.
912 static int pmbus_get_boolean(struct pmbus_data
*data
, struct pmbus_boolean
*b
,
915 struct pmbus_sensor
*s1
= b
->s1
;
916 struct pmbus_sensor
*s2
= b
->s2
;
917 u16 reg
= (index
>> 16) & 0xffff;
918 u16 mask
= index
& 0xffff;
922 status
= data
->status
[reg
];
926 regval
= status
& mask
;
929 } else if (!s1
|| !s2
) {
930 WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b
, s1
, s2
);
940 v1
= pmbus_reg2data(data
, s1
);
941 v2
= pmbus_reg2data(data
, s2
);
942 ret
= !!(regval
&& v1
>= v2
);
947 static ssize_t
pmbus_show_boolean(struct device
*dev
,
948 struct device_attribute
*da
, char *buf
)
950 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
951 struct pmbus_boolean
*boolean
= to_pmbus_boolean(attr
);
952 struct pmbus_data
*data
= pmbus_update_device(dev
);
955 val
= pmbus_get_boolean(data
, boolean
, attr
->index
);
958 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
961 static ssize_t
pmbus_show_sensor(struct device
*dev
,
962 struct device_attribute
*devattr
, char *buf
)
964 struct pmbus_data
*data
= pmbus_update_device(dev
);
965 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
967 if (sensor
->data
< 0)
970 return snprintf(buf
, PAGE_SIZE
, "%ld\n", pmbus_reg2data(data
, sensor
));
973 static ssize_t
pmbus_set_sensor(struct device
*dev
,
974 struct device_attribute
*devattr
,
975 const char *buf
, size_t count
)
977 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
978 struct pmbus_data
*data
= i2c_get_clientdata(client
);
979 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
985 if (kstrtol(buf
, 10, &val
) < 0)
988 mutex_lock(&data
->update_lock
);
989 regval
= pmbus_data2reg(data
, sensor
, val
);
990 ret
= _pmbus_write_word_data(client
, sensor
->page
, sensor
->reg
, regval
);
994 sensor
->data
= regval
;
995 mutex_unlock(&data
->update_lock
);
999 static ssize_t
pmbus_show_label(struct device
*dev
,
1000 struct device_attribute
*da
, char *buf
)
1002 struct pmbus_label
*label
= to_pmbus_label(da
);
1004 return snprintf(buf
, PAGE_SIZE
, "%s\n", label
->label
);
1007 static int pmbus_add_attribute(struct pmbus_data
*data
, struct attribute
*attr
)
1009 if (data
->num_attributes
>= data
->max_attributes
- 1) {
1010 int new_max_attrs
= data
->max_attributes
+ PMBUS_ATTR_ALLOC_SIZE
;
1011 void *new_attrs
= krealloc(data
->group
.attrs
,
1012 new_max_attrs
* sizeof(void *),
1016 data
->group
.attrs
= new_attrs
;
1017 data
->max_attributes
= new_max_attrs
;
1020 data
->group
.attrs
[data
->num_attributes
++] = attr
;
1021 data
->group
.attrs
[data
->num_attributes
] = NULL
;
1025 static void pmbus_dev_attr_init(struct device_attribute
*dev_attr
,
1028 ssize_t (*show
)(struct device
*dev
,
1029 struct device_attribute
*attr
,
1031 ssize_t (*store
)(struct device
*dev
,
1032 struct device_attribute
*attr
,
1033 const char *buf
, size_t count
))
1035 sysfs_attr_init(&dev_attr
->attr
);
1036 dev_attr
->attr
.name
= name
;
1037 dev_attr
->attr
.mode
= mode
;
1038 dev_attr
->show
= show
;
1039 dev_attr
->store
= store
;
1042 static void pmbus_attr_init(struct sensor_device_attribute
*a
,
1045 ssize_t (*show
)(struct device
*dev
,
1046 struct device_attribute
*attr
,
1048 ssize_t (*store
)(struct device
*dev
,
1049 struct device_attribute
*attr
,
1050 const char *buf
, size_t count
),
1053 pmbus_dev_attr_init(&a
->dev_attr
, name
, mode
, show
, store
);
1057 static int pmbus_add_boolean(struct pmbus_data
*data
,
1058 const char *name
, const char *type
, int seq
,
1059 struct pmbus_sensor
*s1
,
1060 struct pmbus_sensor
*s2
,
1063 struct pmbus_boolean
*boolean
;
1064 struct sensor_device_attribute
*a
;
1066 boolean
= devm_kzalloc(data
->dev
, sizeof(*boolean
), GFP_KERNEL
);
1070 a
= &boolean
->attribute
;
1072 snprintf(boolean
->name
, sizeof(boolean
->name
), "%s%d_%s",
1076 pmbus_attr_init(a
, boolean
->name
, S_IRUGO
, pmbus_show_boolean
, NULL
,
1077 (reg
<< 16) | mask
);
1079 return pmbus_add_attribute(data
, &a
->dev_attr
.attr
);
1082 static struct pmbus_sensor
*pmbus_add_sensor(struct pmbus_data
*data
,
1083 const char *name
, const char *type
,
1084 int seq
, int page
, int reg
,
1085 enum pmbus_sensor_classes
class,
1086 bool update
, bool readonly
,
1089 struct pmbus_sensor
*sensor
;
1090 struct device_attribute
*a
;
1092 sensor
= devm_kzalloc(data
->dev
, sizeof(*sensor
), GFP_KERNEL
);
1095 a
= &sensor
->attribute
;
1098 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d_%s",
1101 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d",
1104 sensor
->page
= page
;
1106 sensor
->class = class;
1107 sensor
->update
= update
;
1108 sensor
->convert
= convert
;
1109 pmbus_dev_attr_init(a
, sensor
->name
,
1110 readonly
? S_IRUGO
: S_IRUGO
| S_IWUSR
,
1111 pmbus_show_sensor
, pmbus_set_sensor
);
1113 if (pmbus_add_attribute(data
, &a
->attr
))
1116 sensor
->next
= data
->sensors
;
1117 data
->sensors
= sensor
;
1122 static int pmbus_add_label(struct pmbus_data
*data
,
1123 const char *name
, int seq
,
1124 const char *lstring
, int index
)
1126 struct pmbus_label
*label
;
1127 struct device_attribute
*a
;
1129 label
= devm_kzalloc(data
->dev
, sizeof(*label
), GFP_KERNEL
);
1133 a
= &label
->attribute
;
1135 snprintf(label
->name
, sizeof(label
->name
), "%s%d_label", name
, seq
);
1137 strncpy(label
->label
, lstring
, sizeof(label
->label
) - 1);
1139 snprintf(label
->label
, sizeof(label
->label
), "%s%d", lstring
,
1142 pmbus_dev_attr_init(a
, label
->name
, S_IRUGO
, pmbus_show_label
, NULL
);
1143 return pmbus_add_attribute(data
, &a
->attr
);
1147 * Search for attributes. Allocate sensors, booleans, and labels as needed.
1151 * The pmbus_limit_attr structure describes a single limit attribute
1152 * and its associated alarm attribute.
1154 struct pmbus_limit_attr
{
1155 u16 reg
; /* Limit register */
1156 u16 sbit
; /* Alarm attribute status bit */
1157 bool update
; /* True if register needs updates */
1158 bool low
; /* True if low limit; for limits with compare
1160 const char *attr
; /* Attribute name */
1161 const char *alarm
; /* Alarm attribute name */
1165 * The pmbus_sensor_attr structure describes one sensor attribute. This
1166 * description includes a reference to the associated limit attributes.
1168 struct pmbus_sensor_attr
{
1169 u16 reg
; /* sensor register */
1170 u16 gbit
; /* generic status bit */
1171 u8 nlimit
; /* # of limit registers */
1172 enum pmbus_sensor_classes
class;/* sensor class */
1173 const char *label
; /* sensor label */
1174 bool paged
; /* true if paged sensor */
1175 bool update
; /* true if update needed */
1176 bool compare
; /* true if compare function needed */
1177 u32 func
; /* sensor mask */
1178 u32 sfunc
; /* sensor status mask */
1179 int sbase
; /* status base register */
1180 const struct pmbus_limit_attr
*limit
;/* limit registers */
1184 * Add a set of limit attributes and, if supported, the associated
1186 * returns 0 if no alarm register found, 1 if an alarm register was found,
1189 static int pmbus_add_limit_attrs(struct i2c_client
*client
,
1190 struct pmbus_data
*data
,
1191 const struct pmbus_driver_info
*info
,
1192 const char *name
, int index
, int page
,
1193 struct pmbus_sensor
*base
,
1194 const struct pmbus_sensor_attr
*attr
)
1196 const struct pmbus_limit_attr
*l
= attr
->limit
;
1197 int nlimit
= attr
->nlimit
;
1200 struct pmbus_sensor
*curr
;
1202 for (i
= 0; i
< nlimit
; i
++) {
1203 if (pmbus_check_word_register(client
, page
, l
->reg
)) {
1204 curr
= pmbus_add_sensor(data
, name
, l
->attr
, index
,
1205 page
, l
->reg
, attr
->class,
1206 attr
->update
|| l
->update
,
1210 if (l
->sbit
&& (info
->func
[page
] & attr
->sfunc
)) {
1211 ret
= pmbus_add_boolean(data
, name
,
1213 attr
->compare
? l
->low
? curr
: base
1215 attr
->compare
? l
->low
? base
: curr
1217 attr
->sbase
+ page
, l
->sbit
);
1228 static int pmbus_add_sensor_attrs_one(struct i2c_client
*client
,
1229 struct pmbus_data
*data
,
1230 const struct pmbus_driver_info
*info
,
1232 int index
, int page
,
1233 const struct pmbus_sensor_attr
*attr
)
1235 struct pmbus_sensor
*base
;
1236 bool upper
= !!(attr
->gbit
& 0xff00); /* need to check STATUS_WORD */
1240 ret
= pmbus_add_label(data
, name
, index
, attr
->label
,
1241 attr
->paged
? page
+ 1 : 0);
1245 base
= pmbus_add_sensor(data
, name
, "input", index
, page
, attr
->reg
,
1246 attr
->class, true, true, true);
1250 ret
= pmbus_add_limit_attrs(client
, data
, info
, name
,
1251 index
, page
, base
, attr
);
1255 * Add generic alarm attribute only if there are no individual
1256 * alarm attributes, if there is a global alarm bit, and if
1257 * the generic status register (word or byte, depending on
1258 * which global bit is set) for this page is accessible.
1260 if (!ret
&& attr
->gbit
&&
1261 (!upper
|| (upper
&& data
->has_status_word
)) &&
1262 pmbus_check_status_register(client
, page
)) {
1263 ret
= pmbus_add_boolean(data
, name
, "alarm", index
,
1265 PB_STATUS_BASE
+ page
,
1274 static int pmbus_add_sensor_attrs(struct i2c_client
*client
,
1275 struct pmbus_data
*data
,
1277 const struct pmbus_sensor_attr
*attrs
,
1280 const struct pmbus_driver_info
*info
= data
->info
;
1285 for (i
= 0; i
< nattrs
; i
++) {
1288 pages
= attrs
->paged
? info
->pages
: 1;
1289 for (page
= 0; page
< pages
; page
++) {
1290 if (!(info
->func
[page
] & attrs
->func
))
1292 ret
= pmbus_add_sensor_attrs_one(client
, data
, info
,
1304 static const struct pmbus_limit_attr vin_limit_attrs
[] = {
1306 .reg
= PMBUS_VIN_UV_WARN_LIMIT
,
1308 .alarm
= "min_alarm",
1309 .sbit
= PB_VOLTAGE_UV_WARNING
,
1311 .reg
= PMBUS_VIN_UV_FAULT_LIMIT
,
1313 .alarm
= "lcrit_alarm",
1314 .sbit
= PB_VOLTAGE_UV_FAULT
,
1316 .reg
= PMBUS_VIN_OV_WARN_LIMIT
,
1318 .alarm
= "max_alarm",
1319 .sbit
= PB_VOLTAGE_OV_WARNING
,
1321 .reg
= PMBUS_VIN_OV_FAULT_LIMIT
,
1323 .alarm
= "crit_alarm",
1324 .sbit
= PB_VOLTAGE_OV_FAULT
,
1326 .reg
= PMBUS_VIRT_READ_VIN_AVG
,
1330 .reg
= PMBUS_VIRT_READ_VIN_MIN
,
1334 .reg
= PMBUS_VIRT_READ_VIN_MAX
,
1338 .reg
= PMBUS_VIRT_RESET_VIN_HISTORY
,
1339 .attr
= "reset_history",
1343 static const struct pmbus_limit_attr vmon_limit_attrs
[] = {
1345 .reg
= PMBUS_VIRT_VMON_UV_WARN_LIMIT
,
1347 .alarm
= "min_alarm",
1348 .sbit
= PB_VOLTAGE_UV_WARNING
,
1350 .reg
= PMBUS_VIRT_VMON_UV_FAULT_LIMIT
,
1352 .alarm
= "lcrit_alarm",
1353 .sbit
= PB_VOLTAGE_UV_FAULT
,
1355 .reg
= PMBUS_VIRT_VMON_OV_WARN_LIMIT
,
1357 .alarm
= "max_alarm",
1358 .sbit
= PB_VOLTAGE_OV_WARNING
,
1360 .reg
= PMBUS_VIRT_VMON_OV_FAULT_LIMIT
,
1362 .alarm
= "crit_alarm",
1363 .sbit
= PB_VOLTAGE_OV_FAULT
,
1367 static const struct pmbus_limit_attr vout_limit_attrs
[] = {
1369 .reg
= PMBUS_VOUT_UV_WARN_LIMIT
,
1371 .alarm
= "min_alarm",
1372 .sbit
= PB_VOLTAGE_UV_WARNING
,
1374 .reg
= PMBUS_VOUT_UV_FAULT_LIMIT
,
1376 .alarm
= "lcrit_alarm",
1377 .sbit
= PB_VOLTAGE_UV_FAULT
,
1379 .reg
= PMBUS_VOUT_OV_WARN_LIMIT
,
1381 .alarm
= "max_alarm",
1382 .sbit
= PB_VOLTAGE_OV_WARNING
,
1384 .reg
= PMBUS_VOUT_OV_FAULT_LIMIT
,
1386 .alarm
= "crit_alarm",
1387 .sbit
= PB_VOLTAGE_OV_FAULT
,
1389 .reg
= PMBUS_VIRT_READ_VOUT_AVG
,
1393 .reg
= PMBUS_VIRT_READ_VOUT_MIN
,
1397 .reg
= PMBUS_VIRT_READ_VOUT_MAX
,
1401 .reg
= PMBUS_VIRT_RESET_VOUT_HISTORY
,
1402 .attr
= "reset_history",
1406 static const struct pmbus_sensor_attr voltage_attributes
[] = {
1408 .reg
= PMBUS_READ_VIN
,
1409 .class = PSC_VOLTAGE_IN
,
1411 .func
= PMBUS_HAVE_VIN
,
1412 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1413 .sbase
= PB_STATUS_INPUT_BASE
,
1414 .gbit
= PB_STATUS_VIN_UV
,
1415 .limit
= vin_limit_attrs
,
1416 .nlimit
= ARRAY_SIZE(vin_limit_attrs
),
1418 .reg
= PMBUS_VIRT_READ_VMON
,
1419 .class = PSC_VOLTAGE_IN
,
1421 .func
= PMBUS_HAVE_VMON
,
1422 .sfunc
= PMBUS_HAVE_STATUS_VMON
,
1423 .sbase
= PB_STATUS_VMON_BASE
,
1424 .limit
= vmon_limit_attrs
,
1425 .nlimit
= ARRAY_SIZE(vmon_limit_attrs
),
1427 .reg
= PMBUS_READ_VCAP
,
1428 .class = PSC_VOLTAGE_IN
,
1430 .func
= PMBUS_HAVE_VCAP
,
1432 .reg
= PMBUS_READ_VOUT
,
1433 .class = PSC_VOLTAGE_OUT
,
1436 .func
= PMBUS_HAVE_VOUT
,
1437 .sfunc
= PMBUS_HAVE_STATUS_VOUT
,
1438 .sbase
= PB_STATUS_VOUT_BASE
,
1439 .gbit
= PB_STATUS_VOUT_OV
,
1440 .limit
= vout_limit_attrs
,
1441 .nlimit
= ARRAY_SIZE(vout_limit_attrs
),
1445 /* Current attributes */
1447 static const struct pmbus_limit_attr iin_limit_attrs
[] = {
1449 .reg
= PMBUS_IIN_OC_WARN_LIMIT
,
1451 .alarm
= "max_alarm",
1452 .sbit
= PB_IIN_OC_WARNING
,
1454 .reg
= PMBUS_IIN_OC_FAULT_LIMIT
,
1456 .alarm
= "crit_alarm",
1457 .sbit
= PB_IIN_OC_FAULT
,
1459 .reg
= PMBUS_VIRT_READ_IIN_AVG
,
1463 .reg
= PMBUS_VIRT_READ_IIN_MIN
,
1467 .reg
= PMBUS_VIRT_READ_IIN_MAX
,
1471 .reg
= PMBUS_VIRT_RESET_IIN_HISTORY
,
1472 .attr
= "reset_history",
1476 static const struct pmbus_limit_attr iout_limit_attrs
[] = {
1478 .reg
= PMBUS_IOUT_OC_WARN_LIMIT
,
1480 .alarm
= "max_alarm",
1481 .sbit
= PB_IOUT_OC_WARNING
,
1483 .reg
= PMBUS_IOUT_UC_FAULT_LIMIT
,
1485 .alarm
= "lcrit_alarm",
1486 .sbit
= PB_IOUT_UC_FAULT
,
1488 .reg
= PMBUS_IOUT_OC_FAULT_LIMIT
,
1490 .alarm
= "crit_alarm",
1491 .sbit
= PB_IOUT_OC_FAULT
,
1493 .reg
= PMBUS_VIRT_READ_IOUT_AVG
,
1497 .reg
= PMBUS_VIRT_READ_IOUT_MIN
,
1501 .reg
= PMBUS_VIRT_READ_IOUT_MAX
,
1505 .reg
= PMBUS_VIRT_RESET_IOUT_HISTORY
,
1506 .attr
= "reset_history",
1510 static const struct pmbus_sensor_attr current_attributes
[] = {
1512 .reg
= PMBUS_READ_IIN
,
1513 .class = PSC_CURRENT_IN
,
1515 .func
= PMBUS_HAVE_IIN
,
1516 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1517 .sbase
= PB_STATUS_INPUT_BASE
,
1518 .gbit
= PB_STATUS_INPUT
,
1519 .limit
= iin_limit_attrs
,
1520 .nlimit
= ARRAY_SIZE(iin_limit_attrs
),
1522 .reg
= PMBUS_READ_IOUT
,
1523 .class = PSC_CURRENT_OUT
,
1526 .func
= PMBUS_HAVE_IOUT
,
1527 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1528 .sbase
= PB_STATUS_IOUT_BASE
,
1529 .gbit
= PB_STATUS_IOUT_OC
,
1530 .limit
= iout_limit_attrs
,
1531 .nlimit
= ARRAY_SIZE(iout_limit_attrs
),
1535 /* Power attributes */
1537 static const struct pmbus_limit_attr pin_limit_attrs
[] = {
1539 .reg
= PMBUS_PIN_OP_WARN_LIMIT
,
1542 .sbit
= PB_PIN_OP_WARNING
,
1544 .reg
= PMBUS_VIRT_READ_PIN_AVG
,
1548 .reg
= PMBUS_VIRT_READ_PIN_MIN
,
1550 .attr
= "input_lowest",
1552 .reg
= PMBUS_VIRT_READ_PIN_MAX
,
1554 .attr
= "input_highest",
1556 .reg
= PMBUS_VIRT_RESET_PIN_HISTORY
,
1557 .attr
= "reset_history",
1561 static const struct pmbus_limit_attr pout_limit_attrs
[] = {
1563 .reg
= PMBUS_POUT_MAX
,
1565 .alarm
= "cap_alarm",
1566 .sbit
= PB_POWER_LIMITING
,
1568 .reg
= PMBUS_POUT_OP_WARN_LIMIT
,
1570 .alarm
= "max_alarm",
1571 .sbit
= PB_POUT_OP_WARNING
,
1573 .reg
= PMBUS_POUT_OP_FAULT_LIMIT
,
1575 .alarm
= "crit_alarm",
1576 .sbit
= PB_POUT_OP_FAULT
,
1578 .reg
= PMBUS_VIRT_READ_POUT_AVG
,
1582 .reg
= PMBUS_VIRT_READ_POUT_MIN
,
1584 .attr
= "input_lowest",
1586 .reg
= PMBUS_VIRT_READ_POUT_MAX
,
1588 .attr
= "input_highest",
1590 .reg
= PMBUS_VIRT_RESET_POUT_HISTORY
,
1591 .attr
= "reset_history",
1595 static const struct pmbus_sensor_attr power_attributes
[] = {
1597 .reg
= PMBUS_READ_PIN
,
1600 .func
= PMBUS_HAVE_PIN
,
1601 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1602 .sbase
= PB_STATUS_INPUT_BASE
,
1603 .gbit
= PB_STATUS_INPUT
,
1604 .limit
= pin_limit_attrs
,
1605 .nlimit
= ARRAY_SIZE(pin_limit_attrs
),
1607 .reg
= PMBUS_READ_POUT
,
1611 .func
= PMBUS_HAVE_POUT
,
1612 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1613 .sbase
= PB_STATUS_IOUT_BASE
,
1614 .limit
= pout_limit_attrs
,
1615 .nlimit
= ARRAY_SIZE(pout_limit_attrs
),
1619 /* Temperature atributes */
1621 static const struct pmbus_limit_attr temp_limit_attrs
[] = {
1623 .reg
= PMBUS_UT_WARN_LIMIT
,
1626 .alarm
= "min_alarm",
1627 .sbit
= PB_TEMP_UT_WARNING
,
1629 .reg
= PMBUS_UT_FAULT_LIMIT
,
1632 .alarm
= "lcrit_alarm",
1633 .sbit
= PB_TEMP_UT_FAULT
,
1635 .reg
= PMBUS_OT_WARN_LIMIT
,
1637 .alarm
= "max_alarm",
1638 .sbit
= PB_TEMP_OT_WARNING
,
1640 .reg
= PMBUS_OT_FAULT_LIMIT
,
1642 .alarm
= "crit_alarm",
1643 .sbit
= PB_TEMP_OT_FAULT
,
1645 .reg
= PMBUS_VIRT_READ_TEMP_MIN
,
1648 .reg
= PMBUS_VIRT_READ_TEMP_AVG
,
1651 .reg
= PMBUS_VIRT_READ_TEMP_MAX
,
1654 .reg
= PMBUS_VIRT_RESET_TEMP_HISTORY
,
1655 .attr
= "reset_history",
1659 static const struct pmbus_limit_attr temp_limit_attrs2
[] = {
1661 .reg
= PMBUS_UT_WARN_LIMIT
,
1664 .alarm
= "min_alarm",
1665 .sbit
= PB_TEMP_UT_WARNING
,
1667 .reg
= PMBUS_UT_FAULT_LIMIT
,
1670 .alarm
= "lcrit_alarm",
1671 .sbit
= PB_TEMP_UT_FAULT
,
1673 .reg
= PMBUS_OT_WARN_LIMIT
,
1675 .alarm
= "max_alarm",
1676 .sbit
= PB_TEMP_OT_WARNING
,
1678 .reg
= PMBUS_OT_FAULT_LIMIT
,
1680 .alarm
= "crit_alarm",
1681 .sbit
= PB_TEMP_OT_FAULT
,
1683 .reg
= PMBUS_VIRT_READ_TEMP2_MIN
,
1686 .reg
= PMBUS_VIRT_READ_TEMP2_AVG
,
1689 .reg
= PMBUS_VIRT_READ_TEMP2_MAX
,
1692 .reg
= PMBUS_VIRT_RESET_TEMP2_HISTORY
,
1693 .attr
= "reset_history",
1697 static const struct pmbus_limit_attr temp_limit_attrs3
[] = {
1699 .reg
= PMBUS_UT_WARN_LIMIT
,
1702 .alarm
= "min_alarm",
1703 .sbit
= PB_TEMP_UT_WARNING
,
1705 .reg
= PMBUS_UT_FAULT_LIMIT
,
1708 .alarm
= "lcrit_alarm",
1709 .sbit
= PB_TEMP_UT_FAULT
,
1711 .reg
= PMBUS_OT_WARN_LIMIT
,
1713 .alarm
= "max_alarm",
1714 .sbit
= PB_TEMP_OT_WARNING
,
1716 .reg
= PMBUS_OT_FAULT_LIMIT
,
1718 .alarm
= "crit_alarm",
1719 .sbit
= PB_TEMP_OT_FAULT
,
1723 static const struct pmbus_sensor_attr temp_attributes
[] = {
1725 .reg
= PMBUS_READ_TEMPERATURE_1
,
1726 .class = PSC_TEMPERATURE
,
1730 .func
= PMBUS_HAVE_TEMP
,
1731 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1732 .sbase
= PB_STATUS_TEMP_BASE
,
1733 .gbit
= PB_STATUS_TEMPERATURE
,
1734 .limit
= temp_limit_attrs
,
1735 .nlimit
= ARRAY_SIZE(temp_limit_attrs
),
1737 .reg
= PMBUS_READ_TEMPERATURE_2
,
1738 .class = PSC_TEMPERATURE
,
1742 .func
= PMBUS_HAVE_TEMP2
,
1743 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1744 .sbase
= PB_STATUS_TEMP_BASE
,
1745 .gbit
= PB_STATUS_TEMPERATURE
,
1746 .limit
= temp_limit_attrs2
,
1747 .nlimit
= ARRAY_SIZE(temp_limit_attrs2
),
1749 .reg
= PMBUS_READ_TEMPERATURE_3
,
1750 .class = PSC_TEMPERATURE
,
1754 .func
= PMBUS_HAVE_TEMP3
,
1755 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1756 .sbase
= PB_STATUS_TEMP_BASE
,
1757 .gbit
= PB_STATUS_TEMPERATURE
,
1758 .limit
= temp_limit_attrs3
,
1759 .nlimit
= ARRAY_SIZE(temp_limit_attrs3
),
1763 static const int pmbus_fan_registers
[] = {
1764 PMBUS_READ_FAN_SPEED_1
,
1765 PMBUS_READ_FAN_SPEED_2
,
1766 PMBUS_READ_FAN_SPEED_3
,
1767 PMBUS_READ_FAN_SPEED_4
1770 static const int pmbus_fan_status_registers
[] = {
1771 PMBUS_STATUS_FAN_12
,
1772 PMBUS_STATUS_FAN_12
,
1773 PMBUS_STATUS_FAN_34
,
1777 static const u32 pmbus_fan_flags
[] = {
1784 static const u32 pmbus_fan_status_flags
[] = {
1785 PMBUS_HAVE_STATUS_FAN12
,
1786 PMBUS_HAVE_STATUS_FAN12
,
1787 PMBUS_HAVE_STATUS_FAN34
,
1788 PMBUS_HAVE_STATUS_FAN34
1793 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */
1794 static int pmbus_add_fan_ctrl(struct i2c_client
*client
,
1795 struct pmbus_data
*data
, int index
, int page
, int id
,
1798 struct pmbus_sensor
*sensor
;
1800 sensor
= pmbus_add_sensor(data
, "fan", "target", index
, page
,
1801 PMBUS_VIRT_FAN_TARGET_1
+ id
, PSC_FAN
,
1802 false, false, true);
1807 if (!((data
->info
->func
[page
] & PMBUS_HAVE_PWM12
) ||
1808 (data
->info
->func
[page
] & PMBUS_HAVE_PWM34
)))
1811 sensor
= pmbus_add_sensor(data
, "pwm", NULL
, index
, page
,
1812 PMBUS_VIRT_PWM_1
+ id
, PSC_PWM
,
1813 false, false, true);
1818 sensor
= pmbus_add_sensor(data
, "pwm", "enable", index
, page
,
1819 PMBUS_VIRT_PWM_ENABLE_1
+ id
, PSC_PWM
,
1820 true, false, false);
1828 static int pmbus_add_fan_attributes(struct i2c_client
*client
,
1829 struct pmbus_data
*data
)
1831 const struct pmbus_driver_info
*info
= data
->info
;
1836 for (page
= 0; page
< info
->pages
; page
++) {
1839 for (f
= 0; f
< ARRAY_SIZE(pmbus_fan_registers
); f
++) {
1842 if (!(info
->func
[page
] & pmbus_fan_flags
[f
]))
1845 if (!pmbus_check_word_register(client
, page
,
1846 pmbus_fan_registers
[f
]))
1850 * Skip fan if not installed.
1851 * Each fan configuration register covers multiple fans,
1852 * so we have to do some magic.
1854 regval
= _pmbus_read_byte_data(client
, page
,
1855 pmbus_fan_config_registers
[f
]);
1857 (!(regval
& (PB_FAN_1_INSTALLED
>> ((f
& 1) * 4)))))
1860 if (pmbus_add_sensor(data
, "fan", "input", index
,
1861 page
, pmbus_fan_registers
[f
],
1862 PSC_FAN
, true, true, true) == NULL
)
1866 if (pmbus_check_word_register(client
, page
,
1867 pmbus_fan_command_registers
[f
])) {
1868 ret
= pmbus_add_fan_ctrl(client
, data
, index
,
1875 * Each fan status register covers multiple fans,
1876 * so we have to do some magic.
1878 if ((info
->func
[page
] & pmbus_fan_status_flags
[f
]) &&
1879 pmbus_check_byte_register(client
,
1880 page
, pmbus_fan_status_registers
[f
])) {
1883 if (f
> 1) /* fan 3, 4 */
1884 base
= PB_STATUS_FAN34_BASE
+ page
;
1886 base
= PB_STATUS_FAN_BASE
+ page
;
1887 ret
= pmbus_add_boolean(data
, "fan",
1888 "alarm", index
, NULL
, NULL
, base
,
1889 PB_FAN_FAN1_WARNING
>> (f
& 1));
1892 ret
= pmbus_add_boolean(data
, "fan",
1893 "fault", index
, NULL
, NULL
, base
,
1894 PB_FAN_FAN1_FAULT
>> (f
& 1));
1904 static int pmbus_find_attributes(struct i2c_client
*client
,
1905 struct pmbus_data
*data
)
1909 /* Voltage sensors */
1910 ret
= pmbus_add_sensor_attrs(client
, data
, "in", voltage_attributes
,
1911 ARRAY_SIZE(voltage_attributes
));
1915 /* Current sensors */
1916 ret
= pmbus_add_sensor_attrs(client
, data
, "curr", current_attributes
,
1917 ARRAY_SIZE(current_attributes
));
1922 ret
= pmbus_add_sensor_attrs(client
, data
, "power", power_attributes
,
1923 ARRAY_SIZE(power_attributes
));
1927 /* Temperature sensors */
1928 ret
= pmbus_add_sensor_attrs(client
, data
, "temp", temp_attributes
,
1929 ARRAY_SIZE(temp_attributes
));
1934 ret
= pmbus_add_fan_attributes(client
, data
);
1939 * Identify chip parameters.
1940 * This function is called for all chips.
1942 static int pmbus_identify_common(struct i2c_client
*client
,
1943 struct pmbus_data
*data
, int page
)
1947 if (pmbus_check_byte_register(client
, page
, PMBUS_VOUT_MODE
))
1948 vout_mode
= _pmbus_read_byte_data(client
, page
,
1950 if (vout_mode
>= 0 && vout_mode
!= 0xff) {
1952 * Not all chips support the VOUT_MODE command,
1953 * so a failure to read it is not an error.
1955 switch (vout_mode
>> 5) {
1956 case 0: /* linear mode */
1957 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != linear
)
1960 data
->exponent
[page
] = ((s8
)(vout_mode
<< 3)) >> 3;
1962 case 1: /* VID mode */
1963 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != vid
)
1966 case 2: /* direct mode */
1967 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != direct
)
1975 pmbus_clear_fault_page(client
, page
);
1979 static int pmbus_read_status_byte(struct i2c_client
*client
, int page
)
1981 return _pmbus_read_byte_data(client
, page
, PMBUS_STATUS_BYTE
);
1984 static int pmbus_read_status_word(struct i2c_client
*client
, int page
)
1986 return _pmbus_read_word_data(client
, page
, PMBUS_STATUS_WORD
);
1989 static int pmbus_init_common(struct i2c_client
*client
, struct pmbus_data
*data
,
1990 struct pmbus_driver_info
*info
)
1992 struct device
*dev
= &client
->dev
;
1996 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
1997 * to use PMBUS_STATUS_BYTE instead if that is the case.
1998 * Bail out if both registers are not supported.
2000 data
->read_status
= pmbus_read_status_word
;
2001 ret
= i2c_smbus_read_word_data(client
, PMBUS_STATUS_WORD
);
2002 if (ret
< 0 || ret
== 0xffff) {
2003 data
->read_status
= pmbus_read_status_byte
;
2004 ret
= i2c_smbus_read_byte_data(client
, PMBUS_STATUS_BYTE
);
2005 if (ret
< 0 || ret
== 0xff) {
2006 dev_err(dev
, "PMBus status register not found\n");
2010 data
->has_status_word
= true;
2013 /* Enable PEC if the controller supports it */
2014 ret
= i2c_smbus_read_byte_data(client
, PMBUS_CAPABILITY
);
2015 if (ret
>= 0 && (ret
& PB_CAPABILITY_ERROR_CHECK
))
2016 client
->flags
|= I2C_CLIENT_PEC
;
2018 pmbus_clear_faults(client
);
2020 if (info
->identify
) {
2021 ret
= (*info
->identify
)(client
, info
);
2023 dev_err(dev
, "Chip identification failed\n");
2028 if (info
->pages
<= 0 || info
->pages
> PMBUS_PAGES
) {
2029 dev_err(dev
, "Bad number of PMBus pages: %d\n", info
->pages
);
2033 for (page
= 0; page
< info
->pages
; page
++) {
2034 ret
= pmbus_identify_common(client
, data
, page
);
2036 dev_err(dev
, "Failed to identify chip capabilities\n");
2043 #if IS_ENABLED(CONFIG_REGULATOR)
2044 static int pmbus_regulator_is_enabled(struct regulator_dev
*rdev
)
2046 struct device
*dev
= rdev_get_dev(rdev
);
2047 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2048 u8 page
= rdev_get_id(rdev
);
2051 ret
= pmbus_read_byte_data(client
, page
, PMBUS_OPERATION
);
2055 return !!(ret
& PB_OPERATION_CONTROL_ON
);
2058 static int _pmbus_regulator_on_off(struct regulator_dev
*rdev
, bool enable
)
2060 struct device
*dev
= rdev_get_dev(rdev
);
2061 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
2062 u8 page
= rdev_get_id(rdev
);
2064 return pmbus_update_byte_data(client
, page
, PMBUS_OPERATION
,
2065 PB_OPERATION_CONTROL_ON
,
2066 enable
? PB_OPERATION_CONTROL_ON
: 0);
2069 static int pmbus_regulator_enable(struct regulator_dev
*rdev
)
2071 return _pmbus_regulator_on_off(rdev
, 1);
2074 static int pmbus_regulator_disable(struct regulator_dev
*rdev
)
2076 return _pmbus_regulator_on_off(rdev
, 0);
2079 const struct regulator_ops pmbus_regulator_ops
= {
2080 .enable
= pmbus_regulator_enable
,
2081 .disable
= pmbus_regulator_disable
,
2082 .is_enabled
= pmbus_regulator_is_enabled
,
2084 EXPORT_SYMBOL_GPL(pmbus_regulator_ops
);
2086 static int pmbus_regulator_register(struct pmbus_data
*data
)
2088 struct device
*dev
= data
->dev
;
2089 const struct pmbus_driver_info
*info
= data
->info
;
2090 const struct pmbus_platform_data
*pdata
= dev_get_platdata(dev
);
2091 struct regulator_dev
*rdev
;
2094 for (i
= 0; i
< info
->num_regulators
; i
++) {
2095 struct regulator_config config
= { };
2098 config
.driver_data
= data
;
2100 if (pdata
&& pdata
->reg_init_data
)
2101 config
.init_data
= &pdata
->reg_init_data
[i
];
2103 rdev
= devm_regulator_register(dev
, &info
->reg_desc
[i
],
2106 dev_err(dev
, "Failed to register %s regulator\n",
2107 info
->reg_desc
[i
].name
);
2108 return PTR_ERR(rdev
);
2115 static int pmbus_regulator_register(struct pmbus_data
*data
)
2121 static struct dentry
*pmbus_debugfs_dir
; /* pmbus debugfs directory */
2123 #if IS_ENABLED(CONFIG_DEBUG_FS)
2124 static int pmbus_debugfs_get(void *data
, u64
*val
)
2127 struct pmbus_debugfs_entry
*entry
= data
;
2129 rc
= _pmbus_read_byte_data(entry
->client
, entry
->page
, entry
->reg
);
2137 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops
, pmbus_debugfs_get
, NULL
,
2140 static int pmbus_debugfs_get_status(void *data
, u64
*val
)
2143 struct pmbus_debugfs_entry
*entry
= data
;
2144 struct pmbus_data
*pdata
= i2c_get_clientdata(entry
->client
);
2146 rc
= pdata
->read_status(entry
->client
, entry
->page
);
2154 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status
, pmbus_debugfs_get_status
,
2155 NULL
, "0x%04llx\n");
2157 static int pmbus_init_debugfs(struct i2c_client
*client
,
2158 struct pmbus_data
*data
)
2161 char name
[PMBUS_NAME_SIZE
];
2162 struct pmbus_debugfs_entry
*entries
;
2164 if (!pmbus_debugfs_dir
)
2168 * Create the debugfs directory for this device. Use the hwmon device
2169 * name to avoid conflicts (hwmon numbers are globally unique).
2171 data
->debugfs
= debugfs_create_dir(dev_name(data
->hwmon_dev
),
2173 if (IS_ERR_OR_NULL(data
->debugfs
)) {
2174 data
->debugfs
= NULL
;
2178 /* Allocate the max possible entries we need. */
2179 entries
= devm_kzalloc(data
->dev
,
2180 sizeof(*entries
) * (data
->info
->pages
* 10),
2185 for (i
= 0; i
< data
->info
->pages
; ++i
) {
2186 /* Check accessibility of status register if it's not page 0 */
2187 if (!i
|| pmbus_check_status_register(client
, i
)) {
2188 /* No need to set reg as we have special read op. */
2189 entries
[idx
].client
= client
;
2190 entries
[idx
].page
= i
;
2191 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d", i
);
2192 debugfs_create_file(name
, 0444, data
->debugfs
,
2194 &pmbus_debugfs_ops_status
);
2197 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_VOUT
) {
2198 entries
[idx
].client
= client
;
2199 entries
[idx
].page
= i
;
2200 entries
[idx
].reg
= PMBUS_STATUS_VOUT
;
2201 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_vout", i
);
2202 debugfs_create_file(name
, 0444, data
->debugfs
,
2204 &pmbus_debugfs_ops
);
2207 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_IOUT
) {
2208 entries
[idx
].client
= client
;
2209 entries
[idx
].page
= i
;
2210 entries
[idx
].reg
= PMBUS_STATUS_IOUT
;
2211 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_iout", i
);
2212 debugfs_create_file(name
, 0444, data
->debugfs
,
2214 &pmbus_debugfs_ops
);
2217 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_INPUT
) {
2218 entries
[idx
].client
= client
;
2219 entries
[idx
].page
= i
;
2220 entries
[idx
].reg
= PMBUS_STATUS_INPUT
;
2221 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_input", i
);
2222 debugfs_create_file(name
, 0444, data
->debugfs
,
2224 &pmbus_debugfs_ops
);
2227 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_TEMP
) {
2228 entries
[idx
].client
= client
;
2229 entries
[idx
].page
= i
;
2230 entries
[idx
].reg
= PMBUS_STATUS_TEMPERATURE
;
2231 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_temp", i
);
2232 debugfs_create_file(name
, 0444, data
->debugfs
,
2234 &pmbus_debugfs_ops
);
2237 if (pmbus_check_byte_register(client
, i
, PMBUS_STATUS_CML
)) {
2238 entries
[idx
].client
= client
;
2239 entries
[idx
].page
= i
;
2240 entries
[idx
].reg
= PMBUS_STATUS_CML
;
2241 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_cml", i
);
2242 debugfs_create_file(name
, 0444, data
->debugfs
,
2244 &pmbus_debugfs_ops
);
2247 if (pmbus_check_byte_register(client
, i
, PMBUS_STATUS_OTHER
)) {
2248 entries
[idx
].client
= client
;
2249 entries
[idx
].page
= i
;
2250 entries
[idx
].reg
= PMBUS_STATUS_OTHER
;
2251 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_other", i
);
2252 debugfs_create_file(name
, 0444, data
->debugfs
,
2254 &pmbus_debugfs_ops
);
2257 if (pmbus_check_byte_register(client
, i
,
2258 PMBUS_STATUS_MFR_SPECIFIC
)) {
2259 entries
[idx
].client
= client
;
2260 entries
[idx
].page
= i
;
2261 entries
[idx
].reg
= PMBUS_STATUS_MFR_SPECIFIC
;
2262 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_mfr", i
);
2263 debugfs_create_file(name
, 0444, data
->debugfs
,
2265 &pmbus_debugfs_ops
);
2268 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_FAN12
) {
2269 entries
[idx
].client
= client
;
2270 entries
[idx
].page
= i
;
2271 entries
[idx
].reg
= PMBUS_STATUS_FAN_12
;
2272 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_fan12", i
);
2273 debugfs_create_file(name
, 0444, data
->debugfs
,
2275 &pmbus_debugfs_ops
);
2278 if (data
->info
->func
[i
] & PMBUS_HAVE_STATUS_FAN34
) {
2279 entries
[idx
].client
= client
;
2280 entries
[idx
].page
= i
;
2281 entries
[idx
].reg
= PMBUS_STATUS_FAN_34
;
2282 scnprintf(name
, PMBUS_NAME_SIZE
, "status%d_fan34", i
);
2283 debugfs_create_file(name
, 0444, data
->debugfs
,
2285 &pmbus_debugfs_ops
);
2292 static int pmbus_init_debugfs(struct i2c_client
*client
,
2293 struct pmbus_data
*data
)
2297 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */
2299 int pmbus_do_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
,
2300 struct pmbus_driver_info
*info
)
2302 struct device
*dev
= &client
->dev
;
2303 const struct pmbus_platform_data
*pdata
= dev_get_platdata(dev
);
2304 struct pmbus_data
*data
;
2310 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WRITE_BYTE
2311 | I2C_FUNC_SMBUS_BYTE_DATA
2312 | I2C_FUNC_SMBUS_WORD_DATA
))
2315 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
2319 i2c_set_clientdata(client
, data
);
2320 mutex_init(&data
->update_lock
);
2324 data
->flags
= pdata
->flags
;
2327 ret
= pmbus_init_common(client
, data
, info
);
2331 ret
= pmbus_find_attributes(client
, data
);
2336 * If there are no attributes, something is wrong.
2337 * Bail out instead of trying to register nothing.
2339 if (!data
->num_attributes
) {
2340 dev_err(dev
, "No attributes found\n");
2345 data
->groups
[0] = &data
->group
;
2346 data
->hwmon_dev
= hwmon_device_register_with_groups(dev
, client
->name
,
2347 data
, data
->groups
);
2348 if (IS_ERR(data
->hwmon_dev
)) {
2349 ret
= PTR_ERR(data
->hwmon_dev
);
2350 dev_err(dev
, "Failed to register hwmon device\n");
2354 ret
= pmbus_regulator_register(data
);
2356 goto out_unregister
;
2358 ret
= pmbus_init_debugfs(client
, data
);
2360 dev_warn(dev
, "Failed to register debugfs\n");
2365 hwmon_device_unregister(data
->hwmon_dev
);
2367 kfree(data
->group
.attrs
);
2370 EXPORT_SYMBOL_GPL(pmbus_do_probe
);
2372 int pmbus_do_remove(struct i2c_client
*client
)
2374 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2376 debugfs_remove_recursive(data
->debugfs
);
2378 hwmon_device_unregister(data
->hwmon_dev
);
2379 kfree(data
->group
.attrs
);
2382 EXPORT_SYMBOL_GPL(pmbus_do_remove
);
2384 struct dentry
*pmbus_get_debugfs_dir(struct i2c_client
*client
)
2386 struct pmbus_data
*data
= i2c_get_clientdata(client
);
2388 return data
->debugfs
;
2390 EXPORT_SYMBOL_GPL(pmbus_get_debugfs_dir
);
2392 static int __init
pmbus_core_init(void)
2394 pmbus_debugfs_dir
= debugfs_create_dir("pmbus", NULL
);
2395 if (IS_ERR(pmbus_debugfs_dir
))
2396 pmbus_debugfs_dir
= NULL
;
2401 static void __exit
pmbus_core_exit(void)
2403 debugfs_remove_recursive(pmbus_debugfs_dir
);
2406 module_init(pmbus_core_init
);
2407 module_exit(pmbus_core_exit
);
2409 MODULE_AUTHOR("Guenter Roeck");
2410 MODULE_DESCRIPTION("PMBus core driver");
2411 MODULE_LICENSE("GPL");