Skip to content

Commit 64fbb4c

Browse files
committed
hwmon: (pmbus/core) Optimize debugfs status attribute initialization
Define debugfs attributes used to access status registers in a data structure and loop through it instead of creating debugfs files one by one. This reduces code size and simplifies adding additional attributes if needed. Reviewed-by: Tzung-Bi Shih <[email protected]> Signed-off-by: Guenter Roeck <[email protected]>
1 parent 2a431ad commit 64fbb4c

File tree

1 file changed

+36
-96
lines changed

1 file changed

+36
-96
lines changed

drivers/hwmon/pmbus/pmbus_core.c

Lines changed: 36 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3500,6 +3500,7 @@ static void pmbus_remove_symlink(void *symlink)
35003500

35013501
struct pmbus_debugfs_data {
35023502
u8 reg;
3503+
u32 flag;
35033504
const char *name;
35043505
};
35053506

@@ -3512,14 +3513,27 @@ static const struct pmbus_debugfs_data pmbus_debugfs_block_data[] = {
35123513
{ .reg = PMBUS_MFR_SERIAL, .name = "mfr_serial" },
35133514
};
35143515

3516+
static const struct pmbus_debugfs_data pmbus_debugfs_status_data[] = {
3517+
{ .reg = PMBUS_STATUS_VOUT, .flag = PMBUS_HAVE_STATUS_VOUT, .name = "status%d_vout" },
3518+
{ .reg = PMBUS_STATUS_IOUT, .flag = PMBUS_HAVE_STATUS_IOUT, .name = "status%d_iout" },
3519+
{ .reg = PMBUS_STATUS_INPUT, .flag = PMBUS_HAVE_STATUS_INPUT, .name = "status%d_input" },
3520+
{ .reg = PMBUS_STATUS_TEMPERATURE, .flag = PMBUS_HAVE_STATUS_TEMP,
3521+
.name = "status%d_temp" },
3522+
{ .reg = PMBUS_STATUS_FAN_12, .flag = PMBUS_HAVE_STATUS_FAN12, .name = "status%d_fan12" },
3523+
{ .reg = PMBUS_STATUS_FAN_34, .flag = PMBUS_HAVE_STATUS_FAN34, .name = "status%d_fan34" },
3524+
{ .reg = PMBUS_STATUS_CML, .name = "status%d_cml" },
3525+
{ .reg = PMBUS_STATUS_OTHER, .name = "status%d_other" },
3526+
{ .reg = PMBUS_STATUS_MFR_SPECIFIC, .name = "status%d_mfr" },
3527+
};
3528+
35153529
static void pmbus_init_debugfs(struct i2c_client *client,
35163530
struct pmbus_data *data)
35173531
{
35183532
struct dentry *symlink_d, *debugfs = client->debugfs;
35193533
struct pmbus_debugfs_entry *entries;
35203534
const char *pathname, *symlink;
35213535
char name[PMBUS_NAME_SIZE];
3522-
int i, idx = 0;
3536+
int page, i, idx = 0;
35233537

35243538
/*
35253539
* client->debugfs may be NULL or an ERR_PTR(). dentry_path_raw()
@@ -3555,11 +3569,12 @@ static void pmbus_init_debugfs(struct i2c_client *client,
35553569
* Allocate the max possible entries we need.
35563570
* device specific:
35573571
* ARRAY_SIZE(pmbus_debugfs_block_data) + 1
3558-
* 10 entries page-specific
3572+
* page specific:
3573+
* ARRAY_SIZE(pmbus_debugfs_status_data) + 1
35593574
*/
35603575
entries = devm_kcalloc(data->dev,
35613576
ARRAY_SIZE(pmbus_debugfs_block_data) + 1 +
3562-
data->info->pages * 10,
3577+
data->info->pages * (ARRAY_SIZE(pmbus_debugfs_status_data) + 1),
35633578
sizeof(*entries), GFP_KERNEL);
35643579
if (!entries)
35653580
return;
@@ -3595,107 +3610,32 @@ static void pmbus_init_debugfs(struct i2c_client *client,
35953610
}
35963611

35973612
/* Add page specific entries */
3598-
for (i = 0; i < data->info->pages; ++i) {
3613+
for (page = 0; page < data->info->pages; ++page) {
35993614
/* Check accessibility of status register if it's not page 0 */
3600-
if (!i || pmbus_check_status_register(client, i)) {
3615+
if (!page || pmbus_check_status_register(client, page)) {
36013616
/* No need to set reg as we have special read op. */
36023617
entries[idx].client = client;
3603-
entries[idx].page = i;
3604-
scnprintf(name, PMBUS_NAME_SIZE, "status%d", i);
3618+
entries[idx].page = page;
3619+
scnprintf(name, PMBUS_NAME_SIZE, "status%d", page);
36053620
debugfs_create_file(name, 0444, debugfs,
36063621
&entries[idx++],
36073622
&pmbus_debugfs_ops_status);
36083623
}
36093624

3610-
if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) {
3611-
entries[idx].client = client;
3612-
entries[idx].page = i;
3613-
entries[idx].reg = PMBUS_STATUS_VOUT;
3614-
scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i);
3615-
debugfs_create_file(name, 0444, debugfs,
3616-
&entries[idx++],
3617-
&pmbus_debugfs_ops);
3618-
}
3619-
3620-
if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) {
3621-
entries[idx].client = client;
3622-
entries[idx].page = i;
3623-
entries[idx].reg = PMBUS_STATUS_IOUT;
3624-
scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i);
3625-
debugfs_create_file(name, 0444, debugfs,
3626-
&entries[idx++],
3627-
&pmbus_debugfs_ops);
3628-
}
3629-
3630-
if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) {
3631-
entries[idx].client = client;
3632-
entries[idx].page = i;
3633-
entries[idx].reg = PMBUS_STATUS_INPUT;
3634-
scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i);
3635-
debugfs_create_file(name, 0444, debugfs,
3636-
&entries[idx++],
3637-
&pmbus_debugfs_ops);
3638-
}
3639-
3640-
if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) {
3641-
entries[idx].client = client;
3642-
entries[idx].page = i;
3643-
entries[idx].reg = PMBUS_STATUS_TEMPERATURE;
3644-
scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i);
3645-
debugfs_create_file(name, 0444, debugfs,
3646-
&entries[idx++],
3647-
&pmbus_debugfs_ops);
3648-
}
3649-
3650-
if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) {
3651-
entries[idx].client = client;
3652-
entries[idx].page = i;
3653-
entries[idx].reg = PMBUS_STATUS_CML;
3654-
scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i);
3655-
debugfs_create_file(name, 0444, debugfs,
3656-
&entries[idx++],
3657-
&pmbus_debugfs_ops);
3658-
}
3659-
3660-
if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) {
3661-
entries[idx].client = client;
3662-
entries[idx].page = i;
3663-
entries[idx].reg = PMBUS_STATUS_OTHER;
3664-
scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i);
3665-
debugfs_create_file(name, 0444, debugfs,
3666-
&entries[idx++],
3667-
&pmbus_debugfs_ops);
3668-
}
3669-
3670-
if (pmbus_check_byte_register(client, i,
3671-
PMBUS_STATUS_MFR_SPECIFIC)) {
3672-
entries[idx].client = client;
3673-
entries[idx].page = i;
3674-
entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC;
3675-
scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i);
3676-
debugfs_create_file(name, 0444, debugfs,
3677-
&entries[idx++],
3678-
&pmbus_debugfs_ops);
3679-
}
3680-
3681-
if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) {
3682-
entries[idx].client = client;
3683-
entries[idx].page = i;
3684-
entries[idx].reg = PMBUS_STATUS_FAN_12;
3685-
scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i);
3686-
debugfs_create_file(name, 0444, debugfs,
3687-
&entries[idx++],
3688-
&pmbus_debugfs_ops);
3689-
}
3690-
3691-
if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) {
3692-
entries[idx].client = client;
3693-
entries[idx].page = i;
3694-
entries[idx].reg = PMBUS_STATUS_FAN_34;
3695-
scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i);
3696-
debugfs_create_file(name, 0444, debugfs,
3697-
&entries[idx++],
3698-
&pmbus_debugfs_ops);
3625+
for (i = 0; i < ARRAY_SIZE(pmbus_debugfs_status_data); i++) {
3626+
const struct pmbus_debugfs_data *d =
3627+
&pmbus_debugfs_status_data[i];
3628+
3629+
if ((data->info->func[page] & d->flag) ||
3630+
(!d->flag && pmbus_check_byte_register(client, page, d->reg))) {
3631+
entries[idx].client = client;
3632+
entries[idx].page = page;
3633+
entries[idx].reg = d->reg;
3634+
scnprintf(name, PMBUS_NAME_SIZE, d->name, page);
3635+
debugfs_create_file(name, 0444, debugfs,
3636+
&entries[idx++],
3637+
&pmbus_debugfs_ops);
3638+
}
36993639
}
37003640
}
37013641
}

0 commit comments

Comments
 (0)