All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH v4 1/4] power: regulator: pwm: support pwm polarity setting
@ 2017-04-24  2:27 Kever Yang
  2017-04-24  2:27 ` [U-Boot] [PATCH v4 2/4] rockchip: pwm: implement pwm_set_invert() Kever Yang
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Kever Yang @ 2017-04-24  2:27 UTC (permalink / raw)
  To: u-boot

The latest kernel PWM drivers enable the polarity settings. When system
run from U-Boot to kerenl, if there are differences in polarity set or
duty cycle, the PMW will re-init:
  close -> set polarity and duty cycle -> enable the PWM.
The power supply controled by pwm regulator may have voltage shaking,
which lead to the system not stable.

Signed-off-by: Elaine Zhang <zhangqing@rock-chips.com>
Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
Acked-by: Simon Glass <sjg@chromium.org>
---

Changes in v4:
- Split patch into uclass and rockchip patch

Changes in v3:
- use bool type for polarity

Changes in v2:
- use pwm_set_invert() instead of pwm_set_init()
- add comment for polarity

 drivers/power/regulator/pwm_regulator.c | 16 ++++++++++++++--
 drivers/pwm/pwm-uclass.c                | 10 ++++++++++
 include/pwm.h                           | 19 +++++++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/drivers/power/regulator/pwm_regulator.c b/drivers/power/regulator/pwm_regulator.c
index 4875238..a6c9fcc 100644
--- a/drivers/power/regulator/pwm_regulator.c
+++ b/drivers/power/regulator/pwm_regulator.c
@@ -24,6 +24,12 @@ struct pwm_regulator_info {
 	int pwm_id;
 	/* the period of one PWM cycle */
 	int period_ns;
+	/*
+	 * the polarity of one PWM
+	 * 0: normal polarity
+	 * 1: inverted polarity
+	 */
+	bool polarity;
 	struct udevice *pwm;
 	/* initialize voltage of regulator */
 	unsigned int init_voltage;
@@ -49,7 +55,7 @@ static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV)
 	int max_uV = priv->max_voltage;
 	int diff = max_uV - min_uV;
 
-	return 100 - (((req_uV * 100) - (min_uV * 100)) / diff);
+	return ((req_uV * 100) - (min_uV * 100)) / diff;
 }
 
 static int pwm_regulator_get_voltage(struct udevice *dev)
@@ -67,6 +73,12 @@ static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt)
 
 	duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt);
 
+	ret = pwm_set_invert(priv->pwm, priv->pwm_id, priv->polarity);
+	if (ret) {
+		dev_err(dev, "Failed to init PWM\n");
+		return ret;
+	}
+
 	ret = pwm_set_config(priv->pwm, priv->pwm_id,
 			(priv->period_ns / 100) * duty_cycle, priv->period_ns);
 	if (ret) {
@@ -97,9 +109,9 @@ static int pwm_regulator_ofdata_to_platdata(struct udevice *dev)
 		debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
 		return ret;
 	}
-	/* TODO: pwm_id here from device tree if needed */
 
 	priv->period_ns = args.args[1];
+	priv->polarity = args.args[2];
 
 	priv->init_voltage = fdtdec_get_int(blob, node,
 			"regulator-init-microvolt", -1);
diff --git a/drivers/pwm/pwm-uclass.c b/drivers/pwm/pwm-uclass.c
index c2200af..69051fe 100644
--- a/drivers/pwm/pwm-uclass.c
+++ b/drivers/pwm/pwm-uclass.c
@@ -9,6 +9,16 @@
 #include <dm.h>
 #include <pwm.h>
 
+int pwm_set_invert(struct udevice *dev, uint channel, bool polarity)
+{
+	struct pwm_ops *ops = pwm_get_ops(dev);
+
+	if (!ops->set_invert)
+		return -ENOSYS;
+
+	return ops->set_invert(dev, channel, polarity);
+}
+
 int pwm_set_config(struct udevice *dev, uint channel, uint period_ns,
 		   uint duty_ns)
 {
diff --git a/include/pwm.h b/include/pwm.h
index 851915e..ebee227 100644
--- a/include/pwm.h
+++ b/include/pwm.h
@@ -34,6 +34,15 @@ struct pwm_ops {
 	 * @return 0 if OK, -ve on error
 	 */
 	int (*set_enable)(struct udevice *dev, uint channel, bool enable);
+	/**
+	 * set_invert() - Set the PWM invert
+	 *
+	 * @dev:        PWM device to update
+	 * @channel:    PWM channel to update
+	 * @polarity:   true to invert, false to keep normal polarity
+	 * @return 0 if OK, -ve on error
+	 */
+	int (*set_invert)(struct udevice *dev, uint channel, bool polarity);
 };
 
 #define pwm_get_ops(dev)	((struct pwm_ops *)(dev)->driver->ops)
@@ -60,6 +69,16 @@ int pwm_set_config(struct udevice *dev, uint channel, uint period_ns,
  */
 int pwm_set_enable(struct udevice *dev, uint channel, bool enable);
 
+/**
+ * pwm_set_invert() - Set pwm default polarity
+ *
+ * @dev:	PWM device to update
+ * @channel:	PWM channel to update
+ * @polarity:	true to invert, false to keep normal polarity
+ * @return 0 if OK, -ve on error
+ */
+int pwm_set_invert(struct udevice *dev, uint channel, bool polarity);
+
 /* Legacy interface */
 #ifndef CONFIG_DM_PWM
 int	pwm_init		(int pwm_id, int div, int invert);
-- 
1.9.1

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

* [U-Boot] [PATCH v4 2/4] rockchip: pwm: implement pwm_set_invert()
  2017-04-24  2:27 [U-Boot] [PATCH v4 1/4] power: regulator: pwm: support pwm polarity setting Kever Yang
@ 2017-04-24  2:27 ` Kever Yang
  2017-05-02 11:12   ` sjg at google.com
  2017-04-24  2:27 ` [U-Boot] [PATCH v4 3/4] doc: dtbinding: add pwm binding file Kever Yang
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Kever Yang @ 2017-04-24  2:27 UTC (permalink / raw)
  To: u-boot

