regulator: event: Add regulator netlink event support

This commit introduces netlink event support to the regulator subsystem.

Changes:
- Introduce event.c and regnl.h for netlink event handling.
- Implement reg_generate_netlink_event to broadcast regulator events.
- Update Makefile to include the new event.c file.

Signed-off-by: Naresh Solanki <naresh.solanki@9elements.com>
Link: https://lore.kernel.org/r/20231205105207.1262928-1-naresh.solanki@9elements.com
Signed-off-by: Mark Brown <broonie@kernel.org>
This commit is contained in:
Naresh Solanki 2023-12-05 16:22:04 +05:30 committed by Mark Brown
parent 753e4d5c43
commit 2506c1de40
7 changed files with 224 additions and 47 deletions

View File

@ -56,6 +56,16 @@ config REGULATOR_USERSPACE_CONSUMER
If unsure, say no.
config REGULATOR_NETLINK_EVENTS
bool "Enable support for receiving regulator events via netlink"
depends on NET
help
Enabling this option allows the kernel to broadcast regulator events using
the netlink mechanism. User-space applications can subscribe to these events
for real-time updates on various regulator events.
If unsure, say no.
config REGULATOR_88PG86X
tristate "Marvell 88PG86X voltage regulators"
depends on I2C

View File

@ -5,6 +5,7 @@
obj-$(CONFIG_REGULATOR) += core.o dummy.o fixed-helper.o helpers.o devres.o irq_helpers.o
obj-$(CONFIG_REGULATOR_NETLINK_EVENTS) += event.o
obj-$(CONFIG_OF) += of_regulator.o
obj-$(CONFIG_REGULATOR_FIXED_VOLTAGE) += fixed.o
obj-$(CONFIG_REGULATOR_VIRTUAL_CONSUMER) += virtual.o

View File

