]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/regulator/qcom_saw-regulator.c
Merge branch 'akpm-current/current'
[karo-tx-linux.git] / drivers / regulator / qcom_saw-regulator.c
1 /*
2  * Copyright (c) 2016, Linaro Limited. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/delay.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/of_regulator.h>
24
25 #define SPM_REG_STS_1                   0x10
26 #define SPM_REG_VCTL                    0x14
27 #define SPM_REG_PMIC_DATA_0             0x28
28 #define SPM_REG_PMIC_DATA_1             0x2c
29 #define SPM_REG_RST                     0x30
30
31 struct saw_vreg {
32         struct device           *dev;
33         struct regmap           *regmap;
34         struct regulator_desc   rdesc;
35         struct regulator_dev    *rdev;
36         unsigned int            sel;
37 };
38
39 struct spm_vlevel_data {
40         struct saw_vreg *vreg;
41         unsigned int sel;
42 };
43
44 static int saw_regulator_get_voltage_sel(struct regulator_dev *rdev)
45 {
46         struct saw_vreg *vreg = rdev_get_drvdata(rdev);
47
48         return vreg->sel;
49 }
50
51 static void smp_set_vdd(void *data)
52 {
53         struct spm_vlevel_data *vdata = (struct spm_vlevel_data *)data;
54         struct saw_vreg *vreg = vdata->vreg;
55         unsigned long new_sel = vdata->sel;
56         u32 val, new_val;
57         u32 vctl, data0, data1;
58         unsigned long timeout;
59
60         if (vreg->sel == new_sel)
61                 return;
62
63         regmap_read(vreg->regmap, SPM_REG_VCTL, &vctl);
64         regmap_read(vreg->regmap, SPM_REG_PMIC_DATA_0, &data0);
65         regmap_read(vreg->regmap, SPM_REG_PMIC_DATA_1, &data1);
66
67         /* select the band */
68         val = 0x80 | new_sel;
69
70         vctl &= ~0xff;
71         vctl |= val;
72
73         data0 &= ~0xff;
74         data0 |= val;
75
76         data1 &= ~0x3f;
77         data1 |= val & 0x3f;
78         data1 &= ~0x3f0000;
79         data1 |= ((val & 0x3f) << 16);
80
81         regmap_write(vreg->regmap, SPM_REG_RST, 1);
82         regmap_write(vreg->regmap, SPM_REG_VCTL, vctl);
83         regmap_write(vreg->regmap, SPM_REG_PMIC_DATA_0, data0);
84         regmap_write(vreg->regmap, SPM_REG_PMIC_DATA_1, data1);
85
86         timeout = jiffies + usecs_to_jiffies(100);
87         do {
88                 regmap_read(vreg->regmap, SPM_REG_STS_1, &new_val);
89                 new_val &= 0xff;
90                 if (new_val == val) {
91                         vreg->sel = new_sel;
92                         return;
93                 }
94
95                 cpu_relax();
96
97         } while (time_before(jiffies, timeout));
98
99         pr_err("%s: Voltage not changed: %#x\n", __func__, new_val);
100 }
101
102 static int saw_regulator_set_voltage_sel(struct regulator_dev *rdev,
103                                          unsigned selector)
104 {
105         struct saw_vreg *vreg = rdev_get_drvdata(rdev);
106         struct spm_vlevel_data data;
107         int cpu = rdev_get_id(rdev);
108
109         data.vreg = vreg;
110         data.sel = selector;
111
112         return smp_call_function_single(cpu, smp_set_vdd, &data, true);
113 }
114
115 static struct regulator_ops saw_regulator_ops = {
116         .list_voltage = regulator_list_voltage_linear_range,
117         .set_voltage_sel = saw_regulator_set_voltage_sel,
118         .get_voltage_sel = saw_regulator_get_voltage_sel,
119         .set_voltage_time_sel = regulator_set_voltage_time_sel,
120 };
121
122 static struct regulator_desc saw_regulator = {
123         .owner = THIS_MODULE,
124         .type = REGULATOR_VOLTAGE,
125         .ops  = &saw_regulator_ops,
126         .linear_ranges = (struct regulator_linear_range[]) {
127                 REGULATOR_LINEAR_RANGE(700000, 0, 56, 12500),
128         },
129         .n_linear_ranges = 1,
130         .n_voltages = 57,
131         .ramp_delay = 1250,
132 };
133
134 static struct saw_vreg *saw_get_drv(struct platform_device *pdev,
135                                     int *vreg_cpu)
136 {
137         struct saw_vreg *vreg = NULL;
138         struct device_node *cpu_node, *saw_node;
139         int cpu;
140         bool found;
141
142         for_each_possible_cpu(cpu) {
143                 cpu_node = of_cpu_device_node_get(cpu);
144                 if (!cpu_node)
145                         continue;
146                 saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
147                 found = (saw_node == pdev->dev.of_node->parent);
148                 of_node_put(saw_node);
149                 of_node_put(cpu_node);
150                 if (found)
151                         break;
152         }
153
154         if (found) {
155                 vreg = devm_kzalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL);
156                 if (vreg)
157                         *vreg_cpu = cpu;
158         }
159
160         return vreg;
161 }
162
163 static const struct of_device_id qcom_saw_regulator_match[] = {
164         { .compatible = "qcom,apq8064-saw2-v1.1-regulator" },
165         { }
166 };
167 MODULE_DEVICE_TABLE(of, qcom_saw_regulator_match);
168
169 static int qcom_saw_regulator_probe(struct platform_device *pdev)
170 {
171         struct device *dev = &pdev->dev;
172         struct device_node *np = dev->of_node;
173         struct device_node *saw_np;
174         struct saw_vreg *vreg;
175         struct regulator_config config = { };
176         int ret = 0, cpu = 0;
177         char name[] = "kraitXX";
178
179         vreg = saw_get_drv(pdev, &cpu);
180         if (!vreg)
181                 return -EINVAL;
182
183         saw_np = of_get_parent(np);
184         if (!saw_np)
185                 return -ENODEV;
186
187         vreg->regmap = syscon_node_to_regmap(saw_np);
188         of_node_put(saw_np);
189         if (IS_ERR(config.regmap))
190                 return PTR_ERR(config.regmap);
191
192         snprintf(name, sizeof(name), "krait%d", cpu);
193
194         config.regmap = vreg->regmap;
195         config.dev = &pdev->dev;
196         config.of_node = np;
197         config.driver_data = vreg;
198
199         vreg->rdesc = saw_regulator;
200         vreg->rdesc.id = cpu;
201         vreg->rdesc.name = name;
202         config.init_data = of_get_regulator_init_data(&pdev->dev,
203                                                       pdev->dev.of_node,
204                                                       &vreg->rdesc);
205
206         vreg->rdev = devm_regulator_register(&pdev->dev, &vreg->rdesc, &config);
207         if (IS_ERR(vreg->rdev)) {
208                 ret = PTR_ERR(vreg->rdev);
209                 dev_err(dev, "failed to register SAW regulator: %d\n", ret);
210                 return ret;
211         }
212
213         return 0;
214 }
215
216 static struct platform_driver qcom_saw_regulator_driver = {
217         .driver = {
218                 .name = "qcom-saw-regulator",
219                 .of_match_table = qcom_saw_regulator_match,
220         },
221         .probe = qcom_saw_regulator_probe,
222 };
223
224 module_platform_driver(qcom_saw_regulator_driver);
225
226 MODULE_ALIAS("platform:qcom-saw-regulator");
227 MODULE_DESCRIPTION("Qualcomm SAW regulator driver");
228 MODULE_AUTHOR("Georgi Djakov <georgi.djakov@linaro.org>");
229 MODULE_LICENSE("GPL v2");