From patchwork Mon May 14 21:58:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Hebb X-Patchwork-Id: 913263 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="rKe9Esbw"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 40lF2H0BFfz9s0q for ; Tue, 15 May 2018 07:58:31 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752636AbeENV6V (ORCPT ); Mon, 14 May 2018 17:58:21 -0400 Received: from mail-qt0-f195.google.com ([209.85.216.195]:41527 "EHLO mail-qt0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752044AbeENV6T (ORCPT ); Mon, 14 May 2018 17:58:19 -0400 Received: by mail-qt0-f195.google.com with SMTP id g13-v6so18246307qth.8; Mon, 14 May 2018 14:58:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=LlLnzPitWc5iq50RgBwsSSOvBNrIcG9yFrOp3RKswN8=; b=rKe9Esbwb5X7EIc1RCW+D2ZyaSc0Ny2dY23oZ1Yi2/tzdU3etwEuiv/nNr112NigRW AFjniBbTt0zwnjPPg9ShHiTk1t4tbxJq6wUOyRsubSZB8dRcSFw2KRj6Hb0rIzIwHQeQ Ntw0M9RSmcQdslKSMCIe8mkLvxUXkOsVp0+lwIKEznh5qXskZhRK+oIO6/O4X3NLw7D8 eilT/Xuy19NmTtjClw3mppwRLe/DDiYygLAhs8HHdYlCLBTsETzXMPrWgTbyhfo0z049 Tgc4Cc5uzpmHO2tW5alv/i0ckiPszZAkyoxY00vYyC2Aee8GDdpKAFi/WGD5jn+pUjMc veJw== 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=LlLnzPitWc5iq50RgBwsSSOvBNrIcG9yFrOp3RKswN8=; b=ldfTNMd+JkJvF709oasFgFDnxZf6Sal/RLNx7uOpm1NkG4dQZExJVIvHOqNODqft78 5POt0TucA1gxwIRRoTU8HneSr2qKHZCP3kPNbWwSp1WCuffzdQ9DVfaaU5biIr3Bn9jo IvgnWz7ASbizyyXX6yZFalDfwQZ8oHmt+w/vtejb5zxrl0fzX0UEVPVAkJve9SPXViUW XpiWhqQT6CcpokKyCvKtRSjg8y4l6e38tN8oKfWS4dwTl8PvyGcPmF23yv+gUaKmO1vL cuzhyL48OlGHKJ/NegmOZMw9IaTGWSpAMoVzuXW1XAt4qQftVu0UD1SIFrH6HExgD7/P Xczw== X-Gm-Message-State: ALKqPwdVhtyc23DrnuOSMyibHaBbAzt+VbpFLwzxDgfuqQXVVDVfY6r8 /TZ2zR+cogl1jYDbvxMH/3yvUpTEQ2s= X-Google-Smtp-Source: AB8JxZoD4ytNln7FBeyizcWRpqA2hxTx3yNTyDRRfOKmW0rKIcfJJ83v4gTy+T8I4yIW1EcVAljKyQ== X-Received: by 2002:ac8:24b0:: with SMTP id s45-v6mr10744320qts.84.1526335098176; Mon, 14 May 2018 14:58:18 -0700 (PDT) Received: from glados.lan ([2601:18d:4600:e68c:feaa:14ff:fe71:bf72]) by smtp.gmail.com with ESMTPSA id h128-v6sm7339218qkc.80.2018.05.14.14.58.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 May 2018 14:58:17 -0700 (PDT) From: Thomas Hebb To: linux-kernel@vger.kernel.org Cc: Thomas Hebb , Thierry Reding , linux-pwm@vger.kernel.org (open list:PWM SUBSYSTEM) Subject: [PATCH 1/2] pwm: berlin: Don't use broken prescaler values Date: Mon, 14 May 2018 17:58:14 -0400 Message-Id: X-Mailer: git-send-email 2.17.0 Sender: linux-pwm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pwm@vger.kernel.org Six of the eight prescaler values available for Berlin PWM are not true prescalers but rather internal shifts that throw away the high bits of TCNT. Currently, we attempt to use those high bits, leading to erratic behavior. Restrict the prescaler configurations we select to only the two that respect the full range of TCNT. Tested on BG2CD. Signed-off-by: Thomas Hebb --- drivers/pwm/pwm-berlin.c | 45 ++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/drivers/pwm/pwm-berlin.c b/drivers/pwm/pwm-berlin.c index 771859aca4be..7c8d6a168ceb 100644 --- a/drivers/pwm/pwm-berlin.c +++ b/drivers/pwm/pwm-berlin.c @@ -21,8 +21,18 @@ #define BERLIN_PWM_EN 0x0 #define BERLIN_PWM_ENABLE BIT(0) #define BERLIN_PWM_CONTROL 0x4 -#define BERLIN_PWM_PRESCALE_MASK 0x7 -#define BERLIN_PWM_PRESCALE_MAX 4096 +/* + * The prescaler claims to support 8 different moduli, configured using the + * low three bits of PWM_CONTROL. (Sequentially, they are 1, 4, 8, 16, 64, + * 256, 1024, and 4096.) However, the moduli from 4 to 1024 appear to be + * implemented by internally shifting TCNT left without adding additional + * bits. So, the max TCNT that actually works for a modulus of 4 is 0x3fff; + * for 8, 0x1fff; and so on. This means that those moduli are entirely + * useless, as we could just do the shift ourselves. The 4096 modulus is + * implemented with a real prescaler, so we do use that, but we treat it + * as a flag instead of pretending the modulus is actually configurable. + */ +#define BERLIN_PWM_PRESCALE_4096 0x7 #define BERLIN_PWM_INVERT_POLARITY BIT(3) #define BERLIN_PWM_DUTY 0x8 #define BERLIN_PWM_TCNT 0xc @@ -46,10 +56,6 @@ static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip) return container_of(chip, struct berlin_pwm_chip, chip); } -static const u32 prescaler_table[] = { - 1, 4, 8, 16, 64, 256, 1024, 4096 -}; - static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *chip, unsigned int channel, unsigned long offset) { @@ -86,33 +92,32 @@ static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev, int duty_ns, int period_ns) { struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip); - unsigned int prescale; + bool prescale_4096 = false; u32 value, duty, period; - u64 cycles, tmp; + u64 cycles; cycles = clk_get_rate(pwm->clk); cycles *= period_ns; do_div(cycles, NSEC_PER_SEC); - for (prescale = 0; prescale < ARRAY_SIZE(prescaler_table); prescale++) { - tmp = cycles; - do_div(tmp, prescaler_table[prescale]); + if (cycles > BERLIN_PWM_MAX_TCNT) { + prescale_4096 = true; + cycles >>= 12; // Prescaled by 4096 - if (tmp <= BERLIN_PWM_MAX_TCNT) - break; + if (cycles > BERLIN_PWM_MAX_TCNT) + return -ERANGE; } - if (tmp > BERLIN_PWM_MAX_TCNT) - return -ERANGE; - - period = tmp; - cycles = tmp * duty_ns; + period = cycles; + cycles *= duty_ns; do_div(cycles, period_ns); duty = cycles; value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL); - value &= ~BERLIN_PWM_PRESCALE_MASK; - value |= prescale; + if (prescale_4096) + value |= BERLIN_PWM_PRESCALE_4096; + else + value &= ~BERLIN_PWM_PRESCALE_4096; berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL); berlin_pwm_writel(pwm, pwm_dev->hwpwm, duty, BERLIN_PWM_DUTY);