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/consumer.h>
21 #include <linux/module.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/property.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26 #include <linux/smiapp.h>
27 #include <linux/v4l2-mediabus.h>
28 #include <media/v4l2-cci.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-fwnode.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
+ CCI_REG_WIDTH_BYTES(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 = %u, 0x%x\n",
126 linfo
->reg
, linfo
->name
, offset
, val
, val
);
128 ccs_assign_limit(ptr
, CCI_REG_WIDTH_BYTES(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 (CCI_REG_WIDTH_BYTES(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 sensor
->ccs_limits
= alloc
;
178 for (i
= 0, l
= 0, ptr
= alloc
; ccs_limits
[i
].size
; i
++) {
179 u32 reg
= ccs_limits
[i
].reg
;
180 unsigned int width
= CCI_REG_WIDTH_BYTES(reg
);
183 if (l
== CCS_L_LAST
) {
184 dev_err(&client
->dev
,
185 "internal error --- end of limit array\n");
190 for (j
= 0; j
< ccs_limits
[i
].size
/ width
;
191 j
++, reg
+= width
, ptr
+= width
) {
195 ret
= ccs_read_addr_noconv(sensor
, reg
, &val
);
199 if (ptr
+ width
> end
) {
200 dev_err(&client
->dev
,
201 "internal error --- no room for regs\n");
209 ccs_assign_limit(ptr
, width
, val
);
211 #ifdef CONFIG_DYNAMIC_DEBUG
212 if (reg
& (CCS_FL_FLOAT_IREAL
| CCS_FL_IREAL
))
213 snprintf(str
, sizeof(str
), ", %u",
214 ccs_reg_conv(sensor
, reg
, val
));
217 dev_dbg(&client
->dev
,
218 "0x%8.8x \"%s\" = %u, 0x%x%s\n",
219 reg
, ccs_limits
[i
].name
, val
, val
, str
);
222 if (ccs_limits
[i
].flags
& CCS_L_FL_SAME_REG
)
226 ptr
= alloc
+ ccs_limit_offsets
[l
].lim
;
229 if (l
!= CCS_L_LAST
) {
230 dev_err(&client
->dev
,
231 "internal error --- insufficient limits\n");
236 if (CCS_LIM(sensor
, SCALER_N_MIN
) < 16)
237 ccs_replace_limit(sensor
, CCS_L_SCALER_N_MIN
, 0, 16);
242 sensor
->ccs_limits
= NULL
;
248 static int ccs_read_frame_fmt(struct ccs_sensor
*sensor
)
250 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
251 u8 fmt_model_type
, fmt_model_subtype
, ncol_desc
, nrow_desc
;
256 fmt_model_type
= CCS_LIM(sensor
, FRAME_FORMAT_MODEL_TYPE
);
257 fmt_model_subtype
= CCS_LIM(sensor
, FRAME_FORMAT_MODEL_SUBTYPE
);
259 ncol_desc
= (fmt_model_subtype
260 & CCS_FRAME_FORMAT_MODEL_SUBTYPE_COLUMNS_MASK
)
261 >> CCS_FRAME_FORMAT_MODEL_SUBTYPE_COLUMNS_SHIFT
;
262 nrow_desc
= fmt_model_subtype
263 & CCS_FRAME_FORMAT_MODEL_SUBTYPE_ROWS_MASK
;
265 dev_dbg(&client
->dev
, "format_model_type %s\n",
266 fmt_model_type
== CCS_FRAME_FORMAT_MODEL_TYPE_2_BYTE
268 fmt_model_type
== CCS_FRAME_FORMAT_MODEL_TYPE_4_BYTE
269 ? "4 byte" : "is simply bad");
271 dev_dbg(&client
->dev
, "%u column and %u row descriptors\n",
272 ncol_desc
, nrow_desc
);
274 for (i
= 0; i
< ncol_desc
+ nrow_desc
; i
++) {
281 if (fmt_model_type
== CCS_FRAME_FORMAT_MODEL_TYPE_2_BYTE
) {
282 desc
= CCS_LIM_AT(sensor
, FRAME_FORMAT_DESCRIPTOR
, i
);
286 & CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_MASK
)
287 >> CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_SHIFT
;
288 pixels
= desc
& CCS_FRAME_FORMAT_DESCRIPTOR_PIXELS_MASK
;
289 } else if (fmt_model_type
290 == CCS_FRAME_FORMAT_MODEL_TYPE_4_BYTE
) {
291 desc
= CCS_LIM_AT(sensor
, FRAME_FORMAT_DESCRIPTOR_4
, i
);
295 & CCS_FRAME_FORMAT_DESCRIPTOR_4_PCODE_MASK
)
296 >> CCS_FRAME_FORMAT_DESCRIPTOR_4_PCODE_SHIFT
;
298 CCS_FRAME_FORMAT_DESCRIPTOR_4_PIXELS_MASK
;
300 dev_dbg(&client
->dev
,
301 "invalid frame format model type %u\n",
312 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_EMBEDDED
:
315 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_DUMMY_PIXEL
:
318 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_BLACK_PIXEL
:
321 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_DARK_PIXEL
:
324 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL
:
332 dev_dbg(&client
->dev
,
333 "%s pixels: %u %s (pixelcode %u)\n",
334 what
, pixels
, which
, pixelcode
);
338 CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL
)
339 sensor
->visible_pixel_start
= pixel_count
;
340 pixel_count
+= pixels
;
344 /* Handle row descriptors */
346 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_EMBEDDED
:
347 if (sensor
->embedded_end
)
349 sensor
->embedded_start
= line_count
;
350 sensor
->embedded_end
= line_count
+ pixels
;
352 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL
:
353 sensor
->image_start
= line_count
;
356 line_count
+= pixels
;
359 if (sensor
->embedded_end
> sensor
->image_start
) {
360 dev_dbg(&client
->dev
,
361 "adjusting image start line to %u (was %u)\n",
362 sensor
->embedded_end
, sensor
->image_start
);
363 sensor
->image_start
= sensor
->embedded_end
;
366 dev_dbg(&client
->dev
, "embedded data from lines %u to %u\n",
367 sensor
->embedded_start
, sensor
->embedded_end
);
368 dev_dbg(&client
->dev
, "image data starts at line %u\n",
369 sensor
->image_start
);
374 static int ccs_pll_configure(struct ccs_sensor
*sensor
)
376 struct ccs_pll
*pll
= &sensor
->pll
;
379 rval
= ccs_write(sensor
, VT_PIX_CLK_DIV
, pll
->vt_bk
.pix_clk_div
);
383 rval
= ccs_write(sensor
, VT_SYS_CLK_DIV
, pll
->vt_bk
.sys_clk_div
);
387 rval
= ccs_write(sensor
, PRE_PLL_CLK_DIV
, pll
->vt_fr
.pre_pll_clk_div
);
391 rval
= ccs_write(sensor
, PLL_MULTIPLIER
, pll
->vt_fr
.pll_multiplier
);
395 if (!(CCS_LIM(sensor
, PHY_CTRL_CAPABILITY
) &
396 CCS_PHY_CTRL_CAPABILITY_AUTO_PHY_CTL
)) {
397 /* Lane op clock ratio does not apply here. */
398 rval
= ccs_write(sensor
, REQUESTED_LINK_RATE
,
399 DIV_ROUND_UP(pll
->op_bk
.sys_clk_freq_hz
,
400 1000000 / 256 / 256) *
401 (pll
->flags
& CCS_PLL_FLAG_LANE_SPEED_MODEL
?
402 sensor
->pll
.csi2
.lanes
: 1) <<
403 (pll
->flags
& CCS_PLL_FLAG_OP_SYS_DDR
?
409 if (sensor
->pll
.flags
& CCS_PLL_FLAG_NO_OP_CLOCKS
)
412 rval
= ccs_write(sensor
, OP_PIX_CLK_DIV
, pll
->op_bk
.pix_clk_div
);
416 rval
= ccs_write(sensor
, OP_SYS_CLK_DIV
, pll
->op_bk
.sys_clk_div
);
420 if (!(pll
->flags
& CCS_PLL_FLAG_DUAL_PLL
))
423 rval
= ccs_write(sensor
, PLL_MODE
, CCS_PLL_MODE_DUAL
);
427 rval
= ccs_write(sensor
, OP_PRE_PLL_CLK_DIV
,
428 pll
->op_fr
.pre_pll_clk_div
);
432 return ccs_write(sensor
, OP_PLL_MULTIPLIER
, pll
->op_fr
.pll_multiplier
);
435 static int ccs_pll_try(struct ccs_sensor
*sensor
, struct ccs_pll
*pll
)
437 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
438 struct ccs_pll_limits lim
= {
440 .min_pre_pll_clk_div
= CCS_LIM(sensor
, MIN_PRE_PLL_CLK_DIV
),
441 .max_pre_pll_clk_div
= CCS_LIM(sensor
, MAX_PRE_PLL_CLK_DIV
),
442 .min_pll_ip_clk_freq_hz
= CCS_LIM(sensor
, MIN_PLL_IP_CLK_FREQ_MHZ
),
443 .max_pll_ip_clk_freq_hz
= CCS_LIM(sensor
, MAX_PLL_IP_CLK_FREQ_MHZ
),
444 .min_pll_multiplier
= CCS_LIM(sensor
, MIN_PLL_MULTIPLIER
),
445 .max_pll_multiplier
= CCS_LIM(sensor
, MAX_PLL_MULTIPLIER
),
446 .min_pll_op_clk_freq_hz
= CCS_LIM(sensor
, MIN_PLL_OP_CLK_FREQ_MHZ
),
447 .max_pll_op_clk_freq_hz
= CCS_LIM(sensor
, MAX_PLL_OP_CLK_FREQ_MHZ
),
450 .min_pre_pll_clk_div
= CCS_LIM(sensor
, MIN_OP_PRE_PLL_CLK_DIV
),
451 .max_pre_pll_clk_div
= CCS_LIM(sensor
, MAX_OP_PRE_PLL_CLK_DIV
),
452 .min_pll_ip_clk_freq_hz
= CCS_LIM(sensor
, MIN_OP_PLL_IP_CLK_FREQ_MHZ
),
453 .max_pll_ip_clk_freq_hz
= CCS_LIM(sensor
, MAX_OP_PLL_IP_CLK_FREQ_MHZ
),
454 .min_pll_multiplier
= CCS_LIM(sensor
, MIN_OP_PLL_MULTIPLIER
),
455 .max_pll_multiplier
= CCS_LIM(sensor
, MAX_OP_PLL_MULTIPLIER
),
456 .min_pll_op_clk_freq_hz
= CCS_LIM(sensor
, MIN_OP_PLL_OP_CLK_FREQ_MHZ
),
457 .max_pll_op_clk_freq_hz
= CCS_LIM(sensor
, MAX_OP_PLL_OP_CLK_FREQ_MHZ
),
460 .min_sys_clk_div
= CCS_LIM(sensor
, MIN_OP_SYS_CLK_DIV
),
461 .max_sys_clk_div
= CCS_LIM(sensor
, MAX_OP_SYS_CLK_DIV
),
462 .min_pix_clk_div
= CCS_LIM(sensor
, MIN_OP_PIX_CLK_DIV
),
463 .max_pix_clk_div
= CCS_LIM(sensor
, MAX_OP_PIX_CLK_DIV
),
464 .min_sys_clk_freq_hz
= CCS_LIM(sensor
, MIN_OP_SYS_CLK_FREQ_MHZ
),
465 .max_sys_clk_freq_hz
= CCS_LIM(sensor
, MAX_OP_SYS_CLK_FREQ_MHZ
),
466 .min_pix_clk_freq_hz
= CCS_LIM(sensor
, MIN_OP_PIX_CLK_FREQ_MHZ
),
467 .max_pix_clk_freq_hz
= CCS_LIM(sensor
, MAX_OP_PIX_CLK_FREQ_MHZ
),
470 .min_sys_clk_div
= CCS_LIM(sensor
, MIN_VT_SYS_CLK_DIV
),
471 .max_sys_clk_div
= CCS_LIM(sensor
, MAX_VT_SYS_CLK_DIV
),
472 .min_pix_clk_div
= CCS_LIM(sensor
, MIN_VT_PIX_CLK_DIV
),
473 .max_pix_clk_div
= CCS_LIM(sensor
, MAX_VT_PIX_CLK_DIV
),
474 .min_sys_clk_freq_hz
= CCS_LIM(sensor
, MIN_VT_SYS_CLK_FREQ_MHZ
),
475 .max_sys_clk_freq_hz
= CCS_LIM(sensor
, MAX_VT_SYS_CLK_FREQ_MHZ
),
476 .min_pix_clk_freq_hz
= CCS_LIM(sensor
, MIN_VT_PIX_CLK_FREQ_MHZ
),
477 .max_pix_clk_freq_hz
= CCS_LIM(sensor
, MAX_VT_PIX_CLK_FREQ_MHZ
),
479 .min_line_length_pck_bin
= CCS_LIM(sensor
, MIN_LINE_LENGTH_PCK_BIN
),
480 .min_line_length_pck
= CCS_LIM(sensor
, MIN_LINE_LENGTH_PCK
),
483 return ccs_pll_calculate(&client
->dev
, &lim
, pll
);
486 static int ccs_pll_update(struct ccs_sensor
*sensor
)
488 struct ccs_pll
*pll
= &sensor
->pll
;
491 pll
->binning_horizontal
= sensor
->binning_horizontal
;
492 pll
->binning_vertical
= sensor
->binning_vertical
;
494 sensor
->link_freq
->qmenu_int
[sensor
->link_freq
->val
];
495 pll
->scale_m
= sensor
->scale_m
;
496 pll
->bits_per_pixel
= sensor
->csi_format
->compressed
;
498 rval
= ccs_pll_try(sensor
, pll
);
502 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_parray
,
503 pll
->pixel_rate_pixel_array
);
504 __v4l2_ctrl_s_ctrl_int64(sensor
->pixel_rate_csi
, pll
->pixel_rate_csi
);
512 * V4L2 Controls handling
516 static void __ccs_update_exposure_limits(struct ccs_sensor
*sensor
)
518 struct v4l2_ctrl
*ctrl
= sensor
->exposure
;
521 max
= sensor
->pa_src
.height
+ sensor
->vblank
->val
-
522 CCS_LIM(sensor
, COARSE_INTEGRATION_TIME_MAX_MARGIN
);
524 __v4l2_ctrl_modify_range(ctrl
, ctrl
->minimum
, max
, ctrl
->step
, max
);
530 * 1. Bits-per-pixel, descending.
531 * 2. Bits-per-pixel compressed, descending.
532 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
533 * orders must be defined.
535 static const struct ccs_csi_data_format ccs_csi_data_formats
[] = {
536 { MEDIA_BUS_FMT_SGRBG16_1X16
, 16, 16, CCS_PIXEL_ORDER_GRBG
, },
537 { MEDIA_BUS_FMT_SRGGB16_1X16
, 16, 16, CCS_PIXEL_ORDER_RGGB
, },
538 { MEDIA_BUS_FMT_SBGGR16_1X16
, 16, 16, CCS_PIXEL_ORDER_BGGR
, },
539 { MEDIA_BUS_FMT_SGBRG16_1X16
, 16, 16, CCS_PIXEL_ORDER_GBRG
, },
540 { MEDIA_BUS_FMT_SGRBG14_1X14
, 14, 14, CCS_PIXEL_ORDER_GRBG
, },
541 { MEDIA_BUS_FMT_SRGGB14_1X14
, 14, 14, CCS_PIXEL_ORDER_RGGB
, },
542 { MEDIA_BUS_FMT_SBGGR14_1X14
, 14, 14, CCS_PIXEL_ORDER_BGGR
, },
543 { MEDIA_BUS_FMT_SGBRG14_1X14
, 14, 14, CCS_PIXEL_ORDER_GBRG
, },
544 { MEDIA_BUS_FMT_SGRBG12_1X12
, 12, 12, CCS_PIXEL_ORDER_GRBG
, },
545 { MEDIA_BUS_FMT_SRGGB12_1X12
, 12, 12, CCS_PIXEL_ORDER_RGGB
, },
546 { MEDIA_BUS_FMT_SBGGR12_1X12
, 12, 12, CCS_PIXEL_ORDER_BGGR
, },
547 { MEDIA_BUS_FMT_SGBRG12_1X12
, 12, 12, CCS_PIXEL_ORDER_GBRG
, },
548 { MEDIA_BUS_FMT_SGRBG10_1X10
, 10, 10, CCS_PIXEL_ORDER_GRBG
, },
549 { MEDIA_BUS_FMT_SRGGB10_1X10
, 10, 10, CCS_PIXEL_ORDER_RGGB
, },
550 { MEDIA_BUS_FMT_SBGGR10_1X10
, 10, 10, CCS_PIXEL_ORDER_BGGR
, },
551 { MEDIA_BUS_FMT_SGBRG10_1X10
, 10, 10, CCS_PIXEL_ORDER_GBRG
, },
552 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8
, 10, 8, CCS_PIXEL_ORDER_GRBG
, },
553 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8
, 10, 8, CCS_PIXEL_ORDER_RGGB
, },
554 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8
, 10, 8, CCS_PIXEL_ORDER_BGGR
, },
555 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8
, 10, 8, CCS_PIXEL_ORDER_GBRG
, },
556 { MEDIA_BUS_FMT_SGRBG8_1X8
, 8, 8, CCS_PIXEL_ORDER_GRBG
, },
557 { MEDIA_BUS_FMT_SRGGB8_1X8
, 8, 8, CCS_PIXEL_ORDER_RGGB
, },
558 { MEDIA_BUS_FMT_SBGGR8_1X8
, 8, 8, CCS_PIXEL_ORDER_BGGR
, },
559 { MEDIA_BUS_FMT_SGBRG8_1X8
, 8, 8, CCS_PIXEL_ORDER_GBRG
, },
562 static const char *pixel_order_str
[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
564 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
565 - (unsigned long)ccs_csi_data_formats) \
566 / sizeof(*ccs_csi_data_formats))
568 static u32
ccs_pixel_order(struct ccs_sensor
*sensor
)
570 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
574 if (sensor
->hflip
->val
)
575 flip
|= CCS_IMAGE_ORIENTATION_HORIZONTAL_MIRROR
;
577 if (sensor
->vflip
->val
)
578 flip
|= CCS_IMAGE_ORIENTATION_VERTICAL_FLIP
;
581 dev_dbg(&client
->dev
, "flip %u\n", flip
);
582 return sensor
->default_pixel_order
^ flip
;
585 static void ccs_update_mbus_formats(struct ccs_sensor
*sensor
)
587 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
588 unsigned int csi_format_idx
=
589 to_csi_format_idx(sensor
->csi_format
) & ~3;
590 unsigned int internal_csi_format_idx
=
591 to_csi_format_idx(sensor
->internal_csi_format
) & ~3;
592 unsigned int pixel_order
= ccs_pixel_order(sensor
);
594 if (WARN_ON_ONCE(max(internal_csi_format_idx
, csi_format_idx
) +
595 pixel_order
>= ARRAY_SIZE(ccs_csi_data_formats
)))
598 sensor
->mbus_frame_fmts
=
599 sensor
->default_mbus_frame_fmts
<< pixel_order
;
601 &ccs_csi_data_formats
[csi_format_idx
+ pixel_order
];
602 sensor
->internal_csi_format
=
603 &ccs_csi_data_formats
[internal_csi_format_idx
606 dev_dbg(&client
->dev
, "new pixel order %s\n",
607 pixel_order_str
[pixel_order
]);
610 static const char * const ccs_test_patterns
[] = {
613 "Eight Vertical Colour Bars",
614 "Colour Bars With Fade to Grey",
615 "Pseudorandom Sequence (PN9)",
618 static int ccs_set_ctrl(struct v4l2_ctrl
*ctrl
)
620 struct ccs_sensor
*sensor
=
621 container_of(ctrl
->handler
, struct ccs_subdev
, ctrl_handler
)
623 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
633 if (sensor
->streaming
)
636 if (sensor
->hflip
->val
)
637 orient
|= CCS_IMAGE_ORIENTATION_HORIZONTAL_MIRROR
;
639 if (sensor
->vflip
->val
)
640 orient
|= CCS_IMAGE_ORIENTATION_VERTICAL_FLIP
;
642 ccs_update_mbus_formats(sensor
);
645 case V4L2_CID_VBLANK
:
646 exposure
= sensor
->exposure
->val
;
648 __ccs_update_exposure_limits(sensor
);
650 if (exposure
> sensor
->exposure
->maximum
) {
651 sensor
->exposure
->val
= sensor
->exposure
->maximum
;
652 rval
= ccs_set_ctrl(sensor
->exposure
);
658 case V4L2_CID_LINK_FREQ
:
659 if (sensor
->streaming
)
662 rval
= ccs_pll_update(sensor
);
667 case V4L2_CID_TEST_PATTERN
:
668 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
670 sensor
->test_data
[i
],
672 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR
);
677 pm_status
= pm_runtime_get_if_active(&client
->dev
);
682 case V4L2_CID_ANALOGUE_GAIN
:
683 rval
= ccs_write(sensor
, ANALOG_GAIN_CODE_GLOBAL
, ctrl
->val
);
687 case V4L2_CID_CCS_ANALOGUE_LINEAR_GAIN
:
688 rval
= ccs_write(sensor
, ANALOG_LINEAR_GAIN_GLOBAL
, ctrl
->val
);
692 case V4L2_CID_CCS_ANALOGUE_EXPONENTIAL_GAIN
:
693 rval
= ccs_write(sensor
, ANALOG_EXPONENTIAL_GAIN_GLOBAL
,
698 case V4L2_CID_DIGITAL_GAIN
:
699 if (CCS_LIM(sensor
, DIGITAL_GAIN_CAPABILITY
) ==
700 CCS_DIGITAL_GAIN_CAPABILITY_GLOBAL
) {
701 rval
= ccs_write(sensor
, DIGITAL_GAIN_GLOBAL
,
706 rval
= ccs_write_addr(sensor
,
707 SMIAPP_REG_U16_DIGITAL_GAIN_GREENR
,
712 rval
= ccs_write_addr(sensor
,
713 SMIAPP_REG_U16_DIGITAL_GAIN_RED
,
718 rval
= ccs_write_addr(sensor
,
719 SMIAPP_REG_U16_DIGITAL_GAIN_BLUE
,
724 rval
= ccs_write_addr(sensor
,
725 SMIAPP_REG_U16_DIGITAL_GAIN_GREENB
,
729 case V4L2_CID_EXPOSURE
:
730 rval
= ccs_write(sensor
, COARSE_INTEGRATION_TIME
, ctrl
->val
);
735 rval
= ccs_write(sensor
, IMAGE_ORIENTATION
, orient
);
738 case V4L2_CID_VBLANK
:
739 rval
= ccs_write(sensor
, FRAME_LENGTH_LINES
,
740 sensor
->pa_src
.height
+ ctrl
->val
);
743 case V4L2_CID_HBLANK
:
744 rval
= ccs_write(sensor
, LINE_LENGTH_PCK
,
745 sensor
->pa_src
.width
+ ctrl
->val
);
748 case V4L2_CID_TEST_PATTERN
:
749 rval
= ccs_write(sensor
, TEST_PATTERN_MODE
, ctrl
->val
);
752 case V4L2_CID_TEST_PATTERN_RED
:
753 rval
= ccs_write(sensor
, TEST_DATA_RED
, ctrl
->val
);
756 case V4L2_CID_TEST_PATTERN_GREENR
:
757 rval
= ccs_write(sensor
, TEST_DATA_GREENR
, ctrl
->val
);
760 case V4L2_CID_TEST_PATTERN_BLUE
:
761 rval
= ccs_write(sensor
, TEST_DATA_BLUE
, ctrl
->val
);
764 case V4L2_CID_TEST_PATTERN_GREENB
:
765 rval
= ccs_write(sensor
, TEST_DATA_GREENB
, ctrl
->val
);
768 case V4L2_CID_CCS_SHADING_CORRECTION
:
769 rval
= ccs_write(sensor
, SHADING_CORRECTION_EN
,
770 ctrl
->val
? CCS_SHADING_CORRECTION_EN_ENABLE
:
773 if (!rval
&& sensor
->luminance_level
)
774 v4l2_ctrl_activate(sensor
->luminance_level
, ctrl
->val
);
777 case V4L2_CID_CCS_LUMINANCE_CORRECTION_LEVEL
:
778 rval
= ccs_write(sensor
, LUMINANCE_CORRECTION_LEVEL
, ctrl
->val
);
781 case V4L2_CID_PIXEL_RATE
:
782 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
791 pm_runtime_mark_last_busy(&client
->dev
);
792 pm_runtime_put_autosuspend(&client
->dev
);
798 static const struct v4l2_ctrl_ops ccs_ctrl_ops
= {
799 .s_ctrl
= ccs_set_ctrl
,
802 static int ccs_init_controls(struct ccs_sensor
*sensor
)
804 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
805 struct v4l2_fwnode_device_properties props
;
808 rval
= v4l2_ctrl_handler_init(&sensor
->pixel_array
->ctrl_handler
, 19);
812 sensor
->pixel_array
->ctrl_handler
.lock
= &sensor
->mutex
;
814 rval
= v4l2_fwnode_device_parse(&client
->dev
, &props
);
818 rval
= v4l2_ctrl_new_fwnode_properties(&sensor
->pixel_array
->ctrl_handler
,
819 &ccs_ctrl_ops
, &props
);
823 switch (CCS_LIM(sensor
, ANALOG_GAIN_CAPABILITY
)) {
824 case CCS_ANALOG_GAIN_CAPABILITY_GLOBAL
: {
829 } const gain_ctrls
[] = {
830 { "Analogue Gain m0", V4L2_CID_CCS_ANALOGUE_GAIN_M0
,
831 CCS_LIM(sensor
, ANALOG_GAIN_M0
), },
832 { "Analogue Gain c0", V4L2_CID_CCS_ANALOGUE_GAIN_C0
,
833 CCS_LIM(sensor
, ANALOG_GAIN_C0
), },
834 { "Analogue Gain m1", V4L2_CID_CCS_ANALOGUE_GAIN_M1
,
835 CCS_LIM(sensor
, ANALOG_GAIN_M1
), },
836 { "Analogue Gain c1", V4L2_CID_CCS_ANALOGUE_GAIN_C1
,
837 CCS_LIM(sensor
, ANALOG_GAIN_C1
), },
839 struct v4l2_ctrl_config ctrl_cfg
= {
840 .type
= V4L2_CTRL_TYPE_INTEGER
,
841 .ops
= &ccs_ctrl_ops
,
842 .flags
= V4L2_CTRL_FLAG_READ_ONLY
,
847 for (i
= 0; i
< ARRAY_SIZE(gain_ctrls
); i
++) {
848 ctrl_cfg
.name
= gain_ctrls
[i
].name
;
849 ctrl_cfg
.id
= gain_ctrls
[i
].id
;
850 ctrl_cfg
.min
= ctrl_cfg
.max
= ctrl_cfg
.def
=
853 v4l2_ctrl_new_custom(&sensor
->pixel_array
->ctrl_handler
,
857 v4l2_ctrl_new_std(&sensor
->pixel_array
->ctrl_handler
,
858 &ccs_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
859 CCS_LIM(sensor
, ANALOG_GAIN_CODE_MIN
),
860 CCS_LIM(sensor
, ANALOG_GAIN_CODE_MAX
),
861 max(CCS_LIM(sensor
, ANALOG_GAIN_CODE_STEP
),
863 CCS_LIM(sensor
, ANALOG_GAIN_CODE_MIN
));
867 case CCS_ANALOG_GAIN_CAPABILITY_ALTERNATE_GLOBAL
: {
872 } const gain_ctrls
[] = {
874 "Analogue Linear Gain",
875 V4L2_CID_CCS_ANALOGUE_LINEAR_GAIN
,
876 CCS_LIM(sensor
, ANALOG_LINEAR_GAIN_MIN
),
877 CCS_LIM(sensor
, ANALOG_LINEAR_GAIN_MAX
),
879 ANALOG_LINEAR_GAIN_STEP_SIZE
),
883 "Analogue Exponential Gain",
884 V4L2_CID_CCS_ANALOGUE_EXPONENTIAL_GAIN
,
885 CCS_LIM(sensor
, ANALOG_EXPONENTIAL_GAIN_MIN
),
886 CCS_LIM(sensor
, ANALOG_EXPONENTIAL_GAIN_MAX
),
888 ANALOG_EXPONENTIAL_GAIN_STEP_SIZE
),
892 struct v4l2_ctrl_config ctrl_cfg
= {
893 .type
= V4L2_CTRL_TYPE_INTEGER
,
894 .ops
= &ccs_ctrl_ops
,
898 for (i
= 0; i
< ARRAY_SIZE(gain_ctrls
); i
++) {
899 ctrl_cfg
.name
= gain_ctrls
[i
].name
;
900 ctrl_cfg
.min
= ctrl_cfg
.def
= gain_ctrls
[i
].min
;
901 ctrl_cfg
.max
= gain_ctrls
[i
].max
;
902 ctrl_cfg
.step
= gain_ctrls
[i
].step
;
903 ctrl_cfg
.id
= gain_ctrls
[i
].id
;
905 v4l2_ctrl_new_custom(&sensor
->pixel_array
->ctrl_handler
,
911 if (CCS_LIM(sensor
, SHADING_CORRECTION_CAPABILITY
) &
912 (CCS_SHADING_CORRECTION_CAPABILITY_COLOR_SHADING
|
913 CCS_SHADING_CORRECTION_CAPABILITY_LUMINANCE_CORRECTION
)) {
914 const struct v4l2_ctrl_config ctrl_cfg
= {
915 .name
= "Shading Correction",
916 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
917 .id
= V4L2_CID_CCS_SHADING_CORRECTION
,
918 .ops
= &ccs_ctrl_ops
,
923 v4l2_ctrl_new_custom(&sensor
->pixel_array
->ctrl_handler
,
927 if (CCS_LIM(sensor
, SHADING_CORRECTION_CAPABILITY
) &
928 CCS_SHADING_CORRECTION_CAPABILITY_LUMINANCE_CORRECTION
) {
929 const struct v4l2_ctrl_config ctrl_cfg
= {
930 .name
= "Luminance Correction Level",
931 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
932 .id
= V4L2_CID_CCS_LUMINANCE_CORRECTION_LEVEL
,
933 .ops
= &ccs_ctrl_ops
,
939 sensor
->luminance_level
=
940 v4l2_ctrl_new_custom(&sensor
->pixel_array
->ctrl_handler
,
944 if (CCS_LIM(sensor
, DIGITAL_GAIN_CAPABILITY
) ==
945 CCS_DIGITAL_GAIN_CAPABILITY_GLOBAL
||
946 CCS_LIM(sensor
, DIGITAL_GAIN_CAPABILITY
) ==
947 SMIAPP_DIGITAL_GAIN_CAPABILITY_PER_CHANNEL
)
948 v4l2_ctrl_new_std(&sensor
->pixel_array
->ctrl_handler
,
949 &ccs_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
950 CCS_LIM(sensor
, DIGITAL_GAIN_MIN
),
951 CCS_LIM(sensor
, DIGITAL_GAIN_MAX
),
952 max(CCS_LIM(sensor
, DIGITAL_GAIN_STEP_SIZE
),
956 /* Exposure limits will be updated soon, use just something here. */
957 sensor
->exposure
= v4l2_ctrl_new_std(
958 &sensor
->pixel_array
->ctrl_handler
, &ccs_ctrl_ops
,
959 V4L2_CID_EXPOSURE
, 0, 0, 1, 0);
961 sensor
->hflip
= v4l2_ctrl_new_std(
962 &sensor
->pixel_array
->ctrl_handler
, &ccs_ctrl_ops
,
963 V4L2_CID_HFLIP
, 0, 1, 1, 0);
964 sensor
->vflip
= v4l2_ctrl_new_std(
965 &sensor
->pixel_array
->ctrl_handler
, &ccs_ctrl_ops
,
966 V4L2_CID_VFLIP
, 0, 1, 1, 0);
968 sensor
->vblank
= v4l2_ctrl_new_std(
969 &sensor
->pixel_array
->ctrl_handler
, &ccs_ctrl_ops
,
970 V4L2_CID_VBLANK
, 0, 1, 1, 0);
973 sensor
->vblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
975 sensor
->hblank
= v4l2_ctrl_new_std(
976 &sensor
->pixel_array
->ctrl_handler
, &ccs_ctrl_ops
,
977 V4L2_CID_HBLANK
, 0, 1, 1, 0);
980 sensor
->hblank
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
982 sensor
->pixel_rate_parray
= v4l2_ctrl_new_std(
983 &sensor
->pixel_array
->ctrl_handler
, &ccs_ctrl_ops
,
984 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
986 v4l2_ctrl_new_std_menu_items(&sensor
->pixel_array
->ctrl_handler
,
987 &ccs_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
988 ARRAY_SIZE(ccs_test_patterns
) - 1,
989 0, 0, ccs_test_patterns
);
991 if (sensor
->pixel_array
->ctrl_handler
.error
) {
992 dev_err(&client
->dev
,
993 "pixel array controls initialization failed (%d)\n",
994 sensor
->pixel_array
->ctrl_handler
.error
);
995 return sensor
->pixel_array
->ctrl_handler
.error
;
998 sensor
->pixel_array
->sd
.ctrl_handler
=
999 &sensor
->pixel_array
->ctrl_handler
;
1001 v4l2_ctrl_cluster(2, &sensor
->hflip
);
1003 rval
= v4l2_ctrl_handler_init(&sensor
->src
->ctrl_handler
, 0);
1007 sensor
->src
->ctrl_handler
.lock
= &sensor
->mutex
;
1009 sensor
->pixel_rate_csi
= v4l2_ctrl_new_std(
1010 &sensor
->src
->ctrl_handler
, &ccs_ctrl_ops
,
1011 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
, 1, 1);
1013 if (sensor
->src
->ctrl_handler
.error
) {
1014 dev_err(&client
->dev
,
1015 "src controls initialization failed (%d)\n",
1016 sensor
->src
->ctrl_handler
.error
);
1017 return sensor
->src
->ctrl_handler
.error
;
1020 sensor
->src
->sd
.ctrl_handler
= &sensor
->src
->ctrl_handler
;
1026 * For controls that require information on available media bus codes
1027 * and linke frequencies.
1029 static int ccs_init_late_controls(struct ccs_sensor
*sensor
)
1031 unsigned long *valid_link_freqs
= &sensor
->valid_link_freqs
[
1032 sensor
->csi_format
->compressed
- sensor
->compressed_min_bpp
];
1035 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++) {
1036 int max_value
= (1 << sensor
->csi_format
->width
) - 1;
1038 sensor
->test_data
[i
] = v4l2_ctrl_new_std(
1039 &sensor
->pixel_array
->ctrl_handler
,
1040 &ccs_ctrl_ops
, V4L2_CID_TEST_PATTERN_RED
+ i
,
1041 0, max_value
, 1, max_value
);
1044 sensor
->link_freq
= v4l2_ctrl_new_int_menu(
1045 &sensor
->src
->ctrl_handler
, &ccs_ctrl_ops
,
1046 V4L2_CID_LINK_FREQ
, __fls(*valid_link_freqs
),
1047 __ffs(*valid_link_freqs
), sensor
->hwcfg
.op_sys_clock
);
1049 return sensor
->src
->ctrl_handler
.error
;
1052 static void ccs_free_controls(struct ccs_sensor
*sensor
)
1056 for (i
= 0; i
< sensor
->ssds_used
; i
++)
1057 v4l2_ctrl_handler_free(&sensor
->ssds
[i
].ctrl_handler
);
1060 static int ccs_get_mbus_formats(struct ccs_sensor
*sensor
)
1062 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1063 struct ccs_pll
*pll
= &sensor
->pll
;
1064 u8 compressed_max_bpp
= 0;
1065 unsigned int type
, n
;
1066 unsigned int i
, pixel_order
;
1069 type
= CCS_LIM(sensor
, DATA_FORMAT_MODEL_TYPE
);
1071 dev_dbg(&client
->dev
, "data_format_model_type %u\n", type
);
1073 rval
= ccs_read(sensor
, PIXEL_ORDER
, &pixel_order
);
1077 if (pixel_order
>= ARRAY_SIZE(pixel_order_str
)) {
1078 dev_dbg(&client
->dev
, "bad pixel order %u\n", pixel_order
);
1082 dev_dbg(&client
->dev
, "pixel order %u (%s)\n", pixel_order
,
1083 pixel_order_str
[pixel_order
]);
1086 case CCS_DATA_FORMAT_MODEL_TYPE_NORMAL
:
1087 n
= SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N
;
1089 case CCS_DATA_FORMAT_MODEL_TYPE_EXTENDED
:
1090 n
= CCS_LIM_DATA_FORMAT_DESCRIPTOR_MAX_N
+ 1;
1096 sensor
->default_pixel_order
= pixel_order
;
1097 sensor
->mbus_frame_fmts
= 0;
1099 for (i
= 0; i
< n
; i
++) {
1100 unsigned int fmt
, j
;
1102 fmt
= CCS_LIM_AT(sensor
, DATA_FORMAT_DESCRIPTOR
, i
);
1104 dev_dbg(&client
->dev
, "%u: bpp %u, compressed %u\n",
1105 i
, fmt
>> 8, (u8
)fmt
);
1107 for (j
= 0; j
< ARRAY_SIZE(ccs_csi_data_formats
); j
++) {
1108 const struct ccs_csi_data_format
*f
=
1109 &ccs_csi_data_formats
[j
];
1111 if (f
->pixel_order
!= CCS_PIXEL_ORDER_GRBG
)
1114 if (f
->width
!= fmt
>>
1115 CCS_DATA_FORMAT_DESCRIPTOR_UNCOMPRESSED_SHIFT
||
1117 (fmt
& CCS_DATA_FORMAT_DESCRIPTOR_COMPRESSED_MASK
))
1120 dev_dbg(&client
->dev
, "jolly good! %u\n", j
);
1122 sensor
->default_mbus_frame_fmts
|= 1 << j
;
1126 /* Figure out which BPP values can be used with which formats. */
1127 pll
->binning_horizontal
= 1;
1128 pll
->binning_vertical
= 1;
1129 pll
->scale_m
= sensor
->scale_m
;
1131 for (i
= 0; i
< ARRAY_SIZE(ccs_csi_data_formats
); i
++) {
1132 sensor
->compressed_min_bpp
=
1133 min(ccs_csi_data_formats
[i
].compressed
,
1134 sensor
->compressed_min_bpp
);
1135 compressed_max_bpp
=
1136 max(ccs_csi_data_formats
[i
].compressed
,
1137 compressed_max_bpp
);
1140 sensor
->valid_link_freqs
= devm_kcalloc(
1142 compressed_max_bpp
- sensor
->compressed_min_bpp
+ 1,
1143 sizeof(*sensor
->valid_link_freqs
), GFP_KERNEL
);
1144 if (!sensor
->valid_link_freqs
)
1147 for (i
= 0; i
< ARRAY_SIZE(ccs_csi_data_formats
); i
++) {
1148 const struct ccs_csi_data_format
*f
=
1149 &ccs_csi_data_formats
[i
];
1150 unsigned long *valid_link_freqs
=
1151 &sensor
->valid_link_freqs
[
1152 f
->compressed
- sensor
->compressed_min_bpp
];
1155 if (!(sensor
->default_mbus_frame_fmts
& 1 << i
))
1158 pll
->bits_per_pixel
= f
->compressed
;
1160 for (j
= 0; sensor
->hwcfg
.op_sys_clock
[j
]; j
++) {
1161 pll
->link_freq
= sensor
->hwcfg
.op_sys_clock
[j
];
1163 rval
= ccs_pll_try(sensor
, pll
);
1164 dev_dbg(&client
->dev
, "link freq %u Hz, bpp %u %s\n",
1165 pll
->link_freq
, pll
->bits_per_pixel
,
1166 rval
? "not ok" : "ok");
1170 set_bit(j
, valid_link_freqs
);
1173 if (!*valid_link_freqs
) {
1174 dev_info(&client
->dev
,
1175 "no valid link frequencies for %u bpp\n",
1177 sensor
->default_mbus_frame_fmts
&= ~BIT(i
);
1181 if (!sensor
->csi_format
1182 || f
->width
> sensor
->csi_format
->width
1183 || (f
->width
== sensor
->csi_format
->width
1184 && f
->compressed
> sensor
->csi_format
->compressed
)) {
1185 sensor
->csi_format
= f
;
1186 sensor
->internal_csi_format
= f
;
1190 if (!sensor
->csi_format
) {
1191 dev_err(&client
->dev
, "no supported mbus code found\n");
1195 ccs_update_mbus_formats(sensor
);
1200 static void ccs_update_blanking(struct ccs_sensor
*sensor
)
1202 struct v4l2_ctrl
*vblank
= sensor
->vblank
;
1203 struct v4l2_ctrl
*hblank
= sensor
->hblank
;
1204 u16 min_fll
, max_fll
, min_llp
, max_llp
, min_lbp
;
1207 if (sensor
->binning_vertical
> 1 || sensor
->binning_horizontal
> 1) {
1208 min_fll
= CCS_LIM(sensor
, MIN_FRAME_LENGTH_LINES_BIN
);
1209 max_fll
= CCS_LIM(sensor
, MAX_FRAME_LENGTH_LINES_BIN
);
1210 min_llp
= CCS_LIM(sensor
, MIN_LINE_LENGTH_PCK_BIN
);
1211 max_llp
= CCS_LIM(sensor
, MAX_LINE_LENGTH_PCK_BIN
);
1212 min_lbp
= CCS_LIM(sensor
, MIN_LINE_BLANKING_PCK_BIN
);
1214 min_fll
= CCS_LIM(sensor
, MIN_FRAME_LENGTH_LINES
);
1215 max_fll
= CCS_LIM(sensor
, MAX_FRAME_LENGTH_LINES
);
1216 min_llp
= CCS_LIM(sensor
, MIN_LINE_LENGTH_PCK
);
1217 max_llp
= CCS_LIM(sensor
, MAX_LINE_LENGTH_PCK
);
1218 min_lbp
= CCS_LIM(sensor
, MIN_LINE_BLANKING_PCK
);
1222 CCS_LIM(sensor
, MIN_FRAME_BLANKING_LINES
),
1223 min_fll
- sensor
->pa_src
.height
);
1224 max
= max_fll
- sensor
->pa_src
.height
;
1226 __v4l2_ctrl_modify_range(vblank
, min
, max
, vblank
->step
, min
);
1228 min
= max_t(int, min_llp
- sensor
->pa_src
.width
, min_lbp
);
1229 max
= max_llp
- sensor
->pa_src
.width
;
1231 __v4l2_ctrl_modify_range(hblank
, min
, max
, hblank
->step
, min
);
1233 __ccs_update_exposure_limits(sensor
);
1236 static int ccs_pll_blanking_update(struct ccs_sensor
*sensor
)
1238 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1241 rval
= ccs_pll_update(sensor
);
1245 /* Output from pixel array, including blanking */
1246 ccs_update_blanking(sensor
);
1248 dev_dbg(&client
->dev
, "vblank\t\t%d\n", sensor
->vblank
->val
);
1249 dev_dbg(&client
->dev
, "hblank\t\t%d\n", sensor
->hblank
->val
);
1251 dev_dbg(&client
->dev
, "real timeperframe\t100/%d\n",
1252 sensor
->pll
.pixel_rate_pixel_array
/
1253 ((sensor
->pa_src
.width
+ sensor
->hblank
->val
) *
1254 (sensor
->pa_src
.height
+ sensor
->vblank
->val
) / 100));
1261 * SMIA++ NVM handling
1265 static int ccs_read_nvm_page(struct ccs_sensor
*sensor
, u32 p
, u8
*nvm
,
1274 rval
= ccs_write(sensor
, DATA_TRANSFER_IF_1_PAGE_SELECT
, p
);
1278 rval
= ccs_write(sensor
, DATA_TRANSFER_IF_1_CTRL
,
1279 CCS_DATA_TRANSFER_IF_1_CTRL_ENABLE
);
1283 rval
= ccs_read(sensor
, DATA_TRANSFER_IF_1_STATUS
, &s
);
1287 if (s
& CCS_DATA_TRANSFER_IF_1_STATUS_IMPROPER_IF_USAGE
) {
1292 if (CCS_LIM(sensor
, DATA_TRANSFER_IF_CAPABILITY
) &
1293 CCS_DATA_TRANSFER_IF_CAPABILITY_POLLING
) {
1294 for (i
= 1000; i
> 0; i
--) {
1295 if (s
& CCS_DATA_TRANSFER_IF_1_STATUS_READ_IF_READY
)
1298 rval
= ccs_read(sensor
, DATA_TRANSFER_IF_1_STATUS
, &s
);
1307 for (i
= 0; i
<= CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P
; i
++) {
1310 rval
= ccs_read(sensor
, DATA_TRANSFER_IF_1_DATA(i
), &v
);
1320 static int ccs_read_nvm(struct ccs_sensor
*sensor
, unsigned char *nvm
,
1325 int rval
= 0, rval2
;
1327 for (p
= 0; p
< nvm_size
/ (CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P
+ 1)
1329 rval
= ccs_read_nvm_page(sensor
, p
, nvm
, &status
);
1330 nvm
+= CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P
+ 1;
1333 if (rval
== -ENODATA
&&
1334 status
& CCS_DATA_TRANSFER_IF_1_STATUS_IMPROPER_IF_USAGE
)
1337 rval2
= ccs_write(sensor
, DATA_TRANSFER_IF_1_CTRL
, 0);
1341 return rval2
?: p
* (CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P
+ 1);
1346 * SMIA++ CCI address control
1349 static int ccs_change_cci_addr(struct ccs_sensor
*sensor
)
1351 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1355 client
->addr
= sensor
->hwcfg
.i2c_addr_dfl
;
1357 rval
= ccs_write(sensor
, CCI_ADDRESS_CTRL
,
1358 sensor
->hwcfg
.i2c_addr_alt
<< 1);
1362 client
->addr
= sensor
->hwcfg
.i2c_addr_alt
;
1364 /* verify addr change went ok */
1365 rval
= ccs_read(sensor
, CCI_ADDRESS_CTRL
, &val
);
1369 if (val
!= sensor
->hwcfg
.i2c_addr_alt
<< 1)
1377 * SMIA++ Mode Control
1380 static int ccs_setup_flash_strobe(struct ccs_sensor
*sensor
)
1382 struct ccs_flash_strobe_parms
*strobe_setup
;
1383 unsigned int ext_freq
= sensor
->hwcfg
.ext_clk
;
1385 u32 strobe_adjustment
;
1386 u32 strobe_width_high_rs
;
1389 strobe_setup
= sensor
->hwcfg
.strobe_setup
;
1392 * How to calculate registers related to strobe length. Please
1393 * do not change, or if you do at least know what you're
1396 * Sakari Ailus <sakari.ailus@linux.intel.com> 2010-10-25
1398 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1399 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1401 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1402 * flash_strobe_adjustment E N, [1 - 0xff]
1404 * The formula above is written as below to keep it on one
1407 * l / 10^6 = w / e * a
1409 * Let's mark w * a by x:
1417 * The strobe width must be at least as long as requested,
1418 * thus rounding upwards is needed.
1420 * x = (l * e + 10^6 - 1) / 10^6
1421 * -----------------------------
1423 * Maximum possible accuracy is wanted at all times. Thus keep
1424 * a as small as possible.
1426 * Calculate a, assuming maximum w, with rounding upwards:
1428 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1429 * -------------------------------------
1431 * Thus, we also get w, with that a, with rounding upwards:
1433 * w = (x + a - 1) / a
1434 * -------------------
1438 * x E [1, (2^16 - 1) * (2^8 - 1)]
1440 * Substituting maximum x to the original formula (with rounding),
1441 * the maximum l is thus
1443 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1445 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1446 * --------------------------------------------------
1448 * flash_strobe_length must be clamped between 1 and
1449 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1453 * flash_strobe_adjustment = ((flash_strobe_length *
1454 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1456 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1457 * EXTCLK freq + 10^6 - 1) / 10^6 +
1458 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1460 tmp
= div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1461 1000000 + 1, ext_freq
);
1462 strobe_setup
->strobe_width_high_us
=
1463 clamp_t(u32
, strobe_setup
->strobe_width_high_us
, 1, tmp
);
1465 tmp
= div_u64(((u64
)strobe_setup
->strobe_width_high_us
* (u64
)ext_freq
+
1466 1000000 - 1), 1000000ULL);
1467 strobe_adjustment
= (tmp
+ (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1468 strobe_width_high_rs
= (tmp
+ strobe_adjustment
- 1) /
1471 rval
= ccs_write(sensor
, FLASH_MODE_RS
, strobe_setup
->mode
);
1475 rval
= ccs_write(sensor
, FLASH_STROBE_ADJUSTMENT
, strobe_adjustment
);
1479 rval
= ccs_write(sensor
, TFLASH_STROBE_WIDTH_HIGH_RS_CTRL
,
1480 strobe_width_high_rs
);
1484 rval
= ccs_write(sensor
, TFLASH_STROBE_DELAY_RS_CTRL
,
1485 strobe_setup
->strobe_delay
);
1489 rval
= ccs_write(sensor
, FLASH_STROBE_START_POINT
,
1490 strobe_setup
->stobe_start_point
);
1494 rval
= ccs_write(sensor
, FLASH_TRIGGER_RS
, strobe_setup
->trigger
);
1497 sensor
->hwcfg
.strobe_setup
->trigger
= 0;
1502 /* -----------------------------------------------------------------------------
1506 static int ccs_write_msr_regs(struct ccs_sensor
*sensor
)
1510 rval
= ccs_write_data_regs(sensor
,
1511 sensor
->sdata
.sensor_manufacturer_regs
,
1512 sensor
->sdata
.num_sensor_manufacturer_regs
);
1516 return ccs_write_data_regs(sensor
,
1517 sensor
->mdata
.module_manufacturer_regs
,
1518 sensor
->mdata
.num_module_manufacturer_regs
);
1521 static int ccs_update_phy_ctrl(struct ccs_sensor
*sensor
)
1523 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1526 if (!sensor
->ccs_limits
)
1529 if (CCS_LIM(sensor
, PHY_CTRL_CAPABILITY
) &
1530 CCS_PHY_CTRL_CAPABILITY_AUTO_PHY_CTL
) {
1531 val
= CCS_PHY_CTRL_AUTO
;
1532 } else if (CCS_LIM(sensor
, PHY_CTRL_CAPABILITY
) &
1533 CCS_PHY_CTRL_CAPABILITY_UI_PHY_CTL
) {
1534 val
= CCS_PHY_CTRL_UI
;
1536 dev_err(&client
->dev
, "manual PHY control not supported\n");
1540 return ccs_write(sensor
, PHY_CTRL
, val
);
1543 static int ccs_power_on(struct device
*dev
)
1545 struct v4l2_subdev
*subdev
= dev_get_drvdata(dev
);
1546 struct ccs_subdev
*ssd
= to_ccs_subdev(subdev
);
1548 * The sub-device related to the I2C device is always the
1549 * source one, i.e. ssds[0].
1551 struct ccs_sensor
*sensor
=
1552 container_of(ssd
, struct ccs_sensor
, ssds
[0]);
1553 const struct ccs_device
*ccsdev
= device_get_match_data(dev
);
1556 rval
= regulator_bulk_enable(ARRAY_SIZE(ccs_regulators
),
1557 sensor
->regulators
);
1559 dev_err(dev
, "failed to enable vana regulator\n");
1563 if (sensor
->reset
|| sensor
->xshutdown
|| sensor
->ext_clk
) {
1566 rval
= clk_prepare_enable(sensor
->ext_clk
);
1568 dev_dbg(dev
, "failed to enable xclk\n");
1572 gpiod_set_value(sensor
->reset
, 0);
1573 gpiod_set_value(sensor
->xshutdown
, 1);
1575 if (ccsdev
->flags
& CCS_DEVICE_FLAG_IS_SMIA
)
1576 sleep
= SMIAPP_RESET_DELAY(sensor
->hwcfg
.ext_clk
);
1580 usleep_range(sleep
, sleep
);
1584 * Failures to respond to the address change command have been noticed.
1585 * Those failures seem to be caused by the sensor requiring a longer
1586 * boot time than advertised. An additional 10ms delay seems to work
1587 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1588 * unnecessary. The failures need to be investigated to find a proper
1589 * fix, and a delay will likely need to be added here if the I2C write
1590 * retry hack is reverted before the root cause of the boot time issue
1594 if (!sensor
->reset
&& !sensor
->xshutdown
) {
1598 rval
= ccs_write(sensor
, SOFTWARE_RESET
, CCS_SOFTWARE_RESET_ON
);
1600 dev_err(dev
, "software reset failed\n");
1601 goto out_cci_addr_fail
;
1605 rval
= ccs_read(sensor
, SOFTWARE_RESET
, &reset
);
1606 reset
= !rval
&& reset
== CCS_SOFTWARE_RESET_OFF
;
1610 usleep_range(1000, 2000);
1614 dev_err(dev
, "software reset failed\n");
1616 goto out_cci_addr_fail
;
1620 if (sensor
->hwcfg
.i2c_addr_alt
) {
1621 rval
= ccs_change_cci_addr(sensor
);
1623 dev_err(dev
, "cci address change error\n");
1624 goto out_cci_addr_fail
;
1628 rval
= ccs_write(sensor
, COMPRESSION_MODE
,
1629 CCS_COMPRESSION_MODE_DPCM_PCM_SIMPLE
);
1631 dev_err(dev
, "compression mode set failed\n");
1632 goto out_cci_addr_fail
;
1635 rval
= ccs_write(sensor
, EXTCLK_FREQUENCY_MHZ
,
1636 sensor
->hwcfg
.ext_clk
/ (1000000 / (1 << 8)));
1638 dev_err(dev
, "extclk frequency set failed\n");
1639 goto out_cci_addr_fail
;
1642 rval
= ccs_write(sensor
, CSI_LANE_MODE
, sensor
->hwcfg
.lanes
- 1);
1644 dev_err(dev
, "csi lane mode set failed\n");
1645 goto out_cci_addr_fail
;
1648 rval
= ccs_write(sensor
, FAST_STANDBY_CTRL
,
1649 CCS_FAST_STANDBY_CTRL_FRAME_TRUNCATION
);
1651 dev_err(dev
, "fast standby set failed\n");
1652 goto out_cci_addr_fail
;
1655 rval
= ccs_write(sensor
, CSI_SIGNALING_MODE
,
1656 sensor
->hwcfg
.csi_signalling_mode
);
1658 dev_err(dev
, "csi signalling mode set failed\n");
1659 goto out_cci_addr_fail
;
1662 rval
= ccs_update_phy_ctrl(sensor
);
1664 goto out_cci_addr_fail
;
1666 rval
= ccs_write_msr_regs(sensor
);
1668 goto out_cci_addr_fail
;
1670 rval
= ccs_call_quirk(sensor
, post_poweron
);
1672 dev_err(dev
, "post_poweron quirks failed\n");
1673 goto out_cci_addr_fail
;
1679 gpiod_set_value(sensor
->reset
, 1);
1680 gpiod_set_value(sensor
->xshutdown
, 0);
1681 clk_disable_unprepare(sensor
->ext_clk
);
1684 regulator_bulk_disable(ARRAY_SIZE(ccs_regulators
),
1685 sensor
->regulators
);
1690 static int ccs_power_off(struct device
*dev
)
1692 struct v4l2_subdev
*subdev
= dev_get_drvdata(dev
);
1693 struct ccs_subdev
*ssd
= to_ccs_subdev(subdev
);
1694 struct ccs_sensor
*sensor
=
1695 container_of(ssd
, struct ccs_sensor
, ssds
[0]);
1698 * Currently power/clock to lens are enable/disabled separately
1699 * but they are essentially the same signals. So if the sensor is
1700 * powered off while the lens is powered on the sensor does not
1701 * really see a power off and next time the cci address change
1702 * will fail. So do a soft reset explicitly here.
1704 if (sensor
->hwcfg
.i2c_addr_alt
)
1705 ccs_write(sensor
, SOFTWARE_RESET
, CCS_SOFTWARE_RESET_ON
);
1707 gpiod_set_value(sensor
->reset
, 1);
1708 gpiod_set_value(sensor
->xshutdown
, 0);
1709 clk_disable_unprepare(sensor
->ext_clk
);
1710 usleep_range(5000, 5000);
1711 regulator_bulk_disable(ARRAY_SIZE(ccs_regulators
),
1712 sensor
->regulators
);
1713 sensor
->streaming
= false;
1718 /* -----------------------------------------------------------------------------
1719 * Video stream management
1722 static int ccs_start_streaming(struct ccs_sensor
*sensor
)
1724 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1725 unsigned int binning_mode
;
1728 mutex_lock(&sensor
->mutex
);
1730 rval
= ccs_write(sensor
, CSI_DATA_FORMAT
,
1731 (sensor
->csi_format
->width
<< 8) |
1732 sensor
->csi_format
->compressed
);
1736 /* Binning configuration */
1737 if (sensor
->binning_horizontal
== 1 &&
1738 sensor
->binning_vertical
== 1) {
1742 (sensor
->binning_horizontal
<< 4)
1743 | sensor
->binning_vertical
;
1745 rval
= ccs_write(sensor
, BINNING_TYPE
, binning_type
);
1751 rval
= ccs_write(sensor
, BINNING_MODE
, binning_mode
);
1756 rval
= ccs_pll_configure(sensor
);
1760 /* Analog crop start coordinates */
1761 rval
= ccs_write(sensor
, X_ADDR_START
, sensor
->pa_src
.left
);
1765 rval
= ccs_write(sensor
, Y_ADDR_START
, sensor
->pa_src
.top
);
1769 /* Analog crop end coordinates */
1770 rval
= ccs_write(sensor
, X_ADDR_END
,
1771 sensor
->pa_src
.left
+ sensor
->pa_src
.width
- 1);
1775 rval
= ccs_write(sensor
, Y_ADDR_END
,
1776 sensor
->pa_src
.top
+ sensor
->pa_src
.height
- 1);
1781 * Output from pixel array, including blanking, is set using
1782 * controls below. No need to set here.
1786 if (CCS_LIM(sensor
, DIGITAL_CROP_CAPABILITY
)
1787 == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
1788 rval
= ccs_write(sensor
, DIGITAL_CROP_X_OFFSET
,
1789 sensor
->scaler_sink
.left
);
1793 rval
= ccs_write(sensor
, DIGITAL_CROP_Y_OFFSET
,
1794 sensor
->scaler_sink
.top
);
1798 rval
= ccs_write(sensor
, DIGITAL_CROP_IMAGE_WIDTH
,
1799 sensor
->scaler_sink
.width
);
1803 rval
= ccs_write(sensor
, DIGITAL_CROP_IMAGE_HEIGHT
,
1804 sensor
->scaler_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
, sensor
->src_src
.width
);
1825 rval
= ccs_write(sensor
, Y_OUTPUT_SIZE
, sensor
->src_src
.height
);
1829 if (CCS_LIM(sensor
, FLASH_MODE_CAPABILITY
) &
1830 (CCS_FLASH_MODE_CAPABILITY_SINGLE_STROBE
|
1831 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE
) &&
1832 sensor
->hwcfg
.strobe_setup
!= NULL
&&
1833 sensor
->hwcfg
.strobe_setup
->trigger
!= 0) {
1834 rval
= ccs_setup_flash_strobe(sensor
);
1839 rval
= ccs_call_quirk(sensor
, pre_streamon
);
1841 dev_err(&client
->dev
, "pre_streamon quirks failed\n");
1845 rval
= ccs_write(sensor
, MODE_SELECT
, CCS_MODE_SELECT_STREAMING
);
1848 mutex_unlock(&sensor
->mutex
);
1853 static int ccs_stop_streaming(struct ccs_sensor
*sensor
)
1855 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1858 mutex_lock(&sensor
->mutex
);
1859 rval
= ccs_write(sensor
, MODE_SELECT
, CCS_MODE_SELECT_SOFTWARE_STANDBY
);
1863 rval
= ccs_call_quirk(sensor
, post_streamoff
);
1865 dev_err(&client
->dev
, "post_streamoff quirks failed\n");
1868 mutex_unlock(&sensor
->mutex
);
1872 /* -----------------------------------------------------------------------------
1873 * V4L2 subdev video operations
1876 static int ccs_pm_get_init(struct ccs_sensor
*sensor
)
1878 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1882 * It can't use pm_runtime_resume_and_get() here, as the driver
1883 * relies at the returned value to detect if the device was already
1886 rval
= pm_runtime_get_sync(&client
->dev
);
1890 /* Device was already active, so don't set controls */
1891 if (rval
== 1 && !sensor
->handler_setup_needed
)
1894 sensor
->handler_setup_needed
= false;
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
);
1919 ccs_stop_streaming(sensor
);
1920 sensor
->streaming
= false;
1921 pm_runtime_mark_last_busy(&client
->dev
);
1922 pm_runtime_put_autosuspend(&client
->dev
);
1927 rval
= ccs_pm_get_init(sensor
);
1931 sensor
->streaming
= true;
1933 rval
= ccs_start_streaming(sensor
);
1935 sensor
->streaming
= false;
1936 pm_runtime_mark_last_busy(&client
->dev
);
1937 pm_runtime_put_autosuspend(&client
->dev
);
1943 static int ccs_pre_streamon(struct v4l2_subdev
*subdev
, u32 flags
)
1945 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
1946 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1949 if (flags
& V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP
) {
1950 switch (sensor
->hwcfg
.csi_signalling_mode
) {
1951 case CCS_CSI_SIGNALING_MODE_CSI_2_DPHY
:
1952 if (!(CCS_LIM(sensor
, PHY_CTRL_CAPABILITY_2
) &
1953 CCS_PHY_CTRL_CAPABILITY_2_MANUAL_LP_DPHY
))
1956 case CCS_CSI_SIGNALING_MODE_CSI_2_CPHY
:
1957 if (!(CCS_LIM(sensor
, PHY_CTRL_CAPABILITY_2
) &
1958 CCS_PHY_CTRL_CAPABILITY_2_MANUAL_LP_CPHY
))
1966 rval
= ccs_pm_get_init(sensor
);
1970 if (flags
& V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP
) {
1971 rval
= ccs_write(sensor
, MANUAL_LP_CTRL
,
1972 CCS_MANUAL_LP_CTRL_ENABLE
);
1974 pm_runtime_put(&client
->dev
);
1980 static int ccs_post_streamoff(struct v4l2_subdev
*subdev
)
1982 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
1983 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
1985 return pm_runtime_put(&client
->dev
);
1988 static int ccs_enum_mbus_code(struct v4l2_subdev
*subdev
,
1989 struct v4l2_subdev_state
*sd_state
,
1990 struct v4l2_subdev_mbus_code_enum
*code
)
1992 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
1993 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
1998 mutex_lock(&sensor
->mutex
);
2000 dev_err(&client
->dev
, "subdev %s, pad %u, index %u\n",
2001 subdev
->name
, code
->pad
, code
->index
);
2003 if (subdev
!= &sensor
->src
->sd
|| code
->pad
!= CCS_PAD_SRC
) {
2007 code
->code
= sensor
->internal_csi_format
->code
;
2012 for (i
= 0; i
< ARRAY_SIZE(ccs_csi_data_formats
); i
++) {
2013 if (sensor
->mbus_frame_fmts
& (1 << i
))
2016 if (idx
== code
->index
) {
2017 code
->code
= ccs_csi_data_formats
[i
].code
;
2018 dev_err(&client
->dev
, "found index %u, i %u, code %x\n",
2019 code
->index
, i
, code
->code
);
2026 mutex_unlock(&sensor
->mutex
);
2031 static u32
__ccs_get_mbus_code(struct v4l2_subdev
*subdev
, unsigned int pad
)
2033 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2035 if (subdev
== &sensor
->src
->sd
&& pad
== CCS_PAD_SRC
)
2036 return sensor
->csi_format
->code
;
2038 return sensor
->internal_csi_format
->code
;
2041 static int __ccs_get_format(struct v4l2_subdev
*subdev
,
2042 struct v4l2_subdev_state
*sd_state
,
2043 struct v4l2_subdev_format
*fmt
)
2045 fmt
->format
= *v4l2_subdev_state_get_format(sd_state
, fmt
->pad
);
2046 fmt
->format
.code
= __ccs_get_mbus_code(subdev
, fmt
->pad
);
2051 static int ccs_get_format(struct v4l2_subdev
*subdev
,
2052 struct v4l2_subdev_state
*sd_state
,
2053 struct v4l2_subdev_format
*fmt
)
2055 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2058 mutex_lock(&sensor
->mutex
);
2059 rval
= __ccs_get_format(subdev
, sd_state
, fmt
);
2060 mutex_unlock(&sensor
->mutex
);
2065 static void ccs_get_crop_compose(struct v4l2_subdev
*subdev
,
2066 struct v4l2_subdev_state
*sd_state
,
2067 struct v4l2_rect
**crops
,
2068 struct v4l2_rect
**comps
)
2070 struct ccs_subdev
*ssd
= to_ccs_subdev(subdev
);
2074 for (i
= 0; i
< subdev
->entity
.num_pads
; i
++)
2076 v4l2_subdev_state_get_crop(sd_state
, i
);
2078 *comps
= v4l2_subdev_state_get_compose(sd_state
,
2082 /* Changes require propagation only on sink pad. */
2083 static void ccs_propagate(struct v4l2_subdev
*subdev
,
2084 struct v4l2_subdev_state
*sd_state
, int which
,
2087 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2088 struct ccs_subdev
*ssd
= to_ccs_subdev(subdev
);
2089 struct v4l2_rect
*comp
, *crops
[CCS_PADS
];
2090 struct v4l2_mbus_framefmt
*fmt
;
2092 ccs_get_crop_compose(subdev
, sd_state
, crops
, &comp
);
2095 case V4L2_SEL_TGT_CROP
:
2096 comp
->width
= crops
[CCS_PAD_SINK
]->width
;
2097 comp
->height
= crops
[CCS_PAD_SINK
]->height
;
2098 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2099 if (ssd
== sensor
->scaler
) {
2100 sensor
->scale_m
= CCS_LIM(sensor
, SCALER_N_MIN
);
2101 sensor
->scaling_mode
=
2102 CCS_SCALING_MODE_NO_SCALING
;
2103 sensor
->scaler_sink
= *comp
;
2104 } else if (ssd
== sensor
->binner
) {
2105 sensor
->binning_horizontal
= 1;
2106 sensor
->binning_vertical
= 1;
2110 case V4L2_SEL_TGT_COMPOSE
:
2111 *crops
[CCS_PAD_SRC
] = *comp
;
2112 fmt
= v4l2_subdev_state_get_format(sd_state
, CCS_PAD_SRC
);
2113 fmt
->width
= comp
->width
;
2114 fmt
->height
= comp
->height
;
2115 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
&& ssd
== sensor
->src
)
2116 sensor
->src_src
= *crops
[CCS_PAD_SRC
];
2123 static const struct ccs_csi_data_format
2124 *ccs_validate_csi_data_format(struct ccs_sensor
*sensor
, u32 code
)
2128 for (i
= 0; i
< ARRAY_SIZE(ccs_csi_data_formats
); i
++) {
2129 if (sensor
->mbus_frame_fmts
& (1 << i
) &&
2130 ccs_csi_data_formats
[i
].code
== code
)
2131 return &ccs_csi_data_formats
[i
];
2134 return sensor
->csi_format
;
2137 static int ccs_set_format_source(struct v4l2_subdev
*subdev
,
2138 struct v4l2_subdev_state
*sd_state
,
2139 struct v4l2_subdev_format
*fmt
)
2141 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2142 const struct ccs_csi_data_format
*csi_format
,
2143 *old_csi_format
= sensor
->csi_format
;
2144 unsigned long *valid_link_freqs
;
2145 u32 code
= fmt
->format
.code
;
2149 rval
= __ccs_get_format(subdev
, sd_state
, fmt
);
2154 * Media bus code is changeable on src subdev's source pad. On
2155 * other source pads we just get format here.
2157 if (subdev
!= &sensor
->src
->sd
)
2160 csi_format
= ccs_validate_csi_data_format(sensor
, code
);
2162 fmt
->format
.code
= csi_format
->code
;
2164 if (fmt
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
2167 sensor
->csi_format
= csi_format
;
2169 if (csi_format
->width
!= old_csi_format
->width
)
2170 for (i
= 0; i
< ARRAY_SIZE(sensor
->test_data
); i
++)
2171 __v4l2_ctrl_modify_range(
2172 sensor
->test_data
[i
], 0,
2173 (1 << csi_format
->width
) - 1, 1, 0);
2175 if (csi_format
->compressed
== old_csi_format
->compressed
)
2179 &sensor
->valid_link_freqs
[sensor
->csi_format
->compressed
2180 - sensor
->compressed_min_bpp
];
2182 __v4l2_ctrl_modify_range(
2183 sensor
->link_freq
, 0,
2184 __fls(*valid_link_freqs
), ~*valid_link_freqs
,
2185 __ffs(*valid_link_freqs
));
2187 return ccs_pll_update(sensor
);
2190 static int ccs_set_format(struct v4l2_subdev
*subdev
,
2191 struct v4l2_subdev_state
*sd_state
,
2192 struct v4l2_subdev_format
*fmt
)
2194 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2195 struct ccs_subdev
*ssd
= to_ccs_subdev(subdev
);
2196 struct v4l2_rect
*crops
[CCS_PADS
];
2198 mutex_lock(&sensor
->mutex
);
2200 if (fmt
->pad
== ssd
->source_pad
) {
2203 rval
= ccs_set_format_source(subdev
, sd_state
, fmt
);
2205 mutex_unlock(&sensor
->mutex
);
2210 /* Sink pad. Width and height are changeable here. */
2211 fmt
->format
.code
= __ccs_get_mbus_code(subdev
, fmt
->pad
);
2212 fmt
->format
.width
&= ~1;
2213 fmt
->format
.height
&= ~1;
2214 fmt
->format
.field
= V4L2_FIELD_NONE
;
2217 clamp(fmt
->format
.width
,
2218 CCS_LIM(sensor
, MIN_X_OUTPUT_SIZE
),
2219 CCS_LIM(sensor
, MAX_X_OUTPUT_SIZE
));
2220 fmt
->format
.height
=
2221 clamp(fmt
->format
.height
,
2222 CCS_LIM(sensor
, MIN_Y_OUTPUT_SIZE
),
2223 CCS_LIM(sensor
, MAX_Y_OUTPUT_SIZE
));
2225 ccs_get_crop_compose(subdev
, sd_state
, crops
, NULL
);
2227 crops
[ssd
->sink_pad
]->left
= 0;
2228 crops
[ssd
->sink_pad
]->top
= 0;
2229 crops
[ssd
->sink_pad
]->width
= fmt
->format
.width
;
2230 crops
[ssd
->sink_pad
]->height
= fmt
->format
.height
;
2231 ccs_propagate(subdev
, sd_state
, fmt
->which
, V4L2_SEL_TGT_CROP
);
2233 mutex_unlock(&sensor
->mutex
);
2239 * Calculate goodness of scaled image size compared to expected image
2240 * size and flags provided.
2242 #define SCALING_GOODNESS 100000
2243 #define SCALING_GOODNESS_EXTREME 100000000
2244 static int scaling_goodness(struct v4l2_subdev
*subdev
, int w
, int ask_w
,
2245 int h
, int ask_h
, u32 flags
)
2247 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2248 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2256 if (flags
& V4L2_SEL_FLAG_GE
) {
2258 val
-= SCALING_GOODNESS
;
2260 val
-= SCALING_GOODNESS
;
2263 if (flags
& V4L2_SEL_FLAG_LE
) {
2265 val
-= SCALING_GOODNESS
;
2267 val
-= SCALING_GOODNESS
;
2270 val
-= abs(w
- ask_w
);
2271 val
-= abs(h
- ask_h
);
2273 if (w
< CCS_LIM(sensor
, MIN_X_OUTPUT_SIZE
))
2274 val
-= SCALING_GOODNESS_EXTREME
;
2276 dev_dbg(&client
->dev
, "w %d ask_w %d h %d ask_h %d goodness %d\n",
2277 w
, ask_w
, h
, ask_h
, val
);
2282 static void ccs_set_compose_binner(struct v4l2_subdev
*subdev
,
2283 struct v4l2_subdev_state
*sd_state
,
2284 struct v4l2_subdev_selection
*sel
,
2285 struct v4l2_rect
**crops
,
2286 struct v4l2_rect
*comp
)
2288 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2290 unsigned int binh
= 1, binv
= 1;
2291 int best
= scaling_goodness(
2293 crops
[CCS_PAD_SINK
]->width
, sel
->r
.width
,
2294 crops
[CCS_PAD_SINK
]->height
, sel
->r
.height
, sel
->flags
);
2296 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
2297 int this = scaling_goodness(
2299 crops
[CCS_PAD_SINK
]->width
2300 / sensor
->binning_subtypes
[i
].horizontal
,
2302 crops
[CCS_PAD_SINK
]->height
2303 / sensor
->binning_subtypes
[i
].vertical
,
2304 sel
->r
.height
, sel
->flags
);
2307 binh
= sensor
->binning_subtypes
[i
].horizontal
;
2308 binv
= sensor
->binning_subtypes
[i
].vertical
;
2312 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2313 sensor
->binning_vertical
= binv
;
2314 sensor
->binning_horizontal
= binh
;
2317 sel
->r
.width
= (crops
[CCS_PAD_SINK
]->width
/ binh
) & ~1;
2318 sel
->r
.height
= (crops
[CCS_PAD_SINK
]->height
/ binv
) & ~1;
2322 * Calculate best scaling ratio and mode for given output resolution.
2324 * Try all of these: horizontal ratio, vertical ratio and smallest
2325 * size possible (horizontally).
2327 * Also try whether horizontal scaler or full scaler gives a better
2330 static void ccs_set_compose_scaler(struct v4l2_subdev
*subdev
,
2331 struct v4l2_subdev_state
*sd_state
,
2332 struct v4l2_subdev_selection
*sel
,
2333 struct v4l2_rect
**crops
,
2334 struct v4l2_rect
*comp
)
2336 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2337 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2338 u32 min
, max
, a
, b
, max_m
;
2339 u32 scale_m
= CCS_LIM(sensor
, SCALER_N_MIN
);
2340 int mode
= CCS_SCALING_MODE_HORIZONTAL
;
2346 sel
->r
.width
= min_t(unsigned int, sel
->r
.width
,
2347 crops
[CCS_PAD_SINK
]->width
);
2348 sel
->r
.height
= min_t(unsigned int, sel
->r
.height
,
2349 crops
[CCS_PAD_SINK
]->height
);
2351 a
= crops
[CCS_PAD_SINK
]->width
2352 * CCS_LIM(sensor
, SCALER_N_MIN
) / sel
->r
.width
;
2353 b
= crops
[CCS_PAD_SINK
]->height
2354 * CCS_LIM(sensor
, SCALER_N_MIN
) / sel
->r
.height
;
2355 max_m
= crops
[CCS_PAD_SINK
]->width
2356 * CCS_LIM(sensor
, SCALER_N_MIN
)
2357 / CCS_LIM(sensor
, MIN_X_OUTPUT_SIZE
);
2359 a
= clamp(a
, CCS_LIM(sensor
, SCALER_M_MIN
),
2360 CCS_LIM(sensor
, SCALER_M_MAX
));
2361 b
= clamp(b
, CCS_LIM(sensor
, SCALER_M_MIN
),
2362 CCS_LIM(sensor
, SCALER_M_MAX
));
2363 max_m
= clamp(max_m
, CCS_LIM(sensor
, SCALER_M_MIN
),
2364 CCS_LIM(sensor
, SCALER_M_MAX
));
2366 dev_dbg(&client
->dev
, "scaling: a %u b %u max_m %u\n", a
, b
, max_m
);
2368 min
= min(max_m
, min(a
, b
));
2369 max
= min(max_m
, max(a
, b
));
2378 try[ntry
] = min
+ 1;
2381 try[ntry
] = max
+ 1;
2386 for (i
= 0; i
< ntry
; i
++) {
2387 int this = scaling_goodness(
2389 crops
[CCS_PAD_SINK
]->width
2390 / try[i
] * CCS_LIM(sensor
, SCALER_N_MIN
),
2392 crops
[CCS_PAD_SINK
]->height
,
2396 dev_dbg(&client
->dev
, "trying factor %u (%u)\n", try[i
], i
);
2400 mode
= CCS_SCALING_MODE_HORIZONTAL
;
2404 if (CCS_LIM(sensor
, SCALING_CAPABILITY
)
2405 == CCS_SCALING_CAPABILITY_HORIZONTAL
)
2408 this = scaling_goodness(
2409 subdev
, crops
[CCS_PAD_SINK
]->width
2411 * CCS_LIM(sensor
, SCALER_N_MIN
),
2413 crops
[CCS_PAD_SINK
]->height
2415 * CCS_LIM(sensor
, SCALER_N_MIN
),
2421 mode
= SMIAPP_SCALING_MODE_BOTH
;
2427 (crops
[CCS_PAD_SINK
]->width
2429 * CCS_LIM(sensor
, SCALER_N_MIN
)) & ~1;
2430 if (mode
== SMIAPP_SCALING_MODE_BOTH
)
2432 (crops
[CCS_PAD_SINK
]->height
2434 * CCS_LIM(sensor
, SCALER_N_MIN
))
2437 sel
->r
.height
= crops
[CCS_PAD_SINK
]->height
;
2439 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
2440 sensor
->scale_m
= scale_m
;
2441 sensor
->scaling_mode
= mode
;
2444 /* We're only called on source pads. This function sets scaling. */
2445 static int ccs_set_compose(struct v4l2_subdev
*subdev
,
2446 struct v4l2_subdev_state
*sd_state
,
2447 struct v4l2_subdev_selection
*sel
)
2449 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2450 struct ccs_subdev
*ssd
= to_ccs_subdev(subdev
);
2451 struct v4l2_rect
*comp
, *crops
[CCS_PADS
];
2453 ccs_get_crop_compose(subdev
, sd_state
, crops
, &comp
);
2458 if (ssd
== sensor
->binner
)
2459 ccs_set_compose_binner(subdev
, sd_state
, sel
, crops
, comp
);
2461 ccs_set_compose_scaler(subdev
, sd_state
, sel
, crops
, comp
);
2464 ccs_propagate(subdev
, sd_state
, sel
->which
, V4L2_SEL_TGT_COMPOSE
);
2466 if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
2467 return ccs_pll_blanking_update(sensor
);
2472 static int ccs_sel_supported(struct v4l2_subdev
*subdev
,
2473 struct v4l2_subdev_selection
*sel
)
2475 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2476 struct ccs_subdev
*ssd
= to_ccs_subdev(subdev
);
2478 /* We only implement crop in three places. */
2479 switch (sel
->target
) {
2480 case V4L2_SEL_TGT_CROP
:
2481 case V4L2_SEL_TGT_CROP_BOUNDS
:
2482 if (ssd
== sensor
->pixel_array
&& sel
->pad
== CCS_PA_PAD_SRC
)
2484 if (ssd
== sensor
->src
&& sel
->pad
== CCS_PAD_SRC
)
2486 if (ssd
== sensor
->scaler
&& sel
->pad
== CCS_PAD_SINK
&&
2487 CCS_LIM(sensor
, DIGITAL_CROP_CAPABILITY
)
2488 == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP
)
2491 case V4L2_SEL_TGT_NATIVE_SIZE
:
2492 if (ssd
== sensor
->pixel_array
&& sel
->pad
== CCS_PA_PAD_SRC
)
2495 case V4L2_SEL_TGT_COMPOSE
:
2496 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2497 if (sel
->pad
== ssd
->source_pad
)
2499 if (ssd
== sensor
->binner
)
2501 if (ssd
== sensor
->scaler
&& CCS_LIM(sensor
, SCALING_CAPABILITY
)
2502 != CCS_SCALING_CAPABILITY_NONE
)
2510 static int ccs_set_crop(struct v4l2_subdev
*subdev
,
2511 struct v4l2_subdev_state
*sd_state
,
2512 struct v4l2_subdev_selection
*sel
)
2514 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2515 struct ccs_subdev
*ssd
= to_ccs_subdev(subdev
);
2516 struct v4l2_rect src_size
= { 0 }, *crops
[CCS_PADS
], *comp
;
2518 ccs_get_crop_compose(subdev
, sd_state
, crops
, &comp
);
2520 if (sel
->pad
== ssd
->sink_pad
) {
2521 struct v4l2_mbus_framefmt
*mfmt
=
2522 v4l2_subdev_state_get_format(sd_state
, sel
->pad
);
2524 src_size
.width
= mfmt
->width
;
2525 src_size
.height
= mfmt
->height
;
2530 if (ssd
== sensor
->src
&& sel
->pad
== CCS_PAD_SRC
) {
2535 sel
->r
.width
= min(sel
->r
.width
, src_size
.width
);
2536 sel
->r
.height
= min(sel
->r
.height
, src_size
.height
);
2538 sel
->r
.left
= min_t(int, sel
->r
.left
, src_size
.width
- sel
->r
.width
);
2539 sel
->r
.top
= min_t(int, sel
->r
.top
, src_size
.height
- sel
->r
.height
);
2541 *crops
[sel
->pad
] = sel
->r
;
2543 if (ssd
!= sensor
->pixel_array
&& sel
->pad
== CCS_PAD_SINK
)
2544 ccs_propagate(subdev
, sd_state
, sel
->which
, V4L2_SEL_TGT_CROP
);
2545 else if (sel
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
&&
2546 ssd
== sensor
->pixel_array
)
2547 sensor
->pa_src
= sel
->r
;
2552 static void ccs_get_native_size(struct ccs_subdev
*ssd
, struct v4l2_rect
*r
)
2556 r
->width
= CCS_LIM(ssd
->sensor
, X_ADDR_MAX
) + 1;
2557 r
->height
= CCS_LIM(ssd
->sensor
, Y_ADDR_MAX
) + 1;
2560 static int ccs_get_selection(struct v4l2_subdev
*subdev
,
2561 struct v4l2_subdev_state
*sd_state
,
2562 struct v4l2_subdev_selection
*sel
)
2564 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2565 struct ccs_subdev
*ssd
= to_ccs_subdev(subdev
);
2566 struct v4l2_rect
*comp
, *crops
[CCS_PADS
];
2569 ret
= ccs_sel_supported(subdev
, sel
);
2573 ccs_get_crop_compose(subdev
, sd_state
, crops
, &comp
);
2575 switch (sel
->target
) {
2576 case V4L2_SEL_TGT_CROP_BOUNDS
:
2577 case V4L2_SEL_TGT_NATIVE_SIZE
:
2578 if (ssd
== sensor
->pixel_array
) {
2579 ccs_get_native_size(ssd
, &sel
->r
);
2580 } else if (sel
->pad
== ssd
->sink_pad
) {
2581 struct v4l2_mbus_framefmt
*sink_fmt
=
2582 v4l2_subdev_state_get_format(sd_state
,
2584 sel
->r
.top
= sel
->r
.left
= 0;
2585 sel
->r
.width
= sink_fmt
->width
;
2586 sel
->r
.height
= sink_fmt
->height
;
2591 case V4L2_SEL_TGT_CROP
:
2592 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
2593 sel
->r
= *crops
[sel
->pad
];
2595 case V4L2_SEL_TGT_COMPOSE
:
2603 static int ccs_set_selection(struct v4l2_subdev
*subdev
,
2604 struct v4l2_subdev_state
*sd_state
,
2605 struct v4l2_subdev_selection
*sel
)
2607 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2610 ret
= ccs_sel_supported(subdev
, sel
);
2614 mutex_lock(&sensor
->mutex
);
2616 sel
->r
.left
= max(0, sel
->r
.left
& ~1);
2617 sel
->r
.top
= max(0, sel
->r
.top
& ~1);
2618 sel
->r
.width
= CCS_ALIGN_DIM(sel
->r
.width
, sel
->flags
);
2619 sel
->r
.height
= CCS_ALIGN_DIM(sel
->r
.height
, sel
->flags
);
2621 sel
->r
.width
= max_t(unsigned int, CCS_LIM(sensor
, MIN_X_OUTPUT_SIZE
),
2623 sel
->r
.height
= max_t(unsigned int, CCS_LIM(sensor
, MIN_Y_OUTPUT_SIZE
),
2626 switch (sel
->target
) {
2627 case V4L2_SEL_TGT_CROP
:
2628 ret
= ccs_set_crop(subdev
, sd_state
, sel
);
2630 case V4L2_SEL_TGT_COMPOSE
:
2631 ret
= ccs_set_compose(subdev
, sd_state
, sel
);
2637 mutex_unlock(&sensor
->mutex
);
2641 static int ccs_get_skip_frames(struct v4l2_subdev
*subdev
, u32
*frames
)
2643 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2645 *frames
= sensor
->frame_skip
;
2649 static int ccs_get_skip_top_lines(struct v4l2_subdev
*subdev
, u32
*lines
)
2651 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2653 *lines
= sensor
->image_start
;
2658 /* -----------------------------------------------------------------------------
2663 nvm_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2665 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2666 struct i2c_client
*client
= v4l2_get_subdevdata(subdev
);
2667 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2670 if (!sensor
->dev_init_done
)
2673 rval
= ccs_pm_get_init(sensor
);
2677 rval
= ccs_read_nvm(sensor
, buf
, PAGE_SIZE
);
2679 pm_runtime_put(&client
->dev
);
2680 dev_err(&client
->dev
, "nvm read failed\n");
2684 pm_runtime_mark_last_busy(&client
->dev
);
2685 pm_runtime_put_autosuspend(&client
->dev
);
2688 * NVM is still way below a PAGE_SIZE, so we can safely
2689 * assume this for now.
2693 static DEVICE_ATTR_RO(nvm
);
2696 ident_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2698 struct v4l2_subdev
*subdev
= i2c_get_clientdata(to_i2c_client(dev
));
2699 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2700 struct ccs_module_info
*minfo
= &sensor
->minfo
;
2702 if (minfo
->mipi_manufacturer_id
)
2703 return sysfs_emit(buf
, "%4.4x%4.4x%2.2x\n",
2704 minfo
->mipi_manufacturer_id
, minfo
->model_id
,
2705 minfo
->revision_number
) + 1;
2707 return sysfs_emit(buf
, "%2.2x%4.4x%2.2x\n",
2708 minfo
->smia_manufacturer_id
, minfo
->model_id
,
2709 minfo
->revision_number
) + 1;
2711 static DEVICE_ATTR_RO(ident
);
2713 /* -----------------------------------------------------------------------------
2714 * V4L2 subdev core operations
2717 static int ccs_identify_module(struct ccs_sensor
*sensor
)
2719 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2720 struct ccs_module_info
*minfo
= &sensor
->minfo
;
2726 rval
= ccs_read(sensor
, MODULE_MANUFACTURER_ID
,
2727 &minfo
->mipi_manufacturer_id
);
2728 if (!rval
&& !minfo
->mipi_manufacturer_id
)
2729 rval
= ccs_read_addr(sensor
, SMIAPP_REG_U8_MANUFACTURER_ID
,
2730 &minfo
->smia_manufacturer_id
);
2732 rval
= ccs_read(sensor
, MODULE_MODEL_ID
, &minfo
->model_id
);
2734 rval
= ccs_read(sensor
, MODULE_REVISION_NUMBER_MAJOR
, &rev
);
2736 rval
= ccs_read(sensor
, MODULE_REVISION_NUMBER_MINOR
,
2737 &minfo
->revision_number
);
2738 minfo
->revision_number
|= rev
<< 8;
2741 rval
= ccs_read(sensor
, MODULE_DATE_YEAR
, &minfo
->module_year
);
2743 rval
= ccs_read(sensor
, MODULE_DATE_MONTH
,
2744 &minfo
->module_month
);
2746 rval
= ccs_read(sensor
, MODULE_DATE_DAY
, &minfo
->module_day
);
2750 rval
= ccs_read(sensor
, SENSOR_MANUFACTURER_ID
,
2751 &minfo
->sensor_mipi_manufacturer_id
);
2752 if (!rval
&& !minfo
->sensor_mipi_manufacturer_id
)
2753 rval
= ccs_read(sensor
, SENSOR_MANUFACTURER_ID
,
2754 &minfo
->sensor_smia_manufacturer_id
);
2756 rval
= ccs_read(sensor
, SENSOR_MODEL_ID
,
2757 &minfo
->sensor_model_id
);
2759 rval
= ccs_read(sensor
, SENSOR_REVISION_NUMBER
,
2760 &minfo
->sensor_revision_number
);
2761 if (!rval
&& !minfo
->sensor_revision_number
)
2762 rval
= ccs_read(sensor
, SENSOR_REVISION_NUMBER_16
,
2763 &minfo
->sensor_revision_number
);
2765 rval
= ccs_read(sensor
, SENSOR_FIRMWARE_VERSION
,
2766 &minfo
->sensor_firmware_version
);
2770 rval
= ccs_read(sensor
, MIPI_CCS_VERSION
, &minfo
->ccs_version
);
2771 if (!rval
&& !minfo
->ccs_version
)
2772 rval
= ccs_read_addr(sensor
, SMIAPP_REG_U8_SMIA_VERSION
,
2773 &minfo
->smia_version
);
2774 if (!rval
&& !minfo
->ccs_version
)
2775 rval
= ccs_read_addr(sensor
, SMIAPP_REG_U8_SMIAPP_VERSION
,
2776 &minfo
->smiapp_version
);
2779 dev_err(&client
->dev
, "sensor detection failed\n");
2783 if (minfo
->mipi_manufacturer_id
)
2784 dev_dbg(&client
->dev
, "MIPI CCS module 0x%4.4x-0x%4.4x\n",
2785 minfo
->mipi_manufacturer_id
, minfo
->model_id
);
2787 dev_dbg(&client
->dev
, "SMIA module 0x%2.2x-0x%4.4x\n",
2788 minfo
->smia_manufacturer_id
, minfo
->model_id
);
2790 dev_dbg(&client
->dev
,
2791 "module revision 0x%4.4x date %2.2d-%2.2d-%2.2d\n",
2792 minfo
->revision_number
, minfo
->module_year
, minfo
->module_month
,
2795 if (minfo
->sensor_mipi_manufacturer_id
)
2796 dev_dbg(&client
->dev
, "MIPI CCS sensor 0x%4.4x-0x%4.4x\n",
2797 minfo
->sensor_mipi_manufacturer_id
,
2798 minfo
->sensor_model_id
);
2800 dev_dbg(&client
->dev
, "SMIA sensor 0x%2.2x-0x%4.4x\n",
2801 minfo
->sensor_smia_manufacturer_id
,
2802 minfo
->sensor_model_id
);
2804 dev_dbg(&client
->dev
,
2805 "sensor revision 0x%4.4x firmware version 0x%2.2x\n",
2806 minfo
->sensor_revision_number
, minfo
->sensor_firmware_version
);
2808 if (minfo
->ccs_version
) {
2809 dev_dbg(&client
->dev
, "MIPI CCS version %u.%u",
2810 (minfo
->ccs_version
& CCS_MIPI_CCS_VERSION_MAJOR_MASK
)
2811 >> CCS_MIPI_CCS_VERSION_MAJOR_SHIFT
,
2812 (minfo
->ccs_version
& CCS_MIPI_CCS_VERSION_MINOR_MASK
));
2813 minfo
->name
= CCS_NAME
;
2815 dev_dbg(&client
->dev
,
2816 "smia version %2.2d smiapp version %2.2d\n",
2817 minfo
->smia_version
, minfo
->smiapp_version
);
2818 minfo
->name
= SMIAPP_NAME
;
2820 * Some modules have bad data in the lvalues below. Hope the
2821 * rvalues have better stuff. The lvalues are module
2822 * parameters whereas the rvalues are sensor parameters.
2824 if (minfo
->sensor_smia_manufacturer_id
&&
2825 !minfo
->smia_manufacturer_id
&& !minfo
->model_id
) {
2826 minfo
->smia_manufacturer_id
=
2827 minfo
->sensor_smia_manufacturer_id
;
2828 minfo
->model_id
= minfo
->sensor_model_id
;
2829 minfo
->revision_number
= minfo
->sensor_revision_number
;
2833 for (i
= 0; i
< ARRAY_SIZE(ccs_module_idents
); i
++) {
2834 if (ccs_module_idents
[i
].mipi_manufacturer_id
&&
2835 ccs_module_idents
[i
].mipi_manufacturer_id
2836 != minfo
->mipi_manufacturer_id
)
2838 if (ccs_module_idents
[i
].smia_manufacturer_id
&&
2839 ccs_module_idents
[i
].smia_manufacturer_id
2840 != minfo
->smia_manufacturer_id
)
2842 if (ccs_module_idents
[i
].model_id
!= minfo
->model_id
)
2844 if (ccs_module_idents
[i
].flags
2845 & CCS_MODULE_IDENT_FLAG_REV_LE
) {
2846 if (ccs_module_idents
[i
].revision_number_major
2847 < (minfo
->revision_number
>> 8))
2850 if (ccs_module_idents
[i
].revision_number_major
2851 != (minfo
->revision_number
>> 8))
2855 minfo
->name
= ccs_module_idents
[i
].name
;
2856 minfo
->quirk
= ccs_module_idents
[i
].quirk
;
2860 if (i
>= ARRAY_SIZE(ccs_module_idents
))
2861 dev_warn(&client
->dev
,
2862 "no quirks for this module; let's hope it's fully compliant\n");
2864 dev_dbg(&client
->dev
, "the sensor is called %s\n", minfo
->name
);
2869 static const struct v4l2_subdev_ops ccs_ops
;
2870 static const struct media_entity_operations ccs_entity_ops
;
2872 static int ccs_register_subdev(struct ccs_sensor
*sensor
,
2873 struct ccs_subdev
*ssd
,
2874 struct ccs_subdev
*sink_ssd
,
2875 u16 source_pad
, u16 sink_pad
, u32 link_flags
)
2877 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2883 rval
= v4l2_device_register_subdev(sensor
->src
->sd
.v4l2_dev
, &ssd
->sd
);
2885 dev_err(&client
->dev
, "v4l2_device_register_subdev failed\n");
2889 rval
= media_create_pad_link(&ssd
->sd
.entity
, source_pad
,
2890 &sink_ssd
->sd
.entity
, sink_pad
,
2893 dev_err(&client
->dev
, "media_create_pad_link failed\n");
2894 v4l2_device_unregister_subdev(&ssd
->sd
);
2901 static void ccs_unregistered(struct v4l2_subdev
*subdev
)
2903 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2906 for (i
= 1; i
< sensor
->ssds_used
; i
++)
2907 v4l2_device_unregister_subdev(&sensor
->ssds
[i
].sd
);
2910 static int ccs_registered(struct v4l2_subdev
*subdev
)
2912 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
2915 if (sensor
->scaler
) {
2916 rval
= ccs_register_subdev(sensor
, sensor
->binner
,
2918 CCS_PAD_SRC
, CCS_PAD_SINK
,
2919 MEDIA_LNK_FL_ENABLED
|
2920 MEDIA_LNK_FL_IMMUTABLE
);
2925 rval
= ccs_register_subdev(sensor
, sensor
->pixel_array
, sensor
->binner
,
2926 CCS_PA_PAD_SRC
, CCS_PAD_SINK
,
2927 MEDIA_LNK_FL_ENABLED
|
2928 MEDIA_LNK_FL_IMMUTABLE
);
2935 ccs_unregistered(subdev
);
2940 static void ccs_cleanup(struct ccs_sensor
*sensor
)
2942 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2945 for (i
= 0; i
< sensor
->ssds_used
; i
++) {
2946 v4l2_subdev_cleanup(&sensor
->ssds
[2].sd
);
2947 media_entity_cleanup(&sensor
->ssds
[i
].sd
.entity
);
2950 device_remove_file(&client
->dev
, &dev_attr_nvm
);
2951 device_remove_file(&client
->dev
, &dev_attr_ident
);
2953 ccs_free_controls(sensor
);
2956 static int ccs_init_subdev(struct ccs_sensor
*sensor
,
2957 struct ccs_subdev
*ssd
, const char *name
,
2958 unsigned short num_pads
, u32 function
,
2959 const char *lock_name
,
2960 struct lock_class_key
*lock_key
)
2962 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->src
->sd
);
2968 if (ssd
!= sensor
->src
)
2969 v4l2_subdev_init(&ssd
->sd
, &ccs_ops
);
2971 ssd
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2972 ssd
->sd
.entity
.function
= function
;
2973 ssd
->sensor
= sensor
;
2975 ssd
->npads
= num_pads
;
2976 ssd
->source_pad
= num_pads
- 1;
2978 v4l2_i2c_subdev_set_name(&ssd
->sd
, client
, sensor
->minfo
.name
, name
);
2980 ssd
->pads
[ssd
->source_pad
].flags
= MEDIA_PAD_FL_SOURCE
;
2981 if (ssd
!= sensor
->pixel_array
)
2982 ssd
->pads
[ssd
->sink_pad
].flags
= MEDIA_PAD_FL_SINK
;
2984 ssd
->sd
.entity
.ops
= &ccs_entity_ops
;
2986 if (ssd
!= sensor
->src
) {
2987 ssd
->sd
.owner
= THIS_MODULE
;
2988 ssd
->sd
.dev
= &client
->dev
;
2989 v4l2_set_subdevdata(&ssd
->sd
, client
);
2992 rval
= media_entity_pads_init(&ssd
->sd
.entity
, ssd
->npads
, ssd
->pads
);
2994 dev_err(&client
->dev
, "media_entity_pads_init failed\n");
2998 rval
= __v4l2_subdev_init_finalize(&ssd
->sd
, lock_name
, lock_key
);
3000 media_entity_cleanup(&ssd
->sd
.entity
);
3007 static int ccs_init_state(struct v4l2_subdev
*sd
,
3008 struct v4l2_subdev_state
*sd_state
)
3010 struct ccs_subdev
*ssd
= to_ccs_subdev(sd
);
3011 struct ccs_sensor
*sensor
= ssd
->sensor
;
3012 unsigned int pad
= ssd
== sensor
->pixel_array
?
3013 CCS_PA_PAD_SRC
: CCS_PAD_SINK
;
3014 struct v4l2_mbus_framefmt
*fmt
=
3015 v4l2_subdev_state_get_format(sd_state
, pad
);
3016 struct v4l2_rect
*crop
=
3017 v4l2_subdev_state_get_crop(sd_state
, pad
);
3018 bool is_active
= !sd
->active_state
|| sd
->active_state
== sd_state
;
3020 mutex_lock(&sensor
->mutex
);
3022 ccs_get_native_size(ssd
, crop
);
3024 fmt
->width
= crop
->width
;
3025 fmt
->height
= crop
->height
;
3026 fmt
->code
= sensor
->internal_csi_format
->code
;
3027 fmt
->field
= V4L2_FIELD_NONE
;
3029 if (ssd
== sensor
->pixel_array
) {
3031 sensor
->pa_src
= *crop
;
3033 mutex_unlock(&sensor
->mutex
);
3037 fmt
= v4l2_subdev_state_get_format(sd_state
, CCS_PAD_SRC
);
3038 fmt
->code
= ssd
== sensor
->src
?
3039 sensor
->csi_format
->code
: sensor
->internal_csi_format
->code
;
3040 fmt
->field
= V4L2_FIELD_NONE
;
3042 ccs_propagate(sd
, sd_state
, is_active
, V4L2_SEL_TGT_CROP
);
3044 mutex_unlock(&sensor
->mutex
);
3049 static const struct v4l2_subdev_video_ops ccs_video_ops
= {
3050 .s_stream
= ccs_set_stream
,
3051 .pre_streamon
= ccs_pre_streamon
,
3052 .post_streamoff
= ccs_post_streamoff
,
3055 static const struct v4l2_subdev_pad_ops ccs_pad_ops
= {
3056 .enum_mbus_code
= ccs_enum_mbus_code
,
3057 .get_fmt
= ccs_get_format
,
3058 .set_fmt
= ccs_set_format
,
3059 .get_selection
= ccs_get_selection
,
3060 .set_selection
= ccs_set_selection
,
3063 static const struct v4l2_subdev_sensor_ops ccs_sensor_ops
= {
3064 .g_skip_frames
= ccs_get_skip_frames
,
3065 .g_skip_top_lines
= ccs_get_skip_top_lines
,
3068 static const struct v4l2_subdev_ops ccs_ops
= {
3069 .video
= &ccs_video_ops
,
3070 .pad
= &ccs_pad_ops
,
3071 .sensor
= &ccs_sensor_ops
,
3074 static const struct media_entity_operations ccs_entity_ops
= {
3075 .link_validate
= v4l2_subdev_link_validate
,
3078 static const struct v4l2_subdev_internal_ops ccs_internal_src_ops
= {
3079 .init_state
= ccs_init_state
,
3080 .registered
= ccs_registered
,
3081 .unregistered
= ccs_unregistered
,
3084 /* -----------------------------------------------------------------------------
3088 static int ccs_get_hwconfig(struct ccs_sensor
*sensor
, struct device
*dev
)
3090 struct ccs_hwconfig
*hwcfg
= &sensor
->hwcfg
;
3091 struct v4l2_fwnode_endpoint bus_cfg
= { .bus_type
= V4L2_MBUS_UNKNOWN
};
3092 struct fwnode_handle
*ep
;
3093 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
3097 ep
= fwnode_graph_get_endpoint_by_id(fwnode
, 0, 0,
3098 FWNODE_GRAPH_ENDPOINT_NEXT
);
3103 * Note that we do need to rely on detecting the bus type between CSI-2
3104 * D-PHY and CCP2 as the old bindings did not require it.
3106 rval
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
3110 switch (bus_cfg
.bus_type
) {
3111 case V4L2_MBUS_CSI2_DPHY
:
3112 hwcfg
->csi_signalling_mode
= CCS_CSI_SIGNALING_MODE_CSI_2_DPHY
;
3113 hwcfg
->lanes
= bus_cfg
.bus
.mipi_csi2
.num_data_lanes
;
3115 case V4L2_MBUS_CSI2_CPHY
:
3116 hwcfg
->csi_signalling_mode
= CCS_CSI_SIGNALING_MODE_CSI_2_CPHY
;
3117 hwcfg
->lanes
= bus_cfg
.bus
.mipi_csi2
.num_data_lanes
;
3119 case V4L2_MBUS_CSI1
:
3120 case V4L2_MBUS_CCP2
:
3121 hwcfg
->csi_signalling_mode
= (bus_cfg
.bus
.mipi_csi1
.strobe
) ?
3122 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE
:
3123 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK
;
3127 dev_err(dev
, "unsupported bus %u\n", bus_cfg
.bus_type
);
3132 rval
= fwnode_property_read_u32(dev_fwnode(dev
), "clock-frequency",
3135 dev_info(dev
, "can't get clock-frequency\n");
3137 dev_dbg(dev
, "clk %u, mode %u\n", hwcfg
->ext_clk
,
3138 hwcfg
->csi_signalling_mode
);
3140 if (!bus_cfg
.nr_of_link_frequencies
) {
3141 dev_warn(dev
, "no link frequencies defined\n");
3146 hwcfg
->op_sys_clock
= devm_kcalloc(
3147 dev
, bus_cfg
.nr_of_link_frequencies
+ 1 /* guardian */,
3148 sizeof(*hwcfg
->op_sys_clock
), GFP_KERNEL
);
3149 if (!hwcfg
->op_sys_clock
) {
3154 for (i
= 0; i
< bus_cfg
.nr_of_link_frequencies
; i
++) {
3155 hwcfg
->op_sys_clock
[i
] = bus_cfg
.link_frequencies
[i
];
3156 dev_dbg(dev
, "freq %u: %lld\n", i
, hwcfg
->op_sys_clock
[i
]);
3159 v4l2_fwnode_endpoint_free(&bus_cfg
);
3160 fwnode_handle_put(ep
);
3165 v4l2_fwnode_endpoint_free(&bus_cfg
);
3166 fwnode_handle_put(ep
);
3171 static int ccs_firmware_name(struct i2c_client
*client
,
3172 struct ccs_sensor
*sensor
, char *filename
,
3173 size_t filename_size
, bool is_module
)
3175 const struct ccs_device
*ccsdev
= device_get_match_data(&client
->dev
);
3176 bool is_ccs
= !(ccsdev
->flags
& CCS_DEVICE_FLAG_IS_SMIA
);
3177 bool is_smiapp
= sensor
->minfo
.smiapp_version
;
3178 u16 manufacturer_id
;
3180 u16 revision_number
;
3183 * Old SMIA is module-agnostic. Its sensor identification is based on
3184 * what now are those of the module.
3186 if (is_module
|| (!is_ccs
&& !is_smiapp
)) {
3187 manufacturer_id
= is_ccs
?
3188 sensor
->minfo
.mipi_manufacturer_id
:
3189 sensor
->minfo
.smia_manufacturer_id
;
3190 model_id
= sensor
->minfo
.model_id
;
3191 revision_number
= sensor
->minfo
.revision_number
;
3193 manufacturer_id
= is_ccs
?
3194 sensor
->minfo
.sensor_mipi_manufacturer_id
:
3195 sensor
->minfo
.sensor_smia_manufacturer_id
;
3196 model_id
= sensor
->minfo
.sensor_model_id
;
3197 revision_number
= sensor
->minfo
.sensor_revision_number
;
3200 return snprintf(filename
, filename_size
,
3201 "ccs/%s-%s-%0*x-%4.4x-%0*x.fw",
3202 is_ccs
? "ccs" : is_smiapp
? "smiapp" : "smia",
3203 is_module
|| (!is_ccs
&& !is_smiapp
) ?
3204 "module" : "sensor",
3205 is_ccs
? 4 : 2, manufacturer_id
, model_id
,
3206 !is_ccs
&& !is_module
? 2 : 4, revision_number
);
3209 static int ccs_probe(struct i2c_client
*client
)
3211 static struct lock_class_key pixel_array_lock_key
, binner_lock_key
,
3213 const struct ccs_device
*ccsdev
= device_get_match_data(&client
->dev
);
3214 struct ccs_sensor
*sensor
;
3215 const struct firmware
*fw
;
3220 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
3224 rval
= ccs_get_hwconfig(sensor
, &client
->dev
);
3228 sensor
->src
= &sensor
->ssds
[sensor
->ssds_used
];
3230 v4l2_i2c_subdev_init(&sensor
->src
->sd
, client
, &ccs_ops
);
3231 sensor
->src
->sd
.internal_ops
= &ccs_internal_src_ops
;
3233 sensor
->regulators
= devm_kcalloc(&client
->dev
,
3234 ARRAY_SIZE(ccs_regulators
),
3235 sizeof(*sensor
->regulators
),
3237 if (!sensor
->regulators
)
3240 for (i
= 0; i
< ARRAY_SIZE(ccs_regulators
); i
++)
3241 sensor
->regulators
[i
].supply
= ccs_regulators
[i
];
3243 rval
= devm_regulator_bulk_get(&client
->dev
, ARRAY_SIZE(ccs_regulators
),
3244 sensor
->regulators
);
3246 dev_err(&client
->dev
, "could not get regulators\n");
3250 sensor
->ext_clk
= devm_clk_get(&client
->dev
, NULL
);
3251 if (PTR_ERR(sensor
->ext_clk
) == -ENOENT
) {
3252 dev_info(&client
->dev
, "no clock defined, continuing...\n");
3253 sensor
->ext_clk
= NULL
;
3254 } else if (IS_ERR(sensor
->ext_clk
)) {
3255 dev_err(&client
->dev
, "could not get clock (%ld)\n",
3256 PTR_ERR(sensor
->ext_clk
));
3257 return -EPROBE_DEFER
;
3260 if (sensor
->ext_clk
) {
3261 if (sensor
->hwcfg
.ext_clk
) {
3264 rval
= clk_set_rate(sensor
->ext_clk
,
3265 sensor
->hwcfg
.ext_clk
);
3267 dev_err(&client
->dev
,
3268 "unable to set clock freq to %u\n",
3269 sensor
->hwcfg
.ext_clk
);
3273 rate
= clk_get_rate(sensor
->ext_clk
);
3274 if (rate
!= sensor
->hwcfg
.ext_clk
) {
3275 dev_err(&client
->dev
,
3276 "can't set clock freq, asked for %u but got %lu\n",
3277 sensor
->hwcfg
.ext_clk
, rate
);
3281 sensor
->hwcfg
.ext_clk
= clk_get_rate(sensor
->ext_clk
);
3282 dev_dbg(&client
->dev
, "obtained clock freq %u\n",
3283 sensor
->hwcfg
.ext_clk
);
3285 } else if (sensor
->hwcfg
.ext_clk
) {
3286 dev_dbg(&client
->dev
, "assuming clock freq %u\n",
3287 sensor
->hwcfg
.ext_clk
);
3289 dev_err(&client
->dev
, "unable to obtain clock freq\n");
3293 if (!sensor
->hwcfg
.ext_clk
) {
3294 dev_err(&client
->dev
, "cannot work with xclk frequency 0\n");
3298 sensor
->reset
= devm_gpiod_get_optional(&client
->dev
, "reset",
3300 if (IS_ERR(sensor
->reset
))
3301 return PTR_ERR(sensor
->reset
);
3302 /* Support old users that may have used "xshutdown" property. */
3304 sensor
->xshutdown
= devm_gpiod_get_optional(&client
->dev
,
3307 if (IS_ERR(sensor
->xshutdown
))
3308 return PTR_ERR(sensor
->xshutdown
);
3310 sensor
->regmap
= devm_cci_regmap_init_i2c(client
, 16);
3311 if (IS_ERR(sensor
->regmap
)) {
3312 dev_err(&client
->dev
, "can't initialise CCI (%ld)\n",
3313 PTR_ERR(sensor
->regmap
));
3314 return PTR_ERR(sensor
->regmap
);
3317 rval
= ccs_power_on(&client
->dev
);
3321 mutex_init(&sensor
->mutex
);
3323 rval
= ccs_identify_module(sensor
);
3329 rval
= ccs_firmware_name(client
, sensor
, filename
, sizeof(filename
),
3331 if (rval
>= sizeof(filename
)) {
3336 rval
= request_firmware(&fw
, filename
, &client
->dev
);
3338 ccs_data_parse(&sensor
->sdata
, fw
->data
, fw
->size
, &client
->dev
,
3340 release_firmware(fw
);
3343 if (!(ccsdev
->flags
& CCS_DEVICE_FLAG_IS_SMIA
) ||
3344 sensor
->minfo
.smiapp_version
) {
3345 rval
= ccs_firmware_name(client
, sensor
, filename
,
3346 sizeof(filename
), true);
3347 if (rval
>= sizeof(filename
)) {
3349 goto out_release_sdata
;
3352 rval
= request_firmware(&fw
, filename
, &client
->dev
);
3354 ccs_data_parse(&sensor
->mdata
, fw
->data
, fw
->size
,
3355 &client
->dev
, true);
3356 release_firmware(fw
);
3360 rval
= ccs_read_all_limits(sensor
);
3362 goto out_release_mdata
;
3364 rval
= ccs_read_frame_fmt(sensor
);
3367 goto out_free_ccs_limits
;
3370 rval
= ccs_update_phy_ctrl(sensor
);
3372 goto out_free_ccs_limits
;
3374 rval
= ccs_call_quirk(sensor
, limits
);
3376 dev_err(&client
->dev
, "limits quirks failed\n");
3377 goto out_free_ccs_limits
;
3380 if (CCS_LIM(sensor
, BINNING_CAPABILITY
)) {
3381 sensor
->nbinning_subtypes
=
3382 min_t(u8
, CCS_LIM(sensor
, BINNING_SUB_TYPES
),
3383 CCS_LIM_BINNING_SUB_TYPE_MAX_N
);
3385 for (i
= 0; i
< sensor
->nbinning_subtypes
; i
++) {
3386 sensor
->binning_subtypes
[i
].horizontal
=
3387 CCS_LIM_AT(sensor
, BINNING_SUB_TYPE
, i
) >>
3388 CCS_BINNING_SUB_TYPE_COLUMN_SHIFT
;
3389 sensor
->binning_subtypes
[i
].vertical
=
3390 CCS_LIM_AT(sensor
, BINNING_SUB_TYPE
, i
) &
3391 CCS_BINNING_SUB_TYPE_ROW_MASK
;
3393 dev_dbg(&client
->dev
, "binning %xx%x\n",
3394 sensor
->binning_subtypes
[i
].horizontal
,
3395 sensor
->binning_subtypes
[i
].vertical
);
3398 sensor
->binning_horizontal
= 1;
3399 sensor
->binning_vertical
= 1;
3401 if (device_create_file(&client
->dev
, &dev_attr_ident
) != 0) {
3402 dev_err(&client
->dev
, "sysfs ident entry creation failed\n");
3404 goto out_free_ccs_limits
;
3407 if (sensor
->minfo
.smiapp_version
&&
3408 CCS_LIM(sensor
, DATA_TRANSFER_IF_CAPABILITY
) &
3409 CCS_DATA_TRANSFER_IF_CAPABILITY_SUPPORTED
) {
3410 if (device_create_file(&client
->dev
, &dev_attr_nvm
) != 0) {
3411 dev_err(&client
->dev
, "sysfs nvm entry failed\n");
3417 if (!CCS_LIM(sensor
, MIN_OP_SYS_CLK_DIV
) ||
3418 !CCS_LIM(sensor
, MAX_OP_SYS_CLK_DIV
) ||
3419 !CCS_LIM(sensor
, MIN_OP_PIX_CLK_DIV
) ||
3420 !CCS_LIM(sensor
, MAX_OP_PIX_CLK_DIV
)) {
3421 /* No OP clock branch */
3422 sensor
->pll
.flags
|= CCS_PLL_FLAG_NO_OP_CLOCKS
;
3423 } else if (CCS_LIM(sensor
, SCALING_CAPABILITY
)
3424 != CCS_SCALING_CAPABILITY_NONE
||
3425 CCS_LIM(sensor
, DIGITAL_CROP_CAPABILITY
)
3426 == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP
) {
3427 /* We have a scaler or digital crop. */
3428 sensor
->scaler
= &sensor
->ssds
[sensor
->ssds_used
];
3429 sensor
->ssds_used
++;
3431 sensor
->binner
= &sensor
->ssds
[sensor
->ssds_used
];
3432 sensor
->ssds_used
++;
3433 sensor
->pixel_array
= &sensor
->ssds
[sensor
->ssds_used
];
3434 sensor
->ssds_used
++;
3436 sensor
->scale_m
= CCS_LIM(sensor
, SCALER_N_MIN
);
3438 /* prepare PLL configuration input values */
3439 sensor
->pll
.bus_type
= CCS_PLL_BUS_TYPE_CSI2_DPHY
;
3440 sensor
->pll
.csi2
.lanes
= sensor
->hwcfg
.lanes
;
3441 if (CCS_LIM(sensor
, CLOCK_CALCULATION
) &
3442 CCS_CLOCK_CALCULATION_LANE_SPEED
) {
3443 sensor
->pll
.flags
|= CCS_PLL_FLAG_LANE_SPEED_MODEL
;
3444 if (CCS_LIM(sensor
, CLOCK_CALCULATION
) &
3445 CCS_CLOCK_CALCULATION_LINK_DECOUPLED
) {
3446 sensor
->pll
.vt_lanes
=
3447 CCS_LIM(sensor
, NUM_OF_VT_LANES
) + 1;
3448 sensor
->pll
.op_lanes
=
3449 CCS_LIM(sensor
, NUM_OF_OP_LANES
) + 1;
3450 sensor
->pll
.flags
|= CCS_PLL_FLAG_LINK_DECOUPLED
;
3452 sensor
->pll
.vt_lanes
= sensor
->pll
.csi2
.lanes
;
3453 sensor
->pll
.op_lanes
= sensor
->pll
.csi2
.lanes
;
3456 if (CCS_LIM(sensor
, CLOCK_TREE_PLL_CAPABILITY
) &
3457 CCS_CLOCK_TREE_PLL_CAPABILITY_EXT_DIVIDER
)
3458 sensor
->pll
.flags
|= CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER
;
3459 if (CCS_LIM(sensor
, CLOCK_TREE_PLL_CAPABILITY
) &
3460 CCS_CLOCK_TREE_PLL_CAPABILITY_FLEXIBLE_OP_PIX_CLK_DIV
)
3461 sensor
->pll
.flags
|= CCS_PLL_FLAG_FLEXIBLE_OP_PIX_CLK_DIV
;
3462 if (CCS_LIM(sensor
, FIFO_SUPPORT_CAPABILITY
) &
3463 CCS_FIFO_SUPPORT_CAPABILITY_DERATING
)
3464 sensor
->pll
.flags
|= CCS_PLL_FLAG_FIFO_DERATING
;
3465 if (CCS_LIM(sensor
, FIFO_SUPPORT_CAPABILITY
) &
3466 CCS_FIFO_SUPPORT_CAPABILITY_DERATING_OVERRATING
)
3467 sensor
->pll
.flags
|= CCS_PLL_FLAG_FIFO_DERATING
|
3468 CCS_PLL_FLAG_FIFO_OVERRATING
;
3469 if (CCS_LIM(sensor
, CLOCK_TREE_PLL_CAPABILITY
) &
3470 CCS_CLOCK_TREE_PLL_CAPABILITY_DUAL_PLL
) {
3471 if (CCS_LIM(sensor
, CLOCK_TREE_PLL_CAPABILITY
) &
3472 CCS_CLOCK_TREE_PLL_CAPABILITY_SINGLE_PLL
) {
3475 /* Use sensor default in PLL mode selection */
3476 rval
= ccs_read(sensor
, PLL_MODE
, &v
);
3480 if (v
== CCS_PLL_MODE_DUAL
)
3481 sensor
->pll
.flags
|= CCS_PLL_FLAG_DUAL_PLL
;
3483 sensor
->pll
.flags
|= CCS_PLL_FLAG_DUAL_PLL
;
3485 if (CCS_LIM(sensor
, CLOCK_CALCULATION
) &
3486 CCS_CLOCK_CALCULATION_DUAL_PLL_OP_SYS_DDR
)
3487 sensor
->pll
.flags
|= CCS_PLL_FLAG_OP_SYS_DDR
;
3488 if (CCS_LIM(sensor
, CLOCK_CALCULATION
) &
3489 CCS_CLOCK_CALCULATION_DUAL_PLL_OP_PIX_DDR
)
3490 sensor
->pll
.flags
|= CCS_PLL_FLAG_OP_PIX_DDR
;
3492 sensor
->pll
.op_bits_per_lane
= CCS_LIM(sensor
, OP_BITS_PER_LANE
);
3493 sensor
->pll
.ext_clk_freq_hz
= sensor
->hwcfg
.ext_clk
;
3494 sensor
->pll
.scale_n
= CCS_LIM(sensor
, SCALER_N_MIN
);
3496 rval
= ccs_get_mbus_formats(sensor
);
3502 rval
= ccs_init_subdev(sensor
, sensor
->scaler
, " scaler", 2,
3503 MEDIA_ENT_F_PROC_VIDEO_SCALER
,
3504 "ccs scaler mutex", &scaler_lock_key
);
3507 rval
= ccs_init_subdev(sensor
, sensor
->binner
, " binner", 2,
3508 MEDIA_ENT_F_PROC_VIDEO_SCALER
,
3509 "ccs binner mutex", &binner_lock_key
);
3512 rval
= ccs_init_subdev(sensor
, sensor
->pixel_array
, " pixel_array", 1,
3513 MEDIA_ENT_F_CAM_SENSOR
, "ccs pixel array mutex",
3514 &pixel_array_lock_key
);
3518 rval
= ccs_init_controls(sensor
);
3522 rval
= ccs_call_quirk(sensor
, init
);
3526 rval
= ccs_init_late_controls(sensor
);
3532 mutex_lock(&sensor
->mutex
);
3533 rval
= ccs_pll_blanking_update(sensor
);
3534 mutex_unlock(&sensor
->mutex
);
3536 dev_err(&client
->dev
, "update mode failed\n");
3540 sensor
->streaming
= false;
3541 sensor
->dev_init_done
= true;
3542 sensor
->handler_setup_needed
= true;
3544 rval
= ccs_write_msr_regs(sensor
);
3548 pm_runtime_set_active(&client
->dev
);
3549 pm_runtime_get_noresume(&client
->dev
);
3550 pm_runtime_enable(&client
->dev
);
3552 rval
= v4l2_async_register_subdev_sensor(&sensor
->src
->sd
);
3554 goto out_disable_runtime_pm
;
3556 pm_runtime_set_autosuspend_delay(&client
->dev
, 1000);
3557 pm_runtime_use_autosuspend(&client
->dev
);
3558 pm_runtime_put_autosuspend(&client
->dev
);
3562 out_disable_runtime_pm
:
3563 pm_runtime_put_noidle(&client
->dev
);
3564 pm_runtime_disable(&client
->dev
);
3567 ccs_cleanup(sensor
);
3570 kvfree(sensor
->mdata
.backing
);
3573 kvfree(sensor
->sdata
.backing
);
3575 out_free_ccs_limits
:
3576 kfree(sensor
->ccs_limits
);
3579 ccs_power_off(&client
->dev
);
3580 mutex_destroy(&sensor
->mutex
);
3585 static void ccs_remove(struct i2c_client
*client
)
3587 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
3588 struct ccs_sensor
*sensor
= to_ccs_sensor(subdev
);
3591 v4l2_async_unregister_subdev(subdev
);
3593 pm_runtime_disable(&client
->dev
);
3594 if (!pm_runtime_status_suspended(&client
->dev
))
3595 ccs_power_off(&client
->dev
);
3596 pm_runtime_set_suspended(&client
->dev
);
3598 for (i
= 0; i
< sensor
->ssds_used
; i
++)
3599 v4l2_device_unregister_subdev(&sensor
->ssds
[i
].sd
);
3600 ccs_cleanup(sensor
);
3601 mutex_destroy(&sensor
->mutex
);
3602 kfree(sensor
->ccs_limits
);
3603 kvfree(sensor
->sdata
.backing
);
3604 kvfree(sensor
->mdata
.backing
);
3607 static const struct ccs_device smia_device
= {
3608 .flags
= CCS_DEVICE_FLAG_IS_SMIA
,
3611 static const struct ccs_device ccs_device
= {};
3613 static const struct acpi_device_id ccs_acpi_table
[] = {
3614 { .id
= "MIPI0200", .driver_data
= (unsigned long)&ccs_device
},
3617 MODULE_DEVICE_TABLE(acpi
, ccs_acpi_table
);
3619 static const struct of_device_id ccs_of_table
[] = {
3620 { .compatible
= "mipi-ccs-1.1", .data
= &ccs_device
},
3621 { .compatible
= "mipi-ccs-1.0", .data
= &ccs_device
},
3622 { .compatible
= "mipi-ccs", .data
= &ccs_device
},
3623 { .compatible
= "nokia,smia", .data
= &smia_device
},
3626 MODULE_DEVICE_TABLE(of
, ccs_of_table
);
3628 static const struct dev_pm_ops ccs_pm_ops
= {
3629 SET_RUNTIME_PM_OPS(ccs_power_off
, ccs_power_on
, NULL
)
3632 static struct i2c_driver ccs_i2c_driver
= {
3634 .acpi_match_table
= ccs_acpi_table
,
3635 .of_match_table
= ccs_of_table
,
3640 .remove
= ccs_remove
,
3643 static int ccs_module_init(void)
3649 for (i
= 0, l
= 0; ccs_limits
[i
].size
&& l
< CCS_L_LAST
; i
++) {
3650 if (!(ccs_limits
[i
].flags
& CCS_L_FL_SAME_REG
)) {
3651 ccs_limit_offsets
[l
+ 1].lim
=
3652 ALIGN(ccs_limit_offsets
[l
].lim
+
3654 ccs_limits
[i
+ 1].reg
?
3655 CCI_REG_WIDTH_BYTES(ccs_limits
[i
+ 1].reg
) :
3657 ccs_limit_offsets
[l
].info
= i
;
3660 ccs_limit_offsets
[l
].lim
+= ccs_limits
[i
].size
;
3664 if (WARN_ON(ccs_limits
[i
].size
))
3667 if (WARN_ON(l
!= CCS_L_LAST
))
3670 return i2c_register_driver(THIS_MODULE
, &ccs_i2c_driver
);
3673 static void ccs_module_cleanup(void)
3675 i2c_del_driver(&ccs_i2c_driver
);
3678 module_init(ccs_module_init
);
3679 module_exit(ccs_module_cleanup
);
3681 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
3682 MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ camera sensor driver");
3683 MODULE_LICENSE("GPL v2");
3684 MODULE_ALIAS("smiapp");