drivers/media/v4l2-core/v4l2-i2c.c

Source file repositories/reference/linux-study-clean/drivers/media/v4l2-core/v4l2-i2c.c

File Facts

System
Linux kernel
Corpus path
drivers/media/v4l2-core/v4l2-i2c.c
Extension
.c
Size
5082 bytes
Lines
186
Domain
Driver Families
Bucket
drivers/media
Inferred role
Driver Families: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * v4l2-i2c - I2C helpers for Video4Linux2
 */

#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/property.h>
#include <media/v4l2-common.h>
#include <media/v4l2-device.h>

void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);

	/*
	 * We need to unregister the i2c client
	 * explicitly. We cannot rely on
	 * i2c_del_adapter to always unregister
	 * clients for us, since if the i2c bus is a
	 * platform bus, then it is never deleted.
	 *
	 * Device tree or ACPI based devices must not
	 * be unregistered as they have not been
	 * registered by us, and would not be
	 * re-created by just probing the V4L2 driver.
	 */
	if (client && !dev_fwnode(&client->dev))
		i2c_unregister_device(client);
}

void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd,
			      struct i2c_client *client,
			      const char *devname, const char *postfix)
{
	if (!devname)
		devname = client->dev.driver->name;
	if (!postfix)
		postfix = "";

	snprintf(sd->name, sizeof(sd->name), "%s%s %d-%04x", devname, postfix,
		 i2c_adapter_id(client->adapter), client->addr);
}
EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_set_name);

void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
			  const struct v4l2_subdev_ops *ops)
{
	v4l2_subdev_init(sd, ops);
	sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
	/* the owner is the same as the i2c_client's driver owner */
	sd->owner = client->dev.driver->owner;
	sd->dev = &client->dev;
	/* i2c_client and v4l2_subdev point to one another */
	v4l2_set_subdevdata(sd, client);
	i2c_set_clientdata(client, sd);
	v4l2_i2c_subdev_set_name(sd, client, NULL, NULL);
}
EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);

/* Load an i2c sub-device. */
struct v4l2_subdev
*v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
			   struct i2c_adapter *adapter,
			   struct i2c_board_info *info,
			   const unsigned short *probe_addrs)
{
	struct v4l2_subdev *sd = NULL;
	struct i2c_client *client;

	if (!v4l2_dev)
		return NULL;

	request_module(I2C_MODULE_PREFIX "%s", info->type);

	/* Create the i2c client */
	if (info->addr == 0 && probe_addrs)
		client = i2c_new_scanned_device(adapter, info, probe_addrs,
						NULL);
	else
		client = i2c_new_client_device(adapter, info);

	/*
	 * Note: by loading the module first we are certain that c->driver
	 * will be set if the driver was found. If the module was not loaded
	 * first, then the i2c core tries to delay-load the module for us,
	 * and then c->driver is still NULL until the module is finally
	 * loaded. This delay-load mechanism doesn't work if other drivers
	 * want to use the i2c device, so explicitly loading the module
	 * is the best alternative.

Annotation

Implementation Notes