xref: /linux/drivers/pinctrl/pinctrl-scmi.c (revision 5218b9ab39c96303862b5ad02bce6cbe066a4349)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Control and Power Interface (SCMI) Protocol based pinctrl driver
4  *
5  * Copyright (C) 2024 EPAM
6  * Copyright 2024 NXP
7  */
8 
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/scmi_protocol.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 
18 #include <linux/pinctrl/machine.h>
19 #include <linux/pinctrl/pinconf.h>
20 #include <linux/pinctrl/pinconf-generic.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinmux.h>
23 
24 #include "pinctrl-utils.h"
25 #include "core.h"
26 #include "pinconf.h"
27 #include "pinmux.h"
28 
29 #define DRV_NAME "scmi-pinctrl"
30 
31 /* Define num configs, if not large than 4 use stack, else use kcalloc() */
32 #define SCMI_NUM_CONFIGS	4
33 
34 static const struct scmi_pinctrl_proto_ops *pinctrl_ops;
35 
36 struct scmi_pinctrl {
37 	struct device *dev;
38 	struct scmi_protocol_handle *ph;
39 	struct pinctrl_dev *pctldev;
40 	struct pinctrl_desc pctl_desc;
41 };
42 
43 static const struct pinctrl_ops pinctrl_scmi_pinctrl_ops = {
44 	.get_groups_count = pinctrl_generic_get_group_count,
45 	.get_group_name = pinctrl_generic_get_group_name,
46 	.get_group_pins = pinctrl_generic_get_group_pins,
47 #ifdef CONFIG_OF
48 	.dt_node_to_map = pinconf_generic_dt_node_to_map_all,
49 	.dt_free_map = pinconf_generic_dt_free_map,
50 #endif
51 };
52 
53 static int pinctrl_scmi_func_set_mux(struct pinctrl_dev *pctldev,
54 				     unsigned int selector, unsigned int group)
55 {
56 	struct scmi_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
57 
58 	return pinctrl_ops->mux_set(pmx->ph, selector, group);
59 }
60 
61 static int pinctrl_scmi_request(struct pinctrl_dev *pctldev,
62 				unsigned int offset)
63 {
64 	struct scmi_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
65 
66 	return pinctrl_ops->pin_request(pmx->ph, offset);
67 }
68 
69 static int pinctrl_scmi_free(struct pinctrl_dev *pctldev, unsigned int offset)
70 {
71 	struct scmi_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
72 
73 	return pinctrl_ops->pin_free(pmx->ph, offset);
74 }
75 
76 static const struct pinmux_ops pinctrl_scmi_pinmux_ops = {
77 	.request = pinctrl_scmi_request,
78 	.free = pinctrl_scmi_free,
79 	.get_functions_count = pinmux_generic_get_function_count,
80 	.get_function_name = pinmux_generic_get_function_name,
81 	.get_function_groups = pinmux_generic_get_function_groups,
82 	.set_mux = pinctrl_scmi_func_set_mux,
83 };
84 
85 static int pinctrl_scmi_map_pinconf_type(enum pin_config_param param,
86 					 enum scmi_pinctrl_conf_type *type)
87 {
88 	u32 arg = param;
89 
90 	switch (arg) {
91 	case PIN_CONFIG_BIAS_BUS_HOLD:
92 		*type = SCMI_PIN_BIAS_BUS_HOLD;
93 		break;
94 	case PIN_CONFIG_BIAS_DISABLE:
95 		*type = SCMI_PIN_BIAS_DISABLE;
96 		break;
97 	case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
98 		*type = SCMI_PIN_BIAS_HIGH_IMPEDANCE;
99 		break;
100 	case PIN_CONFIG_BIAS_PULL_DOWN:
101 		*type = SCMI_PIN_BIAS_PULL_DOWN;
102 		break;
103 	case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
104 		*type = SCMI_PIN_BIAS_PULL_DEFAULT;
105 		break;
106 	case PIN_CONFIG_BIAS_PULL_UP:
107 		*type = SCMI_PIN_BIAS_PULL_UP;
108 		break;
109 	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
110 		*type = SCMI_PIN_DRIVE_OPEN_DRAIN;
111 		break;
112 	case PIN_CONFIG_DRIVE_OPEN_SOURCE:
113 		*type = SCMI_PIN_DRIVE_OPEN_SOURCE;
114 		break;
115 	case PIN_CONFIG_DRIVE_PUSH_PULL:
116 		*type = SCMI_PIN_DRIVE_PUSH_PULL;
117 		break;
118 	case PIN_CONFIG_DRIVE_STRENGTH:
119 		*type = SCMI_PIN_DRIVE_STRENGTH;
120 		break;
121 	case PIN_CONFIG_DRIVE_STRENGTH_UA:
122 		*type = SCMI_PIN_DRIVE_STRENGTH;
123 		break;
124 	case PIN_CONFIG_INPUT_DEBOUNCE:
125 		*type = SCMI_PIN_INPUT_DEBOUNCE;
126 		break;
127 	case PIN_CONFIG_INPUT_ENABLE:
128 		*type = SCMI_PIN_INPUT_MODE;
129 		break;
130 	case PIN_CONFIG_INPUT_SCHMITT:
131 		*type = SCMI_PIN_INPUT_SCHMITT;
132 		break;
133 	case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
134 		*type = SCMI_PIN_INPUT_MODE;
135 		break;
136 	case PIN_CONFIG_MODE_LOW_POWER:
137 		*type = SCMI_PIN_LOW_POWER_MODE;
138 		break;
139 	case PIN_CONFIG_OUTPUT_ENABLE:
140 		*type = SCMI_PIN_OUTPUT_MODE;
141 		break;
142 	case PIN_CONFIG_POWER_SOURCE:
143 		*type = SCMI_PIN_POWER_SOURCE;
144 		break;
145 	case PIN_CONFIG_SLEW_RATE:
146 		*type = SCMI_PIN_SLEW_RATE;
147 		break;
148 	case SCMI_PIN_OEM_START ... SCMI_PIN_OEM_END:
149 		*type = arg;
150 		break;
151 	default:
152 		return -EINVAL;
153 	}
154 
155 	return 0;
156 }
157 
158 static int pinctrl_scmi_map_pinconf_type_get(enum pin_config_param param,
159 					     enum scmi_pinctrl_conf_type *type)
160 {
161 	if (param == PIN_CONFIG_LEVEL) {
162 		*type = SCMI_PIN_INPUT_VALUE;
163 		return 0;
164 	}
165 
166 	return pinctrl_scmi_map_pinconf_type(param, type);
167 }
168 
169 static int pinctrl_scmi_map_pinconf_type_set(enum pin_config_param param,
170 					     enum scmi_pinctrl_conf_type *type)
171 {
172 	if (param == PIN_CONFIG_LEVEL) {
173 		*type = SCMI_PIN_OUTPUT_VALUE;
174 		return 0;
175 	}
176 
177 	return pinctrl_scmi_map_pinconf_type(param, type);
178 }
179 
180 static int pinctrl_scmi_pinconf_get(struct pinctrl_dev *pctldev,
181 				    unsigned int pin, unsigned long *config)
182 {
183 	int ret;
184 	struct scmi_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
185 	enum pin_config_param config_type;
186 	enum scmi_pinctrl_conf_type type;
187 	u32 config_value;
188 
189 	if (!config)
190 		return -EINVAL;
191 
192 	config_type = pinconf_to_config_param(*config);
193 
194 	ret = pinctrl_scmi_map_pinconf_type_get(config_type, &type);
195 	if (ret)
196 		return ret;
197 
198 	ret = pinctrl_ops->settings_get_one(pmx->ph, pin, PIN_TYPE, type,
199 					    &config_value);
200 	/* Convert SCMI error code to PINCTRL expected error code */
201 	if (ret == -EOPNOTSUPP)
202 		return -ENOTSUPP;
203 	if (ret)
204 		return ret;
205 
206 	*config = pinconf_to_config_packed(config_type, config_value);
207 
208 	return 0;
209 }
210 
211 static int
212 pinctrl_scmi_alloc_configs(struct pinctrl_dev *pctldev, u32 num_configs,
213 			   u32 **p_config_value,
214 			   enum scmi_pinctrl_conf_type **p_config_type)
215 {
216 	if (num_configs <= SCMI_NUM_CONFIGS)
217 		return 0;
218 
219 	*p_config_value = kcalloc(num_configs, sizeof(**p_config_value), GFP_KERNEL);
220 	if (!*p_config_value)
221 		return -ENOMEM;
222 
223 	*p_config_type = kzalloc_objs(**p_config_type, num_configs);
224 	if (!*p_config_type) {
225 		kfree(*p_config_value);
226 		return -ENOMEM;
227 	}
228 
229 	return 0;
230 }
231 
232 static void
233 pinctrl_scmi_free_configs(struct pinctrl_dev *pctldev, u32 num_configs,
234 			  u32 **p_config_value,
235 			  enum scmi_pinctrl_conf_type **p_config_type)
236 {
237 	if (num_configs <= SCMI_NUM_CONFIGS)
238 		return;
239 
240 	kfree(*p_config_value);
241 	kfree(*p_config_type);
242 }
243 
244 static int pinctrl_scmi_pinconf_set(struct pinctrl_dev *pctldev,
245 				    unsigned int pin,
246 				    unsigned long *configs,
247 				    unsigned int num_configs)
248 {
249 	int i, cnt, ret;
250 	struct scmi_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
251 	enum scmi_pinctrl_conf_type config_type[SCMI_NUM_CONFIGS];
252 	u32 config_value[SCMI_NUM_CONFIGS];
253 	enum scmi_pinctrl_conf_type *p_config_type = config_type;
254 	u32 *p_config_value = config_value;
255 	enum pin_config_param param;
256 
257 	if (!configs || !num_configs)
258 		return -EINVAL;
259 
260 	ret = pinctrl_scmi_alloc_configs(pctldev, num_configs, &p_config_type,
261 					 &p_config_value);
262 	if (ret)
263 		return ret;
264 
265 	cnt = 0;
266 	for (i = 0; i < num_configs; i++) {
267 		param = pinconf_to_config_param(configs[i]);
268 		if (param == PIN_CONFIG_PERSIST_STATE)
269 			continue;
270 		ret = pinctrl_scmi_map_pinconf_type_set(param, &p_config_type[cnt]);
271 		if (ret) {
272 			dev_err(pmx->dev, "Error map pinconf_type %d\n", ret);
273 			goto free_config;
274 		}
275 		p_config_value[cnt] = pinconf_to_config_argument(configs[i]);
276 		cnt++;
277 	}
278 
279 	ret = pinctrl_ops->settings_conf(pmx->ph, pin, PIN_TYPE, cnt,
280 					 p_config_type,  p_config_value);
281 	if (ret)
282 		dev_err(pmx->dev, "Error parsing config %d\n", ret);
283 
284 free_config:
285 	pinctrl_scmi_free_configs(pctldev, num_configs, &p_config_type,
286 				  &p_config_value);
287 	return ret;
288 }
289 
290 static int pinctrl_scmi_pinconf_group_set(struct pinctrl_dev *pctldev,
291 					  unsigned int group,
292 					  unsigned long *configs,
293 					  unsigned int num_configs)
294 {
295 	int i, ret;
296 	struct scmi_pinctrl *pmx =  pinctrl_dev_get_drvdata(pctldev);
297 	enum scmi_pinctrl_conf_type config_type[SCMI_NUM_CONFIGS];
298 	u32 config_value[SCMI_NUM_CONFIGS];
299 	enum scmi_pinctrl_conf_type *p_config_type = config_type;
300 	u32 *p_config_value = config_value;
301 	enum pin_config_param param;
302 
303 	if (!configs || !num_configs)
304 		return -EINVAL;
305 
306 	ret = pinctrl_scmi_alloc_configs(pctldev, num_configs, &p_config_type,
307 					 &p_config_value);
308 	if (ret)
309 		return ret;
310 
311 	for (i = 0; i < num_configs; i++) {
312 		param = pinconf_to_config_param(configs[i]);
313 		ret = pinctrl_scmi_map_pinconf_type_set(param, &p_config_type[i]);
314 		if (ret) {
315 			dev_err(pmx->dev, "Error map pinconf_type %d\n", ret);
316 			goto free_config;
317 		}
318 
319 		p_config_value[i] = pinconf_to_config_argument(configs[i]);
320 	}
321 
322 	ret = pinctrl_ops->settings_conf(pmx->ph, group, GROUP_TYPE,
323 					 num_configs, p_config_type,
324 					 p_config_value);
325 	if (ret)
326 		dev_err(pmx->dev, "Error parsing config %d", ret);
327 
328 free_config:
329 	pinctrl_scmi_free_configs(pctldev, num_configs, &p_config_type,
330 				  &p_config_value);
331 	return ret;
332 };
333 
334 static int pinctrl_scmi_pinconf_group_get(struct pinctrl_dev *pctldev,
335 					  unsigned int group,
336 					  unsigned long *config)
337 {
338 	int ret;
339 	struct scmi_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
340 	enum pin_config_param config_type;
341 	enum scmi_pinctrl_conf_type type;
342 	u32 config_value;
343 
344 	if (!config)
345 		return -EINVAL;
346 
347 	config_type = pinconf_to_config_param(*config);
348 	ret = pinctrl_scmi_map_pinconf_type_get(config_type, &type);
349 	if (ret) {
350 		dev_err(pmx->dev, "Error map pinconf_type %d\n", ret);
351 		return ret;
352 	}
353 
354 	ret = pinctrl_ops->settings_get_one(pmx->ph, group, GROUP_TYPE, type,
355 					    &config_value);
356 	/* Convert SCMI error code to PINCTRL expected error code */
357 	if (ret == -EOPNOTSUPP)
358 		return -ENOTSUPP;
359 	if (ret)
360 		return ret;
361 
362 	*config = pinconf_to_config_packed(config_type, config_value);
363 
364 	return 0;
365 }
366 
367 static const struct pinconf_ops pinctrl_scmi_pinconf_ops = {
368 	.is_generic = true,
369 	.pin_config_get = pinctrl_scmi_pinconf_get,
370 	.pin_config_set = pinctrl_scmi_pinconf_set,
371 	.pin_config_group_set = pinctrl_scmi_pinconf_group_set,
372 	.pin_config_group_get = pinctrl_scmi_pinconf_group_get,
373 	.pin_config_config_dbg_show = pinconf_generic_dump_config,
374 };
375 
376 static int pinctrl_scmi_get_functions(struct scmi_pinctrl *pmx)
377 {
378 	unsigned int nr_funcs, nr_groups, i, j;
379 	const unsigned int *group_ids;
380 	const char *fname;
381 	const char **gnames;
382 	int ret;
383 
384 	nr_funcs = pinctrl_ops->count_get(pmx->ph, FUNCTION_TYPE);
385 
386 	for (i = 0; i < nr_funcs; i++) {
387 		ret = pinctrl_ops->name_get(pmx->ph, i, FUNCTION_TYPE,
388 					    &fname);
389 		if (ret)
390 			return ret;
391 
392 		ret = pinctrl_ops->function_groups_get(pmx->ph, i,
393 						       &nr_groups,
394 						       &group_ids);
395 		if (ret)
396 			return ret;
397 
398 		if (!nr_groups)
399 			dev_warn(pmx->dev,
400 				 "Function %s contains no groups\n",
401 				 fname);
402 
403 		gnames = devm_kmalloc_array(pmx->dev, nr_groups,
404 					    sizeof(*gnames), GFP_KERNEL);
405 		if (!gnames)
406 			return -ENOMEM;
407 
408 		for (j = 0; j < nr_groups; j++) {
409 			ret = pinctrl_ops->name_get(pmx->ph, group_ids[j],
410 						    GROUP_TYPE, &gnames[j]);
411 			if (ret)
412 				return ret;
413 		}
414 
415 		ret = pinmux_generic_add_function(pmx->pctldev, fname, gnames,
416 				  nr_groups, NULL);
417 		if (ret < 0)
418 			return ret;
419 		if (ret != i) {
420 			dev_err(pmx->dev,
421 				"Duplicate function name: %s index: %u selector: %d\n",
422 				fname, i, ret);
423 			return -EINVAL;
424 		}
425 	}
426 
427 	return 0;
428 }
429 
430 static int pinctrl_scmi_get_groups(struct scmi_pinctrl *pmx)
431 {
432 	unsigned int nr_groups, nr_pins, i;
433 	const unsigned int *pins;
434 	const char *gname;
435 	int ret;
436 
437 	nr_groups = pinctrl_ops->count_get(pmx->ph, GROUP_TYPE);
438 
439 	for (i = 0; i < nr_groups; i++) {
440 		ret = pinctrl_ops->name_get(pmx->ph, i, GROUP_TYPE, &gname);
441 		if (ret)
442 			return ret;
443 
444 		ret = pinctrl_ops->group_pins_get(pmx->ph, i, &pins,
445 						  &nr_pins);
446 		if (ret)
447 			return ret;
448 
449 		if (!nr_pins)
450 			dev_warn(pmx->dev,
451 				 "Group %s contains no pins\n", gname);
452 
453 		ret = pinctrl_generic_add_group(pmx->pctldev, gname, pins,
454 						nr_pins, NULL);
455 		if (ret < 0)
456 			return ret;
457 		if (ret != i) {
458 			dev_err(pmx->dev,
459 				"Duplicate group name: %s index: %u selector: %d\n",
460 				gname, i, ret);
461 			return -EINVAL;
462 		}
463 	}
464 
465 	return 0;
466 }
467 
468 static int pinctrl_scmi_get_pins(struct scmi_pinctrl *pmx,
469 				 struct pinctrl_desc *desc)
470 {
471 	struct pinctrl_pin_desc *pins;
472 	unsigned int npins;
473 	int ret, i;
474 
475 	npins = pinctrl_ops->count_get(pmx->ph, PIN_TYPE);
476 	/*
477 	 * npins will never be zero, the scmi pinctrl driver has bailed out
478 	 * if npins is zero.
479 	 */
480 	pins = devm_kmalloc_array(pmx->dev, npins, sizeof(*pins), GFP_KERNEL);
481 	if (!pins)
482 		return -ENOMEM;
483 
484 	for (i = 0; i < npins; i++) {
485 		pins[i].number = i;
486 		/*
487 		 * The memory for name is handled by the scmi firmware driver,
488 		 * no need free here
489 		 */
490 		ret = pinctrl_ops->name_get(pmx->ph, i, PIN_TYPE, &pins[i].name);
491 		if (ret)
492 			return dev_err_probe(pmx->dev, ret,
493 					     "Can't get name for pin %d", i);
494 	}
495 
496 	desc->npins = npins;
497 	desc->pins = pins;
498 	dev_dbg(pmx->dev, "got pins %u", npins);
499 
500 	return 0;
501 }
502 
503 static const char * const scmi_pinctrl_blocklist[] = {
504 	"fsl,imx94",
505 	"fsl,imx95",
506 	"fsl,imx952",
507 	NULL
508 };
509 
510 static int scmi_pinctrl_probe(struct scmi_device *sdev)
511 {
512 	int ret;
513 	struct device *dev = &sdev->dev;
514 	struct scmi_pinctrl *pmx;
515 	const struct scmi_handle *handle;
516 	struct scmi_protocol_handle *ph;
517 
518 	if (!sdev->handle)
519 		return -EINVAL;
520 
521 	if (of_machine_compatible_match(scmi_pinctrl_blocklist))
522 		return -ENODEV;
523 
524 	handle = sdev->handle;
525 
526 	pinctrl_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_PINCTRL, &ph);
527 	if (IS_ERR(pinctrl_ops))
528 		return PTR_ERR(pinctrl_ops);
529 
530 	pmx = devm_kzalloc(dev, sizeof(*pmx), GFP_KERNEL);
531 	if (!pmx)
532 		return -ENOMEM;
533 
534 	pmx->ph = ph;
535 
536 	pmx->dev = dev;
537 	pmx->pctl_desc.name = DRV_NAME;
538 	pmx->pctl_desc.owner = THIS_MODULE;
539 	pmx->pctl_desc.pctlops = &pinctrl_scmi_pinctrl_ops;
540 	pmx->pctl_desc.pmxops = &pinctrl_scmi_pinmux_ops;
541 	pmx->pctl_desc.confops = &pinctrl_scmi_pinconf_ops;
542 
543 	ret = pinctrl_scmi_get_pins(pmx, &pmx->pctl_desc);
544 	if (ret)
545 		return ret;
546 
547 	ret = devm_pinctrl_register_and_init(dev, &pmx->pctl_desc, pmx,
548 					     &pmx->pctldev);
549 	if (ret)
550 		return dev_err_probe(dev, ret, "Failed to register pinctrl\n");
551 
552 	ret = pinctrl_scmi_get_groups(pmx);
553 	if (ret)
554 		return dev_err_probe(dev, ret, "Failed to register groups\n");
555 
556 	ret = pinctrl_scmi_get_functions(pmx);
557 	if (ret)
558 		return dev_err_probe(dev, ret, "Failed to register functions\n");
559 
560 	return pinctrl_enable(pmx->pctldev);
561 }
562 
563 static const struct scmi_device_id scmi_id_table[] = {
564 	{ SCMI_PROTOCOL_PINCTRL, "pinctrl" },
565 	{ }
566 };
567 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
568 
569 static struct scmi_driver scmi_pinctrl_driver = {
570 	.name = DRV_NAME,
571 	.probe = scmi_pinctrl_probe,
572 	.id_table = scmi_id_table,
573 };
574 module_scmi_driver(scmi_pinctrl_driver);
575 
576 MODULE_AUTHOR("Oleksii Moisieiev <oleksii_moisieiev@epam.com>");
577 MODULE_AUTHOR("Peng Fan <peng.fan@nxp.com>");
578 MODULE_DESCRIPTION("ARM SCMI pin controller driver");
579 MODULE_LICENSE("GPL");
580