blob: a056cee7ed46e432d8e65ea83da33f1e41fb88db [file] [log] [blame]
Mark Brown69dc16c2010-01-25 19:41:57 +00001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brown69dc16c2010-01-25 19:41:57 +000023
24#include <linux/mfd/wm8994/core.h>
25#include <linux/mfd/wm8994/registers.h>
26#include <linux/mfd/wm8994/pdata.h>
27
28struct wm8994_ldo {
29 int enable;
Dmitry Torokhov598b3572010-02-23 23:38:44 -080030 bool is_enabled;
Mark Brown69dc16c2010-01-25 19:41:57 +000031 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
38static 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 Brown2ae36362011-05-24 23:14:40 +080046 gpio_set_value_cansleep(ldo->enable, 1);
Dmitry Torokhov598b3572010-02-23 23:38:44 -080047 ldo->is_enabled = true;
Mark Brown69dc16c2010-01-25 19:41:57 +000048
49 return 0;
50}
51
52static 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 Brown2ae36362011-05-24 23:14:40 +080060 gpio_set_value_cansleep(ldo->enable, 0);
Dmitry Torokhov598b3572010-02-23 23:38:44 -080061 ldo->is_enabled = false;
Mark Brown69dc16c2010-01-25 19:41:57 +000062
63 return 0;
64}
65
66static 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
Mark Brown69dc16c2010-01-25 19:41:57 +000073static struct regulator_ops wm8994_ldo1_ops = {
74 .enable = wm8994_ldo_enable,
75 .disable = wm8994_ldo_disable,
76 .is_enabled = wm8994_ldo_is_enabled,
Mark Brown69dc16c2010-01-25 19:41:57 +000077
Axel Linf2d103a2012-05-23 22:40:58 +080078 .list_voltage = regulator_list_voltage_linear,
79 .map_voltage = regulator_map_voltage_linear,
Mark Brown633b6fc2012-04-15 11:25:36 +010080 .get_voltage_sel = regulator_get_voltage_sel_regmap,
81 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brown69dc16c2010-01-25 19:41:57 +000082};
83
84static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
85 unsigned int selector)
86{
Mark Brown5a7743e2010-12-13 15:07:40 +000087 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
88
Mark Brown69dc16c2010-01-25 19:41:57 +000089 if (selector > WM8994_LDO2_MAX_SELECTOR)
90 return -EINVAL;
91
Mark Brown5a7743e2010-12-13 15:07:40 +000092 switch (ldo->wm8994->type) {
93 case WM8994:
94 return (selector * 100000) + 900000;
95 case WM8958:
96 return (selector * 100000) + 1000000;
Mark Browna1ff89e2011-07-24 12:40:48 +010097 case WM1811:
98 switch (selector) {
99 case 0:
100 return -EINVAL;
101 default:
102 return (selector * 100000) + 950000;
103 }
104 break;
Mark Brown5a7743e2010-12-13 15:07:40 +0000105 default:
106 return -EINVAL;
107 }
Mark Brown69dc16c2010-01-25 19:41:57 +0000108}
109
Mark Brown69dc16c2010-01-25 19:41:57 +0000110static struct regulator_ops wm8994_ldo2_ops = {
111 .enable = wm8994_ldo_enable,
112 .disable = wm8994_ldo_disable,
113 .is_enabled = wm8994_ldo_is_enabled,
Mark Brown69dc16c2010-01-25 19:41:57 +0000114
115 .list_voltage = wm8994_ldo2_list_voltage,
Mark Brown633b6fc2012-04-15 11:25:36 +0100116 .get_voltage_sel = regulator_get_voltage_sel_regmap,
117 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brown69dc16c2010-01-25 19:41:57 +0000118};
119
Mark Brown25e4d602012-04-03 20:47:27 +0100120static const struct regulator_desc wm8994_ldo_desc[] = {
Mark Brown69dc16c2010-01-25 19:41:57 +0000121 {
122 .name = "LDO1",
123 .id = 1,
124 .type = REGULATOR_VOLTAGE,
125 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
Mark Brown633b6fc2012-04-15 11:25:36 +0100126 .vsel_reg = WM8994_LDO_1,
127 .vsel_mask = WM8994_LDO1_VSEL_MASK,
Mark Brown69dc16c2010-01-25 19:41:57 +0000128 .ops = &wm8994_ldo1_ops,
Axel Linf2d103a2012-05-23 22:40:58 +0800129 .min_uV = 2400000,
130 .uV_step = 100000,
Mark Brown00581b32012-06-27 14:29:27 +0100131 .enable_time = 3000,
Mark Brown69dc16c2010-01-25 19:41:57 +0000132 .owner = THIS_MODULE,
133 },
134 {
135 .name = "LDO2",
136 .id = 2,
137 .type = REGULATOR_VOLTAGE,
138 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
Mark Brown633b6fc2012-04-15 11:25:36 +0100139 .vsel_reg = WM8994_LDO_2,
140 .vsel_mask = WM8994_LDO2_VSEL_MASK,
Mark Brown69dc16c2010-01-25 19:41:57 +0000141 .ops = &wm8994_ldo2_ops,
Mark Brown00581b32012-06-27 14:29:27 +0100142 .enable_time = 3000,
Mark Brown69dc16c2010-01-25 19:41:57 +0000143 .owner = THIS_MODULE,
144 },
145};
146
147static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
148{
149 struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
150 struct wm8994_pdata *pdata = wm8994->dev->platform_data;
151 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
Mark Brownc172708d2012-04-04 00:50:22 +0100152 struct regulator_config config = { };
Mark Brown69dc16c2010-01-25 19:41:57 +0000153 struct wm8994_ldo *ldo;
154 int ret;
155
156 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
157
Mark Brownb9e03482012-02-15 23:12:17 -0800158 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
Mark Brown69dc16c2010-01-25 19:41:57 +0000159 if (ldo == NULL) {
160 dev_err(&pdev->dev, "Unable to allocate private data\n");
161 return -ENOMEM;
162 }
163
164 ldo->wm8994 = wm8994;
165
Mark Brown69dc16c2010-01-25 19:41:57 +0000166 if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
167 ldo->enable = pdata->ldo[id].enable;
168
Mark Brown09e3fd22012-05-07 18:18:45 +0100169 ret = gpio_request_one(ldo->enable, 0, "WM8994 LDO enable");
Mark Brown69dc16c2010-01-25 19:41:57 +0000170 if (ret < 0) {
171 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
172 ret);
173 goto err;
174 }
Joonyoung Shimc4604e42010-08-06 15:48:24 +0100175 } else
176 ldo->is_enabled = true;
Mark Brown69dc16c2010-01-25 19:41:57 +0000177
Mark Brown5d0526e2012-05-08 17:45:31 +0100178 config.dev = wm8994->dev;
Mark Brownc172708d2012-04-04 00:50:22 +0100179 config.driver_data = ldo;
Mark Brown633b6fc2012-04-15 11:25:36 +0100180 config.regmap = wm8994->regmap;
Mark Brownb68b45d2012-05-14 13:24:13 +0100181 if (pdata)
182 config.init_data = pdata->ldo[id].init_data;
Mark Brownc172708d2012-04-04 00:50:22 +0100183
184 ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &config);
Mark Brown69dc16c2010-01-25 19:41:57 +0000185 if (IS_ERR(ldo->regulator)) {
186 ret = PTR_ERR(ldo->regulator);
187 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
188 id + 1, ret);
189 goto err_gpio;
190 }
191
192 platform_set_drvdata(pdev, ldo);
193
194 return 0;
195
196err_gpio:
197 if (gpio_is_valid(ldo->enable))
198 gpio_free(ldo->enable);
199err:
Mark Brown69dc16c2010-01-25 19:41:57 +0000200 return ret;
201}
202
203static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
204{
205 struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
206
Dmitry Torokhov598b3572010-02-23 23:38:44 -0800207 platform_set_drvdata(pdev, NULL);
208
Mark Brown69dc16c2010-01-25 19:41:57 +0000209 regulator_unregister(ldo->regulator);
210 if (gpio_is_valid(ldo->enable))
211 gpio_free(ldo->enable);
Mark Brown69dc16c2010-01-25 19:41:57 +0000212
213 return 0;
214}
215
216static struct platform_driver wm8994_ldo_driver = {
217 .probe = wm8994_ldo_probe,
218 .remove = __devexit_p(wm8994_ldo_remove),
219 .driver = {
220 .name = "wm8994-ldo",
Dmitry Torokhov598b3572010-02-23 23:38:44 -0800221 .owner = THIS_MODULE,
Mark Brown69dc16c2010-01-25 19:41:57 +0000222 },
223};
224
225static int __init wm8994_ldo_init(void)
226{
227 int ret;
228
229 ret = platform_driver_register(&wm8994_ldo_driver);
230 if (ret != 0)
231 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
232
233 return ret;
234}
235subsys_initcall(wm8994_ldo_init);
236
237static void __exit wm8994_ldo_exit(void)
238{
239 platform_driver_unregister(&wm8994_ldo_driver);
240}
241module_exit(wm8994_ldo_exit);
242
243/* Module information */
244MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
245MODULE_DESCRIPTION("WM8994 LDO driver");
246MODULE_LICENSE("GPL");
247MODULE_ALIAS("platform:wm8994-ldo");