All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boris BREZILLON <boris.brezillon@free-electrons.com>
To: "Gaël PORTAY" <gael.portay@gmail.com>
Cc: Arnd Bergmann <arnd@arndb.de>,
	Daniel Lezcano <daniel.lezcano@linaro.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org,
	Nicolas Ferre <nicolas.ferre@atmel.com>,
	Thierry Reding <thierry.reding@gmail.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Alexandre Belloni <alexandre.belloni@free-electrons.com>,
	Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Subject: Re: [PATCH 2/3] ARM: at91/tclib: move initialization from alloc to probe
Date: Wed, 20 Aug 2014 10:16:35 +0200	[thread overview]
Message-ID: <20140820101635.3a61d090@bbrezillon> (raw)
In-Reply-To: <1408486072-19268-3-git-send-email-gael.portay@gmail.com>

Hi Gael,

On Wed, 20 Aug 2014 00:07:51 +0200
Gaël PORTAY <gael.portay@gmail.com> wrote:

> Move resource retrieval from atmel_tc_alloc to tc_probe to avoid lately
> reporting resource related issues when a TC block user request a TC block.
> 
> Moreover, resources retrieval are usually done in the probe function,
> thus moving them add some consistency with other drivers.
> 
> Initialization is done once, ie not every time a tc block is requested.
> If it fails, the device is not appended to the list of tc blocks.
> 
> Furhermore, the device id is retrieved at probe as well, avoiding parsing
> DT every time the user requests of tc block.
> 
> Signed-off-by: Gaël PORTAY <gael.portay@gmail.com>

Acked-by: Boris Brezillon <boris.brezillon@free-electrons.com>

