blob: 9c6edbffcf083b6bc927b9766d5cc717b518c4ce [file] [log] [blame]
Maulik Shah24c5f032018-02-01 14:39:50 +05301/* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
Lina Iyer15d6df32016-08-18 12:10:27 -06002 *
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/kernel.h>
15#include <linux/platform_device.h>
Archana Sathyakumardf8e8c32017-10-31 13:45:13 -060016#include <asm/arch_timer.h>
Lina Iyera2243002017-12-21 01:30:38 +000017#include <clocksource/arm_arch_timer.h>
Maulik Shah24c5f032018-02-01 14:39:50 +053018#include "rpmh_master_stat.h"
Raju P.L.S.S.S.N99d241e2017-12-07 00:09:01 +053019#include <soc/qcom/lpm_levels.h>
20#include <soc/qcom/rpmh.h>
Lina Iyera2243002017-12-21 01:30:38 +000021
Lina Iyer15d6df32016-08-18 12:10:27 -060022#define PDC_TIME_VALID_SHIFT 31
23#define PDC_TIME_UPPER_MASK 0xFFFFFF
24
25static struct rpmh_client *rpmh_client;
26
Lina Iyera2243002017-12-21 01:30:38 +000027static int setup_wakeup(uint32_t lo, uint32_t hi)
Lina Iyer15d6df32016-08-18 12:10:27 -060028{
Lina Iyerd1a6e682017-06-20 10:05:09 -060029 struct tcs_cmd cmd[2] = { { 0 } };
Lina Iyer15d6df32016-08-18 12:10:27 -060030
Lina Iyera2243002017-12-21 01:30:38 +000031 cmd[0].data = hi & PDC_TIME_UPPER_MASK;
Lina Iyerd93fbce2017-03-01 09:07:20 -070032 cmd[0].data |= 1 << PDC_TIME_VALID_SHIFT;
Lina Iyera2243002017-12-21 01:30:38 +000033 cmd[1].data = lo;
Lina Iyer15d6df32016-08-18 12:10:27 -060034
35 return rpmh_write_control(rpmh_client, cmd, ARRAY_SIZE(cmd));
36}
37
Raju P.L.S.S.S.N99d241e2017-12-07 00:09:01 +053038static int system_sleep_update_wakeup(bool from_idle)
Lina Iyera2243002017-12-21 01:30:38 +000039{
40 uint32_t lo = ~0U, hi = ~0U;
41
42 /* Read the hardware to get the most accurate value */
43 arch_timer_mem_get_cval(&lo, &hi);
44
45 return setup_wakeup(lo, hi);
46}
Lina Iyera2243002017-12-21 01:30:38 +000047
Lina Iyer15d6df32016-08-18 12:10:27 -060048/**
Lina Iyer4524c0e2017-08-15 11:32:31 -060049 * system_sleep_allowed() - Returns if its okay to enter system low power modes
50 */
Raju P.L.S.S.S.N99d241e2017-12-07 00:09:01 +053051static bool system_sleep_allowed(void)
Lina Iyer4524c0e2017-08-15 11:32:31 -060052{
53 return (rpmh_ctrlr_idle(rpmh_client) == 0);
54}
Lina Iyer4524c0e2017-08-15 11:32:31 -060055
56/**
Lina Iyer15d6df32016-08-18 12:10:27 -060057 * system_sleep_enter() - Activties done when entering system low power modes
58 *
Lina Iyera2243002017-12-21 01:30:38 +000059 * Returns 0 for success or error values from writing the sleep/wake values to
60 * the hardware block.
Lina Iyer15d6df32016-08-18 12:10:27 -060061 */
Raju P.L.S.S.S.N99d241e2017-12-07 00:09:01 +053062static int system_sleep_enter(struct cpumask *mask)
Lina Iyer15d6df32016-08-18 12:10:27 -060063{
Lina Iyera2243002017-12-21 01:30:38 +000064 return rpmh_flush(rpmh_client);
Lina Iyer15d6df32016-08-18 12:10:27 -060065}
Lina Iyer15d6df32016-08-18 12:10:27 -060066
67/**
68 * system_sleep_exit() - Activities done when exiting system low power modes
69 */
Raju P.L.S.S.S.N99d241e2017-12-07 00:09:01 +053070static void system_sleep_exit(void)
Lina Iyer15d6df32016-08-18 12:10:27 -060071{
Maulik Shah24c5f032018-02-01 14:39:50 +053072 msm_rpmh_master_stats_update();
Lina Iyer15d6df32016-08-18 12:10:27 -060073}
Raju P.L.S.S.S.N99d241e2017-12-07 00:09:01 +053074
75static struct system_pm_ops pm_ops = {
76 .enter = system_sleep_enter,
77 .exit = system_sleep_exit,
78 .update_wakeup = system_sleep_update_wakeup,
79 .sleep_allowed = system_sleep_allowed,
80};
Lina Iyer15d6df32016-08-18 12:10:27 -060081
82static int sys_pm_probe(struct platform_device *pdev)
83{
84 rpmh_client = rpmh_get_byindex(pdev, 0);
85 if (IS_ERR_OR_NULL(rpmh_client))
86 return PTR_ERR(rpmh_client);
87
Raju P.L.S.S.S.N99d241e2017-12-07 00:09:01 +053088 return register_system_pm_ops(&pm_ops);
Lina Iyer15d6df32016-08-18 12:10:27 -060089}
90
91static const struct of_device_id sys_pm_drv_match[] = {
92 { .compatible = "qcom,system-pm", },
93 { }
94};
95
96static struct platform_driver sys_pm_driver = {
97 .probe = sys_pm_probe,
98 .driver = {
99 .name = KBUILD_MODNAME,
100 .of_match_table = sys_pm_drv_match,
101 },
102};
103builtin_platform_driver(sys_pm_driver);