0001
0002
0003
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
0017
0018
0019
0020
0021
0022
0023
0024
0025
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
0051 sd->owner = client->dev.driver->owner;
0052 sd->dev = &client->dev;
0053
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
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
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
0084
0085
0086
0087
0088
0089
0090
0091 if (!i2c_client_has_driver(client))
0092 goto error;
0093
0094
0095 if (!try_module_get(client->dev.driver->owner))
0096 goto error;
0097 sd = i2c_get_clientdata(client);
0098
0099
0100
0101
0102
0103 if (v4l2_device_register_subdev(v4l2_dev, sd))
0104 sd = NULL;
0105
0106 module_put(client->dev.driver->owner);
0107
0108 error:
0109
0110
0111
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
0129
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
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
0151
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,
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);