@ -33,6 +33,7 @@
#include "dummy.h"
#include "internal.h"
#include "regnl.h"
static DEFINE_WW_CLASS(regulator_ww_class);
static DEFINE_MUTEX(regulator_nesting_mutex);
@ -4854,7 +4855,23 @@ static int _notifier_call_chain(struct regulator_dev *rdev,
unsigned long event, void *data)
{
/* call rdev chain first */
return blocking_notifier_call_chain(&rdev->notifier, event, data);
int ret = blocking_notifier_call_chain(&rdev->notifier, event, data);
if (IS_REACHABLE(CONFIG_REGULATOR_NETLINK_EVENTS)) {
struct device *parent = rdev->dev.parent;
const char *rname = rdev_get_name(rdev);
char name[32];
/* Avoid duplicate debugfs directory names */
if (parent && rname == rdev->desc->name) {
snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
rname);
rname = name;
}
reg_generate_netlink_event(rname, event);
}
return ret;
}
int _regulator_bulk_get(struct device *dev, int num_consumers,

91
drivers/regulator/event.c Normal file
View File

@ -0,0 +1,91 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Regulator event over netlink
*
* Author: Naresh Solanki <Naresh.Solanki@9elements.com>
*/
#include <regulator/regulator.h>
#include <net/netlink.h>
#include <net/genetlink.h>
#include "regnl.h"
static unsigned int reg_event_seqnum;
static const struct genl_multicast_group reg_event_mcgrps[] = {
{ .name = REG_GENL_MCAST_GROUP_NAME, },
};
static struct genl_family reg_event_genl_family __ro_after_init = {
.module = THIS_MODULE,
.name = REG_GENL_FAMILY_NAME,
.version = REG_GENL_VERSION,
.maxattr = REG_GENL_ATTR_MAX,
.mcgrps = reg_event_mcgrps,
.n_mcgrps = ARRAY_SIZE(reg_event_mcgrps),
};
int reg_generate_netlink_event(const char *reg_name, u64 event)
{
struct sk_buff *skb;
struct nlattr *attr;
struct reg_genl_event *edata;
void *msg_header;
int size;
/* allocate memory */
size = nla_total_size(sizeof(struct reg_genl_event)) +
nla_total_size(0);
skb = genlmsg_new(size, GFP_ATOMIC);
if (!skb)
return -ENOMEM;
/* add the genetlink message header */
msg_header = genlmsg_put(skb, 0, reg_event_seqnum++,
&reg_event_genl_family, 0,
REG_GENL_CMD_EVENT);
if (!msg_header) {
nlmsg_free(skb);
return -ENOMEM;
}
/* fill the data */
attr = nla_reserve(skb, REG_GENL_ATTR_EVENT, sizeof(struct reg_genl_event));
if (!attr) {
nlmsg_free(skb);
return -EINVAL;
}
edata = nla_data(attr);
memset(edata, 0, sizeof(struct reg_genl_event));
strscpy(edata->reg_name, reg_name, sizeof(edata->reg_name));
edata->event = event;
/* send multicast genetlink message */
genlmsg_end(skb, msg_header);
size = genlmsg_multicast(&reg_event_genl_family, skb, 0, 0, GFP_ATOMIC);
return size;
}
static int __init reg_event_genetlink_init(void)
{
return genl_register_family(&reg_event_genl_family);
}
static int __init reg_event_init(void)
{
int error;
/* create genetlink for acpi event */
error = reg_event_genetlink_init();
if (error)
pr_warn("Failed to create genetlink family for reg event\n");
return 0;
}
fs_initcall(reg_event_init);

13
drivers/regulator/regnl.h Normal file
View File

@ -0,0 +1,13 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Regulator event over netlink
*
* Author: Naresh Solanki <Naresh.Solanki@9elements.com>
*/
#ifndef __REGULATOR_EVENT_H
#define __REGULATOR_EVENT_H
int reg_generate_netlink_event(const char *reg_name, u64 event);
#endif

View File

@ -33,6 +33,7 @@
#include <linux/err.h>
#include <linux/suspend.h>
#include <regulator/regulator.h>
struct device;
struct notifier_block;
@ -84,52 +85,6 @@ struct regulator_dev;
#define REGULATOR_MODE_IDLE 0x4
#define REGULATOR_MODE_STANDBY 0x8
/*
* Regulator notifier events.
*
* UNDER_VOLTAGE Regulator output is under voltage.
* OVER_CURRENT Regulator output current is too high.
* REGULATION_OUT Regulator output is out of regulation.
* FAIL Regulator output has failed.
* OVER_TEMP Regulator over temp.
* FORCE_DISABLE Regulator forcibly shut down by software.
* VOLTAGE_CHANGE Regulator voltage changed.
* Data passed is old voltage cast to (void *).
* DISABLE Regulator was disabled.
* PRE_VOLTAGE_CHANGE Regulator is about to have voltage changed.
* Data passed is "struct pre_voltage_change_data"
* ABORT_VOLTAGE_CHANGE Regulator voltage change failed for some reason.
* Data passed is old voltage cast to (void *).
* PRE_DISABLE Regulator is about to be disabled
* ABORT_DISABLE Regulator disable failed for some reason
*
* NOTE: These events can be OR'ed together when passed into handler.
*/
#define REGULATOR_EVENT_UNDER_VOLTAGE 0x01
#define REGULATOR_EVENT_OVER_CURRENT 0x02
#define REGULATOR_EVENT_REGULATION_OUT 0x04
#define REGULATOR_EVENT_FAIL 0x08
#define REGULATOR_EVENT_OVER_TEMP 0x10
#define REGULATOR_EVENT_FORCE_DISABLE 0x20
#define REGULATOR_EVENT_VOLTAGE_CHANGE 0x40
#define REGULATOR_EVENT_DISABLE 0x80
#define REGULATOR_EVENT_PRE_VOLTAGE_CHANGE 0x100
#define REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE 0x200
#define REGULATOR_EVENT_PRE_DISABLE 0x400
#define REGULATOR_EVENT_ABORT_DISABLE 0x800
#define REGULATOR_EVENT_ENABLE 0x1000
/*
* Following notifications should be emitted only if detected condition
* is such that the HW is likely to still be working but consumers should
* take a recovery action to prevent problems esacalating into errors.
*/
#define REGULATOR_EVENT_UNDER_VOLTAGE_WARN 0x2000
#define REGULATOR_EVENT_OVER_CURRENT_WARN 0x4000
#define REGULATOR_EVENT_OVER_VOLTAGE_WARN 0x8000
#define REGULATOR_EVENT_OVER_TEMP_WARN 0x10000
#define REGULATOR_EVENT_WARN_MASK 0x1E000
/*
* Regulator errors that can be queried using regulator_get_error_flags
*

View File

@ -0,0 +1,90 @@
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
/*
* Regulator uapi header
*
* Author: Naresh Solanki <Naresh.Solanki@9elements.com>
*/
#ifndef _UAPI_REGULATOR_H
#define _UAPI_REGULATOR_H
#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <stdint.h>
#endif
/*
* Regulator notifier events.
*
* UNDER_VOLTAGE Regulator output is under voltage.
* OVER_CURRENT Regulator output current is too high.
* REGULATION_OUT Regulator output is out of regulation.
* FAIL Regulator output has failed.
* OVER_TEMP Regulator over temp.
* FORCE_DISABLE Regulator forcibly shut down by software.
* VOLTAGE_CHANGE Regulator voltage changed.
* Data passed is old voltage cast to (void *).
* DISABLE Regulator was disabled.
* PRE_VOLTAGE_CHANGE Regulator is about to have voltage changed.
* Data passed is "struct pre_voltage_change_data"
* ABORT_VOLTAGE_CHANGE Regulator voltage change failed for some reason.
* Data passed is old voltage cast to (void *).
* PRE_DISABLE Regulator is about to be disabled
* ABORT_DISABLE Regulator disable failed for some reason
*
* NOTE: These events can be OR'ed together when passed into handler.
*/
#define REGULATOR_EVENT_UNDER_VOLTAGE 0x01
#define REGULATOR_EVENT_OVER_CURRENT 0x02
#define REGULATOR_EVENT_REGULATION_OUT 0x04
#define REGULATOR_EVENT_FAIL 0x08
#define REGULATOR_EVENT_OVER_TEMP 0x10
#define REGULATOR_EVENT_FORCE_DISABLE 0x20
#define REGULATOR_EVENT_VOLTAGE_CHANGE 0x40
#define REGULATOR_EVENT_DISABLE 0x80
#define REGULATOR_EVENT_PRE_VOLTAGE_CHANGE 0x100
#define REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE 0x200
#define REGULATOR_EVENT_PRE_DISABLE 0x400
#define REGULATOR_EVENT_ABORT_DISABLE 0x800
#define REGULATOR_EVENT_ENABLE 0x1000
/*
* Following notifications should be emitted only if detected condition
* is such that the HW is likely to still be working but consumers should
* take a recovery action to prevent problems esacalating into errors.
*/
#define REGULATOR_EVENT_UNDER_VOLTAGE_WARN 0x2000
#define REGULATOR_EVENT_OVER_CURRENT_WARN 0x4000
#define REGULATOR_EVENT_OVER_VOLTAGE_WARN 0x8000
#define REGULATOR_EVENT_OVER_TEMP_WARN 0x10000
#define REGULATOR_EVENT_WARN_MASK 0x1E000
struct reg_genl_event {
char reg_name[32];
uint64_t event;
};
/* attributes of reg_genl_family */
enum {
REG_GENL_ATTR_UNSPEC,
REG_GENL_ATTR_EVENT, /* reg event info needed by user space */
__REG_GENL_ATTR_MAX,
};
#define REG_GENL_ATTR_MAX (__REG_GENL_ATTR_MAX - 1)
/* commands supported by the reg_genl_family */
enum {
REG_GENL_CMD_UNSPEC,
REG_GENL_CMD_EVENT, /* kernel->user notifications for reg events */
__REG_GENL_CMD_MAX,
};
#define REG_GENL_CMD_MAX (__REG_GENL_CMD_MAX - 1)
#define REG_GENL_FAMILY_NAME "reg_event"
#define REG_GENL_VERSION 0x01
#define REG_GENL_MCAST_GROUP_NAME "reg_mc_group"
#endif /* _UAPI_REGULATOR_H */