blob: d068ac93d37309ef7e65aff7fa03a28dcd3d056c [file] [log] [blame]
Saravanan Sekara2731882020-02-04 12:04:18 +01001// SPDX-License-Identifier: GPL-2.0+
2//
3// mp5416.c - regulator driver for mps mp5416
4//
5// Copyright 2020 Monolithic Power Systems, Inc
6//
7// Author: Saravanan Sekar <sravanhome@gmail.com>
8
Robert Markob9dea012022-06-04 16:58:14 +02009#include <linux/err.h>
10#include <linux/i2c.h>
11#include <linux/init.h>
Saravanan Sekara2731882020-02-04 12:04:18 +010012#include <linux/module.h>
Rob Herring045a44d2023-07-14 11:49:28 -060013#include <linux/of.h>
Saravanan Sekara2731882020-02-04 12:04:18 +010014#include <linux/platform_device.h>
15#include <linux/regmap.h>
16#include <linux/regulator/driver.h>
Saravanan Sekara2731882020-02-04 12:04:18 +010017
18#define MP5416_REG_CTL0 0x00
19#define MP5416_REG_CTL1 0x01
20#define MP5416_REG_CTL2 0x02
21#define MP5416_REG_ILIM 0x03
22#define MP5416_REG_BUCK1 0x04
23#define MP5416_REG_BUCK2 0x05
24#define MP5416_REG_BUCK3 0x06
25#define MP5416_REG_BUCK4 0x07
26#define MP5416_REG_LDO1 0x08
27#define MP5416_REG_LDO2 0x09
28#define MP5416_REG_LDO3 0x0a
29#define MP5416_REG_LDO4 0x0b
30
31#define MP5416_REGULATOR_EN BIT(7)
32#define MP5416_MASK_VSET 0x7f
33#define MP5416_MASK_BUCK1_ILIM 0xc0
34#define MP5416_MASK_BUCK2_ILIM 0x0c
35#define MP5416_MASK_BUCK3_ILIM 0x30
36#define MP5416_MASK_BUCK4_ILIM 0x03
37#define MP5416_MASK_DVS_SLEWRATE 0xc0
38
39/* values in uV */
40#define MP5416_VOLT1_MIN 600000
41#define MP5416_VOLT1_MAX 2187500
42#define MP5416_VOLT1_STEP 12500
43#define MP5416_VOLT2_MIN 800000
44#define MP5416_VOLT2_MAX 3975000
45#define MP5416_VOLT2_STEP 25000
46
47#define MP5416_VOLT1_RANGE \
48 ((MP5416_VOLT1_MAX - MP5416_VOLT1_MIN)/MP5416_VOLT1_STEP + 1)
49#define MP5416_VOLT2_RANGE \
50 ((MP5416_VOLT2_MAX - MP5416_VOLT2_MIN)/MP5416_VOLT2_STEP + 1)
51
52#define MP5416BUCK(_name, _id, _ilim, _dreg, _dval, _vsel) \
53 [MP5416_BUCK ## _id] = { \
54 .id = MP5416_BUCK ## _id, \
55 .name = _name, \
56 .of_match = _name, \
57 .regulators_node = "regulators", \
58 .ops = &mp5416_buck_ops, \
59 .min_uV = MP5416_VOLT ##_vsel## _MIN, \
60 .uV_step = MP5416_VOLT ##_vsel## _STEP, \
61 .n_voltages = MP5416_VOLT ##_vsel## _RANGE, \
62 .curr_table = _ilim, \
63 .n_current_limits = ARRAY_SIZE(_ilim), \
64 .csel_reg = MP5416_REG_ILIM, \
65 .csel_mask = MP5416_MASK_BUCK ## _id ##_ILIM, \
66 .vsel_reg = MP5416_REG_BUCK ## _id, \
67 .vsel_mask = MP5416_MASK_VSET, \
68 .enable_reg = MP5416_REG_BUCK ## _id, \
69 .enable_mask = MP5416_REGULATOR_EN, \
Axel Lin15413ce2021-05-22 10:30:18 +080070 .ramp_reg = MP5416_REG_CTL2, \
71 .ramp_mask = MP5416_MASK_DVS_SLEWRATE, \
72 .ramp_delay_table = mp5416_buck_ramp_table, \
73 .n_ramp_values = ARRAY_SIZE(mp5416_buck_ramp_table), \
Saravanan Sekara2731882020-02-04 12:04:18 +010074 .active_discharge_on = _dval, \
75 .active_discharge_reg = _dreg, \
76 .active_discharge_mask = _dval, \
77 .owner = THIS_MODULE, \
78 }
79
Axel Lin502cdd62020-02-12 23:02:23 +080080#define MP5416LDO(_name, _id, _dval) \
Saravanan Sekara2731882020-02-04 12:04:18 +010081 [MP5416_LDO ## _id] = { \
82 .id = MP5416_LDO ## _id, \
83 .name = _name, \
84 .of_match = _name, \
85 .regulators_node = "regulators", \
86 .ops = &mp5416_ldo_ops, \
87 .min_uV = MP5416_VOLT2_MIN, \
88 .uV_step = MP5416_VOLT2_STEP, \
89 .n_voltages = MP5416_VOLT2_RANGE, \
90 .vsel_reg = MP5416_REG_LDO ##_id, \
91 .vsel_mask = MP5416_MASK_VSET, \
92 .enable_reg = MP5416_REG_LDO ##_id, \
93 .enable_mask = MP5416_REGULATOR_EN, \
Axel Lin502cdd62020-02-12 23:02:23 +080094 .active_discharge_on = _dval, \
Saravanan Sekara2731882020-02-04 12:04:18 +010095 .active_discharge_reg = MP5416_REG_CTL2, \
Axel Lin502cdd62020-02-12 23:02:23 +080096 .active_discharge_mask = _dval, \
Saravanan Sekara2731882020-02-04 12:04:18 +010097 .owner = THIS_MODULE, \
98 }
99
100enum mp5416_regulators {
101 MP5416_BUCK1,
102 MP5416_BUCK2,
103 MP5416_BUCK3,
104 MP5416_BUCK4,
105 MP5416_LDO1,
106 MP5416_LDO2,
107 MP5416_LDO3,
108 MP5416_LDO4,
109 MP5416_MAX_REGULATORS,
110};
111
112static const struct regmap_config mp5416_regmap_config = {
113 .reg_bits = 8,
114 .val_bits = 8,
115 .max_register = 0x0d,
116};
117
118/* Current limits array (in uA)
119 * ILIM1 & ILIM3
120 */
121static const unsigned int mp5416_I_limits1[] = {
122 3800000, 4600000, 5600000, 6800000
123};
124
125/* ILIM2 & ILIM4 */
126static const unsigned int mp5416_I_limits2[] = {
127 2200000, 3200000, 4200000, 5200000
128};
129
Axel Lin15413ce2021-05-22 10:30:18 +0800130/*
131 * DVS ramp rate BUCK1 to BUCK4
132 * 00: 32mV/us
133 * 01: 16mV/us
134 * 10: 8mV/us
135 * 11: 4mV/us
136 */
137static const unsigned int mp5416_buck_ramp_table[] = {
138 32000, 16000, 8000, 4000
139};
Saravanan Sekara2731882020-02-04 12:04:18 +0100140
141static const struct regulator_ops mp5416_ldo_ops = {
142 .enable = regulator_enable_regmap,
143 .disable = regulator_disable_regmap,
144 .is_enabled = regulator_is_enabled_regmap,
145 .list_voltage = regulator_list_voltage_linear,
146 .map_voltage = regulator_map_voltage_linear,
147 .get_voltage_sel = regulator_get_voltage_sel_regmap,
148 .set_voltage_sel = regulator_set_voltage_sel_regmap,
149 .set_active_discharge = regulator_set_active_discharge_regmap,
150};
151
152static const struct regulator_ops mp5416_buck_ops = {
153 .enable = regulator_enable_regmap,
154 .disable = regulator_disable_regmap,
155 .is_enabled = regulator_is_enabled_regmap,
156 .list_voltage = regulator_list_voltage_linear,
157 .map_voltage = regulator_map_voltage_linear,
158 .get_voltage_sel = regulator_get_voltage_sel_regmap,
159 .set_voltage_sel = regulator_set_voltage_sel_regmap,
160 .set_active_discharge = regulator_set_active_discharge_regmap,
161 .get_current_limit = regulator_get_current_limit_regmap,
162 .set_current_limit = regulator_set_current_limit_regmap,
Axel Lin15413ce2021-05-22 10:30:18 +0800163 .set_ramp_delay = regulator_set_ramp_delay_regmap,
Saravanan Sekara2731882020-02-04 12:04:18 +0100164};
165
166static struct regulator_desc mp5416_regulators_desc[MP5416_MAX_REGULATORS] = {
167 MP5416BUCK("buck1", 1, mp5416_I_limits1, MP5416_REG_CTL1, BIT(0), 1),
168 MP5416BUCK("buck2", 2, mp5416_I_limits2, MP5416_REG_CTL1, BIT(1), 2),
169 MP5416BUCK("buck3", 3, mp5416_I_limits1, MP5416_REG_CTL1, BIT(2), 1),
170 MP5416BUCK("buck4", 4, mp5416_I_limits2, MP5416_REG_CTL2, BIT(5), 2),
Axel Lin502cdd62020-02-12 23:02:23 +0800171 MP5416LDO("ldo1", 1, BIT(4)),
172 MP5416LDO("ldo2", 2, BIT(3)),
173 MP5416LDO("ldo3", 3, BIT(2)),
174 MP5416LDO("ldo4", 4, BIT(1)),
Saravanan Sekara2731882020-02-04 12:04:18 +0100175};
176
Robert Markofcdaf742022-06-04 16:58:16 +0200177static struct regulator_desc mp5496_regulators_desc[MP5416_MAX_REGULATORS] = {
178 MP5416BUCK("buck1", 1, mp5416_I_limits1, MP5416_REG_CTL1, BIT(0), 1),
179 MP5416BUCK("buck2", 2, mp5416_I_limits2, MP5416_REG_CTL1, BIT(1), 1),
180 MP5416BUCK("buck3", 3, mp5416_I_limits1, MP5416_REG_CTL1, BIT(2), 1),
181 MP5416BUCK("buck4", 4, mp5416_I_limits2, MP5416_REG_CTL2, BIT(5), 1),
182 MP5416LDO("ldo1", 1, BIT(4)),
183 MP5416LDO("ldo2", 2, BIT(3)),
184 MP5416LDO("ldo3", 3, BIT(2)),
185 MP5416LDO("ldo4", 4, BIT(1)),
186};
187
Saravanan Sekara2731882020-02-04 12:04:18 +0100188static int mp5416_i2c_probe(struct i2c_client *client)
189{
190 struct device *dev = &client->dev;
191 struct regulator_config config = { NULL, };
Robert Markodf43c242022-06-04 16:58:15 +0200192 static const struct regulator_desc *desc;
Saravanan Sekara2731882020-02-04 12:04:18 +0100193 struct regulator_dev *rdev;
194 struct regmap *regmap;
195 int i;
196
197 regmap = devm_regmap_init_i2c(client, &mp5416_regmap_config);
198 if (IS_ERR(regmap)) {
199 dev_err(dev, "Failed to allocate regmap!\n");
200 return PTR_ERR(regmap);
201 }
202
Robert Markodf43c242022-06-04 16:58:15 +0200203 desc = of_device_get_match_data(dev);
204 if (!desc)
205 return -ENODEV;
206
Saravanan Sekara2731882020-02-04 12:04:18 +0100207 config.dev = dev;
208 config.regmap = regmap;
209
210 for (i = 0; i < MP5416_MAX_REGULATORS; i++) {
211 rdev = devm_regulator_register(dev,
Robert Markodf43c242022-06-04 16:58:15 +0200212 &desc[i],
Saravanan Sekara2731882020-02-04 12:04:18 +0100213 &config);
214 if (IS_ERR(rdev)) {
215 dev_err(dev, "Failed to register regulator!\n");
216 return PTR_ERR(rdev);
217 }
218 }
219
220 return 0;
221}
222
223static const struct of_device_id mp5416_of_match[] = {
Robert Markodf43c242022-06-04 16:58:15 +0200224 { .compatible = "mps,mp5416", .data = &mp5416_regulators_desc },
Robert Markofcdaf742022-06-04 16:58:16 +0200225 { .compatible = "mps,mp5496", .data = &mp5496_regulators_desc },
Saravanan Sekara2731882020-02-04 12:04:18 +0100226 {},
227};
228MODULE_DEVICE_TABLE(of, mp5416_of_match);
229
230static const struct i2c_device_id mp5416_id[] = {
231 { "mp5416", },
Robert Markofcdaf742022-06-04 16:58:16 +0200232 { "mp5496", },
Saravanan Sekara2731882020-02-04 12:04:18 +0100233 { },
234};
235MODULE_DEVICE_TABLE(i2c, mp5416_id);
236
237static struct i2c_driver mp5416_regulator_driver = {
238 .driver = {
239 .name = "mp5416",
Douglas Anderson67dc71c2023-03-16 12:54:41 -0700240 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
Saravanan Sekara2731882020-02-04 12:04:18 +0100241 .of_match_table = of_match_ptr(mp5416_of_match),
242 },
Uwe Kleine-König964e1862023-05-06 00:02:18 +0200243 .probe = mp5416_i2c_probe,
Saravanan Sekara2731882020-02-04 12:04:18 +0100244 .id_table = mp5416_id,
245};
246module_i2c_driver(mp5416_regulator_driver);
247
248MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
249MODULE_DESCRIPTION("MP5416 PMIC regulator driver");
250MODULE_LICENSE("GPL");