Linux LED subsystem development
 help / color / mirror / Atom feed
From: Kory Maincent <kory.maincent@bootlin.com>
To: Carlo Szelinsky <github@szelinsky.de>,
	Oleksij Rempel <o.rempel@pengutronix.de>
Cc: Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Lee Jones <lee@kernel.org>, Pavel Machek <pavel@kernel.org>,
	Jonas Jelonek <jelonek.jonas@gmail.com>,
	Ross Cawston <ross@r-sc.ca>,
	netdev@vger.kernel.org, linux-leds@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next v6 1/2] net: pse-pd: add pse_poll_helper()
Date: Mon, 30 Nov 2026 16:25:45 +0100	[thread overview]
Message-ID: <c35babda-b225-49bf-a3d5-fb352b7fd26c@bootlin.com> (raw)
In-Reply-To: <20261128121525.3909836-2-github@szelinsky.de>



On 11/28/26 13:15, Carlo Szelinsky wrote:
> Some PSE controllers have no interrupt line, or the board does not wire
> it. These controllers can still report events if the core polls them.
> 
> Move the event loop of pse_isr() into pse_handle_events(), and add
> pse_poll_helper(). It uses the same pse_irq_desc as the IRQ path.
> A delayed work calls the driver's map_event() every poll_interval_ms
> (default 500 ms) and passes the events to pse_handle_events(). When
> map_event() starts or stops failing, the core logs it once, and the
> next poll runs as usual.
> 
> The helper must be called before the controller registers, and returns
> -EBUSY otherwise. It has no devm_ prefix, as it allocates nothing and
> adds no devres action. pse_controller_register() allocates the poll
> buffers once nr_lines is final, and starts the work at its end, so the
> work never runs on a controller that is not fully set up, or on one
> whose probe failed. pse_controller_unregister() stops it next to the IRQ,
> before the PI data and power domains are released.
> 
> The work runs on system_freezable_power_efficient_wq, so it does not
> access the bus during suspend, and it is not bound to one CPU when
> power efficient workqueues are enabled.
> 
> With the "disabled" budget strategy, a polled controller keeps the
> power control in hardware, as a controller without events does today.
> The core takes it over only for an IRQ controller, which it expects to
> report PD detection while a PI is off. With the static strategy, a
> polled controller uses the software power control, as an IRQ one does.
> Update the comments that say this needs an IRQ.

Can you write the commit message manually and don't use LLM that only 
explain the code and not the why.

> Signed-off-by: Carlo Szelinsky <github@szelinsky.de>
> ---
>   drivers/net/pse-pd/pse_core.c | 183 ++++++++++++++++++++++++++++------
>   include/linux/pse-pd/pse.h    |  27 ++++-
>   2 files changed, 177 insertions(+), 33 deletions(-)
> 
> diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
> index 0477cf6787bd..2fb10f5d8f8e 100644
> --- a/drivers/net/pse-pd/pse_core.c
> +++ b/drivers/net/pse-pd/pse_core.c
> @@ -14,10 +14,14 @@
>   #include <linux/regulator/driver.h>
>   #include <linux/regulator/machine.h>
>   #include <linux/rtnetlink.h>
> +#include <linux/workqueue.h>
>   #include <net/net_trackers.h>
>   
>   #define PSE_PW_D_LIMIT INT_MAX
>   
> +/* Default poll interval: fast enough for PD events, light on the I2C bus */
> +#define PSE_DEFAULT_POLL_INTERVAL_MS 500
> +
>   static DEFINE_MUTEX(pse_list_mutex);
>   static LIST_HEAD(pse_controller_list);
>   static DEFINE_XARRAY_ALLOC(pse_pw_d_map);
> @@ -392,8 +396,8 @@ static void pse_pw_d_retry_power_delivery(struct pse_controller_dev *pcdev,
>    * @pw_d: a pointer to the PSE power domain
>    *
>    * This function determines whether the power control for a specific power
> - * domain is managed by software in the interrupt handler rather than directly
> - * by hardware.
> + * domain is managed by software in the event handler (IRQ or poll) rather
> + * than directly by hardware.
>    *
>    * Software power control is active in the following cases:
>    * - When the budget evaluation strategy is set to static
> @@ -1119,6 +1123,19 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
>   		goto free_kfifo;
>   	}
>   
> +	if (pcdev->polling) {
> +		pcdev->poll_notifs = devm_kzalloc_objs(pcdev->dev,
> +						       *pcdev->poll_notifs,
> +						       pcdev->nr_lines);
> +		pcdev->poll_notifs_mask = devm_bitmap_zalloc(pcdev->dev,
> +							     pcdev->nr_lines,
> +							     GFP_KERNEL);
> +		if (!pcdev->poll_notifs || !pcdev->poll_notifs_mask) {
> +			ret = -ENOMEM;
> +			goto free_kfifo;
> +		}
> +	}
> +
>   	ret = of_load_pse_pis(pcdev);
>   	if (ret)
>   		goto free_kfifo;
> @@ -1171,6 +1188,14 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
>   	list_add(&pcdev->list, &pse_controller_list);
>   	mutex_unlock(&pse_list_mutex);
>   
> +	/* Start polling only now, so the worker never sees a controller
> +	 * that is not fully set up.
> +	 */

