详解linux pwm驱动编写

详解linux pwm驱动编写,第1张

详解linux pwm驱动编写

pwm方波可以用来控制很多的设备,比如它可以被用来控制电机。简单来说,就是单位时间内的方波越多,那么电机的转速就会越快;反之就越慢。通过这个特性,soc就可以轻松地利用pwm对外设进行自动控制。所以,今天的主题就是pwm驱动。

1、驱动目录

drivers/pwm

2、查看对应目录下的Kconfig

config PWM_SAMSUNG
 tristate "Samsung PWM support"
 depends on PLAT_SAMSUNG || ARCH_EXYNOS
 help
  Generic PWM framework driver for Samsung.

  To compile this driver as a module, choose M here: the module
  will be called pwm-samsung.

3、确认PWM_SAMSUNG只依赖于自己之外,继续看Makefile

obj-$(CONFIG_PWM) += core.o

obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o

4、根据Makefile查阅pwm-samsung.c文件,结构比较清楚

static struct platform_driver pwm_samsung_driver = {
 .driver = {
 .name = "samsung-pwm",
 .pm = &pwm_samsung_pm_ops,
 .of_match_table = of_match_ptr(samsung_pwm_matches),
 },
 .probe = pwm_samsung_probe,
 .remove = pwm_samsung_remove,
};
module_platform_driver(pwm_samsung_driver);

5、soc设备大多数是platform设备,继续寻找probe函数中的有用信息

ret = pwmchip_add(&chip->chip);
 if (ret < 0) {
 dev_err(dev, "failed to register PWM chip\n");
 clk_disable_unprepare(chip->base_clk);
 return ret;
 }

6、找到注册函数后,接续看看函数接口点在什么地方

static const struct pwm_ops pwm_samsung_ops = {
 .request = pwm_samsung_request,
 .free = pwm_samsung_free,
 .enable = pwm_samsung_enable,
 .disable = pwm_samsung_disable,
 .config = pwm_samsung_config,
 .set_polarity = pwm_samsung_set_polarity,
 .owner = THIS_MODULE,
};

7、pwm设备没有中断函数,一般是立马生效,除此之外,代码中还是有设备树的内容,可以看看

static const struct of_device_id samsung_pwm_matches[] = {
 { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
 { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
 { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
 { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
 { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
 {},
};
MODULE_DEVICE_TABLE(of, samsung_pwm_matches);

static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
{
 struct device_node *np = chip->chip.dev->of_node;
 const struct of_device_id *match;
 struct property *prop;
 const __be32 *cur;
 u32 val;

 match = of_match_node(samsung_pwm_matches, np);
 if (!match)
 return -ENODEV;

 memcpy(&chip->variant, match->data, sizeof(chip->variant));

 of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
 if (val >= SAMSUNG_PWM_NUM) {
  dev_err(chip->chip.dev,
  "%s: invalid channel index in samsung,pwm-outputs property\n",
    __func__);
  continue;
 }
 chip->variant.output_mask |= BIT(val);
 }

 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/903912.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存