/* SPDX-License-Identifier: GPL-2.0-only */1/*2* V4L2 asynchronous subdevice registration API3*4* Copyright (C) 2012-2013, Guennadi Liakhovetski <[email protected]>5*/67#ifndef V4L2_ASYNC_H8#define V4L2_ASYNC_H910#include <linux/list.h>11#include <linux/mutex.h>1213struct dentry;14struct device;15struct device_node;16struct v4l2_device;17struct v4l2_subdev;18struct v4l2_async_notifier;1920/**21* enum v4l2_async_match_type - type of asynchronous subdevice logic to be used22* in order to identify a match23*24* @V4L2_ASYNC_MATCH_TYPE_I2C: Match will check for I2C adapter ID and address25* @V4L2_ASYNC_MATCH_TYPE_FWNODE: Match will use firmware node26*27* This enum is used by the asynchronous connection logic to define the28* algorithm that will be used to match an asynchronous device.29*/30enum v4l2_async_match_type {31V4L2_ASYNC_MATCH_TYPE_I2C,32V4L2_ASYNC_MATCH_TYPE_FWNODE,33};3435/**36* struct v4l2_async_match_desc - async connection match information37*38* @type: type of match that will be used39* @fwnode: pointer to &struct fwnode_handle to be matched.40* Used if @match_type is %V4L2_ASYNC_MATCH_TYPE_FWNODE.41* @i2c: embedded struct with I2C parameters to be matched.42* Both @match.i2c.adapter_id and @match.i2c.address43* should be matched.44* Used if @match_type is %V4L2_ASYNC_MATCH_TYPE_I2C.45* @i2c.adapter_id:46* I2C adapter ID to be matched.47* Used if @match_type is %V4L2_ASYNC_MATCH_TYPE_I2C.48* @i2c.address:49* I2C address to be matched.50* Used if @match_type is %V4L2_ASYNC_MATCH_TYPE_I2C.51*/52struct v4l2_async_match_desc {53enum v4l2_async_match_type type;54union {55struct fwnode_handle *fwnode;56struct {57int adapter_id;58unsigned short address;59} i2c;60};61};6263/**64* struct v4l2_async_connection - sub-device connection descriptor, as known to65* a bridge66*67* @match: struct of match type and per-bus type matching data sets68* @notifier: the async notifier the connection is related to69* @asc_entry: used to add struct v4l2_async_connection objects to the70* notifier @waiting_list or @done_list71* @asc_subdev_entry: entry in struct v4l2_async_subdev.asc_list list72* @sd: the related sub-device73*74* When this struct is used as a member in a driver specific struct, the driver75* specific struct shall contain the &struct v4l2_async_connection as its first76* member.77*/78struct v4l2_async_connection {79struct v4l2_async_match_desc match;80struct v4l2_async_notifier *notifier;81struct list_head asc_entry;82struct list_head asc_subdev_entry;83struct v4l2_subdev *sd;84};8586/**87* struct v4l2_async_notifier_operations - Asynchronous V4L2 notifier operations88* @bound: a sub-device has been bound by the given connection89* @complete: All connections have been bound successfully. The complete90* callback is only executed for the root notifier.91* @unbind: a subdevice is leaving92* @destroy: the asc is about to be freed93*/94struct v4l2_async_notifier_operations {95int (*bound)(struct v4l2_async_notifier *notifier,96struct v4l2_subdev *subdev,97struct v4l2_async_connection *asc);98int (*complete)(struct v4l2_async_notifier *notifier);99void (*unbind)(struct v4l2_async_notifier *notifier,100struct v4l2_subdev *subdev,101struct v4l2_async_connection *asc);102void (*destroy)(struct v4l2_async_connection *asc);103};104105/**106* struct v4l2_async_notifier - v4l2_device notifier data107*108* @ops: notifier operations109* @v4l2_dev: v4l2_device of the root notifier, NULL otherwise110* @sd: sub-device that registered the notifier, NULL otherwise111* @parent: parent notifier112* @waiting_list: list of struct v4l2_async_connection, waiting for their113* drivers114* @done_list: list of struct v4l2_subdev, already probed115* @notifier_entry: member in a global list of notifiers116*/117struct v4l2_async_notifier {118const struct v4l2_async_notifier_operations *ops;119struct v4l2_device *v4l2_dev;120struct v4l2_subdev *sd;121struct v4l2_async_notifier *parent;122struct list_head waiting_list;123struct list_head done_list;124struct list_head notifier_entry;125};126127/**128* struct v4l2_async_subdev_endpoint - Entry in sub-device's fwnode list129*130* @async_subdev_endpoint_entry: An entry in async_subdev_endpoint_list of131* &struct v4l2_subdev132* @endpoint: Endpoint fwnode agains which to match the sub-device133*/134struct v4l2_async_subdev_endpoint {135struct list_head async_subdev_endpoint_entry;136struct fwnode_handle *endpoint;137};138139/**140* v4l2_async_debug_init - Initialize debugging tools.141*142* @debugfs_dir: pointer to the parent debugfs &struct dentry143*/144void v4l2_async_debug_init(struct dentry *debugfs_dir);145146/**147* v4l2_async_nf_init - Initialize a notifier.148*149* @notifier: pointer to &struct v4l2_async_notifier150* @v4l2_dev: pointer to &struct v4l2_device151*152* This function initializes the notifier @asc_entry. It must be called153* before adding a subdevice to a notifier, using one of:154* v4l2_async_nf_add_fwnode_remote(),155* v4l2_async_nf_add_fwnode() or156* v4l2_async_nf_add_i2c().157*/158void v4l2_async_nf_init(struct v4l2_async_notifier *notifier,159struct v4l2_device *v4l2_dev);160161/**162* v4l2_async_subdev_nf_init - Initialize a sub-device notifier.163*164* @notifier: pointer to &struct v4l2_async_notifier165* @sd: pointer to &struct v4l2_subdev166*167* This function initializes the notifier @asc_list. It must be called168* before adding a subdevice to a notifier, using one of:169* v4l2_async_nf_add_fwnode_remote(), v4l2_async_nf_add_fwnode() or170* v4l2_async_nf_add_i2c().171*/172void v4l2_async_subdev_nf_init(struct v4l2_async_notifier *notifier,173struct v4l2_subdev *sd);174175struct v4l2_async_connection *176__v4l2_async_nf_add_fwnode(struct v4l2_async_notifier *notifier,177struct fwnode_handle *fwnode,178unsigned int asc_struct_size);179/**180* v4l2_async_nf_add_fwnode - Allocate and add a fwnode async181* subdev to the notifier's master asc_list.182*183* @notifier: pointer to &struct v4l2_async_notifier184* @fwnode: fwnode handle of the sub-device to be matched, pointer to185* &struct fwnode_handle186* @type: Type of the driver's async sub-device or connection struct. The187* &struct v4l2_async_connection shall be the first member of the188* driver's async struct, i.e. both begin at the same memory address.189*190* Allocate a fwnode-matched asc of size asc_struct_size, and add it to the191* notifiers @asc_list. The function also gets a reference of the fwnode which192* is released later at notifier cleanup time.193*/194#define v4l2_async_nf_add_fwnode(notifier, fwnode, type) \195((type *)__v4l2_async_nf_add_fwnode(notifier, fwnode, sizeof(type)))196197struct v4l2_async_connection *198__v4l2_async_nf_add_fwnode_remote(struct v4l2_async_notifier *notif,199struct fwnode_handle *endpoint,200unsigned int asc_struct_size);201/**202* v4l2_async_nf_add_fwnode_remote - Allocate and add a fwnode203* remote async subdev to the204* notifier's master asc_list.205*206* @notifier: pointer to &struct v4l2_async_notifier207* @ep: local endpoint pointing to the remote connection to be matched,208* pointer to &struct fwnode_handle209* @type: Type of the driver's async connection struct. The &struct210* v4l2_async_connection shall be the first member of the driver's async211* connection struct, i.e. both begin at the same memory address.212*213* Gets the remote endpoint of a given local endpoint, set it up for fwnode214* matching and adds the async connection to the notifier's @asc_list. The215* function also gets a reference of the fwnode which is released later at216* notifier cleanup time.217*218* This is just like v4l2_async_nf_add_fwnode(), but with the219* exception that the fwnode refers to a local endpoint, not the remote one.220*/221#define v4l2_async_nf_add_fwnode_remote(notifier, ep, type) \222((type *)__v4l2_async_nf_add_fwnode_remote(notifier, ep, sizeof(type)))223224struct v4l2_async_connection *225__v4l2_async_nf_add_i2c(struct v4l2_async_notifier *notifier,226int adapter_id, unsigned short address,227unsigned int asc_struct_size);228/**229* v4l2_async_nf_add_i2c - Allocate and add an i2c async230* subdev to the notifier's master asc_list.231*232* @notifier: pointer to &struct v4l2_async_notifier233* @adapter: I2C adapter ID to be matched234* @address: I2C address of connection to be matched235* @type: Type of the driver's async connection struct. The &struct236* v4l2_async_connection shall be the first member of the driver's async237* connection struct, i.e. both begin at the same memory address.238*239* Same as v4l2_async_nf_add_fwnode() but for I2C matched240* connections.241*/242#define v4l2_async_nf_add_i2c(notifier, adapter, address, type) \243((type *)__v4l2_async_nf_add_i2c(notifier, adapter, address, \244sizeof(type)))245246/**247* v4l2_async_subdev_endpoint_add - Add an endpoint fwnode to async sub-device248* matching list249*250* @sd: the sub-device251* @fwnode: the endpoint fwnode to match252*253* Add a fwnode to the async sub-device's matching list. This allows registering254* multiple async sub-devices from a single device.255*256* Note that calling v4l2_subdev_cleanup() as part of the sub-device's cleanup257* if endpoints have been added to the sub-device's fwnode matching list.258*259* Returns an error on failure, 0 on success.260*/261int v4l2_async_subdev_endpoint_add(struct v4l2_subdev *sd,262struct fwnode_handle *fwnode);263264/**265* v4l2_async_connection_unique - return a unique &struct v4l2_async_connection266* for a sub-device267* @sd: the sub-device268*269* Return an async connection for a sub-device, when there is a single270* one only.271*/272struct v4l2_async_connection *273v4l2_async_connection_unique(struct v4l2_subdev *sd);274275/**276* v4l2_async_nf_register - registers a subdevice asynchronous notifier277*278* @notifier: pointer to &struct v4l2_async_notifier279*/280int v4l2_async_nf_register(struct v4l2_async_notifier *notifier);281282/**283* v4l2_async_nf_unregister - unregisters a subdevice284* asynchronous notifier285*286* @notifier: pointer to &struct v4l2_async_notifier287*/288void v4l2_async_nf_unregister(struct v4l2_async_notifier *notifier);289290/**291* v4l2_async_nf_cleanup - clean up notifier resources292* @notifier: the notifier the resources of which are to be cleaned up293*294* Release memory resources related to a notifier, including the async295* connections allocated for the purposes of the notifier but not the notifier296* itself. The user is responsible for calling this function to clean up the297* notifier after calling v4l2_async_nf_add_fwnode_remote(),298* v4l2_async_nf_add_fwnode() or v4l2_async_nf_add_i2c().299*300* There is no harm from calling v4l2_async_nf_cleanup() in other301* cases as long as its memory has been zeroed after it has been302* allocated.303*/304void v4l2_async_nf_cleanup(struct v4l2_async_notifier *notifier);305306/**307* v4l2_async_register_subdev - registers a sub-device to the asynchronous308* subdevice framework309*310* @sd: pointer to &struct v4l2_subdev311*/312#define v4l2_async_register_subdev(sd) \313__v4l2_async_register_subdev(sd, THIS_MODULE)314int __v4l2_async_register_subdev(struct v4l2_subdev *sd, struct module *module);315316/**317* v4l2_async_register_subdev_sensor - registers a sensor sub-device to the318* asynchronous sub-device framework and319* parse set up common sensor related320* devices321*322* @sd: pointer to struct &v4l2_subdev323*324* This function is just like v4l2_async_register_subdev() with the exception325* that calling it will also parse firmware interfaces for remote references326* using v4l2_async_nf_parse_fwnode_sensor() and registers the327* async sub-devices. The sub-device is similarly unregistered by calling328* v4l2_async_unregister_subdev().329*330* While registered, the subdev module is marked as in-use.331*332* An error is returned if the module is no longer loaded on any attempts333* to register it.334*/335int __must_check336v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd);337338/**339* v4l2_async_unregister_subdev - unregisters a sub-device to the asynchronous340* subdevice framework341*342* @sd: pointer to &struct v4l2_subdev343*/344void v4l2_async_unregister_subdev(struct v4l2_subdev *sd);345#endif346347348