1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Power Interface (SCMI) Protocol based pinctrl driver
5 * Copyright (C) 2024 EPAM
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
15 #include <linux/scmi_protocol.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
19 #include <linux/pinctrl/machine.h>
20 #include <linux/pinctrl/pinconf.h>
21 #include <linux/pinctrl/pinconf-generic.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
25 #include "pinctrl-utils.h"
29 #define DRV_NAME "scmi-pinctrl"
31 /* Define num configs, if not large than 4 use stack, else use kcalloc() */
32 #define SCMI_NUM_CONFIGS 4
34 static const struct scmi_pinctrl_proto_ops
*pinctrl_ops
;
38 struct scmi_protocol_handle
*ph
;
39 struct pinctrl_dev
*pctldev
;
40 struct pinctrl_desc pctl_desc
;
41 struct pinfunction
*functions
;
42 unsigned int nr_functions
;
43 struct pinctrl_pin_desc
*pins
;
47 static int pinctrl_scmi_get_groups_count(struct pinctrl_dev
*pctldev
)
49 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
51 return pinctrl_ops
->count_get(pmx
->ph
, GROUP_TYPE
);
54 static const char *pinctrl_scmi_get_group_name(struct pinctrl_dev
*pctldev
,
55 unsigned int selector
)
59 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
61 ret
= pinctrl_ops
->name_get(pmx
->ph
, selector
, GROUP_TYPE
, &name
);
63 dev_err(pmx
->dev
, "get name failed with err %d", ret
);
70 static int pinctrl_scmi_get_group_pins(struct pinctrl_dev
*pctldev
,
71 unsigned int selector
,
72 const unsigned int **pins
,
73 unsigned int *num_pins
)
75 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
77 return pinctrl_ops
->group_pins_get(pmx
->ph
, selector
, pins
, num_pins
);
80 static const struct pinctrl_ops pinctrl_scmi_pinctrl_ops
= {
81 .get_groups_count
= pinctrl_scmi_get_groups_count
,
82 .get_group_name
= pinctrl_scmi_get_group_name
,
83 .get_group_pins
= pinctrl_scmi_get_group_pins
,
85 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
86 .dt_free_map
= pinconf_generic_dt_free_map
,
90 static int pinctrl_scmi_get_functions_count(struct pinctrl_dev
*pctldev
)
92 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
94 return pinctrl_ops
->count_get(pmx
->ph
, FUNCTION_TYPE
);
97 static const char *pinctrl_scmi_get_function_name(struct pinctrl_dev
*pctldev
,
98 unsigned int selector
)
102 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
104 ret
= pinctrl_ops
->name_get(pmx
->ph
, selector
, FUNCTION_TYPE
, &name
);
106 dev_err(pmx
->dev
, "get name failed with err %d", ret
);
113 static int pinctrl_scmi_get_function_groups(struct pinctrl_dev
*pctldev
,
114 unsigned int selector
,
115 const char * const **p_groups
,
116 unsigned int * const p_num_groups
)
118 struct pinfunction
*func
;
119 const unsigned int *group_ids
;
120 unsigned int num_groups
;
123 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
125 if (!p_groups
|| !p_num_groups
)
128 if (selector
>= pmx
->nr_functions
)
131 func
= &pmx
->functions
[selector
];
135 ret
= pinctrl_ops
->function_groups_get(pmx
->ph
, selector
, &num_groups
,
138 dev_err(pmx
->dev
, "Unable to get function groups, err %d", ret
);
144 groups
= kcalloc(num_groups
, sizeof(*groups
), GFP_KERNEL
);
148 for (i
= 0; i
< num_groups
; i
++) {
149 groups
[i
] = pinctrl_scmi_get_group_name(pctldev
, group_ids
[i
]);
156 func
->ngroups
= num_groups
;
157 func
->groups
= groups
;
159 *p_groups
= func
->groups
;
160 *p_num_groups
= func
->ngroups
;
170 static int pinctrl_scmi_func_set_mux(struct pinctrl_dev
*pctldev
,
171 unsigned int selector
, unsigned int group
)
173 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
175 return pinctrl_ops
->mux_set(pmx
->ph
, selector
, group
);
178 static int pinctrl_scmi_request(struct pinctrl_dev
*pctldev
,
181 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
183 return pinctrl_ops
->pin_request(pmx
->ph
, offset
);
186 static int pinctrl_scmi_free(struct pinctrl_dev
*pctldev
, unsigned int offset
)
188 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
190 return pinctrl_ops
->pin_free(pmx
->ph
, offset
);
193 static const struct pinmux_ops pinctrl_scmi_pinmux_ops
= {
194 .request
= pinctrl_scmi_request
,
195 .free
= pinctrl_scmi_free
,
196 .get_functions_count
= pinctrl_scmi_get_functions_count
,
197 .get_function_name
= pinctrl_scmi_get_function_name
,
198 .get_function_groups
= pinctrl_scmi_get_function_groups
,
199 .set_mux
= pinctrl_scmi_func_set_mux
,
202 static int pinctrl_scmi_map_pinconf_type(enum pin_config_param param
,
203 enum scmi_pinctrl_conf_type
*type
)
208 case PIN_CONFIG_BIAS_BUS_HOLD
:
209 *type
= SCMI_PIN_BIAS_BUS_HOLD
;
211 case PIN_CONFIG_BIAS_DISABLE
:
212 *type
= SCMI_PIN_BIAS_DISABLE
;
214 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
215 *type
= SCMI_PIN_BIAS_HIGH_IMPEDANCE
;
217 case PIN_CONFIG_BIAS_PULL_DOWN
:
218 *type
= SCMI_PIN_BIAS_PULL_DOWN
;
220 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
:
221 *type
= SCMI_PIN_BIAS_PULL_DEFAULT
;
223 case PIN_CONFIG_BIAS_PULL_UP
:
224 *type
= SCMI_PIN_BIAS_PULL_UP
;
226 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
227 *type
= SCMI_PIN_DRIVE_OPEN_DRAIN
;
229 case PIN_CONFIG_DRIVE_OPEN_SOURCE
:
230 *type
= SCMI_PIN_DRIVE_OPEN_SOURCE
;
232 case PIN_CONFIG_DRIVE_PUSH_PULL
:
233 *type
= SCMI_PIN_DRIVE_PUSH_PULL
;
235 case PIN_CONFIG_DRIVE_STRENGTH
:
236 *type
= SCMI_PIN_DRIVE_STRENGTH
;
238 case PIN_CONFIG_DRIVE_STRENGTH_UA
:
239 *type
= SCMI_PIN_DRIVE_STRENGTH
;
241 case PIN_CONFIG_INPUT_DEBOUNCE
:
242 *type
= SCMI_PIN_INPUT_DEBOUNCE
;
244 case PIN_CONFIG_INPUT_ENABLE
:
245 *type
= SCMI_PIN_INPUT_MODE
;
247 case PIN_CONFIG_INPUT_SCHMITT
:
248 *type
= SCMI_PIN_INPUT_SCHMITT
;
250 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
251 *type
= SCMI_PIN_INPUT_MODE
;
253 case PIN_CONFIG_MODE_LOW_POWER
:
254 *type
= SCMI_PIN_LOW_POWER_MODE
;
256 case PIN_CONFIG_OUTPUT
:
257 *type
= SCMI_PIN_OUTPUT_VALUE
;
259 case PIN_CONFIG_OUTPUT_ENABLE
:
260 *type
= SCMI_PIN_OUTPUT_MODE
;
262 case PIN_CONFIG_OUTPUT_IMPEDANCE_OHMS
:
263 *type
= SCMI_PIN_OUTPUT_VALUE
;
265 case PIN_CONFIG_POWER_SOURCE
:
266 *type
= SCMI_PIN_POWER_SOURCE
;
268 case PIN_CONFIG_SLEW_RATE
:
269 *type
= SCMI_PIN_SLEW_RATE
;
271 case SCMI_PIN_OEM_START
... SCMI_PIN_OEM_END
:
281 static int pinctrl_scmi_pinconf_get(struct pinctrl_dev
*pctldev
,
282 unsigned int pin
, unsigned long *config
)
285 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
286 enum pin_config_param config_type
;
287 enum scmi_pinctrl_conf_type type
;
293 config_type
= pinconf_to_config_param(*config
);
295 ret
= pinctrl_scmi_map_pinconf_type(config_type
, &type
);
299 ret
= pinctrl_ops
->settings_get_one(pmx
->ph
, pin
, PIN_TYPE
, type
,
301 /* Convert SCMI error code to PINCTRL expected error code */
302 if (ret
== -EOPNOTSUPP
)
307 *config
= pinconf_to_config_packed(config_type
, config_value
);
313 pinctrl_scmi_alloc_configs(struct pinctrl_dev
*pctldev
, u32 num_configs
,
314 u32
**p_config_value
,
315 enum scmi_pinctrl_conf_type
**p_config_type
)
317 if (num_configs
<= SCMI_NUM_CONFIGS
)
320 *p_config_value
= kcalloc(num_configs
, sizeof(**p_config_value
), GFP_KERNEL
);
321 if (!*p_config_value
)
324 *p_config_type
= kcalloc(num_configs
, sizeof(**p_config_type
), GFP_KERNEL
);
325 if (!*p_config_type
) {
326 kfree(*p_config_value
);
334 pinctrl_scmi_free_configs(struct pinctrl_dev
*pctldev
, u32 num_configs
,
335 u32
**p_config_value
,
336 enum scmi_pinctrl_conf_type
**p_config_type
)
338 if (num_configs
<= SCMI_NUM_CONFIGS
)
341 kfree(*p_config_value
);
342 kfree(*p_config_type
);
345 static int pinctrl_scmi_pinconf_set(struct pinctrl_dev
*pctldev
,
347 unsigned long *configs
,
348 unsigned int num_configs
)
351 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
352 enum scmi_pinctrl_conf_type config_type
[SCMI_NUM_CONFIGS
];
353 u32 config_value
[SCMI_NUM_CONFIGS
];
354 enum scmi_pinctrl_conf_type
*p_config_type
= config_type
;
355 u32
*p_config_value
= config_value
;
356 enum pin_config_param param
;
358 if (!configs
|| !num_configs
)
361 ret
= pinctrl_scmi_alloc_configs(pctldev
, num_configs
, &p_config_type
,
366 for (i
= 0; i
< num_configs
; i
++) {
367 param
= pinconf_to_config_param(configs
[i
]);
368 ret
= pinctrl_scmi_map_pinconf_type(param
, &p_config_type
[i
]);
370 dev_err(pmx
->dev
, "Error map pinconf_type %d\n", ret
);
373 p_config_value
[i
] = pinconf_to_config_argument(configs
[i
]);
376 ret
= pinctrl_ops
->settings_conf(pmx
->ph
, pin
, PIN_TYPE
, num_configs
,
377 p_config_type
, p_config_value
);
379 dev_err(pmx
->dev
, "Error parsing config %d\n", ret
);
382 pinctrl_scmi_free_configs(pctldev
, num_configs
, &p_config_type
,
387 static int pinctrl_scmi_pinconf_group_set(struct pinctrl_dev
*pctldev
,
389 unsigned long *configs
,
390 unsigned int num_configs
)
393 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
394 enum scmi_pinctrl_conf_type config_type
[SCMI_NUM_CONFIGS
];
395 u32 config_value
[SCMI_NUM_CONFIGS
];
396 enum scmi_pinctrl_conf_type
*p_config_type
= config_type
;
397 u32
*p_config_value
= config_value
;
398 enum pin_config_param param
;
400 if (!configs
|| !num_configs
)
403 ret
= pinctrl_scmi_alloc_configs(pctldev
, num_configs
, &p_config_type
,
408 for (i
= 0; i
< num_configs
; i
++) {
409 param
= pinconf_to_config_param(configs
[i
]);
410 ret
= pinctrl_scmi_map_pinconf_type(param
, &p_config_type
[i
]);
412 dev_err(pmx
->dev
, "Error map pinconf_type %d\n", ret
);
416 p_config_value
[i
] = pinconf_to_config_argument(configs
[i
]);
419 ret
= pinctrl_ops
->settings_conf(pmx
->ph
, group
, GROUP_TYPE
,
420 num_configs
, p_config_type
,
423 dev_err(pmx
->dev
, "Error parsing config %d", ret
);
426 pinctrl_scmi_free_configs(pctldev
, num_configs
, &p_config_type
,
431 static int pinctrl_scmi_pinconf_group_get(struct pinctrl_dev
*pctldev
,
433 unsigned long *config
)
436 struct scmi_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
437 enum pin_config_param config_type
;
438 enum scmi_pinctrl_conf_type type
;
444 config_type
= pinconf_to_config_param(*config
);
445 ret
= pinctrl_scmi_map_pinconf_type(config_type
, &type
);
447 dev_err(pmx
->dev
, "Error map pinconf_type %d\n", ret
);
451 ret
= pinctrl_ops
->settings_get_one(pmx
->ph
, group
, GROUP_TYPE
, type
,
453 /* Convert SCMI error code to PINCTRL expected error code */
454 if (ret
== -EOPNOTSUPP
)
459 *config
= pinconf_to_config_packed(config_type
, config_value
);
464 static const struct pinconf_ops pinctrl_scmi_pinconf_ops
= {
466 .pin_config_get
= pinctrl_scmi_pinconf_get
,
467 .pin_config_set
= pinctrl_scmi_pinconf_set
,
468 .pin_config_group_set
= pinctrl_scmi_pinconf_group_set
,
469 .pin_config_group_get
= pinctrl_scmi_pinconf_group_get
,
470 .pin_config_config_dbg_show
= pinconf_generic_dump_config
,
473 static int pinctrl_scmi_get_pins(struct scmi_pinctrl
*pmx
,
474 struct pinctrl_desc
*desc
)
476 struct pinctrl_pin_desc
*pins
;
480 npins
= pinctrl_ops
->count_get(pmx
->ph
, PIN_TYPE
);
482 * npins will never be zero, the scmi pinctrl driver has bailed out
485 pins
= devm_kmalloc_array(pmx
->dev
, npins
, sizeof(*pins
), GFP_KERNEL
);
489 for (i
= 0; i
< npins
; i
++) {
492 * The memory for name is handled by the scmi firmware driver,
495 ret
= pinctrl_ops
->name_get(pmx
->ph
, i
, PIN_TYPE
, &pins
[i
].name
);
497 return dev_err_probe(pmx
->dev
, ret
,
498 "Can't get name for pin %d", i
);
503 dev_dbg(pmx
->dev
, "got pins %u", npins
);
508 static const char * const scmi_pinctrl_blocklist
[] = {
513 static int scmi_pinctrl_probe(struct scmi_device
*sdev
)
516 struct device
*dev
= &sdev
->dev
;
517 struct scmi_pinctrl
*pmx
;
518 const struct scmi_handle
*handle
;
519 struct scmi_protocol_handle
*ph
;
524 if (of_machine_compatible_match(scmi_pinctrl_blocklist
))
527 handle
= sdev
->handle
;
529 pinctrl_ops
= handle
->devm_protocol_get(sdev
, SCMI_PROTOCOL_PINCTRL
, &ph
);
530 if (IS_ERR(pinctrl_ops
))
531 return PTR_ERR(pinctrl_ops
);
533 pmx
= devm_kzalloc(dev
, sizeof(*pmx
), GFP_KERNEL
);
540 pmx
->pctl_desc
.name
= DRV_NAME
;
541 pmx
->pctl_desc
.owner
= THIS_MODULE
;
542 pmx
->pctl_desc
.pctlops
= &pinctrl_scmi_pinctrl_ops
;
543 pmx
->pctl_desc
.pmxops
= &pinctrl_scmi_pinmux_ops
;
544 pmx
->pctl_desc
.confops
= &pinctrl_scmi_pinconf_ops
;
546 ret
= pinctrl_scmi_get_pins(pmx
, &pmx
->pctl_desc
);
550 ret
= devm_pinctrl_register_and_init(dev
, &pmx
->pctl_desc
, pmx
,
553 return dev_err_probe(dev
, ret
, "Failed to register pinctrl\n");
555 pmx
->nr_functions
= pinctrl_scmi_get_functions_count(pmx
->pctldev
);
556 pmx
->functions
= devm_kcalloc(dev
, pmx
->nr_functions
,
557 sizeof(*pmx
->functions
), GFP_KERNEL
);
561 return pinctrl_enable(pmx
->pctldev
);
564 static const struct scmi_device_id scmi_id_table
[] = {
565 { SCMI_PROTOCOL_PINCTRL
, "pinctrl" },
568 MODULE_DEVICE_TABLE(scmi
, scmi_id_table
);
570 static struct scmi_driver scmi_pinctrl_driver
= {
572 .probe
= scmi_pinctrl_probe
,
573 .id_table
= scmi_id_table
,
575 module_scmi_driver(scmi_pinctrl_driver
);
577 MODULE_AUTHOR("Oleksii Moisieiev <oleksii_moisieiev@epam.com>");
578 MODULE_AUTHOR("Peng Fan <peng.fan@nxp.com>");
579 MODULE_DESCRIPTION("ARM SCMI pin controller driver");
580 MODULE_LICENSE("GPL");