Linux驱动程序insmod中的PWM

时间:2017-09-04 18:16:20

标签: timer linux-device-driver kernel-module

我正在尝试为Zynq PS insmod一个pwm三重定时器计数器(TTC)驱动程序。 dmesg日志是:

TTC: Inside probe function
pwm-cadence f8001000.timer: PWM 0 has clock source 0 at 108333336 Hz
pwm-cadence f8001000.timer: PWM 1 has clock source 0 at 108333336 Hz
pwm-cadence f8001000.timer: PWM 2 has clock source 0 at 108333336 Hz
pwm-cadence f8001000.timer: cannot add pwm chip (error -22)

当struct cpwm->芯片中的某些字段未初始化时,是否会发生EINVAL(错误-22)功能? 我是这个领域的新手。所以任何提示都会受到赞赏。

static int cadence_pwm_probe(struct platform_device *pdev)
{
    struct cadence_pwm_chip *cpwm;
    struct resource *r_mem;
    int ret;
    struct device_node *node = pdev->dev.of_node;
    const __be32 *value;
    int rlen;
    char propname[24];
    int i;
    struct cadence_pwm_pwm *pwm;
    printk(KERN_DEBUG "TTC: Inside probe function\n");
    cpwm = devm_kzalloc(&pdev->dev, sizeof(*cpwm), GFP_KERNEL);
    if (!cpwm)
        return -ENOMEM;

    r_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    cpwm->base = devm_ioremap_resource(&pdev->dev, r_mem);
    if (IS_ERR(cpwm->base))
       return PTR_ERR(cpwm->base);

    for (i = 0; i < CPWM_NUM_PWM; i ++) {
       pwm = cpwm->pwms + i;

    snprintf(propname, sizeof(propname), "xlnx,ttc-clk%d-freq-hz", i);

    value = of_get_property(node, propname, &rlen);
    if (value)
       pwm->clk_hz = be32_to_cpup(value);
    else {
       dev_err(&pdev->dev, "missing %s property1", propname);
       return -ENODEV;
    }

    snprintf(propname, sizeof(propname), "xlnx,ttc-clk%d-clksrc", i);

    value = of_get_property(node, propname, &rlen);
    if (value)
       pwm->source = be32_to_cpup(value);
    else {
       dev_err(&pdev->dev, "missing %s property2", propname);
       return -ENODEV;
    }

    dev_info(&pdev->dev, "PWM %d has clock source %d at %d Hz", i, pwm->source, pwm->clk_hz);

}

cpwm->chip.dev = &pdev->dev;
cpwm->chip.ops = &cadence_pwm_ops;
cpwm->chip.npwm = CPWM_NUM_PWM;
cpwm->chip.base = -1;

ret = pwmchip_add(&cpwm->chip);
if (ret < 0) {
    dev_err(&pdev->dev, "cannot add pwm chip (error %d)", ret);
    return ret;
}

1 个答案:

答案 0 :(得分:0)

嗯,代码看起来不错。这意味着添加PWM芯片不能失败 - 所有需要的字段都设置好了。

但是,仍然可以根据唯一的剩余原因返回EINVAL,即因为在内核构建配置中禁用了对PWM(CONFIG_PWM)的支持。这来自include/linux/pwm.h文件,其中使用了条件编译。因此,如果已启用CONFIG_PWM选项,则存在正确的符号。而且,当您构建驱动程序时,将从标头中使用this函数原型。但是如果内核是在没有PWM支持的情况下构建的,那么在驱动程序编译期间将使用inline function,这是一个简单的存根,在任何情况下都会返回EINVAL

总而言之,您需要正确检查构建配置。