OSDN Git Service

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