在 linux 内核中使用 pwm_get() 的示例

Example of use pwm_get() in linux kernel

我想尝试在我的 Rasperry Pi 的 linux 内核模块中使用 PWM。我已经通过 SYSFS 接口成功启用了 PWM。 对于在内核模块文档中使用 pwm 状态:

New users should use the pwm_get() function and pass to it the consumer device or a consumer name. pwm_put() is used to free the PWM device. Managed variants of these functions, devm_pwm_get() and devm_pwm_put(), also exist.

pwm_get 函数如下所示:

/**
 * pwm_get() - look up and request a PWM device
 * @dev: device for PWM consumer
 * @con_id: consumer name
....
 */
struct pwm_device *pwm_get(struct device *dev, const char *con_id)

在哪里可以找到 dev 和 con_id?我怀疑它们应该在设备树中定义,但这只是一种怀疑。

One example of pwm_get() is available in the Intel PWM backlight panel driver.
Here it is being used to obtain a PWM source by its name.

/* Get the PWM chip for backlight control */
panel->backlight.pwm = pwm_get(dev->dev, "pwm_backlight");

The PWM provider itself is defined here...

/* PWM consumed by the Intel GFX */
static struct pwm_lookup crc_pwm_lookup[] = {
        PWM_LOOKUP("crystal_cove_pwm", 0, "0000:00:02.0", "pwm_backlight", 0, PWM_POLARITY_NORMAL),
};

...and initialised here.

/* Add lookup table for crc-pwm */
pwm_add_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));

pwm-beeperpwm_get().

的另一个例子
beeper->pwm = pwm_get(&pdev->dev, NULL);

设备树中存在相应的条目here

buzzer {
        compatible = "pwm-beeper";
        pwms = <&pwm 0 1000000 0>;
        pinctrl-names = "default";
        pinctrl-0 = <&pwm0_out>;
};

The inline documentation of pwm_get() describes both the ways it can be used.

/**
* 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.
*/