All of lore.kernel.org
 help / color / mirror / Atom feed
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@codeaurora.org>,
	Magnus Damm <damm+renesas@opensource.se>,
	Simon Horman <horms+renesas@verge.net.au>,
	Rob Herring <robh+dt@kernel.org>, Pawel Moll <pawel.moll@arm.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Ian Campbell <ijc+devicetree@hellion.org.uk>,
	Kumar Gala <galak@codeaurora.org>,
	linux-clk@vger.kernel.org, devicetree@vger.kernel.org,
	linux-sh@vger.kernel.org
Subject: Re: [PATCH v4 3/5] [RFC] clk: shmobile: div6: Extract cpg_div6_register()
Date: Fri, 23 Oct 2015 14:28:43 +0300	[thread overview]
Message-ID: <12260553.nd9Kq07kR3@avalon> (raw)
In-Reply-To: <1444999760-15750-4-git-send-email-geert+renesas@glider.be>

Hi Geert,

Thank you for the patch.

On Friday 16 October 2015 14:49:18 Geert Uytterhoeven wrote:
> Extract cpg_div6_register(), to allow registering div6 clocks from
> another clock driver.
> 
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---
> v4:
>   - New.
> ---
>  drivers/clk/shmobile/clk-div6.c | 119 +++++++++++++++++++++++-------------
>  drivers/clk/shmobile/clk-div6.h |   3 +
>  2 files changed, 77 insertions(+), 45 deletions(-)
>  create mode 100644 drivers/clk/shmobile/clk-div6.h
> 
> diff --git a/drivers/clk/shmobile/clk-div6.c
> b/drivers/clk/shmobile/clk-div6.c index 57016ff9c585fc6e..5e8525fc60427229
> 100644
> --- a/drivers/clk/shmobile/clk-div6.c
> +++ b/drivers/clk/shmobile/clk-div6.c
> @@ -18,6 +18,8 @@
>  #include <linux/of_address.h>
>  #include <linux/slab.h>
> 
> +#include "clk-div6.h"
> +
>  #define CPG_DIV6_CKSTP		BIT(8)
>  #define CPG_DIV6_DIV(d)		((d) & 0x3f)
>  #define CPG_DIV6_DIV_MASK	0x3f
> @@ -172,60 +174,34 @@ static const struct clk_ops cpg_div6_clock_ops = {
>  	.set_rate = cpg_div6_clock_set_rate,
>  };

Non-static functions deserve a bit of documentation :-)

> -static void __init cpg_div6_clock_init(struct device_node *np)
> +struct clk * __init cpg_div6_register(const char *name,
> +				      unsigned int num_parents,
> +				      const char **parent_names,
> +				      void __iomem *reg)
>  {
> -	unsigned int num_parents, valid_parents;
> -	const char **parent_names;
> +	unsigned int valid_parents;
>  	struct clk_init_data init;
>  	struct div6_clock *clock;
> -	const char *clk_name = np->name;
>  	struct clk *clk;
>  	unsigned int i;
> 
>  	clock = kzalloc(sizeof(*clock), GFP_KERNEL);
>  	if (!clock)
> -		return;
> -
> -	num_parents = of_clk_get_parent_count(np);
> -	if (num_parents < 1) {
> -		pr_err("%s: no parent found for %s DIV6 clock\n",
> -		       __func__, np->name);
> -		return;
> -	}
> +		return ERR_PTR(-ENOMEM);
> 
>  	clock->parents = kmalloc_array(num_parents, sizeof(*clock->parents),
> -		GFP_KERNEL);
> -	parent_names = kmalloc_array(num_parents, sizeof(*parent_names),
> -				GFP_KERNEL);
> -	if (!parent_names)
> -		return;
> +				       GFP_KERNEL);
> +	if (!clock->parents)
> +		return ERR_PTR(-ENOMEM);
> 
> -	/* Remap the clock register and read the divisor. Disabling the
> -	 * clock overwrites the divisor, so we need to cache its value for the
> -	 * enable operation.
> -	 */
> -	clock->reg = of_iomap(np, 0);
> -	if (clock->reg == NULL) {
> -		pr_err("%s: failed to map %s DIV6 clock register\n",
> -		       __func__, np->name);
> -		goto error;
> -	}
> +	clock->reg = reg;
> 
> +	/*
> +	 * Read the divisor. Disabling the clock overwrites the divisor, so we
> +	 * need to cache its value for the enable operation.
> +	 */
>  	clock->div = (clk_readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
> 
> -	/* Parse the DT properties. */
> -	of_property_read_string(np, "clock-output-names", &clk_name);
> -
> -	for (i = 0, valid_parents = 0; i < num_parents; i++) {
> -		const char *name = of_clk_get_parent_name(np, i);
> -
> -		if (name) {
> -			parent_names[valid_parents] = name;
> -			clock->parents[valid_parents] = i;
> -			valid_parents++;
> -		}
> -	}
> -
>  	switch (num_parents) {
>  	case 1:
>  		/* fixed parent clock */
> @@ -243,12 +219,22 @@ static void __init cpg_div6_clock_init(struct
> device_node *np) break;
>  	default:
>  		pr_err("%s: invalid number of parents for DIV6 clock %s\n",
> -		       __func__, np->name);
> +		       __func__, name);
> +		clk = ERR_PTR(-EINVAL);
>  		goto error;
>  	}
> 
> +	/* 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++;
> +		}
> +	}
> +
>  	/* Register the clock. */
> -	init.name = clk_name;
> +	init.name = name;
>  	init.ops = &cpg_div6_clock_ops;
>  	init.flags = CLK_IS_BASIC;
>  	init.parent_names = parent_names;
> @@ -257,6 +243,50 @@ static void __init cpg_div6_clock_init(struct
> device_node *np) clock->hw.init = &init;
> 
>  	clk = clk_register(NULL, &clock->hw);
> +	if (!IS_ERR(clk))
> +		return clk;
> +
> +error:
> +	kfree(clock->parents);
> +	kfree(clock);

