Example of use pwm_get() in linux kernel

2.3k views Asked by At

I want to try to use PWM in linux kernel module for my Rasperry Pi. I've successfully enabled PWM via SYSFS interface. For usage of pwm in kernel modules documentation states:

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 function looks like this:

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

Where I can find dev and con_id? I suspect that they should be defined in device tree, but it is only a suspicion.

1

There are 1 answers

0
TheCodeArtist On BEST ANSWER

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-beeper is another example of pwm_get().

beeper->pwm = pwm_get(&pdev->dev, NULL);

A corresponding entry in the device tree is present 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.
*/