From patchwork Mon Dec 18 09:06:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Young X-Patchwork-Id: 755807 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 837BC111A1; Mon, 18 Dec 2023 09:07:18 +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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mess.org header.i=@mess.org header.b="Q8Q++5gh"; dkim=pass (2048-bit key) header.d=mess.org header.i=@mess.org header.b="VZANn3yQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1702890436; bh=4Uv9T6s4wh/LXg7ZHtJ7w1pHCYlvw7d96NQ/caKWDUg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q8Q++5ghCmF2m4LEceslU3Tkgl3mmWb2WZuc8OQ+tuuFW/NseyyesI/r+aqOo6RxW zJfIh6EhLECuP1rgJ97JqIv+KPY/r2bEmCpsiUy+DAq+8G08ZfZadZFIBlgfvHX6N3 eUIanTzwcjF4F6pUuwxWjLlhzxWxp2t5uzVV7hWOKMFXYvcC4i6PRXnCqQ823A3faN as5fNmFmxwxNrw+2vGEUx7JYmdnbxnIfmnK5NBnQpsb79xcyehGhM+sbSifVxRBRX7 AWyr0vR0mhkBQLLj9D/sG0J5ilDVm7Mg8wHRPD6jPhm8taoZjvJOBbOUHF2/APG26y IwD0UHTOzgJwg== Received: by gofer.mess.org (Postfix, from userid 501) id AE980100A00; Mon, 18 Dec 2023 09:07:16 +0000 (GMT) X-Spam-Level: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1702890420; bh=4Uv9T6s4wh/LXg7ZHtJ7w1pHCYlvw7d96NQ/caKWDUg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VZANn3yQEyw65xlkedEQD3QAHxG8/Oyqw0lIhoD6YcuN+B3IBtgNEWv80iTLycZ23 d4nJTNK5CJoCnKSDjP+N7x2rkgxHyf4+TYvyC0xW4rTNpfyriYwgMHdJ2ZZ8YCoSZs S9RTBhtWAUOv2jcnAKlO/jMWkeEMD8+dYcTMY6+9ygh6jUtl6npONx6BwI+Nm9xh8Q ge5FiYXgps6T4bGX25SUNs55g/VuyEYoZ5Hs7oAyzPlIgwupgjxaulcyrFjPZQZ3uO CSuGM0VwRkCHTSXMbRpFjPl/qjjWGmp1dZKOQfXSjScsMirvAo2NA4Cnj7SNudgJd+ ZhxHUGJE1VPOg== 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 436401000CC; Mon, 18 Dec 2023 09:07:00 +0000 (GMT) From: Sean Young To: linux-media@vger.kernel.org, linux-pwm@vger.kernel.org, Ivaylo Dimitrov , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: Sean Young , linux-kernel@vger.kernel.org Subject: [PATCH v9 3/6] pwm: renesas: Remove unused include Date: Mon, 18 Dec 2023 09:06:44 +0000 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 No mutex is used in this driver. Reviewed-by: Uwe Kleine-König Signed-off-by: Sean Young --- drivers/pwm/pwm-renesas-tpu.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/pwm/pwm-renesas-tpu.c b/drivers/pwm/pwm-renesas-tpu.c index ce92db1f8511..28265fdfc92a 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 From patchwork Mon Dec 18 09:06:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Young X-Patchwork-Id: 755806 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 0D71111C95; Mon, 18 Dec 2023 09:07:19 +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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mess.org header.i=@mess.org header.b="FOxTn7/a"; dkim=pass (2048-bit key) header.d=mess.org header.i=@mess.org header.b="I4H5flPJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1702890438; bh=nq08shY4F84JWnlQ3SSHlueQDTZg9R3tBoXVvnOsM60=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FOxTn7/aDUtOeUOzjNz5FSNs+NWeUi6NUfYs9zTgeX0pU5cBCoTQK9FdfleLSjnwD fbUwRl47+2NyCUkSQAOE0WOTkJzEzQUyFXrPMtAMj4THzT0KkuUfuI04FYP3Y/u8RB IuJoDUgsEGm5xj1cFNvWtu7UegW7goaz8qy+v8664SNhan7jmFKGf2xm0KMcC/PVIF dirRu4u/7l2SGwFqg6ht/U45BP5W93sSgmPKNXih/H6K/8h69zgL+3or5kDjkHjvA3 CuLS5evuFQZQ+41yRYPCh0XbcMdhFAk5LiAFarMrMgXFmeM1s1r2a517ucmk1mjH9n vT+kd8Se9csrA== Received: by gofer.mess.org (Postfix, from userid 501) id 1A937100A09; Mon, 18 Dec 2023 09:07:18 +0000 (GMT) X-Spam-Level: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mess.org; s=2020; t=1702890420; bh=nq08shY4F84JWnlQ3SSHlueQDTZg9R3tBoXVvnOsM60=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I4H5flPJJAIwcRSTQGENETb9ftnG6shx2tMwk799sk5aFg+eehZcD45atMqdSani+ ZIFR8g+2jRJ5d2LRNykgt2wh68Wi7Bmrzg4CngsbL8YdfraNfQztKEfJV5Lvm6xB5H p4q8pjE/edXZFixXIbuF/1Nrq3OpiOKhw7mWqObWKCde+sOJcrgnVlw+EdzizgUMQi yzdSzBT0rNXkoFfcoGMpNUyFa11roD+IvLCh6uVBqGOEJwAu3xfl7vAEpUJ4EU/ikx VjivjpQXOs2lbI8A+sffdR8AbBCmC7ixpCx5SoA4fciN21UNzowdVUboLewc8lueRy n5EvfsdZ9WVsw== 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 B65781002A3; Mon, 18 Dec 2023 09:07:00 +0000 (GMT) From: Sean Young To: linux-media@vger.kernel.org, linux-pwm@vger.kernel.org, Ivaylo Dimitrov , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Jonathan Corbet Cc: Sean Young , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v9 4/6] pwm: Make it possible to apply PWM changes in atomic context Date: Mon, 18 Dec 2023 09:06:45 +0000 Message-ID: <2a08b7876059f30c5c081d02978876022fa8d3ea.1702890244.git.sean@mess.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Some PWM devices require sleeping, for example if the pwm device is connected over I2C. However, many PWM devices could be used from atomic context, e.g. memory mapped PWM. This is useful for, for example, the pwm-ir-tx driver which requires precise timing. Sleeping causes havoc with the generated IR signal. Since not all PWM devices can support atomic context, we also add a pwm_might_sleep() function to check if is not supported. Signed-off-by: Sean Young --- Documentation/driver-api/pwm.rst | 9 +++++ MAINTAINERS | 2 +- drivers/pwm/core.c | 62 ++++++++++++++++++++++++++------ include/linux/pwm.h | 25 +++++++++++++ 4 files changed, 86 insertions(+), 12 deletions(-) diff --git a/Documentation/driver-api/pwm.rst b/Documentation/driver-api/pwm.rst index f1d8197c8c43..3c28ccc4b611 100644 --- a/Documentation/driver-api/pwm.rst +++ b/Documentation/driver-api/pwm.rst @@ -46,6 +46,15 @@ After being requested, a PWM has to be configured using:: This API controls both the PWM period/duty_cycle config and the enable/disable state. +PWM devices can be used from atomic context, if the PWM does not sleep. You +can check if this the case with:: + + bool pwm_might_sleep(struct pwm_device *pwm); + +If false, the PWM can also be configured from atomic context with:: + + int pwm_apply_atomic(struct pwm_device *pwm, struct pwm_state *state); + As a consumer, don't rely on the output's state for a disabled PWM. If it's easily possible, drivers are supposed to emit the inactive state, but some drivers cannot. If you rely on getting the inactive state, use .duty_cycle=0, diff --git a/MAINTAINERS b/MAINTAINERS index c58480595220..5342cf32d73f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -17576,7 +17576,7 @@ F: drivers/video/backlight/pwm_bl.c F: include/dt-bindings/pwm/ F: include/linux/pwm.h F: include/linux/pwm_backlight.h -K: pwm_(config|apply_might_sleep|ops) +K: pwm_(config|apply_might_sleep|apply_atomic|ops) PXA GPIO DRIVER M: Robert Jarzmik diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c index c2d78136625d..39bfc8b52294 100644 --- a/drivers/pwm/core.c +++ b/drivers/pwm/core.c @@ -433,24 +433,15 @@ static void pwm_apply_debug(struct pwm_device *pwm, } /** - * pwm_apply_might_sleep() - atomically apply a new state to a PWM device + * __pwm_apply() - atomically apply a new state to a PWM device * @pwm: PWM device * @state: new state to apply */ -int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) +static int __pwm_apply(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_might_sleep() 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; @@ -479,8 +470,57 @@ int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) return 0; } + +/** + * pwm_apply_might_sleep() - 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_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) +{ + 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_might_sleep() 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->atomic) { + /* + * Catch any drivers that have been marked as atomic but + * that will sleep anyway. + */ + non_block_start(); + err = __pwm_apply(pwm, state); + non_block_end(); + } else { + err = __pwm_apply(pwm, state); + } + + return err; +} EXPORT_SYMBOL_GPL(pwm_apply_might_sleep); +/** + * pwm_apply_atomic() - apply a new state to a PWM device from atomic context + * Not all PWM devices support this function, check with pwm_might_sleep(). + * @pwm: PWM device + * @state: new state to apply + */ +int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state) +{ + WARN_ONCE(!pwm->chip->atomic, + "sleeping PWM driver used in atomic context\n"); + + return __pwm_apply(pwm, state); +} +EXPORT_SYMBOL_GPL(pwm_apply_atomic); + /** * pwm_capture() - capture and report a PWM signal * @pwm: PWM device diff --git a/include/linux/pwm.h b/include/linux/pwm.h index c9cb87b59ac8..495af3627939 100644 --- a/include/linux/pwm.h +++ b/include/linux/pwm.h @@ -285,6 +285,7 @@ struct pwm_ops { * @npwm: number of PWMs controlled by this chip * @of_xlate: request a PWM device given a device tree PWM specifier * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier + * @atomic: can the driver's ->apply() be called in atomic context * @pwms: array of PWM devices allocated by the framework */ struct pwm_chip { @@ -297,6 +298,7 @@ struct pwm_chip { struct pwm_device * (*of_xlate)(struct pwm_chip *chip, const struct of_phandle_args *args); unsigned int of_pwm_n_cells; + bool atomic; /* only used internally by the PWM framework */ struct pwm_device *pwms; @@ -305,6 +307,7 @@ struct pwm_chip { #if IS_ENABLED(CONFIG_PWM) /* PWM user APIs */ int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state); +int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state); int pwm_adjust_config(struct pwm_device *pwm); /** @@ -375,6 +378,17 @@ static inline void pwm_disable(struct pwm_device *pwm) pwm_apply_might_sleep(pwm, &state); } +/** + * pwm_might_sleep() - is pwm_apply_atomic() supported? + * @pwm: PWM device + * + * Returns: false if pwm_apply_atomic() can be called from atomic context. + */ +static inline bool pwm_might_sleep(struct pwm_device *pwm) +{ + return !pwm->chip->atomic; +} + /* PWM provider APIs */ int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, unsigned long timeout); @@ -403,6 +417,11 @@ struct pwm_device *devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, const char *con_id); #else +static inline bool pwm_might_sleep(struct pwm_device *pwm) +{ + return true; +} + static inline int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) { @@ -410,6 +429,12 @@ static inline int pwm_apply_might_sleep(struct pwm_device *pwm, return -EOPNOTSUPP; } +static inline int pwm_apply_atomic(struct pwm_device *pwm, + const struct pwm_state *state) +{ + return -EOPNOTSUPP; +} + static inline int pwm_adjust_config(struct pwm_device *pwm) { return -EOPNOTSUPP; From patchwork Mon Dec 18 09:06:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Young X-Patchwork-Id: 755805 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 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" 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_Kleine-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-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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;