clk: at91: clk-generated: make gclk determine audio_pll rate

This allows gclk to determine audio_pll rate and set the parent rate
accordingly.

However, there are multiple children clocks that could technically
change the rate of audio_pll (via gck). With the rate locking, the first
consumer to enable the clock will be the one definitely setting the rate
of the clock.

Since audio IPs are most likely to request the same rate, we enforce
that the only clks able to modify gck rate are those of audio IPs.

To remain consistent, we deny other clocks to be children of audio_pll.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Boris Brezillon <boris.brezillon@free-electrons.com>
Acked-by: Nicolas Ferre <nicolas.ferre@microchip.com>
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>

authored by

Quentin Schulz and committed by
Stephen Boyd
1a1a36d7 8a8f4bf0

+57 -6
+57 -6
drivers/clk/at91/clk-generated.c
··· 26 26 #define GENERATED_SOURCE_MAX 6 27 27 #define GENERATED_MAX_DIV 255 28 28 29 + #define GCK_ID_SSC0 43 30 + #define GCK_ID_SSC1 44 31 + #define GCK_ID_I2S0 54 32 + #define GCK_ID_I2S1 55 33 + #define GCK_ID_CLASSD 59 34 + #define GCK_INDEX_DT_AUDIO_PLL 5 35 + 29 36 struct clk_generated { 30 37 struct clk_hw hw; 31 38 struct regmap *regmap; ··· 41 34 u32 id; 42 35 u32 gckdiv; 43 36 u8 parent_id; 37 + bool audio_pll_allowed; 44 38 }; 45 39 46 40 #define to_clk_generated(hw) \ ··· 134 126 { 135 127 struct clk_generated *gck = to_clk_generated(hw); 136 128 struct clk_hw *parent = NULL; 129 + struct clk_rate_request req_parent = *req; 137 130 long best_rate = -EINVAL; 138 - unsigned long min_rate; 131 + unsigned long min_rate, parent_rate; 139 132 int best_diff = -1; 140 133 int i; 134 + u32 div; 141 135 142 - for (i = 0; i < clk_hw_get_num_parents(hw); i++) { 143 - u32 div; 144 - unsigned long parent_rate; 145 - 136 + for (i = 0; i < clk_hw_get_num_parents(hw) - 1; i++) { 146 137 parent = clk_hw_get_parent_by_index(hw, i); 147 138 if (!parent) 148 139 continue; ··· 157 150 clk_generated_best_diff(req, parent, parent_rate, div, 158 151 &best_diff, &best_rate); 159 152 153 + if (!best_diff) 154 + break; 155 + } 156 + 157 + /* 158 + * The audio_pll rate can be modified, unlike the five others clocks 159 + * that should never be altered. 160 + * The audio_pll can technically be used by multiple consumers. However, 161 + * with the rate locking, the first consumer to enable to clock will be 162 + * the one definitely setting the rate of the clock. 163 + * Since audio IPs are most likely to request the same rate, we enforce 164 + * that the only clks able to modify gck rate are those of audio IPs. 165 + */ 166 + 167 + if (!gck->audio_pll_allowed) 168 + goto end; 169 + 170 + parent = clk_hw_get_parent_by_index(hw, GCK_INDEX_DT_AUDIO_PLL); 171 + if (!parent) 172 + goto end; 173 + 174 + for (div = 1; div < GENERATED_MAX_DIV + 2; div++) { 175 + req_parent.rate = req->rate * div; 176 + __clk_determine_rate(parent, &req_parent); 177 + clk_generated_best_diff(req, parent, req_parent.rate, div, 178 + &best_diff, &best_rate); 160 179 161 180 if (!best_diff) 162 181 break; 163 182 } 164 183 184 + end: 165 185 pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n", 166 186 __func__, best_rate, 167 187 __clk_get_name((req->best_parent_hw)->clk), ··· 298 264 init.ops = &generated_ops; 299 265 init.parent_names = parent_names; 300 266 init.num_parents = num_parents; 301 - init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE; 267 + init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE | 268 + CLK_SET_RATE_PARENT; 302 269 303 270 gck->id = id; 304 271 gck->hw.init = &init; ··· 331 296 struct device_node *gcknp; 332 297 struct clk_range range = CLK_RANGE(0, 0); 333 298 struct regmap *regmap; 299 + struct clk_generated *gck; 334 300 335 301 num_parents = of_clk_get_parent_count(np); 336 302 if (num_parents == 0 || num_parents > GENERATED_SOURCE_MAX) ··· 363 327 hw = at91_clk_register_generated(regmap, &pmc_pcr_lock, name, 364 328 parent_names, num_parents, 365 329 id, &range); 330 + 331 + gck = to_clk_generated(hw); 332 + 333 + if (of_device_is_compatible(np, 334 + "atmel,sama5d2-clk-generated")) { 335 + if (gck->id == GCK_ID_SSC0 || gck->id == GCK_ID_SSC1 || 336 + gck->id == GCK_ID_I2S0 || gck->id == GCK_ID_I2S1 || 337 + gck->id == GCK_ID_CLASSD) 338 + gck->audio_pll_allowed = true; 339 + else 340 + gck->audio_pll_allowed = false; 341 + } else { 342 + gck->audio_pll_allowed = false; 343 + } 344 + 366 345 if (IS_ERR(hw)) 367 346 continue; 368 347