From patchwork Fri Oct 13 10:46:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Young X-Patchwork-Id: 13420648 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98ACACDB482 for ; Fri, 13 Oct 2023 10:47:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230088AbjJMKrc (ORCPT ); Fri, 13 Oct 2023 06:47:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229947AbjJMKra (ORCPT ); Fri, 13 Oct 2023 06:47:30 -0400 Received: from gofer.mess.org (gofer.mess.org [88.97.38.141]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FA88AD; Fri, 13 Oct 2023 03:47:26 -0700 (PDT) Received: by gofer.mess.org (Postfix, from userid 501) id B507C1000C4; Fri, 13 Oct 2023 11:47:24 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1697194044; bh=BPGCbyR+TQNm6kx/RctEE5jmHK5Z6c5mZ1jS/g7uJ/0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i5ulWeE67zrXNBPSDrHdLWH5A9Cg3aO/ZldoanPVTXu/p1ucYdTI4wXJqtlmNp8kh 1rze5jMXQX+BryiZdFJMVWyAeGDYcTKX7YgwdPt/U7hYR+QixoY4YkarRqGZ1GxAR1 +UOIAV0jNNU3g3RrTUY1pADCwLLVS4xZP+JvymDaRvR99VQJaKSYYLvX84Q/ITPNpL qDsP3bdRTK5MPBwqMj4vo8oC+6uXTKEr919LX0C+Icaxe7gFtpNNfcLyHtrRF04yal tGJmFCCZ08Aqw+lOYHm4w+zYLr8QIatFmNPjedmIQclIkwkf4LUFbj4iQgstE9g9B2 jesrYh9H0mwLg== Received: from bigcore.mess.org (unknown [IPv6:2a02:8011:d000:212:ca7f:54ff:fe51:14d6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by gofer.mess.org (Postfix) with ESMTPSA id 6154E1000C4; Fri, 13 Oct 2023 11:47:23 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1697194043; bh=BPGCbyR+TQNm6kx/RctEE5jmHK5Z6c5mZ1jS/g7uJ/0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YjTcWDQFlQc5ZE2UNkgTGp7+iy64nWdtcYgAXxzPUcbzpkqwe1kQwcew2MfE7y5rr U5VngV/vcehLbJvjVPbrzbY63amvwFMdCRV7HovaB/yKXZZ9TV/u4kapwsEo+3m4E5 tM0G333bpew4mkqX9j/eMHIR7vzwEca76EDFnwdkurZn/ezph4/c8q20HKjpDDl/kx mIlVg7VJwyIK46KRhOQKlOaDhqYuFs/+Uh30+Jwrrne8hIkMyyJZF7y7w/d9Gf9qP4 +GPE4iwXWARUdIbnnSMzu1aaXCASVRpxxPNgsXSKZCx9MT0ggkc5GB12d1pCQFQSQO MbS7kE2p/oy7w== From: Sean Young To: linux-media@vger.kernel.org, Ivaylo Dimitrov , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: Sean Young , linux-pwm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/3] pwm: make it possible to apply pwm changes in atomic context Date: Fri, 13 Oct 2023 11:46:14 +0100 Message-ID: <9c0f1616fca5b218336b9321bfefe7abb7e1749f.1697193646.git.sean@mess.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Some drivers require sleeping, for example if the pwm device is connected over i2c. The pwm-ir-tx requires precise timing, and sleeping causes havoc with the generated IR signal when sleeping occurs. This patch makes it possible to use pwm when the driver does not sleep, by introducing the pwm_can_sleep() function. Signed-off-by: Sean Young --- drivers/pwm/core.c | 62 ++++++++++++++++++++++++++++------- drivers/pwm/pwm-renesas-tpu.c | 1 - include/linux/pwm.h | 29 ++++++++++++++-- 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c index dc66e3405bf5..241510ba1823 100644 --- a/drivers/pwm/core.c +++ b/drivers/pwm/core.c @@ -489,24 +489,15 @@ static void pwm_apply_state_debug(struct pwm_device *pwm, } /** - * pwm_apply_state() - atomically apply a new state to a PWM device + * pwm_apply_state_unchecked() - atomically apply a new state to a PWM device * @pwm: PWM device * @state: new state to apply */ -int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) +static int pwm_apply_state_unchecked(struct pwm_device *pwm, const struct pwm_state *state) { struct pwm_chip *chip; int err; - /* - * Some lowlevel driver's implementations of .apply() make use of - * mutexes, also with some drivers only returning when the new - * configuration is active calling pwm_apply_state() from atomic context - * is a bad idea. So make it explicit that calling this function might - * sleep. - */ - might_sleep(); - if (!pwm || !state || !state->period || state->duty_cycle > state->period) return -EINVAL; @@ -535,8 +526,57 @@ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) return 0; } + +/** + * pwm_apply_state() - atomically apply a new state to a PWM device + * Cannot be used in atomic context. + * @pwm: PWM device + * @state: new state to apply + */ +int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) +{ + /* + * Some lowlevel driver's implementations of .apply() make use of + * mutexes, also with some drivers only returning when the new + * configuration is active calling pwm_apply_state() from atomic context + * is a bad idea. So make it explicit that calling this function might + * sleep. + */ + might_sleep(); + + if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->ops->atomic) { + int err; + + /* + * Catch any sleeping drivers when atomic is set. + */ + non_block_start(); + err = pwm_apply_state_unchecked(pwm, state); + non_block_end(); + + return err; + } + + return pwm_apply_state_unchecked(pwm, state); +} EXPORT_SYMBOL_GPL(pwm_apply_state); +/** + * pwm_apply_state_atomic() - atomically apply a new state to a PWM device + * Can be used from atomic context. + * @pwm: PWM device + * @state: new state to apply + */ +int pwm_apply_state_atomic(struct pwm_device *pwm, + const struct pwm_state *state) +{ + WARN_ONCE(!pwm->chip->ops->atomic, + "sleeping pwm driver used in atomic context"); + + return pwm_apply_state_unchecked(pwm, state); +} +EXPORT_SYMBOL_GPL(pwm_apply_state_atomic); + /** * pwm_capture() - capture and report a PWM signal * @pwm: PWM device diff --git a/drivers/pwm/pwm-renesas-tpu.c b/drivers/pwm/pwm-renesas-tpu.c index d7311614c846..96797a33d8c6 100644 --- a/drivers/pwm/pwm-renesas-tpu.c +++ b/drivers/pwm/pwm-renesas-tpu.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include diff --git a/include/linux/pwm.h b/include/linux/pwm.h index d2f9f690a9c1..93f166ab03c1 100644 --- a/include/linux/pwm.h +++ b/include/linux/pwm.h @@ -267,6 +267,7 @@ struct pwm_capture { * @get_state: get the current PWM state. This function is only * called once per PWM device when the PWM chip is * registered. + * @atomic: can the driver execute pwm_apply_state in atomic context * @owner: helps prevent removal of modules exporting active PWMs */ struct pwm_ops { @@ -278,6 +279,7 @@ struct pwm_ops { const struct pwm_state *state); int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_state *state); + bool atomic; struct module *owner; }; @@ -310,6 +312,7 @@ struct pwm_chip { #if IS_ENABLED(CONFIG_PWM) /* PWM user APIs */ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state); +int pwm_apply_state_atomic(struct pwm_device *pwm, const struct pwm_state *state); int pwm_adjust_config(struct pwm_device *pwm); /** @@ -380,6 +383,17 @@ static inline void pwm_disable(struct pwm_device *pwm) pwm_apply_state(pwm, &state); } +/** + * pwm_is_atomic() - is pwm_apply_state_atomic() supported? + * @pwm: PWM device + * + * Returns: true pwm_apply_state_atomic() can be called from atomic context. + */ +static inline bool pwm_is_atomic(struct pwm_device *pwm) +{ + return pwm->chip->ops->atomic; +} + /* PWM provider APIs */ int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, unsigned long timeout); @@ -408,16 +422,27 @@ struct pwm_device *devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, const char *con_id); #else +static inline bool pwm_is_atomic(struct pwm_device *pwm) +{ + return false; +} + static inline int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) { might_sleep(); - return -ENOTSUPP; + return -EOPNOTSUPP; +} + +static inline int pwm_apply_state_atomic(struct pwm_device *pwm, + const struct pwm_state *state) +{ + return -EOPNOTSUPP; } static inline int pwm_adjust_config(struct pwm_device *pwm) { - return -ENOTSUPP; + return -EOPNOTSUPP; } static inline int pwm_config(struct pwm_device *pwm, int duty_ns, From patchwork Fri Oct 13 10:46:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Young X-Patchwork-Id: 13420649 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B32FDCDB485 for ; Fri, 13 Oct 2023 10:47:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230310AbjJMKrd (ORCPT ); Fri, 13 Oct 2023 06:47:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229930AbjJMKrb (ORCPT ); Fri, 13 Oct 2023 06:47:31 -0400 Received: from gofer.mess.org (gofer.mess.org [IPv6:2a02:8011:d000:212::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6A22B7; Fri, 13 Oct 2023 03:47:26 -0700 (PDT) Received: by gofer.mess.org (Postfix, from userid 501) id 819221000C6; Fri, 13 Oct 2023 11:47:25 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1697194045; bh=a+OT+HzNlnPxyf1ivSREODdEglTMX6uJLENTDZVv+3Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o2rYGmFbzU16Zo9TZ5PgbeTlCgt2UAm/9upTR5nZ/Fh/NatlJIEH+dLl8DxMacDLs KwQwemhkJHGkejK2zCttay/P5x3SRy3Afe4ML4IlUbX7EHPffPGG0PiYzrlzy77maB iMLyil3Y4qKit5K365OXf1o+UWhkNx6OgKVaMkVcZA3QUoSzfeqgIWk/xHT3BwfDHt pjmp09NJ9UJfH1/kfvxZ90eP3Ke96ZWPFwvASr2qVzWwVAfCl5QiknABL4QoC6t80d E7AD8rAZ9iAP3oUFyWoZgWdUWaJSLHAcO1Pvk2Oznz5Bc5rZonordQRO1SWEOT2AM8 kfQ/jMqphyllQ== Received: from bigcore.mess.org (unknown [IPv6:2a02:8011:d000:212:ca7f:54ff:fe51:14d6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by gofer.mess.org (Postfix) with ESMTPSA id 95D831000C6; Fri, 13 Oct 2023 11:47:23 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1697194043; bh=a+OT+HzNlnPxyf1ivSREODdEglTMX6uJLENTDZVv+3Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SyFMY6Cp/t2LA/IOZwLp4An5hFfJhhWgS9n8uhI7JwF3aR63b0IceINN0kJpQfTFi i700a30JMLprBEYm3Vduw3pD5FUXcS/T9OjLF2aU/D9fezt2GVGLYp8wdDk2VSLfaI 53ossojhah6OONUpD48GxW93FitpVkDhy7QZsRShrmi/7GtCqG77Sj97QC9knIwL8p HpqnO2LyqV9sb+27mRDxtfC+e1ITLwJ/aXv51PYHDpKq2xt0Jsi3jFMv7NAaIDcype 9dPaFAFBHNxa3/41gjHyOZZDLcCuajJ5dkKyAUDZreLEooHxh6Y8p5sBoNjzt50rM6 /n4UEhoDXHhMg== From: Sean Young To: linux-media@vger.kernel.org, Ivaylo Dimitrov , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Florian Fainelli , Ray Jui , Scott Branden , Broadcom internal kernel review list Cc: Sean Young , linux-pwm@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/3] pwm: bcm2835: allow pwm driver to be used in atomic context Date: Fri, 13 Oct 2023 11:46:15 +0100 Message-ID: <6ce73b2688f059e7169935699044104cf37b2425.1697193646.git.sean@mess.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org clk_get_rate() may do a mutex lock. Since the clock rate cannot change on an rpi, simply fetch it once. Signed-off-by: Sean Young --- drivers/pwm/pwm-bcm2835.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c index bdfc2a5ec0d6..59ea154dd657 100644 --- a/drivers/pwm/pwm-bcm2835.c +++ b/drivers/pwm/pwm-bcm2835.c @@ -28,6 +28,7 @@ struct bcm2835_pwm { struct device *dev; void __iomem *base; struct clk *clk; + unsigned long rate; }; static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip) @@ -63,17 +64,11 @@ static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, { struct bcm2835_pwm *pc = to_bcm2835_pwm(chip); - unsigned long rate = clk_get_rate(pc->clk); unsigned long long period_cycles; u64 max_period; u32 val; - if (!rate) { - dev_err(pc->dev, "failed to get clock rate\n"); - return -EINVAL; - } - /* * period_cycles must be a 32 bit value, so period * rate / NSEC_PER_SEC * must be <= U32_MAX. As U32_MAX * NSEC_PER_SEC < U64_MAX the @@ -88,13 +83,13 @@ static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, * <=> period < ((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate * <=> period <= ceil((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate) - 1 */ - max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, rate) - 1; + max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, pc->rate) - 1; if (state->period > max_period) return -EINVAL; /* set period */ - period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * rate, NSEC_PER_SEC); + period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * pc->rate, NSEC_PER_SEC); /* don't accept a period that is too small */ if (period_cycles < PERIOD_MIN) @@ -103,7 +98,7 @@ static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, writel(period_cycles, pc->base + PERIOD(pwm->hwpwm)); /* set duty cycle */ - val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * rate, NSEC_PER_SEC); + val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * pc->rate, NSEC_PER_SEC); writel(val, pc->base + DUTY(pwm->hwpwm)); /* set polarity */ @@ -129,6 +124,7 @@ static const struct pwm_ops bcm2835_pwm_ops = { .request = bcm2835_pwm_request, .free = bcm2835_pwm_free, .apply = bcm2835_pwm_apply, + .atomic = true, .owner = THIS_MODULE, }; @@ -156,6 +152,13 @@ static int bcm2835_pwm_probe(struct platform_device *pdev) if (ret) return ret; + pc->rate = clk_get_rate(pc->clk); + if (!pc->rate) { + dev_err(pc->dev, "failed to get clock rate\n"); + ret = -EINVAL; + goto add_fail; + } + pc->chip.dev = &pdev->dev; pc->chip.ops = &bcm2835_pwm_ops; pc->chip.npwm = 2; From patchwork Fri Oct 13 10:46:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Young X-Patchwork-Id: 13420650 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 95F44CDB488 for ; Fri, 13 Oct 2023 10:47:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230349AbjJMKrd (ORCPT ); Fri, 13 Oct 2023 06:47:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230052AbjJMKrb (ORCPT ); Fri, 13 Oct 2023 06:47:31 -0400 Received: from gofer.mess.org (gofer.mess.org [IPv6:2a02:8011:d000:212::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93A04BD; Fri, 13 Oct 2023 03:47:27 -0700 (PDT) Received: by gofer.mess.org (Postfix, from userid 501) id 584D3100100; Fri, 13 Oct 2023 11:47:26 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1697194046; bh=cqss2B5a2PjBlYIFpZklT248DB07ue+qVmrQyspsCUw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RvRMgrHry4Y4LsVtp+ipnOWb2RkoXZwoiG2nBMj/4fZcIrx8VpNcNPEbPJdu9QdDK HBtoMCBLpztJj/duGUzwtsbgn3Cw8fzv7TbRQLCEh+hxm5maMQ4ohhp5pZ12TWncJC iA2tRBiZZa97ICIQ4YpYevEdu3DlHDl2ntvSv0oabUgk+nL9T7mJ3X37UoZ9Otx3db jqqBvwEJO7zLRfGFUq9id2ANDx+zOnqvUXQJCR1ekjPbTK6CpS6LHl1kcCuYKTv54Z 3hvTpdlB5Q39NiWloL2SzaMHyALZqCNv9oHPlgpff/39H8wAm2vL5wTLs+F8pp9jas 4gdC/bIB+u2nw== Received: from bigcore.mess.org (unknown [IPv6:2a02:8011:d000:212:ca7f:54ff:fe51:14d6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by gofer.mess.org (Postfix) with ESMTPSA id D9EB110005E; Fri, 13 Oct 2023 11:47:23 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1697194043; bh=cqss2B5a2PjBlYIFpZklT248DB07ue+qVmrQyspsCUw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iFxSQUoWKERA0Q7iR+gRw2j3QTmV/tVah/f8SBfftpjazSqU9PvyGo3EnLFiLJGlp ovjQ8k9qtv7AOQmI1zRHOT90S5hQNtsdRtoi/rqh5xd5S7Mo4I8bxN75d951rKeEnj g35mNJNLne5f1GZKpzUEfiiRRJUrz31e+P6Gqh4iZPNtdenIM+qSMMCv3rIZl8hIhA V5vD7w6ZprQJvShkthxIt/NmMQ+sEWCl9ydPYa7aA1tuufRnJDooYcogWfWZ8iBCDN TIkWqnePt0EOtqHY37VcDQ3cPcnkB2NsQJlsQQmJ5jcfbYSOGpIKCSox5qpWo/ZwIa QVrJzRYkAAbQQ== From: Sean Young To: linux-media@vger.kernel.org, Ivaylo Dimitrov , Sean Young , Mauro Carvalho Chehab , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org Subject: [PATCH v2 3/3] media: pwm-ir-tx: trigger edges from hrtimer interrupt context Date: Fri, 13 Oct 2023 11:46:16 +0100 Message-ID: <1560b474f7d426bc77100665c14c3a29c3af3e75.1697193646.git.sean@mess.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org This makes the driver much more precise. Signed-off-by: Sean Young --- drivers/media/rc/pwm-ir-tx.c | 79 ++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 3 deletions(-) diff --git a/drivers/media/rc/pwm-ir-tx.c b/drivers/media/rc/pwm-ir-tx.c index c5f37c03af9c..3e801fa8ee2c 100644 --- a/drivers/media/rc/pwm-ir-tx.c +++ b/drivers/media/rc/pwm-ir-tx.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #define DRIVER_NAME "pwm-ir-tx" @@ -17,8 +19,14 @@ struct pwm_ir { struct pwm_device *pwm; - unsigned int carrier; - unsigned int duty_cycle; + struct hrtimer timer; + struct completion completion; + struct pwm_state *state; + uint carrier; + uint duty_cycle; + uint *txbuf; + uint txbuf_len; + uint txbuf_index; }; static const struct of_device_id pwm_ir_of_match[] = { @@ -82,6 +90,62 @@ static int pwm_ir_tx(struct rc_dev *dev, unsigned int *txbuf, return count; } +static int pwm_ir_tx_atomic(struct rc_dev *dev, unsigned int *txbuf, + unsigned int count) +{ + struct pwm_ir *pwm_ir = dev->priv; + struct pwm_device *pwm = pwm_ir->pwm; + struct pwm_state state; + + pwm_init_state(pwm, &state); + + state.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, pwm_ir->carrier); + pwm_set_relative_duty_cycle(&state, pwm_ir->duty_cycle, 100); + + pwm_ir->txbuf = txbuf; + pwm_ir->txbuf_len = count; + pwm_ir->txbuf_index = 0; + pwm_ir->state = &state; + + hrtimer_start(&pwm_ir->timer, 0, HRTIMER_MODE_REL); + + wait_for_completion(&pwm_ir->completion); + + return count; +} + +static enum hrtimer_restart pwm_ir_timer(struct hrtimer *timer) +{ + struct pwm_ir *pwm_ir = container_of(timer, struct pwm_ir, timer); + ktime_t now; + + /* + * If we happen to hit an odd latency spike, loop through the + * pulses until we catch up. + */ + do { + u64 ns; + + pwm_ir->state->enabled = !(pwm_ir->txbuf_index % 2); + pwm_apply_state_atomic(pwm_ir->pwm, pwm_ir->state); + + if (pwm_ir->txbuf_index >= pwm_ir->txbuf_len) { + complete(&pwm_ir->completion); + + return HRTIMER_NORESTART; + } + + ns = US_TO_NS(pwm_ir->txbuf[pwm_ir->txbuf_index]); + hrtimer_add_expires_ns(timer, ns); + + pwm_ir->txbuf_index++; + + now = timer->base->get_time(); + } while (hrtimer_get_expires_tv64(timer) < now); + + return HRTIMER_RESTART; +} + static int pwm_ir_probe(struct platform_device *pdev) { struct pwm_ir *pwm_ir; @@ -103,10 +167,19 @@ static int pwm_ir_probe(struct platform_device *pdev) if (!rcdev) return -ENOMEM; + if (pwm_is_atomic(pwm_ir->pwm)) { + init_completion(&pwm_ir->completion); + hrtimer_init(&pwm_ir->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + pwm_ir->timer.function = pwm_ir_timer; + rcdev->tx_ir = pwm_ir_tx_atomic; + } else { + dev_info(&pdev->dev, "tx will not be accurate as pwm device does not support atomic mode"); + rcdev->tx_ir = pwm_ir_tx; + } + rcdev->priv = pwm_ir; rcdev->driver_name = DRIVER_NAME; rcdev->device_name = DEVICE_NAME; - rcdev->tx_ir = pwm_ir_tx; rcdev->s_tx_duty_cycle = pwm_ir_set_duty_cycle; rcdev->s_tx_carrier = pwm_ir_set_carrier;