1 // SPDX-License-Identifier: GPL-2.0+
3 // max14577.c - Regulator driver for the Maxim 14577/77836
5 // Copyright (C) 2013,2014 Samsung Electronics
6 // Krzysztof Kozlowski <krzk@kernel.org>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/regulator/driver.h>
11 #include <linux/mfd/max14577.h>
12 #include <linux/mfd/max14577-private.h>
13 #include <linux/regulator/of_regulator.h>
15 static int max14577_reg_is_enabled(struct regulator_dev
*rdev
)
17 int rid
= rdev_get_id(rdev
);
18 struct regmap
*rmap
= rdev
->regmap
;
22 case MAX14577_CHARGER
:
23 max14577_read_reg(rmap
, MAX14577_CHG_REG_CHG_CTRL2
, ®_data
);
24 if ((reg_data
& CHGCTRL2_MBCHOSTEN_MASK
) == 0)
26 max14577_read_reg(rmap
, MAX14577_CHG_REG_STATUS3
, ®_data
);
27 if ((reg_data
& STATUS3_CGMBC_MASK
) == 0)
29 /* MBCHOSTEN and CGMBC are on */
36 static int max14577_reg_get_current_limit(struct regulator_dev
*rdev
)
39 struct regmap
*rmap
= rdev
->regmap
;
40 struct max14577
*max14577
= rdev_get_drvdata(rdev
);
41 const struct maxim_charger_current
*limits
=
42 &maxim_charger_currents
[max14577
->dev_type
];
44 if (rdev_get_id(rdev
) != MAX14577_CHARGER
)
47 max14577_read_reg(rmap
, MAX14577_CHG_REG_CHG_CTRL4
, ®_data
);
49 if ((reg_data
& CHGCTRL4_MBCICHWRCL_MASK
) == 0)
52 reg_data
= ((reg_data
& CHGCTRL4_MBCICHWRCH_MASK
) >>
53 CHGCTRL4_MBCICHWRCH_SHIFT
);
54 return limits
->high_start
+ reg_data
* limits
->high_step
;
57 static int max14577_reg_set_current_limit(struct regulator_dev
*rdev
,
58 int min_uA
, int max_uA
)
62 struct max14577
*max14577
= rdev_get_drvdata(rdev
);
63 const struct maxim_charger_current
*limits
=
64 &maxim_charger_currents
[max14577
->dev_type
];
66 if (rdev_get_id(rdev
) != MAX14577_CHARGER
)
69 ret
= maxim_charger_calc_reg_current(limits
, min_uA
, max_uA
, ®_data
);
73 return max14577_update_reg(rdev
->regmap
, MAX14577_CHG_REG_CHG_CTRL4
,
74 CHGCTRL4_MBCICHWRCL_MASK
| CHGCTRL4_MBCICHWRCH_MASK
,
78 static const struct regulator_ops max14577_safeout_ops
= {
79 .is_enabled
= regulator_is_enabled_regmap
,
80 .enable
= regulator_enable_regmap
,
81 .disable
= regulator_disable_regmap
,
82 .list_voltage
= regulator_list_voltage_linear
,
85 static const struct regulator_ops max14577_charger_ops
= {
86 .is_enabled
= max14577_reg_is_enabled
,
87 .enable
= regulator_enable_regmap
,
88 .disable
= regulator_disable_regmap
,
89 .get_current_limit
= max14577_reg_get_current_limit
,
90 .set_current_limit
= max14577_reg_set_current_limit
,
93 #define MAX14577_SAFEOUT_REG { \
95 .of_match = of_match_ptr("SAFEOUT"), \
96 .regulators_node = of_match_ptr("regulators"), \
97 .id = MAX14577_SAFEOUT, \
98 .ops = &max14577_safeout_ops, \
99 .type = REGULATOR_VOLTAGE, \
100 .owner = THIS_MODULE, \
102 .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE, \
103 .enable_reg = MAX14577_REG_CONTROL2, \
104 .enable_mask = CTRL2_SFOUTORD_MASK, \
106 #define MAX14577_CHARGER_REG { \
108 .of_match = of_match_ptr("CHARGER"), \
109 .regulators_node = of_match_ptr("regulators"), \
110 .id = MAX14577_CHARGER, \
111 .ops = &max14577_charger_ops, \
112 .type = REGULATOR_CURRENT, \
113 .owner = THIS_MODULE, \
114 .enable_reg = MAX14577_CHG_REG_CHG_CTRL2, \
115 .enable_mask = CHGCTRL2_MBCHOSTEN_MASK, \
118 static const struct regulator_desc max14577_supported_regulators
[] = {
119 [MAX14577_SAFEOUT
] = MAX14577_SAFEOUT_REG
,
120 [MAX14577_CHARGER
] = MAX14577_CHARGER_REG
,
123 static const struct regulator_ops max77836_ldo_ops
= {
124 .is_enabled
= regulator_is_enabled_regmap
,
125 .enable
= regulator_enable_regmap
,
126 .disable
= regulator_disable_regmap
,
127 .list_voltage
= regulator_list_voltage_linear
,
128 .map_voltage
= regulator_map_voltage_linear
,
129 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
130 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
131 /* TODO: add .set_suspend_mode */
134 #define MAX77836_LDO_REG(num) { \
135 .name = "LDO" # num, \
136 .of_match = of_match_ptr("LDO" # num), \
137 .regulators_node = of_match_ptr("regulators"), \
138 .id = MAX77836_LDO ## num, \
139 .ops = &max77836_ldo_ops, \
140 .type = REGULATOR_VOLTAGE, \
141 .owner = THIS_MODULE, \
142 .n_voltages = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM, \
143 .min_uV = MAX77836_REGULATOR_LDO_VOLTAGE_MIN, \
144 .uV_step = MAX77836_REGULATOR_LDO_VOLTAGE_STEP, \
145 .enable_reg = MAX77836_LDO_REG_CNFG1_LDO ## num, \
146 .enable_mask = MAX77836_CNFG1_LDO_PWRMD_MASK, \
147 .vsel_reg = MAX77836_LDO_REG_CNFG1_LDO ## num, \
148 .vsel_mask = MAX77836_CNFG1_LDO_TV_MASK, \
151 static const struct regulator_desc max77836_supported_regulators
[] = {
152 [MAX14577_SAFEOUT
] = MAX14577_SAFEOUT_REG
,
153 [MAX14577_CHARGER
] = MAX14577_CHARGER_REG
,
154 [MAX77836_LDO1
] = MAX77836_LDO_REG(1),
155 [MAX77836_LDO2
] = MAX77836_LDO_REG(2),
159 static struct of_regulator_match max14577_regulator_matches
[] = {
160 { .name
= "SAFEOUT", },
161 { .name
= "CHARGER", },
164 static struct of_regulator_match max77836_regulator_matches
[] = {
165 { .name
= "SAFEOUT", },
166 { .name
= "CHARGER", },
171 static inline struct regulator_init_data
*match_init_data(int index
,
172 enum maxim_device_type dev_type
)
175 case MAXIM_DEVICE_TYPE_MAX77836
:
176 return max77836_regulator_matches
[index
].init_data
;
178 case MAXIM_DEVICE_TYPE_MAX14577
:
180 return max14577_regulator_matches
[index
].init_data
;
184 static inline struct device_node
*match_of_node(int index
,
185 enum maxim_device_type dev_type
)
188 case MAXIM_DEVICE_TYPE_MAX77836
:
189 return max77836_regulator_matches
[index
].of_node
;
191 case MAXIM_DEVICE_TYPE_MAX14577
:
193 return max14577_regulator_matches
[index
].of_node
;
196 #else /* CONFIG_OF */
197 static inline struct regulator_init_data
*match_init_data(int index
,
198 enum maxim_device_type dev_type
)
203 static inline struct device_node
*match_of_node(int index
,
204 enum maxim_device_type dev_type
)
208 #endif /* CONFIG_OF */
211 * Registers for regulators of max77836 use different I2C slave addresses so
212 * different regmaps must be used for them.
214 * Returns proper regmap for accessing regulator passed by id.
216 static struct regmap
*max14577_get_regmap(struct max14577
*max14577
,
219 switch (max14577
->dev_type
) {
220 case MAXIM_DEVICE_TYPE_MAX77836
:
222 case MAX77836_SAFEOUT
... MAX77836_CHARGER
:
223 return max14577
->regmap
;
225 /* MAX77836_LDO1 ... MAX77836_LDO2 */
226 return max14577
->regmap_pmic
;
229 case MAXIM_DEVICE_TYPE_MAX14577
:
231 return max14577
->regmap
;
235 static int max14577_regulator_probe(struct platform_device
*pdev
)
237 struct max14577
*max14577
= dev_get_drvdata(pdev
->dev
.parent
);
238 struct max14577_platform_data
*pdata
= dev_get_platdata(max14577
->dev
);
240 struct regulator_config config
= {};
241 const struct regulator_desc
*supported_regulators
;
242 unsigned int supported_regulators_size
;
243 enum maxim_device_type dev_type
= max14577
->dev_type
;
246 case MAXIM_DEVICE_TYPE_MAX77836
:
247 supported_regulators
= max77836_supported_regulators
;
248 supported_regulators_size
= ARRAY_SIZE(max77836_supported_regulators
);
250 case MAXIM_DEVICE_TYPE_MAX14577
:
252 supported_regulators
= max14577_supported_regulators
;
253 supported_regulators_size
= ARRAY_SIZE(max14577_supported_regulators
);
256 config
.dev
= max14577
->dev
;
257 config
.driver_data
= max14577
;
259 for (i
= 0; i
< supported_regulators_size
; i
++) {
260 struct regulator_dev
*regulator
;
262 * Index of supported_regulators[] is also the id and must
263 * match index of pdata->regulators[].
265 if (pdata
&& pdata
->regulators
) {
266 config
.init_data
= pdata
->regulators
[i
].initdata
;
267 config
.of_node
= pdata
->regulators
[i
].of_node
;
269 config
.init_data
= match_init_data(i
, dev_type
);
270 config
.of_node
= match_of_node(i
, dev_type
);
272 config
.regmap
= max14577_get_regmap(max14577
,
273 supported_regulators
[i
].id
);
275 regulator
= devm_regulator_register(&pdev
->dev
,
276 &supported_regulators
[i
], &config
);
277 if (IS_ERR(regulator
)) {
278 ret
= PTR_ERR(regulator
);
280 "Regulator init failed for %d/%s with error: %d\n",
281 i
, supported_regulators
[i
].name
, ret
);
289 static const struct platform_device_id max14577_regulator_id
[] = {
290 { "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577
, },
291 { "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836
, },
294 MODULE_DEVICE_TABLE(platform
, max14577_regulator_id
);
296 static struct platform_driver max14577_regulator_driver
= {
298 .name
= "max14577-regulator",
300 .probe
= max14577_regulator_probe
,
301 .id_table
= max14577_regulator_id
,
304 static int __init
max14577_regulator_init(void)
306 BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators
) != MAX14577_REGULATOR_NUM
);
307 BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators
) != MAX77836_REGULATOR_NUM
);
309 BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN
+
310 (MAX77836_REGULATOR_LDO_VOLTAGE_STEP
*
311 (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM
- 1)) !=
312 MAX77836_REGULATOR_LDO_VOLTAGE_MAX
);
314 return platform_driver_register(&max14577_regulator_driver
);
316 subsys_initcall(max14577_regulator_init
);
318 static void __exit
max14577_regulator_exit(void)
320 platform_driver_unregister(&max14577_regulator_driver
);
322 module_exit(max14577_regulator_exit
);
324 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
325 MODULE_DESCRIPTION("Maxim 14577/77836 regulator driver");
326 MODULE_LICENSE("GPL");
327 MODULE_ALIAS("platform:max14577-regulator");