18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Backlight emulation LED trigger
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2008 (C) Rodolfo Giometti <giometti@linux.it>
68c2ecf20Sopenharmony_ci * Copyright 2008 (C) Eurotech S.p.A. <info@eurotech.it>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/fb.h>
148c2ecf20Sopenharmony_ci#include <linux/leds.h>
158c2ecf20Sopenharmony_ci#include "../leds.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define BLANK		1
188c2ecf20Sopenharmony_ci#define UNBLANK		0
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistruct bl_trig_notifier {
218c2ecf20Sopenharmony_ci	struct led_classdev *led;
228c2ecf20Sopenharmony_ci	int brightness;
238c2ecf20Sopenharmony_ci	int old_status;
248c2ecf20Sopenharmony_ci	struct notifier_block notifier;
258c2ecf20Sopenharmony_ci	unsigned invert;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic int fb_notifier_callback(struct notifier_block *p,
298c2ecf20Sopenharmony_ci				unsigned long event, void *data)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct bl_trig_notifier *n = container_of(p,
328c2ecf20Sopenharmony_ci					struct bl_trig_notifier, notifier);
338c2ecf20Sopenharmony_ci	struct led_classdev *led = n->led;
348c2ecf20Sopenharmony_ci	struct fb_event *fb_event = data;
358c2ecf20Sopenharmony_ci	int *blank;
368c2ecf20Sopenharmony_ci	int new_status;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/* If we aren't interested in this event, skip it immediately ... */
398c2ecf20Sopenharmony_ci	if (event != FB_EVENT_BLANK)
408c2ecf20Sopenharmony_ci		return 0;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	blank = fb_event->data;
438c2ecf20Sopenharmony_ci	new_status = *blank ? BLANK : UNBLANK;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	if (new_status == n->old_status)
468c2ecf20Sopenharmony_ci		return 0;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	if ((n->old_status == UNBLANK) ^ n->invert) {
498c2ecf20Sopenharmony_ci		n->brightness = led->brightness;
508c2ecf20Sopenharmony_ci		led_set_brightness_nosleep(led, LED_OFF);
518c2ecf20Sopenharmony_ci	} else {
528c2ecf20Sopenharmony_ci		led_set_brightness_nosleep(led, n->brightness);
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	n->old_status = new_status;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return 0;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic ssize_t bl_trig_invert_show(struct device *dev,
618c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", n->invert);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic ssize_t bl_trig_invert_store(struct device *dev,
698c2ecf20Sopenharmony_ci		struct device_attribute *attr, const char *buf, size_t num)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	struct led_classdev *led = led_trigger_get_led(dev);
728c2ecf20Sopenharmony_ci	struct bl_trig_notifier *n = led_trigger_get_drvdata(dev);
738c2ecf20Sopenharmony_ci	unsigned long invert;
748c2ecf20Sopenharmony_ci	int ret;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	ret = kstrtoul(buf, 10, &invert);
778c2ecf20Sopenharmony_ci	if (ret < 0)
788c2ecf20Sopenharmony_ci		return ret;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (invert > 1)
818c2ecf20Sopenharmony_ci		return -EINVAL;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	n->invert = invert;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/* After inverting, we need to update the LED. */
868c2ecf20Sopenharmony_ci	if ((n->old_status == BLANK) ^ n->invert)
878c2ecf20Sopenharmony_ci		led_set_brightness_nosleep(led, LED_OFF);
888c2ecf20Sopenharmony_ci	else
898c2ecf20Sopenharmony_ci		led_set_brightness_nosleep(led, n->brightness);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	return num;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_cistatic DEVICE_ATTR(inverted, 0644, bl_trig_invert_show, bl_trig_invert_store);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic struct attribute *bl_trig_attrs[] = {
968c2ecf20Sopenharmony_ci	&dev_attr_inverted.attr,
978c2ecf20Sopenharmony_ci	NULL,
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(bl_trig);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic int bl_trig_activate(struct led_classdev *led)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	int ret;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	struct bl_trig_notifier *n;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	n = kzalloc(sizeof(struct bl_trig_notifier), GFP_KERNEL);
1088c2ecf20Sopenharmony_ci	if (!n)
1098c2ecf20Sopenharmony_ci		return -ENOMEM;
1108c2ecf20Sopenharmony_ci	led_set_trigger_data(led, n);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	n->led = led;
1138c2ecf20Sopenharmony_ci	n->brightness = led->brightness;
1148c2ecf20Sopenharmony_ci	n->old_status = UNBLANK;
1158c2ecf20Sopenharmony_ci	n->notifier.notifier_call = fb_notifier_callback;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	ret = fb_register_client(&n->notifier);
1188c2ecf20Sopenharmony_ci	if (ret)
1198c2ecf20Sopenharmony_ci		dev_err(led->dev, "unable to register backlight trigger\n");
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return 0;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic void bl_trig_deactivate(struct led_classdev *led)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct bl_trig_notifier *n = led_get_trigger_data(led);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	fb_unregister_client(&n->notifier);
1298c2ecf20Sopenharmony_ci	kfree(n);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic struct led_trigger bl_led_trigger = {
1338c2ecf20Sopenharmony_ci	.name		= "backlight",
1348c2ecf20Sopenharmony_ci	.activate	= bl_trig_activate,
1358c2ecf20Sopenharmony_ci	.deactivate	= bl_trig_deactivate,
1368c2ecf20Sopenharmony_ci	.groups		= bl_trig_groups,
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_cimodule_led_trigger(bl_led_trigger);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciMODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1418c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Backlight emulation LED trigger");
1428c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
143