blob: a15a1319436a2256b664b3a46b6df94484ff1b76 [file] [log] [blame]
Pawel Moll31e54082012-09-24 18:56:54 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * Copyright (C) 2012 ARM Limited
12 */
13
14#define DRVNAME "vexpress-regulator"
15#define pr_fmt(fmt) DRVNAME ": " fmt
16
17#include <linux/device.h>
18#include <linux/err.h>
19#include <linux/module.h>
20#include <linux/of_device.h>
21#include <linux/regulator/driver.h>
22#include <linux/regulator/machine.h>
23#include <linux/regulator/of_regulator.h>
24#include <linux/vexpress.h>
25
Pawel Moll31e54082012-09-24 18:56:54 +010026static int vexpress_regulator_get_voltage(struct regulator_dev *regdev)
27{
Axel Lineeb1b232019-04-29 19:35:41 +080028 unsigned int uV;
29 int err = regmap_read(regdev->regmap, 0, &uV);
Pawel Moll31e54082012-09-24 18:56:54 +010030
31 return err ? err : uV;
32}
33
34static int vexpress_regulator_set_voltage(struct regulator_dev *regdev,
35 int min_uV, int max_uV, unsigned *selector)
36{
Axel Lineeb1b232019-04-29 19:35:41 +080037 return regmap_write(regdev->regmap, 0, min_uV);
Pawel Moll31e54082012-09-24 18:56:54 +010038}
39
Axel Linab54a4d2019-04-11 00:19:37 +080040static const struct regulator_ops vexpress_regulator_ops_ro = {
Pawel Moll31e54082012-09-24 18:56:54 +010041 .get_voltage = vexpress_regulator_get_voltage,
42};
43
Axel Linab54a4d2019-04-11 00:19:37 +080044static const struct regulator_ops vexpress_regulator_ops = {
Pawel Moll31e54082012-09-24 18:56:54 +010045 .get_voltage = vexpress_regulator_get_voltage,
46 .set_voltage = vexpress_regulator_set_voltage,
47};
48
49static int vexpress_regulator_probe(struct platform_device *pdev)
50{
Axel Lineeb1b232019-04-29 19:35:41 +080051 struct regulator_desc *desc;
Pawel Moll31e54082012-09-24 18:56:54 +010052 struct regulator_init_data *init_data;
53 struct regulator_config config = { };
Axel Lineeb1b232019-04-29 19:35:41 +080054 struct regulator_dev *rdev;
55 struct regmap *regmap;
Pawel Moll31e54082012-09-24 18:56:54 +010056
Axel Lineeb1b232019-04-29 19:35:41 +080057 desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
58 if (!desc)
Pawel Moll3b9334a2014-04-30 16:46:29 +010059 return -ENOMEM;
Pawel Moll31e54082012-09-24 18:56:54 +010060
Axel Lineeb1b232019-04-29 19:35:41 +080061 regmap = devm_regmap_init_vexpress_config(&pdev->dev);
62 if (IS_ERR(regmap))
63 return PTR_ERR(regmap);
Pawel Moll31e54082012-09-24 18:56:54 +010064
Axel Lineeb1b232019-04-29 19:35:41 +080065 desc->name = dev_name(&pdev->dev);
66 desc->type = REGULATOR_VOLTAGE;
67 desc->owner = THIS_MODULE;
68 desc->continuous_voltage_range = true;
Pawel Moll31e54082012-09-24 18:56:54 +010069
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +010070 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
Axel Lineeb1b232019-04-29 19:35:41 +080071 desc);
Pawel Moll3b9334a2014-04-30 16:46:29 +010072 if (!init_data)
73 return -EINVAL;
Pawel Moll31e54082012-09-24 18:56:54 +010074
75 init_data->constraints.apply_uV = 0;
76 if (init_data->constraints.min_uV && init_data->constraints.max_uV)
Axel Lineeb1b232019-04-29 19:35:41 +080077 desc->ops = &vexpress_regulator_ops;
Pawel Moll31e54082012-09-24 18:56:54 +010078 else
Axel Lineeb1b232019-04-29 19:35:41 +080079 desc->ops = &vexpress_regulator_ops_ro;
Pawel Moll31e54082012-09-24 18:56:54 +010080
Axel Lineeb1b232019-04-29 19:35:41 +080081 config.regmap = regmap;
Pawel Moll31e54082012-09-24 18:56:54 +010082 config.dev = &pdev->dev;
83 config.init_data = init_data;
Pawel Moll31e54082012-09-24 18:56:54 +010084 config.of_node = pdev->dev.of_node;
85
Axel Lineeb1b232019-04-29 19:35:41 +080086 rdev = devm_regulator_register(&pdev->dev, desc, &config);
87 if (IS_ERR(rdev))
88 return PTR_ERR(rdev);
Pawel Moll31e54082012-09-24 18:56:54 +010089
90 return 0;
Pawel Moll31e54082012-09-24 18:56:54 +010091}
92
Jingoo Han1439afd2014-05-07 17:09:12 +090093static const struct of_device_id vexpress_regulator_of_match[] = {
Pawel Moll31e54082012-09-24 18:56:54 +010094 { .compatible = "arm,vexpress-volt", },
Axel Lin9f4e45f2012-10-17 09:00:20 +080095 { }
Pawel Moll31e54082012-09-24 18:56:54 +010096};
Luis de Bethencourt7209fee2015-09-18 19:09:51 +020097MODULE_DEVICE_TABLE(of, vexpress_regulator_of_match);
Pawel Moll31e54082012-09-24 18:56:54 +010098
99static struct platform_driver vexpress_regulator_driver = {
100 .probe = vexpress_regulator_probe,
Pawel Moll31e54082012-09-24 18:56:54 +0100101 .driver = {
102 .name = DRVNAME,
Pawel Moll31e54082012-09-24 18:56:54 +0100103 .of_match_table = vexpress_regulator_of_match,
104 },
105};
106
107module_platform_driver(vexpress_regulator_driver);
108
109MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
110MODULE_DESCRIPTION("Versatile Express regulator");
111MODULE_LICENSE("GPL");
112MODULE_ALIAS("platform:vexpress-regulator");