xref: /openbmc/linux/drivers/clk/rockchip/clk.c (revision 5d890c2df900db0197d46ec75383d7633ef41c82)
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>
32*5d890c2dSElaine 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,
49a245fecbSHeiko Stübner 		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;
60a245fecbSHeiko Stübner 
61a245fecbSHeiko Stübner 	if (num_parents > 1) {
62a245fecbSHeiko Stübner 		mux = kzalloc(sizeof(*mux), GFP_KERNEL);
63a245fecbSHeiko Stübner 		if (!mux)
64a245fecbSHeiko Stübner 			return ERR_PTR(-ENOMEM);
65a245fecbSHeiko Stübner 
66a245fecbSHeiko Stübner 		mux->reg = base + muxdiv_offset;
67a245fecbSHeiko Stübner 		mux->shift = mux_shift;
68a245fecbSHeiko Stübner 		mux->mask = BIT(mux_width) - 1;
69a245fecbSHeiko Stübner 		mux->flags = mux_flags;
70a245fecbSHeiko Stübner 		mux->lock = lock;
71a245fecbSHeiko Stübner 		mux_ops = (mux_flags & CLK_MUX_READ_ONLY) ? &clk_mux_ro_ops
72a245fecbSHeiko Stübner 							: &clk_mux_ops;
73a245fecbSHeiko Stübner 	}
74a245fecbSHeiko Stübner 
75a245fecbSHeiko Stübner 	if (gate_offset >= 0) {
76a245fecbSHeiko Stübner 		gate = kzalloc(sizeof(*gate), GFP_KERNEL);
77a245fecbSHeiko Stübner 		if (!gate)
782467b674SShawn Lin 			goto err_gate;
79a245fecbSHeiko Stübner 
80a245fecbSHeiko Stübner 		gate->flags = gate_flags;
81a245fecbSHeiko Stübner 		gate->reg = base + gate_offset;
82a245fecbSHeiko Stübner 		gate->bit_idx = gate_shift;
83a245fecbSHeiko Stübner 		gate->lock = lock;
84a245fecbSHeiko Stübner 		gate_ops = &clk_gate_ops;
85a245fecbSHeiko Stübner 	}
86a245fecbSHeiko Stübner 
87a245fecbSHeiko Stübner 	if (div_width > 0) {
88a245fecbSHeiko Stübner 		div = kzalloc(sizeof(*div), GFP_KERNEL);
89a245fecbSHeiko Stübner 		if (!div)
902467b674SShawn Lin 			goto err_div;
91a245fecbSHeiko Stübner 
92a245fecbSHeiko Stübner 		div->flags = div_flags;
93a245fecbSHeiko Stübner 		div->reg = base + muxdiv_offset;
94a245fecbSHeiko Stübner 		div->shift = div_shift;
95a245fecbSHeiko Stübner 		div->width = div_width;
96a245fecbSHeiko Stübner 		div->lock = lock;
97a245fecbSHeiko Stübner 		div->table = div_table;
9850359819SHeiko Stuebner 		div_ops = (div_flags & CLK_DIVIDER_READ_ONLY)
9950359819SHeiko Stuebner 						? &clk_divider_ro_ops
10050359819SHeiko Stuebner 						: &clk_divider_ops;
101a245fecbSHeiko Stübner 	}
102a245fecbSHeiko Stübner 
103a245fecbSHeiko Stübner 	clk = clk_register_composite(NULL, name, parent_names, num_parents,
104a245fecbSHeiko Stübner 				     mux ? &mux->hw : NULL, mux_ops,
105a245fecbSHeiko Stübner 				     div ? &div->hw : NULL, div_ops,
106a245fecbSHeiko Stübner 				     gate ? &gate->hw : NULL, gate_ops,
107a245fecbSHeiko Stübner 				     flags);
108a245fecbSHeiko Stübner 
109a245fecbSHeiko Stübner 	return clk;
1102467b674SShawn Lin err_div:
1112467b674SShawn Lin 	kfree(gate);
1122467b674SShawn Lin err_gate:
1132467b674SShawn Lin 	kfree(mux);
1142467b674SShawn Lin 	return ERR_PTR(-ENOMEM);
115a245fecbSHeiko Stübner }
116a245fecbSHeiko Stübner 
1178ca1ca8fSHeiko Stuebner struct rockchip_clk_frac {
1188ca1ca8fSHeiko Stuebner 	struct notifier_block			clk_nb;
1198ca1ca8fSHeiko Stuebner 	struct clk_fractional_divider		div;
1208ca1ca8fSHeiko Stuebner 	struct clk_gate				gate;
1218ca1ca8fSHeiko Stuebner 
1228ca1ca8fSHeiko Stuebner 	struct clk_mux				mux;
1238ca1ca8fSHeiko Stuebner 	const struct clk_ops			*mux_ops;
1248ca1ca8fSHeiko Stuebner 	int					mux_frac_idx;
1258ca1ca8fSHeiko Stuebner 
1268ca1ca8fSHeiko Stuebner 	bool					rate_change_remuxed;
1278ca1ca8fSHeiko Stuebner 	int					rate_change_idx;
1288ca1ca8fSHeiko Stuebner };
1298ca1ca8fSHeiko Stuebner 
1308ca1ca8fSHeiko Stuebner #define to_rockchip_clk_frac_nb(nb) \
1318ca1ca8fSHeiko Stuebner 			container_of(nb, struct rockchip_clk_frac, clk_nb)
1328ca1ca8fSHeiko Stuebner 
1338ca1ca8fSHeiko Stuebner static int rockchip_clk_frac_notifier_cb(struct notifier_block *nb,
1348ca1ca8fSHeiko Stuebner 					 unsigned long event, void *data)
1358ca1ca8fSHeiko Stuebner {
1368ca1ca8fSHeiko Stuebner 	struct clk_notifier_data *ndata = data;
1378ca1ca8fSHeiko Stuebner 	struct rockchip_clk_frac *frac = to_rockchip_clk_frac_nb(nb);
1388ca1ca8fSHeiko Stuebner 	struct clk_mux *frac_mux = &frac->mux;
1398ca1ca8fSHeiko Stuebner 	int ret = 0;
1408ca1ca8fSHeiko Stuebner 
1418ca1ca8fSHeiko Stuebner 	pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
1428ca1ca8fSHeiko Stuebner 		 __func__, event, ndata->old_rate, ndata->new_rate);
1438ca1ca8fSHeiko Stuebner 	if (event == PRE_RATE_CHANGE) {
14403ae1747SHeiko Stuebner 		frac->rate_change_idx =
14503ae1747SHeiko Stuebner 				frac->mux_ops->get_parent(&frac_mux->hw);
1468ca1ca8fSHeiko Stuebner 		if (frac->rate_change_idx != frac->mux_frac_idx) {
14703ae1747SHeiko Stuebner 			frac->mux_ops->set_parent(&frac_mux->hw,
14803ae1747SHeiko Stuebner 						  frac->mux_frac_idx);
1498ca1ca8fSHeiko Stuebner 			frac->rate_change_remuxed = 1;
1508ca1ca8fSHeiko Stuebner 		}
1518ca1ca8fSHeiko Stuebner 	} else if (event == POST_RATE_CHANGE) {
1528ca1ca8fSHeiko Stuebner 		/*
1538ca1ca8fSHeiko Stuebner 		 * The POST_RATE_CHANGE notifier runs directly after the
1548ca1ca8fSHeiko Stuebner 		 * divider clock is set in clk_change_rate, so we'll have
1558ca1ca8fSHeiko Stuebner 		 * remuxed back to the original parent before clk_change_rate
1568ca1ca8fSHeiko Stuebner 		 * reaches the mux itself.
1578ca1ca8fSHeiko Stuebner 		 */
1588ca1ca8fSHeiko Stuebner 		if (frac->rate_change_remuxed) {
15903ae1747SHeiko Stuebner 			frac->mux_ops->set_parent(&frac_mux->hw,
16003ae1747SHeiko Stuebner 						  frac->rate_change_idx);
1618ca1ca8fSHeiko Stuebner 			frac->rate_change_remuxed = 0;
1628ca1ca8fSHeiko Stuebner 		}
1638ca1ca8fSHeiko Stuebner 	}
1648ca1ca8fSHeiko Stuebner 
1658ca1ca8fSHeiko Stuebner 	return notifier_from_errno(ret);
1668ca1ca8fSHeiko Stuebner }
1678ca1ca8fSHeiko Stuebner 
168*5d890c2dSElaine Zhang /**
169*5d890c2dSElaine Zhang  * fractional divider must set that denominator is 20 times larger than
170*5d890c2dSElaine Zhang  * numerator to generate precise clock frequency.
171*5d890c2dSElaine Zhang  */
172*5d890c2dSElaine Zhang void rockchip_fractional_approximation(struct clk_hw *hw,
173*5d890c2dSElaine Zhang 		unsigned long rate, unsigned long *parent_rate,
174*5d890c2dSElaine Zhang 		unsigned long *m, unsigned long *n)
175*5d890c2dSElaine Zhang {
176*5d890c2dSElaine Zhang 	struct clk_fractional_divider *fd = to_clk_fd(hw);
177*5d890c2dSElaine Zhang 	unsigned long p_rate, p_parent_rate;
178*5d890c2dSElaine Zhang 	struct clk_hw *p_parent;
179*5d890c2dSElaine Zhang 	unsigned long scale;
180*5d890c2dSElaine Zhang 
181*5d890c2dSElaine Zhang 	p_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
182*5d890c2dSElaine Zhang 	if ((rate * 20 > p_rate) && (p_rate % rate != 0)) {
183*5d890c2dSElaine Zhang 		p_parent = clk_hw_get_parent(clk_hw_get_parent(hw));
184*5d890c2dSElaine Zhang 		p_parent_rate = clk_hw_get_rate(p_parent);
185*5d890c2dSElaine Zhang 		*parent_rate = p_parent_rate;
186*5d890c2dSElaine Zhang 	}
187*5d890c2dSElaine Zhang 
188*5d890c2dSElaine Zhang 	/*
189*5d890c2dSElaine Zhang 	 * Get rate closer to *parent_rate to guarantee there is no overflow
190*5d890c2dSElaine Zhang 	 * for m and n. In the result it will be the nearest rate left shifted
191*5d890c2dSElaine Zhang 	 * by (scale - fd->nwidth) bits.
192*5d890c2dSElaine Zhang 	 */
193*5d890c2dSElaine Zhang 	scale = fls_long(*parent_rate / rate - 1);
194*5d890c2dSElaine Zhang 	if (scale > fd->nwidth)
195*5d890c2dSElaine Zhang 		rate <<= scale - fd->nwidth;
196*5d890c2dSElaine Zhang 
197*5d890c2dSElaine Zhang 	rational_best_approximation(rate, *parent_rate,
198*5d890c2dSElaine Zhang 			GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
199*5d890c2dSElaine Zhang 			m, n);
200*5d890c2dSElaine Zhang }
201*5d890c2dSElaine Zhang 
202ef1d9feeSXing Zheng static struct clk *rockchip_clk_register_frac_branch(
203ef1d9feeSXing Zheng 		struct rockchip_clk_provider *ctx, const char *name,
2044a1caed3SUwe Kleine-König 		const char *const *parent_names, u8 num_parents,
2054a1caed3SUwe Kleine-König 		void __iomem *base, int muxdiv_offset, u8 div_flags,
206b2155a71SHeiko Stübner 		int gate_offset, u8 gate_shift, u8 gate_flags,
2078ca1ca8fSHeiko Stuebner 		unsigned long flags, struct rockchip_clk_branch *child,
2088ca1ca8fSHeiko Stuebner 		spinlock_t *lock)
209b2155a71SHeiko Stübner {
2108ca1ca8fSHeiko Stuebner 	struct rockchip_clk_frac *frac;
211b2155a71SHeiko Stübner 	struct clk *clk;
212b2155a71SHeiko Stübner 	struct clk_gate *gate = NULL;
213b2155a71SHeiko Stübner 	struct clk_fractional_divider *div = NULL;
214b2155a71SHeiko Stübner 	const struct clk_ops *div_ops = NULL, *gate_ops = NULL;
215b2155a71SHeiko Stübner 
2168ca1ca8fSHeiko Stuebner 	if (muxdiv_offset < 0)
2178ca1ca8fSHeiko Stuebner 		return ERR_PTR(-EINVAL);
2188ca1ca8fSHeiko Stuebner 
2198ca1ca8fSHeiko Stuebner 	if (child && child->branch_type != branch_mux) {
2208ca1ca8fSHeiko Stuebner 		pr_err("%s: fractional child clock for %s can only be a mux\n",
2218ca1ca8fSHeiko Stuebner 		       __func__, name);
2228ca1ca8fSHeiko Stuebner 		return ERR_PTR(-EINVAL);
2238ca1ca8fSHeiko Stuebner 	}
2248ca1ca8fSHeiko Stuebner 
2258ca1ca8fSHeiko Stuebner 	frac = kzalloc(sizeof(*frac), GFP_KERNEL);
2268ca1ca8fSHeiko Stuebner 	if (!frac)
227b2155a71SHeiko Stübner 		return ERR_PTR(-ENOMEM);
228b2155a71SHeiko Stübner 
2298ca1ca8fSHeiko Stuebner 	if (gate_offset >= 0) {
2308ca1ca8fSHeiko Stuebner 		gate = &frac->gate;
231b2155a71SHeiko Stübner 		gate->flags = gate_flags;
232b2155a71SHeiko Stübner 		gate->reg = base + gate_offset;
233b2155a71SHeiko Stübner 		gate->bit_idx = gate_shift;
234b2155a71SHeiko Stübner 		gate->lock = lock;
235b2155a71SHeiko Stübner 		gate_ops = &clk_gate_ops;
236b2155a71SHeiko Stübner 	}
237b2155a71SHeiko Stübner 
2388ca1ca8fSHeiko Stuebner 	div = &frac->div;
239b2155a71SHeiko Stübner 	div->flags = div_flags;
240b2155a71SHeiko Stübner 	div->reg = base + muxdiv_offset;
241b2155a71SHeiko Stübner 	div->mshift = 16;
2425d49a6e1SAndy Shevchenko 	div->mwidth = 16;
2435d49a6e1SAndy Shevchenko 	div->mmask = GENMASK(div->mwidth - 1, 0) << div->mshift;
244b2155a71SHeiko Stübner 	div->nshift = 0;
2455d49a6e1SAndy Shevchenko 	div->nwidth = 16;
2465d49a6e1SAndy Shevchenko 	div->nmask = GENMASK(div->nwidth - 1, 0) << div->nshift;
247b2155a71SHeiko Stübner 	div->lock = lock;
248*5d890c2dSElaine Zhang 	div->approximation = rockchip_fractional_approximation;
249b2155a71SHeiko Stübner 	div_ops = &clk_fractional_divider_ops;
250b2155a71SHeiko Stübner 
251b2155a71SHeiko Stübner 	clk = clk_register_composite(NULL, name, parent_names, num_parents,
252b2155a71SHeiko Stübner 				     NULL, NULL,
253b2155a71SHeiko Stübner 				     &div->hw, div_ops,
254b2155a71SHeiko Stübner 				     gate ? &gate->hw : NULL, gate_ops,
2558ca1ca8fSHeiko Stuebner 				     flags | CLK_SET_RATE_UNGATE);
2568ca1ca8fSHeiko Stuebner 	if (IS_ERR(clk)) {
2578ca1ca8fSHeiko Stuebner 		kfree(frac);
2588ca1ca8fSHeiko Stuebner 		return clk;
2598ca1ca8fSHeiko Stuebner 	}
2608ca1ca8fSHeiko Stuebner 
2618ca1ca8fSHeiko Stuebner 	if (child) {
2628ca1ca8fSHeiko Stuebner 		struct clk_mux *frac_mux = &frac->mux;
2638ca1ca8fSHeiko Stuebner 		struct clk_init_data init;
2648ca1ca8fSHeiko Stuebner 		struct clk *mux_clk;
2658ca1ca8fSHeiko Stuebner 		int i, ret;
2668ca1ca8fSHeiko Stuebner 
2678ca1ca8fSHeiko Stuebner 		frac->mux_frac_idx = -1;
2688ca1ca8fSHeiko Stuebner 		for (i = 0; i < child->num_parents; i++) {
2698ca1ca8fSHeiko Stuebner 			if (!strcmp(name, child->parent_names[i])) {
2708ca1ca8fSHeiko Stuebner 				pr_debug("%s: found fractional parent in mux at pos %d\n",
2718ca1ca8fSHeiko Stuebner 					 __func__, i);
2728ca1ca8fSHeiko Stuebner 				frac->mux_frac_idx = i;
2738ca1ca8fSHeiko Stuebner 				break;
2748ca1ca8fSHeiko Stuebner 			}
2758ca1ca8fSHeiko Stuebner 		}
2768ca1ca8fSHeiko Stuebner 
2778ca1ca8fSHeiko Stuebner 		frac->mux_ops = &clk_mux_ops;
2788ca1ca8fSHeiko Stuebner 		frac->clk_nb.notifier_call = rockchip_clk_frac_notifier_cb;
2798ca1ca8fSHeiko Stuebner 
2808ca1ca8fSHeiko Stuebner 		frac_mux->reg = base + child->muxdiv_offset;
2818ca1ca8fSHeiko Stuebner 		frac_mux->shift = child->mux_shift;
2828ca1ca8fSHeiko Stuebner 		frac_mux->mask = BIT(child->mux_width) - 1;
2838ca1ca8fSHeiko Stuebner 		frac_mux->flags = child->mux_flags;
2848ca1ca8fSHeiko Stuebner 		frac_mux->lock = lock;
2858ca1ca8fSHeiko Stuebner 		frac_mux->hw.init = &init;
2868ca1ca8fSHeiko Stuebner 
2878ca1ca8fSHeiko Stuebner 		init.name = child->name;
2888ca1ca8fSHeiko Stuebner 		init.flags = child->flags | CLK_SET_RATE_PARENT;
2898ca1ca8fSHeiko Stuebner 		init.ops = frac->mux_ops;
2908ca1ca8fSHeiko Stuebner 		init.parent_names = child->parent_names;
2918ca1ca8fSHeiko Stuebner 		init.num_parents = child->num_parents;
2928ca1ca8fSHeiko Stuebner 
2938ca1ca8fSHeiko Stuebner 		mux_clk = clk_register(NULL, &frac_mux->hw);
2948ca1ca8fSHeiko Stuebner 		if (IS_ERR(mux_clk))
2958ca1ca8fSHeiko Stuebner 			return clk;
2968ca1ca8fSHeiko Stuebner 
297ef1d9feeSXing Zheng 		rockchip_clk_add_lookup(ctx, mux_clk, child->id);
2988ca1ca8fSHeiko Stuebner 
2998ca1ca8fSHeiko Stuebner 		/* notifier on the fraction divider to catch rate changes */
3008ca1ca8fSHeiko Stuebner 		if (frac->mux_frac_idx >= 0) {
3018ca1ca8fSHeiko Stuebner 			ret = clk_notifier_register(clk, &frac->clk_nb);
3028ca1ca8fSHeiko Stuebner 			if (ret)
3038ca1ca8fSHeiko Stuebner 				pr_err("%s: failed to register clock notifier for %s\n",
3048ca1ca8fSHeiko Stuebner 						__func__, name);
3058ca1ca8fSHeiko Stuebner 		} else {
3068ca1ca8fSHeiko Stuebner 			pr_warn("%s: could not find %s as parent of %s, rate changes may not work\n",
3078ca1ca8fSHeiko Stuebner 				__func__, name, child->name);
3088ca1ca8fSHeiko Stuebner 		}
3098ca1ca8fSHeiko Stuebner 	}
310b2155a71SHeiko Stübner 
311b2155a71SHeiko Stübner 	return clk;
312b2155a71SHeiko Stübner }
313b2155a71SHeiko Stübner 
31429a30c26SHeiko Stuebner static struct clk *rockchip_clk_register_factor_branch(const char *name,
31529a30c26SHeiko Stuebner 		const char *const *parent_names, u8 num_parents,
31629a30c26SHeiko Stuebner 		void __iomem *base, unsigned int mult, unsigned int div,
31729a30c26SHeiko Stuebner 		int gate_offset, u8 gate_shift, u8 gate_flags,
31829a30c26SHeiko Stuebner 		unsigned long flags, spinlock_t *lock)
31929a30c26SHeiko Stuebner {
32029a30c26SHeiko Stuebner 	struct clk *clk;
32129a30c26SHeiko Stuebner 	struct clk_gate *gate = NULL;
32229a30c26SHeiko Stuebner 	struct clk_fixed_factor *fix = NULL;
32329a30c26SHeiko Stuebner 
32429a30c26SHeiko Stuebner 	/* without gate, register a simple factor clock */
32529a30c26SHeiko Stuebner 	if (gate_offset == 0) {
32629a30c26SHeiko Stuebner 		return clk_register_fixed_factor(NULL, name,
32729a30c26SHeiko Stuebner 				parent_names[0], flags, mult,
32829a30c26SHeiko Stuebner 				div);
32929a30c26SHeiko Stuebner 	}
33029a30c26SHeiko Stuebner 
33129a30c26SHeiko Stuebner 	gate = kzalloc(sizeof(*gate), GFP_KERNEL);
33229a30c26SHeiko Stuebner 	if (!gate)
33329a30c26SHeiko Stuebner 		return ERR_PTR(-ENOMEM);
33429a30c26SHeiko Stuebner 
33529a30c26SHeiko Stuebner 	gate->flags = gate_flags;
33629a30c26SHeiko Stuebner 	gate->reg = base + gate_offset;
33729a30c26SHeiko Stuebner 	gate->bit_idx = gate_shift;
33829a30c26SHeiko Stuebner 	gate->lock = lock;
33929a30c26SHeiko Stuebner 
34029a30c26SHeiko Stuebner 	fix = kzalloc(sizeof(*fix), GFP_KERNEL);
34129a30c26SHeiko Stuebner 	if (!fix) {
34229a30c26SHeiko Stuebner 		kfree(gate);
34329a30c26SHeiko Stuebner 		return ERR_PTR(-ENOMEM);
34429a30c26SHeiko Stuebner 	}
34529a30c26SHeiko Stuebner 
34629a30c26SHeiko Stuebner 	fix->mult = mult;
34729a30c26SHeiko Stuebner 	fix->div = div;
34829a30c26SHeiko Stuebner 
34929a30c26SHeiko Stuebner 	clk = clk_register_composite(NULL, name, parent_names, num_parents,
35029a30c26SHeiko Stuebner 				     NULL, NULL,
35129a30c26SHeiko Stuebner 				     &fix->hw, &clk_fixed_factor_ops,
35229a30c26SHeiko Stuebner 				     &gate->hw, &clk_gate_ops, flags);
35329a30c26SHeiko Stuebner 	if (IS_ERR(clk)) {
35429a30c26SHeiko Stuebner 		kfree(fix);
35529a30c26SHeiko Stuebner 		kfree(gate);
35629a30c26SHeiko Stuebner 	}
35729a30c26SHeiko Stuebner 
35829a30c26SHeiko Stuebner 	return clk;
35929a30c26SHeiko Stuebner }
36029a30c26SHeiko Stuebner 
361ef1d9feeSXing Zheng struct rockchip_clk_provider * __init rockchip_clk_init(struct device_node *np,
362ef1d9feeSXing Zheng 			void __iomem *base, unsigned long nr_clks)
363a245fecbSHeiko Stübner {
364ef1d9feeSXing Zheng 	struct rockchip_clk_provider *ctx;
365ef1d9feeSXing Zheng 	struct clk **clk_table;
366ef1d9feeSXing Zheng 	int i;
367ef1d9feeSXing Zheng 
368ef1d9feeSXing Zheng 	ctx = kzalloc(sizeof(struct rockchip_clk_provider), GFP_KERNEL);
36903ae1747SHeiko Stuebner 	if (!ctx)
370ef1d9feeSXing Zheng 		return ERR_PTR(-ENOMEM);
371a245fecbSHeiko Stübner 
372a245fecbSHeiko Stübner 	clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL);
37303ae1747SHeiko Stuebner 	if (!clk_table)
374ef1d9feeSXing Zheng 		goto err_free;
375a245fecbSHeiko Stübner 
376ef1d9feeSXing Zheng 	for (i = 0; i < nr_clks; ++i)
377ef1d9feeSXing Zheng 		clk_table[i] = ERR_PTR(-ENOENT);
378ef1d9feeSXing Zheng 
379ef1d9feeSXing Zheng 	ctx->reg_base = base;
380ef1d9feeSXing Zheng 	ctx->clk_data.clks = clk_table;
381ef1d9feeSXing Zheng 	ctx->clk_data.clk_num = nr_clks;
382ef1d9feeSXing Zheng 	ctx->cru_node = np;
383ef1d9feeSXing Zheng 	spin_lock_init(&ctx->lock);
384ef1d9feeSXing Zheng 
3856f339dc2SHeiko Stuebner 	ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node,
3866f339dc2SHeiko Stuebner 						   "rockchip,grf");
3876f339dc2SHeiko Stuebner 
388ef1d9feeSXing Zheng 	return ctx;
389ef1d9feeSXing Zheng 
390ef1d9feeSXing Zheng err_free:
391ef1d9feeSXing Zheng 	kfree(ctx);
392ef1d9feeSXing Zheng 	return ERR_PTR(-ENOMEM);
393ef1d9feeSXing Zheng }
394ef1d9feeSXing Zheng 
395ef1d9feeSXing Zheng void __init rockchip_clk_of_add_provider(struct device_node *np,
396ef1d9feeSXing Zheng 				struct rockchip_clk_provider *ctx)
39790c59025SHeiko Stübner {
398ef1d9feeSXing Zheng 	if (of_clk_add_provider(np, of_clk_src_onecell_get,
399ef1d9feeSXing Zheng 				&ctx->clk_data))
400ef1d9feeSXing Zheng 		pr_err("%s: could not register clk provider\n", __func__);
401ef1d9feeSXing Zheng }
40290c59025SHeiko Stübner 
403ef1d9feeSXing Zheng void rockchip_clk_add_lookup(struct rockchip_clk_provider *ctx,
404ef1d9feeSXing Zheng 			     struct clk *clk, unsigned int id)
405ef1d9feeSXing Zheng {
406ef1d9feeSXing Zheng 	if (ctx->clk_data.clks && id)
407ef1d9feeSXing Zheng 		ctx->clk_data.clks[id] = clk;
408ef1d9feeSXing Zheng }
409ef1d9feeSXing Zheng 
410ef1d9feeSXing Zheng void __init rockchip_clk_register_plls(struct rockchip_clk_provider *ctx,
411ef1d9feeSXing Zheng 				struct rockchip_pll_clock *list,
41290c59025SHeiko Stübner 				unsigned int nr_pll, int grf_lock_offset)
41390c59025SHeiko Stübner {
41490c59025SHeiko Stübner 	struct clk *clk;
41590c59025SHeiko Stübner 	int idx;
41690c59025SHeiko Stübner 
41790c59025SHeiko Stübner 	for (idx = 0; idx < nr_pll; idx++, list++) {
418ef1d9feeSXing Zheng 		clk = rockchip_clk_register_pll(ctx, list->type, list->name,
41990c59025SHeiko Stübner 				list->parent_names, list->num_parents,
420ef1d9feeSXing Zheng 				list->con_offset, grf_lock_offset,
42190c59025SHeiko Stübner 				list->lock_shift, list->mode_offset,
4224f8a7c54SHeiko Stuebner 				list->mode_shift, list->rate_table,
423e6cebc72SHeiko Stübner 				list->flags, list->pll_flags);
42490c59025SHeiko Stübner 		if (IS_ERR(clk)) {
42590c59025SHeiko Stübner 			pr_err("%s: failed to register clock %s\n", __func__,
42690c59025SHeiko Stübner 				list->name);
42790c59025SHeiko Stübner 			continue;
42890c59025SHeiko Stübner 		}
42990c59025SHeiko Stübner 
430ef1d9feeSXing Zheng 		rockchip_clk_add_lookup(ctx, clk, list->id);
43190c59025SHeiko Stübner 	}
43290c59025SHeiko Stübner }
43390c59025SHeiko Stübner 
434a245fecbSHeiko Stübner void __init rockchip_clk_register_branches(
435ef1d9feeSXing Zheng 				      struct rockchip_clk_provider *ctx,
436a245fecbSHeiko Stübner 				      struct rockchip_clk_branch *list,
437a245fecbSHeiko Stübner 				      unsigned int nr_clk)
438a245fecbSHeiko Stübner {
439a245fecbSHeiko Stübner 	struct clk *clk = NULL;
440a245fecbSHeiko Stübner 	unsigned int idx;
441a245fecbSHeiko Stübner 	unsigned long flags;
442a245fecbSHeiko Stübner 
443a245fecbSHeiko Stübner 	for (idx = 0; idx < nr_clk; idx++, list++) {
444a245fecbSHeiko Stübner 		flags = list->flags;
445a245fecbSHeiko Stübner 
446a245fecbSHeiko Stübner 		/* catch simple muxes */
447a245fecbSHeiko Stübner 		switch (list->branch_type) {
448a245fecbSHeiko Stübner 		case branch_mux:
449a245fecbSHeiko Stübner 			clk = clk_register_mux(NULL, list->name,
450a245fecbSHeiko Stübner 				list->parent_names, list->num_parents,
451ef1d9feeSXing Zheng 				flags, ctx->reg_base + list->muxdiv_offset,
452a245fecbSHeiko Stübner 				list->mux_shift, list->mux_width,
453ef1d9feeSXing Zheng 				list->mux_flags, &ctx->lock);
454a245fecbSHeiko Stübner 			break;
455cb1d9f6dSHeiko Stuebner 		case branch_muxgrf:
456cb1d9f6dSHeiko Stuebner 			clk = rockchip_clk_register_muxgrf(list->name,
457cb1d9f6dSHeiko Stuebner 				list->parent_names, list->num_parents,
458cb1d9f6dSHeiko Stuebner 				flags, ctx->grf, list->muxdiv_offset,
459cb1d9f6dSHeiko Stuebner 				list->mux_shift, list->mux_width,
460cb1d9f6dSHeiko Stuebner 				list->mux_flags);
461cb1d9f6dSHeiko Stuebner 			break;
462a245fecbSHeiko Stübner 		case branch_divider:
463a245fecbSHeiko Stübner 			if (list->div_table)
464a245fecbSHeiko Stübner 				clk = clk_register_divider_table(NULL,
465a245fecbSHeiko Stübner 					list->name, list->parent_names[0],
46603ae1747SHeiko Stuebner 					flags,
46703ae1747SHeiko Stuebner 					ctx->reg_base + list->muxdiv_offset,
468a245fecbSHeiko Stübner 					list->div_shift, list->div_width,
469a245fecbSHeiko Stübner 					list->div_flags, list->div_table,
470ef1d9feeSXing Zheng 					&ctx->lock);
471a245fecbSHeiko Stübner 			else
472a245fecbSHeiko Stübner 				clk = clk_register_divider(NULL, list->name,
473a245fecbSHeiko Stübner 					list->parent_names[0], flags,
474ef1d9feeSXing Zheng 					ctx->reg_base + list->muxdiv_offset,
475a245fecbSHeiko Stübner 					list->div_shift, list->div_width,
476ef1d9feeSXing Zheng 					list->div_flags, &ctx->lock);
477a245fecbSHeiko Stübner 			break;
478a245fecbSHeiko Stübner 		case branch_fraction_divider:
479ef1d9feeSXing Zheng 			clk = rockchip_clk_register_frac_branch(ctx, list->name,
480b2155a71SHeiko Stübner 				list->parent_names, list->num_parents,
48103ae1747SHeiko Stuebner 				ctx->reg_base, list->muxdiv_offset,
48203ae1747SHeiko Stuebner 				list->div_flags,
483b2155a71SHeiko Stübner 				list->gate_offset, list->gate_shift,
4848ca1ca8fSHeiko Stuebner 				list->gate_flags, flags, list->child,
485ef1d9feeSXing Zheng 				&ctx->lock);
486a245fecbSHeiko Stübner 			break;
487a245fecbSHeiko Stübner 		case branch_gate:
488a245fecbSHeiko Stübner 			flags |= CLK_SET_RATE_PARENT;
489a245fecbSHeiko Stübner 
490a245fecbSHeiko Stübner 			clk = clk_register_gate(NULL, list->name,
491a245fecbSHeiko Stübner 				list->parent_names[0], flags,
492ef1d9feeSXing Zheng 				ctx->reg_base + list->gate_offset,
493ef1d9feeSXing Zheng 				list->gate_shift, list->gate_flags, &ctx->lock);
494a245fecbSHeiko Stübner 			break;
495a245fecbSHeiko Stübner 		case branch_composite:
496a245fecbSHeiko Stübner 			clk = rockchip_clk_register_branch(list->name,
497a245fecbSHeiko Stübner 				list->parent_names, list->num_parents,
49803ae1747SHeiko Stuebner 				ctx->reg_base, list->muxdiv_offset,
49903ae1747SHeiko Stuebner 				list->mux_shift,
500a245fecbSHeiko Stübner 				list->mux_width, list->mux_flags,
501a245fecbSHeiko Stübner 				list->div_shift, list->div_width,
502a245fecbSHeiko Stübner 				list->div_flags, list->div_table,
503a245fecbSHeiko Stübner 				list->gate_offset, list->gate_shift,
504ef1d9feeSXing Zheng 				list->gate_flags, flags, &ctx->lock);
505a245fecbSHeiko Stübner 			break;
50689bf26cbSAlexandru M Stan 		case branch_mmc:
50789bf26cbSAlexandru M Stan 			clk = rockchip_clk_register_mmc(
50889bf26cbSAlexandru M Stan 				list->name,
50989bf26cbSAlexandru M Stan 				list->parent_names, list->num_parents,
510ef1d9feeSXing Zheng 				ctx->reg_base + list->muxdiv_offset,
51189bf26cbSAlexandru M Stan 				list->div_shift
51289bf26cbSAlexandru M Stan 			);
51389bf26cbSAlexandru M Stan 			break;
5148a76f443SHeiko Stuebner 		case branch_inverter:
5158a76f443SHeiko Stuebner 			clk = rockchip_clk_register_inverter(
5168a76f443SHeiko Stuebner 				list->name, list->parent_names,
5178a76f443SHeiko Stuebner 				list->num_parents,
518ef1d9feeSXing Zheng 				ctx->reg_base + list->muxdiv_offset,
519ef1d9feeSXing Zheng 				list->div_shift, list->div_flags, &ctx->lock);
5208a76f443SHeiko Stuebner 			break;
52129a30c26SHeiko Stuebner 		case branch_factor:
52229a30c26SHeiko Stuebner 			clk = rockchip_clk_register_factor_branch(
52329a30c26SHeiko Stuebner 				list->name, list->parent_names,
524ef1d9feeSXing Zheng 				list->num_parents, ctx->reg_base,
52529a30c26SHeiko Stuebner 				list->div_shift, list->div_width,
52629a30c26SHeiko Stuebner 				list->gate_offset, list->gate_shift,
527ef1d9feeSXing Zheng 				list->gate_flags, flags, &ctx->lock);
52829a30c26SHeiko Stuebner 			break;
529a4f182bfSLin Huang 		case branch_ddrclk:
530a4f182bfSLin Huang 			clk = rockchip_clk_register_ddrclk(
531a4f182bfSLin Huang 				list->name, list->flags,
532a4f182bfSLin Huang 				list->parent_names, list->num_parents,
533a4f182bfSLin Huang 				list->muxdiv_offset, list->mux_shift,
534a4f182bfSLin Huang 				list->mux_width, list->div_shift,
535a4f182bfSLin Huang 				list->div_width, list->div_flags,
536a4f182bfSLin Huang 				ctx->reg_base, &ctx->lock);
537a4f182bfSLin Huang 			break;
538a245fecbSHeiko Stübner 		}
539a245fecbSHeiko Stübner 
540a245fecbSHeiko Stübner 		/* none of the cases above matched */
541a245fecbSHeiko Stübner 		if (!clk) {
542a245fecbSHeiko Stübner 			pr_err("%s: unknown clock type %d\n",
543a245fecbSHeiko Stübner 			       __func__, list->branch_type);
544a245fecbSHeiko Stübner 			continue;
545a245fecbSHeiko Stübner 		}
546a245fecbSHeiko Stübner 
547a245fecbSHeiko Stübner 		if (IS_ERR(clk)) {
548a245fecbSHeiko Stübner 			pr_err("%s: failed to register clock %s: %ld\n",
549a245fecbSHeiko Stübner 			       __func__, list->name, PTR_ERR(clk));
550a245fecbSHeiko Stübner 			continue;
551a245fecbSHeiko Stübner 		}
552a245fecbSHeiko Stübner 
553ef1d9feeSXing Zheng 		rockchip_clk_add_lookup(ctx, clk, list->id);
554a245fecbSHeiko Stübner 	}
555a245fecbSHeiko Stübner }
556fe94f974SHeiko Stübner 
557ef1d9feeSXing Zheng void __init rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx,
558ef1d9feeSXing Zheng 			unsigned int lookup_id,
5594a1caed3SUwe Kleine-König 			const char *name, const char *const *parent_names,
560f6fba5f6SHeiko Stuebner 			u8 num_parents,
561f6fba5f6SHeiko Stuebner 			const struct rockchip_cpuclk_reg_data *reg_data,
562f6fba5f6SHeiko Stuebner 			const struct rockchip_cpuclk_rate_table *rates,
563f6fba5f6SHeiko Stuebner 			int nrates)
564f6fba5f6SHeiko Stuebner {
565f6fba5f6SHeiko Stuebner 	struct clk *clk;
566f6fba5f6SHeiko Stuebner 
567f6fba5f6SHeiko Stuebner 	clk = rockchip_clk_register_cpuclk(name, parent_names, num_parents,
56803ae1747SHeiko Stuebner 					   reg_data, rates, nrates,
56903ae1747SHeiko Stuebner 					   ctx->reg_base, &ctx->lock);
570f6fba5f6SHeiko Stuebner 	if (IS_ERR(clk)) {
571f6fba5f6SHeiko Stuebner 		pr_err("%s: failed to register clock %s: %ld\n",
572f6fba5f6SHeiko Stuebner 		       __func__, name, PTR_ERR(clk));
573f6fba5f6SHeiko Stuebner 		return;
574f6fba5f6SHeiko Stuebner 	}
575f6fba5f6SHeiko Stuebner 
576ef1d9feeSXing Zheng 	rockchip_clk_add_lookup(ctx, clk, lookup_id);
577f6fba5f6SHeiko Stuebner }
578f6fba5f6SHeiko Stuebner 
579692d8328SUwe Kleine-König void __init rockchip_clk_protect_critical(const char *const clocks[],
580692d8328SUwe Kleine-König 					  int nclocks)
581fe94f974SHeiko Stübner {
582fe94f974SHeiko Stübner 	int i;
583fe94f974SHeiko Stübner 
584fe94f974SHeiko Stübner 	/* Protect the clocks that needs to stay on */
585fe94f974SHeiko Stübner 	for (i = 0; i < nclocks; i++) {
586fe94f974SHeiko Stübner 		struct clk *clk = __clk_lookup(clocks[i]);
587fe94f974SHeiko Stübner 
588fe94f974SHeiko Stübner 		if (clk)
589fe94f974SHeiko Stübner 			clk_prepare_enable(clk);
590fe94f974SHeiko Stübner 	}
591fe94f974SHeiko Stübner }
5926f1294b5SHeiko Stübner 
593ef1d9feeSXing Zheng static void __iomem *rst_base;
5946f1294b5SHeiko Stübner static unsigned int reg_restart;
595dfff24bdSHeiko Stuebner static void (*cb_restart)(void);
5966f1294b5SHeiko Stübner static int rockchip_restart_notify(struct notifier_block *this,
5976f1294b5SHeiko Stübner 				   unsigned long mode, void *cmd)
5986f1294b5SHeiko Stübner {
599dfff24bdSHeiko Stuebner 	if (cb_restart)
600dfff24bdSHeiko Stuebner 		cb_restart();
601dfff24bdSHeiko Stuebner 
602ef1d9feeSXing Zheng 	writel(0xfdb9, rst_base + reg_restart);
6036f1294b5SHeiko Stübner 	return NOTIFY_DONE;
6046f1294b5SHeiko Stübner }
6056f1294b5SHeiko Stübner 
6066f1294b5SHeiko Stübner static struct notifier_block rockchip_restart_handler = {
6076f1294b5SHeiko Stübner 	.notifier_call = rockchip_restart_notify,
6086f1294b5SHeiko Stübner 	.priority = 128,
6096f1294b5SHeiko Stübner };
6106f1294b5SHeiko Stübner 
61103ae1747SHeiko Stuebner void __init
61203ae1747SHeiko Stuebner rockchip_register_restart_notifier(struct rockchip_clk_provider *ctx,
61303ae1747SHeiko Stuebner 					       unsigned int reg,
61403ae1747SHeiko Stuebner 					       void (*cb)(void))
6156f1294b5SHeiko Stübner {
6166f1294b5SHeiko Stübner 	int ret;
6176f1294b5SHeiko Stübner 
618ef1d9feeSXing Zheng 	rst_base = ctx->reg_base;
6196f1294b5SHeiko Stübner 	reg_restart = reg;
620dfff24bdSHeiko Stuebner 	cb_restart = cb;
6216f1294b5SHeiko Stübner 	ret = register_restart_handler(&rockchip_restart_handler);
6226f1294b5SHeiko Stübner 	if (ret)
6236f1294b5SHeiko Stübner 		pr_err("%s: cannot register restart handler, %d\n",
6246f1294b5SHeiko Stübner 		       __func__, ret);
6256f1294b5SHeiko Stübner }
626