> ---
>  drivers/clocksource/tcb_clksrc.c |  2 +-
>  drivers/misc/atmel_tclib.c       | 71 +++++++++++++---------------------------
>  drivers/pwm/pwm-atmel-tcb.c      |  2 +-
>  include/linux/atmel_tc.h         |  8 +++--
>  4 files changed, 29 insertions(+), 54 deletions(-)
> 
> diff --git a/drivers/clocksource/tcb_clksrc.c b/drivers/clocksource/tcb_clksrc.c
> index a8d7ea1..f922e81 100644
> --- a/drivers/clocksource/tcb_clksrc.c
> +++ b/drivers/clocksource/tcb_clksrc.c
> @@ -279,7 +279,7 @@ static int __init tcb_clksrc_init(void)
>  	int i;
>  	int ret;
>  
> -	tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK, clksrc.name);
> +	tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK);
>  	if (!tc) {
>  		pr_debug("can't alloc TC for clocksource\n");
>  		return -ENODEV;
> diff --git a/drivers/misc/atmel_tclib.c b/drivers/misc/atmel_tclib.c
> index b514a2d..d505d1e 100644
> --- a/drivers/misc/atmel_tclib.c
> +++ b/drivers/misc/atmel_tclib.c
> @@ -35,60 +35,31 @@ static LIST_HEAD(tc_list);
>  /**
>   * atmel_tc_alloc - allocate a specified TC block
>   * @block: which block to allocate
> - * @name: name to be associated with the iomem resource
>   *
>   * Caller allocates a block.  If it is available, a pointer to a
>   * pre-initialized struct atmel_tc is returned. The caller can access
>   * the registers directly through the "regs" field.
>   */
> -struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name)
> +struct atmel_tc *atmel_tc_alloc(unsigned block)
>  {
>  	struct atmel_tc		*tc;
>  	struct platform_device	*pdev = NULL;
> -	struct resource		*r;
> -	size_t			size;
>  
>  	spin_lock(&tc_list_lock);
>  	list_for_each_entry(tc, &tc_list, node) {
> -		if (tc->pdev->dev.of_node) {
> -			if (of_alias_get_id(tc->pdev->dev.of_node, "tcb")
> -					== block) {
> -				pdev = tc->pdev;
> -				break;
> -			}
> -		} else if (tc->pdev->id == block) {
> +		if (tc->allocated)
> +			continue;
> +
> +		if ((tc->pdev->dev.of_node && tc->id == block) ||
> +		    (tc->pdev->id == block)) {
>  			pdev = tc->pdev;
> +			tc->allocated = true;
>  			break;
>  		}
>  	}
> -
> -	if (!pdev || tc->iomem)
> -		goto fail;
> -
> -	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> -	if (!r)
> -		goto fail;
> -
> -	size = resource_size(r);
> -	r = request_mem_region(r->start, size, name);
> -	if (!r)
> -		goto fail;
> -
> -	tc->regs = ioremap(r->start, size);
> -	if (!tc->regs)
> -		goto fail_ioremap;
> -
> -	tc->iomem = r;
> -
> -out:
>  	spin_unlock(&tc_list_lock);
> -	return tc;
>  
> -fail_ioremap:
> -	release_mem_region(r->start, size);
> -fail:
> -	tc = NULL;
> -	goto out;
> +	return pdev ? tc : NULL;
>  }
>  EXPORT_SYMBOL_GPL(atmel_tc_alloc);
>  
> @@ -96,19 +67,14 @@ EXPORT_SYMBOL_GPL(atmel_tc_alloc);
>   * atmel_tc_free - release a specified TC block
>   * @tc: Timer/counter block that was returned by atmel_tc_alloc()
>   *
> - * This reverses the effect of atmel_tc_alloc(), unmapping the I/O
> - * registers, invalidating the resource returned by that routine and
> - * making the TC available to other drivers.
> + * This reverses the effect of atmel_tc_alloc(), invalidating the resource
> + * returned by that routine and making the TC available to other drivers.
>   */
>  void atmel_tc_free(struct atmel_tc *tc)
>  {
>  	spin_lock(&tc_list_lock);
> -	if (tc->regs) {
> -		iounmap(tc->regs);
> -		release_mem_region(tc->iomem->start, resource_size(tc->iomem));
> -		tc->regs = NULL;
> -		tc->iomem = NULL;
> -	}
> +	if (tc->allocated)
> +		tc->allocated = false;
>  	spin_unlock(&tc_list_lock);
>  }
>  EXPORT_SYMBOL_GPL(atmel_tc_free);
> @@ -142,9 +108,7 @@ static int __init tc_probe(struct platform_device *pdev)
>  	struct atmel_tc *tc;
>  	struct clk	*clk;
>  	int		irq;
> -
> -	if (!platform_get_resource(pdev, IORESOURCE_MEM, 0))
> -		return -EINVAL;
> +	struct resource	*r;
>  
>  	irq = platform_get_irq(pdev, 0);
>  	if (irq < 0)
> @@ -160,12 +124,21 @@ static int __init tc_probe(struct platform_device *pdev)
>  	if (IS_ERR(clk))
>  		return PTR_ERR(clk);
>  
> +	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	tc->regs = devm_ioremap_resource(&pdev->dev, r);
> +	if (IS_ERR(tc->regs))
> +		return PTR_ERR(tc->regs);
> +
>  	/* Now take SoC information if available */
>  	if (pdev->dev.of_node) {
>  		const struct of_device_id *match;
>  		match = of_match_node(atmel_tcb_dt_ids, pdev->dev.of_node);
>  		if (match)
>  			tc->tcb_config = match->data;
> +
> +		tc->id = of_alias_get_id(tc->pdev->dev.of_node, "tcb");
> +	} else {
> +		tc->id = pdev->id;
>  	}
>  
>  	tc->clk[0] = clk;
> diff --git a/drivers/pwm/pwm-atmel-tcb.c b/drivers/pwm/pwm-atmel-tcb.c
> index f3dcd02..d56e5b7 100644
> --- a/drivers/pwm/pwm-atmel-tcb.c
> +++ b/drivers/pwm/pwm-atmel-tcb.c
> @@ -379,7 +379,7 @@ static int atmel_tcb_pwm_probe(struct platform_device *pdev)
>  		return err;
>  	}
>  
> -	tc = atmel_tc_alloc(tcblock, "tcb-pwm");
> +	tc = atmel_tc_alloc(tcblock);
>  	if (tc == NULL) {
>  		dev_err(&pdev->dev, "failed to allocate Timer Counter Block\n");
>  		return -ENOMEM;
> diff --git a/include/linux/atmel_tc.h b/include/linux/atmel_tc.h
> index 89a931b..d8aa884 100644
> --- a/include/linux/atmel_tc.h
> +++ b/include/linux/atmel_tc.h
> @@ -44,12 +44,13 @@ struct atmel_tcb_config {
>  /**
>   * struct atmel_tc - information about a Timer/Counter Block
>   * @pdev: physical device
> - * @iomem: resource associated with the I/O register
>   * @regs: mapping through which the I/O registers can be accessed
> + * @id: block id
>   * @tcb_config: configuration data from SoC
>   * @irq: irq for each of the three channels
>   * @clk: internal clock source for each of the three channels
>   * @node: list node, for tclib internal use
> + * @allocated: if already used, for tclib internal use
>   *
>   * On some platforms, each TC channel has its own clocks and IRQs,
>   * while on others, all TC channels share the same clock and IRQ.
> @@ -61,15 +62,16 @@ struct atmel_tcb_config {
>   */
>  struct atmel_tc {
>  	struct platform_device	*pdev;
> -	struct resource		*iomem;
>  	void __iomem		*regs;
> +	int                     id;
>  	const struct atmel_tcb_config *tcb_config;
>  	int			irq[3];
>  	struct clk		*clk[3];
>  	struct list_head	node;
> +	bool			allocated;
>  };
>  
> -extern struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name);
> +extern struct atmel_tc *atmel_tc_alloc(unsigned block);
>  extern void atmel_tc_free(struct atmel_tc *tc);
>  
>  /* platform-specific ATMEL_TC_TIMER_CLOCKx divisors (0 means 32KiHz) */



-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

WARNING: multiple messages have this Message-ID (diff)
From: boris.brezillon@free-electrons.com (Boris BREZILLON)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 2/3] ARM: at91/tclib: move initialization from alloc to probe
Date: Wed, 20 Aug 2014 10:16:35 +0200	[thread overview]
Message-ID: <20140820101635.3a61d090@bbrezillon> (raw)
In-Reply-To: <1408486072-19268-3-git-send-email-gael.portay@gmail.com>

