1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2018 BayLibre SAS
4 // Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
6 // Regulator driver for MAXIM 77650/77651 charger/power-supply.
9 #include <linux/mfd/max77650.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
13 #include <linux/regulator/driver.h>
15 #define MAX77650_REGULATOR_EN_CTRL_MASK GENMASK(3, 0)
16 #define MAX77650_REGULATOR_EN_CTRL_BITS(_reg) \
17 ((_reg) & MAX77650_REGULATOR_EN_CTRL_MASK)
18 #define MAX77650_REGULATOR_ENABLED GENMASK(2, 1)
19 #define MAX77650_REGULATOR_DISABLED BIT(2)
21 #define MAX77650_REGULATOR_V_LDO_MASK GENMASK(6, 0)
22 #define MAX77650_REGULATOR_V_SBB_MASK GENMASK(5, 0)
23 #define MAX77651_REGULATOR_V_SBB1_MASK GENMASK(5, 2)
24 #define MAX77651_REGULATOR_V_SBB1_RANGE_MASK GENMASK(1, 0)
26 #define MAX77650_REGULATOR_AD_MASK BIT(3)
27 #define MAX77650_REGULATOR_AD_DISABLED 0x00
28 #define MAX77650_REGULATOR_AD_ENABLED BIT(3)
30 #define MAX77650_REGULATOR_CURR_LIM_MASK GENMASK(7, 6)
33 MAX77650_REGULATOR_ID_LDO
= 0,
34 MAX77650_REGULATOR_ID_SBB0
,
35 MAX77650_REGULATOR_ID_SBB1
,
36 MAX77650_REGULATOR_ID_SBB2
,
37 MAX77650_REGULATOR_NUM_REGULATORS
,
40 struct max77650_regulator_desc
{
41 struct regulator_desc desc
;
46 static const unsigned int max77651_sbb1_volt_range_sel
[] = {
50 static const struct linear_range max77651_sbb1_volt_ranges
[] = {
52 REGULATOR_LINEAR_RANGE(2400000, 0x00, 0x0f, 50000),
54 REGULATOR_LINEAR_RANGE(3200000, 0x00, 0x0f, 50000),
56 REGULATOR_LINEAR_RANGE(4000000, 0x00, 0x0f, 50000),
58 REGULATOR_LINEAR_RANGE(4800000, 0x00, 0x09, 50000),
61 static const unsigned int max77650_current_limit_table
[] = {
62 1000000, 866000, 707000, 500000,
65 static int max77650_regulator_is_enabled(struct regulator_dev
*rdev
)
67 const struct max77650_regulator_desc
*rdesc
;
71 rdesc
= container_of(rdev
->desc
, struct max77650_regulator_desc
, desc
);
72 map
= rdev_get_regmap(rdev
);
74 rv
= regmap_read(map
, rdesc
->regB
, &val
);
78 en
= MAX77650_REGULATOR_EN_CTRL_BITS(val
);
80 return en
!= MAX77650_REGULATOR_DISABLED
;
83 static int max77650_regulator_enable(struct regulator_dev
*rdev
)
85 const struct max77650_regulator_desc
*rdesc
;
88 rdesc
= container_of(rdev
->desc
, struct max77650_regulator_desc
, desc
);
89 map
= rdev_get_regmap(rdev
);
91 return regmap_update_bits(map
, rdesc
->regB
,
92 MAX77650_REGULATOR_EN_CTRL_MASK
,
93 MAX77650_REGULATOR_ENABLED
);
96 static int max77650_regulator_disable(struct regulator_dev
*rdev
)
98 const struct max77650_regulator_desc
*rdesc
;
101 rdesc
= container_of(rdev
->desc
, struct max77650_regulator_desc
, desc
);
102 map
= rdev_get_regmap(rdev
);
104 return regmap_update_bits(map
, rdesc
->regB
,
105 MAX77650_REGULATOR_EN_CTRL_MASK
,
106 MAX77650_REGULATOR_DISABLED
);
109 static const struct regulator_ops max77650_regulator_LDO_ops
= {
110 .is_enabled
= max77650_regulator_is_enabled
,
111 .enable
= max77650_regulator_enable
,
112 .disable
= max77650_regulator_disable
,
113 .list_voltage
= regulator_list_voltage_linear
,
114 .map_voltage
= regulator_map_voltage_linear
,
115 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
116 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
117 .set_active_discharge
= regulator_set_active_discharge_regmap
,
120 static const struct regulator_ops max77650_regulator_SBB_ops
= {
121 .is_enabled
= max77650_regulator_is_enabled
,
122 .enable
= max77650_regulator_enable
,
123 .disable
= max77650_regulator_disable
,
124 .list_voltage
= regulator_list_voltage_linear
,
125 .map_voltage
= regulator_map_voltage_linear
,
126 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
127 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
128 .get_current_limit
= regulator_get_current_limit_regmap
,
129 .set_current_limit
= regulator_set_current_limit_regmap
,
130 .set_active_discharge
= regulator_set_active_discharge_regmap
,
133 /* Special case for max77651 SBB1 - pickable linear-range voltage mapping. */
134 static const struct regulator_ops max77651_SBB1_regulator_ops
= {
135 .is_enabled
= max77650_regulator_is_enabled
,
136 .enable
= max77650_regulator_enable
,
137 .disable
= max77650_regulator_disable
,
138 .list_voltage
= regulator_list_voltage_pickable_linear_range
,
139 .get_voltage_sel
= regulator_get_voltage_sel_pickable_regmap
,
140 .set_voltage_sel
= regulator_set_voltage_sel_pickable_regmap
,
141 .get_current_limit
= regulator_get_current_limit_regmap
,
142 .set_current_limit
= regulator_set_current_limit_regmap
,
143 .set_active_discharge
= regulator_set_active_discharge_regmap
,
146 static const struct max77650_regulator_desc max77650_LDO_desc
= {
149 .of_match
= of_match_ptr("ldo"),
150 .regulators_node
= of_match_ptr("regulators"),
151 .supply_name
= "in-ldo",
152 .id
= MAX77650_REGULATOR_ID_LDO
,
153 .ops
= &max77650_regulator_LDO_ops
,
158 .vsel_mask
= MAX77650_REGULATOR_V_LDO_MASK
,
159 .vsel_reg
= MAX77650_REG_CNFG_LDO_A
,
160 .active_discharge_off
= MAX77650_REGULATOR_AD_DISABLED
,
161 .active_discharge_on
= MAX77650_REGULATOR_AD_ENABLED
,
162 .active_discharge_mask
= MAX77650_REGULATOR_AD_MASK
,
163 .active_discharge_reg
= MAX77650_REG_CNFG_LDO_B
,
165 .type
= REGULATOR_VOLTAGE
,
166 .owner
= THIS_MODULE
,
168 .regA
= MAX77650_REG_CNFG_LDO_A
,
169 .regB
= MAX77650_REG_CNFG_LDO_B
,
172 static const struct max77650_regulator_desc max77650_SBB0_desc
= {
175 .of_match
= of_match_ptr("sbb0"),
176 .regulators_node
= of_match_ptr("regulators"),
177 .supply_name
= "in-sbb0",
178 .id
= MAX77650_REGULATOR_ID_SBB0
,
179 .ops
= &max77650_regulator_SBB_ops
,
184 .vsel_mask
= MAX77650_REGULATOR_V_SBB_MASK
,
185 .vsel_reg
= MAX77650_REG_CNFG_SBB0_A
,
186 .active_discharge_off
= MAX77650_REGULATOR_AD_DISABLED
,
187 .active_discharge_on
= MAX77650_REGULATOR_AD_ENABLED
,
188 .active_discharge_mask
= MAX77650_REGULATOR_AD_MASK
,
189 .active_discharge_reg
= MAX77650_REG_CNFG_SBB0_B
,
191 .type
= REGULATOR_VOLTAGE
,
192 .owner
= THIS_MODULE
,
193 .csel_reg
= MAX77650_REG_CNFG_SBB0_A
,
194 .csel_mask
= MAX77650_REGULATOR_CURR_LIM_MASK
,
195 .curr_table
= max77650_current_limit_table
,
196 .n_current_limits
= ARRAY_SIZE(max77650_current_limit_table
),
198 .regA
= MAX77650_REG_CNFG_SBB0_A
,
199 .regB
= MAX77650_REG_CNFG_SBB0_B
,
202 static const struct max77650_regulator_desc max77650_SBB1_desc
= {
205 .of_match
= of_match_ptr("sbb1"),
206 .regulators_node
= of_match_ptr("regulators"),
207 .supply_name
= "in-sbb1",
208 .id
= MAX77650_REGULATOR_ID_SBB1
,
209 .ops
= &max77650_regulator_SBB_ops
,
214 .vsel_mask
= MAX77650_REGULATOR_V_SBB_MASK
,
215 .vsel_reg
= MAX77650_REG_CNFG_SBB1_A
,
216 .active_discharge_off
= MAX77650_REGULATOR_AD_DISABLED
,
217 .active_discharge_on
= MAX77650_REGULATOR_AD_ENABLED
,
218 .active_discharge_mask
= MAX77650_REGULATOR_AD_MASK
,
219 .active_discharge_reg
= MAX77650_REG_CNFG_SBB1_B
,
221 .type
= REGULATOR_VOLTAGE
,
222 .owner
= THIS_MODULE
,
223 .csel_reg
= MAX77650_REG_CNFG_SBB1_A
,
224 .csel_mask
= MAX77650_REGULATOR_CURR_LIM_MASK
,
225 .curr_table
= max77650_current_limit_table
,
226 .n_current_limits
= ARRAY_SIZE(max77650_current_limit_table
),
228 .regA
= MAX77650_REG_CNFG_SBB1_A
,
229 .regB
= MAX77650_REG_CNFG_SBB1_B
,
232 static const struct max77650_regulator_desc max77651_SBB1_desc
= {
235 .of_match
= of_match_ptr("sbb1"),
236 .regulators_node
= of_match_ptr("regulators"),
237 .supply_name
= "in-sbb1",
238 .id
= MAX77650_REGULATOR_ID_SBB1
,
239 .ops
= &max77651_SBB1_regulator_ops
,
240 .linear_range_selectors_bitfield
= max77651_sbb1_volt_range_sel
,
241 .linear_ranges
= max77651_sbb1_volt_ranges
,
242 .n_linear_ranges
= ARRAY_SIZE(max77651_sbb1_volt_ranges
),
245 .vsel_range_mask
= MAX77651_REGULATOR_V_SBB1_RANGE_MASK
,
246 .vsel_range_reg
= MAX77650_REG_CNFG_SBB1_A
,
247 .vsel_mask
= MAX77651_REGULATOR_V_SBB1_MASK
,
248 .vsel_reg
= MAX77650_REG_CNFG_SBB1_A
,
249 .active_discharge_off
= MAX77650_REGULATOR_AD_DISABLED
,
250 .active_discharge_on
= MAX77650_REGULATOR_AD_ENABLED
,
251 .active_discharge_mask
= MAX77650_REGULATOR_AD_MASK
,
252 .active_discharge_reg
= MAX77650_REG_CNFG_SBB1_B
,
254 .type
= REGULATOR_VOLTAGE
,
255 .owner
= THIS_MODULE
,
256 .csel_reg
= MAX77650_REG_CNFG_SBB1_A
,
257 .csel_mask
= MAX77650_REGULATOR_CURR_LIM_MASK
,
258 .curr_table
= max77650_current_limit_table
,
259 .n_current_limits
= ARRAY_SIZE(max77650_current_limit_table
),
261 .regA
= MAX77650_REG_CNFG_SBB1_A
,
262 .regB
= MAX77650_REG_CNFG_SBB1_B
,
265 static const struct max77650_regulator_desc max77650_SBB2_desc
= {
268 .of_match
= of_match_ptr("sbb2"),
269 .regulators_node
= of_match_ptr("regulators"),
270 .supply_name
= "in-sbb0",
271 .id
= MAX77650_REGULATOR_ID_SBB2
,
272 .ops
= &max77650_regulator_SBB_ops
,
277 .vsel_mask
= MAX77650_REGULATOR_V_SBB_MASK
,
278 .vsel_reg
= MAX77650_REG_CNFG_SBB2_A
,
279 .active_discharge_off
= MAX77650_REGULATOR_AD_DISABLED
,
280 .active_discharge_on
= MAX77650_REGULATOR_AD_ENABLED
,
281 .active_discharge_mask
= MAX77650_REGULATOR_AD_MASK
,
282 .active_discharge_reg
= MAX77650_REG_CNFG_SBB2_B
,
284 .type
= REGULATOR_VOLTAGE
,
285 .owner
= THIS_MODULE
,
286 .csel_reg
= MAX77650_REG_CNFG_SBB2_A
,
287 .csel_mask
= MAX77650_REGULATOR_CURR_LIM_MASK
,
288 .curr_table
= max77650_current_limit_table
,
289 .n_current_limits
= ARRAY_SIZE(max77650_current_limit_table
),
291 .regA
= MAX77650_REG_CNFG_SBB2_A
,
292 .regB
= MAX77650_REG_CNFG_SBB2_B
,
295 static const struct max77650_regulator_desc max77651_SBB2_desc
= {
298 .of_match
= of_match_ptr("sbb2"),
299 .regulators_node
= of_match_ptr("regulators"),
300 .supply_name
= "in-sbb0",
301 .id
= MAX77650_REGULATOR_ID_SBB2
,
302 .ops
= &max77650_regulator_SBB_ops
,
307 .vsel_mask
= MAX77650_REGULATOR_V_SBB_MASK
,
308 .vsel_reg
= MAX77650_REG_CNFG_SBB2_A
,
309 .active_discharge_off
= MAX77650_REGULATOR_AD_DISABLED
,
310 .active_discharge_on
= MAX77650_REGULATOR_AD_ENABLED
,
311 .active_discharge_mask
= MAX77650_REGULATOR_AD_MASK
,
312 .active_discharge_reg
= MAX77650_REG_CNFG_SBB2_B
,
314 .type
= REGULATOR_VOLTAGE
,
315 .owner
= THIS_MODULE
,
316 .csel_reg
= MAX77650_REG_CNFG_SBB2_A
,
317 .csel_mask
= MAX77650_REGULATOR_CURR_LIM_MASK
,
318 .curr_table
= max77650_current_limit_table
,
319 .n_current_limits
= ARRAY_SIZE(max77650_current_limit_table
),
321 .regA
= MAX77650_REG_CNFG_SBB2_A
,
322 .regB
= MAX77650_REG_CNFG_SBB2_B
,
325 static int max77650_regulator_probe(struct platform_device
*pdev
)
327 const struct max77650_regulator_desc
**rdescs
;
328 const struct max77650_regulator_desc
*rdesc
;
329 struct regulator_config config
= { };
330 struct device
*dev
, *parent
;
331 struct regulator_dev
*rdev
;
337 parent
= dev
->parent
;
340 dev
->of_node
= parent
->of_node
;
342 rdescs
= devm_kcalloc(dev
, MAX77650_REGULATOR_NUM_REGULATORS
,
343 sizeof(*rdescs
), GFP_KERNEL
);
347 map
= dev_get_regmap(parent
, NULL
);
351 rv
= regmap_read(map
, MAX77650_REG_CID
, &val
);
355 rdescs
[MAX77650_REGULATOR_ID_LDO
] = &max77650_LDO_desc
;
356 rdescs
[MAX77650_REGULATOR_ID_SBB0
] = &max77650_SBB0_desc
;
358 switch (MAX77650_CID_BITS(val
)) {
359 case MAX77650_CID_77650A
:
360 case MAX77650_CID_77650C
:
361 rdescs
[MAX77650_REGULATOR_ID_SBB1
] = &max77650_SBB1_desc
;
362 rdescs
[MAX77650_REGULATOR_ID_SBB2
] = &max77650_SBB2_desc
;
364 case MAX77650_CID_77651A
:
365 case MAX77650_CID_77651B
:
366 rdescs
[MAX77650_REGULATOR_ID_SBB1
] = &max77651_SBB1_desc
;
367 rdescs
[MAX77650_REGULATOR_ID_SBB2
] = &max77651_SBB2_desc
;
375 for (i
= 0; i
< MAX77650_REGULATOR_NUM_REGULATORS
; i
++) {
378 rdev
= devm_regulator_register(dev
, &rdesc
->desc
, &config
);
380 return PTR_ERR(rdev
);
386 static const struct of_device_id max77650_regulator_of_match
[] = {
387 { .compatible
= "maxim,max77650-regulator" },
390 MODULE_DEVICE_TABLE(of
, max77650_regulator_of_match
);
392 static struct platform_driver max77650_regulator_driver
= {
394 .name
= "max77650-regulator",
395 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
396 .of_match_table
= max77650_regulator_of_match
,
398 .probe
= max77650_regulator_probe
,
400 module_platform_driver(max77650_regulator_driver
);
402 MODULE_DESCRIPTION("MAXIM 77650/77651 regulator driver");
403 MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
404 MODULE_LICENSE("GPL v2");
405 MODULE_ALIAS("platform:max77650-regulator");