Skip to contentSkip to conversation

pwm: Add different PWM output types support

PATCH In review Guru Das Srinagesh 5 participants 12 messages Sep 13, 2019 – Sep 18, 2021 PWM

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 pwm: Add different PWM output types support

From: Fenglin Wu <[email protected]>

Normally, PWM channel has fixed output until software request to change its settings. There are some PWM devices which their outputs could be changed autonomously according to a predefined pattern programmed in hardware. Add pwm_output_type enum type to identify these two different PWM types and add relevant helper functions to set and get PWM output types and pattern.

Change-Id Ia1f914a45ab4f4dd7be037a395eeb89d0e65a80e
---
 drivers/pwm/core.c  | 26 ++++++++++++++++++++
 drivers/pwm/sysfs.c | 50 ++++++++++++++++++++++++++++++++++++++
 include/linux/pwm.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 146 insertions(+)
drivers/pwm/core.c +26 -0
index 8edfac1..960a451 100644
@@ -282,6 +282,7 @@ int pwmchip_add_with_polarity(struct pwm_chip *chip,
 		pwm->pwm = chip->base + i;
 		pwm->hwpwm = i;
 		pwm->state.polarity = polarity;
+		pwm->state.output_type = PWM_OUTPUT_FIXED;
 
 		if (chip->ops->get_state)
 			chip->ops->get_state(chip, pwm, &pwm->state);
@@ -498,6 +499,31 @@ int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state)
 			pwm->state.polarity = state->polarity;
 		}
 
