1a245fecbSHeiko Stübner /* 2a245fecbSHeiko Stübner * Copyright (c) 2014 MundoReader S.L. 3a245fecbSHeiko Stübner * Author: Heiko Stuebner <heiko@sntech.de> 4a245fecbSHeiko Stübner * 5ef1d9feeSXing Zheng * Copyright (c) 2016 Rockchip Electronics Co. Ltd. 6ef1d9feeSXing Zheng * Author: Xing Zheng <zhengxing@rock-chips.com> 7ef1d9feeSXing Zheng * 8a245fecbSHeiko Stübner * based on 9a245fecbSHeiko Stübner * 10a245fecbSHeiko Stübner * samsung/clk.c 11a245fecbSHeiko Stübner * Copyright (c) 2013 Samsung Electronics Co., Ltd. 12a245fecbSHeiko Stübner * Copyright (c) 2013 Linaro Ltd. 13a245fecbSHeiko Stübner * Author: Thomas Abraham <thomas.ab@samsung.com> 14a245fecbSHeiko Stübner * 15a245fecbSHeiko Stübner * This program is free software; you can redistribute it and/or modify 16a245fecbSHeiko Stübner * it under the terms of the GNU General Public License as published by 17a245fecbSHeiko Stübner * the Free Software Foundation; either version 2 of the License, or 18a245fecbSHeiko Stübner * (at your option) any later version. 19a245fecbSHeiko Stübner * 20a245fecbSHeiko Stübner * This program is distributed in the hope that it will be useful, 21a245fecbSHeiko Stübner * but WITHOUT ANY WARRANTY; without even the implied warranty of 22a245fecbSHeiko Stübner * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23a245fecbSHeiko Stübner * GNU General Public License for more details. 24a245fecbSHeiko Stübner */ 25a245fecbSHeiko Stübner 26a245fecbSHeiko Stübner #include <linux/slab.h> 27a245fecbSHeiko Stübner #include <linux/clk.h> 28a245fecbSHeiko Stübner #include <linux/clk-provider.h> 2990c59025SHeiko Stübner #include <linux/mfd/syscon.h> 3090c59025SHeiko Stübner #include <linux/regmap.h> 316f1294b5SHeiko Stübner #include <linux/reboot.h> 325d890c2dSElaine Zhang #include <linux/rational.h> 33a245fecbSHeiko Stübner #include "clk.h" 34a245fecbSHeiko Stübner 35a245fecbSHeiko Stübner /** 36a245fecbSHeiko Stübner * Register a clock branch. 37a245fecbSHeiko Stübner * Most clock branches have a form like 38a245fecbSHeiko Stübner * 39a245fecbSHeiko Stübner * src1 --|--\ 40a245fecbSHeiko Stübner * |M |--[GATE]-[DIV]- 41a245fecbSHeiko Stübner * src2 --|--/ 42a245fecbSHeiko Stübner * 43a245fecbSHeiko Stübner * sometimes without one of those components. 44a245fecbSHeiko Stübner */ 451a4b1819SHeiko Stübner static struct clk *rockchip_clk_register_branch(const char *name, 4603ae1747SHeiko Stuebner const char *const *parent_names, u8 num_parents, 4703ae1747SHeiko Stuebner void __iomem *base, 48a245fecbSHeiko Stübner int muxdiv_offset, u8 mux_shift, u8 mux_width, u8 mux_flags, 49*1f55660fSFinley Xiao int div_offset, u8 div_shift, u8 div_width, u8 div_flags, 50a245fecbSHeiko Stübner struct clk_div_table *div_table, int gate_offset, 51a245fecbSHeiko Stübner u8 gate_shift, u8 gate_flags, unsigned long flags, 52a245fecbSHeiko Stübner spinlock_t *lock) 53a245fecbSHeiko Stübner { 54a245fecbSHeiko Stübner struct clk *clk; 55a245fecbSHeiko Stübner struct clk_mux *mux = NULL; 56a245fecbSHeiko Stübner struct clk_gate *gate = NULL; 57a245fecbSHeiko Stübner struct clk_divider *div = NULL; 58a245fecbSHeiko Stübner const struct clk_ops *mux_ops = NULL, *div_ops = NULL, 59a245fecbSHeiko Stübner *gate_ops = NULL; 60fd3cbbfbSShawn Lin int ret; 61a245fecbSHeiko Stübner 62a245fecbSHeiko Stübner if (num_parents > 1) { 63a245fecbSHeiko Stübner mux = kzalloc(sizeof(*mux), GFP_KERNEL); 64a245fecbSHeiko Stübner if (!mux) 65a245fecbSHeiko Stübner return ERR_PTR(-ENOMEM); 66a245fecbSHeiko Stübner 67a245fecbSHeiko Stübner mux->reg = base + muxdiv_offset; 68a245fecbSHeiko Stübner mux->shift = mux_shift; 69a245fecbSHeiko Stübner mux->mask = BIT(mux_width) - 1; 70a245fecbSHeiko Stübner mux->flags = mux_flags; 71a245fecbSHeiko Stübner mux->lock = lock; 72a245fecbSHeiko Stübner mux_ops = (mux_flags & CLK_MUX_READ_ONLY) ? &clk_mux_ro_ops 73a245fecbSHeiko Stübner : &clk_mux_ops; 74a245fecbSHeiko Stübner } 75a245fecbSHeiko Stübner 76a245fecbSHeiko Stübner if (gate_offset >= 0) { 77a245fecbSHeiko Stübner gate = kzalloc(sizeof(*gate), GFP_KERNEL); 78fd3cbbfbSShawn Lin if (!gate) { 79fd3cbbfbSShawn Lin ret = -ENOMEM; 802467b674SShawn Lin goto err_gate; 81fd3cbbfbSShawn Lin } 82a245fecbSHeiko Stübner 83a245fecbSHeiko Stübner gate->flags = gate_flags; 84a245fecbSHeiko Stübner gate->reg = base + gate_offset; 85a245fecbSHeiko Stübner gate->bit_idx = gate_shift; 86a245fecbSHeiko Stübner gate->lock = lock; 87a245fecbSHeiko Stübner gate_ops = &clk_gate_ops; 88a245fecbSHeiko Stübner } 89a245fecbSHeiko Stübner 90a245fecbSHeiko Stübner if (div_width > 0) { 91a245fecbSHeiko Stübner div = kzalloc(sizeof(*div), GFP_KERNEL); 92fd3cbbfbSShawn Lin if (!div) { 93fd3cbbfbSShawn Lin ret = -ENOMEM; 942467b674SShawn Lin goto err_div; 95fd3cbbfbSShawn Lin } 96a245fecbSHeiko Stübner 97a245fecbSHeiko Stübner div->flags = div_flags; 98*1f55660fSFinley Xiao if (div_offset) 99*1f55660fSFinley Xiao div->reg = base + div_offset; 100*1f55660fSFinley Xiao else 101a245fecbSHeiko Stübner div->reg = base + muxdiv_offset; 102a245fecbSHeiko Stübner div->shift = div_shift; 103a245fecbSHeiko Stübner div->width = div_width; 104a245fecbSHeiko Stübner div->lock = lock; 105a245fecbSHeiko Stübner div->table = div_table; 10650359819SHeiko Stuebner div_ops = (div_flags & CLK_DIVIDER_READ_ONLY) 10750359819SHeiko Stuebner ? &clk_divider_ro_ops 10850359819SHeiko Stuebner : &clk_divider_ops; 109a245fecbSHeiko Stübner } 110a245fecbSHeiko Stübner 111a245fecbSHeiko Stübner clk = clk_register_composite(NULL, name, parent_names, num_parents, 112a245fecbSHeiko Stübner mux ? &mux->hw : NULL, mux_ops, 113a245fecbSHeiko Stübner div ? &div->hw : NULL, div_ops, 114a245fecbSHeiko Stübner gate ? &gate->hw : NULL, gate_ops, 115a245fecbSHeiko Stübner flags); 116a245fecbSHeiko Stübner 117fd3cbbfbSShawn Lin if (IS_ERR(clk)) { 118fd3cbbfbSShawn Lin ret = PTR_ERR(clk); 119fd3cbbfbSShawn Lin goto err_composite; 120fd3cbbfbSShawn Lin } 121fd3cbbfbSShawn Lin 122a245fecbSHeiko Stübner return clk; 123fd3cbbfbSShawn Lin err_composite: 124fd3cbbfbSShawn Lin kfree(div); 1252467b674SShawn Lin err_div: 1262467b674SShawn Lin kfree(gate); 1272467b674SShawn Lin err_gate: 1282467b674SShawn Lin kfree(mux); 129fd3cbbfbSShawn Lin return ERR_PTR(ret); 130a245fecbSHeiko Stübner } 131a245fecbSHeiko Stübner 1328ca1ca8fSHeiko Stuebner struct rockchip_clk_frac { 1338ca1ca8fSHeiko Stuebner struct notifier_block clk_nb; 1348ca1ca8fSHeiko Stuebner struct clk_fractional_divider div; 1358ca1ca8fSHeiko Stuebner struct clk_gate gate; 1368ca1ca8fSHeiko Stuebner 1378ca1ca8fSHeiko Stuebner struct clk_mux mux; 1388ca1ca8fSHeiko Stuebner const struct clk_ops *mux_ops; 1398ca1ca8fSHeiko Stuebner int mux_frac_idx; 1408ca1ca8fSHeiko Stuebner 1418ca1ca8fSHeiko Stuebner bool rate_change_remuxed; 1428ca1ca8fSHeiko Stuebner int rate_change_idx; 1438ca1ca8fSHeiko Stuebner }; 1448ca1ca8fSHeiko Stuebner 1458ca1ca8fSHeiko Stuebner #define to_rockchip_clk_frac_nb(nb) \ 1468ca1ca8fSHeiko Stuebner container_of(nb, struct rockchip_clk_frac, clk_nb) 1478ca1ca8fSHeiko Stuebner 1488ca1ca8fSHeiko Stuebner static int rockchip_clk_frac_notifier_cb(struct notifier_block *nb, 1498ca1ca8fSHeiko Stuebner unsigned long event, void *data) 1508ca1ca8fSHeiko Stuebner { 1518ca1ca8fSHeiko Stuebner struct clk_notifier_data *ndata = data; 1528ca1ca8fSHeiko Stuebner struct rockchip_clk_frac *frac = to_rockchip_clk_frac_nb(nb); 1538ca1ca8fSHeiko Stuebner struct clk_mux *frac_mux = &frac->mux; 1548ca1ca8fSHeiko Stuebner int ret = 0; 1558ca1ca8fSHeiko Stuebner 1568ca1ca8fSHeiko Stuebner pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n", 1578ca1ca8fSHeiko Stuebner __func__, event, ndata->old_rate, ndata->new_rate); 1588ca1ca8fSHeiko Stuebner if (event == PRE_RATE_CHANGE) { 15903ae1747SHeiko Stuebner frac->rate_change_idx = 16003ae1747SHeiko Stuebner frac->mux_ops->get_parent(&frac_mux->hw); 1618ca1ca8fSHeiko Stuebner if (frac->rate_change_idx != frac->mux_frac_idx) { 16203ae1747SHeiko Stuebner frac->mux_ops->set_parent(&frac_mux->hw, 16303ae1747SHeiko Stuebner frac->mux_frac_idx); 1648ca1ca8fSHeiko Stuebner frac->rate_change_remuxed = 1; 1658ca1ca8fSHeiko Stuebner } 1668ca1ca8fSHeiko Stuebner } else if (event == POST_RATE_CHANGE) { 1678ca1ca8fSHeiko Stuebner /* 1688ca1ca8fSHeiko Stuebner * The POST_RATE_CHANGE notifier runs directly after the 1698ca1ca8fSHeiko Stuebner * divider clock is set in clk_change_rate, so we'll have 1708ca1ca8fSHeiko Stuebner * remuxed back to the original parent before clk_change_rate 1718ca1ca8fSHeiko Stuebner * reaches the mux itself. 1728ca1ca8fSHeiko Stuebner */ 1738ca1ca8fSHeiko Stuebner if (frac->rate_change_remuxed) { 17403ae1747SHeiko Stuebner frac->mux_ops->set_parent(&frac_mux->hw, 17503ae1747SHeiko Stuebner frac->rate_change_idx); 1768ca1ca8fSHeiko Stuebner frac->rate_change_remuxed = 0; 1778ca1ca8fSHeiko Stuebner } 1788ca1ca8fSHeiko Stuebner } 1798ca1ca8fSHeiko Stuebner 1808ca1ca8fSHeiko Stuebner return notifier_from_errno(ret); 1818ca1ca8fSHeiko Stuebner } 1828ca1ca8fSHeiko Stuebner 1835d890c2dSElaine Zhang /** 1845d890c2dSElaine Zhang * fractional divider must set that denominator is 20 times larger than 1855d890c2dSElaine Zhang * numerator to generate precise clock frequency. 1865d890c2dSElaine Zhang */ 1871dfcfa72SStephen Boyd static void rockchip_fractional_approximation(struct clk_hw *hw, 1885d890c2dSElaine Zhang unsigned long rate, unsigned long *parent_rate, 1895d890c2dSElaine Zhang unsigned long *m, unsigned long *n) 1905d890c2dSElaine Zhang { 1915d890c2dSElaine Zhang struct clk_fractional_divider *fd = to_clk_fd(hw); 1925d890c2dSElaine Zhang unsigned long p_rate, p_parent_rate; 1935d890c2dSElaine Zhang struct clk_hw *p_parent; 1945d890c2dSElaine Zhang unsigned long scale; 1955d890c2dSElaine Zhang 1965d890c2dSElaine Zhang p_rate = clk_hw_get_rate(clk_hw_get_parent(hw)); 1975d890c2dSElaine Zhang if ((rate * 20 > p_rate) && (p_rate % rate != 0)) { 1985d890c2dSElaine Zhang p_parent = clk_hw_get_parent(clk_hw_get_parent(hw)); 1995d890c2dSElaine Zhang p_parent_rate = clk_hw_get_rate(p_parent); 2005d890c2dSElaine Zhang *parent_rate = p_parent_rate; 2015d890c2dSElaine Zhang } 2025d890c2dSElaine Zhang 2035d890c2dSElaine Zhang /* 2045d890c2dSElaine Zhang * Get rate closer to *parent_rate to guarantee there is no overflow 2055d890c2dSElaine Zhang * for m and n. In the result it will be the nearest rate left shifted 2065d890c2dSElaine Zhang * by (scale - fd->nwidth) bits. 2075d890c2dSElaine Zhang */ 2085d890c2dSElaine Zhang scale = fls_long(*parent_rate / rate - 1); 2095d890c2dSElaine Zhang if (scale > fd->nwidth) 2105d890c2dSElaine Zhang rate <<= scale - fd->nwidth; 2115d890c2dSElaine Zhang 2125d890c2dSElaine Zhang rational_best_approximation(rate, *parent_rate, 2135d890c2dSElaine Zhang GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), 2145d890c2dSElaine Zhang m, n); 2155d890c2dSElaine Zhang } 2165d890c2dSElaine Zhang 217ef1d9feeSXing Zheng static struct clk *rockchip_clk_register_frac_branch( 218ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx, const char *name, 2194a1caed3SUwe Kleine-König const char *const *parent_names, u8 num_parents, 2204a1caed3SUwe Kleine-König void __iomem *base, int muxdiv_offset, u8 div_flags, 221b2155a71SHeiko Stübner int gate_offset, u8 gate_shift, u8 gate_flags, 2228ca1ca8fSHeiko Stuebner unsigned long flags, struct rockchip_clk_branch *child, 2238ca1ca8fSHeiko Stuebner spinlock_t *lock) 224b2155a71SHeiko Stübner { 2258ca1ca8fSHeiko Stuebner struct rockchip_clk_frac *frac; 226b2155a71SHeiko Stübner struct clk *clk; 227b2155a71SHeiko Stübner struct clk_gate *gate = NULL; 228b2155a71SHeiko Stübner struct clk_fractional_divider *div = NULL; 229b2155a71SHeiko Stübner const struct clk_ops *div_ops = NULL, *gate_ops = NULL; 230b2155a71SHeiko Stübner 2318ca1ca8fSHeiko Stuebner if (muxdiv_offset < 0) 2328ca1ca8fSHeiko Stuebner return ERR_PTR(-EINVAL); 2338ca1ca8fSHeiko Stuebner 2348ca1ca8fSHeiko Stuebner if (child && child->branch_type != branch_mux) { 2358ca1ca8fSHeiko Stuebner pr_err("%s: fractional child clock for %s can only be a mux\n", 2368ca1ca8fSHeiko Stuebner __func__, name); 2378ca1ca8fSHeiko Stuebner return ERR_PTR(-EINVAL); 2388ca1ca8fSHeiko Stuebner } 2398ca1ca8fSHeiko Stuebner 2408ca1ca8fSHeiko Stuebner frac = kzalloc(sizeof(*frac), GFP_KERNEL); 2418ca1ca8fSHeiko Stuebner if (!frac) 242b2155a71SHeiko Stübner return ERR_PTR(-ENOMEM); 243b2155a71SHeiko Stübner 2448ca1ca8fSHeiko Stuebner if (gate_offset >= 0) { 2458ca1ca8fSHeiko Stuebner gate = &frac->gate; 246b2155a71SHeiko Stübner gate->flags = gate_flags; 247b2155a71SHeiko Stübner gate->reg = base + gate_offset; 248b2155a71SHeiko Stübner gate->bit_idx = gate_shift; 249b2155a71SHeiko Stübner gate->lock = lock; 250b2155a71SHeiko Stübner gate_ops = &clk_gate_ops; 251b2155a71SHeiko Stübner } 252b2155a71SHeiko Stübner 2538ca1ca8fSHeiko Stuebner div = &frac->div; 254b2155a71SHeiko Stübner div->flags = div_flags; 255b2155a71SHeiko Stübner div->reg = base + muxdiv_offset; 256b2155a71SHeiko Stübner div->mshift = 16; 2575d49a6e1SAndy Shevchenko div->mwidth = 16; 2585d49a6e1SAndy Shevchenko div->mmask = GENMASK(div->mwidth - 1, 0) << div->mshift; 259b2155a71SHeiko Stübner div->nshift = 0; 2605d49a6e1SAndy Shevchenko div->nwidth = 16; 2615d49a6e1SAndy Shevchenko div->nmask = GENMASK(div->nwidth - 1, 0) << div->nshift; 262b2155a71SHeiko Stübner div->lock = lock; 2635d890c2dSElaine Zhang div->approximation = rockchip_fractional_approximation; 264b2155a71SHeiko Stübner div_ops = &clk_fractional_divider_ops; 265b2155a71SHeiko Stübner 266b2155a71SHeiko Stübner clk = clk_register_composite(NULL, name, parent_names, num_parents, 267b2155a71SHeiko Stübner NULL, NULL, 268b2155a71SHeiko Stübner &div->hw, div_ops, 269b2155a71SHeiko Stübner gate ? &gate->hw : NULL, gate_ops, 2708ca1ca8fSHeiko Stuebner flags | CLK_SET_RATE_UNGATE); 2718ca1ca8fSHeiko Stuebner if (IS_ERR(clk)) { 2728ca1ca8fSHeiko Stuebner kfree(frac); 2738ca1ca8fSHeiko Stuebner return clk; 2748ca1ca8fSHeiko Stuebner } 2758ca1ca8fSHeiko Stuebner 2768ca1ca8fSHeiko Stuebner if (child) { 2778ca1ca8fSHeiko Stuebner struct clk_mux *frac_mux = &frac->mux; 2788ca1ca8fSHeiko Stuebner struct clk_init_data init; 2798ca1ca8fSHeiko Stuebner struct clk *mux_clk; 280a425702fSYisheng Xie int ret; 2818ca1ca8fSHeiko Stuebner 282a425702fSYisheng Xie frac->mux_frac_idx = match_string(child->parent_names, 283a425702fSYisheng Xie child->num_parents, name); 2848ca1ca8fSHeiko Stuebner frac->mux_ops = &clk_mux_ops; 2858ca1ca8fSHeiko Stuebner frac->clk_nb.notifier_call = rockchip_clk_frac_notifier_cb; 2868ca1ca8fSHeiko Stuebner 2878ca1ca8fSHeiko Stuebner frac_mux->reg = base + child->muxdiv_offset; 2888ca1ca8fSHeiko Stuebner frac_mux->shift = child->mux_shift; 2898ca1ca8fSHeiko Stuebner frac_mux->mask = BIT(child->mux_width) - 1; 2908ca1ca8fSHeiko Stuebner frac_mux->flags = child->mux_flags; 2918ca1ca8fSHeiko Stuebner frac_mux->lock = lock; 2928ca1ca8fSHeiko Stuebner frac_mux->hw.init = &init; 2938ca1ca8fSHeiko Stuebner 2948ca1ca8fSHeiko Stuebner init.name = child->name; 2958ca1ca8fSHeiko Stuebner init.flags = child->flags | CLK_SET_RATE_PARENT; 2968ca1ca8fSHeiko Stuebner init.ops = frac->mux_ops; 2978ca1ca8fSHeiko Stuebner init.parent_names = child->parent_names; 2988ca1ca8fSHeiko Stuebner init.num_parents = child->num_parents; 2998ca1ca8fSHeiko Stuebner 3008ca1ca8fSHeiko Stuebner mux_clk = clk_register(NULL, &frac_mux->hw); 301fd3cbbfbSShawn Lin if (IS_ERR(mux_clk)) { 302fd3cbbfbSShawn Lin kfree(frac); 3038ca1ca8fSHeiko Stuebner return clk; 304fd3cbbfbSShawn Lin } 3058ca1ca8fSHeiko Stuebner 306ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, mux_clk, child->id); 3078ca1ca8fSHeiko Stuebner 3088ca1ca8fSHeiko Stuebner /* notifier on the fraction divider to catch rate changes */ 3098ca1ca8fSHeiko Stuebner if (frac->mux_frac_idx >= 0) { 310a425702fSYisheng Xie pr_debug("%s: found fractional parent in mux at pos %d\n", 311a425702fSYisheng Xie __func__, frac->mux_frac_idx); 3128ca1ca8fSHeiko Stuebner ret = clk_notifier_register(clk, &frac->clk_nb); 3138ca1ca8fSHeiko Stuebner if (ret) 3148ca1ca8fSHeiko Stuebner pr_err("%s: failed to register clock notifier for %s\n", 3158ca1ca8fSHeiko Stuebner __func__, name); 3168ca1ca8fSHeiko Stuebner } else { 3178ca1ca8fSHeiko Stuebner pr_warn("%s: could not find %s as parent of %s, rate changes may not work\n", 3188ca1ca8fSHeiko Stuebner __func__, name, child->name); 3198ca1ca8fSHeiko Stuebner } 3208ca1ca8fSHeiko Stuebner } 321b2155a71SHeiko Stübner 322b2155a71SHeiko Stübner return clk; 323b2155a71SHeiko Stübner } 324b2155a71SHeiko Stübner 32529a30c26SHeiko Stuebner static struct clk *rockchip_clk_register_factor_branch(const char *name, 32629a30c26SHeiko Stuebner const char *const *parent_names, u8 num_parents, 32729a30c26SHeiko Stuebner void __iomem *base, unsigned int mult, unsigned int div, 32829a30c26SHeiko Stuebner int gate_offset, u8 gate_shift, u8 gate_flags, 32929a30c26SHeiko Stuebner unsigned long flags, spinlock_t *lock) 33029a30c26SHeiko Stuebner { 33129a30c26SHeiko Stuebner struct clk *clk; 33229a30c26SHeiko Stuebner struct clk_gate *gate = NULL; 33329a30c26SHeiko Stuebner struct clk_fixed_factor *fix = NULL; 33429a30c26SHeiko Stuebner 33529a30c26SHeiko Stuebner /* without gate, register a simple factor clock */ 33629a30c26SHeiko Stuebner if (gate_offset == 0) { 33729a30c26SHeiko Stuebner return clk_register_fixed_factor(NULL, name, 33829a30c26SHeiko Stuebner parent_names[0], flags, mult, 33929a30c26SHeiko Stuebner div); 34029a30c26SHeiko Stuebner } 34129a30c26SHeiko Stuebner 34229a30c26SHeiko Stuebner gate = kzalloc(sizeof(*gate), GFP_KERNEL); 34329a30c26SHeiko Stuebner if (!gate) 34429a30c26SHeiko Stuebner return ERR_PTR(-ENOMEM); 34529a30c26SHeiko Stuebner 34629a30c26SHeiko Stuebner gate->flags = gate_flags; 34729a30c26SHeiko Stuebner gate->reg = base + gate_offset; 34829a30c26SHeiko Stuebner gate->bit_idx = gate_shift; 34929a30c26SHeiko Stuebner gate->lock = lock; 35029a30c26SHeiko Stuebner 35129a30c26SHeiko Stuebner fix = kzalloc(sizeof(*fix), GFP_KERNEL); 35229a30c26SHeiko Stuebner if (!fix) { 35329a30c26SHeiko Stuebner kfree(gate); 35429a30c26SHeiko Stuebner return ERR_PTR(-ENOMEM); 35529a30c26SHeiko Stuebner } 35629a30c26SHeiko Stuebner 35729a30c26SHeiko Stuebner fix->mult = mult; 35829a30c26SHeiko Stuebner fix->div = div; 35929a30c26SHeiko Stuebner 36029a30c26SHeiko Stuebner clk = clk_register_composite(NULL, name, parent_names, num_parents, 36129a30c26SHeiko Stuebner NULL, NULL, 36229a30c26SHeiko Stuebner &fix->hw, &clk_fixed_factor_ops, 36329a30c26SHeiko Stuebner &gate->hw, &clk_gate_ops, flags); 36429a30c26SHeiko Stuebner if (IS_ERR(clk)) { 36529a30c26SHeiko Stuebner kfree(fix); 36629a30c26SHeiko Stuebner kfree(gate); 36729a30c26SHeiko Stuebner } 36829a30c26SHeiko Stuebner 36929a30c26SHeiko Stuebner return clk; 37029a30c26SHeiko Stuebner } 37129a30c26SHeiko Stuebner 372ef1d9feeSXing Zheng struct rockchip_clk_provider * __init rockchip_clk_init(struct device_node *np, 373ef1d9feeSXing Zheng void __iomem *base, unsigned long nr_clks) 374a245fecbSHeiko Stübner { 375ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx; 376ef1d9feeSXing Zheng struct clk **clk_table; 377ef1d9feeSXing Zheng int i; 378ef1d9feeSXing Zheng 379ef1d9feeSXing Zheng ctx = kzalloc(sizeof(struct rockchip_clk_provider), GFP_KERNEL); 38003ae1747SHeiko Stuebner if (!ctx) 381ef1d9feeSXing Zheng return ERR_PTR(-ENOMEM); 382a245fecbSHeiko Stübner 383a245fecbSHeiko Stübner clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL); 38403ae1747SHeiko Stuebner if (!clk_table) 385ef1d9feeSXing Zheng goto err_free; 386a245fecbSHeiko Stübner 387ef1d9feeSXing Zheng for (i = 0; i < nr_clks; ++i) 388ef1d9feeSXing Zheng clk_table[i] = ERR_PTR(-ENOENT); 389ef1d9feeSXing Zheng 390ef1d9feeSXing Zheng ctx->reg_base = base; 391ef1d9feeSXing Zheng ctx->clk_data.clks = clk_table; 392ef1d9feeSXing Zheng ctx->clk_data.clk_num = nr_clks; 393ef1d9feeSXing Zheng ctx->cru_node = np; 394ef1d9feeSXing Zheng spin_lock_init(&ctx->lock); 395ef1d9feeSXing Zheng 3966f339dc2SHeiko Stuebner ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node, 3976f339dc2SHeiko Stuebner "rockchip,grf"); 3986f339dc2SHeiko Stuebner 399ef1d9feeSXing Zheng return ctx; 400ef1d9feeSXing Zheng 401ef1d9feeSXing Zheng err_free: 402ef1d9feeSXing Zheng kfree(ctx); 403ef1d9feeSXing Zheng return ERR_PTR(-ENOMEM); 404ef1d9feeSXing Zheng } 405ef1d9feeSXing Zheng 406ef1d9feeSXing Zheng void __init rockchip_clk_of_add_provider(struct device_node *np, 407ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx) 40890c59025SHeiko Stübner { 409ef1d9feeSXing Zheng if (of_clk_add_provider(np, of_clk_src_onecell_get, 410ef1d9feeSXing Zheng &ctx->clk_data)) 411ef1d9feeSXing Zheng pr_err("%s: could not register clk provider\n", __func__); 412ef1d9feeSXing Zheng } 41390c59025SHeiko Stübner 414ef1d9feeSXing Zheng void rockchip_clk_add_lookup(struct rockchip_clk_provider *ctx, 415ef1d9feeSXing Zheng struct clk *clk, unsigned int id) 416ef1d9feeSXing Zheng { 417ef1d9feeSXing Zheng if (ctx->clk_data.clks && id) 418ef1d9feeSXing Zheng ctx->clk_data.clks[id] = clk; 419ef1d9feeSXing Zheng } 420ef1d9feeSXing Zheng 421ef1d9feeSXing Zheng void __init rockchip_clk_register_plls(struct rockchip_clk_provider *ctx, 422ef1d9feeSXing Zheng struct rockchip_pll_clock *list, 42390c59025SHeiko Stübner unsigned int nr_pll, int grf_lock_offset) 42490c59025SHeiko Stübner { 42590c59025SHeiko Stübner struct clk *clk; 42690c59025SHeiko Stübner int idx; 42790c59025SHeiko Stübner 42890c59025SHeiko Stübner for (idx = 0; idx < nr_pll; idx++, list++) { 429ef1d9feeSXing Zheng clk = rockchip_clk_register_pll(ctx, list->type, list->name, 43090c59025SHeiko Stübner list->parent_names, list->num_parents, 431ef1d9feeSXing Zheng list->con_offset, grf_lock_offset, 43290c59025SHeiko Stübner list->lock_shift, list->mode_offset, 4334f8a7c54SHeiko Stuebner list->mode_shift, list->rate_table, 434e6cebc72SHeiko Stübner list->flags, list->pll_flags); 43590c59025SHeiko Stübner if (IS_ERR(clk)) { 43690c59025SHeiko Stübner pr_err("%s: failed to register clock %s\n", __func__, 43790c59025SHeiko Stübner list->name); 43890c59025SHeiko Stübner continue; 43990c59025SHeiko Stübner } 44090c59025SHeiko Stübner 441ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, clk, list->id); 44290c59025SHeiko Stübner } 44390c59025SHeiko Stübner } 44490c59025SHeiko Stübner 445a245fecbSHeiko Stübner void __init rockchip_clk_register_branches( 446ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx, 447a245fecbSHeiko Stübner struct rockchip_clk_branch *list, 448a245fecbSHeiko Stübner unsigned int nr_clk) 449a245fecbSHeiko Stübner { 450a245fecbSHeiko Stübner struct clk *clk = NULL; 451a245fecbSHeiko Stübner unsigned int idx; 452a245fecbSHeiko Stübner unsigned long flags; 453a245fecbSHeiko Stübner 454a245fecbSHeiko Stübner for (idx = 0; idx < nr_clk; idx++, list++) { 455a245fecbSHeiko Stübner flags = list->flags; 456a245fecbSHeiko Stübner 457a245fecbSHeiko Stübner /* catch simple muxes */ 458a245fecbSHeiko Stübner switch (list->branch_type) { 459a245fecbSHeiko Stübner case branch_mux: 460a245fecbSHeiko Stübner clk = clk_register_mux(NULL, list->name, 461a245fecbSHeiko Stübner list->parent_names, list->num_parents, 462ef1d9feeSXing Zheng flags, ctx->reg_base + list->muxdiv_offset, 463a245fecbSHeiko Stübner list->mux_shift, list->mux_width, 464ef1d9feeSXing Zheng list->mux_flags, &ctx->lock); 465a245fecbSHeiko Stübner break; 466cb1d9f6dSHeiko Stuebner case branch_muxgrf: 467cb1d9f6dSHeiko Stuebner clk = rockchip_clk_register_muxgrf(list->name, 468cb1d9f6dSHeiko Stuebner list->parent_names, list->num_parents, 469cb1d9f6dSHeiko Stuebner flags, ctx->grf, list->muxdiv_offset, 470cb1d9f6dSHeiko Stuebner list->mux_shift, list->mux_width, 471cb1d9f6dSHeiko Stuebner list->mux_flags); 472cb1d9f6dSHeiko Stuebner break; 473a245fecbSHeiko Stübner case branch_divider: 474a245fecbSHeiko Stübner if (list->div_table) 475a245fecbSHeiko Stübner clk = clk_register_divider_table(NULL, 476a245fecbSHeiko Stübner list->name, list->parent_names[0], 47703ae1747SHeiko Stuebner flags, 47803ae1747SHeiko Stuebner ctx->reg_base + list->muxdiv_offset, 479a245fecbSHeiko Stübner list->div_shift, list->div_width, 480a245fecbSHeiko Stübner list->div_flags, list->div_table, 481ef1d9feeSXing Zheng &ctx->lock); 482a245fecbSHeiko Stübner else 483a245fecbSHeiko Stübner clk = clk_register_divider(NULL, list->name, 484a245fecbSHeiko Stübner list->parent_names[0], flags, 485ef1d9feeSXing Zheng ctx->reg_base + list->muxdiv_offset, 486a245fecbSHeiko Stübner list->div_shift, list->div_width, 487ef1d9feeSXing Zheng list->div_flags, &ctx->lock); 488a245fecbSHeiko Stübner break; 489a245fecbSHeiko Stübner case branch_fraction_divider: 490ef1d9feeSXing Zheng clk = rockchip_clk_register_frac_branch(ctx, list->name, 491b2155a71SHeiko Stübner list->parent_names, list->num_parents, 49203ae1747SHeiko Stuebner ctx->reg_base, list->muxdiv_offset, 49303ae1747SHeiko Stuebner list->div_flags, 494b2155a71SHeiko Stübner list->gate_offset, list->gate_shift, 4958ca1ca8fSHeiko Stuebner list->gate_flags, flags, list->child, 496ef1d9feeSXing Zheng &ctx->lock); 497a245fecbSHeiko Stübner break; 498956060a5SElaine Zhang case branch_half_divider: 499956060a5SElaine Zhang clk = rockchip_clk_register_halfdiv(list->name, 500956060a5SElaine Zhang list->parent_names, list->num_parents, 501956060a5SElaine Zhang ctx->reg_base, list->muxdiv_offset, 502956060a5SElaine Zhang list->mux_shift, list->mux_width, 503956060a5SElaine Zhang list->mux_flags, list->div_shift, 504956060a5SElaine Zhang list->div_width, list->div_flags, 505956060a5SElaine Zhang list->gate_offset, list->gate_shift, 506956060a5SElaine Zhang list->gate_flags, flags, &ctx->lock); 507956060a5SElaine Zhang break; 508a245fecbSHeiko Stübner case branch_gate: 509a245fecbSHeiko Stübner flags |= CLK_SET_RATE_PARENT; 510a245fecbSHeiko Stübner 511a245fecbSHeiko Stübner clk = clk_register_gate(NULL, list->name, 512a245fecbSHeiko Stübner list->parent_names[0], flags, 513ef1d9feeSXing Zheng ctx->reg_base + list->gate_offset, 514ef1d9feeSXing Zheng list->gate_shift, list->gate_flags, &ctx->lock); 515a245fecbSHeiko Stübner break; 516a245fecbSHeiko Stübner case branch_composite: 517a245fecbSHeiko Stübner clk = rockchip_clk_register_branch(list->name, 518a245fecbSHeiko Stübner list->parent_names, list->num_parents, 51903ae1747SHeiko Stuebner ctx->reg_base, list->muxdiv_offset, 52003ae1747SHeiko Stuebner list->mux_shift, 521a245fecbSHeiko Stübner list->mux_width, list->mux_flags, 522*1f55660fSFinley Xiao list->div_offset, list->div_shift, list->div_width, 523a245fecbSHeiko Stübner list->div_flags, list->div_table, 524a245fecbSHeiko Stübner list->gate_offset, list->gate_shift, 525ef1d9feeSXing Zheng list->gate_flags, flags, &ctx->lock); 526a245fecbSHeiko Stübner break; 52789bf26cbSAlexandru M Stan case branch_mmc: 52889bf26cbSAlexandru M Stan clk = rockchip_clk_register_mmc( 52989bf26cbSAlexandru M Stan list->name, 53089bf26cbSAlexandru M Stan list->parent_names, list->num_parents, 531ef1d9feeSXing Zheng ctx->reg_base + list->muxdiv_offset, 53289bf26cbSAlexandru M Stan list->div_shift 53389bf26cbSAlexandru M Stan ); 53489bf26cbSAlexandru M Stan break; 5358a76f443SHeiko Stuebner case branch_inverter: 5368a76f443SHeiko Stuebner clk = rockchip_clk_register_inverter( 5378a76f443SHeiko Stuebner list->name, list->parent_names, 5388a76f443SHeiko Stuebner list->num_parents, 539ef1d9feeSXing Zheng ctx->reg_base + list->muxdiv_offset, 540ef1d9feeSXing Zheng list->div_shift, list->div_flags, &ctx->lock); 5418a76f443SHeiko Stuebner break; 54229a30c26SHeiko Stuebner case branch_factor: 54329a30c26SHeiko Stuebner clk = rockchip_clk_register_factor_branch( 54429a30c26SHeiko Stuebner list->name, list->parent_names, 545ef1d9feeSXing Zheng list->num_parents, ctx->reg_base, 54629a30c26SHeiko Stuebner list->div_shift, list->div_width, 54729a30c26SHeiko Stuebner list->gate_offset, list->gate_shift, 548ef1d9feeSXing Zheng list->gate_flags, flags, &ctx->lock); 54929a30c26SHeiko Stuebner break; 550a4f182bfSLin Huang case branch_ddrclk: 551a4f182bfSLin Huang clk = rockchip_clk_register_ddrclk( 552a4f182bfSLin Huang list->name, list->flags, 553a4f182bfSLin Huang list->parent_names, list->num_parents, 554a4f182bfSLin Huang list->muxdiv_offset, list->mux_shift, 555a4f182bfSLin Huang list->mux_width, list->div_shift, 556a4f182bfSLin Huang list->div_width, list->div_flags, 557a4f182bfSLin Huang ctx->reg_base, &ctx->lock); 558a4f182bfSLin Huang break; 559a245fecbSHeiko Stübner } 560a245fecbSHeiko Stübner 561a245fecbSHeiko Stübner /* none of the cases above matched */ 562a245fecbSHeiko Stübner if (!clk) { 563a245fecbSHeiko Stübner pr_err("%s: unknown clock type %d\n", 564a245fecbSHeiko Stübner __func__, list->branch_type); 565a245fecbSHeiko Stübner continue; 566a245fecbSHeiko Stübner } 567a245fecbSHeiko Stübner 568a245fecbSHeiko Stübner if (IS_ERR(clk)) { 569a245fecbSHeiko Stübner pr_err("%s: failed to register clock %s: %ld\n", 570a245fecbSHeiko Stübner __func__, list->name, PTR_ERR(clk)); 571a245fecbSHeiko Stübner continue; 572a245fecbSHeiko Stübner } 573a245fecbSHeiko Stübner 574ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, clk, list->id); 575a245fecbSHeiko Stübner } 576a245fecbSHeiko Stübner } 577fe94f974SHeiko Stübner 578ef1d9feeSXing Zheng void __init rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx, 579ef1d9feeSXing Zheng unsigned int lookup_id, 5804a1caed3SUwe Kleine-König const char *name, const char *const *parent_names, 581f6fba5f6SHeiko Stuebner u8 num_parents, 582f6fba5f6SHeiko Stuebner const struct rockchip_cpuclk_reg_data *reg_data, 583f6fba5f6SHeiko Stuebner const struct rockchip_cpuclk_rate_table *rates, 584f6fba5f6SHeiko Stuebner int nrates) 585f6fba5f6SHeiko Stuebner { 586f6fba5f6SHeiko Stuebner struct clk *clk; 587f6fba5f6SHeiko Stuebner 588f6fba5f6SHeiko Stuebner clk = rockchip_clk_register_cpuclk(name, parent_names, num_parents, 58903ae1747SHeiko Stuebner reg_data, rates, nrates, 59003ae1747SHeiko Stuebner ctx->reg_base, &ctx->lock); 591f6fba5f6SHeiko Stuebner if (IS_ERR(clk)) { 592f6fba5f6SHeiko Stuebner pr_err("%s: failed to register clock %s: %ld\n", 593f6fba5f6SHeiko Stuebner __func__, name, PTR_ERR(clk)); 594f6fba5f6SHeiko Stuebner return; 595f6fba5f6SHeiko Stuebner } 596f6fba5f6SHeiko Stuebner 597ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, clk, lookup_id); 598f6fba5f6SHeiko Stuebner } 599f6fba5f6SHeiko Stuebner 600692d8328SUwe Kleine-König void __init rockchip_clk_protect_critical(const char *const clocks[], 601692d8328SUwe Kleine-König int nclocks) 602fe94f974SHeiko Stübner { 603fe94f974SHeiko Stübner int i; 604fe94f974SHeiko Stübner 605fe94f974SHeiko Stübner /* Protect the clocks that needs to stay on */ 606fe94f974SHeiko Stübner for (i = 0; i < nclocks; i++) { 607fe94f974SHeiko Stübner struct clk *clk = __clk_lookup(clocks[i]); 608fe94f974SHeiko Stübner 609fe94f974SHeiko Stübner if (clk) 610fe94f974SHeiko Stübner clk_prepare_enable(clk); 611fe94f974SHeiko Stübner } 612fe94f974SHeiko Stübner } 6136f1294b5SHeiko Stübner 614ef1d9feeSXing Zheng static void __iomem *rst_base; 6156f1294b5SHeiko Stübner static unsigned int reg_restart; 616dfff24bdSHeiko Stuebner static void (*cb_restart)(void); 6176f1294b5SHeiko Stübner static int rockchip_restart_notify(struct notifier_block *this, 6186f1294b5SHeiko Stübner unsigned long mode, void *cmd) 6196f1294b5SHeiko Stübner { 620dfff24bdSHeiko Stuebner if (cb_restart) 621dfff24bdSHeiko Stuebner cb_restart(); 622dfff24bdSHeiko Stuebner 623ef1d9feeSXing Zheng writel(0xfdb9, rst_base + reg_restart); 6246f1294b5SHeiko Stübner return NOTIFY_DONE; 6256f1294b5SHeiko Stübner } 6266f1294b5SHeiko Stübner 6276f1294b5SHeiko Stübner static struct notifier_block rockchip_restart_handler = { 6286f1294b5SHeiko Stübner .notifier_call = rockchip_restart_notify, 6296f1294b5SHeiko Stübner .priority = 128, 6306f1294b5SHeiko Stübner }; 6316f1294b5SHeiko Stübner 63203ae1747SHeiko Stuebner void __init 63303ae1747SHeiko Stuebner rockchip_register_restart_notifier(struct rockchip_clk_provider *ctx, 63403ae1747SHeiko Stuebner unsigned int reg, 63503ae1747SHeiko Stuebner void (*cb)(void)) 6366f1294b5SHeiko Stübner { 6376f1294b5SHeiko Stübner int ret; 6386f1294b5SHeiko Stübner 639ef1d9feeSXing Zheng rst_base = ctx->reg_base; 6406f1294b5SHeiko Stübner reg_restart = reg; 641dfff24bdSHeiko Stuebner cb_restart = cb; 6426f1294b5SHeiko Stübner ret = register_restart_handler(&rockchip_restart_handler); 6436f1294b5SHeiko Stübner if (ret) 6446f1294b5SHeiko Stübner pr_err("%s: cannot register restart handler, %d\n", 6456f1294b5SHeiko Stübner __func__, ret); 6466f1294b5SHeiko Stübner } 647