blob: 91b8ff8bac157de97e83974dcb4a2512ea3f0741 [file] [log] [blame]
Carlo Caionedfe7a1b2014-04-11 11:38:10 +02001/*
2 * AXP20x regulators driver.
3 *
4 * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
5 *
6 * This file is subject to the terms and conditions of the GNU General
7 * Public License. See the file "COPYING" in the main directory of this
8 * archive for more details.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_device.h>
21#include <linux/platform_device.h>
22#include <linux/regmap.h>
23#include <linux/mfd/axp20x.h>
24#include <linux/regulator/driver.h>
25#include <linux/regulator/of_regulator.h>
26
27#define AXP20X_IO_ENABLED 0x03
28#define AXP20X_IO_DISABLED 0x07
29
Chen-Yu Tsai3cb99e22015-12-22 17:08:06 +080030#define AXP22X_IO_ENABLED 0x03
31#define AXP22X_IO_DISABLED 0x04
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +080032
Carlo Caionedfe7a1b2014-04-11 11:38:10 +020033#define AXP20X_WORKMODE_DCDC2_MASK BIT(2)
34#define AXP20X_WORKMODE_DCDC3_MASK BIT(1)
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +080035#define AXP22X_WORKMODE_DCDCX_MASK(x) BIT(x)
Carlo Caionedfe7a1b2014-04-11 11:38:10 +020036
37#define AXP20X_FREQ_DCDC_MASK 0x0f
38
Hans de Goede636e2a32016-06-03 18:59:44 +020039#define AXP22X_MISC_N_VBUSEN_FUNC BIT(4)
40
Boris BREZILLON866bd952015-04-10 12:09:03 +080041#define AXP_DESC_IO(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
42 _vmask, _ereg, _emask, _enable_val, _disable_val) \
43 [_family##_##_id] = { \
Chen-Yu Tsaie0bbb382016-02-15 18:31:22 +080044 .name = (_match), \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +020045 .supply_name = (_supply), \
Chen-Yu Tsai880fe822015-01-10 00:23:43 +080046 .of_match = of_match_ptr(_match), \
47 .regulators_node = of_match_ptr("regulators"), \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +020048 .type = REGULATOR_VOLTAGE, \
Boris BREZILLON866bd952015-04-10 12:09:03 +080049 .id = _family##_##_id, \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +020050 .n_voltages = (((_max) - (_min)) / (_step) + 1), \
51 .owner = THIS_MODULE, \
52 .min_uV = (_min) * 1000, \
53 .uV_step = (_step) * 1000, \
54 .vsel_reg = (_vreg), \
55 .vsel_mask = (_vmask), \
56 .enable_reg = (_ereg), \
57 .enable_mask = (_emask), \
58 .enable_val = (_enable_val), \
59 .disable_val = (_disable_val), \
60 .ops = &axp20x_ops, \
61 }
62
Boris BREZILLON866bd952015-04-10 12:09:03 +080063#define AXP_DESC(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
64 _vmask, _ereg, _emask) \
65 [_family##_##_id] = { \
Chen-Yu Tsaie0bbb382016-02-15 18:31:22 +080066 .name = (_match), \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +020067 .supply_name = (_supply), \
Chen-Yu Tsai880fe822015-01-10 00:23:43 +080068 .of_match = of_match_ptr(_match), \
69 .regulators_node = of_match_ptr("regulators"), \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +020070 .type = REGULATOR_VOLTAGE, \
Boris BREZILLON866bd952015-04-10 12:09:03 +080071 .id = _family##_##_id, \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +020072 .n_voltages = (((_max) - (_min)) / (_step) + 1), \
73 .owner = THIS_MODULE, \
74 .min_uV = (_min) * 1000, \
75 .uV_step = (_step) * 1000, \
76 .vsel_reg = (_vreg), \
77 .vsel_mask = (_vmask), \
78 .enable_reg = (_ereg), \
79 .enable_mask = (_emask), \
80 .ops = &axp20x_ops, \
81 }
82
Chen-Yu Tsai94c39042016-02-02 18:27:37 +080083#define AXP_DESC_SW(_family, _id, _match, _supply, _ereg, _emask) \
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +080084 [_family##_##_id] = { \
Chen-Yu Tsaie0bbb382016-02-15 18:31:22 +080085 .name = (_match), \
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +080086 .supply_name = (_supply), \
87 .of_match = of_match_ptr(_match), \
88 .regulators_node = of_match_ptr("regulators"), \
89 .type = REGULATOR_VOLTAGE, \
90 .id = _family##_##_id, \
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +080091 .owner = THIS_MODULE, \
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +080092 .enable_reg = (_ereg), \
93 .enable_mask = (_emask), \
94 .ops = &axp20x_ops_sw, \
95 }
96
Boris BREZILLON866bd952015-04-10 12:09:03 +080097#define AXP_DESC_FIXED(_family, _id, _match, _supply, _volt) \
98 [_family##_##_id] = { \
Chen-Yu Tsaie0bbb382016-02-15 18:31:22 +080099 .name = (_match), \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200100 .supply_name = (_supply), \
Chen-Yu Tsai880fe822015-01-10 00:23:43 +0800101 .of_match = of_match_ptr(_match), \
102 .regulators_node = of_match_ptr("regulators"), \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200103 .type = REGULATOR_VOLTAGE, \
Boris BREZILLON866bd952015-04-10 12:09:03 +0800104 .id = _family##_##_id, \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200105 .n_voltages = 1, \
106 .owner = THIS_MODULE, \
107 .min_uV = (_volt) * 1000, \
108 .ops = &axp20x_ops_fixed \
109 }
110
Chen-Yu Tsai13d57e62016-02-02 18:27:38 +0800111#define AXP_DESC_RANGES(_family, _id, _match, _supply, _ranges, _n_voltages, \
112 _vreg, _vmask, _ereg, _emask) \
Boris BREZILLON866bd952015-04-10 12:09:03 +0800113 [_family##_##_id] = { \
Chen-Yu Tsaie0bbb382016-02-15 18:31:22 +0800114 .name = (_match), \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200115 .supply_name = (_supply), \
Chen-Yu Tsai880fe822015-01-10 00:23:43 +0800116 .of_match = of_match_ptr(_match), \
117 .regulators_node = of_match_ptr("regulators"), \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200118 .type = REGULATOR_VOLTAGE, \
Boris BREZILLON866bd952015-04-10 12:09:03 +0800119 .id = _family##_##_id, \
Chen-Yu Tsai13d57e62016-02-02 18:27:38 +0800120 .n_voltages = (_n_voltages), \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200121 .owner = THIS_MODULE, \
122 .vsel_reg = (_vreg), \
123 .vsel_mask = (_vmask), \
124 .enable_reg = (_ereg), \
125 .enable_mask = (_emask), \
Chen-Yu Tsai13d57e62016-02-02 18:27:38 +0800126 .linear_ranges = (_ranges), \
127 .n_linear_ranges = ARRAY_SIZE(_ranges), \
128 .ops = &axp20x_ops_range, \
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200129 }
130
Bhumika Goyalef306e42017-01-28 19:28:01 +0530131static const struct regulator_ops axp20x_ops_fixed = {
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200132 .list_voltage = regulator_list_voltage_linear,
133};
134
Bhumika Goyalef306e42017-01-28 19:28:01 +0530135static const struct regulator_ops axp20x_ops_range = {
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200136 .set_voltage_sel = regulator_set_voltage_sel_regmap,
137 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Chen-Yu Tsai13d57e62016-02-02 18:27:38 +0800138 .list_voltage = regulator_list_voltage_linear_range,
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200139 .enable = regulator_enable_regmap,
140 .disable = regulator_disable_regmap,
141 .is_enabled = regulator_is_enabled_regmap,
142};
143
Bhumika Goyalef306e42017-01-28 19:28:01 +0530144static const struct regulator_ops axp20x_ops = {
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200145 .set_voltage_sel = regulator_set_voltage_sel_regmap,
146 .get_voltage_sel = regulator_get_voltage_sel_regmap,
147 .list_voltage = regulator_list_voltage_linear,
148 .enable = regulator_enable_regmap,
149 .disable = regulator_disable_regmap,
150 .is_enabled = regulator_is_enabled_regmap,
151};
152
Bhumika Goyalef306e42017-01-28 19:28:01 +0530153static const struct regulator_ops axp20x_ops_sw = {
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800154 .enable = regulator_enable_regmap,
155 .disable = regulator_disable_regmap,
156 .is_enabled = regulator_is_enabled_regmap,
157};
158
Chen-Yu Tsai13d57e62016-02-02 18:27:38 +0800159static const struct regulator_linear_range axp20x_ldo4_ranges[] = {
160 REGULATOR_LINEAR_RANGE(1250000, 0x0, 0x0, 0),
161 REGULATOR_LINEAR_RANGE(1300000, 0x1, 0x8, 100000),
Maxime Ripard8d4d5c32016-04-26 16:00:51 +0200162 REGULATOR_LINEAR_RANGE(2500000, 0x9, 0x9, 0),
163 REGULATOR_LINEAR_RANGE(2700000, 0xa, 0xb, 100000),
164 REGULATOR_LINEAR_RANGE(3000000, 0xc, 0xf, 100000),
Chen-Yu Tsai13d57e62016-02-02 18:27:38 +0800165};
166
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200167static const struct regulator_desc axp20x_regulators[] = {
Boris BREZILLON866bd952015-04-10 12:09:03 +0800168 AXP_DESC(AXP20X, DCDC2, "dcdc2", "vin2", 700, 2275, 25,
169 AXP20X_DCDC2_V_OUT, 0x3f, AXP20X_PWR_OUT_CTRL, 0x10),
170 AXP_DESC(AXP20X, DCDC3, "dcdc3", "vin3", 700, 3500, 25,
171 AXP20X_DCDC3_V_OUT, 0x7f, AXP20X_PWR_OUT_CTRL, 0x02),
172 AXP_DESC_FIXED(AXP20X, LDO1, "ldo1", "acin", 1300),
173 AXP_DESC(AXP20X, LDO2, "ldo2", "ldo24in", 1800, 3300, 100,
174 AXP20X_LDO24_V_OUT, 0xf0, AXP20X_PWR_OUT_CTRL, 0x04),
175 AXP_DESC(AXP20X, LDO3, "ldo3", "ldo3in", 700, 3500, 25,
176 AXP20X_LDO3_V_OUT, 0x7f, AXP20X_PWR_OUT_CTRL, 0x40),
Chen-Yu Tsai13d57e62016-02-02 18:27:38 +0800177 AXP_DESC_RANGES(AXP20X, LDO4, "ldo4", "ldo24in", axp20x_ldo4_ranges,
178 16, AXP20X_LDO24_V_OUT, 0x0f, AXP20X_PWR_OUT_CTRL,
179 0x08),
Boris BREZILLON866bd952015-04-10 12:09:03 +0800180 AXP_DESC_IO(AXP20X, LDO5, "ldo5", "ldo5in", 1800, 3300, 100,
181 AXP20X_LDO5_V_OUT, 0xf0, AXP20X_GPIO0_CTRL, 0x07,
182 AXP20X_IO_ENABLED, AXP20X_IO_DISABLED),
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200183};
184
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800185static const struct regulator_desc axp22x_regulators[] = {
186 AXP_DESC(AXP22X, DCDC1, "dcdc1", "vin1", 1600, 3400, 100,
187 AXP22X_DCDC1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(1)),
188 AXP_DESC(AXP22X, DCDC2, "dcdc2", "vin2", 600, 1540, 20,
189 AXP22X_DCDC2_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(2)),
190 AXP_DESC(AXP22X, DCDC3, "dcdc3", "vin3", 600, 1860, 20,
191 AXP22X_DCDC3_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(3)),
192 AXP_DESC(AXP22X, DCDC4, "dcdc4", "vin4", 600, 1540, 20,
Chen-Yu Tsai6b3600b2015-09-26 21:21:12 +0800193 AXP22X_DCDC4_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(4)),
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800194 AXP_DESC(AXP22X, DCDC5, "dcdc5", "vin5", 1000, 2550, 50,
Chen-Yu Tsai6b3600b2015-09-26 21:21:12 +0800195 AXP22X_DCDC5_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(5)),
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800196 /* secondary switchable output of DCDC1 */
Chen-Yu Tsai94c39042016-02-02 18:27:37 +0800197 AXP_DESC_SW(AXP22X, DC1SW, "dc1sw", NULL, AXP22X_PWR_OUT_CTRL2,
198 BIT(7)),
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800199 /* LDO regulator internally chained to DCDC5 */
Chen-Yu Tsai7118f192015-09-30 14:39:46 +0800200 AXP_DESC(AXP22X, DC5LDO, "dc5ldo", NULL, 700, 1400, 100,
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800201 AXP22X_DC5LDO_V_OUT, 0x7, AXP22X_PWR_OUT_CTRL1, BIT(0)),
202 AXP_DESC(AXP22X, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
203 AXP22X_ALDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(6)),
204 AXP_DESC(AXP22X, ALDO2, "aldo2", "aldoin", 700, 3300, 100,
205 AXP22X_ALDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(7)),
206 AXP_DESC(AXP22X, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
207 AXP22X_ALDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(7)),
208 AXP_DESC(AXP22X, DLDO1, "dldo1", "dldoin", 700, 3300, 100,
209 AXP22X_DLDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(3)),
210 AXP_DESC(AXP22X, DLDO2, "dldo2", "dldoin", 700, 3300, 100,
211 AXP22X_DLDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(4)),
212 AXP_DESC(AXP22X, DLDO3, "dldo3", "dldoin", 700, 3300, 100,
213 AXP22X_DLDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(5)),
214 AXP_DESC(AXP22X, DLDO4, "dldo4", "dldoin", 700, 3300, 100,
215 AXP22X_DLDO4_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(6)),
216 AXP_DESC(AXP22X, ELDO1, "eldo1", "eldoin", 700, 3300, 100,
217 AXP22X_ELDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(0)),
218 AXP_DESC(AXP22X, ELDO2, "eldo2", "eldoin", 700, 3300, 100,
219 AXP22X_ELDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(1)),
220 AXP_DESC(AXP22X, ELDO3, "eldo3", "eldoin", 700, 3300, 100,
221 AXP22X_ELDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(2)),
Hans de Goedef40d4892016-04-27 20:38:44 +0200222 /* Note the datasheet only guarantees reliable operation up to
223 * 3.3V, this needs to be enforced via dts provided constraints */
224 AXP_DESC_IO(AXP22X, LDO_IO0, "ldo_io0", "ips", 700, 3800, 100,
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800225 AXP22X_LDO_IO0_V_OUT, 0x1f, AXP20X_GPIO0_CTRL, 0x07,
226 AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
Hans de Goedef40d4892016-04-27 20:38:44 +0200227 /* Note the datasheet only guarantees reliable operation up to
228 * 3.3V, this needs to be enforced via dts provided constraints */
229 AXP_DESC_IO(AXP22X, LDO_IO1, "ldo_io1", "ips", 700, 3800, 100,
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800230 AXP22X_LDO_IO1_V_OUT, 0x1f, AXP20X_GPIO1_CTRL, 0x07,
231 AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
232 AXP_DESC_FIXED(AXP22X, RTC_LDO, "rtc_ldo", "ips", 3000),
233};
234
Hans de Goede636e2a32016-06-03 18:59:44 +0200235static const struct regulator_desc axp22x_drivevbus_regulator = {
236 .name = "drivevbus",
237 .supply_name = "drivevbus",
238 .of_match = of_match_ptr("drivevbus"),
239 .regulators_node = of_match_ptr("regulators"),
240 .type = REGULATOR_VOLTAGE,
241 .owner = THIS_MODULE,
242 .enable_reg = AXP20X_VBUS_IPSOUT_MGMT,
243 .enable_mask = BIT(2),
244 .ops = &axp20x_ops_sw,
245};
246
Chen-Yu Tsaid81851c2017-09-29 11:25:09 +0800247/* DCDC ranges shared with AXP813 */
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800248static const struct regulator_linear_range axp803_dcdc234_ranges[] = {
249 REGULATOR_LINEAR_RANGE(500000, 0x0, 0x46, 10000),
250 REGULATOR_LINEAR_RANGE(1220000, 0x47, 0x4b, 20000),
251};
252
253static const struct regulator_linear_range axp803_dcdc5_ranges[] = {
254 REGULATOR_LINEAR_RANGE(800000, 0x0, 0x20, 10000),
255 REGULATOR_LINEAR_RANGE(1140000, 0x21, 0x44, 20000),
256};
257
258static const struct regulator_linear_range axp803_dcdc6_ranges[] = {
259 REGULATOR_LINEAR_RANGE(600000, 0x0, 0x32, 10000),
260 REGULATOR_LINEAR_RANGE(1120000, 0x33, 0x47, 20000),
261};
262
263/* AXP806's CLDO2 and AXP809's DLDO1 shares the same range */
264static const struct regulator_linear_range axp803_dldo2_ranges[] = {
265 REGULATOR_LINEAR_RANGE(700000, 0x0, 0x1a, 100000),
266 REGULATOR_LINEAR_RANGE(3400000, 0x1b, 0x1f, 200000),
267};
268
269static const struct regulator_desc axp803_regulators[] = {
270 AXP_DESC(AXP803, DCDC1, "dcdc1", "vin1", 1600, 3400, 100,
271 AXP803_DCDC1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(0)),
272 AXP_DESC_RANGES(AXP803, DCDC2, "dcdc2", "vin2", axp803_dcdc234_ranges,
273 76, AXP803_DCDC2_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
274 BIT(1)),
275 AXP_DESC_RANGES(AXP803, DCDC3, "dcdc3", "vin3", axp803_dcdc234_ranges,
276 76, AXP803_DCDC3_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
277 BIT(2)),
278 AXP_DESC_RANGES(AXP803, DCDC4, "dcdc4", "vin4", axp803_dcdc234_ranges,
279 76, AXP803_DCDC4_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
280 BIT(3)),
281 AXP_DESC_RANGES(AXP803, DCDC5, "dcdc5", "vin5", axp803_dcdc5_ranges,
282 68, AXP803_DCDC5_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
283 BIT(4)),
284 AXP_DESC_RANGES(AXP803, DCDC6, "dcdc6", "vin6", axp803_dcdc6_ranges,
285 72, AXP803_DCDC6_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
286 BIT(5)),
287 /* secondary switchable output of DCDC1 */
288 AXP_DESC_SW(AXP803, DC1SW, "dc1sw", NULL, AXP22X_PWR_OUT_CTRL2,
289 BIT(7)),
290 AXP_DESC(AXP803, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
291 AXP22X_ALDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(5)),
292 AXP_DESC(AXP803, ALDO2, "aldo2", "aldoin", 700, 3300, 100,
293 AXP22X_ALDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(6)),
294 AXP_DESC(AXP803, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
295 AXP22X_ALDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(7)),
296 AXP_DESC(AXP803, DLDO1, "dldo1", "dldoin", 700, 3300, 100,
297 AXP22X_DLDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(3)),
298 AXP_DESC_RANGES(AXP803, DLDO2, "dldo2", "dldoin", axp803_dldo2_ranges,
299 32, AXP22X_DLDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2,
300 BIT(4)),
301 AXP_DESC(AXP803, DLDO3, "dldo3", "dldoin", 700, 3300, 100,
302 AXP22X_DLDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(5)),
303 AXP_DESC(AXP803, DLDO4, "dldo4", "dldoin", 700, 3300, 100,
304 AXP22X_DLDO4_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(6)),
305 AXP_DESC(AXP803, ELDO1, "eldo1", "eldoin", 700, 1900, 50,
306 AXP22X_ELDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(0)),
307 AXP_DESC(AXP803, ELDO2, "eldo2", "eldoin", 700, 1900, 50,
308 AXP22X_ELDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(1)),
309 AXP_DESC(AXP803, ELDO3, "eldo3", "eldoin", 700, 1900, 50,
310 AXP22X_ELDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(2)),
311 AXP_DESC(AXP803, FLDO1, "fldo1", "fldoin", 700, 1450, 50,
312 AXP803_FLDO1_V_OUT, 0x0f, AXP22X_PWR_OUT_CTRL3, BIT(2)),
313 AXP_DESC(AXP803, FLDO2, "fldo2", "fldoin", 700, 1450, 50,
314 AXP803_FLDO2_V_OUT, 0x0f, AXP22X_PWR_OUT_CTRL3, BIT(3)),
315 AXP_DESC_IO(AXP803, LDO_IO0, "ldo-io0", "ips", 700, 3300, 100,
316 AXP22X_LDO_IO0_V_OUT, 0x1f, AXP20X_GPIO0_CTRL, 0x07,
317 AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
318 AXP_DESC_IO(AXP803, LDO_IO1, "ldo-io1", "ips", 700, 3300, 100,
319 AXP22X_LDO_IO1_V_OUT, 0x1f, AXP20X_GPIO1_CTRL, 0x07,
320 AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
321 AXP_DESC_FIXED(AXP803, RTC_LDO, "rtc-ldo", "ips", 3000),
322};
323
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800324static const struct regulator_linear_range axp806_dcdca_ranges[] = {
325 REGULATOR_LINEAR_RANGE(600000, 0x0, 0x32, 10000),
326 REGULATOR_LINEAR_RANGE(1120000, 0x33, 0x47, 20000),
327};
328
329static const struct regulator_linear_range axp806_dcdcd_ranges[] = {
330 REGULATOR_LINEAR_RANGE(600000, 0x0, 0x2d, 20000),
331 REGULATOR_LINEAR_RANGE(1600000, 0x2e, 0x3f, 100000),
332};
333
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800334static const struct regulator_desc axp806_regulators[] = {
335 AXP_DESC_RANGES(AXP806, DCDCA, "dcdca", "vina", axp806_dcdca_ranges,
336 72, AXP806_DCDCA_V_CTRL, 0x7f, AXP806_PWR_OUT_CTRL1,
337 BIT(0)),
338 AXP_DESC(AXP806, DCDCB, "dcdcb", "vinb", 1000, 2550, 50,
339 AXP806_DCDCB_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(1)),
340 AXP_DESC_RANGES(AXP806, DCDCC, "dcdcc", "vinc", axp806_dcdca_ranges,
341 72, AXP806_DCDCC_V_CTRL, 0x7f, AXP806_PWR_OUT_CTRL1,
342 BIT(2)),
343 AXP_DESC_RANGES(AXP806, DCDCD, "dcdcd", "vind", axp806_dcdcd_ranges,
344 64, AXP806_DCDCD_V_CTRL, 0x3f, AXP806_PWR_OUT_CTRL1,
345 BIT(3)),
346 AXP_DESC(AXP806, DCDCE, "dcdce", "vine", 1100, 3400, 100,
Rask Ingemann Lambertsend0e287a2017-01-21 17:11:43 +0100347 AXP806_DCDCE_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(4)),
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800348 AXP_DESC(AXP806, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
349 AXP806_ALDO1_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(5)),
350 AXP_DESC(AXP806, ALDO2, "aldo2", "aldoin", 700, 3400, 100,
351 AXP806_ALDO2_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(6)),
352 AXP_DESC(AXP806, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
353 AXP806_ALDO3_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL1, BIT(7)),
354 AXP_DESC(AXP806, BLDO1, "bldo1", "bldoin", 700, 1900, 100,
355 AXP806_BLDO1_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(0)),
356 AXP_DESC(AXP806, BLDO2, "bldo2", "bldoin", 700, 1900, 100,
357 AXP806_BLDO2_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(1)),
358 AXP_DESC(AXP806, BLDO3, "bldo3", "bldoin", 700, 1900, 100,
359 AXP806_BLDO3_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(2)),
360 AXP_DESC(AXP806, BLDO4, "bldo4", "bldoin", 700, 1900, 100,
361 AXP806_BLDO4_V_CTRL, 0x0f, AXP806_PWR_OUT_CTRL2, BIT(3)),
362 AXP_DESC(AXP806, CLDO1, "cldo1", "cldoin", 700, 3300, 100,
363 AXP806_CLDO1_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL2, BIT(4)),
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800364 AXP_DESC_RANGES(AXP806, CLDO2, "cldo2", "cldoin", axp803_dldo2_ranges,
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800365 32, AXP806_CLDO2_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL2,
366 BIT(5)),
367 AXP_DESC(AXP806, CLDO3, "cldo3", "cldoin", 700, 3300, 100,
368 AXP806_CLDO3_V_CTRL, 0x1f, AXP806_PWR_OUT_CTRL2, BIT(6)),
369 AXP_DESC_SW(AXP806, SW, "sw", "swin", AXP806_PWR_OUT_CTRL2, BIT(7)),
370};
371
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800372static const struct regulator_linear_range axp809_dcdc4_ranges[] = {
373 REGULATOR_LINEAR_RANGE(600000, 0x0, 0x2f, 20000),
374 REGULATOR_LINEAR_RANGE(1800000, 0x30, 0x38, 100000),
375};
376
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800377static const struct regulator_desc axp809_regulators[] = {
378 AXP_DESC(AXP809, DCDC1, "dcdc1", "vin1", 1600, 3400, 100,
379 AXP22X_DCDC1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(1)),
380 AXP_DESC(AXP809, DCDC2, "dcdc2", "vin2", 600, 1540, 20,
381 AXP22X_DCDC2_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(2)),
382 AXP_DESC(AXP809, DCDC3, "dcdc3", "vin3", 600, 1860, 20,
383 AXP22X_DCDC3_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1, BIT(3)),
384 AXP_DESC_RANGES(AXP809, DCDC4, "dcdc4", "vin4", axp809_dcdc4_ranges,
385 57, AXP22X_DCDC4_V_OUT, 0x3f, AXP22X_PWR_OUT_CTRL1,
386 BIT(4)),
387 AXP_DESC(AXP809, DCDC5, "dcdc5", "vin5", 1000, 2550, 50,
388 AXP22X_DCDC5_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(5)),
389 /* secondary switchable output of DCDC1 */
390 AXP_DESC_SW(AXP809, DC1SW, "dc1sw", NULL, AXP22X_PWR_OUT_CTRL2,
391 BIT(7)),
392 /* LDO regulator internally chained to DCDC5 */
393 AXP_DESC(AXP809, DC5LDO, "dc5ldo", NULL, 700, 1400, 100,
394 AXP22X_DC5LDO_V_OUT, 0x7, AXP22X_PWR_OUT_CTRL1, BIT(0)),
395 AXP_DESC(AXP809, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
396 AXP22X_ALDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(6)),
397 AXP_DESC(AXP809, ALDO2, "aldo2", "aldoin", 700, 3300, 100,
398 AXP22X_ALDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(7)),
399 AXP_DESC(AXP809, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
400 AXP22X_ALDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(5)),
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800401 AXP_DESC_RANGES(AXP809, DLDO1, "dldo1", "dldoin", axp803_dldo2_ranges,
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800402 32, AXP22X_DLDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2,
403 BIT(3)),
404 AXP_DESC(AXP809, DLDO2, "dldo2", "dldoin", 700, 3300, 100,
405 AXP22X_DLDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(4)),
406 AXP_DESC(AXP809, ELDO1, "eldo1", "eldoin", 700, 3300, 100,
407 AXP22X_ELDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(0)),
408 AXP_DESC(AXP809, ELDO2, "eldo2", "eldoin", 700, 3300, 100,
409 AXP22X_ELDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(1)),
410 AXP_DESC(AXP809, ELDO3, "eldo3", "eldoin", 700, 3300, 100,
411 AXP22X_ELDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(2)),
Chen-Yu Tsai618c8082016-11-11 11:12:43 +0800412 /*
413 * Note the datasheet only guarantees reliable operation up to
414 * 3.3V, this needs to be enforced via dts provided constraints
415 */
416 AXP_DESC_IO(AXP809, LDO_IO0, "ldo_io0", "ips", 700, 3800, 100,
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800417 AXP22X_LDO_IO0_V_OUT, 0x1f, AXP20X_GPIO0_CTRL, 0x07,
418 AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
Chen-Yu Tsai618c8082016-11-11 11:12:43 +0800419 /*
420 * Note the datasheet only guarantees reliable operation up to
421 * 3.3V, this needs to be enforced via dts provided constraints
422 */
423 AXP_DESC_IO(AXP809, LDO_IO1, "ldo_io1", "ips", 700, 3800, 100,
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800424 AXP22X_LDO_IO1_V_OUT, 0x1f, AXP20X_GPIO1_CTRL, 0x07,
425 AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
426 AXP_DESC_FIXED(AXP809, RTC_LDO, "rtc_ldo", "ips", 1800),
427 AXP_DESC_SW(AXP809, SW, "sw", "swin", AXP22X_PWR_OUT_CTRL2, BIT(6)),
428};
429
Chen-Yu Tsaid81851c2017-09-29 11:25:09 +0800430static const struct regulator_desc axp813_regulators[] = {
431 AXP_DESC(AXP813, DCDC1, "dcdc1", "vin1", 1600, 3400, 100,
432 AXP803_DCDC1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL1, BIT(0)),
433 AXP_DESC_RANGES(AXP813, DCDC2, "dcdc2", "vin2", axp803_dcdc234_ranges,
434 76, AXP803_DCDC2_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
435 BIT(1)),
436 AXP_DESC_RANGES(AXP813, DCDC3, "dcdc3", "vin3", axp803_dcdc234_ranges,
437 76, AXP803_DCDC3_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
438 BIT(2)),
439 AXP_DESC_RANGES(AXP813, DCDC4, "dcdc4", "vin4", axp803_dcdc234_ranges,
440 76, AXP803_DCDC4_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
441 BIT(3)),
442 AXP_DESC_RANGES(AXP813, DCDC5, "dcdc5", "vin5", axp803_dcdc5_ranges,
443 68, AXP803_DCDC5_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
444 BIT(4)),
445 AXP_DESC_RANGES(AXP813, DCDC6, "dcdc6", "vin6", axp803_dcdc6_ranges,
446 72, AXP803_DCDC6_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
447 BIT(5)),
448 AXP_DESC_RANGES(AXP813, DCDC7, "dcdc7", "vin7", axp803_dcdc6_ranges,
449 72, AXP813_DCDC7_V_OUT, 0x7f, AXP22X_PWR_OUT_CTRL1,
450 BIT(6)),
451 AXP_DESC(AXP813, ALDO1, "aldo1", "aldoin", 700, 3300, 100,
452 AXP22X_ALDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(5)),
453 AXP_DESC(AXP813, ALDO2, "aldo2", "aldoin", 700, 3300, 100,
454 AXP22X_ALDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(6)),
455 AXP_DESC(AXP813, ALDO3, "aldo3", "aldoin", 700, 3300, 100,
456 AXP22X_ALDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL3, BIT(7)),
457 AXP_DESC(AXP813, DLDO1, "dldo1", "dldoin", 700, 3300, 100,
458 AXP22X_DLDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(3)),
459 AXP_DESC_RANGES(AXP813, DLDO2, "dldo2", "dldoin", axp803_dldo2_ranges,
460 32, AXP22X_DLDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2,
461 BIT(4)),
462 AXP_DESC(AXP813, DLDO3, "dldo3", "dldoin", 700, 3300, 100,
463 AXP22X_DLDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(5)),
464 AXP_DESC(AXP813, DLDO4, "dldo4", "dldoin", 700, 3300, 100,
465 AXP22X_DLDO4_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(6)),
466 AXP_DESC(AXP813, ELDO1, "eldo1", "eldoin", 700, 1900, 50,
467 AXP22X_ELDO1_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(0)),
468 AXP_DESC(AXP813, ELDO2, "eldo2", "eldoin", 700, 1900, 50,
469 AXP22X_ELDO2_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(1)),
470 AXP_DESC(AXP813, ELDO3, "eldo3", "eldoin", 700, 1900, 50,
471 AXP22X_ELDO3_V_OUT, 0x1f, AXP22X_PWR_OUT_CTRL2, BIT(2)),
472 /* to do / check ... */
473 AXP_DESC(AXP813, FLDO1, "fldo1", "fldoin", 700, 1450, 50,
474 AXP803_FLDO1_V_OUT, 0x0f, AXP22X_PWR_OUT_CTRL3, BIT(2)),
475 AXP_DESC(AXP813, FLDO2, "fldo2", "fldoin", 700, 1450, 50,
476 AXP803_FLDO2_V_OUT, 0x0f, AXP22X_PWR_OUT_CTRL3, BIT(3)),
477 /*
478 * TODO: FLDO3 = {DCDC5, FLDOIN} / 2
479 *
480 * This means FLDO3 effectively switches supplies at runtime,
481 * something the regulator subsystem does not support.
482 */
483 AXP_DESC_FIXED(AXP813, RTC_LDO, "rtc-ldo", "ips", 1800),
484 AXP_DESC_IO(AXP813, LDO_IO0, "ldo-io0", "ips", 700, 3300, 100,
485 AXP22X_LDO_IO0_V_OUT, 0x1f, AXP20X_GPIO0_CTRL, 0x07,
486 AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
487 AXP_DESC_IO(AXP813, LDO_IO1, "ldo-io1", "ips", 700, 3300, 100,
488 AXP22X_LDO_IO1_V_OUT, 0x1f, AXP20X_GPIO1_CTRL, 0x07,
489 AXP22X_IO_ENABLED, AXP22X_IO_DISABLED),
490 AXP_DESC_SW(AXP813, SW, "sw", "swin", AXP22X_PWR_OUT_CTRL2, BIT(7)),
491};
492
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200493static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
494{
495 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800496 unsigned int reg = AXP20X_DCDC_FREQ;
Boris BREZILLON866bd952015-04-10 12:09:03 +0800497 u32 min, max, def, step;
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200498
Boris BREZILLON866bd952015-04-10 12:09:03 +0800499 switch (axp20x->variant) {
500 case AXP202_ID:
501 case AXP209_ID:
502 min = 750;
503 max = 1875;
504 def = 1500;
505 step = 75;
506 break;
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800507 case AXP803_ID:
Chen-Yu Tsaid81851c2017-09-29 11:25:09 +0800508 case AXP813_ID:
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800509 /*
Chen-Yu Tsaid81851c2017-09-29 11:25:09 +0800510 * AXP803/AXP813 DCDC work frequency setting has the same
511 * range and step as AXP22X, but at a different register.
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800512 * Fall through to the check below.
513 * (See include/linux/mfd/axp20x.h)
514 */
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800515 reg = AXP803_DCDC_FREQ_CTRL;
516 case AXP806_ID:
517 /*
518 * AXP806 also have DCDC work frequency setting register at a
519 * different position.
520 */
521 if (axp20x->variant == AXP806_ID)
522 reg = AXP806_DCDC_FREQ_CTRL;
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800523 case AXP221_ID:
Chen-Yu Tsai04e09812016-02-12 10:02:45 +0800524 case AXP223_ID:
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800525 case AXP809_ID:
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800526 min = 1800;
527 max = 4050;
528 def = 3000;
529 step = 150;
530 break;
Boris BREZILLON866bd952015-04-10 12:09:03 +0800531 default:
532 dev_err(&pdev->dev,
533 "Setting DCDC frequency for unsupported AXP variant\n");
534 return -EINVAL;
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200535 }
536
Boris BREZILLON866bd952015-04-10 12:09:03 +0800537 if (dcdcfreq == 0)
538 dcdcfreq = def;
539
540 if (dcdcfreq < min) {
541 dcdcfreq = min;
542 dev_warn(&pdev->dev, "DCDC frequency too low. Set to %ukHz\n",
543 min);
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200544 }
545
Boris BREZILLON866bd952015-04-10 12:09:03 +0800546 if (dcdcfreq > max) {
547 dcdcfreq = max;
548 dev_warn(&pdev->dev, "DCDC frequency too high. Set to %ukHz\n",
549 max);
550 }
551
552 dcdcfreq = (dcdcfreq - min) / step;
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200553
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800554 return regmap_update_bits(axp20x->regmap, reg,
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200555 AXP20X_FREQ_DCDC_MASK, dcdcfreq);
556}
557
558static int axp20x_regulator_parse_dt(struct platform_device *pdev)
559{
560 struct device_node *np, *regulators;
561 int ret;
Boris BREZILLON866bd952015-04-10 12:09:03 +0800562 u32 dcdcfreq = 0;
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200563
564 np = of_node_get(pdev->dev.parent->of_node);
565 if (!np)
566 return 0;
567
Boris BREZILLONa6016c52014-05-19 10:25:30 +0200568 regulators = of_get_child_by_name(np, "regulators");
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200569 if (!regulators) {
570 dev_warn(&pdev->dev, "regulators node not found\n");
571 } else {
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200572 of_property_read_u32(regulators, "x-powers,dcdc-freq", &dcdcfreq);
573 ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
574 if (ret < 0) {
575 dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
576 return ret;
577 }
578
579 of_node_put(regulators);
580 }
581
582 return 0;
583}
584
585static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
586{
Boris BREZILLON866bd952015-04-10 12:09:03 +0800587 struct axp20x_dev *axp20x = rdev_get_drvdata(rdev);
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800588 unsigned int reg = AXP20X_DCDC_MODE;
Boris BREZILLON866bd952015-04-10 12:09:03 +0800589 unsigned int mask;
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200590
Boris BREZILLON866bd952015-04-10 12:09:03 +0800591 switch (axp20x->variant) {
592 case AXP202_ID:
593 case AXP209_ID:
594 if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
595 return -EINVAL;
596
597 mask = AXP20X_WORKMODE_DCDC2_MASK;
598 if (id == AXP20X_DCDC3)
599 mask = AXP20X_WORKMODE_DCDC3_MASK;
600
601 workmode <<= ffs(mask) - 1;
602 break;
603
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800604 case AXP806_ID:
605 reg = AXP806_DCDC_MODE_CTRL2;
606 /*
607 * AXP806 DCDC regulator IDs have the same range as AXP22X.
608 * Fall through to the check below.
609 * (See include/linux/mfd/axp20x.h)
610 */
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800611 case AXP221_ID:
Chen-Yu Tsai04e09812016-02-12 10:02:45 +0800612 case AXP223_ID:
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800613 case AXP809_ID:
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800614 if (id < AXP22X_DCDC1 || id > AXP22X_DCDC5)
615 return -EINVAL;
616
617 mask = AXP22X_WORKMODE_DCDCX_MASK(id - AXP22X_DCDC1);
618 workmode <<= id - AXP22X_DCDC1;
619 break;
620
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800621 case AXP803_ID:
622 if (id < AXP803_DCDC1 || id > AXP803_DCDC6)
623 return -EINVAL;
624
625 mask = AXP22X_WORKMODE_DCDCX_MASK(id - AXP803_DCDC1);
626 workmode <<= id - AXP803_DCDC1;
627 break;
628
Chen-Yu Tsaid81851c2017-09-29 11:25:09 +0800629 case AXP813_ID:
630 if (id < AXP813_DCDC1 || id > AXP813_DCDC7)
631 return -EINVAL;
632
633 mask = AXP22X_WORKMODE_DCDCX_MASK(id - AXP813_DCDC1);
634 workmode <<= id - AXP813_DCDC1;
635 break;
636
Boris BREZILLON866bd952015-04-10 12:09:03 +0800637 default:
638 /* should not happen */
639 WARN_ON(1);
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200640 return -EINVAL;
Boris BREZILLON866bd952015-04-10 12:09:03 +0800641 }
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200642
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800643 return regmap_update_bits(rdev->regmap, reg, mask, workmode);
644}
645
646/*
647 * This function checks whether a regulator is part of a poly-phase
648 * output setup based on the registers settings. Returns true if it is.
649 */
650static bool axp20x_is_polyphase_slave(struct axp20x_dev *axp20x, int id)
651{
652 u32 reg = 0;
653
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800654 /*
Chen-Yu Tsaid81851c2017-09-29 11:25:09 +0800655 * Currently in our supported AXP variants, only AXP803, AXP806,
656 * and AXP813 have polyphase regulators.
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800657 */
658 switch (axp20x->variant) {
659 case AXP803_ID:
Axel Linad92cea2017-10-15 17:03:12 +0800660 case AXP813_ID:
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800661 regmap_read(axp20x->regmap, AXP803_POLYPHASE_CTRL, &reg);
662
663 switch (id) {
664 case AXP803_DCDC3:
665 return !!(reg & BIT(6));
666 case AXP803_DCDC6:
Chen-Yu Tsai986e7b72017-09-29 11:25:08 +0800667 return !!(reg & BIT(5));
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800668 }
669 break;
670
671 case AXP806_ID:
672 regmap_read(axp20x->regmap, AXP806_DCDC_MODE_CTRL2, &reg);
673
674 switch (id) {
675 case AXP806_DCDCB:
676 return (((reg & GENMASK(7, 6)) == BIT(6)) ||
677 ((reg & GENMASK(7, 6)) == BIT(7)));
678 case AXP806_DCDCC:
679 return ((reg & GENMASK(7, 6)) == BIT(7));
680 case AXP806_DCDCE:
681 return !!(reg & BIT(5));
682 }
683 break;
684
685 default:
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800686 return false;
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800687 }
688
689 return false;
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200690}
691
692static int axp20x_regulator_probe(struct platform_device *pdev)
693{
694 struct regulator_dev *rdev;
695 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
Boris BREZILLON866bd952015-04-10 12:09:03 +0800696 const struct regulator_desc *regulators;
Chen-Yu Tsai765e8022015-01-10 00:23:44 +0800697 struct regulator_config config = {
698 .dev = pdev->dev.parent,
699 .regmap = axp20x->regmap,
Boris BREZILLON866bd952015-04-10 12:09:03 +0800700 .driver_data = axp20x,
Chen-Yu Tsai765e8022015-01-10 00:23:44 +0800701 };
Boris BREZILLON866bd952015-04-10 12:09:03 +0800702 int ret, i, nregulators;
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200703 u32 workmode;
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800704 const char *dcdc1_name = axp22x_regulators[AXP22X_DCDC1].name;
705 const char *dcdc5_name = axp22x_regulators[AXP22X_DCDC5].name;
Hans de Goede636e2a32016-06-03 18:59:44 +0200706 bool drivevbus = false;
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200707
Boris BREZILLON866bd952015-04-10 12:09:03 +0800708 switch (axp20x->variant) {
709 case AXP202_ID:
710 case AXP209_ID:
711 regulators = axp20x_regulators;
712 nregulators = AXP20X_REG_ID_MAX;
713 break;
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800714 case AXP221_ID:
Chen-Yu Tsai04e09812016-02-12 10:02:45 +0800715 case AXP223_ID:
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800716 regulators = axp22x_regulators;
717 nregulators = AXP22X_REG_ID_MAX;
Hans de Goede636e2a32016-06-03 18:59:44 +0200718 drivevbus = of_property_read_bool(pdev->dev.parent->of_node,
719 "x-powers,drive-vbus-en");
Boris BREZILLON1b82b4e2015-04-10 12:09:04 +0800720 break;
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800721 case AXP803_ID:
722 regulators = axp803_regulators;
723 nregulators = AXP803_REG_ID_MAX;
Jagan Teki1f5d6462018-04-23 12:02:37 +0530724 drivevbus = of_property_read_bool(pdev->dev.parent->of_node,
725 "x-powers,drive-vbus-en");
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800726 break;
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800727 case AXP806_ID:
728 regulators = axp806_regulators;
729 nregulators = AXP806_REG_ID_MAX;
730 break;
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800731 case AXP809_ID:
732 regulators = axp809_regulators;
733 nregulators = AXP809_REG_ID_MAX;
734 break;
Chen-Yu Tsaid81851c2017-09-29 11:25:09 +0800735 case AXP813_ID:
736 regulators = axp813_regulators;
737 nregulators = AXP813_REG_ID_MAX;
738 drivevbus = of_property_read_bool(pdev->dev.parent->of_node,
739 "x-powers,drive-vbus-en");
740 break;
Boris BREZILLON866bd952015-04-10 12:09:03 +0800741 default:
742 dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
743 axp20x->variant);
744 return -EINVAL;
745 }
746
Chen-Yu Tsai765e8022015-01-10 00:23:44 +0800747 /* This only sets the dcdc freq. Ignore any errors */
748 axp20x_regulator_parse_dt(pdev);
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200749
Boris BREZILLON866bd952015-04-10 12:09:03 +0800750 for (i = 0; i < nregulators; i++) {
Chen-Yu Tsai7118f192015-09-30 14:39:46 +0800751 const struct regulator_desc *desc = &regulators[i];
752 struct regulator_desc *new_desc;
753
754 /*
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800755 * If this regulator is a slave in a poly-phase setup,
756 * skip it, as its controls are bound to the master
757 * regulator and won't work.
758 */
759 if (axp20x_is_polyphase_slave(axp20x, i))
760 continue;
761
Chen-Yu Tsaid81851c2017-09-29 11:25:09 +0800762 /* Support for AXP813's FLDO3 is not implemented */
763 if (axp20x->variant == AXP813_ID && i == AXP813_FLDO3)
764 continue;
765
Chen-Yu Tsai2ca342d2016-08-27 15:55:39 +0800766 /*
Chen-Yu Tsai7118f192015-09-30 14:39:46 +0800767 * Regulators DC1SW and DC5LDO are connected internally,
768 * so we have to handle their supply names separately.
769 *
770 * We always register the regulators in proper sequence,
771 * so the supply names are correctly read. See the last
772 * part of this loop to see where we save the DT defined
773 * name.
774 */
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800775 if ((regulators == axp22x_regulators && i == AXP22X_DC1SW) ||
Icenowy Zheng1dbe0cc2017-05-18 15:16:49 +0800776 (regulators == axp803_regulators && i == AXP803_DC1SW) ||
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800777 (regulators == axp809_regulators && i == AXP809_DC1SW)) {
778 new_desc = devm_kzalloc(&pdev->dev, sizeof(*desc),
779 GFP_KERNEL);
Gustavo A. R. Silvada262962017-07-06 16:49:18 -0500780 if (!new_desc)
781 return -ENOMEM;
782
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800783 *new_desc = regulators[i];
784 new_desc->supply_name = dcdc1_name;
785 desc = new_desc;
786 }
787
788 if ((regulators == axp22x_regulators && i == AXP22X_DC5LDO) ||
789 (regulators == axp809_regulators && i == AXP809_DC5LDO)) {
790 new_desc = devm_kzalloc(&pdev->dev, sizeof(*desc),
791 GFP_KERNEL);
Gustavo A. R. Silvada262962017-07-06 16:49:18 -0500792 if (!new_desc)
793 return -ENOMEM;
794
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800795 *new_desc = regulators[i];
796 new_desc->supply_name = dcdc5_name;
797 desc = new_desc;
Chen-Yu Tsai7118f192015-09-30 14:39:46 +0800798 }
799
800 rdev = devm_regulator_register(&pdev->dev, desc, &config);
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200801 if (IS_ERR(rdev)) {
802 dev_err(&pdev->dev, "Failed to register %s\n",
Boris BREZILLON866bd952015-04-10 12:09:03 +0800803 regulators[i].name);
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200804
805 return PTR_ERR(rdev);
806 }
807
Chen-Yu Tsai765e8022015-01-10 00:23:44 +0800808 ret = of_property_read_u32(rdev->dev.of_node,
809 "x-powers,dcdc-workmode",
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200810 &workmode);
811 if (!ret) {
812 if (axp20x_set_dcdc_workmode(rdev, i, workmode))
813 dev_err(&pdev->dev, "Failed to set workmode on %s\n",
Boris BREZILLON866bd952015-04-10 12:09:03 +0800814 rdev->desc->name);
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200815 }
Chen-Yu Tsai7118f192015-09-30 14:39:46 +0800816
817 /*
818 * Save AXP22X DCDC1 / DCDC5 regulator names for later.
819 */
Chen-Yu Tsaia51f9f42016-06-01 00:23:19 +0800820 if ((regulators == axp22x_regulators && i == AXP22X_DCDC1) ||
821 (regulators == axp809_regulators && i == AXP809_DCDC1))
822 of_property_read_string(rdev->dev.of_node,
823 "regulator-name",
824 &dcdc1_name);
825
826 if ((regulators == axp22x_regulators && i == AXP22X_DCDC5) ||
827 (regulators == axp809_regulators && i == AXP809_DCDC5))
828 of_property_read_string(rdev->dev.of_node,
829 "regulator-name",
830 &dcdc5_name);
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200831 }
832
Hans de Goede636e2a32016-06-03 18:59:44 +0200833 if (drivevbus) {
834 /* Change N_VBUSEN sense pin to DRIVEVBUS output pin */
835 regmap_update_bits(axp20x->regmap, AXP20X_OVER_TMP,
836 AXP22X_MISC_N_VBUSEN_FUNC, 0);
837 rdev = devm_regulator_register(&pdev->dev,
838 &axp22x_drivevbus_regulator,
839 &config);
840 if (IS_ERR(rdev)) {
841 dev_err(&pdev->dev, "Failed to register drivevbus\n");
842 return PTR_ERR(rdev);
843 }
844 }
845
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200846 return 0;
847}
848
849static struct platform_driver axp20x_regulator_driver = {
850 .probe = axp20x_regulator_probe,
851 .driver = {
852 .name = "axp20x-regulator",
Carlo Caionedfe7a1b2014-04-11 11:38:10 +0200853 },
854};
855
856module_platform_driver(axp20x_regulator_driver);
857
858MODULE_LICENSE("GPL v2");
859MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
860MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");
Ian Campbelld4ea7d82015-08-01 18:13:25 +0100861MODULE_ALIAS("platform:axp20x-regulator");