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