xref: /OK3568_Linux_fs/kernel/drivers/regulator/max14577-regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // max14577.c - Regulator driver for the Maxim 14577/77836
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright (C) 2013,2014 Samsung Electronics
6*4882a593Smuzhiyun // Krzysztof Kozlowski <krzk@kernel.org>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/platform_device.h>
10*4882a593Smuzhiyun #include <linux/regulator/driver.h>
11*4882a593Smuzhiyun #include <linux/mfd/max14577.h>
12*4882a593Smuzhiyun #include <linux/mfd/max14577-private.h>
13*4882a593Smuzhiyun #include <linux/regulator/of_regulator.h>
14*4882a593Smuzhiyun 
max14577_reg_is_enabled(struct regulator_dev * rdev)15*4882a593Smuzhiyun static int max14577_reg_is_enabled(struct regulator_dev *rdev)
16*4882a593Smuzhiyun {
17*4882a593Smuzhiyun 	int rid = rdev_get_id(rdev);
18*4882a593Smuzhiyun 	struct regmap *rmap = rdev->regmap;
19*4882a593Smuzhiyun 	u8 reg_data;
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun 	switch (rid) {
22*4882a593Smuzhiyun 	case MAX14577_CHARGER:
23*4882a593Smuzhiyun 		max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
24*4882a593Smuzhiyun 		if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
25*4882a593Smuzhiyun 			return 0;
26*4882a593Smuzhiyun 		max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
27*4882a593Smuzhiyun 		if ((reg_data & STATUS3_CGMBC_MASK) == 0)
28*4882a593Smuzhiyun 			return 0;
29*4882a593Smuzhiyun 		/* MBCHOSTEN and CGMBC are on */
30*4882a593Smuzhiyun 		return 1;
31*4882a593Smuzhiyun 	default:
32*4882a593Smuzhiyun 		return -EINVAL;
33*4882a593Smuzhiyun 	}
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun 
max14577_reg_get_current_limit(struct regulator_dev * rdev)36*4882a593Smuzhiyun static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	u8 reg_data;
39*4882a593Smuzhiyun 	struct regmap *rmap = rdev->regmap;
40*4882a593Smuzhiyun 	struct max14577 *max14577 = rdev_get_drvdata(rdev);
41*4882a593Smuzhiyun 	const struct maxim_charger_current *limits =
42*4882a593Smuzhiyun 		&maxim_charger_currents[max14577->dev_type];
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	if (rdev_get_id(rdev) != MAX14577_CHARGER)
45*4882a593Smuzhiyun 		return -EINVAL;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
50*4882a593Smuzhiyun 		return limits->min;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
53*4882a593Smuzhiyun 			CHGCTRL4_MBCICHWRCH_SHIFT);
54*4882a593Smuzhiyun 	return limits->high_start + reg_data * limits->high_step;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
max14577_reg_set_current_limit(struct regulator_dev * rdev,int min_uA,int max_uA)57*4882a593Smuzhiyun static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
58*4882a593Smuzhiyun 		int min_uA, int max_uA)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	u8 reg_data;
61*4882a593Smuzhiyun 	int ret;
62*4882a593Smuzhiyun 	struct max14577 *max14577 = rdev_get_drvdata(rdev);
63*4882a593Smuzhiyun 	const struct maxim_charger_current *limits =
64*4882a593Smuzhiyun 		&maxim_charger_currents[max14577->dev_type];
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	if (rdev_get_id(rdev) != MAX14577_CHARGER)
67*4882a593Smuzhiyun 		return -EINVAL;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	ret = maxim_charger_calc_reg_current(limits, min_uA, max_uA, &reg_data);
70*4882a593Smuzhiyun 	if (ret)
71*4882a593Smuzhiyun 		return ret;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
74*4882a593Smuzhiyun 			CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
75*4882a593Smuzhiyun 			reg_data);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun static const struct regulator_ops max14577_safeout_ops = {
79*4882a593Smuzhiyun 	.is_enabled		= regulator_is_enabled_regmap,
80*4882a593Smuzhiyun 	.enable			= regulator_enable_regmap,
81*4882a593Smuzhiyun 	.disable		= regulator_disable_regmap,
82*4882a593Smuzhiyun 	.list_voltage		= regulator_list_voltage_linear,
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun static const struct regulator_ops max14577_charger_ops = {
86*4882a593Smuzhiyun 	.is_enabled		= max14577_reg_is_enabled,
87*4882a593Smuzhiyun 	.enable			= regulator_enable_regmap,
88*4882a593Smuzhiyun 	.disable		= regulator_disable_regmap,
89*4882a593Smuzhiyun 	.get_current_limit	= max14577_reg_get_current_limit,
90*4882a593Smuzhiyun 	.set_current_limit	= max14577_reg_set_current_limit,
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun #define MAX14577_SAFEOUT_REG	{ \
94*4882a593Smuzhiyun 	.name		= "SAFEOUT", \
95*4882a593Smuzhiyun 	.of_match	= of_match_ptr("SAFEOUT"), \
96*4882a593Smuzhiyun 	.regulators_node = of_match_ptr("regulators"), \
97*4882a593Smuzhiyun 	.id		= MAX14577_SAFEOUT, \
98*4882a593Smuzhiyun 	.ops		= &max14577_safeout_ops, \
99*4882a593Smuzhiyun 	.type		= REGULATOR_VOLTAGE, \
100*4882a593Smuzhiyun 	.owner		= THIS_MODULE, \
101*4882a593Smuzhiyun 	.n_voltages	= 1, \
102*4882a593Smuzhiyun 	.min_uV		= MAX14577_REGULATOR_SAFEOUT_VOLTAGE, \
103*4882a593Smuzhiyun 	.enable_reg	= MAX14577_REG_CONTROL2, \
104*4882a593Smuzhiyun 	.enable_mask	= CTRL2_SFOUTORD_MASK, \
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun #define MAX14577_CHARGER_REG	{ \
107*4882a593Smuzhiyun 	.name		= "CHARGER", \
108*4882a593Smuzhiyun 	.of_match	= of_match_ptr("CHARGER"), \
109*4882a593Smuzhiyun 	.regulators_node = of_match_ptr("regulators"), \
110*4882a593Smuzhiyun 	.id		= MAX14577_CHARGER, \
111*4882a593Smuzhiyun 	.ops		= &max14577_charger_ops, \
112*4882a593Smuzhiyun 	.type		= REGULATOR_CURRENT, \
113*4882a593Smuzhiyun 	.owner		= THIS_MODULE, \
114*4882a593Smuzhiyun 	.enable_reg	= MAX14577_CHG_REG_CHG_CTRL2, \
115*4882a593Smuzhiyun 	.enable_mask	= CHGCTRL2_MBCHOSTEN_MASK, \
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun static const struct regulator_desc max14577_supported_regulators[] = {
119*4882a593Smuzhiyun 	[MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG,
120*4882a593Smuzhiyun 	[MAX14577_CHARGER] = MAX14577_CHARGER_REG,
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun static const struct regulator_ops max77836_ldo_ops = {
124*4882a593Smuzhiyun 	.is_enabled		= regulator_is_enabled_regmap,
125*4882a593Smuzhiyun 	.enable			= regulator_enable_regmap,
126*4882a593Smuzhiyun 	.disable		= regulator_disable_regmap,
127*4882a593Smuzhiyun 	.list_voltage		= regulator_list_voltage_linear,
128*4882a593Smuzhiyun 	.map_voltage		= regulator_map_voltage_linear,
129*4882a593Smuzhiyun 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
130*4882a593Smuzhiyun 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
131*4882a593Smuzhiyun 	/* TODO: add .set_suspend_mode */
132*4882a593Smuzhiyun };
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun #define MAX77836_LDO_REG(num)	{ \
135*4882a593Smuzhiyun 	.name		= "LDO" # num, \
136*4882a593Smuzhiyun 	.of_match	= of_match_ptr("LDO" # num), \
137*4882a593Smuzhiyun 	.regulators_node = of_match_ptr("regulators"), \
138*4882a593Smuzhiyun 	.id		= MAX77836_LDO ## num, \
139*4882a593Smuzhiyun 	.ops		= &max77836_ldo_ops, \
140*4882a593Smuzhiyun 	.type		= REGULATOR_VOLTAGE, \
141*4882a593Smuzhiyun 	.owner		= THIS_MODULE, \
142*4882a593Smuzhiyun 	.n_voltages	= MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM, \
143*4882a593Smuzhiyun 	.min_uV		= MAX77836_REGULATOR_LDO_VOLTAGE_MIN, \
144*4882a593Smuzhiyun 	.uV_step	= MAX77836_REGULATOR_LDO_VOLTAGE_STEP, \
145*4882a593Smuzhiyun 	.enable_reg	= MAX77836_LDO_REG_CNFG1_LDO ## num, \
146*4882a593Smuzhiyun 	.enable_mask	= MAX77836_CNFG1_LDO_PWRMD_MASK, \
147*4882a593Smuzhiyun 	.vsel_reg	= MAX77836_LDO_REG_CNFG1_LDO ## num, \
148*4882a593Smuzhiyun 	.vsel_mask	= MAX77836_CNFG1_LDO_TV_MASK, \
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun static const struct regulator_desc max77836_supported_regulators[] = {
152*4882a593Smuzhiyun 	[MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG,
153*4882a593Smuzhiyun 	[MAX14577_CHARGER] = MAX14577_CHARGER_REG,
154*4882a593Smuzhiyun 	[MAX77836_LDO1] = MAX77836_LDO_REG(1),
155*4882a593Smuzhiyun 	[MAX77836_LDO2] = MAX77836_LDO_REG(2),
156*4882a593Smuzhiyun };
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun  * Registers for regulators of max77836 use different I2C slave addresses so
160*4882a593Smuzhiyun  * different regmaps must be used for them.
161*4882a593Smuzhiyun  *
162*4882a593Smuzhiyun  * Returns proper regmap for accessing regulator passed by id.
163*4882a593Smuzhiyun  */
max14577_get_regmap(struct max14577 * max14577,int reg_id)164*4882a593Smuzhiyun static struct regmap *max14577_get_regmap(struct max14577 *max14577,
165*4882a593Smuzhiyun 		int reg_id)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	switch (max14577->dev_type) {
168*4882a593Smuzhiyun 	case MAXIM_DEVICE_TYPE_MAX77836:
169*4882a593Smuzhiyun 		switch (reg_id) {
170*4882a593Smuzhiyun 		case MAX77836_SAFEOUT ... MAX77836_CHARGER:
171*4882a593Smuzhiyun 			return max14577->regmap;
172*4882a593Smuzhiyun 		default:
173*4882a593Smuzhiyun 			/* MAX77836_LDO1 ... MAX77836_LDO2 */
174*4882a593Smuzhiyun 			return max14577->regmap_pmic;
175*4882a593Smuzhiyun 		}
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	case MAXIM_DEVICE_TYPE_MAX14577:
178*4882a593Smuzhiyun 	default:
179*4882a593Smuzhiyun 		return max14577->regmap;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
max14577_regulator_probe(struct platform_device * pdev)183*4882a593Smuzhiyun static int max14577_regulator_probe(struct platform_device *pdev)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
186*4882a593Smuzhiyun 	struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
187*4882a593Smuzhiyun 	int i, ret = 0;
188*4882a593Smuzhiyun 	struct regulator_config config = {};
189*4882a593Smuzhiyun 	const struct regulator_desc *supported_regulators;
190*4882a593Smuzhiyun 	unsigned int supported_regulators_size;
191*4882a593Smuzhiyun 	enum maxim_device_type dev_type = max14577->dev_type;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	switch (dev_type) {
194*4882a593Smuzhiyun 	case MAXIM_DEVICE_TYPE_MAX77836:
195*4882a593Smuzhiyun 		supported_regulators = max77836_supported_regulators;
196*4882a593Smuzhiyun 		supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators);
197*4882a593Smuzhiyun 		break;
198*4882a593Smuzhiyun 	case MAXIM_DEVICE_TYPE_MAX14577:
199*4882a593Smuzhiyun 	default:
200*4882a593Smuzhiyun 		supported_regulators = max14577_supported_regulators;
201*4882a593Smuzhiyun 		supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators);
202*4882a593Smuzhiyun 	}
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	config.dev = max14577->dev;
205*4882a593Smuzhiyun 	config.driver_data = max14577;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	for (i = 0; i < supported_regulators_size; i++) {
208*4882a593Smuzhiyun 		struct regulator_dev *regulator;
209*4882a593Smuzhiyun 		/*
210*4882a593Smuzhiyun 		 * Index of supported_regulators[] is also the id and must
211*4882a593Smuzhiyun 		 * match index of pdata->regulators[].
212*4882a593Smuzhiyun 		 */
213*4882a593Smuzhiyun 		if (pdata && pdata->regulators) {
214*4882a593Smuzhiyun 			config.init_data = pdata->regulators[i].initdata;
215*4882a593Smuzhiyun 			config.of_node = pdata->regulators[i].of_node;
216*4882a593Smuzhiyun 		}
217*4882a593Smuzhiyun 		config.regmap = max14577_get_regmap(max14577,
218*4882a593Smuzhiyun 				supported_regulators[i].id);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 		regulator = devm_regulator_register(&pdev->dev,
221*4882a593Smuzhiyun 				&supported_regulators[i], &config);
222*4882a593Smuzhiyun 		if (IS_ERR(regulator)) {
223*4882a593Smuzhiyun 			ret = PTR_ERR(regulator);
224*4882a593Smuzhiyun 			dev_err(&pdev->dev,
225*4882a593Smuzhiyun 					"Regulator init failed for %d/%s with error: %d\n",
226*4882a593Smuzhiyun 					i, supported_regulators[i].name, ret);
227*4882a593Smuzhiyun 			return ret;
228*4882a593Smuzhiyun 		}
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	return ret;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun static const struct platform_device_id max14577_regulator_id[] = {
235*4882a593Smuzhiyun 	{ "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, },
236*4882a593Smuzhiyun 	{ "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, },
237*4882a593Smuzhiyun 	{ }
238*4882a593Smuzhiyun };
239*4882a593Smuzhiyun MODULE_DEVICE_TABLE(platform, max14577_regulator_id);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun static struct platform_driver max14577_regulator_driver = {
242*4882a593Smuzhiyun 	.driver = {
243*4882a593Smuzhiyun 		   .name = "max14577-regulator",
244*4882a593Smuzhiyun 		   },
245*4882a593Smuzhiyun 	.probe		= max14577_regulator_probe,
246*4882a593Smuzhiyun 	.id_table	= max14577_regulator_id,
247*4882a593Smuzhiyun };
248*4882a593Smuzhiyun 
max14577_regulator_init(void)249*4882a593Smuzhiyun static int __init max14577_regulator_init(void)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM);
252*4882a593Smuzhiyun 	BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN +
255*4882a593Smuzhiyun 			(MAX77836_REGULATOR_LDO_VOLTAGE_STEP *
256*4882a593Smuzhiyun 			  (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) !=
257*4882a593Smuzhiyun 			MAX77836_REGULATOR_LDO_VOLTAGE_MAX);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	return platform_driver_register(&max14577_regulator_driver);
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun subsys_initcall(max14577_regulator_init);
262*4882a593Smuzhiyun 
max14577_regulator_exit(void)263*4882a593Smuzhiyun static void __exit max14577_regulator_exit(void)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun 	platform_driver_unregister(&max14577_regulator_driver);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun module_exit(max14577_regulator_exit);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
270*4882a593Smuzhiyun MODULE_DESCRIPTION("Maxim 14577/77836 regulator driver");
271*4882a593Smuzhiyun MODULE_LICENSE("GPL");
272