diff mbox series

[leds,5/8] leds: turris-omnia: Notify sysfs on MCU global LEDs brightness change

Message ID 20240902124104.14297-6-kabel@kernel.org (mailing list archive)
State Superseded
Headers show
Series Turris Omnia LED driver changes | expand

Commit Message

Marek Behún Sept. 2, 2024, 12:41 p.m. UTC
Recall that on Turris Omnia, the LED controller has a global brightness
property, which allows the user to make the front LED panel dimmer.

There is also a button on the front panel, which by default is
configured so that pressing it changes the global brightness to a lower
value (unless it is at 0%, in which case pressing the button changes the
global brightness to 100%).

Newer versions of the MCU firmware support informing the SOC that the
brightness was changed by button press event via an interrupt.

Now that we have the turris-omnia-mcu driver, which adds support for MCU
interrupts, add the ability to inform the userspace (via a sysfs
notification) that the global brightness was changed.

Signed-off-by: Marek Behún <kabel@kernel.org>
---
 drivers/leds/leds-turris-omnia.c | 67 ++++++++++++++++++++++++++++++--
 1 file changed, 64 insertions(+), 3 deletions(-)

Comments

Andy Shevchenko Sept. 2, 2024, 1:17 p.m. UTC | #1
On Mon, Sep 02, 2024 at 02:41:01PM +0200, Marek Behún wrote:
> Recall that on Turris Omnia, the LED controller has a global brightness
> property, which allows the user to make the front LED panel dimmer.
> 
> There is also a button on the front panel, which by default is
> configured so that pressing it changes the global brightness to a lower
> value (unless it is at 0%, in which case pressing the button changes the
> global brightness to 100%).
> 
> Newer versions of the MCU firmware support informing the SOC that the
> brightness was changed by button press event via an interrupt.
> 
> Now that we have the turris-omnia-mcu driver, which adds support for MCU
> interrupts, add the ability to inform the userspace (via a sysfs
> notification) that the global brightness was changed.

...

> +	ret = devm_device_add_group(dev, &omnia_led_controller_group);

AFAIU the intention is to remove that API, hence this shall not be used.
Greg?

https://lore.kernel.org/lkml/20221109140711.105222-1-gregkh@linuxfoundation.org/
Marek Behún Sept. 2, 2024, 2:36 p.m. UTC | #2
On Mon, Sep 02, 2024 at 04:17:15PM +0300, Andy Shevchenko wrote:
> On Mon, Sep 02, 2024 at 02:41:01PM +0200, Marek Behún wrote:
> > Recall that on Turris Omnia, the LED controller has a global brightness
> > property, which allows the user to make the front LED panel dimmer.
> > 
> > There is also a button on the front panel, which by default is
> > configured so that pressing it changes the global brightness to a lower
> > value (unless it is at 0%, in which case pressing the button changes the
> > global brightness to 100%).
> > 
> > Newer versions of the MCU firmware support informing the SOC that the
> > brightness was changed by button press event via an interrupt.
> > 
> > Now that we have the turris-omnia-mcu driver, which adds support for MCU
> > interrupts, add the ability to inform the userspace (via a sysfs
> > notification) that the global brightness was changed.
> 
> ...
> 
> > +	ret = devm_device_add_group(dev, &omnia_led_controller_group);
> 
> AFAIU the intention is to remove that API, hence this shall not be used.
> Greg?

OMG yes this should be with the group .is_visible method.
Sorry.
diff mbox series

Patch

diff --git a/drivers/leds/leds-turris-omnia.c b/drivers/leds/leds-turris-omnia.c
index 14e8fbb5bb69..bf8635cec72e 100644
--- a/drivers/leds/leds-turris-omnia.c
+++ b/drivers/leds/leds-turris-omnia.c
@@ -357,7 +357,57 @@  static struct attribute *omnia_led_controller_attrs[] = {
 	&dev_attr_gamma_correction.attr,
 	NULL,
 };
-ATTRIBUTE_GROUPS(omnia_led_controller);
+
+static const struct attribute_group omnia_led_controller_group = {
+	.attrs = omnia_led_controller_attrs,
+};
+
+static irqreturn_t omnia_brightness_changed_handler(int irq, void *dev_id)
+{
+	struct kernfs_node *brightness_kn = dev_id;
+
+	sysfs_notify_dirent(brightness_kn);
+
+	return IRQ_HANDLED;
+}
+
+static void brightness_kn_release(struct device *dev, void *res)
+{
+	struct kernfs_node **brightness_kn = res;
+
+	sysfs_put(*brightness_kn);
+}
+
+static int omnia_probe_brightness_interrupt(struct i2c_client *client)
+{
+	struct kernfs_node **brightness_kn;
+	struct device *dev = &client->dev;
+	int ret;
+
+	brightness_kn = devres_alloc(brightness_kn_release,
+				     sizeof(*brightness_kn), GFP_KERNEL);
+	if (!brightness_kn)
+		return -ENOMEM;
+
+	*brightness_kn = sysfs_get_dirent(dev->kobj.sd, "brightness");
+	if (!*brightness_kn) {
+		devres_free(brightness_kn);
+		return -EIO;
+	}
+
+	devres_add(dev, brightness_kn);
+
+	ret = devm_request_any_context_irq(dev, client->irq,
+					   omnia_brightness_changed_handler,
+					   IRQF_ONESHOT, "leds-turris-omnia",
+					   *brightness_kn);
+	if (ret < 0) {
+		dev_err(dev, "Cannot request IRQ: %d\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
 
 static int omnia_mcu_get_features(const struct i2c_client *client)
 {
@@ -387,6 +437,7 @@  static int omnia_leds_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
 	struct device_node *np = dev_of_node(dev);
+	bool has_brightness_interrupt;
 	struct omnia_leds *leds;
 	struct omnia_led *led;
 	int ret, count;
@@ -414,6 +465,8 @@  static int omnia_leds_probe(struct i2c_client *client)
 		return ret;
 	}
 
+	has_brightness_interrupt = ret & OMNIA_FEAT_BRIGHTNESS_INT;
+
 	leds->has_gamma_correction = ret & OMNIA_FEAT_LED_GAMMA_CORRECTION;
 	if (!leds->has_gamma_correction) {
 		dev_info(dev,
@@ -439,7 +492,16 @@  static int omnia_leds_probe(struct i2c_client *client)
 		led += ret;
 	}
 
-	return 0;
+	ret = devm_device_add_group(dev, &omnia_led_controller_group);
+	if (ret < 0) {
+		dev_err(dev, "Cannot add sysfs attribute group: %d\n", ret);
+		return ret;
+	}
+
+	if (has_brightness_interrupt)
+		ret = omnia_probe_brightness_interrupt(client);
+
+	return ret;
 }
 
 static void omnia_leds_remove(struct i2c_client *client)
@@ -479,7 +541,6 @@  static struct i2c_driver omnia_leds_driver = {
 	.driver		= {
 		.name	= "leds-turris-omnia",
 		.of_match_table = of_omnia_leds_match,
-		.dev_groups = omnia_led_controller_groups,
 	},
 };