+		if (state->output_type != pwm->state.output_type) {
+			if (!pwm->chip->ops->set_output_type)
+				return -ENOTSUPP;
+
+			err = pwm->chip->ops->set_output_type(pwm->chip, pwm,
+						state->output_type);
+			if (err)
+				return err;
+
+			pwm->state.output_type = state->output_type;
+		}
+
+		if (state->output_pattern != pwm->state.output_pattern &&
+				state->output_pattern != NULL) {
+			if (!pwm->chip->ops->set_output_pattern)
+				return -ENOTSUPP;
+
+			err = pwm->chip->ops->set_output_pattern(pwm->chip,
+					pwm, state->output_pattern);
+			if (err)
+				return err;
+
+			pwm->state.output_pattern = state->output_pattern;
+		}
+
 		if (state->period != pwm->state.period ||
 		    state->duty_cycle != pwm->state.duty_cycle) {
 			err = pwm->chip->ops->config(pwm->chip, pwm,
drivers/pwm/sysfs.c +50 -0

67 lines not on this page · raw message

include/linux/pwm.h +70 -0

106 lines not on this page · raw message

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#1 lore raw

PATCH 2/2 pwm: core: Add option to config PWM duty/period with u64 data length

From: Fenglin Wu <[email protected]>

Currently, PWM core driver provides interfaces for configuring PWM period and duty length in nanoseconds with an integer data type, so the max period can be only set to ~2.147 seconds. Add interfaces which can set PWM period and duty with u64 data type to remove this limitation.

Change-Id Ic8722088510d447fc939ab6a5014711aef1b832f
---
 drivers/pwm/core.c  |  20 ++++++---
 drivers/pwm/sysfs.c |   6 +--
 include/linux/pwm.h | 115 ++++++++++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 126 insertions(+), 15 deletions(-)
drivers/pwm/core.c +15 -5
index 960a451..02ad16b 100644
@@ -526,9 +526,19 @@ int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state)
 
 		if (state->period != pwm->state.period ||
 		    state->duty_cycle != pwm->state.duty_cycle) {
-			err = pwm->chip->ops->config(pwm->chip, pwm,
-						     state->duty_cycle,
-						     state->period);
+			if (pwm->chip->ops->config_extend) {
+				err = pwm->chip->ops->config_extend(pwm->chip,
+						pwm, state->duty_cycle,
+						state->period);
+			} else {
+				if (state->period > UINT_MAX)
+					pr_warn("period %llu duty_cycle %llu will be truncated\n",
+							state->period,
+							state->duty_cycle);
+				err = pwm->chip->ops->config(pwm->chip, pwm,
+						state->duty_cycle,
+						state->period);
+			}
 			if (err)
 				return err;
 
@@ -1181,8 +1191,8 @@ static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
 		if (state.enabled)
 			seq_puts(s, " enabled");
 
-		seq_printf(s, " period: %u ns", state.period);
-		seq_printf(s, " duty: %u ns", state.duty_cycle);
+		seq_printf(s, " period: %llu ns", state.period);
+		seq_printf(s, " duty: %llu ns", state.duty_cycle);
 		seq_printf(s, " polarity: %s",
 			   state.polarity ? "inverse" : "normal");
 
drivers/pwm/sysfs.c +3 -3
index ab703f2..ef78c40 100644
@@ -42,7 +42,7 @@ static ssize_t period_show(struct device *child,
 
 	pwm_get_state(pwm, &state);
 
-	return sprintf(buf, "%u\n", state.period);
+	return sprintf(buf, "%llu\n", state.period);
 }
 
 static ssize_t period_store(struct device *child,
@@ -77,7 +77,7 @@ static ssize_t duty_cycle_show(struct device *child,
 
 	pwm_get_state(pwm, &state);
 
-	return sprintf(buf, "%u\n", state.duty_cycle);
+	return sprintf(buf, "%llu\n", state.duty_cycle);
 }
 
 static ssize_t duty_cycle_store(struct device *child,
@@ -212,7 +212,7 @@ static ssize_t capture_show(struct device *child,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
+	return sprintf(buf, "%llu %llu\n", result.period, result.duty_cycle);
 }
 
 static ssize_t output_type_show(struct device *child,
include/linux/pwm.h +108 -7

182 lines not on this page · raw message

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw

Re: [PATCH 2/2] pwm: core: Add option to config PWM duty/period with u64 data length

Thank you for the patch! Yet something to improve:

Hi Guru,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.3 next-20190915]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Guru-Das-Srinagesh/pwm-Add-different-PWM-output-types-support/20190916-151008
config: i386-randconfig-h004-201937 (attached as .config)
compiler: gcc-7 (Debian 7.4.0-11) 7.4.0 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

ld: drivers/hwmon/pwm-fan.o: in function `pwm_fan_resume':

>> drivers/hwmon/pwm-fan.c:440: undefined reference to `__udivdi3'

vim +440 drivers/hwmon/pwm-fan.c

d82d57767c8598 Kamil Debski    2014-07-16  420  
d82d57767c8598 Kamil Debski    2014-07-16  421  static int pwm_fan_resume(struct device *dev)
d82d57767c8598 Kamil Debski    2014-07-16  422  {
d82d57767c8598 Kamil Debski    2014-07-16  423  	struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
2289711c9d4d58 Boris Brezillon 2016-04-14  424  	struct pwm_args pargs;
48b9d5b4f40825 Kamil Debski    2014-11-03  425  	unsigned long duty;
48b9d5b4f40825 Kamil Debski    2014-11-03  426  	int ret;
d82d57767c8598 Kamil Debski    2014-07-16  427  
b57e1d42939721 Stefan Wahren   2019-02-22  428  	if (ctx->reg_en) {
b57e1d42939721 Stefan Wahren   2019-02-22  429  		ret = regulator_enable(ctx->reg_en);
b57e1d42939721 Stefan Wahren   2019-02-22  430  		if (ret) {
b57e1d42939721 Stefan Wahren   2019-02-22  431  			dev_err(dev, "Failed to enable fan supply: %d\n", ret);
b57e1d42939721 Stefan Wahren   2019-02-22  432  			return ret;
b57e1d42939721 Stefan Wahren   2019-02-22  433  		}
b57e1d42939721 Stefan Wahren   2019-02-22  434  	}
b57e1d42939721 Stefan Wahren   2019-02-22  435  
48b9d5b4f40825 Kamil Debski    2014-11-03  436  	if (ctx->pwm_value == 0)
d82d57767c8598 Kamil Debski    2014-07-16  437  		return 0;
48b9d5b4f40825 Kamil Debski    2014-11-03  438  
2289711c9d4d58 Boris Brezillon 2016-04-14  439  	pwm_get_args(ctx->pwm, &pargs);
2289711c9d4d58 Boris Brezillon 2016-04-14 @440  	duty = DIV_ROUND_UP(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
2289711c9d4d58 Boris Brezillon 2016-04-14  441  	ret = pwm_config(ctx->pwm, duty, pargs.period);
48b9d5b4f40825 Kamil Debski    2014-11-03  442  	if (ret)
48b9d5b4f40825 Kamil Debski    2014-11-03  443  		return ret;
48b9d5b4f40825 Kamil Debski    2014-11-03  444  	return pwm_enable(ctx->pwm);
d82d57767c8598 Kamil Debski    2014-07-16  445  }
d82d57767c8598 Kamil Debski    2014-07-16  446  #endif

d82d57767c8598 Kamil Debski 2014-07-16 447

:::::: The code at line 440 was first introduced by commit
:::::: 2289711c9d4d588954ff86a06685f1579bf6c446 hwmon: pwm-fan: Use pwm_get_args() where appropriate

:::::: TO: Boris Brezillon <[email protected]> :::::: CC: Thierry Reding <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#3 lore raw

Re: [PATCH 2/2] pwm: core: Add option to config PWM duty/period with u64 data length

In reply to Guru Das Srinagesh
Quoted 8 lines · Guru Das Srinagesh

From: Fenglin Wu <[email protected]>

Currently, PWM core driver provides interfaces for configuring PWM period and duty length in nanoseconds with an integer data type, so the max period can be only set to ~2.147 seconds. Add interfaces which can set PWM period and duty with u64 data type to remove this limitation.

Quoted 8 lines
Change-Id Ic8722088510d447fc939ab6a5014711aef1b832f
---
 drivers/pwm/core.c  |  20 ++++++---
 drivers/pwm/sysfs.c |   6 +--
 include/linux/pwm.h | 115 ++++++++++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 126 insertions(+), 15 deletions(-)

pwm_ops->config() is considered legacy API and only remains for drivers that haven't been converted to the atomic API yet. If you want to extend the range for period and duty cycle, please add that to the atomic API, which is pwm_ops->apply() and struct pwm_state.

Thierry

Quoted 269 lines
drivers/pwm/core.c +15 -5
index 960a451..02ad16b 100644
@@ -526,9 +526,19 @@ int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state)
 
 		if (state->period != pwm->state.period ||
 		    state->duty_cycle != pwm->state.duty_cycle) {
-			err = pwm->chip->ops->config(pwm->chip, pwm,
-						     state->duty_cycle,
-						     state->period);
+			if (pwm->chip->ops->config_extend) {
+				err = pwm->chip->ops->config_extend(pwm->chip,
+						pwm, state->duty_cycle,
+						state->period);
+			} else {
+				if (state->period > UINT_MAX)
+					pr_warn("period %llu duty_cycle %llu will be truncated\n",
+							state->period,
+							state->duty_cycle);
+				err = pwm->chip->ops->config(pwm->chip, pwm,
+						state->duty_cycle,
+						state->period);
+			}
 			if (err)
 				return err;
 
@@ -1181,8 +1191,8 @@ static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
 		if (state.enabled)
 			seq_puts(s, " enabled");
 
-		seq_printf(s, " period: %u ns", state.period);
-		seq_printf(s, " duty: %u ns", state.duty_cycle);
+		seq_printf(s, " period: %llu ns", state.period);
+		seq_printf(s, " duty: %llu ns", state.duty_cycle);
 		seq_printf(s, " polarity: %s",
 			   state.polarity ? "inverse" : "normal");
 
drivers/pwm/sysfs.c +3 -3
index ab703f2..ef78c40 100644
@@ -42,7 +42,7 @@ static ssize_t period_show(struct device *child,
 
 	pwm_get_state(pwm, &state);
 
-	return sprintf(buf, "%u\n", state.period);
+	return sprintf(buf, "%llu\n", state.period);
 }
 
 static ssize_t period_store(struct device *child,
@@ -77,7 +77,7 @@ static ssize_t duty_cycle_show(struct device *child,
 
 	pwm_get_state(pwm, &state);
 
-	return sprintf(buf, "%u\n", state.duty_cycle);
+	return sprintf(buf, "%llu\n", state.duty_cycle);
 }
 
 static ssize_t duty_cycle_store(struct device *child,
@@ -212,7 +212,7 @@ static ssize_t capture_show(struct device *child,
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
+	return sprintf(buf, "%llu %llu\n", result.period, result.duty_cycle);
 }
 
 static ssize_t output_type_show(struct device *child,
include/linux/pwm.h +108 -7
index 416f08e..d714385 100644
@@ -39,7 +39,7 @@ enum pwm_polarity {
  * current PWM hardware state.
  */
 struct pwm_args {
-	unsigned int period;
+	u64 period;
 	enum pwm_polarity polarity;
 };
 
@@ -66,9 +66,9 @@ enum pwm_output_type {
  * @cycles_per_duty: number of PWM period cycles an entry stays at
  */
 struct pwm_output_pattern {
-	unsigned int *duty_pattern;
+	u64 *duty_pattern;
 	unsigned int num_entries;
-	unsigned int cycles_per_duty;
+	u64 cycles_per_duty;
 };
 
 /*
@@ -79,8 +79,8 @@ struct pwm_output_pattern {
  * @enabled: PWM enabled status
  */
 struct pwm_state {
-	unsigned int period;
-	unsigned int duty_cycle;
+	u64 period;
+	u64 duty_cycle;
 	enum pwm_polarity polarity;
 	enum pwm_output_type output_type;
 	struct pwm_output_pattern *output_pattern;
@@ -136,12 +136,30 @@ static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
 		pwm->state.period = period;
 }
 
+static inline void pwm_set_period_extend(struct pwm_device *pwm, u64 period)
+{
+	if (pwm)
+		pwm->state.period = period;
+}
+
 static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
 {
 	struct pwm_state state;
 
 	pwm_get_state(pwm, &state);
 
+	if (state.period > UINT_MAX)
+		pr_warn("PWM period %llu is truncated\n", state.period);
+
+	return (unsigned int)state.period;
+}
+
+static inline u64 pwm_get_period_extend(const struct pwm_device *pwm)
+{
+	struct pwm_state state;
+
+	pwm_get_state(pwm, &state);
+
 	return state.period;
 }
 
@@ -151,12 +169,30 @@ static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
 		pwm->state.duty_cycle = duty;
 }
 
+static inline void pwm_set_duty_cycle_extend(struct pwm_device *pwm, u64 duty)
+{
+	if (pwm)
+		pwm->state.duty_cycle = duty;
+}
+
 static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
 {
 	struct pwm_state state;
 
 	pwm_get_state(pwm, &state);
 
+	if (state.duty_cycle > UINT_MAX)
+		pr_warn("PWM duty cycle %llu is truncated\n", state.duty_cycle);
+
+	return (unsigned int)state.duty_cycle;
+}
+
+static inline u64 pwm_get_duty_cycle_extend(const struct pwm_device *pwm)
+{
+	struct pwm_state state;
+
+	pwm_get_state(pwm, &state);
+
 	return state.duty_cycle;
 }
 
@@ -300,6 +336,8 @@ static inline void pwm_init_state(const struct pwm_device *pwm,
  * @set_output_pattern: set the pattern for the modulated output
  * @owner: helps prevent removal of modules exporting active PWMs
  * @config: configure duty cycles and period length for this PWM
+ * @config_extend: configure duty cycles and period length for this
+ *	PWM with u64 data type
  * @set_polarity: configure the polarity of this PWM
  * @enable: enable PWM output toggling
  * @disable: disable PWM output toggling
@@ -325,6 +363,8 @@ struct pwm_ops {
 	/* Only used by legacy drivers */
 	int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
 		      int duty_ns, int period_ns);
+	int (*config_extend)(struct pwm_chip *chip, struct pwm_device *pwm,
+		      u64 duty_ns, u64 period_ns);
 	int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
 			    enum pwm_polarity polarity);
 	int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
@@ -363,8 +403,8 @@ struct pwm_chip {
  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
  */
 struct pwm_capture {
-	unsigned int period;
-	unsigned int duty_cycle;
+	u64 period;
+	u64 duty_cycle;
 };
 
 #if IS_ENABLED(CONFIG_PWM)
@@ -418,6 +458,67 @@ static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
 }
 
 /**
+ * pwm_config_extend() - change PWM period and duty length with u64 data type
+ * @pwm: PWM device
+ * @duty_ns: "on" time (in nanoseconds)
+ * @period_ns: duration (in nanoseconds) of one cycle
+ *
+ * Returns: 0 on success or a negative error code on failure.
+ */
+static inline int pwm_config_extend(struct pwm_device *pwm, u64 duty_ns,
+			     u64 period_ns)
+{
+	struct pwm_state state;
+
+	if (!pwm)
+		return -EINVAL;
+
+	pwm_get_state(pwm, &state);
+	if (state.duty_cycle == duty_ns && state.period == period_ns)
+		return 0;
+
+	state.duty_cycle = duty_ns;
+	state.period = period_ns;
+	return pwm_apply_state(pwm, &state);
+}
+
+/**
+ * pwm_set_polarity() - configure the polarity of a PWM signal
+ * @pwm: PWM device
+ * @polarity: new polarity of the PWM signal
+ *
+ * Note that the polarity cannot be configured while the PWM device is
+ * enabled.
+ *
+ * Returns: 0 on success or a negative error code on failure.
+ */
+static inline int pwm_set_polarity(struct pwm_device *pwm,
+				   enum pwm_polarity polarity)
+{
+	struct pwm_state state;
+
+	if (!pwm)
+		return -EINVAL;
+
+	pwm_get_state(pwm, &state);
+	if (state.polarity == polarity)
+		return 0;
+
+	/*
+	 * Changing the polarity of a running PWM without adjusting the
+	 * dutycycle/period value is a bit risky (can introduce glitches).
+	 * Return -EBUSY in this case.
+	 * Note that this is allowed when using pwm_apply_state() because
+	 * the user specifies all the parameters.
+	 */
+	if (state.enabled)
+		return -EBUSY;
+
+	state.polarity = polarity;
+	return pwm_apply_state(pwm, &state);
+}
+
+/**
  * pwm_enable() - start a PWM output toggling
  * @pwm: PWM device
  *
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#4 lore raw

Re: [PATCH 2/2] pwm: core: Add option to config PWM duty/period with u64 data length

In reply to Guru Das Srinagesh
Thank you for the patch! Yet something to improve:

Hi Guru,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.3 next-20190915]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Guru-Das-Srinagesh/pwm-Add-different-PWM-output-types-support/20190916-151008
config: arm-multi_v4t_defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=arm

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

drivers/pwm/pwm-clps711x.o: In function `clps711x_pwm_config':

>> pwm-clps711x.c:(.text+0x64): undefined reference to `__aeabi_uldivmod'
   drivers/pwm/pwm-clps711x.o: In function `clps711x_pwm_enable':
   pwm-clps711x.c:(.text+0x168): undefined reference to `__aeabi_uldivmod'
   drivers/video/backlight/pwm_bl.o: In function `pwm_backlight_probe':
>> pwm_bl.c:(.text+0x5fc): undefined reference to `__aeabi_uldivmod'

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#5 lore raw
In reply to Guru Das Srinagesh
Quoted 9 lines · Guru Das Srinagesh

From: Fenglin Wu <[email protected]>

Normally, PWM channel has fixed output until software request to change its settings. There are some PWM devices which their outputs could be changed autonomously according to a predefined pattern programmed in hardware. Add pwm_output_type enum type to identify these two different PWM types and add relevant helper functions to set and get PWM output types and pattern.

Quoted 8 lines
Change-Id Ia1f914a45ab4f4dd7be037a395eeb89d0e65a80e
---
 drivers/pwm/core.c  | 26 ++++++++++++++++++++
 drivers/pwm/sysfs.c | 50 ++++++++++++++++++++++++++++++++++++++
 include/linux/pwm.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 146 insertions(+)

This doesn't seem right to me. Are you describing a PWM pin that's actually driven in GPIO mode? We usually configure that using pinctrl.

Thierry

Quoted 238 lines
drivers/pwm/core.c +26 -0
index 8edfac1..960a451 100644
@@ -282,6 +282,7 @@ int pwmchip_add_with_polarity(struct pwm_chip *chip,
 		pwm->pwm = chip->base + i;
 		pwm->hwpwm = i;
 		pwm->state.polarity = polarity;
+		pwm->state.output_type = PWM_OUTPUT_FIXED;
 
 		if (chip->ops->get_state)
 			chip->ops->get_state(chip, pwm, &pwm->state);
@@ -498,6 +499,31 @@ int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state)
 			pwm->state.polarity = state->polarity;
 		}
 
+		if (state->output_type != pwm->state.output_type) {
+			if (!pwm->chip->ops->set_output_type)
+				return -ENOTSUPP;
+
+			err = pwm->chip->ops->set_output_type(pwm->chip, pwm,
+						state->output_type);
+			if (err)
+				return err;
+
+			pwm->state.output_type = state->output_type;
+		}
+
+		if (state->output_pattern != pwm->state.output_pattern &&
+				state->output_pattern != NULL) {
+			if (!pwm->chip->ops->set_output_pattern)
+				return -ENOTSUPP;
+
+			err = pwm->chip->ops->set_output_pattern(pwm->chip,
+					pwm, state->output_pattern);
+			if (err)
+				return err;
+
+			pwm->state.output_pattern = state->output_pattern;
+		}
+
 		if (state->period != pwm->state.period ||
 		    state->duty_cycle != pwm->state.duty_cycle) {
 			err = pwm->chip->ops->config(pwm->chip, pwm,
drivers/pwm/sysfs.c +50 -0
index 2389b86..ab703f2 100644
@@ -215,11 +215,60 @@ static ssize_t capture_show(struct device *child,
 	return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
 }
 
+static ssize_t output_type_show(struct device *child,
+			     struct device_attribute *attr,
+			     char *buf)
+{
+	const struct pwm_device *pwm = child_to_pwm_device(child);
+	const char *output_type = "unknown";
+	struct pwm_state state;
+
+	pwm_get_state(pwm, &state);
+	switch (state.output_type) {
+	case PWM_OUTPUT_FIXED:
+		output_type = "fixed";
+		break;
+	case PWM_OUTPUT_MODULATED:
+		output_type = "modulated";
+		break;
+	default:
+		break;
+	}
+
+	return snprintf(buf, PAGE_SIZE, "%s\n", output_type);
+}
+
+static ssize_t output_type_store(struct device *child,
+			      struct device_attribute *attr,
+			      const char *buf, size_t size)
+{
+	struct pwm_export *export = child_to_pwm_export(child);
+	struct pwm_device *pwm = export->pwm;
+	struct pwm_state state;
+	int ret = -EINVAL;
+
+	mutex_lock(&export->lock);
+	pwm_get_state(pwm, &state);
+	if (sysfs_streq(buf, "fixed"))
+		state.output_type = PWM_OUTPUT_FIXED;
+	else if (sysfs_streq(buf, "modulated"))
+		state.output_type = PWM_OUTPUT_MODULATED;
+	else
+		goto unlock;
+
+	ret = pwm_apply_state(pwm, &state);
+unlock:
+	mutex_unlock(&export->lock);
+
+	return ret ? : size;
+}
+
 static DEVICE_ATTR_RW(period);
 static DEVICE_ATTR_RW(duty_cycle);
 static DEVICE_ATTR_RW(enable);
 static DEVICE_ATTR_RW(polarity);
 static DEVICE_ATTR_RO(capture);
+static DEVICE_ATTR_RW(output_type);
 
 static struct attribute *pwm_attrs[] = {
 	&dev_attr_period.attr,
@@ -227,6 +276,7 @@ static ssize_t capture_show(struct device *child,
 	&dev_attr_enable.attr,
 	&dev_attr_polarity.attr,
 	&dev_attr_capture.attr,
+	&dev_attr_output_type.attr,
 	NULL
 };
 ATTRIBUTE_GROUPS(pwm);
include/linux/pwm.h +70 -0
index 24632a7..416f08e 100644
@@ -48,6 +48,29 @@ enum {
 	PWMF_EXPORTED = 1 << 1,
 };
 
+/**
+ * enum pwm_output_type - output type of the PWM signal
+ * @PWM_OUTPUT_FIXED: PWM output is fixed until a change request
+ * @PWM_OUTPUT_MODULATED: PWM output is modulated in hardware
+ * autonomously with a predefined pattern
+ */
+enum pwm_output_type {
+	PWM_OUTPUT_FIXED = 1 << 0,
+	PWM_OUTPUT_MODULATED = 1 << 1,
+};
+
+/**
+ * struct pwm_output_pattern - PWM duty pattern for MODULATED duty type
+ * @duty_pattern: PWM duty cycles in the pattern for duty modulation
+ * @num_entries: number of entries in the pattern
+ * @cycles_per_duty: number of PWM period cycles an entry stays at
+ */
+struct pwm_output_pattern {
+	unsigned int *duty_pattern;
+	unsigned int num_entries;
+	unsigned int cycles_per_duty;
+};
+
 /*
  * struct pwm_state - state of a PWM channel
  * @period: PWM period (in nanoseconds)
@@ -59,6 +82,8 @@ struct pwm_state {
 	unsigned int period;
 	unsigned int duty_cycle;
 	enum pwm_polarity polarity;
+	enum pwm_output_type output_type;
+	struct pwm_output_pattern *output_pattern;
 	bool enabled;
 };
 
@@ -144,6 +169,26 @@ static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
 	return state.polarity;
 }
 
+static inline enum pwm_output_type pwm_get_output_type(
+		const struct pwm_device *pwm)
+{
+	struct pwm_state state;
+
+	pwm_get_state(pwm, &state);
+
+	return state.output_type;
+}
+
+static inline struct pwm_output_pattern *pwm_get_output_pattern(
+				struct pwm_device *pwm)
+{
+	struct pwm_state state;
+
+	pwm_get_state(pwm, &state);
+
+	return pwm->state.output_pattern ?: NULL;
+}
+
 static inline void pwm_get_args(const struct pwm_device *pwm,
 				struct pwm_args *args)
 {
@@ -250,6 +295,9 @@ static inline void pwm_init_state(const struct pwm_device *pwm,
  * @get_state: get the current PWM state. This function is only
  *	       called once per PWM device when the PWM chip is
  *	       registered.
+ * @get_output_type_supported: get the supported output type
+ * @set_output_type: set PWM output type
+ * @set_output_pattern: set the pattern for the modulated output
  * @owner: helps prevent removal of modules exporting active PWMs
  * @config: configure duty cycles and period length for this PWM
  * @set_polarity: configure the polarity of this PWM
@@ -265,6 +313,13 @@ struct pwm_ops {
 		     struct pwm_state *state);
 	void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
 			  struct pwm_state *state);
+	int (*get_output_type_supported)(struct pwm_chip *chip,
+			struct pwm_device *pwm);
+	int (*set_output_type)(struct pwm_chip *chip, struct pwm_device *pwm,
+			enum pwm_output_type output_type);
+	int (*set_output_pattern)(struct pwm_chip *chip,
+			struct pwm_device *pwm,
+			struct pwm_output_pattern *output_pattern);
 	struct module *owner;
 
 	/* Only used by legacy drivers */
@@ -320,6 +375,21 @@ struct pwm_capture {
 int pwm_adjust_config(struct pwm_device *pwm);
 
 /**
+ * pwm_output_type_support()
+ * @pwm: PWM device
+ *
+ * Returns:  output types supported by the PWM device
+ */
+static inline int pwm_get_output_type_supported(struct pwm_device *pwm)
+{
+	if (pwm->chip->ops->get_output_type_supported != NULL)
+		return pwm->chip->ops->get_output_type_supported(pwm->chip,
+				pwm);
+	else
+		return PWM_OUTPUT_FIXED;
+}
+
+/**
  * pwm_config() - change a PWM device configuration
  * @pwm: PWM device
  * @duty_ns: "on" time (in nanoseconds)
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#6 lore raw
Quoted 19 lines · Thierry Reding
Quoted 9 lines · Guru Das Srinagesh

From: Fenglin Wu <[email protected]>

Normally, PWM channel has fixed output until software request to change its settings. There are some PWM devices which their outputs could be changed autonomously according to a predefined pattern programmed in hardware. Add pwm_output_type enum type to identify these two different PWM types and add relevant helper functions to set and get PWM output types and pattern.

Quoted 8 lines
Change-Id Ia1f914a45ab4f4dd7be037a395eeb89d0e65a80e
---
 drivers/pwm/core.c  | 26 ++++++++++++++++++++
 drivers/pwm/sysfs.c | 50 ++++++++++++++++++++++++++++++++++++++
 include/linux/pwm.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 146 insertions(+)
Quoted 4 lines

This doesn't seem right to me. Are you describing a PWM pin that's actually driven in GPIO mode? We usually configure that using pinctrl.

Thierry

Sorry, let me clarify.

Some Qualcomm PMICs have a PWM block called the Light Pulse Generator (LPG). This block allows for the generation of a HW-controlled PWM "pattern", i.e. a sequential altering of duty cycle, in addition to the normal PWM "fixed" duty cycle operation, which is what the framework does currently. This pattern is user-configurable in the form of a look-up table in the devicetree. The LPG's registers have to be configured with the data in the look up table in order to start the generation of the pattern. An example of a pattern is the "breath" pattern, which simply ramps up the duty cycle and then ramps it down.

This "pattern" mode is what has been defined as PWM_OUTPUT_MODULATED in this patch. I see that the use of the term "modulated" is misleading - a more accurate term would be PWM_OUTPUT_PATTERN perhaps.

This patch merely adds framework support to differentiate between the "fixed" and "pattern" modes of operation. Actions such as configuring the LPG with the devicetree pattern and setting it up for generating the pattern are performed in the driver only if the output type is read as "pattern" and not otherwise.

#7 lore raw
Quoted 27 lines · Guru Das Srinagesh
Quoted 19 lines · Thierry Reding
Quoted 9 lines · Guru Das Srinagesh

From: Fenglin Wu <[email protected]>

Normally, PWM channel has fixed output until software request to change its settings. There are some PWM devices which their outputs could be changed autonomously according to a predefined pattern programmed in hardware. Add pwm_output_type enum type to identify these two different PWM types and add relevant helper functions to set and get PWM output types and pattern.

Quoted 8 lines
Change-Id Ia1f914a45ab4f4dd7be037a395eeb89d0e65a80e
---
 drivers/pwm/core.c  | 26 ++++++++++++++++++++
 drivers/pwm/sysfs.c | 50 ++++++++++++++++++++++++++++++++++++++
 include/linux/pwm.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 146 insertions(+)
Quoted 4 lines

This doesn't seem right to me. Are you describing a PWM pin that's actually driven in GPIO mode? We usually configure that using pinctrl.

Thierry

Sorry, let me clarify.

Quoted 8 lines

Some Qualcomm PMICs have a PWM block called the Light Pulse Generator (LPG). This block allows for the generation of a HW-controlled PWM "pattern", i.e. a sequential altering of duty cycle, in addition to the normal PWM "fixed" duty cycle operation, which is what the framework does currently. This pattern is user-configurable in the form of a look-up table in the devicetree. The LPG's registers have to be configured with the data in the look up table in order to start the generation of the pattern. An example of a pattern is the "breath" pattern, which simply ramps up the duty cycle and then ramps it down.

I'll try to describe it in my words to check if I got it right: So the mode you want to add needs a sequence of PWM states and the hardware is expected to apply them in turn, each for a configurable count of periods. If I understand this right, this is expected to be cyclic?

Quoted 3 lines

This "pattern" mode is what has been defined as PWM_OUTPUT_MODULATED in this patch. I see that the use of the term "modulated" is misleading - a more accurate term would be PWM_OUTPUT_PATTERN perhaps.

Not sure "pattern" is better.

The PWM on the newer imx SoCs (using the imx27 driver) has a FIFO with length 4 that allows to program changing settings. Only the duty cycle can be modified and as repeat count only 1, 2, 4 and 8 are available. I assume the FIFO can be fed by the dma engine.

Note I only know this feature from reading the reference manual and never used it.

Quoted 4 lines

This patch merely adds framework support to differentiate between the "fixed" and "pattern" modes of operation. Actions such as configuring the LPG with the devicetree pattern and setting it up for generating the pattern are performed in the driver only if the output type is read as "pattern" and not otherwise.

Up to now I'm not convinced that this extension is a good one that can be supported by several PWM implementations. I'd say we should collect first some details about different implementations and what these could implement to get a picture what kind of API is sensible.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#8 lore raw
Quoted 61 lines · Uwe Kleine-König
Quoted 27 lines · Guru Das Srinagesh
Quoted 19 lines · Thierry Reding
Quoted 9 lines · Guru Das Srinagesh

From: Fenglin Wu <[email protected]>

Normally, PWM channel has fixed output until software request to change its settings. There are some PWM devices which their outputs could be changed autonomously according to a predefined pattern programmed in hardware. Add pwm_output_type enum type to identify these two different PWM types and add relevant helper functions to set and get PWM output types and pattern.

Quoted 8 lines
Change-Id Ia1f914a45ab4f4dd7be037a395eeb89d0e65a80e
---
 drivers/pwm/core.c  | 26 ++++++++++++++++++++
 drivers/pwm/sysfs.c | 50 ++++++++++++++++++++++++++++++++++++++
 include/linux/pwm.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 146 insertions(+)
Quoted 4 lines

This doesn't seem right to me. Are you describing a PWM pin that's actually driven in GPIO mode? We usually configure that using pinctrl.

Thierry

Sorry, let me clarify.

Quoted 8 lines

Some Qualcomm PMICs have a PWM block called the Light Pulse Generator (LPG). This block allows for the generation of a HW-controlled PWM "pattern", i.e. a sequential altering of duty cycle, in addition to the normal PWM "fixed" duty cycle operation, which is what the framework does currently. This pattern is user-configurable in the form of a look-up table in the devicetree. The LPG's registers have to be configured with the data in the look up table in order to start the generation of the pattern. An example of a pattern is the "breath" pattern, which simply ramps up the duty cycle and then ramps it down.

I'll try to describe it in my words to check if I got it right: So the mode you want to add needs a sequence of PWM states and the hardware is expected to apply them in turn, each for a configurable count of periods. If I understand this right, this is expected to be cyclic?

Quoted 3 lines

This "pattern" mode is what has been defined as PWM_OUTPUT_MODULATED in this patch. I see that the use of the term "modulated" is misleading - a more accurate term would be PWM_OUTPUT_PATTERN perhaps.

Not sure "pattern" is better.

The PWM on the newer imx SoCs (using the imx27 driver) has a FIFO with length 4 that allows to program changing settings. Only the duty cycle can be modified and as repeat count only 1, 2, 4 and 8 are available. I assume the FIFO can be fed by the dma engine.

Note I only know this feature from reading the reference manual and never used it.

Quoted 4 lines

This patch merely adds framework support to differentiate between the "fixed" and "pattern" modes of operation. Actions such as configuring the LPG with the devicetree pattern and setting it up for generating the pattern are performed in the driver only if the output type is read as "pattern" and not otherwise.

Quoted 4 lines

Up to now I'm not convinced that this extension is a good one that can be supported by several PWM implementations. I'd say we should collect first some details about different implementations and what these could implement to get a picture what kind of API is sensible.

I agree. This sounds to me like it's stretching the concept of a PWM a bit too much. Sounds like drivers/leds might be a better fit for this.

Thierry

#9 lore raw
In reply to Guru Das Srinagesh

Hello,

Quoted 8 lines · Guru Das Srinagesh

From: Fenglin Wu <[email protected]>

Normally, PWM channel has fixed output until software request to change its settings. There are some PWM devices which their outputs could be changed autonomously according to a predefined pattern programmed in hardware. Add pwm_output_type enum type to identify these two different PWM types and add relevant helper functions to set and get PWM output types and pattern.

I have problems to understand what your modulated mode does even after reading your commit log and the patch.

Also you should note here what is the intended usage and add support for it for at least one (preferably more) drivers to make this actually usable.

Quoted 1 line

Change-Id: Ia1f914a45ab4f4dd7be037a395eeb89d0e65a80e

In Linux we don't use these. You're making it easier to apply your patch if you drop the change-id lines.

Quoted 45 lines
drivers/pwm/sysfs.c +37 -0
index 2389b86..ab703f2 100644
@@ -215,11 +215,60 @@ static ssize_t capture_show(struct device *child,
 	return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
 }
 
+static ssize_t output_type_show(struct device *child,
+			     struct device_attribute *attr,
+			     char *buf)
+{
+	const struct pwm_device *pwm = child_to_pwm_device(child);
+	const char *output_type = "unknown";
+	struct pwm_state state;
+
+	pwm_get_state(pwm, &state);
+	switch (state.output_type) {
+	case PWM_OUTPUT_FIXED:
+		output_type = "fixed";
+		break;
+	case PWM_OUTPUT_MODULATED:
+		output_type = "modulated";
+		break;
+	default:
+		break;
+	}
+
+	return snprintf(buf, PAGE_SIZE, "%s\n", output_type);
+}
+
+static ssize_t output_type_store(struct device *child,
+			      struct device_attribute *attr,
+			      const char *buf, size_t size)
+{
+	struct pwm_export *export = child_to_pwm_export(child);
+	struct pwm_device *pwm = export->pwm;
+	struct pwm_state state;
+	int ret = -EINVAL;
+
+	mutex_lock(&export->lock);
+	pwm_get_state(pwm, &state);
+	if (sysfs_streq(buf, "fixed"))
+		state.output_type = PWM_OUTPUT_FIXED;
+	else if (sysfs_streq(buf, "modulated"))

drivers/pwm/sysfs.c static ssize_t capture_show(struct device *child, · line 264

Quoted 10 lines
drivers/pwm/sysfs.c +10 -0
+		state.output_type = PWM_OUTPUT_MODULATED;
+	else
+		goto unlock;
+
+	ret = pwm_apply_state(pwm, &state);
+unlock:
+	mutex_unlock(&export->lock);
+
+	return ret ? : size;
+}

So in sysfs you cannot set a pattern. Doesn't that mean this makes using modulated mode hardly useful?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#10 lore raw
Quoted 12 lines · Uwe Kleine-König

Hello,

Quoted 8 lines · Guru Das Srinagesh

From: Fenglin Wu <[email protected]>

Normally, PWM channel has fixed output until software request to change its settings. There are some PWM devices which their outputs could be changed autonomously according to a predefined pattern programmed in hardware. Add pwm_output_type enum type to identify these two different PWM types and add relevant helper functions to set and get PWM output types and pattern.

Quoted 2 lines

I have problems to understand what your modulated mode does even after reading your commit log and the patch.

Hi Uwe,

I have posted a reply to Thierry's email in which I provide some background and details about this patch for your review. Hopefully that clarifies things - I can provide some more clarifications if necessary.

Quoted 4 lines

Also you should note here what is the intended usage and add support for it for at least one (preferably more) drivers to make this actually usable.

The PWM_OUTPUT_MODULATED mode is useful for PWM peripherals like Qualcomm's Light Pulse Generator (LPG) that can output a duty-cycle pattern in hardware as well as output a fixed duty-cycle signal. All this mode really does is to provide a way for drivers to carry out some actions for the "pattern" mode of operation that are not required (or relevant) for the "fixed" mode of operation.

Quoted 5 lines
Quoted 2 lines

Change-Id: Ia1f914a45ab4f4dd7be037a395eeb89d0e65a80e

In Linux we don't use these. You're making it easier to apply your patch if you drop the change-id lines.

Sorry, forgot to strip these before sending out the patch.

Quoted 51 lines
Quoted 51 lines
drivers/pwm/sysfs.c +42 -0
index 2389b86..ab703f2 100644
@@ -215,11 +215,60 @@ static ssize_t capture_show(struct device *child,
 	return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
 }
 
+static ssize_t output_type_show(struct device *child,
+			     struct device_attribute *attr,
+			     char *buf)
+{
+	const struct pwm_device *pwm = child_to_pwm_device(child);
+	const char *output_type = "unknown";
+	struct pwm_state state;
+
+	pwm_get_state(pwm, &state);
+	switch (state.output_type) {
+	case PWM_OUTPUT_FIXED:
+		output_type = "fixed";
+		break;
+	case PWM_OUTPUT_MODULATED:
+		output_type = "modulated";
+		break;
+	default:
+		break;
+	}
+
+	return snprintf(buf, PAGE_SIZE, "%s\n", output_type);
+}
+
+static ssize_t output_type_store(struct device *child,
+			      struct device_attribute *attr,
+			      const char *buf, size_t size)
+{
+	struct pwm_export *export = child_to_pwm_export(child);
+	struct pwm_device *pwm = export->pwm;
+	struct pwm_state state;
+	int ret = -EINVAL;
+
+	mutex_lock(&export->lock);
+	pwm_get_state(pwm, &state);
+	if (sysfs_streq(buf, "fixed"))
+		state.output_type = PWM_OUTPUT_FIXED;
+	else if (sysfs_streq(buf, "modulated"))
+		state.output_type = PWM_OUTPUT_MODULATED;
+	else
+		goto unlock;
+
+	ret = pwm_apply_state(pwm, &state);
Quoted 8 lines

drivers/pwm/sysfs.c static ssize_t capture_show(struct device *child, · line 264

Quoted 5 lines
drivers/pwm/sysfs.c +5 -0
+unlock:
+	mutex_unlock(&export->lock);
+
+	return ret ? : size;
+}

So in sysfs you cannot set a pattern. Doesn't that mean this makes using modulated mode hardly useful?

The pattern has to be set through the devicetree and not sysfs. Will change the output type sysfs parameter to read-only.

Quoted 7 lines

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#11 lore raw
In reply to Guru Das Srinagesh
Quoted 10 lines · Guru Das Srinagesh

From: Fenglin Wu <[email protected]>

Normally, PWM channel has fixed output until software request to change its settings. There are some PWM devices which their outputs could be changed autonomously according to a predefined pattern programmed in hardware. Add pwm_output_type enum type to identify these two different PWM types and add relevant helper functions to set and get PWM output types and pattern.

Change-Id: Ia1f914a45ab4f4dd7be037a395eeb89d0e65a80e

This is not needed an a upstream patch, checkpatch should have told you :(

Quoted 5 lines
---
 drivers/pwm/core.c  | 26 ++++++++++++++++++++
 drivers/pwm/sysfs.c | 50 ++++++++++++++++++++++++++++++++++++++

You forgot a Documentation/ABI/ update for your new sysfs file :(

thanks,

greg k-h