Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
95f1dc08 CX |
2 | /* |
3 | * Regulators driver for Marvell 88PM800 | |
4 | * | |
5 | * Copyright (C) 2012 Marvell International Ltd. | |
6 | * Joseph(Yossi) Hanin <yhanin@marvell.com> | |
7 | * Yi Zhang <yizhang@marvell.com> | |
95f1dc08 CX |
8 | */ |
9 | #include <linux/module.h> | |
10 | #include <linux/moduleparam.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/err.h> | |
13 | #include <linux/regmap.h> | |
14 | #include <linux/regulator/driver.h> | |
15 | #include <linux/regulator/machine.h> | |
16 | #include <linux/mfd/88pm80x.h> | |
17 | #include <linux/delay.h> | |
18 | #include <linux/io.h> | |
19 | #include <linux/of.h> | |
20 | #include <linux/regulator/of_regulator.h> | |
21 | ||
22 | /* LDO1 with DVC[0..3] */ | |
23 | #define PM800_LDO1_VOUT (0x08) /* VOUT1 */ | |
24 | #define PM800_LDO1_VOUT_2 (0x09) | |
25 | #define PM800_LDO1_VOUT_3 (0x0A) | |
26 | #define PM800_LDO2_VOUT (0x0B) | |
27 | #define PM800_LDO3_VOUT (0x0C) | |
28 | #define PM800_LDO4_VOUT (0x0D) | |
29 | #define PM800_LDO5_VOUT (0x0E) | |
30 | #define PM800_LDO6_VOUT (0x0F) | |
31 | #define PM800_LDO7_VOUT (0x10) | |
32 | #define PM800_LDO8_VOUT (0x11) | |
33 | #define PM800_LDO9_VOUT (0x12) | |
34 | #define PM800_LDO10_VOUT (0x13) | |
35 | #define PM800_LDO11_VOUT (0x14) | |
36 | #define PM800_LDO12_VOUT (0x15) | |
37 | #define PM800_LDO13_VOUT (0x16) | |
38 | #define PM800_LDO14_VOUT (0x17) | |
39 | #define PM800_LDO15_VOUT (0x18) | |
40 | #define PM800_LDO16_VOUT (0x19) | |
41 | #define PM800_LDO17_VOUT (0x1A) | |
42 | #define PM800_LDO18_VOUT (0x1B) | |
43 | #define PM800_LDO19_VOUT (0x1C) | |
44 | ||
45 | /* BUCK1 with DVC[0..3] */ | |
46 | #define PM800_BUCK1 (0x3C) | |
47 | #define PM800_BUCK1_1 (0x3D) | |
48 | #define PM800_BUCK1_2 (0x3E) | |
49 | #define PM800_BUCK1_3 (0x3F) | |
50 | #define PM800_BUCK2 (0x40) | |
51 | #define PM800_BUCK3 (0x41) | |
95f1dc08 CX |
52 | #define PM800_BUCK4 (0x42) |
53 | #define PM800_BUCK4_1 (0x43) | |
54 | #define PM800_BUCK4_2 (0x44) | |
55 | #define PM800_BUCK4_3 (0x45) | |
56 | #define PM800_BUCK5 (0x46) | |
57 | ||
58 | #define PM800_BUCK_ENA (0x50) | |
59 | #define PM800_LDO_ENA1_1 (0x51) | |
60 | #define PM800_LDO_ENA1_2 (0x52) | |
61 | #define PM800_LDO_ENA1_3 (0x53) | |
62 | ||
63 | #define PM800_LDO_ENA2_1 (0x56) | |
64 | #define PM800_LDO_ENA2_2 (0x57) | |
65 | #define PM800_LDO_ENA2_3 (0x58) | |
66 | ||
67 | #define PM800_BUCK1_MISC1 (0x78) | |
68 | #define PM800_BUCK3_MISC1 (0x7E) | |
69 | #define PM800_BUCK4_MISC1 (0x81) | |
70 | #define PM800_BUCK5_MISC1 (0x84) | |
71 | ||
72 | struct pm800_regulator_info { | |
73 | struct regulator_desc desc; | |
74 | int max_ua; | |
75 | }; | |
76 | ||
95f1dc08 CX |
77 | /* |
78 | * vreg - the buck regs string. | |
79 | * ereg - the string for the enable register. | |
80 | * ebit - the bit number in the enable register. | |
81 | * amax - the current | |
82 | * Buck has 2 kinds of voltage steps. It is easy to find voltage by ranges, | |
83 | * not the constant voltage table. | |
6625d9d2 | 84 | * n_volt - Number of available selectors |
95f1dc08 | 85 | */ |
fa26e4d2 | 86 | #define PM800_BUCK(match, vreg, ereg, ebit, amax, volt_ranges, n_volt) \ |
95f1dc08 CX |
87 | { \ |
88 | .desc = { \ | |
4d45c70b | 89 | .name = #vreg, \ |
fa26e4d2 VH |
90 | .of_match = of_match_ptr(#match), \ |
91 | .regulators_node = of_match_ptr("regulators"), \ | |
4d45c70b VH |
92 | .ops = &pm800_volt_range_ops, \ |
93 | .type = REGULATOR_VOLTAGE, \ | |
94 | .id = PM800_ID_##vreg, \ | |
95 | .owner = THIS_MODULE, \ | |
6625d9d2 | 96 | .n_voltages = n_volt, \ |
95f1dc08 CX |
97 | .linear_ranges = volt_ranges, \ |
98 | .n_linear_ranges = ARRAY_SIZE(volt_ranges), \ | |
99 | .vsel_reg = PM800_##vreg, \ | |
100 | .vsel_mask = 0x7f, \ | |
101 | .enable_reg = PM800_##ereg, \ | |
102 | .enable_mask = 1 << (ebit), \ | |
103 | }, \ | |
4d45c70b | 104 | .max_ua = (amax), \ |
95f1dc08 CX |
105 | } |
106 | ||
107 | /* | |
108 | * vreg - the LDO regs string | |
109 | * ereg - the string for the enable register. | |
110 | * ebit - the bit number in the enable register. | |
111 | * amax - the current | |
112 | * volt_table - the LDO voltage table | |
113 | * For all the LDOes, there are too many ranges. Using volt_table will be | |
114 | * simpler and faster. | |
115 | */ | |
fa26e4d2 | 116 | #define PM800_LDO(match, vreg, ereg, ebit, amax, ldo_volt_table) \ |
95f1dc08 CX |
117 | { \ |
118 | .desc = { \ | |
4d45c70b | 119 | .name = #vreg, \ |
fa26e4d2 VH |
120 | .of_match = of_match_ptr(#match), \ |
121 | .regulators_node = of_match_ptr("regulators"), \ | |
4d45c70b VH |
122 | .ops = &pm800_volt_table_ops, \ |
123 | .type = REGULATOR_VOLTAGE, \ | |
124 | .id = PM800_ID_##vreg, \ | |
125 | .owner = THIS_MODULE, \ | |
126 | .n_voltages = ARRAY_SIZE(ldo_volt_table), \ | |
127 | .vsel_reg = PM800_##vreg##_VOUT, \ | |
bf1fc23c | 128 | .vsel_mask = 0xf, \ |
4d45c70b VH |
129 | .enable_reg = PM800_##ereg, \ |
130 | .enable_mask = 1 << (ebit), \ | |
131 | .volt_table = ldo_volt_table, \ | |
95f1dc08 | 132 | }, \ |
4d45c70b | 133 | .max_ua = (amax), \ |
95f1dc08 CX |
134 | } |
135 | ||
136 | /* Ranges are sorted in ascending order. */ | |
60ab7f41 | 137 | static const struct linear_range buck1_volt_range[] = { |
8828bae4 AL |
138 | REGULATOR_LINEAR_RANGE(600000, 0, 0x4f, 12500), |
139 | REGULATOR_LINEAR_RANGE(1600000, 0x50, 0x54, 50000), | |
95f1dc08 CX |
140 | }; |
141 | ||
142 | /* BUCK 2~5 have same ranges. */ | |
60ab7f41 | 143 | static const struct linear_range buck2_5_volt_range[] = { |
8828bae4 AL |
144 | REGULATOR_LINEAR_RANGE(600000, 0, 0x4f, 12500), |
145 | REGULATOR_LINEAR_RANGE(1600000, 0x50, 0x72, 50000), | |
95f1dc08 CX |
146 | }; |
147 | ||
148 | static const unsigned int ldo1_volt_table[] = { | |
149 | 600000, 650000, 700000, 750000, 800000, 850000, 900000, 950000, | |
150 | 1000000, 1050000, 1100000, 1150000, 1200000, 1300000, 1400000, 1500000, | |
151 | }; | |
152 | ||
153 | static const unsigned int ldo2_volt_table[] = { | |
154 | 1700000, 1800000, 1900000, 2000000, 2100000, 2500000, 2700000, 2800000, | |
155 | }; | |
156 | ||
157 | /* LDO 3~17 have same voltage table. */ | |
158 | static const unsigned int ldo3_17_volt_table[] = { | |
159 | 1200000, 1250000, 1700000, 1800000, 1850000, 1900000, 2500000, 2600000, | |
160 | 2700000, 2750000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000, | |
161 | }; | |
162 | ||
163 | /* LDO 18~19 have same voltage table. */ | |
164 | static const unsigned int ldo18_19_volt_table[] = { | |
165 | 1700000, 1800000, 1900000, 2500000, 2800000, 2900000, 3100000, 3300000, | |
166 | }; | |
167 | ||
168 | static int pm800_get_current_limit(struct regulator_dev *rdev) | |
169 | { | |
170 | struct pm800_regulator_info *info = rdev_get_drvdata(rdev); | |
171 | ||
172 | return info->max_ua; | |
173 | } | |
174 | ||
b9f19321 | 175 | static const struct regulator_ops pm800_volt_range_ops = { |
4d45c70b VH |
176 | .list_voltage = regulator_list_voltage_linear_range, |
177 | .map_voltage = regulator_map_voltage_linear_range, | |
178 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
179 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
180 | .enable = regulator_enable_regmap, | |
181 | .disable = regulator_disable_regmap, | |
182 | .is_enabled = regulator_is_enabled_regmap, | |
183 | .get_current_limit = pm800_get_current_limit, | |
95f1dc08 CX |
184 | }; |
185 | ||
b9f19321 | 186 | static const struct regulator_ops pm800_volt_table_ops = { |
4d45c70b VH |
187 | .list_voltage = regulator_list_voltage_table, |
188 | .map_voltage = regulator_map_voltage_iterate, | |
189 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
190 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
191 | .enable = regulator_enable_regmap, | |
192 | .disable = regulator_disable_regmap, | |
193 | .is_enabled = regulator_is_enabled_regmap, | |
194 | .get_current_limit = pm800_get_current_limit, | |
95f1dc08 CX |
195 | }; |
196 | ||
197 | /* The array is indexed by id(PM800_ID_XXX) */ | |
198 | static struct pm800_regulator_info pm800_regulator_info[] = { | |
fa26e4d2 VH |
199 | PM800_BUCK(buck1, BUCK1, BUCK_ENA, 0, 3000000, buck1_volt_range, 0x55), |
200 | PM800_BUCK(buck2, BUCK2, BUCK_ENA, 1, 1200000, buck2_5_volt_range, 0x73), | |
201 | PM800_BUCK(buck3, BUCK3, BUCK_ENA, 2, 1200000, buck2_5_volt_range, 0x73), | |
202 | PM800_BUCK(buck4, BUCK4, BUCK_ENA, 3, 1200000, buck2_5_volt_range, 0x73), | |
203 | PM800_BUCK(buck5, BUCK5, BUCK_ENA, 4, 1200000, buck2_5_volt_range, 0x73), | |
204 | ||
205 | PM800_LDO(ldo1, LDO1, LDO_ENA1_1, 0, 200000, ldo1_volt_table), | |
206 | PM800_LDO(ldo2, LDO2, LDO_ENA1_1, 1, 10000, ldo2_volt_table), | |
207 | PM800_LDO(ldo3, LDO3, LDO_ENA1_1, 2, 300000, ldo3_17_volt_table), | |
208 | PM800_LDO(ldo4, LDO4, LDO_ENA1_1, 3, 300000, ldo3_17_volt_table), | |
209 | PM800_LDO(ldo5, LDO5, LDO_ENA1_1, 4, 300000, ldo3_17_volt_table), | |
210 | PM800_LDO(ldo6, LDO6, LDO_ENA1_1, 5, 300000, ldo3_17_volt_table), | |
211 | PM800_LDO(ldo7, LDO7, LDO_ENA1_1, 6, 300000, ldo3_17_volt_table), | |
212 | PM800_LDO(ldo8, LDO8, LDO_ENA1_1, 7, 300000, ldo3_17_volt_table), | |
213 | PM800_LDO(ldo9, LDO9, LDO_ENA1_2, 0, 300000, ldo3_17_volt_table), | |
214 | PM800_LDO(ldo10, LDO10, LDO_ENA1_2, 1, 300000, ldo3_17_volt_table), | |
215 | PM800_LDO(ldo11, LDO11, LDO_ENA1_2, 2, 300000, ldo3_17_volt_table), | |
216 | PM800_LDO(ldo12, LDO12, LDO_ENA1_2, 3, 300000, ldo3_17_volt_table), | |
217 | PM800_LDO(ldo13, LDO13, LDO_ENA1_2, 4, 300000, ldo3_17_volt_table), | |
218 | PM800_LDO(ldo14, LDO14, LDO_ENA1_2, 5, 300000, ldo3_17_volt_table), | |
219 | PM800_LDO(ldo15, LDO15, LDO_ENA1_2, 6, 300000, ldo3_17_volt_table), | |
220 | PM800_LDO(ldo16, LDO16, LDO_ENA1_2, 7, 300000, ldo3_17_volt_table), | |
221 | PM800_LDO(ldo17, LDO17, LDO_ENA1_3, 0, 300000, ldo3_17_volt_table), | |
222 | PM800_LDO(ldo18, LDO18, LDO_ENA1_3, 1, 200000, ldo18_19_volt_table), | |
223 | PM800_LDO(ldo19, LDO19, LDO_ENA1_3, 2, 200000, ldo18_19_volt_table), | |
95f1dc08 CX |
224 | }; |
225 | ||
95f1dc08 CX |
226 | static int pm800_regulator_probe(struct platform_device *pdev) |
227 | { | |
228 | struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent); | |
dff91d0b | 229 | struct pm80x_platform_data *pdata = dev_get_platdata(pdev->dev.parent); |
95f1dc08 CX |
230 | struct regulator_config config = { }; |
231 | struct regulator_init_data *init_data; | |
232 | int i, ret; | |
233 | ||
fa26e4d2 | 234 | if (pdata && pdata->num_regulators) { |
95f1dc08 | 235 | unsigned int count = 0; |
19c6b544 AL |
236 | |
237 | /* Check whether num_regulator is valid. */ | |
720c0273 | 238 | for (i = 0; i < ARRAY_SIZE(pdata->regulators); i++) { |
19c6b544 AL |
239 | if (pdata->regulators[i]) |
240 | count++; | |
241 | } | |
95f1dc08 CX |
242 | if (count != pdata->num_regulators) |
243 | return -EINVAL; | |
95f1dc08 CX |
244 | } |
245 | ||
fa26e4d2 | 246 | config.dev = chip->dev; |
6cabb8b7 | 247 | config.regmap = chip->subchip->regmap_power; |
95f1dc08 | 248 | for (i = 0; i < PM800_ID_RG_MAX; i++) { |
a07d94a5 VH |
249 | struct regulator_dev *regulator; |
250 | ||
fa26e4d2 | 251 | if (pdata && pdata->num_regulators) { |
95f1dc08 | 252 | init_data = pdata->regulators[i]; |
fa26e4d2 VH |
253 | if (!init_data) |
254 | continue; | |
255 | ||
256 | config.init_data = init_data; | |
257 | } | |
258 | ||
259 | config.driver_data = &pm800_regulator_info[i]; | |
95f1dc08 | 260 | |
a07d94a5 | 261 | regulator = devm_regulator_register(&pdev->dev, |
fa26e4d2 | 262 | &pm800_regulator_info[i].desc, &config); |
a07d94a5 VH |
263 | if (IS_ERR(regulator)) { |
264 | ret = PTR_ERR(regulator); | |
95f1dc08 | 265 | dev_err(&pdev->dev, "Failed to register %s\n", |
fa26e4d2 | 266 | pm800_regulator_info[i].desc.name); |
95f1dc08 CX |
267 | return ret; |
268 | } | |
269 | } | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
95f1dc08 CX |
274 | static struct platform_driver pm800_regulator_driver = { |
275 | .driver = { | |
276 | .name = "88pm80x-regulator", | |
95f1dc08 CX |
277 | }, |
278 | .probe = pm800_regulator_probe, | |
95f1dc08 CX |
279 | }; |
280 | ||
281 | module_platform_driver(pm800_regulator_driver); | |
282 | ||
283 | MODULE_LICENSE("GPL"); | |
284 | MODULE_AUTHOR("Joseph(Yossi) Hanin <yhanin@marvell.com>"); | |
285 | MODULE_DESCRIPTION("Regulator Driver for Marvell 88PM800 PMIC"); | |
286 | MODULE_ALIAS("platform:88pm800-regulator"); |