Maulik Shah | 24c5f03 | 2018-02-01 14:39:50 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 2 | * |
| 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 Sathyakumar | df8e8c3 | 2017-10-31 13:45:13 -0600 | [diff] [blame] | 16 | #include <asm/arch_timer.h> |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 17 | #include <clocksource/arm_arch_timer.h> |
Maulik Shah | 24c5f03 | 2018-02-01 14:39:50 +0530 | [diff] [blame] | 18 | #include "rpmh_master_stat.h" |
Raju P.L.S.S.S.N | 99d241e | 2017-12-07 00:09:01 +0530 | [diff] [blame^] | 19 | #include <soc/qcom/lpm_levels.h> |
| 20 | #include <soc/qcom/rpmh.h> |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 21 | |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 22 | #define PDC_TIME_VALID_SHIFT 31 |
| 23 | #define PDC_TIME_UPPER_MASK 0xFFFFFF |
| 24 | |
| 25 | static struct rpmh_client *rpmh_client; |
| 26 | |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 27 | static int setup_wakeup(uint32_t lo, uint32_t hi) |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 28 | { |
Lina Iyer | d1a6e68 | 2017-06-20 10:05:09 -0600 | [diff] [blame] | 29 | struct tcs_cmd cmd[2] = { { 0 } }; |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 30 | |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 31 | cmd[0].data = hi & PDC_TIME_UPPER_MASK; |
Lina Iyer | d93fbce | 2017-03-01 09:07:20 -0700 | [diff] [blame] | 32 | cmd[0].data |= 1 << PDC_TIME_VALID_SHIFT; |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 33 | cmd[1].data = lo; |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 34 | |
| 35 | return rpmh_write_control(rpmh_client, cmd, ARRAY_SIZE(cmd)); |
| 36 | } |
| 37 | |
Raju P.L.S.S.S.N | 99d241e | 2017-12-07 00:09:01 +0530 | [diff] [blame^] | 38 | static int system_sleep_update_wakeup(bool from_idle) |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 39 | { |
| 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 Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 47 | |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 48 | /** |
Lina Iyer | 4524c0e | 2017-08-15 11:32:31 -0600 | [diff] [blame] | 49 | * system_sleep_allowed() - Returns if its okay to enter system low power modes |
| 50 | */ |
Raju P.L.S.S.S.N | 99d241e | 2017-12-07 00:09:01 +0530 | [diff] [blame^] | 51 | static bool system_sleep_allowed(void) |
Lina Iyer | 4524c0e | 2017-08-15 11:32:31 -0600 | [diff] [blame] | 52 | { |
| 53 | return (rpmh_ctrlr_idle(rpmh_client) == 0); |
| 54 | } |
Lina Iyer | 4524c0e | 2017-08-15 11:32:31 -0600 | [diff] [blame] | 55 | |
| 56 | /** |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 57 | * system_sleep_enter() - Activties done when entering system low power modes |
| 58 | * |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 59 | * Returns 0 for success or error values from writing the sleep/wake values to |
| 60 | * the hardware block. |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 61 | */ |
Raju P.L.S.S.S.N | 99d241e | 2017-12-07 00:09:01 +0530 | [diff] [blame^] | 62 | static int system_sleep_enter(struct cpumask *mask) |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 63 | { |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 64 | return rpmh_flush(rpmh_client); |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 65 | } |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * system_sleep_exit() - Activities done when exiting system low power modes |
| 69 | */ |
Raju P.L.S.S.S.N | 99d241e | 2017-12-07 00:09:01 +0530 | [diff] [blame^] | 70 | static void system_sleep_exit(void) |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 71 | { |
Maulik Shah | 24c5f03 | 2018-02-01 14:39:50 +0530 | [diff] [blame] | 72 | msm_rpmh_master_stats_update(); |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 73 | } |
Raju P.L.S.S.S.N | 99d241e | 2017-12-07 00:09:01 +0530 | [diff] [blame^] | 74 | |
| 75 | static 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 Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 81 | |
| 82 | static 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.N | 99d241e | 2017-12-07 00:09:01 +0530 | [diff] [blame^] | 88 | return register_system_pm_ops(&pm_ops); |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static const struct of_device_id sys_pm_drv_match[] = { |
| 92 | { .compatible = "qcom,system-pm", }, |
| 93 | { } |
| 94 | }; |
| 95 | |
| 96 | static 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 | }; |
| 103 | builtin_platform_driver(sys_pm_driver); |