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/gpio/consumer.h>
28 #include <linux/module.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_SGRBG16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_GRBG
, },
332 { MEDIA_BUS_FMT_SRGGB16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_RGGB
, },
333 { MEDIA_BUS_FMT_SBGGR16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_BGGR
, },
334 { MEDIA_BUS_FMT_SGBRG16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_GBRG
, },
335 { MEDIA_BUS_FMT_SGRBG14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_GRBG
, },
336 { MEDIA_BUS_FMT_SRGGB14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_RGGB
, },
337 { MEDIA_BUS_FMT_SBGGR14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_BGGR
, },
338 { MEDIA_BUS_FMT_SGBRG14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_GBRG
, },
339 { MEDIA_BUS_FMT_SGRBG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GRBG
, },
340 { MEDIA_BUS_FMT_SRGGB12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_RGGB
, },
341 { MEDIA_BUS_FMT_SBGGR12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_BGGR
, },
342 { MEDIA_BUS_FMT_SGBRG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GBRG
, },
343 { MEDIA_BUS_FMT_SGRBG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GRBG
, },
344 { MEDIA_BUS_FMT_SRGGB10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_RGGB
, },
345 { MEDIA_BUS_FMT_SBGGR10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_BGGR
, },
346 { MEDIA_BUS_FMT_SGBRG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GBRG
, },
347 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
348 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
349 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
350 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
351 { MEDIA_BUS_FMT_SGRBG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
352 { MEDIA_BUS_FMT_SRGGB8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
353 { MEDIA_BUS_FMT_SBGGR8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
354 { MEDIA_BUS_FMT_SGBRG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
357 static const char *pixel_order_str
[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
359 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
360 - (unsigned long)smiapp_csi_data_formats) \
361 / sizeof(*smiapp_csi_data_formats))
363 static u32
smiapp_pixel_order(struct smiapp_sensor
*sensor
)
365 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
369 if (sensor
->hflip
->val
)
370 flip
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
372 if (sensor
->vflip
->val
)
373 flip
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
376 flip
^= sensor
->hvflip_inv_mask
;
378 dev_dbg(&client
->dev
, "flip %d\n", flip
);
379 return sensor
->default_pixel_order
^ flip
;
382 static void smiapp_update_mbus_formats(struct smiapp_sensor
*sensor
)
384 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
385 unsigned int csi_format_idx
=
386 to_csi_format_idx(sensor
->csi_format
) & ~3;
387 unsigned int internal_csi_format_idx
=
388 to_csi_format_idx(sensor
->internal_csi_format
) & ~3;
389 unsigned int pixel_order
= smiapp_pixel_order(sensor
);
391 sensor
->mbus_frame_fmts
=
392 sensor
->default_mbus_frame_fmts
<< pixel_order
;
394 &smiapp_csi_data_formats
[csi_format_idx
+ pixel_order
];
395 sensor
->internal_csi_format
=
396 &smiapp_csi_data_formats
[internal_csi_format_idx
399 BUG_ON(max(internal_csi_format_idx
, csi_format_idx
) + pixel_order
400 >= ARRAY_SIZE(smiapp_csi_data_formats
));
402 dev_dbg(&client
->dev
, "new pixel order %s\n",
403 pixel_order_str
[pixel_order
]);
406 static const char * const smiapp_test_patterns
[] = {
409 "Eight Vertical Colour Bars",
410 "Colour Bars With Fade to Grey",
411 "Pseudorandom Sequence (PN9)",
414 static int smiapp_set_ctrl(struct v4l2_ctrl
*ctrl
)
416 struct smiapp_sensor
*sensor
=
417 container_of(ctrl
->handler
, struct smiapp_subdev
, ctrl_handler
)
424 case V4L2_CID_ANALOGUE_GAIN
:
427 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL
, ctrl
->val
);
429 case V4L2_CID_EXPOSURE
:
432 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME
, ctrl
->val
);
436 if (sensor
->streaming
)
439 if (sensor
->hflip
->val
)
440 orient
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
442 if (sensor
->vflip
->val
)
443 orient
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
445 orient
^= sensor
->hvflip_inv_mask
;
446 rval
= smiapp_write(sensor
,
447 SMIAPP_REG_U8_IMAGE_ORIENTATION
,
452 smiapp_update_mbus_formats(sensor
);
456 case V4L2_CID_VBLANK
:
457 exposure
= sensor
->exposure
->val
;
459 __smiapp_update_exposure_limits(sensor
);
461 if (exposure
> sensor
->exposure
->maximum
) {
462 sensor
->exposure
->val
=
463 sensor
->exposure
->maximum
;
464 rval
= smiapp_set_ctrl(
471 sensor
, SMIAPP_REG_U16_FRAME_LENGTH_LINES
,
472 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
475 case V4L2_CID_HBLANK
:
477 sensor
, SMIAPP_REG_U16_LINE_LENGTH_PCK
,
478 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
481 case V4L2_CID_LINK_FREQ
:
482 if (sensor
->streaming
)
485 return smiapp_pll_update(sensor
);
487 case V4L2_CID_TEST_PATTERN
: {
490 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
492 sensor
->test_data
[i
],
494 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR
);
497 sensor
, SMIAPP_REG_U16_TEST_PATTERN_MODE
, ctrl
->val
);
500 case V4L2_CID_TEST_PATTERN_RED
:
502 sensor
, SMIAPP_REG_U16_TEST_DATA_RED
, ctrl
->val
);
504 case V4L2_CID_TEST_PATTERN_GREENR
:
506 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENR
, ctrl
->val
);
508 case V4L2_CID_TEST_PATTERN_BLUE
:
510 sensor
, SMIAPP_REG_U16_TEST_DATA_BLUE
, ctrl
->val
);
512 case V4L2_CID_TEST_PATTERN_GREENB
:
514 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENB
, ctrl
->val
);
516 case V4L2_CID_PIXEL_RATE
:
517 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
525 static const struct v4l2_ctrl_ops smiapp_ctrl_ops
= {
526 .s_ctrl
= smiapp_set_ctrl
,
529 static int smiapp_init_controls(struct smiapp_sensor
*sensor
)
531 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
534 rval
= v4l2_ctrl_handler_init(&sensor
->pixel_array
->ctrl_handler
, 12);
538 sensor
->pixel_array
->ctrl_handler
.lock
= &sensor
->mutex
;
540 sensor
->analog_gain
= v4l2_ctrl_new_std(
541 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
542 V4L2_CID_ANALOGUE_GAIN
,
543 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN
],
544 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX
],
545 max(sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP
], 1U),
546 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN
]);
548 /* Exposure limits will be updated soon, use just something here. */
549 sensor
->exposure
= v4l2_ctrl_new_std(
550 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
551 V4L2_CID_EXPOSURE
, 0, 0, 1, 0);
553 sensor
->hflip
= v4l2_ctrl_new_std(
554 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
555 V4L2_CID_HFLIP
, 0, 1, 1, 0);
556 sensor
->vflip
= v4l2_ctrl_new_std(
557 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
558 V4L2_CID_VFLIP
, 0, 1, 1, 0);
560 sensor
->vblank
= v4l2_ctrl_new_std(
561 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
562 V4L2_CID_VBLANK
, 0, 1, 1, 0);
565 sensor
->vblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
567 sensor
->hblank
= v4l2_ctrl_new_std(
568 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
569 V4L2_CID_HBLANK
, 0, 1, 1, 0);
572 sensor
->hblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
574 sensor
->pixel_rate_parray
= v4l2_ctrl_new_std(
575 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
576 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
578 v4l2_ctrl_new_std_menu_items(&sensor
->pixel_array
->ctrl_handler
,
579 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
580 ARRAY_SIZE(smiapp_test_patterns
) - 1,
581 0, 0, smiapp_test_patterns
);
583 if (sensor
->pixel_array
->ctrl_handler
.error
) {
584 dev_err(&client
->dev
,
585 "pixel array controls initialization failed (%d)\n",
586 sensor
->pixel_array
->ctrl_handler
.error
);
587 return sensor
->pixel_array
->ctrl_handler
.error
;
590 sensor
->pixel_array
->sd
.ctrl_handler
=
591 &sensor
->pixel_array
->ctrl_handler
;
593 v4l2_ctrl_cluster(2, &sensor
->hflip
);
595 rval
= v4l2_ctrl_handler_init(&sensor
->src
->ctrl_handler
, 0);
599 sensor
->src
->ctrl_handler
.lock
= &sensor
->mutex
;
601 sensor
->pixel_rate_csi
= v4l2_ctrl_new_std(
602 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
603 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
605 if (sensor
->src
->ctrl_handler
.error
) {
606 dev_err(&client
->dev
,
607 "src controls initialization failed (%d)\n",
608 sensor
->src
->ctrl_handler
.error
);
609 return sensor
->src
->ctrl_handler
.error
;
612 sensor
->src
->sd
.ctrl_handler
= &sensor
->src
->ctrl_handler
;
618 * For controls that require information on available media bus codes
619 * and linke frequencies.
621 static int smiapp_init_late_controls(struct smiapp_sensor
*sensor
)
623 unsigned long *valid_link_freqs
= &sensor
->valid_link_freqs
[
624 sensor
->csi_format
->compressed
- SMIAPP_COMPRESSED_BASE
];
627 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++) {
628 int max_value
= (1 << sensor
->csi_format
->width
) - 1;
630 sensor
->test_data
[i
] = v4l2_ctrl_new_std(
631 &sensor
->pixel_array
->ctrl_handler
,
632 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN_RED
+ i
,
633 0, max_value
, 1, max_value
);
636 for (max
= 0; sensor
->hwcfg
->op_sys_clock
[max
+ 1]; max
++);
638 sensor
->link_freq
= v4l2_ctrl_new_int_menu(
639 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
640 V4L2_CID_LINK_FREQ
, __fls(*valid_link_freqs
),
641 __ffs(*valid_link_freqs
), sensor
->hwcfg
->op_sys_clock
);
643 return sensor
->src
->ctrl_handler
.error
;
646 static void smiapp_free_controls(struct smiapp_sensor
*sensor
)
650 for (i
= 0; i
< sensor
->ssds_used
; i
++)
651 v4l2_ctrl_handler_free(&sensor
->ssds
[i
].ctrl_handler
);
654 static int smiapp_get_limits(struct smiapp_sensor
*sensor
, int const *limit
,
657 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
662 for (i
= 0; i
< n
; i
++) {
664 sensor
, smiapp_reg_limits
[limit
[i
]].addr
, &val
);
667 sensor
->limits
[limit
[i
]] = val
;
668 dev_dbg(&client
->dev
, "0x%8.8x \"%s\" = %u, 0x%x\n",
669 smiapp_reg_limits
[limit
[i
]].addr
,
670 smiapp_reg_limits
[limit
[i
]].what
, val
, val
);
676 static int smiapp_get_all_limits(struct smiapp_sensor
*sensor
)
681 for (i
= 0; i
< SMIAPP_LIMIT_LAST
; i
++) {
682 rval
= smiapp_get_limits(sensor
, &i
, 1);
687 if (sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] == 0)
688 smiapp_replace_limit(sensor
, SMIAPP_LIMIT_SCALER_N_MIN
, 16);
693 static int smiapp_get_limits_binning(struct smiapp_sensor
*sensor
)
695 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
696 static u32
const limits
[] = {
697 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
,
698 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN
,
699 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
,
700 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN
,
701 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
,
702 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN
,
703 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN
,
705 static u32
const limits_replace
[] = {
706 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES
,
707 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES
,
708 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK
,
709 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK
,
710 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK
,
711 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN
,
712 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN
,
717 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
] ==
718 SMIAPP_BINNING_CAPABILITY_NO
) {
719 for (i
= 0; i
< ARRAY_SIZE(limits
); i
++)
720 sensor
->limits
[limits
[i
]] =
721 sensor
->limits
[limits_replace
[i
]];
726 rval
= smiapp_get_limits(sensor
, limits
, ARRAY_SIZE(limits
));
731 * Sanity check whether the binning limits are valid. If not,
732 * use the non-binning ones.
734 if (sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
]
735 && sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
]
736 && sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
])
739 for (i
= 0; i
< ARRAY_SIZE(limits
); i
++) {
740 dev_dbg(&client
->dev
,
741 "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
742 smiapp_reg_limits
[limits
[i
]].addr
,
743 smiapp_reg_limits
[limits
[i
]].what
,
744 sensor
->limits
[limits_replace
[i
]],
745 sensor
->limits
[limits_replace
[i
]]);
746 sensor
->limits
[limits
[i
]] =
747 sensor
->limits
[limits_replace
[i
]];
753 static int smiapp_get_mbus_formats(struct smiapp_sensor
*sensor
)
755 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
756 struct smiapp_pll
*pll
= &sensor
->pll
;
757 unsigned int type
, n
;
758 unsigned int i
, pixel_order
;
762 sensor
, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE
, &type
);
766 dev_dbg(&client
->dev
, "data_format_model_type %d\n", type
);
768 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_PIXEL_ORDER
,
773 if (pixel_order
>= ARRAY_SIZE(pixel_order_str
)) {
774 dev_dbg(&client
->dev
, "bad pixel order %d\n", pixel_order
);
778 dev_dbg(&client
->dev
, "pixel order %d (%s)\n", pixel_order
,
779 pixel_order_str
[pixel_order
]);
782 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL
:
783 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N
;
785 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED
:
786 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N
;
792 sensor
->default_pixel_order
= pixel_order
;
793 sensor
->mbus_frame_fmts
= 0;
795 for (i
= 0; i
< n
; i
++) {
800 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i
), &fmt
);
804 dev_dbg(&client
->dev
, "%u: bpp %u, compressed %u\n",
805 i
, fmt
>> 8, (u8
)fmt
);
807 for (j
= 0; j
< ARRAY_SIZE(smiapp_csi_data_formats
); j
++) {
808 const struct smiapp_csi_data_format
*f
=
809 &smiapp_csi_data_formats
[j
];
811 if (f
->pixel_order
!= SMIAPP_PIXEL_ORDER_GRBG
)
814 if (f
->width
!= fmt
>> 8 || f
->compressed
!= (u8
)fmt
)
817 dev_dbg(&client
->dev
, "jolly good! %d\n", j
);
819 sensor
->default_mbus_frame_fmts
|= 1 << j
;
823 /* Figure out which BPP values can be used with which formats. */
824 pll
->binning_horizontal
= 1;
825 pll
->binning_vertical
= 1;
826 pll
->scale_m
= sensor
->scale_m
;
828 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
829 const struct smiapp_csi_data_format
*f
=
830 &smiapp_csi_data_formats
[i
];
831 unsigned long *valid_link_freqs
=
832 &sensor
->valid_link_freqs
[
833 f
->compressed
- SMIAPP_COMPRESSED_BASE
];
836 BUG_ON(f
->compressed
< SMIAPP_COMPRESSED_BASE
);
837 BUG_ON(f
->compressed
> SMIAPP_COMPRESSED_MAX
);
839 if (!(sensor
->default_mbus_frame_fmts
& 1 << i
))
842 pll
->bits_per_pixel
= f
->compressed
;
844 for (j
= 0; sensor
->hwcfg
->op_sys_clock
[j
]; j
++) {
845 pll
->link_freq
= sensor
->hwcfg
->op_sys_clock
[j
];
847 rval
= smiapp_pll_try(sensor
, pll
);
848 dev_dbg(&client
->dev
, "link freq %u Hz, bpp %u %s\n",
849 pll
->link_freq
, pll
->bits_per_pixel
,
850 rval
? "not ok" : "ok");
854 set_bit(j
, valid_link_freqs
);
857 if (!*valid_link_freqs
) {
858 dev_info(&client
->dev
,
859 "no valid link frequencies for %u bpp\n",
861 sensor
->default_mbus_frame_fmts
&= ~BIT(i
);
865 if (!sensor
->csi_format
866 || f
->width
> sensor
->csi_format
->width
867 || (f
->width
== sensor
->csi_format
->width
868 && f
->compressed
> sensor
->csi_format
->compressed
)) {
869 sensor
->csi_format
= f
;
870 sensor
->internal_csi_format
= f
;
874 if (!sensor
->csi_format
) {
875 dev_err(&client
->dev
, "no supported mbus code found\n");
879 smiapp_update_mbus_formats(sensor
);
884 static void smiapp_update_blanking(struct smiapp_sensor
*sensor
)
886 struct v4l2_ctrl
*vblank
= sensor
->vblank
;
887 struct v4l2_ctrl
*hblank
= sensor
->hblank
;
891 sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES
],
892 sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
] -
893 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
);
894 max
= sensor
->limits
[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN
] -
895 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
;
897 __v4l2_ctrl_modify_range(vblank
, min
, max
, vblank
->step
, min
);
900 sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
] -
901 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
,
902 sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
]);
903 max
= sensor
->limits
[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN
] -
904 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
;
906 __v4l2_ctrl_modify_range(hblank
, min
, max
, hblank
->step
, min
);
908 __smiapp_update_exposure_limits(sensor
);
911 static int smiapp_update_mode(struct smiapp_sensor
*sensor
)
913 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
914 unsigned int binning_mode
;
917 dev_dbg(&client
->dev
, "frame size: %dx%d\n",
918 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
,
919 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
920 dev_dbg(&client
->dev
, "csi format width: %d\n",
921 sensor
->csi_format
->width
);
923 /* Binning has to be set up here; it affects limits */
924 if (sensor
->binning_horizontal
== 1 &&
925 sensor
->binning_vertical
== 1) {
929 (sensor
->binning_horizontal
<< 4)
930 | sensor
->binning_vertical
;
933 sensor
, SMIAPP_REG_U8_BINNING_TYPE
, binning_type
);
939 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_BINNING_MODE
, binning_mode
);
943 /* Get updated limits due to binning */
944 rval
= smiapp_get_limits_binning(sensor
);
948 rval
= smiapp_pll_update(sensor
);
952 /* Output from pixel array, including blanking */
953 smiapp_update_blanking(sensor
);
955 dev_dbg(&client
->dev
, "vblank\t\t%d\n", sensor
->vblank
->val
);
956 dev_dbg(&client
->dev
, "hblank\t\t%d\n", sensor
->hblank
->val
);
958 dev_dbg(&client
->dev
, "real timeperframe\t100/%d\n",
959 sensor
->pll
.pixel_rate_pixel_array
/
960 ((sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
961 + sensor
->hblank
->val
) *
962 (sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
963 + sensor
->vblank
->val
) / 100));
970 * SMIA++ NVM handling
973 static int smiapp_read_nvm(struct smiapp_sensor
*sensor
,
979 np
= sensor
->nvm_size
/ SMIAPP_NVM_PAGE_SIZE
;
980 for (p
= 0; p
< np
; p
++) {
983 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT
, p
);
987 rval
= smiapp_write(sensor
,
988 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
,
989 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN
|
990 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN
);
994 for (i
= 0; i
< 1000; i
++) {
997 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS
, &s
);
1002 if (s
& SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY
)
1012 for (i
= 0; i
< SMIAPP_NVM_PAGE_SIZE
; i
++) {
1015 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0
+ i
,
1025 rval2
= smiapp_write(sensor
, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
, 0);
1034 * SMIA++ CCI address control
1037 static int smiapp_change_cci_addr(struct smiapp_sensor
*sensor
)
1039 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1043 client
->addr
= sensor
->hwcfg
->i2c_addr_dfl
;
1045 rval
= smiapp_write(sensor
,
1046 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
,
1047 sensor
->hwcfg
->i2c_addr_alt
<< 1);
1051 client
->addr
= sensor
->hwcfg
->i2c_addr_alt
;
1053 /* verify addr change went ok */
1054 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
, &val
);
1058 if (val
!= sensor
->hwcfg
->i2c_addr_alt
<< 1)
1066 * SMIA++ Mode Control
1069 static int smiapp_setup_flash_strobe(struct smiapp_sensor
*sensor
)
1071 struct smiapp_flash_strobe_parms
*strobe_setup
;
1072 unsigned int ext_freq
= sensor
->hwcfg
->ext_clk
;
1074 u32 strobe_adjustment
;
1075 u32 strobe_width_high_rs
;
1078 strobe_setup
= sensor
->hwcfg
->strobe_setup
;
1081 * How to calculate registers related to strobe length. Please
1082 * do not change, or if you do at least know what you're
1085 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1087 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1088 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1090 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1091 * flash_strobe_adjustment E N, [1 - 0xff]
1093 * The formula above is written as below to keep it on one
1096 * l / 10^6 = w / e * a
1098 * Let's mark w * a by x:
1106 * The strobe width must be at least as long as requested,
1107 * thus rounding upwards is needed.
1109 * x = (l * e + 10^6 - 1) / 10^6
1110 * -----------------------------
1112 * Maximum possible accuracy is wanted at all times. Thus keep
1113 * a as small as possible.
1115 * Calculate a, assuming maximum w, with rounding upwards:
1117 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1118 * -------------------------------------
1120 * Thus, we also get w, with that a, with rounding upwards:
1122 * w = (x + a - 1) / a
1123 * -------------------
1127 * x E [1, (2^16 - 1) * (2^8 - 1)]
1129 * Substituting maximum x to the original formula (with rounding),
1130 * the maximum l is thus
1132 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1134 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1135 * --------------------------------------------------
1137 * flash_strobe_length must be clamped between 1 and
1138 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1142 * flash_strobe_adjustment = ((flash_strobe_length *
1143 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1145 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1146 * EXTCLK freq + 10^6 - 1) / 10^6 +
1147 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1149 tmp
= div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1150 1000000 + 1, ext_freq
);
1151 strobe_setup
->strobe_width_high_us
=
1152 clamp_t(u32
, strobe_setup
->strobe_width_high_us
, 1, tmp
);
1154 tmp
= div_u64(((u64
)strobe_setup
->strobe_width_high_us
* (u64
)ext_freq
+
1155 1000000 - 1), 1000000ULL);
1156 strobe_adjustment
= (tmp
+ (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1157 strobe_width_high_rs
= (tmp
+ strobe_adjustment
- 1) /
1160 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_MODE_RS
,
1161 strobe_setup
->mode
);
1165 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT
,
1170 rval
= smiapp_write(
1171 sensor
, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL
,
1172 strobe_width_high_rs
);
1176 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL
,
1177 strobe_setup
->strobe_delay
);
1181 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_FLASH_STROBE_START_POINT
,
1182 strobe_setup
->stobe_start_point
);
1186 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_TRIGGER_RS
,
1187 strobe_setup
->trigger
);
1190 sensor
->hwcfg
->strobe_setup
->trigger
= 0;
1195 /* -----------------------------------------------------------------------------
1199 static int smiapp_power_on(struct smiapp_sensor
*sensor
)
1201 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1205 rval
= regulator_enable(sensor
->vana
);
1207 dev_err(&client
->dev
, "failed to enable vana regulator\n");
1210 usleep_range(1000, 1000);
1212 rval
= clk_prepare_enable(sensor
->ext_clk
);
1214 dev_dbg(&client
->dev
, "failed to enable xclk\n");
1217 usleep_range(1000, 1000);
1219 gpiod_set_value(sensor
->xshutdown
, 1);
1221 sleep
= SMIAPP_RESET_DELAY(sensor
->hwcfg
->ext_clk
);
1222 usleep_range(sleep
, sleep
);
1225 * Failures to respond to the address change command have been noticed.
1226 * Those failures seem to be caused by the sensor requiring a longer
1227 * boot time than advertised. An additional 10ms delay seems to work
1228 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1229 * unnecessary. The failures need to be investigated to find a proper
1230 * fix, and a delay will likely need to be added here if the I2C write
1231 * retry hack is reverted before the root cause of the boot time issue
1235 if (sensor
->hwcfg
->i2c_addr_alt
) {
1236 rval
= smiapp_change_cci_addr(sensor
);
1238 dev_err(&client
->dev
, "cci address change error\n");
1239 goto out_cci_addr_fail
;
1243 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_SOFTWARE_RESET
,
1244 SMIAPP_SOFTWARE_RESET
);
1246 dev_err(&client
->dev
, "software reset failed\n");
1247 goto out_cci_addr_fail
;
1250 if (sensor
->hwcfg
->i2c_addr_alt
) {
1251 rval
= smiapp_change_cci_addr(sensor
);
1253 dev_err(&client
->dev
, "cci address change error\n");
1254 goto out_cci_addr_fail
;
1258 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_COMPRESSION_MODE
,
1259 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR
);
1261 dev_err(&client
->dev
, "compression mode set failed\n");
1262 goto out_cci_addr_fail
;
1265 rval
= smiapp_write(
1266 sensor
, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ
,
1267 sensor
->hwcfg
->ext_clk
/ (1000000 / (1 << 8)));
1269 dev_err(&client
->dev
, "extclk frequency set failed\n");
1270 goto out_cci_addr_fail
;
1273 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_LANE_MODE
,
1274 sensor
->hwcfg
->lanes
- 1);
1276 dev_err(&client
->dev
, "csi lane mode set failed\n");
1277 goto out_cci_addr_fail
;
1280 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FAST_STANDBY_CTRL
,
1281 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE
);
1283 dev_err(&client
->dev
, "fast standby set failed\n");
1284 goto out_cci_addr_fail
;
1287 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_SIGNALLING_MODE
,
1288 sensor
->hwcfg
->csi_signalling_mode
);
1290 dev_err(&client
->dev
, "csi signalling mode set failed\n");
1291 goto out_cci_addr_fail
;
1294 /* DPHY control done by sensor based on requested link rate */
1295 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_DPHY_CTRL
,
1296 SMIAPP_DPHY_CTRL_UI
);
1300 rval
= smiapp_call_quirk(sensor
, post_poweron
);
1302 dev_err(&client
->dev
, "post_poweron quirks failed\n");
1303 goto out_cci_addr_fail
;
1306 /* Are we still initialising...? If yes, return here. */
1307 if (!sensor
->pixel_array
)
1310 rval
= v4l2_ctrl_handler_setup(
1311 &sensor
->pixel_array
->ctrl_handler
);
1313 goto out_cci_addr_fail
;
1315 rval
= v4l2_ctrl_handler_setup(&sensor
->src
->ctrl_handler
);
1317 goto out_cci_addr_fail
;
1319 mutex_lock(&sensor
->mutex
);
1320 rval
= smiapp_update_mode(sensor
);
1321 mutex_unlock(&sensor
->mutex
);
1323 goto out_cci_addr_fail
;
1328 gpiod_set_value(sensor
->xshutdown
, 0);
1329 clk_disable_unprepare(sensor
->ext_clk
);
1332 regulator_disable(sensor
->vana
);
1336 static void smiapp_power_off(struct smiapp_sensor
*sensor
)
1339 * Currently power/clock to lens are enable/disabled separately
1340 * but they are essentially the same signals. So if the sensor is
1341 * powered off while the lens is powered on the sensor does not
1342 * really see a power off and next time the cci address change
1343 * will fail. So do a soft reset explicitly here.
1345 if (sensor
->hwcfg
->i2c_addr_alt
)
1346 smiapp_write(sensor
,
1347 SMIAPP_REG_U8_SOFTWARE_RESET
,
1348 SMIAPP_SOFTWARE_RESET
);
1350 gpiod_set_value(sensor
->xshutdown
, 0);
1351 clk_disable_unprepare(sensor
->ext_clk
);
1352 usleep_range(5000, 5000);
1353 regulator_disable(sensor
->vana
);
1354 sensor
->streaming
= false;
1357 static int smiapp_set_power(struct v4l2_subdev
*subdev
, int on
)
1359 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1362 mutex_lock(&sensor
->power_mutex
);
1364 if (on
&& !sensor
->power_count
) {
1365 /* Power on and perform initialisation. */
1366 ret
= smiapp_power_on(sensor
);
1369 } else if (!on
&& sensor
->power_count
== 1) {
1370 smiapp_power_off(sensor
);
1373 /* Update the power count. */
1374 sensor
->power_count
+= on
? 1 : -1;
1375 WARN_ON(sensor
->power_count
< 0);
1378 mutex_unlock(&sensor
->power_mutex
);
1382 /* -----------------------------------------------------------------------------
1383 * Video stream management
1386 static int smiapp_start_streaming(struct smiapp_sensor
*sensor
)
1388 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1391 mutex_lock(&sensor
->mutex
);
1393 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_CSI_DATA_FORMAT
,
1394 (sensor
->csi_format
->width
<< 8) |
1395 sensor
->csi_format
->compressed
);
1399 rval
= smiapp_pll_configure(sensor
);
1403 /* Analog crop start coordinates */
1404 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_ADDR_START
,
1405 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
);
1409 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_ADDR_START
,
1410 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
);
1414 /* Analog crop end coordinates */
1415 rval
= smiapp_write(
1416 sensor
, SMIAPP_REG_U16_X_ADDR_END
,
1417 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
1418 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
- 1);
1422 rval
= smiapp_write(
1423 sensor
, SMIAPP_REG_U16_Y_ADDR_END
,
1424 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
1425 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
- 1);
1430 * Output from pixel array, including blanking, is set using
1431 * controls below. No need to set here.
1435 if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
1436 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
1437 rval
= smiapp_write(
1438 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET
,
1439 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].left
);
1443 rval
= smiapp_write(
1444 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET
,
1445 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].top
);
1449 rval
= smiapp_write(
1450 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH
,
1451 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].width
);
1455 rval
= smiapp_write(
1456 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT
,
1457 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].height
);
1463 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
1464 != SMIAPP_SCALING_CAPABILITY_NONE
) {
1465 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALING_MODE
,
1466 sensor
->scaling_mode
);
1470 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALE_M
,
1476 /* Output size from sensor */
1477 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_OUTPUT_SIZE
,
1478 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
);
1481 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_OUTPUT_SIZE
,
1482 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
1486 if ((sensor
->limits
[SMIAPP_LIMIT_FLASH_MODE_CAPABILITY
] &
1487 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE
|
1488 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE
)) &&
1489 sensor
->hwcfg
->strobe_setup
!= NULL
&&
1490 sensor
->hwcfg
->strobe_setup
->trigger
!= 0) {
1491 rval
= smiapp_setup_flash_strobe(sensor
);
1496 rval
= smiapp_call_quirk(sensor
, pre_streamon
);
1498 dev_err(&client
->dev
, "pre_streamon quirks failed\n");
1502 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1503 SMIAPP_MODE_SELECT_STREAMING
);
1506 mutex_unlock(&sensor
->mutex
);
1511 static int smiapp_stop_streaming(struct smiapp_sensor
*sensor
)
1513 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1516 mutex_lock(&sensor
->mutex
);
1517 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1518 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY
);
1522 rval
= smiapp_call_quirk(sensor
, post_streamoff
);
1524 dev_err(&client
->dev
, "post_streamoff quirks failed\n");
1527 mutex_unlock(&sensor
->mutex
);
1531 /* -----------------------------------------------------------------------------
1532 * V4L2 subdev video operations
1535 static int smiapp_set_stream(struct v4l2_subdev
*subdev
, int enable
)
1537 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1540 if (sensor
->streaming
== enable
)
1544 sensor
->streaming
= true;
1545 rval
= smiapp_start_streaming(sensor
);
1547 sensor
->streaming
= false;
1549 rval
= smiapp_stop_streaming(sensor
);
1550 sensor
->streaming
= false;
1556 static int smiapp_enum_mbus_code(struct v4l2_subdev
*subdev
,
1557 struct v4l2_subdev_pad_config
*cfg
,
1558 struct v4l2_subdev_mbus_code_enum
*code
)
1560 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1561 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1566 mutex_lock(&sensor
->mutex
);
1568 dev_err(&client
->dev
, "subdev %s, pad %d, index %d\n",
1569 subdev
->name
, code
->pad
, code
->index
);
1571 if (subdev
!= &sensor
->src
->sd
|| code
->pad
!= SMIAPP_PAD_SRC
) {
1575 code
->code
= sensor
->internal_csi_format
->code
;
1580 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1581 if (sensor
->mbus_frame_fmts
& (1 << i
))
1584 if (idx
== code
->index
) {
1585 code
->code
= smiapp_csi_data_formats
[i
].code
;
1586 dev_err(&client
->dev
, "found index %d, i %d, code %x\n",
1587 code
->index
, i
, code
->code
);
1594 mutex_unlock(&sensor
->mutex
);
1599 static u32
__smiapp_get_mbus_code(struct v4l2_subdev
*subdev
,
1602 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1604 if (subdev
== &sensor
->src
->sd
&& pad
== SMIAPP_PAD_SRC
)
1605 return sensor
->csi_format
->code
;
1607 return sensor
->internal_csi_format
->code
;
1610 static int __smiapp_get_format(struct v4l2_subdev
*subdev
,
1611 struct v4l2_subdev_pad_config
*cfg
,
1612 struct v4l2_subdev_format
*fmt
)
1614 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1616 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1617 fmt
->format
= *v4l2_subdev_get_try_format(subdev
, cfg
, fmt
->pad
);
1619 struct v4l2_rect
*r
;
1621 if (fmt
->pad
== ssd
->source_pad
)
1622 r
= &ssd
->crop
[ssd
->source_pad
];
1626 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1627 fmt
->format
.width
= r
->width
;
1628 fmt
->format
.height
= r
->height
;
1629 fmt
->format
.field
= V4L2_FIELD_NONE
;
1635 static int smiapp_get_format(struct v4l2_subdev
*subdev
,
1636 struct v4l2_subdev_pad_config
*cfg
,
1637 struct v4l2_subdev_format
*fmt
)
1639 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1642 mutex_lock(&sensor
->mutex
);
1643 rval
= __smiapp_get_format(subdev
, cfg
, fmt
);
1644 mutex_unlock(&sensor
->mutex
);
1649 static void smiapp_get_crop_compose(struct v4l2_subdev
*subdev
,
1650 struct v4l2_subdev_pad_config
*cfg
,
1651 struct v4l2_rect
**crops
,
1652 struct v4l2_rect
**comps
, int which
)
1654 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1657 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1659 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++)
1660 crops
[i
] = &ssd
->crop
[i
];
1662 *comps
= &ssd
->compose
;
1665 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++) {
1666 crops
[i
] = v4l2_subdev_get_try_crop(subdev
, cfg
, i
);
1671 *comps
= v4l2_subdev_get_try_compose(subdev
, cfg
,
1678 /* Changes require propagation only on sink pad. */
1679 static void smiapp_propagate(struct v4l2_subdev
*subdev
,
1680 struct v4l2_subdev_pad_config
*cfg
, int which
,
1683 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1684 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1685 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
1687 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, which
);
1690 case V4L2_SEL_TGT_CROP
:
1691 comp
->width
= crops
[SMIAPP_PAD_SINK
]->width
;
1692 comp
->height
= crops
[SMIAPP_PAD_SINK
]->height
;
1693 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1694 if (ssd
== sensor
->scaler
) {
1697 SMIAPP_LIMIT_SCALER_N_MIN
];
1698 sensor
->scaling_mode
=
1699 SMIAPP_SCALING_MODE_NONE
;
1700 } else if (ssd
== sensor
->binner
) {
1701 sensor
->binning_horizontal
= 1;
1702 sensor
->binning_vertical
= 1;
1706 case V4L2_SEL_TGT_COMPOSE
:
1707 *crops
[SMIAPP_PAD_SRC
] = *comp
;
1714 static const struct smiapp_csi_data_format
1715 *smiapp_validate_csi_data_format(struct smiapp_sensor
*sensor
, u32 code
)
1717 const struct smiapp_csi_data_format
*csi_format
= sensor
->csi_format
;
1720 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1721 if (sensor
->mbus_frame_fmts
& (1 << i
)
1722 && smiapp_csi_data_formats
[i
].code
== code
)
1723 return &smiapp_csi_data_formats
[i
];
1729 static int smiapp_set_format_source(struct v4l2_subdev
*subdev
,
1730 struct v4l2_subdev_pad_config
*cfg
,
1731 struct v4l2_subdev_format
*fmt
)
1733 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1734 const struct smiapp_csi_data_format
*csi_format
,
1735 *old_csi_format
= sensor
->csi_format
;
1736 unsigned long *valid_link_freqs
;
1737 u32 code
= fmt
->format
.code
;
1741 rval
= __smiapp_get_format(subdev
, cfg
, fmt
);
1746 * Media bus code is changeable on src subdev's source pad. On
1747 * other source pads we just get format here.
1749 if (subdev
!= &sensor
->src
->sd
)
1752 csi_format
= smiapp_validate_csi_data_format(sensor
, code
);
1754 fmt
->format
.code
= csi_format
->code
;
1756 if (fmt
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1759 sensor
->csi_format
= csi_format
;
1761 if (csi_format
->width
!= old_csi_format
->width
)
1762 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
1763 __v4l2_ctrl_modify_range(
1764 sensor
->test_data
[i
], 0,
1765 (1 << csi_format
->width
) - 1, 1, 0);
1767 if (csi_format
->compressed
== old_csi_format
->compressed
)
1771 &sensor
->valid_link_freqs
[sensor
->csi_format
->compressed
1772 - SMIAPP_COMPRESSED_BASE
];
1774 __v4l2_ctrl_modify_range(
1775 sensor
->link_freq
, 0,
1776 __fls(*valid_link_freqs
), ~*valid_link_freqs
,
1777 __ffs(*valid_link_freqs
));
1779 return smiapp_pll_update(sensor
);
1782 static int smiapp_set_format(struct v4l2_subdev
*subdev
,
1783 struct v4l2_subdev_pad_config
*cfg
,
1784 struct v4l2_subdev_format
*fmt
)
1786 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1787 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1788 struct v4l2_rect
*crops
[SMIAPP_PADS
];
1790 mutex_lock(&sensor
->mutex
);
1792 if (fmt
->pad
== ssd
->source_pad
) {
1795 rval
= smiapp_set_format_source(subdev
, cfg
, fmt
);
1797 mutex_unlock(&sensor
->mutex
);
1802 /* Sink pad. Width and height are changeable here. */
1803 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1804 fmt
->format
.width
&= ~1;
1805 fmt
->format
.height
&= ~1;
1806 fmt
->format
.field
= V4L2_FIELD_NONE
;
1809 clamp(fmt
->format
.width
,
1810 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
1811 sensor
->limits
[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE
]);
1812 fmt
->format
.height
=
1813 clamp(fmt
->format
.height
,
1814 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
1815 sensor
->limits
[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE
]);
1817 smiapp_get_crop_compose(subdev
, cfg
, crops
, NULL
, fmt
->which
);
1819 crops
[ssd
->sink_pad
]->left
= 0;
1820 crops
[ssd
->sink_pad
]->top
= 0;
1821 crops
[ssd
->sink_pad
]->width
= fmt
->format
.width
;
1822 crops
[ssd
->sink_pad
]->height
= fmt
->format
.height
;
1823 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
1824 ssd
->sink_fmt
= *crops
[ssd
->sink_pad
];
1825 smiapp_propagate(subdev
, cfg
, fmt
->which
,
1828 mutex_unlock(&sensor
->mutex
);
1834 * Calculate goodness of scaled image size compared to expected image
1835 * size and flags provided.
1837 #define SCALING_GOODNESS 100000
1838 #define SCALING_GOODNESS_EXTREME 100000000
1839 static int scaling_goodness(struct v4l2_subdev
*subdev
, int w
, int ask_w
,
1840 int h
, int ask_h
, u32 flags
)
1842 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1843 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1851 if (flags
& V4L2_SEL_FLAG_GE
) {
1853 val
-= SCALING_GOODNESS
;
1855 val
-= SCALING_GOODNESS
;
1858 if (flags
& V4L2_SEL_FLAG_LE
) {
1860 val
-= SCALING_GOODNESS
;
1862 val
-= SCALING_GOODNESS
;
1865 val
-= abs(w
- ask_w
);
1866 val
-= abs(h
- ask_h
);
1868 if (w
< sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
])
1869 val
-= SCALING_GOODNESS_EXTREME
;
1871 dev_dbg(&client
->dev
, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1872 w
, ask_h
, h
, ask_h
, val
);
1877 static void smiapp_set_compose_binner(struct v4l2_subdev
*subdev
,
1878 struct v4l2_subdev_pad_config
*cfg
,
1879 struct v4l2_subdev_selection
*sel
,
1880 struct v4l2_rect
**crops
,
1881 struct v4l2_rect
*comp
)
1883 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1885 unsigned int binh
= 1, binv
= 1;
1886 int best
= scaling_goodness(
1888 crops
[SMIAPP_PAD_SINK
]->width
, sel
->r
.width
,
1889 crops
[SMIAPP_PAD_SINK
]->height
, sel
->r
.height
, sel
->flags
);
1891 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
1892 int this = scaling_goodness(
1894 crops
[SMIAPP_PAD_SINK
]->width
1895 / sensor
->binning_subtypes
[i
].horizontal
,
1897 crops
[SMIAPP_PAD_SINK
]->height
1898 / sensor
->binning_subtypes
[i
].vertical
,
1899 sel
->r
.height
, sel
->flags
);
1902 binh
= sensor
->binning_subtypes
[i
].horizontal
;
1903 binv
= sensor
->binning_subtypes
[i
].vertical
;
1907 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1908 sensor
->binning_vertical
= binv
;
1909 sensor
->binning_horizontal
= binh
;
1912 sel
->r
.width
= (crops
[SMIAPP_PAD_SINK
]->width
/ binh
) & ~1;
1913 sel
->r
.height
= (crops
[SMIAPP_PAD_SINK
]->height
/ binv
) & ~1;
1917 * Calculate best scaling ratio and mode for given output resolution.
1919 * Try all of these: horizontal ratio, vertical ratio and smallest
1920 * size possible (horizontally).
1922 * Also try whether horizontal scaler or full scaler gives a better
1925 static void smiapp_set_compose_scaler(struct v4l2_subdev
*subdev
,
1926 struct v4l2_subdev_pad_config
*cfg
,
1927 struct v4l2_subdev_selection
*sel
,
1928 struct v4l2_rect
**crops
,
1929 struct v4l2_rect
*comp
)
1931 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1932 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1933 u32 min
, max
, a
, b
, max_m
;
1934 u32 scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
1935 int mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
1941 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
1942 crops
[SMIAPP_PAD_SINK
]->width
);
1943 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
1944 crops
[SMIAPP_PAD_SINK
]->height
);
1946 a
= crops
[SMIAPP_PAD_SINK
]->width
1947 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] / sel
->r
.width
;
1948 b
= crops
[SMIAPP_PAD_SINK
]->height
1949 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] / sel
->r
.height
;
1950 max_m
= crops
[SMIAPP_PAD_SINK
]->width
1951 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]
1952 / sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
];
1954 a
= clamp(a
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1955 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1956 b
= clamp(b
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1957 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1958 max_m
= clamp(max_m
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1959 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1961 dev_dbg(&client
->dev
, "scaling: a %d b %d max_m %d\n", a
, b
, max_m
);
1963 min
= min(max_m
, min(a
, b
));
1964 max
= min(max_m
, max(a
, b
));
1973 try[ntry
] = min
+ 1;
1976 try[ntry
] = max
+ 1;
1981 for (i
= 0; i
< ntry
; i
++) {
1982 int this = scaling_goodness(
1984 crops
[SMIAPP_PAD_SINK
]->width
1986 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
1988 crops
[SMIAPP_PAD_SINK
]->height
,
1992 dev_dbg(&client
->dev
, "trying factor %d (%d)\n", try[i
], i
);
1996 mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
2000 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2001 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2004 this = scaling_goodness(
2005 subdev
, crops
[SMIAPP_PAD_SINK
]->width
2007 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2009 crops
[SMIAPP_PAD_SINK
]->height
2011 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2017 mode
= SMIAPP_SCALING_MODE_BOTH
;
2023 (crops
[SMIAPP_PAD_SINK
]->width
2025 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]) & ~1;
2026 if (mode
== SMIAPP_SCALING_MODE_BOTH
)
2028 (crops
[SMIAPP_PAD_SINK
]->height
2030 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
])
2033 sel
->r
.height
= crops
[SMIAPP_PAD_SINK
]->height
;
2035 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2036 sensor
->scale_m
= scale_m
;
2037 sensor
->scaling_mode
= mode
;
2040 /* We're only called on source pads. This function sets scaling. */
2041 static int smiapp_set_compose(struct v4l2_subdev
*subdev
,
2042 struct v4l2_subdev_pad_config
*cfg
,
2043 struct v4l2_subdev_selection
*sel
)
2045 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2046 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2047 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2049 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, sel
->which
);
2054 if (ssd
== sensor
->binner
)
2055 smiapp_set_compose_binner(subdev
, cfg
, sel
, crops
, comp
);
2057 smiapp_set_compose_scaler(subdev
, cfg
, sel
, crops
, comp
);
2060 smiapp_propagate(subdev
, cfg
, sel
->which
,
2061 V4L2_SEL_TGT_COMPOSE
);
2063 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
2064 return smiapp_update_mode(sensor
);
2069 static int __smiapp_sel_supported(struct v4l2_subdev
*subdev
,
2070 struct v4l2_subdev_selection
*sel
)
2072 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2073 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2075 /* We only implement crop in three places. */
2076 switch (sel
->target
) {
2077 case V4L2_SEL_TGT_CROP
:
2078 case V4L2_SEL_TGT_CROP_BOUNDS
:
2079 if (ssd
== sensor
->pixel_array
2080 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2082 if (ssd
== sensor
->src
2083 && sel
->pad
== SMIAPP_PAD_SRC
)
2085 if (ssd
== sensor
->scaler
2086 && sel
->pad
== SMIAPP_PAD_SINK
2087 && sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
2088 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
)
2091 case V4L2_SEL_TGT_NATIVE_SIZE
:
2092 if (ssd
== sensor
->pixel_array
2093 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2096 case V4L2_SEL_TGT_COMPOSE
:
2097 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2098 if (sel
->pad
== ssd
->source_pad
)
2100 if (ssd
== sensor
->binner
)
2102 if (ssd
== sensor
->scaler
2103 && sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2104 != SMIAPP_SCALING_CAPABILITY_NONE
)
2112 static int smiapp_set_crop(struct v4l2_subdev
*subdev
,
2113 struct v4l2_subdev_pad_config
*cfg
,
2114 struct v4l2_subdev_selection
*sel
)
2116 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2117 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2118 struct v4l2_rect
*src_size
, *crops
[SMIAPP_PADS
];
2119 struct v4l2_rect _r
;
2121 smiapp_get_crop_compose(subdev
, cfg
, crops
, NULL
, sel
->which
);
2123 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2124 if (sel
->pad
== ssd
->sink_pad
)
2125 src_size
= &ssd
->sink_fmt
;
2127 src_size
= &ssd
->compose
;
2129 if (sel
->pad
== ssd
->sink_pad
) {
2132 _r
.width
= v4l2_subdev_get_try_format(subdev
, cfg
, sel
->pad
)
2134 _r
.height
= v4l2_subdev_get_try_format(subdev
, cfg
, sel
->pad
)
2139 v4l2_subdev_get_try_compose(
2140 subdev
, cfg
, ssd
->sink_pad
);
2144 if (ssd
== sensor
->src
&& sel
->pad
== SMIAPP_PAD_SRC
) {
2149 sel
->r
.width
= min(sel
->r
.width
, src_size
->width
);
2150 sel
->r
.height
= min(sel
->r
.height
, src_size
->height
);
2152 sel
->r
.left
= min_t(int, sel
->r
.left
, src_size
->width
- sel
->r
.width
);
2153 sel
->r
.top
= min_t(int, sel
->r
.top
, src_size
->height
- sel
->r
.height
);
2155 *crops
[sel
->pad
] = sel
->r
;
2157 if (ssd
!= sensor
->pixel_array
&& sel
->pad
== SMIAPP_PAD_SINK
)
2158 smiapp_propagate(subdev
, cfg
, sel
->which
,
2164 static int __smiapp_get_selection(struct v4l2_subdev
*subdev
,
2165 struct v4l2_subdev_pad_config
*cfg
,
2166 struct v4l2_subdev_selection
*sel
)
2168 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2169 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2170 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2171 struct v4l2_rect sink_fmt
;
2174 ret
= __smiapp_sel_supported(subdev
, sel
);
2178 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, sel
->which
);
2180 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2181 sink_fmt
= ssd
->sink_fmt
;
2183 struct v4l2_mbus_framefmt
*fmt
=
2184 v4l2_subdev_get_try_format(subdev
, cfg
, ssd
->sink_pad
);
2188 sink_fmt
.width
= fmt
->width
;
2189 sink_fmt
.height
= fmt
->height
;
2192 switch (sel
->target
) {
2193 case V4L2_SEL_TGT_CROP_BOUNDS
:
2194 case V4L2_SEL_TGT_NATIVE_SIZE
:
2195 if (ssd
== sensor
->pixel_array
) {
2196 sel
->r
.left
= sel
->r
.top
= 0;
2198 sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2200 sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2201 } else if (sel
->pad
== ssd
->sink_pad
) {
2207 case V4L2_SEL_TGT_CROP
:
2208 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2209 sel
->r
= *crops
[sel
->pad
];
2211 case V4L2_SEL_TGT_COMPOSE
:
2219 static int smiapp_get_selection(struct v4l2_subdev
*subdev
,
2220 struct v4l2_subdev_pad_config
*cfg
,
2221 struct v4l2_subdev_selection
*sel
)
2223 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2226 mutex_lock(&sensor
->mutex
);
2227 rval
= __smiapp_get_selection(subdev
, cfg
, sel
);
2228 mutex_unlock(&sensor
->mutex
);
2232 static int smiapp_set_selection(struct v4l2_subdev
*subdev
,
2233 struct v4l2_subdev_pad_config
*cfg
,
2234 struct v4l2_subdev_selection
*sel
)
2236 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2239 ret
= __smiapp_sel_supported(subdev
, sel
);
2243 mutex_lock(&sensor
->mutex
);
2245 sel
->r
.left
= max(0, sel
->r
.left
& ~1);
2246 sel
->r
.top
= max(0, sel
->r
.top
& ~1);
2247 sel
->r
.width
= SMIAPP_ALIGN_DIM(sel
->r
.width
, sel
->flags
);
2248 sel
->r
.height
= SMIAPP_ALIGN_DIM(sel
->r
.height
, sel
->flags
);
2250 sel
->r
.width
= max_t(unsigned int,
2251 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
2253 sel
->r
.height
= max_t(unsigned int,
2254 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
2257 switch (sel
->target
) {
2258 case V4L2_SEL_TGT_CROP
:
2259 ret
= smiapp_set_crop(subdev
, cfg
, sel
);
2261 case V4L2_SEL_TGT_COMPOSE
:
2262 ret
= smiapp_set_compose(subdev
, cfg
, sel
);
2268 mutex_unlock(&sensor
->mutex
);
2272 static int smiapp_get_skip_frames(struct v4l2_subdev
*subdev
, u32
*frames
)
2274 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2276 *frames
= sensor
->frame_skip
;
2280 static int smiapp_get_skip_top_lines(struct v4l2_subdev
*subdev
, u32
*lines
)
2282 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2284 *lines
= sensor
->image_start
;
2289 /* -----------------------------------------------------------------------------
2294 smiapp_sysfs_nvm_read(struct device
*dev
, struct device_attribute
*attr
,
2297 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2298 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2299 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2300 unsigned int nbytes
;
2302 if (!sensor
->dev_init_done
)
2305 if (!sensor
->nvm_size
) {
2306 /* NVM not read yet - read it now */
2307 sensor
->nvm_size
= sensor
->hwcfg
->nvm_size
;
2308 if (smiapp_set_power(subdev
, 1) < 0)
2310 if (smiapp_read_nvm(sensor
, sensor
->nvm
)) {
2311 dev_err(&client
->dev
, "nvm read failed\n");
2314 smiapp_set_power(subdev
, 0);
2317 * NVM is still way below a PAGE_SIZE, so we can safely
2318 * assume this for now.
2320 nbytes
= min_t(unsigned int, sensor
->nvm_size
, PAGE_SIZE
);
2321 memcpy(buf
, sensor
->nvm
, nbytes
);
2325 static DEVICE_ATTR(nvm
, S_IRUGO
, smiapp_sysfs_nvm_read
, NULL
);
2328 smiapp_sysfs_ident_read(struct device
*dev
, struct device_attribute
*attr
,
2331 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2332 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2333 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2335 return snprintf(buf
, PAGE_SIZE
, "%2.2x%4.4x%2.2x\n",
2336 minfo
->manufacturer_id
, minfo
->model_id
,
2337 minfo
->revision_number_major
) + 1;
2340 static DEVICE_ATTR(ident
, S_IRUGO
, smiapp_sysfs_ident_read
, NULL
);
2342 /* -----------------------------------------------------------------------------
2343 * V4L2 subdev core operations
2346 static int smiapp_identify_module(struct smiapp_sensor
*sensor
)
2348 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2349 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2353 minfo
->name
= SMIAPP_NAME
;
2356 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MANUFACTURER_ID
,
2357 &minfo
->manufacturer_id
);
2359 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U16_MODEL_ID
,
2362 rval
= smiapp_read_8only(sensor
,
2363 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR
,
2364 &minfo
->revision_number_major
);
2366 rval
= smiapp_read_8only(sensor
,
2367 SMIAPP_REG_U8_REVISION_NUMBER_MINOR
,
2368 &minfo
->revision_number_minor
);
2370 rval
= smiapp_read_8only(sensor
,
2371 SMIAPP_REG_U8_MODULE_DATE_YEAR
,
2372 &minfo
->module_year
);
2374 rval
= smiapp_read_8only(sensor
,
2375 SMIAPP_REG_U8_MODULE_DATE_MONTH
,
2376 &minfo
->module_month
);
2378 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MODULE_DATE_DAY
,
2379 &minfo
->module_day
);
2383 rval
= smiapp_read_8only(sensor
,
2384 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID
,
2385 &minfo
->sensor_manufacturer_id
);
2387 rval
= smiapp_read_8only(sensor
,
2388 SMIAPP_REG_U16_SENSOR_MODEL_ID
,
2389 &minfo
->sensor_model_id
);
2391 rval
= smiapp_read_8only(sensor
,
2392 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER
,
2393 &minfo
->sensor_revision_number
);
2395 rval
= smiapp_read_8only(sensor
,
2396 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION
,
2397 &minfo
->sensor_firmware_version
);
2401 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIA_VERSION
,
2402 &minfo
->smia_version
);
2404 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIAPP_VERSION
,
2405 &minfo
->smiapp_version
);
2408 dev_err(&client
->dev
, "sensor detection failed\n");
2412 dev_dbg(&client
->dev
, "module 0x%2.2x-0x%4.4x\n",
2413 minfo
->manufacturer_id
, minfo
->model_id
);
2415 dev_dbg(&client
->dev
,
2416 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2417 minfo
->revision_number_major
, minfo
->revision_number_minor
,
2418 minfo
->module_year
, minfo
->module_month
, minfo
->module_day
);
2420 dev_dbg(&client
->dev
, "sensor 0x%2.2x-0x%4.4x\n",
2421 minfo
->sensor_manufacturer_id
, minfo
->sensor_model_id
);
2423 dev_dbg(&client
->dev
,
2424 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2425 minfo
->sensor_revision_number
, minfo
->sensor_firmware_version
);
2427 dev_dbg(&client
->dev
, "smia version %2.2d smiapp version %2.2d\n",
2428 minfo
->smia_version
, minfo
->smiapp_version
);
2431 * Some modules have bad data in the lvalues below. Hope the
2432 * rvalues have better stuff. The lvalues are module
2433 * parameters whereas the rvalues are sensor parameters.
2435 if (!minfo
->manufacturer_id
&& !minfo
->model_id
) {
2436 minfo
->manufacturer_id
= minfo
->sensor_manufacturer_id
;
2437 minfo
->model_id
= minfo
->sensor_model_id
;
2438 minfo
->revision_number_major
= minfo
->sensor_revision_number
;
2441 for (i
= 0; i
< ARRAY_SIZE(smiapp_module_idents
); i
++) {
2442 if (smiapp_module_idents
[i
].manufacturer_id
2443 != minfo
->manufacturer_id
)
2445 if (smiapp_module_idents
[i
].model_id
!= minfo
->model_id
)
2447 if (smiapp_module_idents
[i
].flags
2448 & SMIAPP_MODULE_IDENT_FLAG_REV_LE
) {
2449 if (smiapp_module_idents
[i
].revision_number_major
2450 < minfo
->revision_number_major
)
2453 if (smiapp_module_idents
[i
].revision_number_major
2454 != minfo
->revision_number_major
)
2458 minfo
->name
= smiapp_module_idents
[i
].name
;
2459 minfo
->quirk
= smiapp_module_idents
[i
].quirk
;
2463 if (i
>= ARRAY_SIZE(smiapp_module_idents
))
2464 dev_warn(&client
->dev
,
2465 "no quirks for this module; let's hope it's fully compliant\n");
2467 dev_dbg(&client
->dev
, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2468 minfo
->name
, minfo
->manufacturer_id
, minfo
->model_id
,
2469 minfo
->revision_number_major
);
2474 static const struct v4l2_subdev_ops smiapp_ops
;
2475 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
;
2476 static const struct media_entity_operations smiapp_entity_ops
;
2478 static int smiapp_register_subdevs(struct smiapp_sensor
*sensor
)
2480 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2481 struct smiapp_subdev
*ssds
[] = {
2484 sensor
->pixel_array
,
2489 for (i
= 0; i
< SMIAPP_SUBDEVS
- 1; i
++) {
2490 struct smiapp_subdev
*this = ssds
[i
+ 1];
2491 struct smiapp_subdev
*last
= ssds
[i
];
2496 rval
= media_entity_pads_init(&this->sd
.entity
,
2497 this->npads
, this->pads
);
2499 dev_err(&client
->dev
,
2500 "media_entity_pads_init failed\n");
2504 rval
= v4l2_device_register_subdev(sensor
->src
->sd
.v4l2_dev
,
2507 dev_err(&client
->dev
,
2508 "v4l2_device_register_subdev failed\n");
2512 rval
= media_create_pad_link(&this->sd
.entity
,
2516 MEDIA_LNK_FL_ENABLED
|
2517 MEDIA_LNK_FL_IMMUTABLE
);
2519 dev_err(&client
->dev
,
2520 "media_create_pad_link failed\n");
2528 static void smiapp_cleanup(struct smiapp_sensor
*sensor
)
2530 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2532 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2533 device_remove_file(&client
->dev
, &dev_attr_ident
);
2535 smiapp_free_controls(sensor
);
2538 static int smiapp_init(struct smiapp_sensor
*sensor
)
2540 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2541 struct smiapp_pll
*pll
= &sensor
->pll
;
2542 struct smiapp_subdev
*last
= NULL
;
2546 sensor
->vana
= devm_regulator_get(&client
->dev
, "vana");
2547 if (IS_ERR(sensor
->vana
)) {
2548 dev_err(&client
->dev
, "could not get regulator for vana\n");
2549 return PTR_ERR(sensor
->vana
);
2552 sensor
->ext_clk
= devm_clk_get(&client
->dev
, NULL
);
2553 if (IS_ERR(sensor
->ext_clk
)) {
2554 dev_err(&client
->dev
, "could not get clock (%ld)\n",
2555 PTR_ERR(sensor
->ext_clk
));
2556 return -EPROBE_DEFER
;
2559 rval
= clk_set_rate(sensor
->ext_clk
,
2560 sensor
->hwcfg
->ext_clk
);
2562 dev_err(&client
->dev
,
2563 "unable to set clock freq to %u\n",
2564 sensor
->hwcfg
->ext_clk
);
2568 sensor
->xshutdown
= devm_gpiod_get_optional(&client
->dev
, "xshutdown",
2570 if (IS_ERR(sensor
->xshutdown
))
2571 return PTR_ERR(sensor
->xshutdown
);
2573 rval
= smiapp_power_on(sensor
);
2577 rval
= smiapp_identify_module(sensor
);
2583 rval
= smiapp_get_all_limits(sensor
);
2590 * Handle Sensor Module orientation on the board.
2592 * The application of H-FLIP and V-FLIP on the sensor is modified by
2593 * the sensor orientation on the board.
2595 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2596 * both H-FLIP and V-FLIP for normal operation which also implies
2597 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2598 * controls will need to be internally inverted.
2600 * Rotation also changes the bayer pattern.
2602 if (sensor
->hwcfg
->module_board_orient
==
2603 SMIAPP_MODULE_BOARD_ORIENT_180
)
2604 sensor
->hvflip_inv_mask
= SMIAPP_IMAGE_ORIENTATION_HFLIP
|
2605 SMIAPP_IMAGE_ORIENTATION_VFLIP
;
2607 rval
= smiapp_call_quirk(sensor
, limits
);
2609 dev_err(&client
->dev
, "limits quirks failed\n");
2613 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
]) {
2616 rval
= smiapp_read(sensor
,
2617 SMIAPP_REG_U8_BINNING_SUBTYPES
, &val
);
2622 sensor
->nbinning_subtypes
= min_t(u8
, val
,
2623 SMIAPP_BINNING_SUBTYPES
);
2625 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
2627 sensor
, SMIAPP_REG_U8_BINNING_TYPE_n(i
), &val
);
2632 sensor
->binning_subtypes
[i
] =
2633 *(struct smiapp_binning_subtype
*)&val
;
2635 dev_dbg(&client
->dev
, "binning %xx%x\n",
2636 sensor
->binning_subtypes
[i
].horizontal
,
2637 sensor
->binning_subtypes
[i
].vertical
);
2640 sensor
->binning_horizontal
= 1;
2641 sensor
->binning_vertical
= 1;
2643 if (device_create_file(&client
->dev
, &dev_attr_ident
) != 0) {
2644 dev_err(&client
->dev
, "sysfs ident entry creation failed\n");
2648 /* SMIA++ NVM initialization - it will be read from the sensor
2649 * when it is first requested by userspace.
2651 if (sensor
->minfo
.smiapp_version
&& sensor
->hwcfg
->nvm_size
) {
2652 sensor
->nvm
= devm_kzalloc(&client
->dev
,
2653 sensor
->hwcfg
->nvm_size
, GFP_KERNEL
);
2654 if (sensor
->nvm
== NULL
) {
2655 dev_err(&client
->dev
, "nvm buf allocation failed\n");
2660 if (device_create_file(&client
->dev
, &dev_attr_nvm
) != 0) {
2661 dev_err(&client
->dev
, "sysfs nvm entry failed\n");
2667 /* We consider this as profile 0 sensor if any of these are zero. */
2668 if (!sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV
] ||
2669 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV
] ||
2670 !sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV
] ||
2671 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV
]) {
2672 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_0
;
2673 } else if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2674 != SMIAPP_SCALING_CAPABILITY_NONE
) {
2675 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2676 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2677 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_1
;
2679 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_2
;
2680 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
2681 sensor
->ssds_used
++;
2682 } else if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
2683 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
2684 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
2685 sensor
->ssds_used
++;
2687 sensor
->binner
= &sensor
->ssds
[sensor
->ssds_used
];
2688 sensor
->ssds_used
++;
2689 sensor
->pixel_array
= &sensor
->ssds
[sensor
->ssds_used
];
2690 sensor
->ssds_used
++;
2692 sensor
->scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
2694 /* prepare PLL configuration input values */
2695 pll
->bus_type
= SMIAPP_PLL_BUS_TYPE_CSI2
;
2696 pll
->csi2
.lanes
= sensor
->hwcfg
->lanes
;
2697 pll
->ext_clk_freq_hz
= sensor
->hwcfg
->ext_clk
;
2698 pll
->scale_n
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
2699 /* Profile 0 sensors have no separate OP clock branch. */
2700 if (sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
2701 pll
->flags
|= SMIAPP_PLL_FLAG_NO_OP_CLOCKS
;
2703 for (i
= 0; i
< SMIAPP_SUBDEVS
; i
++) {
2705 struct smiapp_subdev
*ssd
;
2707 } const __this
[] = {
2708 { sensor
->scaler
, "scaler", },
2709 { sensor
->binner
, "binner", },
2710 { sensor
->pixel_array
, "pixel array", },
2711 }, *_this
= &__this
[i
];
2712 struct smiapp_subdev
*this = _this
->ssd
;
2717 if (this != sensor
->src
)
2718 v4l2_subdev_init(&this->sd
, &smiapp_ops
);
2720 this->sensor
= sensor
;
2722 if (this == sensor
->pixel_array
) {
2726 this->source_pad
= 1;
2729 snprintf(this->sd
.name
,
2730 sizeof(this->sd
.name
), "%s %s %d-%4.4x",
2731 sensor
->minfo
.name
, _this
->name
,
2732 i2c_adapter_id(client
->adapter
), client
->addr
);
2734 this->sink_fmt
.width
=
2735 sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2736 this->sink_fmt
.height
=
2737 sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2738 this->compose
.width
= this->sink_fmt
.width
;
2739 this->compose
.height
= this->sink_fmt
.height
;
2740 this->crop
[this->source_pad
] = this->compose
;
2741 this->pads
[this->source_pad
].flags
= MEDIA_PAD_FL_SOURCE
;
2742 if (this != sensor
->pixel_array
) {
2743 this->crop
[this->sink_pad
] = this->compose
;
2744 this->pads
[this->sink_pad
].flags
= MEDIA_PAD_FL_SINK
;
2747 this->sd
.entity
.ops
= &smiapp_entity_ops
;
2754 this->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2755 this->sd
.internal_ops
= &smiapp_internal_ops
;
2756 this->sd
.owner
= THIS_MODULE
;
2757 v4l2_set_subdevdata(&this->sd
, client
);
2762 dev_dbg(&client
->dev
, "profile %d\n", sensor
->minfo
.smiapp_profile
);
2764 sensor
->pixel_array
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
2767 smiapp_read_frame_fmt(sensor
);
2768 rval
= smiapp_init_controls(sensor
);
2772 rval
= smiapp_call_quirk(sensor
, init
);
2776 rval
= smiapp_get_mbus_formats(sensor
);
2782 rval
= smiapp_init_late_controls(sensor
);
2788 mutex_lock(&sensor
->mutex
);
2789 rval
= smiapp_update_mode(sensor
);
2790 mutex_unlock(&sensor
->mutex
);
2792 dev_err(&client
->dev
, "update mode failed\n");
2796 sensor
->streaming
= false;
2797 sensor
->dev_init_done
= true;
2799 smiapp_power_off(sensor
);
2804 smiapp_cleanup(sensor
);
2807 smiapp_power_off(sensor
);
2811 static int smiapp_registered(struct v4l2_subdev
*subdev
)
2813 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2814 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2817 if (!client
->dev
.of_node
) {
2818 rval
= smiapp_init(sensor
);
2823 rval
= smiapp_register_subdevs(sensor
);
2825 smiapp_cleanup(sensor
);
2830 static int smiapp_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2832 struct smiapp_subdev
*ssd
= to_smiapp_subdev(sd
);
2833 struct smiapp_sensor
*sensor
= ssd
->sensor
;
2835 smiapp_csi_data_formats
[smiapp_pixel_order(sensor
)].code
;
2838 mutex_lock(&sensor
->mutex
);
2840 for (i
= 0; i
< ssd
->npads
; i
++) {
2841 struct v4l2_mbus_framefmt
*try_fmt
=
2842 v4l2_subdev_get_try_format(sd
, fh
->pad
, i
);
2843 struct v4l2_rect
*try_crop
= v4l2_subdev_get_try_crop(sd
, fh
->pad
, i
);
2844 struct v4l2_rect
*try_comp
;
2846 try_fmt
->width
= sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2847 try_fmt
->height
= sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2848 try_fmt
->code
= mbus_code
;
2849 try_fmt
->field
= V4L2_FIELD_NONE
;
2853 try_crop
->width
= try_fmt
->width
;
2854 try_crop
->height
= try_fmt
->height
;
2856 if (ssd
!= sensor
->pixel_array
)
2859 try_comp
= v4l2_subdev_get_try_compose(sd
, fh
->pad
, i
);
2860 *try_comp
= *try_crop
;
2863 mutex_unlock(&sensor
->mutex
);
2865 return smiapp_set_power(sd
, 1);
2868 static int smiapp_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2870 return smiapp_set_power(sd
, 0);
2873 static const struct v4l2_subdev_video_ops smiapp_video_ops
= {
2874 .s_stream
= smiapp_set_stream
,
2877 static const struct v4l2_subdev_core_ops smiapp_core_ops
= {
2878 .s_power
= smiapp_set_power
,
2881 static const struct v4l2_subdev_pad_ops smiapp_pad_ops
= {
2882 .enum_mbus_code
= smiapp_enum_mbus_code
,
2883 .get_fmt
= smiapp_get_format
,
2884 .set_fmt
= smiapp_set_format
,
2885 .get_selection
= smiapp_get_selection
,
2886 .set_selection
= smiapp_set_selection
,
2889 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops
= {
2890 .g_skip_frames
= smiapp_get_skip_frames
,
2891 .g_skip_top_lines
= smiapp_get_skip_top_lines
,
2894 static const struct v4l2_subdev_ops smiapp_ops
= {
2895 .core
= &smiapp_core_ops
,
2896 .video
= &smiapp_video_ops
,
2897 .pad
= &smiapp_pad_ops
,
2898 .sensor
= &smiapp_sensor_ops
,
2901 static const struct media_entity_operations smiapp_entity_ops
= {
2902 .link_validate
= v4l2_subdev_link_validate
,
2905 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops
= {
2906 .registered
= smiapp_registered
,
2907 .open
= smiapp_open
,
2908 .close
= smiapp_close
,
2911 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
= {
2912 .open
= smiapp_open
,
2913 .close
= smiapp_close
,
2916 /* -----------------------------------------------------------------------------
2922 static int smiapp_suspend(struct device
*dev
)
2924 struct i2c_client
*client
= to_i2c_client(dev
);
2925 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2926 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2929 BUG_ON(mutex_is_locked(&sensor
->mutex
));
2931 if (sensor
->power_count
== 0)
2934 if (sensor
->streaming
)
2935 smiapp_stop_streaming(sensor
);
2937 streaming
= sensor
->streaming
;
2939 smiapp_power_off(sensor
);
2941 /* save state for resume */
2942 sensor
->streaming
= streaming
;
2947 static int smiapp_resume(struct device
*dev
)
2949 struct i2c_client
*client
= to_i2c_client(dev
);
2950 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2951 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2954 if (sensor
->power_count
== 0)
2957 rval
= smiapp_power_on(sensor
);
2961 if (sensor
->streaming
)
2962 rval
= smiapp_start_streaming(sensor
);
2969 #define smiapp_suspend NULL
2970 #define smiapp_resume NULL
2972 #endif /* CONFIG_PM */
2974 static struct smiapp_hwconfig
*smiapp_get_hwconfig(struct device
*dev
)
2976 struct smiapp_hwconfig
*hwcfg
;
2977 struct v4l2_of_endpoint
*bus_cfg
;
2978 struct device_node
*ep
;
2983 return dev
->platform_data
;
2985 ep
= of_graph_get_next_endpoint(dev
->of_node
, NULL
);
2989 bus_cfg
= v4l2_of_alloc_parse_endpoint(ep
);
2990 if (IS_ERR(bus_cfg
))
2993 hwcfg
= devm_kzalloc(dev
, sizeof(*hwcfg
), GFP_KERNEL
);
2997 switch (bus_cfg
->bus_type
) {
2998 case V4L2_MBUS_CSI2
:
2999 hwcfg
->csi_signalling_mode
= SMIAPP_CSI_SIGNALLING_MODE_CSI2
;
3001 /* FIXME: add CCP2 support. */
3006 hwcfg
->lanes
= bus_cfg
->bus
.mipi_csi2
.num_data_lanes
;
3007 dev_dbg(dev
, "lanes %u\n", hwcfg
->lanes
);
3009 /* NVM size is not mandatory */
3010 of_property_read_u32(dev
->of_node
, "nokia,nvm-size",
3013 rval
= of_property_read_u32(dev
->of_node
, "clock-frequency",
3016 dev_warn(dev
, "can't get clock-frequency\n");
3020 dev_dbg(dev
, "nvm %d, clk %d, csi %d\n", hwcfg
->nvm_size
,
3021 hwcfg
->ext_clk
, hwcfg
->csi_signalling_mode
);
3023 if (!bus_cfg
->nr_of_link_frequencies
) {
3024 dev_warn(dev
, "no link frequencies defined\n");
3028 hwcfg
->op_sys_clock
= devm_kcalloc(
3029 dev
, bus_cfg
->nr_of_link_frequencies
+ 1 /* guardian */,
3030 sizeof(*hwcfg
->op_sys_clock
), GFP_KERNEL
);
3031 if (!hwcfg
->op_sys_clock
)
3034 for (i
= 0; i
< bus_cfg
->nr_of_link_frequencies
; i
++) {
3035 hwcfg
->op_sys_clock
[i
] = bus_cfg
->link_frequencies
[i
];
3036 dev_dbg(dev
, "freq %d: %lld\n", i
, hwcfg
->op_sys_clock
[i
]);
3039 v4l2_of_free_endpoint(bus_cfg
);
3044 v4l2_of_free_endpoint(bus_cfg
);
3049 static int smiapp_probe(struct i2c_client
*client
,
3050 const struct i2c_device_id
*devid
)
3052 struct smiapp_sensor
*sensor
;
3053 struct smiapp_hwconfig
*hwcfg
= smiapp_get_hwconfig(&client
->dev
);
3059 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
3063 sensor
->hwcfg
= hwcfg
;
3064 mutex_init(&sensor
->mutex
);
3065 mutex_init(&sensor
->power_mutex
);
3066 sensor
->src
= &sensor
->ssds
[sensor
->ssds_used
];
3068 v4l2_i2c_subdev_init(&sensor
->src
->sd
, client
, &smiapp_ops
);
3069 sensor
->src
->sd
.internal_ops
= &smiapp_internal_src_ops
;
3070 sensor
->src
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
3071 sensor
->src
->sensor
= sensor
;
3073 sensor
->src
->pads
[0].flags
= MEDIA_PAD_FL_SOURCE
;
3074 rval
= media_entity_pads_init(&sensor
->src
->sd
.entity
, 2,
3079 if (client
->dev
.of_node
) {
3080 rval
= smiapp_init(sensor
);
3082 goto out_media_entity_cleanup
;
3085 rval
= v4l2_async_register_subdev(&sensor
->src
->sd
);
3087 goto out_media_entity_cleanup
;
3091 out_media_entity_cleanup
:
3092 media_entity_cleanup(&sensor
->src
->sd
.entity
);
3097 static int smiapp_remove(struct i2c_client
*client
)
3099 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
3100 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
3103 v4l2_async_unregister_subdev(subdev
);
3105 if (sensor
->power_count
) {
3106 gpiod_set_value(sensor
->xshutdown
, 0);
3107 clk_disable_unprepare(sensor
->ext_clk
);
3108 sensor
->power_count
= 0;
3111 for (i
= 0; i
< sensor
->ssds_used
; i
++) {
3112 v4l2_device_unregister_subdev(&sensor
->ssds
[i
].sd
);
3113 media_entity_cleanup(&sensor
->ssds
[i
].sd
.entity
);
3115 smiapp_cleanup(sensor
);
3120 static const struct of_device_id smiapp_of_table
[] = {
3121 { .compatible
= "nokia,smia" },
3124 MODULE_DEVICE_TABLE(of
, smiapp_of_table
);
3126 static const struct i2c_device_id smiapp_id_table
[] = {
3130 MODULE_DEVICE_TABLE(i2c
, smiapp_id_table
);
3132 static const struct dev_pm_ops smiapp_pm_ops
= {
3133 .suspend
= smiapp_suspend
,
3134 .resume
= smiapp_resume
,
3137 static struct i2c_driver smiapp_i2c_driver
= {
3139 .of_match_table
= smiapp_of_table
,
3140 .name
= SMIAPP_NAME
,
3141 .pm
= &smiapp_pm_ops
,
3143 .probe
= smiapp_probe
,
3144 .remove
= smiapp_remove
,
3145 .id_table
= smiapp_id_table
,
3148 module_i2c_driver(smiapp_i2c_driver
);
3150 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3151 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3152 MODULE_LICENSE("GPL");