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