1e874a669SEmilio López /* 2e874a669SEmilio López * Copyright (C) 2013 Emilio López <emilio@elopez.com.ar> 3e874a669SEmilio López * 4e874a669SEmilio López * This program is free software; you can redistribute it and/or modify 5e874a669SEmilio López * it under the terms of the GNU General Public License version 2 as 6e874a669SEmilio López * published by the Free Software Foundation. 7e874a669SEmilio López * 8e874a669SEmilio López * Adjustable factor-based clock implementation 9e874a669SEmilio López */ 10e874a669SEmilio López 11e874a669SEmilio López #include <linux/clk-provider.h> 12e874a669SEmilio López #include <linux/delay.h> 13601da9d0SMaxime Ripard #include <linux/err.h> 14601da9d0SMaxime Ripard #include <linux/io.h> 15601da9d0SMaxime Ripard #include <linux/module.h> 16601da9d0SMaxime Ripard #include <linux/of_address.h> 17601da9d0SMaxime Ripard #include <linux/slab.h> 18601da9d0SMaxime Ripard #include <linux/string.h> 19e874a669SEmilio López 20e874a669SEmilio López #include "clk-factors.h" 21e874a669SEmilio López 22e874a669SEmilio López /* 23601da9d0SMaxime Ripard * DOC: basic adjustable factor-based clock 24e874a669SEmilio López * 25e874a669SEmilio López * Traits of this clock: 26e874a669SEmilio López * prepare - clk_prepare only ensures that parents are prepared 27e874a669SEmilio López * enable - clk_enable only ensures that parents are enabled 28e874a669SEmilio López * rate - rate is adjustable. 29e874a669SEmilio López * clk->rate = (parent->rate * N * (K + 1) >> P) / (M + 1) 30e874a669SEmilio López * parent - fixed parent. No clk_set_parent support 31e874a669SEmilio López */ 32e874a669SEmilio López 33e874a669SEmilio López #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw) 34e874a669SEmilio López 35601da9d0SMaxime Ripard #define FACTORS_MAX_PARENTS 5 36601da9d0SMaxime Ripard 37c518e84cSEmilio López #define SETMASK(len, pos) (((1U << (len)) - 1) << (pos)) 38e874a669SEmilio López #define CLRMASK(len, pos) (~(SETMASK(len, pos))) 39e874a669SEmilio López #define FACTOR_GET(bit, len, reg) (((reg) & SETMASK(len, bit)) >> (bit)) 40e874a669SEmilio López 41e874a669SEmilio López #define FACTOR_SET(bit, len, reg, val) \ 42e874a669SEmilio López (((reg) & CLRMASK(len, bit)) | (val << (bit))) 43e874a669SEmilio López 44e874a669SEmilio López static unsigned long clk_factors_recalc_rate(struct clk_hw *hw, 45e874a669SEmilio López unsigned long parent_rate) 46e874a669SEmilio López { 47e874a669SEmilio López u8 n = 1, k = 0, p = 0, m = 0; 48e874a669SEmilio López u32 reg; 49e874a669SEmilio López unsigned long rate; 50e874a669SEmilio López struct clk_factors *factors = to_clk_factors(hw); 51b3e919e0SChen-Yu Tsai const struct clk_factors_config *config = factors->config; 52e874a669SEmilio López 53e874a669SEmilio López /* Fetch the register value */ 54e874a669SEmilio López reg = readl(factors->reg); 55e874a669SEmilio López 56e874a669SEmilio López /* Get each individual factor if applicable */ 57e874a669SEmilio López if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE) 58e874a669SEmilio López n = FACTOR_GET(config->nshift, config->nwidth, reg); 59e874a669SEmilio López if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE) 60e874a669SEmilio López k = FACTOR_GET(config->kshift, config->kwidth, reg); 61e874a669SEmilio López if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE) 62e874a669SEmilio López m = FACTOR_GET(config->mshift, config->mwidth, reg); 63e874a669SEmilio López if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE) 64e874a669SEmilio López p = FACTOR_GET(config->pshift, config->pwidth, reg); 65e874a669SEmilio López 66e874a669SEmilio López /* Calculate the rate */ 679a5e6c7eSChen-Yu Tsai rate = (parent_rate * (n + config->n_start) * (k + 1) >> p) / (m + 1); 68e874a669SEmilio López 69e874a669SEmilio López return rate; 70e874a669SEmilio López } 71e874a669SEmilio López 72e874a669SEmilio López static long clk_factors_round_rate(struct clk_hw *hw, unsigned long rate, 73e874a669SEmilio López unsigned long *parent_rate) 74e874a669SEmilio López { 75e874a669SEmilio López struct clk_factors *factors = to_clk_factors(hw); 76e874a669SEmilio López factors->get_factors((u32 *)&rate, (u32)*parent_rate, 77e874a669SEmilio López NULL, NULL, NULL, NULL); 78e874a669SEmilio López 79e874a669SEmilio López return rate; 80e874a669SEmilio López } 81e874a669SEmilio López 820817b62cSBoris Brezillon static int clk_factors_determine_rate(struct clk_hw *hw, 830817b62cSBoris Brezillon struct clk_rate_request *req) 84862b7283SEmilio López { 851b14afa6SStephen Boyd struct clk_hw *parent, *best_parent = NULL; 86862b7283SEmilio López int i, num_parents; 87862b7283SEmilio López unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0; 88862b7283SEmilio López 89862b7283SEmilio López /* find the parent that can help provide the fastest rate <= rate */ 90497295afSStephen Boyd num_parents = clk_hw_get_num_parents(hw); 91862b7283SEmilio López for (i = 0; i < num_parents; i++) { 921b14afa6SStephen Boyd parent = clk_hw_get_parent_by_index(hw, i); 93862b7283SEmilio López if (!parent) 94862b7283SEmilio López continue; 9598d8a60eSStephen Boyd if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) 961b14afa6SStephen Boyd parent_rate = clk_hw_round_rate(parent, req->rate); 97862b7283SEmilio López else 981b14afa6SStephen Boyd parent_rate = clk_hw_get_rate(parent); 99862b7283SEmilio López 1000817b62cSBoris Brezillon child_rate = clk_factors_round_rate(hw, req->rate, 1010817b62cSBoris Brezillon &parent_rate); 102862b7283SEmilio López 1030817b62cSBoris Brezillon if (child_rate <= req->rate && child_rate > best_child_rate) { 104862b7283SEmilio López best_parent = parent; 105862b7283SEmilio López best = parent_rate; 106862b7283SEmilio López best_child_rate = child_rate; 107862b7283SEmilio López } 108862b7283SEmilio López } 109862b7283SEmilio López 11057d866e6SBoris Brezillon if (!best_parent) 11157d866e6SBoris Brezillon return -EINVAL; 11257d866e6SBoris Brezillon 1131b14afa6SStephen Boyd req->best_parent_hw = best_parent; 1140817b62cSBoris Brezillon req->best_parent_rate = best; 1150817b62cSBoris Brezillon req->rate = best_child_rate; 116862b7283SEmilio López 1170817b62cSBoris Brezillon return 0; 118862b7283SEmilio López } 119862b7283SEmilio López 120e874a669SEmilio López static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate, 121e874a669SEmilio López unsigned long parent_rate) 122e874a669SEmilio López { 12312ef06afSEmilio López u8 n = 0, k = 0, m = 0, p = 0; 124e874a669SEmilio López u32 reg; 125e874a669SEmilio López struct clk_factors *factors = to_clk_factors(hw); 126b3e919e0SChen-Yu Tsai const struct clk_factors_config *config = factors->config; 127e874a669SEmilio López unsigned long flags = 0; 128e874a669SEmilio López 129e874a669SEmilio López factors->get_factors((u32 *)&rate, (u32)parent_rate, &n, &k, &m, &p); 130e874a669SEmilio López 131e874a669SEmilio López if (factors->lock) 132e874a669SEmilio López spin_lock_irqsave(factors->lock, flags); 133e874a669SEmilio López 134e874a669SEmilio López /* Fetch the register value */ 135e874a669SEmilio López reg = readl(factors->reg); 136e874a669SEmilio López 137e874a669SEmilio López /* Set up the new factors - macros do not do anything if width is 0 */ 138e874a669SEmilio López reg = FACTOR_SET(config->nshift, config->nwidth, reg, n); 139e874a669SEmilio López reg = FACTOR_SET(config->kshift, config->kwidth, reg, k); 140e874a669SEmilio López reg = FACTOR_SET(config->mshift, config->mwidth, reg, m); 141e874a669SEmilio López reg = FACTOR_SET(config->pshift, config->pwidth, reg, p); 142e874a669SEmilio López 143e874a669SEmilio López /* Apply them now */ 144e874a669SEmilio López writel(reg, factors->reg); 145e874a669SEmilio López 146e874a669SEmilio López /* delay 500us so pll stabilizes */ 147e874a669SEmilio López __delay((rate >> 20) * 500 / 2); 148e874a669SEmilio López 149e874a669SEmilio López if (factors->lock) 150e874a669SEmilio López spin_unlock_irqrestore(factors->lock, flags); 151e874a669SEmilio López 152e874a669SEmilio López return 0; 153e874a669SEmilio López } 154e874a669SEmilio López 155601da9d0SMaxime Ripard static const struct clk_ops clk_factors_ops = { 156862b7283SEmilio López .determine_rate = clk_factors_determine_rate, 157e874a669SEmilio López .recalc_rate = clk_factors_recalc_rate, 158e874a669SEmilio López .round_rate = clk_factors_round_rate, 159e874a669SEmilio López .set_rate = clk_factors_set_rate, 160e874a669SEmilio López }; 161601da9d0SMaxime Ripard 1627c74c220SHans de Goede struct clk *sunxi_factors_register(struct device_node *node, 163601da9d0SMaxime Ripard const struct factors_data *data, 1647c74c220SHans de Goede spinlock_t *lock, 1657c74c220SHans de Goede void __iomem *reg) 166601da9d0SMaxime Ripard { 167601da9d0SMaxime Ripard struct clk *clk; 168601da9d0SMaxime Ripard struct clk_factors *factors; 169601da9d0SMaxime Ripard struct clk_gate *gate = NULL; 170601da9d0SMaxime Ripard struct clk_mux *mux = NULL; 171601da9d0SMaxime Ripard struct clk_hw *gate_hw = NULL; 172601da9d0SMaxime Ripard struct clk_hw *mux_hw = NULL; 173601da9d0SMaxime Ripard const char *clk_name = node->name; 174601da9d0SMaxime Ripard const char *parents[FACTORS_MAX_PARENTS]; 17578ca95c7SChen-Yu Tsai int ret, i = 0; 176601da9d0SMaxime Ripard 177601da9d0SMaxime Ripard /* if we have a mux, we will have >1 parents */ 1788a53fb2bSDinh Nguyen i = of_clk_parent_fill(node, parents, FACTORS_MAX_PARENTS); 179601da9d0SMaxime Ripard 180601da9d0SMaxime Ripard /* 181601da9d0SMaxime Ripard * some factor clocks, such as pll5 and pll6, may have multiple 182601da9d0SMaxime Ripard * outputs, and have their name designated in factors_data 183601da9d0SMaxime Ripard */ 184601da9d0SMaxime Ripard if (data->name) 185601da9d0SMaxime Ripard clk_name = data->name; 186601da9d0SMaxime Ripard else 187601da9d0SMaxime Ripard of_property_read_string(node, "clock-output-names", &clk_name); 188601da9d0SMaxime Ripard 189601da9d0SMaxime Ripard factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL); 190601da9d0SMaxime Ripard if (!factors) 19178ca95c7SChen-Yu Tsai goto err_factors; 192601da9d0SMaxime Ripard 193601da9d0SMaxime Ripard /* set up factors properties */ 194601da9d0SMaxime Ripard factors->reg = reg; 195601da9d0SMaxime Ripard factors->config = data->table; 196601da9d0SMaxime Ripard factors->get_factors = data->getter; 197601da9d0SMaxime Ripard factors->lock = lock; 198601da9d0SMaxime Ripard 199601da9d0SMaxime Ripard /* Add a gate if this factor clock can be gated */ 200601da9d0SMaxime Ripard if (data->enable) { 201601da9d0SMaxime Ripard gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL); 20278ca95c7SChen-Yu Tsai if (!gate) 20378ca95c7SChen-Yu Tsai goto err_gate; 204601da9d0SMaxime Ripard 205*4cbeaebbSChen-Yu Tsai factors->gate = gate; 206*4cbeaebbSChen-Yu Tsai 207601da9d0SMaxime Ripard /* set up gate properties */ 208601da9d0SMaxime Ripard gate->reg = reg; 209601da9d0SMaxime Ripard gate->bit_idx = data->enable; 210601da9d0SMaxime Ripard gate->lock = factors->lock; 211601da9d0SMaxime Ripard gate_hw = &gate->hw; 212601da9d0SMaxime Ripard } 213601da9d0SMaxime Ripard 214601da9d0SMaxime Ripard /* Add a mux if this factor clock can be muxed */ 215601da9d0SMaxime Ripard if (data->mux) { 216601da9d0SMaxime Ripard mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL); 21778ca95c7SChen-Yu Tsai if (!mux) 21878ca95c7SChen-Yu Tsai goto err_mux; 219601da9d0SMaxime Ripard 220*4cbeaebbSChen-Yu Tsai factors->mux = mux; 221*4cbeaebbSChen-Yu Tsai 222601da9d0SMaxime Ripard /* set up gate properties */ 223601da9d0SMaxime Ripard mux->reg = reg; 224601da9d0SMaxime Ripard mux->shift = data->mux; 225e94f8cb3SChen-Yu Tsai mux->mask = data->muxmask; 226601da9d0SMaxime Ripard mux->lock = factors->lock; 227601da9d0SMaxime Ripard mux_hw = &mux->hw; 228601da9d0SMaxime Ripard } 229601da9d0SMaxime Ripard 230601da9d0SMaxime Ripard clk = clk_register_composite(NULL, clk_name, 231601da9d0SMaxime Ripard parents, i, 232601da9d0SMaxime Ripard mux_hw, &clk_mux_ops, 233601da9d0SMaxime Ripard &factors->hw, &clk_factors_ops, 234601da9d0SMaxime Ripard gate_hw, &clk_gate_ops, 0); 23578ca95c7SChen-Yu Tsai if (IS_ERR(clk)) 23678ca95c7SChen-Yu Tsai goto err_register; 237601da9d0SMaxime Ripard 23878ca95c7SChen-Yu Tsai ret = of_clk_add_provider(node, of_clk_src_simple_get, clk); 23978ca95c7SChen-Yu Tsai if (ret) 24078ca95c7SChen-Yu Tsai goto err_provider; 24178ca95c7SChen-Yu Tsai 24278ca95c7SChen-Yu Tsai ret = clk_register_clkdev(clk, clk_name, NULL); 24378ca95c7SChen-Yu Tsai if (ret) 24478ca95c7SChen-Yu Tsai goto err_clkdev; 245601da9d0SMaxime Ripard 246601da9d0SMaxime Ripard return clk; 24778ca95c7SChen-Yu Tsai 24878ca95c7SChen-Yu Tsai err_clkdev: 24978ca95c7SChen-Yu Tsai of_clk_del_provider(node); 25078ca95c7SChen-Yu Tsai err_provider: 25178ca95c7SChen-Yu Tsai /* TODO: The composite clock stuff will leak a bit here. */ 25278ca95c7SChen-Yu Tsai clk_unregister(clk); 25378ca95c7SChen-Yu Tsai err_register: 25478ca95c7SChen-Yu Tsai kfree(mux); 25578ca95c7SChen-Yu Tsai err_mux: 25678ca95c7SChen-Yu Tsai kfree(gate); 25778ca95c7SChen-Yu Tsai err_gate: 25878ca95c7SChen-Yu Tsai kfree(factors); 25978ca95c7SChen-Yu Tsai err_factors: 26078ca95c7SChen-Yu Tsai return NULL; 261601da9d0SMaxime Ripard } 262*4cbeaebbSChen-Yu Tsai 263*4cbeaebbSChen-Yu Tsai void sunxi_factors_unregister(struct device_node *node, struct clk *clk) 264*4cbeaebbSChen-Yu Tsai { 265*4cbeaebbSChen-Yu Tsai struct clk_hw *hw = __clk_get_hw(clk); 266*4cbeaebbSChen-Yu Tsai struct clk_factors *factors; 267*4cbeaebbSChen-Yu Tsai const char *name; 268*4cbeaebbSChen-Yu Tsai 269*4cbeaebbSChen-Yu Tsai if (!hw) 270*4cbeaebbSChen-Yu Tsai return; 271*4cbeaebbSChen-Yu Tsai 272*4cbeaebbSChen-Yu Tsai factors = to_clk_factors(hw); 273*4cbeaebbSChen-Yu Tsai name = clk_hw_get_name(hw); 274*4cbeaebbSChen-Yu Tsai 275*4cbeaebbSChen-Yu Tsai /* No unregister call for clkdev_* */ 276*4cbeaebbSChen-Yu Tsai of_clk_del_provider(node); 277*4cbeaebbSChen-Yu Tsai /* TODO: The composite clock stuff will leak a bit here. */ 278*4cbeaebbSChen-Yu Tsai clk_unregister(clk); 279*4cbeaebbSChen-Yu Tsai kfree(factors->mux); 280*4cbeaebbSChen-Yu Tsai kfree(factors->gate); 281*4cbeaebbSChen-Yu Tsai kfree(factors); 282*4cbeaebbSChen-Yu Tsai } 283