All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
@ 2016-09-20 14:40 Mika Westerberg
  2016-09-20 21:46 ` Linus Walleij
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Mika Westerberg @ 2016-09-20 14:40 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Linus Walleij, Andy Shevchenko, Mika Westerberg, linux-pwm

The PCA9685 controller has full on/off bit for each PWM channel. Setting
this bit bypasses the PWM control and the line works just as it would be a
GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
discreet muxes on the board.

This patch adds GPIO output only support for the driver so that we can
control the muxes on Galileo using standard GPIO interfaces available in
the kernel. GPIO and PWM functionality is exclusive so only one can be
active at a time on a single PWM channel.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
---
 drivers/pwm/pwm-pca9685.c | 158 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 157 insertions(+), 1 deletion(-)

diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c
index 117fccf7934a..f0c6131ccf33 100644
--- a/drivers/pwm/pwm-pca9685.c
+++ b/drivers/pwm/pwm-pca9685.c
@@ -20,8 +20,10 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/gpio/driver.h>
 #include <linux/i2c.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/pwm.h>
@@ -81,6 +83,10 @@ struct pca9685 {
 	int active_cnt;
 	int duty_ns;
 	int period_ns;
+#if IS_ENABLED(CONFIG_GPIOLIB)
+	struct mutex lock;
+	struct gpio_chip gpio;
+#endif
 };
 
 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
@@ -88,6 +94,149 @@ static inline struct pca9685 *to_pca(struct pwm_chip *chip)
 	return container_of(chip, struct pca9685, chip);
 }
 
+#if IS_ENABLED(CONFIG_GPIOLIB)
+static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
+{
+	struct pca9685 *pca = gpiochip_get_data(gpio);
+	struct pwm_device *pwm;
+
+	mutex_lock(&pca->lock);
+
+	pwm = &pca->chip.pwms[offset];
+	if (pwm->flags & (PWMF_REQUESTED | PWMF_EXPORTED)) {
+		mutex_unlock(&pca->lock);
+		return -EBUSY;
+	}
+	pwm_set_chip_data(pwm, (void *)1);
+
+	mutex_unlock(&pca->lock);
+	return 0;
+}
+
+static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
+{
+	struct pca9685 *pca = gpiochip_get_data(gpio);
+	struct pwm_device *pwm;
+
+	mutex_lock(&pca->lock);
+	pwm = &pca->chip.pwms[offset];
+	pwm_set_chip_data(pwm, NULL);
+	mutex_unlock(&pca->lock);
+}
+
+static bool pca9685_pwm_is_gpio(struct pca9685 *pca, struct pwm_device *pwm)
+{
+	bool is_gpio = false;
+
+	mutex_lock(&pca->lock);
+
+	if (pwm->hwpwm >= PCA9685_MAXCHAN) {
+		unsigned int i;
+
+		/*
+		 * Check if any of the GPIOs are requested and in that case
+		 * prevent using all LEDs channel.
+		 */
+		for (i = 0; i < pca->gpio.ngpio; i++)
+			if (gpiochip_is_requested(&pca->gpio, i)) {
+				is_gpio = true;
+				break;
+			}
+	} else if (pwm_get_chip_data(pwm)) {
+		is_gpio = true;
+	}
+
+	mutex_unlock(&pca->lock);
+	return is_gpio;
+}
+
+static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
+{
+	struct pca9685 *pca = gpiochip_get_data(gpio);
+	struct pwm_device *pwm = &pca->chip.pwms[offset];
+	unsigned int value;
+
+	regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value);
+	return value & LED_FULL;
+}
+
+static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
+				 int value)
+{
+	struct pca9685 *pca = gpiochip_get_data(gpio);
+	struct pwm_device *pwm = &pca->chip.pwms[offset];
+	unsigned int reg;
+
+	/* Clear both OFF registers */
+	reg = LED_N_OFF_L(pwm->hwpwm);
+	regmap_write(pca->regmap, reg, 0);
+	reg = LED_N_OFF_H(pwm->hwpwm);
+	regmap_write(pca->regmap, reg, 0);
+
+	/* Set the full ON bit */
+	reg = LED_N_ON_H(pwm->hwpwm);
+	regmap_write(pca->regmap, reg, value ? LED_FULL : 0);
+}
+
+static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
+					  unsigned int offset)
+{
+	/* Always out */
+	return 0;
+}
+
+static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
+					    unsigned int offset)
+{
+	return -EINVAL;
+}
+
+static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
+					     unsigned int offset, int value)
+{
+	pca9685_pwm_gpio_set(gpio, offset, value);
+	return 0;
+}
+
+/*
+ * The PCA9685 has a bit for turning the PWM output full off or on. Some
+ * boards like Intel Galileo actually uses these as normal GPIOs so we
+ * expose a GPIO chip here which can exclusively take over the underlying
+ * PWM channel.
+ */
+static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
+{
+	struct device *dev = pca->chip.dev;
+
+	mutex_init(&pca->lock);
+
+	pca->gpio.label = dev_name(dev);
+	pca->gpio.parent = dev;
+	pca->gpio.request = pca9685_pwm_gpio_request;
+	pca->gpio.free = pca9685_pwm_gpio_free;
+	pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
+	pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
+	pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
+	pca->gpio.get = pca9685_pwm_gpio_get;
+	pca->gpio.set = pca9685_pwm_gpio_set;
+	pca->gpio.base = -1;
+	pca->gpio.ngpio = PCA9685_MAXCHAN;
+	pca->gpio.can_sleep = true;
+
+	return devm_gpiochip_add_data(dev, &pca->gpio, pca);
+}
+#else
+static inline bool pca9685_pwm_is_gpio(struct pca9685 *pca,
+				       struct pwm_device *pwm)
+{
+	return false;
+}
+static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
+{
+	return 0;
+}
+#endif
+
 static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 			      int duty_ns, int period_ns)
 {
@@ -264,6 +413,9 @@ static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
 {
 	struct pca9685 *pca = to_pca(chip);
 
+	if (pca9685_pwm_is_gpio(pca, pwm))
+		return -EBUSY;
+
 	if (pca->active_cnt++ == 0)
 		return regmap_update_bits(pca->regmap, PCA9685_MODE1,
 					  MODE1_SLEEP, 0x0);
@@ -345,7 +497,11 @@ static int pca9685_pwm_probe(struct i2c_client *client,
 	pca->chip.base = -1;
 	pca->chip.can_sleep = true;
 
-	return pwmchip_add(&pca->chip);
+	ret = pwmchip_add(&pca->chip);
+	if (ret)
+		return ret;
+
+	return pca9685_pwm_gpio_probe(pca);
 }
 
 static int pca9685_pwm_remove(struct i2c_client *client)
-- 
2.9.3

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-09-20 14:40 [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO Mika Westerberg
@ 2016-09-20 21:46 ` Linus Walleij
  2016-10-19 18:56 ` Mika Westerberg
  2017-01-18 10:41 ` Thierry Reding
  2 siblings, 0 replies; 20+ messages in thread
