#include <linux/export.h>
#include <linux/fwnode.h>
#include <linux/phy/phy-common-props.h>
#include <linux/printk.h>
#include <linux/property.h>
#include <linux/slab.h>
static int fwnode_get_u32_prop_for_name(struct fwnode_handle *fwnode,
const char *name,
const char *props_title,
const char *names_title,
unsigned int default_val,
unsigned int *val)
{
int err, n_props, n_names, idx;
u32 *props;
if (!name) {
pr_err("Lookup key inside \"%s\" is mandatory\n", names_title);
return -EINVAL;
}
n_props = fwnode_property_count_u32(fwnode, props_title);
if (n_props <= 0) {
*val = default_val;
return 0;
}
n_names = fwnode_property_string_array_count(fwnode, names_title);
if (n_names >= 0 && n_props != n_names) {
pr_err("%pfw mismatch between \"%s\" and \"%s\" property count (%d vs %d)\n",
fwnode, props_title, names_title, n_props, n_names);
return -EINVAL;
}
idx = fwnode_property_match_string(fwnode, names_title, name);
if (idx < 0)
idx = fwnode_property_match_string(fwnode, names_title, "default");
if (idx < 0 && n_props != 1) {
pr_err("%pfw \"%s \" property has %d elements, but cannot find \"%s\" in \"%s\" and there is no default value\n",
fwnode, props_title, n_props, name, names_title);
return -EINVAL;
}
if (n_props == 1) {
err = fwnode_property_read_u32(fwnode, props_title, val);
if (err)
return err;
return 0;
}
props = kcalloc(n_props, sizeof(*props), GFP_KERNEL);
if (!props)
return -ENOMEM;
err = fwnode_property_read_u32_array(fwnode, props_title, props, n_props);
if (err >= 0)
*val = props[idx];
kfree(props);
return err;
}
static int phy_get_polarity_for_mode(struct fwnode_handle *fwnode,
const char *mode_name,
unsigned int supported,
unsigned int default_val,
const char *polarity_prop,
const char *names_prop,
unsigned int *val)
{
int err;
err = fwnode_get_u32_prop_for_name(fwnode, mode_name, polarity_prop,
names_prop, default_val, val);
if (err)
return err;
if (!(supported & BIT(*val))) {
pr_err("%d is not a supported value for %pfw '%s' element '%s'\n",
*val, fwnode, polarity_prop, mode_name);
err = -EOPNOTSUPP;
}
return err;
}
int __must_check phy_get_rx_polarity(struct fwnode_handle *fwnode,
const char *mode_name,
unsigned int supported,
unsigned int default_val,
unsigned int *val)
{
return phy_get_polarity_for_mode(fwnode, mode_name, supported,
default_val, "rx-polarity",
"rx-polarity-names", val);
}
EXPORT_SYMBOL_GPL(phy_get_rx_polarity);
int __must_check phy_get_tx_polarity(struct fwnode_handle *fwnode,
const char *mode_name, unsigned int supported,
unsigned int default_val, unsigned int *val)
{
return phy_get_polarity_for_mode(fwnode, mode_name, supported,
default_val, "tx-polarity",
"tx-polarity-names", val);
}
EXPORT_SYMBOL_GPL(phy_get_tx_polarity);
int __must_check phy_get_manual_rx_polarity(struct fwnode_handle *fwnode,
const char *mode_name,
unsigned int *val)
{
return phy_get_rx_polarity(fwnode, mode_name,
BIT(PHY_POL_NORMAL) | BIT(PHY_POL_INVERT),
PHY_POL_NORMAL, val);
}
EXPORT_SYMBOL_GPL(phy_get_manual_rx_polarity);
int __must_check phy_get_manual_tx_polarity(struct fwnode_handle *fwnode,
const char *mode_name,
unsigned int *val)
{
return phy_get_tx_polarity(fwnode, mode_name,
BIT(PHY_POL_NORMAL) | BIT(PHY_POL_INVERT),
PHY_POL_NORMAL, val);
}
EXPORT_SYMBOL_GPL(phy_get_manual_tx_polarity);