2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-02-27 23:24:14 +08:00
|
|
|
/*
|
|
|
|
* clkgen-mux.c: ST GEN-MUX Clock driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 STMicroelectronics (R&D) Limited
|
|
|
|
*
|
|
|
|
* Authors: Stephen Gallimore <stephen.gallimore@st.com>
|
|
|
|
* Pankaj Dev <pankaj.dev@st.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
2019-04-19 06:20:22 +08:00
|
|
|
#include <linux/io.h>
|
2014-02-27 23:24:14 +08:00
|
|
|
#include <linux/of_address.h>
|
2015-06-20 06:00:46 +08:00
|
|
|
#include <linux/clk.h>
|
2014-02-27 23:24:14 +08:00
|
|
|
#include <linux/clk-provider.h>
|
2015-10-07 17:08:57 +08:00
|
|
|
#include "clkgen.h"
|
2014-02-27 23:24:14 +08:00
|
|
|
|
|
|
|
static const char ** __init clkgen_mux_get_parents(struct device_node *np,
|
|
|
|
int *num_parents)
|
|
|
|
{
|
|
|
|
const char **parents;
|
2016-02-20 09:43:30 +08:00
|
|
|
unsigned int nparents;
|
2014-02-27 23:24:14 +08:00
|
|
|
|
2015-05-29 17:25:46 +08:00
|
|
|
nparents = of_clk_get_parent_count(np);
|
2016-02-20 09:43:30 +08:00
|
|
|
if (WARN_ON(!nparents))
|
2014-02-27 23:24:14 +08:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2015-07-08 09:30:05 +08:00
|
|
|
parents = kcalloc(nparents, sizeof(const char *), GFP_KERNEL);
|
2014-02-27 23:24:14 +08:00
|
|
|
if (!parents)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2015-07-07 11:59:04 +08:00
|
|
|
*num_parents = of_clk_parent_fill(np, parents, nparents);
|
2014-02-27 23:24:14 +08:00
|
|
|
return parents;
|
|
|
|
}
|
|
|
|
|
2014-02-27 23:24:16 +08:00
|
|
|
struct clkgen_mux_data {
|
|
|
|
u32 offset;
|
|
|
|
u8 shift;
|
|
|
|
u8 width;
|
|
|
|
spinlock_t *lock;
|
|
|
|
unsigned long clk_flags;
|
|
|
|
u8 mux_flags;
|
|
|
|
};
|
|
|
|
|
2014-07-15 23:20:23 +08:00
|
|
|
static struct clkgen_mux_data stih407_a9_mux_data = {
|
|
|
|
.offset = 0x1a4,
|
2015-06-23 22:09:25 +08:00
|
|
|
.shift = 0,
|
2014-07-15 23:20:23 +08:00
|
|
|
.width = 2,
|
2015-10-07 17:08:57 +08:00
|
|
|
.lock = &clkgen_a9_lock,
|
2014-07-15 23:20:23 +08:00
|
|
|
};
|
2014-02-27 23:24:19 +08:00
|
|
|
|
2016-08-29 20:26:54 +08:00
|
|
|
static void __init st_of_clkgen_mux_setup(struct device_node *np,
|
|
|
|
struct clkgen_mux_data *data)
|
2014-02-27 23:24:16 +08:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
void __iomem *reg;
|
|
|
|
const char **parents;
|
2016-08-29 20:26:53 +08:00
|
|
|
int num_parents = 0;
|
2022-06-28 22:24:15 +08:00
|
|
|
struct device_node *parent_np;
|
2014-02-27 23:24:16 +08:00
|
|
|
|
2021-12-19 05:11:57 +08:00
|
|
|
/*
|
|
|
|
* First check for reg property within the node to keep backward
|
|
|
|
* compatibility, then if reg doesn't exist look at the parent node
|
|
|
|
*/
|
2014-02-27 23:24:16 +08:00
|
|
|
reg = of_iomap(np, 0);
|
|
|
|
if (!reg) {
|
2022-06-28 22:24:15 +08:00
|
|
|
parent_np = of_get_parent(np);
|
|
|
|
reg = of_iomap(parent_np, 0);
|
|
|
|
of_node_put(parent_np);
|
2021-12-19 05:11:57 +08:00
|
|
|
if (!reg) {
|
|
|
|
pr_err("%s: Failed to get base address\n", __func__);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-27 23:24:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
parents = clkgen_mux_get_parents(np, &num_parents);
|
|
|
|
if (IS_ERR(parents)) {
|
|
|
|
pr_err("%s: Failed to get parents (%ld)\n",
|
|
|
|
__func__, PTR_ERR(parents));
|
2015-07-08 09:30:05 +08:00
|
|
|
goto err_parents;
|
2014-02-27 23:24:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
clk = clk_register_mux(NULL, np->name, parents, num_parents,
|
|
|
|
data->clk_flags | CLK_SET_RATE_PARENT,
|
|
|
|
reg + data->offset,
|
|
|
|
data->shift, data->width, data->mux_flags,
|
|
|
|
data->lock);
|
|
|
|
if (IS_ERR(clk))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
pr_debug("%s: parent %s rate %u\n",
|
|
|
|
__clk_get_name(clk),
|
|
|
|
__clk_get_name(clk_get_parent(clk)),
|
|
|
|
(unsigned int)clk_get_rate(clk));
|
|
|
|
|
2015-07-08 09:30:05 +08:00
|
|
|
kfree(parents);
|
2014-02-27 23:24:16 +08:00
|
|
|
of_clk_add_provider(np, of_clk_src_simple_get, clk);
|
2015-07-08 09:30:05 +08:00
|
|
|
return;
|
2014-02-27 23:24:16 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
kfree(parents);
|
2015-07-08 09:30:05 +08:00
|
|
|
err_parents:
|
|
|
|
iounmap(reg);
|
2014-02-27 23:24:16 +08:00
|
|
|
}
|
2016-08-29 20:26:54 +08:00
|
|
|
|
|
|
|
static void __init st_of_clkgen_a9_mux_setup(struct device_node *np)
|
|
|
|
{
|
|
|
|
st_of_clkgen_mux_setup(np, &stih407_a9_mux_data);
|
|
|
|
}
|
|
|
|
CLK_OF_DECLARE(clkgen_a9mux, "st,stih407-clkgen-a9-mux",
|
|
|
|
st_of_clkgen_a9_mux_setup);
|