Merge branch 'iov_iter' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[pandora-kernel.git] / drivers / regulator / qcom_rpm-regulator.c
1 /*
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
28 struct request_member {
29         int             word;
30         unsigned int    mask;
31         int             shift;
32 };
33
34 struct 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) \
53         (((reg)->parts->fm.mask >> (reg)->parts->fm.shift) == 3)
54
55 struct 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
73 static 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
84 static 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
97 static 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
106 static 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
114 static 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
125 static 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
139 static 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
148 static 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  */
159 static 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
165 static const struct regulator_linear_range nldo_ranges[] = {
166         REGULATOR_LINEAR_RANGE( 750000,   0,  63, 12500),
167 };
168
169 static 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
174 static 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
180 static 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
186 static 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
193 static const struct regulator_linear_range ncp_ranges[] = {
194         REGULATOR_LINEAR_RANGE(1500000,   0,  31, 50000),
195 };
196
197 static 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
213 static 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         if (vreg->is_enabled)
231                 ret = rpm_reg_write(vreg, req, uV / 1000);
232
233         if (!ret)
234                 vreg->uV = uV;
235         mutex_unlock(&vreg->lock);
236
237         return ret;
238 }
239
240 static int rpm_reg_set_uV_sel(struct regulator_dev *rdev,
241                               unsigned selector)
242 {
243         struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
244         const struct rpm_reg_parts *parts = vreg->parts;
245         const struct request_member *req = &parts->uV;
246         int ret = 0;
247         int uV;
248
249         if (req->mask == 0)
250                 return -EINVAL;
251
252         uV = regulator_list_voltage_linear_range(rdev, selector);
253         if (uV < 0)
254                 return uV;
255
256         mutex_lock(&vreg->lock);
257         if (vreg->is_enabled)
258                 ret = rpm_reg_write(vreg, req, uV);
259
260         if (!ret)
261                 vreg->uV = uV;
262         mutex_unlock(&vreg->lock);
263
264         return ret;
265 }
266
267 static int rpm_reg_get_voltage(struct regulator_dev *rdev)
268 {
269         struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
270
271         return vreg->uV;
272 }
273
274 static int rpm_reg_mV_enable(struct regulator_dev *rdev)
275 {
276         struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
277         const struct rpm_reg_parts *parts = vreg->parts;
278         const struct request_member *req = &parts->mV;
279         int ret;
280
281         if (req->mask == 0)
282                 return -EINVAL;
283
284         mutex_lock(&vreg->lock);
285         ret = rpm_reg_write(vreg, req, vreg->uV / 1000);
286         if (!ret)
287                 vreg->is_enabled = 1;
288         mutex_unlock(&vreg->lock);
289
290         return ret;
291 }
292
293 static int rpm_reg_uV_enable(struct regulator_dev *rdev)
294 {
295         struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
296         const struct rpm_reg_parts *parts = vreg->parts;
297         const struct request_member *req = &parts->uV;
298         int ret;
299
300         if (req->mask == 0)
301                 return -EINVAL;
302
303         mutex_lock(&vreg->lock);
304         ret = rpm_reg_write(vreg, req, vreg->uV);
305         if (!ret)
306                 vreg->is_enabled = 1;
307         mutex_unlock(&vreg->lock);
308
309         return ret;
310 }
311
312 static int rpm_reg_switch_enable(struct regulator_dev *rdev)
313 {
314         struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
315         const struct rpm_reg_parts *parts = vreg->parts;
316         const struct request_member *req = &parts->enable_state;
317         int ret;
318
319         if (req->mask == 0)
320                 return -EINVAL;
321
322         mutex_lock(&vreg->lock);
323         ret = rpm_reg_write(vreg, req, 1);
324         if (!ret)
325                 vreg->is_enabled = 1;
326         mutex_unlock(&vreg->lock);
327
328         return ret;
329 }
330
331 static int rpm_reg_mV_disable(struct regulator_dev *rdev)
332 {
333         struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
334         const struct rpm_reg_parts *parts = vreg->parts;
335         const struct request_member *req = &parts->mV;
336         int ret;
337
338         if (req->mask == 0)
339                 return -EINVAL;
340
341         mutex_lock(&vreg->lock);
342         ret = rpm_reg_write(vreg, req, 0);
343         if (!ret)
344                 vreg->is_enabled = 0;
345         mutex_unlock(&vreg->lock);
346
347         return ret;
348 }
349
350 static int rpm_reg_uV_disable(struct regulator_dev *rdev)
351 {
352         struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
353         const struct rpm_reg_parts *parts = vreg->parts;
354         const struct request_member *req = &parts->uV;
355         int ret;
356
357         if (req->mask == 0)
358                 return -EINVAL;
359
360         mutex_lock(&vreg->lock);
361         ret = rpm_reg_write(vreg, req, 0);
362         if (!ret)
363                 vreg->is_enabled = 0;
364         mutex_unlock(&vreg->lock);
365
366         return ret;
367 }
368
369 static int rpm_reg_switch_disable(struct regulator_dev *rdev)
370 {
371         struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
372         const struct rpm_reg_parts *parts = vreg->parts;
373         const struct request_member *req = &parts->enable_state;
374         int ret;
375
376         if (req->mask == 0)
377                 return -EINVAL;
378
379         mutex_lock(&vreg->lock);
380         ret = rpm_reg_write(vreg, req, 0);
381         if (!ret)
382                 vreg->is_enabled = 0;
383         mutex_unlock(&vreg->lock);
384
385         return ret;
386 }
387
388 static int rpm_reg_is_enabled(struct regulator_dev *rdev)
389 {
390         struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
391
392         return vreg->is_enabled;
393 }
394
395 static struct regulator_ops uV_ops = {
396         .list_voltage = regulator_list_voltage_linear_range,
397
398         .set_voltage_sel = rpm_reg_set_uV_sel,
399         .get_voltage = rpm_reg_get_voltage,
400
401         .enable = rpm_reg_uV_enable,
402         .disable = rpm_reg_uV_disable,
403         .is_enabled = rpm_reg_is_enabled,
404 };
405
406 static struct regulator_ops mV_ops = {
407         .list_voltage = regulator_list_voltage_linear_range,
408
409         .set_voltage_sel = rpm_reg_set_mV_sel,
410         .get_voltage = rpm_reg_get_voltage,
411
412         .enable = rpm_reg_mV_enable,
413         .disable = rpm_reg_mV_disable,
414         .is_enabled = rpm_reg_is_enabled,
415 };
416
417 static struct regulator_ops switch_ops = {
418         .enable = rpm_reg_switch_enable,
419         .disable = rpm_reg_switch_disable,
420         .is_enabled = rpm_reg_is_enabled,
421 };
422
423 /*
424  * PM8058 regulators
425  */
426 static const struct qcom_rpm_reg pm8058_pldo = {
427         .desc.linear_ranges = pldo_ranges,
428         .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
429         .desc.n_voltages = 161,
430         .desc.ops = &mV_ops,
431         .parts = &rpm8660_ldo_parts,
432         .supports_force_mode_auto = false,
433         .supports_force_mode_bypass = false,
434 };
435
436 static const struct qcom_rpm_reg pm8058_nldo = {
437         .desc.linear_ranges = nldo_ranges,
438         .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
439         .desc.n_voltages = 64,
440         .desc.ops = &mV_ops,
441         .parts = &rpm8660_ldo_parts,
442         .supports_force_mode_auto = false,
443         .supports_force_mode_bypass = false,
444 };
445
446 static const struct qcom_rpm_reg pm8058_smps = {
447         .desc.linear_ranges = smps_ranges,
448         .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges),
449         .desc.n_voltages = 154,
450         .desc.ops = &mV_ops,
451         .parts = &rpm8660_smps_parts,
452         .supports_force_mode_auto = false,
453         .supports_force_mode_bypass = false,
454 };
455
456 static const struct qcom_rpm_reg pm8058_ncp = {
457         .desc.linear_ranges = ncp_ranges,
458         .desc.n_linear_ranges = ARRAY_SIZE(ncp_ranges),
459         .desc.n_voltages = 32,
460         .desc.ops = &mV_ops,
461         .parts = &rpm8660_ncp_parts,
462 };
463
464 static const struct qcom_rpm_reg pm8058_switch = {
465         .desc.ops = &switch_ops,
466         .parts = &rpm8660_switch_parts,
467 };
468
469 /*
470  * PM8901 regulators
471  */
472 static const struct qcom_rpm_reg pm8901_pldo = {
473         .desc.linear_ranges = pldo_ranges,
474         .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
475         .desc.n_voltages = 161,
476         .desc.ops = &mV_ops,
477         .parts = &rpm8660_ldo_parts,
478         .supports_force_mode_auto = false,
479         .supports_force_mode_bypass = true,
480 };
481
482 static const struct qcom_rpm_reg pm8901_nldo = {
483         .desc.linear_ranges = nldo_ranges,
484         .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
485         .desc.n_voltages = 64,
486         .desc.ops = &mV_ops,
487         .parts = &rpm8660_ldo_parts,
488         .supports_force_mode_auto = false,
489         .supports_force_mode_bypass = true,
490 };
491
492 static const struct qcom_rpm_reg pm8901_ftsmps = {
493         .desc.linear_ranges = ftsmps_ranges,
494         .desc.n_linear_ranges = ARRAY_SIZE(ftsmps_ranges),
495         .desc.n_voltages = 101,
496         .desc.ops = &mV_ops,
497         .parts = &rpm8660_smps_parts,
498         .supports_force_mode_auto = true,
499         .supports_force_mode_bypass = false,
500 };
501
502 static const struct qcom_rpm_reg pm8901_switch = {
503         .desc.ops = &switch_ops,
504         .parts = &rpm8660_switch_parts,
505 };
506
507 /*
508  * PM8921 regulators
509  */
510 static const struct qcom_rpm_reg pm8921_pldo = {
511         .desc.linear_ranges = pldo_ranges,
512         .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
513         .desc.n_voltages = 161,
514         .desc.ops = &uV_ops,
515         .parts = &rpm8960_ldo_parts,
516         .supports_force_mode_auto = false,
517         .supports_force_mode_bypass = true,
518 };
519
520 static const struct qcom_rpm_reg pm8921_nldo = {
521         .desc.linear_ranges = nldo_ranges,
522         .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
523         .desc.n_voltages = 64,
524         .desc.ops = &uV_ops,
525         .parts = &rpm8960_ldo_parts,
526         .supports_force_mode_auto = false,
527         .supports_force_mode_bypass = true,
528 };
529
530 static const struct qcom_rpm_reg pm8921_nldo1200 = {
531         .desc.linear_ranges = nldo1200_ranges,
532         .desc.n_linear_ranges = ARRAY_SIZE(nldo1200_ranges),
533         .desc.n_voltages = 124,
534         .desc.ops = &uV_ops,
535         .parts = &rpm8960_ldo_parts,
536         .supports_force_mode_auto = false,
537         .supports_force_mode_bypass = true,
538 };
539
540 static const struct qcom_rpm_reg pm8921_smps = {
541         .desc.linear_ranges = smps_ranges,
542         .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges),
543         .desc.n_voltages = 154,
544         .desc.ops = &uV_ops,
545         .parts = &rpm8960_smps_parts,
546         .supports_force_mode_auto = true,
547         .supports_force_mode_bypass = false,
548 };
549
550 static const struct qcom_rpm_reg pm8921_ftsmps = {
551         .desc.linear_ranges = ftsmps_ranges,
552         .desc.n_linear_ranges = ARRAY_SIZE(ftsmps_ranges),
553         .desc.n_voltages = 101,
554         .desc.ops = &uV_ops,
555         .parts = &rpm8960_smps_parts,
556         .supports_force_mode_auto = true,
557         .supports_force_mode_bypass = false,
558 };
559
560 static const struct qcom_rpm_reg pm8921_ncp = {
561         .desc.linear_ranges = ncp_ranges,
562         .desc.n_linear_ranges = ARRAY_SIZE(ncp_ranges),
563         .desc.n_voltages = 32,
564         .desc.ops = &uV_ops,
565         .parts = &rpm8960_ncp_parts,
566 };
567
568 static const struct qcom_rpm_reg pm8921_switch = {
569         .desc.ops = &switch_ops,
570         .parts = &rpm8960_switch_parts,
571 };
572
573 static const struct qcom_rpm_reg smb208_smps = {
574         .desc.linear_ranges = smb208_ranges,
575         .desc.n_linear_ranges = ARRAY_SIZE(smb208_ranges),
576         .desc.n_voltages = 235,
577         .desc.ops = &uV_ops,
578         .parts = &rpm8960_smps_parts,
579         .supports_force_mode_auto = false,
580         .supports_force_mode_bypass = false,
581 };
582
583 static const struct of_device_id rpm_of_match[] = {
584         { .compatible = "qcom,rpm-pm8058-pldo",     .data = &pm8058_pldo },
585         { .compatible = "qcom,rpm-pm8058-nldo",     .data = &pm8058_nldo },
586         { .compatible = "qcom,rpm-pm8058-smps",     .data = &pm8058_smps },
587         { .compatible = "qcom,rpm-pm8058-ncp",      .data = &pm8058_ncp },
588         { .compatible = "qcom,rpm-pm8058-switch",   .data = &pm8058_switch },
589
590         { .compatible = "qcom,rpm-pm8901-pldo",     .data = &pm8901_pldo },
591         { .compatible = "qcom,rpm-pm8901-nldo",     .data = &pm8901_nldo },
592         { .compatible = "qcom,rpm-pm8901-ftsmps",   .data = &pm8901_ftsmps },
593         { .compatible = "qcom,rpm-pm8901-switch",   .data = &pm8901_switch },
594
595         { .compatible = "qcom,rpm-pm8921-pldo",     .data = &pm8921_pldo },
596         { .compatible = "qcom,rpm-pm8921-nldo",     .data = &pm8921_nldo },
597         { .compatible = "qcom,rpm-pm8921-nldo1200", .data = &pm8921_nldo1200 },
598         { .compatible = "qcom,rpm-pm8921-smps",     .data = &pm8921_smps },
599         { .compatible = "qcom,rpm-pm8921-ftsmps",   .data = &pm8921_ftsmps },
600         { .compatible = "qcom,rpm-pm8921-ncp",      .data = &pm8921_ncp },
601         { .compatible = "qcom,rpm-pm8921-switch",   .data = &pm8921_switch },
602
603         { .compatible = "qcom,rpm-smb208", .data = &smb208_smps },
604         { }
605 };
606 MODULE_DEVICE_TABLE(of, rpm_of_match);
607
608 static int rpm_reg_set(struct qcom_rpm_reg *vreg,
609                        const struct request_member *req,
610                        const int value)
611 {
612         if (req->mask == 0 || (value << req->shift) & ~req->mask)
613                 return -EINVAL;
614
615         vreg->val[req->word] &= ~req->mask;
616         vreg->val[req->word] |= value << req->shift;
617
618         return 0;
619 }
620
621 static int rpm_reg_of_parse_freq(struct device *dev, struct qcom_rpm_reg *vreg)
622 {
623         static const int freq_table[] = {
624                 19200000, 9600000, 6400000, 4800000, 3840000, 3200000, 2740000,
625                 2400000, 2130000, 1920000, 1750000, 1600000, 1480000, 1370000,
626                 1280000, 1200000,
627
628         };
629         const char *key;
630         u32 freq;
631         int ret;
632         int i;
633
634         key = "qcom,switch-mode-frequency";
635         ret = of_property_read_u32(dev->of_node, key, &freq);
636         if (ret) {
637                 dev_err(dev, "regulator requires %s property\n", key);
638                 return -EINVAL;
639         }
640
641         for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
642                 if (freq == freq_table[i]) {
643                         rpm_reg_set(vreg, &vreg->parts->freq, i + 1);
644                         return 0;
645                 }
646         }
647
648         dev_err(dev, "invalid frequency %d\n", freq);
649         return -EINVAL;
650 }
651
652 static int rpm_reg_probe(struct platform_device *pdev)
653 {
654         struct regulator_init_data *initdata;
655         const struct qcom_rpm_reg *template;
656         const struct of_device_id *match;
657         struct regulator_config config = { };
658         struct regulator_dev *rdev;
659         struct qcom_rpm_reg *vreg;
660         const char *key;
661         u32 force_mode;
662         bool pwm;
663         u32 val;
664         int ret;
665
666         match = of_match_device(rpm_of_match, &pdev->dev);
667         template = match->data;
668
669         vreg = devm_kmalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL);
670         if (!vreg) {
671                 dev_err(&pdev->dev, "failed to allocate vreg\n");
672                 return -ENOMEM;
673         }
674         memcpy(vreg, template, sizeof(*vreg));
675         mutex_init(&vreg->lock);
676         vreg->dev = &pdev->dev;
677         vreg->desc.id = -1;
678         vreg->desc.owner = THIS_MODULE;
679         vreg->desc.type = REGULATOR_VOLTAGE;
680         vreg->desc.name = pdev->dev.of_node->name;
681         vreg->desc.supply_name = "vin";
682
683         vreg->rpm = dev_get_drvdata(pdev->dev.parent);
684         if (!vreg->rpm) {
685                 dev_err(&pdev->dev, "unable to retrieve handle to rpm\n");
686                 return -ENODEV;
687         }
688
689         initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
690                                               &vreg->desc);
691         if (!initdata)
692                 return -EINVAL;
693
694         key = "reg";
695         ret = of_property_read_u32(pdev->dev.of_node, key, &val);
696         if (ret) {
697                 dev_err(&pdev->dev, "failed to read %s\n", key);
698                 return ret;
699         }
700         vreg->resource = val;
701
702         if ((vreg->parts->uV.mask || vreg->parts->mV.mask) &&
703             (!initdata->constraints.min_uV || !initdata->constraints.max_uV)) {
704                 dev_err(&pdev->dev, "no voltage specified for regulator\n");
705                 return -EINVAL;
706         }
707
708         key = "bias-pull-down";
709         if (of_property_read_bool(pdev->dev.of_node, key)) {
710                 ret = rpm_reg_set(vreg, &vreg->parts->pd, 1);
711                 if (ret) {
712                         dev_err(&pdev->dev, "%s is invalid", key);
713                         return ret;
714                 }
715         }
716
717         if (vreg->parts->freq.mask) {
718                 ret = rpm_reg_of_parse_freq(&pdev->dev, vreg);
719                 if (ret < 0)
720                         return ret;
721         }
722
723         if (vreg->parts->pm.mask) {
724                 key = "qcom,power-mode-hysteretic";
725                 pwm = !of_property_read_bool(pdev->dev.of_node, key);
726
727                 ret = rpm_reg_set(vreg, &vreg->parts->pm, pwm);
728                 if (ret) {
729                         dev_err(&pdev->dev, "failed to set power mode\n");
730                         return ret;
731                 }
732         }
733
734         if (vreg->parts->fm.mask) {
735                 force_mode = -1;
736
737                 key = "qcom,force-mode";
738                 ret = of_property_read_u32(pdev->dev.of_node, key, &val);
739                 if (ret == -EINVAL) {
740                         val = QCOM_RPM_FORCE_MODE_NONE;
741                 } else if (ret < 0) {
742                         dev_err(&pdev->dev, "failed to read %s\n", key);
743                         return ret;
744                 }
745
746                 /*
747                  * If force-mode is encoded as 2 bits then the
748                  * possible register values are:
749                  * NONE, LPM, HPM
750                  * otherwise:
751                  * NONE, LPM, AUTO, HPM, BYPASS
752                  */
753                 switch (val) {
754                 case QCOM_RPM_FORCE_MODE_NONE:
755                         force_mode = 0;
756                         break;
757                 case QCOM_RPM_FORCE_MODE_LPM:
758                         force_mode = 1;
759                         break;
760                 case QCOM_RPM_FORCE_MODE_HPM:
761                         if (FORCE_MODE_IS_2_BITS(vreg))
762                                 force_mode = 2;
763                         else
764                                 force_mode = 3;
765                         break;
766                 case QCOM_RPM_FORCE_MODE_AUTO:
767                         if (vreg->supports_force_mode_auto)
768                                 force_mode = 2;
769                         break;
770                 case QCOM_RPM_FORCE_MODE_BYPASS:
771                         if (vreg->supports_force_mode_bypass)
772                                 force_mode = 4;
773                         break;
774                 }
775
776                 if (force_mode == -1) {
777                         dev_err(&pdev->dev, "invalid force mode\n");
778                         return -EINVAL;
779                 }
780
781                 ret = rpm_reg_set(vreg, &vreg->parts->fm, force_mode);
782                 if (ret) {
783                         dev_err(&pdev->dev, "failed to set force mode\n");
784                         return ret;
785                 }
786         }
787
788         config.dev = &pdev->dev;
789         config.init_data = initdata;
790         config.driver_data = vreg;
791         config.of_node = pdev->dev.of_node;
792         rdev = devm_regulator_register(&pdev->dev, &vreg->desc, &config);
793         if (IS_ERR(rdev)) {
794                 dev_err(&pdev->dev, "can't register regulator\n");
795                 return PTR_ERR(rdev);
796         }
797
798         return 0;
799 }
800
801 static struct platform_driver rpm_reg_driver = {
802         .probe          = rpm_reg_probe,
803         .driver  = {
804                 .name  = "qcom_rpm_reg",
805                 .of_match_table = of_match_ptr(rpm_of_match),
806         },
807 };
808
809 static int __init rpm_reg_init(void)
810 {
811         return platform_driver_register(&rpm_reg_driver);
812 }
813 subsys_initcall(rpm_reg_init);
814
815 static void __exit rpm_reg_exit(void)
816 {
817         platform_driver_unregister(&rpm_reg_driver);
818 }
819 module_exit(rpm_reg_exit)
820
821 MODULE_DESCRIPTION("Qualcomm RPM regulator driver");
822 MODULE_LICENSE("GPL v2");