blob: 5fd76609e61367bfed868ee77c6d7ec0875f7be7 [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>
Hans Verkuil3434eb72007-04-27 12:31:08 -030064#include <media/v4l2-chip-ident.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Hans Verkuil33b687c2008-07-25 05:32:50 -030066#include <linux/videodev2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
69MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
70MODULE_LICENSE("GPL");
71
72/*
73 *
74 * V 4 L 2 D R I V E R H E L P E R A P I
75 *
76 */
77
78/*
79 * Video Standard Operations (contributed by Michael Schimek)
80 */
81
Hans Verkuil9cb23182006-06-18 14:11:08 -030082/* Helper functions for control handling */
83
84/* Check for correctness of the ctrl's value based on the data from
85 struct v4l2_queryctrl and the available menu items. Note that
86 menu_items may be NULL, in that case it is ignored. */
87int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
Hans Verkuil513521e2010-12-29 14:25:52 -030088 const char * const *menu_items)
Hans Verkuil9cb23182006-06-18 14:11:08 -030089{
90 if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
91 return -EINVAL;
92 if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
93 return -EBUSY;
Hans Verkuil6b5a9492009-08-11 18:47:18 -030094 if (qctrl->type == V4L2_CTRL_TYPE_STRING)
95 return 0;
Hans Verkuil9cb23182006-06-18 14:11:08 -030096 if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
97 qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
98 qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
99 return 0;
100 if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
101 return -ERANGE;
102 if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
103 if (menu_items[ctrl->value] == NULL ||
104 menu_items[ctrl->value][0] == '\0')
105 return -EINVAL;
106 }
Hans Verkuilfa4d7092011-05-23 04:07:05 -0300107 if (qctrl->type == V4L2_CTRL_TYPE_BITMASK &&
108 (ctrl->value & ~qctrl->maximum))
109 return -ERANGE;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300110 return 0;
111}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300112EXPORT_SYMBOL(v4l2_ctrl_check);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300113
Hans Verkuil9cb23182006-06-18 14:11:08 -0300114/* Fill in a struct v4l2_queryctrl */
115int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
116{
Hans Verkuil09965172010-08-01 14:32:42 -0300117 const char *name;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300118
Hans Verkuil09965172010-08-01 14:32:42 -0300119 v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
120 &min, &max, &step, &def, &qctrl->flags);
121
Hans Verkuil69028d72008-08-08 07:55:00 -0300122 if (name == NULL)
Hans Verkuil9cb23182006-06-18 14:11:08 -0300123 return -EINVAL;
Hans Verkuil69028d72008-08-08 07:55:00 -0300124
Hans Verkuil9cb23182006-06-18 14:11:08 -0300125 qctrl->minimum = min;
126 qctrl->maximum = max;
127 qctrl->step = step;
128 qctrl->default_value = def;
129 qctrl->reserved[0] = qctrl->reserved[1] = 0;
Hans Verkuilb634a932009-01-17 11:26:59 -0300130 strlcpy(qctrl->name, name, sizeof(qctrl->name));
Hans Verkuil9cb23182006-06-18 14:11:08 -0300131 return 0;
132}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300133EXPORT_SYMBOL(v4l2_ctrl_query_fill);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300134
Hans Verkuil9cb23182006-06-18 14:11:08 -0300135/* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
Hans Verkuile281db582008-08-08 12:43:59 -0300136 the menu. The qctrl pointer may be NULL, in which case it is ignored.
137 If menu_items is NULL, then the menu items are retrieved using
138 v4l2_ctrl_get_menu. */
Hans Verkuil9cb23182006-06-18 14:11:08 -0300139int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
Hans Verkuil513521e2010-12-29 14:25:52 -0300140 const char * const *menu_items)
Hans Verkuil9cb23182006-06-18 14:11:08 -0300141{
142 int i;
143
Hans Verkuil1e551262008-08-08 08:34:19 -0300144 qmenu->reserved = 0;
Hans Verkuile281db582008-08-08 12:43:59 -0300145 if (menu_items == NULL)
146 menu_items = v4l2_ctrl_get_menu(qmenu->id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300147 if (menu_items == NULL ||
148 (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
149 return -EINVAL;
150 for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
151 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
152 return -EINVAL;
Hans Verkuilb634a932009-01-17 11:26:59 -0300153 strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name));
Hans Verkuil9cb23182006-06-18 14:11:08 -0300154 return 0;
155}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300156EXPORT_SYMBOL(v4l2_ctrl_query_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300157
Hans Verkuil1e551262008-08-08 08:34:19 -0300158/* Fill in a struct v4l2_querymenu based on the specified array of valid
159 menu items (terminated by V4L2_CTRL_MENU_IDS_END).
160 Use this if there are 'holes' in the list of valid menu items. */
161int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
162{
Hans Verkuil513521e2010-12-29 14:25:52 -0300163 const char * const *menu_items = v4l2_ctrl_get_menu(qmenu->id);
Hans Verkuil1e551262008-08-08 08:34:19 -0300164
165 qmenu->reserved = 0;
166 if (menu_items == NULL || ids == NULL)
167 return -EINVAL;
168 while (*ids != V4L2_CTRL_MENU_IDS_END) {
169 if (*ids++ == qmenu->index) {
Hans Verkuilb634a932009-01-17 11:26:59 -0300170 strlcpy(qmenu->name, menu_items[qmenu->index],
171 sizeof(qmenu->name));
Hans Verkuil1e551262008-08-08 08:34:19 -0300172 return 0;
173 }
174 }
175 return -EINVAL;
176}
177EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
178
Hans Verkuil9cb23182006-06-18 14:11:08 -0300179/* ctrl_classes points to an array of u32 pointers, the last element is
180 a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
181 Each array must be sorted low to high and belong to the same control
Hans Verkuil2ba58892009-02-13 10:57:48 -0300182 class. The array of u32 pointers must also be sorted, from low class IDs
Hans Verkuil9cb23182006-06-18 14:11:08 -0300183 to high class IDs.
184
185 This function returns the first ID that follows after the given ID.
186 When no more controls are available 0 is returned. */
187u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
188{
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300189 u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300190 const u32 *pctrl;
191
Hans Verkuil9cb23182006-06-18 14:11:08 -0300192 if (ctrl_classes == NULL)
193 return 0;
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300194
195 /* if no query is desired, then check if the ID is part of ctrl_classes */
196 if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
197 /* find class */
198 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
199 ctrl_classes++;
200 if (*ctrl_classes == NULL)
201 return 0;
202 pctrl = *ctrl_classes;
203 /* find control ID */
204 while (*pctrl && *pctrl != id) pctrl++;
205 return *pctrl ? id : 0;
206 }
Hans Verkuil9cb23182006-06-18 14:11:08 -0300207 id &= V4L2_CTRL_ID_MASK;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300208 id++; /* select next control */
209 /* find first class that matches (or is greater than) the class of
210 the ID */
211 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
212 ctrl_classes++;
213 /* no more classes */
214 if (*ctrl_classes == NULL)
215 return 0;
216 pctrl = *ctrl_classes;
217 /* find first ctrl within the class that is >= ID */
218 while (*pctrl && *pctrl < id) pctrl++;
219 if (*pctrl)
220 return *pctrl;
221 /* we are at the end of the controls of the current class. */
222 /* continue with next class if available */
223 ctrl_classes++;
224 if (*ctrl_classes == NULL)
225 return 0;
226 return **ctrl_classes;
227}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300228EXPORT_SYMBOL(v4l2_ctrl_next);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300229
Peter Senna Tschudinffe4db02013-01-19 19:41:31 -0300230#if IS_ENABLED(CONFIG_I2C)
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300231int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match)
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300232{
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300233 int len;
234
235 if (c == NULL || match == NULL)
236 return 0;
237
238 switch (match->type) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300239 case V4L2_CHIP_MATCH_I2C_DRIVER:
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300240 if (c->driver == NULL || c->driver->driver.name == NULL)
241 return 0;
242 len = strlen(c->driver->driver.name);
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300243 return len && !strncmp(c->driver->driver.name, match->name, len);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300244 case V4L2_CHIP_MATCH_I2C_ADDR:
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300245 return c->addr == match->addr;
Hans Verkuil3eef2512013-04-03 04:08:19 -0300246 case V4L2_CHIP_MATCH_SUBDEV:
Hans Verkuil79b0c642013-03-18 12:16:34 -0300247 return 1;
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300248 default:
249 return 0;
250 }
251}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300252EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300253
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300254int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip,
Hans Verkuil3434eb72007-04-27 12:31:08 -0300255 u32 ident, u32 revision)
256{
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300257 if (!v4l2_chip_match_i2c_client(c, &chip->match))
Hans Verkuil3434eb72007-04-27 12:31:08 -0300258 return 0;
259 if (chip->ident == V4L2_IDENT_NONE) {
260 chip->ident = ident;
261 chip->revision = revision;
262 }
263 else {
264 chip->ident = V4L2_IDENT_AMBIGUOUS;
265 chip->revision = 0;
266 }
267 return 0;
268}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300269EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300270
Hans Verkuil9cb23182006-06-18 14:11:08 -0300271/* ----------------------------------------------------------------- */
272
Hans Verkuil78a3b4d2009-04-01 03:41:09 -0300273/* I2C Helper functions */
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300274
Hans Verkuildd991202008-11-23 12:19:45 -0300275
276void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
277 const struct v4l2_subdev_ops *ops)
278{
279 v4l2_subdev_init(sd, ops);
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300280 sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
Hans Verkuildd991202008-11-23 12:19:45 -0300281 /* the owner is the same as the i2c_client's driver owner */
282 sd->owner = client->driver->driver.owner;
283 /* i2c_client and v4l2_subdev point to one another */
284 v4l2_set_subdevdata(sd, client);
285 i2c_set_clientdata(client, sd);
286 /* initialize name */
287 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
288 client->driver->driver.name, i2c_adapter_id(client->adapter),
289 client->addr);
290}
291EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
292
293
294
Hans Verkuile6574f22009-04-01 03:57:53 -0300295/* Load an i2c sub-device. */
Hans Verkuilf0222c72009-06-09 17:12:33 -0300296struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300297 struct i2c_adapter *adapter, struct i2c_board_info *info,
298 const unsigned short *probe_addrs)
Hans Verkuilf0222c72009-06-09 17:12:33 -0300299{
300 struct v4l2_subdev *sd = NULL;
301 struct i2c_client *client;
302
303 BUG_ON(!v4l2_dev);
304
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300305 request_module(I2C_MODULE_PREFIX "%s", info->type);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300306
307 /* Create the i2c client */
308 if (info->addr == 0 && probe_addrs)
Jean Delvare9a942412010-08-11 18:20:56 +0200309 client = i2c_new_probed_device(adapter, info, probe_addrs,
310 NULL);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300311 else
312 client = i2c_new_device(adapter, info);
313
314 /* Note: by loading the module first we are certain that c->driver
315 will be set if the driver was found. If the module was not loaded
316 first, then the i2c core tries to delay-load the module for us,
317 and then c->driver is still NULL until the module is finally
318 loaded. This delay-load mechanism doesn't work if other drivers
319 want to use the i2c device, so explicitly loading the module
320 is the best alternative. */
321 if (client == NULL || client->driver == NULL)
322 goto error;
323
324 /* Lock the module so we can safely get the v4l2_subdev pointer */
325 if (!try_module_get(client->driver->driver.owner))
326 goto error;
327 sd = i2c_get_clientdata(client);
328
329 /* Register with the v4l2_device which increases the module's
330 use count as well. */
331 if (v4l2_device_register_subdev(v4l2_dev, sd))
332 sd = NULL;
333 /* Decrease the module use count to match the first try_module_get. */
334 module_put(client->driver->driver.owner);
335
Hans Verkuilf0222c72009-06-09 17:12:33 -0300336error:
337 /* If we have a client but no subdev, then something went wrong and
338 we must unregister the client. */
339 if (client && sd == NULL)
340 i2c_unregister_device(client);
341 return sd;
342}
343EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
344
Hans Verkuil3c7c9372011-01-08 07:08:02 -0300345struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300346 struct i2c_adapter *adapter, const char *client_type,
Hans Verkuilf0222c72009-06-09 17:12:33 -0300347 u8 addr, const unsigned short *probe_addrs)
348{
349 struct i2c_board_info info;
350
351 /* Setup the i2c board info with the device type and
352 the device address. */
353 memset(&info, 0, sizeof(info));
354 strlcpy(info.type, client_type, sizeof(info.type));
355 info.addr = addr;
Hans Verkuilf0222c72009-06-09 17:12:33 -0300356
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300357 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300358}
Hans Verkuil3c7c9372011-01-08 07:08:02 -0300359EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300360
Hans Verkuilab3731902009-02-21 18:08:41 -0300361/* Return i2c client address of v4l2_subdev. */
362unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
363{
364 struct i2c_client *client = v4l2_get_subdevdata(sd);
365
366 return client ? client->addr : I2C_CLIENT_END;
367}
368EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
369
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300370/* Return a list of I2C tuner addresses to probe. Use only if the tuner
371 addresses are unknown. */
372const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
373{
374 static const unsigned short radio_addrs[] = {
Peter Senna Tschudinffe4db02013-01-19 19:41:31 -0300375#if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761)
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300376 0x10,
377#endif
378 0x60,
379 I2C_CLIENT_END
380 };
381 static const unsigned short demod_addrs[] = {
382 0x42, 0x43, 0x4a, 0x4b,
383 I2C_CLIENT_END
384 };
385 static const unsigned short tv_addrs[] = {
386 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
Hans Verkuil416a7aa2009-05-02 09:42:57 -0300387 0x60, 0x61, 0x62, 0x63, 0x64,
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300388 I2C_CLIENT_END
389 };
390
391 switch (type) {
392 case ADDRS_RADIO:
393 return radio_addrs;
394 case ADDRS_DEMOD:
395 return demod_addrs;
396 case ADDRS_TV:
397 return tv_addrs;
398 case ADDRS_TV_WITH_DEMOD:
399 return tv_addrs + 4;
400 }
401 return NULL;
402}
403EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
404
Trent Piephod8b29962009-06-12 16:31:29 -0300405#endif /* defined(CONFIG_I2C) */
406
Dmitri Belimov85e09212010-03-23 11:23:29 -0300407#if defined(CONFIG_SPI)
408
Michael Jones9d380ad2012-07-26 10:48:25 -0300409/* Load an spi sub-device. */
Dmitri Belimov85e09212010-03-23 11:23:29 -0300410
411void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
412 const struct v4l2_subdev_ops *ops)
413{
414 v4l2_subdev_init(sd, ops);
415 sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
416 /* the owner is the same as the spi_device's driver owner */
417 sd->owner = spi->dev.driver->owner;
418 /* spi_device and v4l2_subdev point to one another */
419 v4l2_set_subdevdata(sd, spi);
420 spi_set_drvdata(spi, sd);
421 /* initialize name */
422 strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
423}
424EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
425
426struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
427 struct spi_master *master, struct spi_board_info *info)
428{
429 struct v4l2_subdev *sd = NULL;
430 struct spi_device *spi = NULL;
431
432 BUG_ON(!v4l2_dev);
433
Alan Cox07ca4182012-09-17 06:51:27 -0300434 if (info->modalias[0])
Dmitri Belimov85e09212010-03-23 11:23:29 -0300435 request_module(info->modalias);
436
437 spi = spi_new_device(master, info);
438
439 if (spi == NULL || spi->dev.driver == NULL)
440 goto error;
441
442 if (!try_module_get(spi->dev.driver->owner))
443 goto error;
444
445 sd = spi_get_drvdata(spi);
446
447 /* Register with the v4l2_device which increases the module's
448 use count as well. */
449 if (v4l2_device_register_subdev(v4l2_dev, sd))
450 sd = NULL;
451
452 /* Decrease the module use count to match the first try_module_get. */
453 module_put(spi->dev.driver->owner);
454
455error:
456 /* If we have a client but no subdev, then something went wrong and
457 we must unregister the client. */
458 if (spi && sd == NULL)
459 spi_unregister_device(spi);
460
461 return sd;
462}
463EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
464
465#endif /* defined(CONFIG_SPI) */
466
Trent Piephob0d31592009-05-30 21:45:46 -0300467/* Clamp x to be between min and max, aligned to a multiple of 2^align. min
468 * and max don't have to be aligned, but there must be at least one valid
469 * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
470 * of 16 between 17 and 31. */
471static unsigned int clamp_align(unsigned int x, unsigned int min,
472 unsigned int max, unsigned int align)
473{
474 /* Bits that must be zero to be aligned */
475 unsigned int mask = ~((1 << align) - 1);
476
477 /* Round to nearest aligned value */
478 if (align)
479 x = (x + (1 << (align - 1))) & mask;
480
481 /* Clamp to aligned value of min and max */
482 if (x < min)
483 x = (min + ~mask) & mask;
484 else if (x > max)
485 x = max & mask;
486
487 return x;
488}
489
490/* Bound an image to have a width between wmin and wmax, and height between
491 * hmin and hmax, inclusive. Additionally, the width will be a multiple of
492 * 2^walign, the height will be a multiple of 2^halign, and the overall size
493 * (width*height) will be a multiple of 2^salign. The image may be shrunk
494 * or enlarged to fit the alignment constraints.
495 *
496 * The width or height maximum must not be smaller than the corresponding
497 * minimum. The alignments must not be so high there are no possible image
498 * sizes within the allowed bounds. wmin and hmin must be at least 1
499 * (don't use 0). If you don't care about a certain alignment, specify 0,
500 * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
501 * you only want to adjust downward, specify a maximum that's the same as
502 * the initial value.
503 */
504void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
505 unsigned int walign,
506 u32 *h, unsigned int hmin, unsigned int hmax,
507 unsigned int halign, unsigned int salign)
508{
509 *w = clamp_align(*w, wmin, wmax, walign);
510 *h = clamp_align(*h, hmin, hmax, halign);
511
512 /* Usually we don't need to align the size and are done now. */
513 if (!salign)
514 return;
515
516 /* How much alignment do we have? */
517 walign = __ffs(*w);
518 halign = __ffs(*h);
519 /* Enough to satisfy the image alignment? */
520 if (walign + halign < salign) {
521 /* Max walign where there is still a valid width */
522 unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
523 /* Max halign where there is still a valid height */
524 unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
525
526 /* up the smaller alignment until we have enough */
527 do {
528 if (halign >= hmaxa ||
529 (walign <= halign && walign < wmaxa)) {
530 *w = clamp_align(*w, wmin, wmax, walign + 1);
531 walign = __ffs(*w);
532 } else {
533 *h = clamp_align(*h, hmin, hmax, halign + 1);
534 halign = __ffs(*h);
535 }
536 } while (halign + walign < salign);
537 }
538}
539EXPORT_SYMBOL_GPL(v4l_bound_align_image);
Muralidharan Karicheri2e535ed2009-12-10 04:39:47 -0300540
541/**
Hans Verkuilc2a667f2012-08-10 06:09:26 -0300542 * v4l_match_dv_timings - check if two timings match
543 * @t1 - compare this v4l2_dv_timings struct...
544 * @t2 - with this struct.
545 * @pclock_delta - the allowed pixelclock deviation.
546 *
547 * Compare t1 with t2 with a given margin of error for the pixelclock.
548 */
549bool v4l_match_dv_timings(const struct v4l2_dv_timings *t1,
550 const struct v4l2_dv_timings *t2,
551 unsigned pclock_delta)
552{
553 if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
554 return false;
555 if (t1->bt.width == t2->bt.width &&
556 t1->bt.height == t2->bt.height &&
557 t1->bt.interlaced == t2->bt.interlaced &&
558 t1->bt.polarities == t2->bt.polarities &&
559 t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
560 t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
561 t1->bt.hfrontporch == t2->bt.hfrontporch &&
562 t1->bt.vfrontporch == t2->bt.vfrontporch &&
563 t1->bt.vsync == t2->bt.vsync &&
564 t1->bt.vbackporch == t2->bt.vbackporch &&
565 (!t1->bt.interlaced ||
566 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
567 t1->bt.il_vsync == t2->bt.il_vsync &&
568 t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
569 return true;
570 return false;
571}
572EXPORT_SYMBOL_GPL(v4l_match_dv_timings);
573
Hans Verkuilc61bd6a2012-08-10 06:11:12 -0300574/*
575 * CVT defines
576 * Based on Coordinated Video Timings Standard
577 * version 1.1 September 10, 2003
578 */
579
580#define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
581
582/* Normal blanking */
583#define CVT_MIN_V_BPORCH 7 /* lines */
584#define CVT_MIN_V_PORCH_RND 3 /* lines */
585#define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
586
587/* Normal blanking for CVT uses GTF to calculate horizontal blanking */
588#define CVT_CELL_GRAN 8 /* character cell granularity */
589#define CVT_M 600 /* blanking formula gradient */
590#define CVT_C 40 /* blanking formula offset */
591#define CVT_K 128 /* blanking formula scaling factor */
592#define CVT_J 20 /* blanking formula scaling factor */
593#define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
594#define CVT_M_PRIME (CVT_K * CVT_M / 256)
595
596/* Reduced Blanking */
597#define CVT_RB_MIN_V_BPORCH 7 /* lines */
598#define CVT_RB_V_FPORCH 3 /* lines */
599#define CVT_RB_MIN_V_BLANK 460 /* us */
600#define CVT_RB_H_SYNC 32 /* pixels */
601#define CVT_RB_H_BPORCH 80 /* pixels */
602#define CVT_RB_H_BLANK 160 /* pixels */
603
604/** v4l2_detect_cvt - detect if the given timings follow the CVT standard
605 * @frame_height - the total height of the frame (including blanking) in lines.
606 * @hfreq - the horizontal frequency in Hz.
607 * @vsync - the height of the vertical sync in lines.
608 * @polarities - the horizontal and vertical polarities (same as struct
609 * v4l2_bt_timings polarities).
610 * @fmt - the resulting timings.
611 *
612 * This function will attempt to detect if the given values correspond to a
613 * valid CVT format. If so, then it will return true, and fmt will be filled
614 * in with the found CVT timings.
615 */
616bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
617 u32 polarities, struct v4l2_dv_timings *fmt)
618{
619 int v_fp, v_bp, h_fp, h_bp, hsync;
620 int frame_width, image_height, image_width;
621 bool reduced_blanking;
622 unsigned pix_clk;
623
624 if (vsync < 4 || vsync > 7)
625 return false;
626
627 if (polarities == V4L2_DV_VSYNC_POS_POL)
628 reduced_blanking = false;
629 else if (polarities == V4L2_DV_HSYNC_POS_POL)
630 reduced_blanking = true;
631 else
632 return false;
633
634 /* Vertical */
635 if (reduced_blanking) {
636 v_fp = CVT_RB_V_FPORCH;
637 v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 999999) / 1000000;
638 v_bp -= vsync + v_fp;
639
640 if (v_bp < CVT_RB_MIN_V_BPORCH)
641 v_bp = CVT_RB_MIN_V_BPORCH;
642 } else {
643 v_fp = CVT_MIN_V_PORCH_RND;
644 v_bp = (CVT_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
645
646 if (v_bp < CVT_MIN_V_BPORCH)
647 v_bp = CVT_MIN_V_BPORCH;
648 }
649 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
650
651 /* Aspect ratio based on vsync */
652 switch (vsync) {
653 case 4:
654 image_width = (image_height * 4) / 3;
655 break;
656 case 5:
657 image_width = (image_height * 16) / 9;
658 break;
659 case 6:
660 image_width = (image_height * 16) / 10;
661 break;
662 case 7:
663 /* special case */
664 if (image_height == 1024)
665 image_width = (image_height * 5) / 4;
666 else if (image_height == 768)
667 image_width = (image_height * 15) / 9;
668 else
669 return false;
670 break;
671 default:
672 return false;
673 }
674
675 image_width = image_width & ~7;
676
677 /* Horizontal */
678 if (reduced_blanking) {
679 pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq;
680 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
681
682 h_bp = CVT_RB_H_BPORCH;
683 hsync = CVT_RB_H_SYNC;
684 h_fp = CVT_RB_H_BLANK - h_bp - hsync;
685
686 frame_width = image_width + CVT_RB_H_BLANK;
687 } else {
688 int h_blank;
689 unsigned ideal_duty_cycle = CVT_C_PRIME - (CVT_M_PRIME * 1000) / hfreq;
690
691 h_blank = (image_width * ideal_duty_cycle + (100 - ideal_duty_cycle) / 2) /
692 (100 - ideal_duty_cycle);
693 h_blank = h_blank - h_blank % (2 * CVT_CELL_GRAN);
694
695 if (h_blank * 100 / image_width < 20) {
696 h_blank = image_width / 5;
697 h_blank = (h_blank + 0x7) & ~0x7;
698 }
699
700 pix_clk = (image_width + h_blank) * hfreq;
701 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
702
703 h_bp = h_blank / 2;
704 frame_width = image_width + h_blank;
705
706 hsync = (frame_width * 8 + 50) / 100;
707 hsync = hsync - hsync % CVT_CELL_GRAN;
708 h_fp = h_blank - hsync - h_bp;
709 }
710
711 fmt->bt.polarities = polarities;
712 fmt->bt.width = image_width;
713 fmt->bt.height = image_height;
714 fmt->bt.hfrontporch = h_fp;
715 fmt->bt.vfrontporch = v_fp;
716 fmt->bt.hsync = hsync;
717 fmt->bt.vsync = vsync;
718 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
719 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
720 fmt->bt.pixelclock = pix_clk;
721 fmt->bt.standards = V4L2_DV_BT_STD_CVT;
722 if (reduced_blanking)
723 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
724 return true;
725}
726EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
727
728/*
729 * GTF defines
730 * Based on Generalized Timing Formula Standard
731 * Version 1.1 September 2, 1999
732 */
733
734#define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
735
736#define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
737#define GTF_V_FP 1 /* vertical front porch (lines) */
738#define GTF_CELL_GRAN 8 /* character cell granularity */
739
740/* Default */
741#define GTF_D_M 600 /* blanking formula gradient */
742#define GTF_D_C 40 /* blanking formula offset */
743#define GTF_D_K 128 /* blanking formula scaling factor */
744#define GTF_D_J 20 /* blanking formula scaling factor */
745#define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
746#define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
747
748/* Secondary */
749#define GTF_S_M 3600 /* blanking formula gradient */
750#define GTF_S_C 40 /* blanking formula offset */
751#define GTF_S_K 128 /* blanking formula scaling factor */
752#define GTF_S_J 35 /* blanking formula scaling factor */
753#define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
754#define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
755
756/** v4l2_detect_gtf - detect if the given timings follow the GTF standard
757 * @frame_height - the total height of the frame (including blanking) in lines.
758 * @hfreq - the horizontal frequency in Hz.
759 * @vsync - the height of the vertical sync in lines.
760 * @polarities - the horizontal and vertical polarities (same as struct
761 * v4l2_bt_timings polarities).
762 * @aspect - preferred aspect ratio. GTF has no method of determining the
763 * aspect ratio in order to derive the image width from the
764 * image height, so it has to be passed explicitly. Usually
765 * the native screen aspect ratio is used for this. If it
766 * is not filled in correctly, then 16:9 will be assumed.
767 * @fmt - the resulting timings.
768 *
769 * This function will attempt to detect if the given values correspond to a
770 * valid GTF format. If so, then it will return true, and fmt will be filled
771 * in with the found GTF timings.
772 */
773bool v4l2_detect_gtf(unsigned frame_height,
774 unsigned hfreq,
775 unsigned vsync,
776 u32 polarities,
777 struct v4l2_fract aspect,
778 struct v4l2_dv_timings *fmt)
779{
780 int pix_clk;
Mauro Carvalho Chehab685a39b2012-10-27 16:26:15 -0300781 int v_fp, v_bp, h_fp, hsync;
Hans Verkuilc61bd6a2012-08-10 06:11:12 -0300782 int frame_width, image_height, image_width;
783 bool default_gtf;
784 int h_blank;
785
786 if (vsync != 3)
787 return false;
788
789 if (polarities == V4L2_DV_VSYNC_POS_POL)
790 default_gtf = true;
791 else if (polarities == V4L2_DV_HSYNC_POS_POL)
792 default_gtf = false;
793 else
794 return false;
795
796 /* Vertical */
797 v_fp = GTF_V_FP;
798 v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
799 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
800
801 if (aspect.numerator == 0 || aspect.denominator == 0) {
802 aspect.numerator = 16;
803 aspect.denominator = 9;
804 }
805 image_width = ((image_height * aspect.numerator) / aspect.denominator);
806
807 /* Horizontal */
808 if (default_gtf)
809 h_blank = ((image_width * GTF_D_C_PRIME * hfreq) -
810 (image_width * GTF_D_M_PRIME * 1000) +
811 (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) /
812 (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000);
813 else
814 h_blank = ((image_width * GTF_S_C_PRIME * hfreq) -
815 (image_width * GTF_S_M_PRIME * 1000) +
816 (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) /
817 (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000);
818
819 h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN);
820 frame_width = image_width + h_blank;
821
822 pix_clk = (image_width + h_blank) * hfreq;
823 pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
824
825 hsync = (frame_width * 8 + 50) / 100;
826 hsync = hsync - hsync % GTF_CELL_GRAN;
827
828 h_fp = h_blank / 2 - hsync;
Hans Verkuilc61bd6a2012-08-10 06:11:12 -0300829
830 fmt->bt.polarities = polarities;
831 fmt->bt.width = image_width;
832 fmt->bt.height = image_height;
833 fmt->bt.hfrontporch = h_fp;
834 fmt->bt.vfrontporch = v_fp;
835 fmt->bt.hsync = hsync;
836 fmt->bt.vsync = vsync;
837 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
838 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
839 fmt->bt.pixelclock = pix_clk;
840 fmt->bt.standards = V4L2_DV_BT_STD_GTF;
841 if (!default_gtf)
842 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
843 return true;
844}
845EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
846
847/** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
848 * 0x15 and 0x16 from the EDID.
849 * @hor_landscape - byte 0x15 from the EDID.
850 * @vert_portrait - byte 0x16 from the EDID.
851 *
852 * Determines the aspect ratio from the EDID.
853 * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
854 * "Horizontal and Vertical Screen Size or Aspect Ratio"
855 */
856struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
857{
858 struct v4l2_fract aspect = { 16, 9 };
859 u32 tmp;
860 u8 ratio;
861
862 /* Nothing filled in, fallback to 16:9 */
863 if (!hor_landscape && !vert_portrait)
864 return aspect;
865 /* Both filled in, so they are interpreted as the screen size in cm */
866 if (hor_landscape && vert_portrait) {
867 aspect.numerator = hor_landscape;
868 aspect.denominator = vert_portrait;
869 return aspect;
870 }
871 /* Only one is filled in, so interpret them as a ratio:
872 (val + 99) / 100 */
873 ratio = hor_landscape | vert_portrait;
874 /* Change some rounded values into the exact aspect ratio */
875 if (ratio == 79) {
876 aspect.numerator = 16;
877 aspect.denominator = 9;
878 } else if (ratio == 34) {
879 aspect.numerator = 4;
880 aspect.numerator = 3;
881 } else if (ratio == 68) {
882 aspect.numerator = 15;
883 aspect.numerator = 9;
884 } else {
885 aspect.numerator = hor_landscape + 99;
886 aspect.denominator = 100;
887 }
888 if (hor_landscape)
889 return aspect;
890 /* The aspect ratio is for portrait, so swap numerator and denominator */
891 tmp = aspect.denominator;
892 aspect.denominator = aspect.numerator;
893 aspect.numerator = tmp;
894 return aspect;
895}
896EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
897
Hans Verkuil3fd8e642010-09-30 09:29:37 -0300898const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
899 const struct v4l2_discrete_probe *probe,
900 s32 width, s32 height)
Guennadi Liakhovetski79c6ff92010-08-27 13:41:44 -0300901{
902 int i;
903 u32 error, min_error = UINT_MAX;
Hans Verkuil3fd8e642010-09-30 09:29:37 -0300904 const struct v4l2_frmsize_discrete *size, *best = NULL;
Guennadi Liakhovetski79c6ff92010-08-27 13:41:44 -0300905
906 if (!probe)
907 return best;
908
909 for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) {
910 error = abs(size->width - width) + abs(size->height - height);
911 if (error < min_error) {
912 min_error = error;
913 best = size;
914 }
915 if (!error)
916 break;
917 }
918
919 return best;
920}
921EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
Sakari Ailusabd23292012-09-15 07:51:47 -0300922
923void v4l2_get_timestamp(struct timeval *tv)
924{
925 struct timespec ts;
926
927 ktime_get_ts(&ts);
928 tv->tv_sec = ts.tv_sec;
929 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
930}
931EXPORT_SYMBOL_GPL(v4l2_get_timestamp);