Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp6418228imm; Mon, 23 Jul 2018 18:08:58 -0700 (PDT) X-Google-Smtp-Source: AAOMgpd8AfqE5T8qu4YOv865EyjvKoBoxpgwgLAtY/01oMYhnfShnAhtiC10t45riC51fP2Au4KC X-Received: by 2002:a63:4763:: with SMTP id w35-v6mr14083141pgk.140.1532394538335; Mon, 23 Jul 2018 18:08:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532394538; cv=none; d=google.com; s=arc-20160816; b=WT/6iPyMcdBs2OET/epuXch8IC1vIINpAS74nUlF+vZqUexS9GdzEkF07XXbkpbL4T lMyoTmQj6tE39apIAsPBayFagppkxdf7bd9UTmZIrH2iWfyAkV2kWqBTVtavjlgYHwKT sDNBJkFWP50uDsSApkeMHG53Pgf2feI9KQ+/3xZu8qAhxkF9A4qtZKVP3K9MfkF+1ZSg CcCT3T4gPEwuYonDhvl0halxzelBAFQsN4c5MX+Y2CaEr//TvdpYrJQ2ksZDtz3v1J2q h8UsPWVMkBtB2VfxTpUAMEj+rsZE83h8SRKo/tfHf+XEfa0WnfkOp+zGCe51HfUzcvZr qfmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=zyZf6/KqGIMDgpREyYCY0R+wT7ZUloWtCIe+utSRNvA=; b=VRskSAFea+Qwf9DHQRCcGubKVHfGEoSy6+KYaNinrLBFHHXP+xJXV+oounq49E3ipw PHtM6Nx7LQ6sNp1ULHThoU/sDxM9VtYAQoQqzzXoqDN0K4MSBhLik+JHkJYYu/4taJ7z q69d/2CQSB6+r9AU5HsVESVfGtnuWh/bSiRdQb+e2X8qydMdWwXNKIAC6va/lba98uHn Hf5XB+zHxotgIXX5mmV+vxkdlzjhseDBLss68S86+ydZ9XYd+hNLQXI9AKK/C7fiM5+M jMTeRVxH8ZBwdQK33wLrtH7oB0snog5iDjg3nyAt06ZfpIBX29K/WElPx3/VSS6nNxp1 4abQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=g28mt7Xj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 k184-v6si9433782pge.209.2018.07.23.18.08.43; Mon, 23 Jul 2018 18:08:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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=g28mt7Xj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 S2388283AbeGXCLq (ORCPT + 99 others); Mon, 23 Jul 2018 22:11:46 -0400 Received: from mail-pl0-f66.google.com ([209.85.160.66]:45978 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388188AbeGXCLq (ORCPT ); Mon, 23 Jul 2018 22:11:46 -0400 Received: by mail-pl0-f66.google.com with SMTP id j8-v6so897312pll.12 for ; Mon, 23 Jul 2018 18:07:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=zyZf6/KqGIMDgpREyYCY0R+wT7ZUloWtCIe+utSRNvA=; b=g28mt7XjpQhdpjpsv/mXKwK35op4V0qTPGg7atWlYvMXGA1HP+vvdLXVdABi6noU+Q WJTLlCHae3CXav1JaoHQYUqTbewytUaXCo0tO/O7T9FjWji31ButcvmMDazdLA7g4WlZ Jd7bfMKqGAUMPJ/1/3PAgmhPKZIJ3dHCBJs+k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=zyZf6/KqGIMDgpREyYCY0R+wT7ZUloWtCIe+utSRNvA=; b=unP8W96j+L9ckGAbKHR9BAvt62nMKubSAx7NdCm9pfKPbzUfJGmJw7KJ5Dp5odv+WN 54kWcVFPLf8FdtXNXlQDNDOkTn7R1cs7jblmcWZ4dAYXnscN55oagZFoJgwZ7fe9eL49 JxC0+r8fMc2+Rd+MNKExXIN/BHGQcU2+RfngTUf7RZEsM/FHcVu8TnF6tThGa1LHV/iM xGBbemXVC4zzjecBwVNqgbuSXQzIMqDcGRE7mMIjYBAjW0DHeegZyFjkQaDVwccNdEiQ QVCAyMtomN/AnppoIxMcBXzsn4yyuxhSMb7pg4L69awMNEwdVpn2icsjTw5TfAoEw3x+ haDQ== X-Gm-Message-State: AOUpUlF0LGPvZT18+hP1U0g86JR+DHomR3C5OJZZ+a97vRLQPEOWvJ2O M/LWHvL56QuZI5r0yoI9Ib2oyQ== X-Received: by 2002:a17:902:8d98:: with SMTP id v24-v6mr14912428plo.250.1532394474378; Mon, 23 Jul 2018 18:07:54 -0700 (PDT) Received: from minitux (104-188-17-28.lightspeed.sndgca.sbcglobal.net. [104.188.17.28]) by smtp.gmail.com with ESMTPSA id 87-v6sm21496387pfn.103.2018.07.23.18.07.53 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 23 Jul 2018 18:07:53 -0700 (PDT) Date: Mon, 23 Jul 2018 18:06:49 -0700 From: Bjorn Andersson To: Baolin Wang Cc: jacek.anaszewski@gmail.com, pavel@ucw.cz, david@lechnology.com, broonie@kernel.org, linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v5 1/2] leds: core: Introduce generic pattern interface Message-ID: <20180724010649.GH30024@minitux> References: <05c235df7bbcc4b898e7b263bf38338d9c822005.1531711385.git.baolin.wang@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <05c235df7bbcc4b898e7b263bf38338d9c822005.1531711385.git.baolin.wang@linaro.org> User-Agent: Mutt/1.10.0 (2018-05-17) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon 16 Jul 04:10 PDT 2018, Baolin Wang wrote: > 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 I'm happy to see that you found this patch useful and think the patch looks good. > --- > Changes from v4: > - Fix some typos. > - Check if pattern_clear() is initialized. > > 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. In the Qualcomm LPG it's possible to specify if the pattern should be run through once of repeated continuously, so if you provide the means to control this by a separately I don't see a problem with this. Regards, Bjorn > --- > Documentation/ABI/testing/sysfs-class-led | 20 +++++ > drivers/leds/led-class.c | 118 +++++++++++++++++++++++++++++ > include/linux/leds.h | 19 +++++ > 3 files changed, 157 insertions(+) > > diff --git a/Documentation/ABI/testing/sysfs-class-led b/Documentation/ABI/testing/sysfs-class-led > index 5f67f7a..a040fd0 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 activate 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..5c6d2e9 100644 > --- a/drivers/leds/led-class.c > +++ b/drivers/leds/led-class.c > @@ -74,6 +74,118 @@ 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) { > + 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 +200,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 +370,10 @@ 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 || !led_cdev->pattern_clear)) > + 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..5f1c751 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 - brightness 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 */ > -- > 1.7.9.5 >