linux-pwm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] meson pwm small fixes
@ 2024-04-25 17:12 George Stark
  2024-04-25 17:12 ` [PATCH v3 1/3] pwm: meson: Drop unneeded check in .get_state() George Stark
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: George Stark @ 2024-04-25 17:12 UTC (permalink / raw)
  To: u.kleine-koenig, neil.armstrong, khilman, jbrunet,
	martin.blumenstingl, thierry.reding, hkallweit1
  Cc: linux-pwm, linux-amlogic, linux-arm-kernel, linux-kernel, kernel,
	George Stark

Just some small fixes for meson pwm.

Changelog:
v1->v2:
  pwm: meson: Drop unneeded check in .get_state()
    - update commit message
    - drop Fixes tag. It's not actually a bug
    previous version: [1]

  pwm: meson: Add check for error from clk_round_rate()
    - update commit message
    - change unsigned long fin_freq; to long fin_freq;
    previous version: [2]

v2->v3:
  pwm: meson: Add check for error from clk_round_rate()
    - change format modifier in dev_dbg() for fin_freq var to %lu
    previous version: [3]

  pwm: meson: Use mul_u64_u64_div_u64() for frequency calculating
    new patch

[1] https://lore.kernel.org/lkml/20240423161356.2522636-2-gnstark@salutedevices.com/T/#m6d1d5cf100dc5b77f85e2a7d01c0b0097b6a04da
[2] https://lore.kernel.org/lkml/20240423161356.2522636-2-gnstark@salutedevices.com/T/#ma84e2d490bf79bda12f8393773c1af37b48d5473
[3] https://lore.kernel.org/linux-arm-kernel/20240424132408.2565916-1-gnstark@salutedevices.com/T/#m52b7e5a34c3e8907235d73c2827b92cb13c93dd3

George Stark (3):
  pwm: meson: Drop unneeded check in .get_state()
  pwm: meson: Add check for error from clk_round_rate()
  pwm: meson: Use mul_u64_u64_div_u64() for frequency calculating

 drivers/pwm/pwm-meson.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

--
2.25.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 1/3] pwm: meson: Drop unneeded check in .get_state()
  2024-04-25 17:12 [PATCH v3 0/3] meson pwm small fixes George Stark
@ 2024-04-25 17:12 ` George Stark
  2024-04-25 17:12 ` [PATCH v3 2/3] pwm: meson: Add check for error from clk_round_rate() George Stark
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: George Stark @ 2024-04-25 17:12 UTC (permalink / raw)
  To: u.kleine-koenig, neil.armstrong, khilman, jbrunet,
	martin.blumenstingl, thierry.reding, hkallweit1
  Cc: linux-pwm, linux-amlogic, linux-arm-kernel, linux-kernel, kernel,
	George Stark, Dmitry Rokosov

Drop checking state argument for NULL pointer in meson_pwm_get_state()
due to it is called only from pwm core with always valid arguments.

Signed-off-by: Dmitry Rokosov <ddrokosov@salutedevices.com>
Signed-off-by: George Stark <gnstark@salutedevices.com>
---
 drivers/pwm/pwm-meson.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c
index ea96c5973488..f4d70da621ec 100644
--- a/drivers/pwm/pwm-meson.c
+++ b/drivers/pwm/pwm-meson.c
@@ -311,9 +311,6 @@ static int meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	struct meson_pwm_channel *channel;
 	u32 value;
 
-	if (!state)
-		return 0;
-
 	channel = &meson->channels[pwm->hwpwm];
 	channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 2/3] pwm: meson: Add check for error from clk_round_rate()
  2024-04-25 17:12 [PATCH v3 0/3] meson pwm small fixes George Stark
  2024-04-25 17:12 ` [PATCH v3 1/3] pwm: meson: Drop unneeded check in .get_state() George Stark
