pwm: Add support referencing PWMs from ACPI

In analogy to referencing a GPIO using the "gpios" property from ACPI,
support referencing a PWM using the "pwms" property.

ACPI entries must look like
 Package () {"pwms", Package ()
     { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}

In contrast to the DT implementation, only _one_ PWM entry in the "pwms"
property is supported. As a consequence "pwm-names"-property and
con_id lookup aren't supported.

Support for ACPI is added via the firmware-node framework which is an
abstraction layer on top of ACPI/DT. To keep this patch clean, DT and
ACPI paths are kept separate. The firmware-node framework could be used
to unify both paths in a future patch.

To support leds-pwm driver, an additional method devm_fwnode_pwm_get()
which supports both ACPI and DT configuration is exported.

Signed-off-by: Nikolaus Voss <nikolaus.voss@loewensteinmedical.de>
[thierry.reding@gmail.com: fix build failures for !ACPI]
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
This commit is contained in:
Nikolaus Voss 2019-06-12 10:36:07 +02:00 committed by Thierry Reding
parent 4a5fa56cc0
commit 4a6ef8e37c
2 changed files with 132 additions and 0 deletions

View File

@ -19,6 +19,7 @@
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/acpi.h>
#include <linux/module.h>
#include <linux/pwm.h>
#include <linux/radix-tree.h>
@ -750,6 +751,85 @@ struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
}
EXPORT_SYMBOL_GPL(of_pwm_get);
#if IS_ENABLED(CONFIG_ACPI)
static struct pwm_chip *device_to_pwmchip(struct device *dev)
{
struct pwm_chip *chip;
mutex_lock(&pwm_lock);
list_for_each_entry(chip, &pwm_chips, list) {
struct acpi_device *adev = ACPI_COMPANION(chip->dev);
if ((chip->dev == dev) || (adev && &adev->dev == dev)) {
mutex_unlock(&pwm_lock);
return chip;
}
}
mutex_unlock(&pwm_lock);
return ERR_PTR(-EPROBE_DEFER);
}
#endif
/**
* acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
* @fwnode: firmware node to get the "pwm" property from
*
* Returns the PWM device parsed from the fwnode and index specified in the
* "pwms" property or a negative error-code on failure.
* Values parsed from the device tree are stored in the returned PWM device
* object.
*
* This is analogous to of_pwm_get() except con_id is not yet supported.
* ACPI entries must look like
* Package () {"pwms", Package ()
* { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
*
* Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
* error code on failure.
*/
static struct pwm_device *acpi_pwm_get(struct fwnode_handle *fwnode)
{
struct pwm_device *pwm = ERR_PTR(-ENODEV);
#if IS_ENABLED(CONFIG_ACPI)
struct fwnode_reference_args args;
struct acpi_device *acpi;
struct pwm_chip *chip;
int ret;
memset(&args, 0, sizeof(args));
ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
if (ret < 0)
return ERR_PTR(ret);
acpi = to_acpi_device_node(args.fwnode);
if (!acpi)
return ERR_PTR(-EINVAL);
if (args.nargs < 2)
return ERR_PTR(-EPROTO);
chip = device_to_pwmchip(&acpi->dev);
if (IS_ERR(chip))
return ERR_CAST(chip);
pwm = pwm_request_from_chip(chip, args.args[0], NULL);
if (IS_ERR(pwm))
return pwm;
pwm->args.period = args.args[1];
pwm->args.polarity = PWM_POLARITY_NORMAL;
if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
pwm->args.polarity = PWM_POLARITY_INVERSED;
#endif
return pwm;
}
/**
* pwm_add_table() - register PWM device consumers
* @table: array of consumers to register
@ -814,6 +894,10 @@ struct pwm_device *pwm_get(struct device *dev, const char *con_id)
if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
return of_pwm_get(dev, dev->of_node, con_id);
/* then lookup via ACPI */
if (dev && is_acpi_node(dev->fwnode))
return acpi_pwm_get(dev->fwnode);
/*
* We look up the provider in the static table typically provided by
* board setup code. We first try to lookup the consumer device by
@ -999,6 +1083,44 @@ struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
}
EXPORT_SYMBOL_GPL(devm_of_pwm_get);
/**
* devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
* @dev: device for PWM consumer
* @fwnode: firmware node to get the PWM from
* @con_id: consumer name
*
* Returns the PWM device parsed from the firmware node. See of_pwm_get() and
* acpi_pwm_get() for a detailed description.
*
* Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
* error code on failure.
*/
struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
struct fwnode_handle *fwnode,
const char *con_id)
{
struct pwm_device **ptr, *pwm = ERR_PTR(-ENODEV);
ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return ERR_PTR(-ENOMEM);
if (is_of_node(fwnode))
pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
else if (is_acpi_node(fwnode))
pwm = acpi_pwm_get(fwnode);
if (!IS_ERR(pwm)) {
*ptr = pwm;
devres_add(dev, ptr);
} else {
devres_free(ptr);
}
return pwm;
}
EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
static int devm_pwm_match(struct device *dev, void *res, void *data)
{
struct pwm_device **p = res;

View File

@ -412,6 +412,9 @@ void pwm_put(struct pwm_device *pwm);
struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
const char *con_id);
struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
struct fwnode_handle *fwnode,
const char *con_id);
void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
#else
static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
@ -518,6 +521,13 @@ static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
return ERR_PTR(-ENODEV);
}
static inline struct pwm_device *
devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
const char *con_id)
{
return ERR_PTR(-ENODEV);
}
static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
{
}