blob: 30b73db753956c04ff80f484e02ebc93ce9ae841 [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 Iyer15d6df32016-08-18 12:10:27 -060017
18#include <soc/qcom/rpmh.h>
Lina Iyer4524c0e2017-08-15 11:32:31 -060019#include <soc/qcom/system_pm.h>
Lina Iyer15d6df32016-08-18 12:10:27 -060020
Lina Iyera2243002017-12-21 01:30:38 +000021#include <clocksource/arm_arch_timer.h>
Maulik Shah24c5f032018-02-01 14:39:50 +053022#include "rpmh_master_stat.h"
Lina Iyera2243002017-12-21 01:30:38 +000023
Lina Iyer15d6df32016-08-18 12:10:27 -060024#define PDC_TIME_VALID_SHIFT 31
25#define PDC_TIME_UPPER_MASK 0xFFFFFF
26
27static struct rpmh_client *rpmh_client;
28
Lina Iyera2243002017-12-21 01:30:38 +000029static int setup_wakeup(uint32_t lo, uint32_t hi)
Lina Iyer15d6df32016-08-18 12:10:27 -060030{
Lina Iyerd1a6e682017-06-20 10:05:09 -060031 struct tcs_cmd cmd[2] = { { 0 } };
Lina Iyer15d6df32016-08-18 12:10:27 -060032
Lina Iyera2243002017-12-21 01:30:38 +000033 cmd[0].data = hi & PDC_TIME_UPPER_MASK;
Lina Iyerd93fbce2017-03-01 09:07:20 -070034 cmd[0].data |= 1 << PDC_TIME_VALID_SHIFT;
Lina Iyera2243002017-12-21 01:30:38 +000035 cmd[1].data = lo;
Lina Iyer15d6df32016-08-18 12:10:27 -060036
37 return rpmh_write_control(rpmh_client, cmd, ARRAY_SIZE(cmd));
38}
39
Lina Iyera2243002017-12-21 01:30:38 +000040int system_sleep_update_wakeup(void)
41{
42 uint32_t lo = ~0U, hi = ~0U;
43
44 /* Read the hardware to get the most accurate value */
45 arch_timer_mem_get_cval(&lo, &hi);
46
47 return setup_wakeup(lo, hi);
48}
49EXPORT_SYMBOL(system_sleep_update_wakeup);
50
Lina Iyer15d6df32016-08-18 12:10:27 -060051/**
Lina Iyer4524c0e2017-08-15 11:32:31 -060052 * system_sleep_allowed() - Returns if its okay to enter system low power modes
53 */
54bool system_sleep_allowed(void)
55{
56 return (rpmh_ctrlr_idle(rpmh_client) == 0);
57}
58EXPORT_SYMBOL(system_sleep_allowed);
59
60/**
Lina Iyer15d6df32016-08-18 12:10:27 -060061 * system_sleep_enter() - Activties done when entering system low power modes
62 *
Lina Iyera2243002017-12-21 01:30:38 +000063 * Returns 0 for success or error values from writing the sleep/wake values to
64 * the hardware block.
Lina Iyer15d6df32016-08-18 12:10:27 -060065 */
Lina Iyera2243002017-12-21 01:30:38 +000066int system_sleep_enter(void)
Lina Iyer15d6df32016-08-18 12:10:27 -060067{
Lina Iyer15d6df32016-08-18 12:10:27 -060068 if (IS_ERR_OR_NULL(rpmh_client))
69 return -EFAULT;
70
Lina Iyera2243002017-12-21 01:30:38 +000071 return rpmh_flush(rpmh_client);
Lina Iyer15d6df32016-08-18 12:10:27 -060072}
73EXPORT_SYMBOL(system_sleep_enter);
74
75/**
76 * system_sleep_exit() - Activities done when exiting system low power modes
77 */
78void system_sleep_exit(void)
79{
Maulik Shah24c5f032018-02-01 14:39:50 +053080 msm_rpmh_master_stats_update();
Lina Iyer15d6df32016-08-18 12:10:27 -060081}
82EXPORT_SYMBOL(system_sleep_exit);
83
84static int sys_pm_probe(struct platform_device *pdev)
85{
86 rpmh_client = rpmh_get_byindex(pdev, 0);
87 if (IS_ERR_OR_NULL(rpmh_client))
88 return PTR_ERR(rpmh_client);
89
90 return 0;
91}
92
93static const struct of_device_id sys_pm_drv_match[] = {
94 { .compatible = "qcom,system-pm", },
95 { }
96};
97
98static struct platform_driver sys_pm_driver = {
99 .probe = sys_pm_probe,
100 .driver = {
101 .name = KBUILD_MODNAME,
102 .of_match_table = sys_pm_drv_match,
103 },
104};
105builtin_platform_driver(sys_pm_driver);