2 * Support for Medifield PNW Camera Imaging ISP subsystem.
4 * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
6 * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/firmware.h>
20 #include <linux/pci.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/kfifo.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/timer.h>
27 #include <asm/iosf_mbi.h>
29 #include <media/v4l2-event.h>
30 #include <media/videobuf-vmalloc.h>
32 #define CREATE_TRACE_POINTS
33 #include "atomisp_trace_event.h"
35 #include "atomisp_cmd.h"
36 #include "atomisp_common.h"
37 #include "atomisp_fops.h"
38 #include "atomisp_internal.h"
39 #include "atomisp_ioctl.h"
40 #include "atomisp-regs.h"
41 #include "atomisp_tables.h"
42 #include "atomisp_acc.h"
43 #include "atomisp_compat.h"
44 #include "atomisp_subdev.h"
45 #include "atomisp_dfs_tables.h"
47 #include "hrt/hive_isp_css_mm_hrt.h"
49 #include "sh_css_hrt.h"
50 #include "sh_css_defs.h"
51 #include "system_global.h"
52 #include "sh_css_internal.h"
53 #include "sh_css_sp.h"
54 #include "gp_device.h"
55 #include "device_access.h"
58 #include "ia_css_types.h"
59 #include "ia_css_stream.h"
60 #include "error_support.h"
63 /* We should never need to run the flash for more than 2 frames.
64 * At 15fps this means 133ms. We set the timeout a bit longer.
65 * Each flash driver is supposed to set its own timeout, but
66 * just in case someone else changed the timeout, we set it
67 * here to make sure we don't damage the flash hardware. */
68 #define FLASH_TIMEOUT 800 /* ms */
73 void __user *user_ptr;
82 * get sensor:dis71430/ov2720 related info from v4l2_subdev->priv data field.
83 * subdev->priv is set in mrst.c
85 struct camera_mipi_info *atomisp_to_sensor_mipi_info(struct v4l2_subdev *sd)
87 return (struct camera_mipi_info *)v4l2_get_subdev_hostdata(sd);
91 * get struct atomisp_video_pipe from v4l2 video_device
93 struct atomisp_video_pipe *atomisp_to_video_pipe(struct video_device *dev)
95 return (struct atomisp_video_pipe *)
96 container_of(dev, struct atomisp_video_pipe, vdev);
100 * get struct atomisp_acc_pipe from v4l2 video_device
102 struct atomisp_acc_pipe *atomisp_to_acc_pipe(struct video_device *dev)
104 return (struct atomisp_acc_pipe *)
105 container_of(dev, struct atomisp_acc_pipe, vdev);
108 static unsigned short atomisp_get_sensor_fps(struct atomisp_sub_device *asd)
110 struct v4l2_subdev_frame_interval fi;
111 struct atomisp_device *isp = asd->isp;
113 unsigned short fps = 0;
116 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
117 video, g_frame_interval, &fi);
119 if (!ret && fi.interval.numerator)
120 fps = fi.interval.denominator / fi.interval.numerator;
126 * DFS progress is shown as follows:
127 * 1. Target frequency is calculated according to FPS/Resolution/ISP running
129 * 2. Ratio is calculated using formula: 2 * HPLL / target frequency - 1
130 * with proper rounding.
131 * 3. Set ratio to ISPFREQ40, 1 to FREQVALID and ISPFREQGUAR40
132 * to 200MHz in ISPSSPM1.
133 * 4. Wait for FREQVALID to be cleared by P-Unit.
134 * 5. Wait for field ISPFREQSTAT40 in ISPSSPM1 turn to ratio set in 3.
136 static int write_target_freq_to_hw(struct atomisp_device *isp,
137 unsigned int new_freq)
139 unsigned int ratio, timeout, guar_ratio;
143 if (!isp->hpll_freq) {
144 dev_err(isp->dev, "failed to get hpll_freq. no change to freq\n");
148 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
149 if (isp_sspm1 & ISP_FREQ_VALID_MASK) {
150 dev_dbg(isp->dev, "clearing ISPSSPM1 valid bit.\n");
151 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
152 isp_sspm1 & ~(1 << ISP_FREQ_VALID_OFFSET));
155 ratio = (2 * isp->hpll_freq + new_freq / 2) / new_freq - 1;
156 guar_ratio = (2 * isp->hpll_freq + 200 / 2) / 200 - 1;
158 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
159 isp_sspm1 &= ~(0x1F << ISP_REQ_FREQ_OFFSET);
161 for (i = 0; i < ISP_DFS_TRY_TIMES; i++) {
162 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
164 | ratio << ISP_REQ_FREQ_OFFSET
165 | 1 << ISP_FREQ_VALID_OFFSET
166 | guar_ratio << ISP_REQ_GUAR_FREQ_OFFSET);
168 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
170 while ((isp_sspm1 & ISP_FREQ_VALID_MASK) && timeout) {
171 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
172 dev_dbg(isp->dev, "waiting for ISPSSPM1 valid bit to be 0.\n");
182 dev_err(isp->dev, "DFS failed due to HW error.\n");
186 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
188 while (((isp_sspm1 >> ISP_FREQ_STAT_OFFSET) != ratio) && timeout) {
189 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
190 dev_dbg(isp->dev, "waiting for ISPSSPM1 status bit to be 0x%x.\n",
196 dev_err(isp->dev, "DFS target freq is rejected by HW.\n");
203 int atomisp_freq_scaling(struct atomisp_device *isp,
204 enum atomisp_dfs_mode mode,
207 /* FIXME! Only use subdev[0] status yet */
208 struct atomisp_sub_device *asd = &isp->asd[0];
209 const struct atomisp_dfs_config *dfs;
210 unsigned int new_freq;
211 struct atomisp_freq_scaling_rule curr_rules;
213 unsigned short fps = 0;
215 if (isp->sw_contex.power_state != ATOM_ISP_POWER_UP) {
216 dev_err(isp->dev, "DFS cannot proceed due to no power.\n");
220 if ((isp->pdev->device & ATOMISP_PCI_DEVICE_SOC_MASK) ==
221 ATOMISP_PCI_DEVICE_SOC_CHT && ATOMISP_USE_YUVPP(asd))
222 isp->dfs = &dfs_config_cht_soc;
226 if (dfs->lowest_freq == 0 || dfs->max_freq_at_vmin == 0 ||
227 dfs->highest_freq == 0 || dfs->dfs_table_size == 0 ||
229 dev_err(isp->dev, "DFS configuration is invalid.\n");
233 if (mode == ATOMISP_DFS_MODE_LOW) {
234 new_freq = dfs->lowest_freq;
238 if (mode == ATOMISP_DFS_MODE_MAX) {
239 new_freq = dfs->highest_freq;
243 fps = atomisp_get_sensor_fps(asd);
247 curr_rules.width = asd->fmt[asd->capture_pad].fmt.width;
248 curr_rules.height = asd->fmt[asd->capture_pad].fmt.height;
249 curr_rules.fps = fps;
250 curr_rules.run_mode = asd->run_mode->val;
252 * For continuous mode, we need to make the capture setting applied
253 * since preview mode, because there is no chance to do this when
254 * starting image capture.
256 if (asd->continuous_mode->val) {
257 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
258 curr_rules.run_mode = ATOMISP_RUN_MODE_SDV;
260 curr_rules.run_mode =
261 ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE;
264 /* search for the target frequency by looping freq rules*/
265 for (i = 0; i < dfs->dfs_table_size; i++) {
266 if (curr_rules.width != dfs->dfs_table[i].width &&
267 dfs->dfs_table[i].width != ISP_FREQ_RULE_ANY)
269 if (curr_rules.height != dfs->dfs_table[i].height &&
270 dfs->dfs_table[i].height != ISP_FREQ_RULE_ANY)
272 if (curr_rules.fps != dfs->dfs_table[i].fps &&
273 dfs->dfs_table[i].fps != ISP_FREQ_RULE_ANY)
275 if (curr_rules.run_mode != dfs->dfs_table[i].run_mode &&
276 dfs->dfs_table[i].run_mode != ISP_FREQ_RULE_ANY)
281 if (i == dfs->dfs_table_size)
282 new_freq = dfs->max_freq_at_vmin;
284 new_freq = dfs->dfs_table[i].isp_freq;
287 dev_dbg(isp->dev, "DFS target frequency=%d.\n", new_freq);
289 if ((new_freq == isp->sw_contex.running_freq) && !force)
292 dev_dbg(isp->dev, "Programming DFS frequency to %d\n", new_freq);
294 ret = write_target_freq_to_hw(isp, new_freq);
296 isp->sw_contex.running_freq = new_freq;
297 trace_ipu_pstate(new_freq, -1);
303 * reset and restore ISP
305 int atomisp_reset(struct atomisp_device *isp)
307 /* Reset ISP by power-cycling it */
310 dev_dbg(isp->dev, "%s\n", __func__);
311 atomisp_css_suspend(isp);
312 ret = atomisp_runtime_suspend(isp->dev);
314 dev_err(isp->dev, "atomisp_runtime_suspend failed, %d\n", ret);
315 ret = atomisp_mrfld_power_down(isp);
317 dev_err(isp->dev, "can not disable ISP power\n");
319 ret = atomisp_mrfld_power_up(isp);
321 dev_err(isp->dev, "can not enable ISP power\n");
322 ret = atomisp_runtime_resume(isp->dev);
324 dev_err(isp->dev, "atomisp_runtime_resume failed, %d\n", ret);
326 ret = atomisp_css_resume(isp);
328 isp->isp_fatal_error = true;
334 * interrupt disable functions
336 static void disable_isp_irq(enum hrt_isp_css_irq irq)
338 irq_disable_channel(IRQ0_ID, irq);
340 if (irq != hrt_isp_css_irq_sp)
343 cnd_sp_irq_enable(SP0_ID, false);
347 * interrupt clean function
349 static void clear_isp_irq(enum hrt_isp_css_irq irq)
351 irq_clear_all(IRQ0_ID);
354 void atomisp_msi_irq_init(struct atomisp_device *isp, struct pci_dev *dev)
359 pci_read_config_dword(dev, PCI_MSI_CAPID, &msg32);
360 msg32 |= 1 << MSI_ENABLE_BIT;
361 pci_write_config_dword(dev, PCI_MSI_CAPID, msg32);
363 msg32 = (1 << INTR_IER) | (1 << INTR_IIR);
364 pci_write_config_dword(dev, PCI_INTERRUPT_CTRL, msg32);
366 pci_read_config_word(dev, PCI_COMMAND, &msg16);
367 msg16 |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
368 PCI_COMMAND_INTX_DISABLE);
369 pci_write_config_word(dev, PCI_COMMAND, msg16);
372 void atomisp_msi_irq_uninit(struct atomisp_device *isp, struct pci_dev *dev)
377 pci_read_config_dword(dev, PCI_MSI_CAPID, &msg32);
378 msg32 &= ~(1 << MSI_ENABLE_BIT);
379 pci_write_config_dword(dev, PCI_MSI_CAPID, msg32);
382 pci_write_config_dword(dev, PCI_INTERRUPT_CTRL, msg32);
384 pci_read_config_word(dev, PCI_COMMAND, &msg16);
385 msg16 &= ~(PCI_COMMAND_MASTER);
386 pci_write_config_word(dev, PCI_COMMAND, msg16);
389 static void atomisp_sof_event(struct atomisp_sub_device *asd)
391 struct v4l2_event event = {0};
393 event.type = V4L2_EVENT_FRAME_SYNC;
394 event.u.frame_sync.frame_sequence = atomic_read(&asd->sof_count);
396 v4l2_event_queue(asd->subdev.devnode, &event);
399 void atomisp_eof_event(struct atomisp_sub_device *asd, uint8_t exp_id)
401 struct v4l2_event event = {0};
403 event.type = V4L2_EVENT_FRAME_END;
404 event.u.frame_sync.frame_sequence = exp_id;
406 v4l2_event_queue(asd->subdev.devnode, &event);
409 static void atomisp_3a_stats_ready_event(struct atomisp_sub_device *asd,
412 struct v4l2_event event = {0};
414 event.type = V4L2_EVENT_ATOMISP_3A_STATS_READY;
415 event.u.frame_sync.frame_sequence = exp_id;
417 v4l2_event_queue(asd->subdev.devnode, &event);
420 static void atomisp_metadata_ready_event(struct atomisp_sub_device *asd,
421 enum atomisp_metadata_type md_type)
423 struct v4l2_event event = {0};
425 event.type = V4L2_EVENT_ATOMISP_METADATA_READY;
426 event.u.data[0] = md_type;
428 v4l2_event_queue(asd->subdev.devnode, &event);
431 static void atomisp_reset_event(struct atomisp_sub_device *asd)
433 struct v4l2_event event = {0};
435 event.type = V4L2_EVENT_ATOMISP_CSS_RESET;
437 v4l2_event_queue(asd->subdev.devnode, &event);
440 static void print_csi_rx_errors(enum mipi_port_id port,
441 struct atomisp_device *isp)
445 atomisp_css_rx_get_irq_info(port, &infos);
447 dev_err(isp->dev, "CSI Receiver port %d errors:\n", port);
448 if (infos & CSS_RX_IRQ_INFO_BUFFER_OVERRUN)
449 dev_err(isp->dev, " buffer overrun");
450 if (infos & CSS_RX_IRQ_INFO_ERR_SOT)
451 dev_err(isp->dev, " start-of-transmission error");
452 if (infos & CSS_RX_IRQ_INFO_ERR_SOT_SYNC)
453 dev_err(isp->dev, " start-of-transmission sync error");
454 if (infos & CSS_RX_IRQ_INFO_ERR_CONTROL)
455 dev_err(isp->dev, " control error");
456 if (infos & CSS_RX_IRQ_INFO_ERR_ECC_DOUBLE)
457 dev_err(isp->dev, " 2 or more ECC errors");
458 if (infos & CSS_RX_IRQ_INFO_ERR_CRC)
459 dev_err(isp->dev, " CRC mismatch");
460 if (infos & CSS_RX_IRQ_INFO_ERR_UNKNOWN_ID)
461 dev_err(isp->dev, " unknown error");
462 if (infos & CSS_RX_IRQ_INFO_ERR_FRAME_SYNC)
463 dev_err(isp->dev, " frame sync error");
464 if (infos & CSS_RX_IRQ_INFO_ERR_FRAME_DATA)
465 dev_err(isp->dev, " frame data error");
466 if (infos & CSS_RX_IRQ_INFO_ERR_DATA_TIMEOUT)
467 dev_err(isp->dev, " data timeout");
468 if (infos & CSS_RX_IRQ_INFO_ERR_UNKNOWN_ESC)
469 dev_err(isp->dev, " unknown escape command entry");
470 if (infos & CSS_RX_IRQ_INFO_ERR_LINE_SYNC)
471 dev_err(isp->dev, " line sync error");
475 static void clear_irq_reg(struct atomisp_device *isp)
479 pci_read_config_dword(isp->pdev, PCI_INTERRUPT_CTRL, &msg_ret);
480 msg_ret |= 1 << INTR_IIR;
481 pci_write_config_dword(isp->pdev, PCI_INTERRUPT_CTRL, msg_ret);
484 static struct atomisp_sub_device *
485 __get_asd_from_port(struct atomisp_device *isp, enum mipi_port_id port)
489 /* Check which isp subdev to send eof */
490 for (i = 0; i < isp->num_of_streams; i++) {
491 struct atomisp_sub_device *asd = &isp->asd[i];
492 struct camera_mipi_info *mipi_info;
494 mipi_info = atomisp_to_sensor_mipi_info(
495 isp->inputs[asd->input_curr].camera);
497 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED &&
498 __get_mipi_port(isp, mipi_info->port) == port) {
506 /* interrupt handling function*/
507 irqreturn_t atomisp_isr(int irq, void *dev)
509 struct atomisp_device *isp = (struct atomisp_device *)dev;
510 struct atomisp_sub_device *asd;
511 struct atomisp_css_event eof_event;
512 unsigned int irq_infos = 0;
517 spin_lock_irqsave(&isp->lock, flags);
518 if (isp->sw_contex.power_state != ATOM_ISP_POWER_UP ||
519 !isp->css_initialized) {
520 spin_unlock_irqrestore(&isp->lock, flags);
523 err = atomisp_css_irq_translate(isp, &irq_infos);
525 spin_unlock_irqrestore(&isp->lock, flags);
529 dev_dbg(isp->dev, "irq:0x%x\n", irq_infos);
533 if (!atomisp_streaming_count(isp) && !atomisp_is_acc_enabled(isp))
536 for (i = 0; i < isp->num_of_streams; i++) {
539 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
542 * Current SOF only support one stream, so the SOF only valid
543 * either solely one stream is running
545 if (irq_infos & CSS_IRQ_INFO_CSS_RECEIVER_SOF) {
546 atomic_inc(&asd->sof_count);
547 atomisp_sof_event(asd);
549 /* If sequence_temp and sequence are the same
550 * there where no frames lost so we can increase
552 * If not then processing of frame is still in progress
553 * and driver needs to keep old sequence_temp value.
554 * NOTE: There is assumption here that ISP will not
555 * start processing next frame from sensor before old
556 * one is completely done. */
557 if (atomic_read(&asd->sequence) == atomic_read(
558 &asd->sequence_temp))
559 atomic_set(&asd->sequence_temp,
560 atomic_read(&asd->sof_count));
562 if (irq_infos & CSS_IRQ_INFO_EVENTS_READY)
563 atomic_set(&asd->sequence,
564 atomic_read(&asd->sequence_temp));
567 if (irq_infos & CSS_IRQ_INFO_CSS_RECEIVER_SOF)
568 irq_infos &= ~CSS_IRQ_INFO_CSS_RECEIVER_SOF;
570 if ((irq_infos & CSS_IRQ_INFO_INPUT_SYSTEM_ERROR) ||
571 (irq_infos & CSS_IRQ_INFO_IF_ERROR)) {
572 /* handle mipi receiver error */
574 enum mipi_port_id port;
576 for (port = MIPI_PORT0_ID; port <= MIPI_PORT2_ID;
578 print_csi_rx_errors(port, isp);
579 atomisp_css_rx_get_irq_info(port, &rx_infos);
580 atomisp_css_rx_clear_irq_info(port, rx_infos);
584 if (irq_infos & IA_CSS_IRQ_INFO_ISYS_EVENTS_READY) {
585 while (ia_css_dequeue_isys_event(&eof_event.event) ==
587 /* EOF Event does not have the css_pipe returned */
588 asd = __get_asd_from_port(isp, eof_event.event.port);
590 dev_err(isp->dev, "%s:no subdev.event:%d", __func__,
591 eof_event.event.type);
595 atomisp_eof_event(asd, eof_event.event.exp_id);
596 dev_dbg(isp->dev, "%s EOF exp_id %d, asd %d\n",
597 __func__, eof_event.event.exp_id, asd->index);
600 irq_infos &= ~IA_CSS_IRQ_INFO_ISYS_EVENTS_READY;
605 spin_unlock_irqrestore(&isp->lock, flags);
607 return IRQ_WAKE_THREAD;
610 spin_unlock_irqrestore(&isp->lock, flags);
615 void atomisp_clear_css_buffer_counters(struct atomisp_sub_device *asd)
619 memset(asd->s3a_bufs_in_css, 0, sizeof(asd->s3a_bufs_in_css));
620 for (i = 0; i < ATOMISP_INPUT_STREAM_NUM; i++)
621 memset(asd->metadata_bufs_in_css[i], 0,
622 sizeof(asd->metadata_bufs_in_css[i]));
623 asd->dis_bufs_in_css = 0;
624 asd->video_out_capture.buffers_in_css = 0;
625 asd->video_out_vf.buffers_in_css = 0;
626 asd->video_out_preview.buffers_in_css = 0;
627 asd->video_out_video_capture.buffers_in_css = 0;
631 bool atomisp_buffers_queued(struct atomisp_sub_device *asd)
633 return asd->video_out_capture.buffers_in_css ||
634 asd->video_out_vf.buffers_in_css ||
635 asd->video_out_preview.buffers_in_css ||
636 asd->video_out_video_capture.buffers_in_css ?
641 bool atomisp_buffers_queued_pipe(struct atomisp_video_pipe *pipe)
643 return pipe->buffers_in_css ? true : false;
646 /* 0x100000 is the start of dmem inside SP */
647 #define SP_DMEM_BASE 0x100000
649 void dump_sp_dmem(struct atomisp_device *isp, unsigned int addr,
652 unsigned int data = 0;
653 unsigned int size32 = DIV_ROUND_UP(size, sizeof(u32));
655 dev_dbg(isp->dev, "atomisp_io_base:%p\n", atomisp_io_base);
656 dev_dbg(isp->dev, "%s, addr:0x%x, size: %d, size32: %d\n", __func__,
658 if (size32 * 4 + addr > 0x4000) {
659 dev_err(isp->dev, "illegal size (%d) or addr (0x%x)\n",
663 addr += SP_DMEM_BASE;
665 data = _hrt_master_port_uload_32(addr);
667 dev_dbg(isp->dev, "%s, \t [0x%x]:0x%x\n", __func__, addr, data);
668 addr += sizeof(unsigned int);
670 } while (size32 > 0);
673 static struct videobuf_buffer *atomisp_css_frame_to_vbuf(
674 struct atomisp_video_pipe *pipe, struct atomisp_css_frame *frame)
676 struct videobuf_vmalloc_memory *vm_mem;
677 struct atomisp_css_frame *handle;
680 for (i = 0; pipe->capq.bufs[i]; i++) {
681 vm_mem = pipe->capq.bufs[i]->priv;
682 handle = vm_mem->vaddr;
683 if (handle && handle->data == frame->data)
684 return pipe->capq.bufs[i];
690 static void atomisp_flush_video_pipe(struct atomisp_sub_device *asd,
691 struct atomisp_video_pipe *pipe)
693 unsigned long irqflags;
699 for (i = 0; pipe->capq.bufs[i]; i++) {
700 spin_lock_irqsave(&pipe->irq_lock, irqflags);
701 if (pipe->capq.bufs[i]->state == VIDEOBUF_ACTIVE ||
702 pipe->capq.bufs[i]->state == VIDEOBUF_QUEUED) {
703 pipe->capq.bufs[i]->ts = ktime_get_ns();
704 pipe->capq.bufs[i]->field_count =
705 atomic_read(&asd->sequence) << 1;
706 dev_dbg(asd->isp->dev, "release buffers on device %s\n",
708 if (pipe->capq.bufs[i]->state == VIDEOBUF_QUEUED)
709 list_del_init(&pipe->capq.bufs[i]->queue);
710 pipe->capq.bufs[i]->state = VIDEOBUF_ERROR;
711 wake_up(&pipe->capq.bufs[i]->done);
713 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
717 /* Returns queued buffers back to video-core */
718 void atomisp_flush_bufs_and_wakeup(struct atomisp_sub_device *asd)
720 atomisp_flush_video_pipe(asd, &asd->video_out_capture);
721 atomisp_flush_video_pipe(asd, &asd->video_out_vf);
722 atomisp_flush_video_pipe(asd, &asd->video_out_preview);
723 atomisp_flush_video_pipe(asd, &asd->video_out_video_capture);
726 /* clean out the parameters that did not apply */
727 void atomisp_flush_params_queue(struct atomisp_video_pipe *pipe)
729 struct atomisp_css_params_with_list *param;
731 while (!list_empty(&pipe->per_frame_params)) {
732 param = list_entry(pipe->per_frame_params.next,
733 struct atomisp_css_params_with_list, list);
734 list_del(¶m->list);
735 atomisp_free_css_parameters(¶m->params);
740 /* Re-queue per-frame parameters */
741 static void atomisp_recover_params_queue(struct atomisp_video_pipe *pipe)
743 struct atomisp_css_params_with_list *param;
746 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
747 param = pipe->frame_params[i];
749 list_add_tail(¶m->list, &pipe->per_frame_params);
750 pipe->frame_params[i] = NULL;
752 atomisp_handle_parameter_and_buffer(pipe);
755 /* find atomisp_video_pipe with css pipe id, buffer type and atomisp run_mode */
756 static struct atomisp_video_pipe *__atomisp_get_pipe(
757 struct atomisp_sub_device *asd,
758 enum atomisp_input_stream_id stream_id,
759 enum atomisp_css_pipe_id css_pipe_id,
760 enum atomisp_css_buffer_type buf_type)
762 struct atomisp_device *isp = asd->isp;
764 if (css_pipe_id == CSS_PIPE_ID_COPY &&
765 isp->inputs[asd->input_curr].camera_caps->
766 sensor[asd->sensor_curr].stream_num > 1) {
768 case ATOMISP_INPUT_STREAM_PREVIEW:
769 return &asd->video_out_preview;
770 case ATOMISP_INPUT_STREAM_POSTVIEW:
771 return &asd->video_out_vf;
772 case ATOMISP_INPUT_STREAM_VIDEO:
773 return &asd->video_out_video_capture;
774 case ATOMISP_INPUT_STREAM_CAPTURE:
776 return &asd->video_out_capture;
780 /* video is same in online as in continuouscapture mode */
781 if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
783 * Disable vf_pp and run CSS in still capture mode. In this
784 * mode, CSS does not cause extra latency with buffering, but
785 * scaling is not available.
787 return &asd->video_out_capture;
788 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
790 * Disable vf_pp and run CSS in video mode. This allows using
791 * ISP scaling but it has one frame delay due to CSS internal
794 return &asd->video_out_video_capture;
795 } else if (css_pipe_id == CSS_PIPE_ID_YUVPP) {
797 * to SOC camera, yuvpp pipe is run for capture/video/SDV/ZSL.
799 if (asd->continuous_mode->val) {
800 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
803 case CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
804 return &asd->video_out_video_capture;
805 case CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
806 return &asd->video_out_preview;
807 case CSS_BUFFER_TYPE_OUTPUT_FRAME:
808 return &asd->video_out_capture;
810 return &asd->video_out_vf;
812 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
815 case CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
816 return &asd->video_out_preview;
817 case CSS_BUFFER_TYPE_OUTPUT_FRAME:
818 return &asd->video_out_capture;
820 return &asd->video_out_vf;
823 } else if (buf_type == CSS_BUFFER_TYPE_OUTPUT_FRAME) {
824 switch (asd->run_mode->val) {
825 case ATOMISP_RUN_MODE_VIDEO:
826 return &asd->video_out_video_capture;
827 case ATOMISP_RUN_MODE_PREVIEW:
828 return &asd->video_out_preview;
830 return &asd->video_out_capture;
832 } else if (buf_type == CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
833 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
834 return &asd->video_out_preview;
836 return &asd->video_out_vf;
838 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
839 /* For online video or SDV video pipe. */
840 if (css_pipe_id == CSS_PIPE_ID_VIDEO ||
841 css_pipe_id == CSS_PIPE_ID_COPY) {
842 if (buf_type == CSS_BUFFER_TYPE_OUTPUT_FRAME)
843 return &asd->video_out_video_capture;
844 return &asd->video_out_preview;
846 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
847 /* For online preview or ZSL preview pipe. */
848 if (css_pipe_id == CSS_PIPE_ID_PREVIEW ||
849 css_pipe_id == CSS_PIPE_ID_COPY)
850 return &asd->video_out_preview;
852 /* For capture pipe. */
853 if (buf_type == CSS_BUFFER_TYPE_VF_OUTPUT_FRAME)
854 return &asd->video_out_vf;
855 return &asd->video_out_capture;
858 enum atomisp_metadata_type
859 atomisp_get_metadata_type(struct atomisp_sub_device *asd,
860 enum ia_css_pipe_id pipe_id) {
861 if (!asd->continuous_mode->val)
862 return ATOMISP_MAIN_METADATA;
864 if (pipe_id == IA_CSS_PIPE_ID_CAPTURE) /* online capture pipe */
865 return ATOMISP_SEC_METADATA;
867 return ATOMISP_MAIN_METADATA;
870 void atomisp_buf_done(struct atomisp_sub_device *asd, int error,
871 enum atomisp_css_buffer_type buf_type,
872 enum atomisp_css_pipe_id css_pipe_id,
873 bool q_buffers, enum atomisp_input_stream_id stream_id)
875 struct videobuf_buffer *vb = NULL;
876 struct atomisp_video_pipe *pipe = NULL;
877 struct atomisp_css_buffer buffer;
878 bool requeue = false;
880 unsigned long irqflags;
881 struct atomisp_css_frame *frame = NULL;
882 struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf_tmp;
883 struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf_tmp;
884 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf_tmp;
885 enum atomisp_metadata_type md_type;
886 struct atomisp_device *isp = asd->isp;
887 struct v4l2_control ctrl;
888 bool reset_wdt_timer = false;
891 buf_type != CSS_BUFFER_TYPE_METADATA &&
892 buf_type != CSS_BUFFER_TYPE_3A_STATISTICS &&
893 buf_type != CSS_BUFFER_TYPE_DIS_STATISTICS &&
894 buf_type != CSS_BUFFER_TYPE_OUTPUT_FRAME &&
895 buf_type != CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
896 buf_type != CSS_BUFFER_TYPE_RAW_OUTPUT_FRAME &&
897 buf_type != CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
898 buf_type != CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
899 dev_err(isp->dev, "%s, unsupported buffer type: %d\n",
904 memset(&buffer, 0, sizeof(struct atomisp_css_buffer));
905 buffer.css_buffer.type = buf_type;
906 err = atomisp_css_dequeue_buffer(asd, stream_id, css_pipe_id,
910 "atomisp_css_dequeue_buffer failed: 0x%x\n", err);
914 /* need to know the atomisp pipe for frame buffers */
915 pipe = __atomisp_get_pipe(asd, stream_id, css_pipe_id, buf_type);
917 dev_err(isp->dev, "error getting atomisp pipe\n");
922 case CSS_BUFFER_TYPE_3A_STATISTICS:
923 list_for_each_entry_safe(s3a_buf, _s3a_buf_tmp,
924 &asd->s3a_stats_in_css, list) {
925 if (s3a_buf->s3a_data ==
926 buffer.css_buffer.data.stats_3a) {
927 list_del_init(&s3a_buf->list);
928 list_add_tail(&s3a_buf->list,
929 &asd->s3a_stats_ready);
934 asd->s3a_bufs_in_css[css_pipe_id]--;
935 atomisp_3a_stats_ready_event(asd, buffer.css_buffer.exp_id);
936 dev_dbg(isp->dev, "%s: s3a stat with exp_id %d is ready\n",
937 __func__, s3a_buf->s3a_data->exp_id);
939 case CSS_BUFFER_TYPE_METADATA:
943 md_type = atomisp_get_metadata_type(asd, css_pipe_id);
944 list_for_each_entry_safe(md_buf, _md_buf_tmp,
945 &asd->metadata_in_css[md_type], list) {
946 if (md_buf->metadata ==
947 buffer.css_buffer.data.metadata) {
948 list_del_init(&md_buf->list);
949 list_add_tail(&md_buf->list,
950 &asd->metadata_ready[md_type]);
954 asd->metadata_bufs_in_css[stream_id][css_pipe_id]--;
955 atomisp_metadata_ready_event(asd, md_type);
956 dev_dbg(isp->dev, "%s: metadata with exp_id %d is ready\n",
957 __func__, md_buf->metadata->exp_id);
959 case CSS_BUFFER_TYPE_DIS_STATISTICS:
960 list_for_each_entry_safe(dis_buf, _dis_buf_tmp,
961 &asd->dis_stats_in_css, list) {
962 if (dis_buf->dis_data ==
963 buffer.css_buffer.data.stats_dvs) {
964 spin_lock_irqsave(&asd->dis_stats_lock,
966 list_del_init(&dis_buf->list);
967 list_add(&dis_buf->list, &asd->dis_stats);
968 asd->params.dis_proj_data_valid = true;
969 spin_unlock_irqrestore(&asd->dis_stats_lock,
974 asd->dis_bufs_in_css--;
975 dev_dbg(isp->dev, "%s: dis stat with exp_id %d is ready\n",
976 __func__, dis_buf->dis_data->exp_id);
978 case CSS_BUFFER_TYPE_VF_OUTPUT_FRAME:
979 case CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
980 if (atomisp_hw_is_isp2401)
981 reset_wdt_timer = true;
983 pipe->buffers_in_css--;
984 frame = buffer.css_buffer.data.frame;
993 * YUVPP doesn't set postview exp_id correctlly in SDV mode.
994 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
996 if (IS_BYT && buf_type == CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
997 asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
998 frame->exp_id = (asd->postview_exp_id++) %
999 (ATOMISP_MAX_EXP_ID + 1);
1001 dev_dbg(isp->dev, "%s: vf frame with exp_id %d is ready\n",
1002 __func__, frame->exp_id);
1003 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
1004 if (frame->flash_state
1005 == CSS_FRAME_FLASH_STATE_PARTIAL)
1006 dev_dbg(isp->dev, "%s thumb partially flashed\n",
1008 else if (frame->flash_state
1009 == CSS_FRAME_FLASH_STATE_FULL)
1010 dev_dbg(isp->dev, "%s thumb completely flashed\n",
1013 dev_dbg(isp->dev, "%s thumb no flash in this frame\n",
1016 vb = atomisp_css_frame_to_vbuf(pipe, frame);
1019 pipe->frame_config_id[vb->i] = frame->isp_config_id;
1020 if (css_pipe_id == IA_CSS_PIPE_ID_CAPTURE &&
1021 asd->pending_capture_request > 0) {
1022 err = atomisp_css_offline_capture_configure(asd,
1023 asd->params.offline_parm.num_captures,
1024 asd->params.offline_parm.skip_frames,
1025 asd->params.offline_parm.offset);
1027 asd->pending_capture_request--;
1029 if (atomisp_hw_is_isp2401)
1030 asd->re_trigger_capture = false;
1032 dev_dbg(isp->dev, "Trigger capture again for new buffer. err=%d\n",
1034 } else if (atomisp_hw_is_isp2401) {
1035 asd->re_trigger_capture = true;
1038 case CSS_BUFFER_TYPE_OUTPUT_FRAME:
1039 case CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
1040 if (atomisp_hw_is_isp2401)
1041 reset_wdt_timer = true;
1043 pipe->buffers_in_css--;
1044 frame = buffer.css_buffer.data.frame;
1054 * YUVPP doesn't set preview exp_id correctlly in ZSL mode.
1055 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
1057 if (IS_BYT && buf_type == CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
1058 asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
1059 frame->exp_id = (asd->preview_exp_id++) %
1060 (ATOMISP_MAX_EXP_ID + 1);
1062 dev_dbg(isp->dev, "%s: main frame with exp_id %d is ready\n",
1063 __func__, frame->exp_id);
1064 vb = atomisp_css_frame_to_vbuf(pipe, frame);
1070 /* free the parameters */
1071 if (pipe->frame_params[vb->i]) {
1072 if (asd->params.dvs_6axis ==
1073 pipe->frame_params[vb->i]->params.dvs_6axis)
1074 asd->params.dvs_6axis = NULL;
1075 atomisp_free_css_parameters(
1076 &pipe->frame_params[vb->i]->params);
1077 kvfree(pipe->frame_params[vb->i]);
1078 pipe->frame_params[vb->i] = NULL;
1081 pipe->frame_config_id[vb->i] = frame->isp_config_id;
1082 ctrl.id = V4L2_CID_FLASH_MODE;
1083 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
1084 if (frame->flash_state
1085 == CSS_FRAME_FLASH_STATE_PARTIAL) {
1086 asd->frame_status[vb->i] =
1087 ATOMISP_FRAME_STATUS_FLASH_PARTIAL;
1088 dev_dbg(isp->dev, "%s partially flashed\n",
1090 } else if (frame->flash_state
1091 == CSS_FRAME_FLASH_STATE_FULL) {
1092 asd->frame_status[vb->i] =
1093 ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
1094 asd->params.num_flash_frames--;
1095 dev_dbg(isp->dev, "%s completely flashed\n",
1098 asd->frame_status[vb->i] =
1099 ATOMISP_FRAME_STATUS_OK;
1101 "%s no flash in this frame\n",
1105 /* Check if flashing sequence is done */
1106 if (asd->frame_status[vb->i] ==
1107 ATOMISP_FRAME_STATUS_FLASH_EXPOSED)
1108 asd->params.flash_state = ATOMISP_FLASH_DONE;
1109 } else if (isp->flash) {
1110 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) ==
1111 0 && ctrl.value == ATOMISP_FLASH_MODE_TORCH) {
1112 ctrl.id = V4L2_CID_FLASH_TORCH_INTENSITY;
1113 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl)
1114 == 0 && ctrl.value > 0) {
1115 asd->frame_status[vb->i] =
1116 ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
1118 asd->frame_status[vb->i] =
1119 ATOMISP_FRAME_STATUS_OK;
1122 asd->frame_status[vb->i] =
1123 ATOMISP_FRAME_STATUS_OK;
1125 asd->frame_status[vb->i] = ATOMISP_FRAME_STATUS_OK;
1128 asd->params.last_frame_status = asd->frame_status[vb->i];
1130 if (asd->continuous_mode->val) {
1131 if (css_pipe_id == CSS_PIPE_ID_PREVIEW ||
1132 css_pipe_id == CSS_PIPE_ID_VIDEO) {
1133 asd->latest_preview_exp_id = frame->exp_id;
1134 } else if (css_pipe_id ==
1135 CSS_PIPE_ID_CAPTURE) {
1136 if (asd->run_mode->val ==
1137 ATOMISP_RUN_MODE_VIDEO)
1138 dev_dbg(isp->dev, "SDV capture raw buffer id: %u\n",
1141 dev_dbg(isp->dev, "ZSL capture raw buffer id: %u\n",
1146 * Only after enabled the raw buffer lock
1147 * and in continuous mode.
1148 * in preview/video pipe, each buffer will
1149 * be locked automatically, so record it here.
1151 if (((css_pipe_id == CSS_PIPE_ID_PREVIEW) ||
1152 (css_pipe_id == CSS_PIPE_ID_VIDEO)) &&
1153 asd->enable_raw_buffer_lock->val &&
1154 asd->continuous_mode->val) {
1155 atomisp_set_raw_buffer_bitmap(asd, frame->exp_id);
1156 WARN_ON(frame->exp_id > ATOMISP_MAX_EXP_ID);
1159 if (asd->params.css_update_params_needed) {
1160 atomisp_apply_css_parameters(asd,
1161 &asd->params.css_param);
1162 if (asd->params.css_param.update_flag.dz_config)
1163 atomisp_css_set_dz_config(asd,
1164 &asd->params.css_param.dz_config);
1165 /* New global dvs 6axis config should be blocked
1166 * here if there's a buffer with per-frame parameters
1167 * pending in CSS frame buffer queue.
1168 * This is to aviod zooming vibration since global
1169 * parameters take effect immediately while
1170 * per-frame parameters are taken after previous
1171 * buffers in CSS got processed.
1173 if (asd->params.dvs_6axis)
1174 atomisp_css_set_dvs_6axis(asd,
1175 asd->params.dvs_6axis);
1177 asd->params.css_update_params_needed = false;
1178 /* The update flag should not be cleaned here
1179 * since it is still going to be used to make up
1180 * following per-frame parameters.
1181 * This will introduce more copy work since each
1182 * time when updating global parameters, the whole
1183 * parameter set are applied.
1184 * FIXME: A new set of parameter copy functions can
1185 * be added to make up per-frame parameters based on
1186 * solid structures stored in asd->params.css_param
1187 * instead of using shadow pointers in update flag.
1189 atomisp_css_update_isp_params(asd);
1197 vb->ts = ktime_get_ns();
1198 vb->field_count = atomic_read(&asd->sequence) << 1;
1199 /*mark videobuffer done for dequeue*/
1200 spin_lock_irqsave(&pipe->irq_lock, irqflags);
1201 vb->state = !error ? VIDEOBUF_DONE : VIDEOBUF_ERROR;
1202 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
1205 * Frame capture done, wake up any process block on
1206 * current active buffer
1207 * possibly hold by videobuf_dqbuf()
1211 if (atomisp_hw_is_isp2401)
1212 atomic_set(&pipe->wdt_count, 0);
1215 * Requeue should only be done for 3a and dis buffers.
1216 * Queue/dequeue order will change if driver recycles image buffers.
1220 err = atomisp_css_queue_buffer(asd,
1221 stream_id, css_pipe_id,
1224 dev_err(isp->dev, "%s, q to css fails: %d\n",
1228 if (!error && q_buffers)
1229 atomisp_qbuffers_to_css(asd);
1231 if (atomisp_hw_is_isp2401) {
1232 /* If there are no buffers queued then
1233 * delete wdt timer. */
1234 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1236 if (!atomisp_buffers_queued_pipe(pipe))
1237 atomisp_wdt_stop_pipe(pipe, false);
1238 else if (reset_wdt_timer)
1239 /* SOF irq should not reset wdt timer. */
1240 atomisp_wdt_refresh_pipe(pipe,
1241 ATOMISP_WDT_KEEP_CURRENT_DELAY);
1245 void atomisp_delayed_init_work(struct work_struct *work)
1247 struct atomisp_sub_device *asd = container_of(work,
1248 struct atomisp_sub_device,
1251 * to SOC camera, use yuvpp pipe and no support continuous mode.
1253 if (!ATOMISP_USE_YUVPP(asd)) {
1254 struct v4l2_event event = {0};
1256 atomisp_css_allocate_continuous_frames(false, asd);
1257 atomisp_css_update_continuous_frames(asd);
1259 event.type = V4L2_EVENT_ATOMISP_RAW_BUFFERS_ALLOC_DONE;
1260 v4l2_event_queue(asd->subdev.devnode, &event);
1263 /* signal streamon after delayed init is done */
1264 asd->delayed_init = ATOMISP_DELAYED_INIT_DONE;
1265 complete(&asd->init_done);
1268 static void __atomisp_css_recover(struct atomisp_device *isp, bool isp_timeout)
1270 enum atomisp_css_pipe_id css_pipe_id;
1271 bool stream_restart[MAX_STREAM_NUM] = {0};
1272 bool depth_mode = false;
1273 int i, ret, depth_cnt = 0;
1275 if (!isp->sw_contex.file_input)
1276 atomisp_css_irq_enable(isp,
1277 CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
1279 BUG_ON(isp->num_of_streams > MAX_STREAM_NUM);
1281 for (i = 0; i < isp->num_of_streams; i++) {
1282 struct atomisp_sub_device *asd = &isp->asd[i];
1283 struct ia_css_pipeline *acc_pipeline;
1284 struct ia_css_pipe *acc_pipe = NULL;
1286 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED &&
1287 !asd->stream_prepared)
1291 * AtomISP::waitStageUpdate is blocked when WDT happens.
1292 * By calling acc_done() for all loaded fw_handles,
1293 * HAL will be unblocked.
1295 acc_pipe = asd->stream_env[i].pipes[CSS_PIPE_ID_ACC];
1297 acc_pipeline = ia_css_pipe_get_pipeline(acc_pipe);
1299 struct ia_css_pipeline_stage *stage;
1301 for (stage = acc_pipeline->stages; stage;
1302 stage = stage->next) {
1303 const struct ia_css_fw_info *fw;
1305 fw = stage->firmware;
1306 atomisp_acc_done(asd, fw->handle);
1313 if (asd->delayed_init == ATOMISP_DELAYED_INIT_QUEUED)
1314 cancel_work_sync(&asd->delayed_init_work);
1316 complete(&asd->init_done);
1317 asd->delayed_init = ATOMISP_DELAYED_INIT_NOT_QUEUED;
1319 stream_restart[asd->index] = true;
1321 asd->streaming = ATOMISP_DEVICE_STREAMING_STOPPING;
1323 /* stream off sensor */
1324 ret = v4l2_subdev_call(
1325 isp->inputs[asd->input_curr].
1326 camera, video, s_stream, 0);
1329 "can't stop streaming on sensor!\n");
1331 atomisp_acc_unload_extensions(asd);
1333 atomisp_clear_css_buffer_counters(asd);
1335 css_pipe_id = atomisp_get_css_pipe_id(asd);
1336 atomisp_css_stop(asd, css_pipe_id, true);
1338 asd->streaming = ATOMISP_DEVICE_STREAMING_DISABLED;
1340 asd->preview_exp_id = 1;
1341 asd->postview_exp_id = 1;
1342 /* notify HAL the CSS reset */
1344 "send reset event to %s\n", asd->subdev.devnode->name);
1345 atomisp_reset_event(asd);
1349 disable_isp_irq(hrt_isp_css_irq_sp);
1350 clear_isp_irq(hrt_isp_css_irq_sp);
1352 /* Set the SRSE to 3 before resetting */
1353 pci_write_config_dword(isp->pdev, PCI_I_CONTROL, isp->saved_regs.i_control |
1354 MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
1356 /* reset ISP and restore its state */
1357 isp->isp_timeout = true;
1359 isp->isp_timeout = false;
1362 for (i = 0; i < isp->num_of_streams; i++) {
1363 if (isp->asd[i].depth_mode->val)
1368 for (i = 0; i < isp->num_of_streams; i++) {
1369 struct atomisp_sub_device *asd = &isp->asd[i];
1371 if (!stream_restart[i])
1374 if (isp->inputs[asd->input_curr].type != FILE_INPUT)
1375 atomisp_css_input_set_mode(asd,
1376 CSS_INPUT_MODE_SENSOR);
1378 css_pipe_id = atomisp_get_css_pipe_id(asd);
1379 if (atomisp_css_start(asd, css_pipe_id, true))
1381 "start SP failed, so do not set streaming to be enable!\n");
1383 asd->streaming = ATOMISP_DEVICE_STREAMING_ENABLED;
1385 atomisp_csi2_configure(asd);
1388 if (!isp->sw_contex.file_input) {
1389 atomisp_css_irq_enable(isp, CSS_IRQ_INFO_CSS_RECEIVER_SOF,
1390 atomisp_css_valid_sof(isp));
1392 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, true) < 0)
1393 dev_dbg(isp->dev, "dfs failed!\n");
1395 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_MAX, true) < 0)
1396 dev_dbg(isp->dev, "dfs failed!\n");
1399 for (i = 0; i < isp->num_of_streams; i++) {
1400 struct atomisp_sub_device *asd;
1404 if (!stream_restart[i])
1407 if (asd->continuous_mode->val &&
1408 asd->delayed_init == ATOMISP_DELAYED_INIT_NOT_QUEUED) {
1409 reinit_completion(&asd->init_done);
1410 asd->delayed_init = ATOMISP_DELAYED_INIT_QUEUED;
1411 queue_work(asd->delayed_init_workq,
1412 &asd->delayed_init_work);
1415 * dequeueing buffers is not needed. CSS will recycle
1416 * buffers that it has.
1418 atomisp_flush_bufs_and_wakeup(asd);
1420 /* Requeue unprocessed per-frame parameters. */
1421 atomisp_recover_params_queue(&asd->video_out_capture);
1422 atomisp_recover_params_queue(&asd->video_out_preview);
1423 atomisp_recover_params_queue(&asd->video_out_video_capture);
1425 if ((asd->depth_mode->val) &&
1426 (depth_cnt == ATOMISP_DEPTH_SENSOR_STREAMON_COUNT)) {
1431 ret = v4l2_subdev_call(
1432 isp->inputs[asd->input_curr].camera, video,
1436 "can't start streaming on sensor!\n");
1440 if (atomisp_stream_on_master_slave_sensor(isp, true))
1442 "master slave sensor stream on failed!\n");
1446 void atomisp_wdt_work(struct work_struct *work)
1448 struct atomisp_device *isp = container_of(work, struct atomisp_device,
1451 unsigned int pipe_wdt_cnt[MAX_STREAM_NUM][4] = { {0} };
1452 bool css_recover = false;
1454 rt_mutex_lock(&isp->mutex);
1455 if (!atomisp_streaming_count(isp)) {
1456 atomic_set(&isp->wdt_work_queued, 0);
1457 rt_mutex_unlock(&isp->mutex);
1461 if (!atomisp_hw_is_isp2401) {
1462 dev_err(isp->dev, "timeout %d of %d\n",
1463 atomic_read(&isp->wdt_count) + 1,
1464 ATOMISP_ISP_MAX_TIMEOUT_COUNT);
1466 if (atomic_inc_return(&isp->wdt_count) < ATOMISP_ISP_MAX_TIMEOUT_COUNT)
1471 for (i = 0; i < isp->num_of_streams; i++) {
1472 struct atomisp_sub_device *asd = &isp->asd[i];
1474 pipe_wdt_cnt[i][0] +=
1475 atomic_read(&asd->video_out_capture.wdt_count);
1476 pipe_wdt_cnt[i][1] +=
1477 atomic_read(&asd->video_out_vf.wdt_count);
1478 pipe_wdt_cnt[i][2] +=
1479 atomic_read(&asd->video_out_preview.wdt_count);
1480 pipe_wdt_cnt[i][3] +=
1481 atomic_read(&asd->video_out_video_capture.wdt_count);
1483 (pipe_wdt_cnt[i][0] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1484 pipe_wdt_cnt[i][1] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1485 pipe_wdt_cnt[i][2] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1486 pipe_wdt_cnt[i][3] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT)
1489 "pipe on asd%d timeout cnt: (%d, %d, %d, %d) of %d, recover = %d\n",
1490 asd->index, pipe_wdt_cnt[i][0], pipe_wdt_cnt[i][1],
1491 pipe_wdt_cnt[i][2], pipe_wdt_cnt[i][3],
1492 ATOMISP_ISP_MAX_TIMEOUT_COUNT, css_recover);
1497 unsigned int old_dbglevel = dbg_level;
1499 atomisp_css_debug_dump_sp_sw_debug_info();
1500 atomisp_css_debug_dump_debug_info(__func__);
1501 dbg_level = old_dbglevel;
1502 for (i = 0; i < isp->num_of_streams; i++) {
1503 struct atomisp_sub_device *asd = &isp->asd[i];
1505 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1507 dev_err(isp->dev, "%s, vdev %s buffers in css: %d\n",
1509 asd->video_out_capture.vdev.name,
1510 asd->video_out_capture.
1513 "%s, vdev %s buffers in css: %d\n",
1515 asd->video_out_vf.vdev.name,
1519 "%s, vdev %s buffers in css: %d\n",
1521 asd->video_out_preview.vdev.name,
1522 asd->video_out_preview.
1525 "%s, vdev %s buffers in css: %d\n",
1527 asd->video_out_video_capture.vdev.name,
1528 asd->video_out_video_capture.
1531 "%s, s3a buffers in css preview pipe:%d\n",
1533 asd->s3a_bufs_in_css[CSS_PIPE_ID_PREVIEW]);
1535 "%s, s3a buffers in css capture pipe:%d\n",
1537 asd->s3a_bufs_in_css[CSS_PIPE_ID_CAPTURE]);
1539 "%s, s3a buffers in css video pipe:%d\n",
1541 asd->s3a_bufs_in_css[CSS_PIPE_ID_VIDEO]);
1543 "%s, dis buffers in css: %d\n",
1544 __func__, asd->dis_bufs_in_css);
1546 "%s, metadata buffers in css preview pipe:%d\n",
1548 asd->metadata_bufs_in_css
1549 [ATOMISP_INPUT_STREAM_GENERAL]
1550 [CSS_PIPE_ID_PREVIEW]);
1552 "%s, metadata buffers in css capture pipe:%d\n",
1554 asd->metadata_bufs_in_css
1555 [ATOMISP_INPUT_STREAM_GENERAL]
1556 [CSS_PIPE_ID_CAPTURE]);
1558 "%s, metadata buffers in css video pipe:%d\n",
1560 asd->metadata_bufs_in_css
1561 [ATOMISP_INPUT_STREAM_GENERAL]
1562 [CSS_PIPE_ID_VIDEO]);
1563 if (asd->enable_raw_buffer_lock->val) {
1566 dev_err(isp->dev, "%s, raw_buffer_locked_count %d\n",
1567 __func__, asd->raw_buffer_locked_count);
1568 for (j = 0; j <= ATOMISP_MAX_EXP_ID / 32; j++)
1569 dev_err(isp->dev, "%s, raw_buffer_bitmap[%d]: 0x%x\n",
1571 asd->raw_buffer_bitmap[j]);
1575 /*sh_css_dump_sp_state();*/
1576 /*sh_css_dump_isp_state();*/
1578 for (i = 0; i < isp->num_of_streams; i++) {
1579 struct atomisp_sub_device *asd = &isp->asd[i];
1581 if (asd->streaming ==
1582 ATOMISP_DEVICE_STREAMING_ENABLED) {
1583 atomisp_clear_css_buffer_counters(asd);
1584 atomisp_flush_bufs_and_wakeup(asd);
1585 complete(&asd->init_done);
1587 if (atomisp_hw_is_isp2401)
1588 atomisp_wdt_stop(asd, false);
1591 if (!atomisp_hw_is_isp2401) {
1592 atomic_set(&isp->wdt_count, 0);
1594 isp->isp_fatal_error = true;
1595 atomic_set(&isp->wdt_work_queued, 0);
1597 rt_mutex_unlock(&isp->mutex);
1602 __atomisp_css_recover(isp, true);
1603 if (atomisp_hw_is_isp2401) {
1604 for (i = 0; i < isp->num_of_streams; i++) {
1605 struct atomisp_sub_device *asd = &isp->asd[i];
1607 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1610 atomisp_wdt_refresh(asd,
1611 isp->sw_contex.file_input ?
1612 ATOMISP_ISP_FILE_TIMEOUT_DURATION :
1613 ATOMISP_ISP_TIMEOUT_DURATION);
1617 dev_err(isp->dev, "timeout recovery handling done\n");
1618 atomic_set(&isp->wdt_work_queued, 0);
1620 rt_mutex_unlock(&isp->mutex);
1623 void atomisp_css_flush(struct atomisp_device *isp)
1627 if (!atomisp_streaming_count(isp))
1631 for (i = 0; i < isp->num_of_streams; i++) {
1632 struct atomisp_sub_device *asd = &isp->asd[i];
1634 atomisp_wdt_stop(asd, true);
1638 __atomisp_css_recover(isp, false);
1640 for (i = 0; i < isp->num_of_streams; i++) {
1641 struct atomisp_sub_device *asd = &isp->asd[i];
1643 if (asd->streaming !=
1644 ATOMISP_DEVICE_STREAMING_ENABLED)
1647 atomisp_wdt_refresh(asd,
1648 isp->sw_contex.file_input ?
1649 ATOMISP_ISP_FILE_TIMEOUT_DURATION :
1650 ATOMISP_ISP_TIMEOUT_DURATION);
1652 dev_dbg(isp->dev, "atomisp css flush done\n");
1655 void atomisp_wdt(struct timer_list *t)
1657 struct atomisp_sub_device *asd;
1658 struct atomisp_device *isp;
1660 if (!atomisp_hw_is_isp2401) {
1661 asd = from_timer(asd, t, wdt);
1664 struct atomisp_video_pipe *pipe = from_timer(pipe, t, wdt);
1669 atomic_inc(&pipe->wdt_count);
1671 "[WARNING]asd %d pipe %s ISP timeout %d!\n",
1672 asd->index, pipe->vdev.name,
1673 atomic_read(&pipe->wdt_count));
1676 if (atomic_read(&isp->wdt_work_queued)) {
1677 dev_dbg(isp->dev, "ISP watchdog was put into workqueue\n");
1680 atomic_set(&isp->wdt_work_queued, 1);
1681 queue_work(isp->wdt_work_queue, &isp->wdt_work);
1685 void atomisp_wdt_start(struct atomisp_sub_device *asd)
1687 atomisp_wdt_refresh(asd, ATOMISP_ISP_TIMEOUT_DURATION);
1691 void atomisp_wdt_refresh_pipe(struct atomisp_video_pipe *pipe,
1696 if (delay != ATOMISP_WDT_KEEP_CURRENT_DELAY)
1697 pipe->wdt_duration = delay;
1699 next = jiffies + pipe->wdt_duration;
1701 /* Override next if it has been pushed beyon the "next" time */
1702 if (atomisp_is_wdt_running(pipe) && time_after(pipe->wdt_expires, next))
1703 next = pipe->wdt_expires;
1705 pipe->wdt_expires = next;
1707 if (atomisp_is_wdt_running(pipe))
1708 dev_dbg(pipe->asd->isp->dev, "WDT will hit after %d ms (%s)\n",
1709 ((int)(next - jiffies) * 1000 / HZ), pipe->vdev.name);
1711 dev_dbg(pipe->asd->isp->dev, "WDT starts with %d ms period (%s)\n",
1712 ((int)(next - jiffies) * 1000 / HZ), pipe->vdev.name);
1714 mod_timer(&pipe->wdt, next);
1717 void atomisp_wdt_refresh(struct atomisp_sub_device *asd, unsigned int delay)
1719 if (!atomisp_hw_is_isp2401) {
1722 if (delay != ATOMISP_WDT_KEEP_CURRENT_DELAY)
1723 asd->wdt_duration = delay;
1725 next = jiffies + asd->wdt_duration;
1727 /* Override next if it has been pushed beyon the "next" time */
1728 if (atomisp_is_wdt_running(asd) && time_after(asd->wdt_expires, next))
1729 next = asd->wdt_expires;
1731 asd->wdt_expires = next;
1733 if (atomisp_is_wdt_running(asd))
1734 dev_dbg(asd->isp->dev, "WDT will hit after %d ms\n",
1735 ((int)(next - jiffies) * 1000 / HZ));
1737 dev_dbg(asd->isp->dev, "WDT starts with %d ms period\n",
1738 ((int)(next - jiffies) * 1000 / HZ));
1740 mod_timer(&asd->wdt, next);
1741 atomic_set(&asd->isp->wdt_count, 0);
1743 dev_dbg(asd->isp->dev, "WDT refresh all:\n");
1744 if (atomisp_is_wdt_running(&asd->video_out_capture))
1745 atomisp_wdt_refresh_pipe(&asd->video_out_capture, delay);
1746 if (atomisp_is_wdt_running(&asd->video_out_preview))
1747 atomisp_wdt_refresh_pipe(&asd->video_out_preview, delay);
1748 if (atomisp_is_wdt_running(&asd->video_out_vf))
1749 atomisp_wdt_refresh_pipe(&asd->video_out_vf, delay);
1750 if (atomisp_is_wdt_running(&asd->video_out_video_capture))
1751 atomisp_wdt_refresh_pipe(&asd->video_out_video_capture, delay);
1756 void atomisp_wdt_stop_pipe(struct atomisp_video_pipe *pipe, bool sync)
1758 if (!atomisp_is_wdt_running(pipe))
1761 dev_dbg(pipe->asd->isp->dev,
1762 "WDT stop asd %d (%s)\n", pipe->asd->index, pipe->vdev.name);
1765 del_timer_sync(&pipe->wdt);
1766 cancel_work_sync(&pipe->asd->isp->wdt_work);
1768 del_timer(&pipe->wdt);
1773 void atomisp_wdt_start_pipe(struct atomisp_video_pipe *pipe)
1775 atomisp_wdt_refresh_pipe(pipe, ATOMISP_ISP_TIMEOUT_DURATION);
1778 void atomisp_wdt_stop(struct atomisp_sub_device *asd, bool sync)
1780 dev_dbg(asd->isp->dev, "WDT stop:\n");
1782 if (!atomisp_hw_is_isp2401) {
1784 del_timer_sync(&asd->wdt);
1785 cancel_work_sync(&asd->isp->wdt_work);
1787 del_timer(&asd->wdt);
1790 atomisp_wdt_stop_pipe(&asd->video_out_capture, sync);
1791 atomisp_wdt_stop_pipe(&asd->video_out_preview, sync);
1792 atomisp_wdt_stop_pipe(&asd->video_out_vf, sync);
1793 atomisp_wdt_stop_pipe(&asd->video_out_video_capture, sync);
1797 void atomisp_setup_flash(struct atomisp_sub_device *asd)
1799 struct atomisp_device *isp = asd->isp;
1800 struct v4l2_control ctrl;
1805 if (asd->params.flash_state != ATOMISP_FLASH_REQUESTED &&
1806 asd->params.flash_state != ATOMISP_FLASH_DONE)
1809 if (asd->params.num_flash_frames) {
1810 /* make sure the timeout is set before setting flash mode */
1811 ctrl.id = V4L2_CID_FLASH_TIMEOUT;
1812 ctrl.value = FLASH_TIMEOUT;
1814 if (v4l2_s_ctrl(NULL, isp->flash->ctrl_handler, &ctrl)) {
1815 dev_err(isp->dev, "flash timeout configure failed\n");
1819 atomisp_css_request_flash(asd);
1820 asd->params.flash_state = ATOMISP_FLASH_ONGOING;
1822 asd->params.flash_state = ATOMISP_FLASH_IDLE;
1826 irqreturn_t atomisp_isr_thread(int irq, void *isp_ptr)
1828 struct atomisp_device *isp = isp_ptr;
1829 unsigned long flags;
1830 bool frame_done_found[MAX_STREAM_NUM] = {0};
1831 bool css_pipe_done[MAX_STREAM_NUM] = {0};
1833 struct atomisp_sub_device *asd;
1835 dev_dbg(isp->dev, ">%s\n", __func__);
1837 spin_lock_irqsave(&isp->lock, flags);
1839 if (!atomisp_streaming_count(isp) && !atomisp_is_acc_enabled(isp)) {
1840 spin_unlock_irqrestore(&isp->lock, flags);
1844 spin_unlock_irqrestore(&isp->lock, flags);
1847 * The standard CSS2.0 API tells the following calling sequence of
1848 * dequeue ready buffers:
1849 * while (ia_css_dequeue_event(...)) {
1850 * switch (event.type) {
1852 * ia_css_pipe_dequeue_buffer()
1855 * That is, dequeue event and buffer are one after another.
1857 * But the following implementation is to first deuque all the event
1858 * to a FIFO, then process the event in the FIFO.
1859 * This will not have issue in single stream mode, but it do have some
1860 * issue in multiple stream case. The issue is that
1861 * ia_css_pipe_dequeue_buffer() will not return the corrent buffer in
1864 * This is due to ia_css_pipe_dequeue_buffer() does not take the
1865 * ia_css_pipe parameter.
1868 * For CSS2.0: we change the way to not dequeue all the event at one
1869 * time, instead, dequue one and process one, then another
1871 rt_mutex_lock(&isp->mutex);
1872 if (atomisp_css_isr_thread(isp, frame_done_found, css_pipe_done))
1875 for (i = 0; i < isp->num_of_streams; i++) {
1877 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1879 atomisp_setup_flash(asd);
1882 rt_mutex_unlock(&isp->mutex);
1883 for (i = 0; i < isp->num_of_streams; i++) {
1885 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED
1886 && css_pipe_done[asd->index]
1887 && isp->sw_contex.file_input)
1888 v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1889 video, s_stream, 1);
1890 /* FIXME! FIX ACC implementation */
1891 if (asd->acc.pipeline && css_pipe_done[asd->index])
1892 atomisp_css_acc_done(asd);
1894 dev_dbg(isp->dev, "<%s\n", __func__);
1900 * utils for buffer allocation/free
1903 int atomisp_get_frame_pgnr(struct atomisp_device *isp,
1904 const struct atomisp_css_frame *frame, u32 *p_pgnr)
1907 dev_err(isp->dev, "%s: NULL frame pointer ERROR.\n", __func__);
1911 *p_pgnr = DIV_ROUND_UP(frame->data_bytes, PAGE_SIZE);
1916 * Get internal fmt according to V4L2 fmt
1918 static enum atomisp_css_frame_format
1919 v4l2_fmt_to_sh_fmt(u32 fmt) {
1922 case V4L2_PIX_FMT_YUV420:
1923 return CSS_FRAME_FORMAT_YUV420;
1924 case V4L2_PIX_FMT_YVU420:
1925 return CSS_FRAME_FORMAT_YV12;
1926 case V4L2_PIX_FMT_YUV422P:
1927 return CSS_FRAME_FORMAT_YUV422;
1928 case V4L2_PIX_FMT_YUV444:
1929 return CSS_FRAME_FORMAT_YUV444;
1930 case V4L2_PIX_FMT_NV12:
1931 return CSS_FRAME_FORMAT_NV12;
1932 case V4L2_PIX_FMT_NV21:
1933 return CSS_FRAME_FORMAT_NV21;
1934 case V4L2_PIX_FMT_NV16:
1935 return CSS_FRAME_FORMAT_NV16;
1936 case V4L2_PIX_FMT_NV61:
1937 return CSS_FRAME_FORMAT_NV61;
1938 case V4L2_PIX_FMT_UYVY:
1939 return CSS_FRAME_FORMAT_UYVY;
1940 case V4L2_PIX_FMT_YUYV:
1941 return CSS_FRAME_FORMAT_YUYV;
1942 case V4L2_PIX_FMT_RGB24:
1943 return CSS_FRAME_FORMAT_PLANAR_RGB888;
1944 case V4L2_PIX_FMT_RGB32:
1945 return CSS_FRAME_FORMAT_RGBA888;
1946 case V4L2_PIX_FMT_RGB565:
1947 return CSS_FRAME_FORMAT_RGB565;
1948 case V4L2_PIX_FMT_JPEG:
1949 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1950 return CSS_FRAME_FORMAT_BINARY_8;
1951 case V4L2_PIX_FMT_SBGGR16:
1952 case V4L2_PIX_FMT_SBGGR10:
1953 case V4L2_PIX_FMT_SGBRG10:
1954 case V4L2_PIX_FMT_SGRBG10:
1955 case V4L2_PIX_FMT_SRGGB10:
1956 case V4L2_PIX_FMT_SBGGR12:
1957 case V4L2_PIX_FMT_SGBRG12:
1958 case V4L2_PIX_FMT_SGRBG12:
1959 case V4L2_PIX_FMT_SRGGB12:
1960 case V4L2_PIX_FMT_SBGGR8:
1961 case V4L2_PIX_FMT_SGBRG8:
1962 case V4L2_PIX_FMT_SGRBG8:
1963 case V4L2_PIX_FMT_SRGGB8:
1964 return CSS_FRAME_FORMAT_RAW;
1971 * raw format match between SH format and V4L2 format
1973 static int raw_output_format_match_input(u32 input, u32 output)
1975 if ((input == CSS_FORMAT_RAW_12) &&
1976 ((output == V4L2_PIX_FMT_SRGGB12) ||
1977 (output == V4L2_PIX_FMT_SGRBG12) ||
1978 (output == V4L2_PIX_FMT_SBGGR12) ||
1979 (output == V4L2_PIX_FMT_SGBRG12)))
1982 if ((input == CSS_FORMAT_RAW_10) &&
1983 ((output == V4L2_PIX_FMT_SRGGB10) ||
1984 (output == V4L2_PIX_FMT_SGRBG10) ||
1985 (output == V4L2_PIX_FMT_SBGGR10) ||
1986 (output == V4L2_PIX_FMT_SGBRG10)))
1989 if ((input == CSS_FORMAT_RAW_8) &&
1990 ((output == V4L2_PIX_FMT_SRGGB8) ||
1991 (output == V4L2_PIX_FMT_SGRBG8) ||
1992 (output == V4L2_PIX_FMT_SBGGR8) ||
1993 (output == V4L2_PIX_FMT_SGBRG8)))
1996 if ((input == CSS_FORMAT_RAW_16) && (output == V4L2_PIX_FMT_SBGGR16))
2002 static u32 get_pixel_depth(u32 pixelformat)
2004 switch (pixelformat) {
2005 case V4L2_PIX_FMT_YUV420:
2006 case V4L2_PIX_FMT_NV12:
2007 case V4L2_PIX_FMT_NV21:
2008 case V4L2_PIX_FMT_YVU420:
2010 case V4L2_PIX_FMT_YUV422P:
2011 case V4L2_PIX_FMT_YUYV:
2012 case V4L2_PIX_FMT_UYVY:
2013 case V4L2_PIX_FMT_NV16:
2014 case V4L2_PIX_FMT_NV61:
2015 case V4L2_PIX_FMT_RGB565:
2016 case V4L2_PIX_FMT_SBGGR16:
2017 case V4L2_PIX_FMT_SBGGR12:
2018 case V4L2_PIX_FMT_SGBRG12:
2019 case V4L2_PIX_FMT_SGRBG12:
2020 case V4L2_PIX_FMT_SRGGB12:
2021 case V4L2_PIX_FMT_SBGGR10:
2022 case V4L2_PIX_FMT_SGBRG10:
2023 case V4L2_PIX_FMT_SGRBG10:
2024 case V4L2_PIX_FMT_SRGGB10:
2026 case V4L2_PIX_FMT_RGB24:
2027 case V4L2_PIX_FMT_YUV444:
2029 case V4L2_PIX_FMT_RGB32:
2031 case V4L2_PIX_FMT_JPEG:
2032 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
2033 case V4L2_PIX_FMT_SBGGR8:
2034 case V4L2_PIX_FMT_SGBRG8:
2035 case V4L2_PIX_FMT_SGRBG8:
2036 case V4L2_PIX_FMT_SRGGB8:
2039 return 8 * 2; /* raw type now */
2043 bool atomisp_is_mbuscode_raw(uint32_t code)
2045 return code >= 0x3000 && code < 0x4000;
2049 * ISP features control function
2053 * Set ISP capture mode based on current settings
2055 static void atomisp_update_capture_mode(struct atomisp_sub_device *asd)
2057 if (asd->params.gdc_cac_en)
2058 atomisp_css_capture_set_mode(asd, CSS_CAPTURE_MODE_ADVANCED);
2059 else if (asd->params.low_light)
2060 atomisp_css_capture_set_mode(asd, CSS_CAPTURE_MODE_LOW_LIGHT);
2061 else if (asd->video_out_capture.sh_fmt == CSS_FRAME_FORMAT_RAW)
2062 atomisp_css_capture_set_mode(asd, CSS_CAPTURE_MODE_RAW);
2064 atomisp_css_capture_set_mode(asd, CSS_CAPTURE_MODE_PRIMARY);
2068 int atomisp_set_sensor_runmode(struct atomisp_sub_device *asd,
2069 struct atomisp_s_runmode *runmode)
2071 struct atomisp_device *isp = asd->isp;
2072 struct v4l2_ctrl *c;
2075 if (!(runmode && (runmode->mode & RUNMODE_MASK)))
2078 mutex_lock(asd->ctrl_handler.lock);
2079 c = v4l2_ctrl_find(isp->inputs[asd->input_curr].camera->ctrl_handler,
2083 ret = v4l2_ctrl_s_ctrl(c, runmode->mode);
2085 mutex_unlock(asd->ctrl_handler.lock);
2090 * Function to enable/disable lens geometry distortion correction (GDC) and
2091 * chromatic aberration correction (CAC)
2093 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag,
2097 *value = asd->params.gdc_cac_en;
2101 asd->params.gdc_cac_en = !!*value;
2102 if (asd->params.gdc_cac_en) {
2103 atomisp_css_set_morph_table(asd,
2104 asd->params.css_param.morph_table);
2106 atomisp_css_set_morph_table(asd, NULL);
2108 asd->params.css_update_params_needed = true;
2109 atomisp_update_capture_mode(asd);
2114 * Function to enable/disable low light mode including ANR
2116 int atomisp_low_light(struct atomisp_sub_device *asd, int flag,
2120 *value = asd->params.low_light;
2124 asd->params.low_light = (*value != 0);
2125 atomisp_update_capture_mode(asd);
2130 * Function to enable/disable extra noise reduction (XNR) in low light
2133 int atomisp_xnr(struct atomisp_sub_device *asd, int flag,
2137 *xnr_enable = asd->params.xnr_en;
2141 atomisp_css_capture_enable_xnr(asd, !!*xnr_enable);
2147 * Function to configure bayer noise reduction
2149 int atomisp_nr(struct atomisp_sub_device *asd, int flag,
2150 struct atomisp_nr_config *arg)
2153 /* Get nr config from current setup */
2154 if (atomisp_css_get_nr_config(asd, arg))
2157 /* Set nr config to isp parameters */
2158 memcpy(&asd->params.css_param.nr_config, arg,
2159 sizeof(struct atomisp_css_nr_config));
2160 atomisp_css_set_nr_config(asd, &asd->params.css_param.nr_config);
2161 asd->params.css_update_params_needed = true;
2167 * Function to configure temporal noise reduction (TNR)
2169 int atomisp_tnr(struct atomisp_sub_device *asd, int flag,
2170 struct atomisp_tnr_config *config)
2172 /* Get tnr config from current setup */
2174 /* Get tnr config from current setup */
2175 if (atomisp_css_get_tnr_config(asd, config))
2178 /* Set tnr config to isp parameters */
2179 memcpy(&asd->params.css_param.tnr_config, config,
2180 sizeof(struct atomisp_css_tnr_config));
2181 atomisp_css_set_tnr_config(asd, &asd->params.css_param.tnr_config);
2182 asd->params.css_update_params_needed = true;
2189 * Function to configure black level compensation
2191 int atomisp_black_level(struct atomisp_sub_device *asd, int flag,
2192 struct atomisp_ob_config *config)
2195 /* Get ob config from current setup */
2196 if (atomisp_css_get_ob_config(asd, config))
2199 /* Set ob config to isp parameters */
2200 memcpy(&asd->params.css_param.ob_config, config,
2201 sizeof(struct atomisp_css_ob_config));
2202 atomisp_css_set_ob_config(asd, &asd->params.css_param.ob_config);
2203 asd->params.css_update_params_needed = true;
2210 * Function to configure edge enhancement
2212 int atomisp_ee(struct atomisp_sub_device *asd, int flag,
2213 struct atomisp_ee_config *config)
2216 /* Get ee config from current setup */
2217 if (atomisp_css_get_ee_config(asd, config))
2220 /* Set ee config to isp parameters */
2221 memcpy(&asd->params.css_param.ee_config, config,
2222 sizeof(asd->params.css_param.ee_config));
2223 atomisp_css_set_ee_config(asd, &asd->params.css_param.ee_config);
2224 asd->params.css_update_params_needed = true;
2231 * Function to update Gamma table for gamma, brightness and contrast config
2233 int atomisp_gamma(struct atomisp_sub_device *asd, int flag,
2234 struct atomisp_gamma_table *config)
2237 /* Get gamma table from current setup */
2238 if (atomisp_css_get_gamma_table(asd, config))
2241 /* Set gamma table to isp parameters */
2242 memcpy(&asd->params.css_param.gamma_table, config,
2243 sizeof(asd->params.css_param.gamma_table));
2244 atomisp_css_set_gamma_table(asd, &asd->params.css_param.gamma_table);
2251 * Function to update Ctc table for Chroma Enhancement
2253 int atomisp_ctc(struct atomisp_sub_device *asd, int flag,
2254 struct atomisp_ctc_table *config)
2257 /* Get ctc table from current setup */
2258 if (atomisp_css_get_ctc_table(asd, config))
2261 /* Set ctc table to isp parameters */
2262 memcpy(&asd->params.css_param.ctc_table, config,
2263 sizeof(asd->params.css_param.ctc_table));
2264 atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table);
2271 * Function to update gamma correction parameters
2273 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag,
2274 struct atomisp_gc_config *config)
2277 /* Get gamma correction params from current setup */
2278 if (atomisp_css_get_gc_config(asd, config))
2281 /* Set gamma correction params to isp parameters */
2282 memcpy(&asd->params.css_param.gc_config, config,
2283 sizeof(asd->params.css_param.gc_config));
2284 atomisp_css_set_gc_config(asd, &asd->params.css_param.gc_config);
2285 asd->params.css_update_params_needed = true;
2292 * Function to update narrow gamma flag
2294 int atomisp_formats(struct atomisp_sub_device *asd, int flag,
2295 struct atomisp_formats_config *config)
2298 /* Get narrow gamma flag from current setup */
2299 if (atomisp_css_get_formats_config(asd, config))
2302 /* Set narrow gamma flag to isp parameters */
2303 memcpy(&asd->params.css_param.formats_config, config,
2304 sizeof(asd->params.css_param.formats_config));
2305 atomisp_css_set_formats_config(asd, &asd->params.css_param.formats_config);
2311 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd)
2313 atomisp_free_css_parameters(&asd->params.css_param);
2315 if (asd->raw_output_frame) {
2316 atomisp_css_frame_free(asd->raw_output_frame);
2317 asd->raw_output_frame = NULL;
2321 static void atomisp_update_grid_info(struct atomisp_sub_device *asd,
2322 enum atomisp_css_pipe_id pipe_id,
2325 struct atomisp_device *isp = asd->isp;
2327 u16 stream_id = atomisp_source_pad_to_stream_id(asd, source_pad);
2329 if (atomisp_css_get_grid_info(asd, pipe_id, source_pad))
2332 /* We must free all buffers because they no longer match
2334 atomisp_css_free_stat_buffers(asd);
2336 err = atomisp_alloc_css_stat_bufs(asd, stream_id);
2338 dev_err(isp->dev, "stat_buf allocate error\n");
2342 if (atomisp_alloc_3a_output_buf(asd)) {
2343 /* Failure for 3A buffers does not influence DIS buffers */
2344 if (asd->params.s3a_output_bytes != 0) {
2345 /* For SOC sensor happens s3a_output_bytes == 0,
2346 * using if condition to exclude false error log */
2347 dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n");
2352 if (atomisp_alloc_dis_coef_buf(asd)) {
2354 "Failed to allocate memory for DIS statistics\n");
2358 if (atomisp_alloc_metadata_output_buf(asd)) {
2359 dev_err(isp->dev, "Failed to allocate memory for metadata\n");
2366 atomisp_css_free_stat_buffers(asd);
2370 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd,
2371 struct atomisp_grid_info *info)
2373 memcpy(info, &asd->params.curr_grid_info.s3a_grid,
2374 sizeof(struct atomisp_css_3a_grid_info));
2377 int atomisp_compare_grid(struct atomisp_sub_device *asd,
2378 struct atomisp_grid_info *atomgrid)
2380 struct atomisp_grid_info tmp = {0};
2382 atomisp_curr_user_grid_info(asd, &tmp);
2383 return memcmp(atomgrid, &tmp, sizeof(tmp));
2387 * Function to update Gdc table for gdc
2389 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag,
2390 struct atomisp_morph_table *config)
2394 struct atomisp_device *isp = asd->isp;
2397 /* Get gdc table from current setup */
2398 struct atomisp_css_morph_table tab = {0};
2400 atomisp_css_get_morph_table(asd, &tab);
2402 config->width = tab.width;
2403 config->height = tab.height;
2405 for (i = 0; i < CSS_MORPH_TABLE_NUM_PLANES; i++) {
2406 ret = copy_to_user(config->coordinates_x[i],
2407 tab.coordinates_x[i], tab.height *
2408 tab.width * sizeof(*tab.coordinates_x[i]));
2411 "Failed to copy to User for x\n");
2414 ret = copy_to_user(config->coordinates_y[i],
2415 tab.coordinates_y[i], tab.height *
2416 tab.width * sizeof(*tab.coordinates_y[i]));
2419 "Failed to copy to User for y\n");
2424 struct atomisp_css_morph_table *tab =
2425 asd->params.css_param.morph_table;
2427 /* free first if we have one */
2429 atomisp_css_morph_table_free(tab);
2430 asd->params.css_param.morph_table = NULL;
2433 /* allocate new one */
2434 tab = atomisp_css_morph_table_allocate(config->width,
2438 dev_err(isp->dev, "out of memory\n");
2442 for (i = 0; i < CSS_MORPH_TABLE_NUM_PLANES; i++) {
2443 ret = copy_from_user(tab->coordinates_x[i],
2444 config->coordinates_x[i],
2445 config->height * config->width *
2446 sizeof(*config->coordinates_x[i]));
2449 "Failed to copy from User for x, ret %d\n",
2451 atomisp_css_morph_table_free(tab);
2454 ret = copy_from_user(tab->coordinates_y[i],
2455 config->coordinates_y[i],
2456 config->height * config->width *
2457 sizeof(*config->coordinates_y[i]));
2460 "Failed to copy from User for y, ret is %d\n",
2462 atomisp_css_morph_table_free(tab);
2466 asd->params.css_param.morph_table = tab;
2467 if (asd->params.gdc_cac_en)
2468 atomisp_css_set_morph_table(asd, tab);
2474 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag,
2475 struct atomisp_macc_config *config)
2477 struct atomisp_css_macc_table *macc_table;
2479 switch (config->color_effect) {
2480 case V4L2_COLORFX_NONE:
2481 macc_table = &asd->params.css_param.macc_table;
2483 case V4L2_COLORFX_SKY_BLUE:
2484 macc_table = &blue_macc_table;
2486 case V4L2_COLORFX_GRASS_GREEN:
2487 macc_table = &green_macc_table;
2489 case V4L2_COLORFX_SKIN_WHITEN_LOW:
2490 macc_table = &skin_low_macc_table;
2492 case V4L2_COLORFX_SKIN_WHITEN:
2493 macc_table = &skin_medium_macc_table;
2495 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
2496 macc_table = &skin_high_macc_table;
2503 /* Get macc table from current setup */
2504 memcpy(&config->table, macc_table,
2505 sizeof(struct atomisp_css_macc_table));
2507 memcpy(macc_table, &config->table,
2508 sizeof(struct atomisp_css_macc_table));
2509 if (config->color_effect == asd->params.color_effect)
2510 atomisp_css_set_macc_table(asd, macc_table);
2516 int atomisp_set_dis_vector(struct atomisp_sub_device *asd,
2517 struct atomisp_dis_vector *vector)
2519 atomisp_css_video_set_dis_vector(asd, vector);
2521 asd->params.dis_proj_data_valid = false;
2522 asd->params.css_update_params_needed = true;
2527 * Function to set/get image stablization statistics
2529 int atomisp_get_dis_stat(struct atomisp_sub_device *asd,
2530 struct atomisp_dis_statistics *stats)
2532 return atomisp_css_get_dis_stat(asd, stats);
2536 * Function set camrea_prefiles.xml current sensor pixel array size
2538 int atomisp_set_array_res(struct atomisp_sub_device *asd,
2539 struct atomisp_resolution *config)
2541 dev_dbg(asd->isp->dev, ">%s start\n", __func__);
2543 dev_err(asd->isp->dev, "Set sensor array size is not valid\n");
2547 asd->sensor_array_res.width = config->width;
2548 asd->sensor_array_res.height = config->height;
2553 * Function to get DVS2 BQ resolution settings
2555 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd,
2556 struct atomisp_dvs2_bq_resolutions *bq_res)
2558 struct ia_css_pipe_config *pipe_cfg = NULL;
2559 struct ia_css_stream_config *stream_cfg = NULL;
2560 struct ia_css_stream_input_config *input_config = NULL;
2562 struct ia_css_stream *stream =
2563 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
2565 dev_warn(asd->isp->dev, "stream is not created");
2569 pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2570 .pipe_configs[CSS_PIPE_ID_VIDEO];
2571 stream_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2573 input_config = &stream_cfg->input_config;
2578 /* the GDC output resolution */
2579 bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2;
2580 bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2;
2582 bq_res->envelope_bq.width_bq = 0;
2583 bq_res->envelope_bq.height_bq = 0;
2584 /* the GDC input resolution */
2585 if (!asd->continuous_mode->val) {
2586 bq_res->source_bq.width_bq = bq_res->output_bq.width_bq +
2587 pipe_cfg->dvs_envelope.width / 2;
2588 bq_res->source_bq.height_bq = bq_res->output_bq.height_bq +
2589 pipe_cfg->dvs_envelope.height / 2;
2591 * Bad pixels caused by spatial filter processing
2592 * ISP filter resolution should be given by CSS/FW, but for now
2593 * there is not such API to query, and it is fixed value, so
2596 bq_res->ispfilter_bq.width_bq = 12 / 2;
2597 bq_res->ispfilter_bq.height_bq = 12 / 2;
2598 /* spatial filter shift, always 4 pixels */
2599 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2600 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2602 if (asd->params.video_dis_en) {
2603 bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width
2604 / 2 - bq_res->ispfilter_bq.width_bq;
2605 bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height
2606 / 2 - bq_res->ispfilter_bq.height_bq;
2609 unsigned int w_padding;
2610 unsigned int gdc_effective_input = 0;
2613 * gdc_effective_input = effective_input + envelope
2615 * From the comment and formula in BZ1786,
2616 * we see the source_bq should be:
2617 * effective_input / bayer_ds_ratio
2619 bq_res->source_bq.width_bq =
2620 (input_config->effective_res.width *
2621 pipe_cfg->bayer_ds_out_res.width /
2622 input_config->effective_res.width + 1) / 2;
2623 bq_res->source_bq.height_bq =
2624 (input_config->effective_res.height *
2625 pipe_cfg->bayer_ds_out_res.height /
2626 input_config->effective_res.height + 1) / 2;
2628 if (!asd->params.video_dis_en) {
2630 * We adjust the ispfilter_bq to:
2631 * ispfilter_bq = 128/BDS
2632 * we still need firmware team to provide an offical
2635 bq_res->ispfilter_bq.width_bq = 128 *
2636 pipe_cfg->bayer_ds_out_res.width /
2637 input_config->effective_res.width / 2;
2638 bq_res->ispfilter_bq.height_bq = 128 *
2639 pipe_cfg->bayer_ds_out_res.width /
2640 input_config->effective_res.width / 2;
2642 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2643 /* No additional left padding for ISYS2401 */
2644 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2645 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2648 * For the w_padding and gdc_shift_bq cacluation
2649 * Please see the BZ 1786 and 4358 for more info.
2650 * Just test that this formula can work now,
2651 * but we still have no offical formula.
2653 * w_padding = ceiling(gdc_effective_input
2654 * /128, 1) * 128 - effective_width
2655 * gdc_shift_bq = w_padding/BDS/2 + ispfilter_bq/2
2657 gdc_effective_input =
2658 input_config->effective_res.width +
2659 pipe_cfg->dvs_envelope.width;
2660 w_padding = roundup(gdc_effective_input, 128) -
2661 input_config->effective_res.width;
2662 w_padding = w_padding *
2663 pipe_cfg->bayer_ds_out_res.width /
2664 input_config->effective_res.width + 1;
2665 w_padding = roundup(w_padding / 2, 1);
2667 bq_res->gdc_shift_bq.width_bq = bq_res->ispfilter_bq.width_bq / 2
2669 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2672 unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
2674 bq_res->ispfilter_bq.width_bq = 8 / 2;
2675 bq_res->ispfilter_bq.height_bq = 8 / 2;
2677 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2678 /* No additional left padding for ISYS2401 */
2679 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2680 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2683 roundup(input_config->effective_res.width, 128) -
2684 input_config->effective_res.width;
2687 bq_res->gdc_shift_bq.width_bq = 4 / 2 +
2689 pipe_cfg->bayer_ds_out_res.width /
2690 input_config->effective_res.width + 1) / 2;
2691 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2694 dvs_w = pipe_cfg->bayer_ds_out_res.width -
2695 pipe_cfg->output_info[0].res.width;
2696 dvs_h = pipe_cfg->bayer_ds_out_res.height -
2697 pipe_cfg->output_info[0].res.height;
2698 dvs_w_max = rounddown(
2699 pipe_cfg->output_info[0].res.width / 5,
2700 ATOM_ISP_STEP_WIDTH);
2701 dvs_h_max = rounddown(
2702 pipe_cfg->output_info[0].res.height / 5,
2703 ATOM_ISP_STEP_HEIGHT);
2704 bq_res->envelope_bq.width_bq =
2705 min((dvs_w / 2), (dvs_w_max / 2)) -
2706 bq_res->ispfilter_bq.width_bq;
2707 bq_res->envelope_bq.height_bq =
2708 min((dvs_h / 2), (dvs_h_max / 2)) -
2709 bq_res->ispfilter_bq.height_bq;
2713 dev_dbg(asd->isp->dev,
2714 "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",
2715 bq_res->source_bq.width_bq, bq_res->source_bq.height_bq,
2716 bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq,
2717 bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq,
2718 bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq,
2719 bq_res->output_bq.width_bq, bq_res->output_bq.height_bq);
2724 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd,
2725 struct atomisp_dis_coefficients *coefs)
2727 return atomisp_css_set_dis_coefs(asd, coefs);
2731 * Function to set/get 3A stat from isp
2733 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag,
2734 struct atomisp_3a_statistics *config)
2736 struct atomisp_device *isp = asd->isp;
2737 struct atomisp_s3a_buf *s3a_buf;
2743 /* sanity check to avoid writing into unallocated memory. */
2744 if (asd->params.s3a_output_bytes == 0)
2747 if (atomisp_compare_grid(asd, &config->grid_info) != 0) {
2748 /* If the grid info in the argument differs from the current
2749 grid info, we tell the caller to reset the grid size and
2754 if (list_empty(&asd->s3a_stats_ready)) {
2755 dev_err(isp->dev, "3a statistics is not valid.\n");
2759 s3a_buf = list_entry(asd->s3a_stats_ready.next,
2760 struct atomisp_s3a_buf, list);
2761 if (s3a_buf->s3a_map)
2762 ia_css_translate_3a_statistics(
2763 asd->params.s3a_user_stat, s3a_buf->s3a_map);
2765 ia_css_get_3a_statistics(asd->params.s3a_user_stat,
2768 config->exp_id = s3a_buf->s3a_data->exp_id;
2769 config->isp_config_id = s3a_buf->s3a_data->isp_config_id;
2771 ret = copy_to_user(config->data, asd->params.s3a_user_stat->data,
2772 asd->params.s3a_output_bytes);
2774 dev_err(isp->dev, "copy to user failed: copied %lu bytes\n",
2779 /* Move to free buffer list */
2780 list_del_init(&s3a_buf->list);
2781 list_add_tail(&s3a_buf->list, &asd->s3a_stats);
2782 dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n",
2784 config->exp_id, config->isp_config_id);
2788 int atomisp_get_metadata(struct atomisp_sub_device *asd, int flag,
2789 struct atomisp_metadata *md)
2791 struct atomisp_device *isp = asd->isp;
2792 struct ia_css_stream_config *stream_config;
2793 struct ia_css_stream_info *stream_info;
2794 struct camera_mipi_info *mipi_info;
2795 struct atomisp_metadata_buf *md_buf;
2796 enum atomisp_metadata_type md_type = ATOMISP_MAIN_METADATA;
2802 stream_config = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2804 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2807 /* We always return the resolution and stride even if there is
2808 * no valid metadata. This allows the caller to get the information
2809 * needed to allocate user-space buffers. */
2810 md->width = stream_info->metadata_info.resolution.width;
2811 md->height = stream_info->metadata_info.resolution.height;
2812 md->stride = stream_info->metadata_info.stride;
2814 /* sanity check to avoid writing into unallocated memory.
2815 * This does not return an error because it is a valid way
2816 * for applications to detect that metadata is not enabled. */
2817 if (md->width == 0 || md->height == 0 || !md->data)
2820 /* This is done in the atomisp_buf_done() */
2821 if (list_empty(&asd->metadata_ready[md_type])) {
2822 dev_warn(isp->dev, "Metadata queue is empty now!\n");
2826 mipi_info = atomisp_to_sensor_mipi_info(
2827 isp->inputs[asd->input_curr].camera);
2831 if (mipi_info->metadata_effective_width) {
2832 for (i = 0; i < md->height; i++)
2833 md->effective_width[i] =
2834 mipi_info->metadata_effective_width[i];
2837 md_buf = list_entry(asd->metadata_ready[md_type].next,
2838 struct atomisp_metadata_buf, list);
2839 md->exp_id = md_buf->metadata->exp_id;
2840 if (md_buf->md_vptr) {
2841 ret = copy_to_user(md->data,
2843 stream_info->metadata_info.size);
2845 hmm_load(md_buf->metadata->address,
2846 asd->params.metadata_user[md_type],
2847 stream_info->metadata_info.size);
2849 ret = copy_to_user(md->data,
2850 asd->params.metadata_user[md_type],
2851 stream_info->metadata_info.size);
2854 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2859 list_del_init(&md_buf->list);
2860 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2862 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2863 __func__, md_type, md->exp_id);
2867 int atomisp_get_metadata_by_type(struct atomisp_sub_device *asd, int flag,
2868 struct atomisp_metadata_with_type *md)
2870 struct atomisp_device *isp = asd->isp;
2871 struct ia_css_stream_config *stream_config;
2872 struct ia_css_stream_info *stream_info;
2873 struct camera_mipi_info *mipi_info;
2874 struct atomisp_metadata_buf *md_buf;
2875 enum atomisp_metadata_type md_type;
2881 stream_config = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2883 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2886 /* We always return the resolution and stride even if there is
2887 * no valid metadata. This allows the caller to get the information
2888 * needed to allocate user-space buffers. */
2889 md->width = stream_info->metadata_info.resolution.width;
2890 md->height = stream_info->metadata_info.resolution.height;
2891 md->stride = stream_info->metadata_info.stride;
2893 /* sanity check to avoid writing into unallocated memory.
2894 * This does not return an error because it is a valid way
2895 * for applications to detect that metadata is not enabled. */
2896 if (md->width == 0 || md->height == 0 || !md->data)
2900 if (md_type < 0 || md_type >= ATOMISP_METADATA_TYPE_NUM)
2903 /* This is done in the atomisp_buf_done() */
2904 if (list_empty(&asd->metadata_ready[md_type])) {
2905 dev_warn(isp->dev, "Metadata queue is empty now!\n");
2909 mipi_info = atomisp_to_sensor_mipi_info(
2910 isp->inputs[asd->input_curr].camera);
2914 if (mipi_info->metadata_effective_width) {
2915 for (i = 0; i < md->height; i++)
2916 md->effective_width[i] =
2917 mipi_info->metadata_effective_width[i];
2920 md_buf = list_entry(asd->metadata_ready[md_type].next,
2921 struct atomisp_metadata_buf, list);
2922 md->exp_id = md_buf->metadata->exp_id;
2923 if (md_buf->md_vptr) {
2924 ret = copy_to_user(md->data,
2926 stream_info->metadata_info.size);
2928 hmm_load(md_buf->metadata->address,
2929 asd->params.metadata_user[md_type],
2930 stream_info->metadata_info.size);
2932 ret = copy_to_user(md->data,
2933 asd->params.metadata_user[md_type],
2934 stream_info->metadata_info.size);
2937 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2941 list_del_init(&md_buf->list);
2942 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2944 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2945 __func__, md_type, md->exp_id);
2950 * Function to calculate real zoom region for every pipe
2952 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd,
2953 struct ia_css_dz_config *dz_config,
2954 enum atomisp_css_pipe_id css_pipe_id)
2957 struct atomisp_stream_env *stream_env =
2958 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL];
2959 struct atomisp_resolution eff_res, out_res;
2960 int w_offset, h_offset;
2962 memset(&eff_res, 0, sizeof(eff_res));
2963 memset(&out_res, 0, sizeof(out_res));
2965 if (dz_config->dx || dz_config->dy)
2968 if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW
2969 && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) {
2970 dev_err(asd->isp->dev, "%s the set pipe no support crop region"
2976 stream_env->stream_config.input_config.effective_res.width;
2978 stream_env->stream_config.input_config.effective_res.height;
2979 if (eff_res.width == 0 || eff_res.height == 0) {
2980 dev_err(asd->isp->dev, "%s err effective resolution"
2985 if (dz_config->zoom_region.resolution.width
2986 == asd->sensor_array_res.width
2987 || dz_config->zoom_region.resolution.height
2988 == asd->sensor_array_res.height) {
2989 /*no need crop region*/
2990 dz_config->zoom_region.origin.x = 0;
2991 dz_config->zoom_region.origin.y = 0;
2992 dz_config->zoom_region.resolution.width = eff_res.width;
2993 dz_config->zoom_region.resolution.height = eff_res.height;
2998 * This is not the correct implementation with Google's definition, due
2999 * to firmware limitation.
3000 * map real crop region base on above calculating base max crop region.
3003 if (!atomisp_hw_is_isp2401) {
3004 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
3006 / asd->sensor_array_res.width;
3007 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
3009 / asd->sensor_array_res.height;
3010 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
3012 / asd->sensor_array_res.width;
3013 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
3015 / asd->sensor_array_res.height;
3017 * Set same ratio of crop region resolution and current pipe output
3020 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
3021 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
3022 if (out_res.width == 0 || out_res.height == 0) {
3023 dev_err(asd->isp->dev, "%s err current pipe output resolution"
3028 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
3029 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
3030 if (out_res.width == 0 || out_res.height == 0) {
3031 dev_err(asd->isp->dev, "%s err current pipe output resolution"
3036 if (asd->sensor_array_res.width * out_res.height
3037 < out_res.width * asd->sensor_array_res.height) {
3038 h_offset = asd->sensor_array_res.height
3039 - asd->sensor_array_res.width
3040 * out_res.height / out_res.width;
3041 h_offset = h_offset / 2;
3042 if (dz_config->zoom_region.origin.y < h_offset)
3043 dz_config->zoom_region.origin.y = 0;
3045 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset;
3048 w_offset = asd->sensor_array_res.width
3049 - asd->sensor_array_res.height
3050 * out_res.width / out_res.height;
3051 w_offset = w_offset / 2;
3052 if (dz_config->zoom_region.origin.x < w_offset)
3053 dz_config->zoom_region.origin.x = 0;
3055 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset;
3058 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
3060 / (asd->sensor_array_res.width - 2 * w_offset);
3061 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
3063 / (asd->sensor_array_res.height - 2 * h_offset);
3064 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
3066 / (asd->sensor_array_res.width - 2 * w_offset);
3067 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
3069 / (asd->sensor_array_res.height - 2 * h_offset);
3072 if (out_res.width * dz_config->zoom_region.resolution.height
3073 > dz_config->zoom_region.resolution.width * out_res.height) {
3074 dz_config->zoom_region.resolution.height =
3075 dz_config->zoom_region.resolution.width
3076 * out_res.height / out_res.width;
3078 dz_config->zoom_region.resolution.width =
3079 dz_config->zoom_region.resolution.height
3080 * out_res.width / out_res.height;
3082 dev_dbg(asd->isp->dev,
3083 "%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n",
3084 __func__, dz_config->zoom_region.origin.x,
3085 dz_config->zoom_region.origin.y,
3086 dz_config->zoom_region.resolution.width,
3087 dz_config->zoom_region.resolution.height,
3088 eff_res.width, eff_res.height,
3089 asd->sensor_array_res.width,
3090 asd->sensor_array_res.height,
3091 out_res.width, out_res.height);
3093 if ((dz_config->zoom_region.origin.x +
3094 dz_config->zoom_region.resolution.width
3096 (dz_config->zoom_region.origin.y +
3097 dz_config->zoom_region.resolution.height
3105 * Function to check the zoom region whether is effective
3107 static bool atomisp_check_zoom_region(
3108 struct atomisp_sub_device *asd,
3109 struct ia_css_dz_config *dz_config)
3111 struct atomisp_resolution config;
3115 memset(&config, 0, sizeof(struct atomisp_resolution));
3117 if (dz_config->dx && dz_config->dy)
3120 config.width = asd->sensor_array_res.width;
3121 config.height = asd->sensor_array_res.height;
3122 w = dz_config->zoom_region.origin.x +
3123 dz_config->zoom_region.resolution.width;
3124 h = dz_config->zoom_region.origin.y +
3125 dz_config->zoom_region.resolution.height;
3127 if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0)
3130 /* setting error zoom region */
3131 dev_err(asd->isp->dev,
3132 "%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n",
3133 __func__, dz_config->zoom_region.origin.x,
3134 dz_config->zoom_region.origin.y,
3135 dz_config->zoom_region.resolution.width,
3136 dz_config->zoom_region.resolution.height,
3137 config.width, config.height);
3142 void atomisp_apply_css_parameters(
3143 struct atomisp_sub_device *asd,
3144 struct atomisp_css_params *css_param)
3146 if (css_param->update_flag.wb_config)
3147 atomisp_css_set_wb_config(asd, &css_param->wb_config);
3149 if (css_param->update_flag.ob_config)
3150 atomisp_css_set_ob_config(asd, &css_param->ob_config);
3152 if (css_param->update_flag.dp_config)
3153 atomisp_css_set_dp_config(asd, &css_param->dp_config);
3155 if (css_param->update_flag.nr_config)
3156 atomisp_css_set_nr_config(asd, &css_param->nr_config);
3158 if (css_param->update_flag.ee_config)
3159 atomisp_css_set_ee_config(asd, &css_param->ee_config);
3161 if (css_param->update_flag.tnr_config)
3162 atomisp_css_set_tnr_config(asd, &css_param->tnr_config);
3164 if (css_param->update_flag.a3a_config)
3165 atomisp_css_set_3a_config(asd, &css_param->s3a_config);
3167 if (css_param->update_flag.ctc_config)
3168 atomisp_css_set_ctc_config(asd, &css_param->ctc_config);
3170 if (css_param->update_flag.cnr_config)
3171 atomisp_css_set_cnr_config(asd, &css_param->cnr_config);
3173 if (css_param->update_flag.ecd_config)
3174 atomisp_css_set_ecd_config(asd, &css_param->ecd_config);
3176 if (css_param->update_flag.ynr_config)
3177 atomisp_css_set_ynr_config(asd, &css_param->ynr_config);
3179 if (css_param->update_flag.fc_config)
3180 atomisp_css_set_fc_config(asd, &css_param->fc_config);
3182 if (css_param->update_flag.macc_config)
3183 atomisp_css_set_macc_config(asd, &css_param->macc_config);
3185 if (css_param->update_flag.aa_config)
3186 atomisp_css_set_aa_config(asd, &css_param->aa_config);
3188 if (css_param->update_flag.anr_config)
3189 atomisp_css_set_anr_config(asd, &css_param->anr_config);
3191 if (css_param->update_flag.xnr_config)
3192 atomisp_css_set_xnr_config(asd, &css_param->xnr_config);
3194 if (css_param->update_flag.yuv2rgb_cc_config)
3195 atomisp_css_set_yuv2rgb_cc_config(asd,
3196 &css_param->yuv2rgb_cc_config);
3198 if (css_param->update_flag.rgb2yuv_cc_config)
3199 atomisp_css_set_rgb2yuv_cc_config(asd,
3200 &css_param->rgb2yuv_cc_config);
3202 if (css_param->update_flag.macc_table)
3203 atomisp_css_set_macc_table(asd, &css_param->macc_table);
3205 if (css_param->update_flag.xnr_table)
3206 atomisp_css_set_xnr_table(asd, &css_param->xnr_table);
3208 if (css_param->update_flag.r_gamma_table)
3209 atomisp_css_set_r_gamma_table(asd, &css_param->r_gamma_table);
3211 if (css_param->update_flag.g_gamma_table)
3212 atomisp_css_set_g_gamma_table(asd, &css_param->g_gamma_table);
3214 if (css_param->update_flag.b_gamma_table)
3215 atomisp_css_set_b_gamma_table(asd, &css_param->b_gamma_table);
3217 if (css_param->update_flag.anr_thres)
3218 atomisp_css_set_anr_thres(asd, &css_param->anr_thres);
3220 if (css_param->update_flag.shading_table)
3221 atomisp_css_set_shading_table(asd, css_param->shading_table);
3223 if (css_param->update_flag.morph_table && asd->params.gdc_cac_en)
3224 atomisp_css_set_morph_table(asd, css_param->morph_table);
3226 if (css_param->update_flag.dvs2_coefs) {
3227 struct atomisp_css_dvs_grid_info *dvs_grid_info =
3228 atomisp_css_get_dvs_grid_info(
3229 &asd->params.curr_grid_info);
3231 if (dvs_grid_info && dvs_grid_info->enable)
3232 atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff);
3235 if (css_param->update_flag.dvs_6axis_config)
3236 atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis);
3238 atomisp_css_set_isp_config_id(asd, css_param->isp_config_id);
3240 * These configurations are on used by ISP1.x, not for ISP2.x,
3241 * so do not handle them. see comments of ia_css_isp_config.
3252 static unsigned int long copy_from_compatible(void *to, const void *from,
3253 unsigned long n, bool from_user)
3256 return copy_from_user(to, (void __user *)from, n);
3258 memcpy(to, from, n);
3262 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd,
3263 struct atomisp_parameters *arg,
3264 struct atomisp_css_params *css_param,
3267 struct atomisp_parameters *cur_config = &css_param->update_flag;
3269 if (!arg || !asd || !css_param)
3272 if (arg->wb_config && (from_user || !cur_config->wb_config)) {
3273 if (copy_from_compatible(&css_param->wb_config, arg->wb_config,
3274 sizeof(struct atomisp_css_wb_config),
3277 css_param->update_flag.wb_config =
3278 (struct atomisp_wb_config *)&css_param->wb_config;
3281 if (arg->ob_config && (from_user || !cur_config->ob_config)) {
3282 if (copy_from_compatible(&css_param->ob_config, arg->ob_config,
3283 sizeof(struct atomisp_css_ob_config),
3286 css_param->update_flag.ob_config =
3287 (struct atomisp_ob_config *)&css_param->ob_config;
3290 if (arg->dp_config && (from_user || !cur_config->dp_config)) {
3291 if (copy_from_compatible(&css_param->dp_config, arg->dp_config,
3292 sizeof(struct atomisp_css_dp_config),
3295 css_param->update_flag.dp_config =
3296 (struct atomisp_dp_config *)&css_param->dp_config;
3299 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
3300 if (arg->dz_config && (from_user || !cur_config->dz_config)) {
3301 if (copy_from_compatible(&css_param->dz_config,
3303 sizeof(struct atomisp_css_dz_config),
3306 if (!atomisp_check_zoom_region(asd,
3307 &css_param->dz_config)) {
3308 dev_err(asd->isp->dev, "crop region error!");
3311 css_param->update_flag.dz_config =
3312 (struct atomisp_dz_config *)
3313 &css_param->dz_config;
3317 if (arg->nr_config && (from_user || !cur_config->nr_config)) {
3318 if (copy_from_compatible(&css_param->nr_config, arg->nr_config,
3319 sizeof(struct atomisp_css_nr_config),
3322 css_param->update_flag.nr_config =
3323 (struct atomisp_nr_config *)&css_param->nr_config;
3326 if (arg->ee_config && (from_user || !cur_config->ee_config)) {
3327 if (copy_from_compatible(&css_param->ee_config, arg->ee_config,
3328 sizeof(struct atomisp_css_ee_config),
3331 css_param->update_flag.ee_config =
3332 (struct atomisp_ee_config *)&css_param->ee_config;
3335 if (arg->tnr_config && (from_user || !cur_config->tnr_config)) {
3336 if (copy_from_compatible(&css_param->tnr_config,
3338 sizeof(struct atomisp_css_tnr_config),
3341 css_param->update_flag.tnr_config =
3342 (struct atomisp_tnr_config *)
3343 &css_param->tnr_config;
3346 if (arg->a3a_config && (from_user || !cur_config->a3a_config)) {
3347 if (copy_from_compatible(&css_param->s3a_config,
3349 sizeof(struct atomisp_css_3a_config),
3352 css_param->update_flag.a3a_config =
3353 (struct atomisp_3a_config *)&css_param->s3a_config;
3356 if (arg->ctc_config && (from_user || !cur_config->ctc_config)) {
3357 if (copy_from_compatible(&css_param->ctc_config,
3359 sizeof(struct atomisp_css_ctc_config),
3362 css_param->update_flag.ctc_config =
3363 (struct atomisp_ctc_config *)
3364 &css_param->ctc_config;
3367 if (arg->cnr_config && (from_user || !cur_config->cnr_config)) {
3368 if (copy_from_compatible(&css_param->cnr_config,
3370 sizeof(struct atomisp_css_cnr_config),
3373 css_param->update_flag.cnr_config =
3374 (struct atomisp_cnr_config *)
3375 &css_param->cnr_config;
3378 if (arg->ecd_config && (from_user || !cur_config->ecd_config)) {
3379 if (copy_from_compatible(&css_param->ecd_config,
3381 sizeof(struct atomisp_css_ecd_config),
3384 css_param->update_flag.ecd_config =
3385 (struct atomisp_ecd_config *)
3386 &css_param->ecd_config;
3389 if (arg->ynr_config && (from_user || !cur_config->ynr_config)) {
3390 if (copy_from_compatible(&css_param->ynr_config,
3392 sizeof(struct atomisp_css_ynr_config),
3395 css_param->update_flag.ynr_config =
3396 (struct atomisp_ynr_config *)
3397 &css_param->ynr_config;
3400 if (arg->fc_config && (from_user || !cur_config->fc_config)) {
3401 if (copy_from_compatible(&css_param->fc_config,
3403 sizeof(struct atomisp_css_fc_config),
3406 css_param->update_flag.fc_config =
3407 (struct atomisp_fc_config *)&css_param->fc_config;
3410 if (arg->macc_config && (from_user || !cur_config->macc_config)) {
3411 if (copy_from_compatible(&css_param->macc_config,
3413 sizeof(struct atomisp_css_macc_config),
3416 css_param->update_flag.macc_config =
3417 (struct atomisp_macc_config *)
3418 &css_param->macc_config;
3421 if (arg->aa_config && (from_user || !cur_config->aa_config)) {
3422 if (copy_from_compatible(&css_param->aa_config, arg->aa_config,
3423 sizeof(struct atomisp_css_aa_config),
3426 css_param->update_flag.aa_config =
3427 (struct atomisp_aa_config *)&css_param->aa_config;
3430 if (arg->anr_config && (from_user || !cur_config->anr_config)) {
3431 if (copy_from_compatible(&css_param->anr_config,
3433 sizeof(struct atomisp_css_anr_config),
3436 css_param->update_flag.anr_config =
3437 (struct atomisp_anr_config *)
3438 &css_param->anr_config;
3441 if (arg->xnr_config && (from_user || !cur_config->xnr_config)) {
3442 if (copy_from_compatible(&css_param->xnr_config,
3444 sizeof(struct atomisp_css_xnr_config),
3447 css_param->update_flag.xnr_config =
3448 (struct atomisp_xnr_config *)
3449 &css_param->xnr_config;
3452 if (arg->yuv2rgb_cc_config &&
3453 (from_user || !cur_config->yuv2rgb_cc_config)) {
3454 if (copy_from_compatible(&css_param->yuv2rgb_cc_config,
3455 arg->yuv2rgb_cc_config,
3456 sizeof(struct atomisp_css_cc_config),
3459 css_param->update_flag.yuv2rgb_cc_config =
3460 (struct atomisp_cc_config *)
3461 &css_param->yuv2rgb_cc_config;
3464 if (arg->rgb2yuv_cc_config &&
3465 (from_user || !cur_config->rgb2yuv_cc_config)) {
3466 if (copy_from_compatible(&css_param->rgb2yuv_cc_config,
3467 arg->rgb2yuv_cc_config,
3468 sizeof(struct atomisp_css_cc_config),
3471 css_param->update_flag.rgb2yuv_cc_config =
3472 (struct atomisp_cc_config *)
3473 &css_param->rgb2yuv_cc_config;
3476 if (arg->macc_table && (from_user || !cur_config->macc_table)) {
3477 if (copy_from_compatible(&css_param->macc_table,
3479 sizeof(struct atomisp_css_macc_table),
3482 css_param->update_flag.macc_table =
3483 (struct atomisp_macc_table *)
3484 &css_param->macc_table;
3487 if (arg->xnr_table && (from_user || !cur_config->xnr_table)) {
3488 if (copy_from_compatible(&css_param->xnr_table,
3490 sizeof(struct atomisp_css_xnr_table),
3493 css_param->update_flag.xnr_table =
3494 (struct atomisp_xnr_table *)&css_param->xnr_table;
3497 if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) {
3498 if (copy_from_compatible(&css_param->r_gamma_table,
3500 sizeof(struct atomisp_css_rgb_gamma_table),
3503 css_param->update_flag.r_gamma_table =
3504 (struct atomisp_rgb_gamma_table *)
3505 &css_param->r_gamma_table;
3508 if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) {
3509 if (copy_from_compatible(&css_param->g_gamma_table,
3511 sizeof(struct atomisp_css_rgb_gamma_table),
3514 css_param->update_flag.g_gamma_table =
3515 (struct atomisp_rgb_gamma_table *)
3516 &css_param->g_gamma_table;
3519 if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) {
3520 if (copy_from_compatible(&css_param->b_gamma_table,
3522 sizeof(struct atomisp_css_rgb_gamma_table),
3525 css_param->update_flag.b_gamma_table =
3526 (struct atomisp_rgb_gamma_table *)
3527 &css_param->b_gamma_table;
3530 if (arg->anr_thres && (from_user || !cur_config->anr_thres)) {
3531 if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres,
3532 sizeof(struct atomisp_css_anr_thres),
3535 css_param->update_flag.anr_thres =
3536 (struct atomisp_anr_thres *)&css_param->anr_thres;
3540 css_param->isp_config_id = arg->isp_config_id;
3542 * These configurations are on used by ISP1.x, not for ISP2.x,
3543 * so do not handle them. see comments of ia_css_isp_config.
3555 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd,
3556 struct atomisp_shading_table *source_st,
3557 struct atomisp_css_params *css_param,
3561 unsigned int len_table;
3562 struct atomisp_css_shading_table *shading_table;
3563 struct atomisp_css_shading_table *old_table;
3564 struct atomisp_shading_table *st, dest_st;
3572 if (!from_user && css_param->update_flag.shading_table)
3575 if (atomisp_hw_is_isp2401) {
3576 if (copy_from_compatible(&dest_st, source_st,
3577 sizeof(struct atomisp_shading_table),
3579 dev_err(asd->isp->dev, "copy shading table failed!");
3587 old_table = css_param->shading_table;
3589 /* user config is to disable the shading table. */
3591 /* Generate a minimum table with enable = 0. */
3592 shading_table = atomisp_css_shading_table_alloc(1, 1);
3595 shading_table->enable = 0;
3599 /* Setting a new table. Validate first - all tables must be set */
3600 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3602 dev_err(asd->isp->dev, "shading table validate failed");
3607 /* Shading table size per color */
3608 if (!atomisp_hw_is_isp2401) {
3609 if (st->width > ISP2400_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
3610 st->height > ISP2400_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
3611 dev_err(asd->isp->dev, "shading table w/h validate failed!");
3615 if (st->width > ISP2401_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
3616 st->height > ISP2401_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
3617 dev_err(asd->isp->dev, "shading table w/h validate failed!");
3622 shading_table = atomisp_css_shading_table_alloc(st->width, st->height);
3626 len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE;
3627 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3628 if (copy_from_compatible(shading_table->data[i],
3629 st->data[i], len_table, from_user)) {
3630 atomisp_css_shading_table_free(shading_table);
3634 shading_table->sensor_width = st->sensor_width;
3635 shading_table->sensor_height = st->sensor_height;
3636 shading_table->fraction_bits = st->fraction_bits;
3637 shading_table->enable = st->enable;
3639 /* No need to update shading table if it is the same */
3641 old_table->sensor_width == shading_table->sensor_width &&
3642 old_table->sensor_height == shading_table->sensor_height &&
3643 old_table->width == shading_table->width &&
3644 old_table->height == shading_table->height &&
3645 old_table->fraction_bits == shading_table->fraction_bits &&
3646 old_table->enable == shading_table->enable) {
3647 bool data_is_same = true;
3649 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3650 if (memcmp(shading_table->data[i], old_table->data[i],
3652 data_is_same = false;
3658 atomisp_css_shading_table_free(shading_table);
3664 /* set LSC to CSS */
3665 css_param->shading_table = shading_table;
3666 css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table;
3667 asd->params.sc_en = shading_table;
3670 atomisp_css_shading_table_free(old_table);
3675 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd,
3676 struct ia_css_dvs2_coefficients *coefs,
3677 struct atomisp_css_params *css_param,
3680 struct atomisp_css_dvs_grid_info *cur =
3681 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3682 int dvs_hor_coef_bytes, dvs_ver_coef_bytes;
3683 struct ia_css_dvs2_coefficients dvs2_coefs;
3688 if (!from_user && css_param->update_flag.dvs2_coefs)
3691 if (!atomisp_hw_is_isp2401) {
3692 if (sizeof(*cur) != sizeof(coefs->grid) ||
3693 memcmp(&coefs->grid, cur, sizeof(coefs->grid))) {
3694 dev_err(asd->isp->dev, "dvs grid mis-match!\n");
3695 /* If the grid info in the argument differs from the current
3696 grid info, we tell the caller to reset the grid size and
3701 if (!coefs->hor_coefs.odd_real ||
3702 !coefs->hor_coefs.odd_imag ||
3703 !coefs->hor_coefs.even_real ||
3704 !coefs->hor_coefs.even_imag ||
3705 !coefs->ver_coefs.odd_real ||
3706 !coefs->ver_coefs.odd_imag ||
3707 !coefs->ver_coefs.even_real ||
3708 !coefs->ver_coefs.even_imag)
3711 if (!css_param->dvs2_coeff) {
3712 /* DIS coefficients. */
3713 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3714 if (!css_param->dvs2_coeff)
3718 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3719 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3720 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3721 coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3722 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3723 coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3724 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3725 coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3726 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3727 coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3728 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3729 coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3730 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3731 coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3732 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3733 coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3734 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3735 coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3736 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3737 css_param->dvs2_coeff = NULL;
3741 if (copy_from_compatible(&dvs2_coefs, coefs,
3742 sizeof(struct ia_css_dvs2_coefficients),
3744 dev_err(asd->isp->dev, "copy dvs2 coef failed");
3748 if (sizeof(*cur) != sizeof(dvs2_coefs.grid) ||
3749 memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) {
3750 dev_err(asd->isp->dev, "dvs grid mis-match!\n");
3751 /* If the grid info in the argument differs from the current
3752 grid info, we tell the caller to reset the grid size and
3757 if (!dvs2_coefs.hor_coefs.odd_real ||
3758 !dvs2_coefs.hor_coefs.odd_imag ||
3759 !dvs2_coefs.hor_coefs.even_real ||
3760 !dvs2_coefs.hor_coefs.even_imag ||
3761 !dvs2_coefs.ver_coefs.odd_real ||
3762 !dvs2_coefs.ver_coefs.odd_imag ||
3763 !dvs2_coefs.ver_coefs.even_real ||
3764 !dvs2_coefs.ver_coefs.even_imag)
3767 if (!css_param->dvs2_coeff) {
3768 /* DIS coefficients. */
3769 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3770 if (!css_param->dvs2_coeff)
3774 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3775 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3776 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3777 dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3778 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3779 dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3780 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3781 dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3782 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3783 dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3784 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3785 dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3786 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3787 dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3788 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3789 dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3790 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3791 dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3792 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3793 css_param->dvs2_coeff = NULL;
3798 css_param->update_flag.dvs2_coefs =
3799 (struct atomisp_dvs2_coefficients *)css_param->dvs2_coeff;
3803 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd,
3804 struct atomisp_dvs_6axis_config *source_6axis_config,
3805 struct atomisp_css_params *css_param,
3808 struct atomisp_css_dvs_6axis_config *dvs_6axis_config;
3809 struct atomisp_css_dvs_6axis_config *old_6axis_config;
3810 struct ia_css_stream *stream =
3811 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
3812 struct atomisp_css_dvs_grid_info *dvs_grid_info =
3813 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3817 dev_err(asd->isp->dev, "%s: internal error!", __func__);
3821 if (!source_6axis_config || !dvs_grid_info)
3824 if (!dvs_grid_info->enable)
3827 if (!from_user && css_param->update_flag.dvs_6axis_config)
3830 /* check whether need to reallocate for 6 axis config */
3831 old_6axis_config = css_param->dvs_6axis;
3832 dvs_6axis_config = old_6axis_config;
3834 if (atomisp_hw_is_isp2401) {
3835 struct atomisp_css_dvs_6axis_config t_6axis_config;
3837 if (copy_from_compatible(&t_6axis_config, source_6axis_config,
3838 sizeof(struct atomisp_dvs_6axis_config),
3840 dev_err(asd->isp->dev, "copy morph table failed!");
3844 if (old_6axis_config &&
3845 (old_6axis_config->width_y != t_6axis_config.width_y ||
3846 old_6axis_config->height_y != t_6axis_config.height_y ||
3847 old_6axis_config->width_uv != t_6axis_config.width_uv ||
3848 old_6axis_config->height_uv != t_6axis_config.height_uv)) {
3849 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3850 css_param->dvs_6axis = NULL;
3852 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3853 if (!dvs_6axis_config)
3855 } else if (!dvs_6axis_config) {
3856 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3857 if (!dvs_6axis_config)
3861 dvs_6axis_config->exp_id = t_6axis_config.exp_id;
3863 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3864 t_6axis_config.xcoords_y,
3865 t_6axis_config.width_y *
3866 t_6axis_config.height_y *
3867 sizeof(*dvs_6axis_config->xcoords_y),
3870 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3871 t_6axis_config.ycoords_y,
3872 t_6axis_config.width_y *
3873 t_6axis_config.height_y *
3874 sizeof(*dvs_6axis_config->ycoords_y),
3877 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3878 t_6axis_config.xcoords_uv,
3879 t_6axis_config.width_uv *
3880 t_6axis_config.height_uv *
3881 sizeof(*dvs_6axis_config->xcoords_uv),
3884 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3885 t_6axis_config.ycoords_uv,
3886 t_6axis_config.width_uv *
3887 t_6axis_config.height_uv *
3888 sizeof(*dvs_6axis_config->ycoords_uv),
3892 if (old_6axis_config &&
3893 (old_6axis_config->width_y != source_6axis_config->width_y ||
3894 old_6axis_config->height_y != source_6axis_config->height_y ||
3895 old_6axis_config->width_uv != source_6axis_config->width_uv ||
3896 old_6axis_config->height_uv != source_6axis_config->height_uv)) {
3897 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3898 css_param->dvs_6axis = NULL;
3900 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3901 if (!dvs_6axis_config)
3903 } else if (!dvs_6axis_config) {
3904 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3905 if (!dvs_6axis_config)
3909 dvs_6axis_config->exp_id = source_6axis_config->exp_id;
3911 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3912 source_6axis_config->xcoords_y,
3913 source_6axis_config->width_y *
3914 source_6axis_config->height_y *
3915 sizeof(*source_6axis_config->xcoords_y),
3918 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3919 source_6axis_config->ycoords_y,
3920 source_6axis_config->width_y *
3921 source_6axis_config->height_y *
3922 sizeof(*source_6axis_config->ycoords_y),
3925 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3926 source_6axis_config->xcoords_uv,
3927 source_6axis_config->width_uv *
3928 source_6axis_config->height_uv *
3929 sizeof(*source_6axis_config->xcoords_uv),
3932 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3933 source_6axis_config->ycoords_uv,
3934 source_6axis_config->width_uv *
3935 source_6axis_config->height_uv *
3936 sizeof(*source_6axis_config->ycoords_uv),
3940 css_param->dvs_6axis = dvs_6axis_config;
3941 css_param->update_flag.dvs_6axis_config =
3942 (struct atomisp_dvs_6axis_config *)dvs_6axis_config;
3946 if (dvs_6axis_config)
3947 ia_css_dvs2_6axis_config_free(dvs_6axis_config);
3951 int atomisp_cp_morph_table(struct atomisp_sub_device *asd,
3952 struct atomisp_morph_table *source_morph_table,
3953 struct atomisp_css_params *css_param,
3958 struct atomisp_css_morph_table *morph_table;
3959 struct atomisp_css_morph_table *old_morph_table;
3961 if (!source_morph_table)
3964 if (!from_user && css_param->update_flag.morph_table)
3967 old_morph_table = css_param->morph_table;
3969 if (atomisp_hw_is_isp2401) {
3970 struct atomisp_css_morph_table mtbl;
3972 if (copy_from_compatible(&mtbl, source_morph_table,
3973 sizeof(struct atomisp_morph_table),
3975 dev_err(asd->isp->dev, "copy morph table failed!");
3979 morph_table = atomisp_css_morph_table_allocate(
3985 for (i = 0; i < CSS_MORPH_TABLE_NUM_PLANES; i++) {
3986 if (copy_from_compatible(morph_table->coordinates_x[i],
3987 (__force void *)source_morph_table->coordinates_x[i],
3988 mtbl.height * mtbl.width *
3989 sizeof(*morph_table->coordinates_x[i]),
3993 if (copy_from_compatible(morph_table->coordinates_y[i],
3994 (__force void *)source_morph_table->coordinates_y[i],
3995 mtbl.height * mtbl.width *
3996 sizeof(*morph_table->coordinates_y[i]),
4001 morph_table = atomisp_css_morph_table_allocate(
4002 source_morph_table->width,
4003 source_morph_table->height);
4007 for (i = 0; i < CSS_MORPH_TABLE_NUM_PLANES; i++) {
4008 if (copy_from_compatible(morph_table->coordinates_x[i],
4009 (__force void *)source_morph_table->coordinates_x[i],
4010 source_morph_table->height * source_morph_table->width *
4011 sizeof(*source_morph_table->coordinates_x[i]),
4015 if (copy_from_compatible(morph_table->coordinates_y[i],
4016 (__force void *)source_morph_table->coordinates_y[i],
4017 source_morph_table->height * source_morph_table->width *
4018 sizeof(*source_morph_table->coordinates_y[i]),
4024 css_param->morph_table = morph_table;
4025 if (old_morph_table)
4026 atomisp_css_morph_table_free(old_morph_table);
4027 css_param->update_flag.morph_table =
4028 (struct atomisp_morph_table *)morph_table;
4033 atomisp_css_morph_table_free(morph_table);
4037 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd,
4038 struct atomisp_parameters *arg,
4039 struct atomisp_css_params *css_param)
4043 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false);
4046 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false);
4049 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false);
4052 ret = atomisp_css_cp_dvs2_coefs(asd,
4053 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
4057 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
4062 void atomisp_free_css_parameters(struct atomisp_css_params *css_param)
4064 if (css_param->dvs_6axis) {
4065 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
4066 css_param->dvs_6axis = NULL;
4068 if (css_param->dvs2_coeff) {
4069 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
4070 css_param->dvs2_coeff = NULL;
4072 if (css_param->shading_table) {
4073 ia_css_shading_table_free(css_param->shading_table);
4074 css_param->shading_table = NULL;
4076 if (css_param->morph_table) {
4077 ia_css_morph_table_free(css_param->morph_table);
4078 css_param->morph_table = NULL;
4083 * Check parameter queue list and buffer queue list to find out if matched items
4084 * and then set parameter to CSS and enqueue buffer to CSS.
4085 * Of course, if the buffer in buffer waiting list is not bound to a per-frame
4086 * parameter, it will be enqueued into CSS as long as the per-frame setting
4087 * buffers before it get enqueued.
4089 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe)
4091 struct atomisp_sub_device *asd = pipe->asd;
4092 struct videobuf_buffer *vb = NULL, *vb_tmp;
4093 struct atomisp_css_params_with_list *param = NULL, *param_tmp;
4094 struct videobuf_vmalloc_memory *vm_mem = NULL;
4095 unsigned long irqflags;
4096 bool need_to_enqueue_buffer = false;
4098 if (atomisp_is_vf_pipe(pipe))
4102 * CSS/FW requires set parameter and enqueue buffer happen after ISP
4105 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
4108 if (list_empty(&pipe->per_frame_params) ||
4109 list_empty(&pipe->buffers_waiting_for_param))
4112 list_for_each_entry_safe(vb, vb_tmp,
4113 &pipe->buffers_waiting_for_param, queue) {
4114 if (pipe->frame_request_config_id[vb->i]) {
4115 list_for_each_entry_safe(param, param_tmp,
4116 &pipe->per_frame_params, list) {
4117 if (pipe->frame_request_config_id[vb->i] !=
4118 param->params.isp_config_id)
4121 list_del(¶m->list);
4122 list_del(&vb->queue);
4124 * clear the request config id as the buffer
4125 * will be handled and enqueued into CSS soon
4127 pipe->frame_request_config_id[vb->i] = 0;
4128 pipe->frame_params[vb->i] = param;
4135 spin_lock_irqsave(&pipe->irq_lock, irqflags);
4136 list_add_tail(&vb->queue, &pipe->activeq);
4137 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
4139 need_to_enqueue_buffer = true;
4141 /* The is the end, stop further loop */
4145 list_del(&vb->queue);
4146 pipe->frame_params[vb->i] = NULL;
4147 spin_lock_irqsave(&pipe->irq_lock, irqflags);
4148 list_add_tail(&vb->queue, &pipe->activeq);
4149 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
4150 need_to_enqueue_buffer = true;
4154 if (!need_to_enqueue_buffer)
4157 atomisp_qbuffers_to_css(asd);
4159 if (!atomisp_hw_is_isp2401) {
4160 if (!atomisp_is_wdt_running(asd) && atomisp_buffers_queued(asd))
4161 atomisp_wdt_start(asd);
4163 if (atomisp_buffers_queued_pipe(pipe)) {
4164 if (!atomisp_is_wdt_running(pipe))
4165 atomisp_wdt_start_pipe(pipe);
4167 atomisp_wdt_refresh_pipe(pipe,
4168 ATOMISP_WDT_KEEP_CURRENT_DELAY);
4174 * Function to configure ISP parameters
4176 int atomisp_set_parameters(struct video_device *vdev,
4177 struct atomisp_parameters *arg)
4179 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4180 struct atomisp_sub_device *asd = pipe->asd;
4181 struct atomisp_css_params_with_list *param = NULL;
4182 struct atomisp_css_params *css_param = &asd->params.css_param;
4185 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) {
4186 dev_err(asd->isp->dev, "%s: internal error!\n", __func__);
4190 dev_dbg(asd->isp->dev,
4191 "%s: set parameter(per_frame_setting %d) for asd%d with isp_config_id %d of %s\n",
4192 __func__, arg->per_frame_setting, asd->index,
4193 arg->isp_config_id, vdev->name);
4195 if (atomisp_hw_is_isp2401) {
4196 if (atomisp_is_vf_pipe(pipe) && arg->per_frame_setting) {
4197 dev_err(asd->isp->dev, "%s: vf pipe not support per_frame_setting",
4203 if (arg->per_frame_setting && !atomisp_is_vf_pipe(pipe)) {
4205 * Per-frame setting enabled, we allocate a new parameter
4206 * buffer to cache the parameters and only when frame buffers
4207 * are ready, the parameters will be set to CSS.
4208 * per-frame setting only works for the main output frame.
4210 param = kvzalloc(sizeof(*param), GFP_KERNEL);
4212 dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n",
4216 css_param = ¶m->params;
4219 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true);
4221 goto apply_parameter_failed;
4223 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true);
4225 goto apply_parameter_failed;
4227 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true);
4229 goto apply_parameter_failed;
4231 ret = atomisp_css_cp_dvs2_coefs(asd,
4232 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
4235 goto apply_parameter_failed;
4237 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
4240 goto apply_parameter_failed;
4242 if (!(arg->per_frame_setting && !atomisp_is_vf_pipe(pipe))) {
4243 /* indicate to CSS that we have parameters to be updated */
4244 asd->params.css_update_params_needed = true;
4246 list_add_tail(¶m->list, &pipe->per_frame_params);
4247 atomisp_handle_parameter_and_buffer(pipe);
4252 apply_parameter_failed:
4254 atomisp_free_css_parameters(css_param);
4262 * Function to set/get isp parameters to isp
4264 int atomisp_param(struct atomisp_sub_device *asd, int flag,
4265 struct atomisp_parm *config)
4267 struct atomisp_device *isp = asd->isp;
4268 struct ia_css_pipe_config *vp_cfg =
4269 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
4270 pipe_configs[IA_CSS_PIPE_ID_VIDEO];
4272 /* Read parameter for 3A binary info */
4274 struct atomisp_css_dvs_grid_info *dvs_grid_info =
4275 atomisp_css_get_dvs_grid_info(
4276 &asd->params.curr_grid_info);
4278 if (!&config->info) {
4279 dev_err(isp->dev, "ERROR: NULL pointer in grid_info\n");
4282 atomisp_curr_user_grid_info(asd, &config->info);
4284 /* We always return the resolution and stride even if there is
4285 * no valid metadata. This allows the caller to get the
4286 * information needed to allocate user-space buffers. */
4287 config->metadata_config.metadata_height = asd->
4288 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
4289 metadata_info.resolution.height;
4290 config->metadata_config.metadata_stride = asd->
4291 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
4292 metadata_info.stride;
4294 /* update dvs grid info */
4296 memcpy(&config->dvs_grid,
4298 sizeof(struct atomisp_css_dvs_grid_info));
4300 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
4301 config->dvs_envelop.width = 0;
4302 config->dvs_envelop.height = 0;
4306 /* update dvs envelop info */
4307 if (!asd->continuous_mode->val) {
4308 config->dvs_envelop.width = vp_cfg->dvs_envelope.width;
4309 config->dvs_envelop.height =
4310 vp_cfg->dvs_envelope.height;
4312 unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
4314 dvs_w = vp_cfg->bayer_ds_out_res.width -
4315 vp_cfg->output_info[0].res.width;
4316 dvs_h = vp_cfg->bayer_ds_out_res.height -
4317 vp_cfg->output_info[0].res.height;
4318 dvs_w_max = rounddown(
4319 vp_cfg->output_info[0].res.width / 5,
4320 ATOM_ISP_STEP_WIDTH);
4321 dvs_h_max = rounddown(
4322 vp_cfg->output_info[0].res.height / 5,
4323 ATOM_ISP_STEP_HEIGHT);
4325 config->dvs_envelop.width = min(dvs_w, dvs_w_max);
4326 config->dvs_envelop.height = min(dvs_h, dvs_h_max);
4332 memcpy(&asd->params.css_param.wb_config, &config->wb_config,
4333 sizeof(struct atomisp_css_wb_config));
4334 memcpy(&asd->params.css_param.ob_config, &config->ob_config,
4335 sizeof(struct atomisp_css_ob_config));
4336 memcpy(&asd->params.css_param.dp_config, &config->dp_config,
4337 sizeof(struct atomisp_css_dp_config));
4338 memcpy(&asd->params.css_param.de_config, &config->de_config,
4339 sizeof(struct atomisp_css_de_config));
4340 memcpy(&asd->params.css_param.dz_config, &config->dz_config,
4341 sizeof(struct atomisp_css_dz_config));
4342 memcpy(&asd->params.css_param.ce_config, &config->ce_config,
4343 sizeof(struct atomisp_css_ce_config));
4344 memcpy(&asd->params.css_param.nr_config, &config->nr_config,
4345 sizeof(struct atomisp_css_nr_config));
4346 memcpy(&asd->params.css_param.ee_config, &config->ee_config,
4347 sizeof(struct atomisp_css_ee_config));
4348 memcpy(&asd->params.css_param.tnr_config, &config->tnr_config,
4349 sizeof(struct atomisp_css_tnr_config));
4351 if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) {
4352 asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3];
4353 asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4];
4354 asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5];
4355 asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6];
4356 asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7];
4357 asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8];
4360 if (asd->params.color_effect != V4L2_COLORFX_SEPIA &&
4361 asd->params.color_effect != V4L2_COLORFX_BW) {
4362 memcpy(&asd->params.css_param.cc_config, &config->cc_config,
4363 sizeof(struct atomisp_css_cc_config));
4364 atomisp_css_set_cc_config(asd, &asd->params.css_param.cc_config);
4367 atomisp_css_set_wb_config(asd, &asd->params.css_param.wb_config);
4368 atomisp_css_set_ob_config(asd, &asd->params.css_param.ob_config);
4369 atomisp_css_set_de_config(asd, &asd->params.css_param.de_config);
4370 atomisp_css_set_dz_config(asd, &asd->params.css_param.dz_config);
4371 atomisp_css_set_ce_config(asd, &asd->params.css_param.ce_config);
4372 atomisp_css_set_dp_config(asd, &asd->params.css_param.dp_config);
4373 atomisp_css_set_nr_config(asd, &asd->params.css_param.nr_config);
4374 atomisp_css_set_ee_config(asd, &asd->params.css_param.ee_config);
4375 atomisp_css_set_tnr_config(asd, &asd->params.css_param.tnr_config);
4376 asd->params.css_update_params_needed = true;
4382 * Function to configure color effect of the image
4384 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag,
4387 struct atomisp_css_cc_config *cc_config = NULL;
4388 struct atomisp_css_macc_table *macc_table = NULL;
4389 struct atomisp_css_ctc_table *ctc_table = NULL;
4391 struct v4l2_control control;
4392 struct atomisp_device *isp = asd->isp;
4395 *effect = asd->params.color_effect;
4399 control.id = V4L2_CID_COLORFX;
4400 control.value = *effect;
4402 v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler,
4405 * if set color effect to sensor successfully, return
4409 asd->params.color_effect = (u32)*effect;
4413 if (*effect == asd->params.color_effect)
4417 * isp_subdev->params.macc_en should be set to false.
4419 asd->params.macc_en = false;
4422 case V4L2_COLORFX_NONE:
4423 macc_table = &asd->params.css_param.macc_table;
4424 asd->params.macc_en = true;
4426 case V4L2_COLORFX_SEPIA:
4427 cc_config = &sepia_cc_config;
4429 case V4L2_COLORFX_NEGATIVE:
4430 cc_config = &nega_cc_config;
4432 case V4L2_COLORFX_BW:
4433 cc_config = &mono_cc_config;
4435 case V4L2_COLORFX_SKY_BLUE:
4436 macc_table = &blue_macc_table;
4437 asd->params.macc_en = true;
4439 case V4L2_COLORFX_GRASS_GREEN:
4440 macc_table = &green_macc_table;
4441 asd->params.macc_en = true;
4443 case V4L2_COLORFX_SKIN_WHITEN_LOW:
4444 macc_table = &skin_low_macc_table;
4445 asd->params.macc_en = true;
4447 case V4L2_COLORFX_SKIN_WHITEN:
4448 macc_table = &skin_medium_macc_table;
4449 asd->params.macc_en = true;
4451 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
4452 macc_table = &skin_high_macc_table;
4453 asd->params.macc_en = true;
4455 case V4L2_COLORFX_VIVID:
4456 ctc_table = &vivid_ctc_table;
4461 atomisp_update_capture_mode(asd);
4464 atomisp_css_set_cc_config(asd, cc_config);
4466 atomisp_css_set_macc_table(asd, macc_table);
4468 atomisp_css_set_ctc_table(asd, ctc_table);
4469 asd->params.color_effect = (u32)*effect;
4470 asd->params.css_update_params_needed = true;
4475 * Function to configure bad pixel correction
4477 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag,
4481 *value = asd->params.bad_pixel_en;
4484 asd->params.bad_pixel_en = !!*value;
4490 * Function to configure bad pixel correction params
4492 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag,
4493 struct atomisp_dp_config *config)
4496 /* Get bad pixel from current setup */
4497 if (atomisp_css_get_dp_config(asd, config))
4500 /* Set bad pixel to isp parameters */
4501 memcpy(&asd->params.css_param.dp_config, config,
4502 sizeof(asd->params.css_param.dp_config));
4503 atomisp_css_set_dp_config(asd, &asd->params.css_param.dp_config);
4504 asd->params.css_update_params_needed = true;
4511 * Function to enable/disable video image stablization
4513 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag,
4517 *value = asd->params.video_dis_en;
4519 asd->params.video_dis_en = !!*value;
4525 * Function to configure fixed pattern noise
4527 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag,
4531 *value = asd->params.fpn_en;
4536 asd->params.fpn_en = false;
4540 /* Add function to get black from from sensor with shutter off */
4545 atomisp_bytesperline_to_padded_width(unsigned int bytesperline,
4546 enum atomisp_css_frame_format format)
4549 case CSS_FRAME_FORMAT_UYVY:
4550 case CSS_FRAME_FORMAT_YUYV:
4551 case CSS_FRAME_FORMAT_RAW:
4552 case CSS_FRAME_FORMAT_RGB565:
4553 return bytesperline / 2;
4554 case CSS_FRAME_FORMAT_RGBA888:
4555 return bytesperline / 4;
4556 /* The following cases could be removed, but we leave them
4557 in to document the formats that are included. */
4558 case CSS_FRAME_FORMAT_NV11:
4559 case CSS_FRAME_FORMAT_NV12:
4560 case CSS_FRAME_FORMAT_NV16:
4561 case CSS_FRAME_FORMAT_NV21:
4562 case CSS_FRAME_FORMAT_NV61:
4563 case CSS_FRAME_FORMAT_YV12:
4564 case CSS_FRAME_FORMAT_YV16:
4565 case CSS_FRAME_FORMAT_YUV420:
4566 case CSS_FRAME_FORMAT_YUV420_16:
4567 case CSS_FRAME_FORMAT_YUV422:
4568 case CSS_FRAME_FORMAT_YUV422_16:
4569 case CSS_FRAME_FORMAT_YUV444:
4570 case CSS_FRAME_FORMAT_YUV_LINE:
4571 case CSS_FRAME_FORMAT_PLANAR_RGB888:
4572 case CSS_FRAME_FORMAT_QPLANE6:
4573 case CSS_FRAME_FORMAT_BINARY_8:
4575 return bytesperline;
4580 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg,
4581 struct atomisp_css_frame **result)
4583 struct atomisp_css_frame *res = NULL;
4584 unsigned int padded_width;
4585 enum atomisp_css_frame_format sh_format;
4586 char *tmp_buf = NULL;
4589 sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat);
4590 padded_width = atomisp_bytesperline_to_padded_width(
4591 arg->fmt.bytesperline, sh_format);
4593 /* Note: the padded width on an atomisp_css_frame is in elements, not in
4594 bytes. The RAW frame we use here should always be a 16bit RAW
4595 frame. This is why we bytesperline/2 is equal to the padded with */
4596 if (atomisp_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height,
4597 sh_format, padded_width, 0)) {
4602 tmp_buf = vmalloc(arg->fmt.sizeimage);
4607 if (copy_from_user(tmp_buf, (void __user __force *)arg->base,
4608 arg->fmt.sizeimage)) {
4613 if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) {
4620 atomisp_css_frame_free(res);
4629 * Function to configure fixed pattern noise table
4631 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd,
4632 struct v4l2_framebuffer *arg)
4634 struct atomisp_css_frame *raw_black_frame = NULL;
4640 ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame);
4643 if (atomisp_css_set_black_frame(asd, raw_black_frame))
4646 atomisp_css_frame_free(raw_black_frame);
4651 * Function to configure false color correction
4653 int atomisp_false_color(struct atomisp_sub_device *asd, int flag,
4656 /* Get nr config from current setup */
4658 *value = asd->params.false_color;
4662 /* Set nr config to isp parameters */
4664 atomisp_css_set_default_de_config(asd);
4666 asd->params.css_param.de_config.pixelnoise = 0;
4667 atomisp_css_set_de_config(asd, &asd->params.css_param.de_config);
4669 asd->params.css_update_params_needed = true;
4670 asd->params.false_color = *value;
4675 * Function to configure bad pixel correction params
4677 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag,
4678 struct atomisp_de_config *config)
4681 /* Get false color from current setup */
4682 if (atomisp_css_get_de_config(asd, config))
4685 /* Set false color to isp parameters */
4686 memcpy(&asd->params.css_param.de_config, config,
4687 sizeof(asd->params.css_param.de_config));
4688 atomisp_css_set_de_config(asd, &asd->params.css_param.de_config);
4689 asd->params.css_update_params_needed = true;
4696 * Function to configure white balance params
4698 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag,
4699 struct atomisp_wb_config *config)
4702 /* Get white balance from current setup */
4703 if (atomisp_css_get_wb_config(asd, config))
4706 /* Set white balance to isp parameters */
4707 memcpy(&asd->params.css_param.wb_config, config,
4708 sizeof(asd->params.css_param.wb_config));
4709 atomisp_css_set_wb_config(asd, &asd->params.css_param.wb_config);
4710 asd->params.css_update_params_needed = true;
4716 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag,
4717 struct atomisp_3a_config *config)
4719 struct atomisp_device *isp = asd->isp;
4721 dev_dbg(isp->dev, ">%s %d\n", __func__, flag);
4724 /* Get white balance from current setup */
4725 if (atomisp_css_get_3a_config(asd, config))
4728 /* Set white balance to isp parameters */
4729 memcpy(&asd->params.css_param.s3a_config, config,
4730 sizeof(asd->params.css_param.s3a_config));
4731 atomisp_css_set_3a_config(asd, &asd->params.css_param.s3a_config);
4732 asd->params.css_update_params_needed = true;
4735 dev_dbg(isp->dev, "<%s %d\n", __func__, flag);
4740 * Function to setup digital zoom
4742 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag,
4746 struct atomisp_device *isp = asd->isp;
4748 unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR;
4751 atomisp_css_get_zoom_factor(asd, &zoom);
4752 *value = max_zoom - zoom;
4757 zoom = max_zoom - min_t(u32, max_zoom - 1, *value);
4758 atomisp_css_set_zoom_factor(asd, zoom);
4760 dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom);
4761 asd->params.css_update_params_needed = true;
4768 * Function to get sensor specific info for current resolution,
4769 * which will be used for auto exposure conversion.
4771 int atomisp_get_sensor_mode_data(struct atomisp_sub_device *asd,
4772 struct atomisp_sensor_mode_data *config)
4774 struct camera_mipi_info *mipi_info;
4775 struct atomisp_device *isp = asd->isp;
4777 mipi_info = atomisp_to_sensor_mipi_info(
4778 isp->inputs[asd->input_curr].camera);
4782 memcpy(config, &mipi_info->data, sizeof(*config));
4786 int atomisp_get_fmt(struct video_device *vdev, struct v4l2_format *f)
4788 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4790 f->fmt.pix = pipe->pix;
4795 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd,
4796 u16 stream_index, struct atomisp_input_stream_info *stream_info)
4800 /* assign virtual channel id return from sensor driver query */
4801 asd->stream_env[stream_index].ch_id = stream_info->ch_id;
4802 asd->stream_env[stream_index].isys_configs = stream_info->isys_configs;
4803 for (i = 0; i < stream_info->isys_configs; i++) {
4804 asd->stream_env[stream_index].isys_info[i].input_format =
4805 stream_info->isys_info[i].input_format;
4806 asd->stream_env[stream_index].isys_info[i].width =
4807 stream_info->isys_info[i].width;
4808 asd->stream_env[stream_index].isys_info[i].height =
4809 stream_info->isys_info[i].height;
4813 static void __atomisp_init_stream_info(u16 stream_index,
4814 struct atomisp_input_stream_info *stream_info)
4818 stream_info->enable = 1;
4819 stream_info->stream = stream_index;
4820 stream_info->ch_id = 0;
4821 stream_info->isys_configs = 0;
4822 for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) {
4823 stream_info->isys_info[i].input_format = 0;
4824 stream_info->isys_info[i].width = 0;
4825 stream_info->isys_info[i].height = 0;
4829 /* This function looks up the closest available resolution. */
4830 int atomisp_try_fmt(struct video_device *vdev, struct v4l2_format *f,
4833 struct atomisp_device *isp = video_get_drvdata(vdev);
4834 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4835 struct v4l2_subdev_pad_config pad_cfg;
4836 struct v4l2_subdev_format format = {
4837 .which = V4L2_SUBDEV_FORMAT_TRY,
4840 struct v4l2_mbus_framefmt *snr_mbus_fmt = &format.format;
4841 const struct atomisp_format_bridge *fmt;
4842 struct atomisp_input_stream_info *stream_info =
4843 (struct atomisp_input_stream_info *)snr_mbus_fmt->reserved;
4845 int source_pad = atomisp_subdev_source_pad(vdev);
4848 if (!isp->inputs[asd->input_curr].camera)
4851 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
4852 fmt = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
4854 dev_err(isp->dev, "unsupported pixelformat!\n");
4855 fmt = atomisp_output_fmts;
4858 if (f->fmt.pix.width <= 0 || f->fmt.pix.height <= 0)
4861 snr_mbus_fmt->code = fmt->mbus_code;
4862 snr_mbus_fmt->width = f->fmt.pix.width;
4863 snr_mbus_fmt->height = f->fmt.pix.height;
4865 __atomisp_init_stream_info(stream_index, stream_info);
4867 dev_dbg(isp->dev, "try_mbus_fmt: asking for %ux%u\n",
4868 snr_mbus_fmt->width, snr_mbus_fmt->height);
4870 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
4871 pad, set_fmt, &pad_cfg, &format);
4875 dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n",
4876 snr_mbus_fmt->width, snr_mbus_fmt->height);
4878 fmt = atomisp_get_format_bridge_from_mbus(snr_mbus_fmt->code);
4880 dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
4881 snr_mbus_fmt->code);
4885 f->fmt.pix.pixelformat = fmt->pixelformat;
4888 * If the format is jpeg or custom RAW, then the width and height will
4889 * not satisfy the normal atomisp requirements and no need to check
4890 * the below conditions. So just assign to what is being returned from
4891 * the sensor driver.
4893 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG ||
4894 f->fmt.pix.pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
4895 f->fmt.pix.width = snr_mbus_fmt->width;
4896 f->fmt.pix.height = snr_mbus_fmt->height;
4900 if (snr_mbus_fmt->width < f->fmt.pix.width
4901 && snr_mbus_fmt->height < f->fmt.pix.height) {
4902 f->fmt.pix.width = snr_mbus_fmt->width;
4903 f->fmt.pix.height = snr_mbus_fmt->height;
4904 /* Set the flag when resolution requested is
4905 * beyond the max value supported by sensor
4908 *res_overflow = true;
4912 f->fmt.pix.width = rounddown(
4913 clamp_t(u32, f->fmt.pix.width, ATOM_ISP_MIN_WIDTH,
4914 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
4915 f->fmt.pix.height = rounddown(
4916 clamp_t(u32, f->fmt.pix.height, ATOM_ISP_MIN_HEIGHT,
4917 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
4923 atomisp_try_fmt_file(struct atomisp_device *isp, struct v4l2_format *f)
4925 u32 width = f->fmt.pix.width;
4926 u32 height = f->fmt.pix.height;
4927 u32 pixelformat = f->fmt.pix.pixelformat;
4928 enum v4l2_field field = f->fmt.pix.field;
4931 if (!atomisp_get_format_bridge(pixelformat)) {
4932 dev_err(isp->dev, "Wrong output pixelformat\n");
4936 depth = get_pixel_depth(pixelformat);
4938 if (field == V4L2_FIELD_ANY)
4939 field = V4L2_FIELD_NONE;
4940 else if (field != V4L2_FIELD_NONE) {
4941 dev_err(isp->dev, "Wrong output field\n");
4945 f->fmt.pix.field = field;
4946 f->fmt.pix.width = clamp_t(u32,
4947 rounddown(width, (u32)ATOM_ISP_STEP_WIDTH),
4948 ATOM_ISP_MIN_WIDTH, ATOM_ISP_MAX_WIDTH);
4949 f->fmt.pix.height = clamp_t(u32, rounddown(height,
4950 (u32)ATOM_ISP_STEP_HEIGHT),
4951 ATOM_ISP_MIN_HEIGHT, ATOM_ISP_MAX_HEIGHT);
4952 f->fmt.pix.bytesperline = (width * depth) >> 3;
4957 enum mipi_port_id __get_mipi_port(struct atomisp_device *isp,
4958 enum atomisp_camera_port port)
4961 case ATOMISP_CAMERA_PORT_PRIMARY:
4962 return MIPI_PORT0_ID;
4963 case ATOMISP_CAMERA_PORT_SECONDARY:
4964 return MIPI_PORT1_ID;
4965 case ATOMISP_CAMERA_PORT_TERTIARY:
4966 if (MIPI_PORT1_ID + 1 != N_MIPI_PORT_ID)
4967 return MIPI_PORT1_ID + 1;
4968 /* go through down for else case */
4970 dev_err(isp->dev, "unsupported port: %d\n", port);
4971 return MIPI_PORT0_ID;
4975 static inline int atomisp_set_sensor_mipi_to_isp(
4976 struct atomisp_sub_device *asd,
4977 enum atomisp_input_stream_id stream_id,
4978 struct camera_mipi_info *mipi_info)
4980 struct v4l2_control ctrl;
4981 struct atomisp_device *isp = asd->isp;
4982 const struct atomisp_in_fmt_conv *fc;
4984 unsigned int input_format, bayer_order;
4986 ctrl.id = V4L2_CID_LINK_FREQ;
4988 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl) == 0)
4989 mipi_freq = ctrl.value;
4991 if (asd->stream_env[stream_id].isys_configs == 1) {
4993 asd->stream_env[stream_id].isys_info[0].input_format;
4994 atomisp_css_isys_set_format(asd, stream_id,
4995 input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX);
4996 } else if (asd->stream_env[stream_id].isys_configs == 2) {
4997 atomisp_css_isys_two_stream_cfg_update_stream1(
4999 asd->stream_env[stream_id].isys_info[0].input_format,
5000 asd->stream_env[stream_id].isys_info[0].width,
5001 asd->stream_env[stream_id].isys_info[0].height);
5003 atomisp_css_isys_two_stream_cfg_update_stream2(
5005 asd->stream_env[stream_id].isys_info[1].input_format,
5006 asd->stream_env[stream_id].isys_info[1].width,
5007 asd->stream_env[stream_id].isys_info[1].height);
5010 /* Compatibility for sensors which provide no media bus code
5011 * in s_mbus_framefmt() nor support pad formats. */
5012 if (mipi_info->input_format != -1) {
5013 bayer_order = mipi_info->raw_bayer_order;
5015 /* Input stream config is still needs configured */
5016 /* TODO: Check if this is necessary */
5017 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5018 mipi_info->input_format);
5021 input_format = fc->css_stream_fmt;
5023 struct v4l2_mbus_framefmt *sink;
5025 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5026 V4L2_SUBDEV_FORMAT_ACTIVE,
5027 ATOMISP_SUBDEV_PAD_SINK);
5028 fc = atomisp_find_in_fmt_conv(sink->code);
5031 input_format = fc->css_stream_fmt;
5032 bayer_order = fc->bayer_order;
5035 atomisp_css_input_set_format(asd, stream_id, input_format);
5036 atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order);
5038 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5039 mipi_info->metadata_format);
5042 input_format = fc->css_stream_fmt;
5043 atomisp_css_input_configure_port(asd,
5044 __get_mipi_port(asd->isp, mipi_info->port),
5045 mipi_info->num_lanes,
5048 mipi_info->metadata_width,
5049 mipi_info->metadata_height);
5053 static int __enable_continuous_mode(struct atomisp_sub_device *asd,
5056 struct atomisp_device *isp = asd->isp;
5059 "continuous mode %d, raw buffers %d, stop preview %d\n",
5060 enable, asd->continuous_raw_buffer_size->val,
5061 !asd->continuous_viewfinder->val);
5063 if (!atomisp_hw_is_isp2401)
5064 atomisp_css_capture_set_mode(asd, CSS_CAPTURE_MODE_PRIMARY);
5066 atomisp_update_capture_mode(asd);
5068 /* in case of ANR, force capture pipe to offline mode */
5069 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
5070 asd->params.low_light ? false : !enable);
5071 atomisp_css_preview_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
5073 atomisp_css_enable_continuous(asd, enable);
5074 atomisp_css_enable_cvf(asd, asd->continuous_viewfinder->val);
5076 if (atomisp_css_continuous_set_num_raw_frames(asd,
5077 asd->continuous_raw_buffer_size->val)) {
5078 dev_err(isp->dev, "css_continuous_set_num_raw_frames failed\n");
5083 atomisp_css_enable_raw_binning(asd, false);
5084 atomisp_css_input_set_two_pixels_per_clock(asd, false);
5087 if (isp->inputs[asd->input_curr].type != FILE_INPUT)
5088 atomisp_css_input_set_mode(asd, CSS_INPUT_MODE_SENSOR);
5090 return atomisp_update_run_mode(asd);
5093 static int configure_pp_input_nop(struct atomisp_sub_device *asd,
5094 unsigned int width, unsigned int height)
5099 static int configure_output_nop(struct atomisp_sub_device *asd,
5100 unsigned int width, unsigned int height,
5101 unsigned int min_width,
5102 enum atomisp_css_frame_format sh_fmt)
5107 static int get_frame_info_nop(struct atomisp_sub_device *asd,
5108 struct atomisp_css_frame_info *finfo)
5114 * Resets CSS parameters that depend on input resolution.
5116 * Update params like CSS RAW binning, 2ppc mode and pp_input
5117 * which depend on input size, but are not automatically
5118 * handled in CSS when the input resolution is changed.
5120 static int css_input_resolution_changed(struct atomisp_sub_device *asd,
5121 struct v4l2_mbus_framefmt *ffmt)
5123 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
5126 dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n",
5127 ffmt->width, ffmt->height);
5129 #if defined(ISP2401_NEW_INPUT_SYSTEM)
5130 atomisp_css_input_set_two_pixels_per_clock(asd, false);
5132 atomisp_css_input_set_two_pixels_per_clock(asd, true);
5134 if (asd->continuous_mode->val) {
5135 /* Note for all checks: ffmt includes pad_w+pad_h */
5136 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
5137 (ffmt->width >= 2048 || ffmt->height >= 1536)) {
5139 * For preview pipe, enable only if resolution
5140 * is >= 3M for ISP2400.
5142 atomisp_css_enable_raw_binning(asd, true);
5146 * If sensor input changed, which means metadata resolution changed
5147 * together. Release all metadata buffers here to let it re-allocated
5148 * next time in reqbufs.
5150 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
5151 list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
5153 atomisp_css_free_metadata_buffer(md_buf);
5154 list_del(&md_buf->list);
5161 * TODO: atomisp_css_preview_configure_pp_input() not
5162 * reset due to CSS bug tracked as PSI BZ 115124
5166 static int atomisp_set_fmt_to_isp(struct video_device *vdev,
5167 struct atomisp_css_frame_info *output_info,
5168 struct atomisp_css_frame_info *raw_output_info,
5169 struct v4l2_pix_format *pix,
5170 unsigned int source_pad)
5172 struct camera_mipi_info *mipi_info;
5173 struct atomisp_device *isp = video_get_drvdata(vdev);
5174 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
5175 const struct atomisp_format_bridge *format;
5176 struct v4l2_rect *isp_sink_crop;
5177 enum atomisp_css_pipe_id pipe_id;
5178 struct v4l2_subdev_fh fh;
5179 int (*configure_output)(struct atomisp_sub_device *asd,
5180 unsigned int width, unsigned int height,
5181 unsigned int min_width,
5182 enum atomisp_css_frame_format sh_fmt) =
5183 configure_output_nop;
5184 int (*get_frame_info)(struct atomisp_sub_device *asd,
5185 struct atomisp_css_frame_info *finfo) =
5187 int (*configure_pp_input)(struct atomisp_sub_device *asd,
5188 unsigned int width, unsigned int height) =
5189 configure_pp_input_nop;
5190 u16 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
5191 const struct atomisp_in_fmt_conv *fc;
5194 v4l2_fh_init(&fh.vfh, vdev);
5196 isp_sink_crop = atomisp_subdev_get_rect(
5197 &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
5198 ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP);
5200 format = atomisp_get_format_bridge(pix->pixelformat);
5204 if (isp->inputs[asd->input_curr].type != TEST_PATTERN &&
5205 isp->inputs[asd->input_curr].type != FILE_INPUT) {
5206 mipi_info = atomisp_to_sensor_mipi_info(
5207 isp->inputs[asd->input_curr].camera);
5209 dev_err(isp->dev, "mipi_info is NULL\n");
5212 if (atomisp_set_sensor_mipi_to_isp(asd, stream_index,
5215 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5216 mipi_info->input_format);
5218 fc = atomisp_find_in_fmt_conv(
5219 atomisp_subdev_get_ffmt(&asd->subdev,
5220 NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
5221 ATOMISP_SUBDEV_PAD_SINK)->code);
5224 if (format->sh_fmt == CSS_FRAME_FORMAT_RAW &&
5225 raw_output_format_match_input(fc->css_stream_fmt,
5231 * Configure viewfinder also when vfpp is disabled: the
5232 * CSS still requires viewfinder configuration.
5234 if (asd->fmt_auto->val ||
5235 asd->vfpp->val != ATOMISP_VFPP_ENABLE) {
5236 struct v4l2_rect vf_size = {0};
5237 struct v4l2_mbus_framefmt vf_ffmt = {0};
5239 if (pix->width < 640 || pix->height < 480) {
5240 vf_size.width = pix->width;
5241 vf_size.height = pix->height;
5243 vf_size.width = 640;
5244 vf_size.height = 480;
5247 /* FIXME: proper format name for this one. See
5248 atomisp_output_fmts[] in atomisp_v4l2.c */
5249 vf_ffmt.code = V4L2_MBUS_FMT_CUSTOM_YUV420;
5251 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5252 V4L2_SUBDEV_FORMAT_ACTIVE,
5253 ATOMISP_SUBDEV_PAD_SOURCE_VF,
5254 V4L2_SEL_TGT_COMPOSE, 0, &vf_size);
5255 atomisp_subdev_set_ffmt(&asd->subdev, fh.pad,
5256 V4L2_SUBDEV_FORMAT_ACTIVE,
5257 ATOMISP_SUBDEV_PAD_SOURCE_VF, &vf_ffmt);
5258 asd->video_out_vf.sh_fmt = CSS_FRAME_FORMAT_NV12;
5260 if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
5261 atomisp_css_video_configure_viewfinder(asd,
5262 vf_size.width, vf_size.height, 0,
5263 asd->video_out_vf.sh_fmt);
5264 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5265 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5266 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO)
5267 atomisp_css_video_configure_viewfinder(asd,
5268 vf_size.width, vf_size.height, 0,
5269 asd->video_out_vf.sh_fmt);
5271 atomisp_css_capture_configure_viewfinder(asd,
5272 vf_size.width, vf_size.height, 0,
5273 asd->video_out_vf.sh_fmt);
5274 } else if (source_pad != ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5275 asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
5276 atomisp_css_capture_configure_viewfinder(asd,
5277 vf_size.width, vf_size.height, 0,
5278 asd->video_out_vf.sh_fmt);
5282 if (asd->continuous_mode->val) {
5283 ret = __enable_continuous_mode(asd, true);
5288 atomisp_css_input_set_mode(asd, CSS_INPUT_MODE_SENSOR);
5289 atomisp_css_disable_vf_pp(asd,
5290 asd->vfpp->val != ATOMISP_VFPP_ENABLE);
5292 /* ISP2401 new input system need to use copy pipe */
5293 if (asd->copy_mode) {
5294 pipe_id = CSS_PIPE_ID_COPY;
5295 atomisp_css_capture_enable_online(asd, stream_index, false);
5296 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
5297 /* video same in continuouscapture and online modes */
5298 configure_output = atomisp_css_video_configure_output;
5299 get_frame_info = atomisp_css_video_get_output_frame_info;
5300 pipe_id = CSS_PIPE_ID_VIDEO;
5301 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5302 if (!asd->continuous_mode->val) {
5303 configure_output = atomisp_css_video_configure_output;
5305 atomisp_css_video_get_output_frame_info;
5306 pipe_id = CSS_PIPE_ID_VIDEO;
5308 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5309 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
5311 atomisp_css_video_configure_output;
5313 atomisp_css_video_get_output_frame_info;
5314 configure_pp_input =
5315 atomisp_css_video_configure_pp_input;
5316 pipe_id = CSS_PIPE_ID_VIDEO;
5319 atomisp_css_capture_configure_output;
5321 atomisp_css_capture_get_output_frame_info;
5322 configure_pp_input =
5323 atomisp_css_capture_configure_pp_input;
5324 pipe_id = CSS_PIPE_ID_CAPTURE;
5326 atomisp_update_capture_mode(asd);
5327 atomisp_css_capture_enable_online(asd, stream_index, false);
5330 } else if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
5331 configure_output = atomisp_css_preview_configure_output;
5332 get_frame_info = atomisp_css_preview_get_output_frame_info;
5333 configure_pp_input = atomisp_css_preview_configure_pp_input;
5334 pipe_id = CSS_PIPE_ID_PREVIEW;
5336 /* CSS doesn't support low light mode on SOC cameras, so disable
5337 * it. FIXME: if this is done elsewhere, it gives corrupted
5338 * colors into thumbnail image.
5340 if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
5341 asd->params.low_light = false;
5343 if (format->sh_fmt == CSS_FRAME_FORMAT_RAW) {
5344 atomisp_css_capture_set_mode(asd, CSS_CAPTURE_MODE_RAW);
5345 atomisp_css_enable_dz(asd, false);
5347 atomisp_update_capture_mode(asd);
5350 if (!asd->continuous_mode->val)
5351 /* in case of ANR, force capture pipe to offline mode */
5352 atomisp_css_capture_enable_online(asd, stream_index,
5353 asd->params.low_light ?
5354 false : asd->params.online_process);
5356 configure_output = atomisp_css_capture_configure_output;
5357 get_frame_info = atomisp_css_capture_get_output_frame_info;
5358 configure_pp_input = atomisp_css_capture_configure_pp_input;
5359 pipe_id = CSS_PIPE_ID_CAPTURE;
5361 if (!asd->params.online_process &&
5362 !asd->continuous_mode->val) {
5363 ret = atomisp_css_capture_get_output_raw_frame_info(asd,
5368 if (!asd->continuous_mode->val && asd->run_mode->val
5369 != ATOMISP_RUN_MODE_STILL_CAPTURE) {
5371 "Need to set the running mode first\n");
5372 asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE;
5377 * to SOC camera, use yuvpp pipe.
5379 if (ATOMISP_USE_YUVPP(asd))
5380 pipe_id = CSS_PIPE_ID_YUVPP;
5383 ret = atomisp_css_copy_configure_output(asd, stream_index,
5384 pix->width, pix->height,
5385 format->planar ? pix->bytesperline :
5386 pix->bytesperline * 8 / format->depth,
5389 ret = configure_output(asd, pix->width, pix->height,
5390 format->planar ? pix->bytesperline :
5391 pix->bytesperline * 8 / format->depth,
5394 dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n",
5395 pix->width, pix->height, format->sh_fmt);
5399 if (asd->continuous_mode->val &&
5400 (configure_pp_input == atomisp_css_preview_configure_pp_input ||
5401 configure_pp_input == atomisp_css_video_configure_pp_input)) {
5402 /* for isp 2.2, configure pp input is available for continuous
5404 ret = configure_pp_input(asd, isp_sink_crop->width,
5405 isp_sink_crop->height);
5407 dev_err(isp->dev, "configure_pp_input %ux%u\n",
5408 isp_sink_crop->width,
5409 isp_sink_crop->height);
5413 ret = configure_pp_input(asd, isp_sink_crop->width,
5414 isp_sink_crop->height);
5416 dev_err(isp->dev, "configure_pp_input %ux%u\n",
5417 isp_sink_crop->width, isp_sink_crop->height);
5422 ret = atomisp_css_copy_get_output_frame_info(asd, stream_index,
5425 ret = get_frame_info(asd, output_info);
5427 dev_err(isp->dev, "get_frame_info %ux%u (padded to %u)\n",
5428 pix->width, pix->height, pix->bytesperline);
5432 atomisp_update_grid_info(asd, pipe_id, source_pad);
5434 /* Free the raw_dump buffer first */
5435 atomisp_css_frame_free(asd->raw_output_frame);
5436 asd->raw_output_frame = NULL;
5438 if (!asd->continuous_mode->val &&
5439 !asd->params.online_process && !isp->sw_contex.file_input &&
5440 atomisp_css_frame_allocate_from_info(&asd->raw_output_frame,
5447 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd,
5448 unsigned int width, unsigned int height,
5449 unsigned int *dvs_env_w, unsigned int *dvs_env_h)
5451 struct atomisp_device *isp = asd->isp;
5453 /* if subdev type is SOC camera,we do not need to set DVS */
5454 if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
5455 asd->params.video_dis_en = false;
5457 if (asd->params.video_dis_en &&
5458 asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5459 /* envelope is 20% of the output resolution */
5461 * dvs envelope cannot be round up.
5462 * it would cause ISP timeout and color switch issue
5464 *dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH);
5465 *dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT);
5468 asd->params.dis_proj_data_valid = false;
5469 asd->params.css_update_params_needed = true;
5472 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd,
5473 int source_pad, struct v4l2_format *f)
5475 #if defined(ISP2401_NEW_INPUT_SYSTEM)
5476 struct v4l2_mbus_framefmt *sink, *src;
5478 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5479 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK);
5480 src = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5481 V4L2_SUBDEV_FORMAT_ACTIVE, source_pad);
5483 if ((sink->code == src->code &&
5484 sink->width == f->fmt.pix.width &&
5485 sink->height == f->fmt.pix.height) ||
5486 ((asd->isp->inputs[asd->input_curr].type == SOC_CAMERA) &&
5487 (asd->isp->inputs[asd->input_curr].camera_caps->
5488 sensor[asd->sensor_curr].stream_num > 1)))
5489 asd->copy_mode = true;
5492 /* Only used for the new input system */
5493 asd->copy_mode = false;
5495 dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode);
5498 static int atomisp_set_fmt_to_snr(struct video_device *vdev,
5499 struct v4l2_format *f, unsigned int pixelformat,
5500 unsigned int padding_w, unsigned int padding_h,
5501 unsigned int dvs_env_w, unsigned int dvs_env_h)
5503 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
5504 const struct atomisp_format_bridge *format;
5505 struct v4l2_subdev_pad_config pad_cfg;
5506 struct v4l2_subdev_format vformat = {
5507 .which = V4L2_SUBDEV_FORMAT_TRY,
5509 struct v4l2_mbus_framefmt *ffmt = &vformat.format;
5510 struct v4l2_mbus_framefmt *req_ffmt;
5511 struct atomisp_device *isp = asd->isp;
5512 struct atomisp_input_stream_info *stream_info =
5513 (struct atomisp_input_stream_info *)ffmt->reserved;
5514 u16 stream_index = ATOMISP_INPUT_STREAM_GENERAL;
5515 int source_pad = atomisp_subdev_source_pad(vdev);
5516 struct v4l2_subdev_fh fh;
5519 v4l2_fh_init(&fh.vfh, vdev);
5521 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
5523 format = atomisp_get_format_bridge(pixelformat);
5527 v4l2_fill_mbus_format(ffmt, &f->fmt.pix, format->mbus_code);
5528 ffmt->height += padding_h + dvs_env_h;
5529 ffmt->width += padding_w + dvs_env_w;
5531 dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n",
5532 ffmt->width, ffmt->height, padding_w, padding_h,
5533 dvs_env_w, dvs_env_h);
5535 __atomisp_init_stream_info(stream_index, stream_info);
5539 /* Disable dvs if resolution can't be supported by sensor */
5540 if (asd->params.video_dis_en &&
5541 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
5542 vformat.which = V4L2_SUBDEV_FORMAT_TRY;
5543 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
5544 pad, set_fmt, &pad_cfg, &vformat);
5547 if (ffmt->width < req_ffmt->width ||
5548 ffmt->height < req_ffmt->height) {
5549 req_ffmt->height -= dvs_env_h;
5550 req_ffmt->width -= dvs_env_w;
5553 "can not enable video dis due to sensor limitation.");
5554 asd->params.video_dis_en = false;
5557 dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
5558 ffmt->width, ffmt->height);
5559 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
5560 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
5561 set_fmt, NULL, &vformat);
5565 __atomisp_update_stream_env(asd, stream_index, stream_info);
5567 dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
5568 ffmt->width, ffmt->height);
5570 if (ffmt->width < ATOM_ISP_STEP_WIDTH ||
5571 ffmt->height < ATOM_ISP_STEP_HEIGHT)
5574 if (asd->params.video_dis_en &&
5575 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
5576 (ffmt->width < req_ffmt->width || ffmt->height < req_ffmt->height)) {
5578 "can not enable video dis due to sensor limitation.");
5579 asd->params.video_dis_en = false;
5582 atomisp_subdev_set_ffmt(&asd->subdev, fh.pad,
5583 V4L2_SUBDEV_FORMAT_ACTIVE,
5584 ATOMISP_SUBDEV_PAD_SINK, ffmt);
5586 return css_input_resolution_changed(asd, ffmt);
5589 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f)
5591 struct atomisp_device *isp = video_get_drvdata(vdev);
5592 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
5593 struct atomisp_sub_device *asd = pipe->asd;
5594 const struct atomisp_format_bridge *format_bridge;
5595 const struct atomisp_format_bridge *snr_format_bridge;
5596 struct atomisp_css_frame_info output_info, raw_output_info;
5597 struct v4l2_format snr_fmt = *f;
5598 struct v4l2_format backup_fmt = *f, s_fmt = *f;
5599 unsigned int dvs_env_w = 0, dvs_env_h = 0;
5600 unsigned int padding_w = pad_w, padding_h = pad_h;
5601 bool res_overflow = false, crop_needs_override = false;
5602 struct v4l2_mbus_framefmt isp_sink_fmt;
5603 struct v4l2_mbus_framefmt isp_source_fmt = {0};
5604 struct v4l2_rect isp_sink_crop;
5605 u16 source_pad = atomisp_subdev_source_pad(vdev);
5606 struct v4l2_subdev_fh fh;
5610 "setting resolution %ux%u on pad %u for asd%d, bytesperline %u\n",
5611 f->fmt.pix.width, f->fmt.pix.height, source_pad,
5612 asd->index, f->fmt.pix.bytesperline);
5614 if (source_pad >= ATOMISP_SUBDEV_PADS_NUM)
5617 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED) {
5618 dev_warn(isp->dev, "ISP does not support set format while at streaming!\n");
5622 v4l2_fh_init(&fh.vfh, vdev);
5624 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
5628 pipe->sh_fmt = format_bridge->sh_fmt;
5629 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
5631 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VF ||
5632 (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5633 && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)) {
5634 if (asd->fmt_auto->val) {
5635 struct v4l2_rect *capture_comp;
5636 struct v4l2_rect r = {0};
5638 r.width = f->fmt.pix.width;
5639 r.height = f->fmt.pix.height;
5641 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW)
5642 capture_comp = atomisp_subdev_get_rect(
5644 V4L2_SUBDEV_FORMAT_ACTIVE,
5645 ATOMISP_SUBDEV_PAD_SOURCE_VIDEO,
5646 V4L2_SEL_TGT_COMPOSE);
5648 capture_comp = atomisp_subdev_get_rect(
5650 V4L2_SUBDEV_FORMAT_ACTIVE,
5651 ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5652 V4L2_SEL_TGT_COMPOSE);
5654 if (capture_comp->width < r.width
5655 || capture_comp->height < r.height) {
5656 r.width = capture_comp->width;
5657 r.height = capture_comp->height;
5660 atomisp_subdev_set_selection(
5661 &asd->subdev, fh.pad,
5662 V4L2_SUBDEV_FORMAT_ACTIVE, source_pad,
5663 V4L2_SEL_TGT_COMPOSE, 0, &r);
5665 f->fmt.pix.width = r.width;
5666 f->fmt.pix.height = r.height;
5669 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW &&
5670 (asd->isp->inputs[asd->input_curr].type == SOC_CAMERA) &&
5671 (asd->isp->inputs[asd->input_curr].camera_caps->
5672 sensor[asd->sensor_curr].stream_num > 1)) {
5673 /* For M10MO outputing YUV preview images. */
5675 atomisp_source_pad_to_stream_id(asd,
5676 ATOMISP_SUBDEV_PAD_SOURCE_VIDEO);
5678 ret = atomisp_css_copy_get_output_frame_info(asd,
5679 video_index, &output_info);
5682 "copy_get_output_frame_info ret %i", ret);
5685 if (!asd->yuvpp_mode) {
5687 * If viewfinder was configured into copy_mode,
5688 * we switch to using yuvpp pipe instead.
5690 asd->yuvpp_mode = true;
5691 ret = atomisp_css_copy_configure_output(
5692 asd, video_index, 0, 0, 0, 0);
5695 "failed to disable copy pipe");
5698 ret = atomisp_css_yuvpp_configure_output(
5700 output_info.res.width,
5701 output_info.res.height,
5702 output_info.padded_width,
5703 output_info.format);
5706 "failed to set up yuvpp pipe\n");
5709 atomisp_css_video_enable_online(asd, false);
5710 atomisp_css_preview_enable_online(asd,
5711 ATOMISP_INPUT_STREAM_GENERAL, false);
5713 atomisp_css_yuvpp_configure_viewfinder(asd, video_index,
5714 f->fmt.pix.width, f->fmt.pix.height,
5715 format_bridge->planar ? f->fmt.pix.bytesperline
5716 : f->fmt.pix.bytesperline * 8
5717 / format_bridge->depth, format_bridge->sh_fmt);
5718 atomisp_css_yuvpp_get_viewfinder_frame_info(
5719 asd, video_index, &output_info);
5720 } else if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
5721 atomisp_css_video_configure_viewfinder(asd,
5722 f->fmt.pix.width, f->fmt.pix.height,
5723 format_bridge->planar ? f->fmt.pix.bytesperline
5724 : f->fmt.pix.bytesperline * 8
5725 / format_bridge->depth, format_bridge->sh_fmt);
5726 atomisp_css_video_get_viewfinder_frame_info(asd,
5728 asd->copy_mode = false;
5730 atomisp_css_capture_configure_viewfinder(asd,
5731 f->fmt.pix.width, f->fmt.pix.height,
5732 format_bridge->planar ? f->fmt.pix.bytesperline
5733 : f->fmt.pix.bytesperline * 8
5734 / format_bridge->depth, format_bridge->sh_fmt);
5735 atomisp_css_capture_get_viewfinder_frame_info(asd,
5737 asd->copy_mode = false;
5743 * Check whether main resolution configured smaller
5744 * than snapshot resolution. If so, force main resolution
5745 * to be the same as snapshot resolution
5747 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE) {
5748 struct v4l2_rect *r;
5750 r = atomisp_subdev_get_rect(
5752 V4L2_SUBDEV_FORMAT_ACTIVE,
5753 ATOMISP_SUBDEV_PAD_SOURCE_VF, V4L2_SEL_TGT_COMPOSE);
5755 if (r->width && r->height
5756 && (r->width > f->fmt.pix.width
5757 || r->height > f->fmt.pix.height))
5759 "Main Resolution config smaller then Vf Resolution. Force to be equal with Vf Resolution.");
5762 /* Pipeline configuration done through subdevs. Bail out now. */
5763 if (!asd->fmt_auto->val)
5764 goto set_fmt_to_isp;
5766 /* get sensor resolution and format */
5767 ret = atomisp_try_fmt(vdev, &snr_fmt, &res_overflow);
5770 f->fmt.pix.width = snr_fmt.fmt.pix.width;
5771 f->fmt.pix.height = snr_fmt.fmt.pix.height;
5774 atomisp_get_format_bridge(snr_fmt.fmt.pix.pixelformat);
5775 if (!snr_format_bridge)
5778 atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5779 V4L2_SUBDEV_FORMAT_ACTIVE,
5780 ATOMISP_SUBDEV_PAD_SINK)->code =
5781 snr_format_bridge->mbus_code;
5783 isp_sink_fmt = *atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5784 V4L2_SUBDEV_FORMAT_ACTIVE,
5785 ATOMISP_SUBDEV_PAD_SINK);
5787 isp_source_fmt.code = format_bridge->mbus_code;
5788 atomisp_subdev_set_ffmt(&asd->subdev, fh.pad,
5789 V4L2_SUBDEV_FORMAT_ACTIVE,
5790 source_pad, &isp_source_fmt);
5792 if (!atomisp_subdev_format_conversion(asd, source_pad)) {
5795 } else if (IS_BYT) {
5800 /* construct resolution supported by isp */
5801 if (res_overflow && !asd->continuous_mode->val) {
5802 f->fmt.pix.width = rounddown(
5803 clamp_t(u32, f->fmt.pix.width - padding_w,
5805 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
5806 f->fmt.pix.height = rounddown(
5807 clamp_t(u32, f->fmt.pix.height - padding_h,
5808 ATOM_ISP_MIN_HEIGHT,
5809 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
5812 atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height,
5813 &dvs_env_w, &dvs_env_h);
5815 if (asd->continuous_mode->val) {
5816 struct v4l2_rect *r;
5818 r = atomisp_subdev_get_rect(
5820 V4L2_SUBDEV_FORMAT_ACTIVE,
5821 ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5822 V4L2_SEL_TGT_COMPOSE);
5824 * The ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE should get resolutions
5825 * properly set otherwise, it should not be the capture_pad.
5827 if (r->width && r->height)
5828 asd->capture_pad = ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE;
5830 asd->capture_pad = source_pad;
5832 asd->capture_pad = source_pad;
5835 * set format info to sensor
5836 * In continuous mode, resolution is set only if it is higher than
5837 * existing value. This because preview pipe will be configured after
5838 * capture pipe and usually has lower resolution than capture pipe.
5840 if (!asd->continuous_mode->val ||
5841 isp_sink_fmt.width < (f->fmt.pix.width + padding_w + dvs_env_w) ||
5842 isp_sink_fmt.height < (f->fmt.pix.height + padding_h +
5845 * For jpeg or custom raw format the sensor will return constant
5846 * width and height. Because we already had quried try_mbus_fmt,
5847 * f->fmt.pix.width and f->fmt.pix.height has been changed to
5848 * this fixed width and height. So we cannot select the correct
5849 * resolution with that information. So use the original width
5850 * and height while set_mbus_fmt() so actual resolutions are
5851 * being used in while set media bus format.
5854 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG ||
5855 f->fmt.pix.pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
5856 s_fmt.fmt.pix.width = backup_fmt.fmt.pix.width;
5857 s_fmt.fmt.pix.height = backup_fmt.fmt.pix.height;
5859 ret = atomisp_set_fmt_to_snr(vdev, &s_fmt,
5860 f->fmt.pix.pixelformat, padding_w,
5861 padding_h, dvs_env_w, dvs_env_h);
5865 atomisp_csi_lane_config(isp);
5866 crop_needs_override = true;
5869 atomisp_check_copy_mode(asd, source_pad, &backup_fmt);
5870 asd->yuvpp_mode = false; /* Reset variable */
5872 isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL,
5873 V4L2_SUBDEV_FORMAT_ACTIVE,
5874 ATOMISP_SUBDEV_PAD_SINK,
5877 /* Try to enable YUV downscaling if ISP input is 10 % (either
5878 * width or height) bigger than the desired result. */
5879 if (isp_sink_crop.width * 9 / 10 < f->fmt.pix.width ||
5880 isp_sink_crop.height * 9 / 10 < f->fmt.pix.height ||
5881 (atomisp_subdev_format_conversion(asd, source_pad) &&
5882 ((asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
5883 !asd->continuous_mode->val) ||
5884 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) {
5885 /* for continuous mode, preview size might be smaller than
5886 * still capture size. if preview size still needs crop,
5887 * pick the larger one between crop size of preview and
5890 if (asd->continuous_mode->val
5891 && source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5892 && !crop_needs_override) {
5893 isp_sink_crop.width =
5894 max_t(unsigned int, f->fmt.pix.width,
5895 isp_sink_crop.width);
5896 isp_sink_crop.height =
5897 max_t(unsigned int, f->fmt.pix.height,
5898 isp_sink_crop.height);
5900 isp_sink_crop.width = f->fmt.pix.width;
5901 isp_sink_crop.height = f->fmt.pix.height;
5904 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5905 V4L2_SUBDEV_FORMAT_ACTIVE,
5906 ATOMISP_SUBDEV_PAD_SINK,
5908 V4L2_SEL_FLAG_KEEP_CONFIG,
5910 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5911 V4L2_SUBDEV_FORMAT_ACTIVE,
5912 source_pad, V4L2_SEL_TGT_COMPOSE,
5914 } else if (IS_MOFD) {
5915 struct v4l2_rect main_compose = {0};
5917 main_compose.width = isp_sink_crop.width;
5918 main_compose.height =
5919 DIV_ROUND_UP(main_compose.width * f->fmt.pix.height,
5921 if (main_compose.height > isp_sink_crop.height) {
5922 main_compose.height = isp_sink_crop.height;
5923 main_compose.width =
5924 DIV_ROUND_UP(main_compose.height *
5929 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5930 V4L2_SUBDEV_FORMAT_ACTIVE,
5932 V4L2_SEL_TGT_COMPOSE, 0,
5935 struct v4l2_rect sink_crop = {0};
5936 struct v4l2_rect main_compose = {0};
5938 main_compose.width = f->fmt.pix.width;
5939 main_compose.height = f->fmt.pix.height;
5941 /* WORKAROUND: this override is universally enabled in
5942 * GMIN to work around a CTS failures (GMINL-539)
5943 * which appears to be related by a hardware
5944 * performance limitation. It's unclear why this
5945 * particular code triggers the issue. */
5946 if (!atomisp_hw_is_isp2401 || crop_needs_override) {
5947 if (isp_sink_crop.width * main_compose.height >
5948 isp_sink_crop.height * main_compose.width) {
5949 sink_crop.height = isp_sink_crop.height;
5950 sink_crop.width = DIV_NEAREST_STEP(
5954 ATOM_ISP_STEP_WIDTH);
5956 sink_crop.width = isp_sink_crop.width;
5957 sink_crop.height = DIV_NEAREST_STEP(
5961 ATOM_ISP_STEP_HEIGHT);
5963 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5964 V4L2_SUBDEV_FORMAT_ACTIVE,
5965 ATOMISP_SUBDEV_PAD_SINK,
5967 V4L2_SEL_FLAG_KEEP_CONFIG,
5970 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5971 V4L2_SUBDEV_FORMAT_ACTIVE,
5973 V4L2_SEL_TGT_COMPOSE, 0,
5978 ret = atomisp_set_fmt_to_isp(vdev, &output_info, &raw_output_info,
5979 &f->fmt.pix, source_pad);
5983 pipe->pix.width = f->fmt.pix.width;
5984 pipe->pix.height = f->fmt.pix.height;
5985 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
5986 if (format_bridge->planar) {
5987 pipe->pix.bytesperline = output_info.padded_width;
5988 pipe->pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height *
5989 DIV_ROUND_UP(format_bridge->depth *
5990 output_info.padded_width, 8));
5992 pipe->pix.bytesperline =
5993 DIV_ROUND_UP(format_bridge->depth *
5994 output_info.padded_width, 8);
5995 pipe->pix.sizeimage =
5996 PAGE_ALIGN(f->fmt.pix.height * pipe->pix.bytesperline);
5998 if (f->fmt.pix.field == V4L2_FIELD_ANY)
5999 f->fmt.pix.field = V4L2_FIELD_NONE;
6000 pipe->pix.field = f->fmt.pix.field;
6002 f->fmt.pix = pipe->pix;
6003 f->fmt.pix.priv = PAGE_ALIGN(pipe->pix.width *
6004 pipe->pix.height * 2);
6006 pipe->capq.field = f->fmt.pix.field;
6009 * If in video 480P case, no GFX throttle
6011 if (asd->run_mode->val == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
6012 f->fmt.pix.width == 720 && f->fmt.pix.height == 480)
6013 isp->need_gfx_throttle = false;
6015 isp->need_gfx_throttle = true;
6020 int atomisp_set_fmt_file(struct video_device *vdev, struct v4l2_format *f)
6022 struct atomisp_device *isp = video_get_drvdata(vdev);
6023 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
6024 struct atomisp_sub_device *asd = pipe->asd;
6025 struct v4l2_mbus_framefmt ffmt = {0};
6026 const struct atomisp_format_bridge *format_bridge;
6027 struct v4l2_subdev_fh fh;
6030 v4l2_fh_init(&fh.vfh, vdev);
6032 dev_dbg(isp->dev, "setting fmt %ux%u 0x%x for file inject\n",
6033 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
6034 ret = atomisp_try_fmt_file(isp, f);
6036 dev_err(isp->dev, "atomisp_try_fmt_file err: %d\n", ret);
6040 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
6041 if (!format_bridge) {
6042 dev_dbg(isp->dev, "atomisp_get_format_bridge err! fmt:0x%x\n",
6043 f->fmt.pix.pixelformat);
6047 pipe->pix = f->fmt.pix;
6048 atomisp_css_input_set_mode(asd, CSS_INPUT_MODE_FIFO);
6049 atomisp_css_input_configure_port(asd,
6050 __get_mipi_port(isp, ATOMISP_CAMERA_PORT_PRIMARY), 2, 0xffff4,
6052 ffmt.width = f->fmt.pix.width;
6053 ffmt.height = f->fmt.pix.height;
6054 ffmt.code = format_bridge->mbus_code;
6056 atomisp_subdev_set_ffmt(&asd->subdev, fh.pad, V4L2_SUBDEV_FORMAT_ACTIVE,
6057 ATOMISP_SUBDEV_PAD_SINK, &ffmt);
6062 int atomisp_set_shading_table(struct atomisp_sub_device *asd,
6063 struct atomisp_shading_table *user_shading_table)
6065 struct atomisp_css_shading_table *shading_table;
6066 struct atomisp_css_shading_table *free_table;
6067 unsigned int len_table;
6071 if (!user_shading_table)
6074 if (!user_shading_table->enable) {
6075 atomisp_css_set_shading_table(asd, NULL);
6076 asd->params.sc_en = false;
6080 /* If enabling, all tables must be set */
6081 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
6082 if (!user_shading_table->data[i])
6086 /* Shading table size per color */
6087 if (!atomisp_hw_is_isp2401) {
6088 if (user_shading_table->width > ISP2400_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
6089 user_shading_table->height > ISP2400_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
6092 if (user_shading_table->width > ISP2401_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
6093 user_shading_table->height > ISP2401_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
6097 shading_table = atomisp_css_shading_table_alloc(
6098 user_shading_table->width, user_shading_table->height);
6102 len_table = user_shading_table->width * user_shading_table->height *
6103 ATOMISP_SC_TYPE_SIZE;
6104 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
6105 ret = copy_from_user(shading_table->data[i],
6106 (void __user *)user_shading_table->data[i],
6109 free_table = shading_table;
6114 shading_table->sensor_width = user_shading_table->sensor_width;
6115 shading_table->sensor_height = user_shading_table->sensor_height;
6116 shading_table->fraction_bits = user_shading_table->fraction_bits;
6118 free_table = asd->params.css_param.shading_table;
6119 asd->params.css_param.shading_table = shading_table;
6120 atomisp_css_set_shading_table(asd, shading_table);
6121 asd->params.sc_en = true;
6125 atomisp_css_shading_table_free(free_table);
6130 /*Turn off ISP dphy */
6131 int atomisp_ospm_dphy_down(struct atomisp_device *isp)
6133 unsigned long flags;
6136 dev_dbg(isp->dev, "%s\n", __func__);
6138 /* if ISP timeout, we can force powerdown */
6139 if (isp->isp_timeout)
6142 if (!atomisp_dev_users(isp))
6145 spin_lock_irqsave(&isp->lock, flags);
6146 isp->sw_contex.power_state = ATOM_ISP_POWER_DOWN;
6147 spin_unlock_irqrestore(&isp->lock, flags);
6150 * MRFLD IUNIT DPHY is located in an always-power-on island
6151 * MRFLD HW design need all CSI ports are disabled before
6152 * powering down the IUNIT.
6154 pci_read_config_dword(isp->pdev, MRFLD_PCI_CSI_CONTROL, ®);
6155 reg |= MRFLD_ALL_CSI_PORTS_OFF_MASK;
6156 pci_write_config_dword(isp->pdev, MRFLD_PCI_CSI_CONTROL, reg);
6160 /*Turn on ISP dphy */
6161 int atomisp_ospm_dphy_up(struct atomisp_device *isp)
6163 unsigned long flags;
6165 dev_dbg(isp->dev, "%s\n", __func__);
6167 spin_lock_irqsave(&isp->lock, flags);
6168 isp->sw_contex.power_state = ATOM_ISP_POWER_UP;
6169 spin_unlock_irqrestore(&isp->lock, flags);
6174 int atomisp_exif_makernote(struct atomisp_sub_device *asd,
6175 struct atomisp_makernote_info *config)
6177 struct v4l2_control ctrl;
6178 struct atomisp_device *isp = asd->isp;
6180 ctrl.id = V4L2_CID_FOCAL_ABSOLUTE;
6182 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6183 dev_warn(isp->dev, "failed to g_ctrl for focal length\n");
6186 config->focal_length = ctrl.value;
6189 ctrl.id = V4L2_CID_FNUMBER_ABSOLUTE;
6191 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6192 dev_warn(isp->dev, "failed to g_ctrl for f-number\n");
6195 config->f_number_curr = ctrl.value;
6198 ctrl.id = V4L2_CID_FNUMBER_RANGE;
6200 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6201 dev_warn(isp->dev, "failed to g_ctrl for f number range\n");
6204 config->f_number_range = ctrl.value;
6210 int atomisp_offline_capture_configure(struct atomisp_sub_device *asd,
6211 struct atomisp_cont_capture_conf *cvf_config)
6213 struct v4l2_ctrl *c;
6216 * In case of M10MO ZSL capture case, we need to issue a separate
6217 * capture request to M10MO which will output captured jpeg image
6220 asd->isp->inputs[asd->input_curr].camera->ctrl_handler,
6221 V4L2_CID_START_ZSL_CAPTURE);
6225 dev_dbg(asd->isp->dev, "%s trigger ZSL capture request\n",
6227 /* TODO: use the cvf_config */
6228 ret = v4l2_ctrl_s_ctrl(c, 1);
6232 return v4l2_ctrl_s_ctrl(c, 0);
6235 asd->params.offline_parm = *cvf_config;
6237 if (asd->params.offline_parm.num_captures) {
6238 if (asd->streaming == ATOMISP_DEVICE_STREAMING_DISABLED) {
6239 unsigned int init_raw_num;
6241 if (asd->enable_raw_buffer_lock->val) {
6243 ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES_LOCK_EN;
6244 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
6245 asd->params.video_dis_en)
6247 ATOMISP_CSS2_NUM_DVS_FRAME_DELAY;
6250 ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES;
6253 /* TODO: this can be removed once user-space
6254 * has been updated to use control API */
6255 asd->continuous_raw_buffer_size->val =
6257 asd->continuous_raw_buffer_size->val,
6258 asd->params.offline_parm.
6259 num_captures + init_raw_num);
6260 asd->continuous_raw_buffer_size->val =
6261 min_t(int, ATOMISP_CONT_RAW_FRAMES,
6262 asd->continuous_raw_buffer_size->val);
6264 asd->continuous_mode->val = true;
6266 asd->continuous_mode->val = false;
6267 __enable_continuous_mode(asd, false);
6274 * set auto exposure metering window to camera sensor
6276 int atomisp_s_ae_window(struct atomisp_sub_device *asd,
6277 struct atomisp_ae_window *arg)
6279 struct atomisp_device *isp = asd->isp;
6280 /* Coverity CID 298071 - initialzize struct */
6281 struct v4l2_subdev_selection sel = { 0 };
6283 sel.r.left = arg->x_left;
6284 sel.r.top = arg->y_top;
6285 sel.r.width = arg->x_right - arg->x_left + 1;
6286 sel.r.height = arg->y_bottom - arg->y_top + 1;
6288 if (v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
6289 pad, set_selection, NULL, &sel)) {
6290 dev_err(isp->dev, "failed to call sensor set_selection.\n");
6297 int atomisp_flash_enable(struct atomisp_sub_device *asd, int num_frames)
6299 struct atomisp_device *isp = asd->isp;
6301 if (num_frames < 0) {
6302 dev_dbg(isp->dev, "%s ERROR: num_frames: %d\n", __func__,
6306 /* a requested flash is still in progress. */
6307 if (num_frames && asd->params.flash_state != ATOMISP_FLASH_IDLE) {
6308 dev_dbg(isp->dev, "%s flash busy: %d frames left: %d\n",
6309 __func__, asd->params.flash_state,
6310 asd->params.num_flash_frames);
6314 asd->params.num_flash_frames = num_frames;
6315 asd->params.flash_state = ATOMISP_FLASH_REQUESTED;
6319 int atomisp_source_pad_to_stream_id(struct atomisp_sub_device *asd,
6320 uint16_t source_pad)
6323 struct atomisp_device *isp = asd->isp;
6325 if (isp->inputs[asd->input_curr].camera_caps->
6326 sensor[asd->sensor_curr].stream_num == 1)
6327 return ATOMISP_INPUT_STREAM_GENERAL;
6329 switch (source_pad) {
6330 case ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE:
6331 stream_id = ATOMISP_INPUT_STREAM_CAPTURE;
6333 case ATOMISP_SUBDEV_PAD_SOURCE_VF:
6334 stream_id = ATOMISP_INPUT_STREAM_POSTVIEW;
6336 case ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW:
6337 stream_id = ATOMISP_INPUT_STREAM_PREVIEW;
6339 case ATOMISP_SUBDEV_PAD_SOURCE_VIDEO:
6340 stream_id = ATOMISP_INPUT_STREAM_VIDEO;
6343 stream_id = ATOMISP_INPUT_STREAM_GENERAL;
6349 bool atomisp_is_vf_pipe(struct atomisp_video_pipe *pipe)
6351 struct atomisp_sub_device *asd = pipe->asd;
6353 if (pipe == &asd->video_out_vf)
6356 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
6357 pipe == &asd->video_out_preview)
6363 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id)
6365 struct atomisp_device *isp = asd->isp;
6367 if (!asd->enable_raw_buffer_lock->val) {
6368 dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__);
6371 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED) {
6372 dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n",
6373 __func__, exp_id, asd->streaming);
6376 if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) {
6377 dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id);
6383 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd)
6385 unsigned long flags;
6387 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6388 memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap));
6389 asd->raw_buffer_locked_count = 0;
6390 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6393 int atomisp_set_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
6396 unsigned long flags;
6398 if (__checking_exp_id(asd, exp_id))
6401 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6403 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6404 (*bitmap) |= (1 << bit);
6405 asd->raw_buffer_locked_count++;
6406 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6408 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
6409 __func__, exp_id, asd->raw_buffer_locked_count);
6411 /* Check if the raw buffer after next is still locked!!! */
6413 if (exp_id > ATOMISP_MAX_EXP_ID)
6414 exp_id -= ATOMISP_MAX_EXP_ID;
6415 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6417 if ((*bitmap) & (1 << bit)) {
6420 /* WORKAROUND unlock the raw buffer compulsively */
6421 ret = atomisp_css_exp_id_unlock(asd, exp_id);
6423 dev_err(asd->isp->dev,
6424 "%s exp_id is wrapping back to %d but force unlock failed,, err %d.\n",
6425 __func__, exp_id, ret);
6429 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6430 (*bitmap) &= ~(1 << bit);
6431 asd->raw_buffer_locked_count--;
6432 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6433 dev_warn(asd->isp->dev,
6434 "%s exp_id is wrapping back to %d but it is still locked so force unlock it, raw_buffer_locked_count %d\n",
6435 __func__, exp_id, asd->raw_buffer_locked_count);
6440 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id)
6443 unsigned long flags;
6446 if (__checking_exp_id(asd, exp_id))
6449 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6451 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6452 ret = ((*bitmap) & (1 << bit));
6453 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6457 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
6460 unsigned long flags;
6462 if (__is_raw_buffer_locked(asd, exp_id))
6465 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6467 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6468 (*bitmap) &= ~(1 << bit);
6469 asd->raw_buffer_locked_count--;
6470 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6472 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
6473 __func__, exp_id, asd->raw_buffer_locked_count);
6477 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id)
6479 struct atomisp_device *isp = asd->isp;
6480 int value = *exp_id;
6483 ret = __is_raw_buffer_locked(asd, value);
6485 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
6489 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
6490 ret = atomisp_css_exp_id_capture(asd, value);
6492 dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value);
6498 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id)
6500 struct atomisp_device *isp = asd->isp;
6501 int value = *exp_id;
6504 ret = __clear_raw_buffer_bitmap(asd, value);
6506 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
6510 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
6511 ret = atomisp_css_exp_id_unlock(asd, value);
6513 dev_err(isp->dev, "%s exp_id %d failed, err %d.\n",
6514 __func__, value, ret);
6519 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd,
6520 unsigned int *enable)
6527 value = *enable > 0 ? true : false;
6529 atomisp_en_dz_capt_pipe(asd, value);
6534 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event)
6536 if (!event || asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
6539 dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n",
6543 case V4L2_EVENT_FRAME_SYNC:
6544 atomisp_sof_event(asd);
6546 case V4L2_EVENT_FRAME_END:
6547 atomisp_eof_event(asd, 0);
6549 case V4L2_EVENT_ATOMISP_3A_STATS_READY:
6550 atomisp_3a_stats_ready_event(asd, 0);
6552 case V4L2_EVENT_ATOMISP_METADATA_READY:
6553 atomisp_metadata_ready_event(asd, 0);
6562 static int atomisp_get_pipe_id(struct atomisp_video_pipe *pipe)
6564 struct atomisp_sub_device *asd = pipe->asd;
6566 if (ATOMISP_USE_YUVPP(asd))
6567 return CSS_PIPE_ID_YUVPP;
6568 else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER)
6569 return CSS_PIPE_ID_VIDEO;
6570 else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT)
6571 return CSS_PIPE_ID_CAPTURE;
6572 else if (pipe == &asd->video_out_video_capture)
6573 return CSS_PIPE_ID_VIDEO;
6574 else if (pipe == &asd->video_out_vf)
6575 return CSS_PIPE_ID_CAPTURE;
6576 else if (pipe == &asd->video_out_preview) {
6577 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
6578 return CSS_PIPE_ID_VIDEO;
6580 return CSS_PIPE_ID_PREVIEW;
6581 } else if (pipe == &asd->video_out_capture) {
6583 return IA_CSS_PIPE_ID_COPY;
6585 return CSS_PIPE_ID_CAPTURE;
6589 dev_warn(asd->isp->dev, "%s failed to find proper pipe\n",
6591 return CSS_PIPE_ID_CAPTURE;
6594 int atomisp_get_invalid_frame_num(struct video_device *vdev,
6595 int *invalid_frame_num)
6597 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
6598 struct atomisp_sub_device *asd = pipe->asd;
6599 enum atomisp_css_pipe_id pipe_id;
6600 struct ia_css_pipe_info p_info;
6603 if (asd->isp->inputs[asd->input_curr].camera_caps->
6604 sensor[asd->sensor_curr].stream_num > 1) {
6606 *invalid_frame_num = 0;
6610 pipe_id = atomisp_get_pipe_id(pipe);
6611 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipes[pipe_id]) {
6612 dev_warn(asd->isp->dev,
6613 "%s pipe %d has not been created yet, do SET_FMT first!\n",
6618 ret = ia_css_pipe_get_info(
6619 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
6620 .pipes[pipe_id], &p_info);
6621 if (ret == IA_CSS_SUCCESS) {
6622 *invalid_frame_num = p_info.num_invalid_frames;
6625 dev_warn(asd->isp->dev, "%s get pipe infor failed %d\n",