From patchwork Mon Jul 20 08:23:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Clemens Gruber X-Patchwork-Id: 497598 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 14972140770 for ; Mon, 20 Jul 2015 18:25:12 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755938AbbGTIYW (ORCPT ); Mon, 20 Jul 2015 04:24:22 -0400 Received: from mail.pqgruber.com ([178.189.19.235]:35452 "EHLO mail.pqgruber.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755869AbbGTIYU (ORCPT ); Mon, 20 Jul 2015 04:24:20 -0400 Received: from archie.tuxnet (chello213047168109.15.14.vie.surfer.at [213.47.168.109]) by mail.pqgruber.com (Postfix) with ESMTPSA id 413B79CEDA; Mon, 20 Jul 2015 10:24:18 +0200 (CEST) From: Clemens Gruber To: linux-pwm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Thierry Reding , Clemens Gruber , Steffen Trumtrar Subject: [PATCH v2 2/2] pwm-pca9685: Support changing the output frequency Date: Mon, 20 Jul 2015 10:23:51 +0200 Message-Id: <1437380631-7149-3-git-send-email-clemens.gruber@pqgruber.com> X-Mailer: git-send-email 2.4.6 In-Reply-To: <1437380631-7149-1-git-send-email-clemens.gruber@pqgruber.com> References: <1437380631-7149-1-git-send-email-clemens.gruber@pqgruber.com> Sender: linux-pwm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pwm@vger.kernel.org Previously, period_ns and duty_ns were only used to determine the ratio of ON and OFF time, the default frequency of 200 Hz was never changed. The PCA9685 however is capable of changing the PWM output frequency, which is expected when changing the period. This patch configures the prescaler accordingly, using the formula and notes provided in the PCA9685 datasheet. Bounds checking for the minimum and maximum frequencies, last updated in revision v.4 of said datasheet, is also added. The prescaler is only touched if the period changed, because we have to put the chip into sleep mode to unlock the prescale register. If it is changed, the PWM output frequency changes for all outputs, because there is one prescaler per chip. This is documented in the PCA9685 datasheet and in the comments. Cc: Thierry Reding Cc: Steffen Trumtrar Signed-off-by: Clemens Gruber --- Changes from v1: - Only put chip into sleep mode if the bounds checking succeeds --- drivers/pwm/pwm-pca9685.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c index f4a9c4a..fba1a63 100644 --- a/drivers/pwm/pwm-pca9685.c +++ b/drivers/pwm/pwm-pca9685.c @@ -2,6 +2,7 @@ * Driver for PCA9685 16-channel 12-bit PWM LED controller * * Copyright (C) 2013 Steffen Trumtrar + * Copyright (C) 2015 Clemens Gruber * * based on the pwm-twl-led.c driver * @@ -24,6 +25,7 @@ #include #include #include +#include #define PCA9685_MODE1 0x00 #define PCA9685_MODE2 0x01 @@ -42,6 +44,13 @@ #define PCA9685_ALL_LED_OFF_H 0xFD #define PCA9685_PRESCALE 0xFE +#define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */ +#define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */ + +#define PCA9685_COUNTER_RANGE 4096 +#define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */ +#define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */ + #define PCA9685_NUMREGS 0xFF #define PCA9685_MAXCHAN 0x10 @@ -59,6 +68,7 @@ struct pca9685 { struct pwm_chip chip; struct regmap *regmap; int active_cnt; + int period_ns; }; static inline struct pca9685 *to_pca(struct pwm_chip *chip) @@ -72,6 +82,41 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, struct pca9685 *pca = to_pca(chip); unsigned long long duty; unsigned int reg; + int prescale; + + if (period_ns != pca->period_ns) { + /* + * Set prescale register according to the configured period, + * which in turn changes the frequency of all 16 PWM outputs. + */ + prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns, + PCA9685_COUNTER_RANGE * 1000) - 1; + + if (prescale >= PCA9685_PRESCALE_MIN && + prescale <= PCA9685_PRESCALE_MAX) { + + /* Set SLEEP bit to allow writing to the prescale register */ + regmap_update_bits(pca->regmap, PCA9685_MODE1, + MODE1_SLEEP, MODE1_SLEEP); + + regmap_write(pca->regmap, PCA9685_PRESCALE, prescale); + + /* Clear SLEEP bit */ + regmap_update_bits(pca->regmap, PCA9685_MODE1, + MODE1_SLEEP, 0x0); + + /* Wait 500us for the oscillator to be back up */ + udelay(500); + + pca->period_ns = period_ns; + } + else { + dev_err(chip->dev, + "prescaler not set: period out of bounds\n"); + return -EINVAL; + } + + } if (duty_ns < 1) { if (pwm->hwpwm >= PCA9685_MAXCHAN) @@ -111,7 +156,7 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, return 0; } - duty = 4096 * (unsigned long long)duty_ns; + duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns; duty = DIV_ROUND_UP_ULL(duty, period_ns); if (pwm->hwpwm >= PCA9685_MAXCHAN) @@ -252,6 +297,7 @@ static int pca9685_pwm_probe(struct i2c_client *client, ret); return ret; } + pca->period_ns = PCA9685_DEFAULT_PERIOD; i2c_set_clientdata(client, pca);