1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright 2020 Samsung Electronics Co., Ltd.
* Copyright 2020 Google LLC.
* Copyright 2026 Linaro Ltd.
*/
#include <linux/array_size.h>
#include <linux/bitfield.h>
#include <linux/bits.h>
#include <linux/firmware/samsung/exynos-acpm-protocol.h>
#include <linux/ktime.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/units.h>
#include "exynos-acpm.h"
#include "exynos-acpm-tmu.h"
/* IPC Request Types */
#define ACPM_TMU_INIT 0x01
#define ACPM_TMU_READ_TEMP 0x02
#define ACPM_TMU_SUSPEND 0x04
#define ACPM_TMU_RESUME 0x10
#define ACPM_TMU_THRESHOLD 0x11
#define ACPM_TMU_INTEN 0x12
#define ACPM_TMU_CONTROL 0x13
#define ACPM_TMU_IRQ_CLEAR 0x14
#define ACPM_TMU_TX_DATA_LEN 8
#define ACPM_TMU_RX_DATA_LEN 7
struct acpm_tmu_tx {
u16 ctx;
u16 fw_use;
u8 type;
u8 rsvd0;
u8 tzid;
u8 rsvd1;
u8 data[ACPM_TMU_TX_DATA_LEN];
} __packed;
struct acpm_tmu_rx {
u16 ctx;
u16 fw_use;
u8 type;
s8 ret;
u8 tzid;
s8 temp;
u8 rsvd;
u8 data[ACPM_TMU_RX_DATA_LEN];
} __packed;
union acpm_tmu_msg {
u32 data[4];
struct acpm_tmu_tx tx;
struct acpm_tmu_rx rx;
};
static int acpm_tmu_to_linux_err(s8 fw_err)
{
/*
* ACPM_TMU_INIT uses BIT(0) and BIT(1) of msg.rx.ret to flag APM
* capabilities. Treat zero and all positive values as success.
*/
if (fw_err >= 0)
return 0;
if (fw_err == -1)
return -EACCES;
return -EIO;
}
int acpm_tmu_init(struct acpm_handle *handle, unsigned int acpm_chan_id)
{
union acpm_tmu_msg msg = {0};
struct acpm_xfer xfer;
int ret;
msg.tx.type = ACPM_TMU_INIT;
acpm_set_xfer(&xfer, msg.data, ARRAY_SIZE(msg.data), acpm_chan_id,
true);
ret = acpm_do_xfer(handle, &xfer);
if (ret)
return ret;
return acpm_tmu_to_linux_err(msg.rx.ret);
}
int acpm_tmu_read_temp(struct acpm_handle *handle, unsigned int acpm_chan_id,
u8 tz, int *temp)
{
union acpm_tmu_msg msg = {0};
struct acpm_xfer xfer;
int ret;
msg.tx.type = ACPM_TMU_READ_TEMP;
msg.tx.tzid = tz;
acpm_set_xfer(&xfer, msg.data, ARRAY_SIZE(msg.data), acpm_chan_id,
true);
ret = acpm_do_xfer(handle, &xfer);
if (ret)
return ret;
ret = acpm_tmu_to_linux_err(msg.rx.ret);
if (ret)
return ret;
*temp = msg.rx.temp;
return 0;
}
int acpm_tmu_set_threshold(struct acpm_handle *handle,
unsigned int acpm_chan_id, u8 tz,
const u8 temperature[8], size_t tlen)
{
union acpm_tmu_msg msg = {0};
struct acpm_xfer xfer;
int ret;
if (tlen > ACPM_TMU_TX_DATA_LEN)
return -EINVAL;
msg.tx.type = ACPM_TMU_THRESHOLD;
msg.tx.tzid = tz;
memcpy(msg.tx.data, temperature, tlen);
acpm_set_xfer(&xfer, msg.data, ARRAY_SIZE(msg.data), acpm_chan_id,
true);
ret = acpm_do_xfer(handle, &xfer);
if (ret)
return ret;
return acpm_tmu_to_linux_err(msg.rx.ret);
}
int acpm_tmu_set_interrupt_enable(struct acpm_handle *handle,
unsigned int acpm_chan_id, u8 tz, u8 inten)
{
union acpm_tmu_msg msg = {0};
struct acpm_xfer xfer;
int ret;
msg.tx.type = ACPM_TMU_INTEN;
msg.tx.tzid = tz;
msg.tx.data[0] = inten;
acpm_set_xfer(&xfer, msg.data, ARRAY_SIZE(msg.data), acpm_chan_id,
true);
ret = acpm_do_xfer(handle, &xfer);
if (ret)
return ret;
return acpm_tmu_to_linux_err(msg.rx.ret);
}
int acpm_tmu_tz_control(struct acpm_handle *handle, unsigned int acpm_chan_id,
u8 tz, bool enable)
{
union acpm_tmu_msg msg = {0};
struct acpm_xfer xfer;
int ret;
msg.tx.type = ACPM_TMU_CONTROL;
msg.tx.tzid = tz;
msg.tx.data[0] = enable ? 1 : 0;
acpm_set_xfer(&xfer, msg.data, ARRAY_SIZE(msg.data), acpm_chan_id,
true);
ret = acpm_do_xfer(handle, &xfer);
if (ret)
return ret;
return acpm_tmu_to_linux_err(msg.rx.ret);
}
int acpm_tmu_clear_tz_irq(struct acpm_handle *handle, unsigned int acpm_chan_id,
u8 tz)
{
union acpm_tmu_msg msg = {0};
struct acpm_xfer xfer;
int ret;
msg.tx.type = ACPM_TMU_IRQ_CLEAR;
msg.tx.tzid = tz;
acpm_set_xfer(&xfer, msg.data, ARRAY_SIZE(msg.data), acpm_chan_id,
true);
ret = acpm_do_xfer(handle, &xfer);
if (ret)
return ret;
return acpm_tmu_to_linux_err(msg.rx.ret);
}
int acpm_tmu_suspend(struct acpm_handle *handle, unsigned int acpm_chan_id)
{
union acpm_tmu_msg msg = {0};
struct acpm_xfer xfer;
int ret;
msg.tx.type = ACPM_TMU_SUSPEND;
acpm_set_xfer(&xfer, msg.data, ARRAY_SIZE(msg.data), acpm_chan_id,
true);
ret = acpm_do_xfer(handle, &xfer);
if (ret)
return ret;
return acpm_tmu_to_linux_err(msg.rx.ret);
}
int acpm_tmu_resume(struct acpm_handle *handle, unsigned int acpm_chan_id)
{
union acpm_tmu_msg msg = {0};
struct acpm_xfer xfer;
int ret;
msg.tx.type = ACPM_TMU_RESUME;
acpm_set_xfer(&xfer, msg.data, ARRAY_SIZE(msg.data), acpm_chan_id,
true);
ret = acpm_do_xfer(handle, &xfer);
if (ret)
return ret;
return acpm_tmu_to_linux_err(msg.rx.ret);
}
|