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 | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 17 | |
| 18 | #include <soc/qcom/rpmh.h> |
Lina Iyer | 4524c0e | 2017-08-15 11:32:31 -0600 | [diff] [blame] | 19 | #include <soc/qcom/system_pm.h> |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 20 | |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 21 | #include <clocksource/arm_arch_timer.h> |
Maulik Shah | 24c5f03 | 2018-02-01 14:39:50 +0530 | [diff] [blame^] | 22 | #include "rpmh_master_stat.h" |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 23 | |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 24 | #define PDC_TIME_VALID_SHIFT 31 |
| 25 | #define PDC_TIME_UPPER_MASK 0xFFFFFF |
| 26 | |
| 27 | static struct rpmh_client *rpmh_client; |
| 28 | |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 29 | static int setup_wakeup(uint32_t lo, uint32_t hi) |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 30 | { |
Lina Iyer | d1a6e68 | 2017-06-20 10:05:09 -0600 | [diff] [blame] | 31 | struct tcs_cmd cmd[2] = { { 0 } }; |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 32 | |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 33 | cmd[0].data = hi & PDC_TIME_UPPER_MASK; |
Lina Iyer | d93fbce | 2017-03-01 09:07:20 -0700 | [diff] [blame] | 34 | cmd[0].data |= 1 << PDC_TIME_VALID_SHIFT; |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 35 | cmd[1].data = lo; |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 36 | |
| 37 | return rpmh_write_control(rpmh_client, cmd, ARRAY_SIZE(cmd)); |
| 38 | } |
| 39 | |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 40 | int 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 | } |
| 49 | EXPORT_SYMBOL(system_sleep_update_wakeup); |
| 50 | |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 51 | /** |
Lina Iyer | 4524c0e | 2017-08-15 11:32:31 -0600 | [diff] [blame] | 52 | * system_sleep_allowed() - Returns if its okay to enter system low power modes |
| 53 | */ |
| 54 | bool system_sleep_allowed(void) |
| 55 | { |
| 56 | return (rpmh_ctrlr_idle(rpmh_client) == 0); |
| 57 | } |
| 58 | EXPORT_SYMBOL(system_sleep_allowed); |
| 59 | |
| 60 | /** |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 61 | * system_sleep_enter() - Activties done when entering system low power modes |
| 62 | * |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 63 | * Returns 0 for success or error values from writing the sleep/wake values to |
| 64 | * the hardware block. |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 65 | */ |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 66 | int system_sleep_enter(void) |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 67 | { |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 68 | if (IS_ERR_OR_NULL(rpmh_client)) |
| 69 | return -EFAULT; |
| 70 | |
Lina Iyer | a224300 | 2017-12-21 01:30:38 +0000 | [diff] [blame] | 71 | return rpmh_flush(rpmh_client); |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 72 | } |
| 73 | EXPORT_SYMBOL(system_sleep_enter); |
| 74 | |
| 75 | /** |
| 76 | * system_sleep_exit() - Activities done when exiting system low power modes |
| 77 | */ |
| 78 | void system_sleep_exit(void) |
| 79 | { |
Maulik Shah | 24c5f03 | 2018-02-01 14:39:50 +0530 | [diff] [blame^] | 80 | msm_rpmh_master_stats_update(); |
Lina Iyer | 15d6df3 | 2016-08-18 12:10:27 -0600 | [diff] [blame] | 81 | } |
| 82 | EXPORT_SYMBOL(system_sleep_exit); |
| 83 | |
| 84 | static 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 | |
| 93 | static const struct of_device_id sys_pm_drv_match[] = { |
| 94 | { .compatible = "qcom,system-pm", }, |
| 95 | { } |
| 96 | }; |
| 97 | |
| 98 | static 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 | }; |
| 105 | builtin_platform_driver(sys_pm_driver); |