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/jiffies.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 7 /* input, cap, max, crit,
46 #define PMBUS_MAX_SENSORS_PER_FAN 1 /* input */
47 #define PMBUS_MAX_SENSORS_PER_TEMP 9 /* input, min, max, lcrit,
48 * crit, lowest, highest, avg,
52 #define PMBUS_MAX_INPUT_BOOLEANS 7 /* v: min_alarm, max_alarm,
53 lcrit_alarm, crit_alarm;
56 #define PMBUS_VOUT_BOOLEANS_PER_PAGE 4 /* min_alarm, max_alarm,
57 lcrit_alarm, crit_alarm */
58 #define PMBUS_IOUT_BOOLEANS_PER_PAGE 3 /* alarm, lcrit_alarm,
60 #define PMBUS_POUT_BOOLEANS_PER_PAGE 3 /* cap_alarm, alarm, crit_alarm
62 #define PMBUS_MAX_BOOLEANS_PER_FAN 2 /* alarm, fault */
63 #define PMBUS_MAX_BOOLEANS_PER_TEMP 4 /* min_alarm, max_alarm,
64 lcrit_alarm, crit_alarm */
66 #define PMBUS_MAX_INPUT_LABELS 4 /* vin, vcap, iin, pin */
69 * status, status_vout, status_iout, status_fans, status_fan34, and status_temp
70 * are paged. status_input is unpaged.
72 #define PB_NUM_STATUS_REG (PMBUS_PAGES * 6 + 1)
75 * Index into status register array, per status register group
77 #define PB_STATUS_BASE 0
78 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
79 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
80 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
81 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
82 #define PB_STATUS_INPUT_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
83 #define PB_STATUS_TEMP_BASE (PB_STATUS_INPUT_BASE + 1)
85 #define PMBUS_NAME_SIZE 24
88 char name
[PMBUS_NAME_SIZE
]; /* sysfs sensor name */
89 struct sensor_device_attribute attribute
;
90 u8 page
; /* page number */
91 u16 reg
; /* register */
92 enum pmbus_sensor_classes
class; /* sensor class */
93 bool update
; /* runtime sensor update needed */
94 int data
; /* Sensor data.
95 Negative if there was a read error */
98 struct pmbus_boolean
{
99 char name
[PMBUS_NAME_SIZE
]; /* sysfs boolean name */
100 struct sensor_device_attribute attribute
;
104 char name
[PMBUS_NAME_SIZE
]; /* sysfs label name */
105 struct sensor_device_attribute attribute
;
106 char label
[PMBUS_NAME_SIZE
]; /* label */
110 struct device
*hwmon_dev
;
112 u32 flags
; /* from platform data */
114 int exponent
; /* linear mode: exponent for output voltages */
116 const struct pmbus_driver_info
*info
;
120 struct attribute
**attributes
;
121 struct attribute_group group
;
124 * Sensors cover both sensor and limit registers.
128 struct pmbus_sensor
*sensors
;
130 * Booleans are used for alarms.
131 * Values are determined from status registers.
135 struct pmbus_boolean
*booleans
;
137 * Labels are used to map generic names (e.g., "in1")
138 * to PMBus specific names (e.g., "vin" or "vout1").
142 struct pmbus_label
*labels
;
144 struct mutex update_lock
;
146 unsigned long last_updated
; /* in jiffies */
149 * A single status register covers multiple attributes,
150 * so we keep them all together.
152 u8 status
[PB_NUM_STATUS_REG
];
157 int pmbus_set_page(struct i2c_client
*client
, u8 page
)
159 struct pmbus_data
*data
= i2c_get_clientdata(client
);
163 if (page
!= data
->currpage
) {
164 rv
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, page
);
165 newpage
= i2c_smbus_read_byte_data(client
, PMBUS_PAGE
);
169 data
->currpage
= page
;
173 EXPORT_SYMBOL_GPL(pmbus_set_page
);
175 int pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
180 rv
= pmbus_set_page(client
, page
);
185 return i2c_smbus_write_byte(client
, value
);
187 EXPORT_SYMBOL_GPL(pmbus_write_byte
);
190 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
191 * a device specific mapping funcion exists and calls it if necessary.
193 static int _pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
195 struct pmbus_data
*data
= i2c_get_clientdata(client
);
196 const struct pmbus_driver_info
*info
= data
->info
;
199 if (info
->write_byte
) {
200 status
= info
->write_byte(client
, page
, value
);
201 if (status
!= -ENODATA
)
204 return pmbus_write_byte(client
, page
, value
);
207 int pmbus_write_word_data(struct i2c_client
*client
, u8 page
, u8 reg
, u16 word
)
211 rv
= pmbus_set_page(client
, page
);
215 return i2c_smbus_write_word_data(client
, reg
, word
);
217 EXPORT_SYMBOL_GPL(pmbus_write_word_data
);
220 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
221 * a device specific mapping function exists and calls it if necessary.
223 static int _pmbus_write_word_data(struct i2c_client
*client
, int page
, int reg
,
226 struct pmbus_data
*data
= i2c_get_clientdata(client
);
227 const struct pmbus_driver_info
*info
= data
->info
;
230 if (info
->write_word_data
) {
231 status
= info
->write_word_data(client
, page
, reg
, word
);
232 if (status
!= -ENODATA
)
235 if (reg
>= PMBUS_VIRT_BASE
)
237 return pmbus_write_word_data(client
, page
, reg
, word
);
240 int pmbus_read_word_data(struct i2c_client
*client
, u8 page
, u8 reg
)
244 rv
= pmbus_set_page(client
, page
);
248 return i2c_smbus_read_word_data(client
, reg
);
250 EXPORT_SYMBOL_GPL(pmbus_read_word_data
);
253 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
254 * a device specific mapping function exists and calls it if necessary.
256 static int _pmbus_read_word_data(struct i2c_client
*client
, int page
, int reg
)
258 struct pmbus_data
*data
= i2c_get_clientdata(client
);
259 const struct pmbus_driver_info
*info
= data
->info
;
262 if (info
->read_word_data
) {
263 status
= info
->read_word_data(client
, page
, reg
);
264 if (status
!= -ENODATA
)
267 if (reg
>= PMBUS_VIRT_BASE
)
269 return pmbus_read_word_data(client
, page
, reg
);
272 int pmbus_read_byte_data(struct i2c_client
*client
, int page
, u8 reg
)
277 rv
= pmbus_set_page(client
, page
);
282 return i2c_smbus_read_byte_data(client
, reg
);
284 EXPORT_SYMBOL_GPL(pmbus_read_byte_data
);
287 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
288 * a device specific mapping function exists and calls it if necessary.
290 static int _pmbus_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
292 struct pmbus_data
*data
= i2c_get_clientdata(client
);
293 const struct pmbus_driver_info
*info
= data
->info
;
296 if (info
->read_byte_data
) {
297 status
= info
->read_byte_data(client
, page
, reg
);
298 if (status
!= -ENODATA
)
301 return pmbus_read_byte_data(client
, page
, reg
);
304 static void pmbus_clear_fault_page(struct i2c_client
*client
, int page
)
306 _pmbus_write_byte(client
, page
, PMBUS_CLEAR_FAULTS
);
309 void pmbus_clear_faults(struct i2c_client
*client
)
311 struct pmbus_data
*data
= i2c_get_clientdata(client
);
314 for (i
= 0; i
< data
->info
->pages
; i
++)
315 pmbus_clear_fault_page(client
, i
);
317 EXPORT_SYMBOL_GPL(pmbus_clear_faults
);
319 static int pmbus_check_status_cml(struct i2c_client
*client
)
323 status
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_BYTE
);
324 if (status
< 0 || (status
& PB_STATUS_CML
)) {
325 status2
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
326 if (status2
< 0 || (status2
& PB_CML_FAULT_INVALID_COMMAND
))
332 bool pmbus_check_byte_register(struct i2c_client
*client
, int page
, int reg
)
335 struct pmbus_data
*data
= i2c_get_clientdata(client
);
337 rv
= _pmbus_read_byte_data(client
, page
, reg
);
338 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
339 rv
= pmbus_check_status_cml(client
);
340 pmbus_clear_fault_page(client
, -1);
343 EXPORT_SYMBOL_GPL(pmbus_check_byte_register
);
345 bool pmbus_check_word_register(struct i2c_client
*client
, int page
, int reg
)
348 struct pmbus_data
*data
= i2c_get_clientdata(client
);
350 rv
= _pmbus_read_word_data(client
, page
, reg
);
351 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
352 rv
= pmbus_check_status_cml(client
);
353 pmbus_clear_fault_page(client
, -1);
356 EXPORT_SYMBOL_GPL(pmbus_check_word_register
);
358 const struct pmbus_driver_info
*pmbus_get_driver_info(struct i2c_client
*client
)
360 struct pmbus_data
*data
= i2c_get_clientdata(client
);
364 EXPORT_SYMBOL_GPL(pmbus_get_driver_info
);
366 static struct pmbus_data
*pmbus_update_device(struct device
*dev
)
368 struct i2c_client
*client
= to_i2c_client(dev
);
369 struct pmbus_data
*data
= i2c_get_clientdata(client
);
370 const struct pmbus_driver_info
*info
= data
->info
;
372 mutex_lock(&data
->update_lock
);
373 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
376 for (i
= 0; i
< info
->pages
; i
++)
377 data
->status
[PB_STATUS_BASE
+ i
]
378 = _pmbus_read_byte_data(client
, i
,
380 for (i
= 0; i
< info
->pages
; i
++) {
381 if (!(info
->func
[i
] & PMBUS_HAVE_STATUS_VOUT
))
383 data
->status
[PB_STATUS_VOUT_BASE
+ i
]
384 = _pmbus_read_byte_data(client
, i
, PMBUS_STATUS_VOUT
);
386 for (i
= 0; i
< info
->pages
; i
++) {
387 if (!(info
->func
[i
] & PMBUS_HAVE_STATUS_IOUT
))
389 data
->status
[PB_STATUS_IOUT_BASE
+ i
]
390 = _pmbus_read_byte_data(client
, i
, PMBUS_STATUS_IOUT
);
392 for (i
= 0; i
< info
->pages
; i
++) {
393 if (!(info
->func
[i
] & PMBUS_HAVE_STATUS_TEMP
))
395 data
->status
[PB_STATUS_TEMP_BASE
+ i
]
396 = _pmbus_read_byte_data(client
, i
,
397 PMBUS_STATUS_TEMPERATURE
);
399 for (i
= 0; i
< info
->pages
; i
++) {
400 if (!(info
->func
[i
] & PMBUS_HAVE_STATUS_FAN12
))
402 data
->status
[PB_STATUS_FAN_BASE
+ i
]
403 = _pmbus_read_byte_data(client
, i
,
404 PMBUS_STATUS_FAN_12
);
407 for (i
= 0; i
< info
->pages
; i
++) {
408 if (!(info
->func
[i
] & PMBUS_HAVE_STATUS_FAN34
))
410 data
->status
[PB_STATUS_FAN34_BASE
+ i
]
411 = _pmbus_read_byte_data(client
, i
,
412 PMBUS_STATUS_FAN_34
);
415 if (info
->func
[0] & PMBUS_HAVE_STATUS_INPUT
)
416 data
->status
[PB_STATUS_INPUT_BASE
]
417 = _pmbus_read_byte_data(client
, 0,
420 for (i
= 0; i
< data
->num_sensors
; i
++) {
421 struct pmbus_sensor
*sensor
= &data
->sensors
[i
];
423 if (!data
->valid
|| sensor
->update
)
425 = _pmbus_read_word_data(client
,
429 pmbus_clear_faults(client
);
430 data
->last_updated
= jiffies
;
433 mutex_unlock(&data
->update_lock
);
438 * Convert linear sensor values to milli- or micro-units
439 * depending on sensor type.
441 static long pmbus_reg2data_linear(struct pmbus_data
*data
,
442 struct pmbus_sensor
*sensor
)
448 if (sensor
->class == PSC_VOLTAGE_OUT
) { /* LINEAR16 */
449 exponent
= data
->exponent
;
450 mantissa
= (u16
) sensor
->data
;
451 } else { /* LINEAR11 */
452 exponent
= ((s16
)sensor
->data
) >> 11;
453 mantissa
= ((s16
)((sensor
->data
& 0x7ff) << 5)) >> 5;
458 /* scale result to milli-units for all sensors except fans */
459 if (sensor
->class != PSC_FAN
)
462 /* scale result to micro-units for power sensors */
463 if (sensor
->class == PSC_POWER
)
475 * Convert direct sensor values to milli- or micro-units
476 * depending on sensor type.
478 static long pmbus_reg2data_direct(struct pmbus_data
*data
,
479 struct pmbus_sensor
*sensor
)
481 long val
= (s16
) sensor
->data
;
484 m
= data
->info
->m
[sensor
->class];
485 b
= data
->info
->b
[sensor
->class];
486 R
= data
->info
->R
[sensor
->class];
491 /* X = 1/m * (Y * 10^-R - b) */
493 /* scale result to milli-units for everything but fans */
494 if (sensor
->class != PSC_FAN
) {
499 /* scale result to micro-units for power sensors */
500 if (sensor
->class == PSC_POWER
) {
510 val
= DIV_ROUND_CLOSEST(val
, 10);
514 return (val
- b
) / m
;
518 * Convert VID sensor values to milli- or micro-units
519 * depending on sensor type.
520 * We currently only support VR11.
522 static long pmbus_reg2data_vid(struct pmbus_data
*data
,
523 struct pmbus_sensor
*sensor
)
525 long val
= sensor
->data
;
527 if (val
< 0x02 || val
> 0xb2)
529 return DIV_ROUND_CLOSEST(160000 - (val
- 2) * 625, 100);
532 static long pmbus_reg2data(struct pmbus_data
*data
, struct pmbus_sensor
*sensor
)
536 switch (data
->info
->format
[sensor
->class]) {
538 val
= pmbus_reg2data_direct(data
, sensor
);
541 val
= pmbus_reg2data_vid(data
, sensor
);
545 val
= pmbus_reg2data_linear(data
, sensor
);
551 #define MAX_MANTISSA (1023 * 1000)
552 #define MIN_MANTISSA (511 * 1000)
554 static u16
pmbus_data2reg_linear(struct pmbus_data
*data
,
555 enum pmbus_sensor_classes
class, long val
)
557 s16 exponent
= 0, mantissa
;
558 bool negative
= false;
564 if (class == PSC_VOLTAGE_OUT
) {
565 /* LINEAR16 does not support negative voltages */
570 * For a static exponents, we don't have a choice
571 * but to adjust the value to it.
573 if (data
->exponent
< 0)
574 val
<<= -data
->exponent
;
576 val
>>= data
->exponent
;
577 val
= DIV_ROUND_CLOSEST(val
, 1000);
586 /* Power is in uW. Convert to mW before converting. */
587 if (class == PSC_POWER
)
588 val
= DIV_ROUND_CLOSEST(val
, 1000L);
591 * For simplicity, convert fan data to milli-units
592 * before calculating the exponent.
594 if (class == PSC_FAN
)
597 /* Reduce large mantissa until it fits into 10 bit */
598 while (val
>= MAX_MANTISSA
&& exponent
< 15) {
602 /* Increase small mantissa to improve precision */
603 while (val
< MIN_MANTISSA
&& exponent
> -15) {
608 /* Convert mantissa from milli-units to units */
609 mantissa
= DIV_ROUND_CLOSEST(val
, 1000);
611 /* Ensure that resulting number is within range */
612 if (mantissa
> 0x3ff)
617 mantissa
= -mantissa
;
619 /* Convert to 5 bit exponent, 11 bit mantissa */
620 return (mantissa
& 0x7ff) | ((exponent
<< 11) & 0xf800);
623 static u16
pmbus_data2reg_direct(struct pmbus_data
*data
,
624 enum pmbus_sensor_classes
class, long val
)
628 m
= data
->info
->m
[class];
629 b
= data
->info
->b
[class];
630 R
= data
->info
->R
[class];
632 /* Power is in uW. Adjust R and b. */
633 if (class == PSC_POWER
) {
638 /* Calculate Y = (m * X + b) * 10^R */
639 if (class != PSC_FAN
) {
640 R
-= 3; /* Adjust R and b for data in milli-units */
650 val
= DIV_ROUND_CLOSEST(val
, 10);
657 static u16
pmbus_data2reg_vid(struct pmbus_data
*data
,
658 enum pmbus_sensor_classes
class, long val
)
660 val
= SENSORS_LIMIT(val
, 500, 1600);
662 return 2 + DIV_ROUND_CLOSEST((1600 - val
) * 100, 625);
665 static u16
pmbus_data2reg(struct pmbus_data
*data
,
666 enum pmbus_sensor_classes
class, long val
)
670 switch (data
->info
->format
[class]) {
672 regval
= pmbus_data2reg_direct(data
, class, val
);
675 regval
= pmbus_data2reg_vid(data
, class, val
);
679 regval
= pmbus_data2reg_linear(data
, class, val
);
686 * Return boolean calculated from converted data.
687 * <index> defines a status register index and mask, and optionally
688 * two sensor indexes.
689 * The upper half-word references the two sensors,
690 * two sensor indices.
691 * The upper half-word references the two optional sensors,
692 * the lower half word references status register and mask.
693 * The function returns true if (status[reg] & mask) is true and,
694 * if specified, if v1 >= v2.
695 * To determine if an object exceeds upper limits, specify <v, limit>.
696 * To determine if an object exceeds lower limits, specify <limit, v>.
698 * For booleans created with pmbus_add_boolean_reg(), only the lower 16 bits of
699 * index are set. s1 and s2 (the sensor index values) are zero in this case.
700 * The function returns true if (status[reg] & mask) is true.
702 * If the boolean was created with pmbus_add_boolean_cmp(), a comparison against
703 * a specified limit has to be performed to determine the boolean result.
704 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
705 * sensor values referenced by sensor indices s1 and s2).
707 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
708 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
710 * If a negative value is stored in any of the referenced registers, this value
711 * reflects an error code which will be returned.
713 static int pmbus_get_boolean(struct pmbus_data
*data
, int index
)
715 u8 s1
= (index
>> 24) & 0xff;
716 u8 s2
= (index
>> 16) & 0xff;
717 u8 reg
= (index
>> 8) & 0xff;
718 u8 mask
= index
& 0xff;
722 status
= data
->status
[reg
];
726 regval
= status
& mask
;
731 struct pmbus_sensor
*sensor1
, *sensor2
;
733 sensor1
= &data
->sensors
[s1
];
734 if (sensor1
->data
< 0)
735 return sensor1
->data
;
736 sensor2
= &data
->sensors
[s2
];
737 if (sensor2
->data
< 0)
738 return sensor2
->data
;
740 v1
= pmbus_reg2data(data
, sensor1
);
741 v2
= pmbus_reg2data(data
, sensor2
);
742 ret
= !!(regval
&& v1
>= v2
);
747 static ssize_t
pmbus_show_boolean(struct device
*dev
,
748 struct device_attribute
*da
, char *buf
)
750 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
751 struct pmbus_data
*data
= pmbus_update_device(dev
);
754 val
= pmbus_get_boolean(data
, attr
->index
);
757 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
760 static ssize_t
pmbus_show_sensor(struct device
*dev
,
761 struct device_attribute
*da
, char *buf
)
763 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
764 struct pmbus_data
*data
= pmbus_update_device(dev
);
765 struct pmbus_sensor
*sensor
;
767 sensor
= &data
->sensors
[attr
->index
];
768 if (sensor
->data
< 0)
771 return snprintf(buf
, PAGE_SIZE
, "%ld\n", pmbus_reg2data(data
, sensor
));
774 static ssize_t
pmbus_set_sensor(struct device
*dev
,
775 struct device_attribute
*devattr
,
776 const char *buf
, size_t count
)
778 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
779 struct i2c_client
*client
= to_i2c_client(dev
);
780 struct pmbus_data
*data
= i2c_get_clientdata(client
);
781 struct pmbus_sensor
*sensor
= &data
->sensors
[attr
->index
];
787 if (kstrtol(buf
, 10, &val
) < 0)
790 mutex_lock(&data
->update_lock
);
791 regval
= pmbus_data2reg(data
, sensor
->class, val
);
792 ret
= _pmbus_write_word_data(client
, sensor
->page
, sensor
->reg
, regval
);
796 data
->sensors
[attr
->index
].data
= regval
;
797 mutex_unlock(&data
->update_lock
);
801 static ssize_t
pmbus_show_label(struct device
*dev
,
802 struct device_attribute
*da
, char *buf
)
804 struct i2c_client
*client
= to_i2c_client(dev
);
805 struct pmbus_data
*data
= i2c_get_clientdata(client
);
806 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
808 return snprintf(buf
, PAGE_SIZE
, "%s\n",
809 data
->labels
[attr
->index
].label
);
812 #define PMBUS_ADD_ATTR(data, _name, _idx, _mode, _type, _show, _set) \
814 struct sensor_device_attribute *a \
815 = &data->_type##s[data->num_##_type##s].attribute; \
816 BUG_ON(data->num_attributes >= data->max_attributes); \
817 sysfs_attr_init(&a->dev_attr.attr); \
818 a->dev_attr.attr.name = _name; \
819 a->dev_attr.attr.mode = _mode; \
820 a->dev_attr.show = _show; \
821 a->dev_attr.store = _set; \
823 data->attributes[data->num_attributes] = &a->dev_attr.attr; \
824 data->num_attributes++; \
827 #define PMBUS_ADD_GET_ATTR(data, _name, _type, _idx) \
828 PMBUS_ADD_ATTR(data, _name, _idx, S_IRUGO, _type, \
829 pmbus_show_##_type, NULL)
831 #define PMBUS_ADD_SET_ATTR(data, _name, _type, _idx) \
832 PMBUS_ADD_ATTR(data, _name, _idx, S_IWUSR | S_IRUGO, _type, \
833 pmbus_show_##_type, pmbus_set_##_type)
835 static void pmbus_add_boolean(struct pmbus_data
*data
,
836 const char *name
, const char *type
, int seq
,
839 struct pmbus_boolean
*boolean
;
841 BUG_ON(data
->num_booleans
>= data
->max_booleans
);
843 boolean
= &data
->booleans
[data
->num_booleans
];
845 snprintf(boolean
->name
, sizeof(boolean
->name
), "%s%d_%s",
847 PMBUS_ADD_GET_ATTR(data
, boolean
->name
, boolean
, idx
);
848 data
->num_booleans
++;
851 static void pmbus_add_boolean_reg(struct pmbus_data
*data
,
852 const char *name
, const char *type
,
853 int seq
, int reg
, int bit
)
855 pmbus_add_boolean(data
, name
, type
, seq
, (reg
<< 8) | bit
);
858 static void pmbus_add_boolean_cmp(struct pmbus_data
*data
,
859 const char *name
, const char *type
,
860 int seq
, int i1
, int i2
, int reg
, int mask
)
862 pmbus_add_boolean(data
, name
, type
, seq
,
863 (i1
<< 24) | (i2
<< 16) | (reg
<< 8) | mask
);
866 static void pmbus_add_sensor(struct pmbus_data
*data
,
867 const char *name
, const char *type
, int seq
,
868 int page
, int reg
, enum pmbus_sensor_classes
class,
869 bool update
, bool readonly
)
871 struct pmbus_sensor
*sensor
;
873 BUG_ON(data
->num_sensors
>= data
->max_sensors
);
875 sensor
= &data
->sensors
[data
->num_sensors
];
876 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d_%s",
880 sensor
->class = class;
881 sensor
->update
= update
;
883 PMBUS_ADD_GET_ATTR(data
, sensor
->name
, sensor
,
886 PMBUS_ADD_SET_ATTR(data
, sensor
->name
, sensor
,
891 static void pmbus_add_label(struct pmbus_data
*data
,
892 const char *name
, int seq
,
893 const char *lstring
, int index
)
895 struct pmbus_label
*label
;
897 BUG_ON(data
->num_labels
>= data
->max_labels
);
899 label
= &data
->labels
[data
->num_labels
];
900 snprintf(label
->name
, sizeof(label
->name
), "%s%d_label", name
, seq
);
902 strncpy(label
->label
, lstring
, sizeof(label
->label
) - 1);
904 snprintf(label
->label
, sizeof(label
->label
), "%s%d", lstring
,
907 PMBUS_ADD_GET_ATTR(data
, label
->name
, label
, data
->num_labels
);
912 * Determine maximum number of sensors, booleans, and labels.
913 * To keep things simple, only make a rough high estimate.
915 static void pmbus_find_max_attr(struct i2c_client
*client
,
916 struct pmbus_data
*data
)
918 const struct pmbus_driver_info
*info
= data
->info
;
919 int page
, max_sensors
, max_booleans
, max_labels
;
921 max_sensors
= PMBUS_MAX_INPUT_SENSORS
;
922 max_booleans
= PMBUS_MAX_INPUT_BOOLEANS
;
923 max_labels
= PMBUS_MAX_INPUT_LABELS
;
925 for (page
= 0; page
< info
->pages
; page
++) {
926 if (info
->func
[page
] & PMBUS_HAVE_VOUT
) {
927 max_sensors
+= PMBUS_VOUT_SENSORS_PER_PAGE
;
928 max_booleans
+= PMBUS_VOUT_BOOLEANS_PER_PAGE
;
931 if (info
->func
[page
] & PMBUS_HAVE_IOUT
) {
932 max_sensors
+= PMBUS_IOUT_SENSORS_PER_PAGE
;
933 max_booleans
+= PMBUS_IOUT_BOOLEANS_PER_PAGE
;
936 if (info
->func
[page
] & PMBUS_HAVE_POUT
) {
937 max_sensors
+= PMBUS_POUT_SENSORS_PER_PAGE
;
938 max_booleans
+= PMBUS_POUT_BOOLEANS_PER_PAGE
;
941 if (info
->func
[page
] & PMBUS_HAVE_FAN12
) {
942 max_sensors
+= 2 * PMBUS_MAX_SENSORS_PER_FAN
;
943 max_booleans
+= 2 * PMBUS_MAX_BOOLEANS_PER_FAN
;
945 if (info
->func
[page
] & PMBUS_HAVE_FAN34
) {
946 max_sensors
+= 2 * PMBUS_MAX_SENSORS_PER_FAN
;
947 max_booleans
+= 2 * PMBUS_MAX_BOOLEANS_PER_FAN
;
949 if (info
->func
[page
] & PMBUS_HAVE_TEMP
) {
950 max_sensors
+= PMBUS_MAX_SENSORS_PER_TEMP
;
951 max_booleans
+= PMBUS_MAX_BOOLEANS_PER_TEMP
;
953 if (info
->func
[page
] & PMBUS_HAVE_TEMP2
) {
954 max_sensors
+= PMBUS_MAX_SENSORS_PER_TEMP
;
955 max_booleans
+= PMBUS_MAX_BOOLEANS_PER_TEMP
;
957 if (info
->func
[page
] & PMBUS_HAVE_TEMP3
) {
958 max_sensors
+= PMBUS_MAX_SENSORS_PER_TEMP
;
959 max_booleans
+= PMBUS_MAX_BOOLEANS_PER_TEMP
;
962 data
->max_sensors
= max_sensors
;
963 data
->max_booleans
= max_booleans
;
964 data
->max_labels
= max_labels
;
965 data
->max_attributes
= max_sensors
+ max_booleans
+ max_labels
;
969 * Search for attributes. Allocate sensors, booleans, and labels as needed.
973 * The pmbus_limit_attr structure describes a single limit attribute
974 * and its associated alarm attribute.
976 struct pmbus_limit_attr
{
977 u16 reg
; /* Limit register */
978 bool update
; /* True if register needs updates */
979 bool low
; /* True if low limit; for limits with compare
981 const char *attr
; /* Attribute name */
982 const char *alarm
; /* Alarm attribute name */
983 u32 sbit
; /* Alarm attribute status bit */
987 * The pmbus_sensor_attr structure describes one sensor attribute. This
988 * description includes a reference to the associated limit attributes.
990 struct pmbus_sensor_attr
{
991 u8 reg
; /* sensor register */
992 enum pmbus_sensor_classes
class;/* sensor class */
993 const char *label
; /* sensor label */
994 bool paged
; /* true if paged sensor */
995 bool update
; /* true if update needed */
996 bool compare
; /* true if compare function needed */
997 u32 func
; /* sensor mask */
998 u32 sfunc
; /* sensor status mask */
999 int sbase
; /* status base register */
1000 u32 gbit
; /* generic status bit */
1001 const struct pmbus_limit_attr
*limit
;/* limit registers */
1002 int nlimit
; /* # of limit registers */
1006 * Add a set of limit attributes and, if supported, the associated
1009 static bool pmbus_add_limit_attrs(struct i2c_client
*client
,
1010 struct pmbus_data
*data
,
1011 const struct pmbus_driver_info
*info
,
1012 const char *name
, int index
, int page
,
1014 const struct pmbus_sensor_attr
*attr
)
1016 const struct pmbus_limit_attr
*l
= attr
->limit
;
1017 int nlimit
= attr
->nlimit
;
1018 bool have_alarm
= false;
1021 for (i
= 0; i
< nlimit
; i
++) {
1022 if (pmbus_check_word_register(client
, page
, l
->reg
)) {
1023 cindex
= data
->num_sensors
;
1024 pmbus_add_sensor(data
, name
, l
->attr
, index
, page
,
1025 l
->reg
, attr
->class,
1026 attr
->update
|| l
->update
,
1028 if (l
->sbit
&& (info
->func
[page
] & attr
->sfunc
)) {
1029 if (attr
->compare
) {
1030 pmbus_add_boolean_cmp(data
, name
,
1032 l
->low
? cindex
: cbase
,
1033 l
->low
? cbase
: cindex
,
1034 attr
->sbase
+ page
, l
->sbit
);
1036 pmbus_add_boolean_reg(data
, name
,
1038 attr
->sbase
+ page
, l
->sbit
);
1048 static void pmbus_add_sensor_attrs_one(struct i2c_client
*client
,
1049 struct pmbus_data
*data
,
1050 const struct pmbus_driver_info
*info
,
1052 int index
, int page
,
1053 const struct pmbus_sensor_attr
*attr
)
1056 int cbase
= data
->num_sensors
;
1059 pmbus_add_label(data
, name
, index
, attr
->label
,
1060 attr
->paged
? page
+ 1 : 0);
1061 pmbus_add_sensor(data
, name
, "input", index
, page
, attr
->reg
,
1062 attr
->class, true, true);
1064 have_alarm
= pmbus_add_limit_attrs(client
, data
, info
, name
,
1065 index
, page
, cbase
, attr
);
1067 * Add generic alarm attribute only if there are no individual
1068 * alarm attributes, if there is a global alarm bit, and if
1069 * the generic status register for this page is accessible.
1071 if (!have_alarm
&& attr
->gbit
&&
1072 pmbus_check_byte_register(client
, page
, PMBUS_STATUS_BYTE
))
1073 pmbus_add_boolean_reg(data
, name
, "alarm", index
,
1074 PB_STATUS_BASE
+ page
,
1079 static void pmbus_add_sensor_attrs(struct i2c_client
*client
,
1080 struct pmbus_data
*data
,
1082 const struct pmbus_sensor_attr
*attrs
,
1085 const struct pmbus_driver_info
*info
= data
->info
;
1089 for (i
= 0; i
< nattrs
; i
++) {
1092 pages
= attrs
->paged
? info
->pages
: 1;
1093 for (page
= 0; page
< pages
; page
++) {
1094 if (!(info
->func
[page
] & attrs
->func
))
1096 pmbus_add_sensor_attrs_one(client
, data
, info
, name
,
1097 index
, page
, attrs
);
1104 static const struct pmbus_limit_attr vin_limit_attrs
[] = {
1106 .reg
= PMBUS_VIN_UV_WARN_LIMIT
,
1108 .alarm
= "min_alarm",
1109 .sbit
= PB_VOLTAGE_UV_WARNING
,
1111 .reg
= PMBUS_VIN_UV_FAULT_LIMIT
,
1113 .alarm
= "lcrit_alarm",
1114 .sbit
= PB_VOLTAGE_UV_FAULT
,
1116 .reg
= PMBUS_VIN_OV_WARN_LIMIT
,
1118 .alarm
= "max_alarm",
1119 .sbit
= PB_VOLTAGE_OV_WARNING
,
1121 .reg
= PMBUS_VIN_OV_FAULT_LIMIT
,
1123 .alarm
= "crit_alarm",
1124 .sbit
= PB_VOLTAGE_OV_FAULT
,
1126 .reg
= PMBUS_VIRT_READ_VIN_AVG
,
1130 .reg
= PMBUS_VIRT_READ_VIN_MIN
,
1134 .reg
= PMBUS_VIRT_READ_VIN_MAX
,
1138 .reg
= PMBUS_VIRT_RESET_VIN_HISTORY
,
1139 .attr
= "reset_history",
1143 static const struct pmbus_limit_attr vout_limit_attrs
[] = {
1145 .reg
= PMBUS_VOUT_UV_WARN_LIMIT
,
1147 .alarm
= "min_alarm",
1148 .sbit
= PB_VOLTAGE_UV_WARNING
,
1150 .reg
= PMBUS_VOUT_UV_FAULT_LIMIT
,
1152 .alarm
= "lcrit_alarm",
1153 .sbit
= PB_VOLTAGE_UV_FAULT
,
1155 .reg
= PMBUS_VOUT_OV_WARN_LIMIT
,
1157 .alarm
= "max_alarm",
1158 .sbit
= PB_VOLTAGE_OV_WARNING
,
1160 .reg
= PMBUS_VOUT_OV_FAULT_LIMIT
,
1162 .alarm
= "crit_alarm",
1163 .sbit
= PB_VOLTAGE_OV_FAULT
,
1165 .reg
= PMBUS_VIRT_READ_VOUT_AVG
,
1169 .reg
= PMBUS_VIRT_READ_VOUT_MIN
,
1173 .reg
= PMBUS_VIRT_READ_VOUT_MAX
,
1177 .reg
= PMBUS_VIRT_RESET_VOUT_HISTORY
,
1178 .attr
= "reset_history",
1182 static const struct pmbus_sensor_attr voltage_attributes
[] = {
1184 .reg
= PMBUS_READ_VIN
,
1185 .class = PSC_VOLTAGE_IN
,
1187 .func
= PMBUS_HAVE_VIN
,
1188 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1189 .sbase
= PB_STATUS_INPUT_BASE
,
1190 .gbit
= PB_STATUS_VIN_UV
,
1191 .limit
= vin_limit_attrs
,
1192 .nlimit
= ARRAY_SIZE(vin_limit_attrs
),
1194 .reg
= PMBUS_READ_VCAP
,
1195 .class = PSC_VOLTAGE_IN
,
1197 .func
= PMBUS_HAVE_VCAP
,
1199 .reg
= PMBUS_READ_VOUT
,
1200 .class = PSC_VOLTAGE_OUT
,
1203 .func
= PMBUS_HAVE_VOUT
,
1204 .sfunc
= PMBUS_HAVE_STATUS_VOUT
,
1205 .sbase
= PB_STATUS_VOUT_BASE
,
1206 .gbit
= PB_STATUS_VOUT_OV
,
1207 .limit
= vout_limit_attrs
,
1208 .nlimit
= ARRAY_SIZE(vout_limit_attrs
),
1212 /* Current attributes */
1214 static const struct pmbus_limit_attr iin_limit_attrs
[] = {
1216 .reg
= PMBUS_IIN_OC_WARN_LIMIT
,
1218 .alarm
= "max_alarm",
1219 .sbit
= PB_IIN_OC_WARNING
,
1221 .reg
= PMBUS_IIN_OC_FAULT_LIMIT
,
1223 .alarm
= "crit_alarm",
1224 .sbit
= PB_IIN_OC_FAULT
,
1226 .reg
= PMBUS_VIRT_READ_IIN_AVG
,
1230 .reg
= PMBUS_VIRT_READ_IIN_MIN
,
1234 .reg
= PMBUS_VIRT_READ_IIN_MAX
,
1238 .reg
= PMBUS_VIRT_RESET_IIN_HISTORY
,
1239 .attr
= "reset_history",
1243 static const struct pmbus_limit_attr iout_limit_attrs
[] = {
1245 .reg
= PMBUS_IOUT_OC_WARN_LIMIT
,
1247 .alarm
= "max_alarm",
1248 .sbit
= PB_IOUT_OC_WARNING
,
1250 .reg
= PMBUS_IOUT_UC_FAULT_LIMIT
,
1252 .alarm
= "lcrit_alarm",
1253 .sbit
= PB_IOUT_UC_FAULT
,
1255 .reg
= PMBUS_IOUT_OC_FAULT_LIMIT
,
1257 .alarm
= "crit_alarm",
1258 .sbit
= PB_IOUT_OC_FAULT
,
1260 .reg
= PMBUS_VIRT_READ_IOUT_AVG
,
1264 .reg
= PMBUS_VIRT_READ_IOUT_MIN
,
1268 .reg
= PMBUS_VIRT_READ_IOUT_MAX
,
1272 .reg
= PMBUS_VIRT_RESET_IOUT_HISTORY
,
1273 .attr
= "reset_history",
1277 static const struct pmbus_sensor_attr current_attributes
[] = {
1279 .reg
= PMBUS_READ_IIN
,
1280 .class = PSC_CURRENT_IN
,
1282 .func
= PMBUS_HAVE_IIN
,
1283 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1284 .sbase
= PB_STATUS_INPUT_BASE
,
1285 .limit
= iin_limit_attrs
,
1286 .nlimit
= ARRAY_SIZE(iin_limit_attrs
),
1288 .reg
= PMBUS_READ_IOUT
,
1289 .class = PSC_CURRENT_OUT
,
1292 .func
= PMBUS_HAVE_IOUT
,
1293 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1294 .sbase
= PB_STATUS_IOUT_BASE
,
1295 .gbit
= PB_STATUS_IOUT_OC
,
1296 .limit
= iout_limit_attrs
,
1297 .nlimit
= ARRAY_SIZE(iout_limit_attrs
),
1301 /* Power attributes */
1303 static const struct pmbus_limit_attr pin_limit_attrs
[] = {
1305 .reg
= PMBUS_PIN_OP_WARN_LIMIT
,
1308 .sbit
= PB_PIN_OP_WARNING
,
1310 .reg
= PMBUS_VIRT_READ_PIN_AVG
,
1314 .reg
= PMBUS_VIRT_READ_PIN_MAX
,
1316 .attr
= "input_highest",
1318 .reg
= PMBUS_VIRT_RESET_PIN_HISTORY
,
1319 .attr
= "reset_history",
1323 static const struct pmbus_limit_attr pout_limit_attrs
[] = {
1325 .reg
= PMBUS_POUT_MAX
,
1327 .alarm
= "cap_alarm",
1328 .sbit
= PB_POWER_LIMITING
,
1330 .reg
= PMBUS_POUT_OP_WARN_LIMIT
,
1332 .alarm
= "max_alarm",
1333 .sbit
= PB_POUT_OP_WARNING
,
1335 .reg
= PMBUS_POUT_OP_FAULT_LIMIT
,
1337 .alarm
= "crit_alarm",
1338 .sbit
= PB_POUT_OP_FAULT
,
1340 .reg
= PMBUS_VIRT_READ_POUT_AVG
,
1344 .reg
= PMBUS_VIRT_READ_POUT_MAX
,
1346 .attr
= "input_highest",
1348 .reg
= PMBUS_VIRT_RESET_POUT_HISTORY
,
1349 .attr
= "reset_history",
1353 static const struct pmbus_sensor_attr power_attributes
[] = {
1355 .reg
= PMBUS_READ_PIN
,
1358 .func
= PMBUS_HAVE_PIN
,
1359 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1360 .sbase
= PB_STATUS_INPUT_BASE
,
1361 .limit
= pin_limit_attrs
,
1362 .nlimit
= ARRAY_SIZE(pin_limit_attrs
),
1364 .reg
= PMBUS_READ_POUT
,
1368 .func
= PMBUS_HAVE_POUT
,
1369 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1370 .sbase
= PB_STATUS_IOUT_BASE
,
1371 .limit
= pout_limit_attrs
,
1372 .nlimit
= ARRAY_SIZE(pout_limit_attrs
),
1376 /* Temperature atributes */
1378 static const struct pmbus_limit_attr temp_limit_attrs
[] = {
1380 .reg
= PMBUS_UT_WARN_LIMIT
,
1383 .alarm
= "min_alarm",
1384 .sbit
= PB_TEMP_UT_WARNING
,
1386 .reg
= PMBUS_UT_FAULT_LIMIT
,
1389 .alarm
= "lcrit_alarm",
1390 .sbit
= PB_TEMP_UT_FAULT
,
1392 .reg
= PMBUS_OT_WARN_LIMIT
,
1394 .alarm
= "max_alarm",
1395 .sbit
= PB_TEMP_OT_WARNING
,
1397 .reg
= PMBUS_OT_FAULT_LIMIT
,
1399 .alarm
= "crit_alarm",
1400 .sbit
= PB_TEMP_OT_FAULT
,
1402 .reg
= PMBUS_VIRT_READ_TEMP_MIN
,
1405 .reg
= PMBUS_VIRT_READ_TEMP_AVG
,
1408 .reg
= PMBUS_VIRT_READ_TEMP_MAX
,
1411 .reg
= PMBUS_VIRT_RESET_TEMP_HISTORY
,
1412 .attr
= "reset_history",
1416 static const struct pmbus_limit_attr temp_limit_attrs2
[] = {
1418 .reg
= PMBUS_UT_WARN_LIMIT
,
1421 .alarm
= "min_alarm",
1422 .sbit
= PB_TEMP_UT_WARNING
,
1424 .reg
= PMBUS_UT_FAULT_LIMIT
,
1427 .alarm
= "lcrit_alarm",
1428 .sbit
= PB_TEMP_UT_FAULT
,
1430 .reg
= PMBUS_OT_WARN_LIMIT
,
1432 .alarm
= "max_alarm",
1433 .sbit
= PB_TEMP_OT_WARNING
,
1435 .reg
= PMBUS_OT_FAULT_LIMIT
,
1437 .alarm
= "crit_alarm",
1438 .sbit
= PB_TEMP_OT_FAULT
,
1440 .reg
= PMBUS_VIRT_READ_TEMP2_MIN
,
1443 .reg
= PMBUS_VIRT_READ_TEMP2_AVG
,
1446 .reg
= PMBUS_VIRT_READ_TEMP2_MAX
,
1449 .reg
= PMBUS_VIRT_RESET_TEMP2_HISTORY
,
1450 .attr
= "reset_history",
1454 static const struct pmbus_limit_attr temp_limit_attrs3
[] = {
1456 .reg
= PMBUS_UT_WARN_LIMIT
,
1459 .alarm
= "min_alarm",
1460 .sbit
= PB_TEMP_UT_WARNING
,
1462 .reg
= PMBUS_UT_FAULT_LIMIT
,
1465 .alarm
= "lcrit_alarm",
1466 .sbit
= PB_TEMP_UT_FAULT
,
1468 .reg
= PMBUS_OT_WARN_LIMIT
,
1470 .alarm
= "max_alarm",
1471 .sbit
= PB_TEMP_OT_WARNING
,
1473 .reg
= PMBUS_OT_FAULT_LIMIT
,
1475 .alarm
= "crit_alarm",
1476 .sbit
= PB_TEMP_OT_FAULT
,
1480 static const struct pmbus_sensor_attr temp_attributes
[] = {
1482 .reg
= PMBUS_READ_TEMPERATURE_1
,
1483 .class = PSC_TEMPERATURE
,
1487 .func
= PMBUS_HAVE_TEMP
,
1488 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1489 .sbase
= PB_STATUS_TEMP_BASE
,
1490 .gbit
= PB_STATUS_TEMPERATURE
,
1491 .limit
= temp_limit_attrs
,
1492 .nlimit
= ARRAY_SIZE(temp_limit_attrs
),
1494 .reg
= PMBUS_READ_TEMPERATURE_2
,
1495 .class = PSC_TEMPERATURE
,
1499 .func
= PMBUS_HAVE_TEMP2
,
1500 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1501 .sbase
= PB_STATUS_TEMP_BASE
,
1502 .gbit
= PB_STATUS_TEMPERATURE
,
1503 .limit
= temp_limit_attrs2
,
1504 .nlimit
= ARRAY_SIZE(temp_limit_attrs2
),
1506 .reg
= PMBUS_READ_TEMPERATURE_3
,
1507 .class = PSC_TEMPERATURE
,
1511 .func
= PMBUS_HAVE_TEMP3
,
1512 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1513 .sbase
= PB_STATUS_TEMP_BASE
,
1514 .gbit
= PB_STATUS_TEMPERATURE
,
1515 .limit
= temp_limit_attrs3
,
1516 .nlimit
= ARRAY_SIZE(temp_limit_attrs3
),
1520 static const int pmbus_fan_registers
[] = {
1521 PMBUS_READ_FAN_SPEED_1
,
1522 PMBUS_READ_FAN_SPEED_2
,
1523 PMBUS_READ_FAN_SPEED_3
,
1524 PMBUS_READ_FAN_SPEED_4
1527 static const int pmbus_fan_config_registers
[] = {
1528 PMBUS_FAN_CONFIG_12
,
1529 PMBUS_FAN_CONFIG_12
,
1530 PMBUS_FAN_CONFIG_34
,
1534 static const int pmbus_fan_status_registers
[] = {
1535 PMBUS_STATUS_FAN_12
,
1536 PMBUS_STATUS_FAN_12
,
1537 PMBUS_STATUS_FAN_34
,
1541 static const u32 pmbus_fan_flags
[] = {
1548 static const u32 pmbus_fan_status_flags
[] = {
1549 PMBUS_HAVE_STATUS_FAN12
,
1550 PMBUS_HAVE_STATUS_FAN12
,
1551 PMBUS_HAVE_STATUS_FAN34
,
1552 PMBUS_HAVE_STATUS_FAN34
1556 static void pmbus_add_fan_attributes(struct i2c_client
*client
,
1557 struct pmbus_data
*data
)
1559 const struct pmbus_driver_info
*info
= data
->info
;
1563 for (page
= 0; page
< info
->pages
; page
++) {
1566 for (f
= 0; f
< ARRAY_SIZE(pmbus_fan_registers
); f
++) {
1569 if (!(info
->func
[page
] & pmbus_fan_flags
[f
]))
1572 if (!pmbus_check_word_register(client
, page
,
1573 pmbus_fan_registers
[f
]))
1577 * Skip fan if not installed.
1578 * Each fan configuration register covers multiple fans,
1579 * so we have to do some magic.
1581 regval
= _pmbus_read_byte_data(client
, page
,
1582 pmbus_fan_config_registers
[f
]);
1584 (!(regval
& (PB_FAN_1_INSTALLED
>> ((f
& 1) * 4)))))
1587 pmbus_add_sensor(data
, "fan", "input", index
, page
,
1588 pmbus_fan_registers
[f
], PSC_FAN
, true,
1592 * Each fan status register covers multiple fans,
1593 * so we have to do some magic.
1595 if ((info
->func
[page
] & pmbus_fan_status_flags
[f
]) &&
1596 pmbus_check_byte_register(client
,
1597 page
, pmbus_fan_status_registers
[f
])) {
1600 if (f
> 1) /* fan 3, 4 */
1601 base
= PB_STATUS_FAN34_BASE
+ page
;
1603 base
= PB_STATUS_FAN_BASE
+ page
;
1604 pmbus_add_boolean_reg(data
, "fan", "alarm",
1606 PB_FAN_FAN1_WARNING
>> (f
& 1));
1607 pmbus_add_boolean_reg(data
, "fan", "fault",
1609 PB_FAN_FAN1_FAULT
>> (f
& 1));
1616 static void pmbus_find_attributes(struct i2c_client
*client
,
1617 struct pmbus_data
*data
)
1619 /* Voltage sensors */
1620 pmbus_add_sensor_attrs(client
, data
, "in", voltage_attributes
,
1621 ARRAY_SIZE(voltage_attributes
));
1623 /* Current sensors */
1624 pmbus_add_sensor_attrs(client
, data
, "curr", current_attributes
,
1625 ARRAY_SIZE(current_attributes
));
1628 pmbus_add_sensor_attrs(client
, data
, "power", power_attributes
,
1629 ARRAY_SIZE(power_attributes
));
1631 /* Temperature sensors */
1632 pmbus_add_sensor_attrs(client
, data
, "temp", temp_attributes
,
1633 ARRAY_SIZE(temp_attributes
));
1636 pmbus_add_fan_attributes(client
, data
);
1640 * Identify chip parameters.
1641 * This function is called for all chips.
1643 static int pmbus_identify_common(struct i2c_client
*client
,
1644 struct pmbus_data
*data
)
1648 if (pmbus_check_byte_register(client
, 0, PMBUS_VOUT_MODE
))
1649 vout_mode
= _pmbus_read_byte_data(client
, 0, PMBUS_VOUT_MODE
);
1650 if (vout_mode
>= 0 && vout_mode
!= 0xff) {
1652 * Not all chips support the VOUT_MODE command,
1653 * so a failure to read it is not an error.
1655 switch (vout_mode
>> 5) {
1656 case 0: /* linear mode */
1657 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != linear
)
1660 data
->exponent
= ((s8
)(vout_mode
<< 3)) >> 3;
1662 case 1: /* VID mode */
1663 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != vid
)
1666 case 2: /* direct mode */
1667 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != direct
)
1675 /* Determine maximum number of sensors, booleans, and labels */
1676 pmbus_find_max_attr(client
, data
);
1677 pmbus_clear_fault_page(client
, 0);
1681 int pmbus_do_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
,
1682 struct pmbus_driver_info
*info
)
1684 const struct pmbus_platform_data
*pdata
= client
->dev
.platform_data
;
1685 struct pmbus_data
*data
;
1689 dev_err(&client
->dev
, "Missing chip information");
1693 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WRITE_BYTE
1694 | I2C_FUNC_SMBUS_BYTE_DATA
1695 | I2C_FUNC_SMBUS_WORD_DATA
))
1698 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1700 dev_err(&client
->dev
, "No memory to allocate driver data\n");
1704 i2c_set_clientdata(client
, data
);
1705 mutex_init(&data
->update_lock
);
1707 /* Bail out if PMBus status register does not exist. */
1708 if (i2c_smbus_read_byte_data(client
, PMBUS_STATUS_BYTE
) < 0) {
1709 dev_err(&client
->dev
, "PMBus status register not found\n");
1714 data
->flags
= pdata
->flags
;
1717 pmbus_clear_faults(client
);
1719 if (info
->identify
) {
1720 ret
= (*info
->identify
)(client
, info
);
1722 dev_err(&client
->dev
, "Chip identification failed\n");
1727 if (info
->pages
<= 0 || info
->pages
> PMBUS_PAGES
) {
1728 dev_err(&client
->dev
, "Bad number of PMBus pages: %d\n",
1733 ret
= pmbus_identify_common(client
, data
);
1735 dev_err(&client
->dev
, "Failed to identify chip capabilities\n");
1740 data
->sensors
= devm_kzalloc(&client
->dev
, sizeof(struct pmbus_sensor
)
1741 * data
->max_sensors
, GFP_KERNEL
);
1742 if (!data
->sensors
) {
1743 dev_err(&client
->dev
, "No memory to allocate sensor data\n");
1747 data
->booleans
= devm_kzalloc(&client
->dev
, sizeof(struct pmbus_boolean
)
1748 * data
->max_booleans
, GFP_KERNEL
);
1749 if (!data
->booleans
) {
1750 dev_err(&client
->dev
, "No memory to allocate boolean data\n");
1754 data
->labels
= devm_kzalloc(&client
->dev
, sizeof(struct pmbus_label
)
1755 * data
->max_labels
, GFP_KERNEL
);
1756 if (!data
->labels
) {
1757 dev_err(&client
->dev
, "No memory to allocate label data\n");
1761 data
->attributes
= devm_kzalloc(&client
->dev
, sizeof(struct attribute
*)
1762 * data
->max_attributes
, GFP_KERNEL
);
1763 if (!data
->attributes
) {
1764 dev_err(&client
->dev
, "No memory to allocate attribute data\n");
1768 pmbus_find_attributes(client
, data
);
1771 * If there are no attributes, something is wrong.
1772 * Bail out instead of trying to register nothing.
1774 if (!data
->num_attributes
) {
1775 dev_err(&client
->dev
, "No attributes found\n");
1779 /* Register sysfs hooks */
1780 data
->group
.attrs
= data
->attributes
;
1781 ret
= sysfs_create_group(&client
->dev
.kobj
, &data
->group
);
1783 dev_err(&client
->dev
, "Failed to create sysfs entries\n");
1786 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
1787 if (IS_ERR(data
->hwmon_dev
)) {
1788 ret
= PTR_ERR(data
->hwmon_dev
);
1789 dev_err(&client
->dev
, "Failed to register hwmon device\n");
1790 goto out_hwmon_device_register
;
1794 out_hwmon_device_register
:
1795 sysfs_remove_group(&client
->dev
.kobj
, &data
->group
);
1798 EXPORT_SYMBOL_GPL(pmbus_do_probe
);
1800 int pmbus_do_remove(struct i2c_client
*client
)
1802 struct pmbus_data
*data
= i2c_get_clientdata(client
);
1803 hwmon_device_unregister(data
->hwmon_dev
);
1804 sysfs_remove_group(&client
->dev
.kobj
, &data
->group
);
1807 EXPORT_SYMBOL_GPL(pmbus_do_remove
);
1809 MODULE_AUTHOR("Guenter Roeck");
1810 MODULE_DESCRIPTION("PMBus core driver");
1811 MODULE_LICENSE("GPL");