xref: /openbmc/linux/drivers/clk/rockchip/clk.c (revision 03ae1747869437a8e4d0d4e79d4c88c25c6df39c)
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,
45*03ae1747SHeiko Stuebner 		const char *const *parent_names, u8 num_parents,
46*03ae1747SHeiko 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) {
143*03ae1747SHeiko Stuebner 		frac->rate_change_idx =
144*03ae1747SHeiko Stuebner 				frac->mux_ops->get_parent(&frac_mux->hw);
1458ca1ca8fSHeiko Stuebner 		if (frac->rate_change_idx != frac->mux_frac_idx) {
146*03ae1747SHeiko Stuebner 			frac->mux_ops->set_parent(&frac_mux->hw,
147*03ae1747SHeiko 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) {
158*03ae1747SHeiko Stuebner 			frac->mux_ops->set_parent(&frac_mux->hw,
159*03ae1747SHeiko 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);
333*03ae1747SHeiko 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);
337*03ae1747SHeiko 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 
350ef1d9feeSXing Zheng 	return ctx;
351ef1d9feeSXing Zheng 
352ef1d9feeSXing Zheng err_free:
353ef1d9feeSXing Zheng 	kfree(ctx);
354ef1d9feeSXing Zheng 	return ERR_PTR(-ENOMEM);
355ef1d9feeSXing Zheng }
356ef1d9feeSXing Zheng 
357ef1d9feeSXing Zheng void __init rockchip_clk_of_add_provider(struct device_node *np,
358ef1d9feeSXing Zheng 				struct rockchip_clk_provider *ctx)
35990c59025SHeiko Stübner {
360ef1d9feeSXing Zheng 	if (of_clk_add_provider(np, of_clk_src_onecell_get,
361ef1d9feeSXing Zheng 				&ctx->clk_data))
362ef1d9feeSXing Zheng 		pr_err("%s: could not register clk provider\n", __func__);
363ef1d9feeSXing Zheng }
36490c59025SHeiko Stübner 
365ef1d9feeSXing Zheng struct regmap *rockchip_clk_get_grf(struct rockchip_clk_provider *ctx)
366a245fecbSHeiko Stübner {
367ef1d9feeSXing Zheng 	if (IS_ERR(ctx->grf))
368*03ae1747SHeiko Stuebner 		ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node,
369*03ae1747SHeiko Stuebner 							   "rockchip,grf");
370ef1d9feeSXing Zheng 	return ctx->grf;
371a245fecbSHeiko Stübner }
372a245fecbSHeiko Stübner 
373ef1d9feeSXing Zheng void rockchip_clk_add_lookup(struct rockchip_clk_provider *ctx,
374ef1d9feeSXing Zheng 			     struct clk *clk, unsigned int id)
375ef1d9feeSXing Zheng {
376ef1d9feeSXing Zheng 	if (ctx->clk_data.clks && id)
377ef1d9feeSXing Zheng 		ctx->clk_data.clks[id] = clk;
378ef1d9feeSXing Zheng }
379ef1d9feeSXing Zheng 
380ef1d9feeSXing Zheng void __init rockchip_clk_register_plls(struct rockchip_clk_provider *ctx,
381ef1d9feeSXing Zheng 				struct rockchip_pll_clock *list,
38290c59025SHeiko Stübner 				unsigned int nr_pll, int grf_lock_offset)
38390c59025SHeiko Stübner {
38490c59025SHeiko Stübner 	struct clk *clk;
38590c59025SHeiko Stübner 	int idx;
38690c59025SHeiko Stübner 
38790c59025SHeiko Stübner 	for (idx = 0; idx < nr_pll; idx++, list++) {
388ef1d9feeSXing Zheng 		clk = rockchip_clk_register_pll(ctx, list->type, list->name,
38990c59025SHeiko Stübner 				list->parent_names, list->num_parents,
390ef1d9feeSXing Zheng 				list->con_offset, grf_lock_offset,
39190c59025SHeiko Stübner 				list->lock_shift, list->mode_offset,
3924f8a7c54SHeiko Stuebner 				list->mode_shift, list->rate_table,
393ef1d9feeSXing Zheng 				list->pll_flags);
39490c59025SHeiko Stübner 		if (IS_ERR(clk)) {
39590c59025SHeiko Stübner 			pr_err("%s: failed to register clock %s\n", __func__,
39690c59025SHeiko Stübner 				list->name);
39790c59025SHeiko Stübner 			continue;
39890c59025SHeiko Stübner 		}
39990c59025SHeiko Stübner 
400ef1d9feeSXing Zheng 		rockchip_clk_add_lookup(ctx, clk, list->id);
40190c59025SHeiko Stübner 	}
40290c59025SHeiko Stübner }
40390c59025SHeiko Stübner 
404a245fecbSHeiko Stübner void __init rockchip_clk_register_branches(
405ef1d9feeSXing Zheng 				      struct rockchip_clk_provider *ctx,
406a245fecbSHeiko Stübner 				      struct rockchip_clk_branch *list,
407a245fecbSHeiko Stübner 				      unsigned int nr_clk)
408a245fecbSHeiko Stübner {
409a245fecbSHeiko Stübner 	struct clk *clk = NULL;
410a245fecbSHeiko Stübner 	unsigned int idx;
411a245fecbSHeiko Stübner 	unsigned long flags;
412a245fecbSHeiko Stübner 
413a245fecbSHeiko Stübner 	for (idx = 0; idx < nr_clk; idx++, list++) {
414a245fecbSHeiko Stübner 		flags = list->flags;
415a245fecbSHeiko Stübner 
416a245fecbSHeiko Stübner 		/* catch simple muxes */
417a245fecbSHeiko Stübner 		switch (list->branch_type) {
418a245fecbSHeiko Stübner 		case branch_mux:
419a245fecbSHeiko Stübner 			clk = clk_register_mux(NULL, list->name,
420a245fecbSHeiko Stübner 				list->parent_names, list->num_parents,
421ef1d9feeSXing Zheng 				flags, ctx->reg_base + list->muxdiv_offset,
422a245fecbSHeiko Stübner 				list->mux_shift, list->mux_width,
423ef1d9feeSXing Zheng 				list->mux_flags, &ctx->lock);
424a245fecbSHeiko Stübner 			break;
425a245fecbSHeiko Stübner 		case branch_divider:
426a245fecbSHeiko Stübner 			if (list->div_table)
427a245fecbSHeiko Stübner 				clk = clk_register_divider_table(NULL,
428a245fecbSHeiko Stübner 					list->name, list->parent_names[0],
429*03ae1747SHeiko Stuebner 					flags,
430*03ae1747SHeiko Stuebner 					ctx->reg_base + list->muxdiv_offset,
431a245fecbSHeiko Stübner 					list->div_shift, list->div_width,
432a245fecbSHeiko Stübner 					list->div_flags, list->div_table,
433ef1d9feeSXing Zheng 					&ctx->lock);
434a245fecbSHeiko Stübner 			else
435a245fecbSHeiko Stübner 				clk = clk_register_divider(NULL, list->name,
436a245fecbSHeiko Stübner 					list->parent_names[0], flags,
437ef1d9feeSXing Zheng 					ctx->reg_base + list->muxdiv_offset,
438a245fecbSHeiko Stübner 					list->div_shift, list->div_width,
439ef1d9feeSXing Zheng 					list->div_flags, &ctx->lock);
440a245fecbSHeiko Stübner 			break;
441a245fecbSHeiko Stübner 		case branch_fraction_divider:
442ef1d9feeSXing Zheng 			clk = rockchip_clk_register_frac_branch(ctx, list->name,
443b2155a71SHeiko Stübner 				list->parent_names, list->num_parents,
444*03ae1747SHeiko Stuebner 				ctx->reg_base, list->muxdiv_offset,
445*03ae1747SHeiko Stuebner 				list->div_flags,
446b2155a71SHeiko Stübner 				list->gate_offset, list->gate_shift,
4478ca1ca8fSHeiko Stuebner 				list->gate_flags, flags, list->child,
448ef1d9feeSXing Zheng 				&ctx->lock);
449a245fecbSHeiko Stübner 			break;
450a245fecbSHeiko Stübner 		case branch_gate:
451a245fecbSHeiko Stübner 			flags |= CLK_SET_RATE_PARENT;
452a245fecbSHeiko Stübner 
453a245fecbSHeiko Stübner 			clk = clk_register_gate(NULL, list->name,
454a245fecbSHeiko Stübner 				list->parent_names[0], flags,
455ef1d9feeSXing Zheng 				ctx->reg_base + list->gate_offset,
456ef1d9feeSXing Zheng 				list->gate_shift, list->gate_flags, &ctx->lock);
457a245fecbSHeiko Stübner 			break;
458a245fecbSHeiko Stübner 		case branch_composite:
459a245fecbSHeiko Stübner 			clk = rockchip_clk_register_branch(list->name,
460a245fecbSHeiko Stübner 				list->parent_names, list->num_parents,
461*03ae1747SHeiko Stuebner 				ctx->reg_base, list->muxdiv_offset,
462*03ae1747SHeiko Stuebner 				list->mux_shift,
463a245fecbSHeiko Stübner 				list->mux_width, list->mux_flags,
464a245fecbSHeiko Stübner 				list->div_shift, list->div_width,
465a245fecbSHeiko Stübner 				list->div_flags, list->div_table,
466a245fecbSHeiko Stübner 				list->gate_offset, list->gate_shift,
467ef1d9feeSXing Zheng 				list->gate_flags, flags, &ctx->lock);
468a245fecbSHeiko Stübner 			break;
46989bf26cbSAlexandru M Stan 		case branch_mmc:
47089bf26cbSAlexandru M Stan 			clk = rockchip_clk_register_mmc(
47189bf26cbSAlexandru M Stan 				list->name,
47289bf26cbSAlexandru M Stan 				list->parent_names, list->num_parents,
473ef1d9feeSXing Zheng 				ctx->reg_base + list->muxdiv_offset,
47489bf26cbSAlexandru M Stan 				list->div_shift
47589bf26cbSAlexandru M Stan 			);
47689bf26cbSAlexandru M Stan 			break;
4778a76f443SHeiko Stuebner 		case branch_inverter:
4788a76f443SHeiko Stuebner 			clk = rockchip_clk_register_inverter(
4798a76f443SHeiko Stuebner 				list->name, list->parent_names,
4808a76f443SHeiko Stuebner 				list->num_parents,
481ef1d9feeSXing Zheng 				ctx->reg_base + list->muxdiv_offset,
482ef1d9feeSXing Zheng 				list->div_shift, list->div_flags, &ctx->lock);
4838a76f443SHeiko Stuebner 			break;
48429a30c26SHeiko Stuebner 		case branch_factor:
48529a30c26SHeiko Stuebner 			clk = rockchip_clk_register_factor_branch(
48629a30c26SHeiko Stuebner 				list->name, list->parent_names,
487ef1d9feeSXing Zheng 				list->num_parents, ctx->reg_base,
48829a30c26SHeiko Stuebner 				list->div_shift, list->div_width,
48929a30c26SHeiko Stuebner 				list->gate_offset, list->gate_shift,
490ef1d9feeSXing Zheng 				list->gate_flags, flags, &ctx->lock);
49129a30c26SHeiko Stuebner 			break;
492a245fecbSHeiko Stübner 		}
493a245fecbSHeiko Stübner 
494a245fecbSHeiko Stübner 		/* none of the cases above matched */
495a245fecbSHeiko Stübner 		if (!clk) {
496a245fecbSHeiko Stübner 			pr_err("%s: unknown clock type %d\n",
497a245fecbSHeiko Stübner 			       __func__, list->branch_type);
498a245fecbSHeiko Stübner 			continue;
499a245fecbSHeiko Stübner 		}
500a245fecbSHeiko Stübner 
501a245fecbSHeiko Stübner 		if (IS_ERR(clk)) {
502a245fecbSHeiko Stübner 			pr_err("%s: failed to register clock %s: %ld\n",
503a245fecbSHeiko Stübner 			       __func__, list->name, PTR_ERR(clk));
504a245fecbSHeiko Stübner 			continue;
505a245fecbSHeiko Stübner 		}
506a245fecbSHeiko Stübner 
507ef1d9feeSXing Zheng 		rockchip_clk_add_lookup(ctx, clk, list->id);
508a245fecbSHeiko Stübner 	}
509a245fecbSHeiko Stübner }
510fe94f974SHeiko Stübner 
511ef1d9feeSXing Zheng void __init rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx,
512ef1d9feeSXing Zheng 			unsigned int lookup_id,
5134a1caed3SUwe Kleine-König 			const char *name, const char *const *parent_names,
514f6fba5f6SHeiko Stuebner 			u8 num_parents,
515f6fba5f6SHeiko Stuebner 			const struct rockchip_cpuclk_reg_data *reg_data,
516f6fba5f6SHeiko Stuebner 			const struct rockchip_cpuclk_rate_table *rates,
517f6fba5f6SHeiko Stuebner 			int nrates)
518f6fba5f6SHeiko Stuebner {
519f6fba5f6SHeiko Stuebner 	struct clk *clk;
520f6fba5f6SHeiko Stuebner 
521f6fba5f6SHeiko Stuebner 	clk = rockchip_clk_register_cpuclk(name, parent_names, num_parents,
522*03ae1747SHeiko Stuebner 					   reg_data, rates, nrates,
523*03ae1747SHeiko Stuebner 					   ctx->reg_base, &ctx->lock);
524f6fba5f6SHeiko Stuebner 	if (IS_ERR(clk)) {
525f6fba5f6SHeiko Stuebner 		pr_err("%s: failed to register clock %s: %ld\n",
526f6fba5f6SHeiko Stuebner 		       __func__, name, PTR_ERR(clk));
527f6fba5f6SHeiko Stuebner 		return;
528f6fba5f6SHeiko Stuebner 	}
529f6fba5f6SHeiko Stuebner 
530ef1d9feeSXing Zheng 	rockchip_clk_add_lookup(ctx, clk, lookup_id);
531f6fba5f6SHeiko Stuebner }
532f6fba5f6SHeiko Stuebner 
533692d8328SUwe Kleine-König void __init rockchip_clk_protect_critical(const char *const clocks[],
534692d8328SUwe Kleine-König 					  int nclocks)
535fe94f974SHeiko Stübner {
536fe94f974SHeiko Stübner 	int i;
537fe94f974SHeiko Stübner 
538fe94f974SHeiko Stübner 	/* Protect the clocks that needs to stay on */
539fe94f974SHeiko Stübner 	for (i = 0; i < nclocks; i++) {
540fe94f974SHeiko Stübner 		struct clk *clk = __clk_lookup(clocks[i]);
541fe94f974SHeiko Stübner 
542fe94f974SHeiko Stübner 		if (clk)
543fe94f974SHeiko Stübner 			clk_prepare_enable(clk);
544fe94f974SHeiko Stübner 	}
545fe94f974SHeiko Stübner }
5466f1294b5SHeiko Stübner 
547ef1d9feeSXing Zheng static void __iomem *rst_base;
5486f1294b5SHeiko Stübner static unsigned int reg_restart;
549dfff24bdSHeiko Stuebner static void (*cb_restart)(void);
5506f1294b5SHeiko Stübner static int rockchip_restart_notify(struct notifier_block *this,
5516f1294b5SHeiko Stübner 				   unsigned long mode, void *cmd)
5526f1294b5SHeiko Stübner {
553dfff24bdSHeiko Stuebner 	if (cb_restart)
554dfff24bdSHeiko Stuebner 		cb_restart();
555dfff24bdSHeiko Stuebner 
556ef1d9feeSXing Zheng 	writel(0xfdb9, rst_base + reg_restart);
5576f1294b5SHeiko Stübner 	return NOTIFY_DONE;
5586f1294b5SHeiko Stübner }
5596f1294b5SHeiko Stübner 
5606f1294b5SHeiko Stübner static struct notifier_block rockchip_restart_handler = {
5616f1294b5SHeiko Stübner 	.notifier_call = rockchip_restart_notify,
5626f1294b5SHeiko Stübner 	.priority = 128,
5636f1294b5SHeiko Stübner };
5646f1294b5SHeiko Stübner 
565*03ae1747SHeiko Stuebner void __init
566*03ae1747SHeiko Stuebner rockchip_register_restart_notifier(struct rockchip_clk_provider *ctx,
567*03ae1747SHeiko Stuebner 					       unsigned int reg,
568*03ae1747SHeiko Stuebner 					       void (*cb)(void))
5696f1294b5SHeiko Stübner {
5706f1294b5SHeiko Stübner 	int ret;
5716f1294b5SHeiko Stübner 
572ef1d9feeSXing Zheng 	rst_base = ctx->reg_base;
5736f1294b5SHeiko Stübner 	reg_restart = reg;
574dfff24bdSHeiko Stuebner 	cb_restart = cb;
5756f1294b5SHeiko Stübner 	ret = register_restart_handler(&rockchip_restart_handler);
5766f1294b5SHeiko Stübner 	if (ret)
5776f1294b5SHeiko Stübner 		pr_err("%s: cannot register restart handler, %d\n",
5786f1294b5SHeiko Stübner 		       __func__, ret);
5796f1294b5SHeiko Stübner }
580