1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * drivers/media/i2c/ccs/ccs-core.c 4 * 5 * Generic driver for MIPI CCS/SMIA/SMIA++ compliant camera sensors 6 * 7 * Copyright (C) 2020 Intel Corporation 8 * Copyright (C) 2010--2012 Nokia Corporation 9 * Contact: Sakari Ailus <sakari.ailus@linux.intel.com> 10 * 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> 14 */ 15 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-fwnode.h> 29 #include <media/v4l2-device.h> 30 #include <uapi/linux/ccs.h> 31 32 #include "ccs.h" 33 34 #define CCS_ALIGN_DIM(dim, flags) \ 35 ((flags) & V4L2_SEL_FLAG_GE \ 36 ? ALIGN((dim), 2) \ 37 : (dim) & ~1) 38 39 static struct ccs_limit_offset { 40 u16 lim; 41 u16 info; 42 } ccs_limit_offsets[CCS_L_LAST + 1]; 43 44 /* 45 * ccs_module_idents - supported camera modules 46 */ 47 static const struct ccs_module_ident ccs_module_idents[] = { 48 CCS_IDENT_L(0x01, 0x022b, -1, "vs6555"), 49 CCS_IDENT_L(0x01, 0x022e, -1, "vw6558"), 50 CCS_IDENT_L(0x07, 0x7698, -1, "ovm7698"), 51 CCS_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"), 52 CCS_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"), 53 CCS_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk), 54 CCS_IDENT_L(0x0c, 0x213e, -1, "et8en2"), 55 CCS_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"), 56 CCS_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk), 57 CCS_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk), 58 CCS_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk), 59 }; 60 61 #define CCS_DEVICE_FLAG_IS_SMIA BIT(0) 62 63 struct ccs_device { 64 unsigned char flags; 65 }; 66 67 static const char * const ccs_regulators[] = { "vcore", "vio", "vana" }; 68 69 /* 70 * 71 * Dynamic Capability Identification 72 * 73 */ 74 75 static void ccs_assign_limit(void *ptr, unsigned int width, u32 val) 76 { 77 switch (width) { 78 case sizeof(u8): 79 *(u8 *)ptr = val; 80 break; 81 case sizeof(u16): 82 *(u16 *)ptr = val; 83 break; 84 case sizeof(u32): 85 *(u32 *)ptr = val; 86 break; 87 } 88 } 89 90 static int ccs_limit_ptr(struct ccs_sensor *sensor, unsigned int limit, 91 unsigned int offset, void **__ptr) 92 { 93 const struct ccs_limit *linfo; 94 95 if (WARN_ON(limit >= CCS_L_LAST)) 96 return -EINVAL; 97 98 linfo = &ccs_limits[ccs_limit_offsets[limit].info]; 99 100 if (WARN_ON(!sensor->ccs_limits) || 101 WARN_ON(offset + ccs_reg_width(linfo->reg) > 102 ccs_limit_offsets[limit + 1].lim)) 103 return -EINVAL; 104 105 *__ptr = sensor->ccs_limits + ccs_limit_offsets[limit].lim + offset; 106 107 return 0; 108 } 109 110 void ccs_replace_limit(struct ccs_sensor *sensor, 111 unsigned int limit, unsigned int offset, u32 val) 112 { 113 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 114 const struct ccs_limit *linfo; 115 void *ptr; 116 int ret; 117 118 ret = ccs_limit_ptr(sensor, limit, offset, &ptr); 119 if (ret) 120 return; 121 122 linfo = &ccs_limits[ccs_limit_offsets[limit].info]; 123 124 dev_dbg(&client->dev, "quirk: 0x%8.8x \"%s\" %u = %u, 0x%x\n", 125 linfo->reg, linfo->name, offset, val, val); 126 127 ccs_assign_limit(ptr, ccs_reg_width(linfo->reg), val); 128 } 129 130 u32 ccs_get_limit(struct ccs_sensor *sensor, unsigned int limit, 131 unsigned int offset) 132 { 133 void *ptr; 134 u32 val; 135 int ret; 136 137 ret = ccs_limit_ptr(sensor, limit, offset, &ptr); 138 if (ret) 139 return 0; 140 141 switch (ccs_reg_width(ccs_limits[ccs_limit_offsets[limit].info].reg)) { 142 case sizeof(u8): 143 val = *(u8 *)ptr; 144 break; 145 case sizeof(u16): 146 val = *(u16 *)ptr; 147 break; 148 case sizeof(u32): 149 val = *(u32 *)ptr; 150 break; 151 default: 152 WARN_ON(1); 153 return 0; 154 } 155 156 return ccs_reg_conv(sensor, ccs_limits[limit].reg, val); 157 } 158 159 static int ccs_read_all_limits(struct ccs_sensor *sensor) 160 { 161 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 162 void *ptr, *alloc, *end; 163 unsigned int i, l; 164 int ret; 165 166 kfree(sensor->ccs_limits); 167 sensor->ccs_limits = NULL; 168 169 alloc = kzalloc(ccs_limit_offsets[CCS_L_LAST].lim, GFP_KERNEL); 170 if (!alloc) 171 return -ENOMEM; 172 173 end = alloc + ccs_limit_offsets[CCS_L_LAST].lim; 174 175 for (i = 0, l = 0, ptr = alloc; ccs_limits[i].size; i++) { 176 u32 reg = ccs_limits[i].reg; 177 unsigned int width = ccs_reg_width(reg); 178 unsigned int j; 179 180 if (l == CCS_L_LAST) { 181 dev_err(&client->dev, 182 "internal error --- end of limit array\n"); 183 ret = -EINVAL; 184 goto out_err; 185 } 186 187 for (j = 0; j < ccs_limits[i].size / width; 188 j++, reg += width, ptr += width) { 189 u32 val; 190 191 ret = ccs_read_addr_noconv(sensor, reg, &val); 192 if (ret) 193 goto out_err; 194 195 if (ptr + width > end) { 196 dev_err(&client->dev, 197 "internal error --- no room for regs\n"); 198 ret = -EINVAL; 199 goto out_err; 200 } 201 202 if (!val && j) 203 break; 204 205 ccs_assign_limit(ptr, width, val); 206 207 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %u, 0x%x\n", 208 reg, ccs_limits[i].name, val, val); 209 } 210 211 if (ccs_limits[i].flags & CCS_L_FL_SAME_REG) 212 continue; 213 214 l++; 215 ptr = alloc + ccs_limit_offsets[l].lim; 216 } 217 218 if (l != CCS_L_LAST) { 219 dev_err(&client->dev, 220 "internal error --- insufficient limits\n"); 221 ret = -EINVAL; 222 goto out_err; 223 } 224 225 sensor->ccs_limits = alloc; 226 227 if (CCS_LIM(sensor, SCALER_N_MIN) < 16) 228 ccs_replace_limit(sensor, CCS_L_SCALER_N_MIN, 0, 16); 229 230 return 0; 231 232 out_err: 233 kfree(alloc); 234 235 return ret; 236 } 237 238 static int ccs_read_frame_fmt(struct ccs_sensor *sensor) 239 { 240 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 241 u8 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc; 242 unsigned int i; 243 int pixel_count = 0; 244 int line_count = 0; 245 246 fmt_model_type = CCS_LIM(sensor, FRAME_FORMAT_MODEL_TYPE); 247 fmt_model_subtype = CCS_LIM(sensor, FRAME_FORMAT_MODEL_SUBTYPE); 248 249 ncol_desc = (fmt_model_subtype 250 & CCS_FRAME_FORMAT_MODEL_SUBTYPE_COLUMNS_MASK) 251 >> CCS_FRAME_FORMAT_MODEL_SUBTYPE_COLUMNS_SHIFT; 252 nrow_desc = fmt_model_subtype 253 & CCS_FRAME_FORMAT_MODEL_SUBTYPE_ROWS_MASK; 254 255 dev_dbg(&client->dev, "format_model_type %s\n", 256 fmt_model_type == CCS_FRAME_FORMAT_MODEL_TYPE_2_BYTE 257 ? "2 byte" : 258 fmt_model_type == CCS_FRAME_FORMAT_MODEL_TYPE_4_BYTE 259 ? "4 byte" : "is simply bad"); 260 261 dev_dbg(&client->dev, "%u column and %u row descriptors\n", 262 ncol_desc, nrow_desc); 263 264 for (i = 0; i < ncol_desc + nrow_desc; i++) { 265 u32 desc; 266 u32 pixelcode; 267 u32 pixels; 268 char *which; 269 char *what; 270 271 if (fmt_model_type == CCS_FRAME_FORMAT_MODEL_TYPE_2_BYTE) { 272 desc = CCS_LIM_AT(sensor, FRAME_FORMAT_DESCRIPTOR, i); 273 274 pixelcode = 275 (desc 276 & CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_MASK) 277 >> CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_SHIFT; 278 pixels = desc & CCS_FRAME_FORMAT_DESCRIPTOR_PIXELS_MASK; 279 } else if (fmt_model_type 280 == CCS_FRAME_FORMAT_MODEL_TYPE_4_BYTE) { 281 desc = CCS_LIM_AT(sensor, FRAME_FORMAT_DESCRIPTOR_4, i); 282 283 pixelcode = 284 (desc 285 & CCS_FRAME_FORMAT_DESCRIPTOR_4_PCODE_MASK) 286 >> CCS_FRAME_FORMAT_DESCRIPTOR_4_PCODE_SHIFT; 287 pixels = desc & 288 CCS_FRAME_FORMAT_DESCRIPTOR_4_PIXELS_MASK; 289 } else { 290 dev_dbg(&client->dev, 291 "invalid frame format model type %u\n", 292 fmt_model_type); 293 return -EINVAL; 294 } 295 296 if (i < ncol_desc) 297 which = "columns"; 298 else 299 which = "rows"; 300 301 switch (pixelcode) { 302 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_EMBEDDED: 303 what = "embedded"; 304 break; 305 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_DUMMY_PIXEL: 306 what = "dummy"; 307 break; 308 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_BLACK_PIXEL: 309 what = "black"; 310 break; 311 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_DARK_PIXEL: 312 what = "dark"; 313 break; 314 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL: 315 what = "visible"; 316 break; 317 default: 318 what = "invalid"; 319 break; 320 } 321 322 dev_dbg(&client->dev, 323 "%s pixels: %u %s (pixelcode %u)\n", 324 what, pixels, which, pixelcode); 325 326 if (i < ncol_desc) { 327 if (pixelcode == 328 CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL) 329 sensor->visible_pixel_start = pixel_count; 330 pixel_count += pixels; 331 continue; 332 } 333 334 /* Handle row descriptors */ 335 switch (pixelcode) { 336 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_EMBEDDED: 337 if (sensor->embedded_end) 338 break; 339 sensor->embedded_start = line_count; 340 sensor->embedded_end = line_count + pixels; 341 break; 342 case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL: 343 sensor->image_start = line_count; 344 break; 345 } 346 line_count += pixels; 347 } 348 349 if (sensor->embedded_end > sensor->image_start) { 350 dev_dbg(&client->dev, 351 "adjusting image start line to %u (was %u)\n", 352 sensor->embedded_end, sensor->image_start); 353 sensor->image_start = sensor->embedded_end; 354 } 355 356 dev_dbg(&client->dev, "embedded data from lines %u to %u\n", 357 sensor->embedded_start, sensor->embedded_end); 358 dev_dbg(&client->dev, "image data starts at line %u\n", 359 sensor->image_start); 360 361 return 0; 362 } 363 364 static int ccs_pll_configure(struct ccs_sensor *sensor) 365 { 366 struct ccs_pll *pll = &sensor->pll; 367 int rval; 368 369 rval = ccs_write(sensor, VT_PIX_CLK_DIV, pll->vt_bk.pix_clk_div); 370 if (rval < 0) 371 return rval; 372 373 rval = ccs_write(sensor, VT_SYS_CLK_DIV, pll->vt_bk.sys_clk_div); 374 if (rval < 0) 375 return rval; 376 377 rval = ccs_write(sensor, PRE_PLL_CLK_DIV, pll->vt_fr.pre_pll_clk_div); 378 if (rval < 0) 379 return rval; 380 381 rval = ccs_write(sensor, PLL_MULTIPLIER, pll->vt_fr.pll_multiplier); 382 if (rval < 0) 383 return rval; 384 385 if (!(CCS_LIM(sensor, PHY_CTRL_CAPABILITY) & 386 CCS_PHY_CTRL_CAPABILITY_AUTO_PHY_CTL)) { 387 /* Lane op clock ratio does not apply here. */ 388 rval = ccs_write(sensor, REQUESTED_LINK_RATE, 389 DIV_ROUND_UP(pll->op_bk.sys_clk_freq_hz, 390 1000000 / 256 / 256) * 391 (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ? 392 sensor->pll.csi2.lanes : 1) << 393 (pll->flags & CCS_PLL_FLAG_OP_SYS_DDR ? 394 1 : 0)); 395 if (rval < 0) 396 return rval; 397 } 398 399 if (sensor->pll.flags & CCS_PLL_FLAG_NO_OP_CLOCKS) 400 return 0; 401 402 rval = ccs_write(sensor, OP_PIX_CLK_DIV, pll->op_bk.pix_clk_div); 403 if (rval < 0) 404 return rval; 405 406 rval = ccs_write(sensor, OP_SYS_CLK_DIV, pll->op_bk.sys_clk_div); 407 if (rval < 0) 408 return rval; 409 410 if (!(pll->flags & CCS_PLL_FLAG_DUAL_PLL)) 411 return 0; 412 413 rval = ccs_write(sensor, PLL_MODE, CCS_PLL_MODE_DUAL); 414 if (rval < 0) 415 return rval; 416 417 rval = ccs_write(sensor, OP_PRE_PLL_CLK_DIV, 418 pll->op_fr.pre_pll_clk_div); 419 if (rval < 0) 420 return rval; 421 422 return ccs_write(sensor, OP_PLL_MULTIPLIER, pll->op_fr.pll_multiplier); 423 } 424 425 static int ccs_pll_try(struct ccs_sensor *sensor, struct ccs_pll *pll) 426 { 427 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 428 struct ccs_pll_limits lim = { 429 .vt_fr = { 430 .min_pre_pll_clk_div = CCS_LIM(sensor, MIN_PRE_PLL_CLK_DIV), 431 .max_pre_pll_clk_div = CCS_LIM(sensor, MAX_PRE_PLL_CLK_DIV), 432 .min_pll_ip_clk_freq_hz = CCS_LIM(sensor, MIN_PLL_IP_CLK_FREQ_MHZ), 433 .max_pll_ip_clk_freq_hz = CCS_LIM(sensor, MAX_PLL_IP_CLK_FREQ_MHZ), 434 .min_pll_multiplier = CCS_LIM(sensor, MIN_PLL_MULTIPLIER), 435 .max_pll_multiplier = CCS_LIM(sensor, MAX_PLL_MULTIPLIER), 436 .min_pll_op_clk_freq_hz = CCS_LIM(sensor, MIN_PLL_OP_CLK_FREQ_MHZ), 437 .max_pll_op_clk_freq_hz = CCS_LIM(sensor, MAX_PLL_OP_CLK_FREQ_MHZ), 438 }, 439 .op_fr = { 440 .min_pre_pll_clk_div = CCS_LIM(sensor, MIN_OP_PRE_PLL_CLK_DIV), 441 .max_pre_pll_clk_div = CCS_LIM(sensor, MAX_OP_PRE_PLL_CLK_DIV), 442 .min_pll_ip_clk_freq_hz = CCS_LIM(sensor, MIN_OP_PLL_IP_CLK_FREQ_MHZ), 443 .max_pll_ip_clk_freq_hz = CCS_LIM(sensor, MAX_OP_PLL_IP_CLK_FREQ_MHZ), 444 .min_pll_multiplier = CCS_LIM(sensor, MIN_OP_PLL_MULTIPLIER), 445 .max_pll_multiplier = CCS_LIM(sensor, MAX_OP_PLL_MULTIPLIER), 446 .min_pll_op_clk_freq_hz = CCS_LIM(sensor, MIN_OP_PLL_OP_CLK_FREQ_MHZ), 447 .max_pll_op_clk_freq_hz = CCS_LIM(sensor, MAX_OP_PLL_OP_CLK_FREQ_MHZ), 448 }, 449 .op_bk = { 450 .min_sys_clk_div = CCS_LIM(sensor, MIN_OP_SYS_CLK_DIV), 451 .max_sys_clk_div = CCS_LIM(sensor, MAX_OP_SYS_CLK_DIV), 452 .min_pix_clk_div = CCS_LIM(sensor, MIN_OP_PIX_CLK_DIV), 453 .max_pix_clk_div = CCS_LIM(sensor, MAX_OP_PIX_CLK_DIV), 454 .min_sys_clk_freq_hz = CCS_LIM(sensor, MIN_OP_SYS_CLK_FREQ_MHZ), 455 .max_sys_clk_freq_hz = CCS_LIM(sensor, MAX_OP_SYS_CLK_FREQ_MHZ), 456 .min_pix_clk_freq_hz = CCS_LIM(sensor, MIN_OP_PIX_CLK_FREQ_MHZ), 457 .max_pix_clk_freq_hz = CCS_LIM(sensor, MAX_OP_PIX_CLK_FREQ_MHZ), 458 }, 459 .vt_bk = { 460 .min_sys_clk_div = CCS_LIM(sensor, MIN_VT_SYS_CLK_DIV), 461 .max_sys_clk_div = CCS_LIM(sensor, MAX_VT_SYS_CLK_DIV), 462 .min_pix_clk_div = CCS_LIM(sensor, MIN_VT_PIX_CLK_DIV), 463 .max_pix_clk_div = CCS_LIM(sensor, MAX_VT_PIX_CLK_DIV), 464 .min_sys_clk_freq_hz = CCS_LIM(sensor, MIN_VT_SYS_CLK_FREQ_MHZ), 465 .max_sys_clk_freq_hz = CCS_LIM(sensor, MAX_VT_SYS_CLK_FREQ_MHZ), 466 .min_pix_clk_freq_hz = CCS_LIM(sensor, MIN_VT_PIX_CLK_FREQ_MHZ), 467 .max_pix_clk_freq_hz = CCS_LIM(sensor, MAX_VT_PIX_CLK_FREQ_MHZ), 468 }, 469 .min_line_length_pck_bin = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK_BIN), 470 .min_line_length_pck = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK), 471 }; 472 473 return ccs_pll_calculate(&client->dev, &lim, pll); 474 } 475 476 static int ccs_pll_update(struct ccs_sensor *sensor) 477 { 478 struct ccs_pll *pll = &sensor->pll; 479 int rval; 480 481 pll->binning_horizontal = sensor->binning_horizontal; 482 pll->binning_vertical = sensor->binning_vertical; 483 pll->link_freq = 484 sensor->link_freq->qmenu_int[sensor->link_freq->val]; 485 pll->scale_m = sensor->scale_m; 486 pll->bits_per_pixel = sensor->csi_format->compressed; 487 488 rval = ccs_pll_try(sensor, pll); 489 if (rval < 0) 490 return rval; 491 492 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_parray, 493 pll->pixel_rate_pixel_array); 494 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_csi, pll->pixel_rate_csi); 495 496 return 0; 497 } 498 499 500 /* 501 * 502 * V4L2 Controls handling 503 * 504 */ 505 506 static void __ccs_update_exposure_limits(struct ccs_sensor *sensor) 507 { 508 struct v4l2_ctrl *ctrl = sensor->exposure; 509 int max; 510 511 max = sensor->pixel_array->crop[CCS_PA_PAD_SRC].height 512 + sensor->vblank->val 513 - CCS_LIM(sensor, COARSE_INTEGRATION_TIME_MAX_MARGIN); 514 515 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, ctrl->step, max); 516 } 517 518 /* 519 * Order matters. 520 * 521 * 1. Bits-per-pixel, descending. 522 * 2. Bits-per-pixel compressed, descending. 523 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel 524 * orders must be defined. 525 */ 526 static const struct ccs_csi_data_format ccs_csi_data_formats[] = { 527 { MEDIA_BUS_FMT_SGRBG16_1X16, 16, 16, CCS_PIXEL_ORDER_GRBG, }, 528 { MEDIA_BUS_FMT_SRGGB16_1X16, 16, 16, CCS_PIXEL_ORDER_RGGB, }, 529 { MEDIA_BUS_FMT_SBGGR16_1X16, 16, 16, CCS_PIXEL_ORDER_BGGR, }, 530 { MEDIA_BUS_FMT_SGBRG16_1X16, 16, 16, CCS_PIXEL_ORDER_GBRG, }, 531 { MEDIA_BUS_FMT_SGRBG14_1X14, 14, 14, CCS_PIXEL_ORDER_GRBG, }, 532 { MEDIA_BUS_FMT_SRGGB14_1X14, 14, 14, CCS_PIXEL_ORDER_RGGB, }, 533 { MEDIA_BUS_FMT_SBGGR14_1X14, 14, 14, CCS_PIXEL_ORDER_BGGR, }, 534 { MEDIA_BUS_FMT_SGBRG14_1X14, 14, 14, CCS_PIXEL_ORDER_GBRG, }, 535 { MEDIA_BUS_FMT_SGRBG12_1X12, 12, 12, CCS_PIXEL_ORDER_GRBG, }, 536 { MEDIA_BUS_FMT_SRGGB12_1X12, 12, 12, CCS_PIXEL_ORDER_RGGB, }, 537 { MEDIA_BUS_FMT_SBGGR12_1X12, 12, 12, CCS_PIXEL_ORDER_BGGR, }, 538 { MEDIA_BUS_FMT_SGBRG12_1X12, 12, 12, CCS_PIXEL_ORDER_GBRG, }, 539 { MEDIA_BUS_FMT_SGRBG10_1X10, 10, 10, CCS_PIXEL_ORDER_GRBG, }, 540 { MEDIA_BUS_FMT_SRGGB10_1X10, 10, 10, CCS_PIXEL_ORDER_RGGB, }, 541 { MEDIA_BUS_FMT_SBGGR10_1X10, 10, 10, CCS_PIXEL_ORDER_BGGR, }, 542 { MEDIA_BUS_FMT_SGBRG10_1X10, 10, 10, CCS_PIXEL_ORDER_GBRG, }, 543 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_GRBG, }, 544 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_RGGB, }, 545 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_BGGR, }, 546 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_GBRG, }, 547 { MEDIA_BUS_FMT_SGRBG8_1X8, 8, 8, CCS_PIXEL_ORDER_GRBG, }, 548 { MEDIA_BUS_FMT_SRGGB8_1X8, 8, 8, CCS_PIXEL_ORDER_RGGB, }, 549 { MEDIA_BUS_FMT_SBGGR8_1X8, 8, 8, CCS_PIXEL_ORDER_BGGR, }, 550 { MEDIA_BUS_FMT_SGBRG8_1X8, 8, 8, CCS_PIXEL_ORDER_GBRG, }, 551 }; 552 553 static const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" }; 554 555 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \ 556 - (unsigned long)ccs_csi_data_formats) \ 557 / sizeof(*ccs_csi_data_formats)) 558 559 static u32 ccs_pixel_order(struct ccs_sensor *sensor) 560 { 561 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 562 int flip = 0; 563 564 if (sensor->hflip) { 565 if (sensor->hflip->val) 566 flip |= CCS_IMAGE_ORIENTATION_HORIZONTAL_MIRROR; 567 568 if (sensor->vflip->val) 569 flip |= CCS_IMAGE_ORIENTATION_VERTICAL_FLIP; 570 } 571 572 dev_dbg(&client->dev, "flip %u\n", flip); 573 return sensor->default_pixel_order ^ flip; 574 } 575 576 static void ccs_update_mbus_formats(struct ccs_sensor *sensor) 577 { 578 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 579 unsigned int csi_format_idx = 580 to_csi_format_idx(sensor->csi_format) & ~3; 581 unsigned int internal_csi_format_idx = 582 to_csi_format_idx(sensor->internal_csi_format) & ~3; 583 unsigned int pixel_order = ccs_pixel_order(sensor); 584 585 if (WARN_ON_ONCE(max(internal_csi_format_idx, csi_format_idx) + 586 pixel_order >= ARRAY_SIZE(ccs_csi_data_formats))) 587 return; 588 589 sensor->mbus_frame_fmts = 590 sensor->default_mbus_frame_fmts << pixel_order; 591 sensor->csi_format = 592 &ccs_csi_data_formats[csi_format_idx + pixel_order]; 593 sensor->internal_csi_format = 594 &ccs_csi_data_formats[internal_csi_format_idx 595 + pixel_order]; 596 597 dev_dbg(&client->dev, "new pixel order %s\n", 598 pixel_order_str[pixel_order]); 599 } 600 601 static const char * const ccs_test_patterns[] = { 602 "Disabled", 603 "Solid Colour", 604 "Eight Vertical Colour Bars", 605 "Colour Bars With Fade to Grey", 606 "Pseudorandom Sequence (PN9)", 607 }; 608 609 static int ccs_set_ctrl(struct v4l2_ctrl *ctrl) 610 { 611 struct ccs_sensor *sensor = 612 container_of(ctrl->handler, struct ccs_subdev, ctrl_handler) 613 ->sensor; 614 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 615 int pm_status; 616 u32 orient = 0; 617 unsigned int i; 618 int exposure; 619 int rval; 620 621 switch (ctrl->id) { 622 case V4L2_CID_HFLIP: 623 case V4L2_CID_VFLIP: 624 if (sensor->streaming) 625 return -EBUSY; 626 627 if (sensor->hflip->val) 628 orient |= CCS_IMAGE_ORIENTATION_HORIZONTAL_MIRROR; 629 630 if (sensor->vflip->val) 631 orient |= CCS_IMAGE_ORIENTATION_VERTICAL_FLIP; 632 633 ccs_update_mbus_formats(sensor); 634 635 break; 636 case V4L2_CID_VBLANK: 637 exposure = sensor->exposure->val; 638 639 __ccs_update_exposure_limits(sensor); 640 641 if (exposure > sensor->exposure->maximum) { 642 sensor->exposure->val = sensor->exposure->maximum; 643 rval = ccs_set_ctrl(sensor->exposure); 644 if (rval < 0) 645 return rval; 646 } 647 648 break; 649 case V4L2_CID_LINK_FREQ: 650 if (sensor->streaming) 651 return -EBUSY; 652 653 rval = ccs_pll_update(sensor); 654 if (rval) 655 return rval; 656 657 return 0; 658 case V4L2_CID_TEST_PATTERN: 659 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) 660 v4l2_ctrl_activate( 661 sensor->test_data[i], 662 ctrl->val == 663 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR); 664 665 break; 666 } 667 668 pm_status = pm_runtime_get_if_active(&client->dev, true); 669 if (!pm_status) 670 return 0; 671 672 switch (ctrl->id) { 673 case V4L2_CID_ANALOGUE_GAIN: 674 rval = ccs_write(sensor, ANALOG_GAIN_CODE_GLOBAL, ctrl->val); 675 676 break; 677 678 case V4L2_CID_CCS_ANALOGUE_LINEAR_GAIN: 679 rval = ccs_write(sensor, ANALOG_LINEAR_GAIN_GLOBAL, ctrl->val); 680 681 break; 682 683 case V4L2_CID_CCS_ANALOGUE_EXPONENTIAL_GAIN: 684 rval = ccs_write(sensor, ANALOG_EXPONENTIAL_GAIN_GLOBAL, 685 ctrl->val); 686 687 break; 688 689 case V4L2_CID_DIGITAL_GAIN: 690 if (CCS_LIM(sensor, DIGITAL_GAIN_CAPABILITY) == 691 CCS_DIGITAL_GAIN_CAPABILITY_GLOBAL) { 692 rval = ccs_write(sensor, DIGITAL_GAIN_GLOBAL, 693 ctrl->val); 694 break; 695 } 696 697 rval = ccs_write_addr(sensor, 698 SMIAPP_REG_U16_DIGITAL_GAIN_GREENR, 699 ctrl->val); 700 if (rval) 701 break; 702 703 rval = ccs_write_addr(sensor, 704 SMIAPP_REG_U16_DIGITAL_GAIN_RED, 705 ctrl->val); 706 if (rval) 707 break; 708 709 rval = ccs_write_addr(sensor, 710 SMIAPP_REG_U16_DIGITAL_GAIN_BLUE, 711 ctrl->val); 712 if (rval) 713 break; 714 715 rval = ccs_write_addr(sensor, 716 SMIAPP_REG_U16_DIGITAL_GAIN_GREENB, 717 ctrl->val); 718 719 break; 720 case V4L2_CID_EXPOSURE: 721 rval = ccs_write(sensor, COARSE_INTEGRATION_TIME, ctrl->val); 722 723 break; 724 case V4L2_CID_HFLIP: 725 case V4L2_CID_VFLIP: 726 rval = ccs_write(sensor, IMAGE_ORIENTATION, orient); 727 728 break; 729 case V4L2_CID_VBLANK: 730 rval = ccs_write(sensor, FRAME_LENGTH_LINES, 731 sensor->pixel_array->crop[ 732 CCS_PA_PAD_SRC].height 733 + ctrl->val); 734 735 break; 736 case V4L2_CID_HBLANK: 737 rval = ccs_write(sensor, LINE_LENGTH_PCK, 738 sensor->pixel_array->crop[CCS_PA_PAD_SRC].width 739 + ctrl->val); 740 741 break; 742 case V4L2_CID_TEST_PATTERN: 743 rval = ccs_write(sensor, TEST_PATTERN_MODE, ctrl->val); 744 745 break; 746 case V4L2_CID_TEST_PATTERN_RED: 747 rval = ccs_write(sensor, TEST_DATA_RED, ctrl->val); 748 749 break; 750 case V4L2_CID_TEST_PATTERN_GREENR: 751 rval = ccs_write(sensor, TEST_DATA_GREENR, ctrl->val); 752 753 break; 754 case V4L2_CID_TEST_PATTERN_BLUE: 755 rval = ccs_write(sensor, TEST_DATA_BLUE, ctrl->val); 756 757 break; 758 case V4L2_CID_TEST_PATTERN_GREENB: 759 rval = ccs_write(sensor, TEST_DATA_GREENB, ctrl->val); 760 761 break; 762 case V4L2_CID_CCS_SHADING_CORRECTION: 763 rval = ccs_write(sensor, SHADING_CORRECTION_EN, 764 ctrl->val ? CCS_SHADING_CORRECTION_EN_ENABLE : 765 0); 766 767 if (!rval && sensor->luminance_level) 768 v4l2_ctrl_activate(sensor->luminance_level, ctrl->val); 769 770 break; 771 case V4L2_CID_CCS_LUMINANCE_CORRECTION_LEVEL: 772 rval = ccs_write(sensor, LUMINANCE_CORRECTION_LEVEL, ctrl->val); 773 774 break; 775 case V4L2_CID_PIXEL_RATE: 776 /* For v4l2_ctrl_s_ctrl_int64() used internally. */ 777 rval = 0; 778 779 break; 780 default: 781 rval = -EINVAL; 782 } 783 784 if (pm_status > 0) { 785 pm_runtime_mark_last_busy(&client->dev); 786 pm_runtime_put_autosuspend(&client->dev); 787 } 788 789 return rval; 790 } 791 792 static const struct v4l2_ctrl_ops ccs_ctrl_ops = { 793 .s_ctrl = ccs_set_ctrl, 794 }; 795 796 static int ccs_init_controls(struct ccs_sensor *sensor) 797 { 798 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 799 struct v4l2_fwnode_device_properties props; 800 int rval; 801 802 rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 19); 803 if (rval) 804 return rval; 805 806 sensor->pixel_array->ctrl_handler.lock = &sensor->mutex; 807 808 rval = v4l2_fwnode_device_parse(&client->dev, &props); 809 if (rval) 810 return rval; 811 812 rval = v4l2_ctrl_new_fwnode_properties(&sensor->pixel_array->ctrl_handler, 813 &ccs_ctrl_ops, &props); 814 if (rval) 815 return rval; 816 817 switch (CCS_LIM(sensor, ANALOG_GAIN_CAPABILITY)) { 818 case CCS_ANALOG_GAIN_CAPABILITY_GLOBAL: { 819 struct { 820 const char *name; 821 u32 id; 822 s32 value; 823 } const gain_ctrls[] = { 824 { "Analogue Gain m0", V4L2_CID_CCS_ANALOGUE_GAIN_M0, 825 CCS_LIM(sensor, ANALOG_GAIN_M0), }, 826 { "Analogue Gain c0", V4L2_CID_CCS_ANALOGUE_GAIN_C0, 827 CCS_LIM(sensor, ANALOG_GAIN_C0), }, 828 { "Analogue Gain m1", V4L2_CID_CCS_ANALOGUE_GAIN_M1, 829 CCS_LIM(sensor, ANALOG_GAIN_M1), }, 830 { "Analogue Gain c1", V4L2_CID_CCS_ANALOGUE_GAIN_C1, 831 CCS_LIM(sensor, ANALOG_GAIN_C1), }, 832 }; 833 struct v4l2_ctrl_config ctrl_cfg = { 834 .type = V4L2_CTRL_TYPE_INTEGER, 835 .ops = &ccs_ctrl_ops, 836 .flags = V4L2_CTRL_FLAG_READ_ONLY, 837 .step = 1, 838 }; 839 unsigned int i; 840 841 for (i = 0; i < ARRAY_SIZE(gain_ctrls); i++) { 842 ctrl_cfg.name = gain_ctrls[i].name; 843 ctrl_cfg.id = gain_ctrls[i].id; 844 ctrl_cfg.min = ctrl_cfg.max = ctrl_cfg.def = 845 gain_ctrls[i].value; 846 847 v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler, 848 &ctrl_cfg, NULL); 849 } 850 851 v4l2_ctrl_new_std(&sensor->pixel_array->ctrl_handler, 852 &ccs_ctrl_ops, V4L2_CID_ANALOGUE_GAIN, 853 CCS_LIM(sensor, ANALOG_GAIN_CODE_MIN), 854 CCS_LIM(sensor, ANALOG_GAIN_CODE_MAX), 855 max(CCS_LIM(sensor, ANALOG_GAIN_CODE_STEP), 856 1U), 857 CCS_LIM(sensor, ANALOG_GAIN_CODE_MIN)); 858 } 859 break; 860 861 case CCS_ANALOG_GAIN_CAPABILITY_ALTERNATE_GLOBAL: { 862 struct { 863 const char *name; 864 u32 id; 865 u16 min, max, step; 866 } const gain_ctrls[] = { 867 { 868 "Analogue Linear Gain", 869 V4L2_CID_CCS_ANALOGUE_LINEAR_GAIN, 870 CCS_LIM(sensor, ANALOG_LINEAR_GAIN_MIN), 871 CCS_LIM(sensor, ANALOG_LINEAR_GAIN_MAX), 872 max(CCS_LIM(sensor, 873 ANALOG_LINEAR_GAIN_STEP_SIZE), 874 1U), 875 }, 876 { 877 "Analogue Exponential Gain", 878 V4L2_CID_CCS_ANALOGUE_EXPONENTIAL_GAIN, 879 CCS_LIM(sensor, ANALOG_EXPONENTIAL_GAIN_MIN), 880 CCS_LIM(sensor, ANALOG_EXPONENTIAL_GAIN_MAX), 881 max(CCS_LIM(sensor, 882 ANALOG_EXPONENTIAL_GAIN_STEP_SIZE), 883 1U), 884 }, 885 }; 886 struct v4l2_ctrl_config ctrl_cfg = { 887 .type = V4L2_CTRL_TYPE_INTEGER, 888 .ops = &ccs_ctrl_ops, 889 }; 890 unsigned int i; 891 892 for (i = 0; i < ARRAY_SIZE(gain_ctrls); i++) { 893 ctrl_cfg.name = gain_ctrls[i].name; 894 ctrl_cfg.min = ctrl_cfg.def = gain_ctrls[i].min; 895 ctrl_cfg.max = gain_ctrls[i].max; 896 ctrl_cfg.step = gain_ctrls[i].step; 897 ctrl_cfg.id = gain_ctrls[i].id; 898 899 v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler, 900 &ctrl_cfg, NULL); 901 } 902 } 903 } 904 905 if (CCS_LIM(sensor, SHADING_CORRECTION_CAPABILITY) & 906 (CCS_SHADING_CORRECTION_CAPABILITY_COLOR_SHADING | 907 CCS_SHADING_CORRECTION_CAPABILITY_LUMINANCE_CORRECTION)) { 908 const struct v4l2_ctrl_config ctrl_cfg = { 909 .name = "Shading Correction", 910 .type = V4L2_CTRL_TYPE_BOOLEAN, 911 .id = V4L2_CID_CCS_SHADING_CORRECTION, 912 .ops = &ccs_ctrl_ops, 913 .max = 1, 914 .step = 1, 915 }; 916 917 v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler, 918 &ctrl_cfg, NULL); 919 } 920 921 if (CCS_LIM(sensor, SHADING_CORRECTION_CAPABILITY) & 922 CCS_SHADING_CORRECTION_CAPABILITY_LUMINANCE_CORRECTION) { 923 const struct v4l2_ctrl_config ctrl_cfg = { 924 .name = "Luminance Correction Level", 925 .type = V4L2_CTRL_TYPE_BOOLEAN, 926 .id = V4L2_CID_CCS_LUMINANCE_CORRECTION_LEVEL, 927 .ops = &ccs_ctrl_ops, 928 .max = 255, 929 .step = 1, 930 .def = 128, 931 }; 932 933 sensor->luminance_level = 934 v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler, 935 &ctrl_cfg, NULL); 936 } 937 938 if (CCS_LIM(sensor, DIGITAL_GAIN_CAPABILITY) == 939 CCS_DIGITAL_GAIN_CAPABILITY_GLOBAL || 940 CCS_LIM(sensor, DIGITAL_GAIN_CAPABILITY) == 941 SMIAPP_DIGITAL_GAIN_CAPABILITY_PER_CHANNEL) 942 v4l2_ctrl_new_std(&sensor->pixel_array->ctrl_handler, 943 &ccs_ctrl_ops, V4L2_CID_DIGITAL_GAIN, 944 CCS_LIM(sensor, DIGITAL_GAIN_MIN), 945 CCS_LIM(sensor, DIGITAL_GAIN_MAX), 946 max(CCS_LIM(sensor, DIGITAL_GAIN_STEP_SIZE), 947 1U), 948 0x100); 949 950 /* Exposure limits will be updated soon, use just something here. */ 951 sensor->exposure = v4l2_ctrl_new_std( 952 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops, 953 V4L2_CID_EXPOSURE, 0, 0, 1, 0); 954 955 sensor->hflip = v4l2_ctrl_new_std( 956 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops, 957 V4L2_CID_HFLIP, 0, 1, 1, 0); 958 sensor->vflip = v4l2_ctrl_new_std( 959 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops, 960 V4L2_CID_VFLIP, 0, 1, 1, 0); 961 962 sensor->vblank = v4l2_ctrl_new_std( 963 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops, 964 V4L2_CID_VBLANK, 0, 1, 1, 0); 965 966 if (sensor->vblank) 967 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE; 968 969 sensor->hblank = v4l2_ctrl_new_std( 970 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops, 971 V4L2_CID_HBLANK, 0, 1, 1, 0); 972 973 if (sensor->hblank) 974 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE; 975 976 sensor->pixel_rate_parray = v4l2_ctrl_new_std( 977 &sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops, 978 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1); 979 980 v4l2_ctrl_new_std_menu_items(&sensor->pixel_array->ctrl_handler, 981 &ccs_ctrl_ops, V4L2_CID_TEST_PATTERN, 982 ARRAY_SIZE(ccs_test_patterns) - 1, 983 0, 0, ccs_test_patterns); 984 985 if (sensor->pixel_array->ctrl_handler.error) { 986 dev_err(&client->dev, 987 "pixel array controls initialization failed (%d)\n", 988 sensor->pixel_array->ctrl_handler.error); 989 return sensor->pixel_array->ctrl_handler.error; 990 } 991 992 sensor->pixel_array->sd.ctrl_handler = 993 &sensor->pixel_array->ctrl_handler; 994 995 v4l2_ctrl_cluster(2, &sensor->hflip); 996 997 rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0); 998 if (rval) 999 return rval; 1000 1001 sensor->src->ctrl_handler.lock = &sensor->mutex; 1002 1003 sensor->pixel_rate_csi = v4l2_ctrl_new_std( 1004 &sensor->src->ctrl_handler, &ccs_ctrl_ops, 1005 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1); 1006 1007 if (sensor->src->ctrl_handler.error) { 1008 dev_err(&client->dev, 1009 "src controls initialization failed (%d)\n", 1010 sensor->src->ctrl_handler.error); 1011 return sensor->src->ctrl_handler.error; 1012 } 1013 1014 sensor->src->sd.ctrl_handler = &sensor->src->ctrl_handler; 1015 1016 return 0; 1017 } 1018 1019 /* 1020 * For controls that require information on available media bus codes 1021 * and linke frequencies. 1022 */ 1023 static int ccs_init_late_controls(struct ccs_sensor *sensor) 1024 { 1025 unsigned long *valid_link_freqs = &sensor->valid_link_freqs[ 1026 sensor->csi_format->compressed - sensor->compressed_min_bpp]; 1027 unsigned int i; 1028 1029 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) { 1030 int max_value = (1 << sensor->csi_format->width) - 1; 1031 1032 sensor->test_data[i] = v4l2_ctrl_new_std( 1033 &sensor->pixel_array->ctrl_handler, 1034 &ccs_ctrl_ops, V4L2_CID_TEST_PATTERN_RED + i, 1035 0, max_value, 1, max_value); 1036 } 1037 1038 sensor->link_freq = v4l2_ctrl_new_int_menu( 1039 &sensor->src->ctrl_handler, &ccs_ctrl_ops, 1040 V4L2_CID_LINK_FREQ, __fls(*valid_link_freqs), 1041 __ffs(*valid_link_freqs), sensor->hwcfg.op_sys_clock); 1042 1043 return sensor->src->ctrl_handler.error; 1044 } 1045 1046 static void ccs_free_controls(struct ccs_sensor *sensor) 1047 { 1048 unsigned int i; 1049 1050 for (i = 0; i < sensor->ssds_used; i++) 1051 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler); 1052 } 1053 1054 static int ccs_get_mbus_formats(struct ccs_sensor *sensor) 1055 { 1056 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 1057 struct ccs_pll *pll = &sensor->pll; 1058 u8 compressed_max_bpp = 0; 1059 unsigned int type, n; 1060 unsigned int i, pixel_order; 1061 int rval; 1062 1063 type = CCS_LIM(sensor, DATA_FORMAT_MODEL_TYPE); 1064 1065 dev_dbg(&client->dev, "data_format_model_type %u\n", type); 1066 1067 rval = ccs_read(sensor, PIXEL_ORDER, &pixel_order); 1068 if (rval) 1069 return rval; 1070 1071 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) { 1072 dev_dbg(&client->dev, "bad pixel order %u\n", pixel_order); 1073 return -EINVAL; 1074 } 1075 1076 dev_dbg(&client->dev, "pixel order %u (%s)\n", pixel_order, 1077 pixel_order_str[pixel_order]); 1078 1079 switch (type) { 1080 case CCS_DATA_FORMAT_MODEL_TYPE_NORMAL: 1081 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N; 1082 break; 1083 case CCS_DATA_FORMAT_MODEL_TYPE_EXTENDED: 1084 n = CCS_LIM_DATA_FORMAT_DESCRIPTOR_MAX_N + 1; 1085 break; 1086 default: 1087 return -EINVAL; 1088 } 1089 1090 sensor->default_pixel_order = pixel_order; 1091 sensor->mbus_frame_fmts = 0; 1092 1093 for (i = 0; i < n; i++) { 1094 unsigned int fmt, j; 1095 1096 fmt = CCS_LIM_AT(sensor, DATA_FORMAT_DESCRIPTOR, i); 1097 1098 dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n", 1099 i, fmt >> 8, (u8)fmt); 1100 1101 for (j = 0; j < ARRAY_SIZE(ccs_csi_data_formats); j++) { 1102 const struct ccs_csi_data_format *f = 1103 &ccs_csi_data_formats[j]; 1104 1105 if (f->pixel_order != CCS_PIXEL_ORDER_GRBG) 1106 continue; 1107 1108 if (f->width != fmt >> 1109 CCS_DATA_FORMAT_DESCRIPTOR_UNCOMPRESSED_SHIFT || 1110 f->compressed != 1111 (fmt & CCS_DATA_FORMAT_DESCRIPTOR_COMPRESSED_MASK)) 1112 continue; 1113 1114 dev_dbg(&client->dev, "jolly good! %u\n", j); 1115 1116 sensor->default_mbus_frame_fmts |= 1 << j; 1117 } 1118 } 1119 1120 /* Figure out which BPP values can be used with which formats. */ 1121 pll->binning_horizontal = 1; 1122 pll->binning_vertical = 1; 1123 pll->scale_m = sensor->scale_m; 1124 1125 for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) { 1126 sensor->compressed_min_bpp = 1127 min(ccs_csi_data_formats[i].compressed, 1128 sensor->compressed_min_bpp); 1129 compressed_max_bpp = 1130 max(ccs_csi_data_formats[i].compressed, 1131 compressed_max_bpp); 1132 } 1133 1134 sensor->valid_link_freqs = devm_kcalloc( 1135 &client->dev, 1136 compressed_max_bpp - sensor->compressed_min_bpp + 1, 1137 sizeof(*sensor->valid_link_freqs), GFP_KERNEL); 1138 if (!sensor->valid_link_freqs) 1139 return -ENOMEM; 1140 1141 for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) { 1142 const struct ccs_csi_data_format *f = 1143 &ccs_csi_data_formats[i]; 1144 unsigned long *valid_link_freqs = 1145 &sensor->valid_link_freqs[ 1146 f->compressed - sensor->compressed_min_bpp]; 1147 unsigned int j; 1148 1149 if (!(sensor->default_mbus_frame_fmts & 1 << i)) 1150 continue; 1151 1152 pll->bits_per_pixel = f->compressed; 1153 1154 for (j = 0; sensor->hwcfg.op_sys_clock[j]; j++) { 1155 pll->link_freq = sensor->hwcfg.op_sys_clock[j]; 1156 1157 rval = ccs_pll_try(sensor, pll); 1158 dev_dbg(&client->dev, "link freq %u Hz, bpp %u %s\n", 1159 pll->link_freq, pll->bits_per_pixel, 1160 rval ? "not ok" : "ok"); 1161 if (rval) 1162 continue; 1163 1164 set_bit(j, valid_link_freqs); 1165 } 1166 1167 if (!*valid_link_freqs) { 1168 dev_info(&client->dev, 1169 "no valid link frequencies for %u bpp\n", 1170 f->compressed); 1171 sensor->default_mbus_frame_fmts &= ~BIT(i); 1172 continue; 1173 } 1174 1175 if (!sensor->csi_format 1176 || f->width > sensor->csi_format->width 1177 || (f->width == sensor->csi_format->width 1178 && f->compressed > sensor->csi_format->compressed)) { 1179 sensor->csi_format = f; 1180 sensor->internal_csi_format = f; 1181 } 1182 } 1183 1184 if (!sensor->csi_format) { 1185 dev_err(&client->dev, "no supported mbus code found\n"); 1186 return -EINVAL; 1187 } 1188 1189 ccs_update_mbus_formats(sensor); 1190 1191 return 0; 1192 } 1193 1194 static void ccs_update_blanking(struct ccs_sensor *sensor) 1195 { 1196 struct v4l2_ctrl *vblank = sensor->vblank; 1197 struct v4l2_ctrl *hblank = sensor->hblank; 1198 u16 min_fll, max_fll, min_llp, max_llp, min_lbp; 1199 int min, max; 1200 1201 if (sensor->binning_vertical > 1 || sensor->binning_horizontal > 1) { 1202 min_fll = CCS_LIM(sensor, MIN_FRAME_LENGTH_LINES_BIN); 1203 max_fll = CCS_LIM(sensor, MAX_FRAME_LENGTH_LINES_BIN); 1204 min_llp = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK_BIN); 1205 max_llp = CCS_LIM(sensor, MAX_LINE_LENGTH_PCK_BIN); 1206 min_lbp = CCS_LIM(sensor, MIN_LINE_BLANKING_PCK_BIN); 1207 } else { 1208 min_fll = CCS_LIM(sensor, MIN_FRAME_LENGTH_LINES); 1209 max_fll = CCS_LIM(sensor, MAX_FRAME_LENGTH_LINES); 1210 min_llp = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK); 1211 max_llp = CCS_LIM(sensor, MAX_LINE_LENGTH_PCK); 1212 min_lbp = CCS_LIM(sensor, MIN_LINE_BLANKING_PCK); 1213 } 1214 1215 min = max_t(int, 1216 CCS_LIM(sensor, MIN_FRAME_BLANKING_LINES), 1217 min_fll - sensor->pixel_array->crop[CCS_PA_PAD_SRC].height); 1218 max = max_fll - sensor->pixel_array->crop[CCS_PA_PAD_SRC].height; 1219 1220 __v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min); 1221 1222 min = max_t(int, 1223 min_llp - sensor->pixel_array->crop[CCS_PA_PAD_SRC].width, 1224 min_lbp); 1225 max = max_llp - sensor->pixel_array->crop[CCS_PA_PAD_SRC].width; 1226 1227 __v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min); 1228 1229 __ccs_update_exposure_limits(sensor); 1230 } 1231 1232 static int ccs_pll_blanking_update(struct ccs_sensor *sensor) 1233 { 1234 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 1235 int rval; 1236 1237 rval = ccs_pll_update(sensor); 1238 if (rval < 0) 1239 return rval; 1240 1241 /* Output from pixel array, including blanking */ 1242 ccs_update_blanking(sensor); 1243 1244 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val); 1245 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val); 1246 1247 dev_dbg(&client->dev, "real timeperframe\t100/%d\n", 1248 sensor->pll.pixel_rate_pixel_array / 1249 ((sensor->pixel_array->crop[CCS_PA_PAD_SRC].width 1250 + sensor->hblank->val) * 1251 (sensor->pixel_array->crop[CCS_PA_PAD_SRC].height 1252 + sensor->vblank->val) / 100)); 1253 1254 return 0; 1255 } 1256 1257 /* 1258 * 1259 * SMIA++ NVM handling 1260 * 1261 */ 1262 1263 static int ccs_read_nvm_page(struct ccs_sensor *sensor, u32 p, u8 *nvm, 1264 u8 *status) 1265 { 1266 unsigned int i; 1267 int rval; 1268 u32 s; 1269 1270 *status = 0; 1271 1272 rval = ccs_write(sensor, DATA_TRANSFER_IF_1_PAGE_SELECT, p); 1273 if (rval) 1274 return rval; 1275 1276 rval = ccs_write(sensor, DATA_TRANSFER_IF_1_CTRL, 1277 CCS_DATA_TRANSFER_IF_1_CTRL_ENABLE); 1278 if (rval) 1279 return rval; 1280 1281 rval = ccs_read(sensor, DATA_TRANSFER_IF_1_STATUS, &s); 1282 if (rval) 1283 return rval; 1284 1285 if (s & CCS_DATA_TRANSFER_IF_1_STATUS_IMPROPER_IF_USAGE) { 1286 *status = s; 1287 return -ENODATA; 1288 } 1289 1290 if (CCS_LIM(sensor, DATA_TRANSFER_IF_CAPABILITY) & 1291 CCS_DATA_TRANSFER_IF_CAPABILITY_POLLING) { 1292 for (i = 1000; i > 0; i--) { 1293 if (s & CCS_DATA_TRANSFER_IF_1_STATUS_READ_IF_READY) 1294 break; 1295 1296 rval = ccs_read(sensor, DATA_TRANSFER_IF_1_STATUS, &s); 1297 if (rval) 1298 return rval; 1299 } 1300 1301 if (!i) 1302 return -ETIMEDOUT; 1303 } 1304 1305 for (i = 0; i <= CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P; i++) { 1306 u32 v; 1307 1308 rval = ccs_read(sensor, DATA_TRANSFER_IF_1_DATA(i), &v); 1309 if (rval) 1310 return rval; 1311 1312 *nvm++ = v; 1313 } 1314 1315 return 0; 1316 } 1317 1318 static int ccs_read_nvm(struct ccs_sensor *sensor, unsigned char *nvm, 1319 size_t nvm_size) 1320 { 1321 u8 status = 0; 1322 u32 p; 1323 int rval = 0, rval2; 1324 1325 for (p = 0; p < nvm_size / (CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P + 1) 1326 && !rval; p++) { 1327 rval = ccs_read_nvm_page(sensor, p, nvm, &status); 1328 nvm += CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P + 1; 1329 } 1330 1331 if (rval == -ENODATA && 1332 status & CCS_DATA_TRANSFER_IF_1_STATUS_IMPROPER_IF_USAGE) 1333 rval = 0; 1334 1335 rval2 = ccs_write(sensor, DATA_TRANSFER_IF_1_CTRL, 0); 1336 if (rval < 0) 1337 return rval; 1338 else 1339 return rval2 ?: p * (CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P + 1); 1340 } 1341 1342 /* 1343 * 1344 * SMIA++ CCI address control 1345 * 1346 */ 1347 static int ccs_change_cci_addr(struct ccs_sensor *sensor) 1348 { 1349 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 1350 int rval; 1351 u32 val; 1352 1353 client->addr = sensor->hwcfg.i2c_addr_dfl; 1354 1355 rval = ccs_write(sensor, CCI_ADDRESS_CTRL, 1356 sensor->hwcfg.i2c_addr_alt << 1); 1357 if (rval) 1358 return rval; 1359 1360 client->addr = sensor->hwcfg.i2c_addr_alt; 1361 1362 /* verify addr change went ok */ 1363 rval = ccs_read(sensor, CCI_ADDRESS_CTRL, &val); 1364 if (rval) 1365 return rval; 1366 1367 if (val != sensor->hwcfg.i2c_addr_alt << 1) 1368 return -ENODEV; 1369 1370 return 0; 1371 } 1372 1373 /* 1374 * 1375 * SMIA++ Mode Control 1376 * 1377 */ 1378 static int ccs_setup_flash_strobe(struct ccs_sensor *sensor) 1379 { 1380 struct ccs_flash_strobe_parms *strobe_setup; 1381 unsigned int ext_freq = sensor->hwcfg.ext_clk; 1382 u32 tmp; 1383 u32 strobe_adjustment; 1384 u32 strobe_width_high_rs; 1385 int rval; 1386 1387 strobe_setup = sensor->hwcfg.strobe_setup; 1388 1389 /* 1390 * How to calculate registers related to strobe length. Please 1391 * do not change, or if you do at least know what you're 1392 * doing. :-) 1393 * 1394 * Sakari Ailus <sakari.ailus@linux.intel.com> 2010-10-25 1395 * 1396 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl 1397 * / EXTCLK freq [Hz]) * flash_strobe_adjustment 1398 * 1399 * tFlash_strobe_width_ctrl E N, [1 - 0xffff] 1400 * flash_strobe_adjustment E N, [1 - 0xff] 1401 * 1402 * The formula above is written as below to keep it on one 1403 * line: 1404 * 1405 * l / 10^6 = w / e * a 1406 * 1407 * Let's mark w * a by x: 1408 * 1409 * x = w * a 1410 * 1411 * Thus, we get: 1412 * 1413 * x = l * e / 10^6 1414 * 1415 * The strobe width must be at least as long as requested, 1416 * thus rounding upwards is needed. 1417 * 1418 * x = (l * e + 10^6 - 1) / 10^6 1419 * ----------------------------- 1420 * 1421 * Maximum possible accuracy is wanted at all times. Thus keep 1422 * a as small as possible. 1423 * 1424 * Calculate a, assuming maximum w, with rounding upwards: 1425 * 1426 * a = (x + (2^16 - 1) - 1) / (2^16 - 1) 1427 * ------------------------------------- 1428 * 1429 * Thus, we also get w, with that a, with rounding upwards: 1430 * 1431 * w = (x + a - 1) / a 1432 * ------------------- 1433 * 1434 * To get limits: 1435 * 1436 * x E [1, (2^16 - 1) * (2^8 - 1)] 1437 * 1438 * Substituting maximum x to the original formula (with rounding), 1439 * the maximum l is thus 1440 * 1441 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1 1442 * 1443 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e 1444 * -------------------------------------------------- 1445 * 1446 * flash_strobe_length must be clamped between 1 and 1447 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq. 1448 * 1449 * Then, 1450 * 1451 * flash_strobe_adjustment = ((flash_strobe_length * 1452 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1) 1453 * 1454 * tFlash_strobe_width_ctrl = ((flash_strobe_length * 1455 * EXTCLK freq + 10^6 - 1) / 10^6 + 1456 * flash_strobe_adjustment - 1) / flash_strobe_adjustment 1457 */ 1458 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) - 1459 1000000 + 1, ext_freq); 1460 strobe_setup->strobe_width_high_us = 1461 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp); 1462 1463 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq + 1464 1000000 - 1), 1000000ULL); 1465 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1); 1466 strobe_width_high_rs = (tmp + strobe_adjustment - 1) / 1467 strobe_adjustment; 1468 1469 rval = ccs_write(sensor, FLASH_MODE_RS, strobe_setup->mode); 1470 if (rval < 0) 1471 goto out; 1472 1473 rval = ccs_write(sensor, FLASH_STROBE_ADJUSTMENT, strobe_adjustment); 1474 if (rval < 0) 1475 goto out; 1476 1477 rval = ccs_write(sensor, TFLASH_STROBE_WIDTH_HIGH_RS_CTRL, 1478 strobe_width_high_rs); 1479 if (rval < 0) 1480 goto out; 1481 1482 rval = ccs_write(sensor, TFLASH_STROBE_DELAY_RS_CTRL, 1483 strobe_setup->strobe_delay); 1484 if (rval < 0) 1485 goto out; 1486 1487 rval = ccs_write(sensor, FLASH_STROBE_START_POINT, 1488 strobe_setup->stobe_start_point); 1489 if (rval < 0) 1490 goto out; 1491 1492 rval = ccs_write(sensor, FLASH_TRIGGER_RS, strobe_setup->trigger); 1493 1494 out: 1495 sensor->hwcfg.strobe_setup->trigger = 0; 1496 1497 return rval; 1498 } 1499 1500 /* ----------------------------------------------------------------------------- 1501 * Power management 1502 */ 1503 1504 static int ccs_write_msr_regs(struct ccs_sensor *sensor) 1505 { 1506 int rval; 1507 1508 rval = ccs_write_data_regs(sensor, 1509 sensor->sdata.sensor_manufacturer_regs, 1510 sensor->sdata.num_sensor_manufacturer_regs); 1511 if (rval) 1512 return rval; 1513 1514 return ccs_write_data_regs(sensor, 1515 sensor->mdata.module_manufacturer_regs, 1516 sensor->mdata.num_module_manufacturer_regs); 1517 } 1518 1519 static int ccs_update_phy_ctrl(struct ccs_sensor *sensor) 1520 { 1521 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 1522 u8 val; 1523 1524 if (!sensor->ccs_limits) 1525 return 0; 1526 1527 if (CCS_LIM(sensor, PHY_CTRL_CAPABILITY) & 1528 CCS_PHY_CTRL_CAPABILITY_AUTO_PHY_CTL) { 1529 val = CCS_PHY_CTRL_AUTO; 1530 } else if (CCS_LIM(sensor, PHY_CTRL_CAPABILITY) & 1531 CCS_PHY_CTRL_CAPABILITY_UI_PHY_CTL) { 1532 val = CCS_PHY_CTRL_UI; 1533 } else { 1534 dev_err(&client->dev, "manual PHY control not supported\n"); 1535 return -EINVAL; 1536 } 1537 1538 return ccs_write(sensor, PHY_CTRL, val); 1539 } 1540 1541 static int ccs_power_on(struct device *dev) 1542 { 1543 struct v4l2_subdev *subdev = dev_get_drvdata(dev); 1544 struct ccs_subdev *ssd = to_ccs_subdev(subdev); 1545 /* 1546 * The sub-device related to the I2C device is always the 1547 * source one, i.e. ssds[0]. 1548 */ 1549 struct ccs_sensor *sensor = 1550 container_of(ssd, struct ccs_sensor, ssds[0]); 1551 const struct ccs_device *ccsdev = device_get_match_data(dev); 1552 int rval; 1553 1554 rval = regulator_bulk_enable(ARRAY_SIZE(ccs_regulators), 1555 sensor->regulators); 1556 if (rval) { 1557 dev_err(dev, "failed to enable vana regulator\n"); 1558 return rval; 1559 } 1560 1561 if (sensor->reset || sensor->xshutdown || sensor->ext_clk) { 1562 unsigned int sleep; 1563 1564 rval = clk_prepare_enable(sensor->ext_clk); 1565 if (rval < 0) { 1566 dev_dbg(dev, "failed to enable xclk\n"); 1567 goto out_xclk_fail; 1568 } 1569 1570 gpiod_set_value(sensor->reset, 0); 1571 gpiod_set_value(sensor->xshutdown, 1); 1572 1573 if (ccsdev->flags & CCS_DEVICE_FLAG_IS_SMIA) 1574 sleep = SMIAPP_RESET_DELAY(sensor->hwcfg.ext_clk); 1575 else 1576 sleep = 5000; 1577 1578 usleep_range(sleep, sleep); 1579 } 1580 1581 /* 1582 * Failures to respond to the address change command have been noticed. 1583 * Those failures seem to be caused by the sensor requiring a longer 1584 * boot time than advertised. An additional 10ms delay seems to work 1585 * around the issue, but the SMIA++ I2C write retry hack makes the delay 1586 * unnecessary. The failures need to be investigated to find a proper 1587 * fix, and a delay will likely need to be added here if the I2C write 1588 * retry hack is reverted before the root cause of the boot time issue 1589 * is found. 1590 */ 1591 1592 if (!sensor->reset && !sensor->xshutdown) { 1593 u8 retry = 100; 1594 u32 reset; 1595 1596 rval = ccs_write(sensor, SOFTWARE_RESET, CCS_SOFTWARE_RESET_ON); 1597 if (rval < 0) { 1598 dev_err(dev, "software reset failed\n"); 1599 goto out_cci_addr_fail; 1600 } 1601 1602 do { 1603 rval = ccs_read(sensor, SOFTWARE_RESET, &reset); 1604 reset = !rval && reset == CCS_SOFTWARE_RESET_OFF; 1605 if (reset) 1606 break; 1607 1608 usleep_range(1000, 2000); 1609 } while (--retry); 1610 1611 if (!reset) { 1612 dev_err(dev, "software reset failed\n"); 1613 rval = -EIO; 1614 goto out_cci_addr_fail; 1615 } 1616 } 1617 1618 if (sensor->hwcfg.i2c_addr_alt) { 1619 rval = ccs_change_cci_addr(sensor); 1620 if (rval) { 1621 dev_err(dev, "cci address change error\n"); 1622 goto out_cci_addr_fail; 1623 } 1624 } 1625 1626 rval = ccs_write(sensor, COMPRESSION_MODE, 1627 CCS_COMPRESSION_MODE_DPCM_PCM_SIMPLE); 1628 if (rval) { 1629 dev_err(dev, "compression mode set failed\n"); 1630 goto out_cci_addr_fail; 1631 } 1632 1633 rval = ccs_write(sensor, EXTCLK_FREQUENCY_MHZ, 1634 sensor->hwcfg.ext_clk / (1000000 / (1 << 8))); 1635 if (rval) { 1636 dev_err(dev, "extclk frequency set failed\n"); 1637 goto out_cci_addr_fail; 1638 } 1639 1640 rval = ccs_write(sensor, CSI_LANE_MODE, sensor->hwcfg.lanes - 1); 1641 if (rval) { 1642 dev_err(dev, "csi lane mode set failed\n"); 1643 goto out_cci_addr_fail; 1644 } 1645 1646 rval = ccs_write(sensor, FAST_STANDBY_CTRL, 1647 CCS_FAST_STANDBY_CTRL_FRAME_TRUNCATION); 1648 if (rval) { 1649 dev_err(dev, "fast standby set failed\n"); 1650 goto out_cci_addr_fail; 1651 } 1652 1653 rval = ccs_write(sensor, CSI_SIGNALING_MODE, 1654 sensor->hwcfg.csi_signalling_mode); 1655 if (rval) { 1656 dev_err(dev, "csi signalling mode set failed\n"); 1657 goto out_cci_addr_fail; 1658 } 1659 1660 rval = ccs_update_phy_ctrl(sensor); 1661 if (rval < 0) 1662 goto out_cci_addr_fail; 1663 1664 rval = ccs_write_msr_regs(sensor); 1665 if (rval) 1666 goto out_cci_addr_fail; 1667 1668 rval = ccs_call_quirk(sensor, post_poweron); 1669 if (rval) { 1670 dev_err(dev, "post_poweron quirks failed\n"); 1671 goto out_cci_addr_fail; 1672 } 1673 1674 return 0; 1675 1676 out_cci_addr_fail: 1677 gpiod_set_value(sensor->reset, 1); 1678 gpiod_set_value(sensor->xshutdown, 0); 1679 clk_disable_unprepare(sensor->ext_clk); 1680 1681 out_xclk_fail: 1682 regulator_bulk_disable(ARRAY_SIZE(ccs_regulators), 1683 sensor->regulators); 1684 1685 return rval; 1686 } 1687 1688 static int ccs_power_off(struct device *dev) 1689 { 1690 struct v4l2_subdev *subdev = dev_get_drvdata(dev); 1691 struct ccs_subdev *ssd = to_ccs_subdev(subdev); 1692 struct ccs_sensor *sensor = 1693 container_of(ssd, struct ccs_sensor, ssds[0]); 1694 1695 /* 1696 * Currently power/clock to lens are enable/disabled separately 1697 * but they are essentially the same signals. So if the sensor is 1698 * powered off while the lens is powered on the sensor does not 1699 * really see a power off and next time the cci address change 1700 * will fail. So do a soft reset explicitly here. 1701 */ 1702 if (sensor->hwcfg.i2c_addr_alt) 1703 ccs_write(sensor, SOFTWARE_RESET, CCS_SOFTWARE_RESET_ON); 1704 1705 gpiod_set_value(sensor->reset, 1); 1706 gpiod_set_value(sensor->xshutdown, 0); 1707 clk_disable_unprepare(sensor->ext_clk); 1708 usleep_range(5000, 5000); 1709 regulator_bulk_disable(ARRAY_SIZE(ccs_regulators), 1710 sensor->regulators); 1711 sensor->streaming = false; 1712 1713 return 0; 1714 } 1715 1716 /* ----------------------------------------------------------------------------- 1717 * Video stream management 1718 */ 1719 1720 static int ccs_start_streaming(struct ccs_sensor *sensor) 1721 { 1722 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 1723 unsigned int binning_mode; 1724 int rval; 1725 1726 mutex_lock(&sensor->mutex); 1727 1728 rval = ccs_write(sensor, CSI_DATA_FORMAT, 1729 (sensor->csi_format->width << 8) | 1730 sensor->csi_format->compressed); 1731 if (rval) 1732 goto out; 1733 1734 /* Binning configuration */ 1735 if (sensor->binning_horizontal == 1 && 1736 sensor->binning_vertical == 1) { 1737 binning_mode = 0; 1738 } else { 1739 u8 binning_type = 1740 (sensor->binning_horizontal << 4) 1741 | sensor->binning_vertical; 1742 1743 rval = ccs_write(sensor, BINNING_TYPE, binning_type); 1744 if (rval < 0) 1745 goto out; 1746 1747 binning_mode = 1; 1748 } 1749 rval = ccs_write(sensor, BINNING_MODE, binning_mode); 1750 if (rval < 0) 1751 goto out; 1752 1753 /* Set up PLL */ 1754 rval = ccs_pll_configure(sensor); 1755 if (rval) 1756 goto out; 1757 1758 /* Analog crop start coordinates */ 1759 rval = ccs_write(sensor, X_ADDR_START, 1760 sensor->pixel_array->crop[CCS_PA_PAD_SRC].left); 1761 if (rval < 0) 1762 goto out; 1763 1764 rval = ccs_write(sensor, Y_ADDR_START, 1765 sensor->pixel_array->crop[CCS_PA_PAD_SRC].top); 1766 if (rval < 0) 1767 goto out; 1768 1769 /* Analog crop end coordinates */ 1770 rval = ccs_write( 1771 sensor, X_ADDR_END, 1772 sensor->pixel_array->crop[CCS_PA_PAD_SRC].left 1773 + sensor->pixel_array->crop[CCS_PA_PAD_SRC].width - 1); 1774 if (rval < 0) 1775 goto out; 1776 1777 rval = ccs_write( 1778 sensor, Y_ADDR_END, 1779 sensor->pixel_array->crop[CCS_PA_PAD_SRC].top 1780 + sensor->pixel_array->crop[CCS_PA_PAD_SRC].height - 1); 1781 if (rval < 0) 1782 goto out; 1783 1784 /* 1785 * Output from pixel array, including blanking, is set using 1786 * controls below. No need to set here. 1787 */ 1788 1789 /* Digital crop */ 1790 if (CCS_LIM(sensor, DIGITAL_CROP_CAPABILITY) 1791 == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP) { 1792 rval = ccs_write( 1793 sensor, DIGITAL_CROP_X_OFFSET, 1794 sensor->scaler->crop[CCS_PAD_SINK].left); 1795 if (rval < 0) 1796 goto out; 1797 1798 rval = ccs_write( 1799 sensor, DIGITAL_CROP_Y_OFFSET, 1800 sensor->scaler->crop[CCS_PAD_SINK].top); 1801 if (rval < 0) 1802 goto out; 1803 1804 rval = ccs_write( 1805 sensor, DIGITAL_CROP_IMAGE_WIDTH, 1806 sensor->scaler->crop[CCS_PAD_SINK].width); 1807 if (rval < 0) 1808 goto out; 1809 1810 rval = ccs_write( 1811 sensor, DIGITAL_CROP_IMAGE_HEIGHT, 1812 sensor->scaler->crop[CCS_PAD_SINK].height); 1813 if (rval < 0) 1814 goto out; 1815 } 1816 1817 /* Scaling */ 1818 if (CCS_LIM(sensor, SCALING_CAPABILITY) 1819 != CCS_SCALING_CAPABILITY_NONE) { 1820 rval = ccs_write(sensor, SCALING_MODE, sensor->scaling_mode); 1821 if (rval < 0) 1822 goto out; 1823 1824 rval = ccs_write(sensor, SCALE_M, sensor->scale_m); 1825 if (rval < 0) 1826 goto out; 1827 } 1828 1829 /* Output size from sensor */ 1830 rval = ccs_write(sensor, X_OUTPUT_SIZE, 1831 sensor->src->crop[CCS_PAD_SRC].width); 1832 if (rval < 0) 1833 goto out; 1834 rval = ccs_write(sensor, Y_OUTPUT_SIZE, 1835 sensor->src->crop[CCS_PAD_SRC].height); 1836 if (rval < 0) 1837 goto out; 1838 1839 if (CCS_LIM(sensor, FLASH_MODE_CAPABILITY) & 1840 (CCS_FLASH_MODE_CAPABILITY_SINGLE_STROBE | 1841 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE) && 1842 sensor->hwcfg.strobe_setup != NULL && 1843 sensor->hwcfg.strobe_setup->trigger != 0) { 1844 rval = ccs_setup_flash_strobe(sensor); 1845 if (rval) 1846 goto out; 1847 } 1848 1849 rval = ccs_call_quirk(sensor, pre_streamon); 1850 if (rval) { 1851 dev_err(&client->dev, "pre_streamon quirks failed\n"); 1852 goto out; 1853 } 1854 1855 rval = ccs_write(sensor, MODE_SELECT, CCS_MODE_SELECT_STREAMING); 1856 1857 out: 1858 mutex_unlock(&sensor->mutex); 1859 1860 return rval; 1861 } 1862 1863 static int ccs_stop_streaming(struct ccs_sensor *sensor) 1864 { 1865 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 1866 int rval; 1867 1868 mutex_lock(&sensor->mutex); 1869 rval = ccs_write(sensor, MODE_SELECT, CCS_MODE_SELECT_SOFTWARE_STANDBY); 1870 if (rval) 1871 goto out; 1872 1873 rval = ccs_call_quirk(sensor, post_streamoff); 1874 if (rval) 1875 dev_err(&client->dev, "post_streamoff quirks failed\n"); 1876 1877 out: 1878 mutex_unlock(&sensor->mutex); 1879 return rval; 1880 } 1881 1882 /* ----------------------------------------------------------------------------- 1883 * V4L2 subdev video operations 1884 */ 1885 1886 static int ccs_pm_get_init(struct ccs_sensor *sensor) 1887 { 1888 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 1889 int rval; 1890 1891 /* 1892 * It can't use pm_runtime_resume_and_get() here, as the driver 1893 * relies at the returned value to detect if the device was already 1894 * active or not. 1895 */ 1896 rval = pm_runtime_get_sync(&client->dev); 1897 if (rval < 0) 1898 goto error; 1899 1900 /* Device was already active, so don't set controls */ 1901 if (rval == 1) 1902 return 0; 1903 1904 /* Restore V4L2 controls to the previously suspended device */ 1905 rval = v4l2_ctrl_handler_setup(&sensor->pixel_array->ctrl_handler); 1906 if (rval) 1907 goto error; 1908 1909 rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler); 1910 if (rval) 1911 goto error; 1912 1913 /* Keep PM runtime usage_count incremented on success */ 1914 return 0; 1915 error: 1916 pm_runtime_put(&client->dev); 1917 return rval; 1918 } 1919 1920 static int ccs_set_stream(struct v4l2_subdev *subdev, int enable) 1921 { 1922 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 1923 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 1924 int rval; 1925 1926 if (sensor->streaming == enable) 1927 return 0; 1928 1929 if (!enable) { 1930 ccs_stop_streaming(sensor); 1931 sensor->streaming = false; 1932 pm_runtime_mark_last_busy(&client->dev); 1933 pm_runtime_put_autosuspend(&client->dev); 1934 1935 return 0; 1936 } 1937 1938 rval = ccs_pm_get_init(sensor); 1939 if (rval) 1940 return rval; 1941 1942 sensor->streaming = true; 1943 1944 rval = ccs_start_streaming(sensor); 1945 if (rval < 0) { 1946 sensor->streaming = false; 1947 pm_runtime_mark_last_busy(&client->dev); 1948 pm_runtime_put_autosuspend(&client->dev); 1949 } 1950 1951 return rval; 1952 } 1953 1954 static int ccs_pre_streamon(struct v4l2_subdev *subdev, u32 flags) 1955 { 1956 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 1957 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 1958 int rval; 1959 1960 if (flags & V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP) { 1961 switch (sensor->hwcfg.csi_signalling_mode) { 1962 case CCS_CSI_SIGNALING_MODE_CSI_2_DPHY: 1963 if (!(CCS_LIM(sensor, PHY_CTRL_CAPABILITY_2) & 1964 CCS_PHY_CTRL_CAPABILITY_2_MANUAL_LP_DPHY)) 1965 return -EACCES; 1966 break; 1967 case CCS_CSI_SIGNALING_MODE_CSI_2_CPHY: 1968 if (!(CCS_LIM(sensor, PHY_CTRL_CAPABILITY_2) & 1969 CCS_PHY_CTRL_CAPABILITY_2_MANUAL_LP_CPHY)) 1970 return -EACCES; 1971 break; 1972 default: 1973 return -EACCES; 1974 } 1975 } 1976 1977 rval = ccs_pm_get_init(sensor); 1978 if (rval) 1979 return rval; 1980 1981 if (flags & V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP) { 1982 rval = ccs_write(sensor, MANUAL_LP_CTRL, 1983 CCS_MANUAL_LP_CTRL_ENABLE); 1984 if (rval) 1985 pm_runtime_put(&client->dev); 1986 } 1987 1988 return rval; 1989 } 1990 1991 static int ccs_post_streamoff(struct v4l2_subdev *subdev) 1992 { 1993 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 1994 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 1995 1996 return pm_runtime_put(&client->dev); 1997 } 1998 1999 static int ccs_enum_mbus_code(struct v4l2_subdev *subdev, 2000 struct v4l2_subdev_state *sd_state, 2001 struct v4l2_subdev_mbus_code_enum *code) 2002 { 2003 struct i2c_client *client = v4l2_get_subdevdata(subdev); 2004 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2005 unsigned int i; 2006 int idx = -1; 2007 int rval = -EINVAL; 2008 2009 mutex_lock(&sensor->mutex); 2010 2011 dev_err(&client->dev, "subdev %s, pad %u, index %u\n", 2012 subdev->name, code->pad, code->index); 2013 2014 if (subdev != &sensor->src->sd || code->pad != CCS_PAD_SRC) { 2015 if (code->index) 2016 goto out; 2017 2018 code->code = sensor->internal_csi_format->code; 2019 rval = 0; 2020 goto out; 2021 } 2022 2023 for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) { 2024 if (sensor->mbus_frame_fmts & (1 << i)) 2025 idx++; 2026 2027 if (idx == code->index) { 2028 code->code = ccs_csi_data_formats[i].code; 2029 dev_err(&client->dev, "found index %u, i %u, code %x\n", 2030 code->index, i, code->code); 2031 rval = 0; 2032 break; 2033 } 2034 } 2035 2036 out: 2037 mutex_unlock(&sensor->mutex); 2038 2039 return rval; 2040 } 2041 2042 static u32 __ccs_get_mbus_code(struct v4l2_subdev *subdev, unsigned int pad) 2043 { 2044 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2045 2046 if (subdev == &sensor->src->sd && pad == CCS_PAD_SRC) 2047 return sensor->csi_format->code; 2048 else 2049 return sensor->internal_csi_format->code; 2050 } 2051 2052 static int __ccs_get_format(struct v4l2_subdev *subdev, 2053 struct v4l2_subdev_state *sd_state, 2054 struct v4l2_subdev_format *fmt) 2055 { 2056 struct ccs_subdev *ssd = to_ccs_subdev(subdev); 2057 2058 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 2059 fmt->format = *v4l2_subdev_get_try_format(subdev, sd_state, 2060 fmt->pad); 2061 } else { 2062 struct v4l2_rect *r; 2063 2064 if (fmt->pad == ssd->source_pad) 2065 r = &ssd->crop[ssd->source_pad]; 2066 else 2067 r = &ssd->sink_fmt; 2068 2069 fmt->format.code = __ccs_get_mbus_code(subdev, fmt->pad); 2070 fmt->format.width = r->width; 2071 fmt->format.height = r->height; 2072 fmt->format.field = V4L2_FIELD_NONE; 2073 } 2074 2075 return 0; 2076 } 2077 2078 static int ccs_get_format(struct v4l2_subdev *subdev, 2079 struct v4l2_subdev_state *sd_state, 2080 struct v4l2_subdev_format *fmt) 2081 { 2082 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2083 int rval; 2084 2085 mutex_lock(&sensor->mutex); 2086 rval = __ccs_get_format(subdev, sd_state, fmt); 2087 mutex_unlock(&sensor->mutex); 2088 2089 return rval; 2090 } 2091 2092 static void ccs_get_crop_compose(struct v4l2_subdev *subdev, 2093 struct v4l2_subdev_state *sd_state, 2094 struct v4l2_rect **crops, 2095 struct v4l2_rect **comps, int which) 2096 { 2097 struct ccs_subdev *ssd = to_ccs_subdev(subdev); 2098 unsigned int i; 2099 2100 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) { 2101 if (crops) 2102 for (i = 0; i < subdev->entity.num_pads; i++) 2103 crops[i] = &ssd->crop[i]; 2104 if (comps) 2105 *comps = &ssd->compose; 2106 } else { 2107 if (crops) { 2108 for (i = 0; i < subdev->entity.num_pads; i++) 2109 crops[i] = v4l2_subdev_get_try_crop(subdev, 2110 sd_state, 2111 i); 2112 } 2113 if (comps) 2114 *comps = v4l2_subdev_get_try_compose(subdev, sd_state, 2115 CCS_PAD_SINK); 2116 } 2117 } 2118 2119 /* Changes require propagation only on sink pad. */ 2120 static void ccs_propagate(struct v4l2_subdev *subdev, 2121 struct v4l2_subdev_state *sd_state, int which, 2122 int target) 2123 { 2124 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2125 struct ccs_subdev *ssd = to_ccs_subdev(subdev); 2126 struct v4l2_rect *comp, *crops[CCS_PADS]; 2127 2128 ccs_get_crop_compose(subdev, sd_state, crops, &comp, which); 2129 2130 switch (target) { 2131 case V4L2_SEL_TGT_CROP: 2132 comp->width = crops[CCS_PAD_SINK]->width; 2133 comp->height = crops[CCS_PAD_SINK]->height; 2134 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) { 2135 if (ssd == sensor->scaler) { 2136 sensor->scale_m = CCS_LIM(sensor, SCALER_N_MIN); 2137 sensor->scaling_mode = 2138 CCS_SCALING_MODE_NO_SCALING; 2139 } else if (ssd == sensor->binner) { 2140 sensor->binning_horizontal = 1; 2141 sensor->binning_vertical = 1; 2142 } 2143 } 2144 fallthrough; 2145 case V4L2_SEL_TGT_COMPOSE: 2146 *crops[CCS_PAD_SRC] = *comp; 2147 break; 2148 default: 2149 WARN_ON_ONCE(1); 2150 } 2151 } 2152 2153 static const struct ccs_csi_data_format 2154 *ccs_validate_csi_data_format(struct ccs_sensor *sensor, u32 code) 2155 { 2156 unsigned int i; 2157 2158 for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) { 2159 if (sensor->mbus_frame_fmts & (1 << i) && 2160 ccs_csi_data_formats[i].code == code) 2161 return &ccs_csi_data_formats[i]; 2162 } 2163 2164 return sensor->csi_format; 2165 } 2166 2167 static int ccs_set_format_source(struct v4l2_subdev *subdev, 2168 struct v4l2_subdev_state *sd_state, 2169 struct v4l2_subdev_format *fmt) 2170 { 2171 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2172 const struct ccs_csi_data_format *csi_format, 2173 *old_csi_format = sensor->csi_format; 2174 unsigned long *valid_link_freqs; 2175 u32 code = fmt->format.code; 2176 unsigned int i; 2177 int rval; 2178 2179 rval = __ccs_get_format(subdev, sd_state, fmt); 2180 if (rval) 2181 return rval; 2182 2183 /* 2184 * Media bus code is changeable on src subdev's source pad. On 2185 * other source pads we just get format here. 2186 */ 2187 if (subdev != &sensor->src->sd) 2188 return 0; 2189 2190 csi_format = ccs_validate_csi_data_format(sensor, code); 2191 2192 fmt->format.code = csi_format->code; 2193 2194 if (fmt->which != V4L2_SUBDEV_FORMAT_ACTIVE) 2195 return 0; 2196 2197 sensor->csi_format = csi_format; 2198 2199 if (csi_format->width != old_csi_format->width) 2200 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) 2201 __v4l2_ctrl_modify_range( 2202 sensor->test_data[i], 0, 2203 (1 << csi_format->width) - 1, 1, 0); 2204 2205 if (csi_format->compressed == old_csi_format->compressed) 2206 return 0; 2207 2208 valid_link_freqs = 2209 &sensor->valid_link_freqs[sensor->csi_format->compressed 2210 - sensor->compressed_min_bpp]; 2211 2212 __v4l2_ctrl_modify_range( 2213 sensor->link_freq, 0, 2214 __fls(*valid_link_freqs), ~*valid_link_freqs, 2215 __ffs(*valid_link_freqs)); 2216 2217 return ccs_pll_update(sensor); 2218 } 2219 2220 static int ccs_set_format(struct v4l2_subdev *subdev, 2221 struct v4l2_subdev_state *sd_state, 2222 struct v4l2_subdev_format *fmt) 2223 { 2224 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2225 struct ccs_subdev *ssd = to_ccs_subdev(subdev); 2226 struct v4l2_rect *crops[CCS_PADS]; 2227 2228 mutex_lock(&sensor->mutex); 2229 2230 if (fmt->pad == ssd->source_pad) { 2231 int rval; 2232 2233 rval = ccs_set_format_source(subdev, sd_state, fmt); 2234 2235 mutex_unlock(&sensor->mutex); 2236 2237 return rval; 2238 } 2239 2240 /* Sink pad. Width and height are changeable here. */ 2241 fmt->format.code = __ccs_get_mbus_code(subdev, fmt->pad); 2242 fmt->format.width &= ~1; 2243 fmt->format.height &= ~1; 2244 fmt->format.field = V4L2_FIELD_NONE; 2245 2246 fmt->format.width = 2247 clamp(fmt->format.width, 2248 CCS_LIM(sensor, MIN_X_OUTPUT_SIZE), 2249 CCS_LIM(sensor, MAX_X_OUTPUT_SIZE)); 2250 fmt->format.height = 2251 clamp(fmt->format.height, 2252 CCS_LIM(sensor, MIN_Y_OUTPUT_SIZE), 2253 CCS_LIM(sensor, MAX_Y_OUTPUT_SIZE)); 2254 2255 ccs_get_crop_compose(subdev, sd_state, crops, NULL, fmt->which); 2256 2257 crops[ssd->sink_pad]->left = 0; 2258 crops[ssd->sink_pad]->top = 0; 2259 crops[ssd->sink_pad]->width = fmt->format.width; 2260 crops[ssd->sink_pad]->height = fmt->format.height; 2261 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) 2262 ssd->sink_fmt = *crops[ssd->sink_pad]; 2263 ccs_propagate(subdev, sd_state, fmt->which, V4L2_SEL_TGT_CROP); 2264 2265 mutex_unlock(&sensor->mutex); 2266 2267 return 0; 2268 } 2269 2270 /* 2271 * Calculate goodness of scaled image size compared to expected image 2272 * size and flags provided. 2273 */ 2274 #define SCALING_GOODNESS 100000 2275 #define SCALING_GOODNESS_EXTREME 100000000 2276 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w, 2277 int h, int ask_h, u32 flags) 2278 { 2279 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2280 struct i2c_client *client = v4l2_get_subdevdata(subdev); 2281 int val = 0; 2282 2283 w &= ~1; 2284 ask_w &= ~1; 2285 h &= ~1; 2286 ask_h &= ~1; 2287 2288 if (flags & V4L2_SEL_FLAG_GE) { 2289 if (w < ask_w) 2290 val -= SCALING_GOODNESS; 2291 if (h < ask_h) 2292 val -= SCALING_GOODNESS; 2293 } 2294 2295 if (flags & V4L2_SEL_FLAG_LE) { 2296 if (w > ask_w) 2297 val -= SCALING_GOODNESS; 2298 if (h > ask_h) 2299 val -= SCALING_GOODNESS; 2300 } 2301 2302 val -= abs(w - ask_w); 2303 val -= abs(h - ask_h); 2304 2305 if (w < CCS_LIM(sensor, MIN_X_OUTPUT_SIZE)) 2306 val -= SCALING_GOODNESS_EXTREME; 2307 2308 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n", 2309 w, ask_w, h, ask_h, val); 2310 2311 return val; 2312 } 2313 2314 static void ccs_set_compose_binner(struct v4l2_subdev *subdev, 2315 struct v4l2_subdev_state *sd_state, 2316 struct v4l2_subdev_selection *sel, 2317 struct v4l2_rect **crops, 2318 struct v4l2_rect *comp) 2319 { 2320 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2321 unsigned int i; 2322 unsigned int binh = 1, binv = 1; 2323 int best = scaling_goodness( 2324 subdev, 2325 crops[CCS_PAD_SINK]->width, sel->r.width, 2326 crops[CCS_PAD_SINK]->height, sel->r.height, sel->flags); 2327 2328 for (i = 0; i < sensor->nbinning_subtypes; i++) { 2329 int this = scaling_goodness( 2330 subdev, 2331 crops[CCS_PAD_SINK]->width 2332 / sensor->binning_subtypes[i].horizontal, 2333 sel->r.width, 2334 crops[CCS_PAD_SINK]->height 2335 / sensor->binning_subtypes[i].vertical, 2336 sel->r.height, sel->flags); 2337 2338 if (this > best) { 2339 binh = sensor->binning_subtypes[i].horizontal; 2340 binv = sensor->binning_subtypes[i].vertical; 2341 best = this; 2342 } 2343 } 2344 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 2345 sensor->binning_vertical = binv; 2346 sensor->binning_horizontal = binh; 2347 } 2348 2349 sel->r.width = (crops[CCS_PAD_SINK]->width / binh) & ~1; 2350 sel->r.height = (crops[CCS_PAD_SINK]->height / binv) & ~1; 2351 } 2352 2353 /* 2354 * Calculate best scaling ratio and mode for given output resolution. 2355 * 2356 * Try all of these: horizontal ratio, vertical ratio and smallest 2357 * size possible (horizontally). 2358 * 2359 * Also try whether horizontal scaler or full scaler gives a better 2360 * result. 2361 */ 2362 static void ccs_set_compose_scaler(struct v4l2_subdev *subdev, 2363 struct v4l2_subdev_state *sd_state, 2364 struct v4l2_subdev_selection *sel, 2365 struct v4l2_rect **crops, 2366 struct v4l2_rect *comp) 2367 { 2368 struct i2c_client *client = v4l2_get_subdevdata(subdev); 2369 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2370 u32 min, max, a, b, max_m; 2371 u32 scale_m = CCS_LIM(sensor, SCALER_N_MIN); 2372 int mode = CCS_SCALING_MODE_HORIZONTAL; 2373 u32 try[4]; 2374 u32 ntry = 0; 2375 unsigned int i; 2376 int best = INT_MIN; 2377 2378 sel->r.width = min_t(unsigned int, sel->r.width, 2379 crops[CCS_PAD_SINK]->width); 2380 sel->r.height = min_t(unsigned int, sel->r.height, 2381 crops[CCS_PAD_SINK]->height); 2382 2383 a = crops[CCS_PAD_SINK]->width 2384 * CCS_LIM(sensor, SCALER_N_MIN) / sel->r.width; 2385 b = crops[CCS_PAD_SINK]->height 2386 * CCS_LIM(sensor, SCALER_N_MIN) / sel->r.height; 2387 max_m = crops[CCS_PAD_SINK]->width 2388 * CCS_LIM(sensor, SCALER_N_MIN) 2389 / CCS_LIM(sensor, MIN_X_OUTPUT_SIZE); 2390 2391 a = clamp(a, CCS_LIM(sensor, SCALER_M_MIN), 2392 CCS_LIM(sensor, SCALER_M_MAX)); 2393 b = clamp(b, CCS_LIM(sensor, SCALER_M_MIN), 2394 CCS_LIM(sensor, SCALER_M_MAX)); 2395 max_m = clamp(max_m, CCS_LIM(sensor, SCALER_M_MIN), 2396 CCS_LIM(sensor, SCALER_M_MAX)); 2397 2398 dev_dbg(&client->dev, "scaling: a %u b %u max_m %u\n", a, b, max_m); 2399 2400 min = min(max_m, min(a, b)); 2401 max = min(max_m, max(a, b)); 2402 2403 try[ntry] = min; 2404 ntry++; 2405 if (min != max) { 2406 try[ntry] = max; 2407 ntry++; 2408 } 2409 if (max != max_m) { 2410 try[ntry] = min + 1; 2411 ntry++; 2412 if (min != max) { 2413 try[ntry] = max + 1; 2414 ntry++; 2415 } 2416 } 2417 2418 for (i = 0; i < ntry; i++) { 2419 int this = scaling_goodness( 2420 subdev, 2421 crops[CCS_PAD_SINK]->width 2422 / try[i] * CCS_LIM(sensor, SCALER_N_MIN), 2423 sel->r.width, 2424 crops[CCS_PAD_SINK]->height, 2425 sel->r.height, 2426 sel->flags); 2427 2428 dev_dbg(&client->dev, "trying factor %u (%u)\n", try[i], i); 2429 2430 if (this > best) { 2431 scale_m = try[i]; 2432 mode = CCS_SCALING_MODE_HORIZONTAL; 2433 best = this; 2434 } 2435 2436 if (CCS_LIM(sensor, SCALING_CAPABILITY) 2437 == CCS_SCALING_CAPABILITY_HORIZONTAL) 2438 continue; 2439 2440 this = scaling_goodness( 2441 subdev, crops[CCS_PAD_SINK]->width 2442 / try[i] 2443 * CCS_LIM(sensor, SCALER_N_MIN), 2444 sel->r.width, 2445 crops[CCS_PAD_SINK]->height 2446 / try[i] 2447 * CCS_LIM(sensor, SCALER_N_MIN), 2448 sel->r.height, 2449 sel->flags); 2450 2451 if (this > best) { 2452 scale_m = try[i]; 2453 mode = SMIAPP_SCALING_MODE_BOTH; 2454 best = this; 2455 } 2456 } 2457 2458 sel->r.width = 2459 (crops[CCS_PAD_SINK]->width 2460 / scale_m 2461 * CCS_LIM(sensor, SCALER_N_MIN)) & ~1; 2462 if (mode == SMIAPP_SCALING_MODE_BOTH) 2463 sel->r.height = 2464 (crops[CCS_PAD_SINK]->height 2465 / scale_m 2466 * CCS_LIM(sensor, SCALER_N_MIN)) 2467 & ~1; 2468 else 2469 sel->r.height = crops[CCS_PAD_SINK]->height; 2470 2471 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 2472 sensor->scale_m = scale_m; 2473 sensor->scaling_mode = mode; 2474 } 2475 } 2476 /* We're only called on source pads. This function sets scaling. */ 2477 static int ccs_set_compose(struct v4l2_subdev *subdev, 2478 struct v4l2_subdev_state *sd_state, 2479 struct v4l2_subdev_selection *sel) 2480 { 2481 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2482 struct ccs_subdev *ssd = to_ccs_subdev(subdev); 2483 struct v4l2_rect *comp, *crops[CCS_PADS]; 2484 2485 ccs_get_crop_compose(subdev, sd_state, crops, &comp, sel->which); 2486 2487 sel->r.top = 0; 2488 sel->r.left = 0; 2489 2490 if (ssd == sensor->binner) 2491 ccs_set_compose_binner(subdev, sd_state, sel, crops, comp); 2492 else 2493 ccs_set_compose_scaler(subdev, sd_state, sel, crops, comp); 2494 2495 *comp = sel->r; 2496 ccs_propagate(subdev, sd_state, sel->which, V4L2_SEL_TGT_COMPOSE); 2497 2498 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) 2499 return ccs_pll_blanking_update(sensor); 2500 2501 return 0; 2502 } 2503 2504 static int __ccs_sel_supported(struct v4l2_subdev *subdev, 2505 struct v4l2_subdev_selection *sel) 2506 { 2507 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2508 struct ccs_subdev *ssd = to_ccs_subdev(subdev); 2509 2510 /* We only implement crop in three places. */ 2511 switch (sel->target) { 2512 case V4L2_SEL_TGT_CROP: 2513 case V4L2_SEL_TGT_CROP_BOUNDS: 2514 if (ssd == sensor->pixel_array && sel->pad == CCS_PA_PAD_SRC) 2515 return 0; 2516 if (ssd == sensor->src && sel->pad == CCS_PAD_SRC) 2517 return 0; 2518 if (ssd == sensor->scaler && sel->pad == CCS_PAD_SINK && 2519 CCS_LIM(sensor, DIGITAL_CROP_CAPABILITY) 2520 == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP) 2521 return 0; 2522 return -EINVAL; 2523 case V4L2_SEL_TGT_NATIVE_SIZE: 2524 if (ssd == sensor->pixel_array && sel->pad == CCS_PA_PAD_SRC) 2525 return 0; 2526 return -EINVAL; 2527 case V4L2_SEL_TGT_COMPOSE: 2528 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 2529 if (sel->pad == ssd->source_pad) 2530 return -EINVAL; 2531 if (ssd == sensor->binner) 2532 return 0; 2533 if (ssd == sensor->scaler && CCS_LIM(sensor, SCALING_CAPABILITY) 2534 != CCS_SCALING_CAPABILITY_NONE) 2535 return 0; 2536 fallthrough; 2537 default: 2538 return -EINVAL; 2539 } 2540 } 2541 2542 static int ccs_set_crop(struct v4l2_subdev *subdev, 2543 struct v4l2_subdev_state *sd_state, 2544 struct v4l2_subdev_selection *sel) 2545 { 2546 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2547 struct ccs_subdev *ssd = to_ccs_subdev(subdev); 2548 struct v4l2_rect *src_size, *crops[CCS_PADS]; 2549 struct v4l2_rect _r; 2550 2551 ccs_get_crop_compose(subdev, sd_state, crops, NULL, sel->which); 2552 2553 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 2554 if (sel->pad == ssd->sink_pad) 2555 src_size = &ssd->sink_fmt; 2556 else 2557 src_size = &ssd->compose; 2558 } else { 2559 if (sel->pad == ssd->sink_pad) { 2560 _r.left = 0; 2561 _r.top = 0; 2562 _r.width = v4l2_subdev_get_try_format(subdev, 2563 sd_state, 2564 sel->pad) 2565 ->width; 2566 _r.height = v4l2_subdev_get_try_format(subdev, 2567 sd_state, 2568 sel->pad) 2569 ->height; 2570 src_size = &_r; 2571 } else { 2572 src_size = v4l2_subdev_get_try_compose( 2573 subdev, sd_state, ssd->sink_pad); 2574 } 2575 } 2576 2577 if (ssd == sensor->src && sel->pad == CCS_PAD_SRC) { 2578 sel->r.left = 0; 2579 sel->r.top = 0; 2580 } 2581 2582 sel->r.width = min(sel->r.width, src_size->width); 2583 sel->r.height = min(sel->r.height, src_size->height); 2584 2585 sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width); 2586 sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height); 2587 2588 *crops[sel->pad] = sel->r; 2589 2590 if (ssd != sensor->pixel_array && sel->pad == CCS_PAD_SINK) 2591 ccs_propagate(subdev, sd_state, sel->which, V4L2_SEL_TGT_CROP); 2592 2593 return 0; 2594 } 2595 2596 static void ccs_get_native_size(struct ccs_subdev *ssd, struct v4l2_rect *r) 2597 { 2598 r->top = 0; 2599 r->left = 0; 2600 r->width = CCS_LIM(ssd->sensor, X_ADDR_MAX) + 1; 2601 r->height = CCS_LIM(ssd->sensor, Y_ADDR_MAX) + 1; 2602 } 2603 2604 static int __ccs_get_selection(struct v4l2_subdev *subdev, 2605 struct v4l2_subdev_state *sd_state, 2606 struct v4l2_subdev_selection *sel) 2607 { 2608 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2609 struct ccs_subdev *ssd = to_ccs_subdev(subdev); 2610 struct v4l2_rect *comp, *crops[CCS_PADS]; 2611 struct v4l2_rect sink_fmt; 2612 int ret; 2613 2614 ret = __ccs_sel_supported(subdev, sel); 2615 if (ret) 2616 return ret; 2617 2618 ccs_get_crop_compose(subdev, sd_state, crops, &comp, sel->which); 2619 2620 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 2621 sink_fmt = ssd->sink_fmt; 2622 } else { 2623 struct v4l2_mbus_framefmt *fmt = 2624 v4l2_subdev_get_try_format(subdev, sd_state, 2625 ssd->sink_pad); 2626 2627 sink_fmt.left = 0; 2628 sink_fmt.top = 0; 2629 sink_fmt.width = fmt->width; 2630 sink_fmt.height = fmt->height; 2631 } 2632 2633 switch (sel->target) { 2634 case V4L2_SEL_TGT_CROP_BOUNDS: 2635 case V4L2_SEL_TGT_NATIVE_SIZE: 2636 if (ssd == sensor->pixel_array) 2637 ccs_get_native_size(ssd, &sel->r); 2638 else if (sel->pad == ssd->sink_pad) 2639 sel->r = sink_fmt; 2640 else 2641 sel->r = *comp; 2642 break; 2643 case V4L2_SEL_TGT_CROP: 2644 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 2645 sel->r = *crops[sel->pad]; 2646 break; 2647 case V4L2_SEL_TGT_COMPOSE: 2648 sel->r = *comp; 2649 break; 2650 } 2651 2652 return 0; 2653 } 2654 2655 static int ccs_get_selection(struct v4l2_subdev *subdev, 2656 struct v4l2_subdev_state *sd_state, 2657 struct v4l2_subdev_selection *sel) 2658 { 2659 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2660 int rval; 2661 2662 mutex_lock(&sensor->mutex); 2663 rval = __ccs_get_selection(subdev, sd_state, sel); 2664 mutex_unlock(&sensor->mutex); 2665 2666 return rval; 2667 } 2668 2669 static int ccs_set_selection(struct v4l2_subdev *subdev, 2670 struct v4l2_subdev_state *sd_state, 2671 struct v4l2_subdev_selection *sel) 2672 { 2673 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2674 int ret; 2675 2676 ret = __ccs_sel_supported(subdev, sel); 2677 if (ret) 2678 return ret; 2679 2680 mutex_lock(&sensor->mutex); 2681 2682 sel->r.left = max(0, sel->r.left & ~1); 2683 sel->r.top = max(0, sel->r.top & ~1); 2684 sel->r.width = CCS_ALIGN_DIM(sel->r.width, sel->flags); 2685 sel->r.height = CCS_ALIGN_DIM(sel->r.height, sel->flags); 2686 2687 sel->r.width = max_t(unsigned int, CCS_LIM(sensor, MIN_X_OUTPUT_SIZE), 2688 sel->r.width); 2689 sel->r.height = max_t(unsigned int, CCS_LIM(sensor, MIN_Y_OUTPUT_SIZE), 2690 sel->r.height); 2691 2692 switch (sel->target) { 2693 case V4L2_SEL_TGT_CROP: 2694 ret = ccs_set_crop(subdev, sd_state, sel); 2695 break; 2696 case V4L2_SEL_TGT_COMPOSE: 2697 ret = ccs_set_compose(subdev, sd_state, sel); 2698 break; 2699 default: 2700 ret = -EINVAL; 2701 } 2702 2703 mutex_unlock(&sensor->mutex); 2704 return ret; 2705 } 2706 2707 static int ccs_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames) 2708 { 2709 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2710 2711 *frames = sensor->frame_skip; 2712 return 0; 2713 } 2714 2715 static int ccs_get_skip_top_lines(struct v4l2_subdev *subdev, u32 *lines) 2716 { 2717 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2718 2719 *lines = sensor->image_start; 2720 2721 return 0; 2722 } 2723 2724 /* ----------------------------------------------------------------------------- 2725 * sysfs attributes 2726 */ 2727 2728 static ssize_t 2729 nvm_show(struct device *dev, struct device_attribute *attr, char *buf) 2730 { 2731 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev)); 2732 struct i2c_client *client = v4l2_get_subdevdata(subdev); 2733 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2734 int rval; 2735 2736 if (!sensor->dev_init_done) 2737 return -EBUSY; 2738 2739 rval = ccs_pm_get_init(sensor); 2740 if (rval < 0) 2741 return -ENODEV; 2742 2743 rval = ccs_read_nvm(sensor, buf, PAGE_SIZE); 2744 if (rval < 0) { 2745 pm_runtime_put(&client->dev); 2746 dev_err(&client->dev, "nvm read failed\n"); 2747 return -ENODEV; 2748 } 2749 2750 pm_runtime_mark_last_busy(&client->dev); 2751 pm_runtime_put_autosuspend(&client->dev); 2752 2753 /* 2754 * NVM is still way below a PAGE_SIZE, so we can safely 2755 * assume this for now. 2756 */ 2757 return rval; 2758 } 2759 static DEVICE_ATTR_RO(nvm); 2760 2761 static ssize_t 2762 ident_show(struct device *dev, struct device_attribute *attr, char *buf) 2763 { 2764 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev)); 2765 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2766 struct ccs_module_info *minfo = &sensor->minfo; 2767 2768 if (minfo->mipi_manufacturer_id) 2769 return sysfs_emit(buf, "%4.4x%4.4x%2.2x\n", 2770 minfo->mipi_manufacturer_id, minfo->model_id, 2771 minfo->revision_number) + 1; 2772 else 2773 return sysfs_emit(buf, "%2.2x%4.4x%2.2x\n", 2774 minfo->smia_manufacturer_id, minfo->model_id, 2775 minfo->revision_number) + 1; 2776 } 2777 static DEVICE_ATTR_RO(ident); 2778 2779 /* ----------------------------------------------------------------------------- 2780 * V4L2 subdev core operations 2781 */ 2782 2783 static int ccs_identify_module(struct ccs_sensor *sensor) 2784 { 2785 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 2786 struct ccs_module_info *minfo = &sensor->minfo; 2787 unsigned int i; 2788 u32 rev; 2789 int rval = 0; 2790 2791 /* Module info */ 2792 rval = ccs_read(sensor, MODULE_MANUFACTURER_ID, 2793 &minfo->mipi_manufacturer_id); 2794 if (!rval && !minfo->mipi_manufacturer_id) 2795 rval = ccs_read_addr_8only(sensor, 2796 SMIAPP_REG_U8_MANUFACTURER_ID, 2797 &minfo->smia_manufacturer_id); 2798 if (!rval) 2799 rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_MODEL_ID, 2800 &minfo->model_id); 2801 if (!rval) 2802 rval = ccs_read_addr_8only(sensor, 2803 CCS_R_MODULE_REVISION_NUMBER_MAJOR, 2804 &rev); 2805 if (!rval) { 2806 rval = ccs_read_addr_8only(sensor, 2807 CCS_R_MODULE_REVISION_NUMBER_MINOR, 2808 &minfo->revision_number); 2809 minfo->revision_number |= rev << 8; 2810 } 2811 if (!rval) 2812 rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_DATE_YEAR, 2813 &minfo->module_year); 2814 if (!rval) 2815 rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_DATE_MONTH, 2816 &minfo->module_month); 2817 if (!rval) 2818 rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_DATE_DAY, 2819 &minfo->module_day); 2820 2821 /* Sensor info */ 2822 if (!rval) 2823 rval = ccs_read(sensor, SENSOR_MANUFACTURER_ID, 2824 &minfo->sensor_mipi_manufacturer_id); 2825 if (!rval && !minfo->sensor_mipi_manufacturer_id) 2826 rval = ccs_read_addr_8only(sensor, 2827 CCS_R_SENSOR_MANUFACTURER_ID, 2828 &minfo->sensor_smia_manufacturer_id); 2829 if (!rval) 2830 rval = ccs_read_addr_8only(sensor, 2831 CCS_R_SENSOR_MODEL_ID, 2832 &minfo->sensor_model_id); 2833 if (!rval) 2834 rval = ccs_read_addr_8only(sensor, 2835 CCS_R_SENSOR_REVISION_NUMBER, 2836 &minfo->sensor_revision_number); 2837 if (!rval && !minfo->sensor_revision_number) 2838 rval = ccs_read_addr_8only(sensor, 2839 CCS_R_SENSOR_REVISION_NUMBER_16, 2840 &minfo->sensor_revision_number); 2841 if (!rval) 2842 rval = ccs_read_addr_8only(sensor, 2843 CCS_R_SENSOR_FIRMWARE_VERSION, 2844 &minfo->sensor_firmware_version); 2845 2846 /* SMIA */ 2847 if (!rval) 2848 rval = ccs_read(sensor, MIPI_CCS_VERSION, &minfo->ccs_version); 2849 if (!rval && !minfo->ccs_version) 2850 rval = ccs_read_addr_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION, 2851 &minfo->smia_version); 2852 if (!rval && !minfo->ccs_version) 2853 rval = ccs_read_addr_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION, 2854 &minfo->smiapp_version); 2855 2856 if (rval) { 2857 dev_err(&client->dev, "sensor detection failed\n"); 2858 return -ENODEV; 2859 } 2860 2861 if (minfo->mipi_manufacturer_id) 2862 dev_dbg(&client->dev, "MIPI CCS module 0x%4.4x-0x%4.4x\n", 2863 minfo->mipi_manufacturer_id, minfo->model_id); 2864 else 2865 dev_dbg(&client->dev, "SMIA module 0x%2.2x-0x%4.4x\n", 2866 minfo->smia_manufacturer_id, minfo->model_id); 2867 2868 dev_dbg(&client->dev, 2869 "module revision 0x%4.4x date %2.2d-%2.2d-%2.2d\n", 2870 minfo->revision_number, minfo->module_year, minfo->module_month, 2871 minfo->module_day); 2872 2873 if (minfo->sensor_mipi_manufacturer_id) 2874 dev_dbg(&client->dev, "MIPI CCS sensor 0x%4.4x-0x%4.4x\n", 2875 minfo->sensor_mipi_manufacturer_id, 2876 minfo->sensor_model_id); 2877 else 2878 dev_dbg(&client->dev, "SMIA sensor 0x%2.2x-0x%4.4x\n", 2879 minfo->sensor_smia_manufacturer_id, 2880 minfo->sensor_model_id); 2881 2882 dev_dbg(&client->dev, 2883 "sensor revision 0x%4.4x firmware version 0x%2.2x\n", 2884 minfo->sensor_revision_number, minfo->sensor_firmware_version); 2885 2886 if (minfo->ccs_version) { 2887 dev_dbg(&client->dev, "MIPI CCS version %u.%u", 2888 (minfo->ccs_version & CCS_MIPI_CCS_VERSION_MAJOR_MASK) 2889 >> CCS_MIPI_CCS_VERSION_MAJOR_SHIFT, 2890 (minfo->ccs_version & CCS_MIPI_CCS_VERSION_MINOR_MASK)); 2891 minfo->name = CCS_NAME; 2892 } else { 2893 dev_dbg(&client->dev, 2894 "smia version %2.2d smiapp version %2.2d\n", 2895 minfo->smia_version, minfo->smiapp_version); 2896 minfo->name = SMIAPP_NAME; 2897 /* 2898 * Some modules have bad data in the lvalues below. Hope the 2899 * rvalues have better stuff. The lvalues are module 2900 * parameters whereas the rvalues are sensor parameters. 2901 */ 2902 if (minfo->sensor_smia_manufacturer_id && 2903 !minfo->smia_manufacturer_id && !minfo->model_id) { 2904 minfo->smia_manufacturer_id = 2905 minfo->sensor_smia_manufacturer_id; 2906 minfo->model_id = minfo->sensor_model_id; 2907 minfo->revision_number = minfo->sensor_revision_number; 2908 } 2909 } 2910 2911 for (i = 0; i < ARRAY_SIZE(ccs_module_idents); i++) { 2912 if (ccs_module_idents[i].mipi_manufacturer_id && 2913 ccs_module_idents[i].mipi_manufacturer_id 2914 != minfo->mipi_manufacturer_id) 2915 continue; 2916 if (ccs_module_idents[i].smia_manufacturer_id && 2917 ccs_module_idents[i].smia_manufacturer_id 2918 != minfo->smia_manufacturer_id) 2919 continue; 2920 if (ccs_module_idents[i].model_id != minfo->model_id) 2921 continue; 2922 if (ccs_module_idents[i].flags 2923 & CCS_MODULE_IDENT_FLAG_REV_LE) { 2924 if (ccs_module_idents[i].revision_number_major 2925 < (minfo->revision_number >> 8)) 2926 continue; 2927 } else { 2928 if (ccs_module_idents[i].revision_number_major 2929 != (minfo->revision_number >> 8)) 2930 continue; 2931 } 2932 2933 minfo->name = ccs_module_idents[i].name; 2934 minfo->quirk = ccs_module_idents[i].quirk; 2935 break; 2936 } 2937 2938 if (i >= ARRAY_SIZE(ccs_module_idents)) 2939 dev_warn(&client->dev, 2940 "no quirks for this module; let's hope it's fully compliant\n"); 2941 2942 dev_dbg(&client->dev, "the sensor is called %s\n", minfo->name); 2943 2944 return 0; 2945 } 2946 2947 static const struct v4l2_subdev_ops ccs_ops; 2948 static const struct v4l2_subdev_internal_ops ccs_internal_ops; 2949 static const struct media_entity_operations ccs_entity_ops; 2950 2951 static int ccs_register_subdev(struct ccs_sensor *sensor, 2952 struct ccs_subdev *ssd, 2953 struct ccs_subdev *sink_ssd, 2954 u16 source_pad, u16 sink_pad, u32 link_flags) 2955 { 2956 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 2957 int rval; 2958 2959 if (!sink_ssd) 2960 return 0; 2961 2962 rval = media_entity_pads_init(&ssd->sd.entity, ssd->npads, ssd->pads); 2963 if (rval) { 2964 dev_err(&client->dev, "media_entity_pads_init failed\n"); 2965 return rval; 2966 } 2967 2968 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev, &ssd->sd); 2969 if (rval) { 2970 dev_err(&client->dev, "v4l2_device_register_subdev failed\n"); 2971 return rval; 2972 } 2973 2974 rval = media_create_pad_link(&ssd->sd.entity, source_pad, 2975 &sink_ssd->sd.entity, sink_pad, 2976 link_flags); 2977 if (rval) { 2978 dev_err(&client->dev, "media_create_pad_link failed\n"); 2979 v4l2_device_unregister_subdev(&ssd->sd); 2980 return rval; 2981 } 2982 2983 return 0; 2984 } 2985 2986 static void ccs_unregistered(struct v4l2_subdev *subdev) 2987 { 2988 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2989 unsigned int i; 2990 2991 for (i = 1; i < sensor->ssds_used; i++) 2992 v4l2_device_unregister_subdev(&sensor->ssds[i].sd); 2993 } 2994 2995 static int ccs_registered(struct v4l2_subdev *subdev) 2996 { 2997 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 2998 int rval; 2999 3000 if (sensor->scaler) { 3001 rval = ccs_register_subdev(sensor, sensor->binner, 3002 sensor->scaler, 3003 CCS_PAD_SRC, CCS_PAD_SINK, 3004 MEDIA_LNK_FL_ENABLED | 3005 MEDIA_LNK_FL_IMMUTABLE); 3006 if (rval < 0) 3007 return rval; 3008 } 3009 3010 rval = ccs_register_subdev(sensor, sensor->pixel_array, sensor->binner, 3011 CCS_PA_PAD_SRC, CCS_PAD_SINK, 3012 MEDIA_LNK_FL_ENABLED | 3013 MEDIA_LNK_FL_IMMUTABLE); 3014 if (rval) 3015 goto out_err; 3016 3017 return 0; 3018 3019 out_err: 3020 ccs_unregistered(subdev); 3021 3022 return rval; 3023 } 3024 3025 static void ccs_cleanup(struct ccs_sensor *sensor) 3026 { 3027 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 3028 3029 device_remove_file(&client->dev, &dev_attr_nvm); 3030 device_remove_file(&client->dev, &dev_attr_ident); 3031 3032 ccs_free_controls(sensor); 3033 } 3034 3035 static void ccs_create_subdev(struct ccs_sensor *sensor, 3036 struct ccs_subdev *ssd, const char *name, 3037 unsigned short num_pads, u32 function) 3038 { 3039 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd); 3040 3041 if (!ssd) 3042 return; 3043 3044 if (ssd != sensor->src) 3045 v4l2_subdev_init(&ssd->sd, &ccs_ops); 3046 3047 ssd->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 3048 ssd->sd.entity.function = function; 3049 ssd->sensor = sensor; 3050 3051 ssd->npads = num_pads; 3052 ssd->source_pad = num_pads - 1; 3053 3054 v4l2_i2c_subdev_set_name(&ssd->sd, client, sensor->minfo.name, name); 3055 3056 ccs_get_native_size(ssd, &ssd->sink_fmt); 3057 3058 ssd->compose.width = ssd->sink_fmt.width; 3059 ssd->compose.height = ssd->sink_fmt.height; 3060 ssd->crop[ssd->source_pad] = ssd->compose; 3061 ssd->pads[ssd->source_pad].flags = MEDIA_PAD_FL_SOURCE; 3062 if (ssd != sensor->pixel_array) { 3063 ssd->crop[ssd->sink_pad] = ssd->compose; 3064 ssd->pads[ssd->sink_pad].flags = MEDIA_PAD_FL_SINK; 3065 } 3066 3067 ssd->sd.entity.ops = &ccs_entity_ops; 3068 3069 if (ssd == sensor->src) 3070 return; 3071 3072 ssd->sd.internal_ops = &ccs_internal_ops; 3073 ssd->sd.owner = THIS_MODULE; 3074 ssd->sd.dev = &client->dev; 3075 v4l2_set_subdevdata(&ssd->sd, client); 3076 } 3077 3078 static int ccs_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 3079 { 3080 struct ccs_subdev *ssd = to_ccs_subdev(sd); 3081 struct ccs_sensor *sensor = ssd->sensor; 3082 unsigned int i; 3083 3084 mutex_lock(&sensor->mutex); 3085 3086 for (i = 0; i < ssd->npads; i++) { 3087 struct v4l2_mbus_framefmt *try_fmt = 3088 v4l2_subdev_get_try_format(sd, fh->state, i); 3089 struct v4l2_rect *try_crop = 3090 v4l2_subdev_get_try_crop(sd, fh->state, i); 3091 struct v4l2_rect *try_comp; 3092 3093 ccs_get_native_size(ssd, try_crop); 3094 3095 try_fmt->width = try_crop->width; 3096 try_fmt->height = try_crop->height; 3097 try_fmt->code = sensor->internal_csi_format->code; 3098 try_fmt->field = V4L2_FIELD_NONE; 3099 3100 if (ssd == sensor->pixel_array) 3101 continue; 3102 3103 try_comp = v4l2_subdev_get_try_compose(sd, fh->state, i); 3104 *try_comp = *try_crop; 3105 } 3106 3107 mutex_unlock(&sensor->mutex); 3108 3109 return 0; 3110 } 3111 3112 static const struct v4l2_subdev_video_ops ccs_video_ops = { 3113 .s_stream = ccs_set_stream, 3114 .pre_streamon = ccs_pre_streamon, 3115 .post_streamoff = ccs_post_streamoff, 3116 }; 3117 3118 static const struct v4l2_subdev_pad_ops ccs_pad_ops = { 3119 .enum_mbus_code = ccs_enum_mbus_code, 3120 .get_fmt = ccs_get_format, 3121 .set_fmt = ccs_set_format, 3122 .get_selection = ccs_get_selection, 3123 .set_selection = ccs_set_selection, 3124 }; 3125 3126 static const struct v4l2_subdev_sensor_ops ccs_sensor_ops = { 3127 .g_skip_frames = ccs_get_skip_frames, 3128 .g_skip_top_lines = ccs_get_skip_top_lines, 3129 }; 3130 3131 static const struct v4l2_subdev_ops ccs_ops = { 3132 .video = &ccs_video_ops, 3133 .pad = &ccs_pad_ops, 3134 .sensor = &ccs_sensor_ops, 3135 }; 3136 3137 static const struct media_entity_operations ccs_entity_ops = { 3138 .link_validate = v4l2_subdev_link_validate, 3139 }; 3140 3141 static const struct v4l2_subdev_internal_ops ccs_internal_src_ops = { 3142 .registered = ccs_registered, 3143 .unregistered = ccs_unregistered, 3144 .open = ccs_open, 3145 }; 3146 3147 static const struct v4l2_subdev_internal_ops ccs_internal_ops = { 3148 .open = ccs_open, 3149 }; 3150 3151 /* ----------------------------------------------------------------------------- 3152 * I2C Driver 3153 */ 3154 3155 static int __maybe_unused ccs_suspend(struct device *dev) 3156 { 3157 struct i2c_client *client = to_i2c_client(dev); 3158 struct v4l2_subdev *subdev = i2c_get_clientdata(client); 3159 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 3160 bool streaming = sensor->streaming; 3161 int rval; 3162 3163 rval = pm_runtime_resume_and_get(dev); 3164 if (rval < 0) 3165 return rval; 3166 3167 if (sensor->streaming) 3168 ccs_stop_streaming(sensor); 3169 3170 /* save state for resume */ 3171 sensor->streaming = streaming; 3172 3173 return 0; 3174 } 3175 3176 static int __maybe_unused ccs_resume(struct device *dev) 3177 { 3178 struct i2c_client *client = to_i2c_client(dev); 3179 struct v4l2_subdev *subdev = i2c_get_clientdata(client); 3180 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 3181 int rval = 0; 3182 3183 pm_runtime_put(dev); 3184 3185 if (sensor->streaming) 3186 rval = ccs_start_streaming(sensor); 3187 3188 return rval; 3189 } 3190 3191 static int ccs_get_hwconfig(struct ccs_sensor *sensor, struct device *dev) 3192 { 3193 struct ccs_hwconfig *hwcfg = &sensor->hwcfg; 3194 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = V4L2_MBUS_UNKNOWN }; 3195 struct fwnode_handle *ep; 3196 struct fwnode_handle *fwnode = dev_fwnode(dev); 3197 unsigned int i; 3198 int rval; 3199 3200 ep = fwnode_graph_get_endpoint_by_id(fwnode, 0, 0, 3201 FWNODE_GRAPH_ENDPOINT_NEXT); 3202 if (!ep) 3203 return -ENODEV; 3204 3205 /* 3206 * Note that we do need to rely on detecting the bus type between CSI-2 3207 * D-PHY and CCP2 as the old bindings did not require it. 3208 */ 3209 rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg); 3210 if (rval) 3211 goto out_err; 3212 3213 switch (bus_cfg.bus_type) { 3214 case V4L2_MBUS_CSI2_DPHY: 3215 hwcfg->csi_signalling_mode = CCS_CSI_SIGNALING_MODE_CSI_2_DPHY; 3216 hwcfg->lanes = bus_cfg.bus.mipi_csi2.num_data_lanes; 3217 break; 3218 case V4L2_MBUS_CSI2_CPHY: 3219 hwcfg->csi_signalling_mode = CCS_CSI_SIGNALING_MODE_CSI_2_CPHY; 3220 hwcfg->lanes = bus_cfg.bus.mipi_csi2.num_data_lanes; 3221 break; 3222 case V4L2_MBUS_CSI1: 3223 case V4L2_MBUS_CCP2: 3224 hwcfg->csi_signalling_mode = (bus_cfg.bus.mipi_csi1.strobe) ? 3225 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE : 3226 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK; 3227 hwcfg->lanes = 1; 3228 break; 3229 default: 3230 dev_err(dev, "unsupported bus %u\n", bus_cfg.bus_type); 3231 rval = -EINVAL; 3232 goto out_err; 3233 } 3234 3235 rval = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency", 3236 &hwcfg->ext_clk); 3237 if (rval) 3238 dev_info(dev, "can't get clock-frequency\n"); 3239 3240 dev_dbg(dev, "clk %u, mode %u\n", hwcfg->ext_clk, 3241 hwcfg->csi_signalling_mode); 3242 3243 if (!bus_cfg.nr_of_link_frequencies) { 3244 dev_warn(dev, "no link frequencies defined\n"); 3245 rval = -EINVAL; 3246 goto out_err; 3247 } 3248 3249 hwcfg->op_sys_clock = devm_kcalloc( 3250 dev, bus_cfg.nr_of_link_frequencies + 1 /* guardian */, 3251 sizeof(*hwcfg->op_sys_clock), GFP_KERNEL); 3252 if (!hwcfg->op_sys_clock) { 3253 rval = -ENOMEM; 3254 goto out_err; 3255 } 3256 3257 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) { 3258 hwcfg->op_sys_clock[i] = bus_cfg.link_frequencies[i]; 3259 dev_dbg(dev, "freq %u: %lld\n", i, hwcfg->op_sys_clock[i]); 3260 } 3261 3262 v4l2_fwnode_endpoint_free(&bus_cfg); 3263 fwnode_handle_put(ep); 3264 3265 return 0; 3266 3267 out_err: 3268 v4l2_fwnode_endpoint_free(&bus_cfg); 3269 fwnode_handle_put(ep); 3270 3271 return rval; 3272 } 3273 3274 static int ccs_firmware_name(struct i2c_client *client, 3275 struct ccs_sensor *sensor, char *filename, 3276 size_t filename_size, bool is_module) 3277 { 3278 const struct ccs_device *ccsdev = device_get_match_data(&client->dev); 3279 bool is_ccs = !(ccsdev->flags & CCS_DEVICE_FLAG_IS_SMIA); 3280 bool is_smiapp = sensor->minfo.smiapp_version; 3281 u16 manufacturer_id; 3282 u16 model_id; 3283 u16 revision_number; 3284 3285 /* 3286 * Old SMIA is module-agnostic. Its sensor identification is based on 3287 * what now are those of the module. 3288 */ 3289 if (is_module || (!is_ccs && !is_smiapp)) { 3290 manufacturer_id = is_ccs ? 3291 sensor->minfo.mipi_manufacturer_id : 3292 sensor->minfo.smia_manufacturer_id; 3293 model_id = sensor->minfo.model_id; 3294 revision_number = sensor->minfo.revision_number; 3295 } else { 3296 manufacturer_id = is_ccs ? 3297 sensor->minfo.sensor_mipi_manufacturer_id : 3298 sensor->minfo.sensor_smia_manufacturer_id; 3299 model_id = sensor->minfo.sensor_model_id; 3300 revision_number = sensor->minfo.sensor_revision_number; 3301 } 3302 3303 return snprintf(filename, filename_size, 3304 "ccs/%s-%s-%0*x-%4.4x-%0*x.fw", 3305 is_ccs ? "ccs" : is_smiapp ? "smiapp" : "smia", 3306 is_module || (!is_ccs && !is_smiapp) ? 3307 "module" : "sensor", 3308 is_ccs ? 4 : 2, manufacturer_id, model_id, 3309 !is_ccs && !is_module ? 2 : 4, revision_number); 3310 } 3311 3312 static int ccs_probe(struct i2c_client *client) 3313 { 3314 const struct ccs_device *ccsdev = device_get_match_data(&client->dev); 3315 struct ccs_sensor *sensor; 3316 const struct firmware *fw; 3317 char filename[40]; 3318 unsigned int i; 3319 int rval; 3320 3321 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL); 3322 if (sensor == NULL) 3323 return -ENOMEM; 3324 3325 rval = ccs_get_hwconfig(sensor, &client->dev); 3326 if (rval) 3327 return rval; 3328 3329 sensor->src = &sensor->ssds[sensor->ssds_used]; 3330 3331 v4l2_i2c_subdev_init(&sensor->src->sd, client, &ccs_ops); 3332 sensor->src->sd.internal_ops = &ccs_internal_src_ops; 3333 3334 sensor->regulators = devm_kcalloc(&client->dev, 3335 ARRAY_SIZE(ccs_regulators), 3336 sizeof(*sensor->regulators), 3337 GFP_KERNEL); 3338 if (!sensor->regulators) 3339 return -ENOMEM; 3340 3341 for (i = 0; i < ARRAY_SIZE(ccs_regulators); i++) 3342 sensor->regulators[i].supply = ccs_regulators[i]; 3343 3344 rval = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(ccs_regulators), 3345 sensor->regulators); 3346 if (rval) { 3347 dev_err(&client->dev, "could not get regulators\n"); 3348 return rval; 3349 } 3350 3351 sensor->ext_clk = devm_clk_get(&client->dev, NULL); 3352 if (PTR_ERR(sensor->ext_clk) == -ENOENT) { 3353 dev_info(&client->dev, "no clock defined, continuing...\n"); 3354 sensor->ext_clk = NULL; 3355 } else if (IS_ERR(sensor->ext_clk)) { 3356 dev_err(&client->dev, "could not get clock (%ld)\n", 3357 PTR_ERR(sensor->ext_clk)); 3358 return -EPROBE_DEFER; 3359 } 3360 3361 if (sensor->ext_clk) { 3362 if (sensor->hwcfg.ext_clk) { 3363 unsigned long rate; 3364 3365 rval = clk_set_rate(sensor->ext_clk, 3366 sensor->hwcfg.ext_clk); 3367 if (rval < 0) { 3368 dev_err(&client->dev, 3369 "unable to set clock freq to %u\n", 3370 sensor->hwcfg.ext_clk); 3371 return rval; 3372 } 3373 3374 rate = clk_get_rate(sensor->ext_clk); 3375 if (rate != sensor->hwcfg.ext_clk) { 3376 dev_err(&client->dev, 3377 "can't set clock freq, asked for %u but got %lu\n", 3378 sensor->hwcfg.ext_clk, rate); 3379 return -EINVAL; 3380 } 3381 } else { 3382 sensor->hwcfg.ext_clk = clk_get_rate(sensor->ext_clk); 3383 dev_dbg(&client->dev, "obtained clock freq %u\n", 3384 sensor->hwcfg.ext_clk); 3385 } 3386 } else if (sensor->hwcfg.ext_clk) { 3387 dev_dbg(&client->dev, "assuming clock freq %u\n", 3388 sensor->hwcfg.ext_clk); 3389 } else { 3390 dev_err(&client->dev, "unable to obtain clock freq\n"); 3391 return -EINVAL; 3392 } 3393 3394 if (!sensor->hwcfg.ext_clk) { 3395 dev_err(&client->dev, "cannot work with xclk frequency 0\n"); 3396 return -EINVAL; 3397 } 3398 3399 sensor->reset = devm_gpiod_get_optional(&client->dev, "reset", 3400 GPIOD_OUT_HIGH); 3401 if (IS_ERR(sensor->reset)) 3402 return PTR_ERR(sensor->reset); 3403 /* Support old users that may have used "xshutdown" property. */ 3404 if (!sensor->reset) 3405 sensor->xshutdown = devm_gpiod_get_optional(&client->dev, 3406 "xshutdown", 3407 GPIOD_OUT_LOW); 3408 if (IS_ERR(sensor->xshutdown)) 3409 return PTR_ERR(sensor->xshutdown); 3410 3411 rval = ccs_power_on(&client->dev); 3412 if (rval < 0) 3413 return rval; 3414 3415 mutex_init(&sensor->mutex); 3416 3417 rval = ccs_identify_module(sensor); 3418 if (rval) { 3419 rval = -ENODEV; 3420 goto out_power_off; 3421 } 3422 3423 rval = ccs_firmware_name(client, sensor, filename, sizeof(filename), 3424 false); 3425 if (rval >= sizeof(filename)) { 3426 rval = -ENOMEM; 3427 goto out_power_off; 3428 } 3429 3430 rval = request_firmware(&fw, filename, &client->dev); 3431 if (!rval) { 3432 ccs_data_parse(&sensor->sdata, fw->data, fw->size, &client->dev, 3433 true); 3434 release_firmware(fw); 3435 } 3436 3437 if (!(ccsdev->flags & CCS_DEVICE_FLAG_IS_SMIA) || 3438 sensor->minfo.smiapp_version) { 3439 rval = ccs_firmware_name(client, sensor, filename, 3440 sizeof(filename), true); 3441 if (rval >= sizeof(filename)) { 3442 rval = -ENOMEM; 3443 goto out_release_sdata; 3444 } 3445 3446 rval = request_firmware(&fw, filename, &client->dev); 3447 if (!rval) { 3448 ccs_data_parse(&sensor->mdata, fw->data, fw->size, 3449 &client->dev, true); 3450 release_firmware(fw); 3451 } 3452 } 3453 3454 rval = ccs_read_all_limits(sensor); 3455 if (rval) 3456 goto out_release_mdata; 3457 3458 rval = ccs_read_frame_fmt(sensor); 3459 if (rval) { 3460 rval = -ENODEV; 3461 goto out_free_ccs_limits; 3462 } 3463 3464 rval = ccs_update_phy_ctrl(sensor); 3465 if (rval < 0) 3466 goto out_free_ccs_limits; 3467 3468 rval = ccs_call_quirk(sensor, limits); 3469 if (rval) { 3470 dev_err(&client->dev, "limits quirks failed\n"); 3471 goto out_free_ccs_limits; 3472 } 3473 3474 if (CCS_LIM(sensor, BINNING_CAPABILITY)) { 3475 sensor->nbinning_subtypes = 3476 min_t(u8, CCS_LIM(sensor, BINNING_SUB_TYPES), 3477 CCS_LIM_BINNING_SUB_TYPE_MAX_N); 3478 3479 for (i = 0; i < sensor->nbinning_subtypes; i++) { 3480 sensor->binning_subtypes[i].horizontal = 3481 CCS_LIM_AT(sensor, BINNING_SUB_TYPE, i) >> 3482 CCS_BINNING_SUB_TYPE_COLUMN_SHIFT; 3483 sensor->binning_subtypes[i].vertical = 3484 CCS_LIM_AT(sensor, BINNING_SUB_TYPE, i) & 3485 CCS_BINNING_SUB_TYPE_ROW_MASK; 3486 3487 dev_dbg(&client->dev, "binning %xx%x\n", 3488 sensor->binning_subtypes[i].horizontal, 3489 sensor->binning_subtypes[i].vertical); 3490 } 3491 } 3492 sensor->binning_horizontal = 1; 3493 sensor->binning_vertical = 1; 3494 3495 if (device_create_file(&client->dev, &dev_attr_ident) != 0) { 3496 dev_err(&client->dev, "sysfs ident entry creation failed\n"); 3497 rval = -ENOENT; 3498 goto out_free_ccs_limits; 3499 } 3500 3501 if (sensor->minfo.smiapp_version && 3502 CCS_LIM(sensor, DATA_TRANSFER_IF_CAPABILITY) & 3503 CCS_DATA_TRANSFER_IF_CAPABILITY_SUPPORTED) { 3504 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) { 3505 dev_err(&client->dev, "sysfs nvm entry failed\n"); 3506 rval = -EBUSY; 3507 goto out_cleanup; 3508 } 3509 } 3510 3511 if (!CCS_LIM(sensor, MIN_OP_SYS_CLK_DIV) || 3512 !CCS_LIM(sensor, MAX_OP_SYS_CLK_DIV) || 3513 !CCS_LIM(sensor, MIN_OP_PIX_CLK_DIV) || 3514 !CCS_LIM(sensor, MAX_OP_PIX_CLK_DIV)) { 3515 /* No OP clock branch */ 3516 sensor->pll.flags |= CCS_PLL_FLAG_NO_OP_CLOCKS; 3517 } else if (CCS_LIM(sensor, SCALING_CAPABILITY) 3518 != CCS_SCALING_CAPABILITY_NONE || 3519 CCS_LIM(sensor, DIGITAL_CROP_CAPABILITY) 3520 == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP) { 3521 /* We have a scaler or digital crop. */ 3522 sensor->scaler = &sensor->ssds[sensor->ssds_used]; 3523 sensor->ssds_used++; 3524 } 3525 sensor->binner = &sensor->ssds[sensor->ssds_used]; 3526 sensor->ssds_used++; 3527 sensor->pixel_array = &sensor->ssds[sensor->ssds_used]; 3528 sensor->ssds_used++; 3529 3530 sensor->scale_m = CCS_LIM(sensor, SCALER_N_MIN); 3531 3532 /* prepare PLL configuration input values */ 3533 sensor->pll.bus_type = CCS_PLL_BUS_TYPE_CSI2_DPHY; 3534 sensor->pll.csi2.lanes = sensor->hwcfg.lanes; 3535 if (CCS_LIM(sensor, CLOCK_CALCULATION) & 3536 CCS_CLOCK_CALCULATION_LANE_SPEED) { 3537 sensor->pll.flags |= CCS_PLL_FLAG_LANE_SPEED_MODEL; 3538 if (CCS_LIM(sensor, CLOCK_CALCULATION) & 3539 CCS_CLOCK_CALCULATION_LINK_DECOUPLED) { 3540 sensor->pll.vt_lanes = 3541 CCS_LIM(sensor, NUM_OF_VT_LANES) + 1; 3542 sensor->pll.op_lanes = 3543 CCS_LIM(sensor, NUM_OF_OP_LANES) + 1; 3544 sensor->pll.flags |= CCS_PLL_FLAG_LINK_DECOUPLED; 3545 } else { 3546 sensor->pll.vt_lanes = sensor->pll.csi2.lanes; 3547 sensor->pll.op_lanes = sensor->pll.csi2.lanes; 3548 } 3549 } 3550 if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) & 3551 CCS_CLOCK_TREE_PLL_CAPABILITY_EXT_DIVIDER) 3552 sensor->pll.flags |= CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER; 3553 if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) & 3554 CCS_CLOCK_TREE_PLL_CAPABILITY_FLEXIBLE_OP_PIX_CLK_DIV) 3555 sensor->pll.flags |= CCS_PLL_FLAG_FLEXIBLE_OP_PIX_CLK_DIV; 3556 if (CCS_LIM(sensor, FIFO_SUPPORT_CAPABILITY) & 3557 CCS_FIFO_SUPPORT_CAPABILITY_DERATING) 3558 sensor->pll.flags |= CCS_PLL_FLAG_FIFO_DERATING; 3559 if (CCS_LIM(sensor, FIFO_SUPPORT_CAPABILITY) & 3560 CCS_FIFO_SUPPORT_CAPABILITY_DERATING_OVERRATING) 3561 sensor->pll.flags |= CCS_PLL_FLAG_FIFO_DERATING | 3562 CCS_PLL_FLAG_FIFO_OVERRATING; 3563 if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) & 3564 CCS_CLOCK_TREE_PLL_CAPABILITY_DUAL_PLL) { 3565 if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) & 3566 CCS_CLOCK_TREE_PLL_CAPABILITY_SINGLE_PLL) { 3567 u32 v; 3568 3569 /* Use sensor default in PLL mode selection */ 3570 rval = ccs_read(sensor, PLL_MODE, &v); 3571 if (rval) 3572 goto out_cleanup; 3573 3574 if (v == CCS_PLL_MODE_DUAL) 3575 sensor->pll.flags |= CCS_PLL_FLAG_DUAL_PLL; 3576 } else { 3577 sensor->pll.flags |= CCS_PLL_FLAG_DUAL_PLL; 3578 } 3579 if (CCS_LIM(sensor, CLOCK_CALCULATION) & 3580 CCS_CLOCK_CALCULATION_DUAL_PLL_OP_SYS_DDR) 3581 sensor->pll.flags |= CCS_PLL_FLAG_OP_SYS_DDR; 3582 if (CCS_LIM(sensor, CLOCK_CALCULATION) & 3583 CCS_CLOCK_CALCULATION_DUAL_PLL_OP_PIX_DDR) 3584 sensor->pll.flags |= CCS_PLL_FLAG_OP_PIX_DDR; 3585 } 3586 sensor->pll.op_bits_per_lane = CCS_LIM(sensor, OP_BITS_PER_LANE); 3587 sensor->pll.ext_clk_freq_hz = sensor->hwcfg.ext_clk; 3588 sensor->pll.scale_n = CCS_LIM(sensor, SCALER_N_MIN); 3589 3590 ccs_create_subdev(sensor, sensor->scaler, " scaler", 2, 3591 MEDIA_ENT_F_PROC_VIDEO_SCALER); 3592 ccs_create_subdev(sensor, sensor->binner, " binner", 2, 3593 MEDIA_ENT_F_PROC_VIDEO_SCALER); 3594 ccs_create_subdev(sensor, sensor->pixel_array, " pixel_array", 1, 3595 MEDIA_ENT_F_CAM_SENSOR); 3596 3597 rval = ccs_init_controls(sensor); 3598 if (rval < 0) 3599 goto out_cleanup; 3600 3601 rval = ccs_call_quirk(sensor, init); 3602 if (rval) 3603 goto out_cleanup; 3604 3605 rval = ccs_get_mbus_formats(sensor); 3606 if (rval) { 3607 rval = -ENODEV; 3608 goto out_cleanup; 3609 } 3610 3611 rval = ccs_init_late_controls(sensor); 3612 if (rval) { 3613 rval = -ENODEV; 3614 goto out_cleanup; 3615 } 3616 3617 mutex_lock(&sensor->mutex); 3618 rval = ccs_pll_blanking_update(sensor); 3619 mutex_unlock(&sensor->mutex); 3620 if (rval) { 3621 dev_err(&client->dev, "update mode failed\n"); 3622 goto out_cleanup; 3623 } 3624 3625 sensor->streaming = false; 3626 sensor->dev_init_done = true; 3627 3628 rval = media_entity_pads_init(&sensor->src->sd.entity, 2, 3629 sensor->src->pads); 3630 if (rval < 0) 3631 goto out_media_entity_cleanup; 3632 3633 rval = ccs_write_msr_regs(sensor); 3634 if (rval) 3635 goto out_media_entity_cleanup; 3636 3637 pm_runtime_set_active(&client->dev); 3638 pm_runtime_get_noresume(&client->dev); 3639 pm_runtime_enable(&client->dev); 3640 3641 rval = v4l2_async_register_subdev_sensor(&sensor->src->sd); 3642 if (rval < 0) 3643 goto out_disable_runtime_pm; 3644 3645 pm_runtime_set_autosuspend_delay(&client->dev, 1000); 3646 pm_runtime_use_autosuspend(&client->dev); 3647 pm_runtime_put_autosuspend(&client->dev); 3648 3649 return 0; 3650 3651 out_disable_runtime_pm: 3652 pm_runtime_put_noidle(&client->dev); 3653 pm_runtime_disable(&client->dev); 3654 3655 out_media_entity_cleanup: 3656 media_entity_cleanup(&sensor->src->sd.entity); 3657 3658 out_cleanup: 3659 ccs_cleanup(sensor); 3660 3661 out_release_mdata: 3662 kvfree(sensor->mdata.backing); 3663 3664 out_release_sdata: 3665 kvfree(sensor->sdata.backing); 3666 3667 out_free_ccs_limits: 3668 kfree(sensor->ccs_limits); 3669 3670 out_power_off: 3671 ccs_power_off(&client->dev); 3672 mutex_destroy(&sensor->mutex); 3673 3674 return rval; 3675 } 3676 3677 static void ccs_remove(struct i2c_client *client) 3678 { 3679 struct v4l2_subdev *subdev = i2c_get_clientdata(client); 3680 struct ccs_sensor *sensor = to_ccs_sensor(subdev); 3681 unsigned int i; 3682 3683 v4l2_async_unregister_subdev(subdev); 3684 3685 pm_runtime_disable(&client->dev); 3686 if (!pm_runtime_status_suspended(&client->dev)) 3687 ccs_power_off(&client->dev); 3688 pm_runtime_set_suspended(&client->dev); 3689 3690 for (i = 0; i < sensor->ssds_used; i++) { 3691 v4l2_device_unregister_subdev(&sensor->ssds[i].sd); 3692 media_entity_cleanup(&sensor->ssds[i].sd.entity); 3693 } 3694 ccs_cleanup(sensor); 3695 mutex_destroy(&sensor->mutex); 3696 kfree(sensor->ccs_limits); 3697 kvfree(sensor->sdata.backing); 3698 kvfree(sensor->mdata.backing); 3699 } 3700 3701 static const struct ccs_device smia_device = { 3702 .flags = CCS_DEVICE_FLAG_IS_SMIA, 3703 }; 3704 3705 static const struct ccs_device ccs_device = {}; 3706 3707 static const struct acpi_device_id ccs_acpi_table[] = { 3708 { .id = "MIPI0200", .driver_data = (unsigned long)&ccs_device }, 3709 { }, 3710 }; 3711 MODULE_DEVICE_TABLE(acpi, ccs_acpi_table); 3712 3713 static const struct of_device_id ccs_of_table[] = { 3714 { .compatible = "mipi-ccs-1.1", .data = &ccs_device }, 3715 { .compatible = "mipi-ccs-1.0", .data = &ccs_device }, 3716 { .compatible = "mipi-ccs", .data = &ccs_device }, 3717 { .compatible = "nokia,smia", .data = &smia_device }, 3718 { }, 3719 }; 3720 MODULE_DEVICE_TABLE(of, ccs_of_table); 3721 3722 static const struct dev_pm_ops ccs_pm_ops = { 3723 SET_SYSTEM_SLEEP_PM_OPS(ccs_suspend, ccs_resume) 3724 SET_RUNTIME_PM_OPS(ccs_power_off, ccs_power_on, NULL) 3725 }; 3726 3727 static struct i2c_driver ccs_i2c_driver = { 3728 .driver = { 3729 .acpi_match_table = ccs_acpi_table, 3730 .of_match_table = ccs_of_table, 3731 .name = CCS_NAME, 3732 .pm = &ccs_pm_ops, 3733 }, 3734 .probe = ccs_probe, 3735 .remove = ccs_remove, 3736 }; 3737 3738 static int ccs_module_init(void) 3739 { 3740 unsigned int i, l; 3741 3742 for (i = 0, l = 0; ccs_limits[i].size && l < CCS_L_LAST; i++) { 3743 if (!(ccs_limits[i].flags & CCS_L_FL_SAME_REG)) { 3744 ccs_limit_offsets[l + 1].lim = 3745 ALIGN(ccs_limit_offsets[l].lim + 3746 ccs_limits[i].size, 3747 ccs_reg_width(ccs_limits[i + 1].reg)); 3748 ccs_limit_offsets[l].info = i; 3749 l++; 3750 } else { 3751 ccs_limit_offsets[l].lim += ccs_limits[i].size; 3752 } 3753 } 3754 3755 if (WARN_ON(ccs_limits[i].size)) 3756 return -EINVAL; 3757 3758 if (WARN_ON(l != CCS_L_LAST)) 3759 return -EINVAL; 3760 3761 return i2c_register_driver(THIS_MODULE, &ccs_i2c_driver); 3762 } 3763 3764 static void ccs_module_cleanup(void) 3765 { 3766 i2c_del_driver(&ccs_i2c_driver); 3767 } 3768 3769 module_init(ccs_module_init); 3770 module_exit(ccs_module_cleanup); 3771 3772 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>"); 3773 MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ camera sensor driver"); 3774 MODULE_LICENSE("GPL v2"); 3775 MODULE_ALIAS("smiapp"); 3776