From patchwork Wed Sep 10 15:54:22 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Herrmann X-Patchwork-Id: 4879001 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0DDB59F390 for ; Wed, 10 Sep 2014 15:55:03 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 192F52011E for ; Wed, 10 Sep 2014 15:55:02 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 7BD02201D5 for ; Wed, 10 Sep 2014 15:55:00 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BAA4789FDE; Wed, 10 Sep 2014 08:54:59 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wg0-f50.google.com (mail-wg0-f50.google.com [74.125.82.50]) by gabe.freedesktop.org (Postfix) with ESMTP id 60DAC89FE8 for ; Wed, 10 Sep 2014 08:54:58 -0700 (PDT) Received: by mail-wg0-f50.google.com with SMTP id x13so6520510wgg.9 for ; Wed, 10 Sep 2014 08:54:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=DDJyVotZooMuw2cwpgbuKVrEOag+aknhE56f0aOixSM=; b=z9ecDZ/N3y34OuWEZYjJN4efhhGomQiV7y+k2dRF0GrJbQ76YZaLOjd6yxjnA8JVhA aDpprT9DlBCPEILlqwvcgqybFElERVUMEMc7LN5jKX2onjlOvxJMUzq5NeifRHr0xFNc 6Gur7ZsYxMfR9Etpw3ayTrZ1YYHb0XgO7N6YMM2ntOxeHrgzVudPJWKNEy5N5kGBfsae pjC/Rp1Q8hwunS93L8g+h6Qltu98iZ3j5JZkBi78RRt92tkZvznNM8TaEp87eIC9jTYL I3zE2a0/Nd5VCA+0RyQwwIdLppfq0ZOhSbUwQg+h4l28Zyt9mpxoWjZPX1WpxR04ytlS P5fQ== X-Received: by 10.180.88.167 with SMTP id bh7mr926916wib.12.1410364497633; Wed, 10 Sep 2014 08:54:57 -0700 (PDT) Received: from david-tp.localdomain (stgt-5f7015fa.pool.mediaWays.net. [95.112.21.250]) by mx.google.com with ESMTPSA id js2sm19046250wjc.9.2014.09.10.08.54.55 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 10 Sep 2014 08:54:56 -0700 (PDT) From: David Herrmann To: dri-devel@lists.freedesktop.org Subject: [PATCH RFC 3/4] backlight: add kernel-internal backlight API Date: Wed, 10 Sep 2014 17:54:22 +0200 Message-Id: <1410364463-12692-4-git-send-email-dh.herrmann@gmail.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1410364463-12692-1-git-send-email-dh.herrmann@gmail.com> References: <1410364463-12692-1-git-send-email-dh.herrmann@gmail.com> Cc: Matthew Garrett , Daniel Vetter , Bryan Wu , linux-kernel@vger.kernel.org, Lee Jones X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-6.6 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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)