1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * v4l2-i2c - I2C helpers for Video4Linux2 4 */ 5 6 #include <linux/i2c.h> 7 #include <linux/module.h> 8 #include <linux/property.h> 9 #include <media/v4l2-common.h> 10 #include <media/v4l2-device.h> 11 12 void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd) 13 { 14 struct i2c_client *client = v4l2_get_subdevdata(sd); 15 16 /* 17 * We need to unregister the i2c client 18 * explicitly. We cannot rely on 19 * i2c_del_adapter to always unregister 20 * clients for us, since if the i2c bus is a 21 * platform bus, then it is never deleted. 22 * 23 * Device tree or ACPI based devices must not 24 * be unregistered as they have not been 25 * registered by us, and would not be 26 * re-created by just probing the V4L2 driver. 27 */ 28 if (client && !dev_fwnode(&client->dev)) 29 i2c_unregister_device(client); 30 } 31 32 void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, 33 struct i2c_client *client, 34 const char *devname, const char *postfix) 35 { 36 if (!devname) 37 devname = client->dev.driver->name; 38 if (!postfix) 39 postfix = ""; 40 41 snprintf(sd->name, sizeof(sd->name), "%s%s %d-%04x", devname, postfix, 42 i2c_adapter_id(client->adapter), client->addr); 43 } 44 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_set_name); 45 46 void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client, 47 const struct v4l2_subdev_ops *ops) 48 { 49 v4l2_subdev_init(sd, ops); 50 sd->flags |= V4L2_SUBDEV_FL_IS_I2C; 51 /* the owner is the same as the i2c_client's driver owner */ 52 sd->owner = client->dev.driver->owner; 53 sd->dev = &client->dev; 54 /* i2c_client and v4l2_subdev point to one another */ 55 v4l2_set_subdevdata(sd, client); 56 i2c_set_clientdata(client, sd); 57 v4l2_i2c_subdev_set_name(sd, client, NULL, NULL); 58 } 59 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init); 60 61 /* Load an i2c sub-device. */ 62 struct v4l2_subdev 63 *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev, 64 struct i2c_adapter *adapter, 65 struct i2c_board_info *info, 66 const unsigned short *probe_addrs) 67 { 68 struct v4l2_subdev *sd = NULL; 69 struct i2c_client *client; 70 71 if (!v4l2_dev) 72 return NULL; 73 74 request_module(I2C_MODULE_PREFIX "%s", info->type); 75 76 /* Create the i2c client */ 77 if (info->addr == 0 && probe_addrs) 78 client = i2c_new_scanned_device(adapter, info, probe_addrs, 79 NULL); 80 else 81 client = i2c_new_client_device(adapter, info); 82 83 /* 84 * Note: by loading the module first we are certain that c->driver 85 * will be set if the driver was found. If the module was not loaded 86 * first, then the i2c core tries to delay-load the module for us, 87 * and then c->driver is still NULL until the module is finally 88 * loaded. This delay-load mechanism doesn't work if other drivers 89 * want to use the i2c device, so explicitly loading the module 90 * is the best alternative. 91 */ 92 if (!i2c_client_has_driver(client)) 93 goto error; 94 95 /* Lock the module so we can safely get the v4l2_subdev pointer */ 96 if (!try_module_get(client->dev.driver->owner)) 97 goto error; 98 sd = i2c_get_clientdata(client); 99 100 /* 101 * Register with the v4l2_device which increases the module's 102 * use count as well. 103 */ 104 if (__v4l2_device_register_subdev(v4l2_dev, sd, sd->owner)) 105 sd = NULL; 106 /* Decrease the module use count to match the first try_module_get. */ 107 module_put(client->dev.driver->owner); 108 109 error: 110 /* 111 * If we have a client but no subdev, then something went wrong and 112 * we must unregister the client. 113 */ 114 if (!IS_ERR(client) && !sd) 115 i2c_unregister_device(client); 116 return sd; 117 } 118 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board); 119 120 struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev, 121 struct i2c_adapter *adapter, 122 const char *client_type, 123 u8 addr, 124 const unsigned short *probe_addrs) 125 { 126 struct i2c_board_info info; 127 128 /* 129 * Setup the i2c board info with the device type and 130 * the device address. 131 */ 132 memset(&info, 0, sizeof(info)); 133 strscpy(info.type, client_type, sizeof(info.type)); 134 info.addr = addr; 135 136 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, 137 probe_addrs); 138 } 139 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev); 140 141 /* Return i2c client address of v4l2_subdev. */ 142 unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd) 143 { 144 struct i2c_client *client = v4l2_get_subdevdata(sd); 145 146 return client ? client->addr : I2C_CLIENT_END; 147 } 148 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr); 149 150 /* 151 * Return a list of I2C tuner addresses to probe. Use only if the tuner 152 * addresses are unknown. 153 */ 154 const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type) 155 { 156 static const unsigned short radio_addrs[] = { 157 #if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761) 158 0x10, 159 #endif 160 0x60, 161 I2C_CLIENT_END 162 }; 163 static const unsigned short demod_addrs[] = { 164 0x42, 0x43, 0x4a, 0x4b, 165 I2C_CLIENT_END 166 }; 167 static const unsigned short tv_addrs[] = { 168 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */ 169 0x60, 0x61, 0x62, 0x63, 0x64, 170 I2C_CLIENT_END 171 }; 172 173 switch (type) { 174 case ADDRS_RADIO: 175 return radio_addrs; 176 case ADDRS_DEMOD: 177 return demod_addrs; 178 case ADDRS_TV: 179 return tv_addrs; 180 case ADDRS_TV_WITH_DEMOD: 181 return tv_addrs + 4; 182 } 183 return NULL; 184 } 185 EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs); 186