PWM子系统用于管理PWM波的输出。PWM子系统一般是由芯片厂商实现默认编译进内核。
PWM子系统一般不单独使用,比如用来控制LCD背光
PWM子系统内核实现文件:include\linux\pwm.h
linux内核使用structpwm_device来描述一个PWM设备
structpwm_device {
constchar *label;
unsignedlong flags;
unsignedint hwpwm;
unsignedint pwm;
structpwm_chip *chip;
void *chip_data;
structpwm_argsargs;
structpwm_statestate;
};
PWM子系统使用比较简单。
申请、释放PWM设备:
struct pwm_device *pwm_get(struct device *dev, constchar *con_id);
voidpwm_put(struct pwm_device *pwm);
struct pwm_device *devm_pwm_get(struct device *dev, constchar *con_id);
voiddevm_pwm_put(struct device *dev, struct pwm_device *pwm);
con_id查找PWM, con_id与设备树节点的pwm-names属性字段相同pwm-names属性带devm开头的API表示申请的PWM设备会在设备卸载的时候自动释放, 不带devm的则要手动释放
配置PWM设备:
enumpwm_polarity {
PWM_POLARITY_NORMAL,
PWM_POLARITY_INVERSED,
};
staticinlineintpwm_set_polarity(struct pwm_device *pwm,
enum pwm_polarity polarity)
staticinlineintpwm_config(struct pwm_device *pwm, int duty_ns,
int period_ns)
启用or禁用PWM设备:
staticinlineintpwm_enable(struct pwm_device *pwm)
staticinlinevoidpwm_disable(struct pwm_device *pwm)
drivers\pwm\pwm-imx.c
.......
staticconststructof_device_idimx_pwm_dt_ids[] = {
{ .compatible = "fsl,imx1-pwm", .data = &imx_pwm_data_v1, },
{ .compatible = "fsl,imx27-pwm", .data = &imx_pwm_data_v2, },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, imx_pwm_dt_ids);
staticintimx_pwm_remove(struct platform_device *pdev)
{
structimx_chip *imx;
imx = platform_get_drvdata(pdev);
if (imx == NULL)
return -ENODEV;
return pwmchip_remove(&imx->chip);
}
staticstructplatform_driverimx_pwm_driver = {
.driver = {
.name = "imx-pwm",
.of_match_table = imx_pwm_dt_ids,
},
.probe = imx_pwm_probe,
.remove = imx_pwm_remove,
};
module_platform_driver(imx_pwm_driver);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Sascha Hauer ");
imx6ull pwm子系统实现 :drivers\pwm\pwm-imx.c
arch\arm\boot\dts\imx6ull.dtsi
pwm1: pwm@2080000 {
compatible = "fsl,imx6ul-pwm", "fsl,imx27-pwm";
reg = <0x20800000x4000>;
interrupts = 83 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&clks IMX6UL_CLK_PWM1>,
<&clks IMX6UL_CLK_PWM1>;
clock-names = "ipg", "per";
#pwm-cells = <2>;
};
pwm2: pwm@2084000 {
compatible = "fsl,imx6ul-pwm", "fsl,imx27-pwm";
reg = <0x20840000x4000>;
interrupts = 84 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&clks IMX6UL_CLK_DUMMY>,
<&clks IMX6UL_CLK_DUMMY>;
clock-names = "ipg", "per";
#pwm-cells = <2>;
};
......
该文件描述了imx6ull的PWM1~PWM8的设备树节点,这是由芯片原厂厂家实现的。 对于一般驱动工程师来说使用只需要关注以下属性:可内核中的文档:Documentation\devicetree\bindings\pwm\pwm.txt
pwms = <“&PWMn id period_ns>;
pwm-names = "name";
需要使用PWM就在相应设备树节点中添加这些属性
设备树节点编写:根节点下添加red_led_pwm 节点
red_led_pwm {
compatible = "red_led,pwm";
pinctrl-names = "default";
pinctrl-0 = <&red_led_pwm>;
back {
pwm-names = "red_led_pwm3";
pwms = <&pwm3 050000>;
};
};
在iomuxc节点下添加相关pinctrl节点
red_led_pwm: ledsgrp {
fsl,pins = <
MX6UL_PAD_GPIO1_IO04__PWM3_OUT 0x1b0b0
>;
};
驱动实现:
/*
* @brief : PWM子系统
* @date : 2021-11-xx
* @version : v1.0.0
* @Change Logs:
* @date author notes:
*/
#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<linux/uaccess.h>
#include<linux/types.h>
#include<linux/kernel.h>
#include<linux/delay.h>
#include<linux/ide.h>
#include<linux/errno.h>
#include<linux/gpio.h>
#include<asm/mach/map.h>
#include<linux/of.h>
#include<linux/of_address.h>
#include<linux/of_gpio.h>
#include<asm/io.h>
#include<linux/device.h>
#include<linux/platform_device.h>
#include<linux/pwm.h>
#define RED_LED_DTS_COMPATIBLE "red_led,pwm"/* 设备树节点匹配属性 */
#define LED_PWM_CMD_SET_DUTY 0x01
#define LED_PWM_CMD_SET_PERIOD 0x02
#define LED_PWM_CMD_SET_BOTH 0x03
#define LED_PWM_CMD_ENABLE 0x04
#define LED_PWM_CMD_DISABLE 0x05
structled_pwm_param {
int duty_ns;
int period_ns;
};
structred_led_dev {
dev_t dev_no;
structcdevchrdev;
structclass *led_class;
structdevice_node *dev_node;
structpwm_device *red_led_pwm;
};
staticstructled_pwm_paramled_pwm;
staticstructred_led_devled_dev;
staticintred_led_drv_open(struct inode *node, struct file *file)
{
int ret = 0;
pwm_set_polarity(led_dev.red_led_pwm, PWM_POLARITY_INVERSED);
pwm_enable(led_dev.red_led_pwm);
printk("red_led_pwm open\r\n");
return ret;
}
staticssize_tred_led_drv_write(struct file *file, constchar __user *buf, size_t size, loff_t *offset)
{
int err;
if (size != sizeof(led_pwm)) return -EINVAL;
err = copy_from_user(&led_pwm, buf, size);
if (err > 0) return -EFAULT;
pwm_config(led_dev.red_led_pwm, led_pwm.duty_ns, led_pwm.period_ns);
return1;
}
staticlong _drv_ioctl(struct file *filp, unsignedint cmd, unsignedlong arg)
{
int ret = 0;
void __user *my_user_space = (void __user *)arg;
switch (cmd)
{
case LED_PWM_CMD_SET_DUTY:
ret = copy_from_user(&led_pwm.duty_ns, my_user_space, sizeof(led_pwm.duty_ns));
if (ret > 0) return -EFAULT;
pwm_config(led_dev.red_led_pwm, led_pwm.duty_ns, led_pwm.period_ns);
break;
case LED_PWM_CMD_SET_PERIOD:
ret = copy_from_user(&led_pwm.period_ns, my_user_space, sizeof(led_pwm.period_ns));
if (ret > 0) return -EFAULT;
pwm_config(led_dev.red_led_pwm, led_pwm.duty_ns, led_pwm.period_ns);
break;
case LED_PWM_CMD_SET_BOTH:
ret = copy_from_user(&led_pwm, my_user_space, sizeof(led_pwm));
if (ret > 0) return -EFAULT;
pwm_config(led_dev.red_led_pwm, led_pwm.duty_ns, led_pwm.period_ns);
break;
case LED_PWM_CMD_ENABLE:
pwm_enable(led_dev.red_led_pwm);
break;
case LED_PWM_CMD_DISABLE:
pwm_disable(led_dev.red_led_pwm);
break;
}
}
staticintred_led_drv_release(struct inode *node, struct file *filp)
{
int ret = 0;
pwm_config(led_dev.red_led_pwm, 0, 5000);
printk("led pwm dev close\r\n");
// pwm_disable(led_dev.red_led_pwm);
return ret;
}
staticstructfile_operationsred_led_drv = {
.owner = THIS_MODULE,
.open = red_led_drv_open,
.write = red_led_drv_write,
.unlocked_ioctl = _drv_ioctl,
.release = red_led_drv_release,
};
/*设备树的匹配列表 */
staticstructof_device_iddts_match_table[] = {
{.compatible = RED_LED_DTS_COMPATIBLE, },
{},
};
staticintled_red_driver_probe(struct platform_device *pdev)
{
int err;
int ret;
structdevice *tdev;
structdevice_node *child;
tdev = &pdev->dev;
child = of_get_next_child(tdev->of_node, NULL); /* 获取设备树子节点 */
if (!child) {
return -EINVAL;
}
led_dev.red_led_pwm = devm_of_pwm_get(tdev, child, NULL); /* 从子节点中获取PWM设备 */
if (IS_ERR(led_dev.red_led_pwm)) {
printk(KERN_ERR"can't get red_led_pwm!!\n");
return -EFAULT;
}
ret = alloc_chrdev_region(&led_dev.dev_no, 0, 1, "red_led_pwm");
if (ret < 0) {
pr_err("Error: failed to register mbochs_dev, err: %d\n", ret);
return ret;
}
cdev_init(&led_dev.chrdev, &red_led_drv);
cdev_add(&led_dev.chrdev, led_dev.dev_no, 1);
led_dev.led_class = class_create(THIS_MODULE, "red_led_pwm");
err = PTR_ERR(led_dev.led_class);
if (IS_ERR(led_dev.led_class)) {
goto failed1;
}
tdev = device_create(led_dev.led_class , NULL, led_dev.dev_no, NULL, "red_led_pwm");
if (IS_ERR(tdev)) {
ret = -EINVAL;
goto failed2;
}
printk(KERN_INFO"%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return0;
failed2:
device_destroy(led_dev.led_class, led_dev.dev_no);
class_destroy(led_dev.led_class);
failed1:
cdev_del(&led_dev.chrdev);
unregister_chrdev_region(led_dev.dev_no, 1);
return ret;
}
intled_red_driver_remove(struct platform_device *dev)
{
// pwm_disable(led_dev.red_led_pwm);
// pwm_free(led_dev.red_led_pwm);
printk(KERN_INFO"driver remove %s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
device_destroy(led_dev.led_class, led_dev.dev_no);
class_destroy(led_dev.led_class);
unregister_chrdev_region(led_dev.dev_no, 1);
cdev_del(&led_dev.chrdev);
return0;
}
staticstructplatform_driverred_led_platform_driver = {
.probe = led_red_driver_probe,
.remove = led_red_driver_remove,
.driver = {
.name = "xgj,red_led",
.owner = THIS_MODULE,
.of_match_table = dts_match_table, //通过设备树匹配
},
};
module_platform_driver(red_led_platform_driver);
MODULE_AUTHOR("XGJ");
MODULE_LICENSE("GPL");
编译驱动:
#RCH=arm
#CROSS_COMPILE=arm-linux-gnueabihf-
#export ARCH CROSS_COMPILE
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH, 比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
# 请参考各开发板的高级用户使用手册
KERN_DIR = /home/ares/work/ebf_linux_kernel-ebf_4.19.35_imx6ul
all:
make -C $(KERN_DIR) M=`pwd` modules
$(CROSS_COMPILE)gcc -o test_led_pwm_subsys_drv test_led_pwm_subsys_drv.c
clean:
make -C $(KERN_DIR) M=`pwd` clean
rm -rf modules.order test_led_pwm_subsys_drv
# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o
obj-m += led_pwm_subsystem_drv.o
测试程序编写:
#include"stdio.h"
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<sys/ioctl.h>
#include<poll.h>
#include<stdint.h>
#define DEV_NAME "/dev/red_led_pwm"
#define LED_PWM_CMD_SET_DUTY 0x01
#define LED_PWM_CMD_SET_PERIOD 0x02
#define LED_PWM_CMD_SET_BOTH 0x03
#define LED_PWM_CMD_ENABLE 0x04
#define LED_PWM_CMD_DISABLE 0x05
structled_pwm_param {
int duty_ns;
int period_ns;
};
voidsleep_ms(unsignedint ms)
{
structtimevaldelay;
delay.tv_sec = 0;
delay.tv_usec = ms * 1000;
select(0, NULL, NULL, NULL, &delay);
}
intmain(int argc, char **argv)
{
int fd;
int ret;
/* 2. 打开文件 */
fd = open(DEV_NAME, O_RDWR | O_NONBLOCK); // | O_NONBLOCK
if (fd < 0)
{
printf("can not open file %s, %d\n", DEV_NAME, fd);
return-1;
}
int buf = 3;
structled_pwm_paramled_pwm;
led_pwm.duty_ns = 500;
led_pwm.period_ns = 5000;
write(fd, &led_pwm, sizeof(led_pwm));
sleep_ms(3000);
for (int i = 0; i < 5; i++)
{
led_pwm.duty_ns += 300;
if (led_pwm.duty_ns < led_pwm.period_ns)
ioctl(fd, LED_PWM_CMD_SET_DUTY, &led_pwm.duty_ns);
sleep_ms(1000);
}
// led_pwm.duty_ns = 0;
// ioctl(fd, LED_PWM_CMD_SET_DUTY, &led_pwm.duty_ns);
sleep_ms(3000);
close(fd);
return0;
}