blob: feddde8304cb28b9e2d8ed5faf6d8e2ee67b93d7 [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
78static int msm_spm_dev_set_low_power_mode(struct msm_spm_device *dev,
79 unsigned int mode, bool notify_rpm)
80{
81 uint32_t i;
82 uint32_t start_addr = 0;
83 int ret = -EINVAL;
84
85 if (mode == MSM_SPM_MODE_DISABLED) {
86 ret = msm_spm_drv_set_spm_enable(&dev->reg_data, false);
87 } else if (!msm_spm_drv_set_spm_enable(&dev->reg_data, true)) {
88 for (i = 0; i < dev->num_modes; i++) {
89 if ((dev->modes[i].mode == mode) &&
90 (dev->modes[i].notify_rpm == notify_rpm)) {
91 start_addr = dev->modes[i].start_addr;
92 break;
93 }
94 }
95 ret = msm_spm_drv_set_low_power_mode(&dev->reg_data,
96 start_addr);
97 }
98 return ret;
99}
100
Stephen Boyddb354112012-05-09 14:24:58 -0700101static int __devinit msm_spm_dev_init(struct msm_spm_device *dev,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102 struct msm_spm_platform_data *data)
103{
104 int i, ret = -ENOMEM;
105 uint32_t offset = 0;
106
107 dev->num_modes = data->num_modes;
108 dev->modes = kmalloc(
109 sizeof(struct msm_spm_power_modes) * dev->num_modes,
110 GFP_KERNEL);
111
112 if (!dev->modes)
113 goto spm_failed_malloc;
114
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600115 dev->reg_data.ver_reg = data->ver_reg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116 ret = msm_spm_drv_init(&dev->reg_data, data);
117
118 if (ret)
119 goto spm_failed_init;
120
121 for (i = 0; i < dev->num_modes; i++) {
122
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600123 /* Default offset is 0 and gets updated as we write more
124 * sequences into SPM
125 */
126 dev->modes[i].start_addr = offset;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127 ret = msm_spm_drv_write_seq_data(&dev->reg_data,
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600128 data->modes[i].cmd, &offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129 if (ret < 0)
130 goto spm_failed_init;
131
132 dev->modes[i].mode = data->modes[i].mode;
133 dev->modes[i].notify_rpm = data->modes[i].notify_rpm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134 }
135 msm_spm_drv_flush_seq_entry(&dev->reg_data);
136 return 0;
137
138spm_failed_init:
139 kfree(dev->modes);
140spm_failed_malloc:
141 return ret;
142}
143
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -0700144int msm_spm_turn_on_cpu_rail(unsigned int cpu)
145{
146 uint32_t val = 0;
147 uint32_t timeout = 0;
148 void *reg = NULL;
Stepan Moskovchenko2b0b06e2012-02-03 15:03:52 -0800149 void *saw_bases[] = {
150 0,
151 MSM_SAW1_BASE,
152 MSM_SAW2_BASE,
153 MSM_SAW3_BASE
154 };
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -0700155
Stepan Moskovchenko2b0b06e2012-02-03 15:03:52 -0800156 if (cpu == 0 || cpu >= num_possible_cpus())
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -0700157 return -EINVAL;
158
Stepan Moskovchenko2b0b06e2012-02-03 15:03:52 -0800159 reg = saw_bases[cpu];
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -0700160
Stepan Moskovchenko0df9bb22012-07-06 18:19:15 -0700161 if (cpu_is_msm8960() || cpu_is_msm8930() || cpu_is_msm8930aa() ||
Jay Chokshibae1cb52012-09-20 14:35:17 -0700162 cpu_is_apq8064() || cpu_is_msm8627() || cpu_is_msm8960ab() ||
163 cpu_is_apq8064ab()) {
Stepan Moskovchenko2b0b06e2012-02-03 15:03:52 -0800164 val = 0xA4;
165 reg += 0x14;
166 timeout = 512;
Praveen Chidambaramc0750ca2012-01-08 10:03:28 -0700167 } else {
168 return -ENOSYS;
169 }
170
171 writel_relaxed(val, reg);
172 mb();
173 udelay(timeout);
174
175 return 0;
176}
177EXPORT_SYMBOL(msm_spm_turn_on_cpu_rail);
178
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600179void msm_spm_reinit(void)
180{
181 unsigned int cpu;
182 for_each_possible_cpu(cpu)
183 msm_spm_drv_reinit(&per_cpu(msm_cpu_spm_device.reg_data, cpu));
184}
185EXPORT_SYMBOL(msm_spm_reinit);
186
187int msm_spm_set_low_power_mode(unsigned int mode, bool notify_rpm)
188{
189 struct msm_spm_device *dev = &__get_cpu_var(msm_cpu_spm_device);
190 return msm_spm_dev_set_low_power_mode(dev, mode, notify_rpm);
191}
192EXPORT_SYMBOL(msm_spm_set_low_power_mode);
193
194/* Board file init function */
195int __init msm_spm_init(struct msm_spm_platform_data *data, int nr_devs)
196{
197 unsigned int cpu;
198 int ret = 0;
199
200 BUG_ON((nr_devs < num_possible_cpus()) || !data);
201
202 for_each_possible_cpu(cpu) {
203 struct msm_spm_device *dev = &per_cpu(msm_cpu_spm_device, cpu);
204 ret = msm_spm_dev_init(dev, &data[cpu]);
205 if (ret < 0) {
206 pr_warn("%s():failed CPU:%u ret:%d\n", __func__,
207 cpu, ret);
208 break;
209 }
210 }
211
212 return ret;
213}
214
215#ifdef CONFIG_MSM_L2_SPM
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216
217int msm_spm_l2_set_low_power_mode(unsigned int mode, bool notify_rpm)
218{
219 return msm_spm_dev_set_low_power_mode(
220 &msm_spm_l2_device, mode, notify_rpm);
221}
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600222EXPORT_SYMBOL(msm_spm_l2_set_low_power_mode);
Maheshkumar Sivasubramanian4ac23762011-11-02 10:03:06 -0600223
224void msm_spm_l2_reinit(void)
225{
226 msm_spm_drv_reinit(&msm_spm_l2_device.reg_data);
227}
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600228EXPORT_SYMBOL(msm_spm_l2_reinit);
229
230int msm_spm_apcs_set_vdd(unsigned int vlevel)
231{
232 return msm_spm_drv_set_vdd(&msm_spm_l2_device.reg_data, vlevel);
233}
234EXPORT_SYMBOL(msm_spm_apcs_set_vdd);
235
236int msm_spm_apcs_set_phase(unsigned int phase_cnt)
237{
238 return msm_spm_drv_set_phase(&msm_spm_l2_device.reg_data, phase_cnt);
239}
240EXPORT_SYMBOL(msm_spm_apcs_set_phase);
241
242/* Board file init function */
243int __init msm_spm_l2_init(struct msm_spm_platform_data *data)
244{
245 return msm_spm_dev_init(&msm_spm_l2_device, data);
246}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247#endif
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600248
Sathish Ambley86487e52012-06-11 13:46:11 -0700249static int __devinit msm_spm_dev_probe(struct platform_device *pdev)
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600250{
251 int ret = 0;
252 int cpu = 0;
253 int i = 0;
254 struct device_node *node = pdev->dev.of_node;
255 struct msm_spm_platform_data spm_data;
256 char *key = NULL;
257 uint32_t val = 0;
258 struct msm_spm_seq_entry modes[MSM_SPM_MODE_NR];
259 size_t len = 0;
260 struct msm_spm_device *dev = NULL;
261 struct resource *res = NULL;
262 uint32_t mode_count = 0;
263
264 struct spm_of {
265 char *key;
266 uint32_t id;
267 };
268
269 struct spm_of spm_of_data[] = {
270 {"qcom,saw2-cfg", MSM_SPM_REG_SAW2_CFG},
271 {"qcom,saw2-avs-ctl", MSM_SPM_REG_SAW2_AVS_CTL},
272 {"qcom,saw2-avs-hysteresis", MSM_SPM_REG_SAW2_AVS_HYSTERESIS},
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600273 {"qcom,saw2-avs-limit", MSM_SPM_REG_SAW2_AVS_LIMIT},
Praveen Chidambaramce73c372012-08-22 11:50:34 -0600274 {"qcom,saw2-avs-dly", MSM_SPM_REG_SAW2_AVS_DLY},
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600275 {"qcom,saw2-spm-dly", MSM_SPM_REG_SAW2_SPM_DLY},
Praveen Chidambaramce73c372012-08-22 11:50:34 -0600276 {"qcom,saw2-spm-ctl", MSM_SPM_REG_SAW2_SPM_CTL},
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600277 {"qcom,saw2-pmic-data0", MSM_SPM_REG_SAW2_PMIC_DATA_0},
278 {"qcom,saw2-pmic-data1", MSM_SPM_REG_SAW2_PMIC_DATA_1},
279 {"qcom,saw2-pmic-data2", MSM_SPM_REG_SAW2_PMIC_DATA_2},
280 {"qcom,saw2-pmic-data3", MSM_SPM_REG_SAW2_PMIC_DATA_3},
281 {"qcom,saw2-pmic-data4", MSM_SPM_REG_SAW2_PMIC_DATA_4},
282 {"qcom,saw2-pmic-data5", MSM_SPM_REG_SAW2_PMIC_DATA_5},
283 {"qcom,saw2-pmic-data6", MSM_SPM_REG_SAW2_PMIC_DATA_6},
284 {"qcom,saw2-pmic-data7", MSM_SPM_REG_SAW2_PMIC_DATA_7},
285 };
286
287 struct mode_of {
288 char *key;
289 uint32_t id;
290 uint32_t notify_rpm;
291 };
292
Mahesh Sivasubramanian11373322012-06-14 11:17:20 -0600293 struct mode_of of_cpu_modes[] = {
294 {"qcom,saw2-spm-cmd-wfi", MSM_SPM_MODE_CLOCK_GATING, 0},
295 {"qcom,saw2-spm-cmd-ret", MSM_SPM_MODE_POWER_RETENTION, 0},
296 {"qcom,saw2-spm-cmd-spc", MSM_SPM_MODE_POWER_COLLAPSE, 0},
297 {"qcom,saw2-spm-cmd-pc", MSM_SPM_MODE_POWER_COLLAPSE, 1},
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600298 };
299
Mahesh Sivasubramanian11373322012-06-14 11:17:20 -0600300 struct mode_of of_l2_modes[] = {
301 {"qcom,saw2-spm-cmd-ret", MSM_SPM_L2_MODE_RETENTION, 1},
302 {"qcom,saw2-spm-cmd-gdhs", MSM_SPM_L2_MODE_GDHS, 1},
303 {"qcom,saw2-spm-cmd-pc", MSM_SPM_L2_MODE_POWER_COLLAPSE, 1},
304 };
305
306 struct mode_of *mode_of_data;
307 int num_modes;
308
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600309 memset(&spm_data, 0, sizeof(struct msm_spm_platform_data));
310 memset(&modes, 0,
311 (MSM_SPM_MODE_NR - 2) * sizeof(struct msm_spm_seq_entry));
312
313 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
314 if (!res)
315 goto fail;
316
317 spm_data.reg_base_addr = devm_ioremap(&pdev->dev, res->start,
318 resource_size(res));
319 if (!spm_data.reg_base_addr)
320 return -ENOMEM;
321
322 key = "qcom,core-id";
323 ret = of_property_read_u32(node, key, &val);
324 if (ret)
325 goto fail;
326 cpu = val;
327
328 key = "qcom,saw2-ver-reg";
329 ret = of_property_read_u32(node, key, &val);
330 if (ret)
331 goto fail;
332 spm_data.ver_reg = val;
333
334 key = "qcom,vctl-timeout-us";
335 ret = of_property_read_u32(node, key, &val);
336 if (!ret)
337 spm_data.vctl_timeout_us = val;
338
339 /* optional */
340 key = "qcom,vctl-port";
341 ret = of_property_read_u32(node, key, &val);
342 if (!ret)
343 spm_data.vctl_port = val;
344
345 /* optional */
346 key = "qcom,phase-port";
347 ret = of_property_read_u32(node, key, &val);
348 if (!ret)
349 spm_data.phase_port = val;
350
351 for (i = 0; i < ARRAY_SIZE(spm_of_data); i++) {
352 ret = of_property_read_u32(node, spm_of_data[i].key, &val);
353 if (ret)
354 continue;
355 spm_data.reg_init_values[spm_of_data[i].id] = val;
356 }
357
Mahesh Sivasubramanian11373322012-06-14 11:17:20 -0600358 /*
359 * Device with id 0..NR_CPUS are SPM for apps cores
360 * Device with id 0xFFFF is for L2 SPM.
361 */
362 if (cpu >= 0 && cpu < num_possible_cpus()) {
363 mode_of_data = of_cpu_modes;
364 num_modes = ARRAY_SIZE(of_cpu_modes);
365 dev = &per_cpu(msm_cpu_spm_device, cpu);
366
367 } else {
368 mode_of_data = of_l2_modes;
369 num_modes = ARRAY_SIZE(of_l2_modes);
370 dev = &msm_spm_l2_device;
371 }
372
373 for (i = 0; i < num_modes; i++) {
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600374 key = mode_of_data[i].key;
375 modes[mode_count].cmd =
376 (uint8_t *)of_get_property(node, key, &len);
377 if (!modes[mode_count].cmd)
378 continue;
379 modes[mode_count].mode = mode_of_data[i].id;
380 modes[mode_count].notify_rpm = mode_of_data[i].notify_rpm;
381 mode_count++;
382 }
383
384 spm_data.modes = modes;
385 spm_data.num_modes = mode_count;
386
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600387 ret = msm_spm_dev_init(dev, &spm_data);
Mahesh Sivasubramanian11373322012-06-14 11:17:20 -0600388
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600389 if (ret < 0)
390 pr_warn("%s():failed core-id:%u ret:%d\n", __func__, cpu, ret);
391
392 return ret;
393
394fail:
395 pr_err("%s: Failed reading node=%s, key=%s\n",
396 __func__, node->full_name, key);
397 return -EFAULT;
398}
399
Sathish Ambley86487e52012-06-11 13:46:11 -0700400static struct of_device_id msm_spm_match_table[] = {
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600401 {.compatible = "qcom,spm-v2"},
402 {},
403};
404
Sathish Ambley86487e52012-06-11 13:46:11 -0700405static struct platform_driver msm_spm_device_driver = {
Praveen Chidambaramaa9d52b2012-04-02 11:09:47 -0600406 .probe = msm_spm_dev_probe,
407 .driver = {
408 .name = "spm-v2",
409 .owner = THIS_MODULE,
410 .of_match_table = msm_spm_match_table,
411 },
412};
413
414int __init msm_spm_device_init(void)
415{
416 return platform_driver_register(&msm_spm_device_driver);
417}