xref: /OK3568_Linux_fs/kernel/drivers/regulator/max77693-regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // max77693.c - Regulator driver for the Maxim 77693 and 77843
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright (C) 2013-2015 Samsung Electronics
6*4882a593Smuzhiyun // Jonghwa Lee <jonghwa3.lee@samsung.com>
7*4882a593Smuzhiyun // Krzysztof Kozlowski <krzk@kernel.org>
8*4882a593Smuzhiyun //
9*4882a593Smuzhiyun // This driver is based on max77686.c
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/err.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/platform_device.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/export.h>
16*4882a593Smuzhiyun #include <linux/regulator/driver.h>
17*4882a593Smuzhiyun #include <linux/regulator/machine.h>
18*4882a593Smuzhiyun #include <linux/mfd/max77693.h>
19*4882a593Smuzhiyun #include <linux/mfd/max77693-common.h>
20*4882a593Smuzhiyun #include <linux/mfd/max77693-private.h>
21*4882a593Smuzhiyun #include <linux/mfd/max77843-private.h>
22*4882a593Smuzhiyun #include <linux/regulator/of_regulator.h>
23*4882a593Smuzhiyun #include <linux/regmap.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun  * ID for MAX77843 regulators.
27*4882a593Smuzhiyun  * There is no need for such for MAX77693.
28*4882a593Smuzhiyun  */
29*4882a593Smuzhiyun enum max77843_regulator_type {
30*4882a593Smuzhiyun 	MAX77843_SAFEOUT1 = 0,
31*4882a593Smuzhiyun 	MAX77843_SAFEOUT2,
32*4882a593Smuzhiyun 	MAX77843_CHARGER,
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	MAX77843_NUM,
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /* Register differences between chargers: MAX77693 and MAX77843 */
38*4882a593Smuzhiyun struct chg_reg_data {
39*4882a593Smuzhiyun 	unsigned int linear_reg;
40*4882a593Smuzhiyun 	unsigned int linear_mask;
41*4882a593Smuzhiyun 	unsigned int uA_step;
42*4882a593Smuzhiyun 	unsigned int min_sel;
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun  * MAX77693 CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
47*4882a593Smuzhiyun  * 0x00, 0x01, 0x2, 0x03	= 60 mA
48*4882a593Smuzhiyun  * 0x04 ~ 0x7E			= (60 + (X - 3) * 20) mA
49*4882a593Smuzhiyun  * Actually for MAX77693 the driver manipulates the maximum input current,
50*4882a593Smuzhiyun  * not the fast charge current (output). This should be fixed.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * On MAX77843 the calculation formula is the same (except values).
53*4882a593Smuzhiyun  * Fortunately it properly manipulates the fast charge current.
54*4882a593Smuzhiyun  */
max77693_chg_get_current_limit(struct regulator_dev * rdev)55*4882a593Smuzhiyun static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
58*4882a593Smuzhiyun 	unsigned int chg_min_uA = rdev->constraints->min_uA;
59*4882a593Smuzhiyun 	unsigned int chg_max_uA = rdev->constraints->max_uA;
60*4882a593Smuzhiyun 	unsigned int reg, sel;
61*4882a593Smuzhiyun 	unsigned int val;
62*4882a593Smuzhiyun 	int ret;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	ret = regmap_read(rdev->regmap, reg_data->linear_reg, &reg);
65*4882a593Smuzhiyun 	if (ret < 0)
66*4882a593Smuzhiyun 		return ret;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	sel = reg & reg_data->linear_mask;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	/* the first four codes for charger current are all 60mA */
71*4882a593Smuzhiyun 	if (sel <= reg_data->min_sel)
72*4882a593Smuzhiyun 		sel = 0;
73*4882a593Smuzhiyun 	else
74*4882a593Smuzhiyun 		sel -= reg_data->min_sel;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	val = chg_min_uA + reg_data->uA_step * sel;
77*4882a593Smuzhiyun 	if (val > chg_max_uA)
78*4882a593Smuzhiyun 		return -EINVAL;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	return val;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
max77693_chg_set_current_limit(struct regulator_dev * rdev,int min_uA,int max_uA)83*4882a593Smuzhiyun static int max77693_chg_set_current_limit(struct regulator_dev *rdev,
84*4882a593Smuzhiyun 						int min_uA, int max_uA)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
87*4882a593Smuzhiyun 	unsigned int chg_min_uA = rdev->constraints->min_uA;
88*4882a593Smuzhiyun 	int sel = 0;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	while (chg_min_uA + reg_data->uA_step * sel < min_uA)
91*4882a593Smuzhiyun 		sel++;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	if (chg_min_uA + reg_data->uA_step * sel > max_uA)
94*4882a593Smuzhiyun 		return -EINVAL;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	/* the first four codes for charger current are all 60mA */
97*4882a593Smuzhiyun 	sel += reg_data->min_sel;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	return regmap_write(rdev->regmap, reg_data->linear_reg, sel);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun /* end of CHARGER regulator ops */
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* Returns regmap suitable for given regulator on chosen device */
max77693_get_regmap(enum max77693_types type,struct max77693_dev * max77693,int reg_id)104*4882a593Smuzhiyun static struct regmap *max77693_get_regmap(enum max77693_types type,
105*4882a593Smuzhiyun 					  struct max77693_dev *max77693,
106*4882a593Smuzhiyun 					  int reg_id)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	if (type == TYPE_MAX77693)
109*4882a593Smuzhiyun 		return max77693->regmap;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	/* Else: TYPE_MAX77843 */
112*4882a593Smuzhiyun 	switch (reg_id) {
113*4882a593Smuzhiyun 	case MAX77843_SAFEOUT1:
114*4882a593Smuzhiyun 	case MAX77843_SAFEOUT2:
115*4882a593Smuzhiyun 		return max77693->regmap;
116*4882a593Smuzhiyun 	case MAX77843_CHARGER:
117*4882a593Smuzhiyun 		return max77693->regmap_chg;
118*4882a593Smuzhiyun 	default:
119*4882a593Smuzhiyun 		return max77693->regmap;
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun static const unsigned int max77693_safeout_table[] = {
124*4882a593Smuzhiyun 	4850000,
125*4882a593Smuzhiyun 	4900000,
126*4882a593Smuzhiyun 	4950000,
127*4882a593Smuzhiyun 	3300000,
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun static const struct regulator_ops max77693_safeout_ops = {
131*4882a593Smuzhiyun 	.list_voltage		= regulator_list_voltage_table,
132*4882a593Smuzhiyun 	.is_enabled		= regulator_is_enabled_regmap,
133*4882a593Smuzhiyun 	.enable			= regulator_enable_regmap,
134*4882a593Smuzhiyun 	.disable		= regulator_disable_regmap,
135*4882a593Smuzhiyun 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
136*4882a593Smuzhiyun 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun static const struct regulator_ops max77693_charger_ops = {
140*4882a593Smuzhiyun 	.is_enabled		= regulator_is_enabled_regmap,
141*4882a593Smuzhiyun 	.enable			= regulator_enable_regmap,
142*4882a593Smuzhiyun 	.disable		= regulator_disable_regmap,
143*4882a593Smuzhiyun 	.get_current_limit	= max77693_chg_get_current_limit,
144*4882a593Smuzhiyun 	.set_current_limit	= max77693_chg_set_current_limit,
145*4882a593Smuzhiyun };
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun #define max77693_regulator_desc_esafeout(_num)	{		\
148*4882a593Smuzhiyun 	.name		= "ESAFEOUT"#_num,			\
149*4882a593Smuzhiyun 	.id		= MAX77693_ESAFEOUT##_num,		\
150*4882a593Smuzhiyun 	.of_match	= of_match_ptr("ESAFEOUT"#_num),	\
151*4882a593Smuzhiyun 	.regulators_node	= of_match_ptr("regulators"),	\
152*4882a593Smuzhiyun 	.n_voltages	= 4,					\
153*4882a593Smuzhiyun 	.ops		= &max77693_safeout_ops,		\
154*4882a593Smuzhiyun 	.type		= REGULATOR_VOLTAGE,			\
155*4882a593Smuzhiyun 	.owner		= THIS_MODULE,				\
156*4882a593Smuzhiyun 	.volt_table	= max77693_safeout_table,		\
157*4882a593Smuzhiyun 	.vsel_reg	= MAX77693_CHG_REG_SAFEOUT_CTRL,	\
158*4882a593Smuzhiyun 	.vsel_mask	= SAFEOUT_CTRL_SAFEOUT##_num##_MASK,	\
159*4882a593Smuzhiyun 	.enable_reg	= MAX77693_CHG_REG_SAFEOUT_CTRL,	\
160*4882a593Smuzhiyun 	.enable_mask	= SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK ,	\
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun static const struct regulator_desc max77693_supported_regulators[] = {
164*4882a593Smuzhiyun 	max77693_regulator_desc_esafeout(1),
165*4882a593Smuzhiyun 	max77693_regulator_desc_esafeout(2),
166*4882a593Smuzhiyun 	{
167*4882a593Smuzhiyun 		.name = "CHARGER",
168*4882a593Smuzhiyun 		.id = MAX77693_CHARGER,
169*4882a593Smuzhiyun 		.of_match = of_match_ptr("CHARGER"),
170*4882a593Smuzhiyun 		.regulators_node = of_match_ptr("regulators"),
171*4882a593Smuzhiyun 		.ops = &max77693_charger_ops,
172*4882a593Smuzhiyun 		.type = REGULATOR_CURRENT,
173*4882a593Smuzhiyun 		.owner = THIS_MODULE,
174*4882a593Smuzhiyun 		.enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
175*4882a593Smuzhiyun 		.enable_mask = CHG_CNFG_00_CHG_MASK |
176*4882a593Smuzhiyun 				CHG_CNFG_00_BUCK_MASK,
177*4882a593Smuzhiyun 		.enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK,
178*4882a593Smuzhiyun 	},
179*4882a593Smuzhiyun };
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun static const struct chg_reg_data max77693_chg_reg_data = {
182*4882a593Smuzhiyun 	.linear_reg	= MAX77693_CHG_REG_CHG_CNFG_09,
183*4882a593Smuzhiyun 	.linear_mask	= CHG_CNFG_09_CHGIN_ILIM_MASK,
184*4882a593Smuzhiyun 	.uA_step	= 20000,
185*4882a593Smuzhiyun 	.min_sel	= 3,
186*4882a593Smuzhiyun };
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun #define	max77843_regulator_desc_esafeout(num)	{			\
189*4882a593Smuzhiyun 	.name		= "SAFEOUT" # num,				\
190*4882a593Smuzhiyun 	.id		= MAX77843_SAFEOUT ## num,			\
191*4882a593Smuzhiyun 	.ops		= &max77693_safeout_ops,			\
192*4882a593Smuzhiyun 	.of_match	= of_match_ptr("SAFEOUT" # num),		\
193*4882a593Smuzhiyun 	.regulators_node = of_match_ptr("regulators"),			\
194*4882a593Smuzhiyun 	.type		= REGULATOR_VOLTAGE,				\
195*4882a593Smuzhiyun 	.owner		= THIS_MODULE,					\
196*4882a593Smuzhiyun 	.n_voltages	= ARRAY_SIZE(max77693_safeout_table),		\
197*4882a593Smuzhiyun 	.volt_table	= max77693_safeout_table,			\
198*4882a593Smuzhiyun 	.enable_reg	= MAX77843_SYS_REG_SAFEOUTCTRL,			\
199*4882a593Smuzhiyun 	.enable_mask	= MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT ## num,	\
200*4882a593Smuzhiyun 	.vsel_reg	= MAX77843_SYS_REG_SAFEOUTCTRL,			\
201*4882a593Smuzhiyun 	.vsel_mask	= MAX77843_REG_SAFEOUTCTRL_SAFEOUT ## num ## _MASK, \
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun static const struct regulator_desc max77843_supported_regulators[] = {
205*4882a593Smuzhiyun 	[MAX77843_SAFEOUT1] = max77843_regulator_desc_esafeout(1),
206*4882a593Smuzhiyun 	[MAX77843_SAFEOUT2] = max77843_regulator_desc_esafeout(2),
207*4882a593Smuzhiyun 	[MAX77843_CHARGER] = {
208*4882a593Smuzhiyun 		.name		= "CHARGER",
209*4882a593Smuzhiyun 		.id		= MAX77843_CHARGER,
210*4882a593Smuzhiyun 		.ops		= &max77693_charger_ops,
211*4882a593Smuzhiyun 		.of_match	= of_match_ptr("CHARGER"),
212*4882a593Smuzhiyun 		.regulators_node = of_match_ptr("regulators"),
213*4882a593Smuzhiyun 		.type		= REGULATOR_CURRENT,
214*4882a593Smuzhiyun 		.owner		= THIS_MODULE,
215*4882a593Smuzhiyun 		.enable_reg	= MAX77843_CHG_REG_CHG_CNFG_00,
216*4882a593Smuzhiyun 		.enable_mask	= MAX77843_CHG_MASK,
217*4882a593Smuzhiyun 		.enable_val	= MAX77843_CHG_MASK,
218*4882a593Smuzhiyun 	},
219*4882a593Smuzhiyun };
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun static const struct chg_reg_data max77843_chg_reg_data = {
222*4882a593Smuzhiyun 	.linear_reg	= MAX77843_CHG_REG_CHG_CNFG_02,
223*4882a593Smuzhiyun 	.linear_mask	= MAX77843_CHG_FAST_CHG_CURRENT_MASK,
224*4882a593Smuzhiyun 	.uA_step	= MAX77843_CHG_FAST_CHG_CURRENT_STEP,
225*4882a593Smuzhiyun 	.min_sel	= 2,
226*4882a593Smuzhiyun };
227*4882a593Smuzhiyun 
max77693_pmic_probe(struct platform_device * pdev)228*4882a593Smuzhiyun static int max77693_pmic_probe(struct platform_device *pdev)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	enum max77693_types type = platform_get_device_id(pdev)->driver_data;
231*4882a593Smuzhiyun 	struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
232*4882a593Smuzhiyun 	const struct regulator_desc *regulators;
233*4882a593Smuzhiyun 	unsigned int regulators_size;
234*4882a593Smuzhiyun 	int i;
235*4882a593Smuzhiyun 	struct regulator_config config = { };
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	config.dev = iodev->dev;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	switch (type) {
240*4882a593Smuzhiyun 	case TYPE_MAX77693:
241*4882a593Smuzhiyun 		regulators = max77693_supported_regulators;
242*4882a593Smuzhiyun 		regulators_size = ARRAY_SIZE(max77693_supported_regulators);
243*4882a593Smuzhiyun 		config.driver_data = (void *)&max77693_chg_reg_data;
244*4882a593Smuzhiyun 		break;
245*4882a593Smuzhiyun 	case TYPE_MAX77843:
246*4882a593Smuzhiyun 		regulators = max77843_supported_regulators;
247*4882a593Smuzhiyun 		regulators_size = ARRAY_SIZE(max77843_supported_regulators);
248*4882a593Smuzhiyun 		config.driver_data = (void *)&max77843_chg_reg_data;
249*4882a593Smuzhiyun 		break;
250*4882a593Smuzhiyun 	default:
251*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Unsupported device type: %u\n", type);
252*4882a593Smuzhiyun 		return -ENODEV;
253*4882a593Smuzhiyun 	}
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	for (i = 0; i < regulators_size; i++) {
256*4882a593Smuzhiyun 		struct regulator_dev *rdev;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 		config.regmap = max77693_get_regmap(type, iodev,
259*4882a593Smuzhiyun 						    regulators[i].id);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 		rdev = devm_regulator_register(&pdev->dev,
262*4882a593Smuzhiyun 						&regulators[i], &config);
263*4882a593Smuzhiyun 		if (IS_ERR(rdev)) {
264*4882a593Smuzhiyun 			dev_err(&pdev->dev,
265*4882a593Smuzhiyun 				"Failed to initialize regulator-%d\n", i);
266*4882a593Smuzhiyun 			return PTR_ERR(rdev);
267*4882a593Smuzhiyun 		}
268*4882a593Smuzhiyun 	}
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	return 0;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun static const struct platform_device_id max77693_pmic_id[] = {
274*4882a593Smuzhiyun 	{ "max77693-pmic", TYPE_MAX77693 },
275*4882a593Smuzhiyun 	{ "max77843-regulator", TYPE_MAX77843 },
276*4882a593Smuzhiyun 	{},
277*4882a593Smuzhiyun };
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun static struct platform_driver max77693_pmic_driver = {
282*4882a593Smuzhiyun 	.driver = {
283*4882a593Smuzhiyun 		   .name = "max77693-pmic",
284*4882a593Smuzhiyun 		   },
285*4882a593Smuzhiyun 	.probe = max77693_pmic_probe,
286*4882a593Smuzhiyun 	.id_table = max77693_pmic_id,
287*4882a593Smuzhiyun };
288*4882a593Smuzhiyun 
max77693_pmic_init(void)289*4882a593Smuzhiyun static int __init max77693_pmic_init(void)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	return platform_driver_register(&max77693_pmic_driver);
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun subsys_initcall(max77693_pmic_init);
294*4882a593Smuzhiyun 
max77693_pmic_cleanup(void)295*4882a593Smuzhiyun static void __exit max77693_pmic_cleanup(void)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun 	platform_driver_unregister(&max77693_pmic_driver);
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun module_exit(max77693_pmic_cleanup);
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun MODULE_DESCRIPTION("MAXIM 77693/77843 regulator driver");
302*4882a593Smuzhiyun MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
303*4882a593Smuzhiyun MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
304*4882a593Smuzhiyun MODULE_LICENSE("GPL");
305