Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * fixed.c |
| 3 | * |
| 4 | * Copyright 2008 Wolfson Microelectronics PLC. |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 8 | * Copyright (c) 2009 Nokia Corporation |
| 9 | * Roger Quadros <ext-roger.quadros@nokia.com> |
| 10 | * |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of the |
| 14 | * License, or (at your option) any later version. |
| 15 | * |
| 16 | * This is useful for systems with mixed controllable and |
| 17 | * non-controllable regulators, as well as for allowing testing on |
| 18 | * systems with no controllable regulators. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/mutex.h> |
Paul Gortmaker | 65602c3 | 2011-07-17 16:28:23 -0400 | [diff] [blame] | 23 | #include <linux/module.h> |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/regulator/driver.h> |
| 26 | #include <linux/regulator/fixed.h> |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 27 | #include <linux/gpio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 29 | #include <linux/of.h> |
| 30 | #include <linux/of_gpio.h> |
| 31 | #include <linux/regulator/of_regulator.h> |
| 32 | #include <linux/regulator/machine.h> |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 33 | |
| 34 | struct fixed_voltage_data { |
| 35 | struct regulator_desc desc; |
| 36 | struct regulator_dev *dev; |
| 37 | int microvolts; |
| 38 | }; |
| 39 | |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * of_get_fixed_voltage_config - extract fixed_voltage_config structure info |
| 43 | * @dev: device requesting for fixed_voltage_config |
| 44 | * |
| 45 | * Populates fixed_voltage_config structure by extracting data from device |
| 46 | * tree node, returns a pointer to the populated structure of NULL if memory |
| 47 | * alloc fails. |
| 48 | */ |
Axel Lin | bc91396 | 2011-11-27 10:35:08 +0800 | [diff] [blame] | 49 | static struct fixed_voltage_config * |
| 50 | of_get_fixed_voltage_config(struct device *dev) |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 51 | { |
| 52 | struct fixed_voltage_config *config; |
| 53 | struct device_node *np = dev->of_node; |
| 54 | const __be32 *delay; |
| 55 | struct regulator_init_data *init_data; |
| 56 | |
| 57 | config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config), |
| 58 | GFP_KERNEL); |
| 59 | if (!config) |
Stephen Warren | f141822 | 2012-06-29 10:33:15 -0600 | [diff] [blame] | 60 | return ERR_PTR(-ENOMEM); |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 61 | |
Shawn Guo | d9a861c | 2011-12-01 17:21:06 +0800 | [diff] [blame] | 62 | config->init_data = of_get_regulator_init_data(dev, dev->of_node); |
Axel Lin | 4b864af | 2011-11-26 20:19:19 +0800 | [diff] [blame] | 63 | if (!config->init_data) |
Stephen Warren | f141822 | 2012-06-29 10:33:15 -0600 | [diff] [blame] | 64 | return ERR_PTR(-EINVAL); |
Axel Lin | 4b864af | 2011-11-26 20:19:19 +0800 | [diff] [blame] | 65 | |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 66 | init_data = config->init_data; |
Richard Zhao | 0c437c4 | 2012-01-04 11:07:29 +0800 | [diff] [blame] | 67 | init_data->constraints.apply_uV = 0; |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 68 | |
| 69 | config->supply_name = init_data->constraints.name; |
| 70 | if (init_data->constraints.min_uV == init_data->constraints.max_uV) { |
| 71 | config->microvolts = init_data->constraints.min_uV; |
| 72 | } else { |
| 73 | dev_err(dev, |
| 74 | "Fixed regulator specified with variable voltages\n"); |
Stephen Warren | f141822 | 2012-06-29 10:33:15 -0600 | [diff] [blame] | 75 | return ERR_PTR(-EINVAL); |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if (init_data->constraints.boot_on) |
| 79 | config->enabled_at_boot = true; |
| 80 | |
| 81 | config->gpio = of_get_named_gpio(np, "gpio", 0); |
Stephen Warren | f141822 | 2012-06-29 10:33:15 -0600 | [diff] [blame] | 82 | /* |
| 83 | * of_get_named_gpio() currently returns ENODEV rather than |
| 84 | * EPROBE_DEFER. This code attempts to be compatible with both |
| 85 | * for now; the ENODEV check can be removed once the API is fixed. |
| 86 | * of_get_named_gpio() doesn't differentiate between a missing |
| 87 | * property (which would be fine here, since the GPIO is optional) |
| 88 | * and some other error. Patches have been posted for both issues. |
| 89 | * Once they are check in, we should replace this with: |
| 90 | * if (config->gpio < 0 && config->gpio != -ENOENT) |
| 91 | */ |
| 92 | if ((config->gpio == -ENODEV) || (config->gpio == -EPROBE_DEFER)) |
| 93 | return ERR_PTR(-EPROBE_DEFER); |
| 94 | |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 95 | delay = of_get_property(np, "startup-delay-us", NULL); |
| 96 | if (delay) |
| 97 | config->startup_delay = be32_to_cpu(*delay); |
| 98 | |
| 99 | if (of_find_property(np, "enable-active-high", NULL)) |
| 100 | config->enable_high = true; |
| 101 | |
Laxman Dewangan | 9a50dba | 2012-05-07 15:58:19 +0530 | [diff] [blame] | 102 | if (of_find_property(np, "gpio-open-drain", NULL)) |
| 103 | config->gpio_is_open_drain = true; |
| 104 | |
Laxman Dewangan | 6be5bfc | 2012-07-05 18:12:01 +0530 | [diff] [blame] | 105 | if (of_find_property(np, "vin-supply", NULL)) |
| 106 | config->input_supply = "vin"; |
| 107 | |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 108 | return config; |
| 109 | } |
| 110 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 111 | static int fixed_voltage_get_voltage(struct regulator_dev *dev) |
| 112 | { |
| 113 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 114 | |
Mark Brown | aebe495 | 2011-11-02 11:38:45 +0000 | [diff] [blame] | 115 | if (data->microvolts) |
| 116 | return data->microvolts; |
| 117 | else |
| 118 | return -EINVAL; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Mark Brown | 9035cef | 2009-04-28 11:13:54 +0100 | [diff] [blame] | 121 | static int fixed_voltage_list_voltage(struct regulator_dev *dev, |
| 122 | unsigned selector) |
| 123 | { |
| 124 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 125 | |
| 126 | if (selector != 0) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | return data->microvolts; |
| 130 | } |
| 131 | |
Mark Brown | 9d44206 | 2012-03-21 20:04:44 +0000 | [diff] [blame] | 132 | static struct regulator_ops fixed_voltage_ops = { |
| 133 | .get_voltage = fixed_voltage_get_voltage, |
| 134 | .list_voltage = fixed_voltage_list_voltage, |
| 135 | }; |
| 136 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 137 | static int reg_fixed_voltage_probe(struct platform_device *pdev) |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 138 | { |
Axel Lin | 22d881c | 2011-11-27 20:07:57 +0800 | [diff] [blame] | 139 | struct fixed_voltage_config *config; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 140 | struct fixed_voltage_data *drvdata; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 141 | struct regulator_config cfg = { }; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 142 | int ret; |
| 143 | |
Stephen Warren | f141822 | 2012-06-29 10:33:15 -0600 | [diff] [blame] | 144 | if (pdev->dev.of_node) { |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 145 | config = of_get_fixed_voltage_config(&pdev->dev); |
Stephen Warren | f141822 | 2012-06-29 10:33:15 -0600 | [diff] [blame] | 146 | if (IS_ERR(config)) |
| 147 | return PTR_ERR(config); |
| 148 | } else { |
Axel Lin | 22d881c | 2011-11-27 20:07:57 +0800 | [diff] [blame] | 149 | config = pdev->dev.platform_data; |
Stephen Warren | f141822 | 2012-06-29 10:33:15 -0600 | [diff] [blame] | 150 | } |
Axel Lin | 22d881c | 2011-11-27 20:07:57 +0800 | [diff] [blame] | 151 | |
| 152 | if (!config) |
| 153 | return -ENOMEM; |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 154 | |
Mark Brown | c45bb35 | 2012-03-21 18:13:29 +0000 | [diff] [blame] | 155 | drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data), |
| 156 | GFP_KERNEL); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 157 | if (drvdata == NULL) { |
Mark Brown | c53ad7f | 2009-08-03 18:49:53 +0100 | [diff] [blame] | 158 | dev_err(&pdev->dev, "Failed to allocate device data\n"); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 159 | ret = -ENOMEM; |
| 160 | goto err; |
| 161 | } |
| 162 | |
| 163 | drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL); |
| 164 | if (drvdata->desc.name == NULL) { |
Mark Brown | c53ad7f | 2009-08-03 18:49:53 +0100 | [diff] [blame] | 165 | dev_err(&pdev->dev, "Failed to allocate supply name\n"); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 166 | ret = -ENOMEM; |
| 167 | goto err; |
| 168 | } |
| 169 | drvdata->desc.type = REGULATOR_VOLTAGE; |
| 170 | drvdata->desc.owner = THIS_MODULE; |
Mark Brown | 25a53df | 2012-06-27 14:30:33 +0100 | [diff] [blame] | 171 | drvdata->desc.ops = &fixed_voltage_ops; |
Sascha Hauer | 1c37f8a | 2012-03-03 12:40:01 +0100 | [diff] [blame] | 172 | |
Mark Brown | 3d0f267 | 2012-06-27 14:27:22 +0100 | [diff] [blame] | 173 | drvdata->desc.enable_time = config->startup_delay; |
| 174 | |
Laxman Dewangan | 6be5bfc | 2012-07-05 18:12:01 +0530 | [diff] [blame] | 175 | if (config->input_supply) { |
| 176 | drvdata->desc.supply_name = kstrdup(config->input_supply, |
| 177 | GFP_KERNEL); |
| 178 | if (!drvdata->desc.supply_name) { |
| 179 | dev_err(&pdev->dev, |
| 180 | "Failed to allocate input supply\n"); |
| 181 | ret = -ENOMEM; |
| 182 | goto err_name; |
| 183 | } |
| 184 | } |
| 185 | |
Sascha Hauer | 1c37f8a | 2012-03-03 12:40:01 +0100 | [diff] [blame] | 186 | if (config->microvolts) |
| 187 | drvdata->desc.n_voltages = 1; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 188 | |
| 189 | drvdata->microvolts = config->microvolts; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 190 | |
Mark Brown | 25a53df | 2012-06-27 14:30:33 +0100 | [diff] [blame] | 191 | if (config->gpio >= 0) |
| 192 | cfg.ena_gpio = config->gpio; |
| 193 | cfg.ena_gpio_invert = !config->enable_high; |
| 194 | if (config->enabled_at_boot) { |
| 195 | if (config->enable_high) { |
| 196 | cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH; |
| 197 | } else { |
| 198 | cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 199 | } |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 200 | } else { |
Mark Brown | 25a53df | 2012-06-27 14:30:33 +0100 | [diff] [blame] | 201 | if (config->enable_high) { |
| 202 | cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW; |
| 203 | } else { |
| 204 | cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH; |
| 205 | } |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 206 | } |
Mark Brown | 25a53df | 2012-06-27 14:30:33 +0100 | [diff] [blame] | 207 | if (config->gpio_is_open_drain) |
| 208 | cfg.ena_gpio_flags |= GPIOF_OPEN_DRAIN; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 209 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 210 | cfg.dev = &pdev->dev; |
| 211 | cfg.init_data = config->init_data; |
| 212 | cfg.driver_data = drvdata; |
| 213 | cfg.of_node = pdev->dev.of_node; |
| 214 | |
| 215 | drvdata->dev = regulator_register(&drvdata->desc, &cfg); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 216 | if (IS_ERR(drvdata->dev)) { |
| 217 | ret = PTR_ERR(drvdata->dev); |
Mark Brown | c53ad7f | 2009-08-03 18:49:53 +0100 | [diff] [blame] | 218 | dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret); |
Laxman Dewangan | 6be5bfc | 2012-07-05 18:12:01 +0530 | [diff] [blame] | 219 | goto err_input; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | platform_set_drvdata(pdev, drvdata); |
| 223 | |
| 224 | dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name, |
| 225 | drvdata->microvolts); |
| 226 | |
| 227 | return 0; |
| 228 | |
Laxman Dewangan | 6be5bfc | 2012-07-05 18:12:01 +0530 | [diff] [blame] | 229 | err_input: |
| 230 | kfree(drvdata->desc.supply_name); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 231 | err_name: |
| 232 | kfree(drvdata->desc.name); |
| 233 | err: |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 237 | static int reg_fixed_voltage_remove(struct platform_device *pdev) |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 238 | { |
| 239 | struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev); |
| 240 | |
| 241 | regulator_unregister(drvdata->dev); |
Laxman Dewangan | 6be5bfc | 2012-07-05 18:12:01 +0530 | [diff] [blame] | 242 | kfree(drvdata->desc.supply_name); |
Dan Carpenter | 80099c7 | 2009-11-16 11:05:03 +0200 | [diff] [blame] | 243 | kfree(drvdata->desc.name); |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 244 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 248 | #if defined(CONFIG_OF) |
| 249 | static const struct of_device_id fixed_of_match[] __devinitconst = { |
| 250 | { .compatible = "regulator-fixed", }, |
| 251 | {}, |
| 252 | }; |
| 253 | MODULE_DEVICE_TABLE(of, fixed_of_match); |
Rajendra Nayak | cef4910 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 254 | #endif |
| 255 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 256 | static struct platform_driver regulator_fixed_voltage_driver = { |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 257 | .probe = reg_fixed_voltage_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 258 | .remove = reg_fixed_voltage_remove, |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 259 | .driver = { |
| 260 | .name = "reg-fixed-voltage", |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 261 | .owner = THIS_MODULE, |
Axel Lin | abcfaf2 | 2012-06-01 12:16:25 +0800 | [diff] [blame] | 262 | .of_match_table = of_match_ptr(fixed_of_match), |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 263 | }, |
| 264 | }; |
| 265 | |
| 266 | static int __init regulator_fixed_voltage_init(void) |
| 267 | { |
| 268 | return platform_driver_register(®ulator_fixed_voltage_driver); |
| 269 | } |
Mark Brown | 5a1b22b | 2009-04-27 18:21:18 +0100 | [diff] [blame] | 270 | subsys_initcall(regulator_fixed_voltage_init); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 271 | |
| 272 | static void __exit regulator_fixed_voltage_exit(void) |
| 273 | { |
| 274 | platform_driver_unregister(®ulator_fixed_voltage_driver); |
| 275 | } |
| 276 | module_exit(regulator_fixed_voltage_exit); |
| 277 | |
| 278 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 279 | MODULE_DESCRIPTION("Fixed voltage regulator"); |
| 280 | MODULE_LICENSE("GPL"); |
Mark Brown | 38c53c8 | 2009-04-28 11:13:55 +0100 | [diff] [blame] | 281 | MODULE_ALIAS("platform:reg-fixed-voltage"); |