blob: 3459f60dcfd1c7dfc67aa641edd49f697ffb8b94 [file] [log] [blame]
Chao Xie95f1dc02013-07-17 23:07:03 -04001/*
2 * Regulators driver for Marvell 88PM800
3 *
4 * Copyright (C) 2012 Marvell International Ltd.
5 * Joseph(Yossi) Hanin <yhanin@marvell.com>
6 * Yi Zhang <yizhang@marvell.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/init.h>
15#include <linux/err.h>
16#include <linux/regmap.h>
17#include <linux/regulator/driver.h>
18#include <linux/regulator/machine.h>
19#include <linux/mfd/88pm80x.h>
20#include <linux/delay.h>
21#include <linux/io.h>
22#include <linux/of.h>
23#include <linux/regulator/of_regulator.h>
24
25/* LDO1 with DVC[0..3] */
26#define PM800_LDO1_VOUT (0x08) /* VOUT1 */
27#define PM800_LDO1_VOUT_2 (0x09)
28#define PM800_LDO1_VOUT_3 (0x0A)
29#define PM800_LDO2_VOUT (0x0B)
30#define PM800_LDO3_VOUT (0x0C)
31#define PM800_LDO4_VOUT (0x0D)
32#define PM800_LDO5_VOUT (0x0E)
33#define PM800_LDO6_VOUT (0x0F)
34#define PM800_LDO7_VOUT (0x10)
35#define PM800_LDO8_VOUT (0x11)
36#define PM800_LDO9_VOUT (0x12)
37#define PM800_LDO10_VOUT (0x13)
38#define PM800_LDO11_VOUT (0x14)
39#define PM800_LDO12_VOUT (0x15)
40#define PM800_LDO13_VOUT (0x16)
41#define PM800_LDO14_VOUT (0x17)
42#define PM800_LDO15_VOUT (0x18)
43#define PM800_LDO16_VOUT (0x19)
44#define PM800_LDO17_VOUT (0x1A)
45#define PM800_LDO18_VOUT (0x1B)
46#define PM800_LDO19_VOUT (0x1C)
47
48/* BUCK1 with DVC[0..3] */
49#define PM800_BUCK1 (0x3C)
50#define PM800_BUCK1_1 (0x3D)
51#define PM800_BUCK1_2 (0x3E)
52#define PM800_BUCK1_3 (0x3F)
53#define PM800_BUCK2 (0x40)
54#define PM800_BUCK3 (0x41)
55#define PM800_BUCK3 (0x41)
56#define PM800_BUCK4 (0x42)
57#define PM800_BUCK4_1 (0x43)
58#define PM800_BUCK4_2 (0x44)
59#define PM800_BUCK4_3 (0x45)
60#define PM800_BUCK5 (0x46)
61
62#define PM800_BUCK_ENA (0x50)
63#define PM800_LDO_ENA1_1 (0x51)
64#define PM800_LDO_ENA1_2 (0x52)
65#define PM800_LDO_ENA1_3 (0x53)
66
67#define PM800_LDO_ENA2_1 (0x56)
68#define PM800_LDO_ENA2_2 (0x57)
69#define PM800_LDO_ENA2_3 (0x58)
70
71#define PM800_BUCK1_MISC1 (0x78)
72#define PM800_BUCK3_MISC1 (0x7E)
73#define PM800_BUCK4_MISC1 (0x81)
74#define PM800_BUCK5_MISC1 (0x84)
75
76struct pm800_regulator_info {
77 struct regulator_desc desc;
78 int max_ua;
79};
80
81struct pm800_regulators {
82 struct regulator_dev *regulators[PM800_ID_RG_MAX];
83 struct pm80x_chip *chip;
84 struct regmap *map;
85};
86
87/*
88 * vreg - the buck regs string.
89 * ereg - the string for the enable register.
90 * ebit - the bit number in the enable register.
91 * amax - the current
92 * Buck has 2 kinds of voltage steps. It is easy to find voltage by ranges,
93 * not the constant voltage table.
Axel Lin6625d9d2013-07-19 13:06:22 +080094 * n_volt - Number of available selectors
Chao Xie95f1dc02013-07-17 23:07:03 -040095 */
Axel Lin6625d9d2013-07-19 13:06:22 +080096#define PM800_BUCK(vreg, ereg, ebit, amax, volt_ranges, n_volt) \
Chao Xie95f1dc02013-07-17 23:07:03 -040097{ \
98 .desc = { \
99 .name = #vreg, \
100 .ops = &pm800_volt_range_ops, \
101 .type = REGULATOR_VOLTAGE, \
102 .id = PM800_ID_##vreg, \
103 .owner = THIS_MODULE, \
Axel Lin6625d9d2013-07-19 13:06:22 +0800104 .n_voltages = n_volt, \
Chao Xie95f1dc02013-07-17 23:07:03 -0400105 .linear_ranges = volt_ranges, \
106 .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
107 .vsel_reg = PM800_##vreg, \
108 .vsel_mask = 0x7f, \
109 .enable_reg = PM800_##ereg, \
110 .enable_mask = 1 << (ebit), \
111 }, \
112 .max_ua = (amax), \
113}
114
115/*
116 * vreg - the LDO regs string
117 * ereg - the string for the enable register.
118 * ebit - the bit number in the enable register.
119 * amax - the current
120 * volt_table - the LDO voltage table
121 * For all the LDOes, there are too many ranges. Using volt_table will be
122 * simpler and faster.
123 */
124#define PM800_LDO(vreg, ereg, ebit, amax, ldo_volt_table) \
125{ \
126 .desc = { \
127 .name = #vreg, \
128 .ops = &pm800_volt_table_ops, \
129 .type = REGULATOR_VOLTAGE, \
130 .id = PM800_ID_##vreg, \
131 .owner = THIS_MODULE, \
132 .n_voltages = ARRAY_SIZE(ldo_volt_table), \
133 .vsel_reg = PM800_##vreg##_VOUT, \
134 .vsel_mask = 0x1f, \
135 .enable_reg = PM800_##ereg, \
136 .enable_mask = 1 << (ebit), \
137 .volt_table = ldo_volt_table, \
138 }, \
139 .max_ua = (amax), \
140}
141
142/* Ranges are sorted in ascending order. */
143static const struct regulator_linear_range buck1_volt_range[] = {
144 { .min_uV = 600000, .max_uV = 1587500, .min_sel = 0, .max_sel = 0x4f,
145 .uV_step = 12500 },
146 { .min_uV = 1600000, .max_uV = 1800000, .min_sel = 0x50,
147 .max_sel = 0x54, .uV_step = 50000 },
148};
149
150/* BUCK 2~5 have same ranges. */
151static const struct regulator_linear_range buck2_5_volt_range[] = {
152 { .min_uV = 600000, .max_uV = 1587500, .min_sel = 0, .max_sel = 0x4f,
153 .uV_step = 12500 },
154 { .min_uV = 1600000, .max_uV = 3300000, .min_sel = 0x50,
155 .max_sel = 0x72, .uV_step = 50000 },
156};
157
158static const unsigned int ldo1_volt_table[] = {
159 600000, 650000, 700000, 750000, 800000, 850000, 900000, 950000,
160 1000000, 1050000, 1100000, 1150000, 1200000, 1300000, 1400000, 1500000,
161};
162
163static const unsigned int ldo2_volt_table[] = {
164 1700000, 1800000, 1900000, 2000000, 2100000, 2500000, 2700000, 2800000,
165};
166
167/* LDO 3~17 have same voltage table. */
168static const unsigned int ldo3_17_volt_table[] = {
169 1200000, 1250000, 1700000, 1800000, 1850000, 1900000, 2500000, 2600000,
170 2700000, 2750000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
171};
172
173/* LDO 18~19 have same voltage table. */
174static const unsigned int ldo18_19_volt_table[] = {
175 1700000, 1800000, 1900000, 2500000, 2800000, 2900000, 3100000, 3300000,
176};
177
178static int pm800_get_current_limit(struct regulator_dev *rdev)
179{
180 struct pm800_regulator_info *info = rdev_get_drvdata(rdev);
181
182 return info->max_ua;
183}
184
185static struct regulator_ops pm800_volt_range_ops = {
186 .list_voltage = regulator_list_voltage_linear_range,
187 .map_voltage = regulator_map_voltage_linear_range,
188 .set_voltage_sel = regulator_set_voltage_sel_regmap,
189 .get_voltage_sel = regulator_get_voltage_sel_regmap,
190 .enable = regulator_enable_regmap,
191 .disable = regulator_disable_regmap,
192 .is_enabled = regulator_is_enabled_regmap,
193 .get_current_limit = pm800_get_current_limit,
194};
195
196static struct regulator_ops pm800_volt_table_ops = {
197 .list_voltage = regulator_list_voltage_table,
198 .map_voltage = regulator_map_voltage_iterate,
199 .set_voltage_sel = regulator_set_voltage_sel_regmap,
200 .get_voltage_sel = regulator_get_voltage_sel_regmap,
201 .enable = regulator_enable_regmap,
202 .disable = regulator_disable_regmap,
203 .is_enabled = regulator_is_enabled_regmap,
204 .get_current_limit = pm800_get_current_limit,
205};
206
207/* The array is indexed by id(PM800_ID_XXX) */
208static struct pm800_regulator_info pm800_regulator_info[] = {
Axel Lin6625d9d2013-07-19 13:06:22 +0800209 PM800_BUCK(BUCK1, BUCK_ENA, 0, 3000000, buck1_volt_range, 0x55),
210 PM800_BUCK(BUCK2, BUCK_ENA, 1, 1200000, buck2_5_volt_range, 0x73),
211 PM800_BUCK(BUCK3, BUCK_ENA, 2, 1200000, buck2_5_volt_range, 0x73),
212 PM800_BUCK(BUCK4, BUCK_ENA, 3, 1200000, buck2_5_volt_range, 0x73),
213 PM800_BUCK(BUCK5, BUCK_ENA, 4, 1200000, buck2_5_volt_range, 0x73),
Chao Xie95f1dc02013-07-17 23:07:03 -0400214
215 PM800_LDO(LDO1, LDO_ENA1_1, 0, 200000, ldo1_volt_table),
216 PM800_LDO(LDO2, LDO_ENA1_1, 1, 10000, ldo2_volt_table),
217 PM800_LDO(LDO3, LDO_ENA1_1, 2, 300000, ldo3_17_volt_table),
218 PM800_LDO(LDO4, LDO_ENA1_1, 3, 300000, ldo3_17_volt_table),
219 PM800_LDO(LDO5, LDO_ENA1_1, 4, 300000, ldo3_17_volt_table),
220 PM800_LDO(LDO6, LDO_ENA1_1, 5, 300000, ldo3_17_volt_table),
221 PM800_LDO(LDO7, LDO_ENA1_1, 6, 300000, ldo3_17_volt_table),
222 PM800_LDO(LDO8, LDO_ENA1_1, 7, 300000, ldo3_17_volt_table),
223 PM800_LDO(LDO9, LDO_ENA1_2, 0, 300000, ldo3_17_volt_table),
224 PM800_LDO(LDO10, LDO_ENA1_2, 1, 300000, ldo3_17_volt_table),
225 PM800_LDO(LDO11, LDO_ENA1_2, 2, 300000, ldo3_17_volt_table),
226 PM800_LDO(LDO12, LDO_ENA1_2, 3, 300000, ldo3_17_volt_table),
227 PM800_LDO(LDO13, LDO_ENA1_2, 4, 300000, ldo3_17_volt_table),
228 PM800_LDO(LDO14, LDO_ENA1_2, 5, 300000, ldo3_17_volt_table),
229 PM800_LDO(LDO15, LDO_ENA1_2, 6, 300000, ldo3_17_volt_table),
230 PM800_LDO(LDO16, LDO_ENA1_2, 7, 300000, ldo3_17_volt_table),
231 PM800_LDO(LDO17, LDO_ENA1_3, 0, 300000, ldo3_17_volt_table),
232 PM800_LDO(LDO18, LDO_ENA1_3, 1, 200000, ldo18_19_volt_table),
233 PM800_LDO(LDO19, LDO_ENA1_3, 2, 200000, ldo18_19_volt_table),
234};
235
236#define PM800_REGULATOR_OF_MATCH(_name, _id) \
237 [PM800_ID_##_id] = { \
238 .name = #_name, \
239 .driver_data = &pm800_regulator_info[PM800_ID_##_id], \
240 }
241
242static struct of_regulator_match pm800_regulator_matches[] = {
243 PM800_REGULATOR_OF_MATCH(buck1, BUCK1),
244 PM800_REGULATOR_OF_MATCH(buck2, BUCK2),
245 PM800_REGULATOR_OF_MATCH(buck3, BUCK3),
246 PM800_REGULATOR_OF_MATCH(buck4, BUCK4),
247 PM800_REGULATOR_OF_MATCH(buck5, BUCK5),
248 PM800_REGULATOR_OF_MATCH(ldo1, LDO1),
249 PM800_REGULATOR_OF_MATCH(ldo2, LDO2),
250 PM800_REGULATOR_OF_MATCH(ldo3, LDO3),
251 PM800_REGULATOR_OF_MATCH(ldo4, LDO4),
252 PM800_REGULATOR_OF_MATCH(ldo5, LDO5),
253 PM800_REGULATOR_OF_MATCH(ldo6, LDO6),
254 PM800_REGULATOR_OF_MATCH(ldo7, LDO7),
255 PM800_REGULATOR_OF_MATCH(ldo8, LDO8),
256 PM800_REGULATOR_OF_MATCH(ldo9, LDO9),
257 PM800_REGULATOR_OF_MATCH(ldo10, LDO10),
258 PM800_REGULATOR_OF_MATCH(ldo11, LDO11),
259 PM800_REGULATOR_OF_MATCH(ldo12, LDO12),
260 PM800_REGULATOR_OF_MATCH(ldo13, LDO13),
261 PM800_REGULATOR_OF_MATCH(ldo14, LDO14),
262 PM800_REGULATOR_OF_MATCH(ldo15, LDO15),
263 PM800_REGULATOR_OF_MATCH(ldo16, LDO16),
264 PM800_REGULATOR_OF_MATCH(ldo17, LDO17),
265 PM800_REGULATOR_OF_MATCH(ldo18, LDO18),
266 PM800_REGULATOR_OF_MATCH(ldo19, LDO19),
267};
268
269static int pm800_regulator_dt_init(struct platform_device *pdev)
270{
271 struct device_node *np = pdev->dev.of_node;
272 int ret;
273
274 ret = of_regulator_match(&pdev->dev, np,
275 pm800_regulator_matches,
276 ARRAY_SIZE(pm800_regulator_matches));
277 if (ret < 0)
278 return ret;
279
280 return 0;
281}
282
283static int pm800_regulator_probe(struct platform_device *pdev)
284{
285 struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900286 struct pm80x_platform_data *pdata = dev_get_platdata(pdev->dev.parent);
Chao Xie95f1dc02013-07-17 23:07:03 -0400287 struct pm800_regulators *pm800_data;
288 struct pm800_regulator_info *info;
289 struct regulator_config config = { };
290 struct regulator_init_data *init_data;
291 int i, ret;
292
293 if (!pdata || pdata->num_regulators == 0) {
294 if (IS_ENABLED(CONFIG_OF)) {
295 ret = pm800_regulator_dt_init(pdev);
296 if (ret)
297 return ret;
298 } else {
299 return -ENODEV;
300 }
301 } else if (pdata->num_regulators) {
Chao Xie95f1dc02013-07-17 23:07:03 -0400302 unsigned int count = 0;
Axel Lin19c6b542013-08-04 09:23:28 +0800303
304 /* Check whether num_regulator is valid. */
Dan Carpenter720c0272013-08-14 19:14:03 +0300305 for (i = 0; i < ARRAY_SIZE(pdata->regulators); i++) {
Axel Lin19c6b542013-08-04 09:23:28 +0800306 if (pdata->regulators[i])
307 count++;
308 }
Chao Xie95f1dc02013-07-17 23:07:03 -0400309 if (count != pdata->num_regulators)
310 return -EINVAL;
311 } else {
312 return -EINVAL;
313 }
314
315 pm800_data = devm_kzalloc(&pdev->dev, sizeof(*pm800_data),
316 GFP_KERNEL);
317 if (!pm800_data) {
318 dev_err(&pdev->dev, "Failed to allocate pm800_regualtors");
319 return -ENOMEM;
320 }
321
322 pm800_data->map = chip->subchip->regmap_power;
323 pm800_data->chip = chip;
324
325 platform_set_drvdata(pdev, pm800_data);
326
327 for (i = 0; i < PM800_ID_RG_MAX; i++) {
328 if (!pdata || pdata->num_regulators == 0)
329 init_data = pm800_regulator_matches[i].init_data;
330 else
331 init_data = pdata->regulators[i];
332 if (!init_data)
333 continue;
334 info = pm800_regulator_matches[i].driver_data;
335 config.dev = &pdev->dev;
336 config.init_data = init_data;
337 config.driver_data = info;
338 config.regmap = pm800_data->map;
Axel Line5e80f62013-07-19 09:16:55 +0800339 config.of_node = pm800_regulator_matches[i].of_node;
Chao Xie95f1dc02013-07-17 23:07:03 -0400340
341 pm800_data->regulators[i] =
342 regulator_register(&info->desc, &config);
343 if (IS_ERR(pm800_data->regulators[i])) {
344 ret = PTR_ERR(pm800_data->regulators[i]);
345 dev_err(&pdev->dev, "Failed to register %s\n",
346 info->desc.name);
347
348 while (--i >= 0)
349 regulator_unregister(pm800_data->regulators[i]);
350
351 return ret;
352 }
353 }
354
355 return 0;
356}
357
358static int pm800_regulator_remove(struct platform_device *pdev)
359{
360 struct pm800_regulators *pm800_data = platform_get_drvdata(pdev);
361 int i;
362
363 for (i = 0; i < PM800_ID_RG_MAX; i++)
364 regulator_unregister(pm800_data->regulators[i]);
365
366 return 0;
367}
368
369static struct platform_driver pm800_regulator_driver = {
370 .driver = {
371 .name = "88pm80x-regulator",
372 .owner = THIS_MODULE,
373 },
374 .probe = pm800_regulator_probe,
375 .remove = pm800_regulator_remove,
376};
377
378module_platform_driver(pm800_regulator_driver);
379
380MODULE_LICENSE("GPL");
381MODULE_AUTHOR("Joseph(Yossi) Hanin <yhanin@marvell.com>");
382MODULE_DESCRIPTION("Regulator Driver for Marvell 88PM800 PMIC");
383MODULE_ALIAS("platform:88pm800-regulator");