xref: /linux/drivers/regulator/qcom_smd-regulator.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * Copyright (c) 2015, Sony Mobile Communications AB.
3  * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 and
7  * only version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/machine.h>
21 #include <linux/regulator/of_regulator.h>
22 #include <linux/soc/qcom/smd-rpm.h>
23 
24 struct qcom_rpm_reg {
25 	struct device *dev;
26 
27 	struct qcom_smd_rpm *rpm;
28 
29 	u32 type;
30 	u32 id;
31 
32 	struct regulator_desc desc;
33 
34 	int is_enabled;
35 	int uV;
36 };
37 
38 struct rpm_regulator_req {
39 	u32 key;
40 	u32 nbytes;
41 	u32 value;
42 };
43 
44 #define RPM_KEY_SWEN	0x6e657773 /* "swen" */
45 #define RPM_KEY_UV	0x00007675 /* "uv" */
46 #define RPM_KEY_MA	0x0000616d /* "ma" */
47 
48 static int rpm_reg_write_active(struct qcom_rpm_reg *vreg,
49 				struct rpm_regulator_req *req,
50 				size_t size)
51 {
52 	return qcom_rpm_smd_write(vreg->rpm,
53 				  QCOM_SMD_RPM_ACTIVE_STATE,
54 				  vreg->type,
55 				  vreg->id,
56 				  req, size);
57 }
58 
59 static int rpm_reg_enable(struct regulator_dev *rdev)
60 {
61 	struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
62 	struct rpm_regulator_req req;
63 	int ret;
64 
65 	req.key = RPM_KEY_SWEN;
66 	req.nbytes = sizeof(u32);
67 	req.value = 1;
68 
69 	ret = rpm_reg_write_active(vreg, &req, sizeof(req));
70 	if (!ret)
71 		vreg->is_enabled = 1;
72 
73 	return ret;
74 }
75 
76 static int rpm_reg_is_enabled(struct regulator_dev *rdev)
77 {
78 	struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
79 
80 	return vreg->is_enabled;
81 }
82 
83 static int rpm_reg_disable(struct regulator_dev *rdev)
84 {
85 	struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
86 	struct rpm_regulator_req req;
87 	int ret;
88 
89 	req.key = RPM_KEY_SWEN;
90 	req.nbytes = sizeof(u32);
91 	req.value = 0;
92 
93 	ret = rpm_reg_write_active(vreg, &req, sizeof(req));
94 	if (!ret)
95 		vreg->is_enabled = 0;
96 
97 	return ret;
98 }
99 
100 static int rpm_reg_get_voltage(struct regulator_dev *rdev)
101 {
102 	struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
103 
104 	return vreg->uV;
105 }
106 
107 static int rpm_reg_set_voltage(struct regulator_dev *rdev,
108 			       int min_uV,
109 			       int max_uV,
110 			       unsigned *selector)
111 {
112 	struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
113 	struct rpm_regulator_req req;
114 	int ret = 0;
115 
116 	req.key = RPM_KEY_UV;
117 	req.nbytes = sizeof(u32);
118 	req.value = min_uV;
119 
120 	ret = rpm_reg_write_active(vreg, &req, sizeof(req));
121 	if (!ret)
122 		vreg->uV = min_uV;
123 
124 	return ret;
125 }
126 
127 static int rpm_reg_set_load(struct regulator_dev *rdev, int load_uA)
128 {
129 	struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
130 	struct rpm_regulator_req req;
131 
132 	req.key = RPM_KEY_MA;
133 	req.nbytes = sizeof(u32);
134 	req.value = load_uA;
135 
136 	return rpm_reg_write_active(vreg, &req, sizeof(req));
137 }
138 
139 static const struct regulator_ops rpm_smps_ldo_ops = {
140 	.enable = rpm_reg_enable,
141 	.disable = rpm_reg_disable,
142 	.is_enabled = rpm_reg_is_enabled,
143 
144 	.get_voltage = rpm_reg_get_voltage,
145 	.set_voltage = rpm_reg_set_voltage,
146 
147 	.set_load = rpm_reg_set_load,
148 };
149 
150 static const struct regulator_ops rpm_switch_ops = {
151 	.enable = rpm_reg_enable,
152 	.disable = rpm_reg_disable,
153 	.is_enabled = rpm_reg_is_enabled,
154 };
155 
156 static const struct regulator_desc pm8x41_hfsmps = {
157 	.linear_ranges = (struct regulator_linear_range[]) {
158 		REGULATOR_LINEAR_RANGE( 375000,  0,  95, 12500),
159 		REGULATOR_LINEAR_RANGE(1550000, 96, 158, 25000),
160 	},
161 	.n_linear_ranges = 2,
162 	.n_voltages = 159,
163 	.ops = &rpm_smps_ldo_ops,
164 };
165 
166 static const struct regulator_desc pm8841_ftsmps = {
167 	.linear_ranges = (struct regulator_linear_range[]) {
168 		REGULATOR_LINEAR_RANGE(350000,  0, 184, 5000),
169 		REGULATOR_LINEAR_RANGE(700000, 185, 339, 10000),
170 	},
171 	.n_linear_ranges = 2,
172 	.n_voltages = 340,
173 	.ops = &rpm_smps_ldo_ops,
174 };
175 
176 static const struct regulator_desc pm8941_boost = {
177 	.linear_ranges = (struct regulator_linear_range[]) {
178 		REGULATOR_LINEAR_RANGE(4000000, 0, 15, 100000),
179 	},
180 	.n_linear_ranges = 1,
181 	.n_voltages = 16,
182 	.ops = &rpm_smps_ldo_ops,
183 };
184 
185 static const struct regulator_desc pm8941_pldo = {
186 	.linear_ranges = (struct regulator_linear_range[]) {
187 		REGULATOR_LINEAR_RANGE( 750000,  0,  30, 25000),
188 		REGULATOR_LINEAR_RANGE(1500000, 31, 99, 50000),
189 	},
190 	.n_linear_ranges = 2,
191 	.n_voltages = 100,
192 	.ops = &rpm_smps_ldo_ops,
193 };
194 
195 static const struct regulator_desc pm8941_nldo = {
196 	.linear_ranges = (struct regulator_linear_range[]) {
197 		REGULATOR_LINEAR_RANGE(750000, 0, 63, 12500),
198 	},
199 	.n_linear_ranges = 1,
200 	.n_voltages = 64,
201 	.ops = &rpm_smps_ldo_ops,
202 };
203 
204 static const struct regulator_desc pm8941_lnldo = {
205 	.fixed_uV = 1740000,
206 	.n_voltages = 1,
207 	.ops = &rpm_smps_ldo_ops,
208 };
209 
210 static const struct regulator_desc pm8941_switch = {
211 	.ops = &rpm_switch_ops,
212 };
213 
214 struct rpm_regulator_data {
215 	const char *name;
216 	u32 type;
217 	u32 id;
218 	const struct regulator_desc *desc;
219 	const char *supply;
220 };
221 
222 static const struct rpm_regulator_data rpm_pm8841_regulators[] = {
223 	{ "s1", QCOM_SMD_RPM_SMPB, 1, &pm8x41_hfsmps, "vdd_s1" },
224 	{ "s2", QCOM_SMD_RPM_SMPB, 2, &pm8841_ftsmps, "vdd_s2" },
225 	{ "s3", QCOM_SMD_RPM_SMPB, 3, &pm8x41_hfsmps, "vdd_s3" },
226 	{ "s4", QCOM_SMD_RPM_SMPB, 4, &pm8841_ftsmps, "vdd_s4" },
227 	{ "s5", QCOM_SMD_RPM_SMPB, 5, &pm8841_ftsmps, "vdd_s5" },
228 	{ "s6", QCOM_SMD_RPM_SMPB, 6, &pm8841_ftsmps, "vdd_s6" },
229 	{ "s7", QCOM_SMD_RPM_SMPB, 7, &pm8841_ftsmps, "vdd_s7" },
230 	{ "s8", QCOM_SMD_RPM_SMPB, 8, &pm8841_ftsmps, "vdd_s8" },
231 	{}
232 };
233 
234 static const struct rpm_regulator_data rpm_pm8941_regulators[] = {
235 	{ "s1", QCOM_SMD_RPM_SMPA, 1, &pm8x41_hfsmps, "vdd_s1" },
236 	{ "s2", QCOM_SMD_RPM_SMPA, 2, &pm8x41_hfsmps, "vdd_s2" },
237 	{ "s3", QCOM_SMD_RPM_SMPA, 3, &pm8x41_hfsmps, "vdd_s3" },
238 	{ "s4", QCOM_SMD_RPM_BOOST, 1, &pm8941_boost },
239 
240 	{ "l1", QCOM_SMD_RPM_LDOA, 1, &pm8941_nldo, "vdd_l1_l3" },
241 	{ "l2", QCOM_SMD_RPM_LDOA, 2, &pm8941_nldo, "vdd_l2_lvs1_2_3" },
242 	{ "l3", QCOM_SMD_RPM_LDOA, 3, &pm8941_nldo, "vdd_l1_l3" },
243 	{ "l4", QCOM_SMD_RPM_LDOA, 4, &pm8941_nldo, "vdd_l4_l11" },
244 	{ "l5", QCOM_SMD_RPM_LDOA, 5, &pm8941_lnldo, "vdd_l5_l7" },
245 	{ "l6", QCOM_SMD_RPM_LDOA, 6, &pm8941_pldo, "vdd_l6_l12_l14_l15" },
246 	{ "l7", QCOM_SMD_RPM_LDOA, 7, &pm8941_lnldo, "vdd_l5_l7" },
247 	{ "l8", QCOM_SMD_RPM_LDOA, 8, &pm8941_pldo, "vdd_l8_l16_l18_l19" },
248 	{ "l9", QCOM_SMD_RPM_LDOA, 9, &pm8941_pldo, "vdd_l9_l10_l17_l22" },
249 	{ "l10", QCOM_SMD_RPM_LDOA, 10, &pm8941_pldo, "vdd_l9_l10_l17_l22" },
250 	{ "l11", QCOM_SMD_RPM_LDOA, 11, &pm8941_nldo, "vdd_l4_l11" },
251 	{ "l12", QCOM_SMD_RPM_LDOA, 12, &pm8941_pldo, "vdd_l6_l12_l14_l15" },
252 	{ "l13", QCOM_SMD_RPM_LDOA, 13, &pm8941_pldo, "vdd_l13_l20_l23_l24" },
253 	{ "l14", QCOM_SMD_RPM_LDOA, 14, &pm8941_pldo, "vdd_l6_l12_l14_l15" },
254 	{ "l15", QCOM_SMD_RPM_LDOA, 15, &pm8941_pldo, "vdd_l6_l12_l14_l15" },
255 	{ "l16", QCOM_SMD_RPM_LDOA, 16, &pm8941_pldo, "vdd_l8_l16_l18_l19" },
256 	{ "l17", QCOM_SMD_RPM_LDOA, 17, &pm8941_pldo, "vdd_l9_l10_l17_l22" },
257 	{ "l18", QCOM_SMD_RPM_LDOA, 18, &pm8941_pldo, "vdd_l8_l16_l18_l19" },
258 	{ "l19", QCOM_SMD_RPM_LDOA, 19, &pm8941_pldo, "vdd_l8_l16_l18_l19" },
259 	{ "l20", QCOM_SMD_RPM_LDOA, 20, &pm8941_pldo, "vdd_l13_l20_l23_l24" },
260 	{ "l21", QCOM_SMD_RPM_LDOA, 21, &pm8941_pldo, "vdd_l21" },
261 	{ "l22", QCOM_SMD_RPM_LDOA, 22, &pm8941_pldo, "vdd_l9_l10_l17_l22" },
262 	{ "l23", QCOM_SMD_RPM_LDOA, 23, &pm8941_pldo, "vdd_l13_l20_l23_l24" },
263 	{ "l24", QCOM_SMD_RPM_LDOA, 24, &pm8941_pldo, "vdd_l13_l20_l23_l24" },
264 
265 	{ "lvs1", QCOM_SMD_RPM_VSA, 1, &pm8941_switch, "vdd_l2_lvs1_2_3" },
266 	{ "lvs2", QCOM_SMD_RPM_VSA, 2, &pm8941_switch, "vdd_l2_lvs1_2_3" },
267 	{ "lvs3", QCOM_SMD_RPM_VSA, 3, &pm8941_switch, "vdd_l2_lvs1_2_3" },
268 
269 	{ "5vs1", QCOM_SMD_RPM_VSA, 4, &pm8941_switch, "vin_5vs" },
270 	{ "5vs2", QCOM_SMD_RPM_VSA, 5, &pm8941_switch, "vin_5vs" },
271 
272 	{}
273 };
274 
275 static const struct of_device_id rpm_of_match[] = {
276 	{ .compatible = "qcom,rpm-pm8841-regulators", .data = &rpm_pm8841_regulators },
277 	{ .compatible = "qcom,rpm-pm8941-regulators", .data = &rpm_pm8941_regulators },
278 	{}
279 };
280 MODULE_DEVICE_TABLE(of, rpm_of_match);
281 
282 static int rpm_reg_probe(struct platform_device *pdev)
283 {
284 	const struct rpm_regulator_data *reg;
285 	const struct of_device_id *match;
286 	struct regulator_config config = { };
287 	struct regulator_dev *rdev;
288 	struct qcom_rpm_reg *vreg;
289 	struct qcom_smd_rpm *rpm;
290 
291 	rpm = dev_get_drvdata(pdev->dev.parent);
292 	if (!rpm) {
293 		dev_err(&pdev->dev, "unable to retrieve handle to rpm\n");
294 		return -ENODEV;
295 	}
296 
297 	match = of_match_device(rpm_of_match, &pdev->dev);
298 	for (reg = match->data; reg->name; reg++) {
299 		vreg = devm_kzalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL);
300 		if (!vreg)
301 			return -ENOMEM;
302 
303 		vreg->dev = &pdev->dev;
304 		vreg->type = reg->type;
305 		vreg->id = reg->id;
306 		vreg->rpm = rpm;
307 
308 		memcpy(&vreg->desc, reg->desc, sizeof(vreg->desc));
309 
310 		vreg->desc.id = -1;
311 		vreg->desc.owner = THIS_MODULE;
312 		vreg->desc.type = REGULATOR_VOLTAGE;
313 		vreg->desc.name = reg->name;
314 		vreg->desc.supply_name = reg->supply;
315 		vreg->desc.of_match = reg->name;
316 
317 		config.dev = &pdev->dev;
318 		config.driver_data = vreg;
319 		rdev = devm_regulator_register(&pdev->dev, &vreg->desc, &config);
320 		if (IS_ERR(rdev)) {
321 			dev_err(&pdev->dev, "failed to register %s\n", reg->name);
322 			return PTR_ERR(rdev);
323 		}
324 	}
325 
326 	return 0;
327 }
328 
329 static struct platform_driver rpm_reg_driver = {
330 	.probe = rpm_reg_probe,
331 	.driver = {
332 		.name  = "qcom_rpm_smd_regulator",
333 		.of_match_table = rpm_of_match,
334 	},
335 };
336 
337 static int __init rpm_reg_init(void)
338 {
339 	return platform_driver_register(&rpm_reg_driver);
340 }
341 subsys_initcall(rpm_reg_init);
342 
343 static void __exit rpm_reg_exit(void)
344 {
345 	platform_driver_unregister(&rpm_reg_driver);
346 }
347 module_exit(rpm_reg_exit)
348 
349 MODULE_DESCRIPTION("Qualcomm RPM regulator driver");
350 MODULE_LICENSE("GPL v2");
351