Skip to content

Commit c5e6763

Browse files
author
Guenter Roeck
committed
hwmon: (pmbus/adm1275) Add support for second current limit
ADM1275 supports a second current limit, which can be configured as either lower or upper limit. Add support for it and report it as either lower or upper critical current limit. Also replace error return code EINVAL for unsupported pages with ENXIO as this is more appropriate for the observed condition. Signed-off-by: Guenter Roeck <guenter.roeck@ericsson.com> Acked-by: Jean Delvare <khali@linux-fr.org>
1 parent 866cf12 commit c5e6763

2 files changed

Lines changed: 96 additions & 7 deletions

File tree

‎Documentation/hwmon/adm1275‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,13 @@ curr1_label "iout1"
6060
curr1_input Measured current. From READ_IOUT register.
6161
curr1_max Maximum current. From IOUT_OC_WARN_LIMIT register.
6262
curr1_max_alarm Current high alarm. From IOUT_OC_WARN_LIMIT register.
63+
curr1_lcrit Critical minimum current. Depending on the chip
64+
configuration, either curr1_lcrit or curr1_crit is
65+
supported, but not both.
66+
curr1_lcrit_alarm Critical current low alarm.
67+
curr1_crit Critical maximum current. Depending on the chip
68+
configuration, either curr1_lcrit or curr1_crit is
69+
supported, but not both.
70+
curr1_crit_alarm Critical current high alarm.
6371
curr1_highest Historical maximum current.
6472
curr1_reset_history Write any value to reset history.

‎drivers/hwmon/pmbus/adm1275.c‎

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,44 @@
3131
#define ADM1275_VIN_VOUT_SELECT (1 << 6)
3232
#define ADM1275_VRANGE (1 << 5)
3333

34+
#define ADM1275_IOUT_WARN2_LIMIT 0xd7
35+
#define ADM1275_DEVICE_CONFIG 0xd8
36+
37+
#define ADM1275_IOUT_WARN2_SELECT (1 << 4)
38+
39+
#define ADM1275_MFR_STATUS_IOUT_WARN2 (1 << 0)
40+
41+
struct adm1275_data {
42+
bool have_oc_fault;
43+
struct pmbus_driver_info info;
44+
};
45+
46+
#define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
47+
3448
static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
3549
{
50+
const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
51+
const struct adm1275_data *data = to_adm1275_data(info);
3652
int ret;
3753

3854
if (page)
39-
return -EINVAL;
55+
return -ENXIO;
4056

4157
switch (reg) {
58+
case PMBUS_IOUT_UC_FAULT_LIMIT:
59+
if (data->have_oc_fault) {
60+
ret = -ENXIO;
61+
break;
62+
}
63+
ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
64+
break;
65+
case PMBUS_IOUT_OC_FAULT_LIMIT:
66+
if (!data->have_oc_fault) {
67+
ret = -ENXIO;
68+
break;
69+
}
70+
ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
71+
break;
4272
case PMBUS_VIRT_READ_IOUT_MAX:
4373
ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
4474
break;
@@ -66,9 +96,14 @@ static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
6696
int ret;
6797

6898
if (page)
69-
return -EINVAL;
99+
return -ENXIO;
70100

71101
switch (reg) {
102+
case PMBUS_IOUT_UC_FAULT_LIMIT:
103+
case PMBUS_IOUT_OC_FAULT_LIMIT:
104+
ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
105+
word);
106+
break;
72107
case PMBUS_VIRT_RESET_IOUT_HISTORY:
73108
ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
74109
break;
@@ -85,19 +120,52 @@ static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
85120
return ret;
86121
}
87122

123+
static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
124+
{
125+
const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
126+
const struct adm1275_data *data = to_adm1275_data(info);
127+
int mfr_status, ret;
128+
129+
if (page)
130+
return -ENXIO;
131+
132+
switch (reg) {
133+
case PMBUS_STATUS_IOUT:
134+
ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
135+
if (ret < 0)
136+
break;
137+
mfr_status = pmbus_read_byte_data(client, page,
138+
PMBUS_STATUS_MFR_SPECIFIC);
139+
if (mfr_status < 0) {
140+
ret = mfr_status;
141+
break;
142+
}
143+
if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
144+
ret |= data->have_oc_fault ?
145+
PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
146+
}
147+
break;
148+
default:
149+
ret = -ENODATA;
150+
break;
151+
}
152+
return ret;
153+
}
154+
88155
static int adm1275_probe(struct i2c_client *client,
89156
const struct i2c_device_id *id)
90157
{
91-
int config;
158+
int config, device_config;
92159
int ret;
93160
struct pmbus_driver_info *info;
161+
struct adm1275_data *data;
94162

95163
if (!i2c_check_functionality(client->adapter,
96164
I2C_FUNC_SMBUS_READ_BYTE_DATA))
97165
return -ENODEV;
98166

99-
info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
100-
if (!info)
167+
data = kzalloc(sizeof(struct adm1275_data), GFP_KERNEL);
168+
if (!data)
101169
return -ENOMEM;
102170

103171
config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
@@ -106,6 +174,14 @@ static int adm1275_probe(struct i2c_client *client,
106174
goto err_mem;
107175
}
108176

177+
device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
178+
if (device_config < 0) {
179+
ret = device_config;
180+
goto err_mem;
181+
}
182+
183+
info = &data->info;
184+
109185
info->pages = 1;
110186
info->format[PSC_VOLTAGE_IN] = direct;
111187
info->format[PSC_VOLTAGE_OUT] = direct;
@@ -116,6 +192,7 @@ static int adm1275_probe(struct i2c_client *client,
116192
info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
117193

118194
info->read_word_data = adm1275_read_word_data;
195+
info->read_byte_data = adm1275_read_byte_data;
119196
info->write_word_data = adm1275_write_word_data;
120197

121198
if (config & ADM1275_VRANGE) {
@@ -134,6 +211,9 @@ static int adm1275_probe(struct i2c_client *client,
134211
info->R[PSC_VOLTAGE_OUT] = -1;
135212
}
136213

214+
if (device_config & ADM1275_IOUT_WARN2_SELECT)
215+
data->have_oc_fault = true;
216+
137217
if (config & ADM1275_VIN_VOUT_SELECT)
138218
info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
139219
else
@@ -145,16 +225,17 @@ static int adm1275_probe(struct i2c_client *client,
145225
return 0;
146226

147227
err_mem:
148-
kfree(info);
228+
kfree(data);
149229
return ret;
150230
}
151231

152232
static int adm1275_remove(struct i2c_client *client)
153233
{
154234
const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
235+
const struct adm1275_data *data = to_adm1275_data(info);
155236

156237
pmbus_do_remove(client);
157-
kfree(info);
238+
kfree(data);
158239
return 0;
159240
}
160241

0 commit comments

Comments
 (0)