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_status = pm_runtime_get_if_active(&client->dev, true);
516 case V4L2_CID_ANALOGUE_GAIN:
519 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
522 case V4L2_CID_EXPOSURE:
525 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);
530 rval = smiapp_write(sensor, SMIAPP_REG_U8_IMAGE_ORIENTATION,
534 case V4L2_CID_VBLANK:
536 sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
537 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
541 case V4L2_CID_HBLANK:
543 sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
544 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
548 case V4L2_CID_TEST_PATTERN:
550 sensor, SMIAPP_REG_U16_TEST_PATTERN_MODE, ctrl->val);
553 case V4L2_CID_TEST_PATTERN_RED:
555 sensor, SMIAPP_REG_U16_TEST_DATA_RED, ctrl->val);
558 case V4L2_CID_TEST_PATTERN_GREENR:
560 sensor, SMIAPP_REG_U16_TEST_DATA_GREENR, ctrl->val);
563 case V4L2_CID_TEST_PATTERN_BLUE:
565 sensor, SMIAPP_REG_U16_TEST_DATA_BLUE, ctrl->val);
568 case V4L2_CID_TEST_PATTERN_GREENB:
570 sensor, SMIAPP_REG_U16_TEST_DATA_GREENB, ctrl->val);
573 case V4L2_CID_PIXEL_RATE:
574 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
583 pm_runtime_mark_last_busy(&client->dev);
584 pm_runtime_put_autosuspend(&client->dev);
590 static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
591 .s_ctrl = smiapp_set_ctrl,
594 static int smiapp_init_controls(struct smiapp_sensor *sensor)
596 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
599 rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 12);
603 sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
605 sensor->analog_gain = v4l2_ctrl_new_std(
606 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
607 V4L2_CID_ANALOGUE_GAIN,
608 SMIA_LIM(sensor, ANALOGUE_GAIN_CODE_MIN),
609 SMIA_LIM(sensor, ANALOGUE_GAIN_CODE_MAX),
610 max(SMIA_LIM(sensor, ANALOGUE_GAIN_CODE_STEP), 1U),
611 SMIA_LIM(sensor, ANALOGUE_GAIN_CODE_MIN));
613 /* Exposure limits will be updated soon, use just something here. */
614 sensor->exposure = v4l2_ctrl_new_std(
615 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
616 V4L2_CID_EXPOSURE, 0, 0, 1, 0);
618 sensor->hflip = v4l2_ctrl_new_std(
619 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
620 V4L2_CID_HFLIP, 0, 1, 1, 0);
621 sensor->vflip = v4l2_ctrl_new_std(
622 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
623 V4L2_CID_VFLIP, 0, 1, 1, 0);
625 sensor->vblank = v4l2_ctrl_new_std(
626 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
627 V4L2_CID_VBLANK, 0, 1, 1, 0);
630 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
632 sensor->hblank = v4l2_ctrl_new_std(
633 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
634 V4L2_CID_HBLANK, 0, 1, 1, 0);
637 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
639 sensor->pixel_rate_parray = v4l2_ctrl_new_std(
640 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
641 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
643 v4l2_ctrl_new_std_menu_items(&sensor->pixel_array->ctrl_handler,
644 &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN,
645 ARRAY_SIZE(smiapp_test_patterns) - 1,
646 0, 0, smiapp_test_patterns);
648 if (sensor->pixel_array->ctrl_handler.error) {
649 dev_err(&client->dev,
650 "pixel array controls initialization failed (%d)\n",
651 sensor->pixel_array->ctrl_handler.error);
652 return sensor->pixel_array->ctrl_handler.error;
655 sensor->pixel_array->sd.ctrl_handler =
656 &sensor->pixel_array->ctrl_handler;
658 v4l2_ctrl_cluster(2, &sensor->hflip);
660 rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
664 sensor->src->ctrl_handler.lock = &sensor->mutex;
666 sensor->pixel_rate_csi = v4l2_ctrl_new_std(
667 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
668 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
670 if (sensor->src->ctrl_handler.error) {
671 dev_err(&client->dev,
672 "src controls initialization failed (%d)\n",
673 sensor->src->ctrl_handler.error);
674 return sensor->src->ctrl_handler.error;
677 sensor->src->sd.ctrl_handler = &sensor->src->ctrl_handler;
683 * For controls that require information on available media bus codes
684 * and linke frequencies.
686 static int smiapp_init_late_controls(struct smiapp_sensor *sensor)
688 unsigned long *valid_link_freqs = &sensor->valid_link_freqs[
689 sensor->csi_format->compressed - sensor->compressed_min_bpp];
692 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) {
693 int max_value = (1 << sensor->csi_format->width) - 1;
695 sensor->test_data[i] = v4l2_ctrl_new_std(
696 &sensor->pixel_array->ctrl_handler,
697 &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN_RED + i,
698 0, max_value, 1, max_value);
701 sensor->link_freq = v4l2_ctrl_new_int_menu(
702 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
703 V4L2_CID_LINK_FREQ, __fls(*valid_link_freqs),
704 __ffs(*valid_link_freqs), sensor->hwcfg->op_sys_clock);
706 return sensor->src->ctrl_handler.error;
709 static void smiapp_free_controls(struct smiapp_sensor *sensor)
713 for (i = 0; i < sensor->ssds_used; i++)
714 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
717 static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
719 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
720 struct smiapp_pll *pll = &sensor->pll;
721 u8 compressed_max_bpp = 0;
722 unsigned int type, n;
723 unsigned int i, pixel_order;
727 sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
731 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
733 rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
738 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
739 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
743 dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
744 pixel_order_str[pixel_order]);
747 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
748 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
750 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
751 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
757 sensor->default_pixel_order = pixel_order;
758 sensor->mbus_frame_fmts = 0;
760 for (i = 0; i < n; i++) {
765 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
769 dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n",
770 i, fmt >> 8, (u8)fmt);
772 for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
773 const struct smiapp_csi_data_format *f =
774 &smiapp_csi_data_formats[j];
776 if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
779 if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
782 dev_dbg(&client->dev, "jolly good! %d\n", j);
784 sensor->default_mbus_frame_fmts |= 1 << j;
788 /* Figure out which BPP values can be used with which formats. */
789 pll->binning_horizontal = 1;
790 pll->binning_vertical = 1;
791 pll->scale_m = sensor->scale_m;
793 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
794 sensor->compressed_min_bpp =
795 min(smiapp_csi_data_formats[i].compressed,
796 sensor->compressed_min_bpp);
798 max(smiapp_csi_data_formats[i].compressed,
802 sensor->valid_link_freqs = devm_kcalloc(
804 compressed_max_bpp - sensor->compressed_min_bpp + 1,
805 sizeof(*sensor->valid_link_freqs), GFP_KERNEL);
806 if (!sensor->valid_link_freqs)
809 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
810 const struct smiapp_csi_data_format *f =
811 &smiapp_csi_data_formats[i];
812 unsigned long *valid_link_freqs =
813 &sensor->valid_link_freqs[
814 f->compressed - sensor->compressed_min_bpp];
817 if (!(sensor->default_mbus_frame_fmts & 1 << i))
820 pll->bits_per_pixel = f->compressed;
822 for (j = 0; sensor->hwcfg->op_sys_clock[j]; j++) {
823 pll->link_freq = sensor->hwcfg->op_sys_clock[j];
825 rval = smiapp_pll_try(sensor, pll);
826 dev_dbg(&client->dev, "link freq %u Hz, bpp %u %s\n",
827 pll->link_freq, pll->bits_per_pixel,
828 rval ? "not ok" : "ok");
832 set_bit(j, valid_link_freqs);
835 if (!*valid_link_freqs) {
836 dev_info(&client->dev,
837 "no valid link frequencies for %u bpp\n",
839 sensor->default_mbus_frame_fmts &= ~BIT(i);
843 if (!sensor->csi_format
844 || f->width > sensor->csi_format->width
845 || (f->width == sensor->csi_format->width
846 && f->compressed > sensor->csi_format->compressed)) {
847 sensor->csi_format = f;
848 sensor->internal_csi_format = f;
852 if (!sensor->csi_format) {
853 dev_err(&client->dev, "no supported mbus code found\n");
857 smiapp_update_mbus_formats(sensor);
862 static void smiapp_update_blanking(struct smiapp_sensor *sensor)
864 struct v4l2_ctrl *vblank = sensor->vblank;
865 struct v4l2_ctrl *hblank = sensor->hblank;
866 uint16_t min_fll, max_fll, min_llp, max_llp, min_lbp;
869 if (sensor->binning_vertical > 1 || sensor->binning_horizontal > 1) {
870 min_fll = SMIA_LIM(sensor, MIN_FRAME_LENGTH_LINES_BIN);
871 max_fll = SMIA_LIM(sensor, MAX_FRAME_LENGTH_LINES_BIN);
872 min_llp = SMIA_LIM(sensor, MIN_LINE_LENGTH_PCK_BIN);
873 max_llp = SMIA_LIM(sensor, MAX_LINE_LENGTH_PCK_BIN);
874 min_lbp = SMIA_LIM(sensor, MIN_LINE_BLANKING_PCK_BIN);
876 min_fll = SMIA_LIM(sensor, MIN_FRAME_LENGTH_LINES);
877 max_fll = SMIA_LIM(sensor, MAX_FRAME_LENGTH_LINES);
878 min_llp = SMIA_LIM(sensor, MIN_LINE_LENGTH_PCK);
879 max_llp = SMIA_LIM(sensor, MAX_LINE_LENGTH_PCK);
880 min_lbp = SMIA_LIM(sensor, MIN_LINE_BLANKING_PCK);
884 SMIA_LIM(sensor, MIN_FRAME_BLANKING_LINES),
886 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
887 max = max_fll - sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
889 __v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min);
893 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
895 max = max_llp - sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
897 __v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min);
899 __smiapp_update_exposure_limits(sensor);
902 static int smiapp_pll_blanking_update(struct smiapp_sensor *sensor)
904 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
907 rval = smiapp_pll_update(sensor);
911 /* Output from pixel array, including blanking */
912 smiapp_update_blanking(sensor);
914 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
915 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
917 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
918 sensor->pll.pixel_rate_pixel_array /
919 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
920 + sensor->hblank->val) *
921 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
922 + sensor->vblank->val) / 100));
929 * SMIA++ NVM handling
933 static int smiapp_read_nvm_page(struct smiapp_sensor *sensor, u32 p, u8 *nvm,
942 rval = smiapp_write(sensor,
943 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
947 rval = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
948 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN);
952 rval = smiapp_read(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS,
957 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_EUSAGE) {
962 if (SMIA_LIM(sensor, DATA_TRANSFER_IF_CAPABILITY) &
963 SMIAPP_DATA_TRANSFER_IF_CAPABILITY_POLL) {
964 for (i = 1000; i > 0; i--) {
965 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
970 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS,
981 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
984 rval = smiapp_read(sensor,
985 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
996 static int smiapp_read_nvm(struct smiapp_sensor *sensor, unsigned char *nvm,
1001 int rval = 0, rval2;
1003 for (p = 0; p < nvm_size / SMIAPP_NVM_PAGE_SIZE && !rval; p++) {
1004 rval = smiapp_read_nvm_page(sensor, p, nvm, &status);
1005 nvm += SMIAPP_NVM_PAGE_SIZE;
1008 if (rval == -ENODATA &&
1009 status & SMIAPP_DATA_TRANSFER_IF_1_STATUS_EUSAGE)
1012 rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
1016 return rval2 ?: p * SMIAPP_NVM_PAGE_SIZE;
1021 * SMIA++ CCI address control
1024 static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
1026 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1030 client->addr = sensor->hwcfg->i2c_addr_dfl;
1032 rval = smiapp_write(sensor,
1033 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
1034 sensor->hwcfg->i2c_addr_alt << 1);
1038 client->addr = sensor->hwcfg->i2c_addr_alt;
1040 /* verify addr change went ok */
1041 rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
1045 if (val != sensor->hwcfg->i2c_addr_alt << 1)
1053 * SMIA++ Mode Control
1056 static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
1058 struct smiapp_flash_strobe_parms *strobe_setup;
1059 unsigned int ext_freq = sensor->hwcfg->ext_clk;
1061 u32 strobe_adjustment;
1062 u32 strobe_width_high_rs;
1065 strobe_setup = sensor->hwcfg->strobe_setup;
1068 * How to calculate registers related to strobe length. Please
1069 * do not change, or if you do at least know what you're
1072 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1074 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1075 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1077 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1078 * flash_strobe_adjustment E N, [1 - 0xff]
1080 * The formula above is written as below to keep it on one
1083 * l / 10^6 = w / e * a
1085 * Let's mark w * a by x:
1093 * The strobe width must be at least as long as requested,
1094 * thus rounding upwards is needed.
1096 * x = (l * e + 10^6 - 1) / 10^6
1097 * -----------------------------
1099 * Maximum possible accuracy is wanted at all times. Thus keep
1100 * a as small as possible.
1102 * Calculate a, assuming maximum w, with rounding upwards:
1104 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1105 * -------------------------------------
1107 * Thus, we also get w, with that a, with rounding upwards:
1109 * w = (x + a - 1) / a
1110 * -------------------
1114 * x E [1, (2^16 - 1) * (2^8 - 1)]
1116 * Substituting maximum x to the original formula (with rounding),
1117 * the maximum l is thus
1119 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1121 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1122 * --------------------------------------------------
1124 * flash_strobe_length must be clamped between 1 and
1125 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1129 * flash_strobe_adjustment = ((flash_strobe_length *
1130 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1132 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1133 * EXTCLK freq + 10^6 - 1) / 10^6 +
1134 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1136 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1137 1000000 + 1, ext_freq);
1138 strobe_setup->strobe_width_high_us =
1139 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1141 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1142 1000000 - 1), 1000000ULL);
1143 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1144 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1147 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
1148 strobe_setup->mode);
1152 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
1157 rval = smiapp_write(
1158 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1159 strobe_width_high_rs);
1163 rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
1164 strobe_setup->strobe_delay);
1168 rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
1169 strobe_setup->stobe_start_point);
1173 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
1174 strobe_setup->trigger);
1177 sensor->hwcfg->strobe_setup->trigger = 0;
1182 /* -----------------------------------------------------------------------------
1186 static int smiapp_power_on(struct device *dev)
1188 struct i2c_client *client = to_i2c_client(dev);
1189 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1190 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1192 * The sub-device related to the I2C device is always the
1193 * source one, i.e. ssds[0].
1195 struct smiapp_sensor *sensor =
1196 container_of(ssd, struct smiapp_sensor, ssds[0]);
1200 rval = regulator_enable(sensor->vana);
1202 dev_err(&client->dev, "failed to enable vana regulator\n");
1205 usleep_range(1000, 1000);
1207 rval = clk_prepare_enable(sensor->ext_clk);
1209 dev_dbg(&client->dev, "failed to enable xclk\n");
1212 usleep_range(1000, 1000);
1214 gpiod_set_value(sensor->xshutdown, 1);
1216 sleep = SMIAPP_RESET_DELAY(sensor->hwcfg->ext_clk);
1217 usleep_range(sleep, sleep);
1220 * Failures to respond to the address change command have been noticed.
1221 * Those failures seem to be caused by the sensor requiring a longer
1222 * boot time than advertised. An additional 10ms delay seems to work
1223 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1224 * unnecessary. The failures need to be investigated to find a proper
1225 * fix, and a delay will likely need to be added here if the I2C write
1226 * retry hack is reverted before the root cause of the boot time issue
1230 if (sensor->hwcfg->i2c_addr_alt) {
1231 rval = smiapp_change_cci_addr(sensor);
1233 dev_err(&client->dev, "cci address change error\n");
1234 goto out_cci_addr_fail;
1238 rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
1239 SMIAPP_SOFTWARE_RESET);
1241 dev_err(&client->dev, "software reset failed\n");
1242 goto out_cci_addr_fail;
1245 if (sensor->hwcfg->i2c_addr_alt) {
1246 rval = smiapp_change_cci_addr(sensor);
1248 dev_err(&client->dev, "cci address change error\n");
1249 goto out_cci_addr_fail;
1253 rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
1254 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1256 dev_err(&client->dev, "compression mode set failed\n");
1257 goto out_cci_addr_fail;
1260 rval = smiapp_write(
1261 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1262 sensor->hwcfg->ext_clk / (1000000 / (1 << 8)));
1264 dev_err(&client->dev, "extclk frequency set failed\n");
1265 goto out_cci_addr_fail;
1268 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
1269 sensor->hwcfg->lanes - 1);
1271 dev_err(&client->dev, "csi lane mode set failed\n");
1272 goto out_cci_addr_fail;
1275 rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
1276 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1278 dev_err(&client->dev, "fast standby set failed\n");
1279 goto out_cci_addr_fail;
1282 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1283 sensor->hwcfg->csi_signalling_mode);
1285 dev_err(&client->dev, "csi signalling mode set failed\n");
1286 goto out_cci_addr_fail;
1289 /* DPHY control done by sensor based on requested link rate */
1290 rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
1291 SMIAPP_DPHY_CTRL_UI);
1293 goto out_cci_addr_fail;
1295 rval = smiapp_call_quirk(sensor, post_poweron);
1297 dev_err(&client->dev, "post_poweron quirks failed\n");
1298 goto out_cci_addr_fail;
1304 gpiod_set_value(sensor->xshutdown, 0);
1305 clk_disable_unprepare(sensor->ext_clk);
1308 regulator_disable(sensor->vana);
1313 static int smiapp_power_off(struct device *dev)
1315 struct i2c_client *client = to_i2c_client(dev);
1316 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1317 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1318 struct smiapp_sensor *sensor =
1319 container_of(ssd, struct smiapp_sensor, ssds[0]);
1322 * Currently power/clock to lens are enable/disabled separately
1323 * but they are essentially the same signals. So if the sensor is
1324 * powered off while the lens is powered on the sensor does not
1325 * really see a power off and next time the cci address change
1326 * will fail. So do a soft reset explicitly here.
1328 if (sensor->hwcfg->i2c_addr_alt)
1329 smiapp_write(sensor,
1330 SMIAPP_REG_U8_SOFTWARE_RESET,
1331 SMIAPP_SOFTWARE_RESET);
1333 gpiod_set_value(sensor->xshutdown, 0);
1334 clk_disable_unprepare(sensor->ext_clk);
1335 usleep_range(5000, 5000);
1336 regulator_disable(sensor->vana);
1337 sensor->streaming = false;
1342 /* -----------------------------------------------------------------------------
1343 * Video stream management
1346 static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1348 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1349 unsigned int binning_mode;
1352 mutex_lock(&sensor->mutex);
1354 rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
1355 (sensor->csi_format->width << 8) |
1356 sensor->csi_format->compressed);
1360 /* Binning configuration */
1361 if (sensor->binning_horizontal == 1 &&
1362 sensor->binning_vertical == 1) {
1366 (sensor->binning_horizontal << 4)
1367 | sensor->binning_vertical;
1369 rval = smiapp_write(
1370 sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
1376 rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
1381 rval = smiapp_pll_configure(sensor);
1385 /* Analog crop start coordinates */
1386 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
1387 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1391 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
1392 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1396 /* Analog crop end coordinates */
1397 rval = smiapp_write(
1398 sensor, SMIAPP_REG_U16_X_ADDR_END,
1399 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1400 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1404 rval = smiapp_write(
1405 sensor, SMIAPP_REG_U16_Y_ADDR_END,
1406 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1407 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1412 * Output from pixel array, including blanking, is set using
1413 * controls below. No need to set here.
1417 if (SMIA_LIM(sensor, DIGITAL_CROP_CAPABILITY)
1418 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1419 rval = smiapp_write(
1420 sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
1421 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1425 rval = smiapp_write(
1426 sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
1427 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1431 rval = smiapp_write(
1432 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
1433 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1437 rval = smiapp_write(
1438 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
1439 sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1445 if (SMIA_LIM(sensor, SCALING_CAPABILITY)
1446 != SMIAPP_SCALING_CAPABILITY_NONE) {
1447 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
1448 sensor->scaling_mode);
1452 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
1458 /* Output size from sensor */
1459 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
1460 sensor->src->crop[SMIAPP_PAD_SRC].width);
1463 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
1464 sensor->src->crop[SMIAPP_PAD_SRC].height);
1468 if ((SMIA_LIM(sensor, FLASH_MODE_CAPABILITY) &
1469 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1470 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1471 sensor->hwcfg->strobe_setup != NULL &&
1472 sensor->hwcfg->strobe_setup->trigger != 0) {
1473 rval = smiapp_setup_flash_strobe(sensor);
1478 rval = smiapp_call_quirk(sensor, pre_streamon);
1480 dev_err(&client->dev, "pre_streamon quirks failed\n");
1484 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1485 SMIAPP_MODE_SELECT_STREAMING);
1488 mutex_unlock(&sensor->mutex);
1493 static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1495 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1498 mutex_lock(&sensor->mutex);
1499 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1500 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1504 rval = smiapp_call_quirk(sensor, post_streamoff);
1506 dev_err(&client->dev, "post_streamoff quirks failed\n");
1509 mutex_unlock(&sensor->mutex);
1513 /* -----------------------------------------------------------------------------
1514 * V4L2 subdev video operations
1517 static int smiapp_pm_get_init(struct smiapp_sensor *sensor)
1519 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1522 rval = pm_runtime_get_sync(&client->dev);
1524 if (rval != -EBUSY && rval != -EAGAIN)
1525 pm_runtime_set_active(&client->dev);
1526 pm_runtime_put_noidle(&client->dev);
1530 rval = v4l2_ctrl_handler_setup(&sensor->pixel_array->
1535 return v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1541 static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1543 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1544 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1547 if (sensor->streaming == enable)
1551 smiapp_stop_streaming(sensor);
1552 sensor->streaming = false;
1553 pm_runtime_mark_last_busy(&client->dev);
1554 pm_runtime_put_autosuspend(&client->dev);
1559 rval = smiapp_pm_get_init(sensor);
1563 sensor->streaming = true;
1565 rval = smiapp_start_streaming(sensor);
1567 sensor->streaming = false;
1568 pm_runtime_mark_last_busy(&client->dev);
1569 pm_runtime_put_autosuspend(&client->dev);
1575 static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1576 struct v4l2_subdev_pad_config *cfg,
1577 struct v4l2_subdev_mbus_code_enum *code)
1579 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1580 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1585 mutex_lock(&sensor->mutex);
1587 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1588 subdev->name, code->pad, code->index);
1590 if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1594 code->code = sensor->internal_csi_format->code;
1599 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1600 if (sensor->mbus_frame_fmts & (1 << i))
1603 if (idx == code->index) {
1604 code->code = smiapp_csi_data_formats[i].code;
1605 dev_err(&client->dev, "found index %d, i %d, code %x\n",
1606 code->index, i, code->code);
1613 mutex_unlock(&sensor->mutex);
1618 static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1621 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1623 if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1624 return sensor->csi_format->code;
1626 return sensor->internal_csi_format->code;
1629 static int __smiapp_get_format(struct v4l2_subdev *subdev,
1630 struct v4l2_subdev_pad_config *cfg,
1631 struct v4l2_subdev_format *fmt)
1633 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1635 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1636 fmt->format = *v4l2_subdev_get_try_format(subdev, cfg,
1639 struct v4l2_rect *r;
1641 if (fmt->pad == ssd->source_pad)
1642 r = &ssd->crop[ssd->source_pad];
1646 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1647 fmt->format.width = r->width;
1648 fmt->format.height = r->height;
1649 fmt->format.field = V4L2_FIELD_NONE;
1655 static int smiapp_get_format(struct v4l2_subdev *subdev,
1656 struct v4l2_subdev_pad_config *cfg,
1657 struct v4l2_subdev_format *fmt)
1659 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1662 mutex_lock(&sensor->mutex);
1663 rval = __smiapp_get_format(subdev, cfg, fmt);
1664 mutex_unlock(&sensor->mutex);
1669 static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1670 struct v4l2_subdev_pad_config *cfg,
1671 struct v4l2_rect **crops,
1672 struct v4l2_rect **comps, int which)
1674 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1677 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1679 for (i = 0; i < subdev->entity.num_pads; i++)
1680 crops[i] = &ssd->crop[i];
1682 *comps = &ssd->compose;
1685 for (i = 0; i < subdev->entity.num_pads; i++) {
1686 crops[i] = v4l2_subdev_get_try_crop(subdev, cfg, i);
1691 *comps = v4l2_subdev_get_try_compose(subdev, cfg,
1698 /* Changes require propagation only on sink pad. */
1699 static void smiapp_propagate(struct v4l2_subdev *subdev,
1700 struct v4l2_subdev_pad_config *cfg, int which,
1703 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1704 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1705 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1707 smiapp_get_crop_compose(subdev, cfg, crops, &comp, which);
1710 case V4L2_SEL_TGT_CROP:
1711 comp->width = crops[SMIAPP_PAD_SINK]->width;
1712 comp->height = crops[SMIAPP_PAD_SINK]->height;
1713 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1714 if (ssd == sensor->scaler) {
1716 SMIA_LIM(sensor, SCALER_N_MIN);
1717 sensor->scaling_mode =
1718 SMIAPP_SCALING_MODE_NONE;
1719 } else if (ssd == sensor->binner) {
1720 sensor->binning_horizontal = 1;
1721 sensor->binning_vertical = 1;
1725 case V4L2_SEL_TGT_COMPOSE:
1726 *crops[SMIAPP_PAD_SRC] = *comp;
1733 static const struct smiapp_csi_data_format
1734 *smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1738 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1739 if (sensor->mbus_frame_fmts & (1 << i)
1740 && smiapp_csi_data_formats[i].code == code)
1741 return &smiapp_csi_data_formats[i];
1744 return sensor->csi_format;
1747 static int smiapp_set_format_source(struct v4l2_subdev *subdev,
1748 struct v4l2_subdev_pad_config *cfg,
1749 struct v4l2_subdev_format *fmt)
1751 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1752 const struct smiapp_csi_data_format *csi_format,
1753 *old_csi_format = sensor->csi_format;
1754 unsigned long *valid_link_freqs;
1755 u32 code = fmt->format.code;
1759 rval = __smiapp_get_format(subdev, cfg, fmt);
1764 * Media bus code is changeable on src subdev's source pad. On
1765 * other source pads we just get format here.
1767 if (subdev != &sensor->src->sd)
1770 csi_format = smiapp_validate_csi_data_format(sensor, code);
1772 fmt->format.code = csi_format->code;
1774 if (fmt->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1777 sensor->csi_format = csi_format;
1779 if (csi_format->width != old_csi_format->width)
1780 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
1781 __v4l2_ctrl_modify_range(
1782 sensor->test_data[i], 0,
1783 (1 << csi_format->width) - 1, 1, 0);
1785 if (csi_format->compressed == old_csi_format->compressed)
1789 &sensor->valid_link_freqs[sensor->csi_format->compressed
1790 - sensor->compressed_min_bpp];
1792 __v4l2_ctrl_modify_range(
1793 sensor->link_freq, 0,
1794 __fls(*valid_link_freqs), ~*valid_link_freqs,
1795 __ffs(*valid_link_freqs));
1797 return smiapp_pll_update(sensor);
1800 static int smiapp_set_format(struct v4l2_subdev *subdev,
1801 struct v4l2_subdev_pad_config *cfg,
1802 struct v4l2_subdev_format *fmt)
1804 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1805 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1806 struct v4l2_rect *crops[SMIAPP_PADS];
1808 mutex_lock(&sensor->mutex);
1810 if (fmt->pad == ssd->source_pad) {
1813 rval = smiapp_set_format_source(subdev, cfg, fmt);
1815 mutex_unlock(&sensor->mutex);
1820 /* Sink pad. Width and height are changeable here. */
1821 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1822 fmt->format.width &= ~1;
1823 fmt->format.height &= ~1;
1824 fmt->format.field = V4L2_FIELD_NONE;
1827 clamp(fmt->format.width,
1828 SMIA_LIM(sensor, MIN_X_OUTPUT_SIZE),
1829 SMIA_LIM(sensor, MAX_X_OUTPUT_SIZE));
1830 fmt->format.height =
1831 clamp(fmt->format.height,
1832 SMIA_LIM(sensor, MIN_Y_OUTPUT_SIZE),
1833 SMIA_LIM(sensor, MAX_Y_OUTPUT_SIZE));
1835 smiapp_get_crop_compose(subdev, cfg, crops, NULL, fmt->which);
1837 crops[ssd->sink_pad]->left = 0;
1838 crops[ssd->sink_pad]->top = 0;
1839 crops[ssd->sink_pad]->width = fmt->format.width;
1840 crops[ssd->sink_pad]->height = fmt->format.height;
1841 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1842 ssd->sink_fmt = *crops[ssd->sink_pad];
1843 smiapp_propagate(subdev, cfg, fmt->which,
1846 mutex_unlock(&sensor->mutex);
1852 * Calculate goodness of scaled image size compared to expected image
1853 * size and flags provided.
1855 #define SCALING_GOODNESS 100000
1856 #define SCALING_GOODNESS_EXTREME 100000000
1857 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1858 int h, int ask_h, u32 flags)
1860 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1861 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1869 if (flags & V4L2_SEL_FLAG_GE) {
1871 val -= SCALING_GOODNESS;
1873 val -= SCALING_GOODNESS;
1876 if (flags & V4L2_SEL_FLAG_LE) {
1878 val -= SCALING_GOODNESS;
1880 val -= SCALING_GOODNESS;
1883 val -= abs(w - ask_w);
1884 val -= abs(h - ask_h);
1886 if (w < SMIA_LIM(sensor, MIN_X_OUTPUT_SIZE))
1887 val -= SCALING_GOODNESS_EXTREME;
1889 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1890 w, ask_w, h, ask_h, val);
1895 static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1896 struct v4l2_subdev_pad_config *cfg,
1897 struct v4l2_subdev_selection *sel,
1898 struct v4l2_rect **crops,
1899 struct v4l2_rect *comp)
1901 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1903 unsigned int binh = 1, binv = 1;
1904 int best = scaling_goodness(
1906 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1907 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1909 for (i = 0; i < sensor->nbinning_subtypes; i++) {
1910 int this = scaling_goodness(
1912 crops[SMIAPP_PAD_SINK]->width
1913 / sensor->binning_subtypes[i].horizontal,
1915 crops[SMIAPP_PAD_SINK]->height
1916 / sensor->binning_subtypes[i].vertical,
1917 sel->r.height, sel->flags);
1920 binh = sensor->binning_subtypes[i].horizontal;
1921 binv = sensor->binning_subtypes[i].vertical;
1925 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1926 sensor->binning_vertical = binv;
1927 sensor->binning_horizontal = binh;
1930 sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1931 sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1935 * Calculate best scaling ratio and mode for given output resolution.
1937 * Try all of these: horizontal ratio, vertical ratio and smallest
1938 * size possible (horizontally).
1940 * Also try whether horizontal scaler or full scaler gives a better
1943 static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1944 struct v4l2_subdev_pad_config *cfg,
1945 struct v4l2_subdev_selection *sel,
1946 struct v4l2_rect **crops,
1947 struct v4l2_rect *comp)
1949 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1950 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1951 u32 min, max, a, b, max_m;
1952 u32 scale_m = SMIA_LIM(sensor, SCALER_N_MIN);
1953 int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1959 sel->r.width = min_t(unsigned int, sel->r.width,
1960 crops[SMIAPP_PAD_SINK]->width);
1961 sel->r.height = min_t(unsigned int, sel->r.height,
1962 crops[SMIAPP_PAD_SINK]->height);
1964 a = crops[SMIAPP_PAD_SINK]->width
1965 * SMIA_LIM(sensor, SCALER_N_MIN) / sel->r.width;
1966 b = crops[SMIAPP_PAD_SINK]->height
1967 * SMIA_LIM(sensor, SCALER_N_MIN) / sel->r.height;
1968 max_m = crops[SMIAPP_PAD_SINK]->width
1969 * SMIA_LIM(sensor, SCALER_N_MIN)
1970 / SMIA_LIM(sensor, MIN_X_OUTPUT_SIZE);
1972 a = clamp(a, SMIA_LIM(sensor, SCALER_M_MIN),
1973 SMIA_LIM(sensor, SCALER_M_MAX));
1974 b = clamp(b, SMIA_LIM(sensor, SCALER_M_MIN),
1975 SMIA_LIM(sensor, SCALER_M_MAX));
1976 max_m = clamp(max_m, SMIA_LIM(sensor, SCALER_M_MIN),
1977 SMIA_LIM(sensor, SCALER_M_MAX));
1979 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1981 min = min(max_m, min(a, b));
1982 max = min(max_m, max(a, b));
1991 try[ntry] = min + 1;
1994 try[ntry] = max + 1;
1999 for (i = 0; i < ntry; i++) {
2000 int this = scaling_goodness(
2002 crops[SMIAPP_PAD_SINK]->width
2004 * SMIA_LIM(sensor, SCALER_N_MIN),
2006 crops[SMIAPP_PAD_SINK]->height,
2010 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
2014 mode = SMIAPP_SCALING_MODE_HORIZONTAL;
2018 if (SMIA_LIM(sensor, SCALING_CAPABILITY)
2019 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2022 this = scaling_goodness(
2023 subdev, crops[SMIAPP_PAD_SINK]->width
2025 * SMIA_LIM(sensor, SCALER_N_MIN),
2027 crops[SMIAPP_PAD_SINK]->height
2029 * SMIA_LIM(sensor, SCALER_N_MIN),
2035 mode = SMIAPP_SCALING_MODE_BOTH;
2041 (crops[SMIAPP_PAD_SINK]->width
2043 * SMIA_LIM(sensor, SCALER_N_MIN)) & ~1;
2044 if (mode == SMIAPP_SCALING_MODE_BOTH)
2046 (crops[SMIAPP_PAD_SINK]->height
2048 * SMIA_LIM(sensor, SCALER_N_MIN))
2051 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
2053 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2054 sensor->scale_m = scale_m;
2055 sensor->scaling_mode = mode;
2058 /* We're only called on source pads. This function sets scaling. */
2059 static int smiapp_set_compose(struct v4l2_subdev *subdev,
2060 struct v4l2_subdev_pad_config *cfg,
2061 struct v4l2_subdev_selection *sel)
2063 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2064 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2065 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2067 smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2072 if (ssd == sensor->binner)
2073 smiapp_set_compose_binner(subdev, cfg, sel, crops, comp);
2075 smiapp_set_compose_scaler(subdev, cfg, sel, crops, comp);
2078 smiapp_propagate(subdev, cfg, sel->which, V4L2_SEL_TGT_COMPOSE);
2080 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
2081 return smiapp_pll_blanking_update(sensor);
2086 static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
2087 struct v4l2_subdev_selection *sel)
2089 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2090 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2092 /* We only implement crop in three places. */
2093 switch (sel->target) {
2094 case V4L2_SEL_TGT_CROP:
2095 case V4L2_SEL_TGT_CROP_BOUNDS:
2096 if (ssd == sensor->pixel_array
2097 && sel->pad == SMIAPP_PA_PAD_SRC)
2099 if (ssd == sensor->src
2100 && sel->pad == SMIAPP_PAD_SRC)
2102 if (ssd == sensor->scaler
2103 && sel->pad == SMIAPP_PAD_SINK
2104 && SMIA_LIM(sensor, DIGITAL_CROP_CAPABILITY)
2105 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
2108 case V4L2_SEL_TGT_NATIVE_SIZE:
2109 if (ssd == sensor->pixel_array
2110 && sel->pad == SMIAPP_PA_PAD_SRC)
2113 case V4L2_SEL_TGT_COMPOSE:
2114 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2115 if (sel->pad == ssd->source_pad)
2117 if (ssd == sensor->binner)
2119 if (ssd == sensor->scaler
2120 && SMIA_LIM(sensor, SCALING_CAPABILITY)
2121 != SMIAPP_SCALING_CAPABILITY_NONE)
2129 static int smiapp_set_crop(struct v4l2_subdev *subdev,
2130 struct v4l2_subdev_pad_config *cfg,
2131 struct v4l2_subdev_selection *sel)
2133 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2134 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2135 struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
2136 struct v4l2_rect _r;
2138 smiapp_get_crop_compose(subdev, cfg, crops, NULL, sel->which);
2140 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2141 if (sel->pad == ssd->sink_pad)
2142 src_size = &ssd->sink_fmt;
2144 src_size = &ssd->compose;
2146 if (sel->pad == ssd->sink_pad) {
2149 _r.width = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2151 _r.height = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2155 src_size = v4l2_subdev_get_try_compose(
2156 subdev, cfg, ssd->sink_pad);
2160 if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2165 sel->r.width = min(sel->r.width, src_size->width);
2166 sel->r.height = min(sel->r.height, src_size->height);
2168 sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width);
2169 sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height);
2171 *crops[sel->pad] = sel->r;
2173 if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2174 smiapp_propagate(subdev, cfg, sel->which,
2180 static void smiapp_get_native_size(struct smiapp_subdev *ssd,
2181 struct v4l2_rect *r)
2185 r->width = SMIA_LIM(ssd->sensor, X_ADDR_MAX) + 1;
2186 r->height = SMIA_LIM(ssd->sensor, Y_ADDR_MAX) + 1;
2189 static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2190 struct v4l2_subdev_pad_config *cfg,
2191 struct v4l2_subdev_selection *sel)
2193 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2194 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2195 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2196 struct v4l2_rect sink_fmt;
2199 ret = __smiapp_sel_supported(subdev, sel);
2203 smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2205 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2206 sink_fmt = ssd->sink_fmt;
2208 struct v4l2_mbus_framefmt *fmt =
2209 v4l2_subdev_get_try_format(subdev, cfg, ssd->sink_pad);
2213 sink_fmt.width = fmt->width;
2214 sink_fmt.height = fmt->height;
2217 switch (sel->target) {
2218 case V4L2_SEL_TGT_CROP_BOUNDS:
2219 case V4L2_SEL_TGT_NATIVE_SIZE:
2220 if (ssd == sensor->pixel_array)
2221 smiapp_get_native_size(ssd, &sel->r);
2222 else if (sel->pad == ssd->sink_pad)
2227 case V4L2_SEL_TGT_CROP:
2228 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2229 sel->r = *crops[sel->pad];
2231 case V4L2_SEL_TGT_COMPOSE:
2239 static int smiapp_get_selection(struct v4l2_subdev *subdev,
2240 struct v4l2_subdev_pad_config *cfg,
2241 struct v4l2_subdev_selection *sel)
2243 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2246 mutex_lock(&sensor->mutex);
2247 rval = __smiapp_get_selection(subdev, cfg, sel);
2248 mutex_unlock(&sensor->mutex);
2252 static int smiapp_set_selection(struct v4l2_subdev *subdev,
2253 struct v4l2_subdev_pad_config *cfg,
2254 struct v4l2_subdev_selection *sel)
2256 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2259 ret = __smiapp_sel_supported(subdev, sel);
2263 mutex_lock(&sensor->mutex);
2265 sel->r.left = max(0, sel->r.left & ~1);
2266 sel->r.top = max(0, sel->r.top & ~1);
2267 sel->r.width = SMIAPP_ALIGN_DIM(sel->r.width, sel->flags);
2268 sel->r.height = SMIAPP_ALIGN_DIM(sel->r.height, sel->flags);
2270 sel->r.width = max_t(unsigned int,
2271 SMIA_LIM(sensor, MIN_X_OUTPUT_SIZE),
2273 sel->r.height = max_t(unsigned int,
2274 SMIA_LIM(sensor, MIN_Y_OUTPUT_SIZE),
2277 switch (sel->target) {
2278 case V4L2_SEL_TGT_CROP:
2279 ret = smiapp_set_crop(subdev, cfg, sel);
2281 case V4L2_SEL_TGT_COMPOSE:
2282 ret = smiapp_set_compose(subdev, cfg, sel);
2288 mutex_unlock(&sensor->mutex);
2292 static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2294 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2296 *frames = sensor->frame_skip;
2300 static int smiapp_get_skip_top_lines(struct v4l2_subdev *subdev, u32 *lines)
2302 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2304 *lines = sensor->image_start;
2309 /* -----------------------------------------------------------------------------
2314 smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2317 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2318 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2319 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2322 if (!sensor->dev_init_done)
2325 rval = smiapp_pm_get_init(sensor);
2329 rval = smiapp_read_nvm(sensor, buf, PAGE_SIZE);
2331 pm_runtime_put(&client->dev);
2332 dev_err(&client->dev, "nvm read failed\n");
2336 pm_runtime_mark_last_busy(&client->dev);
2337 pm_runtime_put_autosuspend(&client->dev);
2340 * NVM is still way below a PAGE_SIZE, so we can safely
2341 * assume this for now.
2345 static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2348 smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
2351 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2352 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2353 struct smiapp_module_info *minfo = &sensor->minfo;
2355 return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2356 minfo->manufacturer_id, minfo->model_id,
2357 minfo->revision_number_major) + 1;
2360 static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);
2362 /* -----------------------------------------------------------------------------
2363 * V4L2 subdev core operations
2366 static int smiapp_identify_module(struct smiapp_sensor *sensor)
2368 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2369 struct smiapp_module_info *minfo = &sensor->minfo;
2373 minfo->name = SMIAPP_NAME;
2376 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2377 &minfo->manufacturer_id);
2379 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2382 rval = smiapp_read_8only(sensor,
2383 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2384 &minfo->revision_number_major);
2386 rval = smiapp_read_8only(sensor,
2387 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2388 &minfo->revision_number_minor);
2390 rval = smiapp_read_8only(sensor,
2391 SMIAPP_REG_U8_MODULE_DATE_YEAR,
2392 &minfo->module_year);
2394 rval = smiapp_read_8only(sensor,
2395 SMIAPP_REG_U8_MODULE_DATE_MONTH,
2396 &minfo->module_month);
2398 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2399 &minfo->module_day);
2403 rval = smiapp_read_8only(sensor,
2404 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2405 &minfo->sensor_manufacturer_id);
2407 rval = smiapp_read_8only(sensor,
2408 SMIAPP_REG_U16_SENSOR_MODEL_ID,
2409 &minfo->sensor_model_id);
2411 rval = smiapp_read_8only(sensor,
2412 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2413 &minfo->sensor_revision_number);
2415 rval = smiapp_read_8only(sensor,
2416 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2417 &minfo->sensor_firmware_version);
2421 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2422 &minfo->smia_version);
2424 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2425 &minfo->smiapp_version);
2428 dev_err(&client->dev, "sensor detection failed\n");
2432 dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2433 minfo->manufacturer_id, minfo->model_id);
2435 dev_dbg(&client->dev,
2436 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2437 minfo->revision_number_major, minfo->revision_number_minor,
2438 minfo->module_year, minfo->module_month, minfo->module_day);
2440 dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2441 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2443 dev_dbg(&client->dev,
2444 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2445 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2447 dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2448 minfo->smia_version, minfo->smiapp_version);
2451 * Some modules have bad data in the lvalues below. Hope the
2452 * rvalues have better stuff. The lvalues are module
2453 * parameters whereas the rvalues are sensor parameters.
2455 if (!minfo->manufacturer_id && !minfo->model_id) {
2456 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2457 minfo->model_id = minfo->sensor_model_id;
2458 minfo->revision_number_major = minfo->sensor_revision_number;
2461 for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2462 if (smiapp_module_idents[i].manufacturer_id
2463 != minfo->manufacturer_id)
2465 if (smiapp_module_idents[i].model_id != minfo->model_id)
2467 if (smiapp_module_idents[i].flags
2468 & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2469 if (smiapp_module_idents[i].revision_number_major
2470 < minfo->revision_number_major)
2473 if (smiapp_module_idents[i].revision_number_major
2474 != minfo->revision_number_major)
2478 minfo->name = smiapp_module_idents[i].name;
2479 minfo->quirk = smiapp_module_idents[i].quirk;
2483 if (i >= ARRAY_SIZE(smiapp_module_idents))
2484 dev_warn(&client->dev,
2485 "no quirks for this module; let's hope it's fully compliant\n");
2487 dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2488 minfo->name, minfo->manufacturer_id, minfo->model_id,
2489 minfo->revision_number_major);
2494 static const struct v4l2_subdev_ops smiapp_ops;
2495 static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2496 static const struct media_entity_operations smiapp_entity_ops;
2498 static int smiapp_register_subdev(struct smiapp_sensor *sensor,
2499 struct smiapp_subdev *ssd,
2500 struct smiapp_subdev *sink_ssd,
2501 u16 source_pad, u16 sink_pad, u32 link_flags)
2503 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2509 rval = media_entity_pads_init(&ssd->sd.entity,
2510 ssd->npads, ssd->pads);
2512 dev_err(&client->dev,
2513 "media_entity_pads_init failed\n");
2517 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2520 dev_err(&client->dev,
2521 "v4l2_device_register_subdev failed\n");
2525 rval = media_create_pad_link(&ssd->sd.entity, source_pad,
2526 &sink_ssd->sd.entity, sink_pad,
2529 dev_err(&client->dev,
2530 "media_create_pad_link failed\n");
2531 v4l2_device_unregister_subdev(&ssd->sd);
2538 static void smiapp_unregistered(struct v4l2_subdev *subdev)
2540 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2543 for (i = 1; i < sensor->ssds_used; i++)
2544 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2547 static int smiapp_registered(struct v4l2_subdev *subdev)
2549 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2552 if (sensor->scaler) {
2553 rval = smiapp_register_subdev(
2554 sensor, sensor->binner, sensor->scaler,
2555 SMIAPP_PAD_SRC, SMIAPP_PAD_SINK,
2556 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2561 rval = smiapp_register_subdev(
2562 sensor, sensor->pixel_array, sensor->binner,
2563 SMIAPP_PA_PAD_SRC, SMIAPP_PAD_SINK,
2564 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2571 smiapp_unregistered(subdev);
2576 static void smiapp_cleanup(struct smiapp_sensor *sensor)
2578 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2580 device_remove_file(&client->dev, &dev_attr_nvm);
2581 device_remove_file(&client->dev, &dev_attr_ident);
2583 smiapp_free_controls(sensor);
2586 static void smiapp_create_subdev(struct smiapp_sensor *sensor,
2587 struct smiapp_subdev *ssd, const char *name,
2588 unsigned short num_pads)
2590 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2595 if (ssd != sensor->src)
2596 v4l2_subdev_init(&ssd->sd, &smiapp_ops);
2598 ssd->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2599 ssd->sensor = sensor;
2601 ssd->npads = num_pads;
2602 ssd->source_pad = num_pads - 1;
2604 v4l2_i2c_subdev_set_name(&ssd->sd, client, sensor->minfo.name, name);
2606 smiapp_get_native_size(ssd, &ssd->sink_fmt);
2608 ssd->compose.width = ssd->sink_fmt.width;
2609 ssd->compose.height = ssd->sink_fmt.height;
2610 ssd->crop[ssd->source_pad] = ssd->compose;
2611 ssd->pads[ssd->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2612 if (ssd != sensor->pixel_array) {
2613 ssd->crop[ssd->sink_pad] = ssd->compose;
2614 ssd->pads[ssd->sink_pad].flags = MEDIA_PAD_FL_SINK;
2617 ssd->sd.entity.ops = &smiapp_entity_ops;
2619 if (ssd == sensor->src)
2622 ssd->sd.internal_ops = &smiapp_internal_ops;
2623 ssd->sd.owner = THIS_MODULE;
2624 ssd->sd.dev = &client->dev;
2625 v4l2_set_subdevdata(&ssd->sd, client);
2628 static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2630 struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2631 struct smiapp_sensor *sensor = ssd->sensor;
2634 mutex_lock(&sensor->mutex);
2636 for (i = 0; i < ssd->npads; i++) {
2637 struct v4l2_mbus_framefmt *try_fmt =
2638 v4l2_subdev_get_try_format(sd, fh->pad, i);
2639 struct v4l2_rect *try_crop =
2640 v4l2_subdev_get_try_crop(sd, fh->pad, i);
2641 struct v4l2_rect *try_comp;
2643 smiapp_get_native_size(ssd, try_crop);
2645 try_fmt->width = try_crop->width;
2646 try_fmt->height = try_crop->height;
2647 try_fmt->code = sensor->internal_csi_format->code;
2648 try_fmt->field = V4L2_FIELD_NONE;
2650 if (ssd != sensor->pixel_array)
2653 try_comp = v4l2_subdev_get_try_compose(sd, fh->pad, i);
2654 *try_comp = *try_crop;
2657 mutex_unlock(&sensor->mutex);
2662 static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2663 .s_stream = smiapp_set_stream,
2666 static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2667 .enum_mbus_code = smiapp_enum_mbus_code,
2668 .get_fmt = smiapp_get_format,
2669 .set_fmt = smiapp_set_format,
2670 .get_selection = smiapp_get_selection,
2671 .set_selection = smiapp_set_selection,
2674 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2675 .g_skip_frames = smiapp_get_skip_frames,
2676 .g_skip_top_lines = smiapp_get_skip_top_lines,
2679 static const struct v4l2_subdev_ops smiapp_ops = {
2680 .video = &smiapp_video_ops,
2681 .pad = &smiapp_pad_ops,
2682 .sensor = &smiapp_sensor_ops,
2685 static const struct media_entity_operations smiapp_entity_ops = {
2686 .link_validate = v4l2_subdev_link_validate,
2689 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2690 .registered = smiapp_registered,
2691 .unregistered = smiapp_unregistered,
2692 .open = smiapp_open,
2695 static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2696 .open = smiapp_open,
2699 /* -----------------------------------------------------------------------------
2703 static int __maybe_unused smiapp_suspend(struct device *dev)
2705 struct i2c_client *client = to_i2c_client(dev);
2706 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2707 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2708 bool streaming = sensor->streaming;
2711 rval = pm_runtime_get_sync(dev);
2713 if (rval != -EBUSY && rval != -EAGAIN)
2714 pm_runtime_set_active(&client->dev);
2715 pm_runtime_put(dev);
2719 if (sensor->streaming)
2720 smiapp_stop_streaming(sensor);
2722 /* save state for resume */
2723 sensor->streaming = streaming;
2728 static int __maybe_unused smiapp_resume(struct device *dev)
2730 struct i2c_client *client = to_i2c_client(dev);
2731 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2732 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2735 pm_runtime_put(dev);
2737 if (sensor->streaming)
2738 rval = smiapp_start_streaming(sensor);
2743 static struct smiapp_hwconfig *smiapp_get_hwconfig(struct device *dev)
2745 struct smiapp_hwconfig *hwcfg;
2746 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
2747 struct fwnode_handle *ep;
2748 struct fwnode_handle *fwnode = dev_fwnode(dev);
2754 return dev->platform_data;
2756 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
2760 bus_cfg.bus_type = V4L2_MBUS_CSI2_DPHY;
2761 rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
2762 if (rval == -ENXIO) {
2763 bus_cfg = (struct v4l2_fwnode_endpoint)
2764 { .bus_type = V4L2_MBUS_CCP2 };
2765 rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
2770 hwcfg = devm_kzalloc(dev, sizeof(*hwcfg), GFP_KERNEL);
2774 switch (bus_cfg.bus_type) {
2775 case V4L2_MBUS_CSI2_DPHY:
2776 hwcfg->csi_signalling_mode = SMIAPP_CSI_SIGNALLING_MODE_CSI2;
2777 hwcfg->lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
2779 case V4L2_MBUS_CCP2:
2780 hwcfg->csi_signalling_mode = (bus_cfg.bus.mipi_csi1.strobe) ?
2781 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE :
2782 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK;
2786 dev_err(dev, "unsupported bus %u\n", bus_cfg.bus_type);
2790 dev_dbg(dev, "lanes %u\n", hwcfg->lanes);
2792 rval = fwnode_property_read_u32(fwnode, "rotation", &rotation);
2796 hwcfg->module_board_orient =
2797 SMIAPP_MODULE_BOARD_ORIENT_180;
2802 dev_err(dev, "invalid rotation %u\n", rotation);
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, "clk %d, mode %d\n", hwcfg->ext_clk,
2813 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)
2843 struct smiapp_sensor *sensor;
2844 struct smiapp_hwconfig *hwcfg = smiapp_get_hwconfig(&client->dev);
2851 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
2855 sensor->hwcfg = hwcfg;
2856 sensor->src = &sensor->ssds[sensor->ssds_used];
2858 v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2859 sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2861 sensor->vana = devm_regulator_get(&client->dev, "vana");
2862 if (IS_ERR(sensor->vana)) {
2863 dev_err(&client->dev, "could not get regulator for vana\n");
2864 return PTR_ERR(sensor->vana);
2867 sensor->ext_clk = devm_clk_get(&client->dev, NULL);
2868 if (PTR_ERR(sensor->ext_clk) == -ENOENT) {
2869 dev_info(&client->dev, "no clock defined, continuing...\n");
2870 sensor->ext_clk = NULL;
2871 } else if (IS_ERR(sensor->ext_clk)) {
2872 dev_err(&client->dev, "could not get clock (%ld)\n",
2873 PTR_ERR(sensor->ext_clk));
2874 return -EPROBE_DEFER;
2877 if (sensor->ext_clk) {
2878 if (sensor->hwcfg->ext_clk) {
2881 rval = clk_set_rate(sensor->ext_clk,
2882 sensor->hwcfg->ext_clk);
2884 dev_err(&client->dev,
2885 "unable to set clock freq to %u\n",
2886 sensor->hwcfg->ext_clk);
2890 rate = clk_get_rate(sensor->ext_clk);
2891 if (rate != sensor->hwcfg->ext_clk) {
2892 dev_err(&client->dev,
2893 "can't set clock freq, asked for %u but got %lu\n",
2894 sensor->hwcfg->ext_clk, rate);
2898 sensor->hwcfg->ext_clk = clk_get_rate(sensor->ext_clk);
2899 dev_dbg(&client->dev, "obtained clock freq %u\n",
2900 sensor->hwcfg->ext_clk);
2902 } else if (sensor->hwcfg->ext_clk) {
2903 dev_dbg(&client->dev, "assuming clock freq %u\n",
2904 sensor->hwcfg->ext_clk);
2906 dev_err(&client->dev, "unable to obtain clock freq\n");
2910 sensor->xshutdown = devm_gpiod_get_optional(&client->dev, "xshutdown",
2912 if (IS_ERR(sensor->xshutdown))
2913 return PTR_ERR(sensor->xshutdown);
2915 rval = smiapp_power_on(&client->dev);
2919 mutex_init(&sensor->mutex);
2921 rval = smiapp_identify_module(sensor);
2927 rval = smiapp_read_all_smia_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 (SMIA_LIM(sensor, 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");
2999 if (sensor->minfo.smiapp_version &&
3000 SMIA_LIM(sensor, DATA_TRANSFER_IF_CAPABILITY) &
3001 SMIAPP_DATA_TRANSFER_IF_CAPABILITY_SUPPORTED) {
3002 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
3003 dev_err(&client->dev, "sysfs nvm entry failed\n");
3009 /* We consider this as profile 0 sensor if any of these are zero. */
3010 if (!SMIA_LIM(sensor, MIN_OP_SYS_CLK_DIV) ||
3011 !SMIA_LIM(sensor, MAX_OP_SYS_CLK_DIV) ||
3012 !SMIA_LIM(sensor, MIN_OP_PIX_CLK_DIV) ||
3013 !SMIA_LIM(sensor, MAX_OP_PIX_CLK_DIV)) {
3014 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
3015 } else if (SMIA_LIM(sensor, SCALING_CAPABILITY)
3016 != SMIAPP_SCALING_CAPABILITY_NONE) {
3017 if (SMIA_LIM(sensor, SCALING_CAPABILITY)
3018 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
3019 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
3021 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
3022 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3023 sensor->ssds_used++;
3024 } else if (SMIA_LIM(sensor, DIGITAL_CROP_CAPABILITY)
3025 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
3026 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3027 sensor->ssds_used++;
3029 sensor->binner = &sensor->ssds[sensor->ssds_used];
3030 sensor->ssds_used++;
3031 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
3032 sensor->ssds_used++;
3034 sensor->scale_m = SMIA_LIM(sensor, SCALER_N_MIN);
3036 /* prepare PLL configuration input values */
3037 sensor->pll.bus_type = SMIAPP_PLL_BUS_TYPE_CSI2;
3038 sensor->pll.csi2.lanes = sensor->hwcfg->lanes;
3039 sensor->pll.ext_clk_freq_hz = sensor->hwcfg->ext_clk;
3040 sensor->pll.scale_n = SMIA_LIM(sensor, SCALER_N_MIN);
3041 /* Profile 0 sensors have no separate OP clock branch. */
3042 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
3043 sensor->pll.flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
3045 smiapp_create_subdev(sensor, sensor->scaler, " scaler", 2);
3046 smiapp_create_subdev(sensor, sensor->binner, " binner", 2);
3047 smiapp_create_subdev(sensor, sensor->pixel_array, " pixel_array", 1);
3049 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
3051 sensor->pixel_array->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3053 rval = smiapp_init_controls(sensor);
3057 rval = smiapp_call_quirk(sensor, init);
3061 rval = smiapp_get_mbus_formats(sensor);
3067 rval = smiapp_init_late_controls(sensor);
3073 mutex_lock(&sensor->mutex);
3074 rval = smiapp_pll_blanking_update(sensor);
3075 mutex_unlock(&sensor->mutex);
3077 dev_err(&client->dev, "update mode failed\n");
3081 sensor->streaming = false;
3082 sensor->dev_init_done = true;
3084 rval = media_entity_pads_init(&sensor->src->sd.entity, 2,
3087 goto out_media_entity_cleanup;
3089 pm_runtime_set_active(&client->dev);
3090 pm_runtime_get_noresume(&client->dev);
3091 pm_runtime_enable(&client->dev);
3093 rval = v4l2_async_register_subdev_sensor_common(&sensor->src->sd);
3095 goto out_disable_runtime_pm;
3097 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
3098 pm_runtime_use_autosuspend(&client->dev);
3099 pm_runtime_put_autosuspend(&client->dev);
3103 out_disable_runtime_pm:
3104 pm_runtime_put_noidle(&client->dev);
3105 pm_runtime_disable(&client->dev);
3107 out_media_entity_cleanup:
3108 media_entity_cleanup(&sensor->src->sd.entity);
3111 smiapp_cleanup(sensor);
3114 smiapp_power_off(&client->dev);
3115 mutex_destroy(&sensor->mutex);
3120 static int smiapp_remove(struct i2c_client *client)
3122 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3123 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
3126 v4l2_async_unregister_subdev(subdev);
3128 pm_runtime_disable(&client->dev);
3129 if (!pm_runtime_status_suspended(&client->dev))
3130 smiapp_power_off(&client->dev);
3131 pm_runtime_set_suspended(&client->dev);
3133 for (i = 0; i < sensor->ssds_used; i++) {
3134 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
3135 media_entity_cleanup(&sensor->ssds[i].sd.entity);
3137 smiapp_cleanup(sensor);
3138 mutex_destroy(&sensor->mutex);
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_new = 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 v2");