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
; /* linear mode: exponent for output voltages */
95 const struct pmbus_driver_info
*info
;
99 struct attribute_group group
;
101 struct pmbus_sensor
*sensors
;
103 struct mutex update_lock
;
105 unsigned long last_updated
; /* in jiffies */
108 * A single status register covers multiple attributes,
109 * so we keep them all together.
111 u8 status
[PB_NUM_STATUS_REG
];
117 void pmbus_clear_cache(struct i2c_client
*client
)
119 struct pmbus_data
*data
= i2c_get_clientdata(client
);
123 EXPORT_SYMBOL_GPL(pmbus_clear_cache
);
125 int pmbus_set_page(struct i2c_client
*client
, u8 page
)
127 struct pmbus_data
*data
= i2c_get_clientdata(client
);
131 if (page
!= data
->currpage
) {
132 rv
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, page
);
133 newpage
= i2c_smbus_read_byte_data(client
, PMBUS_PAGE
);
137 data
->currpage
= page
;
141 EXPORT_SYMBOL_GPL(pmbus_set_page
);
143 int pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
148 rv
= pmbus_set_page(client
, page
);
153 return i2c_smbus_write_byte(client
, value
);
155 EXPORT_SYMBOL_GPL(pmbus_write_byte
);
158 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
159 * a device specific mapping funcion exists and calls it if necessary.
161 static int _pmbus_write_byte(struct i2c_client
*client
, int page
, u8 value
)
163 struct pmbus_data
*data
= i2c_get_clientdata(client
);
164 const struct pmbus_driver_info
*info
= data
->info
;
167 if (info
->write_byte
) {
168 status
= info
->write_byte(client
, page
, value
);
169 if (status
!= -ENODATA
)
172 return pmbus_write_byte(client
, page
, value
);
175 int pmbus_write_word_data(struct i2c_client
*client
, u8 page
, u8 reg
, u16 word
)
179 rv
= pmbus_set_page(client
, page
);
183 return i2c_smbus_write_word_data(client
, reg
, word
);
185 EXPORT_SYMBOL_GPL(pmbus_write_word_data
);
188 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
189 * a device specific mapping function exists and calls it if necessary.
191 static int _pmbus_write_word_data(struct i2c_client
*client
, int page
, int reg
,
194 struct pmbus_data
*data
= i2c_get_clientdata(client
);
195 const struct pmbus_driver_info
*info
= data
->info
;
198 if (info
->write_word_data
) {
199 status
= info
->write_word_data(client
, page
, reg
, word
);
200 if (status
!= -ENODATA
)
203 if (reg
>= PMBUS_VIRT_BASE
)
205 return pmbus_write_word_data(client
, page
, reg
, word
);
208 int pmbus_read_word_data(struct i2c_client
*client
, u8 page
, u8 reg
)
212 rv
= pmbus_set_page(client
, page
);
216 return i2c_smbus_read_word_data(client
, reg
);
218 EXPORT_SYMBOL_GPL(pmbus_read_word_data
);
221 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
222 * a device specific mapping function exists and calls it if necessary.
224 static int _pmbus_read_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
->read_word_data
) {
231 status
= info
->read_word_data(client
, page
, reg
);
232 if (status
!= -ENODATA
)
235 if (reg
>= PMBUS_VIRT_BASE
)
237 return pmbus_read_word_data(client
, page
, reg
);
240 int pmbus_read_byte_data(struct i2c_client
*client
, int page
, u8 reg
)
245 rv
= pmbus_set_page(client
, page
);
250 return i2c_smbus_read_byte_data(client
, reg
);
252 EXPORT_SYMBOL_GPL(pmbus_read_byte_data
);
255 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
256 * a device specific mapping function exists and calls it if necessary.
258 static int _pmbus_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
260 struct pmbus_data
*data
= i2c_get_clientdata(client
);
261 const struct pmbus_driver_info
*info
= data
->info
;
264 if (info
->read_byte_data
) {
265 status
= info
->read_byte_data(client
, page
, reg
);
266 if (status
!= -ENODATA
)
269 return pmbus_read_byte_data(client
, page
, reg
);
272 static void pmbus_clear_fault_page(struct i2c_client
*client
, int page
)
274 _pmbus_write_byte(client
, page
, PMBUS_CLEAR_FAULTS
);
277 void pmbus_clear_faults(struct i2c_client
*client
)
279 struct pmbus_data
*data
= i2c_get_clientdata(client
);
282 for (i
= 0; i
< data
->info
->pages
; i
++)
283 pmbus_clear_fault_page(client
, i
);
285 EXPORT_SYMBOL_GPL(pmbus_clear_faults
);
287 static int pmbus_check_status_cml(struct i2c_client
*client
)
289 struct pmbus_data
*data
= i2c_get_clientdata(client
);
292 status
= _pmbus_read_byte_data(client
, -1, data
->status_register
);
293 if (status
< 0 || (status
& PB_STATUS_CML
)) {
294 status2
= _pmbus_read_byte_data(client
, -1, PMBUS_STATUS_CML
);
295 if (status2
< 0 || (status2
& PB_CML_FAULT_INVALID_COMMAND
))
301 static bool pmbus_check_register(struct i2c_client
*client
,
302 int (*func
)(struct i2c_client
*client
,
307 struct pmbus_data
*data
= i2c_get_clientdata(client
);
309 rv
= func(client
, page
, reg
);
310 if (rv
>= 0 && !(data
->flags
& PMBUS_SKIP_STATUS_CHECK
))
311 rv
= pmbus_check_status_cml(client
);
312 pmbus_clear_fault_page(client
, -1);
316 bool pmbus_check_byte_register(struct i2c_client
*client
, int page
, int reg
)
318 return pmbus_check_register(client
, _pmbus_read_byte_data
, page
, reg
);
320 EXPORT_SYMBOL_GPL(pmbus_check_byte_register
);
322 bool pmbus_check_word_register(struct i2c_client
*client
, int page
, int reg
)
324 return pmbus_check_register(client
, _pmbus_read_word_data
, page
, reg
);
326 EXPORT_SYMBOL_GPL(pmbus_check_word_register
);
328 const struct pmbus_driver_info
*pmbus_get_driver_info(struct i2c_client
*client
)
330 struct pmbus_data
*data
= i2c_get_clientdata(client
);
334 EXPORT_SYMBOL_GPL(pmbus_get_driver_info
);
336 static struct _pmbus_status
{
341 { PMBUS_HAVE_STATUS_VOUT
, PB_STATUS_VOUT_BASE
, PMBUS_STATUS_VOUT
},
342 { PMBUS_HAVE_STATUS_IOUT
, PB_STATUS_IOUT_BASE
, PMBUS_STATUS_IOUT
},
343 { PMBUS_HAVE_STATUS_TEMP
, PB_STATUS_TEMP_BASE
,
344 PMBUS_STATUS_TEMPERATURE
},
345 { PMBUS_HAVE_STATUS_FAN12
, PB_STATUS_FAN_BASE
, PMBUS_STATUS_FAN_12
},
346 { PMBUS_HAVE_STATUS_FAN34
, PB_STATUS_FAN34_BASE
, PMBUS_STATUS_FAN_34
},
349 static struct pmbus_data
*pmbus_update_device(struct device
*dev
)
351 struct i2c_client
*client
= to_i2c_client(dev
);
352 struct pmbus_data
*data
= i2c_get_clientdata(client
);
353 const struct pmbus_driver_info
*info
= data
->info
;
354 struct pmbus_sensor
*sensor
;
356 mutex_lock(&data
->update_lock
);
357 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
360 for (i
= 0; i
< info
->pages
; i
++) {
361 data
->status
[PB_STATUS_BASE
+ i
]
362 = _pmbus_read_byte_data(client
, i
,
363 data
->status_register
);
364 for (j
= 0; j
< ARRAY_SIZE(pmbus_status
); j
++) {
365 struct _pmbus_status
*s
= &pmbus_status
[j
];
367 if (!(info
->func
[i
] & s
->func
))
369 data
->status
[s
->base
+ i
]
370 = _pmbus_read_byte_data(client
, i
,
375 if (info
->func
[0] & PMBUS_HAVE_STATUS_INPUT
)
376 data
->status
[PB_STATUS_INPUT_BASE
]
377 = _pmbus_read_byte_data(client
, 0,
380 if (info
->func
[0] & PMBUS_HAVE_STATUS_VMON
)
381 data
->status
[PB_STATUS_VMON_BASE
]
382 = _pmbus_read_byte_data(client
, 0,
383 PMBUS_VIRT_STATUS_VMON
);
385 for (sensor
= data
->sensors
; sensor
; sensor
= sensor
->next
) {
386 if (!data
->valid
|| sensor
->update
)
388 = _pmbus_read_word_data(client
,
392 pmbus_clear_faults(client
);
393 data
->last_updated
= jiffies
;
396 mutex_unlock(&data
->update_lock
);
401 * Convert linear sensor values to milli- or micro-units
402 * depending on sensor type.
404 static long pmbus_reg2data_linear(struct pmbus_data
*data
,
405 struct pmbus_sensor
*sensor
)
411 if (sensor
->class == PSC_VOLTAGE_OUT
) { /* LINEAR16 */
412 exponent
= data
->exponent
;
413 mantissa
= (u16
) sensor
->data
;
414 } else { /* LINEAR11 */
415 exponent
= ((s16
)sensor
->data
) >> 11;
416 mantissa
= ((s16
)((sensor
->data
& 0x7ff) << 5)) >> 5;
421 /* scale result to milli-units for all sensors except fans */
422 if (sensor
->class != PSC_FAN
)
425 /* scale result to micro-units for power sensors */
426 if (sensor
->class == PSC_POWER
)
438 * Convert direct sensor values to milli- or micro-units
439 * depending on sensor type.
441 static long pmbus_reg2data_direct(struct pmbus_data
*data
,
442 struct pmbus_sensor
*sensor
)
444 long val
= (s16
) sensor
->data
;
447 m
= data
->info
->m
[sensor
->class];
448 b
= data
->info
->b
[sensor
->class];
449 R
= data
->info
->R
[sensor
->class];
454 /* X = 1/m * (Y * 10^-R - b) */
456 /* scale result to milli-units for everything but fans */
457 if (sensor
->class != PSC_FAN
) {
462 /* scale result to micro-units for power sensors */
463 if (sensor
->class == PSC_POWER
) {
473 val
= DIV_ROUND_CLOSEST(val
, 10);
477 return (val
- b
) / m
;
481 * Convert VID sensor values to milli- or micro-units
482 * depending on sensor type.
483 * We currently only support VR11.
485 static long pmbus_reg2data_vid(struct pmbus_data
*data
,
486 struct pmbus_sensor
*sensor
)
488 long val
= sensor
->data
;
490 if (val
< 0x02 || val
> 0xb2)
492 return DIV_ROUND_CLOSEST(160000 - (val
- 2) * 625, 100);
495 static long pmbus_reg2data(struct pmbus_data
*data
, struct pmbus_sensor
*sensor
)
499 switch (data
->info
->format
[sensor
->class]) {
501 val
= pmbus_reg2data_direct(data
, sensor
);
504 val
= pmbus_reg2data_vid(data
, sensor
);
508 val
= pmbus_reg2data_linear(data
, sensor
);
514 #define MAX_MANTISSA (1023 * 1000)
515 #define MIN_MANTISSA (511 * 1000)
517 static u16
pmbus_data2reg_linear(struct pmbus_data
*data
,
518 enum pmbus_sensor_classes
class, long val
)
520 s16 exponent
= 0, mantissa
;
521 bool negative
= false;
527 if (class == PSC_VOLTAGE_OUT
) {
528 /* LINEAR16 does not support negative voltages */
533 * For a static exponents, we don't have a choice
534 * but to adjust the value to it.
536 if (data
->exponent
< 0)
537 val
<<= -data
->exponent
;
539 val
>>= data
->exponent
;
540 val
= DIV_ROUND_CLOSEST(val
, 1000);
549 /* Power is in uW. Convert to mW before converting. */
550 if (class == PSC_POWER
)
551 val
= DIV_ROUND_CLOSEST(val
, 1000L);
554 * For simplicity, convert fan data to milli-units
555 * before calculating the exponent.
557 if (class == PSC_FAN
)
560 /* Reduce large mantissa until it fits into 10 bit */
561 while (val
>= MAX_MANTISSA
&& exponent
< 15) {
565 /* Increase small mantissa to improve precision */
566 while (val
< MIN_MANTISSA
&& exponent
> -15) {
571 /* Convert mantissa from milli-units to units */
572 mantissa
= DIV_ROUND_CLOSEST(val
, 1000);
574 /* Ensure that resulting number is within range */
575 if (mantissa
> 0x3ff)
580 mantissa
= -mantissa
;
582 /* Convert to 5 bit exponent, 11 bit mantissa */
583 return (mantissa
& 0x7ff) | ((exponent
<< 11) & 0xf800);
586 static u16
pmbus_data2reg_direct(struct pmbus_data
*data
,
587 enum pmbus_sensor_classes
class, long val
)
591 m
= data
->info
->m
[class];
592 b
= data
->info
->b
[class];
593 R
= data
->info
->R
[class];
595 /* Power is in uW. Adjust R and b. */
596 if (class == PSC_POWER
) {
601 /* Calculate Y = (m * X + b) * 10^R */
602 if (class != PSC_FAN
) {
603 R
-= 3; /* Adjust R and b for data in milli-units */
613 val
= DIV_ROUND_CLOSEST(val
, 10);
620 static u16
pmbus_data2reg_vid(struct pmbus_data
*data
,
621 enum pmbus_sensor_classes
class, long val
)
623 val
= clamp_val(val
, 500, 1600);
625 return 2 + DIV_ROUND_CLOSEST((1600 - val
) * 100, 625);
628 static u16
pmbus_data2reg(struct pmbus_data
*data
,
629 enum pmbus_sensor_classes
class, long val
)
633 switch (data
->info
->format
[class]) {
635 regval
= pmbus_data2reg_direct(data
, class, val
);
638 regval
= pmbus_data2reg_vid(data
, class, val
);
642 regval
= pmbus_data2reg_linear(data
, class, val
);
649 * Return boolean calculated from converted data.
650 * <index> defines a status register index and mask.
651 * The mask is in the lower 8 bits, the register index is in bits 8..23.
653 * The associated pmbus_boolean structure contains optional pointers to two
654 * sensor attributes. If specified, those attributes are compared against each
655 * other to determine if a limit has been exceeded.
657 * If the sensor attribute pointers are NULL, the function returns true if
658 * (status[reg] & mask) is true.
660 * If sensor attribute pointers are provided, a comparison against a specified
661 * limit has to be performed to determine the boolean result.
662 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
663 * sensor values referenced by sensor attribute pointers s1 and s2).
665 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
666 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
668 * If a negative value is stored in any of the referenced registers, this value
669 * reflects an error code which will be returned.
671 static int pmbus_get_boolean(struct pmbus_data
*data
, struct pmbus_boolean
*b
,
674 struct pmbus_sensor
*s1
= b
->s1
;
675 struct pmbus_sensor
*s2
= b
->s2
;
676 u16 reg
= (index
>> 8) & 0xffff;
677 u8 mask
= index
& 0xff;
681 status
= data
->status
[reg
];
685 regval
= status
& mask
;
688 } else if (!s1
|| !s2
) {
699 v1
= pmbus_reg2data(data
, s1
);
700 v2
= pmbus_reg2data(data
, s2
);
701 ret
= !!(regval
&& v1
>= v2
);
706 static ssize_t
pmbus_show_boolean(struct device
*dev
,
707 struct device_attribute
*da
, char *buf
)
709 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
710 struct pmbus_boolean
*boolean
= to_pmbus_boolean(attr
);
711 struct pmbus_data
*data
= pmbus_update_device(dev
);
714 val
= pmbus_get_boolean(data
, boolean
, attr
->index
);
717 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
720 static ssize_t
pmbus_show_sensor(struct device
*dev
,
721 struct device_attribute
*devattr
, char *buf
)
723 struct pmbus_data
*data
= pmbus_update_device(dev
);
724 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
726 if (sensor
->data
< 0)
729 return snprintf(buf
, PAGE_SIZE
, "%ld\n", pmbus_reg2data(data
, sensor
));
732 static ssize_t
pmbus_set_sensor(struct device
*dev
,
733 struct device_attribute
*devattr
,
734 const char *buf
, size_t count
)
736 struct i2c_client
*client
= to_i2c_client(dev
);
737 struct pmbus_data
*data
= i2c_get_clientdata(client
);
738 struct pmbus_sensor
*sensor
= to_pmbus_sensor(devattr
);
744 if (kstrtol(buf
, 10, &val
) < 0)
747 mutex_lock(&data
->update_lock
);
748 regval
= pmbus_data2reg(data
, sensor
->class, val
);
749 ret
= _pmbus_write_word_data(client
, sensor
->page
, sensor
->reg
, regval
);
753 sensor
->data
= regval
;
754 mutex_unlock(&data
->update_lock
);
758 static ssize_t
pmbus_show_label(struct device
*dev
,
759 struct device_attribute
*da
, char *buf
)
761 struct pmbus_label
*label
= to_pmbus_label(da
);
763 return snprintf(buf
, PAGE_SIZE
, "%s\n", label
->label
);
766 static int pmbus_add_attribute(struct pmbus_data
*data
, struct attribute
*attr
)
768 if (data
->num_attributes
>= data
->max_attributes
- 1) {
769 int new_max_attrs
= data
->max_attributes
+ PMBUS_ATTR_ALLOC_SIZE
;
770 void *new_attrs
= krealloc(data
->group
.attrs
,
771 new_max_attrs
* sizeof(void *),
775 data
->group
.attrs
= new_attrs
;
776 data
->max_attributes
= new_max_attrs
;
779 data
->group
.attrs
[data
->num_attributes
++] = attr
;
780 data
->group
.attrs
[data
->num_attributes
] = NULL
;
784 static void pmbus_dev_attr_init(struct device_attribute
*dev_attr
,
787 ssize_t (*show
)(struct device
*dev
,
788 struct device_attribute
*attr
,
790 ssize_t (*store
)(struct device
*dev
,
791 struct device_attribute
*attr
,
792 const char *buf
, size_t count
))
794 sysfs_attr_init(&dev_attr
->attr
);
795 dev_attr
->attr
.name
= name
;
796 dev_attr
->attr
.mode
= mode
;
797 dev_attr
->show
= show
;
798 dev_attr
->store
= store
;
801 static void pmbus_attr_init(struct sensor_device_attribute
*a
,
804 ssize_t (*show
)(struct device
*dev
,
805 struct device_attribute
*attr
,
807 ssize_t (*store
)(struct device
*dev
,
808 struct device_attribute
*attr
,
809 const char *buf
, size_t count
),
812 pmbus_dev_attr_init(&a
->dev_attr
, name
, mode
, show
, store
);
816 static int pmbus_add_boolean(struct pmbus_data
*data
,
817 const char *name
, const char *type
, int seq
,
818 struct pmbus_sensor
*s1
,
819 struct pmbus_sensor
*s2
,
822 struct pmbus_boolean
*boolean
;
823 struct sensor_device_attribute
*a
;
825 boolean
= devm_kzalloc(data
->dev
, sizeof(*boolean
), GFP_KERNEL
);
829 a
= &boolean
->attribute
;
831 snprintf(boolean
->name
, sizeof(boolean
->name
), "%s%d_%s",
835 pmbus_attr_init(a
, boolean
->name
, S_IRUGO
, pmbus_show_boolean
, NULL
,
838 return pmbus_add_attribute(data
, &a
->dev_attr
.attr
);
841 static struct pmbus_sensor
*pmbus_add_sensor(struct pmbus_data
*data
,
842 const char *name
, const char *type
,
843 int seq
, int page
, int reg
,
844 enum pmbus_sensor_classes
class,
845 bool update
, bool readonly
)
847 struct pmbus_sensor
*sensor
;
848 struct device_attribute
*a
;
850 sensor
= devm_kzalloc(data
->dev
, sizeof(*sensor
), GFP_KERNEL
);
853 a
= &sensor
->attribute
;
855 snprintf(sensor
->name
, sizeof(sensor
->name
), "%s%d_%s",
859 sensor
->class = class;
860 sensor
->update
= update
;
861 pmbus_dev_attr_init(a
, sensor
->name
,
862 readonly
? S_IRUGO
: S_IRUGO
| S_IWUSR
,
863 pmbus_show_sensor
, pmbus_set_sensor
);
865 if (pmbus_add_attribute(data
, &a
->attr
))
868 sensor
->next
= data
->sensors
;
869 data
->sensors
= sensor
;
874 static int pmbus_add_label(struct pmbus_data
*data
,
875 const char *name
, int seq
,
876 const char *lstring
, int index
)
878 struct pmbus_label
*label
;
879 struct device_attribute
*a
;
881 label
= devm_kzalloc(data
->dev
, sizeof(*label
), GFP_KERNEL
);
885 a
= &label
->attribute
;
887 snprintf(label
->name
, sizeof(label
->name
), "%s%d_label", name
, seq
);
889 strncpy(label
->label
, lstring
, sizeof(label
->label
) - 1);
891 snprintf(label
->label
, sizeof(label
->label
), "%s%d", lstring
,
894 pmbus_dev_attr_init(a
, label
->name
, S_IRUGO
, pmbus_show_label
, NULL
);
895 return pmbus_add_attribute(data
, &a
->attr
);
899 * Search for attributes. Allocate sensors, booleans, and labels as needed.
903 * The pmbus_limit_attr structure describes a single limit attribute
904 * and its associated alarm attribute.
906 struct pmbus_limit_attr
{
907 u16 reg
; /* Limit register */
908 u16 sbit
; /* Alarm attribute status bit */
909 bool update
; /* True if register needs updates */
910 bool low
; /* True if low limit; for limits with compare
912 const char *attr
; /* Attribute name */
913 const char *alarm
; /* Alarm attribute name */
917 * The pmbus_sensor_attr structure describes one sensor attribute. This
918 * description includes a reference to the associated limit attributes.
920 struct pmbus_sensor_attr
{
921 u16 reg
; /* sensor register */
922 u8 gbit
; /* generic status bit */
923 u8 nlimit
; /* # of limit registers */
924 enum pmbus_sensor_classes
class;/* sensor class */
925 const char *label
; /* sensor label */
926 bool paged
; /* true if paged sensor */
927 bool update
; /* true if update needed */
928 bool compare
; /* true if compare function needed */
929 u32 func
; /* sensor mask */
930 u32 sfunc
; /* sensor status mask */
931 int sbase
; /* status base register */
932 const struct pmbus_limit_attr
*limit
;/* limit registers */
936 * Add a set of limit attributes and, if supported, the associated
938 * returns 0 if no alarm register found, 1 if an alarm register was found,
941 static int pmbus_add_limit_attrs(struct i2c_client
*client
,
942 struct pmbus_data
*data
,
943 const struct pmbus_driver_info
*info
,
944 const char *name
, int index
, int page
,
945 struct pmbus_sensor
*base
,
946 const struct pmbus_sensor_attr
*attr
)
948 const struct pmbus_limit_attr
*l
= attr
->limit
;
949 int nlimit
= attr
->nlimit
;
952 struct pmbus_sensor
*curr
;
954 for (i
= 0; i
< nlimit
; i
++) {
955 if (pmbus_check_word_register(client
, page
, l
->reg
)) {
956 curr
= pmbus_add_sensor(data
, name
, l
->attr
, index
,
957 page
, l
->reg
, attr
->class,
958 attr
->update
|| l
->update
,
962 if (l
->sbit
&& (info
->func
[page
] & attr
->sfunc
)) {
963 ret
= pmbus_add_boolean(data
, name
,
965 attr
->compare
? l
->low
? curr
: base
967 attr
->compare
? l
->low
? base
: curr
969 attr
->sbase
+ page
, l
->sbit
);
980 static int pmbus_add_sensor_attrs_one(struct i2c_client
*client
,
981 struct pmbus_data
*data
,
982 const struct pmbus_driver_info
*info
,
985 const struct pmbus_sensor_attr
*attr
)
987 struct pmbus_sensor
*base
;
991 ret
= pmbus_add_label(data
, name
, index
, attr
->label
,
992 attr
->paged
? page
+ 1 : 0);
996 base
= pmbus_add_sensor(data
, name
, "input", index
, page
, attr
->reg
,
997 attr
->class, true, true);
1001 ret
= pmbus_add_limit_attrs(client
, data
, info
, name
,
1002 index
, page
, base
, attr
);
1006 * Add generic alarm attribute only if there are no individual
1007 * alarm attributes, if there is a global alarm bit, and if
1008 * the generic status register for this page is accessible.
1010 if (!ret
&& attr
->gbit
&&
1011 pmbus_check_byte_register(client
, page
,
1012 data
->status_register
)) {
1013 ret
= pmbus_add_boolean(data
, name
, "alarm", index
,
1015 PB_STATUS_BASE
+ page
,
1024 static int pmbus_add_sensor_attrs(struct i2c_client
*client
,
1025 struct pmbus_data
*data
,
1027 const struct pmbus_sensor_attr
*attrs
,
1030 const struct pmbus_driver_info
*info
= data
->info
;
1035 for (i
= 0; i
< nattrs
; i
++) {
1038 pages
= attrs
->paged
? info
->pages
: 1;
1039 for (page
= 0; page
< pages
; page
++) {
1040 if (!(info
->func
[page
] & attrs
->func
))
1042 ret
= pmbus_add_sensor_attrs_one(client
, data
, info
,
1054 static const struct pmbus_limit_attr vin_limit_attrs
[] = {
1056 .reg
= PMBUS_VIN_UV_WARN_LIMIT
,
1058 .alarm
= "min_alarm",
1059 .sbit
= PB_VOLTAGE_UV_WARNING
,
1061 .reg
= PMBUS_VIN_UV_FAULT_LIMIT
,
1063 .alarm
= "lcrit_alarm",
1064 .sbit
= PB_VOLTAGE_UV_FAULT
,
1066 .reg
= PMBUS_VIN_OV_WARN_LIMIT
,
1068 .alarm
= "max_alarm",
1069 .sbit
= PB_VOLTAGE_OV_WARNING
,
1071 .reg
= PMBUS_VIN_OV_FAULT_LIMIT
,
1073 .alarm
= "crit_alarm",
1074 .sbit
= PB_VOLTAGE_OV_FAULT
,
1076 .reg
= PMBUS_VIRT_READ_VIN_AVG
,
1080 .reg
= PMBUS_VIRT_READ_VIN_MIN
,
1084 .reg
= PMBUS_VIRT_READ_VIN_MAX
,
1088 .reg
= PMBUS_VIRT_RESET_VIN_HISTORY
,
1089 .attr
= "reset_history",
1093 static const struct pmbus_limit_attr vmon_limit_attrs
[] = {
1095 .reg
= PMBUS_VIRT_VMON_UV_WARN_LIMIT
,
1097 .alarm
= "min_alarm",
1098 .sbit
= PB_VOLTAGE_UV_WARNING
,
1100 .reg
= PMBUS_VIRT_VMON_UV_FAULT_LIMIT
,
1102 .alarm
= "lcrit_alarm",
1103 .sbit
= PB_VOLTAGE_UV_FAULT
,
1105 .reg
= PMBUS_VIRT_VMON_OV_WARN_LIMIT
,
1107 .alarm
= "max_alarm",
1108 .sbit
= PB_VOLTAGE_OV_WARNING
,
1110 .reg
= PMBUS_VIRT_VMON_OV_FAULT_LIMIT
,
1112 .alarm
= "crit_alarm",
1113 .sbit
= PB_VOLTAGE_OV_FAULT
,
1117 static const struct pmbus_limit_attr vout_limit_attrs
[] = {
1119 .reg
= PMBUS_VOUT_UV_WARN_LIMIT
,
1121 .alarm
= "min_alarm",
1122 .sbit
= PB_VOLTAGE_UV_WARNING
,
1124 .reg
= PMBUS_VOUT_UV_FAULT_LIMIT
,
1126 .alarm
= "lcrit_alarm",
1127 .sbit
= PB_VOLTAGE_UV_FAULT
,
1129 .reg
= PMBUS_VOUT_OV_WARN_LIMIT
,
1131 .alarm
= "max_alarm",
1132 .sbit
= PB_VOLTAGE_OV_WARNING
,
1134 .reg
= PMBUS_VOUT_OV_FAULT_LIMIT
,
1136 .alarm
= "crit_alarm",
1137 .sbit
= PB_VOLTAGE_OV_FAULT
,
1139 .reg
= PMBUS_VIRT_READ_VOUT_AVG
,
1143 .reg
= PMBUS_VIRT_READ_VOUT_MIN
,
1147 .reg
= PMBUS_VIRT_READ_VOUT_MAX
,
1151 .reg
= PMBUS_VIRT_RESET_VOUT_HISTORY
,
1152 .attr
= "reset_history",
1156 static const struct pmbus_sensor_attr voltage_attributes
[] = {
1158 .reg
= PMBUS_READ_VIN
,
1159 .class = PSC_VOLTAGE_IN
,
1161 .func
= PMBUS_HAVE_VIN
,
1162 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1163 .sbase
= PB_STATUS_INPUT_BASE
,
1164 .gbit
= PB_STATUS_VIN_UV
,
1165 .limit
= vin_limit_attrs
,
1166 .nlimit
= ARRAY_SIZE(vin_limit_attrs
),
1168 .reg
= PMBUS_VIRT_READ_VMON
,
1169 .class = PSC_VOLTAGE_IN
,
1171 .func
= PMBUS_HAVE_VMON
,
1172 .sfunc
= PMBUS_HAVE_STATUS_VMON
,
1173 .sbase
= PB_STATUS_VMON_BASE
,
1174 .limit
= vmon_limit_attrs
,
1175 .nlimit
= ARRAY_SIZE(vmon_limit_attrs
),
1177 .reg
= PMBUS_READ_VCAP
,
1178 .class = PSC_VOLTAGE_IN
,
1180 .func
= PMBUS_HAVE_VCAP
,
1182 .reg
= PMBUS_READ_VOUT
,
1183 .class = PSC_VOLTAGE_OUT
,
1186 .func
= PMBUS_HAVE_VOUT
,
1187 .sfunc
= PMBUS_HAVE_STATUS_VOUT
,
1188 .sbase
= PB_STATUS_VOUT_BASE
,
1189 .gbit
= PB_STATUS_VOUT_OV
,
1190 .limit
= vout_limit_attrs
,
1191 .nlimit
= ARRAY_SIZE(vout_limit_attrs
),
1195 /* Current attributes */
1197 static const struct pmbus_limit_attr iin_limit_attrs
[] = {
1199 .reg
= PMBUS_IIN_OC_WARN_LIMIT
,
1201 .alarm
= "max_alarm",
1202 .sbit
= PB_IIN_OC_WARNING
,
1204 .reg
= PMBUS_IIN_OC_FAULT_LIMIT
,
1206 .alarm
= "crit_alarm",
1207 .sbit
= PB_IIN_OC_FAULT
,
1209 .reg
= PMBUS_VIRT_READ_IIN_AVG
,
1213 .reg
= PMBUS_VIRT_READ_IIN_MIN
,
1217 .reg
= PMBUS_VIRT_READ_IIN_MAX
,
1221 .reg
= PMBUS_VIRT_RESET_IIN_HISTORY
,
1222 .attr
= "reset_history",
1226 static const struct pmbus_limit_attr iout_limit_attrs
[] = {
1228 .reg
= PMBUS_IOUT_OC_WARN_LIMIT
,
1230 .alarm
= "max_alarm",
1231 .sbit
= PB_IOUT_OC_WARNING
,
1233 .reg
= PMBUS_IOUT_UC_FAULT_LIMIT
,
1235 .alarm
= "lcrit_alarm",
1236 .sbit
= PB_IOUT_UC_FAULT
,
1238 .reg
= PMBUS_IOUT_OC_FAULT_LIMIT
,
1240 .alarm
= "crit_alarm",
1241 .sbit
= PB_IOUT_OC_FAULT
,
1243 .reg
= PMBUS_VIRT_READ_IOUT_AVG
,
1247 .reg
= PMBUS_VIRT_READ_IOUT_MIN
,
1251 .reg
= PMBUS_VIRT_READ_IOUT_MAX
,
1255 .reg
= PMBUS_VIRT_RESET_IOUT_HISTORY
,
1256 .attr
= "reset_history",
1260 static const struct pmbus_sensor_attr current_attributes
[] = {
1262 .reg
= PMBUS_READ_IIN
,
1263 .class = PSC_CURRENT_IN
,
1265 .func
= PMBUS_HAVE_IIN
,
1266 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1267 .sbase
= PB_STATUS_INPUT_BASE
,
1268 .limit
= iin_limit_attrs
,
1269 .nlimit
= ARRAY_SIZE(iin_limit_attrs
),
1271 .reg
= PMBUS_READ_IOUT
,
1272 .class = PSC_CURRENT_OUT
,
1275 .func
= PMBUS_HAVE_IOUT
,
1276 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1277 .sbase
= PB_STATUS_IOUT_BASE
,
1278 .gbit
= PB_STATUS_IOUT_OC
,
1279 .limit
= iout_limit_attrs
,
1280 .nlimit
= ARRAY_SIZE(iout_limit_attrs
),
1284 /* Power attributes */
1286 static const struct pmbus_limit_attr pin_limit_attrs
[] = {
1288 .reg
= PMBUS_PIN_OP_WARN_LIMIT
,
1291 .sbit
= PB_PIN_OP_WARNING
,
1293 .reg
= PMBUS_VIRT_READ_PIN_AVG
,
1297 .reg
= PMBUS_VIRT_READ_PIN_MAX
,
1299 .attr
= "input_highest",
1301 .reg
= PMBUS_VIRT_RESET_PIN_HISTORY
,
1302 .attr
= "reset_history",
1306 static const struct pmbus_limit_attr pout_limit_attrs
[] = {
1308 .reg
= PMBUS_POUT_MAX
,
1310 .alarm
= "cap_alarm",
1311 .sbit
= PB_POWER_LIMITING
,
1313 .reg
= PMBUS_POUT_OP_WARN_LIMIT
,
1315 .alarm
= "max_alarm",
1316 .sbit
= PB_POUT_OP_WARNING
,
1318 .reg
= PMBUS_POUT_OP_FAULT_LIMIT
,
1320 .alarm
= "crit_alarm",
1321 .sbit
= PB_POUT_OP_FAULT
,
1323 .reg
= PMBUS_VIRT_READ_POUT_AVG
,
1327 .reg
= PMBUS_VIRT_READ_POUT_MAX
,
1329 .attr
= "input_highest",
1331 .reg
= PMBUS_VIRT_RESET_POUT_HISTORY
,
1332 .attr
= "reset_history",
1336 static const struct pmbus_sensor_attr power_attributes
[] = {
1338 .reg
= PMBUS_READ_PIN
,
1341 .func
= PMBUS_HAVE_PIN
,
1342 .sfunc
= PMBUS_HAVE_STATUS_INPUT
,
1343 .sbase
= PB_STATUS_INPUT_BASE
,
1344 .limit
= pin_limit_attrs
,
1345 .nlimit
= ARRAY_SIZE(pin_limit_attrs
),
1347 .reg
= PMBUS_READ_POUT
,
1351 .func
= PMBUS_HAVE_POUT
,
1352 .sfunc
= PMBUS_HAVE_STATUS_IOUT
,
1353 .sbase
= PB_STATUS_IOUT_BASE
,
1354 .limit
= pout_limit_attrs
,
1355 .nlimit
= ARRAY_SIZE(pout_limit_attrs
),
1359 /* Temperature atributes */
1361 static const struct pmbus_limit_attr temp_limit_attrs
[] = {
1363 .reg
= PMBUS_UT_WARN_LIMIT
,
1366 .alarm
= "min_alarm",
1367 .sbit
= PB_TEMP_UT_WARNING
,
1369 .reg
= PMBUS_UT_FAULT_LIMIT
,
1372 .alarm
= "lcrit_alarm",
1373 .sbit
= PB_TEMP_UT_FAULT
,
1375 .reg
= PMBUS_OT_WARN_LIMIT
,
1377 .alarm
= "max_alarm",
1378 .sbit
= PB_TEMP_OT_WARNING
,
1380 .reg
= PMBUS_OT_FAULT_LIMIT
,
1382 .alarm
= "crit_alarm",
1383 .sbit
= PB_TEMP_OT_FAULT
,
1385 .reg
= PMBUS_VIRT_READ_TEMP_MIN
,
1388 .reg
= PMBUS_VIRT_READ_TEMP_AVG
,
1391 .reg
= PMBUS_VIRT_READ_TEMP_MAX
,
1394 .reg
= PMBUS_VIRT_RESET_TEMP_HISTORY
,
1395 .attr
= "reset_history",
1399 static const struct pmbus_limit_attr temp_limit_attrs2
[] = {
1401 .reg
= PMBUS_UT_WARN_LIMIT
,
1404 .alarm
= "min_alarm",
1405 .sbit
= PB_TEMP_UT_WARNING
,
1407 .reg
= PMBUS_UT_FAULT_LIMIT
,
1410 .alarm
= "lcrit_alarm",
1411 .sbit
= PB_TEMP_UT_FAULT
,
1413 .reg
= PMBUS_OT_WARN_LIMIT
,
1415 .alarm
= "max_alarm",
1416 .sbit
= PB_TEMP_OT_WARNING
,
1418 .reg
= PMBUS_OT_FAULT_LIMIT
,
1420 .alarm
= "crit_alarm",
1421 .sbit
= PB_TEMP_OT_FAULT
,
1423 .reg
= PMBUS_VIRT_READ_TEMP2_MIN
,
1426 .reg
= PMBUS_VIRT_READ_TEMP2_AVG
,
1429 .reg
= PMBUS_VIRT_READ_TEMP2_MAX
,
1432 .reg
= PMBUS_VIRT_RESET_TEMP2_HISTORY
,
1433 .attr
= "reset_history",
1437 static const struct pmbus_limit_attr temp_limit_attrs3
[] = {
1439 .reg
= PMBUS_UT_WARN_LIMIT
,
1442 .alarm
= "min_alarm",
1443 .sbit
= PB_TEMP_UT_WARNING
,
1445 .reg
= PMBUS_UT_FAULT_LIMIT
,
1448 .alarm
= "lcrit_alarm",
1449 .sbit
= PB_TEMP_UT_FAULT
,
1451 .reg
= PMBUS_OT_WARN_LIMIT
,
1453 .alarm
= "max_alarm",
1454 .sbit
= PB_TEMP_OT_WARNING
,
1456 .reg
= PMBUS_OT_FAULT_LIMIT
,
1458 .alarm
= "crit_alarm",
1459 .sbit
= PB_TEMP_OT_FAULT
,
1463 static const struct pmbus_sensor_attr temp_attributes
[] = {
1465 .reg
= PMBUS_READ_TEMPERATURE_1
,
1466 .class = PSC_TEMPERATURE
,
1470 .func
= PMBUS_HAVE_TEMP
,
1471 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1472 .sbase
= PB_STATUS_TEMP_BASE
,
1473 .gbit
= PB_STATUS_TEMPERATURE
,
1474 .limit
= temp_limit_attrs
,
1475 .nlimit
= ARRAY_SIZE(temp_limit_attrs
),
1477 .reg
= PMBUS_READ_TEMPERATURE_2
,
1478 .class = PSC_TEMPERATURE
,
1482 .func
= PMBUS_HAVE_TEMP2
,
1483 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1484 .sbase
= PB_STATUS_TEMP_BASE
,
1485 .gbit
= PB_STATUS_TEMPERATURE
,
1486 .limit
= temp_limit_attrs2
,
1487 .nlimit
= ARRAY_SIZE(temp_limit_attrs2
),
1489 .reg
= PMBUS_READ_TEMPERATURE_3
,
1490 .class = PSC_TEMPERATURE
,
1494 .func
= PMBUS_HAVE_TEMP3
,
1495 .sfunc
= PMBUS_HAVE_STATUS_TEMP
,
1496 .sbase
= PB_STATUS_TEMP_BASE
,
1497 .gbit
= PB_STATUS_TEMPERATURE
,
1498 .limit
= temp_limit_attrs3
,
1499 .nlimit
= ARRAY_SIZE(temp_limit_attrs3
),
1503 static const int pmbus_fan_registers
[] = {
1504 PMBUS_READ_FAN_SPEED_1
,
1505 PMBUS_READ_FAN_SPEED_2
,
1506 PMBUS_READ_FAN_SPEED_3
,
1507 PMBUS_READ_FAN_SPEED_4
1510 static const int pmbus_fan_config_registers
[] = {
1511 PMBUS_FAN_CONFIG_12
,
1512 PMBUS_FAN_CONFIG_12
,
1513 PMBUS_FAN_CONFIG_34
,
1517 static const int pmbus_fan_status_registers
[] = {
1518 PMBUS_STATUS_FAN_12
,
1519 PMBUS_STATUS_FAN_12
,
1520 PMBUS_STATUS_FAN_34
,
1524 static const u32 pmbus_fan_flags
[] = {
1531 static const u32 pmbus_fan_status_flags
[] = {
1532 PMBUS_HAVE_STATUS_FAN12
,
1533 PMBUS_HAVE_STATUS_FAN12
,
1534 PMBUS_HAVE_STATUS_FAN34
,
1535 PMBUS_HAVE_STATUS_FAN34
1539 static int pmbus_add_fan_attributes(struct i2c_client
*client
,
1540 struct pmbus_data
*data
)
1542 const struct pmbus_driver_info
*info
= data
->info
;
1547 for (page
= 0; page
< info
->pages
; page
++) {
1550 for (f
= 0; f
< ARRAY_SIZE(pmbus_fan_registers
); f
++) {
1553 if (!(info
->func
[page
] & pmbus_fan_flags
[f
]))
1556 if (!pmbus_check_word_register(client
, page
,
1557 pmbus_fan_registers
[f
]))
1561 * Skip fan if not installed.
1562 * Each fan configuration register covers multiple fans,
1563 * so we have to do some magic.
1565 regval
= _pmbus_read_byte_data(client
, page
,
1566 pmbus_fan_config_registers
[f
]);
1568 (!(regval
& (PB_FAN_1_INSTALLED
>> ((f
& 1) * 4)))))
1571 if (pmbus_add_sensor(data
, "fan", "input", index
,
1572 page
, pmbus_fan_registers
[f
],
1573 PSC_FAN
, true, true) == NULL
)
1577 * Each fan status register covers multiple fans,
1578 * so we have to do some magic.
1580 if ((info
->func
[page
] & pmbus_fan_status_flags
[f
]) &&
1581 pmbus_check_byte_register(client
,
1582 page
, pmbus_fan_status_registers
[f
])) {
1585 if (f
> 1) /* fan 3, 4 */
1586 base
= PB_STATUS_FAN34_BASE
+ page
;
1588 base
= PB_STATUS_FAN_BASE
+ page
;
1589 ret
= pmbus_add_boolean(data
, "fan",
1590 "alarm", index
, NULL
, NULL
, base
,
1591 PB_FAN_FAN1_WARNING
>> (f
& 1));
1594 ret
= pmbus_add_boolean(data
, "fan",
1595 "fault", index
, NULL
, NULL
, base
,
1596 PB_FAN_FAN1_FAULT
>> (f
& 1));
1606 static int pmbus_find_attributes(struct i2c_client
*client
,
1607 struct pmbus_data
*data
)
1611 /* Voltage sensors */
1612 ret
= pmbus_add_sensor_attrs(client
, data
, "in", voltage_attributes
,
1613 ARRAY_SIZE(voltage_attributes
));
1617 /* Current sensors */
1618 ret
= pmbus_add_sensor_attrs(client
, data
, "curr", current_attributes
,
1619 ARRAY_SIZE(current_attributes
));
1624 ret
= pmbus_add_sensor_attrs(client
, data
, "power", power_attributes
,
1625 ARRAY_SIZE(power_attributes
));
1629 /* Temperature sensors */
1630 ret
= pmbus_add_sensor_attrs(client
, data
, "temp", temp_attributes
,
1631 ARRAY_SIZE(temp_attributes
));
1636 ret
= pmbus_add_fan_attributes(client
, data
);
1641 * Identify chip parameters.
1642 * This function is called for all chips.
1644 static int pmbus_identify_common(struct i2c_client
*client
,
1645 struct pmbus_data
*data
)
1649 if (pmbus_check_byte_register(client
, 0, PMBUS_VOUT_MODE
))
1650 vout_mode
= _pmbus_read_byte_data(client
, 0, PMBUS_VOUT_MODE
);
1651 if (vout_mode
>= 0 && vout_mode
!= 0xff) {
1653 * Not all chips support the VOUT_MODE command,
1654 * so a failure to read it is not an error.
1656 switch (vout_mode
>> 5) {
1657 case 0: /* linear mode */
1658 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != linear
)
1661 data
->exponent
= ((s8
)(vout_mode
<< 3)) >> 3;
1663 case 1: /* VID mode */
1664 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != vid
)
1667 case 2: /* direct mode */
1668 if (data
->info
->format
[PSC_VOLTAGE_OUT
] != direct
)
1676 pmbus_clear_fault_page(client
, 0);
1680 static int pmbus_init_common(struct i2c_client
*client
, struct pmbus_data
*data
,
1681 struct pmbus_driver_info
*info
)
1683 struct device
*dev
= &client
->dev
;
1687 * Some PMBus chips don't support PMBUS_STATUS_BYTE, so try
1688 * to use PMBUS_STATUS_WORD instead if that is the case.
1689 * Bail out if both registers are not supported.
1691 data
->status_register
= PMBUS_STATUS_BYTE
;
1692 ret
= i2c_smbus_read_byte_data(client
, PMBUS_STATUS_BYTE
);
1693 if (ret
< 0 || ret
== 0xff) {
1694 data
->status_register
= PMBUS_STATUS_WORD
;
1695 ret
= i2c_smbus_read_word_data(client
, PMBUS_STATUS_WORD
);
1696 if (ret
< 0 || ret
== 0xffff) {
1697 dev_err(dev
, "PMBus status register not found\n");
1702 pmbus_clear_faults(client
);
1704 if (info
->identify
) {
1705 ret
= (*info
->identify
)(client
, info
);
1707 dev_err(dev
, "Chip identification failed\n");
1712 if (info
->pages
<= 0 || info
->pages
> PMBUS_PAGES
) {
1713 dev_err(dev
, "Bad number of PMBus pages: %d\n", info
->pages
);
1717 ret
= pmbus_identify_common(client
, data
);
1719 dev_err(dev
, "Failed to identify chip capabilities\n");
1725 int pmbus_do_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
,
1726 struct pmbus_driver_info
*info
)
1728 struct device
*dev
= &client
->dev
;
1729 const struct pmbus_platform_data
*pdata
= dev_get_platdata(dev
);
1730 struct pmbus_data
*data
;
1736 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WRITE_BYTE
1737 | I2C_FUNC_SMBUS_BYTE_DATA
1738 | I2C_FUNC_SMBUS_WORD_DATA
))
1741 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
1745 i2c_set_clientdata(client
, data
);
1746 mutex_init(&data
->update_lock
);
1750 data
->flags
= pdata
->flags
;
1753 ret
= pmbus_init_common(client
, data
, info
);
1757 ret
= pmbus_find_attributes(client
, data
);
1762 * If there are no attributes, something is wrong.
1763 * Bail out instead of trying to register nothing.
1765 if (!data
->num_attributes
) {
1766 dev_err(dev
, "No attributes found\n");
1771 /* Register sysfs hooks */
1772 ret
= sysfs_create_group(&dev
->kobj
, &data
->group
);
1774 dev_err(dev
, "Failed to create sysfs entries\n");
1777 data
->hwmon_dev
= hwmon_device_register(dev
);
1778 if (IS_ERR(data
->hwmon_dev
)) {
1779 ret
= PTR_ERR(data
->hwmon_dev
);
1780 dev_err(dev
, "Failed to register hwmon device\n");
1781 goto out_hwmon_device_register
;
1785 out_hwmon_device_register
:
1786 sysfs_remove_group(&dev
->kobj
, &data
->group
);
1788 kfree(data
->group
.attrs
);
1791 EXPORT_SYMBOL_GPL(pmbus_do_probe
);
1793 int pmbus_do_remove(struct i2c_client
*client
)
1795 struct pmbus_data
*data
= i2c_get_clientdata(client
);
1796 hwmon_device_unregister(data
->hwmon_dev
);
1797 sysfs_remove_group(&client
->dev
.kobj
, &data
->group
);
1798 kfree(data
->group
.attrs
);
1801 EXPORT_SYMBOL_GPL(pmbus_do_remove
);
1803 MODULE_AUTHOR("Guenter Roeck");
1804 MODULE_DESCRIPTION("PMBus core driver");
1805 MODULE_LICENSE("GPL");