From patchwork Fri Jul 13 06:21: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: 141851 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp416827ljj; Thu, 12 Jul 2018 23:21:47 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcuH3/2h54CWkeqm0BzJW8usSubrVNnmODu2+5Ozq6O+QMpoO5QKLC2mZpcv6AgrCJSgxMa X-Received: by 2002:a63:7007:: with SMTP id l7-v6mr4872089pgc.206.1531462907429; Thu, 12 Jul 2018 23:21:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531462907; cv=none; d=google.com; s=arc-20160816; b=TEgbx2H//genVNIWYKtEm0V8cGtN1CecFfLlqIx7j/wPSgef6deWWsb8p20Xonpk6c /tgThmU4vlZvFvpMZZx6FZBJMXUfL7oRkp6M7pEWH86wC1fL8MLPUWyaNHsPGC9FvXLI Xuz8FBrrVzIYFgL/3+wbcSUfCinBvP6+PGQY/U2t1WwYn7KXScDQHnoIOfz+Dwkux3KI KLs/sT1bFTP2Hta5OP09neXLuGHy4U+GBquGhSbGF1Xg1Md95Yx2c9u3fvqnLaI7n9ZG ft0TeWK6kKOHVb+/LRqqNrpwPoiEPA8V1N1cysqHd8Tf6dSsCI/FCcxRJn/yRH77dh3c W+CA== 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=aeXP2hmFR7ySvVHlSIgfBCTB17skKOMWEKnzpmNaRWI=; b=eW/1Ak5nRlSb2qmRbQevgsRqJhVLzRxoFoWw9TAZ3YgYwvL5JLWgFB4WOjbYiZmtck IBcBEp+SBcB4KCSnSVu4YXH9BCY04+yh68I1VHxhoAb5Qe2l20yb2One/nAgPBp64VNF 6eUl7exbk9pVDhLK6i7dhEge1JaK8n9XJQ9InKyotn8/yeyAhUTBeBZvc8iZVbz6oWjh ztPUmYBbI1pZjo5JAg/noSq4iBzRS3Jj/6lBMQ1uqxhwCMxgs6xKjzM1/taPAIHrY2U6 EMN1Q0mI9t7dBEUPRQoMrenmMlKx1hM+fgcw3/8v3AB7OU5A0Bjyf+AUP2DJydCbC236 t6nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O4Q+oc0T; 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 x62-v6si21098349pfa.80.2018.07.12.23.21.47; Thu, 12 Jul 2018 23:21:47 -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=O4Q+oc0T; 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 S1729625AbeGMGex (ORCPT + 1 other); Fri, 13 Jul 2018 02:34:53 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:42450 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729275AbeGMGew (ORCPT ); Fri, 13 Jul 2018 02:34:52 -0400 Received: by mail-pf0-f195.google.com with SMTP id l9-v6so10402109pff.9 for ; Thu, 12 Jul 2018 23:21:45 -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=aeXP2hmFR7ySvVHlSIgfBCTB17skKOMWEKnzpmNaRWI=; b=O4Q+oc0TPNph4vLVn01FYhG2EwOjdOrCIm4gAilTyY2yzDHOPoaFzI2ooGDsUecdie zGU+s858b580Y0FfSGiJF+p9UVz/ANIdQS7Ok11j3fFVv22zLkFKrk/lLvqZ4LplPJ3Q iYhb87uajPkGqLX/6zBlgP6GnDbdv9BANfBHc= 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=aeXP2hmFR7ySvVHlSIgfBCTB17skKOMWEKnzpmNaRWI=; b=laZSQVmi+ZIZViyBLIqGIRYPyniTUwkj6oQbc1NCa/CLcdpTzkLoBmD/tGRJx0+pLh v0vrd85RVB2pAXeKdDNR8LPVMc8HkNoVZcA55EPDRqOt/X2ivZ8MO5Bfcz/q/nANmIPV KeI3yqpYLCJ3KE2SOWQwfD5qwrjO1W0LnLB7hLfmWX5RmstaV3FZIEvhl7aF7eXWckkE C1W/IM+INTVBol1YtKuyV64qgdgZpqzfmiBgS4Vk4pgKpV2XLneHWgchNBc4hVZ0F6U7 XHkFEnl7qHw1lmWc/uNi9l+PK4cKHtQCFLAO1VXuQkvmERPj77+tsJ5QCXoeTzGBDl3J LM/g== X-Gm-Message-State: AOUpUlGDvgTUjRF7mS+eiXbmT3rGWnHc4REdznH+UOtIYyWauwuqU2oT +jkzi9OlvOuh0OQ96bceurVSpw== X-Received: by 2002:a65:5c03:: with SMTP id u3-v6mr4647487pgr.402.1531462904588; Thu, 12 Jul 2018 23:21:44 -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.42 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 12 Jul 2018 23:21:44 -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 2/2] leds: sc27xx: Add pattern_set/get/clear interfaces for LED controller Date: Fri, 13 Jul 2018 14:21:14 +0800 Message-Id: <590b0e33c7c1638f99f32b7ad82243fe2e29ba3b.1531460834.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-leds-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-leds@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 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)