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.
23 #include <linux/clk.h>
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>
29 #include <linux/slab.h>
30 #include <linux/smiapp.h>
31 #include <linux/v4l2-mediabus.h>
32 #include <media/v4l2-device.h>
36 #define SMIAPP_ALIGN_DIM(dim, flags) \
37 ((flags) & V4L2_SEL_FLAG_GE \
42 * smiapp_module_idents - supported camera modules
44 static 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
),
60 * Dynamic Capability Identification
64 static int smiapp_read_frame_fmt(struct smiapp_sensor
*sensor
)
66 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
67 u32 fmt_model_type
, fmt_model_subtype
, ncol_desc
, nrow_desc
;
71 int embedded_start
= -1, embedded_end
= -1;
74 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE
,
79 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE
,
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
;
90 dev_dbg(&client
->dev
, "format_model_type %s\n",
91 fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
93 fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
94 ? "4 byte" : "is simply bad");
96 for (i
= 0; i
< ncol_desc
+ nrow_desc
; i
++) {
103 if (fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
) {
106 SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i
),
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
) {
120 SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i
),
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
;
131 dev_dbg(&client
->dev
,
132 "invalid frame format model type %d\n",
143 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED
:
146 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY
:
149 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK
:
152 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK
:
155 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
:
160 dev_dbg(&client
->dev
, "pixelcode %d\n", pixelcode
);
164 dev_dbg(&client
->dev
, "%s pixels: %d %s\n",
165 what
, pixels
, which
);
170 /* Handle row descriptors */
172 == SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED
) {
173 embedded_start
= line_count
;
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
;
181 line_count
+= pixels
;
184 if (embedded_start
== -1 || embedded_end
== -1) {
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
);
196 static int smiapp_pll_configure(struct smiapp_sensor
*sensor
)
198 struct smiapp_pll
*pll
= &sensor
->pll
;
202 sensor
, SMIAPP_REG_U16_VT_PIX_CLK_DIV
, pll
->vt
.pix_clk_div
);
207 sensor
, SMIAPP_REG_U16_VT_SYS_CLK_DIV
, pll
->vt
.sys_clk_div
);
212 sensor
, SMIAPP_REG_U16_PRE_PLL_CLK_DIV
, pll
->pre_pll_clk_div
);
217 sensor
, SMIAPP_REG_U16_PLL_MULTIPLIER
, pll
->pll_multiplier
);
221 /* Lane op clock ratio does not apply here. */
223 sensor
, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS
,
224 DIV_ROUND_UP(pll
->op
.sys_clk_freq_hz
, 1000000 / 256 / 256));
225 if (rval
< 0 || sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
229 sensor
, SMIAPP_REG_U16_OP_PIX_CLK_DIV
, pll
->op
.pix_clk_div
);
234 sensor
, SMIAPP_REG_U16_OP_SYS_CLK_DIV
, pll
->op
.sys_clk_div
);
237 static int smiapp_pll_try(struct smiapp_sensor
*sensor
,
238 struct smiapp_pll
*pll
)
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
],
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
],
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
],
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
],
273 return smiapp_pll_calculate(&client
->dev
, &lim
, pll
);
276 static int smiapp_pll_update(struct smiapp_sensor
*sensor
)
278 struct smiapp_pll
*pll
= &sensor
->pll
;
281 pll
->binning_horizontal
= sensor
->binning_horizontal
;
282 pll
->binning_vertical
= sensor
->binning_vertical
;
284 sensor
->link_freq
->qmenu_int
[sensor
->link_freq
->val
];
285 pll
->scale_m
= sensor
->scale_m
;
286 pll
->bits_per_pixel
= sensor
->csi_format
->compressed
;
288 rval
= smiapp_pll_try(sensor
, pll
);
292 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_parray
,
293 pll
->pixel_rate_pixel_array
);
294 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_csi
, pll
->pixel_rate_csi
);
302 * V4L2 Controls handling
306 static void __smiapp_update_exposure_limits(struct smiapp_sensor
*sensor
)
308 struct v4l2_ctrl
*ctrl
= sensor
->exposure
;
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
];
315 __v4l2_ctrl_modify_range(ctrl
, ctrl
->minimum
, max
, ctrl
->step
, max
);
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.
326 static const struct smiapp_csi_data_format smiapp_csi_data_formats
[] = {
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
, },
345 const char *pixel_order_str
[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
347 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
348 - (unsigned long)smiapp_csi_data_formats) \
349 / sizeof(*smiapp_csi_data_formats))
351 static u32
smiapp_pixel_order(struct smiapp_sensor
*sensor
)
353 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
357 if (sensor
->hflip
->val
)
358 flip
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
360 if (sensor
->vflip
->val
)
361 flip
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
364 flip
^= sensor
->hvflip_inv_mask
;
366 dev_dbg(&client
->dev
, "flip %d\n", flip
);
367 return sensor
->default_pixel_order
^ flip
;
370 static void smiapp_update_mbus_formats(struct smiapp_sensor
*sensor
)
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
);
379 sensor
->mbus_frame_fmts
=
380 sensor
->default_mbus_frame_fmts
<< pixel_order
;
382 &smiapp_csi_data_formats
[csi_format_idx
+ pixel_order
];
383 sensor
->internal_csi_format
=
384 &smiapp_csi_data_formats
[internal_csi_format_idx
387 BUG_ON(max(internal_csi_format_idx
, csi_format_idx
) + pixel_order
388 >= ARRAY_SIZE(smiapp_csi_data_formats
));
390 dev_dbg(&client
->dev
, "new pixel order %s\n",
391 pixel_order_str
[pixel_order
]);
394 static const char * const smiapp_test_patterns
[] = {
397 "Eight Vertical Colour Bars",
398 "Colour Bars With Fade to Grey",
399 "Pseudorandom Sequence (PN9)",
402 static int smiapp_set_ctrl(struct v4l2_ctrl
*ctrl
)
404 struct smiapp_sensor
*sensor
=
405 container_of(ctrl
->handler
, struct smiapp_subdev
, ctrl_handler
)
412 case V4L2_CID_ANALOGUE_GAIN
:
415 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL
, ctrl
->val
);
417 case V4L2_CID_EXPOSURE
:
420 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME
, ctrl
->val
);
424 if (sensor
->streaming
)
427 if (sensor
->hflip
->val
)
428 orient
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
430 if (sensor
->vflip
->val
)
431 orient
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
433 orient
^= sensor
->hvflip_inv_mask
;
434 rval
= smiapp_write(sensor
,
435 SMIAPP_REG_U8_IMAGE_ORIENTATION
,
440 smiapp_update_mbus_formats(sensor
);
444 case V4L2_CID_VBLANK
:
445 exposure
= sensor
->exposure
->val
;
447 __smiapp_update_exposure_limits(sensor
);
449 if (exposure
> sensor
->exposure
->maximum
) {
450 sensor
->exposure
->val
=
451 sensor
->exposure
->maximum
;
452 rval
= smiapp_set_ctrl(
459 sensor
, SMIAPP_REG_U16_FRAME_LENGTH_LINES
,
460 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
463 case V4L2_CID_HBLANK
:
465 sensor
, SMIAPP_REG_U16_LINE_LENGTH_PCK
,
466 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
469 case V4L2_CID_LINK_FREQ
:
470 if (sensor
->streaming
)
473 return smiapp_pll_update(sensor
);
475 case V4L2_CID_TEST_PATTERN
: {
478 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
480 sensor
->test_data
[i
],
482 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR
);
485 sensor
, SMIAPP_REG_U16_TEST_PATTERN_MODE
, ctrl
->val
);
488 case V4L2_CID_TEST_PATTERN_RED
:
490 sensor
, SMIAPP_REG_U16_TEST_DATA_RED
, ctrl
->val
);
492 case V4L2_CID_TEST_PATTERN_GREENR
:
494 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENR
, ctrl
->val
);
496 case V4L2_CID_TEST_PATTERN_BLUE
:
498 sensor
, SMIAPP_REG_U16_TEST_DATA_BLUE
, ctrl
->val
);
500 case V4L2_CID_TEST_PATTERN_GREENB
:
502 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENB
, ctrl
->val
);
504 case V4L2_CID_PIXEL_RATE
:
505 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
513 static const struct v4l2_ctrl_ops smiapp_ctrl_ops
= {
514 .s_ctrl
= smiapp_set_ctrl
,
517 static int smiapp_init_controls(struct smiapp_sensor
*sensor
)
519 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
520 unsigned long *valid_link_freqs
= &sensor
->valid_link_freqs
[
521 sensor
->csi_format
->compressed
- SMIAPP_COMPRESSED_BASE
];
525 rval
= v4l2_ctrl_handler_init(&sensor
->pixel_array
->ctrl_handler
, 12);
528 sensor
->pixel_array
->ctrl_handler
.lock
= &sensor
->mutex
;
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
]);
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);
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);
550 sensor
->vblank
= v4l2_ctrl_new_std(
551 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
552 V4L2_CID_VBLANK
, 0, 1, 1, 0);
555 sensor
->vblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
557 sensor
->hblank
= v4l2_ctrl_new_std(
558 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
559 V4L2_CID_HBLANK
, 0, 1, 1, 0);
562 sensor
->hblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
564 sensor
->pixel_rate_parray
= v4l2_ctrl_new_std(
565 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
566 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
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
);
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
] =
577 &sensor
->pixel_array
->ctrl_handler
,
578 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN_RED
+ i
,
579 0, max_value
, 1, max_value
);
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
;
590 sensor
->pixel_array
->sd
.ctrl_handler
=
591 &sensor
->pixel_array
->ctrl_handler
;
593 v4l2_ctrl_cluster(2, &sensor
->hflip
);
595 rval
= v4l2_ctrl_handler_init(&sensor
->src
->ctrl_handler
, 0);
598 sensor
->src
->ctrl_handler
.lock
= &sensor
->mutex
;
600 for (max
= 0; sensor
->platform_data
->op_sys_clock
[max
+ 1]; max
++);
602 sensor
->link_freq
= v4l2_ctrl_new_int_menu(
603 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
604 V4L2_CID_LINK_FREQ
, __fls(*valid_link_freqs
),
605 __ffs(*valid_link_freqs
), sensor
->platform_data
->op_sys_clock
);
607 sensor
->pixel_rate_csi
= v4l2_ctrl_new_std(
608 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
609 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
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
;
619 sensor
->src
->sd
.ctrl_handler
=
620 &sensor
->src
->ctrl_handler
;
625 v4l2_ctrl_handler_free(&sensor
->pixel_array
->ctrl_handler
);
626 v4l2_ctrl_handler_free(&sensor
->src
->ctrl_handler
);
631 static void smiapp_free_controls(struct smiapp_sensor
*sensor
)
635 for (i
= 0; i
< sensor
->ssds_used
; i
++)
636 v4l2_ctrl_handler_free(&sensor
->ssds
[i
].ctrl_handler
);
639 static int smiapp_get_limits(struct smiapp_sensor
*sensor
, int const *limit
,
642 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
647 for (i
= 0; i
< n
; i
++) {
649 sensor
, smiapp_reg_limits
[limit
[i
]].addr
, &val
);
652 sensor
->limits
[limit
[i
]] = val
;
653 dev_dbg(&client
->dev
, "0x%8.8x \"%s\" = %u, 0x%x\n",
654 smiapp_reg_limits
[limit
[i
]].addr
,
655 smiapp_reg_limits
[limit
[i
]].what
, val
, val
);
661 static int smiapp_get_all_limits(struct smiapp_sensor
*sensor
)
666 for (i
= 0; i
< SMIAPP_LIMIT_LAST
; i
++) {
667 rval
= smiapp_get_limits(sensor
, &i
, 1);
672 if (sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] == 0)
673 smiapp_replace_limit(sensor
, SMIAPP_LIMIT_SCALER_N_MIN
, 16);
678 static int smiapp_get_limits_binning(struct smiapp_sensor
*sensor
)
680 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
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
,
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
,
702 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
] ==
703 SMIAPP_BINNING_CAPABILITY_NO
) {
704 for (i
= 0; i
< ARRAY_SIZE(limits
); i
++)
705 sensor
->limits
[limits
[i
]] =
706 sensor
->limits
[limits_replace
[i
]];
711 rval
= smiapp_get_limits(sensor
, limits
, ARRAY_SIZE(limits
));
716 * Sanity check whether the binning limits are valid. If not,
717 * use the non-binning ones.
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
])
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
]];
738 static int smiapp_get_mbus_formats(struct smiapp_sensor
*sensor
)
740 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
741 struct smiapp_pll
*pll
= &sensor
->pll
;
742 unsigned int type
, n
;
743 unsigned int i
, pixel_order
;
747 sensor
, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE
, &type
);
751 dev_dbg(&client
->dev
, "data_format_model_type %d\n", type
);
753 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_PIXEL_ORDER
,
758 if (pixel_order
>= ARRAY_SIZE(pixel_order_str
)) {
759 dev_dbg(&client
->dev
, "bad pixel order %d\n", pixel_order
);
763 dev_dbg(&client
->dev
, "pixel order %d (%s)\n", pixel_order
,
764 pixel_order_str
[pixel_order
]);
767 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL
:
768 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N
;
770 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED
:
771 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N
;
777 sensor
->default_pixel_order
= pixel_order
;
778 sensor
->mbus_frame_fmts
= 0;
780 for (i
= 0; i
< n
; i
++) {
785 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i
), &fmt
);
789 dev_dbg(&client
->dev
, "%u: bpp %u, compressed %u\n",
790 i
, fmt
>> 8, (u8
)fmt
);
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
];
796 if (f
->pixel_order
!= SMIAPP_PIXEL_ORDER_GRBG
)
799 if (f
->width
!= fmt
>> 8 || f
->compressed
!= (u8
)fmt
)
802 dev_dbg(&client
->dev
, "jolly good! %d\n", j
);
804 sensor
->default_mbus_frame_fmts
|= 1 << j
;
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
;
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
];
821 BUG_ON(f
->compressed
< SMIAPP_COMPRESSED_BASE
);
822 BUG_ON(f
->compressed
> SMIAPP_COMPRESSED_MAX
);
824 if (!(sensor
->default_mbus_frame_fmts
& 1 << i
))
827 pll
->bits_per_pixel
= f
->compressed
;
829 for (j
= 0; sensor
->platform_data
->op_sys_clock
[j
]; j
++) {
830 pll
->link_freq
= sensor
->platform_data
->op_sys_clock
[j
];
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");
839 set_bit(j
, valid_link_freqs
);
842 if (!*valid_link_freqs
) {
843 dev_info(&client
->dev
,
844 "no valid link frequencies for %u bpp\n",
846 sensor
->default_mbus_frame_fmts
&= ~BIT(i
);
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
;
859 if (!sensor
->csi_format
) {
860 dev_err(&client
->dev
, "no supported mbus code found\n");
864 smiapp_update_mbus_formats(sensor
);
869 static void smiapp_update_blanking(struct smiapp_sensor
*sensor
)
871 struct v4l2_ctrl
*vblank
= sensor
->vblank
;
872 struct v4l2_ctrl
*hblank
= sensor
->hblank
;
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
] -
880 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
;
882 __v4l2_ctrl_modify_range(vblank
, min
, max
, vblank
->step
, min
);
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
] -
889 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
;
891 __v4l2_ctrl_modify_range(hblank
, min
, max
, hblank
->step
, min
);
893 __smiapp_update_exposure_limits(sensor
);
896 static int smiapp_update_mode(struct smiapp_sensor
*sensor
)
898 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
899 unsigned int binning_mode
;
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
);
908 /* Binning has to be set up here; it affects limits */
909 if (sensor
->binning_horizontal
== 1 &&
910 sensor
->binning_vertical
== 1) {
914 (sensor
->binning_horizontal
<< 4)
915 | sensor
->binning_vertical
;
918 sensor
, SMIAPP_REG_U8_BINNING_TYPE
, binning_type
);
924 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_BINNING_MODE
, binning_mode
);
928 /* Get updated limits due to binning */
929 rval
= smiapp_get_limits_binning(sensor
);
933 rval
= smiapp_pll_update(sensor
);
937 /* Output from pixel array, including blanking */
938 smiapp_update_blanking(sensor
);
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
);
943 dev_dbg(&client
->dev
, "real timeperframe\t100/%d\n",
944 sensor
->pll
.pixel_rate_pixel_array
/
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));
955 * SMIA++ NVM handling
958 static int smiapp_read_nvm(struct smiapp_sensor
*sensor
,
964 np
= sensor
->nvm_size
/ SMIAPP_NVM_PAGE_SIZE
;
965 for (p
= 0; p
< np
; p
++) {
968 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT
, p
);
972 rval
= smiapp_write(sensor
,
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
);
979 for (i
= 0; i
< 1000; i
++) {
982 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS
, &s
);
987 if (s
& SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY
)
997 for (i
= 0; i
< SMIAPP_NVM_PAGE_SIZE
; i
++) {
1000 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0
+ i
,
1010 rval2
= smiapp_write(sensor
, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
, 0);
1019 * SMIA++ CCI address control
1022 static int smiapp_change_cci_addr(struct smiapp_sensor
*sensor
)
1024 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1028 client
->addr
= sensor
->platform_data
->i2c_addr_dfl
;
1030 rval
= smiapp_write(sensor
,
1031 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
,
1032 sensor
->platform_data
->i2c_addr_alt
<< 1);
1036 client
->addr
= sensor
->platform_data
->i2c_addr_alt
;
1038 /* verify addr change went ok */
1039 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
, &val
);
1043 if (val
!= sensor
->platform_data
->i2c_addr_alt
<< 1)
1051 * SMIA++ Mode Control
1054 static int smiapp_setup_flash_strobe(struct smiapp_sensor
*sensor
)
1056 struct smiapp_flash_strobe_parms
*strobe_setup
;
1057 unsigned int ext_freq
= sensor
->platform_data
->ext_clk
;
1059 u32 strobe_adjustment
;
1060 u32 strobe_width_high_rs
;
1063 strobe_setup
= sensor
->platform_data
->strobe_setup
;
1066 * How to calculate registers related to strobe length. Please
1067 * do not change, or if you do at least know what you're
1070 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1072 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1073 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1075 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1076 * flash_strobe_adjustment E N, [1 - 0xff]
1078 * The formula above is written as below to keep it on one
1081 * l / 10^6 = w / e * a
1083 * Let's mark w * a by x:
1091 * The strobe width must be at least as long as requested,
1092 * thus rounding upwards is needed.
1094 * x = (l * e + 10^6 - 1) / 10^6
1095 * -----------------------------
1097 * Maximum possible accuracy is wanted at all times. Thus keep
1098 * a as small as possible.
1100 * Calculate a, assuming maximum w, with rounding upwards:
1102 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1103 * -------------------------------------
1105 * Thus, we also get w, with that a, with rounding upwards:
1107 * w = (x + a - 1) / a
1108 * -------------------
1112 * x E [1, (2^16 - 1) * (2^8 - 1)]
1114 * Substituting maximum x to the original formula (with rounding),
1115 * the maximum l is thus
1117 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1119 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1120 * --------------------------------------------------
1122 * flash_strobe_length must be clamped between 1 and
1123 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1127 * flash_strobe_adjustment = ((flash_strobe_length *
1128 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1130 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1131 * EXTCLK freq + 10^6 - 1) / 10^6 +
1132 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
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
);
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) /
1145 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_MODE_RS
,
1146 strobe_setup
->mode
);
1150 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT
,
1155 rval
= smiapp_write(
1156 sensor
, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL
,
1157 strobe_width_high_rs
);
1161 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL
,
1162 strobe_setup
->strobe_delay
);
1166 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_FLASH_STROBE_START_POINT
,
1167 strobe_setup
->stobe_start_point
);
1171 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_TRIGGER_RS
,
1172 strobe_setup
->trigger
);
1175 sensor
->platform_data
->strobe_setup
->trigger
= 0;
1180 /* -----------------------------------------------------------------------------
1184 static int smiapp_power_on(struct smiapp_sensor
*sensor
)
1186 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1190 rval
= regulator_enable(sensor
->vana
);
1192 dev_err(&client
->dev
, "failed to enable vana regulator\n");
1195 usleep_range(1000, 1000);
1197 if (sensor
->platform_data
->set_xclk
)
1198 rval
= sensor
->platform_data
->set_xclk(
1199 &sensor
->src
->sd
, sensor
->platform_data
->ext_clk
);
1201 rval
= clk_prepare_enable(sensor
->ext_clk
);
1203 dev_dbg(&client
->dev
, "failed to enable xclk\n");
1206 usleep_range(1000, 1000);
1208 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1209 gpio_set_value(sensor
->platform_data
->xshutdown
, 1);
1211 sleep
= SMIAPP_RESET_DELAY(sensor
->platform_data
->ext_clk
);
1212 usleep_range(sleep
, sleep
);
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
1225 if (sensor
->platform_data
->i2c_addr_alt
) {
1226 rval
= smiapp_change_cci_addr(sensor
);
1228 dev_err(&client
->dev
, "cci address change error\n");
1229 goto out_cci_addr_fail
;
1233 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_SOFTWARE_RESET
,
1234 SMIAPP_SOFTWARE_RESET
);
1236 dev_err(&client
->dev
, "software reset failed\n");
1237 goto out_cci_addr_fail
;
1240 if (sensor
->platform_data
->i2c_addr_alt
) {
1241 rval
= smiapp_change_cci_addr(sensor
);
1243 dev_err(&client
->dev
, "cci address change error\n");
1244 goto out_cci_addr_fail
;
1248 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_COMPRESSION_MODE
,
1249 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR
);
1251 dev_err(&client
->dev
, "compression mode set failed\n");
1252 goto out_cci_addr_fail
;
1255 rval
= smiapp_write(
1256 sensor
, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ
,
1257 sensor
->platform_data
->ext_clk
/ (1000000 / (1 << 8)));
1259 dev_err(&client
->dev
, "extclk frequency set failed\n");
1260 goto out_cci_addr_fail
;
1263 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_LANE_MODE
,
1264 sensor
->platform_data
->lanes
- 1);
1266 dev_err(&client
->dev
, "csi lane mode set failed\n");
1267 goto out_cci_addr_fail
;
1270 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FAST_STANDBY_CTRL
,
1271 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE
);
1273 dev_err(&client
->dev
, "fast standby set failed\n");
1274 goto out_cci_addr_fail
;
1277 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_SIGNALLING_MODE
,
1278 sensor
->platform_data
->csi_signalling_mode
);
1280 dev_err(&client
->dev
, "csi signalling mode set failed\n");
1281 goto out_cci_addr_fail
;
1284 /* DPHY control done by sensor based on requested link rate */
1285 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_DPHY_CTRL
,
1286 SMIAPP_DPHY_CTRL_UI
);
1290 rval
= smiapp_call_quirk(sensor
, post_poweron
);
1292 dev_err(&client
->dev
, "post_poweron quirks failed\n");
1293 goto out_cci_addr_fail
;
1296 /* Are we still initialising...? If yes, return here. */
1297 if (!sensor
->pixel_array
)
1300 rval
= v4l2_ctrl_handler_setup(
1301 &sensor
->pixel_array
->ctrl_handler
);
1303 goto out_cci_addr_fail
;
1305 rval
= v4l2_ctrl_handler_setup(&sensor
->src
->ctrl_handler
);
1307 goto out_cci_addr_fail
;
1309 mutex_lock(&sensor
->mutex
);
1310 rval
= smiapp_update_mode(sensor
);
1311 mutex_unlock(&sensor
->mutex
);
1313 goto out_cci_addr_fail
;
1318 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1319 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
1320 if (sensor
->platform_data
->set_xclk
)
1321 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
1323 clk_disable_unprepare(sensor
->ext_clk
);
1326 regulator_disable(sensor
->vana
);
1330 static void smiapp_power_off(struct smiapp_sensor
*sensor
)
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.
1339 if (sensor
->platform_data
->i2c_addr_alt
)
1340 smiapp_write(sensor
,
1341 SMIAPP_REG_U8_SOFTWARE_RESET
,
1342 SMIAPP_SOFTWARE_RESET
);
1344 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1345 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
1346 if (sensor
->platform_data
->set_xclk
)
1347 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
1349 clk_disable_unprepare(sensor
->ext_clk
);
1350 usleep_range(5000, 5000);
1351 regulator_disable(sensor
->vana
);
1352 sensor
->streaming
= false;
1355 static int smiapp_set_power(struct v4l2_subdev
*subdev
, int on
)
1357 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1360 mutex_lock(&sensor
->power_mutex
);
1362 if (on
&& !sensor
->power_count
) {
1363 /* Power on and perform initialisation. */
1364 ret
= smiapp_power_on(sensor
);
1367 } else if (!on
&& sensor
->power_count
== 1) {
1368 smiapp_power_off(sensor
);
1371 /* Update the power count. */
1372 sensor
->power_count
+= on
? 1 : -1;
1373 WARN_ON(sensor
->power_count
< 0);
1376 mutex_unlock(&sensor
->power_mutex
);
1380 /* -----------------------------------------------------------------------------
1381 * Video stream management
1384 static int smiapp_start_streaming(struct smiapp_sensor
*sensor
)
1386 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1389 mutex_lock(&sensor
->mutex
);
1391 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_CSI_DATA_FORMAT
,
1392 (sensor
->csi_format
->width
<< 8) |
1393 sensor
->csi_format
->compressed
);
1397 rval
= smiapp_pll_configure(sensor
);
1401 /* Analog crop start coordinates */
1402 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_ADDR_START
,
1403 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
);
1407 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_ADDR_START
,
1408 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
);
1412 /* Analog crop end coordinates */
1413 rval
= smiapp_write(
1414 sensor
, SMIAPP_REG_U16_X_ADDR_END
,
1415 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
1416 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
- 1);
1420 rval
= smiapp_write(
1421 sensor
, SMIAPP_REG_U16_Y_ADDR_END
,
1422 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
1423 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
- 1);
1428 * Output from pixel array, including blanking, is set using
1429 * controls below. No need to set here.
1433 if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
1434 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
1435 rval
= smiapp_write(
1436 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET
,
1437 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].left
);
1441 rval
= smiapp_write(
1442 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET
,
1443 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].top
);
1447 rval
= smiapp_write(
1448 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH
,
1449 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].width
);
1453 rval
= smiapp_write(
1454 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT
,
1455 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].height
);
1461 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
1462 != SMIAPP_SCALING_CAPABILITY_NONE
) {
1463 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALING_MODE
,
1464 sensor
->scaling_mode
);
1468 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALE_M
,
1474 /* Output size from sensor */
1475 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_OUTPUT_SIZE
,
1476 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
);
1479 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_OUTPUT_SIZE
,
1480 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
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
);
1494 rval
= smiapp_call_quirk(sensor
, pre_streamon
);
1496 dev_err(&client
->dev
, "pre_streamon quirks failed\n");
1500 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1501 SMIAPP_MODE_SELECT_STREAMING
);
1504 mutex_unlock(&sensor
->mutex
);
1509 static int smiapp_stop_streaming(struct smiapp_sensor
*sensor
)
1511 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1514 mutex_lock(&sensor
->mutex
);
1515 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1516 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY
);
1520 rval
= smiapp_call_quirk(sensor
, post_streamoff
);
1522 dev_err(&client
->dev
, "post_streamoff quirks failed\n");
1525 mutex_unlock(&sensor
->mutex
);
1529 /* -----------------------------------------------------------------------------
1530 * V4L2 subdev video operations
1533 static int smiapp_set_stream(struct v4l2_subdev
*subdev
, int enable
)
1535 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1538 if (sensor
->streaming
== enable
)
1542 sensor
->streaming
= true;
1543 rval
= smiapp_start_streaming(sensor
);
1545 sensor
->streaming
= false;
1547 rval
= smiapp_stop_streaming(sensor
);
1548 sensor
->streaming
= false;
1554 static int smiapp_enum_mbus_code(struct v4l2_subdev
*subdev
,
1555 struct v4l2_subdev_fh
*fh
,
1556 struct v4l2_subdev_mbus_code_enum
*code
)
1558 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1559 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1564 mutex_lock(&sensor
->mutex
);
1566 dev_err(&client
->dev
, "subdev %s, pad %d, index %d\n",
1567 subdev
->name
, code
->pad
, code
->index
);
1569 if (subdev
!= &sensor
->src
->sd
|| code
->pad
!= SMIAPP_PAD_SRC
) {
1573 code
->code
= sensor
->internal_csi_format
->code
;
1578 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1579 if (sensor
->mbus_frame_fmts
& (1 << i
))
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
);
1592 mutex_unlock(&sensor
->mutex
);
1597 static u32
__smiapp_get_mbus_code(struct v4l2_subdev
*subdev
,
1600 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1602 if (subdev
== &sensor
->src
->sd
&& pad
== SMIAPP_PAD_SRC
)
1603 return sensor
->csi_format
->code
;
1605 return sensor
->internal_csi_format
->code
;
1608 static int __smiapp_get_format(struct v4l2_subdev
*subdev
,
1609 struct v4l2_subdev_fh
*fh
,
1610 struct v4l2_subdev_format
*fmt
)
1612 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1614 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1615 fmt
->format
= *v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1617 struct v4l2_rect
*r
;
1619 if (fmt
->pad
== ssd
->source_pad
)
1620 r
= &ssd
->crop
[ssd
->source_pad
];
1624 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1625 fmt
->format
.width
= r
->width
;
1626 fmt
->format
.height
= r
->height
;
1627 fmt
->format
.field
= V4L2_FIELD_NONE
;
1633 static int smiapp_get_format(struct v4l2_subdev
*subdev
,
1634 struct v4l2_subdev_fh
*fh
,
1635 struct v4l2_subdev_format
*fmt
)
1637 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1640 mutex_lock(&sensor
->mutex
);
1641 rval
= __smiapp_get_format(subdev
, fh
, fmt
);
1642 mutex_unlock(&sensor
->mutex
);
1647 static 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
)
1652 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1655 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1657 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++)
1658 crops
[i
] = &ssd
->crop
[i
];
1660 *comps
= &ssd
->compose
;
1663 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++) {
1664 crops
[i
] = v4l2_subdev_get_try_crop(fh
, i
);
1669 *comps
= v4l2_subdev_get_try_compose(fh
,
1676 /* Changes require propagation only on sink pad. */
1677 static void smiapp_propagate(struct v4l2_subdev
*subdev
,
1678 struct v4l2_subdev_fh
*fh
, int which
,
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
];
1685 smiapp_get_crop_compose(subdev
, fh
, crops
, &comp
, which
);
1688 case V4L2_SEL_TGT_CROP
:
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
) {
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;
1704 case V4L2_SEL_TGT_COMPOSE
:
1705 *crops
[SMIAPP_PAD_SRC
] = *comp
;
1712 static const struct smiapp_csi_data_format
1713 *smiapp_validate_csi_data_format(struct smiapp_sensor
*sensor
, u32 code
)
1715 const struct smiapp_csi_data_format
*csi_format
= sensor
->csi_format
;
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
];
1727 static int smiapp_set_format_source(struct v4l2_subdev
*subdev
,
1728 struct v4l2_subdev_fh
*fh
,
1729 struct v4l2_subdev_format
*fmt
)
1731 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1732 const struct smiapp_csi_data_format
*csi_format
,
1733 *old_csi_format
= sensor
->csi_format
;
1734 unsigned long *valid_link_freqs
;
1735 u32 code
= fmt
->format
.code
;
1739 rval
= __smiapp_get_format(subdev
, fh
, fmt
);
1744 * Media bus code is changeable on src subdev's source pad. On
1745 * other source pads we just get format here.
1747 if (subdev
!= &sensor
->src
->sd
)
1750 csi_format
= smiapp_validate_csi_data_format(sensor
, code
);
1752 fmt
->format
.code
= csi_format
->code
;
1754 if (fmt
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1757 sensor
->csi_format
= csi_format
;
1759 if (csi_format
->width
!= old_csi_format
->width
)
1760 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
1761 __v4l2_ctrl_modify_range(
1762 sensor
->test_data
[i
], 0,
1763 (1 << csi_format
->width
) - 1, 1, 0);
1765 if (csi_format
->compressed
== old_csi_format
->compressed
)
1769 &sensor
->valid_link_freqs
[sensor
->csi_format
->compressed
1770 - SMIAPP_COMPRESSED_BASE
];
1772 __v4l2_ctrl_modify_range(
1773 sensor
->link_freq
, 0,
1774 __fls(*valid_link_freqs
), ~*valid_link_freqs
,
1775 __ffs(*valid_link_freqs
));
1777 return smiapp_pll_update(sensor
);
1780 static int smiapp_set_format(struct v4l2_subdev
*subdev
,
1781 struct v4l2_subdev_fh
*fh
,
1782 struct v4l2_subdev_format
*fmt
)
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
];
1788 mutex_lock(&sensor
->mutex
);
1790 if (fmt
->pad
== ssd
->source_pad
) {
1793 rval
= smiapp_set_format_source(subdev
, fh
, fmt
);
1795 mutex_unlock(&sensor
->mutex
);
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;
1804 fmt
->format
.field
= V4L2_FIELD_NONE
;
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
]);
1815 smiapp_get_crop_compose(subdev
, fh
, crops
, NULL
, fmt
->which
);
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
,
1826 mutex_unlock(&sensor
->mutex
);
1832 * Calculate goodness of scaled image size compared to expected image
1833 * size and flags provided.
1835 #define SCALING_GOODNESS 100000
1836 #define SCALING_GOODNESS_EXTREME 100000000
1837 static int scaling_goodness(struct v4l2_subdev
*subdev
, int w
, int ask_w
,
1838 int h
, int ask_h
, u32 flags
)
1840 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1841 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1849 if (flags
& V4L2_SEL_FLAG_GE
) {
1851 val
-= SCALING_GOODNESS
;
1853 val
-= SCALING_GOODNESS
;
1856 if (flags
& V4L2_SEL_FLAG_LE
) {
1858 val
-= SCALING_GOODNESS
;
1860 val
-= SCALING_GOODNESS
;
1863 val
-= abs(w
- ask_w
);
1864 val
-= abs(h
- ask_h
);
1866 if (w
< sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
])
1867 val
-= SCALING_GOODNESS_EXTREME
;
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
);
1875 static 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
)
1881 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1883 unsigned int binh
= 1, binv
= 1;
1884 int best
= scaling_goodness(
1886 crops
[SMIAPP_PAD_SINK
]->width
, sel
->r
.width
,
1887 crops
[SMIAPP_PAD_SINK
]->height
, sel
->r
.height
, sel
->flags
);
1889 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
1890 int this = scaling_goodness(
1892 crops
[SMIAPP_PAD_SINK
]->width
1893 / sensor
->binning_subtypes
[i
].horizontal
,
1895 crops
[SMIAPP_PAD_SINK
]->height
1896 / sensor
->binning_subtypes
[i
].vertical
,
1897 sel
->r
.height
, sel
->flags
);
1900 binh
= sensor
->binning_subtypes
[i
].horizontal
;
1901 binv
= sensor
->binning_subtypes
[i
].vertical
;
1905 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1906 sensor
->binning_vertical
= binv
;
1907 sensor
->binning_horizontal
= binh
;
1910 sel
->r
.width
= (crops
[SMIAPP_PAD_SINK
]->width
/ binh
) & ~1;
1911 sel
->r
.height
= (crops
[SMIAPP_PAD_SINK
]->height
/ binv
) & ~1;
1915 * Calculate best scaling ratio and mode for given output resolution.
1917 * Try all of these: horizontal ratio, vertical ratio and smallest
1918 * size possible (horizontally).
1920 * Also try whether horizontal scaler or full scaler gives a better
1923 static 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
)
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
;
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
);
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
];
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
]);
1959 dev_dbg(&client
->dev
, "scaling: a %d b %d max_m %d\n", a
, b
, max_m
);
1961 min
= min(max_m
, min(a
, b
));
1962 max
= min(max_m
, max(a
, b
));
1971 try[ntry
] = min
+ 1;
1974 try[ntry
] = max
+ 1;
1979 for (i
= 0; i
< ntry
; i
++) {
1980 int this = scaling_goodness(
1982 crops
[SMIAPP_PAD_SINK
]->width
1984 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
1986 crops
[SMIAPP_PAD_SINK
]->height
,
1990 dev_dbg(&client
->dev
, "trying factor %d (%d)\n", try[i
], i
);
1994 mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
1998 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
1999 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2002 this = scaling_goodness(
2003 subdev
, crops
[SMIAPP_PAD_SINK
]->width
2005 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2007 crops
[SMIAPP_PAD_SINK
]->height
2009 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2015 mode
= SMIAPP_SCALING_MODE_BOTH
;
2021 (crops
[SMIAPP_PAD_SINK
]->width
2023 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]) & ~1;
2024 if (mode
== SMIAPP_SCALING_MODE_BOTH
)
2026 (crops
[SMIAPP_PAD_SINK
]->height
2028 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
])
2031 sel
->r
.height
= crops
[SMIAPP_PAD_SINK
]->height
;
2033 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2034 sensor
->scale_m
= scale_m
;
2035 sensor
->scaling_mode
= mode
;
2038 /* We're only called on source pads. This function sets scaling. */
2039 static int smiapp_set_compose(struct v4l2_subdev
*subdev
,
2040 struct v4l2_subdev_fh
*fh
,
2041 struct v4l2_subdev_selection
*sel
)
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
];
2047 smiapp_get_crop_compose(subdev
, fh
, crops
, &comp
, sel
->which
);
2052 if (ssd
== sensor
->binner
)
2053 smiapp_set_compose_binner(subdev
, fh
, sel
, crops
, comp
);
2055 smiapp_set_compose_scaler(subdev
, fh
, sel
, crops
, comp
);
2058 smiapp_propagate(subdev
, fh
, sel
->which
,
2059 V4L2_SEL_TGT_COMPOSE
);
2061 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
2062 return smiapp_update_mode(sensor
);
2067 static int __smiapp_sel_supported(struct v4l2_subdev
*subdev
,
2068 struct v4l2_subdev_selection
*sel
)
2070 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2071 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2073 /* We only implement crop in three places. */
2074 switch (sel
->target
) {
2075 case V4L2_SEL_TGT_CROP
:
2076 case V4L2_SEL_TGT_CROP_BOUNDS
:
2077 if (ssd
== sensor
->pixel_array
2078 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2080 if (ssd
== sensor
->src
2081 && sel
->pad
== SMIAPP_PAD_SRC
)
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
)
2089 case V4L2_SEL_TGT_NATIVE_SIZE
:
2090 if (ssd
== sensor
->pixel_array
2091 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2094 case V4L2_SEL_TGT_COMPOSE
:
2095 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2096 if (sel
->pad
== ssd
->source_pad
)
2098 if (ssd
== sensor
->binner
)
2100 if (ssd
== sensor
->scaler
2101 && sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2102 != SMIAPP_SCALING_CAPABILITY_NONE
)
2110 static int smiapp_set_crop(struct v4l2_subdev
*subdev
,
2111 struct v4l2_subdev_fh
*fh
,
2112 struct v4l2_subdev_selection
*sel
)
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
;
2119 smiapp_get_crop_compose(subdev
, fh
, crops
, NULL
, sel
->which
);
2121 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2122 if (sel
->pad
== ssd
->sink_pad
)
2123 src_size
= &ssd
->sink_fmt
;
2125 src_size
= &ssd
->compose
;
2127 if (sel
->pad
== ssd
->sink_pad
) {
2130 _r
.width
= v4l2_subdev_get_try_format(fh
, sel
->pad
)
2132 _r
.height
= v4l2_subdev_get_try_format(fh
, sel
->pad
)
2137 v4l2_subdev_get_try_compose(
2142 if (ssd
== sensor
->src
&& sel
->pad
== SMIAPP_PAD_SRC
) {
2147 sel
->r
.width
= min(sel
->r
.width
, src_size
->width
);
2148 sel
->r
.height
= min(sel
->r
.height
, src_size
->height
);
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
);
2153 *crops
[sel
->pad
] = sel
->r
;
2155 if (ssd
!= sensor
->pixel_array
&& sel
->pad
== SMIAPP_PAD_SINK
)
2156 smiapp_propagate(subdev
, fh
, sel
->which
,
2162 static int __smiapp_get_selection(struct v4l2_subdev
*subdev
,
2163 struct v4l2_subdev_fh
*fh
,
2164 struct v4l2_subdev_selection
*sel
)
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
;
2172 ret
= __smiapp_sel_supported(subdev
, sel
);
2176 smiapp_get_crop_compose(subdev
, fh
, crops
, &comp
, sel
->which
);
2178 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2179 sink_fmt
= ssd
->sink_fmt
;
2181 struct v4l2_mbus_framefmt
*fmt
=
2182 v4l2_subdev_get_try_format(fh
, ssd
->sink_pad
);
2186 sink_fmt
.width
= fmt
->width
;
2187 sink_fmt
.height
= fmt
->height
;
2190 switch (sel
->target
) {
2191 case V4L2_SEL_TGT_CROP_BOUNDS
:
2192 case V4L2_SEL_TGT_NATIVE_SIZE
:
2193 if (ssd
== sensor
->pixel_array
) {
2194 sel
->r
.left
= sel
->r
.top
= 0;
2196 sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2198 sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2199 } else if (sel
->pad
== ssd
->sink_pad
) {
2205 case V4L2_SEL_TGT_CROP
:
2206 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2207 sel
->r
= *crops
[sel
->pad
];
2209 case V4L2_SEL_TGT_COMPOSE
:
2217 static int smiapp_get_selection(struct v4l2_subdev
*subdev
,
2218 struct v4l2_subdev_fh
*fh
,
2219 struct v4l2_subdev_selection
*sel
)
2221 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2224 mutex_lock(&sensor
->mutex
);
2225 rval
= __smiapp_get_selection(subdev
, fh
, sel
);
2226 mutex_unlock(&sensor
->mutex
);
2230 static int smiapp_set_selection(struct v4l2_subdev
*subdev
,
2231 struct v4l2_subdev_fh
*fh
,
2232 struct v4l2_subdev_selection
*sel
)
2234 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2237 ret
= __smiapp_sel_supported(subdev
, sel
);
2241 mutex_lock(&sensor
->mutex
);
2243 sel
->r
.left
= max(0, sel
->r
.left
& ~1);
2244 sel
->r
.top
= max(0, sel
->r
.top
& ~1);
2245 sel
->r
.width
= SMIAPP_ALIGN_DIM(sel
->r
.width
, sel
->flags
);
2246 sel
->r
.height
= SMIAPP_ALIGN_DIM(sel
->r
.height
, sel
->flags
);
2248 sel
->r
.width
= max_t(unsigned int,
2249 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
2251 sel
->r
.height
= max_t(unsigned int,
2252 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
2255 switch (sel
->target
) {
2256 case V4L2_SEL_TGT_CROP
:
2257 ret
= smiapp_set_crop(subdev
, fh
, sel
);
2259 case V4L2_SEL_TGT_COMPOSE
:
2260 ret
= smiapp_set_compose(subdev
, fh
, sel
);
2266 mutex_unlock(&sensor
->mutex
);
2270 static int smiapp_get_skip_frames(struct v4l2_subdev
*subdev
, u32
*frames
)
2272 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2274 *frames
= sensor
->frame_skip
;
2278 /* -----------------------------------------------------------------------------
2283 smiapp_sysfs_nvm_read(struct device
*dev
, struct device_attribute
*attr
,
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
;
2291 if (!sensor
->dev_init_done
)
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)
2299 if (smiapp_read_nvm(sensor
, sensor
->nvm
)) {
2300 dev_err(&client
->dev
, "nvm read failed\n");
2303 smiapp_set_power(subdev
, 0);
2306 * NVM is still way below a PAGE_SIZE, so we can safely
2307 * assume this for now.
2309 nbytes
= min_t(unsigned int, sensor
->nvm_size
, PAGE_SIZE
);
2310 memcpy(buf
, sensor
->nvm
, nbytes
);
2314 static DEVICE_ATTR(nvm
, S_IRUGO
, smiapp_sysfs_nvm_read
, NULL
);
2317 smiapp_sysfs_ident_read(struct device
*dev
, struct device_attribute
*attr
,
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
;
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;
2329 static DEVICE_ATTR(ident
, S_IRUGO
, smiapp_sysfs_ident_read
, NULL
);
2331 /* -----------------------------------------------------------------------------
2332 * V4L2 subdev core operations
2335 static int smiapp_identify_module(struct v4l2_subdev
*subdev
)
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
;
2343 minfo
->name
= SMIAPP_NAME
;
2346 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MANUFACTURER_ID
,
2347 &minfo
->manufacturer_id
);
2349 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U16_MODEL_ID
,
2352 rval
= smiapp_read_8only(sensor
,
2353 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR
,
2354 &minfo
->revision_number_major
);
2356 rval
= smiapp_read_8only(sensor
,
2357 SMIAPP_REG_U8_REVISION_NUMBER_MINOR
,
2358 &minfo
->revision_number_minor
);
2360 rval
= smiapp_read_8only(sensor
,
2361 SMIAPP_REG_U8_MODULE_DATE_YEAR
,
2362 &minfo
->module_year
);
2364 rval
= smiapp_read_8only(sensor
,
2365 SMIAPP_REG_U8_MODULE_DATE_MONTH
,
2366 &minfo
->module_month
);
2368 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MODULE_DATE_DAY
,
2369 &minfo
->module_day
);
2373 rval
= smiapp_read_8only(sensor
,
2374 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID
,
2375 &minfo
->sensor_manufacturer_id
);
2377 rval
= smiapp_read_8only(sensor
,
2378 SMIAPP_REG_U16_SENSOR_MODEL_ID
,
2379 &minfo
->sensor_model_id
);
2381 rval
= smiapp_read_8only(sensor
,
2382 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER
,
2383 &minfo
->sensor_revision_number
);
2385 rval
= smiapp_read_8only(sensor
,
2386 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION
,
2387 &minfo
->sensor_firmware_version
);
2391 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIA_VERSION
,
2392 &minfo
->smia_version
);
2394 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIAPP_VERSION
,
2395 &minfo
->smiapp_version
);
2398 dev_err(&client
->dev
, "sensor detection failed\n");
2402 dev_dbg(&client
->dev
, "module 0x%2.2x-0x%4.4x\n",
2403 minfo
->manufacturer_id
, minfo
->model_id
);
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
);
2410 dev_dbg(&client
->dev
, "sensor 0x%2.2x-0x%4.4x\n",
2411 minfo
->sensor_manufacturer_id
, minfo
->sensor_model_id
);
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
);
2417 dev_dbg(&client
->dev
, "smia version %2.2d smiapp version %2.2d\n",
2418 minfo
->smia_version
, minfo
->smiapp_version
);
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.
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
;
2431 for (i
= 0; i
< ARRAY_SIZE(smiapp_module_idents
); i
++) {
2432 if (smiapp_module_idents
[i
].manufacturer_id
2433 != minfo
->manufacturer_id
)
2435 if (smiapp_module_idents
[i
].model_id
!= minfo
->model_id
)
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
)
2443 if (smiapp_module_idents
[i
].revision_number_major
2444 != minfo
->revision_number_major
)
2448 minfo
->name
= smiapp_module_idents
[i
].name
;
2449 minfo
->quirk
= smiapp_module_idents
[i
].quirk
;
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");
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
);
2461 strlcpy(subdev
->name
, sensor
->minfo
.name
, sizeof(subdev
->name
));
2466 static const struct v4l2_subdev_ops smiapp_ops
;
2467 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
;
2468 static const struct media_entity_operations smiapp_entity_ops
;
2470 static int smiapp_registered(struct v4l2_subdev
*subdev
)
2472 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2473 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2474 struct smiapp_pll
*pll
= &sensor
->pll
;
2475 struct smiapp_subdev
*last
= NULL
;
2480 sensor
->vana
= devm_regulator_get(&client
->dev
, "vana");
2481 if (IS_ERR(sensor
->vana
)) {
2482 dev_err(&client
->dev
, "could not get regulator for vana\n");
2483 return PTR_ERR(sensor
->vana
);
2486 if (!sensor
->platform_data
->set_xclk
) {
2487 sensor
->ext_clk
= devm_clk_get(&client
->dev
, "ext_clk");
2488 if (IS_ERR(sensor
->ext_clk
)) {
2489 dev_err(&client
->dev
, "could not get clock\n");
2490 return PTR_ERR(sensor
->ext_clk
);
2493 rval
= clk_set_rate(sensor
->ext_clk
,
2494 sensor
->platform_data
->ext_clk
);
2496 dev_err(&client
->dev
,
2497 "unable to set clock freq to %u\n",
2498 sensor
->platform_data
->ext_clk
);
2503 if (gpio_is_valid(sensor
->platform_data
->xshutdown
)) {
2504 rval
= devm_gpio_request_one(
2505 &client
->dev
, sensor
->platform_data
->xshutdown
, 0,
2506 "SMIA++ xshutdown");
2508 dev_err(&client
->dev
,
2509 "unable to acquire reset gpio %d\n",
2510 sensor
->platform_data
->xshutdown
);
2515 rval
= smiapp_power_on(sensor
);
2519 rval
= smiapp_identify_module(subdev
);
2525 rval
= smiapp_get_all_limits(sensor
);
2532 * Handle Sensor Module orientation on the board.
2534 * The application of H-FLIP and V-FLIP on the sensor is modified by
2535 * the sensor orientation on the board.
2537 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2538 * both H-FLIP and V-FLIP for normal operation which also implies
2539 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2540 * controls will need to be internally inverted.
2542 * Rotation also changes the bayer pattern.
2544 if (sensor
->platform_data
->module_board_orient
==
2545 SMIAPP_MODULE_BOARD_ORIENT_180
)
2546 sensor
->hvflip_inv_mask
= SMIAPP_IMAGE_ORIENTATION_HFLIP
|
2547 SMIAPP_IMAGE_ORIENTATION_VFLIP
;
2549 rval
= smiapp_call_quirk(sensor
, limits
);
2551 dev_err(&client
->dev
, "limits quirks failed\n");
2555 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
]) {
2558 rval
= smiapp_read(sensor
,
2559 SMIAPP_REG_U8_BINNING_SUBTYPES
, &val
);
2564 sensor
->nbinning_subtypes
= min_t(u8
, val
,
2565 SMIAPP_BINNING_SUBTYPES
);
2567 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
2569 sensor
, SMIAPP_REG_U8_BINNING_TYPE_n(i
), &val
);
2574 sensor
->binning_subtypes
[i
] =
2575 *(struct smiapp_binning_subtype
*)&val
;
2577 dev_dbg(&client
->dev
, "binning %xx%x\n",
2578 sensor
->binning_subtypes
[i
].horizontal
,
2579 sensor
->binning_subtypes
[i
].vertical
);
2582 sensor
->binning_horizontal
= 1;
2583 sensor
->binning_vertical
= 1;
2585 if (device_create_file(&client
->dev
, &dev_attr_ident
) != 0) {
2586 dev_err(&client
->dev
, "sysfs ident entry creation failed\n");
2590 /* SMIA++ NVM initialization - it will be read from the sensor
2591 * when it is first requested by userspace.
2593 if (sensor
->minfo
.smiapp_version
&& sensor
->platform_data
->nvm_size
) {
2594 sensor
->nvm
= devm_kzalloc(&client
->dev
,
2595 sensor
->platform_data
->nvm_size
, GFP_KERNEL
);
2596 if (sensor
->nvm
== NULL
) {
2597 dev_err(&client
->dev
, "nvm buf allocation failed\n");
2599 goto out_ident_release
;
2602 if (device_create_file(&client
->dev
, &dev_attr_nvm
) != 0) {
2603 dev_err(&client
->dev
, "sysfs nvm entry failed\n");
2605 goto out_ident_release
;
2609 /* We consider this as profile 0 sensor if any of these are zero. */
2610 if (!sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV
] ||
2611 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV
] ||
2612 !sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV
] ||
2613 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV
]) {
2614 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_0
;
2615 } else if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2616 != SMIAPP_SCALING_CAPABILITY_NONE
) {
2617 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2618 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2619 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_1
;
2621 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_2
;
2622 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
2623 sensor
->ssds_used
++;
2624 } else if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
2625 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
2626 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
2627 sensor
->ssds_used
++;
2629 sensor
->binner
= &sensor
->ssds
[sensor
->ssds_used
];
2630 sensor
->ssds_used
++;
2631 sensor
->pixel_array
= &sensor
->ssds
[sensor
->ssds_used
];
2632 sensor
->ssds_used
++;
2634 sensor
->scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
2636 /* prepare PLL configuration input values */
2637 pll
->bus_type
= SMIAPP_PLL_BUS_TYPE_CSI2
;
2638 pll
->csi2
.lanes
= sensor
->platform_data
->lanes
;
2639 pll
->ext_clk_freq_hz
= sensor
->platform_data
->ext_clk
;
2640 pll
->flags
= smiapp_call_quirk(sensor
, pll_flags
);
2641 pll
->scale_n
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
2642 /* Profile 0 sensors have no separate OP clock branch. */
2643 if (sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
2644 pll
->flags
|= SMIAPP_PLL_FLAG_NO_OP_CLOCKS
;
2646 rval
= smiapp_get_mbus_formats(sensor
);
2649 goto out_nvm_release
;
2652 for (i
= 0; i
< SMIAPP_SUBDEVS
; i
++) {
2654 struct smiapp_subdev
*ssd
;
2656 } const __this
[] = {
2657 { sensor
->scaler
, "scaler", },
2658 { sensor
->binner
, "binner", },
2659 { sensor
->pixel_array
, "pixel array", },
2660 }, *_this
= &__this
[i
];
2661 struct smiapp_subdev
*this = _this
->ssd
;
2666 if (this != sensor
->src
)
2667 v4l2_subdev_init(&this->sd
, &smiapp_ops
);
2669 this->sensor
= sensor
;
2671 if (this == sensor
->pixel_array
) {
2675 this->source_pad
= 1;
2678 snprintf(this->sd
.name
,
2679 sizeof(this->sd
.name
), "%s %s %d-%4.4x",
2680 sensor
->minfo
.name
, _this
->name
,
2681 i2c_adapter_id(client
->adapter
), client
->addr
);
2683 this->sink_fmt
.width
=
2684 sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2685 this->sink_fmt
.height
=
2686 sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2687 this->compose
.width
= this->sink_fmt
.width
;
2688 this->compose
.height
= this->sink_fmt
.height
;
2689 this->crop
[this->source_pad
] = this->compose
;
2690 this->pads
[this->source_pad
].flags
= MEDIA_PAD_FL_SOURCE
;
2691 if (this != sensor
->pixel_array
) {
2692 this->crop
[this->sink_pad
] = this->compose
;
2693 this->pads
[this->sink_pad
].flags
= MEDIA_PAD_FL_SINK
;
2696 this->sd
.entity
.ops
= &smiapp_entity_ops
;
2703 this->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2704 this->sd
.internal_ops
= &smiapp_internal_ops
;
2705 this->sd
.owner
= THIS_MODULE
;
2706 v4l2_set_subdevdata(&this->sd
, client
);
2708 rval
= media_entity_init(&this->sd
.entity
,
2709 this->npads
, this->pads
, 0);
2711 dev_err(&client
->dev
,
2712 "media_entity_init failed\n");
2713 goto out_nvm_release
;
2716 rval
= media_entity_create_link(&this->sd
.entity
,
2720 MEDIA_LNK_FL_ENABLED
|
2721 MEDIA_LNK_FL_IMMUTABLE
);
2723 dev_err(&client
->dev
,
2724 "media_entity_create_link failed\n");
2725 goto out_nvm_release
;
2728 rval
= v4l2_device_register_subdev(sensor
->src
->sd
.v4l2_dev
,
2731 dev_err(&client
->dev
,
2732 "v4l2_device_register_subdev failed\n");
2733 goto out_nvm_release
;
2739 dev_dbg(&client
->dev
, "profile %d\n", sensor
->minfo
.smiapp_profile
);
2741 sensor
->pixel_array
->sd
.entity
.type
= MEDIA_ENT_T_V4L2_SUBDEV_SENSOR
;
2744 smiapp_read_frame_fmt(sensor
);
2745 rval
= smiapp_init_controls(sensor
);
2747 goto out_nvm_release
;
2749 mutex_lock(&sensor
->mutex
);
2750 rval
= smiapp_update_mode(sensor
);
2751 mutex_unlock(&sensor
->mutex
);
2753 dev_err(&client
->dev
, "update mode failed\n");
2754 goto out_nvm_release
;
2757 sensor
->streaming
= false;
2758 sensor
->dev_init_done
= true;
2760 /* check flash capability */
2761 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FLASH_MODE_CAPABILITY
, &tmp
);
2762 sensor
->flash_capability
= tmp
;
2764 goto out_nvm_release
;
2766 smiapp_power_off(sensor
);
2771 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2774 device_remove_file(&client
->dev
, &dev_attr_ident
);
2777 smiapp_power_off(sensor
);
2781 static int smiapp_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2783 struct smiapp_subdev
*ssd
= to_smiapp_subdev(sd
);
2784 struct smiapp_sensor
*sensor
= ssd
->sensor
;
2786 smiapp_csi_data_formats
[smiapp_pixel_order(sensor
)].code
;
2789 mutex_lock(&sensor
->mutex
);
2791 for (i
= 0; i
< ssd
->npads
; i
++) {
2792 struct v4l2_mbus_framefmt
*try_fmt
=
2793 v4l2_subdev_get_try_format(fh
, i
);
2794 struct v4l2_rect
*try_crop
= v4l2_subdev_get_try_crop(fh
, i
);
2795 struct v4l2_rect
*try_comp
;
2797 try_fmt
->width
= sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2798 try_fmt
->height
= sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2799 try_fmt
->code
= mbus_code
;
2800 try_fmt
->field
= V4L2_FIELD_NONE
;
2804 try_crop
->width
= try_fmt
->width
;
2805 try_crop
->height
= try_fmt
->height
;
2807 if (ssd
!= sensor
->pixel_array
)
2810 try_comp
= v4l2_subdev_get_try_compose(fh
, i
);
2811 *try_comp
= *try_crop
;
2814 mutex_unlock(&sensor
->mutex
);
2816 return smiapp_set_power(sd
, 1);
2819 static int smiapp_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2821 return smiapp_set_power(sd
, 0);
2824 static const struct v4l2_subdev_video_ops smiapp_video_ops
= {
2825 .s_stream
= smiapp_set_stream
,
2828 static const struct v4l2_subdev_core_ops smiapp_core_ops
= {
2829 .s_power
= smiapp_set_power
,
2832 static const struct v4l2_subdev_pad_ops smiapp_pad_ops
= {
2833 .enum_mbus_code
= smiapp_enum_mbus_code
,
2834 .get_fmt
= smiapp_get_format
,
2835 .set_fmt
= smiapp_set_format
,
2836 .get_selection
= smiapp_get_selection
,
2837 .set_selection
= smiapp_set_selection
,
2840 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops
= {
2841 .g_skip_frames
= smiapp_get_skip_frames
,
2844 static const struct v4l2_subdev_ops smiapp_ops
= {
2845 .core
= &smiapp_core_ops
,
2846 .video
= &smiapp_video_ops
,
2847 .pad
= &smiapp_pad_ops
,
2848 .sensor
= &smiapp_sensor_ops
,
2851 static const struct media_entity_operations smiapp_entity_ops
= {
2852 .link_validate
= v4l2_subdev_link_validate
,
2855 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops
= {
2856 .registered
= smiapp_registered
,
2857 .open
= smiapp_open
,
2858 .close
= smiapp_close
,
2861 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
= {
2862 .open
= smiapp_open
,
2863 .close
= smiapp_close
,
2866 /* -----------------------------------------------------------------------------
2872 static int smiapp_suspend(struct device
*dev
)
2874 struct i2c_client
*client
= to_i2c_client(dev
);
2875 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2876 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2879 BUG_ON(mutex_is_locked(&sensor
->mutex
));
2881 if (sensor
->power_count
== 0)
2884 if (sensor
->streaming
)
2885 smiapp_stop_streaming(sensor
);
2887 streaming
= sensor
->streaming
;
2889 smiapp_power_off(sensor
);
2891 /* save state for resume */
2892 sensor
->streaming
= streaming
;
2897 static int smiapp_resume(struct device
*dev
)
2899 struct i2c_client
*client
= to_i2c_client(dev
);
2900 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2901 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2904 if (sensor
->power_count
== 0)
2907 rval
= smiapp_power_on(sensor
);
2911 if (sensor
->streaming
)
2912 rval
= smiapp_start_streaming(sensor
);
2919 #define smiapp_suspend NULL
2920 #define smiapp_resume NULL
2922 #endif /* CONFIG_PM */
2924 static int smiapp_probe(struct i2c_client
*client
,
2925 const struct i2c_device_id
*devid
)
2927 struct smiapp_sensor
*sensor
;
2929 if (client
->dev
.platform_data
== NULL
)
2932 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
2936 sensor
->platform_data
= client
->dev
.platform_data
;
2937 mutex_init(&sensor
->mutex
);
2938 mutex_init(&sensor
->power_mutex
);
2939 sensor
->src
= &sensor
->ssds
[sensor
->ssds_used
];
2941 v4l2_i2c_subdev_init(&sensor
->src
->sd
, client
, &smiapp_ops
);
2942 sensor
->src
->sd
.internal_ops
= &smiapp_internal_src_ops
;
2943 sensor
->src
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2944 sensor
->src
->sensor
= sensor
;
2946 sensor
->src
->pads
[0].flags
= MEDIA_PAD_FL_SOURCE
;
2947 return media_entity_init(&sensor
->src
->sd
.entity
, 2,
2948 sensor
->src
->pads
, 0);
2951 static int smiapp_remove(struct i2c_client
*client
)
2953 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2954 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2957 if (sensor
->power_count
) {
2958 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
2959 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
2960 if (sensor
->platform_data
->set_xclk
)
2961 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
2963 clk_disable_unprepare(sensor
->ext_clk
);
2964 sensor
->power_count
= 0;
2967 device_remove_file(&client
->dev
, &dev_attr_ident
);
2969 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2971 for (i
= 0; i
< sensor
->ssds_used
; i
++) {
2972 v4l2_device_unregister_subdev(&sensor
->ssds
[i
].sd
);
2973 media_entity_cleanup(&sensor
->ssds
[i
].sd
.entity
);
2975 smiapp_free_controls(sensor
);
2980 static const struct i2c_device_id smiapp_id_table
[] = {
2984 MODULE_DEVICE_TABLE(i2c
, smiapp_id_table
);
2986 static const struct dev_pm_ops smiapp_pm_ops
= {
2987 .suspend
= smiapp_suspend
,
2988 .resume
= smiapp_resume
,
2991 static struct i2c_driver smiapp_i2c_driver
= {
2993 .name
= SMIAPP_NAME
,
2994 .pm
= &smiapp_pm_ops
,
2996 .probe
= smiapp_probe
,
2997 .remove
= smiapp_remove
,
2998 .id_table
= smiapp_id_table
,
3001 module_i2c_driver(smiapp_i2c_driver
);
3003 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3004 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3005 MODULE_LICENSE("GPL");