blob: db2c626af52f451bffdb1bc321f6698d714dbc70 [file] [log] [blame]
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -07001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/delay.h>
17#include <linux/init.h>
18#include <linux/io.h>
19#include <linux/slab.h>
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -060020#include <linux/of.h>
21#include <linux/of_address.h>
22#include <linux/platform_device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <mach/msm_iomap.h>
Praveen Chidambaram76679d42011-12-16 14:19:02 -070024#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include "spm.h"
26#include "spm_driver.h"
27
28struct msm_spm_power_modes {
29 uint32_t mode;
30 bool notify_rpm;
31 uint32_t start_addr;
32
33};
34
35struct msm_spm_device {
36 struct msm_spm_driver_data reg_data;
37 struct msm_spm_power_modes *modes;
38 uint32_t num_modes;
39};
40
Praveen Chidambaram6a8fb3b2012-09-16 14:54:35 -060041struct msm_spm_vdd_info {
42 uint32_t cpu;
43 uint32_t vlevel;
44 int err;
45};
46
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -060047static struct msm_spm_device msm_spm_l2_device;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048static DEFINE_PER_CPU_SHARED_ALIGNED(struct msm_spm_device, msm_cpu_spm_device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049
Praveen Chidambaram6a8fb3b2012-09-16 14:54:35 -060050
51/* Must be called on the same cpu as the one being set to */
52static void msm_spm_smp_set_vdd(void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054 struct msm_spm_device *dev;
Praveen Chidambaram6a8fb3b2012-09-16 14:54:35 -060055 struct msm_spm_vdd_info *info = (struct msm_spm_vdd_info *)data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056
Praveen Chidambaram6a8fb3b2012-09-16 14:54:35 -060057 dev = &per_cpu(msm_cpu_spm_device, info->cpu);
58 info->err = msm_spm_drv_set_vdd(&dev->reg_data, info->vlevel);
59}
60
61int msm_spm_set_vdd(unsigned int cpu, unsigned int vlevel)
62{
63 struct msm_spm_vdd_info info;
64 int ret;
65
66 info.cpu = cpu;
67 info.vlevel = vlevel;
68
69 /* Set to true to block on vdd change */
70 ret = smp_call_function_single(cpu, msm_spm_smp_set_vdd, &info, true);
71 if (!ret)
72 ret = info.err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070073
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074 return ret;
75}
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -060076EXPORT_SYMBOL(msm_spm_set_vdd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077
Praveen Chidambaramebbf2122012-09-29 22:27:03 -060078unsigned int msm_spm_get_vdd(unsigned int cpu)
79{
80 struct msm_spm_device *dev;
81
82 dev = &per_cpu(msm_cpu_spm_device, cpu);
83 return msm_spm_drv_get_sts_curr_pmic_data(&dev->reg_data);
84}
85EXPORT_SYMBOL(msm_spm_get_vdd);
86
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087static int msm_spm_dev_set_low_power_mode(struct msm_spm_device *dev,
88 unsigned int mode, bool notify_rpm)
89{
90 uint32_t i;
91 uint32_t start_addr = 0;
92 int ret = -EINVAL;
93
94 if (mode == MSM_SPM_MODE_DISABLED) {
95 ret = msm_spm_drv_set_spm_enable(&dev->reg_data, false);
96 } else if (!msm_spm_drv_set_spm_enable(&dev->reg_data, true)) {
97 for (i = 0; i < dev->num_modes; i++) {
98 if ((dev->modes[i].mode == mode) &&
99 (dev->modes[i].notify_rpm == notify_rpm)) {
100 start_addr = dev->modes[i].start_addr;
101 break;
102 }
103 }
104 ret = msm_spm_drv_set_low_power_mode(&dev->reg_data,
105 start_addr);
106 }
107 return ret;
108}
109
Stephen Boyddb354112012-05-09 14:24:58 -0700110static int __devinit msm_spm_dev_init(struct msm_spm_device *dev,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111 struct msm_spm_platform_data *data)
112{
113 int i, ret = -ENOMEM;
114 uint32_t offset = 0;
115
116 dev->num_modes = data->num_modes;
117 dev->modes = kmalloc(
118 sizeof(struct msm_spm_power_modes) * dev->num_modes,
119 GFP_KERNEL);
120
121 if (!dev->modes)
122 goto spm_failed_malloc;
123
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600124 dev->reg_data.ver_reg = data->ver_reg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125 ret = msm_spm_drv_init(&dev->reg_data, data);
126
127 if (ret)
128 goto spm_failed_init;
129
130 for (i = 0; i < dev->num_modes; i++) {
131
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600132 /* Default offset is 0 and gets updated as we write more
133 * sequences into SPM
134 */
135 dev->modes[i].start_addr = offset;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136 ret = msm_spm_drv_write_seq_data(&dev->reg_data,
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600137 data->modes[i].cmd, &offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138 if (ret < 0)
139 goto spm_failed_init;
140
141 dev->modes[i].mode = data->modes[i].mode;
142 dev->modes[i].notify_rpm = data->modes[i].notify_rpm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 }
144 msm_spm_drv_flush_seq_entry(&dev->reg_data);
145 return 0;
146
147spm_failed_init:
148 kfree(dev->modes);
149spm_failed_malloc:
150 return ret;
151}
152
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -0700153int msm_spm_turn_on_cpu_rail(unsigned int cpu)
154{
155 uint32_t val = 0;
156 uint32_t timeout = 0;
157 void *reg = NULL;
Stepan Moskovchenko2b0b06e2012-02-03 15:03:52 -0800158 void *saw_bases[] = {
159 0,
160 MSM_SAW1_BASE,
161 MSM_SAW2_BASE,
162 MSM_SAW3_BASE
163 };
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -0700164
Stepan Moskovchenko2b0b06e2012-02-03 15:03:52 -0800165 if (cpu == 0 || cpu >= num_possible_cpus())
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -0700166 return -EINVAL;
167
Stepan Moskovchenko2b0b06e2012-02-03 15:03:52 -0800168 reg = saw_bases[cpu];
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -0700169
Stepan Moskovchenkoc6a603a2012-09-21 20:32:17 -0700170 if (soc_class_is_msm8960() || soc_class_is_msm8930() ||
171 soc_class_is_apq8064()) {
Stepan Moskovchenko2b0b06e2012-02-03 15:03:52 -0800172 val = 0xA4;
173 reg += 0x14;
174 timeout = 512;
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -0700175 } else {
176 return -ENOSYS;
177 }
178
179 writel_relaxed(val, reg);
180 mb();
181 udelay(timeout);
182
183 return 0;
184}
185EXPORT_SYMBOL(msm_spm_turn_on_cpu_rail);
186
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600187void msm_spm_reinit(void)
188{
189 unsigned int cpu;
190 for_each_possible_cpu(cpu)
191 msm_spm_drv_reinit(&per_cpu(msm_cpu_spm_device.reg_data, cpu));
192}
193EXPORT_SYMBOL(msm_spm_reinit);
194
195int msm_spm_set_low_power_mode(unsigned int mode, bool notify_rpm)
196{
197 struct msm_spm_device *dev = &__get_cpu_var(msm_cpu_spm_device);
198 return msm_spm_dev_set_low_power_mode(dev, mode, notify_rpm);
199}
200EXPORT_SYMBOL(msm_spm_set_low_power_mode);
201
202/* Board file init function */
203int __init msm_spm_init(struct msm_spm_platform_data *data, int nr_devs)
204{
205 unsigned int cpu;
206 int ret = 0;
207
208 BUG_ON((nr_devs < num_possible_cpus()) || !data);
209
210 for_each_possible_cpu(cpu) {
211 struct msm_spm_device *dev = &per_cpu(msm_cpu_spm_device, cpu);
212 ret = msm_spm_dev_init(dev, &data[cpu]);
213 if (ret < 0) {
214 pr_warn("%s():failed CPU:%u ret:%d\n", __func__,
215 cpu, ret);
216 break;
217 }
218 }
219
220 return ret;
221}
222
223#ifdef CONFIG_MSM_L2_SPM
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224
225int msm_spm_l2_set_low_power_mode(unsigned int mode, bool notify_rpm)
226{
227 return msm_spm_dev_set_low_power_mode(
228 &msm_spm_l2_device, mode, notify_rpm);
229}
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600230EXPORT_SYMBOL(msm_spm_l2_set_low_power_mode);
Maheshkumar Sivasubramanian4ac23762011-11-02 10:03:06 -0600231
232void msm_spm_l2_reinit(void)
233{
234 msm_spm_drv_reinit(&msm_spm_l2_device.reg_data);
235}
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600236EXPORT_SYMBOL(msm_spm_l2_reinit);
237
238int msm_spm_apcs_set_vdd(unsigned int vlevel)
239{
240 return msm_spm_drv_set_vdd(&msm_spm_l2_device.reg_data, vlevel);
241}
242EXPORT_SYMBOL(msm_spm_apcs_set_vdd);
243
244int msm_spm_apcs_set_phase(unsigned int phase_cnt)
245{
246 return msm_spm_drv_set_phase(&msm_spm_l2_device.reg_data, phase_cnt);
247}
248EXPORT_SYMBOL(msm_spm_apcs_set_phase);
249
250/* Board file init function */
251int __init msm_spm_l2_init(struct msm_spm_platform_data *data)
252{
253 return msm_spm_dev_init(&msm_spm_l2_device, data);
254}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255#endif
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600256
Sathish Ambley86487e52012-06-11 13:46:11 -0700257static int __devinit msm_spm_dev_probe(struct platform_device *pdev)
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600258{
259 int ret = 0;
260 int cpu = 0;
261 int i = 0;
262 struct device_node *node = pdev->dev.of_node;
263 struct msm_spm_platform_data spm_data;
264 char *key = NULL;
265 uint32_t val = 0;
266 struct msm_spm_seq_entry modes[MSM_SPM_MODE_NR];
267 size_t len = 0;
268 struct msm_spm_device *dev = NULL;
269 struct resource *res = NULL;
270 uint32_t mode_count = 0;
271
272 struct spm_of {
273 char *key;
274 uint32_t id;
275 };
276
277 struct spm_of spm_of_data[] = {
278 {"qcom,saw2-cfg", MSM_SPM_REG_SAW2_CFG},
279 {"qcom,saw2-avs-ctl", MSM_SPM_REG_SAW2_AVS_CTL},
280 {"qcom,saw2-avs-hysteresis", MSM_SPM_REG_SAW2_AVS_HYSTERESIS},
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600281 {"qcom,saw2-avs-limit", MSM_SPM_REG_SAW2_AVS_LIMIT},
Praveen Chidambaramce73c372012-08-22 11:50:34 -0600282 {"qcom,saw2-avs-dly", MSM_SPM_REG_SAW2_AVS_DLY},
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600283 {"qcom,saw2-spm-dly", MSM_SPM_REG_SAW2_SPM_DLY},
Praveen Chidambaramce73c372012-08-22 11:50:34 -0600284 {"qcom,saw2-spm-ctl", MSM_SPM_REG_SAW2_SPM_CTL},
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600285 {"qcom,saw2-pmic-data0", MSM_SPM_REG_SAW2_PMIC_DATA_0},
286 {"qcom,saw2-pmic-data1", MSM_SPM_REG_SAW2_PMIC_DATA_1},
287 {"qcom,saw2-pmic-data2", MSM_SPM_REG_SAW2_PMIC_DATA_2},
288 {"qcom,saw2-pmic-data3", MSM_SPM_REG_SAW2_PMIC_DATA_3},
289 {"qcom,saw2-pmic-data4", MSM_SPM_REG_SAW2_PMIC_DATA_4},
290 {"qcom,saw2-pmic-data5", MSM_SPM_REG_SAW2_PMIC_DATA_5},
291 {"qcom,saw2-pmic-data6", MSM_SPM_REG_SAW2_PMIC_DATA_6},
292 {"qcom,saw2-pmic-data7", MSM_SPM_REG_SAW2_PMIC_DATA_7},
293 };
294
295 struct mode_of {
296 char *key;
297 uint32_t id;
298 uint32_t notify_rpm;
299 };
300
Mahesh Sivasubramanian11373322012-06-14 11:17:20 -0600301 struct mode_of of_cpu_modes[] = {
302 {"qcom,saw2-spm-cmd-wfi", MSM_SPM_MODE_CLOCK_GATING, 0},
303 {"qcom,saw2-spm-cmd-ret", MSM_SPM_MODE_POWER_RETENTION, 0},
304 {"qcom,saw2-spm-cmd-spc", MSM_SPM_MODE_POWER_COLLAPSE, 0},
305 {"qcom,saw2-spm-cmd-pc", MSM_SPM_MODE_POWER_COLLAPSE, 1},
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600306 };
307
Mahesh Sivasubramanian11373322012-06-14 11:17:20 -0600308 struct mode_of of_l2_modes[] = {
309 {"qcom,saw2-spm-cmd-ret", MSM_SPM_L2_MODE_RETENTION, 1},
310 {"qcom,saw2-spm-cmd-gdhs", MSM_SPM_L2_MODE_GDHS, 1},
311 {"qcom,saw2-spm-cmd-pc", MSM_SPM_L2_MODE_POWER_COLLAPSE, 1},
312 };
313
314 struct mode_of *mode_of_data;
315 int num_modes;
316
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600317 memset(&spm_data, 0, sizeof(struct msm_spm_platform_data));
318 memset(&modes, 0,
319 (MSM_SPM_MODE_NR - 2) * sizeof(struct msm_spm_seq_entry));
320
321 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
322 if (!res)
323 goto fail;
324
325 spm_data.reg_base_addr = devm_ioremap(&pdev->dev, res->start,
326 resource_size(res));
327 if (!spm_data.reg_base_addr)
328 return -ENOMEM;
329
330 key = "qcom,core-id";
331 ret = of_property_read_u32(node, key, &val);
332 if (ret)
333 goto fail;
334 cpu = val;
335
336 key = "qcom,saw2-ver-reg";
337 ret = of_property_read_u32(node, key, &val);
338 if (ret)
339 goto fail;
340 spm_data.ver_reg = val;
341
342 key = "qcom,vctl-timeout-us";
343 ret = of_property_read_u32(node, key, &val);
344 if (!ret)
345 spm_data.vctl_timeout_us = val;
346
347 /* optional */
348 key = "qcom,vctl-port";
349 ret = of_property_read_u32(node, key, &val);
350 if (!ret)
351 spm_data.vctl_port = val;
352
353 /* optional */
354 key = "qcom,phase-port";
355 ret = of_property_read_u32(node, key, &val);
356 if (!ret)
357 spm_data.phase_port = val;
358
359 for (i = 0; i < ARRAY_SIZE(spm_of_data); i++) {
360 ret = of_property_read_u32(node, spm_of_data[i].key, &val);
361 if (ret)
362 continue;
363 spm_data.reg_init_values[spm_of_data[i].id] = val;
364 }
365
Mahesh Sivasubramanian11373322012-06-14 11:17:20 -0600366 /*
367 * Device with id 0..NR_CPUS are SPM for apps cores
368 * Device with id 0xFFFF is for L2 SPM.
369 */
370 if (cpu >= 0 && cpu < num_possible_cpus()) {
371 mode_of_data = of_cpu_modes;
372 num_modes = ARRAY_SIZE(of_cpu_modes);
373 dev = &per_cpu(msm_cpu_spm_device, cpu);
374
375 } else {
376 mode_of_data = of_l2_modes;
377 num_modes = ARRAY_SIZE(of_l2_modes);
378 dev = &msm_spm_l2_device;
379 }
380
381 for (i = 0; i < num_modes; i++) {
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600382 key = mode_of_data[i].key;
383 modes[mode_count].cmd =
384 (uint8_t *)of_get_property(node, key, &len);
385 if (!modes[mode_count].cmd)
386 continue;
387 modes[mode_count].mode = mode_of_data[i].id;
388 modes[mode_count].notify_rpm = mode_of_data[i].notify_rpm;
389 mode_count++;
390 }
391
392 spm_data.modes = modes;
393 spm_data.num_modes = mode_count;
394
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600395 ret = msm_spm_dev_init(dev, &spm_data);
Mahesh Sivasubramanian11373322012-06-14 11:17:20 -0600396
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600397 if (ret < 0)
398 pr_warn("%s():failed core-id:%u ret:%d\n", __func__, cpu, ret);
399
400 return ret;
401
402fail:
403 pr_err("%s: Failed reading node=%s, key=%s\n",
404 __func__, node->full_name, key);
405 return -EFAULT;
406}
407
Sathish Ambley86487e52012-06-11 13:46:11 -0700408static struct of_device_id msm_spm_match_table[] = {
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600409 {.compatible = "qcom,spm-v2"},
410 {},
411};
412
Sathish Ambley86487e52012-06-11 13:46:11 -0700413static struct platform_driver msm_spm_device_driver = {
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600414 .probe = msm_spm_dev_probe,
415 .driver = {
416 .name = "spm-v2",
417 .owner = THIS_MODULE,
418 .of_match_table = msm_spm_match_table,
419 },
420};
421
422int __init msm_spm_device_init(void)
423{
424 return platform_driver_register(&msm_spm_device_driver);
425}