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 /* ISP2401 */ 1252 int atomisp_set_sensor_runmode(struct atomisp_sub_device *asd, 1253 struct atomisp_s_runmode *runmode) 1254 { 1255 struct atomisp_device *isp = asd->isp; 1256 struct v4l2_ctrl *c; 1257 int ret = 0; 1258 1259 if (!(runmode && (runmode->mode & RUNMODE_MASK))) 1260 return -EINVAL; 1261 1262 mutex_lock(asd->ctrl_handler.lock); 1263 c = v4l2_ctrl_find(isp->inputs[asd->input_curr].camera->ctrl_handler, 1264 V4L2_CID_RUN_MODE); 1265 1266 if (c) 1267 ret = v4l2_ctrl_s_ctrl(c, runmode->mode); 1268 1269 mutex_unlock(asd->ctrl_handler.lock); 1270 return ret; 1271 } 1272 1273 /* 1274 * Function to enable/disable lens geometry distortion correction (GDC) and 1275 * chromatic aberration correction (CAC) 1276 */ 1277 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag, 1278 __s32 *value) 1279 { 1280 if (flag == 0) { 1281 *value = asd->params.gdc_cac_en; 1282 return 0; 1283 } 1284 1285 asd->params.gdc_cac_en = !!*value; 1286 if (asd->params.gdc_cac_en) { 1287 asd->params.config.morph_table = asd->params.css_param.morph_table; 1288 } else { 1289 asd->params.config.morph_table = NULL; 1290 } 1291 asd->params.css_update_params_needed = true; 1292 atomisp_update_capture_mode(asd); 1293 return 0; 1294 } 1295 1296 /* 1297 * Function to enable/disable low light mode including ANR 1298 */ 1299 int atomisp_low_light(struct atomisp_sub_device *asd, int flag, 1300 __s32 *value) 1301 { 1302 if (flag == 0) { 1303 *value = asd->params.low_light; 1304 return 0; 1305 } 1306 1307 asd->params.low_light = (*value != 0); 1308 atomisp_update_capture_mode(asd); 1309 return 0; 1310 } 1311 1312 /* 1313 * Function to enable/disable extra noise reduction (XNR) in low light 1314 * condition 1315 */ 1316 int atomisp_xnr(struct atomisp_sub_device *asd, int flag, 1317 int *xnr_enable) 1318 { 1319 if (flag == 0) { 1320 *xnr_enable = asd->params.xnr_en; 1321 return 0; 1322 } 1323 1324 atomisp_css_capture_enable_xnr(asd, !!*xnr_enable); 1325 1326 return 0; 1327 } 1328 1329 /* 1330 * Function to configure bayer noise reduction 1331 */ 1332 int atomisp_nr(struct atomisp_sub_device *asd, int flag, 1333 struct atomisp_nr_config *arg) 1334 { 1335 if (flag == 0) { 1336 /* Get nr config from current setup */ 1337 if (atomisp_css_get_nr_config(asd, arg)) 1338 return -EINVAL; 1339 } else { 1340 /* Set nr config to isp parameters */ 1341 memcpy(&asd->params.css_param.nr_config, arg, 1342 sizeof(struct ia_css_nr_config)); 1343 asd->params.config.nr_config = &asd->params.css_param.nr_config; 1344 asd->params.css_update_params_needed = true; 1345 } 1346 return 0; 1347 } 1348 1349 /* 1350 * Function to configure temporal noise reduction (TNR) 1351 */ 1352 int atomisp_tnr(struct atomisp_sub_device *asd, int flag, 1353 struct atomisp_tnr_config *config) 1354 { 1355 /* Get tnr config from current setup */ 1356 if (flag == 0) { 1357 /* Get tnr config from current setup */ 1358 if (atomisp_css_get_tnr_config(asd, config)) 1359 return -EINVAL; 1360 } else { 1361 /* Set tnr config to isp parameters */ 1362 memcpy(&asd->params.css_param.tnr_config, config, 1363 sizeof(struct ia_css_tnr_config)); 1364 asd->params.config.tnr_config = &asd->params.css_param.tnr_config; 1365 asd->params.css_update_params_needed = true; 1366 } 1367 1368 return 0; 1369 } 1370 1371 /* 1372 * Function to configure black level compensation 1373 */ 1374 int atomisp_black_level(struct atomisp_sub_device *asd, int flag, 1375 struct atomisp_ob_config *config) 1376 { 1377 if (flag == 0) { 1378 /* Get ob config from current setup */ 1379 if (atomisp_css_get_ob_config(asd, config)) 1380 return -EINVAL; 1381 } else { 1382 /* Set ob config to isp parameters */ 1383 memcpy(&asd->params.css_param.ob_config, config, 1384 sizeof(struct ia_css_ob_config)); 1385 asd->params.config.ob_config = &asd->params.css_param.ob_config; 1386 asd->params.css_update_params_needed = true; 1387 } 1388 1389 return 0; 1390 } 1391 1392 /* 1393 * Function to configure edge enhancement 1394 */ 1395 int atomisp_ee(struct atomisp_sub_device *asd, int flag, 1396 struct atomisp_ee_config *config) 1397 { 1398 if (flag == 0) { 1399 /* Get ee config from current setup */ 1400 if (atomisp_css_get_ee_config(asd, config)) 1401 return -EINVAL; 1402 } else { 1403 /* Set ee config to isp parameters */ 1404 memcpy(&asd->params.css_param.ee_config, config, 1405 sizeof(asd->params.css_param.ee_config)); 1406 asd->params.config.ee_config = &asd->params.css_param.ee_config; 1407 asd->params.css_update_params_needed = true; 1408 } 1409 1410 return 0; 1411 } 1412 1413 /* 1414 * Function to update Gamma table for gamma, brightness and contrast config 1415 */ 1416 int atomisp_gamma(struct atomisp_sub_device *asd, int flag, 1417 struct atomisp_gamma_table *config) 1418 { 1419 if (flag == 0) { 1420 /* Get gamma table from current setup */ 1421 if (atomisp_css_get_gamma_table(asd, config)) 1422 return -EINVAL; 1423 } else { 1424 /* Set gamma table to isp parameters */ 1425 memcpy(&asd->params.css_param.gamma_table, config, 1426 sizeof(asd->params.css_param.gamma_table)); 1427 asd->params.config.gamma_table = &asd->params.css_param.gamma_table; 1428 } 1429 1430 return 0; 1431 } 1432 1433 /* 1434 * Function to update Ctc table for Chroma Enhancement 1435 */ 1436 int atomisp_ctc(struct atomisp_sub_device *asd, int flag, 1437 struct atomisp_ctc_table *config) 1438 { 1439 if (flag == 0) { 1440 /* Get ctc table from current setup */ 1441 if (atomisp_css_get_ctc_table(asd, config)) 1442 return -EINVAL; 1443 } else { 1444 /* Set ctc table to isp parameters */ 1445 memcpy(&asd->params.css_param.ctc_table, config, 1446 sizeof(asd->params.css_param.ctc_table)); 1447 atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table); 1448 } 1449 1450 return 0; 1451 } 1452 1453 /* 1454 * Function to update gamma correction parameters 1455 */ 1456 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag, 1457 struct atomisp_gc_config *config) 1458 { 1459 if (flag == 0) { 1460 /* Get gamma correction params from current setup */ 1461 if (atomisp_css_get_gc_config(asd, config)) 1462 return -EINVAL; 1463 } else { 1464 /* Set gamma correction params to isp parameters */ 1465 memcpy(&asd->params.css_param.gc_config, config, 1466 sizeof(asd->params.css_param.gc_config)); 1467 asd->params.config.gc_config = &asd->params.css_param.gc_config; 1468 asd->params.css_update_params_needed = true; 1469 } 1470 1471 return 0; 1472 } 1473 1474 /* 1475 * Function to update narrow gamma flag 1476 */ 1477 int atomisp_formats(struct atomisp_sub_device *asd, int flag, 1478 struct atomisp_formats_config *config) 1479 { 1480 if (flag == 0) { 1481 /* Get narrow gamma flag from current setup */ 1482 if (atomisp_css_get_formats_config(asd, config)) 1483 return -EINVAL; 1484 } else { 1485 /* Set narrow gamma flag to isp parameters */ 1486 memcpy(&asd->params.css_param.formats_config, config, 1487 sizeof(asd->params.css_param.formats_config)); 1488 asd->params.config.formats_config = &asd->params.css_param.formats_config; 1489 } 1490 1491 return 0; 1492 } 1493 1494 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd) 1495 { 1496 atomisp_free_css_parameters(&asd->params.css_param); 1497 } 1498 1499 static void atomisp_update_grid_info(struct atomisp_sub_device *asd, 1500 enum ia_css_pipe_id pipe_id) 1501 { 1502 struct atomisp_device *isp = asd->isp; 1503 int err; 1504 1505 if (atomisp_css_get_grid_info(asd, pipe_id)) 1506 return; 1507 1508 /* We must free all buffers because they no longer match 1509 the grid size. */ 1510 atomisp_css_free_stat_buffers(asd); 1511 1512 err = atomisp_alloc_css_stat_bufs(asd, ATOMISP_INPUT_STREAM_GENERAL); 1513 if (err) { 1514 dev_err(isp->dev, "stat_buf allocate error\n"); 1515 goto err; 1516 } 1517 1518 if (atomisp_alloc_3a_output_buf(asd)) { 1519 /* Failure for 3A buffers does not influence DIS buffers */ 1520 if (asd->params.s3a_output_bytes != 0) { 1521 /* For SOC sensor happens s3a_output_bytes == 0, 1522 * using if condition to exclude false error log */ 1523 dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n"); 1524 } 1525 goto err; 1526 } 1527 1528 if (atomisp_alloc_dis_coef_buf(asd)) { 1529 dev_err(isp->dev, 1530 "Failed to allocate memory for DIS statistics\n"); 1531 goto err; 1532 } 1533 1534 if (atomisp_alloc_metadata_output_buf(asd)) { 1535 dev_err(isp->dev, "Failed to allocate memory for metadata\n"); 1536 goto err; 1537 } 1538 1539 return; 1540 1541 err: 1542 atomisp_css_free_stat_buffers(asd); 1543 return; 1544 } 1545 1546 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd, 1547 struct atomisp_grid_info *info) 1548 { 1549 memcpy(info, &asd->params.curr_grid_info.s3a_grid, 1550 sizeof(struct ia_css_3a_grid_info)); 1551 } 1552 1553 int atomisp_compare_grid(struct atomisp_sub_device *asd, 1554 struct atomisp_grid_info *atomgrid) 1555 { 1556 struct atomisp_grid_info tmp = {0}; 1557 1558 atomisp_curr_user_grid_info(asd, &tmp); 1559 return memcmp(atomgrid, &tmp, sizeof(tmp)); 1560 } 1561 1562 /* 1563 * Function to update Gdc table for gdc 1564 */ 1565 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag, 1566 struct atomisp_morph_table *config) 1567 { 1568 int ret; 1569 int i; 1570 struct atomisp_device *isp = asd->isp; 1571 1572 if (flag == 0) { 1573 /* Get gdc table from current setup */ 1574 struct ia_css_morph_table tab = {0}; 1575 1576 atomisp_css_get_morph_table(asd, &tab); 1577 1578 config->width = tab.width; 1579 config->height = tab.height; 1580 1581 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) { 1582 ret = copy_to_user(config->coordinates_x[i], 1583 tab.coordinates_x[i], tab.height * 1584 tab.width * sizeof(*tab.coordinates_x[i])); 1585 if (ret) { 1586 dev_err(isp->dev, 1587 "Failed to copy to User for x\n"); 1588 return -EFAULT; 1589 } 1590 ret = copy_to_user(config->coordinates_y[i], 1591 tab.coordinates_y[i], tab.height * 1592 tab.width * sizeof(*tab.coordinates_y[i])); 1593 if (ret) { 1594 dev_err(isp->dev, 1595 "Failed to copy to User for y\n"); 1596 return -EFAULT; 1597 } 1598 } 1599 } else { 1600 struct ia_css_morph_table *tab = 1601 asd->params.css_param.morph_table; 1602 1603 /* free first if we have one */ 1604 if (tab) { 1605 atomisp_css_morph_table_free(tab); 1606 asd->params.css_param.morph_table = NULL; 1607 } 1608 1609 /* allocate new one */ 1610 tab = atomisp_css_morph_table_allocate(config->width, 1611 config->height); 1612 1613 if (!tab) { 1614 dev_err(isp->dev, "out of memory\n"); 1615 return -EINVAL; 1616 } 1617 1618 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) { 1619 ret = copy_from_user(tab->coordinates_x[i], 1620 config->coordinates_x[i], 1621 config->height * config->width * 1622 sizeof(*config->coordinates_x[i])); 1623 if (ret) { 1624 dev_err(isp->dev, 1625 "Failed to copy from User for x, ret %d\n", 1626 ret); 1627 atomisp_css_morph_table_free(tab); 1628 return -EFAULT; 1629 } 1630 ret = copy_from_user(tab->coordinates_y[i], 1631 config->coordinates_y[i], 1632 config->height * config->width * 1633 sizeof(*config->coordinates_y[i])); 1634 if (ret) { 1635 dev_err(isp->dev, 1636 "Failed to copy from User for y, ret is %d\n", 1637 ret); 1638 atomisp_css_morph_table_free(tab); 1639 return -EFAULT; 1640 } 1641 } 1642 asd->params.css_param.morph_table = tab; 1643 if (asd->params.gdc_cac_en) 1644 asd->params.config.morph_table = tab; 1645 } 1646 1647 return 0; 1648 } 1649 1650 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag, 1651 struct atomisp_macc_config *config) 1652 { 1653 struct ia_css_macc_table *macc_table; 1654 1655 switch (config->color_effect) { 1656 case V4L2_COLORFX_NONE: 1657 macc_table = &asd->params.css_param.macc_table; 1658 break; 1659 case V4L2_COLORFX_SKY_BLUE: 1660 macc_table = &blue_macc_table; 1661 break; 1662 case V4L2_COLORFX_GRASS_GREEN: 1663 macc_table = &green_macc_table; 1664 break; 1665 case V4L2_COLORFX_SKIN_WHITEN_LOW: 1666 macc_table = &skin_low_macc_table; 1667 break; 1668 case V4L2_COLORFX_SKIN_WHITEN: 1669 macc_table = &skin_medium_macc_table; 1670 break; 1671 case V4L2_COLORFX_SKIN_WHITEN_HIGH: 1672 macc_table = &skin_high_macc_table; 1673 break; 1674 default: 1675 return -EINVAL; 1676 } 1677 1678 if (flag == 0) { 1679 /* Get macc table from current setup */ 1680 memcpy(&config->table, macc_table, 1681 sizeof(struct ia_css_macc_table)); 1682 } else { 1683 memcpy(macc_table, &config->table, 1684 sizeof(struct ia_css_macc_table)); 1685 if (config->color_effect == asd->params.color_effect) 1686 asd->params.config.macc_table = macc_table; 1687 } 1688 1689 return 0; 1690 } 1691 1692 int atomisp_set_dis_vector(struct atomisp_sub_device *asd, 1693 struct atomisp_dis_vector *vector) 1694 { 1695 atomisp_css_video_set_dis_vector(asd, vector); 1696 1697 asd->params.dis_proj_data_valid = false; 1698 asd->params.css_update_params_needed = true; 1699 return 0; 1700 } 1701 1702 /* 1703 * Function to set/get image stablization statistics 1704 */ 1705 int atomisp_get_dis_stat(struct atomisp_sub_device *asd, 1706 struct atomisp_dis_statistics *stats) 1707 { 1708 return atomisp_css_get_dis_stat(asd, stats); 1709 } 1710 1711 /* 1712 * Function set camrea_prefiles.xml current sensor pixel array size 1713 */ 1714 int atomisp_set_array_res(struct atomisp_sub_device *asd, 1715 struct atomisp_resolution *config) 1716 { 1717 dev_dbg(asd->isp->dev, ">%s start\n", __func__); 1718 if (!config) { 1719 dev_err(asd->isp->dev, "Set sensor array size is not valid\n"); 1720 return -EINVAL; 1721 } 1722 1723 asd->sensor_array_res.width = config->width; 1724 asd->sensor_array_res.height = config->height; 1725 return 0; 1726 } 1727 1728 /* 1729 * Function to get DVS2 BQ resolution settings 1730 */ 1731 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd, 1732 struct atomisp_dvs2_bq_resolutions *bq_res) 1733 { 1734 struct ia_css_pipe_config *pipe_cfg = NULL; 1735 1736 struct ia_css_stream *stream = 1737 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream; 1738 if (!stream) { 1739 dev_warn(asd->isp->dev, "stream is not created"); 1740 return -EAGAIN; 1741 } 1742 1743 pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL] 1744 .pipe_configs[IA_CSS_PIPE_ID_VIDEO]; 1745 1746 if (!bq_res) 1747 return -EINVAL; 1748 1749 /* the GDC output resolution */ 1750 bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2; 1751 bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2; 1752 1753 bq_res->envelope_bq.width_bq = 0; 1754 bq_res->envelope_bq.height_bq = 0; 1755 /* the GDC input resolution */ 1756 bq_res->source_bq.width_bq = bq_res->output_bq.width_bq + 1757 pipe_cfg->dvs_envelope.width / 2; 1758 bq_res->source_bq.height_bq = bq_res->output_bq.height_bq + 1759 pipe_cfg->dvs_envelope.height / 2; 1760 /* 1761 * Bad pixels caused by spatial filter processing 1762 * ISP filter resolution should be given by CSS/FW, but for now 1763 * there is not such API to query, and it is fixed value, so 1764 * hardcoded here. 1765 */ 1766 bq_res->ispfilter_bq.width_bq = 12 / 2; 1767 bq_res->ispfilter_bq.height_bq = 12 / 2; 1768 /* spatial filter shift, always 4 pixels */ 1769 bq_res->gdc_shift_bq.width_bq = 4 / 2; 1770 bq_res->gdc_shift_bq.height_bq = 4 / 2; 1771 1772 if (asd->params.video_dis_en) { 1773 bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width / 2 - 1774 bq_res->ispfilter_bq.width_bq; 1775 bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height / 2 - 1776 bq_res->ispfilter_bq.height_bq; 1777 } 1778 1779 dev_dbg(asd->isp->dev, 1780 "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", 1781 bq_res->source_bq.width_bq, bq_res->source_bq.height_bq, 1782 bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq, 1783 bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq, 1784 bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq, 1785 bq_res->output_bq.width_bq, bq_res->output_bq.height_bq); 1786 1787 return 0; 1788 } 1789 1790 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd, 1791 struct atomisp_dis_coefficients *coefs) 1792 { 1793 return atomisp_css_set_dis_coefs(asd, coefs); 1794 } 1795 1796 /* 1797 * Function to set/get 3A stat from isp 1798 */ 1799 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag, 1800 struct atomisp_3a_statistics *config) 1801 { 1802 struct atomisp_device *isp = asd->isp; 1803 struct atomisp_s3a_buf *s3a_buf; 1804 unsigned long ret; 1805 1806 if (flag != 0) 1807 return -EINVAL; 1808 1809 /* sanity check to avoid writing into unallocated memory. */ 1810 if (asd->params.s3a_output_bytes == 0) 1811 return -EINVAL; 1812 1813 if (atomisp_compare_grid(asd, &config->grid_info) != 0) { 1814 /* If the grid info in the argument differs from the current 1815 grid info, we tell the caller to reset the grid size and 1816 try again. */ 1817 return -EAGAIN; 1818 } 1819 1820 if (list_empty(&asd->s3a_stats_ready)) { 1821 dev_err(isp->dev, "3a statistics is not valid.\n"); 1822 return -EAGAIN; 1823 } 1824 1825 s3a_buf = list_entry(asd->s3a_stats_ready.next, 1826 struct atomisp_s3a_buf, list); 1827 if (s3a_buf->s3a_map) 1828 ia_css_translate_3a_statistics( 1829 asd->params.s3a_user_stat, s3a_buf->s3a_map); 1830 else 1831 ia_css_get_3a_statistics(asd->params.s3a_user_stat, 1832 s3a_buf->s3a_data); 1833 1834 config->exp_id = s3a_buf->s3a_data->exp_id; 1835 config->isp_config_id = s3a_buf->s3a_data->isp_config_id; 1836 1837 ret = copy_to_user(config->data, asd->params.s3a_user_stat->data, 1838 asd->params.s3a_output_bytes); 1839 if (ret) { 1840 dev_err(isp->dev, "copy to user failed: copied %lu bytes\n", 1841 ret); 1842 return -EFAULT; 1843 } 1844 1845 /* Move to free buffer list */ 1846 list_del_init(&s3a_buf->list); 1847 list_add_tail(&s3a_buf->list, &asd->s3a_stats); 1848 dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n", 1849 __func__, 1850 config->exp_id, config->isp_config_id); 1851 return 0; 1852 } 1853 1854 /* 1855 * Function to calculate real zoom region for every pipe 1856 */ 1857 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd, 1858 struct ia_css_dz_config *dz_config, 1859 enum ia_css_pipe_id css_pipe_id) 1860 1861 { 1862 struct atomisp_stream_env *stream_env = 1863 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]; 1864 struct atomisp_resolution eff_res, out_res; 1865 int w_offset, h_offset; 1866 1867 memset(&eff_res, 0, sizeof(eff_res)); 1868 memset(&out_res, 0, sizeof(out_res)); 1869 1870 if (dz_config->dx || dz_config->dy) 1871 return 0; 1872 1873 if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW 1874 && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) { 1875 dev_err(asd->isp->dev, "%s the set pipe no support crop region" 1876 , __func__); 1877 return -EINVAL; 1878 } 1879 1880 eff_res.width = 1881 stream_env->stream_config.input_config.effective_res.width; 1882 eff_res.height = 1883 stream_env->stream_config.input_config.effective_res.height; 1884 if (eff_res.width == 0 || eff_res.height == 0) { 1885 dev_err(asd->isp->dev, "%s err effective resolution" 1886 , __func__); 1887 return -EINVAL; 1888 } 1889 1890 if (dz_config->zoom_region.resolution.width 1891 == asd->sensor_array_res.width 1892 || dz_config->zoom_region.resolution.height 1893 == asd->sensor_array_res.height) { 1894 /*no need crop region*/ 1895 dz_config->zoom_region.origin.x = 0; 1896 dz_config->zoom_region.origin.y = 0; 1897 dz_config->zoom_region.resolution.width = eff_res.width; 1898 dz_config->zoom_region.resolution.height = eff_res.height; 1899 return 0; 1900 } 1901 1902 /* FIXME: 1903 * This is not the correct implementation with Google's definition, due 1904 * to firmware limitation. 1905 * map real crop region base on above calculating base max crop region. 1906 */ 1907 1908 if (!IS_ISP2401) { 1909 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x 1910 * eff_res.width 1911 / asd->sensor_array_res.width; 1912 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y 1913 * eff_res.height 1914 / asd->sensor_array_res.height; 1915 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width 1916 * eff_res.width 1917 / asd->sensor_array_res.width; 1918 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height 1919 * eff_res.height 1920 / asd->sensor_array_res.height; 1921 /* 1922 * Set same ratio of crop region resolution and current pipe output 1923 * resolution 1924 */ 1925 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width; 1926 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height; 1927 if (out_res.width == 0 || out_res.height == 0) { 1928 dev_err(asd->isp->dev, "%s err current pipe output resolution" 1929 , __func__); 1930 return -EINVAL; 1931 } 1932 } else { 1933 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width; 1934 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height; 1935 if (out_res.width == 0 || out_res.height == 0) { 1936 dev_err(asd->isp->dev, "%s err current pipe output resolution" 1937 , __func__); 1938 return -EINVAL; 1939 } 1940 1941 if (asd->sensor_array_res.width * out_res.height 1942 < out_res.width * asd->sensor_array_res.height) { 1943 h_offset = asd->sensor_array_res.height 1944 - asd->sensor_array_res.width 1945 * out_res.height / out_res.width; 1946 h_offset = h_offset / 2; 1947 if (dz_config->zoom_region.origin.y < h_offset) 1948 dz_config->zoom_region.origin.y = 0; 1949 else 1950 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset; 1951 w_offset = 0; 1952 } else { 1953 w_offset = asd->sensor_array_res.width 1954 - asd->sensor_array_res.height 1955 * out_res.width / out_res.height; 1956 w_offset = w_offset / 2; 1957 if (dz_config->zoom_region.origin.x < w_offset) 1958 dz_config->zoom_region.origin.x = 0; 1959 else 1960 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset; 1961 h_offset = 0; 1962 } 1963 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x 1964 * eff_res.width 1965 / (asd->sensor_array_res.width - 2 * w_offset); 1966 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y 1967 * eff_res.height 1968 / (asd->sensor_array_res.height - 2 * h_offset); 1969 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width 1970 * eff_res.width 1971 / (asd->sensor_array_res.width - 2 * w_offset); 1972 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height 1973 * eff_res.height 1974 / (asd->sensor_array_res.height - 2 * h_offset); 1975 } 1976 1977 if (out_res.width * dz_config->zoom_region.resolution.height 1978 > dz_config->zoom_region.resolution.width * out_res.height) { 1979 dz_config->zoom_region.resolution.height = 1980 dz_config->zoom_region.resolution.width 1981 * out_res.height / out_res.width; 1982 } else { 1983 dz_config->zoom_region.resolution.width = 1984 dz_config->zoom_region.resolution.height 1985 * out_res.width / out_res.height; 1986 } 1987 dev_dbg(asd->isp->dev, 1988 "%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n", 1989 __func__, dz_config->zoom_region.origin.x, 1990 dz_config->zoom_region.origin.y, 1991 dz_config->zoom_region.resolution.width, 1992 dz_config->zoom_region.resolution.height, 1993 eff_res.width, eff_res.height, 1994 asd->sensor_array_res.width, 1995 asd->sensor_array_res.height, 1996 out_res.width, out_res.height); 1997 1998 if ((dz_config->zoom_region.origin.x + 1999 dz_config->zoom_region.resolution.width 2000 > eff_res.width) || 2001 (dz_config->zoom_region.origin.y + 2002 dz_config->zoom_region.resolution.height 2003 > eff_res.height)) 2004 return -EINVAL; 2005 2006 return 0; 2007 } 2008 2009 /* 2010 * Function to check the zoom region whether is effective 2011 */ 2012 static bool atomisp_check_zoom_region( 2013 struct atomisp_sub_device *asd, 2014 struct ia_css_dz_config *dz_config) 2015 { 2016 struct atomisp_resolution config; 2017 bool flag = false; 2018 unsigned int w, h; 2019 2020 memset(&config, 0, sizeof(struct atomisp_resolution)); 2021 2022 if (dz_config->dx && dz_config->dy) 2023 return true; 2024 2025 config.width = asd->sensor_array_res.width; 2026 config.height = asd->sensor_array_res.height; 2027 w = dz_config->zoom_region.origin.x + 2028 dz_config->zoom_region.resolution.width; 2029 h = dz_config->zoom_region.origin.y + 2030 dz_config->zoom_region.resolution.height; 2031 2032 if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0) 2033 flag = true; 2034 else 2035 /* setting error zoom region */ 2036 dev_err(asd->isp->dev, 2037 "%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n", 2038 __func__, dz_config->zoom_region.origin.x, 2039 dz_config->zoom_region.origin.y, 2040 dz_config->zoom_region.resolution.width, 2041 dz_config->zoom_region.resolution.height, 2042 config.width, config.height); 2043 2044 return flag; 2045 } 2046 2047 void atomisp_apply_css_parameters( 2048 struct atomisp_sub_device *asd, 2049 struct atomisp_css_params *css_param) 2050 { 2051 if (css_param->update_flag.wb_config) 2052 asd->params.config.wb_config = &css_param->wb_config; 2053 2054 if (css_param->update_flag.ob_config) 2055 asd->params.config.ob_config = &css_param->ob_config; 2056 2057 if (css_param->update_flag.dp_config) 2058 asd->params.config.dp_config = &css_param->dp_config; 2059 2060 if (css_param->update_flag.nr_config) 2061 asd->params.config.nr_config = &css_param->nr_config; 2062 2063 if (css_param->update_flag.ee_config) 2064 asd->params.config.ee_config = &css_param->ee_config; 2065 2066 if (css_param->update_flag.tnr_config) 2067 asd->params.config.tnr_config = &css_param->tnr_config; 2068 2069 if (css_param->update_flag.a3a_config) 2070 asd->params.config.s3a_config = &css_param->s3a_config; 2071 2072 if (css_param->update_flag.ctc_config) 2073 asd->params.config.ctc_config = &css_param->ctc_config; 2074 2075 if (css_param->update_flag.cnr_config) 2076 asd->params.config.cnr_config = &css_param->cnr_config; 2077 2078 if (css_param->update_flag.ecd_config) 2079 asd->params.config.ecd_config = &css_param->ecd_config; 2080 2081 if (css_param->update_flag.ynr_config) 2082 asd->params.config.ynr_config = &css_param->ynr_config; 2083 2084 if (css_param->update_flag.fc_config) 2085 asd->params.config.fc_config = &css_param->fc_config; 2086 2087 if (css_param->update_flag.macc_config) 2088 asd->params.config.macc_config = &css_param->macc_config; 2089 2090 if (css_param->update_flag.aa_config) 2091 asd->params.config.aa_config = &css_param->aa_config; 2092 2093 if (css_param->update_flag.anr_config) 2094 asd->params.config.anr_config = &css_param->anr_config; 2095 2096 if (css_param->update_flag.xnr_config) 2097 asd->params.config.xnr_config = &css_param->xnr_config; 2098 2099 if (css_param->update_flag.yuv2rgb_cc_config) 2100 asd->params.config.yuv2rgb_cc_config = &css_param->yuv2rgb_cc_config; 2101 2102 if (css_param->update_flag.rgb2yuv_cc_config) 2103 asd->params.config.rgb2yuv_cc_config = &css_param->rgb2yuv_cc_config; 2104 2105 if (css_param->update_flag.macc_table) 2106 asd->params.config.macc_table = &css_param->macc_table; 2107 2108 if (css_param->update_flag.xnr_table) 2109 asd->params.config.xnr_table = &css_param->xnr_table; 2110 2111 if (css_param->update_flag.r_gamma_table) 2112 asd->params.config.r_gamma_table = &css_param->r_gamma_table; 2113 2114 if (css_param->update_flag.g_gamma_table) 2115 asd->params.config.g_gamma_table = &css_param->g_gamma_table; 2116 2117 if (css_param->update_flag.b_gamma_table) 2118 asd->params.config.b_gamma_table = &css_param->b_gamma_table; 2119 2120 if (css_param->update_flag.anr_thres) 2121 atomisp_css_set_anr_thres(asd, &css_param->anr_thres); 2122 2123 if (css_param->update_flag.shading_table) 2124 asd->params.config.shading_table = css_param->shading_table; 2125 2126 if (css_param->update_flag.morph_table && asd->params.gdc_cac_en) 2127 asd->params.config.morph_table = css_param->morph_table; 2128 2129 if (css_param->update_flag.dvs2_coefs) { 2130 struct ia_css_dvs_grid_info *dvs_grid_info = 2131 atomisp_css_get_dvs_grid_info( 2132 &asd->params.curr_grid_info); 2133 2134 if (dvs_grid_info && dvs_grid_info->enable) 2135 atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff); 2136 } 2137 2138 if (css_param->update_flag.dvs_6axis_config) 2139 atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis); 2140 2141 atomisp_css_set_isp_config_id(asd, css_param->isp_config_id); 2142 /* 2143 * These configurations are on used by ISP1.x, not for ISP2.x, 2144 * so do not handle them. see comments of ia_css_isp_config. 2145 * 1 cc_config 2146 * 2 ce_config 2147 * 3 de_config 2148 * 4 gc_config 2149 * 5 gamma_table 2150 * 6 ctc_table 2151 * 7 dvs_coefs 2152 */ 2153 } 2154 2155 static unsigned int long copy_from_compatible(void *to, const void *from, 2156 unsigned long n, bool from_user) 2157 { 2158 if (from_user) 2159 return copy_from_user(to, (void __user *)from, n); 2160 else 2161 memcpy(to, from, n); 2162 return 0; 2163 } 2164 2165 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd, 2166 struct atomisp_parameters *arg, 2167 struct atomisp_css_params *css_param, 2168 bool from_user) 2169 { 2170 struct atomisp_parameters *cur_config = &css_param->update_flag; 2171 2172 if (!arg || !asd || !css_param) 2173 return -EINVAL; 2174 2175 if (arg->wb_config && (from_user || !cur_config->wb_config)) { 2176 if (copy_from_compatible(&css_param->wb_config, arg->wb_config, 2177 sizeof(struct ia_css_wb_config), 2178 from_user)) 2179 return -EFAULT; 2180 css_param->update_flag.wb_config = 2181 (struct atomisp_wb_config *)&css_param->wb_config; 2182 } 2183 2184 if (arg->ob_config && (from_user || !cur_config->ob_config)) { 2185 if (copy_from_compatible(&css_param->ob_config, arg->ob_config, 2186 sizeof(struct ia_css_ob_config), 2187 from_user)) 2188 return -EFAULT; 2189 css_param->update_flag.ob_config = 2190 (struct atomisp_ob_config *)&css_param->ob_config; 2191 } 2192 2193 if (arg->dp_config && (from_user || !cur_config->dp_config)) { 2194 if (copy_from_compatible(&css_param->dp_config, arg->dp_config, 2195 sizeof(struct ia_css_dp_config), 2196 from_user)) 2197 return -EFAULT; 2198 css_param->update_flag.dp_config = 2199 (struct atomisp_dp_config *)&css_param->dp_config; 2200 } 2201 2202 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) { 2203 if (arg->dz_config && (from_user || !cur_config->dz_config)) { 2204 if (copy_from_compatible(&css_param->dz_config, 2205 arg->dz_config, 2206 sizeof(struct ia_css_dz_config), 2207 from_user)) 2208 return -EFAULT; 2209 if (!atomisp_check_zoom_region(asd, 2210 &css_param->dz_config)) { 2211 dev_err(asd->isp->dev, "crop region error!"); 2212 return -EINVAL; 2213 } 2214 css_param->update_flag.dz_config = 2215 (struct atomisp_dz_config *) 2216 &css_param->dz_config; 2217 } 2218 } 2219 2220 if (arg->nr_config && (from_user || !cur_config->nr_config)) { 2221 if (copy_from_compatible(&css_param->nr_config, arg->nr_config, 2222 sizeof(struct ia_css_nr_config), 2223 from_user)) 2224 return -EFAULT; 2225 css_param->update_flag.nr_config = 2226 (struct atomisp_nr_config *)&css_param->nr_config; 2227 } 2228 2229 if (arg->ee_config && (from_user || !cur_config->ee_config)) { 2230 if (copy_from_compatible(&css_param->ee_config, arg->ee_config, 2231 sizeof(struct ia_css_ee_config), 2232 from_user)) 2233 return -EFAULT; 2234 css_param->update_flag.ee_config = 2235 (struct atomisp_ee_config *)&css_param->ee_config; 2236 } 2237 2238 if (arg->tnr_config && (from_user || !cur_config->tnr_config)) { 2239 if (copy_from_compatible(&css_param->tnr_config, 2240 arg->tnr_config, 2241 sizeof(struct ia_css_tnr_config), 2242 from_user)) 2243 return -EFAULT; 2244 css_param->update_flag.tnr_config = 2245 (struct atomisp_tnr_config *) 2246 &css_param->tnr_config; 2247 } 2248 2249 if (arg->a3a_config && (from_user || !cur_config->a3a_config)) { 2250 if (copy_from_compatible(&css_param->s3a_config, 2251 arg->a3a_config, 2252 sizeof(struct ia_css_3a_config), 2253 from_user)) 2254 return -EFAULT; 2255 css_param->update_flag.a3a_config = 2256 (struct atomisp_3a_config *)&css_param->s3a_config; 2257 } 2258 2259 if (arg->ctc_config && (from_user || !cur_config->ctc_config)) { 2260 if (copy_from_compatible(&css_param->ctc_config, 2261 arg->ctc_config, 2262 sizeof(struct ia_css_ctc_config), 2263 from_user)) 2264 return -EFAULT; 2265 css_param->update_flag.ctc_config = 2266 (struct atomisp_ctc_config *) 2267 &css_param->ctc_config; 2268 } 2269 2270 if (arg->cnr_config && (from_user || !cur_config->cnr_config)) { 2271 if (copy_from_compatible(&css_param->cnr_config, 2272 arg->cnr_config, 2273 sizeof(struct ia_css_cnr_config), 2274 from_user)) 2275 return -EFAULT; 2276 css_param->update_flag.cnr_config = 2277 (struct atomisp_cnr_config *) 2278 &css_param->cnr_config; 2279 } 2280 2281 if (arg->ecd_config && (from_user || !cur_config->ecd_config)) { 2282 if (copy_from_compatible(&css_param->ecd_config, 2283 arg->ecd_config, 2284 sizeof(struct ia_css_ecd_config), 2285 from_user)) 2286 return -EFAULT; 2287 css_param->update_flag.ecd_config = 2288 (struct atomisp_ecd_config *) 2289 &css_param->ecd_config; 2290 } 2291 2292 if (arg->ynr_config && (from_user || !cur_config->ynr_config)) { 2293 if (copy_from_compatible(&css_param->ynr_config, 2294 arg->ynr_config, 2295 sizeof(struct ia_css_ynr_config), 2296 from_user)) 2297 return -EFAULT; 2298 css_param->update_flag.ynr_config = 2299 (struct atomisp_ynr_config *) 2300 &css_param->ynr_config; 2301 } 2302 2303 if (arg->fc_config && (from_user || !cur_config->fc_config)) { 2304 if (copy_from_compatible(&css_param->fc_config, 2305 arg->fc_config, 2306 sizeof(struct ia_css_fc_config), 2307 from_user)) 2308 return -EFAULT; 2309 css_param->update_flag.fc_config = 2310 (struct atomisp_fc_config *)&css_param->fc_config; 2311 } 2312 2313 if (arg->macc_config && (from_user || !cur_config->macc_config)) { 2314 if (copy_from_compatible(&css_param->macc_config, 2315 arg->macc_config, 2316 sizeof(struct ia_css_macc_config), 2317 from_user)) 2318 return -EFAULT; 2319 css_param->update_flag.macc_config = 2320 (struct atomisp_macc_config *) 2321 &css_param->macc_config; 2322 } 2323 2324 if (arg->aa_config && (from_user || !cur_config->aa_config)) { 2325 if (copy_from_compatible(&css_param->aa_config, arg->aa_config, 2326 sizeof(struct ia_css_aa_config), 2327 from_user)) 2328 return -EFAULT; 2329 css_param->update_flag.aa_config = 2330 (struct atomisp_aa_config *)&css_param->aa_config; 2331 } 2332 2333 if (arg->anr_config && (from_user || !cur_config->anr_config)) { 2334 if (copy_from_compatible(&css_param->anr_config, 2335 arg->anr_config, 2336 sizeof(struct ia_css_anr_config), 2337 from_user)) 2338 return -EFAULT; 2339 css_param->update_flag.anr_config = 2340 (struct atomisp_anr_config *) 2341 &css_param->anr_config; 2342 } 2343 2344 if (arg->xnr_config && (from_user || !cur_config->xnr_config)) { 2345 if (copy_from_compatible(&css_param->xnr_config, 2346 arg->xnr_config, 2347 sizeof(struct ia_css_xnr_config), 2348 from_user)) 2349 return -EFAULT; 2350 css_param->update_flag.xnr_config = 2351 (struct atomisp_xnr_config *) 2352 &css_param->xnr_config; 2353 } 2354 2355 if (arg->yuv2rgb_cc_config && 2356 (from_user || !cur_config->yuv2rgb_cc_config)) { 2357 if (copy_from_compatible(&css_param->yuv2rgb_cc_config, 2358 arg->yuv2rgb_cc_config, 2359 sizeof(struct ia_css_cc_config), 2360 from_user)) 2361 return -EFAULT; 2362 css_param->update_flag.yuv2rgb_cc_config = 2363 (struct atomisp_cc_config *) 2364 &css_param->yuv2rgb_cc_config; 2365 } 2366 2367 if (arg->rgb2yuv_cc_config && 2368 (from_user || !cur_config->rgb2yuv_cc_config)) { 2369 if (copy_from_compatible(&css_param->rgb2yuv_cc_config, 2370 arg->rgb2yuv_cc_config, 2371 sizeof(struct ia_css_cc_config), 2372 from_user)) 2373 return -EFAULT; 2374 css_param->update_flag.rgb2yuv_cc_config = 2375 (struct atomisp_cc_config *) 2376 &css_param->rgb2yuv_cc_config; 2377 } 2378 2379 if (arg->macc_table && (from_user || !cur_config->macc_table)) { 2380 if (copy_from_compatible(&css_param->macc_table, 2381 arg->macc_table, 2382 sizeof(struct ia_css_macc_table), 2383 from_user)) 2384 return -EFAULT; 2385 css_param->update_flag.macc_table = 2386 (struct atomisp_macc_table *) 2387 &css_param->macc_table; 2388 } 2389 2390 if (arg->xnr_table && (from_user || !cur_config->xnr_table)) { 2391 if (copy_from_compatible(&css_param->xnr_table, 2392 arg->xnr_table, 2393 sizeof(struct ia_css_xnr_table), 2394 from_user)) 2395 return -EFAULT; 2396 css_param->update_flag.xnr_table = 2397 (struct atomisp_xnr_table *)&css_param->xnr_table; 2398 } 2399 2400 if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) { 2401 if (copy_from_compatible(&css_param->r_gamma_table, 2402 arg->r_gamma_table, 2403 sizeof(struct ia_css_rgb_gamma_table), 2404 from_user)) 2405 return -EFAULT; 2406 css_param->update_flag.r_gamma_table = 2407 (struct atomisp_rgb_gamma_table *) 2408 &css_param->r_gamma_table; 2409 } 2410 2411 if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) { 2412 if (copy_from_compatible(&css_param->g_gamma_table, 2413 arg->g_gamma_table, 2414 sizeof(struct ia_css_rgb_gamma_table), 2415 from_user)) 2416 return -EFAULT; 2417 css_param->update_flag.g_gamma_table = 2418 (struct atomisp_rgb_gamma_table *) 2419 &css_param->g_gamma_table; 2420 } 2421 2422 if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) { 2423 if (copy_from_compatible(&css_param->b_gamma_table, 2424 arg->b_gamma_table, 2425 sizeof(struct ia_css_rgb_gamma_table), 2426 from_user)) 2427 return -EFAULT; 2428 css_param->update_flag.b_gamma_table = 2429 (struct atomisp_rgb_gamma_table *) 2430 &css_param->b_gamma_table; 2431 } 2432 2433 if (arg->anr_thres && (from_user || !cur_config->anr_thres)) { 2434 if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres, 2435 sizeof(struct ia_css_anr_thres), 2436 from_user)) 2437 return -EFAULT; 2438 css_param->update_flag.anr_thres = 2439 (struct atomisp_anr_thres *)&css_param->anr_thres; 2440 } 2441 2442 if (from_user) 2443 css_param->isp_config_id = arg->isp_config_id; 2444 /* 2445 * These configurations are on used by ISP1.x, not for ISP2.x, 2446 * so do not handle them. see comments of ia_css_isp_config. 2447 * 1 cc_config 2448 * 2 ce_config 2449 * 3 de_config 2450 * 4 gc_config 2451 * 5 gamma_table 2452 * 6 ctc_table 2453 * 7 dvs_coefs 2454 */ 2455 return 0; 2456 } 2457 2458 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd, 2459 struct atomisp_shading_table *source_st, 2460 struct atomisp_css_params *css_param, 2461 bool from_user) 2462 { 2463 unsigned int i; 2464 unsigned int len_table; 2465 struct ia_css_shading_table *shading_table; 2466 struct ia_css_shading_table *old_table; 2467 struct atomisp_shading_table *st, dest_st; 2468 2469 if (!source_st) 2470 return 0; 2471 2472 if (!css_param) 2473 return -EINVAL; 2474 2475 if (!from_user && css_param->update_flag.shading_table) 2476 return 0; 2477 2478 if (IS_ISP2401) { 2479 if (copy_from_compatible(&dest_st, source_st, 2480 sizeof(struct atomisp_shading_table), 2481 from_user)) { 2482 dev_err(asd->isp->dev, "copy shading table failed!"); 2483 return -EFAULT; 2484 } 2485 st = &dest_st; 2486 } else { 2487 st = source_st; 2488 } 2489 2490 old_table = css_param->shading_table; 2491 2492 /* user config is to disable the shading table. */ 2493 if (!st->enable) { 2494 /* Generate a minimum table with enable = 0. */ 2495 shading_table = atomisp_css_shading_table_alloc(1, 1); 2496 if (!shading_table) 2497 return -ENOMEM; 2498 shading_table->enable = 0; 2499 goto set_lsc; 2500 } 2501 2502 /* Setting a new table. Validate first - all tables must be set */ 2503 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) { 2504 if (!st->data[i]) { 2505 dev_err(asd->isp->dev, "shading table validate failed"); 2506 return -EINVAL; 2507 } 2508 } 2509 2510 /* Shading table size per color */ 2511 if (st->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR || 2512 st->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) { 2513 dev_err(asd->isp->dev, "shading table w/h validate failed!"); 2514 return -EINVAL; 2515 } 2516 2517 shading_table = atomisp_css_shading_table_alloc(st->width, st->height); 2518 if (!shading_table) 2519 return -ENOMEM; 2520 2521 len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE; 2522 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) { 2523 if (copy_from_compatible(shading_table->data[i], 2524 st->data[i], len_table, from_user)) { 2525 atomisp_css_shading_table_free(shading_table); 2526 return -EFAULT; 2527 } 2528 } 2529 shading_table->sensor_width = st->sensor_width; 2530 shading_table->sensor_height = st->sensor_height; 2531 shading_table->fraction_bits = st->fraction_bits; 2532 shading_table->enable = st->enable; 2533 2534 /* No need to update shading table if it is the same */ 2535 if (old_table && 2536 old_table->sensor_width == shading_table->sensor_width && 2537 old_table->sensor_height == shading_table->sensor_height && 2538 old_table->width == shading_table->width && 2539 old_table->height == shading_table->height && 2540 old_table->fraction_bits == shading_table->fraction_bits && 2541 old_table->enable == shading_table->enable) { 2542 bool data_is_same = true; 2543 2544 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) { 2545 if (memcmp(shading_table->data[i], old_table->data[i], 2546 len_table) != 0) { 2547 data_is_same = false; 2548 break; 2549 } 2550 } 2551 2552 if (data_is_same) { 2553 atomisp_css_shading_table_free(shading_table); 2554 return 0; 2555 } 2556 } 2557 2558 set_lsc: 2559 /* set LSC to CSS */ 2560 css_param->shading_table = shading_table; 2561 css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table; 2562 asd->params.sc_en = shading_table; 2563 2564 if (old_table) 2565 atomisp_css_shading_table_free(old_table); 2566 2567 return 0; 2568 } 2569 2570 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd, 2571 struct ia_css_dvs2_coefficients *coefs, 2572 struct atomisp_css_params *css_param, 2573 bool from_user) 2574 { 2575 struct ia_css_dvs_grid_info *cur = 2576 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info); 2577 int dvs_hor_coef_bytes, dvs_ver_coef_bytes; 2578 struct ia_css_dvs2_coefficients dvs2_coefs; 2579 2580 if (!coefs || !cur) 2581 return 0; 2582 2583 if (!from_user && css_param->update_flag.dvs2_coefs) 2584 return 0; 2585 2586 if (!IS_ISP2401) { 2587 if (sizeof(*cur) != sizeof(coefs->grid) || 2588 memcmp(&coefs->grid, cur, sizeof(coefs->grid))) { 2589 dev_err(asd->isp->dev, "dvs grid mismatch!\n"); 2590 /* If the grid info in the argument differs from the current 2591 grid info, we tell the caller to reset the grid size and 2592 try again. */ 2593 return -EAGAIN; 2594 } 2595 2596 if (!coefs->hor_coefs.odd_real || 2597 !coefs->hor_coefs.odd_imag || 2598 !coefs->hor_coefs.even_real || 2599 !coefs->hor_coefs.even_imag || 2600 !coefs->ver_coefs.odd_real || 2601 !coefs->ver_coefs.odd_imag || 2602 !coefs->ver_coefs.even_real || 2603 !coefs->ver_coefs.even_imag) 2604 return -EINVAL; 2605 2606 if (!css_param->dvs2_coeff) { 2607 /* DIS coefficients. */ 2608 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur); 2609 if (!css_param->dvs2_coeff) 2610 return -ENOMEM; 2611 } 2612 2613 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes; 2614 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes; 2615 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real, 2616 coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) || 2617 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag, 2618 coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) || 2619 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real, 2620 coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) || 2621 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag, 2622 coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) || 2623 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real, 2624 coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) || 2625 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag, 2626 coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) || 2627 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real, 2628 coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) || 2629 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag, 2630 coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) { 2631 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff); 2632 css_param->dvs2_coeff = NULL; 2633 return -EFAULT; 2634 } 2635 } else { 2636 if (copy_from_compatible(&dvs2_coefs, coefs, 2637 sizeof(struct ia_css_dvs2_coefficients), 2638 from_user)) { 2639 dev_err(asd->isp->dev, "copy dvs2 coef failed"); 2640 return -EFAULT; 2641 } 2642 2643 if (sizeof(*cur) != sizeof(dvs2_coefs.grid) || 2644 memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) { 2645 dev_err(asd->isp->dev, "dvs grid mismatch!\n"); 2646 /* If the grid info in the argument differs from the current 2647 grid info, we tell the caller to reset the grid size and 2648 try again. */ 2649 return -EAGAIN; 2650 } 2651 2652 if (!dvs2_coefs.hor_coefs.odd_real || 2653 !dvs2_coefs.hor_coefs.odd_imag || 2654 !dvs2_coefs.hor_coefs.even_real || 2655 !dvs2_coefs.hor_coefs.even_imag || 2656 !dvs2_coefs.ver_coefs.odd_real || 2657 !dvs2_coefs.ver_coefs.odd_imag || 2658 !dvs2_coefs.ver_coefs.even_real || 2659 !dvs2_coefs.ver_coefs.even_imag) 2660 return -EINVAL; 2661 2662 if (!css_param->dvs2_coeff) { 2663 /* DIS coefficients. */ 2664 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur); 2665 if (!css_param->dvs2_coeff) 2666 return -ENOMEM; 2667 } 2668 2669 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes; 2670 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes; 2671 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real, 2672 dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) || 2673 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag, 2674 dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) || 2675 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real, 2676 dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) || 2677 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag, 2678 dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) || 2679 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real, 2680 dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) || 2681 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag, 2682 dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) || 2683 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real, 2684 dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) || 2685 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag, 2686 dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) { 2687 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff); 2688 css_param->dvs2_coeff = NULL; 2689 return -EFAULT; 2690 } 2691 } 2692 2693 css_param->update_flag.dvs2_coefs = 2694 (struct atomisp_dis_coefficients *)css_param->dvs2_coeff; 2695 return 0; 2696 } 2697 2698 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd, 2699 struct atomisp_dvs_6axis_config *source_6axis_config, 2700 struct atomisp_css_params *css_param, 2701 bool from_user) 2702 { 2703 struct ia_css_dvs_6axis_config *dvs_6axis_config; 2704 struct ia_css_dvs_6axis_config *old_6axis_config; 2705 struct ia_css_stream *stream = 2706 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream; 2707 struct ia_css_dvs_grid_info *dvs_grid_info = 2708 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info); 2709 int ret = -EFAULT; 2710 2711 if (!stream) { 2712 dev_err(asd->isp->dev, "%s: internal error!", __func__); 2713 return -EINVAL; 2714 } 2715 2716 if (!source_6axis_config || !dvs_grid_info) 2717 return 0; 2718 2719 if (!dvs_grid_info->enable) 2720 return 0; 2721 2722 if (!from_user && css_param->update_flag.dvs_6axis_config) 2723 return 0; 2724 2725 /* check whether need to reallocate for 6 axis config */ 2726 old_6axis_config = css_param->dvs_6axis; 2727 dvs_6axis_config = old_6axis_config; 2728 2729 if (IS_ISP2401) { 2730 struct ia_css_dvs_6axis_config t_6axis_config; 2731 2732 if (copy_from_compatible(&t_6axis_config, source_6axis_config, 2733 sizeof(struct atomisp_dvs_6axis_config), 2734 from_user)) { 2735 dev_err(asd->isp->dev, "copy morph table failed!"); 2736 return -EFAULT; 2737 } 2738 2739 if (old_6axis_config && 2740 (old_6axis_config->width_y != t_6axis_config.width_y || 2741 old_6axis_config->height_y != t_6axis_config.height_y || 2742 old_6axis_config->width_uv != t_6axis_config.width_uv || 2743 old_6axis_config->height_uv != t_6axis_config.height_uv)) { 2744 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis); 2745 css_param->dvs_6axis = NULL; 2746 2747 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream); 2748 if (!dvs_6axis_config) 2749 return -ENOMEM; 2750 } else if (!dvs_6axis_config) { 2751 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream); 2752 if (!dvs_6axis_config) 2753 return -ENOMEM; 2754 } 2755 2756 dvs_6axis_config->exp_id = t_6axis_config.exp_id; 2757 2758 if (copy_from_compatible(dvs_6axis_config->xcoords_y, 2759 t_6axis_config.xcoords_y, 2760 t_6axis_config.width_y * 2761 t_6axis_config.height_y * 2762 sizeof(*dvs_6axis_config->xcoords_y), 2763 from_user)) 2764 goto error; 2765 if (copy_from_compatible(dvs_6axis_config->ycoords_y, 2766 t_6axis_config.ycoords_y, 2767 t_6axis_config.width_y * 2768 t_6axis_config.height_y * 2769 sizeof(*dvs_6axis_config->ycoords_y), 2770 from_user)) 2771 goto error; 2772 if (copy_from_compatible(dvs_6axis_config->xcoords_uv, 2773 t_6axis_config.xcoords_uv, 2774 t_6axis_config.width_uv * 2775 t_6axis_config.height_uv * 2776 sizeof(*dvs_6axis_config->xcoords_uv), 2777 from_user)) 2778 goto error; 2779 if (copy_from_compatible(dvs_6axis_config->ycoords_uv, 2780 t_6axis_config.ycoords_uv, 2781 t_6axis_config.width_uv * 2782 t_6axis_config.height_uv * 2783 sizeof(*dvs_6axis_config->ycoords_uv), 2784 from_user)) 2785 goto error; 2786 } else { 2787 if (old_6axis_config && 2788 (old_6axis_config->width_y != source_6axis_config->width_y || 2789 old_6axis_config->height_y != source_6axis_config->height_y || 2790 old_6axis_config->width_uv != source_6axis_config->width_uv || 2791 old_6axis_config->height_uv != source_6axis_config->height_uv)) { 2792 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis); 2793 css_param->dvs_6axis = NULL; 2794 2795 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream); 2796 if (!dvs_6axis_config) 2797 return -ENOMEM; 2798 } else if (!dvs_6axis_config) { 2799 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream); 2800 if (!dvs_6axis_config) 2801 return -ENOMEM; 2802 } 2803 2804 dvs_6axis_config->exp_id = source_6axis_config->exp_id; 2805 2806 if (copy_from_compatible(dvs_6axis_config->xcoords_y, 2807 source_6axis_config->xcoords_y, 2808 source_6axis_config->width_y * 2809 source_6axis_config->height_y * 2810 sizeof(*source_6axis_config->xcoords_y), 2811 from_user)) 2812 goto error; 2813 if (copy_from_compatible(dvs_6axis_config->ycoords_y, 2814 source_6axis_config->ycoords_y, 2815 source_6axis_config->width_y * 2816 source_6axis_config->height_y * 2817 sizeof(*source_6axis_config->ycoords_y), 2818 from_user)) 2819 goto error; 2820 if (copy_from_compatible(dvs_6axis_config->xcoords_uv, 2821 source_6axis_config->xcoords_uv, 2822 source_6axis_config->width_uv * 2823 source_6axis_config->height_uv * 2824 sizeof(*source_6axis_config->xcoords_uv), 2825 from_user)) 2826 goto error; 2827 if (copy_from_compatible(dvs_6axis_config->ycoords_uv, 2828 source_6axis_config->ycoords_uv, 2829 source_6axis_config->width_uv * 2830 source_6axis_config->height_uv * 2831 sizeof(*source_6axis_config->ycoords_uv), 2832 from_user)) 2833 goto error; 2834 } 2835 css_param->dvs_6axis = dvs_6axis_config; 2836 css_param->update_flag.dvs_6axis_config = 2837 (struct atomisp_dvs_6axis_config *)dvs_6axis_config; 2838 return 0; 2839 2840 error: 2841 if (dvs_6axis_config) 2842 ia_css_dvs2_6axis_config_free(dvs_6axis_config); 2843 return ret; 2844 } 2845 2846 int atomisp_cp_morph_table(struct atomisp_sub_device *asd, 2847 struct atomisp_morph_table *source_morph_table, 2848 struct atomisp_css_params *css_param, 2849 bool from_user) 2850 { 2851 int ret = -EFAULT; 2852 unsigned int i; 2853 struct ia_css_morph_table *morph_table; 2854 struct ia_css_morph_table *old_morph_table; 2855 2856 if (!source_morph_table) 2857 return 0; 2858 2859 if (!from_user && css_param->update_flag.morph_table) 2860 return 0; 2861 2862 old_morph_table = css_param->morph_table; 2863 2864 if (IS_ISP2401) { 2865 struct ia_css_morph_table mtbl; 2866 2867 if (copy_from_compatible(&mtbl, source_morph_table, 2868 sizeof(struct atomisp_morph_table), 2869 from_user)) { 2870 dev_err(asd->isp->dev, "copy morph table failed!"); 2871 return -EFAULT; 2872 } 2873 2874 morph_table = atomisp_css_morph_table_allocate( 2875 mtbl.width, 2876 mtbl.height); 2877 if (!morph_table) 2878 return -ENOMEM; 2879 2880 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) { 2881 if (copy_from_compatible(morph_table->coordinates_x[i], 2882 (__force void *)source_morph_table->coordinates_x[i], 2883 mtbl.height * mtbl.width * 2884 sizeof(*morph_table->coordinates_x[i]), 2885 from_user)) 2886 goto error; 2887 2888 if (copy_from_compatible(morph_table->coordinates_y[i], 2889 (__force void *)source_morph_table->coordinates_y[i], 2890 mtbl.height * mtbl.width * 2891 sizeof(*morph_table->coordinates_y[i]), 2892 from_user)) 2893 goto error; 2894 } 2895 } else { 2896 morph_table = atomisp_css_morph_table_allocate( 2897 source_morph_table->width, 2898 source_morph_table->height); 2899 if (!morph_table) 2900 return -ENOMEM; 2901 2902 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) { 2903 if (copy_from_compatible(morph_table->coordinates_x[i], 2904 (__force void *)source_morph_table->coordinates_x[i], 2905 source_morph_table->height * source_morph_table->width * 2906 sizeof(*source_morph_table->coordinates_x[i]), 2907 from_user)) 2908 goto error; 2909 2910 if (copy_from_compatible(morph_table->coordinates_y[i], 2911 (__force void *)source_morph_table->coordinates_y[i], 2912 source_morph_table->height * source_morph_table->width * 2913 sizeof(*source_morph_table->coordinates_y[i]), 2914 from_user)) 2915 goto error; 2916 } 2917 } 2918 2919 css_param->morph_table = morph_table; 2920 if (old_morph_table) 2921 atomisp_css_morph_table_free(old_morph_table); 2922 css_param->update_flag.morph_table = 2923 (struct atomisp_morph_table *)morph_table; 2924 return 0; 2925 2926 error: 2927 if (morph_table) 2928 atomisp_css_morph_table_free(morph_table); 2929 return ret; 2930 } 2931 2932 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd, 2933 struct atomisp_parameters *arg, 2934 struct atomisp_css_params *css_param) 2935 { 2936 int ret; 2937 2938 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false); 2939 if (ret) 2940 return ret; 2941 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false); 2942 if (ret) 2943 return ret; 2944 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false); 2945 if (ret) 2946 return ret; 2947 ret = atomisp_css_cp_dvs2_coefs(asd, 2948 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs, 2949 css_param, false); 2950 if (ret) 2951 return ret; 2952 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config, 2953 css_param, false); 2954 return ret; 2955 } 2956 2957 void atomisp_free_css_parameters(struct atomisp_css_params *css_param) 2958 { 2959 if (css_param->dvs_6axis) { 2960 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis); 2961 css_param->dvs_6axis = NULL; 2962 } 2963 if (css_param->dvs2_coeff) { 2964 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff); 2965 css_param->dvs2_coeff = NULL; 2966 } 2967 if (css_param->shading_table) { 2968 ia_css_shading_table_free(css_param->shading_table); 2969 css_param->shading_table = NULL; 2970 } 2971 if (css_param->morph_table) { 2972 ia_css_morph_table_free(css_param->morph_table); 2973 css_param->morph_table = NULL; 2974 } 2975 } 2976 2977 static void atomisp_move_frame_to_activeq(struct ia_css_frame *frame, 2978 struct atomisp_css_params_with_list *param) 2979 { 2980 struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf); 2981 unsigned long irqflags; 2982 2983 pipe->frame_params[frame->vb.vb2_buf.index] = param; 2984 spin_lock_irqsave(&pipe->irq_lock, irqflags); 2985 list_move_tail(&frame->queue, &pipe->activeq); 2986 spin_unlock_irqrestore(&pipe->irq_lock, irqflags); 2987 } 2988 2989 /* 2990 * Check parameter queue list and buffer queue list to find out if matched items 2991 * and then set parameter to CSS and enqueue buffer to CSS. 2992 * Of course, if the buffer in buffer waiting list is not bound to a per-frame 2993 * parameter, it will be enqueued into CSS as long as the per-frame setting 2994 * buffers before it get enqueued. 2995 */ 2996 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe) 2997 { 2998 struct atomisp_sub_device *asd = pipe->asd; 2999 struct ia_css_frame *frame = NULL, *frame_tmp; 3000 struct atomisp_css_params_with_list *param = NULL, *param_tmp; 3001 bool need_to_enqueue_buffer = false; 3002 int i; 3003 3004 lockdep_assert_held(&asd->isp->mutex); 3005 3006 /* 3007 * CSS/FW requires set parameter and enqueue buffer happen after ISP 3008 * is streamon. 3009 */ 3010 if (!asd->streaming) 3011 return; 3012 3013 if (list_empty(&pipe->per_frame_params) || 3014 list_empty(&pipe->buffers_waiting_for_param)) 3015 return; 3016 3017 list_for_each_entry_safe(frame, frame_tmp, 3018 &pipe->buffers_waiting_for_param, queue) { 3019 i = frame->vb.vb2_buf.index; 3020 if (pipe->frame_request_config_id[i]) { 3021 list_for_each_entry_safe(param, param_tmp, 3022 &pipe->per_frame_params, list) { 3023 if (pipe->frame_request_config_id[i] != param->params.isp_config_id) 3024 continue; 3025 3026 list_del(¶m->list); 3027 3028 /* 3029 * clear the request config id as the buffer 3030 * will be handled and enqueued into CSS soon 3031 */ 3032 pipe->frame_request_config_id[i] = 0; 3033 atomisp_move_frame_to_activeq(frame, param); 3034 need_to_enqueue_buffer = true; 3035 break; 3036 } 3037 3038 /* If this is the end, stop further loop */ 3039 if (list_entry_is_head(param, &pipe->per_frame_params, list)) 3040 break; 3041 } else { 3042 atomisp_move_frame_to_activeq(frame, NULL); 3043 need_to_enqueue_buffer = true; 3044 } 3045 } 3046 3047 if (!need_to_enqueue_buffer) 3048 return; 3049 3050 atomisp_qbuffers_to_css(asd); 3051 } 3052 3053 /* 3054 * Function to configure ISP parameters 3055 */ 3056 int atomisp_set_parameters(struct video_device *vdev, 3057 struct atomisp_parameters *arg) 3058 { 3059 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev); 3060 struct atomisp_sub_device *asd = pipe->asd; 3061 struct atomisp_css_params_with_list *param = NULL; 3062 struct atomisp_css_params *css_param = &asd->params.css_param; 3063 int ret; 3064 3065 lockdep_assert_held(&asd->isp->mutex); 3066 3067 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) { 3068 dev_err(asd->isp->dev, "%s: internal error!\n", __func__); 3069 return -EINVAL; 3070 } 3071 3072 dev_dbg(asd->isp->dev, "set parameter(per_frame_setting %d) isp_config_id %d of %s\n", 3073 arg->per_frame_setting, arg->isp_config_id, vdev->name); 3074 3075 if (arg->per_frame_setting) { 3076 /* 3077 * Per-frame setting enabled, we allocate a new parameter 3078 * buffer to cache the parameters and only when frame buffers 3079 * are ready, the parameters will be set to CSS. 3080 * per-frame setting only works for the main output frame. 3081 */ 3082 param = kvzalloc(sizeof(*param), GFP_KERNEL); 3083 if (!param) { 3084 dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n", 3085 __func__); 3086 return -ENOMEM; 3087 } 3088 css_param = ¶m->params; 3089 } 3090 3091 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true); 3092 if (ret) 3093 goto apply_parameter_failed; 3094 3095 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true); 3096 if (ret) 3097 goto apply_parameter_failed; 3098 3099 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true); 3100 if (ret) 3101 goto apply_parameter_failed; 3102 3103 ret = atomisp_css_cp_dvs2_coefs(asd, 3104 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs, 3105 css_param, true); 3106 if (ret) 3107 goto apply_parameter_failed; 3108 3109 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config, 3110 css_param, true); 3111 if (ret) 3112 goto apply_parameter_failed; 3113 3114 if (!arg->per_frame_setting) { 3115 /* indicate to CSS that we have parameters to be updated */ 3116 asd->params.css_update_params_needed = true; 3117 } else { 3118 list_add_tail(¶m->list, &pipe->per_frame_params); 3119 atomisp_handle_parameter_and_buffer(pipe); 3120 } 3121 3122 return 0; 3123 3124 apply_parameter_failed: 3125 if (css_param) 3126 atomisp_free_css_parameters(css_param); 3127 kvfree(param); 3128 3129 return ret; 3130 } 3131 3132 /* 3133 * Function to set/get isp parameters to isp 3134 */ 3135 int atomisp_param(struct atomisp_sub_device *asd, int flag, 3136 struct atomisp_parm *config) 3137 { 3138 struct ia_css_pipe_config *vp_cfg = 3139 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]. 3140 pipe_configs[IA_CSS_PIPE_ID_VIDEO]; 3141 3142 /* Read parameter for 3A binary info */ 3143 if (flag == 0) { 3144 struct ia_css_dvs_grid_info *dvs_grid_info = 3145 atomisp_css_get_dvs_grid_info( 3146 &asd->params.curr_grid_info); 3147 3148 atomisp_curr_user_grid_info(asd, &config->info); 3149 3150 /* We always return the resolution and stride even if there is 3151 * no valid metadata. This allows the caller to get the 3152 * information needed to allocate user-space buffers. */ 3153 config->metadata_config.metadata_height = asd-> 3154 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info. 3155 metadata_info.resolution.height; 3156 config->metadata_config.metadata_stride = asd-> 3157 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info. 3158 metadata_info.stride; 3159 3160 /* update dvs grid info */ 3161 if (dvs_grid_info) 3162 memcpy(&config->dvs_grid, 3163 dvs_grid_info, 3164 sizeof(struct ia_css_dvs_grid_info)); 3165 3166 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) { 3167 config->dvs_envelop.width = 0; 3168 config->dvs_envelop.height = 0; 3169 return 0; 3170 } 3171 3172 /* update dvs envelop info */ 3173 config->dvs_envelop.width = vp_cfg->dvs_envelope.width; 3174 config->dvs_envelop.height = vp_cfg->dvs_envelope.height; 3175 return 0; 3176 } 3177 3178 memcpy(&asd->params.css_param.wb_config, &config->wb_config, 3179 sizeof(struct ia_css_wb_config)); 3180 memcpy(&asd->params.css_param.ob_config, &config->ob_config, 3181 sizeof(struct ia_css_ob_config)); 3182 memcpy(&asd->params.css_param.dp_config, &config->dp_config, 3183 sizeof(struct ia_css_dp_config)); 3184 memcpy(&asd->params.css_param.de_config, &config->de_config, 3185 sizeof(struct ia_css_de_config)); 3186 memcpy(&asd->params.css_param.dz_config, &config->dz_config, 3187 sizeof(struct ia_css_dz_config)); 3188 memcpy(&asd->params.css_param.ce_config, &config->ce_config, 3189 sizeof(struct ia_css_ce_config)); 3190 memcpy(&asd->params.css_param.nr_config, &config->nr_config, 3191 sizeof(struct ia_css_nr_config)); 3192 memcpy(&asd->params.css_param.ee_config, &config->ee_config, 3193 sizeof(struct ia_css_ee_config)); 3194 memcpy(&asd->params.css_param.tnr_config, &config->tnr_config, 3195 sizeof(struct ia_css_tnr_config)); 3196 3197 if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) { 3198 asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3]; 3199 asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4]; 3200 asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5]; 3201 asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6]; 3202 asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7]; 3203 asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8]; 3204 } 3205 3206 if (asd->params.color_effect != V4L2_COLORFX_SEPIA && 3207 asd->params.color_effect != V4L2_COLORFX_BW) { 3208 memcpy(&asd->params.css_param.cc_config, &config->cc_config, 3209 sizeof(struct ia_css_cc_config)); 3210 asd->params.config.cc_config = &asd->params.css_param.cc_config; 3211 } 3212 3213 asd->params.config.wb_config = &asd->params.css_param.wb_config; 3214 asd->params.config.ob_config = &asd->params.css_param.ob_config; 3215 asd->params.config.de_config = &asd->params.css_param.de_config; 3216 asd->params.config.dz_config = &asd->params.css_param.dz_config; 3217 asd->params.config.ce_config = &asd->params.css_param.ce_config; 3218 asd->params.config.dp_config = &asd->params.css_param.dp_config; 3219 asd->params.config.nr_config = &asd->params.css_param.nr_config; 3220 asd->params.config.ee_config = &asd->params.css_param.ee_config; 3221 asd->params.config.tnr_config = &asd->params.css_param.tnr_config; 3222 asd->params.css_update_params_needed = true; 3223 3224 return 0; 3225 } 3226 3227 /* 3228 * Function to configure color effect of the image 3229 */ 3230 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag, 3231 __s32 *effect) 3232 { 3233 struct ia_css_cc_config *cc_config = NULL; 3234 struct ia_css_macc_table *macc_table = NULL; 3235 struct ia_css_ctc_table *ctc_table = NULL; 3236 int ret = 0; 3237 struct v4l2_control control; 3238 struct atomisp_device *isp = asd->isp; 3239 3240 if (flag == 0) { 3241 *effect = asd->params.color_effect; 3242 return 0; 3243 } 3244 3245 control.id = V4L2_CID_COLORFX; 3246 control.value = *effect; 3247 ret = 3248 v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler, 3249 &control); 3250 /* 3251 * if set color effect to sensor successfully, return 3252 * 0 directly. 3253 */ 3254 if (!ret) { 3255 asd->params.color_effect = (u32)*effect; 3256 return 0; 3257 } 3258 3259 if (*effect == asd->params.color_effect) 3260 return 0; 3261 3262 /* 3263 * isp_subdev->params.macc_en should be set to false. 3264 */ 3265 asd->params.macc_en = false; 3266 3267 switch (*effect) { 3268 case V4L2_COLORFX_NONE: 3269 macc_table = &asd->params.css_param.macc_table; 3270 asd->params.macc_en = true; 3271 break; 3272 case V4L2_COLORFX_SEPIA: 3273 cc_config = &sepia_cc_config; 3274 break; 3275 case V4L2_COLORFX_NEGATIVE: 3276 cc_config = &nega_cc_config; 3277 break; 3278 case V4L2_COLORFX_BW: 3279 cc_config = &mono_cc_config; 3280 break; 3281 case V4L2_COLORFX_SKY_BLUE: 3282 macc_table = &blue_macc_table; 3283 asd->params.macc_en = true; 3284 break; 3285 case V4L2_COLORFX_GRASS_GREEN: 3286 macc_table = &green_macc_table; 3287 asd->params.macc_en = true; 3288 break; 3289 case V4L2_COLORFX_SKIN_WHITEN_LOW: 3290 macc_table = &skin_low_macc_table; 3291 asd->params.macc_en = true; 3292 break; 3293 case V4L2_COLORFX_SKIN_WHITEN: 3294 macc_table = &skin_medium_macc_table; 3295 asd->params.macc_en = true; 3296 break; 3297 case V4L2_COLORFX_SKIN_WHITEN_HIGH: 3298 macc_table = &skin_high_macc_table; 3299 asd->params.macc_en = true; 3300 break; 3301 case V4L2_COLORFX_VIVID: 3302 ctc_table = &vivid_ctc_table; 3303 break; 3304 default: 3305 return -EINVAL; 3306 } 3307 atomisp_update_capture_mode(asd); 3308 3309 if (cc_config) 3310 asd->params.config.cc_config = cc_config; 3311 if (macc_table) 3312 asd->params.config.macc_table = macc_table; 3313 if (ctc_table) 3314 atomisp_css_set_ctc_table(asd, ctc_table); 3315 asd->params.color_effect = (u32)*effect; 3316 asd->params.css_update_params_needed = true; 3317 return 0; 3318 } 3319 3320 /* 3321 * Function to configure bad pixel correction 3322 */ 3323 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag, 3324 __s32 *value) 3325 { 3326 if (flag == 0) { 3327 *value = asd->params.bad_pixel_en; 3328 return 0; 3329 } 3330 asd->params.bad_pixel_en = !!*value; 3331 3332 return 0; 3333 } 3334 3335 /* 3336 * Function to configure bad pixel correction params 3337 */ 3338 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag, 3339 struct atomisp_dp_config *config) 3340 { 3341 if (flag == 0) { 3342 /* Get bad pixel from current setup */ 3343 if (atomisp_css_get_dp_config(asd, config)) 3344 return -EINVAL; 3345 } else { 3346 /* Set bad pixel to isp parameters */ 3347 memcpy(&asd->params.css_param.dp_config, config, 3348 sizeof(asd->params.css_param.dp_config)); 3349 asd->params.config.dp_config = &asd->params.css_param.dp_config; 3350 asd->params.css_update_params_needed = true; 3351 } 3352 3353 return 0; 3354 } 3355 3356 /* 3357 * Function to enable/disable video image stablization 3358 */ 3359 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag, 3360 __s32 *value) 3361 { 3362 if (flag == 0) 3363 *value = asd->params.video_dis_en; 3364 else 3365 asd->params.video_dis_en = !!*value; 3366 3367 return 0; 3368 } 3369 3370 /* 3371 * Function to configure fixed pattern noise 3372 */ 3373 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag, 3374 __s32 *value) 3375 { 3376 if (flag == 0) { 3377 *value = asd->params.fpn_en; 3378 return 0; 3379 } 3380 3381 if (*value == 0) { 3382 asd->params.fpn_en = false; 3383 return 0; 3384 } 3385 3386 /* Add function to get black from from sensor with shutter off */ 3387 return 0; 3388 } 3389 3390 static unsigned int 3391 atomisp_bytesperline_to_padded_width(unsigned int bytesperline, 3392 enum ia_css_frame_format format) 3393 { 3394 switch (format) { 3395 case IA_CSS_FRAME_FORMAT_UYVY: 3396 case IA_CSS_FRAME_FORMAT_YUYV: 3397 case IA_CSS_FRAME_FORMAT_RAW: 3398 case IA_CSS_FRAME_FORMAT_RGB565: 3399 return bytesperline / 2; 3400 case IA_CSS_FRAME_FORMAT_RGBA888: 3401 return bytesperline / 4; 3402 /* The following cases could be removed, but we leave them 3403 in to document the formats that are included. */ 3404 case IA_CSS_FRAME_FORMAT_NV11: 3405 case IA_CSS_FRAME_FORMAT_NV12: 3406 case IA_CSS_FRAME_FORMAT_NV16: 3407 case IA_CSS_FRAME_FORMAT_NV21: 3408 case IA_CSS_FRAME_FORMAT_NV61: 3409 case IA_CSS_FRAME_FORMAT_YV12: 3410 case IA_CSS_FRAME_FORMAT_YV16: 3411 case IA_CSS_FRAME_FORMAT_YUV420: 3412 case IA_CSS_FRAME_FORMAT_YUV420_16: 3413 case IA_CSS_FRAME_FORMAT_YUV422: 3414 case IA_CSS_FRAME_FORMAT_YUV422_16: 3415 case IA_CSS_FRAME_FORMAT_YUV444: 3416 case IA_CSS_FRAME_FORMAT_YUV_LINE: 3417 case IA_CSS_FRAME_FORMAT_PLANAR_RGB888: 3418 case IA_CSS_FRAME_FORMAT_QPLANE6: 3419 case IA_CSS_FRAME_FORMAT_BINARY_8: 3420 default: 3421 return bytesperline; 3422 } 3423 } 3424 3425 static int 3426 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg, 3427 struct ia_css_frame **result) 3428 { 3429 struct ia_css_frame *res = NULL; 3430 unsigned int padded_width; 3431 enum ia_css_frame_format sh_format; 3432 char *tmp_buf = NULL; 3433 int ret = 0; 3434 3435 sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat); 3436 padded_width = atomisp_bytesperline_to_padded_width( 3437 arg->fmt.bytesperline, sh_format); 3438 3439 /* Note: the padded width on an ia_css_frame is in elements, not in 3440 bytes. The RAW frame we use here should always be a 16bit RAW 3441 frame. This is why we bytesperline/2 is equal to the padded with */ 3442 if (ia_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height, 3443 sh_format, padded_width, 0)) { 3444 ret = -ENOMEM; 3445 goto err; 3446 } 3447 3448 tmp_buf = vmalloc(arg->fmt.sizeimage); 3449 if (!tmp_buf) { 3450 ret = -ENOMEM; 3451 goto err; 3452 } 3453 if (copy_from_user(tmp_buf, (void __user __force *)arg->base, 3454 arg->fmt.sizeimage)) { 3455 ret = -EFAULT; 3456 goto err; 3457 } 3458 3459 if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) { 3460 ret = -EINVAL; 3461 goto err; 3462 } 3463 3464 err: 3465 if (ret && res) 3466 ia_css_frame_free(res); 3467 vfree(tmp_buf); 3468 if (ret == 0) 3469 *result = res; 3470 return ret; 3471 } 3472 3473 /* 3474 * Function to configure fixed pattern noise table 3475 */ 3476 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd, 3477 struct v4l2_framebuffer *arg) 3478 { 3479 struct ia_css_frame *raw_black_frame = NULL; 3480 int ret; 3481 3482 if (!arg) 3483 return -EINVAL; 3484 3485 ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame); 3486 if (ret) 3487 return ret; 3488 3489 if (sh_css_set_black_frame(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream, 3490 raw_black_frame) != 0) 3491 return -ENOMEM; 3492 3493 ia_css_frame_free(raw_black_frame); 3494 return ret; 3495 } 3496 3497 /* 3498 * Function to configure false color correction 3499 */ 3500 int atomisp_false_color(struct atomisp_sub_device *asd, int flag, 3501 __s32 *value) 3502 { 3503 /* Get nr config from current setup */ 3504 if (flag == 0) { 3505 *value = asd->params.false_color; 3506 return 0; 3507 } 3508 3509 /* Set nr config to isp parameters */ 3510 if (*value) { 3511 asd->params.config.de_config = NULL; 3512 } else { 3513 asd->params.css_param.de_config.pixelnoise = 0; 3514 asd->params.config.de_config = &asd->params.css_param.de_config; 3515 } 3516 asd->params.css_update_params_needed = true; 3517 asd->params.false_color = *value; 3518 return 0; 3519 } 3520 3521 /* 3522 * Function to configure bad pixel correction params 3523 */ 3524 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag, 3525 struct atomisp_de_config *config) 3526 { 3527 if (flag == 0) { 3528 /* Get false color from current setup */ 3529 if (atomisp_css_get_de_config(asd, config)) 3530 return -EINVAL; 3531 } else { 3532 /* Set false color to isp parameters */ 3533 memcpy(&asd->params.css_param.de_config, config, 3534 sizeof(asd->params.css_param.de_config)); 3535 asd->params.config.de_config = &asd->params.css_param.de_config; 3536 asd->params.css_update_params_needed = true; 3537 } 3538 3539 return 0; 3540 } 3541 3542 /* 3543 * Function to configure white balance params 3544 */ 3545 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag, 3546 struct atomisp_wb_config *config) 3547 { 3548 if (flag == 0) { 3549 /* Get white balance from current setup */ 3550 if (atomisp_css_get_wb_config(asd, config)) 3551 return -EINVAL; 3552 } else { 3553 /* Set white balance to isp parameters */ 3554 memcpy(&asd->params.css_param.wb_config, config, 3555 sizeof(asd->params.css_param.wb_config)); 3556 asd->params.config.wb_config = &asd->params.css_param.wb_config; 3557 asd->params.css_update_params_needed = true; 3558 } 3559 3560 return 0; 3561 } 3562 3563 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag, 3564 struct atomisp_3a_config *config) 3565 { 3566 struct atomisp_device *isp = asd->isp; 3567 3568 dev_dbg(isp->dev, ">%s %d\n", __func__, flag); 3569 3570 if (flag == 0) { 3571 /* Get white balance from current setup */ 3572 if (atomisp_css_get_3a_config(asd, config)) 3573 return -EINVAL; 3574 } else { 3575 /* Set white balance to isp parameters */ 3576 memcpy(&asd->params.css_param.s3a_config, config, 3577 sizeof(asd->params.css_param.s3a_config)); 3578 asd->params.config.s3a_config = &asd->params.css_param.s3a_config; 3579 asd->params.css_update_params_needed = true; 3580 } 3581 3582 dev_dbg(isp->dev, "<%s %d\n", __func__, flag); 3583 return 0; 3584 } 3585 3586 /* 3587 * Function to setup digital zoom 3588 */ 3589 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag, 3590 __s32 *value) 3591 { 3592 u32 zoom; 3593 struct atomisp_device *isp = asd->isp; 3594 3595 unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR; 3596 3597 if (flag == 0) { 3598 atomisp_css_get_zoom_factor(asd, &zoom); 3599 *value = max_zoom - zoom; 3600 } else { 3601 if (*value < 0) 3602 return -EINVAL; 3603 3604 zoom = max_zoom - min_t(u32, max_zoom - 1, *value); 3605 atomisp_css_set_zoom_factor(asd, zoom); 3606 3607 dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom); 3608 asd->params.css_update_params_needed = true; 3609 } 3610 3611 return 0; 3612 } 3613 3614 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd, 3615 u16 stream_index, struct atomisp_input_stream_info *stream_info) 3616 { 3617 int i; 3618 3619 /* assign virtual channel id return from sensor driver query */ 3620 asd->stream_env[stream_index].ch_id = stream_info->ch_id; 3621 asd->stream_env[stream_index].isys_configs = stream_info->isys_configs; 3622 for (i = 0; i < stream_info->isys_configs; i++) { 3623 asd->stream_env[stream_index].isys_info[i].input_format = 3624 stream_info->isys_info[i].input_format; 3625 asd->stream_env[stream_index].isys_info[i].width = 3626 stream_info->isys_info[i].width; 3627 asd->stream_env[stream_index].isys_info[i].height = 3628 stream_info->isys_info[i].height; 3629 } 3630 } 3631 3632 static void __atomisp_init_stream_info(u16 stream_index, 3633 struct atomisp_input_stream_info *stream_info) 3634 { 3635 int i; 3636 3637 stream_info->enable = 1; 3638 stream_info->stream = stream_index; 3639 stream_info->ch_id = 0; 3640 stream_info->isys_configs = 0; 3641 for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) { 3642 stream_info->isys_info[i].input_format = 0; 3643 stream_info->isys_info[i].width = 0; 3644 stream_info->isys_info[i].height = 0; 3645 } 3646 } 3647 3648 static void atomisp_fill_pix_format(struct v4l2_pix_format *f, 3649 u32 width, u32 height, 3650 const struct atomisp_format_bridge *br_fmt) 3651 { 3652 u32 bytes; 3653 3654 f->width = width; 3655 f->height = height; 3656 f->pixelformat = br_fmt->pixelformat; 3657 3658 /* Adding padding to width for bytesperline calculation */ 3659 width = ia_css_frame_pad_width(width, br_fmt->sh_fmt); 3660 bytes = BITS_TO_BYTES(br_fmt->depth * width); 3661 3662 if (br_fmt->planar) 3663 f->bytesperline = width; 3664 else 3665 f->bytesperline = bytes; 3666 3667 f->sizeimage = PAGE_ALIGN(height * bytes); 3668 3669 if (f->field == V4L2_FIELD_ANY) 3670 f->field = V4L2_FIELD_NONE; 3671 3672 /* 3673 * FIXME: do we need to set this up differently, depending on the 3674 * sensor or the pipeline? 3675 */ 3676 f->colorspace = V4L2_COLORSPACE_REC709; 3677 f->ycbcr_enc = V4L2_YCBCR_ENC_709; 3678 f->xfer_func = V4L2_XFER_FUNC_709; 3679 } 3680 3681 /* Get sensor padding values for the non padded width x height resolution */ 3682 void atomisp_get_padding(struct atomisp_device *isp, u32 width, u32 height, 3683 u32 *padding_w, u32 *padding_h) 3684 { 3685 struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr]; 3686 struct v4l2_rect native_rect = input->native_rect; 3687 const struct atomisp_in_fmt_conv *fc = NULL; 3688 u32 min_pad_w = ISP2400_MIN_PAD_W; 3689 u32 min_pad_h = ISP2400_MIN_PAD_H; 3690 struct v4l2_mbus_framefmt *sink; 3691 3692 if (!input->crop_support) { 3693 *padding_w = pad_w; 3694 *padding_h = pad_h; 3695 return; 3696 } 3697 3698 width = min(width, input->active_rect.width); 3699 height = min(height, input->active_rect.height); 3700 3701 if (input->binning_support && width <= (input->active_rect.width / 2) && 3702 height <= (input->active_rect.height / 2)) { 3703 native_rect.width /= 2; 3704 native_rect.height /= 2; 3705 } 3706 3707 *padding_w = min_t(u32, (native_rect.width - width) & ~1, pad_w); 3708 *padding_h = min_t(u32, (native_rect.height - height) & ~1, pad_h); 3709 3710 /* The below minimum padding requirements are for BYT / ISP2400 only */ 3711 if (IS_ISP2401) 3712 return; 3713 3714 sink = atomisp_subdev_get_ffmt(&isp->asd.subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE, 3715 ATOMISP_SUBDEV_PAD_SINK); 3716 if (sink) 3717 fc = atomisp_find_in_fmt_conv(sink->code); 3718 if (!fc) { 3719 dev_warn(isp->dev, "%s: Could not get sensor format\n", __func__); 3720 goto apply_min_padding; 3721 } 3722 3723 /* 3724 * The ISP only supports GRBG for other bayer-orders additional padding 3725 * is used so that the raw sensor data can be cropped to fix the order. 3726 */ 3727 if (fc->bayer_order == IA_CSS_BAYER_ORDER_RGGB || 3728 fc->bayer_order == IA_CSS_BAYER_ORDER_GBRG) 3729 min_pad_w += 2; 3730 3731 if (fc->bayer_order == IA_CSS_BAYER_ORDER_BGGR || 3732 fc->bayer_order == IA_CSS_BAYER_ORDER_GBRG) 3733 min_pad_h += 2; 3734 3735 apply_min_padding: 3736 *padding_w = max_t(u32, *padding_w, min_pad_w); 3737 *padding_h = max_t(u32, *padding_h, min_pad_h); 3738 } 3739 3740 static int atomisp_set_crop(struct atomisp_device *isp, 3741 const struct v4l2_mbus_framefmt *format, 3742 int which) 3743 { 3744 struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr]; 3745 struct v4l2_subdev_state pad_state = { 3746 .pads = &input->pad_cfg, 3747 }; 3748 struct v4l2_subdev_selection sel = { 3749 .which = which, 3750 .target = V4L2_SEL_TGT_CROP, 3751 .r.width = format->width, 3752 .r.height = format->height, 3753 }; 3754 int ret; 3755 3756 if (!input->crop_support) 3757 return 0; 3758 3759 /* Cropping is done before binning, when binning double the crop rect */ 3760 if (input->binning_support && sel.r.width <= (input->native_rect.width / 2) && 3761 sel.r.height <= (input->native_rect.height / 2)) { 3762 sel.r.width *= 2; 3763 sel.r.height *= 2; 3764 } 3765 3766 /* Clamp to avoid top/left calculations overflowing */ 3767 sel.r.width = min(sel.r.width, input->native_rect.width); 3768 sel.r.height = min(sel.r.height, input->native_rect.height); 3769 3770 sel.r.left = ((input->native_rect.width - sel.r.width) / 2) & ~1; 3771 sel.r.top = ((input->native_rect.height - sel.r.height) / 2) & ~1; 3772 3773 ret = v4l2_subdev_call(input->camera, pad, set_selection, &pad_state, &sel); 3774 if (ret) 3775 dev_err(isp->dev, "Error setting crop to %ux%u @%ux%u: %d\n", 3776 sel.r.width, sel.r.height, sel.r.left, sel.r.top, ret); 3777 3778 return ret; 3779 } 3780 3781 /* This function looks up the closest available resolution. */ 3782 int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f, 3783 const struct atomisp_format_bridge **fmt_ret, 3784 const struct atomisp_format_bridge **snr_fmt_ret) 3785 { 3786 const struct atomisp_format_bridge *fmt, *snr_fmt; 3787 struct atomisp_sub_device *asd = &isp->asd; 3788 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr]; 3789 struct v4l2_subdev_state pad_state = { 3790 .pads = &input->pad_cfg, 3791 }; 3792 struct v4l2_subdev_format format = { 3793 .which = V4L2_SUBDEV_FORMAT_TRY, 3794 }; 3795 u32 padding_w, padding_h; 3796 int ret; 3797 3798 if (!input->camera) 3799 return -EINVAL; 3800 3801 fmt = atomisp_get_format_bridge(f->pixelformat); 3802 /* Currently, raw formats are broken!!! */ 3803 if (!fmt || fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) { 3804 f->pixelformat = V4L2_PIX_FMT_YUV420; 3805 3806 fmt = atomisp_get_format_bridge(f->pixelformat); 3807 if (!fmt) 3808 return -EINVAL; 3809 } 3810 3811 /* 3812 * atomisp_set_fmt() will set the sensor resolution to the requested 3813 * resolution + padding. Add padding here and remove it again after 3814 * the set_fmt call, like atomisp_set_fmt_to_snr() does. 3815 */ 3816 atomisp_get_padding(isp, f->width, f->height, &padding_w, &padding_h); 3817 v4l2_fill_mbus_format(&format.format, f, fmt->mbus_code); 3818 format.format.width += padding_w; 3819 format.format.height += padding_h; 3820 3821 dev_dbg(isp->dev, "try_mbus_fmt: asking for %ux%u\n", 3822 format.format.width, format.format.height); 3823 3824 ret = atomisp_set_crop(isp, &format.format, V4L2_SUBDEV_FORMAT_TRY); 3825 if (ret) 3826 return ret; 3827 3828 ret = v4l2_subdev_call(input->camera, pad, set_fmt, &pad_state, &format); 3829 if (ret) 3830 return ret; 3831 3832 dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n", 3833 format.format.width, format.format.height); 3834 3835 snr_fmt = atomisp_get_format_bridge_from_mbus(format.format.code); 3836 if (!snr_fmt) { 3837 dev_err(isp->dev, "unknown sensor format 0x%8.8x\n", 3838 format.format.code); 3839 return -EINVAL; 3840 } 3841 3842 f->width = format.format.width - padding_w; 3843 f->height = format.format.height - padding_h; 3844 3845 /* 3846 * If the format is jpeg or custom RAW, then the width and height will 3847 * not satisfy the normal atomisp requirements and no need to check 3848 * the below conditions. So just assign to what is being returned from 3849 * the sensor driver. 3850 */ 3851 if (f->pixelformat == V4L2_PIX_FMT_JPEG || 3852 f->pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) 3853 goto out_fill_pix_format; 3854 3855 /* app vs isp */ 3856 f->width = rounddown(clamp_t(u32, f->width, ATOM_ISP_MIN_WIDTH, 3857 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH); 3858 f->height = rounddown(clamp_t(u32, f->height, ATOM_ISP_MIN_HEIGHT, 3859 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT); 3860 3861 out_fill_pix_format: 3862 atomisp_fill_pix_format(f, f->width, f->height, fmt); 3863 3864 if (fmt_ret) 3865 *fmt_ret = fmt; 3866 3867 if (snr_fmt_ret) 3868 *snr_fmt_ret = snr_fmt; 3869 3870 return 0; 3871 } 3872 3873 enum mipi_port_id atomisp_port_to_mipi_port(struct atomisp_device *isp, 3874 enum atomisp_camera_port port) 3875 { 3876 switch (port) { 3877 case ATOMISP_CAMERA_PORT_PRIMARY: 3878 return MIPI_PORT0_ID; 3879 case ATOMISP_CAMERA_PORT_SECONDARY: 3880 return MIPI_PORT1_ID; 3881 case ATOMISP_CAMERA_PORT_TERTIARY: 3882 return MIPI_PORT2_ID; 3883 default: 3884 dev_err(isp->dev, "unsupported port: %d\n", port); 3885 return MIPI_PORT0_ID; 3886 } 3887 } 3888 3889 static inline int atomisp_set_sensor_mipi_to_isp( 3890 struct atomisp_sub_device *asd, 3891 enum atomisp_input_stream_id stream_id, 3892 struct camera_mipi_info *mipi_info) 3893 { 3894 struct v4l2_control ctrl; 3895 struct atomisp_device *isp = asd->isp; 3896 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr]; 3897 const struct atomisp_in_fmt_conv *fc; 3898 int mipi_freq = 0; 3899 unsigned int input_format, bayer_order; 3900 enum atomisp_input_format metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED; 3901 u32 mipi_port, metadata_width = 0, metadata_height = 0; 3902 3903 ctrl.id = V4L2_CID_LINK_FREQ; 3904 if (v4l2_g_ctrl(input->camera->ctrl_handler, &ctrl) == 0) 3905 mipi_freq = ctrl.value; 3906 3907 if (asd->stream_env[stream_id].isys_configs == 1) { 3908 input_format = 3909 asd->stream_env[stream_id].isys_info[0].input_format; 3910 atomisp_css_isys_set_format(asd, stream_id, 3911 input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX); 3912 } else if (asd->stream_env[stream_id].isys_configs == 2) { 3913 atomisp_css_isys_two_stream_cfg_update_stream1( 3914 asd, stream_id, 3915 asd->stream_env[stream_id].isys_info[0].input_format, 3916 asd->stream_env[stream_id].isys_info[0].width, 3917 asd->stream_env[stream_id].isys_info[0].height); 3918 3919 atomisp_css_isys_two_stream_cfg_update_stream2( 3920 asd, stream_id, 3921 asd->stream_env[stream_id].isys_info[1].input_format, 3922 asd->stream_env[stream_id].isys_info[1].width, 3923 asd->stream_env[stream_id].isys_info[1].height); 3924 } 3925 3926 /* Compatibility for sensors which provide no media bus code 3927 * in s_mbus_framefmt() nor support pad formats. */ 3928 if (mipi_info && mipi_info->input_format != -1) { 3929 bayer_order = mipi_info->raw_bayer_order; 3930 3931 /* Input stream config is still needs configured */ 3932 /* TODO: Check if this is necessary */ 3933 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt( 3934 mipi_info->input_format); 3935 if (!fc) 3936 return -EINVAL; 3937 input_format = fc->atomisp_in_fmt; 3938 metadata_format = mipi_info->metadata_format; 3939 metadata_width = mipi_info->metadata_width; 3940 metadata_height = mipi_info->metadata_height; 3941 } else { 3942 struct v4l2_mbus_framefmt *sink; 3943 3944 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL, 3945 V4L2_SUBDEV_FORMAT_ACTIVE, 3946 ATOMISP_SUBDEV_PAD_SINK); 3947 fc = atomisp_find_in_fmt_conv(sink->code); 3948 if (!fc) 3949 return -EINVAL; 3950 input_format = fc->atomisp_in_fmt; 3951 bayer_order = fc->bayer_order; 3952 } 3953 3954 atomisp_css_input_set_format(asd, stream_id, input_format); 3955 atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order); 3956 3957 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(metadata_format); 3958 if (!fc) 3959 return -EINVAL; 3960 3961 input_format = fc->atomisp_in_fmt; 3962 mipi_port = atomisp_port_to_mipi_port(isp, input->port); 3963 atomisp_css_input_configure_port(asd, mipi_port, 3964 isp->sensor_lanes[mipi_port], 3965 0xffff4, mipi_freq, 3966 input_format, 3967 metadata_width, metadata_height); 3968 return 0; 3969 } 3970 3971 static int configure_pp_input_nop(struct atomisp_sub_device *asd, 3972 unsigned int width, unsigned int height) 3973 { 3974 return 0; 3975 } 3976 3977 static int configure_output_nop(struct atomisp_sub_device *asd, 3978 unsigned int width, unsigned int height, 3979 unsigned int min_width, 3980 enum ia_css_frame_format sh_fmt) 3981 { 3982 return 0; 3983 } 3984 3985 static int get_frame_info_nop(struct atomisp_sub_device *asd, 3986 struct ia_css_frame_info *finfo) 3987 { 3988 return 0; 3989 } 3990 3991 /* 3992 * Resets CSS parameters that depend on input resolution. 3993 * 3994 * Update params like CSS RAW binning, 2ppc mode and pp_input 3995 * which depend on input size, but are not automatically 3996 * handled in CSS when the input resolution is changed. 3997 */ 3998 static int css_input_resolution_changed(struct atomisp_sub_device *asd, 3999 struct v4l2_mbus_framefmt *ffmt) 4000 { 4001 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf; 4002 unsigned int i; 4003 4004 dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n", 4005 ffmt->width, ffmt->height); 4006 4007 if (IS_ISP2401) 4008 atomisp_css_input_set_two_pixels_per_clock(asd, false); 4009 else 4010 atomisp_css_input_set_two_pixels_per_clock(asd, true); 4011 4012 /* 4013 * If sensor input changed, which means metadata resolution changed 4014 * together. Release all metadata buffers here to let it re-allocated 4015 * next time in reqbufs. 4016 */ 4017 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) { 4018 list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i], 4019 list) { 4020 atomisp_css_free_metadata_buffer(md_buf); 4021 list_del(&md_buf->list); 4022 kfree(md_buf); 4023 } 4024 } 4025 return 0; 4026 4027 /* 4028 * TODO: atomisp_css_preview_configure_pp_input() not 4029 * reset due to CSS bug tracked as PSI BZ 115124 4030 */ 4031 } 4032 4033 static int atomisp_set_fmt_to_isp(struct video_device *vdev, 4034 struct ia_css_frame_info *output_info, 4035 const struct v4l2_pix_format *pix) 4036 { 4037 struct camera_mipi_info *mipi_info; 4038 struct atomisp_device *isp = video_get_drvdata(vdev); 4039 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd; 4040 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr]; 4041 const struct atomisp_format_bridge *format; 4042 struct v4l2_rect *isp_sink_crop; 4043 enum ia_css_pipe_id pipe_id; 4044 int (*configure_output)(struct atomisp_sub_device *asd, 4045 unsigned int width, unsigned int height, 4046 unsigned int min_width, 4047 enum ia_css_frame_format sh_fmt) = 4048 configure_output_nop; 4049 int (*get_frame_info)(struct atomisp_sub_device *asd, 4050 struct ia_css_frame_info *finfo) = 4051 get_frame_info_nop; 4052 int (*configure_pp_input)(struct atomisp_sub_device *asd, 4053 unsigned int width, unsigned int height) = 4054 configure_pp_input_nop; 4055 const struct atomisp_in_fmt_conv *fc = NULL; 4056 int ret, i; 4057 4058 isp_sink_crop = atomisp_subdev_get_rect( 4059 &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE, 4060 ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP); 4061 4062 format = atomisp_get_format_bridge(pix->pixelformat); 4063 if (!format) 4064 return -EINVAL; 4065 4066 if (input->type != TEST_PATTERN) { 4067 mipi_info = atomisp_to_sensor_mipi_info(input->camera); 4068 4069 if (atomisp_set_sensor_mipi_to_isp(asd, ATOMISP_INPUT_STREAM_GENERAL, 4070 mipi_info)) 4071 return -EINVAL; 4072 4073 if (mipi_info) 4074 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(mipi_info->input_format); 4075 4076 if (!fc) 4077 fc = atomisp_find_in_fmt_conv( 4078 atomisp_subdev_get_ffmt(&asd->subdev, 4079 NULL, V4L2_SUBDEV_FORMAT_ACTIVE, 4080 ATOMISP_SUBDEV_PAD_SINK)->code); 4081 if (!fc) 4082 return -EINVAL; 4083 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW && 4084 raw_output_format_match_input(fc->atomisp_in_fmt, 4085 pix->pixelformat)) 4086 return -EINVAL; 4087 } 4088 4089 /* 4090 * Configure viewfinder also when vfpp is disabled: the 4091 * CSS still requires viewfinder configuration. 4092 */ 4093 { 4094 u32 width, height; 4095 4096 if (pix->width < 640 || pix->height < 480) { 4097 width = pix->width; 4098 height = pix->height; 4099 } else { 4100 width = 640; 4101 height = 480; 4102 } 4103 4104 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO || 4105 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) { 4106 atomisp_css_video_configure_viewfinder(asd, width, height, 0, 4107 IA_CSS_FRAME_FORMAT_NV12); 4108 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_STILL_CAPTURE || 4109 asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) { 4110 atomisp_css_capture_configure_viewfinder(asd, width, height, 0, 4111 IA_CSS_FRAME_FORMAT_NV12); 4112 } 4113 } 4114 4115 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR); 4116 4117 for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) 4118 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipe_extra_configs[i].disable_vf_pp = asd->vfpp->val != ATOMISP_VFPP_ENABLE; 4119 4120 /* ISP2401 new input system need to use copy pipe */ 4121 if (asd->copy_mode) { 4122 pipe_id = IA_CSS_PIPE_ID_COPY; 4123 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL, false); 4124 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) { 4125 /* video same in continuouscapture and online modes */ 4126 configure_output = atomisp_css_video_configure_output; 4127 get_frame_info = atomisp_css_video_get_output_frame_info; 4128 pipe_id = IA_CSS_PIPE_ID_VIDEO; 4129 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) { 4130 configure_output = atomisp_css_video_configure_output; 4131 get_frame_info = atomisp_css_video_get_output_frame_info; 4132 pipe_id = IA_CSS_PIPE_ID_VIDEO; 4133 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) { 4134 configure_output = atomisp_css_preview_configure_output; 4135 get_frame_info = atomisp_css_preview_get_output_frame_info; 4136 configure_pp_input = atomisp_css_preview_configure_pp_input; 4137 pipe_id = IA_CSS_PIPE_ID_PREVIEW; 4138 } else { 4139 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) { 4140 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW); 4141 atomisp_css_enable_dz(asd, false); 4142 } else { 4143 atomisp_update_capture_mode(asd); 4144 } 4145 4146 /* in case of ANR, force capture pipe to offline mode */ 4147 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL, 4148 !asd->params.low_light); 4149 4150 configure_output = atomisp_css_capture_configure_output; 4151 get_frame_info = atomisp_css_capture_get_output_frame_info; 4152 configure_pp_input = atomisp_css_capture_configure_pp_input; 4153 pipe_id = IA_CSS_PIPE_ID_CAPTURE; 4154 4155 if (asd->run_mode->val != ATOMISP_RUN_MODE_STILL_CAPTURE) { 4156 dev_err(isp->dev, 4157 "Need to set the running mode first\n"); 4158 asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE; 4159 } 4160 } 4161 4162 if (asd->copy_mode) 4163 ret = atomisp_css_copy_configure_output(asd, ATOMISP_INPUT_STREAM_GENERAL, 4164 pix->width, pix->height, 4165 format->planar ? pix->bytesperline : 4166 pix->bytesperline * 8 / format->depth, 4167 format->sh_fmt); 4168 else 4169 ret = configure_output(asd, pix->width, pix->height, 4170 format->planar ? pix->bytesperline : 4171 pix->bytesperline * 8 / format->depth, 4172 format->sh_fmt); 4173 if (ret) { 4174 dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n", 4175 pix->width, pix->height, format->sh_fmt); 4176 return -EINVAL; 4177 } 4178 4179 ret = configure_pp_input(asd, isp_sink_crop->width, isp_sink_crop->height); 4180 if (ret) { 4181 dev_err(isp->dev, "configure_pp_input %ux%u\n", 4182 isp_sink_crop->width, 4183 isp_sink_crop->height); 4184 return -EINVAL; 4185 } 4186 if (asd->copy_mode) 4187 ret = atomisp_css_copy_get_output_frame_info(asd, 4188 ATOMISP_INPUT_STREAM_GENERAL, 4189 output_info); 4190 else 4191 ret = get_frame_info(asd, output_info); 4192 if (ret) { 4193 dev_err(isp->dev, "__get_frame_info %ux%u (padded to %u) returned %d\n", 4194 pix->width, pix->height, pix->bytesperline, ret); 4195 return ret; 4196 } 4197 4198 atomisp_update_grid_info(asd, pipe_id); 4199 return 0; 4200 } 4201 4202 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd, 4203 unsigned int width, unsigned int height, 4204 unsigned int *dvs_env_w, unsigned int *dvs_env_h) 4205 { 4206 if (asd->params.video_dis_en && 4207 asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) { 4208 /* envelope is 20% of the output resolution */ 4209 /* 4210 * dvs envelope cannot be round up. 4211 * it would cause ISP timeout and color switch issue 4212 */ 4213 *dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH); 4214 *dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT); 4215 } 4216 4217 asd->params.dis_proj_data_valid = false; 4218 asd->params.css_update_params_needed = true; 4219 } 4220 4221 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd, 4222 const struct v4l2_pix_format *f) 4223 { 4224 struct v4l2_mbus_framefmt *sink, *src; 4225 4226 if (!IS_ISP2401) { 4227 /* Only used for the new input system */ 4228 asd->copy_mode = false; 4229 return; 4230 } 4231 4232 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL, 4233 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK); 4234 src = atomisp_subdev_get_ffmt(&asd->subdev, NULL, 4235 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SOURCE); 4236 4237 if (sink->code == src->code && sink->width == f->width && sink->height == f->height) 4238 asd->copy_mode = true; 4239 else 4240 asd->copy_mode = false; 4241 4242 dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode); 4243 } 4244 4245 static int atomisp_set_fmt_to_snr(struct video_device *vdev, const struct v4l2_pix_format *f, 4246 unsigned int dvs_env_w, unsigned int dvs_env_h) 4247 { 4248 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev); 4249 struct atomisp_sub_device *asd = pipe->asd; 4250 struct atomisp_device *isp = asd->isp; 4251 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr]; 4252 const struct atomisp_format_bridge *format; 4253 struct v4l2_subdev_state pad_state = { 4254 .pads = &input->pad_cfg, 4255 }; 4256 struct v4l2_subdev_format vformat = { 4257 .which = V4L2_SUBDEV_FORMAT_TRY, 4258 }; 4259 struct v4l2_mbus_framefmt *ffmt = &vformat.format; 4260 struct v4l2_mbus_framefmt *req_ffmt; 4261 struct atomisp_input_stream_info *stream_info = 4262 (struct atomisp_input_stream_info *)ffmt->reserved; 4263 int ret; 4264 4265 format = atomisp_get_format_bridge(f->pixelformat); 4266 if (!format) 4267 return -EINVAL; 4268 4269 v4l2_fill_mbus_format(ffmt, f, format->mbus_code); 4270 ffmt->height += asd->sink_pad_padding_h + dvs_env_h; 4271 ffmt->width += asd->sink_pad_padding_w + dvs_env_w; 4272 4273 dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n", 4274 ffmt->width, ffmt->height, asd->sink_pad_padding_w, asd->sink_pad_padding_h, 4275 dvs_env_w, dvs_env_h); 4276 4277 __atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL, stream_info); 4278 4279 req_ffmt = ffmt; 4280 4281 /* Disable dvs if resolution can't be supported by sensor */ 4282 if (asd->params.video_dis_en && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) { 4283 ret = atomisp_set_crop(isp, &vformat.format, V4L2_SUBDEV_FORMAT_TRY); 4284 if (ret) 4285 return ret; 4286 4287 vformat.which = V4L2_SUBDEV_FORMAT_TRY; 4288 ret = v4l2_subdev_call(input->camera, pad, set_fmt, &pad_state, &vformat); 4289 if (ret) 4290 return ret; 4291 4292 dev_dbg(isp->dev, "video dis: sensor width: %d, height: %d\n", 4293 ffmt->width, ffmt->height); 4294 4295 if (ffmt->width < req_ffmt->width || 4296 ffmt->height < req_ffmt->height) { 4297 req_ffmt->height -= dvs_env_h; 4298 req_ffmt->width -= dvs_env_w; 4299 ffmt = req_ffmt; 4300 dev_warn(isp->dev, 4301 "can not enable video dis due to sensor limitation."); 4302 asd->params.video_dis_en = false; 4303 } 4304 } 4305 4306 ret = atomisp_set_crop(isp, &vformat.format, V4L2_SUBDEV_FORMAT_ACTIVE); 4307 if (ret) 4308 return ret; 4309 4310 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE; 4311 ret = v4l2_subdev_call(input->camera, pad, set_fmt, NULL, &vformat); 4312 if (ret) 4313 return ret; 4314 4315 __atomisp_update_stream_env(asd, ATOMISP_INPUT_STREAM_GENERAL, stream_info); 4316 4317 dev_dbg(isp->dev, "sensor width: %d, height: %d\n", 4318 ffmt->width, ffmt->height); 4319 4320 if (ffmt->width < ATOM_ISP_STEP_WIDTH || 4321 ffmt->height < ATOM_ISP_STEP_HEIGHT) 4322 return -EINVAL; 4323 4324 if (asd->params.video_dis_en && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO && 4325 (ffmt->width < req_ffmt->width || ffmt->height < req_ffmt->height)) { 4326 dev_warn(isp->dev, 4327 "can not enable video dis due to sensor limitation."); 4328 asd->params.video_dis_en = false; 4329 } 4330 4331 atomisp_subdev_set_ffmt(&asd->subdev, NULL, 4332 V4L2_SUBDEV_FORMAT_ACTIVE, 4333 ATOMISP_SUBDEV_PAD_SINK, ffmt); 4334 4335 return css_input_resolution_changed(asd, ffmt); 4336 } 4337 4338 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f) 4339 { 4340 struct atomisp_device *isp = video_get_drvdata(vdev); 4341 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev); 4342 struct atomisp_sub_device *asd = pipe->asd; 4343 const struct atomisp_format_bridge *format_bridge; 4344 const struct atomisp_format_bridge *snr_format_bridge; 4345 struct ia_css_frame_info output_info; 4346 unsigned int dvs_env_w = 0, dvs_env_h = 0; 4347 struct v4l2_mbus_framefmt isp_source_fmt = {0}; 4348 struct v4l2_rect isp_sink_crop; 4349 int ret; 4350 4351 ret = atomisp_pipe_check(pipe, true); 4352 if (ret) 4353 return ret; 4354 4355 dev_dbg(isp->dev, 4356 "setting resolution %ux%u bytesperline %u\n", 4357 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.bytesperline); 4358 4359 /* Ensure that the resolution is equal or below the maximum supported */ 4360 ret = atomisp_try_fmt(isp, &f->fmt.pix, &format_bridge, &snr_format_bridge); 4361 if (ret) 4362 return ret; 4363 4364 pipe->sh_fmt = format_bridge->sh_fmt; 4365 pipe->pix.pixelformat = format_bridge->pixelformat; 4366 4367 atomisp_subdev_get_ffmt(&asd->subdev, NULL, 4368 V4L2_SUBDEV_FORMAT_ACTIVE, 4369 ATOMISP_SUBDEV_PAD_SINK)->code = 4370 snr_format_bridge->mbus_code; 4371 4372 isp_source_fmt.code = format_bridge->mbus_code; 4373 atomisp_subdev_set_ffmt(&asd->subdev, NULL, 4374 V4L2_SUBDEV_FORMAT_ACTIVE, 4375 ATOMISP_SUBDEV_PAD_SOURCE, &isp_source_fmt); 4376 4377 if (atomisp_subdev_format_conversion(asd)) { 4378 atomisp_get_padding(isp, f->fmt.pix.width, f->fmt.pix.height, 4379 &asd->sink_pad_padding_w, &asd->sink_pad_padding_h); 4380 } else { 4381 asd->sink_pad_padding_w = 0; 4382 asd->sink_pad_padding_h = 0; 4383 } 4384 4385 atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height, 4386 &dvs_env_w, &dvs_env_h); 4387 4388 ret = atomisp_set_fmt_to_snr(vdev, &f->fmt.pix, dvs_env_w, dvs_env_h); 4389 if (ret) { 4390 dev_warn(isp->dev, 4391 "Set format to sensor failed with %d\n", ret); 4392 return -EINVAL; 4393 } 4394 4395 atomisp_csi_lane_config(isp); 4396 4397 atomisp_check_copy_mode(asd, &f->fmt.pix); 4398 4399 isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL, 4400 V4L2_SUBDEV_FORMAT_ACTIVE, 4401 ATOMISP_SUBDEV_PAD_SINK, 4402 V4L2_SEL_TGT_CROP); 4403 4404 /* Try to enable YUV downscaling if ISP input is 10 % (either 4405 * width or height) bigger than the desired result. */ 4406 if (!IS_MOFD || 4407 isp_sink_crop.width * 9 / 10 < f->fmt.pix.width || 4408 isp_sink_crop.height * 9 / 10 < f->fmt.pix.height || 4409 (atomisp_subdev_format_conversion(asd) && 4410 (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO || 4411 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) { 4412 isp_sink_crop.width = f->fmt.pix.width; 4413 isp_sink_crop.height = f->fmt.pix.height; 4414 4415 atomisp_subdev_set_selection(&asd->subdev, NULL, 4416 V4L2_SUBDEV_FORMAT_ACTIVE, 4417 ATOMISP_SUBDEV_PAD_SOURCE, V4L2_SEL_TGT_COMPOSE, 4418 0, &isp_sink_crop); 4419 } else { 4420 struct v4l2_rect main_compose = {0}; 4421 4422 main_compose.width = isp_sink_crop.width; 4423 main_compose.height = 4424 DIV_ROUND_UP(main_compose.width * f->fmt.pix.height, 4425 f->fmt.pix.width); 4426 if (main_compose.height > isp_sink_crop.height) { 4427 main_compose.height = isp_sink_crop.height; 4428 main_compose.width = 4429 DIV_ROUND_UP(main_compose.height * 4430 f->fmt.pix.width, 4431 f->fmt.pix.height); 4432 } 4433 4434 atomisp_subdev_set_selection(&asd->subdev, NULL, 4435 V4L2_SUBDEV_FORMAT_ACTIVE, 4436 ATOMISP_SUBDEV_PAD_SOURCE, 4437 V4L2_SEL_TGT_COMPOSE, 0, 4438 &main_compose); 4439 } 4440 4441 ret = atomisp_set_fmt_to_isp(vdev, &output_info, &f->fmt.pix); 4442 if (ret) { 4443 dev_warn(isp->dev, "Can't set format on ISP. Error %d\n", ret); 4444 return -EINVAL; 4445 } 4446 4447 atomisp_fill_pix_format(&pipe->pix, f->fmt.pix.width, f->fmt.pix.height, format_bridge); 4448 4449 f->fmt.pix = pipe->pix; 4450 f->fmt.pix.priv = PAGE_ALIGN(pipe->pix.width * 4451 pipe->pix.height * 2); 4452 4453 dev_dbg(isp->dev, "%s: %dx%d, image size: %d, %d bytes per line\n", 4454 __func__, 4455 f->fmt.pix.width, f->fmt.pix.height, 4456 f->fmt.pix.sizeimage, f->fmt.pix.bytesperline); 4457 4458 return 0; 4459 } 4460 4461 int atomisp_set_shading_table(struct atomisp_sub_device *asd, 4462 struct atomisp_shading_table *user_shading_table) 4463 { 4464 struct ia_css_shading_table *shading_table; 4465 struct ia_css_shading_table *free_table; 4466 unsigned int len_table; 4467 int i; 4468 int ret = 0; 4469 4470 if (!user_shading_table) 4471 return -EINVAL; 4472 4473 if (!user_shading_table->enable) { 4474 asd->params.config.shading_table = NULL; 4475 asd->params.sc_en = false; 4476 return 0; 4477 } 4478 4479 /* If enabling, all tables must be set */ 4480 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) { 4481 if (!user_shading_table->data[i]) 4482 return -EINVAL; 4483 } 4484 4485 /* Shading table size per color */ 4486 if (user_shading_table->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR || 4487 user_shading_table->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) 4488 return -EINVAL; 4489 4490 shading_table = atomisp_css_shading_table_alloc( 4491 user_shading_table->width, user_shading_table->height); 4492 if (!shading_table) 4493 return -ENOMEM; 4494 4495 len_table = user_shading_table->width * user_shading_table->height * 4496 ATOMISP_SC_TYPE_SIZE; 4497 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) { 4498 ret = copy_from_user(shading_table->data[i], 4499 (void __user *)user_shading_table->data[i], 4500 len_table); 4501 if (ret) { 4502 free_table = shading_table; 4503 ret = -EFAULT; 4504 goto out; 4505 } 4506 } 4507 shading_table->sensor_width = user_shading_table->sensor_width; 4508 shading_table->sensor_height = user_shading_table->sensor_height; 4509 shading_table->fraction_bits = user_shading_table->fraction_bits; 4510 4511 free_table = asd->params.css_param.shading_table; 4512 asd->params.css_param.shading_table = shading_table; 4513 asd->params.config.shading_table = shading_table; 4514 asd->params.sc_en = true; 4515 4516 out: 4517 if (free_table) 4518 atomisp_css_shading_table_free(free_table); 4519 4520 return ret; 4521 } 4522 4523 int atomisp_flash_enable(struct atomisp_sub_device *asd, int num_frames) 4524 { 4525 struct atomisp_device *isp = asd->isp; 4526 4527 if (num_frames < 0) { 4528 dev_dbg(isp->dev, "%s ERROR: num_frames: %d\n", __func__, 4529 num_frames); 4530 return -EINVAL; 4531 } 4532 /* a requested flash is still in progress. */ 4533 if (num_frames && asd->params.flash_state != ATOMISP_FLASH_IDLE) { 4534 dev_dbg(isp->dev, "%s flash busy: %d frames left: %d\n", 4535 __func__, asd->params.flash_state, 4536 asd->params.num_flash_frames); 4537 return -EBUSY; 4538 } 4539 4540 asd->params.num_flash_frames = num_frames; 4541 asd->params.flash_state = ATOMISP_FLASH_REQUESTED; 4542 return 0; 4543 } 4544 4545 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id) 4546 { 4547 struct atomisp_device *isp = asd->isp; 4548 4549 if (!asd->enable_raw_buffer_lock->val) { 4550 dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__); 4551 return -EINVAL; 4552 } 4553 if (!asd->streaming) { 4554 dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n", 4555 __func__, exp_id, asd->streaming); 4556 return -EINVAL; 4557 } 4558 if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) { 4559 dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id); 4560 return -EINVAL; 4561 } 4562 return 0; 4563 } 4564 4565 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd) 4566 { 4567 unsigned long flags; 4568 4569 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags); 4570 memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap)); 4571 asd->raw_buffer_locked_count = 0; 4572 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags); 4573 } 4574 4575 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id) 4576 { 4577 int *bitmap, bit; 4578 unsigned long flags; 4579 int ret; 4580 4581 if (__checking_exp_id(asd, exp_id)) 4582 return -EINVAL; 4583 4584 bitmap = asd->raw_buffer_bitmap + exp_id / 32; 4585 bit = exp_id % 32; 4586 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags); 4587 ret = ((*bitmap) & (1 << bit)); 4588 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags); 4589 return !ret; 4590 } 4591 4592 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id) 4593 { 4594 int *bitmap, bit; 4595 unsigned long flags; 4596 4597 if (__is_raw_buffer_locked(asd, exp_id)) 4598 return -EINVAL; 4599 4600 bitmap = asd->raw_buffer_bitmap + exp_id / 32; 4601 bit = exp_id % 32; 4602 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags); 4603 (*bitmap) &= ~(1 << bit); 4604 asd->raw_buffer_locked_count--; 4605 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags); 4606 4607 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n", 4608 __func__, exp_id, asd->raw_buffer_locked_count); 4609 return 0; 4610 } 4611 4612 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id) 4613 { 4614 struct atomisp_device *isp = asd->isp; 4615 int value = *exp_id; 4616 int ret; 4617 4618 lockdep_assert_held(&isp->mutex); 4619 4620 ret = __is_raw_buffer_locked(asd, value); 4621 if (ret) { 4622 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret); 4623 return -EINVAL; 4624 } 4625 4626 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value); 4627 ret = atomisp_css_exp_id_capture(asd, value); 4628 if (ret) { 4629 dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value); 4630 return -EIO; 4631 } 4632 return 0; 4633 } 4634 4635 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id) 4636 { 4637 struct atomisp_device *isp = asd->isp; 4638 int value = *exp_id; 4639 int ret; 4640 4641 lockdep_assert_held(&isp->mutex); 4642 4643 ret = __clear_raw_buffer_bitmap(asd, value); 4644 if (ret) { 4645 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret); 4646 return -EINVAL; 4647 } 4648 4649 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value); 4650 ret = atomisp_css_exp_id_unlock(asd, value); 4651 if (ret) 4652 dev_err(isp->dev, "%s exp_id %d failed, err %d.\n", 4653 __func__, value, ret); 4654 4655 return ret; 4656 } 4657 4658 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd, 4659 unsigned int *enable) 4660 { 4661 bool value; 4662 4663 if (!enable) 4664 return -EINVAL; 4665 4666 value = *enable > 0; 4667 4668 atomisp_en_dz_capt_pipe(asd, value); 4669 4670 return 0; 4671 } 4672 4673 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event) 4674 { 4675 if (!event || !asd->streaming) 4676 return -EINVAL; 4677 4678 lockdep_assert_held(&asd->isp->mutex); 4679 4680 dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n", 4681 __func__, *event); 4682 4683 switch (*event) { 4684 case V4L2_EVENT_FRAME_SYNC: 4685 atomisp_sof_event(asd); 4686 break; 4687 case V4L2_EVENT_FRAME_END: 4688 atomisp_eof_event(asd, 0); 4689 break; 4690 case V4L2_EVENT_ATOMISP_3A_STATS_READY: 4691 atomisp_3a_stats_ready_event(asd, 0); 4692 break; 4693 case V4L2_EVENT_ATOMISP_METADATA_READY: 4694 atomisp_metadata_ready_event(asd, 0); 4695 break; 4696 default: 4697 return -EINVAL; 4698 } 4699 4700 return 0; 4701 } 4702