1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
|
/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/cpu.h>
#include <linux/notifier.h>
#include <linux/hrtimer.h>
#include <linux/tick.h>
#include <mach/mpm.h>
#include <mach/rpm-smd.h>
#include "spm.h"
#include "lpm_resources.h"
#include "rpm-notifier.h"
#include "idle.h"
/*Debug Definitions*/
enum {
MSM_LPMRS_DEBUG_RPM = BIT(0),
MSM_LPMRS_DEBUG_PXO = BIT(1),
MSM_LPMRS_DEBUG_VDD_DIG = BIT(2),
MSM_LPMRS_DEBUG_VDD_MEM = BIT(3),
MSM_LPMRS_DEBUG_L2 = BIT(4),
MSM_LPMRS_DEBUG_LVLS = BIT(5),
};
static int msm_lpm_debug_mask;
module_param_named(
debug_mask, msm_lpm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
);
static bool msm_lpm_get_rpm_notif = true;
/*Macros*/
#define VDD_DIG_ACTIVE (5)
#define VDD_MEM_ACTIVE (1050000)
#define MAX_RS_NAME (16)
#define MAX_RS_SIZE (4)
#define IS_RPM_CTL(rs) \
(!strncmp(rs->name, "rpm_ctl", MAX_RS_NAME))
static bool msm_lpm_beyond_limits_vdd_dig(struct msm_rpmrs_limits *limits);
static void msm_lpm_aggregate_vdd_dig(struct msm_rpmrs_limits *limits);
static void msm_lpm_flush_vdd_dig(int notify_rpm);
static void msm_lpm_notify_vdd_dig(struct msm_rpm_notifier_data
*rpm_notifier_cb);
static bool msm_lpm_beyond_limits_vdd_mem(struct msm_rpmrs_limits *limits);
static void msm_lpm_aggregate_vdd_mem(struct msm_rpmrs_limits *limits);
static void msm_lpm_flush_vdd_mem(int notify_rpm);
static void msm_lpm_notify_vdd_mem(struct msm_rpm_notifier_data
*rpm_notifier_cb);
static bool msm_lpm_beyond_limits_pxo(struct msm_rpmrs_limits *limits);
static void msm_lpm_aggregate_pxo(struct msm_rpmrs_limits *limits);
static void msm_lpm_flush_pxo(int notify_rpm);
static void msm_lpm_notify_pxo(struct msm_rpm_notifier_data
*rpm_notifier_cb);
static bool msm_lpm_beyond_limits_l2(struct msm_rpmrs_limits *limits);
static void msm_lpm_flush_l2(int notify_rpm);
static void msm_lpm_aggregate_l2(struct msm_rpmrs_limits *limits);
static void msm_lpm_flush_rpm_ctl(int notify_rpm);
static int msm_lpm_rpm_callback(struct notifier_block *rpm_nb,
unsigned long action, void *rpm_notif);
static int msm_lpm_cpu_callback(struct notifier_block *cpu_nb,
unsigned long action, void *hcpu);
static ssize_t msm_lpm_resource_attr_show(
struct kobject *kobj, struct kobj_attribute *attr, char *buf);
static ssize_t msm_lpm_resource_attr_store(struct kobject *kobj,
struct kobj_attribute *attr, const char *buf, size_t count);
#define RPMRS_ATTR(_name) \
__ATTR(_name, S_IRUGO|S_IWUSR, \
msm_lpm_resource_attr_show, msm_lpm_resource_attr_store)
/*Data structures*/
struct msm_lpm_rs_data {
uint32_t type;
uint32_t id;
uint32_t key;
uint32_t value;
uint32_t default_value;
struct msm_rpm_request *handle;
};
struct msm_lpm_resource {
struct msm_lpm_rs_data rs_data;
uint32_t sleep_value;
char name[MAX_RS_NAME];
uint32_t enable_low_power;
bool valid;
bool (*beyond_limits)(struct msm_rpmrs_limits *limits);
void (*aggregate)(struct msm_rpmrs_limits *limits);
void (*flush)(int notify_rpm);
void (*notify)(struct msm_rpm_notifier_data *rpm_notifier_cb);
struct kobj_attribute ko_attr;
};
static struct msm_lpm_resource msm_lpm_l2 = {
.name = "l2",
.beyond_limits = msm_lpm_beyond_limits_l2,
.aggregate = msm_lpm_aggregate_l2,
.flush = msm_lpm_flush_l2,
.notify = NULL,
.valid = true,
.rs_data = {
.value = MSM_LPM_L2_CACHE_ACTIVE,
.default_value = MSM_LPM_L2_CACHE_ACTIVE,
},
.ko_attr = RPMRS_ATTR(l2),
};
static struct msm_lpm_resource msm_lpm_vdd_dig = {
.name = "vdd-dig",
.beyond_limits = msm_lpm_beyond_limits_vdd_dig,
.aggregate = msm_lpm_aggregate_vdd_dig,
.flush = msm_lpm_flush_vdd_dig,
.notify = msm_lpm_notify_vdd_dig,
.valid = false,
.rs_data = {
.value = VDD_DIG_ACTIVE,
.default_value = VDD_DIG_ACTIVE,
},
.ko_attr = RPMRS_ATTR(vdd_dig),
};
static struct msm_lpm_resource msm_lpm_vdd_mem = {
.name = "vdd-mem",
.beyond_limits = msm_lpm_beyond_limits_vdd_mem,
.aggregate = msm_lpm_aggregate_vdd_mem,
.flush = msm_lpm_flush_vdd_mem,
.notify = msm_lpm_notify_vdd_mem,
.valid = false,
.rs_data = {
.value = VDD_MEM_ACTIVE,
.default_value = VDD_MEM_ACTIVE,
},
.ko_attr = RPMRS_ATTR(vdd_mem),
};
static struct msm_lpm_resource msm_lpm_pxo = {
.name = "pxo",
.beyond_limits = msm_lpm_beyond_limits_pxo,
.aggregate = msm_lpm_aggregate_pxo,
.flush = msm_lpm_flush_pxo,
.notify = msm_lpm_notify_pxo,
.valid = false,
.rs_data = {
.value = MSM_LPM_PXO_ON,
.default_value = MSM_LPM_PXO_ON,
},
.ko_attr = RPMRS_ATTR(pxo),
};
static struct msm_lpm_resource *msm_lpm_resources[] = {
&msm_lpm_vdd_dig,
&msm_lpm_vdd_mem,
&msm_lpm_pxo,
&msm_lpm_l2,
};
static struct msm_lpm_resource msm_lpm_rpm_ctl = {
.name = "rpm_ctl",
.beyond_limits = NULL,
.aggregate = NULL,
.flush = msm_lpm_flush_rpm_ctl,
.valid = true,
.ko_attr = RPMRS_ATTR(rpm_ctl),
};
static struct notifier_block msm_lpm_rpm_nblk = {
.notifier_call = msm_lpm_rpm_callback,
};
static struct notifier_block __refdata msm_lpm_cpu_nblk = {
.notifier_call = msm_lpm_cpu_callback,
};
static DEFINE_SPINLOCK(msm_lpm_sysfs_lock);
/* Attribute Definitions */
static struct attribute *msm_lpm_attributes[] = {
&msm_lpm_vdd_dig.ko_attr.attr,
&msm_lpm_vdd_mem.ko_attr.attr,
&msm_lpm_pxo.ko_attr.attr,
&msm_lpm_l2.ko_attr.attr,
NULL,
};
static struct attribute_group msm_lpm_attribute_group = {
.attrs = msm_lpm_attributes,
};
static struct attribute *msm_lpm_rpm_ctl_attribute[] = {
&msm_lpm_rpm_ctl.ko_attr.attr,
NULL,
};
static struct attribute_group msm_lpm_rpm_ctl_attr_group = {
.attrs = msm_lpm_rpm_ctl_attribute,
};
#define GET_RS_FROM_ATTR(attr) \
(container_of(attr, struct msm_lpm_resource, ko_attr))
/* RPM */
static struct msm_rpm_request *msm_lpm_create_rpm_request
(uint32_t rsc_type, uint32_t rsc_id)
{
struct msm_rpm_request *handle = NULL;
handle = msm_rpm_create_request(MSM_RPM_CTX_SLEEP_SET,
rsc_type,
rsc_id, 1);
return handle;
}
static int msm_lpm_send_sleep_data(struct msm_rpm_request *handle,
uint32_t key, uint8_t *value)
{
int ret = 0;
int msg_id;
if (!handle)
return ret;
ret = msm_rpm_add_kvp_data_noirq(handle, key, value, MAX_RS_SIZE);
if (ret < 0) {
pr_err("%s: Error adding kvp data key %u, size %d\n",
__func__, key, MAX_RS_SIZE);
return ret;
}
msg_id = msm_rpm_send_request_noirq(handle);
if (!msg_id) {
pr_err("%s: Error sending RPM request key %u, handle 0x%x\n",
__func__, key, (unsigned int)handle);
ret = -EIO;
return ret;
}
ret = msm_rpm_wait_for_ack_noirq(msg_id);
if (ret < 0) {
pr_err("%s: Couldn't get ACK from RPM for Msg %d Error %d",
__func__, msg_id, ret);
return ret;
}
if (msm_lpm_debug_mask & MSM_LPMRS_DEBUG_RPM)
pr_info("Rs key %u, value %u, size %d\n", key,
*(unsigned int *)value, MAX_RS_SIZE);
return ret;
}
/* RPM Notifier */
static int msm_lpm_rpm_callback(struct notifier_block *rpm_nb,
unsigned long action,
void *rpm_notif)
{
int i;
struct msm_lpm_resource *rs = NULL;
struct msm_rpm_notifier_data *rpm_notifier_cb =
(struct msm_rpm_notifier_data *)rpm_notif;
if (!msm_lpm_get_rpm_notif)
return NOTIFY_DONE;
if (!(rpm_nb && rpm_notif))
return NOTIFY_BAD;
for (i = 0; i < ARRAY_SIZE(msm_lpm_resources); i++) {
rs = msm_lpm_resources[i];
if (rs && rs->valid && rs->notify)
rs->notify(rpm_notifier_cb);
}
return NOTIFY_OK;
}
/* SYSFS */
static ssize_t msm_lpm_resource_attr_show(
struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
struct kernel_param kp;
unsigned long flags;
unsigned int temp;
int rc;
spin_lock_irqsave(&msm_lpm_sysfs_lock, flags);
temp = GET_RS_FROM_ATTR(attr)->enable_low_power;
spin_unlock_irqrestore(&msm_lpm_sysfs_lock, flags);
kp.arg = &temp;
rc = param_get_uint(buf, &kp);
if (rc > 0) {
strlcat(buf, "\n", PAGE_SIZE);
rc++;
}
return rc;
}
static ssize_t msm_lpm_resource_attr_store(struct kobject *kobj,
struct kobj_attribute *attr, const char *buf, size_t count)
{
struct kernel_param kp;
unsigned long flags;
unsigned int temp;
int rc;
kp.arg = &temp;
rc = param_set_uint(buf, &kp);
if (rc)
return rc;
spin_lock_irqsave(&msm_lpm_sysfs_lock, flags);
GET_RS_FROM_ATTR(attr)->enable_low_power = temp;
if (IS_RPM_CTL(GET_RS_FROM_ATTR(attr))) {
struct msm_lpm_resource *rs = GET_RS_FROM_ATTR(attr);
rs->flush(false);
}
spin_unlock_irqrestore(&msm_lpm_sysfs_lock, flags);
return count;
}
/* lpm resource handling functions */
/* Common */
static void msm_lpm_notify_common(struct msm_rpm_notifier_data *cb,
struct msm_lpm_resource *rs)
{
if ((cb->rsc_type == rs->rs_data.type) &&
(cb->rsc_id == rs->rs_data.id) &&
(cb->key == rs->rs_data.key)) {
BUG_ON(cb->size > MAX_RS_SIZE);
if (rs->valid) {
if (cb->value) {
memcpy(&rs->rs_data.value, cb->value, cb->size);
msm_rpm_add_kvp_data_noirq(rs->rs_data.handle,
cb->key, cb->value, cb->size);
}
else
rs->rs_data.value = rs->rs_data.default_value;
if (msm_lpm_debug_mask & MSM_LPMRS_DEBUG_RPM)
pr_info("Notification received Rs %s value %u\n",
rs->name, rs->rs_data.value);
}
}
}
/* L2 */
static bool msm_lpm_beyond_limits_l2(struct msm_rpmrs_limits *limits)
{
uint32_t l2;
bool ret = true;
struct msm_lpm_resource *rs = &msm_lpm_l2;
if (rs->valid) {
uint32_t l2_buf = rs->rs_data.value;
if (rs->enable_low_power == 1)
l2 = MSM_LPM_L2_CACHE_GDHS;
else if (rs->enable_low_power == 2)
l2 = MSM_LPM_L2_CACHE_HSFS_OPEN;
else
l2 = MSM_LPM_L2_CACHE_ACTIVE ;
if (l2_buf > l2)
l2 = l2_buf;
ret = (l2 > limits->l2_cache);
if (msm_lpm_debug_mask & MSM_LPMRS_DEBUG_L2)
pr_info("%s: l2 buf %u, l2 %u, limits %u\n",
__func__, l2_buf, l2, limits->l2_cache);
}
return ret;
}
static void msm_lpm_aggregate_l2(struct msm_rpmrs_limits *limits)
{
struct msm_lpm_resource *rs = &msm_lpm_l2;
if (rs->valid)
rs->sleep_value = limits->l2_cache;
}
static void msm_lpm_flush_l2(int notify_rpm)
{
struct msm_lpm_resource *rs = &msm_lpm_l2;
int lpm;
int rc;
switch (rs->sleep_value) {
case MSM_LPM_L2_CACHE_HSFS_OPEN:
lpm = MSM_SPM_L2_MODE_POWER_COLLAPSE;
msm_pm_set_l2_flush_flag(1);
break;
case MSM_LPM_L2_CACHE_GDHS:
lpm = MSM_SPM_L2_MODE_GDHS;
break;
case MSM_LPM_L2_CACHE_RETENTION:
lpm = MSM_SPM_L2_MODE_RETENTION;
break;
default:
case MSM_LPM_L2_CACHE_ACTIVE:
lpm = MSM_SPM_L2_MODE_DISABLED;
break;
}
rc = msm_spm_l2_set_low_power_mode(lpm, notify_rpm);
if (rc < 0)
pr_err("%s: Failed to set L2 low power mode %d",
__func__, lpm);
if (msm_lpm_debug_mask & MSM_LPMRS_DEBUG_L2)
pr_info("%s: Requesting low power mode %d\n",
__func__, lpm);
}
/* RPM CTL */
static void msm_lpm_flush_rpm_ctl(int notify_rpm)
{
struct msm_lpm_resource *rs = &msm_lpm_rpm_ctl;
msm_lpm_send_sleep_data(rs->rs_data.handle,
rs->rs_data.key,
(uint8_t *)&rs->sleep_value);
}
/*VDD Dig*/
static bool msm_lpm_beyond_limits_vdd_dig(struct msm_rpmrs_limits *limits)
{
bool ret = true;
struct msm_lpm_resource *rs = &msm_lpm_vdd_dig;
if (rs->valid) {
uint32_t vdd_buf = rs->rs_data.value;
uint32_t vdd_dig = rs->enable_low_power ? rs->enable_low_power :
rs->rs_data.default_value;
if (vdd_buf > vdd_dig)
vdd_dig = vdd_buf;
ret = (vdd_dig > limits->vdd_dig_upper_bound);
if (msm_lpm_debug_mask & MSM_LPMRS_DEBUG_VDD_DIG)
pr_info("%s:buf %d vdd dig %d limits%d\n",
__func__, vdd_buf, vdd_dig,
limits->vdd_dig_upper_bound);
}
return ret;
}
static void msm_lpm_aggregate_vdd_dig(struct msm_rpmrs_limits *limits)
{
struct msm_lpm_resource *rs = &msm_lpm_vdd_dig;
if (rs->valid) {
uint32_t vdd_buf = rs->rs_data.value;
if (limits->vdd_dig_lower_bound > vdd_buf)
rs->sleep_value = limits->vdd_dig_lower_bound;
else
rs->sleep_value = vdd_buf;
}
}
static void msm_lpm_flush_vdd_dig(int notify_rpm)
{
if (notify_rpm) {
struct msm_lpm_resource *rs = &msm_lpm_vdd_dig;
msm_lpm_send_sleep_data(rs->rs_data.handle,
rs->rs_data.key,
(uint8_t *)&rs->sleep_value);
}
}
static void msm_lpm_notify_vdd_dig(struct msm_rpm_notifier_data
*rpm_notifier_cb)
{
struct msm_lpm_resource *rs = &msm_lpm_vdd_dig;
msm_lpm_notify_common(rpm_notifier_cb, rs);
}
/*VDD Mem*/
static bool msm_lpm_beyond_limits_vdd_mem(struct msm_rpmrs_limits *limits)
{
bool ret = true;
struct msm_lpm_resource *rs = &msm_lpm_vdd_mem;
if (rs->valid) {
uint32_t vdd_buf = rs->rs_data.value;
uint32_t vdd_mem = rs->enable_low_power ? rs->enable_low_power :
rs->rs_data.default_value;
if (vdd_buf > vdd_mem)
vdd_mem = vdd_buf;
ret = (vdd_mem > limits->vdd_mem_upper_bound);
if (msm_lpm_debug_mask & MSM_LPMRS_DEBUG_VDD_MEM)
pr_info("%s:buf %d vdd mem %d limits%d\n",
__func__, vdd_buf, vdd_mem,
limits->vdd_mem_upper_bound);
}
return ret;
}
static void msm_lpm_aggregate_vdd_mem(struct msm_rpmrs_limits *limits)
{
struct msm_lpm_resource *rs = &msm_lpm_vdd_mem;
if (rs->valid) {
uint32_t vdd_buf = rs->rs_data.value;
if (limits->vdd_mem_lower_bound > vdd_buf)
rs->sleep_value = limits->vdd_mem_lower_bound;
else
rs->sleep_value = vdd_buf;
}
}
static void msm_lpm_flush_vdd_mem(int notify_rpm)
{
if (notify_rpm) {
struct msm_lpm_resource *rs = &msm_lpm_vdd_mem;
msm_lpm_send_sleep_data(rs->rs_data.handle,
rs->rs_data.key,
(uint8_t *)&rs->sleep_value);
}
}
static void msm_lpm_notify_vdd_mem(struct msm_rpm_notifier_data
*rpm_notifier_cb)
{
struct msm_lpm_resource *rs = &msm_lpm_vdd_mem;
msm_lpm_notify_common(rpm_notifier_cb, rs);
}
/*PXO*/
static bool msm_lpm_beyond_limits_pxo(struct msm_rpmrs_limits *limits)
{
bool ret = true;
struct msm_lpm_resource *rs = &msm_lpm_pxo;
if (rs->valid) {
uint32_t pxo_buf = rs->rs_data.value;
uint32_t pxo = rs->enable_low_power ? MSM_LPM_PXO_OFF :
rs->rs_data.default_value;
if (pxo_buf > pxo)
pxo = pxo_buf;
ret = (pxo > limits->pxo);
if (msm_lpm_debug_mask & MSM_LPMRS_DEBUG_PXO)
pr_info("%s:pxo buf %d pxo %d limits pxo %d\n",
__func__, pxo_buf, pxo, limits->pxo);
}
return ret;
}
static void msm_lpm_aggregate_pxo(struct msm_rpmrs_limits *limits)
{
struct msm_lpm_resource *rs = &msm_lpm_pxo;
if (rs->valid) {
uint32_t pxo_buf = rs->rs_data.value;
if (limits->pxo > pxo_buf)
rs->sleep_value = limits->pxo;
else
rs->sleep_value = pxo_buf;
if (msm_lpm_debug_mask & MSM_LPMRS_DEBUG_PXO)
pr_info("%s: pxo buf %d sleep value %d\n",
__func__, pxo_buf, rs->sleep_value);
}
}
static void msm_lpm_flush_pxo(int notify_rpm)
{
if (notify_rpm) {
struct msm_lpm_resource *rs = &msm_lpm_pxo;
msm_lpm_send_sleep_data(rs->rs_data.handle,
rs->rs_data.key,
(uint8_t *)&rs->sleep_value);
}
}
static void msm_lpm_notify_pxo(struct msm_rpm_notifier_data
*rpm_notifier_cb)
{
struct msm_lpm_resource *rs = &msm_lpm_pxo;
msm_lpm_notify_common(rpm_notifier_cb, rs);
}
static inline bool msm_lpm_use_mpm(struct msm_rpmrs_limits *limits)
{
return (limits->pxo == MSM_LPM_PXO_OFF);
}
/* LPM levels interface */
bool msm_lpm_level_beyond_limit(struct msm_rpmrs_limits *limits)
{
int i;
struct msm_lpm_resource *rs;
bool beyond_limit = false;
for (i = 0; i < ARRAY_SIZE(msm_lpm_resources); i++) {
rs = msm_lpm_resources[i];
if (rs->beyond_limits && rs->beyond_limits(limits)) {
beyond_limit = true;
if (msm_lpm_debug_mask & MSM_LPMRS_DEBUG_LVLS)
pr_info("%s: %s beyond limit", __func__,
rs->name);
break;
}
}
return beyond_limit;
}
int msm_lpmrs_enter_sleep(uint32_t sclk_count, struct msm_rpmrs_limits *limits,
bool from_idle, bool notify_rpm)
{
int ret = 0;
int i;
struct msm_lpm_resource *rs = NULL;
for (i = 0; i < ARRAY_SIZE(msm_lpm_resources); i++) {
rs = msm_lpm_resources[i];
if (rs->aggregate)
rs->aggregate(limits);
}
msm_lpm_get_rpm_notif = false;
for (i = 0; i < ARRAY_SIZE(msm_lpm_resources); i++) {
rs = msm_lpm_resources[i];
if (rs->flush)
rs->flush(notify_rpm);
}
msm_lpm_get_rpm_notif = true;
if (msm_lpm_use_mpm(limits))
msm_mpm_enter_sleep(sclk_count, from_idle);
return ret;
}
void msm_lpmrs_exit_sleep(struct msm_rpmrs_limits *limits,
bool from_idle, bool notify_rpm, bool collapsed)
{
/* MPM exit sleep
if (msm_lpm_use_mpm(limits))
msm_mpm_exit_sleep(from_idle);*/
msm_spm_l2_set_low_power_mode(MSM_SPM_MODE_DISABLED, notify_rpm);
}
static int msm_lpm_cpu_callback(struct notifier_block *cpu_nb,
unsigned long action, void *hcpu)
{
struct msm_lpm_resource *rs = &msm_lpm_l2;
switch (action) {
case CPU_ONLINE_FROZEN:
case CPU_ONLINE:
if (num_online_cpus() > 1)
rs->rs_data.value = MSM_LPM_L2_CACHE_ACTIVE;
break;
case CPU_DEAD_FROZEN:
case CPU_DEAD:
if (num_online_cpus() == 1)
rs->rs_data.value = MSM_LPM_L2_CACHE_HSFS_OPEN;
break;
}
return NOTIFY_OK;
}
/* RPM CTL */
static int __devinit msm_lpm_init_rpm_ctl(void)
{
struct msm_lpm_resource *rs = &msm_lpm_rpm_ctl;
rs->rs_data.handle = msm_rpm_create_request(
MSM_RPM_CTX_ACTIVE_SET,
rs->rs_data.type,
rs->rs_data.id, 1);
if (!rs->rs_data.handle)
return -EIO;
rs->valid = true;
return 0;
}
static int __devinit msm_lpm_resource_sysfs_add(void)
{
struct kobject *module_kobj = NULL;
struct kobject *low_power_kobj = NULL;
struct kobject *mode_kobj = NULL;
int rc = 0;
module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
if (!module_kobj) {
pr_err("%s: cannot find kobject for module %s\n",
__func__, KBUILD_MODNAME);
rc = -ENOENT;
goto resource_sysfs_add_exit;
}
low_power_kobj = kobject_create_and_add(
"enable_low_power", module_kobj);
if (!low_power_kobj) {
pr_err("%s: cannot create kobject\n", __func__);
rc = -ENOMEM;
goto resource_sysfs_add_exit;
}
mode_kobj = kobject_create_and_add(
"mode", module_kobj);
if (!mode_kobj) {
pr_err("%s: cannot create kobject\n", __func__);
rc = -ENOMEM;
goto resource_sysfs_add_exit;
}
rc = sysfs_create_group(low_power_kobj, &msm_lpm_attribute_group);
if (rc) {
pr_err("%s: cannot create kobject attribute group\n", __func__);
goto resource_sysfs_add_exit;
}
rc = sysfs_create_group(mode_kobj, &msm_lpm_rpm_ctl_attr_group);
if (rc) {
pr_err("%s: cannot create kobject attribute group\n", __func__);
goto resource_sysfs_add_exit;
}
resource_sysfs_add_exit:
if (rc) {
if (low_power_kobj)
sysfs_remove_group(low_power_kobj,
&msm_lpm_attribute_group);
kobject_del(low_power_kobj);
kobject_del(mode_kobj);
}
return rc;
}
late_initcall(msm_lpm_resource_sysfs_add);
static int __devinit msm_lpmrs_probe(struct platform_device *pdev)
{
struct device_node *node = NULL;
char *key = NULL;
int ret = 0;
for_each_child_of_node(pdev->dev.of_node, node) {
struct msm_lpm_resource *rs = NULL;
const char *val;
int i;
key = "qcom,name";
ret = of_property_read_string(node, key, &val);
if (ret) {
pr_err("Cannot read string\n");
goto fail;
}
for (i = 0; i < ARRAY_SIZE(msm_lpm_resources); i++) {
char *lpmrs_name = msm_lpm_resources[i]->name;
if (!msm_lpm_resources[i]->valid &&
!strncmp(val, lpmrs_name, strnlen(lpmrs_name,
MAX_RS_NAME))) {
rs = msm_lpm_resources[i];
break;
}
}
if (!rs) {
pr_err("LPM resource not found\n");
continue;
}
key = "qcom,type";
ret = of_property_read_u32(node, key, &rs->rs_data.type);
if (ret) {
pr_err("Failed to read type\n");
goto fail;
}
key = "qcom,id";
ret = of_property_read_u32(node, key, &rs->rs_data.id);
if (ret) {
pr_err("Failed to read id\n");
goto fail;
}
key = "qcom,key";
ret = of_property_read_u32(node, key, &rs->rs_data.key);
if (ret) {
pr_err("Failed to read key\n");
goto fail;
}
rs->rs_data.handle = msm_lpm_create_rpm_request(
rs->rs_data.type, rs->rs_data.id);
if (!rs->rs_data.handle) {
pr_err("%s: Failed to allocate handle for %s\n",
__func__, rs->name);
ret = -1;
goto fail;
}
rs->valid = true;
}
msm_rpm_register_notifier(&msm_lpm_rpm_nblk);
msm_lpm_init_rpm_ctl();
register_hotcpu_notifier(&msm_lpm_cpu_nblk);
/* For UP mode, set the default to HSFS OPEN*/
if (num_possible_cpus() == 1) {
msm_lpm_l2.rs_data.default_value = MSM_LPM_L2_CACHE_HSFS_OPEN;
msm_lpm_l2.rs_data.value = MSM_LPM_L2_CACHE_HSFS_OPEN;
}
msm_pm_set_l2_flush_flag(0);
return 0;
fail:
return ret;
}
static struct of_device_id msm_lpmrs_match_table[] = {
{.compatible = "qcom,lpm-resources"},
{},
};
static struct platform_driver msm_lpmrs_driver = {
.probe = msm_lpmrs_probe,
.driver = {
.name = "lpm-resources",
.owner = THIS_MODULE,
.of_match_table = msm_lpmrs_match_table,
},
};
int __init msm_lpmrs_module_init(void)
{
return platform_driver_register(&msm_lpmrs_driver);
}
|