Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Copyright (C) 2012 ARM Limited
0004 
0005 #define DRVNAME "vexpress-regulator"
0006 #define pr_fmt(fmt) DRVNAME ": " fmt
0007 
0008 #include <linux/device.h>
0009 #include <linux/err.h>
0010 #include <linux/module.h>
0011 #include <linux/of_device.h>
0012 #include <linux/regulator/driver.h>
0013 #include <linux/regulator/machine.h>
0014 #include <linux/regulator/of_regulator.h>
0015 #include <linux/vexpress.h>
0016 
0017 static int vexpress_regulator_get_voltage(struct regulator_dev *regdev)
0018 {
0019     unsigned int uV;
0020     int err = regmap_read(regdev->regmap, 0, &uV);
0021 
0022     return err ? err : uV;
0023 }
0024 
0025 static int vexpress_regulator_set_voltage(struct regulator_dev *regdev,
0026         int min_uV, int max_uV, unsigned *selector)
0027 {
0028     return regmap_write(regdev->regmap, 0, min_uV);
0029 }
0030 
0031 static const struct regulator_ops vexpress_regulator_ops_ro = {
0032     .get_voltage = vexpress_regulator_get_voltage,
0033 };
0034 
0035 static const struct regulator_ops vexpress_regulator_ops = {
0036     .get_voltage = vexpress_regulator_get_voltage,
0037     .set_voltage = vexpress_regulator_set_voltage,
0038 };
0039 
0040 static int vexpress_regulator_probe(struct platform_device *pdev)
0041 {
0042     struct regulator_desc *desc;
0043     struct regulator_init_data *init_data;
0044     struct regulator_config config = { };
0045     struct regulator_dev *rdev;
0046     struct regmap *regmap;
0047 
0048     desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
0049     if (!desc)
0050         return -ENOMEM;
0051 
0052     regmap = devm_regmap_init_vexpress_config(&pdev->dev);
0053     if (IS_ERR(regmap))
0054         return PTR_ERR(regmap);
0055 
0056     desc->name = dev_name(&pdev->dev);
0057     desc->type = REGULATOR_VOLTAGE;
0058     desc->owner = THIS_MODULE;
0059     desc->continuous_voltage_range = true;
0060 
0061     init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
0062                            desc);
0063     if (!init_data)
0064         return -EINVAL;
0065 
0066     init_data->constraints.apply_uV = 0;
0067     if (init_data->constraints.min_uV && init_data->constraints.max_uV)
0068         desc->ops = &vexpress_regulator_ops;
0069     else
0070         desc->ops = &vexpress_regulator_ops_ro;
0071 
0072     config.regmap = regmap;
0073     config.dev = &pdev->dev;
0074     config.init_data = init_data;
0075     config.of_node = pdev->dev.of_node;
0076 
0077     rdev = devm_regulator_register(&pdev->dev, desc, &config);
0078     return PTR_ERR_OR_ZERO(rdev);
0079 }
0080 
0081 static const struct of_device_id vexpress_regulator_of_match[] = {
0082     { .compatible = "arm,vexpress-volt", },
0083     { }
0084 };
0085 MODULE_DEVICE_TABLE(of, vexpress_regulator_of_match);
0086 
0087 static struct platform_driver vexpress_regulator_driver = {
0088     .probe = vexpress_regulator_probe,
0089     .driver = {
0090         .name = DRVNAME,
0091         .of_match_table = vexpress_regulator_of_match,
0092     },
0093 };
0094 
0095 module_platform_driver(vexpress_regulator_driver);
0096 
0097 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
0098 MODULE_DESCRIPTION("Versatile Express regulator");
0099 MODULE_LICENSE("GPL");
0100 MODULE_ALIAS("platform:vexpress-regulator");