From patchwork Fri Apr 22 10:18:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 8908981 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C3E489F1D3 for ; Fri, 22 Apr 2016 10:22:33 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D865A2024F for ; Fri, 22 Apr 2016 10:22:32 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DD5D9201F2 for ; Fri, 22 Apr 2016 10:22:31 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1atYCm-0003oz-8a; Fri, 22 Apr 2016 10:20:56 +0000 Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1atYAm-00009z-At for linux-arm-kernel@lists.infradead.org; Fri, 22 Apr 2016 10:18:53 +0000 Received: by mail-wm0-x22b.google.com with SMTP id n3so19455566wmn.0 for ; Fri, 22 Apr 2016 03:18:31 -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; bh=QVVoLAS0CPSQ3Qwsepl2HWQTgDzCVXkjbDb4fWRqJLA=; b=fe5R045GP7tu1o0uWV3gSG/+mr9HS+vTZrx0f5VzBndPxtHgr0RRFlpP1jZ34NeuJ2 2jhKfypcr+Qe+wVzGNkqPy2AnDvaMED0L1LsKGC11qrq1/5AKXtKGWdT7Qeayw19qOqT BDDgi9tUDWKM95xsJVnoR2gqPVmMjN71jigoM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QVVoLAS0CPSQ3Qwsepl2HWQTgDzCVXkjbDb4fWRqJLA=; b=JxvK5Nh5OvV9fcdYDOXYNxLI4tGTHWCmlWnr7szmiMqWC1Wf2WnJuOR+lxrp/1qKcb rVpuZcvstRCZ/6p0cMYLR/hIczmsOqq5JVkDy8jH0p2KtxI7IGwSTCt61vTGzUCMGiMs /RbYlVSbvqkj1Z7QXg9oyK+6BBZ7BY3u0yqh/SESA17DXQOaEelgsbbr3tXRWtZM9O0W ckEpdPORE1vFHFP5Jkpf+2AL12YZ6hrI+0uibf/mzNWnmlKaFa7vmfjBsCGdg2ldnPkt 9DwVfVhb5IUGptnU4/kLZHVgJJmdl3m2roijefe92JJCzwKPs82ksTZdNRmd0DaPEx7x 6wKw== X-Gm-Message-State: AOPr4FVSHiaF2uMYS94yjTUAzTtYY30b8NEfYlumBnYMKNnbCRn18tzXO9wrMC3y4oPR2qB0 X-Received: by 10.194.117.7 with SMTP id ka7mr19570355wjb.116.1461320310514; Fri, 22 Apr 2016 03:18:30 -0700 (PDT) Received: from dell.localdomain (host81-129-172-5.range81-129.btcentralplus.com. [81.129.172.5]) by smtp.gmail.com with ESMTPSA id a1sm7316345wje.43.2016.04.22.03.18.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Apr 2016 03:18:29 -0700 (PDT) From: Lee Jones To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [[PATCH v2] 10/11] pwm: sti: Add PWM Capture call-back Date: Fri, 22 Apr 2016 11:18:14 +0100 Message-Id: <1461320295-20414-11-git-send-email-lee.jones@linaro.org> X-Mailer: git-send-email 2.8.0 In-Reply-To: <1461320295-20414-1-git-send-email-lee.jones@linaro.org> References: <1461320295-20414-1-git-send-email-lee.jones@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160422_031852_558892_3A340D3E X-CRM114-Status: GOOD ( 14.96 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-pwm@vger.kernel.org, kernel@stlinux.com, ajitpal.singh@st.com, thierry.reding@gmail.com, Lee Jones , maxime.coquelin@st.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Once a PWM Capture has been initiated, the capture call enables a rising edge detection IRQ, then waits. Once each of the 3 phase changes have been recorded the thread then wakes. The remaining part of the call carries out the relevant calculations and passes back a formatted string to the caller. Signed-off-by: Lee Jones --- drivers/pwm/pwm-sti.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/drivers/pwm/pwm-sti.c b/drivers/pwm/pwm-sti.c index 2230afb..cecb6d4 100644 --- a/drivers/pwm/pwm-sti.c +++ b/drivers/pwm/pwm-sti.c @@ -24,6 +24,8 @@ #include #include +#define SECS_TO_NANOSECS(x) ((x) * 1000 * 1000 * 1000) + #define PWM_OUT_VAL(x) (0x00 + (4 * (x))) /* Device's Duty Cycle register */ #define PWM_CPT_VAL(x) (0x10 + (4 * (x))) /* Capture value */ #define PWM_CPT_EDGE(x) (0x30 + (4 * (x))) /* Edge to capture on */ @@ -305,7 +307,88 @@ static void sti_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) clear_bit(pwm->hwpwm, &pc->configured); } +static int sti_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm, + struct pwm_capture *result, unsigned int timeout_ms) +{ + struct sti_pwm_chip *pc = to_sti_pwmchip(chip); + struct sti_pwm_compat_data *cdata = pc->cdata; + struct sti_cpt_ddata *ddata = pwm_get_chip_data(pwm); + struct device *dev = pc->dev; + unsigned int effective_ticks; + unsigned long long high, low; + int ret; + + if (pwm->hwpwm > cdata->cpt_num_devs - 1) { + dev_err(dev, "Device %d is not valid\n", pwm->hwpwm); + return -EINVAL; + } + + mutex_lock(&ddata->lock); + + /* Prepare capture measurement */ + ddata->index = 0; + regmap_write(pc->regmap, PWM_CPT_EDGE(pwm->hwpwm), CPT_EDGE_RISING); + regmap_field_write(pc->pwm_cpt_int_en, BIT(pwm->hwpwm)); + + /* Enable capture */ + ret = regmap_field_write(pc->pwm_cpt_en, 1); + if (ret) { + dev_err(dev, "failed to enable PWM capture %d\n", pwm->hwpwm); + goto out; + } + + ret = wait_event_interruptible_timeout(ddata->wait, + ddata->index > 1, + msecs_to_jiffies(timeout_ms)); + + regmap_write(pc->regmap, PWM_CPT_EDGE(pwm->hwpwm), CPT_EDGE_DISABLED); + + if (ret == -ERESTARTSYS) + goto out; + + switch (ddata->index) { + case 0: + case 1: + /* + * Getting here could mean : + * - input signal is constant of less than 1Hz + * - there is no input signal at all + * + * In such case the frequency is rounded down to 0 + */ + + result->period = 0; + result->duty_cycle = 0; + + break; + case 2: + /* We have everying we need */ + high = ddata->snapshot[1] - ddata->snapshot[0]; + low = ddata->snapshot[2] - ddata->snapshot[1]; + + effective_ticks = clk_get_rate(pc->cpt_clk); + + result->period = SECS_TO_NANOSECS(high + low); + do_div(result->period, effective_ticks); + + result->duty_cycle = SECS_TO_NANOSECS(high); + do_div(result->duty_cycle, effective_ticks); + + break; + default: + dev_err(dev, "Internal error\n"); + } + +out: + /* Disable capture */ + regmap_field_write(pc->pwm_cpt_en, 0); + + mutex_unlock(&ddata->lock); + return ret; +} + static const struct pwm_ops sti_pwm_ops = { + .capture = sti_pwm_capture, .config = sti_pwm_config, .enable = sti_pwm_enable, .disable = sti_pwm_disable, @@ -418,6 +501,11 @@ static int sti_pwm_probe_dt(struct sti_pwm_chip *pc) if (IS_ERR(pc->pwm_out_en)) return PTR_ERR(pc->pwm_out_en); + pc->pwm_cpt_en = devm_regmap_field_alloc(dev, pc->regmap, + reg_fields[PWM_CPT_EN]); + if (IS_ERR(pc->pwm_cpt_en)) + return PTR_ERR(pc->pwm_cpt_en); + pc->pwm_cpt_int_en = devm_regmap_field_alloc(dev, pc->regmap, reg_fields[PWM_CPT_INT_EN]); if (IS_ERR(pc->pwm_cpt_int_en))