1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022 Starfive Technology Co., Ltd.
4 * Author: Kevin Xie <kevin.xie@starfivetech.com>
6 #include <linux/bitops.h>
8 #include <linux/gpio.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/regulator/of_regulator.h>
17 #include <linux/regulator/axp15060.h>
18 #include <linux/slab.h>
19 #include <linux/regulator/consumer.h>
21 #define AXP15060_ON_OFF_CTRL_1 0x10
22 #define AXP15060_ON_OFF_CTRL_2 0x11
23 #define AXP15060_ON_OFF_CTRL_3 0x12
24 #define AXP15060_VOL_CTRL_DCDC_1 0x13
25 #define AXP15060_VOL_CTRL_DCDC_2 0x14
26 #define AXP15060_VOL_CTRL_DCDC_3 0x15
27 #define AXP15060_VOL_CTRL_DCDC_4 0x16
28 #define AXP15060_VOL_CTRL_DCDC_5 0x17
29 #define AXP15060_VOL_CTRL_DCDC_6 0x18
30 #define AXP15060_VOL_CTRL_ALDO_1 0x19
31 #define AXP15060_DCDC_MODE_CTRL_1 0x1A
32 #define AXP15060_DCDC_MODE_CTRL_2 0x1B
34 #define AXP15060_OUTPUT_MONITOR_OFF_DISCHARGE 0x1E
35 #define AXP15060_IRQ_PWROK_VOFF_SETTING 0x1F
36 #define AXP15060_VOL_CTRL_ALDO_2 0x20
37 #define AXP15060_VOL_CTRL_ALDO_3 0x21
38 #define AXP15060_VOL_CTRL_ALDO_4 0x22
39 #define AXP15060_VOL_CTRL_ALDO_5 0x23
40 #define AXP15060_VOL_CTRL_BLDO_1 0x24
41 #define AXP15060_VOL_CTRL_BLDO_2 0x25
42 #define AXP15060_VOL_CTRL_BLDO_3 0x26
43 #define AXP15060_VOL_CTRL_BLDO_4 0x27
44 #define AXP15060_VOL_CTRL_BLDO_5 0x28
45 #define AXP15060_VOL_CTRL_CLDO_1 0x29
46 #define AXP15060_VOL_CTRL_CLDO_2 0x2A
47 /* CLDO3 voltage ctrl and CLDO3/GPIO1/Wakeup ctrl */
48 #define AXP15060_VOL_CTRL_CLDO_3 0x2B
49 #define AXP15060_CLDO_4_GPIO_2_CTRL 0x2C
50 #define AXP15060_VOL_CTRL_CLDO_4 0x2D
51 #define AXP15060_VOL_CTRL_CPUSLDO 0x2E
53 #define AXP15060_PWR_WAKEUP_CTRL 0x31
54 /* Power disable and power down sequence */
55 #define AXP15060_PWR_DISABLE 0x32
57 #define AXP15060_POK_SETTING 0x36
59 #define AXP15060_INTERFACE_MODE_SEL 0x3E
61 #define AXP15060_IRQ_ENABLE_1 0x40
62 #define AXP15060_IRQ_ENABLE_2 0x41
64 #define AXP15060_IRQ_STATUS_1 0x48
65 #define AXP15060_IRQ_STATUS_2 0x49
67 /* AXP15060_ON_OFF_CTRL_1 */
68 #define AXP15060_PWR_OUT_DCDC1_MASK BIT(0)
69 #define AXP15060_PWR_OUT_DCDC2_MASK BIT(1)
70 #define AXP15060_PWR_OUT_DCDC3_MASK BIT(2)
71 #define AXP15060_PWR_OUT_DCDC4_MASK BIT(3)
72 #define AXP15060_PWR_OUT_DCDC5_MASK BIT(4)
73 #define AXP15060_PWR_OUT_DCDC6_MASK BIT(5)
75 /* AXP15060_ON_OFF_CTRL_2 */
76 #define AXP15060_PWR_OUT_ALDO1_MASK BIT(0)
77 #define AXP15060_PWR_OUT_ALDO2_MASK BIT(1)
78 #define AXP15060_PWR_OUT_ALDO3_MASK BIT(2)
79 #define AXP15060_PWR_OUT_ALDO4_MASK BIT(3)
80 #define AXP15060_PWR_OUT_ALDO5_MASK BIT(4)
81 #define AXP15060_PWR_OUT_BLDO1_MASK BIT(5)
82 #define AXP15060_PWR_OUT_BLDO2_MASK BIT(6)
83 #define AXP15060_PWR_OUT_BLDO3_MASK BIT(7)
85 /* AXP15060_ON_OFF_CTRL_3 */
86 #define AXP15060_PWR_OUT_BLDO4_MASK BIT(0)
87 #define AXP15060_PWR_OUT_BLDO5_MASK BIT(1)
88 #define AXP15060_PWR_OUT_CLDO1_MASK BIT(2)
89 #define AXP15060_PWR_OUT_CLDO2_MASK BIT(3)
90 #define AXP15060_PWR_OUT_CLDO3_MASK BIT(4)
91 #define AXP15060_PWR_OUT_CLDO4_MASK BIT(5)
92 #define AXP15060_PWR_OUT_CPULDO_MASK BIT(6)
93 #define AXP15060_PWR_OUT_SWITCH_MASK BIT(7)
95 #define AXP15060_ALDO1_V_OUT_MASK GENMASK(4, 0)
96 #define AXP15060_ALDO2_V_OUT_MASK GENMASK(4, 0)
97 #define AXP15060_ALDO3_V_OUT_MASK GENMASK(4, 0)
98 #define AXP15060_ALDO4_V_OUT_MASK GENMASK(4, 0)
99 #define AXP15060_ALDO5_V_OUT_MASK GENMASK(4, 0)
101 #define AXP15060_BLDO1_V_OUT_MASK GENMASK(4, 0)
102 #define AXP15060_BLDO2_V_OUT_MASK GENMASK(4, 0)
103 #define AXP15060_BLDO3_V_OUT_MASK GENMASK(4, 0)
104 #define AXP15060_BLDO4_V_OUT_MASK GENMASK(4, 0)
105 #define AXP15060_BLDO5_V_OUT_MASK GENMASK(4, 0)
107 #define AXP15060_CLDO1_V_OUT_MASK GENMASK(4, 0)
108 #define AXP15060_CLDO2_V_OUT_MASK GENMASK(4, 0)
109 #define AXP15060_CLDO3_V_OUT_MASK GENMASK(4, 0)
110 #define AXP15060_CLDO4_V_OUT_MASK GENMASK(5, 0)
111 #define AXP15060_CPUSLDO_V_OUT_MASK GENMASK(3, 0)
113 #define AXP15060_DCDC1_V_OUT_MASK GENMASK(4, 0)
114 /* DCDC2 bit7 is set by fw, which is different from datasheet desc. */
115 #define AXP15060_DCDC2_V_OUT_MASK GENMASK(7, 0)
116 #define AXP15060_DCDC3_V_OUT_MASK GENMASK(6, 0)
117 #define AXP15060_DCDC4_V_OUT_MASK GENMASK(6, 0)
118 #define AXP15060_DCDC5_V_OUT_MASK GENMASK(6, 0)
119 #define AXP15060_DCDC6_V_OUT_MASK GENMASK(4, 0)
121 #define AXP15060_DCDC2_V_OUT_RANGE1_7bit_500mV_START 0x00
122 #define AXP15060_DCDC2_V_OUT_RANGE1_7bit_1200mV_END 0x46
123 #define AXP15060_DCDC2_V_OUT_RANGE2_7bit_1220mV_START 0x47
124 #define AXP15060_DCDC2_V_OUT_RANGE2_7bit_1540mV_END 0x57
125 #define AXP15060_DCDC2_V_OUT_RANGE1_500mV_START 0x80
126 #define AXP15060_DCDC2_V_OUT_RANGE1_1200mV_END 0xC6
127 #define AXP15060_DCDC2_V_OUT_RANGE2_1220mV_START 0xC7
128 #define AXP15060_DCDC2_V_OUT_RANGE2_1540mV_END 0xD7
129 #define AXP15060_DCDC2_NUM_VOLTAGES \
130 (AXP15060_DCDC2_V_OUT_RANGE2_1540mV_END + 1)
132 static const struct linear_range axp15060_dcdc2_ranges[] = {
133 REGULATOR_LINEAR_RANGE(500000,
134 AXP15060_DCDC2_V_OUT_RANGE1_7bit_500mV_START,
135 AXP15060_DCDC2_V_OUT_RANGE1_7bit_1200mV_END,
137 REGULATOR_LINEAR_RANGE(1220000,
138 AXP15060_DCDC2_V_OUT_RANGE2_7bit_1220mV_START,
139 AXP15060_DCDC2_V_OUT_RANGE2_7bit_1540mV_END,
141 REGULATOR_LINEAR_RANGE(500000,
142 AXP15060_DCDC2_V_OUT_RANGE1_500mV_START,
143 AXP15060_DCDC2_V_OUT_RANGE1_1200mV_END,
145 REGULATOR_LINEAR_RANGE(1220000,
146 AXP15060_DCDC2_V_OUT_RANGE2_1220mV_START,
147 AXP15060_DCDC2_V_OUT_RANGE2_1540mV_END,
151 static const struct regmap_range axp15060_writeable_ranges[] = {
152 regmap_reg_range(AXP15060_ON_OFF_CTRL_1, AXP15060_DCDC_MODE_CTRL_2),
153 regmap_reg_range(AXP15060_OUTPUT_MONITOR_OFF_DISCHARGE,
154 AXP15060_VOL_CTRL_CPUSLDO),
155 regmap_reg_range(AXP15060_PWR_WAKEUP_CTRL, AXP15060_PWR_DISABLE),
156 regmap_reg_range(AXP15060_POK_SETTING, AXP15060_POK_SETTING),
157 regmap_reg_range(AXP15060_INTERFACE_MODE_SEL, AXP15060_INTERFACE_MODE_SEL),
158 regmap_reg_range(AXP15060_IRQ_ENABLE_1, AXP15060_IRQ_ENABLE_2),
159 regmap_reg_range(AXP15060_IRQ_STATUS_1, AXP15060_IRQ_STATUS_2),
162 static const struct regmap_access_table axp15060_writeable_table = {
163 .yes_ranges = axp15060_writeable_ranges,
164 .n_yes_ranges = ARRAY_SIZE(axp15060_writeable_ranges),
167 static const struct regmap_config axp15060_regmap_config = {
170 .wr_table = &axp15060_writeable_table,
171 .max_register = AXP15060_IRQ_STATUS_2,
172 .cache_type = REGCACHE_NONE,
175 static const struct regulator_ops axp15060_fixed_ops = {
176 .list_voltage = regulator_list_voltage_linear,
179 static const struct regulator_ops axp15060_sw_ops = {
180 .enable = regulator_enable_regmap,
181 .disable = regulator_disable_regmap,
182 .is_enabled = regulator_is_enabled_regmap,
185 static const struct regulator_ops axp15060_ops = {
186 .set_voltage_sel = regulator_set_voltage_sel_regmap,
187 .get_voltage_sel = regulator_get_voltage_sel_regmap,
188 .list_voltage = regulator_list_voltage_linear,
189 .enable = regulator_enable_regmap,
190 .disable = regulator_disable_regmap,
191 .is_enabled = regulator_is_enabled_regmap,
194 static const struct regulator_ops axp15060_range_ops = {
195 .set_voltage_sel = regulator_set_voltage_sel_regmap,
196 .get_voltage_sel = regulator_get_voltage_sel_regmap,
197 .set_voltage_time_sel = regulator_set_voltage_time_sel,
198 .list_voltage = regulator_list_voltage_linear_range,
199 .enable = regulator_enable_regmap,
200 .disable = regulator_disable_regmap,
201 .is_enabled = regulator_is_enabled_regmap,
204 #define AXP15060_DESC_FIXED(_id, _match, _volt) \
207 .of_match = of_match_ptr(_match), \
208 .regulators_node = of_match_ptr("regulators"), \
209 .type = REGULATOR_VOLTAGE, \
210 .id = AXP15060_ID_##_id, \
212 .owner = THIS_MODULE, \
213 .min_uV = (_volt) * 1000, \
214 .ops = &axp15060_fixed_ops, \
217 #define AXP_DESC_SW(_id, _match, _ereg, _emask) \
220 .of_match = of_match_ptr(_match), \
221 .regulators_node = of_match_ptr("regulators"), \
222 .type = REGULATOR_VOLTAGE, \
223 .id = AXP15060_ID_##_id, \
224 .owner = THIS_MODULE, \
225 .enable_reg = (_ereg), \
226 .enable_mask = (_emask), \
227 .ops = &axp15060_sw_ops, \
230 #define AXP15060_DESC(_id, _match, _min, _max, _step, _vreg, \
231 _vmask, _ereg, _emask) \
234 .of_match = of_match_ptr(_match), \
235 .regulators_node = of_match_ptr("regulators"), \
236 .type = REGULATOR_VOLTAGE, \
237 .id = AXP15060_ID_##_id, \
238 .n_voltages = (((_max) - (_min)) / (_step) + 1), \
239 .owner = THIS_MODULE, \
240 .min_uV = (_min) * 1000, \
241 .uV_step = (_step) * 1000, \
242 .vsel_reg = (_vreg), \
243 .vsel_mask = (_vmask), \
244 .enable_reg = (_ereg), \
245 .enable_mask = (_emask), \
246 .ops = &axp15060_ops, \
249 #define AXP15060_DESC_RANGES(_id, _match, _ranges, _n_voltages,\
250 _vreg, _vmask, _ereg, _emask) \
253 .of_match = of_match_ptr(_match), \
254 .regulators_node = of_match_ptr("regulators"), \
255 .type = REGULATOR_VOLTAGE, \
256 .id = AXP15060_ID_##_id, \
257 .n_voltages = (_n_voltages), \
258 .owner = THIS_MODULE, \
259 .vsel_reg = (_vreg), \
260 .vsel_mask = (_vmask), \
261 .enable_reg = (_ereg), \
262 .enable_mask = (_emask), \
263 .linear_ranges = (_ranges), \
264 .n_linear_ranges = ARRAY_SIZE(_ranges), \
265 .ops = &axp15060_range_ops, \
267 /* Only register the regulators that needed to be controlled(onoff/vol) */
268 static const struct regulator_desc axp15060_regulators[] = {
269 AXP15060_DESC(ALDO1, "mipi_0p9", 700, 3300, 100,
270 AXP15060_VOL_CTRL_ALDO_1, AXP15060_ALDO1_V_OUT_MASK,
271 AXP15060_ON_OFF_CTRL_2, AXP15060_PWR_OUT_ALDO1_MASK),
273 AXP15060_DESC(ALDO3, "hdmi_1p8", 700, 3300, 100,
274 AXP15060_VOL_CTRL_ALDO_3, AXP15060_ALDO3_V_OUT_MASK,
275 AXP15060_ON_OFF_CTRL_2, AXP15060_PWR_OUT_ALDO3_MASK),
277 AXP15060_DESC(ALDO4, "sdio_vdd", 700, 3300, 100,
278 AXP15060_VOL_CTRL_ALDO_4, AXP15060_ALDO4_V_OUT_MASK,
279 AXP15060_ON_OFF_CTRL_2, AXP15060_PWR_OUT_ALDO4_MASK),
281 AXP15060_DESC(ALDO5, "hdmi_0p9", 700, 3300, 100,
282 AXP15060_VOL_CTRL_ALDO_5, AXP15060_ALDO5_V_OUT_MASK,
283 AXP15060_ON_OFF_CTRL_2, AXP15060_PWR_OUT_ALDO5_MASK),
285 AXP15060_DESC_RANGES(DCDC2, "cpu_vdd",
286 axp15060_dcdc2_ranges, AXP15060_DCDC2_NUM_VOLTAGES,
287 AXP15060_VOL_CTRL_DCDC_2, AXP15060_DCDC2_V_OUT_MASK,
288 AXP15060_ON_OFF_CTRL_1, AXP15060_PWR_OUT_DCDC2_MASK),
291 static struct of_regulator_match axp15060_matches[] = {
292 { .name = "mipi_0p9", },
293 { .name = "hdmi_1p8", },
294 { .name = "sdio_vdd", },
295 { .name = "hdmi_0p9", },
296 { .name = "cpu_vdd", },
299 static int axp15060_i2c_probe(struct i2c_client *i2c)
301 struct regulator_config config = { };
302 struct regulator_dev *rdev;
303 struct device_node *np, *regulators;
304 struct regmap *regmap;
307 np = of_node_get(i2c->dev.of_node);
311 regulators = of_get_child_by_name(np, "regulators");
313 dev_err(&i2c->dev, "Regulators node not found\n");
317 i = of_regulator_match(&i2c->dev, regulators, axp15060_matches,
318 ARRAY_SIZE(axp15060_matches));
319 of_node_put(i2c->dev.of_node);
321 dev_err(&i2c->dev, "Failed to match regulators\n");
325 regmap = devm_regmap_init_i2c(i2c, &axp15060_regmap_config);
326 if (IS_ERR(regmap)) {
327 ret = PTR_ERR(regmap);
328 dev_err(&i2c->dev, "Failed to allocate register map: %d\n", ret);
332 for (i = 0; i < AXP15060_MAX_REGULATORS; i++) {
333 config.dev = &i2c->dev;
334 config.regmap = regmap;
335 config.init_data = axp15060_matches[i].init_data;
337 rdev = devm_regulator_register(&i2c->dev,
338 &axp15060_regulators[i], &config);
341 "Failed to register AXP15060 regulator\n");
342 return PTR_ERR(rdev);
344 dev_info(&i2c->dev, "Register %s done! vol range:%d ~ %d mV\n",
346 (rdev->constraints->min_uV) / 1000,
347 (rdev->constraints->max_uV) / 1000);
353 static const struct i2c_device_id axp15060_i2c_id[] = {
357 MODULE_DEVICE_TABLE(i2c, axp15060_i2c_id);
360 static const struct of_device_id axp15060_dt_ids[] = {
361 { .compatible = "stf,axp15060-regulator",
362 .data = &axp15060_i2c_id[0] },
365 MODULE_DEVICE_TABLE(of, axp15060_dt_ids);
368 static struct i2c_driver axp15060_regulator_driver = {
370 .name = "axp15060-regulator",
371 .of_match_table = of_match_ptr(axp15060_dt_ids),
373 .probe_new = axp15060_i2c_probe,
374 .id_table = axp15060_i2c_id,
377 module_i2c_driver(axp15060_regulator_driver);
379 MODULE_AUTHOR("Kevin Xie <kevin.xie@starfivetech.com>");
380 MODULE_DESCRIPTION("Regulator device driver for X-Powers AXP15060");
381 MODULE_LICENSE("GPL v2");