21 #include <linux/kernel.h>
22 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
36 #define PMBUS_MAX_INPUT_SENSORS 22
37 #define PMBUS_VOUT_SENSORS_PER_PAGE 9
40 #define PMBUS_IOUT_SENSORS_PER_PAGE 8
43 #define PMBUS_POUT_SENSORS_PER_PAGE 7
46 #define PMBUS_MAX_SENSORS_PER_FAN 1
47 #define PMBUS_MAX_SENSORS_PER_TEMP 9
52 #define PMBUS_MAX_INPUT_BOOLEANS 7
56 #define PMBUS_VOUT_BOOLEANS_PER_PAGE 4
58 #define PMBUS_IOUT_BOOLEANS_PER_PAGE 3
60 #define PMBUS_POUT_BOOLEANS_PER_PAGE 3
62 #define PMBUS_MAX_BOOLEANS_PER_FAN 2
63 #define PMBUS_MAX_BOOLEANS_PER_TEMP 4
66 #define PMBUS_MAX_INPUT_LABELS 4
72 #define PB_NUM_STATUS_REG (PMBUS_PAGES * 6 + 1)
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
193 static int _pmbus_write_byte(
struct i2c_client *client,
int page,
u8 value)
195 struct pmbus_data *data = i2c_get_clientdata(client);
200 status = info->
write_byte(client, page, value);
223 static int _pmbus_write_word_data(
struct i2c_client *client,
int page,
int reg,
226 struct pmbus_data *data = i2c_get_clientdata(client);
256 static int _pmbus_read_word_data(
struct i2c_client *client,
int page,
int reg)
258 struct pmbus_data *data = i2c_get_clientdata(client);
290 static int _pmbus_read_byte_data(
struct i2c_client *client,
int page,
int reg)
292 struct pmbus_data *data = i2c_get_clientdata(client);
304 static void pmbus_clear_fault_page(
struct i2c_client *client,
int page)
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);
319 static int pmbus_check_status_cml(
struct i2c_client *client)
335 struct pmbus_data *data = i2c_get_clientdata(client);
337 rv = _pmbus_read_byte_data(client, page, reg);
339 rv = pmbus_check_status_cml(client);
340 pmbus_clear_fault_page(client, -1);
348 struct pmbus_data *data = i2c_get_clientdata(client);
350 rv = _pmbus_read_word_data(client, page, reg);
352 rv = pmbus_check_status_cml(client);
353 pmbus_clear_fault_page(client, -1);
360 struct pmbus_data *data = i2c_get_clientdata(client);
369 struct pmbus_data *data = i2c_get_clientdata(client);
376 for (i = 0; i < info->
pages; i++)
378 = _pmbus_read_byte_data(client, i,
380 for (i = 0; i < info->
pages; i++) {
386 for (i = 0; i < info->
pages; i++) {
392 for (i = 0; i < info->
pages; i++) {
396 = _pmbus_read_byte_data(client, i,
399 for (i = 0; i < info->
pages; i++) {
403 = _pmbus_read_byte_data(client, i,
407 for (i = 0; i < info->
pages; i++) {
411 = _pmbus_read_byte_data(client, i,
417 = _pmbus_read_byte_data(client, 0,
425 = _pmbus_read_word_data(client,
441 static long pmbus_reg2data_linear(
struct pmbus_data *data,
452 exponent = ((
s16)sensor->
data) >> 11;
453 mantissa = ((
s16)((sensor->
data & 0x7ff) << 5)) >> 5;
478 static long pmbus_reg2data_direct(
struct pmbus_data *data,
514 return (val -
b) /
m;
522 static long pmbus_reg2data_vid(
struct pmbus_data *data,
525 long val = sensor->
data;
527 if (val < 0x02 || val > 0xb2)
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)
557 s16 exponent = 0, mantissa;
558 bool negative =
false;
603 while (val < MIN_MANTISSA && exponent > -15) {
612 if (mantissa > 0x3ff)
617 mantissa = -mantissa;
620 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
628 m = data->
info->m[
class];
629 b = data->
info->b[
class];
630 R = data->
info->R[
class];
660 val = SENSORS_LIMIT(val, 500, 1600);
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);
715 u8 s1 = (index >> 24) & 0xff;
716 u8 s2 = (index >> 16) & 0xff;
717 u8 reg = (index >> 8) & 0xff;
726 regval = status &
mask;
734 if (sensor1->
data < 0)
735 return sensor1->
data;
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);
751 struct pmbus_data *data = pmbus_update_device(dev);
754 val = pmbus_get_boolean(data, attr->
index);
764 struct pmbus_data *data = pmbus_update_device(dev);
768 if (sensor->
data < 0)
776 const char *buf,
size_t count)
780 struct pmbus_data *data = i2c_get_clientdata(client);
787 if (kstrtol(buf, 10, &val) < 0)
791 regval = pmbus_data2reg(data, sensor->
class, val);
792 ret = _pmbus_write_word_data(client, sensor->
page, sensor->
reg, regval);
805 struct pmbus_data *data = i2c_get_clientdata(client);
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,
851 static void pmbus_add_boolean_reg(
struct pmbus_data *data,
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,
880 sensor->
class =
class;
891 static void pmbus_add_label(
struct pmbus_data *data,
892 const char *name,
int seq,
893 const char *lstring,
int index)
915 static void pmbus_find_max_attr(
struct i2c_client *client,
919 int page, max_sensors, max_booleans, max_labels;
925 for (page = 0; page < info->
pages; page++) {
1009 static bool pmbus_add_limit_attrs(
struct i2c_client *client,
1012 const char *name,
int index,
int page,
1017 int nlimit = attr->
nlimit;
1018 bool have_alarm =
false;
1021 for (i = 0; i < nlimit; i++) {
1024 pmbus_add_sensor(data, name, l->
attr, index, page,
1030 pmbus_add_boolean_cmp(data, name,
1032 l->
low ? cindex : cbase,
1033 l->
low ? cbase : cindex,
1036 pmbus_add_boolean_reg(data, name,
1048 static void pmbus_add_sensor_attrs_one(
struct i2c_client *client,
1052 int index,
int page,
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);
1071 if (!have_alarm && attr->
gbit &&
1073 pmbus_add_boolean_reg(data, name,
"alarm", index,
1079 static void pmbus_add_sensor_attrs(
struct i2c_client *client,
1089 for (i = 0; i < nattrs; i++) {
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);
1108 .alarm =
"min_alarm",
1113 .alarm =
"lcrit_alarm",
1118 .alarm =
"max_alarm",
1123 .alarm =
"crit_alarm",
1139 .attr =
"reset_history",
1147 .alarm =
"min_alarm",
1152 .alarm =
"lcrit_alarm",
1157 .alarm =
"max_alarm",
1162 .alarm =
"crit_alarm",
1178 .attr =
"reset_history",
1191 .limit = vin_limit_attrs,
1207 .limit = vout_limit_attrs,
1218 .alarm =
"max_alarm",
1223 .alarm =
"crit_alarm",
1239 .attr =
"reset_history",
1247 .alarm =
"max_alarm",
1252 .alarm =
"lcrit_alarm",
1257 .alarm =
"crit_alarm",
1273 .attr =
"reset_history",
1285 .limit = iin_limit_attrs,
1296 .limit = iout_limit_attrs,
1316 .attr =
"input_highest",
1319 .attr =
"reset_history",
1327 .alarm =
"cap_alarm",
1332 .alarm =
"max_alarm",
1337 .alarm =
"crit_alarm",
1346 .attr =
"input_highest",
1349 .attr =
"reset_history",
1361 .limit = pin_limit_attrs,
1371 .limit = pout_limit_attrs,
1383 .alarm =
"min_alarm",
1389 .alarm =
"lcrit_alarm",
1394 .alarm =
"max_alarm",
1399 .alarm =
"crit_alarm",
1412 .attr =
"reset_history",
1421 .alarm =
"min_alarm",
1427 .alarm =
"lcrit_alarm",
1432 .alarm =
"max_alarm",
1437 .alarm =
"crit_alarm",
1450 .attr =
"reset_history",
1459 .alarm =
"min_alarm",
1465 .alarm =
"lcrit_alarm",
1470 .alarm =
"max_alarm",
1475 .alarm =
"crit_alarm",
1491 .limit = temp_limit_attrs,
1503 .limit = temp_limit_attrs2,
1515 .limit = temp_limit_attrs3,
1520 static const int pmbus_fan_registers[] = {
1527 static const int pmbus_fan_config_registers[] = {
1534 static const int pmbus_fan_status_registers[] = {
1541 static const u32 pmbus_fan_flags[] = {
1548 static const u32 pmbus_fan_status_flags[] = {
1552 PMBUS_HAVE_STATUS_FAN34
1556 static void pmbus_add_fan_attributes(
struct i2c_client *client,
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]))
1573 pmbus_fan_registers[f]))
1581 regval = _pmbus_read_byte_data(client, page,
1582 pmbus_fan_config_registers[f]);
1587 pmbus_add_sensor(data,
"fan",
"input", index, page,
1588 pmbus_fan_registers[f],
PSC_FAN,
true,
1595 if ((info->
func[page] & pmbus_fan_status_flags[f]) &&
1597 page, pmbus_fan_status_registers[f])) {
1604 pmbus_add_boolean_reg(data,
"fan",
"alarm",
1607 pmbus_add_boolean_reg(data,
"fan",
"fault",
1616 static void pmbus_find_attributes(
struct i2c_client *client,
1620 pmbus_add_sensor_attrs(client, data,
"in", voltage_attributes,
1624 pmbus_add_sensor_attrs(client, data,
"curr", current_attributes,
1628 pmbus_add_sensor_attrs(client, data,
"power", power_attributes,
1632 pmbus_add_sensor_attrs(client, data,
"temp", temp_attributes,
1636 pmbus_add_fan_attributes(client, data);
1643 static int pmbus_identify_common(
struct i2c_client *client,
1650 if (vout_mode >= 0 && vout_mode != 0xff) {
1655 switch (vout_mode >> 5) {
1660 data->
exponent = ((
s8)(vout_mode << 3)) >> 3;
1676 pmbus_find_max_attr(client, data);
1677 pmbus_clear_fault_page(client, 0);
1689 dev_err(&client->
dev,
"Missing chip information");
1700 dev_err(&client->
dev,
"No memory to allocate driver data\n");
1704 i2c_set_clientdata(client, data);
1709 dev_err(&client->
dev,
"PMBus status register not found\n");
1720 ret = (*info->
identify)(client, info);
1722 dev_err(&client->
dev,
"Chip identification failed\n");
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");
1743 dev_err(&client->
dev,
"No memory to allocate sensor data\n");
1750 dev_err(&client->
dev,
"No memory to allocate boolean data\n");
1757 dev_err(&client->
dev,
"No memory to allocate label data\n");
1764 dev_err(&client->
dev,
"No memory to allocate attribute data\n");
1768 pmbus_find_attributes(client, data);
1775 dev_err(&client->
dev,
"No attributes found\n");
1783 dev_err(&client->
dev,
"Failed to create sysfs entries\n");
1789 dev_err(&client->
dev,
"Failed to register hwmon device\n");
1790 goto out_hwmon_device_register;
1794 out_hwmon_device_register:
1802 struct pmbus_data *data = i2c_get_clientdata(client);