一、系统框架
内核的PWM core,向下对实际pwm控制器驱动,提供了pwm_chip,soc厂商编程控制器驱动,只需注册结构体,配置好private_data,实例化pwm_ops操作,编写具体函数即可。 向上为其他驱动调用提供了统一的接口,通过pwm_device,关联pwm_chip,其他驱动或者用户程序通过接口来操作pwm_device结构体。
pwm_chip层实际上也是核心层的一部分,其主要对应一个pwm控制器,一个pwm控制器可包含多个pwm_device,针对一个pwm_chip,提供了访问pwm 控制器的方法,通过pwm_chip提供的方法,实现对pwm 控制器的配置
在硬件层,pwm控制器驱动soc厂商已经写好,我们要做的是在设备树(或者是设备树插件)中开启控制器节点, 描述pwm设备节点,然后驱动中调用内核PWM提供的接口,来实现pwm驱动控制。
驱动程序分为两部分:
device部分:目前都是设备数陪住
driver部分:创建设备节点,并且提供open,close,read,write等接口供上层应用调用
二、核心层:
PWM framework非常简单,但它同样具备framework的基本特性:对上,为内核其它driver(Consumer)提供使用PWM功能的统一接口;对下,为PWM driver(Provider)提供driver开发的通用方法和API;内部,抽象并实现公共逻辑,屏蔽技术细节。下面我们通过它所提供的API,进一步认识PWM framework。
api声明见linux\include\linux\pwm.h
PWM linux framework源代码位于内核的 drivers/pwm 目录下,具体的路径如下所示:
drivers/pwm/ ├── core.c--pwm子系统核心。 ├── pwm-imx.c--imx的pwm_chip驱动。 └── sysfs.c--pwm子系统的pwm_class注册,pwm_chip属性,pwm_device属性等定义。
2.1、向PWM consumer提供的APIs:
对consumer而言,关注PWM的如下参数:频率,占空比,极性,开关(使能)
/* include/linux/pwm.h */
/*** pwm_config() - change a PWM device configuration* @pwm: PWM device* @duty_ns: "on" time (in nanoseconds)* @period_ns: duration (in nanoseconds) of one cycle** Returns: 0 on success or a negative error code on failure.*/
static inline int pwm_config(struct pwm_device *pwm, int duty_ns,int period_ns);/*** pwm_enable() - start a PWM output toggling* @pwm: PWM device** Returns: 0 on success or a negative error code on failure.*/
static inline int pwm_enable(struct pwm_device *pwm);/*** pwm_disable() - stop a PWM output toggling* @pwm: PWM device*/
static inline void pwm_disable(struct pwm_device *pwm);/*** pwm_set_polarity() - configure the polarity of a PWM signal* @pwm: PWM device* @polarity: new polarity of the PWM signal** Note that the polarity cannot be configured while the PWM device is* enabled.** Returns: 0 on success or a negative error code on failure.*/
static inline int pwm_set_polarity(struct pwm_device *pwm,enum pwm_polarity polarity);
上面的API都以struct pwm_device类型的指针为操作句柄,该指针抽象了一个PWM设备(consumer不需要关心其内部构成),那么怎么获得PWM句柄呢?使用如下的API:
/* include/linux/pwm.h */struct pwm_device *pwm_get(struct device *dev, const char *con_id);
struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
void pwm_put(struct pwm_device *pwm);struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,const char *con_id);//常用!!
void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
上述api中,常用struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, const char *con_id);
- 参数:
- pwm:PWM设备的指针
- np: 要从中检索 PWM 设备的设备树节点。
- con_id: PWM 消费者的字符串标识符。如果不需要,可以为 NULL。
- 返回值:
- 成功:指向表示 PWM 设备的 pwm_device 结构体的指针
- 失败:错误指针(ERR_PTR)
2.2、向PWM provider提供的APIs
使用说明
初始化pwm chip:填充ops操作函数实例,就是厂家驱动代码
通过pwmchip_add接口注册到kernel中,之后的事情,pwm driver就不用操心了
int pwmchip_add(struct pwm_chip *chip);
int pwmchip_remove(struct pwm_chip *chip);
关键结构体的层级关系如下:
pwm_device仅仅表示一个pwm通道,pwm chip表示一个pwm控制器!!
/*** struct pwm_device - PWM channel object* @label: name of the PWM device* @flags: flags associated with the PWM device* @hwpwm: per-chip relative index of the PWM device* @pwm: global index of the PWM device* @chip: PWM chip providing this PWM device* @chip_data: chip-private data associated with the PWM device* @args: PWM arguments* @state: curent PWM channel state*/
struct pwm_device {const char *label;unsigned long flags;unsigned int hwpwm;//这是 PWM 设备的相对索引,局部于芯片unsigned int pwm;//这是 PWM 设备的系统全局索引struct pwm_chip *chip;void *chip_data;struct pwm_args args;struct pwm_state state;
};struct pwm_args { unsigned int period; /* Device's nitial period */ enum pwm_polarity polarity;
}; struct pwm_state { unsigned int period; /* PWM period (in nanoseconds) */ unsigned int duty_cycle; /* PWM duty cycle (in nanoseconds) */ enum pwm_polarity polarity; /* PWM polarity */ bool enabled; /* PWM enabled status */
}
/*** struct pwm_chip - abstract a PWM controller* @dev: device providing the PWMs* @list: list node for internal use* @ops: callbacks for this PWM controller* @base: number of first PWM controlled by this chip* @npwm: number of PWMs controlled by this chip* @pwms: array of PWM devices allocated by the framework* @of_xlate: request a PWM device given a device tree PWM specifier* @of_pwm_n_cells: number of cells expected in the device tree PWM specifier*/
struct pwm_chip {struct device *dev;//该pwm chip对应的设备,一般由pwm driver对应的platform驱动指定struct list_head list;//const struct pwm_ops *ops;//操作PWM设备的回调函数,后面会详细介绍。必须提供int base;//这是由此芯片控制的第一个 PWM 的编号。如果chip->base < 0,则内核将动态分配一个基数。在将该chip下所有pwm device组成radix tree时使用,只有旧的pwm_request接口会使用,因此忽略它吧,编写pwm driver不需要关心。unsigned int npwm;//该pwm chip可以支持的pwm channel(也可以称作pwm device由struct pwm_device表示)个数struct pwm_device *pwms;//保存所有pwm device的数组,kernel根据npwm自行分配,不需要driver关心。struct pwm_device * (*of_xlate)(struct pwm_chip *pc,const struct of_phandle_args *args);unsigned int of_pwm_n_cells;
};
/*** struct pwm_ops - PWM controller operations* @request: optional hook for requesting a PWM* @free: optional hook for freeing a PWM* @config: configure duty cycles and period length for this PWM* @set_polarity: configure the polarity of this PWM* @capture: capture and report PWM signal* @enable: enable PWM output toggling* @disable: disable PWM output toggling* @apply: atomically apply a new PWM config. The state argument* should be adjusted with the real hardware config (if the* approximate the period or duty_cycle value, state should* reflect it)* @get_state: get the current PWM state. This function is only* called once per PWM device when the PWM chip is* registered.* @dbg_show: optional routine to show contents in debugfs* @owner: helps prevent removal of modules exporting active PWMs*/
struct pwm_ops {int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);//不再使用void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);//不再使用。int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,int duty_ns, int period_ns);int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,enum pwm_polarity polarity);int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,struct pwm_capture *result, unsigned long timeout);int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,struct pwm_state *state);void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,struct pwm_state *state);
#ifdef CONFIG_DEBUG_FSvoid (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
#endifstruct module *owner;
};
关键源码分析:
/*** pwmchip_add() - register a new PWM chip* @chip: the PWM chip to add** Register a new PWM chip. If chip->base < 0 then a dynamically assigned base* will be used. The initial polarity for all channels is normal.** Returns: 0 on success or a negative error code on failure.*/
int pwmchip_add(struct pwm_chip *chip)
{return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
}/*** pwmchip_add_with_polarity() - register a new PWM chip* @chip: the PWM chip to add* @polarity: initial polarity of PWM channels** Register a new PWM chip. If chip->base < 0 then a dynamically assigned base* will be used. The initial polarity for all channels is specified by the* @polarity parameter.** Returns: 0 on success or a negative error code on failure.*/
int pwmchip_add_with_polarity(struct pwm_chip *chip,enum pwm_polarity polarity)
{struct pwm_device *pwm;unsigned int i;int ret;if (!chip || !chip->dev || !chip->ops || !chip->npwm)return -EINVAL;if (!pwm_ops_check(chip->ops))return -EINVAL;mutex_lock(&pwm_lock);ret = alloc_pwms(chip->base, chip->npwm);if (ret < 0)goto out;chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);if (!chip->pwms) {ret = -ENOMEM;goto out;}chip->base = ret;for (i = 0; i < chip->npwm; i++) {pwm = &chip->pwms[i];pwm->chip = chip;pwm->pwm = chip->base + i;pwm->hwpwm = i;pwm->state.polarity = polarity;if (chip->ops->get_state)chip->ops->get_state(chip, pwm, &pwm->state);radix_tree_insert(&pwm_tree, pwm->pwm, pwm);}bitmap_set(allocated_pwms, chip->base, chip->npwm);INIT_LIST_HEAD(&chip->list);list_add(&chip->list, &pwm_chips);ret = 0;if (IS_ENABLED(CONFIG_OF))of_pwmchip_add(chip);out:mutex_unlock(&pwm_lock);if (!ret)pwmchip_sysfs_export(chip);//导出sys fs节点return ret;
}
这个也比较简单,根据这个pwm控制器的channel个数,创建相应的struct pwm_device,最终加到pwm_tree链表树中。这样所有的可以使用的pwm channel都被抽象成了struct pwm_device结构体,也是我们最终的操作结构体。
三、设备驱动层:
四、应用层:
从第一章的系统框架可以看出:应用层使用PWM有两条路径可以选择
-
调用设备驱动来获取核心层提供的数据
- 利用sysfs文件系统直接调用核心层数据,这种方法可以不进行设备树的匹配和设备驱动的编写
导出chip1通道的0设备文件:echo 0 > /sys/class/pwm/pwmchip1/export
配置chip1通道0的周期: echo 10000000 > /sys/class/pwm/pwmchip1 /pwm0/period
配置chip1通道0的占空比:echo 4000000 >/sys/class/pwm/pwmchip1/pwm0/duty_cycle
配置片chip通道0使能: echo 1 > /sys/class/pwm/pwmchip1/pwm0/enable
配置片chip通道0禁能: echo 0 > /sys/class/pwm/pwmchip1/pwm0/enable
取消导出片chip通道0设备文件: echo 0 >/sys/class/pwm/pwmchip1/unexport
PWM 通道使用从 0 到pwm<n-1>
的索引编号。这些数字是相对于芯片的。每个 PWM 通道导出都会在pwmchipN
中创建一个pwmX
目录,该目录与使用的export
文件相同。X是导出的通道号。
完整的 PWM 框架 API 和 sysfs 描述可在内核源树中的Documentation/pwm.txt文件中找到。
/sys/class/pwm/
The pwm/ class sub-directory belongs to the Generic PWM Framework and provides a sysfs interface for using PWM channels/sys/class/pwm/pwmchipN/
A /sys/class/pwm/pwmchipN directory is created for each probed PWM controller/chip where N is the base of the PWM chip./sys/class/pwm/pwmchipN/npwm
The number of PWM channels supported by the PWM chip./sys/class/pwm/pwmchipN/export
Exports a PWM channel from the PWM chip for sysfs control.Value is between 0 and /sys/class/pwm/pwmchipN/npwm - 1./sys/class/pwm/pwmchipN/unexport
Unexports a PWM channel./sys/class/pwm/pwmchipN/pwmX
A /sys/class/pwm/pwmchipN/pwmX directory is created for each exported PWM channel where X is the exported PWM channel number./sys/class/pwm/pwmchipN/pwmX/period
Sets the PWM signal period in nanoseconds./sys/class/pwm/pwmchipN/pwmX/duty_cycle
Sets the PWM signal duty cycle in nanoseconds./sys/class/pwm/pwmchipN/pwmX/enable
Enable/disable the PWM signal.
p17.扩展:为什么可以通过sysyfs操作pwm_哔哩哔哩_bilibili
五、debug方法:
xxx:/ # cat /sys/kernel/debug/pwm
platform/ff680030.pwm, 1 PWM devicepwm-0 ((null) ): period: 0 ns duty: 0 ns polarity: inverseplatform/ff680020.pwm, 1 PWM devicepwm-0 ((null) ): period: 0 ns duty: 0 ns polarity: inverseplatform/ff680010.pwm, 1 PWM devicepwm-0 (backlight ): requested enabled period: 1000000 ns duty: 723313 ns polarity: normal
xxx:/ #
ref:
Linux内核4.14版本——PWM子系统(1)_框架分析-CSDN博客
https://www.cnblogs.com/apachecn/p/18196572
https://wiki.luckfox.com/zh/Luckfox-Pico/Luckfox-Pico-PWM/
https://doc.embedfire.com/linux/imx8mm/linux_base/zh/latest/linux_app/pwm/pwm.html
https://www.cnblogs.com/toutiegongzhu/p/17697360.html