From patchwork Mon Jun 25 05:03:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 139796 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp3542732lji; Sun, 24 Jun 2018 22:04:22 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIXea7NjXRVPyYIARZoirvTduIDEGcRQcgEgrow5v8STcAZBFIdbewrsVsC1/kVGsfEeL8S X-Received: by 2002:a17:902:1127:: with SMTP id d36-v6mr11046674pla.267.1529903062052; Sun, 24 Jun 2018 22:04:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529903062; cv=none; d=google.com; s=arc-20160816; b=Zkg6RVANa/MXBn31+zay86kUMhu0k/2zpNvGPSoHTq6ZnSBv+vMBNNSXfARmbHFUff 9DDFJ1uPtLCCP89AUstzwgwQyl38EZ+kuQexBLtvyiGfGDaG/o8+SdZ9G2f5MDQ0He4x 3DDCmtmAY/1HVIpeD//KhHTzEHPEhKwbW0qD/IIG+uFa+CGbM4fV8uV7x6M0m4DDxTxK SEQFloYaAqId4Vkj+BjCjoJ0jPAX8ePcTUSmKRtVLZnMgFIuoOt7vr92PnqdcTnBgS0P Wv4bZNDMEtNutWX2BQFf3kJuAg2ot8mZpK6TeUENulWo1aK30GyLCjq72OLt7y8DxA3J CfMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=sAXeFmYUgCL/4pfnvuUEpMiXSjTdNPix56iSIf02CTs=; b=QGcpB2nufVAABbhB4wpMHGF6yYtmOfOz+a+6R18hrQS4rZzIFTSAWsaFWLyMJtm/va yW6124PaZAOBjdD2V+ALDDmDzs/pVM6vXV3DEg0vkbS54yh/xeQcSjoDaUHpjGXUvwNU B1SQyGEwMUZ0U9hA3p4aZtXtAUtdStsHXFhxMAJtAsDty+LtGHyhigvCL/1o96GDonuw Y1SCdSyCbHK/0wkFiBXdU2uACMHB2G8HmxKoHflQGSGgbR9LeYLqVhXbfbo4C3Ps1RUN VyEsIBfbPpj0WVmyl2tcwVk+5oJSSzvmoIG90KBeG28Kq+Ds7UTvQZX5+8I1LmkgQ19q xrCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MPqwIwph; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org; dmarc=pass (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 l30-v6si12847911plg.420.2018.06.24.22.04.21; Sun, 24 Jun 2018 22:04:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MPqwIwph; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751045AbeFYFEV (ORCPT + 1 other); Mon, 25 Jun 2018 01:04:21 -0400 Received: from mail-pl0-f66.google.com ([209.85.160.66]:40027 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750825AbeFYFEU (ORCPT ); Mon, 25 Jun 2018 01:04:20 -0400 Received: by mail-pl0-f66.google.com with SMTP id t6-v6so886538plo.7 for ; Sun, 24 Jun 2018 22:04:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=sAXeFmYUgCL/4pfnvuUEpMiXSjTdNPix56iSIf02CTs=; b=MPqwIwph6SrBmdyJq0W19IjeFetRDAY7VkEzVFwvgO/w2FZ7BDMNU1MScVrHrutzdj Zb/9hRfGVqjvL9y9Gbek/AKIe8TNmNF0m5I1H7JjfK/7CJ2DXG7sWwQpdR7pcCWMmxWY 9hYa6gS6em2HfyD3M/cHEN44PdRut+Q+bIU2Q= 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; bh=sAXeFmYUgCL/4pfnvuUEpMiXSjTdNPix56iSIf02CTs=; b=QAuMg8PY5++PEn8+QUcjmccB+vCyE3VyRBBbw4k/CUs/Bcta1/DDv5R+eGSFT6Qdxp w/CN56uVpAJ/ZJ7zws9SQUveyEZNFG7RKYWS4DH9SzYmLobmIzFMENlPKmryNkJizlom ZJYosdSygsob65dIgSa2NrD8kKbcWz//g69GyiEbgM4J2/vznaVSvq+QtF9o4Vk6+7lK NHqa4wiZoorpQAHwvTzbh8K9g8miyKmvX3G01vl5xv0iuVtuA1SmRyd0bfAc8bQu+ijL BY7zjPgQ87Bo7IxgFGyoDFar+sllE9Qy8r4ytVrUfTavcX3yMTj3Esi9Mv06SJDwIk5S I2Aw== X-Gm-Message-State: APt69E0iiT6V1nxJdYgJVeA9YNdHQBJuSz6XQ09jeRCYHD8r/j9+NPhf FFqws0pAqCag0+fRSWuUESdduLnkiJI= X-Received: by 2002:a17:902:a416:: with SMTP id p22-v6mr10984906plq.228.1529903059788; Sun, 24 Jun 2018 22:04:19 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id x5-v6sm19444695pfh.67.2018.06.24.22.04.16 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 24 Jun 2018 22:04:19 -0700 (PDT) From: Baolin Wang To: jacek.anaszewski@gmail.com, pavel@ucw.cz Cc: bjorn.andersson@linaro.org, baolin.wang@linaro.org, broonie@kernel.org, linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] leds: core: Introduce generic pattern interface Date: Mon, 25 Jun 2018 13:03:19 +0800 Message-Id: <046c0cf717cbdfd55dcdc2bb7463642734e2ca4c.1529902636.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 Sender: linux-leds-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org From: Bjorn Andersson 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 Signed-off-by: Baolin Wang --- Note: I removed the pattern repeat check and will get the repeat number by adding one extra file named 'pattern_repeat' according to previous discussion. --- Documentation/ABI/testing/sysfs-class-led | 16 ++++ drivers/leds/led-class.c | 127 +++++++++++++++++++++++++++++ include/linux/leds.h | 19 +++++ 3 files changed, 162 insertions(+) -- 1.7.9.5 diff --git a/Documentation/ABI/testing/sysfs-class-led b/Documentation/ABI/testing/sysfs-class-led index 5f67f7a..fe90a12 100644 --- a/Documentation/ABI/testing/sysfs-class-led +++ b/Documentation/ABI/testing/sysfs-class-led @@ -61,3 +61,19 @@ 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: June 2018 +KernelVersion: 4.18 +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. + + 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 3c7e348..5ebdd83 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -74,6 +74,131 @@ 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; + int count, n, i; + + if (!led_cdev->pattern_get) + return -EOPNOTSUPP; + + pattern = led_cdev->pattern_get(led_cdev, &count); + 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++] = ' '; + } + + 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 ret, len = 0; + bool odd = true; + + 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) { + 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); + +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 +213,13 @@ static ssize_t max_brightness_show(struct device *dev, 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 b7e8255..acdbb2f 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -22,6 +22,7 @@ #include struct device; +struct led_pattern; /* * LED Core */ @@ -88,6 +89,14 @@ 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); + + int (*pattern_clear)(struct led_classdev *led_cdev); + + struct led_pattern *(*pattern_get)(struct led_classdev *led_cdev, + int *len); + struct device *dev; const struct attribute_group **groups; @@ -446,4 +455,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 */