1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#include <linux/module.h>
48#include <linux/types.h>
49#include <linux/kernel.h>
50#include <linux/mm.h>
51#include <linux/string.h>
52#include <linux/errno.h>
53#include <linux/i2c.h>
54#if defined(CONFIG_SPI)
55#include <linux/spi/spi.h>
56#endif
57#include <asm/uaccess.h>
58#include <asm/pgtable.h>
59#include <asm/io.h>
60#include <asm/div64.h>
61#include <media/v4l2-common.h>
62#include <media/v4l2-device.h>
63#include <media/v4l2-ctrls.h>
64
65#include <linux/videodev2.h>
66
67MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
68MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
69MODULE_LICENSE("GPL");
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 _min, s32 _max, s32 _step, s32 _def)
85{
86 const char *name;
87 s64 min = _min;
88 s64 max = _max;
89 u64 step = _step;
90 s64 def = _def;
91
92 v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
93 &min, &max, &step, &def, &qctrl->flags);
94
95 if (name == NULL)
96 return -EINVAL;
97
98 qctrl->minimum = min;
99 qctrl->maximum = max;
100 qctrl->step = step;
101 qctrl->default_value = def;
102 qctrl->reserved[0] = qctrl->reserved[1] = 0;
103 strlcpy(qctrl->name, name, sizeof(qctrl->name));
104 return 0;
105}
106EXPORT_SYMBOL(v4l2_ctrl_query_fill);
107
108
109
110#if IS_ENABLED(CONFIG_I2C)
111
112void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
113 const struct v4l2_subdev_ops *ops)
114{
115 v4l2_subdev_init(sd, ops);
116 sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
117
118 sd->owner = client->dev.driver->owner;
119 sd->dev = &client->dev;
120
121 v4l2_set_subdevdata(sd, client);
122 i2c_set_clientdata(client, sd);
123
124 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
125 client->dev.driver->name, i2c_adapter_id(client->adapter),
126 client->addr);
127}
128EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
129
130
131struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
132 struct i2c_adapter *adapter, struct i2c_board_info *info,
133 const unsigned short *probe_addrs)
134{
135 struct v4l2_subdev *sd = NULL;
136 struct i2c_client *client;
137
138 BUG_ON(!v4l2_dev);
139
140 request_module(I2C_MODULE_PREFIX "%s", info->type);
141
142
143 if (info->addr == 0 && probe_addrs)
144 client = i2c_new_probed_device(adapter, info, probe_addrs,
145 NULL);
146 else
147 client = i2c_new_device(adapter, info);
148
149
150
151
152
153
154
155
156 if (client == NULL || client->dev.driver == NULL)
157 goto error;
158
159
160 if (!try_module_get(client->dev.driver->owner))
161 goto error;
162 sd = i2c_get_clientdata(client);
163
164
165
166 if (v4l2_device_register_subdev(v4l2_dev, sd))
167 sd = NULL;
168
169 module_put(client->dev.driver->owner);
170
171error:
172
173
174 if (client && sd == NULL)
175 i2c_unregister_device(client);
176 return sd;
177}
178EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
179
180struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
181 struct i2c_adapter *adapter, const char *client_type,
182 u8 addr, const unsigned short *probe_addrs)
183{
184 struct i2c_board_info info;
185
186
187
188 memset(&info, 0, sizeof(info));
189 strlcpy(info.type, client_type, sizeof(info.type));
190 info.addr = addr;
191
192 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
193}
194EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
195
196
197unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
198{
199 struct i2c_client *client = v4l2_get_subdevdata(sd);
200
201 return client ? client->addr : I2C_CLIENT_END;
202}
203EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
204
205
206
207const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
208{
209 static const unsigned short radio_addrs[] = {
210#if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761)
211 0x10,
212#endif
213 0x60,
214 I2C_CLIENT_END
215 };
216 static const unsigned short demod_addrs[] = {
217 0x42, 0x43, 0x4a, 0x4b,
218 I2C_CLIENT_END
219 };
220 static const unsigned short tv_addrs[] = {
221 0x42, 0x43, 0x4a, 0x4b,
222 0x60, 0x61, 0x62, 0x63, 0x64,
223 I2C_CLIENT_END
224 };
225
226 switch (type) {
227 case ADDRS_RADIO:
228 return radio_addrs;
229 case ADDRS_DEMOD:
230 return demod_addrs;
231 case ADDRS_TV:
232 return tv_addrs;
233 case ADDRS_TV_WITH_DEMOD:
234 return tv_addrs + 4;
235 }
236 return NULL;
237}
238EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
239
240#endif
241
242#if defined(CONFIG_SPI)
243
244
245
246void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
247 const struct v4l2_subdev_ops *ops)
248{
249 v4l2_subdev_init(sd, ops);
250 sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
251
252 sd->owner = spi->dev.driver->owner;
253 sd->dev = &spi->dev;
254
255 v4l2_set_subdevdata(sd, spi);
256 spi_set_drvdata(spi, sd);
257
258 strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
259}
260EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
261
262struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
263 struct spi_master *master, struct spi_board_info *info)
264{
265 struct v4l2_subdev *sd = NULL;
266 struct spi_device *spi = NULL;
267
268 BUG_ON(!v4l2_dev);
269
270 if (info->modalias[0])
271 request_module(info->modalias);
272
273 spi = spi_new_device(master, info);
274
275 if (spi == NULL || spi->dev.driver == NULL)
276 goto error;
277
278 if (!try_module_get(spi->dev.driver->owner))
279 goto error;
280
281 sd = spi_get_drvdata(spi);
282
283
284
285 if (v4l2_device_register_subdev(v4l2_dev, sd))
286 sd = NULL;
287
288
289 module_put(spi->dev.driver->owner);
290
291error:
292
293
294 if (spi && sd == NULL)
295 spi_unregister_device(spi);
296
297 return sd;
298}
299EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
300
301#endif
302
303
304
305
306
307static unsigned int clamp_align(unsigned int x, unsigned int min,
308 unsigned int max, unsigned int align)
309{
310
311 unsigned int mask = ~((1 << align) - 1);
312
313
314 x = clamp(x, (min + ~mask) & mask, max & mask);
315
316
317 if (align)
318 x = (x + (1 << (align - 1))) & mask;
319
320 return x;
321}
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
338 unsigned int walign,
339 u32 *h, unsigned int hmin, unsigned int hmax,
340 unsigned int halign, unsigned int salign)
341{
342 *w = clamp_align(*w, wmin, wmax, walign);
343 *h = clamp_align(*h, hmin, hmax, halign);
344
345
346 if (!salign)
347 return;
348
349
350 walign = __ffs(*w);
351 halign = __ffs(*h);
352
353 if (walign + halign < salign) {
354
355 unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
356
357 unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
358
359
360 do {
361 if (halign >= hmaxa ||
362 (walign <= halign && walign < wmaxa)) {
363 *w = clamp_align(*w, wmin, wmax, walign + 1);
364 walign = __ffs(*w);
365 } else {
366 *h = clamp_align(*h, hmin, hmax, halign + 1);
367 halign = __ffs(*h);
368 }
369 } while (halign + walign < salign);
370 }
371}
372EXPORT_SYMBOL_GPL(v4l_bound_align_image);
373
374const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
375 const struct v4l2_discrete_probe *probe,
376 s32 width, s32 height)
377{
378 int i;
379 u32 error, min_error = UINT_MAX;
380 const struct v4l2_frmsize_discrete *size, *best = NULL;
381
382 if (!probe)
383 return best;
384
385 for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) {
386 error = abs(size->width - width) + abs(size->height - height);
387 if (error < min_error) {
388 min_error = error;
389 best = size;
390 }
391 if (!error)
392 break;
393 }
394
395 return best;
396}
397EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
398
399void v4l2_get_timestamp(struct timeval *tv)
400{
401 struct timespec ts;
402
403 ktime_get_ts(&ts);
404 tv->tv_sec = ts.tv_sec;
405 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
406}
407EXPORT_SYMBOL_GPL(v4l2_get_timestamp);
408