Hi All,
The following implements a generic pwm framework and adds a user
for it. I already posted this series back in January. Based on
the comments I received I added some details about the motivation
for adding such a framework and not using the led or hwmon framework
to patch 1/2.
I also added some documentation to Documentation/pwm.txt.
This patch does not change the user API for PWMs, in particular
it does not enforce any sleep/nonsleep context to the PWM users.
The patch merely puts the status quo into a core wrapper to be able
to register multiple PWM drivers in the system. Improvements to
the API can still be made later once we have at least a place
in the kernel to collect the existing PWM drivers.
Sascha
The following changes since commit b0af8dfdd67699e25083478c63eedef2e72ebd85:
Linux 3.0-rc5 (2011-06-27 19:12:22 -0700)
are available in the git repository at:
git://git.pengutronix.de/git/imx/linux-2.6.git pwm
Sascha Hauer (2):
PWM: add pwm framework support
pwm: Add a i.MX23/28 pwm driver
Documentation/pwm.txt | 56 ++++++++++
MAINTAINERS | 5 +
drivers/Kconfig | 2 +
drivers/Makefile | 1 +
drivers/pwm/Kconfig | 12 ++
drivers/pwm/Makefile | 1 +
drivers/pwm/core.c | 246 +++++++++++++++++++++++++++++++++++++++++++
drivers/pwm/mxs-pwm.c | 275 +++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/pwm.h | 38 +++++++
9 files changed, 636 insertions(+), 0 deletions(-)
create mode 100644 Documentation/pwm.txt
create mode 100644 drivers/pwm/Kconfig
create mode 100644 drivers/pwm/Makefile
create mode 100644 drivers/pwm/core.c
create mode 100644 drivers/pwm/mxs-pwm.c
This patch adds framework support for PWM (pulse width modulation) devices.
The is a barebone PWM API already in the kernel under include/linux/pwm.h,
but it does not allow for multiple drivers as each of them implements the
pwm_*() functions.
There are other PWM framework patches around from Bill Gatliff. Unlike
his framework this one does not change the existing API for PWMs so that
this framework can act as a drop in replacement for the existing API.
Why another framework?
Several people argue that there should not be another framework for PWMs
but they should be integrated into one of the existing frameworks like led
or hwmon. Unlike these frameworks the PWM framework is agnostic to the
purpose of the PWM. In fact, a PWM can drive a LED, but this makes the
LED framework a user of a PWM, like already done in leds-pwm.c. The gpio
framework also is not suitable for PWMs. Every gpio could be turned into
a PWM using timer based toggling, but on the other hand not every PWM hardware
device can be turned into a gpio due to the lack of hardware capabilities.
This patch does not try to improve the PWM API yet, this could be done in
subsequent patches.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
Documentation/pwm.txt | 56 +++++++++++
MAINTAINERS | 5 +
drivers/Kconfig | 2 +
drivers/Makefile | 1 +
drivers/pwm/Kconfig | 12 +++
drivers/pwm/Makefile | 1 +
drivers/pwm/core.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/pwm.h | 38 ++++++++
8 files changed, 361 insertions(+), 0 deletions(-)
create mode 100644 Documentation/pwm.txt
create mode 100644 drivers/pwm/Kconfig
create mode 100644 drivers/pwm/Makefile
create mode 100644 drivers/pwm/core.c
@@ -0,0 +1,56 @@+Pulse Width Modulation (PWM) interface++This provides an overview about the Linux PWM interface++PWMs are commonly used for controlling LEDs, fans or vibrators in+cell phones. PWMs with a fixed purpose have no need implementing+the Linux PWM API (although they could). However, PWMs are often+found as discrete devices on SoCs which have no fixed purpose. It's+up to the board designer to connect them to LEDs or fans. To provide+this kind of flexibility the generic PWM API exists.++Identifying PWMs+----------------++PWMs are identified by unique ids throughout the system. A platform+should call pwmchip_reserve() during init time to reserve the id range+for internal PWMs so that users have a fixed id to refer to specific+PWMs.++Using PWMs+----------++A PWM can be requested using pwm_request() and freed after usage with+pwm_free(). After being requested a PWM has to be configured using++int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);++To start/stop toggling the PWM output use pwm_enable()/pwm_disable().++Implementing a PWM driver+-------------------------++Currently there are two ways to implement pwm drivers. Traditionally+there only has been the barebone API meaning that each driver has+to implement the pwm_*() functions itself. This means that it's impossible+to have multiple PWM drivers in the system. For this reason it's mandatory+for new drivers to use the generic PWM framework.+A new PWM device can be added using pwmchip_add() and removed again with+pwmchip_remove(). pwmchip_add() takes a filled in struct pwm_chip as+argument which provides the ops and the pwm id to the framework.++Locking+-------++The PWM core list manipulations are protected by a mutex, so pwm_request()+and pwm_free() may not be called from an atomic context. Currently the+PWM core does not enforce any locking to pwm_enable(), pwm_disable() and+pwm_config(), so the calling context is currently driver specific. This+is an issue derived from the former barebone API and should be fixed soon.++Helpers+-------++Currently a PWM can only be configured with period_ns and duty_ns. For several+use cases freq_hz and duty_percent might be better. Instead of calculating +this in your driver please consider adding appropriate helpers to the framework.
Hi All,
The following implements a generic pwm framework and adds a user
for it. I already posted this series back in January. Based on
the comments I received I added some details about the motivation
for adding such a framework and not using the led or hwmon framework
to patch 1/2.
I also added some documentation to Documentation/pwm.txt.
This patch does not change the user API for PWMs, in particular
it does not enforce any sleep/nonsleep context to the PWM users.
The patch merely puts the status quo into a core wrapper to be able
to register multiple PWM drivers in the system. Improvements to
the API can still be made later once we have at least a place
in the kernel to collect the existing PWM drivers.
Just a small question: on PXA (just for example) PWMs are registered
from arch/arm via arch_initcall. Will your subsystem work for such early
registration (of course after moving PXA driver to drivers/pwm)?
--
With best wishes
Dmitry
This patch adds framework support for PWM (pulse width modulation) devices.
The is a barebone PWM API already in the kernel under include/linux/pwm.h,
but it does not allow for multiple drivers as each of them implements the
pwm_*() functions.
Hi Sascha,
This looks very nice.
I have only trivial comments, except for the suggestion to use idr.
+
+/*
+ * The next pwm id to assign. We do not bother to fill gaps which
+ * occur during dynamic registering/deregistering of pwms, but
+ * instead assign a uniq id to each new pwm.
unique
+ */
+static int next_pwm_id;
How about using idr? It provides you a fast lookup and handles giving
out unique numbers.
+
+/**
+ * pwmchip_reserve() - reserve range of pwms to use with platform code only
+ * @npwms: number of pwms to reserve
+ * Context: platform init
+ *
+ * Maybe called only once. It reserves the first pwm_ids for platform use so
I assume you mean "May only be called once".
+/**
+ * struct pwm_ops - PWM operations
+ * @request: optional hook for requesting a PWM
+ * @free: optional hook for freeing a PWM
+ * @config: configure duty cycles and period length for this PWM
+ * @enable: enable PWM output toggling
+ * @disable: disable PWM output toggling
+ */
+struct pwm_ops {
+ int (*request)(struct pwm_chip *chip);
+ void (*free)(struct pwm_chip *chip);
+ int (*config)(struct pwm_chip *chip, int duty_ns,
+ int period_ns);
+ int (*enable)(struct pwm_chip *chip);
+ void (*disable)(struct pwm_chip *chip);
+};
+/**
+ * struct pwm_chip - abstract a PWM
+ * @label: for diagnostics
+ * @owner: helps prevent removal of modules exporting active PWMs
+ * @ops: The callbacks for this PWM
+ */
+struct pwm_chip {
+ int pwm_id;
+ const char *label;
+ struct module *owner;
+ struct pwm_ops *ops;
+};
I think the "owner" field should be in the pwm_ops, not in the pwm_chip,
because the pwm_ops is likely to be statically allocated, while the
pwm_chip is probably runtime allocated and cannot be preinitialized.
Should a pwm_chip contain a "struct device" so you can refer to it in
sysfs and add attributes?
Arnd
The driver looks pretty good overall, but the global pwm_base_common register
is rather ugly. I think this should really be passed down through resources
from the platform device in one way or another.
Arnd
On Tue, Jun 28, 2011 at 02:27:04PM +0200, Arnd Bergmann wrote:
On Tuesday 28 June 2011, Sascha Hauer wrote:
quoted
This patch adds framework support for PWM (pulse width modulation) devices.
The is a barebone PWM API already in the kernel under include/linux/pwm.h,
but it does not allow for multiple drivers as each of them implements the
pwm_*() functions.
Hi Sascha,
This looks very nice.
I have only trivial comments, except for the suggestion to use idr.
+
+/*
+ * The next pwm id to assign. We do not bother to fill gaps which
+ * occur during dynamic registering/deregistering of pwms, but
+ * instead assign a uniq id to each new pwm.
unique
quoted
+ */
+static int next_pwm_id;
How about using idr? It provides you a fast lookup and handles giving
out unique numbers.
Sounds like a good idea, but is idr compatible with the pwmchip_reserve
function below? idr gives no guarantee that the first id is zero, but I
need exactly this to make the ids for the internal PWMs known at compile
time.
(In the longer term it probably makes sense to implement a
pwm_get(struct device *, const char *id) similar to the clk api, I just
wanted to stay compatible to the current PWM API for now)
Sascha
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
How about using idr? It provides you a fast lookup and handles giving
out unique numbers.
Sounds like a good idea, but is idr compatible with the pwmchip_reserve
function below? idr gives no guarantee that the first id is zero, but I
need exactly this to make the ids for the internal PWMs known at compile
time.
You can certainly use ida_get_new_above to guarantee that implicit
number allocation doesn't interfere with the reserved numbers. Whether
that guarantees that you get the reserved numbers when you ask for them,
I don't know.
A possible way to deal with that would be to allocate an array of pointers
for the reserved space but use IDR for the rest, but at that point the
complexity is probably higher than what you have now ;-)
Another question is whether the dynamic allocation even makes sense. How
does a driver that wants to use a PWM find out what number to ask for
when it's not reserved for the platform to start with. Maybe the
answer is to just use an array or tree for the lookup and refuse to
register multiple PWMs to the same number.
Arnd
On Tue, Jun 28, 2011 at 07:03:05PM +0200, Arnd Bergmann wrote:
On Tuesday 28 June 2011, Sascha Hauer wrote:
quoted
quoted
How about using idr? It provides you a fast lookup and handles giving
out unique numbers.
Sounds like a good idea, but is idr compatible with the pwmchip_reserve
function below? idr gives no guarantee that the first id is zero, but I
need exactly this to make the ids for the internal PWMs known at compile
time.
You can certainly use ida_get_new_above to guarantee that implicit
number allocation doesn't interfere with the reserved numbers. Whether
that guarantees that you get the reserved numbers when you ask for them,
I don't know.
A possible way to deal with that would be to allocate an array of pointers
for the reserved space but use IDR for the rest, but at that point the
complexity is probably higher than what you have now ;-)
Another question is whether the dynamic allocation even makes sense. How
does a driver that wants to use a PWM find out what number to ask for
when it's not reserved for the platform to start with. Maybe the
answer is to just use an array or tree for the lookup and refuse to
register multiple PWMs to the same number.
I tend to remove the dynamic allocation support for now. Currently we
have two types of pwm drivers in the tree. The SoC internal PWMs can
cope with a fixed id so that platforms can pass the id to the pwm
function driver (backlight, led).
The other type is some mfd drivers which implement the pwm API themselves
right now. These are currently incompatible with the pwm framework anyway.
If a board wants to connect the mfd pwm to its backlight, the current
pwm_request based on an integer id is not suitable anymore. We need
a (struct device *dev, char *id) based pwm_request function then.
So my plan is to change the pwm API once we have all in tree drivers
in drivers/pwm.
Sascha
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
The other type is some mfd drivers which implement the pwm API themselves
right now. These are currently incompatible with the pwm framework anyway.
If a board wants to connect the mfd pwm to its backlight, the current
pwm_request based on an integer id is not suitable anymore. We need
a (struct device *dev, char *id) based pwm_request function then.
So my plan is to change the pwm API once we have all in tree drivers
in drivers/pwm.
Ok, sounds good. I don't much like the idea of using a string identifier
for this, but let's discuss that when we get there. Using fixed numbers
for the first version seems totally reasonable, as it keeps the existing
API.
Arnd