blob: 6ab78bdbe138ef98685c8def4ecdb6c0f190fe44 [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
26struct vexpress_regulator {
27 struct regulator_desc desc;
28 struct regulator_dev *regdev;
Pawel Moll3b9334a2014-04-30 16:46:29 +010029 struct regmap *regmap;
Pawel Moll31e54082012-09-24 18:56:54 +010030};
31
32static int vexpress_regulator_get_voltage(struct regulator_dev *regdev)
33{
34 struct vexpress_regulator *reg = rdev_get_drvdata(regdev);
35 u32 uV;
Pawel Moll3b9334a2014-04-30 16:46:29 +010036 int err = regmap_read(reg->regmap, 0, &uV);
Pawel Moll31e54082012-09-24 18:56:54 +010037
38 return err ? err : uV;
39}
40
41static int vexpress_regulator_set_voltage(struct regulator_dev *regdev,
42 int min_uV, int max_uV, unsigned *selector)
43{
44 struct vexpress_regulator *reg = rdev_get_drvdata(regdev);
45
Pawel Moll3b9334a2014-04-30 16:46:29 +010046 return regmap_write(reg->regmap, 0, min_uV);
Pawel Moll31e54082012-09-24 18:56:54 +010047}
48
49static struct regulator_ops vexpress_regulator_ops_ro = {
50 .get_voltage = vexpress_regulator_get_voltage,
51};
52
53static struct regulator_ops vexpress_regulator_ops = {
54 .get_voltage = vexpress_regulator_get_voltage,
55 .set_voltage = vexpress_regulator_set_voltage,
56};
57
58static int vexpress_regulator_probe(struct platform_device *pdev)
59{
Pawel Moll31e54082012-09-24 18:56:54 +010060 struct vexpress_regulator *reg;
61 struct regulator_init_data *init_data;
62 struct regulator_config config = { };
63
64 reg = devm_kzalloc(&pdev->dev, sizeof(*reg), GFP_KERNEL);
Pawel Moll3b9334a2014-04-30 16:46:29 +010065 if (!reg)
66 return -ENOMEM;
Pawel Moll31e54082012-09-24 18:56:54 +010067
Pawel Moll3b9334a2014-04-30 16:46:29 +010068 reg->regmap = devm_regmap_init_vexpress_config(&pdev->dev);
69 if (IS_ERR(reg->regmap))
70 return PTR_ERR(reg->regmap);
Pawel Moll31e54082012-09-24 18:56:54 +010071
72 reg->desc.name = dev_name(&pdev->dev);
73 reg->desc.type = REGULATOR_VOLTAGE;
74 reg->desc.owner = THIS_MODULE;
75 reg->desc.continuous_voltage_range = true;
76
77 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node);
Pawel Moll3b9334a2014-04-30 16:46:29 +010078 if (!init_data)
79 return -EINVAL;
Pawel Moll31e54082012-09-24 18:56:54 +010080
81 init_data->constraints.apply_uV = 0;
82 if (init_data->constraints.min_uV && init_data->constraints.max_uV)
83 reg->desc.ops = &vexpress_regulator_ops;
84 else
85 reg->desc.ops = &vexpress_regulator_ops_ro;
86
87 config.dev = &pdev->dev;
88 config.init_data = init_data;
89 config.driver_data = reg;
90 config.of_node = pdev->dev.of_node;
91
Jingoo Hanabf92a32013-09-30 09:59:31 +090092 reg->regdev = devm_regulator_register(&pdev->dev, &reg->desc, &config);
Pawel Moll3b9334a2014-04-30 16:46:29 +010093 if (IS_ERR(reg->regdev))
94 return PTR_ERR(reg->regdev);
Pawel Moll31e54082012-09-24 18:56:54 +010095
96 platform_set_drvdata(pdev, reg);
97
98 return 0;
Pawel Moll31e54082012-09-24 18:56:54 +010099}
100
Jingoo Han1439afd2014-05-07 17:09:12 +0900101static const struct of_device_id vexpress_regulator_of_match[] = {
Pawel Moll31e54082012-09-24 18:56:54 +0100102 { .compatible = "arm,vexpress-volt", },
Axel Lin9f4e45f2012-10-17 09:00:20 +0800103 { }
Pawel Moll31e54082012-09-24 18:56:54 +0100104};
105
106static struct platform_driver vexpress_regulator_driver = {
107 .probe = vexpress_regulator_probe,
Pawel Moll31e54082012-09-24 18:56:54 +0100108 .driver = {
109 .name = DRVNAME,
Pawel Moll31e54082012-09-24 18:56:54 +0100110 .of_match_table = vexpress_regulator_of_match,
111 },
112};
113
114module_platform_driver(vexpress_regulator_driver);
115
116MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
117MODULE_DESCRIPTION("Versatile Express regulator");
118MODULE_LICENSE("GPL");
119MODULE_ALIAS("platform:vexpress-regulator");