devm_pwm_get的实现
/**
* pwm_get() - look up and request a PWM device
* @dev: device for PWM consumer
* @con_id: consumer name
*
* 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()).
*
* Once a PWM chip has been found the specified PWM device will be requested
* and is ready to be used.
*
* Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
* error code on failure.
*/
struct pwm_device *pwm_get(struct device *dev, const char *con_id)
{
const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
const char *dev_id = dev ? dev_name(dev) : NULL;
struct pwm_device *pwm;
struct pwm_chip *chip;
struct device_link *dl;
unsigned int best = 0;
struct pwm_lookup *p, *chosen = NULL;
unsigned int match;
int err;
/* look up via DT first */
if (is_of_node(fwnode))
return of_pwm_get(dev, to_of_node(fwnode), con_id);
/* then lookup via ACPI */
if (is_acpi_node(fwnode)) {
pwm = acpi_pwm_get(fwnode);
if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
return pwm;
}
/*
* 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;
//这里查找provider,比较dev_id和con_id TODO:
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) {
chosen = p;
if (match != 3)
best = match;
else
break;
}
}
mutex_unlock(&pwm_lookup_lock);
if (!chosen)
return ERR_PTR(-ENODEV);
chip = pwmchip_find_by_name(chosen->provider);
/*
* 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);
}
if (!chip)
return ERR_PTR(-EPROBE_DEFER);
pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
if (IS_ERR(pwm))
return pwm;
dl = pwm_device_link_add(dev, pwm);
if (IS_ERR(dl)) {
pwm_put(pwm);
return ERR_CAST(dl);
}
pwm->args.period = chosen->period;
pwm->args.polarity = chosen->polarity;
return pwm;
}
EXPORT_SYMBOL_GPL(pwm_get);
/**
* 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
*
* 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.
*/
struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
unsigned int index,
const char *label)
{
struct pwm_device *pwm;
int err;
if (!chip || index >= chip->npwm)
return ERR_PTR(-EINVAL);
mutex_lock(&pwm_lock);
pwm = &chip->pwms[index];
err = pwm_device_request(pwm, label);
if (err < 0)
pwm = ERR_PTR(err);
mutex_unlock(&pwm_lock);
return pwm;
}
EXPORT_SYMBOL_GPL(pwm_request_from_chip);
static int pwm_device_request(struct pwm_device *pwm, const char *label)
{
int err;
struct pwm_chip *chip = pwm->chip;
const struct pwm_ops *ops = chip->ops;
if (test_bit(PWMF_REQUESTED, &pwm->flags))
return -EBUSY;
if (!try_module_get(chip->owner))
return -ENODEV;
if (ops->request) {
err = ops->request(chip, pwm);
if (err) {
module_put(chip->owner);
return err;
}
}
if (ops->get_state) {
/*
* 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, };
err = ops->get_state(chip, pwm, &state);
trace_pwm_get(pwm, &state, err);
if (!err)
pwm->state = state;
if (IS_ENABLED(CONFIG_PWM_DEBUG))
pwm->last = pwm->state;
}
set_bit(PWMF_REQUESTED, &pwm->flags);
pwm->label = label;
return 0;
}
enum {
PWMF_REQUESTED = 0,
PWMF_EXPORTED = 1,
};
demo:
#define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
_period, _polarity, _module) \
{ \
.provider = _provider, \
.index = _index, \
.dev_id = _dev_id, \
.con_id = _con_id, \
.period = _period, \
.polarity = _polarity, \
.module = _module, \
}
#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
_polarity, NULL)
struct pwm_lookup {
struct list_head list;
const char *provider;
unsigned int index;
const char *dev_id;
const char *con_id;
unsigned int period;
enum pwm_polarity polarity;
const char *module; /* optional, may be NULL */
};
/* PWM consumed by the Intel GFX */
static struct pwm_lookup crc_pwm_lookup[] = {
PWM_LOOKUP("crystal_cove_pwm", 0, "0000:00:02.0", "pwm_pmic_backlight", 0, PWM_POLARITY_NORMAL),
};
//这里注册provider,设置dev_id和con_id TODO:
static int crystal_cove_i2c_probe(struct i2c_client *i2c)
{
pwm_add_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));
}
所以要使用devm_pwm_get获取pwmchip资源,需要类似的add过程