From patchwork Wed Nov 15 07:13:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bjorn Andersson X-Patchwork-Id: 118915 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp3976745qgn; Tue, 14 Nov 2017 23:14:41 -0800 (PST) X-Google-Smtp-Source: AGs4zMbcdra3ApH4XI4kQkQjVkuvypOlUV2Q8gjAdYHQ76SpCMA04bjhKNAl8W3v5/WAiubcb+xw X-Received: by 10.99.62.10 with SMTP id l10mr14494863pga.376.1510730081673; Tue, 14 Nov 2017 23:14:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510730081; cv=none; d=google.com; s=arc-20160816; b=OCWwR0Z6nHOMpfChH7EMwlmmQGXI8VI6che8/FMIhNpSOj9qPNQRv1a72+/RVrRRJY nbJ6Wqy7CVV5EqihJCy84QeLDmtF3bFHCIzcBWTEKt8+qaFjaDQoGc2ym2ZctzkQDXsI b1b8UiFzvjGti71qArduDyzu+zbNGmoyRnvIS8vNOvIgsNBOvSEDPbGRYGSqZHIMXp/Q wpn/SnR/Bh1lwTyxYNPOrnl9TCDQ+nWgImUdkG27jqUoQKs7Y2e7ZSdSKaSgQBb9Tfz5 aeQrlMYQjX730cLVRY9Q27A+XJgTY5hgF2KP9xVFTbLh3G+xFNg3engwM2ATfl71jizQ +RJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=/pfoDBK4LkF9mjzaoqAewU6TA3StzwCLiv+ST01Y7mE=; b=t8ktF28swOlU1ZItSvjVnWoO6PDcFlXTx4SlW+dv5vnAfjZanhtnb2KH7G9OUGI3iW 92ANMczSx1LIB4EZd8UvBcqXaQfDAcuxdCkOCkO/nkr3033sThf7/LbmXgTRqqfyMKN/ V8ak+xZG/ieqyPlmDhHouat3V+CQSwPdiqYw+XlVFIfTqowUiYzES2Y0SALD+0cpHoI0 7m6V957aKzBMU5ZjX5TMwCn6g1Cd24i30dXW9AO1C1bTWdtH34EfX3CoD17V/2lorFJm B0UIamBLC3foOiKVD+4qq+UTHEclRcQtUvIjQBiuIhbSyCy8KXmEbHoo54TQniHu0jHd gv0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=f6qhpAGV; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e11si3882967plj.660.2017.11.14.23.14.41; Tue, 14 Nov 2017 23:14:41 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=f6qhpAGV; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753668AbdKOHOF (ORCPT + 6 others); Wed, 15 Nov 2017 02:14:05 -0500 Received: from mail-pg0-f68.google.com ([74.125.83.68]:48300 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755576AbdKOHNw (ORCPT ); Wed, 15 Nov 2017 02:13:52 -0500 Received: by mail-pg0-f68.google.com with SMTP id s11so11913525pgc.5 for ; Tue, 14 Nov 2017 23:13:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KjMJQky5lkuRJUuk4qns85Mwkl+s0yb9TzOkio7ZQEk=; b=f6qhpAGV9t4DuNdGvMGXxEaHBj1ojYlaU+YK/Ja4agNTxteJcGHCnp3NYDGTwx3D84 apCILZ3CKPtadClOOZI8GiNGSAqlNRfSHtwshmi+FwqJMy+NVUZT90gCKUMVytwmeZm7 76ERz3lBWy/rXZsJsXWK/l0aThZ8NxWNXtrgQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KjMJQky5lkuRJUuk4qns85Mwkl+s0yb9TzOkio7ZQEk=; b=rAAFo9s6Sz20rmWQk4bSbc3b3pFifIkaHbf9VkU0KLMfnl1SfHCvcCcVOtT9IlekVc 9Hw7/tvJ1/nLsdlCK6yFh8RRhytSm1m5tD/WXJNammbttd3IABCT+ogMeCh4QcOd/7QS Ml+WxQ0WqoWghFlmxRfHMjvcLspMbteABn9hqPqjnG3AGXBtSyYpWb/vu2930OURjBqG WxLsbPRRg5E3hUv41WJ1NQiCef6ypwNVz52bdRRj53s67tXtJUfFxStJw13ie5LVhswJ WJErZ6JCNW+nX7keiyRWxJdGkNL79KUY1RrK9D7wlraLyNpRyxYLXiGIb+0X6o3/mGim /JAQ== X-Gm-Message-State: AJaThX7/qfXk/8DnHtwffa7fz5gGRTaglu/n56rALhJ81THMpcNn2WYp Qv93kdfDU2dcaMvT0/g97AZJiQ== X-Received: by 10.84.194.3 with SMTP id g3mr15289886pld.400.1510730031169; Tue, 14 Nov 2017 23:13:51 -0800 (PST) Received: from localhost.localdomain (ip68-111-217-79.sd.sd.cox.net. [68.111.217.79]) by smtp.gmail.com with ESMTPSA id m6sm22357369pfm.121.2017.11.14.23.13.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Nov 2017 23:13:50 -0800 (PST) From: Bjorn Andersson To: Richard Purdie , Jacek Anaszewski , Pavel Machek Cc: linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org, linux-arm-msm@vger.kernel.org, Rob Herring , Mark Rutland , devicetree@vger.kernel.org, Fenglin Wu Subject: [PATCH v3 1/3] leds: core: Introduce generic pattern interface Date: Tue, 14 Nov 2017 23:13:43 -0800 Message-Id: <20171115071345.24331-2-bjorn.andersson@linaro.org> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20171115071345.24331-1-bjorn.andersson@linaro.org> References: <20171115071345.24331-1-bjorn.andersson@linaro.org> Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Some LED controllers have support for autonomously controlling brightness over time, according to some preprogrammed pattern or function. This adds a new optional operator that LED class drivers can implement if they support such functionality as well as a new device attribute to configure the pattern for a given LED. Signed-off-by: Bjorn Andersson --- Changes since v2: - None Changes since v1: - New patch, based on discussions following v1 Documentation/ABI/testing/sysfs-class-led | 20 ++++ drivers/leds/led-class.c | 150 ++++++++++++++++++++++++++++++ include/linux/leds.h | 21 +++++ 3 files changed, 191 insertions(+) -- 2.15.0 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/Documentation/ABI/testing/sysfs-class-led b/Documentation/ABI/testing/sysfs-class-led index 5f67f7ab277b..74a7f5b1f89b 100644 --- a/Documentation/ABI/testing/sysfs-class-led +++ b/Documentation/ABI/testing/sysfs-class-led @@ -61,3 +61,23 @@ Description: gpio and backlight triggers. In case of the backlight trigger, it is useful when driving a LED which is intended to indicate a device in a standby like state. + +What: /sys/class/leds//pattern +Date: July 2017 +KernelVersion: 4.14 +Description: + Specify a pattern for the LED, for LED hardware that support + altering the brightness as a function of time. + + The pattern is given by a series of tuples, of brightness and + duration (ms). The LED is expected to traverse the series and + each brightness value for the specified duration. + + Additionally a repeat marker ":|" can be appended to the + series, which should cause the pattern to be repeated + endlessly. + + As LED hardware might have different capabilities and precision + the requested pattern might be slighly adjusted by the driver + and the resulting pattern of such operation should be returned + when this file is read. diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index b0e2d55acbd6..bd630e2ae967 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -74,6 +74,154 @@ static ssize_t max_brightness_show(struct device *dev, } static DEVICE_ATTR_RO(max_brightness); +static ssize_t pattern_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct led_classdev *led_cdev = dev_get_drvdata(dev); + struct led_pattern *pattern; + size_t offset = 0; + size_t count; + bool repeat; + size_t i; + int n; + + if (!led_cdev->pattern_get) + return -EOPNOTSUPP; + + pattern = led_cdev->pattern_get(led_cdev, &count, &repeat); + if (IS_ERR_OR_NULL(pattern)) + return PTR_ERR(pattern); + + for (i = 0; i < count; i++) { + n = snprintf(buf + offset, PAGE_SIZE - offset, "%d %d", + pattern[i].brightness, pattern[i].delta_t); + + if (offset + n >= PAGE_SIZE) + goto err_nospc; + + offset += n; + + if (i < count - 1) + buf[offset++] = ' '; + } + + if (repeat) { + if (offset + 4 >= PAGE_SIZE) + goto err_nospc; + + memcpy(buf + offset, " :|", 3); + offset += 3; + } + + if (offset + 1 >= PAGE_SIZE) + goto err_nospc; + + buf[offset++] = '\n'; + + kfree(pattern); + return offset; + +err_nospc: + kfree(pattern); + return -ENOSPC; +} + +static ssize_t pattern_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + struct led_classdev *led_cdev = dev_get_drvdata(dev); + struct led_pattern *pattern = NULL; + unsigned long val; + char *sbegin; + char *elem; + char *s; + int len = 0; + int ret = 0; + bool odd = true; + bool repeat = false; + + s = sbegin = kstrndup(buf, size, GFP_KERNEL); + if (!s) + return -ENOMEM; + + /* Trim trailing newline */ + s[strcspn(s, "\n")] = '\0'; + + /* If the remaining string is empty, clear the pattern */ + if (!s[0]) { + ret = led_cdev->pattern_clear(led_cdev); + goto out; + } + + pattern = kcalloc(size, sizeof(*pattern), GFP_KERNEL); + if (!pattern) { + ret = -ENOMEM; + goto out; + } + + /* Parse out the brightness & delta_t touples and check for repeat */ + while ((elem = strsep(&s, " ")) != NULL) { + if (!strcmp(elem, ":|")) { + repeat = true; + break; + } + + ret = kstrtoul(elem, 10, &val); + if (ret) + goto out; + + if (odd) { + pattern[len].brightness = val; + } else { + /* Ensure we don't have any delta_t == 0 */ + if (!val) { + ret = -EINVAL; + goto out; + } + + pattern[len].delta_t = val; + len++; + } + + odd = !odd; + } + + /* + * Fail if we didn't find any data points or last data point was partial + */ + if (!len || !odd) { + ret = -EINVAL; + goto out; + } + + ret = led_cdev->pattern_set(led_cdev, pattern, len, repeat); + +out: + kfree(pattern); + kfree(sbegin); + return ret < 0 ? ret : size; +} + +static DEVICE_ATTR_RW(pattern); + +static umode_t led_class_attrs_mode(struct kobject *kobj, + struct attribute *attr, + int index) +{ + struct device *dev = container_of(kobj, struct device, kobj); + struct led_classdev *led_cdev = dev_get_drvdata(dev); + + if (attr == &dev_attr_brightness.attr) + return attr->mode; + if (attr == &dev_attr_max_brightness.attr) + return attr->mode; + if (attr == &dev_attr_pattern.attr && led_cdev->pattern_set) + return attr->mode; + + return 0; +} + #ifdef CONFIG_LEDS_TRIGGERS static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store); static struct attribute *led_trigger_attrs[] = { @@ -88,11 +236,13 @@ static const struct attribute_group led_trigger_group = { static struct attribute *led_class_attrs[] = { &dev_attr_brightness.attr, &dev_attr_max_brightness.attr, + &dev_attr_pattern.attr, NULL, }; static const struct attribute_group led_group = { .attrs = led_class_attrs, + .is_visible = led_class_attrs_mode, }; static const struct attribute_group *led_groups[] = { diff --git a/include/linux/leds.h b/include/linux/leds.h index bf6db4fe895b..584c79ff5bb5 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -33,6 +33,8 @@ enum led_brightness { LED_FULL = 255, }; +struct led_pattern; + struct led_classdev { const char *name; enum led_brightness brightness; @@ -88,6 +90,15 @@ struct led_classdev { unsigned long *delay_on, unsigned long *delay_off); + int (*pattern_set)(struct led_classdev *led_cdev, + struct led_pattern *pattern, int len, + bool repeat); + + int (*pattern_clear)(struct led_classdev *led_cdev); + + struct led_pattern *(*pattern_get)(struct led_classdev *led_cdev, + size_t *len, bool *repeat); + struct device *dev; const struct attribute_group **groups; @@ -446,4 +457,14 @@ static inline void led_classdev_notify_brightness_hw_changed( struct led_classdev *led_cdev, enum led_brightness brightness) { } #endif +/** + * struct led_pattern - brigheness value in a pattern + * @delta_t: delay until next entry, in milliseconds + * @brightness: brightness at time = 0 + */ +struct led_pattern { + int delta_t; + int brightness; +}; + #endif /* __LINUX_LEDS_H_INCLUDED */