linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] i2c: nomadik: adopt pinctrl support
@ 2012-09-28 12:59 Linus Walleij
       [not found] ` <1348837151-20820-1-git-send-email-linus.walleij-0IS4wlFg1OjSUeElwK9/Pw@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: Linus Walleij @ 2012-09-28 12:59 UTC (permalink / raw)
  To: Ben Dooks, Wolfram Sang, linux-i2c-u79uwXL29TY76Z2rM5mHXA
  Cc: Anmar Oueja, Patrice Chotard, Linus Walleij

From: Patrice Chotard <patrice.chotard-0IS4wlFg1OjSUeElwK9/Pw@public.gmane.org>

Amend the I2C nomadik pin controller to optionally take a pin control
handle and set the state of the pins to:

- "default" on boot, resume and before performing an i2c transfer
- "idle" after initial default, after resume default, and after each
   i2c xfer
- "sleep" on suspend()

This should make it possible to optimize energy usage for the pins
both for the suspend/resume cycle, and for runtime cases inbetween
I2C transfers.

Signed-off-by: Patrice Chotard <patrice.chotard-0IS4wlFg1OjSUeElwK9/Pw@public.gmane.org>
Signed-off-by: Linus Walleij <linus.walleij-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
---
ChangeLog v1->v2:
- We used only two states initially: default and sleep. It turns
  out you can save some energy when idling (between transfers)
  and even more when suspending on our platform, so grab all
  three states and use them as applicable.
---
 drivers/i2c/busses/i2c-nomadik.c | 102 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)

diff --git a/drivers/i2c/busses/i2c-nomadik.c b/drivers/i2c/busses/i2c-nomadik.c
index 1b898b6..bd3da46 100644
--- a/drivers/i2c/busses/i2c-nomadik.c
+++ b/drivers/i2c/busses/i2c-nomadik.c
@@ -24,6 +24,7 @@
 #include <linux/io.h>
 #include <linux/pm_runtime.h>
 #include <linux/platform_data/i2c-nomadik.h>
+#include <linux/pinctrl/consumer.h>
 
 #define DRIVER_NAME "nmk-i2c"
 
@@ -145,6 +146,10 @@ struct i2c_nmk_client {
  * @stop: stop condition.
  * @xfer_complete: acknowledge completion for a I2C message.
  * @result: controller propogated result.
+ * @pinctrl: pinctrl handle.
+ * @pins_default: default state for the pins.
+ * @pins_idle: idle state for the pins.
+ * @pins_sleep: sleep state for the pins.
  * @busy: Busy doing transfer.
  */
 struct nmk_i2c_dev {
@@ -158,6 +163,11 @@ struct nmk_i2c_dev {
 	int				stop;
 	struct completion		xfer_complete;
 	int				result;
+	/* Three pin states - default, idle & sleep */
+	struct pinctrl			*pinctrl;
+	struct pinctrl_state		*pins_default;
+	struct pinctrl_state		*pins_idle;
+	struct pinctrl_state		*pins_sleep;
 	bool				busy;
 };
 
@@ -642,6 +652,15 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
 
 	pm_runtime_get_sync(&dev->adev->dev);
 
+	/* Optionaly enable pins to be muxed in and configured */
+	if (!IS_ERR(dev->pins_default)) {
+		status = pinctrl_select_state(dev->pinctrl,
+				dev->pins_default);
+		if (status)
+			dev_err(&dev->adev->dev,
+				"could not set default pins\n");
+	}
+
 	clk_enable(dev->clk);
 
 	status = init_hw(dev);
@@ -670,6 +689,16 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
 
 out:
 	clk_disable(dev->clk);
+
+	/* Optionally let pins go into idle state */
+	if (!IS_ERR(dev->pins_idle)) {
+		status = pinctrl_select_state(dev->pinctrl,
+				dev->pins_idle);
+		if (status)
+			dev_err(&dev->adev->dev,
+				"could not set pins to idle state\n");
+	}
+
 	pm_runtime_put_sync(&dev->adev->dev);
 
 	dev->busy = false;
@@ -864,15 +893,44 @@ static int nmk_i2c_suspend(struct device *dev)
 {
 	struct amba_device *adev = to_amba_device(dev);
 	struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev);
+	int ret;
 
 	if (nmk_i2c->busy)
 		return -EBUSY;
 
+	if (!IS_ERR(nmk_i2c->pins_sleep)) {
+		ret = pinctrl_select_state(nmk_i2c->pinctrl,
+				nmk_i2c->pins_sleep);
+		if (ret)
+			dev_err(dev,
+				"could not set pins to sleep state\n");
+	}
+
 	return 0;
 }
 
 static int nmk_i2c_resume(struct device *dev)
 {
+	struct amba_device *adev = to_amba_device(dev);
+	struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev);
+	int ret;
+
+	/* First go to the default state */
+	if (!IS_ERR(nmk_i2c->pins_default)) {
+		ret = pinctrl_select_state(nmk_i2c->pinctrl,
+				nmk_i2c->pins_default);
+		if (ret)
+			dev_err(dev,
+				"could not set pins to default state\n");
+	}
+	/* Then let's idle the pins until the next transfer happens */
+	if (!IS_ERR(nmk_i2c->pins_idle)) {
+		ret = pinctrl_select_state(nmk_i2c->pinctrl,
+				nmk_i2c->pins_idle);
+		if (ret)
+			dev_err(dev,
+				"could not set pins to idle state\n");
+	}
 	return 0;
 }
 #else
@@ -936,6 +994,40 @@ static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
 	dev->adev = adev;
 	amba_set_drvdata(adev, dev);
 
+	dev->pinctrl = devm_pinctrl_get(&adev->dev);
+	if (IS_ERR(dev->pinctrl)) {
+		ret = PTR_ERR(dev->pinctrl);
+		goto err_pinctrl;
+	}
+
+	dev->pins_default = pinctrl_lookup_state(dev->pinctrl,
+						 PINCTRL_STATE_DEFAULT);
+	if (IS_ERR(dev->pins_default))
+		dev_err(&adev->dev, "could not get default pinstate\n");
+	else {
+		ret = pinctrl_select_state(dev->pinctrl,
+					   dev->pins_default);
+		if (ret)
+			dev_dbg(&adev->dev, "could not set default pinstate\n");
+	}
+
+	dev->pins_idle = pinctrl_lookup_state(dev->pinctrl,
+					      PINCTRL_STATE_IDLE);
+	if (IS_ERR(dev->pins_idle))
+		dev_dbg(&adev->dev, "could not get idle pinstate\n");
+	else {
+		/* If possible, let's go to idle until the first transfer */
+		ret = pinctrl_select_state(dev->pinctrl,
+					   dev->pins_idle);
+		if (ret)
+			dev_dbg(&adev->dev, "could not set idle pinstate\n");
+	}
+
+	dev->pins_sleep = pinctrl_lookup_state(dev->pinctrl,
+					       PINCTRL_STATE_SLEEP);
+	if (IS_ERR(dev->pins_sleep))
+		dev_dbg(&adev->dev, "could not get sleep pinstate\n");
+
 	dev->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
 	if (!dev->virtbase) {
 		ret = -ENOMEM;
@@ -959,6 +1051,12 @@ static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
 		goto err_no_clk;
 	}
 
+	ret = clk_prepare(dev->clk);
+	if (ret) {
+		dev_err(&adev->dev, "can't prepare clock\n");
+		goto err_prep_clk;
+	}
+
 	adap = &dev->adap;
 	adap->dev.parent = &adev->dev;
 	adap->owner	= THIS_MODULE;
@@ -994,6 +1092,8 @@ static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
 	return 0;
 
  err_add_adap:
+	clk_unprepare(dev->clk);
+ err_prep_clk:
 	clk_put(dev->clk);
  err_no_clk:
 	free_irq(dev->irq, dev);
@@ -1002,6 +1102,7 @@ static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
  err_no_ioremap:
 	amba_set_drvdata(adev, NULL);
 	kfree(dev);
+ err_pinctrl:
  err_no_mem:
 
 	return ret;
@@ -1022,6 +1123,7 @@ static int nmk_i2c_remove(struct amba_device *adev)
 	iounmap(dev->virtbase);
 	if (res)
 		release_mem_region(res->start, resource_size(res));
+	clk_unprepare(dev->clk);
 	clk_put(dev->clk);
 	pm_runtime_disable(&adev->dev);
 	amba_set_drvdata(adev, NULL);
-- 
1.7.11.3

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] i2c: nomadik: adopt pinctrl support
       [not found] ` <1348837151-20820-1-git-send-email-linus.walleij-0IS4wlFg1OjSUeElwK9/Pw@public.gmane.org>
