From patchwork Mon Jul 16 11:10:14 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: 141994 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp2286179ljj; Mon, 16 Jul 2018 04:10:52 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdb+qPJDYDaegEG3wCIQMPdUpF31/s8jSkOsx2q8PsrIFBx4SD7ylBjcA4D6m60of+K4xBZ X-Received: by 2002:a17:902:8bc6:: with SMTP id r6-v6mr15853222plo.257.1531739452076; Mon, 16 Jul 2018 04:10:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531739452; cv=none; d=google.com; s=arc-20160816; b=vN9lArfTV7qXgZJtnAVIk/PQqzQli42ZXMEbsgsr977J4UTITXH7ryV9U8AJNK9h/X njoMkVjjxCd15/4YbqGQHue9Xo8qmP26/vwHF1EuE/BV3Rm1ZlyJLzjKbn3tfrX5dCi8 m1QK6Wuq2UBIkn9esd3c7Uzhx3vtjD281KjvFcu7HauY0M27oU5bsDvTDoY/CteCuerq QzP/6QNA08UZcL7bzZpQ4LMjEkhOUE9Bdh3qj78I8b17U241nebf4YDLeiijHxFzre68 xCI/EQmqC0VJ0X7oA5doW3go8Wbgm+MzTddQ180GZJaGWM8aQPjxH2rrRj+2xzSAlQ9Z 3bxA== 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=ICzV9qxVeY7tlybwwnBRqlnZ3pEe8EtG5FKFXsBr8PE=; b=TnIMDfSW+mKRYLoscIYIh3MBABHBPNka/XQAx40ClZquFwS+evwKAuZSXb1klbvEsP G/Y9VqR9eYlm8K2I6pUjLH/LczjfRvdJiJSWo+UnrJKQ2ib14JREpcAtkwBZR96byStY ZuUAAvKsd6ZmWiWtAvEZG3DntldCC94hREOXDnueFdD1q/qz+Oom0AsJ3iFjE2AV4y3b NJbf3Wl50zYzTIRzOqE4L52J7wu2vBL9Udgfnvx8Y/TKPWgELjVsIgIxvy5bYSzL53fc qY1Jb1fR+W1QSbGKFdaIDgGpVEv8mbmQ8dMmwXyphbMILtP3d5lQXbiL56Aq9JFyDlur rtsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WksxxkjM; 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 d34-v6si29892774pld.252.2018.07.16.04.10.51; Mon, 16 Jul 2018 04:10:52 -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=WksxxkjM; 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 S1729778AbeGPLho (ORCPT + 31 others); Mon, 16 Jul 2018 07:37:44 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:44333 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726602AbeGPLho (ORCPT ); Mon, 16 Jul 2018 07:37:44 -0400 Received: by mail-pf0-f196.google.com with SMTP id k21-v6so1218623pff.11 for ; Mon, 16 Jul 2018 04:10:48 -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=ICzV9qxVeY7tlybwwnBRqlnZ3pEe8EtG5FKFXsBr8PE=; b=WksxxkjMkZyjwH57XRF1sAxkyclS2Ymi0bMsdsM6Rjffu9MZbMbcvJw0HFzW7qysG9 k1bSR2tJyN9at41TJwkViA6YWSp11oPLXczMtBmlfUMwnlwiinBRZJY3xcjh8rqygGYn ySst/ch0wCIb7Nf0rlZ2oehJ5y2Ln+hpVvqKE= 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=ICzV9qxVeY7tlybwwnBRqlnZ3pEe8EtG5FKFXsBr8PE=; b=X+/EjJsSvMoMUFYtwPFvgoxmriwEak3ItxtHELGZo9g2VwigsDItJs2Y3aZMY/q1GN COUj7hNyZwxSb3x2eb/PmQl+vI5ZMXI52MOvFKywQB9Oz+883ibT6b3CC4gH64HtyWxv saAP6aRfuvCjF4dA64h+oo8sXu54y41jaPOgbVg/3U0QsGrQ74hdfupVbym5Q6tbEFDn ARArPNzXGL5npB6P3qQJdms0I8SSpj55u1zM0Fb5RcAHFZR/saEySFz4xMyamXziPWhe qRaYv8pjaVSaC+xPK7WVQgMU9DDRY0Sjw3QmUrmUv5bNGkNgV4KyiXTCig9R3F5gh6tC E4sg== X-Gm-Message-State: AOUpUlFQ0lrMBdxnOtkOVoajjvPGq+BG5J2St5NPKMM82b8By0kwEE/g 3xRt3pfqKCCx7Lpb6BPhdWAlcQ== X-Received: by 2002:a63:1b5c:: with SMTP id b28-v6mr15562544pgm.204.1531739448389; Mon, 16 Jul 2018 04:10:48 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id w72-v6sm51286894pfa.26.2018.07.16.04.10.45 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 16 Jul 2018 04:10:47 -0700 (PDT) From: Baolin Wang To: jacek.anaszewski@gmail.com, pavel@ucw.cz Cc: baolin.wang@linaro.org, bjorn.andersson@linaro.org, david@lechnology.com, broonie@kernel.org, linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 1/2] leds: core: Introduce generic pattern interface Date: Mon, 16 Jul 2018 19:10:14 +0800 Message-Id: <05c235df7bbcc4b898e7b263bf38338d9c822005.1531711385.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 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. --- 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..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 */ From patchwork Mon Jul 16 11:10:15 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: 141995 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp2286208ljj; Mon, 16 Jul 2018 04:10:54 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeRK9+gBqIpja1Pa/lxyvKOOsjw/F/PW1LOfEVqidcOhW05f+MPN81mu4PlH2TCTbHG8opd X-Received: by 2002:a63:5a13:: with SMTP id o19-v6mr14985440pgb.195.1531739454787; Mon, 16 Jul 2018 04:10:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531739454; cv=none; d=google.com; s=arc-20160816; b=z3UqkdL409MrSeBmPymjvYkH5dVzfz19N7CsUKKuf5g4Y2ZXe2d7TP5Zwq4BP1wHlZ a1ryp12g2CPC3mw82JwdJKBNGeA8iJg+7LfW9gnXdtG+gGNndafK1P3AC7htRSCHSNhx KcGPkjRHLDPbIfthmKLjunfCL2ahnx4xohhxh/9S+2HBWTkfEvJpucO1DM9Zo+yDrI8G u36e6tDqIIT0Cvi7nzz9ubYwa50SQ7/FpCJpgsUrW6VHEAWE1J3i/KEZUE5qvyatidBi SCjHEsIaItr9qymY/kdFRz5+ablkTLibMPrZgBmLpWVd6du4FOjM8S5bsY8xyON7sx6Z /2eQ== 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:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=Ax5hAntQ1SuFIPFT3Dsr0GkexTWyYa0Mvsadx1sh0I0=; b=J424HyZOoU5g0KfIEYFvNrLB57EE3KXquVsatbDTVppLp7iyNXSfPrVOqfW0P/k3RI MNcIdp2ABoA3rRwF9sOpGmdrcpqPY3ILO4RetQSXwUXoWrjjTYE8J2UMJGah8/vpFIlU EmWP9dEUvtMuCbtABCmP2nX4QKxHDp+fTmP6B1vI7cGbSBQxfJhfkzejCBftJd+4eA8I sJOcYluP1Muv+TWFLXy+hejbHttJPkaIfYTO9Z/jFrBhEL5iXvqaPWqEnlZT51uDQ5Wl ZTwLj6XQJlHTSITzMM/mMjJV7W3Jxi7ry0TRi3JJTxC0KyA18nEfvH9mJz5ybAP5WFf0 0Vvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="bA8Q5U/j"; 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 g21-v6si14210052pgh.686.2018.07.16.04.10.54; Mon, 16 Jul 2018 04:10:54 -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="bA8Q5U/j"; 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 S1731213AbeGPLhr (ORCPT + 31 others); Mon, 16 Jul 2018 07:37:47 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:39787 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726489AbeGPLhq (ORCPT ); Mon, 16 Jul 2018 07:37:46 -0400 Received: by mail-pg1-f194.google.com with SMTP id g2-v6so2522998pgs.6 for ; Mon, 16 Jul 2018 04:10:51 -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:in-reply-to:references :in-reply-to:references; bh=Ax5hAntQ1SuFIPFT3Dsr0GkexTWyYa0Mvsadx1sh0I0=; b=bA8Q5U/jHVjeqgQw3kPbVlt/ley4hbASmBw9xHI6tqWxx5ap8vDZd061KtykjClmdX tlh5y527mj/40lc6A/HUbHNZYVSTAVlrLmVWcBgd0coNH0qUoZZqVAR55FnRDfXe9xLr Lk0/UJz6TxHogI3gpjruBfxVM3+1RnHSHqdEc= 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:in-reply-to:references; bh=Ax5hAntQ1SuFIPFT3Dsr0GkexTWyYa0Mvsadx1sh0I0=; b=m9G3gMUvrXRpKtWIcAvCAiu/O3LfUsaFXLT4fChsE1VSUu+me7cdZrh423j9Xaqy/J QyM/xYyleMFW44mtxt4hzrnARVgnOVBEk2B05UGlTlLMHIECdf+rU9ve1GWkp/MeY4C9 49NpzWUSDRnuinFMTRKhvl693WpBGnRwXUMVqluSDh5jKyof1qxu5jy9uwIba/BHhPJ3 umAhtkpnNLBShsh/quCN93Nwf2A/cfpAw9nrg+lQfkSSP3St8GM1qiWYALFBJigP/b34 4K6xccHE4ZEfDbgEpOdThme5sLB4ODlhpT0nmBogEcCPXASw9m9qTloSdDU9p1q8Sze+ tMHg== X-Gm-Message-State: AOUpUlG5VB4q4okcTzGrAPlbohXOcu3q3sVswb0c3ky0ReZ0JgIU+9Vd 5p/3t4avi10m9mmdKkfiEBnMOA== X-Received: by 2002:a63:f45:: with SMTP id 5-v6mr15487717pgp.447.1531739450820; Mon, 16 Jul 2018 04:10:50 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id w72-v6sm51286894pfa.26.2018.07.16.04.10.48 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 16 Jul 2018 04:10:50 -0700 (PDT) From: Baolin Wang To: jacek.anaszewski@gmail.com, pavel@ucw.cz Cc: baolin.wang@linaro.org, bjorn.andersson@linaro.org, david@lechnology.com, broonie@kernel.org, linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 2/2] leds: sc27xx: Add pattern_set/get/clear interfaces for LED controller Date: Mon, 16 Jul 2018 19:10:15 +0800 Message-Id: <51cd675efcf546c712377878e353312e9d4980df.1531711385.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <05c235df7bbcc4b898e7b263bf38338d9c822005.1531711385.git.baolin.wang@linaro.org> References: <05c235df7bbcc4b898e7b263bf38338d9c822005.1531711385.git.baolin.wang@linaro.org> In-Reply-To: <05c235df7bbcc4b898e7b263bf38338d9c822005.1531711385.git.baolin.wang@linaro.org> References: <05c235df7bbcc4b898e7b263bf38338d9c822005.1531711385.git.baolin.wang@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch implements the 'pattern_set', 'pattern_get' and 'pattern_clear' interfaces to support SC27XX LED breathing mode. Signed-off-by: Baolin Wang --- Changes from v4: - None. Changes from v3: - None. Changes from v2: - No updates. Changes from v1: - No updates. --- drivers/leds/leds-sc27xx-bltc.c | 160 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) -- 1.7.9.5 diff --git a/drivers/leds/leds-sc27xx-bltc.c b/drivers/leds/leds-sc27xx-bltc.c index 9d9b7aa..898f92d 100644 --- a/drivers/leds/leds-sc27xx-bltc.c +++ b/drivers/leds/leds-sc27xx-bltc.c @@ -6,6 +6,7 @@ #include #include #include +#include #include /* PMIC global control register definition */ @@ -32,8 +33,13 @@ #define SC27XX_DUTY_MASK GENMASK(15, 0) #define SC27XX_MOD_MASK GENMASK(7, 0) +#define SC27XX_CURVE_SHIFT 8 +#define SC27XX_CURVE_L_MASK GENMASK(7, 0) +#define SC27XX_CURVE_H_MASK GENMASK(15, 8) + #define SC27XX_LEDS_OFFSET 0x10 #define SC27XX_LEDS_MAX 3 +#define SC27XX_LEDS_PATTERN_CNT 4 struct sc27xx_led { char name[LED_MAX_NAME_SIZE]; @@ -122,6 +128,157 @@ static int sc27xx_led_set(struct led_classdev *ldev, enum led_brightness value) return err; } +static int sc27xx_led_pattern_clear(struct led_classdev *ldev) +{ + struct sc27xx_led *leds = to_sc27xx_led(ldev); + struct regmap *regmap = leds->priv->regmap; + u32 base = sc27xx_led_get_offset(leds); + u32 ctrl_base = leds->priv->base + SC27XX_LEDS_CTRL; + u8 ctrl_shift = SC27XX_CTRL_SHIFT * leds->line; + int err; + + mutex_lock(&leds->priv->lock); + + /* Reset the rise, high, fall and low time to zero. */ + regmap_write(regmap, base + SC27XX_LEDS_CURVE0, 0); + regmap_write(regmap, base + SC27XX_LEDS_CURVE1, 0); + + err = regmap_update_bits(regmap, ctrl_base, + (SC27XX_LED_RUN | SC27XX_LED_TYPE) << ctrl_shift, 0); + + mutex_unlock(&leds->priv->lock); + + return err; +} + +static int sc27xx_led_pattern_set(struct led_classdev *ldev, + struct led_pattern *pattern, + int len) +{ + struct sc27xx_led *leds = to_sc27xx_led(ldev); + u32 base = sc27xx_led_get_offset(leds); + u32 ctrl_base = leds->priv->base + SC27XX_LEDS_CTRL; + u8 ctrl_shift = SC27XX_CTRL_SHIFT * leds->line; + struct regmap *regmap = leds->priv->regmap; + int err; + + /* + * Must contain 4 patterns to configure the rise time, high time, fall + * time and low time to enable the breathing mode. + */ + if (len != SC27XX_LEDS_PATTERN_CNT) + return -EINVAL; + + mutex_lock(&leds->priv->lock); + + err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE0, + SC27XX_CURVE_L_MASK, pattern[0].delta_t); + if (err) + goto out; + + err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE1, + SC27XX_CURVE_L_MASK, pattern[1].delta_t); + if (err) + goto out; + + err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE0, + SC27XX_CURVE_H_MASK, + pattern[2].delta_t << SC27XX_CURVE_SHIFT); + if (err) + goto out; + + + err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE1, + SC27XX_CURVE_H_MASK, + pattern[3].delta_t << SC27XX_CURVE_SHIFT); + if (err) + goto out; + + + err = regmap_update_bits(regmap, base + SC27XX_LEDS_DUTY, + SC27XX_DUTY_MASK, + (pattern[0].brightness << SC27XX_DUTY_SHIFT) | + SC27XX_MOD_MASK); + if (err) + goto out; + + /* Enable the LED breathing mode */ + err = regmap_update_bits(regmap, ctrl_base, + SC27XX_LED_RUN << ctrl_shift, + SC27XX_LED_RUN << ctrl_shift); + +out: + mutex_unlock(&leds->priv->lock); + + return err; +} + +static struct led_pattern *sc27xx_led_pattern_get(struct led_classdev *ldev, + int *len) +{ + struct sc27xx_led *leds = to_sc27xx_led(ldev); + u32 base = sc27xx_led_get_offset(leds); + struct regmap *regmap = leds->priv->regmap; + struct led_pattern *pattern; + int i, err; + u32 val; + + /* + * Must allocate 4 patterns to show the rise time, high time, fall time + * and low time. + */ + pattern = kcalloc(SC27XX_LEDS_PATTERN_CNT, sizeof(*pattern), + GFP_KERNEL); + if (!pattern) + return ERR_PTR(-ENOMEM); + + mutex_lock(&leds->priv->lock); + + err = regmap_read(regmap, base + SC27XX_LEDS_CURVE0, &val); + if (err) + goto out; + + pattern[0].delta_t = val & SC27XX_CURVE_L_MASK; + + err = regmap_read(regmap, base + SC27XX_LEDS_CURVE1, &val); + if (err) + goto out; + + pattern[1].delta_t = val & SC27XX_CURVE_L_MASK; + + err = regmap_read(regmap, base + SC27XX_LEDS_CURVE0, &val); + if (err) + goto out; + + pattern[2].delta_t = (val & SC27XX_CURVE_H_MASK) >> SC27XX_CURVE_SHIFT; + + err = regmap_read(regmap, base + SC27XX_LEDS_CURVE1, &val); + if (err) + goto out; + + pattern[3].delta_t = (val & SC27XX_CURVE_H_MASK) >> SC27XX_CURVE_SHIFT; + + err = regmap_read(regmap, base + SC27XX_LEDS_DUTY, &val); + if (err) + goto out; + + mutex_unlock(&leds->priv->lock); + + val = (val & SC27XX_DUTY_MASK) >> SC27XX_DUTY_SHIFT; + for (i = 0; i < SC27XX_LEDS_PATTERN_CNT; i++) + pattern[i].brightness = val; + + *len = SC27XX_LEDS_PATTERN_CNT; + + return pattern; + +out: + mutex_unlock(&leds->priv->lock); + kfree(pattern); + + return ERR_PTR(err); +} + static int sc27xx_led_register(struct device *dev, struct sc27xx_led_priv *priv) { int i, err; @@ -140,6 +297,9 @@ static int sc27xx_led_register(struct device *dev, struct sc27xx_led_priv *priv) led->priv = priv; led->ldev.name = led->name; led->ldev.brightness_set_blocking = sc27xx_led_set; + led->ldev.pattern_set = sc27xx_led_pattern_set; + led->ldev.pattern_get = sc27xx_led_pattern_get; + led->ldev.pattern_clear = sc27xx_led_pattern_clear; err = devm_led_classdev_register(dev, &led->ldev); if (err)