If you think that errors when registering the DIV6 clock are not fatal enough 
to not have to care about memory leakage, you should kfree(clock) when the 
kmalloc_array() above fails.

> +	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) {
> +		pr_err("%s: no parent found for %s DIV6 clock\n",
> +		       __func__, np->name);
> +		return;
> +	}
> +
> +	parent_names = kmalloc_array(num_parents, sizeof(*parent_names),
> +				GFP_KERNEL);
> +	if (!parent_names)
> +		return;
> +
> +	reg = of_iomap(np, 0);
> +	if (reg == NULL) {
> +		pr_err("%s: failed to map %s DIV6 clock register\n",
> +		       __func__, np->name);
> +		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);
> +
> +	clk = cpg_div6_register(clk_name, num_parents, parent_names, reg);
>  	if (IS_ERR(clk)) {
>  		pr_err("%s: failed to register %s DIV6 clock (%ld)\n",
>  		       __func__, np->name, PTR_ERR(clk));
> @@ -269,9 +299,8 @@ static void __init cpg_div6_clock_init(struct
> device_node *np) return;
> 
>  error:
> -	if (clock->reg)
> -		iounmap(clock->reg);
> +	if (reg)
> +		iounmap(reg);
>  	kfree(parent_names);
> -	kfree(clock);
>  }
>  CLK_OF_DECLARE(cpg_div6_clk, "renesas,cpg-div6-clock",
> cpg_div6_clock_init); diff --git a/drivers/clk/shmobile/clk-div6.h
> b/drivers/clk/shmobile/clk-div6.h new file mode 100644
> index 0000000000000000..d19531f42953c83f
> --- /dev/null
> +++ b/drivers/clk/shmobile/clk-div6.h
> @@ -0,0 +1,3 @@
> +
> +struct clk *cpg_div6_register(const char *name, unsigned int num_parents,
> +			      const char **parent_names, void __iomem *reg);

Could you please add #ifdef include guard macros ?

-- 
Regards,

Laurent Pinchart

WARNING: multiple messages have this Message-ID (diff)
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@codeaurora.org>,
	Magnus Damm <damm+renesas@opensource.se>,
	Simon Horman <horms+renesas@verge.net.au>,
	Rob Herring <robh+dt@kernel.org>, Pawel Moll <pawel.moll@arm.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Ian Campbell <ijc+devicetree@hellion.org.uk>,
	Kumar Gala <galak@codeaurora.org>,
	linux-clk@vger.kernel.org, devicetree@vger.kernel.org,
	linux-sh@vger.kernel.org
