pwm-stm32: Make use of parametrised register definitions
authorUwe Kleine-König <u.kleine-koenig@baylibre.com>
Wed, 19 Jun 2024 10:11:46 +0000 (12:11 +0200)
committerUwe Kleine-König <ukleinek@kernel.org>
Wed, 10 Jul 2024 15:53:51 +0000 (17:53 +0200)
There is no semantic change, but it is a nicer on the eyes of a reader,
because

TIM_CCR1 + 4 * ch

encodes internal register knowledge, while

TIM_CCRx(ch + 1)

keeps that information completely in the header defining the registers.

While I expected this to not result in any changes in the binary, gcc 13
(as provided by Debian in the gcc-13-arm-linux-gnueabihf 13.2.0-12cross1
package) compiles the new version with an allmodconfig to more compact
code:

$ source/scripts/bloat-o-meter drivers/pwm/pwm-stm32.o-pre drivers/pwm/pwm-stm32.o
add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-488 (-488)
Function                                     old     new   delta
stm32_pwm_get_state                          968     936     -32
stm32_pwm_apply_locked                      1920    1464    -456

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Link: https://lore.kernel.org/r/d7ef7a6158df4ba6687233b0e00d37796b069fb3.1718791090.git.u.kleine-koenig@baylibre.com
Signed-off-by: Uwe Kleine-König <ukleinek@kernel.org>
drivers/pwm/pwm-stm32.c

index c586029caf2337ee24610d3bc2538ed5ee958a7d..fd754a99cf2ed1182a6b5c52c7e4712eae5a7f29 100644 (file)
@@ -368,7 +368,7 @@ static int stm32_pwm_config(struct stm32_pwm *priv, unsigned int ch,
        dty = mul_u64_u64_div_u64(duty_ns, clk_get_rate(priv->clk),
                                  (u64)NSEC_PER_SEC * (prescaler + 1));
 
-       regmap_write(priv->regmap, TIM_CCR1 + 4 * ch, dty);
+       regmap_write(priv->regmap, TIM_CCRx(ch + 1), dty);
 
        /* Configure output mode */
        shift = (ch & 0x1) * CCMR_CHANNEL_SHIFT;
@@ -390,9 +390,9 @@ static int stm32_pwm_set_polarity(struct stm32_pwm *priv, unsigned int ch,
 {
        u32 mask;
 
-       mask = TIM_CCER_CC1P << (ch * 4);
+       mask = TIM_CCER_CCxP(ch + 1);
        if (priv->have_complementary_output)
-               mask |= TIM_CCER_CC1NP << (ch * 4);
+               mask |= TIM_CCER_CCxNP(ch + 1);
 
        regmap_update_bits(priv->regmap, TIM_CCER, mask,
                           polarity == PWM_POLARITY_NORMAL ? 0 : mask);
@@ -410,9 +410,9 @@ static int stm32_pwm_enable(struct stm32_pwm *priv, unsigned int ch)
                return ret;
 
        /* Enable channel */
-       mask = TIM_CCER_CC1E << (ch * 4);
+       mask = TIM_CCER_CCxE(ch + 1);
        if (priv->have_complementary_output)
-               mask |= TIM_CCER_CC1NE << (ch * 4);
+               mask |= TIM_CCER_CCxNE(ch);
 
        regmap_set_bits(priv->regmap, TIM_CCER, mask);
 
@@ -430,9 +430,9 @@ static void stm32_pwm_disable(struct stm32_pwm *priv, unsigned int ch)
        u32 mask;
 
        /* Disable channel */
-       mask = TIM_CCER_CC1E << (ch * 4);
+       mask = TIM_CCER_CCxE(ch + 1);
        if (priv->have_complementary_output)
-               mask |= TIM_CCER_CC1NE << (ch * 4);
+               mask |= TIM_CCER_CCxNE(ch + 1);
 
        regmap_clear_bits(priv->regmap, TIM_CCER, mask);
 
@@ -502,8 +502,8 @@ static int stm32_pwm_get_state(struct pwm_chip *chip,
        if (ret)
                goto out;
 
-       state->enabled = ccer & (TIM_CCER_CC1E << (ch * 4));
-       state->polarity = (ccer & (TIM_CCER_CC1P << (ch * 4))) ?
+       state->enabled = ccer & TIM_CCER_CCxE(ch + 1);
+       state->polarity = (ccer & TIM_CCER_CCxP(ch + 1)) ?
                          PWM_POLARITY_INVERSED : PWM_POLARITY_NORMAL;
        ret = regmap_read(priv->regmap, TIM_PSC, &psc);
        if (ret)
@@ -511,7 +511,7 @@ static int stm32_pwm_get_state(struct pwm_chip *chip,
        ret = regmap_read(priv->regmap, TIM_ARR, &arr);
        if (ret)
                goto out;
-       ret = regmap_read(priv->regmap, TIM_CCR1 + 4 * ch, &ccr);
+       ret = regmap_read(priv->regmap, TIM_CCRx(ch + 1), &ccr);
        if (ret)
                goto out;
 
@@ -712,7 +712,7 @@ static int stm32_pwm_suspend(struct device *dev)
        ccer = active_channels(priv);
 
        for (i = 0; i < chip->npwm; i++) {
-               mask = TIM_CCER_CC1E << (i * 4);
+               mask = TIM_CCER_CCxE(i + 1);
                if (ccer & mask) {
                        dev_err(dev, "PWM %u still in use by consumer %s\n",
                                i, chip->pwms[i].label);