From patchwork Mon Jun 25 05:03:20 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: 139797 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp3542895lji; Sun, 24 Jun 2018 22:04:34 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKchOIktOF3kfiIXIbTxfzV2H7MduTWUXEgAhfQsKHWEi+epI6zGe06fuR7TICqZqb7FQ3J X-Received: by 2002:a62:3481:: with SMTP id b123-v6mr766937pfa.4.1529903074190; Sun, 24 Jun 2018 22:04:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529903074; cv=none; d=google.com; s=arc-20160816; b=NwUz7FZ3WQj1BOkjXpMGQCOeRmmqPCdN1G0xl3kTyJpMGm87UmhtT9bD4e6QLQMN7o 7VuTFWS5nf4h/Ayqwn3mvnsi72wZXlk2ZD41YAv2xAmk3Z39XZ8+8MBB5qEuqH+CNCvl Z9LhMprj17+FvzW+3GCEjWD1mtXI1j27Wg5IsFCzYWy0geBK17qBe1sYKTT+Bwvhp9fR JowoITAXsDSaEmHg/irk6d5sdD1C+oB1ShakMFVOT4ZdbbtTJeqQkhhI9p7fM11K4+Yk mZf8bT4JHbtfr4YoJi8z+OHMYvdBkmEr01qhaeT09qRmI0miX9W5aHSLkE6Zst3hRd9z i6Ag== 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=hziHGUZkooF12/Zrmans442y7pdgZuICpJdqKv9SPu8=; b=SzhU6+Gq9gq4Ppk9uFQrZpnlnMMQt3j8j1e2+r8L6RuDTwgzdtPaWhVWyGc6UHxHBw FQtxW55kkYIJ71QewI6Xl3kYpqaBlXKfCr1ikOEwughBm8en17Py8dXXeZlOOCfSONpD ynvBTFhIIe3EDkwc6C0geY1MqEMptEWosCorEtqeqTqTI+4mgfGi6PpA1z6x6mzFkh/m B3WA2Cra6ItbGi0iN3R+d0cfg5IiE8i87VPGoy3gs6Vd1VwBm29ziaZp3/BtDmKFXOOn ohNbAPseIiJLThiTxhht+N8waCyIeWGlu+Tgj0IJjO1osxOzYf5PPQSSI16gs4QfPSoI /Mcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="f/AjRvls"; 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 w71-v6si3580574pgd.667.2018.06.24.22.04.33; Sun, 24 Jun 2018 22:04:34 -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="f/AjRvls"; 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 S1751675AbeFYFEZ (ORCPT + 31 others); Mon, 25 Jun 2018 01:04:25 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:38680 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751460AbeFYFEX (ORCPT ); Mon, 25 Jun 2018 01:04:23 -0400 Received: by mail-pf0-f195.google.com with SMTP id a1-v6so2036610pfi.5 for ; Sun, 24 Jun 2018 22:04:22 -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=hziHGUZkooF12/Zrmans442y7pdgZuICpJdqKv9SPu8=; b=f/AjRvlsCATOzVT5E4CKIk892NxHvBQsJh8fVOKdS6ZT2wTMaNDxWdu8yLPTNw9+M7 NsR1lm2BcZNqSDvdWJSV0z0ljjKjZ1IhRdiUCbMcBWQUeGm2TLHQC8SbfX58nKhMm8Qn eKKHCsuZLArh3BfJ/cTM3wKR53mGDoqby6OqI= 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=hziHGUZkooF12/Zrmans442y7pdgZuICpJdqKv9SPu8=; b=TF9L9RgaeO65YduWw/OAJR0SJk04Xt0x5x43lGGk3HaHiBLyFvLkFA9G/mNHAcNluK yaxMmcc60uM3gfBk6mriOseVHHUVBcu+oNpoA0SyivTGUtgGC+Lk2la1sNZJasNxa7NX 7GBFLGVQaEOHCBC9S5nqE15u3Dgay/B7wDOFS97HvMTGORzM3j0buwPYUDYGIi6kVzQN bzSd+25LIRWV6tye5TGt5ZtA8nNdraZSrKHnueGZhV2AZDBd65JXduHZP4Ese1ylBIlF XLHDs95ONqBN9NF8oaoFcdsxoNfbY2OeOrfrfSmqAnFONbMWXb3FlzkrRG9490OU6uMk Kedw== X-Gm-Message-State: APt69E0fdOmHl3ud6OvjTl8QrFYfK1gO4xCzJf950AcX2W0cnuXEmSTF sOutkWd8fyHQlgC5bk/7xjtFZw== X-Received: by 2002:aa7:83d1:: with SMTP id j17-v6mr11573821pfn.236.1529903062472; Sun, 24 Jun 2018 22:04:22 -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.20 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 24 Jun 2018 22:04:21 -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 2/2] leds: sc27xx: Add pattern_set/get/clear interfaces for LED controller Date: Mon, 25 Jun 2018 13:03:20 +0800 Message-Id: <9ed96acd5b46ab089cb4e2b906eedbd34df41249.1529902636.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <046c0cf717cbdfd55dcdc2bb7463642734e2ca4c.1529902636.git.baolin.wang@linaro.org> References: <046c0cf717cbdfd55dcdc2bb7463642734e2ca4c.1529902636.git.baolin.wang@linaro.org> In-Reply-To: <046c0cf717cbdfd55dcdc2bb7463642734e2ca4c.1529902636.git.baolin.wang@linaro.org> References: <046c0cf717cbdfd55dcdc2bb7463642734e2ca4c.1529902636.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 --- 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)