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/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/err.h>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
28 #include <linux/hwmon.h>
29 #include <linux/hwmon-sysfs.h>
30 #include <linux/jiffies.h>
31 #include <linux/i2c/pmbus.h>
35 * Number of additional attribute pointers to allocate
36 * with each call to krealloc
38 #define PMBUS_ATTR_ALLOC_SIZE 32
41 * Index into status register array, per status register group
43 #define PB_STATUS_BASE 0
44 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
45 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
46 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
47 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
48 #define PB_STATUS_TEMP_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
49 #define PB_STATUS_INPUT_BASE (PB_STATUS_TEMP_BASE + PMBUS_PAGES)
50 #define PB_STATUS_VMON_BASE (PB_STATUS_INPUT_BASE + 1)
52 #define PB_NUM_STATUS_REG (PB_STATUS_VMON_BASE + 1)
54 #define PMBUS_NAME_SIZE 24
57 struct pmbus_sensor
*next
;
58 char name
[PMBUS_NAME_SIZE
]; /* sysfs sensor name */
59 struct device_attribute attribute
;
60 u8 page
; /* page number */
61 u16 reg
; /* register */
62 enum pmbus_sensor_classes
class; /* sensor class */
63 bool update
; /* runtime sensor update needed */
64 int data
; /* Sensor data.
65 Negative if there was a read error */
67 #define to_pmbus_sensor(_attr) \
68 container_of(_attr, struct pmbus_sensor, attribute)
70 struct pmbus_boolean
{
71 char name
[PMBUS_NAME_SIZE
]; /* sysfs boolean name */
72 struct sensor_device_attribute attribute
;
73 struct pmbus_sensor
*s1
;
74 struct pmbus_sensor
*s2
;
76 #define to_pmbus_boolean(_attr) \
77 container_of(_attr, struct pmbus_boolean, attribute)
80 char name
[PMBUS_NAME_SIZE
]; /* sysfs label name */
81 struct device_attribute attribute
;
82 char label
[PMBUS_NAME_SIZE
]; /* label */
84 #define to_pmbus_label(_attr) \
85 container_of(_attr, struct pmbus_label, attribute)
89 struct device
*hwmon_dev
;
91 u32 flags
; /* from platform data */
93 int exponent
[PMBUS_PAGES
];
94 /* linear mode: exponent for output voltages */
96 const struct pmbus_driver_info
*info
;
100 struct attribute_group group
;
101 const struct attribute_group
*groups
[2];
103 struct pmbus_sensor
*sensors
;
105 struct mutex update_lock
;
107 unsigned long last_updated
; /* in jiffies */
110 * A single status register covers multiple attributes,
111 * so we keep them all together.
113 u8 status
[PB_NUM_STATUS_REG
];
119 void pmbus_clear_cache(struct i2c_client
*client
)
121 struct pmbus_data
*data
= i2c_get_clientdata(client
);
125 EXPORT_SYMBOL_GPL(pmbus_clear_cache
);
127 int pmbus_set_page(struct i2c_client
*client
, u8 page
)
129 struct pmbus_data
*data
= i2c_get_clientdata(client
);
133 if (page
!= data
->currpage
) {
134 rv
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, page
);
135 newpage
= i2c_smbus_read_byte_data(client
, PMBUS_PAGE
);
139 data
->currpage
= page
;
143 EXPORT_SYMBOL_GPL(pmbus_set_page
);
145 int pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
150 rv
= pmbus_set_page(client
, page
);
155 return i2c_smbus_write_byte(client
, value
);
157 EXPORT_SYMBOL_GPL(pmbus_write_byte
);
160 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
161 * a device specific mapping function exists and calls it if necessary.
163 static int _pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
165 struct pmbus_data
*data
= i2c_get_clientdata(client
);
166 const struct pmbus_driver_info
*info
= data
->info
;
169 if (info
->write_byte
) {
170 status
= info
->write_byte(client
, page
, value
);
171 if (status
!= -ENODATA
)
174 return pmbus_write_byte(client
, page
, value
);
177 int pmbus_write_word_data(struct i2c_client
*client
, u8 page
, u8 reg
, u16 word
)
181 rv
= pmbus_set_page(client
, page
);
185 return i2c_smbus_write_word_data(client
, reg
, word
);
187 EXPORT_SYMBOL_GPL(pmbus_write_word_data
);
190 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
191 * a device specific mapping function exists and calls it if necessary.
193 static int _pmbus_write_word_data(struct i2c_client
*client
, int page
, int reg
,
196 struct pmbus_data
*data
= i2c_get_clientdata(client
);
197 const struct pmbus_driver_info
*info
= data
->info
;
200 if (info
->write_word_data
) {
201 status
= info
->write_word_data(client
, page
, reg
, word
);
202 if (status
!= -ENODATA
)
205 if (reg
>= PMBUS_VIRT_BASE
)
207 return pmbus_write_word_data(client
, page
, reg
, word
);
210 int pmbus_read_word_data(struct i2c_client
*client
, u8 page
, u8 reg
)
214 rv
= pmbus_set_page(client
, page
);
218 return i2c_smbus_read_word_data(client
, reg
);
220 EXPORT_SYMBOL_GPL(pmbus_read_word_data
);
223 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
224 * a device specific mapping function exists and calls it if necessary.
226 static int _pmbus_read_word_data(struct i2c_client
*client
, int page
, int reg
)
228 struct pmbus_data
*data
= i2c_get_clientdata(client
);
229 const struct pmbus_driver_info
*info
= data
->info
;
232 if (info
->read_word_data
) {
233 status
= info
->read_word_data(client
, page
, reg
);
234 if (status
!= -ENODATA
)
237 if (reg
>= PMBUS_VIRT_BASE
)
239 return pmbus_read_word_data(client
, page
, reg
);
242 int pmbus_read_byte_data(struct i2c_client
*client
, int page
, u8 reg
)
247 rv
= pmbus_set_page(client
, page
);
252 return i2c_smbus_read_byte_data(client
, reg
);
254 EXPORT_SYMBOL_GPL(pmbus_read_byte_data
);
257 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
258 * a device specific mapping function exists and calls it if necessary.
260 static int _pmbus_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
262 struct pmbus_data
*data
= i2c_get_clientdata(client
);
263 const struct pmbus_driver_info
*info
= data
->info
;
266 if (info
->read_byte_data
) {
267 status
= info
->read_byte_data(client
, page
, reg
);
268 if (status
!= -ENODATA
)
271 return pmbus_read_byte_data(client
, page
, reg
);
274 static void pmbus_clear_fault_page(struct i2c_client
*client
, int page
)
276 _pmbus_write_byte(client
, page
, PMBUS_CLEAR_FAULTS
);
279 void pmbus_clear_faults(struct i2c_client
*client
)
281 struct pmbus_data
*data
= i2c_get_clientdata(client
);
284 for (i
= 0; i
< data
->info
->pages
; i
++)
285 pmbus_clear_fault_page(client
, i
);
287 EXPORT_SYMBOL_GPL(pmbus_clear_faults
);
289 static int pmbus_check_status_cml(struct i2c_client
*client
)
291 struct pmbus_data
*data
= i2c_get_clientdata(client
);
294 status
= _pmbus_read_byte_data(client
, -1, data
->status_register
);
295 if (status
< 0 || (status
& PB_STATUS_CML
)) {
296 status2
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
297 if (status2
< 0 || (status2
& PB_CML_FAULT_INVALID_COMMAND
))
303 static bool pmbus_check_register(struct i2c_client
*client
,
304 int (*func
)(struct i2c_client
*client
,
309 struct pmbus_data
*data
= i2c_get_clientdata(client
);
311 rv
= func(client
, page
, reg
);
312 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
313 rv
= pmbus_check_status_cml(client
);
314 pmbus_clear_fault_page(client
, -1);
318 bool pmbus_check_byte_register(struct i2c_client
*client
, int page
, int reg
)
320 return pmbus_check_register(client
, _pmbus_read_byte_data
, page
, reg
);
322 EXPORT_SYMBOL_GPL(pmbus_check_byte_register
);
324 bool pmbus_check_word_register(struct i2c_client
*client
, int page
, int reg
)
326 return pmbus_check_register(client
, _pmbus_read_word_data
, page
, reg
);
328 EXPORT_SYMBOL_GPL(pmbus_check_word_register
);
330 const struct pmbus_driver_info
*pmbus_get_driver_info(struct i2c_client
*client
)
332 struct pmbus_data
*data
= i2c_get_clientdata(client
);
336 EXPORT_SYMBOL_GPL(pmbus_get_driver_info
);
338 static struct _pmbus_status
{
343 { PMBUS_HAVE_STATUS_VOUT
, PB_STATUS_VOUT_BASE
, PMBUS_STATUS_VOUT
},
344 { PMBUS_HAVE_STATUS_IOUT
, PB_STATUS_IOUT_BASE
, PMBUS_STATUS_IOUT
},
345 { PMBUS_HAVE_STATUS_TEMP
, PB_STATUS_TEMP_BASE
,
346 PMBUS_STATUS_TEMPERATURE
},
347 { PMBUS_HAVE_STATUS_FAN12
, PB_STATUS_FAN_BASE
, PMBUS_STATUS_FAN_12
},
348 { PMBUS_HAVE_STATUS_FAN34
, PB_STATUS_FAN34_BASE
, PMBUS_STATUS_FAN_34
},
351 static struct pmbus_data
*pmbus_update_device(struct device
*dev
)
353 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
354 struct pmbus_data
*data
= i2c_get_clientdata(client
);
355 const struct pmbus_driver_info
*info
= data
->info
;
356 struct pmbus_sensor
*sensor
;
358 mutex_lock(&data
->update_lock
);
359 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
362 for (i
= 0; i
< info
->pages
; i
++) {
363 data
->status
[PB_STATUS_BASE
+ i
]
364 = _pmbus_read_byte_data(client
, i
,
365 data
->status_register
);
366 for (j
= 0; j
< ARRAY_SIZE(pmbus_status
); j
++) {
367 struct _pmbus_status
*s
= &pmbus_status
[j
];
369 if (!(info
->func
[i
] & s
->func
))
371 data
->status
[s
->base
+ i
]
372 = _pmbus_read_byte_data(client
, i
,
377 if (info
->func
[0] & PMBUS_HAVE_STATUS_INPUT
)
378 data
->status
[PB_STATUS_INPUT_BASE
]
379 = _pmbus_read_byte_data(client
, 0,
382 if (info
->func
[0] & PMBUS_HAVE_STATUS_VMON
)
383 data
->status
[PB_STATUS_VMON_BASE
]
384 = _pmbus_read_byte_data(client
, 0,
385 PMBUS_VIRT_STATUS_VMON
);
387 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
) {
388 if (!data
->valid
|| sensor
->update
)
390 = _pmbus_read_word_data(client
,
394 pmbus_clear_faults(client
);
395 data
->last_updated
= jiffies
;
398 mutex_unlock(&data
->update_lock
);
403 * Convert linear sensor values to milli- or micro-units
404 * depending on sensor type.
406 static long pmbus_reg2data_linear(struct pmbus_data
*data
,
407 struct pmbus_sensor
*sensor
)
413 if (sensor
->class == PSC_VOLTAGE_OUT
) { /* LINEAR16 */
414 exponent
= data
->exponent
[sensor
->page
];
415 mantissa
= (u16
) sensor
->data
;
416 } else { /* LINEAR11 */
417 exponent
= ((s16
)sensor
->data
) >> 11;
418 mantissa
= ((s16
)((sensor
->data
& 0x7ff) << 5)) >> 5;
423 /* scale result to milli-units for all sensors except fans */
424 if (sensor
->class != PSC_FAN
)
427 /* scale result to micro-units for power sensors */
428 if (sensor
->class == PSC_POWER
)
440 * Convert direct sensor values to milli- or micro-units
441 * depending on sensor type.
443 static long pmbus_reg2data_direct(struct pmbus_data
*data
,
444 struct pmbus_sensor
*sensor
)
446 long val
= (s16
) sensor
->data
;
449 m
= data
->info
->m
[sensor
->class];
450 b
= data
->info
->b
[sensor
->class];
451 R
= data
->info
->R
[sensor
->class];
456 /* X = 1/m * (Y * 10^-R - b) */
458 /* scale result to milli-units for everything but fans */
459 if (sensor
->class != PSC_FAN
) {
464 /* scale result to micro-units for power sensors */
465 if (sensor
->class == PSC_POWER
) {
475 val
= DIV_ROUND_CLOSEST(val
, 10);
479 return (val
- b
) / m
;
483 * Convert VID sensor values to milli- or micro-units
484 * depending on sensor type.
485 * We currently only support VR11.
487 static long pmbus_reg2data_vid(struct pmbus_data
*data
,
488 struct pmbus_sensor
*sensor
)
490 long val
= sensor
->data
;
492 if (val
< 0x02 || val
> 0xb2)
494 return DIV_ROUND_CLOSEST(160000 - (val
- 2) * 625, 100);
497 static long pmbus_reg2data(struct pmbus_data
*data
, struct pmbus_sensor
*sensor
)
501 switch (data
->info
->format
[sensor
->class]) {
503 val
= pmbus_reg2data_direct(data
, sensor
);
506 val
= pmbus_reg2data_vid(data
, sensor
);
510 val
= pmbus_reg2data_linear(data
, sensor
);
516 #define MAX_MANTISSA (1023 * 1000)
517 #define MIN_MANTISSA (511 * 1000)
519 static u16
pmbus_data2reg_linear(struct pmbus_data
*data
,
520 struct pmbus_sensor
*sensor
, long val
)
522 s16 exponent
= 0, mantissa
;
523 bool negative
= false;
529 if (sensor
->class == PSC_VOLTAGE_OUT
) {
530 /* LINEAR16 does not support negative voltages */
535 * For a static exponents, we don't have a choice
536 * but to adjust the value to it.
538 if (data
->exponent
[sensor
->page
] < 0)
539 val
<<= -data
->exponent
[sensor
->page
];
541 val
>>= data
->exponent
[sensor
->page
];
542 val
= DIV_ROUND_CLOSEST(val
, 1000);
551 /* Power is in uW. Convert to mW before converting. */
552 if (sensor
->class == PSC_POWER
)
553 val
= DIV_ROUND_CLOSEST(val
, 1000L);
556 * For simplicity, convert fan data to milli-units
557 * before calculating the exponent.
559 if (sensor
->class == PSC_FAN
)
562 /* Reduce large mantissa until it fits into 10 bit */
563 while (val
>= MAX_MANTISSA
&& exponent
< 15) {
567 /* Increase small mantissa to improve precision */
568 while (val
< MIN_MANTISSA
&& exponent
> -15) {
573 /* Convert mantissa from milli-units to units */
574 mantissa
= DIV_ROUND_CLOSEST(val
, 1000);
576 /* Ensure that resulting number is within range */
577 if (mantissa
> 0x3ff)
582 mantissa
= -mantissa
;
584 /* Convert to 5 bit exponent, 11 bit mantissa */
585 return (mantissa
& 0x7ff) | ((exponent
<< 11) & 0xf800);
588 static u16
pmbus_data2reg_direct(struct pmbus_data
*data
,
589 struct pmbus_sensor
*sensor
, long val
)
593 m
= data
->info
->m
[sensor
->class];
594 b
= data
->info
->b
[sensor
->class];
595 R
= data
->info
->R
[sensor
->class];
597 /* Power is in uW. Adjust R and b. */
598 if (sensor
->class == PSC_POWER
) {
603 /* Calculate Y = (m * X + b) * 10^R */
604 if (sensor
->class != PSC_FAN
) {
605 R
-= 3; /* Adjust R and b for data in milli-units */
615 val
= DIV_ROUND_CLOSEST(val
, 10);
622 static u16
pmbus_data2reg_vid(struct pmbus_data
*data
,
623 struct pmbus_sensor
*sensor
, long val
)
625 val
= clamp_val(val
, 500, 1600);
627 return 2 + DIV_ROUND_CLOSEST((1600 - val
) * 100, 625);
630 static u16
pmbus_data2reg(struct pmbus_data
*data
,
631 struct pmbus_sensor
*sensor
, long val
)
635 switch (data
->info
->format
[sensor
->class]) {
637 regval
= pmbus_data2reg_direct(data
, sensor
, val
);
640 regval
= pmbus_data2reg_vid(data
, sensor
, val
);
644 regval
= pmbus_data2reg_linear(data
, sensor
, val
);
651 * Return boolean calculated from converted data.
652 * <index> defines a status register index and mask.
653 * The mask is in the lower 8 bits, the register index is in bits 8..23.
655 * The associated pmbus_boolean structure contains optional pointers to two
656 * sensor attributes. If specified, those attributes are compared against each
657 * other to determine if a limit has been exceeded.
659 * If the sensor attribute pointers are NULL, the function returns true if
660 * (status[reg] & mask) is true.
662 * If sensor attribute pointers are provided, a comparison against a specified
663 * limit has to be performed to determine the boolean result.
664 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
665 * sensor values referenced by sensor attribute pointers s1 and s2).
667 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
668 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
670 * If a negative value is stored in any of the referenced registers, this value
671 * reflects an error code which will be returned.
673 static int pmbus_get_boolean(struct pmbus_data
*data
, struct pmbus_boolean
*b
,
676 struct pmbus_sensor
*s1
= b
->s1
;
677 struct pmbus_sensor
*s2
= b
->s2
;
678 u16 reg
= (index
>> 8) & 0xffff;
679 u8 mask
= index
& 0xff;
683 status
= data
->status
[reg
];
687 regval
= status
& mask
;
690 } else if (!s1
|| !s2
) {
691 WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b
, s1
, s2
);
701 v1
= pmbus_reg2data(data
, s1
);
702 v2
= pmbus_reg2data(data
, s2
);
703 ret
= !!(regval
&& v1
>= v2
);
708 static ssize_t
pmbus_show_boolean(struct device
*dev
,
709 struct device_attribute
*da
, char *buf
)
711 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
712 struct pmbus_boolean
*boolean
= to_pmbus_boolean(attr
);
713 struct pmbus_data
*data
= pmbus_update_device(dev
);
716 val
= pmbus_get_boolean(data
, boolean
, attr
->index
);
719 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
722 static ssize_t
pmbus_show_sensor(struct device
*dev
,
723 struct device_attribute
*devattr
, char *buf
)
725 struct pmbus_data
*data
= pmbus_update_device(dev
);
726 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
728 if (sensor
->data
< 0)
731 return snprintf(buf
, PAGE_SIZE
, "%ld\n", pmbus_reg2data(data
, sensor
));
734 static ssize_t
pmbus_set_sensor(struct device
*dev
,
735 struct device_attribute
*devattr
,
736 const char *buf
, size_t count
)
738 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
739 struct pmbus_data
*data
= i2c_get_clientdata(client
);
740 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
746 if (kstrtol(buf
, 10, &val
) < 0)
749 mutex_lock(&data
->update_lock
);
750 regval
= pmbus_data2reg(data
, sensor
, val
);
751 ret
= _pmbus_write_word_data(client
, sensor
->page
, sensor
->reg
, regval
);
755 sensor
->data
= regval
;
756 mutex_unlock(&data
->update_lock
);
760 static ssize_t
pmbus_show_label(struct device
*dev
,
761 struct device_attribute
*da
, char *buf
)
763 struct pmbus_label
*label
= to_pmbus_label(da
);
765 return snprintf(buf
, PAGE_SIZE
, "%s\n", label
->label
);
768 static int pmbus_add_attribute(struct pmbus_data
*data
, struct attribute
*attr
)
770 if (data
->num_attributes
>= data
->max_attributes
- 1) {
771 int new_max_attrs
= data
->max_attributes
+ PMBUS_ATTR_ALLOC_SIZE
;
772 void *new_attrs
= krealloc(data
->group
.attrs
,
773 new_max_attrs
* sizeof(void *),
777 data
->group
.attrs
= new_attrs
;
778 data
->max_attributes
= new_max_attrs
;
781 data
->group
.attrs
[data
->num_attributes
++] = attr
;
782 data
->group
.attrs
[data
->num_attributes
] = NULL
;
786 static void pmbus_dev_attr_init(struct device_attribute
*dev_attr
,
789 ssize_t (*show
)(struct device
*dev
,
790 struct device_attribute
*attr
,
792 ssize_t (*store
)(struct device
*dev
,
793 struct device_attribute
*attr
,
794 const char *buf
, size_t count
))
796 sysfs_attr_init(&dev_attr
->attr
);
797 dev_attr
->attr
.name
= name
;
798 dev_attr
->attr
.mode
= mode
;
799 dev_attr
->show
= show
;
800 dev_attr
->store
= store
;
803 static void pmbus_attr_init(struct sensor_device_attribute
*a
,
806 ssize_t (*show
)(struct device
*dev
,
807 struct device_attribute
*attr
,
809 ssize_t (*store
)(struct device
*dev
,
810 struct device_attribute
*attr
,
811 const char *buf
, size_t count
),
814 pmbus_dev_attr_init(&a
->dev_attr
, name
, mode
, show
, store
);
818 static int pmbus_add_boolean(struct pmbus_data
*data
,
819 const char *name
, const char *type
, int seq
,
820 struct pmbus_sensor
*s1
,
821 struct pmbus_sensor
*s2
,
824 struct pmbus_boolean
*boolean
;
825 struct sensor_device_attribute
*a
;
827 boolean
= devm_kzalloc(data
->dev
, sizeof(*boolean
), GFP_KERNEL
);
831 a
= &boolean
->attribute
;
833 snprintf(boolean
->name
, sizeof(boolean
->name
), "%s%d_%s",
837 pmbus_attr_init(a
, boolean
->name
, S_IRUGO
, pmbus_show_boolean
, NULL
,
840 return pmbus_add_attribute(data
, &a
->dev_attr
.attr
);
843 static struct pmbus_sensor
*pmbus_add_sensor(struct pmbus_data
*data
,
844 const char *name
, const char *type
,
845 int seq
, int page
, int reg
,
846 enum pmbus_sensor_classes
class,
847 bool update
, bool readonly
)
849 struct pmbus_sensor
*sensor
;
850 struct device_attribute
*a
;
852 sensor
= devm_kzalloc(data
->dev
, sizeof(*sensor
), GFP_KERNEL
);
855 a
= &sensor
->attribute
;
857 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d_%s",
861 sensor
->class = class;
862 sensor
->update
= update
;
863 pmbus_dev_attr_init(a
, sensor
->name
,
864 readonly
? S_IRUGO
: S_IRUGO
| S_IWUSR
,
865 pmbus_show_sensor
, pmbus_set_sensor
);
867 if (pmbus_add_attribute(data
, &a
->attr
))
870 sensor
->next
= data
->sensors
;
871 data
->sensors
= sensor
;
876 static int pmbus_add_label(struct pmbus_data
*data
,
877 const char *name
, int seq
,
878 const char *lstring
, int index
)
880 struct pmbus_label
*label
;
881 struct device_attribute
*a
;
883 label
= devm_kzalloc(data
->dev
, sizeof(*label
), GFP_KERNEL
);
887 a
= &label
->attribute
;
889 snprintf(label
->name
, sizeof(label
->name
), "%s%d_label", name
, seq
);
891 strncpy(label
->label
, lstring
, sizeof(label
->label
) - 1);
893 snprintf(label
->label
, sizeof(label
->label
), "%s%d", lstring
,
896 pmbus_dev_attr_init(a
, label
->name
, S_IRUGO
, pmbus_show_label
, NULL
);
897 return pmbus_add_attribute(data
, &a
->attr
);
901 * Search for attributes. Allocate sensors, booleans, and labels as needed.
905 * The pmbus_limit_attr structure describes a single limit attribute
906 * and its associated alarm attribute.
908 struct pmbus_limit_attr
{
909 u16 reg
; /* Limit register */
910 u16 sbit
; /* Alarm attribute status bit */
911 bool update
; /* True if register needs updates */
912 bool low
; /* True if low limit; for limits with compare
914 const char *attr
; /* Attribute name */
915 const char *alarm
; /* Alarm attribute name */
919 * The pmbus_sensor_attr structure describes one sensor attribute. This
920 * description includes a reference to the associated limit attributes.
922 struct pmbus_sensor_attr
{
923 u16 reg
; /* sensor register */
924 u8 gbit
; /* generic status bit */
925 u8 nlimit
; /* # of limit registers */
926 enum pmbus_sensor_classes
class;/* sensor class */
927 const char *label
; /* sensor label */
928 bool paged
; /* true if paged sensor */
929 bool update
; /* true if update needed */
930 bool compare
; /* true if compare function needed */
931 u32 func
; /* sensor mask */
932 u32 sfunc
; /* sensor status mask */
933 int sbase
; /* status base register */
934 const struct pmbus_limit_attr
*limit
;/* limit registers */
938 * Add a set of limit attributes and, if supported, the associated
940 * returns 0 if no alarm register found, 1 if an alarm register was found,
943 static int pmbus_add_limit_attrs(struct i2c_client
*client
,
944 struct pmbus_data
*data
,
945 const struct pmbus_driver_info
*info
,
946 const char *name
, int index
, int page
,
947 struct pmbus_sensor
*base
,
948 const struct pmbus_sensor_attr
*attr
)
950 const struct pmbus_limit_attr
*l
= attr
->limit
;
951 int nlimit
= attr
->nlimit
;
954 struct pmbus_sensor
*curr
;
956 for (i
= 0; i
< nlimit
; i
++) {
957 if (pmbus_check_word_register(client
, page
, l
->reg
)) {
958 curr
= pmbus_add_sensor(data
, name
, l
->attr
, index
,
959 page
, l
->reg
, attr
->class,
960 attr
->update
|| l
->update
,
964 if (l
->sbit
&& (info
->func
[page
] & attr
->sfunc
)) {
965 ret
= pmbus_add_boolean(data
, name
,
967 attr
->compare
? l
->low
? curr
: base
969 attr
->compare
? l
->low
? base
: curr
971 attr
->sbase
+ page
, l
->sbit
);
982 static int pmbus_add_sensor_attrs_one(struct i2c_client
*client
,
983 struct pmbus_data
*data
,
984 const struct pmbus_driver_info
*info
,
987 const struct pmbus_sensor_attr
*attr
)
989 struct pmbus_sensor
*base
;
993 ret
= pmbus_add_label(data
, name
, index
, attr
->label
,
994 attr
->paged
? page
+ 1 : 0);
998 base
= pmbus_add_sensor(data
, name
, "input", index
, page
, attr
->reg
,
999 attr
->class, true, true);
1003 ret
= pmbus_add_limit_attrs(client
, data
, info
, name
,
1004 index
, page
, base
, attr
);
1008 * Add generic alarm attribute only if there are no individual
1009 * alarm attributes, if there is a global alarm bit, and if
1010 * the generic status register for this page is accessible.
1012 if (!ret
&& attr
->gbit
&&
1013 pmbus_check_byte_register(client
, page
,
1014 data
->status_register
)) {
1015 ret
= pmbus_add_boolean(data
, name
, "alarm", index
,
1017 PB_STATUS_BASE
+ page
,
1026 static int pmbus_add_sensor_attrs(struct i2c_client
*client
,
1027 struct pmbus_data
*data
,
1029 const struct pmbus_sensor_attr
*attrs
,
1032 const struct pmbus_driver_info
*info
= data
->info
;
1037 for (i
= 0; i
< nattrs
; i
++) {
1040 pages
= attrs
->paged
? info
->pages
: 1;
1041 for (page
= 0; page
< pages
; page
++) {
1042 if (!(info
->func
[page
] & attrs
->func
))
1044 ret
= pmbus_add_sensor_attrs_one(client
, data
, info
,
1056 static const struct pmbus_limit_attr vin_limit_attrs
[] = {
1058 .reg
= PMBUS_VIN_UV_WARN_LIMIT
,
1060 .alarm
= "min_alarm",
1061 .sbit
= PB_VOLTAGE_UV_WARNING
,
1063 .reg
= PMBUS_VIN_UV_FAULT_LIMIT
,
1065 .alarm
= "lcrit_alarm",
1066 .sbit
= PB_VOLTAGE_UV_FAULT
,
1068 .reg
= PMBUS_VIN_OV_WARN_LIMIT
,
1070 .alarm
= "max_alarm",
1071 .sbit
= PB_VOLTAGE_OV_WARNING
,
1073 .reg
= PMBUS_VIN_OV_FAULT_LIMIT
,
1075 .alarm
= "crit_alarm",
1076 .sbit
= PB_VOLTAGE_OV_FAULT
,
1078 .reg
= PMBUS_VIRT_READ_VIN_AVG
,
1082 .reg
= PMBUS_VIRT_READ_VIN_MIN
,
1086 .reg
= PMBUS_VIRT_READ_VIN_MAX
,
1090 .reg
= PMBUS_VIRT_RESET_VIN_HISTORY
,
1091 .attr
= "reset_history",
1095 static const struct pmbus_limit_attr vmon_limit_attrs
[] = {
1097 .reg
= PMBUS_VIRT_VMON_UV_WARN_LIMIT
,
1099 .alarm
= "min_alarm",
1100 .sbit
= PB_VOLTAGE_UV_WARNING
,
1102 .reg
= PMBUS_VIRT_VMON_UV_FAULT_LIMIT
,
1104 .alarm
= "lcrit_alarm",
1105 .sbit
= PB_VOLTAGE_UV_FAULT
,
1107 .reg
= PMBUS_VIRT_VMON_OV_WARN_LIMIT
,
1109 .alarm
= "max_alarm",
1110 .sbit
= PB_VOLTAGE_OV_WARNING
,
1112 .reg
= PMBUS_VIRT_VMON_OV_FAULT_LIMIT
,
1114 .alarm
= "crit_alarm",
1115 .sbit
= PB_VOLTAGE_OV_FAULT
,
1119 static const struct pmbus_limit_attr vout_limit_attrs
[] = {
1121 .reg
= PMBUS_VOUT_UV_WARN_LIMIT
,
1123 .alarm
= "min_alarm",
1124 .sbit
= PB_VOLTAGE_UV_WARNING
,
1126 .reg
= PMBUS_VOUT_UV_FAULT_LIMIT
,
1128 .alarm
= "lcrit_alarm",
1129 .sbit
= PB_VOLTAGE_UV_FAULT
,
1131 .reg
= PMBUS_VOUT_OV_WARN_LIMIT
,
1133 .alarm
= "max_alarm",
1134 .sbit
= PB_VOLTAGE_OV_WARNING
,
1136 .reg
= PMBUS_VOUT_OV_FAULT_LIMIT
,
1138 .alarm
= "crit_alarm",
1139 .sbit
= PB_VOLTAGE_OV_FAULT
,
1141 .reg
= PMBUS_VIRT_READ_VOUT_AVG
,
1145 .reg
= PMBUS_VIRT_READ_VOUT_MIN
,
1149 .reg
= PMBUS_VIRT_READ_VOUT_MAX
,
1153 .reg
= PMBUS_VIRT_RESET_VOUT_HISTORY
,
1154 .attr
= "reset_history",
1158 static const struct pmbus_sensor_attr voltage_attributes
[] = {
1160 .reg
= PMBUS_READ_VIN
,
1161 .class = PSC_VOLTAGE_IN
,
1163 .func
= PMBUS_HAVE_VIN
,
1164 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1165 .sbase
= PB_STATUS_INPUT_BASE
,
1166 .gbit
= PB_STATUS_VIN_UV
,
1167 .limit
= vin_limit_attrs
,
1168 .nlimit
= ARRAY_SIZE(vin_limit_attrs
),
1170 .reg
= PMBUS_VIRT_READ_VMON
,
1171 .class = PSC_VOLTAGE_IN
,
1173 .func
= PMBUS_HAVE_VMON
,
1174 .sfunc
= PMBUS_HAVE_STATUS_VMON
,
1175 .sbase
= PB_STATUS_VMON_BASE
,
1176 .limit
= vmon_limit_attrs
,
1177 .nlimit
= ARRAY_SIZE(vmon_limit_attrs
),
1179 .reg
= PMBUS_READ_VCAP
,
1180 .class = PSC_VOLTAGE_IN
,
1182 .func
= PMBUS_HAVE_VCAP
,
1184 .reg
= PMBUS_READ_VOUT
,
1185 .class = PSC_VOLTAGE_OUT
,
1188 .func
= PMBUS_HAVE_VOUT
,
1189 .sfunc
= PMBUS_HAVE_STATUS_VOUT
,
1190 .sbase
= PB_STATUS_VOUT_BASE
,
1191 .gbit
= PB_STATUS_VOUT_OV
,
1192 .limit
= vout_limit_attrs
,
1193 .nlimit
= ARRAY_SIZE(vout_limit_attrs
),
1197 /* Current attributes */
1199 static const struct pmbus_limit_attr iin_limit_attrs
[] = {
1201 .reg
= PMBUS_IIN_OC_WARN_LIMIT
,
1203 .alarm
= "max_alarm",
1204 .sbit
= PB_IIN_OC_WARNING
,
1206 .reg
= PMBUS_IIN_OC_FAULT_LIMIT
,
1208 .alarm
= "crit_alarm",
1209 .sbit
= PB_IIN_OC_FAULT
,
1211 .reg
= PMBUS_VIRT_READ_IIN_AVG
,
1215 .reg
= PMBUS_VIRT_READ_IIN_MIN
,
1219 .reg
= PMBUS_VIRT_READ_IIN_MAX
,
1223 .reg
= PMBUS_VIRT_RESET_IIN_HISTORY
,
1224 .attr
= "reset_history",
1228 static const struct pmbus_limit_attr iout_limit_attrs
[] = {
1230 .reg
= PMBUS_IOUT_OC_WARN_LIMIT
,
1232 .alarm
= "max_alarm",
1233 .sbit
= PB_IOUT_OC_WARNING
,
1235 .reg
= PMBUS_IOUT_UC_FAULT_LIMIT
,
1237 .alarm
= "lcrit_alarm",
1238 .sbit
= PB_IOUT_UC_FAULT
,
1240 .reg
= PMBUS_IOUT_OC_FAULT_LIMIT
,
1242 .alarm
= "crit_alarm",
1243 .sbit
= PB_IOUT_OC_FAULT
,
1245 .reg
= PMBUS_VIRT_READ_IOUT_AVG
,
1249 .reg
= PMBUS_VIRT_READ_IOUT_MIN
,
1253 .reg
= PMBUS_VIRT_READ_IOUT_MAX
,
1257 .reg
= PMBUS_VIRT_RESET_IOUT_HISTORY
,
1258 .attr
= "reset_history",
1262 static const struct pmbus_sensor_attr current_attributes
[] = {
1264 .reg
= PMBUS_READ_IIN
,
1265 .class = PSC_CURRENT_IN
,
1267 .func
= PMBUS_HAVE_IIN
,
1268 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1269 .sbase
= PB_STATUS_INPUT_BASE
,
1270 .limit
= iin_limit_attrs
,
1271 .nlimit
= ARRAY_SIZE(iin_limit_attrs
),
1273 .reg
= PMBUS_READ_IOUT
,
1274 .class = PSC_CURRENT_OUT
,
1277 .func
= PMBUS_HAVE_IOUT
,
1278 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1279 .sbase
= PB_STATUS_IOUT_BASE
,
1280 .gbit
= PB_STATUS_IOUT_OC
,
1281 .limit
= iout_limit_attrs
,
1282 .nlimit
= ARRAY_SIZE(iout_limit_attrs
),
1286 /* Power attributes */
1288 static const struct pmbus_limit_attr pin_limit_attrs
[] = {
1290 .reg
= PMBUS_PIN_OP_WARN_LIMIT
,
1293 .sbit
= PB_PIN_OP_WARNING
,
1295 .reg
= PMBUS_VIRT_READ_PIN_AVG
,
1299 .reg
= PMBUS_VIRT_READ_PIN_MAX
,
1301 .attr
= "input_highest",
1303 .reg
= PMBUS_VIRT_RESET_PIN_HISTORY
,
1304 .attr
= "reset_history",
1308 static const struct pmbus_limit_attr pout_limit_attrs
[] = {
1310 .reg
= PMBUS_POUT_MAX
,
1312 .alarm
= "cap_alarm",
1313 .sbit
= PB_POWER_LIMITING
,
1315 .reg
= PMBUS_POUT_OP_WARN_LIMIT
,
1317 .alarm
= "max_alarm",
1318 .sbit
= PB_POUT_OP_WARNING
,
1320 .reg
= PMBUS_POUT_OP_FAULT_LIMIT
,
1322 .alarm
= "crit_alarm",
1323 .sbit
= PB_POUT_OP_FAULT
,
1325 .reg
= PMBUS_VIRT_READ_POUT_AVG
,
1329 .reg
= PMBUS_VIRT_READ_POUT_MAX
,
1331 .attr
= "input_highest",
1333 .reg
= PMBUS_VIRT_RESET_POUT_HISTORY
,
1334 .attr
= "reset_history",
1338 static const struct pmbus_sensor_attr power_attributes
[] = {
1340 .reg
= PMBUS_READ_PIN
,
1343 .func
= PMBUS_HAVE_PIN
,
1344 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1345 .sbase
= PB_STATUS_INPUT_BASE
,
1346 .limit
= pin_limit_attrs
,
1347 .nlimit
= ARRAY_SIZE(pin_limit_attrs
),
1349 .reg
= PMBUS_READ_POUT
,
1353 .func
= PMBUS_HAVE_POUT
,
1354 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1355 .sbase
= PB_STATUS_IOUT_BASE
,
1356 .limit
= pout_limit_attrs
,
1357 .nlimit
= ARRAY_SIZE(pout_limit_attrs
),
1361 /* Temperature atributes */
1363 static const struct pmbus_limit_attr temp_limit_attrs
[] = {
1365 .reg
= PMBUS_UT_WARN_LIMIT
,
1368 .alarm
= "min_alarm",
1369 .sbit
= PB_TEMP_UT_WARNING
,
1371 .reg
= PMBUS_UT_FAULT_LIMIT
,
1374 .alarm
= "lcrit_alarm",
1375 .sbit
= PB_TEMP_UT_FAULT
,
1377 .reg
= PMBUS_OT_WARN_LIMIT
,
1379 .alarm
= "max_alarm",
1380 .sbit
= PB_TEMP_OT_WARNING
,
1382 .reg
= PMBUS_OT_FAULT_LIMIT
,
1384 .alarm
= "crit_alarm",
1385 .sbit
= PB_TEMP_OT_FAULT
,
1387 .reg
= PMBUS_VIRT_READ_TEMP_MIN
,
1390 .reg
= PMBUS_VIRT_READ_TEMP_AVG
,
1393 .reg
= PMBUS_VIRT_READ_TEMP_MAX
,
1396 .reg
= PMBUS_VIRT_RESET_TEMP_HISTORY
,
1397 .attr
= "reset_history",
1401 static const struct pmbus_limit_attr temp_limit_attrs2
[] = {
1403 .reg
= PMBUS_UT_WARN_LIMIT
,
1406 .alarm
= "min_alarm",
1407 .sbit
= PB_TEMP_UT_WARNING
,
1409 .reg
= PMBUS_UT_FAULT_LIMIT
,
1412 .alarm
= "lcrit_alarm",
1413 .sbit
= PB_TEMP_UT_FAULT
,
1415 .reg
= PMBUS_OT_WARN_LIMIT
,
1417 .alarm
= "max_alarm",
1418 .sbit
= PB_TEMP_OT_WARNING
,
1420 .reg
= PMBUS_OT_FAULT_LIMIT
,
1422 .alarm
= "crit_alarm",
1423 .sbit
= PB_TEMP_OT_FAULT
,
1425 .reg
= PMBUS_VIRT_READ_TEMP2_MIN
,
1428 .reg
= PMBUS_VIRT_READ_TEMP2_AVG
,
1431 .reg
= PMBUS_VIRT_READ_TEMP2_MAX
,
1434 .reg
= PMBUS_VIRT_RESET_TEMP2_HISTORY
,
1435 .attr
= "reset_history",
1439 static const struct pmbus_limit_attr temp_limit_attrs3
[] = {
1441 .reg
= PMBUS_UT_WARN_LIMIT
,
1444 .alarm
= "min_alarm",
1445 .sbit
= PB_TEMP_UT_WARNING
,
1447 .reg
= PMBUS_UT_FAULT_LIMIT
,
1450 .alarm
= "lcrit_alarm",
1451 .sbit
= PB_TEMP_UT_FAULT
,
1453 .reg
= PMBUS_OT_WARN_LIMIT
,
1455 .alarm
= "max_alarm",
1456 .sbit
= PB_TEMP_OT_WARNING
,
1458 .reg
= PMBUS_OT_FAULT_LIMIT
,
1460 .alarm
= "crit_alarm",
1461 .sbit
= PB_TEMP_OT_FAULT
,
1465 static const struct pmbus_sensor_attr temp_attributes
[] = {
1467 .reg
= PMBUS_READ_TEMPERATURE_1
,
1468 .class = PSC_TEMPERATURE
,
1472 .func
= PMBUS_HAVE_TEMP
,
1473 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1474 .sbase
= PB_STATUS_TEMP_BASE
,
1475 .gbit
= PB_STATUS_TEMPERATURE
,
1476 .limit
= temp_limit_attrs
,
1477 .nlimit
= ARRAY_SIZE(temp_limit_attrs
),
1479 .reg
= PMBUS_READ_TEMPERATURE_2
,
1480 .class = PSC_TEMPERATURE
,
1484 .func
= PMBUS_HAVE_TEMP2
,
1485 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1486 .sbase
= PB_STATUS_TEMP_BASE
,
1487 .gbit
= PB_STATUS_TEMPERATURE
,
1488 .limit
= temp_limit_attrs2
,
1489 .nlimit
= ARRAY_SIZE(temp_limit_attrs2
),
1491 .reg
= PMBUS_READ_TEMPERATURE_3
,
1492 .class = PSC_TEMPERATURE
,
1496 .func
= PMBUS_HAVE_TEMP3
,
1497 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1498 .sbase
= PB_STATUS_TEMP_BASE
,
1499 .gbit
= PB_STATUS_TEMPERATURE
,
1500 .limit
= temp_limit_attrs3
,
1501 .nlimit
= ARRAY_SIZE(temp_limit_attrs3
),
1505 static const int pmbus_fan_registers
[] = {
1506 PMBUS_READ_FAN_SPEED_1
,
1507 PMBUS_READ_FAN_SPEED_2
,
1508 PMBUS_READ_FAN_SPEED_3
,
1509 PMBUS_READ_FAN_SPEED_4
1512 static const int pmbus_fan_config_registers
[] = {
1513 PMBUS_FAN_CONFIG_12
,
1514 PMBUS_FAN_CONFIG_12
,
1515 PMBUS_FAN_CONFIG_34
,
1519 static const int pmbus_fan_status_registers
[] = {
1520 PMBUS_STATUS_FAN_12
,
1521 PMBUS_STATUS_FAN_12
,
1522 PMBUS_STATUS_FAN_34
,
1526 static const u32 pmbus_fan_flags
[] = {
1533 static const u32 pmbus_fan_status_flags
[] = {
1534 PMBUS_HAVE_STATUS_FAN12
,
1535 PMBUS_HAVE_STATUS_FAN12
,
1536 PMBUS_HAVE_STATUS_FAN34
,
1537 PMBUS_HAVE_STATUS_FAN34
1541 static int pmbus_add_fan_attributes(struct i2c_client
*client
,
1542 struct pmbus_data
*data
)
1544 const struct pmbus_driver_info
*info
= data
->info
;
1549 for (page
= 0; page
< info
->pages
; page
++) {
1552 for (f
= 0; f
< ARRAY_SIZE(pmbus_fan_registers
); f
++) {
1555 if (!(info
->func
[page
] & pmbus_fan_flags
[f
]))
1558 if (!pmbus_check_word_register(client
, page
,
1559 pmbus_fan_registers
[f
]))
1563 * Skip fan if not installed.
1564 * Each fan configuration register covers multiple fans,
1565 * so we have to do some magic.
1567 regval
= _pmbus_read_byte_data(client
, page
,
1568 pmbus_fan_config_registers
[f
]);
1570 (!(regval
& (PB_FAN_1_INSTALLED
>> ((f
& 1) * 4)))))
1573 if (pmbus_add_sensor(data
, "fan", "input", index
,
1574 page
, pmbus_fan_registers
[f
],
1575 PSC_FAN
, true, true) == NULL
)
1579 * Each fan status register covers multiple fans,
1580 * so we have to do some magic.
1582 if ((info
->func
[page
] & pmbus_fan_status_flags
[f
]) &&
1583 pmbus_check_byte_register(client
,
1584 page
, pmbus_fan_status_registers
[f
])) {
1587 if (f
> 1) /* fan 3, 4 */
1588 base
= PB_STATUS_FAN34_BASE
+ page
;
1590 base
= PB_STATUS_FAN_BASE
+ page
;
1591 ret
= pmbus_add_boolean(data
, "fan",
1592 "alarm", index
, NULL
, NULL
, base
,
1593 PB_FAN_FAN1_WARNING
>> (f
& 1));
1596 ret
= pmbus_add_boolean(data
, "fan",
1597 "fault", index
, NULL
, NULL
, base
,
1598 PB_FAN_FAN1_FAULT
>> (f
& 1));
1608 static int pmbus_find_attributes(struct i2c_client
*client
,
1609 struct pmbus_data
*data
)
1613 /* Voltage sensors */
1614 ret
= pmbus_add_sensor_attrs(client
, data
, "in", voltage_attributes
,
1615 ARRAY_SIZE(voltage_attributes
));
1619 /* Current sensors */
1620 ret
= pmbus_add_sensor_attrs(client
, data
, "curr", current_attributes
,
1621 ARRAY_SIZE(current_attributes
));
1626 ret
= pmbus_add_sensor_attrs(client
, data
, "power", power_attributes
,
1627 ARRAY_SIZE(power_attributes
));
1631 /* Temperature sensors */
1632 ret
= pmbus_add_sensor_attrs(client
, data
, "temp", temp_attributes
,
1633 ARRAY_SIZE(temp_attributes
));
1638 ret
= pmbus_add_fan_attributes(client
, data
);
1643 * Identify chip parameters.
1644 * This function is called for all chips.
1646 static int pmbus_identify_common(struct i2c_client
*client
,
1647 struct pmbus_data
*data
, int page
)
1651 if (pmbus_check_byte_register(client
, page
, PMBUS_VOUT_MODE
))
1652 vout_mode
= _pmbus_read_byte_data(client
, page
,
1654 if (vout_mode
>= 0 && vout_mode
!= 0xff) {
1656 * Not all chips support the VOUT_MODE command,
1657 * so a failure to read it is not an error.
1659 switch (vout_mode
>> 5) {
1660 case 0: /* linear mode */
1661 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != linear
)
1664 data
->exponent
[page
] = ((s8
)(vout_mode
<< 3)) >> 3;
1666 case 1: /* VID mode */
1667 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != vid
)
1670 case 2: /* direct mode */
1671 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != direct
)
1679 pmbus_clear_fault_page(client
, page
);
1683 static int pmbus_init_common(struct i2c_client
*client
, struct pmbus_data
*data
,
1684 struct pmbus_driver_info
*info
)
1686 struct device
*dev
= &client
->dev
;
1690 * Some PMBus chips don't support PMBUS_STATUS_BYTE, so try
1691 * to use PMBUS_STATUS_WORD instead if that is the case.
1692 * Bail out if both registers are not supported.
1694 data
->status_register
= PMBUS_STATUS_BYTE
;
1695 ret
= i2c_smbus_read_byte_data(client
, PMBUS_STATUS_BYTE
);
1696 if (ret
< 0 || ret
== 0xff) {
1697 data
->status_register
= PMBUS_STATUS_WORD
;
1698 ret
= i2c_smbus_read_word_data(client
, PMBUS_STATUS_WORD
);
1699 if (ret
< 0 || ret
== 0xffff) {
1700 dev_err(dev
, "PMBus status register not found\n");
1705 pmbus_clear_faults(client
);
1707 if (info
->identify
) {
1708 ret
= (*info
->identify
)(client
, info
);
1710 dev_err(dev
, "Chip identification failed\n");
1715 if (info
->pages
<= 0 || info
->pages
> PMBUS_PAGES
) {
1716 dev_err(dev
, "Bad number of PMBus pages: %d\n", info
->pages
);
1720 for (page
= 0; page
< info
->pages
; page
++) {
1721 ret
= pmbus_identify_common(client
, data
, page
);
1723 dev_err(dev
, "Failed to identify chip capabilities\n");
1730 int pmbus_do_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
,
1731 struct pmbus_driver_info
*info
)
1733 struct device
*dev
= &client
->dev
;
1734 const struct pmbus_platform_data
*pdata
= dev_get_platdata(dev
);
1735 struct pmbus_data
*data
;
1741 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WRITE_BYTE
1742 | I2C_FUNC_SMBUS_BYTE_DATA
1743 | I2C_FUNC_SMBUS_WORD_DATA
))
1746 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
1750 i2c_set_clientdata(client
, data
);
1751 mutex_init(&data
->update_lock
);
1755 data
->flags
= pdata
->flags
;
1758 ret
= pmbus_init_common(client
, data
, info
);
1762 ret
= pmbus_find_attributes(client
, data
);
1767 * If there are no attributes, something is wrong.
1768 * Bail out instead of trying to register nothing.
1770 if (!data
->num_attributes
) {
1771 dev_err(dev
, "No attributes found\n");
1776 data
->groups
[0] = &data
->group
;
1777 data
->hwmon_dev
= hwmon_device_register_with_groups(dev
, client
->name
,
1778 data
, data
->groups
);
1779 if (IS_ERR(data
->hwmon_dev
)) {
1780 ret
= PTR_ERR(data
->hwmon_dev
);
1781 dev_err(dev
, "Failed to register hwmon device\n");
1787 kfree(data
->group
.attrs
);
1790 EXPORT_SYMBOL_GPL(pmbus_do_probe
);
1792 int pmbus_do_remove(struct i2c_client
*client
)
1794 struct pmbus_data
*data
= i2c_get_clientdata(client
);
1795 hwmon_device_unregister(data
->hwmon_dev
);
1796 kfree(data
->group
.attrs
);
1799 EXPORT_SYMBOL_GPL(pmbus_do_remove
);
1801 MODULE_AUTHOR("Guenter Roeck");
1802 MODULE_DESCRIPTION("PMBus core driver");
1803 MODULE_LICENSE("GPL");