2019-05-19 21:51:31 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-11-13 08:51:23 +08:00
|
|
|
/*
|
|
|
|
* Hisilicon clock driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012-2013 Hisilicon Limited.
|
|
|
|
* Copyright (c) 2012-2013 Linaro Limited.
|
|
|
|
*
|
|
|
|
* Author: Haojian Zhuang <haojian.zhuang@linaro.org>
|
|
|
|
* Xin Li <li.xin@linaro.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/clkdev.h>
|
2015-06-20 06:00:46 +08:00
|
|
|
#include <linux/clk-provider.h>
|
2013-11-13 08:51:23 +08:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_address.h>
|
|
|
|
#include <linux/of_device.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
#include "clk.h"
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(hisi_clk_lock);
|
|
|
|
|
2016-06-15 14:26:35 +08:00
|
|
|
struct hisi_clock_data *hisi_clk_alloc(struct platform_device *pdev,
|
|
|
|
int nr_clks)
|
|
|
|
{
|
|
|
|
struct hisi_clock_data *clk_data;
|
|
|
|
struct resource *res;
|
|
|
|
struct clk **clk_table;
|
|
|
|
|
|
|
|
clk_data = devm_kmalloc(&pdev->dev, sizeof(*clk_data), GFP_KERNEL);
|
|
|
|
if (!clk_data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
clk: hisilicon: fix potential NULL dereference in hisi_clk_alloc()
platform_get_resource() may fail and return NULL, so we should
better check it's return value to avoid a NULL pointer dereference
a bit later in the code.
This is detected by Coccinelle semantic patch.
@@
expression pdev, res, n, t, e, e1, e2;
@@
res = platform_get_resource(pdev, t, n);
+ if (!res)
+ return -EINVAL;
... when != res == NULL
e = devm_ioremap(e1, res->start, e2);
Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
Fixes: 322269163a36 ("clk: hisilicon: add hisi_clk_alloc function.")
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
2018-03-20 22:19:34 +08:00
|
|
|
if (!res)
|
|
|
|
return NULL;
|
2016-06-15 14:26:35 +08:00
|
|
|
clk_data->base = devm_ioremap(&pdev->dev,
|
|
|
|
res->start, resource_size(res));
|
|
|
|
if (!clk_data->base)
|
|
|
|
return NULL;
|
|
|
|
|
2017-04-18 16:15:19 +08:00
|
|
|
clk_table = devm_kmalloc_array(&pdev->dev, nr_clks,
|
|
|
|
sizeof(*clk_table),
|
|
|
|
GFP_KERNEL);
|
2016-06-15 14:26:35 +08:00
|
|
|
if (!clk_table)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
clk_data->clk_data.clks = clk_table;
|
|
|
|
clk_data->clk_data.clk_num = nr_clks;
|
|
|
|
|
|
|
|
return clk_data;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(hisi_clk_alloc);
|
|
|
|
|
2016-04-23 15:40:29 +08:00
|
|
|
struct hisi_clock_data *hisi_clk_init(struct device_node *np,
|
2013-12-24 21:38:26 +08:00
|
|
|
int nr_clks)
|
2013-11-13 08:51:23 +08:00
|
|
|
{
|
2013-12-24 21:38:26 +08:00
|
|
|
struct hisi_clock_data *clk_data;
|
|
|
|
struct clk **clk_table;
|
|
|
|
void __iomem *base;
|
|
|
|
|
2015-08-03 09:13:34 +08:00
|
|
|
base = of_iomap(np, 0);
|
|
|
|
if (!base) {
|
|
|
|
pr_err("%s: failed to map clock registers\n", __func__);
|
2013-12-24 21:38:26 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
|
2017-04-18 16:12:32 +08:00
|
|
|
if (!clk_data)
|
2013-12-24 21:38:26 +08:00
|
|
|
goto err;
|
2017-04-18 16:12:32 +08:00
|
|
|
|
2013-12-24 21:38:26 +08:00
|
|
|
clk_data->base = base;
|
2017-04-18 15:25:47 +08:00
|
|
|
clk_table = kcalloc(nr_clks, sizeof(*clk_table), GFP_KERNEL);
|
2017-04-18 16:12:32 +08:00
|
|
|
if (!clk_table)
|
2013-12-24 21:38:26 +08:00
|
|
|
goto err_data;
|
2017-04-18 16:12:32 +08:00
|
|
|
|
2013-12-24 21:38:26 +08:00
|
|
|
clk_data->clk_data.clks = clk_table;
|
|
|
|
clk_data->clk_data.clk_num = nr_clks;
|
|
|
|
of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data->clk_data);
|
|
|
|
return clk_data;
|
|
|
|
err_data:
|
|
|
|
kfree(clk_data);
|
|
|
|
err:
|
|
|
|
return NULL;
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2016-04-23 15:40:29 +08:00
|
|
|
EXPORT_SYMBOL_GPL(hisi_clk_init);
|
2013-11-13 08:51:23 +08:00
|
|
|
|
2016-06-15 14:26:36 +08:00
|
|
|
int hisi_clk_register_fixed_rate(const struct hisi_fixed_rate_clock *clks,
|
2013-12-24 21:38:26 +08:00
|
|
|
int nums, struct hisi_clock_data *data)
|
2013-11-13 08:51:23 +08:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nums; i++) {
|
|
|
|
clk = clk_register_fixed_rate(NULL, clks[i].name,
|
|
|
|
clks[i].parent_name,
|
|
|
|
clks[i].flags,
|
|
|
|
clks[i].fixed_rate);
|
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n",
|
|
|
|
__func__, clks[i].name);
|
2016-06-15 14:26:36 +08:00
|
|
|
goto err;
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2013-12-24 21:38:26 +08:00
|
|
|
data->clk_data.clks[clks[i].id] = clk;
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2016-06-15 14:26:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
while (i--)
|
|
|
|
clk_unregister_fixed_rate(data->clk_data.clks[clks[i].id]);
|
|
|
|
|
|
|
|
return PTR_ERR(clk);
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2016-04-23 15:40:29 +08:00
|
|
|
EXPORT_SYMBOL_GPL(hisi_clk_register_fixed_rate);
|
2013-11-13 08:51:23 +08:00
|
|
|
|
2016-06-15 14:26:36 +08:00
|
|
|
int hisi_clk_register_fixed_factor(const struct hisi_fixed_factor_clock *clks,
|
2013-12-24 21:38:26 +08:00
|
|
|
int nums,
|
|
|
|
struct hisi_clock_data *data)
|
2013-11-13 08:51:23 +08:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nums; i++) {
|
|
|
|
clk = clk_register_fixed_factor(NULL, clks[i].name,
|
|
|
|
clks[i].parent_name,
|
|
|
|
clks[i].flags, clks[i].mult,
|
|
|
|
clks[i].div);
|
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n",
|
|
|
|
__func__, clks[i].name);
|
2016-06-15 14:26:36 +08:00
|
|
|
goto err;
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2013-12-24 21:38:26 +08:00
|
|
|
data->clk_data.clks[clks[i].id] = clk;
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2016-06-15 14:26:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
while (i--)
|
|
|
|
clk_unregister_fixed_factor(data->clk_data.clks[clks[i].id]);
|
|
|
|
|
|
|
|
return PTR_ERR(clk);
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2016-04-23 15:40:29 +08:00
|
|
|
EXPORT_SYMBOL_GPL(hisi_clk_register_fixed_factor);
|
2013-11-13 08:51:23 +08:00
|
|
|
|
2016-06-15 14:26:36 +08:00
|
|
|
int hisi_clk_register_mux(const struct hisi_mux_clock *clks,
|
2013-12-24 21:38:26 +08:00
|
|
|
int nums, struct hisi_clock_data *data)
|
2013-11-13 08:51:23 +08:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
2013-12-24 21:38:26 +08:00
|
|
|
void __iomem *base = data->base;
|
2013-11-13 08:51:23 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nums; i++) {
|
2014-04-21 11:35:21 +08:00
|
|
|
u32 mask = BIT(clks[i].width) - 1;
|
|
|
|
|
|
|
|
clk = clk_register_mux_table(NULL, clks[i].name,
|
|
|
|
clks[i].parent_names,
|
|
|
|
clks[i].num_parents, clks[i].flags,
|
|
|
|
base + clks[i].offset, clks[i].shift,
|
|
|
|
mask, clks[i].mux_flags,
|
|
|
|
clks[i].table, &hisi_clk_lock);
|
2013-11-13 08:51:23 +08:00
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n",
|
|
|
|
__func__, clks[i].name);
|
2016-06-15 14:26:36 +08:00
|
|
|
goto err;
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clks[i].alias)
|
|
|
|
clk_register_clkdev(clk, clks[i].alias, NULL);
|
|
|
|
|
2013-12-24 21:38:26 +08:00
|
|
|
data->clk_data.clks[clks[i].id] = clk;
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2016-06-15 14:26:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
while (i--)
|
|
|
|
clk_unregister_mux(data->clk_data.clks[clks[i].id]);
|
|
|
|
|
|
|
|
return PTR_ERR(clk);
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2016-04-23 15:40:29 +08:00
|
|
|
EXPORT_SYMBOL_GPL(hisi_clk_register_mux);
|
2013-11-13 08:51:23 +08:00
|
|
|
|
2018-03-05 15:01:31 +08:00
|
|
|
int hisi_clk_register_phase(struct device *dev,
|
|
|
|
const struct hisi_phase_clock *clks,
|
|
|
|
int nums, struct hisi_clock_data *data)
|
|
|
|
{
|
|
|
|
void __iomem *base = data->base;
|
|
|
|
struct clk *clk;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nums; i++) {
|
|
|
|
clk = clk_register_hisi_phase(dev, &clks[i], base,
|
|
|
|
&hisi_clk_lock);
|
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n", __func__,
|
|
|
|
clks[i].name);
|
|
|
|
return PTR_ERR(clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
data->clk_data.clks[clks[i].id] = clk;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(hisi_clk_register_phase);
|
|
|
|
|
2016-06-15 14:26:36 +08:00
|
|
|
int hisi_clk_register_divider(const struct hisi_divider_clock *clks,
|
2013-12-24 21:38:26 +08:00
|
|
|
int nums, struct hisi_clock_data *data)
|
2013-11-13 08:51:23 +08:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
2013-12-24 21:38:26 +08:00
|
|
|
void __iomem *base = data->base;
|
2013-11-13 08:51:23 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nums; i++) {
|
|
|
|
clk = clk_register_divider_table(NULL, clks[i].name,
|
|
|
|
clks[i].parent_name,
|
|
|
|
clks[i].flags,
|
|
|
|
base + clks[i].offset,
|
|
|
|
clks[i].shift, clks[i].width,
|
|
|
|
clks[i].div_flags,
|
|
|
|
clks[i].table,
|
|
|
|
&hisi_clk_lock);
|
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n",
|
|
|
|
__func__, clks[i].name);
|
2016-06-15 14:26:36 +08:00
|
|
|
goto err;
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clks[i].alias)
|
|
|
|
clk_register_clkdev(clk, clks[i].alias, NULL);
|
|
|
|
|
2013-12-24 21:38:26 +08:00
|
|
|
data->clk_data.clks[clks[i].id] = clk;
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2016-06-15 14:26:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
while (i--)
|
|
|
|
clk_unregister_divider(data->clk_data.clks[clks[i].id]);
|
|
|
|
|
|
|
|
return PTR_ERR(clk);
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
2016-04-23 15:40:29 +08:00
|
|
|
EXPORT_SYMBOL_GPL(hisi_clk_register_divider);
|
2013-11-13 08:51:23 +08:00
|
|
|
|
2016-06-15 14:26:36 +08:00
|
|
|
int hisi_clk_register_gate(const struct hisi_gate_clock *clks,
|
2014-04-22 15:42:47 +08:00
|
|
|
int nums, struct hisi_clock_data *data)
|
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
void __iomem *base = data->base;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nums; i++) {
|
|
|
|
clk = clk_register_gate(NULL, clks[i].name,
|
|
|
|
clks[i].parent_name,
|
|
|
|
clks[i].flags,
|
|
|
|
base + clks[i].offset,
|
|
|
|
clks[i].bit_idx,
|
|
|
|
clks[i].gate_flags,
|
|
|
|
&hisi_clk_lock);
|
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n",
|
|
|
|
__func__, clks[i].name);
|
2016-06-15 14:26:36 +08:00
|
|
|
goto err;
|
2014-04-22 15:42:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clks[i].alias)
|
|
|
|
clk_register_clkdev(clk, clks[i].alias, NULL);
|
|
|
|
|
|
|
|
data->clk_data.clks[clks[i].id] = clk;
|
|
|
|
}
|
2016-06-15 14:26:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
while (i--)
|
|
|
|
clk_unregister_gate(data->clk_data.clks[clks[i].id]);
|
|
|
|
|
|
|
|
return PTR_ERR(clk);
|
2014-04-22 15:42:47 +08:00
|
|
|
}
|
2016-04-23 15:40:29 +08:00
|
|
|
EXPORT_SYMBOL_GPL(hisi_clk_register_gate);
|
2014-04-22 15:42:47 +08:00
|
|
|
|
2016-04-23 15:40:29 +08:00
|
|
|
void hisi_clk_register_gate_sep(const struct hisi_gate_clock *clks,
|
2013-12-24 21:38:26 +08:00
|
|
|
int nums, struct hisi_clock_data *data)
|
2013-11-13 08:51:23 +08:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
2013-12-24 21:38:26 +08:00
|
|
|
void __iomem *base = data->base;
|
2013-11-13 08:51:23 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nums; i++) {
|
|
|
|
clk = hisi_register_clkgate_sep(NULL, clks[i].name,
|
|
|
|
clks[i].parent_name,
|
|
|
|
clks[i].flags,
|
|
|
|
base + clks[i].offset,
|
|
|
|
clks[i].bit_idx,
|
|
|
|
clks[i].gate_flags,
|
|
|
|
&hisi_clk_lock);
|
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n",
|
|
|
|
__func__, clks[i].name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clks[i].alias)
|
|
|
|
clk_register_clkdev(clk, clks[i].alias, NULL);
|
|
|
|
|
2013-12-24 21:38:26 +08:00
|
|
|
data->clk_data.clks[clks[i].id] = clk;
|
2013-11-13 08:51:23 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-23 15:40:29 +08:00
|
|
|
EXPORT_SYMBOL_GPL(hisi_clk_register_gate_sep);
|
2015-05-29 10:08:38 +08:00
|
|
|
|
2016-04-23 15:40:29 +08:00
|
|
|
void __init hi6220_clk_register_divider(const struct hi6220_divider_clock *clks,
|
2015-05-29 10:08:38 +08:00
|
|
|
int nums, struct hisi_clock_data *data)
|
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
void __iomem *base = data->base;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nums; i++) {
|
|
|
|
clk = hi6220_register_clkdiv(NULL, clks[i].name,
|
|
|
|
clks[i].parent_name,
|
|
|
|
clks[i].flags,
|
|
|
|
base + clks[i].offset,
|
|
|
|
clks[i].shift,
|
|
|
|
clks[i].width,
|
|
|
|
clks[i].mask_bit,
|
|
|
|
&hisi_clk_lock);
|
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n",
|
|
|
|
__func__, clks[i].name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clks[i].alias)
|
|
|
|
clk_register_clkdev(clk, clks[i].alias, NULL);
|
|
|
|
|
|
|
|
data->clk_data.clks[clks[i].id] = clk;
|
|
|
|
}
|
|
|
|
}
|