Merge tag 'JH7110_515_SDK_v3.6.0' into vf2-515-devel
[platform/kernel/linux-starfive.git] / drivers / regulator / axp15060-regulator.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2022 Starfive Technology Co., Ltd.
4  * Author: Kevin Xie <kevin.xie@starfivetech.com>
5  */
6 #include <linux/bitops.h>
7 #include <linux/err.h>
8 #include <linux/gpio.h>
9 #include <linux/i2c.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>
20
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
33
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
52
53 #define AXP15060_PWR_WAKEUP_CTRL        0x31
54 /* Power disable and power down sequence */
55 #define AXP15060_PWR_DISABLE            0x32
56
57 #define AXP15060_POK_SETTING            0x36
58
59 #define AXP15060_INTERFACE_MODE_SEL     0x3E
60
61 #define AXP15060_IRQ_ENABLE_1           0x40
62 #define AXP15060_IRQ_ENABLE_2           0x41
63
64 #define AXP15060_IRQ_STATUS_1           0x48
65 #define AXP15060_IRQ_STATUS_2           0x49
66
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)
74
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)
84
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)
94
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)
100
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)
106
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)
112
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)
120
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)
131
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,
136                                 10000),
137         REGULATOR_LINEAR_RANGE(1220000,
138                                 AXP15060_DCDC2_V_OUT_RANGE2_7bit_1220mV_START,
139                                 AXP15060_DCDC2_V_OUT_RANGE2_7bit_1540mV_END,
140                                 20000),
141         REGULATOR_LINEAR_RANGE(500000,
142                                 AXP15060_DCDC2_V_OUT_RANGE1_500mV_START,
143                                 AXP15060_DCDC2_V_OUT_RANGE1_1200mV_END,
144                                 10000),
145         REGULATOR_LINEAR_RANGE(1220000,
146                                 AXP15060_DCDC2_V_OUT_RANGE2_1220mV_START,
147                                 AXP15060_DCDC2_V_OUT_RANGE2_1540mV_END,
148                                 20000),
149 };
150
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),
160 };
161
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),
165 };
166
167 static const struct regmap_config axp15060_regmap_config = {
168         .reg_bits = 8,
169         .val_bits = 8,
170         .wr_table       = &axp15060_writeable_table,
171         .max_register = AXP15060_IRQ_STATUS_2,
172         .cache_type = REGCACHE_NONE,
173 };
174
175 static const struct regulator_ops axp15060_fixed_ops = {
176         .list_voltage           = regulator_list_voltage_linear,
177 };
178
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,
183 };
184
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,
192 };
193
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,
202 };
203
204 #define AXP15060_DESC_FIXED(_id, _match, _volt)                                 \
205         {                                                                       \
206                 .name           = (_match),                                     \
207                 .of_match       = of_match_ptr(_match),                         \
208                 .regulators_node = of_match_ptr("regulators"),                  \
209                 .type           = REGULATOR_VOLTAGE,                            \
210                 .id             = AXP15060_ID_##_id,                            \
211                 .n_voltages     = 1,                                            \
212                 .owner          = THIS_MODULE,                                  \
213                 .min_uV         = (_volt) * 1000,                               \
214                 .ops            = &axp15060_fixed_ops,                          \
215         }
216
217 #define AXP_DESC_SW(_id, _match, _ereg, _emask)                                 \
218         {                                                                       \
219                 .name           = (_match),                                     \
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,                             \
228         }
229
230 #define AXP15060_DESC(_id, _match, _min, _max, _step, _vreg,                    \
231                 _vmask, _ereg, _emask)                                          \
232         {                                                                       \
233                 .name           = (_match),                                     \
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,                                \
247         }
248
249 #define AXP15060_DESC_RANGES(_id, _match, _ranges, _n_voltages,\
250                                          _vreg, _vmask, _ereg, _emask)          \
251         {                                                                       \
252                 .name                   = (_match),                             \
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,                  \
266         }
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),
272
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),
276
277         AXP15060_DESC(ALDO5, "hdmi_0p9", 700, 3300, 100,
278                         AXP15060_VOL_CTRL_ALDO_5, AXP15060_ALDO5_V_OUT_MASK,
279                         AXP15060_ON_OFF_CTRL_2, AXP15060_PWR_OUT_ALDO5_MASK),
280
281         AXP15060_DESC_RANGES(DCDC2, "cpu_vdd",
282                                 axp15060_dcdc2_ranges, AXP15060_DCDC2_NUM_VOLTAGES,
283                                 AXP15060_VOL_CTRL_DCDC_2, AXP15060_DCDC2_V_OUT_MASK,
284                                 AXP15060_ON_OFF_CTRL_1, AXP15060_PWR_OUT_DCDC2_MASK),
285 };
286
287 static struct of_regulator_match axp15060_matches[] = {
288         { .name = "mipi_0p9", },
289         { .name = "hdmi_1p8", },
290         { .name = "hdmi_0p9", },
291         { .name = "cpu_vdd", },
292 };
293
294 static int axp15060_i2c_probe(struct i2c_client *i2c)
295 {
296         struct regulator_config config = { };
297         struct regulator_dev *rdev;
298         struct device_node *np, *regulators;
299         struct regmap *regmap;
300         int i, ret;
301
302         np = of_node_get(i2c->dev.of_node);
303         if (!np)
304                 return -EINVAL;
305
306         regulators = of_get_child_by_name(np, "regulators");
307         if (!regulators) {
308                 dev_err(&i2c->dev, "Regulators node not found\n");
309                 return -EINVAL;
310         }
311
312         i = of_regulator_match(&i2c->dev, regulators, axp15060_matches,
313                                  ARRAY_SIZE(axp15060_matches));
314         of_node_put(i2c->dev.of_node);
315         if (i < 0) {
316                 dev_err(&i2c->dev, "Failed to match regulators\n");
317                 return -EINVAL;
318         }
319
320         regmap = devm_regmap_init_i2c(i2c, &axp15060_regmap_config);
321         if (IS_ERR(regmap)) {
322                 ret = PTR_ERR(regmap);
323                 dev_err(&i2c->dev, "Failed to allocate register map: %d\n", ret);
324                 return ret;
325         }
326
327         for (i = 0; i < AXP15060_MAX_REGULATORS; i++) {
328                 config.dev = &i2c->dev;
329                 config.regmap = regmap;
330                 config.init_data = axp15060_matches[i].init_data;
331
332                 rdev = devm_regulator_register(&i2c->dev,
333                         &axp15060_regulators[i], &config);
334                 if (IS_ERR(rdev)) {
335                         dev_err(&i2c->dev,
336                                 "Failed to register AXP15060 regulator\n");
337                         return PTR_ERR(rdev);
338                 }
339                 dev_info(&i2c->dev, "Register %s done! vol range:%d ~ %d mV\n",
340                                 rdev->desc->name,
341                                 (rdev->constraints->min_uV) / 1000,
342                                 (rdev->constraints->max_uV) / 1000);
343         }
344
345         return 0;
346 }
347
348 static const struct i2c_device_id axp15060_i2c_id[] = {
349         {"axp15060_reg", 0},
350         {},
351 };
352 MODULE_DEVICE_TABLE(i2c, axp15060_i2c_id);
353
354 #ifdef CONFIG_OF
355 static const struct of_device_id axp15060_dt_ids[] = {
356         { .compatible = "stf,axp15060-regulator",
357           .data = &axp15060_i2c_id[0] },
358         {},
359 };
360 MODULE_DEVICE_TABLE(of, axp15060_dt_ids);
361 #endif
362
363 static struct i2c_driver axp15060_regulator_driver = {
364         .driver = {
365                 .name = "axp15060-regulator",
366                 .of_match_table = of_match_ptr(axp15060_dt_ids),
367         },
368         .probe_new = axp15060_i2c_probe,
369         .id_table = axp15060_i2c_id,
370 };
371
372 module_i2c_driver(axp15060_regulator_driver);
373
374 MODULE_AUTHOR("Kevin Xie <kevin.xie@starfivetech.com>");
375 MODULE_DESCRIPTION("Regulator device driver for X-Powers AXP15060");
376 MODULE_LICENSE("GPL v2");