1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a245fecbSHeiko Stübner /*
3a245fecbSHeiko Stübner * Copyright (c) 2014 MundoReader S.L.
4a245fecbSHeiko Stübner * Author: Heiko Stuebner <heiko@sntech.de>
5a245fecbSHeiko Stübner *
6ef1d9feeSXing Zheng * Copyright (c) 2016 Rockchip Electronics Co. Ltd.
7ef1d9feeSXing Zheng * Author: Xing Zheng <zhengxing@rock-chips.com>
8ef1d9feeSXing Zheng *
9a245fecbSHeiko Stübner * based on
10a245fecbSHeiko Stübner *
11a245fecbSHeiko Stübner * samsung/clk.c
12a245fecbSHeiko Stübner * Copyright (c) 2013 Samsung Electronics Co., Ltd.
13a245fecbSHeiko Stübner * Copyright (c) 2013 Linaro Ltd.
14a245fecbSHeiko Stübner * Author: Thomas Abraham <thomas.ab@samsung.com>
15a245fecbSHeiko Stübner */
16a245fecbSHeiko Stübner
17a245fecbSHeiko Stübner #include <linux/slab.h>
18a245fecbSHeiko Stübner #include <linux/clk.h>
19a245fecbSHeiko Stübner #include <linux/clk-provider.h>
2062e59c4eSStephen Boyd #include <linux/io.h>
2190c59025SHeiko Stübner #include <linux/mfd/syscon.h>
2290c59025SHeiko Stübner #include <linux/regmap.h>
236f1294b5SHeiko Stübner #include <linux/reboot.h>
244e7cf74fSAndy Shevchenko
254e7cf74fSAndy Shevchenko #include "../clk-fractional-divider.h"
26a245fecbSHeiko Stübner #include "clk.h"
27a245fecbSHeiko Stübner
2841517371SLee Jones /*
29a245fecbSHeiko Stübner * Register a clock branch.
30a245fecbSHeiko Stübner * Most clock branches have a form like
31a245fecbSHeiko Stübner *
32a245fecbSHeiko Stübner * src1 --|--\
33a245fecbSHeiko Stübner * |M |--[GATE]-[DIV]-
34a245fecbSHeiko Stübner * src2 --|--/
35a245fecbSHeiko Stübner *
36a245fecbSHeiko Stübner * sometimes without one of those components.
37a245fecbSHeiko Stübner */
rockchip_clk_register_branch(const char * name,const char * const * parent_names,u8 num_parents,void __iomem * base,int muxdiv_offset,u8 mux_shift,u8 mux_width,u8 mux_flags,u32 * mux_table,int div_offset,u8 div_shift,u8 div_width,u8 div_flags,struct clk_div_table * div_table,int gate_offset,u8 gate_shift,u8 gate_flags,unsigned long flags,spinlock_t * lock)381a4b1819SHeiko Stübner static struct clk *rockchip_clk_register_branch(const char *name,
3903ae1747SHeiko Stuebner const char *const *parent_names, u8 num_parents,
4003ae1747SHeiko Stuebner void __iomem *base,
41a245fecbSHeiko Stübner int muxdiv_offset, u8 mux_shift, u8 mux_width, u8 mux_flags,
4230d8b7d4SElaine Zhang u32 *mux_table,
431f55660fSFinley Xiao int div_offset, u8 div_shift, u8 div_width, u8 div_flags,
44a245fecbSHeiko Stübner struct clk_div_table *div_table, int gate_offset,
45a245fecbSHeiko Stübner u8 gate_shift, u8 gate_flags, unsigned long flags,
46a245fecbSHeiko Stübner spinlock_t *lock)
47a245fecbSHeiko Stübner {
4863207c37SElaine Zhang struct clk_hw *hw;
49a245fecbSHeiko Stübner struct clk_mux *mux = NULL;
50a245fecbSHeiko Stübner struct clk_gate *gate = NULL;
51a245fecbSHeiko Stübner struct clk_divider *div = NULL;
52a245fecbSHeiko Stübner const struct clk_ops *mux_ops = NULL, *div_ops = NULL,
53a245fecbSHeiko Stübner *gate_ops = NULL;
54fd3cbbfbSShawn Lin int ret;
55a245fecbSHeiko Stübner
56a245fecbSHeiko Stübner if (num_parents > 1) {
57a245fecbSHeiko Stübner mux = kzalloc(sizeof(*mux), GFP_KERNEL);
58a245fecbSHeiko Stübner if (!mux)
59a245fecbSHeiko Stübner return ERR_PTR(-ENOMEM);
60a245fecbSHeiko Stübner
61a245fecbSHeiko Stübner mux->reg = base + muxdiv_offset;
62a245fecbSHeiko Stübner mux->shift = mux_shift;
63a245fecbSHeiko Stübner mux->mask = BIT(mux_width) - 1;
64a245fecbSHeiko Stübner mux->flags = mux_flags;
6530d8b7d4SElaine Zhang mux->table = mux_table;
66a245fecbSHeiko Stübner mux->lock = lock;
67a245fecbSHeiko Stübner mux_ops = (mux_flags & CLK_MUX_READ_ONLY) ? &clk_mux_ro_ops
68a245fecbSHeiko Stübner : &clk_mux_ops;
69a245fecbSHeiko Stübner }
70a245fecbSHeiko Stübner
71a245fecbSHeiko Stübner if (gate_offset >= 0) {
72a245fecbSHeiko Stübner gate = kzalloc(sizeof(*gate), GFP_KERNEL);
73fd3cbbfbSShawn Lin if (!gate) {
74fd3cbbfbSShawn Lin ret = -ENOMEM;
752467b674SShawn Lin goto err_gate;
76fd3cbbfbSShawn Lin }
77a245fecbSHeiko Stübner
78a245fecbSHeiko Stübner gate->flags = gate_flags;
79a245fecbSHeiko Stübner gate->reg = base + gate_offset;
80a245fecbSHeiko Stübner gate->bit_idx = gate_shift;
81a245fecbSHeiko Stübner gate->lock = lock;
82a245fecbSHeiko Stübner gate_ops = &clk_gate_ops;
83a245fecbSHeiko Stübner }
84a245fecbSHeiko Stübner
85a245fecbSHeiko Stübner if (div_width > 0) {
86a245fecbSHeiko Stübner div = kzalloc(sizeof(*div), GFP_KERNEL);
87fd3cbbfbSShawn Lin if (!div) {
88fd3cbbfbSShawn Lin ret = -ENOMEM;
892467b674SShawn Lin goto err_div;
90fd3cbbfbSShawn Lin }
91a245fecbSHeiko Stübner
92a245fecbSHeiko Stübner div->flags = div_flags;
931f55660fSFinley Xiao if (div_offset)
941f55660fSFinley Xiao div->reg = base + div_offset;
951f55660fSFinley Xiao else
96a245fecbSHeiko Stübner div->reg = base + muxdiv_offset;
97a245fecbSHeiko Stübner div->shift = div_shift;
98a245fecbSHeiko Stübner div->width = div_width;
99a245fecbSHeiko Stübner div->lock = lock;
100a245fecbSHeiko Stübner div->table = div_table;
10150359819SHeiko Stuebner div_ops = (div_flags & CLK_DIVIDER_READ_ONLY)
10250359819SHeiko Stuebner ? &clk_divider_ro_ops
10350359819SHeiko Stuebner : &clk_divider_ops;
104a245fecbSHeiko Stübner }
105a245fecbSHeiko Stübner
10663207c37SElaine Zhang hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
107a245fecbSHeiko Stübner mux ? &mux->hw : NULL, mux_ops,
108a245fecbSHeiko Stübner div ? &div->hw : NULL, div_ops,
109a245fecbSHeiko Stübner gate ? &gate->hw : NULL, gate_ops,
110a245fecbSHeiko Stübner flags);
11163207c37SElaine Zhang if (IS_ERR(hw)) {
11263207c37SElaine Zhang kfree(div);
11363207c37SElaine Zhang kfree(gate);
11463207c37SElaine Zhang return ERR_CAST(hw);
115fd3cbbfbSShawn Lin }
116fd3cbbfbSShawn Lin
11763207c37SElaine Zhang return hw->clk;
1182467b674SShawn Lin err_div:
1192467b674SShawn Lin kfree(gate);
1202467b674SShawn Lin err_gate:
1212467b674SShawn Lin kfree(mux);
122fd3cbbfbSShawn Lin return ERR_PTR(ret);
123a245fecbSHeiko Stübner }
124a245fecbSHeiko Stübner
1258ca1ca8fSHeiko Stuebner struct rockchip_clk_frac {
1268ca1ca8fSHeiko Stuebner struct notifier_block clk_nb;
1278ca1ca8fSHeiko Stuebner struct clk_fractional_divider div;
1288ca1ca8fSHeiko Stuebner struct clk_gate gate;
1298ca1ca8fSHeiko Stuebner
1308ca1ca8fSHeiko Stuebner struct clk_mux mux;
1318ca1ca8fSHeiko Stuebner const struct clk_ops *mux_ops;
1328ca1ca8fSHeiko Stuebner int mux_frac_idx;
1338ca1ca8fSHeiko Stuebner
1348ca1ca8fSHeiko Stuebner bool rate_change_remuxed;
1358ca1ca8fSHeiko Stuebner int rate_change_idx;
1368ca1ca8fSHeiko Stuebner };
1378ca1ca8fSHeiko Stuebner
1388ca1ca8fSHeiko Stuebner #define to_rockchip_clk_frac_nb(nb) \
1398ca1ca8fSHeiko Stuebner container_of(nb, struct rockchip_clk_frac, clk_nb)
1408ca1ca8fSHeiko Stuebner
rockchip_clk_frac_notifier_cb(struct notifier_block * nb,unsigned long event,void * data)1418ca1ca8fSHeiko Stuebner static int rockchip_clk_frac_notifier_cb(struct notifier_block *nb,
1428ca1ca8fSHeiko Stuebner unsigned long event, void *data)
1438ca1ca8fSHeiko Stuebner {
1448ca1ca8fSHeiko Stuebner struct clk_notifier_data *ndata = data;
1458ca1ca8fSHeiko Stuebner struct rockchip_clk_frac *frac = to_rockchip_clk_frac_nb(nb);
1468ca1ca8fSHeiko Stuebner struct clk_mux *frac_mux = &frac->mux;
1478ca1ca8fSHeiko Stuebner int ret = 0;
1488ca1ca8fSHeiko Stuebner
1498ca1ca8fSHeiko Stuebner pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
1508ca1ca8fSHeiko Stuebner __func__, event, ndata->old_rate, ndata->new_rate);
1518ca1ca8fSHeiko Stuebner if (event == PRE_RATE_CHANGE) {
15203ae1747SHeiko Stuebner frac->rate_change_idx =
15303ae1747SHeiko Stuebner frac->mux_ops->get_parent(&frac_mux->hw);
1548ca1ca8fSHeiko Stuebner if (frac->rate_change_idx != frac->mux_frac_idx) {
15503ae1747SHeiko Stuebner frac->mux_ops->set_parent(&frac_mux->hw,
15603ae1747SHeiko Stuebner frac->mux_frac_idx);
1578ca1ca8fSHeiko Stuebner frac->rate_change_remuxed = 1;
1588ca1ca8fSHeiko Stuebner }
1598ca1ca8fSHeiko Stuebner } else if (event == POST_RATE_CHANGE) {
1608ca1ca8fSHeiko Stuebner /*
1618ca1ca8fSHeiko Stuebner * The POST_RATE_CHANGE notifier runs directly after the
1628ca1ca8fSHeiko Stuebner * divider clock is set in clk_change_rate, so we'll have
1638ca1ca8fSHeiko Stuebner * remuxed back to the original parent before clk_change_rate
1648ca1ca8fSHeiko Stuebner * reaches the mux itself.
1658ca1ca8fSHeiko Stuebner */
1668ca1ca8fSHeiko Stuebner if (frac->rate_change_remuxed) {
16703ae1747SHeiko Stuebner frac->mux_ops->set_parent(&frac_mux->hw,
16803ae1747SHeiko Stuebner frac->rate_change_idx);
1698ca1ca8fSHeiko Stuebner frac->rate_change_remuxed = 0;
1708ca1ca8fSHeiko Stuebner }
1718ca1ca8fSHeiko Stuebner }
1728ca1ca8fSHeiko Stuebner
1738ca1ca8fSHeiko Stuebner return notifier_from_errno(ret);
1748ca1ca8fSHeiko Stuebner }
1758ca1ca8fSHeiko Stuebner
17641517371SLee Jones /*
1775d890c2dSElaine Zhang * fractional divider must set that denominator is 20 times larger than
1785d890c2dSElaine Zhang * numerator to generate precise clock frequency.
1795d890c2dSElaine Zhang */
rockchip_fractional_approximation(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate,unsigned long * m,unsigned long * n)1801dfcfa72SStephen Boyd static void rockchip_fractional_approximation(struct clk_hw *hw,
1815d890c2dSElaine Zhang unsigned long rate, unsigned long *parent_rate,
1825d890c2dSElaine Zhang unsigned long *m, unsigned long *n)
1835d890c2dSElaine Zhang {
18410b74af3SQuentin Schulz struct clk_fractional_divider *fd = to_clk_fd(hw);
1855d890c2dSElaine Zhang unsigned long p_rate, p_parent_rate;
1865d890c2dSElaine Zhang struct clk_hw *p_parent;
1875d890c2dSElaine Zhang
1885d890c2dSElaine Zhang p_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
1895d890c2dSElaine Zhang if ((rate * 20 > p_rate) && (p_rate % rate != 0)) {
1905d890c2dSElaine Zhang p_parent = clk_hw_get_parent(clk_hw_get_parent(hw));
1915d890c2dSElaine Zhang p_parent_rate = clk_hw_get_rate(p_parent);
1925d890c2dSElaine Zhang *parent_rate = p_parent_rate;
1935d890c2dSElaine Zhang }
1945d890c2dSElaine Zhang
19510b74af3SQuentin Schulz fd->flags |= CLK_FRAC_DIVIDER_POWER_OF_TWO_PS;
19610b74af3SQuentin Schulz
1974e7cf74fSAndy Shevchenko clk_fractional_divider_general_approximation(hw, rate, parent_rate, m, n);
1985d890c2dSElaine Zhang }
1995d890c2dSElaine Zhang
rockchip_clk_add_lookup(struct rockchip_clk_provider * ctx,struct clk * clk,unsigned int id)200ff94c866SSebastian Reichel static void rockchip_clk_add_lookup(struct rockchip_clk_provider *ctx,
201ff94c866SSebastian Reichel struct clk *clk, unsigned int id)
202ff94c866SSebastian Reichel {
203ff94c866SSebastian Reichel ctx->clk_data.clks[id] = clk;
204ff94c866SSebastian Reichel }
205ff94c866SSebastian Reichel
rockchip_clk_register_frac_branch(struct rockchip_clk_provider * ctx,const char * name,const char * const * parent_names,u8 num_parents,void __iomem * base,int muxdiv_offset,u8 div_flags,int gate_offset,u8 gate_shift,u8 gate_flags,unsigned long flags,struct rockchip_clk_branch * child,spinlock_t * lock)206ef1d9feeSXing Zheng static struct clk *rockchip_clk_register_frac_branch(
207ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx, const char *name,
2084a1caed3SUwe Kleine-König const char *const *parent_names, u8 num_parents,
2094a1caed3SUwe Kleine-König void __iomem *base, int muxdiv_offset, u8 div_flags,
210b2155a71SHeiko Stübner int gate_offset, u8 gate_shift, u8 gate_flags,
2118ca1ca8fSHeiko Stuebner unsigned long flags, struct rockchip_clk_branch *child,
2128ca1ca8fSHeiko Stuebner spinlock_t *lock)
213b2155a71SHeiko Stübner {
21463207c37SElaine Zhang struct clk_hw *hw;
2158ca1ca8fSHeiko Stuebner struct rockchip_clk_frac *frac;
216b2155a71SHeiko Stübner struct clk_gate *gate = NULL;
217b2155a71SHeiko Stübner struct clk_fractional_divider *div = NULL;
218b2155a71SHeiko Stübner const struct clk_ops *div_ops = NULL, *gate_ops = NULL;
219b2155a71SHeiko Stübner
2208ca1ca8fSHeiko Stuebner if (muxdiv_offset < 0)
2218ca1ca8fSHeiko Stuebner return ERR_PTR(-EINVAL);
2228ca1ca8fSHeiko Stuebner
2238ca1ca8fSHeiko Stuebner if (child && child->branch_type != branch_mux) {
2248ca1ca8fSHeiko Stuebner pr_err("%s: fractional child clock for %s can only be a mux\n",
2258ca1ca8fSHeiko Stuebner __func__, name);
2268ca1ca8fSHeiko Stuebner return ERR_PTR(-EINVAL);
2278ca1ca8fSHeiko Stuebner }
2288ca1ca8fSHeiko Stuebner
2298ca1ca8fSHeiko Stuebner frac = kzalloc(sizeof(*frac), GFP_KERNEL);
2308ca1ca8fSHeiko Stuebner if (!frac)
231b2155a71SHeiko Stübner return ERR_PTR(-ENOMEM);
232b2155a71SHeiko Stübner
2338ca1ca8fSHeiko Stuebner if (gate_offset >= 0) {
2348ca1ca8fSHeiko Stuebner gate = &frac->gate;
235b2155a71SHeiko Stübner gate->flags = gate_flags;
236b2155a71SHeiko Stübner gate->reg = base + gate_offset;
237b2155a71SHeiko Stübner gate->bit_idx = gate_shift;
238b2155a71SHeiko Stübner gate->lock = lock;
239b2155a71SHeiko Stübner gate_ops = &clk_gate_ops;
240b2155a71SHeiko Stübner }
241b2155a71SHeiko Stübner
2428ca1ca8fSHeiko Stuebner div = &frac->div;
243b2155a71SHeiko Stübner div->flags = div_flags;
244b2155a71SHeiko Stübner div->reg = base + muxdiv_offset;
245b2155a71SHeiko Stübner div->mshift = 16;
2465d49a6e1SAndy Shevchenko div->mwidth = 16;
247b2155a71SHeiko Stübner div->nshift = 0;
2485d49a6e1SAndy Shevchenko div->nwidth = 16;
249b2155a71SHeiko Stübner div->lock = lock;
2505d890c2dSElaine Zhang div->approximation = rockchip_fractional_approximation;
251b2155a71SHeiko Stübner div_ops = &clk_fractional_divider_ops;
252b2155a71SHeiko Stübner
25363207c37SElaine Zhang hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
254b2155a71SHeiko Stübner NULL, NULL,
255b2155a71SHeiko Stübner &div->hw, div_ops,
256b2155a71SHeiko Stübner gate ? &gate->hw : NULL, gate_ops,
2578ca1ca8fSHeiko Stuebner flags | CLK_SET_RATE_UNGATE);
25863207c37SElaine Zhang if (IS_ERR(hw)) {
2598ca1ca8fSHeiko Stuebner kfree(frac);
26063207c37SElaine Zhang return ERR_CAST(hw);
2618ca1ca8fSHeiko Stuebner }
2628ca1ca8fSHeiko Stuebner
2638ca1ca8fSHeiko Stuebner if (child) {
2648ca1ca8fSHeiko Stuebner struct clk_mux *frac_mux = &frac->mux;
2658ca1ca8fSHeiko Stuebner struct clk_init_data init;
2668ca1ca8fSHeiko Stuebner struct clk *mux_clk;
267a425702fSYisheng Xie int ret;
2688ca1ca8fSHeiko Stuebner
269a425702fSYisheng Xie frac->mux_frac_idx = match_string(child->parent_names,
270a425702fSYisheng Xie child->num_parents, name);
2718ca1ca8fSHeiko Stuebner frac->mux_ops = &clk_mux_ops;
2728ca1ca8fSHeiko Stuebner frac->clk_nb.notifier_call = rockchip_clk_frac_notifier_cb;
2738ca1ca8fSHeiko Stuebner
2748ca1ca8fSHeiko Stuebner frac_mux->reg = base + child->muxdiv_offset;
2758ca1ca8fSHeiko Stuebner frac_mux->shift = child->mux_shift;
2768ca1ca8fSHeiko Stuebner frac_mux->mask = BIT(child->mux_width) - 1;
2778ca1ca8fSHeiko Stuebner frac_mux->flags = child->mux_flags;
27830d8b7d4SElaine Zhang if (child->mux_table)
27930d8b7d4SElaine Zhang frac_mux->table = child->mux_table;
2808ca1ca8fSHeiko Stuebner frac_mux->lock = lock;
2818ca1ca8fSHeiko Stuebner frac_mux->hw.init = &init;
2828ca1ca8fSHeiko Stuebner
2838ca1ca8fSHeiko Stuebner init.name = child->name;
2848ca1ca8fSHeiko Stuebner init.flags = child->flags | CLK_SET_RATE_PARENT;
2858ca1ca8fSHeiko Stuebner init.ops = frac->mux_ops;
2868ca1ca8fSHeiko Stuebner init.parent_names = child->parent_names;
2878ca1ca8fSHeiko Stuebner init.num_parents = child->num_parents;
2888ca1ca8fSHeiko Stuebner
2898ca1ca8fSHeiko Stuebner mux_clk = clk_register(NULL, &frac_mux->hw);
290fd3cbbfbSShawn Lin if (IS_ERR(mux_clk)) {
291fd3cbbfbSShawn Lin kfree(frac);
29263207c37SElaine Zhang return mux_clk;
293fd3cbbfbSShawn Lin }
2948ca1ca8fSHeiko Stuebner
295ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, mux_clk, child->id);
2968ca1ca8fSHeiko Stuebner
2978ca1ca8fSHeiko Stuebner /* notifier on the fraction divider to catch rate changes */
2988ca1ca8fSHeiko Stuebner if (frac->mux_frac_idx >= 0) {
299a425702fSYisheng Xie pr_debug("%s: found fractional parent in mux at pos %d\n",
300a425702fSYisheng Xie __func__, frac->mux_frac_idx);
30163207c37SElaine Zhang ret = clk_notifier_register(hw->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
31163207c37SElaine Zhang return hw->clk;
312b2155a71SHeiko Stübner }
313b2155a71SHeiko Stübner
rockchip_clk_register_factor_branch(const char * name,const char * const * parent_names,u8 num_parents,void __iomem * base,unsigned int mult,unsigned int div,int gate_offset,u8 gate_shift,u8 gate_flags,unsigned long flags,spinlock_t * lock)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 {
32063207c37SElaine Zhang struct clk_hw *hw;
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
34963207c37SElaine Zhang hw = clk_hw_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);
35363207c37SElaine Zhang if (IS_ERR(hw)) {
35429a30c26SHeiko Stuebner kfree(fix);
35529a30c26SHeiko Stuebner kfree(gate);
35663207c37SElaine Zhang return ERR_CAST(hw);
35729a30c26SHeiko Stuebner }
35829a30c26SHeiko Stuebner
35963207c37SElaine Zhang return hw->clk;
36029a30c26SHeiko Stuebner }
36129a30c26SHeiko Stuebner
rockchip_clk_init(struct device_node * np,void __iomem * base,unsigned long nr_clks)362ea650c26SElaine Zhang struct rockchip_clk_provider *rockchip_clk_init(struct device_node *np,
363ea650c26SElaine Zhang void __iomem *base,
364ea650c26SElaine Zhang unsigned long nr_clks)
365a245fecbSHeiko Stübner {
366ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx;
367ef1d9feeSXing Zheng struct clk **clk_table;
368ef1d9feeSXing Zheng int i;
369ef1d9feeSXing Zheng
370ef1d9feeSXing Zheng ctx = kzalloc(sizeof(struct rockchip_clk_provider), GFP_KERNEL);
37103ae1747SHeiko Stuebner if (!ctx)
372ef1d9feeSXing Zheng return ERR_PTR(-ENOMEM);
373a245fecbSHeiko Stübner
374a245fecbSHeiko Stübner clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL);
37503ae1747SHeiko Stuebner if (!clk_table)
376ef1d9feeSXing Zheng goto err_free;
377a245fecbSHeiko Stübner
378ef1d9feeSXing Zheng for (i = 0; i < nr_clks; ++i)
379ef1d9feeSXing Zheng clk_table[i] = ERR_PTR(-ENOENT);
380ef1d9feeSXing Zheng
381ef1d9feeSXing Zheng ctx->reg_base = base;
382ef1d9feeSXing Zheng ctx->clk_data.clks = clk_table;
383ef1d9feeSXing Zheng ctx->clk_data.clk_num = nr_clks;
384ef1d9feeSXing Zheng ctx->cru_node = np;
385ef1d9feeSXing Zheng spin_lock_init(&ctx->lock);
386ef1d9feeSXing Zheng
3876f339dc2SHeiko Stuebner ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node,
3886f339dc2SHeiko Stuebner "rockchip,grf");
3896f339dc2SHeiko Stuebner
390ef1d9feeSXing Zheng return ctx;
391ef1d9feeSXing Zheng
392ef1d9feeSXing Zheng err_free:
393ef1d9feeSXing Zheng kfree(ctx);
394ef1d9feeSXing Zheng return ERR_PTR(-ENOMEM);
395ef1d9feeSXing Zheng }
396ea650c26SElaine Zhang EXPORT_SYMBOL_GPL(rockchip_clk_init);
397ef1d9feeSXing Zheng
rockchip_clk_of_add_provider(struct device_node * np,struct rockchip_clk_provider * ctx)398ea650c26SElaine Zhang void rockchip_clk_of_add_provider(struct device_node *np,
399ef1d9feeSXing Zheng struct rockchip_clk_provider *ctx)
40090c59025SHeiko Stübner {
401ef1d9feeSXing Zheng if (of_clk_add_provider(np, of_clk_src_onecell_get,
402ef1d9feeSXing Zheng &ctx->clk_data))
403ef1d9feeSXing Zheng pr_err("%s: could not register clk provider\n", __func__);
404ef1d9feeSXing Zheng }
405ea650c26SElaine Zhang EXPORT_SYMBOL_GPL(rockchip_clk_of_add_provider);
40690c59025SHeiko Stübner
rockchip_clk_register_plls(struct rockchip_clk_provider * ctx,struct rockchip_pll_clock * list,unsigned int nr_pll,int grf_lock_offset)407ea650c26SElaine Zhang void rockchip_clk_register_plls(struct rockchip_clk_provider *ctx,
408ef1d9feeSXing Zheng struct rockchip_pll_clock *list,
40990c59025SHeiko Stübner unsigned int nr_pll, int grf_lock_offset)
41090c59025SHeiko Stübner {
41190c59025SHeiko Stübner struct clk *clk;
41290c59025SHeiko Stübner int idx;
41390c59025SHeiko Stübner
41490c59025SHeiko Stübner for (idx = 0; idx < nr_pll; idx++, list++) {
415ef1d9feeSXing Zheng clk = rockchip_clk_register_pll(ctx, list->type, list->name,
41690c59025SHeiko Stübner list->parent_names, list->num_parents,
417ef1d9feeSXing Zheng list->con_offset, grf_lock_offset,
41890c59025SHeiko Stübner list->lock_shift, list->mode_offset,
4194f8a7c54SHeiko Stuebner list->mode_shift, list->rate_table,
420e6cebc72SHeiko Stübner list->flags, list->pll_flags);
42190c59025SHeiko Stübner if (IS_ERR(clk)) {
42290c59025SHeiko Stübner pr_err("%s: failed to register clock %s\n", __func__,
42390c59025SHeiko Stübner list->name);
42490c59025SHeiko Stübner continue;
42590c59025SHeiko Stübner }
42690c59025SHeiko Stübner
427ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, clk, list->id);
42890c59025SHeiko Stübner }
42990c59025SHeiko Stübner }
430ea650c26SElaine Zhang EXPORT_SYMBOL_GPL(rockchip_clk_register_plls);
43190c59025SHeiko Stübner
rockchip_clk_register_branches(struct rockchip_clk_provider * ctx,struct rockchip_clk_branch * list,unsigned int nr_clk)432ea650c26SElaine Zhang void rockchip_clk_register_branches(struct rockchip_clk_provider *ctx,
433a245fecbSHeiko Stübner struct rockchip_clk_branch *list,
434a245fecbSHeiko Stübner unsigned int nr_clk)
435a245fecbSHeiko Stübner {
436*bd588d52SSebastian Reichel struct clk *clk;
437a245fecbSHeiko Stübner unsigned int idx;
438a245fecbSHeiko Stübner unsigned long flags;
439a245fecbSHeiko Stübner
440a245fecbSHeiko Stübner for (idx = 0; idx < nr_clk; idx++, list++) {
441a245fecbSHeiko Stübner flags = list->flags;
442*bd588d52SSebastian Reichel clk = NULL;
443a245fecbSHeiko Stübner
444a245fecbSHeiko Stübner /* catch simple muxes */
445a245fecbSHeiko Stübner switch (list->branch_type) {
446a245fecbSHeiko Stübner case branch_mux:
44730d8b7d4SElaine Zhang if (list->mux_table)
44830d8b7d4SElaine Zhang clk = clk_register_mux_table(NULL, list->name,
44930d8b7d4SElaine Zhang list->parent_names, list->num_parents,
45030d8b7d4SElaine Zhang flags,
45130d8b7d4SElaine Zhang ctx->reg_base + list->muxdiv_offset,
45230d8b7d4SElaine Zhang list->mux_shift, list->mux_width,
45330d8b7d4SElaine Zhang list->mux_flags, list->mux_table,
45430d8b7d4SElaine Zhang &ctx->lock);
45530d8b7d4SElaine Zhang else
456a245fecbSHeiko Stübner clk = clk_register_mux(NULL, list->name,
457a245fecbSHeiko Stübner list->parent_names, list->num_parents,
45830d8b7d4SElaine Zhang flags,
45930d8b7d4SElaine Zhang ctx->reg_base + list->muxdiv_offset,
460a245fecbSHeiko Stübner list->mux_shift, list->mux_width,
461ef1d9feeSXing Zheng list->mux_flags, &ctx->lock);
462a245fecbSHeiko Stübner break;
463cb1d9f6dSHeiko Stuebner case branch_muxgrf:
464cb1d9f6dSHeiko Stuebner clk = rockchip_clk_register_muxgrf(list->name,
465cb1d9f6dSHeiko Stuebner list->parent_names, list->num_parents,
466cb1d9f6dSHeiko Stuebner flags, ctx->grf, list->muxdiv_offset,
467cb1d9f6dSHeiko Stuebner list->mux_shift, list->mux_width,
468cb1d9f6dSHeiko Stuebner list->mux_flags);
469cb1d9f6dSHeiko Stuebner break;
470a245fecbSHeiko Stübner case branch_divider:
471a245fecbSHeiko Stübner if (list->div_table)
472a245fecbSHeiko Stübner clk = clk_register_divider_table(NULL,
473a245fecbSHeiko Stübner list->name, list->parent_names[0],
47403ae1747SHeiko Stuebner flags,
47503ae1747SHeiko Stuebner ctx->reg_base + list->muxdiv_offset,
476a245fecbSHeiko Stübner list->div_shift, list->div_width,
477a245fecbSHeiko Stübner list->div_flags, list->div_table,
478ef1d9feeSXing Zheng &ctx->lock);
479a245fecbSHeiko Stübner else
480a245fecbSHeiko Stübner clk = clk_register_divider(NULL, list->name,
481a245fecbSHeiko Stübner list->parent_names[0], flags,
482ef1d9feeSXing Zheng ctx->reg_base + list->muxdiv_offset,
483a245fecbSHeiko Stübner list->div_shift, list->div_width,
484ef1d9feeSXing Zheng list->div_flags, &ctx->lock);
485a245fecbSHeiko Stübner break;
486a245fecbSHeiko Stübner case branch_fraction_divider:
487ef1d9feeSXing Zheng clk = rockchip_clk_register_frac_branch(ctx, list->name,
488b2155a71SHeiko Stübner list->parent_names, list->num_parents,
48903ae1747SHeiko Stuebner ctx->reg_base, list->muxdiv_offset,
49003ae1747SHeiko Stuebner list->div_flags,
491b2155a71SHeiko Stübner list->gate_offset, list->gate_shift,
4928ca1ca8fSHeiko Stuebner list->gate_flags, flags, list->child,
493ef1d9feeSXing Zheng &ctx->lock);
494a245fecbSHeiko Stübner break;
495956060a5SElaine Zhang case branch_half_divider:
496956060a5SElaine Zhang clk = rockchip_clk_register_halfdiv(list->name,
497956060a5SElaine Zhang list->parent_names, list->num_parents,
498956060a5SElaine Zhang ctx->reg_base, list->muxdiv_offset,
499956060a5SElaine Zhang list->mux_shift, list->mux_width,
500956060a5SElaine Zhang list->mux_flags, list->div_shift,
501956060a5SElaine Zhang list->div_width, list->div_flags,
502956060a5SElaine Zhang list->gate_offset, list->gate_shift,
503956060a5SElaine Zhang list->gate_flags, flags, &ctx->lock);
504956060a5SElaine Zhang break;
505a245fecbSHeiko Stübner case branch_gate:
506a245fecbSHeiko Stübner flags |= CLK_SET_RATE_PARENT;
507a245fecbSHeiko Stübner
508a245fecbSHeiko Stübner clk = clk_register_gate(NULL, list->name,
509a245fecbSHeiko Stübner list->parent_names[0], flags,
510ef1d9feeSXing Zheng ctx->reg_base + list->gate_offset,
511ef1d9feeSXing Zheng list->gate_shift, list->gate_flags, &ctx->lock);
512a245fecbSHeiko Stübner break;
513a245fecbSHeiko Stübner case branch_composite:
514a245fecbSHeiko Stübner clk = rockchip_clk_register_branch(list->name,
515a245fecbSHeiko Stübner list->parent_names, list->num_parents,
51603ae1747SHeiko Stuebner ctx->reg_base, list->muxdiv_offset,
51703ae1747SHeiko Stuebner list->mux_shift,
518a245fecbSHeiko Stübner list->mux_width, list->mux_flags,
51930d8b7d4SElaine Zhang list->mux_table, list->div_offset,
52030d8b7d4SElaine Zhang list->div_shift, list->div_width,
521a245fecbSHeiko Stübner list->div_flags, list->div_table,
522a245fecbSHeiko Stübner list->gate_offset, list->gate_shift,
523ef1d9feeSXing Zheng list->gate_flags, flags, &ctx->lock);
524a245fecbSHeiko Stübner break;
52589bf26cbSAlexandru M Stan case branch_mmc:
52689bf26cbSAlexandru M Stan clk = rockchip_clk_register_mmc(
52789bf26cbSAlexandru M Stan list->name,
52889bf26cbSAlexandru M Stan list->parent_names, list->num_parents,
529ef1d9feeSXing Zheng ctx->reg_base + list->muxdiv_offset,
53089bf26cbSAlexandru M Stan list->div_shift
53189bf26cbSAlexandru M Stan );
53289bf26cbSAlexandru M Stan break;
5338a76f443SHeiko Stuebner case branch_inverter:
5348a76f443SHeiko Stuebner clk = rockchip_clk_register_inverter(
5358a76f443SHeiko Stuebner list->name, list->parent_names,
5368a76f443SHeiko Stuebner list->num_parents,
537ef1d9feeSXing Zheng ctx->reg_base + list->muxdiv_offset,
538ef1d9feeSXing Zheng list->div_shift, list->div_flags, &ctx->lock);
5398a76f443SHeiko Stuebner break;
54029a30c26SHeiko Stuebner case branch_factor:
54129a30c26SHeiko Stuebner clk = rockchip_clk_register_factor_branch(
54229a30c26SHeiko Stuebner list->name, list->parent_names,
543ef1d9feeSXing Zheng list->num_parents, ctx->reg_base,
54429a30c26SHeiko Stuebner list->div_shift, list->div_width,
54529a30c26SHeiko Stuebner list->gate_offset, list->gate_shift,
546ef1d9feeSXing Zheng list->gate_flags, flags, &ctx->lock);
54729a30c26SHeiko Stuebner break;
548a4f182bfSLin Huang case branch_ddrclk:
549a4f182bfSLin Huang clk = rockchip_clk_register_ddrclk(
550a4f182bfSLin Huang list->name, list->flags,
551a4f182bfSLin Huang list->parent_names, list->num_parents,
552a4f182bfSLin Huang list->muxdiv_offset, list->mux_shift,
553a4f182bfSLin Huang list->mux_width, list->div_shift,
554a4f182bfSLin Huang list->div_width, list->div_flags,
555a4f182bfSLin Huang ctx->reg_base, &ctx->lock);
556a4f182bfSLin Huang break;
557a245fecbSHeiko Stübner }
558a245fecbSHeiko Stübner
559a245fecbSHeiko Stübner /* none of the cases above matched */
560a245fecbSHeiko Stübner if (!clk) {
561a245fecbSHeiko Stübner pr_err("%s: unknown clock type %d\n",
562a245fecbSHeiko Stübner __func__, list->branch_type);
563a245fecbSHeiko Stübner continue;
564a245fecbSHeiko Stübner }
565a245fecbSHeiko Stübner
566a245fecbSHeiko Stübner if (IS_ERR(clk)) {
567a245fecbSHeiko Stübner pr_err("%s: failed to register clock %s: %ld\n",
568a245fecbSHeiko Stübner __func__, list->name, PTR_ERR(clk));
569a245fecbSHeiko Stübner continue;
570a245fecbSHeiko Stübner }
571a245fecbSHeiko Stübner
572ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, clk, list->id);
573a245fecbSHeiko Stübner }
574a245fecbSHeiko Stübner }
575ea650c26SElaine Zhang EXPORT_SYMBOL_GPL(rockchip_clk_register_branches);
576fe94f974SHeiko Stübner
rockchip_clk_register_armclk(struct rockchip_clk_provider * ctx,unsigned int lookup_id,const char * name,const char * const * parent_names,u8 num_parents,const struct rockchip_cpuclk_reg_data * reg_data,const struct rockchip_cpuclk_rate_table * rates,int nrates)577ea650c26SElaine Zhang void rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx,
578ef1d9feeSXing Zheng unsigned int lookup_id,
5794a1caed3SUwe Kleine-König const char *name, const char *const *parent_names,
580f6fba5f6SHeiko Stuebner u8 num_parents,
581f6fba5f6SHeiko Stuebner const struct rockchip_cpuclk_reg_data *reg_data,
582f6fba5f6SHeiko Stuebner const struct rockchip_cpuclk_rate_table *rates,
583f6fba5f6SHeiko Stuebner int nrates)
584f6fba5f6SHeiko Stuebner {
585f6fba5f6SHeiko Stuebner struct clk *clk;
586f6fba5f6SHeiko Stuebner
587f6fba5f6SHeiko Stuebner clk = rockchip_clk_register_cpuclk(name, parent_names, num_parents,
58803ae1747SHeiko Stuebner reg_data, rates, nrates,
58903ae1747SHeiko Stuebner ctx->reg_base, &ctx->lock);
590f6fba5f6SHeiko Stuebner if (IS_ERR(clk)) {
591f6fba5f6SHeiko Stuebner pr_err("%s: failed to register clock %s: %ld\n",
592f6fba5f6SHeiko Stuebner __func__, name, PTR_ERR(clk));
593f6fba5f6SHeiko Stuebner return;
594f6fba5f6SHeiko Stuebner }
595f6fba5f6SHeiko Stuebner
596ef1d9feeSXing Zheng rockchip_clk_add_lookup(ctx, clk, lookup_id);
597f6fba5f6SHeiko Stuebner }
598ea650c26SElaine Zhang EXPORT_SYMBOL_GPL(rockchip_clk_register_armclk);
599f6fba5f6SHeiko Stuebner
rockchip_clk_protect_critical(const char * const clocks[],int nclocks)600ea650c26SElaine Zhang void rockchip_clk_protect_critical(const char *const clocks[],
601692d8328SUwe Kleine-König int nclocks)
602fe94f974SHeiko Stübner {
603fe94f974SHeiko Stübner int i;
604fe94f974SHeiko Stübner
605fe94f974SHeiko Stübner /* Protect the clocks that needs to stay on */
606fe94f974SHeiko Stübner for (i = 0; i < nclocks; i++) {
607fe94f974SHeiko Stübner struct clk *clk = __clk_lookup(clocks[i]);
608fe94f974SHeiko Stübner
609fe94f974SHeiko Stübner clk_prepare_enable(clk);
610fe94f974SHeiko Stübner }
611fe94f974SHeiko Stübner }
612ea650c26SElaine Zhang EXPORT_SYMBOL_GPL(rockchip_clk_protect_critical);
6136f1294b5SHeiko Stübner
614ef1d9feeSXing Zheng static void __iomem *rst_base;
6156f1294b5SHeiko Stübner static unsigned int reg_restart;
616dfff24bdSHeiko Stuebner static void (*cb_restart)(void);
rockchip_restart_notify(struct notifier_block * this,unsigned long mode,void * cmd)6176f1294b5SHeiko Stübner static int rockchip_restart_notify(struct notifier_block *this,
6186f1294b5SHeiko Stübner unsigned long mode, void *cmd)
6196f1294b5SHeiko Stübner {
620dfff24bdSHeiko Stuebner if (cb_restart)
621dfff24bdSHeiko Stuebner cb_restart();
622dfff24bdSHeiko Stuebner
623ef1d9feeSXing Zheng writel(0xfdb9, rst_base + reg_restart);
6246f1294b5SHeiko Stübner return NOTIFY_DONE;
6256f1294b5SHeiko Stübner }
6266f1294b5SHeiko Stübner
6276f1294b5SHeiko Stübner static struct notifier_block rockchip_restart_handler = {
6286f1294b5SHeiko Stübner .notifier_call = rockchip_restart_notify,
6296f1294b5SHeiko Stübner .priority = 128,
6306f1294b5SHeiko Stübner };
6316f1294b5SHeiko Stübner
632ea650c26SElaine Zhang void
rockchip_register_restart_notifier(struct rockchip_clk_provider * ctx,unsigned int reg,void (* cb)(void))63303ae1747SHeiko Stuebner rockchip_register_restart_notifier(struct rockchip_clk_provider *ctx,
63403ae1747SHeiko Stuebner unsigned int reg,
63503ae1747SHeiko Stuebner void (*cb)(void))
6366f1294b5SHeiko Stübner {
6376f1294b5SHeiko Stübner int ret;
6386f1294b5SHeiko Stübner
639ef1d9feeSXing Zheng rst_base = ctx->reg_base;
6406f1294b5SHeiko Stübner reg_restart = reg;
641dfff24bdSHeiko Stuebner cb_restart = cb;
6426f1294b5SHeiko Stübner ret = register_restart_handler(&rockchip_restart_handler);
6436f1294b5SHeiko Stübner if (ret)
6446f1294b5SHeiko Stübner pr_err("%s: cannot register restart handler, %d\n",
6456f1294b5SHeiko Stübner __func__, ret);
6466f1294b5SHeiko Stübner }
647ea650c26SElaine Zhang EXPORT_SYMBOL_GPL(rockchip_register_restart_notifier);
648