linux驱动编写(电源管理驱动)

Posted 专注it

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux驱动编写(电源管理驱动)相关的知识,希望对你有一定的参考价值。

对于嵌入式设备来说,合适的电源管理,不仅可以延长电池的寿命,而且可以省电,延长设备运行时间,在提高用户体验方面有很大的好处。所以,各个soc厂家在这方面花了很多的功夫。下面,我们可以看看linux是如何处理电源管理驱动的。

 

1、代码目录

drivers/regulator

 

2、查看目录下的Kconfig文件

  1. menuconfig REGULATOR
  2. bool "Voltage and Current Regulator Support"
  3. help
  4. Generic Voltage and Current Regulator support.
  5.  
  6. This framework is designed to provide a generic interface to voltage
  7. and current regulators within the Linux kernel. It‘s intended to
  8. provide voltage and current control to client or consumer drivers and
  9. also provide status information to user space applications through a
  10. sysfs interface.
  11.  
  12. The intention is to allow systems to dynamically control regulator
  13. output in order to save power and prolong battery life. This applies
  14. to both voltage regulators (where voltage output is controllable) and
  15. current sinks (where current output is controllable).
  16.  
  17. This framework safely compiles out if not selected so that client
  18. drivers can still be used in systems with no software controllable
  19. regulators.
  20.  
  21. If unsure, say no.

 

3、阅读文件,得知REGULATOR是最核心的模块macro,那我们可以找一个设备的macro看看

  1. config REGULATOR_STM32_VREFBUF
  2. tristate "STMicroelectronics STM32 VREFBUF"
  3. depends on ARCH_STM32 || COMPILE_TEST
  4. help
  5. This driver supports STMicroelectronics STM32 VREFBUF (voltage
  6. reference buffer) which can be used as voltage reference for
  7. internal ADCs, DACs and also for external components through
  8. dedicated Vref+ pin.
  9.  
  10. This driver can also be built as a module. If so, the module
  11. will be called stm32-vrefbuf.

 

4、没有找到s3c,可以看一下stm32芯片的依赖属性,接着看Makefile

  1. obj-$(CONFIG_REGULATOR) += core.o dummy.o fixed-helper.o helpers.o devres.o
  2. obj-$(CONFIG_OF) += of_regulator.o
  3. obj-$(CONFIG_REGULATOR_FIXED_VOLTAGE) += fixed.o
  4. obj-$(CONFIG_REGULATOR_VIRTUAL_CONSUMER) += virtual.o
  5. obj-$(CONFIG_REGULATOR_USERSPACE_CONSUMER) += userspace-consumer.o
  6.  
  7. obj-$(CONFIG_REGULATOR_STM32_VREFBUF) += stm32-vrefbuf.o

5、看的出来stm32只依赖于stm32-verfbuf.c文件,继续查看

  1. static const struct of_device_id stm32_vrefbuf_of_match[] = {
  2. { .compatible = "st,stm32-vrefbuf", },
  3. {},
  4. };
  5. MODULE_DEVICE_TABLE(of, stm32_vrefbuf_of_match);
  6.  
  7. static struct platform_driver stm32_vrefbuf_driver = {
  8. .probe = stm32_vrefbuf_probe,
  9. .remove = stm32_vrefbuf_remove,
  10. .driver = {
  11. .name = "stm32-vrefbuf",
  12. .of_match_table = of_match_ptr(stm32_vrefbuf_of_match),
  13. },
  14. };
  15. module_platform_driver(stm32_vrefbuf_driver);

 

6、确认驱动为platform驱动,寻找regulator特有的数据结构

  1. static const struct regulator_ops stm32_vrefbuf_volt_ops = {
  2. .enable = stm32_vrefbuf_enable,
  3. .disable = stm32_vrefbuf_disable,
  4. .is_enabled = stm32_vrefbuf_is_enabled,
  5. .get_voltage_sel = stm32_vrefbuf_get_voltage_sel,
  6. .set_voltage_sel = stm32_vrefbuf_set_voltage_sel,
  7. .list_voltage = regulator_list_voltage_table,
  8. };
  9.  
  10. static const struct regulator_desc stm32_vrefbuf_regu = {
  11. .name = "vref",
  12. .supply_name = "vdda",
  13. .volt_table = stm32_vrefbuf_voltages,
  14. .n_voltages = ARRAY_SIZE(stm32_vrefbuf_voltages),
  15. .ops = &stm32_vrefbuf_volt_ops,
  16. .type = REGULATOR_VOLTAGE,
  17. .owner = THIS_MODULE,
  18. };

 

7、由代码得知,regulator_ops和regulator_desc才是特有的regulator数据结构,当然也少不了注册函数

  1. rdev = regulator_register(&stm32_vrefbuf_regu, &config);
  2. if (IS_ERR(rdev)) {
  3. ret = PTR_ERR(rdev);
  4. dev_err(&pdev->dev, "register failed with error %d ", ret);
  5. goto err_clk_dis;
  6. }
  7. platform_set_drvdata(pdev, rdev);

 

8、进一步确认of_device_id是不是真实存在,可以在arch/arm/boot/dts/stm32h743.dtsi找到对应内容

    1. vrefbuf: [email protected]58003C00 {
    2. compatible = "st,stm32-vrefbuf";
    3. reg = <0x58003C00 0x8>;
    4. clocks = <&rcc VREF_CK>;
    5. regulator-min-microvolt = <1500000>;
    6. regulator-max-microvolt = <2500000>;
    7. status = "disabled";
    8. };






以上是关于linux驱动编写(电源管理驱动)的主要内容,如果未能解决你的问题,请参考以下文章

Linux 设备驱动模型

Linux电源管理

Linux电源管理

Linux电源管理

Linux设备驱动框架设计

Linux驱动开发-编写PCF8591(ADC)芯片驱动