xref: /OK3568_Linux_fs/kernel/drivers/regulator/tps65912-regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Regulator driver for TI TPS65912x PMICs
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com/
5*4882a593Smuzhiyun  *	Andrew F. Davis <afd@ti.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or
8*4882a593Smuzhiyun  * modify it under the terms of the GNU General Public License version 2 as
9*4882a593Smuzhiyun  * published by the Free Software Foundation.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12*4882a593Smuzhiyun  * kind, whether expressed or implied; without even the implied warranty
13*4882a593Smuzhiyun  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*4882a593Smuzhiyun  * GNU General Public License version 2 for more details.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Based on the TPS65218 driver and the previous TPS65912 driver by
17*4882a593Smuzhiyun  * Margarita Olaya Cabrera <magi@slimlogic.co.uk>
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
22*4882a593Smuzhiyun #include <linux/platform_device.h>
23*4882a593Smuzhiyun #include <linux/regulator/driver.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <linux/mfd/tps65912.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun enum tps65912_regulators { DCDC1, DCDC2, DCDC3, DCDC4, LDO1, LDO2, LDO3,
28*4882a593Smuzhiyun 	LDO4, LDO5, LDO6, LDO7, LDO8, LDO9, LDO10 };
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define TPS65912_REGULATOR(_name, _id, _of_match, _ops, _vr, _er, _lr)	\
31*4882a593Smuzhiyun 	[_id] = {							\
32*4882a593Smuzhiyun 		.name			= _name,			\
33*4882a593Smuzhiyun 		.of_match		= _of_match,			\
34*4882a593Smuzhiyun 		.regulators_node	= "regulators",			\
35*4882a593Smuzhiyun 		.id			= _id,				\
36*4882a593Smuzhiyun 		.ops			= &_ops,			\
37*4882a593Smuzhiyun 		.n_voltages		= 64,				\
38*4882a593Smuzhiyun 		.type			= REGULATOR_VOLTAGE,		\
39*4882a593Smuzhiyun 		.owner			= THIS_MODULE,			\
40*4882a593Smuzhiyun 		.vsel_reg		= _vr,				\
41*4882a593Smuzhiyun 		.vsel_mask		= 0x3f,				\
42*4882a593Smuzhiyun 		.enable_reg		= _er,				\
43*4882a593Smuzhiyun 		.enable_mask		= BIT(7),			\
44*4882a593Smuzhiyun 		.volt_table		= NULL,				\
45*4882a593Smuzhiyun 		.linear_ranges		= _lr,				\
46*4882a593Smuzhiyun 		.n_linear_ranges	= ARRAY_SIZE(_lr),		\
47*4882a593Smuzhiyun 	}
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun static const struct linear_range tps65912_dcdc_ranges[] = {
50*4882a593Smuzhiyun 	REGULATOR_LINEAR_RANGE(500000, 0x0, 0x3f, 50000),
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static const struct linear_range tps65912_ldo_ranges[] = {
54*4882a593Smuzhiyun 	REGULATOR_LINEAR_RANGE(800000, 0x0, 0x20, 25000),
55*4882a593Smuzhiyun 	REGULATOR_LINEAR_RANGE(1650000, 0x21, 0x3c, 50000),
56*4882a593Smuzhiyun 	REGULATOR_LINEAR_RANGE(3100000, 0x3d, 0x3f, 100000),
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /* Operations permitted on DCDCx */
60*4882a593Smuzhiyun static const struct regulator_ops tps65912_ops_dcdc = {
61*4882a593Smuzhiyun 	.is_enabled		= regulator_is_enabled_regmap,
62*4882a593Smuzhiyun 	.enable			= regulator_enable_regmap,
63*4882a593Smuzhiyun 	.disable		= regulator_disable_regmap,
64*4882a593Smuzhiyun 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
65*4882a593Smuzhiyun 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
66*4882a593Smuzhiyun 	.list_voltage		= regulator_list_voltage_linear_range,
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* Operations permitted on LDOx */
70*4882a593Smuzhiyun static const struct regulator_ops tps65912_ops_ldo = {
71*4882a593Smuzhiyun 	.is_enabled		= regulator_is_enabled_regmap,
72*4882a593Smuzhiyun 	.enable			= regulator_enable_regmap,
73*4882a593Smuzhiyun 	.disable		= regulator_disable_regmap,
74*4882a593Smuzhiyun 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
75*4882a593Smuzhiyun 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
76*4882a593Smuzhiyun 	.list_voltage		= regulator_list_voltage_linear_range,
77*4882a593Smuzhiyun 	.map_voltage		= regulator_map_voltage_linear_range,
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun static const struct regulator_desc regulators[] = {
81*4882a593Smuzhiyun 	TPS65912_REGULATOR("DCDC1", DCDC1, "dcdc1", tps65912_ops_dcdc,
82*4882a593Smuzhiyun 			   TPS65912_DCDC1_OP, TPS65912_DCDC1_CTRL,
83*4882a593Smuzhiyun 			   tps65912_dcdc_ranges),
84*4882a593Smuzhiyun 	TPS65912_REGULATOR("DCDC2", DCDC2, "dcdc2", tps65912_ops_dcdc,
85*4882a593Smuzhiyun 			   TPS65912_DCDC2_OP, TPS65912_DCDC2_CTRL,
86*4882a593Smuzhiyun 			   tps65912_dcdc_ranges),
87*4882a593Smuzhiyun 	TPS65912_REGULATOR("DCDC3", DCDC3, "dcdc3", tps65912_ops_dcdc,
88*4882a593Smuzhiyun 			   TPS65912_DCDC3_OP, TPS65912_DCDC3_CTRL,
89*4882a593Smuzhiyun 			   tps65912_dcdc_ranges),
90*4882a593Smuzhiyun 	TPS65912_REGULATOR("DCDC4", DCDC4, "dcdc4", tps65912_ops_dcdc,
91*4882a593Smuzhiyun 			   TPS65912_DCDC4_OP, TPS65912_DCDC4_CTRL,
92*4882a593Smuzhiyun 			   tps65912_dcdc_ranges),
93*4882a593Smuzhiyun 	TPS65912_REGULATOR("LDO1", LDO1, "ldo1", tps65912_ops_ldo,
94*4882a593Smuzhiyun 			   TPS65912_LDO1_OP, TPS65912_LDO1_AVS,
95*4882a593Smuzhiyun 			   tps65912_ldo_ranges),
96*4882a593Smuzhiyun 	TPS65912_REGULATOR("LDO2", LDO2, "ldo2", tps65912_ops_ldo,
97*4882a593Smuzhiyun 			   TPS65912_LDO2_OP, TPS65912_LDO2_AVS,
98*4882a593Smuzhiyun 			   tps65912_ldo_ranges),
99*4882a593Smuzhiyun 	TPS65912_REGULATOR("LDO3", LDO3, "ldo3", tps65912_ops_ldo,
100*4882a593Smuzhiyun 			   TPS65912_LDO3_OP, TPS65912_LDO3_AVS,
101*4882a593Smuzhiyun 			   tps65912_ldo_ranges),
102*4882a593Smuzhiyun 	TPS65912_REGULATOR("LDO4", LDO4, "ldo4", tps65912_ops_ldo,
103*4882a593Smuzhiyun 			   TPS65912_LDO4_OP, TPS65912_LDO4_AVS,
104*4882a593Smuzhiyun 			   tps65912_ldo_ranges),
105*4882a593Smuzhiyun 	TPS65912_REGULATOR("LDO5", LDO5, "ldo5", tps65912_ops_ldo,
106*4882a593Smuzhiyun 			   TPS65912_LDO5, TPS65912_LDO5,
107*4882a593Smuzhiyun 			   tps65912_ldo_ranges),
108*4882a593Smuzhiyun 	TPS65912_REGULATOR("LDO6", LDO6, "ldo6", tps65912_ops_ldo,
109*4882a593Smuzhiyun 			   TPS65912_LDO6, TPS65912_LDO6,
110*4882a593Smuzhiyun 			   tps65912_ldo_ranges),
111*4882a593Smuzhiyun 	TPS65912_REGULATOR("LDO7", LDO7, "ldo7", tps65912_ops_ldo,
112*4882a593Smuzhiyun 			   TPS65912_LDO7, TPS65912_LDO7,
113*4882a593Smuzhiyun 			   tps65912_ldo_ranges),
114*4882a593Smuzhiyun 	TPS65912_REGULATOR("LDO8", LDO8, "ldo8", tps65912_ops_ldo,
115*4882a593Smuzhiyun 			   TPS65912_LDO8, TPS65912_LDO8,
116*4882a593Smuzhiyun 			   tps65912_ldo_ranges),
117*4882a593Smuzhiyun 	TPS65912_REGULATOR("LDO9", LDO9, "ldo9", tps65912_ops_ldo,
118*4882a593Smuzhiyun 			   TPS65912_LDO9, TPS65912_LDO9,
119*4882a593Smuzhiyun 			   tps65912_ldo_ranges),
120*4882a593Smuzhiyun 	TPS65912_REGULATOR("LDO10", LDO10, "ldo10", tps65912_ops_ldo,
121*4882a593Smuzhiyun 			   TPS65912_LDO10, TPS65912_LDO10,
122*4882a593Smuzhiyun 			   tps65912_ldo_ranges),
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
tps65912_regulator_probe(struct platform_device * pdev)125*4882a593Smuzhiyun static int tps65912_regulator_probe(struct platform_device *pdev)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	struct tps65912 *tps = dev_get_drvdata(pdev->dev.parent);
128*4882a593Smuzhiyun 	struct regulator_config config = { };
129*4882a593Smuzhiyun 	struct regulator_dev *rdev;
130*4882a593Smuzhiyun 	int i;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	platform_set_drvdata(pdev, tps);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	config.dev = &pdev->dev;
135*4882a593Smuzhiyun 	config.driver_data = tps;
136*4882a593Smuzhiyun 	config.dev->of_node = tps->dev->of_node;
137*4882a593Smuzhiyun 	config.regmap = tps->regmap;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(regulators); i++) {
140*4882a593Smuzhiyun 		rdev = devm_regulator_register(&pdev->dev, &regulators[i],
141*4882a593Smuzhiyun 					       &config);
142*4882a593Smuzhiyun 		if (IS_ERR(rdev)) {
143*4882a593Smuzhiyun 			dev_err(tps->dev, "failed to register %s regulator\n",
144*4882a593Smuzhiyun 				pdev->name);
145*4882a593Smuzhiyun 			return PTR_ERR(rdev);
146*4882a593Smuzhiyun 		}
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	return 0;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun static const struct platform_device_id tps65912_regulator_id_table[] = {
153*4882a593Smuzhiyun 	{ "tps65912-regulator", },
154*4882a593Smuzhiyun 	{ /* sentinel */ }
155*4882a593Smuzhiyun };
156*4882a593Smuzhiyun MODULE_DEVICE_TABLE(platform, tps65912_regulator_id_table);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun static struct platform_driver tps65912_regulator_driver = {
159*4882a593Smuzhiyun 	.driver = {
160*4882a593Smuzhiyun 		.name = "tps65912-regulator",
161*4882a593Smuzhiyun 	},
162*4882a593Smuzhiyun 	.probe = tps65912_regulator_probe,
163*4882a593Smuzhiyun 	.id_table = tps65912_regulator_id_table,
164*4882a593Smuzhiyun };
165*4882a593Smuzhiyun module_platform_driver(tps65912_regulator_driver);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
168*4882a593Smuzhiyun MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
169*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
170