2 * Copyright (c) 2016, Linaro Limited. All rights reserved.
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.
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.
14 #include <linux/delay.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.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>
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
33 struct regmap *regmap;
34 struct regulator_desc rdesc;
35 struct regulator_dev *rdev;
39 struct spm_vlevel_data {
40 struct saw_vreg *vreg;
44 static int saw_regulator_get_voltage_sel(struct regulator_dev *rdev)
46 struct saw_vreg *vreg = rdev_get_drvdata(rdev);
51 static void smp_set_vdd(void *data)
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;
57 u32 vctl, data0, data1;
58 unsigned long timeout;
60 if (vreg->sel == new_sel)
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);
79 data1 |= ((val & 0x3f) << 16);
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);
86 timeout = jiffies + usecs_to_jiffies(100);
88 regmap_read(vreg->regmap, SPM_REG_STS_1, &new_val);
97 } while (time_before(jiffies, timeout));
99 pr_err("%s: Voltage not changed: %#x\n", __func__, new_val);
102 static int saw_regulator_set_voltage_sel(struct regulator_dev *rdev,
105 struct saw_vreg *vreg = rdev_get_drvdata(rdev);
106 struct spm_vlevel_data data;
107 int cpu = rdev_get_id(rdev);
112 return smp_call_function_single(cpu, smp_set_vdd, &data, true);
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,
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),
129 .n_linear_ranges = 1,
134 static struct saw_vreg *saw_get_drv(struct platform_device *pdev,
137 struct saw_vreg *vreg = NULL;
138 struct device_node *cpu_node, *saw_node;
142 for_each_possible_cpu(cpu) {
143 cpu_node = of_cpu_device_node_get(cpu);
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);
155 vreg = devm_kzalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL);
163 static const struct of_device_id qcom_saw_regulator_match[] = {
164 { .compatible = "qcom,apq8064-saw2-v1.1-regulator" },
167 MODULE_DEVICE_TABLE(of, qcom_saw_regulator_match);
169 static int qcom_saw_regulator_probe(struct platform_device *pdev)
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";
179 vreg = saw_get_drv(pdev, &cpu);
183 saw_np = of_get_parent(np);
187 vreg->regmap = syscon_node_to_regmap(saw_np);
189 if (IS_ERR(config.regmap))
190 return PTR_ERR(config.regmap);
192 snprintf(name, sizeof(name), "krait%d", cpu);
194 config.regmap = vreg->regmap;
195 config.dev = &pdev->dev;
197 config.driver_data = vreg;
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,
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);
216 static struct platform_driver qcom_saw_regulator_driver = {
218 .name = "qcom-saw-regulator",
219 .of_match_table = qcom_saw_regulator_match,
221 .probe = qcom_saw_regulator_probe,
224 module_platform_driver(qcom_saw_regulator_driver);
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");