2 * Hardware monitoring driver for PMBus devices
4 * Copyright (c) 2010, 2011 Ericsson AB.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/hwmon.h>
28 #include <linux/hwmon-sysfs.h>
29 #include <linux/delay.h>
30 #include <linux/i2c/pmbus.h>
34 * Constants needed to determine number of sensors, booleans, and labels.
36 #define PMBUS_MAX_INPUT_SENSORS 22 /* 10*volt, 7*curr, 5*power */
37 #define PMBUS_VOUT_SENSORS_PER_PAGE 9 /* input, min, max, lcrit,
38 crit, lowest, highest, avg,
40 #define PMBUS_IOUT_SENSORS_PER_PAGE 8 /* input, min, max, crit,
43 #define PMBUS_POUT_SENSORS_PER_PAGE 4 /* input, cap, max, crit */
44 #define PMBUS_MAX_SENSORS_PER_FAN 1 /* input */
45 #define PMBUS_MAX_SENSORS_PER_TEMP 8 /* input, min, max, lcrit,
46 crit, lowest, highest,
49 #define PMBUS_MAX_INPUT_BOOLEANS 7 /* v: min_alarm, max_alarm,
50 lcrit_alarm, crit_alarm;
53 #define PMBUS_VOUT_BOOLEANS_PER_PAGE 4 /* min_alarm, max_alarm,
54 lcrit_alarm, crit_alarm */
55 #define PMBUS_IOUT_BOOLEANS_PER_PAGE 3 /* alarm, lcrit_alarm,
57 #define PMBUS_POUT_BOOLEANS_PER_PAGE 3 /* cap_alarm, alarm, crit_alarm
59 #define PMBUS_MAX_BOOLEANS_PER_FAN 2 /* alarm, fault */
60 #define PMBUS_MAX_BOOLEANS_PER_TEMP 4 /* min_alarm, max_alarm,
61 lcrit_alarm, crit_alarm */
63 #define PMBUS_MAX_INPUT_LABELS 4 /* vin, vcap, iin, pin */
66 * status, status_vout, status_iout, status_fans, status_fan34, and status_temp
67 * are paged. status_input is unpaged.
69 #define PB_NUM_STATUS_REG (PMBUS_PAGES * 6 + 1)
72 * Index into status register array, per status register group
74 #define PB_STATUS_BASE 0
75 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
76 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
77 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
78 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
79 #define PB_STATUS_INPUT_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
80 #define PB_STATUS_TEMP_BASE (PB_STATUS_INPUT_BASE + 1)
82 #define PMBUS_NAME_SIZE 24
85 char name
[PMBUS_NAME_SIZE
]; /* sysfs sensor name */
86 struct sensor_device_attribute attribute
;
87 u8 page
; /* page number */
88 u16 reg
; /* register */
89 enum pmbus_sensor_classes
class; /* sensor class */
90 bool update
; /* runtime sensor update needed */
91 int data
; /* Sensor data.
92 Negative if there was a read error */
95 struct pmbus_boolean
{
96 char name
[PMBUS_NAME_SIZE
]; /* sysfs boolean name */
97 struct sensor_device_attribute attribute
;
101 char name
[PMBUS_NAME_SIZE
]; /* sysfs label name */
102 struct sensor_device_attribute attribute
;
103 char label
[PMBUS_NAME_SIZE
]; /* label */
107 struct device
*hwmon_dev
;
109 u32 flags
; /* from platform data */
111 int exponent
; /* linear mode: exponent for output voltages */
113 const struct pmbus_driver_info
*info
;
117 struct attribute
**attributes
;
118 struct attribute_group group
;
121 * Sensors cover both sensor and limit registers.
125 struct pmbus_sensor
*sensors
;
127 * Booleans are used for alarms.
128 * Values are determined from status registers.
132 struct pmbus_boolean
*booleans
;
134 * Labels are used to map generic names (e.g., "in1")
135 * to PMBus specific names (e.g., "vin" or "vout1").
139 struct pmbus_label
*labels
;
141 struct mutex update_lock
;
143 unsigned long last_updated
; /* in jiffies */
146 * A single status register covers multiple attributes,
147 * so we keep them all together.
149 u8 status
[PB_NUM_STATUS_REG
];
154 int pmbus_set_page(struct i2c_client
*client
, u8 page
)
156 struct pmbus_data
*data
= i2c_get_clientdata(client
);
160 if (page
!= data
->currpage
) {
161 rv
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, page
);
162 newpage
= i2c_smbus_read_byte_data(client
, PMBUS_PAGE
);
166 data
->currpage
= page
;
170 EXPORT_SYMBOL_GPL(pmbus_set_page
);
172 int pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
177 rv
= pmbus_set_page(client
, page
);
182 return i2c_smbus_write_byte(client
, value
);
184 EXPORT_SYMBOL_GPL(pmbus_write_byte
);
187 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
188 * a device specific mapping funcion exists and calls it if necessary.
190 static int _pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
192 struct pmbus_data
*data
= i2c_get_clientdata(client
);
193 const struct pmbus_driver_info
*info
= data
->info
;
196 if (info
->write_byte
) {
197 status
= info
->write_byte(client
, page
, value
);
198 if (status
!= -ENODATA
)
201 return pmbus_write_byte(client
, page
, value
);
204 int pmbus_write_word_data(struct i2c_client
*client
, u8 page
, u8 reg
, u16 word
)
208 rv
= pmbus_set_page(client
, page
);
212 return i2c_smbus_write_word_data(client
, reg
, word
);
214 EXPORT_SYMBOL_GPL(pmbus_write_word_data
);
217 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
218 * a device specific mapping function exists and calls it if necessary.
220 static int _pmbus_write_word_data(struct i2c_client
*client
, int page
, int reg
,
223 struct pmbus_data
*data
= i2c_get_clientdata(client
);
224 const struct pmbus_driver_info
*info
= data
->info
;
227 if (info
->write_word_data
) {
228 status
= info
->write_word_data(client
, page
, reg
, word
);
229 if (status
!= -ENODATA
)
232 if (reg
>= PMBUS_VIRT_BASE
)
234 return pmbus_write_word_data(client
, page
, reg
, word
);
237 int pmbus_read_word_data(struct i2c_client
*client
, u8 page
, u8 reg
)
241 rv
= pmbus_set_page(client
, page
);
245 return i2c_smbus_read_word_data(client
, reg
);
247 EXPORT_SYMBOL_GPL(pmbus_read_word_data
);
250 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
251 * a device specific mapping function exists and calls it if necessary.
253 static int _pmbus_read_word_data(struct i2c_client
*client
, int page
, int reg
)
255 struct pmbus_data
*data
= i2c_get_clientdata(client
);
256 const struct pmbus_driver_info
*info
= data
->info
;
259 if (info
->read_word_data
) {
260 status
= info
->read_word_data(client
, page
, reg
);
261 if (status
!= -ENODATA
)
264 if (reg
>= PMBUS_VIRT_BASE
)
266 return pmbus_read_word_data(client
, page
, reg
);
269 int pmbus_read_byte_data(struct i2c_client
*client
, int page
, u8 reg
)
274 rv
= pmbus_set_page(client
, page
);
279 return i2c_smbus_read_byte_data(client
, reg
);
281 EXPORT_SYMBOL_GPL(pmbus_read_byte_data
);
284 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
285 * a device specific mapping function exists and calls it if necessary.
287 static int _pmbus_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
289 struct pmbus_data
*data
= i2c_get_clientdata(client
);
290 const struct pmbus_driver_info
*info
= data
->info
;
293 if (info
->read_byte_data
) {
294 status
= info
->read_byte_data(client
, page
, reg
);
295 if (status
!= -ENODATA
)
298 return pmbus_read_byte_data(client
, page
, reg
);
301 static void pmbus_clear_fault_page(struct i2c_client
*client
, int page
)
303 _pmbus_write_byte(client
, page
, PMBUS_CLEAR_FAULTS
);
306 void pmbus_clear_faults(struct i2c_client
*client
)
308 struct pmbus_data
*data
= i2c_get_clientdata(client
);
311 for (i
= 0; i
< data
->info
->pages
; i
++)
312 pmbus_clear_fault_page(client
, i
);
314 EXPORT_SYMBOL_GPL(pmbus_clear_faults
);
316 static int pmbus_check_status_cml(struct i2c_client
*client
)
320 status
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_BYTE
);
321 if (status
< 0 || (status
& PB_STATUS_CML
)) {
322 status2
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
323 if (status2
< 0 || (status2
& PB_CML_FAULT_INVALID_COMMAND
))
329 bool pmbus_check_byte_register(struct i2c_client
*client
, int page
, int reg
)
332 struct pmbus_data
*data
= i2c_get_clientdata(client
);
334 rv
= _pmbus_read_byte_data(client
, page
, reg
);
335 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
336 rv
= pmbus_check_status_cml(client
);
337 pmbus_clear_fault_page(client
, -1);
340 EXPORT_SYMBOL_GPL(pmbus_check_byte_register
);
342 bool pmbus_check_word_register(struct i2c_client
*client
, int page
, int reg
)
345 struct pmbus_data
*data
= i2c_get_clientdata(client
);
347 rv
= _pmbus_read_word_data(client
, page
, reg
);
348 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
349 rv
= pmbus_check_status_cml(client
);
350 pmbus_clear_fault_page(client
, -1);
353 EXPORT_SYMBOL_GPL(pmbus_check_word_register
);
355 const struct pmbus_driver_info
*pmbus_get_driver_info(struct i2c_client
*client
)
357 struct pmbus_data
*data
= i2c_get_clientdata(client
);
361 EXPORT_SYMBOL_GPL(pmbus_get_driver_info
);
363 static struct pmbus_data
*pmbus_update_device(struct device
*dev
)
365 struct i2c_client
*client
= to_i2c_client(dev
);
366 struct pmbus_data
*data
= i2c_get_clientdata(client
);
367 const struct pmbus_driver_info
*info
= data
->info
;
369 mutex_lock(&data
->update_lock
);
370 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
373 for (i
= 0; i
< info
->pages
; i
++)
374 data
->status
[PB_STATUS_BASE
+ i
]
375 = _pmbus_read_byte_data(client
, i
,
377 for (i
= 0; i
< info
->pages
; i
++) {
378 if (!(info
->func
[i
] & PMBUS_HAVE_STATUS_VOUT
))
380 data
->status
[PB_STATUS_VOUT_BASE
+ i
]
381 = _pmbus_read_byte_data(client
, i
, PMBUS_STATUS_VOUT
);
383 for (i
= 0; i
< info
->pages
; i
++) {
384 if (!(info
->func
[i
] & PMBUS_HAVE_STATUS_IOUT
))
386 data
->status
[PB_STATUS_IOUT_BASE
+ i
]
387 = _pmbus_read_byte_data(client
, i
, PMBUS_STATUS_IOUT
);
389 for (i
= 0; i
< info
->pages
; i
++) {
390 if (!(info
->func
[i
] & PMBUS_HAVE_STATUS_TEMP
))
392 data
->status
[PB_STATUS_TEMP_BASE
+ i
]
393 = _pmbus_read_byte_data(client
, i
,
394 PMBUS_STATUS_TEMPERATURE
);
396 for (i
= 0; i
< info
->pages
; i
++) {
397 if (!(info
->func
[i
] & PMBUS_HAVE_STATUS_FAN12
))
399 data
->status
[PB_STATUS_FAN_BASE
+ i
]
400 = _pmbus_read_byte_data(client
, i
,
401 PMBUS_STATUS_FAN_12
);
404 for (i
= 0; i
< info
->pages
; i
++) {
405 if (!(info
->func
[i
] & PMBUS_HAVE_STATUS_FAN34
))
407 data
->status
[PB_STATUS_FAN34_BASE
+ i
]
408 = _pmbus_read_byte_data(client
, i
,
409 PMBUS_STATUS_FAN_34
);
412 if (info
->func
[0] & PMBUS_HAVE_STATUS_INPUT
)
413 data
->status
[PB_STATUS_INPUT_BASE
]
414 = _pmbus_read_byte_data(client
, 0,
417 for (i
= 0; i
< data
->num_sensors
; i
++) {
418 struct pmbus_sensor
*sensor
= &data
->sensors
[i
];
420 if (!data
->valid
|| sensor
->update
)
422 = _pmbus_read_word_data(client
,
426 pmbus_clear_faults(client
);
427 data
->last_updated
= jiffies
;
430 mutex_unlock(&data
->update_lock
);
435 * Convert linear sensor values to milli- or micro-units
436 * depending on sensor type.
438 static long pmbus_reg2data_linear(struct pmbus_data
*data
,
439 struct pmbus_sensor
*sensor
)
445 if (sensor
->class == PSC_VOLTAGE_OUT
) { /* LINEAR16 */
446 exponent
= data
->exponent
;
447 mantissa
= (u16
) sensor
->data
;
448 } else { /* LINEAR11 */
449 exponent
= ((s16
)sensor
->data
) >> 11;
450 mantissa
= ((s16
)((sensor
->data
& 0x7ff) << 5)) >> 5;
455 /* scale result to milli-units for all sensors except fans */
456 if (sensor
->class != PSC_FAN
)
459 /* scale result to micro-units for power sensors */
460 if (sensor
->class == PSC_POWER
)
472 * Convert direct sensor values to milli- or micro-units
473 * depending on sensor type.
475 static long pmbus_reg2data_direct(struct pmbus_data
*data
,
476 struct pmbus_sensor
*sensor
)
478 long val
= (s16
) sensor
->data
;
481 m
= data
->info
->m
[sensor
->class];
482 b
= data
->info
->b
[sensor
->class];
483 R
= data
->info
->R
[sensor
->class];
488 /* X = 1/m * (Y * 10^-R - b) */
490 /* scale result to milli-units for everything but fans */
491 if (sensor
->class != PSC_FAN
) {
496 /* scale result to micro-units for power sensors */
497 if (sensor
->class == PSC_POWER
) {
507 val
= DIV_ROUND_CLOSEST(val
, 10);
511 return (val
- b
) / m
;
515 * Convert VID sensor values to milli- or micro-units
516 * depending on sensor type.
517 * We currently only support VR11.
519 static long pmbus_reg2data_vid(struct pmbus_data
*data
,
520 struct pmbus_sensor
*sensor
)
522 long val
= sensor
->data
;
524 if (val
< 0x02 || val
> 0xb2)
526 return DIV_ROUND_CLOSEST(160000 - (val
- 2) * 625, 100);
529 static long pmbus_reg2data(struct pmbus_data
*data
, struct pmbus_sensor
*sensor
)
533 switch (data
->info
->format
[sensor
->class]) {
535 val
= pmbus_reg2data_direct(data
, sensor
);
538 val
= pmbus_reg2data_vid(data
, sensor
);
542 val
= pmbus_reg2data_linear(data
, sensor
);
548 #define MAX_MANTISSA (1023 * 1000)
549 #define MIN_MANTISSA (511 * 1000)
551 static u16
pmbus_data2reg_linear(struct pmbus_data
*data
,
552 enum pmbus_sensor_classes
class, long val
)
554 s16 exponent
= 0, mantissa
;
555 bool negative
= false;
561 if (class == PSC_VOLTAGE_OUT
) {
562 /* LINEAR16 does not support negative voltages */
567 * For a static exponents, we don't have a choice
568 * but to adjust the value to it.
570 if (data
->exponent
< 0)
571 val
<<= -data
->exponent
;
573 val
>>= data
->exponent
;
574 val
= DIV_ROUND_CLOSEST(val
, 1000);
583 /* Power is in uW. Convert to mW before converting. */
584 if (class == PSC_POWER
)
585 val
= DIV_ROUND_CLOSEST(val
, 1000L);
588 * For simplicity, convert fan data to milli-units
589 * before calculating the exponent.
591 if (class == PSC_FAN
)
594 /* Reduce large mantissa until it fits into 10 bit */
595 while (val
>= MAX_MANTISSA
&& exponent
< 15) {
599 /* Increase small mantissa to improve precision */
600 while (val
< MIN_MANTISSA
&& exponent
> -15) {
605 /* Convert mantissa from milli-units to units */
606 mantissa
= DIV_ROUND_CLOSEST(val
, 1000);
608 /* Ensure that resulting number is within range */
609 if (mantissa
> 0x3ff)
614 mantissa
= -mantissa
;
616 /* Convert to 5 bit exponent, 11 bit mantissa */
617 return (mantissa
& 0x7ff) | ((exponent
<< 11) & 0xf800);
620 static u16
pmbus_data2reg_direct(struct pmbus_data
*data
,
621 enum pmbus_sensor_classes
class, long val
)
625 m
= data
->info
->m
[class];
626 b
= data
->info
->b
[class];
627 R
= data
->info
->R
[class];
629 /* Power is in uW. Adjust R and b. */
630 if (class == PSC_POWER
) {
635 /* Calculate Y = (m * X + b) * 10^R */
636 if (class != PSC_FAN
) {
637 R
-= 3; /* Adjust R and b for data in milli-units */
647 val
= DIV_ROUND_CLOSEST(val
, 10);
654 static u16
pmbus_data2reg_vid(struct pmbus_data
*data
,
655 enum pmbus_sensor_classes
class, long val
)
657 val
= SENSORS_LIMIT(val
, 500, 1600);
659 return 2 + DIV_ROUND_CLOSEST((1600 - val
) * 100, 625);
662 static u16
pmbus_data2reg(struct pmbus_data
*data
,
663 enum pmbus_sensor_classes
class, long val
)
667 switch (data
->info
->format
[class]) {
669 regval
= pmbus_data2reg_direct(data
, class, val
);
672 regval
= pmbus_data2reg_vid(data
, class, val
);
676 regval
= pmbus_data2reg_linear(data
, class, val
);
683 * Return boolean calculated from converted data.
684 * <index> defines a status register index and mask, and optionally
685 * two sensor indexes.
686 * The upper half-word references the two sensors,
687 * two sensor indices.
688 * The upper half-word references the two optional sensors,
689 * the lower half word references status register and mask.
690 * The function returns true if (status[reg] & mask) is true and,
691 * if specified, if v1 >= v2.
692 * To determine if an object exceeds upper limits, specify <v, limit>.
693 * To determine if an object exceeds lower limits, specify <limit, v>.
695 * For booleans created with pmbus_add_boolean_reg(), only the lower 16 bits of
696 * index are set. s1 and s2 (the sensor index values) are zero in this case.
697 * The function returns true if (status[reg] & mask) is true.
699 * If the boolean was created with pmbus_add_boolean_cmp(), a comparison against
700 * a specified limit has to be performed to determine the boolean result.
701 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
702 * sensor values referenced by sensor indices s1 and s2).
704 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
705 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
707 * If a negative value is stored in any of the referenced registers, this value
708 * reflects an error code which will be returned.
710 static int pmbus_get_boolean(struct pmbus_data
*data
, int index
, int *val
)
712 u8 s1
= (index
>> 24) & 0xff;
713 u8 s2
= (index
>> 16) & 0xff;
714 u8 reg
= (index
>> 8) & 0xff;
715 u8 mask
= index
& 0xff;
719 status
= data
->status
[reg
];
723 regval
= status
& mask
;
728 struct pmbus_sensor
*sensor1
, *sensor2
;
730 sensor1
= &data
->sensors
[s1
];
731 if (sensor1
->data
< 0)
732 return sensor1
->data
;
733 sensor2
= &data
->sensors
[s2
];
734 if (sensor2
->data
< 0)
735 return sensor2
->data
;
737 v1
= pmbus_reg2data(data
, sensor1
);
738 v2
= pmbus_reg2data(data
, sensor2
);
739 *val
= !!(regval
&& v1
>= v2
);
744 static ssize_t
pmbus_show_boolean(struct device
*dev
,
745 struct device_attribute
*da
, char *buf
)
747 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
748 struct pmbus_data
*data
= pmbus_update_device(dev
);
752 err
= pmbus_get_boolean(data
, attr
->index
, &val
);
755 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
758 static ssize_t
pmbus_show_sensor(struct device
*dev
,
759 struct device_attribute
*da
, char *buf
)
761 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
762 struct pmbus_data
*data
= pmbus_update_device(dev
);
763 struct pmbus_sensor
*sensor
;
765 sensor
= &data
->sensors
[attr
->index
];
766 if (sensor
->data
< 0)
769 return snprintf(buf
, PAGE_SIZE
, "%ld\n", pmbus_reg2data(data
, sensor
));
772 static ssize_t
pmbus_set_sensor(struct device
*dev
,
773 struct device_attribute
*devattr
,
774 const char *buf
, size_t count
)
776 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
777 struct i2c_client
*client
= to_i2c_client(dev
);
778 struct pmbus_data
*data
= i2c_get_clientdata(client
);
779 struct pmbus_sensor
*sensor
= &data
->sensors
[attr
->index
];
785 if (strict_strtol(buf
, 10, &val
) < 0)
788 mutex_lock(&data
->update_lock
);
789 regval
= pmbus_data2reg(data
, sensor
->class, val
);
790 ret
= _pmbus_write_word_data(client
, sensor
->page
, sensor
->reg
, regval
);
794 data
->sensors
[attr
->index
].data
= regval
;
795 mutex_unlock(&data
->update_lock
);
799 static ssize_t
pmbus_show_label(struct device
*dev
,
800 struct device_attribute
*da
, char *buf
)
802 struct i2c_client
*client
= to_i2c_client(dev
);
803 struct pmbus_data
*data
= i2c_get_clientdata(client
);
804 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
806 return snprintf(buf
, PAGE_SIZE
, "%s\n",
807 data
->labels
[attr
->index
].label
);
810 #define PMBUS_ADD_ATTR(data, _name, _idx, _mode, _type, _show, _set) \
812 struct sensor_device_attribute *a \
813 = &data->_type##s[data->num_##_type##s].attribute; \
814 BUG_ON(data->num_attributes >= data->max_attributes); \
815 sysfs_attr_init(&a->dev_attr.attr); \
816 a->dev_attr.attr.name = _name; \
817 a->dev_attr.attr.mode = _mode; \
818 a->dev_attr.show = _show; \
819 a->dev_attr.store = _set; \
821 data->attributes[data->num_attributes] = &a->dev_attr.attr; \
822 data->num_attributes++; \
825 #define PMBUS_ADD_GET_ATTR(data, _name, _type, _idx) \
826 PMBUS_ADD_ATTR(data, _name, _idx, S_IRUGO, _type, \
827 pmbus_show_##_type, NULL)
829 #define PMBUS_ADD_SET_ATTR(data, _name, _type, _idx) \
830 PMBUS_ADD_ATTR(data, _name, _idx, S_IWUSR | S_IRUGO, _type, \
831 pmbus_show_##_type, pmbus_set_##_type)
833 static void pmbus_add_boolean(struct pmbus_data
*data
,
834 const char *name
, const char *type
, int seq
,
837 struct pmbus_boolean
*boolean
;
839 BUG_ON(data
->num_booleans
>= data
->max_booleans
);
841 boolean
= &data
->booleans
[data
->num_booleans
];
843 snprintf(boolean
->name
, sizeof(boolean
->name
), "%s%d_%s",
845 PMBUS_ADD_GET_ATTR(data
, boolean
->name
, boolean
, idx
);
846 data
->num_booleans
++;
849 static void pmbus_add_boolean_reg(struct pmbus_data
*data
,
850 const char *name
, const char *type
,
851 int seq
, int reg
, int bit
)
853 pmbus_add_boolean(data
, name
, type
, seq
, (reg
<< 8) | bit
);
856 static void pmbus_add_boolean_cmp(struct pmbus_data
*data
,
857 const char *name
, const char *type
,
858 int seq
, int i1
, int i2
, int reg
, int mask
)
860 pmbus_add_boolean(data
, name
, type
, seq
,
861 (i1
<< 24) | (i2
<< 16) | (reg
<< 8) | mask
);
864 static void pmbus_add_sensor(struct pmbus_data
*data
,
865 const char *name
, const char *type
, int seq
,
866 int page
, int reg
, enum pmbus_sensor_classes
class,
867 bool update
, bool readonly
)
869 struct pmbus_sensor
*sensor
;
871 BUG_ON(data
->num_sensors
>= data
->max_sensors
);
873 sensor
= &data
->sensors
[data
->num_sensors
];
874 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d_%s",
878 sensor
->class = class;
879 sensor
->update
= update
;
881 PMBUS_ADD_GET_ATTR(data
, sensor
->name
, sensor
,
884 PMBUS_ADD_SET_ATTR(data
, sensor
->name
, sensor
,
889 static void pmbus_add_label(struct pmbus_data
*data
,
890 const char *name
, int seq
,
891 const char *lstring
, int index
)
893 struct pmbus_label
*label
;
895 BUG_ON(data
->num_labels
>= data
->max_labels
);
897 label
= &data
->labels
[data
->num_labels
];
898 snprintf(label
->name
, sizeof(label
->name
), "%s%d_label", name
, seq
);
900 strncpy(label
->label
, lstring
, sizeof(label
->label
) - 1);
902 snprintf(label
->label
, sizeof(label
->label
), "%s%d", lstring
,
905 PMBUS_ADD_GET_ATTR(data
, label
->name
, label
, data
->num_labels
);
910 * Determine maximum number of sensors, booleans, and labels.
911 * To keep things simple, only make a rough high estimate.
913 static void pmbus_find_max_attr(struct i2c_client
*client
,
914 struct pmbus_data
*data
)
916 const struct pmbus_driver_info
*info
= data
->info
;
917 int page
, max_sensors
, max_booleans
, max_labels
;
919 max_sensors
= PMBUS_MAX_INPUT_SENSORS
;
920 max_booleans
= PMBUS_MAX_INPUT_BOOLEANS
;
921 max_labels
= PMBUS_MAX_INPUT_LABELS
;
923 for (page
= 0; page
< info
->pages
; page
++) {
924 if (info
->func
[page
] & PMBUS_HAVE_VOUT
) {
925 max_sensors
+= PMBUS_VOUT_SENSORS_PER_PAGE
;
926 max_booleans
+= PMBUS_VOUT_BOOLEANS_PER_PAGE
;
929 if (info
->func
[page
] & PMBUS_HAVE_IOUT
) {
930 max_sensors
+= PMBUS_IOUT_SENSORS_PER_PAGE
;
931 max_booleans
+= PMBUS_IOUT_BOOLEANS_PER_PAGE
;
934 if (info
->func
[page
] & PMBUS_HAVE_POUT
) {
935 max_sensors
+= PMBUS_POUT_SENSORS_PER_PAGE
;
936 max_booleans
+= PMBUS_POUT_BOOLEANS_PER_PAGE
;
939 if (info
->func
[page
] & PMBUS_HAVE_FAN12
) {
940 max_sensors
+= 2 * PMBUS_MAX_SENSORS_PER_FAN
;
941 max_booleans
+= 2 * PMBUS_MAX_BOOLEANS_PER_FAN
;
943 if (info
->func
[page
] & PMBUS_HAVE_FAN34
) {
944 max_sensors
+= 2 * PMBUS_MAX_SENSORS_PER_FAN
;
945 max_booleans
+= 2 * PMBUS_MAX_BOOLEANS_PER_FAN
;
947 if (info
->func
[page
] & PMBUS_HAVE_TEMP
) {
948 max_sensors
+= PMBUS_MAX_SENSORS_PER_TEMP
;
949 max_booleans
+= PMBUS_MAX_BOOLEANS_PER_TEMP
;
951 if (info
->func
[page
] & PMBUS_HAVE_TEMP2
) {
952 max_sensors
+= PMBUS_MAX_SENSORS_PER_TEMP
;
953 max_booleans
+= PMBUS_MAX_BOOLEANS_PER_TEMP
;
955 if (info
->func
[page
] & PMBUS_HAVE_TEMP3
) {
956 max_sensors
+= PMBUS_MAX_SENSORS_PER_TEMP
;
957 max_booleans
+= PMBUS_MAX_BOOLEANS_PER_TEMP
;
960 data
->max_sensors
= max_sensors
;
961 data
->max_booleans
= max_booleans
;
962 data
->max_labels
= max_labels
;
963 data
->max_attributes
= max_sensors
+ max_booleans
+ max_labels
;
967 * Search for attributes. Allocate sensors, booleans, and labels as needed.
971 * The pmbus_limit_attr structure describes a single limit attribute
972 * and its associated alarm attribute.
974 struct pmbus_limit_attr
{
975 u16 reg
; /* Limit register */
976 bool update
; /* True if register needs updates */
977 bool low
; /* True if low limit; for limits with compare
979 const char *attr
; /* Attribute name */
980 const char *alarm
; /* Alarm attribute name */
981 u32 sbit
; /* Alarm attribute status bit */
985 * The pmbus_sensor_attr structure describes one sensor attribute. This
986 * description includes a reference to the associated limit attributes.
988 struct pmbus_sensor_attr
{
989 u8 reg
; /* sensor register */
990 enum pmbus_sensor_classes
class;/* sensor class */
991 const char *label
; /* sensor label */
992 bool paged
; /* true if paged sensor */
993 bool update
; /* true if update needed */
994 bool compare
; /* true if compare function needed */
995 u32 func
; /* sensor mask */
996 u32 sfunc
; /* sensor status mask */
997 int sbase
; /* status base register */
998 u32 gbit
; /* generic status bit */
999 const struct pmbus_limit_attr
*limit
;/* limit registers */
1000 int nlimit
; /* # of limit registers */
1004 * Add a set of limit attributes and, if supported, the associated
1007 static bool pmbus_add_limit_attrs(struct i2c_client
*client
,
1008 struct pmbus_data
*data
,
1009 const struct pmbus_driver_info
*info
,
1010 const char *name
, int index
, int page
,
1012 const struct pmbus_sensor_attr
*attr
)
1014 const struct pmbus_limit_attr
*l
= attr
->limit
;
1015 int nlimit
= attr
->nlimit
;
1016 bool have_alarm
= false;
1019 for (i
= 0; i
< nlimit
; i
++) {
1020 if (pmbus_check_word_register(client
, page
, l
->reg
)) {
1021 cindex
= data
->num_sensors
;
1022 pmbus_add_sensor(data
, name
, l
->attr
, index
, page
,
1023 l
->reg
, attr
->class,
1024 attr
->update
|| l
->update
,
1026 if (l
->sbit
&& (info
->func
[page
] & attr
->sfunc
)) {
1027 if (attr
->compare
) {
1028 pmbus_add_boolean_cmp(data
, name
,
1030 l
->low
? cindex
: cbase
,
1031 l
->low
? cbase
: cindex
,
1032 attr
->sbase
+ page
, l
->sbit
);
1034 pmbus_add_boolean_reg(data
, name
,
1036 attr
->sbase
+ page
, l
->sbit
);
1046 static void pmbus_add_sensor_attrs_one(struct i2c_client
*client
,
1047 struct pmbus_data
*data
,
1048 const struct pmbus_driver_info
*info
,
1050 int index
, int page
,
1051 const struct pmbus_sensor_attr
*attr
)
1054 int cbase
= data
->num_sensors
;
1057 pmbus_add_label(data
, name
, index
, attr
->label
,
1058 attr
->paged
? page
+ 1 : 0);
1059 pmbus_add_sensor(data
, name
, "input", index
, page
, attr
->reg
,
1060 attr
->class, true, true);
1062 have_alarm
= pmbus_add_limit_attrs(client
, data
, info
, name
,
1063 index
, page
, cbase
, attr
);
1065 * Add generic alarm attribute only if there are no individual
1066 * alarm attributes, if there is a global alarm bit, and if
1067 * the generic status register for this page is accessible.
1069 if (!have_alarm
&& attr
->gbit
&&
1070 pmbus_check_byte_register(client
, page
, PMBUS_STATUS_BYTE
))
1071 pmbus_add_boolean_reg(data
, name
, "alarm", index
,
1072 PB_STATUS_BASE
+ page
,
1077 static void pmbus_add_sensor_attrs(struct i2c_client
*client
,
1078 struct pmbus_data
*data
,
1080 const struct pmbus_sensor_attr
*attrs
,
1083 const struct pmbus_driver_info
*info
= data
->info
;
1087 for (i
= 0; i
< nattrs
; i
++) {
1090 pages
= attrs
->paged
? info
->pages
: 1;
1091 for (page
= 0; page
< pages
; page
++) {
1092 if (!(info
->func
[page
] & attrs
->func
))
1094 pmbus_add_sensor_attrs_one(client
, data
, info
, name
,
1095 index
, page
, attrs
);
1102 static const struct pmbus_limit_attr vin_limit_attrs
[] = {
1104 .reg
= PMBUS_VIN_UV_WARN_LIMIT
,
1106 .alarm
= "min_alarm",
1107 .sbit
= PB_VOLTAGE_UV_WARNING
,
1109 .reg
= PMBUS_VIN_UV_FAULT_LIMIT
,
1111 .alarm
= "lcrit_alarm",
1112 .sbit
= PB_VOLTAGE_UV_FAULT
,
1114 .reg
= PMBUS_VIN_OV_WARN_LIMIT
,
1116 .alarm
= "max_alarm",
1117 .sbit
= PB_VOLTAGE_OV_WARNING
,
1119 .reg
= PMBUS_VIN_OV_FAULT_LIMIT
,
1121 .alarm
= "crit_alarm",
1122 .sbit
= PB_VOLTAGE_OV_FAULT
,
1124 .reg
= PMBUS_VIRT_READ_VIN_AVG
,
1128 .reg
= PMBUS_VIRT_READ_VIN_MIN
,
1132 .reg
= PMBUS_VIRT_READ_VIN_MAX
,
1136 .reg
= PMBUS_VIRT_RESET_VIN_HISTORY
,
1137 .attr
= "reset_history",
1141 static const struct pmbus_limit_attr vout_limit_attrs
[] = {
1143 .reg
= PMBUS_VOUT_UV_WARN_LIMIT
,
1145 .alarm
= "min_alarm",
1146 .sbit
= PB_VOLTAGE_UV_WARNING
,
1148 .reg
= PMBUS_VOUT_UV_FAULT_LIMIT
,
1150 .alarm
= "lcrit_alarm",
1151 .sbit
= PB_VOLTAGE_UV_FAULT
,
1153 .reg
= PMBUS_VOUT_OV_WARN_LIMIT
,
1155 .alarm
= "max_alarm",
1156 .sbit
= PB_VOLTAGE_OV_WARNING
,
1158 .reg
= PMBUS_VOUT_OV_FAULT_LIMIT
,
1160 .alarm
= "crit_alarm",
1161 .sbit
= PB_VOLTAGE_OV_FAULT
,
1163 .reg
= PMBUS_VIRT_READ_VOUT_AVG
,
1167 .reg
= PMBUS_VIRT_READ_VOUT_MIN
,
1171 .reg
= PMBUS_VIRT_READ_VOUT_MAX
,
1175 .reg
= PMBUS_VIRT_RESET_VOUT_HISTORY
,
1176 .attr
= "reset_history",
1180 static const struct pmbus_sensor_attr voltage_attributes
[] = {
1182 .reg
= PMBUS_READ_VIN
,
1183 .class = PSC_VOLTAGE_IN
,
1185 .func
= PMBUS_HAVE_VIN
,
1186 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1187 .sbase
= PB_STATUS_INPUT_BASE
,
1188 .gbit
= PB_STATUS_VIN_UV
,
1189 .limit
= vin_limit_attrs
,
1190 .nlimit
= ARRAY_SIZE(vin_limit_attrs
),
1192 .reg
= PMBUS_READ_VCAP
,
1193 .class = PSC_VOLTAGE_IN
,
1195 .func
= PMBUS_HAVE_VCAP
,
1197 .reg
= PMBUS_READ_VOUT
,
1198 .class = PSC_VOLTAGE_OUT
,
1201 .func
= PMBUS_HAVE_VOUT
,
1202 .sfunc
= PMBUS_HAVE_STATUS_VOUT
,
1203 .sbase
= PB_STATUS_VOUT_BASE
,
1204 .gbit
= PB_STATUS_VOUT_OV
,
1205 .limit
= vout_limit_attrs
,
1206 .nlimit
= ARRAY_SIZE(vout_limit_attrs
),
1210 /* Current attributes */
1212 static const struct pmbus_limit_attr iin_limit_attrs
[] = {
1214 .reg
= PMBUS_IIN_OC_WARN_LIMIT
,
1216 .alarm
= "max_alarm",
1217 .sbit
= PB_IIN_OC_WARNING
,
1219 .reg
= PMBUS_IIN_OC_FAULT_LIMIT
,
1221 .alarm
= "crit_alarm",
1222 .sbit
= PB_IIN_OC_FAULT
,
1224 .reg
= PMBUS_VIRT_READ_IIN_AVG
,
1228 .reg
= PMBUS_VIRT_READ_IIN_MIN
,
1232 .reg
= PMBUS_VIRT_READ_IIN_MAX
,
1236 .reg
= PMBUS_VIRT_RESET_IIN_HISTORY
,
1237 .attr
= "reset_history",
1241 static const struct pmbus_limit_attr iout_limit_attrs
[] = {
1243 .reg
= PMBUS_IOUT_OC_WARN_LIMIT
,
1245 .alarm
= "max_alarm",
1246 .sbit
= PB_IOUT_OC_WARNING
,
1248 .reg
= PMBUS_IOUT_UC_FAULT_LIMIT
,
1250 .alarm
= "lcrit_alarm",
1251 .sbit
= PB_IOUT_UC_FAULT
,
1253 .reg
= PMBUS_IOUT_OC_FAULT_LIMIT
,
1255 .alarm
= "crit_alarm",
1256 .sbit
= PB_IOUT_OC_FAULT
,
1258 .reg
= PMBUS_VIRT_READ_IOUT_AVG
,
1262 .reg
= PMBUS_VIRT_READ_IOUT_MIN
,
1266 .reg
= PMBUS_VIRT_READ_IOUT_MAX
,
1270 .reg
= PMBUS_VIRT_RESET_IOUT_HISTORY
,
1271 .attr
= "reset_history",
1275 static const struct pmbus_sensor_attr current_attributes
[] = {
1277 .reg
= PMBUS_READ_IIN
,
1278 .class = PSC_CURRENT_IN
,
1280 .func
= PMBUS_HAVE_IIN
,
1281 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1282 .sbase
= PB_STATUS_INPUT_BASE
,
1283 .limit
= iin_limit_attrs
,
1284 .nlimit
= ARRAY_SIZE(iin_limit_attrs
),
1286 .reg
= PMBUS_READ_IOUT
,
1287 .class = PSC_CURRENT_OUT
,
1290 .func
= PMBUS_HAVE_IOUT
,
1291 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1292 .sbase
= PB_STATUS_IOUT_BASE
,
1293 .gbit
= PB_STATUS_IOUT_OC
,
1294 .limit
= iout_limit_attrs
,
1295 .nlimit
= ARRAY_SIZE(iout_limit_attrs
),
1299 /* Power attributes */
1301 static const struct pmbus_limit_attr pin_limit_attrs
[] = {
1303 .reg
= PMBUS_PIN_OP_WARN_LIMIT
,
1306 .sbit
= PB_PIN_OP_WARNING
,
1308 .reg
= PMBUS_VIRT_READ_PIN_AVG
,
1312 .reg
= PMBUS_VIRT_READ_PIN_MAX
,
1314 .attr
= "input_highest",
1316 .reg
= PMBUS_VIRT_RESET_PIN_HISTORY
,
1317 .attr
= "reset_history",
1321 static const struct pmbus_limit_attr pout_limit_attrs
[] = {
1323 .reg
= PMBUS_POUT_MAX
,
1325 .alarm
= "cap_alarm",
1326 .sbit
= PB_POWER_LIMITING
,
1328 .reg
= PMBUS_POUT_OP_WARN_LIMIT
,
1330 .alarm
= "max_alarm",
1331 .sbit
= PB_POUT_OP_WARNING
,
1333 .reg
= PMBUS_POUT_OP_FAULT_LIMIT
,
1335 .alarm
= "crit_alarm",
1336 .sbit
= PB_POUT_OP_FAULT
,
1340 static const struct pmbus_sensor_attr power_attributes
[] = {
1342 .reg
= PMBUS_READ_PIN
,
1345 .func
= PMBUS_HAVE_PIN
,
1346 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1347 .sbase
= PB_STATUS_INPUT_BASE
,
1348 .limit
= pin_limit_attrs
,
1349 .nlimit
= ARRAY_SIZE(pin_limit_attrs
),
1351 .reg
= PMBUS_READ_POUT
,
1355 .func
= PMBUS_HAVE_POUT
,
1356 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1357 .sbase
= PB_STATUS_IOUT_BASE
,
1358 .limit
= pout_limit_attrs
,
1359 .nlimit
= ARRAY_SIZE(pout_limit_attrs
),
1363 /* Temperature atributes */
1365 static const struct pmbus_limit_attr temp_limit_attrs
[] = {
1367 .reg
= PMBUS_UT_WARN_LIMIT
,
1370 .alarm
= "min_alarm",
1371 .sbit
= PB_TEMP_UT_WARNING
,
1373 .reg
= PMBUS_UT_FAULT_LIMIT
,
1376 .alarm
= "lcrit_alarm",
1377 .sbit
= PB_TEMP_UT_FAULT
,
1379 .reg
= PMBUS_OT_WARN_LIMIT
,
1381 .alarm
= "max_alarm",
1382 .sbit
= PB_TEMP_OT_WARNING
,
1384 .reg
= PMBUS_OT_FAULT_LIMIT
,
1386 .alarm
= "crit_alarm",
1387 .sbit
= PB_TEMP_OT_FAULT
,
1389 .reg
= PMBUS_VIRT_READ_TEMP_MIN
,
1392 .reg
= PMBUS_VIRT_READ_TEMP_MAX
,
1395 .reg
= PMBUS_VIRT_RESET_TEMP_HISTORY
,
1396 .attr
= "reset_history",
1400 static const struct pmbus_limit_attr temp_limit_attrs2
[] = {
1402 .reg
= PMBUS_UT_WARN_LIMIT
,
1405 .alarm
= "min_alarm",
1406 .sbit
= PB_TEMP_UT_WARNING
,
1408 .reg
= PMBUS_UT_FAULT_LIMIT
,
1411 .alarm
= "lcrit_alarm",
1412 .sbit
= PB_TEMP_UT_FAULT
,
1414 .reg
= PMBUS_OT_WARN_LIMIT
,
1416 .alarm
= "max_alarm",
1417 .sbit
= PB_TEMP_OT_WARNING
,
1419 .reg
= PMBUS_OT_FAULT_LIMIT
,
1421 .alarm
= "crit_alarm",
1422 .sbit
= PB_TEMP_OT_FAULT
,
1424 .reg
= PMBUS_VIRT_READ_TEMP2_MIN
,
1427 .reg
= PMBUS_VIRT_READ_TEMP2_MAX
,
1430 .reg
= PMBUS_VIRT_RESET_TEMP2_HISTORY
,
1431 .attr
= "reset_history",
1435 static const struct pmbus_limit_attr temp_limit_attrs3
[] = {
1437 .reg
= PMBUS_UT_WARN_LIMIT
,
1440 .alarm
= "min_alarm",
1441 .sbit
= PB_TEMP_UT_WARNING
,
1443 .reg
= PMBUS_UT_FAULT_LIMIT
,
1446 .alarm
= "lcrit_alarm",
1447 .sbit
= PB_TEMP_UT_FAULT
,
1449 .reg
= PMBUS_OT_WARN_LIMIT
,
1451 .alarm
= "max_alarm",
1452 .sbit
= PB_TEMP_OT_WARNING
,
1454 .reg
= PMBUS_OT_FAULT_LIMIT
,
1456 .alarm
= "crit_alarm",
1457 .sbit
= PB_TEMP_OT_FAULT
,
1461 static const struct pmbus_sensor_attr temp_attributes
[] = {
1463 .reg
= PMBUS_READ_TEMPERATURE_1
,
1464 .class = PSC_TEMPERATURE
,
1468 .func
= PMBUS_HAVE_TEMP
,
1469 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1470 .sbase
= PB_STATUS_TEMP_BASE
,
1471 .gbit
= PB_STATUS_TEMPERATURE
,
1472 .limit
= temp_limit_attrs
,
1473 .nlimit
= ARRAY_SIZE(temp_limit_attrs
),
1475 .reg
= PMBUS_READ_TEMPERATURE_2
,
1476 .class = PSC_TEMPERATURE
,
1480 .func
= PMBUS_HAVE_TEMP2
,
1481 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1482 .sbase
= PB_STATUS_TEMP_BASE
,
1483 .gbit
= PB_STATUS_TEMPERATURE
,
1484 .limit
= temp_limit_attrs2
,
1485 .nlimit
= ARRAY_SIZE(temp_limit_attrs2
),
1487 .reg
= PMBUS_READ_TEMPERATURE_3
,
1488 .class = PSC_TEMPERATURE
,
1492 .func
= PMBUS_HAVE_TEMP3
,
1493 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1494 .sbase
= PB_STATUS_TEMP_BASE
,
1495 .gbit
= PB_STATUS_TEMPERATURE
,
1496 .limit
= temp_limit_attrs3
,
1497 .nlimit
= ARRAY_SIZE(temp_limit_attrs3
),
1501 static const int pmbus_fan_registers
[] = {
1502 PMBUS_READ_FAN_SPEED_1
,
1503 PMBUS_READ_FAN_SPEED_2
,
1504 PMBUS_READ_FAN_SPEED_3
,
1505 PMBUS_READ_FAN_SPEED_4
1508 static const int pmbus_fan_config_registers
[] = {
1509 PMBUS_FAN_CONFIG_12
,
1510 PMBUS_FAN_CONFIG_12
,
1511 PMBUS_FAN_CONFIG_34
,
1515 static const int pmbus_fan_status_registers
[] = {
1516 PMBUS_STATUS_FAN_12
,
1517 PMBUS_STATUS_FAN_12
,
1518 PMBUS_STATUS_FAN_34
,
1522 static const u32 pmbus_fan_flags
[] = {
1529 static const u32 pmbus_fan_status_flags
[] = {
1530 PMBUS_HAVE_STATUS_FAN12
,
1531 PMBUS_HAVE_STATUS_FAN12
,
1532 PMBUS_HAVE_STATUS_FAN34
,
1533 PMBUS_HAVE_STATUS_FAN34
1537 static void pmbus_add_fan_attributes(struct i2c_client
*client
,
1538 struct pmbus_data
*data
)
1540 const struct pmbus_driver_info
*info
= data
->info
;
1544 for (page
= 0; page
< info
->pages
; page
++) {
1547 for (f
= 0; f
< ARRAY_SIZE(pmbus_fan_registers
); f
++) {
1550 if (!(info
->func
[page
] & pmbus_fan_flags
[f
]))
1553 if (!pmbus_check_word_register(client
, page
,
1554 pmbus_fan_registers
[f
]))
1558 * Skip fan if not installed.
1559 * Each fan configuration register covers multiple fans,
1560 * so we have to do some magic.
1562 regval
= _pmbus_read_byte_data(client
, page
,
1563 pmbus_fan_config_registers
[f
]);
1565 (!(regval
& (PB_FAN_1_INSTALLED
>> ((f
& 1) * 4)))))
1568 pmbus_add_sensor(data
, "fan", "input", index
, page
,
1569 pmbus_fan_registers
[f
], PSC_FAN
, true,
1573 * Each fan status register covers multiple fans,
1574 * so we have to do some magic.
1576 if ((info
->func
[page
] & pmbus_fan_status_flags
[f
]) &&
1577 pmbus_check_byte_register(client
,
1578 page
, pmbus_fan_status_registers
[f
])) {
1581 if (f
> 1) /* fan 3, 4 */
1582 base
= PB_STATUS_FAN34_BASE
+ page
;
1584 base
= PB_STATUS_FAN_BASE
+ page
;
1585 pmbus_add_boolean_reg(data
, "fan", "alarm",
1587 PB_FAN_FAN1_WARNING
>> (f
& 1));
1588 pmbus_add_boolean_reg(data
, "fan", "fault",
1590 PB_FAN_FAN1_FAULT
>> (f
& 1));
1597 static void pmbus_find_attributes(struct i2c_client
*client
,
1598 struct pmbus_data
*data
)
1600 /* Voltage sensors */
1601 pmbus_add_sensor_attrs(client
, data
, "in", voltage_attributes
,
1602 ARRAY_SIZE(voltage_attributes
));
1604 /* Current sensors */
1605 pmbus_add_sensor_attrs(client
, data
, "curr", current_attributes
,
1606 ARRAY_SIZE(current_attributes
));
1609 pmbus_add_sensor_attrs(client
, data
, "power", power_attributes
,
1610 ARRAY_SIZE(power_attributes
));
1612 /* Temperature sensors */
1613 pmbus_add_sensor_attrs(client
, data
, "temp", temp_attributes
,
1614 ARRAY_SIZE(temp_attributes
));
1617 pmbus_add_fan_attributes(client
, data
);
1621 * Identify chip parameters.
1622 * This function is called for all chips.
1624 static int pmbus_identify_common(struct i2c_client
*client
,
1625 struct pmbus_data
*data
)
1629 if (pmbus_check_byte_register(client
, 0, PMBUS_VOUT_MODE
))
1630 vout_mode
= _pmbus_read_byte_data(client
, 0, PMBUS_VOUT_MODE
);
1631 if (vout_mode
>= 0 && vout_mode
!= 0xff) {
1633 * Not all chips support the VOUT_MODE command,
1634 * so a failure to read it is not an error.
1636 switch (vout_mode
>> 5) {
1637 case 0: /* linear mode */
1638 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != linear
)
1641 data
->exponent
= ((s8
)(vout_mode
<< 3)) >> 3;
1643 case 1: /* VID mode */
1644 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != vid
)
1647 case 2: /* direct mode */
1648 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != direct
)
1656 /* Determine maximum number of sensors, booleans, and labels */
1657 pmbus_find_max_attr(client
, data
);
1658 pmbus_clear_fault_page(client
, 0);
1662 int pmbus_do_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
,
1663 struct pmbus_driver_info
*info
)
1665 const struct pmbus_platform_data
*pdata
= client
->dev
.platform_data
;
1666 struct pmbus_data
*data
;
1670 dev_err(&client
->dev
, "Missing chip information");
1674 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WRITE_BYTE
1675 | I2C_FUNC_SMBUS_BYTE_DATA
1676 | I2C_FUNC_SMBUS_WORD_DATA
))
1679 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1681 dev_err(&client
->dev
, "No memory to allocate driver data\n");
1685 i2c_set_clientdata(client
, data
);
1686 mutex_init(&data
->update_lock
);
1688 /* Bail out if PMBus status register does not exist. */
1689 if (i2c_smbus_read_byte_data(client
, PMBUS_STATUS_BYTE
) < 0) {
1690 dev_err(&client
->dev
, "PMBus status register not found\n");
1696 data
->flags
= pdata
->flags
;
1699 pmbus_clear_faults(client
);
1701 if (info
->identify
) {
1702 ret
= (*info
->identify
)(client
, info
);
1704 dev_err(&client
->dev
, "Chip identification failed\n");
1709 if (info
->pages
<= 0 || info
->pages
> PMBUS_PAGES
) {
1710 dev_err(&client
->dev
, "Bad number of PMBus pages: %d\n",
1716 ret
= pmbus_identify_common(client
, data
);
1718 dev_err(&client
->dev
, "Failed to identify chip capabilities\n");
1723 data
->sensors
= kzalloc(sizeof(struct pmbus_sensor
) * data
->max_sensors
,
1725 if (!data
->sensors
) {
1726 dev_err(&client
->dev
, "No memory to allocate sensor data\n");
1730 data
->booleans
= kzalloc(sizeof(struct pmbus_boolean
)
1731 * data
->max_booleans
, GFP_KERNEL
);
1732 if (!data
->booleans
) {
1733 dev_err(&client
->dev
, "No memory to allocate boolean data\n");
1737 data
->labels
= kzalloc(sizeof(struct pmbus_label
) * data
->max_labels
,
1739 if (!data
->labels
) {
1740 dev_err(&client
->dev
, "No memory to allocate label data\n");
1744 data
->attributes
= kzalloc(sizeof(struct attribute
*)
1745 * data
->max_attributes
, GFP_KERNEL
);
1746 if (!data
->attributes
) {
1747 dev_err(&client
->dev
, "No memory to allocate attribute data\n");
1751 pmbus_find_attributes(client
, data
);
1754 * If there are no attributes, something is wrong.
1755 * Bail out instead of trying to register nothing.
1757 if (!data
->num_attributes
) {
1758 dev_err(&client
->dev
, "No attributes found\n");
1760 goto out_attributes
;
1763 /* Register sysfs hooks */
1764 data
->group
.attrs
= data
->attributes
;
1765 ret
= sysfs_create_group(&client
->dev
.kobj
, &data
->group
);
1767 dev_err(&client
->dev
, "Failed to create sysfs entries\n");
1768 goto out_attributes
;
1770 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
1771 if (IS_ERR(data
->hwmon_dev
)) {
1772 ret
= PTR_ERR(data
->hwmon_dev
);
1773 dev_err(&client
->dev
, "Failed to register hwmon device\n");
1774 goto out_hwmon_device_register
;
1778 out_hwmon_device_register
:
1779 sysfs_remove_group(&client
->dev
.kobj
, &data
->group
);
1781 kfree(data
->attributes
);
1783 kfree(data
->labels
);
1785 kfree(data
->booleans
);
1787 kfree(data
->sensors
);
1792 EXPORT_SYMBOL_GPL(pmbus_do_probe
);
1794 void pmbus_do_remove(struct i2c_client
*client
)
1796 struct pmbus_data
*data
= i2c_get_clientdata(client
);
1797 hwmon_device_unregister(data
->hwmon_dev
);
1798 sysfs_remove_group(&client
->dev
.kobj
, &data
->group
);
1799 kfree(data
->attributes
);
1800 kfree(data
->labels
);
1801 kfree(data
->booleans
);
1802 kfree(data
->sensors
);
1805 EXPORT_SYMBOL_GPL(pmbus_do_remove
);
1807 MODULE_AUTHOR("Guenter Roeck");
1808 MODULE_DESCRIPTION("PMBus core driver");
1809 MODULE_LICENSE("GPL");