Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011-2012, 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/module.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/gpio.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/regulator/driver.h> |
| 25 | #include <linux/regulator/machine.h> |
| 26 | #include <linux/regulator/msm-gpio-regulator.h> |
| 27 | |
| 28 | struct gpio_vreg { |
| 29 | struct regulator_desc desc; |
| 30 | struct regulator_dev *rdev; |
| 31 | char *gpio_label; |
| 32 | char *name; |
| 33 | unsigned gpio; |
| 34 | int active_low; |
| 35 | bool gpio_requested; |
| 36 | }; |
| 37 | |
| 38 | static int gpio_vreg_request_gpio(struct gpio_vreg *vreg) |
| 39 | { |
| 40 | int rc = 0; |
| 41 | |
| 42 | /* Request GPIO now if it hasn't been requested before. */ |
| 43 | if (!vreg->gpio_requested) { |
| 44 | rc = gpio_request(vreg->gpio, vreg->gpio_label); |
David Collins | ecff1ce | 2012-06-12 17:09:42 -0700 | [diff] [blame] | 45 | if (rc < 0) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 46 | pr_err("failed to request gpio %u (%s), rc=%d\n", |
| 47 | vreg->gpio, vreg->gpio_label, rc); |
David Collins | ecff1ce | 2012-06-12 17:09:42 -0700 | [diff] [blame] | 48 | return rc; |
| 49 | } else { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 50 | vreg->gpio_requested = true; |
David Collins | ecff1ce | 2012-06-12 17:09:42 -0700 | [diff] [blame] | 51 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 52 | |
| 53 | rc = gpio_sysfs_set_active_low(vreg->gpio, vreg->active_low); |
| 54 | if (rc < 0) |
| 55 | pr_err("active_low=%d failed for gpio %u, rc=%d\n", |
| 56 | vreg->active_low, vreg->gpio, rc); |
| 57 | } |
| 58 | |
| 59 | return rc; |
| 60 | } |
| 61 | |
| 62 | static int gpio_vreg_is_enabled(struct regulator_dev *rdev) |
| 63 | { |
| 64 | struct gpio_vreg *vreg = rdev_get_drvdata(rdev); |
| 65 | int rc; |
| 66 | |
| 67 | rc = gpio_vreg_request_gpio(vreg); |
| 68 | if (rc < 0) |
| 69 | return rc; |
| 70 | |
| 71 | return (gpio_get_value_cansleep(vreg->gpio) ? 1 : 0) ^ vreg->active_low; |
| 72 | } |
| 73 | |
| 74 | static int gpio_vreg_enable(struct regulator_dev *rdev) |
| 75 | { |
| 76 | struct gpio_vreg *vreg = rdev_get_drvdata(rdev); |
| 77 | int rc; |
| 78 | |
| 79 | rc = gpio_vreg_request_gpio(vreg); |
| 80 | if (rc < 0) |
| 81 | return rc; |
| 82 | |
| 83 | return gpio_direction_output(vreg->gpio, !vreg->active_low); |
| 84 | } |
| 85 | |
| 86 | static int gpio_vreg_disable(struct regulator_dev *rdev) |
| 87 | { |
| 88 | struct gpio_vreg *vreg = rdev_get_drvdata(rdev); |
| 89 | int rc; |
| 90 | |
| 91 | rc = gpio_vreg_request_gpio(vreg); |
| 92 | if (rc < 0) |
| 93 | return rc; |
| 94 | |
| 95 | return gpio_direction_output(vreg->gpio, vreg->active_low); |
| 96 | } |
| 97 | |
| 98 | static struct regulator_ops gpio_vreg_ops = { |
| 99 | .enable = gpio_vreg_enable, |
| 100 | .disable = gpio_vreg_disable, |
| 101 | .is_enabled = gpio_vreg_is_enabled, |
| 102 | }; |
| 103 | |
| 104 | static int __devinit gpio_vreg_probe(struct platform_device *pdev) |
| 105 | { |
| 106 | const struct gpio_regulator_platform_data *pdata; |
| 107 | struct gpio_vreg *vreg; |
| 108 | int rc = 0; |
| 109 | |
| 110 | pdata = pdev->dev.platform_data; |
| 111 | |
| 112 | if (!pdata) { |
| 113 | pr_err("platform data required.\n"); |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | if (!pdata->gpio_label) { |
| 118 | pr_err("gpio_label required.\n"); |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | |
| 122 | if (!pdata->regulator_name) { |
| 123 | pr_err("regulator_name required.\n"); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
| 127 | vreg = kzalloc(sizeof(struct gpio_vreg), GFP_KERNEL); |
| 128 | if (!vreg) { |
| 129 | pr_err("kzalloc failed.\n"); |
| 130 | return -ENOMEM; |
| 131 | } |
| 132 | |
| 133 | vreg->name = kstrdup(pdata->regulator_name, GFP_KERNEL); |
| 134 | if (!vreg->name) { |
| 135 | pr_err("kzalloc failed.\n"); |
| 136 | rc = -ENOMEM; |
| 137 | goto free_vreg; |
| 138 | } |
| 139 | |
| 140 | vreg->gpio_label = kstrdup(pdata->gpio_label, GFP_KERNEL); |
| 141 | if (!vreg->gpio_label) { |
| 142 | pr_err("kzalloc failed.\n"); |
| 143 | rc = -ENOMEM; |
| 144 | goto free_name; |
| 145 | } |
| 146 | |
| 147 | vreg->gpio = pdata->gpio; |
| 148 | vreg->active_low = (pdata->active_low ? 1 : 0); |
| 149 | vreg->gpio_requested = false; |
| 150 | |
| 151 | vreg->desc.name = vreg->name; |
| 152 | vreg->desc.id = pdev->id; |
| 153 | vreg->desc.ops = &gpio_vreg_ops; |
| 154 | vreg->desc.type = REGULATOR_VOLTAGE; |
| 155 | vreg->desc.owner = THIS_MODULE; |
| 156 | |
| 157 | vreg->rdev = regulator_register(&vreg->desc, &pdev->dev, |
| 158 | &pdata->init_data, vreg, NULL); |
| 159 | if (IS_ERR(vreg->rdev)) { |
| 160 | rc = PTR_ERR(vreg->rdev); |
| 161 | pr_err("%s: regulator_register failed, rc=%d.\n", vreg->name, |
| 162 | rc); |
| 163 | goto free_gpio_label; |
| 164 | } |
| 165 | |
| 166 | platform_set_drvdata(pdev, vreg); |
| 167 | |
| 168 | pr_info("id=%d, name=%s, gpio=%u, gpio_label=%s\n", pdev->id, |
| 169 | vreg->name, vreg->gpio, vreg->gpio_label); |
| 170 | |
| 171 | return rc; |
| 172 | |
| 173 | free_gpio_label: |
| 174 | kfree(vreg->gpio_label); |
| 175 | free_name: |
| 176 | kfree(vreg->name); |
| 177 | free_vreg: |
| 178 | kfree(vreg); |
| 179 | |
| 180 | return rc; |
| 181 | } |
| 182 | |
| 183 | static int __devexit gpio_vreg_remove(struct platform_device *pdev) |
| 184 | { |
| 185 | struct gpio_vreg *vreg = platform_get_drvdata(pdev); |
| 186 | |
| 187 | if (vreg->gpio_requested) |
| 188 | gpio_free(vreg->gpio); |
| 189 | |
| 190 | regulator_unregister(vreg->rdev); |
| 191 | kfree(vreg->name); |
| 192 | kfree(vreg->gpio_label); |
| 193 | kfree(vreg); |
| 194 | platform_set_drvdata(pdev, NULL); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static struct platform_driver gpio_vreg_driver = { |
| 200 | .probe = gpio_vreg_probe, |
| 201 | .remove = __devexit_p(gpio_vreg_remove), |
| 202 | .driver = { |
| 203 | .name = GPIO_REGULATOR_DEV_NAME, |
| 204 | .owner = THIS_MODULE, |
| 205 | }, |
| 206 | }; |
| 207 | |
| 208 | static int __init gpio_vreg_init(void) |
| 209 | { |
| 210 | return platform_driver_register(&gpio_vreg_driver); |
| 211 | } |
| 212 | |
| 213 | static void __exit gpio_vreg_exit(void) |
| 214 | { |
| 215 | platform_driver_unregister(&gpio_vreg_driver); |
| 216 | } |
| 217 | |
| 218 | postcore_initcall(gpio_vreg_init); |
| 219 | module_exit(gpio_vreg_exit); |
| 220 | |
| 221 | MODULE_LICENSE("GPL v2"); |
| 222 | MODULE_DESCRIPTION("GPIO regulator driver"); |
| 223 | MODULE_VERSION("1.0"); |
| 224 | MODULE_ALIAS("platform:" GPIO_REGULATOR_DEV_NAME); |