如何修改device tree


linux Kernel 4.x的driver,增加了一层device tree,是对driver的配置机制。
这样的机制有几个好处:

1.用了哪些驱动,配置参数是什么,有个大纲,看起来一目了然。改起来也方便,改了也不容易出现端口冲突。

2.这样的机制下,同样接口/协议的硬件设备driver,在device tree中的配置非常类似。这样板子改了元器件,需要用不同的driver时,现在修改几行配置生成新的dts, 替换uboot中的dts, 1分钟后板子就能用了。不像以前还要改makefile,编译,重新烧写,很麻烦。

总而言之,对于kernel里已经有的driver,通过配置device tree来做bring up速度极快。

例子基于PWM驱动的风扇
如何知道 device tree node 怎么写
node driver 是什么关系
cat ./Documentation/hwmon/pwm-fan
Kernel driver pwm-fan
=====================
 
This driver enables the use of a PWM module to drive a fan. It uses the
generic PWM interface thus it is hardware independent. It can be used on
many SoCs, as long as the SoC supplies a PWM line driver that exposes
the generic PWM API.
 
Author: Kamil Debski <k.debski@samsung.com>
 
Description
-----------
 
The driver implements a simple interface for driving a fan connected to
a PWM output. It uses the generic PWM interface, thus it can be used with
a range of SoCs. The driver exposes the fan to the user space through
the hwmon's sysfs interface.
 
cat ./Documentation/devicetree/bindings/hwmon/pwm-fan.txt
Bindings for a fan connected to the PWM lines
 
Required properties:
- compatible     : "pwm-fan"
- pwms           : the PWM that is used to control the PWM fan
 
Example:
       pwm-fan {
                 compatible = " pwm-fan ";
                 status = "okay";
                 pwms = <& pwm 0 10000 0>;
       };
emacs drivers/hwmon/pwm-fan.c
 
static SIMPLE_DEV_PM_OPS( pwm_fan_pm , pwm_fan_suspend, pwm_fan_resume);
 
static const struct of_device_id of_pwm_fan_match [] = {
       { .compatible = " pwm-fan ", },
       {},
};
 
static struct platform_driver pwm_fan_driver = {
       .probe         = pwm_fan_probe,
       .remove         = pwm_fan_remove,
       .driver = {
               .name           = "pwm-fan",
               .pm             = & pwm_fan_pm ,
              .of_match_table = of_pwm_fan_match ,
       },
};
 
module_platform_driver(pwm_fan_driver);
 
MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
MODULE_ALIAS("platform:pwm-fan");
MODULE_DESCRIPTION("PWM FAN driver");
 
Node device tree 中的上下文
imx6ul-14x14-evk.dts
imx6ul.dtsi
 
       pwm-fan {
               compatible = " pwm-fan ";
               pwms = <& pwm2 0 10000 0>;
               status = "okay";
       };
 
& pwm2 { /*use & to override* /
       pinctrl-names = "default";
       pinctrl-0 = <& pinctrl_pwm2 >;
       status = "okay";
};
 
               pinctrl_pwm2 : pwm2grp {
                       fsl,pins = <
                                 MX6UL_PAD_GPIO1_IO09__PWM2_OUT     0x110b0
                       >;
              };
                       pwm2 : pwm@02084000 {
                               compatible = "fsl,imx6ul-pwm", "fsl,imx27-pwm";
                                reg = <0x02084000 0x4000>;
                               interrupts = <GIC_SPI 84 IRQ_TYPE_LEVEL_HIGH>;
                               clocks = <&clks IMX6UL_CLK_PWM2>,
                                           <&clks IMX6UL_CLK_PWM2>;
                               clock-names = "ipg", "per";
                               #pwm-cells = <2>;
                       };
 
PWM pin MX6UL_PAD_GPIO1_IO09__PWM2_OUT   Datasheet 中的查找步骤略。

Menuconfig->device drivers->hardware monitoring support->pwm fan->enable
然后编译,烧写。

启动设备后可以通过下面方式操作,控制风扇转速
root@imx6ulevk:/sys/class/hwmon/hwmon0# ls
device   name of_node power   pwm1 subsystem uevent
root@imx6ulevk:/sys/class/hwmon/hwmon0# cat name
pwmfan
root@imx6ulevk:/sys/class/hwmon/hwmon0# cat pwm1
255
root@imx6ulevk:/sys/class/hwmon/hwmon0# echo 128 > pwm1
root@imx6ulevk:/sys/class/hwmon/hwmon0# echo 200 > pwm1
 
至此,没有修改kernel代码,仅仅通过修改devicetree和menuconfig,即实现了对风扇的控制


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值