@ 2012-10-06 13:30   ` Wolfram Sang
       [not found]     ` <20121006133006.GA25123-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: Wolfram Sang @ 2012-10-06 13:30 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Ben Dooks, linux-i2c-u79uwXL29TY76Z2rM5mHXA, Anmar Oueja,
	Patrice Chotard, Linus Walleij

[-- Attachment #1: Type: text/plain, Size: 698 bytes --]

Hi,

> +	ret = clk_prepare(dev->clk);
> +	if (ret) {
> +		dev_err(&adev->dev, "can't prepare clock\n");
> +		goto err_prep_clk;
> +	}
> +
>  	adap = &dev->adap;
>  	adap->dev.parent = &adev->dev;
>  	adap->owner	= THIS_MODULE;
> @@ -994,6 +1092,8 @@ static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
>  	return 0;
>  
>   err_add_adap:
> +	clk_unprepare(dev->clk);
> + err_prep_clk:

This is unrelated, right? And it is also unneeded because of Ulf
Hansson's patch?

Regards,

   Wolfram

-- 
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] i2c: nomadik: adopt pinctrl support
       [not found]     ` <20121006133006.GA25123-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
@ 2012-10-10  8:03       ` Linus Walleij
  2012-10-10  9:16       ` Linus Walleij
  1 sibling, 0 replies; 5+ messages in thread
From: Linus Walleij @ 2012-10-10  8:03 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: Linus Walleij, Ben Dooks, linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	Anmar Oueja, Patrice Chotard

On Sat, Oct 6, 2012 at 3:30 PM, Wolfram Sang <w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org> wrote:

>>   err_add_adap:
>> +     clk_unprepare(dev->clk);
>> + err_prep_clk:
>
> This is unrelated, right? And it is also unneeded because of Ulf
> Hansson's patch?

True. I'll roll out a v3 based on the latest i2c code.

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] i2c: nomadik: adopt pinctrl support
       [not found]     ` <20121006133006.GA25123-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
  2012-10-10  8:03       ` Linus Walleij
@ 2012-10-10  9:16       ` Linus Walleij
       [not found]         ` <CACRpkdbdH6uLMt9oRTDEH3TBFppb2idtapHRKjoGtQ3cfvis2g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  1 sibling, 1 reply; 5+ messages in thread
From: Linus Walleij @ 2012-10-10  9:16 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: Linus Walleij, Ben Dooks, linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	Anmar Oueja, Patrice Chotard

On Sat, Oct 6, 2012 at 3:30 PM, Wolfram Sang <w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org> wrote:

> And it is also unneeded because of Ulf
> Hansson's patch?

Hm Ulf's patch is not even in linux-next? Where is this thing merged...

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] i2c: nomadik: adopt pinctrl support
       [not found]         ` <CACRpkdbdH6uLMt9oRTDEH3TBFppb2idtapHRKjoGtQ3cfvis2g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2012-10-10  9:27           ` Wolfram Sang
  0 siblings, 0 replies; 5+ messages in thread
From: Wolfram Sang @ 2012-10-10  9:27 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Linus Walleij, Ben Dooks, linux-i2c-u79uwXL29TY76Z2rM5mHXA,
	Anmar Oueja, Patrice Chotard

[-- Attachment #1: Type: text/plain, Size: 736 bytes --]

On Wed, Oct 10, 2012 at 11:16:45AM +0200, Linus Walleij wrote:
> On Sat, Oct 6, 2012 at 3:30 PM, Wolfram Sang <w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org> wrote:
> 
> > And it is also unneeded because of Ulf
> > Hansson's patch?
> 
> Hm Ulf's patch is not even in linux-next? Where is this thing merged...

It is not merged yet since I was waiting for an answer if my assumption
was right that Ulf's patch should go in first. I wasn't 100% sure. If
you could resend both patches based on the latest code that would be
much appreciated!

Thanks,

   Wolfram

-- 
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-10-10  9:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-28 12:59 [PATCH v2] i2c: nomadik: adopt pinctrl support Linus Walleij
     [not found] ` <1348837151-20820-1-git-send-email-linus.walleij-0IS4wlFg1OjSUeElwK9/Pw@public.gmane.org>
2012-10-06 13:30   ` Wolfram Sang
     [not found]     ` <20121006133006.GA25123-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2012-10-10  8:03       ` Linus Walleij
2012-10-10  9:16       ` Linus Walleij
     [not found]         ` <CACRpkdbdH6uLMt9oRTDEH3TBFppb2idtapHRKjoGtQ3cfvis2g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-10-10  9:27           ` Wolfram Sang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).