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> 32a245fecbSHeiko Stübner #include "clk.h" 33a245fecbSHeiko Stübner 34a245fecbSHeiko Stübner /** 35a245fecbSHeiko Stübner * Register a clock branch. 36a245fecbSHeiko Stübner * Most clock branches have a form like 37a245fecbSHeiko Stübner * 38a245fecbSHeiko Stübner * src1 --|--\ 39a245fecbSHeiko Stübner * |M |--[GATE]-[DIV]- 40a245fecbSHeiko Stübner * src2 --|--/ 41a245fecbSHeiko Stübner * 42a245fecbSHeiko Stübner * sometimes without one of those components. 43a245fecbSHeiko Stübner */ 441a4b1819SHeiko Stübner static struct clk *rockchip_clk_register_branch(const char *name, 4503ae1747SHeiko Stuebner const char *const *parent_names, u8 num_parents, 4603ae1747SHeiko Stuebner void __iomem *base, 47a245fecbSHeiko Stübner int muxdiv_offset, u8 mux_shift, u8 mux_width, u8 mux_flags, 48a245fecbSHeiko Stübner u8 div_shift, u8 div_width, u8 div_flags, 49a245fecbSHeiko Stübner struct clk_div_table *div_table, int gate_offset, 50a245fecbSHeiko Stübner u8 gate_shift, u8 gate_flags, unsigned long flags, 51a245fecbSHeiko Stübner spinlock_t *lock) 52a245fecbSHeiko Stübner { 53a245fecbSHeiko Stübner struct clk *clk; 54a245fecbSHeiko Stübner struct clk_mux *mux = NULL; 55a245fecbSHeiko Stübner struct clk_gate *gate = NULL; 56a245fecbSHeiko Stübner struct clk_divider *div = NULL; 57a245fecbSHeiko Stübner const struct clk_ops *mux_ops = NULL, *div_ops = NULL, 58a245fecbSHeiko Stübner *gate_ops = NULL; 59a245fecbSHeiko Stübner 60a245fecbSHeiko Stübner if (num_parents > 1) { 61a245fecbSHeiko Stübner mux = kzalloc(sizeof(*mux), GFP_KERNEL); 62a245fecbSHeiko Stübner if (!mux) 63a245fecbSHeiko Stübner return ERR_PTR(-ENOMEM); 64a245fecbSHeiko Stübner 65a245fecbSHeiko Stübner mux->reg = base + muxdiv_offset; 66a245fecbSHeiko Stübner mux->shift = mux_shift; 67a245fecbSHeiko Stübner mux->mask = BIT(mux_width) - 1; 68a245fecbSHeiko Stübner mux->flags = mux_flags; 69a245fecbSHeiko Stübner mux->lock = lock; 70a245fecbSHeiko Stübner mux_ops = (mux_flags & CLK_MUX_READ_ONLY) ? &clk_mux_ro_ops 71a245fecbSHeiko Stübner : &clk_mux_ops; 72a245fecbSHeiko Stübner } 73a245fecbSHeiko Stübner 74a245fecbSHeiko Stübner if (gate_offset >= 0) { 75a245fecbSHeiko Stübner gate = kzalloc(sizeof(*gate), GFP_KERNEL); 76a245fecbSHeiko Stübner if (!gate) 772467b674SShawn Lin goto err_gate; 78a245fecbSHeiko Stübner 79a245fecbSHeiko Stübner gate->flags = gate_flags; 80a245fecbSHeiko Stübner gate->reg = base + gate_offset; 81a245fecbSHeiko Stübner gate->bit_idx = gate_shift; 82a245fecbSHeiko Stübner gate->lock = lock; 83a245fecbSHeiko Stübner gate_ops = &clk_gate_ops; 84a245fecbSHeiko Stübner } 85a245fecbSHeiko Stübner 86a245fecbSHeiko Stübner if (div_width > 0) { 87a245fecbSHeiko Stübner div = kzalloc(sizeof(*div), GFP_KERNEL); 88a245fecbSHeiko Stübner if (!div) 892467b674SShawn Lin goto err_div; 90a245fecbSHeiko Stübner 91a245fecbSHeiko Stübner div->flags = div_flags; 92a245fecbSHeiko Stübner div->reg = base + muxdiv_offset; 93a245fecbSHeiko Stübner div->shift = div_shift; 94a245fecbSHeiko Stübner div->width = div_width; 95a245fecbSHeiko Stübner div->lock = lock; 96a245fecbSHeiko Stübner div->table = div_table; 9750359819SHeiko Stuebner div_ops = (div_flags & CLK_DIVIDER_READ_ONLY) 9850359819SHeiko Stuebner ? &clk_divider_ro_ops 9950359819SHeiko Stuebner : &clk_divider_ops; 100a245fecbSHeiko Stübner } 101a245fecbSHeiko Stübner 102a245fecbSHeiko Stübner clk = clk_register_composite(NULL, name, parent_names, num_parents, 103a245fecbSHeiko Stübner mux ? &mux->hw : NULL, mux_ops, 104a245fecbSHeiko Stübner div ? &div->hw : NULL, div_ops, 105a245fecbSHeiko Stübner gate ? &gate->hw : NULL, gate_ops, 106a245fecbSHeiko Stübner flags); 107a245fecbSHeiko Stübner 108a245fecbSHeiko Stübner return clk; 1092467b674SShawn Lin err_div: 1102467b674SShawn Lin kfree(gate); 1112467b674SShawn Lin err_gate: 1122467b674SShawn Lin kfree(mux); 1132467b674SShawn Lin return ERR_PTR(-ENOMEM); 114a245fecbSHeiko Stübner } 115a245fecbSHeiko Stübner 1168ca1ca8fSHeiko Stuebner struct rockchip_clk_frac { 1178ca1ca8fSHeiko Stuebner struct notifier_block clk_nb; 1188ca1ca8fSHeiko Stuebner struct clk_fractional_divider div; 1198ca1ca8fSHeiko Stuebner struct clk_gate gate; 1208ca1ca8fSHeiko Stuebner 1218ca1ca8fSHeiko Stuebner struct clk_mux mux; 1228ca1ca8fSHeiko Stuebner const struct clk_ops *mux_ops; 1238ca1ca8fSHeiko Stuebner int mux_frac_idx; 1248ca1ca8fSHeiko Stuebner 1258ca1ca8fSHeiko Stuebner bool rate_change_remuxed; 1268ca1ca8fSHeiko Stuebner int rate_change_idx; 1278ca1ca8fSHeiko Stuebner }; 1288ca1ca8fSHeiko Stuebner 1298ca1ca8fSHeiko Stuebner #define to_rockchip_clk_frac_nb(nb) \ 1308ca1ca8fSHeiko Stuebner container_of(nb, struct rockchip_clk_frac, clk_nb) 1318ca1ca8fSHeiko Stuebner 1328ca1ca8fSHeiko Stuebner static int rockchip_clk_frac_notifier_cb(struct notifier_block *nb, 1338ca1ca8fSHeiko Stuebner unsigned long event, void *data) 1348ca1ca8fSHeiko Stuebner { 1358ca1ca8fSHeiko Stuebner struct clk_notifier_data *ndata = data; 1368ca1ca8fSHeiko Stuebner struct rockchip_clk_frac *frac = to_rockchip_clk_frac_nb(nb); 1378ca1ca8fSHeiko Stuebner struct clk_mux *frac_mux = &frac->mux; 1388ca1ca8fSHeiko Stuebner int ret = 0; 1398ca1ca8fSHeiko Stuebner 1408ca1ca8fSHeiko Stuebner pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n", 1418ca1ca8fSHeiko Stuebner __func__, event, ndata->old_rate, ndata->new_rate); 1428ca1ca8fSHeiko Stuebner if (event == PRE_RATE_CHANGE) { 14303ae1747SHeiko Stuebner frac->rate_change_idx = 14403ae1747SHeiko Stuebner frac->mux_ops->get_parent(&frac_mux->hw); 1458ca1ca8fSHeiko Stuebner if (frac->rate_change_idx != frac->mux_frac_idx) { 14603ae1747SHeiko Stuebner frac->mux_ops->set_parent(&frac_mux->hw, 14703ae1747SHeiko Stuebner frac->mux_frac_idx); 1488ca1ca8fSHeiko Stuebner frac->rate_change_remuxed = 1; 1498ca1ca8fSHeiko Stuebner } 1508ca1ca8fSHeiko Stuebner } else if (event == POST_RATE_CHANGE) { 1518ca1ca8fSHeiko Stuebner /* 1528ca1ca8fSHeiko Stuebner * The POST_RATE_CHANGE notifier runs directly after the 1538ca1ca8fSHeiko Stuebner * divider clock is set in clk_change_rate, so we'll have 1548ca1ca8fSHeiko Stuebner * remuxed back to the original parent before clk_change_rate 1558ca1ca8fSHeiko Stuebner * reaches the mux itself. 1568ca1ca8fSHeiko Stuebner */ 1578ca1ca8fSHeiko Stuebner if (frac->rate_change_remuxed) { 15803ae1747SHeiko Stuebner frac->mux_ops->set_parent(&frac_mux->hw, 15903ae1747SHeiko Stuebner frac->rate_change_idx); 1608ca1ca8fSHeiko Stuebner frac->rate_change_remuxed = 0; 1618ca1ca8fSHeiko Stuebner } 1628ca1ca8fSHeiko Stuebner } 1638ca1ca8fSHeiko Stuebner 1648ca1ca8fSHeiko Stuebner return notifier_from_errno(ret); 1658ca1ca8fSHeiko Stuebner } 1668ca1ca8fSHeiko Stuebner 167ef1d9feeSXing Zheng static struct clk *rockchip_clk_register_frac_branch( 168ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx, const char *name, 1694a1caed3SUwe Kleine-König const char *const *parent_names, u8 num_parents, 1704a1caed3SUwe Kleine-König void __iomem *base, int muxdiv_offset, u8 div_flags, 171b2155a71SHeiko Stübner int gate_offset, u8 gate_shift, u8 gate_flags, 1728ca1ca8fSHeiko Stuebner unsigned long flags, struct rockchip_clk_branch *child, 1738ca1ca8fSHeiko Stuebner spinlock_t *lock) 174b2155a71SHeiko Stübner { 1758ca1ca8fSHeiko Stuebner struct rockchip_clk_frac *frac; 176b2155a71SHeiko Stübner struct clk *clk; 177b2155a71SHeiko Stübner struct clk_gate *gate = NULL; 178b2155a71SHeiko Stübner struct clk_fractional_divider *div = NULL; 179b2155a71SHeiko Stübner const struct clk_ops *div_ops = NULL, *gate_ops = NULL; 180b2155a71SHeiko Stübner 1818ca1ca8fSHeiko Stuebner if (muxdiv_offset < 0) 1828ca1ca8fSHeiko Stuebner return ERR_PTR(-EINVAL); 1838ca1ca8fSHeiko Stuebner 1848ca1ca8fSHeiko Stuebner if (child && child->branch_type != branch_mux) { 1858ca1ca8fSHeiko Stuebner pr_err("%s: fractional child clock for %s can only be a mux\n", 1868ca1ca8fSHeiko Stuebner __func__, name); 1878ca1ca8fSHeiko Stuebner return ERR_PTR(-EINVAL); 1888ca1ca8fSHeiko Stuebner } 1898ca1ca8fSHeiko Stuebner 1908ca1ca8fSHeiko Stuebner frac = kzalloc(sizeof(*frac), GFP_KERNEL); 1918ca1ca8fSHeiko Stuebner if (!frac) 192b2155a71SHeiko Stübner return ERR_PTR(-ENOMEM); 193b2155a71SHeiko Stübner 1948ca1ca8fSHeiko Stuebner if (gate_offset >= 0) { 1958ca1ca8fSHeiko Stuebner gate = &frac->gate; 196b2155a71SHeiko Stübner gate->flags = gate_flags; 197b2155a71SHeiko Stübner gate->reg = base + gate_offset; 198b2155a71SHeiko Stübner gate->bit_idx = gate_shift; 199b2155a71SHeiko Stübner gate->lock = lock; 200b2155a71SHeiko Stübner gate_ops = &clk_gate_ops; 201b2155a71SHeiko Stübner } 202b2155a71SHeiko Stübner 2038ca1ca8fSHeiko Stuebner div = &frac->div; 204b2155a71SHeiko Stübner div->flags = div_flags; 205b2155a71SHeiko Stübner div->reg = base + muxdiv_offset; 206b2155a71SHeiko Stübner div->mshift = 16; 2075d49a6e1SAndy Shevchenko div->mwidth = 16; 2085d49a6e1SAndy Shevchenko div->mmask = GENMASK(div->mwidth - 1, 0) << div->mshift; 209b2155a71SHeiko Stübner div->nshift = 0; 2105d49a6e1SAndy Shevchenko div->nwidth = 16; 2115d49a6e1SAndy Shevchenko div->nmask = GENMASK(div->nwidth - 1, 0) << div->nshift; 212b2155a71SHeiko Stübner div->lock = lock; 213b2155a71SHeiko Stübner div_ops = &clk_fractional_divider_ops; 214b2155a71SHeiko Stübner 215b2155a71SHeiko Stübner clk = clk_register_composite(NULL, name, parent_names, num_parents, 216b2155a71SHeiko Stübner NULL, NULL, 217b2155a71SHeiko Stübner &div->hw, div_ops, 218b2155a71SHeiko Stübner gate ? &gate->hw : NULL, gate_ops, 2198ca1ca8fSHeiko Stuebner flags | CLK_SET_RATE_UNGATE); 2208ca1ca8fSHeiko Stuebner if (IS_ERR(clk)) { 2218ca1ca8fSHeiko Stuebner kfree(frac); 2228ca1ca8fSHeiko Stuebner return clk; 2238ca1ca8fSHeiko Stuebner } 2248ca1ca8fSHeiko Stuebner 2258ca1ca8fSHeiko Stuebner if (child) { 2268ca1ca8fSHeiko Stuebner struct clk_mux *frac_mux = &frac->mux; 2278ca1ca8fSHeiko Stuebner struct clk_init_data init; 2288ca1ca8fSHeiko Stuebner struct clk *mux_clk; 2298ca1ca8fSHeiko Stuebner int i, ret; 2308ca1ca8fSHeiko Stuebner 2318ca1ca8fSHeiko Stuebner frac->mux_frac_idx = -1; 2328ca1ca8fSHeiko Stuebner for (i = 0; i < child->num_parents; i++) { 2338ca1ca8fSHeiko Stuebner if (!strcmp(name, child->parent_names[i])) { 2348ca1ca8fSHeiko Stuebner pr_debug("%s: found fractional parent in mux at pos %d\n", 2358ca1ca8fSHeiko Stuebner __func__, i); 2368ca1ca8fSHeiko Stuebner frac->mux_frac_idx = i; 2378ca1ca8fSHeiko Stuebner break; 2388ca1ca8fSHeiko Stuebner } 2398ca1ca8fSHeiko Stuebner } 2408ca1ca8fSHeiko Stuebner 2418ca1ca8fSHeiko Stuebner frac->mux_ops = &clk_mux_ops; 2428ca1ca8fSHeiko Stuebner frac->clk_nb.notifier_call = rockchip_clk_frac_notifier_cb; 2438ca1ca8fSHeiko Stuebner 2448ca1ca8fSHeiko Stuebner frac_mux->reg = base + child->muxdiv_offset; 2458ca1ca8fSHeiko Stuebner frac_mux->shift = child->mux_shift; 2468ca1ca8fSHeiko Stuebner frac_mux->mask = BIT(child->mux_width) - 1; 2478ca1ca8fSHeiko Stuebner frac_mux->flags = child->mux_flags; 2488ca1ca8fSHeiko Stuebner frac_mux->lock = lock; 2498ca1ca8fSHeiko Stuebner frac_mux->hw.init = &init; 2508ca1ca8fSHeiko Stuebner 2518ca1ca8fSHeiko Stuebner init.name = child->name; 2528ca1ca8fSHeiko Stuebner init.flags = child->flags | CLK_SET_RATE_PARENT; 2538ca1ca8fSHeiko Stuebner init.ops = frac->mux_ops; 2548ca1ca8fSHeiko Stuebner init.parent_names = child->parent_names; 2558ca1ca8fSHeiko Stuebner init.num_parents = child->num_parents; 2568ca1ca8fSHeiko Stuebner 2578ca1ca8fSHeiko Stuebner mux_clk = clk_register(NULL, &frac_mux->hw); 2588ca1ca8fSHeiko Stuebner if (IS_ERR(mux_clk)) 2598ca1ca8fSHeiko Stuebner return clk; 2608ca1ca8fSHeiko Stuebner 261ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, mux_clk, child->id); 2628ca1ca8fSHeiko Stuebner 2638ca1ca8fSHeiko Stuebner /* notifier on the fraction divider to catch rate changes */ 2648ca1ca8fSHeiko Stuebner if (frac->mux_frac_idx >= 0) { 2658ca1ca8fSHeiko Stuebner ret = clk_notifier_register(clk, &frac->clk_nb); 2668ca1ca8fSHeiko Stuebner if (ret) 2678ca1ca8fSHeiko Stuebner pr_err("%s: failed to register clock notifier for %s\n", 2688ca1ca8fSHeiko Stuebner __func__, name); 2698ca1ca8fSHeiko Stuebner } else { 2708ca1ca8fSHeiko Stuebner pr_warn("%s: could not find %s as parent of %s, rate changes may not work\n", 2718ca1ca8fSHeiko Stuebner __func__, name, child->name); 2728ca1ca8fSHeiko Stuebner } 2738ca1ca8fSHeiko Stuebner } 274b2155a71SHeiko Stübner 275b2155a71SHeiko Stübner return clk; 276b2155a71SHeiko Stübner } 277b2155a71SHeiko Stübner 27829a30c26SHeiko Stuebner static struct clk *rockchip_clk_register_factor_branch(const char *name, 27929a30c26SHeiko Stuebner const char *const *parent_names, u8 num_parents, 28029a30c26SHeiko Stuebner void __iomem *base, unsigned int mult, unsigned int div, 28129a30c26SHeiko Stuebner int gate_offset, u8 gate_shift, u8 gate_flags, 28229a30c26SHeiko Stuebner unsigned long flags, spinlock_t *lock) 28329a30c26SHeiko Stuebner { 28429a30c26SHeiko Stuebner struct clk *clk; 28529a30c26SHeiko Stuebner struct clk_gate *gate = NULL; 28629a30c26SHeiko Stuebner struct clk_fixed_factor *fix = NULL; 28729a30c26SHeiko Stuebner 28829a30c26SHeiko Stuebner /* without gate, register a simple factor clock */ 28929a30c26SHeiko Stuebner if (gate_offset == 0) { 29029a30c26SHeiko Stuebner return clk_register_fixed_factor(NULL, name, 29129a30c26SHeiko Stuebner parent_names[0], flags, mult, 29229a30c26SHeiko Stuebner div); 29329a30c26SHeiko Stuebner } 29429a30c26SHeiko Stuebner 29529a30c26SHeiko Stuebner gate = kzalloc(sizeof(*gate), GFP_KERNEL); 29629a30c26SHeiko Stuebner if (!gate) 29729a30c26SHeiko Stuebner return ERR_PTR(-ENOMEM); 29829a30c26SHeiko Stuebner 29929a30c26SHeiko Stuebner gate->flags = gate_flags; 30029a30c26SHeiko Stuebner gate->reg = base + gate_offset; 30129a30c26SHeiko Stuebner gate->bit_idx = gate_shift; 30229a30c26SHeiko Stuebner gate->lock = lock; 30329a30c26SHeiko Stuebner 30429a30c26SHeiko Stuebner fix = kzalloc(sizeof(*fix), GFP_KERNEL); 30529a30c26SHeiko Stuebner if (!fix) { 30629a30c26SHeiko Stuebner kfree(gate); 30729a30c26SHeiko Stuebner return ERR_PTR(-ENOMEM); 30829a30c26SHeiko Stuebner } 30929a30c26SHeiko Stuebner 31029a30c26SHeiko Stuebner fix->mult = mult; 31129a30c26SHeiko Stuebner fix->div = div; 31229a30c26SHeiko Stuebner 31329a30c26SHeiko Stuebner clk = clk_register_composite(NULL, name, parent_names, num_parents, 31429a30c26SHeiko Stuebner NULL, NULL, 31529a30c26SHeiko Stuebner &fix->hw, &clk_fixed_factor_ops, 31629a30c26SHeiko Stuebner &gate->hw, &clk_gate_ops, flags); 31729a30c26SHeiko Stuebner if (IS_ERR(clk)) { 31829a30c26SHeiko Stuebner kfree(fix); 31929a30c26SHeiko Stuebner kfree(gate); 32029a30c26SHeiko Stuebner } 32129a30c26SHeiko Stuebner 32229a30c26SHeiko Stuebner return clk; 32329a30c26SHeiko Stuebner } 32429a30c26SHeiko Stuebner 325ef1d9feeSXing Zheng struct rockchip_clk_provider * __init rockchip_clk_init(struct device_node *np, 326ef1d9feeSXing Zheng void __iomem *base, unsigned long nr_clks) 327a245fecbSHeiko Stübner { 328ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx; 329ef1d9feeSXing Zheng struct clk **clk_table; 330ef1d9feeSXing Zheng int i; 331ef1d9feeSXing Zheng 332ef1d9feeSXing Zheng ctx = kzalloc(sizeof(struct rockchip_clk_provider), GFP_KERNEL); 33303ae1747SHeiko Stuebner if (!ctx) 334ef1d9feeSXing Zheng return ERR_PTR(-ENOMEM); 335a245fecbSHeiko Stübner 336a245fecbSHeiko Stübner clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL); 33703ae1747SHeiko Stuebner if (!clk_table) 338ef1d9feeSXing Zheng goto err_free; 339a245fecbSHeiko Stübner 340ef1d9feeSXing Zheng for (i = 0; i < nr_clks; ++i) 341ef1d9feeSXing Zheng clk_table[i] = ERR_PTR(-ENOENT); 342ef1d9feeSXing Zheng 343ef1d9feeSXing Zheng ctx->reg_base = base; 344ef1d9feeSXing Zheng ctx->clk_data.clks = clk_table; 345ef1d9feeSXing Zheng ctx->clk_data.clk_num = nr_clks; 346ef1d9feeSXing Zheng ctx->cru_node = np; 347ef1d9feeSXing Zheng ctx->grf = ERR_PTR(-EPROBE_DEFER); 348ef1d9feeSXing Zheng spin_lock_init(&ctx->lock); 349ef1d9feeSXing Zheng 3506f339dc2SHeiko Stuebner ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node, 3516f339dc2SHeiko Stuebner "rockchip,grf"); 3526f339dc2SHeiko Stuebner 353ef1d9feeSXing Zheng return ctx; 354ef1d9feeSXing Zheng 355ef1d9feeSXing Zheng err_free: 356ef1d9feeSXing Zheng kfree(ctx); 357ef1d9feeSXing Zheng return ERR_PTR(-ENOMEM); 358ef1d9feeSXing Zheng } 359ef1d9feeSXing Zheng 360ef1d9feeSXing Zheng void __init rockchip_clk_of_add_provider(struct device_node *np, 361ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx) 36290c59025SHeiko Stübner { 363ef1d9feeSXing Zheng if (of_clk_add_provider(np, of_clk_src_onecell_get, 364ef1d9feeSXing Zheng &ctx->clk_data)) 365ef1d9feeSXing Zheng pr_err("%s: could not register clk provider\n", __func__); 366ef1d9feeSXing Zheng } 36790c59025SHeiko Stübner 368ef1d9feeSXing Zheng void rockchip_clk_add_lookup(struct rockchip_clk_provider *ctx, 369ef1d9feeSXing Zheng struct clk *clk, unsigned int id) 370ef1d9feeSXing Zheng { 371ef1d9feeSXing Zheng if (ctx->clk_data.clks && id) 372ef1d9feeSXing Zheng ctx->clk_data.clks[id] = clk; 373ef1d9feeSXing Zheng } 374ef1d9feeSXing Zheng 375ef1d9feeSXing Zheng void __init rockchip_clk_register_plls(struct rockchip_clk_provider *ctx, 376ef1d9feeSXing Zheng struct rockchip_pll_clock *list, 37790c59025SHeiko Stübner unsigned int nr_pll, int grf_lock_offset) 37890c59025SHeiko Stübner { 37990c59025SHeiko Stübner struct clk *clk; 38090c59025SHeiko Stübner int idx; 38190c59025SHeiko Stübner 38290c59025SHeiko Stübner for (idx = 0; idx < nr_pll; idx++, list++) { 383ef1d9feeSXing Zheng clk = rockchip_clk_register_pll(ctx, list->type, list->name, 38490c59025SHeiko Stübner list->parent_names, list->num_parents, 385ef1d9feeSXing Zheng list->con_offset, grf_lock_offset, 38690c59025SHeiko Stübner list->lock_shift, list->mode_offset, 3874f8a7c54SHeiko Stuebner list->mode_shift, list->rate_table, 388e6cebc72SHeiko Stübner list->flags, list->pll_flags); 38990c59025SHeiko Stübner if (IS_ERR(clk)) { 39090c59025SHeiko Stübner pr_err("%s: failed to register clock %s\n", __func__, 39190c59025SHeiko Stübner list->name); 39290c59025SHeiko Stübner continue; 39390c59025SHeiko Stübner } 39490c59025SHeiko Stübner 395ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, clk, list->id); 39690c59025SHeiko Stübner } 39790c59025SHeiko Stübner } 39890c59025SHeiko Stübner 399a245fecbSHeiko Stübner void __init rockchip_clk_register_branches( 400ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx, 401a245fecbSHeiko Stübner struct rockchip_clk_branch *list, 402a245fecbSHeiko Stübner unsigned int nr_clk) 403a245fecbSHeiko Stübner { 404a245fecbSHeiko Stübner struct clk *clk = NULL; 405a245fecbSHeiko Stübner unsigned int idx; 406a245fecbSHeiko Stübner unsigned long flags; 407a245fecbSHeiko Stübner 408a245fecbSHeiko Stübner for (idx = 0; idx < nr_clk; idx++, list++) { 409a245fecbSHeiko Stübner flags = list->flags; 410a245fecbSHeiko Stübner 411a245fecbSHeiko Stübner /* catch simple muxes */ 412a245fecbSHeiko Stübner switch (list->branch_type) { 413a245fecbSHeiko Stübner case branch_mux: 414a245fecbSHeiko Stübner clk = clk_register_mux(NULL, list->name, 415a245fecbSHeiko Stübner list->parent_names, list->num_parents, 416ef1d9feeSXing Zheng flags, ctx->reg_base + list->muxdiv_offset, 417a245fecbSHeiko Stübner list->mux_shift, list->mux_width, 418ef1d9feeSXing Zheng list->mux_flags, &ctx->lock); 419a245fecbSHeiko Stübner break; 420*cb1d9f6dSHeiko Stuebner case branch_muxgrf: 421*cb1d9f6dSHeiko Stuebner clk = rockchip_clk_register_muxgrf(list->name, 422*cb1d9f6dSHeiko Stuebner list->parent_names, list->num_parents, 423*cb1d9f6dSHeiko Stuebner flags, ctx->grf, list->muxdiv_offset, 424*cb1d9f6dSHeiko Stuebner list->mux_shift, list->mux_width, 425*cb1d9f6dSHeiko Stuebner list->mux_flags); 426*cb1d9f6dSHeiko Stuebner break; 427a245fecbSHeiko Stübner case branch_divider: 428a245fecbSHeiko Stübner if (list->div_table) 429a245fecbSHeiko Stübner clk = clk_register_divider_table(NULL, 430a245fecbSHeiko Stübner list->name, list->parent_names[0], 43103ae1747SHeiko Stuebner flags, 43203ae1747SHeiko Stuebner ctx->reg_base + list->muxdiv_offset, 433a245fecbSHeiko Stübner list->div_shift, list->div_width, 434a245fecbSHeiko Stübner list->div_flags, list->div_table, 435ef1d9feeSXing Zheng &ctx->lock); 436a245fecbSHeiko Stübner else 437a245fecbSHeiko Stübner clk = clk_register_divider(NULL, list->name, 438a245fecbSHeiko Stübner list->parent_names[0], flags, 439ef1d9feeSXing Zheng ctx->reg_base + list->muxdiv_offset, 440a245fecbSHeiko Stübner list->div_shift, list->div_width, 441ef1d9feeSXing Zheng list->div_flags, &ctx->lock); 442a245fecbSHeiko Stübner break; 443a245fecbSHeiko Stübner case branch_fraction_divider: 444ef1d9feeSXing Zheng clk = rockchip_clk_register_frac_branch(ctx, list->name, 445b2155a71SHeiko Stübner list->parent_names, list->num_parents, 44603ae1747SHeiko Stuebner ctx->reg_base, list->muxdiv_offset, 44703ae1747SHeiko Stuebner list->div_flags, 448b2155a71SHeiko Stübner list->gate_offset, list->gate_shift, 4498ca1ca8fSHeiko Stuebner list->gate_flags, flags, list->child, 450ef1d9feeSXing Zheng &ctx->lock); 451a245fecbSHeiko Stübner break; 452a245fecbSHeiko Stübner case branch_gate: 453a245fecbSHeiko Stübner flags |= CLK_SET_RATE_PARENT; 454a245fecbSHeiko Stübner 455a245fecbSHeiko Stübner clk = clk_register_gate(NULL, list->name, 456a245fecbSHeiko Stübner list->parent_names[0], flags, 457ef1d9feeSXing Zheng ctx->reg_base + list->gate_offset, 458ef1d9feeSXing Zheng list->gate_shift, list->gate_flags, &ctx->lock); 459a245fecbSHeiko Stübner break; 460a245fecbSHeiko Stübner case branch_composite: 461a245fecbSHeiko Stübner clk = rockchip_clk_register_branch(list->name, 462a245fecbSHeiko Stübner list->parent_names, list->num_parents, 46303ae1747SHeiko Stuebner ctx->reg_base, list->muxdiv_offset, 46403ae1747SHeiko Stuebner list->mux_shift, 465a245fecbSHeiko Stübner list->mux_width, list->mux_flags, 466a245fecbSHeiko Stübner list->div_shift, list->div_width, 467a245fecbSHeiko Stübner list->div_flags, list->div_table, 468a245fecbSHeiko Stübner list->gate_offset, list->gate_shift, 469ef1d9feeSXing Zheng list->gate_flags, flags, &ctx->lock); 470a245fecbSHeiko Stübner break; 47189bf26cbSAlexandru M Stan case branch_mmc: 47289bf26cbSAlexandru M Stan clk = rockchip_clk_register_mmc( 47389bf26cbSAlexandru M Stan list->name, 47489bf26cbSAlexandru M Stan list->parent_names, list->num_parents, 475ef1d9feeSXing Zheng ctx->reg_base + list->muxdiv_offset, 47689bf26cbSAlexandru M Stan list->div_shift 47789bf26cbSAlexandru M Stan ); 47889bf26cbSAlexandru M Stan break; 4798a76f443SHeiko Stuebner case branch_inverter: 4808a76f443SHeiko Stuebner clk = rockchip_clk_register_inverter( 4818a76f443SHeiko Stuebner list->name, list->parent_names, 4828a76f443SHeiko Stuebner list->num_parents, 483ef1d9feeSXing Zheng ctx->reg_base + list->muxdiv_offset, 484ef1d9feeSXing Zheng list->div_shift, list->div_flags, &ctx->lock); 4858a76f443SHeiko Stuebner break; 48629a30c26SHeiko Stuebner case branch_factor: 48729a30c26SHeiko Stuebner clk = rockchip_clk_register_factor_branch( 48829a30c26SHeiko Stuebner list->name, list->parent_names, 489ef1d9feeSXing Zheng list->num_parents, ctx->reg_base, 49029a30c26SHeiko Stuebner list->div_shift, list->div_width, 49129a30c26SHeiko Stuebner list->gate_offset, list->gate_shift, 492ef1d9feeSXing Zheng list->gate_flags, flags, &ctx->lock); 49329a30c26SHeiko Stuebner break; 494a4f182bfSLin Huang case branch_ddrclk: 495a4f182bfSLin Huang clk = rockchip_clk_register_ddrclk( 496a4f182bfSLin Huang list->name, list->flags, 497a4f182bfSLin Huang list->parent_names, list->num_parents, 498a4f182bfSLin Huang list->muxdiv_offset, list->mux_shift, 499a4f182bfSLin Huang list->mux_width, list->div_shift, 500a4f182bfSLin Huang list->div_width, list->div_flags, 501a4f182bfSLin Huang ctx->reg_base, &ctx->lock); 502a4f182bfSLin Huang break; 503a245fecbSHeiko Stübner } 504a245fecbSHeiko Stübner 505a245fecbSHeiko Stübner /* none of the cases above matched */ 506a245fecbSHeiko Stübner if (!clk) { 507a245fecbSHeiko Stübner pr_err("%s: unknown clock type %d\n", 508a245fecbSHeiko Stübner __func__, list->branch_type); 509a245fecbSHeiko Stübner continue; 510a245fecbSHeiko Stübner } 511a245fecbSHeiko Stübner 512a245fecbSHeiko Stübner if (IS_ERR(clk)) { 513a245fecbSHeiko Stübner pr_err("%s: failed to register clock %s: %ld\n", 514a245fecbSHeiko Stübner __func__, list->name, PTR_ERR(clk)); 515a245fecbSHeiko Stübner continue; 516a245fecbSHeiko Stübner } 517a245fecbSHeiko Stübner 518ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, clk, list->id); 519a245fecbSHeiko Stübner } 520a245fecbSHeiko Stübner } 521fe94f974SHeiko Stübner 522ef1d9feeSXing Zheng void __init rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx, 523ef1d9feeSXing Zheng unsigned int lookup_id, 5244a1caed3SUwe Kleine-König const char *name, const char *const *parent_names, 525f6fba5f6SHeiko Stuebner u8 num_parents, 526f6fba5f6SHeiko Stuebner const struct rockchip_cpuclk_reg_data *reg_data, 527f6fba5f6SHeiko Stuebner const struct rockchip_cpuclk_rate_table *rates, 528f6fba5f6SHeiko Stuebner int nrates) 529f6fba5f6SHeiko Stuebner { 530f6fba5f6SHeiko Stuebner struct clk *clk; 531f6fba5f6SHeiko Stuebner 532f6fba5f6SHeiko Stuebner clk = rockchip_clk_register_cpuclk(name, parent_names, num_parents, 53303ae1747SHeiko Stuebner reg_data, rates, nrates, 53403ae1747SHeiko Stuebner ctx->reg_base, &ctx->lock); 535f6fba5f6SHeiko Stuebner if (IS_ERR(clk)) { 536f6fba5f6SHeiko Stuebner pr_err("%s: failed to register clock %s: %ld\n", 537f6fba5f6SHeiko Stuebner __func__, name, PTR_ERR(clk)); 538f6fba5f6SHeiko Stuebner return; 539f6fba5f6SHeiko Stuebner } 540f6fba5f6SHeiko Stuebner 541ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, clk, lookup_id); 542f6fba5f6SHeiko Stuebner } 543f6fba5f6SHeiko Stuebner 544692d8328SUwe Kleine-König void __init rockchip_clk_protect_critical(const char *const clocks[], 545692d8328SUwe Kleine-König int nclocks) 546fe94f974SHeiko Stübner { 547fe94f974SHeiko Stübner int i; 548fe94f974SHeiko Stübner 549fe94f974SHeiko Stübner /* Protect the clocks that needs to stay on */ 550fe94f974SHeiko Stübner for (i = 0; i < nclocks; i++) { 551fe94f974SHeiko Stübner struct clk *clk = __clk_lookup(clocks[i]); 552fe94f974SHeiko Stübner 553fe94f974SHeiko Stübner if (clk) 554fe94f974SHeiko Stübner clk_prepare_enable(clk); 555fe94f974SHeiko Stübner } 556fe94f974SHeiko Stübner } 5576f1294b5SHeiko Stübner 558ef1d9feeSXing Zheng static void __iomem *rst_base; 5596f1294b5SHeiko Stübner static unsigned int reg_restart; 560dfff24bdSHeiko Stuebner static void (*cb_restart)(void); 5616f1294b5SHeiko Stübner static int rockchip_restart_notify(struct notifier_block *this, 5626f1294b5SHeiko Stübner unsigned long mode, void *cmd) 5636f1294b5SHeiko Stübner { 564dfff24bdSHeiko Stuebner if (cb_restart) 565dfff24bdSHeiko Stuebner cb_restart(); 566dfff24bdSHeiko Stuebner 567ef1d9feeSXing Zheng writel(0xfdb9, rst_base + reg_restart); 5686f1294b5SHeiko Stübner return NOTIFY_DONE; 5696f1294b5SHeiko Stübner } 5706f1294b5SHeiko Stübner 5716f1294b5SHeiko Stübner static struct notifier_block rockchip_restart_handler = { 5726f1294b5SHeiko Stübner .notifier_call = rockchip_restart_notify, 5736f1294b5SHeiko Stübner .priority = 128, 5746f1294b5SHeiko Stübner }; 5756f1294b5SHeiko Stübner 57603ae1747SHeiko Stuebner void __init 57703ae1747SHeiko Stuebner rockchip_register_restart_notifier(struct rockchip_clk_provider *ctx, 57803ae1747SHeiko Stuebner unsigned int reg, 57903ae1747SHeiko Stuebner void (*cb)(void)) 5806f1294b5SHeiko Stübner { 5816f1294b5SHeiko Stübner int ret; 5826f1294b5SHeiko Stübner 583ef1d9feeSXing Zheng rst_base = ctx->reg_base; 5846f1294b5SHeiko Stübner reg_restart = reg; 585dfff24bdSHeiko Stuebner cb_restart = cb; 5866f1294b5SHeiko Stübner ret = register_restart_handler(&rockchip_restart_handler); 5876f1294b5SHeiko Stübner if (ret) 5886f1294b5SHeiko Stübner pr_err("%s: cannot register restart handler, %d\n", 5896f1294b5SHeiko Stübner __func__, ret); 5906f1294b5SHeiko Stübner } 591