1 // SPDX-License-Identifier: GPL-2.0
3 * Support for Medifield PNW Camera Imaging ISP subsystem.
5 * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
7 * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <linux/errno.h>
21 #include <linux/firmware.h>
22 #include <linux/pci.h>
23 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/kfifo.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/timer.h>
29 #include <asm/iosf_mbi.h>
31 #include <media/v4l2-event.h>
32 #include <media/videobuf-vmalloc.h>
34 #define CREATE_TRACE_POINTS
35 #include "atomisp_trace_event.h"
37 #include "atomisp_cmd.h"
38 #include "atomisp_common.h"
39 #include "atomisp_fops.h"
40 #include "atomisp_internal.h"
41 #include "atomisp_ioctl.h"
42 #include "atomisp-regs.h"
43 #include "atomisp_tables.h"
44 #include "atomisp_acc.h"
45 #include "atomisp_compat.h"
46 #include "atomisp_subdev.h"
47 #include "atomisp_dfs_tables.h"
51 #include "sh_css_hrt.h"
52 #include "sh_css_defs.h"
53 #include "system_global.h"
54 #include "sh_css_internal.h"
55 #include "sh_css_sp.h"
56 #include "gp_device.h"
57 #include "device_access.h"
60 #include "ia_css_types.h"
61 #include "ia_css_stream.h"
62 #include "ia_css_debug.h"
65 /* We should never need to run the flash for more than 2 frames.
66 * At 15fps this means 133ms. We set the timeout a bit longer.
67 * Each flash driver is supposed to set its own timeout, but
68 * just in case someone else changed the timeout, we set it
69 * here to make sure we don't damage the flash hardware. */
70 #define FLASH_TIMEOUT 800 /* ms */
75 void __user *user_ptr;
84 * get sensor:dis71430/ov2720 related info from v4l2_subdev->priv data field.
85 * subdev->priv is set in mrst.c
87 struct camera_mipi_info *atomisp_to_sensor_mipi_info(struct v4l2_subdev *sd)
89 return (struct camera_mipi_info *)v4l2_get_subdev_hostdata(sd);
93 * get struct atomisp_video_pipe from v4l2 video_device
95 struct atomisp_video_pipe *atomisp_to_video_pipe(struct video_device *dev)
97 return (struct atomisp_video_pipe *)
98 container_of(dev, struct atomisp_video_pipe, vdev);
102 * get struct atomisp_acc_pipe from v4l2 video_device
104 struct atomisp_acc_pipe *atomisp_to_acc_pipe(struct video_device *dev)
106 return (struct atomisp_acc_pipe *)
107 container_of(dev, struct atomisp_acc_pipe, vdev);
110 static unsigned short atomisp_get_sensor_fps(struct atomisp_sub_device *asd)
112 struct v4l2_subdev_frame_interval fi;
113 struct atomisp_device *isp = asd->isp;
115 unsigned short fps = 0;
118 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
119 video, g_frame_interval, &fi);
121 if (!ret && fi.interval.numerator)
122 fps = fi.interval.denominator / fi.interval.numerator;
128 * DFS progress is shown as follows:
129 * 1. Target frequency is calculated according to FPS/Resolution/ISP running
131 * 2. Ratio is calculated using formula: 2 * HPLL / target frequency - 1
132 * with proper rounding.
133 * 3. Set ratio to ISPFREQ40, 1 to FREQVALID and ISPFREQGUAR40
134 * to 200MHz in ISPSSPM1.
135 * 4. Wait for FREQVALID to be cleared by P-Unit.
136 * 5. Wait for field ISPFREQSTAT40 in ISPSSPM1 turn to ratio set in 3.
138 static int write_target_freq_to_hw(struct atomisp_device *isp,
139 unsigned int new_freq)
141 unsigned int ratio, timeout, guar_ratio;
145 if (!isp->hpll_freq) {
146 dev_err(isp->dev, "failed to get hpll_freq. no change to freq\n");
150 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
151 if (isp_sspm1 & ISP_FREQ_VALID_MASK) {
152 dev_dbg(isp->dev, "clearing ISPSSPM1 valid bit.\n");
153 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
154 isp_sspm1 & ~(1 << ISP_FREQ_VALID_OFFSET));
157 ratio = (2 * isp->hpll_freq + new_freq / 2) / new_freq - 1;
158 guar_ratio = (2 * isp->hpll_freq + 200 / 2) / 200 - 1;
160 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
161 isp_sspm1 &= ~(0x1F << ISP_REQ_FREQ_OFFSET);
163 for (i = 0; i < ISP_DFS_TRY_TIMES; i++) {
164 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
166 | ratio << ISP_REQ_FREQ_OFFSET
167 | 1 << ISP_FREQ_VALID_OFFSET
168 | guar_ratio << ISP_REQ_GUAR_FREQ_OFFSET);
170 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
172 while ((isp_sspm1 & ISP_FREQ_VALID_MASK) && timeout) {
173 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
174 dev_dbg(isp->dev, "waiting for ISPSSPM1 valid bit to be 0.\n");
184 dev_err(isp->dev, "DFS failed due to HW error.\n");
188 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
190 while (((isp_sspm1 >> ISP_FREQ_STAT_OFFSET) != ratio) && timeout) {
191 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
192 dev_dbg(isp->dev, "waiting for ISPSSPM1 status bit to be 0x%x.\n",
198 dev_err(isp->dev, "DFS target freq is rejected by HW.\n");
205 int atomisp_freq_scaling(struct atomisp_device *isp,
206 enum atomisp_dfs_mode mode,
209 /* FIXME! Only use subdev[0] status yet */
210 struct atomisp_sub_device *asd = &isp->asd[0];
211 const struct atomisp_dfs_config *dfs;
212 unsigned int new_freq;
213 struct atomisp_freq_scaling_rule curr_rules;
215 unsigned short fps = 0;
217 if (isp->sw_contex.power_state != ATOM_ISP_POWER_UP) {
218 dev_err(isp->dev, "DFS cannot proceed due to no power.\n");
222 if ((isp->pdev->device & ATOMISP_PCI_DEVICE_SOC_MASK) ==
223 ATOMISP_PCI_DEVICE_SOC_CHT && ATOMISP_USE_YUVPP(asd))
224 isp->dfs = &dfs_config_cht_soc;
228 if (dfs->lowest_freq == 0 || dfs->max_freq_at_vmin == 0 ||
229 dfs->highest_freq == 0 || dfs->dfs_table_size == 0 ||
231 dev_err(isp->dev, "DFS configuration is invalid.\n");
235 if (mode == ATOMISP_DFS_MODE_LOW) {
236 new_freq = dfs->lowest_freq;
240 if (mode == ATOMISP_DFS_MODE_MAX) {
241 new_freq = dfs->highest_freq;
245 fps = atomisp_get_sensor_fps(asd);
249 curr_rules.width = asd->fmt[asd->capture_pad].fmt.width;
250 curr_rules.height = asd->fmt[asd->capture_pad].fmt.height;
251 curr_rules.fps = fps;
252 curr_rules.run_mode = asd->run_mode->val;
254 * For continuous mode, we need to make the capture setting applied
255 * since preview mode, because there is no chance to do this when
256 * starting image capture.
258 if (asd->continuous_mode->val) {
259 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
260 curr_rules.run_mode = ATOMISP_RUN_MODE_SDV;
262 curr_rules.run_mode =
263 ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE;
266 /* search for the target frequency by looping freq rules*/
267 for (i = 0; i < dfs->dfs_table_size; i++) {
268 if (curr_rules.width != dfs->dfs_table[i].width &&
269 dfs->dfs_table[i].width != ISP_FREQ_RULE_ANY)
271 if (curr_rules.height != dfs->dfs_table[i].height &&
272 dfs->dfs_table[i].height != ISP_FREQ_RULE_ANY)
274 if (curr_rules.fps != dfs->dfs_table[i].fps &&
275 dfs->dfs_table[i].fps != ISP_FREQ_RULE_ANY)
277 if (curr_rules.run_mode != dfs->dfs_table[i].run_mode &&
278 dfs->dfs_table[i].run_mode != ISP_FREQ_RULE_ANY)
283 if (i == dfs->dfs_table_size)
284 new_freq = dfs->max_freq_at_vmin;
286 new_freq = dfs->dfs_table[i].isp_freq;
289 dev_dbg(isp->dev, "DFS target frequency=%d.\n", new_freq);
291 if ((new_freq == isp->sw_contex.running_freq) && !force)
294 dev_dbg(isp->dev, "Programming DFS frequency to %d\n", new_freq);
296 ret = write_target_freq_to_hw(isp, new_freq);
298 isp->sw_contex.running_freq = new_freq;
299 trace_ipu_pstate(new_freq, -1);
305 * reset and restore ISP
307 int atomisp_reset(struct atomisp_device *isp)
309 /* Reset ISP by power-cycling it */
312 dev_dbg(isp->dev, "%s\n", __func__);
313 atomisp_css_suspend(isp);
314 ret = atomisp_runtime_suspend(isp->dev);
316 dev_err(isp->dev, "atomisp_runtime_suspend failed, %d\n", ret);
317 ret = atomisp_mrfld_power_down(isp);
319 dev_err(isp->dev, "can not disable ISP power\n");
321 ret = atomisp_mrfld_power_up(isp);
323 dev_err(isp->dev, "can not enable ISP power\n");
324 ret = atomisp_runtime_resume(isp->dev);
326 dev_err(isp->dev, "atomisp_runtime_resume failed, %d\n", ret);
328 ret = atomisp_css_resume(isp);
330 isp->isp_fatal_error = true;
336 * interrupt disable functions
338 static void disable_isp_irq(enum hrt_isp_css_irq irq)
340 irq_disable_channel(IRQ0_ID, irq);
342 if (irq != hrt_isp_css_irq_sp)
345 cnd_sp_irq_enable(SP0_ID, false);
349 * interrupt clean function
351 static void clear_isp_irq(enum hrt_isp_css_irq irq)
353 irq_clear_all(IRQ0_ID);
356 void atomisp_msi_irq_init(struct atomisp_device *isp, struct pci_dev *dev)
361 pci_read_config_dword(dev, PCI_MSI_CAPID, &msg32);
362 msg32 |= 1 << MSI_ENABLE_BIT;
363 pci_write_config_dword(dev, PCI_MSI_CAPID, msg32);
365 msg32 = (1 << INTR_IER) | (1 << INTR_IIR);
366 pci_write_config_dword(dev, PCI_INTERRUPT_CTRL, msg32);
368 pci_read_config_word(dev, PCI_COMMAND, &msg16);
369 msg16 |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
370 PCI_COMMAND_INTX_DISABLE);
371 pci_write_config_word(dev, PCI_COMMAND, msg16);
374 void atomisp_msi_irq_uninit(struct atomisp_device *isp, struct pci_dev *dev)
379 pci_read_config_dword(dev, PCI_MSI_CAPID, &msg32);
380 msg32 &= ~(1 << MSI_ENABLE_BIT);
381 pci_write_config_dword(dev, PCI_MSI_CAPID, msg32);
384 pci_write_config_dword(dev, PCI_INTERRUPT_CTRL, msg32);
386 pci_read_config_word(dev, PCI_COMMAND, &msg16);
387 msg16 &= ~(PCI_COMMAND_MASTER);
388 pci_write_config_word(dev, PCI_COMMAND, msg16);
391 static void atomisp_sof_event(struct atomisp_sub_device *asd)
393 struct v4l2_event event = {0};
395 event.type = V4L2_EVENT_FRAME_SYNC;
396 event.u.frame_sync.frame_sequence = atomic_read(&asd->sof_count);
398 v4l2_event_queue(asd->subdev.devnode, &event);
401 void atomisp_eof_event(struct atomisp_sub_device *asd, uint8_t exp_id)
403 struct v4l2_event event = {0};
405 event.type = V4L2_EVENT_FRAME_END;
406 event.u.frame_sync.frame_sequence = exp_id;
408 v4l2_event_queue(asd->subdev.devnode, &event);
411 static void atomisp_3a_stats_ready_event(struct atomisp_sub_device *asd,
414 struct v4l2_event event = {0};
416 event.type = V4L2_EVENT_ATOMISP_3A_STATS_READY;
417 event.u.frame_sync.frame_sequence = exp_id;
419 v4l2_event_queue(asd->subdev.devnode, &event);
422 static void atomisp_metadata_ready_event(struct atomisp_sub_device *asd,
423 enum atomisp_metadata_type md_type)
425 struct v4l2_event event = {0};
427 event.type = V4L2_EVENT_ATOMISP_METADATA_READY;
428 event.u.data[0] = md_type;
430 v4l2_event_queue(asd->subdev.devnode, &event);
433 static void atomisp_reset_event(struct atomisp_sub_device *asd)
435 struct v4l2_event event = {0};
437 event.type = V4L2_EVENT_ATOMISP_CSS_RESET;
439 v4l2_event_queue(asd->subdev.devnode, &event);
442 static void print_csi_rx_errors(enum mipi_port_id port,
443 struct atomisp_device *isp)
447 atomisp_css_rx_get_irq_info(port, &infos);
449 dev_err(isp->dev, "CSI Receiver port %d errors:\n", port);
450 if (infos & IA_CSS_RX_IRQ_INFO_BUFFER_OVERRUN)
451 dev_err(isp->dev, " buffer overrun");
452 if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT)
453 dev_err(isp->dev, " start-of-transmission error");
454 if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT_SYNC)
455 dev_err(isp->dev, " start-of-transmission sync error");
456 if (infos & IA_CSS_RX_IRQ_INFO_ERR_CONTROL)
457 dev_err(isp->dev, " control error");
458 if (infos & IA_CSS_RX_IRQ_INFO_ERR_ECC_DOUBLE)
459 dev_err(isp->dev, " 2 or more ECC errors");
460 if (infos & IA_CSS_RX_IRQ_INFO_ERR_CRC)
461 dev_err(isp->dev, " CRC mismatch");
462 if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ID)
463 dev_err(isp->dev, " unknown error");
464 if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_SYNC)
465 dev_err(isp->dev, " frame sync error");
466 if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_DATA)
467 dev_err(isp->dev, " frame data error");
468 if (infos & IA_CSS_RX_IRQ_INFO_ERR_DATA_TIMEOUT)
469 dev_err(isp->dev, " data timeout");
470 if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ESC)
471 dev_err(isp->dev, " unknown escape command entry");
472 if (infos & IA_CSS_RX_IRQ_INFO_ERR_LINE_SYNC)
473 dev_err(isp->dev, " line sync error");
477 static void clear_irq_reg(struct atomisp_device *isp)
481 pci_read_config_dword(isp->pdev, PCI_INTERRUPT_CTRL, &msg_ret);
482 msg_ret |= 1 << INTR_IIR;
483 pci_write_config_dword(isp->pdev, PCI_INTERRUPT_CTRL, msg_ret);
486 static struct atomisp_sub_device *
487 __get_asd_from_port(struct atomisp_device *isp, enum mipi_port_id port)
491 /* Check which isp subdev to send eof */
492 for (i = 0; i < isp->num_of_streams; i++) {
493 struct atomisp_sub_device *asd = &isp->asd[i];
494 struct camera_mipi_info *mipi_info;
496 mipi_info = atomisp_to_sensor_mipi_info(
497 isp->inputs[asd->input_curr].camera);
499 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED &&
500 __get_mipi_port(isp, mipi_info->port) == port) {
508 /* interrupt handling function*/
509 irqreturn_t atomisp_isr(int irq, void *dev)
511 struct atomisp_device *isp = (struct atomisp_device *)dev;
512 struct atomisp_sub_device *asd;
513 struct atomisp_css_event eof_event;
514 unsigned int irq_infos = 0;
519 spin_lock_irqsave(&isp->lock, flags);
520 if (isp->sw_contex.power_state != ATOM_ISP_POWER_UP ||
521 !isp->css_initialized) {
522 spin_unlock_irqrestore(&isp->lock, flags);
525 err = atomisp_css_irq_translate(isp, &irq_infos);
527 spin_unlock_irqrestore(&isp->lock, flags);
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 & IA_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 & IA_CSS_IRQ_INFO_EVENTS_READY)
563 atomic_set(&asd->sequence,
564 atomic_read(&asd->sequence_temp));
567 if (irq_infos & IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF) {
568 dev_dbg_ratelimited(isp->dev,
569 "irq:0x%x (IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF)\n",
571 irq_infos &= ~IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF;
574 if ((irq_infos & IA_CSS_IRQ_INFO_INPUT_SYSTEM_ERROR) ||
575 (irq_infos & IA_CSS_IRQ_INFO_IF_ERROR)) {
576 /* handle mipi receiver error */
578 enum mipi_port_id port;
580 for (port = MIPI_PORT0_ID; port <= MIPI_PORT2_ID;
582 print_csi_rx_errors(port, isp);
583 atomisp_css_rx_get_irq_info(port, &rx_infos);
584 atomisp_css_rx_clear_irq_info(port, rx_infos);
588 if (irq_infos & IA_CSS_IRQ_INFO_ISYS_EVENTS_READY) {
589 while (ia_css_dequeue_isys_event(&eof_event.event) ==
591 /* EOF Event does not have the css_pipe returned */
592 asd = __get_asd_from_port(isp, eof_event.event.port);
594 dev_err(isp->dev, "%s:no subdev.event:%d",
595 __func__, eof_event.event.type);
599 atomisp_eof_event(asd, eof_event.event.exp_id);
600 dev_dbg_ratelimited(isp->dev,
601 "%s EOF exp_id %d, asd %d\n",
602 __func__, eof_event.event.exp_id,
606 irq_infos &= ~IA_CSS_IRQ_INFO_ISYS_EVENTS_READY;
611 spin_unlock_irqrestore(&isp->lock, flags);
613 dev_dbg_ratelimited(isp->dev, "irq:0x%x\n", irq_infos);
615 return IRQ_WAKE_THREAD;
618 spin_unlock_irqrestore(&isp->lock, flags);
621 dev_dbg_ratelimited(isp->dev, "irq:0x%x\n", irq_infos);
626 void atomisp_clear_css_buffer_counters(struct atomisp_sub_device *asd)
630 memset(asd->s3a_bufs_in_css, 0, sizeof(asd->s3a_bufs_in_css));
631 for (i = 0; i < ATOMISP_INPUT_STREAM_NUM; i++)
632 memset(asd->metadata_bufs_in_css[i], 0,
633 sizeof(asd->metadata_bufs_in_css[i]));
634 asd->dis_bufs_in_css = 0;
635 asd->video_out_capture.buffers_in_css = 0;
636 asd->video_out_vf.buffers_in_css = 0;
637 asd->video_out_preview.buffers_in_css = 0;
638 asd->video_out_video_capture.buffers_in_css = 0;
642 bool atomisp_buffers_queued(struct atomisp_sub_device *asd)
644 return asd->video_out_capture.buffers_in_css ||
645 asd->video_out_vf.buffers_in_css ||
646 asd->video_out_preview.buffers_in_css ||
647 asd->video_out_video_capture.buffers_in_css ?
652 bool atomisp_buffers_queued_pipe(struct atomisp_video_pipe *pipe)
654 return pipe->buffers_in_css ? true : false;
657 /* 0x100000 is the start of dmem inside SP */
658 #define SP_DMEM_BASE 0x100000
660 void dump_sp_dmem(struct atomisp_device *isp, unsigned int addr,
663 unsigned int data = 0;
664 unsigned int size32 = DIV_ROUND_UP(size, sizeof(u32));
666 dev_dbg(isp->dev, "atomisp_io_base:%p\n", atomisp_io_base);
667 dev_dbg(isp->dev, "%s, addr:0x%x, size: %d, size32: %d\n", __func__,
669 if (size32 * 4 + addr > 0x4000) {
670 dev_err(isp->dev, "illegal size (%d) or addr (0x%x)\n",
674 addr += SP_DMEM_BASE;
676 data = _hrt_master_port_uload_32(addr);
678 dev_dbg(isp->dev, "%s, \t [0x%x]:0x%x\n", __func__, addr, data);
679 addr += sizeof(unsigned int);
681 } while (size32 > 0);
684 static struct videobuf_buffer *atomisp_css_frame_to_vbuf(
685 struct atomisp_video_pipe *pipe, struct ia_css_frame *frame)
687 struct videobuf_vmalloc_memory *vm_mem;
688 struct ia_css_frame *handle;
691 for (i = 0; pipe->capq.bufs[i]; i++) {
692 vm_mem = pipe->capq.bufs[i]->priv;
693 handle = vm_mem->vaddr;
694 if (handle && handle->data == frame->data)
695 return pipe->capq.bufs[i];
701 static void atomisp_flush_video_pipe(struct atomisp_sub_device *asd,
702 struct atomisp_video_pipe *pipe)
704 unsigned long irqflags;
710 for (i = 0; pipe->capq.bufs[i]; i++) {
711 spin_lock_irqsave(&pipe->irq_lock, irqflags);
712 if (pipe->capq.bufs[i]->state == VIDEOBUF_ACTIVE ||
713 pipe->capq.bufs[i]->state == VIDEOBUF_QUEUED) {
714 pipe->capq.bufs[i]->ts = ktime_get_ns();
715 pipe->capq.bufs[i]->field_count =
716 atomic_read(&asd->sequence) << 1;
717 dev_dbg(asd->isp->dev, "release buffers on device %s\n",
719 if (pipe->capq.bufs[i]->state == VIDEOBUF_QUEUED)
720 list_del_init(&pipe->capq.bufs[i]->queue);
721 pipe->capq.bufs[i]->state = VIDEOBUF_ERROR;
722 wake_up(&pipe->capq.bufs[i]->done);
724 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
728 /* Returns queued buffers back to video-core */
729 void atomisp_flush_bufs_and_wakeup(struct atomisp_sub_device *asd)
731 atomisp_flush_video_pipe(asd, &asd->video_out_capture);
732 atomisp_flush_video_pipe(asd, &asd->video_out_vf);
733 atomisp_flush_video_pipe(asd, &asd->video_out_preview);
734 atomisp_flush_video_pipe(asd, &asd->video_out_video_capture);
737 /* clean out the parameters that did not apply */
738 void atomisp_flush_params_queue(struct atomisp_video_pipe *pipe)
740 struct atomisp_css_params_with_list *param;
742 while (!list_empty(&pipe->per_frame_params)) {
743 param = list_entry(pipe->per_frame_params.next,
744 struct atomisp_css_params_with_list, list);
745 list_del(¶m->list);
746 atomisp_free_css_parameters(¶m->params);
751 /* Re-queue per-frame parameters */
752 static void atomisp_recover_params_queue(struct atomisp_video_pipe *pipe)
754 struct atomisp_css_params_with_list *param;
757 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
758 param = pipe->frame_params[i];
760 list_add_tail(¶m->list, &pipe->per_frame_params);
761 pipe->frame_params[i] = NULL;
763 atomisp_handle_parameter_and_buffer(pipe);
766 /* find atomisp_video_pipe with css pipe id, buffer type and atomisp run_mode */
767 static struct atomisp_video_pipe *__atomisp_get_pipe(
768 struct atomisp_sub_device *asd,
769 enum atomisp_input_stream_id stream_id,
770 enum ia_css_pipe_id css_pipe_id,
771 enum ia_css_buffer_type buf_type)
773 struct atomisp_device *isp = asd->isp;
775 if (css_pipe_id == IA_CSS_PIPE_ID_COPY &&
776 isp->inputs[asd->input_curr].camera_caps->
777 sensor[asd->sensor_curr].stream_num > 1) {
779 case ATOMISP_INPUT_STREAM_PREVIEW:
780 return &asd->video_out_preview;
781 case ATOMISP_INPUT_STREAM_POSTVIEW:
782 return &asd->video_out_vf;
783 case ATOMISP_INPUT_STREAM_VIDEO:
784 return &asd->video_out_video_capture;
785 case ATOMISP_INPUT_STREAM_CAPTURE:
787 return &asd->video_out_capture;
791 /* video is same in online as in continuouscapture mode */
792 if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
794 * Disable vf_pp and run CSS in still capture mode. In this
795 * mode, CSS does not cause extra latency with buffering, but
796 * scaling is not available.
798 return &asd->video_out_capture;
799 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
801 * Disable vf_pp and run CSS in video mode. This allows using
802 * ISP scaling but it has one frame delay due to CSS internal
805 return &asd->video_out_video_capture;
806 } else if (css_pipe_id == IA_CSS_PIPE_ID_YUVPP) {
808 * to SOC camera, yuvpp pipe is run for capture/video/SDV/ZSL.
810 if (asd->continuous_mode->val) {
811 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
814 case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
815 return &asd->video_out_video_capture;
816 case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
817 return &asd->video_out_preview;
818 case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
819 return &asd->video_out_capture;
821 return &asd->video_out_vf;
823 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
826 case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
827 return &asd->video_out_preview;
828 case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
829 return &asd->video_out_capture;
831 return &asd->video_out_vf;
834 } else if (buf_type == IA_CSS_BUFFER_TYPE_OUTPUT_FRAME) {
835 switch (asd->run_mode->val) {
836 case ATOMISP_RUN_MODE_VIDEO:
837 return &asd->video_out_video_capture;
838 case ATOMISP_RUN_MODE_PREVIEW:
839 return &asd->video_out_preview;
841 return &asd->video_out_capture;
843 } else if (buf_type == IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
844 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
845 return &asd->video_out_preview;
847 return &asd->video_out_vf;
849 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
850 /* For online video or SDV video pipe. */
851 if (css_pipe_id == IA_CSS_PIPE_ID_VIDEO ||
852 css_pipe_id == IA_CSS_PIPE_ID_COPY) {
853 if (buf_type == IA_CSS_BUFFER_TYPE_OUTPUT_FRAME)
854 return &asd->video_out_video_capture;
855 return &asd->video_out_preview;
857 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
858 /* For online preview or ZSL preview pipe. */
859 if (css_pipe_id == IA_CSS_PIPE_ID_PREVIEW ||
860 css_pipe_id == IA_CSS_PIPE_ID_COPY)
861 return &asd->video_out_preview;
863 /* For capture pipe. */
864 if (buf_type == IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME)
865 return &asd->video_out_vf;
866 return &asd->video_out_capture;
869 enum atomisp_metadata_type
870 atomisp_get_metadata_type(struct atomisp_sub_device *asd,
871 enum ia_css_pipe_id pipe_id) {
872 if (!asd->continuous_mode->val)
873 return ATOMISP_MAIN_METADATA;
875 if (pipe_id == IA_CSS_PIPE_ID_CAPTURE) /* online capture pipe */
876 return ATOMISP_SEC_METADATA;
878 return ATOMISP_MAIN_METADATA;
881 void atomisp_buf_done(struct atomisp_sub_device *asd, int error,
882 enum ia_css_buffer_type buf_type,
883 enum ia_css_pipe_id css_pipe_id,
884 bool q_buffers, enum atomisp_input_stream_id stream_id)
886 struct videobuf_buffer *vb = NULL;
887 struct atomisp_video_pipe *pipe = NULL;
888 struct atomisp_css_buffer buffer;
889 bool requeue = false;
891 unsigned long irqflags;
892 struct ia_css_frame *frame = NULL;
893 struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf_tmp;
894 struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf_tmp;
895 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf_tmp;
896 enum atomisp_metadata_type md_type;
897 struct atomisp_device *isp = asd->isp;
898 struct v4l2_control ctrl;
899 bool reset_wdt_timer = false;
902 buf_type != IA_CSS_BUFFER_TYPE_METADATA &&
903 buf_type != IA_CSS_BUFFER_TYPE_3A_STATISTICS &&
904 buf_type != IA_CSS_BUFFER_TYPE_DIS_STATISTICS &&
905 buf_type != IA_CSS_BUFFER_TYPE_OUTPUT_FRAME &&
906 buf_type != IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
907 buf_type != IA_CSS_BUFFER_TYPE_RAW_OUTPUT_FRAME &&
908 buf_type != IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
909 buf_type != IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
910 dev_err(isp->dev, "%s, unsupported buffer type: %d\n",
915 memset(&buffer, 0, sizeof(struct atomisp_css_buffer));
916 buffer.css_buffer.type = buf_type;
917 err = atomisp_css_dequeue_buffer(asd, stream_id, css_pipe_id,
921 "atomisp_css_dequeue_buffer failed: 0x%x\n", err);
925 /* need to know the atomisp pipe for frame buffers */
926 pipe = __atomisp_get_pipe(asd, stream_id, css_pipe_id, buf_type);
928 dev_err(isp->dev, "error getting atomisp pipe\n");
933 case IA_CSS_BUFFER_TYPE_3A_STATISTICS:
934 list_for_each_entry_safe(s3a_buf, _s3a_buf_tmp,
935 &asd->s3a_stats_in_css, list) {
936 if (s3a_buf->s3a_data ==
937 buffer.css_buffer.data.stats_3a) {
938 list_del_init(&s3a_buf->list);
939 list_add_tail(&s3a_buf->list,
940 &asd->s3a_stats_ready);
945 asd->s3a_bufs_in_css[css_pipe_id]--;
946 atomisp_3a_stats_ready_event(asd, buffer.css_buffer.exp_id);
947 dev_dbg(isp->dev, "%s: s3a stat with exp_id %d is ready\n",
948 __func__, s3a_buf->s3a_data->exp_id);
950 case IA_CSS_BUFFER_TYPE_METADATA:
954 md_type = atomisp_get_metadata_type(asd, css_pipe_id);
955 list_for_each_entry_safe(md_buf, _md_buf_tmp,
956 &asd->metadata_in_css[md_type], list) {
957 if (md_buf->metadata ==
958 buffer.css_buffer.data.metadata) {
959 list_del_init(&md_buf->list);
960 list_add_tail(&md_buf->list,
961 &asd->metadata_ready[md_type]);
965 asd->metadata_bufs_in_css[stream_id][css_pipe_id]--;
966 atomisp_metadata_ready_event(asd, md_type);
967 dev_dbg(isp->dev, "%s: metadata with exp_id %d is ready\n",
968 __func__, md_buf->metadata->exp_id);
970 case IA_CSS_BUFFER_TYPE_DIS_STATISTICS:
971 list_for_each_entry_safe(dis_buf, _dis_buf_tmp,
972 &asd->dis_stats_in_css, list) {
973 if (dis_buf->dis_data ==
974 buffer.css_buffer.data.stats_dvs) {
975 spin_lock_irqsave(&asd->dis_stats_lock,
977 list_del_init(&dis_buf->list);
978 list_add(&dis_buf->list, &asd->dis_stats);
979 asd->params.dis_proj_data_valid = true;
980 spin_unlock_irqrestore(&asd->dis_stats_lock,
985 asd->dis_bufs_in_css--;
986 dev_dbg(isp->dev, "%s: dis stat with exp_id %d is ready\n",
987 __func__, dis_buf->dis_data->exp_id);
989 case IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME:
990 case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
991 if (atomisp_hw_is_isp2401)
992 reset_wdt_timer = true;
994 pipe->buffers_in_css--;
995 frame = buffer.css_buffer.data.frame;
1004 * YUVPP doesn't set postview exp_id correctlly in SDV mode.
1005 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
1007 if (IS_BYT && buf_type == IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
1008 asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
1009 frame->exp_id = (asd->postview_exp_id++) %
1010 (ATOMISP_MAX_EXP_ID + 1);
1012 dev_dbg(isp->dev, "%s: vf frame with exp_id %d is ready\n",
1013 __func__, frame->exp_id);
1014 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
1015 if (frame->flash_state
1016 == IA_CSS_FRAME_FLASH_STATE_PARTIAL)
1017 dev_dbg(isp->dev, "%s thumb partially flashed\n",
1019 else if (frame->flash_state
1020 == IA_CSS_FRAME_FLASH_STATE_FULL)
1021 dev_dbg(isp->dev, "%s thumb completely flashed\n",
1024 dev_dbg(isp->dev, "%s thumb no flash in this frame\n",
1027 vb = atomisp_css_frame_to_vbuf(pipe, frame);
1030 pipe->frame_config_id[vb->i] = frame->isp_config_id;
1031 if (css_pipe_id == IA_CSS_PIPE_ID_CAPTURE &&
1032 asd->pending_capture_request > 0) {
1033 err = atomisp_css_offline_capture_configure(asd,
1034 asd->params.offline_parm.num_captures,
1035 asd->params.offline_parm.skip_frames,
1036 asd->params.offline_parm.offset);
1038 asd->pending_capture_request--;
1040 if (atomisp_hw_is_isp2401)
1041 asd->re_trigger_capture = false;
1043 dev_dbg(isp->dev, "Trigger capture again for new buffer. err=%d\n",
1045 } else if (atomisp_hw_is_isp2401) {
1046 asd->re_trigger_capture = true;
1049 case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
1050 case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
1051 if (atomisp_hw_is_isp2401)
1052 reset_wdt_timer = true;
1054 pipe->buffers_in_css--;
1055 frame = buffer.css_buffer.data.frame;
1065 * YUVPP doesn't set preview exp_id correctlly in ZSL mode.
1066 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
1068 if (IS_BYT && buf_type == IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
1069 asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
1070 frame->exp_id = (asd->preview_exp_id++) %
1071 (ATOMISP_MAX_EXP_ID + 1);
1073 dev_dbg(isp->dev, "%s: main frame with exp_id %d is ready\n",
1074 __func__, frame->exp_id);
1075 vb = atomisp_css_frame_to_vbuf(pipe, frame);
1081 /* free the parameters */
1082 if (pipe->frame_params[vb->i]) {
1083 if (asd->params.dvs_6axis ==
1084 pipe->frame_params[vb->i]->params.dvs_6axis)
1085 asd->params.dvs_6axis = NULL;
1086 atomisp_free_css_parameters(
1087 &pipe->frame_params[vb->i]->params);
1088 kvfree(pipe->frame_params[vb->i]);
1089 pipe->frame_params[vb->i] = NULL;
1092 pipe->frame_config_id[vb->i] = frame->isp_config_id;
1093 ctrl.id = V4L2_CID_FLASH_MODE;
1094 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
1095 if (frame->flash_state
1096 == IA_CSS_FRAME_FLASH_STATE_PARTIAL) {
1097 asd->frame_status[vb->i] =
1098 ATOMISP_FRAME_STATUS_FLASH_PARTIAL;
1099 dev_dbg(isp->dev, "%s partially flashed\n",
1101 } else if (frame->flash_state
1102 == IA_CSS_FRAME_FLASH_STATE_FULL) {
1103 asd->frame_status[vb->i] =
1104 ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
1105 asd->params.num_flash_frames--;
1106 dev_dbg(isp->dev, "%s completely flashed\n",
1109 asd->frame_status[vb->i] =
1110 ATOMISP_FRAME_STATUS_OK;
1112 "%s no flash in this frame\n",
1116 /* Check if flashing sequence is done */
1117 if (asd->frame_status[vb->i] ==
1118 ATOMISP_FRAME_STATUS_FLASH_EXPOSED)
1119 asd->params.flash_state = ATOMISP_FLASH_DONE;
1120 } else if (isp->flash) {
1121 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) ==
1122 0 && ctrl.value == ATOMISP_FLASH_MODE_TORCH) {
1123 ctrl.id = V4L2_CID_FLASH_TORCH_INTENSITY;
1124 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl)
1125 == 0 && ctrl.value > 0) {
1126 asd->frame_status[vb->i] =
1127 ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
1129 asd->frame_status[vb->i] =
1130 ATOMISP_FRAME_STATUS_OK;
1133 asd->frame_status[vb->i] =
1134 ATOMISP_FRAME_STATUS_OK;
1136 asd->frame_status[vb->i] = ATOMISP_FRAME_STATUS_OK;
1139 asd->params.last_frame_status = asd->frame_status[vb->i];
1141 if (asd->continuous_mode->val) {
1142 if (css_pipe_id == IA_CSS_PIPE_ID_PREVIEW ||
1143 css_pipe_id == IA_CSS_PIPE_ID_VIDEO) {
1144 asd->latest_preview_exp_id = frame->exp_id;
1145 } else if (css_pipe_id ==
1146 IA_CSS_PIPE_ID_CAPTURE) {
1147 if (asd->run_mode->val ==
1148 ATOMISP_RUN_MODE_VIDEO)
1149 dev_dbg(isp->dev, "SDV capture raw buffer id: %u\n",
1152 dev_dbg(isp->dev, "ZSL capture raw buffer id: %u\n",
1157 * Only after enabled the raw buffer lock
1158 * and in continuous mode.
1159 * in preview/video pipe, each buffer will
1160 * be locked automatically, so record it here.
1162 if (((css_pipe_id == IA_CSS_PIPE_ID_PREVIEW) ||
1163 (css_pipe_id == IA_CSS_PIPE_ID_VIDEO)) &&
1164 asd->enable_raw_buffer_lock->val &&
1165 asd->continuous_mode->val) {
1166 atomisp_set_raw_buffer_bitmap(asd, frame->exp_id);
1167 WARN_ON(frame->exp_id > ATOMISP_MAX_EXP_ID);
1170 if (asd->params.css_update_params_needed) {
1171 atomisp_apply_css_parameters(asd,
1172 &asd->params.css_param);
1173 if (asd->params.css_param.update_flag.dz_config)
1174 asd->params.config.dz_config = &asd->params.css_param.dz_config;
1175 /* New global dvs 6axis config should be blocked
1176 * here if there's a buffer with per-frame parameters
1177 * pending in CSS frame buffer queue.
1178 * This is to aviod zooming vibration since global
1179 * parameters take effect immediately while
1180 * per-frame parameters are taken after previous
1181 * buffers in CSS got processed.
1183 if (asd->params.dvs_6axis)
1184 atomisp_css_set_dvs_6axis(asd,
1185 asd->params.dvs_6axis);
1187 asd->params.css_update_params_needed = false;
1188 /* The update flag should not be cleaned here
1189 * since it is still going to be used to make up
1190 * following per-frame parameters.
1191 * This will introduce more copy work since each
1192 * time when updating global parameters, the whole
1193 * parameter set are applied.
1194 * FIXME: A new set of parameter copy functions can
1195 * be added to make up per-frame parameters based on
1196 * solid structures stored in asd->params.css_param
1197 * instead of using shadow pointers in update flag.
1199 atomisp_css_update_isp_params(asd);
1207 vb->ts = ktime_get_ns();
1208 vb->field_count = atomic_read(&asd->sequence) << 1;
1209 /*mark videobuffer done for dequeue*/
1210 spin_lock_irqsave(&pipe->irq_lock, irqflags);
1211 vb->state = !error ? VIDEOBUF_DONE : VIDEOBUF_ERROR;
1212 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
1215 * Frame capture done, wake up any process block on
1216 * current active buffer
1217 * possibly hold by videobuf_dqbuf()
1221 if (atomisp_hw_is_isp2401)
1222 atomic_set(&pipe->wdt_count, 0);
1225 * Requeue should only be done for 3a and dis buffers.
1226 * Queue/dequeue order will change if driver recycles image buffers.
1230 err = atomisp_css_queue_buffer(asd,
1231 stream_id, css_pipe_id,
1234 dev_err(isp->dev, "%s, q to css fails: %d\n",
1238 if (!error && q_buffers)
1239 atomisp_qbuffers_to_css(asd);
1241 if (atomisp_hw_is_isp2401) {
1242 /* If there are no buffers queued then
1243 * delete wdt timer. */
1244 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1246 if (!atomisp_buffers_queued_pipe(pipe))
1247 atomisp_wdt_stop_pipe(pipe, false);
1248 else if (reset_wdt_timer)
1249 /* SOF irq should not reset wdt timer. */
1250 atomisp_wdt_refresh_pipe(pipe,
1251 ATOMISP_WDT_KEEP_CURRENT_DELAY);
1255 void atomisp_delayed_init_work(struct work_struct *work)
1257 struct atomisp_sub_device *asd = container_of(work,
1258 struct atomisp_sub_device,
1261 * to SOC camera, use yuvpp pipe and no support continuous mode.
1263 if (!ATOMISP_USE_YUVPP(asd)) {
1264 struct v4l2_event event = {0};
1265 struct ia_css_stream *stream;
1267 stream = asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
1270 if (ia_css_alloc_continuous_frame_remain(stream))
1273 ia_css_update_continuous_frames(stream);
1275 event.type = V4L2_EVENT_ATOMISP_RAW_BUFFERS_ALLOC_DONE;
1276 v4l2_event_queue(asd->subdev.devnode, &event);
1279 /* signal streamon after delayed init is done */
1280 asd->delayed_init = ATOMISP_DELAYED_INIT_DONE;
1281 complete(&asd->init_done);
1284 static void __atomisp_css_recover(struct atomisp_device *isp, bool isp_timeout)
1286 enum ia_css_pipe_id css_pipe_id;
1287 bool stream_restart[MAX_STREAM_NUM] = {0};
1288 bool depth_mode = false;
1289 int i, ret, depth_cnt = 0;
1291 if (!isp->sw_contex.file_input)
1292 atomisp_css_irq_enable(isp,
1293 IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
1295 BUG_ON(isp->num_of_streams > MAX_STREAM_NUM);
1297 for (i = 0; i < isp->num_of_streams; i++) {
1298 struct atomisp_sub_device *asd = &isp->asd[i];
1299 struct ia_css_pipeline *acc_pipeline;
1300 struct ia_css_pipe *acc_pipe = NULL;
1302 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED &&
1303 !asd->stream_prepared)
1307 * AtomISP::waitStageUpdate is blocked when WDT happens.
1308 * By calling acc_done() for all loaded fw_handles,
1309 * HAL will be unblocked.
1311 acc_pipe = asd->stream_env[i].pipes[IA_CSS_PIPE_ID_ACC];
1313 acc_pipeline = ia_css_pipe_get_pipeline(acc_pipe);
1315 struct ia_css_pipeline_stage *stage;
1317 for (stage = acc_pipeline->stages; stage;
1318 stage = stage->next) {
1319 const struct ia_css_fw_info *fw;
1321 fw = stage->firmware;
1322 atomisp_acc_done(asd, fw->handle);
1329 if (asd->delayed_init == ATOMISP_DELAYED_INIT_QUEUED)
1330 cancel_work_sync(&asd->delayed_init_work);
1332 complete(&asd->init_done);
1333 asd->delayed_init = ATOMISP_DELAYED_INIT_NOT_QUEUED;
1335 stream_restart[asd->index] = true;
1337 asd->streaming = ATOMISP_DEVICE_STREAMING_STOPPING;
1339 /* stream off sensor */
1340 ret = v4l2_subdev_call(
1341 isp->inputs[asd->input_curr].
1342 camera, video, s_stream, 0);
1345 "can't stop streaming on sensor!\n");
1347 atomisp_acc_unload_extensions(asd);
1349 atomisp_clear_css_buffer_counters(asd);
1351 css_pipe_id = atomisp_get_css_pipe_id(asd);
1352 atomisp_css_stop(asd, css_pipe_id, true);
1354 asd->streaming = ATOMISP_DEVICE_STREAMING_DISABLED;
1356 asd->preview_exp_id = 1;
1357 asd->postview_exp_id = 1;
1358 /* notify HAL the CSS reset */
1360 "send reset event to %s\n", asd->subdev.devnode->name);
1361 atomisp_reset_event(asd);
1365 disable_isp_irq(hrt_isp_css_irq_sp);
1366 clear_isp_irq(hrt_isp_css_irq_sp);
1368 /* Set the SRSE to 3 before resetting */
1369 pci_write_config_dword(isp->pdev, PCI_I_CONTROL, isp->saved_regs.i_control |
1370 MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
1372 /* reset ISP and restore its state */
1373 isp->isp_timeout = true;
1375 isp->isp_timeout = false;
1378 for (i = 0; i < isp->num_of_streams; i++) {
1379 if (isp->asd[i].depth_mode->val)
1384 for (i = 0; i < isp->num_of_streams; i++) {
1385 struct atomisp_sub_device *asd = &isp->asd[i];
1387 if (!stream_restart[i])
1390 if (isp->inputs[asd->input_curr].type != FILE_INPUT)
1391 atomisp_css_input_set_mode(asd,
1392 IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
1394 css_pipe_id = atomisp_get_css_pipe_id(asd);
1395 if (atomisp_css_start(asd, css_pipe_id, true))
1397 "start SP failed, so do not set streaming to be enable!\n");
1399 asd->streaming = ATOMISP_DEVICE_STREAMING_ENABLED;
1401 atomisp_csi2_configure(asd);
1404 if (!isp->sw_contex.file_input) {
1405 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
1406 atomisp_css_valid_sof(isp));
1408 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, true) < 0)
1409 dev_dbg(isp->dev, "dfs failed!\n");
1411 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_MAX, true) < 0)
1412 dev_dbg(isp->dev, "dfs failed!\n");
1415 for (i = 0; i < isp->num_of_streams; i++) {
1416 struct atomisp_sub_device *asd;
1420 if (!stream_restart[i])
1423 if (asd->continuous_mode->val &&
1424 asd->delayed_init == ATOMISP_DELAYED_INIT_NOT_QUEUED) {
1425 reinit_completion(&asd->init_done);
1426 asd->delayed_init = ATOMISP_DELAYED_INIT_QUEUED;
1427 queue_work(asd->delayed_init_workq,
1428 &asd->delayed_init_work);
1431 * dequeueing buffers is not needed. CSS will recycle
1432 * buffers that it has.
1434 atomisp_flush_bufs_and_wakeup(asd);
1436 /* Requeue unprocessed per-frame parameters. */
1437 atomisp_recover_params_queue(&asd->video_out_capture);
1438 atomisp_recover_params_queue(&asd->video_out_preview);
1439 atomisp_recover_params_queue(&asd->video_out_video_capture);
1441 if ((asd->depth_mode->val) &&
1442 (depth_cnt == ATOMISP_DEPTH_SENSOR_STREAMON_COUNT)) {
1447 ret = v4l2_subdev_call(
1448 isp->inputs[asd->input_curr].camera, video,
1452 "can't start streaming on sensor!\n");
1456 if (atomisp_stream_on_master_slave_sensor(isp, true))
1458 "master slave sensor stream on failed!\n");
1462 void atomisp_wdt_work(struct work_struct *work)
1464 struct atomisp_device *isp = container_of(work, struct atomisp_device,
1467 unsigned int pipe_wdt_cnt[MAX_STREAM_NUM][4] = { {0} };
1468 bool css_recover = false;
1470 rt_mutex_lock(&isp->mutex);
1471 if (!atomisp_streaming_count(isp)) {
1472 atomic_set(&isp->wdt_work_queued, 0);
1473 rt_mutex_unlock(&isp->mutex);
1477 if (!atomisp_hw_is_isp2401) {
1478 dev_err(isp->dev, "timeout %d of %d\n",
1479 atomic_read(&isp->wdt_count) + 1,
1480 ATOMISP_ISP_MAX_TIMEOUT_COUNT);
1482 if (atomic_inc_return(&isp->wdt_count) < ATOMISP_ISP_MAX_TIMEOUT_COUNT)
1487 for (i = 0; i < isp->num_of_streams; i++) {
1488 struct atomisp_sub_device *asd = &isp->asd[i];
1490 pipe_wdt_cnt[i][0] +=
1491 atomic_read(&asd->video_out_capture.wdt_count);
1492 pipe_wdt_cnt[i][1] +=
1493 atomic_read(&asd->video_out_vf.wdt_count);
1494 pipe_wdt_cnt[i][2] +=
1495 atomic_read(&asd->video_out_preview.wdt_count);
1496 pipe_wdt_cnt[i][3] +=
1497 atomic_read(&asd->video_out_video_capture.wdt_count);
1499 (pipe_wdt_cnt[i][0] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1500 pipe_wdt_cnt[i][1] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1501 pipe_wdt_cnt[i][2] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1502 pipe_wdt_cnt[i][3] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT)
1505 "pipe on asd%d timeout cnt: (%d, %d, %d, %d) of %d, recover = %d\n",
1506 asd->index, pipe_wdt_cnt[i][0], pipe_wdt_cnt[i][1],
1507 pipe_wdt_cnt[i][2], pipe_wdt_cnt[i][3],
1508 ATOMISP_ISP_MAX_TIMEOUT_COUNT, css_recover);
1513 ia_css_debug_dump_sp_sw_debug_info();
1514 ia_css_debug_dump_debug_info(__func__);
1515 for (i = 0; i < isp->num_of_streams; i++) {
1516 struct atomisp_sub_device *asd = &isp->asd[i];
1518 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1520 dev_err(isp->dev, "%s, vdev %s buffers in css: %d\n",
1522 asd->video_out_capture.vdev.name,
1523 asd->video_out_capture.
1526 "%s, vdev %s buffers in css: %d\n",
1528 asd->video_out_vf.vdev.name,
1532 "%s, vdev %s buffers in css: %d\n",
1534 asd->video_out_preview.vdev.name,
1535 asd->video_out_preview.
1538 "%s, vdev %s buffers in css: %d\n",
1540 asd->video_out_video_capture.vdev.name,
1541 asd->video_out_video_capture.
1544 "%s, s3a buffers in css preview pipe:%d\n",
1546 asd->s3a_bufs_in_css[IA_CSS_PIPE_ID_PREVIEW]);
1548 "%s, s3a buffers in css capture pipe:%d\n",
1550 asd->s3a_bufs_in_css[IA_CSS_PIPE_ID_CAPTURE]);
1552 "%s, s3a buffers in css video pipe:%d\n",
1554 asd->s3a_bufs_in_css[IA_CSS_PIPE_ID_VIDEO]);
1556 "%s, dis buffers in css: %d\n",
1557 __func__, asd->dis_bufs_in_css);
1559 "%s, metadata buffers in css preview pipe:%d\n",
1561 asd->metadata_bufs_in_css
1562 [ATOMISP_INPUT_STREAM_GENERAL]
1563 [IA_CSS_PIPE_ID_PREVIEW]);
1565 "%s, metadata buffers in css capture pipe:%d\n",
1567 asd->metadata_bufs_in_css
1568 [ATOMISP_INPUT_STREAM_GENERAL]
1569 [IA_CSS_PIPE_ID_CAPTURE]);
1571 "%s, metadata buffers in css video pipe:%d\n",
1573 asd->metadata_bufs_in_css
1574 [ATOMISP_INPUT_STREAM_GENERAL]
1575 [IA_CSS_PIPE_ID_VIDEO]);
1576 if (asd->enable_raw_buffer_lock->val) {
1579 dev_err(isp->dev, "%s, raw_buffer_locked_count %d\n",
1580 __func__, asd->raw_buffer_locked_count);
1581 for (j = 0; j <= ATOMISP_MAX_EXP_ID / 32; j++)
1582 dev_err(isp->dev, "%s, raw_buffer_bitmap[%d]: 0x%x\n",
1584 asd->raw_buffer_bitmap[j]);
1588 /*sh_css_dump_sp_state();*/
1589 /*sh_css_dump_isp_state();*/
1591 for (i = 0; i < isp->num_of_streams; i++) {
1592 struct atomisp_sub_device *asd = &isp->asd[i];
1594 if (asd->streaming ==
1595 ATOMISP_DEVICE_STREAMING_ENABLED) {
1596 atomisp_clear_css_buffer_counters(asd);
1597 atomisp_flush_bufs_and_wakeup(asd);
1598 complete(&asd->init_done);
1600 if (atomisp_hw_is_isp2401)
1601 atomisp_wdt_stop(asd, false);
1604 if (!atomisp_hw_is_isp2401) {
1605 atomic_set(&isp->wdt_count, 0);
1607 isp->isp_fatal_error = true;
1608 atomic_set(&isp->wdt_work_queued, 0);
1610 rt_mutex_unlock(&isp->mutex);
1615 __atomisp_css_recover(isp, true);
1616 if (atomisp_hw_is_isp2401) {
1617 for (i = 0; i < isp->num_of_streams; i++) {
1618 struct atomisp_sub_device *asd = &isp->asd[i];
1620 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1623 atomisp_wdt_refresh(asd,
1624 isp->sw_contex.file_input ?
1625 ATOMISP_ISP_FILE_TIMEOUT_DURATION :
1626 ATOMISP_ISP_TIMEOUT_DURATION);
1630 dev_err(isp->dev, "timeout recovery handling done\n");
1631 atomic_set(&isp->wdt_work_queued, 0);
1633 rt_mutex_unlock(&isp->mutex);
1636 void atomisp_css_flush(struct atomisp_device *isp)
1640 if (!atomisp_streaming_count(isp))
1644 for (i = 0; i < isp->num_of_streams; i++) {
1645 struct atomisp_sub_device *asd = &isp->asd[i];
1647 atomisp_wdt_stop(asd, true);
1651 __atomisp_css_recover(isp, false);
1653 for (i = 0; i < isp->num_of_streams; i++) {
1654 struct atomisp_sub_device *asd = &isp->asd[i];
1656 if (asd->streaming !=
1657 ATOMISP_DEVICE_STREAMING_ENABLED)
1660 atomisp_wdt_refresh(asd,
1661 isp->sw_contex.file_input ?
1662 ATOMISP_ISP_FILE_TIMEOUT_DURATION :
1663 ATOMISP_ISP_TIMEOUT_DURATION);
1665 dev_dbg(isp->dev, "atomisp css flush done\n");
1668 void atomisp_wdt(struct timer_list *t)
1670 struct atomisp_sub_device *asd;
1671 struct atomisp_device *isp;
1673 if (!atomisp_hw_is_isp2401) {
1674 asd = from_timer(asd, t, wdt);
1677 struct atomisp_video_pipe *pipe = from_timer(pipe, t, wdt);
1682 atomic_inc(&pipe->wdt_count);
1684 "[WARNING]asd %d pipe %s ISP timeout %d!\n",
1685 asd->index, pipe->vdev.name,
1686 atomic_read(&pipe->wdt_count));
1689 if (atomic_read(&isp->wdt_work_queued)) {
1690 dev_dbg(isp->dev, "ISP watchdog was put into workqueue\n");
1693 atomic_set(&isp->wdt_work_queued, 1);
1694 queue_work(isp->wdt_work_queue, &isp->wdt_work);
1698 void atomisp_wdt_start(struct atomisp_sub_device *asd)
1700 atomisp_wdt_refresh(asd, ATOMISP_ISP_TIMEOUT_DURATION);
1704 void atomisp_wdt_refresh_pipe(struct atomisp_video_pipe *pipe,
1709 if (delay != ATOMISP_WDT_KEEP_CURRENT_DELAY)
1710 pipe->wdt_duration = delay;
1712 next = jiffies + pipe->wdt_duration;
1714 /* Override next if it has been pushed beyon the "next" time */
1715 if (atomisp_is_wdt_running(pipe) && time_after(pipe->wdt_expires, next))
1716 next = pipe->wdt_expires;
1718 pipe->wdt_expires = next;
1720 if (atomisp_is_wdt_running(pipe))
1721 dev_dbg(pipe->asd->isp->dev, "WDT will hit after %d ms (%s)\n",
1722 ((int)(next - jiffies) * 1000 / HZ), pipe->vdev.name);
1724 dev_dbg(pipe->asd->isp->dev, "WDT starts with %d ms period (%s)\n",
1725 ((int)(next - jiffies) * 1000 / HZ), pipe->vdev.name);
1727 mod_timer(&pipe->wdt, next);
1730 void atomisp_wdt_refresh(struct atomisp_sub_device *asd, unsigned int delay)
1732 if (!atomisp_hw_is_isp2401) {
1735 if (delay != ATOMISP_WDT_KEEP_CURRENT_DELAY)
1736 asd->wdt_duration = delay;
1738 next = jiffies + asd->wdt_duration;
1740 /* Override next if it has been pushed beyon the "next" time */
1741 if (atomisp_is_wdt_running(asd) && time_after(asd->wdt_expires, next))
1742 next = asd->wdt_expires;
1744 asd->wdt_expires = next;
1746 if (atomisp_is_wdt_running(asd))
1747 dev_dbg(asd->isp->dev, "WDT will hit after %d ms\n",
1748 ((int)(next - jiffies) * 1000 / HZ));
1750 dev_dbg(asd->isp->dev, "WDT starts with %d ms period\n",
1751 ((int)(next - jiffies) * 1000 / HZ));
1753 mod_timer(&asd->wdt, next);
1754 atomic_set(&asd->isp->wdt_count, 0);
1756 dev_dbg(asd->isp->dev, "WDT refresh all:\n");
1757 if (atomisp_is_wdt_running(&asd->video_out_capture))
1758 atomisp_wdt_refresh_pipe(&asd->video_out_capture, delay);
1759 if (atomisp_is_wdt_running(&asd->video_out_preview))
1760 atomisp_wdt_refresh_pipe(&asd->video_out_preview, delay);
1761 if (atomisp_is_wdt_running(&asd->video_out_vf))
1762 atomisp_wdt_refresh_pipe(&asd->video_out_vf, delay);
1763 if (atomisp_is_wdt_running(&asd->video_out_video_capture))
1764 atomisp_wdt_refresh_pipe(&asd->video_out_video_capture, delay);
1769 void atomisp_wdt_stop_pipe(struct atomisp_video_pipe *pipe, bool sync)
1771 if (!atomisp_is_wdt_running(pipe))
1774 dev_dbg(pipe->asd->isp->dev,
1775 "WDT stop asd %d (%s)\n", pipe->asd->index, pipe->vdev.name);
1778 del_timer_sync(&pipe->wdt);
1779 cancel_work_sync(&pipe->asd->isp->wdt_work);
1781 del_timer(&pipe->wdt);
1786 void atomisp_wdt_start_pipe(struct atomisp_video_pipe *pipe)
1788 atomisp_wdt_refresh_pipe(pipe, ATOMISP_ISP_TIMEOUT_DURATION);
1791 void atomisp_wdt_stop(struct atomisp_sub_device *asd, bool sync)
1793 dev_dbg(asd->isp->dev, "WDT stop:\n");
1795 if (!atomisp_hw_is_isp2401) {
1797 del_timer_sync(&asd->wdt);
1798 cancel_work_sync(&asd->isp->wdt_work);
1800 del_timer(&asd->wdt);
1803 atomisp_wdt_stop_pipe(&asd->video_out_capture, sync);
1804 atomisp_wdt_stop_pipe(&asd->video_out_preview, sync);
1805 atomisp_wdt_stop_pipe(&asd->video_out_vf, sync);
1806 atomisp_wdt_stop_pipe(&asd->video_out_video_capture, sync);
1810 void atomisp_setup_flash(struct atomisp_sub_device *asd)
1812 struct atomisp_device *isp = asd->isp;
1813 struct v4l2_control ctrl;
1818 if (asd->params.flash_state != ATOMISP_FLASH_REQUESTED &&
1819 asd->params.flash_state != ATOMISP_FLASH_DONE)
1822 if (asd->params.num_flash_frames) {
1823 /* make sure the timeout is set before setting flash mode */
1824 ctrl.id = V4L2_CID_FLASH_TIMEOUT;
1825 ctrl.value = FLASH_TIMEOUT;
1827 if (v4l2_s_ctrl(NULL, isp->flash->ctrl_handler, &ctrl)) {
1828 dev_err(isp->dev, "flash timeout configure failed\n");
1832 ia_css_stream_request_flash(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream);
1834 asd->params.flash_state = ATOMISP_FLASH_ONGOING;
1836 asd->params.flash_state = ATOMISP_FLASH_IDLE;
1840 irqreturn_t atomisp_isr_thread(int irq, void *isp_ptr)
1842 struct atomisp_device *isp = isp_ptr;
1843 unsigned long flags;
1844 bool frame_done_found[MAX_STREAM_NUM] = {0};
1845 bool css_pipe_done[MAX_STREAM_NUM] = {0};
1847 struct atomisp_sub_device *asd;
1849 dev_dbg(isp->dev, ">%s\n", __func__);
1851 spin_lock_irqsave(&isp->lock, flags);
1853 if (!atomisp_streaming_count(isp) && !atomisp_is_acc_enabled(isp)) {
1854 spin_unlock_irqrestore(&isp->lock, flags);
1858 spin_unlock_irqrestore(&isp->lock, flags);
1861 * The standard CSS2.0 API tells the following calling sequence of
1862 * dequeue ready buffers:
1863 * while (ia_css_dequeue_event(...)) {
1864 * switch (event.type) {
1866 * ia_css_pipe_dequeue_buffer()
1869 * That is, dequeue event and buffer are one after another.
1871 * But the following implementation is to first deuque all the event
1872 * to a FIFO, then process the event in the FIFO.
1873 * This will not have issue in single stream mode, but it do have some
1874 * issue in multiple stream case. The issue is that
1875 * ia_css_pipe_dequeue_buffer() will not return the corrent buffer in
1878 * This is due to ia_css_pipe_dequeue_buffer() does not take the
1879 * ia_css_pipe parameter.
1882 * For CSS2.0: we change the way to not dequeue all the event at one
1883 * time, instead, dequue one and process one, then another
1885 rt_mutex_lock(&isp->mutex);
1886 if (atomisp_css_isr_thread(isp, frame_done_found, css_pipe_done))
1889 for (i = 0; i < isp->num_of_streams; i++) {
1891 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1893 atomisp_setup_flash(asd);
1896 rt_mutex_unlock(&isp->mutex);
1897 for (i = 0; i < isp->num_of_streams; i++) {
1899 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED
1900 && css_pipe_done[asd->index]
1901 && isp->sw_contex.file_input)
1902 v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1903 video, s_stream, 1);
1904 /* FIXME! FIX ACC implementation */
1905 if (asd->acc.pipeline && css_pipe_done[asd->index])
1906 atomisp_css_acc_done(asd);
1908 dev_dbg(isp->dev, "<%s\n", __func__);
1914 * utils for buffer allocation/free
1917 int atomisp_get_frame_pgnr(struct atomisp_device *isp,
1918 const struct ia_css_frame *frame, u32 *p_pgnr)
1921 dev_err(isp->dev, "%s: NULL frame pointer ERROR.\n", __func__);
1925 *p_pgnr = DIV_ROUND_UP(frame->data_bytes, PAGE_SIZE);
1930 * Get internal fmt according to V4L2 fmt
1932 static enum ia_css_frame_format
1933 v4l2_fmt_to_sh_fmt(u32 fmt) {
1936 case V4L2_PIX_FMT_YUV420:
1937 return IA_CSS_FRAME_FORMAT_YUV420;
1938 case V4L2_PIX_FMT_YVU420:
1939 return IA_CSS_FRAME_FORMAT_YV12;
1940 case V4L2_PIX_FMT_YUV422P:
1941 return IA_CSS_FRAME_FORMAT_YUV422;
1942 case V4L2_PIX_FMT_YUV444:
1943 return IA_CSS_FRAME_FORMAT_YUV444;
1944 case V4L2_PIX_FMT_NV12:
1945 return IA_CSS_FRAME_FORMAT_NV12;
1946 case V4L2_PIX_FMT_NV21:
1947 return IA_CSS_FRAME_FORMAT_NV21;
1948 case V4L2_PIX_FMT_NV16:
1949 return IA_CSS_FRAME_FORMAT_NV16;
1950 case V4L2_PIX_FMT_NV61:
1951 return IA_CSS_FRAME_FORMAT_NV61;
1952 case V4L2_PIX_FMT_UYVY:
1953 return IA_CSS_FRAME_FORMAT_UYVY;
1954 case V4L2_PIX_FMT_YUYV:
1955 return IA_CSS_FRAME_FORMAT_YUYV;
1956 case V4L2_PIX_FMT_RGB24:
1957 return IA_CSS_FRAME_FORMAT_PLANAR_RGB888;
1958 case V4L2_PIX_FMT_RGB32:
1959 return IA_CSS_FRAME_FORMAT_RGBA888;
1960 case V4L2_PIX_FMT_RGB565:
1961 return IA_CSS_FRAME_FORMAT_RGB565;
1962 case V4L2_PIX_FMT_JPEG:
1963 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1964 return IA_CSS_FRAME_FORMAT_BINARY_8;
1965 case V4L2_PIX_FMT_SBGGR16:
1966 case V4L2_PIX_FMT_SBGGR10:
1967 case V4L2_PIX_FMT_SGBRG10:
1968 case V4L2_PIX_FMT_SGRBG10:
1969 case V4L2_PIX_FMT_SRGGB10:
1970 case V4L2_PIX_FMT_SBGGR12:
1971 case V4L2_PIX_FMT_SGBRG12:
1972 case V4L2_PIX_FMT_SGRBG12:
1973 case V4L2_PIX_FMT_SRGGB12:
1974 case V4L2_PIX_FMT_SBGGR8:
1975 case V4L2_PIX_FMT_SGBRG8:
1976 case V4L2_PIX_FMT_SGRBG8:
1977 case V4L2_PIX_FMT_SRGGB8:
1978 return IA_CSS_FRAME_FORMAT_RAW;
1985 * raw format match between SH format and V4L2 format
1987 static int raw_output_format_match_input(u32 input, u32 output)
1989 if ((input == ATOMISP_INPUT_FORMAT_RAW_12) &&
1990 ((output == V4L2_PIX_FMT_SRGGB12) ||
1991 (output == V4L2_PIX_FMT_SGRBG12) ||
1992 (output == V4L2_PIX_FMT_SBGGR12) ||
1993 (output == V4L2_PIX_FMT_SGBRG12)))
1996 if ((input == ATOMISP_INPUT_FORMAT_RAW_10) &&
1997 ((output == V4L2_PIX_FMT_SRGGB10) ||
1998 (output == V4L2_PIX_FMT_SGRBG10) ||
1999 (output == V4L2_PIX_FMT_SBGGR10) ||
2000 (output == V4L2_PIX_FMT_SGBRG10)))
2003 if ((input == ATOMISP_INPUT_FORMAT_RAW_8) &&
2004 ((output == V4L2_PIX_FMT_SRGGB8) ||
2005 (output == V4L2_PIX_FMT_SGRBG8) ||
2006 (output == V4L2_PIX_FMT_SBGGR8) ||
2007 (output == V4L2_PIX_FMT_SGBRG8)))
2010 if ((input == ATOMISP_INPUT_FORMAT_RAW_16) && (output == V4L2_PIX_FMT_SBGGR16))
2016 static u32 get_pixel_depth(u32 pixelformat)
2018 switch (pixelformat) {
2019 case V4L2_PIX_FMT_YUV420:
2020 case V4L2_PIX_FMT_NV12:
2021 case V4L2_PIX_FMT_NV21:
2022 case V4L2_PIX_FMT_YVU420:
2024 case V4L2_PIX_FMT_YUV422P:
2025 case V4L2_PIX_FMT_YUYV:
2026 case V4L2_PIX_FMT_UYVY:
2027 case V4L2_PIX_FMT_NV16:
2028 case V4L2_PIX_FMT_NV61:
2029 case V4L2_PIX_FMT_RGB565:
2030 case V4L2_PIX_FMT_SBGGR16:
2031 case V4L2_PIX_FMT_SBGGR12:
2032 case V4L2_PIX_FMT_SGBRG12:
2033 case V4L2_PIX_FMT_SGRBG12:
2034 case V4L2_PIX_FMT_SRGGB12:
2035 case V4L2_PIX_FMT_SBGGR10:
2036 case V4L2_PIX_FMT_SGBRG10:
2037 case V4L2_PIX_FMT_SGRBG10:
2038 case V4L2_PIX_FMT_SRGGB10:
2040 case V4L2_PIX_FMT_RGB24:
2041 case V4L2_PIX_FMT_YUV444:
2043 case V4L2_PIX_FMT_RGB32:
2045 case V4L2_PIX_FMT_JPEG:
2046 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
2047 case V4L2_PIX_FMT_SBGGR8:
2048 case V4L2_PIX_FMT_SGBRG8:
2049 case V4L2_PIX_FMT_SGRBG8:
2050 case V4L2_PIX_FMT_SRGGB8:
2053 return 8 * 2; /* raw type now */
2057 bool atomisp_is_mbuscode_raw(uint32_t code)
2059 return code >= 0x3000 && code < 0x4000;
2063 * ISP features control function
2067 * Set ISP capture mode based on current settings
2069 static void atomisp_update_capture_mode(struct atomisp_sub_device *asd)
2071 if (asd->params.gdc_cac_en)
2072 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_ADVANCED);
2073 else if (asd->params.low_light)
2074 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_LOW_LIGHT);
2075 else if (asd->video_out_capture.sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
2076 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
2078 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
2082 int atomisp_set_sensor_runmode(struct atomisp_sub_device *asd,
2083 struct atomisp_s_runmode *runmode)
2085 struct atomisp_device *isp = asd->isp;
2086 struct v4l2_ctrl *c;
2089 if (!(runmode && (runmode->mode & RUNMODE_MASK)))
2092 mutex_lock(asd->ctrl_handler.lock);
2093 c = v4l2_ctrl_find(isp->inputs[asd->input_curr].camera->ctrl_handler,
2097 ret = v4l2_ctrl_s_ctrl(c, runmode->mode);
2099 mutex_unlock(asd->ctrl_handler.lock);
2104 * Function to enable/disable lens geometry distortion correction (GDC) and
2105 * chromatic aberration correction (CAC)
2107 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag,
2111 *value = asd->params.gdc_cac_en;
2115 asd->params.gdc_cac_en = !!*value;
2116 if (asd->params.gdc_cac_en) {
2117 asd->params.config.morph_table = asd->params.css_param.morph_table;
2119 asd->params.config.morph_table = NULL;
2121 asd->params.css_update_params_needed = true;
2122 atomisp_update_capture_mode(asd);
2127 * Function to enable/disable low light mode including ANR
2129 int atomisp_low_light(struct atomisp_sub_device *asd, int flag,
2133 *value = asd->params.low_light;
2137 asd->params.low_light = (*value != 0);
2138 atomisp_update_capture_mode(asd);
2143 * Function to enable/disable extra noise reduction (XNR) in low light
2146 int atomisp_xnr(struct atomisp_sub_device *asd, int flag,
2150 *xnr_enable = asd->params.xnr_en;
2154 atomisp_css_capture_enable_xnr(asd, !!*xnr_enable);
2160 * Function to configure bayer noise reduction
2162 int atomisp_nr(struct atomisp_sub_device *asd, int flag,
2163 struct atomisp_nr_config *arg)
2166 /* Get nr config from current setup */
2167 if (atomisp_css_get_nr_config(asd, arg))
2170 /* Set nr config to isp parameters */
2171 memcpy(&asd->params.css_param.nr_config, arg,
2172 sizeof(struct ia_css_nr_config));
2173 asd->params.config.nr_config = &asd->params.css_param.nr_config;
2174 asd->params.css_update_params_needed = true;
2180 * Function to configure temporal noise reduction (TNR)
2182 int atomisp_tnr(struct atomisp_sub_device *asd, int flag,
2183 struct atomisp_tnr_config *config)
2185 /* Get tnr config from current setup */
2187 /* Get tnr config from current setup */
2188 if (atomisp_css_get_tnr_config(asd, config))
2191 /* Set tnr config to isp parameters */
2192 memcpy(&asd->params.css_param.tnr_config, config,
2193 sizeof(struct ia_css_tnr_config));
2194 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
2195 asd->params.css_update_params_needed = true;
2202 * Function to configure black level compensation
2204 int atomisp_black_level(struct atomisp_sub_device *asd, int flag,
2205 struct atomisp_ob_config *config)
2208 /* Get ob config from current setup */
2209 if (atomisp_css_get_ob_config(asd, config))
2212 /* Set ob config to isp parameters */
2213 memcpy(&asd->params.css_param.ob_config, config,
2214 sizeof(struct ia_css_ob_config));
2215 asd->params.config.ob_config = &asd->params.css_param.ob_config;
2216 asd->params.css_update_params_needed = true;
2223 * Function to configure edge enhancement
2225 int atomisp_ee(struct atomisp_sub_device *asd, int flag,
2226 struct atomisp_ee_config *config)
2229 /* Get ee config from current setup */
2230 if (atomisp_css_get_ee_config(asd, config))
2233 /* Set ee config to isp parameters */
2234 memcpy(&asd->params.css_param.ee_config, config,
2235 sizeof(asd->params.css_param.ee_config));
2236 asd->params.config.ee_config = &asd->params.css_param.ee_config;
2237 asd->params.css_update_params_needed = true;
2244 * Function to update Gamma table for gamma, brightness and contrast config
2246 int atomisp_gamma(struct atomisp_sub_device *asd, int flag,
2247 struct atomisp_gamma_table *config)
2250 /* Get gamma table from current setup */
2251 if (atomisp_css_get_gamma_table(asd, config))
2254 /* Set gamma table to isp parameters */
2255 memcpy(&asd->params.css_param.gamma_table, config,
2256 sizeof(asd->params.css_param.gamma_table));
2257 asd->params.config.gamma_table = &asd->params.css_param.gamma_table;
2264 * Function to update Ctc table for Chroma Enhancement
2266 int atomisp_ctc(struct atomisp_sub_device *asd, int flag,
2267 struct atomisp_ctc_table *config)
2270 /* Get ctc table from current setup */
2271 if (atomisp_css_get_ctc_table(asd, config))
2274 /* Set ctc table to isp parameters */
2275 memcpy(&asd->params.css_param.ctc_table, config,
2276 sizeof(asd->params.css_param.ctc_table));
2277 atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table);
2284 * Function to update gamma correction parameters
2286 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag,
2287 struct atomisp_gc_config *config)
2290 /* Get gamma correction params from current setup */
2291 if (atomisp_css_get_gc_config(asd, config))
2294 /* Set gamma correction params to isp parameters */
2295 memcpy(&asd->params.css_param.gc_config, config,
2296 sizeof(asd->params.css_param.gc_config));
2297 asd->params.config.gc_config = &asd->params.css_param.gc_config;
2298 asd->params.css_update_params_needed = true;
2305 * Function to update narrow gamma flag
2307 int atomisp_formats(struct atomisp_sub_device *asd, int flag,
2308 struct atomisp_formats_config *config)
2311 /* Get narrow gamma flag from current setup */
2312 if (atomisp_css_get_formats_config(asd, config))
2315 /* Set narrow gamma flag to isp parameters */
2316 memcpy(&asd->params.css_param.formats_config, config,
2317 sizeof(asd->params.css_param.formats_config));
2318 asd->params.config.formats_config = &asd->params.css_param.formats_config;
2324 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd)
2326 atomisp_free_css_parameters(&asd->params.css_param);
2328 if (asd->raw_output_frame) {
2329 ia_css_frame_free(asd->raw_output_frame);
2330 asd->raw_output_frame = NULL;
2334 static void atomisp_update_grid_info(struct atomisp_sub_device *asd,
2335 enum ia_css_pipe_id pipe_id,
2338 struct atomisp_device *isp = asd->isp;
2340 u16 stream_id = atomisp_source_pad_to_stream_id(asd, source_pad);
2342 if (atomisp_css_get_grid_info(asd, pipe_id, source_pad))
2345 /* We must free all buffers because they no longer match
2347 atomisp_css_free_stat_buffers(asd);
2349 err = atomisp_alloc_css_stat_bufs(asd, stream_id);
2351 dev_err(isp->dev, "stat_buf allocate error\n");
2355 if (atomisp_alloc_3a_output_buf(asd)) {
2356 /* Failure for 3A buffers does not influence DIS buffers */
2357 if (asd->params.s3a_output_bytes != 0) {
2358 /* For SOC sensor happens s3a_output_bytes == 0,
2359 * using if condition to exclude false error log */
2360 dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n");
2365 if (atomisp_alloc_dis_coef_buf(asd)) {
2367 "Failed to allocate memory for DIS statistics\n");
2371 if (atomisp_alloc_metadata_output_buf(asd)) {
2372 dev_err(isp->dev, "Failed to allocate memory for metadata\n");
2379 atomisp_css_free_stat_buffers(asd);
2383 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd,
2384 struct atomisp_grid_info *info)
2386 memcpy(info, &asd->params.curr_grid_info.s3a_grid,
2387 sizeof(struct ia_css_3a_grid_info));
2390 int atomisp_compare_grid(struct atomisp_sub_device *asd,
2391 struct atomisp_grid_info *atomgrid)
2393 struct atomisp_grid_info tmp = {0};
2395 atomisp_curr_user_grid_info(asd, &tmp);
2396 return memcmp(atomgrid, &tmp, sizeof(tmp));
2400 * Function to update Gdc table for gdc
2402 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag,
2403 struct atomisp_morph_table *config)
2407 struct atomisp_device *isp = asd->isp;
2410 /* Get gdc table from current setup */
2411 struct ia_css_morph_table tab = {0};
2413 atomisp_css_get_morph_table(asd, &tab);
2415 config->width = tab.width;
2416 config->height = tab.height;
2418 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
2419 ret = copy_to_user(config->coordinates_x[i],
2420 tab.coordinates_x[i], tab.height *
2421 tab.width * sizeof(*tab.coordinates_x[i]));
2424 "Failed to copy to User for x\n");
2427 ret = copy_to_user(config->coordinates_y[i],
2428 tab.coordinates_y[i], tab.height *
2429 tab.width * sizeof(*tab.coordinates_y[i]));
2432 "Failed to copy to User for y\n");
2437 struct ia_css_morph_table *tab =
2438 asd->params.css_param.morph_table;
2440 /* free first if we have one */
2442 atomisp_css_morph_table_free(tab);
2443 asd->params.css_param.morph_table = NULL;
2446 /* allocate new one */
2447 tab = atomisp_css_morph_table_allocate(config->width,
2451 dev_err(isp->dev, "out of memory\n");
2455 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
2456 ret = copy_from_user(tab->coordinates_x[i],
2457 config->coordinates_x[i],
2458 config->height * config->width *
2459 sizeof(*config->coordinates_x[i]));
2462 "Failed to copy from User for x, ret %d\n",
2464 atomisp_css_morph_table_free(tab);
2467 ret = copy_from_user(tab->coordinates_y[i],
2468 config->coordinates_y[i],
2469 config->height * config->width *
2470 sizeof(*config->coordinates_y[i]));
2473 "Failed to copy from User for y, ret is %d\n",
2475 atomisp_css_morph_table_free(tab);
2479 asd->params.css_param.morph_table = tab;
2480 if (asd->params.gdc_cac_en)
2481 asd->params.config.morph_table = tab;
2487 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag,
2488 struct atomisp_macc_config *config)
2490 struct ia_css_macc_table *macc_table;
2492 switch (config->color_effect) {
2493 case V4L2_COLORFX_NONE:
2494 macc_table = &asd->params.css_param.macc_table;
2496 case V4L2_COLORFX_SKY_BLUE:
2497 macc_table = &blue_macc_table;
2499 case V4L2_COLORFX_GRASS_GREEN:
2500 macc_table = &green_macc_table;
2502 case V4L2_COLORFX_SKIN_WHITEN_LOW:
2503 macc_table = &skin_low_macc_table;
2505 case V4L2_COLORFX_SKIN_WHITEN:
2506 macc_table = &skin_medium_macc_table;
2508 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
2509 macc_table = &skin_high_macc_table;
2516 /* Get macc table from current setup */
2517 memcpy(&config->table, macc_table,
2518 sizeof(struct ia_css_macc_table));
2520 memcpy(macc_table, &config->table,
2521 sizeof(struct ia_css_macc_table));
2522 if (config->color_effect == asd->params.color_effect)
2523 asd->params.config.macc_table = macc_table;
2529 int atomisp_set_dis_vector(struct atomisp_sub_device *asd,
2530 struct atomisp_dis_vector *vector)
2532 atomisp_css_video_set_dis_vector(asd, vector);
2534 asd->params.dis_proj_data_valid = false;
2535 asd->params.css_update_params_needed = true;
2540 * Function to set/get image stablization statistics
2542 int atomisp_get_dis_stat(struct atomisp_sub_device *asd,
2543 struct atomisp_dis_statistics *stats)
2545 return atomisp_css_get_dis_stat(asd, stats);
2549 * Function set camrea_prefiles.xml current sensor pixel array size
2551 int atomisp_set_array_res(struct atomisp_sub_device *asd,
2552 struct atomisp_resolution *config)
2554 dev_dbg(asd->isp->dev, ">%s start\n", __func__);
2556 dev_err(asd->isp->dev, "Set sensor array size is not valid\n");
2560 asd->sensor_array_res.width = config->width;
2561 asd->sensor_array_res.height = config->height;
2566 * Function to get DVS2 BQ resolution settings
2568 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd,
2569 struct atomisp_dvs2_bq_resolutions *bq_res)
2571 struct ia_css_pipe_config *pipe_cfg = NULL;
2572 struct ia_css_stream_config *stream_cfg = NULL;
2573 struct ia_css_stream_input_config *input_config = NULL;
2575 struct ia_css_stream *stream =
2576 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
2578 dev_warn(asd->isp->dev, "stream is not created");
2582 pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2583 .pipe_configs[IA_CSS_PIPE_ID_VIDEO];
2584 stream_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2586 input_config = &stream_cfg->input_config;
2591 /* the GDC output resolution */
2592 bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2;
2593 bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2;
2595 bq_res->envelope_bq.width_bq = 0;
2596 bq_res->envelope_bq.height_bq = 0;
2597 /* the GDC input resolution */
2598 if (!asd->continuous_mode->val) {
2599 bq_res->source_bq.width_bq = bq_res->output_bq.width_bq +
2600 pipe_cfg->dvs_envelope.width / 2;
2601 bq_res->source_bq.height_bq = bq_res->output_bq.height_bq +
2602 pipe_cfg->dvs_envelope.height / 2;
2604 * Bad pixels caused by spatial filter processing
2605 * ISP filter resolution should be given by CSS/FW, but for now
2606 * there is not such API to query, and it is fixed value, so
2609 bq_res->ispfilter_bq.width_bq = 12 / 2;
2610 bq_res->ispfilter_bq.height_bq = 12 / 2;
2611 /* spatial filter shift, always 4 pixels */
2612 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2613 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2615 if (asd->params.video_dis_en) {
2616 bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width
2617 / 2 - bq_res->ispfilter_bq.width_bq;
2618 bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height
2619 / 2 - bq_res->ispfilter_bq.height_bq;
2622 unsigned int w_padding;
2623 unsigned int gdc_effective_input = 0;
2626 * gdc_effective_input = effective_input + envelope
2628 * From the comment and formula in BZ1786,
2629 * we see the source_bq should be:
2630 * effective_input / bayer_ds_ratio
2632 bq_res->source_bq.width_bq =
2633 (input_config->effective_res.width *
2634 pipe_cfg->bayer_ds_out_res.width /
2635 input_config->effective_res.width + 1) / 2;
2636 bq_res->source_bq.height_bq =
2637 (input_config->effective_res.height *
2638 pipe_cfg->bayer_ds_out_res.height /
2639 input_config->effective_res.height + 1) / 2;
2641 if (!asd->params.video_dis_en) {
2643 * We adjust the ispfilter_bq to:
2644 * ispfilter_bq = 128/BDS
2645 * we still need firmware team to provide an offical
2648 bq_res->ispfilter_bq.width_bq = 128 *
2649 pipe_cfg->bayer_ds_out_res.width /
2650 input_config->effective_res.width / 2;
2651 bq_res->ispfilter_bq.height_bq = 128 *
2652 pipe_cfg->bayer_ds_out_res.width /
2653 input_config->effective_res.width / 2;
2655 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2656 /* No additional left padding for ISYS2401 */
2657 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2658 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2661 * For the w_padding and gdc_shift_bq cacluation
2662 * Please see the BZ 1786 and 4358 for more info.
2663 * Just test that this formula can work now,
2664 * but we still have no offical formula.
2666 * w_padding = ceiling(gdc_effective_input
2667 * /128, 1) * 128 - effective_width
2668 * gdc_shift_bq = w_padding/BDS/2 + ispfilter_bq/2
2670 gdc_effective_input =
2671 input_config->effective_res.width +
2672 pipe_cfg->dvs_envelope.width;
2673 w_padding = roundup(gdc_effective_input, 128) -
2674 input_config->effective_res.width;
2675 w_padding = w_padding *
2676 pipe_cfg->bayer_ds_out_res.width /
2677 input_config->effective_res.width + 1;
2678 w_padding = roundup(w_padding / 2, 1);
2680 bq_res->gdc_shift_bq.width_bq = bq_res->ispfilter_bq.width_bq / 2
2682 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2685 unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
2687 bq_res->ispfilter_bq.width_bq = 8 / 2;
2688 bq_res->ispfilter_bq.height_bq = 8 / 2;
2690 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2691 /* No additional left padding for ISYS2401 */
2692 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2693 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2696 roundup(input_config->effective_res.width, 128) -
2697 input_config->effective_res.width;
2700 bq_res->gdc_shift_bq.width_bq = 4 / 2 +
2702 pipe_cfg->bayer_ds_out_res.width /
2703 input_config->effective_res.width + 1) / 2;
2704 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2707 dvs_w = pipe_cfg->bayer_ds_out_res.width -
2708 pipe_cfg->output_info[0].res.width;
2709 dvs_h = pipe_cfg->bayer_ds_out_res.height -
2710 pipe_cfg->output_info[0].res.height;
2711 dvs_w_max = rounddown(
2712 pipe_cfg->output_info[0].res.width / 5,
2713 ATOM_ISP_STEP_WIDTH);
2714 dvs_h_max = rounddown(
2715 pipe_cfg->output_info[0].res.height / 5,
2716 ATOM_ISP_STEP_HEIGHT);
2717 bq_res->envelope_bq.width_bq =
2718 min((dvs_w / 2), (dvs_w_max / 2)) -
2719 bq_res->ispfilter_bq.width_bq;
2720 bq_res->envelope_bq.height_bq =
2721 min((dvs_h / 2), (dvs_h_max / 2)) -
2722 bq_res->ispfilter_bq.height_bq;
2726 dev_dbg(asd->isp->dev,
2727 "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",
2728 bq_res->source_bq.width_bq, bq_res->source_bq.height_bq,
2729 bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq,
2730 bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq,
2731 bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq,
2732 bq_res->output_bq.width_bq, bq_res->output_bq.height_bq);
2737 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd,
2738 struct atomisp_dis_coefficients *coefs)
2740 return atomisp_css_set_dis_coefs(asd, coefs);
2744 * Function to set/get 3A stat from isp
2746 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag,
2747 struct atomisp_3a_statistics *config)
2749 struct atomisp_device *isp = asd->isp;
2750 struct atomisp_s3a_buf *s3a_buf;
2756 /* sanity check to avoid writing into unallocated memory. */
2757 if (asd->params.s3a_output_bytes == 0)
2760 if (atomisp_compare_grid(asd, &config->grid_info) != 0) {
2761 /* If the grid info in the argument differs from the current
2762 grid info, we tell the caller to reset the grid size and
2767 if (list_empty(&asd->s3a_stats_ready)) {
2768 dev_err(isp->dev, "3a statistics is not valid.\n");
2772 s3a_buf = list_entry(asd->s3a_stats_ready.next,
2773 struct atomisp_s3a_buf, list);
2774 if (s3a_buf->s3a_map)
2775 ia_css_translate_3a_statistics(
2776 asd->params.s3a_user_stat, s3a_buf->s3a_map);
2778 ia_css_get_3a_statistics(asd->params.s3a_user_stat,
2781 config->exp_id = s3a_buf->s3a_data->exp_id;
2782 config->isp_config_id = s3a_buf->s3a_data->isp_config_id;
2784 ret = copy_to_user(config->data, asd->params.s3a_user_stat->data,
2785 asd->params.s3a_output_bytes);
2787 dev_err(isp->dev, "copy to user failed: copied %lu bytes\n",
2792 /* Move to free buffer list */
2793 list_del_init(&s3a_buf->list);
2794 list_add_tail(&s3a_buf->list, &asd->s3a_stats);
2795 dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n",
2797 config->exp_id, config->isp_config_id);
2801 int atomisp_get_metadata(struct atomisp_sub_device *asd, int flag,
2802 struct atomisp_metadata *md)
2804 struct atomisp_device *isp = asd->isp;
2805 struct ia_css_stream_config *stream_config;
2806 struct ia_css_stream_info *stream_info;
2807 struct camera_mipi_info *mipi_info;
2808 struct atomisp_metadata_buf *md_buf;
2809 enum atomisp_metadata_type md_type = ATOMISP_MAIN_METADATA;
2815 stream_config = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2817 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2820 /* We always return the resolution and stride even if there is
2821 * no valid metadata. This allows the caller to get the information
2822 * needed to allocate user-space buffers. */
2823 md->width = stream_info->metadata_info.resolution.width;
2824 md->height = stream_info->metadata_info.resolution.height;
2825 md->stride = stream_info->metadata_info.stride;
2827 /* sanity check to avoid writing into unallocated memory.
2828 * This does not return an error because it is a valid way
2829 * for applications to detect that metadata is not enabled. */
2830 if (md->width == 0 || md->height == 0 || !md->data)
2833 /* This is done in the atomisp_buf_done() */
2834 if (list_empty(&asd->metadata_ready[md_type])) {
2835 dev_warn(isp->dev, "Metadata queue is empty now!\n");
2839 mipi_info = atomisp_to_sensor_mipi_info(
2840 isp->inputs[asd->input_curr].camera);
2844 if (mipi_info->metadata_effective_width) {
2845 for (i = 0; i < md->height; i++)
2846 md->effective_width[i] =
2847 mipi_info->metadata_effective_width[i];
2850 md_buf = list_entry(asd->metadata_ready[md_type].next,
2851 struct atomisp_metadata_buf, list);
2852 md->exp_id = md_buf->metadata->exp_id;
2853 if (md_buf->md_vptr) {
2854 ret = copy_to_user(md->data,
2856 stream_info->metadata_info.size);
2858 hmm_load(md_buf->metadata->address,
2859 asd->params.metadata_user[md_type],
2860 stream_info->metadata_info.size);
2862 ret = copy_to_user(md->data,
2863 asd->params.metadata_user[md_type],
2864 stream_info->metadata_info.size);
2867 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2872 list_del_init(&md_buf->list);
2873 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2875 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2876 __func__, md_type, md->exp_id);
2880 int atomisp_get_metadata_by_type(struct atomisp_sub_device *asd, int flag,
2881 struct atomisp_metadata_with_type *md)
2883 struct atomisp_device *isp = asd->isp;
2884 struct ia_css_stream_config *stream_config;
2885 struct ia_css_stream_info *stream_info;
2886 struct camera_mipi_info *mipi_info;
2887 struct atomisp_metadata_buf *md_buf;
2888 enum atomisp_metadata_type md_type;
2894 stream_config = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2896 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2899 /* We always return the resolution and stride even if there is
2900 * no valid metadata. This allows the caller to get the information
2901 * needed to allocate user-space buffers. */
2902 md->width = stream_info->metadata_info.resolution.width;
2903 md->height = stream_info->metadata_info.resolution.height;
2904 md->stride = stream_info->metadata_info.stride;
2906 /* sanity check to avoid writing into unallocated memory.
2907 * This does not return an error because it is a valid way
2908 * for applications to detect that metadata is not enabled. */
2909 if (md->width == 0 || md->height == 0 || !md->data)
2913 if (md_type < 0 || md_type >= ATOMISP_METADATA_TYPE_NUM)
2916 /* This is done in the atomisp_buf_done() */
2917 if (list_empty(&asd->metadata_ready[md_type])) {
2918 dev_warn(isp->dev, "Metadata queue is empty now!\n");
2922 mipi_info = atomisp_to_sensor_mipi_info(
2923 isp->inputs[asd->input_curr].camera);
2927 if (mipi_info->metadata_effective_width) {
2928 for (i = 0; i < md->height; i++)
2929 md->effective_width[i] =
2930 mipi_info->metadata_effective_width[i];
2933 md_buf = list_entry(asd->metadata_ready[md_type].next,
2934 struct atomisp_metadata_buf, list);
2935 md->exp_id = md_buf->metadata->exp_id;
2936 if (md_buf->md_vptr) {
2937 ret = copy_to_user(md->data,
2939 stream_info->metadata_info.size);
2941 hmm_load(md_buf->metadata->address,
2942 asd->params.metadata_user[md_type],
2943 stream_info->metadata_info.size);
2945 ret = copy_to_user(md->data,
2946 asd->params.metadata_user[md_type],
2947 stream_info->metadata_info.size);
2950 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2954 list_del_init(&md_buf->list);
2955 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2957 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2958 __func__, md_type, md->exp_id);
2963 * Function to calculate real zoom region for every pipe
2965 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd,
2966 struct ia_css_dz_config *dz_config,
2967 enum ia_css_pipe_id css_pipe_id)
2970 struct atomisp_stream_env *stream_env =
2971 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL];
2972 struct atomisp_resolution eff_res, out_res;
2973 int w_offset, h_offset;
2975 memset(&eff_res, 0, sizeof(eff_res));
2976 memset(&out_res, 0, sizeof(out_res));
2978 if (dz_config->dx || dz_config->dy)
2981 if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW
2982 && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) {
2983 dev_err(asd->isp->dev, "%s the set pipe no support crop region"
2989 stream_env->stream_config.input_config.effective_res.width;
2991 stream_env->stream_config.input_config.effective_res.height;
2992 if (eff_res.width == 0 || eff_res.height == 0) {
2993 dev_err(asd->isp->dev, "%s err effective resolution"
2998 if (dz_config->zoom_region.resolution.width
2999 == asd->sensor_array_res.width
3000 || dz_config->zoom_region.resolution.height
3001 == asd->sensor_array_res.height) {
3002 /*no need crop region*/
3003 dz_config->zoom_region.origin.x = 0;
3004 dz_config->zoom_region.origin.y = 0;
3005 dz_config->zoom_region.resolution.width = eff_res.width;
3006 dz_config->zoom_region.resolution.height = eff_res.height;
3011 * This is not the correct implementation with Google's definition, due
3012 * to firmware limitation.
3013 * map real crop region base on above calculating base max crop region.
3016 if (!atomisp_hw_is_isp2401) {
3017 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
3019 / asd->sensor_array_res.width;
3020 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
3022 / asd->sensor_array_res.height;
3023 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
3025 / asd->sensor_array_res.width;
3026 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
3028 / asd->sensor_array_res.height;
3030 * Set same ratio of crop region resolution and current pipe output
3033 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
3034 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
3035 if (out_res.width == 0 || out_res.height == 0) {
3036 dev_err(asd->isp->dev, "%s err current pipe output resolution"
3041 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
3042 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
3043 if (out_res.width == 0 || out_res.height == 0) {
3044 dev_err(asd->isp->dev, "%s err current pipe output resolution"
3049 if (asd->sensor_array_res.width * out_res.height
3050 < out_res.width * asd->sensor_array_res.height) {
3051 h_offset = asd->sensor_array_res.height
3052 - asd->sensor_array_res.width
3053 * out_res.height / out_res.width;
3054 h_offset = h_offset / 2;
3055 if (dz_config->zoom_region.origin.y < h_offset)
3056 dz_config->zoom_region.origin.y = 0;
3058 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset;
3061 w_offset = asd->sensor_array_res.width
3062 - asd->sensor_array_res.height
3063 * out_res.width / out_res.height;
3064 w_offset = w_offset / 2;
3065 if (dz_config->zoom_region.origin.x < w_offset)
3066 dz_config->zoom_region.origin.x = 0;
3068 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset;
3071 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
3073 / (asd->sensor_array_res.width - 2 * w_offset);
3074 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
3076 / (asd->sensor_array_res.height - 2 * h_offset);
3077 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
3079 / (asd->sensor_array_res.width - 2 * w_offset);
3080 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
3082 / (asd->sensor_array_res.height - 2 * h_offset);
3085 if (out_res.width * dz_config->zoom_region.resolution.height
3086 > dz_config->zoom_region.resolution.width * out_res.height) {
3087 dz_config->zoom_region.resolution.height =
3088 dz_config->zoom_region.resolution.width
3089 * out_res.height / out_res.width;
3091 dz_config->zoom_region.resolution.width =
3092 dz_config->zoom_region.resolution.height
3093 * out_res.width / out_res.height;
3095 dev_dbg(asd->isp->dev,
3096 "%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n",
3097 __func__, dz_config->zoom_region.origin.x,
3098 dz_config->zoom_region.origin.y,
3099 dz_config->zoom_region.resolution.width,
3100 dz_config->zoom_region.resolution.height,
3101 eff_res.width, eff_res.height,
3102 asd->sensor_array_res.width,
3103 asd->sensor_array_res.height,
3104 out_res.width, out_res.height);
3106 if ((dz_config->zoom_region.origin.x +
3107 dz_config->zoom_region.resolution.width
3109 (dz_config->zoom_region.origin.y +
3110 dz_config->zoom_region.resolution.height
3118 * Function to check the zoom region whether is effective
3120 static bool atomisp_check_zoom_region(
3121 struct atomisp_sub_device *asd,
3122 struct ia_css_dz_config *dz_config)
3124 struct atomisp_resolution config;
3128 memset(&config, 0, sizeof(struct atomisp_resolution));
3130 if (dz_config->dx && dz_config->dy)
3133 config.width = asd->sensor_array_res.width;
3134 config.height = asd->sensor_array_res.height;
3135 w = dz_config->zoom_region.origin.x +
3136 dz_config->zoom_region.resolution.width;
3137 h = dz_config->zoom_region.origin.y +
3138 dz_config->zoom_region.resolution.height;
3140 if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0)
3143 /* setting error zoom region */
3144 dev_err(asd->isp->dev,
3145 "%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n",
3146 __func__, dz_config->zoom_region.origin.x,
3147 dz_config->zoom_region.origin.y,
3148 dz_config->zoom_region.resolution.width,
3149 dz_config->zoom_region.resolution.height,
3150 config.width, config.height);
3155 void atomisp_apply_css_parameters(
3156 struct atomisp_sub_device *asd,
3157 struct atomisp_css_params *css_param)
3159 if (css_param->update_flag.wb_config)
3160 asd->params.config.wb_config = &css_param->wb_config;
3162 if (css_param->update_flag.ob_config)
3163 asd->params.config.ob_config = &css_param->ob_config;
3165 if (css_param->update_flag.dp_config)
3166 asd->params.config.dp_config = &css_param->dp_config;
3168 if (css_param->update_flag.nr_config)
3169 asd->params.config.nr_config = &css_param->nr_config;
3171 if (css_param->update_flag.ee_config)
3172 asd->params.config.ee_config = &css_param->ee_config;
3174 if (css_param->update_flag.tnr_config)
3175 asd->params.config.tnr_config = &css_param->tnr_config;
3177 if (css_param->update_flag.a3a_config)
3178 asd->params.config.s3a_config = &css_param->s3a_config;
3180 if (css_param->update_flag.ctc_config)
3181 asd->params.config.ctc_config = &css_param->ctc_config;
3183 if (css_param->update_flag.cnr_config)
3184 asd->params.config.cnr_config = &css_param->cnr_config;
3186 if (css_param->update_flag.ecd_config)
3187 asd->params.config.ecd_config = &css_param->ecd_config;
3189 if (css_param->update_flag.ynr_config)
3190 asd->params.config.ynr_config = &css_param->ynr_config;
3192 if (css_param->update_flag.fc_config)
3193 asd->params.config.fc_config = &css_param->fc_config;
3195 if (css_param->update_flag.macc_config)
3196 asd->params.config.macc_config = &css_param->macc_config;
3198 if (css_param->update_flag.aa_config)
3199 asd->params.config.aa_config = &css_param->aa_config;
3201 if (css_param->update_flag.anr_config)
3202 asd->params.config.anr_config = &css_param->anr_config;
3204 if (css_param->update_flag.xnr_config)
3205 asd->params.config.xnr_config = &css_param->xnr_config;
3207 if (css_param->update_flag.yuv2rgb_cc_config)
3208 asd->params.config.yuv2rgb_cc_config = &css_param->yuv2rgb_cc_config;
3210 if (css_param->update_flag.rgb2yuv_cc_config)
3211 asd->params.config.rgb2yuv_cc_config = &css_param->rgb2yuv_cc_config;
3213 if (css_param->update_flag.macc_table)
3214 asd->params.config.macc_table = &css_param->macc_table;
3216 if (css_param->update_flag.xnr_table)
3217 asd->params.config.xnr_table = &css_param->xnr_table;
3219 if (css_param->update_flag.r_gamma_table)
3220 asd->params.config.r_gamma_table = &css_param->r_gamma_table;
3222 if (css_param->update_flag.g_gamma_table)
3223 asd->params.config.g_gamma_table = &css_param->g_gamma_table;
3225 if (css_param->update_flag.b_gamma_table)
3226 asd->params.config.b_gamma_table = &css_param->b_gamma_table;
3228 if (css_param->update_flag.anr_thres)
3229 atomisp_css_set_anr_thres(asd, &css_param->anr_thres);
3231 if (css_param->update_flag.shading_table)
3232 asd->params.config.shading_table = css_param->shading_table;
3234 if (css_param->update_flag.morph_table && asd->params.gdc_cac_en)
3235 asd->params.config.morph_table = css_param->morph_table;
3237 if (css_param->update_flag.dvs2_coefs) {
3238 struct ia_css_dvs_grid_info *dvs_grid_info =
3239 atomisp_css_get_dvs_grid_info(
3240 &asd->params.curr_grid_info);
3242 if (dvs_grid_info && dvs_grid_info->enable)
3243 atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff);
3246 if (css_param->update_flag.dvs_6axis_config)
3247 atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis);
3249 atomisp_css_set_isp_config_id(asd, css_param->isp_config_id);
3251 * These configurations are on used by ISP1.x, not for ISP2.x,
3252 * so do not handle them. see comments of ia_css_isp_config.
3263 static unsigned int long copy_from_compatible(void *to, const void *from,
3264 unsigned long n, bool from_user)
3267 return copy_from_user(to, (void __user *)from, n);
3269 memcpy(to, from, n);
3273 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd,
3274 struct atomisp_parameters *arg,
3275 struct atomisp_css_params *css_param,
3278 struct atomisp_parameters *cur_config = &css_param->update_flag;
3280 if (!arg || !asd || !css_param)
3283 if (arg->wb_config && (from_user || !cur_config->wb_config)) {
3284 if (copy_from_compatible(&css_param->wb_config, arg->wb_config,
3285 sizeof(struct ia_css_wb_config),
3288 css_param->update_flag.wb_config =
3289 (struct atomisp_wb_config *)&css_param->wb_config;
3292 if (arg->ob_config && (from_user || !cur_config->ob_config)) {
3293 if (copy_from_compatible(&css_param->ob_config, arg->ob_config,
3294 sizeof(struct ia_css_ob_config),
3297 css_param->update_flag.ob_config =
3298 (struct atomisp_ob_config *)&css_param->ob_config;
3301 if (arg->dp_config && (from_user || !cur_config->dp_config)) {
3302 if (copy_from_compatible(&css_param->dp_config, arg->dp_config,
3303 sizeof(struct ia_css_dp_config),
3306 css_param->update_flag.dp_config =
3307 (struct atomisp_dp_config *)&css_param->dp_config;
3310 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
3311 if (arg->dz_config && (from_user || !cur_config->dz_config)) {
3312 if (copy_from_compatible(&css_param->dz_config,
3314 sizeof(struct ia_css_dz_config),
3317 if (!atomisp_check_zoom_region(asd,
3318 &css_param->dz_config)) {
3319 dev_err(asd->isp->dev, "crop region error!");
3322 css_param->update_flag.dz_config =
3323 (struct atomisp_dz_config *)
3324 &css_param->dz_config;
3328 if (arg->nr_config && (from_user || !cur_config->nr_config)) {
3329 if (copy_from_compatible(&css_param->nr_config, arg->nr_config,
3330 sizeof(struct ia_css_nr_config),
3333 css_param->update_flag.nr_config =
3334 (struct atomisp_nr_config *)&css_param->nr_config;
3337 if (arg->ee_config && (from_user || !cur_config->ee_config)) {
3338 if (copy_from_compatible(&css_param->ee_config, arg->ee_config,
3339 sizeof(struct ia_css_ee_config),
3342 css_param->update_flag.ee_config =
3343 (struct atomisp_ee_config *)&css_param->ee_config;
3346 if (arg->tnr_config && (from_user || !cur_config->tnr_config)) {
3347 if (copy_from_compatible(&css_param->tnr_config,
3349 sizeof(struct ia_css_tnr_config),
3352 css_param->update_flag.tnr_config =
3353 (struct atomisp_tnr_config *)
3354 &css_param->tnr_config;
3357 if (arg->a3a_config && (from_user || !cur_config->a3a_config)) {
3358 if (copy_from_compatible(&css_param->s3a_config,
3360 sizeof(struct ia_css_3a_config),
3363 css_param->update_flag.a3a_config =
3364 (struct atomisp_3a_config *)&css_param->s3a_config;
3367 if (arg->ctc_config && (from_user || !cur_config->ctc_config)) {
3368 if (copy_from_compatible(&css_param->ctc_config,
3370 sizeof(struct ia_css_ctc_config),
3373 css_param->update_flag.ctc_config =
3374 (struct atomisp_ctc_config *)
3375 &css_param->ctc_config;
3378 if (arg->cnr_config && (from_user || !cur_config->cnr_config)) {
3379 if (copy_from_compatible(&css_param->cnr_config,
3381 sizeof(struct ia_css_cnr_config),
3384 css_param->update_flag.cnr_config =
3385 (struct atomisp_cnr_config *)
3386 &css_param->cnr_config;
3389 if (arg->ecd_config && (from_user || !cur_config->ecd_config)) {
3390 if (copy_from_compatible(&css_param->ecd_config,
3392 sizeof(struct ia_css_ecd_config),
3395 css_param->update_flag.ecd_config =
3396 (struct atomisp_ecd_config *)
3397 &css_param->ecd_config;
3400 if (arg->ynr_config && (from_user || !cur_config->ynr_config)) {
3401 if (copy_from_compatible(&css_param->ynr_config,
3403 sizeof(struct ia_css_ynr_config),
3406 css_param->update_flag.ynr_config =
3407 (struct atomisp_ynr_config *)
3408 &css_param->ynr_config;
3411 if (arg->fc_config && (from_user || !cur_config->fc_config)) {
3412 if (copy_from_compatible(&css_param->fc_config,
3414 sizeof(struct ia_css_fc_config),
3417 css_param->update_flag.fc_config =
3418 (struct atomisp_fc_config *)&css_param->fc_config;
3421 if (arg->macc_config && (from_user || !cur_config->macc_config)) {
3422 if (copy_from_compatible(&css_param->macc_config,
3424 sizeof(struct ia_css_macc_config),
3427 css_param->update_flag.macc_config =
3428 (struct atomisp_macc_config *)
3429 &css_param->macc_config;
3432 if (arg->aa_config && (from_user || !cur_config->aa_config)) {
3433 if (copy_from_compatible(&css_param->aa_config, arg->aa_config,
3434 sizeof(struct ia_css_aa_config),
3437 css_param->update_flag.aa_config =
3438 (struct atomisp_aa_config *)&css_param->aa_config;
3441 if (arg->anr_config && (from_user || !cur_config->anr_config)) {
3442 if (copy_from_compatible(&css_param->anr_config,
3444 sizeof(struct ia_css_anr_config),
3447 css_param->update_flag.anr_config =
3448 (struct atomisp_anr_config *)
3449 &css_param->anr_config;
3452 if (arg->xnr_config && (from_user || !cur_config->xnr_config)) {
3453 if (copy_from_compatible(&css_param->xnr_config,
3455 sizeof(struct ia_css_xnr_config),
3458 css_param->update_flag.xnr_config =
3459 (struct atomisp_xnr_config *)
3460 &css_param->xnr_config;
3463 if (arg->yuv2rgb_cc_config &&
3464 (from_user || !cur_config->yuv2rgb_cc_config)) {
3465 if (copy_from_compatible(&css_param->yuv2rgb_cc_config,
3466 arg->yuv2rgb_cc_config,
3467 sizeof(struct ia_css_cc_config),
3470 css_param->update_flag.yuv2rgb_cc_config =
3471 (struct atomisp_cc_config *)
3472 &css_param->yuv2rgb_cc_config;
3475 if (arg->rgb2yuv_cc_config &&
3476 (from_user || !cur_config->rgb2yuv_cc_config)) {
3477 if (copy_from_compatible(&css_param->rgb2yuv_cc_config,
3478 arg->rgb2yuv_cc_config,
3479 sizeof(struct ia_css_cc_config),
3482 css_param->update_flag.rgb2yuv_cc_config =
3483 (struct atomisp_cc_config *)
3484 &css_param->rgb2yuv_cc_config;
3487 if (arg->macc_table && (from_user || !cur_config->macc_table)) {
3488 if (copy_from_compatible(&css_param->macc_table,
3490 sizeof(struct ia_css_macc_table),
3493 css_param->update_flag.macc_table =
3494 (struct atomisp_macc_table *)
3495 &css_param->macc_table;
3498 if (arg->xnr_table && (from_user || !cur_config->xnr_table)) {
3499 if (copy_from_compatible(&css_param->xnr_table,
3501 sizeof(struct ia_css_xnr_table),
3504 css_param->update_flag.xnr_table =
3505 (struct atomisp_xnr_table *)&css_param->xnr_table;
3508 if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) {
3509 if (copy_from_compatible(&css_param->r_gamma_table,
3511 sizeof(struct ia_css_rgb_gamma_table),
3514 css_param->update_flag.r_gamma_table =
3515 (struct atomisp_rgb_gamma_table *)
3516 &css_param->r_gamma_table;
3519 if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) {
3520 if (copy_from_compatible(&css_param->g_gamma_table,
3522 sizeof(struct ia_css_rgb_gamma_table),
3525 css_param->update_flag.g_gamma_table =
3526 (struct atomisp_rgb_gamma_table *)
3527 &css_param->g_gamma_table;
3530 if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) {
3531 if (copy_from_compatible(&css_param->b_gamma_table,
3533 sizeof(struct ia_css_rgb_gamma_table),
3536 css_param->update_flag.b_gamma_table =
3537 (struct atomisp_rgb_gamma_table *)
3538 &css_param->b_gamma_table;
3541 if (arg->anr_thres && (from_user || !cur_config->anr_thres)) {
3542 if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres,
3543 sizeof(struct ia_css_anr_thres),
3546 css_param->update_flag.anr_thres =
3547 (struct atomisp_anr_thres *)&css_param->anr_thres;
3551 css_param->isp_config_id = arg->isp_config_id;
3553 * These configurations are on used by ISP1.x, not for ISP2.x,
3554 * so do not handle them. see comments of ia_css_isp_config.
3566 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd,
3567 struct atomisp_shading_table *source_st,
3568 struct atomisp_css_params *css_param,
3572 unsigned int len_table;
3573 struct ia_css_shading_table *shading_table;
3574 struct ia_css_shading_table *old_table;
3575 struct atomisp_shading_table *st, dest_st;
3583 if (!from_user && css_param->update_flag.shading_table)
3586 if (atomisp_hw_is_isp2401) {
3587 if (copy_from_compatible(&dest_st, source_st,
3588 sizeof(struct atomisp_shading_table),
3590 dev_err(asd->isp->dev, "copy shading table failed!");
3598 old_table = css_param->shading_table;
3600 /* user config is to disable the shading table. */
3602 /* Generate a minimum table with enable = 0. */
3603 shading_table = atomisp_css_shading_table_alloc(1, 1);
3606 shading_table->enable = 0;
3610 /* Setting a new table. Validate first - all tables must be set */
3611 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3613 dev_err(asd->isp->dev, "shading table validate failed");
3618 /* Shading table size per color */
3619 if (!atomisp_hw_is_isp2401) {
3620 if (st->width > ISP2400_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
3621 st->height > ISP2400_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
3622 dev_err(asd->isp->dev, "shading table w/h validate failed!");
3626 if (st->width > ISP2401_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
3627 st->height > ISP2401_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
3628 dev_err(asd->isp->dev, "shading table w/h validate failed!");
3633 shading_table = atomisp_css_shading_table_alloc(st->width, st->height);
3637 len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE;
3638 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3639 if (copy_from_compatible(shading_table->data[i],
3640 st->data[i], len_table, from_user)) {
3641 atomisp_css_shading_table_free(shading_table);
3645 shading_table->sensor_width = st->sensor_width;
3646 shading_table->sensor_height = st->sensor_height;
3647 shading_table->fraction_bits = st->fraction_bits;
3648 shading_table->enable = st->enable;
3650 /* No need to update shading table if it is the same */
3652 old_table->sensor_width == shading_table->sensor_width &&
3653 old_table->sensor_height == shading_table->sensor_height &&
3654 old_table->width == shading_table->width &&
3655 old_table->height == shading_table->height &&
3656 old_table->fraction_bits == shading_table->fraction_bits &&
3657 old_table->enable == shading_table->enable) {
3658 bool data_is_same = true;
3660 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3661 if (memcmp(shading_table->data[i], old_table->data[i],
3663 data_is_same = false;
3669 atomisp_css_shading_table_free(shading_table);
3675 /* set LSC to CSS */
3676 css_param->shading_table = shading_table;
3677 css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table;
3678 asd->params.sc_en = shading_table;
3681 atomisp_css_shading_table_free(old_table);
3686 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd,
3687 struct ia_css_dvs2_coefficients *coefs,
3688 struct atomisp_css_params *css_param,
3691 struct ia_css_dvs_grid_info *cur =
3692 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3693 int dvs_hor_coef_bytes, dvs_ver_coef_bytes;
3694 struct ia_css_dvs2_coefficients dvs2_coefs;
3699 if (!from_user && css_param->update_flag.dvs2_coefs)
3702 if (!atomisp_hw_is_isp2401) {
3703 if (sizeof(*cur) != sizeof(coefs->grid) ||
3704 memcmp(&coefs->grid, cur, sizeof(coefs->grid))) {
3705 dev_err(asd->isp->dev, "dvs grid mis-match!\n");
3706 /* If the grid info in the argument differs from the current
3707 grid info, we tell the caller to reset the grid size and
3712 if (!coefs->hor_coefs.odd_real ||
3713 !coefs->hor_coefs.odd_imag ||
3714 !coefs->hor_coefs.even_real ||
3715 !coefs->hor_coefs.even_imag ||
3716 !coefs->ver_coefs.odd_real ||
3717 !coefs->ver_coefs.odd_imag ||
3718 !coefs->ver_coefs.even_real ||
3719 !coefs->ver_coefs.even_imag)
3722 if (!css_param->dvs2_coeff) {
3723 /* DIS coefficients. */
3724 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3725 if (!css_param->dvs2_coeff)
3729 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3730 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3731 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3732 coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3733 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3734 coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3735 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3736 coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3737 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3738 coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3739 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3740 coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3741 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3742 coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3743 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3744 coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3745 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3746 coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3747 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3748 css_param->dvs2_coeff = NULL;
3752 if (copy_from_compatible(&dvs2_coefs, coefs,
3753 sizeof(struct ia_css_dvs2_coefficients),
3755 dev_err(asd->isp->dev, "copy dvs2 coef failed");
3759 if (sizeof(*cur) != sizeof(dvs2_coefs.grid) ||
3760 memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) {
3761 dev_err(asd->isp->dev, "dvs grid mis-match!\n");
3762 /* If the grid info in the argument differs from the current
3763 grid info, we tell the caller to reset the grid size and
3768 if (!dvs2_coefs.hor_coefs.odd_real ||
3769 !dvs2_coefs.hor_coefs.odd_imag ||
3770 !dvs2_coefs.hor_coefs.even_real ||
3771 !dvs2_coefs.hor_coefs.even_imag ||
3772 !dvs2_coefs.ver_coefs.odd_real ||
3773 !dvs2_coefs.ver_coefs.odd_imag ||
3774 !dvs2_coefs.ver_coefs.even_real ||
3775 !dvs2_coefs.ver_coefs.even_imag)
3778 if (!css_param->dvs2_coeff) {
3779 /* DIS coefficients. */
3780 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3781 if (!css_param->dvs2_coeff)
3785 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3786 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3787 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3788 dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3789 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3790 dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3791 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3792 dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3793 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3794 dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3795 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3796 dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3797 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3798 dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3799 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3800 dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3801 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3802 dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3803 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3804 css_param->dvs2_coeff = NULL;
3809 css_param->update_flag.dvs2_coefs =
3810 (struct atomisp_dis_coefficients *)css_param->dvs2_coeff;
3814 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd,
3815 struct atomisp_dvs_6axis_config *source_6axis_config,
3816 struct atomisp_css_params *css_param,
3819 struct ia_css_dvs_6axis_config *dvs_6axis_config;
3820 struct ia_css_dvs_6axis_config *old_6axis_config;
3821 struct ia_css_stream *stream =
3822 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
3823 struct ia_css_dvs_grid_info *dvs_grid_info =
3824 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3828 dev_err(asd->isp->dev, "%s: internal error!", __func__);
3832 if (!source_6axis_config || !dvs_grid_info)
3835 if (!dvs_grid_info->enable)
3838 if (!from_user && css_param->update_flag.dvs_6axis_config)
3841 /* check whether need to reallocate for 6 axis config */
3842 old_6axis_config = css_param->dvs_6axis;
3843 dvs_6axis_config = old_6axis_config;
3845 if (atomisp_hw_is_isp2401) {
3846 struct ia_css_dvs_6axis_config t_6axis_config;
3848 if (copy_from_compatible(&t_6axis_config, source_6axis_config,
3849 sizeof(struct atomisp_dvs_6axis_config),
3851 dev_err(asd->isp->dev, "copy morph table failed!");
3855 if (old_6axis_config &&
3856 (old_6axis_config->width_y != t_6axis_config.width_y ||
3857 old_6axis_config->height_y != t_6axis_config.height_y ||
3858 old_6axis_config->width_uv != t_6axis_config.width_uv ||
3859 old_6axis_config->height_uv != t_6axis_config.height_uv)) {
3860 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3861 css_param->dvs_6axis = NULL;
3863 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3864 if (!dvs_6axis_config)
3866 } else if (!dvs_6axis_config) {
3867 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3868 if (!dvs_6axis_config)
3872 dvs_6axis_config->exp_id = t_6axis_config.exp_id;
3874 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3875 t_6axis_config.xcoords_y,
3876 t_6axis_config.width_y *
3877 t_6axis_config.height_y *
3878 sizeof(*dvs_6axis_config->xcoords_y),
3881 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3882 t_6axis_config.ycoords_y,
3883 t_6axis_config.width_y *
3884 t_6axis_config.height_y *
3885 sizeof(*dvs_6axis_config->ycoords_y),
3888 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3889 t_6axis_config.xcoords_uv,
3890 t_6axis_config.width_uv *
3891 t_6axis_config.height_uv *
3892 sizeof(*dvs_6axis_config->xcoords_uv),
3895 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3896 t_6axis_config.ycoords_uv,
3897 t_6axis_config.width_uv *
3898 t_6axis_config.height_uv *
3899 sizeof(*dvs_6axis_config->ycoords_uv),
3903 if (old_6axis_config &&
3904 (old_6axis_config->width_y != source_6axis_config->width_y ||
3905 old_6axis_config->height_y != source_6axis_config->height_y ||
3906 old_6axis_config->width_uv != source_6axis_config->width_uv ||
3907 old_6axis_config->height_uv != source_6axis_config->height_uv)) {
3908 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3909 css_param->dvs_6axis = NULL;
3911 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3912 if (!dvs_6axis_config)
3914 } else if (!dvs_6axis_config) {
3915 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3916 if (!dvs_6axis_config)
3920 dvs_6axis_config->exp_id = source_6axis_config->exp_id;
3922 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3923 source_6axis_config->xcoords_y,
3924 source_6axis_config->width_y *
3925 source_6axis_config->height_y *
3926 sizeof(*source_6axis_config->xcoords_y),
3929 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3930 source_6axis_config->ycoords_y,
3931 source_6axis_config->width_y *
3932 source_6axis_config->height_y *
3933 sizeof(*source_6axis_config->ycoords_y),
3936 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3937 source_6axis_config->xcoords_uv,
3938 source_6axis_config->width_uv *
3939 source_6axis_config->height_uv *
3940 sizeof(*source_6axis_config->xcoords_uv),
3943 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3944 source_6axis_config->ycoords_uv,
3945 source_6axis_config->width_uv *
3946 source_6axis_config->height_uv *
3947 sizeof(*source_6axis_config->ycoords_uv),
3951 css_param->dvs_6axis = dvs_6axis_config;
3952 css_param->update_flag.dvs_6axis_config =
3953 (struct atomisp_dvs_6axis_config *)dvs_6axis_config;
3957 if (dvs_6axis_config)
3958 ia_css_dvs2_6axis_config_free(dvs_6axis_config);
3962 int atomisp_cp_morph_table(struct atomisp_sub_device *asd,
3963 struct atomisp_morph_table *source_morph_table,
3964 struct atomisp_css_params *css_param,
3969 struct ia_css_morph_table *morph_table;
3970 struct ia_css_morph_table *old_morph_table;
3972 if (!source_morph_table)
3975 if (!from_user && css_param->update_flag.morph_table)
3978 old_morph_table = css_param->morph_table;
3980 if (atomisp_hw_is_isp2401) {
3981 struct ia_css_morph_table mtbl;
3983 if (copy_from_compatible(&mtbl, source_morph_table,
3984 sizeof(struct atomisp_morph_table),
3986 dev_err(asd->isp->dev, "copy morph table failed!");
3990 morph_table = atomisp_css_morph_table_allocate(
3996 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3997 if (copy_from_compatible(morph_table->coordinates_x[i],
3998 (__force void *)source_morph_table->coordinates_x[i],
3999 mtbl.height * mtbl.width *
4000 sizeof(*morph_table->coordinates_x[i]),
4004 if (copy_from_compatible(morph_table->coordinates_y[i],
4005 (__force void *)source_morph_table->coordinates_y[i],
4006 mtbl.height * mtbl.width *
4007 sizeof(*morph_table->coordinates_y[i]),
4012 morph_table = atomisp_css_morph_table_allocate(
4013 source_morph_table->width,
4014 source_morph_table->height);
4018 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
4019 if (copy_from_compatible(morph_table->coordinates_x[i],
4020 (__force void *)source_morph_table->coordinates_x[i],
4021 source_morph_table->height * source_morph_table->width *
4022 sizeof(*source_morph_table->coordinates_x[i]),
4026 if (copy_from_compatible(morph_table->coordinates_y[i],
4027 (__force void *)source_morph_table->coordinates_y[i],
4028 source_morph_table->height * source_morph_table->width *
4029 sizeof(*source_morph_table->coordinates_y[i]),
4035 css_param->morph_table = morph_table;
4036 if (old_morph_table)
4037 atomisp_css_morph_table_free(old_morph_table);
4038 css_param->update_flag.morph_table =
4039 (struct atomisp_morph_table *)morph_table;
4044 atomisp_css_morph_table_free(morph_table);
4048 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd,
4049 struct atomisp_parameters *arg,
4050 struct atomisp_css_params *css_param)
4054 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false);
4057 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false);
4060 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false);
4063 ret = atomisp_css_cp_dvs2_coefs(asd,
4064 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
4068 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
4073 void atomisp_free_css_parameters(struct atomisp_css_params *css_param)
4075 if (css_param->dvs_6axis) {
4076 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
4077 css_param->dvs_6axis = NULL;
4079 if (css_param->dvs2_coeff) {
4080 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
4081 css_param->dvs2_coeff = NULL;
4083 if (css_param->shading_table) {
4084 ia_css_shading_table_free(css_param->shading_table);
4085 css_param->shading_table = NULL;
4087 if (css_param->morph_table) {
4088 ia_css_morph_table_free(css_param->morph_table);
4089 css_param->morph_table = NULL;
4094 * Check parameter queue list and buffer queue list to find out if matched items
4095 * and then set parameter to CSS and enqueue buffer to CSS.
4096 * Of course, if the buffer in buffer waiting list is not bound to a per-frame
4097 * parameter, it will be enqueued into CSS as long as the per-frame setting
4098 * buffers before it get enqueued.
4100 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe)
4102 struct atomisp_sub_device *asd = pipe->asd;
4103 struct videobuf_buffer *vb = NULL, *vb_tmp;
4104 struct atomisp_css_params_with_list *param = NULL, *param_tmp;
4105 struct videobuf_vmalloc_memory *vm_mem = NULL;
4106 unsigned long irqflags;
4107 bool need_to_enqueue_buffer = false;
4109 if (atomisp_is_vf_pipe(pipe))
4113 * CSS/FW requires set parameter and enqueue buffer happen after ISP
4116 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
4119 if (list_empty(&pipe->per_frame_params) ||
4120 list_empty(&pipe->buffers_waiting_for_param))
4123 list_for_each_entry_safe(vb, vb_tmp,
4124 &pipe->buffers_waiting_for_param, queue) {
4125 if (pipe->frame_request_config_id[vb->i]) {
4126 list_for_each_entry_safe(param, param_tmp,
4127 &pipe->per_frame_params, list) {
4128 if (pipe->frame_request_config_id[vb->i] !=
4129 param->params.isp_config_id)
4132 list_del(¶m->list);
4133 list_del(&vb->queue);
4135 * clear the request config id as the buffer
4136 * will be handled and enqueued into CSS soon
4138 pipe->frame_request_config_id[vb->i] = 0;
4139 pipe->frame_params[vb->i] = param;
4146 spin_lock_irqsave(&pipe->irq_lock, irqflags);
4147 list_add_tail(&vb->queue, &pipe->activeq);
4148 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
4150 need_to_enqueue_buffer = true;
4152 /* The is the end, stop further loop */
4156 list_del(&vb->queue);
4157 pipe->frame_params[vb->i] = NULL;
4158 spin_lock_irqsave(&pipe->irq_lock, irqflags);
4159 list_add_tail(&vb->queue, &pipe->activeq);
4160 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
4161 need_to_enqueue_buffer = true;
4165 if (!need_to_enqueue_buffer)
4168 atomisp_qbuffers_to_css(asd);
4170 if (!atomisp_hw_is_isp2401) {
4171 if (!atomisp_is_wdt_running(asd) && atomisp_buffers_queued(asd))
4172 atomisp_wdt_start(asd);
4174 if (atomisp_buffers_queued_pipe(pipe)) {
4175 if (!atomisp_is_wdt_running(pipe))
4176 atomisp_wdt_start_pipe(pipe);
4178 atomisp_wdt_refresh_pipe(pipe,
4179 ATOMISP_WDT_KEEP_CURRENT_DELAY);
4185 * Function to configure ISP parameters
4187 int atomisp_set_parameters(struct video_device *vdev,
4188 struct atomisp_parameters *arg)
4190 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4191 struct atomisp_sub_device *asd = pipe->asd;
4192 struct atomisp_css_params_with_list *param = NULL;
4193 struct atomisp_css_params *css_param = &asd->params.css_param;
4196 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) {
4197 dev_err(asd->isp->dev, "%s: internal error!\n", __func__);
4201 dev_dbg(asd->isp->dev,
4202 "%s: set parameter(per_frame_setting %d) for asd%d with isp_config_id %d of %s\n",
4203 __func__, arg->per_frame_setting, asd->index,
4204 arg->isp_config_id, vdev->name);
4206 if (atomisp_hw_is_isp2401) {
4207 if (atomisp_is_vf_pipe(pipe) && arg->per_frame_setting) {
4208 dev_err(asd->isp->dev, "%s: vf pipe not support per_frame_setting",
4214 if (arg->per_frame_setting && !atomisp_is_vf_pipe(pipe)) {
4216 * Per-frame setting enabled, we allocate a new parameter
4217 * buffer to cache the parameters and only when frame buffers
4218 * are ready, the parameters will be set to CSS.
4219 * per-frame setting only works for the main output frame.
4221 param = kvzalloc(sizeof(*param), GFP_KERNEL);
4223 dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n",
4227 css_param = ¶m->params;
4230 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true);
4232 goto apply_parameter_failed;
4234 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true);
4236 goto apply_parameter_failed;
4238 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true);
4240 goto apply_parameter_failed;
4242 ret = atomisp_css_cp_dvs2_coefs(asd,
4243 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
4246 goto apply_parameter_failed;
4248 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
4251 goto apply_parameter_failed;
4253 if (!(arg->per_frame_setting && !atomisp_is_vf_pipe(pipe))) {
4254 /* indicate to CSS that we have parameters to be updated */
4255 asd->params.css_update_params_needed = true;
4257 list_add_tail(¶m->list, &pipe->per_frame_params);
4258 atomisp_handle_parameter_and_buffer(pipe);
4263 apply_parameter_failed:
4265 atomisp_free_css_parameters(css_param);
4273 * Function to set/get isp parameters to isp
4275 int atomisp_param(struct atomisp_sub_device *asd, int flag,
4276 struct atomisp_parm *config)
4278 struct ia_css_pipe_config *vp_cfg =
4279 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
4280 pipe_configs[IA_CSS_PIPE_ID_VIDEO];
4282 /* Read parameter for 3A binary info */
4284 struct ia_css_dvs_grid_info *dvs_grid_info =
4285 atomisp_css_get_dvs_grid_info(
4286 &asd->params.curr_grid_info);
4288 atomisp_curr_user_grid_info(asd, &config->info);
4290 /* We always return the resolution and stride even if there is
4291 * no valid metadata. This allows the caller to get the
4292 * information needed to allocate user-space buffers. */
4293 config->metadata_config.metadata_height = asd->
4294 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
4295 metadata_info.resolution.height;
4296 config->metadata_config.metadata_stride = asd->
4297 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
4298 metadata_info.stride;
4300 /* update dvs grid info */
4302 memcpy(&config->dvs_grid,
4304 sizeof(struct ia_css_dvs_grid_info));
4306 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
4307 config->dvs_envelop.width = 0;
4308 config->dvs_envelop.height = 0;
4312 /* update dvs envelop info */
4313 if (!asd->continuous_mode->val) {
4314 config->dvs_envelop.width = vp_cfg->dvs_envelope.width;
4315 config->dvs_envelop.height =
4316 vp_cfg->dvs_envelope.height;
4318 unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
4320 dvs_w = vp_cfg->bayer_ds_out_res.width -
4321 vp_cfg->output_info[0].res.width;
4322 dvs_h = vp_cfg->bayer_ds_out_res.height -
4323 vp_cfg->output_info[0].res.height;
4324 dvs_w_max = rounddown(
4325 vp_cfg->output_info[0].res.width / 5,
4326 ATOM_ISP_STEP_WIDTH);
4327 dvs_h_max = rounddown(
4328 vp_cfg->output_info[0].res.height / 5,
4329 ATOM_ISP_STEP_HEIGHT);
4331 config->dvs_envelop.width = min(dvs_w, dvs_w_max);
4332 config->dvs_envelop.height = min(dvs_h, dvs_h_max);
4338 memcpy(&asd->params.css_param.wb_config, &config->wb_config,
4339 sizeof(struct ia_css_wb_config));
4340 memcpy(&asd->params.css_param.ob_config, &config->ob_config,
4341 sizeof(struct ia_css_ob_config));
4342 memcpy(&asd->params.css_param.dp_config, &config->dp_config,
4343 sizeof(struct ia_css_dp_config));
4344 memcpy(&asd->params.css_param.de_config, &config->de_config,
4345 sizeof(struct ia_css_de_config));
4346 memcpy(&asd->params.css_param.dz_config, &config->dz_config,
4347 sizeof(struct ia_css_dz_config));
4348 memcpy(&asd->params.css_param.ce_config, &config->ce_config,
4349 sizeof(struct ia_css_ce_config));
4350 memcpy(&asd->params.css_param.nr_config, &config->nr_config,
4351 sizeof(struct ia_css_nr_config));
4352 memcpy(&asd->params.css_param.ee_config, &config->ee_config,
4353 sizeof(struct ia_css_ee_config));
4354 memcpy(&asd->params.css_param.tnr_config, &config->tnr_config,
4355 sizeof(struct ia_css_tnr_config));
4357 if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) {
4358 asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3];
4359 asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4];
4360 asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5];
4361 asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6];
4362 asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7];
4363 asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8];
4366 if (asd->params.color_effect != V4L2_COLORFX_SEPIA &&
4367 asd->params.color_effect != V4L2_COLORFX_BW) {
4368 memcpy(&asd->params.css_param.cc_config, &config->cc_config,
4369 sizeof(struct ia_css_cc_config));
4370 asd->params.config.cc_config = &asd->params.css_param.cc_config;
4373 asd->params.config.wb_config = &asd->params.css_param.wb_config;
4374 asd->params.config.ob_config = &asd->params.css_param.ob_config;
4375 asd->params.config.de_config = &asd->params.css_param.de_config;
4376 asd->params.config.dz_config = &asd->params.css_param.dz_config;
4377 asd->params.config.ce_config = &asd->params.css_param.ce_config;
4378 asd->params.config.dp_config = &asd->params.css_param.dp_config;
4379 asd->params.config.nr_config = &asd->params.css_param.nr_config;
4380 asd->params.config.ee_config = &asd->params.css_param.ee_config;
4381 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
4382 asd->params.css_update_params_needed = true;
4388 * Function to configure color effect of the image
4390 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag,
4393 struct ia_css_cc_config *cc_config = NULL;
4394 struct ia_css_macc_table *macc_table = NULL;
4395 struct ia_css_ctc_table *ctc_table = NULL;
4397 struct v4l2_control control;
4398 struct atomisp_device *isp = asd->isp;
4401 *effect = asd->params.color_effect;
4405 control.id = V4L2_CID_COLORFX;
4406 control.value = *effect;
4408 v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler,
4411 * if set color effect to sensor successfully, return
4415 asd->params.color_effect = (u32)*effect;
4419 if (*effect == asd->params.color_effect)
4423 * isp_subdev->params.macc_en should be set to false.
4425 asd->params.macc_en = false;
4428 case V4L2_COLORFX_NONE:
4429 macc_table = &asd->params.css_param.macc_table;
4430 asd->params.macc_en = true;
4432 case V4L2_COLORFX_SEPIA:
4433 cc_config = &sepia_cc_config;
4435 case V4L2_COLORFX_NEGATIVE:
4436 cc_config = &nega_cc_config;
4438 case V4L2_COLORFX_BW:
4439 cc_config = &mono_cc_config;
4441 case V4L2_COLORFX_SKY_BLUE:
4442 macc_table = &blue_macc_table;
4443 asd->params.macc_en = true;
4445 case V4L2_COLORFX_GRASS_GREEN:
4446 macc_table = &green_macc_table;
4447 asd->params.macc_en = true;
4449 case V4L2_COLORFX_SKIN_WHITEN_LOW:
4450 macc_table = &skin_low_macc_table;
4451 asd->params.macc_en = true;
4453 case V4L2_COLORFX_SKIN_WHITEN:
4454 macc_table = &skin_medium_macc_table;
4455 asd->params.macc_en = true;
4457 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
4458 macc_table = &skin_high_macc_table;
4459 asd->params.macc_en = true;
4461 case V4L2_COLORFX_VIVID:
4462 ctc_table = &vivid_ctc_table;
4467 atomisp_update_capture_mode(asd);
4470 asd->params.config.cc_config = cc_config;
4472 asd->params.config.macc_table = macc_table;
4474 atomisp_css_set_ctc_table(asd, ctc_table);
4475 asd->params.color_effect = (u32)*effect;
4476 asd->params.css_update_params_needed = true;
4481 * Function to configure bad pixel correction
4483 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag,
4487 *value = asd->params.bad_pixel_en;
4490 asd->params.bad_pixel_en = !!*value;
4496 * Function to configure bad pixel correction params
4498 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag,
4499 struct atomisp_dp_config *config)
4502 /* Get bad pixel from current setup */
4503 if (atomisp_css_get_dp_config(asd, config))
4506 /* Set bad pixel to isp parameters */
4507 memcpy(&asd->params.css_param.dp_config, config,
4508 sizeof(asd->params.css_param.dp_config));
4509 asd->params.config.dp_config = &asd->params.css_param.dp_config;
4510 asd->params.css_update_params_needed = true;
4517 * Function to enable/disable video image stablization
4519 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag,
4523 *value = asd->params.video_dis_en;
4525 asd->params.video_dis_en = !!*value;
4531 * Function to configure fixed pattern noise
4533 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag,
4537 *value = asd->params.fpn_en;
4542 asd->params.fpn_en = false;
4546 /* Add function to get black from from sensor with shutter off */
4551 atomisp_bytesperline_to_padded_width(unsigned int bytesperline,
4552 enum ia_css_frame_format format)
4555 case IA_CSS_FRAME_FORMAT_UYVY:
4556 case IA_CSS_FRAME_FORMAT_YUYV:
4557 case IA_CSS_FRAME_FORMAT_RAW:
4558 case IA_CSS_FRAME_FORMAT_RGB565:
4559 return bytesperline / 2;
4560 case IA_CSS_FRAME_FORMAT_RGBA888:
4561 return bytesperline / 4;
4562 /* The following cases could be removed, but we leave them
4563 in to document the formats that are included. */
4564 case IA_CSS_FRAME_FORMAT_NV11:
4565 case IA_CSS_FRAME_FORMAT_NV12:
4566 case IA_CSS_FRAME_FORMAT_NV16:
4567 case IA_CSS_FRAME_FORMAT_NV21:
4568 case IA_CSS_FRAME_FORMAT_NV61:
4569 case IA_CSS_FRAME_FORMAT_YV12:
4570 case IA_CSS_FRAME_FORMAT_YV16:
4571 case IA_CSS_FRAME_FORMAT_YUV420:
4572 case IA_CSS_FRAME_FORMAT_YUV420_16:
4573 case IA_CSS_FRAME_FORMAT_YUV422:
4574 case IA_CSS_FRAME_FORMAT_YUV422_16:
4575 case IA_CSS_FRAME_FORMAT_YUV444:
4576 case IA_CSS_FRAME_FORMAT_YUV_LINE:
4577 case IA_CSS_FRAME_FORMAT_PLANAR_RGB888:
4578 case IA_CSS_FRAME_FORMAT_QPLANE6:
4579 case IA_CSS_FRAME_FORMAT_BINARY_8:
4581 return bytesperline;
4586 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg,
4587 struct ia_css_frame **result)
4589 struct ia_css_frame *res = NULL;
4590 unsigned int padded_width;
4591 enum ia_css_frame_format sh_format;
4592 char *tmp_buf = NULL;
4595 sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat);
4596 padded_width = atomisp_bytesperline_to_padded_width(
4597 arg->fmt.bytesperline, sh_format);
4599 /* Note: the padded width on an ia_css_frame is in elements, not in
4600 bytes. The RAW frame we use here should always be a 16bit RAW
4601 frame. This is why we bytesperline/2 is equal to the padded with */
4602 if (ia_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height,
4603 sh_format, padded_width, 0)) {
4608 tmp_buf = vmalloc(arg->fmt.sizeimage);
4613 if (copy_from_user(tmp_buf, (void __user __force *)arg->base,
4614 arg->fmt.sizeimage)) {
4619 if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) {
4626 ia_css_frame_free(res);
4635 * Function to configure fixed pattern noise table
4637 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd,
4638 struct v4l2_framebuffer *arg)
4640 struct ia_css_frame *raw_black_frame = NULL;
4646 ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame);
4650 if (sh_css_set_black_frame(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream,
4651 raw_black_frame) != 0)
4654 ia_css_frame_free(raw_black_frame);
4659 * Function to configure false color correction
4661 int atomisp_false_color(struct atomisp_sub_device *asd, int flag,
4664 /* Get nr config from current setup */
4666 *value = asd->params.false_color;
4670 /* Set nr config to isp parameters */
4672 asd->params.config.de_config = NULL;
4674 asd->params.css_param.de_config.pixelnoise = 0;
4675 asd->params.config.de_config = &asd->params.css_param.de_config;
4677 asd->params.css_update_params_needed = true;
4678 asd->params.false_color = *value;
4683 * Function to configure bad pixel correction params
4685 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag,
4686 struct atomisp_de_config *config)
4689 /* Get false color from current setup */
4690 if (atomisp_css_get_de_config(asd, config))
4693 /* Set false color to isp parameters */
4694 memcpy(&asd->params.css_param.de_config, config,
4695 sizeof(asd->params.css_param.de_config));
4696 asd->params.config.de_config = &asd->params.css_param.de_config;
4697 asd->params.css_update_params_needed = true;
4704 * Function to configure white balance params
4706 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag,
4707 struct atomisp_wb_config *config)
4710 /* Get white balance from current setup */
4711 if (atomisp_css_get_wb_config(asd, config))
4714 /* Set white balance to isp parameters */
4715 memcpy(&asd->params.css_param.wb_config, config,
4716 sizeof(asd->params.css_param.wb_config));
4717 asd->params.config.wb_config = &asd->params.css_param.wb_config;
4718 asd->params.css_update_params_needed = true;
4724 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag,
4725 struct atomisp_3a_config *config)
4727 struct atomisp_device *isp = asd->isp;
4729 dev_dbg(isp->dev, ">%s %d\n", __func__, flag);
4732 /* Get white balance from current setup */
4733 if (atomisp_css_get_3a_config(asd, config))
4736 /* Set white balance to isp parameters */
4737 memcpy(&asd->params.css_param.s3a_config, config,
4738 sizeof(asd->params.css_param.s3a_config));
4739 asd->params.config.s3a_config = &asd->params.css_param.s3a_config;
4740 asd->params.css_update_params_needed = true;
4743 dev_dbg(isp->dev, "<%s %d\n", __func__, flag);
4748 * Function to setup digital zoom
4750 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag,
4754 struct atomisp_device *isp = asd->isp;
4756 unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR;
4759 atomisp_css_get_zoom_factor(asd, &zoom);
4760 *value = max_zoom - zoom;
4765 zoom = max_zoom - min_t(u32, max_zoom - 1, *value);
4766 atomisp_css_set_zoom_factor(asd, zoom);
4768 dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom);
4769 asd->params.css_update_params_needed = true;
4776 * Function to get sensor specific info for current resolution,
4777 * which will be used for auto exposure conversion.
4779 int atomisp_get_sensor_mode_data(struct atomisp_sub_device *asd,
4780 struct atomisp_sensor_mode_data *config)
4782 struct camera_mipi_info *mipi_info;
4783 struct atomisp_device *isp = asd->isp;
4785 mipi_info = atomisp_to_sensor_mipi_info(
4786 isp->inputs[asd->input_curr].camera);
4790 memcpy(config, &mipi_info->data, sizeof(*config));
4794 int atomisp_get_fmt(struct video_device *vdev, struct v4l2_format *f)
4796 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4798 f->fmt.pix = pipe->pix;
4803 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd,
4804 u16 stream_index, struct atomisp_input_stream_info *stream_info)
4808 /* assign virtual channel id return from sensor driver query */
4809 asd->stream_env[stream_index].ch_id = stream_info->ch_id;
4810 asd->stream_env[stream_index].isys_configs = stream_info->isys_configs;
4811 for (i = 0; i < stream_info->isys_configs; i++) {
4812 asd->stream_env[stream_index].isys_info[i].input_format =
4813 stream_info->isys_info[i].input_format;
4814 asd->stream_env[stream_index].isys_info[i].width =
4815 stream_info->isys_info[i].width;
4816 asd->stream_env[stream_index].isys_info[i].height =
4817 stream_info->isys_info[i].height;
4821 static void __atomisp_init_stream_info(u16 stream_index,
4822 struct atomisp_input_stream_info *stream_info)
4826 stream_info->enable = 1;
4827 stream_info->stream = stream_index;
4828 stream_info->ch_id = 0;
4829 stream_info->isys_configs = 0;
4830 for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) {
4831 stream_info->isys_info[i].input_format = 0;
4832 stream_info->isys_info[i].width = 0;
4833 stream_info->isys_info[i].height = 0;
4837 /* This function looks up the closest available resolution. */
4838 int atomisp_try_fmt(struct video_device *vdev, struct v4l2_format *f,
4841 struct atomisp_device *isp = video_get_drvdata(vdev);
4842 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4843 struct v4l2_subdev_pad_config pad_cfg;
4844 struct v4l2_subdev_format format = {
4845 .which = V4L2_SUBDEV_FORMAT_TRY,
4848 struct v4l2_mbus_framefmt *snr_mbus_fmt = &format.format;
4849 const struct atomisp_format_bridge *fmt;
4850 struct atomisp_input_stream_info *stream_info =
4851 (struct atomisp_input_stream_info *)snr_mbus_fmt->reserved;
4853 int source_pad = atomisp_subdev_source_pad(vdev);
4856 if (!isp->inputs[asd->input_curr].camera)
4859 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
4860 fmt = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
4862 dev_err(isp->dev, "unsupported pixelformat!\n");
4863 fmt = atomisp_output_fmts;
4866 if (f->fmt.pix.width <= 0 || f->fmt.pix.height <= 0)
4869 snr_mbus_fmt->code = fmt->mbus_code;
4870 snr_mbus_fmt->width = f->fmt.pix.width;
4871 snr_mbus_fmt->height = f->fmt.pix.height;
4873 __atomisp_init_stream_info(stream_index, stream_info);
4875 dev_dbg(isp->dev, "try_mbus_fmt: asking for %ux%u\n",
4876 snr_mbus_fmt->width, snr_mbus_fmt->height);
4878 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
4879 pad, set_fmt, &pad_cfg, &format);
4883 dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n",
4884 snr_mbus_fmt->width, snr_mbus_fmt->height);
4886 fmt = atomisp_get_format_bridge_from_mbus(snr_mbus_fmt->code);
4888 dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
4889 snr_mbus_fmt->code);
4893 f->fmt.pix.pixelformat = fmt->pixelformat;
4896 * If the format is jpeg or custom RAW, then the width and height will
4897 * not satisfy the normal atomisp requirements and no need to check
4898 * the below conditions. So just assign to what is being returned from
4899 * the sensor driver.
4901 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG ||
4902 f->fmt.pix.pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
4903 f->fmt.pix.width = snr_mbus_fmt->width;
4904 f->fmt.pix.height = snr_mbus_fmt->height;
4908 if (snr_mbus_fmt->width < f->fmt.pix.width
4909 && snr_mbus_fmt->height < f->fmt.pix.height) {
4910 f->fmt.pix.width = snr_mbus_fmt->width;
4911 f->fmt.pix.height = snr_mbus_fmt->height;
4912 /* Set the flag when resolution requested is
4913 * beyond the max value supported by sensor
4916 *res_overflow = true;
4920 f->fmt.pix.width = rounddown(
4921 clamp_t(u32, f->fmt.pix.width, ATOM_ISP_MIN_WIDTH,
4922 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
4923 f->fmt.pix.height = rounddown(
4924 clamp_t(u32, f->fmt.pix.height, ATOM_ISP_MIN_HEIGHT,
4925 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
4931 atomisp_try_fmt_file(struct atomisp_device *isp, struct v4l2_format *f)
4933 u32 width = f->fmt.pix.width;
4934 u32 height = f->fmt.pix.height;
4935 u32 pixelformat = f->fmt.pix.pixelformat;
4936 enum v4l2_field field = f->fmt.pix.field;
4939 if (!atomisp_get_format_bridge(pixelformat)) {
4940 dev_err(isp->dev, "Wrong output pixelformat\n");
4944 depth = get_pixel_depth(pixelformat);
4946 if (field == V4L2_FIELD_ANY)
4947 field = V4L2_FIELD_NONE;
4948 else if (field != V4L2_FIELD_NONE) {
4949 dev_err(isp->dev, "Wrong output field\n");
4953 f->fmt.pix.field = field;
4954 f->fmt.pix.width = clamp_t(u32,
4955 rounddown(width, (u32)ATOM_ISP_STEP_WIDTH),
4956 ATOM_ISP_MIN_WIDTH, ATOM_ISP_MAX_WIDTH);
4957 f->fmt.pix.height = clamp_t(u32, rounddown(height,
4958 (u32)ATOM_ISP_STEP_HEIGHT),
4959 ATOM_ISP_MIN_HEIGHT, ATOM_ISP_MAX_HEIGHT);
4960 f->fmt.pix.bytesperline = (width * depth) >> 3;
4965 enum mipi_port_id __get_mipi_port(struct atomisp_device *isp,
4966 enum atomisp_camera_port port)
4969 case ATOMISP_CAMERA_PORT_PRIMARY:
4970 return MIPI_PORT0_ID;
4971 case ATOMISP_CAMERA_PORT_SECONDARY:
4972 return MIPI_PORT1_ID;
4973 case ATOMISP_CAMERA_PORT_TERTIARY:
4974 if (MIPI_PORT1_ID + 1 != N_MIPI_PORT_ID)
4975 return MIPI_PORT1_ID + 1;
4976 /* go through down for else case */
4978 dev_err(isp->dev, "unsupported port: %d\n", port);
4979 return MIPI_PORT0_ID;
4983 static inline int atomisp_set_sensor_mipi_to_isp(
4984 struct atomisp_sub_device *asd,
4985 enum atomisp_input_stream_id stream_id,
4986 struct camera_mipi_info *mipi_info)
4988 struct v4l2_control ctrl;
4989 struct atomisp_device *isp = asd->isp;
4990 const struct atomisp_in_fmt_conv *fc;
4992 unsigned int input_format, bayer_order;
4994 ctrl.id = V4L2_CID_LINK_FREQ;
4996 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl) == 0)
4997 mipi_freq = ctrl.value;
4999 if (asd->stream_env[stream_id].isys_configs == 1) {
5001 asd->stream_env[stream_id].isys_info[0].input_format;
5002 atomisp_css_isys_set_format(asd, stream_id,
5003 input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX);
5004 } else if (asd->stream_env[stream_id].isys_configs == 2) {
5005 atomisp_css_isys_two_stream_cfg_update_stream1(
5007 asd->stream_env[stream_id].isys_info[0].input_format,
5008 asd->stream_env[stream_id].isys_info[0].width,
5009 asd->stream_env[stream_id].isys_info[0].height);
5011 atomisp_css_isys_two_stream_cfg_update_stream2(
5013 asd->stream_env[stream_id].isys_info[1].input_format,
5014 asd->stream_env[stream_id].isys_info[1].width,
5015 asd->stream_env[stream_id].isys_info[1].height);
5018 /* Compatibility for sensors which provide no media bus code
5019 * in s_mbus_framefmt() nor support pad formats. */
5020 if (mipi_info->input_format != -1) {
5021 bayer_order = mipi_info->raw_bayer_order;
5023 /* Input stream config is still needs configured */
5024 /* TODO: Check if this is necessary */
5025 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5026 mipi_info->input_format);
5029 input_format = fc->atomisp_in_fmt;
5031 struct v4l2_mbus_framefmt *sink;
5033 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5034 V4L2_SUBDEV_FORMAT_ACTIVE,
5035 ATOMISP_SUBDEV_PAD_SINK);
5036 fc = atomisp_find_in_fmt_conv(sink->code);
5039 input_format = fc->atomisp_in_fmt;
5040 bayer_order = fc->bayer_order;
5043 atomisp_css_input_set_format(asd, stream_id, input_format);
5044 atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order);
5046 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5047 mipi_info->metadata_format);
5050 input_format = fc->atomisp_in_fmt;
5051 atomisp_css_input_configure_port(asd,
5052 __get_mipi_port(asd->isp, mipi_info->port),
5053 mipi_info->num_lanes,
5056 mipi_info->metadata_width,
5057 mipi_info->metadata_height);
5061 static int __enable_continuous_mode(struct atomisp_sub_device *asd,
5064 struct atomisp_device *isp = asd->isp;
5067 "continuous mode %d, raw buffers %d, stop preview %d\n",
5068 enable, asd->continuous_raw_buffer_size->val,
5069 !asd->continuous_viewfinder->val);
5071 if (!atomisp_hw_is_isp2401)
5072 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
5074 atomisp_update_capture_mode(asd);
5076 /* in case of ANR, force capture pipe to offline mode */
5077 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
5078 asd->params.low_light ? false : !enable);
5079 atomisp_css_preview_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
5081 atomisp_css_enable_continuous(asd, enable);
5082 atomisp_css_enable_cvf(asd, asd->continuous_viewfinder->val);
5084 if (atomisp_css_continuous_set_num_raw_frames(asd,
5085 asd->continuous_raw_buffer_size->val)) {
5086 dev_err(isp->dev, "css_continuous_set_num_raw_frames failed\n");
5091 atomisp_css_enable_raw_binning(asd, false);
5092 atomisp_css_input_set_two_pixels_per_clock(asd, false);
5095 if (isp->inputs[asd->input_curr].type != FILE_INPUT)
5096 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
5098 return atomisp_update_run_mode(asd);
5101 static int configure_pp_input_nop(struct atomisp_sub_device *asd,
5102 unsigned int width, unsigned int height)
5107 static int configure_output_nop(struct atomisp_sub_device *asd,
5108 unsigned int width, unsigned int height,
5109 unsigned int min_width,
5110 enum ia_css_frame_format sh_fmt)
5115 static int get_frame_info_nop(struct atomisp_sub_device *asd,
5116 struct ia_css_frame_info *finfo)
5122 * Resets CSS parameters that depend on input resolution.
5124 * Update params like CSS RAW binning, 2ppc mode and pp_input
5125 * which depend on input size, but are not automatically
5126 * handled in CSS when the input resolution is changed.
5128 static int css_input_resolution_changed(struct atomisp_sub_device *asd,
5129 struct v4l2_mbus_framefmt *ffmt)
5131 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
5134 dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n",
5135 ffmt->width, ffmt->height);
5137 #if defined(ISP2401_NEW_INPUT_SYSTEM)
5138 atomisp_css_input_set_two_pixels_per_clock(asd, false);
5140 atomisp_css_input_set_two_pixels_per_clock(asd, true);
5142 if (asd->continuous_mode->val) {
5143 /* Note for all checks: ffmt includes pad_w+pad_h */
5144 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
5145 (ffmt->width >= 2048 || ffmt->height >= 1536)) {
5147 * For preview pipe, enable only if resolution
5148 * is >= 3M for ISP2400.
5150 atomisp_css_enable_raw_binning(asd, true);
5154 * If sensor input changed, which means metadata resolution changed
5155 * together. Release all metadata buffers here to let it re-allocated
5156 * next time in reqbufs.
5158 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
5159 list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
5161 atomisp_css_free_metadata_buffer(md_buf);
5162 list_del(&md_buf->list);
5169 * TODO: atomisp_css_preview_configure_pp_input() not
5170 * reset due to CSS bug tracked as PSI BZ 115124
5174 static int atomisp_set_fmt_to_isp(struct video_device *vdev,
5175 struct ia_css_frame_info *output_info,
5176 struct ia_css_frame_info *raw_output_info,
5177 struct v4l2_pix_format *pix,
5178 unsigned int source_pad)
5180 struct camera_mipi_info *mipi_info;
5181 struct atomisp_device *isp = video_get_drvdata(vdev);
5182 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
5183 const struct atomisp_format_bridge *format;
5184 struct v4l2_rect *isp_sink_crop;
5185 enum ia_css_pipe_id pipe_id;
5186 struct v4l2_subdev_fh fh;
5187 int (*configure_output)(struct atomisp_sub_device *asd,
5188 unsigned int width, unsigned int height,
5189 unsigned int min_width,
5190 enum ia_css_frame_format sh_fmt) =
5191 configure_output_nop;
5192 int (*get_frame_info)(struct atomisp_sub_device *asd,
5193 struct ia_css_frame_info *finfo) =
5195 int (*configure_pp_input)(struct atomisp_sub_device *asd,
5196 unsigned int width, unsigned int height) =
5197 configure_pp_input_nop;
5198 u16 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
5199 const struct atomisp_in_fmt_conv *fc;
5202 v4l2_fh_init(&fh.vfh, vdev);
5204 isp_sink_crop = atomisp_subdev_get_rect(
5205 &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
5206 ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP);
5208 format = atomisp_get_format_bridge(pix->pixelformat);
5212 if (isp->inputs[asd->input_curr].type != TEST_PATTERN &&
5213 isp->inputs[asd->input_curr].type != FILE_INPUT) {
5214 mipi_info = atomisp_to_sensor_mipi_info(
5215 isp->inputs[asd->input_curr].camera);
5217 dev_err(isp->dev, "mipi_info is NULL\n");
5220 if (atomisp_set_sensor_mipi_to_isp(asd, stream_index,
5223 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5224 mipi_info->input_format);
5226 fc = atomisp_find_in_fmt_conv(
5227 atomisp_subdev_get_ffmt(&asd->subdev,
5228 NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
5229 ATOMISP_SUBDEV_PAD_SINK)->code);
5232 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW &&
5233 raw_output_format_match_input(fc->atomisp_in_fmt,
5239 * Configure viewfinder also when vfpp is disabled: the
5240 * CSS still requires viewfinder configuration.
5242 if (asd->fmt_auto->val ||
5243 asd->vfpp->val != ATOMISP_VFPP_ENABLE) {
5244 struct v4l2_rect vf_size = {0};
5245 struct v4l2_mbus_framefmt vf_ffmt = {0};
5247 if (pix->width < 640 || pix->height < 480) {
5248 vf_size.width = pix->width;
5249 vf_size.height = pix->height;
5251 vf_size.width = 640;
5252 vf_size.height = 480;
5255 /* FIXME: proper format name for this one. See
5256 atomisp_output_fmts[] in atomisp_v4l2.c */
5257 vf_ffmt.code = V4L2_MBUS_FMT_CUSTOM_YUV420;
5259 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5260 V4L2_SUBDEV_FORMAT_ACTIVE,
5261 ATOMISP_SUBDEV_PAD_SOURCE_VF,
5262 V4L2_SEL_TGT_COMPOSE, 0, &vf_size);
5263 atomisp_subdev_set_ffmt(&asd->subdev, fh.pad,
5264 V4L2_SUBDEV_FORMAT_ACTIVE,
5265 ATOMISP_SUBDEV_PAD_SOURCE_VF, &vf_ffmt);
5266 asd->video_out_vf.sh_fmt = IA_CSS_FRAME_FORMAT_NV12;
5268 if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
5269 atomisp_css_video_configure_viewfinder(asd,
5270 vf_size.width, vf_size.height, 0,
5271 asd->video_out_vf.sh_fmt);
5272 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5273 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5274 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO)
5275 atomisp_css_video_configure_viewfinder(asd,
5276 vf_size.width, vf_size.height, 0,
5277 asd->video_out_vf.sh_fmt);
5279 atomisp_css_capture_configure_viewfinder(asd,
5280 vf_size.width, vf_size.height, 0,
5281 asd->video_out_vf.sh_fmt);
5282 } else if (source_pad != ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5283 asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
5284 atomisp_css_capture_configure_viewfinder(asd,
5285 vf_size.width, vf_size.height, 0,
5286 asd->video_out_vf.sh_fmt);
5290 if (asd->continuous_mode->val) {
5291 ret = __enable_continuous_mode(asd, true);
5296 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
5298 for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++)
5299 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipe_extra_configs[i].disable_vf_pp = asd->vfpp->val != ATOMISP_VFPP_ENABLE;
5301 /* ISP2401 new input system need to use copy pipe */
5302 if (asd->copy_mode) {
5303 pipe_id = IA_CSS_PIPE_ID_COPY;
5304 atomisp_css_capture_enable_online(asd, stream_index, false);
5305 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
5306 /* video same in continuouscapture and online modes */
5307 configure_output = atomisp_css_video_configure_output;
5308 get_frame_info = atomisp_css_video_get_output_frame_info;
5309 pipe_id = IA_CSS_PIPE_ID_VIDEO;
5310 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5311 if (!asd->continuous_mode->val) {
5312 configure_output = atomisp_css_video_configure_output;
5314 atomisp_css_video_get_output_frame_info;
5315 pipe_id = IA_CSS_PIPE_ID_VIDEO;
5317 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5318 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
5320 atomisp_css_video_configure_output;
5322 atomisp_css_video_get_output_frame_info;
5323 configure_pp_input =
5324 atomisp_css_video_configure_pp_input;
5325 pipe_id = IA_CSS_PIPE_ID_VIDEO;
5328 atomisp_css_capture_configure_output;
5330 atomisp_css_capture_get_output_frame_info;
5331 configure_pp_input =
5332 atomisp_css_capture_configure_pp_input;
5333 pipe_id = IA_CSS_PIPE_ID_CAPTURE;
5335 atomisp_update_capture_mode(asd);
5336 atomisp_css_capture_enable_online(asd, stream_index, false);
5339 } else if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
5340 configure_output = atomisp_css_preview_configure_output;
5341 get_frame_info = atomisp_css_preview_get_output_frame_info;
5342 configure_pp_input = atomisp_css_preview_configure_pp_input;
5343 pipe_id = IA_CSS_PIPE_ID_PREVIEW;
5345 /* CSS doesn't support low light mode on SOC cameras, so disable
5346 * it. FIXME: if this is done elsewhere, it gives corrupted
5347 * colors into thumbnail image.
5349 if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
5350 asd->params.low_light = false;
5352 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
5353 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
5354 atomisp_css_enable_dz(asd, false);
5356 atomisp_update_capture_mode(asd);
5359 if (!asd->continuous_mode->val)
5360 /* in case of ANR, force capture pipe to offline mode */
5361 atomisp_css_capture_enable_online(asd, stream_index,
5362 asd->params.low_light ?
5363 false : asd->params.online_process);
5365 configure_output = atomisp_css_capture_configure_output;
5366 get_frame_info = atomisp_css_capture_get_output_frame_info;
5367 configure_pp_input = atomisp_css_capture_configure_pp_input;
5368 pipe_id = IA_CSS_PIPE_ID_CAPTURE;
5370 if (!asd->params.online_process &&
5371 !asd->continuous_mode->val) {
5372 ret = atomisp_css_capture_get_output_raw_frame_info(asd,
5377 if (!asd->continuous_mode->val && asd->run_mode->val
5378 != ATOMISP_RUN_MODE_STILL_CAPTURE) {
5380 "Need to set the running mode first\n");
5381 asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE;
5386 * to SOC camera, use yuvpp pipe.
5388 if (ATOMISP_USE_YUVPP(asd))
5389 pipe_id = IA_CSS_PIPE_ID_YUVPP;
5392 ret = atomisp_css_copy_configure_output(asd, stream_index,
5393 pix->width, pix->height,
5394 format->planar ? pix->bytesperline :
5395 pix->bytesperline * 8 / format->depth,
5398 ret = configure_output(asd, pix->width, pix->height,
5399 format->planar ? pix->bytesperline :
5400 pix->bytesperline * 8 / format->depth,
5403 dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n",
5404 pix->width, pix->height, format->sh_fmt);
5408 if (asd->continuous_mode->val &&
5409 (configure_pp_input == atomisp_css_preview_configure_pp_input ||
5410 configure_pp_input == atomisp_css_video_configure_pp_input)) {
5411 /* for isp 2.2, configure pp input is available for continuous
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,
5418 isp_sink_crop->height);
5422 ret = configure_pp_input(asd, isp_sink_crop->width,
5423 isp_sink_crop->height);
5425 dev_err(isp->dev, "configure_pp_input %ux%u\n",
5426 isp_sink_crop->width, isp_sink_crop->height);
5431 ret = atomisp_css_copy_get_output_frame_info(asd, stream_index,
5434 ret = get_frame_info(asd, output_info);
5436 dev_err(isp->dev, "get_frame_info %ux%u (padded to %u)\n",
5437 pix->width, pix->height, pix->bytesperline);
5441 atomisp_update_grid_info(asd, pipe_id, source_pad);
5443 /* Free the raw_dump buffer first */
5444 ia_css_frame_free(asd->raw_output_frame);
5445 asd->raw_output_frame = NULL;
5447 if (!asd->continuous_mode->val &&
5448 !asd->params.online_process && !isp->sw_contex.file_input &&
5449 ia_css_frame_allocate_from_info(&asd->raw_output_frame,
5456 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd,
5457 unsigned int width, unsigned int height,
5458 unsigned int *dvs_env_w, unsigned int *dvs_env_h)
5460 struct atomisp_device *isp = asd->isp;
5462 /* if subdev type is SOC camera,we do not need to set DVS */
5463 if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
5464 asd->params.video_dis_en = false;
5466 if (asd->params.video_dis_en &&
5467 asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5468 /* envelope is 20% of the output resolution */
5470 * dvs envelope cannot be round up.
5471 * it would cause ISP timeout and color switch issue
5473 *dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH);
5474 *dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT);
5477 asd->params.dis_proj_data_valid = false;
5478 asd->params.css_update_params_needed = true;
5481 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd,
5482 int source_pad, struct v4l2_format *f)
5484 #if defined(ISP2401_NEW_INPUT_SYSTEM)
5485 struct v4l2_mbus_framefmt *sink, *src;
5487 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5488 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK);
5489 src = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5490 V4L2_SUBDEV_FORMAT_ACTIVE, source_pad);
5492 if ((sink->code == src->code &&
5493 sink->width == f->fmt.pix.width &&
5494 sink->height == f->fmt.pix.height) ||
5495 ((asd->isp->inputs[asd->input_curr].type == SOC_CAMERA) &&
5496 (asd->isp->inputs[asd->input_curr].camera_caps->
5497 sensor[asd->sensor_curr].stream_num > 1)))
5498 asd->copy_mode = true;
5501 /* Only used for the new input system */
5502 asd->copy_mode = false;
5504 dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode);
5507 static int atomisp_set_fmt_to_snr(struct video_device *vdev,
5508 struct v4l2_format *f, unsigned int pixelformat,
5509 unsigned int padding_w, unsigned int padding_h,
5510 unsigned int dvs_env_w, unsigned int dvs_env_h)
5512 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
5513 const struct atomisp_format_bridge *format;
5514 struct v4l2_subdev_pad_config pad_cfg;
5515 struct v4l2_subdev_format vformat = {
5516 .which = V4L2_SUBDEV_FORMAT_TRY,
5518 struct v4l2_mbus_framefmt *ffmt = &vformat.format;
5519 struct v4l2_mbus_framefmt *req_ffmt;
5520 struct atomisp_device *isp = asd->isp;
5521 struct atomisp_input_stream_info *stream_info =
5522 (struct atomisp_input_stream_info *)ffmt->reserved;
5523 u16 stream_index = ATOMISP_INPUT_STREAM_GENERAL;
5524 int source_pad = atomisp_subdev_source_pad(vdev);
5525 struct v4l2_subdev_fh fh;
5528 v4l2_fh_init(&fh.vfh, vdev);
5530 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
5532 format = atomisp_get_format_bridge(pixelformat);
5536 v4l2_fill_mbus_format(ffmt, &f->fmt.pix, format->mbus_code);
5537 ffmt->height += padding_h + dvs_env_h;
5538 ffmt->width += padding_w + dvs_env_w;
5540 dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n",
5541 ffmt->width, ffmt->height, padding_w, padding_h,
5542 dvs_env_w, dvs_env_h);
5544 __atomisp_init_stream_info(stream_index, stream_info);
5548 /* Disable dvs if resolution can't be supported by sensor */
5549 if (asd->params.video_dis_en &&
5550 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
5551 vformat.which = V4L2_SUBDEV_FORMAT_TRY;
5552 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
5553 pad, set_fmt, &pad_cfg, &vformat);
5556 if (ffmt->width < req_ffmt->width ||
5557 ffmt->height < req_ffmt->height) {
5558 req_ffmt->height -= dvs_env_h;
5559 req_ffmt->width -= dvs_env_w;
5562 "can not enable video dis due to sensor limitation.");
5563 asd->params.video_dis_en = false;
5566 dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
5567 ffmt->width, ffmt->height);
5568 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
5569 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
5570 set_fmt, NULL, &vformat);
5574 __atomisp_update_stream_env(asd, stream_index, stream_info);
5576 dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
5577 ffmt->width, ffmt->height);
5579 if (ffmt->width < ATOM_ISP_STEP_WIDTH ||
5580 ffmt->height < ATOM_ISP_STEP_HEIGHT)
5583 if (asd->params.video_dis_en &&
5584 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
5585 (ffmt->width < req_ffmt->width || ffmt->height < req_ffmt->height)) {
5587 "can not enable video dis due to sensor limitation.");
5588 asd->params.video_dis_en = false;
5591 atomisp_subdev_set_ffmt(&asd->subdev, fh.pad,
5592 V4L2_SUBDEV_FORMAT_ACTIVE,
5593 ATOMISP_SUBDEV_PAD_SINK, ffmt);
5595 return css_input_resolution_changed(asd, ffmt);
5598 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f)
5600 struct atomisp_device *isp = video_get_drvdata(vdev);
5601 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
5602 struct atomisp_sub_device *asd = pipe->asd;
5603 const struct atomisp_format_bridge *format_bridge;
5604 const struct atomisp_format_bridge *snr_format_bridge;
5605 struct ia_css_frame_info output_info, raw_output_info;
5606 struct v4l2_format snr_fmt = *f;
5607 struct v4l2_format backup_fmt = *f, s_fmt = *f;
5608 unsigned int dvs_env_w = 0, dvs_env_h = 0;
5609 unsigned int padding_w = pad_w, padding_h = pad_h;
5610 bool res_overflow = false, crop_needs_override = false;
5611 struct v4l2_mbus_framefmt *isp_sink_fmt;
5612 struct v4l2_mbus_framefmt isp_source_fmt = {0};
5613 struct v4l2_rect isp_sink_crop;
5614 u16 source_pad = atomisp_subdev_source_pad(vdev);
5615 struct v4l2_subdev_fh fh;
5618 if (source_pad >= ATOMISP_SUBDEV_PADS_NUM)
5621 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED) {
5622 dev_warn(isp->dev, "ISP does not support set format while at streaming!\n");
5627 "setting resolution %ux%u on pad %u for asd%d, bytesperline %u\n",
5628 f->fmt.pix.width, f->fmt.pix.height, source_pad,
5629 asd->index, f->fmt.pix.bytesperline);
5631 v4l2_fh_init(&fh.vfh, vdev);
5633 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
5637 pipe->sh_fmt = format_bridge->sh_fmt;
5638 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
5640 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VF ||
5641 (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5642 && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)) {
5643 if (asd->fmt_auto->val) {
5644 struct v4l2_rect *capture_comp;
5645 struct v4l2_rect r = {0};
5647 r.width = f->fmt.pix.width;
5648 r.height = f->fmt.pix.height;
5650 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW)
5651 capture_comp = atomisp_subdev_get_rect(
5653 V4L2_SUBDEV_FORMAT_ACTIVE,
5654 ATOMISP_SUBDEV_PAD_SOURCE_VIDEO,
5655 V4L2_SEL_TGT_COMPOSE);
5657 capture_comp = atomisp_subdev_get_rect(
5659 V4L2_SUBDEV_FORMAT_ACTIVE,
5660 ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5661 V4L2_SEL_TGT_COMPOSE);
5663 if (capture_comp->width < r.width
5664 || capture_comp->height < r.height) {
5665 r.width = capture_comp->width;
5666 r.height = capture_comp->height;
5669 atomisp_subdev_set_selection(
5670 &asd->subdev, fh.pad,
5671 V4L2_SUBDEV_FORMAT_ACTIVE, source_pad,
5672 V4L2_SEL_TGT_COMPOSE, 0, &r);
5674 f->fmt.pix.width = r.width;
5675 f->fmt.pix.height = r.height;
5678 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW &&
5679 (asd->isp->inputs[asd->input_curr].type == SOC_CAMERA) &&
5680 (asd->isp->inputs[asd->input_curr].camera_caps->
5681 sensor[asd->sensor_curr].stream_num > 1)) {
5682 /* For M10MO outputing YUV preview images. */
5684 atomisp_source_pad_to_stream_id(asd,
5685 ATOMISP_SUBDEV_PAD_SOURCE_VIDEO);
5687 ret = atomisp_css_copy_get_output_frame_info(asd,
5688 video_index, &output_info);
5691 "copy_get_output_frame_info ret %i", ret);
5694 if (!asd->yuvpp_mode) {
5696 * If viewfinder was configured into copy_mode,
5697 * we switch to using yuvpp pipe instead.
5699 asd->yuvpp_mode = true;
5700 ret = atomisp_css_copy_configure_output(
5701 asd, video_index, 0, 0, 0, 0);
5704 "failed to disable copy pipe");
5707 ret = atomisp_css_yuvpp_configure_output(
5709 output_info.res.width,
5710 output_info.res.height,
5711 output_info.padded_width,
5712 output_info.format);
5715 "failed to set up yuvpp pipe\n");
5718 atomisp_css_video_enable_online(asd, false);
5719 atomisp_css_preview_enable_online(asd,
5720 ATOMISP_INPUT_STREAM_GENERAL, false);
5722 atomisp_css_yuvpp_configure_viewfinder(asd, video_index,
5723 f->fmt.pix.width, f->fmt.pix.height,
5724 format_bridge->planar ? f->fmt.pix.bytesperline
5725 : f->fmt.pix.bytesperline * 8
5726 / format_bridge->depth, format_bridge->sh_fmt);
5727 atomisp_css_yuvpp_get_viewfinder_frame_info(
5728 asd, video_index, &output_info);
5729 } else if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
5730 atomisp_css_video_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_video_get_viewfinder_frame_info(asd,
5737 asd->copy_mode = false;
5739 atomisp_css_capture_configure_viewfinder(asd,
5740 f->fmt.pix.width, f->fmt.pix.height,
5741 format_bridge->planar ? f->fmt.pix.bytesperline
5742 : f->fmt.pix.bytesperline * 8
5743 / format_bridge->depth, format_bridge->sh_fmt);
5744 atomisp_css_capture_get_viewfinder_frame_info(asd,
5746 asd->copy_mode = false;
5752 * Check whether main resolution configured smaller
5753 * than snapshot resolution. If so, force main resolution
5754 * to be the same as snapshot resolution
5756 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE) {
5757 struct v4l2_rect *r;
5759 r = atomisp_subdev_get_rect(
5761 V4L2_SUBDEV_FORMAT_ACTIVE,
5762 ATOMISP_SUBDEV_PAD_SOURCE_VF, V4L2_SEL_TGT_COMPOSE);
5764 if (r->width && r->height
5765 && (r->width > f->fmt.pix.width
5766 || r->height > f->fmt.pix.height))
5768 "Main Resolution config smaller then Vf Resolution. Force to be equal with Vf Resolution.");
5771 /* Pipeline configuration done through subdevs. Bail out now. */
5772 if (!asd->fmt_auto->val)
5773 goto set_fmt_to_isp;
5775 /* get sensor resolution and format */
5776 ret = atomisp_try_fmt(vdev, &snr_fmt, &res_overflow);
5778 dev_warn(isp->dev, "Try format failed with error %d\n", ret);
5781 f->fmt.pix.width = snr_fmt.fmt.pix.width;
5782 f->fmt.pix.height = snr_fmt.fmt.pix.height;
5785 atomisp_get_format_bridge(snr_fmt.fmt.pix.pixelformat);
5786 if (!snr_format_bridge) {
5787 dev_warn(isp->dev, "Can't find bridge format\n");
5791 atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5792 V4L2_SUBDEV_FORMAT_ACTIVE,
5793 ATOMISP_SUBDEV_PAD_SINK)->code =
5794 snr_format_bridge->mbus_code;
5796 isp_sink_fmt = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5797 V4L2_SUBDEV_FORMAT_ACTIVE,
5798 ATOMISP_SUBDEV_PAD_SINK);
5800 isp_source_fmt.code = format_bridge->mbus_code;
5801 atomisp_subdev_set_ffmt(&asd->subdev, fh.pad,
5802 V4L2_SUBDEV_FORMAT_ACTIVE,
5803 source_pad, &isp_source_fmt);
5805 if (!atomisp_subdev_format_conversion(asd, source_pad)) {
5808 } else if (IS_BYT) {
5813 /* construct resolution supported by isp */
5814 if (res_overflow && !asd->continuous_mode->val) {
5815 f->fmt.pix.width = rounddown(
5816 clamp_t(u32, f->fmt.pix.width - padding_w,
5818 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
5819 f->fmt.pix.height = rounddown(
5820 clamp_t(u32, f->fmt.pix.height - padding_h,
5821 ATOM_ISP_MIN_HEIGHT,
5822 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
5825 atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height,
5826 &dvs_env_w, &dvs_env_h);
5828 if (asd->continuous_mode->val) {
5829 struct v4l2_rect *r;
5831 r = atomisp_subdev_get_rect(
5833 V4L2_SUBDEV_FORMAT_ACTIVE,
5834 ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5835 V4L2_SEL_TGT_COMPOSE);
5837 * The ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE should get resolutions
5838 * properly set otherwise, it should not be the capture_pad.
5840 if (r->width && r->height)
5841 asd->capture_pad = ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE;
5843 asd->capture_pad = source_pad;
5845 asd->capture_pad = source_pad;
5848 * set format info to sensor
5849 * In continuous mode, resolution is set only if it is higher than
5850 * existing value. This because preview pipe will be configured after
5851 * capture pipe and usually has lower resolution than capture pipe.
5853 if (!asd->continuous_mode->val ||
5854 isp_sink_fmt->width < (f->fmt.pix.width + padding_w + dvs_env_w) ||
5855 isp_sink_fmt->height < (f->fmt.pix.height + padding_h +
5858 * For jpeg or custom raw format the sensor will return constant
5859 * width and height. Because we already had quried try_mbus_fmt,
5860 * f->fmt.pix.width and f->fmt.pix.height has been changed to
5861 * this fixed width and height. So we cannot select the correct
5862 * resolution with that information. So use the original width
5863 * and height while set_mbus_fmt() so actual resolutions are
5864 * being used in while set media bus format.
5867 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG ||
5868 f->fmt.pix.pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
5869 s_fmt.fmt.pix.width = backup_fmt.fmt.pix.width;
5870 s_fmt.fmt.pix.height = backup_fmt.fmt.pix.height;
5872 ret = atomisp_set_fmt_to_snr(vdev, &s_fmt,
5873 f->fmt.pix.pixelformat, padding_w,
5874 padding_h, dvs_env_w, dvs_env_h);
5877 "Set format to sensor failed with %d\n", ret);
5881 atomisp_csi_lane_config(isp);
5882 crop_needs_override = true;
5885 atomisp_check_copy_mode(asd, source_pad, &backup_fmt);
5886 asd->yuvpp_mode = false; /* Reset variable */
5888 isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL,
5889 V4L2_SUBDEV_FORMAT_ACTIVE,
5890 ATOMISP_SUBDEV_PAD_SINK,
5893 /* Try to enable YUV downscaling if ISP input is 10 % (either
5894 * width or height) bigger than the desired result. */
5895 if (isp_sink_crop.width * 9 / 10 < f->fmt.pix.width ||
5896 isp_sink_crop.height * 9 / 10 < f->fmt.pix.height ||
5897 (atomisp_subdev_format_conversion(asd, source_pad) &&
5898 ((asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
5899 !asd->continuous_mode->val) ||
5900 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) {
5901 /* for continuous mode, preview size might be smaller than
5902 * still capture size. if preview size still needs crop,
5903 * pick the larger one between crop size of preview and
5906 if (asd->continuous_mode->val
5907 && source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5908 && !crop_needs_override) {
5909 isp_sink_crop.width =
5910 max_t(unsigned int, f->fmt.pix.width,
5911 isp_sink_crop.width);
5912 isp_sink_crop.height =
5913 max_t(unsigned int, f->fmt.pix.height,
5914 isp_sink_crop.height);
5916 isp_sink_crop.width = f->fmt.pix.width;
5917 isp_sink_crop.height = f->fmt.pix.height;
5920 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5921 V4L2_SUBDEV_FORMAT_ACTIVE,
5922 ATOMISP_SUBDEV_PAD_SINK,
5924 V4L2_SEL_FLAG_KEEP_CONFIG,
5926 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5927 V4L2_SUBDEV_FORMAT_ACTIVE,
5928 source_pad, V4L2_SEL_TGT_COMPOSE,
5930 } else if (IS_MOFD) {
5931 struct v4l2_rect main_compose = {0};
5933 main_compose.width = isp_sink_crop.width;
5934 main_compose.height =
5935 DIV_ROUND_UP(main_compose.width * f->fmt.pix.height,
5937 if (main_compose.height > isp_sink_crop.height) {
5938 main_compose.height = isp_sink_crop.height;
5939 main_compose.width =
5940 DIV_ROUND_UP(main_compose.height *
5945 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5946 V4L2_SUBDEV_FORMAT_ACTIVE,
5948 V4L2_SEL_TGT_COMPOSE, 0,
5951 struct v4l2_rect sink_crop = {0};
5952 struct v4l2_rect main_compose = {0};
5954 main_compose.width = f->fmt.pix.width;
5955 main_compose.height = f->fmt.pix.height;
5957 /* WORKAROUND: this override is universally enabled in
5958 * GMIN to work around a CTS failures (GMINL-539)
5959 * which appears to be related by a hardware
5960 * performance limitation. It's unclear why this
5961 * particular code triggers the issue. */
5962 if (!atomisp_hw_is_isp2401 || crop_needs_override) {
5963 if (isp_sink_crop.width * main_compose.height >
5964 isp_sink_crop.height * main_compose.width) {
5965 sink_crop.height = isp_sink_crop.height;
5966 sink_crop.width = DIV_NEAREST_STEP(
5970 ATOM_ISP_STEP_WIDTH);
5972 sink_crop.width = isp_sink_crop.width;
5973 sink_crop.height = DIV_NEAREST_STEP(
5977 ATOM_ISP_STEP_HEIGHT);
5979 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5980 V4L2_SUBDEV_FORMAT_ACTIVE,
5981 ATOMISP_SUBDEV_PAD_SINK,
5983 V4L2_SEL_FLAG_KEEP_CONFIG,
5986 atomisp_subdev_set_selection(&asd->subdev, fh.pad,
5987 V4L2_SUBDEV_FORMAT_ACTIVE,
5989 V4L2_SEL_TGT_COMPOSE, 0,
5994 ret = atomisp_set_fmt_to_isp(vdev, &output_info, &raw_output_info,
5995 &f->fmt.pix, source_pad);
5997 dev_warn(isp->dev, "Can't set format on ISP. Error %d\n", ret);
6001 pipe->pix.width = f->fmt.pix.width;
6002 pipe->pix.height = f->fmt.pix.height;
6003 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
6004 if (format_bridge->planar) {
6005 pipe->pix.bytesperline = output_info.padded_width;
6006 pipe->pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height *
6007 DIV_ROUND_UP(format_bridge->depth *
6008 output_info.padded_width, 8));
6010 pipe->pix.bytesperline =
6011 DIV_ROUND_UP(format_bridge->depth *
6012 output_info.padded_width, 8);
6013 pipe->pix.sizeimage =
6014 PAGE_ALIGN(f->fmt.pix.height * pipe->pix.bytesperline);
6016 dev_dbg(isp->dev, "%s: image size: %d, %d bytes per line\n",
6017 __func__, pipe->pix.sizeimage, pipe->pix.bytesperline);
6019 if (f->fmt.pix.field == V4L2_FIELD_ANY)
6020 f->fmt.pix.field = V4L2_FIELD_NONE;
6021 pipe->pix.field = f->fmt.pix.field;
6023 f->fmt.pix = pipe->pix;
6024 f->fmt.pix.priv = PAGE_ALIGN(pipe->pix.width *
6025 pipe->pix.height * 2);
6027 pipe->capq.field = f->fmt.pix.field;
6030 * If in video 480P case, no GFX throttle
6032 if (asd->run_mode->val == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
6033 f->fmt.pix.width == 720 && f->fmt.pix.height == 480)
6034 isp->need_gfx_throttle = false;
6036 isp->need_gfx_throttle = true;
6041 int atomisp_set_fmt_file(struct video_device *vdev, struct v4l2_format *f)
6043 struct atomisp_device *isp = video_get_drvdata(vdev);
6044 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
6045 struct atomisp_sub_device *asd = pipe->asd;
6046 struct v4l2_mbus_framefmt ffmt = {0};
6047 const struct atomisp_format_bridge *format_bridge;
6048 struct v4l2_subdev_fh fh;
6051 v4l2_fh_init(&fh.vfh, vdev);
6053 dev_dbg(isp->dev, "setting fmt %ux%u 0x%x for file inject\n",
6054 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
6055 ret = atomisp_try_fmt_file(isp, f);
6057 dev_err(isp->dev, "atomisp_try_fmt_file err: %d\n", ret);
6061 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
6062 if (!format_bridge) {
6063 dev_dbg(isp->dev, "atomisp_get_format_bridge err! fmt:0x%x\n",
6064 f->fmt.pix.pixelformat);
6068 pipe->pix = f->fmt.pix;
6069 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_FIFO);
6070 atomisp_css_input_configure_port(asd,
6071 __get_mipi_port(isp, ATOMISP_CAMERA_PORT_PRIMARY), 2, 0xffff4,
6073 ffmt.width = f->fmt.pix.width;
6074 ffmt.height = f->fmt.pix.height;
6075 ffmt.code = format_bridge->mbus_code;
6077 atomisp_subdev_set_ffmt(&asd->subdev, fh.pad, V4L2_SUBDEV_FORMAT_ACTIVE,
6078 ATOMISP_SUBDEV_PAD_SINK, &ffmt);
6083 int atomisp_set_shading_table(struct atomisp_sub_device *asd,
6084 struct atomisp_shading_table *user_shading_table)
6086 struct ia_css_shading_table *shading_table;
6087 struct ia_css_shading_table *free_table;
6088 unsigned int len_table;
6092 if (!user_shading_table)
6095 if (!user_shading_table->enable) {
6096 asd->params.config.shading_table = NULL;
6097 asd->params.sc_en = false;
6101 /* If enabling, all tables must be set */
6102 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
6103 if (!user_shading_table->data[i])
6107 /* Shading table size per color */
6108 if (!atomisp_hw_is_isp2401) {
6109 if (user_shading_table->width > ISP2400_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
6110 user_shading_table->height > ISP2400_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
6113 if (user_shading_table->width > ISP2401_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
6114 user_shading_table->height > ISP2401_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
6118 shading_table = atomisp_css_shading_table_alloc(
6119 user_shading_table->width, user_shading_table->height);
6123 len_table = user_shading_table->width * user_shading_table->height *
6124 ATOMISP_SC_TYPE_SIZE;
6125 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
6126 ret = copy_from_user(shading_table->data[i],
6127 (void __user *)user_shading_table->data[i],
6130 free_table = shading_table;
6135 shading_table->sensor_width = user_shading_table->sensor_width;
6136 shading_table->sensor_height = user_shading_table->sensor_height;
6137 shading_table->fraction_bits = user_shading_table->fraction_bits;
6139 free_table = asd->params.css_param.shading_table;
6140 asd->params.css_param.shading_table = shading_table;
6141 asd->params.config.shading_table = shading_table;
6142 asd->params.sc_en = true;
6146 atomisp_css_shading_table_free(free_table);
6151 /*Turn off ISP dphy */
6152 int atomisp_ospm_dphy_down(struct atomisp_device *isp)
6154 unsigned long flags;
6157 dev_dbg(isp->dev, "%s\n", __func__);
6159 /* if ISP timeout, we can force powerdown */
6160 if (isp->isp_timeout)
6163 if (!atomisp_dev_users(isp))
6166 spin_lock_irqsave(&isp->lock, flags);
6167 isp->sw_contex.power_state = ATOM_ISP_POWER_DOWN;
6168 spin_unlock_irqrestore(&isp->lock, flags);
6171 * MRFLD IUNIT DPHY is located in an always-power-on island
6172 * MRFLD HW design need all CSI ports are disabled before
6173 * powering down the IUNIT.
6175 pci_read_config_dword(isp->pdev, MRFLD_PCI_CSI_CONTROL, ®);
6176 reg |= MRFLD_ALL_CSI_PORTS_OFF_MASK;
6177 pci_write_config_dword(isp->pdev, MRFLD_PCI_CSI_CONTROL, reg);
6181 /*Turn on ISP dphy */
6182 int atomisp_ospm_dphy_up(struct atomisp_device *isp)
6184 unsigned long flags;
6186 dev_dbg(isp->dev, "%s\n", __func__);
6188 spin_lock_irqsave(&isp->lock, flags);
6189 isp->sw_contex.power_state = ATOM_ISP_POWER_UP;
6190 spin_unlock_irqrestore(&isp->lock, flags);
6195 int atomisp_exif_makernote(struct atomisp_sub_device *asd,
6196 struct atomisp_makernote_info *config)
6198 struct v4l2_control ctrl;
6199 struct atomisp_device *isp = asd->isp;
6201 ctrl.id = V4L2_CID_FOCAL_ABSOLUTE;
6203 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6204 dev_warn(isp->dev, "failed to g_ctrl for focal length\n");
6207 config->focal_length = ctrl.value;
6210 ctrl.id = V4L2_CID_FNUMBER_ABSOLUTE;
6212 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6213 dev_warn(isp->dev, "failed to g_ctrl for f-number\n");
6216 config->f_number_curr = ctrl.value;
6219 ctrl.id = V4L2_CID_FNUMBER_RANGE;
6221 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6222 dev_warn(isp->dev, "failed to g_ctrl for f number range\n");
6225 config->f_number_range = ctrl.value;
6231 int atomisp_offline_capture_configure(struct atomisp_sub_device *asd,
6232 struct atomisp_cont_capture_conf *cvf_config)
6234 struct v4l2_ctrl *c;
6237 * In case of M10MO ZSL capture case, we need to issue a separate
6238 * capture request to M10MO which will output captured jpeg image
6241 asd->isp->inputs[asd->input_curr].camera->ctrl_handler,
6242 V4L2_CID_START_ZSL_CAPTURE);
6246 dev_dbg(asd->isp->dev, "%s trigger ZSL capture request\n",
6248 /* TODO: use the cvf_config */
6249 ret = v4l2_ctrl_s_ctrl(c, 1);
6253 return v4l2_ctrl_s_ctrl(c, 0);
6256 asd->params.offline_parm = *cvf_config;
6258 if (asd->params.offline_parm.num_captures) {
6259 if (asd->streaming == ATOMISP_DEVICE_STREAMING_DISABLED) {
6260 unsigned int init_raw_num;
6262 if (asd->enable_raw_buffer_lock->val) {
6264 ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES_LOCK_EN;
6265 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
6266 asd->params.video_dis_en)
6268 ATOMISP_CSS2_NUM_DVS_FRAME_DELAY;
6271 ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES;
6274 /* TODO: this can be removed once user-space
6275 * has been updated to use control API */
6276 asd->continuous_raw_buffer_size->val =
6278 asd->continuous_raw_buffer_size->val,
6279 asd->params.offline_parm.
6280 num_captures + init_raw_num);
6281 asd->continuous_raw_buffer_size->val =
6282 min_t(int, ATOMISP_CONT_RAW_FRAMES,
6283 asd->continuous_raw_buffer_size->val);
6285 asd->continuous_mode->val = true;
6287 asd->continuous_mode->val = false;
6288 __enable_continuous_mode(asd, false);
6295 * set auto exposure metering window to camera sensor
6297 int atomisp_s_ae_window(struct atomisp_sub_device *asd,
6298 struct atomisp_ae_window *arg)
6300 struct atomisp_device *isp = asd->isp;
6301 /* Coverity CID 298071 - initialzize struct */
6302 struct v4l2_subdev_selection sel = { 0 };
6304 sel.r.left = arg->x_left;
6305 sel.r.top = arg->y_top;
6306 sel.r.width = arg->x_right - arg->x_left + 1;
6307 sel.r.height = arg->y_bottom - arg->y_top + 1;
6309 if (v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
6310 pad, set_selection, NULL, &sel)) {
6311 dev_err(isp->dev, "failed to call sensor set_selection.\n");
6318 int atomisp_flash_enable(struct atomisp_sub_device *asd, int num_frames)
6320 struct atomisp_device *isp = asd->isp;
6322 if (num_frames < 0) {
6323 dev_dbg(isp->dev, "%s ERROR: num_frames: %d\n", __func__,
6327 /* a requested flash is still in progress. */
6328 if (num_frames && asd->params.flash_state != ATOMISP_FLASH_IDLE) {
6329 dev_dbg(isp->dev, "%s flash busy: %d frames left: %d\n",
6330 __func__, asd->params.flash_state,
6331 asd->params.num_flash_frames);
6335 asd->params.num_flash_frames = num_frames;
6336 asd->params.flash_state = ATOMISP_FLASH_REQUESTED;
6340 int atomisp_source_pad_to_stream_id(struct atomisp_sub_device *asd,
6341 uint16_t source_pad)
6344 struct atomisp_device *isp = asd->isp;
6346 if (isp->inputs[asd->input_curr].camera_caps->
6347 sensor[asd->sensor_curr].stream_num == 1)
6348 return ATOMISP_INPUT_STREAM_GENERAL;
6350 switch (source_pad) {
6351 case ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE:
6352 stream_id = ATOMISP_INPUT_STREAM_CAPTURE;
6354 case ATOMISP_SUBDEV_PAD_SOURCE_VF:
6355 stream_id = ATOMISP_INPUT_STREAM_POSTVIEW;
6357 case ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW:
6358 stream_id = ATOMISP_INPUT_STREAM_PREVIEW;
6360 case ATOMISP_SUBDEV_PAD_SOURCE_VIDEO:
6361 stream_id = ATOMISP_INPUT_STREAM_VIDEO;
6364 stream_id = ATOMISP_INPUT_STREAM_GENERAL;
6370 bool atomisp_is_vf_pipe(struct atomisp_video_pipe *pipe)
6372 struct atomisp_sub_device *asd = pipe->asd;
6374 if (pipe == &asd->video_out_vf)
6377 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
6378 pipe == &asd->video_out_preview)
6384 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id)
6386 struct atomisp_device *isp = asd->isp;
6388 if (!asd->enable_raw_buffer_lock->val) {
6389 dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__);
6392 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED) {
6393 dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n",
6394 __func__, exp_id, asd->streaming);
6397 if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) {
6398 dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id);
6404 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd)
6406 unsigned long flags;
6408 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6409 memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap));
6410 asd->raw_buffer_locked_count = 0;
6411 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6414 int atomisp_set_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
6417 unsigned long flags;
6419 if (__checking_exp_id(asd, exp_id))
6422 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6424 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6425 (*bitmap) |= (1 << bit);
6426 asd->raw_buffer_locked_count++;
6427 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6429 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
6430 __func__, exp_id, asd->raw_buffer_locked_count);
6432 /* Check if the raw buffer after next is still locked!!! */
6434 if (exp_id > ATOMISP_MAX_EXP_ID)
6435 exp_id -= ATOMISP_MAX_EXP_ID;
6436 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6438 if ((*bitmap) & (1 << bit)) {
6441 /* WORKAROUND unlock the raw buffer compulsively */
6442 ret = atomisp_css_exp_id_unlock(asd, exp_id);
6444 dev_err(asd->isp->dev,
6445 "%s exp_id is wrapping back to %d but force unlock failed,, err %d.\n",
6446 __func__, exp_id, ret);
6450 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6451 (*bitmap) &= ~(1 << bit);
6452 asd->raw_buffer_locked_count--;
6453 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6454 dev_warn(asd->isp->dev,
6455 "%s exp_id is wrapping back to %d but it is still locked so force unlock it, raw_buffer_locked_count %d\n",
6456 __func__, exp_id, asd->raw_buffer_locked_count);
6461 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id)
6464 unsigned long flags;
6467 if (__checking_exp_id(asd, exp_id))
6470 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6472 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6473 ret = ((*bitmap) & (1 << bit));
6474 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6478 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
6481 unsigned long flags;
6483 if (__is_raw_buffer_locked(asd, exp_id))
6486 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6488 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6489 (*bitmap) &= ~(1 << bit);
6490 asd->raw_buffer_locked_count--;
6491 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6493 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
6494 __func__, exp_id, asd->raw_buffer_locked_count);
6498 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id)
6500 struct atomisp_device *isp = asd->isp;
6501 int value = *exp_id;
6504 ret = __is_raw_buffer_locked(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_capture(asd, value);
6513 dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value);
6519 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id)
6521 struct atomisp_device *isp = asd->isp;
6522 int value = *exp_id;
6525 ret = __clear_raw_buffer_bitmap(asd, value);
6527 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
6531 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
6532 ret = atomisp_css_exp_id_unlock(asd, value);
6534 dev_err(isp->dev, "%s exp_id %d failed, err %d.\n",
6535 __func__, value, ret);
6540 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd,
6541 unsigned int *enable)
6548 value = *enable > 0 ? true : false;
6550 atomisp_en_dz_capt_pipe(asd, value);
6555 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event)
6557 if (!event || asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
6560 dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n",
6564 case V4L2_EVENT_FRAME_SYNC:
6565 atomisp_sof_event(asd);
6567 case V4L2_EVENT_FRAME_END:
6568 atomisp_eof_event(asd, 0);
6570 case V4L2_EVENT_ATOMISP_3A_STATS_READY:
6571 atomisp_3a_stats_ready_event(asd, 0);
6573 case V4L2_EVENT_ATOMISP_METADATA_READY:
6574 atomisp_metadata_ready_event(asd, 0);
6583 static int atomisp_get_pipe_id(struct atomisp_video_pipe *pipe)
6585 struct atomisp_sub_device *asd = pipe->asd;
6587 if (ATOMISP_USE_YUVPP(asd))
6588 return IA_CSS_PIPE_ID_YUVPP;
6589 else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER)
6590 return IA_CSS_PIPE_ID_VIDEO;
6591 else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT)
6592 return IA_CSS_PIPE_ID_CAPTURE;
6593 else if (pipe == &asd->video_out_video_capture)
6594 return IA_CSS_PIPE_ID_VIDEO;
6595 else if (pipe == &asd->video_out_vf)
6596 return IA_CSS_PIPE_ID_CAPTURE;
6597 else if (pipe == &asd->video_out_preview) {
6598 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
6599 return IA_CSS_PIPE_ID_VIDEO;
6601 return IA_CSS_PIPE_ID_PREVIEW;
6602 } else if (pipe == &asd->video_out_capture) {
6604 return IA_CSS_PIPE_ID_COPY;
6606 return IA_CSS_PIPE_ID_CAPTURE;
6610 dev_warn(asd->isp->dev, "%s failed to find proper pipe\n",
6612 return IA_CSS_PIPE_ID_CAPTURE;
6615 int atomisp_get_invalid_frame_num(struct video_device *vdev,
6616 int *invalid_frame_num)
6618 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
6619 struct atomisp_sub_device *asd = pipe->asd;
6620 enum ia_css_pipe_id pipe_id;
6621 struct ia_css_pipe_info p_info;
6624 if (asd->isp->inputs[asd->input_curr].camera_caps->
6625 sensor[asd->sensor_curr].stream_num > 1) {
6627 *invalid_frame_num = 0;
6631 pipe_id = atomisp_get_pipe_id(pipe);
6632 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipes[pipe_id]) {
6633 dev_warn(asd->isp->dev,
6634 "%s pipe %d has not been created yet, do SET_FMT first!\n",
6639 ret = ia_css_pipe_get_info(
6640 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
6641 .pipes[pipe_id], &p_info);
6643 *invalid_frame_num = p_info.num_invalid_frames;
6646 dev_warn(asd->isp->dev, "%s get pipe infor failed %d\n",