Subject: Re: [PATCH v4 3/5] [RFC] clk: shmobile: div6: Extract cpg_div6_register()
Date: Fri, 23 Oct 2015 11:28:43 +0000	[thread overview]
Message-ID: <12260553.nd9Kq07kR3@avalon> (raw)
In-Reply-To: <1444999760-15750-4-git-send-email-geert+renesas@glider.be>

Hi Geert,

Thank you for the patch.

On Friday 16 October 2015 14:49:18 Geert Uytterhoeven wrote:
> Extract cpg_div6_register(), to allow registering div6 clocks from
> another clock driver.
> 
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---
> v4:
>   - New.
> ---
>  drivers/clk/shmobile/clk-div6.c | 119 +++++++++++++++++++++++-------------
>  drivers/clk/shmobile/clk-div6.h |   3 +
>  2 files changed, 77 insertions(+), 45 deletions(-)
>  create mode 100644 drivers/clk/shmobile/clk-div6.h
> 
> diff --git a/drivers/clk/shmobile/clk-div6.c
> b/drivers/clk/shmobile/clk-div6.c index 57016ff9c585fc6e..5e8525fc60427229
> 100644
> --- a/drivers/clk/shmobile/clk-div6.c
> +++ b/drivers/clk/shmobile/clk-div6.c
> @@ -18,6 +18,8 @@
>  #include <linux/of_address.h>
>  #include <linux/slab.h>
> 
> +#include "clk-div6.h"
> +
>  #define CPG_DIV6_CKSTP		BIT(8)
>  #define CPG_DIV6_DIV(d)		((d) & 0x3f)
>  #define CPG_DIV6_DIV_MASK	0x3f
> @@ -172,60 +174,34 @@ static const struct clk_ops cpg_div6_clock_ops = {
>  	.set_rate = cpg_div6_clock_set_rate,
>  };

Non-static functions deserve a bit of documentation :-)

> -static void __init cpg_div6_clock_init(struct device_node *np)
> +struct clk * __init cpg_div6_register(const char *name,
> +				      unsigned int num_parents,
> +				      const char **parent_names,
> +				      void __iomem *reg)
>  {
> -	unsigned int num_parents, valid_parents;
> -	const char **parent_names;
> +	unsigned int valid_parents;
>  	struct clk_init_data init;
>  	struct div6_clock *clock;
> -	const char *clk_name = np->name;
>  	struct clk *clk;
>  	unsigned int i;
> 
>  	clock = kzalloc(sizeof(*clock), GFP_KERNEL);
>  	if (!clock)
> -		return;
> -
> -	num_parents = of_clk_get_parent_count(np);
> -	if (num_parents < 1) {
> -		pr_err("%s: no parent found for %s DIV6 clock\n",
> -		       __func__, np->name);
> -		return;
> -	}
> +		return ERR_PTR(-ENOMEM);
> 
>  	clock->parents = kmalloc_array(num_parents, sizeof(*clock->parents),
> -		GFP_KERNEL);
> -	parent_names = kmalloc_array(num_parents, sizeof(*parent_names),
> -				GFP_KERNEL);
> -	if (!parent_names)
> -		return;
> +				       GFP_KERNEL);
> +	if (!clock->parents)
> +		return ERR_PTR(-ENOMEM);
> 
> -	/* Remap the clock register and read the divisor. Disabling the
> -	 * clock overwrites the divisor, so we need to cache its value for the
> -	 * enable operation.
> -	 */
> -	clock->reg = of_iomap(np, 0);
> -	if (clock->reg = NULL) {
> -		pr_err("%s: failed to map %s DIV6 clock register\n",
> -		       __func__, np->name);
> -		goto error;
> -	}
> +	clock->reg = reg;
> 
> +	/*
> +	 * Read the divisor. Disabling the clock overwrites the divisor, so we
> +	 * need to cache its value for the enable operation.
> +	 */
>  	clock->div = (clk_readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
> 
> -	/* Parse the DT properties. */
> -	of_property_read_string(np, "clock-output-names", &clk_name);
> -
> -	for (i = 0, valid_parents = 0; i < num_parents; i++) {
> -		const char *name = of_clk_get_parent_name(np, i);
> -
> -		if (name) {
> -			parent_names[valid_parents] = name;
> -			clock->parents[valid_parents] = i;
> -			valid_parents++;
> -		}
> -	}
> -
>  	switch (num_parents) {
>  	case 1:
>  		/* fixed parent clock */
> @@ -243,12 +219,22 @@ static void __init cpg_div6_clock_init(struct
> device_node *np) break;
>  	default:
>  		pr_err("%s: invalid number of parents for DIV6 clock %s\n",
> -		       __func__, np->name);
> +		       __func__, name);
> +		clk = ERR_PTR(-EINVAL);
>  		goto error;
>  	}
> 
> +	/* 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++;
> +		}
> +	}
> +
>  	/* Register the clock. */
> -	init.name = clk_name;
> +	init.name = name;
>  	init.ops = &cpg_div6_clock_ops;
>  	init.flags = CLK_IS_BASIC;
>  	init.parent_names = parent_names;
> @@ -257,6 +243,50 @@ static void __init cpg_div6_clock_init(struct
> device_node *np) clock->hw.init = &init;
> 
>  	clk = clk_register(NULL, &clock->hw);
> +	if (!IS_ERR(clk))
> +		return clk;
> +
> +error:
> +	kfree(clock->parents);
> +	kfree(clock);