This comment seems a bit useless.

> +	if (pcdev->polling)
> +		queue_delayed_work(system_freezable_power_efficient_wq,
> +				   &pcdev->poll_work,
> +				   msecs_to_jiffies(pcdev->poll_interval_ms));
> +
>   	return 0;
>   
>   free_kfifo:
> @@ -1193,12 +1218,15 @@ void pse_controller_unregister(struct pse_controller_dev *pcdev)
>   	pcdev->unregistering = true;
>   	mutex_unlock(&pcdev->lock);
>   
> -	/* Stop the IRQ and the notification worker first: pse_isr() uses
> -	 * pcdev->pi and the power domains that pse_flush_pw_ds() drops,
> -	 * and the worker reads the kfifo that is freed below.
> +	/* Stop the IRQ, the poll work and the notification worker first:
> +	 * pse_isr() and pse_poll_worker() use pcdev->pi and the power
> +	 * domains that pse_flush_pw_ds() drops, and the worker reads the
> +	 * kfifo that is freed below.
>   	 */

Could you make this comment human readable with only a few words?
Yes I should not have accepted this AI comment in the first place. Now I 
feel that it brings more confusion that explanation.

>   	if (pcdev->irq)
>   		disable_irq(pcdev->irq);
> +	if (pcdev->polling)
> +		cancel_delayed_work_sync(&pcdev->poll_work);
>   	/* Unlink before the PI array is freed: of_pse_control_get() walks
>   	 * the list and reads pcdev->pi[].
>   	 */
> @@ -1326,48 +1354,39 @@ static int pse_set_config_isr(struct pse_controller_dev *pcdev, int id,
>   }
>   
>   /**
> - * pse_isr - IRQ handler for PSE
> - * @irq: irq number
> - * @data: pointer to user interrupt structure
> + * pse_handle_events - Process PSE events for all PIs
> + * @pcdev: a pointer to the PSE controller device
> + * @pi_notifs: per-PI notification array
> + * @notifs_mask: bitmap of PIs with events (sized for pcdev->nr_lines)
>    *
> - * Return: irqreturn_t - status of IRQ
> + * Common event handling shared between IRQ and poll paths.
> + * Caller must hold pcdev->lock.
>    */
> -static irqreturn_t pse_isr(int irq, void *data)
> +static void pse_handle_events(struct pse_controller_dev *pcdev,
> +			      unsigned long *pi_notifs,
> +			      unsigned long *notifs_mask)
>   {
> -	struct pse_controller_dev *pcdev;
> -	struct pse_irq_desc *desc;
> -	struct pse_irq *h = data;
> -	int ret, i;
> -
> -	desc = &h->desc;
> -	pcdev = h->pcdev;
> +	int i;
>   
> -	/* Clear notifs mask */
> -	memset(h->notifs, 0, pcdev->nr_lines * sizeof(*h->notifs));
> -	bitmap_zero(h->notifs_mask, pcdev->nr_lines);
> -	mutex_lock(&pcdev->lock);
> -	ret = desc->map_event(irq, pcdev, h->notifs, h->notifs_mask);
> -	if (ret || bitmap_empty(h->notifs_mask, pcdev->nr_lines)) {
> -		mutex_unlock(&pcdev->lock);
> -		return IRQ_NONE;
> -	}
> +	lockdep_assert_held(&pcdev->lock);
>   
> -	for_each_set_bit(i, h->notifs_mask, pcdev->nr_lines) {
> +	for_each_set_bit(i, notifs_mask, pcdev->nr_lines) {
>   		unsigned long notifs, rnotifs;
>   		struct pse_ntf ntf = {};
> +		int ret;
>   
>   		/* Do nothing PI not described */
>   		if (!pcdev->pi[i].rdev)
>   			continue;
>   
> -		notifs = h->notifs[i];
> +		notifs = pi_notifs[i];
>   		if (pse_pw_d_is_sw_pw_control(pcdev, pcdev->pi[i].pw_d)) {
>   			ret = pse_set_config_isr(pcdev, i, notifs);
>   			if (ret)
>   				notifs |= ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR;
>   		}
>   
> -		dev_dbg(h->pcdev->dev,
> +		dev_dbg(pcdev->dev,
>   			"Sending PSE notification EVT 0x%lx\n", notifs);
>   
>   		ntf.notifs = notifs;
> @@ -1380,12 +1399,73 @@ static irqreturn_t pse_isr(int irq, void *data)
>   		regulator_notifier_call_chain(pcdev->pi[i].rdev, rnotifs,
>   					      NULL);
>   	}
> +}
> +
> +/**
> + * pse_isr - IRQ handler for PSE
> + * @irq: irq number
> + * @data: pointer to user interrupt structure
> + *
> + * Return: irqreturn_t - status of IRQ
> + */
> +static irqreturn_t pse_isr(int irq, void *data)
> +{
> +	struct pse_controller_dev *pcdev;
> +	struct pse_irq *h = data;
> +	int ret;
> +
> +	pcdev = h->pcdev;
> +
> +	/* Clear notifs mask */
> +	memset(h->notifs, 0, pcdev->nr_lines * sizeof(*h->notifs));
> +	bitmap_zero(h->notifs_mask, pcdev->nr_lines);
> +	mutex_lock(&pcdev->lock);
> +	ret = h->desc.map_event(irq, pcdev, h->notifs, h->notifs_mask);
> +	if (ret || bitmap_empty(h->notifs_mask, pcdev->nr_lines)) {
> +		mutex_unlock(&pcdev->lock);
> +		return IRQ_NONE;
> +	}
>   
> +	pse_handle_events(pcdev, h->notifs, h->notifs_mask);
>   	mutex_unlock(&pcdev->lock);
>   
>   	return IRQ_HANDLED;
>   }
>   
> +static void pse_poll_worker(struct work_struct *work)
> +{
> +	struct pse_controller_dev *pcdev =
> +		container_of(work, struct pse_controller_dev,
> +			     poll_work.work);
> +	int ret;
> +
> +	memset(pcdev->poll_notifs, 0,
> +	       pcdev->nr_lines * sizeof(*pcdev->poll_notifs));
> +	bitmap_zero(pcdev->poll_notifs_mask, pcdev->nr_lines);
> +	mutex_lock(&pcdev->lock);
> +	ret = pcdev->poll_desc.map_event(0, pcdev, pcdev->poll_notifs,
> +					 pcdev->poll_notifs_mask);
> +	/* Log only changes: a controller that stopped answering would
> +	 * otherwise log an error at every poll.
> +	 */
> +	if (ret != pcdev->poll_err) {
> +		if (ret)
> +			dev_err(pcdev->dev, "Failed to poll events: %pe\n",
> +				ERR_PTR(ret));
> +		else
> +			dev_info(pcdev->dev, "Polling events works again\n");
> +		pcdev->poll_err = ret;
> +	}
> +	if (!ret && !bitmap_empty(pcdev->poll_notifs_mask, pcdev->nr_lines))
> +		pse_handle_events(pcdev, pcdev->poll_notifs,
> +				  pcdev->poll_notifs_mask);
> +	mutex_unlock(&pcdev->lock);
> +
> +	queue_delayed_work(system_freezable_power_efficient_wq,
> +			   &pcdev->poll_work,
> +			   msecs_to_jiffies(pcdev->poll_interval_ms));
> +}
> +
>   /**
>    * devm_pse_irq_helper - Register IRQ based PSE event notifier
>    * @pcdev: a pointer to the PSE
> @@ -1448,6 +1528,51 @@ int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq,
>   }
>   EXPORT_SYMBOL_GPL(devm_pse_irq_helper);
>   
> +/**
> + * pse_poll_helper - Set up a poll-based PSE event notifier
> + * @pcdev: a pointer to the PSE controller device
> + * @d: PSE event description, the same as for devm_pse_irq_helper().
> + *     d->name is not used.
> + *
> + * For PSE controllers without a usable IRQ. Every poll_interval_ms
> + * (default 500 ms), a delayed work calls d->map_event() with irq 0 and
> + * handles the events like pse_isr() does. The work runs on a freezable
> + * workqueue, so it does not touch the hardware during suspend.
> + *
> + * With the "disabled" budget strategy, the hardware keeps the power
> + * control of a polled controller. The PSE core takes it over only for
> + * an IRQ controller, see pse_pw_d_is_sw_pw_control().
> + *
> + * Must be called before devm_pse_controller_register(), which allocates
> + * the poll buffers and starts the polling once the controller is
> + * registered.
> + *
> + * Return: 0 on success, -EBUSY if the controller is already registered,
> + *	   or another errno on failure
> + */
> +int pse_poll_helper(struct pse_controller_dev *pcdev,
> +		    const struct pse_irq_desc *d)
> +{
> +	if (!d || !d->map_event)
> +		return -EINVAL;
> +
> +	/* pcdev->pi is set by pse_controller_register(), which must come
> +	 * after this call to start the polling.
> +	 */
> +	if (WARN_ON(pcdev->pi))
> +		return -EBUSY;
> +
> +	pcdev->poll_desc = *d;
> +	if (!pcdev->poll_interval_ms)
> +		pcdev->poll_interval_ms = PSE_DEFAULT_POLL_INTERVAL_MS;
> +
> +	INIT_DELAYED_WORK(&pcdev->poll_work, pse_poll_worker);
> +	pcdev->polling = true;
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(pse_poll_helper);
> +
>   /* PSE control section */
>   
>   static void __pse_control_release(struct kref *kref)
> diff --git a/include/linux/pse-pd/pse.h b/include/linux/pse-pd/pse.h
> index 17738ffcd224..e40ed754dbdb 100644
> --- a/include/linux/pse-pd/pse.h
> +++ b/include/linux/pse-pd/pse.h
> @@ -43,10 +43,12 @@ struct ethtool_c33_pse_pw_limit_range {
>   };
>   
>   /**
> - * struct pse_irq_desc - notification sender description for IRQ based events.
> + * struct pse_irq_desc - notification sender description for IRQ or poll
> + *			  based events.
>    *
> - * @name: the visible name for the IRQ
> + * @name: the visible name for the IRQ, not used for polling
>    * @map_event: driver callback to map IRQ status into PSE devices with events.
> + *	       irq is 0 when it is called from the poll work.

You can rather say that the irq value is not used in case of polling.

>    */
>   struct pse_irq_desc {
>   	const char *name;
> @@ -186,8 +188,8 @@ struct ethtool_pse_control_status {
>    * @pi_get_prio: Get the PSE PI priority.
>    * @pi_set_prio: Configure the PSE PI priority.
>    * @pi_get_pw_req: Get the power requested by a PD before enabling the PSE PI.
> - *		   This is only relevant when an interrupt is registered using
> - *		   devm_pse_irq_helper helper.
> + *		   This is only relevant when events are reported, using the
> + *		   devm_pse_irq_helper() or pse_poll_helper() helper.
>    */
>   struct pse_controller_ops {
>   	int (*setup_pi_matrix)(struct pse_controller_dev *pcdev);
> @@ -294,6 +296,14 @@ struct pse_ntf {
>    * @pi: table of PSE PIs described in this controller device
>    * @no_of_pse_pi: flag set if the pse_pis devicetree node is not used
>    * @irq: PSE interrupt
> + * @polling: flag indicating poll-based event detection is active
> + * @poll_interval_ms: poll interval in milliseconds, a driver may set it
> + *		      before pse_poll_helper(), 0 means 500 ms
> + * @poll_work: delayed work for poll-based event detection
> + * @poll_desc: copy of the driver's event descriptor for polling
> + * @poll_notifs: per-PI notification scratch space for poll worker
> + * @poll_notifs_mask: bitmap of PIs with events for poll worker
> + * @poll_err: result of the last poll, to log only changes
>    * @pis_prio_max: Maximum value allowed for the PSE PIs priority
>    * @supp_budget_eval_strategies: budget evaluation strategies supported
>    *				 by the PSE
> @@ -316,6 +326,13 @@ struct pse_controller_dev {
>   	struct pse_pi *pi;
>   	bool no_of_pse_pi;
>   	int irq;
> +	bool polling;
> +	unsigned int poll_interval_ms;
> +	struct delayed_work poll_work;
> +	struct pse_irq_desc poll_desc;
> +	unsigned long *poll_notifs;
> +	unsigned long *poll_notifs_mask;
> +	int poll_err;

I would prefer here to use an internal sub structure with its content 
not accessible from the drivers.

I am glad to see this development and have tested it on a new project 
that does not have the interruption line wired. So you have saved some 
of my time. Thank you!
-- 
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com


  reply	other threads:[~2026-11-30 15:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 12:15 [PATCH net-next v6 0/2] net: pse-pd: add poll path and LED trigger support Carlo Szelinsky
2026-11-28 12:15 ` [PATCH net-next v6 1/2] net: pse-pd: add pse_poll_helper() Carlo Szelinsky
2026-11-30 15:25   ` Kory Maincent [this message]
2026-11-28 12:15 ` [PATCH net-next v6 2/2] net: pse-pd: add LED trigger support via notification path Carlo Szelinsky
2026-11-29 12:15   ` sashiko-bot
2026-11-29 12:16 ` [PATCH net-next v6 0/2] net: pse-pd: add poll path and LED trigger support sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c35babda-b225-49bf-a3d5-fb352b7fd26c@bootlin.com \
    --to=kory.maincent@bootlin.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=github@szelinsky.de \
    --cc=jelonek.jonas@gmail.com \
    --cc=kuba@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=pabeni@redhat.com \
    --cc=pavel@kernel.org \
    --cc=ross@r-sc.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox