2018-08-22 01:02:14 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2013-10-18 01:54:07 +04:00
|
|
|
/*
|
|
|
|
* r8a7790 Common Clock Framework support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Renesas Solutions Corp.
|
|
|
|
*
|
|
|
|
* Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk-provider.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/kernel.h>
|
2017-06-21 23:34:33 +03:00
|
|
|
#include <linux/notifier.h>
|
2013-10-18 01:54:07 +04:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_address.h>
|
2017-06-21 23:34:33 +03:00
|
|
|
#include <linux/pm.h>
|
2015-06-23 16:09:27 +03:00
|
|
|
#include <linux/slab.h>
|
2013-10-18 01:54:07 +04:00
|
|
|
|
2015-10-16 12:41:19 +03:00
|
|
|
#include "clk-div6.h"
|
|
|
|
|
2013-10-18 01:54:07 +04:00
|
|
|
#define CPG_DIV6_CKSTP BIT(8)
|
|
|
|
#define CPG_DIV6_DIV(d) ((d) & 0x3f)
|
|
|
|
#define CPG_DIV6_DIV_MASK 0x3f
|
|
|
|
|
|
|
|
/**
|
2014-02-24 23:57:11 +04:00
|
|
|
* struct div6_clock - CPG 6 bit divider clock
|
2013-10-18 01:54:07 +04:00
|
|
|
* @hw: handle between common and hardware-specific interfaces
|
|
|
|
* @reg: IO-remapped register
|
|
|
|
* @div: divisor value (1-64)
|
2021-04-01 16:01:35 +03:00
|
|
|
* @src_mask: Bitmask covering the register bits to select the parent clock
|
2017-06-21 23:34:33 +03:00
|
|
|
* @nb: Notifier block to save/restore clock state for system resume
|
2019-06-12 18:22:18 +03:00
|
|
|
* @parents: Array to map from valid parent clocks indices to hardware indices
|
2013-10-18 01:54:07 +04:00
|
|
|
*/
|
|
|
|
struct div6_clock {
|
|
|
|
struct clk_hw hw;
|
|
|
|
void __iomem *reg;
|
|
|
|
unsigned int div;
|
2021-04-01 16:01:35 +03:00
|
|
|
u32 src_mask;
|
2017-06-21 23:34:33 +03:00
|
|
|
struct notifier_block nb;
|
2019-06-12 18:22:18 +03:00
|
|
|
u8 parents[];
|
2013-10-18 01:54:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define to_div6_clock(_hw) container_of(_hw, struct div6_clock, hw)
|
|
|
|
|
|
|
|
static int cpg_div6_clock_enable(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct div6_clock *clock = to_div6_clock(hw);
|
2014-11-07 18:51:07 +03:00
|
|
|
u32 val;
|
2013-10-18 01:54:07 +04:00
|
|
|
|
2018-03-15 12:43:12 +03:00
|
|
|
val = (readl(clock->reg) & ~(CPG_DIV6_DIV_MASK | CPG_DIV6_CKSTP))
|
2014-11-07 18:51:07 +03:00
|
|
|
| CPG_DIV6_DIV(clock->div - 1);
|
2018-03-15 12:43:12 +03:00
|
|
|
writel(val, clock->reg);
|
2013-10-18 01:54:07 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cpg_div6_clock_disable(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct div6_clock *clock = to_div6_clock(hw);
|
2014-11-24 17:57:59 +03:00
|
|
|
u32 val;
|
2013-10-18 01:54:07 +04:00
|
|
|
|
2018-03-15 12:43:12 +03:00
|
|
|
val = readl(clock->reg);
|
2014-11-24 17:57:59 +03:00
|
|
|
val |= CPG_DIV6_CKSTP;
|
|
|
|
/*
|
|
|
|
* DIV6 clocks require the divisor field to be non-zero when stopping
|
|
|
|
* the clock. However, some clocks (e.g. ZB on sh73a0) fail to be
|
|
|
|
* re-enabled later if the divisor field is changed when stopping the
|
|
|
|
* clock
|
2013-10-18 01:54:07 +04:00
|
|
|
*/
|
2014-11-24 17:57:59 +03:00
|
|
|
if (!(val & CPG_DIV6_DIV_MASK))
|
|
|
|
val |= CPG_DIV6_DIV_MASK;
|
2018-03-15 12:43:12 +03:00
|
|
|
writel(val, clock->reg);
|
2013-10-18 01:54:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cpg_div6_clock_is_enabled(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct div6_clock *clock = to_div6_clock(hw);
|
|
|
|
|
2018-03-15 12:43:12 +03:00
|
|
|
return !(readl(clock->reg) & CPG_DIV6_CKSTP);
|
2013-10-18 01:54:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long cpg_div6_clock_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct div6_clock *clock = to_div6_clock(hw);
|
|
|
|
|
2016-02-18 17:16:02 +03:00
|
|
|
return parent_rate / clock->div;
|
2013-10-18 01:54:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int cpg_div6_clock_calc_div(unsigned long rate,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
unsigned int div;
|
|
|
|
|
2015-02-04 15:27:21 +03:00
|
|
|
if (!rate)
|
|
|
|
rate = 1;
|
|
|
|
|
2013-10-18 01:54:07 +04:00
|
|
|
div = DIV_ROUND_CLOSEST(parent_rate, rate);
|
2021-04-01 16:01:34 +03:00
|
|
|
return clamp(div, 1U, 64U);
|
2013-10-18 01:54:07 +04:00
|
|
|
}
|
|
|
|
|
2021-04-01 16:01:36 +03:00
|
|
|
static int cpg_div6_clock_determine_rate(struct clk_hw *hw,
|
|
|
|
struct clk_rate_request *req)
|
2013-10-18 01:54:07 +04:00
|
|
|
{
|
2021-04-01 16:01:37 +03:00
|
|
|
unsigned long prate, calc_rate, diff, best_rate, best_prate;
|
|
|
|
unsigned int num_parents = clk_hw_get_num_parents(hw);
|
|
|
|
struct clk_hw *parent, *best_parent = NULL;
|
2021-04-01 16:01:38 +03:00
|
|
|
unsigned int i, min_div, max_div, div;
|
2021-04-01 16:01:37 +03:00
|
|
|
unsigned long min_diff = ULONG_MAX;
|
|
|
|
|
|
|
|
for (i = 0; i < num_parents; i++) {
|
|
|
|
parent = clk_hw_get_parent_by_index(hw, i);
|
|
|
|
if (!parent)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
prate = clk_hw_get_rate(parent);
|
|
|
|
if (!prate)
|
|
|
|
continue;
|
|
|
|
|
2021-04-01 16:01:38 +03:00
|
|
|
min_div = max(DIV_ROUND_UP(prate, req->max_rate), 1UL);
|
|
|
|
max_div = req->min_rate ? min(prate / req->min_rate, 64UL) : 64;
|
|
|
|
if (max_div < min_div)
|
|
|
|
continue;
|
|
|
|
|
2021-04-01 16:01:37 +03:00
|
|
|
div = cpg_div6_clock_calc_div(req->rate, prate);
|
2021-04-01 16:01:38 +03:00
|
|
|
div = clamp(div, min_div, max_div);
|
2021-04-01 16:01:37 +03:00
|
|
|
calc_rate = prate / div;
|
|
|
|
diff = calc_rate > req->rate ? calc_rate - req->rate
|
|
|
|
: req->rate - calc_rate;
|
|
|
|
if (diff < min_diff) {
|
|
|
|
best_rate = calc_rate;
|
|
|
|
best_parent = parent;
|
|
|
|
best_prate = prate;
|
|
|
|
min_diff = diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!best_parent)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
req->best_parent_rate = best_prate;
|
|
|
|
req->best_parent_hw = best_parent;
|
|
|
|
req->rate = best_rate;
|
2021-04-01 16:01:36 +03:00
|
|
|
return 0;
|
2013-10-18 01:54:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cpg_div6_clock_set_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct div6_clock *clock = to_div6_clock(hw);
|
|
|
|
unsigned int div = cpg_div6_clock_calc_div(rate, parent_rate);
|
2014-11-07 18:51:07 +03:00
|
|
|
u32 val;
|
2013-10-18 01:54:07 +04:00
|
|
|
|
|
|
|
clock->div = div;
|
|
|
|
|
2018-03-15 12:43:12 +03:00
|
|
|
val = readl(clock->reg) & ~CPG_DIV6_DIV_MASK;
|
2013-10-18 01:54:07 +04:00
|
|
|
/* Only program the new divisor if the clock isn't stopped. */
|
2014-11-07 18:51:07 +03:00
|
|
|
if (!(val & CPG_DIV6_CKSTP))
|
2018-03-15 12:43:12 +03:00
|
|
|
writel(val | CPG_DIV6_DIV(clock->div - 1), clock->reg);
|
2014-11-07 18:51:07 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u8 cpg_div6_clock_get_parent(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct div6_clock *clock = to_div6_clock(hw);
|
|
|
|
unsigned int i;
|
|
|
|
u8 hw_index;
|
|
|
|
|
2021-04-01 16:01:35 +03:00
|
|
|
if (clock->src_mask == 0)
|
2014-11-07 18:51:07 +03:00
|
|
|
return 0;
|
|
|
|
|
2021-04-01 16:01:35 +03:00
|
|
|
hw_index = (readl(clock->reg) & clock->src_mask) >>
|
|
|
|
__ffs(clock->src_mask);
|
2015-06-26 02:53:23 +03:00
|
|
|
for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
|
2014-11-07 18:51:07 +03:00
|
|
|
if (clock->parents[i] == hw_index)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_err("%s: %s DIV6 clock set to invalid parent %u\n",
|
2015-08-12 21:42:23 +03:00
|
|
|
__func__, clk_hw_get_name(hw), hw_index);
|
2014-11-07 18:51:07 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cpg_div6_clock_set_parent(struct clk_hw *hw, u8 index)
|
|
|
|
{
|
|
|
|
struct div6_clock *clock = to_div6_clock(hw);
|
2021-04-01 16:01:35 +03:00
|
|
|
u32 src;
|
2014-11-07 18:51:07 +03:00
|
|
|
|
2015-06-26 02:53:23 +03:00
|
|
|
if (index >= clk_hw_get_num_parents(hw))
|
2014-11-07 18:51:07 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2021-04-01 16:01:35 +03:00
|
|
|
src = clock->parents[index] << __ffs(clock->src_mask);
|
|
|
|
writel((readl(clock->reg) & ~clock->src_mask) | src, clock->reg);
|
2013-10-18 01:54:07 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops cpg_div6_clock_ops = {
|
|
|
|
.enable = cpg_div6_clock_enable,
|
|
|
|
.disable = cpg_div6_clock_disable,
|
|
|
|
.is_enabled = cpg_div6_clock_is_enabled,
|
2014-11-07 18:51:07 +03:00
|
|
|
.get_parent = cpg_div6_clock_get_parent,
|
|
|
|
.set_parent = cpg_div6_clock_set_parent,
|
2013-10-18 01:54:07 +04:00
|
|
|
.recalc_rate = cpg_div6_clock_recalc_rate,
|
2021-04-01 16:01:36 +03:00
|
|
|
.determine_rate = cpg_div6_clock_determine_rate,
|
2013-10-18 01:54:07 +04:00
|
|
|
.set_rate = cpg_div6_clock_set_rate,
|
|
|
|
};
|
|
|
|
|
2017-06-21 23:34:33 +03:00
|
|
|
static int cpg_div6_clock_notifier_call(struct notifier_block *nb,
|
|
|
|
unsigned long action, void *data)
|
|
|
|
{
|
|
|
|
struct div6_clock *clock = container_of(nb, struct div6_clock, nb);
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case PM_EVENT_RESUME:
|
|
|
|
/*
|
|
|
|
* TODO: This does not yet support DIV6 clocks with multiple
|
|
|
|
* parents, as the parent selection bits are not restored.
|
|
|
|
* Fortunately so far such DIV6 clocks are found only on
|
|
|
|
* R/SH-Mobile SoCs, while the resume functionality is only
|
|
|
|
* needed on R-Car Gen3.
|
|
|
|
*/
|
|
|
|
if (__clk_get_enable_count(clock->hw.clk))
|
|
|
|
cpg_div6_clock_enable(&clock->hw);
|
|
|
|
else
|
|
|
|
cpg_div6_clock_disable(&clock->hw);
|
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
2015-10-16 12:41:19 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* cpg_div6_register - Register a DIV6 clock
|
|
|
|
* @name: Name of the DIV6 clock
|
|
|
|
* @num_parents: Number of parent clocks of the DIV6 clock (1, 4, or 8)
|
|
|
|
* @parent_names: Array containing the names of the parent clocks
|
|
|
|
* @reg: Mapped register used to control the DIV6 clock
|
2017-06-21 23:34:33 +03:00
|
|
|
* @notifiers: Optional notifier chain to save/restore state for system resume
|
2015-10-16 12:41:19 +03:00
|
|
|
*/
|
|
|
|
struct clk * __init cpg_div6_register(const char *name,
|
|
|
|
unsigned int num_parents,
|
|
|
|
const char **parent_names,
|
2017-06-21 23:34:33 +03:00
|
|
|
void __iomem *reg,
|
|
|
|
struct raw_notifier_head *notifiers)
|
2013-10-18 01:54:07 +04:00
|
|
|
{
|
2015-10-16 12:41:19 +03:00
|
|
|
unsigned int valid_parents;
|
2021-03-26 13:54:34 +03:00
|
|
|
struct clk_init_data init = {};
|
2013-10-18 01:54:07 +04:00
|
|
|
struct div6_clock *clock;
|
|
|
|
struct clk *clk;
|
2014-11-07 18:51:07 +03:00
|
|
|
unsigned int i;
|
2013-10-18 01:54:07 +04:00
|
|
|
|
2019-06-12 18:22:18 +03:00
|
|
|
clock = kzalloc(struct_size(clock, parents, num_parents), GFP_KERNEL);
|
2014-11-07 18:51:07 +03:00
|
|
|
if (!clock)
|
2015-10-16 12:41:19 +03:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2014-11-07 18:51:07 +03:00
|
|
|
|
2015-10-16 12:41:19 +03:00
|
|
|
clock->reg = reg;
|
2014-11-07 18:51:07 +03:00
|
|
|
|
2015-10-16 12:41:19 +03:00
|
|
|
/*
|
|
|
|
* Read the divisor. Disabling the clock overwrites the divisor, so we
|
|
|
|
* need to cache its value for the enable operation.
|
2013-10-18 01:54:07 +04:00
|
|
|
*/
|
2018-03-15 12:43:12 +03:00
|
|
|
clock->div = (readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
|
2013-10-18 01:54:07 +04:00
|
|
|
|
2014-11-07 18:51:07 +03:00
|
|
|
switch (num_parents) {
|
|
|
|
case 1:
|
|
|
|
/* fixed parent clock */
|
2021-04-01 16:01:35 +03:00
|
|
|
clock->src_mask = 0;
|
2014-11-07 18:51:07 +03:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
/* clock with EXSRC bits 6-7 */
|
2021-04-01 16:01:35 +03:00
|
|
|
clock->src_mask = GENMASK(7, 6);
|
2014-11-07 18:51:07 +03:00
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
/* VCLK with EXSRC bits 12-14 */
|
2021-04-01 16:01:35 +03:00
|
|
|
clock->src_mask = GENMASK(14, 12);
|
2014-11-07 18:51:07 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pr_err("%s: invalid number of parents for DIV6 clock %s\n",
|
2015-10-16 12:41:19 +03:00
|
|
|
__func__, name);
|
|
|
|
clk = ERR_PTR(-EINVAL);
|
2019-06-12 18:22:18 +03:00
|
|
|
goto free_clock;
|
2015-10-16 12:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Filter out invalid parents */
|
|
|
|
for (i = 0, valid_parents = 0; i < num_parents; i++) {
|
|
|
|
if (parent_names[i]) {
|
|
|
|
parent_names[valid_parents] = parent_names[i];
|
|
|
|
clock->parents[valid_parents] = i;
|
|
|
|
valid_parents++;
|
|
|
|
}
|
2013-10-18 01:54:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Register the clock. */
|
2015-10-16 12:41:19 +03:00
|
|
|
init.name = name;
|
2013-10-18 01:54:07 +04:00
|
|
|
init.ops = &cpg_div6_clock_ops;
|
2014-11-07 18:51:07 +03:00
|
|
|
init.parent_names = parent_names;
|
|
|
|
init.num_parents = valid_parents;
|
2013-10-18 01:54:07 +04:00
|
|
|
|
|
|
|
clock->hw.init = &init;
|
|
|
|
|
|
|
|
clk = clk_register(NULL, &clock->hw);
|
2015-10-16 12:41:19 +03:00
|
|
|
if (IS_ERR(clk))
|
2019-06-12 18:22:18 +03:00
|
|
|
goto free_clock;
|
2015-10-16 12:41:19 +03:00
|
|
|
|
2017-06-21 23:34:33 +03:00
|
|
|
if (notifiers) {
|
|
|
|
clock->nb.notifier_call = cpg_div6_clock_notifier_call;
|
|
|
|
raw_notifier_chain_register(notifiers, &clock->nb);
|
|
|
|
}
|
|
|
|
|
2015-10-16 12:41:19 +03:00
|
|
|
return clk;
|
|
|
|
|
|
|
|
free_clock:
|
|
|
|
kfree(clock);
|
|
|
|
return clk;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init cpg_div6_clock_init(struct device_node *np)
|
|
|
|
{
|
|
|
|
unsigned int num_parents;
|
|
|
|
const char **parent_names;
|
|
|
|
const char *clk_name = np->name;
|
|
|
|
void __iomem *reg;
|
|
|
|
struct clk *clk;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
num_parents = of_clk_get_parent_count(np);
|
|
|
|
if (num_parents < 1) {
|
2018-08-28 18:44:29 +03:00
|
|
|
pr_err("%s: no parent found for %pOFn DIV6 clock\n",
|
|
|
|
__func__, np);
|
2015-10-16 12:41:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent_names = kmalloc_array(num_parents, sizeof(*parent_names),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!parent_names)
|
|
|
|
return;
|
|
|
|
|
|
|
|
reg = of_iomap(np, 0);
|
|
|
|
if (reg == NULL) {
|
2018-08-28 18:44:29 +03:00
|
|
|
pr_err("%s: failed to map %pOFn DIV6 clock register\n",
|
|
|
|
__func__, np);
|
2015-10-16 12:41:19 +03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the DT properties. */
|
|
|
|
of_property_read_string(np, "clock-output-names", &clk_name);
|
|
|
|
|
|
|
|
for (i = 0; i < num_parents; i++)
|
|
|
|
parent_names[i] = of_clk_get_parent_name(np, i);
|
|
|
|
|
2017-06-21 23:34:33 +03:00
|
|
|
clk = cpg_div6_register(clk_name, num_parents, parent_names, reg, NULL);
|
2013-10-18 01:54:07 +04:00
|
|
|
if (IS_ERR(clk)) {
|
2018-08-28 18:44:29 +03:00
|
|
|
pr_err("%s: failed to register %pOFn DIV6 clock (%ld)\n",
|
|
|
|
__func__, np, PTR_ERR(clk));
|
2013-10-18 01:54:07 +04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
of_clk_add_provider(np, of_clk_src_simple_get, clk);
|
|
|
|
|
2014-11-07 18:51:07 +03:00
|
|
|
kfree(parent_names);
|
2013-10-18 01:54:07 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
2015-10-16 12:41:19 +03:00
|
|
|
if (reg)
|
|
|
|
iounmap(reg);
|
2014-11-07 18:51:07 +03:00
|
|
|
kfree(parent_names);
|
2013-10-18 01:54:07 +04:00
|
|
|
}
|
|
|
|
CLK_OF_DECLARE(cpg_div6_clk, "renesas,cpg-div6-clock", cpg_div6_clock_init);
|