2013-03-09 08:02:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Samsung Electronics Co., Ltd.
|
|
|
|
* Copyright (c) 2013 Linaro Ltd.
|
|
|
|
* Author: Thomas Abraham <thomas.ab@samsung.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This file includes utility functions to register clocks to common
|
|
|
|
* clock framework for Samsung platforms.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/syscore_ops.h>
|
|
|
|
#include "clk.h"
|
|
|
|
|
2014-02-13 23:16:00 +00:00
|
|
|
void samsung_clk_save(void __iomem *base,
|
|
|
|
struct samsung_clk_reg_dump *rd,
|
|
|
|
unsigned int num_regs)
|
|
|
|
{
|
|
|
|
for (; num_regs > 0; --num_regs, ++rd)
|
|
|
|
rd->value = readl(base + rd->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void samsung_clk_restore(void __iomem *base,
|
|
|
|
const struct samsung_clk_reg_dump *rd,
|
|
|
|
unsigned int num_regs)
|
|
|
|
{
|
|
|
|
for (; num_regs > 0; --num_regs, ++rd)
|
|
|
|
writel(rd->value, base + rd->offset);
|
|
|
|
}
|
|
|
|
|
2014-02-13 23:16:00 +00:00
|
|
|
struct samsung_clk_reg_dump *samsung_clk_alloc_reg_dump(
|
|
|
|
const unsigned long *rdump,
|
|
|
|
unsigned long nr_rdump)
|
2014-02-13 23:16:00 +00:00
|
|
|
{
|
|
|
|
struct samsung_clk_reg_dump *rd;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
rd = kcalloc(nr_rdump, sizeof(*rd), GFP_KERNEL);
|
|
|
|
if (!rd)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_rdump; ++i)
|
|
|
|
rd[i].offset = rdump[i];
|
|
|
|
|
|
|
|
return rd;
|
|
|
|
}
|
|
|
|
|
2013-03-09 08:02:44 +00:00
|
|
|
/* setup the essentials required to support clock lookup using ccf */
|
2014-03-12 14:56:44 +00:00
|
|
|
struct samsung_clk_provider *__init samsung_clk_init(struct device_node *np,
|
|
|
|
void __iomem *base, unsigned long nr_clks)
|
2013-03-09 08:02:44 +00:00
|
|
|
{
|
2014-03-12 14:56:44 +00:00
|
|
|
struct samsung_clk_provider *ctx;
|
|
|
|
struct clk **clk_table;
|
|
|
|
int ret;
|
2014-02-06 18:33:11 +00:00
|
|
|
int i;
|
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
ctx = kzalloc(sizeof(struct samsung_clk_provider), GFP_KERNEL);
|
|
|
|
if (!ctx)
|
|
|
|
panic("could not allocate clock provider context.\n");
|
2013-03-09 08:02:44 +00:00
|
|
|
|
2014-02-06 18:33:11 +00:00
|
|
|
clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL);
|
2013-03-18 04:43:52 +00:00
|
|
|
if (!clk_table)
|
|
|
|
panic("could not allocate clock lookup table\n");
|
|
|
|
|
2014-02-06 18:33:11 +00:00
|
|
|
for (i = 0; i < nr_clks; ++i)
|
|
|
|
clk_table[i] = ERR_PTR(-ENOENT);
|
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
ctx->reg_base = base;
|
|
|
|
ctx->clk_data.clks = clk_table;
|
|
|
|
ctx->clk_data.clk_num = nr_clks;
|
|
|
|
spin_lock_init(&ctx->lock);
|
|
|
|
|
2013-03-18 04:43:52 +00:00
|
|
|
if (!np)
|
2014-03-12 14:56:44 +00:00
|
|
|
return ctx;
|
2013-03-18 04:43:52 +00:00
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
ret = of_clk_add_provider(np, of_clk_src_onecell_get,
|
|
|
|
&ctx->clk_data);
|
|
|
|
if (ret)
|
|
|
|
panic("could not register clock provide\n");
|
|
|
|
|
|
|
|
return ctx;
|
2013-03-09 08:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add a clock instance to the clock lookup table used for dt based lookup */
|
2014-03-12 14:56:44 +00:00
|
|
|
void samsung_clk_add_lookup(struct samsung_clk_provider *ctx, struct clk *clk,
|
|
|
|
unsigned int id)
|
2013-03-09 08:02:44 +00:00
|
|
|
{
|
2014-03-12 14:56:44 +00:00
|
|
|
if (ctx->clk_data.clks && id)
|
|
|
|
ctx->clk_data.clks[id] = clk;
|
2013-03-09 08:02:44 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 04:43:56 +00:00
|
|
|
/* register a list of aliases */
|
2014-03-12 14:56:44 +00:00
|
|
|
void __init samsung_clk_register_alias(struct samsung_clk_provider *ctx,
|
|
|
|
struct samsung_clock_alias *list,
|
|
|
|
unsigned int nr_clk)
|
2013-03-18 04:43:56 +00:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
unsigned int idx, ret;
|
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
if (!ctx->clk_data.clks) {
|
2013-03-18 04:43:56 +00:00
|
|
|
pr_err("%s: clock table missing\n", __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (idx = 0; idx < nr_clk; idx++, list++) {
|
|
|
|
if (!list->id) {
|
|
|
|
pr_err("%s: clock id missing for index %d\n", __func__,
|
|
|
|
idx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
clk = ctx->clk_data.clks[list->id];
|
2013-03-18 04:43:56 +00:00
|
|
|
if (!clk) {
|
|
|
|
pr_err("%s: failed to find clock %d\n", __func__,
|
|
|
|
list->id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_register_clkdev(clk, list->alias, list->dev_name);
|
|
|
|
if (ret)
|
|
|
|
pr_err("%s: failed to register lookup %s\n",
|
|
|
|
__func__, list->alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-09 08:02:44 +00:00
|
|
|
/* register a list of fixed clocks */
|
2014-03-12 14:56:44 +00:00
|
|
|
void __init samsung_clk_register_fixed_rate(struct samsung_clk_provider *ctx,
|
2013-03-09 08:02:44 +00:00
|
|
|
struct samsung_fixed_rate_clock *list, unsigned int nr_clk)
|
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
unsigned int idx, ret;
|
|
|
|
|
|
|
|
for (idx = 0; idx < nr_clk; idx++, list++) {
|
|
|
|
clk = clk_register_fixed_rate(NULL, list->name,
|
|
|
|
list->parent_name, list->flags, list->fixed_rate);
|
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n", __func__,
|
|
|
|
list->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
samsung_clk_add_lookup(ctx, clk, list->id);
|
2013-03-09 08:02:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unconditionally add a clock lookup for the fixed rate clocks.
|
|
|
|
* There are not many of these on any of Samsung platforms.
|
|
|
|
*/
|
|
|
|
ret = clk_register_clkdev(clk, list->name, NULL);
|
|
|
|
if (ret)
|
|
|
|
pr_err("%s: failed to register clock lookup for %s",
|
|
|
|
__func__, list->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register a list of fixed factor clocks */
|
2014-03-12 14:56:44 +00:00
|
|
|
void __init samsung_clk_register_fixed_factor(struct samsung_clk_provider *ctx,
|
2013-03-09 08:02:44 +00:00
|
|
|
struct samsung_fixed_factor_clock *list, unsigned int nr_clk)
|
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
unsigned int idx;
|
|
|
|
|
|
|
|
for (idx = 0; idx < nr_clk; idx++, list++) {
|
|
|
|
clk = clk_register_fixed_factor(NULL, list->name,
|
|
|
|
list->parent_name, list->flags, list->mult, list->div);
|
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n", __func__,
|
|
|
|
list->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
samsung_clk_add_lookup(ctx, clk, list->id);
|
2013-03-09 08:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register a list of mux clocks */
|
2014-03-12 14:56:44 +00:00
|
|
|
void __init samsung_clk_register_mux(struct samsung_clk_provider *ctx,
|
|
|
|
struct samsung_mux_clock *list,
|
|
|
|
unsigned int nr_clk)
|
2013-03-09 08:02:44 +00:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
unsigned int idx, ret;
|
|
|
|
|
|
|
|
for (idx = 0; idx < nr_clk; idx++, list++) {
|
|
|
|
clk = clk_register_mux(NULL, list->name, list->parent_names,
|
2014-03-12 14:56:44 +00:00
|
|
|
list->num_parents, list->flags,
|
|
|
|
ctx->reg_base + list->offset,
|
|
|
|
list->shift, list->width, list->mux_flags, &ctx->lock);
|
2013-03-09 08:02:44 +00:00
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n", __func__,
|
|
|
|
list->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
samsung_clk_add_lookup(ctx, clk, list->id);
|
2013-03-09 08:02:44 +00:00
|
|
|
|
|
|
|
/* register a clock lookup only if a clock alias is specified */
|
|
|
|
if (list->alias) {
|
|
|
|
ret = clk_register_clkdev(clk, list->alias,
|
|
|
|
list->dev_name);
|
|
|
|
if (ret)
|
|
|
|
pr_err("%s: failed to register lookup %s\n",
|
|
|
|
__func__, list->alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register a list of div clocks */
|
2014-03-12 14:56:44 +00:00
|
|
|
void __init samsung_clk_register_div(struct samsung_clk_provider *ctx,
|
|
|
|
struct samsung_div_clock *list,
|
|
|
|
unsigned int nr_clk)
|
2013-03-09 08:02:44 +00:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
unsigned int idx, ret;
|
|
|
|
|
|
|
|
for (idx = 0; idx < nr_clk; idx++, list++) {
|
2013-03-18 04:43:52 +00:00
|
|
|
if (list->table)
|
|
|
|
clk = clk_register_divider_table(NULL, list->name,
|
2014-03-12 14:56:44 +00:00
|
|
|
list->parent_name, list->flags,
|
|
|
|
ctx->reg_base + list->offset,
|
|
|
|
list->shift, list->width, list->div_flags,
|
|
|
|
list->table, &ctx->lock);
|
2013-03-18 04:43:52 +00:00
|
|
|
else
|
|
|
|
clk = clk_register_divider(NULL, list->name,
|
2014-03-12 14:56:44 +00:00
|
|
|
list->parent_name, list->flags,
|
|
|
|
ctx->reg_base + list->offset, list->shift,
|
|
|
|
list->width, list->div_flags, &ctx->lock);
|
2013-03-09 08:02:44 +00:00
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n", __func__,
|
|
|
|
list->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
samsung_clk_add_lookup(ctx, clk, list->id);
|
2013-03-09 08:02:44 +00:00
|
|
|
|
|
|
|
/* register a clock lookup only if a clock alias is specified */
|
|
|
|
if (list->alias) {
|
|
|
|
ret = clk_register_clkdev(clk, list->alias,
|
|
|
|
list->dev_name);
|
|
|
|
if (ret)
|
|
|
|
pr_err("%s: failed to register lookup %s\n",
|
|
|
|
__func__, list->alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register a list of gate clocks */
|
2014-03-12 14:56:44 +00:00
|
|
|
void __init samsung_clk_register_gate(struct samsung_clk_provider *ctx,
|
|
|
|
struct samsung_gate_clock *list,
|
|
|
|
unsigned int nr_clk)
|
2013-03-09 08:02:44 +00:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
unsigned int idx, ret;
|
|
|
|
|
|
|
|
for (idx = 0; idx < nr_clk; idx++, list++) {
|
|
|
|
clk = clk_register_gate(NULL, list->name, list->parent_name,
|
2014-03-12 14:56:44 +00:00
|
|
|
list->flags, ctx->reg_base + list->offset,
|
|
|
|
list->bit_idx, list->gate_flags, &ctx->lock);
|
2013-03-09 08:02:44 +00:00
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
pr_err("%s: failed to register clock %s\n", __func__,
|
|
|
|
list->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register a clock lookup only if a clock alias is specified */
|
|
|
|
if (list->alias) {
|
|
|
|
ret = clk_register_clkdev(clk, list->alias,
|
|
|
|
list->dev_name);
|
|
|
|
if (ret)
|
|
|
|
pr_err("%s: failed to register lookup %s\n",
|
|
|
|
__func__, list->alias);
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
samsung_clk_add_lookup(ctx, clk, list->id);
|
2013-03-09 08:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* obtain the clock speed of all external fixed clock sources from device
|
|
|
|
* tree and register it
|
|
|
|
*/
|
2013-04-08 12:35:25 +00:00
|
|
|
#ifdef CONFIG_OF
|
2014-03-12 14:56:44 +00:00
|
|
|
void __init samsung_clk_of_register_fixed_ext(struct samsung_clk_provider *ctx,
|
2013-03-09 08:02:44 +00:00
|
|
|
struct samsung_fixed_rate_clock *fixed_rate_clk,
|
|
|
|
unsigned int nr_fixed_rate_clk,
|
|
|
|
struct of_device_id *clk_matches)
|
|
|
|
{
|
|
|
|
const struct of_device_id *match;
|
2014-03-12 14:56:44 +00:00
|
|
|
struct device_node *clk_np;
|
2013-03-09 08:02:44 +00:00
|
|
|
u32 freq;
|
|
|
|
|
2014-03-12 14:56:44 +00:00
|
|
|
for_each_matching_node_and_match(clk_np, clk_matches, &match) {
|
|
|
|
if (of_property_read_u32(clk_np, "clock-frequency", &freq))
|
2013-03-09 08:02:44 +00:00
|
|
|
continue;
|
2014-02-26 02:42:41 +00:00
|
|
|
fixed_rate_clk[(unsigned long)match->data].fixed_rate = freq;
|
2013-03-09 08:02:44 +00:00
|
|
|
}
|
2014-03-12 14:56:44 +00:00
|
|
|
samsung_clk_register_fixed_rate(ctx, fixed_rate_clk, nr_fixed_rate_clk);
|
2013-03-09 08:02:44 +00:00
|
|
|
}
|
2013-04-08 12:35:25 +00:00
|
|
|
#endif
|
2013-03-09 08:02:44 +00:00
|
|
|
|
|
|
|
/* utility function to get the rate of a specified clock */
|
|
|
|
unsigned long _get_rate(const char *clk_name)
|
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
|
2013-08-26 17:09:00 +00:00
|
|
|
clk = __clk_lookup(clk_name);
|
|
|
|
if (!clk) {
|
2013-03-09 08:02:44 +00:00
|
|
|
pr_err("%s: could not find clock %s\n", __func__, clk_name);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-26 17:09:00 +00:00
|
|
|
|
|
|
|
return clk_get_rate(clk);
|
2013-03-09 08:02:44 +00:00
|
|
|
}
|