1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // Copyright (C) 2012 ARM Limited
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #define DRVNAME "vexpress-regulator"
6*4882a593Smuzhiyun #define pr_fmt(fmt) DRVNAME ": " fmt
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/device.h>
9*4882a593Smuzhiyun #include <linux/err.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/of_device.h>
12*4882a593Smuzhiyun #include <linux/regulator/driver.h>
13*4882a593Smuzhiyun #include <linux/regulator/machine.h>
14*4882a593Smuzhiyun #include <linux/regulator/of_regulator.h>
15*4882a593Smuzhiyun #include <linux/vexpress.h>
16*4882a593Smuzhiyun
vexpress_regulator_get_voltage(struct regulator_dev * regdev)17*4882a593Smuzhiyun static int vexpress_regulator_get_voltage(struct regulator_dev *regdev)
18*4882a593Smuzhiyun {
19*4882a593Smuzhiyun unsigned int uV;
20*4882a593Smuzhiyun int err = regmap_read(regdev->regmap, 0, &uV);
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun return err ? err : uV;
23*4882a593Smuzhiyun }
24*4882a593Smuzhiyun
vexpress_regulator_set_voltage(struct regulator_dev * regdev,int min_uV,int max_uV,unsigned * selector)25*4882a593Smuzhiyun static int vexpress_regulator_set_voltage(struct regulator_dev *regdev,
26*4882a593Smuzhiyun int min_uV, int max_uV, unsigned *selector)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun return regmap_write(regdev->regmap, 0, min_uV);
29*4882a593Smuzhiyun }
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun static const struct regulator_ops vexpress_regulator_ops_ro = {
32*4882a593Smuzhiyun .get_voltage = vexpress_regulator_get_voltage,
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun static const struct regulator_ops vexpress_regulator_ops = {
36*4882a593Smuzhiyun .get_voltage = vexpress_regulator_get_voltage,
37*4882a593Smuzhiyun .set_voltage = vexpress_regulator_set_voltage,
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun
vexpress_regulator_probe(struct platform_device * pdev)40*4882a593Smuzhiyun static int vexpress_regulator_probe(struct platform_device *pdev)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun struct regulator_desc *desc;
43*4882a593Smuzhiyun struct regulator_init_data *init_data;
44*4882a593Smuzhiyun struct regulator_config config = { };
45*4882a593Smuzhiyun struct regulator_dev *rdev;
46*4882a593Smuzhiyun struct regmap *regmap;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
49*4882a593Smuzhiyun if (!desc)
50*4882a593Smuzhiyun return -ENOMEM;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun regmap = devm_regmap_init_vexpress_config(&pdev->dev);
53*4882a593Smuzhiyun if (IS_ERR(regmap))
54*4882a593Smuzhiyun return PTR_ERR(regmap);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun desc->name = dev_name(&pdev->dev);
57*4882a593Smuzhiyun desc->type = REGULATOR_VOLTAGE;
58*4882a593Smuzhiyun desc->owner = THIS_MODULE;
59*4882a593Smuzhiyun desc->continuous_voltage_range = true;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
62*4882a593Smuzhiyun desc);
63*4882a593Smuzhiyun if (!init_data)
64*4882a593Smuzhiyun return -EINVAL;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun init_data->constraints.apply_uV = 0;
67*4882a593Smuzhiyun if (init_data->constraints.min_uV && init_data->constraints.max_uV)
68*4882a593Smuzhiyun desc->ops = &vexpress_regulator_ops;
69*4882a593Smuzhiyun else
70*4882a593Smuzhiyun desc->ops = &vexpress_regulator_ops_ro;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun config.regmap = regmap;
73*4882a593Smuzhiyun config.dev = &pdev->dev;
74*4882a593Smuzhiyun config.init_data = init_data;
75*4882a593Smuzhiyun config.of_node = pdev->dev.of_node;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun rdev = devm_regulator_register(&pdev->dev, desc, &config);
78*4882a593Smuzhiyun return PTR_ERR_OR_ZERO(rdev);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun static const struct of_device_id vexpress_regulator_of_match[] = {
82*4882a593Smuzhiyun { .compatible = "arm,vexpress-volt", },
83*4882a593Smuzhiyun { }
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, vexpress_regulator_of_match);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun static struct platform_driver vexpress_regulator_driver = {
88*4882a593Smuzhiyun .probe = vexpress_regulator_probe,
89*4882a593Smuzhiyun .driver = {
90*4882a593Smuzhiyun .name = DRVNAME,
91*4882a593Smuzhiyun .of_match_table = vexpress_regulator_of_match,
92*4882a593Smuzhiyun },
93*4882a593Smuzhiyun };
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun module_platform_driver(vexpress_regulator_driver);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
98*4882a593Smuzhiyun MODULE_DESCRIPTION("Versatile Express regulator");
99*4882a593Smuzhiyun MODULE_LICENSE("GPL");
100*4882a593Smuzhiyun MODULE_ALIAS("platform:vexpress-regulator");
101