clk: core: copy parent_names & return error codes

This patch cleans up clk_register and solves a few bugs by teaching
clk_register and __clk_init to return error codes (instead of just NULL)
to better align with the existing clk.h api.

Along with that change this patch also introduces a new behavior whereby
clk_register copies the parent_names array, thus allowing platforms to
declare their parent_names arrays as __initdata.

Signed-off-by: Mike Turquette <mturquette@linaro.org>
This commit is contained in:
Mike Turquette 2012-03-29 14:30:40 -07:00
parent d305fb78f3
commit d1302a36a7
3 changed files with 58 additions and 14 deletions

View file

@ -1185,34 +1185,41 @@ EXPORT_SYMBOL_GPL(clk_set_parent);
* very large numbers of clocks that need to be statically initialized. It is * very large numbers of clocks that need to be statically initialized. It is
* a layering violation to include clk-private.h from any code which implements * a layering violation to include clk-private.h from any code which implements
* a clock's .ops; as such any statically initialized clock data MUST be in a * a clock's .ops; as such any statically initialized clock data MUST be in a
* separate C file from the logic that implements it's operations. * separate C file from the logic that implements it's operations. Returns 0
* on success, otherwise an error code.
*/ */
void __clk_init(struct device *dev, struct clk *clk) int __clk_init(struct device *dev, struct clk *clk)
{ {
int i; int i, ret = 0;
struct clk *orphan; struct clk *orphan;
struct hlist_node *tmp, *tmp2; struct hlist_node *tmp, *tmp2;
if (!clk) if (!clk)
return; return -EINVAL;
mutex_lock(&prepare_lock); mutex_lock(&prepare_lock);
/* check to see if a clock with this name is already registered */ /* check to see if a clock with this name is already registered */
if (__clk_lookup(clk->name)) if (__clk_lookup(clk->name)) {
pr_debug("%s: clk %s already initialized\n",
__func__, clk->name);
ret = -EEXIST;
goto out; goto out;
}
/* check that clk_ops are sane. See Documentation/clk.txt */ /* check that clk_ops are sane. See Documentation/clk.txt */
if (clk->ops->set_rate && if (clk->ops->set_rate &&
!(clk->ops->round_rate && clk->ops->recalc_rate)) { !(clk->ops->round_rate && clk->ops->recalc_rate)) {
pr_warning("%s: %s must implement .round_rate & .recalc_rate\n", pr_warning("%s: %s must implement .round_rate & .recalc_rate\n",
__func__, clk->name); __func__, clk->name);
ret = -EINVAL;
goto out; goto out;
} }
if (clk->ops->set_parent && !clk->ops->get_parent) { if (clk->ops->set_parent && !clk->ops->get_parent) {
pr_warning("%s: %s must implement .get_parent & .set_parent\n", pr_warning("%s: %s must implement .get_parent & .set_parent\n",
__func__, clk->name); __func__, clk->name);
ret = -EINVAL;
goto out; goto out;
} }
@ -1308,7 +1315,7 @@ void __clk_init(struct device *dev, struct clk *clk)
out: out:
mutex_unlock(&prepare_lock); mutex_unlock(&prepare_lock);
return; return ret;
} }
/** /**
@ -1324,29 +1331,63 @@ void __clk_init(struct device *dev, struct clk *clk)
* clk_register is the primary interface for populating the clock tree with new * clk_register is the primary interface for populating the clock tree with new
* clock nodes. It returns a pointer to the newly allocated struct clk which * clock nodes. It returns a pointer to the newly allocated struct clk which
* cannot be dereferenced by driver code but may be used in conjuction with the * cannot be dereferenced by driver code but may be used in conjuction with the
* rest of the clock API. * rest of the clock API. In the event of an error clk_register will return an
* error code; drivers must test for an error code after calling clk_register.
*/ */
struct clk *clk_register(struct device *dev, const char *name, struct clk *clk_register(struct device *dev, const char *name,
const struct clk_ops *ops, struct clk_hw *hw, const struct clk_ops *ops, struct clk_hw *hw,
const char **parent_names, u8 num_parents, unsigned long flags) const char **parent_names, u8 num_parents, unsigned long flags)
{ {
int i, ret;
struct clk *clk; struct clk *clk;
clk = kzalloc(sizeof(*clk), GFP_KERNEL); clk = kzalloc(sizeof(*clk), GFP_KERNEL);
if (!clk) if (!clk) {
return NULL; pr_err("%s: could not allocate clk\n", __func__);
ret = -ENOMEM;
goto fail_out;
}
clk->name = name; clk->name = name;
clk->ops = ops; clk->ops = ops;
clk->hw = hw; clk->hw = hw;
clk->flags = flags; clk->flags = flags;
clk->parent_names = parent_names;
clk->num_parents = num_parents; clk->num_parents = num_parents;
hw->clk = clk; hw->clk = clk;
__clk_init(dev, clk); /* allocate local copy in case parent_names is __initdata */
clk->parent_names = kzalloc((sizeof(char*) * num_parents),
GFP_KERNEL);
return clk; if (!clk->parent_names) {
pr_err("%s: could not allocate clk->parent_names\n", __func__);
ret = -ENOMEM;
goto fail_parent_names;
}
/* copy each string name in case parent_names is __initdata */
for (i = 0; i < num_parents; i++) {
clk->parent_names[i] = kstrdup(parent_names[i], GFP_KERNEL);
if (!clk->parent_names[i]) {
pr_err("%s: could not copy parent_names\n", __func__);
ret = -ENOMEM;
goto fail_parent_names_copy;
}
}
ret = __clk_init(dev, clk);
if (!ret)
return clk;
fail_parent_names_copy:
while (--i >= 0)
kfree(clk->parent_names[i]);
kfree(clk->parent_names);
fail_parent_names:
kfree(clk);
fail_out:
return ERR_PTR(ret);
} }
EXPORT_SYMBOL_GPL(clk_register); EXPORT_SYMBOL_GPL(clk_register);

View file

@ -181,8 +181,10 @@ struct clk {
* *
* It is not necessary to call clk_register if __clk_init is used directly with * It is not necessary to call clk_register if __clk_init is used directly with
* statically initialized clock data. * statically initialized clock data.
*
* Returns 0 on success, otherwise an error code.
*/ */
void __clk_init(struct device *dev, struct clk *clk); int __clk_init(struct device *dev, struct clk *clk);
#endif /* CONFIG_COMMON_CLK */ #endif /* CONFIG_COMMON_CLK */
#endif /* CLK_PRIVATE_H */ #endif /* CLK_PRIVATE_H */

View file

@ -274,7 +274,8 @@ struct clk *clk_register_mux(struct device *dev, const char *name,
* clk_register is the primary interface for populating the clock tree with new * clk_register is the primary interface for populating the clock tree with new
* clock nodes. It returns a pointer to the newly allocated struct clk which * clock nodes. It returns a pointer to the newly allocated struct clk which
* cannot be dereferenced by driver code but may be used in conjuction with the * cannot be dereferenced by driver code but may be used in conjuction with the
* rest of the clock API. * rest of the clock API. In the event of an error clk_register will return an
* error code; drivers must test for an error code after calling clk_register.
*/ */
struct clk *clk_register(struct device *dev, const char *name, struct clk *clk_register(struct device *dev, const char *name,
const struct clk_ops *ops, struct clk_hw *hw, const struct clk_ops *ops, struct clk_hw *hw,