All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Herrmann <dh.herrmann@gmail.com>
To: dri-devel@lists.freedesktop.org
Cc: Dave Airlie <airlied@gmail.com>,
	Daniel Vetter <daniel.vetter@ffwll.ch>,
	Jingoo Han <jg1.han@samsung.com>, Bryan Wu <cooloney@gmail.com>,
	Lee Jones <lee.jones@linaro.org>,
	Matthew Garrett <matthew.garrett@nebula.com>,
	Adam Jackson <ajax@redhat.com>,
	linux-kernel@vger.kernel.org,
	David Herrmann <dh.herrmann@gmail.com>
Subject: [PATCH RFC 3/4] backlight: add kernel-internal backlight API
Date: Wed, 10 Sep 2014 17:54:22 +0200	[thread overview]
Message-ID: <1410364463-12692-4-git-send-email-dh.herrmann@gmail.com> (raw)
In-Reply-To: <1410364463-12692-1-git-send-email-dh.herrmann@gmail.com>

So far backlights have only been controlled via sysfs. However, sysfs is
not a proper user-space API for runtime modifications, and never was
intended to provide such. The DRM drivers are now prepared to provide
such a backlight link so user-space can control backlight via DRM
connector properties. This allows us to employ the same access-management
we use for mode-setting.

This patch adds few kernel-internal backlight helpers so we can modify
backlights from within DRM.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
---
 drivers/video/backlight/backlight.c | 65 +++++++++++++++++++++++++++++++++++++
 include/linux/backlight.h           | 16 +++++++++
 2 files changed, 81 insertions(+)

