From patchwork Fri Jul 13 06:21:13 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: 141850 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp416767ljj; Thu, 12 Jul 2018 23:21:44 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdrCLUzW9tHfrlXkHbLimlT+619vaZKEfmDsAC1bDkm7Wv8La/snrvDH886DqXVMl6dXkFL X-Received: by 2002:a17:902:2702:: with SMTP id c2-v6mr4996990plb.297.1531462904040; Thu, 12 Jul 2018 23:21:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531462904; cv=none; d=google.com; s=arc-20160816; b=kvgzUT1/8perSoy1utr9ZZy2pGMwzw/HdnXfMRyrmG9gRYagoGCKea2QQGWj08aRN/ XCk14T6BOvK/V+2voo4k23uGgDzWrI4hgPyimg4vYXx5LHpKt+QN3H2yqgRpe6eo39wT BKCuKELSCAnNeG8X/BQWe97Mkv+Spb6xkr8Axqixlw+bsMeZppvQAWdfDi83n0Q8qgbv xT4x/QX8vc5QPEEx+k1GcPIxxDhrQeaKWUe9fvUPr7HPjcUwiW+4tXD5dzI1YM3z59/a IN2sk1Jji2m2+7Ya37YKoeKeyVXOHvypI3hmLNHLK0vxdfI/hUPVyySWJeoMASL4e5E/ THvw== 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=+0G0cm319x/IzAoVqzdxV4FsrRaG8wFCZDpe3isoq5Q=; b=PmmT/2APPAYsnDzQYgtrnJRMpiUl9Dq8m2p4fpKo2zH1VupF+TA/ATd+A03Ni+lm5a frwyPLKvpP9lLzly+whwAjfuOePadtYbxkWGFgy4PkVA1zpVeJKwic/v51FIggwFlxB/ l1iQ/9rt9/sGyE5Qy9ZS+DK8LtIC3M2Ev1qiZsHXdORE7sEeuh1F6VyToyA1/Fmyi+CF i16urjKbA9gxf/g/94aY4Fjc2+NzkQkYHsc1Y2mV8CGPiC5SiSsfWOGElnxzi5WAMPN3 NqyRCoJzUb5yq/HXLKyoXjs4Ew70ZC8jTA2Mgi0JVPUG7Js0Izlj6/54cZJgm8/bYcSX HBVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="hMeb/smE"; 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 t66-v6si4925317pgt.181.2018.07.12.23.21.43; Thu, 12 Jul 2018 23:21:44 -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="hMeb/smE"; 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 S1726511AbeGMGeu (ORCPT + 1 other); Fri, 13 Jul 2018 02:34:50 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:37721 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726655AbeGMGeu (ORCPT ); Fri, 13 Jul 2018 02:34:50 -0400 Received: by mail-pg1-f196.google.com with SMTP id n15-v6so4653462pgv.4 for ; Thu, 12 Jul 2018 23:21:42 -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=+0G0cm319x/IzAoVqzdxV4FsrRaG8wFCZDpe3isoq5Q=; b=hMeb/smEk0Ve8P3T8L7kMW7+JJThv2lBs3WZ0cD7nRdTrLATWTHxk9C8U/fdfJqSlH clvPKdJ3iO4IvO67lMtYLdUOPCscFLciwu4Y0Qt74kuW9Fhb9PFZAcuvSYL5gFzYmZol wcAUMgRoLSb+Hm9qg3l1PIXxyp6BnXwvJduL8= 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=+0G0cm319x/IzAoVqzdxV4FsrRaG8wFCZDpe3isoq5Q=; b=sdlsLa9zNpnjS2J5S1a3iPFZUdHqgfEEYeSjn/82S0rHwxBQgQUbO3LRSMU36uqod8 Y5sduh0zdQg2asMeSZ1EEggqo6mhuJqtESXJLpp7r+yZ2FsSPS6NJUZixt18UxRUAkwH uVTBQn5m4hWVcYSF4Nr/8tJfxkv+/SGBZmVpJ0yF0ImQgiDMLE8crEmm+ooWLfWyBngm ayrIdfulFW3MGpzCqf1XyNxA5B+PP+2HwxNFmm9nwayUu4FB/1Fp2DP7Ir5qPJ8O5vQJ A3OaDLknYtTO3lo8fDq7klJAne75CLMV5cYKcjPf+cLGsjttRBWIIDAcjR1nG0xBiUGd 2NGQ== X-Gm-Message-State: AOUpUlGAOjMOT/aVK4xb3tYxu6iddvvIjDRa6Fax15ae+wKD/Fsa9PEL 34qP5dxqJTu8hXhb8XgXxiRhlw== X-Received: by 2002:a63:6188:: with SMTP id v130-v6mr4861822pgb.100.1531462902355; Thu, 12 Jul 2018 23:21:42 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id r16-v6sm46006878pfe.173.2018.07.12.23.21.40 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 12 Jul 2018 23:21:41 -0700 (PDT) From: Baolin Wang To: jacek.anaszewski@gmail.com, pavel@ucw.cz Cc: baolin.wang@linaro.org, bjorn.andersson@linaro.org, broonie@kernel.org, linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/2] leds: core: Introduce generic pattern interface Date: Fri, 13 Jul 2018 14:21:13 +0800 Message-Id: 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. [Baolin Wang did some improvements.] Signed-off-by: Bjorn Andersson Signed-off-by: Baolin Wang --- Changes from v3: - Move the check in pattern_show() to of_led_classdev_register(). - Add more documentation to explain how to set/clear one pattern. Changes from v2: - Change kernel version to 4.19. - Force user to return error pointer if failed to issue pattern_get(). - Use strstrip() to trim trailing newline. - Other optimization. Changes from v1: - Add some comments suggested by Pavel. - Change 'delta_t' can be 0. 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 | 20 +++++ drivers/leds/led-class.c | 118 +++++++++++++++++++++++++++++ include/linux/leds.h | 19 +++++ 3 files changed, 157 insertions(+) -- 1.7.9.5 diff --git a/Documentation/ABI/testing/sysfs-class-led b/Documentation/ABI/testing/sysfs-class-led index 5f67f7a..f4b73ad 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 2018 +KernelVersion: 4.19 +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. Duration of + 0 means brightness should immediately change to new value. + + 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. + + Writing non-empty string to this file will active the pattern, + and empty string will disable the pattern. diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index 3c7e348..0992a0e 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -74,6 +74,119 @@ 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; + + pattern = led_cdev->pattern_get(led_cdev, &count); + if (IS_ERR(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; + } + + buf[offset - 1] = '\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; + char *sbegin, *elem, *s; + unsigned long val; + int ret = 0, len = 0; + bool odd = true; + + sbegin = kstrndup(buf, size, GFP_KERNEL); + if (!sbegin) + return -ENOMEM; + + /* + * Trim trailing newline, if the remaining string is empty, + * clear the pattern. + */ + s = strstrip(sbegin); + if (!*s) { + if (led_cdev->pattern_clear) + 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 */ + while ((elem = strsep(&s, " ")) != NULL) { + ret = kstrtoul(elem, 10, &val); + if (ret) + goto out; + + if (odd) { + pattern[len].brightness = val; + } else { + 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 +201,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[] = { @@ -256,6 +371,9 @@ int of_led_classdev_register(struct device *parent, struct device_node *np, char name[LED_MAX_NAME_SIZE]; int ret; + if (led_cdev->pattern_set && !led_cdev->pattern_get) + return -EINVAL; + ret = led_classdev_next_name(led_cdev->name, name, sizeof(name)); if (ret < 0) return ret; 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 */