Merge tag 'v3.10.85' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / regulator / wm8994-regulator.c
CommitLineData
69dc16c3
MB
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>
e3d27775 21#include <linux/regulator/machine.h>
69dc16c3 22#include <linux/gpio.h>
5a0e3ad6 23#include <linux/slab.h>
69dc16c3
MB
24
25#include <linux/mfd/wm8994/core.h>
26#include <linux/mfd/wm8994/registers.h>
27#include <linux/mfd/wm8994/pdata.h>
28
29struct wm8994_ldo {
69dc16c3
MB
30 struct regulator_dev *regulator;
31 struct wm8994 *wm8994;
e3d27775
MB
32 struct regulator_consumer_supply supply;
33 struct regulator_init_data init_data;
69dc16c3
MB
34};
35
36#define WM8994_LDO1_MAX_SELECTOR 0x7
37#define WM8994_LDO2_MAX_SELECTOR 0x3
38
69dc16c3 39static struct regulator_ops wm8994_ldo1_ops = {
f2d103ad
AL
40 .list_voltage = regulator_list_voltage_linear,
41 .map_voltage = regulator_map_voltage_linear,
633b6fcd
MB
42 .get_voltage_sel = regulator_get_voltage_sel_regmap,
43 .set_voltage_sel = regulator_set_voltage_sel_regmap,
69dc16c3
MB
44};
45
46static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
47 unsigned int selector)
48{
5a7743ed
MB
49 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
50
69dc16c3
MB
51 if (selector > WM8994_LDO2_MAX_SELECTOR)
52 return -EINVAL;
53
5a7743ed
MB
54 switch (ldo->wm8994->type) {
55 case WM8994:
56 return (selector * 100000) + 900000;
57 case WM8958:
58 return (selector * 100000) + 1000000;
a1ff89ef
MB
59 case WM1811:
60 switch (selector) {
61 case 0:
62 return -EINVAL;
63 default:
64 return (selector * 100000) + 950000;
65 }
66 break;
5a7743ed
MB
67 default:
68 return -EINVAL;
69 }
69dc16c3
MB
70}
71
69dc16c3 72static struct regulator_ops wm8994_ldo2_ops = {
69dc16c3 73 .list_voltage = wm8994_ldo2_list_voltage,
633b6fcd
MB
74 .get_voltage_sel = regulator_get_voltage_sel_regmap,
75 .set_voltage_sel = regulator_set_voltage_sel_regmap,
69dc16c3
MB
76};
77
25e4d602 78static const struct regulator_desc wm8994_ldo_desc[] = {
69dc16c3
MB
79 {
80 .name = "LDO1",
81 .id = 1,
82 .type = REGULATOR_VOLTAGE,
83 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
633b6fcd
MB
84 .vsel_reg = WM8994_LDO_1,
85 .vsel_mask = WM8994_LDO1_VSEL_MASK,
69dc16c3 86 .ops = &wm8994_ldo1_ops,
f2d103ad
AL
87 .min_uV = 2400000,
88 .uV_step = 100000,
00581b30 89 .enable_time = 3000,
69dc16c3
MB
90 .owner = THIS_MODULE,
91 },
92 {
93 .name = "LDO2",
94 .id = 2,
95 .type = REGULATOR_VOLTAGE,
96 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
633b6fcd
MB
97 .vsel_reg = WM8994_LDO_2,
98 .vsel_mask = WM8994_LDO2_VSEL_MASK,
69dc16c3 99 .ops = &wm8994_ldo2_ops,
00581b30 100 .enable_time = 3000,
69dc16c3
MB
101 .owner = THIS_MODULE,
102 },
103};
104
e3d27775
MB
105static const struct regulator_consumer_supply wm8994_ldo_consumer[] = {
106 { .supply = "AVDD1" },
107 { .supply = "DCVDD" },
108};
109
110static const struct regulator_init_data wm8994_ldo_default[] = {
111 {
112 .constraints = {
113 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
114 },
115 .num_consumer_supplies = 1,
116 },
117 {
118 .constraints = {
119 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
120 },
121 .num_consumer_supplies = 1,
122 },
123};
124
a5023574 125static int wm8994_ldo_probe(struct platform_device *pdev)
69dc16c3
MB
126{
127 struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
128 struct wm8994_pdata *pdata = wm8994->dev->platform_data;
129 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
c172708d 130 struct regulator_config config = { };
69dc16c3
MB
131 struct wm8994_ldo *ldo;
132 int ret;
133
134 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
135
b9e0348f 136 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
69dc16c3
MB
137 if (ldo == NULL) {
138 dev_err(&pdev->dev, "Unable to allocate private data\n");
139 return -ENOMEM;
140 }
141
142 ldo->wm8994 = wm8994;
e3d27775
MB
143 ldo->supply = wm8994_ldo_consumer[id];
144 ldo->supply.dev_name = dev_name(wm8994->dev);
69dc16c3 145
5d0526ea 146 config.dev = wm8994->dev;
c172708d 147 config.driver_data = ldo;
633b6fcd 148 config.regmap = wm8994->regmap;
e3d27775
MB
149 config.init_data = &ldo->init_data;
150 if (pdata)
495fd8c8 151 config.ena_gpio = pdata->ldo[id].enable;
3ea93896
SN
152 else if (wm8994->dev->of_node)
153 config.ena_gpio = wm8994->pdata.ldo[id].enable;
e3d27775
MB
154
155 /* Use default constraints if none set up */
3ea93896 156 if (!pdata || !pdata->ldo[id].init_data || wm8994->dev->of_node) {
e3d27775
MB
157 dev_dbg(wm8994->dev, "Using default init data, supply %s %s\n",
158 ldo->supply.dev_name, ldo->supply.supply);
159
160 ldo->init_data = wm8994_ldo_default[id];
161 ldo->init_data.consumer_supplies = &ldo->supply;
162 if (!config.ena_gpio)
163 ldo->init_data.constraints.valid_ops_mask = 0;
164 } else {
165 ldo->init_data = *pdata->ldo[id].init_data;
495fd8c8 166 }
c172708d
MB
167
168 ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &config);
69dc16c3
MB
169 if (IS_ERR(ldo->regulator)) {
170 ret = PTR_ERR(ldo->regulator);
171 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
172 id + 1, ret);
495fd8c8 173 goto err;
69dc16c3
MB
174 }
175
176 platform_set_drvdata(pdev, ldo);
177
178 return 0;
179
69dc16c3 180err:
69dc16c3
MB
181 return ret;
182}
183
8dc995f5 184static int wm8994_ldo_remove(struct platform_device *pdev)
69dc16c3
MB
185{
186 struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
187
598b3578
DT
188 platform_set_drvdata(pdev, NULL);
189
69dc16c3 190 regulator_unregister(ldo->regulator);
69dc16c3
MB
191
192 return 0;
193}
194
195static struct platform_driver wm8994_ldo_driver = {
196 .probe = wm8994_ldo_probe,
5eb9f2b9 197 .remove = wm8994_ldo_remove,
69dc16c3
MB
198 .driver = {
199 .name = "wm8994-ldo",
598b3578 200 .owner = THIS_MODULE,
69dc16c3
MB
201 },
202};
203
05650216 204module_platform_driver(wm8994_ldo_driver);
69dc16c3
MB
205
206/* Module information */
207MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
208MODULE_DESCRIPTION("WM8994 LDO driver");
209MODULE_LICENSE("GPL");
210MODULE_ALIAS("platform:wm8994-ldo");