Skip to contentSkip to conversation

pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO

PATCH In review Mika Westerberg 5 participants 20 messages Sep 20, 2016 – Jan 18, 2017 PWM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pwm-pca9685: Allow any of the 16 PWMs to be used as a GPIO

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.

---
 drivers/pwm/pwm-pca9685.c | 158 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 157 insertions(+), 1 deletion(-)
drivers/pwm/pwm-pca9685.c +157 -1
index 117fccf7934a..f0c6131ccf33 100644
@@ -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)
Signature · 1 line
2.9.3
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Mika Westerberg

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.

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

Yours,
Linus Walleij

#2 lore raw
In reply to Mika Westerberg
Quoted 11 lines · Mika Westerberg

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.

Thierry,

Any comments on this?

#3 lore raw
Quoted 6 lines · Mika Westerberg
Quoted 5 lines

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.

Quoted 6 lines
Quoted 6 lines

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.

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

#4 lore raw
Quoted 11 lines · Clemens Gruber
Quoted 11 lines · Mika Westerberg
Quoted 10 lines

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.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

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.

Quoted 3 lines

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.

Quoted 4 lines

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?

#5 lore raw
Quoted 16 lines · Mika Westerberg
Quoted 4 lines · Clemens Gruber

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.

Quoted 3 lines

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.

Quoted 8 lines
Quoted 4 lines

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

#6 lore raw
Quoted 18 lines · Clemens Gruber

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>;
};
Quoted 5 lines

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).

Quoted 4 lines

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.

Quoted 4 lines

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 :)

Quoted 4 lines

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.

#7 lore raw
In reply to Mika Westerberg
Quoted 11 lines · Mika Westerberg
Quoted 10 lines · Mika Westerberg

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.

Quoted 5 lines
Quoted 1 line

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

#8 lore raw
Quoted 16 lines · Thierry Reding
Quoted 15 lines · Mika Westerberg
Quoted 11 lines · Mika Westerberg

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.

Thierry,

Quoted 7 lines
Quoted 1 line

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.

#9 lore raw
Quoted 23 lines · Mika Westerberg
Quoted 22 lines · Thierry Reding
Quoted 11 lines · Mika Westerberg
Quoted 10 lines · Mika Westerberg

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.

Quoted 5 lines
Quoted 1 line

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.

Quoted 8 lines
Quoted 1 line

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

#10 lore raw
Quoted 13 lines · Thierry Reding
Quoted 3 lines · Mika Westerberg
Quoted 2 lines · Thierry Reding
Quoted 1 line · Mika Westerberg

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.

Quoted 8 lines

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).

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#11 lore raw
In reply to Thierry Reding
Quoted 28 lines · Thierry Reding
Quoted 27 lines · Mika Westerberg
Quoted 16 lines · Thierry Reding
Quoted 15 lines · Mika Westerberg
Quoted 11 lines · Mika Westerberg

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.

Thierry,

Quoted 7 lines
Quoted 1 line

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.

Quoted 7 lines
Quoted 4 lines

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.

Quoted 9 lines

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.

#12 lore raw
Quoted 43 lines · Mika Westerberg
Quoted 28 lines · Thierry Reding
Quoted 27 lines · Mika Westerberg
Quoted 16 lines · Thierry Reding
Quoted 15 lines · Mika Westerberg
Quoted 11 lines · Mika Westerberg

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.

Thierry,

Quoted 7 lines
Quoted 1 line

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.

Quoted 7 lines
Quoted 4 lines

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.

Quoted 4 lines

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.

Quoted 8 lines
Quoted 5 lines

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

#13 lore raw
Quoted 5 lines · Thierry Reding
Quoted 2 lines

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!

#14 lore raw
Quoted 8 lines · Mika Westerberg
Quoted 5 lines · Thierry Reding
Quoted 2 lines

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?

#15 lore raw
Quoted 5 lines · Mika Westerberg
Quoted 4 lines · Mika Westerberg
Quoted 3 lines · Thierry Reding
Quoted 2 lines

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.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

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.

#16 lore raw
In reply to Thierry Reding
Quoted 7 lines · Thierry Reding

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.

Quoted 5 lines

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

#17 lore raw
Quoted 14 lines · Clemens Gruber
Quoted 7 lines · Thierry Reding

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.

Quoted 5 lines

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.

Quoted 8 lines

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

#18 lore raw
In reply to Thierry Reding
Quoted 3 lines · Thierry Reding

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

#19 lore raw
In reply to Mika Westerberg
Quoted 10 lines · Mika Westerberg

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.

Quoted 4 lines
---
 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