2 * wm8994-regulator.c -- Regulator driver for the WM8994
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
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>
22 #include <linux/slab.h>
24 #include <linux/mfd/wm8994/core.h>
25 #include <linux/mfd/wm8994/registers.h>
26 #include <linux/mfd/wm8994/pdata.h>
31 struct regulator_dev *regulator;
32 struct wm8994 *wm8994;
35 #define WM8994_LDO1_MAX_SELECTOR 0x7
36 #define WM8994_LDO2_MAX_SELECTOR 0x3
38 static int wm8994_ldo_enable(struct regulator_dev *rdev)
40 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
42 /* If we have no soft control assume that the LDO is always enabled. */
46 gpio_set_value_cansleep(ldo->enable, 1);
47 ldo->is_enabled = true;
52 static int wm8994_ldo_disable(struct regulator_dev *rdev)
54 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
56 /* If we have no soft control assume that the LDO is always enabled. */
60 gpio_set_value_cansleep(ldo->enable, 0);
61 ldo->is_enabled = false;
66 static int wm8994_ldo_is_enabled(struct regulator_dev *rdev)
68 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
70 return ldo->is_enabled;
73 static int wm8994_ldo_enable_time(struct regulator_dev *rdev)
75 /* 3ms is fairly conservative but this shouldn't be too performance
76 * critical; can be tweaked per-system if required. */
80 static int wm8994_ldo1_list_voltage(struct regulator_dev *rdev,
81 unsigned int selector)
83 if (selector > WM8994_LDO1_MAX_SELECTOR)
86 return (selector * 100000) + 2400000;
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,
95 .list_voltage = wm8994_ldo1_list_voltage,
96 .get_voltage_sel = regulator_get_voltage_sel_regmap,
97 .set_voltage_sel = regulator_set_voltage_sel_regmap,
100 static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
101 unsigned int selector)
103 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
105 if (selector > WM8994_LDO2_MAX_SELECTOR)
108 switch (ldo->wm8994->type) {
110 return (selector * 100000) + 900000;
112 return (selector * 100000) + 1000000;
118 return (selector * 100000) + 950000;
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,
132 .list_voltage = wm8994_ldo2_list_voltage,
133 .get_voltage_sel = regulator_get_voltage_sel_regmap,
134 .set_voltage_sel = regulator_set_voltage_sel_regmap,
137 static const struct regulator_desc wm8994_ldo_desc[] = {
141 .type = REGULATOR_VOLTAGE,
142 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
143 .vsel_reg = WM8994_LDO_1,
144 .vsel_mask = WM8994_LDO1_VSEL_MASK,
145 .ops = &wm8994_ldo1_ops,
146 .owner = THIS_MODULE,
151 .type = REGULATOR_VOLTAGE,
152 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
153 .vsel_reg = WM8994_LDO_2,
154 .vsel_mask = WM8994_LDO2_VSEL_MASK,
155 .ops = &wm8994_ldo2_ops,
156 .owner = THIS_MODULE,
160 static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
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);
165 struct regulator_config config = { };
166 struct wm8994_ldo *ldo;
169 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
174 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
176 dev_err(&pdev->dev, "Unable to allocate private data\n");
180 ldo->wm8994 = wm8994;
182 if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
183 ldo->enable = pdata->ldo[id].enable;
185 ret = gpio_request(ldo->enable, "WM8994 LDO enable");
187 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
192 ret = gpio_direction_output(ldo->enable, ldo->is_enabled);
194 dev_err(&pdev->dev, "Failed to set GPIO up: %d\n",
199 ldo->is_enabled = true;
201 config.dev = &pdev->dev;
202 config.init_data = pdata->ldo[id].init_data;
203 config.driver_data = ldo;
204 config.regmap = wm8994->regmap;
206 ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &config);
207 if (IS_ERR(ldo->regulator)) {
208 ret = PTR_ERR(ldo->regulator);
209 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
214 platform_set_drvdata(pdev, ldo);
219 if (gpio_is_valid(ldo->enable))
220 gpio_free(ldo->enable);
225 static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
227 struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
229 platform_set_drvdata(pdev, NULL);
231 regulator_unregister(ldo->regulator);
232 if (gpio_is_valid(ldo->enable))
233 gpio_free(ldo->enable);
238 static struct platform_driver wm8994_ldo_driver = {
239 .probe = wm8994_ldo_probe,
240 .remove = __devexit_p(wm8994_ldo_remove),
242 .name = "wm8994-ldo",
243 .owner = THIS_MODULE,
247 static int __init wm8994_ldo_init(void)
251 ret = platform_driver_register(&wm8994_ldo_driver);
253 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
257 subsys_initcall(wm8994_ldo_init);
259 static void __exit wm8994_ldo_exit(void)
261 platform_driver_unregister(&wm8994_ldo_driver);
263 module_exit(wm8994_ldo_exit);
265 /* Module information */
266 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
267 MODULE_DESCRIPTION("WM8994 LDO driver");
268 MODULE_LICENSE("GPL");
269 MODULE_ALIAS("platform:wm8994-ldo");