summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Herrmann <dh.herrmann at gmail.com>2014-09-10 08:54:22 -0700
committerMartin Peres <martin.peres@linux.intel.com>2017-01-23 10:26:54 +0200
commit0faaadb6ded61bd47af7c4a5034ef1afe3a6368e (patch)
tree6cf0323476826cc62ed739d4317223f3b97626b1
parent1b60f6367b4b22a73064fcbee8ac2e946653424e (diff)
backlight: add kernel-internal backlight API
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. V2: Marta Lofstedt <marta.lofstedt@intel.com> - rebase - minor edit for checkpatch warning Signed-off-by: David Herrmann <dh.herrmann@gmail.com> Signed-off-by: Marta Lofstedt <marta.lofstedt@intel.com>
-rw-r--r--drivers/video/backlight/backlight.c65
-rw-r--r--include/linux/backlight.h16
2 files changed, 81 insertions, 0 deletions
diff --git a/drivers/video/backlight/backlight.c b/drivers/video/backlight/backlight.c
index 37f6173dc872..8af860f42507 100644
--- a/drivers/video/backlight/backlight.c
+++ b/drivers/video/backlight/backlight.c
@@ -487,6 +487,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;
+ const char *t;
+
+ mutex_lock(&backlight_dev_list_mutex);
+
+ list_for_each_entry(bd, &backlight_dev_list, entry) {
+ t = dev_name(&bd->dev);
+ if (t && !strcmp(t, name))
+ goto out;
+ }
+
+ bd = NULL;
+
+out:
+ mutex_unlock(&backlight_dev_list_mutex);
+ 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 int)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 5f2fd61ef4fb..895c4661bccd 100644
--- a/include/linux/backlight.h
+++ b/include/linux/backlight.h
@@ -146,6 +146,22 @@ extern int backlight_unregister_notifier(struct notifier_block *nb);
extern struct backlight_device *backlight_device_get_by_type(enum backlight_type type);
extern int backlight_device_set_brightness(struct backlight_device *bd, unsigned long brightness);
+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)