[PATCH v16 6/9] clk: realtek: Add support for MMC-tuned PLL clocks

Brian Masney masneyb at kernel.org
Wed Nov 18 18:23:54 PST 2026


Hi Yu-Chun,

On Fri, Nov 13, 2026 at 02:12:59PM +0000, Yu-Chun Lin [林祐君] wrote:
> > > +static const struct rtk_clk_pll_mmc_rate *
> > > +rtk_clk_pll_mmc_find_closest_rate(const struct rtk_clk_pll_mmc_rate *tbl,
> > u32 num,
> > > +                               unsigned long rate, unsigned long
> > min_rate,
> > > +                               unsigned long max_rate) {
> > > +     const struct rtk_clk_pll_mmc_rate *best = NULL;
> > > +     unsigned long best_delta = ~0UL;
> > > +
> > > +     for (unsigned int i = 0; i < num; i++) {
> > > +             const struct rtk_clk_pll_mmc_rate *entry = &tbl[i];
> > > +             unsigned long delta;
> > > +
> > > +             if (entry->rate < min_rate || entry->rate > max_rate)
> > > +                     continue;
> > > +
> > > +             delta = entry->rate >= rate ? entry->rate - rate : rate -
> > entry->rate;
> > > +             if (delta < best_delta) {
> > > +                     best = entry;
> > > +                     best_delta = delta;
> > > +             }
> > > +     }
> > > +
> > > +     return best;
> > > +}
> > 
> > This duplicates the functionality in ftbl_find_closest_by_rate() from patch 3.
> > 
> 
> I considered sharing the logic, but the two tables have different entry
> layouts.
> 
> Unifying them would need a macro or (void *) (both lose type checking), or
> embedding rtk_freq_table with container_of() (a larger rework for little
> gain). I prefer keeping the two type-safe functions. Is that acceptable?

What about this? This is completely untested.

  #define ftbl_find_closest_by_rate(_tbl, _rate, _min, _max) ({         \
        typeof(&(_tbl)[0]) __best = NULL, __e;                          \
        unsigned long __r = (_rate), __lo = (_min), __hi = (_max);      \
        unsigned long __best_delta = ULONG_MAX;                         \
                                                                        \
        for (__e = (_tbl); __e->rate; __e++) {                          \
                unsigned long __d;                                      \
                                                                        \
                if (__e->rate < __lo || __e->rate > __hi)               \
                        continue;                                       \
                                                                        \
                __d = abs_diff(__e->rate, __r);                         \
                if (__d < __best_delta) {                               \
                        __best_delta = __d;                             \
                        __best = __e;                                   \
                }                                                       \
        }                                                               \
        __best;                                                         \ 
  })

Brian



More information about the linux-arm-kernel mailing list