详解linux电源管理驱动编写
对于嵌入式设备来说,合适的电源管理,不仅可以延长电池的寿命,而且可以省电,延长设备运行时间,在提高用户体验方面有很大的好处。所以,各个soc厂家在这方面花了很多的功夫。下面,我们可以看看linux是如何处理电源管理驱动的。
1、代码目录
drivers/regulator
2、查看目录下的Kconfig文件
menuconfigREGULATOR bool"VoltageandCurrentRegulatorSupport" help GenericVoltageandCurrentRegulatorsupport. Thisframeworkisdesignedtoprovideagenericinterfacetovoltage andcurrentregulatorswithintheLinuxkernel.It'sintendedto providevoltageandcurrentcontroltoclientorconsumerdriversand alsoprovidestatusinformationtouserspaceapplicationsthrougha sysfsinterface. Theintentionistoallowsystemstodynamicallycontrolregulator outputinordertosavepowerandprolongbatterylife.Thisapplies tobothvoltageregulators(wherevoltageoutputiscontrollable)and currentsinks(wherecurrentoutputiscontrollable). Thisframeworksafelycompilesoutifnotselectedsothatclient driverscanstillbeusedinsystemswithnosoftwarecontrollable regulators. Ifunsure,sayno.
3、阅读文件,得知REGULATOR是最核心的模块macro,那我们可以找一个设备的macro看看
configREGULATOR_STM32_VREFBUF tristate"STMicroelectronicsSTM32VREFBUF" dependsonARCH_STM32||COMPILE_TEST help ThisdriversupportsSTMicroelectronicsSTM32VREFBUF(voltage referencebuffer)whichcanbeusedasvoltagereferencefor internalADCs,DACsandalsoforexternalcomponentsthrough dedicatedVref+pin. Thisdrivercanalsobebuiltasamodule.Ifso,themodule willbecalledstm32-vrefbuf.
4、没有找到s3c,可以看一下stm32芯片的依赖属性,接着看Makefile
obj-$(CONFIG_REGULATOR)+=core.odummy.ofixed-helper.ohelpers.odevres.o obj-$(CONFIG_OF)+=of_regulator.o obj-$(CONFIG_REGULATOR_FIXED_VOLTAGE)+=fixed.o obj-$(CONFIG_REGULATOR_VIRTUAL_CONSUMER)+=virtual.o obj-$(CONFIG_REGULATOR_USERSPACE_CONSUMER)+=userspace-consumer.o obj-$(CONFIG_REGULATOR_STM32_VREFBUF)+=stm32-vrefbuf.o
5、看的出来stm32只依赖于stm32-verfbuf.c文件,继续查看
staticconststructof_device_idstm32_vrefbuf_of_match[]={
{.compatible="st,stm32-vrefbuf",},
{},
};
MODULE_DEVICE_TABLE(of,stm32_vrefbuf_of_match);
staticstructplatform_driverstm32_vrefbuf_driver={
.probe=stm32_vrefbuf_probe,
.remove=stm32_vrefbuf_remove,
.driver={
.name="stm32-vrefbuf",
.of_match_table=of_match_ptr(stm32_vrefbuf_of_match),
},
};
module_platform_driver(stm32_vrefbuf_driver);
6、确认驱动为platform驱动,寻找regulator特有的数据结构
staticconststructregulator_opsstm32_vrefbuf_volt_ops={
.enable=stm32_vrefbuf_enable,
.disable=stm32_vrefbuf_disable,
.is_enabled=stm32_vrefbuf_is_enabled,
.get_voltage_sel=stm32_vrefbuf_get_voltage_sel,
.set_voltage_sel=stm32_vrefbuf_set_voltage_sel,
.list_voltage=regulator_list_voltage_table,
};
staticconststructregulator_descstm32_vrefbuf_regu={
.name="vref",
.supply_name="vdda",
.volt_table=stm32_vrefbuf_voltages,
.n_voltages=ARRAY_SIZE(stm32_vrefbuf_voltages),
.ops=&stm32_vrefbuf_volt_ops,
.type=REGULATOR_VOLTAGE,
.owner=THIS_MODULE,
};
7、由代码得知,regulator_ops和regulator_desc才是特有的regulator数据结构,当然也少不了注册函数
rdev=regulator_register(&stm32_vrefbuf_regu,&config);
if(IS_ERR(rdev)){
ret=PTR_ERR(rdev);
dev_err(&pdev->dev,"registerfailedwitherror%d\n",ret);
gotoerr_clk_dis;
}
platform_set_drvdata(pdev,rdev);
8、进一步确认of_device_id是不是真实存在,可以在arch/arm/boot/dts/stm32h743.dtsi找到对应内容
vrefbuf:regulator@58003C00{
compatible="st,stm32-vrefbuf";
reg=<0x58003C000x8>;
clocks=<&rccVREF_CK>;
regulator-min-microvolt=<1500000>;
regulator-max-microvolt=<2500000>;
status="disabled";
};
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。