Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: v4l2-core: move i2c helpers out of v4l2-common.c

Separate the i2c helpers to v4l2-i2c.c, in order to get rid
of the ifdefery. No functional changes intended, this is
just a cosmetic change to organize the code better.

Given I2C is a tristate symbol, a hidden boolean symbol
is introduced, to make the conditional build easier.

Signed-off-by: Ezequiel Garcia <ezequiel@collabora.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>

authored by

Ezequiel Garcia and committed by
Mauro Carvalho Chehab
02283b98 7c795df5

+229 -182
+5
drivers/media/v4l2-core/Kconfig
··· 11 11 select VIDEOBUF2_V4L2 if VIDEOBUF2_CORE 12 12 default (I2C || I2C=n) && VIDEO_DEV 13 13 14 + config VIDEO_V4L2_I2C 15 + bool 16 + depends on I2C && VIDEO_V4L2 17 + default y 18 + 14 19 config VIDEO_ADV_DEBUG 15 20 bool "Enable advanced debug functionality on V4L2 drivers" 16 21 help
+1
drivers/media/v4l2-core/Makefile
··· 12 12 videodev-$(CONFIG_TRACEPOINTS) += v4l2-trace.o 13 13 videodev-$(CONFIG_MEDIA_CONTROLLER) += v4l2-mc.o 14 14 videodev-$(CONFIG_SPI) += v4l2-spi.o 15 + videodev-$(CONFIG_VIDEO_V4L2_I2C) += v4l2-i2c.o 15 16 16 17 obj-$(CONFIG_V4L2_FWNODE) += v4l2-fwnode.o 17 18 obj-$(CONFIG_VIDEO_V4L2) += videodev.o
-145
drivers/media/v4l2-core/v4l2-common.c
··· 40 40 #include <linux/mm.h> 41 41 #include <linux/string.h> 42 42 #include <linux/errno.h> 43 - #include <linux/i2c.h> 44 43 #include <linux/uaccess.h> 45 44 #include <asm/pgtable.h> 46 45 #include <asm/io.h> ··· 86 87 return 0; 87 88 } 88 89 EXPORT_SYMBOL(v4l2_ctrl_query_fill); 89 - 90 - /* I2C Helper functions */ 91 - 92 - #if IS_ENABLED(CONFIG_I2C) 93 - 94 - void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client, 95 - const char *devname, const char *postfix) 96 - { 97 - if (!devname) 98 - devname = client->dev.driver->name; 99 - if (!postfix) 100 - postfix = ""; 101 - 102 - snprintf(sd->name, sizeof(sd->name), "%s%s %d-%04x", devname, postfix, 103 - i2c_adapter_id(client->adapter), client->addr); 104 - } 105 - EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_set_name); 106 - 107 - void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client, 108 - const struct v4l2_subdev_ops *ops) 109 - { 110 - v4l2_subdev_init(sd, ops); 111 - sd->flags |= V4L2_SUBDEV_FL_IS_I2C; 112 - /* the owner is the same as the i2c_client's driver owner */ 113 - sd->owner = client->dev.driver->owner; 114 - sd->dev = &client->dev; 115 - /* i2c_client and v4l2_subdev point to one another */ 116 - v4l2_set_subdevdata(sd, client); 117 - i2c_set_clientdata(client, sd); 118 - v4l2_i2c_subdev_set_name(sd, client, NULL, NULL); 119 - } 120 - EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init); 121 - 122 - /* Load an i2c sub-device. */ 123 - struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev, 124 - struct i2c_adapter *adapter, struct i2c_board_info *info, 125 - const unsigned short *probe_addrs) 126 - { 127 - struct v4l2_subdev *sd = NULL; 128 - struct i2c_client *client; 129 - 130 - BUG_ON(!v4l2_dev); 131 - 132 - request_module(I2C_MODULE_PREFIX "%s", info->type); 133 - 134 - /* Create the i2c client */ 135 - if (info->addr == 0 && probe_addrs) 136 - client = i2c_new_probed_device(adapter, info, probe_addrs, 137 - NULL); 138 - else 139 - client = i2c_new_device(adapter, info); 140 - 141 - /* Note: by loading the module first we are certain that c->driver 142 - will be set if the driver was found. If the module was not loaded 143 - first, then the i2c core tries to delay-load the module for us, 144 - and then c->driver is still NULL until the module is finally 145 - loaded. This delay-load mechanism doesn't work if other drivers 146 - want to use the i2c device, so explicitly loading the module 147 - is the best alternative. */ 148 - if (client == NULL || client->dev.driver == NULL) 149 - goto error; 150 - 151 - /* Lock the module so we can safely get the v4l2_subdev pointer */ 152 - if (!try_module_get(client->dev.driver->owner)) 153 - goto error; 154 - sd = i2c_get_clientdata(client); 155 - 156 - /* Register with the v4l2_device which increases the module's 157 - use count as well. */ 158 - if (v4l2_device_register_subdev(v4l2_dev, sd)) 159 - sd = NULL; 160 - /* Decrease the module use count to match the first try_module_get. */ 161 - module_put(client->dev.driver->owner); 162 - 163 - error: 164 - /* If we have a client but no subdev, then something went wrong and 165 - we must unregister the client. */ 166 - if (client && sd == NULL) 167 - i2c_unregister_device(client); 168 - return sd; 169 - } 170 - EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board); 171 - 172 - struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev, 173 - struct i2c_adapter *adapter, const char *client_type, 174 - u8 addr, const unsigned short *probe_addrs) 175 - { 176 - struct i2c_board_info info; 177 - 178 - /* Setup the i2c board info with the device type and 179 - the device address. */ 180 - memset(&info, 0, sizeof(info)); 181 - strscpy(info.type, client_type, sizeof(info.type)); 182 - info.addr = addr; 183 - 184 - return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs); 185 - } 186 - EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev); 187 - 188 - /* Return i2c client address of v4l2_subdev. */ 189 - unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd) 190 - { 191 - struct i2c_client *client = v4l2_get_subdevdata(sd); 192 - 193 - return client ? client->addr : I2C_CLIENT_END; 194 - } 195 - EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr); 196 - 197 - /* Return a list of I2C tuner addresses to probe. Use only if the tuner 198 - addresses are unknown. */ 199 - const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type) 200 - { 201 - static const unsigned short radio_addrs[] = { 202 - #if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761) 203 - 0x10, 204 - #endif 205 - 0x60, 206 - I2C_CLIENT_END 207 - }; 208 - static const unsigned short demod_addrs[] = { 209 - 0x42, 0x43, 0x4a, 0x4b, 210 - I2C_CLIENT_END 211 - }; 212 - static const unsigned short tv_addrs[] = { 213 - 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */ 214 - 0x60, 0x61, 0x62, 0x63, 0x64, 215 - I2C_CLIENT_END 216 - }; 217 - 218 - switch (type) { 219 - case ADDRS_RADIO: 220 - return radio_addrs; 221 - case ADDRS_DEMOD: 222 - return demod_addrs; 223 - case ADDRS_TV: 224 - return tv_addrs; 225 - case ADDRS_TV_WITH_DEMOD: 226 - return tv_addrs + 4; 227 - } 228 - return NULL; 229 - } 230 - EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs); 231 - 232 - #endif /* defined(CONFIG_I2C) */ 233 90 234 91 /* Clamp x to be between min and max, aligned to a multiple of 2^align. min 235 92 * and max don't have to be aligned, but there must be at least one valid
+147
drivers/media/v4l2-core/v4l2-i2c.c
··· 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 <media/v4l2-common.h> 9 + #include <media/v4l2-device.h> 10 + 11 + void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client, 12 + const char *devname, const char *postfix) 13 + { 14 + if (!devname) 15 + devname = client->dev.driver->name; 16 + if (!postfix) 17 + postfix = ""; 18 + 19 + snprintf(sd->name, sizeof(sd->name), "%s%s %d-%04x", devname, postfix, 20 + i2c_adapter_id(client->adapter), client->addr); 21 + } 22 + EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_set_name); 23 + 24 + void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client, 25 + const struct v4l2_subdev_ops *ops) 26 + { 27 + v4l2_subdev_init(sd, ops); 28 + sd->flags |= V4L2_SUBDEV_FL_IS_I2C; 29 + /* the owner is the same as the i2c_client's driver owner */ 30 + sd->owner = client->dev.driver->owner; 31 + sd->dev = &client->dev; 32 + /* i2c_client and v4l2_subdev point to one another */ 33 + v4l2_set_subdevdata(sd, client); 34 + i2c_set_clientdata(client, sd); 35 + v4l2_i2c_subdev_set_name(sd, client, NULL, NULL); 36 + } 37 + EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init); 38 + 39 + /* Load an i2c sub-device. */ 40 + struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev, 41 + struct i2c_adapter *adapter, struct i2c_board_info *info, 42 + const unsigned short *probe_addrs) 43 + { 44 + struct v4l2_subdev *sd = NULL; 45 + struct i2c_client *client; 46 + 47 + BUG_ON(!v4l2_dev); 48 + 49 + request_module(I2C_MODULE_PREFIX "%s", info->type); 50 + 51 + /* Create the i2c client */ 52 + if (info->addr == 0 && probe_addrs) 53 + client = i2c_new_probed_device(adapter, info, probe_addrs, 54 + NULL); 55 + else 56 + client = i2c_new_device(adapter, info); 57 + 58 + /* Note: by loading the module first we are certain that c->driver 59 + will be set if the driver was found. If the module was not loaded 60 + first, then the i2c core tries to delay-load the module for us, 61 + and then c->driver is still NULL until the module is finally 62 + loaded. This delay-load mechanism doesn't work if other drivers 63 + want to use the i2c device, so explicitly loading the module 64 + is the best alternative. */ 65 + if (client == NULL || client->dev.driver == NULL) 66 + goto error; 67 + 68 + /* Lock the module so we can safely get the v4l2_subdev pointer */ 69 + if (!try_module_get(client->dev.driver->owner)) 70 + goto error; 71 + sd = i2c_get_clientdata(client); 72 + 73 + /* Register with the v4l2_device which increases the module's 74 + use count as well. */ 75 + if (v4l2_device_register_subdev(v4l2_dev, sd)) 76 + sd = NULL; 77 + /* Decrease the module use count to match the first try_module_get. */ 78 + module_put(client->dev.driver->owner); 79 + 80 + error: 81 + /* If we have a client but no subdev, then something went wrong and 82 + we must unregister the client. */ 83 + if (client && sd == NULL) 84 + i2c_unregister_device(client); 85 + return sd; 86 + } 87 + EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board); 88 + 89 + struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev, 90 + struct i2c_adapter *adapter, const char *client_type, 91 + u8 addr, const unsigned short *probe_addrs) 92 + { 93 + struct i2c_board_info info; 94 + 95 + /* Setup the i2c board info with the device type and 96 + the device address. */ 97 + memset(&info, 0, sizeof(info)); 98 + strscpy(info.type, client_type, sizeof(info.type)); 99 + info.addr = addr; 100 + 101 + return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs); 102 + } 103 + EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev); 104 + 105 + /* Return i2c client address of v4l2_subdev. */ 106 + unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd) 107 + { 108 + struct i2c_client *client = v4l2_get_subdevdata(sd); 109 + 110 + return client ? client->addr : I2C_CLIENT_END; 111 + } 112 + EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr); 113 + 114 + /* Return a list of I2C tuner addresses to probe. Use only if the tuner 115 + addresses are unknown. */ 116 + const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type) 117 + { 118 + static const unsigned short radio_addrs[] = { 119 + #if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761) 120 + 0x10, 121 + #endif 122 + 0x60, 123 + I2C_CLIENT_END 124 + }; 125 + static const unsigned short demod_addrs[] = { 126 + 0x42, 0x43, 0x4a, 0x4b, 127 + I2C_CLIENT_END 128 + }; 129 + static const unsigned short tv_addrs[] = { 130 + 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */ 131 + 0x60, 0x61, 0x62, 0x63, 0x64, 132 + I2C_CLIENT_END 133 + }; 134 + 135 + switch (type) { 136 + case ADDRS_RADIO: 137 + return radio_addrs; 138 + case ADDRS_DEMOD: 139 + return demod_addrs; 140 + case ADDRS_TV: 141 + return tv_addrs; 142 + case ADDRS_TV_WITH_DEMOD: 143 + return tv_addrs + 4; 144 + } 145 + return NULL; 146 + } 147 + EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
+76 -37
include/media/v4l2-common.h
··· 96 96 97 97 /* ------------------------------------------------------------------------- */ 98 98 99 - /* I2C Helper functions */ 100 - 101 - struct i2c_driver; 102 - struct i2c_adapter; 103 - struct i2c_client; 104 - struct i2c_device_id; 105 99 struct v4l2_device; 106 100 struct v4l2_subdev; 107 101 struct v4l2_subdev_ops; 102 + 103 + /* I2C Helper functions */ 104 + #include <linux/i2c.h> 105 + 106 + /** 107 + * enum v4l2_i2c_tuner_type - specifies the range of tuner address that 108 + * should be used when seeking for I2C devices. 109 + * 110 + * @ADDRS_RADIO: Radio tuner addresses. 111 + * Represent the following I2C addresses: 112 + * 0x10 (if compiled with tea5761 support) 113 + * and 0x60. 114 + * @ADDRS_DEMOD: Demod tuner addresses. 115 + * Represent the following I2C addresses: 116 + * 0x42, 0x43, 0x4a and 0x4b. 117 + * @ADDRS_TV: TV tuner addresses. 118 + * Represent the following I2C addresses: 119 + * 0x42, 0x43, 0x4a, 0x4b, 0x60, 0x61, 0x62, 120 + * 0x63 and 0x64. 121 + * @ADDRS_TV_WITH_DEMOD: TV tuner addresses if demod is present, this 122 + * excludes addresses used by the demodulator 123 + * from the list of candidates. 124 + * Represent the following I2C addresses: 125 + * 0x60, 0x61, 0x62, 0x63 and 0x64. 126 + * 127 + * NOTE: All I2C addresses above use the 7-bit notation. 128 + */ 129 + enum v4l2_i2c_tuner_type { 130 + ADDRS_RADIO, 131 + ADDRS_DEMOD, 132 + ADDRS_TV, 133 + ADDRS_TV_WITH_DEMOD, 134 + }; 135 + 136 + #if defined(CONFIG_VIDEO_V4L2_I2C) 108 137 109 138 /** 110 139 * v4l2_i2c_new_subdev - Load an i2c module and return an initialized ··· 151 122 struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev, 152 123 struct i2c_adapter *adapter, const char *client_type, 153 124 u8 addr, const unsigned short *probe_addrs); 154 - 155 - struct i2c_board_info; 156 125 157 126 /** 158 127 * v4l2_i2c_new_subdev_board - Load an i2c module and return an initialized ··· 202 175 unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd); 203 176 204 177 /** 205 - * enum v4l2_i2c_tuner_type - specifies the range of tuner address that 206 - * should be used when seeking for I2C devices. 207 - * 208 - * @ADDRS_RADIO: Radio tuner addresses. 209 - * Represent the following I2C addresses: 210 - * 0x10 (if compiled with tea5761 support) 211 - * and 0x60. 212 - * @ADDRS_DEMOD: Demod tuner addresses. 213 - * Represent the following I2C addresses: 214 - * 0x42, 0x43, 0x4a and 0x4b. 215 - * @ADDRS_TV: TV tuner addresses. 216 - * Represent the following I2C addresses: 217 - * 0x42, 0x43, 0x4a, 0x4b, 0x60, 0x61, 0x62, 218 - * 0x63 and 0x64. 219 - * @ADDRS_TV_WITH_DEMOD: TV tuner addresses if demod is present, this 220 - * excludes addresses used by the demodulator 221 - * from the list of candidates. 222 - * Represent the following I2C addresses: 223 - * 0x60, 0x61, 0x62, 0x63 and 0x64. 224 - * 225 - * NOTE: All I2C addresses above use the 7-bit notation. 226 - */ 227 - enum v4l2_i2c_tuner_type { 228 - ADDRS_RADIO, 229 - ADDRS_DEMOD, 230 - ADDRS_TV, 231 - ADDRS_TV_WITH_DEMOD, 232 - }; 233 - /** 234 178 * v4l2_i2c_tuner_addrs - Return a list of I2C tuner addresses to probe. 235 179 * 236 180 * @type: type of the tuner to seek, as defined by ··· 210 212 * NOTE: Use only if the tuner addresses are unknown. 211 213 */ 212 214 const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type); 215 + 216 + #else 217 + 218 + static inline struct v4l2_subdev * 219 + v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev, 220 + struct i2c_adapter *adapter, const char *client_type, 221 + u8 addr, const unsigned short *probe_addrs) 222 + { 223 + return NULL; 224 + } 225 + 226 + static inline struct v4l2_subdev * 227 + v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev, 228 + struct i2c_adapter *adapter, struct i2c_board_info *info, 229 + const unsigned short *probe_addrs) 230 + { 231 + return NULL; 232 + } 233 + 234 + static inline void 235 + v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client, 236 + const char *devname, const char *postfix) 237 + {} 238 + 239 + static inline void 240 + v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client, 241 + const struct v4l2_subdev_ops *ops) 242 + {} 243 + 244 + static inline unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd) 245 + { 246 + return I2C_CLIENT_END; 247 + } 248 + 249 + static inline const unsigned short * 250 + v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type) 251 + { 252 + return NULL; 253 + } 254 + 255 + #endif 213 256 214 257 /* ------------------------------------------------------------------------- */ 215 258