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 int smiapp_read_frame_fmt(struct smiapp_sensor *sensor)
62 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
63 u32 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc;
69 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE,
74 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE,
79 ncol_desc = (fmt_model_subtype
80 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK)
81 >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT;
82 nrow_desc = fmt_model_subtype
83 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK;
85 dev_dbg(&client->dev, "format_model_type %s\n",
86 fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
88 fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
89 ? "4 byte" : "is simply bad");
91 for (i = 0; i < ncol_desc + nrow_desc; i++) {
99 if (fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE) {
100 reg = SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i);
101 rval = smiapp_read(sensor, reg, &desc);
107 & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK)
108 >> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT;
109 pixels = desc & SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK;
110 } else if (fmt_model_type
111 == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE) {
112 reg = SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i);
113 rval = smiapp_read(sensor, reg, &desc);
119 & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK)
120 >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT;
121 pixels = desc & SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK;
123 dev_dbg(&client->dev,
124 "invalid frame format model type %d\n",
135 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
138 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY:
141 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK:
144 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK:
147 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
155 dev_dbg(&client->dev,
156 "0x%8.8x %s pixels: %d %s (pixelcode %u)\n", reg,
157 what, pixels, which, pixelcode);
161 SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE)
162 sensor->visible_pixel_start = pixel_count;
163 pixel_count += pixels;
167 /* Handle row descriptors */
169 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
170 if (sensor->embedded_end)
172 sensor->embedded_start = line_count;
173 sensor->embedded_end = line_count + pixels;
175 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
176 sensor->image_start = line_count;
179 line_count += pixels;
182 if (sensor->embedded_end > sensor->image_start) {
183 dev_dbg(&client->dev,
184 "adjusting image start line to %u (was %u)\n",
185 sensor->embedded_end, sensor->image_start);
186 sensor->image_start = sensor->embedded_end;
189 dev_dbg(&client->dev, "embedded data from lines %d to %d\n",
190 sensor->embedded_start, sensor->embedded_end);
191 dev_dbg(&client->dev, "image data starts at line %d\n",
192 sensor->image_start);
197 static int smiapp_pll_configure(struct smiapp_sensor *sensor)
199 struct smiapp_pll *pll = &sensor->pll;
203 sensor, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt.pix_clk_div);
208 sensor, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt.sys_clk_div);
213 sensor, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div);
218 sensor, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier);
222 /* Lane op clock ratio does not apply here. */
224 sensor, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS,
225 DIV_ROUND_UP(pll->op.sys_clk_freq_hz, 1000000 / 256 / 256));
226 if (rval < 0 || sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
230 sensor, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op.pix_clk_div);
235 sensor, SMIAPP_REG_U16_OP_SYS_CLK_DIV, pll->op.sys_clk_div);
238 static int smiapp_pll_try(struct smiapp_sensor *sensor,
239 struct smiapp_pll *pll)
241 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
242 struct smiapp_pll_limits lim = {
243 .min_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV],
244 .max_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV],
245 .min_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ],
246 .max_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ],
247 .min_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER],
248 .max_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER],
249 .min_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ],
250 .max_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ],
252 .op.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV],
253 .op.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV],
254 .op.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV],
255 .op.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV],
256 .op.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ],
257 .op.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ],
258 .op.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ],
259 .op.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ],
261 .vt.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV],
262 .vt.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV],
263 .vt.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV],
264 .vt.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV],
265 .vt.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ],
266 .vt.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ],
267 .vt.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ],
268 .vt.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ],
270 .min_line_length_pck_bin = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN],
271 .min_line_length_pck = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK],
274 return smiapp_pll_calculate(&client->dev, &lim, pll);
277 static int smiapp_pll_update(struct smiapp_sensor *sensor)
279 struct smiapp_pll *pll = &sensor->pll;
282 pll->binning_horizontal = sensor->binning_horizontal;
283 pll->binning_vertical = sensor->binning_vertical;
285 sensor->link_freq->qmenu_int[sensor->link_freq->val];
286 pll->scale_m = sensor->scale_m;
287 pll->bits_per_pixel = sensor->csi_format->compressed;
289 rval = smiapp_pll_try(sensor, pll);
293 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_parray,
294 pll->pixel_rate_pixel_array);
295 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_csi, pll->pixel_rate_csi);
303 * V4L2 Controls handling
307 static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor)
309 struct v4l2_ctrl *ctrl = sensor->exposure;
312 max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
313 + sensor->vblank->val
314 - sensor->limits[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN];
316 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, ctrl->step, max);
322 * 1. Bits-per-pixel, descending.
323 * 2. Bits-per-pixel compressed, descending.
324 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
325 * orders must be defined.
327 static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = {
328 { MEDIA_BUS_FMT_SGRBG16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_GRBG, },
329 { MEDIA_BUS_FMT_SRGGB16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_RGGB, },
330 { MEDIA_BUS_FMT_SBGGR16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_BGGR, },
331 { MEDIA_BUS_FMT_SGBRG16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_GBRG, },
332 { MEDIA_BUS_FMT_SGRBG14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_GRBG, },
333 { MEDIA_BUS_FMT_SRGGB14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_RGGB, },
334 { MEDIA_BUS_FMT_SBGGR14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_BGGR, },
335 { MEDIA_BUS_FMT_SGBRG14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_GBRG, },
336 { MEDIA_BUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, },
337 { MEDIA_BUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, },
338 { MEDIA_BUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, },
339 { MEDIA_BUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, },
340 { MEDIA_BUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, },
341 { MEDIA_BUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, },
342 { MEDIA_BUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, },
343 { MEDIA_BUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, },
344 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, },
345 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, },
346 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, },
347 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, },
348 { MEDIA_BUS_FMT_SGRBG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GRBG, },
349 { MEDIA_BUS_FMT_SRGGB8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_RGGB, },
350 { MEDIA_BUS_FMT_SBGGR8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_BGGR, },
351 { MEDIA_BUS_FMT_SGBRG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GBRG, },
354 static const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
356 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
357 - (unsigned long)smiapp_csi_data_formats) \
358 / sizeof(*smiapp_csi_data_formats))
360 static u32 smiapp_pixel_order(struct smiapp_sensor *sensor)
362 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
366 if (sensor->hflip->val)
367 flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
369 if (sensor->vflip->val)
370 flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
373 flip ^= sensor->hvflip_inv_mask;
375 dev_dbg(&client->dev, "flip %d\n", flip);
376 return sensor->default_pixel_order ^ flip;
379 static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor)
381 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
382 unsigned int csi_format_idx =
383 to_csi_format_idx(sensor->csi_format) & ~3;
384 unsigned int internal_csi_format_idx =
385 to_csi_format_idx(sensor->internal_csi_format) & ~3;
386 unsigned int pixel_order = smiapp_pixel_order(sensor);
388 sensor->mbus_frame_fmts =
389 sensor->default_mbus_frame_fmts << pixel_order;
391 &smiapp_csi_data_formats[csi_format_idx + pixel_order];
392 sensor->internal_csi_format =
393 &smiapp_csi_data_formats[internal_csi_format_idx
396 BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order
397 >= ARRAY_SIZE(smiapp_csi_data_formats));
399 dev_dbg(&client->dev, "new pixel order %s\n",
400 pixel_order_str[pixel_order]);
403 static const char * const smiapp_test_patterns[] = {
406 "Eight Vertical Colour Bars",
407 "Colour Bars With Fade to Grey",
408 "Pseudorandom Sequence (PN9)",
411 static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl)
413 struct smiapp_sensor *sensor =
414 container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler)
421 case V4L2_CID_ANALOGUE_GAIN:
424 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
426 case V4L2_CID_EXPOSURE:
429 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);
433 if (sensor->streaming)
436 if (sensor->hflip->val)
437 orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
439 if (sensor->vflip->val)
440 orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
442 orient ^= sensor->hvflip_inv_mask;
443 rval = smiapp_write(sensor, SMIAPP_REG_U8_IMAGE_ORIENTATION,
448 smiapp_update_mbus_formats(sensor);
452 case V4L2_CID_VBLANK:
453 exposure = sensor->exposure->val;
455 __smiapp_update_exposure_limits(sensor);
457 if (exposure > sensor->exposure->maximum) {
458 sensor->exposure->val = sensor->exposure->maximum;
459 rval = smiapp_set_ctrl(sensor->exposure);
465 sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
466 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
469 case V4L2_CID_HBLANK:
471 sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
472 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
475 case V4L2_CID_LINK_FREQ:
476 if (sensor->streaming)
479 return smiapp_pll_update(sensor);
481 case V4L2_CID_TEST_PATTERN: {
484 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
486 sensor->test_data[i],
488 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR);
491 sensor, SMIAPP_REG_U16_TEST_PATTERN_MODE, ctrl->val);
494 case V4L2_CID_TEST_PATTERN_RED:
496 sensor, SMIAPP_REG_U16_TEST_DATA_RED, ctrl->val);
498 case V4L2_CID_TEST_PATTERN_GREENR:
500 sensor, SMIAPP_REG_U16_TEST_DATA_GREENR, ctrl->val);
502 case V4L2_CID_TEST_PATTERN_BLUE:
504 sensor, SMIAPP_REG_U16_TEST_DATA_BLUE, ctrl->val);
506 case V4L2_CID_TEST_PATTERN_GREENB:
508 sensor, SMIAPP_REG_U16_TEST_DATA_GREENB, ctrl->val);
510 case V4L2_CID_PIXEL_RATE:
511 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
519 static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
520 .s_ctrl = smiapp_set_ctrl,
523 static int smiapp_init_controls(struct smiapp_sensor *sensor)
525 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
528 rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 12);
532 sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
534 sensor->analog_gain = v4l2_ctrl_new_std(
535 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
536 V4L2_CID_ANALOGUE_GAIN,
537 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN],
538 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX],
539 max(sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP], 1U),
540 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN]);
542 /* Exposure limits will be updated soon, use just something here. */
543 sensor->exposure = v4l2_ctrl_new_std(
544 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
545 V4L2_CID_EXPOSURE, 0, 0, 1, 0);
547 sensor->hflip = v4l2_ctrl_new_std(
548 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
549 V4L2_CID_HFLIP, 0, 1, 1, 0);
550 sensor->vflip = v4l2_ctrl_new_std(
551 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
552 V4L2_CID_VFLIP, 0, 1, 1, 0);
554 sensor->vblank = v4l2_ctrl_new_std(
555 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
556 V4L2_CID_VBLANK, 0, 1, 1, 0);
559 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
561 sensor->hblank = v4l2_ctrl_new_std(
562 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
563 V4L2_CID_HBLANK, 0, 1, 1, 0);
566 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
568 sensor->pixel_rate_parray = v4l2_ctrl_new_std(
569 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
570 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
572 v4l2_ctrl_new_std_menu_items(&sensor->pixel_array->ctrl_handler,
573 &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN,
574 ARRAY_SIZE(smiapp_test_patterns) - 1,
575 0, 0, smiapp_test_patterns);
577 if (sensor->pixel_array->ctrl_handler.error) {
578 dev_err(&client->dev,
579 "pixel array controls initialization failed (%d)\n",
580 sensor->pixel_array->ctrl_handler.error);
581 return sensor->pixel_array->ctrl_handler.error;
584 sensor->pixel_array->sd.ctrl_handler =
585 &sensor->pixel_array->ctrl_handler;
587 v4l2_ctrl_cluster(2, &sensor->hflip);
589 rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
593 sensor->src->ctrl_handler.lock = &sensor->mutex;
595 sensor->pixel_rate_csi = v4l2_ctrl_new_std(
596 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
597 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
599 if (sensor->src->ctrl_handler.error) {
600 dev_err(&client->dev,
601 "src controls initialization failed (%d)\n",
602 sensor->src->ctrl_handler.error);
603 return sensor->src->ctrl_handler.error;
606 sensor->src->sd.ctrl_handler = &sensor->src->ctrl_handler;
612 * For controls that require information on available media bus codes
613 * and linke frequencies.
615 static int smiapp_init_late_controls(struct smiapp_sensor *sensor)
617 unsigned long *valid_link_freqs = &sensor->valid_link_freqs[
618 sensor->csi_format->compressed - sensor->compressed_min_bpp];
621 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) {
622 int max_value = (1 << sensor->csi_format->width) - 1;
624 sensor->test_data[i] = v4l2_ctrl_new_std(
625 &sensor->pixel_array->ctrl_handler,
626 &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN_RED + i,
627 0, max_value, 1, max_value);
630 sensor->link_freq = v4l2_ctrl_new_int_menu(
631 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
632 V4L2_CID_LINK_FREQ, __fls(*valid_link_freqs),
633 __ffs(*valid_link_freqs), sensor->hwcfg->op_sys_clock);
635 return sensor->src->ctrl_handler.error;
638 static void smiapp_free_controls(struct smiapp_sensor *sensor)
642 for (i = 0; i < sensor->ssds_used; i++)
643 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
646 static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit,
649 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
654 for (i = 0; i < n; i++) {
656 sensor, smiapp_reg_limits[limit[i]].addr, &val);
659 sensor->limits[limit[i]] = val;
660 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %u, 0x%x\n",
661 smiapp_reg_limits[limit[i]].addr,
662 smiapp_reg_limits[limit[i]].what, val, val);
668 static int smiapp_get_all_limits(struct smiapp_sensor *sensor)
673 for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
674 rval = smiapp_get_limits(sensor, &i, 1);
679 if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0)
680 smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);
685 static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
687 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
688 struct smiapp_pll *pll = &sensor->pll;
689 u8 compressed_max_bpp = 0;
690 unsigned int type, n;
691 unsigned int i, pixel_order;
695 sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
699 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
701 rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
706 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
707 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
711 dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
712 pixel_order_str[pixel_order]);
715 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
716 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
718 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
719 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
725 sensor->default_pixel_order = pixel_order;
726 sensor->mbus_frame_fmts = 0;
728 for (i = 0; i < n; i++) {
733 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
737 dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n",
738 i, fmt >> 8, (u8)fmt);
740 for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
741 const struct smiapp_csi_data_format *f =
742 &smiapp_csi_data_formats[j];
744 if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
747 if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
750 dev_dbg(&client->dev, "jolly good! %d\n", j);
752 sensor->default_mbus_frame_fmts |= 1 << j;
756 /* Figure out which BPP values can be used with which formats. */
757 pll->binning_horizontal = 1;
758 pll->binning_vertical = 1;
759 pll->scale_m = sensor->scale_m;
761 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
762 sensor->compressed_min_bpp =
763 min(smiapp_csi_data_formats[i].compressed,
764 sensor->compressed_min_bpp);
766 max(smiapp_csi_data_formats[i].compressed,
770 sensor->valid_link_freqs = devm_kcalloc(
772 compressed_max_bpp - sensor->compressed_min_bpp + 1,
773 sizeof(*sensor->valid_link_freqs), GFP_KERNEL);
774 if (!sensor->valid_link_freqs)
777 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
778 const struct smiapp_csi_data_format *f =
779 &smiapp_csi_data_formats[i];
780 unsigned long *valid_link_freqs =
781 &sensor->valid_link_freqs[
782 f->compressed - sensor->compressed_min_bpp];
785 if (!(sensor->default_mbus_frame_fmts & 1 << i))
788 pll->bits_per_pixel = f->compressed;
790 for (j = 0; sensor->hwcfg->op_sys_clock[j]; j++) {
791 pll->link_freq = sensor->hwcfg->op_sys_clock[j];
793 rval = smiapp_pll_try(sensor, pll);
794 dev_dbg(&client->dev, "link freq %u Hz, bpp %u %s\n",
795 pll->link_freq, pll->bits_per_pixel,
796 rval ? "not ok" : "ok");
800 set_bit(j, valid_link_freqs);
803 if (!*valid_link_freqs) {
804 dev_info(&client->dev,
805 "no valid link frequencies for %u bpp\n",
807 sensor->default_mbus_frame_fmts &= ~BIT(i);
811 if (!sensor->csi_format
812 || f->width > sensor->csi_format->width
813 || (f->width == sensor->csi_format->width
814 && f->compressed > sensor->csi_format->compressed)) {
815 sensor->csi_format = f;
816 sensor->internal_csi_format = f;
820 if (!sensor->csi_format) {
821 dev_err(&client->dev, "no supported mbus code found\n");
825 smiapp_update_mbus_formats(sensor);
830 static void smiapp_update_blanking(struct smiapp_sensor *sensor)
832 struct v4l2_ctrl *vblank = sensor->vblank;
833 struct v4l2_ctrl *hblank = sensor->hblank;
834 uint16_t min_fll, max_fll, min_llp, max_llp, min_lbp;
837 if (sensor->binning_vertical > 1 || sensor->binning_horizontal > 1) {
838 min_fll = sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN];
839 max_fll = sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN];
840 min_llp = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN];
841 max_llp = sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN];
842 min_lbp = sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN];
844 min_fll = sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES];
845 max_fll = sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES];
846 min_llp = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK];
847 max_llp = sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK];
848 min_lbp = sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK];
852 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
854 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
855 max = max_fll - sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
857 __v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min);
861 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
863 max = max_llp - sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
865 __v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min);
867 __smiapp_update_exposure_limits(sensor);
870 static int smiapp_pll_blanking_update(struct smiapp_sensor *sensor)
872 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
875 rval = smiapp_pll_update(sensor);
879 /* Output from pixel array, including blanking */
880 smiapp_update_blanking(sensor);
882 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
883 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
885 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
886 sensor->pll.pixel_rate_pixel_array /
887 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
888 + sensor->hblank->val) *
889 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
890 + sensor->vblank->val) / 100));
897 * SMIA++ NVM handling
901 static int smiapp_read_nvm_page(struct smiapp_sensor *sensor, u32 p, u8 *nvm,
910 rval = smiapp_write(sensor,
911 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
915 rval = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
916 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN);
920 rval = smiapp_read(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS,
925 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_EUSAGE) {
930 if (sensor->limits[SMIAPP_LIMIT_DATA_TRANSFER_IF_CAPABILITY] &
931 SMIAPP_DATA_TRANSFER_IF_CAPABILITY_POLL) {
932 for (i = 1000; i > 0; i--) {
933 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
938 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS,
949 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
952 rval = smiapp_read(sensor,
953 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
964 static int smiapp_read_nvm(struct smiapp_sensor *sensor, unsigned char *nvm,
971 for (p = 0; p < nvm_size / SMIAPP_NVM_PAGE_SIZE && !rval; p++) {
972 rval = smiapp_read_nvm_page(sensor, p, nvm, &status);
973 nvm += SMIAPP_NVM_PAGE_SIZE;
976 if (rval == -ENODATA &&
977 status & SMIAPP_DATA_TRANSFER_IF_1_STATUS_EUSAGE)
980 rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
984 return rval2 ?: p * SMIAPP_NVM_PAGE_SIZE;
989 * SMIA++ CCI address control
992 static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
994 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
998 client->addr = sensor->hwcfg->i2c_addr_dfl;
1000 rval = smiapp_write(sensor,
1001 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
1002 sensor->hwcfg->i2c_addr_alt << 1);
1006 client->addr = sensor->hwcfg->i2c_addr_alt;
1008 /* verify addr change went ok */
1009 rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
1013 if (val != sensor->hwcfg->i2c_addr_alt << 1)
1021 * SMIA++ Mode Control
1024 static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
1026 struct smiapp_flash_strobe_parms *strobe_setup;
1027 unsigned int ext_freq = sensor->hwcfg->ext_clk;
1029 u32 strobe_adjustment;
1030 u32 strobe_width_high_rs;
1033 strobe_setup = sensor->hwcfg->strobe_setup;
1036 * How to calculate registers related to strobe length. Please
1037 * do not change, or if you do at least know what you're
1040 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1042 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1043 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1045 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1046 * flash_strobe_adjustment E N, [1 - 0xff]
1048 * The formula above is written as below to keep it on one
1051 * l / 10^6 = w / e * a
1053 * Let's mark w * a by x:
1061 * The strobe width must be at least as long as requested,
1062 * thus rounding upwards is needed.
1064 * x = (l * e + 10^6 - 1) / 10^6
1065 * -----------------------------
1067 * Maximum possible accuracy is wanted at all times. Thus keep
1068 * a as small as possible.
1070 * Calculate a, assuming maximum w, with rounding upwards:
1072 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1073 * -------------------------------------
1075 * Thus, we also get w, with that a, with rounding upwards:
1077 * w = (x + a - 1) / a
1078 * -------------------
1082 * x E [1, (2^16 - 1) * (2^8 - 1)]
1084 * Substituting maximum x to the original formula (with rounding),
1085 * the maximum l is thus
1087 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1089 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1090 * --------------------------------------------------
1092 * flash_strobe_length must be clamped between 1 and
1093 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1097 * flash_strobe_adjustment = ((flash_strobe_length *
1098 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1100 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1101 * EXTCLK freq + 10^6 - 1) / 10^6 +
1102 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1104 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1105 1000000 + 1, ext_freq);
1106 strobe_setup->strobe_width_high_us =
1107 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1109 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1110 1000000 - 1), 1000000ULL);
1111 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1112 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1115 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
1116 strobe_setup->mode);
1120 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
1125 rval = smiapp_write(
1126 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1127 strobe_width_high_rs);
1131 rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
1132 strobe_setup->strobe_delay);
1136 rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
1137 strobe_setup->stobe_start_point);
1141 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
1142 strobe_setup->trigger);
1145 sensor->hwcfg->strobe_setup->trigger = 0;
1150 /* -----------------------------------------------------------------------------
1154 static int smiapp_power_on(struct device *dev)
1156 struct i2c_client *client = to_i2c_client(dev);
1157 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1158 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1160 * The sub-device related to the I2C device is always the
1161 * source one, i.e. ssds[0].
1163 struct smiapp_sensor *sensor =
1164 container_of(ssd, struct smiapp_sensor, ssds[0]);
1168 rval = regulator_enable(sensor->vana);
1170 dev_err(&client->dev, "failed to enable vana regulator\n");
1173 usleep_range(1000, 1000);
1175 rval = clk_prepare_enable(sensor->ext_clk);
1177 dev_dbg(&client->dev, "failed to enable xclk\n");
1180 usleep_range(1000, 1000);
1182 gpiod_set_value(sensor->xshutdown, 1);
1184 sleep = SMIAPP_RESET_DELAY(sensor->hwcfg->ext_clk);
1185 usleep_range(sleep, sleep);
1187 mutex_lock(&sensor->mutex);
1189 sensor->active = true;
1192 * Failures to respond to the address change command have been noticed.
1193 * Those failures seem to be caused by the sensor requiring a longer
1194 * boot time than advertised. An additional 10ms delay seems to work
1195 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1196 * unnecessary. The failures need to be investigated to find a proper
1197 * fix, and a delay will likely need to be added here if the I2C write
1198 * retry hack is reverted before the root cause of the boot time issue
1202 if (sensor->hwcfg->i2c_addr_alt) {
1203 rval = smiapp_change_cci_addr(sensor);
1205 dev_err(&client->dev, "cci address change error\n");
1206 goto out_cci_addr_fail;
1210 rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
1211 SMIAPP_SOFTWARE_RESET);
1213 dev_err(&client->dev, "software reset failed\n");
1214 goto out_cci_addr_fail;
1217 if (sensor->hwcfg->i2c_addr_alt) {
1218 rval = smiapp_change_cci_addr(sensor);
1220 dev_err(&client->dev, "cci address change error\n");
1221 goto out_cci_addr_fail;
1225 rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
1226 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1228 dev_err(&client->dev, "compression mode set failed\n");
1229 goto out_cci_addr_fail;
1232 rval = smiapp_write(
1233 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1234 sensor->hwcfg->ext_clk / (1000000 / (1 << 8)));
1236 dev_err(&client->dev, "extclk frequency set failed\n");
1237 goto out_cci_addr_fail;
1240 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
1241 sensor->hwcfg->lanes - 1);
1243 dev_err(&client->dev, "csi lane mode set failed\n");
1244 goto out_cci_addr_fail;
1247 rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
1248 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1250 dev_err(&client->dev, "fast standby set failed\n");
1251 goto out_cci_addr_fail;
1254 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1255 sensor->hwcfg->csi_signalling_mode);
1257 dev_err(&client->dev, "csi signalling mode set failed\n");
1258 goto out_cci_addr_fail;
1261 /* DPHY control done by sensor based on requested link rate */
1262 rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
1263 SMIAPP_DPHY_CTRL_UI);
1265 goto out_cci_addr_fail;
1267 rval = smiapp_call_quirk(sensor, post_poweron);
1269 dev_err(&client->dev, "post_poweron quirks failed\n");
1270 goto out_cci_addr_fail;
1273 /* Are we still initialising...? If not, proceed with control setup. */
1274 if (sensor->pixel_array) {
1275 rval = __v4l2_ctrl_handler_setup(
1276 &sensor->pixel_array->ctrl_handler);
1278 goto out_cci_addr_fail;
1280 rval = __v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1282 goto out_cci_addr_fail;
1285 mutex_unlock(&sensor->mutex);
1290 mutex_unlock(&sensor->mutex);
1291 gpiod_set_value(sensor->xshutdown, 0);
1292 clk_disable_unprepare(sensor->ext_clk);
1295 regulator_disable(sensor->vana);
1300 static int smiapp_power_off(struct device *dev)
1302 struct i2c_client *client = to_i2c_client(dev);
1303 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1304 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1305 struct smiapp_sensor *sensor =
1306 container_of(ssd, struct smiapp_sensor, ssds[0]);
1308 mutex_lock(&sensor->mutex);
1311 * Currently power/clock to lens are enable/disabled separately
1312 * but they are essentially the same signals. So if the sensor is
1313 * powered off while the lens is powered on the sensor does not
1314 * really see a power off and next time the cci address change
1315 * will fail. So do a soft reset explicitly here.
1317 if (sensor->hwcfg->i2c_addr_alt)
1318 smiapp_write(sensor,
1319 SMIAPP_REG_U8_SOFTWARE_RESET,
1320 SMIAPP_SOFTWARE_RESET);
1322 sensor->active = false;
1324 mutex_unlock(&sensor->mutex);
1326 gpiod_set_value(sensor->xshutdown, 0);
1327 clk_disable_unprepare(sensor->ext_clk);
1328 usleep_range(5000, 5000);
1329 regulator_disable(sensor->vana);
1330 sensor->streaming = false;
1335 /* -----------------------------------------------------------------------------
1336 * Video stream management
1339 static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1341 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1342 unsigned int binning_mode;
1345 mutex_lock(&sensor->mutex);
1347 rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
1348 (sensor->csi_format->width << 8) |
1349 sensor->csi_format->compressed);
1353 /* Binning configuration */
1354 if (sensor->binning_horizontal == 1 &&
1355 sensor->binning_vertical == 1) {
1359 (sensor->binning_horizontal << 4)
1360 | sensor->binning_vertical;
1362 rval = smiapp_write(
1363 sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
1369 rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
1374 rval = smiapp_pll_configure(sensor);
1378 /* Analog crop start coordinates */
1379 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
1380 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1384 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
1385 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1389 /* Analog crop end coordinates */
1390 rval = smiapp_write(
1391 sensor, SMIAPP_REG_U16_X_ADDR_END,
1392 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1393 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1397 rval = smiapp_write(
1398 sensor, SMIAPP_REG_U16_Y_ADDR_END,
1399 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1400 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1405 * Output from pixel array, including blanking, is set using
1406 * controls below. No need to set here.
1410 if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1411 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1412 rval = smiapp_write(
1413 sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
1414 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1418 rval = smiapp_write(
1419 sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
1420 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1424 rval = smiapp_write(
1425 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
1426 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1430 rval = smiapp_write(
1431 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
1432 sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1438 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1439 != SMIAPP_SCALING_CAPABILITY_NONE) {
1440 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
1441 sensor->scaling_mode);
1445 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
1451 /* Output size from sensor */
1452 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
1453 sensor->src->crop[SMIAPP_PAD_SRC].width);
1456 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
1457 sensor->src->crop[SMIAPP_PAD_SRC].height);
1461 if ((sensor->limits[SMIAPP_LIMIT_FLASH_MODE_CAPABILITY] &
1462 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1463 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1464 sensor->hwcfg->strobe_setup != NULL &&
1465 sensor->hwcfg->strobe_setup->trigger != 0) {
1466 rval = smiapp_setup_flash_strobe(sensor);
1471 rval = smiapp_call_quirk(sensor, pre_streamon);
1473 dev_err(&client->dev, "pre_streamon quirks failed\n");
1477 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1478 SMIAPP_MODE_SELECT_STREAMING);
1481 mutex_unlock(&sensor->mutex);
1486 static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1488 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1491 mutex_lock(&sensor->mutex);
1492 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1493 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1497 rval = smiapp_call_quirk(sensor, post_streamoff);
1499 dev_err(&client->dev, "post_streamoff quirks failed\n");
1502 mutex_unlock(&sensor->mutex);
1506 /* -----------------------------------------------------------------------------
1507 * V4L2 subdev video operations
1510 static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1512 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1513 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1516 if (sensor->streaming == enable)
1520 rval = pm_runtime_get_sync(&client->dev);
1522 if (rval != -EBUSY && rval != -EAGAIN)
1523 pm_runtime_set_active(&client->dev);
1524 pm_runtime_put(&client->dev);
1528 sensor->streaming = true;
1530 rval = smiapp_start_streaming(sensor);
1532 sensor->streaming = false;
1534 rval = smiapp_stop_streaming(sensor);
1535 sensor->streaming = false;
1536 pm_runtime_mark_last_busy(&client->dev);
1537 pm_runtime_put_autosuspend(&client->dev);
1543 static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1544 struct v4l2_subdev_pad_config *cfg,
1545 struct v4l2_subdev_mbus_code_enum *code)
1547 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1548 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1553 mutex_lock(&sensor->mutex);
1555 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1556 subdev->name, code->pad, code->index);
1558 if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1562 code->code = sensor->internal_csi_format->code;
1567 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1568 if (sensor->mbus_frame_fmts & (1 << i))
1571 if (idx == code->index) {
1572 code->code = smiapp_csi_data_formats[i].code;
1573 dev_err(&client->dev, "found index %d, i %d, code %x\n",
1574 code->index, i, code->code);
1581 mutex_unlock(&sensor->mutex);
1586 static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1589 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1591 if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1592 return sensor->csi_format->code;
1594 return sensor->internal_csi_format->code;
1597 static int __smiapp_get_format(struct v4l2_subdev *subdev,
1598 struct v4l2_subdev_pad_config *cfg,
1599 struct v4l2_subdev_format *fmt)
1601 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1603 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1604 fmt->format = *v4l2_subdev_get_try_format(subdev, cfg,
1607 struct v4l2_rect *r;
1609 if (fmt->pad == ssd->source_pad)
1610 r = &ssd->crop[ssd->source_pad];
1614 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1615 fmt->format.width = r->width;
1616 fmt->format.height = r->height;
1617 fmt->format.field = V4L2_FIELD_NONE;
1623 static int smiapp_get_format(struct v4l2_subdev *subdev,
1624 struct v4l2_subdev_pad_config *cfg,
1625 struct v4l2_subdev_format *fmt)
1627 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1630 mutex_lock(&sensor->mutex);
1631 rval = __smiapp_get_format(subdev, cfg, fmt);
1632 mutex_unlock(&sensor->mutex);
1637 static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1638 struct v4l2_subdev_pad_config *cfg,
1639 struct v4l2_rect **crops,
1640 struct v4l2_rect **comps, int which)
1642 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1645 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1647 for (i = 0; i < subdev->entity.num_pads; i++)
1648 crops[i] = &ssd->crop[i];
1650 *comps = &ssd->compose;
1653 for (i = 0; i < subdev->entity.num_pads; i++) {
1654 crops[i] = v4l2_subdev_get_try_crop(subdev, cfg, i);
1659 *comps = v4l2_subdev_get_try_compose(subdev, cfg,
1666 /* Changes require propagation only on sink pad. */
1667 static void smiapp_propagate(struct v4l2_subdev *subdev,
1668 struct v4l2_subdev_pad_config *cfg, int which,
1671 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1672 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1673 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1675 smiapp_get_crop_compose(subdev, cfg, crops, &comp, which);
1678 case V4L2_SEL_TGT_CROP:
1679 comp->width = crops[SMIAPP_PAD_SINK]->width;
1680 comp->height = crops[SMIAPP_PAD_SINK]->height;
1681 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1682 if (ssd == sensor->scaler) {
1685 SMIAPP_LIMIT_SCALER_N_MIN];
1686 sensor->scaling_mode =
1687 SMIAPP_SCALING_MODE_NONE;
1688 } else if (ssd == sensor->binner) {
1689 sensor->binning_horizontal = 1;
1690 sensor->binning_vertical = 1;
1694 case V4L2_SEL_TGT_COMPOSE:
1695 *crops[SMIAPP_PAD_SRC] = *comp;
1702 static const struct smiapp_csi_data_format
1703 *smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1707 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1708 if (sensor->mbus_frame_fmts & (1 << i)
1709 && smiapp_csi_data_formats[i].code == code)
1710 return &smiapp_csi_data_formats[i];
1713 return sensor->csi_format;
1716 static int smiapp_set_format_source(struct v4l2_subdev *subdev,
1717 struct v4l2_subdev_pad_config *cfg,
1718 struct v4l2_subdev_format *fmt)
1720 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1721 const struct smiapp_csi_data_format *csi_format,
1722 *old_csi_format = sensor->csi_format;
1723 unsigned long *valid_link_freqs;
1724 u32 code = fmt->format.code;
1728 rval = __smiapp_get_format(subdev, cfg, fmt);
1733 * Media bus code is changeable on src subdev's source pad. On
1734 * other source pads we just get format here.
1736 if (subdev != &sensor->src->sd)
1739 csi_format = smiapp_validate_csi_data_format(sensor, code);
1741 fmt->format.code = csi_format->code;
1743 if (fmt->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1746 sensor->csi_format = csi_format;
1748 if (csi_format->width != old_csi_format->width)
1749 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
1750 __v4l2_ctrl_modify_range(
1751 sensor->test_data[i], 0,
1752 (1 << csi_format->width) - 1, 1, 0);
1754 if (csi_format->compressed == old_csi_format->compressed)
1758 &sensor->valid_link_freqs[sensor->csi_format->compressed
1759 - sensor->compressed_min_bpp];
1761 __v4l2_ctrl_modify_range(
1762 sensor->link_freq, 0,
1763 __fls(*valid_link_freqs), ~*valid_link_freqs,
1764 __ffs(*valid_link_freqs));
1766 return smiapp_pll_update(sensor);
1769 static int smiapp_set_format(struct v4l2_subdev *subdev,
1770 struct v4l2_subdev_pad_config *cfg,
1771 struct v4l2_subdev_format *fmt)
1773 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1774 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1775 struct v4l2_rect *crops[SMIAPP_PADS];
1777 mutex_lock(&sensor->mutex);
1779 if (fmt->pad == ssd->source_pad) {
1782 rval = smiapp_set_format_source(subdev, cfg, fmt);
1784 mutex_unlock(&sensor->mutex);
1789 /* Sink pad. Width and height are changeable here. */
1790 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1791 fmt->format.width &= ~1;
1792 fmt->format.height &= ~1;
1793 fmt->format.field = V4L2_FIELD_NONE;
1796 clamp(fmt->format.width,
1797 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
1798 sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
1799 fmt->format.height =
1800 clamp(fmt->format.height,
1801 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
1802 sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);
1804 smiapp_get_crop_compose(subdev, cfg, crops, NULL, fmt->which);
1806 crops[ssd->sink_pad]->left = 0;
1807 crops[ssd->sink_pad]->top = 0;
1808 crops[ssd->sink_pad]->width = fmt->format.width;
1809 crops[ssd->sink_pad]->height = fmt->format.height;
1810 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1811 ssd->sink_fmt = *crops[ssd->sink_pad];
1812 smiapp_propagate(subdev, cfg, fmt->which,
1815 mutex_unlock(&sensor->mutex);
1821 * Calculate goodness of scaled image size compared to expected image
1822 * size and flags provided.
1824 #define SCALING_GOODNESS 100000
1825 #define SCALING_GOODNESS_EXTREME 100000000
1826 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1827 int h, int ask_h, u32 flags)
1829 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1830 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1838 if (flags & V4L2_SEL_FLAG_GE) {
1840 val -= SCALING_GOODNESS;
1842 val -= SCALING_GOODNESS;
1845 if (flags & V4L2_SEL_FLAG_LE) {
1847 val -= SCALING_GOODNESS;
1849 val -= SCALING_GOODNESS;
1852 val -= abs(w - ask_w);
1853 val -= abs(h - ask_h);
1855 if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
1856 val -= SCALING_GOODNESS_EXTREME;
1858 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1859 w, ask_w, h, ask_h, val);
1864 static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1865 struct v4l2_subdev_pad_config *cfg,
1866 struct v4l2_subdev_selection *sel,
1867 struct v4l2_rect **crops,
1868 struct v4l2_rect *comp)
1870 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1872 unsigned int binh = 1, binv = 1;
1873 int best = scaling_goodness(
1875 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1876 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1878 for (i = 0; i < sensor->nbinning_subtypes; i++) {
1879 int this = scaling_goodness(
1881 crops[SMIAPP_PAD_SINK]->width
1882 / sensor->binning_subtypes[i].horizontal,
1884 crops[SMIAPP_PAD_SINK]->height
1885 / sensor->binning_subtypes[i].vertical,
1886 sel->r.height, sel->flags);
1889 binh = sensor->binning_subtypes[i].horizontal;
1890 binv = sensor->binning_subtypes[i].vertical;
1894 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1895 sensor->binning_vertical = binv;
1896 sensor->binning_horizontal = binh;
1899 sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1900 sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1904 * Calculate best scaling ratio and mode for given output resolution.
1906 * Try all of these: horizontal ratio, vertical ratio and smallest
1907 * size possible (horizontally).
1909 * Also try whether horizontal scaler or full scaler gives a better
1912 static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1913 struct v4l2_subdev_pad_config *cfg,
1914 struct v4l2_subdev_selection *sel,
1915 struct v4l2_rect **crops,
1916 struct v4l2_rect *comp)
1918 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1919 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1920 u32 min, max, a, b, max_m;
1921 u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
1922 int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1928 sel->r.width = min_t(unsigned int, sel->r.width,
1929 crops[SMIAPP_PAD_SINK]->width);
1930 sel->r.height = min_t(unsigned int, sel->r.height,
1931 crops[SMIAPP_PAD_SINK]->height);
1933 a = crops[SMIAPP_PAD_SINK]->width
1934 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
1935 b = crops[SMIAPP_PAD_SINK]->height
1936 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
1937 max_m = crops[SMIAPP_PAD_SINK]->width
1938 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
1939 / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];
1941 a = clamp(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1942 sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1943 b = clamp(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1944 sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1945 max_m = clamp(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1946 sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1948 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1950 min = min(max_m, min(a, b));
1951 max = min(max_m, max(a, b));
1960 try[ntry] = min + 1;
1963 try[ntry] = max + 1;
1968 for (i = 0; i < ntry; i++) {
1969 int this = scaling_goodness(
1971 crops[SMIAPP_PAD_SINK]->width
1973 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1975 crops[SMIAPP_PAD_SINK]->height,
1979 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
1983 mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1987 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1988 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
1991 this = scaling_goodness(
1992 subdev, crops[SMIAPP_PAD_SINK]->width
1994 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1996 crops[SMIAPP_PAD_SINK]->height
1998 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
2004 mode = SMIAPP_SCALING_MODE_BOTH;
2010 (crops[SMIAPP_PAD_SINK]->width
2012 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
2013 if (mode == SMIAPP_SCALING_MODE_BOTH)
2015 (crops[SMIAPP_PAD_SINK]->height
2017 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
2020 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
2022 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2023 sensor->scale_m = scale_m;
2024 sensor->scaling_mode = mode;
2027 /* We're only called on source pads. This function sets scaling. */
2028 static int smiapp_set_compose(struct v4l2_subdev *subdev,
2029 struct v4l2_subdev_pad_config *cfg,
2030 struct v4l2_subdev_selection *sel)
2032 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2033 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2034 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2036 smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2041 if (ssd == sensor->binner)
2042 smiapp_set_compose_binner(subdev, cfg, sel, crops, comp);
2044 smiapp_set_compose_scaler(subdev, cfg, sel, crops, comp);
2047 smiapp_propagate(subdev, cfg, sel->which, V4L2_SEL_TGT_COMPOSE);
2049 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
2050 return smiapp_pll_blanking_update(sensor);
2055 static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
2056 struct v4l2_subdev_selection *sel)
2058 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2059 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2061 /* We only implement crop in three places. */
2062 switch (sel->target) {
2063 case V4L2_SEL_TGT_CROP:
2064 case V4L2_SEL_TGT_CROP_BOUNDS:
2065 if (ssd == sensor->pixel_array
2066 && sel->pad == SMIAPP_PA_PAD_SRC)
2068 if (ssd == sensor->src
2069 && sel->pad == SMIAPP_PAD_SRC)
2071 if (ssd == sensor->scaler
2072 && sel->pad == SMIAPP_PAD_SINK
2073 && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2074 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
2077 case V4L2_SEL_TGT_NATIVE_SIZE:
2078 if (ssd == sensor->pixel_array
2079 && sel->pad == SMIAPP_PA_PAD_SRC)
2082 case V4L2_SEL_TGT_COMPOSE:
2083 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2084 if (sel->pad == ssd->source_pad)
2086 if (ssd == sensor->binner)
2088 if (ssd == sensor->scaler
2089 && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2090 != SMIAPP_SCALING_CAPABILITY_NONE)
2098 static int smiapp_set_crop(struct v4l2_subdev *subdev,
2099 struct v4l2_subdev_pad_config *cfg,
2100 struct v4l2_subdev_selection *sel)
2102 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2103 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2104 struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
2105 struct v4l2_rect _r;
2107 smiapp_get_crop_compose(subdev, cfg, crops, NULL, sel->which);
2109 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2110 if (sel->pad == ssd->sink_pad)
2111 src_size = &ssd->sink_fmt;
2113 src_size = &ssd->compose;
2115 if (sel->pad == ssd->sink_pad) {
2118 _r.width = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2120 _r.height = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2124 src_size = v4l2_subdev_get_try_compose(
2125 subdev, cfg, ssd->sink_pad);
2129 if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2134 sel->r.width = min(sel->r.width, src_size->width);
2135 sel->r.height = min(sel->r.height, src_size->height);
2137 sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width);
2138 sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height);
2140 *crops[sel->pad] = sel->r;
2142 if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2143 smiapp_propagate(subdev, cfg, sel->which,
2149 static void smiapp_get_native_size(struct smiapp_subdev *ssd,
2150 struct v4l2_rect *r)
2154 r->width = ssd->sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2155 r->height = ssd->sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2158 static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2159 struct v4l2_subdev_pad_config *cfg,
2160 struct v4l2_subdev_selection *sel)
2162 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2163 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2164 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2165 struct v4l2_rect sink_fmt;
2168 ret = __smiapp_sel_supported(subdev, sel);
2172 smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2174 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2175 sink_fmt = ssd->sink_fmt;
2177 struct v4l2_mbus_framefmt *fmt =
2178 v4l2_subdev_get_try_format(subdev, cfg, ssd->sink_pad);
2182 sink_fmt.width = fmt->width;
2183 sink_fmt.height = fmt->height;
2186 switch (sel->target) {
2187 case V4L2_SEL_TGT_CROP_BOUNDS:
2188 case V4L2_SEL_TGT_NATIVE_SIZE:
2189 if (ssd == sensor->pixel_array)
2190 smiapp_get_native_size(ssd, &sel->r);
2191 else if (sel->pad == ssd->sink_pad)
2196 case V4L2_SEL_TGT_CROP:
2197 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2198 sel->r = *crops[sel->pad];
2200 case V4L2_SEL_TGT_COMPOSE:
2208 static int smiapp_get_selection(struct v4l2_subdev *subdev,
2209 struct v4l2_subdev_pad_config *cfg,
2210 struct v4l2_subdev_selection *sel)
2212 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2215 mutex_lock(&sensor->mutex);
2216 rval = __smiapp_get_selection(subdev, cfg, sel);
2217 mutex_unlock(&sensor->mutex);
2221 static int smiapp_set_selection(struct v4l2_subdev *subdev,
2222 struct v4l2_subdev_pad_config *cfg,
2223 struct v4l2_subdev_selection *sel)
2225 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2228 ret = __smiapp_sel_supported(subdev, sel);
2232 mutex_lock(&sensor->mutex);
2234 sel->r.left = max(0, sel->r.left & ~1);
2235 sel->r.top = max(0, sel->r.top & ~1);
2236 sel->r.width = SMIAPP_ALIGN_DIM(sel->r.width, sel->flags);
2237 sel->r.height = SMIAPP_ALIGN_DIM(sel->r.height, sel->flags);
2239 sel->r.width = max_t(unsigned int,
2240 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
2242 sel->r.height = max_t(unsigned int,
2243 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
2246 switch (sel->target) {
2247 case V4L2_SEL_TGT_CROP:
2248 ret = smiapp_set_crop(subdev, cfg, sel);
2250 case V4L2_SEL_TGT_COMPOSE:
2251 ret = smiapp_set_compose(subdev, cfg, sel);
2257 mutex_unlock(&sensor->mutex);
2261 static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2263 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2265 *frames = sensor->frame_skip;
2269 static int smiapp_get_skip_top_lines(struct v4l2_subdev *subdev, u32 *lines)
2271 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2273 *lines = sensor->image_start;
2278 /* -----------------------------------------------------------------------------
2283 smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2286 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2287 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2288 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2291 if (!sensor->dev_init_done)
2294 rval = pm_runtime_get_sync(&client->dev);
2296 if (rval != -EBUSY && rval != -EAGAIN)
2297 pm_runtime_set_active(&client->dev);
2298 pm_runtime_put_noidle(&client->dev);
2302 rval = smiapp_read_nvm(sensor, buf, PAGE_SIZE);
2304 pm_runtime_put(&client->dev);
2305 dev_err(&client->dev, "nvm read failed\n");
2309 pm_runtime_mark_last_busy(&client->dev);
2310 pm_runtime_put_autosuspend(&client->dev);
2313 * NVM is still way below a PAGE_SIZE, so we can safely
2314 * assume this for now.
2318 static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2321 smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
2324 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2325 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2326 struct smiapp_module_info *minfo = &sensor->minfo;
2328 return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2329 minfo->manufacturer_id, minfo->model_id,
2330 minfo->revision_number_major) + 1;
2333 static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);
2335 /* -----------------------------------------------------------------------------
2336 * V4L2 subdev core operations
2339 static int smiapp_identify_module(struct smiapp_sensor *sensor)
2341 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2342 struct smiapp_module_info *minfo = &sensor->minfo;
2346 minfo->name = SMIAPP_NAME;
2349 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2350 &minfo->manufacturer_id);
2352 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2355 rval = smiapp_read_8only(sensor,
2356 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2357 &minfo->revision_number_major);
2359 rval = smiapp_read_8only(sensor,
2360 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2361 &minfo->revision_number_minor);
2363 rval = smiapp_read_8only(sensor,
2364 SMIAPP_REG_U8_MODULE_DATE_YEAR,
2365 &minfo->module_year);
2367 rval = smiapp_read_8only(sensor,
2368 SMIAPP_REG_U8_MODULE_DATE_MONTH,
2369 &minfo->module_month);
2371 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2372 &minfo->module_day);
2376 rval = smiapp_read_8only(sensor,
2377 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2378 &minfo->sensor_manufacturer_id);
2380 rval = smiapp_read_8only(sensor,
2381 SMIAPP_REG_U16_SENSOR_MODEL_ID,
2382 &minfo->sensor_model_id);
2384 rval = smiapp_read_8only(sensor,
2385 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2386 &minfo->sensor_revision_number);
2388 rval = smiapp_read_8only(sensor,
2389 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2390 &minfo->sensor_firmware_version);
2394 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2395 &minfo->smia_version);
2397 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2398 &minfo->smiapp_version);
2401 dev_err(&client->dev, "sensor detection failed\n");
2405 dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2406 minfo->manufacturer_id, minfo->model_id);
2408 dev_dbg(&client->dev,
2409 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2410 minfo->revision_number_major, minfo->revision_number_minor,
2411 minfo->module_year, minfo->module_month, minfo->module_day);
2413 dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2414 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2416 dev_dbg(&client->dev,
2417 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2418 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2420 dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2421 minfo->smia_version, minfo->smiapp_version);
2424 * Some modules have bad data in the lvalues below. Hope the
2425 * rvalues have better stuff. The lvalues are module
2426 * parameters whereas the rvalues are sensor parameters.
2428 if (!minfo->manufacturer_id && !minfo->model_id) {
2429 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2430 minfo->model_id = minfo->sensor_model_id;
2431 minfo->revision_number_major = minfo->sensor_revision_number;
2434 for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2435 if (smiapp_module_idents[i].manufacturer_id
2436 != minfo->manufacturer_id)
2438 if (smiapp_module_idents[i].model_id != minfo->model_id)
2440 if (smiapp_module_idents[i].flags
2441 & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2442 if (smiapp_module_idents[i].revision_number_major
2443 < minfo->revision_number_major)
2446 if (smiapp_module_idents[i].revision_number_major
2447 != minfo->revision_number_major)
2451 minfo->name = smiapp_module_idents[i].name;
2452 minfo->quirk = smiapp_module_idents[i].quirk;
2456 if (i >= ARRAY_SIZE(smiapp_module_idents))
2457 dev_warn(&client->dev,
2458 "no quirks for this module; let's hope it's fully compliant\n");
2460 dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2461 minfo->name, minfo->manufacturer_id, minfo->model_id,
2462 minfo->revision_number_major);
2467 static const struct v4l2_subdev_ops smiapp_ops;
2468 static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2469 static const struct media_entity_operations smiapp_entity_ops;
2471 static int smiapp_register_subdev(struct smiapp_sensor *sensor,
2472 struct smiapp_subdev *ssd,
2473 struct smiapp_subdev *sink_ssd,
2474 u16 source_pad, u16 sink_pad, u32 link_flags)
2476 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2482 rval = media_entity_pads_init(&ssd->sd.entity,
2483 ssd->npads, ssd->pads);
2485 dev_err(&client->dev,
2486 "media_entity_pads_init failed\n");
2490 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2493 dev_err(&client->dev,
2494 "v4l2_device_register_subdev failed\n");
2498 rval = media_create_pad_link(&ssd->sd.entity, source_pad,
2499 &sink_ssd->sd.entity, sink_pad,
2502 dev_err(&client->dev,
2503 "media_create_pad_link failed\n");
2504 v4l2_device_unregister_subdev(&ssd->sd);
2511 static void smiapp_unregistered(struct v4l2_subdev *subdev)
2513 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2516 for (i = 1; i < sensor->ssds_used; i++)
2517 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2520 static int smiapp_registered(struct v4l2_subdev *subdev)
2522 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2525 if (sensor->scaler) {
2526 rval = smiapp_register_subdev(
2527 sensor, sensor->binner, sensor->scaler,
2528 SMIAPP_PAD_SRC, SMIAPP_PAD_SINK,
2529 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2534 rval = smiapp_register_subdev(
2535 sensor, sensor->pixel_array, sensor->binner,
2536 SMIAPP_PA_PAD_SRC, SMIAPP_PAD_SINK,
2537 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2544 smiapp_unregistered(subdev);
2549 static void smiapp_cleanup(struct smiapp_sensor *sensor)
2551 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2553 device_remove_file(&client->dev, &dev_attr_nvm);
2554 device_remove_file(&client->dev, &dev_attr_ident);
2556 smiapp_free_controls(sensor);
2559 static void smiapp_create_subdev(struct smiapp_sensor *sensor,
2560 struct smiapp_subdev *ssd, const char *name,
2561 unsigned short num_pads)
2563 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2568 if (ssd != sensor->src)
2569 v4l2_subdev_init(&ssd->sd, &smiapp_ops);
2571 ssd->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2572 ssd->sensor = sensor;
2574 ssd->npads = num_pads;
2575 ssd->source_pad = num_pads - 1;
2577 v4l2_i2c_subdev_set_name(&ssd->sd, client, sensor->minfo.name, name);
2579 smiapp_get_native_size(ssd, &ssd->sink_fmt);
2581 ssd->compose.width = ssd->sink_fmt.width;
2582 ssd->compose.height = ssd->sink_fmt.height;
2583 ssd->crop[ssd->source_pad] = ssd->compose;
2584 ssd->pads[ssd->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2585 if (ssd != sensor->pixel_array) {
2586 ssd->crop[ssd->sink_pad] = ssd->compose;
2587 ssd->pads[ssd->sink_pad].flags = MEDIA_PAD_FL_SINK;
2590 ssd->sd.entity.ops = &smiapp_entity_ops;
2592 if (ssd == sensor->src)
2595 ssd->sd.internal_ops = &smiapp_internal_ops;
2596 ssd->sd.owner = THIS_MODULE;
2597 ssd->sd.dev = &client->dev;
2598 v4l2_set_subdevdata(&ssd->sd, client);
2601 static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2603 struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2604 struct smiapp_sensor *sensor = ssd->sensor;
2607 mutex_lock(&sensor->mutex);
2609 for (i = 0; i < ssd->npads; i++) {
2610 struct v4l2_mbus_framefmt *try_fmt =
2611 v4l2_subdev_get_try_format(sd, fh->pad, i);
2612 struct v4l2_rect *try_crop =
2613 v4l2_subdev_get_try_crop(sd, fh->pad, i);
2614 struct v4l2_rect *try_comp;
2616 smiapp_get_native_size(ssd, try_crop);
2618 try_fmt->width = try_crop->width;
2619 try_fmt->height = try_crop->height;
2620 try_fmt->code = sensor->internal_csi_format->code;
2621 try_fmt->field = V4L2_FIELD_NONE;
2623 if (ssd != sensor->pixel_array)
2626 try_comp = v4l2_subdev_get_try_compose(sd, fh->pad, i);
2627 *try_comp = *try_crop;
2630 mutex_unlock(&sensor->mutex);
2635 static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2636 .s_stream = smiapp_set_stream,
2639 static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2640 .enum_mbus_code = smiapp_enum_mbus_code,
2641 .get_fmt = smiapp_get_format,
2642 .set_fmt = smiapp_set_format,
2643 .get_selection = smiapp_get_selection,
2644 .set_selection = smiapp_set_selection,
2647 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2648 .g_skip_frames = smiapp_get_skip_frames,
2649 .g_skip_top_lines = smiapp_get_skip_top_lines,
2652 static const struct v4l2_subdev_ops smiapp_ops = {
2653 .video = &smiapp_video_ops,
2654 .pad = &smiapp_pad_ops,
2655 .sensor = &smiapp_sensor_ops,
2658 static const struct media_entity_operations smiapp_entity_ops = {
2659 .link_validate = v4l2_subdev_link_validate,
2662 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2663 .registered = smiapp_registered,
2664 .unregistered = smiapp_unregistered,
2665 .open = smiapp_open,
2668 static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2669 .open = smiapp_open,
2672 /* -----------------------------------------------------------------------------
2676 static int __maybe_unused smiapp_suspend(struct device *dev)
2678 struct i2c_client *client = to_i2c_client(dev);
2679 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2680 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2681 bool streaming = sensor->streaming;
2684 rval = pm_runtime_get_sync(dev);
2686 if (rval != -EBUSY && rval != -EAGAIN)
2687 pm_runtime_set_active(&client->dev);
2688 pm_runtime_put(dev);
2692 if (sensor->streaming)
2693 smiapp_stop_streaming(sensor);
2695 /* save state for resume */
2696 sensor->streaming = streaming;
2701 static int __maybe_unused smiapp_resume(struct device *dev)
2703 struct i2c_client *client = to_i2c_client(dev);
2704 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2705 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2708 pm_runtime_put(dev);
2710 if (sensor->streaming)
2711 rval = smiapp_start_streaming(sensor);
2716 static struct smiapp_hwconfig *smiapp_get_hwconfig(struct device *dev)
2718 struct smiapp_hwconfig *hwcfg;
2719 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
2720 struct fwnode_handle *ep;
2721 struct fwnode_handle *fwnode = dev_fwnode(dev);
2727 return dev->platform_data;
2729 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
2733 bus_cfg.bus_type = V4L2_MBUS_CSI2_DPHY;
2734 rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
2735 if (rval == -ENXIO) {
2736 bus_cfg = (struct v4l2_fwnode_endpoint)
2737 { .bus_type = V4L2_MBUS_CCP2 };
2738 rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
2743 hwcfg = devm_kzalloc(dev, sizeof(*hwcfg), GFP_KERNEL);
2747 switch (bus_cfg.bus_type) {
2748 case V4L2_MBUS_CSI2_DPHY:
2749 hwcfg->csi_signalling_mode = SMIAPP_CSI_SIGNALLING_MODE_CSI2;
2750 hwcfg->lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
2752 case V4L2_MBUS_CCP2:
2753 hwcfg->csi_signalling_mode = (bus_cfg.bus.mipi_csi1.strobe) ?
2754 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE :
2755 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK;
2759 dev_err(dev, "unsupported bus %u\n", bus_cfg.bus_type);
2763 dev_dbg(dev, "lanes %u\n", hwcfg->lanes);
2765 rval = fwnode_property_read_u32(fwnode, "rotation", &rotation);
2769 hwcfg->module_board_orient =
2770 SMIAPP_MODULE_BOARD_ORIENT_180;
2775 dev_err(dev, "invalid rotation %u\n", rotation);
2780 rval = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
2783 dev_info(dev, "can't get clock-frequency\n");
2785 dev_dbg(dev, "clk %d, mode %d\n", hwcfg->ext_clk,
2786 hwcfg->csi_signalling_mode);
2788 if (!bus_cfg.nr_of_link_frequencies) {
2789 dev_warn(dev, "no link frequencies defined\n");
2793 hwcfg->op_sys_clock = devm_kcalloc(
2794 dev, bus_cfg.nr_of_link_frequencies + 1 /* guardian */,
2795 sizeof(*hwcfg->op_sys_clock), GFP_KERNEL);
2796 if (!hwcfg->op_sys_clock)
2799 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
2800 hwcfg->op_sys_clock[i] = bus_cfg.link_frequencies[i];
2801 dev_dbg(dev, "freq %d: %lld\n", i, hwcfg->op_sys_clock[i]);
2804 v4l2_fwnode_endpoint_free(&bus_cfg);
2805 fwnode_handle_put(ep);
2809 v4l2_fwnode_endpoint_free(&bus_cfg);
2810 fwnode_handle_put(ep);
2814 static int smiapp_probe(struct i2c_client *client)
2816 struct smiapp_sensor *sensor;
2817 struct smiapp_hwconfig *hwcfg = smiapp_get_hwconfig(&client->dev);
2824 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
2828 sensor->hwcfg = hwcfg;
2829 sensor->src = &sensor->ssds[sensor->ssds_used];
2831 v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2832 sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2834 sensor->vana = devm_regulator_get(&client->dev, "vana");
2835 if (IS_ERR(sensor->vana)) {
2836 dev_err(&client->dev, "could not get regulator for vana\n");
2837 return PTR_ERR(sensor->vana);
2840 sensor->ext_clk = devm_clk_get(&client->dev, NULL);
2841 if (PTR_ERR(sensor->ext_clk) == -ENOENT) {
2842 dev_info(&client->dev, "no clock defined, continuing...\n");
2843 sensor->ext_clk = NULL;
2844 } else if (IS_ERR(sensor->ext_clk)) {
2845 dev_err(&client->dev, "could not get clock (%ld)\n",
2846 PTR_ERR(sensor->ext_clk));
2847 return -EPROBE_DEFER;
2850 if (sensor->ext_clk) {
2851 if (sensor->hwcfg->ext_clk) {
2854 rval = clk_set_rate(sensor->ext_clk,
2855 sensor->hwcfg->ext_clk);
2857 dev_err(&client->dev,
2858 "unable to set clock freq to %u\n",
2859 sensor->hwcfg->ext_clk);
2863 rate = clk_get_rate(sensor->ext_clk);
2864 if (rate != sensor->hwcfg->ext_clk) {
2865 dev_err(&client->dev,
2866 "can't set clock freq, asked for %u but got %lu\n",
2867 sensor->hwcfg->ext_clk, rate);
2871 sensor->hwcfg->ext_clk = clk_get_rate(sensor->ext_clk);
2872 dev_dbg(&client->dev, "obtained clock freq %u\n",
2873 sensor->hwcfg->ext_clk);
2875 } else if (sensor->hwcfg->ext_clk) {
2876 dev_dbg(&client->dev, "assuming clock freq %u\n",
2877 sensor->hwcfg->ext_clk);
2879 dev_err(&client->dev, "unable to obtain clock freq\n");
2883 sensor->xshutdown = devm_gpiod_get_optional(&client->dev, "xshutdown",
2885 if (IS_ERR(sensor->xshutdown))
2886 return PTR_ERR(sensor->xshutdown);
2888 rval = smiapp_power_on(&client->dev);
2892 mutex_init(&sensor->mutex);
2894 rval = smiapp_identify_module(sensor);
2900 rval = smiapp_get_all_limits(sensor);
2906 rval = smiapp_read_frame_fmt(sensor);
2913 * Handle Sensor Module orientation on the board.
2915 * The application of H-FLIP and V-FLIP on the sensor is modified by
2916 * the sensor orientation on the board.
2918 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2919 * both H-FLIP and V-FLIP for normal operation which also implies
2920 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2921 * controls will need to be internally inverted.
2923 * Rotation also changes the bayer pattern.
2925 if (sensor->hwcfg->module_board_orient ==
2926 SMIAPP_MODULE_BOARD_ORIENT_180)
2927 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2928 SMIAPP_IMAGE_ORIENTATION_VFLIP;
2930 rval = smiapp_call_quirk(sensor, limits);
2932 dev_err(&client->dev, "limits quirks failed\n");
2936 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
2939 rval = smiapp_read(sensor,
2940 SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2945 sensor->nbinning_subtypes = min_t(u8, val,
2946 SMIAPP_BINNING_SUBTYPES);
2948 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2950 sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
2955 sensor->binning_subtypes[i] =
2956 *(struct smiapp_binning_subtype *)&val;
2958 dev_dbg(&client->dev, "binning %xx%x\n",
2959 sensor->binning_subtypes[i].horizontal,
2960 sensor->binning_subtypes[i].vertical);
2963 sensor->binning_horizontal = 1;
2964 sensor->binning_vertical = 1;
2966 if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
2967 dev_err(&client->dev, "sysfs ident entry creation failed\n");
2972 if (sensor->minfo.smiapp_version &&
2973 sensor->limits[SMIAPP_LIMIT_DATA_TRANSFER_IF_CAPABILITY] &
2974 SMIAPP_DATA_TRANSFER_IF_CAPABILITY_SUPPORTED) {
2975 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
2976 dev_err(&client->dev, "sysfs nvm entry failed\n");
2982 /* We consider this as profile 0 sensor if any of these are zero. */
2983 if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
2984 !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
2985 !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
2986 !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
2987 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
2988 } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2989 != SMIAPP_SCALING_CAPABILITY_NONE) {
2990 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2991 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2992 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
2994 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
2995 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2996 sensor->ssds_used++;
2997 } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2998 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
2999 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3000 sensor->ssds_used++;
3002 sensor->binner = &sensor->ssds[sensor->ssds_used];
3003 sensor->ssds_used++;
3004 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
3005 sensor->ssds_used++;
3007 sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
3009 /* prepare PLL configuration input values */
3010 sensor->pll.bus_type = SMIAPP_PLL_BUS_TYPE_CSI2;
3011 sensor->pll.csi2.lanes = sensor->hwcfg->lanes;
3012 sensor->pll.ext_clk_freq_hz = sensor->hwcfg->ext_clk;
3013 sensor->pll.scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
3014 /* Profile 0 sensors have no separate OP clock branch. */
3015 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
3016 sensor->pll.flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
3018 smiapp_create_subdev(sensor, sensor->scaler, " scaler", 2);
3019 smiapp_create_subdev(sensor, sensor->binner, " binner", 2);
3020 smiapp_create_subdev(sensor, sensor->pixel_array, " pixel_array", 1);
3022 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
3024 sensor->pixel_array->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3026 rval = smiapp_init_controls(sensor);
3030 rval = smiapp_call_quirk(sensor, init);
3034 rval = smiapp_get_mbus_formats(sensor);
3040 rval = smiapp_init_late_controls(sensor);
3046 mutex_lock(&sensor->mutex);
3047 rval = smiapp_pll_blanking_update(sensor);
3048 mutex_unlock(&sensor->mutex);
3050 dev_err(&client->dev, "update mode failed\n");
3054 sensor->streaming = false;
3055 sensor->dev_init_done = true;
3057 rval = media_entity_pads_init(&sensor->src->sd.entity, 2,
3060 goto out_media_entity_cleanup;
3062 pm_runtime_set_active(&client->dev);
3063 pm_runtime_get_noresume(&client->dev);
3064 pm_runtime_enable(&client->dev);
3066 rval = v4l2_async_register_subdev_sensor_common(&sensor->src->sd);
3068 goto out_disable_runtime_pm;
3070 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
3071 pm_runtime_use_autosuspend(&client->dev);
3072 pm_runtime_put_autosuspend(&client->dev);
3076 out_disable_runtime_pm:
3077 pm_runtime_disable(&client->dev);
3079 out_media_entity_cleanup:
3080 media_entity_cleanup(&sensor->src->sd.entity);
3083 smiapp_cleanup(sensor);
3086 smiapp_power_off(&client->dev);
3087 mutex_destroy(&sensor->mutex);
3092 static int smiapp_remove(struct i2c_client *client)
3094 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3095 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
3098 v4l2_async_unregister_subdev(subdev);
3100 pm_runtime_disable(&client->dev);
3101 if (!pm_runtime_status_suspended(&client->dev))
3102 smiapp_power_off(&client->dev);
3103 pm_runtime_set_suspended(&client->dev);
3105 for (i = 0; i < sensor->ssds_used; i++) {
3106 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
3107 media_entity_cleanup(&sensor->ssds[i].sd.entity);
3109 smiapp_cleanup(sensor);
3110 mutex_destroy(&sensor->mutex);
3115 static const struct of_device_id smiapp_of_table[] = {
3116 { .compatible = "nokia,smia" },
3119 MODULE_DEVICE_TABLE(of, smiapp_of_table);
3121 static const struct i2c_device_id smiapp_id_table[] = {
3125 MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
3127 static const struct dev_pm_ops smiapp_pm_ops = {
3128 SET_SYSTEM_SLEEP_PM_OPS(smiapp_suspend, smiapp_resume)
3129 SET_RUNTIME_PM_OPS(smiapp_power_off, smiapp_power_on, NULL)
3132 static struct i2c_driver smiapp_i2c_driver = {
3134 .of_match_table = smiapp_of_table,
3135 .name = SMIAPP_NAME,
3136 .pm = &smiapp_pm_ops,
3138 .probe_new = smiapp_probe,
3139 .remove = smiapp_remove,
3140 .id_table = smiapp_id_table,
3143 module_i2c_driver(smiapp_i2c_driver);
3145 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3146 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3147 MODULE_LICENSE("GPL v2");