应该是当前内核pwm框架与pwm驱动对于极性的判断相反导致,并且kernel阶段的pwm极性与uboot阶段的不一致。
可以参考下面的解决方法:
添加函数将极性配置与uboot阶段保持一致的极性状态。具体方法是在pwm代码中添加接口:
static void sunxi_pwm_get_state(struct pwm_chip *chip,
struct pwm_device *pwm,
struct pwm_state *state)
{
unsigned int reg_offset;
u32 val, sel;
sel = pwm->pwm - chip->base;
reg_offset = PWM_PCR_BASE + sel * 0x20;
val = sunxi_pwm_readl(chip, reg_offset);
if (val & BIT_MASK(8)) {
state->polarity = PWM_POLARITY_NORMAL;
} else {
state->polarity = PWM_POLARITY_INVERSED;
}
}
static struct pwm_ops sunxi_pwm_ops = {
...
.get_state = sunxi_pwm_get_state,
...
};
这个函数的作用是在pwm驱动初始化阶段,读取uboot阶段设置的极性,并将kernel阶段的极性设置与其保持一致。具体内核操作可追流程。
除了修改驱动代码,还需要同对应修改每个板级目录下的board.dts。该修改主要是针对regulator使用pwm进行调压,会在dts中配置pwm调压的极性,为了保证与uboot阶段的电压一致,现在需要把dts中配置的极性的值置位1。
reg_vdd_cpu: vdd-cpu {
compatible = "sunxi-pwm-regulator";
- pwms = <&pwm 0 5000 0>; // 第一个参数是使用的pwm通道,第二个参数是频率,第三个参数是极性
+ pwms = <&pwm 0 5000 1>; // 第一个参数是使用的pwm通道,第二个参数是频率,第三个参数是极性
regulator-name = "vdd_cpu";
regulator-min-microvolt = <810000>;
regulator-max-microvolt = <1160000>;
regulator-ramp-delay = <25>;
regulator-always-on;
regulator-boot-on;
status = "okay";
};
在修改完成后,编译打包,确认版型后烧录测试,先看板子能不能起来,起不来的话证明电压极性有误,可以拿万用表测量一下电压,看启动阶段电压有没有发生突变。成功起来后还需要测试一下pwm功能是否正常,最好使用示波器来测一下board.dts里配置的pwm通道的管脚,然后调试该通道看有无波形变化,具体方法如下:
进入/sys/class/pwm目录,该目录是linux内核为pwm子系统提供的类目录,遍历该目录:
/sys/class/pwm # ls
pwmchip0
可以看到,上述 pwmchip0 就是我们注册的pwm控制器,进入该目录,然后遍历该目录:
/sys/class/pwm # cd pwmchip0/
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0 # ls
device export npwm subsystem uevent unexport
其中npwm文件储存了该pwm控制器的pwm个数,而export和unexport是导出和删除某个pwm设备的文件,下面演示导出pwm1。
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0 # cat npwm
2
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0 # echo 1 > export
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0 # ls
device export npwm pwm1 subsystem uevent unexport
可以看到目录中多出pwm1目录,进入该目录,遍历:
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0 # cd pwm1/
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0/pwm1 # ls
capture duty_cycle enable period polarity uevent
该目录中,enable是使能pwm,duty_cycle是占空比,period是周期,polarity是极性,可以配置相关的pwm并且使能:
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0/pwm1 # echo 1000000000 > period
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0/pwm1 # echo 500000000 > duty_cycle
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0/pwm1 # echo normal > polarity
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0/pwm1 # echo 1 > enable
如果相应的引脚外接了示波器,可以看到相关的波形。最后返回上层目录,删除该pwm设备:
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0/pwm1 # cd ..
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0 # ls
device export npwm pwm1 subsystem uevent unexport
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0 # echo 1 > unexport
/sys/devices/platform/soc/1c23400.pwm/pwm/pwmchip0 # ls
device export npwm subsystem uevent unexport
在有条件的情况且能找到相应屏幕的情况下,最好接上屏幕进行验证,确认在uboot阶段进入kernel阶段出现闪屏的情况。
不带屏幕,且没有vdd-cpu的话,pwm初始化极性就不会影响到默认固件正常使用,使用示波器验证就好。