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