Commit | Line | Data |
---|---|---|
c5e911ad AL |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | // | |
3 | // Copyright (C) 2012 ARM Limited | |
31e54086 PM |
4 | |
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> | |
045a44d4 RH |
11 | #include <linux/mod_devicetable.h> |
12 | #include <linux/platform_device.h> | |
31e54086 PM |
13 | #include <linux/regulator/driver.h> |
14 | #include <linux/regulator/machine.h> | |
15 | #include <linux/regulator/of_regulator.h> | |
16 | #include <linux/vexpress.h> | |
17 | ||
31e54086 PM |
18 | static int vexpress_regulator_get_voltage(struct regulator_dev *regdev) |
19 | { | |
eeb1b235 AL |
20 | unsigned int uV; |
21 | int err = regmap_read(regdev->regmap, 0, &uV); | |
31e54086 PM |
22 | |
23 | return err ? err : uV; | |
24 | } | |
25 | ||
26 | static int vexpress_regulator_set_voltage(struct regulator_dev *regdev, | |
27 | int min_uV, int max_uV, unsigned *selector) | |
28 | { | |
eeb1b235 | 29 | return regmap_write(regdev->regmap, 0, min_uV); |
31e54086 PM |
30 | } |
31 | ||
ab54a4d7 | 32 | static const struct regulator_ops vexpress_regulator_ops_ro = { |
31e54086 PM |
33 | .get_voltage = vexpress_regulator_get_voltage, |
34 | }; | |
35 | ||
ab54a4d7 | 36 | static const struct regulator_ops vexpress_regulator_ops = { |
31e54086 PM |
37 | .get_voltage = vexpress_regulator_get_voltage, |
38 | .set_voltage = vexpress_regulator_set_voltage, | |
39 | }; | |
40 | ||
41 | static int vexpress_regulator_probe(struct platform_device *pdev) | |
42 | { | |
eeb1b235 | 43 | struct regulator_desc *desc; |
31e54086 PM |
44 | struct regulator_init_data *init_data; |
45 | struct regulator_config config = { }; | |
eeb1b235 AL |
46 | struct regulator_dev *rdev; |
47 | struct regmap *regmap; | |
31e54086 | 48 | |
eeb1b235 AL |
49 | desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL); |
50 | if (!desc) | |
3b9334ac | 51 | return -ENOMEM; |
31e54086 | 52 | |
eeb1b235 AL |
53 | regmap = devm_regmap_init_vexpress_config(&pdev->dev); |
54 | if (IS_ERR(regmap)) | |
55 | return PTR_ERR(regmap); | |
31e54086 | 56 | |
eeb1b235 AL |
57 | desc->name = dev_name(&pdev->dev); |
58 | desc->type = REGULATOR_VOLTAGE; | |
59 | desc->owner = THIS_MODULE; | |
60 | desc->continuous_voltage_range = true; | |
31e54086 | 61 | |
072e78b1 | 62 | init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node, |
eeb1b235 | 63 | desc); |
3b9334ac PM |
64 | if (!init_data) |
65 | return -EINVAL; | |
31e54086 PM |
66 | |
67 | init_data->constraints.apply_uV = 0; | |
68 | if (init_data->constraints.min_uV && init_data->constraints.max_uV) | |
eeb1b235 | 69 | desc->ops = &vexpress_regulator_ops; |
31e54086 | 70 | else |
eeb1b235 | 71 | desc->ops = &vexpress_regulator_ops_ro; |
31e54086 | 72 | |
eeb1b235 | 73 | config.regmap = regmap; |
31e54086 PM |
74 | config.dev = &pdev->dev; |
75 | config.init_data = init_data; | |
31e54086 PM |
76 | config.of_node = pdev->dev.of_node; |
77 | ||
eeb1b235 | 78 | rdev = devm_regulator_register(&pdev->dev, desc, &config); |
1d7c4c11 | 79 | return PTR_ERR_OR_ZERO(rdev); |
31e54086 PM |
80 | } |
81 | ||
1439afd8 | 82 | static const struct of_device_id vexpress_regulator_of_match[] = { |
31e54086 | 83 | { .compatible = "arm,vexpress-volt", }, |
9f4e45f7 | 84 | { } |
31e54086 | 85 | }; |
7209fee8 | 86 | MODULE_DEVICE_TABLE(of, vexpress_regulator_of_match); |
31e54086 PM |
87 | |
88 | static struct platform_driver vexpress_regulator_driver = { | |
89 | .probe = vexpress_regulator_probe, | |
31e54086 PM |
90 | .driver = { |
91 | .name = DRVNAME, | |
259b93b2 | 92 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
31e54086 PM |
93 | .of_match_table = vexpress_regulator_of_match, |
94 | }, | |
95 | }; | |
96 | ||
97 | module_platform_driver(vexpress_regulator_driver); | |
98 | ||
99 | MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>"); | |
100 | MODULE_DESCRIPTION("Versatile Express regulator"); | |
101 | MODULE_LICENSE("GPL"); | |
102 | MODULE_ALIAS("platform:vexpress-regulator"); |