From patchwork Mon Jun 4 18:32:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Hebb X-Patchwork-Id: 925189 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="PRE0QJ2a"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 4103TW3Ljcz9s1R for ; Tue, 5 Jun 2018 04:33:03 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751135AbeFDScu (ORCPT ); Mon, 4 Jun 2018 14:32:50 -0400 Received: from mail-qt0-f195.google.com ([209.85.216.195]:46373 "EHLO mail-qt0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750759AbeFDScq (ORCPT ); Mon, 4 Jun 2018 14:32:46 -0400 Received: by mail-qt0-f195.google.com with SMTP id h5-v6so33694533qtm.13; Mon, 04 Jun 2018 11:32:45 -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=PRE0QJ2aG0TRgW8igtDvZg8s2GxIhz2cD7fpmhDcE7JLN7wzz/UECFwOBT6AbpKaUU spflTgy9lQamZG7l6pYbiBgTk2TJYFBtdrP5Pczz6Ju5NwYcsNHZcZcU38LCfS+HxM4T kXucYOcqAV6Vvv4dN08GO7QnjrvIQo5HLOSoBukoRuoP8stkDHT6x741MWXtBHuFJsB2 MDNcM1uzVcslRwl65jWKuKW9Sk/yfozaqEpPyoal78vxAZvI5VjQO7M/Z8Z8U+sjuHZy l0PvtxjPHvjzt5hZM1BsEZBrVButvW48MvyTZ0answdcBLNQj5OV9oMRBEUaMmKbNOje AvEw== 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=gMYjQH30YVwuNQ+dhN/lsVu6wn1zL2OAnMG5mfGg6Kpg1UfWLkM9KQjwLNCwaEwnR2 q0lNJXP/a/Okqxu0RzFuS3qr9ckvTRItKJm0Z+U6ID1OMN9q8Ew3k3/myKej0ku9uTeq DC4l6AB9RwYRNgvTAcoqW3uZ26XT4B9zqOgr7LfZLd5jNwNK320cLozFyKsQF7iZd0Gl XqON7cy6ULvW8dsIJZkOe2oavMVoKhrokVjyaf5rY/xs1ujEMQxArY1fAGKEaqwuo7S3 LrzErOBI4ocxl5yXiJWNM/KxzGQhrNiKHIoHVLNtWjHQ1SHgdBPAec9O3Yjwesk5waCW Cgog== X-Gm-Message-State: APt69E1Iobr6GI+Xp6bbd5QDMXCMaTAPDbD3IFRLxSIpGB6N5s6z5F0Y hCAanI4RekzR0mXFHRdyakOP6eq62QQ= X-Google-Smtp-Source: ADUXVKIu6m3YkDj8pHHBweXMdudmhC3FyDFgzGNCBVGA3ILjkDOg06g4wdhVSyAgt5sPJdN+7DYoaw== X-Received: by 2002:ac8:1c6f:: with SMTP id j44-v6mr8532377qtk.380.1528137164789; Mon, 04 Jun 2018 11:32:44 -0700 (PDT) Received: from glados.lan ([2601:18d:4600:e68c:feaa:14ff:fe71:bf72]) by smtp.gmail.com with ESMTPSA id y41-v6sm40022567qty.84.2018.06.04.11.32.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Jun 2018 11:32:43 -0700 (PDT) From: Thomas Hebb To: linux-arm-kernel@vger.kernel.org Cc: Antoine Tenart , sebastian.hesselbarth@gmail.com, jszhang@marvell.com, Thomas Hebb , Thierry Reding , linux-pwm@vger.kernel.org (open list:PWM SUBSYSTEM), linux-kernel@vger.kernel.org (open list) Subject: [PATCH RESEND] pwm: berlin: Don't use broken prescaler values Date: Mon, 4 Jun 2018 14:32:41 -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);