2019-06-24 13:50:45 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 DENX Software Engineering
|
|
|
|
* Lukasz Majewski, DENX Software Engineering, lukma@denx.de
|
|
|
|
*/
|
|
|
|
|
2021-11-19 14:12:07 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_CLK
|
|
|
|
|
2019-06-24 13:50:45 +00:00
|
|
|
#include <common.h>
|
2021-11-19 14:12:06 +00:00
|
|
|
#include <clk.h>
|
2019-06-24 13:50:45 +00:00
|
|
|
#include <clk-uclass.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2019-06-24 13:50:45 +00:00
|
|
|
#include <dm/device.h>
|
|
|
|
#include <dm/uclass.h>
|
|
|
|
#include <dm/lists.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
|
|
|
|
int clk_register(struct clk *clk, const char *drv_name,
|
|
|
|
const char *name, const char *parent_name)
|
|
|
|
{
|
|
|
|
struct udevice *parent;
|
|
|
|
struct driver *drv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_get_device_by_name(UCLASS_CLK, parent_name, &parent);
|
2019-10-22 03:31:08 +00:00
|
|
|
if (ret) {
|
2021-11-19 14:12:07 +00:00
|
|
|
log_err("%s: failed to get %s device (parent of %s)\n",
|
|
|
|
__func__, parent_name, name);
|
2020-05-02 15:38:11 +00:00
|
|
|
} else {
|
2021-11-19 14:12:07 +00:00
|
|
|
log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name,
|
|
|
|
parent->name, parent);
|
2019-10-22 03:31:08 +00:00
|
|
|
}
|
2019-06-24 13:50:45 +00:00
|
|
|
|
|
|
|
drv = lists_driver_lookup_name(drv_name);
|
|
|
|
if (!drv) {
|
2021-11-19 14:12:07 +00:00
|
|
|
log_err("%s: %s is not a valid driver name\n",
|
|
|
|
__func__, drv_name);
|
2019-06-24 13:50:45 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2020-11-29 00:50:03 +00:00
|
|
|
ret = device_bind(parent, drv, name, NULL, ofnode_null(), &clk->dev);
|
2019-06-24 13:50:45 +00:00
|
|
|
if (ret) {
|
2021-11-19 14:12:07 +00:00
|
|
|
log_err("%s: CLK: %s driver bind error [%d]!\n", __func__, name,
|
|
|
|
ret);
|
2019-06-24 13:50:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-08-21 13:35:03 +00:00
|
|
|
clk->enable_count = 0;
|
2020-12-23 02:30:28 +00:00
|
|
|
|
2019-06-24 13:50:45 +00:00
|
|
|
/* Store back pointer to clk from udevice */
|
2020-12-23 02:30:28 +00:00
|
|
|
/* FIXME: This is not allowed...should be allocated by driver model */
|
|
|
|
dev_set_uclass_priv(clk->dev, clk);
|
2019-06-24 13:50:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong clk_generic_get_rate(struct clk *clk)
|
|
|
|
{
|
|
|
|
return clk_get_parent_rate(clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *clk_hw_get_name(const struct clk *hw)
|
|
|
|
{
|
2020-09-07 14:46:32 +00:00
|
|
|
assert(hw);
|
|
|
|
assert(hw->dev);
|
|
|
|
|
2019-06-24 13:50:45 +00:00
|
|
|
return hw->dev->name;
|
|
|
|
}
|
2019-07-31 07:01:23 +00:00
|
|
|
|
|
|
|
bool clk_dev_binded(struct clk *clk)
|
|
|
|
{
|
2020-12-19 17:40:10 +00:00
|
|
|
if (clk->dev && (dev_get_flags(clk->dev) & DM_FLAG_BOUND))
|
2019-07-31 07:01:23 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-20 20:34:45 +00:00
|
|
|
|
|
|
|
/* Helper functions for clock ops */
|
|
|
|
|
|
|
|
ulong ccf_clk_get_rate(struct clk *clk)
|
|
|
|
{
|
|
|
|
struct clk *c;
|
|
|
|
int err = clk_get_by_id(clk->id, &c);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
return clk_get_rate(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong ccf_clk_set_rate(struct clk *clk, unsigned long rate)
|
|
|
|
{
|
|
|
|
struct clk *c;
|
|
|
|
int err = clk_get_by_id(clk->id, &c);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
return clk_set_rate(c, rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ccf_clk_set_parent(struct clk *clk, struct clk *parent)
|
|
|
|
{
|
|
|
|
struct clk *c, *p;
|
|
|
|
int err = clk_get_by_id(clk->id, &c);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = clk_get_by_id(parent->id, &p);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return clk_set_parent(c, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ccf_clk_endisable(struct clk *clk, bool enable)
|
|
|
|
{
|
|
|
|
struct clk *c;
|
|
|
|
int err = clk_get_by_id(clk->id, &c);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
return enable ? clk_enable(c) : clk_disable(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ccf_clk_enable(struct clk *clk)
|
|
|
|
{
|
|
|
|
return ccf_clk_endisable(clk, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ccf_clk_disable(struct clk *clk)
|
|
|
|
{
|
|
|
|
return ccf_clk_endisable(clk, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct clk_ops ccf_clk_ops = {
|
|
|
|
.set_rate = ccf_clk_set_rate,
|
|
|
|
.get_rate = ccf_clk_get_rate,
|
|
|
|
.set_parent = ccf_clk_set_parent,
|
|
|
|
.enable = ccf_clk_enable,
|
|
|
|
.disable = ccf_clk_disable,
|
|
|
|
};
|