Commit | Line | Data |
---|---|---|
e8d59d9a AL |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | // | |
3 | // wm8994-regulator.c -- Regulator driver for the WM8994 | |
4 | // | |
5 | // Copyright 2009 Wolfson Microelectronics PLC. | |
6 | // | |
7 | // Author: Mark Brown <broonie@opensource.wolfsonmicro.com> | |
69dc16c3 MB |
8 | |
9 | #include <linux/module.h> | |
10 | #include <linux/moduleparam.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/bitops.h> | |
13 | #include <linux/err.h> | |
14 | #include <linux/platform_device.h> | |
15 | #include <linux/regulator/driver.h> | |
e3d27775 | 16 | #include <linux/regulator/machine.h> |
1d2f4681 | 17 | #include <linux/gpio/consumer.h> |
5a0e3ad6 | 18 | #include <linux/slab.h> |
69dc16c3 MB |
19 | |
20 | #include <linux/mfd/wm8994/core.h> | |
21 | #include <linux/mfd/wm8994/registers.h> | |
22 | #include <linux/mfd/wm8994/pdata.h> | |
23 | ||
24 | struct wm8994_ldo { | |
69dc16c3 MB |
25 | struct regulator_dev *regulator; |
26 | struct wm8994 *wm8994; | |
e3d27775 MB |
27 | struct regulator_consumer_supply supply; |
28 | struct regulator_init_data init_data; | |
69dc16c3 MB |
29 | }; |
30 | ||
31 | #define WM8994_LDO1_MAX_SELECTOR 0x7 | |
32 | #define WM8994_LDO2_MAX_SELECTOR 0x3 | |
33 | ||
b0d6dd3b | 34 | static const struct regulator_ops wm8994_ldo1_ops = { |
f2d103ad AL |
35 | .list_voltage = regulator_list_voltage_linear, |
36 | .map_voltage = regulator_map_voltage_linear, | |
633b6fcd MB |
37 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
38 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
69dc16c3 MB |
39 | }; |
40 | ||
41 | static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev, | |
42 | unsigned int selector) | |
43 | { | |
5a7743ed MB |
44 | struct wm8994_ldo *ldo = rdev_get_drvdata(rdev); |
45 | ||
69dc16c3 MB |
46 | if (selector > WM8994_LDO2_MAX_SELECTOR) |
47 | return -EINVAL; | |
48 | ||
5a7743ed MB |
49 | switch (ldo->wm8994->type) { |
50 | case WM8994: | |
51 | return (selector * 100000) + 900000; | |
52 | case WM8958: | |
53 | return (selector * 100000) + 1000000; | |
a1ff89ef MB |
54 | case WM1811: |
55 | switch (selector) { | |
56 | case 0: | |
57 | return -EINVAL; | |
58 | default: | |
59 | return (selector * 100000) + 950000; | |
60 | } | |
61 | break; | |
5a7743ed MB |
62 | default: |
63 | return -EINVAL; | |
64 | } | |
69dc16c3 MB |
65 | } |
66 | ||
b0d6dd3b | 67 | static const struct regulator_ops wm8994_ldo2_ops = { |
69dc16c3 | 68 | .list_voltage = wm8994_ldo2_list_voltage, |
633b6fcd MB |
69 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
70 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
69dc16c3 MB |
71 | }; |
72 | ||
25e4d602 | 73 | static const struct regulator_desc wm8994_ldo_desc[] = { |
92d96b60 JB |
74 | { |
75 | .name = "LDO1", | |
76 | .id = 1, | |
77 | .type = REGULATOR_VOLTAGE, | |
78 | .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1, | |
79 | .vsel_reg = WM8994_LDO_1, | |
80 | .vsel_mask = WM8994_LDO1_VSEL_MASK, | |
81 | .ops = &wm8994_ldo1_ops, | |
82 | .min_uV = 2400000, | |
83 | .uV_step = 100000, | |
84 | .enable_time = 3000, | |
85 | .off_on_delay = 36000, | |
86 | .owner = THIS_MODULE, | |
87 | }, | |
88 | { | |
89 | .name = "LDO2", | |
90 | .id = 2, | |
91 | .type = REGULATOR_VOLTAGE, | |
92 | .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1, | |
93 | .vsel_reg = WM8994_LDO_2, | |
94 | .vsel_mask = WM8994_LDO2_VSEL_MASK, | |
95 | .ops = &wm8994_ldo2_ops, | |
96 | .enable_time = 3000, | |
97 | .off_on_delay = 36000, | |
98 | .owner = THIS_MODULE, | |
99 | }, | |
100 | }; | |
101 | ||
102 | static const struct regulator_desc wm8958_ldo_desc[] = { | |
69dc16c3 MB |
103 | { |
104 | .name = "LDO1", | |
105 | .id = 1, | |
106 | .type = REGULATOR_VOLTAGE, | |
107 | .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1, | |
633b6fcd MB |
108 | .vsel_reg = WM8994_LDO_1, |
109 | .vsel_mask = WM8994_LDO1_VSEL_MASK, | |
69dc16c3 | 110 | .ops = &wm8994_ldo1_ops, |
f2d103ad AL |
111 | .min_uV = 2400000, |
112 | .uV_step = 100000, | |
00581b30 | 113 | .enable_time = 3000, |
69dc16c3 MB |
114 | .owner = THIS_MODULE, |
115 | }, | |
116 | { | |
117 | .name = "LDO2", | |
118 | .id = 2, | |
119 | .type = REGULATOR_VOLTAGE, | |
120 | .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1, | |
633b6fcd MB |
121 | .vsel_reg = WM8994_LDO_2, |
122 | .vsel_mask = WM8994_LDO2_VSEL_MASK, | |
69dc16c3 | 123 | .ops = &wm8994_ldo2_ops, |
00581b30 | 124 | .enable_time = 3000, |
69dc16c3 MB |
125 | .owner = THIS_MODULE, |
126 | }, | |
127 | }; | |
128 | ||
e3d27775 MB |
129 | static const struct regulator_consumer_supply wm8994_ldo_consumer[] = { |
130 | { .supply = "AVDD1" }, | |
131 | { .supply = "DCVDD" }, | |
132 | }; | |
133 | ||
134 | static const struct regulator_init_data wm8994_ldo_default[] = { | |
135 | { | |
136 | .constraints = { | |
137 | .valid_ops_mask = REGULATOR_CHANGE_STATUS, | |
138 | }, | |
139 | .num_consumer_supplies = 1, | |
140 | }, | |
141 | { | |
142 | .constraints = { | |
143 | .valid_ops_mask = REGULATOR_CHANGE_STATUS, | |
144 | }, | |
145 | .num_consumer_supplies = 1, | |
146 | }, | |
147 | }; | |
148 | ||
a5023574 | 149 | static int wm8994_ldo_probe(struct platform_device *pdev) |
69dc16c3 MB |
150 | { |
151 | struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent); | |
dff91d0b | 152 | struct wm8994_pdata *pdata = dev_get_platdata(wm8994->dev); |
69dc16c3 | 153 | int id = pdev->id % ARRAY_SIZE(pdata->ldo); |
c172708d | 154 | struct regulator_config config = { }; |
69dc16c3 | 155 | struct wm8994_ldo *ldo; |
1d2f4681 | 156 | struct gpio_desc *gpiod; |
69dc16c3 MB |
157 | int ret; |
158 | ||
159 | dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1); | |
160 | ||
b9e0348f | 161 | ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL); |
4deaf85a | 162 | if (!ldo) |
69dc16c3 | 163 | return -ENOMEM; |
69dc16c3 MB |
164 | |
165 | ldo->wm8994 = wm8994; | |
e3d27775 MB |
166 | ldo->supply = wm8994_ldo_consumer[id]; |
167 | ldo->supply.dev_name = dev_name(wm8994->dev); | |
69dc16c3 | 168 | |
5d0526ea | 169 | config.dev = wm8994->dev; |
c172708d | 170 | config.driver_data = ldo; |
633b6fcd | 171 | config.regmap = wm8994->regmap; |
e3d27775 | 172 | config.init_data = &ldo->init_data; |
1d2f4681 | 173 | |
466affa0 CK |
174 | /* |
175 | * Look up LDO enable GPIO from the parent device node, we don't | |
176 | * use devm because the regulator core will free the GPIO | |
177 | */ | |
178 | gpiod = gpiod_get_optional(pdev->dev.parent, | |
179 | id ? "wlf,ldo2ena" : "wlf,ldo1ena", | |
180 | GPIOD_OUT_LOW | | |
181 | GPIOD_FLAGS_BIT_NONEXCLUSIVE); | |
1d2f4681 LW |
182 | if (IS_ERR(gpiod)) |
183 | return PTR_ERR(gpiod); | |
184 | config.ena_gpiod = gpiod; | |
e3d27775 MB |
185 | |
186 | /* Use default constraints if none set up */ | |
3ea93896 | 187 | if (!pdata || !pdata->ldo[id].init_data || wm8994->dev->of_node) { |
e3d27775 MB |
188 | dev_dbg(wm8994->dev, "Using default init data, supply %s %s\n", |
189 | ldo->supply.dev_name, ldo->supply.supply); | |
190 | ||
191 | ldo->init_data = wm8994_ldo_default[id]; | |
192 | ldo->init_data.consumer_supplies = &ldo->supply; | |
1d2f4681 | 193 | if (!gpiod) |
e3d27775 MB |
194 | ldo->init_data.constraints.valid_ops_mask = 0; |
195 | } else { | |
196 | ldo->init_data = *pdata->ldo[id].init_data; | |
495fd8c8 | 197 | } |
c172708d | 198 | |
daa531db LW |
199 | /* |
200 | * At this point the GPIO descriptor is handled over to the | |
201 | * regulator core and we need not worry about it on the | |
202 | * error path. | |
203 | */ | |
92d96b60 JB |
204 | if (ldo->wm8994->type == WM8994) { |
205 | ldo->regulator = devm_regulator_register(&pdev->dev, | |
206 | &wm8994_ldo_desc[id], | |
207 | &config); | |
208 | } else { | |
209 | ldo->regulator = devm_regulator_register(&pdev->dev, | |
210 | &wm8958_ldo_desc[id], | |
211 | &config); | |
212 | } | |
213 | ||
69dc16c3 MB |
214 | if (IS_ERR(ldo->regulator)) { |
215 | ret = PTR_ERR(ldo->regulator); | |
216 | dev_err(wm8994->dev, "Failed to register LDO%d: %d\n", | |
217 | id + 1, ret); | |
daa531db | 218 | return ret; |
69dc16c3 MB |
219 | } |
220 | ||
221 | platform_set_drvdata(pdev, ldo); | |
222 | ||
223 | return 0; | |
69dc16c3 MB |
224 | } |
225 | ||
69dc16c3 MB |
226 | static struct platform_driver wm8994_ldo_driver = { |
227 | .probe = wm8994_ldo_probe, | |
69dc16c3 MB |
228 | .driver = { |
229 | .name = "wm8994-ldo", | |
eef644d3 | 230 | .probe_type = PROBE_FORCE_SYNCHRONOUS, |
69dc16c3 MB |
231 | }, |
232 | }; | |
233 | ||
05650216 | 234 | module_platform_driver(wm8994_ldo_driver); |
69dc16c3 MB |
235 | |
236 | /* Module information */ | |
237 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); | |
238 | MODULE_DESCRIPTION("WM8994 LDO driver"); | |
239 | MODULE_LICENSE("GPL"); | |
240 | MODULE_ALIAS("platform:wm8994-ldo"); |