Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * wm8994-regulator.c -- Regulator driver for the WM8994 |
| 3 | * |
| 4 | * Copyright 2009 Wolfson Microelectronics PLC. |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/bitops.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/regulator/driver.h> |
| 21 | #include <linux/gpio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 23 | |
| 24 | #include <linux/mfd/wm8994/core.h> |
| 25 | #include <linux/mfd/wm8994/registers.h> |
| 26 | #include <linux/mfd/wm8994/pdata.h> |
| 27 | |
| 28 | struct wm8994_ldo { |
| 29 | int enable; |
Dmitry Torokhov | 598b357 | 2010-02-23 23:38:44 -0800 | [diff] [blame] | 30 | bool is_enabled; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 31 | struct regulator_dev *regulator; |
| 32 | struct wm8994 *wm8994; |
| 33 | }; |
| 34 | |
| 35 | #define WM8994_LDO1_MAX_SELECTOR 0x7 |
| 36 | #define WM8994_LDO2_MAX_SELECTOR 0x3 |
| 37 | |
| 38 | static int wm8994_ldo_enable(struct regulator_dev *rdev) |
| 39 | { |
| 40 | struct wm8994_ldo *ldo = rdev_get_drvdata(rdev); |
| 41 | |
| 42 | /* If we have no soft control assume that the LDO is always enabled. */ |
| 43 | if (!ldo->enable) |
| 44 | return 0; |
| 45 | |
Mark Brown | 2ae3636 | 2011-05-24 23:14:40 +0800 | [diff] [blame] | 46 | gpio_set_value_cansleep(ldo->enable, 1); |
Dmitry Torokhov | 598b357 | 2010-02-23 23:38:44 -0800 | [diff] [blame] | 47 | ldo->is_enabled = true; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int wm8994_ldo_disable(struct regulator_dev *rdev) |
| 53 | { |
| 54 | struct wm8994_ldo *ldo = rdev_get_drvdata(rdev); |
| 55 | |
| 56 | /* If we have no soft control assume that the LDO is always enabled. */ |
| 57 | if (!ldo->enable) |
| 58 | return -EINVAL; |
| 59 | |
Mark Brown | 2ae3636 | 2011-05-24 23:14:40 +0800 | [diff] [blame] | 60 | gpio_set_value_cansleep(ldo->enable, 0); |
Dmitry Torokhov | 598b357 | 2010-02-23 23:38:44 -0800 | [diff] [blame] | 61 | ldo->is_enabled = false; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int wm8994_ldo_is_enabled(struct regulator_dev *rdev) |
| 67 | { |
| 68 | struct wm8994_ldo *ldo = rdev_get_drvdata(rdev); |
| 69 | |
| 70 | return ldo->is_enabled; |
| 71 | } |
| 72 | |
| 73 | static int wm8994_ldo_enable_time(struct regulator_dev *rdev) |
| 74 | { |
| 75 | /* 3ms is fairly conservative but this shouldn't be too performance |
| 76 | * critical; can be tweaked per-system if required. */ |
| 77 | return 3000; |
| 78 | } |
| 79 | |
| 80 | static int wm8994_ldo1_list_voltage(struct regulator_dev *rdev, |
| 81 | unsigned int selector) |
| 82 | { |
| 83 | if (selector > WM8994_LDO1_MAX_SELECTOR) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | return (selector * 100000) + 2400000; |
| 87 | } |
| 88 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 89 | static struct regulator_ops wm8994_ldo1_ops = { |
| 90 | .enable = wm8994_ldo_enable, |
| 91 | .disable = wm8994_ldo_disable, |
| 92 | .is_enabled = wm8994_ldo_is_enabled, |
| 93 | .enable_time = wm8994_ldo_enable_time, |
| 94 | |
| 95 | .list_voltage = wm8994_ldo1_list_voltage, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 96 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 97 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev, |
| 101 | unsigned int selector) |
| 102 | { |
Mark Brown | 5a7743e | 2010-12-13 15:07:40 +0000 | [diff] [blame] | 103 | struct wm8994_ldo *ldo = rdev_get_drvdata(rdev); |
| 104 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 105 | if (selector > WM8994_LDO2_MAX_SELECTOR) |
| 106 | return -EINVAL; |
| 107 | |
Mark Brown | 5a7743e | 2010-12-13 15:07:40 +0000 | [diff] [blame] | 108 | switch (ldo->wm8994->type) { |
| 109 | case WM8994: |
| 110 | return (selector * 100000) + 900000; |
| 111 | case WM8958: |
| 112 | return (selector * 100000) + 1000000; |
Mark Brown | a1ff89e | 2011-07-24 12:40:48 +0100 | [diff] [blame] | 113 | case WM1811: |
| 114 | switch (selector) { |
| 115 | case 0: |
| 116 | return -EINVAL; |
| 117 | default: |
| 118 | return (selector * 100000) + 950000; |
| 119 | } |
| 120 | break; |
Mark Brown | 5a7743e | 2010-12-13 15:07:40 +0000 | [diff] [blame] | 121 | default: |
| 122 | return -EINVAL; |
| 123 | } |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 126 | static struct regulator_ops wm8994_ldo2_ops = { |
| 127 | .enable = wm8994_ldo_enable, |
| 128 | .disable = wm8994_ldo_disable, |
| 129 | .is_enabled = wm8994_ldo_is_enabled, |
| 130 | .enable_time = wm8994_ldo_enable_time, |
| 131 | |
| 132 | .list_voltage = wm8994_ldo2_list_voltage, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 133 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 134 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
Mark Brown | 25e4d60 | 2012-04-03 20:47:27 +0100 | [diff] [blame] | 137 | static const struct regulator_desc wm8994_ldo_desc[] = { |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 138 | { |
| 139 | .name = "LDO1", |
| 140 | .id = 1, |
| 141 | .type = REGULATOR_VOLTAGE, |
| 142 | .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 143 | .vsel_reg = WM8994_LDO_1, |
| 144 | .vsel_mask = WM8994_LDO1_VSEL_MASK, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 145 | .ops = &wm8994_ldo1_ops, |
| 146 | .owner = THIS_MODULE, |
| 147 | }, |
| 148 | { |
| 149 | .name = "LDO2", |
| 150 | .id = 2, |
| 151 | .type = REGULATOR_VOLTAGE, |
| 152 | .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 153 | .vsel_reg = WM8994_LDO_2, |
| 154 | .vsel_mask = WM8994_LDO2_VSEL_MASK, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 155 | .ops = &wm8994_ldo2_ops, |
| 156 | .owner = THIS_MODULE, |
| 157 | }, |
| 158 | }; |
| 159 | |
| 160 | static __devinit int wm8994_ldo_probe(struct platform_device *pdev) |
| 161 | { |
| 162 | struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent); |
| 163 | struct wm8994_pdata *pdata = wm8994->dev->platform_data; |
| 164 | int id = pdev->id % ARRAY_SIZE(pdata->ldo); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 165 | struct regulator_config config = { }; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 166 | struct wm8994_ldo *ldo; |
| 167 | int ret; |
| 168 | |
| 169 | dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1); |
| 170 | |
Mark Brown | b9e0348 | 2012-02-15 23:12:17 -0800 | [diff] [blame] | 171 | ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 172 | if (ldo == NULL) { |
| 173 | dev_err(&pdev->dev, "Unable to allocate private data\n"); |
| 174 | return -ENOMEM; |
| 175 | } |
| 176 | |
| 177 | ldo->wm8994 = wm8994; |
| 178 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 179 | if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) { |
| 180 | ldo->enable = pdata->ldo[id].enable; |
| 181 | |
Mark Brown | 09e3fd2 | 2012-05-07 18:18:45 +0100 | [diff] [blame] | 182 | ret = gpio_request_one(ldo->enable, 0, "WM8994 LDO enable"); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 183 | if (ret < 0) { |
| 184 | dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", |
| 185 | ret); |
| 186 | goto err; |
| 187 | } |
Joonyoung Shim | c4604e4 | 2010-08-06 15:48:24 +0100 | [diff] [blame] | 188 | } else |
| 189 | ldo->is_enabled = true; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 190 | |
Mark Brown | 5d0526e | 2012-05-08 17:45:31 +0100 | [diff] [blame] | 191 | config.dev = wm8994->dev; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 192 | config.driver_data = ldo; |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 193 | config.regmap = wm8994->regmap; |
Mark Brown | b68b45d | 2012-05-14 13:24:13 +0100 | [diff] [blame^] | 194 | if (pdata) |
| 195 | config.init_data = pdata->ldo[id].init_data; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 196 | |
| 197 | ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &config); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 198 | if (IS_ERR(ldo->regulator)) { |
| 199 | ret = PTR_ERR(ldo->regulator); |
| 200 | dev_err(wm8994->dev, "Failed to register LDO%d: %d\n", |
| 201 | id + 1, ret); |
| 202 | goto err_gpio; |
| 203 | } |
| 204 | |
| 205 | platform_set_drvdata(pdev, ldo); |
| 206 | |
| 207 | return 0; |
| 208 | |
| 209 | err_gpio: |
| 210 | if (gpio_is_valid(ldo->enable)) |
| 211 | gpio_free(ldo->enable); |
| 212 | err: |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | static __devexit int wm8994_ldo_remove(struct platform_device *pdev) |
| 217 | { |
| 218 | struct wm8994_ldo *ldo = platform_get_drvdata(pdev); |
| 219 | |
Dmitry Torokhov | 598b357 | 2010-02-23 23:38:44 -0800 | [diff] [blame] | 220 | platform_set_drvdata(pdev, NULL); |
| 221 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 222 | regulator_unregister(ldo->regulator); |
| 223 | if (gpio_is_valid(ldo->enable)) |
| 224 | gpio_free(ldo->enable); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static struct platform_driver wm8994_ldo_driver = { |
| 230 | .probe = wm8994_ldo_probe, |
| 231 | .remove = __devexit_p(wm8994_ldo_remove), |
| 232 | .driver = { |
| 233 | .name = "wm8994-ldo", |
Dmitry Torokhov | 598b357 | 2010-02-23 23:38:44 -0800 | [diff] [blame] | 234 | .owner = THIS_MODULE, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 235 | }, |
| 236 | }; |
| 237 | |
| 238 | static int __init wm8994_ldo_init(void) |
| 239 | { |
| 240 | int ret; |
| 241 | |
| 242 | ret = platform_driver_register(&wm8994_ldo_driver); |
| 243 | if (ret != 0) |
| 244 | pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret); |
| 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | subsys_initcall(wm8994_ldo_init); |
| 249 | |
| 250 | static void __exit wm8994_ldo_exit(void) |
| 251 | { |
| 252 | platform_driver_unregister(&wm8994_ldo_driver); |
| 253 | } |
| 254 | module_exit(wm8994_ldo_exit); |
| 255 | |
| 256 | /* Module information */ |
| 257 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 258 | MODULE_DESCRIPTION("WM8994 LDO driver"); |
| 259 | MODULE_LICENSE("GPL"); |
| 260 | MODULE_ALIAS("platform:wm8994-ldo"); |