18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Atmel Corporation, 48c2ecf20Sopenharmony_ci * Nicolas Ferre <nicolas.ferre@atmel.com> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Based on clk-programmable & clk-peripheral drivers by Boris BREZILLON. 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/bitfield.h> 108c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 118c2ecf20Sopenharmony_ci#include <linux/clkdev.h> 128c2ecf20Sopenharmony_ci#include <linux/clk/at91_pmc.h> 138c2ecf20Sopenharmony_ci#include <linux/of.h> 148c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h> 158c2ecf20Sopenharmony_ci#include <linux/regmap.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include "pmc.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define GENERATED_MAX_DIV 255 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistruct clk_generated { 228c2ecf20Sopenharmony_ci struct clk_hw hw; 238c2ecf20Sopenharmony_ci struct regmap *regmap; 248c2ecf20Sopenharmony_ci struct clk_range range; 258c2ecf20Sopenharmony_ci spinlock_t *lock; 268c2ecf20Sopenharmony_ci u32 *mux_table; 278c2ecf20Sopenharmony_ci u32 id; 288c2ecf20Sopenharmony_ci u32 gckdiv; 298c2ecf20Sopenharmony_ci const struct clk_pcr_layout *layout; 308c2ecf20Sopenharmony_ci u8 parent_id; 318c2ecf20Sopenharmony_ci int chg_pid; 328c2ecf20Sopenharmony_ci}; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#define to_clk_generated(hw) \ 358c2ecf20Sopenharmony_ci container_of(hw, struct clk_generated, hw) 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic int clk_generated_enable(struct clk_hw *hw) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci struct clk_generated *gck = to_clk_generated(hw); 408c2ecf20Sopenharmony_ci unsigned long flags; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n", 438c2ecf20Sopenharmony_ci __func__, gck->gckdiv, gck->parent_id); 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci spin_lock_irqsave(gck->lock, flags); 468c2ecf20Sopenharmony_ci regmap_write(gck->regmap, gck->layout->offset, 478c2ecf20Sopenharmony_ci (gck->id & gck->layout->pid_mask)); 488c2ecf20Sopenharmony_ci regmap_update_bits(gck->regmap, gck->layout->offset, 498c2ecf20Sopenharmony_ci AT91_PMC_PCR_GCKDIV_MASK | gck->layout->gckcss_mask | 508c2ecf20Sopenharmony_ci gck->layout->cmd | AT91_PMC_PCR_GCKEN, 518c2ecf20Sopenharmony_ci field_prep(gck->layout->gckcss_mask, gck->parent_id) | 528c2ecf20Sopenharmony_ci gck->layout->cmd | 538c2ecf20Sopenharmony_ci FIELD_PREP(AT91_PMC_PCR_GCKDIV_MASK, gck->gckdiv) | 548c2ecf20Sopenharmony_ci AT91_PMC_PCR_GCKEN); 558c2ecf20Sopenharmony_ci spin_unlock_irqrestore(gck->lock, flags); 568c2ecf20Sopenharmony_ci return 0; 578c2ecf20Sopenharmony_ci} 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistatic void clk_generated_disable(struct clk_hw *hw) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci struct clk_generated *gck = to_clk_generated(hw); 628c2ecf20Sopenharmony_ci unsigned long flags; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci spin_lock_irqsave(gck->lock, flags); 658c2ecf20Sopenharmony_ci regmap_write(gck->regmap, gck->layout->offset, 668c2ecf20Sopenharmony_ci (gck->id & gck->layout->pid_mask)); 678c2ecf20Sopenharmony_ci regmap_update_bits(gck->regmap, gck->layout->offset, 688c2ecf20Sopenharmony_ci gck->layout->cmd | AT91_PMC_PCR_GCKEN, 698c2ecf20Sopenharmony_ci gck->layout->cmd); 708c2ecf20Sopenharmony_ci spin_unlock_irqrestore(gck->lock, flags); 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic int clk_generated_is_enabled(struct clk_hw *hw) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci struct clk_generated *gck = to_clk_generated(hw); 768c2ecf20Sopenharmony_ci unsigned long flags; 778c2ecf20Sopenharmony_ci unsigned int status; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci spin_lock_irqsave(gck->lock, flags); 808c2ecf20Sopenharmony_ci regmap_write(gck->regmap, gck->layout->offset, 818c2ecf20Sopenharmony_ci (gck->id & gck->layout->pid_mask)); 828c2ecf20Sopenharmony_ci regmap_read(gck->regmap, gck->layout->offset, &status); 838c2ecf20Sopenharmony_ci spin_unlock_irqrestore(gck->lock, flags); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci return !!(status & AT91_PMC_PCR_GCKEN); 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic unsigned long 898c2ecf20Sopenharmony_ciclk_generated_recalc_rate(struct clk_hw *hw, 908c2ecf20Sopenharmony_ci unsigned long parent_rate) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci struct clk_generated *gck = to_clk_generated(hw); 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci return DIV_ROUND_CLOSEST(parent_rate, gck->gckdiv + 1); 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic void clk_generated_best_diff(struct clk_rate_request *req, 988c2ecf20Sopenharmony_ci struct clk_hw *parent, 998c2ecf20Sopenharmony_ci unsigned long parent_rate, u32 div, 1008c2ecf20Sopenharmony_ci int *best_diff, long *best_rate) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci unsigned long tmp_rate; 1038c2ecf20Sopenharmony_ci int tmp_diff; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci if (!div) 1068c2ecf20Sopenharmony_ci tmp_rate = parent_rate; 1078c2ecf20Sopenharmony_ci else 1088c2ecf20Sopenharmony_ci tmp_rate = parent_rate / div; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci if (tmp_rate < req->min_rate || tmp_rate > req->max_rate) 1118c2ecf20Sopenharmony_ci return; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci tmp_diff = abs(req->rate - tmp_rate); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci if (*best_diff < 0 || *best_diff >= tmp_diff) { 1168c2ecf20Sopenharmony_ci *best_rate = tmp_rate; 1178c2ecf20Sopenharmony_ci *best_diff = tmp_diff; 1188c2ecf20Sopenharmony_ci req->best_parent_rate = parent_rate; 1198c2ecf20Sopenharmony_ci req->best_parent_hw = parent; 1208c2ecf20Sopenharmony_ci } 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic int clk_generated_determine_rate(struct clk_hw *hw, 1248c2ecf20Sopenharmony_ci struct clk_rate_request *req) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci struct clk_generated *gck = to_clk_generated(hw); 1278c2ecf20Sopenharmony_ci struct clk_hw *parent = NULL; 1288c2ecf20Sopenharmony_ci struct clk_rate_request req_parent = *req; 1298c2ecf20Sopenharmony_ci long best_rate = -EINVAL; 1308c2ecf20Sopenharmony_ci unsigned long min_rate, parent_rate; 1318c2ecf20Sopenharmony_ci int best_diff = -1; 1328c2ecf20Sopenharmony_ci int i; 1338c2ecf20Sopenharmony_ci u32 div; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* do not look for a rate that is outside of our range */ 1368c2ecf20Sopenharmony_ci if (gck->range.max && req->rate > gck->range.max) 1378c2ecf20Sopenharmony_ci req->rate = gck->range.max; 1388c2ecf20Sopenharmony_ci if (gck->range.min && req->rate < gck->range.min) 1398c2ecf20Sopenharmony_ci req->rate = gck->range.min; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci for (i = 0; i < clk_hw_get_num_parents(hw); i++) { 1428c2ecf20Sopenharmony_ci if (gck->chg_pid == i) 1438c2ecf20Sopenharmony_ci continue; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci parent = clk_hw_get_parent_by_index(hw, i); 1468c2ecf20Sopenharmony_ci if (!parent) 1478c2ecf20Sopenharmony_ci continue; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci parent_rate = clk_hw_get_rate(parent); 1508c2ecf20Sopenharmony_ci min_rate = DIV_ROUND_CLOSEST(parent_rate, GENERATED_MAX_DIV + 1); 1518c2ecf20Sopenharmony_ci if (!parent_rate || 1528c2ecf20Sopenharmony_ci (gck->range.max && min_rate > gck->range.max)) 1538c2ecf20Sopenharmony_ci continue; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci div = DIV_ROUND_CLOSEST(parent_rate, req->rate); 1568c2ecf20Sopenharmony_ci if (div > GENERATED_MAX_DIV + 1) 1578c2ecf20Sopenharmony_ci div = GENERATED_MAX_DIV + 1; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci clk_generated_best_diff(req, parent, parent_rate, div, 1608c2ecf20Sopenharmony_ci &best_diff, &best_rate); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci if (!best_diff) 1638c2ecf20Sopenharmony_ci break; 1648c2ecf20Sopenharmony_ci } 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci /* 1678c2ecf20Sopenharmony_ci * The audio_pll rate can be modified, unlike the five others clocks 1688c2ecf20Sopenharmony_ci * that should never be altered. 1698c2ecf20Sopenharmony_ci * The audio_pll can technically be used by multiple consumers. However, 1708c2ecf20Sopenharmony_ci * with the rate locking, the first consumer to enable to clock will be 1718c2ecf20Sopenharmony_ci * the one definitely setting the rate of the clock. 1728c2ecf20Sopenharmony_ci * Since audio IPs are most likely to request the same rate, we enforce 1738c2ecf20Sopenharmony_ci * that the only clks able to modify gck rate are those of audio IPs. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci if (gck->chg_pid < 0) 1778c2ecf20Sopenharmony_ci goto end; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci parent = clk_hw_get_parent_by_index(hw, gck->chg_pid); 1808c2ecf20Sopenharmony_ci if (!parent) 1818c2ecf20Sopenharmony_ci goto end; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci for (div = 1; div < GENERATED_MAX_DIV + 2; div++) { 1848c2ecf20Sopenharmony_ci req_parent.rate = req->rate * div; 1858c2ecf20Sopenharmony_ci if (__clk_determine_rate(parent, &req_parent)) 1868c2ecf20Sopenharmony_ci continue; 1878c2ecf20Sopenharmony_ci clk_generated_best_diff(req, parent, req_parent.rate, div, 1888c2ecf20Sopenharmony_ci &best_diff, &best_rate); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci if (!best_diff) 1918c2ecf20Sopenharmony_ci break; 1928c2ecf20Sopenharmony_ci } 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ciend: 1958c2ecf20Sopenharmony_ci pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n", 1968c2ecf20Sopenharmony_ci __func__, best_rate, 1978c2ecf20Sopenharmony_ci __clk_get_name((req->best_parent_hw)->clk), 1988c2ecf20Sopenharmony_ci req->best_parent_rate); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci if (best_rate < 0 || (gck->range.max && best_rate > gck->range.max)) 2018c2ecf20Sopenharmony_ci return -EINVAL; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci req->rate = best_rate; 2048c2ecf20Sopenharmony_ci return 0; 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci/* No modification of hardware as we have the flag CLK_SET_PARENT_GATE set */ 2088c2ecf20Sopenharmony_cistatic int clk_generated_set_parent(struct clk_hw *hw, u8 index) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci struct clk_generated *gck = to_clk_generated(hw); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci if (index >= clk_hw_get_num_parents(hw)) 2138c2ecf20Sopenharmony_ci return -EINVAL; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci if (gck->mux_table) 2168c2ecf20Sopenharmony_ci gck->parent_id = clk_mux_index_to_val(gck->mux_table, 0, index); 2178c2ecf20Sopenharmony_ci else 2188c2ecf20Sopenharmony_ci gck->parent_id = index; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci return 0; 2218c2ecf20Sopenharmony_ci} 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_cistatic u8 clk_generated_get_parent(struct clk_hw *hw) 2248c2ecf20Sopenharmony_ci{ 2258c2ecf20Sopenharmony_ci struct clk_generated *gck = to_clk_generated(hw); 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci return gck->parent_id; 2288c2ecf20Sopenharmony_ci} 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci/* No modification of hardware as we have the flag CLK_SET_RATE_GATE set */ 2318c2ecf20Sopenharmony_cistatic int clk_generated_set_rate(struct clk_hw *hw, 2328c2ecf20Sopenharmony_ci unsigned long rate, 2338c2ecf20Sopenharmony_ci unsigned long parent_rate) 2348c2ecf20Sopenharmony_ci{ 2358c2ecf20Sopenharmony_ci struct clk_generated *gck = to_clk_generated(hw); 2368c2ecf20Sopenharmony_ci u32 div; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci if (!rate) 2398c2ecf20Sopenharmony_ci return -EINVAL; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci if (gck->range.max && rate > gck->range.max) 2428c2ecf20Sopenharmony_ci return -EINVAL; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci div = DIV_ROUND_CLOSEST(parent_rate, rate); 2458c2ecf20Sopenharmony_ci if (div > GENERATED_MAX_DIV + 1 || !div) 2468c2ecf20Sopenharmony_ci return -EINVAL; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci gck->gckdiv = div - 1; 2498c2ecf20Sopenharmony_ci return 0; 2508c2ecf20Sopenharmony_ci} 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_cistatic const struct clk_ops generated_ops = { 2538c2ecf20Sopenharmony_ci .enable = clk_generated_enable, 2548c2ecf20Sopenharmony_ci .disable = clk_generated_disable, 2558c2ecf20Sopenharmony_ci .is_enabled = clk_generated_is_enabled, 2568c2ecf20Sopenharmony_ci .recalc_rate = clk_generated_recalc_rate, 2578c2ecf20Sopenharmony_ci .determine_rate = clk_generated_determine_rate, 2588c2ecf20Sopenharmony_ci .get_parent = clk_generated_get_parent, 2598c2ecf20Sopenharmony_ci .set_parent = clk_generated_set_parent, 2608c2ecf20Sopenharmony_ci .set_rate = clk_generated_set_rate, 2618c2ecf20Sopenharmony_ci}; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci/** 2648c2ecf20Sopenharmony_ci * clk_generated_startup - Initialize a given clock to its default parent and 2658c2ecf20Sopenharmony_ci * divisor parameter. 2668c2ecf20Sopenharmony_ci * 2678c2ecf20Sopenharmony_ci * @gck: Generated clock to set the startup parameters for. 2688c2ecf20Sopenharmony_ci * 2698c2ecf20Sopenharmony_ci * Take parameters from the hardware and update local clock configuration 2708c2ecf20Sopenharmony_ci * accordingly. 2718c2ecf20Sopenharmony_ci */ 2728c2ecf20Sopenharmony_cistatic void clk_generated_startup(struct clk_generated *gck) 2738c2ecf20Sopenharmony_ci{ 2748c2ecf20Sopenharmony_ci u32 tmp; 2758c2ecf20Sopenharmony_ci unsigned long flags; 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci spin_lock_irqsave(gck->lock, flags); 2788c2ecf20Sopenharmony_ci regmap_write(gck->regmap, gck->layout->offset, 2798c2ecf20Sopenharmony_ci (gck->id & gck->layout->pid_mask)); 2808c2ecf20Sopenharmony_ci regmap_read(gck->regmap, gck->layout->offset, &tmp); 2818c2ecf20Sopenharmony_ci spin_unlock_irqrestore(gck->lock, flags); 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci gck->parent_id = field_get(gck->layout->gckcss_mask, tmp); 2848c2ecf20Sopenharmony_ci gck->gckdiv = FIELD_GET(AT91_PMC_PCR_GCKDIV_MASK, tmp); 2858c2ecf20Sopenharmony_ci} 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_cistruct clk_hw * __init 2888c2ecf20Sopenharmony_ciat91_clk_register_generated(struct regmap *regmap, spinlock_t *lock, 2898c2ecf20Sopenharmony_ci const struct clk_pcr_layout *layout, 2908c2ecf20Sopenharmony_ci const char *name, const char **parent_names, 2918c2ecf20Sopenharmony_ci u32 *mux_table, u8 num_parents, u8 id, 2928c2ecf20Sopenharmony_ci const struct clk_range *range, 2938c2ecf20Sopenharmony_ci int chg_pid) 2948c2ecf20Sopenharmony_ci{ 2958c2ecf20Sopenharmony_ci struct clk_generated *gck; 2968c2ecf20Sopenharmony_ci struct clk_init_data init; 2978c2ecf20Sopenharmony_ci struct clk_hw *hw; 2988c2ecf20Sopenharmony_ci int ret; 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci gck = kzalloc(sizeof(*gck), GFP_KERNEL); 3018c2ecf20Sopenharmony_ci if (!gck) 3028c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci init.name = name; 3058c2ecf20Sopenharmony_ci init.ops = &generated_ops; 3068c2ecf20Sopenharmony_ci init.parent_names = parent_names; 3078c2ecf20Sopenharmony_ci init.num_parents = num_parents; 3088c2ecf20Sopenharmony_ci init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE; 3098c2ecf20Sopenharmony_ci if (chg_pid >= 0) 3108c2ecf20Sopenharmony_ci init.flags |= CLK_SET_RATE_PARENT; 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci gck->id = id; 3138c2ecf20Sopenharmony_ci gck->hw.init = &init; 3148c2ecf20Sopenharmony_ci gck->regmap = regmap; 3158c2ecf20Sopenharmony_ci gck->lock = lock; 3168c2ecf20Sopenharmony_ci gck->range = *range; 3178c2ecf20Sopenharmony_ci gck->chg_pid = chg_pid; 3188c2ecf20Sopenharmony_ci gck->layout = layout; 3198c2ecf20Sopenharmony_ci gck->mux_table = mux_table; 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci clk_generated_startup(gck); 3228c2ecf20Sopenharmony_ci hw = &gck->hw; 3238c2ecf20Sopenharmony_ci ret = clk_hw_register(NULL, &gck->hw); 3248c2ecf20Sopenharmony_ci if (ret) { 3258c2ecf20Sopenharmony_ci kfree(gck); 3268c2ecf20Sopenharmony_ci hw = ERR_PTR(ret); 3278c2ecf20Sopenharmony_ci } else { 3288c2ecf20Sopenharmony_ci pmc_register_id(id); 3298c2ecf20Sopenharmony_ci } 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci return hw; 3328c2ecf20Sopenharmony_ci} 333