#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/dev_printk.h>
#include <linux/err.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/i2c-of-prober.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/stddef.h>
static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, const char *type)
{
struct device_node *node __free(device_node) = of_find_node_by_name(NULL, type);
if (!node) {
dev_err(dev, "Could not find %s device node\n", type);
return NULL;
}
struct device_node *i2c_node __free(device_node) = of_get_parent(node);
if (!of_node_name_eq(i2c_node, "i2c")) {
dev_err(dev, "%s device isn't on I2C bus\n", type);
return NULL;
}
if (!of_device_is_available(i2c_node)) {
dev_err(dev, "I2C controller not available\n");
return NULL;
}
return no_free_ptr(i2c_node);
}
static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node)
{
int ret;
dev_dbg(dev, "Enabling %pOF\n", node);
struct of_changeset *ocs __free(kfree) = kzalloc_obj(*ocs);
if (!ocs)
return -ENOMEM;
of_changeset_init(ocs);
ret = of_changeset_update_prop_string(ocs, node, "status", "okay");
if (ret)
return ret;
ret = of_changeset_apply(ocs);
if (ret) {
of_changeset_destroy(ocs);
} else {
void *ptr __always_unused = no_free_ptr(ocs);
}
return ret;
}
static const struct i2c_of_probe_ops i2c_of_probe_dummy_ops;
int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx)
{
const struct i2c_of_probe_ops *ops;
const char *type;
struct i2c_adapter *i2c;
int ret;
ops = cfg->ops ?: &i2c_of_probe_dummy_ops;
type = cfg->type;
struct device_node *i2c_node __free(device_node) = i2c_of_probe_get_i2c_node(dev, type);
if (!i2c_node)
return -ENODEV;
for_each_child_of_node_with_prefix(i2c_node, node, type)
if (of_device_is_available(node))
return 0;
i2c = of_get_i2c_adapter_by_node(i2c_node);
if (!i2c)
return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n");
ret = 0;
if (ops->enable)
ret = ops->enable(dev, i2c_node, ctx);
if (ret)
goto out_put_i2c_adapter;
for_each_child_of_node_with_prefix(i2c_node, node, type) {
union i2c_smbus_data data;
u32 addr;
if (of_property_read_u32(node, "reg", &addr))
continue;
if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0)
continue;
if (ops->cleanup_early)
ops->cleanup_early(dev, ctx);
ret = i2c_of_probe_enable_node(dev, node);
break;
}
if (ops->cleanup)
ops->cleanup(dev, ctx);
out_put_i2c_adapter:
i2c_put_adapter(i2c);
return ret;
}
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_component, "I2C_OF_PROBER");
static int i2c_of_probe_simple_get_supply(struct device *dev, struct device_node *node,
struct i2c_of_probe_simple_ctx *ctx)
{
const char *supply_name;
struct regulator *supply;
supply_name = ctx->opts->supply_name;
if (!supply_name)
return 0;
supply = of_regulator_get_optional(dev, node, supply_name);
if (IS_ERR(supply)) {
return dev_err_probe(dev, PTR_ERR(supply),
"Failed to get regulator supply \"%s\" from %pOF\n",
supply_name, node);
}
ctx->supply = supply;
return 0;
}
static void i2c_of_probe_simple_put_supply(struct i2c_of_probe_simple_ctx *ctx)
{
regulator_put(ctx->supply);
ctx->supply = NULL;
}
static int i2c_of_probe_simple_enable_regulator(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
{
int ret;
if (!ctx->supply)
return 0;
dev_dbg(dev, "Enabling regulator supply \"%s\"\n", ctx->opts->supply_name);
ret = regulator_enable(ctx->supply);
if (ret)
return ret;
if (ctx->opts->post_power_on_delay_ms)
msleep(ctx->opts->post_power_on_delay_ms);
return 0;
}
static void i2c_of_probe_simple_disable_regulator(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
{
if (!ctx->supply)
return;
dev_dbg(dev, "Disabling regulator supply \"%s\"\n", ctx->opts->supply_name);
regulator_disable(ctx->supply);
}
static int i2c_of_probe_simple_get_gpiod(struct device *dev, struct device_node *node,
struct i2c_of_probe_simple_ctx *ctx)
{
struct fwnode_handle *fwnode = of_fwnode_handle(node);
struct gpio_desc *gpiod;
const char *con_id;
if (!ctx->opts->gpio_name)
return 0;
if (!ctx->opts->gpio_name[0])
con_id = NULL;
else
con_id = ctx->opts->gpio_name;
gpiod = fwnode_gpiod_get_index(fwnode, con_id, 0, GPIOD_ASIS, "i2c-of-prober");
if (IS_ERR(gpiod))
return PTR_ERR(gpiod);
ctx->gpiod = gpiod;
return 0;
}
static void i2c_of_probe_simple_put_gpiod(struct i2c_of_probe_simple_ctx *ctx)
{
gpiod_put(ctx->gpiod);
ctx->gpiod = NULL;
}
static int i2c_of_probe_simple_set_gpio(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
{
int ret;
if (!ctx->gpiod)
return 0;
dev_dbg(dev, "Configuring GPIO\n");
ret = gpiod_direction_output(ctx->gpiod, ctx->opts->gpio_assert_to_enable);
if (ret)
return ret;
if (ctx->opts->post_gpio_config_delay_ms)
msleep(ctx->opts->post_gpio_config_delay_ms);
return 0;
}
static void i2c_of_probe_simple_disable_gpio(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
{
gpiod_set_value(ctx->gpiod, !ctx->opts->gpio_assert_to_enable);
}
int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node, void *data)
{
struct i2c_of_probe_simple_ctx *ctx = data;
struct device_node *node;
const char *compat;
int ret;
dev_dbg(dev, "Requesting resources for components under I2C bus %pOF\n", bus_node);
if (!ctx || !ctx->opts)
return -EINVAL;
compat = ctx->opts->res_node_compatible;
if (!compat)
return -EINVAL;
node = of_get_compatible_child(bus_node, compat);
if (!node)
return dev_err_probe(dev, -ENODEV, "No device compatible with \"%s\" found\n",
compat);
ret = i2c_of_probe_simple_get_supply(dev, node, ctx);
if (ret)
goto out_put_node;
ret = i2c_of_probe_simple_get_gpiod(dev, node, ctx);
if (ret)
goto out_put_supply;
ret = i2c_of_probe_simple_enable_regulator(dev, ctx);
if (ret)
goto out_put_gpiod;
ret = i2c_of_probe_simple_set_gpio(dev, ctx);
if (ret)
goto out_disable_regulator;
return 0;
out_disable_regulator:
i2c_of_probe_simple_disable_regulator(dev, ctx);
out_put_gpiod:
i2c_of_probe_simple_put_gpiod(ctx);
out_put_supply:
i2c_of_probe_simple_put_supply(ctx);
out_put_node:
of_node_put(node);
return ret;
}
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, "I2C_OF_PROBER");
void i2c_of_probe_simple_cleanup_early(struct device *dev, void *data)
{
struct i2c_of_probe_simple_ctx *ctx = data;
i2c_of_probe_simple_put_gpiod(ctx);
}
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup_early, "I2C_OF_PROBER");
void i2c_of_probe_simple_cleanup(struct device *dev, void *data)
{
struct i2c_of_probe_simple_ctx *ctx = data;
i2c_of_probe_simple_disable_gpio(dev, ctx);
i2c_of_probe_simple_put_gpiod(ctx);
i2c_of_probe_simple_disable_regulator(dev, ctx);
i2c_of_probe_simple_put_supply(ctx);
}
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup, "I2C_OF_PROBER");
struct i2c_of_probe_ops i2c_of_probe_simple_ops = {
.enable = i2c_of_probe_simple_enable,
.cleanup_early = i2c_of_probe_simple_cleanup_early,
.cleanup = i2c_of_probe_simple_cleanup,
};
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, "I2C_OF_PROBER");