From patchwork Fri Jun 3 08:22:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris BREZILLON X-Patchwork-Id: 9152127 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1F18960221 for ; Fri, 3 Jun 2016 08:24:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0B6C12654B for ; Fri, 3 Jun 2016 08:24:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0030C28309; Fri, 3 Jun 2016 08:24:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.2 required=2.0 tests=BAYES_00,FSL_HELO_HOME, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id ABCC92654B for ; Fri, 3 Jun 2016 08:24:28 +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 1b8kP6-0001fP-62; Fri, 03 Jun 2016 08:24:28 +0000 Received: from down.free-electrons.com ([37.187.137.238] helo=mail.free-electrons.com) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1b8kOJ-00018H-UL; Fri, 03 Jun 2016 08:23:42 +0000 Received: by mail.free-electrons.com (Postfix, from userid 110) id 57B581D0; Fri, 3 Jun 2016 10:23:19 +0200 (CEST) Received: from bbrezillon.home (LStLambert-657-1-97-87.w90-63.abo.wanadoo.fr [90.63.216.87]) by mail.free-electrons.com (Postfix) with ESMTPSA id AEA6D1D6; Fri, 3 Jun 2016 10:23:18 +0200 (CEST) From: Boris Brezillon To: Thierry Reding , linux-pwm@vger.kernel.org, Mark Brown , Liam Girdwood Subject: [PATCH 01/14] pwm: Add new helpers to create/manipulate PWM states Date: Fri, 3 Jun 2016 10:22:59 +0200 Message-Id: <1464942192-25967-2-git-send-email-boris.brezillon@free-electrons.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1464942192-25967-1-git-send-email-boris.brezillon@free-electrons.com> References: <1464942192-25967-1-git-send-email-boris.brezillon@free-electrons.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160603_012340_408548_F9B0DEE8 X-CRM114-Status: GOOD ( 18.18 ) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , devicetree@vger.kernel.org, Heiko Stuebner , Pawel Moll , Ian Campbell , Srinivas Kandagatla , Brian Norris , linux-kernel@vger.kernel.org, Patrice Chotard , Doug Anderson , Milo Kim , linux-rockchip@lists.infradead.org, Rob Herring , kernel@stlinux.com, Boris Brezillon , Kumar Gala , Maxime Coquelin , Stephen Barber , Ajit Pal Singh , linux-arm-kernel@lists.infradead.org, Caesar Wang MIME-Version: 1.0 Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+patchwork-linux-rockchip=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP The pwm_prepare_new_state() helper prepares a new state object containing the current PWM state except for the polarity and period fields which are set to the reference values. This is particurly useful for PWM users who want to apply a new duty-cycle expressed relatively to the reference period without changing the enable state. The PWM framework expect PWM users to configure the duty cycle in nanosecond, but most users just want to express this duty cycle relatively to the period value (i.e. duty_cycle = 33% of the period). Add pwm_{get,set}_relative_duty_cycle() helpers to ease this kind of conversion. Signed-off-by: Boris Brezillon --- include/linux/pwm.h | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/include/linux/pwm.h b/include/linux/pwm.h index 17018f3..e09babf 100644 --- a/include/linux/pwm.h +++ b/include/linux/pwm.h @@ -148,6 +148,87 @@ static inline void pwm_get_args(const struct pwm_device *pwm, } /** + * pwm_prepare_new_state() - prepare a new state to be applied with + * pwm_apply_state() + * @pwm: PWM device + * @state: state to fill with the prepared PWM state + * + * This functions prepares a state that can later be tweaked and applied + * to the PWM device with pwm_apply_state(). This is a convenient function + * that first retrieves the current PWM state and the replaces the period + * and polarity fields with the reference values defined in pwm->args. + * Once the new state is created you can adjust the ->enable and ->duty_cycle + * according to your need before calling pwm_apply_state(). + */ +static inline void pwm_prepare_new_state(const struct pwm_device *pwm, + struct pwm_state *state) +{ + struct pwm_args args; + + /* First get the current state. */ + pwm_get_state(pwm, state); + + /* Then fill it with the reference config */ + pwm_get_args(pwm, &args); + + state->period = args.period; + state->polarity = args.polarity; + state->duty_cycle = 0; +} + +/** + * pwm_get_relative_duty_cycle() - Get a relative duty_cycle value + * @state: the PWM state to extract period and duty_cycle from + * @scale: the scale you want to use for you relative duty_cycle value + * + * This functions converts the absolute duty_cycle stored in @state + * (expressed in nanosecond) into a relative value. + * For example if you want to get the duty_cycle expressed in nanosecond, + * call: + * + * pwm_get_state(pwm, &state); + * duty = pwm_get_relative_duty_cycle(&state, 100); + */ +static inline unsigned int +pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale) +{ + if (!state->period) + return 0; + + return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, + state->period); +} + +/** + * pwm_set_relative_duty_cycle() - Set a relative duty_cycle value + * @state: the PWM state to fill + * @val: the relative duty_cycle value + * @scale: the scale you use for you relative duty_cycle value + * + * This functions converts a relative duty_cycle stored into an absolute + * one (expressed in nanoseconds), and put the result in state->duty_cycle. + * For example if you want to change configure a 50% duty_cycle, call: + * + * pwm_prepare_new_state(pwm, &state); + * pwm_set_relative_duty_cycle(&state, 50, 100); + * pwm_apply_state(pwm, &state); + */ +static inline void +pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int val, + unsigned int scale) +{ + if (!scale) + return; + + /* Make sure we don't have duty_cycle > period */ + if (val > scale) + val = scale; + + state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)val * state->period, + scale); +} + +/** * struct pwm_ops - PWM controller operations * @request: optional hook for requesting a PWM * @free: optional hook for freeing a PWM