If you think that errors when registering the DIV6 clock are not fatal enough 
to not have to care about memory leakage, you should kfree(clock) when the 
kmalloc_array() above fails.

> +	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) {
> +		pr_err("%s: no parent found for %s DIV6 clock\n",
> +		       __func__, np->name);
> +		return;
> +	}
> +
> +	parent_names = kmalloc_array(num_parents, sizeof(*parent_names),
> +				GFP_KERNEL);
> +	if (!parent_names)
> +		return;
> +
> +	reg = of_iomap(np, 0);
> +	if (reg = NULL) {
> +		pr_err("%s: failed to map %s DIV6 clock register\n",
> +		       __func__, np->name);
> +		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);
> +
> +	clk = cpg_div6_register(clk_name, num_parents, parent_names, reg);
>  	if (IS_ERR(clk)) {
>  		pr_err("%s: failed to register %s DIV6 clock (%ld)\n",
>  		       __func__, np->name, PTR_ERR(clk));
> @@ -269,9 +299,8 @@ static void __init cpg_div6_clock_init(struct
> device_node *np) return;
> 
>  error:
> -	if (clock->reg)
> -		iounmap(clock->reg);
> +	if (reg)
> +		iounmap(reg);
>  	kfree(parent_names);
> -	kfree(clock);
>  }
>  CLK_OF_DECLARE(cpg_div6_clk, "renesas,cpg-div6-clock",
> cpg_div6_clock_init); diff --git a/drivers/clk/shmobile/clk-div6.h
> b/drivers/clk/shmobile/clk-div6.h new file mode 100644
> index 0000000000000000..d19531f42953c83f
> --- /dev/null
> +++ b/drivers/clk/shmobile/clk-div6.h
> @@ -0,0 +1,3 @@
> +
> +struct clk *cpg_div6_register(const char *name, unsigned int num_parents,
> +			      const char **parent_names, void __iomem *reg);

Could you please add #ifdef include guard macros ?

-- 
Regards,

