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/of_gpio.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/slab.h>
31 #include <linux/smiapp.h>
32 #include <linux/v4l2-mediabus.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-of.h>
38 #define SMIAPP_ALIGN_DIM(dim, flags) \
39 ((flags) & V4L2_SEL_FLAG_GE \
44 * smiapp_module_idents - supported camera modules
46 static const struct smiapp_module_ident smiapp_module_idents
[] = {
47 SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"),
48 SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"),
49 SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
50 SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
51 SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
52 SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk
),
53 SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
54 SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
55 SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk
),
56 SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk
),
57 SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk
),
62 * Dynamic Capability Identification
66 static int smiapp_read_frame_fmt(struct smiapp_sensor
*sensor
)
68 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
69 u32 fmt_model_type
, fmt_model_subtype
, ncol_desc
, nrow_desc
;
73 int embedded_start
= -1, embedded_end
= -1;
76 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE
,
81 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE
,
86 ncol_desc
= (fmt_model_subtype
87 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK
)
88 >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT
;
89 nrow_desc
= fmt_model_subtype
90 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK
;
92 dev_dbg(&client
->dev
, "format_model_type %s\n",
93 fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
95 fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
96 ? "4 byte" : "is simply bad");
98 for (i
= 0; i
< ncol_desc
+ nrow_desc
; i
++) {
105 if (fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
) {
108 SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i
),
115 & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK
)
116 >> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT
;
117 pixels
= desc
& SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK
;
118 } else if (fmt_model_type
119 == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
) {
122 SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i
),
129 & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK
)
130 >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT
;
131 pixels
= desc
& SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK
;
133 dev_dbg(&client
->dev
,
134 "invalid frame format model type %d\n",
145 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED
:
148 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY
:
151 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK
:
154 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK
:
157 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
:
162 dev_dbg(&client
->dev
, "pixelcode %d\n", pixelcode
);
166 dev_dbg(&client
->dev
, "%s pixels: %d %s\n",
167 what
, pixels
, which
);
172 /* Handle row descriptors */
174 == SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED
) {
175 embedded_start
= line_count
;
177 if (pixelcode
== SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
178 || pixels
>= sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES
] / 2)
179 image_start
= line_count
;
180 if (embedded_start
!= -1 && embedded_end
== -1)
181 embedded_end
= line_count
;
183 line_count
+= pixels
;
186 if (embedded_start
== -1 || embedded_end
== -1) {
191 sensor
->image_start
= image_start
;
193 dev_dbg(&client
->dev
, "embedded data from lines %d to %d\n",
194 embedded_start
, embedded_end
);
195 dev_dbg(&client
->dev
, "image data starts at line %d\n", image_start
);
200 static int smiapp_pll_configure(struct smiapp_sensor
*sensor
)
202 struct smiapp_pll
*pll
= &sensor
->pll
;
206 sensor
, SMIAPP_REG_U16_VT_PIX_CLK_DIV
, pll
->vt
.pix_clk_div
);
211 sensor
, SMIAPP_REG_U16_VT_SYS_CLK_DIV
, pll
->vt
.sys_clk_div
);
216 sensor
, SMIAPP_REG_U16_PRE_PLL_CLK_DIV
, pll
->pre_pll_clk_div
);
221 sensor
, SMIAPP_REG_U16_PLL_MULTIPLIER
, pll
->pll_multiplier
);
225 /* Lane op clock ratio does not apply here. */
227 sensor
, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS
,
228 DIV_ROUND_UP(pll
->op
.sys_clk_freq_hz
, 1000000 / 256 / 256));
229 if (rval
< 0 || sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
233 sensor
, SMIAPP_REG_U16_OP_PIX_CLK_DIV
, pll
->op
.pix_clk_div
);
238 sensor
, SMIAPP_REG_U16_OP_SYS_CLK_DIV
, pll
->op
.sys_clk_div
);
241 static int smiapp_pll_try(struct smiapp_sensor
*sensor
,
242 struct smiapp_pll
*pll
)
244 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
245 struct smiapp_pll_limits lim
= {
246 .min_pre_pll_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV
],
247 .max_pre_pll_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV
],
248 .min_pll_ip_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ
],
249 .max_pll_ip_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ
],
250 .min_pll_multiplier
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER
],
251 .max_pll_multiplier
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER
],
252 .min_pll_op_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ
],
253 .max_pll_op_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ
],
255 .op
.min_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV
],
256 .op
.max_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV
],
257 .op
.min_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV
],
258 .op
.max_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV
],
259 .op
.min_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ
],
260 .op
.max_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ
],
261 .op
.min_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ
],
262 .op
.max_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ
],
264 .vt
.min_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV
],
265 .vt
.max_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV
],
266 .vt
.min_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV
],
267 .vt
.max_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV
],
268 .vt
.min_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ
],
269 .vt
.max_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ
],
270 .vt
.min_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ
],
271 .vt
.max_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ
],
273 .min_line_length_pck_bin
= sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
],
274 .min_line_length_pck
= sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK
],
277 return smiapp_pll_calculate(&client
->dev
, &lim
, pll
);
280 static int smiapp_pll_update(struct smiapp_sensor
*sensor
)
282 struct smiapp_pll
*pll
= &sensor
->pll
;
285 pll
->binning_horizontal
= sensor
->binning_horizontal
;
286 pll
->binning_vertical
= sensor
->binning_vertical
;
288 sensor
->link_freq
->qmenu_int
[sensor
->link_freq
->val
];
289 pll
->scale_m
= sensor
->scale_m
;
290 pll
->bits_per_pixel
= sensor
->csi_format
->compressed
;
292 rval
= smiapp_pll_try(sensor
, pll
);
296 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_parray
,
297 pll
->pixel_rate_pixel_array
);
298 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_csi
, pll
->pixel_rate_csi
);
306 * V4L2 Controls handling
310 static void __smiapp_update_exposure_limits(struct smiapp_sensor
*sensor
)
312 struct v4l2_ctrl
*ctrl
= sensor
->exposure
;
315 max
= sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
316 + sensor
->vblank
->val
317 - sensor
->limits
[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN
];
319 __v4l2_ctrl_modify_range(ctrl
, ctrl
->minimum
, max
, ctrl
->step
, max
);
325 * 1. Bits-per-pixel, descending.
326 * 2. Bits-per-pixel compressed, descending.
327 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
328 * orders must be defined.
330 static const struct smiapp_csi_data_format smiapp_csi_data_formats
[] = {
331 { MEDIA_BUS_FMT_SGRBG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GRBG
, },
332 { MEDIA_BUS_FMT_SRGGB12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_RGGB
, },
333 { MEDIA_BUS_FMT_SBGGR12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_BGGR
, },
334 { MEDIA_BUS_FMT_SGBRG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GBRG
, },
335 { MEDIA_BUS_FMT_SGRBG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GRBG
, },
336 { MEDIA_BUS_FMT_SRGGB10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_RGGB
, },
337 { MEDIA_BUS_FMT_SBGGR10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_BGGR
, },
338 { MEDIA_BUS_FMT_SGBRG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GBRG
, },
339 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
340 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
341 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
342 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
343 { MEDIA_BUS_FMT_SGRBG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
344 { MEDIA_BUS_FMT_SRGGB8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
345 { MEDIA_BUS_FMT_SBGGR8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
346 { MEDIA_BUS_FMT_SGBRG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
349 static const char *pixel_order_str
[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
351 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
352 - (unsigned long)smiapp_csi_data_formats) \
353 / sizeof(*smiapp_csi_data_formats))
355 static u32
smiapp_pixel_order(struct smiapp_sensor
*sensor
)
357 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
361 if (sensor
->hflip
->val
)
362 flip
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
364 if (sensor
->vflip
->val
)
365 flip
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
368 flip
^= sensor
->hvflip_inv_mask
;
370 dev_dbg(&client
->dev
, "flip %d\n", flip
);
371 return sensor
->default_pixel_order
^ flip
;
374 static void smiapp_update_mbus_formats(struct smiapp_sensor
*sensor
)
376 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
377 unsigned int csi_format_idx
=
378 to_csi_format_idx(sensor
->csi_format
) & ~3;
379 unsigned int internal_csi_format_idx
=
380 to_csi_format_idx(sensor
->internal_csi_format
) & ~3;
381 unsigned int pixel_order
= smiapp_pixel_order(sensor
);
383 sensor
->mbus_frame_fmts
=
384 sensor
->default_mbus_frame_fmts
<< pixel_order
;
386 &smiapp_csi_data_formats
[csi_format_idx
+ pixel_order
];
387 sensor
->internal_csi_format
=
388 &smiapp_csi_data_formats
[internal_csi_format_idx
391 BUG_ON(max(internal_csi_format_idx
, csi_format_idx
) + pixel_order
392 >= ARRAY_SIZE(smiapp_csi_data_formats
));
394 dev_dbg(&client
->dev
, "new pixel order %s\n",
395 pixel_order_str
[pixel_order
]);
398 static const char * const smiapp_test_patterns
[] = {
401 "Eight Vertical Colour Bars",
402 "Colour Bars With Fade to Grey",
403 "Pseudorandom Sequence (PN9)",
406 static int smiapp_set_ctrl(struct v4l2_ctrl
*ctrl
)
408 struct smiapp_sensor
*sensor
=
409 container_of(ctrl
->handler
, struct smiapp_subdev
, ctrl_handler
)
416 case V4L2_CID_ANALOGUE_GAIN
:
419 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL
, ctrl
->val
);
421 case V4L2_CID_EXPOSURE
:
424 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME
, ctrl
->val
);
428 if (sensor
->streaming
)
431 if (sensor
->hflip
->val
)
432 orient
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
434 if (sensor
->vflip
->val
)
435 orient
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
437 orient
^= sensor
->hvflip_inv_mask
;
438 rval
= smiapp_write(sensor
,
439 SMIAPP_REG_U8_IMAGE_ORIENTATION
,
444 smiapp_update_mbus_formats(sensor
);
448 case V4L2_CID_VBLANK
:
449 exposure
= sensor
->exposure
->val
;
451 __smiapp_update_exposure_limits(sensor
);
453 if (exposure
> sensor
->exposure
->maximum
) {
454 sensor
->exposure
->val
=
455 sensor
->exposure
->maximum
;
456 rval
= smiapp_set_ctrl(
463 sensor
, SMIAPP_REG_U16_FRAME_LENGTH_LINES
,
464 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
467 case V4L2_CID_HBLANK
:
469 sensor
, SMIAPP_REG_U16_LINE_LENGTH_PCK
,
470 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
473 case V4L2_CID_LINK_FREQ
:
474 if (sensor
->streaming
)
477 return smiapp_pll_update(sensor
);
479 case V4L2_CID_TEST_PATTERN
: {
482 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
484 sensor
->test_data
[i
],
486 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR
);
489 sensor
, SMIAPP_REG_U16_TEST_PATTERN_MODE
, ctrl
->val
);
492 case V4L2_CID_TEST_PATTERN_RED
:
494 sensor
, SMIAPP_REG_U16_TEST_DATA_RED
, ctrl
->val
);
496 case V4L2_CID_TEST_PATTERN_GREENR
:
498 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENR
, ctrl
->val
);
500 case V4L2_CID_TEST_PATTERN_BLUE
:
502 sensor
, SMIAPP_REG_U16_TEST_DATA_BLUE
, ctrl
->val
);
504 case V4L2_CID_TEST_PATTERN_GREENB
:
506 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENB
, ctrl
->val
);
508 case V4L2_CID_PIXEL_RATE
:
509 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
517 static const struct v4l2_ctrl_ops smiapp_ctrl_ops
= {
518 .s_ctrl
= smiapp_set_ctrl
,
521 static int smiapp_init_controls(struct smiapp_sensor
*sensor
)
523 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
526 rval
= v4l2_ctrl_handler_init(&sensor
->pixel_array
->ctrl_handler
, 12);
530 sensor
->pixel_array
->ctrl_handler
.lock
= &sensor
->mutex
;
532 sensor
->analog_gain
= v4l2_ctrl_new_std(
533 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
534 V4L2_CID_ANALOGUE_GAIN
,
535 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN
],
536 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX
],
537 max(sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP
], 1U),
538 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN
]);
540 /* Exposure limits will be updated soon, use just something here. */
541 sensor
->exposure
= v4l2_ctrl_new_std(
542 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
543 V4L2_CID_EXPOSURE
, 0, 0, 1, 0);
545 sensor
->hflip
= v4l2_ctrl_new_std(
546 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
547 V4L2_CID_HFLIP
, 0, 1, 1, 0);
548 sensor
->vflip
= v4l2_ctrl_new_std(
549 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
550 V4L2_CID_VFLIP
, 0, 1, 1, 0);
552 sensor
->vblank
= v4l2_ctrl_new_std(
553 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
554 V4L2_CID_VBLANK
, 0, 1, 1, 0);
557 sensor
->vblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
559 sensor
->hblank
= v4l2_ctrl_new_std(
560 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
561 V4L2_CID_HBLANK
, 0, 1, 1, 0);
564 sensor
->hblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
566 sensor
->pixel_rate_parray
= v4l2_ctrl_new_std(
567 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
568 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
570 v4l2_ctrl_new_std_menu_items(&sensor
->pixel_array
->ctrl_handler
,
571 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
572 ARRAY_SIZE(smiapp_test_patterns
) - 1,
573 0, 0, smiapp_test_patterns
);
575 if (sensor
->pixel_array
->ctrl_handler
.error
) {
576 dev_err(&client
->dev
,
577 "pixel array controls initialization failed (%d)\n",
578 sensor
->pixel_array
->ctrl_handler
.error
);
579 return sensor
->pixel_array
->ctrl_handler
.error
;
582 sensor
->pixel_array
->sd
.ctrl_handler
=
583 &sensor
->pixel_array
->ctrl_handler
;
585 v4l2_ctrl_cluster(2, &sensor
->hflip
);
587 rval
= v4l2_ctrl_handler_init(&sensor
->src
->ctrl_handler
, 0);
591 sensor
->src
->ctrl_handler
.lock
= &sensor
->mutex
;
593 sensor
->pixel_rate_csi
= v4l2_ctrl_new_std(
594 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
595 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
597 if (sensor
->src
->ctrl_handler
.error
) {
598 dev_err(&client
->dev
,
599 "src controls initialization failed (%d)\n",
600 sensor
->src
->ctrl_handler
.error
);
601 return sensor
->src
->ctrl_handler
.error
;
604 sensor
->src
->sd
.ctrl_handler
= &sensor
->src
->ctrl_handler
;
610 * For controls that require information on available media bus codes
611 * and linke frequencies.
613 static int smiapp_init_late_controls(struct smiapp_sensor
*sensor
)
615 unsigned long *valid_link_freqs
= &sensor
->valid_link_freqs
[
616 sensor
->csi_format
->compressed
- SMIAPP_COMPRESSED_BASE
];
619 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++) {
620 int max_value
= (1 << sensor
->csi_format
->width
) - 1;
622 sensor
->test_data
[i
] = v4l2_ctrl_new_std(
623 &sensor
->pixel_array
->ctrl_handler
,
624 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN_RED
+ i
,
625 0, max_value
, 1, max_value
);
628 for (max
= 0; sensor
->platform_data
->op_sys_clock
[max
+ 1]; max
++);
630 sensor
->link_freq
= v4l2_ctrl_new_int_menu(
631 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
632 V4L2_CID_LINK_FREQ
, __fls(*valid_link_freqs
),
633 __ffs(*valid_link_freqs
), sensor
->platform_data
->op_sys_clock
);
635 return sensor
->src
->ctrl_handler
.error
;
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 struct smiapp_pll
*pll
= &sensor
->pll
;
749 unsigned int type
, n
;
750 unsigned int i
, pixel_order
;
754 sensor
, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE
, &type
);
758 dev_dbg(&client
->dev
, "data_format_model_type %d\n", type
);
760 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_PIXEL_ORDER
,
765 if (pixel_order
>= ARRAY_SIZE(pixel_order_str
)) {
766 dev_dbg(&client
->dev
, "bad pixel order %d\n", pixel_order
);
770 dev_dbg(&client
->dev
, "pixel order %d (%s)\n", pixel_order
,
771 pixel_order_str
[pixel_order
]);
774 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL
:
775 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N
;
777 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED
:
778 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N
;
784 sensor
->default_pixel_order
= pixel_order
;
785 sensor
->mbus_frame_fmts
= 0;
787 for (i
= 0; i
< n
; i
++) {
792 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i
), &fmt
);
796 dev_dbg(&client
->dev
, "%u: bpp %u, compressed %u\n",
797 i
, fmt
>> 8, (u8
)fmt
);
799 for (j
= 0; j
< ARRAY_SIZE(smiapp_csi_data_formats
); j
++) {
800 const struct smiapp_csi_data_format
*f
=
801 &smiapp_csi_data_formats
[j
];
803 if (f
->pixel_order
!= SMIAPP_PIXEL_ORDER_GRBG
)
806 if (f
->width
!= fmt
>> 8 || f
->compressed
!= (u8
)fmt
)
809 dev_dbg(&client
->dev
, "jolly good! %d\n", j
);
811 sensor
->default_mbus_frame_fmts
|= 1 << j
;
815 /* Figure out which BPP values can be used with which formats. */
816 pll
->binning_horizontal
= 1;
817 pll
->binning_vertical
= 1;
818 pll
->scale_m
= sensor
->scale_m
;
820 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
821 const struct smiapp_csi_data_format
*f
=
822 &smiapp_csi_data_formats
[i
];
823 unsigned long *valid_link_freqs
=
824 &sensor
->valid_link_freqs
[
825 f
->compressed
- SMIAPP_COMPRESSED_BASE
];
828 BUG_ON(f
->compressed
< SMIAPP_COMPRESSED_BASE
);
829 BUG_ON(f
->compressed
> SMIAPP_COMPRESSED_MAX
);
831 if (!(sensor
->default_mbus_frame_fmts
& 1 << i
))
834 pll
->bits_per_pixel
= f
->compressed
;
836 for (j
= 0; sensor
->platform_data
->op_sys_clock
[j
]; j
++) {
837 pll
->link_freq
= sensor
->platform_data
->op_sys_clock
[j
];
839 rval
= smiapp_pll_try(sensor
, pll
);
840 dev_dbg(&client
->dev
, "link freq %u Hz, bpp %u %s\n",
841 pll
->link_freq
, pll
->bits_per_pixel
,
842 rval
? "not ok" : "ok");
846 set_bit(j
, valid_link_freqs
);
849 if (!*valid_link_freqs
) {
850 dev_info(&client
->dev
,
851 "no valid link frequencies for %u bpp\n",
853 sensor
->default_mbus_frame_fmts
&= ~BIT(i
);
857 if (!sensor
->csi_format
858 || f
->width
> sensor
->csi_format
->width
859 || (f
->width
== sensor
->csi_format
->width
860 && f
->compressed
> sensor
->csi_format
->compressed
)) {
861 sensor
->csi_format
= f
;
862 sensor
->internal_csi_format
= f
;
866 if (!sensor
->csi_format
) {
867 dev_err(&client
->dev
, "no supported mbus code found\n");
871 smiapp_update_mbus_formats(sensor
);
876 static void smiapp_update_blanking(struct smiapp_sensor
*sensor
)
878 struct v4l2_ctrl
*vblank
= sensor
->vblank
;
879 struct v4l2_ctrl
*hblank
= sensor
->hblank
;
883 sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES
],
884 sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
] -
885 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
);
886 max
= sensor
->limits
[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN
] -
887 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
;
889 __v4l2_ctrl_modify_range(vblank
, min
, max
, vblank
->step
, min
);
892 sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
] -
893 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
,
894 sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
]);
895 max
= sensor
->limits
[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN
] -
896 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
;
898 __v4l2_ctrl_modify_range(hblank
, min
, max
, hblank
->step
, min
);
900 __smiapp_update_exposure_limits(sensor
);
903 static int smiapp_update_mode(struct smiapp_sensor
*sensor
)
905 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
906 unsigned int binning_mode
;
909 dev_dbg(&client
->dev
, "frame size: %dx%d\n",
910 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
,
911 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
912 dev_dbg(&client
->dev
, "csi format width: %d\n",
913 sensor
->csi_format
->width
);
915 /* Binning has to be set up here; it affects limits */
916 if (sensor
->binning_horizontal
== 1 &&
917 sensor
->binning_vertical
== 1) {
921 (sensor
->binning_horizontal
<< 4)
922 | sensor
->binning_vertical
;
925 sensor
, SMIAPP_REG_U8_BINNING_TYPE
, binning_type
);
931 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_BINNING_MODE
, binning_mode
);
935 /* Get updated limits due to binning */
936 rval
= smiapp_get_limits_binning(sensor
);
940 rval
= smiapp_pll_update(sensor
);
944 /* Output from pixel array, including blanking */
945 smiapp_update_blanking(sensor
);
947 dev_dbg(&client
->dev
, "vblank\t\t%d\n", sensor
->vblank
->val
);
948 dev_dbg(&client
->dev
, "hblank\t\t%d\n", sensor
->hblank
->val
);
950 dev_dbg(&client
->dev
, "real timeperframe\t100/%d\n",
951 sensor
->pll
.pixel_rate_pixel_array
/
952 ((sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
953 + sensor
->hblank
->val
) *
954 (sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
955 + sensor
->vblank
->val
) / 100));
962 * SMIA++ NVM handling
965 static int smiapp_read_nvm(struct smiapp_sensor
*sensor
,
971 np
= sensor
->nvm_size
/ SMIAPP_NVM_PAGE_SIZE
;
972 for (p
= 0; p
< np
; p
++) {
975 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT
, p
);
979 rval
= smiapp_write(sensor
,
980 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
,
981 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN
|
982 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN
);
986 for (i
= 0; i
< 1000; i
++) {
989 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS
, &s
);
994 if (s
& SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY
)
1004 for (i
= 0; i
< SMIAPP_NVM_PAGE_SIZE
; i
++) {
1007 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0
+ i
,
1017 rval2
= smiapp_write(sensor
, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
, 0);
1026 * SMIA++ CCI address control
1029 static int smiapp_change_cci_addr(struct smiapp_sensor
*sensor
)
1031 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1035 client
->addr
= sensor
->platform_data
->i2c_addr_dfl
;
1037 rval
= smiapp_write(sensor
,
1038 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
,
1039 sensor
->platform_data
->i2c_addr_alt
<< 1);
1043 client
->addr
= sensor
->platform_data
->i2c_addr_alt
;
1045 /* verify addr change went ok */
1046 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
, &val
);
1050 if (val
!= sensor
->platform_data
->i2c_addr_alt
<< 1)
1058 * SMIA++ Mode Control
1061 static int smiapp_setup_flash_strobe(struct smiapp_sensor
*sensor
)
1063 struct smiapp_flash_strobe_parms
*strobe_setup
;
1064 unsigned int ext_freq
= sensor
->platform_data
->ext_clk
;
1066 u32 strobe_adjustment
;
1067 u32 strobe_width_high_rs
;
1070 strobe_setup
= sensor
->platform_data
->strobe_setup
;
1073 * How to calculate registers related to strobe length. Please
1074 * do not change, or if you do at least know what you're
1077 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1079 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1080 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1082 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1083 * flash_strobe_adjustment E N, [1 - 0xff]
1085 * The formula above is written as below to keep it on one
1088 * l / 10^6 = w / e * a
1090 * Let's mark w * a by x:
1098 * The strobe width must be at least as long as requested,
1099 * thus rounding upwards is needed.
1101 * x = (l * e + 10^6 - 1) / 10^6
1102 * -----------------------------
1104 * Maximum possible accuracy is wanted at all times. Thus keep
1105 * a as small as possible.
1107 * Calculate a, assuming maximum w, with rounding upwards:
1109 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1110 * -------------------------------------
1112 * Thus, we also get w, with that a, with rounding upwards:
1114 * w = (x + a - 1) / a
1115 * -------------------
1119 * x E [1, (2^16 - 1) * (2^8 - 1)]
1121 * Substituting maximum x to the original formula (with rounding),
1122 * the maximum l is thus
1124 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1126 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1127 * --------------------------------------------------
1129 * flash_strobe_length must be clamped between 1 and
1130 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1134 * flash_strobe_adjustment = ((flash_strobe_length *
1135 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1137 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1138 * EXTCLK freq + 10^6 - 1) / 10^6 +
1139 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1141 tmp
= div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1142 1000000 + 1, ext_freq
);
1143 strobe_setup
->strobe_width_high_us
=
1144 clamp_t(u32
, strobe_setup
->strobe_width_high_us
, 1, tmp
);
1146 tmp
= div_u64(((u64
)strobe_setup
->strobe_width_high_us
* (u64
)ext_freq
+
1147 1000000 - 1), 1000000ULL);
1148 strobe_adjustment
= (tmp
+ (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1149 strobe_width_high_rs
= (tmp
+ strobe_adjustment
- 1) /
1152 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_MODE_RS
,
1153 strobe_setup
->mode
);
1157 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT
,
1162 rval
= smiapp_write(
1163 sensor
, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL
,
1164 strobe_width_high_rs
);
1168 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL
,
1169 strobe_setup
->strobe_delay
);
1173 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_FLASH_STROBE_START_POINT
,
1174 strobe_setup
->stobe_start_point
);
1178 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_TRIGGER_RS
,
1179 strobe_setup
->trigger
);
1182 sensor
->platform_data
->strobe_setup
->trigger
= 0;
1187 /* -----------------------------------------------------------------------------
1191 static int smiapp_power_on(struct smiapp_sensor
*sensor
)
1193 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1197 rval
= regulator_enable(sensor
->vana
);
1199 dev_err(&client
->dev
, "failed to enable vana regulator\n");
1202 usleep_range(1000, 1000);
1204 if (sensor
->platform_data
->set_xclk
)
1205 rval
= sensor
->platform_data
->set_xclk(
1206 &sensor
->src
->sd
, sensor
->platform_data
->ext_clk
);
1208 rval
= clk_prepare_enable(sensor
->ext_clk
);
1210 dev_dbg(&client
->dev
, "failed to enable xclk\n");
1213 usleep_range(1000, 1000);
1215 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1216 gpio_set_value(sensor
->platform_data
->xshutdown
, 1);
1218 sleep
= SMIAPP_RESET_DELAY(sensor
->platform_data
->ext_clk
);
1219 usleep_range(sleep
, sleep
);
1222 * Failures to respond to the address change command have been noticed.
1223 * Those failures seem to be caused by the sensor requiring a longer
1224 * boot time than advertised. An additional 10ms delay seems to work
1225 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1226 * unnecessary. The failures need to be investigated to find a proper
1227 * fix, and a delay will likely need to be added here if the I2C write
1228 * retry hack is reverted before the root cause of the boot time issue
1232 if (sensor
->platform_data
->i2c_addr_alt
) {
1233 rval
= smiapp_change_cci_addr(sensor
);
1235 dev_err(&client
->dev
, "cci address change error\n");
1236 goto out_cci_addr_fail
;
1240 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_SOFTWARE_RESET
,
1241 SMIAPP_SOFTWARE_RESET
);
1243 dev_err(&client
->dev
, "software reset failed\n");
1244 goto out_cci_addr_fail
;
1247 if (sensor
->platform_data
->i2c_addr_alt
) {
1248 rval
= smiapp_change_cci_addr(sensor
);
1250 dev_err(&client
->dev
, "cci address change error\n");
1251 goto out_cci_addr_fail
;
1255 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_COMPRESSION_MODE
,
1256 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR
);
1258 dev_err(&client
->dev
, "compression mode set failed\n");
1259 goto out_cci_addr_fail
;
1262 rval
= smiapp_write(
1263 sensor
, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ
,
1264 sensor
->platform_data
->ext_clk
/ (1000000 / (1 << 8)));
1266 dev_err(&client
->dev
, "extclk frequency set failed\n");
1267 goto out_cci_addr_fail
;
1270 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_LANE_MODE
,
1271 sensor
->platform_data
->lanes
- 1);
1273 dev_err(&client
->dev
, "csi lane mode set failed\n");
1274 goto out_cci_addr_fail
;
1277 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FAST_STANDBY_CTRL
,
1278 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE
);
1280 dev_err(&client
->dev
, "fast standby set failed\n");
1281 goto out_cci_addr_fail
;
1284 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_SIGNALLING_MODE
,
1285 sensor
->platform_data
->csi_signalling_mode
);
1287 dev_err(&client
->dev
, "csi signalling mode set failed\n");
1288 goto out_cci_addr_fail
;
1291 /* DPHY control done by sensor based on requested link rate */
1292 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_DPHY_CTRL
,
1293 SMIAPP_DPHY_CTRL_UI
);
1297 rval
= smiapp_call_quirk(sensor
, post_poweron
);
1299 dev_err(&client
->dev
, "post_poweron quirks failed\n");
1300 goto out_cci_addr_fail
;
1303 /* Are we still initialising...? If yes, return here. */
1304 if (!sensor
->pixel_array
)
1307 rval
= v4l2_ctrl_handler_setup(
1308 &sensor
->pixel_array
->ctrl_handler
);
1310 goto out_cci_addr_fail
;
1312 rval
= v4l2_ctrl_handler_setup(&sensor
->src
->ctrl_handler
);
1314 goto out_cci_addr_fail
;
1316 mutex_lock(&sensor
->mutex
);
1317 rval
= smiapp_update_mode(sensor
);
1318 mutex_unlock(&sensor
->mutex
);
1320 goto out_cci_addr_fail
;
1325 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1326 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
1327 if (sensor
->platform_data
->set_xclk
)
1328 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
1330 clk_disable_unprepare(sensor
->ext_clk
);
1333 regulator_disable(sensor
->vana
);
1337 static void smiapp_power_off(struct smiapp_sensor
*sensor
)
1340 * Currently power/clock to lens are enable/disabled separately
1341 * but they are essentially the same signals. So if the sensor is
1342 * powered off while the lens is powered on the sensor does not
1343 * really see a power off and next time the cci address change
1344 * will fail. So do a soft reset explicitly here.
1346 if (sensor
->platform_data
->i2c_addr_alt
)
1347 smiapp_write(sensor
,
1348 SMIAPP_REG_U8_SOFTWARE_RESET
,
1349 SMIAPP_SOFTWARE_RESET
);
1351 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
1352 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
1353 if (sensor
->platform_data
->set_xclk
)
1354 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
1356 clk_disable_unprepare(sensor
->ext_clk
);
1357 usleep_range(5000, 5000);
1358 regulator_disable(sensor
->vana
);
1359 sensor
->streaming
= false;
1362 static int smiapp_set_power(struct v4l2_subdev
*subdev
, int on
)
1364 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1367 mutex_lock(&sensor
->power_mutex
);
1369 if (on
&& !sensor
->power_count
) {
1370 /* Power on and perform initialisation. */
1371 ret
= smiapp_power_on(sensor
);
1374 } else if (!on
&& sensor
->power_count
== 1) {
1375 smiapp_power_off(sensor
);
1378 /* Update the power count. */
1379 sensor
->power_count
+= on
? 1 : -1;
1380 WARN_ON(sensor
->power_count
< 0);
1383 mutex_unlock(&sensor
->power_mutex
);
1387 /* -----------------------------------------------------------------------------
1388 * Video stream management
1391 static int smiapp_start_streaming(struct smiapp_sensor
*sensor
)
1393 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1396 mutex_lock(&sensor
->mutex
);
1398 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_CSI_DATA_FORMAT
,
1399 (sensor
->csi_format
->width
<< 8) |
1400 sensor
->csi_format
->compressed
);
1404 rval
= smiapp_pll_configure(sensor
);
1408 /* Analog crop start coordinates */
1409 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_ADDR_START
,
1410 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
);
1414 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_ADDR_START
,
1415 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
);
1419 /* Analog crop end coordinates */
1420 rval
= smiapp_write(
1421 sensor
, SMIAPP_REG_U16_X_ADDR_END
,
1422 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
1423 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
- 1);
1427 rval
= smiapp_write(
1428 sensor
, SMIAPP_REG_U16_Y_ADDR_END
,
1429 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
1430 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
- 1);
1435 * Output from pixel array, including blanking, is set using
1436 * controls below. No need to set here.
1440 if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
1441 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
1442 rval
= smiapp_write(
1443 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET
,
1444 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].left
);
1448 rval
= smiapp_write(
1449 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET
,
1450 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].top
);
1454 rval
= smiapp_write(
1455 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH
,
1456 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].width
);
1460 rval
= smiapp_write(
1461 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT
,
1462 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].height
);
1468 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
1469 != SMIAPP_SCALING_CAPABILITY_NONE
) {
1470 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALING_MODE
,
1471 sensor
->scaling_mode
);
1475 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALE_M
,
1481 /* Output size from sensor */
1482 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_OUTPUT_SIZE
,
1483 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
);
1486 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_OUTPUT_SIZE
,
1487 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
1491 if ((sensor
->limits
[SMIAPP_LIMIT_FLASH_MODE_CAPABILITY
] &
1492 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE
|
1493 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE
)) &&
1494 sensor
->platform_data
->strobe_setup
!= NULL
&&
1495 sensor
->platform_data
->strobe_setup
->trigger
!= 0) {
1496 rval
= smiapp_setup_flash_strobe(sensor
);
1501 rval
= smiapp_call_quirk(sensor
, pre_streamon
);
1503 dev_err(&client
->dev
, "pre_streamon quirks failed\n");
1507 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1508 SMIAPP_MODE_SELECT_STREAMING
);
1511 mutex_unlock(&sensor
->mutex
);
1516 static int smiapp_stop_streaming(struct smiapp_sensor
*sensor
)
1518 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1521 mutex_lock(&sensor
->mutex
);
1522 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1523 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY
);
1527 rval
= smiapp_call_quirk(sensor
, post_streamoff
);
1529 dev_err(&client
->dev
, "post_streamoff quirks failed\n");
1532 mutex_unlock(&sensor
->mutex
);
1536 /* -----------------------------------------------------------------------------
1537 * V4L2 subdev video operations
1540 static int smiapp_set_stream(struct v4l2_subdev
*subdev
, int enable
)
1542 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1545 if (sensor
->streaming
== enable
)
1549 sensor
->streaming
= true;
1550 rval
= smiapp_start_streaming(sensor
);
1552 sensor
->streaming
= false;
1554 rval
= smiapp_stop_streaming(sensor
);
1555 sensor
->streaming
= false;
1561 static int smiapp_enum_mbus_code(struct v4l2_subdev
*subdev
,
1562 struct v4l2_subdev_pad_config
*cfg
,
1563 struct v4l2_subdev_mbus_code_enum
*code
)
1565 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1566 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1571 mutex_lock(&sensor
->mutex
);
1573 dev_err(&client
->dev
, "subdev %s, pad %d, index %d\n",
1574 subdev
->name
, code
->pad
, code
->index
);
1576 if (subdev
!= &sensor
->src
->sd
|| code
->pad
!= SMIAPP_PAD_SRC
) {
1580 code
->code
= sensor
->internal_csi_format
->code
;
1585 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1586 if (sensor
->mbus_frame_fmts
& (1 << i
))
1589 if (idx
== code
->index
) {
1590 code
->code
= smiapp_csi_data_formats
[i
].code
;
1591 dev_err(&client
->dev
, "found index %d, i %d, code %x\n",
1592 code
->index
, i
, code
->code
);
1599 mutex_unlock(&sensor
->mutex
);
1604 static u32
__smiapp_get_mbus_code(struct v4l2_subdev
*subdev
,
1607 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1609 if (subdev
== &sensor
->src
->sd
&& pad
== SMIAPP_PAD_SRC
)
1610 return sensor
->csi_format
->code
;
1612 return sensor
->internal_csi_format
->code
;
1615 static int __smiapp_get_format(struct v4l2_subdev
*subdev
,
1616 struct v4l2_subdev_pad_config
*cfg
,
1617 struct v4l2_subdev_format
*fmt
)
1619 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1621 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1622 fmt
->format
= *v4l2_subdev_get_try_format(subdev
, cfg
, fmt
->pad
);
1624 struct v4l2_rect
*r
;
1626 if (fmt
->pad
== ssd
->source_pad
)
1627 r
= &ssd
->crop
[ssd
->source_pad
];
1631 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1632 fmt
->format
.width
= r
->width
;
1633 fmt
->format
.height
= r
->height
;
1634 fmt
->format
.field
= V4L2_FIELD_NONE
;
1640 static int smiapp_get_format(struct v4l2_subdev
*subdev
,
1641 struct v4l2_subdev_pad_config
*cfg
,
1642 struct v4l2_subdev_format
*fmt
)
1644 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1647 mutex_lock(&sensor
->mutex
);
1648 rval
= __smiapp_get_format(subdev
, cfg
, fmt
);
1649 mutex_unlock(&sensor
->mutex
);
1654 static void smiapp_get_crop_compose(struct v4l2_subdev
*subdev
,
1655 struct v4l2_subdev_pad_config
*cfg
,
1656 struct v4l2_rect
**crops
,
1657 struct v4l2_rect
**comps
, int which
)
1659 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1662 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1664 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++)
1665 crops
[i
] = &ssd
->crop
[i
];
1667 *comps
= &ssd
->compose
;
1670 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++) {
1671 crops
[i
] = v4l2_subdev_get_try_crop(subdev
, cfg
, i
);
1676 *comps
= v4l2_subdev_get_try_compose(subdev
, cfg
,
1683 /* Changes require propagation only on sink pad. */
1684 static void smiapp_propagate(struct v4l2_subdev
*subdev
,
1685 struct v4l2_subdev_pad_config
*cfg
, int which
,
1688 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1689 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1690 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
1692 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, which
);
1695 case V4L2_SEL_TGT_CROP
:
1696 comp
->width
= crops
[SMIAPP_PAD_SINK
]->width
;
1697 comp
->height
= crops
[SMIAPP_PAD_SINK
]->height
;
1698 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1699 if (ssd
== sensor
->scaler
) {
1702 SMIAPP_LIMIT_SCALER_N_MIN
];
1703 sensor
->scaling_mode
=
1704 SMIAPP_SCALING_MODE_NONE
;
1705 } else if (ssd
== sensor
->binner
) {
1706 sensor
->binning_horizontal
= 1;
1707 sensor
->binning_vertical
= 1;
1711 case V4L2_SEL_TGT_COMPOSE
:
1712 *crops
[SMIAPP_PAD_SRC
] = *comp
;
1719 static const struct smiapp_csi_data_format
1720 *smiapp_validate_csi_data_format(struct smiapp_sensor
*sensor
, u32 code
)
1722 const struct smiapp_csi_data_format
*csi_format
= sensor
->csi_format
;
1725 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1726 if (sensor
->mbus_frame_fmts
& (1 << i
)
1727 && smiapp_csi_data_formats
[i
].code
== code
)
1728 return &smiapp_csi_data_formats
[i
];
1734 static int smiapp_set_format_source(struct v4l2_subdev
*subdev
,
1735 struct v4l2_subdev_pad_config
*cfg
,
1736 struct v4l2_subdev_format
*fmt
)
1738 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1739 const struct smiapp_csi_data_format
*csi_format
,
1740 *old_csi_format
= sensor
->csi_format
;
1741 unsigned long *valid_link_freqs
;
1742 u32 code
= fmt
->format
.code
;
1746 rval
= __smiapp_get_format(subdev
, cfg
, fmt
);
1751 * Media bus code is changeable on src subdev's source pad. On
1752 * other source pads we just get format here.
1754 if (subdev
!= &sensor
->src
->sd
)
1757 csi_format
= smiapp_validate_csi_data_format(sensor
, code
);
1759 fmt
->format
.code
= csi_format
->code
;
1761 if (fmt
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1764 sensor
->csi_format
= csi_format
;
1766 if (csi_format
->width
!= old_csi_format
->width
)
1767 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
1768 __v4l2_ctrl_modify_range(
1769 sensor
->test_data
[i
], 0,
1770 (1 << csi_format
->width
) - 1, 1, 0);
1772 if (csi_format
->compressed
== old_csi_format
->compressed
)
1776 &sensor
->valid_link_freqs
[sensor
->csi_format
->compressed
1777 - SMIAPP_COMPRESSED_BASE
];
1779 __v4l2_ctrl_modify_range(
1780 sensor
->link_freq
, 0,
1781 __fls(*valid_link_freqs
), ~*valid_link_freqs
,
1782 __ffs(*valid_link_freqs
));
1784 return smiapp_pll_update(sensor
);
1787 static int smiapp_set_format(struct v4l2_subdev
*subdev
,
1788 struct v4l2_subdev_pad_config
*cfg
,
1789 struct v4l2_subdev_format
*fmt
)
1791 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1792 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1793 struct v4l2_rect
*crops
[SMIAPP_PADS
];
1795 mutex_lock(&sensor
->mutex
);
1797 if (fmt
->pad
== ssd
->source_pad
) {
1800 rval
= smiapp_set_format_source(subdev
, cfg
, fmt
);
1802 mutex_unlock(&sensor
->mutex
);
1807 /* Sink pad. Width and height are changeable here. */
1808 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1809 fmt
->format
.width
&= ~1;
1810 fmt
->format
.height
&= ~1;
1811 fmt
->format
.field
= V4L2_FIELD_NONE
;
1814 clamp(fmt
->format
.width
,
1815 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
1816 sensor
->limits
[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE
]);
1817 fmt
->format
.height
=
1818 clamp(fmt
->format
.height
,
1819 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
1820 sensor
->limits
[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE
]);
1822 smiapp_get_crop_compose(subdev
, cfg
, crops
, NULL
, fmt
->which
);
1824 crops
[ssd
->sink_pad
]->left
= 0;
1825 crops
[ssd
->sink_pad
]->top
= 0;
1826 crops
[ssd
->sink_pad
]->width
= fmt
->format
.width
;
1827 crops
[ssd
->sink_pad
]->height
= fmt
->format
.height
;
1828 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
1829 ssd
->sink_fmt
= *crops
[ssd
->sink_pad
];
1830 smiapp_propagate(subdev
, cfg
, fmt
->which
,
1833 mutex_unlock(&sensor
->mutex
);
1839 * Calculate goodness of scaled image size compared to expected image
1840 * size and flags provided.
1842 #define SCALING_GOODNESS 100000
1843 #define SCALING_GOODNESS_EXTREME 100000000
1844 static int scaling_goodness(struct v4l2_subdev
*subdev
, int w
, int ask_w
,
1845 int h
, int ask_h
, u32 flags
)
1847 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1848 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1856 if (flags
& V4L2_SEL_FLAG_GE
) {
1858 val
-= SCALING_GOODNESS
;
1860 val
-= SCALING_GOODNESS
;
1863 if (flags
& V4L2_SEL_FLAG_LE
) {
1865 val
-= SCALING_GOODNESS
;
1867 val
-= SCALING_GOODNESS
;
1870 val
-= abs(w
- ask_w
);
1871 val
-= abs(h
- ask_h
);
1873 if (w
< sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
])
1874 val
-= SCALING_GOODNESS_EXTREME
;
1876 dev_dbg(&client
->dev
, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1877 w
, ask_h
, h
, ask_h
, val
);
1882 static void smiapp_set_compose_binner(struct v4l2_subdev
*subdev
,
1883 struct v4l2_subdev_pad_config
*cfg
,
1884 struct v4l2_subdev_selection
*sel
,
1885 struct v4l2_rect
**crops
,
1886 struct v4l2_rect
*comp
)
1888 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1890 unsigned int binh
= 1, binv
= 1;
1891 int best
= scaling_goodness(
1893 crops
[SMIAPP_PAD_SINK
]->width
, sel
->r
.width
,
1894 crops
[SMIAPP_PAD_SINK
]->height
, sel
->r
.height
, sel
->flags
);
1896 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
1897 int this = scaling_goodness(
1899 crops
[SMIAPP_PAD_SINK
]->width
1900 / sensor
->binning_subtypes
[i
].horizontal
,
1902 crops
[SMIAPP_PAD_SINK
]->height
1903 / sensor
->binning_subtypes
[i
].vertical
,
1904 sel
->r
.height
, sel
->flags
);
1907 binh
= sensor
->binning_subtypes
[i
].horizontal
;
1908 binv
= sensor
->binning_subtypes
[i
].vertical
;
1912 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1913 sensor
->binning_vertical
= binv
;
1914 sensor
->binning_horizontal
= binh
;
1917 sel
->r
.width
= (crops
[SMIAPP_PAD_SINK
]->width
/ binh
) & ~1;
1918 sel
->r
.height
= (crops
[SMIAPP_PAD_SINK
]->height
/ binv
) & ~1;
1922 * Calculate best scaling ratio and mode for given output resolution.
1924 * Try all of these: horizontal ratio, vertical ratio and smallest
1925 * size possible (horizontally).
1927 * Also try whether horizontal scaler or full scaler gives a better
1930 static void smiapp_set_compose_scaler(struct v4l2_subdev
*subdev
,
1931 struct v4l2_subdev_pad_config
*cfg
,
1932 struct v4l2_subdev_selection
*sel
,
1933 struct v4l2_rect
**crops
,
1934 struct v4l2_rect
*comp
)
1936 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1937 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1938 u32 min
, max
, a
, b
, max_m
;
1939 u32 scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
1940 int mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
1946 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
1947 crops
[SMIAPP_PAD_SINK
]->width
);
1948 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
1949 crops
[SMIAPP_PAD_SINK
]->height
);
1951 a
= crops
[SMIAPP_PAD_SINK
]->width
1952 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] / sel
->r
.width
;
1953 b
= crops
[SMIAPP_PAD_SINK
]->height
1954 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] / sel
->r
.height
;
1955 max_m
= crops
[SMIAPP_PAD_SINK
]->width
1956 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]
1957 / sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
];
1959 a
= clamp(a
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1960 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1961 b
= clamp(b
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1962 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1963 max_m
= clamp(max_m
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1964 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1966 dev_dbg(&client
->dev
, "scaling: a %d b %d max_m %d\n", a
, b
, max_m
);
1968 min
= min(max_m
, min(a
, b
));
1969 max
= min(max_m
, max(a
, b
));
1978 try[ntry
] = min
+ 1;
1981 try[ntry
] = max
+ 1;
1986 for (i
= 0; i
< ntry
; i
++) {
1987 int this = scaling_goodness(
1989 crops
[SMIAPP_PAD_SINK
]->width
1991 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
1993 crops
[SMIAPP_PAD_SINK
]->height
,
1997 dev_dbg(&client
->dev
, "trying factor %d (%d)\n", try[i
], i
);
2001 mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
2005 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2006 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2009 this = scaling_goodness(
2010 subdev
, crops
[SMIAPP_PAD_SINK
]->width
2012 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2014 crops
[SMIAPP_PAD_SINK
]->height
2016 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2022 mode
= SMIAPP_SCALING_MODE_BOTH
;
2028 (crops
[SMIAPP_PAD_SINK
]->width
2030 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]) & ~1;
2031 if (mode
== SMIAPP_SCALING_MODE_BOTH
)
2033 (crops
[SMIAPP_PAD_SINK
]->height
2035 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
])
2038 sel
->r
.height
= crops
[SMIAPP_PAD_SINK
]->height
;
2040 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2041 sensor
->scale_m
= scale_m
;
2042 sensor
->scaling_mode
= mode
;
2045 /* We're only called on source pads. This function sets scaling. */
2046 static int smiapp_set_compose(struct v4l2_subdev
*subdev
,
2047 struct v4l2_subdev_pad_config
*cfg
,
2048 struct v4l2_subdev_selection
*sel
)
2050 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2051 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2052 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2054 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, sel
->which
);
2059 if (ssd
== sensor
->binner
)
2060 smiapp_set_compose_binner(subdev
, cfg
, sel
, crops
, comp
);
2062 smiapp_set_compose_scaler(subdev
, cfg
, sel
, crops
, comp
);
2065 smiapp_propagate(subdev
, cfg
, sel
->which
,
2066 V4L2_SEL_TGT_COMPOSE
);
2068 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
2069 return smiapp_update_mode(sensor
);
2074 static int __smiapp_sel_supported(struct v4l2_subdev
*subdev
,
2075 struct v4l2_subdev_selection
*sel
)
2077 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2078 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2080 /* We only implement crop in three places. */
2081 switch (sel
->target
) {
2082 case V4L2_SEL_TGT_CROP
:
2083 case V4L2_SEL_TGT_CROP_BOUNDS
:
2084 if (ssd
== sensor
->pixel_array
2085 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2087 if (ssd
== sensor
->src
2088 && sel
->pad
== SMIAPP_PAD_SRC
)
2090 if (ssd
== sensor
->scaler
2091 && sel
->pad
== SMIAPP_PAD_SINK
2092 && sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
2093 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
)
2096 case V4L2_SEL_TGT_NATIVE_SIZE
:
2097 if (ssd
== sensor
->pixel_array
2098 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2101 case V4L2_SEL_TGT_COMPOSE
:
2102 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2103 if (sel
->pad
== ssd
->source_pad
)
2105 if (ssd
== sensor
->binner
)
2107 if (ssd
== sensor
->scaler
2108 && sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2109 != SMIAPP_SCALING_CAPABILITY_NONE
)
2117 static int smiapp_set_crop(struct v4l2_subdev
*subdev
,
2118 struct v4l2_subdev_pad_config
*cfg
,
2119 struct v4l2_subdev_selection
*sel
)
2121 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2122 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2123 struct v4l2_rect
*src_size
, *crops
[SMIAPP_PADS
];
2124 struct v4l2_rect _r
;
2126 smiapp_get_crop_compose(subdev
, cfg
, crops
, NULL
, sel
->which
);
2128 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2129 if (sel
->pad
== ssd
->sink_pad
)
2130 src_size
= &ssd
->sink_fmt
;
2132 src_size
= &ssd
->compose
;
2134 if (sel
->pad
== ssd
->sink_pad
) {
2137 _r
.width
= v4l2_subdev_get_try_format(subdev
, cfg
, sel
->pad
)
2139 _r
.height
= v4l2_subdev_get_try_format(subdev
, cfg
, sel
->pad
)
2144 v4l2_subdev_get_try_compose(
2145 subdev
, cfg
, ssd
->sink_pad
);
2149 if (ssd
== sensor
->src
&& sel
->pad
== SMIAPP_PAD_SRC
) {
2154 sel
->r
.width
= min(sel
->r
.width
, src_size
->width
);
2155 sel
->r
.height
= min(sel
->r
.height
, src_size
->height
);
2157 sel
->r
.left
= min_t(int, sel
->r
.left
, src_size
->width
- sel
->r
.width
);
2158 sel
->r
.top
= min_t(int, sel
->r
.top
, src_size
->height
- sel
->r
.height
);
2160 *crops
[sel
->pad
] = sel
->r
;
2162 if (ssd
!= sensor
->pixel_array
&& sel
->pad
== SMIAPP_PAD_SINK
)
2163 smiapp_propagate(subdev
, cfg
, sel
->which
,
2169 static int __smiapp_get_selection(struct v4l2_subdev
*subdev
,
2170 struct v4l2_subdev_pad_config
*cfg
,
2171 struct v4l2_subdev_selection
*sel
)
2173 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2174 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2175 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2176 struct v4l2_rect sink_fmt
;
2179 ret
= __smiapp_sel_supported(subdev
, sel
);
2183 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, sel
->which
);
2185 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2186 sink_fmt
= ssd
->sink_fmt
;
2188 struct v4l2_mbus_framefmt
*fmt
=
2189 v4l2_subdev_get_try_format(subdev
, cfg
, ssd
->sink_pad
);
2193 sink_fmt
.width
= fmt
->width
;
2194 sink_fmt
.height
= fmt
->height
;
2197 switch (sel
->target
) {
2198 case V4L2_SEL_TGT_CROP_BOUNDS
:
2199 case V4L2_SEL_TGT_NATIVE_SIZE
:
2200 if (ssd
== sensor
->pixel_array
) {
2201 sel
->r
.left
= sel
->r
.top
= 0;
2203 sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2205 sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2206 } else if (sel
->pad
== ssd
->sink_pad
) {
2212 case V4L2_SEL_TGT_CROP
:
2213 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2214 sel
->r
= *crops
[sel
->pad
];
2216 case V4L2_SEL_TGT_COMPOSE
:
2224 static int smiapp_get_selection(struct v4l2_subdev
*subdev
,
2225 struct v4l2_subdev_pad_config
*cfg
,
2226 struct v4l2_subdev_selection
*sel
)
2228 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2231 mutex_lock(&sensor
->mutex
);
2232 rval
= __smiapp_get_selection(subdev
, cfg
, sel
);
2233 mutex_unlock(&sensor
->mutex
);
2237 static int smiapp_set_selection(struct v4l2_subdev
*subdev
,
2238 struct v4l2_subdev_pad_config
*cfg
,
2239 struct v4l2_subdev_selection
*sel
)
2241 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2244 ret
= __smiapp_sel_supported(subdev
, sel
);
2248 mutex_lock(&sensor
->mutex
);
2250 sel
->r
.left
= max(0, sel
->r
.left
& ~1);
2251 sel
->r
.top
= max(0, sel
->r
.top
& ~1);
2252 sel
->r
.width
= SMIAPP_ALIGN_DIM(sel
->r
.width
, sel
->flags
);
2253 sel
->r
.height
= SMIAPP_ALIGN_DIM(sel
->r
.height
, sel
->flags
);
2255 sel
->r
.width
= max_t(unsigned int,
2256 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
2258 sel
->r
.height
= max_t(unsigned int,
2259 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
2262 switch (sel
->target
) {
2263 case V4L2_SEL_TGT_CROP
:
2264 ret
= smiapp_set_crop(subdev
, cfg
, sel
);
2266 case V4L2_SEL_TGT_COMPOSE
:
2267 ret
= smiapp_set_compose(subdev
, cfg
, sel
);
2273 mutex_unlock(&sensor
->mutex
);
2277 static int smiapp_get_skip_frames(struct v4l2_subdev
*subdev
, u32
*frames
)
2279 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2281 *frames
= sensor
->frame_skip
;
2285 static int smiapp_get_skip_top_lines(struct v4l2_subdev
*subdev
, u32
*lines
)
2287 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2289 *lines
= sensor
->image_start
;
2294 /* -----------------------------------------------------------------------------
2299 smiapp_sysfs_nvm_read(struct device
*dev
, struct device_attribute
*attr
,
2302 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2303 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2304 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2305 unsigned int nbytes
;
2307 if (!sensor
->dev_init_done
)
2310 if (!sensor
->nvm_size
) {
2311 /* NVM not read yet - read it now */
2312 sensor
->nvm_size
= sensor
->platform_data
->nvm_size
;
2313 if (smiapp_set_power(subdev
, 1) < 0)
2315 if (smiapp_read_nvm(sensor
, sensor
->nvm
)) {
2316 dev_err(&client
->dev
, "nvm read failed\n");
2319 smiapp_set_power(subdev
, 0);
2322 * NVM is still way below a PAGE_SIZE, so we can safely
2323 * assume this for now.
2325 nbytes
= min_t(unsigned int, sensor
->nvm_size
, PAGE_SIZE
);
2326 memcpy(buf
, sensor
->nvm
, nbytes
);
2330 static DEVICE_ATTR(nvm
, S_IRUGO
, smiapp_sysfs_nvm_read
, NULL
);
2333 smiapp_sysfs_ident_read(struct device
*dev
, struct device_attribute
*attr
,
2336 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2337 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2338 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2340 return snprintf(buf
, PAGE_SIZE
, "%2.2x%4.4x%2.2x\n",
2341 minfo
->manufacturer_id
, minfo
->model_id
,
2342 minfo
->revision_number_major
) + 1;
2345 static DEVICE_ATTR(ident
, S_IRUGO
, smiapp_sysfs_ident_read
, NULL
);
2347 /* -----------------------------------------------------------------------------
2348 * V4L2 subdev core operations
2351 static int smiapp_identify_module(struct smiapp_sensor
*sensor
)
2353 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2354 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2358 minfo
->name
= SMIAPP_NAME
;
2361 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MANUFACTURER_ID
,
2362 &minfo
->manufacturer_id
);
2364 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U16_MODEL_ID
,
2367 rval
= smiapp_read_8only(sensor
,
2368 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR
,
2369 &minfo
->revision_number_major
);
2371 rval
= smiapp_read_8only(sensor
,
2372 SMIAPP_REG_U8_REVISION_NUMBER_MINOR
,
2373 &minfo
->revision_number_minor
);
2375 rval
= smiapp_read_8only(sensor
,
2376 SMIAPP_REG_U8_MODULE_DATE_YEAR
,
2377 &minfo
->module_year
);
2379 rval
= smiapp_read_8only(sensor
,
2380 SMIAPP_REG_U8_MODULE_DATE_MONTH
,
2381 &minfo
->module_month
);
2383 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MODULE_DATE_DAY
,
2384 &minfo
->module_day
);
2388 rval
= smiapp_read_8only(sensor
,
2389 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID
,
2390 &minfo
->sensor_manufacturer_id
);
2392 rval
= smiapp_read_8only(sensor
,
2393 SMIAPP_REG_U16_SENSOR_MODEL_ID
,
2394 &minfo
->sensor_model_id
);
2396 rval
= smiapp_read_8only(sensor
,
2397 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER
,
2398 &minfo
->sensor_revision_number
);
2400 rval
= smiapp_read_8only(sensor
,
2401 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION
,
2402 &minfo
->sensor_firmware_version
);
2406 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIA_VERSION
,
2407 &minfo
->smia_version
);
2409 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIAPP_VERSION
,
2410 &minfo
->smiapp_version
);
2413 dev_err(&client
->dev
, "sensor detection failed\n");
2417 dev_dbg(&client
->dev
, "module 0x%2.2x-0x%4.4x\n",
2418 minfo
->manufacturer_id
, minfo
->model_id
);
2420 dev_dbg(&client
->dev
,
2421 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2422 minfo
->revision_number_major
, minfo
->revision_number_minor
,
2423 minfo
->module_year
, minfo
->module_month
, minfo
->module_day
);
2425 dev_dbg(&client
->dev
, "sensor 0x%2.2x-0x%4.4x\n",
2426 minfo
->sensor_manufacturer_id
, minfo
->sensor_model_id
);
2428 dev_dbg(&client
->dev
,
2429 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2430 minfo
->sensor_revision_number
, minfo
->sensor_firmware_version
);
2432 dev_dbg(&client
->dev
, "smia version %2.2d smiapp version %2.2d\n",
2433 minfo
->smia_version
, minfo
->smiapp_version
);
2436 * Some modules have bad data in the lvalues below. Hope the
2437 * rvalues have better stuff. The lvalues are module
2438 * parameters whereas the rvalues are sensor parameters.
2440 if (!minfo
->manufacturer_id
&& !minfo
->model_id
) {
2441 minfo
->manufacturer_id
= minfo
->sensor_manufacturer_id
;
2442 minfo
->model_id
= minfo
->sensor_model_id
;
2443 minfo
->revision_number_major
= minfo
->sensor_revision_number
;
2446 for (i
= 0; i
< ARRAY_SIZE(smiapp_module_idents
); i
++) {
2447 if (smiapp_module_idents
[i
].manufacturer_id
2448 != minfo
->manufacturer_id
)
2450 if (smiapp_module_idents
[i
].model_id
!= minfo
->model_id
)
2452 if (smiapp_module_idents
[i
].flags
2453 & SMIAPP_MODULE_IDENT_FLAG_REV_LE
) {
2454 if (smiapp_module_idents
[i
].revision_number_major
2455 < minfo
->revision_number_major
)
2458 if (smiapp_module_idents
[i
].revision_number_major
2459 != minfo
->revision_number_major
)
2463 minfo
->name
= smiapp_module_idents
[i
].name
;
2464 minfo
->quirk
= smiapp_module_idents
[i
].quirk
;
2468 if (i
>= ARRAY_SIZE(smiapp_module_idents
))
2469 dev_warn(&client
->dev
,
2470 "no quirks for this module; let's hope it's fully compliant\n");
2472 dev_dbg(&client
->dev
, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2473 minfo
->name
, minfo
->manufacturer_id
, minfo
->model_id
,
2474 minfo
->revision_number_major
);
2479 static const struct v4l2_subdev_ops smiapp_ops
;
2480 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
;
2481 static const struct media_entity_operations smiapp_entity_ops
;
2483 static int smiapp_register_subdevs(struct smiapp_sensor
*sensor
)
2485 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2486 struct smiapp_subdev
*ssds
[] = {
2489 sensor
->pixel_array
,
2494 for (i
= 0; i
< SMIAPP_SUBDEVS
- 1; i
++) {
2495 struct smiapp_subdev
*this = ssds
[i
+ 1];
2496 struct smiapp_subdev
*last
= ssds
[i
];
2501 rval
= media_entity_pads_init(&this->sd
.entity
,
2502 this->npads
, this->pads
);
2504 dev_err(&client
->dev
,
2505 "media_entity_pads_init failed\n");
2509 rval
= v4l2_device_register_subdev(sensor
->src
->sd
.v4l2_dev
,
2512 dev_err(&client
->dev
,
2513 "v4l2_device_register_subdev failed\n");
2517 rval
= media_create_pad_link(&this->sd
.entity
,
2521 MEDIA_LNK_FL_ENABLED
|
2522 MEDIA_LNK_FL_IMMUTABLE
);
2524 dev_err(&client
->dev
,
2525 "media_create_pad_link failed\n");
2533 static void smiapp_cleanup(struct smiapp_sensor
*sensor
)
2535 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2537 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2538 device_remove_file(&client
->dev
, &dev_attr_ident
);
2540 smiapp_free_controls(sensor
);
2543 static int smiapp_init(struct smiapp_sensor
*sensor
)
2545 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2546 struct smiapp_pll
*pll
= &sensor
->pll
;
2547 struct smiapp_subdev
*last
= NULL
;
2551 sensor
->vana
= devm_regulator_get(&client
->dev
, "vana");
2552 if (IS_ERR(sensor
->vana
)) {
2553 dev_err(&client
->dev
, "could not get regulator for vana\n");
2554 return PTR_ERR(sensor
->vana
);
2557 if (!sensor
->platform_data
->set_xclk
) {
2558 sensor
->ext_clk
= devm_clk_get(&client
->dev
, NULL
);
2559 if (IS_ERR(sensor
->ext_clk
)) {
2560 dev_err(&client
->dev
, "could not get clock\n");
2561 return PTR_ERR(sensor
->ext_clk
);
2564 rval
= clk_set_rate(sensor
->ext_clk
,
2565 sensor
->platform_data
->ext_clk
);
2567 dev_err(&client
->dev
,
2568 "unable to set clock freq to %u\n",
2569 sensor
->platform_data
->ext_clk
);
2574 if (gpio_is_valid(sensor
->platform_data
->xshutdown
)) {
2575 rval
= devm_gpio_request_one(
2576 &client
->dev
, sensor
->platform_data
->xshutdown
, 0,
2577 "SMIA++ xshutdown");
2579 dev_err(&client
->dev
,
2580 "unable to acquire reset gpio %d\n",
2581 sensor
->platform_data
->xshutdown
);
2586 rval
= smiapp_power_on(sensor
);
2590 rval
= smiapp_identify_module(sensor
);
2596 rval
= smiapp_get_all_limits(sensor
);
2603 * Handle Sensor Module orientation on the board.
2605 * The application of H-FLIP and V-FLIP on the sensor is modified by
2606 * the sensor orientation on the board.
2608 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2609 * both H-FLIP and V-FLIP for normal operation which also implies
2610 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2611 * controls will need to be internally inverted.
2613 * Rotation also changes the bayer pattern.
2615 if (sensor
->platform_data
->module_board_orient
==
2616 SMIAPP_MODULE_BOARD_ORIENT_180
)
2617 sensor
->hvflip_inv_mask
= SMIAPP_IMAGE_ORIENTATION_HFLIP
|
2618 SMIAPP_IMAGE_ORIENTATION_VFLIP
;
2620 rval
= smiapp_call_quirk(sensor
, limits
);
2622 dev_err(&client
->dev
, "limits quirks failed\n");
2626 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
]) {
2629 rval
= smiapp_read(sensor
,
2630 SMIAPP_REG_U8_BINNING_SUBTYPES
, &val
);
2635 sensor
->nbinning_subtypes
= min_t(u8
, val
,
2636 SMIAPP_BINNING_SUBTYPES
);
2638 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
2640 sensor
, SMIAPP_REG_U8_BINNING_TYPE_n(i
), &val
);
2645 sensor
->binning_subtypes
[i
] =
2646 *(struct smiapp_binning_subtype
*)&val
;
2648 dev_dbg(&client
->dev
, "binning %xx%x\n",
2649 sensor
->binning_subtypes
[i
].horizontal
,
2650 sensor
->binning_subtypes
[i
].vertical
);
2653 sensor
->binning_horizontal
= 1;
2654 sensor
->binning_vertical
= 1;
2656 if (device_create_file(&client
->dev
, &dev_attr_ident
) != 0) {
2657 dev_err(&client
->dev
, "sysfs ident entry creation failed\n");
2661 /* SMIA++ NVM initialization - it will be read from the sensor
2662 * when it is first requested by userspace.
2664 if (sensor
->minfo
.smiapp_version
&& sensor
->platform_data
->nvm_size
) {
2665 sensor
->nvm
= devm_kzalloc(&client
->dev
,
2666 sensor
->platform_data
->nvm_size
, GFP_KERNEL
);
2667 if (sensor
->nvm
== NULL
) {
2668 dev_err(&client
->dev
, "nvm buf allocation failed\n");
2673 if (device_create_file(&client
->dev
, &dev_attr_nvm
) != 0) {
2674 dev_err(&client
->dev
, "sysfs nvm entry failed\n");
2680 /* We consider this as profile 0 sensor if any of these are zero. */
2681 if (!sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV
] ||
2682 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV
] ||
2683 !sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV
] ||
2684 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV
]) {
2685 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_0
;
2686 } else if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2687 != SMIAPP_SCALING_CAPABILITY_NONE
) {
2688 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2689 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2690 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_1
;
2692 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_2
;
2693 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
2694 sensor
->ssds_used
++;
2695 } else if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
2696 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
2697 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
2698 sensor
->ssds_used
++;
2700 sensor
->binner
= &sensor
->ssds
[sensor
->ssds_used
];
2701 sensor
->ssds_used
++;
2702 sensor
->pixel_array
= &sensor
->ssds
[sensor
->ssds_used
];
2703 sensor
->ssds_used
++;
2705 sensor
->scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
2707 /* prepare PLL configuration input values */
2708 pll
->bus_type
= SMIAPP_PLL_BUS_TYPE_CSI2
;
2709 pll
->csi2
.lanes
= sensor
->platform_data
->lanes
;
2710 pll
->ext_clk_freq_hz
= sensor
->platform_data
->ext_clk
;
2711 pll
->scale_n
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
2712 /* Profile 0 sensors have no separate OP clock branch. */
2713 if (sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
2714 pll
->flags
|= SMIAPP_PLL_FLAG_NO_OP_CLOCKS
;
2716 for (i
= 0; i
< SMIAPP_SUBDEVS
; i
++) {
2718 struct smiapp_subdev
*ssd
;
2720 } const __this
[] = {
2721 { sensor
->scaler
, "scaler", },
2722 { sensor
->binner
, "binner", },
2723 { sensor
->pixel_array
, "pixel array", },
2724 }, *_this
= &__this
[i
];
2725 struct smiapp_subdev
*this = _this
->ssd
;
2730 if (this != sensor
->src
)
2731 v4l2_subdev_init(&this->sd
, &smiapp_ops
);
2733 this->sensor
= sensor
;
2735 if (this == sensor
->pixel_array
) {
2739 this->source_pad
= 1;
2742 snprintf(this->sd
.name
,
2743 sizeof(this->sd
.name
), "%s %s %d-%4.4x",
2744 sensor
->minfo
.name
, _this
->name
,
2745 i2c_adapter_id(client
->adapter
), client
->addr
);
2747 this->sink_fmt
.width
=
2748 sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2749 this->sink_fmt
.height
=
2750 sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2751 this->compose
.width
= this->sink_fmt
.width
;
2752 this->compose
.height
= this->sink_fmt
.height
;
2753 this->crop
[this->source_pad
] = this->compose
;
2754 this->pads
[this->source_pad
].flags
= MEDIA_PAD_FL_SOURCE
;
2755 if (this != sensor
->pixel_array
) {
2756 this->crop
[this->sink_pad
] = this->compose
;
2757 this->pads
[this->sink_pad
].flags
= MEDIA_PAD_FL_SINK
;
2760 this->sd
.entity
.ops
= &smiapp_entity_ops
;
2767 this->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2768 this->sd
.internal_ops
= &smiapp_internal_ops
;
2769 this->sd
.owner
= THIS_MODULE
;
2770 v4l2_set_subdevdata(&this->sd
, client
);
2775 dev_dbg(&client
->dev
, "profile %d\n", sensor
->minfo
.smiapp_profile
);
2777 sensor
->pixel_array
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
2780 smiapp_read_frame_fmt(sensor
);
2781 rval
= smiapp_init_controls(sensor
);
2785 rval
= smiapp_call_quirk(sensor
, init
);
2789 rval
= smiapp_get_mbus_formats(sensor
);
2795 rval
= smiapp_init_late_controls(sensor
);
2801 mutex_lock(&sensor
->mutex
);
2802 rval
= smiapp_update_mode(sensor
);
2803 mutex_unlock(&sensor
->mutex
);
2805 dev_err(&client
->dev
, "update mode failed\n");
2809 sensor
->streaming
= false;
2810 sensor
->dev_init_done
= true;
2812 smiapp_power_off(sensor
);
2817 smiapp_cleanup(sensor
);
2820 smiapp_power_off(sensor
);
2824 static int smiapp_registered(struct v4l2_subdev
*subdev
)
2826 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2827 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2830 if (!client
->dev
.of_node
) {
2831 rval
= smiapp_init(sensor
);
2836 rval
= smiapp_register_subdevs(sensor
);
2838 smiapp_cleanup(sensor
);
2843 static int smiapp_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2845 struct smiapp_subdev
*ssd
= to_smiapp_subdev(sd
);
2846 struct smiapp_sensor
*sensor
= ssd
->sensor
;
2848 smiapp_csi_data_formats
[smiapp_pixel_order(sensor
)].code
;
2851 mutex_lock(&sensor
->mutex
);
2853 for (i
= 0; i
< ssd
->npads
; i
++) {
2854 struct v4l2_mbus_framefmt
*try_fmt
=
2855 v4l2_subdev_get_try_format(sd
, fh
->pad
, i
);
2856 struct v4l2_rect
*try_crop
= v4l2_subdev_get_try_crop(sd
, fh
->pad
, i
);
2857 struct v4l2_rect
*try_comp
;
2859 try_fmt
->width
= sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2860 try_fmt
->height
= sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2861 try_fmt
->code
= mbus_code
;
2862 try_fmt
->field
= V4L2_FIELD_NONE
;
2866 try_crop
->width
= try_fmt
->width
;
2867 try_crop
->height
= try_fmt
->height
;
2869 if (ssd
!= sensor
->pixel_array
)
2872 try_comp
= v4l2_subdev_get_try_compose(sd
, fh
->pad
, i
);
2873 *try_comp
= *try_crop
;
2876 mutex_unlock(&sensor
->mutex
);
2878 return smiapp_set_power(sd
, 1);
2881 static int smiapp_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2883 return smiapp_set_power(sd
, 0);
2886 static const struct v4l2_subdev_video_ops smiapp_video_ops
= {
2887 .s_stream
= smiapp_set_stream
,
2890 static const struct v4l2_subdev_core_ops smiapp_core_ops
= {
2891 .s_power
= smiapp_set_power
,
2894 static const struct v4l2_subdev_pad_ops smiapp_pad_ops
= {
2895 .enum_mbus_code
= smiapp_enum_mbus_code
,
2896 .get_fmt
= smiapp_get_format
,
2897 .set_fmt
= smiapp_set_format
,
2898 .get_selection
= smiapp_get_selection
,
2899 .set_selection
= smiapp_set_selection
,
2902 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops
= {
2903 .g_skip_frames
= smiapp_get_skip_frames
,
2904 .g_skip_top_lines
= smiapp_get_skip_top_lines
,
2907 static const struct v4l2_subdev_ops smiapp_ops
= {
2908 .core
= &smiapp_core_ops
,
2909 .video
= &smiapp_video_ops
,
2910 .pad
= &smiapp_pad_ops
,
2911 .sensor
= &smiapp_sensor_ops
,
2914 static const struct media_entity_operations smiapp_entity_ops
= {
2915 .link_validate
= v4l2_subdev_link_validate
,
2918 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops
= {
2919 .registered
= smiapp_registered
,
2920 .open
= smiapp_open
,
2921 .close
= smiapp_close
,
2924 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
= {
2925 .open
= smiapp_open
,
2926 .close
= smiapp_close
,
2929 /* -----------------------------------------------------------------------------
2935 static int smiapp_suspend(struct device
*dev
)
2937 struct i2c_client
*client
= to_i2c_client(dev
);
2938 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2939 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2942 BUG_ON(mutex_is_locked(&sensor
->mutex
));
2944 if (sensor
->power_count
== 0)
2947 if (sensor
->streaming
)
2948 smiapp_stop_streaming(sensor
);
2950 streaming
= sensor
->streaming
;
2952 smiapp_power_off(sensor
);
2954 /* save state for resume */
2955 sensor
->streaming
= streaming
;
2960 static int smiapp_resume(struct device
*dev
)
2962 struct i2c_client
*client
= to_i2c_client(dev
);
2963 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2964 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2967 if (sensor
->power_count
== 0)
2970 rval
= smiapp_power_on(sensor
);
2974 if (sensor
->streaming
)
2975 rval
= smiapp_start_streaming(sensor
);
2982 #define smiapp_suspend NULL
2983 #define smiapp_resume NULL
2985 #endif /* CONFIG_PM */
2987 static struct smiapp_platform_data
*smiapp_get_pdata(struct device
*dev
)
2989 struct smiapp_platform_data
*pdata
;
2990 struct v4l2_of_endpoint
*bus_cfg
;
2991 struct device_node
*ep
;
2996 return dev
->platform_data
;
2998 ep
= of_graph_get_next_endpoint(dev
->of_node
, NULL
);
3002 bus_cfg
= v4l2_of_alloc_parse_endpoint(ep
);
3003 if (IS_ERR(bus_cfg
))
3006 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
3010 switch (bus_cfg
->bus_type
) {
3011 case V4L2_MBUS_CSI2
:
3012 pdata
->csi_signalling_mode
= SMIAPP_CSI_SIGNALLING_MODE_CSI2
;
3014 /* FIXME: add CCP2 support. */
3019 pdata
->lanes
= bus_cfg
->bus
.mipi_csi2
.num_data_lanes
;
3020 dev_dbg(dev
, "lanes %u\n", pdata
->lanes
);
3022 /* xshutdown GPIO is optional */
3023 pdata
->xshutdown
= of_get_named_gpio(dev
->of_node
, "reset-gpios", 0);
3025 /* NVM size is not mandatory */
3026 of_property_read_u32(dev
->of_node
, "nokia,nvm-size",
3029 rval
= of_property_read_u32(dev
->of_node
, "clock-frequency",
3032 dev_warn(dev
, "can't get clock-frequency\n");
3036 dev_dbg(dev
, "reset %d, nvm %d, clk %d, csi %d\n", pdata
->xshutdown
,
3037 pdata
->nvm_size
, pdata
->ext_clk
, pdata
->csi_signalling_mode
);
3039 if (!bus_cfg
->nr_of_link_frequencies
) {
3040 dev_warn(dev
, "no link frequencies defined\n");
3044 pdata
->op_sys_clock
= devm_kcalloc(
3045 dev
, bus_cfg
->nr_of_link_frequencies
+ 1 /* guardian */,
3046 sizeof(*pdata
->op_sys_clock
), GFP_KERNEL
);
3047 if (!pdata
->op_sys_clock
)
3050 for (i
= 0; i
< bus_cfg
->nr_of_link_frequencies
; i
++) {
3051 pdata
->op_sys_clock
[i
] = bus_cfg
->link_frequencies
[i
];
3052 dev_dbg(dev
, "freq %d: %lld\n", i
, pdata
->op_sys_clock
[i
]);
3055 v4l2_of_free_endpoint(bus_cfg
);
3060 v4l2_of_free_endpoint(bus_cfg
);
3065 static int smiapp_probe(struct i2c_client
*client
,
3066 const struct i2c_device_id
*devid
)
3068 struct smiapp_sensor
*sensor
;
3069 struct smiapp_platform_data
*pdata
= smiapp_get_pdata(&client
->dev
);
3075 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
3079 sensor
->platform_data
= pdata
;
3080 mutex_init(&sensor
->mutex
);
3081 mutex_init(&sensor
->power_mutex
);
3082 sensor
->src
= &sensor
->ssds
[sensor
->ssds_used
];
3084 v4l2_i2c_subdev_init(&sensor
->src
->sd
, client
, &smiapp_ops
);
3085 sensor
->src
->sd
.internal_ops
= &smiapp_internal_src_ops
;
3086 sensor
->src
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
3087 sensor
->src
->sensor
= sensor
;
3089 sensor
->src
->pads
[0].flags
= MEDIA_PAD_FL_SOURCE
;
3090 rval
= media_entity_pads_init(&sensor
->src
->sd
.entity
, 2,
3095 if (client
->dev
.of_node
) {
3096 rval
= smiapp_init(sensor
);
3098 goto out_media_entity_cleanup
;
3101 rval
= v4l2_async_register_subdev(&sensor
->src
->sd
);
3103 goto out_media_entity_cleanup
;
3107 out_media_entity_cleanup
:
3108 media_entity_cleanup(&sensor
->src
->sd
.entity
);
3113 static int smiapp_remove(struct i2c_client
*client
)
3115 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
3116 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
3119 v4l2_async_unregister_subdev(subdev
);
3121 if (sensor
->power_count
) {
3122 if (gpio_is_valid(sensor
->platform_data
->xshutdown
))
3123 gpio_set_value(sensor
->platform_data
->xshutdown
, 0);
3124 if (sensor
->platform_data
->set_xclk
)
3125 sensor
->platform_data
->set_xclk(&sensor
->src
->sd
, 0);
3127 clk_disable_unprepare(sensor
->ext_clk
);
3128 sensor
->power_count
= 0;
3131 for (i
= 0; i
< sensor
->ssds_used
; i
++) {
3132 v4l2_device_unregister_subdev(&sensor
->ssds
[i
].sd
);
3133 media_entity_cleanup(&sensor
->ssds
[i
].sd
.entity
);
3135 smiapp_cleanup(sensor
);
3140 static const struct of_device_id smiapp_of_table
[] = {
3141 { .compatible
= "nokia,smia" },
3144 MODULE_DEVICE_TABLE(of
, smiapp_of_table
);
3146 static const struct i2c_device_id smiapp_id_table
[] = {
3150 MODULE_DEVICE_TABLE(i2c
, smiapp_id_table
);
3152 static const struct dev_pm_ops smiapp_pm_ops
= {
3153 .suspend
= smiapp_suspend
,
3154 .resume
= smiapp_resume
,
3157 static struct i2c_driver smiapp_i2c_driver
= {
3159 .of_match_table
= smiapp_of_table
,
3160 .name
= SMIAPP_NAME
,
3161 .pm
= &smiapp_pm_ops
,
3163 .probe
= smiapp_probe
,
3164 .remove
= smiapp_remove
,
3165 .id_table
= smiapp_id_table
,
3168 module_i2c_driver(smiapp_i2c_driver
);
3170 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3171 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3172 MODULE_LICENSE("GPL");