diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
| commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
| tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/regulator/gpio-switch-regulator.c | |
| parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) | |
Diffstat (limited to 'drivers/regulator/gpio-switch-regulator.c')
| -rw-r--r-- | drivers/regulator/gpio-switch-regulator.c | 412 |
1 files changed, 412 insertions, 0 deletions
diff --git a/drivers/regulator/gpio-switch-regulator.c b/drivers/regulator/gpio-switch-regulator.c new file mode 100644 index 00000000000..55dd63675a0 --- /dev/null +++ b/drivers/regulator/gpio-switch-regulator.c | |||
| @@ -0,0 +1,412 @@ | |||
| 1 | /* | ||
| 2 | * driver/regulator/gpio-switch-regulator.c | ||
| 3 | * GPIO based switch regulator to enable/disable power rails. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2011, NVIDIA Corporation. | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License as published by | ||
| 9 | * the Free Software Foundation; either version 2 of the License, or | ||
| 10 | * (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 15 | * more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License along | ||
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 20 | */ | ||
| 21 | |||
| 22 | /*#define DEBUG 1*/ | ||
| 23 | /*#define VERBOSE_DEBUG 1*/ | ||
| 24 | |||
| 25 | #include <linux/kernel.h> | ||
| 26 | #include <linux/init.h> | ||
| 27 | #include <linux/err.h> | ||
| 28 | #include <linux/slab.h> | ||
| 29 | #include <linux/platform_device.h> | ||
| 30 | #include <linux/regulator/driver.h> | ||
| 31 | #include <linux/regulator/machine.h> | ||
| 32 | #include <linux/gpio.h> | ||
| 33 | #include <linux/regulator/gpio-switch-regulator.h> | ||
| 34 | |||
| 35 | struct gpio_switch_regulator { | ||
| 36 | struct regulator_desc reg_desc; | ||
| 37 | struct regulator_init_data reg_init_data; | ||
| 38 | struct regulator *input_regulator; | ||
| 39 | struct regulator_dev *rdev; | ||
| 40 | struct device *dev; | ||
| 41 | int gpio_nr; | ||
| 42 | int pin_group; | ||
| 43 | bool is_gpio_init; | ||
| 44 | bool is_enable; | ||
| 45 | bool active_low; | ||
| 46 | bool is_init_success; | ||
| 47 | int *voltages; | ||
| 48 | unsigned curr_vol_sel; | ||
| 49 | struct gpio_switch_regulator_subdev_data *psubdev_data; | ||
| 50 | int (*enable_rail)(struct gpio_switch_regulator_subdev_data *sdata); | ||
| 51 | int (*disable_rail)(struct gpio_switch_regulator_subdev_data *sdata); | ||
| 52 | }; | ||
| 53 | |||
| 54 | static int _gpio_regulator_enable(struct device *dev, | ||
| 55 | struct gpio_switch_regulator *ri) | ||
| 56 | { | ||
| 57 | int init_val; | ||
| 58 | int ret; | ||
| 59 | |||
| 60 | if (ri->enable_rail) { | ||
| 61 | ret = ri->enable_rail(ri->psubdev_data); | ||
| 62 | if (ret < 0) | ||
| 63 | dev_err(dev, "Unable to enable rail through board api" | ||
| 64 | " error %d\n", ret); | ||
| 65 | } else { | ||
| 66 | init_val = (ri->active_low) ? 0 : 1; | ||
| 67 | ret = gpio_direction_output(ri->gpio_nr, init_val); | ||
| 68 | if (ret < 0) | ||
| 69 | dev_err(dev, "Unable to set direction %d\n", | ||
| 70 | ri->gpio_nr); | ||
| 71 | } | ||
| 72 | return ret; | ||
| 73 | } | ||
| 74 | |||
| 75 | static int _gpio_regulator_disable(struct device *dev, | ||
| 76 | struct gpio_switch_regulator *ri) | ||
| 77 | { | ||
| 78 | int init_val; | ||
| 79 | int ret; | ||
| 80 | |||
| 81 | if (ri->disable_rail) { | ||
| 82 | ret = ri->disable_rail(ri->psubdev_data); | ||
| 83 | if (ret < 0) | ||
| 84 | dev_err(dev, "Unable to disable rail through " | ||
| 85 | "board api %d\n", ret); | ||
| 86 | } else { | ||
| 87 | init_val = (ri->active_low) ? 1 : 0; | ||
| 88 | ret = gpio_direction_output(ri->gpio_nr, init_val); | ||
| 89 | if (ret < 0) | ||
| 90 | dev_err(dev, "Unable to set direction %d\n", | ||
| 91 | ri->gpio_nr); | ||
| 92 | } | ||
| 93 | return ret; | ||
| 94 | } | ||
| 95 | |||
| 96 | static int gpio_switch_list_voltage(struct regulator_dev *rdev, | ||
| 97 | unsigned selector) | ||
| 98 | { | ||
| 99 | struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev); | ||
| 100 | |||
| 101 | if (selector < ri->reg_desc.n_voltages) | ||
| 102 | return ri->voltages[selector] * 1000; | ||
| 103 | else | ||
| 104 | return 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | static int gpio_switch_set_voltage(struct regulator_dev *rdev, | ||
| 108 | int min_uV, int max_uV, unsigned *selector) | ||
| 109 | { | ||
| 110 | struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev); | ||
| 111 | int uV; | ||
| 112 | bool found = false; | ||
| 113 | unsigned val; | ||
| 114 | |||
| 115 | for (val = 0; val < ri->reg_desc.n_voltages; val++) { | ||
| 116 | uV = ri->voltages[val] * 1000; | ||
| 117 | if (min_uV <= uV && uV <= max_uV) { | ||
| 118 | found = true; | ||
| 119 | *selector = ri->curr_vol_sel = val; | ||
| 120 | break; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | if (found && ri->input_regulator) | ||
| 124 | return regulator_set_voltage(ri->input_regulator, min_uV, | ||
| 125 | max_uV); | ||
| 126 | ri->curr_vol_sel = 0; | ||
| 127 | return -EINVAL; | ||
| 128 | } | ||
| 129 | |||
| 130 | static int gpio_switch_get_voltage(struct regulator_dev *rdev) | ||
| 131 | { | ||
| 132 | struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev); | ||
| 133 | if (ri->input_regulator) | ||
| 134 | return regulator_get_voltage(ri->input_regulator); | ||
| 135 | |||
| 136 | if (ri->curr_vol_sel < ri->reg_desc.n_voltages) | ||
| 137 | return ri->voltages[ri->curr_vol_sel] * 1000; | ||
| 138 | return 0; | ||
| 139 | } | ||
| 140 | |||
| 141 | static int gpio_switch_regulator_enable(struct regulator_dev *rdev) | ||
| 142 | { | ||
| 143 | struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev); | ||
| 144 | int ret = 0; | ||
| 145 | if (ri->is_enable) | ||
| 146 | return 0; | ||
| 147 | |||
| 148 | if (ri->input_regulator) { | ||
| 149 | ret = regulator_enable(ri->input_regulator); | ||
| 150 | if (ret < 0) { | ||
| 151 | dev_err(&rdev->dev, "%s:Failed to enable regulator" | ||
| 152 | " Error %d\n", __func__, ret); | ||
| 153 | return ret; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | ret = _gpio_regulator_enable(&rdev->dev, ri); | ||
| 158 | if (ret < 0) | ||
| 159 | return ret; | ||
| 160 | ri->is_enable = true; | ||
| 161 | return 0; | ||
| 162 | } | ||
| 163 | |||
| 164 | static int gpio_switch_regulator_disable(struct regulator_dev *rdev) | ||
| 165 | { | ||
| 166 | struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev); | ||
| 167 | int ret = 0; | ||
| 168 | |||
| 169 | if (!ri->is_enable) | ||
| 170 | return 0; | ||
| 171 | |||
| 172 | ret = _gpio_regulator_disable(&rdev->dev, ri); | ||
| 173 | if (ret < 0) | ||
| 174 | return ret; | ||
| 175 | |||
| 176 | if (ri->input_regulator) { | ||
| 177 | ret = regulator_disable(ri->input_regulator); | ||
| 178 | if (ret < 0) { | ||
| 179 | dev_err(&rdev->dev, "%s:Failed to disable regulator" | ||
| 180 | " Error %d\n", __func__, ret); | ||
| 181 | return ret; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | ri->is_enable = false; | ||
| 186 | return 0; | ||
| 187 | } | ||
| 188 | |||
| 189 | static int gpio_switch_regulator_is_enabled(struct regulator_dev *rdev) | ||
| 190 | { | ||
| 191 | struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev); | ||
| 192 | int ret = 0; | ||
| 193 | if (ri->input_regulator) { | ||
| 194 | ret = regulator_is_enabled(ri->input_regulator); | ||
| 195 | if (!ret) | ||
| 196 | return ret; | ||
| 197 | } | ||
| 198 | return (ri->is_enable) ? 1 : 0; | ||
| 199 | } | ||
| 200 | |||
| 201 | static struct regulator_ops gpio_switch_regulator_ | ||