Laurent Pinchart


  reply	other threads:[~2015-10-23 11:28 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-16 12:49 [PATCH/RFC v4 0/5] clk: shmobile: Add new Renesas CPG/MSSR DT bindings Geert Uytterhoeven
2015-10-16 12:49 ` Geert Uytterhoeven
2015-10-16 12:49 ` [PATCH v4 1/5] [RFC] " Geert Uytterhoeven
2015-10-16 12:49   ` Geert Uytterhoeven
2015-10-20 10:15   ` Michael Turquette
2015-10-20 10:15     ` Michael Turquette
2015-10-20 10:15     ` Michael Turquette
2015-10-20 12:16   ` Geert Uytterhoeven
2015-10-20 12:16     ` Geert Uytterhoeven
2015-10-20 12:16     ` Geert Uytterhoeven
2015-10-20 16:01     ` Magnus Damm
2015-10-20 16:01       ` Magnus Damm
2015-10-20 16:01       ` Magnus Damm
2015-10-23 11:05       ` Laurent Pinchart
2015-10-23 11:05         ` Laurent Pinchart
2015-10-23 11:05         ` Laurent Pinchart
2015-10-23 11:09         ` Geert Uytterhoeven
2015-10-23 11:09           ` Geert Uytterhoeven
2015-10-23 11:09           ` Geert Uytterhoeven
2015-10-23 11:11           ` Laurent Pinchart
2015-10-23 11:11             ` Laurent Pinchart
2015-10-23 11:10   ` Laurent Pinchart
2015-10-23 11:10     ` Laurent Pinchart
2015-10-26 19:02     ` Geert Uytterhoeven
2015-10-26 19:02       ` Geert Uytterhoeven
2015-10-27  1:34       ` Laurent Pinchart
2015-10-27  1:34         ` Laurent Pinchart
2015-10-27  8:14         ` Geert Uytterhoeven
2015-10-27  8:14           ` Geert Uytterhoeven
2015-10-30 13:30           ` Laurent Pinchart
2015-10-30 13:30             ` Laurent Pinchart
2015-10-16 12:49 ` [PATCH v4 2/5] [RFC] clk: shmobile: Add r8a7795 CPG Core Clock Definitions Geert Uytterhoeven
2015-10-16 12:49   ` Geert Uytterhoeven
2015-10-20 10:09   ` Geert Uytterhoeven
2015-10-20 10:09     ` Geert Uytterhoeven
2015-10-20 16:21     ` Magnus Damm
2015-10-20 16:21       ` Magnus Damm
2015-10-23 11:21   ` Laurent Pinchart
2015-10-23 11:21     ` Laurent Pinchart
2015-10-23 11:25     ` Geert Uytterhoeven
2015-10-23 11:25       ` Geert Uytterhoeven
2015-10-23 11:25       ` Geert Uytterhoeven
2015-10-16 12:49 ` [PATCH v4 3/5] [RFC] clk: shmobile: div6: Extract cpg_div6_register() Geert Uytterhoeven
2015-10-16 12:49   ` Geert Uytterhoeven
2015-10-23 11:28   ` Laurent Pinchart [this message]
2015-10-23 11:28     ` Laurent Pinchart
2015-10-16 12:49 ` [PATCH v4 4/5] [RFC] clk: shmobile: cpg-mssr: Add new CPG/MSSR driver core Geert Uytterhoeven
2015-10-16 12:49   ` Geert Uytterhoeven
2015-10-16 12:49 ` [PATCH v4 5/5] [RFC] clk: shmobile: r8a7795: Add new CPG/MSSR driver Geert Uytterhoeven
2015-10-16 12:49   ` Geert Uytterhoeven
2015-10-20 12:24   ` Michael Turquette
2015-10-20 12:24     ` Michael Turquette
2015-10-20 12:24     ` Michael Turquette
2015-10-20 12:31     ` Geert Uytterhoeven
2015-10-20 12:31       ` Geert Uytterhoeven
2015-10-20 12:31       ` Geert Uytterhoeven
2015-10-20 13:00       ` Michael Turquette
2015-10-20 13:00         ` Michael Turquette
2015-10-20 13:07         ` Geert Uytterhoeven
2015-10-20 13:07           ` Geert Uytterhoeven
2015-10-20 13:07           ` Geert Uytterhoeven
2015-10-22 12:58           ` Geert Uytterhoeven
2015-10-22 12:58             ` Geert Uytterhoeven
2015-10-22 12:58             ` Geert Uytterhoeven
2015-10-24  1:10             ` Stephen Boyd
2015-10-24  1:10               ` Stephen Boyd
2015-10-24 17:34               ` Geert Uytterhoeven
2015-10-24 17:34                 ` Geert Uytterhoeven
2015-10-26  2:25                 ` Laurent Pinchart
2015-10-26  2:25                   ` Laurent Pinchart
2015-10-26  8:03                   ` Geert Uytterhoeven
2015-10-26  8:03                     ` Geert Uytterhoeven
2015-10-30 13:12                     ` Laurent Pinchart
2015-10-30 13:12                       ` Laurent Pinchart
2015-10-29 14:03             ` Geert Uytterhoeven
2015-10-29 14:03               ` Geert Uytterhoeven

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12260553.nd9Kq07kR3@avalon \
    --to=laurent.pinchart@ideasonboard.com \
    --cc=damm+renesas@opensource.se \
    --cc=devicetree@vger.kernel.org \
    --cc=galak@codeaurora.org \
    --cc=geert+renesas@glider.be \
    --cc=horms+renesas@verge.net.au \
    --cc=ijc+devicetree@hellion.org.uk \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-sh@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mturquette@baylibre.com \
    --cc=pawel.moll@arm.com \
    --cc=robh+dt@kernel.org \
    --cc=sboyd@codeaurora.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.