Skip to content

Commit

Permalink
clk: Implement clk_unregister
Browse files Browse the repository at this point in the history
clk_unregister() is currently not implemented and it is required when
a clock provider module needs to be unloaded.

Normally the clock supplier module is prevented to be unloaded by
taking reference on the module in clk_get().

For cases when the clock supplier module deinitializes despite the
consumers of its clocks holding a reference on the module, e.g. when
the driver is unbound through "unbind" sysfs attribute, there are
empty clock ops added. These ops are assigned temporarily to struct
clk and used until all consumers release the clock, to avoid invoking
callbacks from the module which just got removed.

Signed-off-by: Jiada Wang <jiada_wang@mentor.com>
Signed-off-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
  • Loading branch information
Sylwester Nawrocki committed Dec 4, 2013
1 parent ac2df52 commit fcb0ee6
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
121 changes: 118 additions & 3 deletions drivers/clk/clk.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,21 @@ static int clk_debug_register(struct clk *clk)
return ret;
}

/**
* clk_debug_unregister - remove a clk node from the debugfs clk tree
* @clk: the clk being removed from the debugfs clk tree
*
* Dynamically removes a clk and all it's children clk nodes from the
* debugfs clk tree if clk->dentry points to debugfs created by
* clk_debug_register in __clk_init.
*
* Caller must hold prepare_lock.
*/
static void clk_debug_unregister(struct clk *clk)
{
debugfs_remove_recursive(clk->dentry);
}

/**
* clk_debug_reparent - reparent clk node in the debugfs clk tree
* @clk: the clk being reparented
Expand Down Expand Up @@ -435,6 +450,9 @@ static inline int clk_debug_register(struct clk *clk) { return 0; }
static inline void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
{
}
static inline void clk_debug_unregister(struct clk *clk)
{
}
#endif

/* caller must hold prepare_lock */
Expand Down Expand Up @@ -1778,6 +1796,7 @@ int __clk_init(struct device *dev, struct clk *clk)

clk_debug_register(clk);

kref_init(&clk->ref);
out:
clk_prepare_unlock();

Expand Down Expand Up @@ -1913,13 +1932,104 @@ struct clk *clk_register(struct device *dev, struct clk_hw *hw)
}
EXPORT_SYMBOL_GPL(clk_register);

/*
* Free memory allocated for a clock.
* Caller must hold prepare_lock.
*/
static void __clk_release(struct kref *ref)
{
struct clk *clk = container_of(ref, struct clk, ref);
int i = clk->num_parents;

kfree(clk->parents);
while (--i >= 0)
kfree(clk->parent_names[i]);

kfree(clk->parent_names);
kfree(clk->name);
kfree(clk);
}

/*
* Empty clk_ops for unregistered clocks. These are used temporarily
* after clk_unregister() was called on a clock and until last clock
* consumer calls clk_put() and the struct clk object is freed.
*/
static int clk_nodrv_prepare_enable(struct clk_hw *hw)
{
return -ENXIO;
}

static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
{
WARN_ON_ONCE(1);
}

static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
unsigned long parent_rate)
{
return -ENXIO;
}

static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
{
return -ENXIO;
}

static const struct clk_ops clk_nodrv_ops = {
.enable = clk_nodrv_prepare_enable,
.disable = clk_nodrv_disable_unprepare,
.prepare = clk_nodrv_prepare_enable,
.unprepare = clk_nodrv_disable_unprepare,
.set_rate = clk_nodrv_set_rate,
.set_parent = clk_nodrv_set_parent,
};

/**
* clk_unregister - unregister a currently registered clock
* @clk: clock to unregister
*
* Currently unimplemented.
*/
void clk_unregister(struct clk *clk) {}
void clk_unregister(struct clk *clk)
{
unsigned long flags;

if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
return;

clk_prepare_lock();

if (clk->ops == &clk_nodrv_ops) {
pr_err("%s: unregistered clock: %s\n", __func__, clk->name);
goto out;
}
/*
* Assign empty clock ops for consumers that might still hold
* a reference to this clock.
*/
flags = clk_enable_lock();
clk->ops = &clk_nodrv_ops;
clk_enable_unlock(flags);

if (!hlist_empty(&clk->children)) {
struct clk *child;

/* Reparent all children to the orphan list. */
hlist_for_each_entry(child, &clk->children, child_node)
clk_set_parent(child, NULL);
}

clk_debug_unregister(clk);

hlist_del_init(&clk->child_node);

if (clk->prepare_count)
pr_warn("%s: unregistering prepared clock: %s\n",
__func__, clk->name);

kref_put(&clk->ref, __clk_release);
out:
clk_prepare_unlock();
}
EXPORT_SYMBOL_GPL(clk_unregister);

static void devm_clk_release(struct device *dev, void *res)
Expand Down Expand Up @@ -1987,6 +2097,7 @@ int __clk_get(struct clk *clk)
if (clk && !try_module_get(clk->owner))
return 0;

kref_get(&clk->ref);
return 1;
}

Expand All @@ -1995,6 +2106,10 @@ void __clk_put(struct clk *clk)
if (WARN_ON_ONCE(IS_ERR(clk)))
return;

clk_prepare_lock();
kref_put(&clk->ref, __clk_release);
clk_prepare_unlock();

if (clk)
module_put(clk->owner);
}
Expand Down
2 changes: 2 additions & 0 deletions include/linux/clk-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define __LINUX_CLK_PRIVATE_H

#include <linux/clk-provider.h>
#include <linux/kref.h>
#include <linux/list.h>

/*
Expand Down Expand Up @@ -50,6 +51,7 @@ struct clk {
#ifdef CONFIG_COMMON_CLK_DEBUG
struct dentry *dentry;
#endif
struct kref ref;
};

/*
Expand Down

0 comments on commit fcb0ee6

Please sign in to comment.