Skip to content

Commit d82e09d

Browse files
elkablolag-linaro
authored andcommitted
leds: turris-omnia: Notify sysfs on MCU global LEDs brightness change
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> Link: https://lore.kernel.org/r/20241111100355.6978-8-kabel@kernel.org Signed-off-by: Lee Jones <lee@kernel.org>
1 parent 8ca5bf8 commit d82e09d

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

drivers/leds/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ config LEDS_TURRIS_OMNIA
218218
depends on MACH_ARMADA_38X || COMPILE_TEST
219219
depends on OF
220220
depends on TURRIS_OMNIA_MCU
221+
depends on TURRIS_OMNIA_MCU_GPIO
221222
select LEDS_TRIGGERS
222223
help
223224
This option enables basic support for the LEDs found on the front

drivers/leds/leds-turris-omnia.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ struct omnia_led {
4242
* @client: I2C client device
4343
* @lock: mutex to protect cached state
4444
* @has_gamma_correction: whether the MCU firmware supports gamma correction
45+
* @brightness_knode: kernel node of the "brightness" device sysfs attribute (this is the
46+
* driver specific global brightness, not the LED classdev brightness)
4547
* @leds: flexible array of per-LED data
4648
*/
4749
struct omnia_leds {
4850
struct i2c_client *client;
4951
struct mutex lock;
5052
bool has_gamma_correction;
53+
struct kernfs_node *brightness_knode;
5154
struct omnia_led leds[];
5255
};
5356

@@ -372,6 +375,59 @@ static struct attribute *omnia_led_controller_attrs[] = {
372375
};
373376
ATTRIBUTE_GROUPS(omnia_led_controller);
374377

378+
static irqreturn_t omnia_brightness_changed_threaded_fn(int irq, void *data)
379+
{
380+
struct omnia_leds *leds = data;
381+
382+
if (unlikely(!leds->brightness_knode)) {
383+
/*
384+
* Note that sysfs_get_dirent() may sleep. This is okay, because we are in threaded
385+
* context.
386+
*/
387+
leds->brightness_knode = sysfs_get_dirent(leds->client->dev.kobj.sd, "brightness");
388+
if (!leds->brightness_knode)
389+
return IRQ_NONE;
390+
}
391+
392+
sysfs_notify_dirent(leds->brightness_knode);
393+
394+
return IRQ_HANDLED;
395+
}
396+
397+
static void omnia_brightness_knode_put(void *data)
398+
{
399+
struct omnia_leds *leds = data;
400+
401+
if (leds->brightness_knode)
402+
sysfs_put(leds->brightness_knode);
403+
}
404+
405+
static int omnia_request_brightness_irq(struct omnia_leds *leds)
406+
{
407+
struct device *dev = &leds->client->dev;
408+
int ret;
409+
410+
if (!leds->client->irq) {
411+
dev_info(dev,
412+
"Brightness change interrupt supported by MCU firmware but not described in device-tree\n");
413+
414+
return 0;
415+
}
416+
417+
/*
418+
* Registering the brightness_knode destructor before requesting the IRQ ensures that on
419+
* removal the brightness_knode sysfs node is put only after the IRQ is freed.
420+
* This is needed because the interrupt handler uses the knode.
421+
*/
422+
ret = devm_add_action(dev, omnia_brightness_knode_put, leds);
423+
if (ret < 0)
424+
return ret;
425+
426+
return devm_request_threaded_irq(dev, leds->client->irq, NULL,
427+
omnia_brightness_changed_threaded_fn, IRQF_ONESHOT,
428+
"leds-turris-omnia", leds);
429+
}
430+
375431
static int omnia_mcu_get_features(const struct i2c_client *mcu_client)
376432
{
377433
u16 reply;
@@ -458,6 +514,12 @@ static int omnia_leds_probe(struct i2c_client *client)
458514
"Consider upgrading MCU firmware with the omnia-mcutool utility.\n");
459515
}
460516

517+
if (ret & OMNIA_FEAT_BRIGHTNESS_INT) {
518+
ret = omnia_request_brightness_irq(leds);
519+
if (ret < 0)
520+
return dev_err_probe(dev, ret, "Cannot request brightness IRQ\n");
521+
}
522+
461523
mutex_init(&leds->lock);
462524

463525
ret = devm_led_trigger_register(dev, &omnia_hw_trigger);

0 commit comments

Comments
 (0)