1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/media/i2c/smiapp/smiapp-core.c
5 * Generic driver for SMIA/SMIA++ compliant camera modules
7 * Copyright (C) 2010--2012 Nokia Corporation
8 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
10 * Based on smiapp driver by Vimarsh Zutshi
11 * Based on jt8ev1.c by Vimarsh Zutshi
12 * Based on smia-sensor.c by Tuukka Toivonen <tuukkat76@gmail.com>
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/gpio.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/property.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/smiapp.h>
26 #include <linux/v4l2-mediabus.h>
27 #include <media/v4l2-fwnode.h>
28 #include <media/v4l2-device.h>
32 #define SMIAPP_ALIGN_DIM(dim, flags) \
33 ((flags) & V4L2_SEL_FLAG_GE \
38 * smiapp_module_idents - supported camera modules
40 static const struct smiapp_module_ident smiapp_module_idents
[] = {
41 SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"),
42 SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"),
43 SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
44 SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
45 SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
46 SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk
),
47 SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
48 SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
49 SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk
),
50 SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk
),
51 SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk
),
56 * Dynamic Capability Identification
60 static u32
smiapp_get_limit(struct smiapp_sensor
*sensor
,
63 if (WARN_ON(limit
>= SMIAPP_LIMIT_LAST
))
66 return sensor
->limits
[limit
];
69 #define SMIA_LIM(sensor, limit) \
70 smiapp_get_limit(sensor, SMIAPP_LIMIT_##limit)
72 static int smiapp_read_all_smia_limits(struct smiapp_sensor
*sensor
)
74 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
78 for (i
= 0; i
< SMIAPP_LIMIT_LAST
; i
++) {
82 sensor
, smiapp_reg_limits
[i
].addr
, &val
);
86 sensor
->limits
[i
] = val
;
88 dev_dbg(&client
->dev
, "0x%8.8x \"%s\" = %u, 0x%x\n",
89 smiapp_reg_limits
[i
].addr
,
90 smiapp_reg_limits
[i
].what
, val
, val
);
93 if (SMIA_LIM(sensor
, SCALER_N_MIN
) == 0)
94 smiapp_replace_limit(sensor
, SMIAPP_LIMIT_SCALER_N_MIN
, 16);
99 static int smiapp_read_frame_fmt(struct smiapp_sensor
*sensor
)
101 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
102 u32 fmt_model_type
, fmt_model_subtype
, ncol_desc
, nrow_desc
;
108 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE
,
113 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE
,
118 ncol_desc
= (fmt_model_subtype
119 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK
)
120 >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT
;
121 nrow_desc
= fmt_model_subtype
122 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK
;
124 dev_dbg(&client
->dev
, "format_model_type %s\n",
125 fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
127 fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
128 ? "4 byte" : "is simply bad");
130 for (i
= 0; i
< ncol_desc
+ nrow_desc
; i
++) {
138 if (fmt_model_type
== SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
) {
139 reg
= SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i
);
140 rval
= smiapp_read(sensor
, reg
, &desc
);
146 & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK
)
147 >> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT
;
148 pixels
= desc
& SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK
;
149 } else if (fmt_model_type
150 == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
) {
151 reg
= SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i
);
152 rval
= smiapp_read(sensor
, reg
, &desc
);
158 & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK
)
159 >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT
;
160 pixels
= desc
& SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK
;
162 dev_dbg(&client
->dev
,
163 "invalid frame format model type %d\n",
174 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED
:
177 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY
:
180 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK
:
183 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK
:
186 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
:
194 dev_dbg(&client
->dev
,
195 "0x%8.8x %s pixels: %d %s (pixelcode %u)\n", reg
,
196 what
, pixels
, which
, pixelcode
);
200 SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
)
201 sensor
->visible_pixel_start
= pixel_count
;
202 pixel_count
+= pixels
;
206 /* Handle row descriptors */
208 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED
:
209 if (sensor
->embedded_end
)
211 sensor
->embedded_start
= line_count
;
212 sensor
->embedded_end
= line_count
+ pixels
;
214 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
:
215 sensor
->image_start
= line_count
;
218 line_count
+= pixels
;
221 if (sensor
->embedded_end
> sensor
->image_start
) {
222 dev_dbg(&client
->dev
,
223 "adjusting image start line to %u (was %u)\n",
224 sensor
->embedded_end
, sensor
->image_start
);
225 sensor
->image_start
= sensor
->embedded_end
;
228 dev_dbg(&client
->dev
, "embedded data from lines %d to %d\n",
229 sensor
->embedded_start
, sensor
->embedded_end
);
230 dev_dbg(&client
->dev
, "image data starts at line %d\n",
231 sensor
->image_start
);
236 static int smiapp_pll_configure(struct smiapp_sensor
*sensor
)
238 struct smiapp_pll
*pll
= &sensor
->pll
;
242 sensor
, SMIAPP_REG_U16_VT_PIX_CLK_DIV
, pll
->vt
.pix_clk_div
);
247 sensor
, SMIAPP_REG_U16_VT_SYS_CLK_DIV
, pll
->vt
.sys_clk_div
);
252 sensor
, SMIAPP_REG_U16_PRE_PLL_CLK_DIV
, pll
->pre_pll_clk_div
);
257 sensor
, SMIAPP_REG_U16_PLL_MULTIPLIER
, pll
->pll_multiplier
);
261 /* Lane op clock ratio does not apply here. */
263 sensor
, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS
,
264 DIV_ROUND_UP(pll
->op
.sys_clk_freq_hz
, 1000000 / 256 / 256));
265 if (rval
< 0 || sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
269 sensor
, SMIAPP_REG_U16_OP_PIX_CLK_DIV
, pll
->op
.pix_clk_div
);
274 sensor
, SMIAPP_REG_U16_OP_SYS_CLK_DIV
, pll
->op
.sys_clk_div
);
277 static int smiapp_pll_try(struct smiapp_sensor
*sensor
,
278 struct smiapp_pll
*pll
)
280 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
281 struct smiapp_pll_limits lim
= {
282 .min_pre_pll_clk_div
= SMIA_LIM(sensor
, MIN_PRE_PLL_CLK_DIV
),
283 .max_pre_pll_clk_div
= SMIA_LIM(sensor
, MAX_PRE_PLL_CLK_DIV
),
284 .min_pll_ip_freq_hz
= SMIA_LIM(sensor
, MIN_PLL_IP_FREQ_HZ
),
285 .max_pll_ip_freq_hz
= SMIA_LIM(sensor
, MAX_PLL_IP_FREQ_HZ
),
286 .min_pll_multiplier
= SMIA_LIM(sensor
, MIN_PLL_MULTIPLIER
),
287 .max_pll_multiplier
= SMIA_LIM(sensor
, MAX_PLL_MULTIPLIER
),
288 .min_pll_op_freq_hz
= SMIA_LIM(sensor
, MIN_PLL_OP_FREQ_HZ
),
289 .max_pll_op_freq_hz
= SMIA_LIM(sensor
, MAX_PLL_OP_FREQ_HZ
),
291 .op
.min_sys_clk_div
= SMIA_LIM(sensor
, MIN_OP_SYS_CLK_DIV
),
292 .op
.max_sys_clk_div
= SMIA_LIM(sensor
, MAX_OP_SYS_CLK_DIV
),
293 .op
.min_pix_clk_div
= SMIA_LIM(sensor
, MIN_OP_PIX_CLK_DIV
),
294 .op
.max_pix_clk_div
= SMIA_LIM(sensor
, MAX_OP_PIX_CLK_DIV
),
295 .op
.min_sys_clk_freq_hz
= SMIA_LIM(sensor
, MIN_OP_SYS_CLK_FREQ_HZ
),
296 .op
.max_sys_clk_freq_hz
= SMIA_LIM(sensor
, MAX_OP_SYS_CLK_FREQ_HZ
),
297 .op
.min_pix_clk_freq_hz
= SMIA_LIM(sensor
, MIN_OP_PIX_CLK_FREQ_HZ
),
298 .op
.max_pix_clk_freq_hz
= SMIA_LIM(sensor
, MAX_OP_PIX_CLK_FREQ_HZ
),
300 .vt
.min_sys_clk_div
= SMIA_LIM(sensor
, MIN_VT_SYS_CLK_DIV
),
301 .vt
.max_sys_clk_div
= SMIA_LIM(sensor
, MAX_VT_SYS_CLK_DIV
),
302 .vt
.min_pix_clk_div
= SMIA_LIM(sensor
, MIN_VT_PIX_CLK_DIV
),
303 .vt
.max_pix_clk_div
= SMIA_LIM(sensor
, MAX_VT_PIX_CLK_DIV
),
304 .vt
.min_sys_clk_freq_hz
= SMIA_LIM(sensor
, MIN_VT_SYS_CLK_FREQ_HZ
),
305 .vt
.max_sys_clk_freq_hz
= SMIA_LIM(sensor
, MAX_VT_SYS_CLK_FREQ_HZ
),
306 .vt
.min_pix_clk_freq_hz
= SMIA_LIM(sensor
, MIN_VT_PIX_CLK_FREQ_HZ
),
307 .vt
.max_pix_clk_freq_hz
= SMIA_LIM(sensor
, MAX_VT_PIX_CLK_FREQ_HZ
),
309 .min_line_length_pck_bin
= SMIA_LIM(sensor
, MIN_LINE_LENGTH_PCK_BIN
),
310 .min_line_length_pck
= SMIA_LIM(sensor
, MIN_LINE_LENGTH_PCK
),
313 return smiapp_pll_calculate(&client
->dev
, &lim
, pll
);
316 static int smiapp_pll_update(struct smiapp_sensor
*sensor
)
318 struct smiapp_pll
*pll
= &sensor
->pll
;
321 pll
->binning_horizontal
= sensor
->binning_horizontal
;
322 pll
->binning_vertical
= sensor
->binning_vertical
;
324 sensor
->link_freq
->qmenu_int
[sensor
->link_freq
->val
];
325 pll
->scale_m
= sensor
->scale_m
;
326 pll
->bits_per_pixel
= sensor
->csi_format
->compressed
;
328 rval
= smiapp_pll_try(sensor
, pll
);
332 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_parray
,
333 pll
->pixel_rate_pixel_array
);
334 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_csi
, pll
->pixel_rate_csi
);
342 * V4L2 Controls handling
346 static void __smiapp_update_exposure_limits(struct smiapp_sensor
*sensor
)
348 struct v4l2_ctrl
*ctrl
= sensor
->exposure
;
351 max
= sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
352 + sensor
->vblank
->val
353 - SMIA_LIM(sensor
, COARSE_INTEGRATION_TIME_MAX_MARGIN
);
355 __v4l2_ctrl_modify_range(ctrl
, ctrl
->minimum
, max
, ctrl
->step
, max
);
361 * 1. Bits-per-pixel, descending.
362 * 2. Bits-per-pixel compressed, descending.
363 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
364 * orders must be defined.
366 static const struct smiapp_csi_data_format smiapp_csi_data_formats
[] = {
367 { MEDIA_BUS_FMT_SGRBG16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_GRBG
, },
368 { MEDIA_BUS_FMT_SRGGB16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_RGGB
, },
369 { MEDIA_BUS_FMT_SBGGR16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_BGGR
, },
370 { MEDIA_BUS_FMT_SGBRG16_1X16
, 16, 16, SMIAPP_PIXEL_ORDER_GBRG
, },
371 { MEDIA_BUS_FMT_SGRBG14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_GRBG
, },
372 { MEDIA_BUS_FMT_SRGGB14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_RGGB
, },
373 { MEDIA_BUS_FMT_SBGGR14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_BGGR
, },
374 { MEDIA_BUS_FMT_SGBRG14_1X14
, 14, 14, SMIAPP_PIXEL_ORDER_GBRG
, },
375 { MEDIA_BUS_FMT_SGRBG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GRBG
, },
376 { MEDIA_BUS_FMT_SRGGB12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_RGGB
, },
377 { MEDIA_BUS_FMT_SBGGR12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_BGGR
, },
378 { MEDIA_BUS_FMT_SGBRG12_1X12
, 12, 12, SMIAPP_PIXEL_ORDER_GBRG
, },
379 { MEDIA_BUS_FMT_SGRBG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GRBG
, },
380 { MEDIA_BUS_FMT_SRGGB10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_RGGB
, },
381 { MEDIA_BUS_FMT_SBGGR10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_BGGR
, },
382 { MEDIA_BUS_FMT_SGBRG10_1X10
, 10, 10, SMIAPP_PIXEL_ORDER_GBRG
, },
383 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
384 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
385 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
386 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8
, 10, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
387 { MEDIA_BUS_FMT_SGRBG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GRBG
, },
388 { MEDIA_BUS_FMT_SRGGB8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_RGGB
, },
389 { MEDIA_BUS_FMT_SBGGR8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_BGGR
, },
390 { MEDIA_BUS_FMT_SGBRG8_1X8
, 8, 8, SMIAPP_PIXEL_ORDER_GBRG
, },
393 static const char *pixel_order_str
[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
395 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
396 - (unsigned long)smiapp_csi_data_formats) \
397 / sizeof(*smiapp_csi_data_formats))
399 static u32
smiapp_pixel_order(struct smiapp_sensor
*sensor
)
401 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
405 if (sensor
->hflip
->val
)
406 flip
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
408 if (sensor
->vflip
->val
)
409 flip
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
412 flip
^= sensor
->hvflip_inv_mask
;
414 dev_dbg(&client
->dev
, "flip %d\n", flip
);
415 return sensor
->default_pixel_order
^ flip
;
418 static void smiapp_update_mbus_formats(struct smiapp_sensor
*sensor
)
420 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
421 unsigned int csi_format_idx
=
422 to_csi_format_idx(sensor
->csi_format
) & ~3;
423 unsigned int internal_csi_format_idx
=
424 to_csi_format_idx(sensor
->internal_csi_format
) & ~3;
425 unsigned int pixel_order
= smiapp_pixel_order(sensor
);
427 sensor
->mbus_frame_fmts
=
428 sensor
->default_mbus_frame_fmts
<< pixel_order
;
430 &smiapp_csi_data_formats
[csi_format_idx
+ pixel_order
];
431 sensor
->internal_csi_format
=
432 &smiapp_csi_data_formats
[internal_csi_format_idx
435 BUG_ON(max(internal_csi_format_idx
, csi_format_idx
) + pixel_order
436 >= ARRAY_SIZE(smiapp_csi_data_formats
));
438 dev_dbg(&client
->dev
, "new pixel order %s\n",
439 pixel_order_str
[pixel_order
]);
442 static const char * const smiapp_test_patterns
[] = {
445 "Eight Vertical Colour Bars",
446 "Colour Bars With Fade to Grey",
447 "Pseudorandom Sequence (PN9)",
450 static int smiapp_set_ctrl(struct v4l2_ctrl
*ctrl
)
452 struct smiapp_sensor
*sensor
=
453 container_of(ctrl
->handler
, struct smiapp_subdev
, ctrl_handler
)
455 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
465 if (sensor
->streaming
)
468 if (sensor
->hflip
->val
)
469 orient
|= SMIAPP_IMAGE_ORIENTATION_HFLIP
;
471 if (sensor
->vflip
->val
)
472 orient
|= SMIAPP_IMAGE_ORIENTATION_VFLIP
;
474 orient
^= sensor
->hvflip_inv_mask
;
476 smiapp_update_mbus_formats(sensor
);
479 case V4L2_CID_VBLANK
:
480 exposure
= sensor
->exposure
->val
;
482 __smiapp_update_exposure_limits(sensor
);
484 if (exposure
> sensor
->exposure
->maximum
) {
485 sensor
->exposure
->val
= sensor
->exposure
->maximum
;
486 rval
= smiapp_set_ctrl(sensor
->exposure
);
492 case V4L2_CID_LINK_FREQ
:
493 if (sensor
->streaming
)
496 rval
= smiapp_pll_update(sensor
);
501 case V4L2_CID_TEST_PATTERN
:
502 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
504 sensor
->test_data
[i
],
506 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR
);
511 pm_runtime_get_noresume(&client
->dev
);
512 pm_status
= pm_runtime_get_if_in_use(&client
->dev
);
513 pm_runtime_put_noidle(&client
->dev
);
518 case V4L2_CID_ANALOGUE_GAIN
:
521 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL
, ctrl
->val
);
524 case V4L2_CID_EXPOSURE
:
527 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME
, ctrl
->val
);
532 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_IMAGE_ORIENTATION
,
536 case V4L2_CID_VBLANK
:
538 sensor
, SMIAPP_REG_U16_FRAME_LENGTH_LINES
,
539 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
543 case V4L2_CID_HBLANK
:
545 sensor
, SMIAPP_REG_U16_LINE_LENGTH_PCK
,
546 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
550 case V4L2_CID_TEST_PATTERN
:
552 sensor
, SMIAPP_REG_U16_TEST_PATTERN_MODE
, ctrl
->val
);
555 case V4L2_CID_TEST_PATTERN_RED
:
557 sensor
, SMIAPP_REG_U16_TEST_DATA_RED
, ctrl
->val
);
560 case V4L2_CID_TEST_PATTERN_GREENR
:
562 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENR
, ctrl
->val
);
565 case V4L2_CID_TEST_PATTERN_BLUE
:
567 sensor
, SMIAPP_REG_U16_TEST_DATA_BLUE
, ctrl
->val
);
570 case V4L2_CID_TEST_PATTERN_GREENB
:
572 sensor
, SMIAPP_REG_U16_TEST_DATA_GREENB
, ctrl
->val
);
575 case V4L2_CID_PIXEL_RATE
:
576 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
585 pm_runtime_mark_last_busy(&client
->dev
);
586 pm_runtime_put_autosuspend(&client
->dev
);
592 static const struct v4l2_ctrl_ops smiapp_ctrl_ops
= {
593 .s_ctrl
= smiapp_set_ctrl
,
596 static int smiapp_init_controls(struct smiapp_sensor
*sensor
)
598 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
601 rval
= v4l2_ctrl_handler_init(&sensor
->pixel_array
->ctrl_handler
, 12);
605 sensor
->pixel_array
->ctrl_handler
.lock
= &sensor
->mutex
;
607 sensor
->analog_gain
= v4l2_ctrl_new_std(
608 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
609 V4L2_CID_ANALOGUE_GAIN
,
610 SMIA_LIM(sensor
, ANALOGUE_GAIN_CODE_MIN
),
611 SMIA_LIM(sensor
, ANALOGUE_GAIN_CODE_MAX
),
612 max(SMIA_LIM(sensor
, ANALOGUE_GAIN_CODE_STEP
), 1U),
613 SMIA_LIM(sensor
, ANALOGUE_GAIN_CODE_MIN
));
615 /* Exposure limits will be updated soon, use just something here. */
616 sensor
->exposure
= v4l2_ctrl_new_std(
617 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
618 V4L2_CID_EXPOSURE
, 0, 0, 1, 0);
620 sensor
->hflip
= v4l2_ctrl_new_std(
621 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
622 V4L2_CID_HFLIP
, 0, 1, 1, 0);
623 sensor
->vflip
= v4l2_ctrl_new_std(
624 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
625 V4L2_CID_VFLIP
, 0, 1, 1, 0);
627 sensor
->vblank
= v4l2_ctrl_new_std(
628 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
629 V4L2_CID_VBLANK
, 0, 1, 1, 0);
632 sensor
->vblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
634 sensor
->hblank
= v4l2_ctrl_new_std(
635 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
636 V4L2_CID_HBLANK
, 0, 1, 1, 0);
639 sensor
->hblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
641 sensor
->pixel_rate_parray
= v4l2_ctrl_new_std(
642 &sensor
->pixel_array
->ctrl_handler
, &smiapp_ctrl_ops
,
643 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
645 v4l2_ctrl_new_std_menu_items(&sensor
->pixel_array
->ctrl_handler
,
646 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
647 ARRAY_SIZE(smiapp_test_patterns
) - 1,
648 0, 0, smiapp_test_patterns
);
650 if (sensor
->pixel_array
->ctrl_handler
.error
) {
651 dev_err(&client
->dev
,
652 "pixel array controls initialization failed (%d)\n",
653 sensor
->pixel_array
->ctrl_handler
.error
);
654 return sensor
->pixel_array
->ctrl_handler
.error
;
657 sensor
->pixel_array
->sd
.ctrl_handler
=
658 &sensor
->pixel_array
->ctrl_handler
;
660 v4l2_ctrl_cluster(2, &sensor
->hflip
);
662 rval
= v4l2_ctrl_handler_init(&sensor
->src
->ctrl_handler
, 0);
666 sensor
->src
->ctrl_handler
.lock
= &sensor
->mutex
;
668 sensor
->pixel_rate_csi
= v4l2_ctrl_new_std(
669 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
670 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
672 if (sensor
->src
->ctrl_handler
.error
) {
673 dev_err(&client
->dev
,
674 "src controls initialization failed (%d)\n",
675 sensor
->src
->ctrl_handler
.error
);
676 return sensor
->src
->ctrl_handler
.error
;
679 sensor
->src
->sd
.ctrl_handler
= &sensor
->src
->ctrl_handler
;
685 * For controls that require information on available media bus codes
686 * and linke frequencies.
688 static int smiapp_init_late_controls(struct smiapp_sensor
*sensor
)
690 unsigned long *valid_link_freqs
= &sensor
->valid_link_freqs
[
691 sensor
->csi_format
->compressed
- sensor
->compressed_min_bpp
];
694 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++) {
695 int max_value
= (1 << sensor
->csi_format
->width
) - 1;
697 sensor
->test_data
[i
] = v4l2_ctrl_new_std(
698 &sensor
->pixel_array
->ctrl_handler
,
699 &smiapp_ctrl_ops
, V4L2_CID_TEST_PATTERN_RED
+ i
,
700 0, max_value
, 1, max_value
);
703 sensor
->link_freq
= v4l2_ctrl_new_int_menu(
704 &sensor
->src
->ctrl_handler
, &smiapp_ctrl_ops
,
705 V4L2_CID_LINK_FREQ
, __fls(*valid_link_freqs
),
706 __ffs(*valid_link_freqs
), sensor
->hwcfg
->op_sys_clock
);
708 return sensor
->src
->ctrl_handler
.error
;
711 static void smiapp_free_controls(struct smiapp_sensor
*sensor
)
715 for (i
= 0; i
< sensor
->ssds_used
; i
++)
716 v4l2_ctrl_handler_free(&sensor
->ssds
[i
].ctrl_handler
);
719 static int smiapp_get_mbus_formats(struct smiapp_sensor
*sensor
)
721 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
722 struct smiapp_pll
*pll
= &sensor
->pll
;
723 u8 compressed_max_bpp
= 0;
724 unsigned int type
, n
;
725 unsigned int i
, pixel_order
;
729 sensor
, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE
, &type
);
733 dev_dbg(&client
->dev
, "data_format_model_type %d\n", type
);
735 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_PIXEL_ORDER
,
740 if (pixel_order
>= ARRAY_SIZE(pixel_order_str
)) {
741 dev_dbg(&client
->dev
, "bad pixel order %d\n", pixel_order
);
745 dev_dbg(&client
->dev
, "pixel order %d (%s)\n", pixel_order
,
746 pixel_order_str
[pixel_order
]);
749 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL
:
750 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N
;
752 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED
:
753 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N
;
759 sensor
->default_pixel_order
= pixel_order
;
760 sensor
->mbus_frame_fmts
= 0;
762 for (i
= 0; i
< n
; i
++) {
767 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i
), &fmt
);
771 dev_dbg(&client
->dev
, "%u: bpp %u, compressed %u\n",
772 i
, fmt
>> 8, (u8
)fmt
);
774 for (j
= 0; j
< ARRAY_SIZE(smiapp_csi_data_formats
); j
++) {
775 const struct smiapp_csi_data_format
*f
=
776 &smiapp_csi_data_formats
[j
];
778 if (f
->pixel_order
!= SMIAPP_PIXEL_ORDER_GRBG
)
781 if (f
->width
!= fmt
>> 8 || f
->compressed
!= (u8
)fmt
)
784 dev_dbg(&client
->dev
, "jolly good! %d\n", j
);
786 sensor
->default_mbus_frame_fmts
|= 1 << j
;
790 /* Figure out which BPP values can be used with which formats. */
791 pll
->binning_horizontal
= 1;
792 pll
->binning_vertical
= 1;
793 pll
->scale_m
= sensor
->scale_m
;
795 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
796 sensor
->compressed_min_bpp
=
797 min(smiapp_csi_data_formats
[i
].compressed
,
798 sensor
->compressed_min_bpp
);
800 max(smiapp_csi_data_formats
[i
].compressed
,
804 sensor
->valid_link_freqs
= devm_kcalloc(
806 compressed_max_bpp
- sensor
->compressed_min_bpp
+ 1,
807 sizeof(*sensor
->valid_link_freqs
), GFP_KERNEL
);
808 if (!sensor
->valid_link_freqs
)
811 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
812 const struct smiapp_csi_data_format
*f
=
813 &smiapp_csi_data_formats
[i
];
814 unsigned long *valid_link_freqs
=
815 &sensor
->valid_link_freqs
[
816 f
->compressed
- sensor
->compressed_min_bpp
];
819 if (!(sensor
->default_mbus_frame_fmts
& 1 << i
))
822 pll
->bits_per_pixel
= f
->compressed
;
824 for (j
= 0; sensor
->hwcfg
->op_sys_clock
[j
]; j
++) {
825 pll
->link_freq
= sensor
->hwcfg
->op_sys_clock
[j
];
827 rval
= smiapp_pll_try(sensor
, pll
);
828 dev_dbg(&client
->dev
, "link freq %u Hz, bpp %u %s\n",
829 pll
->link_freq
, pll
->bits_per_pixel
,
830 rval
? "not ok" : "ok");
834 set_bit(j
, valid_link_freqs
);
837 if (!*valid_link_freqs
) {
838 dev_info(&client
->dev
,
839 "no valid link frequencies for %u bpp\n",
841 sensor
->default_mbus_frame_fmts
&= ~BIT(i
);
845 if (!sensor
->csi_format
846 || f
->width
> sensor
->csi_format
->width
847 || (f
->width
== sensor
->csi_format
->width
848 && f
->compressed
> sensor
->csi_format
->compressed
)) {
849 sensor
->csi_format
= f
;
850 sensor
->internal_csi_format
= f
;
854 if (!sensor
->csi_format
) {
855 dev_err(&client
->dev
, "no supported mbus code found\n");
859 smiapp_update_mbus_formats(sensor
);
864 static void smiapp_update_blanking(struct smiapp_sensor
*sensor
)
866 struct v4l2_ctrl
*vblank
= sensor
->vblank
;
867 struct v4l2_ctrl
*hblank
= sensor
->hblank
;
868 uint16_t min_fll
, max_fll
, min_llp
, max_llp
, min_lbp
;
871 if (sensor
->binning_vertical
> 1 || sensor
->binning_horizontal
> 1) {
872 min_fll
= SMIA_LIM(sensor
, MIN_FRAME_LENGTH_LINES_BIN
);
873 max_fll
= SMIA_LIM(sensor
, MAX_FRAME_LENGTH_LINES_BIN
);
874 min_llp
= SMIA_LIM(sensor
, MIN_LINE_LENGTH_PCK_BIN
);
875 max_llp
= SMIA_LIM(sensor
, MAX_LINE_LENGTH_PCK_BIN
);
876 min_lbp
= SMIA_LIM(sensor
, MIN_LINE_BLANKING_PCK_BIN
);
878 min_fll
= SMIA_LIM(sensor
, MIN_FRAME_LENGTH_LINES
);
879 max_fll
= SMIA_LIM(sensor
, MAX_FRAME_LENGTH_LINES
);
880 min_llp
= SMIA_LIM(sensor
, MIN_LINE_LENGTH_PCK
);
881 max_llp
= SMIA_LIM(sensor
, MAX_LINE_LENGTH_PCK
);
882 min_lbp
= SMIA_LIM(sensor
, MIN_LINE_BLANKING_PCK
);
886 SMIA_LIM(sensor
, MIN_FRAME_BLANKING_LINES
),
888 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
);
889 max
= max_fll
- sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
;
891 __v4l2_ctrl_modify_range(vblank
, min
, max
, vblank
->step
, min
);
895 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
,
897 max
= max_llp
- sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
;
899 __v4l2_ctrl_modify_range(hblank
, min
, max
, hblank
->step
, min
);
901 __smiapp_update_exposure_limits(sensor
);
904 static int smiapp_pll_blanking_update(struct smiapp_sensor
*sensor
)
906 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
909 rval
= smiapp_pll_update(sensor
);
913 /* Output from pixel array, including blanking */
914 smiapp_update_blanking(sensor
);
916 dev_dbg(&client
->dev
, "vblank\t\t%d\n", sensor
->vblank
->val
);
917 dev_dbg(&client
->dev
, "hblank\t\t%d\n", sensor
->hblank
->val
);
919 dev_dbg(&client
->dev
, "real timeperframe\t100/%d\n",
920 sensor
->pll
.pixel_rate_pixel_array
/
921 ((sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
922 + sensor
->hblank
->val
) *
923 (sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
924 + sensor
->vblank
->val
) / 100));
931 * SMIA++ NVM handling
935 static int smiapp_read_nvm_page(struct smiapp_sensor
*sensor
, u32 p
, u8
*nvm
,
944 rval
= smiapp_write(sensor
,
945 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT
, p
);
949 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
,
950 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN
);
954 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS
,
959 if (s
& SMIAPP_DATA_TRANSFER_IF_1_STATUS_EUSAGE
) {
964 if (SMIA_LIM(sensor
, DATA_TRANSFER_IF_CAPABILITY
) &
965 SMIAPP_DATA_TRANSFER_IF_CAPABILITY_POLL
) {
966 for (i
= 1000; i
> 0; i
--) {
967 if (s
& SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY
)
972 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS
,
983 for (i
= 0; i
< SMIAPP_NVM_PAGE_SIZE
; i
++) {
986 rval
= smiapp_read(sensor
,
987 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0
+ i
,
998 static int smiapp_read_nvm(struct smiapp_sensor
*sensor
, unsigned char *nvm
,
1003 int rval
= 0, rval2
;
1005 for (p
= 0; p
< nvm_size
/ SMIAPP_NVM_PAGE_SIZE
&& !rval
; p
++) {
1006 rval
= smiapp_read_nvm_page(sensor
, p
, nvm
, &status
);
1007 nvm
+= SMIAPP_NVM_PAGE_SIZE
;
1010 if (rval
== -ENODATA
&&
1011 status
& SMIAPP_DATA_TRANSFER_IF_1_STATUS_EUSAGE
)
1014 rval2
= smiapp_write(sensor
, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL
, 0);
1018 return rval2
?: p
* SMIAPP_NVM_PAGE_SIZE
;
1023 * SMIA++ CCI address control
1026 static int smiapp_change_cci_addr(struct smiapp_sensor
*sensor
)
1028 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1032 client
->addr
= sensor
->hwcfg
->i2c_addr_dfl
;
1034 rval
= smiapp_write(sensor
,
1035 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
,
1036 sensor
->hwcfg
->i2c_addr_alt
<< 1);
1040 client
->addr
= sensor
->hwcfg
->i2c_addr_alt
;
1042 /* verify addr change went ok */
1043 rval
= smiapp_read(sensor
, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL
, &val
);
1047 if (val
!= sensor
->hwcfg
->i2c_addr_alt
<< 1)
1055 * SMIA++ Mode Control
1058 static int smiapp_setup_flash_strobe(struct smiapp_sensor
*sensor
)
1060 struct smiapp_flash_strobe_parms
*strobe_setup
;
1061 unsigned int ext_freq
= sensor
->hwcfg
->ext_clk
;
1063 u32 strobe_adjustment
;
1064 u32 strobe_width_high_rs
;
1067 strobe_setup
= sensor
->hwcfg
->strobe_setup
;
1070 * How to calculate registers related to strobe length. Please
1071 * do not change, or if you do at least know what you're
1074 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1076 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1077 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1079 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1080 * flash_strobe_adjustment E N, [1 - 0xff]
1082 * The formula above is written as below to keep it on one
1085 * l / 10^6 = w / e * a
1087 * Let's mark w * a by x:
1095 * The strobe width must be at least as long as requested,
1096 * thus rounding upwards is needed.
1098 * x = (l * e + 10^6 - 1) / 10^6
1099 * -----------------------------
1101 * Maximum possible accuracy is wanted at all times. Thus keep
1102 * a as small as possible.
1104 * Calculate a, assuming maximum w, with rounding upwards:
1106 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1107 * -------------------------------------
1109 * Thus, we also get w, with that a, with rounding upwards:
1111 * w = (x + a - 1) / a
1112 * -------------------
1116 * x E [1, (2^16 - 1) * (2^8 - 1)]
1118 * Substituting maximum x to the original formula (with rounding),
1119 * the maximum l is thus
1121 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1123 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1124 * --------------------------------------------------
1126 * flash_strobe_length must be clamped between 1 and
1127 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1131 * flash_strobe_adjustment = ((flash_strobe_length *
1132 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1134 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1135 * EXTCLK freq + 10^6 - 1) / 10^6 +
1136 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1138 tmp
= div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1139 1000000 + 1, ext_freq
);
1140 strobe_setup
->strobe_width_high_us
=
1141 clamp_t(u32
, strobe_setup
->strobe_width_high_us
, 1, tmp
);
1143 tmp
= div_u64(((u64
)strobe_setup
->strobe_width_high_us
* (u64
)ext_freq
+
1144 1000000 - 1), 1000000ULL);
1145 strobe_adjustment
= (tmp
+ (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1146 strobe_width_high_rs
= (tmp
+ strobe_adjustment
- 1) /
1149 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_MODE_RS
,
1150 strobe_setup
->mode
);
1154 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT
,
1159 rval
= smiapp_write(
1160 sensor
, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL
,
1161 strobe_width_high_rs
);
1165 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL
,
1166 strobe_setup
->strobe_delay
);
1170 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_FLASH_STROBE_START_POINT
,
1171 strobe_setup
->stobe_start_point
);
1175 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FLASH_TRIGGER_RS
,
1176 strobe_setup
->trigger
);
1179 sensor
->hwcfg
->strobe_setup
->trigger
= 0;
1184 /* -----------------------------------------------------------------------------
1188 static int smiapp_power_on(struct device
*dev
)
1190 struct i2c_client
*client
= to_i2c_client(dev
);
1191 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
1192 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1194 * The sub-device related to the I2C device is always the
1195 * source one, i.e. ssds[0].
1197 struct smiapp_sensor
*sensor
=
1198 container_of(ssd
, struct smiapp_sensor
, ssds
[0]);
1202 rval
= regulator_enable(sensor
->vana
);
1204 dev_err(&client
->dev
, "failed to enable vana regulator\n");
1207 usleep_range(1000, 1000);
1209 rval
= clk_prepare_enable(sensor
->ext_clk
);
1211 dev_dbg(&client
->dev
, "failed to enable xclk\n");
1214 usleep_range(1000, 1000);
1216 gpiod_set_value(sensor
->xshutdown
, 1);
1218 sleep
= SMIAPP_RESET_DELAY(sensor
->hwcfg
->ext_clk
);
1219 usleep_range(sleep
, sleep
);
1222 * Failures to respond to the address change command have been noticed.
1223 * Those failures seem to be caused by the sensor requiring a longer
1224 * boot time than advertised. An additional 10ms delay seems to work
1225 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1226 * unnecessary. The failures need to be investigated to find a proper
1227 * fix, and a delay will likely need to be added here if the I2C write
1228 * retry hack is reverted before the root cause of the boot time issue
1232 if (sensor
->hwcfg
->i2c_addr_alt
) {
1233 rval
= smiapp_change_cci_addr(sensor
);
1235 dev_err(&client
->dev
, "cci address change error\n");
1236 goto out_cci_addr_fail
;
1240 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_SOFTWARE_RESET
,
1241 SMIAPP_SOFTWARE_RESET
);
1243 dev_err(&client
->dev
, "software reset failed\n");
1244 goto out_cci_addr_fail
;
1247 if (sensor
->hwcfg
->i2c_addr_alt
) {
1248 rval
= smiapp_change_cci_addr(sensor
);
1250 dev_err(&client
->dev
, "cci address change error\n");
1251 goto out_cci_addr_fail
;
1255 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_COMPRESSION_MODE
,
1256 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR
);
1258 dev_err(&client
->dev
, "compression mode set failed\n");
1259 goto out_cci_addr_fail
;
1262 rval
= smiapp_write(
1263 sensor
, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ
,
1264 sensor
->hwcfg
->ext_clk
/ (1000000 / (1 << 8)));
1266 dev_err(&client
->dev
, "extclk frequency set failed\n");
1267 goto out_cci_addr_fail
;
1270 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_LANE_MODE
,
1271 sensor
->hwcfg
->lanes
- 1);
1273 dev_err(&client
->dev
, "csi lane mode set failed\n");
1274 goto out_cci_addr_fail
;
1277 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_FAST_STANDBY_CTRL
,
1278 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE
);
1280 dev_err(&client
->dev
, "fast standby set failed\n");
1281 goto out_cci_addr_fail
;
1284 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_CSI_SIGNALLING_MODE
,
1285 sensor
->hwcfg
->csi_signalling_mode
);
1287 dev_err(&client
->dev
, "csi signalling mode set failed\n");
1288 goto out_cci_addr_fail
;
1291 /* DPHY control done by sensor based on requested link rate */
1292 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_DPHY_CTRL
,
1293 SMIAPP_DPHY_CTRL_UI
);
1295 goto out_cci_addr_fail
;
1297 rval
= smiapp_call_quirk(sensor
, post_poweron
);
1299 dev_err(&client
->dev
, "post_poweron quirks failed\n");
1300 goto out_cci_addr_fail
;
1306 gpiod_set_value(sensor
->xshutdown
, 0);
1307 clk_disable_unprepare(sensor
->ext_clk
);
1310 regulator_disable(sensor
->vana
);
1315 static int smiapp_power_off(struct device
*dev
)
1317 struct i2c_client
*client
= to_i2c_client(dev
);
1318 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
1319 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1320 struct smiapp_sensor
*sensor
=
1321 container_of(ssd
, struct smiapp_sensor
, ssds
[0]);
1324 * Currently power/clock to lens are enable/disabled separately
1325 * but they are essentially the same signals. So if the sensor is
1326 * powered off while the lens is powered on the sensor does not
1327 * really see a power off and next time the cci address change
1328 * will fail. So do a soft reset explicitly here.
1330 if (sensor
->hwcfg
->i2c_addr_alt
)
1331 smiapp_write(sensor
,
1332 SMIAPP_REG_U8_SOFTWARE_RESET
,
1333 SMIAPP_SOFTWARE_RESET
);
1335 gpiod_set_value(sensor
->xshutdown
, 0);
1336 clk_disable_unprepare(sensor
->ext_clk
);
1337 usleep_range(5000, 5000);
1338 regulator_disable(sensor
->vana
);
1339 sensor
->streaming
= false;
1344 /* -----------------------------------------------------------------------------
1345 * Video stream management
1348 static int smiapp_start_streaming(struct smiapp_sensor
*sensor
)
1350 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1351 unsigned int binning_mode
;
1354 mutex_lock(&sensor
->mutex
);
1356 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_CSI_DATA_FORMAT
,
1357 (sensor
->csi_format
->width
<< 8) |
1358 sensor
->csi_format
->compressed
);
1362 /* Binning configuration */
1363 if (sensor
->binning_horizontal
== 1 &&
1364 sensor
->binning_vertical
== 1) {
1368 (sensor
->binning_horizontal
<< 4)
1369 | sensor
->binning_vertical
;
1371 rval
= smiapp_write(
1372 sensor
, SMIAPP_REG_U8_BINNING_TYPE
, binning_type
);
1378 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_BINNING_MODE
, binning_mode
);
1383 rval
= smiapp_pll_configure(sensor
);
1387 /* Analog crop start coordinates */
1388 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_ADDR_START
,
1389 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
);
1393 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_ADDR_START
,
1394 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
);
1398 /* Analog crop end coordinates */
1399 rval
= smiapp_write(
1400 sensor
, SMIAPP_REG_U16_X_ADDR_END
,
1401 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].left
1402 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].width
- 1);
1406 rval
= smiapp_write(
1407 sensor
, SMIAPP_REG_U16_Y_ADDR_END
,
1408 sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].top
1409 + sensor
->pixel_array
->crop
[SMIAPP_PA_PAD_SRC
].height
- 1);
1414 * Output from pixel array, including blanking, is set using
1415 * controls below. No need to set here.
1419 if (SMIA_LIM(sensor
, DIGITAL_CROP_CAPABILITY
)
1420 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
1421 rval
= smiapp_write(
1422 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET
,
1423 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].left
);
1427 rval
= smiapp_write(
1428 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET
,
1429 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].top
);
1433 rval
= smiapp_write(
1434 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH
,
1435 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].width
);
1439 rval
= smiapp_write(
1440 sensor
, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT
,
1441 sensor
->scaler
->crop
[SMIAPP_PAD_SINK
].height
);
1447 if (SMIA_LIM(sensor
, SCALING_CAPABILITY
)
1448 != SMIAPP_SCALING_CAPABILITY_NONE
) {
1449 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALING_MODE
,
1450 sensor
->scaling_mode
);
1454 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_SCALE_M
,
1460 /* Output size from sensor */
1461 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_X_OUTPUT_SIZE
,
1462 sensor
->src
->crop
[SMIAPP_PAD_SRC
].width
);
1465 rval
= smiapp_write(sensor
, SMIAPP_REG_U16_Y_OUTPUT_SIZE
,
1466 sensor
->src
->crop
[SMIAPP_PAD_SRC
].height
);
1470 if ((SMIA_LIM(sensor
, FLASH_MODE_CAPABILITY
) &
1471 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE
|
1472 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE
)) &&
1473 sensor
->hwcfg
->strobe_setup
!= NULL
&&
1474 sensor
->hwcfg
->strobe_setup
->trigger
!= 0) {
1475 rval
= smiapp_setup_flash_strobe(sensor
);
1480 rval
= smiapp_call_quirk(sensor
, pre_streamon
);
1482 dev_err(&client
->dev
, "pre_streamon quirks failed\n");
1486 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1487 SMIAPP_MODE_SELECT_STREAMING
);
1490 mutex_unlock(&sensor
->mutex
);
1495 static int smiapp_stop_streaming(struct smiapp_sensor
*sensor
)
1497 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1500 mutex_lock(&sensor
->mutex
);
1501 rval
= smiapp_write(sensor
, SMIAPP_REG_U8_MODE_SELECT
,
1502 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY
);
1506 rval
= smiapp_call_quirk(sensor
, post_streamoff
);
1508 dev_err(&client
->dev
, "post_streamoff quirks failed\n");
1511 mutex_unlock(&sensor
->mutex
);
1515 /* -----------------------------------------------------------------------------
1516 * V4L2 subdev video operations
1519 static int smiapp_pm_get_init(struct smiapp_sensor
*sensor
)
1521 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1524 rval
= pm_runtime_get_sync(&client
->dev
);
1526 if (rval
!= -EBUSY
&& rval
!= -EAGAIN
)
1527 pm_runtime_set_active(&client
->dev
);
1528 pm_runtime_put_noidle(&client
->dev
);
1532 rval
= v4l2_ctrl_handler_setup(&sensor
->pixel_array
->
1537 return v4l2_ctrl_handler_setup(&sensor
->src
->ctrl_handler
);
1543 static int smiapp_set_stream(struct v4l2_subdev
*subdev
, int enable
)
1545 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1546 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1549 if (sensor
->streaming
== enable
)
1553 smiapp_stop_streaming(sensor
);
1554 sensor
->streaming
= false;
1555 pm_runtime_mark_last_busy(&client
->dev
);
1556 pm_runtime_put_autosuspend(&client
->dev
);
1561 rval
= smiapp_pm_get_init(sensor
);
1565 sensor
->streaming
= true;
1567 rval
= smiapp_start_streaming(sensor
);
1569 sensor
->streaming
= false;
1570 pm_runtime_mark_last_busy(&client
->dev
);
1571 pm_runtime_put_autosuspend(&client
->dev
);
1577 static int smiapp_enum_mbus_code(struct v4l2_subdev
*subdev
,
1578 struct v4l2_subdev_pad_config
*cfg
,
1579 struct v4l2_subdev_mbus_code_enum
*code
)
1581 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1582 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1587 mutex_lock(&sensor
->mutex
);
1589 dev_err(&client
->dev
, "subdev %s, pad %d, index %d\n",
1590 subdev
->name
, code
->pad
, code
->index
);
1592 if (subdev
!= &sensor
->src
->sd
|| code
->pad
!= SMIAPP_PAD_SRC
) {
1596 code
->code
= sensor
->internal_csi_format
->code
;
1601 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1602 if (sensor
->mbus_frame_fmts
& (1 << i
))
1605 if (idx
== code
->index
) {
1606 code
->code
= smiapp_csi_data_formats
[i
].code
;
1607 dev_err(&client
->dev
, "found index %d, i %d, code %x\n",
1608 code
->index
, i
, code
->code
);
1615 mutex_unlock(&sensor
->mutex
);
1620 static u32
__smiapp_get_mbus_code(struct v4l2_subdev
*subdev
,
1623 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1625 if (subdev
== &sensor
->src
->sd
&& pad
== SMIAPP_PAD_SRC
)
1626 return sensor
->csi_format
->code
;
1628 return sensor
->internal_csi_format
->code
;
1631 static int __smiapp_get_format(struct v4l2_subdev
*subdev
,
1632 struct v4l2_subdev_pad_config
*cfg
,
1633 struct v4l2_subdev_format
*fmt
)
1635 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1637 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1638 fmt
->format
= *v4l2_subdev_get_try_format(subdev
, cfg
,
1641 struct v4l2_rect
*r
;
1643 if (fmt
->pad
== ssd
->source_pad
)
1644 r
= &ssd
->crop
[ssd
->source_pad
];
1648 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1649 fmt
->format
.width
= r
->width
;
1650 fmt
->format
.height
= r
->height
;
1651 fmt
->format
.field
= V4L2_FIELD_NONE
;
1657 static int smiapp_get_format(struct v4l2_subdev
*subdev
,
1658 struct v4l2_subdev_pad_config
*cfg
,
1659 struct v4l2_subdev_format
*fmt
)
1661 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1664 mutex_lock(&sensor
->mutex
);
1665 rval
= __smiapp_get_format(subdev
, cfg
, fmt
);
1666 mutex_unlock(&sensor
->mutex
);
1671 static void smiapp_get_crop_compose(struct v4l2_subdev
*subdev
,
1672 struct v4l2_subdev_pad_config
*cfg
,
1673 struct v4l2_rect
**crops
,
1674 struct v4l2_rect
**comps
, int which
)
1676 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1679 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1681 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++)
1682 crops
[i
] = &ssd
->crop
[i
];
1684 *comps
= &ssd
->compose
;
1687 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++) {
1688 crops
[i
] = v4l2_subdev_get_try_crop(subdev
, cfg
, i
);
1693 *comps
= v4l2_subdev_get_try_compose(subdev
, cfg
,
1700 /* Changes require propagation only on sink pad. */
1701 static void smiapp_propagate(struct v4l2_subdev
*subdev
,
1702 struct v4l2_subdev_pad_config
*cfg
, int which
,
1705 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1706 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1707 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
1709 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, which
);
1712 case V4L2_SEL_TGT_CROP
:
1713 comp
->width
= crops
[SMIAPP_PAD_SINK
]->width
;
1714 comp
->height
= crops
[SMIAPP_PAD_SINK
]->height
;
1715 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1716 if (ssd
== sensor
->scaler
) {
1718 SMIA_LIM(sensor
, SCALER_N_MIN
);
1719 sensor
->scaling_mode
=
1720 SMIAPP_SCALING_MODE_NONE
;
1721 } else if (ssd
== sensor
->binner
) {
1722 sensor
->binning_horizontal
= 1;
1723 sensor
->binning_vertical
= 1;
1727 case V4L2_SEL_TGT_COMPOSE
:
1728 *crops
[SMIAPP_PAD_SRC
] = *comp
;
1735 static const struct smiapp_csi_data_format
1736 *smiapp_validate_csi_data_format(struct smiapp_sensor
*sensor
, u32 code
)
1740 for (i
= 0; i
< ARRAY_SIZE(smiapp_csi_data_formats
); i
++) {
1741 if (sensor
->mbus_frame_fmts
& (1 << i
)
1742 && smiapp_csi_data_formats
[i
].code
== code
)
1743 return &smiapp_csi_data_formats
[i
];
1746 return sensor
->csi_format
;
1749 static int smiapp_set_format_source(struct v4l2_subdev
*subdev
,
1750 struct v4l2_subdev_pad_config
*cfg
,
1751 struct v4l2_subdev_format
*fmt
)
1753 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1754 const struct smiapp_csi_data_format
*csi_format
,
1755 *old_csi_format
= sensor
->csi_format
;
1756 unsigned long *valid_link_freqs
;
1757 u32 code
= fmt
->format
.code
;
1761 rval
= __smiapp_get_format(subdev
, cfg
, fmt
);
1766 * Media bus code is changeable on src subdev's source pad. On
1767 * other source pads we just get format here.
1769 if (subdev
!= &sensor
->src
->sd
)
1772 csi_format
= smiapp_validate_csi_data_format(sensor
, code
);
1774 fmt
->format
.code
= csi_format
->code
;
1776 if (fmt
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1779 sensor
->csi_format
= csi_format
;
1781 if (csi_format
->width
!= old_csi_format
->width
)
1782 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
1783 __v4l2_ctrl_modify_range(
1784 sensor
->test_data
[i
], 0,
1785 (1 << csi_format
->width
) - 1, 1, 0);
1787 if (csi_format
->compressed
== old_csi_format
->compressed
)
1791 &sensor
->valid_link_freqs
[sensor
->csi_format
->compressed
1792 - sensor
->compressed_min_bpp
];
1794 __v4l2_ctrl_modify_range(
1795 sensor
->link_freq
, 0,
1796 __fls(*valid_link_freqs
), ~*valid_link_freqs
,
1797 __ffs(*valid_link_freqs
));
1799 return smiapp_pll_update(sensor
);
1802 static int smiapp_set_format(struct v4l2_subdev
*subdev
,
1803 struct v4l2_subdev_pad_config
*cfg
,
1804 struct v4l2_subdev_format
*fmt
)
1806 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1807 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
1808 struct v4l2_rect
*crops
[SMIAPP_PADS
];
1810 mutex_lock(&sensor
->mutex
);
1812 if (fmt
->pad
== ssd
->source_pad
) {
1815 rval
= smiapp_set_format_source(subdev
, cfg
, fmt
);
1817 mutex_unlock(&sensor
->mutex
);
1822 /* Sink pad. Width and height are changeable here. */
1823 fmt
->format
.code
= __smiapp_get_mbus_code(subdev
, fmt
->pad
);
1824 fmt
->format
.width
&= ~1;
1825 fmt
->format
.height
&= ~1;
1826 fmt
->format
.field
= V4L2_FIELD_NONE
;
1829 clamp(fmt
->format
.width
,
1830 SMIA_LIM(sensor
, MIN_X_OUTPUT_SIZE
),
1831 SMIA_LIM(sensor
, MAX_X_OUTPUT_SIZE
));
1832 fmt
->format
.height
=
1833 clamp(fmt
->format
.height
,
1834 SMIA_LIM(sensor
, MIN_Y_OUTPUT_SIZE
),
1835 SMIA_LIM(sensor
, MAX_Y_OUTPUT_SIZE
));
1837 smiapp_get_crop_compose(subdev
, cfg
, crops
, NULL
, fmt
->which
);
1839 crops
[ssd
->sink_pad
]->left
= 0;
1840 crops
[ssd
->sink_pad
]->top
= 0;
1841 crops
[ssd
->sink_pad
]->width
= fmt
->format
.width
;
1842 crops
[ssd
->sink_pad
]->height
= fmt
->format
.height
;
1843 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
1844 ssd
->sink_fmt
= *crops
[ssd
->sink_pad
];
1845 smiapp_propagate(subdev
, cfg
, fmt
->which
,
1848 mutex_unlock(&sensor
->mutex
);
1854 * Calculate goodness of scaled image size compared to expected image
1855 * size and flags provided.
1857 #define SCALING_GOODNESS 100000
1858 #define SCALING_GOODNESS_EXTREME 100000000
1859 static int scaling_goodness(struct v4l2_subdev
*subdev
, int w
, int ask_w
,
1860 int h
, int ask_h
, u32 flags
)
1862 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1863 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1871 if (flags
& V4L2_SEL_FLAG_GE
) {
1873 val
-= SCALING_GOODNESS
;
1875 val
-= SCALING_GOODNESS
;
1878 if (flags
& V4L2_SEL_FLAG_LE
) {
1880 val
-= SCALING_GOODNESS
;
1882 val
-= SCALING_GOODNESS
;
1885 val
-= abs(w
- ask_w
);
1886 val
-= abs(h
- ask_h
);
1888 if (w
< SMIA_LIM(sensor
, MIN_X_OUTPUT_SIZE
))
1889 val
-= SCALING_GOODNESS_EXTREME
;
1891 dev_dbg(&client
->dev
, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1892 w
, ask_w
, h
, ask_h
, val
);
1897 static void smiapp_set_compose_binner(struct v4l2_subdev
*subdev
,
1898 struct v4l2_subdev_pad_config
*cfg
,
1899 struct v4l2_subdev_selection
*sel
,
1900 struct v4l2_rect
**crops
,
1901 struct v4l2_rect
*comp
)
1903 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1905 unsigned int binh
= 1, binv
= 1;
1906 int best
= scaling_goodness(
1908 crops
[SMIAPP_PAD_SINK
]->width
, sel
->r
.width
,
1909 crops
[SMIAPP_PAD_SINK
]->height
, sel
->r
.height
, sel
->flags
);
1911 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
1912 int this = scaling_goodness(
1914 crops
[SMIAPP_PAD_SINK
]->width
1915 / sensor
->binning_subtypes
[i
].horizontal
,
1917 crops
[SMIAPP_PAD_SINK
]->height
1918 / sensor
->binning_subtypes
[i
].vertical
,
1919 sel
->r
.height
, sel
->flags
);
1922 binh
= sensor
->binning_subtypes
[i
].horizontal
;
1923 binv
= sensor
->binning_subtypes
[i
].vertical
;
1927 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
1928 sensor
->binning_vertical
= binv
;
1929 sensor
->binning_horizontal
= binh
;
1932 sel
->r
.width
= (crops
[SMIAPP_PAD_SINK
]->width
/ binh
) & ~1;
1933 sel
->r
.height
= (crops
[SMIAPP_PAD_SINK
]->height
/ binv
) & ~1;
1937 * Calculate best scaling ratio and mode for given output resolution.
1939 * Try all of these: horizontal ratio, vertical ratio and smallest
1940 * size possible (horizontally).
1942 * Also try whether horizontal scaler or full scaler gives a better
1945 static void smiapp_set_compose_scaler(struct v4l2_subdev
*subdev
,
1946 struct v4l2_subdev_pad_config
*cfg
,
1947 struct v4l2_subdev_selection
*sel
,
1948 struct v4l2_rect
**crops
,
1949 struct v4l2_rect
*comp
)
1951 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1952 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
1953 u32 min
, max
, a
, b
, max_m
;
1954 u32 scale_m
= SMIA_LIM(sensor
, SCALER_N_MIN
);
1955 int mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
1961 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
1962 crops
[SMIAPP_PAD_SINK
]->width
);
1963 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
1964 crops
[SMIAPP_PAD_SINK
]->height
);
1966 a
= crops
[SMIAPP_PAD_SINK
]->width
1967 * SMIA_LIM(sensor
, SCALER_N_MIN
) / sel
->r
.width
;
1968 b
= crops
[SMIAPP_PAD_SINK
]->height
1969 * SMIA_LIM(sensor
, SCALER_N_MIN
) / sel
->r
.height
;
1970 max_m
= crops
[SMIAPP_PAD_SINK
]->width
1971 * SMIA_LIM(sensor
, SCALER_N_MIN
)
1972 / SMIA_LIM(sensor
, MIN_X_OUTPUT_SIZE
);
1974 a
= clamp(a
, SMIA_LIM(sensor
, SCALER_M_MIN
),
1975 SMIA_LIM(sensor
, SCALER_M_MAX
));
1976 b
= clamp(b
, SMIA_LIM(sensor
, SCALER_M_MIN
),
1977 SMIA_LIM(sensor
, SCALER_M_MAX
));
1978 max_m
= clamp(max_m
, SMIA_LIM(sensor
, SCALER_M_MIN
),
1979 SMIA_LIM(sensor
, SCALER_M_MAX
));
1981 dev_dbg(&client
->dev
, "scaling: a %d b %d max_m %d\n", a
, b
, max_m
);
1983 min
= min(max_m
, min(a
, b
));
1984 max
= min(max_m
, max(a
, b
));
1993 try[ntry
] = min
+ 1;
1996 try[ntry
] = max
+ 1;
2001 for (i
= 0; i
< ntry
; i
++) {
2002 int this = scaling_goodness(
2004 crops
[SMIAPP_PAD_SINK
]->width
2006 * SMIA_LIM(sensor
, SCALER_N_MIN
),
2008 crops
[SMIAPP_PAD_SINK
]->height
,
2012 dev_dbg(&client
->dev
, "trying factor %d (%d)\n", try[i
], i
);
2016 mode
= SMIAPP_SCALING_MODE_HORIZONTAL
;
2020 if (SMIA_LIM(sensor
, SCALING_CAPABILITY
)
2021 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
2024 this = scaling_goodness(
2025 subdev
, crops
[SMIAPP_PAD_SINK
]->width
2027 * SMIA_LIM(sensor
, SCALER_N_MIN
),
2029 crops
[SMIAPP_PAD_SINK
]->height
2031 * SMIA_LIM(sensor
, SCALER_N_MIN
),
2037 mode
= SMIAPP_SCALING_MODE_BOTH
;
2043 (crops
[SMIAPP_PAD_SINK
]->width
2045 * SMIA_LIM(sensor
, SCALER_N_MIN
)) & ~1;
2046 if (mode
== SMIAPP_SCALING_MODE_BOTH
)
2048 (crops
[SMIAPP_PAD_SINK
]->height
2050 * SMIA_LIM(sensor
, SCALER_N_MIN
))
2053 sel
->r
.height
= crops
[SMIAPP_PAD_SINK
]->height
;
2055 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2056 sensor
->scale_m
= scale_m
;
2057 sensor
->scaling_mode
= mode
;
2060 /* We're only called on source pads. This function sets scaling. */
2061 static int smiapp_set_compose(struct v4l2_subdev
*subdev
,
2062 struct v4l2_subdev_pad_config
*cfg
,
2063 struct v4l2_subdev_selection
*sel
)
2065 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2066 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2067 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2069 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, sel
->which
);
2074 if (ssd
== sensor
->binner
)
2075 smiapp_set_compose_binner(subdev
, cfg
, sel
, crops
, comp
);
2077 smiapp_set_compose_scaler(subdev
, cfg
, sel
, crops
, comp
);
2080 smiapp_propagate(subdev
, cfg
, sel
->which
, V4L2_SEL_TGT_COMPOSE
);
2082 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
2083 return smiapp_pll_blanking_update(sensor
);
2088 static int __smiapp_sel_supported(struct v4l2_subdev
*subdev
,
2089 struct v4l2_subdev_selection
*sel
)
2091 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2092 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2094 /* We only implement crop in three places. */
2095 switch (sel
->target
) {
2096 case V4L2_SEL_TGT_CROP
:
2097 case V4L2_SEL_TGT_CROP_BOUNDS
:
2098 if (ssd
== sensor
->pixel_array
2099 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2101 if (ssd
== sensor
->src
2102 && sel
->pad
== SMIAPP_PAD_SRC
)
2104 if (ssd
== sensor
->scaler
2105 && sel
->pad
== SMIAPP_PAD_SINK
2106 && SMIA_LIM(sensor
, DIGITAL_CROP_CAPABILITY
)
2107 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
)
2110 case V4L2_SEL_TGT_NATIVE_SIZE
:
2111 if (ssd
== sensor
->pixel_array
2112 && sel
->pad
== SMIAPP_PA_PAD_SRC
)
2115 case V4L2_SEL_TGT_COMPOSE
:
2116 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2117 if (sel
->pad
== ssd
->source_pad
)
2119 if (ssd
== sensor
->binner
)
2121 if (ssd
== sensor
->scaler
2122 && SMIA_LIM(sensor
, SCALING_CAPABILITY
)
2123 != SMIAPP_SCALING_CAPABILITY_NONE
)
2131 static int smiapp_set_crop(struct v4l2_subdev
*subdev
,
2132 struct v4l2_subdev_pad_config
*cfg
,
2133 struct v4l2_subdev_selection
*sel
)
2135 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2136 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2137 struct v4l2_rect
*src_size
, *crops
[SMIAPP_PADS
];
2138 struct v4l2_rect _r
;
2140 smiapp_get_crop_compose(subdev
, cfg
, crops
, NULL
, sel
->which
);
2142 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2143 if (sel
->pad
== ssd
->sink_pad
)
2144 src_size
= &ssd
->sink_fmt
;
2146 src_size
= &ssd
->compose
;
2148 if (sel
->pad
== ssd
->sink_pad
) {
2151 _r
.width
= v4l2_subdev_get_try_format(subdev
, cfg
, sel
->pad
)
2153 _r
.height
= v4l2_subdev_get_try_format(subdev
, cfg
, sel
->pad
)
2157 src_size
= v4l2_subdev_get_try_compose(
2158 subdev
, cfg
, ssd
->sink_pad
);
2162 if (ssd
== sensor
->src
&& sel
->pad
== SMIAPP_PAD_SRC
) {
2167 sel
->r
.width
= min(sel
->r
.width
, src_size
->width
);
2168 sel
->r
.height
= min(sel
->r
.height
, src_size
->height
);
2170 sel
->r
.left
= min_t(int, sel
->r
.left
, src_size
->width
- sel
->r
.width
);
2171 sel
->r
.top
= min_t(int, sel
->r
.top
, src_size
->height
- sel
->r
.height
);
2173 *crops
[sel
->pad
] = sel
->r
;
2175 if (ssd
!= sensor
->pixel_array
&& sel
->pad
== SMIAPP_PAD_SINK
)
2176 smiapp_propagate(subdev
, cfg
, sel
->which
,
2182 static void smiapp_get_native_size(struct smiapp_subdev
*ssd
,
2183 struct v4l2_rect
*r
)
2187 r
->width
= SMIA_LIM(ssd
->sensor
, X_ADDR_MAX
) + 1;
2188 r
->height
= SMIA_LIM(ssd
->sensor
, Y_ADDR_MAX
) + 1;
2191 static int __smiapp_get_selection(struct v4l2_subdev
*subdev
,
2192 struct v4l2_subdev_pad_config
*cfg
,
2193 struct v4l2_subdev_selection
*sel
)
2195 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2196 struct smiapp_subdev
*ssd
= to_smiapp_subdev(subdev
);
2197 struct v4l2_rect
*comp
, *crops
[SMIAPP_PADS
];
2198 struct v4l2_rect sink_fmt
;
2201 ret
= __smiapp_sel_supported(subdev
, sel
);
2205 smiapp_get_crop_compose(subdev
, cfg
, crops
, &comp
, sel
->which
);
2207 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2208 sink_fmt
= ssd
->sink_fmt
;
2210 struct v4l2_mbus_framefmt
*fmt
=
2211 v4l2_subdev_get_try_format(subdev
, cfg
, ssd
->sink_pad
);
2215 sink_fmt
.width
= fmt
->width
;
2216 sink_fmt
.height
= fmt
->height
;
2219 switch (sel
->target
) {
2220 case V4L2_SEL_TGT_CROP_BOUNDS
:
2221 case V4L2_SEL_TGT_NATIVE_SIZE
:
2222 if (ssd
== sensor
->pixel_array
)
2223 smiapp_get_native_size(ssd
, &sel
->r
);
2224 else if (sel
->pad
== ssd
->sink_pad
)
2229 case V4L2_SEL_TGT_CROP
:
2230 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2231 sel
->r
= *crops
[sel
->pad
];
2233 case V4L2_SEL_TGT_COMPOSE
:
2241 static int smiapp_get_selection(struct v4l2_subdev
*subdev
,
2242 struct v4l2_subdev_pad_config
*cfg
,
2243 struct v4l2_subdev_selection
*sel
)
2245 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2248 mutex_lock(&sensor
->mutex
);
2249 rval
= __smiapp_get_selection(subdev
, cfg
, sel
);
2250 mutex_unlock(&sensor
->mutex
);
2254 static int smiapp_set_selection(struct v4l2_subdev
*subdev
,
2255 struct v4l2_subdev_pad_config
*cfg
,
2256 struct v4l2_subdev_selection
*sel
)
2258 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2261 ret
= __smiapp_sel_supported(subdev
, sel
);
2265 mutex_lock(&sensor
->mutex
);
2267 sel
->r
.left
= max(0, sel
->r
.left
& ~1);
2268 sel
->r
.top
= max(0, sel
->r
.top
& ~1);
2269 sel
->r
.width
= SMIAPP_ALIGN_DIM(sel
->r
.width
, sel
->flags
);
2270 sel
->r
.height
= SMIAPP_ALIGN_DIM(sel
->r
.height
, sel
->flags
);
2272 sel
->r
.width
= max_t(unsigned int,
2273 SMIA_LIM(sensor
, MIN_X_OUTPUT_SIZE
),
2275 sel
->r
.height
= max_t(unsigned int,
2276 SMIA_LIM(sensor
, MIN_Y_OUTPUT_SIZE
),
2279 switch (sel
->target
) {
2280 case V4L2_SEL_TGT_CROP
:
2281 ret
= smiapp_set_crop(subdev
, cfg
, sel
);
2283 case V4L2_SEL_TGT_COMPOSE
:
2284 ret
= smiapp_set_compose(subdev
, cfg
, sel
);
2290 mutex_unlock(&sensor
->mutex
);
2294 static int smiapp_get_skip_frames(struct v4l2_subdev
*subdev
, u32
*frames
)
2296 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2298 *frames
= sensor
->frame_skip
;
2302 static int smiapp_get_skip_top_lines(struct v4l2_subdev
*subdev
, u32
*lines
)
2304 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2306 *lines
= sensor
->image_start
;
2311 /* -----------------------------------------------------------------------------
2316 smiapp_sysfs_nvm_read(struct device
*dev
, struct device_attribute
*attr
,
2319 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2320 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2321 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2324 if (!sensor
->dev_init_done
)
2327 rval
= smiapp_pm_get_init(sensor
);
2331 rval
= smiapp_read_nvm(sensor
, buf
, PAGE_SIZE
);
2333 pm_runtime_put(&client
->dev
);
2334 dev_err(&client
->dev
, "nvm read failed\n");
2338 pm_runtime_mark_last_busy(&client
->dev
);
2339 pm_runtime_put_autosuspend(&client
->dev
);
2342 * NVM is still way below a PAGE_SIZE, so we can safely
2343 * assume this for now.
2347 static DEVICE_ATTR(nvm
, S_IRUGO
, smiapp_sysfs_nvm_read
, NULL
);
2350 smiapp_sysfs_ident_read(struct device
*dev
, struct device_attribute
*attr
,
2353 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2354 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2355 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2357 return snprintf(buf
, PAGE_SIZE
, "%2.2x%4.4x%2.2x\n",
2358 minfo
->manufacturer_id
, minfo
->model_id
,
2359 minfo
->revision_number_major
) + 1;
2362 static DEVICE_ATTR(ident
, S_IRUGO
, smiapp_sysfs_ident_read
, NULL
);
2364 /* -----------------------------------------------------------------------------
2365 * V4L2 subdev core operations
2368 static int smiapp_identify_module(struct smiapp_sensor
*sensor
)
2370 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2371 struct smiapp_module_info
*minfo
= &sensor
->minfo
;
2375 minfo
->name
= SMIAPP_NAME
;
2378 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MANUFACTURER_ID
,
2379 &minfo
->manufacturer_id
);
2381 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U16_MODEL_ID
,
2384 rval
= smiapp_read_8only(sensor
,
2385 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR
,
2386 &minfo
->revision_number_major
);
2388 rval
= smiapp_read_8only(sensor
,
2389 SMIAPP_REG_U8_REVISION_NUMBER_MINOR
,
2390 &minfo
->revision_number_minor
);
2392 rval
= smiapp_read_8only(sensor
,
2393 SMIAPP_REG_U8_MODULE_DATE_YEAR
,
2394 &minfo
->module_year
);
2396 rval
= smiapp_read_8only(sensor
,
2397 SMIAPP_REG_U8_MODULE_DATE_MONTH
,
2398 &minfo
->module_month
);
2400 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_MODULE_DATE_DAY
,
2401 &minfo
->module_day
);
2405 rval
= smiapp_read_8only(sensor
,
2406 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID
,
2407 &minfo
->sensor_manufacturer_id
);
2409 rval
= smiapp_read_8only(sensor
,
2410 SMIAPP_REG_U16_SENSOR_MODEL_ID
,
2411 &minfo
->sensor_model_id
);
2413 rval
= smiapp_read_8only(sensor
,
2414 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER
,
2415 &minfo
->sensor_revision_number
);
2417 rval
= smiapp_read_8only(sensor
,
2418 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION
,
2419 &minfo
->sensor_firmware_version
);
2423 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIA_VERSION
,
2424 &minfo
->smia_version
);
2426 rval
= smiapp_read_8only(sensor
, SMIAPP_REG_U8_SMIAPP_VERSION
,
2427 &minfo
->smiapp_version
);
2430 dev_err(&client
->dev
, "sensor detection failed\n");
2434 dev_dbg(&client
->dev
, "module 0x%2.2x-0x%4.4x\n",
2435 minfo
->manufacturer_id
, minfo
->model_id
);
2437 dev_dbg(&client
->dev
,
2438 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2439 minfo
->revision_number_major
, minfo
->revision_number_minor
,
2440 minfo
->module_year
, minfo
->module_month
, minfo
->module_day
);
2442 dev_dbg(&client
->dev
, "sensor 0x%2.2x-0x%4.4x\n",
2443 minfo
->sensor_manufacturer_id
, minfo
->sensor_model_id
);
2445 dev_dbg(&client
->dev
,
2446 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2447 minfo
->sensor_revision_number
, minfo
->sensor_firmware_version
);
2449 dev_dbg(&client
->dev
, "smia version %2.2d smiapp version %2.2d\n",
2450 minfo
->smia_version
, minfo
->smiapp_version
);
2453 * Some modules have bad data in the lvalues below. Hope the
2454 * rvalues have better stuff. The lvalues are module
2455 * parameters whereas the rvalues are sensor parameters.
2457 if (!minfo
->manufacturer_id
&& !minfo
->model_id
) {
2458 minfo
->manufacturer_id
= minfo
->sensor_manufacturer_id
;
2459 minfo
->model_id
= minfo
->sensor_model_id
;
2460 minfo
->revision_number_major
= minfo
->sensor_revision_number
;
2463 for (i
= 0; i
< ARRAY_SIZE(smiapp_module_idents
); i
++) {
2464 if (smiapp_module_idents
[i
].manufacturer_id
2465 != minfo
->manufacturer_id
)
2467 if (smiapp_module_idents
[i
].model_id
!= minfo
->model_id
)
2469 if (smiapp_module_idents
[i
].flags
2470 & SMIAPP_MODULE_IDENT_FLAG_REV_LE
) {
2471 if (smiapp_module_idents
[i
].revision_number_major
2472 < minfo
->revision_number_major
)
2475 if (smiapp_module_idents
[i
].revision_number_major
2476 != minfo
->revision_number_major
)
2480 minfo
->name
= smiapp_module_idents
[i
].name
;
2481 minfo
->quirk
= smiapp_module_idents
[i
].quirk
;
2485 if (i
>= ARRAY_SIZE(smiapp_module_idents
))
2486 dev_warn(&client
->dev
,
2487 "no quirks for this module; let's hope it's fully compliant\n");
2489 dev_dbg(&client
->dev
, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2490 minfo
->name
, minfo
->manufacturer_id
, minfo
->model_id
,
2491 minfo
->revision_number_major
);
2496 static const struct v4l2_subdev_ops smiapp_ops
;
2497 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
;
2498 static const struct media_entity_operations smiapp_entity_ops
;
2500 static int smiapp_register_subdev(struct smiapp_sensor
*sensor
,
2501 struct smiapp_subdev
*ssd
,
2502 struct smiapp_subdev
*sink_ssd
,
2503 u16 source_pad
, u16 sink_pad
, u32 link_flags
)
2505 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2511 rval
= media_entity_pads_init(&ssd
->sd
.entity
,
2512 ssd
->npads
, ssd
->pads
);
2514 dev_err(&client
->dev
,
2515 "media_entity_pads_init failed\n");
2519 rval
= v4l2_device_register_subdev(sensor
->src
->sd
.v4l2_dev
,
2522 dev_err(&client
->dev
,
2523 "v4l2_device_register_subdev failed\n");
2527 rval
= media_create_pad_link(&ssd
->sd
.entity
, source_pad
,
2528 &sink_ssd
->sd
.entity
, sink_pad
,
2531 dev_err(&client
->dev
,
2532 "media_create_pad_link failed\n");
2533 v4l2_device_unregister_subdev(&ssd
->sd
);
2540 static void smiapp_unregistered(struct v4l2_subdev
*subdev
)
2542 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2545 for (i
= 1; i
< sensor
->ssds_used
; i
++)
2546 v4l2_device_unregister_subdev(&sensor
->ssds
[i
].sd
);
2549 static int smiapp_registered(struct v4l2_subdev
*subdev
)
2551 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2554 if (sensor
->scaler
) {
2555 rval
= smiapp_register_subdev(
2556 sensor
, sensor
->binner
, sensor
->scaler
,
2557 SMIAPP_PAD_SRC
, SMIAPP_PAD_SINK
,
2558 MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
);
2563 rval
= smiapp_register_subdev(
2564 sensor
, sensor
->pixel_array
, sensor
->binner
,
2565 SMIAPP_PA_PAD_SRC
, SMIAPP_PAD_SINK
,
2566 MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
);
2573 smiapp_unregistered(subdev
);
2578 static void smiapp_cleanup(struct smiapp_sensor
*sensor
)
2580 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2582 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2583 device_remove_file(&client
->dev
, &dev_attr_ident
);
2585 smiapp_free_controls(sensor
);
2588 static void smiapp_create_subdev(struct smiapp_sensor
*sensor
,
2589 struct smiapp_subdev
*ssd
, const char *name
,
2590 unsigned short num_pads
)
2592 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2597 if (ssd
!= sensor
->src
)
2598 v4l2_subdev_init(&ssd
->sd
, &smiapp_ops
);
2600 ssd
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2601 ssd
->sensor
= sensor
;
2603 ssd
->npads
= num_pads
;
2604 ssd
->source_pad
= num_pads
- 1;
2606 v4l2_i2c_subdev_set_name(&ssd
->sd
, client
, sensor
->minfo
.name
, name
);
2608 smiapp_get_native_size(ssd
, &ssd
->sink_fmt
);
2610 ssd
->compose
.width
= ssd
->sink_fmt
.width
;
2611 ssd
->compose
.height
= ssd
->sink_fmt
.height
;
2612 ssd
->crop
[ssd
->source_pad
] = ssd
->compose
;
2613 ssd
->pads
[ssd
->source_pad
].flags
= MEDIA_PAD_FL_SOURCE
;
2614 if (ssd
!= sensor
->pixel_array
) {
2615 ssd
->crop
[ssd
->sink_pad
] = ssd
->compose
;
2616 ssd
->pads
[ssd
->sink_pad
].flags
= MEDIA_PAD_FL_SINK
;
2619 ssd
->sd
.entity
.ops
= &smiapp_entity_ops
;
2621 if (ssd
== sensor
->src
)
2624 ssd
->sd
.internal_ops
= &smiapp_internal_ops
;
2625 ssd
->sd
.owner
= THIS_MODULE
;
2626 ssd
->sd
.dev
= &client
->dev
;
2627 v4l2_set_subdevdata(&ssd
->sd
, client
);
2630 static int smiapp_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
2632 struct smiapp_subdev
*ssd
= to_smiapp_subdev(sd
);
2633 struct smiapp_sensor
*sensor
= ssd
->sensor
;
2636 mutex_lock(&sensor
->mutex
);
2638 for (i
= 0; i
< ssd
->npads
; i
++) {
2639 struct v4l2_mbus_framefmt
*try_fmt
=
2640 v4l2_subdev_get_try_format(sd
, fh
->pad
, i
);
2641 struct v4l2_rect
*try_crop
=
2642 v4l2_subdev_get_try_crop(sd
, fh
->pad
, i
);
2643 struct v4l2_rect
*try_comp
;
2645 smiapp_get_native_size(ssd
, try_crop
);
2647 try_fmt
->width
= try_crop
->width
;
2648 try_fmt
->height
= try_crop
->height
;
2649 try_fmt
->code
= sensor
->internal_csi_format
->code
;
2650 try_fmt
->field
= V4L2_FIELD_NONE
;
2652 if (ssd
!= sensor
->pixel_array
)
2655 try_comp
= v4l2_subdev_get_try_compose(sd
, fh
->pad
, i
);
2656 *try_comp
= *try_crop
;
2659 mutex_unlock(&sensor
->mutex
);
2664 static const struct v4l2_subdev_video_ops smiapp_video_ops
= {
2665 .s_stream
= smiapp_set_stream
,
2668 static const struct v4l2_subdev_pad_ops smiapp_pad_ops
= {
2669 .enum_mbus_code
= smiapp_enum_mbus_code
,
2670 .get_fmt
= smiapp_get_format
,
2671 .set_fmt
= smiapp_set_format
,
2672 .get_selection
= smiapp_get_selection
,
2673 .set_selection
= smiapp_set_selection
,
2676 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops
= {
2677 .g_skip_frames
= smiapp_get_skip_frames
,
2678 .g_skip_top_lines
= smiapp_get_skip_top_lines
,
2681 static const struct v4l2_subdev_ops smiapp_ops
= {
2682 .video
= &smiapp_video_ops
,
2683 .pad
= &smiapp_pad_ops
,
2684 .sensor
= &smiapp_sensor_ops
,
2687 static const struct media_entity_operations smiapp_entity_ops
= {
2688 .link_validate
= v4l2_subdev_link_validate
,
2691 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops
= {
2692 .registered
= smiapp_registered
,
2693 .unregistered
= smiapp_unregistered
,
2694 .open
= smiapp_open
,
2697 static const struct v4l2_subdev_internal_ops smiapp_internal_ops
= {
2698 .open
= smiapp_open
,
2701 /* -----------------------------------------------------------------------------
2705 static int __maybe_unused
smiapp_suspend(struct device
*dev
)
2707 struct i2c_client
*client
= to_i2c_client(dev
);
2708 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2709 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2710 bool streaming
= sensor
->streaming
;
2713 rval
= pm_runtime_get_sync(dev
);
2715 if (rval
!= -EBUSY
&& rval
!= -EAGAIN
)
2716 pm_runtime_set_active(&client
->dev
);
2717 pm_runtime_put(dev
);
2721 if (sensor
->streaming
)
2722 smiapp_stop_streaming(sensor
);
2724 /* save state for resume */
2725 sensor
->streaming
= streaming
;
2730 static int __maybe_unused
smiapp_resume(struct device
*dev
)
2732 struct i2c_client
*client
= to_i2c_client(dev
);
2733 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2734 struct smiapp_sensor
*sensor
= to_smiapp_sensor(subdev
);
2737 pm_runtime_put(dev
);
2739 if (sensor
->streaming
)
2740 rval
= smiapp_start_streaming(sensor
);
2745 static struct smiapp_hwconfig
*smiapp_get_hwconfig(struct device
*dev
)
2747 struct smiapp_hwconfig
*hwcfg
;
2748 struct v4l2_fwnode_endpoint bus_cfg
= { .bus_type
= 0 };
2749 struct fwnode_handle
*ep
;
2750 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
2756 return dev
->platform_data
;
2758 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
2762 bus_cfg
.bus_type
= V4L2_MBUS_CSI2_DPHY
;
2763 rval
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
2764 if (rval
== -ENXIO
) {
2765 bus_cfg
= (struct v4l2_fwnode_endpoint
)
2766 { .bus_type
= V4L2_MBUS_CCP2
};
2767 rval
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
2772 hwcfg
= devm_kzalloc(dev
, sizeof(*hwcfg
), GFP_KERNEL
);
2776 switch (bus_cfg
.bus_type
) {
2777 case V4L2_MBUS_CSI2_DPHY
:
2778 hwcfg
->csi_signalling_mode
= SMIAPP_CSI_SIGNALLING_MODE_CSI2
;
2779 hwcfg
->lanes
= bus_cfg
.bus
.mipi_csi2
.num_data_lanes
;
2781 case V4L2_MBUS_CCP2
:
2782 hwcfg
->csi_signalling_mode
= (bus_cfg
.bus
.mipi_csi1
.strobe
) ?
2783 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE
:
2784 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK
;
2788 dev_err(dev
, "unsupported bus %u\n", bus_cfg
.bus_type
);
2792 dev_dbg(dev
, "lanes %u\n", hwcfg
->lanes
);
2794 rval
= fwnode_property_read_u32(fwnode
, "rotation", &rotation
);
2798 hwcfg
->module_board_orient
=
2799 SMIAPP_MODULE_BOARD_ORIENT_180
;
2804 dev_err(dev
, "invalid rotation %u\n", rotation
);
2809 rval
= fwnode_property_read_u32(dev_fwnode(dev
), "clock-frequency",
2812 dev_info(dev
, "can't get clock-frequency\n");
2814 dev_dbg(dev
, "clk %d, mode %d\n", hwcfg
->ext_clk
,
2815 hwcfg
->csi_signalling_mode
);
2817 if (!bus_cfg
.nr_of_link_frequencies
) {
2818 dev_warn(dev
, "no link frequencies defined\n");
2822 hwcfg
->op_sys_clock
= devm_kcalloc(
2823 dev
, bus_cfg
.nr_of_link_frequencies
+ 1 /* guardian */,
2824 sizeof(*hwcfg
->op_sys_clock
), GFP_KERNEL
);
2825 if (!hwcfg
->op_sys_clock
)
2828 for (i
= 0; i
< bus_cfg
.nr_of_link_frequencies
; i
++) {
2829 hwcfg
->op_sys_clock
[i
] = bus_cfg
.link_frequencies
[i
];
2830 dev_dbg(dev
, "freq %d: %lld\n", i
, hwcfg
->op_sys_clock
[i
]);
2833 v4l2_fwnode_endpoint_free(&bus_cfg
);
2834 fwnode_handle_put(ep
);
2838 v4l2_fwnode_endpoint_free(&bus_cfg
);
2839 fwnode_handle_put(ep
);
2843 static int smiapp_probe(struct i2c_client
*client
)
2845 struct smiapp_sensor
*sensor
;
2846 struct smiapp_hwconfig
*hwcfg
= smiapp_get_hwconfig(&client
->dev
);
2853 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
2857 sensor
->hwcfg
= hwcfg
;
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 mutex_init(&sensor
->mutex
);
2923 rval
= smiapp_identify_module(sensor
);
2929 rval
= smiapp_read_all_smia_limits(sensor
);
2935 rval
= smiapp_read_frame_fmt(sensor
);
2942 * Handle Sensor Module orientation on the board.
2944 * The application of H-FLIP and V-FLIP on the sensor is modified by
2945 * the sensor orientation on the board.
2947 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2948 * both H-FLIP and V-FLIP for normal operation which also implies
2949 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2950 * controls will need to be internally inverted.
2952 * Rotation also changes the bayer pattern.
2954 if (sensor
->hwcfg
->module_board_orient
==
2955 SMIAPP_MODULE_BOARD_ORIENT_180
)
2956 sensor
->hvflip_inv_mask
= SMIAPP_IMAGE_ORIENTATION_HFLIP
|
2957 SMIAPP_IMAGE_ORIENTATION_VFLIP
;
2959 rval
= smiapp_call_quirk(sensor
, limits
);
2961 dev_err(&client
->dev
, "limits quirks failed\n");
2965 if (SMIA_LIM(sensor
, BINNING_CAPABILITY
)) {
2968 rval
= smiapp_read(sensor
,
2969 SMIAPP_REG_U8_BINNING_SUBTYPES
, &val
);
2974 sensor
->nbinning_subtypes
= min_t(u8
, val
,
2975 SMIAPP_BINNING_SUBTYPES
);
2977 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
2979 sensor
, SMIAPP_REG_U8_BINNING_TYPE_n(i
), &val
);
2984 sensor
->binning_subtypes
[i
] =
2985 *(struct smiapp_binning_subtype
*)&val
;
2987 dev_dbg(&client
->dev
, "binning %xx%x\n",
2988 sensor
->binning_subtypes
[i
].horizontal
,
2989 sensor
->binning_subtypes
[i
].vertical
);
2992 sensor
->binning_horizontal
= 1;
2993 sensor
->binning_vertical
= 1;
2995 if (device_create_file(&client
->dev
, &dev_attr_ident
) != 0) {
2996 dev_err(&client
->dev
, "sysfs ident entry creation failed\n");
3001 if (sensor
->minfo
.smiapp_version
&&
3002 SMIA_LIM(sensor
, DATA_TRANSFER_IF_CAPABILITY
) &
3003 SMIAPP_DATA_TRANSFER_IF_CAPABILITY_SUPPORTED
) {
3004 if (device_create_file(&client
->dev
, &dev_attr_nvm
) != 0) {
3005 dev_err(&client
->dev
, "sysfs nvm entry failed\n");
3011 /* We consider this as profile 0 sensor if any of these are zero. */
3012 if (!SMIA_LIM(sensor
, MIN_OP_SYS_CLK_DIV
) ||
3013 !SMIA_LIM(sensor
, MAX_OP_SYS_CLK_DIV
) ||
3014 !SMIA_LIM(sensor
, MIN_OP_PIX_CLK_DIV
) ||
3015 !SMIA_LIM(sensor
, MAX_OP_PIX_CLK_DIV
)) {
3016 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_0
;
3017 } else if (SMIA_LIM(sensor
, SCALING_CAPABILITY
)
3018 != SMIAPP_SCALING_CAPABILITY_NONE
) {
3019 if (SMIA_LIM(sensor
, SCALING_CAPABILITY
)
3020 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL
)
3021 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_1
;
3023 sensor
->minfo
.smiapp_profile
= SMIAPP_PROFILE_2
;
3024 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
3025 sensor
->ssds_used
++;
3026 } else if (SMIA_LIM(sensor
, DIGITAL_CROP_CAPABILITY
)
3027 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
3028 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
3029 sensor
->ssds_used
++;
3031 sensor
->binner
= &sensor
->ssds
[sensor
->ssds_used
];
3032 sensor
->ssds_used
++;
3033 sensor
->pixel_array
= &sensor
->ssds
[sensor
->ssds_used
];
3034 sensor
->ssds_used
++;
3036 sensor
->scale_m
= SMIA_LIM(sensor
, SCALER_N_MIN
);
3038 /* prepare PLL configuration input values */
3039 sensor
->pll
.bus_type
= SMIAPP_PLL_BUS_TYPE_CSI2
;
3040 sensor
->pll
.csi2
.lanes
= sensor
->hwcfg
->lanes
;
3041 sensor
->pll
.ext_clk_freq_hz
= sensor
->hwcfg
->ext_clk
;
3042 sensor
->pll
.scale_n
= SMIA_LIM(sensor
, SCALER_N_MIN
);
3043 /* Profile 0 sensors have no separate OP clock branch. */
3044 if (sensor
->minfo
.smiapp_profile
== SMIAPP_PROFILE_0
)
3045 sensor
->pll
.flags
|= SMIAPP_PLL_FLAG_NO_OP_CLOCKS
;
3047 smiapp_create_subdev(sensor
, sensor
->scaler
, " scaler", 2);
3048 smiapp_create_subdev(sensor
, sensor
->binner
, " binner", 2);
3049 smiapp_create_subdev(sensor
, sensor
->pixel_array
, " pixel_array", 1);
3051 dev_dbg(&client
->dev
, "profile %d\n", sensor
->minfo
.smiapp_profile
);
3053 sensor
->pixel_array
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
3055 rval
= smiapp_init_controls(sensor
);
3059 rval
= smiapp_call_quirk(sensor
, init
);
3063 rval
= smiapp_get_mbus_formats(sensor
);
3069 rval
= smiapp_init_late_controls(sensor
);
3075 mutex_lock(&sensor
->mutex
);
3076 rval
= smiapp_pll_blanking_update(sensor
);
3077 mutex_unlock(&sensor
->mutex
);
3079 dev_err(&client
->dev
, "update mode failed\n");
3083 sensor
->streaming
= false;
3084 sensor
->dev_init_done
= true;
3086 rval
= media_entity_pads_init(&sensor
->src
->sd
.entity
, 2,
3089 goto out_media_entity_cleanup
;
3091 pm_runtime_set_active(&client
->dev
);
3092 pm_runtime_get_noresume(&client
->dev
);
3093 pm_runtime_enable(&client
->dev
);
3095 rval
= v4l2_async_register_subdev_sensor_common(&sensor
->src
->sd
);
3097 goto out_disable_runtime_pm
;
3099 pm_runtime_set_autosuspend_delay(&client
->dev
, 1000);
3100 pm_runtime_use_autosuspend(&client
->dev
);
3101 pm_runtime_put_autosuspend(&client
->dev
);
3105 out_disable_runtime_pm
:
3106 pm_runtime_disable(&client
->dev
);
3108 out_media_entity_cleanup
:
3109 media_entity_cleanup(&sensor
->src
->sd
.entity
);
3112 smiapp_cleanup(sensor
);
3115 smiapp_power_off(&client
->dev
);
3116 mutex_destroy(&sensor
->mutex
);
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
);
3139 mutex_destroy(&sensor
->mutex
);
3144 static const struct of_device_id smiapp_of_table
[] = {
3145 { .compatible
= "nokia,smia" },
3148 MODULE_DEVICE_TABLE(of
, smiapp_of_table
);
3150 static const struct i2c_device_id smiapp_id_table
[] = {
3154 MODULE_DEVICE_TABLE(i2c
, smiapp_id_table
);
3156 static const struct dev_pm_ops smiapp_pm_ops
= {
3157 SET_SYSTEM_SLEEP_PM_OPS(smiapp_suspend
, smiapp_resume
)
3158 SET_RUNTIME_PM_OPS(smiapp_power_off
, smiapp_power_on
, NULL
)
3161 static struct i2c_driver smiapp_i2c_driver
= {
3163 .of_match_table
= smiapp_of_table
,
3164 .name
= SMIAPP_NAME
,
3165 .pm
= &smiapp_pm_ops
,
3167 .probe_new
= smiapp_probe
,
3168 .remove
= smiapp_remove
,
3169 .id_table
= smiapp_id_table
,
3172 module_i2c_driver(smiapp_i2c_driver
);
3174 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3175 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3176 MODULE_LICENSE("GPL v2");