1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/media/i2c/ccs/ccs-core.c
5 * Generic driver for MIPI CCS/SMIA/SMIA++ compliant camera sensors
7 * Copyright (C) 2020 Intel Corporation
8 * Copyright (C) 2010--2012 Nokia Corporation
9 * Contact: Sakari Ailus <sakari.ailus@linux.intel.com>
11 * Based on smiapp driver by Vimarsh Zutshi
12 * Based on jt8ev1.c by Vimarsh Zutshi
13 * Based on smia-sensor.c by Tuukka Toivonen <tuukkat76@gmail.com>
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/firmware.h>
20 #include <linux/gpio.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/module.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/property.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
27 #include <linux/smiapp.h>
28 #include <linux/v4l2-mediabus.h>
29 #include <media/v4l2-fwnode.h>
30 #include <media/v4l2-device.h>
31 #include <uapi/linux/ccs.h>
35 #define CCS_ALIGN_DIM(dim, flags) \
36 ((flags) & V4L2_SEL_FLAG_GE \
40 static struct ccs_limit_offset {
43 } ccs_limit_offsets[CCS_L_LAST + 1];
46 * ccs_module_idents - supported camera modules
48 static const struct ccs_module_ident ccs_module_idents[] = {
49 CCS_IDENT_L(0x01, 0x022b, -1, "vs6555"),
50 CCS_IDENT_L(0x01, 0x022e, -1, "vw6558"),
51 CCS_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
52 CCS_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
53 CCS_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
54 CCS_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk),
55 CCS_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
56 CCS_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
57 CCS_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk),
58 CCS_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk),
59 CCS_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk),
62 #define CCS_DEVICE_FLAG_IS_SMIA BIT(0)
68 static const char * const ccs_regulators[] = { "vcore", "vio", "vana" };
72 * Dynamic Capability Identification
76 static void ccs_assign_limit(void *ptr, unsigned int width, u32 val)
91 static int ccs_limit_ptr(struct ccs_sensor *sensor, unsigned int limit,
92 unsigned int offset, void **__ptr)
94 const struct ccs_limit *linfo;
96 if (WARN_ON(limit >= CCS_L_LAST))
99 linfo = &ccs_limits[ccs_limit_offsets[limit].info];
101 if (WARN_ON(!sensor->ccs_limits) ||
102 WARN_ON(offset + ccs_reg_width(linfo->reg) >
103 ccs_limit_offsets[limit + 1].lim))
106 *__ptr = sensor->ccs_limits + ccs_limit_offsets[limit].lim + offset;
111 void ccs_replace_limit(struct ccs_sensor *sensor,
112 unsigned int limit, unsigned int offset, u32 val)
114 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
115 const struct ccs_limit *linfo;
119 ret = ccs_limit_ptr(sensor, limit, offset, &ptr);
123 linfo = &ccs_limits[ccs_limit_offsets[limit].info];
125 dev_dbg(&client->dev, "quirk: 0x%8.8x \"%s\" %u = %d, 0x%x\n",
126 linfo->reg, linfo->name, offset, val, val);
128 ccs_assign_limit(ptr, ccs_reg_width(linfo->reg), val);
131 u32 ccs_get_limit(struct ccs_sensor *sensor, unsigned int limit,
138 ret = ccs_limit_ptr(sensor, limit, offset, &ptr);
142 switch (ccs_reg_width(ccs_limits[ccs_limit_offsets[limit].info].reg)) {
157 return ccs_reg_conv(sensor, ccs_limits[limit].reg, val);
160 static int ccs_read_all_limits(struct ccs_sensor *sensor)
162 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
163 void *ptr, *alloc, *end;
167 kfree(sensor->ccs_limits);
168 sensor->ccs_limits = NULL;
170 alloc = kzalloc(ccs_limit_offsets[CCS_L_LAST].lim, GFP_KERNEL);
174 end = alloc + ccs_limit_offsets[CCS_L_LAST].lim;
176 for (i = 0, l = 0, ptr = alloc; ccs_limits[i].size; i++) {
177 u32 reg = ccs_limits[i].reg;
178 unsigned int width = ccs_reg_width(reg);
181 if (l == CCS_L_LAST) {
182 dev_err(&client->dev,
183 "internal error --- end of limit array\n");
188 for (j = 0; j < ccs_limits[i].size / width;
189 j++, reg += width, ptr += width) {
192 ret = ccs_read_addr_noconv(sensor, reg, &val);
196 if (ptr + width > end) {
197 dev_err(&client->dev,
198 "internal error --- no room for regs\n");
206 ccs_assign_limit(ptr, width, val);
208 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %u, 0x%x\n",
209 reg, ccs_limits[i].name, val, val);
212 if (ccs_limits[i].flags & CCS_L_FL_SAME_REG)
216 ptr = alloc + ccs_limit_offsets[l].lim;
219 if (l != CCS_L_LAST) {
220 dev_err(&client->dev,
221 "internal error --- insufficient limits\n");
226 sensor->ccs_limits = alloc;
228 if (CCS_LIM(sensor, SCALER_N_MIN) < 16)
229 ccs_replace_limit(sensor, CCS_L_SCALER_N_MIN, 0, 16);
239 static int ccs_read_frame_fmt(struct ccs_sensor *sensor)
241 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
242 u8 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc;
247 fmt_model_type = CCS_LIM(sensor, FRAME_FORMAT_MODEL_TYPE);
248 fmt_model_subtype = CCS_LIM(sensor, FRAME_FORMAT_MODEL_SUBTYPE);
250 ncol_desc = (fmt_model_subtype
251 & CCS_FRAME_FORMAT_MODEL_SUBTYPE_COLUMNS_MASK)
252 >> CCS_FRAME_FORMAT_MODEL_SUBTYPE_COLUMNS_SHIFT;
253 nrow_desc = fmt_model_subtype
254 & CCS_FRAME_FORMAT_MODEL_SUBTYPE_ROWS_MASK;
256 dev_dbg(&client->dev, "format_model_type %s\n",
257 fmt_model_type == CCS_FRAME_FORMAT_MODEL_TYPE_2_BYTE
259 fmt_model_type == CCS_FRAME_FORMAT_MODEL_TYPE_4_BYTE
260 ? "4 byte" : "is simply bad");
262 dev_dbg(&client->dev, "%u column and %u row descriptors\n",
263 ncol_desc, nrow_desc);
265 for (i = 0; i < ncol_desc + nrow_desc; i++) {
272 if (fmt_model_type == CCS_FRAME_FORMAT_MODEL_TYPE_2_BYTE) {
273 desc = CCS_LIM_AT(sensor, FRAME_FORMAT_DESCRIPTOR, i);
277 & CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_MASK)
278 >> CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_SHIFT;
279 pixels = desc & CCS_FRAME_FORMAT_DESCRIPTOR_PIXELS_MASK;
280 } else if (fmt_model_type
281 == CCS_FRAME_FORMAT_MODEL_TYPE_4_BYTE) {
282 desc = CCS_LIM_AT(sensor, FRAME_FORMAT_DESCRIPTOR_4, i);
286 & CCS_FRAME_FORMAT_DESCRIPTOR_4_PCODE_MASK)
287 >> CCS_FRAME_FORMAT_DESCRIPTOR_4_PCODE_SHIFT;
289 CCS_FRAME_FORMAT_DESCRIPTOR_4_PIXELS_MASK;
291 dev_dbg(&client->dev,
292 "invalid frame format model type %d\n",
303 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_EMBEDDED:
306 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_DUMMY_PIXEL:
309 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_BLACK_PIXEL:
312 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_DARK_PIXEL:
315 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL:
323 dev_dbg(&client->dev,
324 "%s pixels: %d %s (pixelcode %u)\n",
325 what, pixels, which, pixelcode);
329 CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL)
330 sensor->visible_pixel_start = pixel_count;
331 pixel_count += pixels;
335 /* Handle row descriptors */
337 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_EMBEDDED:
338 if (sensor->embedded_end)
340 sensor->embedded_start = line_count;
341 sensor->embedded_end = line_count + pixels;
343 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL:
344 sensor->image_start = line_count;
347 line_count += pixels;
350 if (sensor->embedded_end > sensor->image_start) {
351 dev_dbg(&client->dev,
352 "adjusting image start line to %u (was %u)\n",
353 sensor->embedded_end, sensor->image_start);
354 sensor->image_start = sensor->embedded_end;
357 dev_dbg(&client->dev, "embedded data from lines %d to %d\n",
358 sensor->embedded_start, sensor->embedded_end);
359 dev_dbg(&client->dev, "image data starts at line %d\n",
360 sensor->image_start);
365 static int ccs_pll_configure(struct ccs_sensor *sensor)
367 struct ccs_pll *pll = &sensor->pll;
370 rval = ccs_write(sensor, VT_PIX_CLK_DIV, pll->vt_bk.pix_clk_div);
374 rval = ccs_write(sensor, VT_SYS_CLK_DIV, pll->vt_bk.sys_clk_div);
378 rval = ccs_write(sensor, PRE_PLL_CLK_DIV, pll->vt_fr.pre_pll_clk_div);
382 rval = ccs_write(sensor, PLL_MULTIPLIER, pll->vt_fr.pll_multiplier);
386 if (!(CCS_LIM(sensor, PHY_CTRL_CAPABILITY) &
387 CCS_PHY_CTRL_CAPABILITY_AUTO_PHY_CTL)) {
388 /* Lane op clock ratio does not apply here. */
389 rval = ccs_write(sensor, REQUESTED_LINK_RATE,
390 DIV_ROUND_UP(pll->op_bk.sys_clk_freq_hz,
391 1000000 / 256 / 256) *
392 (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ?
393 sensor->pll.csi2.lanes : 1) <<
394 (pll->flags & CCS_PLL_FLAG_OP_SYS_DDR ?
400 if (sensor->pll.flags & CCS_PLL_FLAG_NO_OP_CLOCKS)
403 rval = ccs_write(sensor, OP_PIX_CLK_DIV, pll->op_bk.pix_clk_div);
407 rval = ccs_write(sensor, OP_SYS_CLK_DIV, pll->op_bk.sys_clk_div);
411 if (!(pll->flags & CCS_PLL_FLAG_DUAL_PLL))
414 rval = ccs_write(sensor, PLL_MODE, CCS_PLL_MODE_DUAL);
418 rval = ccs_write(sensor, OP_PRE_PLL_CLK_DIV,
419 pll->op_fr.pre_pll_clk_div);
423 return ccs_write(sensor, OP_PLL_MULTIPLIER, pll->op_fr.pll_multiplier);
426 static int ccs_pll_try(struct ccs_sensor *sensor, struct ccs_pll *pll)
428 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
429 struct ccs_pll_limits lim = {
431 .min_pre_pll_clk_div = CCS_LIM(sensor, MIN_PRE_PLL_CLK_DIV),
432 .max_pre_pll_clk_div = CCS_LIM(sensor, MAX_PRE_PLL_CLK_DIV),
433 .min_pll_ip_clk_freq_hz = CCS_LIM(sensor, MIN_PLL_IP_CLK_FREQ_MHZ),
434 .max_pll_ip_clk_freq_hz = CCS_LIM(sensor, MAX_PLL_IP_CLK_FREQ_MHZ),
435 .min_pll_multiplier = CCS_LIM(sensor, MIN_PLL_MULTIPLIER),
436 .max_pll_multiplier = CCS_LIM(sensor, MAX_PLL_MULTIPLIER),
437 .min_pll_op_clk_freq_hz = CCS_LIM(sensor, MIN_PLL_OP_CLK_FREQ_MHZ),
438 .max_pll_op_clk_freq_hz = CCS_LIM(sensor, MAX_PLL_OP_CLK_FREQ_MHZ),
441 .min_pre_pll_clk_div = CCS_LIM(sensor, MIN_OP_PRE_PLL_CLK_DIV),
442 .max_pre_pll_clk_div = CCS_LIM(sensor, MAX_OP_PRE_PLL_CLK_DIV),
443 .min_pll_ip_clk_freq_hz = CCS_LIM(sensor, MIN_OP_PLL_IP_CLK_FREQ_MHZ),
444 .max_pll_ip_clk_freq_hz = CCS_LIM(sensor, MAX_OP_PLL_IP_CLK_FREQ_MHZ),
445 .min_pll_multiplier = CCS_LIM(sensor, MIN_OP_PLL_MULTIPLIER),
446 .max_pll_multiplier = CCS_LIM(sensor, MAX_OP_PLL_MULTIPLIER),
447 .min_pll_op_clk_freq_hz = CCS_LIM(sensor, MIN_OP_PLL_OP_CLK_FREQ_MHZ),
448 .max_pll_op_clk_freq_hz = CCS_LIM(sensor, MAX_OP_PLL_OP_CLK_FREQ_MHZ),
451 .min_sys_clk_div = CCS_LIM(sensor, MIN_OP_SYS_CLK_DIV),
452 .max_sys_clk_div = CCS_LIM(sensor, MAX_OP_SYS_CLK_DIV),
453 .min_pix_clk_div = CCS_LIM(sensor, MIN_OP_PIX_CLK_DIV),
454 .max_pix_clk_div = CCS_LIM(sensor, MAX_OP_PIX_CLK_DIV),
455 .min_sys_clk_freq_hz = CCS_LIM(sensor, MIN_OP_SYS_CLK_FREQ_MHZ),
456 .max_sys_clk_freq_hz = CCS_LIM(sensor, MAX_OP_SYS_CLK_FREQ_MHZ),
457 .min_pix_clk_freq_hz = CCS_LIM(sensor, MIN_OP_PIX_CLK_FREQ_MHZ),
458 .max_pix_clk_freq_hz = CCS_LIM(sensor, MAX_OP_PIX_CLK_FREQ_MHZ),
461 .min_sys_clk_div = CCS_LIM(sensor, MIN_VT_SYS_CLK_DIV),
462 .max_sys_clk_div = CCS_LIM(sensor, MAX_VT_SYS_CLK_DIV),
463 .min_pix_clk_div = CCS_LIM(sensor, MIN_VT_PIX_CLK_DIV),
464 .max_pix_clk_div = CCS_LIM(sensor, MAX_VT_PIX_CLK_DIV),
465 .min_sys_clk_freq_hz = CCS_LIM(sensor, MIN_VT_SYS_CLK_FREQ_MHZ),
466 .max_sys_clk_freq_hz = CCS_LIM(sensor, MAX_VT_SYS_CLK_FREQ_MHZ),
467 .min_pix_clk_freq_hz = CCS_LIM(sensor, MIN_VT_PIX_CLK_FREQ_MHZ),
468 .max_pix_clk_freq_hz = CCS_LIM(sensor, MAX_VT_PIX_CLK_FREQ_MHZ),
470 .min_line_length_pck_bin = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK_BIN),
471 .min_line_length_pck = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK),
474 return ccs_pll_calculate(&client->dev, &lim, pll);
477 static int ccs_pll_update(struct ccs_sensor *sensor)
479 struct ccs_pll *pll = &sensor->pll;
482 pll->binning_horizontal = sensor->binning_horizontal;
483 pll->binning_vertical = sensor->binning_vertical;
485 sensor->link_freq->qmenu_int[sensor->link_freq->val];
486 pll->scale_m = sensor->scale_m;
487 pll->bits_per_pixel = sensor->csi_format->compressed;
489 rval = ccs_pll_try(sensor, pll);
493 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_parray,
494 pll->pixel_rate_pixel_array);
495 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_csi, pll->pixel_rate_csi);
503 * V4L2 Controls handling
507 static void __ccs_update_exposure_limits(struct ccs_sensor *sensor)
509 struct v4l2_ctrl *ctrl = sensor->exposure;
512 max = sensor->pixel_array->crop[CCS_PA_PAD_SRC].height
513 + sensor->vblank->val
514 - CCS_LIM(sensor, COARSE_INTEGRATION_TIME_MAX_MARGIN);
516 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, ctrl->step, max);
522 * 1. Bits-per-pixel, descending.
523 * 2. Bits-per-pixel compressed, descending.
524 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
525 * orders must be defined.
527 static const struct ccs_csi_data_format ccs_csi_data_formats[] = {
528 { MEDIA_BUS_FMT_SGRBG16_1X16, 16, 16, CCS_PIXEL_ORDER_GRBG, },
529 { MEDIA_BUS_FMT_SRGGB16_1X16, 16, 16, CCS_PIXEL_ORDER_RGGB, },
530 { MEDIA_BUS_FMT_SBGGR16_1X16, 16, 16, CCS_PIXEL_ORDER_BGGR, },
531 { MEDIA_BUS_FMT_SGBRG16_1X16, 16, 16, CCS_PIXEL_ORDER_GBRG, },
532 { MEDIA_BUS_FMT_SGRBG14_1X14, 14, 14, CCS_PIXEL_ORDER_GRBG, },
533 { MEDIA_BUS_FMT_SRGGB14_1X14, 14, 14, CCS_PIXEL_ORDER_RGGB, },
534 { MEDIA_BUS_FMT_SBGGR14_1X14, 14, 14, CCS_PIXEL_ORDER_BGGR, },
535 { MEDIA_BUS_FMT_SGBRG14_1X14, 14, 14, CCS_PIXEL_ORDER_GBRG, },
536 { MEDIA_BUS_FMT_SGRBG12_1X12, 12, 12, CCS_PIXEL_ORDER_GRBG, },
537 { MEDIA_BUS_FMT_SRGGB12_1X12, 12, 12, CCS_PIXEL_ORDER_RGGB, },
538 { MEDIA_BUS_FMT_SBGGR12_1X12, 12, 12, CCS_PIXEL_ORDER_BGGR, },
539 { MEDIA_BUS_FMT_SGBRG12_1X12, 12, 12, CCS_PIXEL_ORDER_GBRG, },
540 { MEDIA_BUS_FMT_SGRBG10_1X10, 10, 10, CCS_PIXEL_ORDER_GRBG, },
541 { MEDIA_BUS_FMT_SRGGB10_1X10, 10, 10, CCS_PIXEL_ORDER_RGGB, },
542 { MEDIA_BUS_FMT_SBGGR10_1X10, 10, 10, CCS_PIXEL_ORDER_BGGR, },
543 { MEDIA_BUS_FMT_SGBRG10_1X10, 10, 10, CCS_PIXEL_ORDER_GBRG, },
544 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_GRBG, },
545 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_RGGB, },
546 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_BGGR, },
547 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_GBRG, },
548 { MEDIA_BUS_FMT_SGRBG8_1X8, 8, 8, CCS_PIXEL_ORDER_GRBG, },
549 { MEDIA_BUS_FMT_SRGGB8_1X8, 8, 8, CCS_PIXEL_ORDER_RGGB, },
550 { MEDIA_BUS_FMT_SBGGR8_1X8, 8, 8, CCS_PIXEL_ORDER_BGGR, },
551 { MEDIA_BUS_FMT_SGBRG8_1X8, 8, 8, CCS_PIXEL_ORDER_GBRG, },
554 static const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
556 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
557 - (unsigned long)ccs_csi_data_formats) \
558 / sizeof(*ccs_csi_data_formats))
560 static u32 ccs_pixel_order(struct ccs_sensor *sensor)
562 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
566 if (sensor->hflip->val)
567 flip |= CCS_IMAGE_ORIENTATION_HORIZONTAL_MIRROR;
569 if (sensor->vflip->val)
570 flip |= CCS_IMAGE_ORIENTATION_VERTICAL_FLIP;
573 flip ^= sensor->hvflip_inv_mask;
575 dev_dbg(&client->dev, "flip %d\n", flip);
576 return sensor->default_pixel_order ^ flip;
579 static void ccs_update_mbus_formats(struct ccs_sensor *sensor)
581 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
582 unsigned int csi_format_idx =
583 to_csi_format_idx(sensor->csi_format) & ~3;
584 unsigned int internal_csi_format_idx =
585 to_csi_format_idx(sensor->internal_csi_format) & ~3;
586 unsigned int pixel_order = ccs_pixel_order(sensor);
588 if (WARN_ON_ONCE(max(internal_csi_format_idx, csi_format_idx) +
589 pixel_order >= ARRAY_SIZE(ccs_csi_data_formats)))
592 sensor->mbus_frame_fmts =
593 sensor->default_mbus_frame_fmts << pixel_order;
595 &ccs_csi_data_formats[csi_format_idx + pixel_order];
596 sensor->internal_csi_format =
597 &ccs_csi_data_formats[internal_csi_format_idx
600 dev_dbg(&client->dev, "new pixel order %s\n",
601 pixel_order_str[pixel_order]);
604 static const char * const ccs_test_patterns[] = {
607 "Eight Vertical Colour Bars",
608 "Colour Bars With Fade to Grey",
609 "Pseudorandom Sequence (PN9)",
612 static int ccs_set_ctrl(struct v4l2_ctrl *ctrl)
614 struct ccs_sensor *sensor =
615 container_of(ctrl->handler, struct ccs_subdev, ctrl_handler)
617 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
627 if (sensor->streaming)
630 if (sensor->hflip->val)
631 orient |= CCS_IMAGE_ORIENTATION_HORIZONTAL_MIRROR;
633 if (sensor->vflip->val)
634 orient |= CCS_IMAGE_ORIENTATION_VERTICAL_FLIP;
636 orient ^= sensor->hvflip_inv_mask;
638 ccs_update_mbus_formats(sensor);
641 case V4L2_CID_VBLANK:
642 exposure = sensor->exposure->val;
644 __ccs_update_exposure_limits(sensor);
646 if (exposure > sensor->exposure->maximum) {
647 sensor->exposure->val = sensor->exposure->maximum;
648 rval = ccs_set_ctrl(sensor->exposure);
654 case V4L2_CID_LINK_FREQ:
655 if (sensor->streaming)
658 rval = ccs_pll_update(sensor);
663 case V4L2_CID_TEST_PATTERN:
664 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
666 sensor->test_data[i],
668 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR);
673 pm_status = pm_runtime_get_if_active(&client->dev, true);
678 case V4L2_CID_ANALOGUE_GAIN:
679 rval = ccs_write(sensor, ANALOG_GAIN_CODE_GLOBAL, ctrl->val);
683 case V4L2_CID_CCS_ANALOGUE_LINEAR_GAIN:
684 rval = ccs_write(sensor, ANALOG_LINEAR_GAIN_GLOBAL, ctrl->val);
688 case V4L2_CID_CCS_ANALOGUE_EXPONENTIAL_GAIN:
689 rval = ccs_write(sensor, ANALOG_EXPONENTIAL_GAIN_GLOBAL,
694 case V4L2_CID_DIGITAL_GAIN:
695 if (CCS_LIM(sensor, DIGITAL_GAIN_CAPABILITY) ==
696 CCS_DIGITAL_GAIN_CAPABILITY_GLOBAL) {
697 rval = ccs_write(sensor, DIGITAL_GAIN_GLOBAL,
702 rval = ccs_write_addr(sensor,
703 SMIAPP_REG_U16_DIGITAL_GAIN_GREENR,
708 rval = ccs_write_addr(sensor,
709 SMIAPP_REG_U16_DIGITAL_GAIN_RED,
714 rval = ccs_write_addr(sensor,
715 SMIAPP_REG_U16_DIGITAL_GAIN_BLUE,
720 rval = ccs_write_addr(sensor,
721 SMIAPP_REG_U16_DIGITAL_GAIN_GREENB,
725 case V4L2_CID_EXPOSURE:
726 rval = ccs_write(sensor, COARSE_INTEGRATION_TIME, ctrl->val);
731 rval = ccs_write(sensor, IMAGE_ORIENTATION, orient);
734 case V4L2_CID_VBLANK:
735 rval = ccs_write(sensor, FRAME_LENGTH_LINES,
736 sensor->pixel_array->crop[
737 CCS_PA_PAD_SRC].height
741 case V4L2_CID_HBLANK:
742 rval = ccs_write(sensor, LINE_LENGTH_PCK,
743 sensor->pixel_array->crop[CCS_PA_PAD_SRC].width
747 case V4L2_CID_TEST_PATTERN:
748 rval = ccs_write(sensor, TEST_PATTERN_MODE, ctrl->val);
751 case V4L2_CID_TEST_PATTERN_RED:
752 rval = ccs_write(sensor, TEST_DATA_RED, ctrl->val);
755 case V4L2_CID_TEST_PATTERN_GREENR:
756 rval = ccs_write(sensor, TEST_DATA_GREENR, ctrl->val);
759 case V4L2_CID_TEST_PATTERN_BLUE:
760 rval = ccs_write(sensor, TEST_DATA_BLUE, ctrl->val);
763 case V4L2_CID_TEST_PATTERN_GREENB:
764 rval = ccs_write(sensor, TEST_DATA_GREENB, ctrl->val);
767 case V4L2_CID_CCS_SHADING_CORRECTION:
768 rval = ccs_write(sensor, SHADING_CORRECTION_EN,
769 ctrl->val ? CCS_SHADING_CORRECTION_EN_ENABLE :
772 if (!rval && sensor->luminance_level)
773 v4l2_ctrl_activate(sensor->luminance_level, ctrl->val);
776 case V4L2_CID_CCS_LUMINANCE_CORRECTION_LEVEL:
777 rval = ccs_write(sensor, LUMINANCE_CORRECTION_LEVEL, ctrl->val);
780 case V4L2_CID_PIXEL_RATE:
781 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
790 pm_runtime_mark_last_busy(&client->dev);
791 pm_runtime_put_autosuspend(&client->dev);
797 static const struct v4l2_ctrl_ops ccs_ctrl_ops = {
798 .s_ctrl = ccs_set_ctrl,
801 static int ccs_init_controls(struct ccs_sensor *sensor)
803 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
806 rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 17);
810 sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
812 switch (CCS_LIM(sensor, ANALOG_GAIN_CAPABILITY)) {
813 case CCS_ANALOG_GAIN_CAPABILITY_GLOBAL: {
818 } const gain_ctrls[] = {
819 { "Analogue Gain m0", V4L2_CID_CCS_ANALOGUE_GAIN_M0,
820 CCS_LIM(sensor, ANALOG_GAIN_M0), },
821 { "Analogue Gain c0", V4L2_CID_CCS_ANALOGUE_GAIN_C0,
822 CCS_LIM(sensor, ANALOG_GAIN_C0), },
823 { "Analogue Gain m1", V4L2_CID_CCS_ANALOGUE_GAIN_M1,
824 CCS_LIM(sensor, ANALOG_GAIN_M1), },
825 { "Analogue Gain c1", V4L2_CID_CCS_ANALOGUE_GAIN_C1,
826 CCS_LIM(sensor, ANALOG_GAIN_C1), },
828 struct v4l2_ctrl_config ctrl_cfg = {
829 .type = V4L2_CTRL_TYPE_INTEGER,
830 .ops = &ccs_ctrl_ops,
831 .flags = V4L2_CTRL_FLAG_READ_ONLY,
836 for (i = 0; i < ARRAY_SIZE(gain_ctrls); i++) {
837 ctrl_cfg.name = gain_ctrls[i].name;
838 ctrl_cfg.id = gain_ctrls[i].id;
839 ctrl_cfg.min = ctrl_cfg.max = ctrl_cfg.def =
842 v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler,
846 v4l2_ctrl_new_std(&sensor->pixel_array->ctrl_handler,
847 &ccs_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
848 CCS_LIM(sensor, ANALOG_GAIN_CODE_MIN),
849 CCS_LIM(sensor, ANALOG_GAIN_CODE_MAX),
850 max(CCS_LIM(sensor, ANALOG_GAIN_CODE_STEP),
852 CCS_LIM(sensor, ANALOG_GAIN_CODE_MIN));
856 case CCS_ANALOG_GAIN_CAPABILITY_ALTERNATE_GLOBAL: {
861 } const gain_ctrls[] = {
863 "Analogue Linear Gain",
864 V4L2_CID_CCS_ANALOGUE_LINEAR_GAIN,
865 CCS_LIM(sensor, ANALOG_LINEAR_GAIN_MIN),
866 CCS_LIM(sensor, ANALOG_LINEAR_GAIN_MAX),
868 ANALOG_LINEAR_GAIN_STEP_SIZE),
872 "Analogue Exponential Gain",
873 V4L2_CID_CCS_ANALOGUE_EXPONENTIAL_GAIN,
874 CCS_LIM(sensor, ANALOG_EXPONENTIAL_GAIN_MIN),
875 CCS_LIM(sensor, ANALOG_EXPONENTIAL_GAIN_MAX),
877 ANALOG_EXPONENTIAL_GAIN_STEP_SIZE),
881 struct v4l2_ctrl_config ctrl_cfg = {
882 .type = V4L2_CTRL_TYPE_INTEGER,
883 .ops = &ccs_ctrl_ops,
887 for (i = 0; i < ARRAY_SIZE(gain_ctrls); i++) {
888 ctrl_cfg.name = gain_ctrls[i].name;
889 ctrl_cfg.min = ctrl_cfg.def = gain_ctrls[i].min;
890 ctrl_cfg.max = gain_ctrls[i].max;
891 ctrl_cfg.step = gain_ctrls[i].step;
892 ctrl_cfg.id = gain_ctrls[i].id;
894 v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler,
900 if (CCS_LIM(sensor, SHADING_CORRECTION_CAPABILITY) &
901 (CCS_SHADING_CORRECTION_CAPABILITY_COLOR_SHADING |
902 CCS_SHADING_CORRECTION_CAPABILITY_LUMINANCE_CORRECTION)) {
903 const struct v4l2_ctrl_config ctrl_cfg = {
904 .name = "Shading Correction",
905 .type = V4L2_CTRL_TYPE_BOOLEAN,
906 .id = V4L2_CID_CCS_SHADING_CORRECTION,
907 .ops = &ccs_ctrl_ops,
912 v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler,
916 if (CCS_LIM(sensor, SHADING_CORRECTION_CAPABILITY) &
917 CCS_SHADING_CORRECTION_CAPABILITY_LUMINANCE_CORRECTION) {
918 const struct v4l2_ctrl_config ctrl_cfg = {
919 .name = "Luminance Correction Level",
920 .type = V4L2_CTRL_TYPE_BOOLEAN,
921 .id = V4L2_CID_CCS_LUMINANCE_CORRECTION_LEVEL,
922 .ops = &ccs_ctrl_ops,
928 sensor->luminance_level =
929 v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler,
933 if (CCS_LIM(sensor, DIGITAL_GAIN_CAPABILITY) ==
934 CCS_DIGITAL_GAIN_CAPABILITY_GLOBAL ||
935 CCS_LIM(sensor, DIGITAL_GAIN_CAPABILITY) ==
936 SMIAPP_DIGITAL_GAIN_CAPABILITY_PER_CHANNEL)
937 v4l2_ctrl_new_std(&sensor->pixel_array->ctrl_handler,
938 &ccs_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
939 CCS_LIM(sensor, DIGITAL_GAIN_MIN),
940 CCS_LIM(sensor, DIGITAL_GAIN_MAX),
941 max(CCS_LIM(sensor, DIGITAL_GAIN_STEP_SIZE),
945 /* Exposure limits will be updated soon, use just something here. */
946 sensor->exposure = v4l2_ctrl_new_std(
947 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
948 V4L2_CID_EXPOSURE, 0, 0, 1, 0);
950 sensor->hflip = v4l2_ctrl_new_std(
951 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
952 V4L2_CID_HFLIP, 0, 1, 1, 0);
953 sensor->vflip = v4l2_ctrl_new_std(
954 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
955 V4L2_CID_VFLIP, 0, 1, 1, 0);
957 sensor->vblank = v4l2_ctrl_new_std(
958 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
959 V4L2_CID_VBLANK, 0, 1, 1, 0);
962 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
964 sensor->hblank = v4l2_ctrl_new_std(
965 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
966 V4L2_CID_HBLANK, 0, 1, 1, 0);
969 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
971 sensor->pixel_rate_parray = v4l2_ctrl_new_std(
972 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
973 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
975 v4l2_ctrl_new_std_menu_items(&sensor->pixel_array->ctrl_handler,
976 &ccs_ctrl_ops, V4L2_CID_TEST_PATTERN,
977 ARRAY_SIZE(ccs_test_patterns) - 1,
978 0, 0, ccs_test_patterns);
980 if (sensor->pixel_array->ctrl_handler.error) {
981 dev_err(&client->dev,
982 "pixel array controls initialization failed (%d)\n",
983 sensor->pixel_array->ctrl_handler.error);
984 return sensor->pixel_array->ctrl_handler.error;
987 sensor->pixel_array->sd.ctrl_handler =
988 &sensor->pixel_array->ctrl_handler;
990 v4l2_ctrl_cluster(2, &sensor->hflip);
992 rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
996 sensor->src->ctrl_handler.lock = &sensor->mutex;
998 sensor->pixel_rate_csi = v4l2_ctrl_new_std(
999 &sensor->src->ctrl_handler, &ccs_ctrl_ops,
1000 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
1002 if (sensor->src->ctrl_handler.error) {
1003 dev_err(&client->dev,
1004 "src controls initialization failed (%d)\n",
1005 sensor->src->ctrl_handler.error);
1006 return sensor->src->ctrl_handler.error;
1009 sensor->src->sd.ctrl_handler = &sensor->src->ctrl_handler;
1015 * For controls that require information on available media bus codes
1016 * and linke frequencies.
1018 static int ccs_init_late_controls(struct ccs_sensor *sensor)
1020 unsigned long *valid_link_freqs = &sensor->valid_link_freqs[
1021 sensor->csi_format->compressed - sensor->compressed_min_bpp];
1024 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) {
1025 int max_value = (1 << sensor->csi_format->width) - 1;
1027 sensor->test_data[i] = v4l2_ctrl_new_std(
1028 &sensor->pixel_array->ctrl_handler,
1029 &ccs_ctrl_ops, V4L2_CID_TEST_PATTERN_RED + i,
1030 0, max_value, 1, max_value);
1033 sensor->link_freq = v4l2_ctrl_new_int_menu(
1034 &sensor->src->ctrl_handler, &ccs_ctrl_ops,
1035 V4L2_CID_LINK_FREQ, __fls(*valid_link_freqs),
1036 __ffs(*valid_link_freqs), sensor->hwcfg.op_sys_clock);
1038 return sensor->src->ctrl_handler.error;
1041 static void ccs_free_controls(struct ccs_sensor *sensor)
1045 for (i = 0; i < sensor->ssds_used; i++)
1046 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
1049 static int ccs_get_mbus_formats(struct ccs_sensor *sensor)
1051 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1052 struct ccs_pll *pll = &sensor->pll;
1053 u8 compressed_max_bpp = 0;
1054 unsigned int type, n;
1055 unsigned int i, pixel_order;
1058 type = CCS_LIM(sensor, DATA_FORMAT_MODEL_TYPE);
1060 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
1062 rval = ccs_read(sensor, PIXEL_ORDER, &pixel_order);
1066 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
1067 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
1071 dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
1072 pixel_order_str[pixel_order]);
1075 case CCS_DATA_FORMAT_MODEL_TYPE_NORMAL:
1076 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
1078 case CCS_DATA_FORMAT_MODEL_TYPE_EXTENDED:
1079 n = CCS_LIM_DATA_FORMAT_DESCRIPTOR_MAX_N + 1;
1085 sensor->default_pixel_order = pixel_order;
1086 sensor->mbus_frame_fmts = 0;
1088 for (i = 0; i < n; i++) {
1089 unsigned int fmt, j;
1091 fmt = CCS_LIM_AT(sensor, DATA_FORMAT_DESCRIPTOR, i);
1093 dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n",
1094 i, fmt >> 8, (u8)fmt);
1096 for (j = 0; j < ARRAY_SIZE(ccs_csi_data_formats); j++) {
1097 const struct ccs_csi_data_format *f =
1098 &ccs_csi_data_formats[j];
1100 if (f->pixel_order != CCS_PIXEL_ORDER_GRBG)
1103 if (f->width != fmt >>
1104 CCS_DATA_FORMAT_DESCRIPTOR_UNCOMPRESSED_SHIFT ||
1106 (fmt & CCS_DATA_FORMAT_DESCRIPTOR_COMPRESSED_MASK))
1109 dev_dbg(&client->dev, "jolly good! %d\n", j);
1111 sensor->default_mbus_frame_fmts |= 1 << j;
1115 /* Figure out which BPP values can be used with which formats. */
1116 pll->binning_horizontal = 1;
1117 pll->binning_vertical = 1;
1118 pll->scale_m = sensor->scale_m;
1120 for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) {
1121 sensor->compressed_min_bpp =
1122 min(ccs_csi_data_formats[i].compressed,
1123 sensor->compressed_min_bpp);
1124 compressed_max_bpp =
1125 max(ccs_csi_data_formats[i].compressed,
1126 compressed_max_bpp);
1129 sensor->valid_link_freqs = devm_kcalloc(
1131 compressed_max_bpp - sensor->compressed_min_bpp + 1,
1132 sizeof(*sensor->valid_link_freqs), GFP_KERNEL);
1133 if (!sensor->valid_link_freqs)
1136 for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) {
1137 const struct ccs_csi_data_format *f =
1138 &ccs_csi_data_formats[i];
1139 unsigned long *valid_link_freqs =
1140 &sensor->valid_link_freqs[
1141 f->compressed - sensor->compressed_min_bpp];
1144 if (!(sensor->default_mbus_frame_fmts & 1 << i))
1147 pll->bits_per_pixel = f->compressed;
1149 for (j = 0; sensor->hwcfg.op_sys_clock[j]; j++) {
1150 pll->link_freq = sensor->hwcfg.op_sys_clock[j];
1152 rval = ccs_pll_try(sensor, pll);
1153 dev_dbg(&client->dev, "link freq %u Hz, bpp %u %s\n",
1154 pll->link_freq, pll->bits_per_pixel,
1155 rval ? "not ok" : "ok");
1159 set_bit(j, valid_link_freqs);
1162 if (!*valid_link_freqs) {
1163 dev_info(&client->dev,
1164 "no valid link frequencies for %u bpp\n",
1166 sensor->default_mbus_frame_fmts &= ~BIT(i);
1170 if (!sensor->csi_format
1171 || f->width > sensor->csi_format->width
1172 || (f->width == sensor->csi_format->width
1173 && f->compressed > sensor->csi_format->compressed)) {
1174 sensor->csi_format = f;
1175 sensor->internal_csi_format = f;
1179 if (!sensor->csi_format) {
1180 dev_err(&client->dev, "no supported mbus code found\n");
1184 ccs_update_mbus_formats(sensor);
1189 static void ccs_update_blanking(struct ccs_sensor *sensor)
1191 struct v4l2_ctrl *vblank = sensor->vblank;
1192 struct v4l2_ctrl *hblank = sensor->hblank;
1193 u16 min_fll, max_fll, min_llp, max_llp, min_lbp;
1196 if (sensor->binning_vertical > 1 || sensor->binning_horizontal > 1) {
1197 min_fll = CCS_LIM(sensor, MIN_FRAME_LENGTH_LINES_BIN);
1198 max_fll = CCS_LIM(sensor, MAX_FRAME_LENGTH_LINES_BIN);
1199 min_llp = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK_BIN);
1200 max_llp = CCS_LIM(sensor, MAX_LINE_LENGTH_PCK_BIN);
1201 min_lbp = CCS_LIM(sensor, MIN_LINE_BLANKING_PCK_BIN);
1203 min_fll = CCS_LIM(sensor, MIN_FRAME_LENGTH_LINES);
1204 max_fll = CCS_LIM(sensor, MAX_FRAME_LENGTH_LINES);
1205 min_llp = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK);
1206 max_llp = CCS_LIM(sensor, MAX_LINE_LENGTH_PCK);
1207 min_lbp = CCS_LIM(sensor, MIN_LINE_BLANKING_PCK);
1211 CCS_LIM(sensor, MIN_FRAME_BLANKING_LINES),
1212 min_fll - sensor->pixel_array->crop[CCS_PA_PAD_SRC].height);
1213 max = max_fll - sensor->pixel_array->crop[CCS_PA_PAD_SRC].height;
1215 __v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min);
1218 min_llp - sensor->pixel_array->crop[CCS_PA_PAD_SRC].width,
1220 max = max_llp - sensor->pixel_array->crop[CCS_PA_PAD_SRC].width;
1222 __v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min);
1224 __ccs_update_exposure_limits(sensor);
1227 static int ccs_pll_blanking_update(struct ccs_sensor *sensor)
1229 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1232 rval = ccs_pll_update(sensor);
1236 /* Output from pixel array, including blanking */
1237 ccs_update_blanking(sensor);
1239 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
1240 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
1242 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
1243 sensor->pll.pixel_rate_pixel_array /
1244 ((sensor->pixel_array->crop[CCS_PA_PAD_SRC].width
1245 + sensor->hblank->val) *
1246 (sensor->pixel_array->crop[CCS_PA_PAD_SRC].height
1247 + sensor->vblank->val) / 100));
1254 * SMIA++ NVM handling
1258 static int ccs_read_nvm_page(struct ccs_sensor *sensor, u32 p, u8 *nvm,
1267 rval = ccs_write(sensor, DATA_TRANSFER_IF_1_PAGE_SELECT, p);
1271 rval = ccs_write(sensor, DATA_TRANSFER_IF_1_CTRL,
1272 CCS_DATA_TRANSFER_IF_1_CTRL_ENABLE);
1276 rval = ccs_read(sensor, DATA_TRANSFER_IF_1_STATUS, &s);
1280 if (s & CCS_DATA_TRANSFER_IF_1_STATUS_IMPROPER_IF_USAGE) {
1285 if (CCS_LIM(sensor, DATA_TRANSFER_IF_CAPABILITY) &
1286 CCS_DATA_TRANSFER_IF_CAPABILITY_POLLING) {
1287 for (i = 1000; i > 0; i--) {
1288 if (s & CCS_DATA_TRANSFER_IF_1_STATUS_READ_IF_READY)
1291 rval = ccs_read(sensor, DATA_TRANSFER_IF_1_STATUS, &s);
1300 for (i = 0; i <= CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P; i++) {
1303 rval = ccs_read(sensor, DATA_TRANSFER_IF_1_DATA(i), &v);
1313 static int ccs_read_nvm(struct ccs_sensor *sensor, unsigned char *nvm,
1318 int rval = 0, rval2;
1320 for (p = 0; p < nvm_size / (CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P + 1)
1322 rval = ccs_read_nvm_page(sensor, p, nvm, &status);
1323 nvm += CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P + 1;
1326 if (rval == -ENODATA &&
1327 status & CCS_DATA_TRANSFER_IF_1_STATUS_IMPROPER_IF_USAGE)
1330 rval2 = ccs_write(sensor, DATA_TRANSFER_IF_1_CTRL, 0);
1334 return rval2 ?: p * (CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P + 1);
1339 * SMIA++ CCI address control
1342 static int ccs_change_cci_addr(struct ccs_sensor *sensor)
1344 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1348 client->addr = sensor->hwcfg.i2c_addr_dfl;
1350 rval = ccs_write(sensor, CCI_ADDRESS_CTRL,
1351 sensor->hwcfg.i2c_addr_alt << 1);
1355 client->addr = sensor->hwcfg.i2c_addr_alt;
1357 /* verify addr change went ok */
1358 rval = ccs_read(sensor, CCI_ADDRESS_CTRL, &val);
1362 if (val != sensor->hwcfg.i2c_addr_alt << 1)
1370 * SMIA++ Mode Control
1373 static int ccs_setup_flash_strobe(struct ccs_sensor *sensor)
1375 struct ccs_flash_strobe_parms *strobe_setup;
1376 unsigned int ext_freq = sensor->hwcfg.ext_clk;
1378 u32 strobe_adjustment;
1379 u32 strobe_width_high_rs;
1382 strobe_setup = sensor->hwcfg.strobe_setup;
1385 * How to calculate registers related to strobe length. Please
1386 * do not change, or if you do at least know what you're
1389 * Sakari Ailus <sakari.ailus@linux.intel.com> 2010-10-25
1391 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1392 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1394 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1395 * flash_strobe_adjustment E N, [1 - 0xff]
1397 * The formula above is written as below to keep it on one
1400 * l / 10^6 = w / e * a
1402 * Let's mark w * a by x:
1410 * The strobe width must be at least as long as requested,
1411 * thus rounding upwards is needed.
1413 * x = (l * e + 10^6 - 1) / 10^6
1414 * -----------------------------
1416 * Maximum possible accuracy is wanted at all times. Thus keep
1417 * a as small as possible.
1419 * Calculate a, assuming maximum w, with rounding upwards:
1421 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1422 * -------------------------------------
1424 * Thus, we also get w, with that a, with rounding upwards:
1426 * w = (x + a - 1) / a
1427 * -------------------
1431 * x E [1, (2^16 - 1) * (2^8 - 1)]
1433 * Substituting maximum x to the original formula (with rounding),
1434 * the maximum l is thus
1436 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1438 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1439 * --------------------------------------------------
1441 * flash_strobe_length must be clamped between 1 and
1442 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1446 * flash_strobe_adjustment = ((flash_strobe_length *
1447 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1449 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1450 * EXTCLK freq + 10^6 - 1) / 10^6 +
1451 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1453 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1454 1000000 + 1, ext_freq);
1455 strobe_setup->strobe_width_high_us =
1456 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1458 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1459 1000000 - 1), 1000000ULL);
1460 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1461 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1464 rval = ccs_write(sensor, FLASH_MODE_RS, strobe_setup->mode);
1468 rval = ccs_write(sensor, FLASH_STROBE_ADJUSTMENT, strobe_adjustment);
1472 rval = ccs_write(sensor, TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1473 strobe_width_high_rs);
1477 rval = ccs_write(sensor, TFLASH_STROBE_DELAY_RS_CTRL,
1478 strobe_setup->strobe_delay);
1482 rval = ccs_write(sensor, FLASH_STROBE_START_POINT,
1483 strobe_setup->stobe_start_point);
1487 rval = ccs_write(sensor, FLASH_TRIGGER_RS, strobe_setup->trigger);
1490 sensor->hwcfg.strobe_setup->trigger = 0;
1495 /* -----------------------------------------------------------------------------
1499 static int ccs_write_msr_regs(struct ccs_sensor *sensor)
1503 rval = ccs_write_data_regs(sensor,
1504 sensor->sdata.sensor_manufacturer_regs,
1505 sensor->sdata.num_sensor_manufacturer_regs);
1509 return ccs_write_data_regs(sensor,
1510 sensor->mdata.module_manufacturer_regs,
1511 sensor->mdata.num_module_manufacturer_regs);
1514 static int ccs_update_phy_ctrl(struct ccs_sensor *sensor)
1516 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1519 if (!sensor->ccs_limits)
1522 if (CCS_LIM(sensor, PHY_CTRL_CAPABILITY) &
1523 CCS_PHY_CTRL_CAPABILITY_AUTO_PHY_CTL) {
1524 val = CCS_PHY_CTRL_AUTO;
1525 } else if (CCS_LIM(sensor, PHY_CTRL_CAPABILITY) &
1526 CCS_PHY_CTRL_CAPABILITY_UI_PHY_CTL) {
1527 val = CCS_PHY_CTRL_UI;
1529 dev_err(&client->dev, "manual PHY control not supported\n");
1533 return ccs_write(sensor, PHY_CTRL, val);
1536 static int ccs_power_on(struct device *dev)
1538 struct v4l2_subdev *subdev = dev_get_drvdata(dev);
1539 struct ccs_subdev *ssd = to_ccs_subdev(subdev);
1541 * The sub-device related to the I2C device is always the
1542 * source one, i.e. ssds[0].
1544 struct ccs_sensor *sensor =
1545 container_of(ssd, struct ccs_sensor, ssds[0]);
1546 const struct ccs_device *ccsdev = device_get_match_data(dev);
1549 rval = regulator_bulk_enable(ARRAY_SIZE(ccs_regulators),
1550 sensor->regulators);
1552 dev_err(dev, "failed to enable vana regulator\n");
1556 if (sensor->reset || sensor->xshutdown || sensor->ext_clk) {
1559 rval = clk_prepare_enable(sensor->ext_clk);
1561 dev_dbg(dev, "failed to enable xclk\n");
1565 gpiod_set_value(sensor->reset, 0);
1566 gpiod_set_value(sensor->xshutdown, 1);
1568 if (ccsdev->flags & CCS_DEVICE_FLAG_IS_SMIA)
1569 sleep = SMIAPP_RESET_DELAY(sensor->hwcfg.ext_clk);
1573 usleep_range(sleep, sleep);
1577 * Failures to respond to the address change command have been noticed.
1578 * Those failures seem to be caused by the sensor requiring a longer
1579 * boot time than advertised. An additional 10ms delay seems to work
1580 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1581 * unnecessary. The failures need to be investigated to find a proper
1582 * fix, and a delay will likely need to be added here if the I2C write
1583 * retry hack is reverted before the root cause of the boot time issue
1587 if (!sensor->reset && !sensor->xshutdown) {
1591 rval = ccs_write(sensor, SOFTWARE_RESET, CCS_SOFTWARE_RESET_ON);
1593 dev_err(dev, "software reset failed\n");
1594 goto out_cci_addr_fail;
1598 rval = ccs_read(sensor, SOFTWARE_RESET, &reset);
1599 reset = !rval && reset == CCS_SOFTWARE_RESET_OFF;
1603 usleep_range(1000, 2000);
1610 if (sensor->hwcfg.i2c_addr_alt) {
1611 rval = ccs_change_cci_addr(sensor);
1613 dev_err(dev, "cci address change error\n");
1614 goto out_cci_addr_fail;
1618 rval = ccs_write(sensor, COMPRESSION_MODE,
1619 CCS_COMPRESSION_MODE_DPCM_PCM_SIMPLE);
1621 dev_err(dev, "compression mode set failed\n");
1622 goto out_cci_addr_fail;
1625 rval = ccs_write(sensor, EXTCLK_FREQUENCY_MHZ,
1626 sensor->hwcfg.ext_clk / (1000000 / (1 << 8)));
1628 dev_err(dev, "extclk frequency set failed\n");
1629 goto out_cci_addr_fail;
1632 rval = ccs_write(sensor, CSI_LANE_MODE, sensor->hwcfg.lanes - 1);
1634 dev_err(dev, "csi lane mode set failed\n");
1635 goto out_cci_addr_fail;
1638 rval = ccs_write(sensor, FAST_STANDBY_CTRL,
1639 CCS_FAST_STANDBY_CTRL_FRAME_TRUNCATION);
1641 dev_err(dev, "fast standby set failed\n");
1642 goto out_cci_addr_fail;
1645 rval = ccs_write(sensor, CSI_SIGNALING_MODE,
1646 sensor->hwcfg.csi_signalling_mode);
1648 dev_err(dev, "csi signalling mode set failed\n");
1649 goto out_cci_addr_fail;
1652 rval = ccs_update_phy_ctrl(sensor);
1654 goto out_cci_addr_fail;
1656 rval = ccs_write_msr_regs(sensor);
1658 goto out_cci_addr_fail;
1660 rval = ccs_call_quirk(sensor, post_poweron);
1662 dev_err(dev, "post_poweron quirks failed\n");
1663 goto out_cci_addr_fail;
1669 gpiod_set_value(sensor->reset, 1);
1670 gpiod_set_value(sensor->xshutdown, 0);
1671 clk_disable_unprepare(sensor->ext_clk);
1674 regulator_bulk_disable(ARRAY_SIZE(ccs_regulators),
1675 sensor->regulators);
1680 static int ccs_power_off(struct device *dev)
1682 struct v4l2_subdev *subdev = dev_get_drvdata(dev);
1683 struct ccs_subdev *ssd = to_ccs_subdev(subdev);
1684 struct ccs_sensor *sensor =
1685 container_of(ssd, struct ccs_sensor, ssds[0]);
1688 * Currently power/clock to lens are enable/disabled separately
1689 * but they are essentially the same signals. So if the sensor is
1690 * powered off while the lens is powered on the sensor does not
1691 * really see a power off and next time the cci address change
1692 * will fail. So do a soft reset explicitly here.
1694 if (sensor->hwcfg.i2c_addr_alt)
1695 ccs_write(sensor, SOFTWARE_RESET, CCS_SOFTWARE_RESET_ON);
1697 gpiod_set_value(sensor->reset, 1);
1698 gpiod_set_value(sensor->xshutdown, 0);
1699 clk_disable_unprepare(sensor->ext_clk);
1700 usleep_range(5000, 5000);
1701 regulator_bulk_disable(ARRAY_SIZE(ccs_regulators),
1702 sensor->regulators);
1703 sensor->streaming = false;
1708 /* -----------------------------------------------------------------------------
1709 * Video stream management
1712 static int ccs_start_streaming(struct ccs_sensor *sensor)
1714 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1715 unsigned int binning_mode;
1718 mutex_lock(&sensor->mutex);
1720 rval = ccs_write(sensor, CSI_DATA_FORMAT,
1721 (sensor->csi_format->width << 8) |
1722 sensor->csi_format->compressed);
1726 /* Binning configuration */
1727 if (sensor->binning_horizontal == 1 &&
1728 sensor->binning_vertical == 1) {
1732 (sensor->binning_horizontal << 4)
1733 | sensor->binning_vertical;
1735 rval = ccs_write(sensor, BINNING_TYPE, binning_type);
1741 rval = ccs_write(sensor, BINNING_MODE, binning_mode);
1746 rval = ccs_pll_configure(sensor);
1750 /* Analog crop start coordinates */
1751 rval = ccs_write(sensor, X_ADDR_START,
1752 sensor->pixel_array->crop[CCS_PA_PAD_SRC].left);
1756 rval = ccs_write(sensor, Y_ADDR_START,
1757 sensor->pixel_array->crop[CCS_PA_PAD_SRC].top);
1761 /* Analog crop end coordinates */
1764 sensor->pixel_array->crop[CCS_PA_PAD_SRC].left
1765 + sensor->pixel_array->crop[CCS_PA_PAD_SRC].width - 1);
1771 sensor->pixel_array->crop[CCS_PA_PAD_SRC].top
1772 + sensor->pixel_array->crop[CCS_PA_PAD_SRC].height - 1);
1777 * Output from pixel array, including blanking, is set using
1778 * controls below. No need to set here.
1782 if (CCS_LIM(sensor, DIGITAL_CROP_CAPABILITY)
1783 == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1785 sensor, DIGITAL_CROP_X_OFFSET,
1786 sensor->scaler->crop[CCS_PAD_SINK].left);
1791 sensor, DIGITAL_CROP_Y_OFFSET,
1792 sensor->scaler->crop[CCS_PAD_SINK].top);
1797 sensor, DIGITAL_CROP_IMAGE_WIDTH,
1798 sensor->scaler->crop[CCS_PAD_SINK].width);
1803 sensor, DIGITAL_CROP_IMAGE_HEIGHT,
1804 sensor->scaler->crop[CCS_PAD_SINK].height);
1810 if (CCS_LIM(sensor, SCALING_CAPABILITY)
1811 != CCS_SCALING_CAPABILITY_NONE) {
1812 rval = ccs_write(sensor, SCALING_MODE, sensor->scaling_mode);
1816 rval = ccs_write(sensor, SCALE_M, sensor->scale_m);
1821 /* Output size from sensor */
1822 rval = ccs_write(sensor, X_OUTPUT_SIZE,
1823 sensor->src->crop[CCS_PAD_SRC].width);
1826 rval = ccs_write(sensor, Y_OUTPUT_SIZE,
1827 sensor->src->crop[CCS_PAD_SRC].height);
1831 if (CCS_LIM(sensor, FLASH_MODE_CAPABILITY) &
1832 (CCS_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1833 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE) &&
1834 sensor->hwcfg.strobe_setup != NULL &&
1835 sensor->hwcfg.strobe_setup->trigger != 0) {
1836 rval = ccs_setup_flash_strobe(sensor);
1841 rval = ccs_call_quirk(sensor, pre_streamon);
1843 dev_err(&client->dev, "pre_streamon quirks failed\n");
1847 rval = ccs_write(sensor, MODE_SELECT, CCS_MODE_SELECT_STREAMING);
1850 mutex_unlock(&sensor->mutex);
1855 static int ccs_stop_streaming(struct ccs_sensor *sensor)
1857 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1860 mutex_lock(&sensor->mutex);
1861 rval = ccs_write(sensor, MODE_SELECT, CCS_MODE_SELECT_SOFTWARE_STANDBY);
1865 rval = ccs_call_quirk(sensor, post_streamoff);
1867 dev_err(&client->dev, "post_streamoff quirks failed\n");
1870 mutex_unlock(&sensor->mutex);
1874 /* -----------------------------------------------------------------------------
1875 * V4L2 subdev video operations
1878 static int ccs_pm_get_init(struct ccs_sensor *sensor)
1880 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1884 * It can't use pm_runtime_resume_and_get() here, as the driver
1885 * relies at the returned value to detect if the device was already
1888 rval = pm_runtime_get_sync(&client->dev);
1892 /* Device was already active, so don't set controls */
1896 /* Restore V4L2 controls to the previously suspended device */
1897 rval = v4l2_ctrl_handler_setup(&sensor->pixel_array->ctrl_handler);
1901 rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1905 /* Keep PM runtime usage_count incremented on success */
1908 pm_runtime_put(&client->dev);
1912 static int ccs_set_stream(struct v4l2_subdev *subdev, int enable)
1914 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
1915 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1918 if (sensor->streaming == enable)
1922 ccs_stop_streaming(sensor);
1923 sensor->streaming = false;
1924 pm_runtime_mark_last_busy(&client->dev);
1925 pm_runtime_put_autosuspend(&client->dev);
1930 rval = ccs_pm_get_init(sensor);
1934 sensor->streaming = true;
1936 rval = ccs_start_streaming(sensor);
1938 sensor->streaming = false;
1939 pm_runtime_mark_last_busy(&client->dev);
1940 pm_runtime_put_autosuspend(&client->dev);
1946 static int ccs_pre_streamon(struct v4l2_subdev *subdev, u32 flags)
1948 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
1949 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1952 if (flags & V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP) {
1953 switch (sensor->hwcfg.csi_signalling_mode) {
1954 case CCS_CSI_SIGNALING_MODE_CSI_2_DPHY:
1955 if (!(CCS_LIM(sensor, PHY_CTRL_CAPABILITY_2) &
1956 CCS_PHY_CTRL_CAPABILITY_2_MANUAL_LP_DPHY))
1959 case CCS_CSI_SIGNALING_MODE_CSI_2_CPHY:
1960 if (!(CCS_LIM(sensor, PHY_CTRL_CAPABILITY_2) &
1961 CCS_PHY_CTRL_CAPABILITY_2_MANUAL_LP_CPHY))
1969 rval = ccs_pm_get_init(sensor);
1973 if (flags & V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP) {
1974 rval = ccs_write(sensor, MANUAL_LP_CTRL,
1975 CCS_MANUAL_LP_CTRL_ENABLE);
1977 pm_runtime_put(&client->dev);
1983 static int ccs_post_streamoff(struct v4l2_subdev *subdev)
1985 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
1986 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1988 return pm_runtime_put(&client->dev);
1991 static int ccs_enum_mbus_code(struct v4l2_subdev *subdev,
1992 struct v4l2_subdev_state *sd_state,
1993 struct v4l2_subdev_mbus_code_enum *code)
1995 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1996 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2001 mutex_lock(&sensor->mutex);
2003 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
2004 subdev->name, code->pad, code->index);
2006 if (subdev != &sensor->src->sd || code->pad != CCS_PAD_SRC) {
2010 code->code = sensor->internal_csi_format->code;
2015 for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) {
2016 if (sensor->mbus_frame_fmts & (1 << i))
2019 if (idx == code->index) {
2020 code->code = ccs_csi_data_formats[i].code;
2021 dev_err(&client->dev, "found index %d, i %d, code %x\n",
2022 code->index, i, code->code);
2029 mutex_unlock(&sensor->mutex);
2034 static u32 __ccs_get_mbus_code(struct v4l2_subdev *subdev, unsigned int pad)
2036 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2038 if (subdev == &sensor->src->sd && pad == CCS_PAD_SRC)
2039 return sensor->csi_format->code;
2041 return sensor->internal_csi_format->code;
2044 static int __ccs_get_format(struct v4l2_subdev *subdev,
2045 struct v4l2_subdev_state *sd_state,
2046 struct v4l2_subdev_format *fmt)
2048 struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2050 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2051 fmt->format = *v4l2_subdev_get_try_format(subdev, sd_state,
2054 struct v4l2_rect *r;
2056 if (fmt->pad == ssd->source_pad)
2057 r = &ssd->crop[ssd->source_pad];
2061 fmt->format.code = __ccs_get_mbus_code(subdev, fmt->pad);
2062 fmt->format.width = r->width;
2063 fmt->format.height = r->height;
2064 fmt->format.field = V4L2_FIELD_NONE;
2070 static int ccs_get_format(struct v4l2_subdev *subdev,
2071 struct v4l2_subdev_state *sd_state,
2072 struct v4l2_subdev_format *fmt)
2074 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2077 mutex_lock(&sensor->mutex);
2078 rval = __ccs_get_format(subdev, sd_state, fmt);
2079 mutex_unlock(&sensor->mutex);
2084 static void ccs_get_crop_compose(struct v4l2_subdev *subdev,
2085 struct v4l2_subdev_state *sd_state,
2086 struct v4l2_rect **crops,
2087 struct v4l2_rect **comps, int which)
2089 struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2092 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2094 for (i = 0; i < subdev->entity.num_pads; i++)
2095 crops[i] = &ssd->crop[i];
2097 *comps = &ssd->compose;
2100 for (i = 0; i < subdev->entity.num_pads; i++)
2101 crops[i] = v4l2_subdev_get_try_crop(subdev,
2106 *comps = v4l2_subdev_get_try_compose(subdev, sd_state,
2111 /* Changes require propagation only on sink pad. */
2112 static void ccs_propagate(struct v4l2_subdev *subdev,
2113 struct v4l2_subdev_state *sd_state, int which,
2116 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2117 struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2118 struct v4l2_rect *comp, *crops[CCS_PADS];
2120 ccs_get_crop_compose(subdev, sd_state, crops, &comp, which);
2123 case V4L2_SEL_TGT_CROP:
2124 comp->width = crops[CCS_PAD_SINK]->width;
2125 comp->height = crops[CCS_PAD_SINK]->height;
2126 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2127 if (ssd == sensor->scaler) {
2128 sensor->scale_m = CCS_LIM(sensor, SCALER_N_MIN);
2129 sensor->scaling_mode =
2130 CCS_SCALING_MODE_NO_SCALING;
2131 } else if (ssd == sensor->binner) {
2132 sensor->binning_horizontal = 1;
2133 sensor->binning_vertical = 1;
2137 case V4L2_SEL_TGT_COMPOSE:
2138 *crops[CCS_PAD_SRC] = *comp;
2145 static const struct ccs_csi_data_format
2146 *ccs_validate_csi_data_format(struct ccs_sensor *sensor, u32 code)
2150 for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) {
2151 if (sensor->mbus_frame_fmts & (1 << i) &&
2152 ccs_csi_data_formats[i].code == code)
2153 return &ccs_csi_data_formats[i];
2156 return sensor->csi_format;
2159 static int ccs_set_format_source(struct v4l2_subdev *subdev,
2160 struct v4l2_subdev_state *sd_state,
2161 struct v4l2_subdev_format *fmt)
2163 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2164 const struct ccs_csi_data_format *csi_format,
2165 *old_csi_format = sensor->csi_format;
2166 unsigned long *valid_link_freqs;
2167 u32 code = fmt->format.code;
2171 rval = __ccs_get_format(subdev, sd_state, fmt);
2176 * Media bus code is changeable on src subdev's source pad. On
2177 * other source pads we just get format here.
2179 if (subdev != &sensor->src->sd)
2182 csi_format = ccs_validate_csi_data_format(sensor, code);
2184 fmt->format.code = csi_format->code;
2186 if (fmt->which != V4L2_SUBDEV_FORMAT_ACTIVE)
2189 sensor->csi_format = csi_format;
2191 if (csi_format->width != old_csi_format->width)
2192 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
2193 __v4l2_ctrl_modify_range(
2194 sensor->test_data[i], 0,
2195 (1 << csi_format->width) - 1, 1, 0);
2197 if (csi_format->compressed == old_csi_format->compressed)
2201 &sensor->valid_link_freqs[sensor->csi_format->compressed
2202 - sensor->compressed_min_bpp];
2204 __v4l2_ctrl_modify_range(
2205 sensor->link_freq, 0,
2206 __fls(*valid_link_freqs), ~*valid_link_freqs,
2207 __ffs(*valid_link_freqs));
2209 return ccs_pll_update(sensor);
2212 static int ccs_set_format(struct v4l2_subdev *subdev,
2213 struct v4l2_subdev_state *sd_state,
2214 struct v4l2_subdev_format *fmt)
2216 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2217 struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2218 struct v4l2_rect *crops[CCS_PADS];
2220 mutex_lock(&sensor->mutex);
2222 if (fmt->pad == ssd->source_pad) {
2225 rval = ccs_set_format_source(subdev, sd_state, fmt);
2227 mutex_unlock(&sensor->mutex);
2232 /* Sink pad. Width and height are changeable here. */
2233 fmt->format.code = __ccs_get_mbus_code(subdev, fmt->pad);
2234 fmt->format.width &= ~1;
2235 fmt->format.height &= ~1;
2236 fmt->format.field = V4L2_FIELD_NONE;
2239 clamp(fmt->format.width,
2240 CCS_LIM(sensor, MIN_X_OUTPUT_SIZE),
2241 CCS_LIM(sensor, MAX_X_OUTPUT_SIZE));
2242 fmt->format.height =
2243 clamp(fmt->format.height,
2244 CCS_LIM(sensor, MIN_Y_OUTPUT_SIZE),
2245 CCS_LIM(sensor, MAX_Y_OUTPUT_SIZE));
2247 ccs_get_crop_compose(subdev, sd_state, crops, NULL, fmt->which);
2249 crops[ssd->sink_pad]->left = 0;
2250 crops[ssd->sink_pad]->top = 0;
2251 crops[ssd->sink_pad]->width = fmt->format.width;
2252 crops[ssd->sink_pad]->height = fmt->format.height;
2253 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
2254 ssd->sink_fmt = *crops[ssd->sink_pad];
2255 ccs_propagate(subdev, sd_state, fmt->which, V4L2_SEL_TGT_CROP);
2257 mutex_unlock(&sensor->mutex);
2263 * Calculate goodness of scaled image size compared to expected image
2264 * size and flags provided.
2266 #define SCALING_GOODNESS 100000
2267 #define SCALING_GOODNESS_EXTREME 100000000
2268 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
2269 int h, int ask_h, u32 flags)
2271 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2272 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2280 if (flags & V4L2_SEL_FLAG_GE) {
2282 val -= SCALING_GOODNESS;
2284 val -= SCALING_GOODNESS;
2287 if (flags & V4L2_SEL_FLAG_LE) {
2289 val -= SCALING_GOODNESS;
2291 val -= SCALING_GOODNESS;
2294 val -= abs(w - ask_w);
2295 val -= abs(h - ask_h);
2297 if (w < CCS_LIM(sensor, MIN_X_OUTPUT_SIZE))
2298 val -= SCALING_GOODNESS_EXTREME;
2300 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
2301 w, ask_w, h, ask_h, val);
2306 static void ccs_set_compose_binner(struct v4l2_subdev *subdev,
2307 struct v4l2_subdev_state *sd_state,
2308 struct v4l2_subdev_selection *sel,
2309 struct v4l2_rect **crops,
2310 struct v4l2_rect *comp)
2312 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2314 unsigned int binh = 1, binv = 1;
2315 int best = scaling_goodness(
2317 crops[CCS_PAD_SINK]->width, sel->r.width,
2318 crops[CCS_PAD_SINK]->height, sel->r.height, sel->flags);
2320 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2321 int this = scaling_goodness(
2323 crops[CCS_PAD_SINK]->width
2324 / sensor->binning_subtypes[i].horizontal,
2326 crops[CCS_PAD_SINK]->height
2327 / sensor->binning_subtypes[i].vertical,
2328 sel->r.height, sel->flags);
2331 binh = sensor->binning_subtypes[i].horizontal;
2332 binv = sensor->binning_subtypes[i].vertical;
2336 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2337 sensor->binning_vertical = binv;
2338 sensor->binning_horizontal = binh;
2341 sel->r.width = (crops[CCS_PAD_SINK]->width / binh) & ~1;
2342 sel->r.height = (crops[CCS_PAD_SINK]->height / binv) & ~1;
2346 * Calculate best scaling ratio and mode for given output resolution.
2348 * Try all of these: horizontal ratio, vertical ratio and smallest
2349 * size possible (horizontally).
2351 * Also try whether horizontal scaler or full scaler gives a better
2354 static void ccs_set_compose_scaler(struct v4l2_subdev *subdev,
2355 struct v4l2_subdev_state *sd_state,
2356 struct v4l2_subdev_selection *sel,
2357 struct v4l2_rect **crops,
2358 struct v4l2_rect *comp)
2360 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2361 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2362 u32 min, max, a, b, max_m;
2363 u32 scale_m = CCS_LIM(sensor, SCALER_N_MIN);
2364 int mode = CCS_SCALING_MODE_HORIZONTAL;
2370 sel->r.width = min_t(unsigned int, sel->r.width,
2371 crops[CCS_PAD_SINK]->width);
2372 sel->r.height = min_t(unsigned int, sel->r.height,
2373 crops[CCS_PAD_SINK]->height);
2375 a = crops[CCS_PAD_SINK]->width
2376 * CCS_LIM(sensor, SCALER_N_MIN) / sel->r.width;
2377 b = crops[CCS_PAD_SINK]->height
2378 * CCS_LIM(sensor, SCALER_N_MIN) / sel->r.height;
2379 max_m = crops[CCS_PAD_SINK]->width
2380 * CCS_LIM(sensor, SCALER_N_MIN)
2381 / CCS_LIM(sensor, MIN_X_OUTPUT_SIZE);
2383 a = clamp(a, CCS_LIM(sensor, SCALER_M_MIN),
2384 CCS_LIM(sensor, SCALER_M_MAX));
2385 b = clamp(b, CCS_LIM(sensor, SCALER_M_MIN),
2386 CCS_LIM(sensor, SCALER_M_MAX));
2387 max_m = clamp(max_m, CCS_LIM(sensor, SCALER_M_MIN),
2388 CCS_LIM(sensor, SCALER_M_MAX));
2390 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
2392 min = min(max_m, min(a, b));
2393 max = min(max_m, max(a, b));
2402 try[ntry] = min + 1;
2405 try[ntry] = max + 1;
2410 for (i = 0; i < ntry; i++) {
2411 int this = scaling_goodness(
2413 crops[CCS_PAD_SINK]->width
2414 / try[i] * CCS_LIM(sensor, SCALER_N_MIN),
2416 crops[CCS_PAD_SINK]->height,
2420 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
2424 mode = CCS_SCALING_MODE_HORIZONTAL;
2428 if (CCS_LIM(sensor, SCALING_CAPABILITY)
2429 == CCS_SCALING_CAPABILITY_HORIZONTAL)
2432 this = scaling_goodness(
2433 subdev, crops[CCS_PAD_SINK]->width
2435 * CCS_LIM(sensor, SCALER_N_MIN),
2437 crops[CCS_PAD_SINK]->height
2439 * CCS_LIM(sensor, SCALER_N_MIN),
2445 mode = SMIAPP_SCALING_MODE_BOTH;
2451 (crops[CCS_PAD_SINK]->width
2453 * CCS_LIM(sensor, SCALER_N_MIN)) & ~1;
2454 if (mode == SMIAPP_SCALING_MODE_BOTH)
2456 (crops[CCS_PAD_SINK]->height
2458 * CCS_LIM(sensor, SCALER_N_MIN))
2461 sel->r.height = crops[CCS_PAD_SINK]->height;
2463 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2464 sensor->scale_m = scale_m;
2465 sensor->scaling_mode = mode;
2468 /* We're only called on source pads. This function sets scaling. */
2469 static int ccs_set_compose(struct v4l2_subdev *subdev,
2470 struct v4l2_subdev_state *sd_state,
2471 struct v4l2_subdev_selection *sel)
2473 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2474 struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2475 struct v4l2_rect *comp, *crops[CCS_PADS];
2477 ccs_get_crop_compose(subdev, sd_state, crops, &comp, sel->which);
2482 if (ssd == sensor->binner)
2483 ccs_set_compose_binner(subdev, sd_state, sel, crops, comp);
2485 ccs_set_compose_scaler(subdev, sd_state, sel, crops, comp);
2488 ccs_propagate(subdev, sd_state, sel->which, V4L2_SEL_TGT_COMPOSE);
2490 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
2491 return ccs_pll_blanking_update(sensor);
2496 static int __ccs_sel_supported(struct v4l2_subdev *subdev,
2497 struct v4l2_subdev_selection *sel)
2499 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2500 struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2502 /* We only implement crop in three places. */
2503 switch (sel->target) {
2504 case V4L2_SEL_TGT_CROP:
2505 case V4L2_SEL_TGT_CROP_BOUNDS:
2506 if (ssd == sensor->pixel_array && sel->pad == CCS_PA_PAD_SRC)
2508 if (ssd == sensor->src && sel->pad == CCS_PAD_SRC)
2510 if (ssd == sensor->scaler && sel->pad == CCS_PAD_SINK &&
2511 CCS_LIM(sensor, DIGITAL_CROP_CAPABILITY)
2512 == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
2515 case V4L2_SEL_TGT_NATIVE_SIZE:
2516 if (ssd == sensor->pixel_array && sel->pad == CCS_PA_PAD_SRC)
2519 case V4L2_SEL_TGT_COMPOSE:
2520 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2521 if (sel->pad == ssd->source_pad)
2523 if (ssd == sensor->binner)
2525 if (ssd == sensor->scaler && CCS_LIM(sensor, SCALING_CAPABILITY)
2526 != CCS_SCALING_CAPABILITY_NONE)
2534 static int ccs_set_crop(struct v4l2_subdev *subdev,
2535 struct v4l2_subdev_state *sd_state,
2536 struct v4l2_subdev_selection *sel)
2538 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2539 struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2540 struct v4l2_rect *src_size, *crops[CCS_PADS];
2541 struct v4l2_rect _r;
2543 ccs_get_crop_compose(subdev, sd_state, crops, NULL, sel->which);
2545 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2546 if (sel->pad == ssd->sink_pad)
2547 src_size = &ssd->sink_fmt;
2549 src_size = &ssd->compose;
2551 if (sel->pad == ssd->sink_pad) {
2554 _r.width = v4l2_subdev_get_try_format(subdev,
2558 _r.height = v4l2_subdev_get_try_format(subdev,
2564 src_size = v4l2_subdev_get_try_compose(
2565 subdev, sd_state, ssd->sink_pad);
2569 if (ssd == sensor->src && sel->pad == CCS_PAD_SRC) {
2574 sel->r.width = min(sel->r.width, src_size->width);
2575 sel->r.height = min(sel->r.height, src_size->height);
2577 sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width);
2578 sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height);
2580 *crops[sel->pad] = sel->r;
2582 if (ssd != sensor->pixel_array && sel->pad == CCS_PAD_SINK)
2583 ccs_propagate(subdev, sd_state, sel->which, V4L2_SEL_TGT_CROP);
2588 static void ccs_get_native_size(struct ccs_subdev *ssd, struct v4l2_rect *r)
2592 r->width = CCS_LIM(ssd->sensor, X_ADDR_MAX) + 1;
2593 r->height = CCS_LIM(ssd->sensor, Y_ADDR_MAX) + 1;
2596 static int __ccs_get_selection(struct v4l2_subdev *subdev,
2597 struct v4l2_subdev_state *sd_state,
2598 struct v4l2_subdev_selection *sel)
2600 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2601 struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2602 struct v4l2_rect *comp, *crops[CCS_PADS];
2603 struct v4l2_rect sink_fmt;
2606 ret = __ccs_sel_supported(subdev, sel);
2610 ccs_get_crop_compose(subdev, sd_state, crops, &comp, sel->which);
2612 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2613 sink_fmt = ssd->sink_fmt;
2615 struct v4l2_mbus_framefmt *fmt =
2616 v4l2_subdev_get_try_format(subdev, sd_state,
2621 sink_fmt.width = fmt->width;
2622 sink_fmt.height = fmt->height;
2625 switch (sel->target) {
2626 case V4L2_SEL_TGT_CROP_BOUNDS:
2627 case V4L2_SEL_TGT_NATIVE_SIZE:
2628 if (ssd == sensor->pixel_array)
2629 ccs_get_native_size(ssd, &sel->r);
2630 else if (sel->pad == ssd->sink_pad)
2635 case V4L2_SEL_TGT_CROP:
2636 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2637 sel->r = *crops[sel->pad];
2639 case V4L2_SEL_TGT_COMPOSE:
2647 static int ccs_get_selection(struct v4l2_subdev *subdev,
2648 struct v4l2_subdev_state *sd_state,
2649 struct v4l2_subdev_selection *sel)
2651 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2654 mutex_lock(&sensor->mutex);
2655 rval = __ccs_get_selection(subdev, sd_state, sel);
2656 mutex_unlock(&sensor->mutex);
2661 static int ccs_set_selection(struct v4l2_subdev *subdev,
2662 struct v4l2_subdev_state *sd_state,
2663 struct v4l2_subdev_selection *sel)
2665 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2668 ret = __ccs_sel_supported(subdev, sel);
2672 mutex_lock(&sensor->mutex);
2674 sel->r.left = max(0, sel->r.left & ~1);
2675 sel->r.top = max(0, sel->r.top & ~1);
2676 sel->r.width = CCS_ALIGN_DIM(sel->r.width, sel->flags);
2677 sel->r.height = CCS_ALIGN_DIM(sel->r.height, sel->flags);
2679 sel->r.width = max_t(unsigned int, CCS_LIM(sensor, MIN_X_OUTPUT_SIZE),
2681 sel->r.height = max_t(unsigned int, CCS_LIM(sensor, MIN_Y_OUTPUT_SIZE),
2684 switch (sel->target) {
2685 case V4L2_SEL_TGT_CROP:
2686 ret = ccs_set_crop(subdev, sd_state, sel);
2688 case V4L2_SEL_TGT_COMPOSE:
2689 ret = ccs_set_compose(subdev, sd_state, sel);
2695 mutex_unlock(&sensor->mutex);
2699 static int ccs_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2701 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2703 *frames = sensor->frame_skip;
2707 static int ccs_get_skip_top_lines(struct v4l2_subdev *subdev, u32 *lines)
2709 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2711 *lines = sensor->image_start;
2716 /* -----------------------------------------------------------------------------
2721 nvm_show(struct device *dev, struct device_attribute *attr, char *buf)
2723 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2724 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2725 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2728 if (!sensor->dev_init_done)
2731 rval = ccs_pm_get_init(sensor);
2735 rval = ccs_read_nvm(sensor, buf, PAGE_SIZE);
2737 pm_runtime_put(&client->dev);
2738 dev_err(&client->dev, "nvm read failed\n");
2742 pm_runtime_mark_last_busy(&client->dev);
2743 pm_runtime_put_autosuspend(&client->dev);
2746 * NVM is still way below a PAGE_SIZE, so we can safely
2747 * assume this for now.
2751 static DEVICE_ATTR_RO(nvm);
2754 ident_show(struct device *dev, struct device_attribute *attr, char *buf)
2756 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2757 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2758 struct ccs_module_info *minfo = &sensor->minfo;
2760 if (minfo->mipi_manufacturer_id)
2761 return snprintf(buf, PAGE_SIZE, "%4.4x%4.4x%2.2x\n",
2762 minfo->mipi_manufacturer_id, minfo->model_id,
2763 minfo->revision_number) + 1;
2765 return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2766 minfo->smia_manufacturer_id, minfo->model_id,
2767 minfo->revision_number) + 1;
2769 static DEVICE_ATTR_RO(ident);
2771 /* -----------------------------------------------------------------------------
2772 * V4L2 subdev core operations
2775 static int ccs_identify_module(struct ccs_sensor *sensor)
2777 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2778 struct ccs_module_info *minfo = &sensor->minfo;
2784 rval = ccs_read(sensor, MODULE_MANUFACTURER_ID,
2785 &minfo->mipi_manufacturer_id);
2786 if (!rval && !minfo->mipi_manufacturer_id)
2787 rval = ccs_read_addr_8only(sensor,
2788 SMIAPP_REG_U8_MANUFACTURER_ID,
2789 &minfo->smia_manufacturer_id);
2791 rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_MODEL_ID,
2794 rval = ccs_read_addr_8only(sensor,
2795 CCS_R_MODULE_REVISION_NUMBER_MAJOR,
2798 rval = ccs_read_addr_8only(sensor,
2799 CCS_R_MODULE_REVISION_NUMBER_MINOR,
2800 &minfo->revision_number);
2801 minfo->revision_number |= rev << 8;
2804 rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_DATE_YEAR,
2805 &minfo->module_year);
2807 rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_DATE_MONTH,
2808 &minfo->module_month);
2810 rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_DATE_DAY,
2811 &minfo->module_day);
2815 rval = ccs_read(sensor, SENSOR_MANUFACTURER_ID,
2816 &minfo->sensor_mipi_manufacturer_id);
2817 if (!rval && !minfo->sensor_mipi_manufacturer_id)
2818 rval = ccs_read_addr_8only(sensor,
2819 CCS_R_SENSOR_MANUFACTURER_ID,
2820 &minfo->sensor_smia_manufacturer_id);
2822 rval = ccs_read_addr_8only(sensor,
2823 CCS_R_SENSOR_MODEL_ID,
2824 &minfo->sensor_model_id);
2826 rval = ccs_read_addr_8only(sensor,
2827 CCS_R_SENSOR_REVISION_NUMBER,
2828 &minfo->sensor_revision_number);
2830 rval = ccs_read_addr_8only(sensor,
2831 CCS_R_SENSOR_FIRMWARE_VERSION,
2832 &minfo->sensor_firmware_version);
2836 rval = ccs_read(sensor, MIPI_CCS_VERSION, &minfo->ccs_version);
2837 if (!rval && !minfo->ccs_version)
2838 rval = ccs_read_addr_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2839 &minfo->smia_version);
2840 if (!rval && !minfo->ccs_version)
2841 rval = ccs_read_addr_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2842 &minfo->smiapp_version);
2845 dev_err(&client->dev, "sensor detection failed\n");
2849 if (minfo->mipi_manufacturer_id)
2850 dev_dbg(&client->dev, "MIPI CCS module 0x%4.4x-0x%4.4x\n",
2851 minfo->mipi_manufacturer_id, minfo->model_id);
2853 dev_dbg(&client->dev, "SMIA module 0x%2.2x-0x%4.4x\n",
2854 minfo->smia_manufacturer_id, minfo->model_id);
2856 dev_dbg(&client->dev,
2857 "module revision 0x%4.4x date %2.2d-%2.2d-%2.2d\n",
2858 minfo->revision_number, minfo->module_year, minfo->module_month,
2861 if (minfo->sensor_mipi_manufacturer_id)
2862 dev_dbg(&client->dev, "MIPI CCS sensor 0x%4.4x-0x%4.4x\n",
2863 minfo->sensor_mipi_manufacturer_id,
2864 minfo->sensor_model_id);
2866 dev_dbg(&client->dev, "SMIA sensor 0x%2.2x-0x%4.4x\n",
2867 minfo->sensor_smia_manufacturer_id,
2868 minfo->sensor_model_id);
2870 dev_dbg(&client->dev,
2871 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2872 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2874 if (minfo->ccs_version) {
2875 dev_dbg(&client->dev, "MIPI CCS version %u.%u",
2876 (minfo->ccs_version & CCS_MIPI_CCS_VERSION_MAJOR_MASK)
2877 >> CCS_MIPI_CCS_VERSION_MAJOR_SHIFT,
2878 (minfo->ccs_version & CCS_MIPI_CCS_VERSION_MINOR_MASK));
2879 minfo->name = CCS_NAME;
2881 dev_dbg(&client->dev,
2882 "smia version %2.2d smiapp version %2.2d\n",
2883 minfo->smia_version, minfo->smiapp_version);
2884 minfo->name = SMIAPP_NAME;
2888 * Some modules have bad data in the lvalues below. Hope the
2889 * rvalues have better stuff. The lvalues are module
2890 * parameters whereas the rvalues are sensor parameters.
2892 if (minfo->sensor_smia_manufacturer_id &&
2893 !minfo->smia_manufacturer_id && !minfo->model_id) {
2894 minfo->smia_manufacturer_id =
2895 minfo->sensor_smia_manufacturer_id;
2896 minfo->model_id = minfo->sensor_model_id;
2897 minfo->revision_number = minfo->sensor_revision_number;
2900 for (i = 0; i < ARRAY_SIZE(ccs_module_idents); i++) {
2901 if (ccs_module_idents[i].mipi_manufacturer_id &&
2902 ccs_module_idents[i].mipi_manufacturer_id
2903 != minfo->mipi_manufacturer_id)
2905 if (ccs_module_idents[i].smia_manufacturer_id &&
2906 ccs_module_idents[i].smia_manufacturer_id
2907 != minfo->smia_manufacturer_id)
2909 if (ccs_module_idents[i].model_id != minfo->model_id)
2911 if (ccs_module_idents[i].flags
2912 & CCS_MODULE_IDENT_FLAG_REV_LE) {
2913 if (ccs_module_idents[i].revision_number_major
2914 < (minfo->revision_number >> 8))
2917 if (ccs_module_idents[i].revision_number_major
2918 != (minfo->revision_number >> 8))
2922 minfo->name = ccs_module_idents[i].name;
2923 minfo->quirk = ccs_module_idents[i].quirk;
2927 if (i >= ARRAY_SIZE(ccs_module_idents))
2928 dev_warn(&client->dev,
2929 "no quirks for this module; let's hope it's fully compliant\n");
2931 dev_dbg(&client->dev, "the sensor is called %s\n", minfo->name);
2936 static const struct v4l2_subdev_ops ccs_ops;
2937 static const struct v4l2_subdev_internal_ops ccs_internal_ops;
2938 static const struct media_entity_operations ccs_entity_ops;
2940 static int ccs_register_subdev(struct ccs_sensor *sensor,
2941 struct ccs_subdev *ssd,
2942 struct ccs_subdev *sink_ssd,
2943 u16 source_pad, u16 sink_pad, u32 link_flags)
2945 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2951 rval = media_entity_pads_init(&ssd->sd.entity, ssd->npads, ssd->pads);
2953 dev_err(&client->dev, "media_entity_pads_init failed\n");
2957 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev, &ssd->sd);
2959 dev_err(&client->dev, "v4l2_device_register_subdev failed\n");
2963 rval = media_create_pad_link(&ssd->sd.entity, source_pad,
2964 &sink_ssd->sd.entity, sink_pad,
2967 dev_err(&client->dev, "media_create_pad_link failed\n");
2968 v4l2_device_unregister_subdev(&ssd->sd);
2975 static void ccs_unregistered(struct v4l2_subdev *subdev)
2977 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2980 for (i = 1; i < sensor->ssds_used; i++)
2981 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2984 static int ccs_registered(struct v4l2_subdev *subdev)
2986 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2989 if (sensor->scaler) {
2990 rval = ccs_register_subdev(sensor, sensor->binner,
2992 CCS_PAD_SRC, CCS_PAD_SINK,
2993 MEDIA_LNK_FL_ENABLED |
2994 MEDIA_LNK_FL_IMMUTABLE);
2999 rval = ccs_register_subdev(sensor, sensor->pixel_array, sensor->binner,
3000 CCS_PA_PAD_SRC, CCS_PAD_SINK,
3001 MEDIA_LNK_FL_ENABLED |
3002 MEDIA_LNK_FL_IMMUTABLE);
3009 ccs_unregistered(subdev);
3014 static void ccs_cleanup(struct ccs_sensor *sensor)
3016 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
3018 device_remove_file(&client->dev, &dev_attr_nvm);
3019 device_remove_file(&client->dev, &dev_attr_ident);
3021 ccs_free_controls(sensor);
3024 static void ccs_create_subdev(struct ccs_sensor *sensor,
3025 struct ccs_subdev *ssd, const char *name,
3026 unsigned short num_pads, u32 function)
3028 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
3033 if (ssd != sensor->src)
3034 v4l2_subdev_init(&ssd->sd, &ccs_ops);
3036 ssd->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
3037 ssd->sd.entity.function = function;
3038 ssd->sensor = sensor;
3040 ssd->npads = num_pads;
3041 ssd->source_pad = num_pads - 1;
3043 v4l2_i2c_subdev_set_name(&ssd->sd, client, sensor->minfo.name, name);
3045 ccs_get_native_size(ssd, &ssd->sink_fmt);
3047 ssd->compose.width = ssd->sink_fmt.width;
3048 ssd->compose.height = ssd->sink_fmt.height;
3049 ssd->crop[ssd->source_pad] = ssd->compose;
3050 ssd->pads[ssd->source_pad].flags = MEDIA_PAD_FL_SOURCE;
3051 if (ssd != sensor->pixel_array) {
3052 ssd->crop[ssd->sink_pad] = ssd->compose;
3053 ssd->pads[ssd->sink_pad].flags = MEDIA_PAD_FL_SINK;
3056 ssd->sd.entity.ops = &ccs_entity_ops;
3058 if (ssd == sensor->src)
3061 ssd->sd.internal_ops = &ccs_internal_ops;
3062 ssd->sd.owner = THIS_MODULE;
3063 ssd->sd.dev = &client->dev;
3064 v4l2_set_subdevdata(&ssd->sd, client);
3067 static int ccs_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
3069 struct ccs_subdev *ssd = to_ccs_subdev(sd);
3070 struct ccs_sensor *sensor = ssd->sensor;
3073 mutex_lock(&sensor->mutex);
3075 for (i = 0; i < ssd->npads; i++) {
3076 struct v4l2_mbus_framefmt *try_fmt =
3077 v4l2_subdev_get_try_format(sd, fh->state, i);
3078 struct v4l2_rect *try_crop =
3079 v4l2_subdev_get_try_crop(sd, fh->state, i);
3080 struct v4l2_rect *try_comp;
3082 ccs_get_native_size(ssd, try_crop);
3084 try_fmt->width = try_crop->width;
3085 try_fmt->height = try_crop->height;
3086 try_fmt->code = sensor->internal_csi_format->code;
3087 try_fmt->field = V4L2_FIELD_NONE;
3089 if (ssd != sensor->pixel_array)
3092 try_comp = v4l2_subdev_get_try_compose(sd, fh->state, i);
3093 *try_comp = *try_crop;
3096 mutex_unlock(&sensor->mutex);
3101 static const struct v4l2_subdev_video_ops ccs_video_ops = {
3102 .s_stream = ccs_set_stream,
3103 .pre_streamon = ccs_pre_streamon,
3104 .post_streamoff = ccs_post_streamoff,
3107 static const struct v4l2_subdev_pad_ops ccs_pad_ops = {
3108 .enum_mbus_code = ccs_enum_mbus_code,
3109 .get_fmt = ccs_get_format,
3110 .set_fmt = ccs_set_format,
3111 .get_selection = ccs_get_selection,
3112 .set_selection = ccs_set_selection,
3115 static const struct v4l2_subdev_sensor_ops ccs_sensor_ops = {
3116 .g_skip_frames = ccs_get_skip_frames,
3117 .g_skip_top_lines = ccs_get_skip_top_lines,
3120 static const struct v4l2_subdev_ops ccs_ops = {
3121 .video = &ccs_video_ops,
3122 .pad = &ccs_pad_ops,
3123 .sensor = &ccs_sensor_ops,
3126 static const struct media_entity_operations ccs_entity_ops = {
3127 .link_validate = v4l2_subdev_link_validate,
3130 static const struct v4l2_subdev_internal_ops ccs_internal_src_ops = {
3131 .registered = ccs_registered,
3132 .unregistered = ccs_unregistered,
3136 static const struct v4l2_subdev_internal_ops ccs_internal_ops = {
3140 /* -----------------------------------------------------------------------------
3144 static int __maybe_unused ccs_suspend(struct device *dev)
3146 struct i2c_client *client = to_i2c_client(dev);
3147 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3148 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
3149 bool streaming = sensor->streaming;
3152 rval = pm_runtime_resume_and_get(dev);
3156 if (sensor->streaming)
3157 ccs_stop_streaming(sensor);
3159 /* save state for resume */
3160 sensor->streaming = streaming;
3165 static int __maybe_unused ccs_resume(struct device *dev)
3167 struct i2c_client *client = to_i2c_client(dev);
3168 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3169 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
3172 pm_runtime_put(dev);
3174 if (sensor->streaming)
3175 rval = ccs_start_streaming(sensor);
3180 static int ccs_get_hwconfig(struct ccs_sensor *sensor, struct device *dev)
3182 struct ccs_hwconfig *hwcfg = &sensor->hwcfg;
3183 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = V4L2_MBUS_UNKNOWN };
3184 struct fwnode_handle *ep;
3185 struct fwnode_handle *fwnode = dev_fwnode(dev);
3190 ep = fwnode_graph_get_endpoint_by_id(fwnode, 0, 0,
3191 FWNODE_GRAPH_ENDPOINT_NEXT);
3196 * Note that we do need to rely on detecting the bus type between CSI-2
3197 * D-PHY and CCP2 as the old bindings did not require it.
3199 rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
3203 switch (bus_cfg.bus_type) {
3204 case V4L2_MBUS_CSI2_DPHY:
3205 hwcfg->csi_signalling_mode = CCS_CSI_SIGNALING_MODE_CSI_2_DPHY;
3206 hwcfg->lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
3208 case V4L2_MBUS_CSI2_CPHY:
3209 hwcfg->csi_signalling_mode = CCS_CSI_SIGNALING_MODE_CSI_2_CPHY;
3210 hwcfg->lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
3212 case V4L2_MBUS_CSI1:
3213 case V4L2_MBUS_CCP2:
3214 hwcfg->csi_signalling_mode = (bus_cfg.bus.mipi_csi1.strobe) ?
3215 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE :
3216 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK;
3220 dev_err(dev, "unsupported bus %u\n", bus_cfg.bus_type);
3225 dev_dbg(dev, "lanes %u\n", hwcfg->lanes);
3227 rval = fwnode_property_read_u32(fwnode, "rotation", &rotation);
3231 hwcfg->module_board_orient =
3232 CCS_MODULE_BOARD_ORIENT_180;
3237 dev_err(dev, "invalid rotation %u\n", rotation);
3243 rval = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
3246 dev_info(dev, "can't get clock-frequency\n");
3248 dev_dbg(dev, "clk %d, mode %d\n", hwcfg->ext_clk,
3249 hwcfg->csi_signalling_mode);
3251 if (!bus_cfg.nr_of_link_frequencies) {
3252 dev_warn(dev, "no link frequencies defined\n");
3257 hwcfg->op_sys_clock = devm_kcalloc(
3258 dev, bus_cfg.nr_of_link_frequencies + 1 /* guardian */,
3259 sizeof(*hwcfg->op_sys_clock), GFP_KERNEL);
3260 if (!hwcfg->op_sys_clock) {
3265 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
3266 hwcfg->op_sys_clock[i] = bus_cfg.link_frequencies[i];
3267 dev_dbg(dev, "freq %d: %lld\n", i, hwcfg->op_sys_clock[i]);
3270 v4l2_fwnode_endpoint_free(&bus_cfg);
3271 fwnode_handle_put(ep);
3276 v4l2_fwnode_endpoint_free(&bus_cfg);
3277 fwnode_handle_put(ep);
3282 static int ccs_probe(struct i2c_client *client)
3284 struct ccs_sensor *sensor;
3285 const struct firmware *fw;
3290 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
3294 rval = ccs_get_hwconfig(sensor, &client->dev);
3298 sensor->src = &sensor->ssds[sensor->ssds_used];
3300 v4l2_i2c_subdev_init(&sensor->src->sd, client, &ccs_ops);
3301 sensor->src->sd.internal_ops = &ccs_internal_src_ops;
3303 sensor->regulators = devm_kcalloc(&client->dev,
3304 ARRAY_SIZE(ccs_regulators),
3305 sizeof(*sensor->regulators),
3307 if (!sensor->regulators)
3310 for (i = 0; i < ARRAY_SIZE(ccs_regulators); i++)
3311 sensor->regulators[i].supply = ccs_regulators[i];
3313 rval = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(ccs_regulators),
3314 sensor->regulators);
3316 dev_err(&client->dev, "could not get regulators\n");
3320 sensor->ext_clk = devm_clk_get(&client->dev, NULL);
3321 if (PTR_ERR(sensor->ext_clk) == -ENOENT) {
3322 dev_info(&client->dev, "no clock defined, continuing...\n");
3323 sensor->ext_clk = NULL;
3324 } else if (IS_ERR(sensor->ext_clk)) {
3325 dev_err(&client->dev, "could not get clock (%ld)\n",
3326 PTR_ERR(sensor->ext_clk));
3327 return -EPROBE_DEFER;
3330 if (sensor->ext_clk) {
3331 if (sensor->hwcfg.ext_clk) {
3334 rval = clk_set_rate(sensor->ext_clk,
3335 sensor->hwcfg.ext_clk);
3337 dev_err(&client->dev,
3338 "unable to set clock freq to %u\n",
3339 sensor->hwcfg.ext_clk);
3343 rate = clk_get_rate(sensor->ext_clk);
3344 if (rate != sensor->hwcfg.ext_clk) {
3345 dev_err(&client->dev,
3346 "can't set clock freq, asked for %u but got %lu\n",
3347 sensor->hwcfg.ext_clk, rate);
3351 sensor->hwcfg.ext_clk = clk_get_rate(sensor->ext_clk);
3352 dev_dbg(&client->dev, "obtained clock freq %u\n",
3353 sensor->hwcfg.ext_clk);
3355 } else if (sensor->hwcfg.ext_clk) {
3356 dev_dbg(&client->dev, "assuming clock freq %u\n",
3357 sensor->hwcfg.ext_clk);
3359 dev_err(&client->dev, "unable to obtain clock freq\n");
3363 if (!sensor->hwcfg.ext_clk) {
3364 dev_err(&client->dev, "cannot work with xclk frequency 0\n");
3368 sensor->reset = devm_gpiod_get_optional(&client->dev, "reset",
3370 if (IS_ERR(sensor->reset))
3371 return PTR_ERR(sensor->reset);
3372 /* Support old users that may have used "xshutdown" property. */
3374 sensor->xshutdown = devm_gpiod_get_optional(&client->dev,
3377 if (IS_ERR(sensor->xshutdown))
3378 return PTR_ERR(sensor->xshutdown);
3380 rval = ccs_power_on(&client->dev);
3384 mutex_init(&sensor->mutex);
3386 rval = ccs_identify_module(sensor);
3392 rval = snprintf(filename, sizeof(filename),
3393 "ccs/ccs-sensor-%4.4x-%4.4x-%4.4x.fw",
3394 sensor->minfo.sensor_mipi_manufacturer_id,
3395 sensor->minfo.sensor_model_id,
3396 sensor->minfo.sensor_revision_number);
3397 if (rval >= sizeof(filename)) {
3402 rval = request_firmware(&fw, filename, &client->dev);
3404 ccs_data_parse(&sensor->sdata, fw->data, fw->size, &client->dev,
3406 release_firmware(fw);
3409 rval = snprintf(filename, sizeof(filename),
3410 "ccs/ccs-module-%4.4x-%4.4x-%4.4x.fw",
3411 sensor->minfo.mipi_manufacturer_id,
3412 sensor->minfo.model_id,
3413 sensor->minfo.revision_number);
3414 if (rval >= sizeof(filename)) {
3416 goto out_release_sdata;
3419 rval = request_firmware(&fw, filename, &client->dev);
3421 ccs_data_parse(&sensor->mdata, fw->data, fw->size, &client->dev,
3423 release_firmware(fw);
3426 rval = ccs_read_all_limits(sensor);
3428 goto out_release_mdata;
3430 rval = ccs_read_frame_fmt(sensor);
3433 goto out_free_ccs_limits;
3436 rval = ccs_update_phy_ctrl(sensor);
3438 goto out_free_ccs_limits;
3441 * Handle Sensor Module orientation on the board.
3443 * The application of H-FLIP and V-FLIP on the sensor is modified by
3444 * the sensor orientation on the board.
3446 * For CCS_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
3447 * both H-FLIP and V-FLIP for normal operation which also implies
3448 * that a set/unset operation for user space HFLIP and VFLIP v4l2
3449 * controls will need to be internally inverted.
3451 * Rotation also changes the bayer pattern.
3453 if (sensor->hwcfg.module_board_orient ==
3454 CCS_MODULE_BOARD_ORIENT_180)
3455 sensor->hvflip_inv_mask =
3456 CCS_IMAGE_ORIENTATION_HORIZONTAL_MIRROR |
3457 CCS_IMAGE_ORIENTATION_VERTICAL_FLIP;
3459 rval = ccs_call_quirk(sensor, limits);
3461 dev_err(&client->dev, "limits quirks failed\n");
3462 goto out_free_ccs_limits;
3465 if (CCS_LIM(sensor, BINNING_CAPABILITY)) {
3466 sensor->nbinning_subtypes =
3467 min_t(u8, CCS_LIM(sensor, BINNING_SUB_TYPES),
3468 CCS_LIM_BINNING_SUB_TYPE_MAX_N);
3470 for (i = 0; i < sensor->nbinning_subtypes; i++) {
3471 sensor->binning_subtypes[i].horizontal =
3472 CCS_LIM_AT(sensor, BINNING_SUB_TYPE, i) >>
3473 CCS_BINNING_SUB_TYPE_COLUMN_SHIFT;
3474 sensor->binning_subtypes[i].vertical =
3475 CCS_LIM_AT(sensor, BINNING_SUB_TYPE, i) &
3476 CCS_BINNING_SUB_TYPE_ROW_MASK;
3478 dev_dbg(&client->dev, "binning %xx%x\n",
3479 sensor->binning_subtypes[i].horizontal,
3480 sensor->binning_subtypes[i].vertical);
3483 sensor->binning_horizontal = 1;
3484 sensor->binning_vertical = 1;
3486 if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
3487 dev_err(&client->dev, "sysfs ident entry creation failed\n");
3489 goto out_free_ccs_limits;
3492 if (sensor->minfo.smiapp_version &&
3493 CCS_LIM(sensor, DATA_TRANSFER_IF_CAPABILITY) &
3494 CCS_DATA_TRANSFER_IF_CAPABILITY_SUPPORTED) {
3495 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
3496 dev_err(&client->dev, "sysfs nvm entry failed\n");
3502 if (!CCS_LIM(sensor, MIN_OP_SYS_CLK_DIV) ||
3503 !CCS_LIM(sensor, MAX_OP_SYS_CLK_DIV) ||
3504 !CCS_LIM(sensor, MIN_OP_PIX_CLK_DIV) ||
3505 !CCS_LIM(sensor, MAX_OP_PIX_CLK_DIV)) {
3506 /* No OP clock branch */
3507 sensor->pll.flags |= CCS_PLL_FLAG_NO_OP_CLOCKS;
3508 } else if (CCS_LIM(sensor, SCALING_CAPABILITY)
3509 != CCS_SCALING_CAPABILITY_NONE ||
3510 CCS_LIM(sensor, DIGITAL_CROP_CAPABILITY)
3511 == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
3512 /* We have a scaler or digital crop. */
3513 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3514 sensor->ssds_used++;
3516 sensor->binner = &sensor->ssds[sensor->ssds_used];
3517 sensor->ssds_used++;
3518 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
3519 sensor->ssds_used++;
3521 sensor->scale_m = CCS_LIM(sensor, SCALER_N_MIN);
3523 /* prepare PLL configuration input values */
3524 sensor->pll.bus_type = CCS_PLL_BUS_TYPE_CSI2_DPHY;
3525 sensor->pll.csi2.lanes = sensor->hwcfg.lanes;
3526 if (CCS_LIM(sensor, CLOCK_CALCULATION) &
3527 CCS_CLOCK_CALCULATION_LANE_SPEED) {
3528 sensor->pll.flags |= CCS_PLL_FLAG_LANE_SPEED_MODEL;
3529 if (CCS_LIM(sensor, CLOCK_CALCULATION) &
3530 CCS_CLOCK_CALCULATION_LINK_DECOUPLED) {
3531 sensor->pll.vt_lanes =
3532 CCS_LIM(sensor, NUM_OF_VT_LANES) + 1;
3533 sensor->pll.op_lanes =
3534 CCS_LIM(sensor, NUM_OF_OP_LANES) + 1;
3535 sensor->pll.flags |= CCS_PLL_FLAG_LINK_DECOUPLED;
3537 sensor->pll.vt_lanes = sensor->pll.csi2.lanes;
3538 sensor->pll.op_lanes = sensor->pll.csi2.lanes;
3541 if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) &
3542 CCS_CLOCK_TREE_PLL_CAPABILITY_EXT_DIVIDER)
3543 sensor->pll.flags |= CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER;
3544 if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) &
3545 CCS_CLOCK_TREE_PLL_CAPABILITY_FLEXIBLE_OP_PIX_CLK_DIV)
3546 sensor->pll.flags |= CCS_PLL_FLAG_FLEXIBLE_OP_PIX_CLK_DIV;
3547 if (CCS_LIM(sensor, FIFO_SUPPORT_CAPABILITY) &
3548 CCS_FIFO_SUPPORT_CAPABILITY_DERATING)
3549 sensor->pll.flags |= CCS_PLL_FLAG_FIFO_DERATING;
3550 if (CCS_LIM(sensor, FIFO_SUPPORT_CAPABILITY) &
3551 CCS_FIFO_SUPPORT_CAPABILITY_DERATING_OVERRATING)
3552 sensor->pll.flags |= CCS_PLL_FLAG_FIFO_DERATING |
3553 CCS_PLL_FLAG_FIFO_OVERRATING;
3554 if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) &
3555 CCS_CLOCK_TREE_PLL_CAPABILITY_DUAL_PLL) {
3556 if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) &
3557 CCS_CLOCK_TREE_PLL_CAPABILITY_SINGLE_PLL) {
3560 /* Use sensor default in PLL mode selection */
3561 rval = ccs_read(sensor, PLL_MODE, &v);
3565 if (v == CCS_PLL_MODE_DUAL)
3566 sensor->pll.flags |= CCS_PLL_FLAG_DUAL_PLL;
3568 sensor->pll.flags |= CCS_PLL_FLAG_DUAL_PLL;
3570 if (CCS_LIM(sensor, CLOCK_CALCULATION) &
3571 CCS_CLOCK_CALCULATION_DUAL_PLL_OP_SYS_DDR)
3572 sensor->pll.flags |= CCS_PLL_FLAG_OP_SYS_DDR;
3573 if (CCS_LIM(sensor, CLOCK_CALCULATION) &
3574 CCS_CLOCK_CALCULATION_DUAL_PLL_OP_PIX_DDR)
3575 sensor->pll.flags |= CCS_PLL_FLAG_OP_PIX_DDR;
3577 sensor->pll.op_bits_per_lane = CCS_LIM(sensor, OP_BITS_PER_LANE);
3578 sensor->pll.ext_clk_freq_hz = sensor->hwcfg.ext_clk;
3579 sensor->pll.scale_n = CCS_LIM(sensor, SCALER_N_MIN);
3581 ccs_create_subdev(sensor, sensor->scaler, " scaler", 2,
3582 MEDIA_ENT_F_PROC_VIDEO_SCALER);
3583 ccs_create_subdev(sensor, sensor->binner, " binner", 2,
3584 MEDIA_ENT_F_PROC_VIDEO_SCALER);
3585 ccs_create_subdev(sensor, sensor->pixel_array, " pixel_array", 1,
3586 MEDIA_ENT_F_CAM_SENSOR);
3588 rval = ccs_init_controls(sensor);
3592 rval = ccs_call_quirk(sensor, init);
3596 rval = ccs_get_mbus_formats(sensor);
3602 rval = ccs_init_late_controls(sensor);
3608 mutex_lock(&sensor->mutex);
3609 rval = ccs_pll_blanking_update(sensor);
3610 mutex_unlock(&sensor->mutex);
3612 dev_err(&client->dev, "update mode failed\n");
3616 sensor->streaming = false;
3617 sensor->dev_init_done = true;
3619 rval = media_entity_pads_init(&sensor->src->sd.entity, 2,
3622 goto out_media_entity_cleanup;
3624 rval = ccs_write_msr_regs(sensor);
3626 goto out_media_entity_cleanup;
3628 pm_runtime_set_active(&client->dev);
3629 pm_runtime_get_noresume(&client->dev);
3630 pm_runtime_enable(&client->dev);
3632 rval = v4l2_async_register_subdev_sensor(&sensor->src->sd);
3634 goto out_disable_runtime_pm;
3636 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
3637 pm_runtime_use_autosuspend(&client->dev);
3638 pm_runtime_put_autosuspend(&client->dev);
3642 out_disable_runtime_pm:
3643 pm_runtime_put_noidle(&client->dev);
3644 pm_runtime_disable(&client->dev);
3646 out_media_entity_cleanup:
3647 media_entity_cleanup(&sensor->src->sd.entity);
3650 ccs_cleanup(sensor);
3653 kvfree(sensor->mdata.backing);
3656 kvfree(sensor->sdata.backing);
3658 out_free_ccs_limits:
3659 kfree(sensor->ccs_limits);
3662 ccs_power_off(&client->dev);
3663 mutex_destroy(&sensor->mutex);
3668 static int ccs_remove(struct i2c_client *client)
3670 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3671 struct ccs_sensor *sensor = to_ccs_sensor(subdev);
3674 v4l2_async_unregister_subdev(subdev);
3676 pm_runtime_disable(&client->dev);
3677 if (!pm_runtime_status_suspended(&client->dev))
3678 ccs_power_off(&client->dev);
3679 pm_runtime_set_suspended(&client->dev);
3681 for (i = 0; i < sensor->ssds_used; i++) {
3682 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
3683 media_entity_cleanup(&sensor->ssds[i].sd.entity);
3685 ccs_cleanup(sensor);
3686 mutex_destroy(&sensor->mutex);
3687 kfree(sensor->ccs_limits);
3688 kvfree(sensor->sdata.backing);
3689 kvfree(sensor->mdata.backing);
3694 static const struct ccs_device smia_device = {
3695 .flags = CCS_DEVICE_FLAG_IS_SMIA,
3698 static const struct ccs_device ccs_device = {};
3700 static const struct acpi_device_id ccs_acpi_table[] = {
3701 { .id = "MIPI0200", .driver_data = (unsigned long)&ccs_device },
3704 MODULE_DEVICE_TABLE(acpi, ccs_acpi_table);
3706 static const struct of_device_id ccs_of_table[] = {
3707 { .compatible = "mipi-ccs-1.1", .data = &ccs_device },
3708 { .compatible = "mipi-ccs-1.0", .data = &ccs_device },
3709 { .compatible = "mipi-ccs", .data = &ccs_device },
3710 { .compatible = "nokia,smia", .data = &smia_device },
3713 MODULE_DEVICE_TABLE(of, ccs_of_table);
3715 static const struct dev_pm_ops ccs_pm_ops = {
3716 SET_SYSTEM_SLEEP_PM_OPS(ccs_suspend, ccs_resume)
3717 SET_RUNTIME_PM_OPS(ccs_power_off, ccs_power_on, NULL)
3720 static struct i2c_driver ccs_i2c_driver = {
3722 .acpi_match_table = ccs_acpi_table,
3723 .of_match_table = ccs_of_table,
3727 .probe_new = ccs_probe,
3728 .remove = ccs_remove,
3731 static int ccs_module_init(void)
3735 for (i = 0, l = 0; ccs_limits[i].size && l < CCS_L_LAST; i++) {
3736 if (!(ccs_limits[i].flags & CCS_L_FL_SAME_REG)) {
3737 ccs_limit_offsets[l + 1].lim =
3738 ALIGN(ccs_limit_offsets[l].lim +
3740 ccs_reg_width(ccs_limits[i + 1].reg));
3741 ccs_limit_offsets[l].info = i;
3744 ccs_limit_offsets[l].lim += ccs_limits[i].size;
3748 if (WARN_ON(ccs_limits[i].size))
3751 if (WARN_ON(l != CCS_L_LAST))
3754 return i2c_register_driver(THIS_MODULE, &ccs_i2c_driver);
3757 static void ccs_module_cleanup(void)
3759 i2c_del_driver(&ccs_i2c_driver);
3762 module_init(ccs_module_init);
3763 module_exit(ccs_module_cleanup);
3765 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
3766 MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ camera sensor driver");
3767 MODULE_LICENSE("GPL v2");
3768 MODULE_ALIAS("smiapp");