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