Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * v4l2-i2c - I2C helpers for Video4Linux2
0004  */
0005 
0006 #include <linux/i2c.h>
0007 #include <linux/module.h>
0008 #include <media/v4l2-common.h>
0009 #include <media/v4l2-device.h>
0010 
0011 void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd)
0012 {
0013     struct i2c_client *client = v4l2_get_subdevdata(sd);
0014 
0015     /*
0016      * We need to unregister the i2c client
0017      * explicitly. We cannot rely on
0018      * i2c_del_adapter to always unregister
0019      * clients for us, since if the i2c bus is a
0020      * platform bus, then it is never deleted.
0021      *
0022      * Device tree or ACPI based devices must not
0023      * be unregistered as they have not been
0024      * registered by us, and would not be
0025      * re-created by just probing the V4L2 driver.
0026      */
0027     if (client && !client->dev.of_node && !client->dev.fwnode)
0028         i2c_unregister_device(client);
0029 }
0030 
0031 void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd,
0032                   struct i2c_client *client,
0033                   const char *devname, const char *postfix)
0034 {
0035     if (!devname)
0036         devname = client->dev.driver->name;
0037     if (!postfix)
0038         postfix = "";
0039 
0040     snprintf(sd->name, sizeof(sd->name), "%s%s %d-%04x", devname, postfix,
0041          i2c_adapter_id(client->adapter), client->addr);
0042 }
0043 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_set_name);
0044 
0045 void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
0046               const struct v4l2_subdev_ops *ops)
0047 {
0048     v4l2_subdev_init(sd, ops);
0049     sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
0050     /* the owner is the same as the i2c_client's driver owner */
0051     sd->owner = client->dev.driver->owner;
0052     sd->dev = &client->dev;
0053     /* i2c_client and v4l2_subdev point to one another */
0054     v4l2_set_subdevdata(sd, client);
0055     i2c_set_clientdata(client, sd);
0056     v4l2_i2c_subdev_set_name(sd, client, NULL, NULL);
0057 }
0058 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
0059 
0060 /* Load an i2c sub-device. */
0061 struct v4l2_subdev
0062 *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
0063                struct i2c_adapter *adapter,
0064                struct i2c_board_info *info,
0065                const unsigned short *probe_addrs)
0066 {
0067     struct v4l2_subdev *sd = NULL;
0068     struct i2c_client *client;
0069 
0070     if (!v4l2_dev)
0071         return NULL;
0072 
0073     request_module(I2C_MODULE_PREFIX "%s", info->type);
0074 
0075     /* Create the i2c client */
0076     if (info->addr == 0 && probe_addrs)
0077         client = i2c_new_scanned_device(adapter, info, probe_addrs,
0078                         NULL);
0079     else
0080         client = i2c_new_client_device(adapter, info);
0081 
0082     /*
0083      * Note: by loading the module first we are certain that c->driver
0084      * will be set if the driver was found. If the module was not loaded
0085      * first, then the i2c core tries to delay-load the module for us,
0086      * and then c->driver is still NULL until the module is finally
0087      * loaded. This delay-load mechanism doesn't work if other drivers
0088      * want to use the i2c device, so explicitly loading the module
0089      * is the best alternative.
0090      */
0091     if (!i2c_client_has_driver(client))
0092         goto error;
0093 
0094     /* Lock the module so we can safely get the v4l2_subdev pointer */
0095     if (!try_module_get(client->dev.driver->owner))
0096         goto error;
0097     sd = i2c_get_clientdata(client);
0098 
0099     /*
0100      * Register with the v4l2_device which increases the module's
0101      * use count as well.
0102      */
0103     if (v4l2_device_register_subdev(v4l2_dev, sd))
0104         sd = NULL;
0105     /* Decrease the module use count to match the first try_module_get. */
0106     module_put(client->dev.driver->owner);
0107 
0108 error:
0109     /*
0110      * If we have a client but no subdev, then something went wrong and
0111      * we must unregister the client.
0112      */
0113     if (!IS_ERR(client) && !sd)
0114         i2c_unregister_device(client);
0115     return sd;
0116 }
0117 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
0118 
0119 struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
0120                     struct i2c_adapter *adapter,
0121                     const char *client_type,
0122                     u8 addr,
0123                     const unsigned short *probe_addrs)
0124 {
0125     struct i2c_board_info info;
0126 
0127     /*
0128      * Setup the i2c board info with the device type and
0129      * the device address.
0130      */
0131     memset(&info, 0, sizeof(info));
0132     strscpy(info.type, client_type, sizeof(info.type));
0133     info.addr = addr;
0134 
0135     return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info,
0136                      probe_addrs);
0137 }
0138 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
0139 
0140 /* Return i2c client address of v4l2_subdev. */
0141 unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
0142 {
0143     struct i2c_client *client = v4l2_get_subdevdata(sd);
0144 
0145     return client ? client->addr : I2C_CLIENT_END;
0146 }
0147 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
0148 
0149 /*
0150  * Return a list of I2C tuner addresses to probe. Use only if the tuner
0151  * addresses are unknown.
0152  */
0153 const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
0154 {
0155     static const unsigned short radio_addrs[] = {
0156 #if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761)
0157         0x10,
0158 #endif
0159         0x60,
0160         I2C_CLIENT_END
0161     };
0162     static const unsigned short demod_addrs[] = {
0163         0x42, 0x43, 0x4a, 0x4b,
0164         I2C_CLIENT_END
0165     };
0166     static const unsigned short tv_addrs[] = {
0167         0x42, 0x43, 0x4a, 0x4b,     /* tda8290 */
0168         0x60, 0x61, 0x62, 0x63, 0x64,
0169         I2C_CLIENT_END
0170     };
0171 
0172     switch (type) {
0173     case ADDRS_RADIO:
0174         return radio_addrs;
0175     case ADDRS_DEMOD:
0176         return demod_addrs;
0177     case ADDRS_TV:
0178         return tv_addrs;
0179     case ADDRS_TV_WITH_DEMOD:
0180         return tv_addrs + 4;
0181     }
0182     return NULL;
0183 }
0184 EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);