Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1 | /* |
Mauro Carvalho Chehab | cb7a01a | 2012-08-14 16:23:43 -0300 | [diff] [blame] | 2 | * drivers/media/i2c/smiapp/smiapp-core.c |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 3 | * |
| 4 | * Generic driver for SMIA/SMIA++ compliant camera modules |
| 5 | * |
| 6 | * Copyright (C) 2010--2012 Nokia Corporation |
Sakari Ailus | 8c5dff9 | 2012-10-28 06:44:17 -0300 | [diff] [blame] | 7 | * Contact: Sakari Ailus <sakari.ailus@iki.fi> |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 8 | * |
| 9 | * Based on smiapp driver by Vimarsh Zutshi |
| 10 | * Based on jt8ev1.c by Vimarsh Zutshi |
| 11 | * Based on smia-sensor.c by Tuukka Toivonen <tuukkat76@gmail.com> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License |
| 15 | * version 2 as published by the Free Software Foundation. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, but |
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 25 | * 02110-1301 USA |
| 26 | * |
| 27 | */ |
| 28 | |
Sakari Ailus | 2547428 | 2012-04-22 08:24:33 -0300 | [diff] [blame] | 29 | #include <linux/clk.h> |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 30 | #include <linux/delay.h> |
| 31 | #include <linux/device.h> |
| 32 | #include <linux/gpio.h> |
| 33 | #include <linux/module.h> |
Mauro Carvalho Chehab | ce7d16a | 2012-06-21 10:05:56 -0300 | [diff] [blame] | 34 | #include <linux/slab.h> |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 35 | #include <linux/regulator/consumer.h> |
| 36 | #include <linux/v4l2-mediabus.h> |
| 37 | #include <media/v4l2-device.h> |
| 38 | |
| 39 | #include "smiapp.h" |
| 40 | |
Sakari Ailus | 563df3d | 2012-06-13 16:01:10 -0300 | [diff] [blame] | 41 | #define SMIAPP_ALIGN_DIM(dim, flags) \ |
| 42 | ((flags) & V4L2_SEL_FLAG_GE \ |
| 43 | ? ALIGN((dim), 2) \ |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 44 | : (dim) & ~1) |
| 45 | |
| 46 | /* |
| 47 | * smiapp_module_idents - supported camera modules |
| 48 | */ |
| 49 | static const struct smiapp_module_ident smiapp_module_idents[] = { |
| 50 | SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"), |
| 51 | SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"), |
| 52 | SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"), |
| 53 | SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"), |
| 54 | SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"), |
| 55 | SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk), |
| 56 | SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"), |
| 57 | SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"), |
| 58 | SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk), |
| 59 | SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk), |
| 60 | SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk), |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * |
| 65 | * Dynamic Capability Identification |
| 66 | * |
| 67 | */ |
| 68 | |
| 69 | static int smiapp_read_frame_fmt(struct smiapp_sensor *sensor) |
| 70 | { |
| 71 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 72 | u32 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc; |
| 73 | unsigned int i; |
| 74 | int rval; |
| 75 | int line_count = 0; |
| 76 | int embedded_start = -1, embedded_end = -1; |
| 77 | int image_start = 0; |
| 78 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 79 | rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 80 | &fmt_model_type); |
| 81 | if (rval) |
| 82 | return rval; |
| 83 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 84 | rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 85 | &fmt_model_subtype); |
| 86 | if (rval) |
| 87 | return rval; |
| 88 | |
| 89 | ncol_desc = (fmt_model_subtype |
| 90 | & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK) |
| 91 | >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT; |
| 92 | nrow_desc = fmt_model_subtype |
| 93 | & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK; |
| 94 | |
| 95 | dev_dbg(&client->dev, "format_model_type %s\n", |
| 96 | fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE |
| 97 | ? "2 byte" : |
| 98 | fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE |
| 99 | ? "4 byte" : "is simply bad"); |
| 100 | |
| 101 | for (i = 0; i < ncol_desc + nrow_desc; i++) { |
| 102 | u32 desc; |
| 103 | u32 pixelcode; |
| 104 | u32 pixels; |
| 105 | char *which; |
| 106 | char *what; |
| 107 | |
| 108 | if (fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE) { |
| 109 | rval = smiapp_read( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 110 | sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 111 | SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i), |
| 112 | &desc); |
| 113 | if (rval) |
| 114 | return rval; |
| 115 | |
| 116 | pixelcode = |
| 117 | (desc |
| 118 | & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK) |
| 119 | >> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT; |
| 120 | pixels = desc & SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK; |
| 121 | } else if (fmt_model_type |
| 122 | == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE) { |
| 123 | rval = smiapp_read( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 124 | sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 125 | SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i), |
| 126 | &desc); |
| 127 | if (rval) |
| 128 | return rval; |
| 129 | |
| 130 | pixelcode = |
| 131 | (desc |
| 132 | & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK) |
| 133 | >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT; |
| 134 | pixels = desc & SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK; |
| 135 | } else { |
| 136 | dev_dbg(&client->dev, |
| 137 | "invalid frame format model type %d\n", |
| 138 | fmt_model_type); |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | |
| 142 | if (i < ncol_desc) |
| 143 | which = "columns"; |
| 144 | else |
| 145 | which = "rows"; |
| 146 | |
| 147 | switch (pixelcode) { |
| 148 | case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED: |
| 149 | what = "embedded"; |
| 150 | break; |
| 151 | case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY: |
| 152 | what = "dummy"; |
| 153 | break; |
| 154 | case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK: |
| 155 | what = "black"; |
| 156 | break; |
| 157 | case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK: |
| 158 | what = "dark"; |
| 159 | break; |
| 160 | case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE: |
| 161 | what = "visible"; |
| 162 | break; |
| 163 | default: |
| 164 | what = "invalid"; |
| 165 | dev_dbg(&client->dev, "pixelcode %d\n", pixelcode); |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | dev_dbg(&client->dev, "%s pixels: %d %s\n", |
| 170 | what, pixels, which); |
| 171 | |
| 172 | if (i < ncol_desc) |
| 173 | continue; |
| 174 | |
| 175 | /* Handle row descriptors */ |
| 176 | if (pixelcode |
| 177 | == SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED) { |
| 178 | embedded_start = line_count; |
| 179 | } else { |
| 180 | if (pixelcode == SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE |
| 181 | || pixels >= sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES] / 2) |
| 182 | image_start = line_count; |
| 183 | if (embedded_start != -1 && embedded_end == -1) |
| 184 | embedded_end = line_count; |
| 185 | } |
| 186 | line_count += pixels; |
| 187 | } |
| 188 | |
| 189 | if (embedded_start == -1 || embedded_end == -1) { |
| 190 | embedded_start = 0; |
| 191 | embedded_end = 0; |
| 192 | } |
| 193 | |
| 194 | dev_dbg(&client->dev, "embedded data from lines %d to %d\n", |
| 195 | embedded_start, embedded_end); |
| 196 | dev_dbg(&client->dev, "image data starts at line %d\n", image_start); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int smiapp_pll_configure(struct smiapp_sensor *sensor) |
| 202 | { |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 203 | struct smiapp_pll *pll = &sensor->pll; |
| 204 | int rval; |
| 205 | |
| 206 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 207 | sensor, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt_pix_clk_div); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 208 | if (rval < 0) |
| 209 | return rval; |
| 210 | |
| 211 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 212 | sensor, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt_sys_clk_div); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 213 | if (rval < 0) |
| 214 | return rval; |
| 215 | |
| 216 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 217 | sensor, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 218 | if (rval < 0) |
| 219 | return rval; |
| 220 | |
| 221 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 222 | sensor, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 223 | if (rval < 0) |
| 224 | return rval; |
| 225 | |
| 226 | /* Lane op clock ratio does not apply here. */ |
| 227 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 228 | sensor, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 229 | DIV_ROUND_UP(pll->op_sys_clk_freq_hz, 1000000 / 256 / 256)); |
| 230 | if (rval < 0 || sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0) |
| 231 | return rval; |
| 232 | |
| 233 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 234 | sensor, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op_pix_clk_div); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 235 | if (rval < 0) |
| 236 | return rval; |
| 237 | |
| 238 | return smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 239 | sensor, SMIAPP_REG_U16_OP_SYS_CLK_DIV, pll->op_sys_clk_div); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static int smiapp_pll_update(struct smiapp_sensor *sensor) |
| 243 | { |
| 244 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 245 | struct smiapp_pll_limits lim = { |
| 246 | .min_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV], |
| 247 | .max_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV], |
| 248 | .min_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ], |
| 249 | .max_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ], |
| 250 | .min_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER], |
| 251 | .max_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER], |
| 252 | .min_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ], |
| 253 | .max_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ], |
| 254 | |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 255 | .op.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV], |
| 256 | .op.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV], |
| 257 | .op.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV], |
| 258 | .op.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV], |
| 259 | .op.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ], |
| 260 | .op.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ], |
| 261 | .op.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ], |
| 262 | .op.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ], |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 263 | |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 264 | .vt.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV], |
| 265 | .vt.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV], |
| 266 | .vt.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV], |
| 267 | .vt.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV], |
| 268 | .vt.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ], |
| 269 | .vt.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ], |
| 270 | .vt.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ], |
| 271 | .vt.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ], |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 272 | |
| 273 | .min_line_length_pck_bin = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN], |
| 274 | .min_line_length_pck = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK], |
| 275 | }; |
| 276 | struct smiapp_pll *pll = &sensor->pll; |
| 277 | int rval; |
| 278 | |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 279 | if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0) { |
| 280 | /* |
| 281 | * Fill in operational clock divisors limits from the |
| 282 | * video timing ones. On profile 0 sensors the |
| 283 | * requirements regarding them are essentially the |
| 284 | * same as on VT ones. |
| 285 | */ |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 286 | lim.op = lim.vt; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 287 | } |
| 288 | |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 289 | pll->binning_horizontal = sensor->binning_horizontal; |
| 290 | pll->binning_vertical = sensor->binning_vertical; |
| 291 | pll->link_freq = |
| 292 | sensor->link_freq->qmenu_int[sensor->link_freq->val]; |
| 293 | pll->scale_m = sensor->scale_m; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 294 | pll->bits_per_pixel = sensor->csi_format->compressed; |
| 295 | |
| 296 | rval = smiapp_pll_calculate(&client->dev, &lim, pll); |
| 297 | if (rval < 0) |
| 298 | return rval; |
| 299 | |
| 300 | sensor->pixel_rate_parray->cur.val64 = pll->vt_pix_clk_freq_hz; |
| 301 | sensor->pixel_rate_csi->cur.val64 = pll->pixel_rate_csi; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /* |
| 308 | * |
| 309 | * V4L2 Controls handling |
| 310 | * |
| 311 | */ |
| 312 | |
| 313 | static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor) |
| 314 | { |
| 315 | struct v4l2_ctrl *ctrl = sensor->exposure; |
| 316 | int max; |
| 317 | |
| 318 | max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height |
| 319 | + sensor->vblank->val |
| 320 | - sensor->limits[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN]; |
| 321 | |
| 322 | ctrl->maximum = max; |
| 323 | if (ctrl->default_value > max) |
| 324 | ctrl->default_value = max; |
| 325 | if (ctrl->val > max) |
| 326 | ctrl->val = max; |
| 327 | if (ctrl->cur.val > max) |
| 328 | ctrl->cur.val = max; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Order matters. |
| 333 | * |
| 334 | * 1. Bits-per-pixel, descending. |
| 335 | * 2. Bits-per-pixel compressed, descending. |
| 336 | * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel |
| 337 | * orders must be defined. |
| 338 | */ |
| 339 | static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = { |
| 340 | { V4L2_MBUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, }, |
| 341 | { V4L2_MBUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, }, |
| 342 | { V4L2_MBUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, }, |
| 343 | { V4L2_MBUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, }, |
| 344 | { V4L2_MBUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, }, |
| 345 | { V4L2_MBUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, }, |
| 346 | { V4L2_MBUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, }, |
| 347 | { V4L2_MBUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, }, |
| 348 | { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, }, |
| 349 | { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, }, |
| 350 | { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, }, |
| 351 | { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, }, |
Sakari Ailus | b8cc8d7 | 2012-04-27 10:01:46 -0300 | [diff] [blame] | 352 | { V4L2_MBUS_FMT_SGRBG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GRBG, }, |
| 353 | { V4L2_MBUS_FMT_SRGGB8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_RGGB, }, |
| 354 | { V4L2_MBUS_FMT_SBGGR8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_BGGR, }, |
| 355 | { V4L2_MBUS_FMT_SGBRG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GBRG, }, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" }; |
| 359 | |
| 360 | #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \ |
| 361 | - (unsigned long)smiapp_csi_data_formats) \ |
| 362 | / sizeof(*smiapp_csi_data_formats)) |
| 363 | |
| 364 | static u32 smiapp_pixel_order(struct smiapp_sensor *sensor) |
| 365 | { |
| 366 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 367 | int flip = 0; |
| 368 | |
| 369 | if (sensor->hflip) { |
| 370 | if (sensor->hflip->val) |
| 371 | flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP; |
| 372 | |
| 373 | if (sensor->vflip->val) |
| 374 | flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP; |
| 375 | } |
| 376 | |
| 377 | flip ^= sensor->hvflip_inv_mask; |
| 378 | |
| 379 | dev_dbg(&client->dev, "flip %d\n", flip); |
| 380 | return sensor->default_pixel_order ^ flip; |
| 381 | } |
| 382 | |
| 383 | static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor) |
| 384 | { |
| 385 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 386 | unsigned int csi_format_idx = |
| 387 | to_csi_format_idx(sensor->csi_format) & ~3; |
| 388 | unsigned int internal_csi_format_idx = |
| 389 | to_csi_format_idx(sensor->internal_csi_format) & ~3; |
| 390 | unsigned int pixel_order = smiapp_pixel_order(sensor); |
| 391 | |
| 392 | sensor->mbus_frame_fmts = |
| 393 | sensor->default_mbus_frame_fmts << pixel_order; |
| 394 | sensor->csi_format = |
| 395 | &smiapp_csi_data_formats[csi_format_idx + pixel_order]; |
| 396 | sensor->internal_csi_format = |
| 397 | &smiapp_csi_data_formats[internal_csi_format_idx |
| 398 | + pixel_order]; |
| 399 | |
| 400 | BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order |
| 401 | >= ARRAY_SIZE(smiapp_csi_data_formats)); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 402 | |
| 403 | dev_dbg(&client->dev, "new pixel order %s\n", |
| 404 | pixel_order_str[pixel_order]); |
| 405 | } |
| 406 | |
| 407 | static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl) |
| 408 | { |
| 409 | struct smiapp_sensor *sensor = |
| 410 | container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler) |
| 411 | ->sensor; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 412 | u32 orient = 0; |
| 413 | int exposure; |
| 414 | int rval; |
| 415 | |
| 416 | switch (ctrl->id) { |
| 417 | case V4L2_CID_ANALOGUE_GAIN: |
| 418 | return smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 419 | sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 420 | SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val); |
| 421 | |
| 422 | case V4L2_CID_EXPOSURE: |
| 423 | return smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 424 | sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 425 | SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val); |
| 426 | |
| 427 | case V4L2_CID_HFLIP: |
| 428 | case V4L2_CID_VFLIP: |
| 429 | if (sensor->streaming) |
| 430 | return -EBUSY; |
| 431 | |
| 432 | if (sensor->hflip->val) |
| 433 | orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP; |
| 434 | |
| 435 | if (sensor->vflip->val) |
| 436 | orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP; |
| 437 | |
| 438 | orient ^= sensor->hvflip_inv_mask; |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 439 | rval = smiapp_write(sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 440 | SMIAPP_REG_U8_IMAGE_ORIENTATION, |
| 441 | orient); |
| 442 | if (rval < 0) |
| 443 | return rval; |
| 444 | |
| 445 | smiapp_update_mbus_formats(sensor); |
| 446 | |
| 447 | return 0; |
| 448 | |
| 449 | case V4L2_CID_VBLANK: |
| 450 | exposure = sensor->exposure->val; |
| 451 | |
| 452 | __smiapp_update_exposure_limits(sensor); |
| 453 | |
| 454 | if (exposure > sensor->exposure->maximum) { |
| 455 | sensor->exposure->val = |
| 456 | sensor->exposure->maximum; |
| 457 | rval = smiapp_set_ctrl( |
| 458 | sensor->exposure); |
| 459 | if (rval < 0) |
| 460 | return rval; |
| 461 | } |
| 462 | |
| 463 | return smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 464 | sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 465 | sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height |
| 466 | + ctrl->val); |
| 467 | |
| 468 | case V4L2_CID_HBLANK: |
| 469 | return smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 470 | sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 471 | sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width |
| 472 | + ctrl->val); |
| 473 | |
| 474 | case V4L2_CID_LINK_FREQ: |
| 475 | if (sensor->streaming) |
| 476 | return -EBUSY; |
| 477 | |
| 478 | return smiapp_pll_update(sensor); |
| 479 | |
| 480 | default: |
| 481 | return -EINVAL; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | static const struct v4l2_ctrl_ops smiapp_ctrl_ops = { |
| 486 | .s_ctrl = smiapp_set_ctrl, |
| 487 | }; |
| 488 | |
| 489 | static int smiapp_init_controls(struct smiapp_sensor *sensor) |
| 490 | { |
| 491 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
Sakari Ailus | 06b491f | 2012-05-06 07:03:46 -0300 | [diff] [blame] | 492 | unsigned int max; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 493 | int rval; |
| 494 | |
| 495 | rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 7); |
| 496 | if (rval) |
| 497 | return rval; |
| 498 | sensor->pixel_array->ctrl_handler.lock = &sensor->mutex; |
| 499 | |
| 500 | sensor->analog_gain = v4l2_ctrl_new_std( |
| 501 | &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops, |
| 502 | V4L2_CID_ANALOGUE_GAIN, |
| 503 | sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN], |
| 504 | sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX], |
| 505 | max(sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP], 1U), |
| 506 | sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN]); |
| 507 | |
| 508 | /* Exposure limits will be updated soon, use just something here. */ |
| 509 | sensor->exposure = v4l2_ctrl_new_std( |
| 510 | &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops, |
| 511 | V4L2_CID_EXPOSURE, 0, 0, 1, 0); |
| 512 | |
| 513 | sensor->hflip = v4l2_ctrl_new_std( |
| 514 | &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops, |
| 515 | V4L2_CID_HFLIP, 0, 1, 1, 0); |
| 516 | sensor->vflip = v4l2_ctrl_new_std( |
| 517 | &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops, |
| 518 | V4L2_CID_VFLIP, 0, 1, 1, 0); |
| 519 | |
| 520 | sensor->vblank = v4l2_ctrl_new_std( |
| 521 | &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops, |
| 522 | V4L2_CID_VBLANK, 0, 1, 1, 0); |
| 523 | |
| 524 | if (sensor->vblank) |
| 525 | sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE; |
| 526 | |
| 527 | sensor->hblank = v4l2_ctrl_new_std( |
| 528 | &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops, |
| 529 | V4L2_CID_HBLANK, 0, 1, 1, 0); |
| 530 | |
| 531 | if (sensor->hblank) |
| 532 | sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE; |
| 533 | |
| 534 | sensor->pixel_rate_parray = v4l2_ctrl_new_std( |
| 535 | &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops, |
| 536 | V4L2_CID_PIXEL_RATE, 0, 0, 1, 0); |
| 537 | |
| 538 | if (sensor->pixel_array->ctrl_handler.error) { |
| 539 | dev_err(&client->dev, |
| 540 | "pixel array controls initialization failed (%d)\n", |
| 541 | sensor->pixel_array->ctrl_handler.error); |
| 542 | rval = sensor->pixel_array->ctrl_handler.error; |
| 543 | goto error; |
| 544 | } |
| 545 | |
| 546 | sensor->pixel_array->sd.ctrl_handler = |
| 547 | &sensor->pixel_array->ctrl_handler; |
| 548 | |
| 549 | v4l2_ctrl_cluster(2, &sensor->hflip); |
| 550 | |
| 551 | rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0); |
| 552 | if (rval) |
| 553 | goto error; |
| 554 | sensor->src->ctrl_handler.lock = &sensor->mutex; |
| 555 | |
Sakari Ailus | 06b491f | 2012-05-06 07:03:46 -0300 | [diff] [blame] | 556 | for (max = 0; sensor->platform_data->op_sys_clock[max + 1]; max++); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 557 | |
Sakari Ailus | 06b491f | 2012-05-06 07:03:46 -0300 | [diff] [blame] | 558 | sensor->link_freq = v4l2_ctrl_new_int_menu( |
| 559 | &sensor->src->ctrl_handler, &smiapp_ctrl_ops, |
| 560 | V4L2_CID_LINK_FREQ, max, 0, |
| 561 | sensor->platform_data->op_sys_clock); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 562 | |
| 563 | sensor->pixel_rate_csi = v4l2_ctrl_new_std( |
| 564 | &sensor->src->ctrl_handler, &smiapp_ctrl_ops, |
| 565 | V4L2_CID_PIXEL_RATE, 0, 0, 1, 0); |
| 566 | |
| 567 | if (sensor->src->ctrl_handler.error) { |
| 568 | dev_err(&client->dev, |
| 569 | "src controls initialization failed (%d)\n", |
| 570 | sensor->src->ctrl_handler.error); |
| 571 | rval = sensor->src->ctrl_handler.error; |
| 572 | goto error; |
| 573 | } |
| 574 | |
| 575 | sensor->src->sd.ctrl_handler = |
| 576 | &sensor->src->ctrl_handler; |
| 577 | |
| 578 | return 0; |
| 579 | |
| 580 | error: |
| 581 | v4l2_ctrl_handler_free(&sensor->pixel_array->ctrl_handler); |
| 582 | v4l2_ctrl_handler_free(&sensor->src->ctrl_handler); |
| 583 | |
| 584 | return rval; |
| 585 | } |
| 586 | |
| 587 | static void smiapp_free_controls(struct smiapp_sensor *sensor) |
| 588 | { |
| 589 | unsigned int i; |
| 590 | |
| 591 | for (i = 0; i < sensor->ssds_used; i++) |
| 592 | v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler); |
| 593 | } |
| 594 | |
| 595 | static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit, |
| 596 | unsigned int n) |
| 597 | { |
| 598 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 599 | unsigned int i; |
| 600 | u32 val; |
| 601 | int rval; |
| 602 | |
| 603 | for (i = 0; i < n; i++) { |
| 604 | rval = smiapp_read( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 605 | sensor, smiapp_reg_limits[limit[i]].addr, &val); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 606 | if (rval) |
| 607 | return rval; |
| 608 | sensor->limits[limit[i]] = val; |
| 609 | dev_dbg(&client->dev, "0x%8.8x \"%s\" = %d, 0x%x\n", |
| 610 | smiapp_reg_limits[limit[i]].addr, |
| 611 | smiapp_reg_limits[limit[i]].what, val, val); |
| 612 | } |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int smiapp_get_all_limits(struct smiapp_sensor *sensor) |
| 618 | { |
| 619 | unsigned int i; |
| 620 | int rval; |
| 621 | |
| 622 | for (i = 0; i < SMIAPP_LIMIT_LAST; i++) { |
| 623 | rval = smiapp_get_limits(sensor, &i, 1); |
| 624 | if (rval < 0) |
| 625 | return rval; |
| 626 | } |
| 627 | |
| 628 | if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0) |
| 629 | smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16); |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static int smiapp_get_limits_binning(struct smiapp_sensor *sensor) |
| 635 | { |
Sakari Ailus | 3de886e | 2012-04-26 11:55:13 -0300 | [diff] [blame] | 636 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 637 | static u32 const limits[] = { |
| 638 | SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN, |
| 639 | SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN, |
| 640 | SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN, |
| 641 | SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN, |
| 642 | SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN, |
| 643 | SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN, |
| 644 | SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN, |
| 645 | }; |
| 646 | static u32 const limits_replace[] = { |
| 647 | SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES, |
| 648 | SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES, |
| 649 | SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK, |
| 650 | SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK, |
| 651 | SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK, |
| 652 | SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN, |
| 653 | SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN, |
| 654 | }; |
Sakari Ailus | 3de886e | 2012-04-26 11:55:13 -0300 | [diff] [blame] | 655 | unsigned int i; |
| 656 | int rval; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 657 | |
| 658 | if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] == |
| 659 | SMIAPP_BINNING_CAPABILITY_NO) { |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 660 | for (i = 0; i < ARRAY_SIZE(limits); i++) |
| 661 | sensor->limits[limits[i]] = |
| 662 | sensor->limits[limits_replace[i]]; |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
Sakari Ailus | 3de886e | 2012-04-26 11:55:13 -0300 | [diff] [blame] | 667 | rval = smiapp_get_limits(sensor, limits, ARRAY_SIZE(limits)); |
| 668 | if (rval < 0) |
| 669 | return rval; |
| 670 | |
| 671 | /* |
| 672 | * Sanity check whether the binning limits are valid. If not, |
| 673 | * use the non-binning ones. |
| 674 | */ |
| 675 | if (sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] |
| 676 | && sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] |
| 677 | && sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]) |
| 678 | return 0; |
| 679 | |
| 680 | for (i = 0; i < ARRAY_SIZE(limits); i++) { |
| 681 | dev_dbg(&client->dev, |
| 682 | "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n", |
| 683 | smiapp_reg_limits[limits[i]].addr, |
| 684 | smiapp_reg_limits[limits[i]].what, |
| 685 | sensor->limits[limits_replace[i]], |
| 686 | sensor->limits[limits_replace[i]]); |
| 687 | sensor->limits[limits[i]] = |
| 688 | sensor->limits[limits_replace[i]]; |
| 689 | } |
| 690 | |
| 691 | return 0; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor) |
| 695 | { |
| 696 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 697 | unsigned int type, n; |
| 698 | unsigned int i, pixel_order; |
| 699 | int rval; |
| 700 | |
| 701 | rval = smiapp_read( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 702 | sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 703 | if (rval) |
| 704 | return rval; |
| 705 | |
| 706 | dev_dbg(&client->dev, "data_format_model_type %d\n", type); |
| 707 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 708 | rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 709 | &pixel_order); |
| 710 | if (rval) |
| 711 | return rval; |
| 712 | |
| 713 | if (pixel_order >= ARRAY_SIZE(pixel_order_str)) { |
| 714 | dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order); |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | |
| 718 | dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order, |
| 719 | pixel_order_str[pixel_order]); |
| 720 | |
| 721 | switch (type) { |
| 722 | case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL: |
| 723 | n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N; |
| 724 | break; |
| 725 | case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED: |
| 726 | n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N; |
| 727 | break; |
| 728 | default: |
| 729 | return -EINVAL; |
| 730 | } |
| 731 | |
| 732 | sensor->default_pixel_order = pixel_order; |
| 733 | sensor->mbus_frame_fmts = 0; |
| 734 | |
| 735 | for (i = 0; i < n; i++) { |
| 736 | unsigned int fmt, j; |
| 737 | |
| 738 | rval = smiapp_read( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 739 | sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 740 | SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt); |
| 741 | if (rval) |
| 742 | return rval; |
| 743 | |
| 744 | dev_dbg(&client->dev, "bpp %d, compressed %d\n", |
| 745 | fmt >> 8, (u8)fmt); |
| 746 | |
| 747 | for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) { |
| 748 | const struct smiapp_csi_data_format *f = |
| 749 | &smiapp_csi_data_formats[j]; |
| 750 | |
| 751 | if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG) |
| 752 | continue; |
| 753 | |
| 754 | if (f->width != fmt >> 8 || f->compressed != (u8)fmt) |
| 755 | continue; |
| 756 | |
| 757 | dev_dbg(&client->dev, "jolly good! %d\n", j); |
| 758 | |
| 759 | sensor->default_mbus_frame_fmts |= 1 << j; |
Sakari Ailus | f67e157 | 2012-09-15 17:19:49 -0300 | [diff] [blame] | 760 | if (!sensor->csi_format |
| 761 | || f->width > sensor->csi_format->width |
| 762 | || (f->width == sensor->csi_format->width |
| 763 | && f->compressed |
| 764 | > sensor->csi_format->compressed)) { |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 765 | sensor->csi_format = f; |
| 766 | sensor->internal_csi_format = f; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (!sensor->csi_format) { |
| 772 | dev_err(&client->dev, "no supported mbus code found\n"); |
| 773 | return -EINVAL; |
| 774 | } |
| 775 | |
| 776 | smiapp_update_mbus_formats(sensor); |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | static void smiapp_update_blanking(struct smiapp_sensor *sensor) |
| 782 | { |
| 783 | struct v4l2_ctrl *vblank = sensor->vblank; |
| 784 | struct v4l2_ctrl *hblank = sensor->hblank; |
| 785 | |
| 786 | vblank->minimum = |
| 787 | max_t(int, |
| 788 | sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES], |
| 789 | sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] - |
| 790 | sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height); |
| 791 | vblank->maximum = |
| 792 | sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] - |
| 793 | sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height; |
| 794 | |
| 795 | vblank->val = clamp_t(int, vblank->val, |
| 796 | vblank->minimum, vblank->maximum); |
| 797 | vblank->default_value = vblank->minimum; |
| 798 | vblank->val = vblank->val; |
| 799 | vblank->cur.val = vblank->val; |
| 800 | |
| 801 | hblank->minimum = |
| 802 | max_t(int, |
| 803 | sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] - |
| 804 | sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width, |
| 805 | sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]); |
| 806 | hblank->maximum = |
| 807 | sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] - |
| 808 | sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width; |
| 809 | |
| 810 | hblank->val = clamp_t(int, hblank->val, |
| 811 | hblank->minimum, hblank->maximum); |
| 812 | hblank->default_value = hblank->minimum; |
| 813 | hblank->val = hblank->val; |
| 814 | hblank->cur.val = hblank->val; |
| 815 | |
| 816 | __smiapp_update_exposure_limits(sensor); |
| 817 | } |
| 818 | |
| 819 | static int smiapp_update_mode(struct smiapp_sensor *sensor) |
| 820 | { |
| 821 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 822 | unsigned int binning_mode; |
| 823 | int rval; |
| 824 | |
| 825 | dev_dbg(&client->dev, "frame size: %dx%d\n", |
| 826 | sensor->src->crop[SMIAPP_PAD_SRC].width, |
| 827 | sensor->src->crop[SMIAPP_PAD_SRC].height); |
| 828 | dev_dbg(&client->dev, "csi format width: %d\n", |
| 829 | sensor->csi_format->width); |
| 830 | |
| 831 | /* Binning has to be set up here; it affects limits */ |
| 832 | if (sensor->binning_horizontal == 1 && |
| 833 | sensor->binning_vertical == 1) { |
| 834 | binning_mode = 0; |
| 835 | } else { |
| 836 | u8 binning_type = |
| 837 | (sensor->binning_horizontal << 4) |
| 838 | | sensor->binning_vertical; |
| 839 | |
| 840 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 841 | sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 842 | if (rval < 0) |
| 843 | return rval; |
| 844 | |
| 845 | binning_mode = 1; |
| 846 | } |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 847 | rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 848 | if (rval < 0) |
| 849 | return rval; |
| 850 | |
| 851 | /* Get updated limits due to binning */ |
| 852 | rval = smiapp_get_limits_binning(sensor); |
| 853 | if (rval < 0) |
| 854 | return rval; |
| 855 | |
| 856 | rval = smiapp_pll_update(sensor); |
| 857 | if (rval < 0) |
| 858 | return rval; |
| 859 | |
| 860 | /* Output from pixel array, including blanking */ |
| 861 | smiapp_update_blanking(sensor); |
| 862 | |
| 863 | dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val); |
| 864 | dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val); |
| 865 | |
| 866 | dev_dbg(&client->dev, "real timeperframe\t100/%d\n", |
| 867 | sensor->pll.vt_pix_clk_freq_hz / |
| 868 | ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width |
| 869 | + sensor->hblank->val) * |
| 870 | (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height |
| 871 | + sensor->vblank->val) / 100)); |
| 872 | |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * |
| 878 | * SMIA++ NVM handling |
| 879 | * |
| 880 | */ |
| 881 | static int smiapp_read_nvm(struct smiapp_sensor *sensor, |
| 882 | unsigned char *nvm) |
| 883 | { |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 884 | u32 i, s, p, np, v; |
Sakari Ailus | 0458294 | 2012-04-26 12:05:50 -0300 | [diff] [blame] | 885 | int rval = 0, rval2; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 886 | |
| 887 | np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE; |
| 888 | for (p = 0; p < np; p++) { |
| 889 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 890 | sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 891 | SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p); |
| 892 | if (rval) |
| 893 | goto out; |
| 894 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 895 | rval = smiapp_write(sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 896 | SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, |
| 897 | SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN | |
| 898 | SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN); |
| 899 | if (rval) |
| 900 | goto out; |
| 901 | |
| 902 | for (i = 0; i < 1000; i++) { |
| 903 | rval = smiapp_read( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 904 | sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 905 | SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s); |
| 906 | |
| 907 | if (rval) |
| 908 | goto out; |
| 909 | |
| 910 | if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY) |
| 911 | break; |
| 912 | |
| 913 | if (--i == 0) { |
| 914 | rval = -ETIMEDOUT; |
| 915 | goto out; |
| 916 | } |
| 917 | |
| 918 | } |
| 919 | |
| 920 | for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) { |
| 921 | rval = smiapp_read( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 922 | sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 923 | SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i, |
| 924 | &v); |
| 925 | if (rval) |
| 926 | goto out; |
| 927 | |
| 928 | *nvm++ = v; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | out: |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 933 | rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 934 | if (rval < 0) |
| 935 | return rval; |
| 936 | else |
| 937 | return rval2; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * |
| 942 | * SMIA++ CCI address control |
| 943 | * |
| 944 | */ |
| 945 | static int smiapp_change_cci_addr(struct smiapp_sensor *sensor) |
| 946 | { |
| 947 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 948 | int rval; |
| 949 | u32 val; |
| 950 | |
| 951 | client->addr = sensor->platform_data->i2c_addr_dfl; |
| 952 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 953 | rval = smiapp_write(sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 954 | SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, |
| 955 | sensor->platform_data->i2c_addr_alt << 1); |
| 956 | if (rval) |
| 957 | return rval; |
| 958 | |
| 959 | client->addr = sensor->platform_data->i2c_addr_alt; |
| 960 | |
| 961 | /* verify addr change went ok */ |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 962 | rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 963 | if (rval) |
| 964 | return rval; |
| 965 | |
| 966 | if (val != sensor->platform_data->i2c_addr_alt << 1) |
| 967 | return -ENODEV; |
| 968 | |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * |
| 974 | * SMIA++ Mode Control |
| 975 | * |
| 976 | */ |
| 977 | static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor) |
| 978 | { |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 979 | struct smiapp_flash_strobe_parms *strobe_setup; |
| 980 | unsigned int ext_freq = sensor->platform_data->ext_clk; |
| 981 | u32 tmp; |
| 982 | u32 strobe_adjustment; |
| 983 | u32 strobe_width_high_rs; |
| 984 | int rval; |
| 985 | |
| 986 | strobe_setup = sensor->platform_data->strobe_setup; |
| 987 | |
| 988 | /* |
| 989 | * How to calculate registers related to strobe length. Please |
| 990 | * do not change, or if you do at least know what you're |
| 991 | * doing. :-) |
| 992 | * |
Sakari Ailus | 8c5dff9 | 2012-10-28 06:44:17 -0300 | [diff] [blame] | 993 | * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25 |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 994 | * |
| 995 | * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl |
| 996 | * / EXTCLK freq [Hz]) * flash_strobe_adjustment |
| 997 | * |
| 998 | * tFlash_strobe_width_ctrl E N, [1 - 0xffff] |
| 999 | * flash_strobe_adjustment E N, [1 - 0xff] |
| 1000 | * |
| 1001 | * The formula above is written as below to keep it on one |
| 1002 | * line: |
| 1003 | * |
| 1004 | * l / 10^6 = w / e * a |
| 1005 | * |
| 1006 | * Let's mark w * a by x: |
| 1007 | * |
| 1008 | * x = w * a |
| 1009 | * |
| 1010 | * Thus, we get: |
| 1011 | * |
| 1012 | * x = l * e / 10^6 |
| 1013 | * |
| 1014 | * The strobe width must be at least as long as requested, |
| 1015 | * thus rounding upwards is needed. |
| 1016 | * |
| 1017 | * x = (l * e + 10^6 - 1) / 10^6 |
| 1018 | * ----------------------------- |
| 1019 | * |
| 1020 | * Maximum possible accuracy is wanted at all times. Thus keep |
| 1021 | * a as small as possible. |
| 1022 | * |
| 1023 | * Calculate a, assuming maximum w, with rounding upwards: |
| 1024 | * |
| 1025 | * a = (x + (2^16 - 1) - 1) / (2^16 - 1) |
| 1026 | * ------------------------------------- |
| 1027 | * |
| 1028 | * Thus, we also get w, with that a, with rounding upwards: |
| 1029 | * |
| 1030 | * w = (x + a - 1) / a |
| 1031 | * ------------------- |
| 1032 | * |
| 1033 | * To get limits: |
| 1034 | * |
| 1035 | * x E [1, (2^16 - 1) * (2^8 - 1)] |
| 1036 | * |
| 1037 | * Substituting maximum x to the original formula (with rounding), |
| 1038 | * the maximum l is thus |
| 1039 | * |
| 1040 | * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1 |
| 1041 | * |
| 1042 | * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e |
| 1043 | * -------------------------------------------------- |
| 1044 | * |
| 1045 | * flash_strobe_length must be clamped between 1 and |
| 1046 | * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq. |
| 1047 | * |
| 1048 | * Then, |
| 1049 | * |
| 1050 | * flash_strobe_adjustment = ((flash_strobe_length * |
| 1051 | * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1) |
| 1052 | * |
| 1053 | * tFlash_strobe_width_ctrl = ((flash_strobe_length * |
| 1054 | * EXTCLK freq + 10^6 - 1) / 10^6 + |
| 1055 | * flash_strobe_adjustment - 1) / flash_strobe_adjustment |
| 1056 | */ |
| 1057 | tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) - |
| 1058 | 1000000 + 1, ext_freq); |
| 1059 | strobe_setup->strobe_width_high_us = |
| 1060 | clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp); |
| 1061 | |
| 1062 | tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq + |
| 1063 | 1000000 - 1), 1000000ULL); |
| 1064 | strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1); |
| 1065 | strobe_width_high_rs = (tmp + strobe_adjustment - 1) / |
| 1066 | strobe_adjustment; |
| 1067 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1068 | rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1069 | strobe_setup->mode); |
| 1070 | if (rval < 0) |
| 1071 | goto out; |
| 1072 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1073 | rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1074 | strobe_adjustment); |
| 1075 | if (rval < 0) |
| 1076 | goto out; |
| 1077 | |
| 1078 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1079 | sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1080 | strobe_width_high_rs); |
| 1081 | if (rval < 0) |
| 1082 | goto out; |
| 1083 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1084 | rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1085 | strobe_setup->strobe_delay); |
| 1086 | if (rval < 0) |
| 1087 | goto out; |
| 1088 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1089 | rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1090 | strobe_setup->stobe_start_point); |
| 1091 | if (rval < 0) |
| 1092 | goto out; |
| 1093 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1094 | rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1095 | strobe_setup->trigger); |
| 1096 | |
| 1097 | out: |
| 1098 | sensor->platform_data->strobe_setup->trigger = 0; |
| 1099 | |
| 1100 | return rval; |
| 1101 | } |
| 1102 | |
| 1103 | /* ----------------------------------------------------------------------------- |
| 1104 | * Power management |
| 1105 | */ |
| 1106 | |
| 1107 | static int smiapp_power_on(struct smiapp_sensor *sensor) |
| 1108 | { |
| 1109 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 1110 | unsigned int sleep; |
| 1111 | int rval; |
| 1112 | |
| 1113 | rval = regulator_enable(sensor->vana); |
| 1114 | if (rval) { |
| 1115 | dev_err(&client->dev, "failed to enable vana regulator\n"); |
| 1116 | return rval; |
| 1117 | } |
| 1118 | usleep_range(1000, 1000); |
| 1119 | |
Sakari Ailus | 2547428 | 2012-04-22 08:24:33 -0300 | [diff] [blame] | 1120 | if (sensor->platform_data->set_xclk) |
| 1121 | rval = sensor->platform_data->set_xclk( |
| 1122 | &sensor->src->sd, sensor->platform_data->ext_clk); |
| 1123 | else |
Sakari Ailus | d0aae00 | 2013-08-10 14:49:47 -0300 | [diff] [blame] | 1124 | rval = clk_prepare_enable(sensor->ext_clk); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1125 | if (rval < 0) { |
Sakari Ailus | d0aae00 | 2013-08-10 14:49:47 -0300 | [diff] [blame] | 1126 | dev_dbg(&client->dev, "failed to enable xclk\n"); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1127 | goto out_xclk_fail; |
| 1128 | } |
| 1129 | usleep_range(1000, 1000); |
| 1130 | |
| 1131 | if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) |
| 1132 | gpio_set_value(sensor->platform_data->xshutdown, 1); |
| 1133 | |
| 1134 | sleep = SMIAPP_RESET_DELAY(sensor->platform_data->ext_clk); |
| 1135 | usleep_range(sleep, sleep); |
| 1136 | |
| 1137 | /* |
| 1138 | * Failures to respond to the address change command have been noticed. |
| 1139 | * Those failures seem to be caused by the sensor requiring a longer |
| 1140 | * boot time than advertised. An additional 10ms delay seems to work |
| 1141 | * around the issue, but the SMIA++ I2C write retry hack makes the delay |
| 1142 | * unnecessary. The failures need to be investigated to find a proper |
| 1143 | * fix, and a delay will likely need to be added here if the I2C write |
| 1144 | * retry hack is reverted before the root cause of the boot time issue |
| 1145 | * is found. |
| 1146 | */ |
| 1147 | |
| 1148 | if (sensor->platform_data->i2c_addr_alt) { |
| 1149 | rval = smiapp_change_cci_addr(sensor); |
| 1150 | if (rval) { |
| 1151 | dev_err(&client->dev, "cci address change error\n"); |
| 1152 | goto out_cci_addr_fail; |
| 1153 | } |
| 1154 | } |
| 1155 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1156 | rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1157 | SMIAPP_SOFTWARE_RESET); |
| 1158 | if (rval < 0) { |
| 1159 | dev_err(&client->dev, "software reset failed\n"); |
| 1160 | goto out_cci_addr_fail; |
| 1161 | } |
| 1162 | |
| 1163 | if (sensor->platform_data->i2c_addr_alt) { |
| 1164 | rval = smiapp_change_cci_addr(sensor); |
| 1165 | if (rval) { |
| 1166 | dev_err(&client->dev, "cci address change error\n"); |
| 1167 | goto out_cci_addr_fail; |
| 1168 | } |
| 1169 | } |
| 1170 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1171 | rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1172 | SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR); |
| 1173 | if (rval) { |
| 1174 | dev_err(&client->dev, "compression mode set failed\n"); |
| 1175 | goto out_cci_addr_fail; |
| 1176 | } |
| 1177 | |
| 1178 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1179 | sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1180 | sensor->platform_data->ext_clk / (1000000 / (1 << 8))); |
| 1181 | if (rval) { |
| 1182 | dev_err(&client->dev, "extclk frequency set failed\n"); |
| 1183 | goto out_cci_addr_fail; |
| 1184 | } |
| 1185 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1186 | rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1187 | sensor->platform_data->lanes - 1); |
| 1188 | if (rval) { |
| 1189 | dev_err(&client->dev, "csi lane mode set failed\n"); |
| 1190 | goto out_cci_addr_fail; |
| 1191 | } |
| 1192 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1193 | rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1194 | SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE); |
| 1195 | if (rval) { |
| 1196 | dev_err(&client->dev, "fast standby set failed\n"); |
| 1197 | goto out_cci_addr_fail; |
| 1198 | } |
| 1199 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1200 | rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1201 | sensor->platform_data->csi_signalling_mode); |
| 1202 | if (rval) { |
| 1203 | dev_err(&client->dev, "csi signalling mode set failed\n"); |
| 1204 | goto out_cci_addr_fail; |
| 1205 | } |
| 1206 | |
| 1207 | /* DPHY control done by sensor based on requested link rate */ |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1208 | rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1209 | SMIAPP_DPHY_CTRL_UI); |
| 1210 | if (rval < 0) |
| 1211 | return rval; |
| 1212 | |
| 1213 | rval = smiapp_call_quirk(sensor, post_poweron); |
| 1214 | if (rval) { |
| 1215 | dev_err(&client->dev, "post_poweron quirks failed\n"); |
| 1216 | goto out_cci_addr_fail; |
| 1217 | } |
| 1218 | |
| 1219 | /* Are we still initialising...? If yes, return here. */ |
| 1220 | if (!sensor->pixel_array) |
| 1221 | return 0; |
| 1222 | |
| 1223 | rval = v4l2_ctrl_handler_setup( |
| 1224 | &sensor->pixel_array->ctrl_handler); |
| 1225 | if (rval) |
| 1226 | goto out_cci_addr_fail; |
| 1227 | |
| 1228 | rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler); |
| 1229 | if (rval) |
| 1230 | goto out_cci_addr_fail; |
| 1231 | |
| 1232 | mutex_lock(&sensor->mutex); |
| 1233 | rval = smiapp_update_mode(sensor); |
| 1234 | mutex_unlock(&sensor->mutex); |
| 1235 | if (rval < 0) |
| 1236 | goto out_cci_addr_fail; |
| 1237 | |
| 1238 | return 0; |
| 1239 | |
| 1240 | out_cci_addr_fail: |
| 1241 | if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) |
| 1242 | gpio_set_value(sensor->platform_data->xshutdown, 0); |
Sakari Ailus | 2547428 | 2012-04-22 08:24:33 -0300 | [diff] [blame] | 1243 | if (sensor->platform_data->set_xclk) |
| 1244 | sensor->platform_data->set_xclk(&sensor->src->sd, 0); |
| 1245 | else |
Sakari Ailus | d0aae00 | 2013-08-10 14:49:47 -0300 | [diff] [blame] | 1246 | clk_disable_unprepare(sensor->ext_clk); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1247 | |
| 1248 | out_xclk_fail: |
| 1249 | regulator_disable(sensor->vana); |
| 1250 | return rval; |
| 1251 | } |
| 1252 | |
| 1253 | static void smiapp_power_off(struct smiapp_sensor *sensor) |
| 1254 | { |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1255 | /* |
| 1256 | * Currently power/clock to lens are enable/disabled separately |
| 1257 | * but they are essentially the same signals. So if the sensor is |
| 1258 | * powered off while the lens is powered on the sensor does not |
| 1259 | * really see a power off and next time the cci address change |
| 1260 | * will fail. So do a soft reset explicitly here. |
| 1261 | */ |
| 1262 | if (sensor->platform_data->i2c_addr_alt) |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1263 | smiapp_write(sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1264 | SMIAPP_REG_U8_SOFTWARE_RESET, |
| 1265 | SMIAPP_SOFTWARE_RESET); |
| 1266 | |
| 1267 | if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) |
| 1268 | gpio_set_value(sensor->platform_data->xshutdown, 0); |
Sakari Ailus | 2547428 | 2012-04-22 08:24:33 -0300 | [diff] [blame] | 1269 | if (sensor->platform_data->set_xclk) |
| 1270 | sensor->platform_data->set_xclk(&sensor->src->sd, 0); |
| 1271 | else |
Sakari Ailus | d0aae00 | 2013-08-10 14:49:47 -0300 | [diff] [blame] | 1272 | clk_disable_unprepare(sensor->ext_clk); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1273 | usleep_range(5000, 5000); |
| 1274 | regulator_disable(sensor->vana); |
| 1275 | sensor->streaming = 0; |
| 1276 | } |
| 1277 | |
| 1278 | static int smiapp_set_power(struct v4l2_subdev *subdev, int on) |
| 1279 | { |
| 1280 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1281 | int ret = 0; |
| 1282 | |
| 1283 | mutex_lock(&sensor->power_mutex); |
| 1284 | |
| 1285 | /* |
| 1286 | * If the power count is modified from 0 to != 0 or from != 0 |
| 1287 | * to 0, update the power state. |
| 1288 | */ |
| 1289 | if (!sensor->power_count == !on) |
| 1290 | goto out; |
| 1291 | |
| 1292 | if (on) { |
| 1293 | /* Power on and perform initialisation. */ |
| 1294 | ret = smiapp_power_on(sensor); |
| 1295 | if (ret < 0) |
| 1296 | goto out; |
| 1297 | } else { |
| 1298 | smiapp_power_off(sensor); |
| 1299 | } |
| 1300 | |
| 1301 | /* Update the power count. */ |
| 1302 | sensor->power_count += on ? 1 : -1; |
| 1303 | WARN_ON(sensor->power_count < 0); |
| 1304 | |
| 1305 | out: |
| 1306 | mutex_unlock(&sensor->power_mutex); |
| 1307 | return ret; |
| 1308 | } |
| 1309 | |
| 1310 | /* ----------------------------------------------------------------------------- |
| 1311 | * Video stream management |
| 1312 | */ |
| 1313 | |
| 1314 | static int smiapp_start_streaming(struct smiapp_sensor *sensor) |
| 1315 | { |
| 1316 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 1317 | int rval; |
| 1318 | |
| 1319 | mutex_lock(&sensor->mutex); |
| 1320 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1321 | rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1322 | (sensor->csi_format->width << 8) | |
| 1323 | sensor->csi_format->compressed); |
| 1324 | if (rval) |
| 1325 | goto out; |
| 1326 | |
| 1327 | rval = smiapp_pll_configure(sensor); |
| 1328 | if (rval) |
| 1329 | goto out; |
| 1330 | |
| 1331 | /* Analog crop start coordinates */ |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1332 | rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1333 | sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left); |
| 1334 | if (rval < 0) |
| 1335 | goto out; |
| 1336 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1337 | rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1338 | sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top); |
| 1339 | if (rval < 0) |
| 1340 | goto out; |
| 1341 | |
| 1342 | /* Analog crop end coordinates */ |
| 1343 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1344 | sensor, SMIAPP_REG_U16_X_ADDR_END, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1345 | sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left |
| 1346 | + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1); |
| 1347 | if (rval < 0) |
| 1348 | goto out; |
| 1349 | |
| 1350 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1351 | sensor, SMIAPP_REG_U16_Y_ADDR_END, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1352 | sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top |
| 1353 | + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1); |
| 1354 | if (rval < 0) |
| 1355 | goto out; |
| 1356 | |
| 1357 | /* |
| 1358 | * Output from pixel array, including blanking, is set using |
| 1359 | * controls below. No need to set here. |
| 1360 | */ |
| 1361 | |
| 1362 | /* Digital crop */ |
| 1363 | if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY] |
| 1364 | == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) { |
| 1365 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1366 | sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1367 | sensor->scaler->crop[SMIAPP_PAD_SINK].left); |
| 1368 | if (rval < 0) |
| 1369 | goto out; |
| 1370 | |
| 1371 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1372 | sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1373 | sensor->scaler->crop[SMIAPP_PAD_SINK].top); |
| 1374 | if (rval < 0) |
| 1375 | goto out; |
| 1376 | |
| 1377 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1378 | sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1379 | sensor->scaler->crop[SMIAPP_PAD_SINK].width); |
| 1380 | if (rval < 0) |
| 1381 | goto out; |
| 1382 | |
| 1383 | rval = smiapp_write( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1384 | sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1385 | sensor->scaler->crop[SMIAPP_PAD_SINK].height); |
| 1386 | if (rval < 0) |
| 1387 | goto out; |
| 1388 | } |
| 1389 | |
| 1390 | /* Scaling */ |
| 1391 | if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY] |
| 1392 | != SMIAPP_SCALING_CAPABILITY_NONE) { |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1393 | rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1394 | sensor->scaling_mode); |
| 1395 | if (rval < 0) |
| 1396 | goto out; |
| 1397 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1398 | rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1399 | sensor->scale_m); |
| 1400 | if (rval < 0) |
| 1401 | goto out; |
| 1402 | } |
| 1403 | |
| 1404 | /* Output size from sensor */ |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1405 | rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1406 | sensor->src->crop[SMIAPP_PAD_SRC].width); |
| 1407 | if (rval < 0) |
| 1408 | goto out; |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1409 | rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1410 | sensor->src->crop[SMIAPP_PAD_SRC].height); |
| 1411 | if (rval < 0) |
| 1412 | goto out; |
| 1413 | |
| 1414 | if ((sensor->flash_capability & |
| 1415 | (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE | |
| 1416 | SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) && |
| 1417 | sensor->platform_data->strobe_setup != NULL && |
| 1418 | sensor->platform_data->strobe_setup->trigger != 0) { |
| 1419 | rval = smiapp_setup_flash_strobe(sensor); |
| 1420 | if (rval) |
| 1421 | goto out; |
| 1422 | } |
| 1423 | |
| 1424 | rval = smiapp_call_quirk(sensor, pre_streamon); |
| 1425 | if (rval) { |
| 1426 | dev_err(&client->dev, "pre_streamon quirks failed\n"); |
| 1427 | goto out; |
| 1428 | } |
| 1429 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1430 | rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1431 | SMIAPP_MODE_SELECT_STREAMING); |
| 1432 | |
| 1433 | out: |
| 1434 | mutex_unlock(&sensor->mutex); |
| 1435 | |
| 1436 | return rval; |
| 1437 | } |
| 1438 | |
| 1439 | static int smiapp_stop_streaming(struct smiapp_sensor *sensor) |
| 1440 | { |
| 1441 | struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); |
| 1442 | int rval; |
| 1443 | |
| 1444 | mutex_lock(&sensor->mutex); |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 1445 | rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1446 | SMIAPP_MODE_SELECT_SOFTWARE_STANDBY); |
| 1447 | if (rval) |
| 1448 | goto out; |
| 1449 | |
| 1450 | rval = smiapp_call_quirk(sensor, post_streamoff); |
| 1451 | if (rval) |
| 1452 | dev_err(&client->dev, "post_streamoff quirks failed\n"); |
| 1453 | |
| 1454 | out: |
| 1455 | mutex_unlock(&sensor->mutex); |
| 1456 | return rval; |
| 1457 | } |
| 1458 | |
| 1459 | /* ----------------------------------------------------------------------------- |
| 1460 | * V4L2 subdev video operations |
| 1461 | */ |
| 1462 | |
| 1463 | static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable) |
| 1464 | { |
| 1465 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1466 | int rval; |
| 1467 | |
| 1468 | if (sensor->streaming == enable) |
| 1469 | return 0; |
| 1470 | |
| 1471 | if (enable) { |
| 1472 | sensor->streaming = 1; |
| 1473 | rval = smiapp_start_streaming(sensor); |
| 1474 | if (rval < 0) |
| 1475 | sensor->streaming = 0; |
| 1476 | } else { |
| 1477 | rval = smiapp_stop_streaming(sensor); |
| 1478 | sensor->streaming = 0; |
| 1479 | } |
| 1480 | |
| 1481 | return rval; |
| 1482 | } |
| 1483 | |
| 1484 | static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev, |
| 1485 | struct v4l2_subdev_fh *fh, |
| 1486 | struct v4l2_subdev_mbus_code_enum *code) |
| 1487 | { |
| 1488 | struct i2c_client *client = v4l2_get_subdevdata(subdev); |
| 1489 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1490 | unsigned int i; |
| 1491 | int idx = -1; |
| 1492 | int rval = -EINVAL; |
| 1493 | |
| 1494 | mutex_lock(&sensor->mutex); |
| 1495 | |
| 1496 | dev_err(&client->dev, "subdev %s, pad %d, index %d\n", |
| 1497 | subdev->name, code->pad, code->index); |
| 1498 | |
| 1499 | if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) { |
| 1500 | if (code->index) |
| 1501 | goto out; |
| 1502 | |
| 1503 | code->code = sensor->internal_csi_format->code; |
| 1504 | rval = 0; |
| 1505 | goto out; |
| 1506 | } |
| 1507 | |
| 1508 | for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) { |
| 1509 | if (sensor->mbus_frame_fmts & (1 << i)) |
| 1510 | idx++; |
| 1511 | |
| 1512 | if (idx == code->index) { |
| 1513 | code->code = smiapp_csi_data_formats[i].code; |
| 1514 | dev_err(&client->dev, "found index %d, i %d, code %x\n", |
| 1515 | code->index, i, code->code); |
| 1516 | rval = 0; |
| 1517 | break; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | out: |
| 1522 | mutex_unlock(&sensor->mutex); |
| 1523 | |
| 1524 | return rval; |
| 1525 | } |
| 1526 | |
| 1527 | static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev, |
| 1528 | unsigned int pad) |
| 1529 | { |
| 1530 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1531 | |
| 1532 | if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC) |
| 1533 | return sensor->csi_format->code; |
| 1534 | else |
| 1535 | return sensor->internal_csi_format->code; |
| 1536 | } |
| 1537 | |
| 1538 | static int __smiapp_get_format(struct v4l2_subdev *subdev, |
| 1539 | struct v4l2_subdev_fh *fh, |
| 1540 | struct v4l2_subdev_format *fmt) |
| 1541 | { |
| 1542 | struct smiapp_subdev *ssd = to_smiapp_subdev(subdev); |
| 1543 | |
| 1544 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1545 | fmt->format = *v4l2_subdev_get_try_format(fh, fmt->pad); |
| 1546 | } else { |
| 1547 | struct v4l2_rect *r; |
| 1548 | |
| 1549 | if (fmt->pad == ssd->source_pad) |
| 1550 | r = &ssd->crop[ssd->source_pad]; |
| 1551 | else |
| 1552 | r = &ssd->sink_fmt; |
| 1553 | |
| 1554 | fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad); |
| 1555 | fmt->format.width = r->width; |
| 1556 | fmt->format.height = r->height; |
| 1557 | } |
| 1558 | |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | static int smiapp_get_format(struct v4l2_subdev *subdev, |
| 1563 | struct v4l2_subdev_fh *fh, |
| 1564 | struct v4l2_subdev_format *fmt) |
| 1565 | { |
| 1566 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1567 | int rval; |
| 1568 | |
| 1569 | mutex_lock(&sensor->mutex); |
| 1570 | rval = __smiapp_get_format(subdev, fh, fmt); |
| 1571 | mutex_unlock(&sensor->mutex); |
| 1572 | |
| 1573 | return rval; |
| 1574 | } |
| 1575 | |
| 1576 | static void smiapp_get_crop_compose(struct v4l2_subdev *subdev, |
| 1577 | struct v4l2_subdev_fh *fh, |
| 1578 | struct v4l2_rect **crops, |
| 1579 | struct v4l2_rect **comps, int which) |
| 1580 | { |
| 1581 | struct smiapp_subdev *ssd = to_smiapp_subdev(subdev); |
| 1582 | unsigned int i; |
| 1583 | |
| 1584 | if (which == V4L2_SUBDEV_FORMAT_ACTIVE) { |
| 1585 | if (crops) |
| 1586 | for (i = 0; i < subdev->entity.num_pads; i++) |
| 1587 | crops[i] = &ssd->crop[i]; |
| 1588 | if (comps) |
| 1589 | *comps = &ssd->compose; |
| 1590 | } else { |
| 1591 | if (crops) { |
| 1592 | for (i = 0; i < subdev->entity.num_pads; i++) { |
| 1593 | crops[i] = v4l2_subdev_get_try_crop(fh, i); |
| 1594 | BUG_ON(!crops[i]); |
| 1595 | } |
| 1596 | } |
| 1597 | if (comps) { |
| 1598 | *comps = v4l2_subdev_get_try_compose(fh, |
| 1599 | SMIAPP_PAD_SINK); |
| 1600 | BUG_ON(!*comps); |
| 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | /* Changes require propagation only on sink pad. */ |
| 1606 | static void smiapp_propagate(struct v4l2_subdev *subdev, |
| 1607 | struct v4l2_subdev_fh *fh, int which, |
| 1608 | int target) |
| 1609 | { |
| 1610 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1611 | struct smiapp_subdev *ssd = to_smiapp_subdev(subdev); |
| 1612 | struct v4l2_rect *comp, *crops[SMIAPP_PADS]; |
| 1613 | |
| 1614 | smiapp_get_crop_compose(subdev, fh, crops, &comp, which); |
| 1615 | |
| 1616 | switch (target) { |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1617 | case V4L2_SEL_TGT_CROP: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1618 | comp->width = crops[SMIAPP_PAD_SINK]->width; |
| 1619 | comp->height = crops[SMIAPP_PAD_SINK]->height; |
| 1620 | if (which == V4L2_SUBDEV_FORMAT_ACTIVE) { |
| 1621 | if (ssd == sensor->scaler) { |
| 1622 | sensor->scale_m = |
| 1623 | sensor->limits[ |
| 1624 | SMIAPP_LIMIT_SCALER_N_MIN]; |
| 1625 | sensor->scaling_mode = |
| 1626 | SMIAPP_SCALING_MODE_NONE; |
| 1627 | } else if (ssd == sensor->binner) { |
| 1628 | sensor->binning_horizontal = 1; |
| 1629 | sensor->binning_vertical = 1; |
| 1630 | } |
| 1631 | } |
| 1632 | /* Fall through */ |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1633 | case V4L2_SEL_TGT_COMPOSE: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1634 | *crops[SMIAPP_PAD_SRC] = *comp; |
| 1635 | break; |
| 1636 | default: |
| 1637 | BUG(); |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | static const struct smiapp_csi_data_format |
| 1642 | *smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code) |
| 1643 | { |
| 1644 | const struct smiapp_csi_data_format *csi_format = sensor->csi_format; |
| 1645 | unsigned int i; |
| 1646 | |
| 1647 | for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) { |
| 1648 | if (sensor->mbus_frame_fmts & (1 << i) |
| 1649 | && smiapp_csi_data_formats[i].code == code) |
| 1650 | return &smiapp_csi_data_formats[i]; |
| 1651 | } |
| 1652 | |
| 1653 | return csi_format; |
| 1654 | } |
| 1655 | |
| 1656 | static int smiapp_set_format(struct v4l2_subdev *subdev, |
| 1657 | struct v4l2_subdev_fh *fh, |
| 1658 | struct v4l2_subdev_format *fmt) |
| 1659 | { |
| 1660 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1661 | struct smiapp_subdev *ssd = to_smiapp_subdev(subdev); |
| 1662 | struct v4l2_rect *crops[SMIAPP_PADS]; |
| 1663 | |
| 1664 | mutex_lock(&sensor->mutex); |
| 1665 | |
| 1666 | /* |
| 1667 | * Media bus code is changeable on src subdev's source pad. On |
| 1668 | * other source pads we just get format here. |
| 1669 | */ |
| 1670 | if (fmt->pad == ssd->source_pad) { |
| 1671 | u32 code = fmt->format.code; |
| 1672 | int rval = __smiapp_get_format(subdev, fh, fmt); |
| 1673 | |
| 1674 | if (!rval && subdev == &sensor->src->sd) { |
| 1675 | const struct smiapp_csi_data_format *csi_format = |
| 1676 | smiapp_validate_csi_data_format(sensor, code); |
| 1677 | if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) |
| 1678 | sensor->csi_format = csi_format; |
| 1679 | fmt->format.code = csi_format->code; |
| 1680 | } |
| 1681 | |
| 1682 | mutex_unlock(&sensor->mutex); |
| 1683 | return rval; |
| 1684 | } |
| 1685 | |
| 1686 | /* Sink pad. Width and height are changeable here. */ |
| 1687 | fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad); |
| 1688 | fmt->format.width &= ~1; |
| 1689 | fmt->format.height &= ~1; |
| 1690 | |
| 1691 | fmt->format.width = |
| 1692 | clamp(fmt->format.width, |
| 1693 | sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE], |
| 1694 | sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]); |
| 1695 | fmt->format.height = |
| 1696 | clamp(fmt->format.height, |
| 1697 | sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE], |
| 1698 | sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]); |
| 1699 | |
| 1700 | smiapp_get_crop_compose(subdev, fh, crops, NULL, fmt->which); |
| 1701 | |
| 1702 | crops[ssd->sink_pad]->left = 0; |
| 1703 | crops[ssd->sink_pad]->top = 0; |
| 1704 | crops[ssd->sink_pad]->width = fmt->format.width; |
| 1705 | crops[ssd->sink_pad]->height = fmt->format.height; |
| 1706 | if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) |
| 1707 | ssd->sink_fmt = *crops[ssd->sink_pad]; |
| 1708 | smiapp_propagate(subdev, fh, fmt->which, |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1709 | V4L2_SEL_TGT_CROP); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1710 | |
| 1711 | mutex_unlock(&sensor->mutex); |
| 1712 | |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * Calculate goodness of scaled image size compared to expected image |
| 1718 | * size and flags provided. |
| 1719 | */ |
| 1720 | #define SCALING_GOODNESS 100000 |
| 1721 | #define SCALING_GOODNESS_EXTREME 100000000 |
| 1722 | static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w, |
| 1723 | int h, int ask_h, u32 flags) |
| 1724 | { |
| 1725 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1726 | struct i2c_client *client = v4l2_get_subdevdata(subdev); |
| 1727 | int val = 0; |
| 1728 | |
| 1729 | w &= ~1; |
| 1730 | ask_w &= ~1; |
| 1731 | h &= ~1; |
| 1732 | ask_h &= ~1; |
| 1733 | |
Sakari Ailus | 563df3d | 2012-06-13 16:01:10 -0300 | [diff] [blame] | 1734 | if (flags & V4L2_SEL_FLAG_GE) { |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1735 | if (w < ask_w) |
| 1736 | val -= SCALING_GOODNESS; |
| 1737 | if (h < ask_h) |
| 1738 | val -= SCALING_GOODNESS; |
| 1739 | } |
| 1740 | |
Sakari Ailus | 563df3d | 2012-06-13 16:01:10 -0300 | [diff] [blame] | 1741 | if (flags & V4L2_SEL_FLAG_LE) { |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1742 | if (w > ask_w) |
| 1743 | val -= SCALING_GOODNESS; |
| 1744 | if (h > ask_h) |
| 1745 | val -= SCALING_GOODNESS; |
| 1746 | } |
| 1747 | |
| 1748 | val -= abs(w - ask_w); |
| 1749 | val -= abs(h - ask_h); |
| 1750 | |
| 1751 | if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE]) |
| 1752 | val -= SCALING_GOODNESS_EXTREME; |
| 1753 | |
| 1754 | dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n", |
| 1755 | w, ask_h, h, ask_h, val); |
| 1756 | |
| 1757 | return val; |
| 1758 | } |
| 1759 | |
| 1760 | static void smiapp_set_compose_binner(struct v4l2_subdev *subdev, |
| 1761 | struct v4l2_subdev_fh *fh, |
| 1762 | struct v4l2_subdev_selection *sel, |
| 1763 | struct v4l2_rect **crops, |
| 1764 | struct v4l2_rect *comp) |
| 1765 | { |
| 1766 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1767 | unsigned int i; |
| 1768 | unsigned int binh = 1, binv = 1; |
| 1769 | unsigned int best = scaling_goodness( |
| 1770 | subdev, |
| 1771 | crops[SMIAPP_PAD_SINK]->width, sel->r.width, |
| 1772 | crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags); |
| 1773 | |
| 1774 | for (i = 0; i < sensor->nbinning_subtypes; i++) { |
| 1775 | int this = scaling_goodness( |
| 1776 | subdev, |
| 1777 | crops[SMIAPP_PAD_SINK]->width |
| 1778 | / sensor->binning_subtypes[i].horizontal, |
| 1779 | sel->r.width, |
| 1780 | crops[SMIAPP_PAD_SINK]->height |
| 1781 | / sensor->binning_subtypes[i].vertical, |
| 1782 | sel->r.height, sel->flags); |
| 1783 | |
| 1784 | if (this > best) { |
| 1785 | binh = sensor->binning_subtypes[i].horizontal; |
| 1786 | binv = sensor->binning_subtypes[i].vertical; |
| 1787 | best = this; |
| 1788 | } |
| 1789 | } |
| 1790 | if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { |
| 1791 | sensor->binning_vertical = binv; |
| 1792 | sensor->binning_horizontal = binh; |
| 1793 | } |
| 1794 | |
| 1795 | sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1; |
| 1796 | sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1; |
| 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * Calculate best scaling ratio and mode for given output resolution. |
| 1801 | * |
| 1802 | * Try all of these: horizontal ratio, vertical ratio and smallest |
| 1803 | * size possible (horizontally). |
| 1804 | * |
| 1805 | * Also try whether horizontal scaler or full scaler gives a better |
| 1806 | * result. |
| 1807 | */ |
| 1808 | static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev, |
| 1809 | struct v4l2_subdev_fh *fh, |
| 1810 | struct v4l2_subdev_selection *sel, |
| 1811 | struct v4l2_rect **crops, |
| 1812 | struct v4l2_rect *comp) |
| 1813 | { |
| 1814 | struct i2c_client *client = v4l2_get_subdevdata(subdev); |
| 1815 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1816 | u32 min, max, a, b, max_m; |
| 1817 | u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]; |
| 1818 | int mode = SMIAPP_SCALING_MODE_HORIZONTAL; |
| 1819 | u32 try[4]; |
| 1820 | u32 ntry = 0; |
| 1821 | unsigned int i; |
| 1822 | int best = INT_MIN; |
| 1823 | |
| 1824 | sel->r.width = min_t(unsigned int, sel->r.width, |
| 1825 | crops[SMIAPP_PAD_SINK]->width); |
| 1826 | sel->r.height = min_t(unsigned int, sel->r.height, |
| 1827 | crops[SMIAPP_PAD_SINK]->height); |
| 1828 | |
| 1829 | a = crops[SMIAPP_PAD_SINK]->width |
| 1830 | * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width; |
| 1831 | b = crops[SMIAPP_PAD_SINK]->height |
| 1832 | * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height; |
| 1833 | max_m = crops[SMIAPP_PAD_SINK]->width |
| 1834 | * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] |
| 1835 | / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE]; |
| 1836 | |
Andy Shevchenko | 7be5c28 | 2013-08-10 14:49:45 -0300 | [diff] [blame] | 1837 | a = clamp(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN], |
| 1838 | sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]); |
| 1839 | b = clamp(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN], |
| 1840 | sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]); |
| 1841 | max_m = clamp(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN], |
| 1842 | sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1843 | |
| 1844 | dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m); |
| 1845 | |
| 1846 | min = min(max_m, min(a, b)); |
| 1847 | max = min(max_m, max(a, b)); |
| 1848 | |
| 1849 | try[ntry] = min; |
| 1850 | ntry++; |
| 1851 | if (min != max) { |
| 1852 | try[ntry] = max; |
| 1853 | ntry++; |
| 1854 | } |
| 1855 | if (max != max_m) { |
| 1856 | try[ntry] = min + 1; |
| 1857 | ntry++; |
| 1858 | if (min != max) { |
| 1859 | try[ntry] = max + 1; |
| 1860 | ntry++; |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | for (i = 0; i < ntry; i++) { |
| 1865 | int this = scaling_goodness( |
| 1866 | subdev, |
| 1867 | crops[SMIAPP_PAD_SINK]->width |
| 1868 | / try[i] |
| 1869 | * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN], |
| 1870 | sel->r.width, |
| 1871 | crops[SMIAPP_PAD_SINK]->height, |
| 1872 | sel->r.height, |
| 1873 | sel->flags); |
| 1874 | |
| 1875 | dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i); |
| 1876 | |
| 1877 | if (this > best) { |
| 1878 | scale_m = try[i]; |
| 1879 | mode = SMIAPP_SCALING_MODE_HORIZONTAL; |
| 1880 | best = this; |
| 1881 | } |
| 1882 | |
| 1883 | if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY] |
| 1884 | == SMIAPP_SCALING_CAPABILITY_HORIZONTAL) |
| 1885 | continue; |
| 1886 | |
| 1887 | this = scaling_goodness( |
| 1888 | subdev, crops[SMIAPP_PAD_SINK]->width |
| 1889 | / try[i] |
| 1890 | * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN], |
| 1891 | sel->r.width, |
| 1892 | crops[SMIAPP_PAD_SINK]->height |
| 1893 | / try[i] |
| 1894 | * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN], |
| 1895 | sel->r.height, |
| 1896 | sel->flags); |
| 1897 | |
| 1898 | if (this > best) { |
| 1899 | scale_m = try[i]; |
| 1900 | mode = SMIAPP_SCALING_MODE_BOTH; |
| 1901 | best = this; |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | sel->r.width = |
| 1906 | (crops[SMIAPP_PAD_SINK]->width |
| 1907 | / scale_m |
| 1908 | * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1; |
| 1909 | if (mode == SMIAPP_SCALING_MODE_BOTH) |
| 1910 | sel->r.height = |
| 1911 | (crops[SMIAPP_PAD_SINK]->height |
| 1912 | / scale_m |
| 1913 | * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) |
| 1914 | & ~1; |
| 1915 | else |
| 1916 | sel->r.height = crops[SMIAPP_PAD_SINK]->height; |
| 1917 | |
| 1918 | if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { |
| 1919 | sensor->scale_m = scale_m; |
| 1920 | sensor->scaling_mode = mode; |
| 1921 | } |
| 1922 | } |
| 1923 | /* We're only called on source pads. This function sets scaling. */ |
| 1924 | static int smiapp_set_compose(struct v4l2_subdev *subdev, |
| 1925 | struct v4l2_subdev_fh *fh, |
| 1926 | struct v4l2_subdev_selection *sel) |
| 1927 | { |
| 1928 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1929 | struct smiapp_subdev *ssd = to_smiapp_subdev(subdev); |
| 1930 | struct v4l2_rect *comp, *crops[SMIAPP_PADS]; |
| 1931 | |
| 1932 | smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which); |
| 1933 | |
| 1934 | sel->r.top = 0; |
| 1935 | sel->r.left = 0; |
| 1936 | |
| 1937 | if (ssd == sensor->binner) |
| 1938 | smiapp_set_compose_binner(subdev, fh, sel, crops, comp); |
| 1939 | else |
| 1940 | smiapp_set_compose_scaler(subdev, fh, sel, crops, comp); |
| 1941 | |
| 1942 | *comp = sel->r; |
| 1943 | smiapp_propagate(subdev, fh, sel->which, |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1944 | V4L2_SEL_TGT_COMPOSE); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1945 | |
| 1946 | if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) |
| 1947 | return smiapp_update_mode(sensor); |
| 1948 | |
| 1949 | return 0; |
| 1950 | } |
| 1951 | |
| 1952 | static int __smiapp_sel_supported(struct v4l2_subdev *subdev, |
| 1953 | struct v4l2_subdev_selection *sel) |
| 1954 | { |
| 1955 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1956 | struct smiapp_subdev *ssd = to_smiapp_subdev(subdev); |
| 1957 | |
| 1958 | /* We only implement crop in three places. */ |
| 1959 | switch (sel->target) { |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1960 | case V4L2_SEL_TGT_CROP: |
| 1961 | case V4L2_SEL_TGT_CROP_BOUNDS: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1962 | if (ssd == sensor->pixel_array |
| 1963 | && sel->pad == SMIAPP_PA_PAD_SRC) |
| 1964 | return 0; |
| 1965 | if (ssd == sensor->src |
| 1966 | && sel->pad == SMIAPP_PAD_SRC) |
| 1967 | return 0; |
| 1968 | if (ssd == sensor->scaler |
| 1969 | && sel->pad == SMIAPP_PAD_SINK |
| 1970 | && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY] |
| 1971 | == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) |
| 1972 | return 0; |
| 1973 | return -EINVAL; |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1974 | case V4L2_SEL_TGT_COMPOSE: |
| 1975 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 1976 | if (sel->pad == ssd->source_pad) |
| 1977 | return -EINVAL; |
| 1978 | if (ssd == sensor->binner) |
| 1979 | return 0; |
| 1980 | if (ssd == sensor->scaler |
| 1981 | && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY] |
| 1982 | != SMIAPP_SCALING_CAPABILITY_NONE) |
| 1983 | return 0; |
| 1984 | /* Fall through */ |
| 1985 | default: |
| 1986 | return -EINVAL; |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | static int smiapp_set_crop(struct v4l2_subdev *subdev, |
| 1991 | struct v4l2_subdev_fh *fh, |
| 1992 | struct v4l2_subdev_selection *sel) |
| 1993 | { |
| 1994 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 1995 | struct smiapp_subdev *ssd = to_smiapp_subdev(subdev); |
| 1996 | struct v4l2_rect *src_size, *crops[SMIAPP_PADS]; |
| 1997 | struct v4l2_rect _r; |
| 1998 | |
| 1999 | smiapp_get_crop_compose(subdev, fh, crops, NULL, sel->which); |
| 2000 | |
| 2001 | if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { |
| 2002 | if (sel->pad == ssd->sink_pad) |
| 2003 | src_size = &ssd->sink_fmt; |
| 2004 | else |
| 2005 | src_size = &ssd->compose; |
| 2006 | } else { |
| 2007 | if (sel->pad == ssd->sink_pad) { |
| 2008 | _r.left = 0; |
| 2009 | _r.top = 0; |
| 2010 | _r.width = v4l2_subdev_get_try_format(fh, sel->pad) |
| 2011 | ->width; |
| 2012 | _r.height = v4l2_subdev_get_try_format(fh, sel->pad) |
| 2013 | ->height; |
| 2014 | src_size = &_r; |
| 2015 | } else { |
| 2016 | src_size = |
| 2017 | v4l2_subdev_get_try_compose( |
| 2018 | fh, ssd->sink_pad); |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) { |
| 2023 | sel->r.left = 0; |
| 2024 | sel->r.top = 0; |
| 2025 | } |
| 2026 | |
| 2027 | sel->r.width = min(sel->r.width, src_size->width); |
| 2028 | sel->r.height = min(sel->r.height, src_size->height); |
| 2029 | |
Ricardo Ribalda | f90580c | 2013-11-26 05:31:42 -0300 | [diff] [blame] | 2030 | sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width); |
| 2031 | sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2032 | |
| 2033 | *crops[sel->pad] = sel->r; |
| 2034 | |
| 2035 | if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK) |
| 2036 | smiapp_propagate(subdev, fh, sel->which, |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 2037 | V4L2_SEL_TGT_CROP); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2038 | |
| 2039 | return 0; |
| 2040 | } |
| 2041 | |
| 2042 | static int __smiapp_get_selection(struct v4l2_subdev *subdev, |
| 2043 | struct v4l2_subdev_fh *fh, |
| 2044 | struct v4l2_subdev_selection *sel) |
| 2045 | { |
| 2046 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2047 | struct smiapp_subdev *ssd = to_smiapp_subdev(subdev); |
| 2048 | struct v4l2_rect *comp, *crops[SMIAPP_PADS]; |
| 2049 | struct v4l2_rect sink_fmt; |
| 2050 | int ret; |
| 2051 | |
| 2052 | ret = __smiapp_sel_supported(subdev, sel); |
| 2053 | if (ret) |
| 2054 | return ret; |
| 2055 | |
| 2056 | smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which); |
| 2057 | |
| 2058 | if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { |
| 2059 | sink_fmt = ssd->sink_fmt; |
| 2060 | } else { |
| 2061 | struct v4l2_mbus_framefmt *fmt = |
| 2062 | v4l2_subdev_get_try_format(fh, ssd->sink_pad); |
| 2063 | |
| 2064 | sink_fmt.left = 0; |
| 2065 | sink_fmt.top = 0; |
| 2066 | sink_fmt.width = fmt->width; |
| 2067 | sink_fmt.height = fmt->height; |
| 2068 | } |
| 2069 | |
| 2070 | switch (sel->target) { |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 2071 | case V4L2_SEL_TGT_CROP_BOUNDS: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2072 | if (ssd == sensor->pixel_array) { |
| 2073 | sel->r.width = |
| 2074 | sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1; |
| 2075 | sel->r.height = |
| 2076 | sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1; |
| 2077 | } else if (sel->pad == ssd->sink_pad) { |
| 2078 | sel->r = sink_fmt; |
| 2079 | } else { |
| 2080 | sel->r = *comp; |
| 2081 | } |
| 2082 | break; |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 2083 | case V4L2_SEL_TGT_CROP: |
| 2084 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2085 | sel->r = *crops[sel->pad]; |
| 2086 | break; |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 2087 | case V4L2_SEL_TGT_COMPOSE: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2088 | sel->r = *comp; |
| 2089 | break; |
| 2090 | } |
| 2091 | |
| 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | static int smiapp_get_selection(struct v4l2_subdev *subdev, |
| 2096 | struct v4l2_subdev_fh *fh, |
| 2097 | struct v4l2_subdev_selection *sel) |
| 2098 | { |
| 2099 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2100 | int rval; |
| 2101 | |
| 2102 | mutex_lock(&sensor->mutex); |
| 2103 | rval = __smiapp_get_selection(subdev, fh, sel); |
| 2104 | mutex_unlock(&sensor->mutex); |
| 2105 | |
| 2106 | return rval; |
| 2107 | } |
| 2108 | static int smiapp_set_selection(struct v4l2_subdev *subdev, |
| 2109 | struct v4l2_subdev_fh *fh, |
| 2110 | struct v4l2_subdev_selection *sel) |
| 2111 | { |
| 2112 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2113 | int ret; |
| 2114 | |
| 2115 | ret = __smiapp_sel_supported(subdev, sel); |
| 2116 | if (ret) |
| 2117 | return ret; |
| 2118 | |
| 2119 | mutex_lock(&sensor->mutex); |
| 2120 | |
| 2121 | sel->r.left = max(0, sel->r.left & ~1); |
| 2122 | sel->r.top = max(0, sel->r.top & ~1); |
Ricardo Ribalda | f90580c | 2013-11-26 05:31:42 -0300 | [diff] [blame] | 2123 | sel->r.width = SMIAPP_ALIGN_DIM(sel->r.width, sel->flags); |
| 2124 | sel->r.height = SMIAPP_ALIGN_DIM(sel->r.height, sel->flags); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2125 | |
| 2126 | sel->r.width = max_t(unsigned int, |
| 2127 | sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE], |
| 2128 | sel->r.width); |
| 2129 | sel->r.height = max_t(unsigned int, |
| 2130 | sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE], |
| 2131 | sel->r.height); |
| 2132 | |
| 2133 | switch (sel->target) { |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 2134 | case V4L2_SEL_TGT_CROP: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2135 | ret = smiapp_set_crop(subdev, fh, sel); |
| 2136 | break; |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 2137 | case V4L2_SEL_TGT_COMPOSE: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2138 | ret = smiapp_set_compose(subdev, fh, sel); |
| 2139 | break; |
| 2140 | default: |
| 2141 | BUG(); |
| 2142 | } |
| 2143 | |
| 2144 | mutex_unlock(&sensor->mutex); |
| 2145 | return ret; |
| 2146 | } |
| 2147 | |
| 2148 | static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames) |
| 2149 | { |
| 2150 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2151 | |
| 2152 | *frames = sensor->frame_skip; |
| 2153 | return 0; |
| 2154 | } |
| 2155 | |
| 2156 | /* ----------------------------------------------------------------------------- |
| 2157 | * sysfs attributes |
| 2158 | */ |
| 2159 | |
| 2160 | static ssize_t |
| 2161 | smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr, |
| 2162 | char *buf) |
| 2163 | { |
| 2164 | struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev)); |
| 2165 | struct i2c_client *client = v4l2_get_subdevdata(subdev); |
| 2166 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2167 | unsigned int nbytes; |
| 2168 | |
| 2169 | if (!sensor->dev_init_done) |
| 2170 | return -EBUSY; |
| 2171 | |
| 2172 | if (!sensor->nvm_size) { |
| 2173 | /* NVM not read yet - read it now */ |
| 2174 | sensor->nvm_size = sensor->platform_data->nvm_size; |
| 2175 | if (smiapp_set_power(subdev, 1) < 0) |
| 2176 | return -ENODEV; |
| 2177 | if (smiapp_read_nvm(sensor, sensor->nvm)) { |
| 2178 | dev_err(&client->dev, "nvm read failed\n"); |
| 2179 | return -ENODEV; |
| 2180 | } |
| 2181 | smiapp_set_power(subdev, 0); |
| 2182 | } |
| 2183 | /* |
| 2184 | * NVM is still way below a PAGE_SIZE, so we can safely |
| 2185 | * assume this for now. |
| 2186 | */ |
| 2187 | nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE); |
| 2188 | memcpy(buf, sensor->nvm, nbytes); |
| 2189 | |
| 2190 | return nbytes; |
| 2191 | } |
| 2192 | static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL); |
| 2193 | |
Sakari Ailus | eba66b3 | 2012-09-15 18:26:54 -0300 | [diff] [blame] | 2194 | static ssize_t |
| 2195 | smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr, |
| 2196 | char *buf) |
| 2197 | { |
| 2198 | struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev)); |
| 2199 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2200 | struct smiapp_module_info *minfo = &sensor->minfo; |
| 2201 | |
| 2202 | return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n", |
| 2203 | minfo->manufacturer_id, minfo->model_id, |
| 2204 | minfo->revision_number_major) + 1; |
| 2205 | } |
| 2206 | |
| 2207 | static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL); |
| 2208 | |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2209 | /* ----------------------------------------------------------------------------- |
| 2210 | * V4L2 subdev core operations |
| 2211 | */ |
| 2212 | |
| 2213 | static int smiapp_identify_module(struct v4l2_subdev *subdev) |
| 2214 | { |
| 2215 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2216 | struct i2c_client *client = v4l2_get_subdevdata(subdev); |
| 2217 | struct smiapp_module_info *minfo = &sensor->minfo; |
| 2218 | unsigned int i; |
| 2219 | int rval = 0; |
| 2220 | |
| 2221 | minfo->name = SMIAPP_NAME; |
| 2222 | |
| 2223 | /* Module info */ |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2224 | rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID, |
| 2225 | &minfo->manufacturer_id); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2226 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2227 | rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID, |
| 2228 | &minfo->model_id); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2229 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2230 | rval = smiapp_read_8only(sensor, |
| 2231 | SMIAPP_REG_U8_REVISION_NUMBER_MAJOR, |
| 2232 | &minfo->revision_number_major); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2233 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2234 | rval = smiapp_read_8only(sensor, |
| 2235 | SMIAPP_REG_U8_REVISION_NUMBER_MINOR, |
| 2236 | &minfo->revision_number_minor); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2237 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2238 | rval = smiapp_read_8only(sensor, |
| 2239 | SMIAPP_REG_U8_MODULE_DATE_YEAR, |
| 2240 | &minfo->module_year); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2241 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2242 | rval = smiapp_read_8only(sensor, |
| 2243 | SMIAPP_REG_U8_MODULE_DATE_MONTH, |
| 2244 | &minfo->module_month); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2245 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2246 | rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY, |
| 2247 | &minfo->module_day); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2248 | |
| 2249 | /* Sensor info */ |
| 2250 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2251 | rval = smiapp_read_8only(sensor, |
| 2252 | SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID, |
| 2253 | &minfo->sensor_manufacturer_id); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2254 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2255 | rval = smiapp_read_8only(sensor, |
| 2256 | SMIAPP_REG_U16_SENSOR_MODEL_ID, |
| 2257 | &minfo->sensor_model_id); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2258 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2259 | rval = smiapp_read_8only(sensor, |
| 2260 | SMIAPP_REG_U8_SENSOR_REVISION_NUMBER, |
| 2261 | &minfo->sensor_revision_number); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2262 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2263 | rval = smiapp_read_8only(sensor, |
| 2264 | SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION, |
| 2265 | &minfo->sensor_firmware_version); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2266 | |
| 2267 | /* SMIA */ |
| 2268 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2269 | rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION, |
| 2270 | &minfo->smia_version); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2271 | if (!rval) |
Sakari Ailus | 98add8e | 2012-04-22 09:30:19 -0300 | [diff] [blame] | 2272 | rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION, |
| 2273 | &minfo->smiapp_version); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2274 | |
| 2275 | if (rval) { |
| 2276 | dev_err(&client->dev, "sensor detection failed\n"); |
| 2277 | return -ENODEV; |
| 2278 | } |
| 2279 | |
| 2280 | dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n", |
| 2281 | minfo->manufacturer_id, minfo->model_id); |
| 2282 | |
| 2283 | dev_dbg(&client->dev, |
| 2284 | "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n", |
| 2285 | minfo->revision_number_major, minfo->revision_number_minor, |
| 2286 | minfo->module_year, minfo->module_month, minfo->module_day); |
| 2287 | |
| 2288 | dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n", |
| 2289 | minfo->sensor_manufacturer_id, minfo->sensor_model_id); |
| 2290 | |
| 2291 | dev_dbg(&client->dev, |
| 2292 | "sensor revision 0x%2.2x firmware version 0x%2.2x\n", |
| 2293 | minfo->sensor_revision_number, minfo->sensor_firmware_version); |
| 2294 | |
| 2295 | dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n", |
| 2296 | minfo->smia_version, minfo->smiapp_version); |
| 2297 | |
| 2298 | /* |
| 2299 | * Some modules have bad data in the lvalues below. Hope the |
| 2300 | * rvalues have better stuff. The lvalues are module |
| 2301 | * parameters whereas the rvalues are sensor parameters. |
| 2302 | */ |
| 2303 | if (!minfo->manufacturer_id && !minfo->model_id) { |
| 2304 | minfo->manufacturer_id = minfo->sensor_manufacturer_id; |
| 2305 | minfo->model_id = minfo->sensor_model_id; |
| 2306 | minfo->revision_number_major = minfo->sensor_revision_number; |
| 2307 | } |
| 2308 | |
| 2309 | for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) { |
| 2310 | if (smiapp_module_idents[i].manufacturer_id |
| 2311 | != minfo->manufacturer_id) |
| 2312 | continue; |
| 2313 | if (smiapp_module_idents[i].model_id != minfo->model_id) |
| 2314 | continue; |
| 2315 | if (smiapp_module_idents[i].flags |
| 2316 | & SMIAPP_MODULE_IDENT_FLAG_REV_LE) { |
| 2317 | if (smiapp_module_idents[i].revision_number_major |
| 2318 | < minfo->revision_number_major) |
| 2319 | continue; |
| 2320 | } else { |
| 2321 | if (smiapp_module_idents[i].revision_number_major |
| 2322 | != minfo->revision_number_major) |
| 2323 | continue; |
| 2324 | } |
| 2325 | |
| 2326 | minfo->name = smiapp_module_idents[i].name; |
| 2327 | minfo->quirk = smiapp_module_idents[i].quirk; |
| 2328 | break; |
| 2329 | } |
| 2330 | |
| 2331 | if (i >= ARRAY_SIZE(smiapp_module_idents)) |
| 2332 | dev_warn(&client->dev, |
| 2333 | "no quirks for this module; let's hope it's fully compliant\n"); |
| 2334 | |
| 2335 | dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n", |
| 2336 | minfo->name, minfo->manufacturer_id, minfo->model_id, |
| 2337 | minfo->revision_number_major); |
| 2338 | |
| 2339 | strlcpy(subdev->name, sensor->minfo.name, sizeof(subdev->name)); |
| 2340 | |
| 2341 | return 0; |
| 2342 | } |
| 2343 | |
| 2344 | static const struct v4l2_subdev_ops smiapp_ops; |
| 2345 | static const struct v4l2_subdev_internal_ops smiapp_internal_ops; |
| 2346 | static const struct media_entity_operations smiapp_entity_ops; |
| 2347 | |
| 2348 | static int smiapp_registered(struct v4l2_subdev *subdev) |
| 2349 | { |
| 2350 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2351 | struct i2c_client *client = v4l2_get_subdevdata(subdev); |
Sakari Ailus | 1e9240b | 2012-10-20 10:17:02 -0300 | [diff] [blame] | 2352 | struct smiapp_pll *pll = &sensor->pll; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2353 | struct smiapp_subdev *last = NULL; |
| 2354 | u32 tmp; |
| 2355 | unsigned int i; |
| 2356 | int rval; |
| 2357 | |
Sachin Kamat | 31c1d17 | 2012-08-17 01:49:02 -0300 | [diff] [blame] | 2358 | sensor->vana = devm_regulator_get(&client->dev, "VANA"); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2359 | if (IS_ERR(sensor->vana)) { |
| 2360 | dev_err(&client->dev, "could not get regulator for vana\n"); |
| 2361 | return -ENODEV; |
| 2362 | } |
| 2363 | |
Sakari Ailus | 2547428 | 2012-04-22 08:24:33 -0300 | [diff] [blame] | 2364 | if (!sensor->platform_data->set_xclk) { |
Sakari Ailus | a354177 | 2013-08-10 14:49:48 -0300 | [diff] [blame] | 2365 | sensor->ext_clk = devm_clk_get(&client->dev, "ext_clk"); |
Sakari Ailus | 2547428 | 2012-04-22 08:24:33 -0300 | [diff] [blame] | 2366 | if (IS_ERR(sensor->ext_clk)) { |
Sakari Ailus | a354177 | 2013-08-10 14:49:48 -0300 | [diff] [blame] | 2367 | dev_err(&client->dev, "could not get clock\n"); |
Sachin Kamat | 31c1d17 | 2012-08-17 01:49:02 -0300 | [diff] [blame] | 2368 | return -ENODEV; |
Sakari Ailus | 2547428 | 2012-04-22 08:24:33 -0300 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | rval = clk_set_rate(sensor->ext_clk, |
| 2372 | sensor->platform_data->ext_clk); |
| 2373 | if (rval < 0) { |
| 2374 | dev_err(&client->dev, |
Sakari Ailus | a354177 | 2013-08-10 14:49:48 -0300 | [diff] [blame] | 2375 | "unable to set clock freq to %u\n", |
Sakari Ailus | 2547428 | 2012-04-22 08:24:33 -0300 | [diff] [blame] | 2376 | sensor->platform_data->ext_clk); |
Sachin Kamat | 31c1d17 | 2012-08-17 01:49:02 -0300 | [diff] [blame] | 2377 | return -ENODEV; |
Sakari Ailus | 2547428 | 2012-04-22 08:24:33 -0300 | [diff] [blame] | 2378 | } |
| 2379 | } |
| 2380 | |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2381 | if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) { |
Laurent Pinchart | b015ba2 | 2013-05-02 08:29:43 -0300 | [diff] [blame] | 2382 | if (devm_gpio_request_one(&client->dev, |
| 2383 | sensor->platform_data->xshutdown, 0, |
| 2384 | "SMIA++ xshutdown") != 0) { |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2385 | dev_err(&client->dev, |
| 2386 | "unable to acquire reset gpio %d\n", |
| 2387 | sensor->platform_data->xshutdown); |
Sachin Kamat | 31c1d17 | 2012-08-17 01:49:02 -0300 | [diff] [blame] | 2388 | return -ENODEV; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | rval = smiapp_power_on(sensor); |
Laurent Pinchart | b015ba2 | 2013-05-02 08:29:43 -0300 | [diff] [blame] | 2393 | if (rval) |
| 2394 | return -ENODEV; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2395 | |
| 2396 | rval = smiapp_identify_module(subdev); |
| 2397 | if (rval) { |
| 2398 | rval = -ENODEV; |
| 2399 | goto out_power_off; |
| 2400 | } |
| 2401 | |
| 2402 | rval = smiapp_get_all_limits(sensor); |
| 2403 | if (rval) { |
| 2404 | rval = -ENODEV; |
| 2405 | goto out_power_off; |
| 2406 | } |
| 2407 | |
| 2408 | /* |
| 2409 | * Handle Sensor Module orientation on the board. |
| 2410 | * |
| 2411 | * The application of H-FLIP and V-FLIP on the sensor is modified by |
| 2412 | * the sensor orientation on the board. |
| 2413 | * |
| 2414 | * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set |
| 2415 | * both H-FLIP and V-FLIP for normal operation which also implies |
| 2416 | * that a set/unset operation for user space HFLIP and VFLIP v4l2 |
| 2417 | * controls will need to be internally inverted. |
| 2418 | * |
| 2419 | * Rotation also changes the bayer pattern. |
| 2420 | */ |
| 2421 | if (sensor->platform_data->module_board_orient == |
| 2422 | SMIAPP_MODULE_BOARD_ORIENT_180) |
| 2423 | sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP | |
| 2424 | SMIAPP_IMAGE_ORIENTATION_VFLIP; |
| 2425 | |
| 2426 | rval = smiapp_get_mbus_formats(sensor); |
| 2427 | if (rval) { |
| 2428 | rval = -ENODEV; |
| 2429 | goto out_power_off; |
| 2430 | } |
| 2431 | |
| 2432 | if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) { |
| 2433 | u32 val; |
| 2434 | |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 2435 | rval = smiapp_read(sensor, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2436 | SMIAPP_REG_U8_BINNING_SUBTYPES, &val); |
| 2437 | if (rval < 0) { |
| 2438 | rval = -ENODEV; |
| 2439 | goto out_power_off; |
| 2440 | } |
| 2441 | sensor->nbinning_subtypes = min_t(u8, val, |
| 2442 | SMIAPP_BINNING_SUBTYPES); |
| 2443 | |
| 2444 | for (i = 0; i < sensor->nbinning_subtypes; i++) { |
| 2445 | rval = smiapp_read( |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 2446 | sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2447 | if (rval < 0) { |
| 2448 | rval = -ENODEV; |
| 2449 | goto out_power_off; |
| 2450 | } |
| 2451 | sensor->binning_subtypes[i] = |
| 2452 | *(struct smiapp_binning_subtype *)&val; |
| 2453 | |
| 2454 | dev_dbg(&client->dev, "binning %xx%x\n", |
| 2455 | sensor->binning_subtypes[i].horizontal, |
| 2456 | sensor->binning_subtypes[i].vertical); |
| 2457 | } |
| 2458 | } |
| 2459 | sensor->binning_horizontal = 1; |
| 2460 | sensor->binning_vertical = 1; |
| 2461 | |
Sakari Ailus | eba66b3 | 2012-09-15 18:26:54 -0300 | [diff] [blame] | 2462 | if (device_create_file(&client->dev, &dev_attr_ident) != 0) { |
| 2463 | dev_err(&client->dev, "sysfs ident entry creation failed\n"); |
| 2464 | rval = -ENOENT; |
| 2465 | goto out_power_off; |
| 2466 | } |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2467 | /* SMIA++ NVM initialization - it will be read from the sensor |
| 2468 | * when it is first requested by userspace. |
| 2469 | */ |
| 2470 | if (sensor->minfo.smiapp_version && sensor->platform_data->nvm_size) { |
Sachin Kamat | 31c1d17 | 2012-08-17 01:49:02 -0300 | [diff] [blame] | 2471 | sensor->nvm = devm_kzalloc(&client->dev, |
| 2472 | sensor->platform_data->nvm_size, GFP_KERNEL); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2473 | if (sensor->nvm == NULL) { |
| 2474 | dev_err(&client->dev, "nvm buf allocation failed\n"); |
| 2475 | rval = -ENOMEM; |
Sakari Ailus | eba66b3 | 2012-09-15 18:26:54 -0300 | [diff] [blame] | 2476 | goto out_ident_release; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | if (device_create_file(&client->dev, &dev_attr_nvm) != 0) { |
| 2480 | dev_err(&client->dev, "sysfs nvm entry failed\n"); |
| 2481 | rval = -EBUSY; |
Sakari Ailus | eba66b3 | 2012-09-15 18:26:54 -0300 | [diff] [blame] | 2482 | goto out_ident_release; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | rval = smiapp_call_quirk(sensor, limits); |
| 2487 | if (rval) { |
| 2488 | dev_err(&client->dev, "limits quirks failed\n"); |
| 2489 | goto out_nvm_release; |
| 2490 | } |
| 2491 | |
| 2492 | /* We consider this as profile 0 sensor if any of these are zero. */ |
| 2493 | if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] || |
| 2494 | !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] || |
| 2495 | !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] || |
| 2496 | !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) { |
| 2497 | sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0; |
| 2498 | } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY] |
| 2499 | != SMIAPP_SCALING_CAPABILITY_NONE) { |
| 2500 | if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY] |
| 2501 | == SMIAPP_SCALING_CAPABILITY_HORIZONTAL) |
| 2502 | sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1; |
| 2503 | else |
| 2504 | sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2; |
| 2505 | sensor->scaler = &sensor->ssds[sensor->ssds_used]; |
| 2506 | sensor->ssds_used++; |
| 2507 | } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY] |
| 2508 | == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) { |
| 2509 | sensor->scaler = &sensor->ssds[sensor->ssds_used]; |
| 2510 | sensor->ssds_used++; |
| 2511 | } |
| 2512 | sensor->binner = &sensor->ssds[sensor->ssds_used]; |
| 2513 | sensor->ssds_used++; |
| 2514 | sensor->pixel_array = &sensor->ssds[sensor->ssds_used]; |
| 2515 | sensor->ssds_used++; |
| 2516 | |
| 2517 | sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]; |
| 2518 | |
| 2519 | for (i = 0; i < SMIAPP_SUBDEVS; i++) { |
| 2520 | struct { |
| 2521 | struct smiapp_subdev *ssd; |
| 2522 | char *name; |
| 2523 | } const __this[] = { |
| 2524 | { sensor->scaler, "scaler", }, |
| 2525 | { sensor->binner, "binner", }, |
| 2526 | { sensor->pixel_array, "pixel array", }, |
| 2527 | }, *_this = &__this[i]; |
| 2528 | struct smiapp_subdev *this = _this->ssd; |
| 2529 | |
| 2530 | if (!this) |
| 2531 | continue; |
| 2532 | |
| 2533 | if (this != sensor->src) |
| 2534 | v4l2_subdev_init(&this->sd, &smiapp_ops); |
| 2535 | |
| 2536 | this->sensor = sensor; |
| 2537 | |
| 2538 | if (this == sensor->pixel_array) { |
| 2539 | this->npads = 1; |
| 2540 | } else { |
| 2541 | this->npads = 2; |
| 2542 | this->source_pad = 1; |
| 2543 | } |
| 2544 | |
| 2545 | snprintf(this->sd.name, |
Sakari Ailus | 3c9e7ea | 2014-03-26 13:34:29 -0300 | [diff] [blame] | 2546 | sizeof(this->sd.name), "%s %d-%4.4x %s", |
| 2547 | sensor->minfo.name, i2c_adapter_id(client->adapter), |
| 2548 | client->addr, _this->name); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2549 | |
| 2550 | this->sink_fmt.width = |
| 2551 | sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1; |
| 2552 | this->sink_fmt.height = |
| 2553 | sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1; |
| 2554 | this->compose.width = this->sink_fmt.width; |
| 2555 | this->compose.height = this->sink_fmt.height; |
| 2556 | this->crop[this->source_pad] = this->compose; |
| 2557 | this->pads[this->source_pad].flags = MEDIA_PAD_FL_SOURCE; |
| 2558 | if (this != sensor->pixel_array) { |
| 2559 | this->crop[this->sink_pad] = this->compose; |
| 2560 | this->pads[this->sink_pad].flags = MEDIA_PAD_FL_SINK; |
| 2561 | } |
| 2562 | |
| 2563 | this->sd.entity.ops = &smiapp_entity_ops; |
| 2564 | |
| 2565 | if (last == NULL) { |
| 2566 | last = this; |
| 2567 | continue; |
| 2568 | } |
| 2569 | |
| 2570 | this->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 2571 | this->sd.internal_ops = &smiapp_internal_ops; |
| 2572 | this->sd.owner = NULL; |
| 2573 | v4l2_set_subdevdata(&this->sd, client); |
| 2574 | |
| 2575 | rval = media_entity_init(&this->sd.entity, |
| 2576 | this->npads, this->pads, 0); |
| 2577 | if (rval) { |
| 2578 | dev_err(&client->dev, |
| 2579 | "media_entity_init failed\n"); |
| 2580 | goto out_nvm_release; |
| 2581 | } |
| 2582 | |
| 2583 | rval = media_entity_create_link(&this->sd.entity, |
| 2584 | this->source_pad, |
| 2585 | &last->sd.entity, |
| 2586 | last->sink_pad, |
| 2587 | MEDIA_LNK_FL_ENABLED | |
| 2588 | MEDIA_LNK_FL_IMMUTABLE); |
| 2589 | if (rval) { |
| 2590 | dev_err(&client->dev, |
| 2591 | "media_entity_create_link failed\n"); |
| 2592 | goto out_nvm_release; |
| 2593 | } |
| 2594 | |
| 2595 | rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev, |
| 2596 | &this->sd); |
| 2597 | if (rval) { |
| 2598 | dev_err(&client->dev, |
| 2599 | "v4l2_device_register_subdev failed\n"); |
| 2600 | goto out_nvm_release; |
| 2601 | } |
| 2602 | |
| 2603 | last = this; |
| 2604 | } |
| 2605 | |
| 2606 | dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile); |
| 2607 | |
| 2608 | sensor->pixel_array->sd.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR; |
| 2609 | |
| 2610 | /* final steps */ |
| 2611 | smiapp_read_frame_fmt(sensor); |
| 2612 | rval = smiapp_init_controls(sensor); |
| 2613 | if (rval < 0) |
| 2614 | goto out_nvm_release; |
| 2615 | |
Sakari Ailus | 1e9240b | 2012-10-20 10:17:02 -0300 | [diff] [blame] | 2616 | /* prepare PLL configuration input values */ |
Sakari Ailus | f5984bb | 2012-10-20 10:35:25 -0300 | [diff] [blame] | 2617 | pll->bus_type = SMIAPP_PLL_BUS_TYPE_CSI2; |
| 2618 | pll->csi2.lanes = sensor->platform_data->lanes; |
Sakari Ailus | 1e9240b | 2012-10-20 10:17:02 -0300 | [diff] [blame] | 2619 | pll->ext_clk_freq_hz = sensor->platform_data->ext_clk; |
Sakari Ailus | caf447b | 2014-02-12 10:33:51 -0300 | [diff] [blame] | 2620 | if (sensor->minfo.quirk) |
| 2621 | pll->flags = sensor->minfo.quirk->pll_flags; |
Sakari Ailus | 1e9240b | 2012-10-20 10:17:02 -0300 | [diff] [blame] | 2622 | /* Profile 0 sensors have no separate OP clock branch. */ |
| 2623 | if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0) |
| 2624 | pll->flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS; |
Sakari Ailus | 1e9240b | 2012-10-20 10:17:02 -0300 | [diff] [blame] | 2625 | pll->scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]; |
| 2626 | |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2627 | rval = smiapp_update_mode(sensor); |
| 2628 | if (rval) { |
| 2629 | dev_err(&client->dev, "update mode failed\n"); |
| 2630 | goto out_nvm_release; |
| 2631 | } |
| 2632 | |
| 2633 | sensor->streaming = false; |
| 2634 | sensor->dev_init_done = true; |
| 2635 | |
| 2636 | /* check flash capability */ |
Sakari Ailus | 1e73eea | 2012-04-22 08:55:10 -0300 | [diff] [blame] | 2637 | rval = smiapp_read(sensor, SMIAPP_REG_U8_FLASH_MODE_CAPABILITY, &tmp); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2638 | sensor->flash_capability = tmp; |
| 2639 | if (rval) |
| 2640 | goto out_nvm_release; |
| 2641 | |
| 2642 | smiapp_power_off(sensor); |
| 2643 | |
| 2644 | return 0; |
| 2645 | |
| 2646 | out_nvm_release: |
| 2647 | device_remove_file(&client->dev, &dev_attr_nvm); |
| 2648 | |
Sakari Ailus | eba66b3 | 2012-09-15 18:26:54 -0300 | [diff] [blame] | 2649 | out_ident_release: |
| 2650 | device_remove_file(&client->dev, &dev_attr_ident); |
| 2651 | |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2652 | out_power_off: |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2653 | smiapp_power_off(sensor); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2654 | return rval; |
| 2655 | } |
| 2656 | |
| 2657 | static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) |
| 2658 | { |
| 2659 | struct smiapp_subdev *ssd = to_smiapp_subdev(sd); |
| 2660 | struct smiapp_sensor *sensor = ssd->sensor; |
| 2661 | u32 mbus_code = |
| 2662 | smiapp_csi_data_formats[smiapp_pixel_order(sensor)].code; |
| 2663 | unsigned int i; |
| 2664 | |
| 2665 | mutex_lock(&sensor->mutex); |
| 2666 | |
| 2667 | for (i = 0; i < ssd->npads; i++) { |
| 2668 | struct v4l2_mbus_framefmt *try_fmt = |
| 2669 | v4l2_subdev_get_try_format(fh, i); |
| 2670 | struct v4l2_rect *try_crop = v4l2_subdev_get_try_crop(fh, i); |
| 2671 | struct v4l2_rect *try_comp; |
| 2672 | |
| 2673 | try_fmt->width = sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1; |
| 2674 | try_fmt->height = sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1; |
| 2675 | try_fmt->code = mbus_code; |
| 2676 | |
| 2677 | try_crop->top = 0; |
| 2678 | try_crop->left = 0; |
| 2679 | try_crop->width = try_fmt->width; |
| 2680 | try_crop->height = try_fmt->height; |
| 2681 | |
| 2682 | if (ssd != sensor->pixel_array) |
| 2683 | continue; |
| 2684 | |
| 2685 | try_comp = v4l2_subdev_get_try_compose(fh, i); |
| 2686 | *try_comp = *try_crop; |
| 2687 | } |
| 2688 | |
| 2689 | mutex_unlock(&sensor->mutex); |
| 2690 | |
| 2691 | return smiapp_set_power(sd, 1); |
| 2692 | } |
| 2693 | |
| 2694 | static int smiapp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) |
| 2695 | { |
| 2696 | return smiapp_set_power(sd, 0); |
| 2697 | } |
| 2698 | |
| 2699 | static const struct v4l2_subdev_video_ops smiapp_video_ops = { |
| 2700 | .s_stream = smiapp_set_stream, |
| 2701 | }; |
| 2702 | |
| 2703 | static const struct v4l2_subdev_core_ops smiapp_core_ops = { |
| 2704 | .s_power = smiapp_set_power, |
| 2705 | }; |
| 2706 | |
| 2707 | static const struct v4l2_subdev_pad_ops smiapp_pad_ops = { |
| 2708 | .enum_mbus_code = smiapp_enum_mbus_code, |
| 2709 | .get_fmt = smiapp_get_format, |
| 2710 | .set_fmt = smiapp_set_format, |
| 2711 | .get_selection = smiapp_get_selection, |
| 2712 | .set_selection = smiapp_set_selection, |
| 2713 | }; |
| 2714 | |
| 2715 | static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = { |
| 2716 | .g_skip_frames = smiapp_get_skip_frames, |
| 2717 | }; |
| 2718 | |
| 2719 | static const struct v4l2_subdev_ops smiapp_ops = { |
| 2720 | .core = &smiapp_core_ops, |
| 2721 | .video = &smiapp_video_ops, |
| 2722 | .pad = &smiapp_pad_ops, |
| 2723 | .sensor = &smiapp_sensor_ops, |
| 2724 | }; |
| 2725 | |
| 2726 | static const struct media_entity_operations smiapp_entity_ops = { |
| 2727 | .link_validate = v4l2_subdev_link_validate, |
| 2728 | }; |
| 2729 | |
| 2730 | static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = { |
| 2731 | .registered = smiapp_registered, |
| 2732 | .open = smiapp_open, |
| 2733 | .close = smiapp_close, |
| 2734 | }; |
| 2735 | |
| 2736 | static const struct v4l2_subdev_internal_ops smiapp_internal_ops = { |
| 2737 | .open = smiapp_open, |
| 2738 | .close = smiapp_close, |
| 2739 | }; |
| 2740 | |
| 2741 | /* ----------------------------------------------------------------------------- |
| 2742 | * I2C Driver |
| 2743 | */ |
| 2744 | |
| 2745 | #ifdef CONFIG_PM |
| 2746 | |
| 2747 | static int smiapp_suspend(struct device *dev) |
| 2748 | { |
| 2749 | struct i2c_client *client = to_i2c_client(dev); |
| 2750 | struct v4l2_subdev *subdev = i2c_get_clientdata(client); |
| 2751 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2752 | bool streaming; |
| 2753 | |
| 2754 | BUG_ON(mutex_is_locked(&sensor->mutex)); |
| 2755 | |
| 2756 | if (sensor->power_count == 0) |
| 2757 | return 0; |
| 2758 | |
| 2759 | if (sensor->streaming) |
| 2760 | smiapp_stop_streaming(sensor); |
| 2761 | |
| 2762 | streaming = sensor->streaming; |
| 2763 | |
| 2764 | smiapp_power_off(sensor); |
| 2765 | |
| 2766 | /* save state for resume */ |
| 2767 | sensor->streaming = streaming; |
| 2768 | |
| 2769 | return 0; |
| 2770 | } |
| 2771 | |
| 2772 | static int smiapp_resume(struct device *dev) |
| 2773 | { |
| 2774 | struct i2c_client *client = to_i2c_client(dev); |
| 2775 | struct v4l2_subdev *subdev = i2c_get_clientdata(client); |
| 2776 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2777 | int rval; |
| 2778 | |
| 2779 | if (sensor->power_count == 0) |
| 2780 | return 0; |
| 2781 | |
| 2782 | rval = smiapp_power_on(sensor); |
| 2783 | if (rval) |
| 2784 | return rval; |
| 2785 | |
| 2786 | if (sensor->streaming) |
| 2787 | rval = smiapp_start_streaming(sensor); |
| 2788 | |
| 2789 | return rval; |
| 2790 | } |
| 2791 | |
| 2792 | #else |
| 2793 | |
| 2794 | #define smiapp_suspend NULL |
| 2795 | #define smiapp_resume NULL |
| 2796 | |
| 2797 | #endif /* CONFIG_PM */ |
| 2798 | |
| 2799 | static int smiapp_probe(struct i2c_client *client, |
| 2800 | const struct i2c_device_id *devid) |
| 2801 | { |
| 2802 | struct smiapp_sensor *sensor; |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2803 | |
| 2804 | if (client->dev.platform_data == NULL) |
| 2805 | return -ENODEV; |
| 2806 | |
Sachin Kamat | 31c1d17 | 2012-08-17 01:49:02 -0300 | [diff] [blame] | 2807 | sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2808 | if (sensor == NULL) |
| 2809 | return -ENOMEM; |
| 2810 | |
| 2811 | sensor->platform_data = client->dev.platform_data; |
| 2812 | mutex_init(&sensor->mutex); |
| 2813 | mutex_init(&sensor->power_mutex); |
| 2814 | sensor->src = &sensor->ssds[sensor->ssds_used]; |
| 2815 | |
| 2816 | v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops); |
| 2817 | sensor->src->sd.internal_ops = &smiapp_internal_src_ops; |
| 2818 | sensor->src->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 2819 | sensor->src->sensor = sensor; |
| 2820 | |
| 2821 | sensor->src->pads[0].flags = MEDIA_PAD_FL_SOURCE; |
Sachin Kamat | 31c1d17 | 2012-08-17 01:49:02 -0300 | [diff] [blame] | 2822 | return media_entity_init(&sensor->src->sd.entity, 2, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2823 | sensor->src->pads, 0); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2824 | } |
| 2825 | |
Dmitry Torokhov | bf30690 | 2013-02-26 03:17:27 -0300 | [diff] [blame] | 2826 | static int smiapp_remove(struct i2c_client *client) |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2827 | { |
| 2828 | struct v4l2_subdev *subdev = i2c_get_clientdata(client); |
| 2829 | struct smiapp_sensor *sensor = to_smiapp_sensor(subdev); |
| 2830 | unsigned int i; |
| 2831 | |
| 2832 | if (sensor->power_count) { |
| 2833 | if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) |
| 2834 | gpio_set_value(sensor->platform_data->xshutdown, 0); |
Sakari Ailus | 2547428 | 2012-04-22 08:24:33 -0300 | [diff] [blame] | 2835 | if (sensor->platform_data->set_xclk) |
| 2836 | sensor->platform_data->set_xclk(&sensor->src->sd, 0); |
| 2837 | else |
Sakari Ailus | d0aae00 | 2013-08-10 14:49:47 -0300 | [diff] [blame] | 2838 | clk_disable_unprepare(sensor->ext_clk); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2839 | sensor->power_count = 0; |
| 2840 | } |
| 2841 | |
Sakari Ailus | eba66b3 | 2012-09-15 18:26:54 -0300 | [diff] [blame] | 2842 | device_remove_file(&client->dev, &dev_attr_ident); |
Sachin Kamat | 31c1d17 | 2012-08-17 01:49:02 -0300 | [diff] [blame] | 2843 | if (sensor->nvm) |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2844 | device_remove_file(&client->dev, &dev_attr_nvm); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2845 | |
| 2846 | for (i = 0; i < sensor->ssds_used; i++) { |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2847 | v4l2_device_unregister_subdev(&sensor->ssds[i].sd); |
Sakari Ailus | 2a3e725 | 2013-06-11 06:50:48 -0300 | [diff] [blame] | 2848 | media_entity_cleanup(&sensor->ssds[i].sd.entity); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2849 | } |
| 2850 | smiapp_free_controls(sensor); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2851 | |
| 2852 | return 0; |
| 2853 | } |
| 2854 | |
| 2855 | static const struct i2c_device_id smiapp_id_table[] = { |
| 2856 | { SMIAPP_NAME, 0 }, |
| 2857 | { }, |
| 2858 | }; |
| 2859 | MODULE_DEVICE_TABLE(i2c, smiapp_id_table); |
| 2860 | |
| 2861 | static const struct dev_pm_ops smiapp_pm_ops = { |
| 2862 | .suspend = smiapp_suspend, |
| 2863 | .resume = smiapp_resume, |
| 2864 | }; |
| 2865 | |
| 2866 | static struct i2c_driver smiapp_i2c_driver = { |
| 2867 | .driver = { |
| 2868 | .name = SMIAPP_NAME, |
| 2869 | .pm = &smiapp_pm_ops, |
| 2870 | }, |
| 2871 | .probe = smiapp_probe, |
Dmitry Torokhov | bf30690 | 2013-02-26 03:17:27 -0300 | [diff] [blame] | 2872 | .remove = smiapp_remove, |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2873 | .id_table = smiapp_id_table, |
| 2874 | }; |
| 2875 | |
| 2876 | module_i2c_driver(smiapp_i2c_driver); |
| 2877 | |
Sakari Ailus | 8c5dff9 | 2012-10-28 06:44:17 -0300 | [diff] [blame] | 2878 | MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>"); |
Sakari Ailus | ccfc97b | 2012-03-03 17:19:52 -0300 | [diff] [blame] | 2879 | MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver"); |
| 2880 | MODULE_LICENSE("GPL"); |