blob: 5d39663efcaad1d86fd4d83f9b0f3c2d12381d47 [file] [log] [blame]
Axel Linc5e911a2019-04-29 19:35:42 +08001// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (C) 2012 ARM Limited
Pawel Moll31e54082012-09-24 18:56:54 +01004
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
Pawel Moll31e54082012-09-24 18:56:54 +010017static int vexpress_regulator_get_voltage(struct regulator_dev *regdev)
18{
Axel Lineeb1b232019-04-29 19:35:41 +080019 unsigned int uV;
20 int err = regmap_read(regdev->regmap, 0, &uV);
Pawel Moll31e54082012-09-24 18:56:54 +010021
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{
Axel Lineeb1b232019-04-29 19:35:41 +080028 return regmap_write(regdev->regmap, 0, min_uV);
Pawel Moll31e54082012-09-24 18:56:54 +010029}
30
Axel Linab54a4d2019-04-11 00:19:37 +080031static const struct regulator_ops vexpress_regulator_ops_ro = {
Pawel Moll31e54082012-09-24 18:56:54 +010032 .get_voltage = vexpress_regulator_get_voltage,
33};
34
Axel Linab54a4d2019-04-11 00:19:37 +080035static const struct regulator_ops vexpress_regulator_ops = {
Pawel Moll31e54082012-09-24 18:56:54 +010036 .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{
Axel Lineeb1b232019-04-29 19:35:41 +080042 struct regulator_desc *desc;
Pawel Moll31e54082012-09-24 18:56:54 +010043 struct regulator_init_data *init_data;
44 struct regulator_config config = { };
Axel Lineeb1b232019-04-29 19:35:41 +080045 struct regulator_dev *rdev;
46 struct regmap *regmap;
Pawel Moll31e54082012-09-24 18:56:54 +010047
Axel Lineeb1b232019-04-29 19:35:41 +080048 desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
49 if (!desc)
Pawel Moll3b9334a2014-04-30 16:46:29 +010050 return -ENOMEM;
Pawel Moll31e54082012-09-24 18:56:54 +010051
Axel Lineeb1b232019-04-29 19:35:41 +080052 regmap = devm_regmap_init_vexpress_config(&pdev->dev);
53 if (IS_ERR(regmap))
54 return PTR_ERR(regmap);
Pawel Moll31e54082012-09-24 18:56:54 +010055
Axel Lineeb1b232019-04-29 19:35:41 +080056 desc->name = dev_name(&pdev->dev);
57 desc->type = REGULATOR_VOLTAGE;
58 desc->owner = THIS_MODULE;
59 desc->continuous_voltage_range = true;
Pawel Moll31e54082012-09-24 18:56:54 +010060
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +010061 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
Axel Lineeb1b232019-04-29 19:35:41 +080062 desc);
Pawel Moll3b9334a2014-04-30 16:46:29 +010063 if (!init_data)
64 return -EINVAL;
Pawel Moll31e54082012-09-24 18:56:54 +010065
66 init_data->constraints.apply_uV = 0;
67 if (init_data->constraints.min_uV && init_data->constraints.max_uV)
Axel Lineeb1b232019-04-29 19:35:41 +080068 desc->ops = &vexpress_regulator_ops;
Pawel Moll31e54082012-09-24 18:56:54 +010069 else
Axel Lineeb1b232019-04-29 19:35:41 +080070 desc->ops = &vexpress_regulator_ops_ro;
Pawel Moll31e54082012-09-24 18:56:54 +010071
Axel Lineeb1b232019-04-29 19:35:41 +080072 config.regmap = regmap;
Pawel Moll31e54082012-09-24 18:56:54 +010073 config.dev = &pdev->dev;
74 config.init_data = init_data;
Pawel Moll31e54082012-09-24 18:56:54 +010075 config.of_node = pdev->dev.of_node;
76
Axel Lineeb1b232019-04-29 19:35:41 +080077 rdev = devm_regulator_register(&pdev->dev, desc, &config);
zhengbin1d7c4c12019-11-18 18:59:22 +080078 return PTR_ERR_OR_ZERO(rdev);
Pawel Moll31e54082012-09-24 18:56:54 +010079}
80
Jingoo Han1439afd2014-05-07 17:09:12 +090081static const struct of_device_id vexpress_regulator_of_match[] = {
Pawel Moll31e54082012-09-24 18:56:54 +010082 { .compatible = "arm,vexpress-volt", },
Axel Lin9f4e45f2012-10-17 09:00:20 +080083 { }
Pawel Moll31e54082012-09-24 18:56:54 +010084};
Luis de Bethencourt7209fee2015-09-18 19:09:51 +020085MODULE_DEVICE_TABLE(of, vexpress_regulator_of_match);
Pawel Moll31e54082012-09-24 18:56:54 +010086
87static struct platform_driver vexpress_regulator_driver = {
88 .probe = vexpress_regulator_probe,
Pawel Moll31e54082012-09-24 18:56:54 +010089 .driver = {
90 .name = DRVNAME,
Pawel Moll31e54082012-09-24 18:56:54 +010091 .of_match_table = vexpress_regulator_of_match,
92 },
93};
94
95module_platform_driver(vexpress_regulator_driver);
96
97MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
98MODULE_DESCRIPTION("Versatile Express regulator");
99MODULE_LICENSE("GPL");
100MODULE_ALIAS("platform:vexpress-regulator");