#include <linux/device.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/property.h>
#include <linux/slab.h>
#include "class.h"
#include "retimer.h"
static int retimer_fwnode_match(struct device *dev, const void *fwnode)
{
return is_typec_retimer(dev) && device_match_fwnode(dev, fwnode);
}
static void *typec_retimer_match(const struct fwnode_handle *fwnode, const char *id, void *data)
{
struct device *dev;
if (id && !fwnode_property_present(fwnode, id))
return NULL;
dev = class_find_device(&retimer_class, NULL, fwnode,
retimer_fwnode_match);
return dev ? to_typec_retimer(dev) : ERR_PTR(-EPROBE_DEFER);
}
struct typec_retimer *fwnode_typec_retimer_get(struct fwnode_handle *fwnode)
{
struct typec_retimer *retimer;
retimer = fwnode_connection_find_match(fwnode, "retimer-switch", NULL, typec_retimer_match);
if (!IS_ERR_OR_NULL(retimer))
WARN_ON(!try_module_get(retimer->dev.parent->driver->owner));
return retimer;
}
EXPORT_SYMBOL_GPL(fwnode_typec_retimer_get);
void typec_retimer_put(struct typec_retimer *retimer)
{
if (!IS_ERR_OR_NULL(retimer)) {
module_put(retimer->dev.parent->driver->owner);
put_device(&retimer->dev);
}
}
EXPORT_SYMBOL_GPL(typec_retimer_put);
int typec_retimer_set(struct typec_retimer *retimer, struct typec_retimer_state *state)
{
if (IS_ERR_OR_NULL(retimer))
return 0;
return retimer->set(retimer, state);
}
EXPORT_SYMBOL_GPL(typec_retimer_set);
static void typec_retimer_release(struct device *dev)
{
kfree(to_typec_retimer(dev));
}
const struct device_type typec_retimer_dev_type = {
.name = "typec_retimer",
.release = typec_retimer_release,
};
struct typec_retimer *
typec_retimer_register(struct device *parent, const struct typec_retimer_desc *desc)
{
struct typec_retimer *retimer;
int ret;
if (!desc || !desc->set)
return ERR_PTR(-EINVAL);
retimer = kzalloc(sizeof(*retimer), GFP_KERNEL);
if (!retimer)
return ERR_PTR(-ENOMEM);
retimer->set = desc->set;
device_initialize(&retimer->dev);
retimer->dev.parent = parent;
retimer->dev.fwnode = desc->fwnode;
retimer->dev.class = &retimer_class;
retimer->dev.type = &typec_retimer_dev_type;
retimer->dev.driver_data = desc->drvdata;
dev_set_name(&retimer->dev, "%s-retimer",
desc->name ? desc->name : dev_name(parent));
ret = device_add(&retimer->dev);
if (ret) {
dev_err(parent, "failed to register retimer (%d)\n", ret);
put_device(&retimer->dev);
return ERR_PTR(ret);
}
return retimer;
}
EXPORT_SYMBOL_GPL(typec_retimer_register);
void typec_retimer_unregister(struct typec_retimer *retimer)
{
if (!IS_ERR_OR_NULL(retimer))
device_unregister(&retimer->dev);
}
EXPORT_SYMBOL_GPL(typec_retimer_unregister);
void *typec_retimer_get_drvdata(struct typec_retimer *retimer)
{
return dev_get_drvdata(&retimer->dev);
}
EXPORT_SYMBOL_GPL(typec_retimer_get_drvdata);
struct class retimer_class = {
.name = "retimer",
}