Rockchip pwm need to init polarity, implement pwm_set_invert()
to do it.

Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
Acked-by: Simon Glass <sjg@chromium.org>
---

Changes in v4: None
Changes in v3: None
Changes in v2: None

 drivers/pwm/rk_pwm.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/drivers/pwm/rk_pwm.c b/drivers/pwm/rk_pwm.c
index 9254f5b..f3b2f76 100644
--- a/drivers/pwm/rk_pwm.c
+++ b/drivers/pwm/rk_pwm.c
@@ -21,8 +21,22 @@ DECLARE_GLOBAL_DATA_PTR;
 struct rk_pwm_priv {
 	struct rk3288_pwm *regs;
 	ulong freq;
+	uint enable_conf;
 };
 
+static int rk_pwm_set_invert(struct udevice *dev, uint channel, bool polarity)
+{
+	struct rk_pwm_priv *priv = dev_get_priv(dev);
+
+	debug("%s: polarity=%u\n", __func__, polarity);
+	if (polarity)
+		priv->enable_conf |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSTIVE;
+	else
+		priv->enable_conf |= PWM_DUTY_POSTIVE | PWM_INACTIVE_NEGATIVE;
+
+	return 0;
+}
+
 static int rk_pwm_set_config(struct udevice *dev, uint channel, uint period_ns,
 			     uint duty_ns)
 {
@@ -32,7 +46,7 @@ static int rk_pwm_set_config(struct udevice *dev, uint channel, uint period_ns,
 
 	debug("%s: period_ns=%u, duty_ns=%u\n", __func__, period_ns, duty_ns);
 	writel(PWM_SEL_SRC_CLK | PWM_OUTPUT_LEFT | PWM_LP_DISABLE |
-		PWM_CONTINUOUS | PWM_DUTY_POSTIVE | PWM_INACTIVE_POSTIVE |
+		PWM_CONTINUOUS | priv->enable_conf |
 		RK_PWM_DISABLE,
 		&regs->ctrl);
 
@@ -83,6 +97,7 @@ static int rk_pwm_probe(struct udevice *dev)
 }
 
 static const struct pwm_ops rk_pwm_ops = {
+	.set_invert	= rk_pwm_set_invert,
 	.set_config	= rk_pwm_set_config,
 	.set_enable	= rk_pwm_set_enable,
 };
-- 
1.9.1

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

* [U-Boot] [PATCH v4 3/4] doc: dtbinding: add pwm binding file
  2017-04-24  2:27 [U-Boot] [PATCH v4 1/4] power: regulator: pwm: support pwm polarity setting Kever Yang
  2017-04-24  2:27 ` [U-Boot] [PATCH v4 2/4] rockchip: pwm: implement pwm_set_invert() Kever Yang
@ 2017-04-24  2:27 ` Kever Yang
  2017-05-02 11:12   ` sjg at google.com
  2017-04-24  2:27 ` [U-Boot] [PATCH v4 4/4] dm: sanbox: pwm: add test for pwm_set_invert() Kever Yang
  2017-05-02 11:12 ` [U-Boot] [PATCH v4 1/4] power: regulator: pwm: support pwm polarity setting sjg at google.com
  3 siblings, 1 reply; 9+ messages in thread
From: Kever Yang @ 2017-04-24  2:27 UTC (permalink / raw)
  To: u-boot

This is a copy from kernel.

Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
Acked-by: Simon Glass <sjg@chromium.org>
---

Changes in v4: None
Changes in v3: None
Changes in v2: None

 doc/device-tree-bindings/pwm/pwm.txt | 69 ++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 doc/device-tree-bindings/pwm/pwm.txt

diff --git a/doc/device-tree-bindings/pwm/pwm.txt b/doc/device-tree-bindings/pwm/pwm.txt
new file mode 100644
index 0000000..8556263
--- /dev/null
+++ b/doc/device-tree-bindings/pwm/pwm.txt
@@ -0,0 +1,69 @@
+Specifying PWM information for devices
+======================================
+
+1) PWM user nodes
+-----------------
+
+PWM users should specify a list of PWM devices that they want to use
+with a property containing a 'pwm-list':
+
+	pwm-list ::= <single-pwm> [pwm-list]
+	single-pwm ::= <pwm-phandle> <pwm-specifier>
+	pwm-phandle : phandle to PWM controller node
+	pwm-specifier : array of #pwm-cells specifying the given PWM
+			(controller specific)
+
+PWM properties should be named "pwms". The exact meaning of each pwms
+property must be documented in the device tree binding for each device.
+An optional property "pwm-names" may contain a list of strings to label
+each of the PWM devices listed in the "pwms" property. If no "pwm-names"
+property is given, the name of the user node will be used as fallback.
+
+Drivers for devices that use more than a single PWM device can use the
+"pwm-names" property to map the name of the PWM device requested by the
+pwm_get() call to an index into the list given by the "pwms" property.
+
+The following example could be used to describe a PWM-based backlight
+device:
+
+	pwm: pwm {
+		#pwm-cells = <2>;
+	};
+
+	[...]
+
+	bl: backlight {
+		pwms = <&pwm 0 5000000>;
+		pwm-names = "backlight";
+	};
+
+Note that in the example above, specifying the "pwm-names" is redundant
+because the name "backlight" would be used as fallback anyway.
+
+pwm-specifier typically encodes the chip-relative PWM number and the PWM
+period in nanoseconds.
+
+Optionally, the pwm-specifier can encode a number of flags (defined in
+<dt-bindings/pwm/pwm.h>) in a third cell:
+- PWM_POLARITY_INVERTED: invert the PWM signal polarity
+
+Example with optional PWM specifier for inverse polarity
+
+	bl: backlight {
+		pwms = <&pwm 0 5000000 PWM_POLARITY_INVERTED>;
+		pwm-names = "backlight";
+	};
+
+2) PWM controller nodes
+-----------------------
+
+PWM controller nodes must specify the number of cells used for the
+specifier using the '#pwm-cells' property.
+
+An example PWM controller might look like this:
+
+	pwm: pwm at 7000a000 {
+		compatible = "nvidia,tegra20-pwm";
+		reg = <0x7000a000 0x100>;
+		#pwm-cells = <2>;
+	};
-- 
1.9.1

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

* [U-Boot] [PATCH v4 4/4] dm: sanbox: pwm: add test for pwm_set_invert()
  2017-04-24  2:27 [U-Boot] [PATCH v4 1/4] power: regulator: pwm: support pwm polarity setting Kever Yang
  2017-04-24  2:27 ` [U-Boot] [PATCH v4 2/4] rockchip: pwm: implement pwm_set_invert() Kever Yang
  2017-04-24  2:27 ` [U-Boot] [PATCH v4 3/4] doc: dtbinding: add pwm binding file Kever Yang
@ 2017-04-24  2:27 ` Kever Yang
  2017-05-02 11:12   ` sjg at google.com
  2017-05-02 11:12 ` [U-Boot] [PATCH v4 1/4] power: regulator: pwm: support pwm polarity setting sjg at google.com
  3 siblings, 1 reply; 9+ messages in thread
From: Kever Yang @ 2017-04-24  2:27 UTC (permalink / raw)
  To: u-boot

Add test case for new interface set_invert().

Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
Acked-by: Simon Glass <sjg@chromium.org>
---

Changes in v4: None
Changes in v3:
- add sanbox test

Changes in v2: None

 drivers/pwm/sandbox_pwm.c | 16 ++++++++++++++++
 test/dm/pwm.c             |  1 +
 2 files changed, 17 insertions(+)

diff --git a/drivers/pwm/sandbox_pwm.c b/drivers/pwm/sandbox_pwm.c
index c2ce974..50a35d9 100644
--- a/drivers/pwm/sandbox_pwm.c
+++ b/drivers/pwm/sandbox_pwm.c
@@ -21,6 +21,7 @@ struct sandbox_pwm_chan {
 	uint period_ns;
 	uint duty_ns;
 	bool enable;
+	bool polarity;
 };
 
 struct sandbox_pwm_priv {
@@ -56,9 +57,24 @@ static int sandbox_pwm_set_enable(struct udevice *dev, uint channel,
 	return 0;
 }
 
+static int sandbox_pwm_set_invert(struct udevice *dev, uint channel,
+				  bool polarity);
+{
+	struct sandbox_pwm_priv *priv = dev_get_priv(dev);
+	struct sandbox_pwm_chan *chan;
+
+	if (channel >= NUM_CHANNELS)
+		return -ENOSPC;
+	chan = &priv->chan[channel];
+	chan->polarity = polarity;
+
+	return 0;
+}
+
 static const struct pwm_ops sandbox_pwm_ops = {
 	.set_config	= sandbox_pwm_set_config,
 	.set_enable	= sandbox_pwm_set_enable,
+	.set_invert	= sandbox_pwm_set_invert,
 };
 
 static const struct udevice_id sandbox_pwm_ids[] = {
diff --git a/test/dm/pwm.c b/test/dm/pwm.c
index 7bdc75a..f1e38c7 100644
--- a/test/dm/pwm.c
+++ b/test/dm/pwm.c
@@ -23,6 +23,7 @@ static int dm_test_pwm_base(struct unit_test_state *uts)
 	ut_assertok(pwm_set_enable(dev, 1, true));
 	ut_assertok(pwm_set_enable(dev, 2, true));
 	ut_asserteq(-ENOSPC, pwm_set_enable(dev, 3, true));
+	ut_assertok(pwm_set_invert(dev, 0, true));
 
 	ut_assertok(uclass_get_device(UCLASS_PWM, 1, &dev));
 	ut_asserteq(-ENODEV, uclass_get_device(UCLASS_PWM, 2, &dev));
-- 
1.9.1

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

* [U-Boot] [PATCH v4 4/4] dm: sanbox: pwm: add test for pwm_set_invert()
  2017-04-24  2:27 ` [U-Boot] [PATCH v4 4/4] dm: sanbox: pwm: add test for pwm_set_invert() Kever Yang
@ 2017-05-02 11:12   ` sjg at google.com
  2017-05-02 11:13     ` Simon Glass
  0 siblings, 1 reply; 9+ messages in thread
From: sjg at google.com @ 2017-05-02 11:12 UTC (permalink / raw)
  To: u-boot

Add test case for new interface set_invert().

Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
Acked-by: Simon Glass <sjg@chromium.org>
---

Changes in v4: None
Changes in v3:
- add sanbox test

Changes in v2: None

 drivers/pwm/sandbox_pwm.c | 16 ++++++++++++++++
 test/dm/pwm.c             |  1 +
 2 files changed, 17 insertions(+)

Applied to u-boot-rockchip/next, thanks!

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

* [U-Boot] [PATCH v4 3/4] doc: dtbinding: add pwm binding file
  2017-04-24  2:27 ` [U-Boot] [PATCH v4 3/4] doc: dtbinding: add pwm binding file Kever Yang
@ 2017-05-02 11:12   ` sjg at google.com
  0 siblings, 0 replies; 9+ messages in thread
From: sjg at google.com @ 2017-05-02 11:12 UTC (permalink / raw)
  To: u-boot

This is a copy from kernel.

Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
Acked-by: Simon Glass <sjg@chromium.org>
---

Changes in v4: None
Changes in v3: None
Changes in v2: None

 doc/device-tree-bindings/pwm/pwm.txt | 69 ++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 doc/device-tree-bindings/pwm/pwm.txt

Applied to u-boot-rockchip/next, thanks!

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

* [U-Boot] [PATCH v4 2/4] rockchip: pwm: implement pwm_set_invert()
  2017-04-24  2:27 ` [U-Boot] [PATCH v4 2/4] rockchip: pwm: implement pwm_set_invert() Kever Yang
@ 2017-05-02 11:12   ` sjg at google.com
  0 siblings, 0 replies; 9+ messages in thread
From: sjg at google.com @ 2017-05-02 11:12 UTC (permalink / raw)
  To: u-boot

Rockchip pwm need to init polarity, implement pwm_set_invert()
to do it.

Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
Acked-by: Simon Glass <sjg@chromium.org>
---

Changes in v4: None
Changes in v3: None
Changes in v2: None

 drivers/pwm/rk_pwm.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

Applied to u-boot-rockchip/next, thanks!

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

* [U-Boot] [PATCH v4 1/4] power: regulator: pwm: support pwm polarity setting
  2017-04-24  2:27 [U-Boot] [PATCH v4 1/4] power: regulator: pwm: support pwm polarity setting Kever Yang
                   ` (2 preceding siblings ...)
  2017-04-24  2:27 ` [U-Boot] [PATCH v4 4/4] dm: sanbox: pwm: add test for pwm_set_invert() Kever Yang
@ 2017-05-02 11:12 ` sjg at google.com
  3 siblings, 0 replies; 9+ messages in thread
From: sjg at google.com @ 2017-05-02 11:12 UTC (permalink / raw)
  To: u-boot

The latest kernel PWM drivers enable the polarity settings. When system
run from U-Boot to kerenl, if there are differences in polarity set or
duty cycle, the PMW will re-init:
  close -> set polarity and duty cycle -> enable the PWM.
The power supply controled by pwm regulator may have voltage shaking,
which lead to the system not stable.

Signed-off-by: Elaine Zhang <zhangqing@rock-chips.com>
Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
Acked-by: Simon Glass <sjg@chromium.org>
---

Changes in v4:
- Split patch into uclass and rockchip patch

Changes in v3:
- use bool type for polarity

Changes in v2:
- use pwm_set_invert() instead of pwm_set_init()
- add comment for polarity

 drivers/power/regulator/pwm_regulator.c | 16 ++++++++++++++--
 drivers/pwm/pwm-uclass.c                | 10 ++++++++++
 include/pwm.h                           | 19 +++++++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)

Applied to u-boot-rockchip/next, thanks!

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

* [U-Boot] [PATCH v4 4/4] dm: sanbox: pwm: add test for pwm_set_invert()
  2017-05-02 11:12   ` sjg at google.com
@ 2017-05-02 11:13     ` Simon Glass
  0 siblings, 0 replies; 9+ messages in thread
From: Simon Glass @ 2017-05-02 11:13 UTC (permalink / raw)
  To: u-boot

On 2 May 2017 at 05:12,  <sjg@google.com> wrote:
> Add test case for new interface set_invert().
>
> Signed-off-by: Kever Yang <kever.yang@rock-chips.com>
> Acked-by: Simon Glass <sjg@chromium.org>
> ---
>
> Changes in v4: None
> Changes in v3:
> - add sanbox test
>
> Changes in v2: None
>
>  drivers/pwm/sandbox_pwm.c | 16 ++++++++++++++++
>  test/dm/pwm.c             |  1 +
>  2 files changed, 17 insertions(+)
>
> Applied to u-boot-rockchip/next, thanks!

(note on this one I fixed a build error at sandbox_pwm_set_invert()
and the commit subject typo)

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

end of thread, other threads:[~2017-05-02 11:13 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-24  2:27 [U-Boot] [PATCH v4 1/4] power: regulator: pwm: support pwm polarity setting Kever Yang
2017-04-24  2:27 ` [U-Boot] [PATCH v4 2/4] rockchip: pwm: implement pwm_set_invert() Kever Yang
2017-05-02 11:12   ` sjg at google.com
2017-04-24  2:27 ` [U-Boot] [PATCH v4 3/4] doc: dtbinding: add pwm binding file Kever Yang
2017-05-02 11:12   ` sjg at google.com
2017-04-24  2:27 ` [U-Boot] [PATCH v4 4/4] dm: sanbox: pwm: add test for pwm_set_invert() Kever Yang
2017-05-02 11:12   ` sjg at google.com
2017-05-02 11:13     ` Simon Glass
2017-05-02 11:12 ` [U-Boot] [PATCH v4 1/4] power: regulator: pwm: support pwm polarity setting sjg at google.com

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.