Hi Gael,

On Wed, 20 Aug 2014 00:07:51 +0200
Ga?l PORTAY <gael.portay@gmail.com> wrote:

> Move resource retrieval from atmel_tc_alloc to tc_probe to avoid lately
> reporting resource related issues when a TC block user request a TC block.
> 
> Moreover, resources retrieval are usually done in the probe function,
> thus moving them add some consistency with other drivers.
> 
> Initialization is done once, ie not every time a tc block is requested.
> If it fails, the device is not appended to the list of tc blocks.
> 
> Furhermore, the device id is retrieved at probe as well, avoiding parsing
> DT every time the user requests of tc block.
> 
> Signed-off-by: Ga?l PORTAY <gael.portay@gmail.com>

Acked-by: Boris Brezillon <boris.brezillon@free-electrons.com>

> ---
>  drivers/clocksource/tcb_clksrc.c |  2 +-
>  drivers/misc/atmel_tclib.c       | 71 +++++++++++++---------------------------
>  drivers/pwm/pwm-atmel-tcb.c      |  2 +-
>  include/linux/atmel_tc.h         |  8 +++--
>  4 files changed, 29 insertions(+), 54 deletions(-)
> 
> diff --git a/drivers/clocksource/tcb_clksrc.c b/drivers/clocksource/tcb_clksrc.c
> index a8d7ea1..f922e81 100644
> --- a/drivers/clocksource/tcb_clksrc.c
> +++ b/drivers/clocksource/tcb_clksrc.c
> @@ -279,7 +279,7 @@ static int __init tcb_clksrc_init(void)
>  	int i;
>  	int ret;
>  
> -	tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK, clksrc.name);
> +	tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK);
>  	if (!tc) {
>  		pr_debug("can't alloc TC for clocksource\n");
>  		return -ENODEV;
> diff --git a/drivers/misc/atmel_tclib.c b/drivers/misc/atmel_tclib.c
> index b514a2d..d505d1e 100644
> --- a/drivers/misc/atmel_tclib.c
> +++ b/drivers/misc/atmel_tclib.c
> @@ -35,60 +35,31 @@ static LIST_HEAD(tc_list);
>  /**
>   * atmel_tc_alloc - allocate a specified TC block
>   * @block: which block to allocate
> - * @name: name to be associated with the iomem resource
>   *
>   * Caller allocates a block.  If it is available, a pointer to a
>   * pre-initialized struct atmel_tc is returned. The caller can access
>   * the registers directly through the "regs" field.
>   */
> -struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name)
> +struct atmel_tc *atmel_tc_alloc(unsigned block)
>  {
>  	struct atmel_tc		*tc;
>  	struct platform_device	*pdev = NULL;
> -	struct resource		*r;
> -	size_t			size;
>  
>  	spin_lock(&tc_list_lock);
>  	list_for_each_entry(tc, &tc_list, node) {
> -		if (tc->pdev->dev.of_node) {
> -			if (of_alias_get_id(tc->pdev->dev.of_node, "tcb")
> -					== block) {
> -				pdev = tc->pdev;
> -				break;
> -			}
> -		} else if (tc->pdev->id == block) {
> +		if (tc->allocated)
> +			continue;
> +
> +		if ((tc->pdev->dev.of_node && tc->id == block) ||
> +		    (tc->pdev->id == block)) {
>  			pdev = tc->pdev;
> +			tc->allocated = true;
>  			break;
>  		}
>  	}
> -
> -	if (!pdev || tc->iomem)
> -		goto fail;
> -
> -	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> -	if (!r)
> -		goto fail;
> -
> -	size = resource_size(r);
> -	r = request_mem_region(r->start, size, name);
> -	if (!r)
> -		goto fail;
> -
> -	tc->regs = ioremap(r->start, size);
> -	if (!tc->regs)
> -		goto fail_ioremap;
> -
> -	tc->iomem = r;
> -
> -out:
>  	spin_unlock(&tc_list_lock);
> -	return tc;
>  
> -fail_ioremap:
> -	release_mem_region(r->start, size);
> -fail:
> -	tc = NULL;
> -	goto out;
> +	return pdev ? tc : NULL;
>  }
>  EXPORT_SYMBOL_GPL(atmel_tc_alloc);
>  
> @@ -96,19 +67,14 @@ EXPORT_SYMBOL_GPL(atmel_tc_alloc);
>   * atmel_tc_free - release a specified TC block
>   * @tc: Timer/counter block that was returned by atmel_tc_alloc()
>   *
> - * This reverses the effect of atmel_tc_alloc(), unmapping the I/O
> - * registers, invalidating the resource returned by that routine and
> - * making the TC available to other drivers.
> + * This reverses the effect of atmel_tc_alloc(), invalidating the resource
> + * returned by that routine and making the TC available to other drivers.
>   */
>  void atmel_tc_free(struct atmel_tc *tc)
>  {
>  	spin_lock(&tc_list_lock);
> -	if (tc->regs) {
> -		iounmap(tc->regs);
> -		release_mem_region(tc->iomem->start, resource_size(tc->iomem));
> -		tc->regs = NULL;
> -		tc->iomem = NULL;
> -	}
> +	if (tc->allocated)
> +		tc->allocated = false;
>  	spin_unlock(&tc_list_lock);
>  }
>  EXPORT_SYMBOL_GPL(atmel_tc_free);
> @@ -142,9 +108,7 @@ static int __init tc_probe(struct platform_device *pdev)
>  	struct atmel_tc *tc;
>  	struct clk	*clk;
>  	int		irq;
> -
> -	if (!platform_get_resource(pdev, IORESOURCE_MEM, 0))
> -		return -EINVAL;
> +	struct resource	*r;
>  
>  	irq = platform_get_irq(pdev, 0);
>  	if (irq < 0)
> @@ -160,12 +124,21 @@ static int __init tc_probe(struct platform_device *pdev)
>  	if (IS_ERR(clk))
>  		return PTR_ERR(clk);
>  
> +	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	tc->regs = devm_ioremap_resource(&pdev->dev, r);
> +	if (IS_ERR(tc->regs))
> +		return PTR_ERR(tc->regs);
> +
>  	/* Now take SoC information if available */
>  	if (pdev->dev.of_node) {
>  		const struct of_device_id *match;
>  		match = of_match_node(atmel_tcb_dt_ids, pdev->dev.of_node);
>  		if (match)
>  			tc->tcb_config = match->data;
> +
> +		tc->id = of_alias_get_id(tc->pdev->dev.of_node, "tcb");
> +	} else {
> +		tc->id = pdev->id;
>  	}
>  
>  	tc->clk[0] = clk;
> diff --git a/drivers/pwm/pwm-atmel-tcb.c b/drivers/pwm/pwm-atmel-tcb.c
> index f3dcd02..d56e5b7 100644
> --- a/drivers/pwm/pwm-atmel-tcb.c
> +++ b/drivers/pwm/pwm-atmel-tcb.c
> @@ -379,7 +379,7 @@ static int atmel_tcb_pwm_probe(struct platform_device *pdev)
>  		return err;
>  	}
>  
> -	tc = atmel_tc_alloc(tcblock, "tcb-pwm");
> +	tc = atmel_tc_alloc(tcblock);
>  	if (tc == NULL) {
>  		dev_err(&pdev->dev, "failed to allocate Timer Counter Block\n");
>  		return -ENOMEM;
> diff --git a/include/linux/atmel_tc.h b/include/linux/atmel_tc.h
> index 89a931b..d8aa884 100644
> --- a/include/linux/atmel_tc.h
> +++ b/include/linux/atmel_tc.h
> @@ -44,12 +44,13 @@ struct atmel_tcb_config {
>  /**
>   * struct atmel_tc - information about a Timer/Counter Block
>   * @pdev: physical device
> - * @iomem: resource associated with the I/O register
>   * @regs: mapping through which the I/O registers can be accessed
> + * @id: block id
>   * @tcb_config: configuration data from SoC
>   * @irq: irq for each of the three channels
>   * @clk: internal clock source for each of the three channels
>   * @node: list node, for tclib internal use
> + * @allocated: if already used, for tclib internal use
>   *
>   * On some platforms, each TC channel has its own clocks and IRQs,
>   * while on others, all TC channels share the same clock and IRQ.
> @@ -61,15 +62,16 @@ struct atmel_tcb_config {
>   */
>  struct atmel_tc {
>  	struct platform_device	*pdev;
> -	struct resource		*iomem;
>  	void __iomem		*regs;
> +	int                     id;
>  	const struct atmel_tcb_config *tcb_config;
>  	int			irq[3];
>  	struct clk		*clk[3];
>  	struct list_head	node;
> +	bool			allocated;
>  };
>  
> -extern struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name);
> +extern struct atmel_tc *atmel_tc_alloc(unsigned block);
>  extern void atmel_tc_free(struct atmel_tc *tc);
>  
>  /* platform-specific ATMEL_TC_TIMER_CLOCKx divisors (0 means 32KiHz) */



-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

  parent reply	other threads:[~2014-08-20  8:16 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-19 22:07 [PATCH 0/3] ARM: at91/tclib: fix segmentation fault Gaël PORTAY
2014-08-19 22:07 ` Gaël PORTAY
2014-08-19 22:07 ` [PATCH 1/3] ARM: at91/tclib: prefer using of devm_* functions Gaël PORTAY
2014-08-19 22:07   ` Gaël PORTAY
2014-08-20  8:19   ` Boris BREZILLON
2014-08-20  8:19     ` Boris BREZILLON
2014-08-19 22:07 ` [PATCH 2/3] ARM: at91/tclib: move initialization from alloc to probe Gaël PORTAY
2014-08-19 22:07   ` Gaël PORTAY
2014-08-20  7:39   ` Thierry Reding
2014-08-20  7:39     ` Thierry Reding
2014-08-20  8:16   ` Boris BREZILLON [this message]
2014-08-20  8:16     ` Boris BREZILLON
2014-08-19 22:07 ` [PATCH 3/3] ARM: at91/tclib: mask interruptions at shutdown and probe Gaël PORTAY
2014-08-19 22:07   ` Gaël PORTAY
2014-08-19 22:11   ` Jean-Christophe PLAGNIOL-VILLARD
2014-08-19 22:11     ` Jean-Christophe PLAGNIOL-VILLARD
2014-08-19 23:01     ` Boris BREZILLON
2014-08-19 23:01       ` Boris BREZILLON
2014-08-20  7:31       ` Thierry Reding
2014-08-20  7:31         ` Thierry Reding
2014-08-20  8:14         ` Boris BREZILLON
2014-08-20  8:14           ` Boris BREZILLON
2014-08-20  8:28           ` Thierry Reding
2014-08-20  8:28             ` Thierry Reding
2014-08-20  9:06             ` Boris BREZILLON
2014-08-20  9:06               ` Boris BREZILLON
2014-08-20  9:48               ` Thierry Reding
2014-08-20  9:48                 ` Thierry Reding
2014-08-20 10:07                 ` Boris BREZILLON
2014-08-20 10:07                   ` Boris BREZILLON
2014-08-27 15:27         ` Gaël PORTAY
2014-08-21  3:32   ` Arnd Bergmann
2014-08-21  3:32     ` Arnd Bergmann
2014-08-21  9:43     ` Gaël PORTAY

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=20140820101635.3a61d090@bbrezillon \
    --to=boris.brezillon@free-electrons.com \
    --cc=alexandre.belloni@free-electrons.com \
    --cc=arnd@arndb.de \
    --cc=daniel.lezcano@linaro.org \
    --cc=gael.portay@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=nicolas.ferre@atmel.com \
    --cc=plagnioj@jcrosoft.com \
    --cc=tglx@linutronix.de \
    --cc=thierry.reding@gmail.com \
    /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.