2019-05-19 21:51:48 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-01-28 16:40:40 +08:00
|
|
|
/*
|
|
|
|
* Generic pwmlib implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
|
2011-12-14 18:12:23 +08:00
|
|
|
* Copyright (C) 2011-2012 Avionic Design GmbH
|
2011-01-28 16:40:40 +08:00
|
|
|
*/
|
|
|
|
|
2019-06-12 16:36:07 +08:00
|
|
|
#include <linux/acpi.h>
|
2011-01-28 16:40:40 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/pwm.h>
|
2011-12-14 18:12:23 +08:00
|
|
|
#include <linux/radix-tree.h>
|
2011-01-28 16:40:40 +08:00
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/device.h>
|
2012-03-26 15:31:48 +08:00
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/seq_file.h>
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2013-07-18 06:54:22 +08:00
|
|
|
#include <dt-bindings/pwm/pwm.h>
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2019-10-24 16:08:29 +08:00
|
|
|
#define CREATE_TRACE_POINTS
|
|
|
|
#include <trace/events/pwm.h>
|
|
|
|
|
2013-07-18 06:54:22 +08:00
|
|
|
#define MAX_PWMS 1024
|
2012-11-21 15:40:44 +08:00
|
|
|
|
2012-03-26 14:42:48 +08:00
|
|
|
static DEFINE_MUTEX(pwm_lookup_lock);
|
|
|
|
static LIST_HEAD(pwm_lookup_list);
|
2022-12-03 02:35:09 +08:00
|
|
|
|
2023-04-13 20:25:48 +08:00
|
|
|
/* protects access to pwm_chips and allocated_pwms */
|
2011-01-28 16:40:40 +08:00
|
|
|
static DEFINE_MUTEX(pwm_lock);
|
2022-12-03 02:35:09 +08:00
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
static LIST_HEAD(pwm_chips);
|
|
|
|
static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2022-12-03 02:35:09 +08:00
|
|
|
/* Called with pwm_lock held */
|
2021-03-02 02:57:19 +08:00
|
|
|
static int alloc_pwms(unsigned int count)
|
2011-12-14 18:12:23 +08:00
|
|
|
{
|
|
|
|
unsigned int start;
|
|
|
|
|
2021-03-02 02:57:19 +08:00
|
|
|
start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, 0,
|
2011-12-14 18:12:23 +08:00
|
|
|
count, 0);
|
|
|
|
|
|
|
|
if (start + count > MAX_PWMS)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
2022-12-03 02:35:11 +08:00
|
|
|
bitmap_set(allocated_pwms, start, count);
|
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2022-12-03 02:35:09 +08:00
|
|
|
/* Called with pwm_lock held */
|
2011-12-14 18:12:23 +08:00
|
|
|
static void free_pwms(struct pwm_chip *chip)
|
|
|
|
{
|
|
|
|
bitmap_clear(allocated_pwms, chip->base, chip->npwm);
|
|
|
|
|
|
|
|
kfree(chip->pwms);
|
|
|
|
chip->pwms = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-26 14:42:48 +08:00
|
|
|
static struct pwm_chip *pwmchip_find_by_name(const char *name)
|
|
|
|
{
|
|
|
|
struct pwm_chip *chip;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mutex_lock(&pwm_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(chip, &pwm_chips, list) {
|
|
|
|
const char *chip_name = dev_name(chip->dev);
|
|
|
|
|
|
|
|
if (chip_name && strcmp(chip_name, name) == 0) {
|
|
|
|
mutex_unlock(&pwm_lock);
|
|
|
|
return chip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&pwm_lock);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
static int pwm_device_request(struct pwm_device *pwm, const char *label)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (test_bit(PWMF_REQUESTED, &pwm->flags))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
if (!try_module_get(pwm->chip->ops->owner))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (pwm->chip->ops->request) {
|
|
|
|
err = pwm->chip->ops->request(pwm->chip, pwm);
|
|
|
|
if (err) {
|
|
|
|
module_put(pwm->chip->ops->owner);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:08:29 +08:00
|
|
|
if (pwm->chip->ops->get_state) {
|
2023-03-23 05:45:45 +08:00
|
|
|
/*
|
|
|
|
* Zero-initialize state because most drivers are unaware of
|
|
|
|
* .usage_power. The other members of state are supposed to be
|
|
|
|
* set by lowlevel drivers. We still initialize the whole
|
|
|
|
* structure for simplicity even though this might paper over
|
|
|
|
* faulty implementations of .get_state().
|
|
|
|
*/
|
|
|
|
struct pwm_state state = { 0, };
|
2022-12-03 02:35:36 +08:00
|
|
|
|
|
|
|
err = pwm->chip->ops->get_state(pwm->chip, pwm, &state);
|
|
|
|
trace_pwm_get(pwm, &state, err);
|
|
|
|
|
|
|
|
if (!err)
|
|
|
|
pwm->state = state;
|
2020-02-11 05:35:18 +08:00
|
|
|
|
2020-06-04 06:40:56 +08:00
|
|
|
if (IS_ENABLED(CONFIG_PWM_DEBUG))
|
2020-02-11 05:35:18 +08:00
|
|
|
pwm->last = pwm->state;
|
2019-10-24 16:08:29 +08:00
|
|
|
}
|
2019-10-21 18:51:56 +08:00
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
set_bit(PWMF_REQUESTED, &pwm->flags);
|
|
|
|
pwm->label = label;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 15:40:44 +08:00
|
|
|
struct pwm_device *
|
2023-07-15 04:56:14 +08:00
|
|
|
of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
|
2012-11-21 15:40:44 +08:00
|
|
|
{
|
|
|
|
struct pwm_device *pwm;
|
|
|
|
|
2023-07-15 04:56:14 +08:00
|
|
|
if (chip->of_pwm_n_cells < 2)
|
2012-11-21 15:40:44 +08:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2017-01-30 05:54:13 +08:00
|
|
|
/* flags in the third cell are optional */
|
|
|
|
if (args->args_count < 2)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2023-07-15 04:56:14 +08:00
|
|
|
if (args->args[0] >= chip->npwm)
|
2012-11-21 15:40:44 +08:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2023-07-15 04:56:14 +08:00
|
|
|
pwm = pwm_request_from_chip(chip, args->args[0], NULL);
|
2012-11-21 15:40:44 +08:00
|
|
|
if (IS_ERR(pwm))
|
|
|
|
return pwm;
|
|
|
|
|
2016-04-15 03:17:21 +08:00
|
|
|
pwm->args.period = args->args[1];
|
2017-01-30 05:54:13 +08:00
|
|
|
pwm->args.polarity = PWM_POLARITY_NORMAL;
|
2012-11-21 15:40:44 +08:00
|
|
|
|
2023-07-15 04:56:14 +08:00
|
|
|
if (chip->of_pwm_n_cells >= 3) {
|
2021-05-10 22:06:37 +08:00
|
|
|
if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
|
|
|
|
pwm->args.polarity = PWM_POLARITY_INVERSED;
|
|
|
|
}
|
2012-11-21 15:40:44 +08:00
|
|
|
|
|
|
|
return pwm;
|
|
|
|
}
|
2012-11-28 22:12:07 +08:00
|
|
|
EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
|
2012-11-21 15:40:44 +08:00
|
|
|
|
2021-10-26 01:09:23 +08:00
|
|
|
struct pwm_device *
|
2023-07-15 04:56:14 +08:00
|
|
|
of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
|
2021-10-26 01:09:23 +08:00
|
|
|
{
|
|
|
|
struct pwm_device *pwm;
|
|
|
|
|
2023-07-15 04:56:14 +08:00
|
|
|
if (chip->of_pwm_n_cells < 1)
|
2021-10-26 01:09:23 +08:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
/* validate that one cell is specified, optionally with flags */
|
|
|
|
if (args->args_count != 1 && args->args_count != 2)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2023-07-15 04:56:14 +08:00
|
|
|
pwm = pwm_request_from_chip(chip, 0, NULL);
|
2021-10-26 01:09:23 +08:00
|
|
|
if (IS_ERR(pwm))
|
|
|
|
return pwm;
|
|
|
|
|
|
|
|
pwm->args.period = args->args[0];
|
|
|
|
pwm->args.polarity = PWM_POLARITY_NORMAL;
|
|
|
|
|
|
|
|
if (args->args_count == 2 && args->args[2] & PWM_POLARITY_INVERTED)
|
|
|
|
pwm->args.polarity = PWM_POLARITY_INVERSED;
|
|
|
|
|
|
|
|
return pwm;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
|
|
|
|
|
2012-08-02 15:02:42 +08:00
|
|
|
static void of_pwmchip_add(struct pwm_chip *chip)
|
2011-12-14 18:10:32 +08:00
|
|
|
{
|
|
|
|
if (!chip->dev || !chip->dev->of_node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!chip->of_xlate) {
|
2021-05-10 22:06:39 +08:00
|
|
|
u32 pwm_cells;
|
|
|
|
|
|
|
|
if (of_property_read_u32(chip->dev->of_node, "#pwm-cells",
|
|
|
|
&pwm_cells))
|
|
|
|
pwm_cells = 2;
|
|
|
|
|
2021-05-10 22:06:38 +08:00
|
|
|
chip->of_xlate = of_pwm_xlate_with_flags;
|
2021-05-10 22:06:39 +08:00
|
|
|
chip->of_pwm_n_cells = pwm_cells;
|
2011-12-14 18:10:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
of_node_get(chip->dev->of_node);
|
|
|
|
}
|
|
|
|
|
2012-08-02 15:02:42 +08:00
|
|
|
static void of_pwmchip_remove(struct pwm_chip *chip)
|
2011-12-14 18:10:32 +08:00
|
|
|
{
|
2015-02-03 18:54:28 +08:00
|
|
|
if (chip->dev)
|
2011-12-14 18:10:32 +08:00
|
|
|
of_node_put(chip->dev->of_node);
|
|
|
|
}
|
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
/**
|
|
|
|
* pwm_set_chip_data() - set private chip data for a PWM
|
|
|
|
* @pwm: PWM device
|
|
|
|
* @data: pointer to chip-specific data
|
2015-07-27 17:58:32 +08:00
|
|
|
*
|
|
|
|
* Returns: 0 on success or a negative error code on failure.
|
2011-12-14 18:12:23 +08:00
|
|
|
*/
|
|
|
|
int pwm_set_chip_data(struct pwm_device *pwm, void *data)
|
|
|
|
{
|
|
|
|
if (!pwm)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
pwm->chip_data = data;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 16:22:24 +08:00
|
|
|
EXPORT_SYMBOL_GPL(pwm_set_chip_data);
|
2011-12-14 18:12:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* pwm_get_chip_data() - get private chip data for a PWM
|
|
|
|
* @pwm: PWM device
|
2015-07-27 17:58:32 +08:00
|
|
|
*
|
|
|
|
* Returns: A pointer to the chip-private data for the PWM device.
|
2011-12-14 18:12:23 +08:00
|
|
|
*/
|
|
|
|
void *pwm_get_chip_data(struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
return pwm ? pwm->chip_data : NULL;
|
2011-01-28 16:40:40 +08:00
|
|
|
}
|
2013-01-30 16:22:24 +08:00
|
|
|
EXPORT_SYMBOL_GPL(pwm_get_chip_data);
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2020-02-11 05:35:18 +08:00
|
|
|
static bool pwm_ops_check(const struct pwm_chip *chip)
|
2016-04-15 03:17:41 +08:00
|
|
|
{
|
2020-02-11 05:35:18 +08:00
|
|
|
const struct pwm_ops *ops = chip->ops;
|
|
|
|
|
|
|
|
if (!ops->apply)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
|
|
|
|
dev_warn(chip->dev,
|
|
|
|
"Please implement the .get_state() callback\n");
|
2016-04-15 03:17:41 +08:00
|
|
|
|
2020-02-11 05:35:18 +08:00
|
|
|
return true;
|
2016-04-15 03:17:41 +08:00
|
|
|
}
|
|
|
|
|
2011-01-28 16:40:40 +08:00
|
|
|
/**
|
2020-12-07 21:45:56 +08:00
|
|
|
* pwmchip_add() - register a new PWM chip
|
2011-01-28 16:40:40 +08:00
|
|
|
* @chip: the PWM chip to add
|
2011-12-14 18:12:23 +08:00
|
|
|
*
|
2020-12-07 21:45:56 +08:00
|
|
|
* Register a new PWM chip.
|
2015-07-27 17:58:32 +08:00
|
|
|
*
|
|
|
|
* Returns: 0 on success or a negative error code on failure.
|
2011-01-28 16:40:40 +08:00
|
|
|
*/
|
2020-12-07 21:45:56 +08:00
|
|
|
int pwmchip_add(struct pwm_chip *chip)
|
2011-01-28 16:40:40 +08:00
|
|
|
{
|
|
|
|
struct pwm_device *pwm;
|
2011-12-14 18:12:23 +08:00
|
|
|
unsigned int i;
|
|
|
|
int ret;
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2016-04-15 03:17:41 +08:00
|
|
|
if (!chip || !chip->dev || !chip->ops || !chip->npwm)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-02-11 05:35:18 +08:00
|
|
|
if (!pwm_ops_check(chip))
|
2011-12-14 18:12:23 +08:00
|
|
|
return -EINVAL;
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2022-12-03 02:35:10 +08:00
|
|
|
chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
|
|
|
|
if (!chip->pwms)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-01-28 16:40:40 +08:00
|
|
|
mutex_lock(&pwm_lock);
|
|
|
|
|
2021-03-02 02:57:19 +08:00
|
|
|
ret = alloc_pwms(chip->npwm);
|
2022-12-03 02:35:10 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
mutex_unlock(&pwm_lock);
|
|
|
|
kfree(chip->pwms);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-12-14 18:12:23 +08:00
|
|
|
|
2021-03-02 02:57:19 +08:00
|
|
|
chip->base = ret;
|
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
for (i = 0; i < chip->npwm; i++) {
|
|
|
|
pwm = &chip->pwms[i];
|
|
|
|
|
|
|
|
pwm->chip = chip;
|
|
|
|
pwm->pwm = chip->base + i;
|
|
|
|
pwm->hwpwm = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add(&chip->list, &pwm_chips);
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2022-12-03 02:35:10 +08:00
|
|
|
mutex_unlock(&pwm_lock);
|
2011-12-14 18:12:23 +08:00
|
|
|
|
2011-12-14 18:10:32 +08:00
|
|
|
if (IS_ENABLED(CONFIG_OF))
|
|
|
|
of_pwmchip_add(chip);
|
|
|
|
|
2022-12-03 02:35:10 +08:00
|
|
|
pwmchip_sysfs_export(chip);
|
pwm: Fix deadlock warning when removing PWM device
This patch fixes deadlock warning if removing PWM device
when CONFIG_PROVE_LOCKING is enabled.
This issue can be reproceduced by the following steps on
the R-Car H3 Salvator-X board if the backlight is disabled:
# cd /sys/class/pwm/pwmchip0
# echo 0 > export
# ls
device export npwm power pwm0 subsystem uevent unexport
# cd device/driver
# ls
bind e6e31000.pwm uevent unbind
# echo e6e31000.pwm > unbind
[ 87.659974] ======================================================
[ 87.666149] WARNING: possible circular locking dependency detected
[ 87.672327] 5.0.0 #7 Not tainted
[ 87.675549] ------------------------------------------------------
[ 87.681723] bash/2986 is trying to acquire lock:
[ 87.686337] 000000005ea0e178 (kn->count#58){++++}, at: kernfs_remove_by_name_ns+0x50/0xa0
[ 87.694528]
[ 87.694528] but task is already holding lock:
[ 87.700353] 000000006313b17c (pwm_lock){+.+.}, at: pwmchip_remove+0x28/0x13c
[ 87.707405]
[ 87.707405] which lock already depends on the new lock.
[ 87.707405]
[ 87.715574]
[ 87.715574] the existing dependency chain (in reverse order) is:
[ 87.723048]
[ 87.723048] -> #1 (pwm_lock){+.+.}:
[ 87.728017] __mutex_lock+0x70/0x7e4
[ 87.732108] mutex_lock_nested+0x1c/0x24
[ 87.736547] pwm_request_from_chip.part.6+0x34/0x74
[ 87.741940] pwm_request_from_chip+0x20/0x40
[ 87.746725] export_store+0x6c/0x1f4
[ 87.750820] dev_attr_store+0x18/0x28
[ 87.754998] sysfs_kf_write+0x54/0x64
[ 87.759175] kernfs_fop_write+0xe4/0x1e8
[ 87.763615] __vfs_write+0x40/0x184
[ 87.767619] vfs_write+0xa8/0x19c
[ 87.771448] ksys_write+0x58/0xbc
[ 87.775278] __arm64_sys_write+0x18/0x20
[ 87.779721] el0_svc_common+0xd0/0x124
[ 87.783986] el0_svc_compat_handler+0x1c/0x24
[ 87.788858] el0_svc_compat+0x8/0x18
[ 87.792947]
[ 87.792947] -> #0 (kn->count#58){++++}:
[ 87.798260] lock_acquire+0xc4/0x22c
[ 87.802353] __kernfs_remove+0x258/0x2c4
[ 87.806790] kernfs_remove_by_name_ns+0x50/0xa0
[ 87.811836] remove_files.isra.1+0x38/0x78
[ 87.816447] sysfs_remove_group+0x48/0x98
[ 87.820971] sysfs_remove_groups+0x34/0x4c
[ 87.825583] device_remove_attrs+0x6c/0x7c
[ 87.830197] device_del+0x11c/0x33c
[ 87.834201] device_unregister+0x14/0x2c
[ 87.838638] pwmchip_sysfs_unexport+0x40/0x4c
[ 87.843509] pwmchip_remove+0xf4/0x13c
[ 87.847773] rcar_pwm_remove+0x28/0x34
[ 87.852039] platform_drv_remove+0x24/0x64
[ 87.856651] device_release_driver_internal+0x18c/0x21c
[ 87.862391] device_release_driver+0x14/0x1c
[ 87.867175] unbind_store+0xe0/0x124
[ 87.871265] drv_attr_store+0x20/0x30
[ 87.875442] sysfs_kf_write+0x54/0x64
[ 87.879618] kernfs_fop_write+0xe4/0x1e8
[ 87.884055] __vfs_write+0x40/0x184
[ 87.888057] vfs_write+0xa8/0x19c
[ 87.891887] ksys_write+0x58/0xbc
[ 87.895716] __arm64_sys_write+0x18/0x20
[ 87.900154] el0_svc_common+0xd0/0x124
[ 87.904417] el0_svc_compat_handler+0x1c/0x24
[ 87.909289] el0_svc_compat+0x8/0x18
[ 87.913378]
[ 87.913378] other info that might help us debug this:
[ 87.913378]
[ 87.921374] Possible unsafe locking scenario:
[ 87.921374]
[ 87.927286] CPU0 CPU1
[ 87.931808] ---- ----
[ 87.936331] lock(pwm_lock);
[ 87.939293] lock(kn->count#58);
[ 87.945120] lock(pwm_lock);
[ 87.950599] lock(kn->count#58);
[ 87.953908]
[ 87.953908] *** DEADLOCK ***
[ 87.953908]
[ 87.959821] 4 locks held by bash/2986:
[ 87.963563] #0: 00000000ace7bc30 (sb_writers#6){.+.+}, at: vfs_write+0x188/0x19c
[ 87.971044] #1: 00000000287991b2 (&of->mutex){+.+.}, at: kernfs_fop_write+0xb4/0x1e8
[ 87.978872] #2: 00000000f739d016 (&dev->mutex){....}, at: device_release_driver_internal+0x40/0x21c
[ 87.988001] #3: 000000006313b17c (pwm_lock){+.+.}, at: pwmchip_remove+0x28/0x13c
[ 87.995481]
[ 87.995481] stack backtrace:
[ 87.999836] CPU: 0 PID: 2986 Comm: bash Not tainted 5.0.0 #7
[ 88.005489] Hardware name: Renesas Salvator-X board based on r8a7795 ES1.x (DT)
[ 88.012791] Call trace:
[ 88.015235] dump_backtrace+0x0/0x190
[ 88.018891] show_stack+0x14/0x1c
[ 88.022204] dump_stack+0xb0/0xec
[ 88.025514] print_circular_bug.isra.32+0x1d0/0x2e0
[ 88.030385] __lock_acquire+0x1318/0x1864
[ 88.034388] lock_acquire+0xc4/0x22c
[ 88.037958] __kernfs_remove+0x258/0x2c4
[ 88.041874] kernfs_remove_by_name_ns+0x50/0xa0
[ 88.046398] remove_files.isra.1+0x38/0x78
[ 88.050487] sysfs_remove_group+0x48/0x98
[ 88.054490] sysfs_remove_groups+0x34/0x4c
[ 88.058580] device_remove_attrs+0x6c/0x7c
[ 88.062671] device_del+0x11c/0x33c
[ 88.066154] device_unregister+0x14/0x2c
[ 88.070070] pwmchip_sysfs_unexport+0x40/0x4c
[ 88.074421] pwmchip_remove+0xf4/0x13c
[ 88.078163] rcar_pwm_remove+0x28/0x34
[ 88.081906] platform_drv_remove+0x24/0x64
[ 88.085996] device_release_driver_internal+0x18c/0x21c
[ 88.091215] device_release_driver+0x14/0x1c
[ 88.095478] unbind_store+0xe0/0x124
[ 88.099048] drv_attr_store+0x20/0x30
[ 88.102704] sysfs_kf_write+0x54/0x64
[ 88.106359] kernfs_fop_write+0xe4/0x1e8
[ 88.110275] __vfs_write+0x40/0x184
[ 88.113757] vfs_write+0xa8/0x19c
[ 88.117065] ksys_write+0x58/0xbc
[ 88.120374] __arm64_sys_write+0x18/0x20
[ 88.124291] el0_svc_common+0xd0/0x124
[ 88.128034] el0_svc_compat_handler+0x1c/0x24
[ 88.132384] el0_svc_compat+0x8/0x18
The sysfs unexport in pwmchip_remove() is completely asymmetric
to what we do in pwmchip_add_with_polarity() and commit 0733424c9ba9
("pwm: Unexport children before chip removal") is a strong indication
that this was wrong to begin with. We should just move
pwmchip_sysfs_unexport() where it belongs, which is right after
pwmchip_sysfs_unexport_children(). In that case, we do not need
separate functions anymore either.
We also really want to remove sysfs irrespective of whether or not
the chip will be removed as a result of pwmchip_remove(). We can only
assume that the driver will be gone after that, so we shouldn't leave
any dangling sysfs files around.
This warning disappears if we move pwmchip_sysfs_unexport() to
the top of pwmchip_remove(), pwmchip_sysfs_unexport_children().
That way it is also outside of the pwm_lock section, which indeed
doesn't seem to be needed.
Moving the pwmchip_sysfs_export() call outside of that section also
seems fine and it'd be perfectly symmetric with pwmchip_remove() again.
So, this patch fixes them.
Signed-off-by: Phong Hoang <phong.hoang.wz@renesas.com>
[shimoda: revise the commit log and code]
Fixes: 76abbdde2d95 ("pwm: Add sysfs interface")
Fixes: 0733424c9ba9 ("pwm: Unexport children before chip removal")
Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
Tested-by: Hoan Nguyen An <na-hoan@jinso.co.jp>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Reviewed-by: Simon Horman <horms+renesas@verge.net.au>
Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2019-03-19 18:40:08 +08:00
|
|
|
|
2022-12-03 02:35:10 +08:00
|
|
|
return 0;
|
2011-01-28 16:40:40 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(pwmchip_add);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pwmchip_remove() - remove a PWM chip
|
|
|
|
* @chip: the PWM chip to remove
|
|
|
|
*
|
|
|
|
* Removes a PWM chip. This function may return busy if the PWM chip provides
|
|
|
|
* a PWM device that is still requested.
|
2015-07-27 17:58:32 +08:00
|
|
|
*
|
|
|
|
* Returns: 0 on success or a negative error code on failure.
|
2011-01-28 16:40:40 +08:00
|
|
|
*/
|
2021-07-08 00:28:35 +08:00
|
|
|
void pwmchip_remove(struct pwm_chip *chip)
|
2011-01-28 16:40:40 +08:00
|
|
|
{
|
pwm: Fix deadlock warning when removing PWM device
This patch fixes deadlock warning if removing PWM device
when CONFIG_PROVE_LOCKING is enabled.
This issue can be reproceduced by the following steps on
the R-Car H3 Salvator-X board if the backlight is disabled:
# cd /sys/class/pwm/pwmchip0
# echo 0 > export
# ls
device export npwm power pwm0 subsystem uevent unexport
# cd device/driver
# ls
bind e6e31000.pwm uevent unbind
# echo e6e31000.pwm > unbind
[ 87.659974] ======================================================
[ 87.666149] WARNING: possible circular locking dependency detected
[ 87.672327] 5.0.0 #7 Not tainted
[ 87.675549] ------------------------------------------------------
[ 87.681723] bash/2986 is trying to acquire lock:
[ 87.686337] 000000005ea0e178 (kn->count#58){++++}, at: kernfs_remove_by_name_ns+0x50/0xa0
[ 87.694528]
[ 87.694528] but task is already holding lock:
[ 87.700353] 000000006313b17c (pwm_lock){+.+.}, at: pwmchip_remove+0x28/0x13c
[ 87.707405]
[ 87.707405] which lock already depends on the new lock.
[ 87.707405]
[ 87.715574]
[ 87.715574] the existing dependency chain (in reverse order) is:
[ 87.723048]
[ 87.723048] -> #1 (pwm_lock){+.+.}:
[ 87.728017] __mutex_lock+0x70/0x7e4
[ 87.732108] mutex_lock_nested+0x1c/0x24
[ 87.736547] pwm_request_from_chip.part.6+0x34/0x74
[ 87.741940] pwm_request_from_chip+0x20/0x40
[ 87.746725] export_store+0x6c/0x1f4
[ 87.750820] dev_attr_store+0x18/0x28
[ 87.754998] sysfs_kf_write+0x54/0x64
[ 87.759175] kernfs_fop_write+0xe4/0x1e8
[ 87.763615] __vfs_write+0x40/0x184
[ 87.767619] vfs_write+0xa8/0x19c
[ 87.771448] ksys_write+0x58/0xbc
[ 87.775278] __arm64_sys_write+0x18/0x20
[ 87.779721] el0_svc_common+0xd0/0x124
[ 87.783986] el0_svc_compat_handler+0x1c/0x24
[ 87.788858] el0_svc_compat+0x8/0x18
[ 87.792947]
[ 87.792947] -> #0 (kn->count#58){++++}:
[ 87.798260] lock_acquire+0xc4/0x22c
[ 87.802353] __kernfs_remove+0x258/0x2c4
[ 87.806790] kernfs_remove_by_name_ns+0x50/0xa0
[ 87.811836] remove_files.isra.1+0x38/0x78
[ 87.816447] sysfs_remove_group+0x48/0x98
[ 87.820971] sysfs_remove_groups+0x34/0x4c
[ 87.825583] device_remove_attrs+0x6c/0x7c
[ 87.830197] device_del+0x11c/0x33c
[ 87.834201] device_unregister+0x14/0x2c
[ 87.838638] pwmchip_sysfs_unexport+0x40/0x4c
[ 87.843509] pwmchip_remove+0xf4/0x13c
[ 87.847773] rcar_pwm_remove+0x28/0x34
[ 87.852039] platform_drv_remove+0x24/0x64
[ 87.856651] device_release_driver_internal+0x18c/0x21c
[ 87.862391] device_release_driver+0x14/0x1c
[ 87.867175] unbind_store+0xe0/0x124
[ 87.871265] drv_attr_store+0x20/0x30
[ 87.875442] sysfs_kf_write+0x54/0x64
[ 87.879618] kernfs_fop_write+0xe4/0x1e8
[ 87.884055] __vfs_write+0x40/0x184
[ 87.888057] vfs_write+0xa8/0x19c
[ 87.891887] ksys_write+0x58/0xbc
[ 87.895716] __arm64_sys_write+0x18/0x20
[ 87.900154] el0_svc_common+0xd0/0x124
[ 87.904417] el0_svc_compat_handler+0x1c/0x24
[ 87.909289] el0_svc_compat+0x8/0x18
[ 87.913378]
[ 87.913378] other info that might help us debug this:
[ 87.913378]
[ 87.921374] Possible unsafe locking scenario:
[ 87.921374]
[ 87.927286] CPU0 CPU1
[ 87.931808] ---- ----
[ 87.936331] lock(pwm_lock);
[ 87.939293] lock(kn->count#58);
[ 87.945120] lock(pwm_lock);
[ 87.950599] lock(kn->count#58);
[ 87.953908]
[ 87.953908] *** DEADLOCK ***
[ 87.953908]
[ 87.959821] 4 locks held by bash/2986:
[ 87.963563] #0: 00000000ace7bc30 (sb_writers#6){.+.+}, at: vfs_write+0x188/0x19c
[ 87.971044] #1: 00000000287991b2 (&of->mutex){+.+.}, at: kernfs_fop_write+0xb4/0x1e8
[ 87.978872] #2: 00000000f739d016 (&dev->mutex){....}, at: device_release_driver_internal+0x40/0x21c
[ 87.988001] #3: 000000006313b17c (pwm_lock){+.+.}, at: pwmchip_remove+0x28/0x13c
[ 87.995481]
[ 87.995481] stack backtrace:
[ 87.999836] CPU: 0 PID: 2986 Comm: bash Not tainted 5.0.0 #7
[ 88.005489] Hardware name: Renesas Salvator-X board based on r8a7795 ES1.x (DT)
[ 88.012791] Call trace:
[ 88.015235] dump_backtrace+0x0/0x190
[ 88.018891] show_stack+0x14/0x1c
[ 88.022204] dump_stack+0xb0/0xec
[ 88.025514] print_circular_bug.isra.32+0x1d0/0x2e0
[ 88.030385] __lock_acquire+0x1318/0x1864
[ 88.034388] lock_acquire+0xc4/0x22c
[ 88.037958] __kernfs_remove+0x258/0x2c4
[ 88.041874] kernfs_remove_by_name_ns+0x50/0xa0
[ 88.046398] remove_files.isra.1+0x38/0x78
[ 88.050487] sysfs_remove_group+0x48/0x98
[ 88.054490] sysfs_remove_groups+0x34/0x4c
[ 88.058580] device_remove_attrs+0x6c/0x7c
[ 88.062671] device_del+0x11c/0x33c
[ 88.066154] device_unregister+0x14/0x2c
[ 88.070070] pwmchip_sysfs_unexport+0x40/0x4c
[ 88.074421] pwmchip_remove+0xf4/0x13c
[ 88.078163] rcar_pwm_remove+0x28/0x34
[ 88.081906] platform_drv_remove+0x24/0x64
[ 88.085996] device_release_driver_internal+0x18c/0x21c
[ 88.091215] device_release_driver+0x14/0x1c
[ 88.095478] unbind_store+0xe0/0x124
[ 88.099048] drv_attr_store+0x20/0x30
[ 88.102704] sysfs_kf_write+0x54/0x64
[ 88.106359] kernfs_fop_write+0xe4/0x1e8
[ 88.110275] __vfs_write+0x40/0x184
[ 88.113757] vfs_write+0xa8/0x19c
[ 88.117065] ksys_write+0x58/0xbc
[ 88.120374] __arm64_sys_write+0x18/0x20
[ 88.124291] el0_svc_common+0xd0/0x124
[ 88.128034] el0_svc_compat_handler+0x1c/0x24
[ 88.132384] el0_svc_compat+0x8/0x18
The sysfs unexport in pwmchip_remove() is completely asymmetric
to what we do in pwmchip_add_with_polarity() and commit 0733424c9ba9
("pwm: Unexport children before chip removal") is a strong indication
that this was wrong to begin with. We should just move
pwmchip_sysfs_unexport() where it belongs, which is right after
pwmchip_sysfs_unexport_children(). In that case, we do not need
separate functions anymore either.
We also really want to remove sysfs irrespective of whether or not
the chip will be removed as a result of pwmchip_remove(). We can only
assume that the driver will be gone after that, so we shouldn't leave
any dangling sysfs files around.
This warning disappears if we move pwmchip_sysfs_unexport() to
the top of pwmchip_remove(), pwmchip_sysfs_unexport_children().
That way it is also outside of the pwm_lock section, which indeed
doesn't seem to be needed.
Moving the pwmchip_sysfs_export() call outside of that section also
seems fine and it'd be perfectly symmetric with pwmchip_remove() again.
So, this patch fixes them.
Signed-off-by: Phong Hoang <phong.hoang.wz@renesas.com>
[shimoda: revise the commit log and code]
Fixes: 76abbdde2d95 ("pwm: Add sysfs interface")
Fixes: 0733424c9ba9 ("pwm: Unexport children before chip removal")
Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
Tested-by: Hoan Nguyen An <na-hoan@jinso.co.jp>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Reviewed-by: Simon Horman <horms+renesas@verge.net.au>
Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2019-03-19 18:40:08 +08:00
|
|
|
pwmchip_sysfs_unexport(chip);
|
2016-08-10 05:57:46 +08:00
|
|
|
|
2011-01-28 16:40:40 +08:00
|
|
|
mutex_lock(&pwm_lock);
|
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
list_del_init(&chip->list);
|
2011-12-14 18:10:32 +08:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_OF))
|
|
|
|
of_pwmchip_remove(chip);
|
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
free_pwms(chip);
|
2011-01-28 16:40:40 +08:00
|
|
|
|
|
|
|
mutex_unlock(&pwm_lock);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(pwmchip_remove);
|
|
|
|
|
2021-04-07 16:01:54 +08:00
|
|
|
static void devm_pwmchip_remove(void *data)
|
|
|
|
{
|
|
|
|
struct pwm_chip *chip = data;
|
|
|
|
|
|
|
|
pwmchip_remove(chip);
|
|
|
|
}
|
|
|
|
|
|
|
|
int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pwmchip_add(chip);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_pwmchip_add);
|
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
/**
|
|
|
|
* pwm_request_from_chip() - request a PWM device relative to a PWM chip
|
|
|
|
* @chip: PWM chip
|
|
|
|
* @index: per-chip index of the PWM to request
|
|
|
|
* @label: a literal description string of this PWM
|
|
|
|
*
|
2015-07-27 17:58:32 +08:00
|
|
|
* Returns: A pointer to the PWM device at the given index of the given PWM
|
|
|
|
* chip. A negative error code is returned if the index is not valid for the
|
|
|
|
* specified PWM chip or if the PWM device cannot be requested.
|
2011-12-14 18:12:23 +08:00
|
|
|
*/
|
|
|
|
struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
|
|
|
|
unsigned int index,
|
|
|
|
const char *label)
|
|
|
|
{
|
|
|
|
struct pwm_device *pwm;
|
|
|
|
int err;
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
if (!chip || index >= chip->npwm)
|
|
|
|
return ERR_PTR(-EINVAL);
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
mutex_lock(&pwm_lock);
|
|
|
|
pwm = &chip->pwms[index];
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2011-12-14 18:12:23 +08:00
|
|
|
err = pwm_device_request(pwm, label);
|
|
|
|
if (err < 0)
|
|
|
|
pwm = ERR_PTR(err);
|
|
|
|
|
|
|
|
mutex_unlock(&pwm_lock);
|
2011-01-28 16:40:40 +08:00
|
|
|
return pwm;
|
|
|
|
}
|
2011-12-14 18:12:23 +08:00
|
|
|
EXPORT_SYMBOL_GPL(pwm_request_from_chip);
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2020-04-02 14:57:18 +08:00
|
|
|
static void pwm_apply_state_debug(struct pwm_device *pwm,
|
|
|
|
const struct pwm_state *state)
|
2020-02-11 05:35:18 +08:00
|
|
|
{
|
|
|
|
struct pwm_state *last = &pwm->last;
|
|
|
|
struct pwm_chip *chip = pwm->chip;
|
2023-03-23 05:45:45 +08:00
|
|
|
struct pwm_state s1 = { 0 }, s2 = { 0 };
|
2020-02-11 05:35:18 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_PWM_DEBUG))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* No reasonable diagnosis possible without .get_state() */
|
|
|
|
if (!chip->ops->get_state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* *state was just applied. Read out the hardware state and do some
|
|
|
|
* checks.
|
|
|
|
*/
|
|
|
|
|
2022-12-03 02:35:27 +08:00
|
|
|
err = chip->ops->get_state(chip, pwm, &s1);
|
|
|
|
trace_pwm_get(pwm, &s1, err);
|
2022-12-03 02:35:36 +08:00
|
|
|
if (err)
|
|
|
|
/* If that failed there isn't much to debug */
|
|
|
|
return;
|
2020-02-11 05:35:18 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The lowlevel driver either ignored .polarity (which is a bug) or as
|
|
|
|
* best effort inverted .polarity and fixed .duty_cycle respectively.
|
|
|
|
* Undo this inversion and fixup for further tests.
|
|
|
|
*/
|
|
|
|
if (s1.enabled && s1.polarity != state->polarity) {
|
|
|
|
s2.polarity = state->polarity;
|
|
|
|
s2.duty_cycle = s1.period - s1.duty_cycle;
|
|
|
|
s2.period = s1.period;
|
|
|
|
s2.enabled = s1.enabled;
|
|
|
|
} else {
|
|
|
|
s2 = s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s2.polarity != state->polarity &&
|
|
|
|
state->duty_cycle < state->period)
|
|
|
|
dev_warn(chip->dev, ".apply ignored .polarity\n");
|
|
|
|
|
|
|
|
if (state->enabled &&
|
|
|
|
last->polarity == state->polarity &&
|
|
|
|
last->period > s2.period &&
|
|
|
|
last->period <= state->period)
|
|
|
|
dev_warn(chip->dev,
|
2020-06-03 06:31:16 +08:00
|
|
|
".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
|
2020-02-11 05:35:18 +08:00
|
|
|
state->period, s2.period, last->period);
|
|
|
|
|
|
|
|
if (state->enabled && state->period < s2.period)
|
|
|
|
dev_warn(chip->dev,
|
2020-06-03 06:31:16 +08:00
|
|
|
".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
|
2020-02-11 05:35:18 +08:00
|
|
|
state->period, s2.period);
|
|
|
|
|
|
|
|
if (state->enabled &&
|
|
|
|
last->polarity == state->polarity &&
|
|
|
|
last->period == s2.period &&
|
|
|
|
last->duty_cycle > s2.duty_cycle &&
|
|
|
|
last->duty_cycle <= state->duty_cycle)
|
|
|
|
dev_warn(chip->dev,
|
2020-06-03 06:31:16 +08:00
|
|
|
".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
|
2020-02-11 05:35:18 +08:00
|
|
|
state->duty_cycle, state->period,
|
|
|
|
s2.duty_cycle, s2.period,
|
|
|
|
last->duty_cycle, last->period);
|
|
|
|
|
|
|
|
if (state->enabled && state->duty_cycle < s2.duty_cycle)
|
|
|
|
dev_warn(chip->dev,
|
2020-06-03 06:31:16 +08:00
|
|
|
".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
|
2020-02-11 05:35:18 +08:00
|
|
|
state->duty_cycle, state->period,
|
|
|
|
s2.duty_cycle, s2.period);
|
|
|
|
|
|
|
|
if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
|
|
|
|
dev_warn(chip->dev,
|
2020-04-11 23:35:28 +08:00
|
|
|
"requested disabled, but yielded enabled with duty > 0\n");
|
2020-02-11 05:35:18 +08:00
|
|
|
|
|
|
|
/* reapply the state that the driver reported being configured. */
|
|
|
|
err = chip->ops->apply(chip, pwm, &s1);
|
2022-12-03 02:35:27 +08:00
|
|
|
trace_pwm_apply(pwm, &s1, err);
|
2020-02-11 05:35:18 +08:00
|
|
|
if (err) {
|
|
|
|
*last = s1;
|
|
|
|
dev_err(chip->dev, "failed to reapply current setting\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-23 05:45:45 +08:00
|
|
|
*last = (struct pwm_state){ 0 };
|
2022-12-03 02:35:27 +08:00
|
|
|
err = chip->ops->get_state(chip, pwm, last);
|
|
|
|
trace_pwm_get(pwm, last, err);
|
2022-12-03 02:35:36 +08:00
|
|
|
if (err)
|
|
|
|
return;
|
2020-02-11 05:35:18 +08:00
|
|
|
|
|
|
|
/* reapplication of the current state should give an exact match */
|
|
|
|
if (s1.enabled != last->enabled ||
|
|
|
|
s1.polarity != last->polarity ||
|
|
|
|
(s1.enabled && s1.period != last->period) ||
|
|
|
|
(s1.enabled && s1.duty_cycle != last->duty_cycle)) {
|
|
|
|
dev_err(chip->dev,
|
2020-06-03 06:31:16 +08:00
|
|
|
".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
|
2020-02-11 05:35:18 +08:00
|
|
|
s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
|
|
|
|
last->enabled, last->polarity, last->duty_cycle,
|
|
|
|
last->period);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-28 16:40:40 +08:00
|
|
|
/**
|
2016-04-15 03:17:41 +08:00
|
|
|
* pwm_apply_state() - atomically apply a new state to a PWM device
|
2011-01-28 16:40:40 +08:00
|
|
|
* @pwm: PWM device
|
2019-08-24 23:37:07 +08:00
|
|
|
* @state: new state to apply
|
2011-01-28 16:40:40 +08:00
|
|
|
*/
|
2019-08-24 23:37:07 +08:00
|
|
|
int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
|
2011-01-28 16:40:40 +08:00
|
|
|
{
|
2019-08-24 23:37:02 +08:00
|
|
|
struct pwm_chip *chip;
|
2013-06-12 01:38:59 +08:00
|
|
|
int err;
|
|
|
|
|
2021-09-09 17:48:49 +08:00
|
|
|
/*
|
|
|
|
* Some lowlevel driver's implementations of .apply() make use of
|
|
|
|
* mutexes, also with some drivers only returning when the new
|
|
|
|
* configuration is active calling pwm_apply_state() from atomic context
|
|
|
|
* is a bad idea. So make it explicit that calling this function might
|
|
|
|
* sleep.
|
|
|
|
*/
|
|
|
|
might_sleep();
|
|
|
|
|
2016-05-28 00:45:49 +08:00
|
|
|
if (!pwm || !state || !state->period ||
|
|
|
|
state->duty_cycle > state->period)
|
2011-12-14 18:12:23 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-08-24 23:37:02 +08:00
|
|
|
chip = pwm->chip;
|
|
|
|
|
2019-01-08 03:49:37 +08:00
|
|
|
if (state->period == pwm->state.period &&
|
|
|
|
state->duty_cycle == pwm->state.duty_cycle &&
|
|
|
|
state->polarity == pwm->state.polarity &&
|
2021-05-07 21:18:42 +08:00
|
|
|
state->enabled == pwm->state.enabled &&
|
|
|
|
state->usage_power == pwm->state.usage_power)
|
2016-04-15 03:17:41 +08:00
|
|
|
return 0;
|
2013-06-12 01:38:59 +08:00
|
|
|
|
2022-05-21 19:02:03 +08:00
|
|
|
err = chip->ops->apply(chip, pwm, state);
|
2022-12-03 02:35:27 +08:00
|
|
|
trace_pwm_apply(pwm, state, err);
|
2021-07-01 15:29:25 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
2016-04-15 03:17:41 +08:00
|
|
|
|
2021-07-01 15:29:25 +08:00
|
|
|
pwm->state = *state;
|
2016-04-15 03:17:41 +08:00
|
|
|
|
2021-07-01 15:29:25 +08:00
|
|
|
/*
|
|
|
|
* only do this after pwm->state was applied as some
|
|
|
|
* implementations of .get_state depend on this
|
|
|
|
*/
|
|
|
|
pwm_apply_state_debug(pwm, state);
|
2012-07-24 22:05:32 +08:00
|
|
|
|
2016-03-31 04:03:27 +08:00
|
|
|
return 0;
|
2012-07-24 22:05:32 +08:00
|
|
|
}
|
2016-04-15 03:17:41 +08:00
|
|
|
EXPORT_SYMBOL_GPL(pwm_apply_state);
|
2012-07-24 22:05:32 +08:00
|
|
|
|
2016-06-08 17:21:23 +08:00
|
|
|
/**
|
|
|
|
* pwm_capture() - capture and report a PWM signal
|
|
|
|
* @pwm: PWM device
|
|
|
|
* @result: structure to fill with capture result
|
|
|
|
* @timeout: time to wait, in milliseconds, before giving up on capture
|
|
|
|
*
|
|
|
|
* Returns: 0 on success or a negative error code on failure.
|
|
|
|
*/
|
|
|
|
int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
|
|
|
|
unsigned long timeout)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!pwm || !pwm->chip->ops)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!pwm->chip->ops->capture)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
mutex_lock(&pwm_lock);
|
|
|
|
err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
|
|
|
|
mutex_unlock(&pwm_lock);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(pwm_capture);
|
|
|
|
|
2011-01-28 16:40:40 +08:00
|
|
|
/**
|
2016-04-15 03:17:41 +08:00
|
|
|
* pwm_adjust_config() - adjust the current PWM config to the PWM arguments
|
2011-01-28 16:40:40 +08:00
|
|
|
* @pwm: PWM device
|
2015-07-27 17:58:32 +08:00
|
|
|
*
|
2016-04-15 03:17:41 +08:00
|
|
|
* This function will adjust the PWM config to the PWM arguments provided
|
|
|
|
* by the DT or PWM lookup table. This is particularly useful to adapt
|
|
|
|
* the bootloader config to the Linux one.
|
2011-01-28 16:40:40 +08:00
|
|
|
*/
|
2016-04-15 03:17:41 +08:00
|
|
|
int pwm_adjust_config(struct pwm_device *pwm)
|
2011-01-28 16:40:40 +08:00
|
|
|
{
|
2016-04-15 03:17:41 +08:00
|
|
|
struct pwm_state state;
|
|
|
|
struct pwm_args pargs;
|
2015-10-17 08:40:58 +08:00
|
|
|
|
2016-04-15 03:17:41 +08:00
|
|
|
pwm_get_args(pwm, &pargs);
|
|
|
|
pwm_get_state(pwm, &state);
|
2015-10-17 08:40:58 +08:00
|
|
|
|
2016-04-15 03:17:41 +08:00
|
|
|
/*
|
|
|
|
* If the current period is zero it means that either the PWM driver
|
|
|
|
* does not support initial state retrieval or the PWM has not yet
|
|
|
|
* been configured.
|
|
|
|
*
|
|
|
|
* In either case, we setup the new period and polarity, and assign a
|
|
|
|
* duty cycle of 0.
|
|
|
|
*/
|
|
|
|
if (!state.period) {
|
|
|
|
state.duty_cycle = 0;
|
|
|
|
state.period = pargs.period;
|
|
|
|
state.polarity = pargs.polarity;
|
2015-10-17 08:40:58 +08:00
|
|
|
|
2016-04-15 03:17:41 +08:00
|
|
|
return pwm_apply_state(pwm, &state);
|
2015-10-17 08:40:58 +08:00
|
|
|
}
|
|
|
|
|
2016-04-15 03:17:41 +08:00
|
|
|
/*
|
|
|
|
* Adjust the PWM duty cycle/period based on the period value provided
|
|
|
|
* in PWM args.
|
|
|
|
*/
|
|
|
|
if (pargs.period != state.period) {
|
|
|
|
u64 dutycycle = (u64)state.duty_cycle * pargs.period;
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2016-04-15 03:17:41 +08:00
|
|
|
do_div(dutycycle, state.period);
|
|
|
|
state.duty_cycle = dutycycle;
|
|
|
|
state.period = pargs.period;
|
|
|
|
}
|
2011-01-28 16:40:40 +08:00
|
|
|
|
2016-04-15 03:17:41 +08:00
|
|
|
/*
|
|
|
|
* If the polarity changed, we should also change the duty cycle.
|
|
|
|
*/
|
|
|
|
if (pargs.polarity != state.polarity) {
|
|
|
|
state.polarity = pargs.polarity;
|
|
|
|
state.duty_cycle = state.period - state.duty_cycle;
|
2016-04-15 03:17:39 +08:00
|
|
|
}
|
2016-04-15 03:17:41 +08:00
|
|
|
|
|
|
|
return pwm_apply_state(pwm, &state);
|
2011-01-28 16:40:40 +08:00
|
|
|
}
|
2016-04-15 03:17:41 +08:00
|
|
|
EXPORT_SYMBOL_GPL(pwm_adjust_config);
|
2012-03-26 15:31:48 +08:00
|
|
|
|
2021-06-07 20:24:54 +08:00
|
|
|
static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
|
2011-12-14 18:10:32 +08:00
|
|
|
{
|
|
|
|
struct pwm_chip *chip;
|
|
|
|
|
|
|
|
mutex_lock(&pwm_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(chip, &pwm_chips, list)
|
2022-09-28 01:22:58 +08:00
|
|
|
if (chip->dev && device_match_fwnode(chip->dev, fwnode)) {
|
2011-12-14 18:10:32 +08:00
|
|
|
mutex_unlock(&pwm_lock);
|
|
|
|
return chip;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&pwm_lock);
|
|
|
|
|
|
|
|
return ERR_PTR(-EPROBE_DEFER);
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:37:47 +08:00
|
|
|
static struct device_link *pwm_device_link_add(struct device *dev,
|
|
|
|
struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct device_link *dl;
|
|
|
|
|
|
|
|
if (!dev) {
|
|
|
|
/*
|
|
|
|
* No device for the PWM consumer has been provided. It may
|
|
|
|
* impact the PM sequence ordering: the PWM supplier may get
|
|
|
|
* suspended before the consumer.
|
|
|
|
*/
|
|
|
|
dev_warn(pwm->chip->dev,
|
|
|
|
"No consumer device specified to create a link to\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
|
|
|
|
if (!dl) {
|
|
|
|
dev_err(dev, "failed to create device link to %s\n",
|
|
|
|
dev_name(pwm->chip->dev));
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dl;
|
|
|
|
}
|
|
|
|
|
2011-12-14 18:10:32 +08:00
|
|
|
/**
|
2012-12-21 17:43:58 +08:00
|
|
|
* of_pwm_get() - request a PWM via the PWM framework
|
2019-04-18 17:37:47 +08:00
|
|
|
* @dev: device for PWM consumer
|
2011-12-14 18:10:32 +08:00
|
|
|
* @np: device node to get the PWM from
|
|
|
|
* @con_id: consumer name
|
|
|
|
*
|
|
|
|
* Returns the PWM device parsed from the phandle and index specified in the
|
|
|
|
* "pwms" property of a device tree node or a negative error-code on failure.
|
|
|
|
* Values parsed from the device tree are stored in the returned PWM device
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* If con_id is NULL, the first PWM device listed in the "pwms" property will
|
|
|
|
* be requested. Otherwise the "pwm-names" property is used to do a reverse
|
|
|
|
* lookup of the PWM index. This also means that the "pwm-names" property
|
|
|
|
* becomes mandatory for devices that look up the PWM device via the con_id
|
|
|
|
* parameter.
|
2015-07-27 17:58:32 +08:00
|
|
|
*
|
|
|
|
* Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
|
|
|
|
* error code on failure.
|
2011-12-14 18:10:32 +08:00
|
|
|
*/
|
2022-08-27 01:26:42 +08:00
|
|
|
static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
|
|
|
|
const char *con_id)
|
2011-12-14 18:10:32 +08:00
|
|
|
{
|
|
|
|
struct pwm_device *pwm = NULL;
|
|
|
|
struct of_phandle_args args;
|
2019-04-18 17:37:47 +08:00
|
|
|
struct device_link *dl;
|
2023-07-15 04:56:14 +08:00
|
|
|
struct pwm_chip *chip;
|
2011-12-14 18:10:32 +08:00
|
|
|
int index = 0;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (con_id) {
|
|
|
|
index = of_property_match_string(np, "pwm-names", con_id);
|
|
|
|
if (index < 0)
|
|
|
|
return ERR_PTR(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
|
|
|
|
&args);
|
|
|
|
if (err) {
|
2017-01-30 05:54:05 +08:00
|
|
|
pr_err("%s(): can't parse \"pwms\" property\n", __func__);
|
2011-12-14 18:10:32 +08:00
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
2023-07-15 04:56:14 +08:00
|
|
|
chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
|
|
|
|
if (IS_ERR(chip)) {
|
|
|
|
if (PTR_ERR(chip) != -EPROBE_DEFER)
|
2017-05-24 00:05:03 +08:00
|
|
|
pr_err("%s(): PWM chip not found\n", __func__);
|
|
|
|
|
2023-07-15 04:56:14 +08:00
|
|
|
pwm = ERR_CAST(chip);
|
2011-12-14 18:10:32 +08:00
|
|
|
goto put;
|
|
|
|
}
|
|
|
|
|
2023-07-15 04:56:14 +08:00
|
|
|
pwm = chip->of_xlate(chip, &args);
|
2011-12-14 18:10:32 +08:00
|
|
|
if (IS_ERR(pwm))
|
|
|
|
goto put;
|
|
|
|
|
2019-04-18 17:37:47 +08:00
|
|
|
dl = pwm_device_link_add(dev, pwm);
|
|
|
|
if (IS_ERR(dl)) {
|
|
|
|
/* of_xlate ended up calling pwm_request_from_chip() */
|
2023-04-12 19:56:36 +08:00
|
|
|
pwm_put(pwm);
|
2019-04-18 17:37:47 +08:00
|
|
|
pwm = ERR_CAST(dl);
|
|
|
|
goto put;
|
|
|
|
}
|
|
|
|
|
2011-12-14 18:10:32 +08:00
|
|
|
/*
|
|
|
|
* If a consumer name was not given, try to look it up from the
|
|
|
|
* "pwm-names" property if it exists. Otherwise use the name of
|
|
|
|
* the user device node.
|
|
|
|
*/
|
|
|
|
if (!con_id) {
|
|
|
|
err = of_property_read_string_index(np, "pwm-names", index,
|
|
|
|
&con_id);
|
|
|
|
if (err < 0)
|
|
|
|
con_id = np->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
pwm->label = con_id;
|
|
|
|
|
|
|
|
put:
|
|
|
|
of_node_put(args.np);
|
|
|
|
|
|
|
|
return pwm;
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:36:07 +08:00
|
|
|
/**
|
|
|
|
* acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
|
2021-06-07 20:24:56 +08:00
|
|
|
* @fwnode: firmware node to get the "pwms" property from
|
2019-06-12 16:36:07 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-06-07 20:24:56 +08:00
|
|
|
static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
|
2019-06-12 16:36:07 +08:00
|
|
|
{
|
2021-07-06 23:11:32 +08:00
|
|
|
struct pwm_device *pwm;
|
2019-06-12 16:36:07 +08:00
|
|
|
struct fwnode_reference_args args;
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (args.nargs < 2)
|
|
|
|
return ERR_PTR(-EPROTO);
|
|
|
|
|
2021-06-07 20:24:55 +08:00
|
|
|
chip = fwnode_to_pwmchip(args.fwnode);
|
2019-06-12 16:36:07 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
return pwm;
|
|
|
|
}
|
|
|
|
|
2012-03-26 14:42:48 +08:00
|
|
|
/**
|
|
|
|
* pwm_add_table() - register PWM device consumers
|
|
|
|
* @table: array of consumers to register
|
|
|
|
* @num: number of consumers in table
|
|
|
|
*/
|
2015-03-13 00:31:31 +08:00
|
|
|
void pwm_add_table(struct pwm_lookup *table, size_t num)
|
2012-03-26 14:42:48 +08:00
|
|
|
{
|
|
|
|
mutex_lock(&pwm_lookup_lock);
|
|
|
|
|
|
|
|
while (num--) {
|
|
|
|
list_add_tail(&table->list, &pwm_lookup_list);
|
|
|
|
table++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&pwm_lookup_lock);
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:34:18 +08:00
|
|
|
/**
|
|
|
|
* pwm_remove_table() - unregister PWM device consumers
|
|
|
|
* @table: array of consumers to unregister
|
|
|
|
* @num: number of consumers in table
|
|
|
|
*/
|
|
|
|
void pwm_remove_table(struct pwm_lookup *table, size_t num)
|
|
|
|
{
|
|
|
|
mutex_lock(&pwm_lookup_lock);
|
|
|
|
|
|
|
|
while (num--) {
|
|
|
|
list_del(&table->list);
|
|
|
|
table++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&pwm_lookup_lock);
|
|
|
|
}
|
|
|
|
|
2012-03-26 14:42:48 +08:00
|
|
|
/**
|
|
|
|
* pwm_get() - look up and request a PWM device
|
|
|
|
* @dev: device for PWM consumer
|
|
|
|
* @con_id: consumer name
|
|
|
|
*
|
2011-12-14 18:10:32 +08:00
|
|
|
* Lookup is first attempted using DT. If the device was not instantiated from
|
|
|
|
* a device tree, a PWM chip and a relative index is looked up via a table
|
|
|
|
* supplied by board setup code (see pwm_add_table()).
|
2012-03-26 14:42:48 +08:00
|
|
|
*
|
|
|
|
* Once a PWM chip has been found the specified PWM device will be requested
|
|
|
|
* and is ready to be used.
|
2015-07-27 17:58:32 +08:00
|
|
|
*
|
|
|
|
* Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
|
|
|
|
* error code on failure.
|
2012-03-26 14:42:48 +08:00
|
|
|
*/
|
|
|
|
struct pwm_device *pwm_get(struct device *dev, const char *con_id)
|
|
|
|
{
|
2021-06-07 20:24:56 +08:00
|
|
|
const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
|
2012-08-10 19:11:13 +08:00
|
|
|
const char *dev_id = dev ? dev_name(dev) : NULL;
|
2017-01-23 00:14:07 +08:00
|
|
|
struct pwm_device *pwm;
|
|
|
|
struct pwm_chip *chip;
|
2019-04-18 17:37:47 +08:00
|
|
|
struct device_link *dl;
|
2012-03-26 14:42:48 +08:00
|
|
|
unsigned int best = 0;
|
2014-08-28 17:03:14 +08:00
|
|
|
struct pwm_lookup *p, *chosen = NULL;
|
2012-03-26 14:42:48 +08:00
|
|
|
unsigned int match;
|
2017-01-23 00:14:08 +08:00
|
|
|
int err;
|
2012-03-26 14:42:48 +08:00
|
|
|
|
2011-12-14 18:10:32 +08:00
|
|
|
/* look up via DT first */
|
2021-06-07 20:24:56 +08:00
|
|
|
if (is_of_node(fwnode))
|
|
|
|
return of_pwm_get(dev, to_of_node(fwnode), con_id);
|
2011-12-14 18:10:32 +08:00
|
|
|
|
2019-06-12 16:36:07 +08:00
|
|
|
/* then lookup via ACPI */
|
2021-06-07 20:24:56 +08:00
|
|
|
if (is_acpi_node(fwnode)) {
|
|
|
|
pwm = acpi_pwm_get(fwnode);
|
2019-07-30 23:48:48 +08:00
|
|
|
if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
|
|
|
|
return pwm;
|
|
|
|
}
|
2011-12-14 18:10:32 +08:00
|
|
|
|
2012-03-26 14:42:48 +08:00
|
|
|
/*
|
|
|
|
* We look up the provider in the static table typically provided by
|
|
|
|
* board setup code. We first try to lookup the consumer device by
|
|
|
|
* name. If the consumer device was passed in as NULL or if no match
|
|
|
|
* was found, we try to find the consumer by directly looking it up
|
|
|
|
* by name.
|
|
|
|
*
|
|
|
|
* If a match is found, the provider PWM chip is looked up by name
|
|
|
|
* and a PWM device is requested using the PWM device per-chip index.
|
|
|
|
*
|
|
|
|
* The lookup algorithm was shamelessly taken from the clock
|
|
|
|
* framework:
|
|
|
|
*
|
|
|
|
* We do slightly fuzzy matching here:
|
|
|
|
* An entry with a NULL ID is assumed to be a wildcard.
|
|
|
|
* If an entry has a device ID, it must match
|
|
|
|
* If an entry has a connection ID, it must match
|
|
|
|
* Then we take the most specific entry - with the following order
|
|
|
|
* of precedence: dev+con > dev only > con only.
|
|
|
|
*/
|
|
|
|
mutex_lock(&pwm_lookup_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(p, &pwm_lookup_list, list) {
|
|
|
|
match = 0;
|
|
|
|
|
|
|
|
if (p->dev_id) {
|
|
|
|
if (!dev_id || strcmp(p->dev_id, dev_id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
match += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->con_id) {
|
|
|
|
if (!con_id || strcmp(p->con_id, con_id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
match += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match > best) {
|
2014-08-28 17:03:14 +08:00
|
|
|
chosen = p;
|
2012-03-26 14:42:48 +08:00
|
|
|
|
|
|
|
if (match != 3)
|
|
|
|
best = match;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 00:14:07 +08:00
|
|
|
mutex_unlock(&pwm_lookup_lock);
|
|
|
|
|
|
|
|
if (!chosen)
|
|
|
|
return ERR_PTR(-ENODEV);
|
2014-05-20 04:42:32 +08:00
|
|
|
|
2014-08-28 17:03:14 +08:00
|
|
|
chip = pwmchip_find_by_name(chosen->provider);
|
2017-01-23 00:14:08 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the lookup entry specifies a module, load the module and retry
|
|
|
|
* the PWM chip lookup. This can be used to work around driver load
|
|
|
|
* ordering issues if driver's can't be made to properly support the
|
|
|
|
* deferred probe mechanism.
|
|
|
|
*/
|
|
|
|
if (!chip && chosen->module) {
|
|
|
|
err = request_module(chosen->module);
|
|
|
|
if (err == 0)
|
|
|
|
chip = pwmchip_find_by_name(chosen->provider);
|
|
|
|
}
|
|
|
|
|
2014-08-28 17:03:14 +08:00
|
|
|
if (!chip)
|
2017-01-23 00:14:07 +08:00
|
|
|
return ERR_PTR(-EPROBE_DEFER);
|
2014-05-20 04:42:32 +08:00
|
|
|
|
2014-08-28 17:03:14 +08:00
|
|
|
pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
|
|
|
|
if (IS_ERR(pwm))
|
2017-01-23 00:14:07 +08:00
|
|
|
return pwm;
|
2012-03-26 14:42:48 +08:00
|
|
|
|
2019-04-18 17:37:47 +08:00
|
|
|
dl = pwm_device_link_add(dev, pwm);
|
|
|
|
if (IS_ERR(dl)) {
|
2023-04-12 19:56:36 +08:00
|
|
|
pwm_put(pwm);
|
2019-04-18 17:37:47 +08:00
|
|
|
return ERR_CAST(dl);
|
|
|
|
}
|
|
|
|
|
2016-05-17 20:27:25 +08:00
|
|
|
pwm->args.period = chosen->period;
|
|
|
|
pwm->args.polarity = chosen->polarity;
|
|
|
|
|
2012-03-26 14:42:48 +08:00
|
|
|
return pwm;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(pwm_get);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pwm_put() - release a PWM device
|
|
|
|
* @pwm: PWM device
|
|
|
|
*/
|
|
|
|
void pwm_put(struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
if (!pwm)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mutex_lock(&pwm_lock);
|
|
|
|
|
|
|
|
if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
|
2012-08-10 19:11:13 +08:00
|
|
|
pr_warn("PWM device already freed\n");
|
2012-03-26 14:42:48 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pwm->chip->ops->free)
|
|
|
|
pwm->chip->ops->free(pwm->chip, pwm);
|
|
|
|
|
2019-03-25 17:49:33 +08:00
|
|
|
pwm_set_chip_data(pwm, NULL);
|
2012-03-26 14:42:48 +08:00
|
|
|
pwm->label = NULL;
|
|
|
|
|
|
|
|
module_put(pwm->chip->ops->owner);
|
|
|
|
out:
|
|
|
|
mutex_unlock(&pwm_lock);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(pwm_put);
|
|
|
|
|
2021-06-07 20:24:58 +08:00
|
|
|
static void devm_pwm_release(void *pwm)
|
2012-08-01 18:20:58 +08:00
|
|
|
{
|
2021-06-07 20:24:58 +08:00
|
|
|
pwm_put(pwm);
|
2012-08-01 18:20:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devm_pwm_get() - resource managed pwm_get()
|
|
|
|
* @dev: device for PWM consumer
|
|
|
|
* @con_id: consumer name
|
|
|
|
*
|
|
|
|
* This function performs like pwm_get() but the acquired PWM device will
|
|
|
|
* automatically be released on driver detach.
|
2015-07-27 17:58:32 +08:00
|
|
|
*
|
|
|
|
* Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
|
|
|
|
* error code on failure.
|
2012-08-01 18:20:58 +08:00
|
|
|
*/
|
|
|
|
struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
|
|
|
|
{
|
2021-06-07 20:24:58 +08:00
|
|
|
struct pwm_device *pwm;
|
|
|
|
int ret;
|
2012-08-01 18:20:58 +08:00
|
|
|
|
|
|
|
pwm = pwm_get(dev, con_id);
|
2021-06-07 20:24:58 +08:00
|
|
|
if (IS_ERR(pwm))
|
|
|
|
return pwm;
|
|
|
|
|
|
|
|
ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
2012-08-01 18:20:58 +08:00
|
|
|
|
|
|
|
return pwm;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_pwm_get);
|
|
|
|
|
2019-06-12 16:36:07 +08:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2021-06-07 20:24:58 +08:00
|
|
|
struct pwm_device *pwm = ERR_PTR(-ENODEV);
|
|
|
|
int ret;
|
2019-06-12 16:36:07 +08:00
|
|
|
|
|
|
|
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);
|
2021-06-07 20:24:58 +08:00
|
|
|
if (IS_ERR(pwm))
|
|
|
|
return pwm;
|
2019-06-12 16:36:07 +08:00
|
|
|
|
2021-06-07 20:24:58 +08:00
|
|
|
ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
2019-06-12 16:36:07 +08:00
|
|
|
|
|
|
|
return pwm;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
|
|
|
|
|
2012-03-26 15:31:48 +08:00
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < chip->npwm; i++) {
|
|
|
|
struct pwm_device *pwm = &chip->pwms[i];
|
2016-04-15 03:17:43 +08:00
|
|
|
struct pwm_state state;
|
|
|
|
|
|
|
|
pwm_get_state(pwm, &state);
|
2012-03-26 15:31:48 +08:00
|
|
|
|
|
|
|
seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
|
|
|
|
|
|
|
|
if (test_bit(PWMF_REQUESTED, &pwm->flags))
|
2013-12-19 12:31:24 +08:00
|
|
|
seq_puts(s, " requested");
|
2012-03-26 15:31:48 +08:00
|
|
|
|
2016-04-15 03:17:43 +08:00
|
|
|
if (state.enabled)
|
2013-12-19 12:31:24 +08:00
|
|
|
seq_puts(s, " enabled");
|
2012-03-26 15:31:48 +08:00
|
|
|
|
2020-06-03 06:31:16 +08:00
|
|
|
seq_printf(s, " period: %llu ns", state.period);
|
|
|
|
seq_printf(s, " duty: %llu ns", state.duty_cycle);
|
2016-04-15 03:17:44 +08:00
|
|
|
seq_printf(s, " polarity: %s",
|
|
|
|
state.polarity ? "inverse" : "normal");
|
|
|
|
|
2021-05-07 21:18:42 +08:00
|
|
|
if (state.usage_power)
|
|
|
|
seq_puts(s, " usage_power");
|
|
|
|
|
2013-12-19 12:31:24 +08:00
|
|
|
seq_puts(s, "\n");
|
2012-03-26 15:31:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
|
|
|
|
{
|
|
|
|
mutex_lock(&pwm_lock);
|
|
|
|
s->private = "";
|
|
|
|
|
|
|
|
return seq_list_start(&pwm_chips, *pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
s->private = "\n";
|
|
|
|
|
|
|
|
return seq_list_next(v, &pwm_chips, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pwm_seq_stop(struct seq_file *s, void *v)
|
|
|
|
{
|
|
|
|
mutex_unlock(&pwm_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pwm_seq_show(struct seq_file *s, void *v)
|
|
|
|
{
|
|
|
|
struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
|
|
|
|
|
|
|
|
seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
|
|
|
|
chip->dev->bus ? chip->dev->bus->name : "no-bus",
|
|
|
|
dev_name(chip->dev), chip->npwm,
|
|
|
|
(chip->npwm != 1) ? "s" : "");
|
|
|
|
|
2019-01-08 03:49:39 +08:00
|
|
|
pwm_dbg_show(chip, s);
|
2012-03-26 15:31:48 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-16 10:50:28 +08:00
|
|
|
static const struct seq_operations pwm_debugfs_sops = {
|
2012-03-26 15:31:48 +08:00
|
|
|
.start = pwm_seq_start,
|
|
|
|
.next = pwm_seq_next,
|
|
|
|
.stop = pwm_seq_stop,
|
|
|
|
.show = pwm_seq_show,
|
|
|
|
};
|
|
|
|
|
2020-09-16 10:50:28 +08:00
|
|
|
DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
|
2012-03-26 15:31:48 +08:00
|
|
|
|
|
|
|
static int __init pwm_debugfs_init(void)
|
|
|
|
{
|
2022-12-03 02:35:13 +08:00
|
|
|
debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
|
2012-03-26 15:31:48 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
subsys_initcall(pwm_debugfs_init);
|
|
|
|
#endif /* CONFIG_DEBUG_FS */
|