1 // SPDX-License-Identifier: GPL-2.0
3 * ZynqMP pin controller
5 * Copyright (C) 2020, 2021 Xilinx, Inc.
7 * Sai Krishna Potthuri <lakshmi.sai.krishna.potthuri@xilinx.com>
8 * Rajan Vaja <rajan.vaja@xilinx.com>
11 #include <dt-bindings/pinctrl/pinctrl-zynqmp.h>
13 #include <linux/bitfield.h>
14 #include <linux/bitmap.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/platform_device.h>
20 #include <linux/firmware/xlnx-zynqmp.h>
22 #include <linux/pinctrl/pinconf-generic.h>
23 #include <linux/pinctrl/pinconf.h>
24 #include <linux/pinctrl/pinctrl.h>
25 #include <linux/pinctrl/pinmux.h>
28 #include "pinctrl-utils.h"
30 #define ZYNQMP_PIN_PREFIX "MIO"
31 #define PINCTRL_GET_FUNC_NAME_RESP_LEN 16
32 #define MAX_FUNC_NAME_LEN 16
33 #define MAX_GROUP_PIN 50
34 #define MAX_PIN_GROUPS 50
35 #define END_OF_FUNCTIONS "END_OF_FUNCTIONS"
36 #define NUM_GROUPS_PER_RESP 6
38 #define PINCTRL_GET_FUNC_GROUPS_RESP_LEN 12
39 #define PINCTRL_GET_PIN_GROUPS_RESP_LEN 12
40 #define NA_GROUP 0xFFFF
41 #define RESERVED_GROUP 0xFFFE
43 #define DRIVE_STRENGTH_2MA 2
44 #define DRIVE_STRENGTH_4MA 4
45 #define DRIVE_STRENGTH_8MA 8
46 #define DRIVE_STRENGTH_12MA 12
48 #define VERSAL_LPD_PIN_PREFIX "LPD_MIO"
49 #define VERSAL_PMC_PIN_PREFIX "PMC_MIO"
51 #define VERSAL_PINCTRL_ATTR_NODETYPE_MASK GENMASK(19, 14)
52 #define VERSAL_PINCTRL_NODETYPE_LPD_MIO BIT(0)
55 * struct zynqmp_pmux_function - a pinmux function
56 * @name: Name of the pin mux function
57 * @groups: List of pin groups for this function
58 * @ngroups: Number of entries in @groups
60 * This structure holds information about pin control function
61 * and function group names supporting that function.
63 struct zynqmp_pmux_function
{
64 char name
[MAX_FUNC_NAME_LEN
];
65 const char * const *groups
;
70 * struct zynqmp_pinctrl - driver data
71 * @pctrl: Pin control device
73 * @ngroups: Number of @groups
74 * @funcs: Pin mux functions
75 * @nfuncs: Number of @funcs
77 * This struct is stored as driver data and used to retrieve
78 * information regarding pin control functions, groups and
81 struct zynqmp_pinctrl
{
82 struct pinctrl_dev
*pctrl
;
83 const struct zynqmp_pctrl_group
*groups
;
85 const struct zynqmp_pmux_function
*funcs
;
90 * struct zynqmp_pctrl_group - Pin control group info
92 * @pins: Group pin numbers
93 * @npins: Number of pins in the group
95 struct zynqmp_pctrl_group
{
97 unsigned int pins
[MAX_GROUP_PIN
];
101 static struct pinctrl_desc zynqmp_desc
;
102 static u32 family_code
;
103 static u32 sub_family_code
;
105 static int zynqmp_pctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
107 struct zynqmp_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
109 return pctrl
->ngroups
+ zynqmp_desc
.npins
;
112 static const char *zynqmp_pctrl_get_group_name(struct pinctrl_dev
*pctldev
,
113 unsigned int selector
)
115 struct zynqmp_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
117 if (selector
< pctrl
->ngroups
)
118 return pctrl
->groups
[selector
].name
;
120 return zynqmp_desc
.pins
[selector
- pctrl
->ngroups
].name
;
123 static int zynqmp_pctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
124 unsigned int selector
,
125 const unsigned int **pins
,
128 struct zynqmp_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
130 if (selector
< pctrl
->ngroups
) {
131 *pins
= pctrl
->groups
[selector
].pins
;
132 *npins
= pctrl
->groups
[selector
].npins
;
134 *pins
= &zynqmp_desc
.pins
[selector
- pctrl
->ngroups
].number
;
141 static const struct pinctrl_ops zynqmp_pctrl_ops
= {
142 .get_groups_count
= zynqmp_pctrl_get_groups_count
,
143 .get_group_name
= zynqmp_pctrl_get_group_name
,
144 .get_group_pins
= zynqmp_pctrl_get_group_pins
,
145 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
146 .dt_free_map
= pinctrl_utils_free_map
,
149 static int zynqmp_pinmux_request_pin(struct pinctrl_dev
*pctldev
,
154 ret
= zynqmp_pm_pinctrl_request(pin
);
156 dev_err(pctldev
->dev
, "request failed for pin %u\n", pin
);
163 static int zynqmp_pmux_get_functions_count(struct pinctrl_dev
*pctldev
)
165 struct zynqmp_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
167 return pctrl
->nfuncs
;
170 static const char *zynqmp_pmux_get_function_name(struct pinctrl_dev
*pctldev
,
171 unsigned int selector
)
173 struct zynqmp_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
175 return pctrl
->funcs
[selector
].name
;
179 * zynqmp_pmux_get_function_groups() - Get groups for the function
180 * @pctldev: Pincontrol device pointer.
181 * @selector: Function ID
182 * @groups: Group names.
183 * @num_groups: Number of function groups.
185 * Get function's group count and group names.
189 static int zynqmp_pmux_get_function_groups(struct pinctrl_dev
*pctldev
,
190 unsigned int selector
,
191 const char * const **groups
,
192 unsigned * const num_groups
)
194 struct zynqmp_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
196 *groups
= pctrl
->funcs
[selector
].groups
;
197 *num_groups
= pctrl
->funcs
[selector
].ngroups
;
203 * zynqmp_pinmux_set_mux() - Set requested function for the group
204 * @pctldev: Pincontrol device pointer.
205 * @function: Function ID.
208 * Loop through all pins of the group and call firmware API
209 * to set requested function for all pins in the group.
211 * Return: 0 on success else error code.
213 static int zynqmp_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
214 unsigned int function
,
217 const unsigned int *pins
;
221 zynqmp_pctrl_get_group_pins(pctldev
, group
, &pins
, &npins
);
222 for (i
= 0; i
< npins
; i
++) {
223 ret
= zynqmp_pm_pinctrl_set_function(pins
[i
], function
);
225 dev_err(pctldev
->dev
, "set mux failed for pin %u\n",
234 static int zynqmp_pinmux_release_pin(struct pinctrl_dev
*pctldev
,
239 ret
= zynqmp_pm_pinctrl_release(pin
);
241 dev_err(pctldev
->dev
, "free pin failed for pin %u\n",
249 static const struct pinmux_ops zynqmp_pinmux_ops
= {
250 .request
= zynqmp_pinmux_request_pin
,
251 .get_functions_count
= zynqmp_pmux_get_functions_count
,
252 .get_function_name
= zynqmp_pmux_get_function_name
,
253 .get_function_groups
= zynqmp_pmux_get_function_groups
,
254 .set_mux
= zynqmp_pinmux_set_mux
,
255 .free
= zynqmp_pinmux_release_pin
,
259 * zynqmp_pinconf_cfg_get() - get config value for the pin
260 * @pctldev: Pin control device pointer.
262 * @config: Value of config param.
264 * Get value of the requested configuration parameter for the
267 * Return: 0 on success else error code.
269 static int zynqmp_pinconf_cfg_get(struct pinctrl_dev
*pctldev
,
271 unsigned long *config
)
273 unsigned int arg
, param
= pinconf_to_config_param(*config
);
277 case PIN_CONFIG_SLEW_RATE
:
278 param
= PM_PINCTRL_CONFIG_SLEW_RATE
;
279 ret
= zynqmp_pm_pinctrl_get_config(pin
, param
, &arg
);
281 case PIN_CONFIG_BIAS_PULL_UP
:
282 param
= PM_PINCTRL_CONFIG_PULL_CTRL
;
283 ret
= zynqmp_pm_pinctrl_get_config(pin
, param
, &arg
);
284 if (arg
!= PM_PINCTRL_BIAS_PULL_UP
)
289 case PIN_CONFIG_BIAS_PULL_DOWN
:
290 param
= PM_PINCTRL_CONFIG_PULL_CTRL
;
291 ret
= zynqmp_pm_pinctrl_get_config(pin
, param
, &arg
);
292 if (arg
!= PM_PINCTRL_BIAS_PULL_DOWN
)
297 case PIN_CONFIG_BIAS_DISABLE
:
298 param
= PM_PINCTRL_CONFIG_BIAS_STATUS
;
299 ret
= zynqmp_pm_pinctrl_get_config(pin
, param
, &arg
);
300 if (arg
!= PM_PINCTRL_BIAS_DISABLE
)
305 case PIN_CONFIG_POWER_SOURCE
:
306 param
= PM_PINCTRL_CONFIG_VOLTAGE_STATUS
;
307 ret
= zynqmp_pm_pinctrl_get_config(pin
, param
, &arg
);
309 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
310 param
= PM_PINCTRL_CONFIG_SCHMITT_CMOS
;
311 ret
= zynqmp_pm_pinctrl_get_config(pin
, param
, &arg
);
313 case PIN_CONFIG_DRIVE_STRENGTH
:
314 param
= PM_PINCTRL_CONFIG_DRIVE_STRENGTH
;
315 ret
= zynqmp_pm_pinctrl_get_config(pin
, param
, &arg
);
317 case PM_PINCTRL_DRIVE_STRENGTH_2MA
:
318 arg
= DRIVE_STRENGTH_2MA
;
320 case PM_PINCTRL_DRIVE_STRENGTH_4MA
:
321 arg
= DRIVE_STRENGTH_4MA
;
323 case PM_PINCTRL_DRIVE_STRENGTH_8MA
:
324 arg
= DRIVE_STRENGTH_8MA
;
326 case PM_PINCTRL_DRIVE_STRENGTH_12MA
:
327 arg
= DRIVE_STRENGTH_12MA
;
330 /* Invalid drive strength */
331 dev_warn(pctldev
->dev
,
332 "Invalid drive strength for pin %d\n",
345 param
= pinconf_to_config_param(*config
);
346 *config
= pinconf_to_config_packed(param
, arg
);
352 * zynqmp_pinconf_cfg_set() - Set requested config for the pin
353 * @pctldev: Pincontrol device pointer.
355 * @configs: Configuration to set.
356 * @num_configs: Number of configurations.
358 * Loop through all configurations and call firmware API
359 * to set requested configurations for the pin.
361 * Return: 0 on success else error code.
363 static int zynqmp_pinconf_cfg_set(struct pinctrl_dev
*pctldev
,
364 unsigned int pin
, unsigned long *configs
,
365 unsigned int num_configs
)
369 for (i
= 0; i
< num_configs
; i
++) {
370 unsigned int param
= pinconf_to_config_param(configs
[i
]);
371 unsigned int arg
= pinconf_to_config_argument(configs
[i
]);
375 case PIN_CONFIG_SLEW_RATE
:
376 param
= PM_PINCTRL_CONFIG_SLEW_RATE
;
377 ret
= zynqmp_pm_pinctrl_set_config(pin
, param
, arg
);
379 case PIN_CONFIG_BIAS_PULL_UP
:
380 param
= PM_PINCTRL_CONFIG_PULL_CTRL
;
381 arg
= PM_PINCTRL_BIAS_PULL_UP
;
382 ret
= zynqmp_pm_pinctrl_set_config(pin
, param
, arg
);
384 case PIN_CONFIG_BIAS_PULL_DOWN
:
385 param
= PM_PINCTRL_CONFIG_PULL_CTRL
;
386 arg
= PM_PINCTRL_BIAS_PULL_DOWN
;
387 ret
= zynqmp_pm_pinctrl_set_config(pin
, param
, arg
);
389 case PIN_CONFIG_BIAS_DISABLE
:
390 param
= PM_PINCTRL_CONFIG_BIAS_STATUS
;
391 arg
= PM_PINCTRL_BIAS_DISABLE
;
392 ret
= zynqmp_pm_pinctrl_set_config(pin
, param
, arg
);
394 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
395 param
= PM_PINCTRL_CONFIG_SCHMITT_CMOS
;
396 ret
= zynqmp_pm_pinctrl_set_config(pin
, param
, arg
);
398 case PIN_CONFIG_DRIVE_STRENGTH
:
400 case DRIVE_STRENGTH_2MA
:
401 value
= PM_PINCTRL_DRIVE_STRENGTH_2MA
;
403 case DRIVE_STRENGTH_4MA
:
404 value
= PM_PINCTRL_DRIVE_STRENGTH_4MA
;
406 case DRIVE_STRENGTH_8MA
:
407 value
= PM_PINCTRL_DRIVE_STRENGTH_8MA
;
409 case DRIVE_STRENGTH_12MA
:
410 value
= PM_PINCTRL_DRIVE_STRENGTH_12MA
;
413 /* Invalid drive strength */
414 dev_warn(pctldev
->dev
,
415 "Invalid drive strength for pin %d\n",
420 param
= PM_PINCTRL_CONFIG_DRIVE_STRENGTH
;
421 ret
= zynqmp_pm_pinctrl_set_config(pin
, param
, value
);
423 case PIN_CONFIG_POWER_SOURCE
:
424 param
= PM_PINCTRL_CONFIG_VOLTAGE_STATUS
;
425 ret
= zynqmp_pm_pinctrl_get_config(pin
, param
, &value
);
428 dev_warn(pctldev
->dev
,
429 "Invalid IO Standard requested for pin %d\n",
433 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE
:
434 param
= PM_PINCTRL_CONFIG_TRI_STATE
;
435 arg
= PM_PINCTRL_TRI_STATE_ENABLE
;
436 ret
= zynqmp_pm_pinctrl_set_config(pin
, param
, arg
);
438 case PIN_CONFIG_MODE_LOW_POWER
:
440 * These cases are mentioned in dts but configurable
441 * registers are unknown. So falling through to ignore
442 * boot time warnings as of now.
446 case PIN_CONFIG_OUTPUT_ENABLE
:
447 param
= PM_PINCTRL_CONFIG_TRI_STATE
;
448 arg
= PM_PINCTRL_TRI_STATE_DISABLE
;
449 ret
= zynqmp_pm_pinctrl_set_config(pin
, param
, arg
);
452 dev_warn(pctldev
->dev
,
453 "unsupported configuration parameter '%u'\n",
459 param
= pinconf_to_config_param(configs
[i
]);
460 arg
= pinconf_to_config_argument(configs
[i
]);
462 dev_warn(pctldev
->dev
,
463 "failed to set: pin %u param %u value %u\n",
471 * zynqmp_pinconf_group_set() - Set requested config for the group
472 * @pctldev: Pincontrol device pointer.
473 * @selector: Group ID.
474 * @configs: Configuration to set.
475 * @num_configs: Number of configurations.
477 * Call function to set configs for each pin in the group.
479 * Return: 0 on success else error code.
481 static int zynqmp_pinconf_group_set(struct pinctrl_dev
*pctldev
,
482 unsigned int selector
,
483 unsigned long *configs
,
484 unsigned int num_configs
)
486 const unsigned int *pins
;
490 zynqmp_pctrl_get_group_pins(pctldev
, selector
, &pins
, &npins
);
491 for (i
= 0; i
< npins
; i
++) {
492 ret
= zynqmp_pinconf_cfg_set(pctldev
, pins
[i
], configs
,
501 static const struct pinconf_ops zynqmp_pinconf_ops
= {
503 .pin_config_get
= zynqmp_pinconf_cfg_get
,
504 .pin_config_set
= zynqmp_pinconf_cfg_set
,
505 .pin_config_group_set
= zynqmp_pinconf_group_set
,
508 static struct pinctrl_desc zynqmp_desc
= {
509 .name
= "zynqmp_pinctrl",
510 .owner
= THIS_MODULE
,
511 .pctlops
= &zynqmp_pctrl_ops
,
512 .pmxops
= &zynqmp_pinmux_ops
,
513 .confops
= &zynqmp_pinconf_ops
,
516 static int zynqmp_pinctrl_get_function_groups(u32 fid
, u32 index
, u16
*groups
)
518 struct zynqmp_pm_query_data qdata
= {0};
519 u32 payload
[PAYLOAD_ARG_CNT
];
522 qdata
.qid
= PM_QID_PINCTRL_GET_FUNCTION_GROUPS
;
526 ret
= zynqmp_pm_query_data(qdata
, payload
);
530 memcpy(groups
, &payload
[1], PINCTRL_GET_FUNC_GROUPS_RESP_LEN
);
535 static int zynqmp_pinctrl_get_func_num_groups(u32 fid
, unsigned int *ngroups
)
537 struct zynqmp_pm_query_data qdata
= {0};
538 u32 payload
[PAYLOAD_ARG_CNT
];
541 qdata
.qid
= PM_QID_PINCTRL_GET_NUM_FUNCTION_GROUPS
;
544 ret
= zynqmp_pm_query_data(qdata
, payload
);
548 *ngroups
= payload
[1];
554 * zynqmp_pinctrl_prepare_func_groups() - prepare function and groups data
555 * @dev: Device pointer.
557 * @func: Function data.
558 * @groups: Groups data.
560 * Query firmware to get group IDs for each function. Firmware returns
561 * group IDs. Based on the group index for the function, group names in
562 * the function are stored. For example, the first group in "eth0" function
563 * is named as "eth0_0" and the second group as "eth0_1" and so on.
565 * Based on the group ID received from the firmware, function stores name of
566 * the group for that group ID. For example, if "eth0" first group ID
567 * is x, groups[x] name will be stored as "eth0_0".
569 * Once done for each function, each function would have its group names
570 * and each group would also have their names.
572 * Return: 0 on success else error code.
574 static int zynqmp_pinctrl_prepare_func_groups(struct device
*dev
, u32 fid
,
575 struct zynqmp_pmux_function
*func
,
576 struct zynqmp_pctrl_group
*groups
)
578 u16 resp
[NUM_GROUPS_PER_RESP
] = {0};
579 const char **fgroups
;
580 int ret
, index
, i
, pin
;
582 unsigned long *used_pins
__free(bitmap
) =
583 bitmap_zalloc(zynqmp_desc
.npins
, GFP_KERNEL
);
588 for (index
= 0; index
< func
->ngroups
; index
+= NUM_GROUPS_PER_RESP
) {
589 ret
= zynqmp_pinctrl_get_function_groups(fid
, index
, resp
);
593 for (i
= 0; i
< NUM_GROUPS_PER_RESP
; i
++) {
594 if (resp
[i
] == NA_GROUP
)
597 if (resp
[i
] == RESERVED_GROUP
)
600 groups
[resp
[i
]].name
= devm_kasprintf(dev
, GFP_KERNEL
,
604 if (!groups
[resp
[i
]].name
)
607 for (pin
= 0; pin
< groups
[resp
[i
]].npins
; pin
++) {
608 if (family_code
== ZYNQMP_FAMILY_CODE
)
609 __set_bit(groups
[resp
[i
]].pins
[pin
], used_pins
);
611 __set_bit((u8
)groups
[resp
[i
]].pins
[pin
] - 1, used_pins
);
616 npins
= bitmap_weight(used_pins
, zynqmp_desc
.npins
);
617 fgroups
= devm_kcalloc(dev
, size_add(func
->ngroups
, npins
),
618 sizeof(*fgroups
), GFP_KERNEL
);
622 for (i
= 0; i
< func
->ngroups
; i
++) {
623 fgroups
[i
] = devm_kasprintf(dev
, GFP_KERNEL
, "%s_%d_grp",
630 for_each_set_bit(pin
, used_pins
, zynqmp_desc
.npins
)
631 fgroups
[i
++] = zynqmp_desc
.pins
[pin
].name
;
633 func
->groups
= fgroups
;
634 func
->ngroups
+= npins
;
639 static void zynqmp_pinctrl_get_function_name(u32 fid
, char *name
)
641 struct zynqmp_pm_query_data qdata
= {0};
642 u32 payload
[PAYLOAD_ARG_CNT
];
644 qdata
.qid
= PM_QID_PINCTRL_GET_FUNCTION_NAME
;
648 * Name of the function is maximum 16 bytes and cannot
649 * accommodate the return value in SMC buffers, hence ignoring
650 * the return value for this specific qid.
652 zynqmp_pm_query_data(qdata
, payload
);
653 memcpy(name
, payload
, PINCTRL_GET_FUNC_NAME_RESP_LEN
);
656 static int zynqmp_pinctrl_get_num_functions(unsigned int *nfuncs
)
658 struct zynqmp_pm_query_data qdata
= {0};
659 u32 payload
[PAYLOAD_ARG_CNT
];
662 qdata
.qid
= PM_QID_PINCTRL_GET_NUM_FUNCTIONS
;
664 ret
= zynqmp_pm_query_data(qdata
, payload
);
668 *nfuncs
= payload
[1];
673 static int zynqmp_pinctrl_get_pin_groups(u32 pin
, u32 index
, u16
*groups
)
675 struct zynqmp_pm_query_data qdata
= {0};
676 u32 payload
[PAYLOAD_ARG_CNT
];
679 qdata
.qid
= PM_QID_PINCTRL_GET_PIN_GROUPS
;
683 ret
= zynqmp_pm_query_data(qdata
, payload
);
687 memcpy(groups
, &payload
[1], PINCTRL_GET_PIN_GROUPS_RESP_LEN
);
692 static void zynqmp_pinctrl_group_add_pin(struct zynqmp_pctrl_group
*group
,
695 group
->pins
[group
->npins
++] = pin
;
699 * zynqmp_pinctrl_create_pin_groups() - assign pins to respective groups
700 * @dev: Device pointer.
701 * @groups: Groups data.
704 * Query firmware to get groups available for the given pin.
705 * Based on the firmware response(group IDs for the pin), add
706 * pin number to the respective group's pin array.
708 * Once all pins are queries, each group would have its number
709 * of pins and pin numbers data.
711 * Return: 0 on success else error code.
713 static int zynqmp_pinctrl_create_pin_groups(struct device
*dev
,
714 struct zynqmp_pctrl_group
*groups
,
717 u16 resp
[NUM_GROUPS_PER_RESP
] = {0};
718 int ret
, i
, index
= 0;
721 ret
= zynqmp_pinctrl_get_pin_groups(pin
, index
, resp
);
725 for (i
= 0; i
< NUM_GROUPS_PER_RESP
; i
++) {
726 if (resp
[i
] == NA_GROUP
)
729 if (resp
[i
] == RESERVED_GROUP
)
732 zynqmp_pinctrl_group_add_pin(&groups
[resp
[i
]], pin
);
734 index
+= NUM_GROUPS_PER_RESP
;
735 } while (index
<= MAX_PIN_GROUPS
);
741 * zynqmp_pinctrl_prepare_group_pins() - prepare each group's pin data
742 * @dev: Device pointer.
743 * @groups: Groups data.
744 * @ngroups: Number of groups.
746 * Prepare pin number and number of pins data for each pins.
748 * Return: 0 on success else error code.
750 static int zynqmp_pinctrl_prepare_group_pins(struct device
*dev
,
751 struct zynqmp_pctrl_group
*groups
,
752 unsigned int ngroups
)
757 for (pin
= 0; pin
< zynqmp_desc
.npins
; pin
++) {
758 ret
= zynqmp_pinctrl_create_pin_groups(dev
, groups
, zynqmp_desc
.pins
[pin
].number
);
767 * zynqmp_pinctrl_prepare_function_info() - prepare function info
768 * @dev: Device pointer.
769 * @pctrl: Pin control driver data.
771 * Query firmware for functions, groups and pin information and
772 * prepare pin control driver data.
774 * Query number of functions and number of function groups (number
775 * of groups in the given function) to allocate required memory buffers
776 * for functions and groups. Once buffers are allocated to store
777 * functions and groups data, query and store required information
778 * (number of groups and group names for each function, number of
779 * pins and pin numbers for each group).
781 * Return: 0 on success else error code.
783 static int zynqmp_pinctrl_prepare_function_info(struct device
*dev
,
784 struct zynqmp_pinctrl
*pctrl
)
786 struct zynqmp_pmux_function
*funcs
;
787 struct zynqmp_pctrl_group
*groups
;
790 ret
= zynqmp_pinctrl_get_num_functions(&pctrl
->nfuncs
);
794 funcs
= devm_kcalloc(dev
, pctrl
->nfuncs
, sizeof(*funcs
), GFP_KERNEL
);
798 for (i
= 0; i
< pctrl
->nfuncs
; i
++) {
799 zynqmp_pinctrl_get_function_name(i
, funcs
[i
].name
);
801 ret
= zynqmp_pinctrl_get_func_num_groups(i
, &funcs
[i
].ngroups
);
805 pctrl
->ngroups
+= funcs
[i
].ngroups
;
808 groups
= devm_kcalloc(dev
, pctrl
->ngroups
, sizeof(*groups
), GFP_KERNEL
);
812 ret
= zynqmp_pinctrl_prepare_group_pins(dev
, groups
, pctrl
->ngroups
);
816 for (i
= 0; i
< pctrl
->nfuncs
; i
++) {
817 ret
= zynqmp_pinctrl_prepare_func_groups(dev
, i
, &funcs
[i
],
823 pctrl
->funcs
= funcs
;
824 pctrl
->groups
= groups
;
829 static int zynqmp_pinctrl_get_num_pins(unsigned int *npins
)
831 struct zynqmp_pm_query_data qdata
= {0};
832 u32 payload
[PAYLOAD_ARG_CNT
];
835 qdata
.qid
= PM_QID_PINCTRL_GET_NUM_PINS
;
837 ret
= zynqmp_pm_query_data(qdata
, payload
);
847 * zynqmp_pinctrl_prepare_pin_desc() - prepare pin description info
848 * @dev: Device pointer.
849 * @zynqmp_pins: Pin information.
850 * @npins: Number of pins.
852 * Query number of pins information from firmware and prepare pin
853 * description containing pin number and pin name.
855 * Return: 0 on success else error code.
857 static int zynqmp_pinctrl_prepare_pin_desc(struct device
*dev
,
858 const struct pinctrl_pin_desc
862 struct pinctrl_pin_desc
*pins
, *pin
;
866 ret
= zynqmp_pinctrl_get_num_pins(npins
);
870 pins
= devm_kcalloc(dev
, *npins
, sizeof(*pins
), GFP_KERNEL
);
874 for (i
= 0; i
< *npins
; i
++) {
877 pin
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s%d",
878 ZYNQMP_PIN_PREFIX
, i
);
888 static int versal_pinctrl_get_attributes(u32 pin_idx
, u32
*response
)
890 struct zynqmp_pm_query_data qdata
= {0};
891 u32 payload
[PAYLOAD_ARG_CNT
];
894 qdata
.qid
= PM_QID_PINCTRL_GET_ATTRIBUTES
;
895 qdata
.arg1
= pin_idx
;
897 ret
= zynqmp_pm_query_data(qdata
, payload
);
901 memcpy(response
, &payload
[1], sizeof(*response
));
906 static int versal_pinctrl_prepare_pin_desc(struct device
*dev
,
907 const struct pinctrl_pin_desc
**zynqmp_pins
,
910 u32 lpd_mio_pins
= 0, attr
, nodetype
;
911 struct pinctrl_pin_desc
*pins
, *pin
;
914 ret
= zynqmp_pm_is_function_supported(PM_QUERY_DATA
, PM_QID_PINCTRL_GET_ATTRIBUTES
);
918 ret
= zynqmp_pinctrl_get_num_pins(npins
);
922 pins
= devm_kzalloc(dev
, sizeof(*pins
) * *npins
, GFP_KERNEL
);
926 for (i
= 0; i
< *npins
; i
++) {
927 ret
= versal_pinctrl_get_attributes(i
, &attr
);
933 nodetype
= FIELD_GET(VERSAL_PINCTRL_ATTR_NODETYPE_MASK
, attr
);
934 if (nodetype
== VERSAL_PINCTRL_NODETYPE_LPD_MIO
) {
935 pin
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s%d",
936 VERSAL_LPD_PIN_PREFIX
, i
);
939 pin
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s%d",
940 VERSAL_PMC_PIN_PREFIX
, i
- lpd_mio_pins
);
952 static int zynqmp_pinctrl_probe(struct platform_device
*pdev
)
954 struct zynqmp_pinctrl
*pctrl
;
957 pctrl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctrl
), GFP_KERNEL
);
961 ret
= zynqmp_pm_get_family_info(&family_code
, &sub_family_code
);
965 if (family_code
== ZYNQMP_FAMILY_CODE
) {
966 ret
= zynqmp_pinctrl_prepare_pin_desc(&pdev
->dev
, &zynqmp_desc
.pins
,
969 ret
= versal_pinctrl_prepare_pin_desc(&pdev
->dev
, &zynqmp_desc
.pins
,
974 dev_err(&pdev
->dev
, "pin desc prepare fail with %d\n", ret
);
978 ret
= zynqmp_pinctrl_prepare_function_info(&pdev
->dev
, pctrl
);
980 dev_err(&pdev
->dev
, "function info prepare fail with %d\n", ret
);
984 pctrl
->pctrl
= devm_pinctrl_register(&pdev
->dev
, &zynqmp_desc
, pctrl
);
985 if (IS_ERR(pctrl
->pctrl
))
986 return PTR_ERR(pctrl
->pctrl
);
988 platform_set_drvdata(pdev
, pctrl
);
993 static const struct of_device_id zynqmp_pinctrl_of_match
[] = {
994 { .compatible
= "xlnx,zynqmp-pinctrl" },
995 { .compatible
= "xlnx,versal-pinctrl" },
998 MODULE_DEVICE_TABLE(of
, zynqmp_pinctrl_of_match
);
1000 static struct platform_driver zynqmp_pinctrl_driver
= {
1002 .name
= "zynqmp-pinctrl",
1003 .of_match_table
= zynqmp_pinctrl_of_match
,
1005 .probe
= zynqmp_pinctrl_probe
,
1007 module_platform_driver(zynqmp_pinctrl_driver
);
1009 MODULE_AUTHOR("Sai Krishna Potthuri <lakshmi.sai.krishna.potthuri@xilinx.com>");
1010 MODULE_DESCRIPTION("ZynqMP Pin Controller Driver");
1011 MODULE_LICENSE("GPL v2");