blob: 8364ff331a81838c6e84fe822b17d434bfb74aa7 [file] [log] [blame]
Bjorn Andersson27203862014-09-22 16:25:29 -07001/*
2 * Copyright (c) 2014, 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/platform_device.h>
17#include <linux/of.h>
18#include <linux/of_device.h>
19#include <linux/regulator/driver.h>
20#include <linux/regulator/machine.h>
21#include <linux/regulator/of_regulator.h>
22#include <linux/mfd/qcom_rpm.h>
23
24#include <dt-bindings/mfd/qcom-rpm.h>
25
26#define MAX_REQUEST_LEN 2
27
28struct request_member {
29 int word;
30 unsigned int mask;
31 int shift;
32};
33
34struct rpm_reg_parts {
35 struct request_member mV; /* used if voltage is in mV */
36 struct request_member uV; /* used if voltage is in uV */
37 struct request_member ip; /* peak current in mA */
38 struct request_member pd; /* pull down enable */
39 struct request_member ia; /* average current in mA */
40 struct request_member fm; /* force mode */
41 struct request_member pm; /* power mode */
42 struct request_member pc; /* pin control */
43 struct request_member pf; /* pin function */
44 struct request_member enable_state; /* NCP and switch */
45 struct request_member comp_mode; /* NCP */
46 struct request_member freq; /* frequency: NCP and SMPS */
47 struct request_member freq_clk_src; /* clock source: SMPS */
48 struct request_member hpm; /* switch: control OCP and SS */
49 int request_len;
50};
51
52#define FORCE_MODE_IS_2_BITS(reg) \
Axel Lin6a642502014-09-25 10:17:29 +080053 (((reg)->parts->fm.mask >> (reg)->parts->fm.shift) == 3)
Bjorn Andersson27203862014-09-22 16:25:29 -070054
55struct qcom_rpm_reg {
56 struct qcom_rpm *rpm;
57
58 struct mutex lock;
59 struct device *dev;
60 struct regulator_desc desc;
61 const struct rpm_reg_parts *parts;
62
63 int resource;
64 u32 val[MAX_REQUEST_LEN];
65
66 int uV;
67 int is_enabled;
68
69 bool supports_force_mode_auto;
70 bool supports_force_mode_bypass;
71};
72
73static const struct rpm_reg_parts rpm8660_ldo_parts = {
74 .request_len = 2,
75 .mV = { 0, 0x00000FFF, 0 },
76 .ip = { 0, 0x00FFF000, 12 },
77 .fm = { 0, 0x03000000, 24 },
78 .pc = { 0, 0x3C000000, 26 },
79 .pf = { 0, 0xC0000000, 30 },
80 .pd = { 1, 0x00000001, 0 },
81 .ia = { 1, 0x00001FFE, 1 },
82};
83
84static const struct rpm_reg_parts rpm8660_smps_parts = {
85 .request_len = 2,
86 .mV = { 0, 0x00000FFF, 0 },
87 .ip = { 0, 0x00FFF000, 12 },
88 .fm = { 0, 0x03000000, 24 },
89 .pc = { 0, 0x3C000000, 26 },
90 .pf = { 0, 0xC0000000, 30 },
91 .pd = { 1, 0x00000001, 0 },
92 .ia = { 1, 0x00001FFE, 1 },
93 .freq = { 1, 0x001FE000, 13 },
94 .freq_clk_src = { 1, 0x00600000, 21 },
95};
96
97static const struct rpm_reg_parts rpm8660_switch_parts = {
98 .request_len = 1,
99 .enable_state = { 0, 0x00000001, 0 },
100 .pd = { 0, 0x00000002, 1 },
101 .pc = { 0, 0x0000003C, 2 },
102 .pf = { 0, 0x000000C0, 6 },
103 .hpm = { 0, 0x00000300, 8 },
104};
105
106static const struct rpm_reg_parts rpm8660_ncp_parts = {
107 .request_len = 1,
108 .mV = { 0, 0x00000FFF, 0 },
109 .enable_state = { 0, 0x00001000, 12 },
110 .comp_mode = { 0, 0x00002000, 13 },
111 .freq = { 0, 0x003FC000, 14 },
112};
113
114static const struct rpm_reg_parts rpm8960_ldo_parts = {
115 .request_len = 2,
116 .uV = { 0, 0x007FFFFF, 0 },
117 .pd = { 0, 0x00800000, 23 },
118 .pc = { 0, 0x0F000000, 24 },
119 .pf = { 0, 0xF0000000, 28 },
120 .ip = { 1, 0x000003FF, 0 },
121 .ia = { 1, 0x000FFC00, 10 },
122 .fm = { 1, 0x00700000, 20 },
123};
124
125static const struct rpm_reg_parts rpm8960_smps_parts = {
126 .request_len = 2,
127 .uV = { 0, 0x007FFFFF, 0 },
128 .pd = { 0, 0x00800000, 23 },
129 .pc = { 0, 0x0F000000, 24 },
130 .pf = { 0, 0xF0000000, 28 },
131 .ip = { 1, 0x000003FF, 0 },
132 .ia = { 1, 0x000FFC00, 10 },
133 .fm = { 1, 0x00700000, 20 },
134 .pm = { 1, 0x00800000, 23 },
135 .freq = { 1, 0x1F000000, 24 },
136 .freq_clk_src = { 1, 0x60000000, 29 },
137};
138
139static const struct rpm_reg_parts rpm8960_switch_parts = {
140 .request_len = 1,
141 .enable_state = { 0, 0x00000001, 0 },
142 .pd = { 0, 0x00000002, 1 },
143 .pc = { 0, 0x0000003C, 2 },
144 .pf = { 0, 0x000003C0, 6 },
145 .hpm = { 0, 0x00000C00, 10 },
146};
147
148static const struct rpm_reg_parts rpm8960_ncp_parts = {
149 .request_len = 1,
150 .uV = { 0, 0x007FFFFF, 0 },
151 .enable_state = { 0, 0x00800000, 23 },
152 .comp_mode = { 0, 0x01000000, 24 },
153 .freq = { 0, 0x3E000000, 25 },
154};
155
156/*
157 * Physically available PMIC regulator voltage ranges
158 */
159static const struct regulator_linear_range pldo_ranges[] = {
160 REGULATOR_LINEAR_RANGE( 750000, 0, 59, 12500),
161 REGULATOR_LINEAR_RANGE(1500000, 60, 123, 25000),
162 REGULATOR_LINEAR_RANGE(3100000, 124, 160, 50000),
163};
164
165static const struct regulator_linear_range nldo_ranges[] = {
166 REGULATOR_LINEAR_RANGE( 750000, 0, 63, 12500),
167};
168
169static const struct regulator_linear_range nldo1200_ranges[] = {
170 REGULATOR_LINEAR_RANGE( 375000, 0, 59, 6250),
171 REGULATOR_LINEAR_RANGE( 750000, 60, 123, 12500),
172};
173
174static const struct regulator_linear_range smps_ranges[] = {
175 REGULATOR_LINEAR_RANGE( 375000, 0, 29, 12500),
176 REGULATOR_LINEAR_RANGE( 750000, 30, 89, 12500),
177 REGULATOR_LINEAR_RANGE(1500000, 90, 153, 25000),
178};
179
180static const struct regulator_linear_range ftsmps_ranges[] = {
181 REGULATOR_LINEAR_RANGE( 350000, 0, 6, 50000),
182 REGULATOR_LINEAR_RANGE( 700000, 7, 63, 12500),
183 REGULATOR_LINEAR_RANGE(1500000, 64, 100, 50000),
184};
185
Josh Cartwright0f5bb5b2014-11-20 13:41:25 -0600186static const struct regulator_linear_range smb208_ranges[] = {
187 REGULATOR_LINEAR_RANGE( 375000, 0, 29, 12500),
188 REGULATOR_LINEAR_RANGE( 750000, 30, 89, 12500),
189 REGULATOR_LINEAR_RANGE(1500000, 90, 153, 25000),
190 REGULATOR_LINEAR_RANGE(3100000, 154, 234, 25000),
191};
192
Bjorn Andersson27203862014-09-22 16:25:29 -0700193static const struct regulator_linear_range ncp_ranges[] = {
194 REGULATOR_LINEAR_RANGE(1500000, 0, 31, 50000),
195};
196
197static int rpm_reg_write(struct qcom_rpm_reg *vreg,
198 const struct request_member *req,
199 const int value)
200{
201 if (WARN_ON((value << req->shift) & ~req->mask))
202 return -EINVAL;
203
204 vreg->val[req->word] &= ~req->mask;
205 vreg->val[req->word] |= value << req->shift;
206
207 return qcom_rpm_write(vreg->rpm,
208 vreg->resource,
209 vreg->val,
210 vreg->parts->request_len);
211}
212
213static int rpm_reg_set_mV_sel(struct regulator_dev *rdev,
214 unsigned selector)
215{
216 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
217 const struct rpm_reg_parts *parts = vreg->parts;
218 const struct request_member *req = &parts->mV;
219 int ret = 0;
220 int uV;
221
222 if (req->mask == 0)
223 return -EINVAL;
224
225 uV = regulator_list_voltage_linear_range(rdev, selector);
226 if (uV < 0)
227 return uV;
228
229 mutex_lock(&vreg->lock);
230 vreg->uV = uV;
231 if (vreg->is_enabled)
232 ret = rpm_reg_write(vreg, req, vreg->uV / 1000);
233 mutex_unlock(&vreg->lock);
234
235 return ret;
236}
237
238static int rpm_reg_set_uV_sel(struct regulator_dev *rdev,
239 unsigned selector)
240{
241 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
242 const struct rpm_reg_parts *parts = vreg->parts;
243 const struct request_member *req = &parts->uV;
244 int ret = 0;
245 int uV;
246
247 if (req->mask == 0)
248 return -EINVAL;
249
250 uV = regulator_list_voltage_linear_range(rdev, selector);
251 if (uV < 0)
252 return uV;
253
254 mutex_lock(&vreg->lock);
255 vreg->uV = uV;
256 if (vreg->is_enabled)
257 ret = rpm_reg_write(vreg, req, vreg->uV);
258 mutex_unlock(&vreg->lock);
259
260 return ret;
261}
262
263static int rpm_reg_get_voltage(struct regulator_dev *rdev)
264{
265 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
266
267 return vreg->uV;
268}
269
270static int rpm_reg_mV_enable(struct regulator_dev *rdev)
271{
272 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
273 const struct rpm_reg_parts *parts = vreg->parts;
274 const struct request_member *req = &parts->mV;
275 int ret;
276
277 if (req->mask == 0)
278 return -EINVAL;
279
280 mutex_lock(&vreg->lock);
281 ret = rpm_reg_write(vreg, req, vreg->uV / 1000);
282 if (!ret)
283 vreg->is_enabled = 1;
284 mutex_unlock(&vreg->lock);
285
286 return ret;
287}
288
289static int rpm_reg_uV_enable(struct regulator_dev *rdev)
290{
291 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
292 const struct rpm_reg_parts *parts = vreg->parts;
293 const struct request_member *req = &parts->uV;
294 int ret;
295
296 if (req->mask == 0)
297 return -EINVAL;
298
299 mutex_lock(&vreg->lock);
300 ret = rpm_reg_write(vreg, req, vreg->uV);
301 if (!ret)
302 vreg->is_enabled = 1;
303 mutex_unlock(&vreg->lock);
304
305 return ret;
306}
307
308static int rpm_reg_switch_enable(struct regulator_dev *rdev)
309{
310 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
311 const struct rpm_reg_parts *parts = vreg->parts;
312 const struct request_member *req = &parts->enable_state;
313 int ret;
314
315 if (req->mask == 0)
316 return -EINVAL;
317
318 mutex_lock(&vreg->lock);
319 ret = rpm_reg_write(vreg, req, 1);
320 if (!ret)
321 vreg->is_enabled = 1;
322 mutex_unlock(&vreg->lock);
323
324 return ret;
325}
326
327static int rpm_reg_mV_disable(struct regulator_dev *rdev)
328{
329 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
330 const struct rpm_reg_parts *parts = vreg->parts;
331 const struct request_member *req = &parts->mV;
332 int ret;
333
334 if (req->mask == 0)
335 return -EINVAL;
336
337 mutex_lock(&vreg->lock);
338 ret = rpm_reg_write(vreg, req, 0);
339 if (!ret)
340 vreg->is_enabled = 0;
341 mutex_unlock(&vreg->lock);
342
343 return ret;
344}
345
346static int rpm_reg_uV_disable(struct regulator_dev *rdev)
347{
348 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
349 const struct rpm_reg_parts *parts = vreg->parts;
350 const struct request_member *req = &parts->uV;
351 int ret;
352
353 if (req->mask == 0)
354 return -EINVAL;
355
356 mutex_lock(&vreg->lock);
357 ret = rpm_reg_write(vreg, req, 0);
358 if (!ret)
359 vreg->is_enabled = 0;
360 mutex_unlock(&vreg->lock);
361
362 return ret;
363}
364
365static int rpm_reg_switch_disable(struct regulator_dev *rdev)
366{
367 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
368 const struct rpm_reg_parts *parts = vreg->parts;
369 const struct request_member *req = &parts->enable_state;
370 int ret;
371
372 if (req->mask == 0)
373 return -EINVAL;
374
375 mutex_lock(&vreg->lock);
376 ret = rpm_reg_write(vreg, req, 0);
377 if (!ret)
378 vreg->is_enabled = 0;
379 mutex_unlock(&vreg->lock);
380
381 return ret;
382}
383
384static int rpm_reg_is_enabled(struct regulator_dev *rdev)
385{
386 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
387
388 return vreg->is_enabled;
389}
390
391static struct regulator_ops uV_ops = {
392 .list_voltage = regulator_list_voltage_linear_range,
393
394 .set_voltage_sel = rpm_reg_set_uV_sel,
395 .get_voltage = rpm_reg_get_voltage,
396
397 .enable = rpm_reg_uV_enable,
398 .disable = rpm_reg_uV_disable,
399 .is_enabled = rpm_reg_is_enabled,
400};
401
402static struct regulator_ops mV_ops = {
403 .list_voltage = regulator_list_voltage_linear_range,
404
405 .set_voltage_sel = rpm_reg_set_mV_sel,
406 .get_voltage = rpm_reg_get_voltage,
407
408 .enable = rpm_reg_mV_enable,
409 .disable = rpm_reg_mV_disable,
410 .is_enabled = rpm_reg_is_enabled,
411};
412
413static struct regulator_ops switch_ops = {
414 .enable = rpm_reg_switch_enable,
415 .disable = rpm_reg_switch_disable,
416 .is_enabled = rpm_reg_is_enabled,
417};
418
419/*
420 * PM8058 regulators
421 */
422static const struct qcom_rpm_reg pm8058_pldo = {
423 .desc.linear_ranges = pldo_ranges,
424 .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
425 .desc.n_voltages = 161,
426 .desc.ops = &mV_ops,
427 .parts = &rpm8660_ldo_parts,
428 .supports_force_mode_auto = false,
429 .supports_force_mode_bypass = false,
430};
431
432static const struct qcom_rpm_reg pm8058_nldo = {
433 .desc.linear_ranges = nldo_ranges,
434 .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
435 .desc.n_voltages = 64,
436 .desc.ops = &mV_ops,
437 .parts = &rpm8660_ldo_parts,
438 .supports_force_mode_auto = false,
439 .supports_force_mode_bypass = false,
440};
441
442static const struct qcom_rpm_reg pm8058_smps = {
443 .desc.linear_ranges = smps_ranges,
444 .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges),
445 .desc.n_voltages = 154,
446 .desc.ops = &mV_ops,
447 .parts = &rpm8660_smps_parts,
448 .supports_force_mode_auto = false,
449 .supports_force_mode_bypass = false,
450};
451
452static const struct qcom_rpm_reg pm8058_ncp = {
453 .desc.linear_ranges = ncp_ranges,
454 .desc.n_linear_ranges = ARRAY_SIZE(ncp_ranges),
455 .desc.n_voltages = 32,
456 .desc.ops = &mV_ops,
457 .parts = &rpm8660_ncp_parts,
458};
459
460static const struct qcom_rpm_reg pm8058_switch = {
461 .desc.ops = &switch_ops,
462 .parts = &rpm8660_switch_parts,
463};
464
465/*
466 * PM8901 regulators
467 */
468static const struct qcom_rpm_reg pm8901_pldo = {
469 .desc.linear_ranges = pldo_ranges,
470 .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
471 .desc.n_voltages = 161,
472 .desc.ops = &mV_ops,
473 .parts = &rpm8660_ldo_parts,
474 .supports_force_mode_auto = false,
475 .supports_force_mode_bypass = true,
476};
477
478static const struct qcom_rpm_reg pm8901_nldo = {
479 .desc.linear_ranges = nldo_ranges,
480 .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
481 .desc.n_voltages = 64,
482 .desc.ops = &mV_ops,
483 .parts = &rpm8660_ldo_parts,
484 .supports_force_mode_auto = false,
485 .supports_force_mode_bypass = true,
486};
487
488static const struct qcom_rpm_reg pm8901_ftsmps = {
489 .desc.linear_ranges = ftsmps_ranges,
490 .desc.n_linear_ranges = ARRAY_SIZE(ftsmps_ranges),
491 .desc.n_voltages = 101,
492 .desc.ops = &mV_ops,
493 .parts = &rpm8660_smps_parts,
494 .supports_force_mode_auto = true,
495 .supports_force_mode_bypass = false,
496};
497
498static const struct qcom_rpm_reg pm8901_switch = {
499 .desc.ops = &switch_ops,
500 .parts = &rpm8660_switch_parts,
501};
502
503/*
504 * PM8921 regulators
505 */
506static const struct qcom_rpm_reg pm8921_pldo = {
507 .desc.linear_ranges = pldo_ranges,
508 .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
509 .desc.n_voltages = 161,
510 .desc.ops = &uV_ops,
511 .parts = &rpm8960_ldo_parts,
512 .supports_force_mode_auto = false,
513 .supports_force_mode_bypass = true,
514};
515
516static const struct qcom_rpm_reg pm8921_nldo = {
517 .desc.linear_ranges = nldo_ranges,
518 .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
519 .desc.n_voltages = 64,
520 .desc.ops = &uV_ops,
521 .parts = &rpm8960_ldo_parts,
522 .supports_force_mode_auto = false,
523 .supports_force_mode_bypass = true,
524};
525
526static const struct qcom_rpm_reg pm8921_nldo1200 = {
527 .desc.linear_ranges = nldo1200_ranges,
528 .desc.n_linear_ranges = ARRAY_SIZE(nldo1200_ranges),
529 .desc.n_voltages = 124,
530 .desc.ops = &uV_ops,
531 .parts = &rpm8960_ldo_parts,
532 .supports_force_mode_auto = false,
533 .supports_force_mode_bypass = true,
534};
535
536static const struct qcom_rpm_reg pm8921_smps = {
537 .desc.linear_ranges = smps_ranges,
538 .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges),
539 .desc.n_voltages = 154,
540 .desc.ops = &uV_ops,
541 .parts = &rpm8960_smps_parts,
542 .supports_force_mode_auto = true,
543 .supports_force_mode_bypass = false,
544};
545
546static const struct qcom_rpm_reg pm8921_ftsmps = {
547 .desc.linear_ranges = ftsmps_ranges,
548 .desc.n_linear_ranges = ARRAY_SIZE(ftsmps_ranges),
549 .desc.n_voltages = 101,
550 .desc.ops = &uV_ops,
551 .parts = &rpm8960_smps_parts,
552 .supports_force_mode_auto = true,
553 .supports_force_mode_bypass = false,
554};
555
556static const struct qcom_rpm_reg pm8921_ncp = {
557 .desc.linear_ranges = ncp_ranges,
558 .desc.n_linear_ranges = ARRAY_SIZE(ncp_ranges),
559 .desc.n_voltages = 32,
560 .desc.ops = &uV_ops,
561 .parts = &rpm8960_ncp_parts,
562};
563
564static const struct qcom_rpm_reg pm8921_switch = {
565 .desc.ops = &switch_ops,
566 .parts = &rpm8960_switch_parts,
567};
568
Josh Cartwright0f5bb5b2014-11-20 13:41:25 -0600569static const struct qcom_rpm_reg smb208_smps = {
570 .desc.linear_ranges = smb208_ranges,
571 .desc.n_linear_ranges = ARRAY_SIZE(smb208_ranges),
572 .desc.n_voltages = 235,
573 .desc.ops = &uV_ops,
574 .parts = &rpm8960_smps_parts,
575 .supports_force_mode_auto = false,
576 .supports_force_mode_bypass = false,
577};
578
Bjorn Andersson27203862014-09-22 16:25:29 -0700579static const struct of_device_id rpm_of_match[] = {
580 { .compatible = "qcom,rpm-pm8058-pldo", .data = &pm8058_pldo },
581 { .compatible = "qcom,rpm-pm8058-nldo", .data = &pm8058_nldo },
582 { .compatible = "qcom,rpm-pm8058-smps", .data = &pm8058_smps },
583 { .compatible = "qcom,rpm-pm8058-ncp", .data = &pm8058_ncp },
584 { .compatible = "qcom,rpm-pm8058-switch", .data = &pm8058_switch },
585
586 { .compatible = "qcom,rpm-pm8901-pldo", .data = &pm8901_pldo },
587 { .compatible = "qcom,rpm-pm8901-nldo", .data = &pm8901_nldo },
588 { .compatible = "qcom,rpm-pm8901-ftsmps", .data = &pm8901_ftsmps },
589 { .compatible = "qcom,rpm-pm8901-switch", .data = &pm8901_switch },
590
591 { .compatible = "qcom,rpm-pm8921-pldo", .data = &pm8921_pldo },
592 { .compatible = "qcom,rpm-pm8921-nldo", .data = &pm8921_nldo },
593 { .compatible = "qcom,rpm-pm8921-nldo1200", .data = &pm8921_nldo1200 },
594 { .compatible = "qcom,rpm-pm8921-smps", .data = &pm8921_smps },
595 { .compatible = "qcom,rpm-pm8921-ftsmps", .data = &pm8921_ftsmps },
596 { .compatible = "qcom,rpm-pm8921-ncp", .data = &pm8921_ncp },
597 { .compatible = "qcom,rpm-pm8921-switch", .data = &pm8921_switch },
Josh Cartwright0f5bb5b2014-11-20 13:41:25 -0600598
599 { .compatible = "qcom,rpm-smb208", .data = &smb208_smps },
Bjorn Andersson27203862014-09-22 16:25:29 -0700600 { }
601};
602MODULE_DEVICE_TABLE(of, rpm_of_match);
603
604static int rpm_reg_set(struct qcom_rpm_reg *vreg,
605 const struct request_member *req,
606 const int value)
607{
608 if (req->mask == 0 || (value << req->shift) & ~req->mask)
609 return -EINVAL;
610
611 vreg->val[req->word] &= ~req->mask;
612 vreg->val[req->word] |= value << req->shift;
613
614 return 0;
615}
616
617static int rpm_reg_of_parse_freq(struct device *dev, struct qcom_rpm_reg *vreg)
618{
619 static const int freq_table[] = {
620 19200000, 9600000, 6400000, 4800000, 3840000, 3200000, 2740000,
621 2400000, 2130000, 1920000, 1750000, 1600000, 1480000, 1370000,
622 1280000, 1200000,
623
624 };
625 const char *key;
626 u32 freq;
627 int ret;
628 int i;
629
630 key = "qcom,switch-mode-frequency";
631 ret = of_property_read_u32(dev->of_node, key, &freq);
632 if (ret) {
633 dev_err(dev, "regulator requires %s property\n", key);
634 return -EINVAL;
635 }
636
637 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
638 if (freq == freq_table[i]) {
639 rpm_reg_set(vreg, &vreg->parts->freq, i + 1);
640 return 0;
641 }
642 }
643
644 dev_err(dev, "invalid frequency %d\n", freq);
645 return -EINVAL;
646}
647
648static int rpm_reg_probe(struct platform_device *pdev)
649{
650 struct regulator_init_data *initdata;
651 const struct qcom_rpm_reg *template;
652 const struct of_device_id *match;
Axel Linaad615c2014-09-24 17:45:25 +0800653 struct regulator_config config = { };
Bjorn Andersson27203862014-09-22 16:25:29 -0700654 struct regulator_dev *rdev;
655 struct qcom_rpm_reg *vreg;
656 const char *key;
657 u32 force_mode;
658 bool pwm;
659 u32 val;
660 int ret;
661
662 match = of_match_device(rpm_of_match, &pdev->dev);
663 template = match->data;
664
Bjorn Andersson27203862014-09-22 16:25:29 -0700665 vreg = devm_kmalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL);
666 if (!vreg) {
667 dev_err(&pdev->dev, "failed to allocate vreg\n");
668 return -ENOMEM;
669 }
670 memcpy(vreg, template, sizeof(*vreg));
671 mutex_init(&vreg->lock);
672 vreg->dev = &pdev->dev;
673 vreg->desc.id = -1;
674 vreg->desc.owner = THIS_MODULE;
675 vreg->desc.type = REGULATOR_VOLTAGE;
676 vreg->desc.name = pdev->dev.of_node->name;
677
678 vreg->rpm = dev_get_drvdata(pdev->dev.parent);
679 if (!vreg->rpm) {
680 dev_err(&pdev->dev, "unable to retrieve handle to rpm\n");
681 return -ENODEV;
682 }
683
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100684 initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
685 &vreg->desc);
686 if (!initdata)
687 return -EINVAL;
688
Bjorn Andersson27203862014-09-22 16:25:29 -0700689 key = "reg";
690 ret = of_property_read_u32(pdev->dev.of_node, key, &val);
691 if (ret) {
692 dev_err(&pdev->dev, "failed to read %s\n", key);
693 return ret;
694 }
695 vreg->resource = val;
696
697 if ((vreg->parts->uV.mask || vreg->parts->mV.mask) &&
698 (!initdata->constraints.min_uV || !initdata->constraints.max_uV)) {
699 dev_err(&pdev->dev, "no voltage specified for regulator\n");
700 return -EINVAL;
701 }
702
703 key = "bias-pull-down";
704 if (of_property_read_bool(pdev->dev.of_node, key)) {
705 ret = rpm_reg_set(vreg, &vreg->parts->pd, 1);
706 if (ret) {
707 dev_err(&pdev->dev, "%s is invalid", key);
708 return ret;
709 }
710 }
711
712 if (vreg->parts->freq.mask) {
713 ret = rpm_reg_of_parse_freq(&pdev->dev, vreg);
714 if (ret < 0)
715 return ret;
716 }
717
718 if (vreg->parts->pm.mask) {
719 key = "qcom,power-mode-hysteretic";
720 pwm = !of_property_read_bool(pdev->dev.of_node, key);
721
722 ret = rpm_reg_set(vreg, &vreg->parts->pm, pwm);
723 if (ret) {
724 dev_err(&pdev->dev, "failed to set power mode\n");
725 return ret;
726 }
727 }
728
729 if (vreg->parts->fm.mask) {
730 force_mode = -1;
731
732 key = "qcom,force-mode";
733 ret = of_property_read_u32(pdev->dev.of_node, key, &val);
734 if (ret == -EINVAL) {
735 val = QCOM_RPM_FORCE_MODE_NONE;
736 } else if (ret < 0) {
737 dev_err(&pdev->dev, "failed to read %s\n", key);
738 return ret;
739 }
740
741 /*
742 * If force-mode is encoded as 2 bits then the
743 * possible register values are:
744 * NONE, LPM, HPM
745 * otherwise:
746 * NONE, LPM, AUTO, HPM, BYPASS
747 */
748 switch (val) {
749 case QCOM_RPM_FORCE_MODE_NONE:
750 force_mode = 0;
751 break;
752 case QCOM_RPM_FORCE_MODE_LPM:
753 force_mode = 1;
754 break;
755 case QCOM_RPM_FORCE_MODE_HPM:
756 if (FORCE_MODE_IS_2_BITS(vreg))
757 force_mode = 2;
758 else
759 force_mode = 3;
760 break;
761 case QCOM_RPM_FORCE_MODE_AUTO:
762 if (vreg->supports_force_mode_auto)
763 force_mode = 2;
764 break;
765 case QCOM_RPM_FORCE_MODE_BYPASS:
766 if (vreg->supports_force_mode_bypass)
767 force_mode = 4;
768 break;
769 }
770
771 if (force_mode < 0) {
772 dev_err(&pdev->dev, "invalid force mode\n");
773 return -EINVAL;
774 }
775
776 ret = rpm_reg_set(vreg, &vreg->parts->fm, force_mode);
777 if (ret) {
778 dev_err(&pdev->dev, "failed to set force mode\n");
779 return ret;
780 }
781 }
782
783 config.dev = &pdev->dev;
784 config.init_data = initdata;
785 config.driver_data = vreg;
786 config.of_node = pdev->dev.of_node;
787 rdev = devm_regulator_register(&pdev->dev, &vreg->desc, &config);
788 if (IS_ERR(rdev)) {
789 dev_err(&pdev->dev, "can't register regulator\n");
790 return PTR_ERR(rdev);
791 }
792
793 return 0;
794}
795
796static struct platform_driver rpm_reg_driver = {
797 .probe = rpm_reg_probe,
798 .driver = {
799 .name = "qcom_rpm_reg",
Bjorn Andersson27203862014-09-22 16:25:29 -0700800 .of_match_table = of_match_ptr(rpm_of_match),
801 },
802};
803
804static int __init rpm_reg_init(void)
805{
806 return platform_driver_register(&rpm_reg_driver);
807}
808subsys_initcall(rpm_reg_init);
809
810static void __exit rpm_reg_exit(void)
811{
812 platform_driver_unregister(&rpm_reg_driver);
813}
814module_exit(rpm_reg_exit)
815
816MODULE_DESCRIPTION("Qualcomm RPM regulator driver");
817MODULE_LICENSE("GPL v2");