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> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/regulator/driver.h> |
| 25 | #include <linux/regulator/fixed.h> |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 26 | #include <linux/gpio.h> |
Adrian Hunter | eda79a3 | 2010-01-12 12:25:13 +0200 | [diff] [blame] | 27 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Rajendra Nayak | 1dc5240 | 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; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 38 | int gpio; |
Adrian Hunter | eda79a3 | 2010-01-12 12:25:13 +0200 | [diff] [blame] | 39 | unsigned startup_delay; |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 40 | bool enable_high; |
| 41 | bool is_enabled; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Rajendra Nayak | 1dc5240 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * of_get_fixed_voltage_config - extract fixed_voltage_config structure info |
| 47 | * @dev: device requesting for fixed_voltage_config |
| 48 | * |
| 49 | * Populates fixed_voltage_config structure by extracting data from device |
| 50 | * tree node, returns a pointer to the populated structure of NULL if memory |
| 51 | * alloc fails. |
| 52 | */ |
| 53 | struct fixed_voltage_config *of_get_fixed_voltage_config(struct device *dev) |
| 54 | { |
| 55 | struct fixed_voltage_config *config; |
| 56 | struct device_node *np = dev->of_node; |
| 57 | const __be32 *delay; |
| 58 | struct regulator_init_data *init_data; |
| 59 | |
| 60 | config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config), |
| 61 | GFP_KERNEL); |
| 62 | if (!config) |
| 63 | return NULL; |
| 64 | |
| 65 | config->init_data = of_get_regulator_init_data(dev); |
| 66 | init_data = config->init_data; |
| 67 | |
| 68 | config->supply_name = init_data->constraints.name; |
| 69 | if (init_data->constraints.min_uV == init_data->constraints.max_uV) { |
| 70 | config->microvolts = init_data->constraints.min_uV; |
| 71 | } else { |
| 72 | dev_err(dev, |
| 73 | "Fixed regulator specified with variable voltages\n"); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | if (init_data->constraints.boot_on) |
| 78 | config->enabled_at_boot = true; |
| 79 | |
| 80 | config->gpio = of_get_named_gpio(np, "gpio", 0); |
| 81 | delay = of_get_property(np, "startup-delay-us", NULL); |
| 82 | if (delay) |
| 83 | config->startup_delay = be32_to_cpu(*delay); |
| 84 | |
| 85 | if (of_find_property(np, "enable-active-high", NULL)) |
| 86 | config->enable_high = true; |
| 87 | |
| 88 | return config; |
| 89 | } |
| 90 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 91 | static int fixed_voltage_is_enabled(struct regulator_dev *dev) |
| 92 | { |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 93 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 94 | |
| 95 | return data->is_enabled; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int fixed_voltage_enable(struct regulator_dev *dev) |
| 99 | { |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 100 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 101 | |
| 102 | if (gpio_is_valid(data->gpio)) { |
| 103 | gpio_set_value_cansleep(data->gpio, data->enable_high); |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 104 | data->is_enabled = true; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int fixed_voltage_disable(struct regulator_dev *dev) |
| 111 | { |
| 112 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 113 | |
| 114 | if (gpio_is_valid(data->gpio)) { |
| 115 | gpio_set_value_cansleep(data->gpio, !data->enable_high); |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 116 | data->is_enabled = false; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 117 | } |
| 118 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Mark Brown | 17133dc | 2010-01-28 10:21:05 +0000 | [diff] [blame] | 122 | static int fixed_voltage_enable_time(struct regulator_dev *dev) |
| 123 | { |
| 124 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 125 | |
| 126 | return data->startup_delay; |
| 127 | } |
| 128 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 129 | static int fixed_voltage_get_voltage(struct regulator_dev *dev) |
| 130 | { |
| 131 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 132 | |
| 133 | return data->microvolts; |
| 134 | } |
| 135 | |
Mark Brown | 9035cef | 2009-04-28 11:13:54 +0100 | [diff] [blame] | 136 | static int fixed_voltage_list_voltage(struct regulator_dev *dev, |
| 137 | unsigned selector) |
| 138 | { |
| 139 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 140 | |
| 141 | if (selector != 0) |
| 142 | return -EINVAL; |
| 143 | |
| 144 | return data->microvolts; |
| 145 | } |
| 146 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 147 | static struct regulator_ops fixed_voltage_ops = { |
| 148 | .is_enabled = fixed_voltage_is_enabled, |
| 149 | .enable = fixed_voltage_enable, |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 150 | .disable = fixed_voltage_disable, |
Mark Brown | 17133dc | 2010-01-28 10:21:05 +0000 | [diff] [blame] | 151 | .enable_time = fixed_voltage_enable_time, |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 152 | .get_voltage = fixed_voltage_get_voltage, |
Mark Brown | 9035cef | 2009-04-28 11:13:54 +0100 | [diff] [blame] | 153 | .list_voltage = fixed_voltage_list_voltage, |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 154 | }; |
| 155 | |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 156 | static int __devinit reg_fixed_voltage_probe(struct platform_device *pdev) |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 157 | { |
| 158 | struct fixed_voltage_config *config = pdev->dev.platform_data; |
| 159 | struct fixed_voltage_data *drvdata; |
| 160 | int ret; |
| 161 | |
Rajendra Nayak | 1dc5240 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 162 | if (pdev->dev.of_node) |
| 163 | config = of_get_fixed_voltage_config(&pdev->dev); |
| 164 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 165 | drvdata = kzalloc(sizeof(struct fixed_voltage_data), GFP_KERNEL); |
| 166 | if (drvdata == NULL) { |
Mark Brown | c53ad7f | 2009-08-03 18:49:53 +0100 | [diff] [blame] | 167 | dev_err(&pdev->dev, "Failed to allocate device data\n"); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 168 | ret = -ENOMEM; |
| 169 | goto err; |
| 170 | } |
| 171 | |
| 172 | drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL); |
| 173 | if (drvdata->desc.name == NULL) { |
Mark Brown | c53ad7f | 2009-08-03 18:49:53 +0100 | [diff] [blame] | 174 | dev_err(&pdev->dev, "Failed to allocate supply name\n"); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 175 | ret = -ENOMEM; |
| 176 | goto err; |
| 177 | } |
| 178 | drvdata->desc.type = REGULATOR_VOLTAGE; |
| 179 | drvdata->desc.owner = THIS_MODULE; |
Mark Brown | 9035cef | 2009-04-28 11:13:54 +0100 | [diff] [blame] | 180 | drvdata->desc.ops = &fixed_voltage_ops; |
| 181 | drvdata->desc.n_voltages = 1; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 182 | |
| 183 | drvdata->microvolts = config->microvolts; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 184 | drvdata->gpio = config->gpio; |
Adrian Hunter | eda79a3 | 2010-01-12 12:25:13 +0200 | [diff] [blame] | 185 | drvdata->startup_delay = config->startup_delay; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 186 | |
| 187 | if (gpio_is_valid(config->gpio)) { |
| 188 | drvdata->enable_high = config->enable_high; |
| 189 | |
| 190 | /* FIXME: Remove below print warning |
| 191 | * |
| 192 | * config->gpio must be set to -EINVAL by platform code if |
| 193 | * GPIO control is not required. However, early adopters |
| 194 | * not requiring GPIO control may forget to initialize |
| 195 | * config->gpio to -EINVAL. This will cause GPIO 0 to be used |
| 196 | * for GPIO control. |
| 197 | * |
| 198 | * This warning will be removed once there are a couple of users |
| 199 | * for this driver. |
| 200 | */ |
| 201 | if (!config->gpio) |
| 202 | dev_warn(&pdev->dev, |
| 203 | "using GPIO 0 for regulator enable control\n"); |
| 204 | |
| 205 | ret = gpio_request(config->gpio, config->supply_name); |
| 206 | if (ret) { |
| 207 | dev_err(&pdev->dev, |
| 208 | "Could not obtain regulator enable GPIO %d: %d\n", |
| 209 | config->gpio, ret); |
| 210 | goto err_name; |
| 211 | } |
| 212 | |
| 213 | /* set output direction without changing state |
| 214 | * to prevent glitch |
| 215 | */ |
| 216 | drvdata->is_enabled = config->enabled_at_boot; |
| 217 | ret = drvdata->is_enabled ? |
| 218 | config->enable_high : !config->enable_high; |
| 219 | |
| 220 | ret = gpio_direction_output(config->gpio, ret); |
| 221 | if (ret) { |
| 222 | dev_err(&pdev->dev, |
| 223 | "Could not configure regulator enable GPIO %d direction: %d\n", |
| 224 | config->gpio, ret); |
| 225 | goto err_gpio; |
| 226 | } |
| 227 | |
| 228 | } else { |
| 229 | /* Regulator without GPIO control is considered |
| 230 | * always enabled |
| 231 | */ |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 232 | drvdata->is_enabled = true; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 233 | } |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 234 | |
Mark Brown | bcf3402 | 2009-01-19 13:37:04 +0000 | [diff] [blame] | 235 | drvdata->dev = regulator_register(&drvdata->desc, &pdev->dev, |
Rajendra Nayak | 11eafc6 | 2011-11-18 16:47:19 +0530 | [diff] [blame] | 236 | config->init_data, drvdata, NULL); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 237 | if (IS_ERR(drvdata->dev)) { |
| 238 | ret = PTR_ERR(drvdata->dev); |
Mark Brown | c53ad7f | 2009-08-03 18:49:53 +0100 | [diff] [blame] | 239 | dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret); |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 240 | goto err_gpio; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | platform_set_drvdata(pdev, drvdata); |
| 244 | |
| 245 | dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name, |
| 246 | drvdata->microvolts); |
| 247 | |
| 248 | return 0; |
| 249 | |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 250 | err_gpio: |
| 251 | if (gpio_is_valid(config->gpio)) |
| 252 | gpio_free(config->gpio); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 253 | err_name: |
| 254 | kfree(drvdata->desc.name); |
| 255 | err: |
| 256 | kfree(drvdata); |
| 257 | return ret; |
| 258 | } |
| 259 | |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 260 | static int __devexit reg_fixed_voltage_remove(struct platform_device *pdev) |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 261 | { |
| 262 | struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev); |
| 263 | |
| 264 | regulator_unregister(drvdata->dev); |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 265 | if (gpio_is_valid(drvdata->gpio)) |
| 266 | gpio_free(drvdata->gpio); |
Dan Carpenter | 80099c7 | 2009-11-16 11:05:03 +0200 | [diff] [blame] | 267 | kfree(drvdata->desc.name); |
| 268 | kfree(drvdata); |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 269 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Rajendra Nayak | 1dc5240 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 273 | #if defined(CONFIG_OF) |
| 274 | static const struct of_device_id fixed_of_match[] __devinitconst = { |
| 275 | { .compatible = "regulator-fixed", }, |
| 276 | {}, |
| 277 | }; |
| 278 | MODULE_DEVICE_TABLE(of, fixed_of_match); |
| 279 | #else |
| 280 | #define fixed_of_match NULL |
| 281 | #endif |
| 282 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 283 | static struct platform_driver regulator_fixed_voltage_driver = { |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 284 | .probe = reg_fixed_voltage_probe, |
| 285 | .remove = __devexit_p(reg_fixed_voltage_remove), |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 286 | .driver = { |
| 287 | .name = "reg-fixed-voltage", |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 288 | .owner = THIS_MODULE, |
Rajendra Nayak | 1dc5240 | 2011-11-18 16:47:18 +0530 | [diff] [blame] | 289 | .of_match_table = fixed_of_match, |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 290 | }, |
| 291 | }; |
| 292 | |
| 293 | static int __init regulator_fixed_voltage_init(void) |
| 294 | { |
| 295 | return platform_driver_register(®ulator_fixed_voltage_driver); |
| 296 | } |
Mark Brown | 5a1b22b | 2009-04-27 18:21:18 +0100 | [diff] [blame] | 297 | subsys_initcall(regulator_fixed_voltage_init); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 298 | |
| 299 | static void __exit regulator_fixed_voltage_exit(void) |
| 300 | { |
| 301 | platform_driver_unregister(®ulator_fixed_voltage_driver); |
| 302 | } |
| 303 | module_exit(regulator_fixed_voltage_exit); |
| 304 | |
| 305 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 306 | MODULE_DESCRIPTION("Fixed voltage regulator"); |
| 307 | MODULE_LICENSE("GPL"); |
Mark Brown | 38c53c8 | 2009-04-28 11:13:55 +0100 | [diff] [blame] | 308 | MODULE_ALIAS("platform:reg-fixed-voltage"); |