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> |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 28 | |
| 29 | struct fixed_voltage_data { |
| 30 | struct regulator_desc desc; |
| 31 | struct regulator_dev *dev; |
| 32 | int microvolts; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 33 | int gpio; |
Adrian Hunter | eda79a3 | 2010-01-12 12:25:13 +0200 | [diff] [blame] | 34 | unsigned startup_delay; |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 35 | bool enable_high; |
| 36 | bool is_enabled; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static int fixed_voltage_is_enabled(struct regulator_dev *dev) |
| 40 | { |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 41 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 42 | |
| 43 | return data->is_enabled; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static int fixed_voltage_enable(struct regulator_dev *dev) |
| 47 | { |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 48 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 49 | |
| 50 | if (gpio_is_valid(data->gpio)) { |
| 51 | gpio_set_value_cansleep(data->gpio, data->enable_high); |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 52 | data->is_enabled = true; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int fixed_voltage_disable(struct regulator_dev *dev) |
| 59 | { |
| 60 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 61 | |
| 62 | if (gpio_is_valid(data->gpio)) { |
| 63 | gpio_set_value_cansleep(data->gpio, !data->enable_high); |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 64 | data->is_enabled = false; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 65 | } |
| 66 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
Mark Brown | 17133dc | 2010-01-28 10:21:05 +0000 | [diff] [blame] | 70 | static int fixed_voltage_enable_time(struct regulator_dev *dev) |
| 71 | { |
| 72 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 73 | |
| 74 | return data->startup_delay; |
| 75 | } |
| 76 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 77 | static int fixed_voltage_get_voltage(struct regulator_dev *dev) |
| 78 | { |
| 79 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 80 | |
| 81 | return data->microvolts; |
| 82 | } |
| 83 | |
Mark Brown | 9035cef | 2009-04-28 11:13:54 +0100 | [diff] [blame] | 84 | static int fixed_voltage_list_voltage(struct regulator_dev *dev, |
| 85 | unsigned selector) |
| 86 | { |
| 87 | struct fixed_voltage_data *data = rdev_get_drvdata(dev); |
| 88 | |
| 89 | if (selector != 0) |
| 90 | return -EINVAL; |
| 91 | |
| 92 | return data->microvolts; |
| 93 | } |
| 94 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 95 | static struct regulator_ops fixed_voltage_ops = { |
| 96 | .is_enabled = fixed_voltage_is_enabled, |
| 97 | .enable = fixed_voltage_enable, |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 98 | .disable = fixed_voltage_disable, |
Mark Brown | 17133dc | 2010-01-28 10:21:05 +0000 | [diff] [blame] | 99 | .enable_time = fixed_voltage_enable_time, |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 100 | .get_voltage = fixed_voltage_get_voltage, |
Mark Brown | 9035cef | 2009-04-28 11:13:54 +0100 | [diff] [blame] | 101 | .list_voltage = fixed_voltage_list_voltage, |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 104 | static int __devinit reg_fixed_voltage_probe(struct platform_device *pdev) |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 105 | { |
| 106 | struct fixed_voltage_config *config = pdev->dev.platform_data; |
| 107 | struct fixed_voltage_data *drvdata; |
| 108 | int ret; |
| 109 | |
| 110 | drvdata = kzalloc(sizeof(struct fixed_voltage_data), GFP_KERNEL); |
| 111 | if (drvdata == NULL) { |
Mark Brown | c53ad7f | 2009-08-03 18:49:53 +0100 | [diff] [blame] | 112 | dev_err(&pdev->dev, "Failed to allocate device data\n"); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 113 | ret = -ENOMEM; |
| 114 | goto err; |
| 115 | } |
| 116 | |
| 117 | drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL); |
| 118 | if (drvdata->desc.name == NULL) { |
Mark Brown | c53ad7f | 2009-08-03 18:49:53 +0100 | [diff] [blame] | 119 | dev_err(&pdev->dev, "Failed to allocate supply name\n"); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 120 | ret = -ENOMEM; |
| 121 | goto err; |
| 122 | } |
| 123 | drvdata->desc.type = REGULATOR_VOLTAGE; |
| 124 | drvdata->desc.owner = THIS_MODULE; |
Mark Brown | 9035cef | 2009-04-28 11:13:54 +0100 | [diff] [blame] | 125 | drvdata->desc.ops = &fixed_voltage_ops; |
| 126 | drvdata->desc.n_voltages = 1; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 127 | |
| 128 | drvdata->microvolts = config->microvolts; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 129 | drvdata->gpio = config->gpio; |
Adrian Hunter | eda79a3 | 2010-01-12 12:25:13 +0200 | [diff] [blame] | 130 | drvdata->startup_delay = config->startup_delay; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 131 | |
| 132 | if (gpio_is_valid(config->gpio)) { |
| 133 | drvdata->enable_high = config->enable_high; |
| 134 | |
| 135 | /* FIXME: Remove below print warning |
| 136 | * |
| 137 | * config->gpio must be set to -EINVAL by platform code if |
| 138 | * GPIO control is not required. However, early adopters |
| 139 | * not requiring GPIO control may forget to initialize |
| 140 | * config->gpio to -EINVAL. This will cause GPIO 0 to be used |
| 141 | * for GPIO control. |
| 142 | * |
| 143 | * This warning will be removed once there are a couple of users |
| 144 | * for this driver. |
| 145 | */ |
| 146 | if (!config->gpio) |
| 147 | dev_warn(&pdev->dev, |
| 148 | "using GPIO 0 for regulator enable control\n"); |
| 149 | |
| 150 | ret = gpio_request(config->gpio, config->supply_name); |
| 151 | if (ret) { |
| 152 | dev_err(&pdev->dev, |
| 153 | "Could not obtain regulator enable GPIO %d: %d\n", |
| 154 | config->gpio, ret); |
| 155 | goto err_name; |
| 156 | } |
| 157 | |
| 158 | /* set output direction without changing state |
| 159 | * to prevent glitch |
| 160 | */ |
| 161 | drvdata->is_enabled = config->enabled_at_boot; |
| 162 | ret = drvdata->is_enabled ? |
| 163 | config->enable_high : !config->enable_high; |
| 164 | |
| 165 | ret = gpio_direction_output(config->gpio, ret); |
| 166 | if (ret) { |
| 167 | dev_err(&pdev->dev, |
| 168 | "Could not configure regulator enable GPIO %d direction: %d\n", |
| 169 | config->gpio, ret); |
| 170 | goto err_gpio; |
| 171 | } |
| 172 | |
| 173 | } else { |
| 174 | /* Regulator without GPIO control is considered |
| 175 | * always enabled |
| 176 | */ |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 177 | drvdata->is_enabled = true; |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 178 | } |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 179 | |
Mark Brown | bcf3402 | 2009-01-19 13:37:04 +0000 | [diff] [blame] | 180 | drvdata->dev = regulator_register(&drvdata->desc, &pdev->dev, |
| 181 | config->init_data, drvdata); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 182 | if (IS_ERR(drvdata->dev)) { |
| 183 | ret = PTR_ERR(drvdata->dev); |
Mark Brown | c53ad7f | 2009-08-03 18:49:53 +0100 | [diff] [blame] | 184 | dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret); |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 185 | goto err_gpio; |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | platform_set_drvdata(pdev, drvdata); |
| 189 | |
| 190 | dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name, |
| 191 | drvdata->microvolts); |
| 192 | |
| 193 | return 0; |
| 194 | |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 195 | err_gpio: |
| 196 | if (gpio_is_valid(config->gpio)) |
| 197 | gpio_free(config->gpio); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 198 | err_name: |
| 199 | kfree(drvdata->desc.name); |
| 200 | err: |
| 201 | kfree(drvdata); |
| 202 | return ret; |
| 203 | } |
| 204 | |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 205 | static int __devexit reg_fixed_voltage_remove(struct platform_device *pdev) |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 206 | { |
| 207 | struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev); |
| 208 | |
| 209 | regulator_unregister(drvdata->dev); |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 210 | if (gpio_is_valid(drvdata->gpio)) |
| 211 | gpio_free(drvdata->gpio); |
Dan Carpenter | 80099c7 | 2009-11-16 11:05:03 +0200 | [diff] [blame] | 212 | kfree(drvdata->desc.name); |
| 213 | kfree(drvdata); |
Roger Quadros | 86d9884 | 2009-08-06 19:37:29 +0300 | [diff] [blame] | 214 | |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static struct platform_driver regulator_fixed_voltage_driver = { |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 219 | .probe = reg_fixed_voltage_probe, |
| 220 | .remove = __devexit_p(reg_fixed_voltage_remove), |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 221 | .driver = { |
| 222 | .name = "reg-fixed-voltage", |
Dmitry Torokhov | 8ab3343 | 2010-02-23 23:37:55 -0800 | [diff] [blame] | 223 | .owner = THIS_MODULE, |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 224 | }, |
| 225 | }; |
| 226 | |
| 227 | static int __init regulator_fixed_voltage_init(void) |
| 228 | { |
| 229 | return platform_driver_register(®ulator_fixed_voltage_driver); |
| 230 | } |
Mark Brown | 5a1b22b | 2009-04-27 18:21:18 +0100 | [diff] [blame] | 231 | subsys_initcall(regulator_fixed_voltage_init); |
Mark Brown | 4b74ff6 | 2008-04-30 16:27:12 +0100 | [diff] [blame] | 232 | |
| 233 | static void __exit regulator_fixed_voltage_exit(void) |
| 234 | { |
| 235 | platform_driver_unregister(®ulator_fixed_voltage_driver); |
| 236 | } |
| 237 | module_exit(regulator_fixed_voltage_exit); |
| 238 | |
| 239 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 240 | MODULE_DESCRIPTION("Fixed voltage regulator"); |
| 241 | MODULE_LICENSE("GPL"); |
Mark Brown | 38c53c8 | 2009-04-28 11:13:55 +0100 | [diff] [blame] | 242 | MODULE_ALIAS("platform:reg-fixed-voltage"); |