Linux-libre 4.15.7-gnu
[librecmc/linux-libre.git] / drivers / staging / ccree / ssi_pm.c
1 /*
2  * Copyright (C) 2012-2017 ARM Limited or its affiliates.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include "ssi_config.h"
18 #include <linux/kernel.h>
19 #include <linux/platform_device.h>
20 #include <linux/interrupt.h>
21 #include <crypto/ctr.h>
22 #include <linux/pm_runtime.h>
23 #include "ssi_driver.h"
24 #include "ssi_buffer_mgr.h"
25 #include "ssi_request_mgr.h"
26 #include "ssi_sram_mgr.h"
27 #include "ssi_sysfs.h"
28 #include "ssi_ivgen.h"
29 #include "ssi_hash.h"
30 #include "ssi_pm.h"
31
32 #if defined(CONFIG_PM_RUNTIME) || defined(CONFIG_PM_SLEEP)
33
34 #define POWER_DOWN_ENABLE 0x01
35 #define POWER_DOWN_DISABLE 0x00
36
37 int ssi_power_mgr_runtime_suspend(struct device *dev)
38 {
39         struct ssi_drvdata *drvdata =
40                 (struct ssi_drvdata *)dev_get_drvdata(dev);
41         int rc;
42
43         dev_dbg(dev, "set HOST_POWER_DOWN_EN\n");
44         cc_iowrite(drvdata, CC_REG(HOST_POWER_DOWN_EN), POWER_DOWN_ENABLE);
45         rc = ssi_request_mgr_runtime_suspend_queue(drvdata);
46         if (rc != 0) {
47                 dev_err(dev, "ssi_request_mgr_runtime_suspend_queue (%x)\n",
48                         rc);
49                 return rc;
50         }
51         fini_cc_regs(drvdata);
52         cc_clk_off(drvdata);
53         return 0;
54 }
55
56 int ssi_power_mgr_runtime_resume(struct device *dev)
57 {
58         int rc;
59         struct ssi_drvdata *drvdata =
60                 (struct ssi_drvdata *)dev_get_drvdata(dev);
61
62         dev_dbg(dev, "unset HOST_POWER_DOWN_EN\n");
63         cc_iowrite(drvdata, CC_REG(HOST_POWER_DOWN_EN), POWER_DOWN_DISABLE);
64
65         rc = cc_clk_on(drvdata);
66         if (rc) {
67                 dev_err(dev, "failed getting clock back on. We're toast.\n");
68                 return rc;
69         }
70
71         rc = init_cc_regs(drvdata, false);
72         if (rc != 0) {
73                 dev_err(dev, "init_cc_regs (%x)\n", rc);
74                 return rc;
75         }
76
77         rc = ssi_request_mgr_runtime_resume_queue(drvdata);
78         if (rc != 0) {
79                 dev_err(dev, "ssi_request_mgr_runtime_resume_queue (%x)\n", rc);
80                 return rc;
81         }
82
83         /* must be after the queue resuming as it uses the HW queue*/
84         ssi_hash_init_sram_digest_consts(drvdata);
85
86         ssi_ivgen_init_sram_pool(drvdata);
87         return 0;
88 }
89
90 int ssi_power_mgr_runtime_get(struct device *dev)
91 {
92         int rc = 0;
93
94         if (ssi_request_mgr_is_queue_runtime_suspend(
95                                 (struct ssi_drvdata *)dev_get_drvdata(dev))) {
96                 rc = pm_runtime_get_sync(dev);
97         } else {
98                 pm_runtime_get_noresume(dev);
99         }
100         return rc;
101 }
102
103 int ssi_power_mgr_runtime_put_suspend(struct device *dev)
104 {
105         int rc = 0;
106
107         if (!ssi_request_mgr_is_queue_runtime_suspend(
108                                 (struct ssi_drvdata *)dev_get_drvdata(dev))) {
109                 pm_runtime_mark_last_busy(dev);
110                 rc = pm_runtime_put_autosuspend(dev);
111         } else {
112                 /* Something wrong happens*/
113                 dev_err(dev, "request to suspend already suspended queue");
114                 rc = -EBUSY;
115         }
116         return rc;
117 }
118
119 #endif
120
121 int ssi_power_mgr_init(struct ssi_drvdata *drvdata)
122 {
123         int rc = 0;
124 #if defined(CONFIG_PM_RUNTIME) || defined(CONFIG_PM_SLEEP)
125         struct device *dev = drvdata_to_dev(drvdata);
126
127         /* must be before the enabling to avoid resdundent suspending */
128         pm_runtime_set_autosuspend_delay(dev, SSI_SUSPEND_TIMEOUT);
129         pm_runtime_use_autosuspend(dev);
130         /* activate the PM module */
131         rc = pm_runtime_set_active(dev);
132         if (rc != 0)
133                 return rc;
134         /* enable the PM module*/
135         pm_runtime_enable(dev);
136 #endif
137         return rc;
138 }
139
140 void ssi_power_mgr_fini(struct ssi_drvdata *drvdata)
141 {
142 #if defined(CONFIG_PM_RUNTIME) || defined(CONFIG_PM_SLEEP)
143         pm_runtime_disable(drvdata_to_dev(drvdata));
144 #endif
145 }