Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Chehab | 43db48d | 2007-01-09 11:20:59 -0300 | [diff] [blame] | 15 | * Author: Bill Dirks <bill@thedirks.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * 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 Cox | d9b0144 | 2008-10-27 15:13:47 -0300 | [diff] [blame] | 31 | * Author: Alan Cox, <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | * |
| 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 Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 40 | * Iñaki García Etxebarria <garetxe@euskalnet.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/module.h> |
| 48 | #include <linux/types.h> |
| 49 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/mm.h> |
| 51 | #include <linux/string.h> |
| 52 | #include <linux/errno.h> |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 53 | #include <linux/i2c.h> |
Dmitri Belimov | 85e0921 | 2010-03-23 11:23:29 -0300 | [diff] [blame] | 54 | #if defined(CONFIG_SPI) |
| 55 | #include <linux/spi/spi.h> |
| 56 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <asm/pgtable.h> |
| 59 | #include <asm/io.h> |
| 60 | #include <asm/div64.h> |
Michael Krufky | 5e453dc | 2006-01-09 15:32:31 -0200 | [diff] [blame] | 61 | #include <media/v4l2-common.h> |
Hans Verkuil | dd99120 | 2008-11-23 12:19:45 -0300 | [diff] [blame] | 62 | #include <media/v4l2-device.h> |
Hans Verkuil | 0996517 | 2010-08-01 14:32:42 -0300 | [diff] [blame] | 63 | #include <media/v4l2-ctrls.h> |
Hans Verkuil | 3434eb7 | 2007-04-27 12:31:08 -0300 | [diff] [blame] | 64 | #include <media/v4l2-chip-ident.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Hans Verkuil | 33b687c | 2008-07-25 05:32:50 -0300 | [diff] [blame] | 66 | #include <linux/videodev2.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr"); |
| 69 | MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers"); |
| 70 | MODULE_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 Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 82 | /* 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. */ |
| 87 | int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl, |
Hans Verkuil | 513521e | 2010-12-29 14:25:52 -0300 | [diff] [blame] | 88 | const char * const *menu_items) |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 89 | { |
| 90 | if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED) |
| 91 | return -EINVAL; |
| 92 | if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED) |
| 93 | return -EBUSY; |
Hans Verkuil | 6b5a949 | 2009-08-11 18:47:18 -0300 | [diff] [blame] | 94 | if (qctrl->type == V4L2_CTRL_TYPE_STRING) |
| 95 | return 0; |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 96 | 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 Verkuil | fa4d709 | 2011-05-23 04:07:05 -0300 | [diff] [blame] | 107 | if (qctrl->type == V4L2_CTRL_TYPE_BITMASK && |
| 108 | (ctrl->value & ~qctrl->maximum)) |
| 109 | return -ERANGE; |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 110 | return 0; |
| 111 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 112 | EXPORT_SYMBOL(v4l2_ctrl_check); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 113 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 114 | /* Fill in a struct v4l2_queryctrl */ |
| 115 | int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def) |
| 116 | { |
Hans Verkuil | 0996517 | 2010-08-01 14:32:42 -0300 | [diff] [blame] | 117 | const char *name; |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 118 | |
Hans Verkuil | 0996517 | 2010-08-01 14:32:42 -0300 | [diff] [blame] | 119 | v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type, |
| 120 | &min, &max, &step, &def, &qctrl->flags); |
| 121 | |
Hans Verkuil | 69028d7 | 2008-08-08 07:55:00 -0300 | [diff] [blame] | 122 | if (name == NULL) |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 123 | return -EINVAL; |
Hans Verkuil | 69028d7 | 2008-08-08 07:55:00 -0300 | [diff] [blame] | 124 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 125 | 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 Verkuil | b634a93 | 2009-01-17 11:26:59 -0300 | [diff] [blame] | 130 | strlcpy(qctrl->name, name, sizeof(qctrl->name)); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 131 | return 0; |
| 132 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 133 | EXPORT_SYMBOL(v4l2_ctrl_query_fill); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 134 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 135 | /* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 136 | 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 Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 139 | int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl, |
Hans Verkuil | 513521e | 2010-12-29 14:25:52 -0300 | [diff] [blame] | 140 | const char * const *menu_items) |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 141 | { |
| 142 | int i; |
| 143 | |
Hans Verkuil | 1e55126 | 2008-08-08 08:34:19 -0300 | [diff] [blame] | 144 | qmenu->reserved = 0; |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 145 | if (menu_items == NULL) |
| 146 | menu_items = v4l2_ctrl_get_menu(qmenu->id); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 147 | 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 Verkuil | b634a93 | 2009-01-17 11:26:59 -0300 | [diff] [blame] | 153 | strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name)); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 154 | return 0; |
| 155 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 156 | EXPORT_SYMBOL(v4l2_ctrl_query_menu); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 157 | |
Hans Verkuil | 1e55126 | 2008-08-08 08:34:19 -0300 | [diff] [blame] | 158 | /* 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. */ |
| 161 | int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids) |
| 162 | { |
Hans Verkuil | 513521e | 2010-12-29 14:25:52 -0300 | [diff] [blame] | 163 | const char * const *menu_items = v4l2_ctrl_get_menu(qmenu->id); |
Hans Verkuil | 1e55126 | 2008-08-08 08:34:19 -0300 | [diff] [blame] | 164 | |
| 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 Verkuil | b634a93 | 2009-01-17 11:26:59 -0300 | [diff] [blame] | 170 | strlcpy(qmenu->name, menu_items[qmenu->index], |
| 171 | sizeof(qmenu->name)); |
Hans Verkuil | 1e55126 | 2008-08-08 08:34:19 -0300 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | } |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items); |
| 178 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 179 | /* 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 Verkuil | 2ba5889 | 2009-02-13 10:57:48 -0300 | [diff] [blame] | 182 | class. The array of u32 pointers must also be sorted, from low class IDs |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 183 | 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. */ |
| 187 | u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id) |
| 188 | { |
Hans Verkuil | a46c5fb | 2007-07-19 04:53:36 -0300 | [diff] [blame] | 189 | u32 ctrl_class = V4L2_CTRL_ID2CLASS(id); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 190 | const u32 *pctrl; |
| 191 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 192 | if (ctrl_classes == NULL) |
| 193 | return 0; |
Hans Verkuil | a46c5fb | 2007-07-19 04:53:36 -0300 | [diff] [blame] | 194 | |
| 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 Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 207 | id &= V4L2_CTRL_ID_MASK; |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 208 | 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 Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 228 | EXPORT_SYMBOL(v4l2_ctrl_next); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 229 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 230 | int v4l2_chip_match_host(const struct v4l2_dbg_match *match) |
Mauro Carvalho Chehab | a925447 | 2008-01-29 18:32:35 -0300 | [diff] [blame] | 231 | { |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 232 | switch (match->type) { |
Mauro Carvalho Chehab | a925447 | 2008-01-29 18:32:35 -0300 | [diff] [blame] | 233 | case V4L2_CHIP_MATCH_HOST: |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 234 | return match->addr == 0; |
Mauro Carvalho Chehab | a925447 | 2008-01-29 18:32:35 -0300 | [diff] [blame] | 235 | default: |
| 236 | return 0; |
| 237 | } |
| 238 | } |
| 239 | EXPORT_SYMBOL(v4l2_chip_match_host); |
| 240 | |
Peter Senna Tschudin | ffe4db0 | 2013-01-19 19:41:31 -0300 | [diff] [blame] | 241 | #if IS_ENABLED(CONFIG_I2C) |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 242 | int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match) |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 243 | { |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 244 | int len; |
| 245 | |
| 246 | if (c == NULL || match == NULL) |
| 247 | return 0; |
| 248 | |
| 249 | switch (match->type) { |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 250 | case V4L2_CHIP_MATCH_I2C_DRIVER: |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 251 | if (c->driver == NULL || c->driver->driver.name == NULL) |
| 252 | return 0; |
| 253 | len = strlen(c->driver->driver.name); |
| 254 | /* legacy drivers have a ' suffix, don't try to match that */ |
| 255 | if (len && c->driver->driver.name[len - 1] == '\'') |
| 256 | len--; |
| 257 | return len && !strncmp(c->driver->driver.name, match->name, len); |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 258 | case V4L2_CHIP_MATCH_I2C_ADDR: |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 259 | return c->addr == match->addr; |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 260 | default: |
| 261 | return 0; |
| 262 | } |
| 263 | } |
Mauro Carvalho Chehab | a925447 | 2008-01-29 18:32:35 -0300 | [diff] [blame] | 264 | EXPORT_SYMBOL(v4l2_chip_match_i2c_client); |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 265 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 266 | int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip, |
Hans Verkuil | 3434eb7 | 2007-04-27 12:31:08 -0300 | [diff] [blame] | 267 | u32 ident, u32 revision) |
| 268 | { |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 269 | if (!v4l2_chip_match_i2c_client(c, &chip->match)) |
Hans Verkuil | 3434eb7 | 2007-04-27 12:31:08 -0300 | [diff] [blame] | 270 | return 0; |
| 271 | if (chip->ident == V4L2_IDENT_NONE) { |
| 272 | chip->ident = ident; |
| 273 | chip->revision = revision; |
| 274 | } |
| 275 | else { |
| 276 | chip->ident = V4L2_IDENT_AMBIGUOUS; |
| 277 | chip->revision = 0; |
| 278 | } |
| 279 | return 0; |
| 280 | } |
Mauro Carvalho Chehab | a925447 | 2008-01-29 18:32:35 -0300 | [diff] [blame] | 281 | EXPORT_SYMBOL(v4l2_chip_ident_i2c_client); |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 282 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 283 | /* ----------------------------------------------------------------- */ |
| 284 | |
Hans Verkuil | 78a3b4d | 2009-04-01 03:41:09 -0300 | [diff] [blame] | 285 | /* I2C Helper functions */ |
Hans Verkuil | 8ffbc65 | 2007-09-12 08:32:50 -0300 | [diff] [blame] | 286 | |
Hans Verkuil | dd99120 | 2008-11-23 12:19:45 -0300 | [diff] [blame] | 287 | |
| 288 | void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client, |
| 289 | const struct v4l2_subdev_ops *ops) |
| 290 | { |
| 291 | v4l2_subdev_init(sd, ops); |
Hans Verkuil | b5b2b7e | 2009-05-02 10:58:51 -0300 | [diff] [blame] | 292 | sd->flags |= V4L2_SUBDEV_FL_IS_I2C; |
Hans Verkuil | dd99120 | 2008-11-23 12:19:45 -0300 | [diff] [blame] | 293 | /* the owner is the same as the i2c_client's driver owner */ |
| 294 | sd->owner = client->driver->driver.owner; |
| 295 | /* i2c_client and v4l2_subdev point to one another */ |
| 296 | v4l2_set_subdevdata(sd, client); |
| 297 | i2c_set_clientdata(client, sd); |
| 298 | /* initialize name */ |
| 299 | snprintf(sd->name, sizeof(sd->name), "%s %d-%04x", |
| 300 | client->driver->driver.name, i2c_adapter_id(client->adapter), |
| 301 | client->addr); |
| 302 | } |
| 303 | EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init); |
| 304 | |
| 305 | |
| 306 | |
Hans Verkuil | e6574f2 | 2009-04-01 03:57:53 -0300 | [diff] [blame] | 307 | /* Load an i2c sub-device. */ |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 308 | struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 309 | struct i2c_adapter *adapter, struct i2c_board_info *info, |
| 310 | const unsigned short *probe_addrs) |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 311 | { |
| 312 | struct v4l2_subdev *sd = NULL; |
| 313 | struct i2c_client *client; |
| 314 | |
| 315 | BUG_ON(!v4l2_dev); |
| 316 | |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 317 | request_module(I2C_MODULE_PREFIX "%s", info->type); |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 318 | |
| 319 | /* Create the i2c client */ |
| 320 | if (info->addr == 0 && probe_addrs) |
Jean Delvare | 9a94241 | 2010-08-11 18:20:56 +0200 | [diff] [blame] | 321 | client = i2c_new_probed_device(adapter, info, probe_addrs, |
| 322 | NULL); |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 323 | else |
| 324 | client = i2c_new_device(adapter, info); |
| 325 | |
| 326 | /* Note: by loading the module first we are certain that c->driver |
| 327 | will be set if the driver was found. If the module was not loaded |
| 328 | first, then the i2c core tries to delay-load the module for us, |
| 329 | and then c->driver is still NULL until the module is finally |
| 330 | loaded. This delay-load mechanism doesn't work if other drivers |
| 331 | want to use the i2c device, so explicitly loading the module |
| 332 | is the best alternative. */ |
| 333 | if (client == NULL || client->driver == NULL) |
| 334 | goto error; |
| 335 | |
| 336 | /* Lock the module so we can safely get the v4l2_subdev pointer */ |
| 337 | if (!try_module_get(client->driver->driver.owner)) |
| 338 | goto error; |
| 339 | sd = i2c_get_clientdata(client); |
| 340 | |
| 341 | /* Register with the v4l2_device which increases the module's |
| 342 | use count as well. */ |
| 343 | if (v4l2_device_register_subdev(v4l2_dev, sd)) |
| 344 | sd = NULL; |
| 345 | /* Decrease the module use count to match the first try_module_get. */ |
| 346 | module_put(client->driver->driver.owner); |
| 347 | |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 348 | error: |
| 349 | /* If we have a client but no subdev, then something went wrong and |
| 350 | we must unregister the client. */ |
| 351 | if (client && sd == NULL) |
| 352 | i2c_unregister_device(client); |
| 353 | return sd; |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board); |
| 356 | |
Hans Verkuil | 3c7c937 | 2011-01-08 07:08:02 -0300 | [diff] [blame] | 357 | struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 358 | struct i2c_adapter *adapter, const char *client_type, |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 359 | u8 addr, const unsigned short *probe_addrs) |
| 360 | { |
| 361 | struct i2c_board_info info; |
| 362 | |
| 363 | /* Setup the i2c board info with the device type and |
| 364 | the device address. */ |
| 365 | memset(&info, 0, sizeof(info)); |
| 366 | strlcpy(info.type, client_type, sizeof(info.type)); |
| 367 | info.addr = addr; |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 368 | |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 369 | return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs); |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 370 | } |
Hans Verkuil | 3c7c937 | 2011-01-08 07:08:02 -0300 | [diff] [blame] | 371 | EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev); |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 372 | |
Hans Verkuil | ab373190 | 2009-02-21 18:08:41 -0300 | [diff] [blame] | 373 | /* Return i2c client address of v4l2_subdev. */ |
| 374 | unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd) |
| 375 | { |
| 376 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 377 | |
| 378 | return client ? client->addr : I2C_CLIENT_END; |
| 379 | } |
| 380 | EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr); |
| 381 | |
Hans Verkuil | c7d29e2 | 2009-01-18 19:37:59 -0300 | [diff] [blame] | 382 | /* Return a list of I2C tuner addresses to probe. Use only if the tuner |
| 383 | addresses are unknown. */ |
| 384 | const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type) |
| 385 | { |
| 386 | static const unsigned short radio_addrs[] = { |
Peter Senna Tschudin | ffe4db0 | 2013-01-19 19:41:31 -0300 | [diff] [blame] | 387 | #if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761) |
Hans Verkuil | c7d29e2 | 2009-01-18 19:37:59 -0300 | [diff] [blame] | 388 | 0x10, |
| 389 | #endif |
| 390 | 0x60, |
| 391 | I2C_CLIENT_END |
| 392 | }; |
| 393 | static const unsigned short demod_addrs[] = { |
| 394 | 0x42, 0x43, 0x4a, 0x4b, |
| 395 | I2C_CLIENT_END |
| 396 | }; |
| 397 | static const unsigned short tv_addrs[] = { |
| 398 | 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */ |
Hans Verkuil | 416a7aa | 2009-05-02 09:42:57 -0300 | [diff] [blame] | 399 | 0x60, 0x61, 0x62, 0x63, 0x64, |
Hans Verkuil | c7d29e2 | 2009-01-18 19:37:59 -0300 | [diff] [blame] | 400 | I2C_CLIENT_END |
| 401 | }; |
| 402 | |
| 403 | switch (type) { |
| 404 | case ADDRS_RADIO: |
| 405 | return radio_addrs; |
| 406 | case ADDRS_DEMOD: |
| 407 | return demod_addrs; |
| 408 | case ADDRS_TV: |
| 409 | return tv_addrs; |
| 410 | case ADDRS_TV_WITH_DEMOD: |
| 411 | return tv_addrs + 4; |
| 412 | } |
| 413 | return NULL; |
| 414 | } |
| 415 | EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs); |
| 416 | |
Trent Piepho | d8b2996 | 2009-06-12 16:31:29 -0300 | [diff] [blame] | 417 | #endif /* defined(CONFIG_I2C) */ |
| 418 | |
Dmitri Belimov | 85e0921 | 2010-03-23 11:23:29 -0300 | [diff] [blame] | 419 | #if defined(CONFIG_SPI) |
| 420 | |
Michael Jones | 9d380ad | 2012-07-26 10:48:25 -0300 | [diff] [blame] | 421 | /* Load an spi sub-device. */ |
Dmitri Belimov | 85e0921 | 2010-03-23 11:23:29 -0300 | [diff] [blame] | 422 | |
| 423 | void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi, |
| 424 | const struct v4l2_subdev_ops *ops) |
| 425 | { |
| 426 | v4l2_subdev_init(sd, ops); |
| 427 | sd->flags |= V4L2_SUBDEV_FL_IS_SPI; |
| 428 | /* the owner is the same as the spi_device's driver owner */ |
| 429 | sd->owner = spi->dev.driver->owner; |
| 430 | /* spi_device and v4l2_subdev point to one another */ |
| 431 | v4l2_set_subdevdata(sd, spi); |
| 432 | spi_set_drvdata(spi, sd); |
| 433 | /* initialize name */ |
| 434 | strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name)); |
| 435 | } |
| 436 | EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init); |
| 437 | |
| 438 | struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev, |
| 439 | struct spi_master *master, struct spi_board_info *info) |
| 440 | { |
| 441 | struct v4l2_subdev *sd = NULL; |
| 442 | struct spi_device *spi = NULL; |
| 443 | |
| 444 | BUG_ON(!v4l2_dev); |
| 445 | |
Alan Cox | 07ca418 | 2012-09-17 06:51:27 -0300 | [diff] [blame] | 446 | if (info->modalias[0]) |
Dmitri Belimov | 85e0921 | 2010-03-23 11:23:29 -0300 | [diff] [blame] | 447 | request_module(info->modalias); |
| 448 | |
| 449 | spi = spi_new_device(master, info); |
| 450 | |
| 451 | if (spi == NULL || spi->dev.driver == NULL) |
| 452 | goto error; |
| 453 | |
| 454 | if (!try_module_get(spi->dev.driver->owner)) |
| 455 | goto error; |
| 456 | |
| 457 | sd = spi_get_drvdata(spi); |
| 458 | |
| 459 | /* Register with the v4l2_device which increases the module's |
| 460 | use count as well. */ |
| 461 | if (v4l2_device_register_subdev(v4l2_dev, sd)) |
| 462 | sd = NULL; |
| 463 | |
| 464 | /* Decrease the module use count to match the first try_module_get. */ |
| 465 | module_put(spi->dev.driver->owner); |
| 466 | |
| 467 | error: |
| 468 | /* If we have a client but no subdev, then something went wrong and |
| 469 | we must unregister the client. */ |
| 470 | if (spi && sd == NULL) |
| 471 | spi_unregister_device(spi); |
| 472 | |
| 473 | return sd; |
| 474 | } |
| 475 | EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev); |
| 476 | |
| 477 | #endif /* defined(CONFIG_SPI) */ |
| 478 | |
Trent Piepho | b0d3159 | 2009-05-30 21:45:46 -0300 | [diff] [blame] | 479 | /* Clamp x to be between min and max, aligned to a multiple of 2^align. min |
| 480 | * and max don't have to be aligned, but there must be at least one valid |
| 481 | * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples |
| 482 | * of 16 between 17 and 31. */ |
| 483 | static unsigned int clamp_align(unsigned int x, unsigned int min, |
| 484 | unsigned int max, unsigned int align) |
| 485 | { |
| 486 | /* Bits that must be zero to be aligned */ |
| 487 | unsigned int mask = ~((1 << align) - 1); |
| 488 | |
| 489 | /* Round to nearest aligned value */ |
| 490 | if (align) |
| 491 | x = (x + (1 << (align - 1))) & mask; |
| 492 | |
| 493 | /* Clamp to aligned value of min and max */ |
| 494 | if (x < min) |
| 495 | x = (min + ~mask) & mask; |
| 496 | else if (x > max) |
| 497 | x = max & mask; |
| 498 | |
| 499 | return x; |
| 500 | } |
| 501 | |
| 502 | /* Bound an image to have a width between wmin and wmax, and height between |
| 503 | * hmin and hmax, inclusive. Additionally, the width will be a multiple of |
| 504 | * 2^walign, the height will be a multiple of 2^halign, and the overall size |
| 505 | * (width*height) will be a multiple of 2^salign. The image may be shrunk |
| 506 | * or enlarged to fit the alignment constraints. |
| 507 | * |
| 508 | * The width or height maximum must not be smaller than the corresponding |
| 509 | * minimum. The alignments must not be so high there are no possible image |
| 510 | * sizes within the allowed bounds. wmin and hmin must be at least 1 |
| 511 | * (don't use 0). If you don't care about a certain alignment, specify 0, |
| 512 | * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If |
| 513 | * you only want to adjust downward, specify a maximum that's the same as |
| 514 | * the initial value. |
| 515 | */ |
| 516 | void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax, |
| 517 | unsigned int walign, |
| 518 | u32 *h, unsigned int hmin, unsigned int hmax, |
| 519 | unsigned int halign, unsigned int salign) |
| 520 | { |
| 521 | *w = clamp_align(*w, wmin, wmax, walign); |
| 522 | *h = clamp_align(*h, hmin, hmax, halign); |
| 523 | |
| 524 | /* Usually we don't need to align the size and are done now. */ |
| 525 | if (!salign) |
| 526 | return; |
| 527 | |
| 528 | /* How much alignment do we have? */ |
| 529 | walign = __ffs(*w); |
| 530 | halign = __ffs(*h); |
| 531 | /* Enough to satisfy the image alignment? */ |
| 532 | if (walign + halign < salign) { |
| 533 | /* Max walign where there is still a valid width */ |
| 534 | unsigned int wmaxa = __fls(wmax ^ (wmin - 1)); |
| 535 | /* Max halign where there is still a valid height */ |
| 536 | unsigned int hmaxa = __fls(hmax ^ (hmin - 1)); |
| 537 | |
| 538 | /* up the smaller alignment until we have enough */ |
| 539 | do { |
| 540 | if (halign >= hmaxa || |
| 541 | (walign <= halign && walign < wmaxa)) { |
| 542 | *w = clamp_align(*w, wmin, wmax, walign + 1); |
| 543 | walign = __ffs(*w); |
| 544 | } else { |
| 545 | *h = clamp_align(*h, hmin, hmax, halign + 1); |
| 546 | halign = __ffs(*h); |
| 547 | } |
| 548 | } while (halign + walign < salign); |
| 549 | } |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(v4l_bound_align_image); |
Muralidharan Karicheri | 2e535ed | 2009-12-10 04:39:47 -0300 | [diff] [blame] | 552 | |
| 553 | /** |
| 554 | * v4l_fill_dv_preset_info - fill description of a digital video preset |
| 555 | * @preset - preset value |
| 556 | * @info - pointer to struct v4l2_dv_enum_preset |
| 557 | * |
| 558 | * drivers can use this helper function to fill description of dv preset |
| 559 | * in info. |
| 560 | */ |
| 561 | int v4l_fill_dv_preset_info(u32 preset, struct v4l2_dv_enum_preset *info) |
| 562 | { |
| 563 | static const struct v4l2_dv_preset_info { |
| 564 | u16 width; |
| 565 | u16 height; |
| 566 | const char *name; |
| 567 | } dv_presets[] = { |
| 568 | { 0, 0, "Invalid" }, /* V4L2_DV_INVALID */ |
| 569 | { 720, 480, "480p@59.94" }, /* V4L2_DV_480P59_94 */ |
| 570 | { 720, 576, "576p@50" }, /* V4L2_DV_576P50 */ |
| 571 | { 1280, 720, "720p@24" }, /* V4L2_DV_720P24 */ |
| 572 | { 1280, 720, "720p@25" }, /* V4L2_DV_720P25 */ |
| 573 | { 1280, 720, "720p@30" }, /* V4L2_DV_720P30 */ |
| 574 | { 1280, 720, "720p@50" }, /* V4L2_DV_720P50 */ |
| 575 | { 1280, 720, "720p@59.94" }, /* V4L2_DV_720P59_94 */ |
| 576 | { 1280, 720, "720p@60" }, /* V4L2_DV_720P60 */ |
| 577 | { 1920, 1080, "1080i@29.97" }, /* V4L2_DV_1080I29_97 */ |
| 578 | { 1920, 1080, "1080i@30" }, /* V4L2_DV_1080I30 */ |
| 579 | { 1920, 1080, "1080i@25" }, /* V4L2_DV_1080I25 */ |
| 580 | { 1920, 1080, "1080i@50" }, /* V4L2_DV_1080I50 */ |
| 581 | { 1920, 1080, "1080i@60" }, /* V4L2_DV_1080I60 */ |
| 582 | { 1920, 1080, "1080p@24" }, /* V4L2_DV_1080P24 */ |
| 583 | { 1920, 1080, "1080p@25" }, /* V4L2_DV_1080P25 */ |
| 584 | { 1920, 1080, "1080p@30" }, /* V4L2_DV_1080P30 */ |
| 585 | { 1920, 1080, "1080p@50" }, /* V4L2_DV_1080P50 */ |
| 586 | { 1920, 1080, "1080p@60" }, /* V4L2_DV_1080P60 */ |
| 587 | }; |
| 588 | |
| 589 | if (info == NULL || preset >= ARRAY_SIZE(dv_presets)) |
| 590 | return -EINVAL; |
| 591 | |
| 592 | info->preset = preset; |
| 593 | info->width = dv_presets[preset].width; |
| 594 | info->height = dv_presets[preset].height; |
| 595 | strlcpy(info->name, dv_presets[preset].name, sizeof(info->name)); |
| 596 | return 0; |
| 597 | } |
| 598 | EXPORT_SYMBOL_GPL(v4l_fill_dv_preset_info); |
Guennadi Liakhovetski | 79c6ff9 | 2010-08-27 13:41:44 -0300 | [diff] [blame] | 599 | |
Hans Verkuil | c2a667f | 2012-08-10 06:09:26 -0300 | [diff] [blame] | 600 | /** |
| 601 | * v4l_match_dv_timings - check if two timings match |
| 602 | * @t1 - compare this v4l2_dv_timings struct... |
| 603 | * @t2 - with this struct. |
| 604 | * @pclock_delta - the allowed pixelclock deviation. |
| 605 | * |
| 606 | * Compare t1 with t2 with a given margin of error for the pixelclock. |
| 607 | */ |
| 608 | bool v4l_match_dv_timings(const struct v4l2_dv_timings *t1, |
| 609 | const struct v4l2_dv_timings *t2, |
| 610 | unsigned pclock_delta) |
| 611 | { |
| 612 | if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120) |
| 613 | return false; |
| 614 | if (t1->bt.width == t2->bt.width && |
| 615 | t1->bt.height == t2->bt.height && |
| 616 | t1->bt.interlaced == t2->bt.interlaced && |
| 617 | t1->bt.polarities == t2->bt.polarities && |
| 618 | t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta && |
| 619 | t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta && |
| 620 | t1->bt.hfrontporch == t2->bt.hfrontporch && |
| 621 | t1->bt.vfrontporch == t2->bt.vfrontporch && |
| 622 | t1->bt.vsync == t2->bt.vsync && |
| 623 | t1->bt.vbackporch == t2->bt.vbackporch && |
| 624 | (!t1->bt.interlaced || |
| 625 | (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch && |
| 626 | t1->bt.il_vsync == t2->bt.il_vsync && |
| 627 | t1->bt.il_vbackporch == t2->bt.il_vbackporch))) |
| 628 | return true; |
| 629 | return false; |
| 630 | } |
| 631 | EXPORT_SYMBOL_GPL(v4l_match_dv_timings); |
| 632 | |
Hans Verkuil | c61bd6a | 2012-08-10 06:11:12 -0300 | [diff] [blame] | 633 | /* |
| 634 | * CVT defines |
| 635 | * Based on Coordinated Video Timings Standard |
| 636 | * version 1.1 September 10, 2003 |
| 637 | */ |
| 638 | |
| 639 | #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */ |
| 640 | |
| 641 | /* Normal blanking */ |
| 642 | #define CVT_MIN_V_BPORCH 7 /* lines */ |
| 643 | #define CVT_MIN_V_PORCH_RND 3 /* lines */ |
| 644 | #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */ |
| 645 | |
| 646 | /* Normal blanking for CVT uses GTF to calculate horizontal blanking */ |
| 647 | #define CVT_CELL_GRAN 8 /* character cell granularity */ |
| 648 | #define CVT_M 600 /* blanking formula gradient */ |
| 649 | #define CVT_C 40 /* blanking formula offset */ |
| 650 | #define CVT_K 128 /* blanking formula scaling factor */ |
| 651 | #define CVT_J 20 /* blanking formula scaling factor */ |
| 652 | #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J) |
| 653 | #define CVT_M_PRIME (CVT_K * CVT_M / 256) |
| 654 | |
| 655 | /* Reduced Blanking */ |
| 656 | #define CVT_RB_MIN_V_BPORCH 7 /* lines */ |
| 657 | #define CVT_RB_V_FPORCH 3 /* lines */ |
| 658 | #define CVT_RB_MIN_V_BLANK 460 /* us */ |
| 659 | #define CVT_RB_H_SYNC 32 /* pixels */ |
| 660 | #define CVT_RB_H_BPORCH 80 /* pixels */ |
| 661 | #define CVT_RB_H_BLANK 160 /* pixels */ |
| 662 | |
| 663 | /** v4l2_detect_cvt - detect if the given timings follow the CVT standard |
| 664 | * @frame_height - the total height of the frame (including blanking) in lines. |
| 665 | * @hfreq - the horizontal frequency in Hz. |
| 666 | * @vsync - the height of the vertical sync in lines. |
| 667 | * @polarities - the horizontal and vertical polarities (same as struct |
| 668 | * v4l2_bt_timings polarities). |
| 669 | * @fmt - the resulting timings. |
| 670 | * |
| 671 | * This function will attempt to detect if the given values correspond to a |
| 672 | * valid CVT format. If so, then it will return true, and fmt will be filled |
| 673 | * in with the found CVT timings. |
| 674 | */ |
| 675 | bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync, |
| 676 | u32 polarities, struct v4l2_dv_timings *fmt) |
| 677 | { |
| 678 | int v_fp, v_bp, h_fp, h_bp, hsync; |
| 679 | int frame_width, image_height, image_width; |
| 680 | bool reduced_blanking; |
| 681 | unsigned pix_clk; |
| 682 | |
| 683 | if (vsync < 4 || vsync > 7) |
| 684 | return false; |
| 685 | |
| 686 | if (polarities == V4L2_DV_VSYNC_POS_POL) |
| 687 | reduced_blanking = false; |
| 688 | else if (polarities == V4L2_DV_HSYNC_POS_POL) |
| 689 | reduced_blanking = true; |
| 690 | else |
| 691 | return false; |
| 692 | |
| 693 | /* Vertical */ |
| 694 | if (reduced_blanking) { |
| 695 | v_fp = CVT_RB_V_FPORCH; |
| 696 | v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 999999) / 1000000; |
| 697 | v_bp -= vsync + v_fp; |
| 698 | |
| 699 | if (v_bp < CVT_RB_MIN_V_BPORCH) |
| 700 | v_bp = CVT_RB_MIN_V_BPORCH; |
| 701 | } else { |
| 702 | v_fp = CVT_MIN_V_PORCH_RND; |
| 703 | v_bp = (CVT_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync; |
| 704 | |
| 705 | if (v_bp < CVT_MIN_V_BPORCH) |
| 706 | v_bp = CVT_MIN_V_BPORCH; |
| 707 | } |
| 708 | image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1; |
| 709 | |
| 710 | /* Aspect ratio based on vsync */ |
| 711 | switch (vsync) { |
| 712 | case 4: |
| 713 | image_width = (image_height * 4) / 3; |
| 714 | break; |
| 715 | case 5: |
| 716 | image_width = (image_height * 16) / 9; |
| 717 | break; |
| 718 | case 6: |
| 719 | image_width = (image_height * 16) / 10; |
| 720 | break; |
| 721 | case 7: |
| 722 | /* special case */ |
| 723 | if (image_height == 1024) |
| 724 | image_width = (image_height * 5) / 4; |
| 725 | else if (image_height == 768) |
| 726 | image_width = (image_height * 15) / 9; |
| 727 | else |
| 728 | return false; |
| 729 | break; |
| 730 | default: |
| 731 | return false; |
| 732 | } |
| 733 | |
| 734 | image_width = image_width & ~7; |
| 735 | |
| 736 | /* Horizontal */ |
| 737 | if (reduced_blanking) { |
| 738 | pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq; |
| 739 | pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN; |
| 740 | |
| 741 | h_bp = CVT_RB_H_BPORCH; |
| 742 | hsync = CVT_RB_H_SYNC; |
| 743 | h_fp = CVT_RB_H_BLANK - h_bp - hsync; |
| 744 | |
| 745 | frame_width = image_width + CVT_RB_H_BLANK; |
| 746 | } else { |
| 747 | int h_blank; |
| 748 | unsigned ideal_duty_cycle = CVT_C_PRIME - (CVT_M_PRIME * 1000) / hfreq; |
| 749 | |
| 750 | h_blank = (image_width * ideal_duty_cycle + (100 - ideal_duty_cycle) / 2) / |
| 751 | (100 - ideal_duty_cycle); |
| 752 | h_blank = h_blank - h_blank % (2 * CVT_CELL_GRAN); |
| 753 | |
| 754 | if (h_blank * 100 / image_width < 20) { |
| 755 | h_blank = image_width / 5; |
| 756 | h_blank = (h_blank + 0x7) & ~0x7; |
| 757 | } |
| 758 | |
| 759 | pix_clk = (image_width + h_blank) * hfreq; |
| 760 | pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN; |
| 761 | |
| 762 | h_bp = h_blank / 2; |
| 763 | frame_width = image_width + h_blank; |
| 764 | |
| 765 | hsync = (frame_width * 8 + 50) / 100; |
| 766 | hsync = hsync - hsync % CVT_CELL_GRAN; |
| 767 | h_fp = h_blank - hsync - h_bp; |
| 768 | } |
| 769 | |
| 770 | fmt->bt.polarities = polarities; |
| 771 | fmt->bt.width = image_width; |
| 772 | fmt->bt.height = image_height; |
| 773 | fmt->bt.hfrontporch = h_fp; |
| 774 | fmt->bt.vfrontporch = v_fp; |
| 775 | fmt->bt.hsync = hsync; |
| 776 | fmt->bt.vsync = vsync; |
| 777 | fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync; |
| 778 | fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync; |
| 779 | fmt->bt.pixelclock = pix_clk; |
| 780 | fmt->bt.standards = V4L2_DV_BT_STD_CVT; |
| 781 | if (reduced_blanking) |
| 782 | fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING; |
| 783 | return true; |
| 784 | } |
| 785 | EXPORT_SYMBOL_GPL(v4l2_detect_cvt); |
| 786 | |
| 787 | /* |
| 788 | * GTF defines |
| 789 | * Based on Generalized Timing Formula Standard |
| 790 | * Version 1.1 September 2, 1999 |
| 791 | */ |
| 792 | |
| 793 | #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */ |
| 794 | |
| 795 | #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */ |
| 796 | #define GTF_V_FP 1 /* vertical front porch (lines) */ |
| 797 | #define GTF_CELL_GRAN 8 /* character cell granularity */ |
| 798 | |
| 799 | /* Default */ |
| 800 | #define GTF_D_M 600 /* blanking formula gradient */ |
| 801 | #define GTF_D_C 40 /* blanking formula offset */ |
| 802 | #define GTF_D_K 128 /* blanking formula scaling factor */ |
| 803 | #define GTF_D_J 20 /* blanking formula scaling factor */ |
| 804 | #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J) |
| 805 | #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256) |
| 806 | |
| 807 | /* Secondary */ |
| 808 | #define GTF_S_M 3600 /* blanking formula gradient */ |
| 809 | #define GTF_S_C 40 /* blanking formula offset */ |
| 810 | #define GTF_S_K 128 /* blanking formula scaling factor */ |
| 811 | #define GTF_S_J 35 /* blanking formula scaling factor */ |
| 812 | #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J) |
| 813 | #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256) |
| 814 | |
| 815 | /** v4l2_detect_gtf - detect if the given timings follow the GTF standard |
| 816 | * @frame_height - the total height of the frame (including blanking) in lines. |
| 817 | * @hfreq - the horizontal frequency in Hz. |
| 818 | * @vsync - the height of the vertical sync in lines. |
| 819 | * @polarities - the horizontal and vertical polarities (same as struct |
| 820 | * v4l2_bt_timings polarities). |
| 821 | * @aspect - preferred aspect ratio. GTF has no method of determining the |
| 822 | * aspect ratio in order to derive the image width from the |
| 823 | * image height, so it has to be passed explicitly. Usually |
| 824 | * the native screen aspect ratio is used for this. If it |
| 825 | * is not filled in correctly, then 16:9 will be assumed. |
| 826 | * @fmt - the resulting timings. |
| 827 | * |
| 828 | * This function will attempt to detect if the given values correspond to a |
| 829 | * valid GTF format. If so, then it will return true, and fmt will be filled |
| 830 | * in with the found GTF timings. |
| 831 | */ |
| 832 | bool v4l2_detect_gtf(unsigned frame_height, |
| 833 | unsigned hfreq, |
| 834 | unsigned vsync, |
| 835 | u32 polarities, |
| 836 | struct v4l2_fract aspect, |
| 837 | struct v4l2_dv_timings *fmt) |
| 838 | { |
| 839 | int pix_clk; |
Mauro Carvalho Chehab | 685a39b | 2012-10-27 16:26:15 -0300 | [diff] [blame] | 840 | int v_fp, v_bp, h_fp, hsync; |
Hans Verkuil | c61bd6a | 2012-08-10 06:11:12 -0300 | [diff] [blame] | 841 | int frame_width, image_height, image_width; |
| 842 | bool default_gtf; |
| 843 | int h_blank; |
| 844 | |
| 845 | if (vsync != 3) |
| 846 | return false; |
| 847 | |
| 848 | if (polarities == V4L2_DV_VSYNC_POS_POL) |
| 849 | default_gtf = true; |
| 850 | else if (polarities == V4L2_DV_HSYNC_POS_POL) |
| 851 | default_gtf = false; |
| 852 | else |
| 853 | return false; |
| 854 | |
| 855 | /* Vertical */ |
| 856 | v_fp = GTF_V_FP; |
| 857 | v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync; |
| 858 | image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1; |
| 859 | |
| 860 | if (aspect.numerator == 0 || aspect.denominator == 0) { |
| 861 | aspect.numerator = 16; |
| 862 | aspect.denominator = 9; |
| 863 | } |
| 864 | image_width = ((image_height * aspect.numerator) / aspect.denominator); |
| 865 | |
| 866 | /* Horizontal */ |
| 867 | if (default_gtf) |
| 868 | h_blank = ((image_width * GTF_D_C_PRIME * hfreq) - |
| 869 | (image_width * GTF_D_M_PRIME * 1000) + |
| 870 | (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) / |
| 871 | (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000); |
| 872 | else |
| 873 | h_blank = ((image_width * GTF_S_C_PRIME * hfreq) - |
| 874 | (image_width * GTF_S_M_PRIME * 1000) + |
| 875 | (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) / |
| 876 | (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000); |
| 877 | |
| 878 | h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN); |
| 879 | frame_width = image_width + h_blank; |
| 880 | |
| 881 | pix_clk = (image_width + h_blank) * hfreq; |
| 882 | pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN; |
| 883 | |
| 884 | hsync = (frame_width * 8 + 50) / 100; |
| 885 | hsync = hsync - hsync % GTF_CELL_GRAN; |
| 886 | |
| 887 | h_fp = h_blank / 2 - hsync; |
Hans Verkuil | c61bd6a | 2012-08-10 06:11:12 -0300 | [diff] [blame] | 888 | |
| 889 | fmt->bt.polarities = polarities; |
| 890 | fmt->bt.width = image_width; |
| 891 | fmt->bt.height = image_height; |
| 892 | fmt->bt.hfrontporch = h_fp; |
| 893 | fmt->bt.vfrontporch = v_fp; |
| 894 | fmt->bt.hsync = hsync; |
| 895 | fmt->bt.vsync = vsync; |
| 896 | fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync; |
| 897 | fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync; |
| 898 | fmt->bt.pixelclock = pix_clk; |
| 899 | fmt->bt.standards = V4L2_DV_BT_STD_GTF; |
| 900 | if (!default_gtf) |
| 901 | fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING; |
| 902 | return true; |
| 903 | } |
| 904 | EXPORT_SYMBOL_GPL(v4l2_detect_gtf); |
| 905 | |
| 906 | /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes |
| 907 | * 0x15 and 0x16 from the EDID. |
| 908 | * @hor_landscape - byte 0x15 from the EDID. |
| 909 | * @vert_portrait - byte 0x16 from the EDID. |
| 910 | * |
| 911 | * Determines the aspect ratio from the EDID. |
| 912 | * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2: |
| 913 | * "Horizontal and Vertical Screen Size or Aspect Ratio" |
| 914 | */ |
| 915 | struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait) |
| 916 | { |
| 917 | struct v4l2_fract aspect = { 16, 9 }; |
| 918 | u32 tmp; |
| 919 | u8 ratio; |
| 920 | |
| 921 | /* Nothing filled in, fallback to 16:9 */ |
| 922 | if (!hor_landscape && !vert_portrait) |
| 923 | return aspect; |
| 924 | /* Both filled in, so they are interpreted as the screen size in cm */ |
| 925 | if (hor_landscape && vert_portrait) { |
| 926 | aspect.numerator = hor_landscape; |
| 927 | aspect.denominator = vert_portrait; |
| 928 | return aspect; |
| 929 | } |
| 930 | /* Only one is filled in, so interpret them as a ratio: |
| 931 | (val + 99) / 100 */ |
| 932 | ratio = hor_landscape | vert_portrait; |
| 933 | /* Change some rounded values into the exact aspect ratio */ |
| 934 | if (ratio == 79) { |
| 935 | aspect.numerator = 16; |
| 936 | aspect.denominator = 9; |
| 937 | } else if (ratio == 34) { |
| 938 | aspect.numerator = 4; |
| 939 | aspect.numerator = 3; |
| 940 | } else if (ratio == 68) { |
| 941 | aspect.numerator = 15; |
| 942 | aspect.numerator = 9; |
| 943 | } else { |
| 944 | aspect.numerator = hor_landscape + 99; |
| 945 | aspect.denominator = 100; |
| 946 | } |
| 947 | if (hor_landscape) |
| 948 | return aspect; |
| 949 | /* The aspect ratio is for portrait, so swap numerator and denominator */ |
| 950 | tmp = aspect.denominator; |
| 951 | aspect.denominator = aspect.numerator; |
| 952 | aspect.numerator = tmp; |
| 953 | return aspect; |
| 954 | } |
| 955 | EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio); |
| 956 | |
Hans Verkuil | 3fd8e64 | 2010-09-30 09:29:37 -0300 | [diff] [blame] | 957 | const struct v4l2_frmsize_discrete *v4l2_find_nearest_format( |
| 958 | const struct v4l2_discrete_probe *probe, |
| 959 | s32 width, s32 height) |
Guennadi Liakhovetski | 79c6ff9 | 2010-08-27 13:41:44 -0300 | [diff] [blame] | 960 | { |
| 961 | int i; |
| 962 | u32 error, min_error = UINT_MAX; |
Hans Verkuil | 3fd8e64 | 2010-09-30 09:29:37 -0300 | [diff] [blame] | 963 | const struct v4l2_frmsize_discrete *size, *best = NULL; |
Guennadi Liakhovetski | 79c6ff9 | 2010-08-27 13:41:44 -0300 | [diff] [blame] | 964 | |
| 965 | if (!probe) |
| 966 | return best; |
| 967 | |
| 968 | for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) { |
| 969 | error = abs(size->width - width) + abs(size->height - height); |
| 970 | if (error < min_error) { |
| 971 | min_error = error; |
| 972 | best = size; |
| 973 | } |
| 974 | if (!error) |
| 975 | break; |
| 976 | } |
| 977 | |
| 978 | return best; |
| 979 | } |
| 980 | EXPORT_SYMBOL_GPL(v4l2_find_nearest_format); |
Sakari Ailus | abd2329 | 2012-09-15 07:51:47 -0300 | [diff] [blame] | 981 | |
| 982 | void v4l2_get_timestamp(struct timeval *tv) |
| 983 | { |
| 984 | struct timespec ts; |
| 985 | |
| 986 | ktime_get_ts(&ts); |
| 987 | tv->tv_sec = ts.tv_sec; |
| 988 | tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC; |
| 989 | } |
| 990 | EXPORT_SYMBOL_GPL(v4l2_get_timestamp); |