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