Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 15 | |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/gpio.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/regulator/driver.h> |
| 24 | #include <linux/regulator/machine.h> |
| 25 | #include <linux/regulator/gpio-regulator.h> |
| 26 | |
| 27 | struct gpio_vreg { |
| 28 | struct regulator_desc desc; |
| 29 | struct regulator_dev *rdev; |
| 30 | char *gpio_label; |
| 31 | char *name; |
| 32 | unsigned gpio; |
| 33 | int active_low; |
| 34 | bool gpio_requested; |
| 35 | }; |
| 36 | |
| 37 | static int gpio_vreg_request_gpio(struct gpio_vreg *vreg) |
| 38 | { |
| 39 | int rc = 0; |
| 40 | |
| 41 | /* Request GPIO now if it hasn't been requested before. */ |
| 42 | if (!vreg->gpio_requested) { |
| 43 | rc = gpio_request(vreg->gpio, vreg->gpio_label); |
| 44 | if (rc < 0) |
| 45 | pr_err("failed to request gpio %u (%s), rc=%d\n", |
| 46 | vreg->gpio, vreg->gpio_label, rc); |
| 47 | else |
| 48 | vreg->gpio_requested = true; |
| 49 | |
| 50 | rc = gpio_sysfs_set_active_low(vreg->gpio, vreg->active_low); |
| 51 | if (rc < 0) |
| 52 | pr_err("active_low=%d failed for gpio %u, rc=%d\n", |
| 53 | vreg->active_low, vreg->gpio, rc); |
| 54 | } |
| 55 | |
| 56 | return rc; |
| 57 | } |
| 58 | |
| 59 | static int gpio_vreg_is_enabled(struct regulator_dev *rdev) |
| 60 | { |
| 61 | struct gpio_vreg *vreg = rdev_get_drvdata(rdev); |
| 62 | int rc; |
| 63 | |
| 64 | rc = gpio_vreg_request_gpio(vreg); |
| 65 | if (rc < 0) |
| 66 | return rc; |
| 67 | |
| 68 | return (gpio_get_value_cansleep(vreg->gpio) ? 1 : 0) ^ vreg->active_low; |
| 69 | } |
| 70 | |
| 71 | static int gpio_vreg_enable(struct regulator_dev *rdev) |
| 72 | { |
| 73 | struct gpio_vreg *vreg = rdev_get_drvdata(rdev); |
| 74 | int rc; |
| 75 | |
| 76 | rc = gpio_vreg_request_gpio(vreg); |
| 77 | if (rc < 0) |
| 78 | return rc; |
| 79 | |
| 80 | return gpio_direction_output(vreg->gpio, !vreg->active_low); |
| 81 | } |
| 82 | |
| 83 | static int gpio_vreg_disable(struct regulator_dev *rdev) |
| 84 | { |
| 85 | struct gpio_vreg *vreg = rdev_get_drvdata(rdev); |
| 86 | int rc; |
| 87 | |
| 88 | rc = gpio_vreg_request_gpio(vreg); |
| 89 | if (rc < 0) |
| 90 | return rc; |
| 91 | |
| 92 | return gpio_direction_output(vreg->gpio, vreg->active_low); |
| 93 | } |
| 94 | |
| 95 | static struct regulator_ops gpio_vreg_ops = { |
| 96 | .enable = gpio_vreg_enable, |
| 97 | .disable = gpio_vreg_disable, |
| 98 | .is_enabled = gpio_vreg_is_enabled, |
| 99 | }; |
| 100 | |
| 101 | static int __devinit gpio_vreg_probe(struct platform_device *pdev) |
| 102 | { |
| 103 | const struct gpio_regulator_platform_data *pdata; |
| 104 | struct gpio_vreg *vreg; |
| 105 | int rc = 0; |
| 106 | |
| 107 | pdata = pdev->dev.platform_data; |
| 108 | |
| 109 | if (!pdata) { |
| 110 | pr_err("platform data required.\n"); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
| 114 | if (!pdata->gpio_label) { |
| 115 | pr_err("gpio_label required.\n"); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | if (!pdata->regulator_name) { |
| 120 | pr_err("regulator_name required.\n"); |
| 121 | return -EINVAL; |
| 122 | } |
| 123 | |
| 124 | vreg = kzalloc(sizeof(struct gpio_vreg), GFP_KERNEL); |
| 125 | if (!vreg) { |
| 126 | pr_err("kzalloc failed.\n"); |
| 127 | return -ENOMEM; |
| 128 | } |
| 129 | |
| 130 | vreg->name = kstrdup(pdata->regulator_name, GFP_KERNEL); |
| 131 | if (!vreg->name) { |
| 132 | pr_err("kzalloc failed.\n"); |
| 133 | rc = -ENOMEM; |
| 134 | goto free_vreg; |
| 135 | } |
| 136 | |
| 137 | vreg->gpio_label = kstrdup(pdata->gpio_label, GFP_KERNEL); |
| 138 | if (!vreg->gpio_label) { |
| 139 | pr_err("kzalloc failed.\n"); |
| 140 | rc = -ENOMEM; |
| 141 | goto free_name; |
| 142 | } |
| 143 | |
| 144 | vreg->gpio = pdata->gpio; |
| 145 | vreg->active_low = (pdata->active_low ? 1 : 0); |
| 146 | vreg->gpio_requested = false; |
| 147 | |
| 148 | vreg->desc.name = vreg->name; |
| 149 | vreg->desc.id = pdev->id; |
| 150 | vreg->desc.ops = &gpio_vreg_ops; |
| 151 | vreg->desc.type = REGULATOR_VOLTAGE; |
| 152 | vreg->desc.owner = THIS_MODULE; |
| 153 | |
| 154 | vreg->rdev = regulator_register(&vreg->desc, &pdev->dev, |
| 155 | &pdata->init_data, vreg); |
| 156 | if (IS_ERR(vreg->rdev)) { |
| 157 | rc = PTR_ERR(vreg->rdev); |
| 158 | pr_err("%s: regulator_register failed, rc=%d.\n", vreg->name, |
| 159 | rc); |
| 160 | goto free_gpio_label; |
| 161 | } |
| 162 | |
| 163 | platform_set_drvdata(pdev, vreg); |
| 164 | |
| 165 | pr_info("id=%d, name=%s, gpio=%u, gpio_label=%s\n", pdev->id, |
| 166 | vreg->name, vreg->gpio, vreg->gpio_label); |
| 167 | |
| 168 | return rc; |
| 169 | |
| 170 | free_gpio_label: |
| 171 | kfree(vreg->gpio_label); |
| 172 | free_name: |
| 173 | kfree(vreg->name); |
| 174 | free_vreg: |
| 175 | kfree(vreg); |
| 176 | |
| 177 | return rc; |
| 178 | } |
| 179 | |
| 180 | static int __devexit gpio_vreg_remove(struct platform_device *pdev) |
| 181 | { |
| 182 | struct gpio_vreg *vreg = platform_get_drvdata(pdev); |
| 183 | |
| 184 | if (vreg->gpio_requested) |
| 185 | gpio_free(vreg->gpio); |
| 186 | |
| 187 | regulator_unregister(vreg->rdev); |
| 188 | kfree(vreg->name); |
| 189 | kfree(vreg->gpio_label); |
| 190 | kfree(vreg); |
| 191 | platform_set_drvdata(pdev, NULL); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static struct platform_driver gpio_vreg_driver = { |
| 197 | .probe = gpio_vreg_probe, |
| 198 | .remove = __devexit_p(gpio_vreg_remove), |
| 199 | .driver = { |
| 200 | .name = GPIO_REGULATOR_DEV_NAME, |
| 201 | .owner = THIS_MODULE, |
| 202 | }, |
| 203 | }; |
| 204 | |
| 205 | static int __init gpio_vreg_init(void) |
| 206 | { |
| 207 | return platform_driver_register(&gpio_vreg_driver); |
| 208 | } |
| 209 | |
| 210 | static void __exit gpio_vreg_exit(void) |
| 211 | { |
| 212 | platform_driver_unregister(&gpio_vreg_driver); |
| 213 | } |
| 214 | |
| 215 | postcore_initcall(gpio_vreg_init); |
| 216 | module_exit(gpio_vreg_exit); |
| 217 | |
| 218 | MODULE_LICENSE("GPL v2"); |
| 219 | MODULE_DESCRIPTION("GPIO regulator driver"); |
| 220 | MODULE_VERSION("1.0"); |
| 221 | MODULE_ALIAS("platform:" GPIO_REGULATOR_DEV_NAME); |