blob: 2e9d81f4c1a57e9f51cd728b991c887558d11c72 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Video for Linux Two
3 *
4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
6 *
7 * This file replaces the videodev.c file that comes with the
8 * regular kernel distribution.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
Mauro Carvalho Chehab43db48d2007-01-09 11:20:59 -030015 * Author: Bill Dirks <bill@thedirks.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * based on code by Alan Cox, <alan@cymru.net>
17 *
18 */
19
20/*
21 * Video capture interface for Linux
22 *
23 * A generic video device interface for the LINUX operating system
24 * using a set of device structures/vectors for low level operations.
25 *
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version
29 * 2 of the License, or (at your option) any later version.
30 *
Alan Coxd9b01442008-10-27 15:13:47 -030031 * Author: Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
33 * Fixes:
34 */
35
36/*
37 * Video4linux 1/2 integration by Justin Schoeman
38 * <justin@suntiger.ee.up.ac.za>
39 * 2.4 PROCFS support ported from 2.4 kernels by
Jan Engelhardt96de0e22007-10-19 23:21:04 +020040 * Iñaki García Etxebarria <garetxe@euskalnet.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * Makefile fix by "W. Michael Petullo" <mike@flyn.org>
42 * 2.4 devfs support ported from 2.4 kernels by
43 * Dan Merillat <dan@merillat.org>
44 * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
45 */
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/module.h>
48#include <linux/types.h>
49#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mm.h>
51#include <linux/string.h>
52#include <linux/errno.h>
Hans Verkuilf3d092b2007-02-23 20:55:14 -030053#include <linux/i2c.h>
Dmitri Belimov85e09212010-03-23 11:23:29 -030054#if defined(CONFIG_SPI)
55#include <linux/spi/spi.h>
56#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/pgtable.h>
59#include <asm/io.h>
60#include <asm/div64.h>
Michael Krufky5e453dc2006-01-09 15:32:31 -020061#include <media/v4l2-common.h>
Hans Verkuildd991202008-11-23 12:19:45 -030062#include <media/v4l2-device.h>
Hans Verkuil09965172010-08-01 14:32:42 -030063#include <media/v4l2-ctrls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Hans Verkuil33b687c2008-07-25 05:32:50 -030065#include <linux/videodev2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
68MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
69MODULE_LICENSE("GPL");
70
71/*
72 *
73 * V 4 L 2 D R I V E R H E L P E R A P I
74 *
75 */
76
77/*
78 * Video Standard Operations (contributed by Michael Schimek)
79 */
80
Hans Verkuil9cb23182006-06-18 14:11:08 -030081/* Helper functions for control handling */
82
83/* Check for correctness of the ctrl's value based on the data from
84 struct v4l2_queryctrl and the available menu items. Note that
85 menu_items may be NULL, in that case it is ignored. */
86int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
Hans Verkuil513521e2010-12-29 14:25:52 -030087 const char * const *menu_items)
Hans Verkuil9cb23182006-06-18 14:11:08 -030088{
89 if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
90 return -EINVAL;
91 if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
92 return -EBUSY;
Hans Verkuil6b5a9492009-08-11 18:47:18 -030093 if (qctrl->type == V4L2_CTRL_TYPE_STRING)
94 return 0;
Hans Verkuil9cb23182006-06-18 14:11:08 -030095 if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
96 qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
97 qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
98 return 0;
99 if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
100 return -ERANGE;
101 if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
102 if (menu_items[ctrl->value] == NULL ||
103 menu_items[ctrl->value][0] == '\0')
104 return -EINVAL;
105 }
Hans Verkuilfa4d7092011-05-23 04:07:05 -0300106 if (qctrl->type == V4L2_CTRL_TYPE_BITMASK &&
107 (ctrl->value & ~qctrl->maximum))
108 return -ERANGE;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300109 return 0;
110}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300111EXPORT_SYMBOL(v4l2_ctrl_check);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300112
Hans Verkuil9cb23182006-06-18 14:11:08 -0300113/* Fill in a struct v4l2_queryctrl */
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300114int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 _min, s32 _max, s32 _step, s32 _def)
Hans Verkuil9cb23182006-06-18 14:11:08 -0300115{
Hans Verkuil09965172010-08-01 14:32:42 -0300116 const char *name;
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300117 s64 min = _min;
118 s64 max = _max;
119 u64 step = _step;
120 s64 def = _def;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300121
Hans Verkuil09965172010-08-01 14:32:42 -0300122 v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
123 &min, &max, &step, &def, &qctrl->flags);
124
Hans Verkuil69028d72008-08-08 07:55:00 -0300125 if (name == NULL)
Hans Verkuil9cb23182006-06-18 14:11:08 -0300126 return -EINVAL;
Hans Verkuil69028d72008-08-08 07:55:00 -0300127
Hans Verkuil9cb23182006-06-18 14:11:08 -0300128 qctrl->minimum = min;
129 qctrl->maximum = max;
130 qctrl->step = step;
131 qctrl->default_value = def;
132 qctrl->reserved[0] = qctrl->reserved[1] = 0;
Hans Verkuilb634a932009-01-17 11:26:59 -0300133 strlcpy(qctrl->name, name, sizeof(qctrl->name));
Hans Verkuil9cb23182006-06-18 14:11:08 -0300134 return 0;
135}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300136EXPORT_SYMBOL(v4l2_ctrl_query_fill);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300137
Hans Verkuil9cb23182006-06-18 14:11:08 -0300138/* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
Hans Verkuile281db582008-08-08 12:43:59 -0300139 the menu. The qctrl pointer may be NULL, in which case it is ignored.
140 If menu_items is NULL, then the menu items are retrieved using
141 v4l2_ctrl_get_menu. */
Hans Verkuil9cb23182006-06-18 14:11:08 -0300142int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
Hans Verkuil513521e2010-12-29 14:25:52 -0300143 const char * const *menu_items)
Hans Verkuil9cb23182006-06-18 14:11:08 -0300144{
145 int i;
146
Hans Verkuil1e551262008-08-08 08:34:19 -0300147 qmenu->reserved = 0;
Hans Verkuile281db582008-08-08 12:43:59 -0300148 if (menu_items == NULL)
149 menu_items = v4l2_ctrl_get_menu(qmenu->id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300150 if (menu_items == NULL ||
151 (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
152 return -EINVAL;
153 for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
154 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
155 return -EINVAL;
Hans Verkuilb634a932009-01-17 11:26:59 -0300156 strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name));
Hans Verkuil9cb23182006-06-18 14:11:08 -0300157 return 0;
158}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300159EXPORT_SYMBOL(v4l2_ctrl_query_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300160
Hans Verkuil1e551262008-08-08 08:34:19 -0300161/* Fill in a struct v4l2_querymenu based on the specified array of valid
162 menu items (terminated by V4L2_CTRL_MENU_IDS_END).
163 Use this if there are 'holes' in the list of valid menu items. */
164int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
165{
Hans Verkuil513521e2010-12-29 14:25:52 -0300166 const char * const *menu_items = v4l2_ctrl_get_menu(qmenu->id);
Hans Verkuil1e551262008-08-08 08:34:19 -0300167
168 qmenu->reserved = 0;
169 if (menu_items == NULL || ids == NULL)
170 return -EINVAL;
171 while (*ids != V4L2_CTRL_MENU_IDS_END) {
172 if (*ids++ == qmenu->index) {
Hans Verkuilb634a932009-01-17 11:26:59 -0300173 strlcpy(qmenu->name, menu_items[qmenu->index],
174 sizeof(qmenu->name));
Hans Verkuil1e551262008-08-08 08:34:19 -0300175 return 0;
176 }
177 }
178 return -EINVAL;
179}
180EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
181
Hans Verkuil9cb23182006-06-18 14:11:08 -0300182/* ctrl_classes points to an array of u32 pointers, the last element is
183 a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
184 Each array must be sorted low to high and belong to the same control
Hans Verkuil2ba58892009-02-13 10:57:48 -0300185 class. The array of u32 pointers must also be sorted, from low class IDs
Hans Verkuil9cb23182006-06-18 14:11:08 -0300186 to high class IDs.
187
188 This function returns the first ID that follows after the given ID.
189 When no more controls are available 0 is returned. */
190u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
191{
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300192 u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300193 const u32 *pctrl;
194
Hans Verkuil9cb23182006-06-18 14:11:08 -0300195 if (ctrl_classes == NULL)
196 return 0;
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300197
198 /* if no query is desired, then check if the ID is part of ctrl_classes */
199 if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
200 /* find class */
201 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
202 ctrl_classes++;
203 if (*ctrl_classes == NULL)
204 return 0;
205 pctrl = *ctrl_classes;
206 /* find control ID */
207 while (*pctrl && *pctrl != id) pctrl++;
208 return *pctrl ? id : 0;
209 }
Hans Verkuil9cb23182006-06-18 14:11:08 -0300210 id &= V4L2_CTRL_ID_MASK;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300211 id++; /* select next control */
212 /* find first class that matches (or is greater than) the class of
213 the ID */
214 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
215 ctrl_classes++;
216 /* no more classes */
217 if (*ctrl_classes == NULL)
218 return 0;
219 pctrl = *ctrl_classes;
220 /* find first ctrl within the class that is >= ID */
221 while (*pctrl && *pctrl < id) pctrl++;
222 if (*pctrl)
223 return *pctrl;
224 /* we are at the end of the controls of the current class. */
225 /* continue with next class if available */
226 ctrl_classes++;
227 if (*ctrl_classes == NULL)
228 return 0;
229 return **ctrl_classes;
230}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300231EXPORT_SYMBOL(v4l2_ctrl_next);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300232
Hans Verkuil78a3b4d2009-04-01 03:41:09 -0300233/* I2C Helper functions */
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300234
Hans Verkuilaee38732013-05-26 10:01:42 -0300235#if IS_ENABLED(CONFIG_I2C)
Hans Verkuildd991202008-11-23 12:19:45 -0300236
237void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
238 const struct v4l2_subdev_ops *ops)
239{
240 v4l2_subdev_init(sd, ops);
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300241 sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
Hans Verkuildd991202008-11-23 12:19:45 -0300242 /* the owner is the same as the i2c_client's driver owner */
Lars-Peter Clausenf9d32f22013-09-29 10:51:01 +0200243 sd->owner = client->dev.driver->owner;
Guennadi Liakhovetski668773b2013-06-10 15:07:35 -0300244 sd->dev = &client->dev;
Hans Verkuildd991202008-11-23 12:19:45 -0300245 /* i2c_client and v4l2_subdev point to one another */
246 v4l2_set_subdevdata(sd, client);
247 i2c_set_clientdata(client, sd);
248 /* initialize name */
249 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
Lars-Peter Clausenf9d32f22013-09-29 10:51:01 +0200250 client->dev.driver->name, i2c_adapter_id(client->adapter),
Hans Verkuildd991202008-11-23 12:19:45 -0300251 client->addr);
252}
253EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
254
Hans Verkuile6574f22009-04-01 03:57:53 -0300255/* Load an i2c sub-device. */
Hans Verkuilf0222c72009-06-09 17:12:33 -0300256struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300257 struct i2c_adapter *adapter, struct i2c_board_info *info,
258 const unsigned short *probe_addrs)
Hans Verkuilf0222c72009-06-09 17:12:33 -0300259{
260 struct v4l2_subdev *sd = NULL;
261 struct i2c_client *client;
262
263 BUG_ON(!v4l2_dev);
264
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300265 request_module(I2C_MODULE_PREFIX "%s", info->type);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300266
267 /* Create the i2c client */
268 if (info->addr == 0 && probe_addrs)
Jean Delvare9a942412010-08-11 18:20:56 +0200269 client = i2c_new_probed_device(adapter, info, probe_addrs,
270 NULL);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300271 else
272 client = i2c_new_device(adapter, info);
273
274 /* Note: by loading the module first we are certain that c->driver
275 will be set if the driver was found. If the module was not loaded
276 first, then the i2c core tries to delay-load the module for us,
277 and then c->driver is still NULL until the module is finally
278 loaded. This delay-load mechanism doesn't work if other drivers
279 want to use the i2c device, so explicitly loading the module
280 is the best alternative. */
Lars-Peter Clausenf9d32f22013-09-29 10:51:01 +0200281 if (client == NULL || client->dev.driver == NULL)
Hans Verkuilf0222c72009-06-09 17:12:33 -0300282 goto error;
283
284 /* Lock the module so we can safely get the v4l2_subdev pointer */
Lars-Peter Clausenf9d32f22013-09-29 10:51:01 +0200285 if (!try_module_get(client->dev.driver->owner))
Hans Verkuilf0222c72009-06-09 17:12:33 -0300286 goto error;
287 sd = i2c_get_clientdata(client);
288
289 /* Register with the v4l2_device which increases the module's
290 use count as well. */
291 if (v4l2_device_register_subdev(v4l2_dev, sd))
292 sd = NULL;
293 /* Decrease the module use count to match the first try_module_get. */
Lars-Peter Clausenf9d32f22013-09-29 10:51:01 +0200294 module_put(client->dev.driver->owner);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300295
Hans Verkuilf0222c72009-06-09 17:12:33 -0300296error:
297 /* If we have a client but no subdev, then something went wrong and
298 we must unregister the client. */
299 if (client && sd == NULL)
300 i2c_unregister_device(client);
301 return sd;
302}
303EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
304
Hans Verkuil3c7c9372011-01-08 07:08:02 -0300305struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300306 struct i2c_adapter *adapter, const char *client_type,
Hans Verkuilf0222c72009-06-09 17:12:33 -0300307 u8 addr, const unsigned short *probe_addrs)
308{
309 struct i2c_board_info info;
310
311 /* Setup the i2c board info with the device type and
312 the device address. */
313 memset(&info, 0, sizeof(info));
314 strlcpy(info.type, client_type, sizeof(info.type));
315 info.addr = addr;
Hans Verkuilf0222c72009-06-09 17:12:33 -0300316
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300317 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300318}
Hans Verkuil3c7c9372011-01-08 07:08:02 -0300319EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300320
Hans Verkuilab3731902009-02-21 18:08:41 -0300321/* Return i2c client address of v4l2_subdev. */
322unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
323{
324 struct i2c_client *client = v4l2_get_subdevdata(sd);
325
326 return client ? client->addr : I2C_CLIENT_END;
327}
328EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
329
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300330/* Return a list of I2C tuner addresses to probe. Use only if the tuner
331 addresses are unknown. */
332const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
333{
334 static const unsigned short radio_addrs[] = {
Peter Senna Tschudinffe4db02013-01-19 19:41:31 -0300335#if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761)
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300336 0x10,
337#endif
338 0x60,
339 I2C_CLIENT_END
340 };
341 static const unsigned short demod_addrs[] = {
342 0x42, 0x43, 0x4a, 0x4b,
343 I2C_CLIENT_END
344 };
345 static const unsigned short tv_addrs[] = {
346 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
Hans Verkuil416a7aa2009-05-02 09:42:57 -0300347 0x60, 0x61, 0x62, 0x63, 0x64,
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300348 I2C_CLIENT_END
349 };
350
351 switch (type) {
352 case ADDRS_RADIO:
353 return radio_addrs;
354 case ADDRS_DEMOD:
355 return demod_addrs;
356 case ADDRS_TV:
357 return tv_addrs;
358 case ADDRS_TV_WITH_DEMOD:
359 return tv_addrs + 4;
360 }
361 return NULL;
362}
363EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
364
Trent Piephod8b29962009-06-12 16:31:29 -0300365#endif /* defined(CONFIG_I2C) */
366
Dmitri Belimov85e09212010-03-23 11:23:29 -0300367#if defined(CONFIG_SPI)
368
Michael Jones9d380ad2012-07-26 10:48:25 -0300369/* Load an spi sub-device. */
Dmitri Belimov85e09212010-03-23 11:23:29 -0300370
371void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
372 const struct v4l2_subdev_ops *ops)
373{
374 v4l2_subdev_init(sd, ops);
375 sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
376 /* the owner is the same as the spi_device's driver owner */
377 sd->owner = spi->dev.driver->owner;
Guennadi Liakhovetski668773b2013-06-10 15:07:35 -0300378 sd->dev = &spi->dev;
Dmitri Belimov85e09212010-03-23 11:23:29 -0300379 /* spi_device and v4l2_subdev point to one another */
380 v4l2_set_subdevdata(sd, spi);
381 spi_set_drvdata(spi, sd);
382 /* initialize name */
383 strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
384}
385EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
386
387struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
388 struct spi_master *master, struct spi_board_info *info)
389{
390 struct v4l2_subdev *sd = NULL;
391 struct spi_device *spi = NULL;
392
393 BUG_ON(!v4l2_dev);
394
Alan Cox07ca4182012-09-17 06:51:27 -0300395 if (info->modalias[0])
Dmitri Belimov85e09212010-03-23 11:23:29 -0300396 request_module(info->modalias);
397
398 spi = spi_new_device(master, info);
399
400 if (spi == NULL || spi->dev.driver == NULL)
401 goto error;
402
403 if (!try_module_get(spi->dev.driver->owner))
404 goto error;
405
406 sd = spi_get_drvdata(spi);
407
408 /* Register with the v4l2_device which increases the module's
409 use count as well. */
410 if (v4l2_device_register_subdev(v4l2_dev, sd))
411 sd = NULL;
412
413 /* Decrease the module use count to match the first try_module_get. */
414 module_put(spi->dev.driver->owner);
415
416error:
417 /* If we have a client but no subdev, then something went wrong and
418 we must unregister the client. */
419 if (spi && sd == NULL)
420 spi_unregister_device(spi);
421
422 return sd;
423}
424EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
425
426#endif /* defined(CONFIG_SPI) */
427
Trent Piephob0d31592009-05-30 21:45:46 -0300428/* Clamp x to be between min and max, aligned to a multiple of 2^align. min
429 * and max don't have to be aligned, but there must be at least one valid
430 * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
431 * of 16 between 17 and 31. */
432static unsigned int clamp_align(unsigned int x, unsigned int min,
433 unsigned int max, unsigned int align)
434{
435 /* Bits that must be zero to be aligned */
436 unsigned int mask = ~((1 << align) - 1);
437
Maciej Matraszek3bacc102014-09-15 05:14:48 -0300438 /* Clamp to aligned min and max */
439 x = clamp(x, (min + ~mask) & mask, max & mask);
440
Trent Piephob0d31592009-05-30 21:45:46 -0300441 /* Round to nearest aligned value */
442 if (align)
443 x = (x + (1 << (align - 1))) & mask;
444
Trent Piephob0d31592009-05-30 21:45:46 -0300445 return x;
446}
447
448/* Bound an image to have a width between wmin and wmax, and height between
449 * hmin and hmax, inclusive. Additionally, the width will be a multiple of
450 * 2^walign, the height will be a multiple of 2^halign, and the overall size
451 * (width*height) will be a multiple of 2^salign. The image may be shrunk
452 * or enlarged to fit the alignment constraints.
453 *
454 * The width or height maximum must not be smaller than the corresponding
455 * minimum. The alignments must not be so high there are no possible image
456 * sizes within the allowed bounds. wmin and hmin must be at least 1
457 * (don't use 0). If you don't care about a certain alignment, specify 0,
458 * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
459 * you only want to adjust downward, specify a maximum that's the same as
460 * the initial value.
461 */
462void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
463 unsigned int walign,
464 u32 *h, unsigned int hmin, unsigned int hmax,
465 unsigned int halign, unsigned int salign)
466{
467 *w = clamp_align(*w, wmin, wmax, walign);
468 *h = clamp_align(*h, hmin, hmax, halign);
469
470 /* Usually we don't need to align the size and are done now. */
471 if (!salign)
472 return;
473
474 /* How much alignment do we have? */
475 walign = __ffs(*w);
476 halign = __ffs(*h);
477 /* Enough to satisfy the image alignment? */
478 if (walign + halign < salign) {
479 /* Max walign where there is still a valid width */
480 unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
481 /* Max halign where there is still a valid height */
482 unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
483
484 /* up the smaller alignment until we have enough */
485 do {
486 if (halign >= hmaxa ||
487 (walign <= halign && walign < wmaxa)) {
488 *w = clamp_align(*w, wmin, wmax, walign + 1);
489 walign = __ffs(*w);
490 } else {
491 *h = clamp_align(*h, hmin, hmax, halign + 1);
492 halign = __ffs(*h);
493 }
494 } while (halign + walign < salign);
495 }
496}
497EXPORT_SYMBOL_GPL(v4l_bound_align_image);
Muralidharan Karicheri2e535ed2009-12-10 04:39:47 -0300498
Hans Verkuil3fd8e642010-09-30 09:29:37 -0300499const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
500 const struct v4l2_discrete_probe *probe,
501 s32 width, s32 height)
Guennadi Liakhovetski79c6ff92010-08-27 13:41:44 -0300502{
503 int i;
504 u32 error, min_error = UINT_MAX;
Hans Verkuil3fd8e642010-09-30 09:29:37 -0300505 const struct v4l2_frmsize_discrete *size, *best = NULL;
Guennadi Liakhovetski79c6ff92010-08-27 13:41:44 -0300506
507 if (!probe)
508 return best;
509
510 for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) {
511 error = abs(size->width - width) + abs(size->height - height);
512 if (error < min_error) {
513 min_error = error;
514 best = size;
515 }
516 if (!error)
517 break;
518 }
519
520 return best;
521}
522EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
Sakari Ailusabd23292012-09-15 07:51:47 -0300523
524void v4l2_get_timestamp(struct timeval *tv)
525{
526 struct timespec ts;
527
528 ktime_get_ts(&ts);
529 tv->tv_sec = ts.tv_sec;
530 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
531}
532EXPORT_SYMBOL_GPL(v4l2_get_timestamp);