Received: by 2002:a05:7412:b112:b0:f9:3106:f1c0 with SMTP id az18csp133312rdb; Mon, 18 Dec 2023 01:09:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHDEV4TdOuq0qU2du9NLleONMJRcZxd0nt9mtIVc27hMjbpUeJeyOj8LGb2833Ae09gRU04 X-Received: by 2002:a05:6a00:2789:b0:6ce:477b:5c06 with SMTP id bd9-20020a056a00278900b006ce477b5c06mr8545754pfb.58.1702890551612; Mon, 18 Dec 2023 01:09:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702890551; cv=none; d=google.com; s=arc-20160816; b=E4vhd/enYTefUFMH8GDdA5Iuw09Bq8mj6KM7ggaQB/Dp7vGWqBqcQQFIec3U9wZsck Ci5kiewh8r9ot1FjcREFGz7ZILzbZT7O1eVAE01YWHfvnlLP+5D/e0vDTh92nD+Gn7jZ uFcZR95ccWfPYHh6qBXz2vHV/aA1BT3HrRvESmh4Wjc9k29cKmehpBosAZzvGQD0DTPb pvotVHeJdDEBtJOrjwpCGYvWqeBE421pCFZBQt4G5FenjimxItCtIxmHpI0AHp3tC2UG 4w62YONanuWH5XkZFRt3a8rbTGL4bpm+dsSgm+Txk49dAFK0WAXBdQ5NwWZRPCn6MW7/ fIDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:dkim-signature; bh=QRZm4mUaf52sFo3XS/ZMq7LXrR6TeHsJVIwLttHc5EM=; fh=783w7aDAZyP4p7OqZukZFTwWfu+r3305h4buZFldemk=; b=Akj8qMqLUAWvJAZTajYT4uvpNxfYHqLuRxjBv0qy6nIueqlxiFz1NnZAU0VfETQB+a OQPzVuVoecSEutwvqmpgczlyK6ARj3Xq1q7s6w4flYOHDsyPi77ac919Xt7ta2SUs3vq 2fFuDvr4eKTw6UXebVdsUawhOyU/kPaRze9MlYJ1HCGkX2Ax6BCTAfoBQoI6WpwZxR4R 8H12ZyopIjpB+747u/Z2pI2EQFL+ZiJUx8V3xRuYKW8kCT9Fix1NYtB/21R8kBlWeZo8 rdztUohkJoisvwHZFTINM+2w4T/5Ug1xJTeiWZ54ilkvTKw5QX4l7L0XQO/wf1t1+70j Lbmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mess.org header.s=2020 header.b=EpbINsD2; dkim=pass header.i=@mess.org header.s=2020 header.b="KI/vNOLV"; spf=pass (google.com: domain of linux-kernel+bounces-3218-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3218-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=mess.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id j4-20020a654304000000b005c661efffd0si17000685pgq.754.2023.12.18.01.09.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 01:09:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3218-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@mess.org header.s=2020 header.b=EpbINsD2; dkim=pass header.i=@mess.org header.s=2020 header.b="KI/vNOLV"; spf=pass (google.com: domain of linux-kernel+bounces-3218-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3218-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=mess.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 015FE2837E9 for ; Mon, 18 Dec 2023 09:09:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F418313AE2; Mon, 18 Dec 2023 09:07:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mess.org header.i=@mess.org header.b="EpbINsD2"; dkim=pass (2048-bit key) header.d=mess.org header.i=@mess.org header.b="KI/vNOLV" X-Original-To: linux-kernel@vger.kernel.org Received: from gofer.mess.org (gofer.mess.org [88.97.38.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 958CF12B9E; Mon, 18 Dec 2023 09:07:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mess.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mess.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1702890440; bh=hUUyx2Ra/j0PfiviV+OXUA5B+Rr5c5IfU2vtPHgn40o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EpbINsD2V6YvKtDU7RoAm2Bv9YQSnNbOpk6xPAPJ/76rPpxtRWSYGW+6lEl8bxKOv GJzuHSMaV+nkfVADJnKQ64HNUUQTOZ9WQV+nE9QpJoxc3f1Pycpz7WVs7aFA+teN8q oj+B1IIu/lUigZesYaO1i7UC7xf5A7ilIKr0uTECvqqj6cWEjjuwdrek/pKz6ajR+/ wuMqbsbLiJCUhNXWIWwW6/4AAqR+gXXxfWKuhbozSzVaMjczwHyQNv4vfxWkod13Vb TPCVX5XkNAMD8yseReccY5WNJNGHKmHLY9SBRKngUA1r5ZNbIqopOo+W1HYvc8tGAb a/0DmS6HwX9VQ== Received: by gofer.mess.org (Postfix, from userid 501) id 1D84B100A00; Mon, 18 Dec 2023 09:07:20 +0000 (GMT) X-Spam-Level: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1702890421; bh=hUUyx2Ra/j0PfiviV+OXUA5B+Rr5c5IfU2vtPHgn40o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KI/vNOLV/Bn3e+bPRS5X9dubMgKyM6izosHjarzB4iO0rSXjvsZm5F40sO4RQ93TF jgC87BcntD2ahnJbYy+z/o1HGPfg+qbYBlmdIJk6HKC7Kyv4fd/I1uBup+lh2MACX6 DwUgdP7E4PfIJ37YBWnSPMM7QrWU3KKQYo5nY0DB/4LfhQp7Q6zWFcEvDy1/0k0ViD 9BaEWnOYs7JEXDEUQ9qoYbAA1LrvthHBVGGPrzNCICwvUADgZc2uSTVYAcT6WAQhgN 7VlN7myn0V+CC8hRYIHsHVfmbpW7AhEkA/k48OuvpTt5jt73H8PiZHChuGsfKdzAW7 rL/lqKzjIcMIw== Received: from localhost.localdomain (bigcore-79.local [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 4D6881009FF; Mon, 18 Dec 2023 09:07:01 +0000 (GMT) From: Sean Young To: linux-media@vger.kernel.org, linux-pwm@vger.kernel.org, Ivaylo Dimitrov , Thierry Reding , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Sean Young , Mauro Carvalho Chehab Cc: linux-kernel@vger.kernel.org Subject: [PATCH v9 6/6] media: pwm-ir-tx: Trigger edges from hrtimer interrupt context Date: Mon, 18 Dec 2023 09:06:47 +0000 Message-ID: <13a4f0ba8e3b2c45b07d1003e736aa7ee3487523.1702890244.git.sean@mess.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This makes the generated IR much more precise. Before this change, the driver is unreliable and many users opted to use gpio-ir-tx instead. Signed-off-by: Sean Young --- drivers/media/rc/pwm-ir-tx.c | 83 +++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 5 deletions(-) diff --git a/drivers/media/rc/pwm-ir-tx.c b/drivers/media/rc/pwm-ir-tx.c index cf51e2760975..fe368aebbc13 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 tx_done; + struct pwm_state *state; + u32 carrier; + u32 duty_cycle; + const unsigned int *txbuf; + unsigned int txbuf_len; + unsigned int txbuf_index; }; static const struct of_device_id pwm_ir_of_match[] = { @@ -49,8 +57,8 @@ static int pwm_ir_set_carrier(struct rc_dev *dev, u32 carrier) return 0; } -static int pwm_ir_tx(struct rc_dev *dev, unsigned int *txbuf, - unsigned int count) +static int pwm_ir_tx_sleep(struct rc_dev *dev, unsigned int *txbuf, + unsigned int count) { struct pwm_ir *pwm_ir = dev->priv; struct pwm_device *pwm = pwm_ir->pwm; @@ -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->tx_done); + + 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_atomic(pwm_ir->pwm, pwm_ir->state); + + if (pwm_ir->txbuf_index >= pwm_ir->txbuf_len) { + complete(&pwm_ir->tx_done); + + 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_might_sleep(pwm_ir->pwm)) { + dev_info(&pdev->dev, "TX will not be accurate as PWM device might sleep\n"); + rcdev->tx_ir = pwm_ir_tx_sleep; + } else { + init_completion(&pwm_ir->tx_done); + hrtimer_init(&pwm_ir->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + pwm_ir->timer.function = pwm_ir_timer; + rcdev->tx_ir = pwm_ir_tx_atomic; + } + 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; -- 2.43.0