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/pm_runtime.h>
30 #include <linux/property.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/slab.h>
33 #include <linux/smiapp.h>
34 #include <linux/v4l2-mediabus.h>
35 #include <media/v4l2-fwnode.h>
36 #include <media/v4l2-device.h>
40 #define SMIAPP_ALIGN_DIM(dim, flags) \
41 ((flags) & V4L2_SEL_FLAG_GE \
46 * smiapp_module_idents - supported camera modules
48 static const struct smiapp_module_ident smiapp_module_idents
[] = {
49 SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"),
50 SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"),
51 SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
52 SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
53 SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
54 SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk
),
55 SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
56 SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
57 SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk
),
58 SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk
),
59 SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk
),
64 * Dynamic Capability Identification
68 static int smiapp_read_frame_fmt(struct smiapp_sensor
*sensor
)
70 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
71 u32 fmt_model_type
, fmt_model_subtype
, ncol_desc
, nrow_desc
;
77 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE
,
82 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE
,
87 ncol_desc
= (fmt_model_subtype
88 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK
)
89 >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT
;
90 nrow_desc
= fmt_model_subtype
91 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK
;
93 dev_dbg(&client
->dev
, "format_model_type %s\n",
94 fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
96 fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
97 ? "4 byte" : "is simply bad");
99 for (i
= 0; i
< ncol_desc
+ nrow_desc
; i
++) {
107 if (fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
) {
108 reg
= SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i
);
109 rval
= smiapp_read(sensor
, reg
, &desc
);
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
) {
120 reg
= SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i
);
121 rval
= smiapp_read(sensor
, reg
, &desc
);
127 & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK
)
128 >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT
;
129 pixels
= desc
& SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK
;
131 dev_dbg(&client
->dev
,
132 "invalid frame format model type %d\n",
143 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED
:
146 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY
:
149 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK
:
152 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK
:
155 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
:
163 dev_dbg(&client
->dev
,
164 "0x%8.8x %s pixels: %d %s (pixelcode %u)\n", reg
,
165 what
, pixels
, which
, pixelcode
);
169 SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
)
170 sensor
->visible_pixel_start
= pixel_count
;
171 pixel_count
+= pixels
;
175 /* Handle row descriptors */
177 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED
:
178 if (sensor
->embedded_end
)
180 sensor
->embedded_start
= line_count
;
181 sensor
->embedded_end
= line_count
+ pixels
;
183 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
:
184 sensor
->image_start
= line_count
;
187 line_count
+= pixels
;
190 if (sensor
->embedded_end
> sensor
->image_start
) {
191 dev_dbg(&client
->dev
,
192 "adjusting image start line to %u (was %u)\n",
193 sensor
->embedded_end
, sensor
->image_start
);
194 sensor
->image_start
= sensor
->embedded_end
;
197 dev_dbg(&client
->dev
, "embedded data from lines %d to %d\n",
198 sensor
->embedded_start
, sensor
->embedded_end
);
199 dev_dbg(&client
->dev
, "image data starts at line %d\n",
200 sensor
->image_start
);
205 static int smiapp_pll_configure(struct smiapp_sensor
*sensor
)
207 struct smiapp_pll
*pll
= &sensor
->pll
;
211 sensor
, SMIAPP_REG_U16_VT_PIX_CLK_DIV
, pll
->vt
.pix_clk_div
);
216 sensor
, SMIAPP_REG_U16_VT_SYS_CLK_DIV
, pll
->vt
.sys_clk_div
);
221 sensor
, SMIAPP_REG_U16_PRE_PLL_CLK_DIV
, pll
->pre_pll_clk_div
);
226 sensor
, SMIAPP_REG_U16_PLL_MULTIPLIER
, pll
->pll_multiplier
);
230 /* Lane op clock ratio does not apply here. */
232 sensor
, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS
,
233 DIV_ROUND_UP(pll
->op
.sys_clk_freq_hz
, 1000000 / 256 / 256));
234 if (rval
< 0 || sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
238 sensor
, SMIAPP_REG_U16_OP_PIX_CLK_DIV
, pll
->op
.pix_clk_div
);
243 sensor
, SMIAPP_REG_U16_OP_SYS_CLK_DIV
, pll
->op
.sys_clk_div
);
246 static int smiapp_pll_try(struct smiapp_sensor
*sensor
,
247 struct smiapp_pll
*pll
)
249 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
250 struct smiapp_pll_limits lim
= {
251 .min_pre_pll_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV
],
252 .max_pre_pll_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV
],
253 .min_pll_ip_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ
],
254 .max_pll_ip_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ
],
255 .min_pll_multiplier
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER
],
256 .max_pll_multiplier
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER
],
257 .min_pll_op_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ
],
258 .max_pll_op_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ
],
260 .op
.min_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV
],
261 .op
.max_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV
],
262 .op
.min_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV
],
263 .op
.max_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV
],
264 .op
.min_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ
],
265 .op
.max_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ
],
266 .op
.min_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ
],
267 .op
.max_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ
],
269 .vt
.min_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV
],
270 .vt
.max_sys_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV
],
271 .vt
.min_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV
],
272 .vt
.max_pix_clk_div
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV
],
273 .vt
.min_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ
],
274 .vt
.max_sys_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ
],
275 .vt
.min_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ
],
276 .vt
.max_pix_clk_freq_hz
= sensor
->limits
[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ
],
278 .min_line_length_pck_bin
= sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
],
279 .min_line_length_pck
= sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK
],
282 return smiapp_pll_calculate(&client
->dev
, &lim
, pll
);
285 static int smiapp_pll_update(struct smiapp_sensor
*sensor
)
287 struct smiapp_pll
*pll
= &sensor
->pll
;
290 pll
->binning_horizontal
= sensor
->binning_horizontal
;
291 pll
->binning_vertical
= sensor
->binning_vertical
;
293 sensor
->link_freq
->qmenu_int
[sensor
->link_freq
->val
];
294 pll
->scale_m
= sensor
->scale_m
;
295 pll
->bits_per_pixel
= sensor
->csi_format
->compressed
;
297 rval
= smiapp_pll_try(sensor
, pll
);
301 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_parray
,
302 pll
->pixel_rate_pixel_array
);
303 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_csi
, pll
->pixel_rate_csi
);
311 * V4L2 Controls handling
315 static void __smiapp_update_exposure_limits(struct smiapp_sensor
*sensor
)
317 struct v4l2_ctrl
*ctrl
= sensor
->exposure
;
320 max
= sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
321 + sensor
->vblank
->val
322 - sensor
->limits
[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN
];
324 __v4l2_ctrl_modify_range(ctrl
, ctrl
->minimum
, max
, ctrl
->step
, max
);
330 * 1. Bits-per-pixel, descending.
331 * 2. Bits-per-pixel compressed, descending.
332 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
333 * orders must be defined.
335 static const struct smiapp_csi_data_format smiapp_csi_data_formats
[] = {
336 { MEDIA_BUS_FMT_SGRBG16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_GRBG
, },
337 { MEDIA_BUS_FMT_SRGGB16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_RGGB
, },
338 { MEDIA_BUS_FMT_SBGGR16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_BGGR
, },
339 { MEDIA_BUS_FMT_SGBRG16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_GBRG
, },
340 { MEDIA_BUS_FMT_SGRBG14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_GRBG
, },
341 { MEDIA_BUS_FMT_SRGGB14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_RGGB
, },
342 { MEDIA_BUS_FMT_SBGGR14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_BGGR
, },
343 { MEDIA_BUS_FMT_SGBRG14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_GBRG
, },
344 { MEDIA_BUS_FMT_SGRBG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GRBG
, },
345 { MEDIA_BUS_FMT_SRGGB12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_RGGB
, },
346 { MEDIA_BUS_FMT_SBGGR12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_BGGR
, },
347 { MEDIA_BUS_FMT_SGBRG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GBRG
, },
348 { MEDIA_BUS_FMT_SGRBG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GRBG
, },
349 { MEDIA_BUS_FMT_SRGGB10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_RGGB
, },
350 { MEDIA_BUS_FMT_SBGGR10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_BGGR
, },
351 { MEDIA_BUS_FMT_SGBRG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GBRG
, },
352 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
353 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
354 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
355 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
356 { MEDIA_BUS_FMT_SGRBG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
357 { MEDIA_BUS_FMT_SRGGB8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
358 { MEDIA_BUS_FMT_SBGGR8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
359 { MEDIA_BUS_FMT_SGBRG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
362 static const char *pixel_order_str
[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
364 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
365 - (unsigned long)smiapp_csi_data_formats) \
366 / sizeof(*smiapp_csi_data_formats))
368 static u32
smiapp_pixel_order(struct smiapp_sensor
*sensor
)
370 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
374 if (sensor
->hflip
->val
)
375 flip
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
377 if (sensor
->vflip
->val
)
378 flip
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
381 flip
^= sensor
->hvflip_inv_mask
;
383 dev_dbg(&client
->dev
, "flip %d\n", flip
);
384 return sensor
->default_pixel_order
^ flip
;
387 static void smiapp_update_mbus_formats(struct smiapp_sensor
*sensor
)
389 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
390 unsigned int csi_format_idx
=
391 to_csi_format_idx(sensor
->csi_format
) & ~3;
392 unsigned int internal_csi_format_idx
=
393 to_csi_format_idx(sensor
->internal_csi_format
) & ~3;
394 unsigned int pixel_order
= smiapp_pixel_order(sensor
);
396 sensor
->mbus_frame_fmts
=
397 sensor
->default_mbus_frame_fmts
<< pixel_order
;
399 &smiapp_csi_data_formats
[csi_format_idx
+ pixel_order
];
400 sensor
->internal_csi_format
=
401 &smiapp_csi_data_formats
[internal_csi_format_idx
404 BUG_ON(max(internal_csi_format_idx
, csi_format_idx
) + pixel_order
405 >= ARRAY_SIZE(smiapp_csi_data_formats
));
407 dev_dbg(&client
->dev
, "new pixel order %s\n",
408 pixel_order_str
[pixel_order
]);
411 static const char * const smiapp_test_patterns
[] = {
414 "Eight Vertical Colour Bars",
415 "Colour Bars With Fade to Grey",
416 "Pseudorandom Sequence (PN9)",
419 static int smiapp_set_ctrl(struct v4l2_ctrl
*ctrl
)
421 struct smiapp_sensor
*sensor
=
422 container_of(ctrl
->handler
, struct smiapp_subdev
, ctrl_handler
)
429 case V4L2_CID_ANALOGUE_GAIN
:
432 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL
, ctrl
->val
);
434 case V4L2_CID_EXPOSURE
:
437 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME
, ctrl
->val
);
441 if (sensor
->streaming
)
444 if (sensor
->hflip
->val
)
445 orient
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
447 if (sensor
->vflip
->val
)
448 orient
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
450 orient
^= sensor
->hvflip_inv_mask
;
451 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_IMAGE_ORIENTATION
,
456 smiapp_update_mbus_formats(sensor
);
460 case V4L2_CID_VBLANK
:
461 exposure
= sensor
->exposure
->val
;
463 __smiapp_update_exposure_limits(sensor
);
465 if (exposure
> sensor
->exposure
->maximum
) {
466 sensor
->exposure
->val
= sensor
->exposure
->maximum
;
467 rval
= smiapp_set_ctrl(sensor
->exposure
);
473 sensor
, SMIAPP_REG_U16_FRAME_LENGTH_LINES
,
474 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
477 case V4L2_CID_HBLANK
:
479 sensor
, SMIAPP_REG_U16_LINE_LENGTH_PCK
,
480 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
483 case V4L2_CID_LINK_FREQ
:
484 if (sensor
->streaming
)
487 return smiapp_pll_update(sensor
);
489 case V4L2_CID_TEST_PATTERN
: {
492 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
494 sensor
->test_data
[i
],
496 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR
);
499 sensor
, SMIAPP_REG_U16_TEST_PATTERN_MODE
, ctrl
->val
);
502 case V4L2_CID_TEST_PATTERN_RED
:
504 sensor
, SMIAPP_REG_U16_TEST_DATA_RED
, ctrl
->val
);
506 case V4L2_CID_TEST_PATTERN_GREENR
:
508 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENR
, ctrl
->val
);
510 case V4L2_CID_TEST_PATTERN_BLUE
:
512 sensor
, SMIAPP_REG_U16_TEST_DATA_BLUE
, ctrl
->val
);
514 case V4L2_CID_TEST_PATTERN_GREENB
:
516 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENB
, ctrl
->val
);
518 case V4L2_CID_PIXEL_RATE
:
519 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
527 static const struct v4l2_ctrl_ops smiapp_ctrl_ops
= {
528 .s_ctrl
= smiapp_set_ctrl
,
531 static int smiapp_init_controls(struct smiapp_sensor
*sensor
)
533 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
536 rval
= v4l2_ctrl_handler_init(&sensor
->pixel_array
->ctrl_handler
, 12);
540 sensor
->pixel_array
->ctrl_handler
.lock
= &sensor
->mutex
;
542 sensor
->analog_gain
= v4l2_ctrl_new_std(
543 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
544 V4L2_CID_ANALOGUE_GAIN
,
545 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN
],
546 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX
],
547 max(sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP
], 1U),
548 sensor
->limits
[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN
]);
550 /* Exposure limits will be updated soon, use just something here. */
551 sensor
->exposure
= v4l2_ctrl_new_std(
552 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
553 V4L2_CID_EXPOSURE
, 0, 0, 1, 0);
555 sensor
->hflip
= v4l2_ctrl_new_std(
556 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
557 V4L2_CID_HFLIP
, 0, 1, 1, 0);
558 sensor
->vflip
= v4l2_ctrl_new_std(
559 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
560 V4L2_CID_VFLIP
, 0, 1, 1, 0);
562 sensor
->vblank
= v4l2_ctrl_new_std(
563 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
564 V4L2_CID_VBLANK
, 0, 1, 1, 0);
567 sensor
->vblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
569 sensor
->hblank
= v4l2_ctrl_new_std(
570 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
571 V4L2_CID_HBLANK
, 0, 1, 1, 0);
574 sensor
->hblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
576 sensor
->pixel_rate_parray
= v4l2_ctrl_new_std(
577 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
578 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
580 v4l2_ctrl_new_std_menu_items(&sensor
->pixel_array
->ctrl_handler
,
581 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
582 ARRAY_SIZE(smiapp_test_patterns
) - 1,
583 0, 0, smiapp_test_patterns
);
585 if (sensor
->pixel_array
->ctrl_handler
.error
) {
586 dev_err(&client
->dev
,
587 "pixel array controls initialization failed (%d)\n",
588 sensor
->pixel_array
->ctrl_handler
.error
);
589 return sensor
->pixel_array
->ctrl_handler
.error
;
592 sensor
->pixel_array
->sd
.ctrl_handler
=
593 &sensor
->pixel_array
->ctrl_handler
;
595 v4l2_ctrl_cluster(2, &sensor
->hflip
);
597 rval
= v4l2_ctrl_handler_init(&sensor
->src
->ctrl_handler
, 0);
601 sensor
->src
->ctrl_handler
.lock
= &sensor
->mutex
;
603 sensor
->pixel_rate_csi
= v4l2_ctrl_new_std(
604 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
605 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
607 if (sensor
->src
->ctrl_handler
.error
) {
608 dev_err(&client
->dev
,
609 "src controls initialization failed (%d)\n",
610 sensor
->src
->ctrl_handler
.error
);
611 return sensor
->src
->ctrl_handler
.error
;
614 sensor
->src
->sd
.ctrl_handler
= &sensor
->src
->ctrl_handler
;
620 * For controls that require information on available media bus codes
621 * and linke frequencies.
623 static int smiapp_init_late_controls(struct smiapp_sensor
*sensor
)
625 unsigned long *valid_link_freqs
= &sensor
->valid_link_freqs
[
626 sensor
->csi_format
->compressed
- sensor
->compressed_min_bpp
];
629 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++) {
630 int max_value
= (1 << sensor
->csi_format
->width
) - 1;
632 sensor
->test_data
[i
] = v4l2_ctrl_new_std(
633 &sensor
->pixel_array
->ctrl_handler
,
634 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN_RED
+ i
,
635 0, max_value
, 1, max_value
);
638 for (max
= 0; sensor
->hwcfg
->op_sys_clock
[max
+ 1]; max
++);
640 sensor
->link_freq
= v4l2_ctrl_new_int_menu(
641 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
642 V4L2_CID_LINK_FREQ
, __fls(*valid_link_freqs
),
643 __ffs(*valid_link_freqs
), sensor
->hwcfg
->op_sys_clock
);
645 return sensor
->src
->ctrl_handler
.error
;
648 static void smiapp_free_controls(struct smiapp_sensor
*sensor
)
652 for (i
= 0; i
< sensor
->ssds_used
; i
++)
653 v4l2_ctrl_handler_free(&sensor
->ssds
[i
].ctrl_handler
);
656 static int smiapp_get_limits(struct smiapp_sensor
*sensor
, int const *limit
,
659 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
664 for (i
= 0; i
< n
; i
++) {
666 sensor
, smiapp_reg_limits
[limit
[i
]].addr
, &val
);
669 sensor
->limits
[limit
[i
]] = val
;
670 dev_dbg(&client
->dev
, "0x%8.8x \"%s\" = %u, 0x%x\n",
671 smiapp_reg_limits
[limit
[i
]].addr
,
672 smiapp_reg_limits
[limit
[i
]].what
, val
, val
);
678 static int smiapp_get_all_limits(struct smiapp_sensor
*sensor
)
683 for (i
= 0; i
< SMIAPP_LIMIT_LAST
; i
++) {
684 rval
= smiapp_get_limits(sensor
, &i
, 1);
689 if (sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] == 0)
690 smiapp_replace_limit(sensor
, SMIAPP_LIMIT_SCALER_N_MIN
, 16);
695 static int smiapp_get_limits_binning(struct smiapp_sensor
*sensor
)
697 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
698 static u32
const limits
[] = {
699 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
,
700 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN
,
701 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
,
702 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN
,
703 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
,
704 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN
,
705 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN
,
707 static u32
const limits_replace
[] = {
708 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES
,
709 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES
,
710 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK
,
711 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK
,
712 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK
,
713 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN
,
714 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN
,
719 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
] ==
720 SMIAPP_BINNING_CAPABILITY_NO
) {
721 for (i
= 0; i
< ARRAY_SIZE(limits
); i
++)
722 sensor
->limits
[limits
[i
]] =
723 sensor
->limits
[limits_replace
[i
]];
728 rval
= smiapp_get_limits(sensor
, limits
, ARRAY_SIZE(limits
));
733 * Sanity check whether the binning limits are valid. If not,
734 * use the non-binning ones.
736 if (sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
]
737 && sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
]
738 && sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
])
741 for (i
= 0; i
< ARRAY_SIZE(limits
); i
++) {
742 dev_dbg(&client
->dev
,
743 "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
744 smiapp_reg_limits
[limits
[i
]].addr
,
745 smiapp_reg_limits
[limits
[i
]].what
,
746 sensor
->limits
[limits_replace
[i
]],
747 sensor
->limits
[limits_replace
[i
]]);
748 sensor
->limits
[limits
[i
]] =
749 sensor
->limits
[limits_replace
[i
]];
755 static int smiapp_get_mbus_formats(struct smiapp_sensor
*sensor
)
757 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
758 struct smiapp_pll
*pll
= &sensor
->pll
;
759 u8 compressed_max_bpp
= 0;
760 unsigned int type
, n
;
761 unsigned int i
, pixel_order
;
765 sensor
, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE
, &type
);
769 dev_dbg(&client
->dev
, "data_format_model_type %d\n", type
);
771 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_PIXEL_ORDER
,
776 if (pixel_order
>= ARRAY_SIZE(pixel_order_str
)) {
777 dev_dbg(&client
->dev
, "bad pixel order %d\n", pixel_order
);
781 dev_dbg(&client
->dev
, "pixel order %d (%s)\n", pixel_order
,
782 pixel_order_str
[pixel_order
]);
785 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL
:
786 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N
;
788 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED
:
789 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N
;
795 sensor
->default_pixel_order
= pixel_order
;
796 sensor
->mbus_frame_fmts
= 0;
798 for (i
= 0; i
< n
; i
++) {
803 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i
), &fmt
);
807 dev_dbg(&client
->dev
, "%u: bpp %u, compressed %u\n",
808 i
, fmt
>> 8, (u8
)fmt
);
810 for (j
= 0; j
< ARRAY_SIZE(smiapp_csi_data_formats
); j
++) {
811 const struct smiapp_csi_data_format
*f
=
812 &smiapp_csi_data_formats
[j
];
814 if (f
->pixel_order
!= SMIAPP_PIXEL_ORDER_GRBG
)
817 if (f
->width
!= fmt
>> 8 || f
->compressed
!= (u8
)fmt
)
820 dev_dbg(&client
->dev
, "jolly good! %d\n", j
);
822 sensor
->default_mbus_frame_fmts
|= 1 << j
;
826 /* Figure out which BPP values can be used with which formats. */
827 pll
->binning_horizontal
= 1;
828 pll
->binning_vertical
= 1;
829 pll
->scale_m
= sensor
->scale_m
;
831 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
832 sensor
->compressed_min_bpp
=
833 min(smiapp_csi_data_formats
[i
].compressed
,
834 sensor
->compressed_min_bpp
);
836 max(smiapp_csi_data_formats
[i
].compressed
,
840 sensor
->valid_link_freqs
= devm_kcalloc(
842 compressed_max_bpp
- sensor
->compressed_min_bpp
+ 1,
843 sizeof(*sensor
->valid_link_freqs
), GFP_KERNEL
);
844 if (!sensor
->valid_link_freqs
)
847 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
848 const struct smiapp_csi_data_format
*f
=
849 &smiapp_csi_data_formats
[i
];
850 unsigned long *valid_link_freqs
=
851 &sensor
->valid_link_freqs
[
852 f
->compressed
- sensor
->compressed_min_bpp
];
855 if (!(sensor
->default_mbus_frame_fmts
& 1 << i
))
858 pll
->bits_per_pixel
= f
->compressed
;
860 for (j
= 0; sensor
->hwcfg
->op_sys_clock
[j
]; j
++) {
861 pll
->link_freq
= sensor
->hwcfg
->op_sys_clock
[j
];
863 rval
= smiapp_pll_try(sensor
, pll
);
864 dev_dbg(&client
->dev
, "link freq %u Hz, bpp %u %s\n",
865 pll
->link_freq
, pll
->bits_per_pixel
,
866 rval
? "not ok" : "ok");
870 set_bit(j
, valid_link_freqs
);
873 if (!*valid_link_freqs
) {
874 dev_info(&client
->dev
,
875 "no valid link frequencies for %u bpp\n",
877 sensor
->default_mbus_frame_fmts
&= ~BIT(i
);
881 if (!sensor
->csi_format
882 || f
->width
> sensor
->csi_format
->width
883 || (f
->width
== sensor
->csi_format
->width
884 && f
->compressed
> sensor
->csi_format
->compressed
)) {
885 sensor
->csi_format
= f
;
886 sensor
->internal_csi_format
= f
;
890 if (!sensor
->csi_format
) {
891 dev_err(&client
->dev
, "no supported mbus code found\n");
895 smiapp_update_mbus_formats(sensor
);
900 static void smiapp_update_blanking(struct smiapp_sensor
*sensor
)
902 struct v4l2_ctrl
*vblank
= sensor
->vblank
;
903 struct v4l2_ctrl
*hblank
= sensor
->hblank
;
907 sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES
],
908 sensor
->limits
[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN
] -
909 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
);
910 max
= sensor
->limits
[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN
] -
911 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
;
913 __v4l2_ctrl_modify_range(vblank
, min
, max
, vblank
->step
, min
);
916 sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN
] -
917 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
,
918 sensor
->limits
[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN
]);
919 max
= sensor
->limits
[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN
] -
920 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
;
922 __v4l2_ctrl_modify_range(hblank
, min
, max
, hblank
->step
, min
);
924 __smiapp_update_exposure_limits(sensor
);
927 static int smiapp_update_mode(struct smiapp_sensor
*sensor
)
929 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
930 unsigned int binning_mode
;
933 /* Binning has to be set up here; it affects limits */
934 if (sensor
->binning_horizontal
== 1 &&
935 sensor
->binning_vertical
== 1) {
939 (sensor
->binning_horizontal
<< 4)
940 | sensor
->binning_vertical
;
943 sensor
, SMIAPP_REG_U8_BINNING_TYPE
, binning_type
);
949 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_BINNING_MODE
, binning_mode
);
953 /* Get updated limits due to binning */
954 rval
= smiapp_get_limits_binning(sensor
);
958 rval
= smiapp_pll_update(sensor
);
962 /* Output from pixel array, including blanking */
963 smiapp_update_blanking(sensor
);
965 dev_dbg(&client
->dev
, "vblank\t\t%d\n", sensor
->vblank
->val
);
966 dev_dbg(&client
->dev
, "hblank\t\t%d\n", sensor
->hblank
->val
);
968 dev_dbg(&client
->dev
, "real timeperframe\t100/%d\n",
969 sensor
->pll
.pixel_rate_pixel_array
/
970 ((sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
971 + sensor
->hblank
->val
) *
972 (sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
973 + sensor
->vblank
->val
) / 100));
980 * SMIA++ NVM handling
983 static int smiapp_read_nvm(struct smiapp_sensor
*sensor
,
989 np
= sensor
->nvm_size
/ SMIAPP_NVM_PAGE_SIZE
;
990 for (p
= 0; p
< np
; p
++) {
993 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT
, p
);
997 rval
= smiapp_write(sensor
,
998 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
,
999 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN
|
1000 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN
);
1004 for (i
= 0; i
< 1000; i
++) {
1007 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS
, &s
);
1012 if (s
& SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY
)
1022 for (i
= 0; i
< SMIAPP_NVM_PAGE_SIZE
; i
++) {
1025 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0
+ i
,
1035 rval2
= smiapp_write(sensor
, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
, 0);
1044 * SMIA++ CCI address control
1047 static int smiapp_change_cci_addr(struct smiapp_sensor
*sensor
)
1049 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1053 client
->addr
= sensor
->hwcfg
->i2c_addr_dfl
;
1055 rval
= smiapp_write(sensor
,
1056 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
,
1057 sensor
->hwcfg
->i2c_addr_alt
<< 1);
1061 client
->addr
= sensor
->hwcfg
->i2c_addr_alt
;
1063 /* verify addr change went ok */
1064 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
, &val
);
1068 if (val
!= sensor
->hwcfg
->i2c_addr_alt
<< 1)
1076 * SMIA++ Mode Control
1079 static int smiapp_setup_flash_strobe(struct smiapp_sensor
*sensor
)
1081 struct smiapp_flash_strobe_parms
*strobe_setup
;
1082 unsigned int ext_freq
= sensor
->hwcfg
->ext_clk
;
1084 u32 strobe_adjustment
;
1085 u32 strobe_width_high_rs
;
1088 strobe_setup
= sensor
->hwcfg
->strobe_setup
;
1091 * How to calculate registers related to strobe length. Please
1092 * do not change, or if you do at least know what you're
1095 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1097 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1098 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1100 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1101 * flash_strobe_adjustment E N, [1 - 0xff]
1103 * The formula above is written as below to keep it on one
1106 * l / 10^6 = w / e * a
1108 * Let's mark w * a by x:
1116 * The strobe width must be at least as long as requested,
1117 * thus rounding upwards is needed.
1119 * x = (l * e + 10^6 - 1) / 10^6
1120 * -----------------------------
1122 * Maximum possible accuracy is wanted at all times. Thus keep
1123 * a as small as possible.
1125 * Calculate a, assuming maximum w, with rounding upwards:
1127 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1128 * -------------------------------------
1130 * Thus, we also get w, with that a, with rounding upwards:
1132 * w = (x + a - 1) / a
1133 * -------------------
1137 * x E [1, (2^16 - 1) * (2^8 - 1)]
1139 * Substituting maximum x to the original formula (with rounding),
1140 * the maximum l is thus
1142 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1144 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1145 * --------------------------------------------------
1147 * flash_strobe_length must be clamped between 1 and
1148 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1152 * flash_strobe_adjustment = ((flash_strobe_length *
1153 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1155 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1156 * EXTCLK freq + 10^6 - 1) / 10^6 +
1157 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1159 tmp
= div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1160 1000000 + 1, ext_freq
);
1161 strobe_setup
->strobe_width_high_us
=
1162 clamp_t(u32
, strobe_setup
->strobe_width_high_us
, 1, tmp
);
1164 tmp
= div_u64(((u64
)strobe_setup
->strobe_width_high_us
* (u64
)ext_freq
+
1165 1000000 - 1), 1000000ULL);
1166 strobe_adjustment
= (tmp
+ (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1167 strobe_width_high_rs
= (tmp
+ strobe_adjustment
- 1) /
1170 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_MODE_RS
,
1171 strobe_setup
->mode
);
1175 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT
,
1180 rval
= smiapp_write(
1181 sensor
, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL
,
1182 strobe_width_high_rs
);
1186 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL
,
1187 strobe_setup
->strobe_delay
);
1191 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_FLASH_STROBE_START_POINT
,
1192 strobe_setup
->stobe_start_point
);
1196 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_TRIGGER_RS
,
1197 strobe_setup
->trigger
);
1200 sensor
->hwcfg
->strobe_setup
->trigger
= 0;
1205 /* -----------------------------------------------------------------------------
1209 static int smiapp_power_on(struct device
*dev
)
1211 struct i2c_client
*client
= to_i2c_client(dev
);
1212 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
1213 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1215 * The sub-device related to the I2C device is always the
1216 * source one, i.e. ssds[0].
1218 struct smiapp_sensor
*sensor
=
1219 container_of(ssd
, struct smiapp_sensor
, ssds
[0]);
1223 rval
= regulator_enable(sensor
->vana
);
1225 dev_err(&client
->dev
, "failed to enable vana regulator\n");
1228 usleep_range(1000, 1000);
1230 rval
= clk_prepare_enable(sensor
->ext_clk
);
1232 dev_dbg(&client
->dev
, "failed to enable xclk\n");
1235 usleep_range(1000, 1000);
1237 gpiod_set_value(sensor
->xshutdown
, 1);
1239 sleep
= SMIAPP_RESET_DELAY(sensor
->hwcfg
->ext_clk
);
1240 usleep_range(sleep
, sleep
);
1242 mutex_lock(&sensor
->mutex
);
1244 sensor
->active
= true;
1247 * Failures to respond to the address change command have been noticed.
1248 * Those failures seem to be caused by the sensor requiring a longer
1249 * boot time than advertised. An additional 10ms delay seems to work
1250 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1251 * unnecessary. The failures need to be investigated to find a proper
1252 * fix, and a delay will likely need to be added here if the I2C write
1253 * retry hack is reverted before the root cause of the boot time issue
1257 if (sensor
->hwcfg
->i2c_addr_alt
) {
1258 rval
= smiapp_change_cci_addr(sensor
);
1260 dev_err(&client
->dev
, "cci address change error\n");
1261 goto out_cci_addr_fail
;
1265 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_SOFTWARE_RESET
,
1266 SMIAPP_SOFTWARE_RESET
);
1268 dev_err(&client
->dev
, "software reset failed\n");
1269 goto out_cci_addr_fail
;
1272 if (sensor
->hwcfg
->i2c_addr_alt
) {
1273 rval
= smiapp_change_cci_addr(sensor
);
1275 dev_err(&client
->dev
, "cci address change error\n");
1276 goto out_cci_addr_fail
;
1280 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_COMPRESSION_MODE
,
1281 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR
);
1283 dev_err(&client
->dev
, "compression mode set failed\n");
1284 goto out_cci_addr_fail
;
1287 rval
= smiapp_write(
1288 sensor
, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ
,
1289 sensor
->hwcfg
->ext_clk
/ (1000000 / (1 << 8)));
1291 dev_err(&client
->dev
, "extclk frequency set failed\n");
1292 goto out_cci_addr_fail
;
1295 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_LANE_MODE
,
1296 sensor
->hwcfg
->lanes
- 1);
1298 dev_err(&client
->dev
, "csi lane mode set failed\n");
1299 goto out_cci_addr_fail
;
1302 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FAST_STANDBY_CTRL
,
1303 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE
);
1305 dev_err(&client
->dev
, "fast standby set failed\n");
1306 goto out_cci_addr_fail
;
1309 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_SIGNALLING_MODE
,
1310 sensor
->hwcfg
->csi_signalling_mode
);
1312 dev_err(&client
->dev
, "csi signalling mode set failed\n");
1313 goto out_cci_addr_fail
;
1316 /* DPHY control done by sensor based on requested link rate */
1317 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_DPHY_CTRL
,
1318 SMIAPP_DPHY_CTRL_UI
);
1320 goto out_cci_addr_fail
;
1322 rval
= smiapp_call_quirk(sensor
, post_poweron
);
1324 dev_err(&client
->dev
, "post_poweron quirks failed\n");
1325 goto out_cci_addr_fail
;
1328 /* Are we still initialising...? If not, proceed with control setup. */
1329 if (sensor
->pixel_array
) {
1330 rval
= __v4l2_ctrl_handler_setup(
1331 &sensor
->pixel_array
->ctrl_handler
);
1333 goto out_cci_addr_fail
;
1335 rval
= __v4l2_ctrl_handler_setup(&sensor
->src
->ctrl_handler
);
1337 goto out_cci_addr_fail
;
1339 rval
= smiapp_update_mode(sensor
);
1341 goto out_cci_addr_fail
;
1344 mutex_unlock(&sensor
->mutex
);
1349 mutex_unlock(&sensor
->mutex
);
1350 gpiod_set_value(sensor
->xshutdown
, 0);
1351 clk_disable_unprepare(sensor
->ext_clk
);
1354 regulator_disable(sensor
->vana
);
1359 static int smiapp_power_off(struct device
*dev
)
1361 struct i2c_client
*client
= to_i2c_client(dev
);
1362 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
1363 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1364 struct smiapp_sensor
*sensor
=
1365 container_of(ssd
, struct smiapp_sensor
, ssds
[0]);
1367 mutex_lock(&sensor
->mutex
);
1370 * Currently power/clock to lens are enable/disabled separately
1371 * but they are essentially the same signals. So if the sensor is
1372 * powered off while the lens is powered on the sensor does not
1373 * really see a power off and next time the cci address change
1374 * will fail. So do a soft reset explicitly here.
1376 if (sensor
->hwcfg
->i2c_addr_alt
)
1377 smiapp_write(sensor
,
1378 SMIAPP_REG_U8_SOFTWARE_RESET
,
1379 SMIAPP_SOFTWARE_RESET
);
1381 sensor
->active
= false;
1383 mutex_unlock(&sensor
->mutex
);
1385 gpiod_set_value(sensor
->xshutdown
, 0);
1386 clk_disable_unprepare(sensor
->ext_clk
);
1387 usleep_range(5000, 5000);
1388 regulator_disable(sensor
->vana
);
1389 sensor
->streaming
= false;
1394 /* -----------------------------------------------------------------------------
1395 * Video stream management
1398 static int smiapp_start_streaming(struct smiapp_sensor
*sensor
)
1400 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1403 mutex_lock(&sensor
->mutex
);
1405 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_CSI_DATA_FORMAT
,
1406 (sensor
->csi_format
->width
<< 8) |
1407 sensor
->csi_format
->compressed
);
1411 rval
= smiapp_pll_configure(sensor
);
1415 /* Analog crop start coordinates */
1416 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_ADDR_START
,
1417 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
);
1421 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_ADDR_START
,
1422 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
);
1426 /* Analog crop end coordinates */
1427 rval
= smiapp_write(
1428 sensor
, SMIAPP_REG_U16_X_ADDR_END
,
1429 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
1430 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
- 1);
1434 rval
= smiapp_write(
1435 sensor
, SMIAPP_REG_U16_Y_ADDR_END
,
1436 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
1437 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
- 1);
1442 * Output from pixel array, including blanking, is set using
1443 * controls below. No need to set here.
1447 if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
1448 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
1449 rval
= smiapp_write(
1450 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET
,
1451 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].left
);
1455 rval
= smiapp_write(
1456 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET
,
1457 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].top
);
1461 rval
= smiapp_write(
1462 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH
,
1463 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].width
);
1467 rval
= smiapp_write(
1468 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT
,
1469 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].height
);
1475 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
1476 != SMIAPP_SCALING_CAPABILITY_NONE
) {
1477 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALING_MODE
,
1478 sensor
->scaling_mode
);
1482 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALE_M
,
1488 /* Output size from sensor */
1489 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_OUTPUT_SIZE
,
1490 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
);
1493 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_OUTPUT_SIZE
,
1494 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
1498 if ((sensor
->limits
[SMIAPP_LIMIT_FLASH_MODE_CAPABILITY
] &
1499 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE
|
1500 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE
)) &&
1501 sensor
->hwcfg
->strobe_setup
!= NULL
&&
1502 sensor
->hwcfg
->strobe_setup
->trigger
!= 0) {
1503 rval
= smiapp_setup_flash_strobe(sensor
);
1508 rval
= smiapp_call_quirk(sensor
, pre_streamon
);
1510 dev_err(&client
->dev
, "pre_streamon quirks failed\n");
1514 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1515 SMIAPP_MODE_SELECT_STREAMING
);
1518 mutex_unlock(&sensor
->mutex
);
1523 static int smiapp_stop_streaming(struct smiapp_sensor
*sensor
)
1525 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1528 mutex_lock(&sensor
->mutex
);
1529 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1530 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY
);
1534 rval
= smiapp_call_quirk(sensor
, post_streamoff
);
1536 dev_err(&client
->dev
, "post_streamoff quirks failed\n");
1539 mutex_unlock(&sensor
->mutex
);
1543 /* -----------------------------------------------------------------------------
1544 * V4L2 subdev video operations
1547 static int smiapp_set_stream(struct v4l2_subdev
*subdev
, int enable
)
1549 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1550 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1553 if (sensor
->streaming
== enable
)
1557 rval
= pm_runtime_get_sync(&client
->dev
);
1559 if (rval
!= -EBUSY
&& rval
!= -EAGAIN
)
1560 pm_runtime_set_active(&client
->dev
);
1561 pm_runtime_put(&client
->dev
);
1565 sensor
->streaming
= true;
1567 rval
= smiapp_start_streaming(sensor
);
1569 sensor
->streaming
= false;
1571 rval
= smiapp_stop_streaming(sensor
);
1572 sensor
->streaming
= false;
1573 pm_runtime_mark_last_busy(&client
->dev
);
1574 pm_runtime_put_autosuspend(&client
->dev
);
1580 static int smiapp_enum_mbus_code(struct v4l2_subdev
*subdev
,
1581 struct v4l2_subdev_pad_config
*cfg
,
1582 struct v4l2_subdev_mbus_code_enum
*code
)
1584 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1585 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1590 mutex_lock(&sensor
->mutex
);
1592 dev_err(&client
->dev
, "subdev %s, pad %d, index %d\n",
1593 subdev
->name
, code
->pad
, code
->index
);
1595 if (subdev
!= &sensor
->src
->sd
|| code
->pad
!= SMIAPP_PAD_SRC
) {
1599 code
->code
= sensor
->internal_csi_format
->code
;
1604 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1605 if (sensor
->mbus_frame_fmts
& (1 << i
))
1608 if (idx
== code
->index
) {
1609 code
->code
= smiapp_csi_data_formats
[i
].code
;
1610 dev_err(&client
->dev
, "found index %d, i %d, code %x\n",
1611 code
->index
, i
, code
->code
);
1618 mutex_unlock(&sensor
->mutex
);
1623 static u32
__smiapp_get_mbus_code(struct v4l2_subdev
*subdev
,
1626 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1628 if (subdev
== &sensor
->src
->sd
&& pad
== SMIAPP_PAD_SRC
)
1629 return sensor
->csi_format
->code
;
1631 return sensor
->internal_csi_format
->code
;
1634 static int __smiapp_get_format(struct v4l2_subdev
*subdev
,
1635 struct v4l2_subdev_pad_config
*cfg
,
1636 struct v4l2_subdev_format
*fmt
)
1638 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1640 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1641 fmt
->format
= *v4l2_subdev_get_try_format(subdev
, cfg
,
1644 struct v4l2_rect
*r
;
1646 if (fmt
->pad
== ssd
->source_pad
)
1647 r
= &ssd
->crop
[ssd
->source_pad
];
1651 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1652 fmt
->format
.width
= r
->width
;
1653 fmt
->format
.height
= r
->height
;
1654 fmt
->format
.field
= V4L2_FIELD_NONE
;
1660 static int smiapp_get_format(struct v4l2_subdev
*subdev
,
1661 struct v4l2_subdev_pad_config
*cfg
,
1662 struct v4l2_subdev_format
*fmt
)
1664 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1667 mutex_lock(&sensor
->mutex
);
1668 rval
= __smiapp_get_format(subdev
, cfg
, fmt
);
1669 mutex_unlock(&sensor
->mutex
);
1674 static void smiapp_get_crop_compose(struct v4l2_subdev
*subdev
,
1675 struct v4l2_subdev_pad_config
*cfg
,
1676 struct v4l2_rect
**crops
,
1677 struct v4l2_rect
**comps
, int which
)
1679 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1682 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1684 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++)
1685 crops
[i
] = &ssd
->crop
[i
];
1687 *comps
= &ssd
->compose
;
1690 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++) {
1691 crops
[i
] = v4l2_subdev_get_try_crop(subdev
, cfg
, i
);
1696 *comps
= v4l2_subdev_get_try_compose(subdev
, cfg
,
1703 /* Changes require propagation only on sink pad. */
1704 static void smiapp_propagate(struct v4l2_subdev
*subdev
,
1705 struct v4l2_subdev_pad_config
*cfg
, int which
,
1708 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1709 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1710 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
1712 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, which
);
1715 case V4L2_SEL_TGT_CROP
:
1716 comp
->width
= crops
[SMIAPP_PAD_SINK
]->width
;
1717 comp
->height
= crops
[SMIAPP_PAD_SINK
]->height
;
1718 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1719 if (ssd
== sensor
->scaler
) {
1722 SMIAPP_LIMIT_SCALER_N_MIN
];
1723 sensor
->scaling_mode
=
1724 SMIAPP_SCALING_MODE_NONE
;
1725 } else if (ssd
== sensor
->binner
) {
1726 sensor
->binning_horizontal
= 1;
1727 sensor
->binning_vertical
= 1;
1731 case V4L2_SEL_TGT_COMPOSE
:
1732 *crops
[SMIAPP_PAD_SRC
] = *comp
;
1739 static const struct smiapp_csi_data_format
1740 *smiapp_validate_csi_data_format(struct smiapp_sensor
*sensor
, u32 code
)
1744 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1745 if (sensor
->mbus_frame_fmts
& (1 << i
)
1746 && smiapp_csi_data_formats
[i
].code
== code
)
1747 return &smiapp_csi_data_formats
[i
];
1750 return sensor
->csi_format
;
1753 static int smiapp_set_format_source(struct v4l2_subdev
*subdev
,
1754 struct v4l2_subdev_pad_config
*cfg
,
1755 struct v4l2_subdev_format
*fmt
)
1757 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1758 const struct smiapp_csi_data_format
*csi_format
,
1759 *old_csi_format
= sensor
->csi_format
;
1760 unsigned long *valid_link_freqs
;
1761 u32 code
= fmt
->format
.code
;
1765 rval
= __smiapp_get_format(subdev
, cfg
, fmt
);
1770 * Media bus code is changeable on src subdev's source pad. On
1771 * other source pads we just get format here.
1773 if (subdev
!= &sensor
->src
->sd
)
1776 csi_format
= smiapp_validate_csi_data_format(sensor
, code
);
1778 fmt
->format
.code
= csi_format
->code
;
1780 if (fmt
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1783 sensor
->csi_format
= csi_format
;
1785 if (csi_format
->width
!= old_csi_format
->width
)
1786 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
1787 __v4l2_ctrl_modify_range(
1788 sensor
->test_data
[i
], 0,
1789 (1 << csi_format
->width
) - 1, 1, 0);
1791 if (csi_format
->compressed
== old_csi_format
->compressed
)
1795 &sensor
->valid_link_freqs
[sensor
->csi_format
->compressed
1796 - sensor
->compressed_min_bpp
];
1798 __v4l2_ctrl_modify_range(
1799 sensor
->link_freq
, 0,
1800 __fls(*valid_link_freqs
), ~*valid_link_freqs
,
1801 __ffs(*valid_link_freqs
));
1803 return smiapp_pll_update(sensor
);
1806 static int smiapp_set_format(struct v4l2_subdev
*subdev
,
1807 struct v4l2_subdev_pad_config
*cfg
,
1808 struct v4l2_subdev_format
*fmt
)
1810 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1811 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1812 struct v4l2_rect
*crops
[SMIAPP_PADS
];
1814 mutex_lock(&sensor
->mutex
);
1816 if (fmt
->pad
== ssd
->source_pad
) {
1819 rval
= smiapp_set_format_source(subdev
, cfg
, fmt
);
1821 mutex_unlock(&sensor
->mutex
);
1826 /* Sink pad. Width and height are changeable here. */
1827 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1828 fmt
->format
.width
&= ~1;
1829 fmt
->format
.height
&= ~1;
1830 fmt
->format
.field
= V4L2_FIELD_NONE
;
1833 clamp(fmt
->format
.width
,
1834 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
1835 sensor
->limits
[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE
]);
1836 fmt
->format
.height
=
1837 clamp(fmt
->format
.height
,
1838 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
1839 sensor
->limits
[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE
]);
1841 smiapp_get_crop_compose(subdev
, cfg
, crops
, NULL
, fmt
->which
);
1843 crops
[ssd
->sink_pad
]->left
= 0;
1844 crops
[ssd
->sink_pad
]->top
= 0;
1845 crops
[ssd
->sink_pad
]->width
= fmt
->format
.width
;
1846 crops
[ssd
->sink_pad
]->height
= fmt
->format
.height
;
1847 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
1848 ssd
->sink_fmt
= *crops
[ssd
->sink_pad
];
1849 smiapp_propagate(subdev
, cfg
, fmt
->which
,
1852 mutex_unlock(&sensor
->mutex
);
1858 * Calculate goodness of scaled image size compared to expected image
1859 * size and flags provided.
1861 #define SCALING_GOODNESS 100000
1862 #define SCALING_GOODNESS_EXTREME 100000000
1863 static int scaling_goodness(struct v4l2_subdev
*subdev
, int w
, int ask_w
,
1864 int h
, int ask_h
, u32 flags
)
1866 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1867 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1875 if (flags
& V4L2_SEL_FLAG_GE
) {
1877 val
-= SCALING_GOODNESS
;
1879 val
-= SCALING_GOODNESS
;
1882 if (flags
& V4L2_SEL_FLAG_LE
) {
1884 val
-= SCALING_GOODNESS
;
1886 val
-= SCALING_GOODNESS
;
1889 val
-= abs(w
- ask_w
);
1890 val
-= abs(h
- ask_h
);
1892 if (w
< sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
])
1893 val
-= SCALING_GOODNESS_EXTREME
;
1895 dev_dbg(&client
->dev
, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1896 w
, ask_h
, h
, ask_h
, val
);
1901 static void smiapp_set_compose_binner(struct v4l2_subdev
*subdev
,
1902 struct v4l2_subdev_pad_config
*cfg
,
1903 struct v4l2_subdev_selection
*sel
,
1904 struct v4l2_rect
**crops
,
1905 struct v4l2_rect
*comp
)
1907 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1909 unsigned int binh
= 1, binv
= 1;
1910 int best
= scaling_goodness(
1912 crops
[SMIAPP_PAD_SINK
]->width
, sel
->r
.width
,
1913 crops
[SMIAPP_PAD_SINK
]->height
, sel
->r
.height
, sel
->flags
);
1915 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
1916 int this = scaling_goodness(
1918 crops
[SMIAPP_PAD_SINK
]->width
1919 / sensor
->binning_subtypes
[i
].horizontal
,
1921 crops
[SMIAPP_PAD_SINK
]->height
1922 / sensor
->binning_subtypes
[i
].vertical
,
1923 sel
->r
.height
, sel
->flags
);
1926 binh
= sensor
->binning_subtypes
[i
].horizontal
;
1927 binv
= sensor
->binning_subtypes
[i
].vertical
;
1931 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1932 sensor
->binning_vertical
= binv
;
1933 sensor
->binning_horizontal
= binh
;
1936 sel
->r
.width
= (crops
[SMIAPP_PAD_SINK
]->width
/ binh
) & ~1;
1937 sel
->r
.height
= (crops
[SMIAPP_PAD_SINK
]->height
/ binv
) & ~1;
1941 * Calculate best scaling ratio and mode for given output resolution.
1943 * Try all of these: horizontal ratio, vertical ratio and smallest
1944 * size possible (horizontally).
1946 * Also try whether horizontal scaler or full scaler gives a better
1949 static void smiapp_set_compose_scaler(struct v4l2_subdev
*subdev
,
1950 struct v4l2_subdev_pad_config
*cfg
,
1951 struct v4l2_subdev_selection
*sel
,
1952 struct v4l2_rect
**crops
,
1953 struct v4l2_rect
*comp
)
1955 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1956 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1957 u32 min
, max
, a
, b
, max_m
;
1958 u32 scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
1959 int mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
1965 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
1966 crops
[SMIAPP_PAD_SINK
]->width
);
1967 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
1968 crops
[SMIAPP_PAD_SINK
]->height
);
1970 a
= crops
[SMIAPP_PAD_SINK
]->width
1971 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] / sel
->r
.width
;
1972 b
= crops
[SMIAPP_PAD_SINK
]->height
1973 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
] / sel
->r
.height
;
1974 max_m
= crops
[SMIAPP_PAD_SINK
]->width
1975 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]
1976 / sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
];
1978 a
= clamp(a
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1979 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1980 b
= clamp(b
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1981 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1982 max_m
= clamp(max_m
, sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MIN
],
1983 sensor
->limits
[SMIAPP_LIMIT_SCALER_M_MAX
]);
1985 dev_dbg(&client
->dev
, "scaling: a %d b %d max_m %d\n", a
, b
, max_m
);
1987 min
= min(max_m
, min(a
, b
));
1988 max
= min(max_m
, max(a
, b
));
1997 try[ntry
] = min
+ 1;
2000 try[ntry
] = max
+ 1;
2005 for (i
= 0; i
< ntry
; i
++) {
2006 int this = scaling_goodness(
2008 crops
[SMIAPP_PAD_SINK
]->width
2010 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2012 crops
[SMIAPP_PAD_SINK
]->height
,
2016 dev_dbg(&client
->dev
, "trying factor %d (%d)\n", try[i
], i
);
2020 mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
2024 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2025 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2028 this = scaling_goodness(
2029 subdev
, crops
[SMIAPP_PAD_SINK
]->width
2031 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2033 crops
[SMIAPP_PAD_SINK
]->height
2035 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
],
2041 mode
= SMIAPP_SCALING_MODE_BOTH
;
2047 (crops
[SMIAPP_PAD_SINK
]->width
2049 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
]) & ~1;
2050 if (mode
== SMIAPP_SCALING_MODE_BOTH
)
2052 (crops
[SMIAPP_PAD_SINK
]->height
2054 * sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
])
2057 sel
->r
.height
= crops
[SMIAPP_PAD_SINK
]->height
;
2059 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2060 sensor
->scale_m
= scale_m
;
2061 sensor
->scaling_mode
= mode
;
2064 /* We're only called on source pads. This function sets scaling. */
2065 static int smiapp_set_compose(struct v4l2_subdev
*subdev
,
2066 struct v4l2_subdev_pad_config
*cfg
,
2067 struct v4l2_subdev_selection
*sel
)
2069 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2070 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2071 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2073 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, sel
->which
);
2078 if (ssd
== sensor
->binner
)
2079 smiapp_set_compose_binner(subdev
, cfg
, sel
, crops
, comp
);
2081 smiapp_set_compose_scaler(subdev
, cfg
, sel
, crops
, comp
);
2084 smiapp_propagate(subdev
, cfg
, sel
->which
, V4L2_SEL_TGT_COMPOSE
);
2086 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
2087 return smiapp_update_mode(sensor
);
2092 static int __smiapp_sel_supported(struct v4l2_subdev
*subdev
,
2093 struct v4l2_subdev_selection
*sel
)
2095 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2096 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2098 /* We only implement crop in three places. */
2099 switch (sel
->target
) {
2100 case V4L2_SEL_TGT_CROP
:
2101 case V4L2_SEL_TGT_CROP_BOUNDS
:
2102 if (ssd
== sensor
->pixel_array
2103 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2105 if (ssd
== sensor
->src
2106 && sel
->pad
== SMIAPP_PAD_SRC
)
2108 if (ssd
== sensor
->scaler
2109 && sel
->pad
== SMIAPP_PAD_SINK
2110 && sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
2111 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
)
2114 case V4L2_SEL_TGT_NATIVE_SIZE
:
2115 if (ssd
== sensor
->pixel_array
2116 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2119 case V4L2_SEL_TGT_COMPOSE
:
2120 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2121 if (sel
->pad
== ssd
->source_pad
)
2123 if (ssd
== sensor
->binner
)
2125 if (ssd
== sensor
->scaler
2126 && sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
2127 != SMIAPP_SCALING_CAPABILITY_NONE
)
2135 static int smiapp_set_crop(struct v4l2_subdev
*subdev
,
2136 struct v4l2_subdev_pad_config
*cfg
,
2137 struct v4l2_subdev_selection
*sel
)
2139 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2140 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2141 struct v4l2_rect
*src_size
, *crops
[SMIAPP_PADS
];
2142 struct v4l2_rect _r
;
2144 smiapp_get_crop_compose(subdev
, cfg
, crops
, NULL
, sel
->which
);
2146 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2147 if (sel
->pad
== ssd
->sink_pad
)
2148 src_size
= &ssd
->sink_fmt
;
2150 src_size
= &ssd
->compose
;
2152 if (sel
->pad
== ssd
->sink_pad
) {
2155 _r
.width
= v4l2_subdev_get_try_format(subdev
, cfg
, sel
->pad
)
2157 _r
.height
= v4l2_subdev_get_try_format(subdev
, cfg
, sel
->pad
)
2161 src_size
= v4l2_subdev_get_try_compose(
2162 subdev
, cfg
, ssd
->sink_pad
);
2166 if (ssd
== sensor
->src
&& sel
->pad
== SMIAPP_PAD_SRC
) {
2171 sel
->r
.width
= min(sel
->r
.width
, src_size
->width
);
2172 sel
->r
.height
= min(sel
->r
.height
, src_size
->height
);
2174 sel
->r
.left
= min_t(int, sel
->r
.left
, src_size
->width
- sel
->r
.width
);
2175 sel
->r
.top
= min_t(int, sel
->r
.top
, src_size
->height
- sel
->r
.height
);
2177 *crops
[sel
->pad
] = sel
->r
;
2179 if (ssd
!= sensor
->pixel_array
&& sel
->pad
== SMIAPP_PAD_SINK
)
2180 smiapp_propagate(subdev
, cfg
, sel
->which
,
2186 static void smiapp_get_native_size(struct smiapp_subdev
*ssd
,
2187 struct v4l2_rect
*r
)
2191 r
->width
= ssd
->sensor
->limits
[SMIAPP_LIMIT_X_ADDR_MAX
] + 1;
2192 r
->height
= ssd
->sensor
->limits
[SMIAPP_LIMIT_Y_ADDR_MAX
] + 1;
2195 static int __smiapp_get_selection(struct v4l2_subdev
*subdev
,
2196 struct v4l2_subdev_pad_config
*cfg
,
2197 struct v4l2_subdev_selection
*sel
)
2199 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2200 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2201 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2202 struct v4l2_rect sink_fmt
;
2205 ret
= __smiapp_sel_supported(subdev
, sel
);
2209 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, sel
->which
);
2211 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2212 sink_fmt
= ssd
->sink_fmt
;
2214 struct v4l2_mbus_framefmt
*fmt
=
2215 v4l2_subdev_get_try_format(subdev
, cfg
, ssd
->sink_pad
);
2219 sink_fmt
.width
= fmt
->width
;
2220 sink_fmt
.height
= fmt
->height
;
2223 switch (sel
->target
) {
2224 case V4L2_SEL_TGT_CROP_BOUNDS
:
2225 case V4L2_SEL_TGT_NATIVE_SIZE
:
2226 if (ssd
== sensor
->pixel_array
)
2227 smiapp_get_native_size(ssd
, &sel
->r
);
2228 else if (sel
->pad
== ssd
->sink_pad
)
2233 case V4L2_SEL_TGT_CROP
:
2234 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2235 sel
->r
= *crops
[sel
->pad
];
2237 case V4L2_SEL_TGT_COMPOSE
:
2245 static int smiapp_get_selection(struct v4l2_subdev
*subdev
,
2246 struct v4l2_subdev_pad_config
*cfg
,
2247 struct v4l2_subdev_selection
*sel
)
2249 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2252 mutex_lock(&sensor
->mutex
);
2253 rval
= __smiapp_get_selection(subdev
, cfg
, sel
);
2254 mutex_unlock(&sensor
->mutex
);
2258 static int smiapp_set_selection(struct v4l2_subdev
*subdev
,
2259 struct v4l2_subdev_pad_config
*cfg
,
2260 struct v4l2_subdev_selection
*sel
)
2262 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2265 ret
= __smiapp_sel_supported(subdev
, sel
);
2269 mutex_lock(&sensor
->mutex
);
2271 sel
->r
.left
= max(0, sel
->r
.left
& ~1);
2272 sel
->r
.top
= max(0, sel
->r
.top
& ~1);
2273 sel
->r
.width
= SMIAPP_ALIGN_DIM(sel
->r
.width
, sel
->flags
);
2274 sel
->r
.height
= SMIAPP_ALIGN_DIM(sel
->r
.height
, sel
->flags
);
2276 sel
->r
.width
= max_t(unsigned int,
2277 sensor
->limits
[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE
],
2279 sel
->r
.height
= max_t(unsigned int,
2280 sensor
->limits
[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE
],
2283 switch (sel
->target
) {
2284 case V4L2_SEL_TGT_CROP
:
2285 ret
= smiapp_set_crop(subdev
, cfg
, sel
);
2287 case V4L2_SEL_TGT_COMPOSE
:
2288 ret
= smiapp_set_compose(subdev
, cfg
, sel
);
2294 mutex_unlock(&sensor
->mutex
);
2298 static int smiapp_get_skip_frames(struct v4l2_subdev
*subdev
, u32
*frames
)
2300 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2302 *frames
= sensor
->frame_skip
;
2306 static int smiapp_get_skip_top_lines(struct v4l2_subdev
*subdev
, u32
*lines
)
2308 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2310 *lines
= sensor
->image_start
;
2315 /* -----------------------------------------------------------------------------
2320 smiapp_sysfs_nvm_read(struct device
*dev
, struct device_attribute
*attr
,
2323 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2324 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2325 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2326 unsigned int nbytes
;
2328 if (!sensor
->dev_init_done
)
2331 if (!sensor
->nvm_size
) {
2334 /* NVM not read yet - read it now */
2335 sensor
->nvm_size
= sensor
->hwcfg
->nvm_size
;
2337 rval
= pm_runtime_get_sync(&client
->dev
);
2339 if (rval
!= -EBUSY
&& rval
!= -EAGAIN
)
2340 pm_runtime_set_active(&client
->dev
);
2341 pm_runtime_put(&client
->dev
);
2345 if (smiapp_read_nvm(sensor
, sensor
->nvm
)) {
2346 dev_err(&client
->dev
, "nvm read failed\n");
2350 pm_runtime_mark_last_busy(&client
->dev
);
2351 pm_runtime_put_autosuspend(&client
->dev
);
2354 * NVM is still way below a PAGE_SIZE, so we can safely
2355 * assume this for now.
2357 nbytes
= min_t(unsigned int, sensor
->nvm_size
, PAGE_SIZE
);
2358 memcpy(buf
, sensor
->nvm
, nbytes
);
2362 static DEVICE_ATTR(nvm
, S_IRUGO
, smiapp_sysfs_nvm_read
, NULL
);
2365 smiapp_sysfs_ident_read(struct device
*dev
, struct device_attribute
*attr
,
2368 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2369 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2370 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2372 return snprintf(buf
, PAGE_SIZE
, "%2.2x%4.4x%2.2x\n",
2373 minfo
->manufacturer_id
, minfo
->model_id
,
2374 minfo
->revision_number_major
) + 1;
2377 static DEVICE_ATTR(ident
, S_IRUGO
, smiapp_sysfs_ident_read
, NULL
);
2379 /* -----------------------------------------------------------------------------
2380 * V4L2 subdev core operations
2383 static int smiapp_identify_module(struct smiapp_sensor
*sensor
)
2385 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2386 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2390 minfo
->name
= SMIAPP_NAME
;
2393 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MANUFACTURER_ID
,
2394 &minfo
->manufacturer_id
);
2396 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U16_MODEL_ID
,
2399 rval
= smiapp_read_8only(sensor
,
2400 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR
,
2401 &minfo
->revision_number_major
);
2403 rval
= smiapp_read_8only(sensor
,
2404 SMIAPP_REG_U8_REVISION_NUMBER_MINOR
,
2405 &minfo
->revision_number_minor
);
2407 rval
= smiapp_read_8only(sensor
,
2408 SMIAPP_REG_U8_MODULE_DATE_YEAR
,
2409 &minfo
->module_year
);
2411 rval
= smiapp_read_8only(sensor
,
2412 SMIAPP_REG_U8_MODULE_DATE_MONTH
,
2413 &minfo
->module_month
);
2415 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MODULE_DATE_DAY
,
2416 &minfo
->module_day
);
2420 rval
= smiapp_read_8only(sensor
,
2421 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID
,
2422 &minfo
->sensor_manufacturer_id
);
2424 rval
= smiapp_read_8only(sensor
,
2425 SMIAPP_REG_U16_SENSOR_MODEL_ID
,
2426 &minfo
->sensor_model_id
);
2428 rval
= smiapp_read_8only(sensor
,
2429 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER
,
2430 &minfo
->sensor_revision_number
);
2432 rval
= smiapp_read_8only(sensor
,
2433 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION
,
2434 &minfo
->sensor_firmware_version
);
2438 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIA_VERSION
,
2439 &minfo
->smia_version
);
2441 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIAPP_VERSION
,
2442 &minfo
->smiapp_version
);
2445 dev_err(&client
->dev
, "sensor detection failed\n");
2449 dev_dbg(&client
->dev
, "module 0x%2.2x-0x%4.4x\n",
2450 minfo
->manufacturer_id
, minfo
->model_id
);
2452 dev_dbg(&client
->dev
,
2453 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2454 minfo
->revision_number_major
, minfo
->revision_number_minor
,
2455 minfo
->module_year
, minfo
->module_month
, minfo
->module_day
);
2457 dev_dbg(&client
->dev
, "sensor 0x%2.2x-0x%4.4x\n",
2458 minfo
->sensor_manufacturer_id
, minfo
->sensor_model_id
);
2460 dev_dbg(&client
->dev
,
2461 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2462 minfo
->sensor_revision_number
, minfo
->sensor_firmware_version
);
2464 dev_dbg(&client
->dev
, "smia version %2.2d smiapp version %2.2d\n",
2465 minfo
->smia_version
, minfo
->smiapp_version
);
2468 * Some modules have bad data in the lvalues below. Hope the
2469 * rvalues have better stuff. The lvalues are module
2470 * parameters whereas the rvalues are sensor parameters.
2472 if (!minfo
->manufacturer_id
&& !minfo
->model_id
) {
2473 minfo
->manufacturer_id
= minfo
->sensor_manufacturer_id
;
2474 minfo
->model_id
= minfo
->sensor_model_id
;
2475 minfo
->revision_number_major
= minfo
->sensor_revision_number
;
2478 for (i
= 0; i
< ARRAY_SIZE(smiapp_module_idents
); i
++) {
2479 if (smiapp_module_idents
[i
].manufacturer_id
2480 != minfo
->manufacturer_id
)
2482 if (smiapp_module_idents
[i
].model_id
!= minfo
->model_id
)
2484 if (smiapp_module_idents
[i
].flags
2485 & SMIAPP_MODULE_IDENT_FLAG_REV_LE
) {
2486 if (smiapp_module_idents
[i
].revision_number_major
2487 < minfo
->revision_number_major
)
2490 if (smiapp_module_idents
[i
].revision_number_major
2491 != minfo
->revision_number_major
)
2495 minfo
->name
= smiapp_module_idents
[i
].name
;
2496 minfo
->quirk
= smiapp_module_idents
[i
].quirk
;
2500 if (i
>= ARRAY_SIZE(smiapp_module_idents
))
2501 dev_warn(&client
->dev
,
2502 "no quirks for this module; let's hope it's fully compliant\n");
2504 dev_dbg(&client
->dev
, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2505 minfo
->name
, minfo
->manufacturer_id
, minfo
->model_id
,
2506 minfo
->revision_number_major
);
2511 static const struct v4l2_subdev_ops smiapp_ops
;
2512 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
;
2513 static const struct media_entity_operations smiapp_entity_ops
;
2515 static int smiapp_register_subdev(struct smiapp_sensor
*sensor
,
2516 struct smiapp_subdev
*ssd
,
2517 struct smiapp_subdev
*sink_ssd
,
2518 u16 source_pad
, u16 sink_pad
, u32 link_flags
)
2520 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2526 rval
= media_entity_pads_init(&ssd
->sd
.entity
,
2527 ssd
->npads
, ssd
->pads
);
2529 dev_err(&client
->dev
,
2530 "media_entity_pads_init failed\n");
2534 rval
= v4l2_device_register_subdev(sensor
->src
->sd
.v4l2_dev
,
2537 dev_err(&client
->dev
,
2538 "v4l2_device_register_subdev failed\n");
2542 rval
= media_create_pad_link(&ssd
->sd
.entity
, source_pad
,
2543 &sink_ssd
->sd
.entity
, sink_pad
,
2546 dev_err(&client
->dev
,
2547 "media_create_pad_link failed\n");
2548 v4l2_device_unregister_subdev(&ssd
->sd
);
2555 static void smiapp_unregistered(struct v4l2_subdev
*subdev
)
2557 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2560 for (i
= 1; i
< sensor
->ssds_used
; i
++)
2561 v4l2_device_unregister_subdev(&sensor
->ssds
[i
].sd
);
2564 static int smiapp_registered(struct v4l2_subdev
*subdev
)
2566 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2569 if (sensor
->scaler
) {
2570 rval
= smiapp_register_subdev(
2571 sensor
, sensor
->binner
, sensor
->scaler
,
2572 SMIAPP_PAD_SRC
, SMIAPP_PAD_SINK
,
2573 MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
);
2578 rval
= smiapp_register_subdev(
2579 sensor
, sensor
->pixel_array
, sensor
->binner
,
2580 SMIAPP_PA_PAD_SRC
, SMIAPP_PAD_SINK
,
2581 MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
);
2588 smiapp_unregistered(subdev
);
2593 static void smiapp_cleanup(struct smiapp_sensor
*sensor
)
2595 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2597 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2598 device_remove_file(&client
->dev
, &dev_attr_ident
);
2600 smiapp_free_controls(sensor
);
2603 static void smiapp_create_subdev(struct smiapp_sensor
*sensor
,
2604 struct smiapp_subdev
*ssd
, const char *name
,
2605 unsigned short num_pads
)
2607 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2612 if (ssd
!= sensor
->src
)
2613 v4l2_subdev_init(&ssd
->sd
, &smiapp_ops
);
2615 ssd
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2616 ssd
->sensor
= sensor
;
2618 ssd
->npads
= num_pads
;
2619 ssd
->source_pad
= num_pads
- 1;
2621 snprintf(ssd
->sd
.name
,
2622 sizeof(ssd
->sd
.name
), "%s %s %d-%4.4x", sensor
->minfo
.name
,
2623 name
, i2c_adapter_id(client
->adapter
), client
->addr
);
2625 smiapp_get_native_size(ssd
, &ssd
->sink_fmt
);
2627 ssd
->compose
.width
= ssd
->sink_fmt
.width
;
2628 ssd
->compose
.height
= ssd
->sink_fmt
.height
;
2629 ssd
->crop
[ssd
->source_pad
] = ssd
->compose
;
2630 ssd
->pads
[ssd
->source_pad
].flags
= MEDIA_PAD_FL_SOURCE
;
2631 if (ssd
!= sensor
->pixel_array
) {
2632 ssd
->crop
[ssd
->sink_pad
] = ssd
->compose
;
2633 ssd
->pads
[ssd
->sink_pad
].flags
= MEDIA_PAD_FL_SINK
;
2636 ssd
->sd
.entity
.ops
= &smiapp_entity_ops
;
2638 if (ssd
== sensor
->src
)
2641 ssd
->sd
.internal_ops
= &smiapp_internal_ops
;
2642 ssd
->sd
.owner
= THIS_MODULE
;
2643 ssd
->sd
.dev
= &client
->dev
;
2644 v4l2_set_subdevdata(&ssd
->sd
, client
);
2647 static int smiapp_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2649 struct smiapp_subdev
*ssd
= to_smiapp_subdev(sd
);
2650 struct smiapp_sensor
*sensor
= ssd
->sensor
;
2653 mutex_lock(&sensor
->mutex
);
2655 for (i
= 0; i
< ssd
->npads
; i
++) {
2656 struct v4l2_mbus_framefmt
*try_fmt
=
2657 v4l2_subdev_get_try_format(sd
, fh
->pad
, i
);
2658 struct v4l2_rect
*try_crop
=
2659 v4l2_subdev_get_try_crop(sd
, fh
->pad
, i
);
2660 struct v4l2_rect
*try_comp
;
2662 smiapp_get_native_size(ssd
, try_crop
);
2664 try_fmt
->width
= try_crop
->width
;
2665 try_fmt
->height
= try_crop
->height
;
2666 try_fmt
->code
= sensor
->internal_csi_format
->code
;
2667 try_fmt
->field
= V4L2_FIELD_NONE
;
2669 if (ssd
!= sensor
->pixel_array
)
2672 try_comp
= v4l2_subdev_get_try_compose(sd
, fh
->pad
, i
);
2673 *try_comp
= *try_crop
;
2676 mutex_unlock(&sensor
->mutex
);
2681 static const struct v4l2_subdev_video_ops smiapp_video_ops
= {
2682 .s_stream
= smiapp_set_stream
,
2685 static const struct v4l2_subdev_pad_ops smiapp_pad_ops
= {
2686 .enum_mbus_code
= smiapp_enum_mbus_code
,
2687 .get_fmt
= smiapp_get_format
,
2688 .set_fmt
= smiapp_set_format
,
2689 .get_selection
= smiapp_get_selection
,
2690 .set_selection
= smiapp_set_selection
,
2693 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops
= {
2694 .g_skip_frames
= smiapp_get_skip_frames
,
2695 .g_skip_top_lines
= smiapp_get_skip_top_lines
,
2698 static const struct v4l2_subdev_ops smiapp_ops
= {
2699 .video
= &smiapp_video_ops
,
2700 .pad
= &smiapp_pad_ops
,
2701 .sensor
= &smiapp_sensor_ops
,
2704 static const struct media_entity_operations smiapp_entity_ops
= {
2705 .link_validate
= v4l2_subdev_link_validate
,
2708 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops
= {
2709 .registered
= smiapp_registered
,
2710 .unregistered
= smiapp_unregistered
,
2711 .open
= smiapp_open
,
2714 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
= {
2715 .open
= smiapp_open
,
2718 /* -----------------------------------------------------------------------------
2722 static int __maybe_unused
smiapp_suspend(struct device
*dev
)
2724 struct i2c_client
*client
= to_i2c_client(dev
);
2725 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2726 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2727 bool streaming
= sensor
->streaming
;
2730 rval
= pm_runtime_get_sync(dev
);
2732 if (rval
!= -EBUSY
&& rval
!= -EAGAIN
)
2733 pm_runtime_set_active(&client
->dev
);
2734 pm_runtime_put(dev
);
2738 if (sensor
->streaming
)
2739 smiapp_stop_streaming(sensor
);
2741 /* save state for resume */
2742 sensor
->streaming
= streaming
;
2747 static int __maybe_unused
smiapp_resume(struct device
*dev
)
2749 struct i2c_client
*client
= to_i2c_client(dev
);
2750 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2751 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2754 pm_runtime_put(dev
);
2756 if (sensor
->streaming
)
2757 rval
= smiapp_start_streaming(sensor
);
2762 static struct smiapp_hwconfig
*smiapp_get_hwconfig(struct device
*dev
)
2764 struct smiapp_hwconfig
*hwcfg
;
2765 struct v4l2_fwnode_endpoint
*bus_cfg
;
2766 struct fwnode_handle
*ep
;
2767 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
2772 return dev
->platform_data
;
2774 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
2778 bus_cfg
= v4l2_fwnode_endpoint_alloc_parse(ep
);
2779 if (IS_ERR(bus_cfg
))
2782 hwcfg
= devm_kzalloc(dev
, sizeof(*hwcfg
), GFP_KERNEL
);
2786 switch (bus_cfg
->bus_type
) {
2787 case V4L2_MBUS_CSI2
:
2788 hwcfg
->csi_signalling_mode
= SMIAPP_CSI_SIGNALLING_MODE_CSI2
;
2789 hwcfg
->lanes
= bus_cfg
->bus
.mipi_csi2
.num_data_lanes
;
2791 case V4L2_MBUS_CCP2
:
2792 hwcfg
->csi_signalling_mode
= (bus_cfg
->bus
.mipi_csi1
.strobe
) ?
2793 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE
:
2794 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK
;
2798 dev_err(dev
, "unsupported bus %u\n", bus_cfg
->bus_type
);
2802 dev_dbg(dev
, "lanes %u\n", hwcfg
->lanes
);
2804 /* NVM size is not mandatory */
2805 fwnode_property_read_u32(fwnode
, "nokia,nvm-size", &hwcfg
->nvm_size
);
2807 rval
= fwnode_property_read_u32(dev_fwnode(dev
), "clock-frequency",
2810 dev_info(dev
, "can't get clock-frequency\n");
2812 dev_dbg(dev
, "nvm %d, clk %d, mode %d\n",
2813 hwcfg
->nvm_size
, hwcfg
->ext_clk
, hwcfg
->csi_signalling_mode
);
2815 if (!bus_cfg
->nr_of_link_frequencies
) {
2816 dev_warn(dev
, "no link frequencies defined\n");
2820 hwcfg
->op_sys_clock
= devm_kcalloc(
2821 dev
, bus_cfg
->nr_of_link_frequencies
+ 1 /* guardian */,
2822 sizeof(*hwcfg
->op_sys_clock
), GFP_KERNEL
);
2823 if (!hwcfg
->op_sys_clock
)
2826 for (i
= 0; i
< bus_cfg
->nr_of_link_frequencies
; i
++) {
2827 hwcfg
->op_sys_clock
[i
] = bus_cfg
->link_frequencies
[i
];
2828 dev_dbg(dev
, "freq %d: %lld\n", i
, hwcfg
->op_sys_clock
[i
]);
2831 v4l2_fwnode_endpoint_free(bus_cfg
);
2832 fwnode_handle_put(ep
);
2836 v4l2_fwnode_endpoint_free(bus_cfg
);
2837 fwnode_handle_put(ep
);
2841 static int smiapp_probe(struct i2c_client
*client
,
2842 const struct i2c_device_id
*devid
)
2844 struct smiapp_sensor
*sensor
;
2845 struct smiapp_hwconfig
*hwcfg
= smiapp_get_hwconfig(&client
->dev
);
2852 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
2856 sensor
->hwcfg
= hwcfg
;
2857 mutex_init(&sensor
->mutex
);
2858 sensor
->src
= &sensor
->ssds
[sensor
->ssds_used
];
2860 v4l2_i2c_subdev_init(&sensor
->src
->sd
, client
, &smiapp_ops
);
2861 sensor
->src
->sd
.internal_ops
= &smiapp_internal_src_ops
;
2863 sensor
->vana
= devm_regulator_get(&client
->dev
, "vana");
2864 if (IS_ERR(sensor
->vana
)) {
2865 dev_err(&client
->dev
, "could not get regulator for vana\n");
2866 return PTR_ERR(sensor
->vana
);
2869 sensor
->ext_clk
= devm_clk_get(&client
->dev
, NULL
);
2870 if (PTR_ERR(sensor
->ext_clk
) == -ENOENT
) {
2871 dev_info(&client
->dev
, "no clock defined, continuing...\n");
2872 sensor
->ext_clk
= NULL
;
2873 } else if (IS_ERR(sensor
->ext_clk
)) {
2874 dev_err(&client
->dev
, "could not get clock (%ld)\n",
2875 PTR_ERR(sensor
->ext_clk
));
2876 return -EPROBE_DEFER
;
2879 if (sensor
->ext_clk
) {
2880 if (sensor
->hwcfg
->ext_clk
) {
2883 rval
= clk_set_rate(sensor
->ext_clk
,
2884 sensor
->hwcfg
->ext_clk
);
2886 dev_err(&client
->dev
,
2887 "unable to set clock freq to %u\n",
2888 sensor
->hwcfg
->ext_clk
);
2892 rate
= clk_get_rate(sensor
->ext_clk
);
2893 if (rate
!= sensor
->hwcfg
->ext_clk
) {
2894 dev_err(&client
->dev
,
2895 "can't set clock freq, asked for %u but got %lu\n",
2896 sensor
->hwcfg
->ext_clk
, rate
);
2900 sensor
->hwcfg
->ext_clk
= clk_get_rate(sensor
->ext_clk
);
2901 dev_dbg(&client
->dev
, "obtained clock freq %u\n",
2902 sensor
->hwcfg
->ext_clk
);
2904 } else if (sensor
->hwcfg
->ext_clk
) {
2905 dev_dbg(&client
->dev
, "assuming clock freq %u\n",
2906 sensor
->hwcfg
->ext_clk
);
2908 dev_err(&client
->dev
, "unable to obtain clock freq\n");
2912 sensor
->xshutdown
= devm_gpiod_get_optional(&client
->dev
, "xshutdown",
2914 if (IS_ERR(sensor
->xshutdown
))
2915 return PTR_ERR(sensor
->xshutdown
);
2917 rval
= smiapp_power_on(&client
->dev
);
2921 rval
= smiapp_identify_module(sensor
);
2927 rval
= smiapp_get_all_limits(sensor
);
2933 rval
= smiapp_read_frame_fmt(sensor
);
2940 * Handle Sensor Module orientation on the board.
2942 * The application of H-FLIP and V-FLIP on the sensor is modified by
2943 * the sensor orientation on the board.
2945 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2946 * both H-FLIP and V-FLIP for normal operation which also implies
2947 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2948 * controls will need to be internally inverted.
2950 * Rotation also changes the bayer pattern.
2952 if (sensor
->hwcfg
->module_board_orient
==
2953 SMIAPP_MODULE_BOARD_ORIENT_180
)
2954 sensor
->hvflip_inv_mask
= SMIAPP_IMAGE_ORIENTATION_HFLIP
|
2955 SMIAPP_IMAGE_ORIENTATION_VFLIP
;
2957 rval
= smiapp_call_quirk(sensor
, limits
);
2959 dev_err(&client
->dev
, "limits quirks failed\n");
2963 if (sensor
->limits
[SMIAPP_LIMIT_BINNING_CAPABILITY
]) {
2966 rval
= smiapp_read(sensor
,
2967 SMIAPP_REG_U8_BINNING_SUBTYPES
, &val
);
2972 sensor
->nbinning_subtypes
= min_t(u8
, val
,
2973 SMIAPP_BINNING_SUBTYPES
);
2975 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
2977 sensor
, SMIAPP_REG_U8_BINNING_TYPE_n(i
), &val
);
2982 sensor
->binning_subtypes
[i
] =
2983 *(struct smiapp_binning_subtype
*)&val
;
2985 dev_dbg(&client
->dev
, "binning %xx%x\n",
2986 sensor
->binning_subtypes
[i
].horizontal
,
2987 sensor
->binning_subtypes
[i
].vertical
);
2990 sensor
->binning_horizontal
= 1;
2991 sensor
->binning_vertical
= 1;
2993 if (device_create_file(&client
->dev
, &dev_attr_ident
) != 0) {
2994 dev_err(&client
->dev
, "sysfs ident entry creation failed\n");
2998 /* SMIA++ NVM initialization - it will be read from the sensor
2999 * when it is first requested by userspace.
3001 if (sensor
->minfo
.smiapp_version
&& sensor
->hwcfg
->nvm_size
) {
3002 sensor
->nvm
= devm_kzalloc(&client
->dev
,
3003 sensor
->hwcfg
->nvm_size
, GFP_KERNEL
);
3004 if (sensor
->nvm
== NULL
) {
3009 if (device_create_file(&client
->dev
, &dev_attr_nvm
) != 0) {
3010 dev_err(&client
->dev
, "sysfs nvm entry failed\n");
3016 /* We consider this as profile 0 sensor if any of these are zero. */
3017 if (!sensor
->limits
[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV
] ||
3018 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV
] ||
3019 !sensor
->limits
[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV
] ||
3020 !sensor
->limits
[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV
]) {
3021 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_0
;
3022 } else if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
3023 != SMIAPP_SCALING_CAPABILITY_NONE
) {
3024 if (sensor
->limits
[SMIAPP_LIMIT_SCALING_CAPABILITY
]
3025 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
3026 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_1
;
3028 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_2
;
3029 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
3030 sensor
->ssds_used
++;
3031 } else if (sensor
->limits
[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY
]
3032 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
3033 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
3034 sensor
->ssds_used
++;
3036 sensor
->binner
= &sensor
->ssds
[sensor
->ssds_used
];
3037 sensor
->ssds_used
++;
3038 sensor
->pixel_array
= &sensor
->ssds
[sensor
->ssds_used
];
3039 sensor
->ssds_used
++;
3041 sensor
->scale_m
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
3043 /* prepare PLL configuration input values */
3044 sensor
->pll
.bus_type
= SMIAPP_PLL_BUS_TYPE_CSI2
;
3045 sensor
->pll
.csi2
.lanes
= sensor
->hwcfg
->lanes
;
3046 sensor
->pll
.ext_clk_freq_hz
= sensor
->hwcfg
->ext_clk
;
3047 sensor
->pll
.scale_n
= sensor
->limits
[SMIAPP_LIMIT_SCALER_N_MIN
];
3048 /* Profile 0 sensors have no separate OP clock branch. */
3049 if (sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
3050 sensor
->pll
.flags
|= SMIAPP_PLL_FLAG_NO_OP_CLOCKS
;
3052 smiapp_create_subdev(sensor
, sensor
->scaler
, "scaler", 2);
3053 smiapp_create_subdev(sensor
, sensor
->binner
, "binner", 2);
3054 smiapp_create_subdev(sensor
, sensor
->pixel_array
, "pixel_array", 1);
3056 dev_dbg(&client
->dev
, "profile %d\n", sensor
->minfo
.smiapp_profile
);
3058 sensor
->pixel_array
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
3060 rval
= smiapp_init_controls(sensor
);
3064 rval
= smiapp_call_quirk(sensor
, init
);
3068 rval
= smiapp_get_mbus_formats(sensor
);
3074 rval
= smiapp_init_late_controls(sensor
);
3080 mutex_lock(&sensor
->mutex
);
3081 rval
= smiapp_update_mode(sensor
);
3082 mutex_unlock(&sensor
->mutex
);
3084 dev_err(&client
->dev
, "update mode failed\n");
3088 sensor
->streaming
= false;
3089 sensor
->dev_init_done
= true;
3091 rval
= media_entity_pads_init(&sensor
->src
->sd
.entity
, 2,
3094 goto out_media_entity_cleanup
;
3096 rval
= v4l2_async_register_subdev_sensor_common(&sensor
->src
->sd
);
3098 goto out_media_entity_cleanup
;
3100 pm_runtime_set_active(&client
->dev
);
3101 pm_runtime_get_noresume(&client
->dev
);
3102 pm_runtime_enable(&client
->dev
);
3103 pm_runtime_set_autosuspend_delay(&client
->dev
, 1000);
3104 pm_runtime_use_autosuspend(&client
->dev
);
3105 pm_runtime_put_autosuspend(&client
->dev
);
3109 out_media_entity_cleanup
:
3110 media_entity_cleanup(&sensor
->src
->sd
.entity
);
3113 smiapp_cleanup(sensor
);
3116 smiapp_power_off(&client
->dev
);
3121 static int smiapp_remove(struct i2c_client
*client
)
3123 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
3124 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
3127 v4l2_async_unregister_subdev(subdev
);
3129 pm_runtime_disable(&client
->dev
);
3130 if (!pm_runtime_status_suspended(&client
->dev
))
3131 smiapp_power_off(&client
->dev
);
3132 pm_runtime_set_suspended(&client
->dev
);
3134 for (i
= 0; i
< sensor
->ssds_used
; i
++) {
3135 v4l2_device_unregister_subdev(&sensor
->ssds
[i
].sd
);
3136 media_entity_cleanup(&sensor
->ssds
[i
].sd
.entity
);
3138 smiapp_cleanup(sensor
);
3143 static const struct of_device_id smiapp_of_table
[] = {
3144 { .compatible
= "nokia,smia" },
3147 MODULE_DEVICE_TABLE(of
, smiapp_of_table
);
3149 static const struct i2c_device_id smiapp_id_table
[] = {
3153 MODULE_DEVICE_TABLE(i2c
, smiapp_id_table
);
3155 static const struct dev_pm_ops smiapp_pm_ops
= {
3156 SET_SYSTEM_SLEEP_PM_OPS(smiapp_suspend
, smiapp_resume
)
3157 SET_RUNTIME_PM_OPS(smiapp_power_off
, smiapp_power_on
, NULL
)
3160 static struct i2c_driver smiapp_i2c_driver
= {
3162 .of_match_table
= smiapp_of_table
,
3163 .name
= SMIAPP_NAME
,
3164 .pm
= &smiapp_pm_ops
,
3166 .probe
= smiapp_probe
,
3167 .remove
= smiapp_remove
,
3168 .id_table
= smiapp_id_table
,
3171 module_i2c_driver(smiapp_i2c_driver
);
3173 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3174 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3175 MODULE_LICENSE("GPL");