162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2014 MundoReader S.L.
462306a36Sopenharmony_ci * Author: Heiko Stuebner <heiko@sntech.de>
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * based on clk/samsung/clk-cpu.c
762306a36Sopenharmony_ci * Copyright (c) 2014 Samsung Electronics Co., Ltd.
862306a36Sopenharmony_ci * Author: Thomas Abraham <thomas.ab@samsung.com>
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * A CPU clock is defined as a clock supplied to a CPU or a group of CPUs.
1162306a36Sopenharmony_ci * The CPU clock is typically derived from a hierarchy of clock
1262306a36Sopenharmony_ci * blocks which includes mux and divider blocks. There are a number of other
1362306a36Sopenharmony_ci * auxiliary clocks supplied to the CPU domain such as the debug blocks and AXI
1462306a36Sopenharmony_ci * clock for CPU domain. The rates of these auxiliary clocks are related to the
1562306a36Sopenharmony_ci * CPU clock rate and this relation is usually specified in the hardware manual
1662306a36Sopenharmony_ci * of the SoC or supplied after the SoC characterization.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * The below implementation of the CPU clock allows the rate changes of the CPU
1962306a36Sopenharmony_ci * clock and the corresponding rate changes of the auxillary clocks of the CPU
2062306a36Sopenharmony_ci * domain. The platform clock driver provides a clock register configuration
2162306a36Sopenharmony_ci * for each configurable rate which is then used to program the clock hardware
2262306a36Sopenharmony_ci * registers to acheive a fast co-oridinated rate change for all the CPU domain
2362306a36Sopenharmony_ci * clocks.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * On a rate change request for the CPU clock, the rate change is propagated
2662306a36Sopenharmony_ci * upto the PLL supplying the clock to the CPU domain clock blocks. While the
2762306a36Sopenharmony_ci * CPU domain PLL is reconfigured, the CPU domain clocks are driven using an
2862306a36Sopenharmony_ci * alternate clock source. If required, the alternate clock source is divided
2962306a36Sopenharmony_ci * down in order to keep the output clock rate within the previous OPP limits.
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#include <linux/of.h>
3362306a36Sopenharmony_ci#include <linux/slab.h>
3462306a36Sopenharmony_ci#include <linux/io.h>
3562306a36Sopenharmony_ci#include <linux/clk.h>
3662306a36Sopenharmony_ci#include <linux/clk-provider.h>
3762306a36Sopenharmony_ci#include "clk.h"
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci/**
4062306a36Sopenharmony_ci * struct rockchip_cpuclk: information about clock supplied to a CPU core.
4162306a36Sopenharmony_ci * @hw:		handle between ccf and cpu clock.
4262306a36Sopenharmony_ci * @alt_parent:	alternate parent clock to use when switching the speed
4362306a36Sopenharmony_ci *		of the primary parent clock.
4462306a36Sopenharmony_ci * @reg_base:	base register for cpu-clock values.
4562306a36Sopenharmony_ci * @clk_nb:	clock notifier registered for changes in clock speed of the
4662306a36Sopenharmony_ci *		primary parent clock.
4762306a36Sopenharmony_ci * @rate_count:	number of rates in the rate_table
4862306a36Sopenharmony_ci * @rate_table:	pll-rates and their associated dividers
4962306a36Sopenharmony_ci * @reg_data:	cpu-specific register settings
5062306a36Sopenharmony_ci * @lock:	clock lock
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_cistruct rockchip_cpuclk {
5362306a36Sopenharmony_ci	struct clk_hw				hw;
5462306a36Sopenharmony_ci	struct clk				*alt_parent;
5562306a36Sopenharmony_ci	void __iomem				*reg_base;
5662306a36Sopenharmony_ci	struct notifier_block			clk_nb;
5762306a36Sopenharmony_ci	unsigned int				rate_count;
5862306a36Sopenharmony_ci	struct rockchip_cpuclk_rate_table	*rate_table;
5962306a36Sopenharmony_ci	const struct rockchip_cpuclk_reg_data	*reg_data;
6062306a36Sopenharmony_ci	spinlock_t				*lock;
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci#define to_rockchip_cpuclk_hw(hw) container_of(hw, struct rockchip_cpuclk, hw)
6462306a36Sopenharmony_ci#define to_rockchip_cpuclk_nb(nb) \
6562306a36Sopenharmony_ci			container_of(nb, struct rockchip_cpuclk, clk_nb)
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistatic const struct rockchip_cpuclk_rate_table *rockchip_get_cpuclk_settings(
6862306a36Sopenharmony_ci			    struct rockchip_cpuclk *cpuclk, unsigned long rate)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	const struct rockchip_cpuclk_rate_table *rate_table =
7162306a36Sopenharmony_ci							cpuclk->rate_table;
7262306a36Sopenharmony_ci	int i;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	for (i = 0; i < cpuclk->rate_count; i++) {
7562306a36Sopenharmony_ci		if (rate == rate_table[i].prate)
7662306a36Sopenharmony_ci			return &rate_table[i];
7762306a36Sopenharmony_ci	}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	return NULL;
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic unsigned long rockchip_cpuclk_recalc_rate(struct clk_hw *hw,
8362306a36Sopenharmony_ci					unsigned long parent_rate)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_hw(hw);
8662306a36Sopenharmony_ci	const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
8762306a36Sopenharmony_ci	u32 clksel0 = readl_relaxed(cpuclk->reg_base + reg_data->core_reg[0]);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	clksel0 >>= reg_data->div_core_shift[0];
9062306a36Sopenharmony_ci	clksel0 &= reg_data->div_core_mask[0];
9162306a36Sopenharmony_ci	return parent_rate / (clksel0 + 1);
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistatic const struct clk_ops rockchip_cpuclk_ops = {
9562306a36Sopenharmony_ci	.recalc_rate = rockchip_cpuclk_recalc_rate,
9662306a36Sopenharmony_ci};
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_cistatic void rockchip_cpuclk_set_dividers(struct rockchip_cpuclk *cpuclk,
9962306a36Sopenharmony_ci				const struct rockchip_cpuclk_rate_table *rate)
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci	int i;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	/* alternate parent is active now. set the dividers */
10462306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rate->divs); i++) {
10562306a36Sopenharmony_ci		const struct rockchip_cpuclk_clksel *clksel = &rate->divs[i];
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci		if (!clksel->reg)
10862306a36Sopenharmony_ci			continue;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci		pr_debug("%s: setting reg 0x%x to 0x%x\n",
11162306a36Sopenharmony_ci			 __func__, clksel->reg, clksel->val);
11262306a36Sopenharmony_ci		writel(clksel->val, cpuclk->reg_base + clksel->reg);
11362306a36Sopenharmony_ci	}
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic void rockchip_cpuclk_set_pre_muxs(struct rockchip_cpuclk *cpuclk,
11762306a36Sopenharmony_ci					 const struct rockchip_cpuclk_rate_table *rate)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	int i;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	/* alternate parent is active now. set the pre_muxs */
12262306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rate->pre_muxs); i++) {
12362306a36Sopenharmony_ci		const struct rockchip_cpuclk_clksel *clksel = &rate->pre_muxs[i];
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci		if (!clksel->reg)
12662306a36Sopenharmony_ci			break;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci		pr_debug("%s: setting reg 0x%x to 0x%x\n",
12962306a36Sopenharmony_ci			 __func__, clksel->reg, clksel->val);
13062306a36Sopenharmony_ci		writel(clksel->val, cpuclk->reg_base + clksel->reg);
13162306a36Sopenharmony_ci	}
13262306a36Sopenharmony_ci}
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cistatic void rockchip_cpuclk_set_post_muxs(struct rockchip_cpuclk *cpuclk,
13562306a36Sopenharmony_ci					  const struct rockchip_cpuclk_rate_table *rate)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	int i;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	/* alternate parent is active now. set the muxs */
14062306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rate->post_muxs); i++) {
14162306a36Sopenharmony_ci		const struct rockchip_cpuclk_clksel *clksel = &rate->post_muxs[i];
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci		if (!clksel->reg)
14462306a36Sopenharmony_ci			break;
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci		pr_debug("%s: setting reg 0x%x to 0x%x\n",
14762306a36Sopenharmony_ci			 __func__, clksel->reg, clksel->val);
14862306a36Sopenharmony_ci		writel(clksel->val, cpuclk->reg_base + clksel->reg);
14962306a36Sopenharmony_ci	}
15062306a36Sopenharmony_ci}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cistatic int rockchip_cpuclk_pre_rate_change(struct rockchip_cpuclk *cpuclk,
15362306a36Sopenharmony_ci					   struct clk_notifier_data *ndata)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
15662306a36Sopenharmony_ci	const struct rockchip_cpuclk_rate_table *rate;
15762306a36Sopenharmony_ci	unsigned long alt_prate, alt_div;
15862306a36Sopenharmony_ci	unsigned long flags;
15962306a36Sopenharmony_ci	int i = 0;
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	/* check validity of the new rate */
16262306a36Sopenharmony_ci	rate = rockchip_get_cpuclk_settings(cpuclk, ndata->new_rate);
16362306a36Sopenharmony_ci	if (!rate) {
16462306a36Sopenharmony_ci		pr_err("%s: Invalid rate : %lu for cpuclk\n",
16562306a36Sopenharmony_ci		       __func__, ndata->new_rate);
16662306a36Sopenharmony_ci		return -EINVAL;
16762306a36Sopenharmony_ci	}
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	alt_prate = clk_get_rate(cpuclk->alt_parent);
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	spin_lock_irqsave(cpuclk->lock, flags);
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	/*
17462306a36Sopenharmony_ci	 * If the old parent clock speed is less than the clock speed
17562306a36Sopenharmony_ci	 * of the alternate parent, then it should be ensured that at no point
17662306a36Sopenharmony_ci	 * the armclk speed is more than the old_rate until the dividers are
17762306a36Sopenharmony_ci	 * set.
17862306a36Sopenharmony_ci	 */
17962306a36Sopenharmony_ci	if (alt_prate > ndata->old_rate) {
18062306a36Sopenharmony_ci		/* calculate dividers */
18162306a36Sopenharmony_ci		alt_div =  DIV_ROUND_UP(alt_prate, ndata->old_rate) - 1;
18262306a36Sopenharmony_ci		if (alt_div > reg_data->div_core_mask[0]) {
18362306a36Sopenharmony_ci			pr_warn("%s: limiting alt-divider %lu to %d\n",
18462306a36Sopenharmony_ci				__func__, alt_div, reg_data->div_core_mask[0]);
18562306a36Sopenharmony_ci			alt_div = reg_data->div_core_mask[0];
18662306a36Sopenharmony_ci		}
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci		/*
18962306a36Sopenharmony_ci		 * Change parents and add dividers in a single transaction.
19062306a36Sopenharmony_ci		 *
19162306a36Sopenharmony_ci		 * NOTE: we do this in a single transaction so we're never
19262306a36Sopenharmony_ci		 * dividing the primary parent by the extra dividers that were
19362306a36Sopenharmony_ci		 * needed for the alt.
19462306a36Sopenharmony_ci		 */
19562306a36Sopenharmony_ci		pr_debug("%s: setting div %lu as alt-rate %lu > old-rate %lu\n",
19662306a36Sopenharmony_ci			 __func__, alt_div, alt_prate, ndata->old_rate);
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci		for (i = 0; i < reg_data->num_cores; i++) {
19962306a36Sopenharmony_ci			writel(HIWORD_UPDATE(alt_div, reg_data->div_core_mask[i],
20062306a36Sopenharmony_ci					     reg_data->div_core_shift[i]),
20162306a36Sopenharmony_ci			       cpuclk->reg_base + reg_data->core_reg[i]);
20262306a36Sopenharmony_ci		}
20362306a36Sopenharmony_ci	}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	rockchip_cpuclk_set_pre_muxs(cpuclk, rate);
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	/* select alternate parent */
20862306a36Sopenharmony_ci	if (reg_data->mux_core_reg)
20962306a36Sopenharmony_ci		writel(HIWORD_UPDATE(reg_data->mux_core_alt,
21062306a36Sopenharmony_ci				     reg_data->mux_core_mask,
21162306a36Sopenharmony_ci				     reg_data->mux_core_shift),
21262306a36Sopenharmony_ci		       cpuclk->reg_base + reg_data->mux_core_reg);
21362306a36Sopenharmony_ci	else
21462306a36Sopenharmony_ci		writel(HIWORD_UPDATE(reg_data->mux_core_alt,
21562306a36Sopenharmony_ci				     reg_data->mux_core_mask,
21662306a36Sopenharmony_ci				     reg_data->mux_core_shift),
21762306a36Sopenharmony_ci		       cpuclk->reg_base + reg_data->core_reg[0]);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	spin_unlock_irqrestore(cpuclk->lock, flags);
22062306a36Sopenharmony_ci	return 0;
22162306a36Sopenharmony_ci}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_cistatic int rockchip_cpuclk_post_rate_change(struct rockchip_cpuclk *cpuclk,
22462306a36Sopenharmony_ci					    struct clk_notifier_data *ndata)
22562306a36Sopenharmony_ci{
22662306a36Sopenharmony_ci	const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
22762306a36Sopenharmony_ci	const struct rockchip_cpuclk_rate_table *rate;
22862306a36Sopenharmony_ci	unsigned long flags;
22962306a36Sopenharmony_ci	int i = 0;
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	rate = rockchip_get_cpuclk_settings(cpuclk, ndata->new_rate);
23262306a36Sopenharmony_ci	if (!rate) {
23362306a36Sopenharmony_ci		pr_err("%s: Invalid rate : %lu for cpuclk\n",
23462306a36Sopenharmony_ci		       __func__, ndata->new_rate);
23562306a36Sopenharmony_ci		return -EINVAL;
23662306a36Sopenharmony_ci	}
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	spin_lock_irqsave(cpuclk->lock, flags);
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	if (ndata->old_rate < ndata->new_rate)
24162306a36Sopenharmony_ci		rockchip_cpuclk_set_dividers(cpuclk, rate);
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	/*
24462306a36Sopenharmony_ci	 * post-rate change event, re-mux to primary parent and remove dividers.
24562306a36Sopenharmony_ci	 *
24662306a36Sopenharmony_ci	 * NOTE: we do this in a single transaction so we're never dividing the
24762306a36Sopenharmony_ci	 * primary parent by the extra dividers that were needed for the alt.
24862306a36Sopenharmony_ci	 */
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	if (reg_data->mux_core_reg)
25162306a36Sopenharmony_ci		writel(HIWORD_UPDATE(reg_data->mux_core_main,
25262306a36Sopenharmony_ci				     reg_data->mux_core_mask,
25362306a36Sopenharmony_ci				     reg_data->mux_core_shift),
25462306a36Sopenharmony_ci		       cpuclk->reg_base + reg_data->mux_core_reg);
25562306a36Sopenharmony_ci	else
25662306a36Sopenharmony_ci		writel(HIWORD_UPDATE(reg_data->mux_core_main,
25762306a36Sopenharmony_ci				     reg_data->mux_core_mask,
25862306a36Sopenharmony_ci				     reg_data->mux_core_shift),
25962306a36Sopenharmony_ci		       cpuclk->reg_base + reg_data->core_reg[0]);
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	rockchip_cpuclk_set_post_muxs(cpuclk, rate);
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	/* remove dividers */
26462306a36Sopenharmony_ci	for (i = 0; i < reg_data->num_cores; i++) {
26562306a36Sopenharmony_ci		writel(HIWORD_UPDATE(0, reg_data->div_core_mask[i],
26662306a36Sopenharmony_ci				     reg_data->div_core_shift[i]),
26762306a36Sopenharmony_ci		       cpuclk->reg_base + reg_data->core_reg[i]);
26862306a36Sopenharmony_ci	}
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	if (ndata->old_rate > ndata->new_rate)
27162306a36Sopenharmony_ci		rockchip_cpuclk_set_dividers(cpuclk, rate);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	spin_unlock_irqrestore(cpuclk->lock, flags);
27462306a36Sopenharmony_ci	return 0;
27562306a36Sopenharmony_ci}
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci/*
27862306a36Sopenharmony_ci * This clock notifier is called when the frequency of the parent clock
27962306a36Sopenharmony_ci * of cpuclk is to be changed. This notifier handles the setting up all
28062306a36Sopenharmony_ci * the divider clocks, remux to temporary parent and handling the safe
28162306a36Sopenharmony_ci * frequency levels when using temporary parent.
28262306a36Sopenharmony_ci */
28362306a36Sopenharmony_cistatic int rockchip_cpuclk_notifier_cb(struct notifier_block *nb,
28462306a36Sopenharmony_ci					unsigned long event, void *data)
28562306a36Sopenharmony_ci{
28662306a36Sopenharmony_ci	struct clk_notifier_data *ndata = data;
28762306a36Sopenharmony_ci	struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_nb(nb);
28862306a36Sopenharmony_ci	int ret = 0;
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
29162306a36Sopenharmony_ci		 __func__, event, ndata->old_rate, ndata->new_rate);
29262306a36Sopenharmony_ci	if (event == PRE_RATE_CHANGE)
29362306a36Sopenharmony_ci		ret = rockchip_cpuclk_pre_rate_change(cpuclk, ndata);
29462306a36Sopenharmony_ci	else if (event == POST_RATE_CHANGE)
29562306a36Sopenharmony_ci		ret = rockchip_cpuclk_post_rate_change(cpuclk, ndata);
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci	return notifier_from_errno(ret);
29862306a36Sopenharmony_ci}
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_cistruct clk *rockchip_clk_register_cpuclk(const char *name,
30162306a36Sopenharmony_ci			const char *const *parent_names, u8 num_parents,
30262306a36Sopenharmony_ci			const struct rockchip_cpuclk_reg_data *reg_data,
30362306a36Sopenharmony_ci			const struct rockchip_cpuclk_rate_table *rates,
30462306a36Sopenharmony_ci			int nrates, void __iomem *reg_base, spinlock_t *lock)
30562306a36Sopenharmony_ci{
30662306a36Sopenharmony_ci	struct rockchip_cpuclk *cpuclk;
30762306a36Sopenharmony_ci	struct clk_init_data init;
30862306a36Sopenharmony_ci	struct clk *clk, *cclk;
30962306a36Sopenharmony_ci	int ret;
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci	if (num_parents < 2) {
31262306a36Sopenharmony_ci		pr_err("%s: needs at least two parent clocks\n", __func__);
31362306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
31462306a36Sopenharmony_ci	}
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	cpuclk = kzalloc(sizeof(*cpuclk), GFP_KERNEL);
31762306a36Sopenharmony_ci	if (!cpuclk)
31862306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	init.name = name;
32162306a36Sopenharmony_ci	init.parent_names = &parent_names[reg_data->mux_core_main];
32262306a36Sopenharmony_ci	init.num_parents = 1;
32362306a36Sopenharmony_ci	init.ops = &rockchip_cpuclk_ops;
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	/* only allow rate changes when we have a rate table */
32662306a36Sopenharmony_ci	init.flags = (nrates > 0) ? CLK_SET_RATE_PARENT : 0;
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci	/* disallow automatic parent changes by ccf */
32962306a36Sopenharmony_ci	init.flags |= CLK_SET_RATE_NO_REPARENT;
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	init.flags |= CLK_GET_RATE_NOCACHE;
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	cpuclk->reg_base = reg_base;
33462306a36Sopenharmony_ci	cpuclk->lock = lock;
33562306a36Sopenharmony_ci	cpuclk->reg_data = reg_data;
33662306a36Sopenharmony_ci	cpuclk->clk_nb.notifier_call = rockchip_cpuclk_notifier_cb;
33762306a36Sopenharmony_ci	cpuclk->hw.init = &init;
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	cpuclk->alt_parent = __clk_lookup(parent_names[reg_data->mux_core_alt]);
34062306a36Sopenharmony_ci	if (!cpuclk->alt_parent) {
34162306a36Sopenharmony_ci		pr_err("%s: could not lookup alternate parent: (%d)\n",
34262306a36Sopenharmony_ci		       __func__, reg_data->mux_core_alt);
34362306a36Sopenharmony_ci		ret = -EINVAL;
34462306a36Sopenharmony_ci		goto free_cpuclk;
34562306a36Sopenharmony_ci	}
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci	ret = clk_prepare_enable(cpuclk->alt_parent);
34862306a36Sopenharmony_ci	if (ret) {
34962306a36Sopenharmony_ci		pr_err("%s: could not enable alternate parent\n",
35062306a36Sopenharmony_ci		       __func__);
35162306a36Sopenharmony_ci		goto free_cpuclk;
35262306a36Sopenharmony_ci	}
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci	clk = __clk_lookup(parent_names[reg_data->mux_core_main]);
35562306a36Sopenharmony_ci	if (!clk) {
35662306a36Sopenharmony_ci		pr_err("%s: could not lookup parent clock: (%d) %s\n",
35762306a36Sopenharmony_ci		       __func__, reg_data->mux_core_main,
35862306a36Sopenharmony_ci		       parent_names[reg_data->mux_core_main]);
35962306a36Sopenharmony_ci		ret = -EINVAL;
36062306a36Sopenharmony_ci		goto free_alt_parent;
36162306a36Sopenharmony_ci	}
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	ret = clk_notifier_register(clk, &cpuclk->clk_nb);
36462306a36Sopenharmony_ci	if (ret) {
36562306a36Sopenharmony_ci		pr_err("%s: failed to register clock notifier for %s\n",
36662306a36Sopenharmony_ci				__func__, name);
36762306a36Sopenharmony_ci		goto free_alt_parent;
36862306a36Sopenharmony_ci	}
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	if (nrates > 0) {
37162306a36Sopenharmony_ci		cpuclk->rate_count = nrates;
37262306a36Sopenharmony_ci		cpuclk->rate_table = kmemdup(rates,
37362306a36Sopenharmony_ci					     sizeof(*rates) * nrates,
37462306a36Sopenharmony_ci					     GFP_KERNEL);
37562306a36Sopenharmony_ci		if (!cpuclk->rate_table) {
37662306a36Sopenharmony_ci			ret = -ENOMEM;
37762306a36Sopenharmony_ci			goto unregister_notifier;
37862306a36Sopenharmony_ci		}
37962306a36Sopenharmony_ci	}
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci	cclk = clk_register(NULL, &cpuclk->hw);
38262306a36Sopenharmony_ci	if (IS_ERR(cclk)) {
38362306a36Sopenharmony_ci		pr_err("%s: could not register cpuclk %s\n", __func__,	name);
38462306a36Sopenharmony_ci		ret = PTR_ERR(cclk);
38562306a36Sopenharmony_ci		goto free_rate_table;
38662306a36Sopenharmony_ci	}
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	return cclk;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_cifree_rate_table:
39162306a36Sopenharmony_ci	kfree(cpuclk->rate_table);
39262306a36Sopenharmony_ciunregister_notifier:
39362306a36Sopenharmony_ci	clk_notifier_unregister(clk, &cpuclk->clk_nb);
39462306a36Sopenharmony_cifree_alt_parent:
39562306a36Sopenharmony_ci	clk_disable_unprepare(cpuclk->alt_parent);
39662306a36Sopenharmony_cifree_cpuclk:
39762306a36Sopenharmony_ci	kfree(cpuclk);
39862306a36Sopenharmony_ci	return ERR_PTR(ret);
39962306a36Sopenharmony_ci}
400