blob: 9fd379732d1868ba8e686d1fd4edb1ace9beb35b [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)
Chao Xie95f1dc02013-07-17 23:07:03 -040055#define PM800_BUCK4 (0x42)
56#define PM800_BUCK4_1 (0x43)
57#define PM800_BUCK4_2 (0x44)
58#define PM800_BUCK4_3 (0x45)
59#define PM800_BUCK5 (0x46)
60
61#define PM800_BUCK_ENA (0x50)
62#define PM800_LDO_ENA1_1 (0x51)
63#define PM800_LDO_ENA1_2 (0x52)
64#define PM800_LDO_ENA1_3 (0x53)
65
66#define PM800_LDO_ENA2_1 (0x56)
67#define PM800_LDO_ENA2_2 (0x57)
68#define PM800_LDO_ENA2_3 (0x58)
69
70#define PM800_BUCK1_MISC1 (0x78)
71#define PM800_BUCK3_MISC1 (0x7E)
72#define PM800_BUCK4_MISC1 (0x81)
73#define PM800_BUCK5_MISC1 (0x84)
74
75struct pm800_regulator_info {
76 struct regulator_desc desc;
77 int max_ua;
78};
79
Chao Xie95f1dc02013-07-17 23:07:03 -040080/*
81 * vreg - the buck regs string.
82 * ereg - the string for the enable register.
83 * ebit - the bit number in the enable register.
84 * amax - the current
85 * Buck has 2 kinds of voltage steps. It is easy to find voltage by ranges,
86 * not the constant voltage table.
Axel Lin6625d9d2013-07-19 13:06:22 +080087 * n_volt - Number of available selectors
Chao Xie95f1dc02013-07-17 23:07:03 -040088 */
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +053089#define PM800_BUCK(match, vreg, ereg, ebit, amax, volt_ranges, n_volt) \
Chao Xie95f1dc02013-07-17 23:07:03 -040090{ \
91 .desc = { \
Vaibhav Hiremath4d45c702015-07-16 23:46:54 +053092 .name = #vreg, \
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +053093 .of_match = of_match_ptr(#match), \
94 .regulators_node = of_match_ptr("regulators"), \
Vaibhav Hiremath4d45c702015-07-16 23:46:54 +053095 .ops = &pm800_volt_range_ops, \
96 .type = REGULATOR_VOLTAGE, \
97 .id = PM800_ID_##vreg, \
98 .owner = THIS_MODULE, \
Axel Lin6625d9d2013-07-19 13:06:22 +080099 .n_voltages = n_volt, \
Chao Xie95f1dc02013-07-17 23:07:03 -0400100 .linear_ranges = volt_ranges, \
101 .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
102 .vsel_reg = PM800_##vreg, \
103 .vsel_mask = 0x7f, \
104 .enable_reg = PM800_##ereg, \
105 .enable_mask = 1 << (ebit), \
106 }, \
Vaibhav Hiremath4d45c702015-07-16 23:46:54 +0530107 .max_ua = (amax), \
Chao Xie95f1dc02013-07-17 23:07:03 -0400108}
109
110/*
111 * vreg - the LDO regs string
112 * ereg - the string for the enable register.
113 * ebit - the bit number in the enable register.
114 * amax - the current
115 * volt_table - the LDO voltage table
116 * For all the LDOes, there are too many ranges. Using volt_table will be
117 * simpler and faster.
118 */
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +0530119#define PM800_LDO(match, vreg, ereg, ebit, amax, ldo_volt_table) \
Chao Xie95f1dc02013-07-17 23:07:03 -0400120{ \
121 .desc = { \
Vaibhav Hiremath4d45c702015-07-16 23:46:54 +0530122 .name = #vreg, \
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +0530123 .of_match = of_match_ptr(#match), \
124 .regulators_node = of_match_ptr("regulators"), \
Vaibhav Hiremath4d45c702015-07-16 23:46:54 +0530125 .ops = &pm800_volt_table_ops, \
126 .type = REGULATOR_VOLTAGE, \
127 .id = PM800_ID_##vreg, \
128 .owner = THIS_MODULE, \
129 .n_voltages = ARRAY_SIZE(ldo_volt_table), \
130 .vsel_reg = PM800_##vreg##_VOUT, \
Mark Brownbf1fc232015-07-17 12:42:48 +0100131 .vsel_mask = 0xf, \
Vaibhav Hiremath4d45c702015-07-16 23:46:54 +0530132 .enable_reg = PM800_##ereg, \
133 .enable_mask = 1 << (ebit), \
134 .volt_table = ldo_volt_table, \
Chao Xie95f1dc02013-07-17 23:07:03 -0400135 }, \
Vaibhav Hiremath4d45c702015-07-16 23:46:54 +0530136 .max_ua = (amax), \
Chao Xie95f1dc02013-07-17 23:07:03 -0400137}
138
139/* Ranges are sorted in ascending order. */
140static const struct regulator_linear_range buck1_volt_range[] = {
Axel Lin8828bae2013-10-11 09:32:18 +0800141 REGULATOR_LINEAR_RANGE(600000, 0, 0x4f, 12500),
142 REGULATOR_LINEAR_RANGE(1600000, 0x50, 0x54, 50000),
Chao Xie95f1dc02013-07-17 23:07:03 -0400143};
144
145/* BUCK 2~5 have same ranges. */
146static const struct regulator_linear_range buck2_5_volt_range[] = {
Axel Lin8828bae2013-10-11 09:32:18 +0800147 REGULATOR_LINEAR_RANGE(600000, 0, 0x4f, 12500),
148 REGULATOR_LINEAR_RANGE(1600000, 0x50, 0x72, 50000),
Chao Xie95f1dc02013-07-17 23:07:03 -0400149};
150
151static const unsigned int ldo1_volt_table[] = {
152 600000, 650000, 700000, 750000, 800000, 850000, 900000, 950000,
153 1000000, 1050000, 1100000, 1150000, 1200000, 1300000, 1400000, 1500000,
154};
155
156static const unsigned int ldo2_volt_table[] = {
157 1700000, 1800000, 1900000, 2000000, 2100000, 2500000, 2700000, 2800000,
158};
159
160/* LDO 3~17 have same voltage table. */
161static const unsigned int ldo3_17_volt_table[] = {
162 1200000, 1250000, 1700000, 1800000, 1850000, 1900000, 2500000, 2600000,
163 2700000, 2750000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
164};
165
166/* LDO 18~19 have same voltage table. */
167static const unsigned int ldo18_19_volt_table[] = {
168 1700000, 1800000, 1900000, 2500000, 2800000, 2900000, 3100000, 3300000,
169};
170
171static int pm800_get_current_limit(struct regulator_dev *rdev)
172{
173 struct pm800_regulator_info *info = rdev_get_drvdata(rdev);
174
175 return info->max_ua;
176}
177
Bhumika Goyalb9f19322017-01-28 18:53:18 +0530178static const struct regulator_ops pm800_volt_range_ops = {
Vaibhav Hiremath4d45c702015-07-16 23:46:54 +0530179 .list_voltage = regulator_list_voltage_linear_range,
180 .map_voltage = regulator_map_voltage_linear_range,
181 .set_voltage_sel = regulator_set_voltage_sel_regmap,
182 .get_voltage_sel = regulator_get_voltage_sel_regmap,
183 .enable = regulator_enable_regmap,
184 .disable = regulator_disable_regmap,
185 .is_enabled = regulator_is_enabled_regmap,
186 .get_current_limit = pm800_get_current_limit,
Chao Xie95f1dc02013-07-17 23:07:03 -0400187};
188
Bhumika Goyalb9f19322017-01-28 18:53:18 +0530189static const struct regulator_ops pm800_volt_table_ops = {
Vaibhav Hiremath4d45c702015-07-16 23:46:54 +0530190 .list_voltage = regulator_list_voltage_table,
191 .map_voltage = regulator_map_voltage_iterate,
192 .set_voltage_sel = regulator_set_voltage_sel_regmap,
193 .get_voltage_sel = regulator_get_voltage_sel_regmap,
194 .enable = regulator_enable_regmap,
195 .disable = regulator_disable_regmap,
196 .is_enabled = regulator_is_enabled_regmap,
197 .get_current_limit = pm800_get_current_limit,
Chao Xie95f1dc02013-07-17 23:07:03 -0400198};
199
200/* The array is indexed by id(PM800_ID_XXX) */
201static struct pm800_regulator_info pm800_regulator_info[] = {
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +0530202 PM800_BUCK(buck1, BUCK1, BUCK_ENA, 0, 3000000, buck1_volt_range, 0x55),
203 PM800_BUCK(buck2, BUCK2, BUCK_ENA, 1, 1200000, buck2_5_volt_range, 0x73),
204 PM800_BUCK(buck3, BUCK3, BUCK_ENA, 2, 1200000, buck2_5_volt_range, 0x73),
205 PM800_BUCK(buck4, BUCK4, BUCK_ENA, 3, 1200000, buck2_5_volt_range, 0x73),
206 PM800_BUCK(buck5, BUCK5, BUCK_ENA, 4, 1200000, buck2_5_volt_range, 0x73),
Chao Xie95f1dc02013-07-17 23:07:03 -0400207
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +0530208 PM800_LDO(ldo1, LDO1, LDO_ENA1_1, 0, 200000, ldo1_volt_table),
209 PM800_LDO(ldo2, LDO2, LDO_ENA1_1, 1, 10000, ldo2_volt_table),
210 PM800_LDO(ldo3, LDO3, LDO_ENA1_1, 2, 300000, ldo3_17_volt_table),
211 PM800_LDO(ldo4, LDO4, LDO_ENA1_1, 3, 300000, ldo3_17_volt_table),
212 PM800_LDO(ldo5, LDO5, LDO_ENA1_1, 4, 300000, ldo3_17_volt_table),
213 PM800_LDO(ldo6, LDO6, LDO_ENA1_1, 5, 300000, ldo3_17_volt_table),
214 PM800_LDO(ldo7, LDO7, LDO_ENA1_1, 6, 300000, ldo3_17_volt_table),
215 PM800_LDO(ldo8, LDO8, LDO_ENA1_1, 7, 300000, ldo3_17_volt_table),
216 PM800_LDO(ldo9, LDO9, LDO_ENA1_2, 0, 300000, ldo3_17_volt_table),
217 PM800_LDO(ldo10, LDO10, LDO_ENA1_2, 1, 300000, ldo3_17_volt_table),
218 PM800_LDO(ldo11, LDO11, LDO_ENA1_2, 2, 300000, ldo3_17_volt_table),
219 PM800_LDO(ldo12, LDO12, LDO_ENA1_2, 3, 300000, ldo3_17_volt_table),
220 PM800_LDO(ldo13, LDO13, LDO_ENA1_2, 4, 300000, ldo3_17_volt_table),
221 PM800_LDO(ldo14, LDO14, LDO_ENA1_2, 5, 300000, ldo3_17_volt_table),
222 PM800_LDO(ldo15, LDO15, LDO_ENA1_2, 6, 300000, ldo3_17_volt_table),
223 PM800_LDO(ldo16, LDO16, LDO_ENA1_2, 7, 300000, ldo3_17_volt_table),
224 PM800_LDO(ldo17, LDO17, LDO_ENA1_3, 0, 300000, ldo3_17_volt_table),
225 PM800_LDO(ldo18, LDO18, LDO_ENA1_3, 1, 200000, ldo18_19_volt_table),
226 PM800_LDO(ldo19, LDO19, LDO_ENA1_3, 2, 200000, ldo18_19_volt_table),
Chao Xie95f1dc02013-07-17 23:07:03 -0400227};
228
Chao Xie95f1dc02013-07-17 23:07:03 -0400229static int pm800_regulator_probe(struct platform_device *pdev)
230{
231 struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900232 struct pm80x_platform_data *pdata = dev_get_platdata(pdev->dev.parent);
Chao Xie95f1dc02013-07-17 23:07:03 -0400233 struct regulator_config config = { };
234 struct regulator_init_data *init_data;
235 int i, ret;
236
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +0530237 if (pdata && pdata->num_regulators) {
Chao Xie95f1dc02013-07-17 23:07:03 -0400238 unsigned int count = 0;
Axel Lin19c6b542013-08-04 09:23:28 +0800239
240 /* Check whether num_regulator is valid. */
Dan Carpenter720c0272013-08-14 19:14:03 +0300241 for (i = 0; i < ARRAY_SIZE(pdata->regulators); i++) {
Axel Lin19c6b542013-08-04 09:23:28 +0800242 if (pdata->regulators[i])
243 count++;
244 }
Chao Xie95f1dc02013-07-17 23:07:03 -0400245 if (count != pdata->num_regulators)
246 return -EINVAL;
Chao Xie95f1dc02013-07-17 23:07:03 -0400247 }
248
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +0530249 config.dev = chip->dev;
Axel Lin6cabb8b2019-03-14 19:55:19 +0800250 config.regmap = chip->subchip->regmap_power;
Chao Xie95f1dc02013-07-17 23:07:03 -0400251 for (i = 0; i < PM800_ID_RG_MAX; i++) {
Vaibhav Hirematha07d94a2015-07-16 23:46:55 +0530252 struct regulator_dev *regulator;
253
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +0530254 if (pdata && pdata->num_regulators) {
Chao Xie95f1dc02013-07-17 23:07:03 -0400255 init_data = pdata->regulators[i];
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +0530256 if (!init_data)
257 continue;
258
259 config.init_data = init_data;
260 }
261
262 config.driver_data = &pm800_regulator_info[i];
Chao Xie95f1dc02013-07-17 23:07:03 -0400263
Vaibhav Hirematha07d94a2015-07-16 23:46:55 +0530264 regulator = devm_regulator_register(&pdev->dev,
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +0530265 &pm800_regulator_info[i].desc, &config);
Vaibhav Hirematha07d94a2015-07-16 23:46:55 +0530266 if (IS_ERR(regulator)) {
267 ret = PTR_ERR(regulator);
Chao Xie95f1dc02013-07-17 23:07:03 -0400268 dev_err(&pdev->dev, "Failed to register %s\n",
Vaibhav Hiremathfa26e4d2015-07-16 23:46:56 +0530269 pm800_regulator_info[i].desc.name);
Chao Xie95f1dc02013-07-17 23:07:03 -0400270 return ret;
271 }
272 }
273
274 return 0;
275}
276
Chao Xie95f1dc02013-07-17 23:07:03 -0400277static struct platform_driver pm800_regulator_driver = {
278 .driver = {
279 .name = "88pm80x-regulator",
Chao Xie95f1dc02013-07-17 23:07:03 -0400280 },
281 .probe = pm800_regulator_probe,
Chao Xie95f1dc02013-07-17 23:07:03 -0400282};
283
284module_platform_driver(pm800_regulator_driver);
285
286MODULE_LICENSE("GPL");
287MODULE_AUTHOR("Joseph(Yossi) Hanin <yhanin@marvell.com>");
288MODULE_DESCRIPTION("Regulator Driver for Marvell 88PM800 PMIC");
289MODULE_ALIAS("platform:88pm800-regulator");