blob: 1235f46e633eff2487286ffee82534eaf729b127 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (C) 2012 ARM Limited
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004
5#define DRVNAME "vexpress-regulator"
6#define pr_fmt(fmt) DRVNAME ": " fmt
7
8#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/module.h>
11#include <linux/of_device.h>
12#include <linux/regulator/driver.h>
13#include <linux/regulator/machine.h>
14#include <linux/regulator/of_regulator.h>
15#include <linux/vexpress.h>
16
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017static int vexpress_regulator_get_voltage(struct regulator_dev *regdev)
18{
David Brazdil0f672f62019-12-10 10:32:29 +000019 unsigned int uV;
20 int err = regmap_read(regdev->regmap, 0, &uV);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021
22 return err ? err : uV;
23}
24
25static int vexpress_regulator_set_voltage(struct regulator_dev *regdev,
26 int min_uV, int max_uV, unsigned *selector)
27{
David Brazdil0f672f62019-12-10 10:32:29 +000028 return regmap_write(regdev->regmap, 0, min_uV);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029}
30
David Brazdil0f672f62019-12-10 10:32:29 +000031static const struct regulator_ops vexpress_regulator_ops_ro = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032 .get_voltage = vexpress_regulator_get_voltage,
33};
34
David Brazdil0f672f62019-12-10 10:32:29 +000035static const struct regulator_ops vexpress_regulator_ops = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036 .get_voltage = vexpress_regulator_get_voltage,
37 .set_voltage = vexpress_regulator_set_voltage,
38};
39
40static int vexpress_regulator_probe(struct platform_device *pdev)
41{
David Brazdil0f672f62019-12-10 10:32:29 +000042 struct regulator_desc *desc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043 struct regulator_init_data *init_data;
44 struct regulator_config config = { };
David Brazdil0f672f62019-12-10 10:32:29 +000045 struct regulator_dev *rdev;
46 struct regmap *regmap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047
David Brazdil0f672f62019-12-10 10:32:29 +000048 desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
49 if (!desc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050 return -ENOMEM;
51
David Brazdil0f672f62019-12-10 10:32:29 +000052 regmap = devm_regmap_init_vexpress_config(&pdev->dev);
53 if (IS_ERR(regmap))
54 return PTR_ERR(regmap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055
David Brazdil0f672f62019-12-10 10:32:29 +000056 desc->name = dev_name(&pdev->dev);
57 desc->type = REGULATOR_VOLTAGE;
58 desc->owner = THIS_MODULE;
59 desc->continuous_voltage_range = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060
61 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
David Brazdil0f672f62019-12-10 10:32:29 +000062 desc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063 if (!init_data)
64 return -EINVAL;
65
66 init_data->constraints.apply_uV = 0;
67 if (init_data->constraints.min_uV && init_data->constraints.max_uV)
David Brazdil0f672f62019-12-10 10:32:29 +000068 desc->ops = &vexpress_regulator_ops;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069 else
David Brazdil0f672f62019-12-10 10:32:29 +000070 desc->ops = &vexpress_regulator_ops_ro;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071
David Brazdil0f672f62019-12-10 10:32:29 +000072 config.regmap = regmap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073 config.dev = &pdev->dev;
74 config.init_data = init_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075 config.of_node = pdev->dev.of_node;
76
David Brazdil0f672f62019-12-10 10:32:29 +000077 rdev = devm_regulator_register(&pdev->dev, desc, &config);
78 if (IS_ERR(rdev))
79 return PTR_ERR(rdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080
81 return 0;
82}
83
84static const struct of_device_id vexpress_regulator_of_match[] = {
85 { .compatible = "arm,vexpress-volt", },
86 { }
87};
88MODULE_DEVICE_TABLE(of, vexpress_regulator_of_match);
89
90static struct platform_driver vexpress_regulator_driver = {
91 .probe = vexpress_regulator_probe,
92 .driver = {
93 .name = DRVNAME,
94 .of_match_table = vexpress_regulator_of_match,
95 },
96};
97
98module_platform_driver(vexpress_regulator_driver);
99
100MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
101MODULE_DESCRIPTION("Versatile Express regulator");
102MODULE_LICENSE("GPL");
103MODULE_ALIAS("platform:vexpress-regulator");