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 #define GENERATED_SOURCE_MAX 6 27 #define GENERATED_MAX_DIV 255 28 29 struct clk_generated { 30 struct clk_hw hw; 31 struct regmap *regmap; ··· 41 u32 id; 42 u32 gckdiv; 43 u8 parent_id; 44 }; 45 46 #define to_clk_generated(hw) \ ··· 134 { 135 struct clk_generated *gck = to_clk_generated(hw); 136 struct clk_hw *parent = NULL; 137 long best_rate = -EINVAL; 138 - unsigned long min_rate; 139 int best_diff = -1; 140 int i; 141 142 - for (i = 0; i < clk_hw_get_num_parents(hw); i++) { 143 - u32 div; 144 - unsigned long parent_rate; 145 - 146 parent = clk_hw_get_parent_by_index(hw, i); 147 if (!parent) 148 continue; ··· 157 clk_generated_best_diff(req, parent, parent_rate, div, 158 &best_diff, &best_rate); 159 160 161 if (!best_diff) 162 break; 163 } 164 165 pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n", 166 __func__, best_rate, 167 __clk_get_name((req->best_parent_hw)->clk), ··· 298 init.ops = &generated_ops; 299 init.parent_names = parent_names; 300 init.num_parents = num_parents; 301 - init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE; 302 303 gck->id = id; 304 gck->hw.init = &init; ··· 331 struct device_node *gcknp; 332 struct clk_range range = CLK_RANGE(0, 0); 333 struct regmap *regmap; 334 335 num_parents = of_clk_get_parent_count(np); 336 if (num_parents == 0 || num_parents > GENERATED_SOURCE_MAX) ··· 363 hw = at91_clk_register_generated(regmap, &pmc_pcr_lock, name, 364 parent_names, num_parents, 365 id, &range); 366 if (IS_ERR(hw)) 367 continue; 368
··· 26 #define GENERATED_SOURCE_MAX 6 27 #define GENERATED_MAX_DIV 255 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 + 36 struct clk_generated { 37 struct clk_hw hw; 38 struct regmap *regmap; ··· 34 u32 id; 35 u32 gckdiv; 36 u8 parent_id; 37 + bool audio_pll_allowed; 38 }; 39 40 #define to_clk_generated(hw) \ ··· 126 { 127 struct clk_generated *gck = to_clk_generated(hw); 128 struct clk_hw *parent = NULL; 129 + struct clk_rate_request req_parent = *req; 130 long best_rate = -EINVAL; 131 + unsigned long min_rate, parent_rate; 132 int best_diff = -1; 133 int i; 134 + u32 div; 135 136 + for (i = 0; i < clk_hw_get_num_parents(hw) - 1; i++) { 137 parent = clk_hw_get_parent_by_index(hw, i); 138 if (!parent) 139 continue; ··· 150 clk_generated_best_diff(req, parent, parent_rate, div, 151 &best_diff, &best_rate); 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); 179 180 if (!best_diff) 181 break; 182 } 183 184 + end: 185 pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n", 186 __func__, best_rate, 187 __clk_get_name((req->best_parent_hw)->clk), ··· 264 init.ops = &generated_ops; 265 init.parent_names = parent_names; 266 init.num_parents = num_parents; 267 + init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE | 268 + CLK_SET_RATE_PARENT; 269 270 gck->id = id; 271 gck->hw.init = &init; ··· 296 struct device_node *gcknp; 297 struct clk_range range = CLK_RANGE(0, 0); 298 struct regmap *regmap; 299 + struct clk_generated *gck; 300 301 num_parents = of_clk_get_parent_count(np); 302 if (num_parents == 0 || num_parents > GENERATED_SOURCE_MAX) ··· 327 hw = at91_clk_register_generated(regmap, &pmc_pcr_lock, name, 328 parent_names, num_parents, 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 + 345 if (IS_ERR(hw)) 346 continue; 347