Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pwm: Fix period and polarity in pwm_get() for non-perfect matches

If pwm_get() finds a look-up entry with a perfect match (both dev_id and
con_id match), the loop is aborted, and "p" still points to the correct
struct pwm_lookup.

If only an entry with a matching dev_id or con_id is found, the loop
terminates after traversing the whole list, and "p" now points to
arbitrary memory, not part of the pwm_lookup list.
Then pwm_set_period() and pwm_set_polarity() will set random values for
period resp. polarity.

To fix this, save period and polarity when finding a new best match,
just like is done for chip (for the provider) and index.

This fixes the LCD backlight on r8a7740/armadillo-legacy, which was fed
period 0 and polarity -1068821144 instead of 33333 resp. 1.

Fixes: 3796ce1d4d4b ("pwm: add period and polarity to struct pwm_lookup")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: stable@vger.kernel.org
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>

authored by

Geert Uytterhoeven and committed by
Thierry Reding
d717ea73 7d1311b9

+6 -2
+6 -2
drivers/pwm/core.c
··· 606 606 unsigned int best = 0; 607 607 struct pwm_lookup *p; 608 608 unsigned int match; 609 + unsigned int period; 610 + enum pwm_polarity polarity; 609 611 610 612 /* look up via DT first */ 611 613 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) ··· 655 653 if (match > best) { 656 654 chip = pwmchip_find_by_name(p->provider); 657 655 index = p->index; 656 + period = p->period; 657 + polarity = p->polarity; 658 658 659 659 if (match != 3) 660 660 best = match; ··· 672 668 if (IS_ERR(pwm)) 673 669 return pwm; 674 670 675 - pwm_set_period(pwm, p->period); 676 - pwm_set_polarity(pwm, p->polarity); 671 + pwm_set_period(pwm, period); 672 + pwm_set_polarity(pwm, polarity); 677 673 678 674 679 675 return pwm;