regulator: More explict error reporting for fixed regulator
[cascardo/linux.git] / drivers / regulator / fixed.c
1 /*
2  * fixed.c
3  *
4  * Copyright 2008 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This is useful for systems with mixed controllable and
14  * non-controllable regulators, as well as for allowing testing on
15  * systems with no controllable regulators.
16  */
17
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/fixed.h>
23
24 struct fixed_voltage_data {
25         struct regulator_desc desc;
26         struct regulator_dev *dev;
27         int microvolts;
28 };
29
30 static int fixed_voltage_is_enabled(struct regulator_dev *dev)
31 {
32         return 1;
33 }
34
35 static int fixed_voltage_enable(struct regulator_dev *dev)
36 {
37         return 0;
38 }
39
40 static int fixed_voltage_get_voltage(struct regulator_dev *dev)
41 {
42         struct fixed_voltage_data *data = rdev_get_drvdata(dev);
43
44         return data->microvolts;
45 }
46
47 static int fixed_voltage_list_voltage(struct regulator_dev *dev,
48                                       unsigned selector)
49 {
50         struct fixed_voltage_data *data = rdev_get_drvdata(dev);
51
52         if (selector != 0)
53                 return -EINVAL;
54
55         return data->microvolts;
56 }
57
58 static struct regulator_ops fixed_voltage_ops = {
59         .is_enabled = fixed_voltage_is_enabled,
60         .enable = fixed_voltage_enable,
61         .get_voltage = fixed_voltage_get_voltage,
62         .list_voltage = fixed_voltage_list_voltage,
63 };
64
65 static int regulator_fixed_voltage_probe(struct platform_device *pdev)
66 {
67         struct fixed_voltage_config *config = pdev->dev.platform_data;
68         struct fixed_voltage_data *drvdata;
69         int ret;
70
71         drvdata = kzalloc(sizeof(struct fixed_voltage_data), GFP_KERNEL);
72         if (drvdata == NULL) {
73                 dev_err(&pdev->dev, "Failed to allocate device data\n");
74                 ret = -ENOMEM;
75                 goto err;
76         }
77
78         drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
79         if (drvdata->desc.name == NULL) {
80                 dev_err(&pdev->dev, "Failed to allocate supply name\n");
81                 ret = -ENOMEM;
82                 goto err;
83         }
84         drvdata->desc.type = REGULATOR_VOLTAGE;
85         drvdata->desc.owner = THIS_MODULE;
86         drvdata->desc.ops = &fixed_voltage_ops;
87         drvdata->desc.n_voltages = 1;
88
89         drvdata->microvolts = config->microvolts;
90
91         drvdata->dev = regulator_register(&drvdata->desc, &pdev->dev,
92                                           config->init_data, drvdata);
93         if (IS_ERR(drvdata->dev)) {
94                 ret = PTR_ERR(drvdata->dev);
95                 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
96                 goto err_name;
97         }
98
99         platform_set_drvdata(pdev, drvdata);
100
101         dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
102                 drvdata->microvolts);
103
104         return 0;
105
106 err_name:
107         kfree(drvdata->desc.name);
108 err:
109         kfree(drvdata);
110         return ret;
111 }
112
113 static int regulator_fixed_voltage_remove(struct platform_device *pdev)
114 {
115         struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
116
117         regulator_unregister(drvdata->dev);
118         kfree(drvdata->desc.name);
119         kfree(drvdata);
120
121         return 0;
122 }
123
124 static struct platform_driver regulator_fixed_voltage_driver = {
125         .probe          = regulator_fixed_voltage_probe,
126         .remove         = regulator_fixed_voltage_remove,
127         .driver         = {
128                 .name           = "reg-fixed-voltage",
129         },
130 };
131
132 static int __init regulator_fixed_voltage_init(void)
133 {
134         return platform_driver_register(&regulator_fixed_voltage_driver);
135 }
136 subsys_initcall(regulator_fixed_voltage_init);
137
138 static void __exit regulator_fixed_voltage_exit(void)
139 {
140         platform_driver_unregister(&regulator_fixed_voltage_driver);
141 }
142 module_exit(regulator_fixed_voltage_exit);
143
144 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
145 MODULE_DESCRIPTION("Fixed voltage regulator");
146 MODULE_LICENSE("GPL");
147 MODULE_ALIAS("platform:reg-fixed-voltage");