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_update(struct smiapp_sensor
*sensor
)
245 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
246 struct smiapp_pll_limits lim
= {
247 .min_pre_pll_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV
],
248 .max_pre_pll_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV
],
249 .min_pll_ip_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ
],
250 .max_pll_ip_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ
],
251 .min_pll_multiplier
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER
],
252 .max_pll_multiplier
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER
],
253 .min_pll_op_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ
],
254 .max_pll_op_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ
],
256 .op
.min_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV
],
257 .op
.max_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV
],
258 .op
.min_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV
],
259 .op
.max_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV
],
260 .op
.min_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ
],
261 .op
.max_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ
],
262 .op
.min_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ
],
263 .op
.max_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ
],
265 .vt
.min_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV
],
266 .vt
.max_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV
],
267 .vt
.min_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV
],
268 .vt
.max_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV
],
269 .vt
.min_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ
],
270 .vt
.max_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ
],
271 .vt
.min_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ
],
272 .vt
.max_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ
],
274 .min_line_length_pck_bin
= sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
],
275 .min_line_length_pck
= sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK
],
277 struct smiapp_pll
*pll
= &sensor
->pll
;
280 if (sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
) {
282 * Fill in operational clock divisors limits from the
283 * video timing ones. On profile 0 sensors the
284 * requirements regarding them are essentially the
285 * same as on VT ones.
290 pll
->binning_horizontal
= sensor
->binning_horizontal
;
291 pll
->binning_vertical
= sensor
->binning_vertical
;
293 sensor
->link_freq
->qmenu_int
[sensor
->link_freq
->val
];
294 pll
->scale_m
= sensor
->scale_m
;
295 pll
->bits_per_pixel
= sensor
->csi_format
->compressed
;
297 rval
= smiapp_pll_calculate(&client
->dev
, &lim
, pll
);
301 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_parray
,
302 pll
->vt_pix_clk_freq_hz
);
303 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_csi
, pll
->pixel_rate_csi
);
311 * V4L2 Controls handling
315 static void __smiapp_update_exposure_limits(struct smiapp_sensor
*sensor
)
317 struct v4l2_ctrl
*ctrl
= sensor
->exposure
;
320 max
= sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
321 + sensor
->vblank
->val
322 - sensor
->limits
[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN
];
324 __v4l2_ctrl_modify_range(ctrl
, ctrl
->minimum
, max
, ctrl
->step
, max
);
330 * 1. Bits-per-pixel, descending.
331 * 2. Bits-per-pixel compressed, descending.
332 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
333 * orders must be defined.
335 static const struct smiapp_csi_data_format smiapp_csi_data_formats
[] = {
336 { V4L2_MBUS_FMT_SGRBG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GRBG
, },
337 { V4L2_MBUS_FMT_SRGGB12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_RGGB
, },
338 { V4L2_MBUS_FMT_SBGGR12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_BGGR
, },
339 { V4L2_MBUS_FMT_SGBRG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GBRG
, },
340 { V4L2_MBUS_FMT_SGRBG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GRBG
, },
341 { V4L2_MBUS_FMT_SRGGB10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_RGGB
, },
342 { V4L2_MBUS_FMT_SBGGR10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_BGGR
, },
343 { V4L2_MBUS_FMT_SGBRG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GBRG
, },
344 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
345 { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
346 { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
347 { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
348 { V4L2_MBUS_FMT_SGRBG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
349 { V4L2_MBUS_FMT_SRGGB8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
350 { V4L2_MBUS_FMT_SBGGR8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
351 { V4L2_MBUS_FMT_SGBRG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
354 const char *pixel_order_str
[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
356 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
357 - (unsigned long)smiapp_csi_data_formats) \
358 / sizeof(*smiapp_csi_data_formats))
360 static u32
smiapp_pixel_order(struct smiapp_sensor
*sensor
)
362 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
366 if (sensor
->hflip
->val
)
367 flip
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
369 if (sensor
->vflip
->val
)
370 flip
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
373 flip
^= sensor
->hvflip_inv_mask
;
375 dev_dbg(&client
->dev
, "flip %d\n", flip
);
376 return sensor
->default_pixel_order
^ flip
;
379 static void smiapp_update_mbus_formats(struct smiapp_sensor
*sensor
)
381 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
382 unsigned int csi_format_idx
=
383 to_csi_format_idx(sensor
->csi_format
) & ~3;
384 unsigned int internal_csi_format_idx
=
385 to_csi_format_idx(sensor
->internal_csi_format
) & ~3;
386 unsigned int pixel_order
= smiapp_pixel_order(sensor
);
388 sensor
->mbus_frame_fmts
=
389 sensor
->default_mbus_frame_fmts
<< pixel_order
;
391 &smiapp_csi_data_formats
[csi_format_idx
+ pixel_order
];
392 sensor
->internal_csi_format
=
393 &smiapp_csi_data_formats
[internal_csi_format_idx
396 BUG_ON(max(internal_csi_format_idx
, csi_format_idx
) + pixel_order
397 >= ARRAY_SIZE(smiapp_csi_data_formats
));
399 dev_dbg(&client
->dev
, "new pixel order %s\n",
400 pixel_order_str
[pixel_order
]);
403 static const char * const smiapp_test_patterns
[] = {
406 "Eight Vertical Colour Bars",
407 "Colour Bars With Fade to Grey",
408 "Pseudorandom Sequence (PN9)",
411 static int smiapp_set_ctrl(struct v4l2_ctrl
*ctrl
)
413 struct smiapp_sensor
*sensor
=
414 container_of(ctrl
->handler
, struct smiapp_subdev
, ctrl_handler
)
421 case V4L2_CID_ANALOGUE_GAIN
:
424 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL
, ctrl
->val
);
426 case V4L2_CID_EXPOSURE
:
429 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME
, ctrl
->val
);
433 if (sensor
->streaming
)
436 if (sensor
->hflip
->val
)
437 orient
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
439 if (sensor
->vflip
->val
)
440 orient
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
442 orient
^= sensor
->hvflip_inv_mask
;
443 rval
= smiapp_write(sensor
,
444 SMIAPP_REG_U8_IMAGE_ORIENTATION
,
449 smiapp_update_mbus_formats(sensor
);
453 case V4L2_CID_VBLANK
:
454 exposure
= sensor
->exposure
->val
;
456 __smiapp_update_exposure_limits(sensor
);
458 if (exposure
> sensor
->exposure
->maximum
) {
459 sensor
->exposure
->val
=
460 sensor
->exposure
->maximum
;
461 rval
= smiapp_set_ctrl(
468 sensor
, SMIAPP_REG_U16_FRAME_LENGTH_LINES
,
469 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
472 case V4L2_CID_HBLANK
:
474 sensor
, SMIAPP_REG_U16_LINE_LENGTH_PCK
,
475 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
478 case V4L2_CID_LINK_FREQ
:
479 if (sensor
->streaming
)
482 return smiapp_pll_update(sensor
);
484 case V4L2_CID_TEST_PATTERN
: {
487 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
489 sensor
->test_data
[i
],
491 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR
);
494 sensor
, SMIAPP_REG_U16_TEST_PATTERN_MODE
, ctrl
->val
);
497 case V4L2_CID_TEST_PATTERN_RED
:
499 sensor
, SMIAPP_REG_U16_TEST_DATA_RED
, ctrl
->val
);
501 case V4L2_CID_TEST_PATTERN_GREENR
:
503 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENR
, ctrl
->val
);
505 case V4L2_CID_TEST_PATTERN_BLUE
:
507 sensor
, SMIAPP_REG_U16_TEST_DATA_BLUE
, ctrl
->val
);
509 case V4L2_CID_TEST_PATTERN_GREENB
:
511 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENB
, ctrl
->val
);
513 case V4L2_CID_PIXEL_RATE
:
514 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
522 static const struct v4l2_ctrl_ops smiapp_ctrl_ops
= {
523 .s_ctrl
= smiapp_set_ctrl
,
526 static int smiapp_init_controls(struct smiapp_sensor
*sensor
)
528 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
532 rval
= v4l2_ctrl_handler_init(&sensor
->pixel_array
->ctrl_handler
, 12);
535 sensor
->pixel_array
->ctrl_handler
.lock
= &sensor
->mutex
;
537 sensor
->analog_gain
= v4l2_ctrl_new_std(
538 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
539 V4L2_CID_ANALOGUE_GAIN
,
540 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN
],
541 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX
],
542 max(sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP
], 1U),
543 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN
]);
545 /* Exposure limits will be updated soon, use just something here. */
546 sensor
->exposure
= v4l2_ctrl_new_std(
547 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
548 V4L2_CID_EXPOSURE
, 0, 0, 1, 0);
550 sensor
->hflip
= v4l2_ctrl_new_std(
551 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
552 V4L2_CID_HFLIP
, 0, 1, 1, 0);
553 sensor
->vflip
= v4l2_ctrl_new_std(
554 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
555 V4L2_CID_VFLIP
, 0, 1, 1, 0);
557 sensor
->vblank
= v4l2_ctrl_new_std(
558 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
559 V4L2_CID_VBLANK
, 0, 1, 1, 0);
562 sensor
->vblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
564 sensor
->hblank
= v4l2_ctrl_new_std(
565 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
566 V4L2_CID_HBLANK
, 0, 1, 1, 0);
569 sensor
->hblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
571 sensor
->pixel_rate_parray
= v4l2_ctrl_new_std(
572 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
573 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
575 v4l2_ctrl_new_std_menu_items(&sensor
->pixel_array
->ctrl_handler
,
576 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
577 ARRAY_SIZE(smiapp_test_patterns
) - 1,
578 0, 0, smiapp_test_patterns
);
580 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++) {
581 int max_value
= (1 << sensor
->csi_format
->width
) - 1;
582 sensor
->test_data
[i
] =
584 &sensor
->pixel_array
->ctrl_handler
,
585 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN_RED
+ i
,
586 0, max_value
, 1, max_value
);
589 if (sensor
->pixel_array
->ctrl_handler
.error
) {
590 dev_err(&client
->dev
,
591 "pixel array controls initialization failed (%d)\n",
592 sensor
->pixel_array
->ctrl_handler
.error
);
593 rval
= sensor
->pixel_array
->ctrl_handler
.error
;
597 sensor
->pixel_array
->sd
.ctrl_handler
=
598 &sensor
->pixel_array
->ctrl_handler
;
600 v4l2_ctrl_cluster(2, &sensor
->hflip
);
602 rval
= v4l2_ctrl_handler_init(&sensor
->src
->ctrl_handler
, 0);
605 sensor
->src
->ctrl_handler
.lock
= &sensor
->mutex
;
607 for (max
= 0; sensor
->platform_data
->op_sys_clock
[max
+ 1]; max
++);
609 sensor
->link_freq
= v4l2_ctrl_new_int_menu(
610 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
611 V4L2_CID_LINK_FREQ
, max
, 0,
612 sensor
->platform_data
->op_sys_clock
);
614 sensor
->pixel_rate_csi
= v4l2_ctrl_new_std(
615 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
616 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
618 if (sensor
->src
->ctrl_handler
.error
) {
619 dev_err(&client
->dev
,
620 "src controls initialization failed (%d)\n",
621 sensor
->src
->ctrl_handler
.error
);
622 rval
= sensor
->src
->ctrl_handler
.error
;
626 sensor
->src
->sd
.ctrl_handler
=
627 &sensor
->src
->ctrl_handler
;
632 v4l2_ctrl_handler_free(&sensor
->pixel_array
->ctrl_handler
);
633 v4l2_ctrl_handler_free(&sensor
->src
->ctrl_handler
);
638 static void smiapp_free_controls(struct smiapp_sensor
*sensor
)
642 for (i
= 0; i
< sensor
->ssds_used
; i
++)
643 v4l2_ctrl_handler_free(&sensor
->ssds
[i
].ctrl_handler
);
646 static int smiapp_get_limits(struct smiapp_sensor
*sensor
, int const *limit
,
649 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
654 for (i
= 0; i
< n
; i
++) {
656 sensor
, smiapp_reg_limits
[limit
[i
]].addr
, &val
);
659 sensor
->limits
[limit
[i
]] = val
;
660 dev_dbg(&client
->dev
, "0x%8.8x \"%s\" = %u, 0x%x\n",
661 smiapp_reg_limits
[limit
[i
]].addr
,
662 smiapp_reg_limits
[limit
[i
]].what
, val
, val
);
668 static int smiapp_get_all_limits(struct smiapp_sensor
*sensor
)
673 for (i
= 0; i
< SMIAPP_LIMIT_LAST
; i
++) {
674 rval
= smiapp_get_limits(sensor
, &i
, 1);
679 if (sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] == 0)
680 smiapp_replace_limit(sensor
, SMIAPP_LIMIT_SCALER_N_MIN
, 16);
685 static int smiapp_get_limits_binning(struct smiapp_sensor
*sensor
)
687 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
688 static u32
const limits
[] = {
689 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
,
690 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN
,
691 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
,
692 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN
,
693 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
,
694 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN
,
695 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN
,
697 static u32
const limits_replace
[] = {
698 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES
,
699 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES
,
700 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK
,
701 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK
,
702 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK
,
703 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN
,
704 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN
,
709 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
] ==
710 SMIAPP_BINNING_CAPABILITY_NO
) {
711 for (i
= 0; i
< ARRAY_SIZE(limits
); i
++)
712 sensor
->limits
[limits
[i
]] =
713 sensor
->limits
[limits_replace
[i
]];
718 rval
= smiapp_get_limits(sensor
, limits
, ARRAY_SIZE(limits
));
723 * Sanity check whether the binning limits are valid. If not,
724 * use the non-binning ones.
726 if (sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
]
727 && sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
]
728 && sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
])
731 for (i
= 0; i
< ARRAY_SIZE(limits
); i
++) {
732 dev_dbg(&client
->dev
,
733 "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
734 smiapp_reg_limits
[limits
[i
]].addr
,
735 smiapp_reg_limits
[limits
[i
]].what
,
736 sensor
->limits
[limits_replace
[i
]],
737 sensor
->limits
[limits_replace
[i
]]);
738 sensor
->limits
[limits
[i
]] =
739 sensor
->limits
[limits_replace
[i
]];
745 static int smiapp_get_mbus_formats(struct smiapp_sensor
*sensor
)
747 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
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
;
811 if (!sensor
->csi_format
812 || f
->width
> sensor
->csi_format
->width
813 || (f
->width
== sensor
->csi_format
->width
815 > sensor
->csi_format
->compressed
)) {
816 sensor
->csi_format
= f
;
817 sensor
->internal_csi_format
= f
;
822 if (!sensor
->csi_format
) {
823 dev_err(&client
->dev
, "no supported mbus code found\n");
827 smiapp_update_mbus_formats(sensor
);
832 static void smiapp_update_blanking(struct smiapp_sensor
*sensor
)
834 struct v4l2_ctrl
*vblank
= sensor
->vblank
;
835 struct v4l2_ctrl
*hblank
= sensor
->hblank
;
839 sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES
],
840 sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
] -
841 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
);
842 max
= sensor
->limits
[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN
] -
843 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
;
845 __v4l2_ctrl_modify_range(vblank
, min
, max
, vblank
->step
, min
);
848 sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
] -
849 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
,
850 sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
]);
851 max
= sensor
->limits
[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN
] -
852 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
;
854 __v4l2_ctrl_modify_range(hblank
, min
, max
, hblank
->step
, min
);
856 __smiapp_update_exposure_limits(sensor
);
859 static int smiapp_update_mode(struct smiapp_sensor
*sensor
)
861 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
862 unsigned int binning_mode
;
865 dev_dbg(&client
->dev
, "frame size: %dx%d\n",
866 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
,
867 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
868 dev_dbg(&client
->dev
, "csi format width: %d\n",
869 sensor
->csi_format
->width
);
871 /* Binning has to be set up here; it affects limits */
872 if (sensor
->binning_horizontal
== 1 &&
873 sensor
->binning_vertical
== 1) {
877 (sensor
->binning_horizontal
<< 4)
878 | sensor
->binning_vertical
;
881 sensor
, SMIAPP_REG_U8_BINNING_TYPE
, binning_type
);
887 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_BINNING_MODE
, binning_mode
);
891 /* Get updated limits due to binning */
892 rval
= smiapp_get_limits_binning(sensor
);
896 rval
= smiapp_pll_update(sensor
);
900 /* Output from pixel array, including blanking */
901 smiapp_update_blanking(sensor
);
903 dev_dbg(&client
->dev
, "vblank\t\t%d\n", sensor
->vblank
->val
);
904 dev_dbg(&client
->dev
, "hblank\t\t%d\n", sensor
->hblank
->val
);
906 dev_dbg(&client
->dev
, "real timeperframe\t100/%d\n",
907 sensor
->pll
.vt_pix_clk_freq_hz
/
908 ((sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
909 + sensor
->hblank
->val
) *
910 (sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
911 + sensor
->vblank
->val
) / 100));
918 * SMIA++ NVM handling
921 static int smiapp_read_nvm(struct smiapp_sensor
*sensor
,
927 np
= sensor
->nvm_size
/ SMIAPP_NVM_PAGE_SIZE
;
928 for (p
= 0; p
< np
; p
++) {
931 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT
, p
);
935 rval
= smiapp_write(sensor
,
936 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
,
937 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN
|
938 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN
);
942 for (i
= 0; i
< 1000; i
++) {
945 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS
, &s
);
950 if (s
& SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY
)
960 for (i
= 0; i
< SMIAPP_NVM_PAGE_SIZE
; i
++) {
963 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0
+ i
,
973 rval2
= smiapp_write(sensor
, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
, 0);
982 * SMIA++ CCI address control
985 static int smiapp_change_cci_addr(struct smiapp_sensor
*sensor
)
987 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
991 client
->addr
= sensor
->platform_data
->i2c_addr_dfl
;
993 rval
= smiapp_write(sensor
,
994 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
,
995 sensor
->platform_data
->i2c_addr_alt
<< 1);
999 client
->addr
= sensor
->platform_data
->i2c_addr_alt
;
1001 /* verify addr change went ok */
1002 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
, &val
);
1006 if (val
!= sensor
->platform_data
->i2c_addr_alt
<< 1)
1014 * SMIA++ Mode Control
1017 static int smiapp_setup_flash_strobe(struct smiapp_sensor
*sensor
)
1019 struct smiapp_flash_strobe_parms
*strobe_setup
;
1020 unsigned int ext_freq
= sensor
->platform_data
->ext_clk
;
1022 u32 strobe_adjustment
;
1023 u32 strobe_width_high_rs
;
1026 strobe_setup
= sensor
->platform_data
->strobe_setup
;
1029 * How to calculate registers related to strobe length. Please
1030 * do not change, or if you do at least know what you're
1033 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1035 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1036 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1038 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1039 * flash_strobe_adjustment E N, [1 - 0xff]
1041 * The formula above is written as below to keep it on one
1044 * l / 10^6 = w / e * a
1046 * Let's mark w * a by x:
1054 * The strobe width must be at least as long as requested,
1055 * thus rounding upwards is needed.
1057 * x = (l * e + 10^6 - 1) / 10^6
1058 * -----------------------------
1060 * Maximum possible accuracy is wanted at all times. Thus keep
1061 * a as small as possible.
1063 * Calculate a, assuming maximum w, with rounding upwards:
1065 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1066 * -------------------------------------
1068 * Thus, we also get w, with that a, with rounding upwards:
1070 * w = (x + a - 1) / a
1071 * -------------------
1075 * x E [1, (2^16 - 1) * (2^8 - 1)]
1077 * Substituting maximum x to the original formula (with rounding),
1078 * the maximum l is thus
1080 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1082 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1083 * --------------------------------------------------
1085 * flash_strobe_length must be clamped between 1 and
1086 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1090 * flash_strobe_adjustment = ((flash_strobe_length *
1091 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1093 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1094 * EXTCLK freq + 10^6 - 1) / 10^6 +
1095 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1097 tmp
= div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1098 1000000 + 1, ext_freq
);
1099 strobe_setup
->strobe_width_high_us
=
1100 clamp_t(u32
, strobe_setup
->strobe_width_high_us
, 1, tmp
);
1102 tmp
= div_u64(((u64
)strobe_setup
->strobe_width_high_us
* (u64
)ext_freq
+
1103 1000000 - 1), 1000000ULL);
1104 strobe_adjustment
= (tmp
+ (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1105 strobe_width_high_rs
= (tmp
+ strobe_adjustment
- 1) /
1108 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_MODE_RS
,
1109 strobe_setup
->mode
);
1113 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT
,
1118 rval
= smiapp_write(
1119 sensor
, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL
,
1120 strobe_width_high_rs
);
1124 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL
,
1125 strobe_setup
->strobe_delay
);
1129 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_FLASH_STROBE_START_POINT
,
1130 strobe_setup
->stobe_start_point
);
1134 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_TRIGGER_RS
,
1135 strobe_setup
->trigger
);
1138 sensor
->platform_data
->strobe_setup
->trigger
= 0;
1143 /* -----------------------------------------------------------------------------
1147 static int smiapp_power_on(struct smiapp_sensor
*sensor
)
1149 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1153 rval
= regulator_enable(sensor
->vana
);
1155 dev_err(&client
->dev
, "failed to enable vana regulator\n");
1158 usleep_range(1000, 1000);
1160 if (sensor
->platform_data
->set_xclk
)
1161 rval
= sensor
->platform_data
->set_xclk(
1162 &sensor
->src
->sd
, sensor
->platform_data
->ext_clk
);
1164 rval
= clk_prepare_enable(sensor
->ext_clk
);
1166 dev_dbg(&client
->dev
, "failed to enable xclk\n");
1169 usleep_range(1000, 1000);
1171 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1172 gpio_set_value(sensor
->platform_data
->xshutdown
, 1);
1174 sleep
= SMIAPP_RESET_DELAY(sensor
->platform_data
->ext_clk
);
1175 usleep_range(sleep
, sleep
);
1178 * Failures to respond to the address change command have been noticed.
1179 * Those failures seem to be caused by the sensor requiring a longer
1180 * boot time than advertised. An additional 10ms delay seems to work
1181 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1182 * unnecessary. The failures need to be investigated to find a proper
1183 * fix, and a delay will likely need to be added here if the I2C write
1184 * retry hack is reverted before the root cause of the boot time issue
1188 if (sensor
->platform_data
->i2c_addr_alt
) {
1189 rval
= smiapp_change_cci_addr(sensor
);
1191 dev_err(&client
->dev
, "cci address change error\n");
1192 goto out_cci_addr_fail
;
1196 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_SOFTWARE_RESET
,
1197 SMIAPP_SOFTWARE_RESET
);
1199 dev_err(&client
->dev
, "software reset failed\n");
1200 goto out_cci_addr_fail
;
1203 if (sensor
->platform_data
->i2c_addr_alt
) {
1204 rval
= smiapp_change_cci_addr(sensor
);
1206 dev_err(&client
->dev
, "cci address change error\n");
1207 goto out_cci_addr_fail
;
1211 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_COMPRESSION_MODE
,
1212 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR
);
1214 dev_err(&client
->dev
, "compression mode set failed\n");
1215 goto out_cci_addr_fail
;
1218 rval
= smiapp_write(
1219 sensor
, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ
,
1220 sensor
->platform_data
->ext_clk
/ (1000000 / (1 << 8)));
1222 dev_err(&client
->dev
, "extclk frequency set failed\n");
1223 goto out_cci_addr_fail
;
1226 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_LANE_MODE
,
1227 sensor
->platform_data
->lanes
- 1);
1229 dev_err(&client
->dev
, "csi lane mode set failed\n");
1230 goto out_cci_addr_fail
;
1233 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FAST_STANDBY_CTRL
,
1234 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE
);
1236 dev_err(&client
->dev
, "fast standby set failed\n");
1237 goto out_cci_addr_fail
;
1240 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_SIGNALLING_MODE
,
1241 sensor
->platform_data
->csi_signalling_mode
);
1243 dev_err(&client
->dev
, "csi signalling mode set failed\n");
1244 goto out_cci_addr_fail
;
1247 /* DPHY control done by sensor based on requested link rate */
1248 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_DPHY_CTRL
,
1249 SMIAPP_DPHY_CTRL_UI
);
1253 rval
= smiapp_call_quirk(sensor
, post_poweron
);
1255 dev_err(&client
->dev
, "post_poweron quirks failed\n");
1256 goto out_cci_addr_fail
;
1259 /* Are we still initialising...? If yes, return here. */
1260 if (!sensor
->pixel_array
)
1263 rval
= v4l2_ctrl_handler_setup(
1264 &sensor
->pixel_array
->ctrl_handler
);
1266 goto out_cci_addr_fail
;
1268 rval
= v4l2_ctrl_handler_setup(&sensor
->src
->ctrl_handler
);
1270 goto out_cci_addr_fail
;
1272 mutex_lock(&sensor
->mutex
);
1273 rval
= smiapp_update_mode(sensor
);
1274 mutex_unlock(&sensor
->mutex
);
1276 goto out_cci_addr_fail
;
1281 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1282 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
1283 if (sensor
->platform_data
->set_xclk
)
1284 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
1286 clk_disable_unprepare(sensor
->ext_clk
);
1289 regulator_disable(sensor
->vana
);
1293 static void smiapp_power_off(struct smiapp_sensor
*sensor
)
1296 * Currently power/clock to lens are enable/disabled separately
1297 * but they are essentially the same signals. So if the sensor is
1298 * powered off while the lens is powered on the sensor does not
1299 * really see a power off and next time the cci address change
1300 * will fail. So do a soft reset explicitly here.
1302 if (sensor
->platform_data
->i2c_addr_alt
)
1303 smiapp_write(sensor
,
1304 SMIAPP_REG_U8_SOFTWARE_RESET
,
1305 SMIAPP_SOFTWARE_RESET
);
1307 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1308 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
1309 if (sensor
->platform_data
->set_xclk
)
1310 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
1312 clk_disable_unprepare(sensor
->ext_clk
);
1313 usleep_range(5000, 5000);
1314 regulator_disable(sensor
->vana
);
1315 sensor
->streaming
= false;
1318 static int smiapp_set_power(struct v4l2_subdev
*subdev
, int on
)
1320 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1323 mutex_lock(&sensor
->power_mutex
);
1325 if (on
&& !sensor
->power_count
) {
1326 /* Power on and perform initialisation. */
1327 ret
= smiapp_power_on(sensor
);
1330 } else if (!on
&& sensor
->power_count
== 1) {
1331 smiapp_power_off(sensor
);
1334 /* Update the power count. */
1335 sensor
->power_count
+= on
? 1 : -1;
1336 WARN_ON(sensor
->power_count
< 0);
1339 mutex_unlock(&sensor
->power_mutex
);
1343 /* -----------------------------------------------------------------------------
1344 * Video stream management
1347 static int smiapp_start_streaming(struct smiapp_sensor
*sensor
)
1349 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1352 mutex_lock(&sensor
->mutex
);
1354 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_CSI_DATA_FORMAT
,
1355 (sensor
->csi_format
->width
<< 8) |
1356 sensor
->csi_format
->compressed
);
1360 rval
= smiapp_pll_configure(sensor
);
1364 /* Analog crop start coordinates */
1365 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_ADDR_START
,
1366 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
);
1370 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_ADDR_START
,
1371 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
);
1375 /* Analog crop end coordinates */
1376 rval
= smiapp_write(
1377 sensor
, SMIAPP_REG_U16_X_ADDR_END
,
1378 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
1379 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
- 1);
1383 rval
= smiapp_write(
1384 sensor
, SMIAPP_REG_U16_Y_ADDR_END
,
1385 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
1386 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
- 1);
1391 * Output from pixel array, including blanking, is set using
1392 * controls below. No need to set here.
1396 if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
1397 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
1398 rval
= smiapp_write(
1399 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET
,
1400 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].left
);
1404 rval
= smiapp_write(
1405 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET
,
1406 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].top
);
1410 rval
= smiapp_write(
1411 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH
,
1412 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].width
);
1416 rval
= smiapp_write(
1417 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT
,
1418 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].height
);
1424 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
1425 != SMIAPP_SCALING_CAPABILITY_NONE
) {
1426 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALING_MODE
,
1427 sensor
->scaling_mode
);
1431 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALE_M
,
1437 /* Output size from sensor */
1438 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_OUTPUT_SIZE
,
1439 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
);
1442 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_OUTPUT_SIZE
,
1443 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
1447 if ((sensor
->flash_capability
&
1448 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE
|
1449 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE
)) &&
1450 sensor
->platform_data
->strobe_setup
!= NULL
&&
1451 sensor
->platform_data
->strobe_setup
->trigger
!= 0) {
1452 rval
= smiapp_setup_flash_strobe(sensor
);
1457 rval
= smiapp_call_quirk(sensor
, pre_streamon
);
1459 dev_err(&client
->dev
, "pre_streamon quirks failed\n");
1463 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1464 SMIAPP_MODE_SELECT_STREAMING
);
1467 mutex_unlock(&sensor
->mutex
);
1472 static int smiapp_stop_streaming(struct smiapp_sensor
*sensor
)
1474 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1477 mutex_lock(&sensor
->mutex
);
1478 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1479 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY
);
1483 rval
= smiapp_call_quirk(sensor
, post_streamoff
);
1485 dev_err(&client
->dev
, "post_streamoff quirks failed\n");
1488 mutex_unlock(&sensor
->mutex
);
1492 /* -----------------------------------------------------------------------------
1493 * V4L2 subdev video operations
1496 static int smiapp_set_stream(struct v4l2_subdev
*subdev
, int enable
)
1498 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1501 if (sensor
->streaming
== enable
)
1505 sensor
->streaming
= true;
1506 rval
= smiapp_start_streaming(sensor
);
1508 sensor
->streaming
= false;
1510 rval
= smiapp_stop_streaming(sensor
);
1511 sensor
->streaming
= false;
1517 static int smiapp_enum_mbus_code(struct v4l2_subdev
*subdev
,
1518 struct v4l2_subdev_fh
*fh
,
1519 struct v4l2_subdev_mbus_code_enum
*code
)
1521 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1522 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1527 mutex_lock(&sensor
->mutex
);
1529 dev_err(&client
->dev
, "subdev %s, pad %d, index %d\n",
1530 subdev
->name
, code
->pad
, code
->index
);
1532 if (subdev
!= &sensor
->src
->sd
|| code
->pad
!= SMIAPP_PAD_SRC
) {
1536 code
->code
= sensor
->internal_csi_format
->code
;
1541 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1542 if (sensor
->mbus_frame_fmts
& (1 << i
))
1545 if (idx
== code
->index
) {
1546 code
->code
= smiapp_csi_data_formats
[i
].code
;
1547 dev_err(&client
->dev
, "found index %d, i %d, code %x\n",
1548 code
->index
, i
, code
->code
);
1555 mutex_unlock(&sensor
->mutex
);
1560 static u32
__smiapp_get_mbus_code(struct v4l2_subdev
*subdev
,
1563 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1565 if (subdev
== &sensor
->src
->sd
&& pad
== SMIAPP_PAD_SRC
)
1566 return sensor
->csi_format
->code
;
1568 return sensor
->internal_csi_format
->code
;
1571 static int __smiapp_get_format(struct v4l2_subdev
*subdev
,
1572 struct v4l2_subdev_fh
*fh
,
1573 struct v4l2_subdev_format
*fmt
)
1575 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1577 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1578 fmt
->format
= *v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1580 struct v4l2_rect
*r
;
1582 if (fmt
->pad
== ssd
->source_pad
)
1583 r
= &ssd
->crop
[ssd
->source_pad
];
1587 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1588 fmt
->format
.width
= r
->width
;
1589 fmt
->format
.height
= r
->height
;
1590 fmt
->format
.field
= V4L2_FIELD_NONE
;
1596 static int smiapp_get_format(struct v4l2_subdev
*subdev
,
1597 struct v4l2_subdev_fh
*fh
,
1598 struct v4l2_subdev_format
*fmt
)
1600 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1603 mutex_lock(&sensor
->mutex
);
1604 rval
= __smiapp_get_format(subdev
, fh
, fmt
);
1605 mutex_unlock(&sensor
->mutex
);
1610 static void smiapp_get_crop_compose(struct v4l2_subdev
*subdev
,
1611 struct v4l2_subdev_fh
*fh
,
1612 struct v4l2_rect
**crops
,
1613 struct v4l2_rect
**comps
, int which
)
1615 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1618 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1620 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++)
1621 crops
[i
] = &ssd
->crop
[i
];
1623 *comps
= &ssd
->compose
;
1626 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++) {
1627 crops
[i
] = v4l2_subdev_get_try_crop(fh
, i
);
1632 *comps
= v4l2_subdev_get_try_compose(fh
,
1639 /* Changes require propagation only on sink pad. */
1640 static void smiapp_propagate(struct v4l2_subdev
*subdev
,
1641 struct v4l2_subdev_fh
*fh
, int which
,
1644 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1645 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1646 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
1648 smiapp_get_crop_compose(subdev
, fh
, crops
, &comp
, which
);
1651 case V4L2_SEL_TGT_CROP
:
1652 comp
->width
= crops
[SMIAPP_PAD_SINK
]->width
;
1653 comp
->height
= crops
[SMIAPP_PAD_SINK
]->height
;
1654 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1655 if (ssd
== sensor
->scaler
) {
1658 SMIAPP_LIMIT_SCALER_N_MIN
];
1659 sensor
->scaling_mode
=
1660 SMIAPP_SCALING_MODE_NONE
;
1661 } else if (ssd
== sensor
->binner
) {
1662 sensor
->binning_horizontal
= 1;
1663 sensor
->binning_vertical
= 1;
1667 case V4L2_SEL_TGT_COMPOSE
:
1668 *crops
[SMIAPP_PAD_SRC
] = *comp
;
1675 static const struct smiapp_csi_data_format
1676 *smiapp_validate_csi_data_format(struct smiapp_sensor
*sensor
, u32 code
)
1678 const struct smiapp_csi_data_format
*csi_format
= sensor
->csi_format
;
1681 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1682 if (sensor
->mbus_frame_fmts
& (1 << i
)
1683 && smiapp_csi_data_formats
[i
].code
== code
)
1684 return &smiapp_csi_data_formats
[i
];
1690 static int smiapp_set_format(struct v4l2_subdev
*subdev
,
1691 struct v4l2_subdev_fh
*fh
,
1692 struct v4l2_subdev_format
*fmt
)
1694 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1695 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1696 struct v4l2_rect
*crops
[SMIAPP_PADS
];
1698 mutex_lock(&sensor
->mutex
);
1701 * Media bus code is changeable on src subdev's source pad. On
1702 * other source pads we just get format here.
1704 if (fmt
->pad
== ssd
->source_pad
) {
1705 u32 code
= fmt
->format
.code
;
1706 int rval
= __smiapp_get_format(subdev
, fh
, fmt
);
1707 bool range_changed
= false;
1710 if (!rval
&& subdev
== &sensor
->src
->sd
) {
1711 const struct smiapp_csi_data_format
*csi_format
=
1712 smiapp_validate_csi_data_format(sensor
, code
);
1714 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1715 if (csi_format
->width
!=
1716 sensor
->csi_format
->width
)
1717 range_changed
= true;
1719 sensor
->csi_format
= csi_format
;
1722 fmt
->format
.code
= csi_format
->code
;
1725 mutex_unlock(&sensor
->mutex
);
1726 if (rval
|| !range_changed
)
1729 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
1730 v4l2_ctrl_modify_range(
1731 sensor
->test_data
[i
],
1732 0, (1 << sensor
->csi_format
->width
) - 1, 1, 0);
1737 /* Sink pad. Width and height are changeable here. */
1738 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1739 fmt
->format
.width
&= ~1;
1740 fmt
->format
.height
&= ~1;
1741 fmt
->format
.field
= V4L2_FIELD_NONE
;
1744 clamp(fmt
->format
.width
,
1745 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
1746 sensor
->limits
[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE
]);
1747 fmt
->format
.height
=
1748 clamp(fmt
->format
.height
,
1749 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
1750 sensor
->limits
[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE
]);
1752 smiapp_get_crop_compose(subdev
, fh
, crops
, NULL
, fmt
->which
);
1754 crops
[ssd
->sink_pad
]->left
= 0;
1755 crops
[ssd
->sink_pad
]->top
= 0;
1756 crops
[ssd
->sink_pad
]->width
= fmt
->format
.width
;
1757 crops
[ssd
->sink_pad
]->height
= fmt
->format
.height
;
1758 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
1759 ssd
->sink_fmt
= *crops
[ssd
->sink_pad
];
1760 smiapp_propagate(subdev
, fh
, fmt
->which
,
1763 mutex_unlock(&sensor
->mutex
);
1769 * Calculate goodness of scaled image size compared to expected image
1770 * size and flags provided.
1772 #define SCALING_GOODNESS 100000
1773 #define SCALING_GOODNESS_EXTREME 100000000
1774 static int scaling_goodness(struct v4l2_subdev
*subdev
, int w
, int ask_w
,
1775 int h
, int ask_h
, u32 flags
)
1777 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1778 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1786 if (flags
& V4L2_SEL_FLAG_GE
) {
1788 val
-= SCALING_GOODNESS
;
1790 val
-= SCALING_GOODNESS
;
1793 if (flags
& V4L2_SEL_FLAG_LE
) {
1795 val
-= SCALING_GOODNESS
;
1797 val
-= SCALING_GOODNESS
;
1800 val
-= abs(w
- ask_w
);
1801 val
-= abs(h
- ask_h
);
1803 if (w
< sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
])
1804 val
-= SCALING_GOODNESS_EXTREME
;
1806 dev_dbg(&client
->dev
, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1807 w
, ask_h
, h
, ask_h
, val
);
1812 static void smiapp_set_compose_binner(struct v4l2_subdev
*subdev
,
1813 struct v4l2_subdev_fh
*fh
,
1814 struct v4l2_subdev_selection
*sel
,
1815 struct v4l2_rect
**crops
,
1816 struct v4l2_rect
*comp
)
1818 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1820 unsigned int binh
= 1, binv
= 1;
1821 int best
= scaling_goodness(
1823 crops
[SMIAPP_PAD_SINK
]->width
, sel
->r
.width
,
1824 crops
[SMIAPP_PAD_SINK
]->height
, sel
->r
.height
, sel
->flags
);
1826 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
1827 int this = scaling_goodness(
1829 crops
[SMIAPP_PAD_SINK
]->width
1830 / sensor
->binning_subtypes
[i
].horizontal
,
1832 crops
[SMIAPP_PAD_SINK
]->height
1833 / sensor
->binning_subtypes
[i
].vertical
,
1834 sel
->r
.height
, sel
->flags
);
1837 binh
= sensor
->binning_subtypes
[i
].horizontal
;
1838 binv
= sensor
->binning_subtypes
[i
].vertical
;
1842 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1843 sensor
->binning_vertical
= binv
;
1844 sensor
->binning_horizontal
= binh
;
1847 sel
->r
.width
= (crops
[SMIAPP_PAD_SINK
]->width
/ binh
) & ~1;
1848 sel
->r
.height
= (crops
[SMIAPP_PAD_SINK
]->height
/ binv
) & ~1;
1852 * Calculate best scaling ratio and mode for given output resolution.
1854 * Try all of these: horizontal ratio, vertical ratio and smallest
1855 * size possible (horizontally).
1857 * Also try whether horizontal scaler or full scaler gives a better
1860 static void smiapp_set_compose_scaler(struct v4l2_subdev
*subdev
,
1861 struct v4l2_subdev_fh
*fh
,
1862 struct v4l2_subdev_selection
*sel
,
1863 struct v4l2_rect
**crops
,
1864 struct v4l2_rect
*comp
)
1866 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1867 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1868 u32 min
, max
, a
, b
, max_m
;
1869 u32 scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
1870 int mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
1876 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
1877 crops
[SMIAPP_PAD_SINK
]->width
);
1878 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
1879 crops
[SMIAPP_PAD_SINK
]->height
);
1881 a
= crops
[SMIAPP_PAD_SINK
]->width
1882 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] / sel
->r
.width
;
1883 b
= crops
[SMIAPP_PAD_SINK
]->height
1884 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] / sel
->r
.height
;
1885 max_m
= crops
[SMIAPP_PAD_SINK
]->width
1886 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]
1887 / sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
];
1889 a
= clamp(a
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1890 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1891 b
= clamp(b
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1892 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1893 max_m
= clamp(max_m
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1894 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1896 dev_dbg(&client
->dev
, "scaling: a %d b %d max_m %d\n", a
, b
, max_m
);
1898 min
= min(max_m
, min(a
, b
));
1899 max
= min(max_m
, max(a
, b
));
1908 try[ntry
] = min
+ 1;
1911 try[ntry
] = max
+ 1;
1916 for (i
= 0; i
< ntry
; i
++) {
1917 int this = scaling_goodness(
1919 crops
[SMIAPP_PAD_SINK
]->width
1921 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
1923 crops
[SMIAPP_PAD_SINK
]->height
,
1927 dev_dbg(&client
->dev
, "trying factor %d (%d)\n", try[i
], i
);
1931 mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
1935 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
1936 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
1939 this = scaling_goodness(
1940 subdev
, crops
[SMIAPP_PAD_SINK
]->width
1942 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
1944 crops
[SMIAPP_PAD_SINK
]->height
1946 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
1952 mode
= SMIAPP_SCALING_MODE_BOTH
;
1958 (crops
[SMIAPP_PAD_SINK
]->width
1960 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]) & ~1;
1961 if (mode
== SMIAPP_SCALING_MODE_BOTH
)
1963 (crops
[SMIAPP_PAD_SINK
]->height
1965 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
])
1968 sel
->r
.height
= crops
[SMIAPP_PAD_SINK
]->height
;
1970 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1971 sensor
->scale_m
= scale_m
;
1972 sensor
->scaling_mode
= mode
;
1975 /* We're only called on source pads. This function sets scaling. */
1976 static int smiapp_set_compose(struct v4l2_subdev
*subdev
,
1977 struct v4l2_subdev_fh
*fh
,
1978 struct v4l2_subdev_selection
*sel
)
1980 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1981 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1982 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
1984 smiapp_get_crop_compose(subdev
, fh
, crops
, &comp
, sel
->which
);
1989 if (ssd
== sensor
->binner
)
1990 smiapp_set_compose_binner(subdev
, fh
, sel
, crops
, comp
);
1992 smiapp_set_compose_scaler(subdev
, fh
, sel
, crops
, comp
);
1995 smiapp_propagate(subdev
, fh
, sel
->which
,
1996 V4L2_SEL_TGT_COMPOSE
);
1998 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
1999 return smiapp_update_mode(sensor
);
2004 static int __smiapp_sel_supported(struct v4l2_subdev
*subdev
,
2005 struct v4l2_subdev_selection
*sel
)
2007 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2008 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2010 /* We only implement crop in three places. */
2011 switch (sel
->target
) {
2012 case V4L2_SEL_TGT_CROP
:
2013 case V4L2_SEL_TGT_CROP_BOUNDS
:
2014 if (ssd
== sensor
->pixel_array
2015 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2017 if (ssd
== sensor
->src
2018 && sel
->pad
== SMIAPP_PAD_SRC
)
2020 if (ssd
== sensor
->scaler
2021 && sel
->pad
== SMIAPP_PAD_SINK
2022 && sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
2023 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
)
2026 case V4L2_SEL_TGT_COMPOSE
:
2027 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2028 if (sel
->pad
== ssd
->source_pad
)
2030 if (ssd
== sensor
->binner
)
2032 if (ssd
== sensor
->scaler
2033 && sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2034 != SMIAPP_SCALING_CAPABILITY_NONE
)
2042 static int smiapp_set_crop(struct v4l2_subdev
*subdev
,
2043 struct v4l2_subdev_fh
*fh
,
2044 struct v4l2_subdev_selection
*sel
)
2046 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2047 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2048 struct v4l2_rect
*src_size
, *crops
[SMIAPP_PADS
];
2049 struct v4l2_rect _r
;
2051 smiapp_get_crop_compose(subdev
, fh
, crops
, NULL
, sel
->which
);
2053 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2054 if (sel
->pad
== ssd
->sink_pad
)
2055 src_size
= &ssd
->sink_fmt
;
2057 src_size
= &ssd
->compose
;
2059 if (sel
->pad
== ssd
->sink_pad
) {
2062 _r
.width
= v4l2_subdev_get_try_format(fh
, sel
->pad
)
2064 _r
.height
= v4l2_subdev_get_try_format(fh
, sel
->pad
)
2069 v4l2_subdev_get_try_compose(
2074 if (ssd
== sensor
->src
&& sel
->pad
== SMIAPP_PAD_SRC
) {
2079 sel
->r
.width
= min(sel
->r
.width
, src_size
->width
);
2080 sel
->r
.height
= min(sel
->r
.height
, src_size
->height
);
2082 sel
->r
.left
= min_t(int, sel
->r
.left
, src_size
->width
- sel
->r
.width
);
2083 sel
->r
.top
= min_t(int, sel
->r
.top
, src_size
->height
- sel
->r
.height
);
2085 *crops
[sel
->pad
] = sel
->r
;
2087 if (ssd
!= sensor
->pixel_array
&& sel
->pad
== SMIAPP_PAD_SINK
)
2088 smiapp_propagate(subdev
, fh
, sel
->which
,
2094 static int __smiapp_get_selection(struct v4l2_subdev
*subdev
,
2095 struct v4l2_subdev_fh
*fh
,
2096 struct v4l2_subdev_selection
*sel
)
2098 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2099 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2100 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2101 struct v4l2_rect sink_fmt
;
2104 ret
= __smiapp_sel_supported(subdev
, sel
);
2108 smiapp_get_crop_compose(subdev
, fh
, crops
, &comp
, sel
->which
);
2110 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2111 sink_fmt
= ssd
->sink_fmt
;
2113 struct v4l2_mbus_framefmt
*fmt
=
2114 v4l2_subdev_get_try_format(fh
, ssd
->sink_pad
);
2118 sink_fmt
.width
= fmt
->width
;
2119 sink_fmt
.height
= fmt
->height
;
2122 switch (sel
->target
) {
2123 case V4L2_SEL_TGT_CROP_BOUNDS
:
2124 if (ssd
== sensor
->pixel_array
) {
2126 sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2128 sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2129 } else if (sel
->pad
== ssd
->sink_pad
) {
2135 case V4L2_SEL_TGT_CROP
:
2136 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2137 sel
->r
= *crops
[sel
->pad
];
2139 case V4L2_SEL_TGT_COMPOSE
:
2147 static int smiapp_get_selection(struct v4l2_subdev
*subdev
,
2148 struct v4l2_subdev_fh
*fh
,
2149 struct v4l2_subdev_selection
*sel
)
2151 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2154 mutex_lock(&sensor
->mutex
);
2155 rval
= __smiapp_get_selection(subdev
, fh
, sel
);
2156 mutex_unlock(&sensor
->mutex
);
2160 static int smiapp_set_selection(struct v4l2_subdev
*subdev
,
2161 struct v4l2_subdev_fh
*fh
,
2162 struct v4l2_subdev_selection
*sel
)
2164 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2167 ret
= __smiapp_sel_supported(subdev
, sel
);
2171 mutex_lock(&sensor
->mutex
);
2173 sel
->r
.left
= max(0, sel
->r
.left
& ~1);
2174 sel
->r
.top
= max(0, sel
->r
.top
& ~1);
2175 sel
->r
.width
= SMIAPP_ALIGN_DIM(sel
->r
.width
, sel
->flags
);
2176 sel
->r
.height
= SMIAPP_ALIGN_DIM(sel
->r
.height
, sel
->flags
);
2178 sel
->r
.width
= max_t(unsigned int,
2179 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
2181 sel
->r
.height
= max_t(unsigned int,
2182 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
2185 switch (sel
->target
) {
2186 case V4L2_SEL_TGT_CROP
:
2187 ret
= smiapp_set_crop(subdev
, fh
, sel
);
2189 case V4L2_SEL_TGT_COMPOSE
:
2190 ret
= smiapp_set_compose(subdev
, fh
, sel
);
2196 mutex_unlock(&sensor
->mutex
);
2200 static int smiapp_get_skip_frames(struct v4l2_subdev
*subdev
, u32
*frames
)
2202 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2204 *frames
= sensor
->frame_skip
;
2208 /* -----------------------------------------------------------------------------
2213 smiapp_sysfs_nvm_read(struct device
*dev
, struct device_attribute
*attr
,
2216 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2217 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2218 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2219 unsigned int nbytes
;
2221 if (!sensor
->dev_init_done
)
2224 if (!sensor
->nvm_size
) {
2225 /* NVM not read yet - read it now */
2226 sensor
->nvm_size
= sensor
->platform_data
->nvm_size
;
2227 if (smiapp_set_power(subdev
, 1) < 0)
2229 if (smiapp_read_nvm(sensor
, sensor
->nvm
)) {
2230 dev_err(&client
->dev
, "nvm read failed\n");
2233 smiapp_set_power(subdev
, 0);
2236 * NVM is still way below a PAGE_SIZE, so we can safely
2237 * assume this for now.
2239 nbytes
= min_t(unsigned int, sensor
->nvm_size
, PAGE_SIZE
);
2240 memcpy(buf
, sensor
->nvm
, nbytes
);
2244 static DEVICE_ATTR(nvm
, S_IRUGO
, smiapp_sysfs_nvm_read
, NULL
);
2247 smiapp_sysfs_ident_read(struct device
*dev
, struct device_attribute
*attr
,
2250 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2251 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2252 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2254 return snprintf(buf
, PAGE_SIZE
, "%2.2x%4.4x%2.2x\n",
2255 minfo
->manufacturer_id
, minfo
->model_id
,
2256 minfo
->revision_number_major
) + 1;
2259 static DEVICE_ATTR(ident
, S_IRUGO
, smiapp_sysfs_ident_read
, NULL
);
2261 /* -----------------------------------------------------------------------------
2262 * V4L2 subdev core operations
2265 static int smiapp_identify_module(struct v4l2_subdev
*subdev
)
2267 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2268 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2269 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2273 minfo
->name
= SMIAPP_NAME
;
2276 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MANUFACTURER_ID
,
2277 &minfo
->manufacturer_id
);
2279 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U16_MODEL_ID
,
2282 rval
= smiapp_read_8only(sensor
,
2283 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR
,
2284 &minfo
->revision_number_major
);
2286 rval
= smiapp_read_8only(sensor
,
2287 SMIAPP_REG_U8_REVISION_NUMBER_MINOR
,
2288 &minfo
->revision_number_minor
);
2290 rval
= smiapp_read_8only(sensor
,
2291 SMIAPP_REG_U8_MODULE_DATE_YEAR
,
2292 &minfo
->module_year
);
2294 rval
= smiapp_read_8only(sensor
,
2295 SMIAPP_REG_U8_MODULE_DATE_MONTH
,
2296 &minfo
->module_month
);
2298 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MODULE_DATE_DAY
,
2299 &minfo
->module_day
);
2303 rval
= smiapp_read_8only(sensor
,
2304 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID
,
2305 &minfo
->sensor_manufacturer_id
);
2307 rval
= smiapp_read_8only(sensor
,
2308 SMIAPP_REG_U16_SENSOR_MODEL_ID
,
2309 &minfo
->sensor_model_id
);
2311 rval
= smiapp_read_8only(sensor
,
2312 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER
,
2313 &minfo
->sensor_revision_number
);
2315 rval
= smiapp_read_8only(sensor
,
2316 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION
,
2317 &minfo
->sensor_firmware_version
);
2321 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIA_VERSION
,
2322 &minfo
->smia_version
);
2324 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIAPP_VERSION
,
2325 &minfo
->smiapp_version
);
2328 dev_err(&client
->dev
, "sensor detection failed\n");
2332 dev_dbg(&client
->dev
, "module 0x%2.2x-0x%4.4x\n",
2333 minfo
->manufacturer_id
, minfo
->model_id
);
2335 dev_dbg(&client
->dev
,
2336 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2337 minfo
->revision_number_major
, minfo
->revision_number_minor
,
2338 minfo
->module_year
, minfo
->module_month
, minfo
->module_day
);
2340 dev_dbg(&client
->dev
, "sensor 0x%2.2x-0x%4.4x\n",
2341 minfo
->sensor_manufacturer_id
, minfo
->sensor_model_id
);
2343 dev_dbg(&client
->dev
,
2344 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2345 minfo
->sensor_revision_number
, minfo
->sensor_firmware_version
);
2347 dev_dbg(&client
->dev
, "smia version %2.2d smiapp version %2.2d\n",
2348 minfo
->smia_version
, minfo
->smiapp_version
);
2351 * Some modules have bad data in the lvalues below. Hope the
2352 * rvalues have better stuff. The lvalues are module
2353 * parameters whereas the rvalues are sensor parameters.
2355 if (!minfo
->manufacturer_id
&& !minfo
->model_id
) {
2356 minfo
->manufacturer_id
= minfo
->sensor_manufacturer_id
;
2357 minfo
->model_id
= minfo
->sensor_model_id
;
2358 minfo
->revision_number_major
= minfo
->sensor_revision_number
;
2361 for (i
= 0; i
< ARRAY_SIZE(smiapp_module_idents
); i
++) {
2362 if (smiapp_module_idents
[i
].manufacturer_id
2363 != minfo
->manufacturer_id
)
2365 if (smiapp_module_idents
[i
].model_id
!= minfo
->model_id
)
2367 if (smiapp_module_idents
[i
].flags
2368 & SMIAPP_MODULE_IDENT_FLAG_REV_LE
) {
2369 if (smiapp_module_idents
[i
].revision_number_major
2370 < minfo
->revision_number_major
)
2373 if (smiapp_module_idents
[i
].revision_number_major
2374 != minfo
->revision_number_major
)
2378 minfo
->name
= smiapp_module_idents
[i
].name
;
2379 minfo
->quirk
= smiapp_module_idents
[i
].quirk
;
2383 if (i
>= ARRAY_SIZE(smiapp_module_idents
))
2384 dev_warn(&client
->dev
,
2385 "no quirks for this module; let's hope it's fully compliant\n");
2387 dev_dbg(&client
->dev
, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2388 minfo
->name
, minfo
->manufacturer_id
, minfo
->model_id
,
2389 minfo
->revision_number_major
);
2391 strlcpy(subdev
->name
, sensor
->minfo
.name
, sizeof(subdev
->name
));
2396 static const struct v4l2_subdev_ops smiapp_ops
;
2397 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
;
2398 static const struct media_entity_operations smiapp_entity_ops
;
2400 static int smiapp_registered(struct v4l2_subdev
*subdev
)
2402 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2403 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2404 struct smiapp_pll
*pll
= &sensor
->pll
;
2405 struct smiapp_subdev
*last
= NULL
;
2410 sensor
->vana
= devm_regulator_get(&client
->dev
, "vana");
2411 if (IS_ERR(sensor
->vana
)) {
2412 dev_err(&client
->dev
, "could not get regulator for vana\n");
2413 return PTR_ERR(sensor
->vana
);
2416 if (!sensor
->platform_data
->set_xclk
) {
2417 sensor
->ext_clk
= devm_clk_get(&client
->dev
, "ext_clk");
2418 if (IS_ERR(sensor
->ext_clk
)) {
2419 dev_err(&client
->dev
, "could not get clock\n");
2420 return PTR_ERR(sensor
->ext_clk
);
2423 rval
= clk_set_rate(sensor
->ext_clk
,
2424 sensor
->platform_data
->ext_clk
);
2426 dev_err(&client
->dev
,
2427 "unable to set clock freq to %u\n",
2428 sensor
->platform_data
->ext_clk
);
2433 if (gpio_is_valid(sensor
->platform_data
->xshutdown
)) {
2434 rval
= devm_gpio_request_one(
2435 &client
->dev
, sensor
->platform_data
->xshutdown
, 0,
2436 "SMIA++ xshutdown");
2438 dev_err(&client
->dev
,
2439 "unable to acquire reset gpio %d\n",
2440 sensor
->platform_data
->xshutdown
);
2445 rval
= smiapp_power_on(sensor
);
2449 rval
= smiapp_identify_module(subdev
);
2455 rval
= smiapp_get_all_limits(sensor
);
2462 * Handle Sensor Module orientation on the board.
2464 * The application of H-FLIP and V-FLIP on the sensor is modified by
2465 * the sensor orientation on the board.
2467 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2468 * both H-FLIP and V-FLIP for normal operation which also implies
2469 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2470 * controls will need to be internally inverted.
2472 * Rotation also changes the bayer pattern.
2474 if (sensor
->platform_data
->module_board_orient
==
2475 SMIAPP_MODULE_BOARD_ORIENT_180
)
2476 sensor
->hvflip_inv_mask
= SMIAPP_IMAGE_ORIENTATION_HFLIP
|
2477 SMIAPP_IMAGE_ORIENTATION_VFLIP
;
2479 rval
= smiapp_call_quirk(sensor
, limits
);
2481 dev_err(&client
->dev
, "limits quirks failed\n");
2485 rval
= smiapp_get_mbus_formats(sensor
);
2491 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
]) {
2494 rval
= smiapp_read(sensor
,
2495 SMIAPP_REG_U8_BINNING_SUBTYPES
, &val
);
2500 sensor
->nbinning_subtypes
= min_t(u8
, val
,
2501 SMIAPP_BINNING_SUBTYPES
);
2503 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
2505 sensor
, SMIAPP_REG_U8_BINNING_TYPE_n(i
), &val
);
2510 sensor
->binning_subtypes
[i
] =
2511 *(struct smiapp_binning_subtype
*)&val
;
2513 dev_dbg(&client
->dev
, "binning %xx%x\n",
2514 sensor
->binning_subtypes
[i
].horizontal
,
2515 sensor
->binning_subtypes
[i
].vertical
);
2518 sensor
->binning_horizontal
= 1;
2519 sensor
->binning_vertical
= 1;
2521 if (device_create_file(&client
->dev
, &dev_attr_ident
) != 0) {
2522 dev_err(&client
->dev
, "sysfs ident entry creation failed\n");
2526 /* SMIA++ NVM initialization - it will be read from the sensor
2527 * when it is first requested by userspace.
2529 if (sensor
->minfo
.smiapp_version
&& sensor
->platform_data
->nvm_size
) {
2530 sensor
->nvm
= devm_kzalloc(&client
->dev
,
2531 sensor
->platform_data
->nvm_size
, GFP_KERNEL
);
2532 if (sensor
->nvm
== NULL
) {
2533 dev_err(&client
->dev
, "nvm buf allocation failed\n");
2535 goto out_ident_release
;
2538 if (device_create_file(&client
->dev
, &dev_attr_nvm
) != 0) {
2539 dev_err(&client
->dev
, "sysfs nvm entry failed\n");
2541 goto out_ident_release
;
2545 /* We consider this as profile 0 sensor if any of these are zero. */
2546 if (!sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV
] ||
2547 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV
] ||
2548 !sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV
] ||
2549 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV
]) {
2550 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_0
;
2551 } else if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2552 != SMIAPP_SCALING_CAPABILITY_NONE
) {
2553 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2554 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2555 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_1
;
2557 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_2
;
2558 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
2559 sensor
->ssds_used
++;
2560 } else if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
2561 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
2562 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
2563 sensor
->ssds_used
++;
2565 sensor
->binner
= &sensor
->ssds
[sensor
->ssds_used
];
2566 sensor
->ssds_used
++;
2567 sensor
->pixel_array
= &sensor
->ssds
[sensor
->ssds_used
];
2568 sensor
->ssds_used
++;
2570 sensor
->scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
2572 for (i
= 0; i
< SMIAPP_SUBDEVS
; i
++) {
2574 struct smiapp_subdev
*ssd
;
2576 } const __this
[] = {
2577 { sensor
->scaler
, "scaler", },
2578 { sensor
->binner
, "binner", },
2579 { sensor
->pixel_array
, "pixel array", },
2580 }, *_this
= &__this
[i
];
2581 struct smiapp_subdev
*this = _this
->ssd
;
2586 if (this != sensor
->src
)
2587 v4l2_subdev_init(&this->sd
, &smiapp_ops
);
2589 this->sensor
= sensor
;
2591 if (this == sensor
->pixel_array
) {
2595 this->source_pad
= 1;
2598 snprintf(this->sd
.name
,
2599 sizeof(this->sd
.name
), "%s %s %d-%4.4x",
2600 sensor
->minfo
.name
, _this
->name
,
2601 i2c_adapter_id(client
->adapter
), client
->addr
);
2603 this->sink_fmt
.width
=
2604 sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2605 this->sink_fmt
.height
=
2606 sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2607 this->compose
.width
= this->sink_fmt
.width
;
2608 this->compose
.height
= this->sink_fmt
.height
;
2609 this->crop
[this->source_pad
] = this->compose
;
2610 this->pads
[this->source_pad
].flags
= MEDIA_PAD_FL_SOURCE
;
2611 if (this != sensor
->pixel_array
) {
2612 this->crop
[this->sink_pad
] = this->compose
;
2613 this->pads
[this->sink_pad
].flags
= MEDIA_PAD_FL_SINK
;
2616 this->sd
.entity
.ops
= &smiapp_entity_ops
;
2623 this->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2624 this->sd
.internal_ops
= &smiapp_internal_ops
;
2625 this->sd
.owner
= THIS_MODULE
;
2626 v4l2_set_subdevdata(&this->sd
, client
);
2628 rval
= media_entity_init(&this->sd
.entity
,
2629 this->npads
, this->pads
, 0);
2631 dev_err(&client
->dev
,
2632 "media_entity_init failed\n");
2633 goto out_nvm_release
;
2636 rval
= media_entity_create_link(&this->sd
.entity
,
2640 MEDIA_LNK_FL_ENABLED
|
2641 MEDIA_LNK_FL_IMMUTABLE
);
2643 dev_err(&client
->dev
,
2644 "media_entity_create_link failed\n");
2645 goto out_nvm_release
;
2648 rval
= v4l2_device_register_subdev(sensor
->src
->sd
.v4l2_dev
,
2651 dev_err(&client
->dev
,
2652 "v4l2_device_register_subdev failed\n");
2653 goto out_nvm_release
;
2659 dev_dbg(&client
->dev
, "profile %d\n", sensor
->minfo
.smiapp_profile
);
2661 sensor
->pixel_array
->sd
.entity
.type
= MEDIA_ENT_T_V4L2_SUBDEV_SENSOR
;
2664 smiapp_read_frame_fmt(sensor
);
2665 rval
= smiapp_init_controls(sensor
);
2667 goto out_nvm_release
;
2669 /* prepare PLL configuration input values */
2670 pll
->bus_type
= SMIAPP_PLL_BUS_TYPE_CSI2
;
2671 pll
->csi2
.lanes
= sensor
->platform_data
->lanes
;
2672 pll
->ext_clk_freq_hz
= sensor
->platform_data
->ext_clk
;
2673 pll
->flags
= smiapp_call_quirk(sensor
, pll_flags
);
2675 /* Profile 0 sensors have no separate OP clock branch. */
2676 if (sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
2677 pll
->flags
|= SMIAPP_PLL_FLAG_NO_OP_CLOCKS
;
2678 pll
->scale_n
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
2680 rval
= smiapp_update_mode(sensor
);
2682 dev_err(&client
->dev
, "update mode failed\n");
2683 goto out_nvm_release
;
2686 sensor
->streaming
= false;
2687 sensor
->dev_init_done
= true;
2689 /* check flash capability */
2690 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FLASH_MODE_CAPABILITY
, &tmp
);
2691 sensor
->flash_capability
= tmp
;
2693 goto out_nvm_release
;
2695 smiapp_power_off(sensor
);
2700 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2703 device_remove_file(&client
->dev
, &dev_attr_ident
);
2706 smiapp_power_off(sensor
);
2710 static int smiapp_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2712 struct smiapp_subdev
*ssd
= to_smiapp_subdev(sd
);
2713 struct smiapp_sensor
*sensor
= ssd
->sensor
;
2715 smiapp_csi_data_formats
[smiapp_pixel_order(sensor
)].code
;
2718 mutex_lock(&sensor
->mutex
);
2720 for (i
= 0; i
< ssd
->npads
; i
++) {
2721 struct v4l2_mbus_framefmt
*try_fmt
=
2722 v4l2_subdev_get_try_format(fh
, i
);
2723 struct v4l2_rect
*try_crop
= v4l2_subdev_get_try_crop(fh
, i
);
2724 struct v4l2_rect
*try_comp
;
2726 try_fmt
->width
= sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2727 try_fmt
->height
= sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2728 try_fmt
->code
= mbus_code
;
2729 try_fmt
->field
= V4L2_FIELD_NONE
;
2733 try_crop
->width
= try_fmt
->width
;
2734 try_crop
->height
= try_fmt
->height
;
2736 if (ssd
!= sensor
->pixel_array
)
2739 try_comp
= v4l2_subdev_get_try_compose(fh
, i
);
2740 *try_comp
= *try_crop
;
2743 mutex_unlock(&sensor
->mutex
);
2745 return smiapp_set_power(sd
, 1);
2748 static int smiapp_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2750 return smiapp_set_power(sd
, 0);
2753 static const struct v4l2_subdev_video_ops smiapp_video_ops
= {
2754 .s_stream
= smiapp_set_stream
,
2757 static const struct v4l2_subdev_core_ops smiapp_core_ops
= {
2758 .s_power
= smiapp_set_power
,
2761 static const struct v4l2_subdev_pad_ops smiapp_pad_ops
= {
2762 .enum_mbus_code
= smiapp_enum_mbus_code
,
2763 .get_fmt
= smiapp_get_format
,
2764 .set_fmt
= smiapp_set_format
,
2765 .get_selection
= smiapp_get_selection
,
2766 .set_selection
= smiapp_set_selection
,
2769 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops
= {
2770 .g_skip_frames
= smiapp_get_skip_frames
,
2773 static const struct v4l2_subdev_ops smiapp_ops
= {
2774 .core
= &smiapp_core_ops
,
2775 .video
= &smiapp_video_ops
,
2776 .pad
= &smiapp_pad_ops
,
2777 .sensor
= &smiapp_sensor_ops
,
2780 static const struct media_entity_operations smiapp_entity_ops
= {
2781 .link_validate
= v4l2_subdev_link_validate
,
2784 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops
= {
2785 .registered
= smiapp_registered
,
2786 .open
= smiapp_open
,
2787 .close
= smiapp_close
,
2790 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
= {
2791 .open
= smiapp_open
,
2792 .close
= smiapp_close
,
2795 /* -----------------------------------------------------------------------------
2801 static int smiapp_suspend(struct device
*dev
)
2803 struct i2c_client
*client
= to_i2c_client(dev
);
2804 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2805 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2808 BUG_ON(mutex_is_locked(&sensor
->mutex
));
2810 if (sensor
->power_count
== 0)
2813 if (sensor
->streaming
)
2814 smiapp_stop_streaming(sensor
);
2816 streaming
= sensor
->streaming
;
2818 smiapp_power_off(sensor
);
2820 /* save state for resume */
2821 sensor
->streaming
= streaming
;
2826 static int smiapp_resume(struct device
*dev
)
2828 struct i2c_client
*client
= to_i2c_client(dev
);
2829 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2830 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2833 if (sensor
->power_count
== 0)
2836 rval
= smiapp_power_on(sensor
);
2840 if (sensor
->streaming
)
2841 rval
= smiapp_start_streaming(sensor
);
2848 #define smiapp_suspend NULL
2849 #define smiapp_resume NULL
2851 #endif /* CONFIG_PM */
2853 static int smiapp_probe(struct i2c_client
*client
,
2854 const struct i2c_device_id
*devid
)
2856 struct smiapp_sensor
*sensor
;
2858 if (client
->dev
.platform_data
== NULL
)
2861 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
2865 sensor
->platform_data
= client
->dev
.platform_data
;
2866 mutex_init(&sensor
->mutex
);
2867 mutex_init(&sensor
->power_mutex
);
2868 sensor
->src
= &sensor
->ssds
[sensor
->ssds_used
];
2870 v4l2_i2c_subdev_init(&sensor
->src
->sd
, client
, &smiapp_ops
);
2871 sensor
->src
->sd
.internal_ops
= &smiapp_internal_src_ops
;
2872 sensor
->src
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2873 sensor
->src
->sensor
= sensor
;
2875 sensor
->src
->pads
[0].flags
= MEDIA_PAD_FL_SOURCE
;
2876 return media_entity_init(&sensor
->src
->sd
.entity
, 2,
2877 sensor
->src
->pads
, 0);
2880 static int smiapp_remove(struct i2c_client
*client
)
2882 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2883 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2886 if (sensor
->power_count
) {
2887 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
2888 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
2889 if (sensor
->platform_data
->set_xclk
)
2890 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
2892 clk_disable_unprepare(sensor
->ext_clk
);
2893 sensor
->power_count
= 0;
2896 device_remove_file(&client
->dev
, &dev_attr_ident
);
2898 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2900 for (i
= 0; i
< sensor
->ssds_used
; i
++) {
2901 v4l2_device_unregister_subdev(&sensor
->ssds
[i
].sd
);
2902 media_entity_cleanup(&sensor
->ssds
[i
].sd
.entity
);
2904 smiapp_free_controls(sensor
);
2909 static const struct i2c_device_id smiapp_id_table
[] = {
2913 MODULE_DEVICE_TABLE(i2c
, smiapp_id_table
);
2915 static const struct dev_pm_ops smiapp_pm_ops
= {
2916 .suspend
= smiapp_suspend
,
2917 .resume
= smiapp_resume
,
2920 static struct i2c_driver smiapp_i2c_driver
= {
2922 .name
= SMIAPP_NAME
,
2923 .pm
= &smiapp_pm_ops
,
2925 .probe
= smiapp_probe
,
2926 .remove
= smiapp_remove
,
2927 .id_table
= smiapp_id_table
,
2930 module_i2c_driver(smiapp_i2c_driver
);
2932 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
2933 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
2934 MODULE_LICENSE("GPL");