regulator demo

最近工作涉及到电源管理的内容,因此熟悉了一下regulator的部分

在已有regulator的框架的情况下,主打的就是一个devm_regulator_register()
有时候这个注册会嵌套在一个for循环里面…目测…目测是要自己创建N多个电源

devm_regulator_register涉及到的参数主要如下:
struct regulator_init_data
这个结构里面有最大最小电压之类的设置,设置不正确,在调set_voltage的时候可能中途就直接返回了
还有一个valid_ops_mask,他决定了你的regulator_dev有什么操作,enable,set之类的接口会检查valid,如果检查失败了,也就返回了。

struct regulator_desc
这个结构目前看主要就是设置,struct regulator_ops,其他的参数没有跟进去看了。

在regulator框架已有的情况下,大致的使用思路是这样的,下面是一个qemu上运行的demo
没有物理设备,主要就是跟踪一下各个接口的功能

#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_domain.h>
#include <linux/pm_opp.h>
#include <linux/reboot.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/fixed.h>
#include <linux/gpio/consumer.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/regulator/of_regulator.h>
#include <linux/regulator/machine.h>
#include <linux/clk.h>

// valid_ops_mask关系到一些enable和设置的功能,min_uV,max_uV关系到set的时候值的合法检测
static struct regulator_init_data init_data = {
	.constraints = {
		.min_uV = 1800000,
		.max_uV = 3800000,
		.valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE,
	},
};

static int xiaoshidi_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV, unsigned *selector)
{
	printk("xiaoshidi-debug-[%s]:%s min:%d, max:%d\n", __func__, rdev->desc->name, min_uV, max_uV);
	return 0;
}

static int xiaoshidi_get_voltage(struct regulator_dev *rdev)
{
	printk("xiaoshidi-debug-[%s]:%s\n", __func__, rdev->desc->name);
	return 0;
}

static int xiaoshidi_enable(struct regulator_dev *rdev)
{
	printk("xiaoshidi-debug-[%s]:%s\n", __func__, rdev->desc->name);
	return 0;
}

static int xiaoshidi_disable(struct regulator_dev *rdev)
{
	printk("xiaoshidi-debug-[%s]:%s\n", __func__, rdev->desc->name);
	return 0;
}

static int xiaoshidi_is_enabled(struct regulator_dev *rdev)
{
	int ret = 0;
	printk("xiaoshidi-debug-[%s]:%s\n", __func__, rdev->desc->name);
	return ret;
}

static const struct regulator_ops xiaoshidi_regulator_ops = {
	.enable = xiaoshidi_enable,
	.disable = xiaoshidi_disable,
	.is_enabled = xiaoshidi_is_enabled,
	.set_voltage = xiaoshidi_set_voltage,
	.get_voltage = xiaoshidi_get_voltage,
};

// ops
static const struct regulator_desc xiaoshidi_regulators[] = {
	{
		.id = 0,
		.type = REGULATOR_VOLTAGE,
		.name = "buck1",
		.vsel_reg  = 0x01,
		.vsel_mask = 0xff,
		.ops = &xiaoshidi_regulator_ops,
		.owner = THIS_MODULE
	},
	{
		.id = 1,
		.type = REGULATOR_VOLTAGE,
		.name = "buck2",
		.vsel_reg  = 0x02,
		.vsel_mask = 0xff,
		.ops = &xiaoshidi_regulator_ops,
		.owner = THIS_MODULE
	},
};

static int reg_fixed_voltage_probe(struct platform_device *pdev)
{
	int ret;
	struct regulator *r;
	struct regulator_dev *rdev;
	struct regulator_config config = {};

	for (int i = 0; i < ARRAY_SIZE(xiaoshidi_regulators); i++) {
		config.dev = &pdev->dev;
		config.init_data = &(init_data);
		rdev = devm_regulator_register(&pdev->dev,
				&(xiaoshidi_regulators[i]), &config);
		if (IS_ERR(rdev)) {
			dev_err(&pdev->dev, "failed to register %s\n",
				xiaoshidi_regulators[i].name);
			return PTR_ERR(rdev);
		}
		pr_info("add regulator:%p\n", rdev);
	}

	r = regulator_get(&(pdev->dev), "buck1");
	r = regulator_get(&(pdev->dev), "buck2");
	ret = regulator_enable(r);
	ret = regulator_set_voltage(r, 2500000, 3500000);
	ret = regulator_get_voltage(r);
	ret = regulator_disable(r);

	return 0;
}

static const struct of_device_id fixed_of_match[] = {
	{
		.compatible = "regulator-xiaoshididebug",
	},
};

static struct platform_driver regulator_fixed_voltage_driver = {
	.probe		= reg_fixed_voltage_probe,
	.driver		= {
		.name		= "xiaoshidi-fixed-voltage",\
		.of_match_table = of_match_ptr(fixed_of_match),
	},
};

static int __init regulator_fixed_voltage_init(void)
{
	return platform_driver_register(&regulator_fixed_voltage_driver);
}
subsys_initcall(regulator_fixed_voltage_init);

static void __exit regulator_fixed_voltage_exit(void)
{
	platform_driver_unregister(&regulator_fixed_voltage_driver);
}
module_exit(regulator_fixed_voltage_exit);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值