Skip to content

Commit 2a431ad

Browse files
committed
hwmon: (pmbus/core) Optimize debugfs block data attribute initialization
Define debugfs attributes which need block data access 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. While this is currently only used for manufacturer specific attributes, the access code is generic and also works for other block attributes, so rename operation functions from _mfg to _block. While at it, rename the "revison" file to "pmbus_revision" to make its meaning more obvious and to create a clear distinction against the "mfg_revision" file. Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent 510db88 commit 2a431ad

1 file changed

Lines changed: 34 additions & 59 deletions

File tree

drivers/hwmon/pmbus/pmbus_core.c

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3460,8 +3460,8 @@ static int pmbus_debugfs_get_status(void *data, u64 *val)
34603460
DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status,
34613461
NULL, "0x%04llx\n");
34623462

3463-
static ssize_t pmbus_debugfs_mfr_read(struct file *file, char __user *buf,
3464-
size_t count, loff_t *ppos)
3463+
static ssize_t pmbus_debugfs_block_read(struct file *file, char __user *buf,
3464+
size_t count, loff_t *ppos)
34653465
{
34663466
int rc;
34673467
struct pmbus_debugfs_entry *entry = file->private_data;
@@ -3486,9 +3486,9 @@ static ssize_t pmbus_debugfs_mfr_read(struct file *file, char __user *buf,
34863486
return simple_read_from_buffer(buf, count, ppos, data, rc);
34873487
}
34883488

3489-
static const struct file_operations pmbus_debugfs_ops_mfr = {
3489+
static const struct file_operations pmbus_debugfs_block_ops = {
34903490
.llseek = noop_llseek,
3491-
.read = pmbus_debugfs_mfr_read,
3491+
.read = pmbus_debugfs_block_read,
34923492
.write = NULL,
34933493
.open = simple_open,
34943494
};
@@ -3498,6 +3498,20 @@ static void pmbus_remove_symlink(void *symlink)
34983498
debugfs_remove(symlink);
34993499
}
35003500

3501+
struct pmbus_debugfs_data {
3502+
u8 reg;
3503+
const char *name;
3504+
};
3505+
3506+
static const struct pmbus_debugfs_data pmbus_debugfs_block_data[] = {
3507+
{ .reg = PMBUS_MFR_ID, .name = "mfr_id" },
3508+
{ .reg = PMBUS_MFR_MODEL, .name = "mfr_model" },
3509+
{ .reg = PMBUS_MFR_REVISION, .name = "mfr_revision" },
3510+
{ .reg = PMBUS_MFR_LOCATION, .name = "mfr_location" },
3511+
{ .reg = PMBUS_MFR_DATE, .name = "mfr_date" },
3512+
{ .reg = PMBUS_MFR_SERIAL, .name = "mfr_serial" },
3513+
};
3514+
35013515
static void pmbus_init_debugfs(struct i2c_client *client,
35023516
struct pmbus_data *data)
35033517
{
@@ -3539,12 +3553,14 @@ static void pmbus_init_debugfs(struct i2c_client *client,
35393553

35403554
/*
35413555
* Allocate the max possible entries we need.
3542-
* 7 entries device-specific
3556+
* device specific:
3557+
* ARRAY_SIZE(pmbus_debugfs_block_data) + 1
35433558
* 10 entries page-specific
35443559
*/
35453560
entries = devm_kcalloc(data->dev,
3546-
7 + data->info->pages * 10, sizeof(*entries),
3547-
GFP_KERNEL);
3561+
ARRAY_SIZE(pmbus_debugfs_block_data) + 1 +
3562+
data->info->pages * 10,
3563+
sizeof(*entries), GFP_KERNEL);
35483564
if (!entries)
35493565
return;
35503566

@@ -3560,63 +3576,22 @@ static void pmbus_init_debugfs(struct i2c_client *client,
35603576
entries[idx].client = client;
35613577
entries[idx].page = 0;
35623578
entries[idx].reg = PMBUS_REVISION;
3563-
debugfs_create_file("revision", 0444, debugfs,
3579+
debugfs_create_file("pmbus_revision", 0444, debugfs,
35643580
&entries[idx++],
35653581
&pmbus_debugfs_ops);
35663582
}
35673583

3568-
if (pmbus_check_block_register(client, 0, PMBUS_MFR_ID)) {
3569-
entries[idx].client = client;
3570-
entries[idx].page = 0;
3571-
entries[idx].reg = PMBUS_MFR_ID;
3572-
debugfs_create_file("mfr_id", 0444, debugfs,
3573-
&entries[idx++],
3574-
&pmbus_debugfs_ops_mfr);
3575-
}
3576-
3577-
if (pmbus_check_block_register(client, 0, PMBUS_MFR_MODEL)) {
3578-
entries[idx].client = client;
3579-
entries[idx].page = 0;
3580-
entries[idx].reg = PMBUS_MFR_MODEL;
3581-
debugfs_create_file("mfr_model", 0444, debugfs,
3582-
&entries[idx++],
3583-
&pmbus_debugfs_ops_mfr);
3584-
}
3584+
for (i = 0; i < ARRAY_SIZE(pmbus_debugfs_block_data); i++) {
3585+
const struct pmbus_debugfs_data *d = &pmbus_debugfs_block_data[i];
35853586

3586-
if (pmbus_check_block_register(client, 0, PMBUS_MFR_REVISION)) {
3587-
entries[idx].client = client;
3588-
entries[idx].page = 0;
3589-
entries[idx].reg = PMBUS_MFR_REVISION;
3590-
debugfs_create_file("mfr_revision", 0444, debugfs,
3591-
&entries[idx++],
3592-
&pmbus_debugfs_ops_mfr);
3593-
}
3594-
3595-
if (pmbus_check_block_register(client, 0, PMBUS_MFR_LOCATION)) {
3596-
entries[idx].client = client;
3597-
entries[idx].page = 0;
3598-
entries[idx].reg = PMBUS_MFR_LOCATION;
3599-
debugfs_create_file("mfr_location", 0444, debugfs,
3600-
&entries[idx++],
3601-
&pmbus_debugfs_ops_mfr);
3602-
}
3603-
3604-
if (pmbus_check_block_register(client, 0, PMBUS_MFR_DATE)) {
3605-
entries[idx].client = client;
3606-
entries[idx].page = 0;
3607-
entries[idx].reg = PMBUS_MFR_DATE;
3608-
debugfs_create_file("mfr_date", 0444, debugfs,
3609-
&entries[idx++],
3610-
&pmbus_debugfs_ops_mfr);
3611-
}
3612-
3613-
if (pmbus_check_block_register(client, 0, PMBUS_MFR_SERIAL)) {
3614-
entries[idx].client = client;
3615-
entries[idx].page = 0;
3616-
entries[idx].reg = PMBUS_MFR_SERIAL;
3617-
debugfs_create_file("mfr_serial", 0444, debugfs,
3618-
&entries[idx++],
3619-
&pmbus_debugfs_ops_mfr);
3587+
if (pmbus_check_block_register(client, 0, d->reg)) {
3588+
entries[idx].client = client;
3589+
entries[idx].page = 0;
3590+
entries[idx].reg = d->reg;
3591+
debugfs_create_file(d->name, 0444, debugfs,
3592+
&entries[idx++],
3593+
&pmbus_debugfs_block_ops);
3594+
}
36203595
}
36213596

36223597
/* Add page specific entries */

0 commit comments

Comments
 (0)