* [PATCH] input: misc: max8997: Simplify open coding of a division using up to 64 divisions
@ 2021-03-16 21:18 Uwe Kleine-König
2021-03-16 21:22 ` [PATCH v2] " Uwe Kleine-König
0 siblings, 1 reply; 3+ messages in thread
From: Uwe Kleine-König @ 2021-03-16 21:18 UTC (permalink / raw)
To: Dmitry Torokhov; +Cc: linux-input
The for loop is just a complicate way to express a division. Replace it
by the actual division which is both simpler to understand for a human
and more efficient for a CPU to calculate.
Signed-off-by: Uwe Kleine-König <uwe@kleine-koenig.org>
---
drivers/input/misc/max8997_haptic.c | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/drivers/input/misc/max8997_haptic.c b/drivers/input/misc/max8997_haptic.c
index c86966ea0f16..6baca94ed424 100644
--- a/drivers/input/misc/max8997_haptic.c
+++ b/drivers/input/misc/max8997_haptic.c
@@ -68,12 +68,8 @@ static int max8997_haptic_set_duty_cycle(struct max8997_haptic *chip)
int i;
u8 duty_index = 0;
- for (i = 0; i <= 64; i++) {
- if (chip->level <= i * 100 / 64) {
- duty_index = i;
- break;
- }
- }
+ duty_index = DIV_ROUNDUP(chip->level * 64, 100);
+
switch (chip->internal_mode_pattern) {
case 0:
max8997_write_reg(chip->client,
--
2.30.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2] input: misc: max8997: Simplify open coding of a division using up to 64 divisions
2021-03-16 21:18 [PATCH] input: misc: max8997: Simplify open coding of a division using up to 64 divisions Uwe Kleine-König
@ 2021-03-16 21:22 ` Uwe Kleine-König
2021-03-23 18:40 ` Dmitry Torokhov
0 siblings, 1 reply; 3+ messages in thread
From: Uwe Kleine-König @ 2021-03-16 21:22 UTC (permalink / raw)
To: Dmitry Torokhov; +Cc: linux-input
The for loop is just a complicate way to express a division. Replace it
by the actual division which is both simpler to understand for a human
and more efficient for a CPU to calculate.
Signed-off-by: Uwe Kleine-König <uwe@kleine-koenig.org>
---
Changes since (implicit) v1:
- drop now unused variable i
- s/DIV_ROUNDUP/DIV_ROUND_UP/ to make the compiler happy.
Nota bene: Better do the compile test before calling git send-email :-)
Best regards
Uwe
drivers/input/misc/max8997_haptic.c | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/drivers/input/misc/max8997_haptic.c b/drivers/input/misc/max8997_haptic.c
index c86966ea0f16..ad82837c7ad5 100644
--- a/drivers/input/misc/max8997_haptic.c
+++ b/drivers/input/misc/max8997_haptic.c
@@ -65,15 +65,10 @@ static int max8997_haptic_set_duty_cycle(struct max8997_haptic *chip)
pwm_set_relative_duty_cycle(&state, chip->level, 100);
ret = pwm_apply_state(chip->pwm, &state);
} else {
- int i;
u8 duty_index = 0;
- for (i = 0; i <= 64; i++) {
- if (chip->level <= i * 100 / 64) {
- duty_index = i;
- break;
- }
- }
+ duty_index = DIV_ROUND_UP(chip->level * 64, 100);
+
switch (chip->internal_mode_pattern) {
case 0:
max8997_write_reg(chip->client,
--
2.30.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] input: misc: max8997: Simplify open coding of a division using up to 64 divisions
2021-03-16 21:22 ` [PATCH v2] " Uwe Kleine-König
@ 2021-03-23 18:40 ` Dmitry Torokhov
0 siblings, 0 replies; 3+ messages in thread
From: Dmitry Torokhov @ 2021-03-23 18:40 UTC (permalink / raw)
To: Uwe Kleine-König; +Cc: linux-input
On Tue, Mar 16, 2021 at 10:22:33PM +0100, Uwe Kleine-König wrote:
> The for loop is just a complicate way to express a division. Replace it
> by the actual division which is both simpler to understand for a human
> and more efficient for a CPU to calculate.
>
> Signed-off-by: Uwe Kleine-König <uwe@kleine-koenig.org>
Applied, thank you.
--
Dmitry
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-03-23 18:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-16 21:18 [PATCH] input: misc: max8997: Simplify open coding of a division using up to 64 divisions Uwe Kleine-König
2021-03-16 21:22 ` [PATCH v2] " Uwe Kleine-König
2021-03-23 18:40 ` Dmitry Torokhov
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).