diff --git a/drivers/video/backlight/backlight.c b/drivers/video/backlight/backlight.c
index 33b64be..04f323b 100644
--- a/drivers/video/backlight/backlight.c
+++ b/drivers/video/backlight/backlight.c
@@ -480,6 +480,71 @@ int backlight_unregister_notifier(struct notifier_block *nb)
 EXPORT_SYMBOL(backlight_unregister_notifier);
 
 /**
+ * backlight_device_lookup - find a backlight device
+ * @name: sysname of the backlight device
+ *
+ * @return Reference to the backlight device, NULL if not found.
+ *
+ * This searches through all registered backlight devices for a device with the
+ * given device name. In case none is found, NULL is returned, otherwise a
+ * new reference to the backlight device is returned. You must drop this
+ * reference via backlight_device_unref() once done.
+ * Note that the devices might get unregistered at any time. You need to lock
+ * around this lookup and inside of your backlight-notifier if you need to know
+ * when a device gets unregistered.
+ *
+ * This function can be safely called from IRQ context.
+ */
+struct backlight_device *backlight_device_lookup(const char *name)
+{
+	struct backlight_device *bd;
+	unsigned long flags;
+	const char *t;
+
+	spin_lock_irqsave(&backlight_dev_list_lock, flags);
+
+	list_for_each_entry(bd, &backlight_dev_list, entry) {
+		t = dev_name(&bd->dev);
+		if (t && !strcmp(t, name))
+			goto out;
+	}
+
+	bd = NULL;
+
+out:
+	spin_unlock_irqrestore(&backlight_dev_list_lock, flags);
+	backlight_device_ref(bd);
+	return bd;
+}
+EXPORT_SYMBOL_GPL(backlight_device_lookup);
+
+/**
+ * backlight_set_brightness - set brightness on a backlight device
+ * @bd: backlight device to operate on
+ * @value: brightness value to set on the device
+ * @reason: backlight-change reason to use for notifications
+ *
+ * This is the in-kernel API equivalent of writing into the 'brightness' sysfs
+ * file. It calls into the underlying backlight driver to change the brightness
+ * value. The value is clamped according to device bounds.
+ * A uevent notification is sent with the reason set to @reason.
+ */
+void backlight_set_brightness(struct backlight_device *bd, unsigned int value,
+			      enum backlight_update_reason reason)
+{
+	mutex_lock(&bd->ops_lock);
+	if (bd->ops) {
+		value = clamp(value, 0U, (unsigned)bd->props.max_brightness);
+		pr_debug("set brightness to %u\n", value);
+		bd->props.brightness = value;
+		backlight_update_status(bd);
+	}
+	mutex_unlock(&bd->ops_lock);
+	backlight_generate_event(bd, reason);
+}
+EXPORT_SYMBOL_GPL(backlight_set_brightness);
+
+/**
  * devm_backlight_device_register - resource managed backlight_device_register()
  * @dev: the device to register
  * @name: the name of the device
diff --git a/include/linux/backlight.h b/include/linux/backlight.h
index adb14a8..bcc0dec 100644
--- a/include/linux/backlight.h
+++ b/include/linux/backlight.h
@@ -141,6 +141,22 @@ extern bool backlight_device_registered(enum backlight_type type);
 extern int backlight_register_notifier(struct notifier_block *nb);
 extern int backlight_unregister_notifier(struct notifier_block *nb);
 
+struct backlight_device *backlight_device_lookup(const char *name);
+void backlight_set_brightness(struct backlight_device *bd, unsigned int value,
+			      enum backlight_update_reason reason);
+
+static inline void backlight_device_ref(struct backlight_device *bd)
+{
+	if (bd)
+		get_device(&bd->dev);
+}
+
+static inline void backlight_device_unref(struct backlight_device *bd)
+{
+	if (bd)
+		put_device(&bd->dev);
+}
+
 #define to_backlight_device(obj) container_of(obj, struct backlight_device, dev)
 
 static inline void * bl_get_data(struct backlight_device *bl_dev)
-- 
2.1.0


  parent reply	other threads:[~2014-09-10 15:55 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-10 15:54 [PATCH RFC 0/4] Linking DRM Connectors to Backlight Devices David Herrmann
2014-09-10 15:54 ` David Herrmann
2014-09-10 15:54 ` [PATCH RFC 1/4] backlight: use static initializers David Herrmann
2014-09-10 15:54   ` David Herrmann
2014-09-11  8:59   ` Jani Nikula
2014-09-11  8:59     ` Jani Nikula
2014-09-10 15:54 ` [PATCH RFC 2/4] backlight: use spin-lock to protect device list David Herrmann
2014-09-10 15:54   ` David Herrmann
2014-09-11  9:00   ` Jani Nikula
2014-09-11  9:00     ` Jani Nikula
2014-09-10 15:54 ` David Herrmann [this message]
2014-09-11 11:10   ` [PATCH RFC 3/4] backlight: add kernel-internal backlight API Thierry Reding
2014-09-11 11:10     ` Thierry Reding
2014-09-11 11:14     ` David Herrmann
2014-09-11 11:14       ` David Herrmann
2014-09-11 11:21       ` Thierry Reding
2014-09-11 11:21         ` Thierry Reding
2014-09-10 15:54 ` [PATCH RFC 4/4] drm: link connectors to backlight devices David Herrmann
2014-09-10 15:54   ` David Herrmann
2014-09-11  6:48   ` Daniel Vetter
2014-09-11  6:48     ` Daniel Vetter
2014-09-11 12:22     ` David Herrmann
2014-09-11 13:06       ` Daniel Vetter
2014-09-11 13:06         ` Daniel Vetter
2014-09-11 16:07         ` David Herrmann
2014-09-11 12:46     ` Jani Nikula
2014-09-10 20:40 ` [PATCH RFC 0/4] Linking DRM Connectors to Backlight Devices Matthew Garrett
2014-09-10 20:40   ` Matthew Garrett
2014-09-11 12:48   ` David Herrmann
2014-09-11 12:48     ` David Herrmann
2016-10-24 13:08 ` [PATCH 0/6] Rebase of David Herrmann drm connector link to backlight device Marta Lofstedt
2016-10-24 13:08   ` [PATCH 1/6] backlight: use static initializers Marta Lofstedt
2016-10-24 13:08   ` [PATCH 2/6] backlight: use spin-lock to protect device list Marta Lofstedt
2016-10-24 13:08   ` [PATCH 3/6] backlight: add kernel-internal backlight API Marta Lofstedt
2016-10-24 13:08   ` [PATCH 4/6] drm: link connectors to backlight devices Marta Lofstedt
2016-10-24 13:08   ` [PATCH 5/6] i915: Use drm backlight Marta Lofstedt
2016-10-24 13:08   ` [PATCH 6/6] drm: drm_backlight use the connect value to set brightness property Marta Lofstedt
2016-10-24 14:33   ` [PATCH 0/6] Rebase of David Herrmann drm connector link to backlight device Daniel Vetter

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=1410364463-12692-4-git-send-email-dh.herrmann@gmail.com \
    --to=dh.herrmann@gmail.com \
    --cc=airlied@gmail.com \
    --cc=ajax@redhat.com \
    --cc=cooloney@gmail.com \
    --cc=daniel.vetter@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jg1.han@samsung.com \
    --cc=lee.jones@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthew.garrett@nebula.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.