2 * drivers/media/i2c/smiapp/smiapp-core.c
4 * Generic driver for SMIA/SMIA++ compliant camera modules
6 * Copyright (C) 2010--2012 Nokia Corporation
7 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
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>
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.
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.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29 #include <linux/clk.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/gpio.h>
33 #include <linux/module.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/slab.h>
36 #include <linux/smiapp.h>
37 #include <linux/v4l2-mediabus.h>
38 #include <media/v4l2-device.h>
42 #define SMIAPP_ALIGN_DIM(dim, flags) \
43 ((flags) & V4L2_SEL_FLAG_GE \
48 * smiapp_module_idents - supported camera modules
50 static const struct smiapp_module_ident smiapp_module_idents
[] = {
51 SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"),
52 SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"),
53 SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
54 SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
55 SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
56 SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk
),
57 SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
58 SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
59 SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk
),
60 SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk
),
61 SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk
),
66 * Dynamic Capability Identification
70 static int smiapp_read_frame_fmt(struct smiapp_sensor
*sensor
)
72 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
73 u32 fmt_model_type
, fmt_model_subtype
, ncol_desc
, nrow_desc
;
77 int embedded_start
= -1, embedded_end
= -1;
80 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE
,
85 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE
,
90 ncol_desc
= (fmt_model_subtype
91 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK
)
92 >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT
;
93 nrow_desc
= fmt_model_subtype
94 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK
;
96 dev_dbg(&client
->dev
, "format_model_type %s\n",
97 fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
99 fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
100 ? "4 byte" : "is simply bad");
102 for (i
= 0; i
< ncol_desc
+ nrow_desc
; i
++) {
109 if (fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
) {
112 SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i
),
119 & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK
)
120 >> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT
;
121 pixels
= desc
& SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK
;
122 } else if (fmt_model_type
123 == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
) {
126 SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i
),
133 & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK
)
134 >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT
;
135 pixels
= desc
& SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK
;
137 dev_dbg(&client
->dev
,
138 "invalid frame format model type %d\n",
149 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED
:
152 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY
:
155 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK
:
158 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK
:
161 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
:
166 dev_dbg(&client
->dev
, "pixelcode %d\n", pixelcode
);
170 dev_dbg(&client
->dev
, "%s pixels: %d %s\n",
171 what
, pixels
, which
);
176 /* Handle row descriptors */
178 == SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED
) {
179 embedded_start
= line_count
;
181 if (pixelcode
== SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
182 || pixels
>= sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES
] / 2)
183 image_start
= line_count
;
184 if (embedded_start
!= -1 && embedded_end
== -1)
185 embedded_end
= line_count
;
187 line_count
+= pixels
;
190 if (embedded_start
== -1 || embedded_end
== -1) {
195 dev_dbg(&client
->dev
, "embedded data from lines %d to %d\n",
196 embedded_start
, embedded_end
);
197 dev_dbg(&client
->dev
, "image data starts at line %d\n", image_start
);
202 static int smiapp_pll_configure(struct smiapp_sensor
*sensor
)
204 struct smiapp_pll
*pll
= &sensor
->pll
;
208 sensor
, SMIAPP_REG_U16_VT_PIX_CLK_DIV
, pll
->vt
.pix_clk_div
);
213 sensor
, SMIAPP_REG_U16_VT_SYS_CLK_DIV
, pll
->vt
.sys_clk_div
);
218 sensor
, SMIAPP_REG_U16_PRE_PLL_CLK_DIV
, pll
->pre_pll_clk_div
);
223 sensor
, SMIAPP_REG_U16_PLL_MULTIPLIER
, pll
->pll_multiplier
);
227 /* Lane op clock ratio does not apply here. */
229 sensor
, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS
,
230 DIV_ROUND_UP(pll
->op
.sys_clk_freq_hz
, 1000000 / 256 / 256));
231 if (rval
< 0 || sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
235 sensor
, SMIAPP_REG_U16_OP_PIX_CLK_DIV
, pll
->op
.pix_clk_div
);
240 sensor
, SMIAPP_REG_U16_OP_SYS_CLK_DIV
, pll
->op
.sys_clk_div
);
243 static int smiapp_pll_try(struct smiapp_sensor
*sensor
,
244 struct smiapp_pll
*pll
)
246 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
247 struct smiapp_pll_limits lim
= {
248 .min_pre_pll_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV
],
249 .max_pre_pll_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV
],
250 .min_pll_ip_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ
],
251 .max_pll_ip_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ
],
252 .min_pll_multiplier
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER
],
253 .max_pll_multiplier
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER
],
254 .min_pll_op_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ
],
255 .max_pll_op_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ
],
257 .op
.min_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV
],
258 .op
.max_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV
],
259 .op
.min_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV
],
260 .op
.max_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV
],
261 .op
.min_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ
],
262 .op
.max_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ
],
263 .op
.min_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ
],
264 .op
.max_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ
],
266 .vt
.min_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV
],
267 .vt
.max_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV
],
268 .vt
.min_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV
],
269 .vt
.max_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV
],
270 .vt
.min_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ
],
271 .vt
.max_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ
],
272 .vt
.min_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ
],
273 .vt
.max_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ
],
275 .min_line_length_pck_bin
= sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
],
276 .min_line_length_pck
= sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK
],
279 return smiapp_pll_calculate(&client
->dev
, &lim
, pll
);
282 static int smiapp_pll_update(struct smiapp_sensor
*sensor
)
284 struct smiapp_pll
*pll
= &sensor
->pll
;
287 pll
->binning_horizontal
= sensor
->binning_horizontal
;
288 pll
->binning_vertical
= sensor
->binning_vertical
;
290 sensor
->link_freq
->qmenu_int
[sensor
->link_freq
->val
];
291 pll
->scale_m
= sensor
->scale_m
;
292 pll
->bits_per_pixel
= sensor
->csi_format
->compressed
;
294 rval
= smiapp_pll_try(sensor
, pll
);
298 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_parray
,
299 pll
->pixel_rate_pixel_array
);
300 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_csi
, pll
->pixel_rate_csi
);
308 * V4L2 Controls handling
312 static void __smiapp_update_exposure_limits(struct smiapp_sensor
*sensor
)
314 struct v4l2_ctrl
*ctrl
= sensor
->exposure
;
317 max
= sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
318 + sensor
->vblank
->val
319 - sensor
->limits
[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN
];
321 __v4l2_ctrl_modify_range(ctrl
, ctrl
->minimum
, max
, ctrl
->step
, max
);
327 * 1. Bits-per-pixel, descending.
328 * 2. Bits-per-pixel compressed, descending.
329 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
330 * orders must be defined.
332 static const struct smiapp_csi_data_format smiapp_csi_data_formats
[] = {
333 { MEDIA_BUS_FMT_SGRBG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GRBG
, },
334 { MEDIA_BUS_FMT_SRGGB12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_RGGB
, },
335 { MEDIA_BUS_FMT_SBGGR12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_BGGR
, },
336 { MEDIA_BUS_FMT_SGBRG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GBRG
, },
337 { MEDIA_BUS_FMT_SGRBG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GRBG
, },
338 { MEDIA_BUS_FMT_SRGGB10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_RGGB
, },
339 { MEDIA_BUS_FMT_SBGGR10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_BGGR
, },
340 { MEDIA_BUS_FMT_SGBRG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GBRG
, },
341 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
342 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
343 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
344 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
345 { MEDIA_BUS_FMT_SGRBG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
346 { MEDIA_BUS_FMT_SRGGB8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
347 { MEDIA_BUS_FMT_SBGGR8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
348 { MEDIA_BUS_FMT_SGBRG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
351 const char *pixel_order_str
[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
353 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
354 - (unsigned long)smiapp_csi_data_formats) \
355 / sizeof(*smiapp_csi_data_formats))
357 static u32
smiapp_pixel_order(struct smiapp_sensor
*sensor
)
359 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
363 if (sensor
->hflip
->val
)
364 flip
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
366 if (sensor
->vflip
->val
)
367 flip
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
370 flip
^= sensor
->hvflip_inv_mask
;
372 dev_dbg(&client
->dev
, "flip %d\n", flip
);
373 return sensor
->default_pixel_order
^ flip
;
376 static void smiapp_update_mbus_formats(struct smiapp_sensor
*sensor
)
378 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
379 unsigned int csi_format_idx
=
380 to_csi_format_idx(sensor
->csi_format
) & ~3;
381 unsigned int internal_csi_format_idx
=
382 to_csi_format_idx(sensor
->internal_csi_format
) & ~3;
383 unsigned int pixel_order
= smiapp_pixel_order(sensor
);
385 sensor
->mbus_frame_fmts
=
386 sensor
->default_mbus_frame_fmts
<< pixel_order
;
388 &smiapp_csi_data_formats
[csi_format_idx
+ pixel_order
];
389 sensor
->internal_csi_format
=
390 &smiapp_csi_data_formats
[internal_csi_format_idx
393 BUG_ON(max(internal_csi_format_idx
, csi_format_idx
) + pixel_order
394 >= ARRAY_SIZE(smiapp_csi_data_formats
));
396 dev_dbg(&client
->dev
, "new pixel order %s\n",
397 pixel_order_str
[pixel_order
]);
400 static const char * const smiapp_test_patterns
[] = {
403 "Eight Vertical Colour Bars",
404 "Colour Bars With Fade to Grey",
405 "Pseudorandom Sequence (PN9)",
408 static int smiapp_set_ctrl(struct v4l2_ctrl
*ctrl
)
410 struct smiapp_sensor
*sensor
=
411 container_of(ctrl
->handler
, struct smiapp_subdev
, ctrl_handler
)
418 case V4L2_CID_ANALOGUE_GAIN
:
421 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL
, ctrl
->val
);
423 case V4L2_CID_EXPOSURE
:
426 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME
, ctrl
->val
);
430 if (sensor
->streaming
)
433 if (sensor
->hflip
->val
)
434 orient
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
436 if (sensor
->vflip
->val
)
437 orient
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
439 orient
^= sensor
->hvflip_inv_mask
;
440 rval
= smiapp_write(sensor
,
441 SMIAPP_REG_U8_IMAGE_ORIENTATION
,
446 smiapp_update_mbus_formats(sensor
);
450 case V4L2_CID_VBLANK
:
451 exposure
= sensor
->exposure
->val
;
453 __smiapp_update_exposure_limits(sensor
);
455 if (exposure
> sensor
->exposure
->maximum
) {
456 sensor
->exposure
->val
=
457 sensor
->exposure
->maximum
;
458 rval
= smiapp_set_ctrl(
465 sensor
, SMIAPP_REG_U16_FRAME_LENGTH_LINES
,
466 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
469 case V4L2_CID_HBLANK
:
471 sensor
, SMIAPP_REG_U16_LINE_LENGTH_PCK
,
472 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
475 case V4L2_CID_LINK_FREQ
:
476 if (sensor
->streaming
)
479 return smiapp_pll_update(sensor
);
481 case V4L2_CID_TEST_PATTERN
: {
484 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
486 sensor
->test_data
[i
],
488 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR
);
491 sensor
, SMIAPP_REG_U16_TEST_PATTERN_MODE
, ctrl
->val
);
494 case V4L2_CID_TEST_PATTERN_RED
:
496 sensor
, SMIAPP_REG_U16_TEST_DATA_RED
, ctrl
->val
);
498 case V4L2_CID_TEST_PATTERN_GREENR
:
500 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENR
, ctrl
->val
);
502 case V4L2_CID_TEST_PATTERN_BLUE
:
504 sensor
, SMIAPP_REG_U16_TEST_DATA_BLUE
, ctrl
->val
);
506 case V4L2_CID_TEST_PATTERN_GREENB
:
508 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENB
, ctrl
->val
);
510 case V4L2_CID_PIXEL_RATE
:
511 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
519 static const struct v4l2_ctrl_ops smiapp_ctrl_ops
= {
520 .s_ctrl
= smiapp_set_ctrl
,
523 static int smiapp_init_controls(struct smiapp_sensor
*sensor
)
525 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
526 unsigned long *valid_link_freqs
= &sensor
->valid_link_freqs
[
527 sensor
->csi_format
->compressed
- SMIAPP_COMPRESSED_BASE
];
531 rval
= v4l2_ctrl_handler_init(&sensor
->pixel_array
->ctrl_handler
, 12);
534 sensor
->pixel_array
->ctrl_handler
.lock
= &sensor
->mutex
;
536 sensor
->analog_gain
= v4l2_ctrl_new_std(
537 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
538 V4L2_CID_ANALOGUE_GAIN
,
539 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN
],
540 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX
],
541 max(sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP
], 1U),
542 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN
]);
544 /* Exposure limits will be updated soon, use just something here. */
545 sensor
->exposure
= v4l2_ctrl_new_std(
546 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
547 V4L2_CID_EXPOSURE
, 0, 0, 1, 0);
549 sensor
->hflip
= v4l2_ctrl_new_std(
550 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
551 V4L2_CID_HFLIP
, 0, 1, 1, 0);
552 sensor
->vflip
= v4l2_ctrl_new_std(
553 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
554 V4L2_CID_VFLIP
, 0, 1, 1, 0);
556 sensor
->vblank
= v4l2_ctrl_new_std(
557 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
558 V4L2_CID_VBLANK
, 0, 1, 1, 0);
561 sensor
->vblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
563 sensor
->hblank
= v4l2_ctrl_new_std(
564 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
565 V4L2_CID_HBLANK
, 0, 1, 1, 0);
568 sensor
->hblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
570 sensor
->pixel_rate_parray
= v4l2_ctrl_new_std(
571 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
572 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
574 v4l2_ctrl_new_std_menu_items(&sensor
->pixel_array
->ctrl_handler
,
575 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
576 ARRAY_SIZE(smiapp_test_patterns
) - 1,
577 0, 0, smiapp_test_patterns
);
579 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++) {
580 int max_value
= (1 << sensor
->csi_format
->width
) - 1;
581 sensor
->test_data
[i
] =
583 &sensor
->pixel_array
->ctrl_handler
,
584 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN_RED
+ i
,
585 0, max_value
, 1, max_value
);
588 if (sensor
->pixel_array
->ctrl_handler
.error
) {
589 dev_err(&client
->dev
,
590 "pixel array controls initialization failed (%d)\n",
591 sensor
->pixel_array
->ctrl_handler
.error
);
592 rval
= sensor
->pixel_array
->ctrl_handler
.error
;
596 sensor
->pixel_array
->sd
.ctrl_handler
=
597 &sensor
->pixel_array
->ctrl_handler
;
599 v4l2_ctrl_cluster(2, &sensor
->hflip
);
601 rval
= v4l2_ctrl_handler_init(&sensor
->src
->ctrl_handler
, 0);
604 sensor
->src
->ctrl_handler
.lock
= &sensor
->mutex
;
606 for (max
= 0; sensor
->platform_data
->op_sys_clock
[max
+ 1]; max
++);
608 sensor
->link_freq
= v4l2_ctrl_new_int_menu(
609 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
610 V4L2_CID_LINK_FREQ
, __fls(*valid_link_freqs
),
611 __ffs(*valid_link_freqs
), sensor
->platform_data
->op_sys_clock
);
613 sensor
->pixel_rate_csi
= v4l2_ctrl_new_std(
614 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
615 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
617 if (sensor
->src
->ctrl_handler
.error
) {
618 dev_err(&client
->dev
,
619 "src controls initialization failed (%d)\n",
620 sensor
->src
->ctrl_handler
.error
);
621 rval
= sensor
->src
->ctrl_handler
.error
;
625 sensor
->src
->sd
.ctrl_handler
=
626 &sensor
->src
->ctrl_handler
;
631 v4l2_ctrl_handler_free(&sensor
->pixel_array
->ctrl_handler
);
632 v4l2_ctrl_handler_free(&sensor
->src
->ctrl_handler
);
637 static void smiapp_free_controls(struct smiapp_sensor
*sensor
)
641 for (i
= 0; i
< sensor
->ssds_used
; i
++)
642 v4l2_ctrl_handler_free(&sensor
->ssds
[i
].ctrl_handler
);
645 static int smiapp_get_limits(struct smiapp_sensor
*sensor
, int const *limit
,
648 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
653 for (i
= 0; i
< n
; i
++) {
655 sensor
, smiapp_reg_limits
[limit
[i
]].addr
, &val
);
658 sensor
->limits
[limit
[i
]] = val
;
659 dev_dbg(&client
->dev
, "0x%8.8x \"%s\" = %u, 0x%x\n",
660 smiapp_reg_limits
[limit
[i
]].addr
,
661 smiapp_reg_limits
[limit
[i
]].what
, val
, val
);
667 static int smiapp_get_all_limits(struct smiapp_sensor
*sensor
)
672 for (i
= 0; i
< SMIAPP_LIMIT_LAST
; i
++) {
673 rval
= smiapp_get_limits(sensor
, &i
, 1);
678 if (sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] == 0)
679 smiapp_replace_limit(sensor
, SMIAPP_LIMIT_SCALER_N_MIN
, 16);
684 static int smiapp_get_limits_binning(struct smiapp_sensor
*sensor
)
686 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
687 static u32
const limits
[] = {
688 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
,
689 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN
,
690 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
,
691 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN
,
692 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
,
693 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN
,
694 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN
,
696 static u32
const limits_replace
[] = {
697 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES
,
698 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES
,
699 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK
,
700 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK
,
701 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK
,
702 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN
,
703 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN
,
708 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
] ==
709 SMIAPP_BINNING_CAPABILITY_NO
) {
710 for (i
= 0; i
< ARRAY_SIZE(limits
); i
++)
711 sensor
->limits
[limits
[i
]] =
712 sensor
->limits
[limits_replace
[i
]];
717 rval
= smiapp_get_limits(sensor
, limits
, ARRAY_SIZE(limits
));
722 * Sanity check whether the binning limits are valid. If not,
723 * use the non-binning ones.
725 if (sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
]
726 && sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
]
727 && sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
])
730 for (i
= 0; i
< ARRAY_SIZE(limits
); i
++) {
731 dev_dbg(&client
->dev
,
732 "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
733 smiapp_reg_limits
[limits
[i
]].addr
,
734 smiapp_reg_limits
[limits
[i
]].what
,
735 sensor
->limits
[limits_replace
[i
]],
736 sensor
->limits
[limits_replace
[i
]]);
737 sensor
->limits
[limits
[i
]] =
738 sensor
->limits
[limits_replace
[i
]];
744 static int smiapp_get_mbus_formats(struct smiapp_sensor
*sensor
)
746 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
747 struct smiapp_pll
*pll
= &sensor
->pll
;
748 unsigned int type
, n
;
749 unsigned int i
, pixel_order
;
753 sensor
, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE
, &type
);
757 dev_dbg(&client
->dev
, "data_format_model_type %d\n", type
);
759 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_PIXEL_ORDER
,
764 if (pixel_order
>= ARRAY_SIZE(pixel_order_str
)) {
765 dev_dbg(&client
->dev
, "bad pixel order %d\n", pixel_order
);
769 dev_dbg(&client
->dev
, "pixel order %d (%s)\n", pixel_order
,
770 pixel_order_str
[pixel_order
]);
773 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL
:
774 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N
;
776 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED
:
777 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N
;
783 sensor
->default_pixel_order
= pixel_order
;
784 sensor
->mbus_frame_fmts
= 0;
786 for (i
= 0; i
< n
; i
++) {
791 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i
), &fmt
);
795 dev_dbg(&client
->dev
, "%u: bpp %u, compressed %u\n",
796 i
, fmt
>> 8, (u8
)fmt
);
798 for (j
= 0; j
< ARRAY_SIZE(smiapp_csi_data_formats
); j
++) {
799 const struct smiapp_csi_data_format
*f
=
800 &smiapp_csi_data_formats
[j
];
802 if (f
->pixel_order
!= SMIAPP_PIXEL_ORDER_GRBG
)
805 if (f
->width
!= fmt
>> 8 || f
->compressed
!= (u8
)fmt
)
808 dev_dbg(&client
->dev
, "jolly good! %d\n", j
);
810 sensor
->default_mbus_frame_fmts
|= 1 << j
;
814 /* Figure out which BPP values can be used with which formats. */
815 pll
->binning_horizontal
= 1;
816 pll
->binning_vertical
= 1;
817 pll
->scale_m
= sensor
->scale_m
;
819 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
820 const struct smiapp_csi_data_format
*f
=
821 &smiapp_csi_data_formats
[i
];
822 unsigned long *valid_link_freqs
=
823 &sensor
->valid_link_freqs
[
824 f
->compressed
- SMIAPP_COMPRESSED_BASE
];
827 BUG_ON(f
->compressed
< SMIAPP_COMPRESSED_BASE
);
828 BUG_ON(f
->compressed
> SMIAPP_COMPRESSED_MAX
);
830 if (!(sensor
->default_mbus_frame_fmts
& 1 << i
))
833 pll
->bits_per_pixel
= f
->compressed
;
835 for (j
= 0; sensor
->platform_data
->op_sys_clock
[j
]; j
++) {
836 pll
->link_freq
= sensor
->platform_data
->op_sys_clock
[j
];
838 rval
= smiapp_pll_try(sensor
, pll
);
839 dev_dbg(&client
->dev
, "link freq %u Hz, bpp %u %s\n",
840 pll
->link_freq
, pll
->bits_per_pixel
,
841 rval
? "not ok" : "ok");
845 set_bit(j
, valid_link_freqs
);
848 if (!*valid_link_freqs
) {
849 dev_info(&client
->dev
,
850 "no valid link frequencies for %u bpp\n",
852 sensor
->default_mbus_frame_fmts
&= ~BIT(i
);
856 if (!sensor
->csi_format
857 || f
->width
> sensor
->csi_format
->width
858 || (f
->width
== sensor
->csi_format
->width
859 && f
->compressed
> sensor
->csi_format
->compressed
)) {
860 sensor
->csi_format
= f
;
861 sensor
->internal_csi_format
= f
;
865 if (!sensor
->csi_format
) {
866 dev_err(&client
->dev
, "no supported mbus code found\n");
870 smiapp_update_mbus_formats(sensor
);
875 static void smiapp_update_blanking(struct smiapp_sensor
*sensor
)
877 struct v4l2_ctrl
*vblank
= sensor
->vblank
;
878 struct v4l2_ctrl
*hblank
= sensor
->hblank
;
882 sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES
],
883 sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
] -
884 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
);
885 max
= sensor
->limits
[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN
] -
886 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
;
888 __v4l2_ctrl_modify_range(vblank
, min
, max
, vblank
->step
, min
);
891 sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
] -
892 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
,
893 sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
]);
894 max
= sensor
->limits
[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN
] -
895 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
;
897 __v4l2_ctrl_modify_range(hblank
, min
, max
, hblank
->step
, min
);
899 __smiapp_update_exposure_limits(sensor
);
902 static int smiapp_update_mode(struct smiapp_sensor
*sensor
)
904 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
905 unsigned int binning_mode
;
908 dev_dbg(&client
->dev
, "frame size: %dx%d\n",
909 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
,
910 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
911 dev_dbg(&client
->dev
, "csi format width: %d\n",
912 sensor
->csi_format
->width
);
914 /* Binning has to be set up here; it affects limits */
915 if (sensor
->binning_horizontal
== 1 &&
916 sensor
->binning_vertical
== 1) {
920 (sensor
->binning_horizontal
<< 4)
921 | sensor
->binning_vertical
;
924 sensor
, SMIAPP_REG_U8_BINNING_TYPE
, binning_type
);
930 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_BINNING_MODE
, binning_mode
);
934 /* Get updated limits due to binning */
935 rval
= smiapp_get_limits_binning(sensor
);
939 rval
= smiapp_pll_update(sensor
);
943 /* Output from pixel array, including blanking */
944 smiapp_update_blanking(sensor
);
946 dev_dbg(&client
->dev
, "vblank\t\t%d\n", sensor
->vblank
->val
);
947 dev_dbg(&client
->dev
, "hblank\t\t%d\n", sensor
->hblank
->val
);
949 dev_dbg(&client
->dev
, "real timeperframe\t100/%d\n",
950 sensor
->pll
.pixel_rate_pixel_array
/
951 ((sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
952 + sensor
->hblank
->val
) *
953 (sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
954 + sensor
->vblank
->val
) / 100));
961 * SMIA++ NVM handling
964 static int smiapp_read_nvm(struct smiapp_sensor
*sensor
,
970 np
= sensor
->nvm_size
/ SMIAPP_NVM_PAGE_SIZE
;
971 for (p
= 0; p
< np
; p
++) {
974 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT
, p
);
978 rval
= smiapp_write(sensor
,
979 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
,
980 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN
|
981 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN
);
985 for (i
= 0; i
< 1000; i
++) {
988 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS
, &s
);
993 if (s
& SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY
)
1003 for (i
= 0; i
< SMIAPP_NVM_PAGE_SIZE
; i
++) {
1006 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0
+ i
,
1016 rval2
= smiapp_write(sensor
, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
, 0);
1025 * SMIA++ CCI address control
1028 static int smiapp_change_cci_addr(struct smiapp_sensor
*sensor
)
1030 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1034 client
->addr
= sensor
->platform_data
->i2c_addr_dfl
;
1036 rval
= smiapp_write(sensor
,
1037 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
,
1038 sensor
->platform_data
->i2c_addr_alt
<< 1);
1042 client
->addr
= sensor
->platform_data
->i2c_addr_alt
;
1044 /* verify addr change went ok */
1045 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
, &val
);
1049 if (val
!= sensor
->platform_data
->i2c_addr_alt
<< 1)
1057 * SMIA++ Mode Control
1060 static int smiapp_setup_flash_strobe(struct smiapp_sensor
*sensor
)
1062 struct smiapp_flash_strobe_parms
*strobe_setup
;
1063 unsigned int ext_freq
= sensor
->platform_data
->ext_clk
;
1065 u32 strobe_adjustment
;
1066 u32 strobe_width_high_rs
;
1069 strobe_setup
= sensor
->platform_data
->strobe_setup
;
1072 * How to calculate registers related to strobe length. Please
1073 * do not change, or if you do at least know what you're
1076 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1078 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1079 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1081 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1082 * flash_strobe_adjustment E N, [1 - 0xff]
1084 * The formula above is written as below to keep it on one
1087 * l / 10^6 = w / e * a
1089 * Let's mark w * a by x:
1097 * The strobe width must be at least as long as requested,
1098 * thus rounding upwards is needed.
1100 * x = (l * e + 10^6 - 1) / 10^6
1101 * -----------------------------
1103 * Maximum possible accuracy is wanted at all times. Thus keep
1104 * a as small as possible.
1106 * Calculate a, assuming maximum w, with rounding upwards:
1108 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1109 * -------------------------------------
1111 * Thus, we also get w, with that a, with rounding upwards:
1113 * w = (x + a - 1) / a
1114 * -------------------
1118 * x E [1, (2^16 - 1) * (2^8 - 1)]
1120 * Substituting maximum x to the original formula (with rounding),
1121 * the maximum l is thus
1123 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1125 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1126 * --------------------------------------------------
1128 * flash_strobe_length must be clamped between 1 and
1129 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1133 * flash_strobe_adjustment = ((flash_strobe_length *
1134 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1136 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1137 * EXTCLK freq + 10^6 - 1) / 10^6 +
1138 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1140 tmp
= div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1141 1000000 + 1, ext_freq
);
1142 strobe_setup
->strobe_width_high_us
=
1143 clamp_t(u32
, strobe_setup
->strobe_width_high_us
, 1, tmp
);
1145 tmp
= div_u64(((u64
)strobe_setup
->strobe_width_high_us
* (u64
)ext_freq
+
1146 1000000 - 1), 1000000ULL);
1147 strobe_adjustment
= (tmp
+ (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1148 strobe_width_high_rs
= (tmp
+ strobe_adjustment
- 1) /
1151 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_MODE_RS
,
1152 strobe_setup
->mode
);
1156 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT
,
1161 rval
= smiapp_write(
1162 sensor
, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL
,
1163 strobe_width_high_rs
);
1167 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL
,
1168 strobe_setup
->strobe_delay
);
1172 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_FLASH_STROBE_START_POINT
,
1173 strobe_setup
->stobe_start_point
);
1177 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_TRIGGER_RS
,
1178 strobe_setup
->trigger
);
1181 sensor
->platform_data
->strobe_setup
->trigger
= 0;
1186 /* -----------------------------------------------------------------------------
1190 static int smiapp_power_on(struct smiapp_sensor
*sensor
)
1192 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1196 rval
= regulator_enable(sensor
->vana
);
1198 dev_err(&client
->dev
, "failed to enable vana regulator\n");
1201 usleep_range(1000, 1000);
1203 if (sensor
->platform_data
->set_xclk
)
1204 rval
= sensor
->platform_data
->set_xclk(
1205 &sensor
->src
->sd
, sensor
->platform_data
->ext_clk
);
1207 rval
= clk_prepare_enable(sensor
->ext_clk
);
1209 dev_dbg(&client
->dev
, "failed to enable xclk\n");
1212 usleep_range(1000, 1000);
1214 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1215 gpio_set_value(sensor
->platform_data
->xshutdown
, 1);
1217 sleep
= SMIAPP_RESET_DELAY(sensor
->platform_data
->ext_clk
);
1218 usleep_range(sleep
, sleep
);
1221 * Failures to respond to the address change command have been noticed.
1222 * Those failures seem to be caused by the sensor requiring a longer
1223 * boot time than advertised. An additional 10ms delay seems to work
1224 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1225 * unnecessary. The failures need to be investigated to find a proper
1226 * fix, and a delay will likely need to be added here if the I2C write
1227 * retry hack is reverted before the root cause of the boot time issue
1231 if (sensor
->platform_data
->i2c_addr_alt
) {
1232 rval
= smiapp_change_cci_addr(sensor
);
1234 dev_err(&client
->dev
, "cci address change error\n");
1235 goto out_cci_addr_fail
;
1239 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_SOFTWARE_RESET
,
1240 SMIAPP_SOFTWARE_RESET
);
1242 dev_err(&client
->dev
, "software reset failed\n");
1243 goto out_cci_addr_fail
;
1246 if (sensor
->platform_data
->i2c_addr_alt
) {
1247 rval
= smiapp_change_cci_addr(sensor
);
1249 dev_err(&client
->dev
, "cci address change error\n");
1250 goto out_cci_addr_fail
;
1254 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_COMPRESSION_MODE
,
1255 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR
);
1257 dev_err(&client
->dev
, "compression mode set failed\n");
1258 goto out_cci_addr_fail
;
1261 rval
= smiapp_write(
1262 sensor
, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ
,
1263 sensor
->platform_data
->ext_clk
/ (1000000 / (1 << 8)));
1265 dev_err(&client
->dev
, "extclk frequency set failed\n");
1266 goto out_cci_addr_fail
;
1269 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_LANE_MODE
,
1270 sensor
->platform_data
->lanes
- 1);
1272 dev_err(&client
->dev
, "csi lane mode set failed\n");
1273 goto out_cci_addr_fail
;
1276 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FAST_STANDBY_CTRL
,
1277 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE
);
1279 dev_err(&client
->dev
, "fast standby set failed\n");
1280 goto out_cci_addr_fail
;
1283 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_SIGNALLING_MODE
,
1284 sensor
->platform_data
->csi_signalling_mode
);
1286 dev_err(&client
->dev
, "csi signalling mode set failed\n");
1287 goto out_cci_addr_fail
;
1290 /* DPHY control done by sensor based on requested link rate */
1291 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_DPHY_CTRL
,
1292 SMIAPP_DPHY_CTRL_UI
);
1296 rval
= smiapp_call_quirk(sensor
, post_poweron
);
1298 dev_err(&client
->dev
, "post_poweron quirks failed\n");
1299 goto out_cci_addr_fail
;
1302 /* Are we still initialising...? If yes, return here. */
1303 if (!sensor
->pixel_array
)
1306 rval
= v4l2_ctrl_handler_setup(
1307 &sensor
->pixel_array
->ctrl_handler
);
1309 goto out_cci_addr_fail
;
1311 rval
= v4l2_ctrl_handler_setup(&sensor
->src
->ctrl_handler
);
1313 goto out_cci_addr_fail
;
1315 mutex_lock(&sensor
->mutex
);
1316 rval
= smiapp_update_mode(sensor
);
1317 mutex_unlock(&sensor
->mutex
);
1319 goto out_cci_addr_fail
;
1324 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1325 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
1326 if (sensor
->platform_data
->set_xclk
)
1327 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
1329 clk_disable_unprepare(sensor
->ext_clk
);
1332 regulator_disable(sensor
->vana
);
1336 static void smiapp_power_off(struct smiapp_sensor
*sensor
)
1339 * Currently power/clock to lens are enable/disabled separately
1340 * but they are essentially the same signals. So if the sensor is
1341 * powered off while the lens is powered on the sensor does not
1342 * really see a power off and next time the cci address change
1343 * will fail. So do a soft reset explicitly here.
1345 if (sensor
->platform_data
->i2c_addr_alt
)
1346 smiapp_write(sensor
,
1347 SMIAPP_REG_U8_SOFTWARE_RESET
,
1348 SMIAPP_SOFTWARE_RESET
);
1350 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1351 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
1352 if (sensor
->platform_data
->set_xclk
)
1353 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
1355 clk_disable_unprepare(sensor
->ext_clk
);
1356 usleep_range(5000, 5000);
1357 regulator_disable(sensor
->vana
);
1358 sensor
->streaming
= false;
1361 static int smiapp_set_power(struct v4l2_subdev
*subdev
, int on
)
1363 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1366 mutex_lock(&sensor
->power_mutex
);
1368 if (on
&& !sensor
->power_count
) {
1369 /* Power on and perform initialisation. */
1370 ret
= smiapp_power_on(sensor
);
1373 } else if (!on
&& sensor
->power_count
== 1) {
1374 smiapp_power_off(sensor
);
1377 /* Update the power count. */
1378 sensor
->power_count
+= on
? 1 : -1;
1379 WARN_ON(sensor
->power_count
< 0);
1382 mutex_unlock(&sensor
->power_mutex
);
1386 /* -----------------------------------------------------------------------------
1387 * Video stream management
1390 static int smiapp_start_streaming(struct smiapp_sensor
*sensor
)
1392 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1395 mutex_lock(&sensor
->mutex
);
1397 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_CSI_DATA_FORMAT
,
1398 (sensor
->csi_format
->width
<< 8) |
1399 sensor
->csi_format
->compressed
);
1403 rval
= smiapp_pll_configure(sensor
);
1407 /* Analog crop start coordinates */
1408 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_ADDR_START
,
1409 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
);
1413 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_ADDR_START
,
1414 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
);
1418 /* Analog crop end coordinates */
1419 rval
= smiapp_write(
1420 sensor
, SMIAPP_REG_U16_X_ADDR_END
,
1421 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
1422 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
- 1);
1426 rval
= smiapp_write(
1427 sensor
, SMIAPP_REG_U16_Y_ADDR_END
,
1428 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
1429 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
- 1);
1434 * Output from pixel array, including blanking, is set using
1435 * controls below. No need to set here.
1439 if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
1440 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
1441 rval
= smiapp_write(
1442 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET
,
1443 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].left
);
1447 rval
= smiapp_write(
1448 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET
,
1449 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].top
);
1453 rval
= smiapp_write(
1454 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH
,
1455 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].width
);
1459 rval
= smiapp_write(
1460 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT
,
1461 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].height
);
1467 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
1468 != SMIAPP_SCALING_CAPABILITY_NONE
) {
1469 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALING_MODE
,
1470 sensor
->scaling_mode
);
1474 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALE_M
,
1480 /* Output size from sensor */
1481 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_OUTPUT_SIZE
,
1482 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
);
1485 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_OUTPUT_SIZE
,
1486 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
1490 if ((sensor
->flash_capability
&
1491 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE
|
1492 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE
)) &&
1493 sensor
->platform_data
->strobe_setup
!= NULL
&&
1494 sensor
->platform_data
->strobe_setup
->trigger
!= 0) {
1495 rval
= smiapp_setup_flash_strobe(sensor
);
1500 rval
= smiapp_call_quirk(sensor
, pre_streamon
);
1502 dev_err(&client
->dev
, "pre_streamon quirks failed\n");
1506 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1507 SMIAPP_MODE_SELECT_STREAMING
);
1510 mutex_unlock(&sensor
->mutex
);
1515 static int smiapp_stop_streaming(struct smiapp_sensor
*sensor
)
1517 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1520 mutex_lock(&sensor
->mutex
);
1521 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1522 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY
);
1526 rval
= smiapp_call_quirk(sensor
, post_streamoff
);
1528 dev_err(&client
->dev
, "post_streamoff quirks failed\n");
1531 mutex_unlock(&sensor
->mutex
);
1535 /* -----------------------------------------------------------------------------
1536 * V4L2 subdev video operations
1539 static int smiapp_set_stream(struct v4l2_subdev
*subdev
, int enable
)
1541 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1544 if (sensor
->streaming
== enable
)
1548 sensor
->streaming
= true;
1549 rval
= smiapp_start_streaming(sensor
);
1551 sensor
->streaming
= false;
1553 rval
= smiapp_stop_streaming(sensor
);
1554 sensor
->streaming
= false;
1560 static int smiapp_enum_mbus_code(struct v4l2_subdev
*subdev
,
1561 struct v4l2_subdev_fh
*fh
,
1562 struct v4l2_subdev_mbus_code_enum
*code
)
1564 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1565 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1570 mutex_lock(&sensor
->mutex
);
1572 dev_err(&client
->dev
, "subdev %s, pad %d, index %d\n",
1573 subdev
->name
, code
->pad
, code
->index
);
1575 if (subdev
!= &sensor
->src
->sd
|| code
->pad
!= SMIAPP_PAD_SRC
) {
1579 code
->code
= sensor
->internal_csi_format
->code
;
1584 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1585 if (sensor
->mbus_frame_fmts
& (1 << i
))
1588 if (idx
== code
->index
) {
1589 code
->code
= smiapp_csi_data_formats
[i
].code
;
1590 dev_err(&client
->dev
, "found index %d, i %d, code %x\n",
1591 code
->index
, i
, code
->code
);
1598 mutex_unlock(&sensor
->mutex
);
1603 static u32
__smiapp_get_mbus_code(struct v4l2_subdev
*subdev
,
1606 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1608 if (subdev
== &sensor
->src
->sd
&& pad
== SMIAPP_PAD_SRC
)
1609 return sensor
->csi_format
->code
;
1611 return sensor
->internal_csi_format
->code
;
1614 static int __smiapp_get_format(struct v4l2_subdev
*subdev
,
1615 struct v4l2_subdev_fh
*fh
,
1616 struct v4l2_subdev_format
*fmt
)
1618 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1620 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1621 fmt
->format
= *v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1623 struct v4l2_rect
*r
;
1625 if (fmt
->pad
== ssd
->source_pad
)
1626 r
= &ssd
->crop
[ssd
->source_pad
];
1630 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1631 fmt
->format
.width
= r
->width
;
1632 fmt
->format
.height
= r
->height
;
1633 fmt
->format
.field
= V4L2_FIELD_NONE
;
1639 static int smiapp_get_format(struct v4l2_subdev
*subdev
,
1640 struct v4l2_subdev_fh
*fh
,
1641 struct v4l2_subdev_format
*fmt
)
1643 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1646 mutex_lock(&sensor
->mutex
);
1647 rval
= __smiapp_get_format(subdev
, fh
, fmt
);
1648 mutex_unlock(&sensor
->mutex
);
1653 static void smiapp_get_crop_compose(struct v4l2_subdev
*subdev
,
1654 struct v4l2_subdev_fh
*fh
,
1655 struct v4l2_rect
**crops
,
1656 struct v4l2_rect
**comps
, int which
)
1658 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1661 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1663 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++)
1664 crops
[i
] = &ssd
->crop
[i
];
1666 *comps
= &ssd
->compose
;
1669 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++) {
1670 crops
[i
] = v4l2_subdev_get_try_crop(fh
, i
);
1675 *comps
= v4l2_subdev_get_try_compose(fh
,
1682 /* Changes require propagation only on sink pad. */
1683 static void smiapp_propagate(struct v4l2_subdev
*subdev
,
1684 struct v4l2_subdev_fh
*fh
, int which
,
1687 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1688 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1689 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
1691 smiapp_get_crop_compose(subdev
, fh
, crops
, &comp
, which
);
1694 case V4L2_SEL_TGT_CROP
:
1695 comp
->width
= crops
[SMIAPP_PAD_SINK
]->width
;
1696 comp
->height
= crops
[SMIAPP_PAD_SINK
]->height
;
1697 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1698 if (ssd
== sensor
->scaler
) {
1701 SMIAPP_LIMIT_SCALER_N_MIN
];
1702 sensor
->scaling_mode
=
1703 SMIAPP_SCALING_MODE_NONE
;
1704 } else if (ssd
== sensor
->binner
) {
1705 sensor
->binning_horizontal
= 1;
1706 sensor
->binning_vertical
= 1;
1710 case V4L2_SEL_TGT_COMPOSE
:
1711 *crops
[SMIAPP_PAD_SRC
] = *comp
;
1718 static const struct smiapp_csi_data_format
1719 *smiapp_validate_csi_data_format(struct smiapp_sensor
*sensor
, u32 code
)
1721 const struct smiapp_csi_data_format
*csi_format
= sensor
->csi_format
;
1724 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1725 if (sensor
->mbus_frame_fmts
& (1 << i
)
1726 && smiapp_csi_data_formats
[i
].code
== code
)
1727 return &smiapp_csi_data_formats
[i
];
1733 static int smiapp_set_format_source(struct v4l2_subdev
*subdev
,
1734 struct v4l2_subdev_fh
*fh
,
1735 struct v4l2_subdev_format
*fmt
)
1737 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1738 const struct smiapp_csi_data_format
*csi_format
,
1739 *old_csi_format
= sensor
->csi_format
;
1740 unsigned long *valid_link_freqs
;
1741 u32 code
= fmt
->format
.code
;
1745 rval
= __smiapp_get_format(subdev
, fh
, fmt
);
1750 * Media bus code is changeable on src subdev's source pad. On
1751 * other source pads we just get format here.
1753 if (subdev
!= &sensor
->src
->sd
)
1756 csi_format
= smiapp_validate_csi_data_format(sensor
, code
);
1758 fmt
->format
.code
= csi_format
->code
;
1760 if (fmt
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1763 sensor
->csi_format
= csi_format
;
1765 if (csi_format
->width
!= old_csi_format
->width
)
1766 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
1767 __v4l2_ctrl_modify_range(
1768 sensor
->test_data
[i
], 0,
1769 (1 << csi_format
->width
) - 1, 1, 0);
1771 if (csi_format
->compressed
== old_csi_format
->compressed
)
1775 &sensor
->valid_link_freqs
[sensor
->csi_format
->compressed
1776 - SMIAPP_COMPRESSED_BASE
];
1778 __v4l2_ctrl_modify_range(
1779 sensor
->link_freq
, 0,
1780 __fls(*valid_link_freqs
), ~*valid_link_freqs
,
1781 __ffs(*valid_link_freqs
));
1783 return smiapp_pll_update(sensor
);
1786 static int smiapp_set_format(struct v4l2_subdev
*subdev
,
1787 struct v4l2_subdev_fh
*fh
,
1788 struct v4l2_subdev_format
*fmt
)
1790 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1791 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1792 struct v4l2_rect
*crops
[SMIAPP_PADS
];
1794 mutex_lock(&sensor
->mutex
);
1796 if (fmt
->pad
== ssd
->source_pad
) {
1799 rval
= smiapp_set_format_source(subdev
, fh
, fmt
);
1801 mutex_unlock(&sensor
->mutex
);
1806 /* Sink pad. Width and height are changeable here. */
1807 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1808 fmt
->format
.width
&= ~1;
1809 fmt
->format
.height
&= ~1;
1810 fmt
->format
.field
= V4L2_FIELD_NONE
;
1813 clamp(fmt
->format
.width
,
1814 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
1815 sensor
->limits
[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE
]);
1816 fmt
->format
.height
=
1817 clamp(fmt
->format
.height
,
1818 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
1819 sensor
->limits
[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE
]);
1821 smiapp_get_crop_compose(subdev
, fh
, crops
, NULL
, fmt
->which
);
1823 crops
[ssd
->sink_pad
]->left
= 0;
1824 crops
[ssd
->sink_pad
]->top
= 0;
1825 crops
[ssd
->sink_pad
]->width
= fmt
->format
.width
;
1826 crops
[ssd
->sink_pad
]->height
= fmt
->format
.height
;
1827 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
1828 ssd
->sink_fmt
= *crops
[ssd
->sink_pad
];
1829 smiapp_propagate(subdev
, fh
, fmt
->which
,
1832 mutex_unlock(&sensor
->mutex
);
1838 * Calculate goodness of scaled image size compared to expected image
1839 * size and flags provided.
1841 #define SCALING_GOODNESS 100000
1842 #define SCALING_GOODNESS_EXTREME 100000000
1843 static int scaling_goodness(struct v4l2_subdev
*subdev
, int w
, int ask_w
,
1844 int h
, int ask_h
, u32 flags
)
1846 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1847 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1855 if (flags
& V4L2_SEL_FLAG_GE
) {
1857 val
-= SCALING_GOODNESS
;
1859 val
-= SCALING_GOODNESS
;
1862 if (flags
& V4L2_SEL_FLAG_LE
) {
1864 val
-= SCALING_GOODNESS
;
1866 val
-= SCALING_GOODNESS
;
1869 val
-= abs(w
- ask_w
);
1870 val
-= abs(h
- ask_h
);
1872 if (w
< sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
])
1873 val
-= SCALING_GOODNESS_EXTREME
;
1875 dev_dbg(&client
->dev
, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1876 w
, ask_h
, h
, ask_h
, val
);
1881 static void smiapp_set_compose_binner(struct v4l2_subdev
*subdev
,
1882 struct v4l2_subdev_fh
*fh
,
1883 struct v4l2_subdev_selection
*sel
,
1884 struct v4l2_rect
**crops
,
1885 struct v4l2_rect
*comp
)
1887 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1889 unsigned int binh
= 1, binv
= 1;
1890 int best
= scaling_goodness(
1892 crops
[SMIAPP_PAD_SINK
]->width
, sel
->r
.width
,
1893 crops
[SMIAPP_PAD_SINK
]->height
, sel
->r
.height
, sel
->flags
);
1895 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
1896 int this = scaling_goodness(
1898 crops
[SMIAPP_PAD_SINK
]->width
1899 / sensor
->binning_subtypes
[i
].horizontal
,
1901 crops
[SMIAPP_PAD_SINK
]->height
1902 / sensor
->binning_subtypes
[i
].vertical
,
1903 sel
->r
.height
, sel
->flags
);
1906 binh
= sensor
->binning_subtypes
[i
].horizontal
;
1907 binv
= sensor
->binning_subtypes
[i
].vertical
;
1911 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1912 sensor
->binning_vertical
= binv
;
1913 sensor
->binning_horizontal
= binh
;
1916 sel
->r
.width
= (crops
[SMIAPP_PAD_SINK
]->width
/ binh
) & ~1;
1917 sel
->r
.height
= (crops
[SMIAPP_PAD_SINK
]->height
/ binv
) & ~1;
1921 * Calculate best scaling ratio and mode for given output resolution.
1923 * Try all of these: horizontal ratio, vertical ratio and smallest
1924 * size possible (horizontally).
1926 * Also try whether horizontal scaler or full scaler gives a better
1929 static void smiapp_set_compose_scaler(struct v4l2_subdev
*subdev
,
1930 struct v4l2_subdev_fh
*fh
,
1931 struct v4l2_subdev_selection
*sel
,
1932 struct v4l2_rect
**crops
,
1933 struct v4l2_rect
*comp
)
1935 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1936 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1937 u32 min
, max
, a
, b
, max_m
;
1938 u32 scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
1939 int mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
1945 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
1946 crops
[SMIAPP_PAD_SINK
]->width
);
1947 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
1948 crops
[SMIAPP_PAD_SINK
]->height
);
1950 a
= crops
[SMIAPP_PAD_SINK
]->width
1951 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] / sel
->r
.width
;
1952 b
= crops
[SMIAPP_PAD_SINK
]->height
1953 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] / sel
->r
.height
;
1954 max_m
= crops
[SMIAPP_PAD_SINK
]->width
1955 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]
1956 / sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
];
1958 a
= clamp(a
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1959 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1960 b
= clamp(b
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1961 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1962 max_m
= clamp(max_m
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1963 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1965 dev_dbg(&client
->dev
, "scaling: a %d b %d max_m %d\n", a
, b
, max_m
);
1967 min
= min(max_m
, min(a
, b
));
1968 max
= min(max_m
, max(a
, b
));
1977 try[ntry
] = min
+ 1;
1980 try[ntry
] = max
+ 1;
1985 for (i
= 0; i
< ntry
; i
++) {
1986 int this = scaling_goodness(
1988 crops
[SMIAPP_PAD_SINK
]->width
1990 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
1992 crops
[SMIAPP_PAD_SINK
]->height
,
1996 dev_dbg(&client
->dev
, "trying factor %d (%d)\n", try[i
], i
);
2000 mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
2004 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2005 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2008 this = scaling_goodness(
2009 subdev
, crops
[SMIAPP_PAD_SINK
]->width
2011 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2013 crops
[SMIAPP_PAD_SINK
]->height
2015 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2021 mode
= SMIAPP_SCALING_MODE_BOTH
;
2027 (crops
[SMIAPP_PAD_SINK
]->width
2029 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]) & ~1;
2030 if (mode
== SMIAPP_SCALING_MODE_BOTH
)
2032 (crops
[SMIAPP_PAD_SINK
]->height
2034 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
])
2037 sel
->r
.height
= crops
[SMIAPP_PAD_SINK
]->height
;
2039 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2040 sensor
->scale_m
= scale_m
;
2041 sensor
->scaling_mode
= mode
;
2044 /* We're only called on source pads. This function sets scaling. */
2045 static int smiapp_set_compose(struct v4l2_subdev
*subdev
,
2046 struct v4l2_subdev_fh
*fh
,
2047 struct v4l2_subdev_selection
*sel
)
2049 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2050 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2051 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2053 smiapp_get_crop_compose(subdev
, fh
, crops
, &comp
, sel
->which
);
2058 if (ssd
== sensor
->binner
)
2059 smiapp_set_compose_binner(subdev
, fh
, sel
, crops
, comp
);
2061 smiapp_set_compose_scaler(subdev
, fh
, sel
, crops
, comp
);
2064 smiapp_propagate(subdev
, fh
, sel
->which
,
2065 V4L2_SEL_TGT_COMPOSE
);
2067 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
2068 return smiapp_update_mode(sensor
);
2073 static int __smiapp_sel_supported(struct v4l2_subdev
*subdev
,
2074 struct v4l2_subdev_selection
*sel
)
2076 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2077 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2079 /* We only implement crop in three places. */
2080 switch (sel
->target
) {
2081 case V4L2_SEL_TGT_CROP
:
2082 case V4L2_SEL_TGT_CROP_BOUNDS
:
2083 if (ssd
== sensor
->pixel_array
2084 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2086 if (ssd
== sensor
->src
2087 && sel
->pad
== SMIAPP_PAD_SRC
)
2089 if (ssd
== sensor
->scaler
2090 && sel
->pad
== SMIAPP_PAD_SINK
2091 && sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
2092 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
)
2095 case V4L2_SEL_TGT_NATIVE_SIZE
:
2096 if (ssd
== sensor
->pixel_array
2097 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2100 case V4L2_SEL_TGT_COMPOSE
:
2101 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2102 if (sel
->pad
== ssd
->source_pad
)
2104 if (ssd
== sensor
->binner
)
2106 if (ssd
== sensor
->scaler
2107 && sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2108 != SMIAPP_SCALING_CAPABILITY_NONE
)
2116 static int smiapp_set_crop(struct v4l2_subdev
*subdev
,
2117 struct v4l2_subdev_fh
*fh
,
2118 struct v4l2_subdev_selection
*sel
)
2120 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2121 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2122 struct v4l2_rect
*src_size
, *crops
[SMIAPP_PADS
];
2123 struct v4l2_rect _r
;
2125 smiapp_get_crop_compose(subdev
, fh
, crops
, NULL
, sel
->which
);
2127 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2128 if (sel
->pad
== ssd
->sink_pad
)
2129 src_size
= &ssd
->sink_fmt
;
2131 src_size
= &ssd
->compose
;
2133 if (sel
->pad
== ssd
->sink_pad
) {
2136 _r
.width
= v4l2_subdev_get_try_format(fh
, sel
->pad
)
2138 _r
.height
= v4l2_subdev_get_try_format(fh
, sel
->pad
)
2143 v4l2_subdev_get_try_compose(
2148 if (ssd
== sensor
->src
&& sel
->pad
== SMIAPP_PAD_SRC
) {
2153 sel
->r
.width
= min(sel
->r
.width
, src_size
->width
);
2154 sel
->r
.height
= min(sel
->r
.height
, src_size
->height
);
2156 sel
->r
.left
= min_t(int, sel
->r
.left
, src_size
->width
- sel
->r
.width
);
2157 sel
->r
.top
= min_t(int, sel
->r
.top
, src_size
->height
- sel
->r
.height
);
2159 *crops
[sel
->pad
] = sel
->r
;
2161 if (ssd
!= sensor
->pixel_array
&& sel
->pad
== SMIAPP_PAD_SINK
)
2162 smiapp_propagate(subdev
, fh
, sel
->which
,
2168 static int __smiapp_get_selection(struct v4l2_subdev
*subdev
,
2169 struct v4l2_subdev_fh
*fh
,
2170 struct v4l2_subdev_selection
*sel
)
2172 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2173 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2174 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2175 struct v4l2_rect sink_fmt
;
2178 ret
= __smiapp_sel_supported(subdev
, sel
);
2182 smiapp_get_crop_compose(subdev
, fh
, crops
, &comp
, sel
->which
);
2184 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2185 sink_fmt
= ssd
->sink_fmt
;
2187 struct v4l2_mbus_framefmt
*fmt
=
2188 v4l2_subdev_get_try_format(fh
, ssd
->sink_pad
);
2192 sink_fmt
.width
= fmt
->width
;
2193 sink_fmt
.height
= fmt
->height
;
2196 switch (sel
->target
) {
2197 case V4L2_SEL_TGT_CROP_BOUNDS
:
2198 case V4L2_SEL_TGT_NATIVE_SIZE
:
2199 if (ssd
== sensor
->pixel_array
) {
2200 sel
->r
.left
= sel
->r
.top
= 0;
2202 sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2204 sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2205 } else if (sel
->pad
== ssd
->sink_pad
) {
2211 case V4L2_SEL_TGT_CROP
:
2212 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2213 sel
->r
= *crops
[sel
->pad
];
2215 case V4L2_SEL_TGT_COMPOSE
:
2223 static int smiapp_get_selection(struct v4l2_subdev
*subdev
,
2224 struct v4l2_subdev_fh
*fh
,
2225 struct v4l2_subdev_selection
*sel
)
2227 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2230 mutex_lock(&sensor
->mutex
);
2231 rval
= __smiapp_get_selection(subdev
, fh
, sel
);
2232 mutex_unlock(&sensor
->mutex
);
2236 static int smiapp_set_selection(struct v4l2_subdev
*subdev
,
2237 struct v4l2_subdev_fh
*fh
,
2238 struct v4l2_subdev_selection
*sel
)
2240 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2243 ret
= __smiapp_sel_supported(subdev
, sel
);
2247 mutex_lock(&sensor
->mutex
);
2249 sel
->r
.left
= max(0, sel
->r
.left
& ~1);
2250 sel
->r
.top
= max(0, sel
->r
.top
& ~1);
2251 sel
->r
.width
= SMIAPP_ALIGN_DIM(sel
->r
.width
, sel
->flags
);
2252 sel
->r
.height
= SMIAPP_ALIGN_DIM(sel
->r
.height
, sel
->flags
);
2254 sel
->r
.width
= max_t(unsigned int,
2255 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
2257 sel
->r
.height
= max_t(unsigned int,
2258 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
2261 switch (sel
->target
) {
2262 case V4L2_SEL_TGT_CROP
:
2263 ret
= smiapp_set_crop(subdev
, fh
, sel
);
2265 case V4L2_SEL_TGT_COMPOSE
:
2266 ret
= smiapp_set_compose(subdev
, fh
, sel
);
2272 mutex_unlock(&sensor
->mutex
);
2276 static int smiapp_get_skip_frames(struct v4l2_subdev
*subdev
, u32
*frames
)
2278 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2280 *frames
= sensor
->frame_skip
;
2284 /* -----------------------------------------------------------------------------
2289 smiapp_sysfs_nvm_read(struct device
*dev
, struct device_attribute
*attr
,
2292 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2293 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2294 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2295 unsigned int nbytes
;
2297 if (!sensor
->dev_init_done
)
2300 if (!sensor
->nvm_size
) {
2301 /* NVM not read yet - read it now */
2302 sensor
->nvm_size
= sensor
->platform_data
->nvm_size
;
2303 if (smiapp_set_power(subdev
, 1) < 0)
2305 if (smiapp_read_nvm(sensor
, sensor
->nvm
)) {
2306 dev_err(&client
->dev
, "nvm read failed\n");
2309 smiapp_set_power(subdev
, 0);
2312 * NVM is still way below a PAGE_SIZE, so we can safely
2313 * assume this for now.
2315 nbytes
= min_t(unsigned int, sensor
->nvm_size
, PAGE_SIZE
);
2316 memcpy(buf
, sensor
->nvm
, nbytes
);
2320 static DEVICE_ATTR(nvm
, S_IRUGO
, smiapp_sysfs_nvm_read
, NULL
);
2323 smiapp_sysfs_ident_read(struct device
*dev
, struct device_attribute
*attr
,
2326 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2327 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2328 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2330 return snprintf(buf
, PAGE_SIZE
, "%2.2x%4.4x%2.2x\n",
2331 minfo
->manufacturer_id
, minfo
->model_id
,
2332 minfo
->revision_number_major
) + 1;
2335 static DEVICE_ATTR(ident
, S_IRUGO
, smiapp_sysfs_ident_read
, NULL
);
2337 /* -----------------------------------------------------------------------------
2338 * V4L2 subdev core operations
2341 static int smiapp_identify_module(struct v4l2_subdev
*subdev
)
2343 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2344 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2345 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2349 minfo
->name
= SMIAPP_NAME
;
2352 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MANUFACTURER_ID
,
2353 &minfo
->manufacturer_id
);
2355 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U16_MODEL_ID
,
2358 rval
= smiapp_read_8only(sensor
,
2359 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR
,
2360 &minfo
->revision_number_major
);
2362 rval
= smiapp_read_8only(sensor
,
2363 SMIAPP_REG_U8_REVISION_NUMBER_MINOR
,
2364 &minfo
->revision_number_minor
);
2366 rval
= smiapp_read_8only(sensor
,
2367 SMIAPP_REG_U8_MODULE_DATE_YEAR
,
2368 &minfo
->module_year
);
2370 rval
= smiapp_read_8only(sensor
,
2371 SMIAPP_REG_U8_MODULE_DATE_MONTH
,
2372 &minfo
->module_month
);
2374 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MODULE_DATE_DAY
,
2375 &minfo
->module_day
);
2379 rval
= smiapp_read_8only(sensor
,
2380 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID
,
2381 &minfo
->sensor_manufacturer_id
);
2383 rval
= smiapp_read_8only(sensor
,
2384 SMIAPP_REG_U16_SENSOR_MODEL_ID
,
2385 &minfo
->sensor_model_id
);
2387 rval
= smiapp_read_8only(sensor
,
2388 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER
,
2389 &minfo
->sensor_revision_number
);
2391 rval
= smiapp_read_8only(sensor
,
2392 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION
,
2393 &minfo
->sensor_firmware_version
);
2397 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIA_VERSION
,
2398 &minfo
->smia_version
);
2400 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIAPP_VERSION
,
2401 &minfo
->smiapp_version
);
2404 dev_err(&client
->dev
, "sensor detection failed\n");
2408 dev_dbg(&client
->dev
, "module 0x%2.2x-0x%4.4x\n",
2409 minfo
->manufacturer_id
, minfo
->model_id
);
2411 dev_dbg(&client
->dev
,
2412 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2413 minfo
->revision_number_major
, minfo
->revision_number_minor
,
2414 minfo
->module_year
, minfo
->module_month
, minfo
->module_day
);
2416 dev_dbg(&client
->dev
, "sensor 0x%2.2x-0x%4.4x\n",
2417 minfo
->sensor_manufacturer_id
, minfo
->sensor_model_id
);
2419 dev_dbg(&client
->dev
,
2420 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2421 minfo
->sensor_revision_number
, minfo
->sensor_firmware_version
);
2423 dev_dbg(&client
->dev
, "smia version %2.2d smiapp version %2.2d\n",
2424 minfo
->smia_version
, minfo
->smiapp_version
);
2427 * Some modules have bad data in the lvalues below. Hope the
2428 * rvalues have better stuff. The lvalues are module
2429 * parameters whereas the rvalues are sensor parameters.
2431 if (!minfo
->manufacturer_id
&& !minfo
->model_id
) {
2432 minfo
->manufacturer_id
= minfo
->sensor_manufacturer_id
;
2433 minfo
->model_id
= minfo
->sensor_model_id
;
2434 minfo
->revision_number_major
= minfo
->sensor_revision_number
;
2437 for (i
= 0; i
< ARRAY_SIZE(smiapp_module_idents
); i
++) {
2438 if (smiapp_module_idents
[i
].manufacturer_id
2439 != minfo
->manufacturer_id
)
2441 if (smiapp_module_idents
[i
].model_id
!= minfo
->model_id
)
2443 if (smiapp_module_idents
[i
].flags
2444 & SMIAPP_MODULE_IDENT_FLAG_REV_LE
) {
2445 if (smiapp_module_idents
[i
].revision_number_major
2446 < minfo
->revision_number_major
)
2449 if (smiapp_module_idents
[i
].revision_number_major
2450 != minfo
->revision_number_major
)
2454 minfo
->name
= smiapp_module_idents
[i
].name
;
2455 minfo
->quirk
= smiapp_module_idents
[i
].quirk
;
2459 if (i
>= ARRAY_SIZE(smiapp_module_idents
))
2460 dev_warn(&client
->dev
,
2461 "no quirks for this module; let's hope it's fully compliant\n");
2463 dev_dbg(&client
->dev
, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2464 minfo
->name
, minfo
->manufacturer_id
, minfo
->model_id
,
2465 minfo
->revision_number_major
);
2467 strlcpy(subdev
->name
, sensor
->minfo
.name
, sizeof(subdev
->name
));
2472 static const struct v4l2_subdev_ops smiapp_ops
;
2473 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
;
2474 static const struct media_entity_operations smiapp_entity_ops
;
2476 static int smiapp_registered(struct v4l2_subdev
*subdev
)
2478 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2479 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2480 struct smiapp_pll
*pll
= &sensor
->pll
;
2481 struct smiapp_subdev
*last
= NULL
;
2486 sensor
->vana
= devm_regulator_get(&client
->dev
, "vana");
2487 if (IS_ERR(sensor
->vana
)) {
2488 dev_err(&client
->dev
, "could not get regulator for vana\n");
2489 return PTR_ERR(sensor
->vana
);
2492 if (!sensor
->platform_data
->set_xclk
) {
2493 sensor
->ext_clk
= devm_clk_get(&client
->dev
, "ext_clk");
2494 if (IS_ERR(sensor
->ext_clk
)) {
2495 dev_err(&client
->dev
, "could not get clock\n");
2496 return PTR_ERR(sensor
->ext_clk
);
2499 rval
= clk_set_rate(sensor
->ext_clk
,
2500 sensor
->platform_data
->ext_clk
);
2502 dev_err(&client
->dev
,
2503 "unable to set clock freq to %u\n",
2504 sensor
->platform_data
->ext_clk
);
2509 if (gpio_is_valid(sensor
->platform_data
->xshutdown
)) {
2510 rval
= devm_gpio_request_one(
2511 &client
->dev
, sensor
->platform_data
->xshutdown
, 0,
2512 "SMIA++ xshutdown");
2514 dev_err(&client
->dev
,
2515 "unable to acquire reset gpio %d\n",
2516 sensor
->platform_data
->xshutdown
);
2521 rval
= smiapp_power_on(sensor
);
2525 rval
= smiapp_identify_module(subdev
);
2531 rval
= smiapp_get_all_limits(sensor
);
2538 * Handle Sensor Module orientation on the board.
2540 * The application of H-FLIP and V-FLIP on the sensor is modified by
2541 * the sensor orientation on the board.
2543 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2544 * both H-FLIP and V-FLIP for normal operation which also implies
2545 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2546 * controls will need to be internally inverted.
2548 * Rotation also changes the bayer pattern.
2550 if (sensor
->platform_data
->module_board_orient
==
2551 SMIAPP_MODULE_BOARD_ORIENT_180
)
2552 sensor
->hvflip_inv_mask
= SMIAPP_IMAGE_ORIENTATION_HFLIP
|
2553 SMIAPP_IMAGE_ORIENTATION_VFLIP
;
2555 rval
= smiapp_call_quirk(sensor
, limits
);
2557 dev_err(&client
->dev
, "limits quirks failed\n");
2561 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
]) {
2564 rval
= smiapp_read(sensor
,
2565 SMIAPP_REG_U8_BINNING_SUBTYPES
, &val
);
2570 sensor
->nbinning_subtypes
= min_t(u8
, val
,
2571 SMIAPP_BINNING_SUBTYPES
);
2573 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
2575 sensor
, SMIAPP_REG_U8_BINNING_TYPE_n(i
), &val
);
2580 sensor
->binning_subtypes
[i
] =
2581 *(struct smiapp_binning_subtype
*)&val
;
2583 dev_dbg(&client
->dev
, "binning %xx%x\n",
2584 sensor
->binning_subtypes
[i
].horizontal
,
2585 sensor
->binning_subtypes
[i
].vertical
);
2588 sensor
->binning_horizontal
= 1;
2589 sensor
->binning_vertical
= 1;
2591 if (device_create_file(&client
->dev
, &dev_attr_ident
) != 0) {
2592 dev_err(&client
->dev
, "sysfs ident entry creation failed\n");
2596 /* SMIA++ NVM initialization - it will be read from the sensor
2597 * when it is first requested by userspace.
2599 if (sensor
->minfo
.smiapp_version
&& sensor
->platform_data
->nvm_size
) {
2600 sensor
->nvm
= devm_kzalloc(&client
->dev
,
2601 sensor
->platform_data
->nvm_size
, GFP_KERNEL
);
2602 if (sensor
->nvm
== NULL
) {
2603 dev_err(&client
->dev
, "nvm buf allocation failed\n");
2605 goto out_ident_release
;
2608 if (device_create_file(&client
->dev
, &dev_attr_nvm
) != 0) {
2609 dev_err(&client
->dev
, "sysfs nvm entry failed\n");
2611 goto out_ident_release
;
2615 /* We consider this as profile 0 sensor if any of these are zero. */
2616 if (!sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV
] ||
2617 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV
] ||
2618 !sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV
] ||
2619 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV
]) {
2620 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_0
;
2621 } else if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2622 != SMIAPP_SCALING_CAPABILITY_NONE
) {
2623 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2624 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2625 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_1
;
2627 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_2
;
2628 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
2629 sensor
->ssds_used
++;
2630 } else if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
2631 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
2632 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
2633 sensor
->ssds_used
++;
2635 sensor
->binner
= &sensor
->ssds
[sensor
->ssds_used
];
2636 sensor
->ssds_used
++;
2637 sensor
->pixel_array
= &sensor
->ssds
[sensor
->ssds_used
];
2638 sensor
->ssds_used
++;
2640 sensor
->scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
2642 /* prepare PLL configuration input values */
2643 pll
->bus_type
= SMIAPP_PLL_BUS_TYPE_CSI2
;
2644 pll
->csi2
.lanes
= sensor
->platform_data
->lanes
;
2645 pll
->ext_clk_freq_hz
= sensor
->platform_data
->ext_clk
;
2646 pll
->flags
= smiapp_call_quirk(sensor
, pll_flags
);
2647 pll
->scale_n
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
2648 /* Profile 0 sensors have no separate OP clock branch. */
2649 if (sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
2650 pll
->flags
|= SMIAPP_PLL_FLAG_NO_OP_CLOCKS
;
2652 rval
= smiapp_get_mbus_formats(sensor
);
2655 goto out_nvm_release
;
2658 for (i
= 0; i
< SMIAPP_SUBDEVS
; i
++) {
2660 struct smiapp_subdev
*ssd
;
2662 } const __this
[] = {
2663 { sensor
->scaler
, "scaler", },
2664 { sensor
->binner
, "binner", },
2665 { sensor
->pixel_array
, "pixel array", },
2666 }, *_this
= &__this
[i
];
2667 struct smiapp_subdev
*this = _this
->ssd
;
2672 if (this != sensor
->src
)
2673 v4l2_subdev_init(&this->sd
, &smiapp_ops
);
2675 this->sensor
= sensor
;
2677 if (this == sensor
->pixel_array
) {
2681 this->source_pad
= 1;
2684 snprintf(this->sd
.name
,
2685 sizeof(this->sd
.name
), "%s %s %d-%4.4x",
2686 sensor
->minfo
.name
, _this
->name
,
2687 i2c_adapter_id(client
->adapter
), client
->addr
);
2689 this->sink_fmt
.width
=
2690 sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2691 this->sink_fmt
.height
=
2692 sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2693 this->compose
.width
= this->sink_fmt
.width
;
2694 this->compose
.height
= this->sink_fmt
.height
;
2695 this->crop
[this->source_pad
] = this->compose
;
2696 this->pads
[this->source_pad
].flags
= MEDIA_PAD_FL_SOURCE
;
2697 if (this != sensor
->pixel_array
) {
2698 this->crop
[this->sink_pad
] = this->compose
;
2699 this->pads
[this->sink_pad
].flags
= MEDIA_PAD_FL_SINK
;
2702 this->sd
.entity
.ops
= &smiapp_entity_ops
;
2709 this->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2710 this->sd
.internal_ops
= &smiapp_internal_ops
;
2711 this->sd
.owner
= THIS_MODULE
;
2712 v4l2_set_subdevdata(&this->sd
, client
);
2714 rval
= media_entity_init(&this->sd
.entity
,
2715 this->npads
, this->pads
, 0);
2717 dev_err(&client
->dev
,
2718 "media_entity_init failed\n");
2719 goto out_nvm_release
;
2722 rval
= media_entity_create_link(&this->sd
.entity
,
2726 MEDIA_LNK_FL_ENABLED
|
2727 MEDIA_LNK_FL_IMMUTABLE
);
2729 dev_err(&client
->dev
,
2730 "media_entity_create_link failed\n");
2731 goto out_nvm_release
;
2734 rval
= v4l2_device_register_subdev(sensor
->src
->sd
.v4l2_dev
,
2737 dev_err(&client
->dev
,
2738 "v4l2_device_register_subdev failed\n");
2739 goto out_nvm_release
;
2745 dev_dbg(&client
->dev
, "profile %d\n", sensor
->minfo
.smiapp_profile
);
2747 sensor
->pixel_array
->sd
.entity
.type
= MEDIA_ENT_T_V4L2_SUBDEV_SENSOR
;
2750 smiapp_read_frame_fmt(sensor
);
2751 rval
= smiapp_init_controls(sensor
);
2753 goto out_nvm_release
;
2755 mutex_lock(&sensor
->mutex
);
2756 rval
= smiapp_update_mode(sensor
);
2757 mutex_unlock(&sensor
->mutex
);
2759 dev_err(&client
->dev
, "update mode failed\n");
2760 goto out_nvm_release
;
2763 sensor
->streaming
= false;
2764 sensor
->dev_init_done
= true;
2766 /* check flash capability */
2767 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FLASH_MODE_CAPABILITY
, &tmp
);
2768 sensor
->flash_capability
= tmp
;
2770 goto out_nvm_release
;
2772 smiapp_power_off(sensor
);
2777 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2780 device_remove_file(&client
->dev
, &dev_attr_ident
);
2783 smiapp_power_off(sensor
);
2787 static int smiapp_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2789 struct smiapp_subdev
*ssd
= to_smiapp_subdev(sd
);
2790 struct smiapp_sensor
*sensor
= ssd
->sensor
;
2792 smiapp_csi_data_formats
[smiapp_pixel_order(sensor
)].code
;
2795 mutex_lock(&sensor
->mutex
);
2797 for (i
= 0; i
< ssd
->npads
; i
++) {
2798 struct v4l2_mbus_framefmt
*try_fmt
=
2799 v4l2_subdev_get_try_format(fh
, i
);
2800 struct v4l2_rect
*try_crop
= v4l2_subdev_get_try_crop(fh
, i
);
2801 struct v4l2_rect
*try_comp
;
2803 try_fmt
->width
= sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2804 try_fmt
->height
= sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2805 try_fmt
->code
= mbus_code
;
2806 try_fmt
->field
= V4L2_FIELD_NONE
;
2810 try_crop
->width
= try_fmt
->width
;
2811 try_crop
->height
= try_fmt
->height
;
2813 if (ssd
!= sensor
->pixel_array
)
2816 try_comp
= v4l2_subdev_get_try_compose(fh
, i
);
2817 *try_comp
= *try_crop
;
2820 mutex_unlock(&sensor
->mutex
);
2822 return smiapp_set_power(sd
, 1);
2825 static int smiapp_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2827 return smiapp_set_power(sd
, 0);
2830 static const struct v4l2_subdev_video_ops smiapp_video_ops
= {
2831 .s_stream
= smiapp_set_stream
,
2834 static const struct v4l2_subdev_core_ops smiapp_core_ops
= {
2835 .s_power
= smiapp_set_power
,
2838 static const struct v4l2_subdev_pad_ops smiapp_pad_ops
= {
2839 .enum_mbus_code
= smiapp_enum_mbus_code
,
2840 .get_fmt
= smiapp_get_format
,
2841 .set_fmt
= smiapp_set_format
,
2842 .get_selection
= smiapp_get_selection
,
2843 .set_selection
= smiapp_set_selection
,
2846 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops
= {
2847 .g_skip_frames
= smiapp_get_skip_frames
,
2850 static const struct v4l2_subdev_ops smiapp_ops
= {
2851 .core
= &smiapp_core_ops
,
2852 .video
= &smiapp_video_ops
,
2853 .pad
= &smiapp_pad_ops
,
2854 .sensor
= &smiapp_sensor_ops
,
2857 static const struct media_entity_operations smiapp_entity_ops
= {
2858 .link_validate
= v4l2_subdev_link_validate
,
2861 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops
= {
2862 .registered
= smiapp_registered
,
2863 .open
= smiapp_open
,
2864 .close
= smiapp_close
,
2867 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
= {
2868 .open
= smiapp_open
,
2869 .close
= smiapp_close
,
2872 /* -----------------------------------------------------------------------------
2878 static int smiapp_suspend(struct device
*dev
)
2880 struct i2c_client
*client
= to_i2c_client(dev
);
2881 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2882 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2885 BUG_ON(mutex_is_locked(&sensor
->mutex
));
2887 if (sensor
->power_count
== 0)
2890 if (sensor
->streaming
)
2891 smiapp_stop_streaming(sensor
);
2893 streaming
= sensor
->streaming
;
2895 smiapp_power_off(sensor
);
2897 /* save state for resume */
2898 sensor
->streaming
= streaming
;
2903 static int smiapp_resume(struct device
*dev
)
2905 struct i2c_client
*client
= to_i2c_client(dev
);
2906 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2907 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2910 if (sensor
->power_count
== 0)
2913 rval
= smiapp_power_on(sensor
);
2917 if (sensor
->streaming
)
2918 rval
= smiapp_start_streaming(sensor
);
2925 #define smiapp_suspend NULL
2926 #define smiapp_resume NULL
2928 #endif /* CONFIG_PM */
2930 static int smiapp_probe(struct i2c_client
*client
,
2931 const struct i2c_device_id
*devid
)
2933 struct smiapp_sensor
*sensor
;
2935 if (client
->dev
.platform_data
== NULL
)
2938 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
2942 sensor
->platform_data
= client
->dev
.platform_data
;
2943 mutex_init(&sensor
->mutex
);
2944 mutex_init(&sensor
->power_mutex
);
2945 sensor
->src
= &sensor
->ssds
[sensor
->ssds_used
];
2947 v4l2_i2c_subdev_init(&sensor
->src
->sd
, client
, &smiapp_ops
);
2948 sensor
->src
->sd
.internal_ops
= &smiapp_internal_src_ops
;
2949 sensor
->src
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2950 sensor
->src
->sensor
= sensor
;
2952 sensor
->src
->pads
[0].flags
= MEDIA_PAD_FL_SOURCE
;
2953 return media_entity_init(&sensor
->src
->sd
.entity
, 2,
2954 sensor
->src
->pads
, 0);
2957 static int smiapp_remove(struct i2c_client
*client
)
2959 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2960 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2963 if (sensor
->power_count
) {
2964 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
2965 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
2966 if (sensor
->platform_data
->set_xclk
)
2967 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
2969 clk_disable_unprepare(sensor
->ext_clk
);
2970 sensor
->power_count
= 0;
2973 device_remove_file(&client
->dev
, &dev_attr_ident
);
2975 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2977 for (i
= 0; i
< sensor
->ssds_used
; i
++) {
2978 v4l2_device_unregister_subdev(&sensor
->ssds
[i
].sd
);
2979 media_entity_cleanup(&sensor
->ssds
[i
].sd
.entity
);
2981 smiapp_free_controls(sensor
);
2986 static const struct i2c_device_id smiapp_id_table
[] = {
2990 MODULE_DEVICE_TABLE(i2c
, smiapp_id_table
);
2992 static const struct dev_pm_ops smiapp_pm_ops
= {
2993 .suspend
= smiapp_suspend
,
2994 .resume
= smiapp_resume
,
2997 static struct i2c_driver smiapp_i2c_driver
= {
2999 .name
= SMIAPP_NAME
,
3000 .pm
= &smiapp_pm_ops
,
3002 .probe
= smiapp_probe
,
3003 .remove
= smiapp_remove
,
3004 .id_table
= smiapp_id_table
,
3007 module_i2c_driver(smiapp_i2c_driver
);
3009 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3010 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3011 MODULE_LICENSE("GPL");