// SPDX-License-Identifier: GPL-2.0
/*
* System Control and Management Interface (SCMI) Pinctrl Protocol
*
* Copyright (C) 2024 EPAM
* Copyright 2024 NXP
*/
#include <asm/byteorder.h>
#include <linux/bits.h>
#include <linux/bitfield.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/scmi_protocol.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/types.h>
#include "common.h"
#include "protocols.h"
/* Updated only after ALL the mandatory features for that version are merged */
#define SCMI_PROTOCOL_SUPPORTED_VERSION 0x10000
#define GET_GROUPS_NR(x) le32_get_bits((x), GENMASK(31, 16))
#define GET_PINS_NR(x) le32_get_bits((x), GENMASK(15, 0))
#define GET_FUNCTIONS_NR(x) le32_get_bits((x), GENMASK(15, 0))
#define EXT_NAME_FLAG(x) le32_get_bits((x), BIT(31))
#define NUM_ELEMS(x) le32_get_bits((x), GENMASK(15, 0))
#define REMAINING(x) le32_get_bits((x), GENMASK(31, 16))
#define RETURNED(x) le32_get_bits((x), GENMASK(11, 0))
#define CONFIG_FLAG_MASK GENMASK(19, 18)
#define SELECTOR_MASK GENMASK(17, 16)
#define SKIP_CONFIGS_MASK GENMASK(15, 8)
#define CONFIG_TYPE_MASK GENMASK(7, 0)
enum scmi_pinctrl_protocol_cmd {
PINCTRL_ATTRIBUTES = 0x3,
PINCTRL_LIST_ASSOCIATIONS = 0x4,
PINCTRL_SETTINGS_GET = 0x5,
PINCTRL_SETTINGS_CONFIGURE = 0x6,
PINCTRL_REQUEST = 0x7,
PINCTRL_RELEASE = 0x8,
PINCTRL_NAME_GET = 0x9,
PINCTRL_SET_PERMISSIONS = 0xa,
};
struct scmi_msg_settings_conf {
__le32 identifier;
__le32 function_id;
__le32 attributes;
__le32 configs[];
};
struct scmi_msg_settings_get {
__le32 identifier;
__le32 attributes;
};
struct scmi_resp_settings_get {
__le32 function_selected;
__le32 num_configs;
__le32 configs[];
};
struct scmi_msg_pinctrl_protocol_attributes {
__le32 attributes_low;
__le32 attributes_high;
};
struct scmi_msg_pinctrl_attributes {
__le32 identifier;
__le32 flags;
};
struct scmi_resp_pinctrl_attributes {
__le32 attributes;
u8 name[SCMI_SHORT_NAME_MAX_SIZE];
};
struct scmi_msg_pinctrl_list_assoc {
__le32 identifier;
__le32 flags;
__le32 index;
};
struct scmi_resp_pinctrl_list_assoc {
__le32 flags;
__le16 array[];
};
struct scmi_msg_request {
__le32 identifier;
__le32 flags;
};
struct scmi_group_info {
char name[SCMI_MAX_STR_SIZE];
bool present;
u32 *group_pins;
u32 nr_pins;
};
struct scmi_function_info {
char name[SCMI_MAX_STR_SIZE];
bool present;
u32 *groups;
u32 nr_groups;
};
struct scmi_pin_info {
char name[SCMI_MAX_STR_SIZE];
bool present;
};
struct scmi_pinctrl_info {
u32 version;
int nr_groups;
int nr_functions;
int nr_pins;
struct scmi_group_info *groups;
struct scmi_function_info *functions;
struct scmi_pin_info *pins;
};
static int scmi_pinctrl_attributes_get(const struct scmi_protocol_handle *ph,
struct scmi_pinctrl_info *pi)
{
int ret;
struct scmi_xfer *t;
struct scmi_msg_pinctrl_protocol_attributes *attr;
ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0, sizeof(*attr), &t);
if (ret)
return ret;
attr = t->rx.buf;
ret = ph->xops->do_xfer(ph, t);
if (!ret) {
pi->nr_functions = GET_FUNCTIONS_NR(attr->attributes_high);
pi->nr_groups = GET_GROUPS_NR(attr->attributes_low);
pi->nr_pins = GET_PINS_NR(attr->attributes_low);
if (pi->nr_pins == 0) {
dev_warn(ph->dev, "returned zero pins\n");
ret = -EINVAL;
}
}
ph->xops->xfer_put(ph, t);
return ret;
}
static int scmi_pinctrl_count_get(const struct scmi_protocol_handle *ph,
enum scmi_pinctrl_selector_type type)
{
struct scmi_pinctrl_info *pi = ph->get_priv(ph);
switch (type) {
case PIN_TYPE:
return pi->nr_pins;
case GROUP_TYPE:
return pi->nr_groups;
case FUNCTION_TYPE:
return pi->nr_functions;
default:
return -EINVAL;
}
}
static int scmi_pinctrl_validate_id(const struct scmi_protocol_handle *ph,
u32 selector,
enum scmi_pinctrl_selector_type type)
{
int value;
value = scmi_pinctrl_count_get(ph, type);
if (value < 0)
return value;
if (selector >= value || value == 0)
return -EINVAL;
return 0;
}
static int scmi_pinctrl_attributes(const struct scmi_protocol_handle *ph,
enum scmi_pinctrl_selector_type type,
u32 selector, char *name,
u32 *n_elems)
{
int ret;
struct scmi_xfer *t;
struct scmi_msg_pinctrl_attributes *tx;
struct scmi_resp_pinctrl_attributes *rx;
bool ext_name_flag;
if (!name)