@ 2024-04-25 17:12 ` George Stark
  2024-04-25 17:12 ` [PATCH v3 3/3] pwm: meson: Use mul_u64_u64_div_u64() for frequency calculating George Stark
  2024-04-28 20:55 ` [PATCH v3 0/3] meson pwm small fixes Uwe Kleine-König
  3 siblings, 0 replies; 5+ messages in thread
From: George Stark @ 2024-04-25 17:12 UTC (permalink / raw)
  To: u.kleine-koenig, neil.armstrong, khilman, jbrunet,
	martin.blumenstingl, thierry.reding, hkallweit1
  Cc: linux-pwm, linux-amlogic, linux-arm-kernel, linux-kernel, kernel,
	George Stark, Dmitry Rokosov

clk_round_rate() can return not only zero if requested frequency can not
be provided but also negative error code so add check for it too.

Also change type of variable holding clk_round_rate() result from
unsigned long to long. It's safe due to clk_round_rate() returns long.

Fixes: 329db102a26d ("pwm: meson: make full use of common clock framework")
Signed-off-by: Dmitry Rokosov <ddrokosov@salutedevices.com>
Signed-off-by: George Stark <gnstark@salutedevices.com>
---
 drivers/pwm/pwm-meson.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c
index f4d70da621ec..4a652d500dfc 100644
--- a/drivers/pwm/pwm-meson.c
+++ b/drivers/pwm/pwm-meson.c
@@ -148,7 +148,7 @@ static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm,
 	struct meson_pwm *meson = to_meson_pwm(chip);
 	struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
 	unsigned int cnt, duty_cnt;
-	unsigned long fin_freq;
+	long fin_freq;
 	u64 duty, period, freq;
 
 	duty = state->duty_cycle;
@@ -168,12 +168,13 @@ static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm,
 		freq = ULONG_MAX;
 
 	fin_freq = clk_round_rate(channel->clk, freq);
-	if (fin_freq == 0) {
-		dev_err(pwmchip_parent(chip), "invalid source clock frequency\n");
-		return -EINVAL;
+	if (fin_freq <= 0) {
+		dev_err(pwmchip_parent(chip),
+			"invalid source clock frequency %llu\n", freq);
+		return fin_freq ? fin_freq : -EINVAL;
 	}
 
-	dev_dbg(pwmchip_parent(chip), "fin_freq: %lu Hz\n", fin_freq);
+	dev_dbg(pwmchip_parent(chip), "fin_freq: %ld Hz\n", fin_freq);
 
 	cnt = div_u64(fin_freq * period, NSEC_PER_SEC);
 	if (cnt > 0xffff) {
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 3/3] pwm: meson: Use mul_u64_u64_div_u64() for frequency calculating
  2024-04-25 17:12 [PATCH v3 0/3] meson pwm small fixes George Stark
  2024-04-25 17:12 ` [PATCH v3 1/3] pwm: meson: Drop unneeded check in .get_state() George Stark
  2024-04-25 17:12 ` [PATCH v3 2/3] pwm: meson: Add check for error from clk_round_rate() George Stark
@ 2024-04-25 17:12 ` George Stark
  2024-04-28 20:55 ` [PATCH v3 0/3] meson pwm small fixes Uwe Kleine-König
  3 siblings, 0 replies; 5+ messages in thread
From: George Stark @ 2024-04-25 17:12 UTC (permalink / raw)
  To: u.kleine-koenig, neil.armstrong, khilman, jbrunet,
	martin.blumenstingl, thierry.reding, hkallweit1
  Cc: linux-pwm, linux-amlogic, linux-arm-kernel, linux-kernel, kernel,
	George Stark

While calculating frequency for the given period u64 numbers are
multiplied before division what can lead to overflow in theory so use
secure mul_u64_u64_div_u64() which handles overflow correctly.

Fixes: 329db102a26d ("pwm: meson: make full use of common clock framework")
Suggested-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: George Stark <gnstark@salutedevices.com>
---
 drivers/pwm/pwm-meson.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c
index 4a652d500dfc..b2f97dfb01bb 100644
--- a/drivers/pwm/pwm-meson.c
+++ b/drivers/pwm/pwm-meson.c
@@ -176,7 +176,7 @@ static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm,
 
 	dev_dbg(pwmchip_parent(chip), "fin_freq: %ld Hz\n", fin_freq);
 
-	cnt = div_u64(fin_freq * period, NSEC_PER_SEC);
+	cnt = mul_u64_u64_div_u64(fin_freq, period, NSEC_PER_SEC);
 	if (cnt > 0xffff) {
 		dev_err(pwmchip_parent(chip), "unable to get period cnt\n");
 		return -EINVAL;
@@ -191,7 +191,7 @@ static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm,
 		channel->hi = 0;
 		channel->lo = cnt;
 	} else {
-		duty_cnt = div_u64(fin_freq * duty, NSEC_PER_SEC);
+		duty_cnt = mul_u64_u64_div_u64(fin_freq, duty, NSEC_PER_SEC);
 
 		dev_dbg(pwmchip_parent(chip), "duty=%llu duty_cnt=%u\n", duty, duty_cnt);
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 0/3] meson pwm small fixes
  2024-04-25 17:12 [PATCH v3 0/3] meson pwm small fixes George Stark
                   ` (2 preceding siblings ...)
  2024-04-25 17:12 ` [PATCH v3 3/3] pwm: meson: Use mul_u64_u64_div_u64() for frequency calculating George Stark
@ 2024-04-28 20:55 ` Uwe Kleine-König
  3 siblings, 0 replies; 5+ messages in thread
From: Uwe Kleine-König @ 2024-04-28 20:55 UTC (permalink / raw)
  To: George Stark
  Cc: neil.armstrong, khilman, jbrunet, martin.blumenstingl,
	thierry.reding, hkallweit1, linux-pwm, linux-amlogic,
	linux-arm-kernel, linux-kernel, kernel

[-- Attachment #1: Type: text/plain, Size: 396 bytes --]

Hello,

On Thu, Apr 25, 2024 at 08:12:50PM +0300, George Stark wrote:
> Just some small fixes for meson pwm.

Applied the series to
https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next
.

Thanks
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-04-28 20:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-25 17:12 [PATCH v3 0/3] meson pwm small fixes George Stark
2024-04-25 17:12 ` [PATCH v3 1/3] pwm: meson: Drop unneeded check in .get_state() George Stark
2024-04-25 17:12 ` [PATCH v3 2/3] pwm: meson: Add check for error from clk_round_rate() George Stark
2024-04-25 17:12 ` [PATCH v3 3/3] pwm: meson: Use mul_u64_u64_div_u64() for frequency calculating George Stark
2024-04-28 20:55 ` [PATCH v3 0/3] meson pwm small fixes Uwe Kleine-König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).