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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2026 Linaro Ltd.
*/
#define LOG_CATEGORY UCLASS_PINCTRL
#include <dm.h>
#include <dm/device_compat.h>
#include <dm/devres.h>
#include <dm/pinctrl.h>
#include <linux/bitfield.h>
#include <linux/compat.h>
#include <scmi_agent.h>
#include <scmi_agent-uclass.h>
#include <scmi_protocols.h>
static int map_config_param_to_scmi(u32 config_param)
{
switch (config_param) {
case PIN_CONFIG_BIAS_BUS_HOLD:
return SCMI_PIN_BIAS_BUS_HOLD;
case PIN_CONFIG_BIAS_DISABLE:
return SCMI_PIN_BIAS_DISABLE;
case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
return SCMI_PIN_BIAS_HIGH_IMPEDANCE;
case PIN_CONFIG_BIAS_PULL_DOWN:
return SCMI_PIN_BIAS_PULL_DOWN;
case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
return SCMI_PIN_BIAS_PULL_DEFAULT;
case PIN_CONFIG_BIAS_PULL_UP:
return SCMI_PIN_BIAS_PULL_UP;
case PIN_CONFIG_DRIVE_OPEN_DRAIN:
return SCMI_PIN_DRIVE_OPEN_DRAIN;
case PIN_CONFIG_DRIVE_OPEN_SOURCE:
return SCMI_PIN_DRIVE_OPEN_SOURCE;
case PIN_CONFIG_DRIVE_PUSH_PULL:
return SCMI_PIN_DRIVE_PUSH_PULL;
case PIN_CONFIG_DRIVE_STRENGTH:
return SCMI_PIN_DRIVE_STRENGTH;
case PIN_CONFIG_INPUT_DEBOUNCE:
return SCMI_PIN_INPUT_DEBOUNCE;
case PIN_CONFIG_INPUT_ENABLE:
return SCMI_PIN_INPUT_MODE;
case PIN_CONFIG_INPUT_SCHMITT:
return SCMI_PIN_INPUT_SCHMITT;
case PIN_CONFIG_LOW_POWER_MODE:
return SCMI_PIN_LOW_POWER_MODE;
case PIN_CONFIG_OUTPUT_ENABLE:
return SCMI_PIN_OUTPUT_MODE;
case PIN_CONFIG_OUTPUT:
return SCMI_PIN_OUTPUT_VALUE;
case PIN_CONFIG_POWER_SOURCE:
return SCMI_PIN_POWER_SOURCE;
case PIN_CONFIG_SLEW_RATE:
return SCMI_PIN_SLEW_RATE;
}
return -EINVAL;
}
int scmi_pinctrl_protocol_attrs(struct udevice *dev, int *num_pins,
int *num_groups, int *num_functions)
{
struct scmi_pinctrl_protocol_attrs_out out;
struct scmi_msg msg = {
.protocol_id = SCMI_PROTOCOL_ID_PINCTRL,
.message_id = SCMI_PROTOCOL_ATTRIBUTES,
.out_msg = (u8 *)&out,
.out_msg_sz = sizeof(out),
};
int ret;
ret = devm_scmi_process_msg(dev, &msg);
if (ret)
return ret;
if (out.status)
return scmi_to_linux_errno(out.status);
if (num_groups)
*num_groups = FIELD_GET(GENMASK(31, 16), out.attr_low);
if (num_pins)
*num_pins = FIELD_GET(GENMASK(15, 0), out.attr_low);
if (num_functions)
*num_functions = FIELD_GET(GENMASK(15, 0), out.attr_high);
return 0;
}
int scmi_pinctrl_attrs(struct udevice *dev, enum select_type select_type,
unsigned int selector, bool *gpio, unsigned int *count,
char *name)
{
struct scmi_pinctrl_attrs_in in;
struct scmi_pinctrl_attrs_out out;
struct scmi_msg msg = {
.protocol_id = SCMI_PROTOCOL_ID_PINCTRL,
.message_id = SCMI_PINCTRL_ATTRIBUTES,
.in_msg = (u8 *)&in,
.in_msg_sz = sizeof(in),
.out_msg = (u8 *)&out,
.out_msg_sz = sizeof(out),
};
int ret;
in.select_type = select_type;
in.id = selector;
ret = devm_scmi_process_msg(dev, &msg);
if (ret)
return ret;
if (out.status)
return scmi_to_linux_errno(out.status);
if (gpio)
*gpio = FIELD_GET(BIT(17), out.attr);
if (count)
*count = FIELD_GET(GENMASK(15, 0), out.attr);
if (name)
strncpy(name, out.name, sizeof(out.name));
return 0;
}
int scmi_pinctrl_list_associations(struct udevice *dev,
enum select_type select_type,
unsigned int selector,
unsigned short *output,
unsigned short num_out)
{
struct scmi_pinctrl_list_associations_in in;
struct scmi_pinctrl_list_associations_out *out;
struct scmi_msg msg = {
.protocol_id = SCMI_PROTOCOL_ID_PINCTRL,
.message_id = SCMI_PINCTRL_LIST_ASSOCIATIONS,
.in_msg = (u8 *)&in,
.in_msg_sz = sizeof(in),
};
size_t out_sz = sizeof(*out) + num_out * sizeof(out->array[0]);
unsigned int count;
int ret = -EINVAL;
out = kzalloc(out_sz, GFP_KERNEL);
if (!out)
return -ENOMEM;
msg.out_msg = (u8 *)out;
msg.out_msg_sz = out_sz;
in.select_type = select_type;
in.id = selector;
in.index = 0;
while (num_out > 0) {
ret = devm_scmi_process_msg(dev, &msg);
if (ret)
goto free;
if (out->status) {
ret = scmi_to_linux_errno(out->status);
goto free;
}
count = FIELD_GET(GENMASK(11, 0), out->flags);
if (count > num_out)
return -EINVAL;
memcpy(&output[in.index], out->array, count * sizeof(u16));
num_out -= count;
in.index += count;
}
free:
kfree(out);
return ret;
}
#define SCMI_PINCTRL_CONFIG_SETTINGS_FUNCTION -2u
int scmi_pinctrl_settings_get_one(struct udevice *dev, enum select_type select_type,
unsigned int selector,
u32 config_type, u32 *value)
{
struct scmi_pinctrl_settings_get_in in;
struct scmi_pinctrl_settings_get_out *out;
struct scmi_msg msg = {
.protocol_id = SCMI_PROTOCOL_ID_PINCTRL,
.message_id = SCMI_PINCTRL_SETTINGS_GET,
.in_msg = (u8 *)&in,
.in_msg_sz = sizeof(in),
};
size_t out_sz = sizeof(*out) + (sizeof(u32) * 2);
u32 num_configs;
int ret;
if (config_type == SCMI_PINCTRL_CONFIG_SETTINGS_ALL) {
/* FIXME: implement */
return -EIO;
}
out = kzalloc(out_sz, GFP_KERNEL);
if (!out)
return -ENOMEM;
msg.out_msg = (u8 *)out;
msg.out_msg_sz = out_sz;
in.id = selector;
in.attr = 0;
if (config_type == SCMI_PINCTRL_CONFIG_SETTINGS_FUNCTION)
in.attr = FIELD_PREP(GENMASK(19, 18), 2);
in.attr |= FIELD_PREP(GENMASK(17, 16), select_type);
if (config_type != SCMI_PINCTRL_CONFIG_SETTINGS_FUNCTION)
in.attr |= FIELD_PREP(GENMASK(7, 0), config_type);
ret = devm_scmi_process_msg(dev, &msg);
if (ret)
goto free;
if (out->status) {
ret = scmi_to_linux_errno(out->status);
goto free;
}
num_configs = FIELD_GET(GENMASK(7, 0), out->num_configs);
if (out->num_configs == 0) {
*value = out->function_selected;
goto free;
}
if (num_configs != 1) {
ret = -EINVAL;
goto free;
}
*value = out->configs[1];
free:
kfree(out);
return ret;
}
static int scmi_pinctrl_settings_configure_helper(struct udevice *dev,
enum select_type select_type,
unsigned int selector,
u32 function_id,
u16 num_configs, u32 *configs)
{
struct scmi_pinctrl_settings_configure_in *in;
struct scmi_pinctrl_settings_configure_out out;
struct scmi_msg msg = {
.protocol_id = SCMI_PROTOCOL_ID_PINCTRL,
.message_id = SCMI_PINCTRL_SETTINGS_CONFIGURE,
.out_msg = (u8 *)&out,
.out_msg_sz = sizeof(out),
};
size_t in_sz = sizeof(*in) + (num_configs * sizeof(u32) * 2);
int ret;
in = kzalloc(in_sz, GFP_KERNEL);
if (!in)
return -ENOMEM;
msg.in_msg = (u8 *)in;
msg.in_msg_sz = in_sz;
in->id = selector;
in->function_id = function_id;
in->attr = 0;
in->attr |= FIELD_PREP(GENMASK(9, 2), num_configs);
in->attr |= FIELD_PREP(GENMASK(1, 0), select_type);
memcpy(in->configs, configs, num_configs * sizeof(u32) * 2);
ret = devm_scmi_process_msg(dev, &msg);
if (ret)
goto free;
if (out.status) {
ret = scmi_to_linux_errno(out.status);
goto free;
}
free:
kfree(in);
return ret;
}
int scmi_pinctrl_settings_configure(struct udevice *dev, enum select_type select_type,
unsigned int selector, u16 num_configs,
u32 *configs)
{
return scmi_pinctrl_settings_configure_helper(dev, select_type,
selector,
SCMI_PINCTRL_FUNCTION_NONE,
num_configs, configs);
}
int scmi_pinctrl_settings_configure_one(struct udevice *dev, enum select_type select_type,
unsigned int selector,
u32 param, u32 argument)
{
u32 config_value[2];
int scmi_config;
/* see stmfx_pinctrl_conf_set() */
scmi_config = map_config_param_to_scmi(param);
if (scmi_config < 0)
return scmi_config;
config_value[0] = scmi_config;
config_value[1] = argument;
return scmi_pinctrl_settings_configure(dev, select_type, selector, 1,
&config_value[0]);
}
int scmi_pinctrl_set_function(struct udevice *dev, enum select_type select_type,
unsigned int selector, u32 function_id)
{
return scmi_pinctrl_settings_configure_helper(dev, select_type, selector,
function_id, 0, NULL);
}
int scmi_pinctrl_request(struct udevice *dev, enum select_type select_type,
unsigned int selector)
{
struct scmi_pinctrl_request_in in;
struct scmi_pinctrl_request_out out;
struct scmi_msg msg = {
.protocol_id = SCMI_PROTOCOL_ID_PINCTRL,
.message_id = SCMI_PINCTRL_REQUEST,
.in_msg = (u8 *)&in,
.in_msg_sz = sizeof(in),
.out_msg = (u8 *)&out,
.out_msg_sz = sizeof(out),
};
int ret;
in.id = selector;
in.flags = FIELD_PREP(GENMASK(1, 0), select_type);
ret = devm_scmi_process_msg(dev, &msg);
if (ret)
return ret;
if (out.status)
return scmi_to_linux_errno(out.status);
return 0;
}
int scmi_pinctrl_release(struct udevice *dev, enum select_type select_type,
unsigned int selector)
{
struct scmi_pinctrl_release_in in;
struct scmi_pinctrl_release_out out;
struct scmi_msg msg = {
.protocol_id = SCMI_PROTOCOL_ID_PINCTRL,
.message_id = SCMI_PINCTRL_RELEASE,
.in_msg = (u8 *)&in,
.in_msg_sz = sizeof(in),
.out_msg = (u8 *)&out,
.out_msg_sz = sizeof(out),
};
int ret;
in.id = selector;
in.flags = FIELD_PREP(GENMASK(1, 0), select_type);
ret = devm_scmi_process_msg(dev, &msg);
if (ret)
return ret;
if (out.status)
return scmi_to_linux_errno(out.status);
return 0;
}
|