From: Linus Walleij @ 2016-09-20 21:46 UTC (permalink / raw)
  To: Mika Westerberg; +Cc: Thierry Reding, Andy Shevchenko, linux-pwm

On Tue, Sep 20, 2016 at 4:40 PM, Mika Westerberg
<mika.westerberg@linux.intel.com> wrote:

> The PCA9685 controller has full on/off bit for each PWM channel. Setting
> this bit bypasses the PWM control and the line works just as it would be a
> GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
> discreet muxes on the board.
>
> This patch adds GPIO output only support for the driver so that we can
> control the muxes on Galileo using standard GPIO interfaces available in
> the kernel. GPIO and PWM functionality is exclusive so only one can be
> active at a time on a single PWM channel.
>
> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>

I had bad feelings about the patch, but:
Actually, I have nothing against it.
I think it's a neat hack.

Acked-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-09-20 14:40 [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO Mika Westerberg
  2016-09-20 21:46 ` Linus Walleij
@ 2016-10-19 18:56 ` Mika Westerberg
  2016-10-19 20:05   ` Clemens Gruber
  2016-10-20 10:45   ` Thierry Reding
  2017-01-18 10:41 ` Thierry Reding
  2 siblings, 2 replies; 20+ messages in thread
From: Mika Westerberg @ 2016-10-19 18:56 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Linus Walleij, Andy Shevchenko, linux-pwm

On Tue, Sep 20, 2016 at 05:40:56PM +0300, Mika Westerberg wrote:
> The PCA9685 controller has full on/off bit for each PWM channel. Setting
> this bit bypasses the PWM control and the line works just as it would be a
> GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
> discreet muxes on the board.
> 
> This patch adds GPIO output only support for the driver so that we can
> control the muxes on Galileo using standard GPIO interfaces available in
> the kernel. GPIO and PWM functionality is exclusive so only one can be
> active at a time on a single PWM channel.
> 
> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>

Thierry,

Any comments on this?

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-19 18:56 ` Mika Westerberg
@ 2016-10-19 20:05   ` Clemens Gruber
  2016-10-19 20:28     ` Mika Westerberg
  2016-10-20 10:45   ` Thierry Reding
  1 sibling, 1 reply; 20+ messages in thread
From: Clemens Gruber @ 2016-10-19 20:05 UTC (permalink / raw)
  To: Mika Westerberg; +Cc: Thierry Reding, Linus Walleij, Andy Shevchenko, linux-pwm

> On Tue, Sep 20, 2016 at 05:40:56PM +0300, Mika Westerberg wrote:
> > The PCA9685 controller has full on/off bit for each PWM channel. Setting
> > this bit bypasses the PWM control and the line works just as it would be a
> > GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
> > discreet muxes on the board.
> > 
> > This patch adds GPIO output only support for the driver so that we can
> > control the muxes on Galileo using standard GPIO interfaces available in
> > the kernel. GPIO and PWM functionality is exclusive so only one can be
> > active at a time on a single PWM channel.
> > 
> > Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>

Hi Mika,

what do you think about implementing this on a higher level / in the pwm
core instead of adding it to one specific pwm driver?

The fact that the PCA9685 bypasses PWM control if the full on/off bits
are set is beneficial but not a requirement to work as a GPIO.
Every pwm output could act as GPIO if the duty cycle is set to 0%/100%.

The pwm-pca9685 driver does already clear both OFF registers and sets
the ON register depending upon the duty cycle being at 0% or 100%.
So it might work without making any changes to specific pwm drivers and
it would then be available for all pwm chips.

Regards,
Clemens

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-19 20:05   ` Clemens Gruber
@ 2016-10-19 20:28     ` Mika Westerberg
  2016-10-19 22:59       ` Clemens Gruber
  0 siblings, 1 reply; 20+ messages in thread
From: Mika Westerberg @ 2016-10-19 20:28 UTC (permalink / raw)
  To: Clemens Gruber; +Cc: Thierry Reding, Linus Walleij, Andy Shevchenko, linux-pwm

On Wed, Oct 19, 2016 at 10:05:33PM +0200, Clemens Gruber wrote:
> > On Tue, Sep 20, 2016 at 05:40:56PM +0300, Mika Westerberg wrote:
> > > The PCA9685 controller has full on/off bit for each PWM channel. Setting
> > > this bit bypasses the PWM control and the line works just as it would be a
> > > GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
> > > discreet muxes on the board.
> > > 
> > > This patch adds GPIO output only support for the driver so that we can
> > > control the muxes on Galileo using standard GPIO interfaces available in
> > > the kernel. GPIO and PWM functionality is exclusive so only one can be
> > > active at a time on a single PWM channel.
> > > 
> > > Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> 
> Hi Mika,
> 
> what do you think about implementing this on a higher level / in the pwm
> core instead of adding it to one specific pwm driver?

I originally thought about it but since there is only one driver I'm
aware of that actually needs this functionality (because of how Galileo
uses the PWM outputs), I decided to add it only to this driver.

> The fact that the PCA9685 bypasses PWM control if the full on/off bits
> are set is beneficial but not a requirement to work as a GPIO.
> Every pwm output could act as GPIO if the duty cycle is set to 0%/100%.

Yes, that's right.

> The pwm-pca9685 driver does already clear both OFF registers and sets
> the ON register depending upon the duty cycle being at 0% or 100%.
> So it might work without making any changes to specific pwm drivers and
> it would then be available for all pwm chips.

I'm fine adding it to PWM core instead if Thierry does not have
objections. However, do you think there is actual use for this outside
of Galileo?

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-19 20:28     ` Mika Westerberg
@ 2016-10-19 22:59       ` Clemens Gruber
  2016-10-20  8:07         ` Mika Westerberg
  0 siblings, 1 reply; 20+ messages in thread
From: Clemens Gruber @ 2016-10-19 22:59 UTC (permalink / raw)
  To: Mika Westerberg; +Cc: Thierry Reding, Linus Walleij, Andy Shevchenko, linux-pwm

On Wed, Oct 19, 2016 at 11:28:27PM +0300, Mika Westerberg wrote:
> On Wed, Oct 19, 2016 at 10:05:33PM +0200, Clemens Gruber wrote:
> > Hi Mika,
> > 
> > what do you think about implementing this on a higher level / in the pwm
> > core instead of adding it to one specific pwm driver?
> 
> I originally thought about it but since there is only one driver I'm
> aware of that actually needs this functionality (because of how Galileo
> uses the PWM outputs), I decided to add it only to this driver.
> 
> > The fact that the PCA9685 bypasses PWM control if the full on/off bits
> > are set is beneficial but not a requirement to work as a GPIO.
> > Every pwm output could act as GPIO if the duty cycle is set to 0%/100%.
> 
> Yes, that's right.
> 
> > The pwm-pca9685 driver does already clear both OFF registers and sets
> > the ON register depending upon the duty cycle being at 0% or 100%.
> > So it might work without making any changes to specific pwm drivers and
> > it would then be available for all pwm chips.
> 
> I'm fine adding it to PWM core instead if Thierry does not have
> objections. However, do you think there is actual use for this outside
> of Galileo?

It could be useful for boards that have a PWM chip but only need a few
of its PWM channels for PWM output. Then they could use the others as
GPOs. This issue has not come up in any of my projects yet, but I just
found out that this topic did come up once before, about 4 years ago:
http://www.spinics.net/lists/linux-omap/msg82913.html

So there are (at least) two problems if we move it to pwm core:

1) Which period to use?

If we can't get a default period from the hardware or the driver, do we
need something like the following in the devicetree:

mypwmgpios: pwm-as-gpio {
	compatible = "pwm-as-gpio";
	pwms = <&pwmchip1 0 PERIOD>, <&pwmchip1 1 PERIOD>;
};

And then use it as follows:
gpio-foo = <&mypwmgpios 0 GPIO_ACTIVE_HIGH>;
gpio-bar = <&mypwmgpios 1 GPIO_ACTIVE_HIGH>;

?


2) Are there PWM chips that toggle one time before keeping the level?

This would be pretty bad. One possibility would be a blacklist for the
known pwm chips with this behavior?


The more I think about it, the more I like your pca9685-specific
solution, where we can use: gpio-foo = <&pwmchip1 0 GPIO_ACTIVE_HIGH>;
And the period is not even needed for the PCA9685 because the PWM
control is elegantly bypassed.

Maybe there is a way to keep it as simple to use in the devicetree and
allow other PWM drivers to use it iff they have a known default period
and do not toggle? So the driver tells the pwm core the default period
and if he can act as gpio controller, but the pwm core creates it?

Regards,
Clemens

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-19 22:59       ` Clemens Gruber
@ 2016-10-20  8:07         ` Mika Westerberg
  0 siblings, 0 replies; 20+ messages in thread
From: Mika Westerberg @ 2016-10-20  8:07 UTC (permalink / raw)
  To: Clemens Gruber; +Cc: Thierry Reding, Linus Walleij, Andy Shevchenko, linux-pwm

On Thu, Oct 20, 2016 at 12:59:02AM +0200, Clemens Gruber wrote:
> It could be useful for boards that have a PWM chip but only need a few
> of its PWM channels for PWM output. Then they could use the others as
> GPOs. This issue has not come up in any of my projects yet, but I just
> found out that this topic did come up once before, about 4 years ago:
> http://www.spinics.net/lists/linux-omap/msg82913.html
> 
> So there are (at least) two problems if we move it to pwm core:
> 
> 1) Which period to use?
> 
> If we can't get a default period from the hardware or the driver, do we
> need something like the following in the devicetree:
> 
> mypwmgpios: pwm-as-gpio {
> 	compatible = "pwm-as-gpio";
> 	pwms = <&pwmchip1 0 PERIOD>, <&pwmchip1 1 PERIOD>;
> };
> 
> And then use it as follows:
> gpio-foo = <&mypwmgpios 0 GPIO_ACTIVE_HIGH>;
> gpio-bar = <&mypwmgpios 1 GPIO_ACTIVE_HIGH>;
> 
> ?

How about adding a callback to struct pwm_ops: gpio_set() and if that is
implemented by the driver, the core will export a GPIO chip and expects
the driver to be able to set the default period (or better override the
whole PWM functionality as PCA9685 can do).

That also is not tied to DT (Galileo is an ACPI system).

> 2) Are there PWM chips that toggle one time before keeping the level?
> 
> This would be pretty bad. One possibility would be a blacklist for the
> known pwm chips with this behavior?

..or do the above and let each driver to implement that hook only if it
is actually capable of doing that.

> The more I think about it, the more I like your pca9685-specific
> solution, where we can use: gpio-foo = <&pwmchip1 0 GPIO_ACTIVE_HIGH>;
> And the period is not even needed for the PCA9685 because the PWM
> control is elegantly bypassed.

Indeed :)

> Maybe there is a way to keep it as simple to use in the devicetree and
> allow other PWM drivers to use it iff they have a known default period
> and do not toggle? So the driver tells the pwm core the default period
> and if he can act as gpio controller, but the pwm core creates it?

Unfortunately we can't use DT in Galileo but that gpio_set() callback, I
suggest above, should work regardless of the underlying firmware
interface.

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-19 18:56 ` Mika Westerberg
  2016-10-19 20:05   ` Clemens Gruber
@ 2016-10-20 10:45   ` Thierry Reding
  2016-10-20 11:18     ` Mika Westerberg
  2016-10-23 10:23     ` Linus Walleij
  1 sibling, 2 replies; 20+ messages in thread
From: Thierry Reding @ 2016-10-20 10:45 UTC (permalink / raw)
  To: Mika Westerberg; +Cc: Linus Walleij, Andy Shevchenko, linux-pwm

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

On Wed, Oct 19, 2016 at 09:56:38PM +0300, Mika Westerberg wrote:
> On Tue, Sep 20, 2016 at 05:40:56PM +0300, Mika Westerberg wrote:
> > The PCA9685 controller has full on/off bit for each PWM channel. Setting
> > this bit bypasses the PWM control and the line works just as it would be a
> > GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
> > discreet muxes on the board.
> > 
> > This patch adds GPIO output only support for the driver so that we can
> > control the muxes on Galileo using standard GPIO interfaces available in
> > the kernel. GPIO and PWM functionality is exclusive so only one can be
> > active at a time on a single PWM channel.
> > 
> > Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> 
> Thierry,
> 
> Any comments on this?

It seems to me like maybe pinmux would be a better framework to handle
this kind of use case. The full on/off bit sounds like it's a mux that
selects between GPIO and PWM functionality.

Have you thought about supporting pinmux for this?

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-20 10:45   ` Thierry Reding
@ 2016-10-20 11:18     ` Mika Westerberg
  2016-10-20 12:51       ` Thierry Reding
  2016-10-23 10:23     ` Linus Walleij
  1 sibling, 1 reply; 20+ messages in thread
From: Mika Westerberg @ 2016-10-20 11:18 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Linus Walleij, Andy Shevchenko, linux-pwm

On Thu, Oct 20, 2016 at 12:45:41PM +0200, Thierry Reding wrote:
> On Wed, Oct 19, 2016 at 09:56:38PM +0300, Mika Westerberg wrote:
> > On Tue, Sep 20, 2016 at 05:40:56PM +0300, Mika Westerberg wrote:
> > > The PCA9685 controller has full on/off bit for each PWM channel. Setting
> > > this bit bypasses the PWM control and the line works just as it would be a
> > > GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
> > > discreet muxes on the board.
> > > 
> > > This patch adds GPIO output only support for the driver so that we can
> > > control the muxes on Galileo using standard GPIO interfaces available in
> > > the kernel. GPIO and PWM functionality is exclusive so only one can be
> > > active at a time on a single PWM channel.
> > > 
> > > Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> > 
> > Thierry,
> > 
> > Any comments on this?
> 
> It seems to me like maybe pinmux would be a better framework to handle
> this kind of use case. The full on/off bit sounds like it's a mux that
> selects between GPIO and PWM functionality.
> 
> Have you thought about supporting pinmux for this?

Adding a full pinmux just for this sounds a bit overkill if you ask me.

How about adding that ->gpio_set() callback in pwm_ops and let the PWM
core to export a GPIO chip in that case? That would support also other
PWMs having similar full on/off capability without the need to add a new
pinmux driver for each.

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-20 11:18     ` Mika Westerberg
@ 2016-10-20 12:51       ` Thierry Reding
  2016-10-20 12:56         ` Andy Shevchenko
                           ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Thierry Reding @ 2016-10-20 12:51 UTC (permalink / raw)
  To: Mika Westerberg; +Cc: Linus Walleij, Andy Shevchenko, linux-pwm

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

On Thu, Oct 20, 2016 at 02:18:44PM +0300, Mika Westerberg wrote:
> On Thu, Oct 20, 2016 at 12:45:41PM +0200, Thierry Reding wrote:
> > On Wed, Oct 19, 2016 at 09:56:38PM +0300, Mika Westerberg wrote:
> > > On Tue, Sep 20, 2016 at 05:40:56PM +0300, Mika Westerberg wrote:
> > > > The PCA9685 controller has full on/off bit for each PWM channel. Setting
> > > > this bit bypasses the PWM control and the line works just as it would be a
> > > > GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
> > > > discreet muxes on the board.
> > > > 
> > > > This patch adds GPIO output only support for the driver so that we can
> > > > control the muxes on Galileo using standard GPIO interfaces available in
> > > > the kernel. GPIO and PWM functionality is exclusive so only one can be
> > > > active at a time on a single PWM channel.
> > > > 
> > > > Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> > > 
> > > Thierry,
> > > 
> > > Any comments on this?
> > 
> > It seems to me like maybe pinmux would be a better framework to handle
> > this kind of use case. The full on/off bit sounds like it's a mux that
> > selects between GPIO and PWM functionality.
> > 
> > Have you thought about supporting pinmux for this?
> 
> Adding a full pinmux just for this sounds a bit overkill if you ask me.
> 
> How about adding that ->gpio_set() callback in pwm_ops and let the PWM
> core to export a GPIO chip in that case? That would support also other
> PWMs having similar full on/off capability without the need to add a new
> pinmux driver for each.

I'm reluctant to add this to the PWM core because it's effectively
duplicating something for which a proper subsystem already exists.

If adding pinmux is considered overkill, maybe doing so needs to be
simplified. Surely if this is applicable to more than one PWM controller
some helpers could be extracted to make it easier to add.

But if it isn't generally useful I don't think it makes sense to add it
to the PWM core either. So I think our choices here are to register via
pinmux and in the process add helpers to make that easier (remove the
overkill) or to keep this to the driver until we start seeing a pattern
emerge.

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-20 12:51       ` Thierry Reding
@ 2016-10-20 12:56         ` Andy Shevchenko
  2016-10-20 13:01         ` Mika Westerberg
  2016-10-20 13:08         ` Clemens Gruber
  2 siblings, 0 replies; 20+ messages in thread
From: Andy Shevchenko @ 2016-10-20 12:56 UTC (permalink / raw)
  To: Thierry Reding, Mika Westerberg; +Cc: Linus Walleij, linux-pwm

On Thu, 2016-10-20 at 14:51 +0200, Thierry Reding wrote:
> On Thu, Oct 20, 2016 at 02:18:44PM +0300, Mika Westerberg wrote:
> > On Thu, Oct 20, 2016 at 12:45:41PM +0200, Thierry Reding wrote:
> > > On Wed, Oct 19, 2016 at 09:56:38PM +0300, Mika Westerberg wrote:
> > > > On Tue, Sep 20, 2016 at 05:40:56PM +0300, Mika Westerberg wrote:

> I'm reluctant to add this to the PWM core because it's effectively
> duplicating something for which a proper subsystem already exists.
> 
> If adding pinmux is considered overkill, maybe doing so needs to be
> simplified. Surely if this is applicable to more than one PWM
> controller
> some helpers could be extracted to make it easier to add.
> 
> But if it isn't generally useful I don't think it makes sense to add
> it
> to the PWM core either. So I think our choices here are to register
> via
> pinmux and in the process add helpers to make that easier (remove the
> overkill) or to keep this to the driver until we start seeing a
> pattern
> emerge.

I vote for the latter since we have only one user for that and that one
is not so demonstrative (it is clean GPIO/PWM case, when others might
require GPIO emulation).

-- 
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Intel Finland Oy

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-20 12:51       ` Thierry Reding
  2016-10-20 12:56         ` Andy Shevchenko
@ 2016-10-20 13:01         ` Mika Westerberg
  2016-10-20 21:50           ` Thierry Reding
  2016-10-20 13:08         ` Clemens Gruber
  2 siblings, 1 reply; 20+ messages in thread
From: Mika Westerberg @ 2016-10-20 13:01 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Linus Walleij, Andy Shevchenko, linux-pwm

On Thu, Oct 20, 2016 at 02:51:44PM +0200, Thierry Reding wrote:
> On Thu, Oct 20, 2016 at 02:18:44PM +0300, Mika Westerberg wrote:
> > On Thu, Oct 20, 2016 at 12:45:41PM +0200, Thierry Reding wrote:
> > > On Wed, Oct 19, 2016 at 09:56:38PM +0300, Mika Westerberg wrote:
> > > > On Tue, Sep 20, 2016 at 05:40:56PM +0300, Mika Westerberg wrote:
> > > > > The PCA9685 controller has full on/off bit for each PWM channel. Setting
> > > > > this bit bypasses the PWM control and the line works just as it would be a
> > > > > GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
> > > > > discreet muxes on the board.
> > > > > 
> > > > > This patch adds GPIO output only support for the driver so that we can
> > > > > control the muxes on Galileo using standard GPIO interfaces available in
> > > > > the kernel. GPIO and PWM functionality is exclusive so only one can be
> > > > > active at a time on a single PWM channel.
> > > > > 
> > > > > Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> > > > 
> > > > Thierry,
> > > > 
> > > > Any comments on this?
> > > 
> > > It seems to me like maybe pinmux would be a better framework to handle
> > > this kind of use case. The full on/off bit sounds like it's a mux that
> > > selects between GPIO and PWM functionality.
> > > 
> > > Have you thought about supporting pinmux for this?
> > 
> > Adding a full pinmux just for this sounds a bit overkill if you ask me.
> > 
> > How about adding that ->gpio_set() callback in pwm_ops and let the PWM
> > core to export a GPIO chip in that case? That would support also other
> > PWMs having similar full on/off capability without the need to add a new
> > pinmux driver for each.
> 
> I'm reluctant to add this to the PWM core because it's effectively
> duplicating something for which a proper subsystem already exists.

OK, I see.

> If adding pinmux is considered overkill, maybe doing so needs to be
> simplified. Surely if this is applicable to more than one PWM controller
> some helpers could be extracted to make it easier to add.
> 
> But if it isn't generally useful I don't think it makes sense to add it
> to the PWM core either. So I think our choices here are to register via
> pinmux and in the process add helpers to make that easier (remove the
> overkill) or to keep this to the driver until we start seeing a pattern
> emerge.

I would then go for keeping the code in this single driver for now and
re-think this later if other drivers start to develop similar.

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-20 12:51       ` Thierry Reding
  2016-10-20 12:56         ` Andy Shevchenko
  2016-10-20 13:01         ` Mika Westerberg
@ 2016-10-20 13:08         ` Clemens Gruber
  2016-10-20 21:52           ` Thierry Reding
  2 siblings, 1 reply; 20+ messages in thread
From: Clemens Gruber @ 2016-10-20 13:08 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Mika Westerberg, Linus Walleij, Andy Shevchenko, linux-pwm

On Thu, Oct 20, 2016 at 02:51:44PM +0200, Thierry Reding wrote:
> I'm reluctant to add this to the PWM core because it's effectively
> duplicating something for which a proper subsystem already exists.
> 
> If adding pinmux is considered overkill, maybe doing so needs to be
> simplified. Surely if this is applicable to more than one PWM controller
> some helpers could be extracted to make it easier to add.
> 
> But if it isn't generally useful I don't think it makes sense to add it
> to the PWM core either. So I think our choices here are to register via
> pinmux and in the process add helpers to make that easier (remove the
> overkill) or to keep this to the driver until we start seeing a pattern
> emerge.

But the fact that this one pwm chip bypasses PWM control and acts as a
GPO is an implementation detail of the chip, this feature is helpful for
other PWM chips too, as long as they don't toggle.
Would this really be a good fit in the pinmux subsystem?

I really like the idea of Mika to add a gpio_set callback in pwm_ops and
let the pwm core export a GPIO chip. For now we could add this callback
only to the pwm-pca9685 driver but others could opt-in if needed.

But the initial patch from Mika is fine too, in my opinion.

Regards,
Clemens

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-20 13:01         ` Mika Westerberg
@ 2016-10-20 21:50           ` Thierry Reding
  2016-10-21 12:23             ` Mika Westerberg
  0 siblings, 1 reply; 20+ messages in thread
From: Thierry Reding @ 2016-10-20 21:50 UTC (permalink / raw)
  To: Mika Westerberg; +Cc: Linus Walleij, Andy Shevchenko, linux-pwm

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

On Thu, Oct 20, 2016 at 04:01:17PM +0300, Mika Westerberg wrote:
> On Thu, Oct 20, 2016 at 02:51:44PM +0200, Thierry Reding wrote:
> > On Thu, Oct 20, 2016 at 02:18:44PM +0300, Mika Westerberg wrote:
> > > On Thu, Oct 20, 2016 at 12:45:41PM +0200, Thierry Reding wrote:
> > > > On Wed, Oct 19, 2016 at 09:56:38PM +0300, Mika Westerberg wrote:
> > > > > On Tue, Sep 20, 2016 at 05:40:56PM +0300, Mika Westerberg wrote:
> > > > > > The PCA9685 controller has full on/off bit for each PWM channel. Setting
> > > > > > this bit bypasses the PWM control and the line works just as it would be a
> > > > > > GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
> > > > > > discreet muxes on the board.
> > > > > > 
> > > > > > This patch adds GPIO output only support for the driver so that we can
> > > > > > control the muxes on Galileo using standard GPIO interfaces available in
> > > > > > the kernel. GPIO and PWM functionality is exclusive so only one can be
> > > > > > active at a time on a single PWM channel.
> > > > > > 
> > > > > > Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> > > > > 
> > > > > Thierry,
> > > > > 
> > > > > Any comments on this?
> > > > 
> > > > It seems to me like maybe pinmux would be a better framework to handle
> > > > this kind of use case. The full on/off bit sounds like it's a mux that
> > > > selects between GPIO and PWM functionality.
> > > > 
> > > > Have you thought about supporting pinmux for this?
> > > 
> > > Adding a full pinmux just for this sounds a bit overkill if you ask me.
> > > 
> > > How about adding that ->gpio_set() callback in pwm_ops and let the PWM
> > > core to export a GPIO chip in that case? That would support also other
> > > PWMs having similar full on/off capability without the need to add a new
> > > pinmux driver for each.
> > 
> > I'm reluctant to add this to the PWM core because it's effectively
> > duplicating something for which a proper subsystem already exists.
> 
> OK, I see.
> 
> > If adding pinmux is considered overkill, maybe doing so needs to be
> > simplified. Surely if this is applicable to more than one PWM controller
> > some helpers could be extracted to make it easier to add.
> > 
> > But if it isn't generally useful I don't think it makes sense to add it
> > to the PWM core either. So I think our choices here are to register via
> > pinmux and in the process add helpers to make that easier (remove the
> > overkill) or to keep this to the driver until we start seeing a pattern
> > emerge.
> 
> I would then go for keeping the code in this single driver for now and
> re-think this later if other drivers start to develop similar.

That's fine with me. I'll take a closer look at this patch and apply if
I don't find anything out of place.

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-20 13:08         ` Clemens Gruber
@ 2016-10-20 21:52           ` Thierry Reding
  0 siblings, 0 replies; 20+ messages in thread
From: Thierry Reding @ 2016-10-20 21:52 UTC (permalink / raw)
  To: Clemens Gruber; +Cc: Mika Westerberg, Linus Walleij, Andy Shevchenko, linux-pwm

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

On Thu, Oct 20, 2016 at 03:08:53PM +0200, Clemens Gruber wrote:
> On Thu, Oct 20, 2016 at 02:51:44PM +0200, Thierry Reding wrote:
> > I'm reluctant to add this to the PWM core because it's effectively
> > duplicating something for which a proper subsystem already exists.
> > 
> > If adding pinmux is considered overkill, maybe doing so needs to be
> > simplified. Surely if this is applicable to more than one PWM controller
> > some helpers could be extracted to make it easier to add.
> > 
> > But if it isn't generally useful I don't think it makes sense to add it
> > to the PWM core either. So I think our choices here are to register via
> > pinmux and in the process add helpers to make that easier (remove the
> > overkill) or to keep this to the driver until we start seeing a pattern
> > emerge.
> 
> But the fact that this one pwm chip bypasses PWM control and acts as a
> GPO is an implementation detail of the chip, this feature is helpful for
> other PWM chips too, as long as they don't toggle.
> Would this really be a good fit in the pinmux subsystem?
> 
> I really like the idea of Mika to add a gpio_set callback in pwm_ops and
> let the pwm core export a GPIO chip. For now we could add this callback
> only to the pwm-pca9685 driver but others could opt-in if needed.

I don't think it's as simple as that. There could be chips where only a
subset of PWM channels could act as a GPIO, or there could be any other
amount of quirks. A single callback is unlikely to cut it, so I think a
better alternative is to keep this in drivers until some patterns start
to emerge.

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-20 21:50           ` Thierry Reding
@ 2016-10-21 12:23             ` Mika Westerberg
  2016-10-31 11:42               ` Mika Westerberg
  0 siblings, 1 reply; 20+ messages in thread
From: Mika Westerberg @ 2016-10-21 12:23 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Linus Walleij, Andy Shevchenko, linux-pwm

On Thu, Oct 20, 2016 at 11:50:11PM +0200, Thierry Reding wrote:
> > I would then go for keeping the code in this single driver for now and
> > re-think this later if other drivers start to develop similar.
> 
> That's fine with me. I'll take a closer look at this patch and apply if
> I don't find anything out of place.

Thanks!

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-20 10:45   ` Thierry Reding
  2016-10-20 11:18     ` Mika Westerberg
@ 2016-10-23 10:23     ` Linus Walleij
  1 sibling, 0 replies; 20+ messages in thread
From: Linus Walleij @ 2016-10-23 10:23 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Mika Westerberg, Andy Shevchenko, linux-pwm

On Thu, Oct 20, 2016 at 12:45 PM, Thierry Reding
<thierry.reding@gmail.com> wrote:

> It seems to me like maybe pinmux would be a better framework to handle
> this kind of use case. The full on/off bit sounds like it's a mux that
> selects between GPIO and PWM functionality.

Muxing is mostly selecting between two or more hardware blocks to be
routed to one and the same pin. Like if there was a GPIO block and a PWM
block and either could be routed to the pin.

So selecting between two physical drivers behind the pin.

What you are describing seems more like muxing two different usecases
on the same PWM hardware, I don't think that is appropriate for pin
multiplexing.

I would more say that if the PWM is used as GPIO then it is a neat hack
with a PWM set to duty cycle 100% or 0% and as such should be a
GPIO driver on top of the PWM. Supported in the core as described
in the other responses.

Yours,
Linus Walleij

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-21 12:23             ` Mika Westerberg
@ 2016-10-31 11:42               ` Mika Westerberg
  2016-12-01  7:28                 ` Mika Westerberg
  0 siblings, 1 reply; 20+ messages in thread
From: Mika Westerberg @ 2016-10-31 11:42 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Linus Walleij, Andy Shevchenko, linux-pwm

On Fri, Oct 21, 2016 at 03:23:41PM +0300, Mika Westerberg wrote:
> On Thu, Oct 20, 2016 at 11:50:11PM +0200, Thierry Reding wrote:
> > > I would then go for keeping the code in this single driver for now and
> > > re-think this later if other drivers start to develop similar.
> > 
> > That's fine with me. I'll take a closer look at this patch and apply if
> > I don't find anything out of place.
> 
> Thanks!

Hi Thierry,

Did you get a chance to look at the patch?

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-10-31 11:42               ` Mika Westerberg
@ 2016-12-01  7:28                 ` Mika Westerberg
  0 siblings, 0 replies; 20+ messages in thread
From: Mika Westerberg @ 2016-12-01  7:28 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Linus Walleij, Andy Shevchenko, linux-pwm

On Mon, Oct 31, 2016 at 01:42:56PM +0200, Mika Westerberg wrote:
> On Fri, Oct 21, 2016 at 03:23:41PM +0300, Mika Westerberg wrote:
> > On Thu, Oct 20, 2016 at 11:50:11PM +0200, Thierry Reding wrote:
> > > > I would then go for keeping the code in this single driver for now and
> > > > re-think this later if other drivers start to develop similar.
> > > 
> > > That's fine with me. I'll take a closer look at this patch and apply if
> > > I don't find anything out of place.
> > 
> > Thanks!
> 
> Hi Thierry,
> 
> Did you get a chance to look at the patch?

Just a friendly reminder as we are getting closer to v4.9 release and
merge window opens soon.

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

* Re: [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO
  2016-09-20 14:40 [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO Mika Westerberg
  2016-09-20 21:46 ` Linus Walleij
  2016-10-19 18:56 ` Mika Westerberg
@ 2017-01-18 10:41 ` Thierry Reding
  2 siblings, 0 replies; 20+ messages in thread
From: Thierry Reding @ 2017-01-18 10:41 UTC (permalink / raw)
  To: Mika Westerberg; +Cc: Linus Walleij, Andy Shevchenko, linux-pwm

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

On Tue, Sep 20, 2016 at 05:40:56PM +0300, Mika Westerberg wrote:
> The PCA9685 controller has full on/off bit for each PWM channel. Setting
> this bit bypasses the PWM control and the line works just as it would be a
> GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for
> discreet muxes on the board.
> 
> This patch adds GPIO output only support for the driver so that we can
> control the muxes on Galileo using standard GPIO interfaces available in
> the kernel. GPIO and PWM functionality is exclusive so only one can be
> active at a time on a single PWM channel.
> 
> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> ---
>  drivers/pwm/pwm-pca9685.c | 158 +++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 157 insertions(+), 1 deletion(-)

I've applied this with some minor stylistic fixups and removing the
PWM chip if the GPIO chip fails to register.

Please shout if that's not okay.

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

end of thread, other threads:[~2017-01-18 10:49 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-20 14:40 [PATCH] pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO Mika Westerberg
2016-09-20 21:46 ` Linus Walleij
2016-10-19 18:56 ` Mika Westerberg
2016-10-19 20:05   ` Clemens Gruber
2016-10-19 20:28     ` Mika Westerberg
2016-10-19 22:59       ` Clemens Gruber
2016-10-20  8:07         ` Mika Westerberg
2016-10-20 10:45   ` Thierry Reding
2016-10-20 11:18     ` Mika Westerberg
2016-10-20 12:51       ` Thierry Reding
2016-10-20 12:56         ` Andy Shevchenko
2016-10-20 13:01         ` Mika Westerberg
2016-10-20 21:50           ` Thierry Reding
2016-10-21 12:23             ` Mika Westerberg
2016-10-31 11:42               ` Mika Westerberg
2016-12-01  7:28                 ` Mika Westerberg
2016-10-20 13:08         ` Clemens Gruber
2016-10-20 21:52           ` Thierry Reding
2016-10-23 10:23     ` Linus Walleij
2017-01-18 10:41 ` Thierry Reding

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.