1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Support for Medifield PNW Camera Imaging ISP subsystem. 4 * 5 * Copyright (c) 2010 Intel Corporation. All Rights Reserved. 6 * 7 * Copyright (c) 2010 Silicon Hive www.siliconhive.com. 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License version 11 * 2 as published by the Free Software Foundation. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * 19 */ 20 #include <linux/errno.h> 21 #include <linux/firmware.h> 22 #include <linux/pci.h> 23 #include <linux/interrupt.h> 24 #include <linux/io.h> 25 #include <linux/kernel.h> 26 #include <linux/kfifo.h> 27 #include <linux/pm_runtime.h> 28 #include <linux/timer.h> 29 30 #include <asm/iosf_mbi.h> 31 32 #include <media/v4l2-event.h> 33 34 #define CREATE_TRACE_POINTS 35 #include "atomisp_trace_event.h" 36 37 #include "atomisp_cmd.h" 38 #include "atomisp_common.h" 39 #include "atomisp_fops.h" 40 #include "atomisp_internal.h" 41 #include "atomisp_ioctl.h" 42 #include "atomisp-regs.h" 43 #include "atomisp_tables.h" 44 #include "atomisp_compat.h" 45 #include "atomisp_subdev.h" 46 #include "atomisp_dfs_tables.h" 47 48 #include <hmm/hmm.h> 49 50 #include "sh_css_hrt.h" 51 #include "sh_css_defs.h" 52 #include "system_global.h" 53 #include "sh_css_internal.h" 54 #include "sh_css_sp.h" 55 #include "gp_device.h" 56 #include "device_access.h" 57 #include "irq.h" 58 59 #include "ia_css_types.h" 60 #include "ia_css_stream.h" 61 #include "ia_css_debug.h" 62 #include "bits.h" 63 64 /* We should never need to run the flash for more than 2 frames. 65 * At 15fps this means 133ms. We set the timeout a bit longer. 66 * Each flash driver is supposed to set its own timeout, but 67 * just in case someone else changed the timeout, we set it 68 * here to make sure we don't damage the flash hardware. */ 69 #define FLASH_TIMEOUT 800 /* ms */ 70 71 union host { 72 struct { 73 void *kernel_ptr; 74 void __user *user_ptr; 75 int size; 76 } scalar; 77 struct { 78 void *hmm_ptr; 79 } ptr; 80 }; 81 82 /* 83 * get sensor:dis71430/ov2720 related info from v4l2_subdev->priv data field. 84 * subdev->priv is set in mrst.c 85 */ 86 struct camera_mipi_info *atomisp_to_sensor_mipi_info(struct v4l2_subdev *sd) 87 { 88 return (struct camera_mipi_info *)v4l2_get_subdev_hostdata(sd); 89 } 90 91 /* 92 * get struct atomisp_video_pipe from v4l2 video_device 93 */ 94 struct atomisp_video_pipe *atomisp_to_video_pipe(struct video_device *dev) 95 { 96 return (struct atomisp_video_pipe *) 97 container_of(dev, struct atomisp_video_pipe, vdev); 98 } 99 100 static unsigned short atomisp_get_sensor_fps(struct atomisp_sub_device *asd) 101 { 102 struct v4l2_subdev_frame_interval fi = { 0 }; 103 struct atomisp_device *isp = asd->isp; 104 105 unsigned short fps = 0; 106 int ret; 107 108 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, 109 video, g_frame_interval, &fi); 110 111 if (!ret && fi.interval.numerator) 112 fps = fi.interval.denominator / fi.interval.numerator; 113 114 return fps; 115 } 116 117 /* 118 * DFS progress is shown as follows: 119 * 1. Target frequency is calculated according to FPS/Resolution/ISP running 120 * mode. 121 * 2. Ratio is calculated using formula: 2 * HPLL / target frequency - 1 122 * with proper rounding. 123 * 3. Set ratio to ISPFREQ40, 1 to FREQVALID and ISPFREQGUAR40 124 * to 200MHz in ISPSSPM1. 125 * 4. Wait for FREQVALID to be cleared by P-Unit. 126 * 5. Wait for field ISPFREQSTAT40 in ISPSSPM1 turn to ratio set in 3. 127 */ 128 static int write_target_freq_to_hw(struct atomisp_device *isp, 129 unsigned int new_freq) 130 { 131 unsigned int ratio, timeout, guar_ratio; 132 u32 isp_sspm1 = 0; 133 int i; 134 135 if (!isp->hpll_freq) { 136 dev_err(isp->dev, "failed to get hpll_freq. no change to freq\n"); 137 return -EINVAL; 138 } 139 140 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1); 141 if (isp_sspm1 & ISP_FREQ_VALID_MASK) { 142 dev_dbg(isp->dev, "clearing ISPSSPM1 valid bit.\n"); 143 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1, 144 isp_sspm1 & ~(1 << ISP_FREQ_VALID_OFFSET)); 145 } 146 147 ratio = (2 * isp->hpll_freq + new_freq / 2) / new_freq - 1; 148 guar_ratio = (2 * isp->hpll_freq + 200 / 2) / 200 - 1; 149 150 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1); 151 isp_sspm1 &= ~(0x1F << ISP_REQ_FREQ_OFFSET); 152 153 for (i = 0; i < ISP_DFS_TRY_TIMES; i++) { 154 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1, 155 isp_sspm1 156 | ratio << ISP_REQ_FREQ_OFFSET 157 | 1 << ISP_FREQ_VALID_OFFSET 158 | guar_ratio << ISP_REQ_GUAR_FREQ_OFFSET); 159 160 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1); 161 timeout = 20; 162 while ((isp_sspm1 & ISP_FREQ_VALID_MASK) && timeout) { 163 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1); 164 dev_dbg(isp->dev, "waiting for ISPSSPM1 valid bit to be 0.\n"); 165 udelay(100); 166 timeout--; 167 } 168 169 if (timeout != 0) 170 break; 171 } 172 173 if (timeout == 0) { 174 dev_err(isp->dev, "DFS failed due to HW error.\n"); 175 return -EINVAL; 176 } 177 178 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1); 179 timeout = 10; 180 while (((isp_sspm1 >> ISP_FREQ_STAT_OFFSET) != ratio) && timeout) { 181 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1); 182 dev_dbg(isp->dev, "waiting for ISPSSPM1 status bit to be 0x%x.\n", 183 new_freq); 184 udelay(100); 185 timeout--; 186 } 187 if (timeout == 0) { 188 dev_err(isp->dev, "DFS target freq is rejected by HW.\n"); 189 return -EINVAL; 190 } 191 192 return 0; 193 } 194 195 int atomisp_freq_scaling(struct atomisp_device *isp, 196 enum atomisp_dfs_mode mode, 197 bool force) 198 { 199 const struct atomisp_dfs_config *dfs; 200 unsigned int new_freq; 201 struct atomisp_freq_scaling_rule curr_rules; 202 int i, ret; 203 unsigned short fps = 0; 204 205 dfs = isp->dfs; 206 207 if (dfs->lowest_freq == 0 || dfs->max_freq_at_vmin == 0 || 208 dfs->highest_freq == 0 || dfs->dfs_table_size == 0 || 209 !dfs->dfs_table) { 210 dev_err(isp->dev, "DFS configuration is invalid.\n"); 211 return -EINVAL; 212 } 213 214 if (mode == ATOMISP_DFS_MODE_LOW) { 215 new_freq = dfs->lowest_freq; 216 goto done; 217 } 218 219 if (mode == ATOMISP_DFS_MODE_MAX) { 220 new_freq = dfs->highest_freq; 221 goto done; 222 } 223 224 fps = atomisp_get_sensor_fps(&isp->asd); 225 if (fps == 0) { 226 dev_info(isp->dev, 227 "Sensor didn't report FPS. Using DFS max mode.\n"); 228 new_freq = dfs->highest_freq; 229 goto done; 230 } 231 232 curr_rules.width = isp->asd.fmt[ATOMISP_SUBDEV_PAD_SOURCE].fmt.width; 233 curr_rules.height = isp->asd.fmt[ATOMISP_SUBDEV_PAD_SOURCE].fmt.height; 234 curr_rules.fps = fps; 235 curr_rules.run_mode = isp->asd.run_mode->val; 236 237 /* search for the target frequency by looping freq rules*/ 238 for (i = 0; i < dfs->dfs_table_size; i++) { 239 if (curr_rules.width != dfs->dfs_table[i].width && 240 dfs->dfs_table[i].width != ISP_FREQ_RULE_ANY) 241 continue; 242 if (curr_rules.height != dfs->dfs_table[i].height && 243 dfs->dfs_table[i].height != ISP_FREQ_RULE_ANY) 244 continue; 245 if (curr_rules.fps != dfs->dfs_table[i].fps && 246 dfs->dfs_table[i].fps != ISP_FREQ_RULE_ANY) 247 continue; 248 if (curr_rules.run_mode != dfs->dfs_table[i].run_mode && 249 dfs->dfs_table[i].run_mode != ISP_FREQ_RULE_ANY) 250 continue; 251 break; 252 } 253 254 if (i == dfs->dfs_table_size) 255 new_freq = dfs->max_freq_at_vmin; 256 else 257 new_freq = dfs->dfs_table[i].isp_freq; 258 259 done: 260 dev_dbg(isp->dev, "DFS target frequency=%d.\n", new_freq); 261 262 if ((new_freq == isp->running_freq) && !force) 263 return 0; 264 265 dev_dbg(isp->dev, "Programming DFS frequency to %d\n", new_freq); 266 267 ret = write_target_freq_to_hw(isp, new_freq); 268 if (!ret) { 269 isp->running_freq = new_freq; 270 trace_ipu_pstate(new_freq, -1); 271 } 272 return ret; 273 } 274 275 /* 276 * reset and restore ISP 277 */ 278 int atomisp_reset(struct atomisp_device *isp) 279 { 280 /* Reset ISP by power-cycling it */ 281 int ret = 0; 282 283 dev_dbg(isp->dev, "%s\n", __func__); 284 285 ret = atomisp_power_off(isp->dev); 286 if (ret < 0) 287 dev_err(isp->dev, "atomisp_power_off failed, %d\n", ret); 288 289 ret = atomisp_power_on(isp->dev); 290 if (ret < 0) { 291 dev_err(isp->dev, "atomisp_power_on failed, %d\n", ret); 292 isp->isp_fatal_error = true; 293 } 294 295 return ret; 296 } 297 298 /* 299 * interrupt disable functions 300 */ 301 static void disable_isp_irq(enum hrt_isp_css_irq irq) 302 { 303 irq_disable_channel(IRQ0_ID, irq); 304 305 if (irq != hrt_isp_css_irq_sp) 306 return; 307 308 cnd_sp_irq_enable(SP0_ID, false); 309 } 310 311 /* 312 * interrupt clean function 313 */ 314 static void clear_isp_irq(enum hrt_isp_css_irq irq) 315 { 316 irq_clear_all(IRQ0_ID); 317 } 318 319 void atomisp_msi_irq_init(struct atomisp_device *isp) 320 { 321 struct pci_dev *pdev = to_pci_dev(isp->dev); 322 u32 msg32; 323 u16 msg16; 324 325 pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32); 326 msg32 |= 1 << MSI_ENABLE_BIT; 327 pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32); 328 329 msg32 = (1 << INTR_IER) | (1 << INTR_IIR); 330 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32); 331 332 pci_read_config_word(pdev, PCI_COMMAND, &msg16); 333 msg16 |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | 334 PCI_COMMAND_INTX_DISABLE); 335 pci_write_config_word(pdev, PCI_COMMAND, msg16); 336 } 337 338 void atomisp_msi_irq_uninit(struct atomisp_device *isp) 339 { 340 struct pci_dev *pdev = to_pci_dev(isp->dev); 341 u32 msg32; 342 u16 msg16; 343 344 pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32); 345 msg32 &= ~(1 << MSI_ENABLE_BIT); 346 pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32); 347 348 msg32 = 0x0; 349 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32); 350 351 pci_read_config_word(pdev, PCI_COMMAND, &msg16); 352 msg16 &= ~(PCI_COMMAND_MASTER); 353 pci_write_config_word(pdev, PCI_COMMAND, msg16); 354 } 355 356 static void atomisp_sof_event(struct atomisp_sub_device *asd) 357 { 358 struct v4l2_event event = {0}; 359 360 event.type = V4L2_EVENT_FRAME_SYNC; 361 event.u.frame_sync.frame_sequence = atomic_read(&asd->sof_count); 362 363 v4l2_event_queue(asd->subdev.devnode, &event); 364 } 365 366 void atomisp_eof_event(struct atomisp_sub_device *asd, uint8_t exp_id) 367 { 368 struct v4l2_event event = {0}; 369 370 event.type = V4L2_EVENT_FRAME_END; 371 event.u.frame_sync.frame_sequence = exp_id; 372 373 v4l2_event_queue(asd->subdev.devnode, &event); 374 } 375 376 static void atomisp_3a_stats_ready_event(struct atomisp_sub_device *asd, 377 uint8_t exp_id) 378 { 379 struct v4l2_event event = {0}; 380 381 event.type = V4L2_EVENT_ATOMISP_3A_STATS_READY; 382 event.u.frame_sync.frame_sequence = exp_id; 383 384 v4l2_event_queue(asd->subdev.devnode, &event); 385 } 386 387 static void atomisp_metadata_ready_event(struct atomisp_sub_device *asd, 388 enum atomisp_metadata_type md_type) 389 { 390 struct v4l2_event event = {0}; 391 392 event.type = V4L2_EVENT_ATOMISP_METADATA_READY; 393 event.u.data[0] = md_type; 394 395 v4l2_event_queue(asd->subdev.devnode, &event); 396 } 397 398 static void atomisp_reset_event(struct atomisp_sub_device *asd) 399 { 400 struct v4l2_event event = {0}; 401 402 event.type = V4L2_EVENT_ATOMISP_CSS_RESET; 403 404 v4l2_event_queue(asd->subdev.devnode, &event); 405 } 406 407 static void print_csi_rx_errors(enum mipi_port_id port, 408 struct atomisp_device *isp) 409 { 410 u32 infos = 0; 411 412 atomisp_css_rx_get_irq_info(port, &infos); 413 414 dev_err(isp->dev, "CSI Receiver port %d errors:\n", port); 415 if (infos & IA_CSS_RX_IRQ_INFO_BUFFER_OVERRUN) 416 dev_err(isp->dev, " buffer overrun"); 417 if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT) 418 dev_err(isp->dev, " start-of-transmission error"); 419 if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT_SYNC) 420 dev_err(isp->dev, " start-of-transmission sync error"); 421 if (infos & IA_CSS_RX_IRQ_INFO_ERR_CONTROL) 422 dev_err(isp->dev, " control error"); 423 if (infos & IA_CSS_RX_IRQ_INFO_ERR_ECC_DOUBLE) 424 dev_err(isp->dev, " 2 or more ECC errors"); 425 if (infos & IA_CSS_RX_IRQ_INFO_ERR_CRC) 426 dev_err(isp->dev, " CRC mismatch"); 427 if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ID) 428 dev_err(isp->dev, " unknown error"); 429 if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_SYNC) 430 dev_err(isp->dev, " frame sync error"); 431 if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_DATA) 432 dev_err(isp->dev, " frame data error"); 433 if (infos & IA_CSS_RX_IRQ_INFO_ERR_DATA_TIMEOUT) 434 dev_err(isp->dev, " data timeout"); 435 if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ESC) 436 dev_err(isp->dev, " unknown escape command entry"); 437 if (infos & IA_CSS_RX_IRQ_INFO_ERR_LINE_SYNC) 438 dev_err(isp->dev, " line sync error"); 439 } 440 441 /* Clear irq reg */ 442 static void clear_irq_reg(struct atomisp_device *isp) 443 { 444 struct pci_dev *pdev = to_pci_dev(isp->dev); 445 u32 msg_ret; 446 447 pci_read_config_dword(pdev, PCI_INTERRUPT_CTRL, &msg_ret); 448 msg_ret |= 1 << INTR_IIR; 449 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg_ret); 450 } 451 452 /* interrupt handling function*/ 453 irqreturn_t atomisp_isr(int irq, void *dev) 454 { 455 struct atomisp_device *isp = (struct atomisp_device *)dev; 456 struct atomisp_css_event eof_event; 457 unsigned int irq_infos = 0; 458 unsigned long flags; 459 int err; 460 461 spin_lock_irqsave(&isp->lock, flags); 462 463 if (!isp->css_initialized) { 464 spin_unlock_irqrestore(&isp->lock, flags); 465 return IRQ_HANDLED; 466 } 467 err = atomisp_css_irq_translate(isp, &irq_infos); 468 if (err) { 469 spin_unlock_irqrestore(&isp->lock, flags); 470 return IRQ_NONE; 471 } 472 473 clear_irq_reg(isp); 474 475 if (!isp->asd.streaming) 476 goto out_nowake; 477 478 if (irq_infos & IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF) { 479 atomic_inc(&isp->asd.sof_count); 480 atomisp_sof_event(&isp->asd); 481 482 /* 483 * If sequence_temp and sequence are the same there where no frames 484 * lost so we can increase sequence_temp. 485 * If not then processing of frame is still in progress and driver 486 * needs to keep old sequence_temp value. 487 * NOTE: There is assumption here that ISP will not start processing 488 * next frame from sensor before old one is completely done. 489 */ 490 if (atomic_read(&isp->asd.sequence) == atomic_read(&isp->asd.sequence_temp)) 491 atomic_set(&isp->asd.sequence_temp, atomic_read(&isp->asd.sof_count)); 492 493 dev_dbg_ratelimited(isp->dev, "irq:0x%x (SOF)\n", irq_infos); 494 irq_infos &= ~IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF; 495 } 496 497 if (irq_infos & IA_CSS_IRQ_INFO_EVENTS_READY) 498 atomic_set(&isp->asd.sequence, atomic_read(&isp->asd.sequence_temp)); 499 500 if ((irq_infos & IA_CSS_IRQ_INFO_INPUT_SYSTEM_ERROR) || 501 (irq_infos & IA_CSS_IRQ_INFO_IF_ERROR)) { 502 /* handle mipi receiver error */ 503 u32 rx_infos; 504 enum mipi_port_id port; 505 506 for (port = MIPI_PORT0_ID; port <= MIPI_PORT2_ID; 507 port++) { 508 print_csi_rx_errors(port, isp); 509 atomisp_css_rx_get_irq_info(port, &rx_infos); 510 atomisp_css_rx_clear_irq_info(port, rx_infos); 511 } 512 } 513 514 if (irq_infos & IA_CSS_IRQ_INFO_ISYS_EVENTS_READY) { 515 while (ia_css_dequeue_isys_event(&eof_event.event) == 0) { 516 atomisp_eof_event(&isp->asd, eof_event.event.exp_id); 517 dev_dbg_ratelimited(isp->dev, "ISYS event: EOF exp_id %d\n", 518 eof_event.event.exp_id); 519 } 520 521 irq_infos &= ~IA_CSS_IRQ_INFO_ISYS_EVENTS_READY; 522 if (irq_infos == 0) 523 goto out_nowake; 524 } 525 526 spin_unlock_irqrestore(&isp->lock, flags); 527 528 dev_dbg_ratelimited(isp->dev, "irq:0x%x (unhandled)\n", irq_infos); 529 530 return IRQ_WAKE_THREAD; 531 532 out_nowake: 533 spin_unlock_irqrestore(&isp->lock, flags); 534 535 if (irq_infos) 536 dev_dbg_ratelimited(isp->dev, "irq:0x%x (ignored, as not streaming anymore)\n", 537 irq_infos); 538 539 return IRQ_HANDLED; 540 } 541 542 void atomisp_clear_css_buffer_counters(struct atomisp_sub_device *asd) 543 { 544 int i; 545 546 memset(asd->s3a_bufs_in_css, 0, sizeof(asd->s3a_bufs_in_css)); 547 for (i = 0; i < ATOMISP_INPUT_STREAM_NUM; i++) 548 memset(asd->metadata_bufs_in_css[i], 0, 549 sizeof(asd->metadata_bufs_in_css[i])); 550 asd->dis_bufs_in_css = 0; 551 } 552 553 /* 0x100000 is the start of dmem inside SP */ 554 #define SP_DMEM_BASE 0x100000 555 556 void dump_sp_dmem(struct atomisp_device *isp, unsigned int addr, 557 unsigned int size) 558 { 559 unsigned int data = 0; 560 unsigned int size32 = DIV_ROUND_UP(size, sizeof(u32)); 561 562 dev_dbg(isp->dev, "atomisp mmio base: %p\n", isp->base); 563 dev_dbg(isp->dev, "%s, addr:0x%x, size: %d, size32: %d\n", __func__, 564 addr, size, size32); 565 if (size32 * 4 + addr > 0x4000) { 566 dev_err(isp->dev, "illegal size (%d) or addr (0x%x)\n", 567 size32, addr); 568 return; 569 } 570 addr += SP_DMEM_BASE; 571 addr &= 0x003FFFFF; 572 do { 573 data = readl(isp->base + addr); 574 dev_dbg(isp->dev, "%s, \t [0x%x]:0x%x\n", __func__, addr, data); 575 addr += sizeof(u32); 576 } while (--size32); 577 } 578 579 int atomisp_buffers_in_css(struct atomisp_video_pipe *pipe) 580 { 581 unsigned long irqflags; 582 struct list_head *pos; 583 int buffers_in_css = 0; 584 585 spin_lock_irqsave(&pipe->irq_lock, irqflags); 586 587 list_for_each(pos, &pipe->buffers_in_css) 588 buffers_in_css++; 589 590 spin_unlock_irqrestore(&pipe->irq_lock, irqflags); 591 592 return buffers_in_css; 593 } 594 595 void atomisp_buffer_done(struct ia_css_frame *frame, enum vb2_buffer_state state) 596 { 597 struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf); 598 599 lockdep_assert_held(&pipe->irq_lock); 600 601 frame->vb.vb2_buf.timestamp = ktime_get_ns(); 602 frame->vb.field = pipe->pix.field; 603 frame->vb.sequence = atomic_read(&pipe->asd->sequence); 604 list_del(&frame->queue); 605 if (state == VB2_BUF_STATE_DONE) 606 vb2_set_plane_payload(&frame->vb.vb2_buf, 0, pipe->pix.sizeimage); 607 vb2_buffer_done(&frame->vb.vb2_buf, state); 608 } 609 610 void atomisp_flush_video_pipe(struct atomisp_video_pipe *pipe, enum vb2_buffer_state state, 611 bool warn_on_css_frames) 612 { 613 struct ia_css_frame *frame, *_frame; 614 unsigned long irqflags; 615 616 spin_lock_irqsave(&pipe->irq_lock, irqflags); 617 618 list_for_each_entry_safe(frame, _frame, &pipe->buffers_in_css, queue) { 619 if (warn_on_css_frames) 620 dev_warn(pipe->isp->dev, "Warning: CSS frames queued on flush\n"); 621 atomisp_buffer_done(frame, state); 622 } 623 624 list_for_each_entry_safe(frame, _frame, &pipe->activeq, queue) 625 atomisp_buffer_done(frame, state); 626 627 list_for_each_entry_safe(frame, _frame, &pipe->buffers_waiting_for_param, queue) { 628 pipe->frame_request_config_id[frame->vb.vb2_buf.index] = 0; 629 atomisp_buffer_done(frame, state); 630 } 631 632 spin_unlock_irqrestore(&pipe->irq_lock, irqflags); 633 } 634 635 /* clean out the parameters that did not apply */ 636 void atomisp_flush_params_queue(struct atomisp_video_pipe *pipe) 637 { 638 struct atomisp_css_params_with_list *param; 639 640 while (!list_empty(&pipe->per_frame_params)) { 641 param = list_entry(pipe->per_frame_params.next, 642 struct atomisp_css_params_with_list, list); 643 list_del(¶m->list); 644 atomisp_free_css_parameters(¶m->params); 645 kvfree(param); 646 } 647 } 648 649 /* Re-queue per-frame parameters */ 650 static void atomisp_recover_params_queue(struct atomisp_video_pipe *pipe) 651 { 652 struct atomisp_css_params_with_list *param; 653 int i; 654 655 for (i = 0; i < VIDEO_MAX_FRAME; i++) { 656 param = pipe->frame_params[i]; 657 if (param) 658 list_add_tail(¶m->list, &pipe->per_frame_params); 659 pipe->frame_params[i] = NULL; 660 } 661 atomisp_handle_parameter_and_buffer(pipe); 662 } 663 664 void atomisp_buf_done(struct atomisp_sub_device *asd, int error, 665 enum ia_css_buffer_type buf_type, 666 enum ia_css_pipe_id css_pipe_id, 667 bool q_buffers, enum atomisp_input_stream_id stream_id) 668 { 669 struct atomisp_video_pipe *pipe = NULL; 670 struct atomisp_css_buffer buffer; 671 bool requeue = false; 672 unsigned long irqflags; 673 struct ia_css_frame *frame = NULL; 674 struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf_tmp, *s3a_iter; 675 struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf_tmp, *dis_iter; 676 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf_tmp, *md_iter; 677 enum atomisp_metadata_type md_type; 678 struct atomisp_device *isp = asd->isp; 679 struct v4l2_control ctrl; 680 int i, err; 681 682 lockdep_assert_held(&isp->mutex); 683 684 if ( 685 buf_type != IA_CSS_BUFFER_TYPE_METADATA && 686 buf_type != IA_CSS_BUFFER_TYPE_3A_STATISTICS && 687 buf_type != IA_CSS_BUFFER_TYPE_DIS_STATISTICS && 688 buf_type != IA_CSS_BUFFER_TYPE_OUTPUT_FRAME && 689 buf_type != IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME && 690 buf_type != IA_CSS_BUFFER_TYPE_RAW_OUTPUT_FRAME && 691 buf_type != IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME && 692 buf_type != IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) { 693 dev_err(isp->dev, "%s, unsupported buffer type: %d\n", 694 __func__, buf_type); 695 return; 696 } 697 698 memset(&buffer, 0, sizeof(struct atomisp_css_buffer)); 699 buffer.css_buffer.type = buf_type; 700 err = atomisp_css_dequeue_buffer(asd, stream_id, css_pipe_id, 701 buf_type, &buffer); 702 if (err) { 703 dev_err(isp->dev, 704 "atomisp_css_dequeue_buffer failed: 0x%x\n", err); 705 return; 706 } 707 708 switch (buf_type) { 709 case IA_CSS_BUFFER_TYPE_3A_STATISTICS: 710 list_for_each_entry_safe(s3a_iter, _s3a_buf_tmp, 711 &asd->s3a_stats_in_css, list) { 712 if (s3a_iter->s3a_data == 713 buffer.css_buffer.data.stats_3a) { 714 list_del_init(&s3a_iter->list); 715 list_add_tail(&s3a_iter->list, 716 &asd->s3a_stats_ready); 717 s3a_buf = s3a_iter; 718 break; 719 } 720 } 721 722 asd->s3a_bufs_in_css[css_pipe_id]--; 723 atomisp_3a_stats_ready_event(asd, buffer.css_buffer.exp_id); 724 if (s3a_buf) 725 dev_dbg(isp->dev, "%s: s3a stat with exp_id %d is ready\n", 726 __func__, s3a_buf->s3a_data->exp_id); 727 else 728 dev_dbg(isp->dev, "%s: s3a stat is ready with no exp_id found\n", 729 __func__); 730 break; 731 case IA_CSS_BUFFER_TYPE_METADATA: 732 if (error) 733 break; 734 735 md_type = ATOMISP_MAIN_METADATA; 736 list_for_each_entry_safe(md_iter, _md_buf_tmp, 737 &asd->metadata_in_css[md_type], list) { 738 if (md_iter->metadata == 739 buffer.css_buffer.data.metadata) { 740 list_del_init(&md_iter->list); 741 list_add_tail(&md_iter->list, 742 &asd->metadata_ready[md_type]); 743 md_buf = md_iter; 744 break; 745 } 746 } 747 asd->metadata_bufs_in_css[stream_id][css_pipe_id]--; 748 atomisp_metadata_ready_event(asd, md_type); 749 if (md_buf) 750 dev_dbg(isp->dev, "%s: metadata with exp_id %d is ready\n", 751 __func__, md_buf->metadata->exp_id); 752 else 753 dev_dbg(isp->dev, "%s: metadata is ready with no exp_id found\n", 754 __func__); 755 break; 756 case IA_CSS_BUFFER_TYPE_DIS_STATISTICS: 757 list_for_each_entry_safe(dis_iter, _dis_buf_tmp, 758 &asd->dis_stats_in_css, list) { 759 if (dis_iter->dis_data == 760 buffer.css_buffer.data.stats_dvs) { 761 spin_lock_irqsave(&asd->dis_stats_lock, 762 irqflags); 763 list_del_init(&dis_iter->list); 764 list_add(&dis_iter->list, &asd->dis_stats); 765 asd->params.dis_proj_data_valid = true; 766 spin_unlock_irqrestore(&asd->dis_stats_lock, 767 irqflags); 768 dis_buf = dis_iter; 769 break; 770 } 771 } 772 asd->dis_bufs_in_css--; 773 if (dis_buf) 774 dev_dbg(isp->dev, "%s: dis stat with exp_id %d is ready\n", 775 __func__, dis_buf->dis_data->exp_id); 776 else 777 dev_dbg(isp->dev, "%s: dis stat is ready with no exp_id found\n", 778 __func__); 779 break; 780 case IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME: 781 case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME: 782 frame = buffer.css_buffer.data.frame; 783 if (!frame) { 784 WARN_ON(1); 785 break; 786 } 787 if (!frame->valid) 788 error = true; 789 790 pipe = vb_to_pipe(&frame->vb.vb2_buf); 791 792 dev_dbg(isp->dev, "%s: vf frame with exp_id %d is ready\n", 793 __func__, frame->exp_id); 794 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) { 795 if (frame->flash_state 796 == IA_CSS_FRAME_FLASH_STATE_PARTIAL) 797 dev_dbg(isp->dev, "%s thumb partially flashed\n", 798 __func__); 799 else if (frame->flash_state 800 == IA_CSS_FRAME_FLASH_STATE_FULL) 801 dev_dbg(isp->dev, "%s thumb completely flashed\n", 802 __func__); 803 else 804 dev_dbg(isp->dev, "%s thumb no flash in this frame\n", 805 __func__); 806 } 807 pipe->frame_config_id[frame->vb.vb2_buf.index] = frame->isp_config_id; 808 break; 809 case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME: 810 case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME: 811 frame = buffer.css_buffer.data.frame; 812 if (!frame) { 813 WARN_ON(1); 814 break; 815 } 816 817 if (!frame->valid) 818 error = true; 819 820 pipe = vb_to_pipe(&frame->vb.vb2_buf); 821 822 dev_dbg(isp->dev, "%s: main frame with exp_id %d is ready\n", 823 __func__, frame->exp_id); 824 825 i = frame->vb.vb2_buf.index; 826 827 /* free the parameters */ 828 if (pipe->frame_params[i]) { 829 if (asd->params.dvs_6axis == pipe->frame_params[i]->params.dvs_6axis) 830 asd->params.dvs_6axis = NULL; 831 atomisp_free_css_parameters(&pipe->frame_params[i]->params); 832 kvfree(pipe->frame_params[i]); 833 pipe->frame_params[i] = NULL; 834 } 835 836 pipe->frame_config_id[i] = frame->isp_config_id; 837 ctrl.id = V4L2_CID_FLASH_MODE; 838 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) { 839 if (frame->flash_state == IA_CSS_FRAME_FLASH_STATE_PARTIAL) { 840 asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_PARTIAL; 841 dev_dbg(isp->dev, "%s partially flashed\n", __func__); 842 } else if (frame->flash_state == IA_CSS_FRAME_FLASH_STATE_FULL) { 843 asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_EXPOSED; 844 asd->params.num_flash_frames--; 845 dev_dbg(isp->dev, "%s completely flashed\n", __func__); 846 } else { 847 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK; 848 dev_dbg(isp->dev, "%s no flash in this frame\n", __func__); 849 } 850 851 /* Check if flashing sequence is done */ 852 if (asd->frame_status[i] == ATOMISP_FRAME_STATUS_FLASH_EXPOSED) 853 asd->params.flash_state = ATOMISP_FLASH_DONE; 854 } else if (isp->flash) { 855 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) == 0 && 856 ctrl.value == ATOMISP_FLASH_MODE_TORCH) { 857 ctrl.id = V4L2_CID_FLASH_TORCH_INTENSITY; 858 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) == 0 && 859 ctrl.value > 0) 860 asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_EXPOSED; 861 else 862 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK; 863 } else { 864 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK; 865 } 866 } else { 867 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK; 868 } 869 870 asd->params.last_frame_status = asd->frame_status[i]; 871 872 if (asd->params.css_update_params_needed) { 873 atomisp_apply_css_parameters(asd, 874 &asd->params.css_param); 875 if (asd->params.css_param.update_flag.dz_config) 876 asd->params.config.dz_config = &asd->params.css_param.dz_config; 877 /* New global dvs 6axis config should be blocked 878 * here if there's a buffer with per-frame parameters 879 * pending in CSS frame buffer queue. 880 * This is to aviod zooming vibration since global 881 * parameters take effect immediately while 882 * per-frame parameters are taken after previous 883 * buffers in CSS got processed. 884 */ 885 if (asd->params.dvs_6axis) 886 atomisp_css_set_dvs_6axis(asd, 887 asd->params.dvs_6axis); 888 else 889 asd->params.css_update_params_needed = false; 890 /* The update flag should not be cleaned here 891 * since it is still going to be used to make up 892 * following per-frame parameters. 893 * This will introduce more copy work since each 894 * time when updating global parameters, the whole 895 * parameter set are applied. 896 * FIXME: A new set of parameter copy functions can 897 * be added to make up per-frame parameters based on 898 * solid structures stored in asd->params.css_param 899 * instead of using shadow pointers in update flag. 900 */ 901 atomisp_css_update_isp_params(asd); 902 } 903 break; 904 default: 905 break; 906 } 907 if (frame) { 908 spin_lock_irqsave(&pipe->irq_lock, irqflags); 909 atomisp_buffer_done(frame, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); 910 spin_unlock_irqrestore(&pipe->irq_lock, irqflags); 911 } 912 913 /* 914 * Requeue should only be done for 3a and dis buffers. 915 * Queue/dequeue order will change if driver recycles image buffers. 916 */ 917 if (requeue) { 918 err = atomisp_css_queue_buffer(asd, 919 stream_id, css_pipe_id, 920 buf_type, &buffer); 921 if (err) 922 dev_err(isp->dev, "%s, q to css fails: %d\n", 923 __func__, err); 924 return; 925 } 926 if (!error && q_buffers) 927 atomisp_qbuffers_to_css(asd); 928 } 929 930 void atomisp_assert_recovery_work(struct work_struct *work) 931 { 932 struct atomisp_device *isp = container_of(work, struct atomisp_device, 933 assert_recovery_work); 934 struct pci_dev *pdev = to_pci_dev(isp->dev); 935 unsigned long flags; 936 int ret; 937 938 mutex_lock(&isp->mutex); 939 940 if (!isp->asd.streaming) 941 goto out_unlock; 942 943 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false); 944 945 spin_lock_irqsave(&isp->lock, flags); 946 isp->asd.streaming = false; 947 spin_unlock_irqrestore(&isp->lock, flags); 948 949 /* stream off sensor */ 950 ret = v4l2_subdev_call(isp->inputs[isp->asd.input_curr].camera, video, s_stream, 0); 951 if (ret) 952 dev_warn(isp->dev, "Stopping sensor stream failed: %d\n", ret); 953 954 atomisp_clear_css_buffer_counters(&isp->asd); 955 956 atomisp_css_stop(&isp->asd, true); 957 958 isp->asd.preview_exp_id = 1; 959 isp->asd.postview_exp_id = 1; 960 /* notify HAL the CSS reset */ 961 dev_dbg(isp->dev, "send reset event to %s\n", isp->asd.subdev.devnode->name); 962 atomisp_reset_event(&isp->asd); 963 964 /* clear irq */ 965 disable_isp_irq(hrt_isp_css_irq_sp); 966 clear_isp_irq(hrt_isp_css_irq_sp); 967 968 /* Set the SRSE to 3 before resetting */ 969 pci_write_config_dword(pdev, PCI_I_CONTROL, 970 isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK); 971 972 /* reset ISP and restore its state */ 973 atomisp_reset(isp); 974 975 atomisp_css_input_set_mode(&isp->asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR); 976 977 /* Recreate streams destroyed by atomisp_css_stop() */ 978 atomisp_create_pipes_stream(&isp->asd); 979 980 /* Invalidate caches. FIXME: should flush only necessary buffers */ 981 wbinvd(); 982 983 if (atomisp_css_start(&isp->asd)) { 984 dev_warn(isp->dev, "start SP failed, so do not set streaming to be enable!\n"); 985 } else { 986 spin_lock_irqsave(&isp->lock, flags); 987 isp->asd.streaming = true; 988 spin_unlock_irqrestore(&isp->lock, flags); 989 } 990 991 atomisp_csi2_configure(&isp->asd); 992 993 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, 994 atomisp_css_valid_sof(isp)); 995 996 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, true) < 0) 997 dev_dbg(isp->dev, "DFS auto failed while recovering!\n"); 998 999 /* Dequeueing buffers is not needed, CSS will recycle buffers that it has */ 1000 atomisp_flush_video_pipe(&isp->asd.video_out, VB2_BUF_STATE_ERROR, false); 1001 1002 /* Requeue unprocessed per-frame parameters. */ 1003 atomisp_recover_params_queue(&isp->asd.video_out); 1004 1005 ret = v4l2_subdev_call(isp->inputs[isp->asd.input_curr].camera, video, s_stream, 1); 1006 if (ret) 1007 dev_err(isp->dev, "Starting sensor stream failed: %d\n", ret); 1008 1009 out_unlock: 1010 mutex_unlock(&isp->mutex); 1011 } 1012 1013 void atomisp_setup_flash(struct atomisp_sub_device *asd) 1014 { 1015 struct atomisp_device *isp = asd->isp; 1016 struct v4l2_control ctrl; 1017 1018 if (!isp->flash) 1019 return; 1020 1021 if (asd->params.flash_state != ATOMISP_FLASH_REQUESTED && 1022 asd->params.flash_state != ATOMISP_FLASH_DONE) 1023 return; 1024 1025 if (asd->params.num_flash_frames) { 1026 /* make sure the timeout is set before setting flash mode */ 1027 ctrl.id = V4L2_CID_FLASH_TIMEOUT; 1028 ctrl.value = FLASH_TIMEOUT; 1029 1030 if (v4l2_s_ctrl(NULL, isp->flash->ctrl_handler, &ctrl)) { 1031 dev_err(isp->dev, "flash timeout configure failed\n"); 1032 return; 1033 } 1034 1035 ia_css_stream_request_flash(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream); 1036 1037 asd->params.flash_state = ATOMISP_FLASH_ONGOING; 1038 } else { 1039 asd->params.flash_state = ATOMISP_FLASH_IDLE; 1040 } 1041 } 1042 1043 irqreturn_t atomisp_isr_thread(int irq, void *isp_ptr) 1044 { 1045 struct atomisp_device *isp = isp_ptr; 1046 unsigned long flags; 1047 1048 dev_dbg(isp->dev, ">%s\n", __func__); 1049 1050 spin_lock_irqsave(&isp->lock, flags); 1051 1052 if (!isp->asd.streaming) { 1053 spin_unlock_irqrestore(&isp->lock, flags); 1054 return IRQ_HANDLED; 1055 } 1056 1057 spin_unlock_irqrestore(&isp->lock, flags); 1058 1059 /* 1060 * The standard CSS2.0 API tells the following calling sequence of 1061 * dequeue ready buffers: 1062 * while (ia_css_dequeue_psys_event(...)) { 1063 * switch (event.type) { 1064 * ... 1065 * ia_css_pipe_dequeue_buffer() 1066 * } 1067 * } 1068 * That is, dequeue event and buffer are one after another. 1069 * 1070 * But the following implementation is to first deuque all the event 1071 * to a FIFO, then process the event in the FIFO. 1072 * This will not have issue in single stream mode, but it do have some 1073 * issue in multiple stream case. The issue is that 1074 * ia_css_pipe_dequeue_buffer() will not return the corrent buffer in 1075 * a specific pipe. 1076 * 1077 * This is due to ia_css_pipe_dequeue_buffer() does not take the 1078 * ia_css_pipe parameter. 1079 * 1080 * So: 1081 * For CSS2.0: we change the way to not dequeue all the event at one 1082 * time, instead, dequue one and process one, then another 1083 */ 1084 mutex_lock(&isp->mutex); 1085 if (atomisp_css_isr_thread(isp)) 1086 goto out; 1087 1088 if (isp->asd.streaming) 1089 atomisp_setup_flash(&isp->asd); 1090 out: 1091 mutex_unlock(&isp->mutex); 1092 dev_dbg(isp->dev, "<%s\n", __func__); 1093 1094 return IRQ_HANDLED; 1095 } 1096 1097 /* 1098 * Get internal fmt according to V4L2 fmt 1099 */ 1100 static enum ia_css_frame_format 1101 v4l2_fmt_to_sh_fmt(u32 fmt) 1102 { 1103 switch (fmt) { 1104 case V4L2_PIX_FMT_YUV420: 1105 return IA_CSS_FRAME_FORMAT_YUV420; 1106 case V4L2_PIX_FMT_YVU420: 1107 return IA_CSS_FRAME_FORMAT_YV12; 1108 case V4L2_PIX_FMT_YUV422P: 1109 return IA_CSS_FRAME_FORMAT_YUV422; 1110 case V4L2_PIX_FMT_YUV444: 1111 return IA_CSS_FRAME_FORMAT_YUV444; 1112 case V4L2_PIX_FMT_NV12: 1113 return IA_CSS_FRAME_FORMAT_NV12; 1114 case V4L2_PIX_FMT_NV21: 1115 return IA_CSS_FRAME_FORMAT_NV21; 1116 case V4L2_PIX_FMT_NV16: 1117 return IA_CSS_FRAME_FORMAT_NV16; 1118 case V4L2_PIX_FMT_NV61: 1119 return IA_CSS_FRAME_FORMAT_NV61; 1120 case V4L2_PIX_FMT_UYVY: 1121 return IA_CSS_FRAME_FORMAT_UYVY; 1122 case V4L2_PIX_FMT_YUYV: 1123 return IA_CSS_FRAME_FORMAT_YUYV; 1124 case V4L2_PIX_FMT_RGB24: 1125 return IA_CSS_FRAME_FORMAT_PLANAR_RGB888; 1126 case V4L2_PIX_FMT_RGB32: 1127 return IA_CSS_FRAME_FORMAT_RGBA888; 1128 case V4L2_PIX_FMT_RGB565: 1129 return IA_CSS_FRAME_FORMAT_RGB565; 1130 #if 0 1131 case V4L2_PIX_FMT_JPEG: 1132 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW: 1133 return IA_CSS_FRAME_FORMAT_BINARY_8; 1134 #endif 1135 case V4L2_PIX_FMT_SBGGR16: 1136 case V4L2_PIX_FMT_SBGGR10: 1137 case V4L2_PIX_FMT_SGBRG10: 1138 case V4L2_PIX_FMT_SGRBG10: 1139 case V4L2_PIX_FMT_SRGGB10: 1140 case V4L2_PIX_FMT_SBGGR12: 1141 case V4L2_PIX_FMT_SGBRG12: 1142 case V4L2_PIX_FMT_SGRBG12: 1143 case V4L2_PIX_FMT_SRGGB12: 1144 case V4L2_PIX_FMT_SBGGR8: 1145 case V4L2_PIX_FMT_SGBRG8: 1146 case V4L2_PIX_FMT_SGRBG8: 1147 case V4L2_PIX_FMT_SRGGB8: 1148 return IA_CSS_FRAME_FORMAT_RAW; 1149 default: 1150 return -EINVAL; 1151 } 1152 } 1153 1154 /* 1155 * raw format match between SH format and V4L2 format 1156 */ 1157 static int raw_output_format_match_input(u32 input, u32 output) 1158 { 1159 if ((input == ATOMISP_INPUT_FORMAT_RAW_12) && 1160 ((output == V4L2_PIX_FMT_SRGGB12) || 1161 (output == V4L2_PIX_FMT_SGRBG12) || 1162 (output == V4L2_PIX_FMT_SBGGR12) || 1163 (output == V4L2_PIX_FMT_SGBRG12))) 1164 return 0; 1165 1166 if ((input == ATOMISP_INPUT_FORMAT_RAW_10) && 1167 ((output == V4L2_PIX_FMT_SRGGB10) || 1168 (output == V4L2_PIX_FMT_SGRBG10) || 1169 (output == V4L2_PIX_FMT_SBGGR10) || 1170 (output == V4L2_PIX_FMT_SGBRG10))) 1171 return 0; 1172 1173 if ((input == ATOMISP_INPUT_FORMAT_RAW_8) && 1174 ((output == V4L2_PIX_FMT_SRGGB8) || 1175 (output == V4L2_PIX_FMT_SGRBG8) || 1176 (output == V4L2_PIX_FMT_SBGGR8) || 1177 (output == V4L2_PIX_FMT_SGBRG8))) 1178 return 0; 1179 1180 if ((input == ATOMISP_INPUT_FORMAT_RAW_16) && (output == V4L2_PIX_FMT_SBGGR16)) 1181 return 0; 1182 1183 return -EINVAL; 1184 } 1185 1186 u32 atomisp_get_pixel_depth(u32 pixelformat) 1187 { 1188 switch (pixelformat) { 1189 case V4L2_PIX_FMT_YUV420: 1190 case V4L2_PIX_FMT_NV12: 1191 case V4L2_PIX_FMT_NV21: 1192 case V4L2_PIX_FMT_YVU420: 1193 return 12; 1194 case V4L2_PIX_FMT_YUV422P: 1195 case V4L2_PIX_FMT_YUYV: 1196 case V4L2_PIX_FMT_UYVY: 1197 case V4L2_PIX_FMT_NV16: 1198 case V4L2_PIX_FMT_NV61: 1199 case V4L2_PIX_FMT_RGB565: 1200 case V4L2_PIX_FMT_SBGGR16: 1201 case V4L2_PIX_FMT_SBGGR12: 1202 case V4L2_PIX_FMT_SGBRG12: 1203 case V4L2_PIX_FMT_SGRBG12: 1204 case V4L2_PIX_FMT_SRGGB12: 1205 case V4L2_PIX_FMT_SBGGR10: 1206 case V4L2_PIX_FMT_SGBRG10: 1207 case V4L2_PIX_FMT_SGRBG10: 1208 case V4L2_PIX_FMT_SRGGB10: 1209 return 16; 1210 case V4L2_PIX_FMT_RGB24: 1211 case V4L2_PIX_FMT_YUV444: 1212 return 24; 1213 case V4L2_PIX_FMT_RGB32: 1214 return 32; 1215 case V4L2_PIX_FMT_JPEG: 1216 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW: 1217 case V4L2_PIX_FMT_SBGGR8: 1218 case V4L2_PIX_FMT_SGBRG8: 1219 case V4L2_PIX_FMT_SGRBG8: 1220 case V4L2_PIX_FMT_SRGGB8: 1221 return 8; 1222 default: 1223 return 8 * 2; /* raw type now */ 1224 } 1225 } 1226 1227 bool atomisp_is_mbuscode_raw(uint32_t code) 1228 { 1229 return code >= 0x3000 && code < 0x4000; 1230 } 1231 1232 /* 1233 * ISP features control function 1234 */ 1235 1236 /* 1237 * Set ISP capture mode based on current settings 1238 */ 1239 static void atomisp_update_capture_mode(struct atomisp_sub_device *asd) 1240 { 1241 if (asd->params.gdc_cac_en) 1242 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_ADVANCED); 1243 else if (asd->params.low_light) 1244 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_LOW_LIGHT); 1245 else if (asd->video_out.sh_fmt == IA_CSS_FRAME_FORMAT_RAW) 1246 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW); 1247 else 1248 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY); 1249 } 1250 1251 /* 1252 * Function to enable/disable lens geometry distortion correction (GDC) and 1253 * chromatic aberration correction (CAC) 1254 */ 1255 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag, 1256 __s32 *value) 1257 { 1258 if (flag == 0) { 1259 *value = asd->params.gdc_cac_en; 1260 return 0; 1261 } 1262 1263 asd->params.gdc_cac_en = !!*value; 1264 if (asd->params.gdc_cac_en) { 1265 asd->params.config.morph_table = asd->params.css_param.morph_table; 1266 } else { 1267 asd->params.config.morph_table = NULL; 1268 } 1269 asd->params.css_update_params_needed = true; 1270 atomisp_update_capture_mode(asd); 1271 return 0; 1272 } 1273 1274 /* 1275 * Function to enable/disable low light mode including ANR 1276 */ 1277 int atomisp_low_light(struct atomisp_sub_device *asd, int flag, 1278 __s32 *value) 1279 { 1280 if (flag == 0) { 1281 *value = asd->params.low_light; 1282 return 0; 1283 } 1284 1285 asd->params.low_light = (*value != 0); 1286 atomisp_update_capture_mode(asd); 1287 return 0; 1288 } 1289 1290 /* 1291 * Function to enable/disable extra noise reduction (XNR) in low light 1292 * condition 1293 */ 1294 int atomisp_xnr(struct atomisp_sub_device *asd, int flag, 1295 int *xnr_enable) 1296 { 1297 if (flag == 0) { 1298 *xnr_enable = asd->params.xnr_en; 1299 return 0; 1300 } 1301 1302 atomisp_css_capture_enable_xnr(asd, !!*xnr_enable); 1303 1304 return 0; 1305 } 1306 1307 /* 1308 * Function to configure bayer noise reduction 1309 */ 1310 int atomisp_nr(struct atomisp_sub_device *asd, int flag, 1311 struct atomisp_nr_config *arg) 1312 { 1313 if (flag == 0) { 1314 /* Get nr config from current setup */ 1315 if (atomisp_css_get_nr_config(asd, arg)) 1316 return -EINVAL; 1317 } else { 1318 /* Set nr config to isp parameters */ 1319 memcpy(&asd->params.css_param.nr_config, arg, 1320 sizeof(struct ia_css_nr_config)); 1321 asd->params.config.nr_config = &asd->params.css_param.nr_config; 1322 asd->params.css_update_params_needed = true; 1323 } 1324 return 0; 1325 } 1326 1327 /* 1328 * Function to configure temporal noise reduction (TNR) 1329 */ 1330 int atomisp_tnr(struct atomisp_sub_device *asd, int flag, 1331 struct atomisp_tnr_config *config) 1332 { 1333 /* Get tnr config from current setup */ 1334 if (flag == 0) { 1335 /* Get tnr config from current setup */ 1336 if (atomisp_css_get_tnr_config(asd, config)) 1337 return -EINVAL; 1338 } else { 1339 /* Set tnr config to isp parameters */ 1340 memcpy(&asd->params.css_param.tnr_config, config, 1341 sizeof(struct ia_css_tnr_config)); 1342 asd->params.config.tnr_config = &asd->params.css_param.tnr_config; 1343 asd->params.css_update_params_needed = true; 1344 } 1345 1346 return 0; 1347 } 1348 1349 /* 1350 * Function to configure black level compensation 1351 */ 1352 int atomisp_black_level(struct atomisp_sub_device *asd, int flag, 1353 struct atomisp_ob_config *config) 1354 { 1355 if (flag == 0) { 1356 /* Get ob config from current setup */ 1357 if (atomisp_css_get_ob_config(asd, config)) 1358 return -EINVAL; 1359 } else { 1360 /* Set ob config to isp parameters */ 1361 memcpy(&asd->params.css_param.ob_config, config, 1362 sizeof(struct ia_css_ob_config)); 1363 asd->params.config.ob_config = &asd->params.css_param.ob_config; 1364 asd->params.css_update_params_needed = true; 1365 } 1366 1367 return 0; 1368 } 1369 1370 /* 1371 * Function to configure edge enhancement 1372 */ 1373 int atomisp_ee(struct atomisp_sub_device *asd, int flag, 1374 struct atomisp_ee_config *config) 1375 { 1376 if (flag == 0) { 1377 /* Get ee config from current setup */ 1378 if (atomisp_css_get_ee_config(asd, config)) 1379 return -EINVAL; 1380 } else { 1381 /* Set ee config to isp parameters */ 1382 memcpy(&asd->params.css_param.ee_config, config, 1383 sizeof(asd->params.css_param.ee_config)); 1384 asd->params.config.ee_config = &asd->params.css_param.ee_config; 1385 asd->params.css_update_params_needed = true; 1386 } 1387 1388 return 0; 1389 } 1390 1391 /* 1392 * Function to update Gamma table for gamma, brightness and contrast config 1393 */ 1394 int atomisp_gamma(struct atomisp_sub_device *asd, int flag, 1395 struct atomisp_gamma_table *config) 1396 { 1397 if (flag == 0) { 1398 /* Get gamma table from current setup */ 1399 if (atomisp_css_get_gamma_table(asd, config)) 1400 return -EINVAL; 1401 } else { 1402 /* Set gamma table to isp parameters */ 1403 memcpy(&asd->params.css_param.gamma_table, config, 1404 sizeof(asd->params.css_param.gamma_table)); 1405 asd->params.config.gamma_table = &asd->params.css_param.gamma_table; 1406 } 1407 1408 return 0; 1409 } 1410 1411 /* 1412 * Function to update Ctc table for Chroma Enhancement 1413 */ 1414 int atomisp_ctc(struct atomisp_sub_device *asd, int flag, 1415 struct atomisp_ctc_table *config) 1416 { 1417 if (flag == 0) { 1418 /* Get ctc table from current setup */ 1419 if (atomisp_css_get_ctc_table(asd, config)) 1420 return -EINVAL; 1421 } else { 1422 /* Set ctc table to isp parameters */ 1423 memcpy(&asd->params.css_param.ctc_table, config, 1424 sizeof(asd->params.css_param.ctc_table)); 1425 atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table); 1426 } 1427 1428 return 0; 1429 } 1430 1431 /* 1432 * Function to update gamma correction parameters 1433 */ 1434 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag, 1435 struct atomisp_gc_config *config) 1436 { 1437 if (flag == 0) { 1438 /* Get gamma correction params from current setup */ 1439 if (atomisp_css_get_gc_config(asd, config)) 1440 return -EINVAL; 1441 } else { 1442 /* Set gamma correction params to isp parameters */ 1443 memcpy(&asd->params.css_param.gc_config, config, 1444 sizeof(asd->params.css_param.gc_config)); 1445 asd->params.config.gc_config = &asd->params.css_param.gc_config; 1446 asd->params.css_update_params_needed = true; 1447 } 1448 1449 return 0; 1450 } 1451 1452 /* 1453 * Function to update narrow gamma flag 1454 */ 1455 int atomisp_formats(struct atomisp_sub_device *asd, int flag, 1456 struct atomisp_formats_config *config) 1457 { 1458 if (flag == 0) { 1459 /* Get narrow gamma flag from current setup */ 1460 if (atomisp_css_get_formats_config(asd, config)) 1461 return -EINVAL; 1462 } else { 1463 /* Set narrow gamma flag to isp parameters */ 1464 memcpy(&asd->params.css_param.formats_config, config, 1465 sizeof(asd->params.css_param.formats_config)); 1466 asd->params.config.formats_config = &asd->params.css_param.formats_config; 1467 } 1468 1469 return 0; 1470 } 1471 1472 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd) 1473 { 1474 atomisp_free_css_parameters(&asd->params.css_param); 1475 } 1476 1477 static void atomisp_update_grid_info(struct atomisp_sub_device *asd, 1478 enum ia_css_pipe_id pipe_id) 1479 { 1480 struct atomisp_device *isp = asd->isp; 1481 int err; 1482 1483 if (atomisp_css_get_grid_info(asd, pipe_id)) 1484 return; 1485 1486 /* We must free all buffers because they no longer match 1487 the grid size. */ 1488 atomisp_css_free_stat_buffers(asd); 1489 1490 err = atomisp_alloc_css_stat_bufs(asd, ATOMISP_INPUT_STREAM_GENERAL); 1491 if (err) { 1492 dev_err(isp->dev, "stat_buf allocate error\n"); 1493 goto err; 1494 } 1495 1496 if (atomisp_alloc_3a_output_buf(asd)) { 1497 /* Failure for 3A buffers does not influence DIS buffers */ 1498 if (asd->params.s3a_output_bytes != 0) { 1499 /* For SOC sensor happens s3a_output_bytes == 0, 1500 * using if condition to exclude false error log */ 1501 dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n"); 1502 } 1503 goto err; 1504 } 1505 1506 if (atomisp_alloc_dis_coef_buf(asd)) { 1507 dev_err(isp->dev, 1508 "Failed to allocate memory for DIS statistics\n"); 1509 goto err; 1510 } 1511 1512 if (atomisp_alloc_metadata_output_buf(asd)) { 1513 dev_err(isp->dev, "Failed to allocate memory for metadata\n"); 1514 goto err; 1515 } 1516 1517 return; 1518 1519 err: 1520 atomisp_css_free_stat_buffers(asd); 1521 return; 1522 } 1523 1524 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd, 1525 struct atomisp_grid_info *info) 1526 { 1527 memcpy(info, &asd->params.curr_grid_info.s3a_grid, 1528 sizeof(struct ia_css_3a_grid_info)); 1529 } 1530 1531 int atomisp_compare_grid(struct atomisp_sub_device *asd, 1532 struct atomisp_grid_info *atomgrid) 1533 { 1534 struct atomisp_grid_info tmp = {0}; 1535 1536 atomisp_curr_user_grid_info(asd, &tmp); 1537 return memcmp(atomgrid, &tmp, sizeof(tmp)); 1538 } 1539 1540 /* 1541 * Function to update Gdc table for gdc 1542 */ 1543 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag, 1544 struct atomisp_morph_table *config) 1545 { 1546 int ret; 1547 int i; 1548 struct atomisp_device *isp = asd->isp; 1549 1550 if (flag == 0) { 1551 /* Get gdc table from current setup */ 1552 struct ia_css_morph_table tab = {0}; 1553 1554 atomisp_css_get_morph_table(asd, &tab); 1555 1556 config->width = tab.width; 1557 config->height = tab.height; 1558 1559 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) { 1560 ret = copy_to_user(config->coordinates_x[i], 1561 tab.coordinates_x[i], tab.height * 1562 tab.width * sizeof(*tab.coordinates_x[i])); 1563 if (ret) { 1564 dev_err(isp->dev, 1565 "Failed to copy to User for x\n"); 1566 return -EFAULT; 1567 } 1568 ret = copy_to_user(config->coordinates_y[i], 1569 tab.coordinates_y[i], tab.height * 1570 tab.width * sizeof(*tab.coordinates_y[i])); 1571 if (ret) { 1572 dev_err(isp->dev, 1573 "Failed to copy to User for y\n"); 1574 return -EFAULT; 1575 } 1576 } 1577 } else { 1578 struct ia_css_morph_table *tab = 1579 asd->params.css_param.morph_table; 1580 1581 /* free first if we have one */ 1582 if (tab) { 1583 atomisp_css_morph_table_free(tab); 1584 asd->params.css_param.morph_table = NULL; 1585 } 1586 1587 /* allocate new one */ 1588 tab = atomisp_css_morph_table_allocate(config->width, 1589 config->height); 1590 1591 if (!tab) { 1592 dev_err(isp->dev, "out of memory\n"); 1593 return -EINVAL; 1594 } 1595 1596 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) { 1597 ret = copy_from_user(tab->coordinates_x[i], 1598 config->coordinates_x[i], 1599 config->height * config->width * 1600 sizeof(*config->coordinates_x[i])); 1601 if (ret) { 1602 dev_err(isp->dev, 1603 "Failed to copy from User for x, ret %d\n", 1604 ret); 1605 atomisp_css_morph_table_free(tab); 1606 return -EFAULT; 1607 } 1608 ret = copy_from_user(tab->coordinates_y[i], 1609 config->coordinates_y[i], 1610 config->height * config->width * 1611 sizeof(*config->coordinates_y[i])); 1612 if (ret) { 1613 dev_err(isp->dev, 1614 "Failed to copy from User for y, ret is %d\n", 1615 ret); 1616 atomisp_css_morph_table_free(tab); 1617 return -EFAULT; 1618 } 1619 } 1620 asd->params.css_param.morph_table = tab; 1621 if (asd->params.gdc_cac_en) 1622 asd->params.config.morph_table = tab; 1623 } 1624 1625 return 0; 1626 } 1627 1628 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag, 1629 struct atomisp_macc_config *config) 1630 { 1631 struct ia_css_macc_table *macc_table; 1632 1633 switch (config->color_effect) { 1634 case V4L2_COLORFX_NONE: 1635 macc_table = &asd->params.css_param.macc_table; 1636 break; 1637 case V4L2_COLORFX_SKY_BLUE: 1638 macc_table = &blue_macc_table; 1639 break; 1640 case V4L2_COLORFX_GRASS_GREEN: 1641 macc_table = &green_macc_table; 1642 break; 1643 case V4L2_COLORFX_SKIN_WHITEN_LOW: 1644 macc_table = &skin_low_macc_table; 1645 break; 1646 case V4L2_COLORFX_SKIN_WHITEN: 1647 macc_table = &skin_medium_macc_table; 1648 break; 1649 case V4L2_COLORFX_SKIN_WHITEN_HIGH: 1650 macc_table = &skin_high_macc_table; 1651 break; 1652 default: 1653 return -EINVAL; 1654 } 1655 1656 if (flag == 0) { 1657 /* Get macc table from current setup */ 1658 memcpy(&config->table, macc_table, 1659 sizeof(struct ia_css_macc_table)); 1660 } else { 1661 memcpy(macc_table, &config->table, 1662 sizeof(struct ia_css_macc_table)); 1663 if (config->color_effect == asd->params.color_effect) 1664 asd->params.config.macc_table = macc_table; 1665 } 1666 1667 return 0; 1668 } 1669 1670 int atomisp_set_dis_vector(struct atomisp_sub_device *asd, 1671 struct atomisp_dis_vector *vector) 1672 { 1673 atomisp_css_video_set_dis_vector(asd, vector); 1674 1675 asd->params.dis_proj_data_valid = false; 1676 asd->params.css_update_params_needed = true; 1677 return 0; 1678 } 1679 1680 /* 1681 * Function to set/get image stablization statistics 1682 */ 1683 int atomisp_get_dis_stat(struct atomisp_sub_device *asd, 1684 struct atomisp_dis_statistics *stats) 1685 { 1686 return atomisp_css_get_dis_stat(asd, stats); 1687 } 1688 1689 /* 1690 * Function set camrea_prefiles.xml current sensor pixel array size 1691 */ 1692 int atomisp_set_array_res(struct atomisp_sub_device *asd, 1693 struct atomisp_resolution *config) 1694 { 1695 dev_dbg(asd->isp->dev, ">%s start\n", __func__); 1696 if (!config) { 1697 dev_err(asd->isp->dev, "Set sensor array size is not valid\n"); 1698 return -EINVAL; 1699 } 1700 1701 asd->sensor_array_res.width = config->width; 1702 asd->sensor_array_res.height = config->height; 1703 return 0; 1704 } 1705 1706 /* 1707 * Function to get DVS2 BQ resolution settings 1708 */ 1709 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd, 1710 struct atomisp_dvs2_bq_resolutions *bq_res) 1711 { 1712 struct ia_css_pipe_config *pipe_cfg = NULL; 1713 1714 struct ia_css_stream *stream = 1715 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream; 1716 if (!stream) { 1717 dev_warn(asd->isp->dev, "stream is not created"); 1718 return -EAGAIN; 1719 } 1720 1721 pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL] 1722 .pipe_configs[IA_CSS_PIPE_ID_VIDEO]; 1723 1724 if (!bq_res) 1725 return -EINVAL; 1726 1727 /* the GDC output resolution */ 1728 bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2; 1729 bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2; 1730 1731 bq_res->envelope_bq.width_bq = 0; 1732 bq_res->envelope_bq.height_bq = 0; 1733 /* the GDC input resolution */ 1734 bq_res->source_bq.width_bq = bq_res->output_bq.width_bq + 1735 pipe_cfg->dvs_envelope.width / 2; 1736 bq_res->source_bq.height_bq = bq_res->output_bq.height_bq + 1737 pipe_cfg->dvs_envelope.height / 2; 1738 /* 1739 * Bad pixels caused by spatial filter processing 1740 * ISP filter resolution should be given by CSS/FW, but for now 1741 * there is not such API to query, and it is fixed value, so 1742 * hardcoded here. 1743 */ 1744 bq_res->ispfilter_bq.width_bq = 12 / 2; 1745 bq_res->ispfilter_bq.height_bq = 12 / 2; 1746 /* spatial filter shift, always 4 pixels */ 1747 bq_res->gdc_shift_bq.width_bq = 4 / 2; 1748 bq_res->gdc_shift_bq.height_bq = 4 / 2; 1749 1750 if (asd->params.video_dis_en) { 1751 bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width / 2 - 1752 bq_res->ispfilter_bq.width_bq; 1753 bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height / 2 - 1754 bq_res->ispfilter_bq.height_bq; 1755 } 1756 1757 dev_dbg(asd->isp->dev, 1758 "source_bq.width_bq %d, source_bq.height_bq %d,\nispfilter_bq.width_bq %d, ispfilter_bq.height_bq %d,\ngdc_shift_bq.width_bq %d, gdc_shift_bq.height_bq %d,\nenvelope_bq.width_bq %d, envelope_bq.height_bq %d,\noutput_bq.width_bq %d, output_bq.height_bq %d\n", 1759 bq_res->source_bq.width_bq, bq_res->source_bq.height_bq, 1760 bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq, 1761 bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq, 1762 bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq, 1763 bq_res->output_bq.width_bq, bq_res->output_bq.height_bq); 1764 1765 return 0; 1766 } 1767 1768 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd, 1769 struct atomisp_dis_coefficients *coefs) 1770 { 1771 return atomisp_css_set_dis_coefs(asd, coefs); 1772 } 1773 1774 /* 1775 * Function to set/get 3A stat from isp 1776 */ 1777 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag, 1778 struct atomisp_3a_statistics *config) 1779 { 1780 struct atomisp_device *isp = asd->isp; 1781 struct atomisp_s3a_buf *s3a_buf; 1782 unsigned long ret; 1783 1784 if (flag != 0) 1785 return -EINVAL; 1786 1787 /* sanity check to avoid writing into unallocated memory. */ 1788 if (asd->params.s3a_output_bytes == 0) 1789 return -EINVAL; 1790 1791 if (atomisp_compare_grid(asd, &config->grid_info) != 0) { 1792 /* If the grid info in the argument differs from the current 1793 grid info, we tell the caller to reset the grid size and 1794 try again. */ 1795 return -EAGAIN; 1796 } 1797 1798 if (list_empty(&asd->s3a_stats_ready)) { 1799 dev_err(isp->dev, "3a statistics is not valid.\n"); 1800 return -EAGAIN; 1801 } 1802 1803 s3a_buf = list_entry(asd->s3a_stats_ready.next, 1804 struct atomisp_s3a_buf, list); 1805 if (s3a_buf->s3a_map) 1806 ia_css_translate_3a_statistics( 1807 asd->params.s3a_user_stat, s3a_buf->s3a_map); 1808 else 1809 ia_css_get_3a_statistics(asd->params.s3a_user_stat, 1810 s3a_buf->s3a_data); 1811 1812 config->exp_id = s3a_buf->s3a_data->exp_id; 1813 config->isp_config_id = s3a_buf->s3a_data->isp_config_id; 1814 1815 ret = copy_to_user(config->data, asd->params.s3a_user_stat->data, 1816 asd->params.s3a_output_bytes); 1817 if (ret) { 1818 dev_err(isp->dev, "copy to user failed: copied %lu bytes\n", 1819 ret); 1820 return -EFAULT; 1821 } 1822 1823 /* Move to free buffer list */ 1824 list_del_init(&s3a_buf->list); 1825 list_add_tail(&s3a_buf->list, &asd->s3a_stats); 1826 dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n", 1827 __func__, 1828 config->exp_id, config->isp_config_id); 1829 return 0; 1830 } 1831 1832 /* 1833 * Function to calculate real zoom region for every pipe 1834 */ 1835 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd, 1836 struct ia_css_dz_config *dz_config, 1837 enum ia_css_pipe_id css_pipe_id) 1838 1839 { 1840 struct atomisp_stream_env *stream_env = 1841 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]; 1842 struct atomisp_resolution eff_res, out_res; 1843 int w_offset, h_offset; 1844 1845 memset(&eff_res, 0, sizeof(eff_res)); 1846 memset(&out_res, 0, sizeof(out_res)); 1847 1848 if (dz_config->dx || dz_config->dy) 1849 return 0; 1850 1851 if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW 1852 && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) { 1853 dev_err(asd->isp->dev, "%s the set pipe no support crop region" 1854 , __func__); 1855 return -EINVAL; 1856 } 1857 1858 eff_res.width = 1859 stream_env->stream_config.input_config.effective_res.width; 1860 eff_res.height = 1861 stream_env->stream_config.input_config.effective_res.height; 1862 if (eff_res.width == 0 || eff_res.height == 0) { 1863 dev_err(asd->isp->dev, "%s err effective resolution" 1864 , __func__); 1865 return -EINVAL; 1866 } 1867 1868 if (dz_config->zoom_region.resolution.width 1869 == asd->sensor_array_res.width 1870 || dz_config->zoom_region.resolution.height 1871 == asd->sensor_array_res.height) { 1872 /*no need crop region*/ 1873 dz_config->zoom_region.origin.x = 0; 1874 dz_config->zoom_region.origin.y = 0; 1875 dz_config->zoom_region.resolution.width = eff_res.width; 1876 dz_config->zoom_region.resolution.height = eff_res.height; 1877 return 0; 1878 } 1879 1880 /* FIXME: 1881 * This is not the correct implementation with Google's definition, due 1882 * to firmware limitation. 1883 * map real crop region base on above calculating base max crop region. 1884 */ 1885 1886 if (!IS_ISP2401) { 1887 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x 1888 * eff_res.width 1889 / asd->sensor_array_res.width; 1890 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y 1891 * eff_res.height 1892 / asd->sensor_array_res.height; 1893 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width 1894 * eff_res.width 1895 / asd->sensor_array_res.width; 1896 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height 1897 * eff_res.height 1898 / asd->sensor_array_res.height; 1899 /* 1900 * Set same ratio of crop region resolution and current pipe output 1901 * resolution 1902 */ 1903 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width; 1904 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height; 1905 if (out_res.width == 0 || out_res.height == 0) { 1906 dev_err(asd->isp->dev, "%s err current pipe output resolution" 1907 , __func__); 1908 return -EINVAL; 1909 } 1910 } else { 1911 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width; 1912 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height; 1913 if (out_res.width == 0 || out_res.height == 0) { 1914 dev_err(asd->isp->dev, "%s err current pipe output resolution" 1915 , __func__); 1916 return -EINVAL; 1917 } 1918 1919 if (asd->sensor_array_res.width * out_res.height 1920 < out_res.width * asd->sensor_array_res.height) { 1921 h_offset = asd->sensor_array_res.height 1922 - asd->sensor_array_res.width 1923 * out_res.height / out_res.width; 1924 h_offset = h_offset / 2; 1925 if (dz_config->zoom_region.origin.y < h_offset) 1926 dz_config->zoom_region.origin.y = 0; 1927 else 1928 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset; 1929 w_offset = 0; 1930 } else { 1931 w_offset = asd->sensor_array_res.width 1932 - asd->sensor_array_res.height 1933 * out_res.width / out_res.height; 1934 w_offset = w_offset / 2; 1935 if (dz_config->zoom_region.origin.x < w_offset) 1936 dz_config->zoom_region.origin.x = 0; 1937 else 1938 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset; 1939 h_offset = 0; 1940 } 1941 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x 1942 * eff_res.width 1943 / (asd->sensor_array_res.width - 2 * w_offset); 1944 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y 1945 * eff_res.height 1946 / (asd->sensor_array_res.height - 2 * h_offset); 1947 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width 1948 * eff_res.width 1949 / (asd->sensor_array_res.width - 2 * w_offset); 1950 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height 1951 * eff_res.height 1952 / (asd->sensor_array_res.height - 2 * h_offset); 1953 } 1954 1955 if (out_res.width * dz_config->zoom_region.resolution.height 1956 > dz_config->zoom_region.resolution.width * out_res.height) { 1957 dz_config->zoom_region.resolution.height = 1958 dz_config->zoom_region.resolution.width 1959 * out_res.height / out_res.width; 1960 } else { 1961 dz_config->zoom_region.resolution.width = 1962 dz_config->zoom_region.resolution.height 1963 * out_res.width / out_res.height; 1964 } 1965 dev_dbg(asd->isp->dev, 1966 "%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n", 1967 __func__, dz_config->zoom_region.origin.x, 1968 dz_config->zoom_region.origin.y, 1969 dz_config->zoom_region.resolution.width, 1970 dz_config->zoom_region.resolution.height, 1971 eff_res.width, eff_res.height, 1972 asd->sensor_array_res.width, 1973 asd->sensor_array_res.height, 1974 out_res.width, out_res.height); 1975 1976 if ((dz_config->zoom_region.origin.x + 1977 dz_config->zoom_region.resolution.width 1978 > eff_res.width) || 1979 (dz_config->zoom_region.origin.y + 1980 dz_config->zoom_region.resolution.height 1981 > eff_res.height)) 1982 return -EINVAL; 1983 1984 return 0; 1985 } 1986 1987 /* 1988 * Function to check the zoom region whether is effective 1989 */ 1990 static bool atomisp_check_zoom_region( 1991 struct atomisp_sub_device *asd, 1992 struct ia_css_dz_config *dz_config) 1993 { 1994 struct atomisp_resolution config; 1995 bool flag = false; 1996 unsigned int w, h; 1997 1998 memset(&config, 0, sizeof(struct atomisp_resolution)); 1999 2000 if (dz_config->dx && dz_config->dy) 2001 return true; 2002 2003 config.width = asd->sensor_array_res.width; 2004 config.height = asd->sensor_array_res.height; 2005 w = dz_config->zoom_region.origin.x + 2006 dz_config->zoom_region.resolution.width; 2007 h = dz_config->zoom_region.origin.y + 2008 dz_config->zoom_region.resolution.height; 2009 2010 if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0) 2011 flag = true; 2012 else 2013 /* setting error zoom region */ 2014 dev_err(asd->isp->dev, 2015 "%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n", 2016 __func__, dz_config->zoom_region.origin.x, 2017 dz_config->zoom_region.origin.y, 2018 dz_config->zoom_region.resolution.width, 2019 dz_config->zoom_region.resolution.height, 2020 config.width, config.height); 2021 2022 return flag; 2023 } 2024 2025 void atomisp_apply_css_parameters( 2026 struct atomisp_sub_device *asd, 2027 struct atomisp_css_params *css_param) 2028 { 2029 if (css_param->update_flag.wb_config) 2030 asd->params.config.wb_config = &css_param->wb_config; 2031 2032 if (css_param->update_flag.ob_config) 2033 asd->params.config.ob_config = &css_param->ob_config; 2034 2035 if (css_param->update_flag.dp_config) 2036 asd->params.config.dp_config = &css_param->dp_config; 2037 2038 if (css_param->update_flag.nr_config) 2039 asd->params.config.nr_config = &css_param->nr_config; 2040 2041 if (css_param->update_flag.ee_config) 2042 asd->params.config.ee_config = &css_param->ee_config; 2043 2044 if (css_param->update_flag.tnr_config) 2045 asd->params.config.tnr_config = &css_param->tnr_config; 2046 2047 if (css_param->update_flag.a3a_config) 2048 asd->params.config.s3a_config = &css_param->s3a_config; 2049 2050 if (css_param->update_flag.ctc_config) 2051 asd->params.config.ctc_config = &css_param->ctc_config; 2052 2053 if (css_param->update_flag.cnr_config) 2054 asd->params.config.cnr_config = &css_param->cnr_config; 2055 2056 if (css_param->update_flag.ecd_config) 2057 asd->params.config.ecd_config = &css_param->ecd_config; 2058 2059 if (css_param->update_flag.ynr_config) 2060 asd->params.config.ynr_config = &css_param->ynr_config; 2061 2062 if (css_param->update_flag.fc_config) 2063 asd->params.config.fc_config = &css_param->fc_config; 2064 2065 if (css_param->update_flag.macc_config) 2066 asd->params.config.macc_config = &css_param->macc_config; 2067 2068 if (css_param->update_flag.aa_config) 2069 asd->params.config.aa_config = &css_param->aa_config; 2070 2071 if (css_param->update_flag.anr_config) 2072 asd->params.config.anr_config = &css_param->anr_config; 2073 2074 if (css_param->update_flag.xnr_config) 2075 asd->params.config.xnr_config = &css_param->xnr_config; 2076 2077 if (css_param->update_flag.yuv2rgb_cc_config) 2078 asd->params.config.yuv2rgb_cc_config = &css_param->yuv2rgb_cc_config; 2079 2080 if (css_param->update_flag.rgb2yuv_cc_config) 2081 asd->params.config.rgb2yuv_cc_config = &css_param->rgb2yuv_cc_config; 2082 2083 if (css_param->update_flag.macc_table) 2084 asd->params.config.macc_table = &css_param->macc_table; 2085 2086 if (css_param->update_flag.xnr_table) 2087 asd->params.config.xnr_table = &css_param->xnr_table; 2088 2089 if (css_param->update_flag.r_gamma_table) 2090 asd->params.config.r_gamma_table = &css_param->r_gamma_table; 2091 2092 if (css_param->update_flag.g_gamma_table) 2093 asd->params.config.g_gamma_table = &css_param->g_gamma_table; 2094 2095 if (css_param->update_flag.b_gamma_table) 2096 asd->params.config.b_gamma_table = &css_param->b_gamma_table; 2097 2098 if (css_param->update_flag.anr_thres) 2099 atomisp_css_set_anr_thres(asd, &css_param->anr_thres); 2100 2101 if (css_param->update_flag.shading_table) 2102 asd->params.config.shading_table = css_param->shading_table; 2103 2104 if (css_param->update_flag.morph_table && asd->params.gdc_cac_en) 2105 asd->params.config.morph_table = css_param->morph_table; 2106 2107 if (css_param->update_flag.dvs2_coefs) { 2108 struct ia_css_dvs_grid_info *dvs_grid_info = 2109 atomisp_css_get_dvs_grid_info( 2110 &asd->params.curr_grid_info); 2111 2112 if (dvs_grid_info && dvs_grid_info->enable) 2113 atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff); 2114 } 2115 2116 if (css_param->update_flag.dvs_6axis_config) 2117 atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis); 2118 2119 atomisp_css_set_isp_config_id(asd, css_param->isp_config_id); 2120 /* 2121 * These configurations are on used by ISP1.x, not for ISP2.x, 2122 * so do not handle them. see comments of ia_css_isp_config. 2123 * 1 cc_config 2124 * 2 ce_config 2125 * 3 de_config 2126 * 4 gc_config 2127 * 5 gamma_table 2128 * 6 ctc_table 2129 * 7 dvs_coefs 2130 */ 2131 } 2132 2133 static unsigned int long copy_from_compatible(void *to, const void *from, 2134 unsigned long n, bool from_user) 2135 { 2136 if (from_user) 2137 return copy_from_user(to, (void __user *)from, n); 2138 else 2139 memcpy(to, from, n); 2140 return 0; 2141 } 2142 2143 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd, 2144 struct atomisp_parameters *arg, 2145 struct atomisp_css_params *css_param, 2146 bool from_user) 2147 { 2148 struct atomisp_parameters *cur_config = &css_param->update_flag; 2149 2150 if (!arg || !asd || !css_param) 2151 return -EINVAL; 2152 2153 if (arg->wb_config && (from_user || !cur_config->wb_config)) { 2154 if (copy_from_compatible(&css_param->wb_config, arg->wb_config, 2155 sizeof(struct ia_css_wb_config), 2156 from_user)) 2157 return -EFAULT; 2158 css_param->update_flag.wb_config = 2159 (struct atomisp_wb_config *)&css_param->wb_config; 2160 } 2161 2162 if (arg->ob_config && (from_user || !cur_config->ob_config)) { 2163 if (copy_from_compatible(&css_param->ob_config, arg->ob_config, 2164 sizeof(struct ia_css_ob_config), 2165 from_user)) 2166 return -EFAULT; 2167 css_param->update_flag.ob_config = 2168 (struct atomisp_ob_config *)&css_param->ob_config; 2169 } 2170 2171 if (arg->dp_config && (from_user || !cur_config->dp_config)) { 2172 if (copy_from_compatible(&css_param->dp_config, arg->dp_config, 2173 sizeof(struct ia_css_dp_config), 2174 from_user)) 2175 return -EFAULT; 2176 css_param->update_flag.dp_config = 2177 (struct atomisp_dp_config *)&css_param->dp_config; 2178 } 2179 2180 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) { 2181 if (arg->dz_config && (from_user || !cur_config->dz_config)) { 2182 if (copy_from_compatible(&css_param->dz_config, 2183 arg->dz_config, 2184 sizeof(struct ia_css_dz_config), 2185 from_user)) 2186 return -EFAULT; 2187 if (!atomisp_check_zoom_region(asd, 2188 &css_param->dz_config)) { 2189 dev_err(asd->isp->dev, "crop region error!"); 2190 return -EINVAL; 2191 } 2192 css_param->update_flag.dz_config = 2193 (struct atomisp_dz_config *) 2194 &css_param->dz_config; 2195 } 2196 } 2197 2198 if (arg->nr_config && (from_user || !cur_config->nr_config)) { 2199 if (copy_from_compatible(&css_param->nr_config, arg->nr_config, 2200 sizeof(struct ia_css_nr_config), 2201 from_user)) 2202 return -EFAULT; 2203 css_param->update_flag.nr_config = 2204 (struct atomisp_nr_config *)&css_param->nr_config; 2205 } 2206 2207 if (arg->ee_config && (from_user || !cur_config->ee_config)) { 2208 if (copy_from_compatible(&css_param->ee_config, arg->ee_config, 2209 sizeof(struct ia_css_ee_config), 2210 from_user)) 2211 return -EFAULT; 2212 css_param->update_flag.ee_config = 2213 (struct atomisp_ee_config *)&css_param->ee_config; 2214 } 2215 2216 if (arg->tnr_config && (from_user || !cur_config->tnr_config)) { 2217 if (copy_from_compatible(&css_param->tnr_config, 2218 arg->tnr_config, 2219 sizeof(struct ia_css_tnr_config), 2220 from_user)) 2221 return -EFAULT; 2222 css_param->update_flag.tnr_config = 2223 (struct atomisp_tnr_config *) 2224 &css_param->tnr_config; 2225 } 2226 2227 if (arg->a3a_config && (from_user || !cur_config->a3a_config)) { 2228 if (copy_from_compatible(&css_param->s3a_config, 2229 arg->a3a_config, 2230 sizeof(struct ia_css_3a_config), 2231 from_user)) 2232 return -EFAULT; 2233 css_param->update_flag.a3a_config = 2234 (struct atomisp_3a_config *)&css_param->s3a_config; 2235 } 2236 2237 if (arg->ctc_config && (from_user || !cur_config->ctc_config)) { 2238 if (copy_from_compatible(&css_param->ctc_config, 2239 arg->ctc_config, 2240 sizeof(struct ia_css_ctc_config), 2241 from_user)) 2242 return -EFAULT; 2243 css_param->update_flag.ctc_config = 2244 (struct atomisp_ctc_config *) 2245 &css_param->ctc_config; 2246 } 2247 2248 if (arg->cnr_config && (from_user || !cur_config->cnr_config)) { 2249 if (copy_from_compatible(&css_param->cnr_config, 2250 arg->cnr_config, 2251 sizeof(struct ia_css_cnr_config), 2252 from_user)) 2253 return -EFAULT; 2254 css_param->update_flag.cnr_config = 2255 (struct atomisp_cnr_config *) 2256 &css_param->cnr_config; 2257 } 2258 2259 if (arg->ecd_config && (from_user || !cur_config->ecd_config)) { 2260 if (copy_from_compatible(&css_param->ecd_config, 2261 arg->ecd_config, 2262 sizeof(struct ia_css_ecd_config), 2263 from_user)) 2264 return -EFAULT; 2265 css_param->update_flag.ecd_config = 2266 (struct atomisp_ecd_config *) 2267 &css_param->ecd_config; 2268 } 2269 2270 if (arg->ynr_config && (from_user || !cur_config->ynr_config)) { 2271 if (copy_from_compatible(&css_param->ynr_config, 2272 arg->ynr_config, 2273 sizeof(struct ia_css_ynr_config), 2274 from_user)) 2275 return -EFAULT; 2276 css_param->update_flag.ynr_config = 2277 (struct atomisp_ynr_config *) 2278 &css_param->ynr_config; 2279 } 2280 2281 if (arg->fc_config && (from_user || !cur_config->fc_config)) { 2282 if (copy_from_compatible(&css_param->fc_config, 2283 arg->fc_config, 2284 sizeof(struct ia_css_fc_config), 2285 from_user)) 2286 return -EFAULT; 2287 css_param->update_flag.fc_config = 2288 (struct atomisp_fc_config *)&css_param->fc_config; 2289 } 2290 2291 if (arg->macc_config && (from_user || !cur_config->macc_config)) { 2292 if (copy_from_compatible(&css_param->macc_config, 2293 arg->macc_config, 2294 sizeof(struct ia_css_macc_config), 2295 from_user)) 2296 return -EFAULT; 2297 css_param->update_flag.macc_config = 2298 (struct atomisp_macc_config *) 2299 &css_param->macc_config; 2300 } 2301 2302 if (arg->aa_config && (from_user || !cur_config->aa_config)) { 2303 if (copy_from_compatible(&css_param->aa_config, arg->aa_config, 2304 sizeof(struct ia_css_aa_config), 2305 from_user)) 2306 return -EFAULT; 2307 css_param->update_flag.aa_config = 2308 (struct atomisp_aa_config *)&css_param->aa_config; 2309 } 2310 2311 if (arg->anr_config && (from_user || !cur_config->anr_config)) { 2312 if (copy_from_compatible(&css_param->anr_config, 2313 arg->anr_config, 2314 sizeof(struct ia_css_anr_config), 2315 from_user)) 2316 return -EFAULT; 2317 css_param->update_flag.anr_config = 2318 (struct atomisp_anr_config *) 2319 &css_param->anr_config; 2320 } 2321 2322 if (arg->xnr_config && (from_user || !cur_config->xnr_config)) { 2323 if (copy_from_compatible(&css_param->xnr_config, 2324 arg->xnr_config, 2325 sizeof(struct ia_css_xnr_config), 2326 from_user)) 2327 return -EFAULT; 2328 css_param->update_flag.xnr_config = 2329 (struct atomisp_xnr_config *) 2330 &css_param->xnr_config; 2331 } 2332 2333 if (arg->yuv2rgb_cc_config && 2334 (from_user || !cur_config->yuv2rgb_cc_config)) { 2335 if (copy_from_compatible(&css_param->yuv2rgb_cc_config, 2336 arg->yuv2rgb_cc_config, 2337 sizeof(struct ia_css_cc_config), 2338 from_user)) 2339 return -EFAULT; 2340 css_param->update_flag.yuv2rgb_cc_config = 2341 (struct atomisp_cc_config *) 2342 &css_param->yuv2rgb_cc_config; 2343 } 2344 2345 if (arg->rgb2yuv_cc_config && 2346 (from_user || !cur_config->rgb2yuv_cc_config)) { 2347 if (copy_from_compatible(&css_param->rgb2yuv_cc_config, 2348 arg->rgb2yuv_cc_config, 2349 sizeof(struct ia_css_cc_config), 2350 from_user)) 2351 return -EFAULT; 2352 css_param->update_flag.rgb2yuv_cc_config = 2353 (struct atomisp_cc_config *) 2354 &css_param->rgb2yuv_cc_config; 2355 } 2356 2357 if (arg->macc_table && (from_user || !cur_config->macc_table)) { 2358 if (copy_from_compatible(&css_param->macc_table, 2359 arg->macc_table, 2360 sizeof(struct ia_css_macc_table), 2361 from_user)) 2362 return -EFAULT; 2363 css_param->update_flag.macc_table = 2364 (struct atomisp_macc_table *) 2365 &css_param->macc_table; 2366 } 2367 2368 if (arg->xnr_table && (from_user || !cur_config->xnr_table)) { 2369 if (copy_from_compatible(&css_param->xnr_table, 2370 arg->xnr_table, 2371 sizeof(struct ia_css_xnr_table), 2372 from_user)) 2373 return -EFAULT; 2374 css_param->update_flag.xnr_table = 2375 (struct atomisp_xnr_table *)&css_param->xnr_table; 2376 } 2377 2378 if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) { 2379 if (copy_from_compatible(&css_param->r_gamma_table, 2380 arg->r_gamma_table, 2381 sizeof(struct ia_css_rgb_gamma_table), 2382 from_user)) 2383 return -EFAULT; 2384 css_param->update_flag.r_gamma_table = 2385 (struct atomisp_rgb_gamma_table *) 2386 &css_param->r_gamma_table; 2387 } 2388 2389 if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) { 2390 if (copy_from_compatible(&css_param->g_gamma_table, 2391 arg->g_gamma_table, 2392 sizeof(struct ia_css_rgb_gamma_table), 2393 from_user)) 2394 return -EFAULT; 2395 css_param->update_flag.g_gamma_table = 2396 (struct atomisp_rgb_gamma_table *) 2397 &css_param->g_gamma_table; 2398 } 2399 2400 if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) { 2401 if (copy_from_compatible(&css_param->b_gamma_table, 2402 arg->b_gamma_table, 2403 sizeof(struct ia_css_rgb_gamma_table), 2404 from_user)) 2405 return -EFAULT; 2406 css_param->update_flag.b_gamma_table = 2407 (struct atomisp_rgb_gamma_table *) 2408 &css_param->b_gamma_table; 2409 } 2410 2411 if (arg->anr_thres && (from_user || !cur_config->anr_thres)) { 2412 if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres, 2413 sizeof(struct ia_css_anr_thres), 2414 from_user)) 2415 return -EFAULT; 2416 css_param->update_flag.anr_thres = 2417 (struct atomisp_anr_thres *)&css_param->anr_thres; 2418 } 2419 2420 if (from_user) 2421 css_param->isp_config_id = arg->isp_config_id; 2422 /* 2423 * These configurations are on used by ISP1.x, not for ISP2.x, 2424 * so do not handle them. see comments of ia_css_isp_config. 2425 * 1 cc_config 2426 * 2 ce_config 2427 * 3 de_config 2428 * 4 gc_config 2429 * 5 gamma_table 2430 * 6 ctc_table 2431 * 7 dvs_coefs 2432 */ 2433 return 0; 2434 } 2435 2436 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd, 2437 struct atomisp_shading_table *source_st, 2438 struct atomisp_css_params *css_param, 2439 bool from_user) 2440 { 2441 unsigned int i; 2442 unsigned int len_table; 2443 struct ia_css_shading_table *shading_table; 2444 struct ia_css_shading_table *old_table; 2445 struct atomisp_shading_table *st, dest_st; 2446 2447 if (!source_st) 2448 return 0; 2449 2450 if (!css_param) 2451 return -EINVAL; 2452 2453 if (!from_user && css_param->update_flag.shading_table) 2454 return 0; 2455 2456 if (IS_ISP2401) { 2457 if (copy_from_compatible(&dest_st, source_st, 2458 sizeof(struct atomisp_shading_table), 2459 from_user)) { 2460 dev_err(asd->isp->dev, "copy shading table failed!"); 2461 return -EFAULT; 2462 } 2463 st = &dest_st; 2464 } else { 2465 st = source_st; 2466 } 2467 2468 old_table = css_param->shading_table; 2469 2470 /* user config is to disable the shading table. */ 2471 if (!st->enable) { 2472 /* Generate a minimum table with enable = 0. */ 2473 shading_table = atomisp_css_shading_table_alloc(1, 1); 2474 if (!shading_table) 2475 return -ENOMEM; 2476 shading_table->enable = 0; 2477 goto set_lsc; 2478 } 2479 2480 /* Setting a new table. Validate first - all tables must be set */ 2481 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) { 2482 if (!st->data[i]) { 2483 dev_err(asd->isp->dev, "shading table validate failed"); 2484 return -EINVAL; 2485 } 2486 } 2487 2488 /* Shading table size per color */ 2489 if (st->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR || 2490 st->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) { 2491 dev_err(asd->isp->dev, "shading table w/h validate failed!"); 2492 return -EINVAL; 2493 } 2494 2495 shading_table = atomisp_css_shading_table_alloc(st->width, st->height); 2496 if (!shading_table) 2497 return -ENOMEM; 2498 2499 len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE; 2500 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) { 2501 if (copy_from_compatible(shading_table->data[i], 2502 st->data[i], len_table, from_user)) { 2503 atomisp_css_shading_table_free(shading_table); 2504 return -EFAULT; 2505 } 2506 } 2507 shading_table->sensor_width = st->sensor_width; 2508 shading_table->sensor_height = st->sensor_height; 2509 shading_table->fraction_bits = st->fraction_bits; 2510 shading_table->enable = st->enable; 2511 2512 /* No need to update shading table if it is the same */ 2513 if (old_table && 2514 old_table->sensor_width == shading_table->sensor_width && 2515 old_table->sensor_height == shading_table->sensor_height && 2516 old_table->width == shading_table->width && 2517 old_table->height == shading_table->height && 2518 old_table->fraction_bits == shading_table->fraction_bits && 2519 old_table->enable == shading_table->enable) { 2520 bool data_is_same = true; 2521 2522 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) { 2523 if (memcmp(shading_table->data[i], old_table->data[i], 2524 len_table) != 0) { 2525 data_is_same = false; 2526 break; 2527 } 2528 } 2529 2530 if (data_is_same) { 2531 atomisp_css_shading_table_free(shading_table); 2532 return 0; 2533 } 2534 } 2535 2536 set_lsc: 2537 /* set LSC to CSS */ 2538 css_param->shading_table = shading_table; 2539 css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table; 2540 asd->params.sc_en = shading_table; 2541 2542 if (old_table) 2543 atomisp_css_shading_table_free(old_table); 2544 2545 return 0; 2546 } 2547 2548 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd, 2549 struct ia_css_dvs2_coefficients *coefs, 2550 struct atomisp_css_params *css_param, 2551 bool from_user) 2552 { 2553 struct ia_css_dvs_grid_info *cur = 2554 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info); 2555 int dvs_hor_coef_bytes, dvs_ver_coef_bytes; 2556 struct ia_css_dvs2_coefficients dvs2_coefs; 2557 2558 if (!coefs || !cur) 2559 return 0; 2560 2561 if (!from_user && css_param->update_flag.dvs2_coefs) 2562 return 0; 2563 2564 if (!IS_ISP2401) { 2565 if (sizeof(*cur) != sizeof(coefs->grid) || 2566 memcmp(&coefs->grid, cur, sizeof(coefs->grid))) { 2567 dev_err(asd->isp->dev, "dvs grid mismatch!\n"); 2568 /* If the grid info in the argument differs from the current 2569 grid info, we tell the caller to reset the grid size and 2570 try again. */ 2571 return -EAGAIN; 2572 } 2573 2574 if (!coefs->hor_coefs.odd_real || 2575 !coefs->hor_coefs.odd_imag || 2576 !coefs->hor_coefs.even_real || 2577 !coefs->hor_coefs.even_imag || 2578 !coefs->ver_coefs.odd_real || 2579 !coefs->ver_coefs.odd_imag || 2580 !coefs->ver_coefs.even_real || 2581 !coefs->ver_coefs.even_imag) 2582 return -EINVAL; 2583 2584 if (!css_param->dvs2_coeff) { 2585 /* DIS coefficients. */ 2586 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur); 2587 if (!css_param->dvs2_coeff) 2588 return -ENOMEM; 2589 } 2590 2591 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes; 2592 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes; 2593 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real, 2594 coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) || 2595 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag, 2596 coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) || 2597 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real, 2598 coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) || 2599 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag, 2600 coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) || 2601 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real, 2602 coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) || 2603 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag, 2604 coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) || 2605 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real, 2606 coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) || 2607 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag, 2608 coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) { 2609 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff); 2610 css_param->dvs2_coeff = NULL; 2611 return -EFAULT; 2612 } 2613 } else { 2614 if (copy_from_compatible(&dvs2_coefs, coefs, 2615 sizeof(struct ia_css_dvs2_coefficients), 2616 from_user)) { 2617 dev_err(asd->isp->dev, "copy dvs2 coef failed"); 2618 return -EFAULT; 2619 } 2620 2621 if (sizeof(*cur) != sizeof(dvs2_coefs.grid) || 2622 memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) { 2623 dev_err(asd->isp->dev, "dvs grid mismatch!\n"); 2624 /* If the grid info in the argument differs from the current 2625 grid info, we tell the caller to reset the grid size and 2626 try again. */ 2627 return -EAGAIN; 2628 } 2629 2630 if (!dvs2_coefs.hor_coefs.odd_real || 2631 !dvs2_coefs.hor_coefs.odd_imag || 2632 !dvs2_coefs.hor_coefs.even_real || 2633 !dvs2_coefs.hor_coefs.even_imag || 2634 !dvs2_coefs.ver_coefs.odd_real || 2635 !dvs2_coefs.ver_coefs.odd_imag || 2636 !dvs2_coefs.ver_coefs.even_real || 2637 !dvs2_coefs.ver_coefs.even_imag) 2638 return -EINVAL; 2639 2640 if (!css_param->dvs2_coeff) { 2641 /* DIS coefficients. */ 2642 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur); 2643 if (!css_param->dvs2_coeff) 2644 return -ENOMEM; 2645 } 2646 2647 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes; 2648 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes; 2649 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real, 2650 dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) || 2651 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag, 2652 dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) || 2653 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real, 2654 dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) || 2655 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag, 2656 dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) || 2657 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real, 2658 dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) || 2659 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag, 2660 dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) || 2661 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real, 2662 dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) || 2663 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag, 2664 dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) { 2665 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff); 2666 css_param->dvs2_coeff = NULL; 2667 return -EFAULT; 2668 } 2669 } 2670 2671 css_param->update_flag.dvs2_coefs = 2672 (struct atomisp_dis_coefficients *)css_param->dvs2_coeff; 2673 return 0; 2674 } 2675 2676 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd, 2677 struct atomisp_dvs_6axis_config *source_6axis_config, 2678 struct atomisp_css_params *css_param, 2679 bool from_user) 2680 { 2681 struct ia_css_dvs_6axis_config *dvs_6axis_config; 2682 struct ia_css_dvs_6axis_config *old_6axis_config; 2683 struct ia_css_stream *stream = 2684 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream; 2685 struct ia_css_dvs_grid_info *dvs_grid_info = 2686 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info); 2687 int ret = -EFAULT; 2688 2689 if (!stream) { 2690 dev_err(asd->isp->dev, "%s: internal error!", __func__); 2691 return -EINVAL; 2692 } 2693 2694 if (!source_6axis_config || !dvs_grid_info) 2695 return 0; 2696 2697 if (!dvs_grid_info->enable) 2698 return 0; 2699 2700 if (!from_user && css_param->update_flag.dvs_6axis_config) 2701 return 0; 2702 2703 /* check whether need to reallocate for 6 axis config */ 2704 old_6axis_config = css_param->dvs_6axis; 2705 dvs_6axis_config = old_6axis_config; 2706 2707 if (IS_ISP2401) { 2708 struct ia_css_dvs_6axis_config t_6axis_config; 2709 2710 if (copy_from_compatible(&t_6axis_config, source_6axis_config, 2711 sizeof(struct atomisp_dvs_6axis_config), 2712 from_user)) { 2713 dev_err(asd->isp->dev, "copy morph table failed!"); 2714 return -EFAULT; 2715 } 2716 2717 if (old_6axis_config && 2718 (old_6axis_config->width_y != t_6axis_config.width_y || 2719 old_6axis_config->height_y != t_6axis_config.height_y || 2720 old_6axis_config->width_uv != t_6axis_config.width_uv || 2721 old_6axis_config->height_uv != t_6axis_config.height_uv)) { 2722 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis); 2723 css_param->dvs_6axis = NULL; 2724 2725 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream); 2726 if (!dvs_6axis_config) 2727 return -ENOMEM; 2728 } else if (!dvs_6axis_config) { 2729 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream); 2730 if (!dvs_6axis_config) 2731 return -ENOMEM; 2732 } 2733 2734 dvs_6axis_config->exp_id = t_6axis_config.exp_id; 2735 2736 if (copy_from_compatible(dvs_6axis_config->xcoords_y, 2737 t_6axis_config.xcoords_y, 2738 t_6axis_config.width_y * 2739 t_6axis_config.height_y * 2740 sizeof(*dvs_6axis_config->xcoords_y), 2741 from_user)) 2742 goto error; 2743 if (copy_from_compatible(dvs_6axis_config->ycoords_y, 2744 t_6axis_config.ycoords_y, 2745 t_6axis_config.width_y * 2746 t_6axis_config.height_y * 2747 sizeof(*dvs_6axis_config->ycoords_y), 2748 from_user)) 2749 goto error; 2750 if (copy_from_compatible(dvs_6axis_config->xcoords_uv, 2751 t_6axis_config.xcoords_uv, 2752 t_6axis_config.width_uv * 2753 t_6axis_config.height_uv * 2754 sizeof(*dvs_6axis_config->xcoords_uv), 2755 from_user)) 2756 goto error; 2757 if (copy_from_compatible(dvs_6axis_config->ycoords_uv, 2758 t_6axis_config.ycoords_uv, 2759 t_6axis_config.width_uv * 2760 t_6axis_config.height_uv * 2761 sizeof(*dvs_6axis_config->ycoords_uv), 2762 from_user)) 2763 goto error; 2764 } else { 2765 if (old_6axis_config && 2766 (old_6axis_config->width_y != source_6axis_config->width_y || 2767 old_6axis_config->height_y != source_6axis_config->height_y || 2768 old_6axis_config->width_uv != source_6axis_config->width_uv || 2769 old_6axis_config->height_uv != source_6axis_config->height_uv)) { 2770 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis); 2771 css_param->dvs_6axis = NULL; 2772 2773 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream); 2774 if (!dvs_6axis_config) { 2775 ret = -ENOMEM; 2776 goto error; 2777 } 2778 } else if (!dvs_6axis_config) { 2779 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream); 2780 if (!dvs_6axis_config) { 2781 ret = -ENOMEM; 2782 goto error; 2783 } 2784 } 2785 2786 dvs_6axis_config->exp_id = source_6axis_config->exp_id; 2787 2788 if (copy_from_compatible(dvs_6axis_config->xcoords_y, 2789 source_6axis_config->xcoords_y, 2790 source_6axis_config->width_y * 2791 source_6axis_config->height_y * 2792 sizeof(*source_6axis_config->xcoords_y), 2793 from_user)) 2794 goto error; 2795 if (copy_from_compatible(dvs_6axis_config->ycoords_y, 2796 source_6axis_config->ycoords_y, 2797 source_6axis_config->width_y * 2798 source_6axis_config->height_y * 2799 sizeof(*source_6axis_config->ycoords_y), 2800 from_user)) 2801 goto error; 2802 if (copy_from_compatible(dvs_6axis_config->xcoords_uv, 2803 source_6axis_config->xcoords_uv, 2804 source_6axis_config->width_uv * 2805 source_6axis_config->height_uv * 2806 sizeof(*source_6axis_config->xcoords_uv), 2807 from_user)) 2808 goto error; 2809 if (copy_from_compatible(dvs_6axis_config->ycoords_uv, 2810 source_6axis_config->ycoords_uv, 2811 source_6axis_config->width_uv * 2812 source_6axis_config->height_uv * 2813 sizeof(*source_6axis_config->ycoords_uv), 2814 from_user)) 2815 goto error; 2816 } 2817 css_param->dvs_6axis = dvs_6axis_config; 2818 css_param->update_flag.dvs_6axis_config = 2819 (struct atomisp_dvs_6axis_config *)dvs_6axis_config; 2820 return 0; 2821 2822 error: 2823 if (dvs_6axis_config) 2824 ia_css_dvs2_6axis_config_free(dvs_6axis_config); 2825 return ret; 2826 } 2827 2828 int atomisp_cp_morph_table(struct atomisp_sub_device *asd, 2829 struct atomisp_morph_table *source_morph_table, 2830 struct atomisp_css_params *css_param, 2831 bool from_user) 2832 { 2833 int ret = -EFAULT; 2834 unsigned int i; 2835 struct ia_css_morph_table *morph_table; 2836 struct ia_css_morph_table *old_morph_table; 2837 2838 if (!source_morph_table) 2839 return 0; 2840 2841 if (!from_user && css_param->update_flag.morph_table) 2842 return 0; 2843 2844 old_morph_table = css_param->morph_table; 2845 2846 if (IS_ISP2401) { 2847 struct ia_css_morph_table mtbl; 2848 2849 if (copy_from_compatible(&mtbl, source_morph_table, 2850 sizeof(struct atomisp_morph_table), 2851 from_user)) { 2852 dev_err(asd->isp->dev, "copy morph table failed!"); 2853 return -EFAULT; 2854 } 2855 2856 morph_table = atomisp_css_morph_table_allocate( 2857 mtbl.width, 2858 mtbl.height); 2859 if (!morph_table) 2860 return -ENOMEM; 2861 2862 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) { 2863 if (copy_from_compatible(morph_table->coordinates_x[i], 2864 (__force void *)source_morph_table->coordinates_x[i], 2865 mtbl.height * mtbl.width * 2866 sizeof(*morph_table->coordinates_x[i]), 2867 from_user)) 2868 goto error; 2869 2870 if (copy_from_compatible(morph_table->coordinates_y[i], 2871 (__force void *)source_morph_table->coordinates_y[i], 2872 mtbl.height * mtbl.width * 2873 sizeof(*morph_table->coordinates_y[i]), 2874 from_user)) 2875 goto error; 2876 } 2877 } else { 2878 morph_table = atomisp_css_morph_table_allocate( 2879 source_morph_table->width, 2880 source_morph_table->height); 2881 if (!morph_table) { 2882 ret = -ENOMEM; 2883 goto error; 2884 } 2885 2886 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) { 2887 if (copy_from_compatible(morph_table->coordinates_x[i], 2888 (__force void *)source_morph_table->coordinates_x[i], 2889 source_morph_table->height * source_morph_table->width * 2890 sizeof(*source_morph_table->coordinates_x[i]), 2891 from_user)) 2892 goto error; 2893 2894 if (copy_from_compatible(morph_table->coordinates_y[i], 2895 (__force void *)source_morph_table->coordinates_y[i], 2896 source_morph_table->height * source_morph_table->width * 2897 sizeof(*source_morph_table->coordinates_y[i]), 2898 from_user)) 2899 goto error; 2900 } 2901 } 2902 2903 css_param->morph_table = morph_table; 2904 if (old_morph_table) 2905 atomisp_css_morph_table_free(old_morph_table); 2906 css_param->update_flag.morph_table = 2907 (struct atomisp_morph_table *)morph_table; 2908 return 0; 2909 2910 error: 2911 if (morph_table) 2912 atomisp_css_morph_table_free(morph_table); 2913 return ret; 2914 } 2915 2916 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd, 2917 struct atomisp_parameters *arg, 2918 struct atomisp_css_params *css_param) 2919 { 2920 int ret; 2921 2922 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false); 2923 if (ret) 2924 return ret; 2925 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false); 2926 if (ret) 2927 return ret; 2928 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false); 2929 if (ret) 2930 return ret; 2931 ret = atomisp_css_cp_dvs2_coefs(asd, 2932 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs, 2933 css_param, false); 2934 if (ret) 2935 return ret; 2936 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config, 2937 css_param, false); 2938 return ret; 2939 } 2940 2941 void atomisp_free_css_parameters(struct atomisp_css_params *css_param) 2942 { 2943 if (css_param->dvs_6axis) { 2944 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis); 2945 css_param->dvs_6axis = NULL; 2946 } 2947 if (css_param->dvs2_coeff) { 2948 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff); 2949 css_param->dvs2_coeff = NULL; 2950 } 2951 if (css_param->shading_table) { 2952 ia_css_shading_table_free(css_param->shading_table); 2953 css_param->shading_table = NULL; 2954 } 2955 if (css_param->morph_table) { 2956 ia_css_morph_table_free(css_param->morph_table); 2957 css_param->morph_table = NULL; 2958 } 2959 } 2960 2961 static void atomisp_move_frame_to_activeq(struct ia_css_frame *frame, 2962 struct atomisp_css_params_with_list *param) 2963 { 2964 struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf); 2965 unsigned long irqflags; 2966 2967 pipe->frame_params[frame->vb.vb2_buf.index] = param; 2968 spin_lock_irqsave(&pipe->irq_lock, irqflags); 2969 list_move_tail(&frame->queue, &pipe->activeq); 2970 spin_unlock_irqrestore(&pipe->irq_lock, irqflags); 2971 } 2972 2973 /* 2974 * Check parameter queue list and buffer queue list to find out if matched items 2975 * and then set parameter to CSS and enqueue buffer to CSS. 2976 * Of course, if the buffer in buffer waiting list is not bound to a per-frame 2977 * parameter, it will be enqueued into CSS as long as the per-frame setting 2978 * buffers before it get enqueued. 2979 */ 2980 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe) 2981 { 2982 struct atomisp_sub_device *asd = pipe->asd; 2983 struct ia_css_frame *frame = NULL, *frame_tmp; 2984 struct atomisp_css_params_with_list *param = NULL, *param_tmp; 2985 bool need_to_enqueue_buffer = false; 2986 int i; 2987 2988 lockdep_assert_held(&asd->isp->mutex); 2989 2990 /* 2991 * CSS/FW requires set parameter and enqueue buffer happen after ISP 2992 * is streamon. 2993 */ 2994 if (!asd->streaming) 2995 return; 2996 2997 if (list_empty(&pipe->per_frame_params) || 2998 list_empty(&pipe->buffers_waiting_for_param)) 2999 return; 3000 3001 list_for_each_entry_safe(frame, frame_tmp, 3002 &pipe->buffers_waiting_for_param, queue) { 3003 i = frame->vb.vb2_buf.index; 3004 if (pipe->frame_request_config_id[i]) { 3005 list_for_each_entry_safe(param, param_tmp, 3006 &pipe->per_frame_params, list) { 3007 if (pipe->frame_request_config_id[i] != param->params.isp_config_id) 3008 continue; 3009 3010 list_del(¶m->list); 3011 3012 /* 3013 * clear the request config id as the buffer 3014 * will be handled and enqueued into CSS soon 3015 */ 3016 pipe->frame_request_config_id[i] = 0; 3017 atomisp_move_frame_to_activeq(frame, param); 3018 need_to_enqueue_buffer = true; 3019 break; 3020 } 3021 3022 /* If this is the end, stop further loop */ 3023 if (list_entry_is_head(param, &pipe->per_frame_params, list)) 3024 break; 3025 } else { 3026 atomisp_move_frame_to_activeq(frame, NULL); 3027 need_to_enqueue_buffer = true; 3028 } 3029 } 3030 3031 if (!need_to_enqueue_buffer) 3032 return; 3033 3034 atomisp_qbuffers_to_css(asd); 3035 } 3036 3037 /* 3038 * Function to configure ISP parameters 3039 */ 3040 int atomisp_set_parameters(struct video_device *vdev, 3041 struct atomisp_parameters *arg) 3042 { 3043 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev); 3044 struct atomisp_sub_device *asd = pipe->asd; 3045 struct atomisp_css_params_with_list *param = NULL; 3046 struct atomisp_css_params *css_param = &asd->params.css_param; 3047 int ret; 3048 3049 lockdep_assert_held(&asd->isp->mutex); 3050 3051 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) { 3052 dev_err(asd->isp->dev, "%s: internal error!\n", __func__); 3053 return -EINVAL; 3054 } 3055 3056 dev_dbg(asd->isp->dev, "set parameter(per_frame_setting %d) isp_config_id %d of %s\n", 3057 arg->per_frame_setting, arg->isp_config_id, vdev->name); 3058 3059 if (arg->per_frame_setting) { 3060 /* 3061 * Per-frame setting enabled, we allocate a new parameter 3062 * buffer to cache the parameters and only when frame buffers 3063 * are ready, the parameters will be set to CSS. 3064 * per-frame setting only works for the main output frame. 3065 */ 3066 param = kvzalloc(sizeof(*param), GFP_KERNEL); 3067 if (!param) { 3068 dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n", 3069 __func__); 3070 return -ENOMEM; 3071 } 3072 css_param = ¶m->params; 3073 } 3074 3075 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true); 3076 if (ret) 3077 goto apply_parameter_failed; 3078 3079 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true); 3080 if (ret) 3081 goto apply_parameter_failed; 3082 3083 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true); 3084 if (ret) 3085 goto apply_parameter_failed; 3086 3087 ret = atomisp_css_cp_dvs2_coefs(asd, 3088 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs, 3089 css_param, true); 3090 if (ret) 3091 goto apply_parameter_failed; 3092 3093 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config, 3094 css_param, true); 3095 if (ret) 3096 goto apply_parameter_failed; 3097 3098 if (!arg->per_frame_setting) { 3099 /* indicate to CSS that we have parameters to be updated */ 3100 asd->params.css_update_params_needed = true; 3101 } else { 3102 list_add_tail(¶m->list, &pipe->per_frame_params); 3103 atomisp_handle_parameter_and_buffer(pipe); 3104 } 3105 3106 return 0; 3107 3108 apply_parameter_failed: 3109 if (css_param) 3110 atomisp_free_css_parameters(css_param); 3111 kvfree(param); 3112 3113 return ret; 3114 } 3115 3116 /* 3117 * Function to set/get isp parameters to isp 3118 */ 3119 int atomisp_param(struct atomisp_sub_device *asd, int flag, 3120 struct atomisp_parm *config) 3121 { 3122 struct ia_css_pipe_config *vp_cfg = 3123 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]. 3124 pipe_configs[IA_CSS_PIPE_ID_VIDEO]; 3125 3126 /* Read parameter for 3A binary info */ 3127 if (flag == 0) { 3128 struct ia_css_dvs_grid_info *dvs_grid_info = 3129 atomisp_css_get_dvs_grid_info( 3130 &asd->params.curr_grid_info); 3131 3132 atomisp_curr_user_grid_info(asd, &config->info); 3133 3134 /* We always return the resolution and stride even if there is 3135 * no valid metadata. This allows the caller to get the 3136 * information needed to allocate user-space buffers. */ 3137 config->metadata_config.metadata_height = asd-> 3138 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info. 3139 metadata_info.resolution.height; 3140 config->metadata_config.metadata_stride = asd-> 3141 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info. 3142 metadata_info.stride; 3143 3144 /* update dvs grid info */ 3145 if (dvs_grid_info) 3146 memcpy(&config->dvs_grid, 3147 dvs_grid_info, 3148 sizeof(struct ia_css_dvs_grid_info)); 3149 3150 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) { 3151 config->dvs_envelop.width = 0; 3152 config->dvs_envelop.height = 0; 3153 return 0; 3154 } 3155 3156 /* update dvs envelop info */ 3157 config->dvs_envelop.width = vp_cfg->dvs_envelope.width; 3158 config->dvs_envelop.height = vp_cfg->dvs_envelope.height; 3159 return 0; 3160 } 3161 3162 memcpy(&asd->params.css_param.wb_config, &config->wb_config, 3163 sizeof(struct ia_css_wb_config)); 3164 memcpy(&asd->params.css_param.ob_config, &config->ob_config, 3165 sizeof(struct ia_css_ob_config)); 3166 memcpy(&asd->params.css_param.dp_config, &config->dp_config, 3167 sizeof(struct ia_css_dp_config)); 3168 memcpy(&asd->params.css_param.de_config, &config->de_config, 3169 sizeof(struct ia_css_de_config)); 3170 memcpy(&asd->params.css_param.dz_config, &config->dz_config, 3171 sizeof(struct ia_css_dz_config)); 3172 memcpy(&asd->params.css_param.ce_config, &config->ce_config, 3173 sizeof(struct ia_css_ce_config)); 3174 memcpy(&asd->params.css_param.nr_config, &config->nr_config, 3175 sizeof(struct ia_css_nr_config)); 3176 memcpy(&asd->params.css_param.ee_config, &config->ee_config, 3177 sizeof(struct ia_css_ee_config)); 3178 memcpy(&asd->params.css_param.tnr_config, &config->tnr_config, 3179 sizeof(struct ia_css_tnr_config)); 3180 3181 if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) { 3182 asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3]; 3183 asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4]; 3184 asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5]; 3185 asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6]; 3186 asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7]; 3187 asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8]; 3188 } 3189 3190 if (asd->params.color_effect != V4L2_COLORFX_SEPIA && 3191 asd->params.color_effect != V4L2_COLORFX_BW) { 3192 memcpy(&asd->params.css_param.cc_config, &config->cc_config, 3193 sizeof(struct ia_css_cc_config)); 3194 asd->params.config.cc_config = &asd->params.css_param.cc_config; 3195 } 3196 3197 asd->params.config.wb_config = &asd->params.css_param.wb_config; 3198 asd->params.config.ob_config = &asd->params.css_param.ob_config; 3199 asd->params.config.de_config = &asd->params.css_param.de_config; 3200 asd->params.config.dz_config = &asd->params.css_param.dz_config; 3201 asd->params.config.ce_config = &asd->params.css_param.ce_config; 3202 asd->params.config.dp_config = &asd->params.css_param.dp_config; 3203 asd->params.config.nr_config = &asd->params.css_param.nr_config; 3204 asd->params.config.ee_config = &asd->params.css_param.ee_config; 3205 asd->params.config.tnr_config = &asd->params.css_param.tnr_config; 3206 asd->params.css_update_params_needed = true; 3207 3208 return 0; 3209 } 3210 3211 /* 3212 * Function to configure color effect of the image 3213 */ 3214 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag, 3215 __s32 *effect) 3216 { 3217 struct ia_css_cc_config *cc_config = NULL; 3218 struct ia_css_macc_table *macc_table = NULL; 3219 struct ia_css_ctc_table *ctc_table = NULL; 3220 int ret = 0; 3221 struct v4l2_control control; 3222 struct atomisp_device *isp = asd->isp; 3223 3224 if (flag == 0) { 3225 *effect = asd->params.color_effect; 3226 return 0; 3227 } 3228 3229 control.id = V4L2_CID_COLORFX; 3230 control.value = *effect; 3231 ret = 3232 v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler, 3233 &control); 3234 /* 3235 * if set color effect to sensor successfully, return 3236 * 0 directly. 3237 */ 3238 if (!ret) { 3239 asd->params.color_effect = (u32)*effect; 3240 return 0; 3241 } 3242 3243 if (*effect == asd->params.color_effect) 3244 return 0; 3245 3246 /* 3247 * isp_subdev->params.macc_en should be set to false. 3248 */ 3249 asd->params.macc_en = false; 3250 3251 switch (*effect) { 3252 case V4L2_COLORFX_NONE: 3253 macc_table = &asd->params.css_param.macc_table; 3254 asd->params.macc_en = true; 3255 break; 3256 case V4L2_COLORFX_SEPIA: 3257 cc_config = &sepia_cc_config; 3258 break; 3259 case V4L2_COLORFX_NEGATIVE: 3260 cc_config = &nega_cc_config; 3261 break; 3262 case V4L2_COLORFX_BW: 3263 cc_config = &mono_cc_config; 3264 break; 3265 case V4L2_COLORFX_SKY_BLUE: 3266 macc_table = &blue_macc_table; 3267 asd->params.macc_en = true; 3268 break; 3269 case V4L2_COLORFX_GRASS_GREEN: 3270 macc_table = &green_macc_table; 3271 asd->params.macc_en = true; 3272 break; 3273 case V4L2_COLORFX_SKIN_WHITEN_LOW: 3274 macc_table = &skin_low_macc_table; 3275 asd->params.macc_en = true; 3276 break; 3277 case V4L2_COLORFX_SKIN_WHITEN: 3278 macc_table = &skin_medium_macc_table; 3279 asd->params.macc_en = true; 3280 break; 3281 case V4L2_COLORFX_SKIN_WHITEN_HIGH: 3282 macc_table = &skin_high_macc_table; 3283 asd->params.macc_en = true; 3284 break; 3285 case V4L2_COLORFX_VIVID: 3286 ctc_table = &vivid_ctc_table; 3287 break; 3288 default: 3289 return -EINVAL; 3290 } 3291 atomisp_update_capture_mode(asd); 3292 3293 if (cc_config) 3294 asd->params.config.cc_config = cc_config; 3295 if (macc_table) 3296 asd->params.config.macc_table = macc_table; 3297 if (ctc_table) 3298 atomisp_css_set_ctc_table(asd, ctc_table); 3299 asd->params.color_effect = (u32)*effect; 3300 asd->params.css_update_params_needed = true; 3301 return 0; 3302 } 3303 3304 /* 3305 * Function to configure bad pixel correction 3306 */ 3307 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag, 3308 __s32 *value) 3309 { 3310 if (flag == 0) { 3311 *value = asd->params.bad_pixel_en; 3312 return 0; 3313 } 3314 asd->params.bad_pixel_en = !!*value; 3315 3316 return 0; 3317 } 3318 3319 /* 3320 * Function to configure bad pixel correction params 3321 */ 3322 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag, 3323 struct atomisp_dp_config *config) 3324 { 3325 if (flag == 0) { 3326 /* Get bad pixel from current setup */ 3327 if (atomisp_css_get_dp_config(asd, config)) 3328 return -EINVAL; 3329 } else { 3330 /* Set bad pixel to isp parameters */ 3331 memcpy(&asd->params.css_param.dp_config, config, 3332 sizeof(asd->params.css_param.dp_config)); 3333 asd->params.config.dp_config = &asd->params.css_param.dp_config; 3334 asd->params.css_update_params_needed = true; 3335 } 3336 3337 return 0; 3338 } 3339 3340 /* 3341 * Function to enable/disable video image stablization 3342 */ 3343 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag, 3344 __s32 *value) 3345 { 3346 if (flag == 0) 3347 *value = asd->params.video_dis_en; 3348 else 3349 asd->params.video_dis_en = !!*value; 3350 3351 return 0; 3352 } 3353 3354 /* 3355 * Function to configure fixed pattern noise 3356 */ 3357 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag, 3358 __s32 *value) 3359 { 3360 if (flag == 0) { 3361 *value = asd->params.fpn_en; 3362 return 0; 3363 } 3364 3365 if (*value == 0) { 3366 asd->params.fpn_en = false; 3367 return 0; 3368 } 3369 3370 /* Add function to get black from from sensor with shutter off */ 3371 return 0; 3372 } 3373 3374 static unsigned int 3375 atomisp_bytesperline_to_padded_width(unsigned int bytesperline, 3376 enum ia_css_frame_format format) 3377 { 3378 switch (format) { 3379 case IA_CSS_FRAME_FORMAT_UYVY: 3380 case IA_CSS_FRAME_FORMAT_YUYV: 3381 case IA_CSS_FRAME_FORMAT_RAW: 3382 case IA_CSS_FRAME_FORMAT_RGB565: 3383 return bytesperline / 2; 3384 case IA_CSS_FRAME_FORMAT_RGBA888: 3385 return bytesperline / 4; 3386 /* The following cases could be removed, but we leave them 3387 in to document the formats that are included. */ 3388 case IA_CSS_FRAME_FORMAT_NV11: 3389 case IA_CSS_FRAME_FORMAT_NV12: 3390 case IA_CSS_FRAME_FORMAT_NV16: 3391 case IA_CSS_FRAME_FORMAT_NV21: 3392 case IA_CSS_FRAME_FORMAT_NV61: 3393 case IA_CSS_FRAME_FORMAT_YV12: 3394 case IA_CSS_FRAME_FORMAT_YV16: 3395 case IA_CSS_FRAME_FORMAT_YUV420: 3396 case IA_CSS_FRAME_FORMAT_YUV420_16: 3397 case IA_CSS_FRAME_FORMAT_YUV422: 3398 case IA_CSS_FRAME_FORMAT_YUV422_16: 3399 case IA_CSS_FRAME_FORMAT_YUV444: 3400 case IA_CSS_FRAME_FORMAT_YUV_LINE: 3401 case IA_CSS_FRAME_FORMAT_PLANAR_RGB888: 3402 case IA_CSS_FRAME_FORMAT_QPLANE6: 3403 case IA_CSS_FRAME_FORMAT_BINARY_8: 3404 default: 3405 return bytesperline; 3406 } 3407 } 3408 3409 static int 3410 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg, 3411 struct ia_css_frame **result) 3412 { 3413 struct ia_css_frame *res = NULL; 3414 unsigned int padded_width; 3415 enum ia_css_frame_format sh_format; 3416 char *tmp_buf = NULL; 3417 int ret = 0; 3418 3419 sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat); 3420 padded_width = atomisp_bytesperline_to_padded_width( 3421 arg->fmt.bytesperline, sh_format); 3422 3423 /* Note: the padded width on an ia_css_frame is in elements, not in 3424 bytes. The RAW frame we use here should always be a 16bit RAW 3425 frame. This is why we bytesperline/2 is equal to the padded with */ 3426 if (ia_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height, 3427 sh_format, padded_width, 0)) { 3428 ret = -ENOMEM; 3429 goto err; 3430 } 3431 3432 tmp_buf = vmalloc(arg->fmt.sizeimage); 3433 if (!tmp_buf) { 3434 ret = -ENOMEM; 3435 goto err; 3436 } 3437 if (copy_from_user(tmp_buf, (void __user __force *)arg->base, 3438 arg->fmt.sizeimage)) { 3439 ret = -EFAULT; 3440 goto err; 3441 } 3442 3443 if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) { 3444 ret = -EINVAL; 3445 goto err; 3446 } 3447 3448 err: 3449 if (ret && res) 3450 ia_css_frame_free(res); 3451 vfree(tmp_buf); 3452 if (ret == 0) 3453 *result = res; 3454 return ret; 3455 } 3456 3457 /* 3458 * Function to configure fixed pattern noise table 3459 */ 3460 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd, 3461 struct v4l2_framebuffer *arg) 3462 { 3463 struct ia_css_frame *raw_black_frame = NULL; 3464 int ret; 3465 3466 if (!arg) 3467 return -EINVAL; 3468 3469 ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame); 3470 if (ret) 3471 return ret; 3472 3473 if (sh_css_set_black_frame(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream, 3474 raw_black_frame) != 0) 3475 return -ENOMEM; 3476 3477 ia_css_frame_free(raw_black_frame); 3478 return ret; 3479 } 3480 3481 /* 3482 * Function to configure false color correction 3483 */ 3484 int atomisp_false_color(struct atomisp_sub_device *asd, int flag, 3485 __s32 *value) 3486 { 3487 /* Get nr config from current setup */ 3488 if (flag == 0) { 3489 *value = asd->params.false_color; 3490 return 0; 3491 } 3492 3493 /* Set nr config to isp parameters */ 3494 if (*value) { 3495 asd->params.config.de_config = NULL; 3496 } else { 3497 asd->params.css_param.de_config.pixelnoise = 0; 3498 asd->params.config.de_config = &asd->params.css_param.de_config; 3499 } 3500 asd->params.css_update_params_needed = true; 3501 asd->params.false_color = *value; 3502 return 0; 3503 } 3504 3505 /* 3506 * Function to configure bad pixel correction params 3507 */ 3508 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag, 3509 struct atomisp_de_config *config) 3510 { 3511 if (flag == 0) { 3512 /* Get false color from current setup */ 3513 if (atomisp_css_get_de_config(asd, config)) 3514 return -EINVAL; 3515 } else { 3516 /* Set false color to isp parameters */ 3517 memcpy(&asd->params.css_param.de_config, config, 3518 sizeof(asd->params.css_param.de_config)); 3519 asd->params.config.de_config = &asd->params.css_param.de_config; 3520 asd->params.css_update_params_needed = true; 3521 } 3522 3523 return 0; 3524 } 3525 3526 /* 3527 * Function to configure white balance params 3528 */ 3529 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag, 3530 struct atomisp_wb_config *config) 3531 { 3532 if (flag == 0) { 3533 /* Get white balance from current setup */ 3534 if (atomisp_css_get_wb_config(asd, config)) 3535 return -EINVAL; 3536 } else { 3537 /* Set white balance to isp parameters */ 3538 memcpy(&asd->params.css_param.wb_config, config, 3539 sizeof(asd->params.css_param.wb_config)); 3540 asd->params.config.wb_config = &asd->params.css_param.wb_config; 3541 asd->params.css_update_params_needed = true; 3542 } 3543 3544 return 0; 3545 } 3546 3547 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag, 3548 struct atomisp_3a_config *config) 3549 { 3550 struct atomisp_device *isp = asd->isp; 3551 3552 dev_dbg(isp->dev, ">%s %d\n", __func__, flag); 3553 3554 if (flag == 0) { 3555 /* Get white balance from current setup */ 3556 if (atomisp_css_get_3a_config(asd, config)) 3557 return -EINVAL; 3558 } else { 3559 /* Set white balance to isp parameters */ 3560 memcpy(&asd->params.css_param.s3a_config, config, 3561 sizeof(asd->params.css_param.s3a_config)); 3562 asd->params.config.s3a_config = &asd->params.css_param.s3a_config; 3563 asd->params.css_update_params_needed = true; 3564 } 3565 3566 dev_dbg(isp->dev, "<%s %d\n", __func__, flag); 3567 return 0; 3568 } 3569 3570 /* 3571 * Function to setup digital zoom 3572 */ 3573 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag, 3574 __s32 *value) 3575 { 3576 u32 zoom; 3577 struct atomisp_device *isp = asd->isp; 3578 3579 unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR; 3580 3581 if (flag == 0) { 3582 atomisp_css_get_zoom_factor(asd, &zoom); 3583 *value = max_zoom - zoom; 3584 } else { 3585 if (*value < 0) 3586 return -EINVAL; 3587 3588 zoom = max_zoom - min_t(u32, max_zoom - 1, *value); 3589 atomisp_css_set_zoom_factor(asd, zoom); 3590 3591 dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom); 3592 asd->params.css_update_params_needed = true; 3593 } 3594 3595 return 0; 3596 } 3597 3598 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd, 3599 u16 stream_index, struct atomisp_input_stream_info *stream_info) 3600 { 3601 int i; 3602 3603 /* assign virtual channel id return from sensor driver query */ 3604 asd->stream_env[stream_index].ch_id = stream_info->ch_id; 3605 asd->stream_env[stream_index].isys_configs = stream_info->isys_configs; 3606 for (i = 0; i < stream_info->isys_configs; i++) { 3607 asd->stream_env[stream_index].isys_info[i].input_format = 3608 stream_info->isys_info[i].input_format; 3609 asd->stream_env[stream_index].isys_info[i].width = 3610 stream_info->isys_info[i].width; 3611 asd->stream_env[stream_index].isys_info[i].height = 3612 stream_info->isys_info[i].height; 3613 } 3614 } 3615 3616 static void __atomisp_init_stream_info(u16 stream_index, 3617 struct atomisp_input_stream_info *stream_info) 3618 { 3619 int i; 3620 3621 stream_info->enable = 1; 3622 stream_info->stream = stream_index; 3623 stream_info->ch_id = 0; 3624 stream_info->isys_configs = 0; 3625 for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) { 3626 stream_info->isys_info[i].input_format = 0; 3627 stream_info->isys_info[i].width = 0; 3628 stream_info->isys_info[i].height = 0; 3629 } 3630 } 3631 3632 static void atomisp_fill_pix_format(struct v4l2_pix_format *f, 3633 u32 width, u32 height, 3634 const struct atomisp_format_bridge *br_fmt) 3635 { 3636 u32 bytes; 3637 3638 f->width = width; 3639 f->height = height; 3640 f->pixelformat = br_fmt->pixelformat; 3641 3642 /* Adding padding to width for bytesperline calculation */ 3643 width = ia_css_frame_pad_width(width, br_fmt->sh_fmt); 3644 bytes = BITS_TO_BYTES(br_fmt->depth * width); 3645 3646 if (br_fmt->planar) 3647 f->bytesperline = width; 3648 else 3649 f->bytesperline = bytes; 3650 3651 f->sizeimage = PAGE_ALIGN(height * bytes); 3652 3653 if (f->field == V4L2_FIELD_ANY) 3654 f->field = V4L2_FIELD_NONE; 3655 3656 /* 3657 * FIXME: do we need to set this up differently, depending on the 3658 * sensor or the pipeline? 3659 */ 3660 f->colorspace = V4L2_COLORSPACE_REC709; 3661 f->ycbcr_enc = V4L2_YCBCR_ENC_709; 3662 f->xfer_func = V4L2_XFER_FUNC_709; 3663 } 3664 3665 /* Get sensor padding values for the non padded width x height resolution */ 3666 void atomisp_get_padding(struct atomisp_device *isp, u32 width, u32 height, 3667 u32 *padding_w, u32 *padding_h) 3668 { 3669 struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr]; 3670 struct v4l2_rect native_rect = input->native_rect; 3671 const struct atomisp_in_fmt_conv *fc = NULL; 3672 u32 min_pad_w = ISP2400_MIN_PAD_W; 3673 u32 min_pad_h = ISP2400_MIN_PAD_H; 3674 struct v4l2_mbus_framefmt *sink; 3675 3676 if (!input->crop_support) { 3677 *padding_w = pad_w; 3678 *padding_h = pad_h; 3679 return; 3680 } 3681 3682 width = min(width, input->active_rect.width); 3683 height = min(height, input->active_rect.height); 3684 3685 if (input->binning_support && width <= (input->active_rect.width / 2) && 3686 height <= (input->active_rect.height / 2)) { 3687 native_rect.width /= 2; 3688 native_rect.height /= 2; 3689 } 3690 3691 *padding_w = min_t(u32, (native_rect.width - width) & ~1, pad_w); 3692 *padding_h = min_t(u32, (native_rect.height - height) & ~1, pad_h); 3693 3694 /* The below minimum padding requirements are for BYT / ISP2400 only */ 3695 if (IS_ISP2401) 3696 return; 3697 3698 sink = atomisp_subdev_get_ffmt(&isp->asd.subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE, 3699 ATOMISP_SUBDEV_PAD_SINK); 3700 if (sink) 3701 fc = atomisp_find_in_fmt_conv(sink->code); 3702 if (!fc) { 3703 dev_warn(isp->dev, "%s: Could not get sensor format\n", __func__); 3704 goto apply_min_padding; 3705 } 3706 3707 /* 3708 * The ISP only supports GRBG for other bayer-orders additional padding 3709 * is used so that the raw sensor data can be cropped to fix the order. 3710 */ 3711 if (fc->bayer_order == IA_CSS_BAYER_ORDER_RGGB || 3712 fc->bayer_order == IA_CSS_BAYER_ORDER_GBRG) 3713 min_pad_w += 2; 3714 3715 if (fc->bayer_order == IA_CSS_BAYER_ORDER_BGGR || 3716 fc->bayer_order == IA_CSS_BAYER_ORDER_GBRG) 3717 min_pad_h += 2; 3718 3719 apply_min_padding: 3720 *padding_w = max_t(u32, *padding_w, min_pad_w); 3721 *padding_h = max_t(u32, *padding_h, min_pad_h); 3722 } 3723 3724 static int atomisp_set_crop(struct atomisp_device *isp, 3725 const struct v4l2_mbus_framefmt *format, 3726 int which) 3727 { 3728 struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr]; 3729 struct v4l2_subdev_state pad_state = { 3730 .pads = &input->pad_cfg, 3731 }; 3732 struct v4l2_subdev_selection sel = { 3733 .which = which, 3734 .target = V4L2_SEL_TGT_CROP, 3735 .r.width = format->width, 3736 .r.height = format->height, 3737 }; 3738 int ret; 3739 3740 if (!input->crop_support) 3741 return 0; 3742 3743 /* Cropping is done before binning, when binning double the crop rect */ 3744 if (input->binning_support && sel.r.width <= (input->native_rect.width / 2) && 3745 sel.r.height <= (input->native_rect.height / 2)) { 3746 sel.r.width *= 2; 3747 sel.r.height *= 2; 3748 } 3749 3750 /* Clamp to avoid top/left calculations overflowing */ 3751 sel.r.width = min(sel.r.width, input->native_rect.width); 3752 sel.r.height = min(sel.r.height, input->native_rect.height); 3753 3754 sel.r.left = ((input->native_rect.width - sel.r.width) / 2) & ~1; 3755 sel.r.top = ((input->native_rect.height - sel.r.height) / 2) & ~1; 3756 3757 ret = v4l2_subdev_call(input->camera, pad, set_selection, &pad_state, &sel); 3758 if (ret) 3759 dev_err(isp->dev, "Error setting crop to %ux%u @%ux%u: %d\n", 3760 sel.r.width, sel.r.height, sel.r.left, sel.r.top, ret); 3761 3762 return ret; 3763 } 3764 3765 /* This function looks up the closest available resolution. */ 3766 int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f, 3767 const struct atomisp_format_bridge **fmt_ret, 3768 const struct atomisp_format_bridge **snr_fmt_ret) 3769 { 3770 const struct atomisp_format_bridge *fmt, *snr_fmt; 3771 struct atomisp_sub_device *asd = &isp->asd; 3772 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr]; 3773 struct v4l2_subdev_state pad_state = { 3774 .pads = &input->pad_cfg, 3775 }; 3776 struct v4l2_subdev_format format = { 3777 .which = V4L2_SUBDEV_FORMAT_TRY, 3778 }; 3779 u32 padding_w, padding_h; 3780 int ret; 3781 3782 if (!input->camera) 3783 return -EINVAL; 3784 3785 fmt = atomisp_get_format_bridge(f->pixelformat); 3786 /* Currently, raw formats are broken!!! */ 3787 if (!fmt || fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) { 3788 f->pixelformat = V4L2_PIX_FMT_YUV420; 3789 3790 fmt = atomisp_get_format_bridge(f->pixelformat); 3791 if (!fmt) 3792 return -EINVAL; 3793 } 3794 3795 /* The preview pipeline does not support width > 1920 */ 3796 if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) 3797 f->width = min_t(u32, f->width, 1920); 3798 3799 /* 3800 * atomisp_set_fmt() will set the sensor resolution to the requested 3801 * resolution + padding. Add padding here and remove it again after 3802 * the set_fmt call, like atomisp_set_fmt_to_snr() does. 3803 */ 3804 atomisp_get_padding(isp, f->width, f->height, &padding_w, &padding_h); 3805 v4l2_fill_mbus_format(&format.format, f, fmt->mbus_code); 3806 format.format.width += padding_w; 3807 format.format.height += padding_h; 3808 3809 dev_dbg(isp->dev, "try_mbus_fmt: asking for %ux%u\n", 3810 format.format.width, format.format.height); 3811 3812 ret = atomisp_set_crop(isp, &format.format, V4L2_SUBDEV_FORMAT_TRY); 3813 if (ret) 3814 return ret; 3815 3816 ret = v4l2_subdev_call(input->camera, pad, set_fmt, &pad_state, &format); 3817 if (ret) 3818 return ret; 3819 3820 dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n", 3821 format.format.width, format.format.height); 3822 3823 snr_fmt = atomisp_get_format_bridge_from_mbus(format.format.code); 3824 if (!snr_fmt) { 3825 dev_err(isp->dev, "unknown sensor format 0x%8.8x\n", 3826 format.format.code); 3827 return -EINVAL; 3828 } 3829 3830 f->width = format.format.width - padding_w; 3831 f->height = format.format.height - padding_h; 3832 3833 /* 3834 * If the format is jpeg or custom RAW, then the width and height will 3835 * not satisfy the normal atomisp requirements and no need to check 3836 * the below conditions. So just assign to what is being returned from 3837 * the sensor driver. 3838 */ 3839 if (f->pixelformat == V4L2_PIX_FMT_JPEG || 3840 f->pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) 3841 goto out_fill_pix_format; 3842 3843 /* app vs isp */ 3844 f->width = rounddown(clamp_t(u32, f->width, ATOM_ISP_MIN_WIDTH, 3845 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH); 3846 f->height = rounddown(clamp_t(u32, f->height, ATOM_ISP_MIN_HEIGHT, 3847 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT); 3848 3849 out_fill_pix_format: 3850 atomisp_fill_pix_format(f, f->width, f->height, fmt); 3851 3852 if (fmt_ret) 3853 *fmt_ret = fmt; 3854 3855 if (snr_fmt_ret) 3856 *snr_fmt_ret = snr_fmt; 3857 3858 return 0; 3859 } 3860 3861 enum mipi_port_id atomisp_port_to_mipi_port(struct atomisp_device *isp, 3862 enum atomisp_camera_port port) 3863 { 3864 switch (port) { 3865 case ATOMISP_CAMERA_PORT_PRIMARY: 3866 return MIPI_PORT0_ID; 3867 case ATOMISP_CAMERA_PORT_SECONDARY: 3868 return MIPI_PORT1_ID; 3869 case ATOMISP_CAMERA_PORT_TERTIARY: 3870 return MIPI_PORT2_ID; 3871 default: 3872 dev_err(isp->dev, "unsupported port: %d\n", port); 3873 return MIPI_PORT0_ID; 3874 } 3875 } 3876 3877 static inline int atomisp_set_sensor_mipi_to_isp( 3878 struct atomisp_sub_device *asd, 3879 enum atomisp_input_stream_id stream_id, 3880 struct camera_mipi_info *mipi_info) 3881 { 3882 struct v4l2_control ctrl; 3883 struct atomisp_device *isp = asd->isp; 3884 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr]; 3885 const struct atomisp_in_fmt_conv *fc; 3886 int mipi_freq = 0; 3887 unsigned int input_format, bayer_order; 3888 enum atomisp_input_format metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED; 3889 u32 mipi_port, metadata_width = 0, metadata_height = 0; 3890 3891 ctrl.id = V4L2_CID_LINK_FREQ; 3892 if (v4l2_g_ctrl(input->camera->ctrl_handler, &ctrl) == 0) 3893 mipi_freq = ctrl.value; 3894 3895 if (asd->stream_env[stream_id].isys_configs == 1) { 3896 input_format = 3897 asd->stream_env[stream_id].isys_info[0].input_format; 3898 atomisp_css_isys_set_format(asd, stream_id, 3899 input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX); 3900 } else if (asd->stream_env[stream_id].isys_configs == 2) { 3901 atomisp_css_isys_two_stream_cfg_update_stream1( 3902 asd, stream_id, 3903 asd->stream_env[stream_id].isys_info[0].input_format, 3904 asd->stream_env[stream_id].isys_info[0].width, 3905 asd->stream_env[stream_id].isys_info[0].height); 3906 3907 atomisp_css_isys_two_stream_cfg_update_stream2( 3908 asd, stream_id, 3909 asd->stream_env[stream_id].isys_info[1].input_format, 3910 asd->stream_env[stream_id].isys_info[1].width, 3911 asd->stream_env[stream_id].isys_info[1].height); 3912 } 3913 3914 /* Compatibility for sensors which provide no media bus code 3915 * in s_mbus_framefmt() nor support pad formats. */ 3916 if (mipi_info && mipi_info->input_format != -1) { 3917 bayer_order = mipi_info->raw_bayer_order; 3918 3919 /* Input stream config is still needs configured */ 3920 /* TODO: Check if this is necessary */ 3921 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt( 3922 mipi_info->input_format); 3923 if (!fc) 3924 return -EINVAL; 3925 input_format = fc->atomisp_in_fmt; 3926 metadata_format = mipi_info->metadata_format; 3927 metadata_width = mipi_info->metadata_width; 3928 metadata_height = mipi_info->metadata_height; 3929 } else { 3930 struct v4l2_mbus_framefmt *sink; 3931 3932 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL, 3933 V4L2_SUBDEV_FORMAT_ACTIVE, 3934 ATOMISP_SUBDEV_PAD_SINK); 3935 fc = atomisp_find_in_fmt_conv(sink->code); 3936 if (!fc) 3937 return -EINVAL; 3938 input_format = fc->atomisp_in_fmt; 3939 bayer_order = fc->bayer_order; 3940 } 3941 3942 atomisp_css_input_set_format(asd, stream_id, input_format); 3943 atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order); 3944 3945 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(metadata_format); 3946 if (!fc) 3947 return -EINVAL; 3948 3949 input_format = fc->atomisp_in_fmt; 3950 mipi_port = atomisp_port_to_mipi_port(isp, input->port); 3951 atomisp_css_input_configure_port(asd, mipi_port, 3952 isp->sensor_lanes[mipi_port], 3953 0xffff4, mipi_freq, 3954 input_format, 3955 metadata_width, metadata_height); 3956 return 0; 3957 } 3958 3959 static int configure_pp_input_nop(struct atomisp_sub_device *asd, 3960 unsigned int width, unsigned int height) 3961 { 3962 return 0; 3963 } 3964 3965 static int configure_output_nop(struct atomisp_sub_device *asd, 3966 unsigned int width, unsigned int height, 3967 unsigned int min_width, 3968 enum ia_css_frame_format sh_fmt) 3969 { 3970 return 0; 3971 } 3972 3973 static int get_frame_info_nop(struct atomisp_sub_device *asd, 3974 struct ia_css_frame_info *finfo) 3975 { 3976 return 0; 3977 } 3978 3979 /* 3980 * Resets CSS parameters that depend on input resolution. 3981 * 3982 * Update params like CSS RAW binning, 2ppc mode and pp_input 3983 * which depend on input size, but are not automatically 3984 * handled in CSS when the input resolution is changed. 3985 */ 3986 static int css_input_resolution_changed(struct atomisp_sub_device *asd, 3987 struct v4l2_mbus_framefmt *ffmt) 3988 { 3989 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf; 3990 unsigned int i; 3991 3992 dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n", 3993 ffmt->width, ffmt->height); 3994 3995 if (IS_ISP2401) 3996 atomisp_css_input_set_two_pixels_per_clock(asd, false); 3997 else 3998 atomisp_css_input_set_two_pixels_per_clock(asd, true); 3999 4000 /* 4001 * If sensor input changed, which means metadata resolution changed 4002 * together. Release all metadata buffers here to let it re-allocated 4003 * next time in reqbufs. 4004 */ 4005 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) { 4006 list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i], 4007 list) { 4008 atomisp_css_free_metadata_buffer(md_buf); 4009 list_del(&md_buf->list); 4010 kfree(md_buf); 4011 } 4012 } 4013 return 0; 4014 4015 /* 4016 * TODO: atomisp_css_preview_configure_pp_input() not 4017 * reset due to CSS bug tracked as PSI BZ 115124 4018 */ 4019 } 4020 4021 static int atomisp_set_fmt_to_isp(struct video_device *vdev, 4022 struct ia_css_frame_info *output_info, 4023 const struct v4l2_pix_format *pix) 4024 { 4025 struct camera_mipi_info *mipi_info; 4026 struct atomisp_device *isp = video_get_drvdata(vdev); 4027 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd; 4028 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr]; 4029 const struct atomisp_format_bridge *format; 4030 struct v4l2_rect *isp_sink_crop; 4031 enum ia_css_pipe_id pipe_id; 4032 int (*configure_output)(struct atomisp_sub_device *asd, 4033 unsigned int width, unsigned int height, 4034 unsigned int min_width, 4035 enum ia_css_frame_format sh_fmt) = 4036 configure_output_nop; 4037 int (*get_frame_info)(struct atomisp_sub_device *asd, 4038 struct ia_css_frame_info *finfo) = 4039 get_frame_info_nop; 4040 int (*configure_pp_input)(struct atomisp_sub_device *asd, 4041 unsigned int width, unsigned int height) = 4042 configure_pp_input_nop; 4043 const struct atomisp_in_fmt_conv *fc = NULL; 4044 int ret, i; 4045 4046 isp_sink_crop = atomisp_subdev_get_rect( 4047 &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE, 4048 ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP); 4049 4050 format = atomisp_get_format_bridge(pix->pixelformat); 4051 if (!format) 4052 return -EINVAL; 4053 4054 if (input->type != TEST_PATTERN) { 4055 mipi_info = atomisp_to_sensor_mipi_info(input->camera); 4056 4057 if (atomisp_set_sensor_mipi_to_isp(asd, ATOMISP_INPUT_STREAM_GENERAL, 4058 mipi_info)) 4059 return -EINVAL; 4060 4061 if (mipi_info) 4062 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(mipi_info->input_format); 4063 4064 if (!fc) 4065 fc = atomisp_find_in_fmt_conv( 4066 atomisp_subdev_get_ffmt(&asd->subdev, 4067 NULL, V4L2_SUBDEV_FORMAT_ACTIVE, 4068 ATOMISP_SUBDEV_PAD_SINK)->code); 4069 if (!fc) 4070 return -EINVAL; 4071 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW && 4072 raw_output_format_match_input(fc->atomisp_in_fmt, 4073 pix->pixelformat)) 4074 return -EINVAL; 4075 } 4076 4077 /* 4078 * Configure viewfinder also when vfpp is disabled: the 4079 * CSS still requires viewfinder configuration. 4080 */ 4081 { 4082 u32 width, height; 4083 4084 if (pix->width < 640 || pix->height < 480) { 4085 width = pix->width; 4086 height = pix->height; 4087 } else { 4088 width = 640; 4089 height = 480; 4090 } 4091 4092 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO || 4093 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) { 4094 atomisp_css_video_configure_viewfinder(asd, width, height, 0, 4095 IA_CSS_FRAME_FORMAT_NV12); 4096 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_STILL_CAPTURE || 4097 asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) { 4098 atomisp_css_capture_configure_viewfinder(asd, width, height, 0, 4099 IA_CSS_FRAME_FORMAT_NV12); 4100 } 4101 } 4102 4103 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR); 4104 4105 for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) 4106 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipe_extra_configs[i].disable_vf_pp = asd->vfpp->val != ATOMISP_VFPP_ENABLE; 4107 4108 /* ISP2401 new input system need to use copy pipe */ 4109 if (asd->copy_mode) { 4110 pipe_id = IA_CSS_PIPE_ID_COPY; 4111 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL, false); 4112 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) { 4113 /* video same in continuouscapture and online modes */ 4114 configure_output = atomisp_css_video_configure_output; 4115 get_frame_info = atomisp_css_video_get_output_frame_info; 4116 pipe_id = IA_CSS_PIPE_ID_VIDEO; 4117 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) { 4118 configure_output = atomisp_css_video_configure_output; 4119 get_frame_info = atomisp_css_video_get_output_frame_info; 4120 pipe_id = IA_CSS_PIPE_ID_VIDEO; 4121 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) { 4122 configure_output = atomisp_css_preview_configure_output; 4123 get_frame_info = atomisp_css_preview_get_output_frame_info; 4124 configure_pp_input = atomisp_css_preview_configure_pp_input; 4125 pipe_id = IA_CSS_PIPE_ID_PREVIEW; 4126 } else { 4127 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) { 4128 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW); 4129 atomisp_css_enable_dz(asd, false); 4130 } else { 4131 atomisp_update_capture_mode(asd); 4132 } 4133 4134 /* in case of ANR, force capture pipe to offline mode */ 4135 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL, 4136 !asd->params.low_light); 4137 4138 configure_output = atomisp_css_capture_configure_output; 4139 get_frame_info = atomisp_css_capture_get_output_frame_info; 4140 configure_pp_input = atomisp_css_capture_configure_pp_input; 4141 pipe_id = IA_CSS_PIPE_ID_CAPTURE; 4142 4143 if (asd->run_mode->val != ATOMISP_RUN_MODE_STILL_CAPTURE) { 4144 dev_err(isp->dev, 4145 "Need to set the running mode first\n"); 4146 asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE; 4147 } 4148 } 4149 4150 if (asd->copy_mode) 4151 ret = atomisp_css_copy_configure_output(asd, ATOMISP_INPUT_STREAM_GENERAL, 4152 pix->width, pix->height, 4153 format->planar ? pix->bytesperline : 4154 pix->bytesperline * 8 / format->depth, 4155 format->sh_fmt); 4156 else 4157 ret = configure_output(asd, pix->width, pix->height, 4158 format->planar ? pix->bytesperline : 4159 pix->bytesperline * 8 / format->depth, 4160 format->sh_fmt); 4161 if (ret) { 4162 dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n", 4163 pix->width, pix->height, format->sh_fmt); 4164 return -EINVAL; 4165 } 4166 4167 ret = configure_pp_input(asd, isp_sink_crop->width, isp_sink_crop->height); 4168 if (ret) { 4169 dev_err(isp->dev, "configure_pp_input %ux%u\n", 4170 isp_sink_crop->width, 4171 isp_sink_crop->height); 4172 return -EINVAL; 4173 } 4174 if (asd->copy_mode) 4175 ret = atomisp_css_copy_get_output_frame_info(asd, 4176 ATOMISP_INPUT_STREAM_GENERAL, 4177 output_info); 4178 else 4179 ret = get_frame_info(asd, output_info); 4180 if (ret) { 4181 dev_err(isp->dev, "__get_frame_info %ux%u (padded to %u) returned %d\n", 4182 pix->width, pix->height, pix->bytesperline, ret); 4183 return ret; 4184 } 4185 4186 atomisp_update_grid_info(asd, pipe_id); 4187 return 0; 4188 } 4189 4190 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd, 4191 unsigned int width, unsigned int height, 4192 unsigned int *dvs_env_w, unsigned int *dvs_env_h) 4193 { 4194 if (asd->params.video_dis_en && 4195 asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) { 4196 /* envelope is 20% of the output resolution */ 4197 /* 4198 * dvs envelope cannot be round up. 4199 * it would cause ISP timeout and color switch issue 4200 */ 4201 *dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH); 4202 *dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT); 4203 } 4204 4205 asd->params.dis_proj_data_valid = false; 4206 asd->params.css_update_params_needed = true; 4207 } 4208 4209 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd, 4210 const struct v4l2_pix_format *f) 4211 { 4212 struct v4l2_mbus_framefmt *sink, *src; 4213 4214 if (!IS_ISP2401) { 4215 /* Only used for the new input system */ 4216 asd->copy_mode = false; 4217 return; 4218 } 4219 4220 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL, 4221 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK); 4222 src = atomisp_subdev_get_ffmt(&asd->subdev, NULL, 4223 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SOURCE); 4224 4225 if (sink->code == src->code && sink->width == f->width && sink->height == f->height) 4226 asd->copy_mode = true; 4227 else 4228 asd->copy_mode = false; 4229 4230 dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode); 4231 } 4232 4233 static int atomisp_set_fmt_to_snr(struct video_device *vdev, const struct v4l2_pix_format *f, 4234 unsigned int dvs_env_w, unsigned int dvs_env_h) 4235 { 4236 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev); 4237 struct atomisp_sub_device *asd = pipe->asd; 4238 struct atomisp_device *isp = asd->isp; 4239 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr]; 4240 const struct atomisp_format_bridge *format; 4241 struct v4l2_subdev_state pad_state = { 4242 .pads = &input->pad_cfg, 4243 }; 4244 struct v4l2_subdev_format vformat = { 4245 .which = V4L2_SUBDEV_FORMAT_TRY, 4246 }; 4247 struct v4l2_mbus_framefmt *ffmt = &vformat.format; 4248 struct v4l2_mbus_framefmt *req_ffmt; 4249 struct atomisp_input_stream_info *stream_info = 4250 (struct atomisp_input_stream_info *)ffmt->reserved; 4251 int ret; 4252 4253 format = atomisp_get_format_bridge(f->pixelformat); 4254 if (!format) 4255 return -EINVAL; 4256 4257 v4l2_fill_mbus_format(ffmt, f, format->mbus_code); 4258 ffmt->height += asd->sink_pad_padding_h + dvs_env_h; 4259 ffmt->width += asd->sink_pad_padding_w + dvs_env_w; 4260 4261 dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n", 4262 ffmt->width, ffmt->height, asd->sink_pad_padding_w, asd->sink_pad_padding_h, 4263 dvs_env_w, dvs_env_h); 4264 4265 __atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL, stream_info); 4266 4267 req_ffmt = ffmt; 4268 4269 /* Disable dvs if resolution can't be supported by sensor */ 4270 if (asd->params.video_dis_en && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) { 4271 ret = atomisp_set_crop(isp, &vformat.format, V4L2_SUBDEV_FORMAT_TRY); 4272 if (ret) 4273 return ret; 4274 4275 vformat.which = V4L2_SUBDEV_FORMAT_TRY; 4276 ret = v4l2_subdev_call(input->camera, pad, set_fmt, &pad_state, &vformat); 4277 if (ret) 4278 return ret; 4279 4280 dev_dbg(isp->dev, "video dis: sensor width: %d, height: %d\n", 4281 ffmt->width, ffmt->height); 4282 4283 if (ffmt->width < req_ffmt->width || 4284 ffmt->height < req_ffmt->height) { 4285 req_ffmt->height -= dvs_env_h; 4286 req_ffmt->width -= dvs_env_w; 4287 ffmt = req_ffmt; 4288 dev_warn(isp->dev, 4289 "can not enable video dis due to sensor limitation."); 4290 asd->params.video_dis_en = false; 4291 } 4292 } 4293 4294 ret = atomisp_set_crop(isp, &vformat.format, V4L2_SUBDEV_FORMAT_ACTIVE); 4295 if (ret) 4296 return ret; 4297 4298 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE; 4299 ret = v4l2_subdev_call(input->camera, pad, set_fmt, NULL, &vformat); 4300 if (ret) 4301 return ret; 4302 4303 __atomisp_update_stream_env(asd, ATOMISP_INPUT_STREAM_GENERAL, stream_info); 4304 4305 dev_dbg(isp->dev, "sensor width: %d, height: %d\n", 4306 ffmt->width, ffmt->height); 4307 4308 if (ffmt->width < ATOM_ISP_STEP_WIDTH || 4309 ffmt->height < ATOM_ISP_STEP_HEIGHT) 4310 return -EINVAL; 4311 4312 if (asd->params.video_dis_en && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO && 4313 (ffmt->width < req_ffmt->width || ffmt->height < req_ffmt->height)) { 4314 dev_warn(isp->dev, 4315 "can not enable video dis due to sensor limitation."); 4316 asd->params.video_dis_en = false; 4317 } 4318 4319 atomisp_subdev_set_ffmt(&asd->subdev, NULL, 4320 V4L2_SUBDEV_FORMAT_ACTIVE, 4321 ATOMISP_SUBDEV_PAD_SINK, ffmt); 4322 4323 return css_input_resolution_changed(asd, ffmt); 4324 } 4325 4326 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f) 4327 { 4328 struct atomisp_device *isp = video_get_drvdata(vdev); 4329 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev); 4330 struct atomisp_sub_device *asd = pipe->asd; 4331 const struct atomisp_format_bridge *format_bridge; 4332 const struct atomisp_format_bridge *snr_format_bridge; 4333 struct ia_css_frame_info output_info; 4334 unsigned int dvs_env_w = 0, dvs_env_h = 0; 4335 struct v4l2_mbus_framefmt isp_source_fmt = {0}; 4336 struct v4l2_rect isp_sink_crop; 4337 int ret; 4338 4339 ret = atomisp_pipe_check(pipe, true); 4340 if (ret) 4341 return ret; 4342 4343 dev_dbg(isp->dev, 4344 "setting resolution %ux%u bytesperline %u\n", 4345 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.bytesperline); 4346 4347 /* Ensure that the resolution is equal or below the maximum supported */ 4348 ret = atomisp_try_fmt(isp, &f->fmt.pix, &format_bridge, &snr_format_bridge); 4349 if (ret) 4350 return ret; 4351 4352 pipe->sh_fmt = format_bridge->sh_fmt; 4353 pipe->pix.pixelformat = format_bridge->pixelformat; 4354 4355 atomisp_subdev_get_ffmt(&asd->subdev, NULL, 4356 V4L2_SUBDEV_FORMAT_ACTIVE, 4357 ATOMISP_SUBDEV_PAD_SINK)->code = 4358 snr_format_bridge->mbus_code; 4359 4360 isp_source_fmt.code = format_bridge->mbus_code; 4361 atomisp_subdev_set_ffmt(&asd->subdev, NULL, 4362 V4L2_SUBDEV_FORMAT_ACTIVE, 4363 ATOMISP_SUBDEV_PAD_SOURCE, &isp_source_fmt); 4364 4365 if (atomisp_subdev_format_conversion(asd)) { 4366 atomisp_get_padding(isp, f->fmt.pix.width, f->fmt.pix.height, 4367 &asd->sink_pad_padding_w, &asd->sink_pad_padding_h); 4368 } else { 4369 asd->sink_pad_padding_w = 0; 4370 asd->sink_pad_padding_h = 0; 4371 } 4372 4373 atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height, 4374 &dvs_env_w, &dvs_env_h); 4375 4376 ret = atomisp_set_fmt_to_snr(vdev, &f->fmt.pix, dvs_env_w, dvs_env_h); 4377 if (ret) { 4378 dev_warn(isp->dev, 4379 "Set format to sensor failed with %d\n", ret); 4380 return -EINVAL; 4381 } 4382 4383 atomisp_csi_lane_config(isp); 4384 4385 atomisp_check_copy_mode(asd, &f->fmt.pix); 4386 4387 isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL, 4388 V4L2_SUBDEV_FORMAT_ACTIVE, 4389 ATOMISP_SUBDEV_PAD_SINK, 4390 V4L2_SEL_TGT_CROP); 4391 4392 /* Try to enable YUV downscaling if ISP input is 10 % (either 4393 * width or height) bigger than the desired result. */ 4394 if (!IS_MOFD || 4395 isp_sink_crop.width * 9 / 10 < f->fmt.pix.width || 4396 isp_sink_crop.height * 9 / 10 < f->fmt.pix.height || 4397 (atomisp_subdev_format_conversion(asd) && 4398 (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO || 4399 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) { 4400 isp_sink_crop.width = f->fmt.pix.width; 4401 isp_sink_crop.height = f->fmt.pix.height; 4402 4403 atomisp_subdev_set_selection(&asd->subdev, NULL, 4404 V4L2_SUBDEV_FORMAT_ACTIVE, 4405 ATOMISP_SUBDEV_PAD_SOURCE, V4L2_SEL_TGT_COMPOSE, 4406 0, &isp_sink_crop); 4407 } else { 4408 struct v4l2_rect main_compose = {0}; 4409 4410 main_compose.width = isp_sink_crop.width; 4411 main_compose.height = 4412 DIV_ROUND_UP(main_compose.width * f->fmt.pix.height, 4413 f->fmt.pix.width); 4414 if (main_compose.height > isp_sink_crop.height) { 4415 main_compose.height = isp_sink_crop.height; 4416 main_compose.width = 4417 DIV_ROUND_UP(main_compose.height * 4418 f->fmt.pix.width, 4419 f->fmt.pix.height); 4420 } 4421 4422 atomisp_subdev_set_selection(&asd->subdev, NULL, 4423 V4L2_SUBDEV_FORMAT_ACTIVE, 4424 ATOMISP_SUBDEV_PAD_SOURCE, 4425 V4L2_SEL_TGT_COMPOSE, 0, 4426 &main_compose); 4427 } 4428 4429 ret = atomisp_set_fmt_to_isp(vdev, &output_info, &f->fmt.pix); 4430 if (ret) { 4431 dev_warn(isp->dev, "Can't set format on ISP. Error %d\n", ret); 4432 return -EINVAL; 4433 } 4434 4435 atomisp_fill_pix_format(&pipe->pix, f->fmt.pix.width, f->fmt.pix.height, format_bridge); 4436 4437 f->fmt.pix = pipe->pix; 4438 f->fmt.pix.priv = PAGE_ALIGN(pipe->pix.width * 4439 pipe->pix.height * 2); 4440 4441 dev_dbg(isp->dev, "%s: %dx%d, image size: %d, %d bytes per line\n", 4442 __func__, 4443 f->fmt.pix.width, f->fmt.pix.height, 4444 f->fmt.pix.sizeimage, f->fmt.pix.bytesperline); 4445 4446 return 0; 4447 } 4448 4449 int atomisp_set_shading_table(struct atomisp_sub_device *asd, 4450 struct atomisp_shading_table *user_shading_table) 4451 { 4452 struct ia_css_shading_table *shading_table; 4453 struct ia_css_shading_table *free_table; 4454 unsigned int len_table; 4455 int i; 4456 int ret = 0; 4457 4458 if (!user_shading_table) 4459 return -EINVAL; 4460 4461 if (!user_shading_table->enable) { 4462 asd->params.config.shading_table = NULL; 4463 asd->params.sc_en = false; 4464 return 0; 4465 } 4466 4467 /* If enabling, all tables must be set */ 4468 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) { 4469 if (!user_shading_table->data[i]) 4470 return -EINVAL; 4471 } 4472 4473 /* Shading table size per color */ 4474 if (user_shading_table->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR || 4475 user_shading_table->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) 4476 return -EINVAL; 4477 4478 shading_table = atomisp_css_shading_table_alloc( 4479 user_shading_table->width, user_shading_table->height); 4480 if (!shading_table) 4481 return -ENOMEM; 4482 4483 len_table = user_shading_table->width * user_shading_table->height * 4484 ATOMISP_SC_TYPE_SIZE; 4485 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) { 4486 ret = copy_from_user(shading_table->data[i], 4487 (void __user *)user_shading_table->data[i], 4488 len_table); 4489 if (ret) { 4490 free_table = shading_table; 4491 ret = -EFAULT; 4492 goto out; 4493 } 4494 } 4495 shading_table->sensor_width = user_shading_table->sensor_width; 4496 shading_table->sensor_height = user_shading_table->sensor_height; 4497 shading_table->fraction_bits = user_shading_table->fraction_bits; 4498 4499 free_table = asd->params.css_param.shading_table; 4500 asd->params.css_param.shading_table = shading_table; 4501 asd->params.config.shading_table = shading_table; 4502 asd->params.sc_en = true; 4503 4504 out: 4505 if (free_table) 4506 atomisp_css_shading_table_free(free_table); 4507 4508 return ret; 4509 } 4510 4511 int atomisp_flash_enable(struct atomisp_sub_device *asd, int num_frames) 4512 { 4513 struct atomisp_device *isp = asd->isp; 4514 4515 if (num_frames < 0) { 4516 dev_dbg(isp->dev, "%s ERROR: num_frames: %d\n", __func__, 4517 num_frames); 4518 return -EINVAL; 4519 } 4520 /* a requested flash is still in progress. */ 4521 if (num_frames && asd->params.flash_state != ATOMISP_FLASH_IDLE) { 4522 dev_dbg(isp->dev, "%s flash busy: %d frames left: %d\n", 4523 __func__, asd->params.flash_state, 4524 asd->params.num_flash_frames); 4525 return -EBUSY; 4526 } 4527 4528 asd->params.num_flash_frames = num_frames; 4529 asd->params.flash_state = ATOMISP_FLASH_REQUESTED; 4530 return 0; 4531 } 4532 4533 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id) 4534 { 4535 struct atomisp_device *isp = asd->isp; 4536 4537 if (!asd->enable_raw_buffer_lock->val) { 4538 dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__); 4539 return -EINVAL; 4540 } 4541 if (!asd->streaming) { 4542 dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n", 4543 __func__, exp_id, asd->streaming); 4544 return -EINVAL; 4545 } 4546 if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) { 4547 dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id); 4548 return -EINVAL; 4549 } 4550 return 0; 4551 } 4552 4553 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd) 4554 { 4555 unsigned long flags; 4556 4557 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags); 4558 memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap)); 4559 asd->raw_buffer_locked_count = 0; 4560 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags); 4561 } 4562 4563 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id) 4564 { 4565 int *bitmap, bit; 4566 unsigned long flags; 4567 int ret; 4568 4569 if (__checking_exp_id(asd, exp_id)) 4570 return -EINVAL; 4571 4572 bitmap = asd->raw_buffer_bitmap + exp_id / 32; 4573 bit = exp_id % 32; 4574 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags); 4575 ret = ((*bitmap) & (1 << bit)); 4576 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags); 4577 return !ret; 4578 } 4579 4580 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id) 4581 { 4582 int *bitmap, bit; 4583 unsigned long flags; 4584 4585 if (__is_raw_buffer_locked(asd, exp_id)) 4586 return -EINVAL; 4587 4588 bitmap = asd->raw_buffer_bitmap + exp_id / 32; 4589 bit = exp_id % 32; 4590 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags); 4591 (*bitmap) &= ~(1 << bit); 4592 asd->raw_buffer_locked_count--; 4593 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags); 4594 4595 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n", 4596 __func__, exp_id, asd->raw_buffer_locked_count); 4597 return 0; 4598 } 4599 4600 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id) 4601 { 4602 struct atomisp_device *isp = asd->isp; 4603 int value = *exp_id; 4604 int ret; 4605 4606 lockdep_assert_held(&isp->mutex); 4607 4608 ret = __is_raw_buffer_locked(asd, value); 4609 if (ret) { 4610 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret); 4611 return -EINVAL; 4612 } 4613 4614 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value); 4615 ret = atomisp_css_exp_id_capture(asd, value); 4616 if (ret) { 4617 dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value); 4618 return -EIO; 4619 } 4620 return 0; 4621 } 4622 4623 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id) 4624 { 4625 struct atomisp_device *isp = asd->isp; 4626 int value = *exp_id; 4627 int ret; 4628 4629 lockdep_assert_held(&isp->mutex); 4630 4631 ret = __clear_raw_buffer_bitmap(asd, value); 4632 if (ret) { 4633 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret); 4634 return -EINVAL; 4635 } 4636 4637 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value); 4638 ret = atomisp_css_exp_id_unlock(asd, value); 4639 if (ret) 4640 dev_err(isp->dev, "%s exp_id %d failed, err %d.\n", 4641 __func__, value, ret); 4642 4643 return ret; 4644 } 4645 4646 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd, 4647 unsigned int *enable) 4648 { 4649 bool value; 4650 4651 if (!enable) 4652 return -EINVAL; 4653 4654 value = *enable > 0; 4655 4656 atomisp_en_dz_capt_pipe(asd, value); 4657 4658 return 0; 4659 } 4660 4661 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event) 4662 { 4663 if (!event || !asd->streaming) 4664 return -EINVAL; 4665 4666 lockdep_assert_held(&asd->isp->mutex); 4667 4668 dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n", 4669 __func__, *event); 4670 4671 switch (*event) { 4672 case V4L2_EVENT_FRAME_SYNC: 4673 atomisp_sof_event(asd); 4674 break; 4675 case V4L2_EVENT_FRAME_END: 4676 atomisp_eof_event(asd, 0); 4677 break; 4678 case V4L2_EVENT_ATOMISP_3A_STATS_READY: 4679 atomisp_3a_stats_ready_event(asd, 0); 4680 break; 4681 case V4L2_EVENT_ATOMISP_METADATA_READY: 4682 atomisp_metadata_ready_event(asd, 0); 4683 break; 4684 default: 4685 return -EINVAL; 4686 } 4687 4688 return 0; 4689 } 4690