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>
25 #include <linux/kernel.h>
26 #include <linux/kfifo.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/timer.h>
30 #include <asm/iosf_mbi.h>
32 #include <media/v4l2-event.h>
33 #include <media/videobuf-vmalloc.h>
35 #define CREATE_TRACE_POINTS
36 #include "atomisp_trace_event.h"
38 #include "atomisp_cmd.h"
39 #include "atomisp_common.h"
40 #include "atomisp_fops.h"
41 #include "atomisp_internal.h"
42 #include "atomisp_ioctl.h"
43 #include "atomisp-regs.h"
44 #include "atomisp_tables.h"
45 #include "atomisp_acc.h"
46 #include "atomisp_compat.h"
47 #include "atomisp_subdev.h"
48 #include "atomisp_dfs_tables.h"
52 #include "sh_css_hrt.h"
53 #include "sh_css_defs.h"
54 #include "system_global.h"
55 #include "sh_css_internal.h"
56 #include "sh_css_sp.h"
57 #include "gp_device.h"
58 #include "device_access.h"
61 #include "ia_css_types.h"
62 #include "ia_css_stream.h"
63 #include "ia_css_debug.h"
66 /* We should never need to run the flash for more than 2 frames.
67 * At 15fps this means 133ms. We set the timeout a bit longer.
68 * Each flash driver is supposed to set its own timeout, but
69 * just in case someone else changed the timeout, we set it
70 * here to make sure we don't damage the flash hardware. */
71 #define FLASH_TIMEOUT 800 /* ms */
76 void __user *user_ptr;
85 * get sensor:dis71430/ov2720 related info from v4l2_subdev->priv data field.
86 * subdev->priv is set in mrst.c
88 struct camera_mipi_info *atomisp_to_sensor_mipi_info(struct v4l2_subdev *sd)
90 return (struct camera_mipi_info *)v4l2_get_subdev_hostdata(sd);
94 * get struct atomisp_video_pipe from v4l2 video_device
96 struct atomisp_video_pipe *atomisp_to_video_pipe(struct video_device *dev)
98 return (struct atomisp_video_pipe *)
99 container_of(dev, struct atomisp_video_pipe, vdev);
103 * get struct atomisp_acc_pipe from v4l2 video_device
105 struct atomisp_acc_pipe *atomisp_to_acc_pipe(struct video_device *dev)
107 return (struct atomisp_acc_pipe *)
108 container_of(dev, struct atomisp_acc_pipe, vdev);
111 static unsigned short atomisp_get_sensor_fps(struct atomisp_sub_device *asd)
113 struct v4l2_subdev_frame_interval fi = { 0 };
114 struct atomisp_device *isp = asd->isp;
116 unsigned short fps = 0;
119 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
120 video, g_frame_interval, &fi);
122 if (!ret && fi.interval.numerator)
123 fps = fi.interval.denominator / fi.interval.numerator;
129 * DFS progress is shown as follows:
130 * 1. Target frequency is calculated according to FPS/Resolution/ISP running
132 * 2. Ratio is calculated using formula: 2 * HPLL / target frequency - 1
133 * with proper rounding.
134 * 3. Set ratio to ISPFREQ40, 1 to FREQVALID and ISPFREQGUAR40
135 * to 200MHz in ISPSSPM1.
136 * 4. Wait for FREQVALID to be cleared by P-Unit.
137 * 5. Wait for field ISPFREQSTAT40 in ISPSSPM1 turn to ratio set in 3.
139 static int write_target_freq_to_hw(struct atomisp_device *isp,
140 unsigned int new_freq)
142 unsigned int ratio, timeout, guar_ratio;
146 if (!isp->hpll_freq) {
147 dev_err(isp->dev, "failed to get hpll_freq. no change to freq\n");
151 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
152 if (isp_sspm1 & ISP_FREQ_VALID_MASK) {
153 dev_dbg(isp->dev, "clearing ISPSSPM1 valid bit.\n");
154 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
155 isp_sspm1 & ~(1 << ISP_FREQ_VALID_OFFSET));
158 ratio = (2 * isp->hpll_freq + new_freq / 2) / new_freq - 1;
159 guar_ratio = (2 * isp->hpll_freq + 200 / 2) / 200 - 1;
161 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
162 isp_sspm1 &= ~(0x1F << ISP_REQ_FREQ_OFFSET);
164 for (i = 0; i < ISP_DFS_TRY_TIMES; i++) {
165 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
167 | ratio << ISP_REQ_FREQ_OFFSET
168 | 1 << ISP_FREQ_VALID_OFFSET
169 | guar_ratio << ISP_REQ_GUAR_FREQ_OFFSET);
171 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
173 while ((isp_sspm1 & ISP_FREQ_VALID_MASK) && timeout) {
174 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
175 dev_dbg(isp->dev, "waiting for ISPSSPM1 valid bit to be 0.\n");
185 dev_err(isp->dev, "DFS failed due to HW error.\n");
189 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
191 while (((isp_sspm1 >> ISP_FREQ_STAT_OFFSET) != ratio) && timeout) {
192 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
193 dev_dbg(isp->dev, "waiting for ISPSSPM1 status bit to be 0x%x.\n",
199 dev_err(isp->dev, "DFS target freq is rejected by HW.\n");
206 int atomisp_freq_scaling(struct atomisp_device *isp,
207 enum atomisp_dfs_mode mode,
210 struct pci_dev *pdev = to_pci_dev(isp->dev);
211 /* FIXME! Only use subdev[0] status yet */
212 struct atomisp_sub_device *asd = &isp->asd[0];
213 const struct atomisp_dfs_config *dfs;
214 unsigned int new_freq;
215 struct atomisp_freq_scaling_rule curr_rules;
217 unsigned short fps = 0;
219 if (isp->sw_contex.power_state != ATOM_ISP_POWER_UP) {
220 dev_err(isp->dev, "DFS cannot proceed due to no power.\n");
224 if ((pdev->device & ATOMISP_PCI_DEVICE_SOC_MASK) ==
225 ATOMISP_PCI_DEVICE_SOC_CHT && ATOMISP_USE_YUVPP(asd))
226 isp->dfs = &dfs_config_cht_soc;
230 if (dfs->lowest_freq == 0 || dfs->max_freq_at_vmin == 0 ||
231 dfs->highest_freq == 0 || dfs->dfs_table_size == 0 ||
233 dev_err(isp->dev, "DFS configuration is invalid.\n");
237 if (mode == ATOMISP_DFS_MODE_LOW) {
238 new_freq = dfs->lowest_freq;
242 if (mode == ATOMISP_DFS_MODE_MAX) {
243 new_freq = dfs->highest_freq;
247 fps = atomisp_get_sensor_fps(asd);
250 "Sensor didn't report FPS. Using DFS max mode.\n");
251 new_freq = dfs->highest_freq;
255 curr_rules.width = asd->fmt[asd->capture_pad].fmt.width;
256 curr_rules.height = asd->fmt[asd->capture_pad].fmt.height;
257 curr_rules.fps = fps;
258 curr_rules.run_mode = asd->run_mode->val;
260 * For continuous mode, we need to make the capture setting applied
261 * since preview mode, because there is no chance to do this when
262 * starting image capture.
264 if (asd->continuous_mode->val) {
265 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
266 curr_rules.run_mode = ATOMISP_RUN_MODE_SDV;
268 curr_rules.run_mode =
269 ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE;
272 /* search for the target frequency by looping freq rules*/
273 for (i = 0; i < dfs->dfs_table_size; i++) {
274 if (curr_rules.width != dfs->dfs_table[i].width &&
275 dfs->dfs_table[i].width != ISP_FREQ_RULE_ANY)
277 if (curr_rules.height != dfs->dfs_table[i].height &&
278 dfs->dfs_table[i].height != ISP_FREQ_RULE_ANY)
280 if (curr_rules.fps != dfs->dfs_table[i].fps &&
281 dfs->dfs_table[i].fps != ISP_FREQ_RULE_ANY)
283 if (curr_rules.run_mode != dfs->dfs_table[i].run_mode &&
284 dfs->dfs_table[i].run_mode != ISP_FREQ_RULE_ANY)
289 if (i == dfs->dfs_table_size)
290 new_freq = dfs->max_freq_at_vmin;
292 new_freq = dfs->dfs_table[i].isp_freq;
295 dev_dbg(isp->dev, "DFS target frequency=%d.\n", new_freq);
297 if ((new_freq == isp->sw_contex.running_freq) && !force)
300 dev_dbg(isp->dev, "Programming DFS frequency to %d\n", new_freq);
302 ret = write_target_freq_to_hw(isp, new_freq);
304 isp->sw_contex.running_freq = new_freq;
305 trace_ipu_pstate(new_freq, -1);
311 * reset and restore ISP
313 int atomisp_reset(struct atomisp_device *isp)
315 /* Reset ISP by power-cycling it */
318 dev_dbg(isp->dev, "%s\n", __func__);
319 atomisp_css_suspend(isp);
320 ret = atomisp_runtime_suspend(isp->dev);
322 dev_err(isp->dev, "atomisp_runtime_suspend failed, %d\n", ret);
323 ret = atomisp_mrfld_power_down(isp);
325 dev_err(isp->dev, "can not disable ISP power\n");
327 ret = atomisp_mrfld_power_up(isp);
329 dev_err(isp->dev, "can not enable ISP power\n");
330 ret = atomisp_runtime_resume(isp->dev);
332 dev_err(isp->dev, "atomisp_runtime_resume failed, %d\n", ret);
334 ret = atomisp_css_resume(isp);
336 isp->isp_fatal_error = true;
342 * interrupt disable functions
344 static void disable_isp_irq(enum hrt_isp_css_irq irq)
346 irq_disable_channel(IRQ0_ID, irq);
348 if (irq != hrt_isp_css_irq_sp)
351 cnd_sp_irq_enable(SP0_ID, false);
355 * interrupt clean function
357 static void clear_isp_irq(enum hrt_isp_css_irq irq)
359 irq_clear_all(IRQ0_ID);
362 void atomisp_msi_irq_init(struct atomisp_device *isp)
364 struct pci_dev *pdev = to_pci_dev(isp->dev);
368 pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32);
369 msg32 |= 1 << MSI_ENABLE_BIT;
370 pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32);
372 msg32 = (1 << INTR_IER) | (1 << INTR_IIR);
373 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32);
375 pci_read_config_word(pdev, PCI_COMMAND, &msg16);
376 msg16 |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
377 PCI_COMMAND_INTX_DISABLE);
378 pci_write_config_word(pdev, PCI_COMMAND, msg16);
381 void atomisp_msi_irq_uninit(struct atomisp_device *isp)
383 struct pci_dev *pdev = to_pci_dev(isp->dev);
387 pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32);
388 msg32 &= ~(1 << MSI_ENABLE_BIT);
389 pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32);
392 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32);
394 pci_read_config_word(pdev, PCI_COMMAND, &msg16);
395 msg16 &= ~(PCI_COMMAND_MASTER);
396 pci_write_config_word(pdev, PCI_COMMAND, msg16);
399 static void atomisp_sof_event(struct atomisp_sub_device *asd)
401 struct v4l2_event event = {0};
403 event.type = V4L2_EVENT_FRAME_SYNC;
404 event.u.frame_sync.frame_sequence = atomic_read(&asd->sof_count);
406 v4l2_event_queue(asd->subdev.devnode, &event);
409 void atomisp_eof_event(struct atomisp_sub_device *asd, uint8_t exp_id)
411 struct v4l2_event event = {0};
413 event.type = V4L2_EVENT_FRAME_END;
414 event.u.frame_sync.frame_sequence = exp_id;
416 v4l2_event_queue(asd->subdev.devnode, &event);
419 static void atomisp_3a_stats_ready_event(struct atomisp_sub_device *asd,
422 struct v4l2_event event = {0};
424 event.type = V4L2_EVENT_ATOMISP_3A_STATS_READY;
425 event.u.frame_sync.frame_sequence = exp_id;
427 v4l2_event_queue(asd->subdev.devnode, &event);
430 static void atomisp_metadata_ready_event(struct atomisp_sub_device *asd,
431 enum atomisp_metadata_type md_type)
433 struct v4l2_event event = {0};
435 event.type = V4L2_EVENT_ATOMISP_METADATA_READY;
436 event.u.data[0] = md_type;
438 v4l2_event_queue(asd->subdev.devnode, &event);
441 static void atomisp_reset_event(struct atomisp_sub_device *asd)
443 struct v4l2_event event = {0};
445 event.type = V4L2_EVENT_ATOMISP_CSS_RESET;
447 v4l2_event_queue(asd->subdev.devnode, &event);
450 static void print_csi_rx_errors(enum mipi_port_id port,
451 struct atomisp_device *isp)
455 atomisp_css_rx_get_irq_info(port, &infos);
457 dev_err(isp->dev, "CSI Receiver port %d errors:\n", port);
458 if (infos & IA_CSS_RX_IRQ_INFO_BUFFER_OVERRUN)
459 dev_err(isp->dev, " buffer overrun");
460 if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT)
461 dev_err(isp->dev, " start-of-transmission error");
462 if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT_SYNC)
463 dev_err(isp->dev, " start-of-transmission sync error");
464 if (infos & IA_CSS_RX_IRQ_INFO_ERR_CONTROL)
465 dev_err(isp->dev, " control error");
466 if (infos & IA_CSS_RX_IRQ_INFO_ERR_ECC_DOUBLE)
467 dev_err(isp->dev, " 2 or more ECC errors");
468 if (infos & IA_CSS_RX_IRQ_INFO_ERR_CRC)
469 dev_err(isp->dev, " CRC mismatch");
470 if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ID)
471 dev_err(isp->dev, " unknown error");
472 if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_SYNC)
473 dev_err(isp->dev, " frame sync error");
474 if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_DATA)
475 dev_err(isp->dev, " frame data error");
476 if (infos & IA_CSS_RX_IRQ_INFO_ERR_DATA_TIMEOUT)
477 dev_err(isp->dev, " data timeout");
478 if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ESC)
479 dev_err(isp->dev, " unknown escape command entry");
480 if (infos & IA_CSS_RX_IRQ_INFO_ERR_LINE_SYNC)
481 dev_err(isp->dev, " line sync error");
485 static void clear_irq_reg(struct atomisp_device *isp)
487 struct pci_dev *pdev = to_pci_dev(isp->dev);
490 pci_read_config_dword(pdev, PCI_INTERRUPT_CTRL, &msg_ret);
491 msg_ret |= 1 << INTR_IIR;
492 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg_ret);
495 static struct atomisp_sub_device *
496 __get_asd_from_port(struct atomisp_device *isp, enum mipi_port_id port)
500 /* Check which isp subdev to send eof */
501 for (i = 0; i < isp->num_of_streams; i++) {
502 struct atomisp_sub_device *asd = &isp->asd[i];
503 struct camera_mipi_info *mipi_info;
505 mipi_info = atomisp_to_sensor_mipi_info(
506 isp->inputs[asd->input_curr].camera);
508 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED &&
509 __get_mipi_port(isp, mipi_info->port) == port) {
517 /* interrupt handling function*/
518 irqreturn_t atomisp_isr(int irq, void *dev)
520 struct atomisp_device *isp = (struct atomisp_device *)dev;
521 struct atomisp_sub_device *asd;
522 struct atomisp_css_event eof_event;
523 unsigned int irq_infos = 0;
528 spin_lock_irqsave(&isp->lock, flags);
529 if (isp->sw_contex.power_state != ATOM_ISP_POWER_UP ||
530 !isp->css_initialized) {
531 spin_unlock_irqrestore(&isp->lock, flags);
534 err = atomisp_css_irq_translate(isp, &irq_infos);
536 spin_unlock_irqrestore(&isp->lock, flags);
542 if (!atomisp_streaming_count(isp) && !atomisp_is_acc_enabled(isp))
545 for (i = 0; i < isp->num_of_streams; i++) {
548 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
551 * Current SOF only support one stream, so the SOF only valid
552 * either solely one stream is running
554 if (irq_infos & IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF) {
555 atomic_inc(&asd->sof_count);
556 atomisp_sof_event(asd);
558 /* If sequence_temp and sequence are the same
559 * there where no frames lost so we can increase
561 * If not then processing of frame is still in progress
562 * and driver needs to keep old sequence_temp value.
563 * NOTE: There is assumption here that ISP will not
564 * start processing next frame from sensor before old
565 * one is completely done. */
566 if (atomic_read(&asd->sequence) == atomic_read(
567 &asd->sequence_temp))
568 atomic_set(&asd->sequence_temp,
569 atomic_read(&asd->sof_count));
571 if (irq_infos & IA_CSS_IRQ_INFO_EVENTS_READY)
572 atomic_set(&asd->sequence,
573 atomic_read(&asd->sequence_temp));
576 if (irq_infos & IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF) {
577 dev_dbg_ratelimited(isp->dev,
580 irq_infos &= ~IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF;
583 if ((irq_infos & IA_CSS_IRQ_INFO_INPUT_SYSTEM_ERROR) ||
584 (irq_infos & IA_CSS_IRQ_INFO_IF_ERROR)) {
585 /* handle mipi receiver error */
587 enum mipi_port_id port;
589 for (port = MIPI_PORT0_ID; port <= MIPI_PORT2_ID;
591 print_csi_rx_errors(port, isp);
592 atomisp_css_rx_get_irq_info(port, &rx_infos);
593 atomisp_css_rx_clear_irq_info(port, rx_infos);
597 if (irq_infos & IA_CSS_IRQ_INFO_ISYS_EVENTS_READY) {
598 while (ia_css_dequeue_isys_event(&eof_event.event) ==
600 /* EOF Event does not have the css_pipe returned */
601 asd = __get_asd_from_port(isp, eof_event.event.port);
603 dev_err(isp->dev, "%s: ISYS event, but no subdev.event:%d",
604 __func__, eof_event.event.type);
608 atomisp_eof_event(asd, eof_event.event.exp_id);
609 dev_dbg_ratelimited(isp->dev,
610 "%s ISYS event: EOF exp_id %d, asd %d\n",
611 __func__, eof_event.event.exp_id,
615 irq_infos &= ~IA_CSS_IRQ_INFO_ISYS_EVENTS_READY;
620 spin_unlock_irqrestore(&isp->lock, flags);
622 dev_dbg_ratelimited(isp->dev, "irq:0x%x (unhandled)\n", irq_infos);
624 return IRQ_WAKE_THREAD;
627 spin_unlock_irqrestore(&isp->lock, flags);
630 dev_dbg_ratelimited(isp->dev, "irq:0x%x (ignored, as not streaming anymore)\n",
636 void atomisp_clear_css_buffer_counters(struct atomisp_sub_device *asd)
640 memset(asd->s3a_bufs_in_css, 0, sizeof(asd->s3a_bufs_in_css));
641 for (i = 0; i < ATOMISP_INPUT_STREAM_NUM; i++)
642 memset(asd->metadata_bufs_in_css[i], 0,
643 sizeof(asd->metadata_bufs_in_css[i]));
644 asd->dis_bufs_in_css = 0;
645 asd->video_out_capture.buffers_in_css = 0;
646 asd->video_out_vf.buffers_in_css = 0;
647 asd->video_out_preview.buffers_in_css = 0;
648 asd->video_out_video_capture.buffers_in_css = 0;
652 bool atomisp_buffers_queued(struct atomisp_sub_device *asd)
654 return asd->video_out_capture.buffers_in_css ||
655 asd->video_out_vf.buffers_in_css ||
656 asd->video_out_preview.buffers_in_css ||
657 asd->video_out_video_capture.buffers_in_css;
661 bool atomisp_buffers_queued_pipe(struct atomisp_video_pipe *pipe)
663 return pipe->buffers_in_css ? true : false;
666 /* 0x100000 is the start of dmem inside SP */
667 #define SP_DMEM_BASE 0x100000
669 void dump_sp_dmem(struct atomisp_device *isp, unsigned int addr,
672 unsigned int data = 0;
673 unsigned int size32 = DIV_ROUND_UP(size, sizeof(u32));
675 dev_dbg(isp->dev, "atomisp mmio base: %p\n", isp->base);
676 dev_dbg(isp->dev, "%s, addr:0x%x, size: %d, size32: %d\n", __func__,
678 if (size32 * 4 + addr > 0x4000) {
679 dev_err(isp->dev, "illegal size (%d) or addr (0x%x)\n",
683 addr += SP_DMEM_BASE;
686 data = readl(isp->base + addr);
687 dev_dbg(isp->dev, "%s, \t [0x%x]:0x%x\n", __func__, addr, data);
692 static struct videobuf_buffer *atomisp_css_frame_to_vbuf(
693 struct atomisp_video_pipe *pipe, struct ia_css_frame *frame)
695 struct videobuf_vmalloc_memory *vm_mem;
696 struct ia_css_frame *handle;
699 for (i = 0; pipe->capq.bufs[i]; i++) {
700 vm_mem = pipe->capq.bufs[i]->priv;
701 handle = vm_mem->vaddr;
702 if (handle && handle->data == frame->data)
703 return pipe->capq.bufs[i];
709 static void atomisp_flush_video_pipe(struct atomisp_sub_device *asd,
710 struct atomisp_video_pipe *pipe)
712 unsigned long irqflags;
718 for (i = 0; pipe->capq.bufs[i]; i++) {
719 spin_lock_irqsave(&pipe->irq_lock, irqflags);
720 if (pipe->capq.bufs[i]->state == VIDEOBUF_ACTIVE ||
721 pipe->capq.bufs[i]->state == VIDEOBUF_QUEUED) {
722 pipe->capq.bufs[i]->ts = ktime_get_ns();
723 pipe->capq.bufs[i]->field_count =
724 atomic_read(&asd->sequence) << 1;
725 dev_dbg(asd->isp->dev, "release buffers on device %s\n",
727 if (pipe->capq.bufs[i]->state == VIDEOBUF_QUEUED)
728 list_del_init(&pipe->capq.bufs[i]->queue);
729 pipe->capq.bufs[i]->state = VIDEOBUF_ERROR;
730 wake_up(&pipe->capq.bufs[i]->done);
732 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
736 /* Returns queued buffers back to video-core */
737 void atomisp_flush_bufs_and_wakeup(struct atomisp_sub_device *asd)
739 atomisp_flush_video_pipe(asd, &asd->video_out_capture);
740 atomisp_flush_video_pipe(asd, &asd->video_out_vf);
741 atomisp_flush_video_pipe(asd, &asd->video_out_preview);
742 atomisp_flush_video_pipe(asd, &asd->video_out_video_capture);
745 /* clean out the parameters that did not apply */
746 void atomisp_flush_params_queue(struct atomisp_video_pipe *pipe)
748 struct atomisp_css_params_with_list *param;
750 while (!list_empty(&pipe->per_frame_params)) {
751 param = list_entry(pipe->per_frame_params.next,
752 struct atomisp_css_params_with_list, list);
753 list_del(¶m->list);
754 atomisp_free_css_parameters(¶m->params);
759 /* Re-queue per-frame parameters */
760 static void atomisp_recover_params_queue(struct atomisp_video_pipe *pipe)
762 struct atomisp_css_params_with_list *param;
765 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
766 param = pipe->frame_params[i];
768 list_add_tail(¶m->list, &pipe->per_frame_params);
769 pipe->frame_params[i] = NULL;
771 atomisp_handle_parameter_and_buffer(pipe);
774 /* find atomisp_video_pipe with css pipe id, buffer type and atomisp run_mode */
775 static struct atomisp_video_pipe *__atomisp_get_pipe(
776 struct atomisp_sub_device *asd,
777 enum atomisp_input_stream_id stream_id,
778 enum ia_css_pipe_id css_pipe_id,
779 enum ia_css_buffer_type buf_type)
781 struct atomisp_device *isp = asd->isp;
783 if (css_pipe_id == IA_CSS_PIPE_ID_COPY &&
784 isp->inputs[asd->input_curr].camera_caps->
785 sensor[asd->sensor_curr].stream_num > 1) {
787 case ATOMISP_INPUT_STREAM_PREVIEW:
788 return &asd->video_out_preview;
789 case ATOMISP_INPUT_STREAM_POSTVIEW:
790 return &asd->video_out_vf;
791 case ATOMISP_INPUT_STREAM_VIDEO:
792 return &asd->video_out_video_capture;
793 case ATOMISP_INPUT_STREAM_CAPTURE:
795 return &asd->video_out_capture;
799 /* video is same in online as in continuouscapture mode */
800 if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
802 * Disable vf_pp and run CSS in still capture mode. In this
803 * mode, CSS does not cause extra latency with buffering, but
804 * scaling is not available.
806 return &asd->video_out_capture;
807 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
809 * Disable vf_pp and run CSS in video mode. This allows using
810 * ISP scaling but it has one frame delay due to CSS internal
813 return &asd->video_out_video_capture;
814 } else if (css_pipe_id == IA_CSS_PIPE_ID_YUVPP) {
816 * to SOC camera, yuvpp pipe is run for capture/video/SDV/ZSL.
818 if (asd->continuous_mode->val) {
819 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
822 case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
823 return &asd->video_out_video_capture;
824 case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
825 return &asd->video_out_preview;
826 case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
827 return &asd->video_out_capture;
829 return &asd->video_out_vf;
831 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
834 case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
835 return &asd->video_out_preview;
836 case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
837 return &asd->video_out_capture;
839 return &asd->video_out_vf;
842 } else if (buf_type == IA_CSS_BUFFER_TYPE_OUTPUT_FRAME) {
843 switch (asd->run_mode->val) {
844 case ATOMISP_RUN_MODE_VIDEO:
845 return &asd->video_out_video_capture;
846 case ATOMISP_RUN_MODE_PREVIEW:
847 return &asd->video_out_preview;
849 return &asd->video_out_capture;
851 } else if (buf_type == IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
852 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
853 return &asd->video_out_preview;
855 return &asd->video_out_vf;
857 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
858 /* For online video or SDV video pipe. */
859 if (css_pipe_id == IA_CSS_PIPE_ID_VIDEO ||
860 css_pipe_id == IA_CSS_PIPE_ID_COPY ||
861 css_pipe_id == IA_CSS_PIPE_ID_YUVPP) {
862 if (buf_type == IA_CSS_BUFFER_TYPE_OUTPUT_FRAME)
863 return &asd->video_out_video_capture;
864 return &asd->video_out_preview;
866 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
867 /* For online preview or ZSL preview pipe. */
868 if (css_pipe_id == IA_CSS_PIPE_ID_PREVIEW ||
869 css_pipe_id == IA_CSS_PIPE_ID_COPY ||
870 css_pipe_id == IA_CSS_PIPE_ID_YUVPP)
871 return &asd->video_out_preview;
873 /* For capture pipe. */
874 if (buf_type == IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME)
875 return &asd->video_out_vf;
876 return &asd->video_out_capture;
879 enum atomisp_metadata_type
880 atomisp_get_metadata_type(struct atomisp_sub_device *asd,
881 enum ia_css_pipe_id pipe_id)
883 if (!asd->continuous_mode->val)
884 return ATOMISP_MAIN_METADATA;
886 if (pipe_id == IA_CSS_PIPE_ID_CAPTURE) /* online capture pipe */
887 return ATOMISP_SEC_METADATA;
889 return ATOMISP_MAIN_METADATA;
892 void atomisp_buf_done(struct atomisp_sub_device *asd, int error,
893 enum ia_css_buffer_type buf_type,
894 enum ia_css_pipe_id css_pipe_id,
895 bool q_buffers, enum atomisp_input_stream_id stream_id)
897 struct videobuf_buffer *vb = NULL;
898 struct atomisp_video_pipe *pipe = NULL;
899 struct atomisp_css_buffer buffer;
900 bool requeue = false;
902 unsigned long irqflags;
903 struct ia_css_frame *frame = NULL;
904 struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf_tmp;
905 struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf_tmp;
906 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf_tmp;
907 enum atomisp_metadata_type md_type;
908 struct atomisp_device *isp = asd->isp;
909 struct v4l2_control ctrl;
910 bool reset_wdt_timer = false;
913 buf_type != IA_CSS_BUFFER_TYPE_METADATA &&
914 buf_type != IA_CSS_BUFFER_TYPE_3A_STATISTICS &&
915 buf_type != IA_CSS_BUFFER_TYPE_DIS_STATISTICS &&
916 buf_type != IA_CSS_BUFFER_TYPE_OUTPUT_FRAME &&
917 buf_type != IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
918 buf_type != IA_CSS_BUFFER_TYPE_RAW_OUTPUT_FRAME &&
919 buf_type != IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
920 buf_type != IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
921 dev_err(isp->dev, "%s, unsupported buffer type: %d\n",
926 memset(&buffer, 0, sizeof(struct atomisp_css_buffer));
927 buffer.css_buffer.type = buf_type;
928 err = atomisp_css_dequeue_buffer(asd, stream_id, css_pipe_id,
932 "atomisp_css_dequeue_buffer failed: 0x%x\n", err);
936 /* need to know the atomisp pipe for frame buffers */
937 pipe = __atomisp_get_pipe(asd, stream_id, css_pipe_id, buf_type);
939 dev_err(isp->dev, "error getting atomisp pipe\n");
944 case IA_CSS_BUFFER_TYPE_3A_STATISTICS:
945 list_for_each_entry_safe(s3a_buf, _s3a_buf_tmp,
946 &asd->s3a_stats_in_css, list) {
947 if (s3a_buf->s3a_data ==
948 buffer.css_buffer.data.stats_3a) {
949 list_del_init(&s3a_buf->list);
950 list_add_tail(&s3a_buf->list,
951 &asd->s3a_stats_ready);
956 asd->s3a_bufs_in_css[css_pipe_id]--;
957 atomisp_3a_stats_ready_event(asd, buffer.css_buffer.exp_id);
958 dev_dbg(isp->dev, "%s: s3a stat with exp_id %d is ready\n",
959 __func__, s3a_buf->s3a_data->exp_id);
961 case IA_CSS_BUFFER_TYPE_METADATA:
965 md_type = atomisp_get_metadata_type(asd, css_pipe_id);
966 list_for_each_entry_safe(md_buf, _md_buf_tmp,
967 &asd->metadata_in_css[md_type], list) {
968 if (md_buf->metadata ==
969 buffer.css_buffer.data.metadata) {
970 list_del_init(&md_buf->list);
971 list_add_tail(&md_buf->list,
972 &asd->metadata_ready[md_type]);
976 asd->metadata_bufs_in_css[stream_id][css_pipe_id]--;
977 atomisp_metadata_ready_event(asd, md_type);
978 dev_dbg(isp->dev, "%s: metadata with exp_id %d is ready\n",
979 __func__, md_buf->metadata->exp_id);
981 case IA_CSS_BUFFER_TYPE_DIS_STATISTICS:
982 list_for_each_entry_safe(dis_buf, _dis_buf_tmp,
983 &asd->dis_stats_in_css, list) {
984 if (dis_buf->dis_data ==
985 buffer.css_buffer.data.stats_dvs) {
986 spin_lock_irqsave(&asd->dis_stats_lock,
988 list_del_init(&dis_buf->list);
989 list_add(&dis_buf->list, &asd->dis_stats);
990 asd->params.dis_proj_data_valid = true;
991 spin_unlock_irqrestore(&asd->dis_stats_lock,
996 asd->dis_bufs_in_css--;
997 dev_dbg(isp->dev, "%s: dis stat with exp_id %d is ready\n",
998 __func__, dis_buf->dis_data->exp_id);
1000 case IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME:
1001 case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
1003 reset_wdt_timer = true;
1005 pipe->buffers_in_css--;
1006 frame = buffer.css_buffer.data.frame;
1015 * YUVPP doesn't set postview exp_id correctlly in SDV mode.
1016 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
1018 if (IS_BYT && buf_type == IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
1019 asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
1020 frame->exp_id = (asd->postview_exp_id++) %
1021 (ATOMISP_MAX_EXP_ID + 1);
1023 dev_dbg(isp->dev, "%s: vf frame with exp_id %d is ready\n",
1024 __func__, frame->exp_id);
1025 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
1026 if (frame->flash_state
1027 == IA_CSS_FRAME_FLASH_STATE_PARTIAL)
1028 dev_dbg(isp->dev, "%s thumb partially flashed\n",
1030 else if (frame->flash_state
1031 == IA_CSS_FRAME_FLASH_STATE_FULL)
1032 dev_dbg(isp->dev, "%s thumb completely flashed\n",
1035 dev_dbg(isp->dev, "%s thumb no flash in this frame\n",
1038 vb = atomisp_css_frame_to_vbuf(pipe, frame);
1041 pipe->frame_config_id[vb->i] = frame->isp_config_id;
1042 if (css_pipe_id == IA_CSS_PIPE_ID_CAPTURE &&
1043 asd->pending_capture_request > 0) {
1044 err = atomisp_css_offline_capture_configure(asd,
1045 asd->params.offline_parm.num_captures,
1046 asd->params.offline_parm.skip_frames,
1047 asd->params.offline_parm.offset);
1049 asd->pending_capture_request--;
1051 dev_dbg(isp->dev, "Trigger capture again for new buffer. err=%d\n",
1055 case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
1056 case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
1058 reset_wdt_timer = true;
1060 pipe->buffers_in_css--;
1061 frame = buffer.css_buffer.data.frame;
1071 * YUVPP doesn't set preview exp_id correctlly in ZSL mode.
1072 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
1074 if (IS_BYT && buf_type == IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
1075 asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
1076 frame->exp_id = (asd->preview_exp_id++) %
1077 (ATOMISP_MAX_EXP_ID + 1);
1079 dev_dbg(isp->dev, "%s: main frame with exp_id %d is ready\n",
1080 __func__, frame->exp_id);
1081 vb = atomisp_css_frame_to_vbuf(pipe, frame);
1087 /* free the parameters */
1088 if (pipe->frame_params[vb->i]) {
1089 if (asd->params.dvs_6axis ==
1090 pipe->frame_params[vb->i]->params.dvs_6axis)
1091 asd->params.dvs_6axis = NULL;
1092 atomisp_free_css_parameters(
1093 &pipe->frame_params[vb->i]->params);
1094 kvfree(pipe->frame_params[vb->i]);
1095 pipe->frame_params[vb->i] = NULL;
1098 pipe->frame_config_id[vb->i] = frame->isp_config_id;
1099 ctrl.id = V4L2_CID_FLASH_MODE;
1100 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
1101 if (frame->flash_state
1102 == IA_CSS_FRAME_FLASH_STATE_PARTIAL) {
1103 asd->frame_status[vb->i] =
1104 ATOMISP_FRAME_STATUS_FLASH_PARTIAL;
1105 dev_dbg(isp->dev, "%s partially flashed\n",
1107 } else if (frame->flash_state
1108 == IA_CSS_FRAME_FLASH_STATE_FULL) {
1109 asd->frame_status[vb->i] =
1110 ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
1111 asd->params.num_flash_frames--;
1112 dev_dbg(isp->dev, "%s completely flashed\n",
1115 asd->frame_status[vb->i] =
1116 ATOMISP_FRAME_STATUS_OK;
1118 "%s no flash in this frame\n",
1122 /* Check if flashing sequence is done */
1123 if (asd->frame_status[vb->i] ==
1124 ATOMISP_FRAME_STATUS_FLASH_EXPOSED)
1125 asd->params.flash_state = ATOMISP_FLASH_DONE;
1126 } else if (isp->flash) {
1127 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) ==
1128 0 && ctrl.value == ATOMISP_FLASH_MODE_TORCH) {
1129 ctrl.id = V4L2_CID_FLASH_TORCH_INTENSITY;
1130 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl)
1131 == 0 && ctrl.value > 0) {
1132 asd->frame_status[vb->i] =
1133 ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
1135 asd->frame_status[vb->i] =
1136 ATOMISP_FRAME_STATUS_OK;
1139 asd->frame_status[vb->i] =
1140 ATOMISP_FRAME_STATUS_OK;
1143 asd->frame_status[vb->i] = ATOMISP_FRAME_STATUS_OK;
1146 asd->params.last_frame_status = asd->frame_status[vb->i];
1148 if (asd->continuous_mode->val) {
1149 if (css_pipe_id == IA_CSS_PIPE_ID_PREVIEW ||
1150 css_pipe_id == IA_CSS_PIPE_ID_VIDEO) {
1151 asd->latest_preview_exp_id = frame->exp_id;
1152 } else if (css_pipe_id ==
1153 IA_CSS_PIPE_ID_CAPTURE) {
1154 if (asd->run_mode->val ==
1155 ATOMISP_RUN_MODE_VIDEO)
1156 dev_dbg(isp->dev, "SDV capture raw buffer id: %u\n",
1159 dev_dbg(isp->dev, "ZSL capture raw buffer id: %u\n",
1164 * Only after enabled the raw buffer lock
1165 * and in continuous mode.
1166 * in preview/video pipe, each buffer will
1167 * be locked automatically, so record it here.
1169 if (((css_pipe_id == IA_CSS_PIPE_ID_PREVIEW) ||
1170 (css_pipe_id == IA_CSS_PIPE_ID_VIDEO)) &&
1171 asd->enable_raw_buffer_lock->val &&
1172 asd->continuous_mode->val) {
1173 atomisp_set_raw_buffer_bitmap(asd, frame->exp_id);
1174 WARN_ON(frame->exp_id > ATOMISP_MAX_EXP_ID);
1177 if (asd->params.css_update_params_needed) {
1178 atomisp_apply_css_parameters(asd,
1179 &asd->params.css_param);
1180 if (asd->params.css_param.update_flag.dz_config)
1181 asd->params.config.dz_config = &asd->params.css_param.dz_config;
1182 /* New global dvs 6axis config should be blocked
1183 * here if there's a buffer with per-frame parameters
1184 * pending in CSS frame buffer queue.
1185 * This is to aviod zooming vibration since global
1186 * parameters take effect immediately while
1187 * per-frame parameters are taken after previous
1188 * buffers in CSS got processed.
1190 if (asd->params.dvs_6axis)
1191 atomisp_css_set_dvs_6axis(asd,
1192 asd->params.dvs_6axis);
1194 asd->params.css_update_params_needed = false;
1195 /* The update flag should not be cleaned here
1196 * since it is still going to be used to make up
1197 * following per-frame parameters.
1198 * This will introduce more copy work since each
1199 * time when updating global parameters, the whole
1200 * parameter set are applied.
1201 * FIXME: A new set of parameter copy functions can
1202 * be added to make up per-frame parameters based on
1203 * solid structures stored in asd->params.css_param
1204 * instead of using shadow pointers in update flag.
1206 atomisp_css_update_isp_params(asd);
1213 vb->ts = ktime_get_ns();
1214 vb->field_count = atomic_read(&asd->sequence) << 1;
1215 /*mark videobuffer done for dequeue*/
1216 spin_lock_irqsave(&pipe->irq_lock, irqflags);
1217 vb->state = !error ? VIDEOBUF_DONE : VIDEOBUF_ERROR;
1218 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
1221 * Frame capture done, wake up any process block on
1222 * current active buffer
1223 * possibly hold by videobuf_dqbuf()
1228 atomic_set(&pipe->wdt_count, 0);
1231 * Requeue should only be done for 3a and dis buffers.
1232 * Queue/dequeue order will change if driver recycles image buffers.
1235 err = atomisp_css_queue_buffer(asd,
1236 stream_id, css_pipe_id,
1239 dev_err(isp->dev, "%s, q to css fails: %d\n",
1243 if (!error && q_buffers)
1244 atomisp_qbuffers_to_css(asd);
1247 /* If there are no buffers queued then
1248 * delete wdt timer. */
1249 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1251 if (!atomisp_buffers_queued_pipe(pipe))
1252 atomisp_wdt_stop_pipe(pipe, false);
1253 else if (reset_wdt_timer)
1254 /* SOF irq should not reset wdt timer. */
1255 atomisp_wdt_refresh_pipe(pipe,
1256 ATOMISP_WDT_KEEP_CURRENT_DELAY);
1260 void atomisp_delayed_init_work(struct work_struct *work)
1262 struct atomisp_sub_device *asd = container_of(work,
1263 struct atomisp_sub_device,
1266 * to SOC camera, use yuvpp pipe and no support continuous mode.
1268 if (!ATOMISP_USE_YUVPP(asd)) {
1269 struct v4l2_event event = {0};
1270 struct ia_css_stream *stream;
1272 stream = asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
1275 if (ia_css_alloc_continuous_frame_remain(stream))
1278 ia_css_update_continuous_frames(stream);
1280 event.type = V4L2_EVENT_ATOMISP_RAW_BUFFERS_ALLOC_DONE;
1281 v4l2_event_queue(asd->subdev.devnode, &event);
1284 /* signal streamon after delayed init is done */
1285 asd->delayed_init = ATOMISP_DELAYED_INIT_DONE;
1286 complete(&asd->init_done);
1289 static void __atomisp_css_recover(struct atomisp_device *isp, bool isp_timeout)
1291 struct pci_dev *pdev = to_pci_dev(isp->dev);
1292 enum ia_css_pipe_id css_pipe_id;
1293 bool stream_restart[MAX_STREAM_NUM] = {0};
1294 bool depth_mode = false;
1295 int i, ret, depth_cnt = 0;
1297 if (!isp->sw_contex.file_input)
1298 atomisp_css_irq_enable(isp,
1299 IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
1301 BUG_ON(isp->num_of_streams > MAX_STREAM_NUM);
1303 for (i = 0; i < isp->num_of_streams; i++) {
1304 struct atomisp_sub_device *asd = &isp->asd[i];
1305 struct ia_css_pipeline *acc_pipeline;
1306 struct ia_css_pipe *acc_pipe = NULL;
1308 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED &&
1309 !asd->stream_prepared)
1313 * AtomISP::waitStageUpdate is blocked when WDT happens.
1314 * By calling acc_done() for all loaded fw_handles,
1315 * HAL will be unblocked.
1317 acc_pipe = asd->stream_env[i].pipes[IA_CSS_PIPE_ID_ACC];
1319 acc_pipeline = ia_css_pipe_get_pipeline(acc_pipe);
1321 struct ia_css_pipeline_stage *stage;
1323 for (stage = acc_pipeline->stages; stage;
1324 stage = stage->next) {
1325 const struct ia_css_fw_info *fw;
1327 fw = stage->firmware;
1328 atomisp_acc_done(asd, fw->handle);
1335 if (asd->delayed_init == ATOMISP_DELAYED_INIT_QUEUED)
1336 cancel_work_sync(&asd->delayed_init_work);
1338 complete(&asd->init_done);
1339 asd->delayed_init = ATOMISP_DELAYED_INIT_NOT_QUEUED;
1341 stream_restart[asd->index] = true;
1343 asd->streaming = ATOMISP_DEVICE_STREAMING_STOPPING;
1345 /* stream off sensor */
1346 ret = v4l2_subdev_call(
1347 isp->inputs[asd->input_curr].
1348 camera, video, s_stream, 0);
1351 "can't stop streaming on sensor!\n");
1353 atomisp_acc_unload_extensions(asd);
1355 atomisp_clear_css_buffer_counters(asd);
1357 css_pipe_id = atomisp_get_css_pipe_id(asd);
1358 atomisp_css_stop(asd, css_pipe_id, true);
1360 asd->streaming = ATOMISP_DEVICE_STREAMING_DISABLED;
1362 asd->preview_exp_id = 1;
1363 asd->postview_exp_id = 1;
1364 /* notify HAL the CSS reset */
1366 "send reset event to %s\n", asd->subdev.devnode->name);
1367 atomisp_reset_event(asd);
1371 disable_isp_irq(hrt_isp_css_irq_sp);
1372 clear_isp_irq(hrt_isp_css_irq_sp);
1374 /* Set the SRSE to 3 before resetting */
1375 pci_write_config_dword(pdev, PCI_I_CONTROL,
1376 isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
1378 /* reset ISP and restore its state */
1379 isp->isp_timeout = true;
1381 isp->isp_timeout = false;
1384 for (i = 0; i < isp->num_of_streams; i++) {
1385 if (isp->asd[i].depth_mode->val)
1390 for (i = 0; i < isp->num_of_streams; i++) {
1391 struct atomisp_sub_device *asd = &isp->asd[i];
1393 if (!stream_restart[i])
1396 if (isp->inputs[asd->input_curr].type != FILE_INPUT)
1397 atomisp_css_input_set_mode(asd,
1398 IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
1400 css_pipe_id = atomisp_get_css_pipe_id(asd);
1401 if (atomisp_css_start(asd, css_pipe_id, true))
1403 "start SP failed, so do not set streaming to be enable!\n");
1405 asd->streaming = ATOMISP_DEVICE_STREAMING_ENABLED;
1407 atomisp_csi2_configure(asd);
1410 if (!isp->sw_contex.file_input) {
1411 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
1412 atomisp_css_valid_sof(isp));
1414 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, true) < 0)
1415 dev_dbg(isp->dev, "DFS auto failed while recovering!\n");
1417 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_MAX, true) < 0)
1418 dev_dbg(isp->dev, "DFS max failed while recovering!\n");
1421 for (i = 0; i < isp->num_of_streams; i++) {
1422 struct atomisp_sub_device *asd;
1426 if (!stream_restart[i])
1429 if (asd->continuous_mode->val &&
1430 asd->delayed_init == ATOMISP_DELAYED_INIT_NOT_QUEUED) {
1431 reinit_completion(&asd->init_done);
1432 asd->delayed_init = ATOMISP_DELAYED_INIT_QUEUED;
1433 queue_work(asd->delayed_init_workq,
1434 &asd->delayed_init_work);
1437 * dequeueing buffers is not needed. CSS will recycle
1438 * buffers that it has.
1440 atomisp_flush_bufs_and_wakeup(asd);
1442 /* Requeue unprocessed per-frame parameters. */
1443 atomisp_recover_params_queue(&asd->video_out_capture);
1444 atomisp_recover_params_queue(&asd->video_out_preview);
1445 atomisp_recover_params_queue(&asd->video_out_video_capture);
1447 if ((asd->depth_mode->val) &&
1448 (depth_cnt == ATOMISP_DEPTH_SENSOR_STREAMON_COUNT)) {
1453 ret = v4l2_subdev_call(
1454 isp->inputs[asd->input_curr].camera, video,
1458 "can't start streaming on sensor!\n");
1462 if (atomisp_stream_on_master_slave_sensor(isp, true))
1464 "master slave sensor stream on failed!\n");
1468 void atomisp_wdt_work(struct work_struct *work)
1470 struct atomisp_device *isp = container_of(work, struct atomisp_device,
1473 unsigned int pipe_wdt_cnt[MAX_STREAM_NUM][4] = { {0} };
1474 bool css_recover = true;
1476 rt_mutex_lock(&isp->mutex);
1477 if (!atomisp_streaming_count(isp)) {
1478 atomic_set(&isp->wdt_work_queued, 0);
1479 rt_mutex_unlock(&isp->mutex);
1484 dev_err(isp->dev, "timeout %d of %d\n",
1485 atomic_read(&isp->wdt_count) + 1,
1486 ATOMISP_ISP_MAX_TIMEOUT_COUNT);
1488 for (i = 0; i < isp->num_of_streams; i++) {
1489 struct atomisp_sub_device *asd = &isp->asd[i];
1491 pipe_wdt_cnt[i][0] +=
1492 atomic_read(&asd->video_out_capture.wdt_count);
1493 pipe_wdt_cnt[i][1] +=
1494 atomic_read(&asd->video_out_vf.wdt_count);
1495 pipe_wdt_cnt[i][2] +=
1496 atomic_read(&asd->video_out_preview.wdt_count);
1497 pipe_wdt_cnt[i][3] +=
1498 atomic_read(&asd->video_out_video_capture.wdt_count);
1500 (pipe_wdt_cnt[i][0] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1501 pipe_wdt_cnt[i][1] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1502 pipe_wdt_cnt[i][2] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1503 pipe_wdt_cnt[i][3] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT)
1506 "pipe on asd%d timeout cnt: (%d, %d, %d, %d) of %d, recover = %d\n",
1507 asd->index, pipe_wdt_cnt[i][0], pipe_wdt_cnt[i][1],
1508 pipe_wdt_cnt[i][2], pipe_wdt_cnt[i][3],
1509 ATOMISP_ISP_MAX_TIMEOUT_COUNT, css_recover);
1514 ia_css_debug_dump_sp_sw_debug_info();
1515 ia_css_debug_dump_debug_info(__func__);
1516 for (i = 0; i < isp->num_of_streams; i++) {
1517 struct atomisp_sub_device *asd = &isp->asd[i];
1519 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1521 dev_err(isp->dev, "%s, vdev %s buffers in css: %d\n",
1523 asd->video_out_capture.vdev.name,
1524 asd->video_out_capture.
1527 "%s, vdev %s buffers in css: %d\n",
1529 asd->video_out_vf.vdev.name,
1533 "%s, vdev %s buffers in css: %d\n",
1535 asd->video_out_preview.vdev.name,
1536 asd->video_out_preview.
1539 "%s, vdev %s buffers in css: %d\n",
1541 asd->video_out_video_capture.vdev.name,
1542 asd->video_out_video_capture.
1545 "%s, s3a buffers in css preview pipe:%d\n",
1547 asd->s3a_bufs_in_css[IA_CSS_PIPE_ID_PREVIEW]);
1549 "%s, s3a buffers in css capture pipe:%d\n",
1551 asd->s3a_bufs_in_css[IA_CSS_PIPE_ID_CAPTURE]);
1553 "%s, s3a buffers in css video pipe:%d\n",
1555 asd->s3a_bufs_in_css[IA_CSS_PIPE_ID_VIDEO]);
1557 "%s, dis buffers in css: %d\n",
1558 __func__, asd->dis_bufs_in_css);
1560 "%s, metadata buffers in css preview pipe:%d\n",
1562 asd->metadata_bufs_in_css
1563 [ATOMISP_INPUT_STREAM_GENERAL]
1564 [IA_CSS_PIPE_ID_PREVIEW]);
1566 "%s, metadata buffers in css capture pipe:%d\n",
1568 asd->metadata_bufs_in_css
1569 [ATOMISP_INPUT_STREAM_GENERAL]
1570 [IA_CSS_PIPE_ID_CAPTURE]);
1572 "%s, metadata buffers in css video pipe:%d\n",
1574 asd->metadata_bufs_in_css
1575 [ATOMISP_INPUT_STREAM_GENERAL]
1576 [IA_CSS_PIPE_ID_VIDEO]);
1577 if (asd->enable_raw_buffer_lock->val) {
1580 dev_err(isp->dev, "%s, raw_buffer_locked_count %d\n",
1581 __func__, asd->raw_buffer_locked_count);
1582 for (j = 0; j <= ATOMISP_MAX_EXP_ID / 32; j++)
1583 dev_err(isp->dev, "%s, raw_buffer_bitmap[%d]: 0x%x\n",
1585 asd->raw_buffer_bitmap[j]);
1589 /*sh_css_dump_sp_state();*/
1590 /*sh_css_dump_isp_state();*/
1592 for (i = 0; i < isp->num_of_streams; i++) {
1593 struct atomisp_sub_device *asd = &isp->asd[i];
1595 if (asd->streaming ==
1596 ATOMISP_DEVICE_STREAMING_ENABLED) {
1597 atomisp_clear_css_buffer_counters(asd);
1598 atomisp_flush_bufs_and_wakeup(asd);
1599 complete(&asd->init_done);
1602 atomisp_wdt_stop(asd, false);
1606 atomic_set(&isp->wdt_count, 0);
1608 isp->isp_fatal_error = true;
1609 atomic_set(&isp->wdt_work_queued, 0);
1611 rt_mutex_unlock(&isp->mutex);
1616 __atomisp_css_recover(isp, true);
1618 for (i = 0; i < isp->num_of_streams; i++) {
1619 struct atomisp_sub_device *asd = &isp->asd[i];
1621 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1624 atomisp_wdt_refresh(asd,
1625 isp->sw_contex.file_input ?
1626 ATOMISP_ISP_FILE_TIMEOUT_DURATION :
1627 ATOMISP_ISP_TIMEOUT_DURATION);
1631 dev_err(isp->dev, "timeout recovery handling done\n");
1632 atomic_set(&isp->wdt_work_queued, 0);
1634 rt_mutex_unlock(&isp->mutex);
1637 void atomisp_css_flush(struct atomisp_device *isp)
1641 if (!atomisp_streaming_count(isp))
1645 for (i = 0; i < isp->num_of_streams; i++) {
1646 struct atomisp_sub_device *asd = &isp->asd[i];
1648 atomisp_wdt_stop(asd, true);
1652 __atomisp_css_recover(isp, false);
1654 for (i = 0; i < isp->num_of_streams; i++) {
1655 struct atomisp_sub_device *asd = &isp->asd[i];
1657 if (asd->streaming !=
1658 ATOMISP_DEVICE_STREAMING_ENABLED)
1661 atomisp_wdt_refresh(asd,
1662 isp->sw_contex.file_input ?
1663 ATOMISP_ISP_FILE_TIMEOUT_DURATION :
1664 ATOMISP_ISP_TIMEOUT_DURATION);
1666 dev_dbg(isp->dev, "atomisp css flush done\n");
1669 void atomisp_wdt(struct timer_list *t)
1671 struct atomisp_sub_device *asd;
1672 struct atomisp_device *isp;
1675 asd = from_timer(asd, t, wdt);
1678 struct atomisp_video_pipe *pipe = from_timer(pipe, t, wdt);
1683 atomic_inc(&pipe->wdt_count);
1685 "[WARNING]asd %d pipe %s ISP timeout %d!\n",
1686 asd->index, pipe->vdev.name,
1687 atomic_read(&pipe->wdt_count));
1690 if (atomic_read(&isp->wdt_work_queued)) {
1691 dev_dbg(isp->dev, "ISP watchdog was put into workqueue\n");
1694 atomic_set(&isp->wdt_work_queued, 1);
1695 queue_work(isp->wdt_work_queue, &isp->wdt_work);
1699 void atomisp_wdt_start(struct atomisp_sub_device *asd)
1701 atomisp_wdt_refresh(asd, ATOMISP_ISP_TIMEOUT_DURATION);
1705 void atomisp_wdt_refresh_pipe(struct atomisp_video_pipe *pipe,
1711 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
1712 __func__, pipe->vdev.name);
1716 if (delay != ATOMISP_WDT_KEEP_CURRENT_DELAY)
1717 pipe->wdt_duration = delay;
1719 next = jiffies + pipe->wdt_duration;
1721 /* Override next if it has been pushed beyon the "next" time */
1722 if (atomisp_is_wdt_running(pipe) && time_after(pipe->wdt_expires, next))
1723 next = pipe->wdt_expires;
1725 pipe->wdt_expires = next;
1727 if (atomisp_is_wdt_running(pipe))
1728 dev_dbg(pipe->asd->isp->dev, "WDT will hit after %d ms (%s)\n",
1729 ((int)(next - jiffies) * 1000 / HZ), pipe->vdev.name);
1731 dev_dbg(pipe->asd->isp->dev, "WDT starts with %d ms period (%s)\n",
1732 ((int)(next - jiffies) * 1000 / HZ), pipe->vdev.name);
1734 mod_timer(&pipe->wdt, next);
1737 void atomisp_wdt_refresh(struct atomisp_sub_device *asd, unsigned int delay)
1742 if (delay != ATOMISP_WDT_KEEP_CURRENT_DELAY)
1743 asd->wdt_duration = delay;
1745 next = jiffies + asd->wdt_duration;
1747 /* Override next if it has been pushed beyon the "next" time */
1748 if (atomisp_is_wdt_running(asd) && time_after(asd->wdt_expires, next))
1749 next = asd->wdt_expires;
1751 asd->wdt_expires = next;
1753 if (atomisp_is_wdt_running(asd))
1754 dev_dbg(asd->isp->dev, "WDT will hit after %d ms\n",
1755 ((int)(next - jiffies) * 1000 / HZ));
1757 dev_dbg(asd->isp->dev, "WDT starts with %d ms period\n",
1758 ((int)(next - jiffies) * 1000 / HZ));
1760 mod_timer(&asd->wdt, next);
1761 atomic_set(&asd->isp->wdt_count, 0);
1763 dev_dbg(asd->isp->dev, "WDT refresh all:\n");
1764 if (atomisp_is_wdt_running(&asd->video_out_capture))
1765 atomisp_wdt_refresh_pipe(&asd->video_out_capture, delay);
1766 if (atomisp_is_wdt_running(&asd->video_out_preview))
1767 atomisp_wdt_refresh_pipe(&asd->video_out_preview, delay);
1768 if (atomisp_is_wdt_running(&asd->video_out_vf))
1769 atomisp_wdt_refresh_pipe(&asd->video_out_vf, delay);
1770 if (atomisp_is_wdt_running(&asd->video_out_video_capture))
1771 atomisp_wdt_refresh_pipe(&asd->video_out_video_capture, delay);
1776 void atomisp_wdt_stop_pipe(struct atomisp_video_pipe *pipe, bool sync)
1779 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
1780 __func__, pipe->vdev.name);
1784 if (!atomisp_is_wdt_running(pipe))
1787 dev_dbg(pipe->asd->isp->dev,
1788 "WDT stop asd %d (%s)\n", pipe->asd->index, pipe->vdev.name);
1791 del_timer_sync(&pipe->wdt);
1792 cancel_work_sync(&pipe->asd->isp->wdt_work);
1794 del_timer(&pipe->wdt);
1799 void atomisp_wdt_start_pipe(struct atomisp_video_pipe *pipe)
1801 atomisp_wdt_refresh_pipe(pipe, ATOMISP_ISP_TIMEOUT_DURATION);
1804 void atomisp_wdt_stop(struct atomisp_sub_device *asd, bool sync)
1806 dev_dbg(asd->isp->dev, "WDT stop:\n");
1810 del_timer_sync(&asd->wdt);
1811 cancel_work_sync(&asd->isp->wdt_work);
1813 del_timer(&asd->wdt);
1816 atomisp_wdt_stop_pipe(&asd->video_out_capture, sync);
1817 atomisp_wdt_stop_pipe(&asd->video_out_preview, sync);
1818 atomisp_wdt_stop_pipe(&asd->video_out_vf, sync);
1819 atomisp_wdt_stop_pipe(&asd->video_out_video_capture, sync);
1823 void atomisp_setup_flash(struct atomisp_sub_device *asd)
1825 struct atomisp_device *isp = asd->isp;
1826 struct v4l2_control ctrl;
1831 if (asd->params.flash_state != ATOMISP_FLASH_REQUESTED &&
1832 asd->params.flash_state != ATOMISP_FLASH_DONE)
1835 if (asd->params.num_flash_frames) {
1836 /* make sure the timeout is set before setting flash mode */
1837 ctrl.id = V4L2_CID_FLASH_TIMEOUT;
1838 ctrl.value = FLASH_TIMEOUT;
1840 if (v4l2_s_ctrl(NULL, isp->flash->ctrl_handler, &ctrl)) {
1841 dev_err(isp->dev, "flash timeout configure failed\n");
1845 ia_css_stream_request_flash(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream);
1847 asd->params.flash_state = ATOMISP_FLASH_ONGOING;
1849 asd->params.flash_state = ATOMISP_FLASH_IDLE;
1853 irqreturn_t atomisp_isr_thread(int irq, void *isp_ptr)
1855 struct atomisp_device *isp = isp_ptr;
1856 unsigned long flags;
1857 bool frame_done_found[MAX_STREAM_NUM] = {0};
1858 bool css_pipe_done[MAX_STREAM_NUM] = {0};
1860 struct atomisp_sub_device *asd;
1862 dev_dbg(isp->dev, ">%s\n", __func__);
1864 spin_lock_irqsave(&isp->lock, flags);
1866 if (!atomisp_streaming_count(isp) && !atomisp_is_acc_enabled(isp)) {
1867 spin_unlock_irqrestore(&isp->lock, flags);
1871 spin_unlock_irqrestore(&isp->lock, flags);
1874 * The standard CSS2.0 API tells the following calling sequence of
1875 * dequeue ready buffers:
1876 * while (ia_css_dequeue_psys_event(...)) {
1877 * switch (event.type) {
1879 * ia_css_pipe_dequeue_buffer()
1882 * That is, dequeue event and buffer are one after another.
1884 * But the following implementation is to first deuque all the event
1885 * to a FIFO, then process the event in the FIFO.
1886 * This will not have issue in single stream mode, but it do have some
1887 * issue in multiple stream case. The issue is that
1888 * ia_css_pipe_dequeue_buffer() will not return the corrent buffer in
1891 * This is due to ia_css_pipe_dequeue_buffer() does not take the
1892 * ia_css_pipe parameter.
1895 * For CSS2.0: we change the way to not dequeue all the event at one
1896 * time, instead, dequue one and process one, then another
1898 rt_mutex_lock(&isp->mutex);
1899 if (atomisp_css_isr_thread(isp, frame_done_found, css_pipe_done))
1902 for (i = 0; i < isp->num_of_streams; i++) {
1904 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1906 atomisp_setup_flash(asd);
1909 rt_mutex_unlock(&isp->mutex);
1910 for (i = 0; i < isp->num_of_streams; i++) {
1912 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED
1913 && css_pipe_done[asd->index]
1914 && isp->sw_contex.file_input)
1915 v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1916 video, s_stream, 1);
1917 /* FIXME! FIX ACC implementation */
1918 if (asd->acc.pipeline && css_pipe_done[asd->index])
1919 atomisp_css_acc_done(asd);
1921 dev_dbg(isp->dev, "<%s\n", __func__);
1927 * Get internal fmt according to V4L2 fmt
1929 static enum ia_css_frame_format
1930 v4l2_fmt_to_sh_fmt(u32 fmt)
1933 case V4L2_PIX_FMT_YUV420:
1934 return IA_CSS_FRAME_FORMAT_YUV420;
1935 case V4L2_PIX_FMT_YVU420:
1936 return IA_CSS_FRAME_FORMAT_YV12;
1937 case V4L2_PIX_FMT_YUV422P:
1938 return IA_CSS_FRAME_FORMAT_YUV422;
1939 case V4L2_PIX_FMT_YUV444:
1940 return IA_CSS_FRAME_FORMAT_YUV444;
1941 case V4L2_PIX_FMT_NV12:
1942 return IA_CSS_FRAME_FORMAT_NV12;
1943 case V4L2_PIX_FMT_NV21:
1944 return IA_CSS_FRAME_FORMAT_NV21;
1945 case V4L2_PIX_FMT_NV16:
1946 return IA_CSS_FRAME_FORMAT_NV16;
1947 case V4L2_PIX_FMT_NV61:
1948 return IA_CSS_FRAME_FORMAT_NV61;
1949 case V4L2_PIX_FMT_UYVY:
1950 return IA_CSS_FRAME_FORMAT_UYVY;
1951 case V4L2_PIX_FMT_YUYV:
1952 return IA_CSS_FRAME_FORMAT_YUYV;
1953 case V4L2_PIX_FMT_RGB24:
1954 return IA_CSS_FRAME_FORMAT_PLANAR_RGB888;
1955 case V4L2_PIX_FMT_RGB32:
1956 return IA_CSS_FRAME_FORMAT_RGBA888;
1957 case V4L2_PIX_FMT_RGB565:
1958 return IA_CSS_FRAME_FORMAT_RGB565;
1960 case V4L2_PIX_FMT_JPEG:
1961 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1962 return IA_CSS_FRAME_FORMAT_BINARY_8;
1964 case V4L2_PIX_FMT_SBGGR16:
1965 case V4L2_PIX_FMT_SBGGR10:
1966 case V4L2_PIX_FMT_SGBRG10:
1967 case V4L2_PIX_FMT_SGRBG10:
1968 case V4L2_PIX_FMT_SRGGB10:
1969 case V4L2_PIX_FMT_SBGGR12:
1970 case V4L2_PIX_FMT_SGBRG12:
1971 case V4L2_PIX_FMT_SGRBG12:
1972 case V4L2_PIX_FMT_SRGGB12:
1973 case V4L2_PIX_FMT_SBGGR8:
1974 case V4L2_PIX_FMT_SGBRG8:
1975 case V4L2_PIX_FMT_SGRBG8:
1976 case V4L2_PIX_FMT_SRGGB8:
1977 return IA_CSS_FRAME_FORMAT_RAW;
1984 * raw format match between SH format and V4L2 format
1986 static int raw_output_format_match_input(u32 input, u32 output)
1988 if ((input == ATOMISP_INPUT_FORMAT_RAW_12) &&
1989 ((output == V4L2_PIX_FMT_SRGGB12) ||
1990 (output == V4L2_PIX_FMT_SGRBG12) ||
1991 (output == V4L2_PIX_FMT_SBGGR12) ||
1992 (output == V4L2_PIX_FMT_SGBRG12)))
1995 if ((input == ATOMISP_INPUT_FORMAT_RAW_10) &&
1996 ((output == V4L2_PIX_FMT_SRGGB10) ||
1997 (output == V4L2_PIX_FMT_SGRBG10) ||
1998 (output == V4L2_PIX_FMT_SBGGR10) ||
1999 (output == V4L2_PIX_FMT_SGBRG10)))
2002 if ((input == ATOMISP_INPUT_FORMAT_RAW_8) &&
2003 ((output == V4L2_PIX_FMT_SRGGB8) ||
2004 (output == V4L2_PIX_FMT_SGRBG8) ||
2005 (output == V4L2_PIX_FMT_SBGGR8) ||
2006 (output == V4L2_PIX_FMT_SGBRG8)))
2009 if ((input == ATOMISP_INPUT_FORMAT_RAW_16) && (output == V4L2_PIX_FMT_SBGGR16))
2015 u32 atomisp_get_pixel_depth(u32 pixelformat)
2017 switch (pixelformat) {
2018 case V4L2_PIX_FMT_YUV420:
2019 case V4L2_PIX_FMT_NV12:
2020 case V4L2_PIX_FMT_NV21:
2021 case V4L2_PIX_FMT_YVU420:
2023 case V4L2_PIX_FMT_YUV422P:
2024 case V4L2_PIX_FMT_YUYV:
2025 case V4L2_PIX_FMT_UYVY:
2026 case V4L2_PIX_FMT_NV16:
2027 case V4L2_PIX_FMT_NV61:
2028 case V4L2_PIX_FMT_RGB565:
2029 case V4L2_PIX_FMT_SBGGR16:
2030 case V4L2_PIX_FMT_SBGGR12:
2031 case V4L2_PIX_FMT_SGBRG12:
2032 case V4L2_PIX_FMT_SGRBG12:
2033 case V4L2_PIX_FMT_SRGGB12:
2034 case V4L2_PIX_FMT_SBGGR10:
2035 case V4L2_PIX_FMT_SGBRG10:
2036 case V4L2_PIX_FMT_SGRBG10:
2037 case V4L2_PIX_FMT_SRGGB10:
2039 case V4L2_PIX_FMT_RGB24:
2040 case V4L2_PIX_FMT_YUV444:
2042 case V4L2_PIX_FMT_RGB32:
2044 case V4L2_PIX_FMT_JPEG:
2045 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
2046 case V4L2_PIX_FMT_SBGGR8:
2047 case V4L2_PIX_FMT_SGBRG8:
2048 case V4L2_PIX_FMT_SGRBG8:
2049 case V4L2_PIX_FMT_SRGGB8:
2052 return 8 * 2; /* raw type now */
2056 bool atomisp_is_mbuscode_raw(uint32_t code)
2058 return code >= 0x3000 && code < 0x4000;
2062 * ISP features control function
2066 * Set ISP capture mode based on current settings
2068 static void atomisp_update_capture_mode(struct atomisp_sub_device *asd)
2070 if (asd->params.gdc_cac_en)
2071 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_ADVANCED);
2072 else if (asd->params.low_light)
2073 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_LOW_LIGHT);
2074 else if (asd->video_out_capture.sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
2075 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
2077 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
2081 int atomisp_set_sensor_runmode(struct atomisp_sub_device *asd,
2082 struct atomisp_s_runmode *runmode)
2084 struct atomisp_device *isp = asd->isp;
2085 struct v4l2_ctrl *c;
2088 if (!(runmode && (runmode->mode & RUNMODE_MASK)))
2091 mutex_lock(asd->ctrl_handler.lock);
2092 c = v4l2_ctrl_find(isp->inputs[asd->input_curr].camera->ctrl_handler,
2096 ret = v4l2_ctrl_s_ctrl(c, runmode->mode);
2098 mutex_unlock(asd->ctrl_handler.lock);
2103 * Function to enable/disable lens geometry distortion correction (GDC) and
2104 * chromatic aberration correction (CAC)
2106 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag,
2110 *value = asd->params.gdc_cac_en;
2114 asd->params.gdc_cac_en = !!*value;
2115 if (asd->params.gdc_cac_en) {
2116 asd->params.config.morph_table = asd->params.css_param.morph_table;
2118 asd->params.config.morph_table = NULL;
2120 asd->params.css_update_params_needed = true;
2121 atomisp_update_capture_mode(asd);
2126 * Function to enable/disable low light mode including ANR
2128 int atomisp_low_light(struct atomisp_sub_device *asd, int flag,
2132 *value = asd->params.low_light;
2136 asd->params.low_light = (*value != 0);
2137 atomisp_update_capture_mode(asd);
2142 * Function to enable/disable extra noise reduction (XNR) in low light
2145 int atomisp_xnr(struct atomisp_sub_device *asd, int flag,
2149 *xnr_enable = asd->params.xnr_en;
2153 atomisp_css_capture_enable_xnr(asd, !!*xnr_enable);
2159 * Function to configure bayer noise reduction
2161 int atomisp_nr(struct atomisp_sub_device *asd, int flag,
2162 struct atomisp_nr_config *arg)
2165 /* Get nr config from current setup */
2166 if (atomisp_css_get_nr_config(asd, arg))
2169 /* Set nr config to isp parameters */
2170 memcpy(&asd->params.css_param.nr_config, arg,
2171 sizeof(struct ia_css_nr_config));
2172 asd->params.config.nr_config = &asd->params.css_param.nr_config;
2173 asd->params.css_update_params_needed = true;
2179 * Function to configure temporal noise reduction (TNR)
2181 int atomisp_tnr(struct atomisp_sub_device *asd, int flag,
2182 struct atomisp_tnr_config *config)
2184 /* Get tnr config from current setup */
2186 /* Get tnr config from current setup */
2187 if (atomisp_css_get_tnr_config(asd, config))
2190 /* Set tnr config to isp parameters */
2191 memcpy(&asd->params.css_param.tnr_config, config,
2192 sizeof(struct ia_css_tnr_config));
2193 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
2194 asd->params.css_update_params_needed = true;
2201 * Function to configure black level compensation
2203 int atomisp_black_level(struct atomisp_sub_device *asd, int flag,
2204 struct atomisp_ob_config *config)
2207 /* Get ob config from current setup */
2208 if (atomisp_css_get_ob_config(asd, config))
2211 /* Set ob config to isp parameters */
2212 memcpy(&asd->params.css_param.ob_config, config,
2213 sizeof(struct ia_css_ob_config));
2214 asd->params.config.ob_config = &asd->params.css_param.ob_config;
2215 asd->params.css_update_params_needed = true;
2222 * Function to configure edge enhancement
2224 int atomisp_ee(struct atomisp_sub_device *asd, int flag,
2225 struct atomisp_ee_config *config)
2228 /* Get ee config from current setup */
2229 if (atomisp_css_get_ee_config(asd, config))
2232 /* Set ee config to isp parameters */
2233 memcpy(&asd->params.css_param.ee_config, config,
2234 sizeof(asd->params.css_param.ee_config));
2235 asd->params.config.ee_config = &asd->params.css_param.ee_config;
2236 asd->params.css_update_params_needed = true;
2243 * Function to update Gamma table for gamma, brightness and contrast config
2245 int atomisp_gamma(struct atomisp_sub_device *asd, int flag,
2246 struct atomisp_gamma_table *config)
2249 /* Get gamma table from current setup */
2250 if (atomisp_css_get_gamma_table(asd, config))
2253 /* Set gamma table to isp parameters */
2254 memcpy(&asd->params.css_param.gamma_table, config,
2255 sizeof(asd->params.css_param.gamma_table));
2256 asd->params.config.gamma_table = &asd->params.css_param.gamma_table;
2263 * Function to update Ctc table for Chroma Enhancement
2265 int atomisp_ctc(struct atomisp_sub_device *asd, int flag,
2266 struct atomisp_ctc_table *config)
2269 /* Get ctc table from current setup */
2270 if (atomisp_css_get_ctc_table(asd, config))
2273 /* Set ctc table to isp parameters */
2274 memcpy(&asd->params.css_param.ctc_table, config,
2275 sizeof(asd->params.css_param.ctc_table));
2276 atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table);
2283 * Function to update gamma correction parameters
2285 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag,
2286 struct atomisp_gc_config *config)
2289 /* Get gamma correction params from current setup */
2290 if (atomisp_css_get_gc_config(asd, config))
2293 /* Set gamma correction params to isp parameters */
2294 memcpy(&asd->params.css_param.gc_config, config,
2295 sizeof(asd->params.css_param.gc_config));
2296 asd->params.config.gc_config = &asd->params.css_param.gc_config;
2297 asd->params.css_update_params_needed = true;
2304 * Function to update narrow gamma flag
2306 int atomisp_formats(struct atomisp_sub_device *asd, int flag,
2307 struct atomisp_formats_config *config)
2310 /* Get narrow gamma flag from current setup */
2311 if (atomisp_css_get_formats_config(asd, config))
2314 /* Set narrow gamma flag to isp parameters */
2315 memcpy(&asd->params.css_param.formats_config, config,
2316 sizeof(asd->params.css_param.formats_config));
2317 asd->params.config.formats_config = &asd->params.css_param.formats_config;
2323 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd)
2325 atomisp_free_css_parameters(&asd->params.css_param);
2327 if (asd->raw_output_frame) {
2328 ia_css_frame_free(asd->raw_output_frame);
2329 asd->raw_output_frame = NULL;
2333 static void atomisp_update_grid_info(struct atomisp_sub_device *asd,
2334 enum ia_css_pipe_id pipe_id,
2337 struct atomisp_device *isp = asd->isp;
2339 u16 stream_id = atomisp_source_pad_to_stream_id(asd, source_pad);
2341 if (atomisp_css_get_grid_info(asd, pipe_id, source_pad))
2344 /* We must free all buffers because they no longer match
2346 atomisp_css_free_stat_buffers(asd);
2348 err = atomisp_alloc_css_stat_bufs(asd, stream_id);
2350 dev_err(isp->dev, "stat_buf allocate error\n");
2354 if (atomisp_alloc_3a_output_buf(asd)) {
2355 /* Failure for 3A buffers does not influence DIS buffers */
2356 if (asd->params.s3a_output_bytes != 0) {
2357 /* For SOC sensor happens s3a_output_bytes == 0,
2358 * using if condition to exclude false error log */
2359 dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n");
2364 if (atomisp_alloc_dis_coef_buf(asd)) {
2366 "Failed to allocate memory for DIS statistics\n");
2370 if (atomisp_alloc_metadata_output_buf(asd)) {
2371 dev_err(isp->dev, "Failed to allocate memory for metadata\n");
2378 atomisp_css_free_stat_buffers(asd);
2382 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd,
2383 struct atomisp_grid_info *info)
2385 memcpy(info, &asd->params.curr_grid_info.s3a_grid,
2386 sizeof(struct ia_css_3a_grid_info));
2389 int atomisp_compare_grid(struct atomisp_sub_device *asd,
2390 struct atomisp_grid_info *atomgrid)
2392 struct atomisp_grid_info tmp = {0};
2394 atomisp_curr_user_grid_info(asd, &tmp);
2395 return memcmp(atomgrid, &tmp, sizeof(tmp));
2399 * Function to update Gdc table for gdc
2401 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag,
2402 struct atomisp_morph_table *config)
2406 struct atomisp_device *isp = asd->isp;
2409 /* Get gdc table from current setup */
2410 struct ia_css_morph_table tab = {0};
2412 atomisp_css_get_morph_table(asd, &tab);
2414 config->width = tab.width;
2415 config->height = tab.height;
2417 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
2418 ret = copy_to_user(config->coordinates_x[i],
2419 tab.coordinates_x[i], tab.height *
2420 tab.width * sizeof(*tab.coordinates_x[i]));
2423 "Failed to copy to User for x\n");
2426 ret = copy_to_user(config->coordinates_y[i],
2427 tab.coordinates_y[i], tab.height *
2428 tab.width * sizeof(*tab.coordinates_y[i]));
2431 "Failed to copy to User for y\n");
2436 struct ia_css_morph_table *tab =
2437 asd->params.css_param.morph_table;
2439 /* free first if we have one */
2441 atomisp_css_morph_table_free(tab);
2442 asd->params.css_param.morph_table = NULL;
2445 /* allocate new one */
2446 tab = atomisp_css_morph_table_allocate(config->width,
2450 dev_err(isp->dev, "out of memory\n");
2454 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
2455 ret = copy_from_user(tab->coordinates_x[i],
2456 config->coordinates_x[i],
2457 config->height * config->width *
2458 sizeof(*config->coordinates_x[i]));
2461 "Failed to copy from User for x, ret %d\n",
2463 atomisp_css_morph_table_free(tab);
2466 ret = copy_from_user(tab->coordinates_y[i],
2467 config->coordinates_y[i],
2468 config->height * config->width *
2469 sizeof(*config->coordinates_y[i]));
2472 "Failed to copy from User for y, ret is %d\n",
2474 atomisp_css_morph_table_free(tab);
2478 asd->params.css_param.morph_table = tab;
2479 if (asd->params.gdc_cac_en)
2480 asd->params.config.morph_table = tab;
2486 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag,
2487 struct atomisp_macc_config *config)
2489 struct ia_css_macc_table *macc_table;
2491 switch (config->color_effect) {
2492 case V4L2_COLORFX_NONE:
2493 macc_table = &asd->params.css_param.macc_table;
2495 case V4L2_COLORFX_SKY_BLUE:
2496 macc_table = &blue_macc_table;
2498 case V4L2_COLORFX_GRASS_GREEN:
2499 macc_table = &green_macc_table;
2501 case V4L2_COLORFX_SKIN_WHITEN_LOW:
2502 macc_table = &skin_low_macc_table;
2504 case V4L2_COLORFX_SKIN_WHITEN:
2505 macc_table = &skin_medium_macc_table;
2507 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
2508 macc_table = &skin_high_macc_table;
2515 /* Get macc table from current setup */
2516 memcpy(&config->table, macc_table,
2517 sizeof(struct ia_css_macc_table));
2519 memcpy(macc_table, &config->table,
2520 sizeof(struct ia_css_macc_table));
2521 if (config->color_effect == asd->params.color_effect)
2522 asd->params.config.macc_table = macc_table;
2528 int atomisp_set_dis_vector(struct atomisp_sub_device *asd,
2529 struct atomisp_dis_vector *vector)
2531 atomisp_css_video_set_dis_vector(asd, vector);
2533 asd->params.dis_proj_data_valid = false;
2534 asd->params.css_update_params_needed = true;
2539 * Function to set/get image stablization statistics
2541 int atomisp_get_dis_stat(struct atomisp_sub_device *asd,
2542 struct atomisp_dis_statistics *stats)
2544 return atomisp_css_get_dis_stat(asd, stats);
2548 * Function set camrea_prefiles.xml current sensor pixel array size
2550 int atomisp_set_array_res(struct atomisp_sub_device *asd,
2551 struct atomisp_resolution *config)
2553 dev_dbg(asd->isp->dev, ">%s start\n", __func__);
2555 dev_err(asd->isp->dev, "Set sensor array size is not valid\n");
2559 asd->sensor_array_res.width = config->width;
2560 asd->sensor_array_res.height = config->height;
2565 * Function to get DVS2 BQ resolution settings
2567 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd,
2568 struct atomisp_dvs2_bq_resolutions *bq_res)
2570 struct ia_css_pipe_config *pipe_cfg = NULL;
2571 struct ia_css_stream_config *stream_cfg = NULL;
2572 struct ia_css_stream_input_config *input_config = NULL;
2574 struct ia_css_stream *stream =
2575 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
2577 dev_warn(asd->isp->dev, "stream is not created");
2581 pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2582 .pipe_configs[IA_CSS_PIPE_ID_VIDEO];
2583 stream_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2585 input_config = &stream_cfg->input_config;
2590 /* the GDC output resolution */
2591 bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2;
2592 bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2;
2594 bq_res->envelope_bq.width_bq = 0;
2595 bq_res->envelope_bq.height_bq = 0;
2596 /* the GDC input resolution */
2597 if (!asd->continuous_mode->val) {
2598 bq_res->source_bq.width_bq = bq_res->output_bq.width_bq +
2599 pipe_cfg->dvs_envelope.width / 2;
2600 bq_res->source_bq.height_bq = bq_res->output_bq.height_bq +
2601 pipe_cfg->dvs_envelope.height / 2;
2603 * Bad pixels caused by spatial filter processing
2604 * ISP filter resolution should be given by CSS/FW, but for now
2605 * there is not such API to query, and it is fixed value, so
2608 bq_res->ispfilter_bq.width_bq = 12 / 2;
2609 bq_res->ispfilter_bq.height_bq = 12 / 2;
2610 /* spatial filter shift, always 4 pixels */
2611 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2612 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2614 if (asd->params.video_dis_en) {
2615 bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width
2616 / 2 - bq_res->ispfilter_bq.width_bq;
2617 bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height
2618 / 2 - bq_res->ispfilter_bq.height_bq;
2621 unsigned int w_padding;
2622 unsigned int gdc_effective_input = 0;
2625 * gdc_effective_input = effective_input + envelope
2627 * From the comment and formula in BZ1786,
2628 * we see the source_bq should be:
2629 * effective_input / bayer_ds_ratio
2631 bq_res->source_bq.width_bq =
2632 (input_config->effective_res.width *
2633 pipe_cfg->bayer_ds_out_res.width /
2634 input_config->effective_res.width + 1) / 2;
2635 bq_res->source_bq.height_bq =
2636 (input_config->effective_res.height *
2637 pipe_cfg->bayer_ds_out_res.height /
2638 input_config->effective_res.height + 1) / 2;
2640 if (!asd->params.video_dis_en) {
2642 * We adjust the ispfilter_bq to:
2643 * ispfilter_bq = 128/BDS
2644 * we still need firmware team to provide an offical
2647 bq_res->ispfilter_bq.width_bq = 128 *
2648 pipe_cfg->bayer_ds_out_res.width /
2649 input_config->effective_res.width / 2;
2650 bq_res->ispfilter_bq.height_bq = 128 *
2651 pipe_cfg->bayer_ds_out_res.width /
2652 input_config->effective_res.width / 2;
2654 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2655 /* No additional left padding for ISYS2401 */
2656 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2657 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2660 * For the w_padding and gdc_shift_bq cacluation
2661 * Please see the BZ 1786 and 4358 for more info.
2662 * Just test that this formula can work now,
2663 * but we still have no offical formula.
2665 * w_padding = ceiling(gdc_effective_input
2666 * /128, 1) * 128 - effective_width
2667 * gdc_shift_bq = w_padding/BDS/2 + ispfilter_bq/2
2669 gdc_effective_input =
2670 input_config->effective_res.width +
2671 pipe_cfg->dvs_envelope.width;
2672 w_padding = roundup(gdc_effective_input, 128) -
2673 input_config->effective_res.width;
2674 w_padding = w_padding *
2675 pipe_cfg->bayer_ds_out_res.width /
2676 input_config->effective_res.width + 1;
2677 w_padding = roundup(w_padding / 2, 1);
2679 bq_res->gdc_shift_bq.width_bq = bq_res->ispfilter_bq.width_bq / 2
2681 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2684 unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
2686 bq_res->ispfilter_bq.width_bq = 8 / 2;
2687 bq_res->ispfilter_bq.height_bq = 8 / 2;
2689 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2690 /* No additional left padding for ISYS2401 */
2691 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2692 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2695 roundup(input_config->effective_res.width, 128) -
2696 input_config->effective_res.width;
2699 bq_res->gdc_shift_bq.width_bq = 4 / 2 +
2701 pipe_cfg->bayer_ds_out_res.width /
2702 input_config->effective_res.width + 1) / 2;
2703 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2706 dvs_w = pipe_cfg->bayer_ds_out_res.width -
2707 pipe_cfg->output_info[0].res.width;
2708 dvs_h = pipe_cfg->bayer_ds_out_res.height -
2709 pipe_cfg->output_info[0].res.height;
2710 dvs_w_max = rounddown(
2711 pipe_cfg->output_info[0].res.width / 5,
2712 ATOM_ISP_STEP_WIDTH);
2713 dvs_h_max = rounddown(
2714 pipe_cfg->output_info[0].res.height / 5,
2715 ATOM_ISP_STEP_HEIGHT);
2716 bq_res->envelope_bq.width_bq =
2717 min((dvs_w / 2), (dvs_w_max / 2)) -
2718 bq_res->ispfilter_bq.width_bq;
2719 bq_res->envelope_bq.height_bq =
2720 min((dvs_h / 2), (dvs_h_max / 2)) -
2721 bq_res->ispfilter_bq.height_bq;
2725 dev_dbg(asd->isp->dev,
2726 "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",
2727 bq_res->source_bq.width_bq, bq_res->source_bq.height_bq,
2728 bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq,
2729 bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq,
2730 bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq,
2731 bq_res->output_bq.width_bq, bq_res->output_bq.height_bq);
2736 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd,
2737 struct atomisp_dis_coefficients *coefs)
2739 return atomisp_css_set_dis_coefs(asd, coefs);
2743 * Function to set/get 3A stat from isp
2745 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag,
2746 struct atomisp_3a_statistics *config)
2748 struct atomisp_device *isp = asd->isp;
2749 struct atomisp_s3a_buf *s3a_buf;
2755 /* sanity check to avoid writing into unallocated memory. */
2756 if (asd->params.s3a_output_bytes == 0)
2759 if (atomisp_compare_grid(asd, &config->grid_info) != 0) {
2760 /* If the grid info in the argument differs from the current
2761 grid info, we tell the caller to reset the grid size and
2766 if (list_empty(&asd->s3a_stats_ready)) {
2767 dev_err(isp->dev, "3a statistics is not valid.\n");
2771 s3a_buf = list_entry(asd->s3a_stats_ready.next,
2772 struct atomisp_s3a_buf, list);
2773 if (s3a_buf->s3a_map)
2774 ia_css_translate_3a_statistics(
2775 asd->params.s3a_user_stat, s3a_buf->s3a_map);
2777 ia_css_get_3a_statistics(asd->params.s3a_user_stat,
2780 config->exp_id = s3a_buf->s3a_data->exp_id;
2781 config->isp_config_id = s3a_buf->s3a_data->isp_config_id;
2783 ret = copy_to_user(config->data, asd->params.s3a_user_stat->data,
2784 asd->params.s3a_output_bytes);
2786 dev_err(isp->dev, "copy to user failed: copied %lu bytes\n",
2791 /* Move to free buffer list */
2792 list_del_init(&s3a_buf->list);
2793 list_add_tail(&s3a_buf->list, &asd->s3a_stats);
2794 dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n",
2796 config->exp_id, config->isp_config_id);
2800 int atomisp_get_metadata(struct atomisp_sub_device *asd, int flag,
2801 struct atomisp_metadata *md)
2803 struct atomisp_device *isp = asd->isp;
2804 struct ia_css_stream_info *stream_info;
2805 struct camera_mipi_info *mipi_info;
2806 struct atomisp_metadata_buf *md_buf;
2807 enum atomisp_metadata_type md_type = ATOMISP_MAIN_METADATA;
2813 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2816 /* We always return the resolution and stride even if there is
2817 * no valid metadata. This allows the caller to get the information
2818 * needed to allocate user-space buffers. */
2819 md->width = stream_info->metadata_info.resolution.width;
2820 md->height = stream_info->metadata_info.resolution.height;
2821 md->stride = stream_info->metadata_info.stride;
2823 /* sanity check to avoid writing into unallocated memory.
2824 * This does not return an error because it is a valid way
2825 * for applications to detect that metadata is not enabled. */
2826 if (md->width == 0 || md->height == 0 || !md->data)
2829 /* This is done in the atomisp_buf_done() */
2830 if (list_empty(&asd->metadata_ready[md_type])) {
2831 dev_warn(isp->dev, "Metadata queue is empty now!\n");
2835 mipi_info = atomisp_to_sensor_mipi_info(
2836 isp->inputs[asd->input_curr].camera);
2840 if (mipi_info->metadata_effective_width) {
2841 for (i = 0; i < md->height; i++)
2842 md->effective_width[i] =
2843 mipi_info->metadata_effective_width[i];
2846 md_buf = list_entry(asd->metadata_ready[md_type].next,
2847 struct atomisp_metadata_buf, list);
2848 md->exp_id = md_buf->metadata->exp_id;
2849 if (md_buf->md_vptr) {
2850 ret = copy_to_user(md->data,
2852 stream_info->metadata_info.size);
2854 hmm_load(md_buf->metadata->address,
2855 asd->params.metadata_user[md_type],
2856 stream_info->metadata_info.size);
2858 ret = copy_to_user(md->data,
2859 asd->params.metadata_user[md_type],
2860 stream_info->metadata_info.size);
2863 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2868 list_del_init(&md_buf->list);
2869 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2871 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2872 __func__, md_type, md->exp_id);
2876 int atomisp_get_metadata_by_type(struct atomisp_sub_device *asd, int flag,
2877 struct atomisp_metadata_with_type *md)
2879 struct atomisp_device *isp = asd->isp;
2880 struct ia_css_stream_info *stream_info;
2881 struct camera_mipi_info *mipi_info;
2882 struct atomisp_metadata_buf *md_buf;
2883 enum atomisp_metadata_type md_type;
2889 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2892 /* We always return the resolution and stride even if there is
2893 * no valid metadata. This allows the caller to get the information
2894 * needed to allocate user-space buffers. */
2895 md->width = stream_info->metadata_info.resolution.width;
2896 md->height = stream_info->metadata_info.resolution.height;
2897 md->stride = stream_info->metadata_info.stride;
2899 /* sanity check to avoid writing into unallocated memory.
2900 * This does not return an error because it is a valid way
2901 * for applications to detect that metadata is not enabled. */
2902 if (md->width == 0 || md->height == 0 || !md->data)
2906 if (md_type < 0 || md_type >= ATOMISP_METADATA_TYPE_NUM)
2909 /* This is done in the atomisp_buf_done() */
2910 if (list_empty(&asd->metadata_ready[md_type])) {
2911 dev_warn(isp->dev, "Metadata queue is empty now!\n");
2915 mipi_info = atomisp_to_sensor_mipi_info(
2916 isp->inputs[asd->input_curr].camera);
2920 if (mipi_info->metadata_effective_width) {
2921 for (i = 0; i < md->height; i++)
2922 md->effective_width[i] =
2923 mipi_info->metadata_effective_width[i];
2926 md_buf = list_entry(asd->metadata_ready[md_type].next,
2927 struct atomisp_metadata_buf, list);
2928 md->exp_id = md_buf->metadata->exp_id;
2929 if (md_buf->md_vptr) {
2930 ret = copy_to_user(md->data,
2932 stream_info->metadata_info.size);
2934 hmm_load(md_buf->metadata->address,
2935 asd->params.metadata_user[md_type],
2936 stream_info->metadata_info.size);
2938 ret = copy_to_user(md->data,
2939 asd->params.metadata_user[md_type],
2940 stream_info->metadata_info.size);
2943 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2947 list_del_init(&md_buf->list);
2948 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2950 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2951 __func__, md_type, md->exp_id);
2956 * Function to calculate real zoom region for every pipe
2958 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd,
2959 struct ia_css_dz_config *dz_config,
2960 enum ia_css_pipe_id css_pipe_id)
2963 struct atomisp_stream_env *stream_env =
2964 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL];
2965 struct atomisp_resolution eff_res, out_res;
2966 int w_offset, h_offset;
2968 memset(&eff_res, 0, sizeof(eff_res));
2969 memset(&out_res, 0, sizeof(out_res));
2971 if (dz_config->dx || dz_config->dy)
2974 if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW
2975 && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) {
2976 dev_err(asd->isp->dev, "%s the set pipe no support crop region"
2982 stream_env->stream_config.input_config.effective_res.width;
2984 stream_env->stream_config.input_config.effective_res.height;
2985 if (eff_res.width == 0 || eff_res.height == 0) {
2986 dev_err(asd->isp->dev, "%s err effective resolution"
2991 if (dz_config->zoom_region.resolution.width
2992 == asd->sensor_array_res.width
2993 || dz_config->zoom_region.resolution.height
2994 == asd->sensor_array_res.height) {
2995 /*no need crop region*/
2996 dz_config->zoom_region.origin.x = 0;
2997 dz_config->zoom_region.origin.y = 0;
2998 dz_config->zoom_region.resolution.width = eff_res.width;
2999 dz_config->zoom_region.resolution.height = eff_res.height;
3004 * This is not the correct implementation with Google's definition, due
3005 * to firmware limitation.
3006 * map real crop region base on above calculating base max crop region.
3010 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
3012 / asd->sensor_array_res.width;
3013 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
3015 / asd->sensor_array_res.height;
3016 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
3018 / asd->sensor_array_res.width;
3019 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
3021 / asd->sensor_array_res.height;
3023 * Set same ratio of crop region resolution and current pipe output
3026 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
3027 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
3028 if (out_res.width == 0 || out_res.height == 0) {
3029 dev_err(asd->isp->dev, "%s err current pipe output resolution"
3034 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
3035 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
3036 if (out_res.width == 0 || out_res.height == 0) {
3037 dev_err(asd->isp->dev, "%s err current pipe output resolution"
3042 if (asd->sensor_array_res.width * out_res.height
3043 < out_res.width * asd->sensor_array_res.height) {
3044 h_offset = asd->sensor_array_res.height
3045 - asd->sensor_array_res.width
3046 * out_res.height / out_res.width;
3047 h_offset = h_offset / 2;
3048 if (dz_config->zoom_region.origin.y < h_offset)
3049 dz_config->zoom_region.origin.y = 0;
3051 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset;
3054 w_offset = asd->sensor_array_res.width
3055 - asd->sensor_array_res.height
3056 * out_res.width / out_res.height;
3057 w_offset = w_offset / 2;
3058 if (dz_config->zoom_region.origin.x < w_offset)
3059 dz_config->zoom_region.origin.x = 0;
3061 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset;
3064 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
3066 / (asd->sensor_array_res.width - 2 * w_offset);
3067 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
3069 / (asd->sensor_array_res.height - 2 * h_offset);
3070 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
3072 / (asd->sensor_array_res.width - 2 * w_offset);
3073 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
3075 / (asd->sensor_array_res.height - 2 * h_offset);
3078 if (out_res.width * dz_config->zoom_region.resolution.height
3079 > dz_config->zoom_region.resolution.width * out_res.height) {
3080 dz_config->zoom_region.resolution.height =
3081 dz_config->zoom_region.resolution.width
3082 * out_res.height / out_res.width;
3084 dz_config->zoom_region.resolution.width =
3085 dz_config->zoom_region.resolution.height
3086 * out_res.width / out_res.height;
3088 dev_dbg(asd->isp->dev,
3089 "%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n",
3090 __func__, dz_config->zoom_region.origin.x,
3091 dz_config->zoom_region.origin.y,
3092 dz_config->zoom_region.resolution.width,
3093 dz_config->zoom_region.resolution.height,
3094 eff_res.width, eff_res.height,
3095 asd->sensor_array_res.width,
3096 asd->sensor_array_res.height,
3097 out_res.width, out_res.height);
3099 if ((dz_config->zoom_region.origin.x +
3100 dz_config->zoom_region.resolution.width
3102 (dz_config->zoom_region.origin.y +
3103 dz_config->zoom_region.resolution.height
3111 * Function to check the zoom region whether is effective
3113 static bool atomisp_check_zoom_region(
3114 struct atomisp_sub_device *asd,
3115 struct ia_css_dz_config *dz_config)
3117 struct atomisp_resolution config;
3121 memset(&config, 0, sizeof(struct atomisp_resolution));
3123 if (dz_config->dx && dz_config->dy)
3126 config.width = asd->sensor_array_res.width;
3127 config.height = asd->sensor_array_res.height;
3128 w = dz_config->zoom_region.origin.x +
3129 dz_config->zoom_region.resolution.width;
3130 h = dz_config->zoom_region.origin.y +
3131 dz_config->zoom_region.resolution.height;
3133 if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0)
3136 /* setting error zoom region */
3137 dev_err(asd->isp->dev,
3138 "%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n",
3139 __func__, dz_config->zoom_region.origin.x,
3140 dz_config->zoom_region.origin.y,
3141 dz_config->zoom_region.resolution.width,
3142 dz_config->zoom_region.resolution.height,
3143 config.width, config.height);
3148 void atomisp_apply_css_parameters(
3149 struct atomisp_sub_device *asd,
3150 struct atomisp_css_params *css_param)
3152 if (css_param->update_flag.wb_config)
3153 asd->params.config.wb_config = &css_param->wb_config;
3155 if (css_param->update_flag.ob_config)
3156 asd->params.config.ob_config = &css_param->ob_config;
3158 if (css_param->update_flag.dp_config)
3159 asd->params.config.dp_config = &css_param->dp_config;
3161 if (css_param->update_flag.nr_config)
3162 asd->params.config.nr_config = &css_param->nr_config;
3164 if (css_param->update_flag.ee_config)
3165 asd->params.config.ee_config = &css_param->ee_config;
3167 if (css_param->update_flag.tnr_config)
3168 asd->params.config.tnr_config = &css_param->tnr_config;
3170 if (css_param->update_flag.a3a_config)
3171 asd->params.config.s3a_config = &css_param->s3a_config;
3173 if (css_param->update_flag.ctc_config)
3174 asd->params.config.ctc_config = &css_param->ctc_config;
3176 if (css_param->update_flag.cnr_config)
3177 asd->params.config.cnr_config = &css_param->cnr_config;
3179 if (css_param->update_flag.ecd_config)
3180 asd->params.config.ecd_config = &css_param->ecd_config;
3182 if (css_param->update_flag.ynr_config)
3183 asd->params.config.ynr_config = &css_param->ynr_config;
3185 if (css_param->update_flag.fc_config)
3186 asd->params.config.fc_config = &css_param->fc_config;
3188 if (css_param->update_flag.macc_config)
3189 asd->params.config.macc_config = &css_param->macc_config;
3191 if (css_param->update_flag.aa_config)
3192 asd->params.config.aa_config = &css_param->aa_config;
3194 if (css_param->update_flag.anr_config)
3195 asd->params.config.anr_config = &css_param->anr_config;
3197 if (css_param->update_flag.xnr_config)
3198 asd->params.config.xnr_config = &css_param->xnr_config;
3200 if (css_param->update_flag.yuv2rgb_cc_config)
3201 asd->params.config.yuv2rgb_cc_config = &css_param->yuv2rgb_cc_config;
3203 if (css_param->update_flag.rgb2yuv_cc_config)
3204 asd->params.config.rgb2yuv_cc_config = &css_param->rgb2yuv_cc_config;
3206 if (css_param->update_flag.macc_table)
3207 asd->params.config.macc_table = &css_param->macc_table;
3209 if (css_param->update_flag.xnr_table)
3210 asd->params.config.xnr_table = &css_param->xnr_table;
3212 if (css_param->update_flag.r_gamma_table)
3213 asd->params.config.r_gamma_table = &css_param->r_gamma_table;
3215 if (css_param->update_flag.g_gamma_table)
3216 asd->params.config.g_gamma_table = &css_param->g_gamma_table;
3218 if (css_param->update_flag.b_gamma_table)
3219 asd->params.config.b_gamma_table = &css_param->b_gamma_table;
3221 if (css_param->update_flag.anr_thres)
3222 atomisp_css_set_anr_thres(asd, &css_param->anr_thres);
3224 if (css_param->update_flag.shading_table)
3225 asd->params.config.shading_table = css_param->shading_table;
3227 if (css_param->update_flag.morph_table && asd->params.gdc_cac_en)
3228 asd->params.config.morph_table = css_param->morph_table;
3230 if (css_param->update_flag.dvs2_coefs) {
3231 struct ia_css_dvs_grid_info *dvs_grid_info =
3232 atomisp_css_get_dvs_grid_info(
3233 &asd->params.curr_grid_info);
3235 if (dvs_grid_info && dvs_grid_info->enable)
3236 atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff);
3239 if (css_param->update_flag.dvs_6axis_config)
3240 atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis);
3242 atomisp_css_set_isp_config_id(asd, css_param->isp_config_id);
3244 * These configurations are on used by ISP1.x, not for ISP2.x,
3245 * so do not handle them. see comments of ia_css_isp_config.
3256 static unsigned int long copy_from_compatible(void *to, const void *from,
3257 unsigned long n, bool from_user)
3260 return copy_from_user(to, (void __user *)from, n);
3262 memcpy(to, from, n);
3266 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd,
3267 struct atomisp_parameters *arg,
3268 struct atomisp_css_params *css_param,
3271 struct atomisp_parameters *cur_config = &css_param->update_flag;
3273 if (!arg || !asd || !css_param)
3276 if (arg->wb_config && (from_user || !cur_config->wb_config)) {
3277 if (copy_from_compatible(&css_param->wb_config, arg->wb_config,
3278 sizeof(struct ia_css_wb_config),
3281 css_param->update_flag.wb_config =
3282 (struct atomisp_wb_config *)&css_param->wb_config;
3285 if (arg->ob_config && (from_user || !cur_config->ob_config)) {
3286 if (copy_from_compatible(&css_param->ob_config, arg->ob_config,
3287 sizeof(struct ia_css_ob_config),
3290 css_param->update_flag.ob_config =
3291 (struct atomisp_ob_config *)&css_param->ob_config;
3294 if (arg->dp_config && (from_user || !cur_config->dp_config)) {
3295 if (copy_from_compatible(&css_param->dp_config, arg->dp_config,
3296 sizeof(struct ia_css_dp_config),
3299 css_param->update_flag.dp_config =
3300 (struct atomisp_dp_config *)&css_param->dp_config;
3303 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
3304 if (arg->dz_config && (from_user || !cur_config->dz_config)) {
3305 if (copy_from_compatible(&css_param->dz_config,
3307 sizeof(struct ia_css_dz_config),
3310 if (!atomisp_check_zoom_region(asd,
3311 &css_param->dz_config)) {
3312 dev_err(asd->isp->dev, "crop region error!");
3315 css_param->update_flag.dz_config =
3316 (struct atomisp_dz_config *)
3317 &css_param->dz_config;
3321 if (arg->nr_config && (from_user || !cur_config->nr_config)) {
3322 if (copy_from_compatible(&css_param->nr_config, arg->nr_config,
3323 sizeof(struct ia_css_nr_config),
3326 css_param->update_flag.nr_config =
3327 (struct atomisp_nr_config *)&css_param->nr_config;
3330 if (arg->ee_config && (from_user || !cur_config->ee_config)) {
3331 if (copy_from_compatible(&css_param->ee_config, arg->ee_config,
3332 sizeof(struct ia_css_ee_config),
3335 css_param->update_flag.ee_config =
3336 (struct atomisp_ee_config *)&css_param->ee_config;
3339 if (arg->tnr_config && (from_user || !cur_config->tnr_config)) {
3340 if (copy_from_compatible(&css_param->tnr_config,
3342 sizeof(struct ia_css_tnr_config),
3345 css_param->update_flag.tnr_config =
3346 (struct atomisp_tnr_config *)
3347 &css_param->tnr_config;
3350 if (arg->a3a_config && (from_user || !cur_config->a3a_config)) {
3351 if (copy_from_compatible(&css_param->s3a_config,
3353 sizeof(struct ia_css_3a_config),
3356 css_param->update_flag.a3a_config =
3357 (struct atomisp_3a_config *)&css_param->s3a_config;
3360 if (arg->ctc_config && (from_user || !cur_config->ctc_config)) {
3361 if (copy_from_compatible(&css_param->ctc_config,
3363 sizeof(struct ia_css_ctc_config),
3366 css_param->update_flag.ctc_config =
3367 (struct atomisp_ctc_config *)
3368 &css_param->ctc_config;
3371 if (arg->cnr_config && (from_user || !cur_config->cnr_config)) {
3372 if (copy_from_compatible(&css_param->cnr_config,
3374 sizeof(struct ia_css_cnr_config),
3377 css_param->update_flag.cnr_config =
3378 (struct atomisp_cnr_config *)
3379 &css_param->cnr_config;
3382 if (arg->ecd_config && (from_user || !cur_config->ecd_config)) {
3383 if (copy_from_compatible(&css_param->ecd_config,
3385 sizeof(struct ia_css_ecd_config),
3388 css_param->update_flag.ecd_config =
3389 (struct atomisp_ecd_config *)
3390 &css_param->ecd_config;
3393 if (arg->ynr_config && (from_user || !cur_config->ynr_config)) {
3394 if (copy_from_compatible(&css_param->ynr_config,
3396 sizeof(struct ia_css_ynr_config),
3399 css_param->update_flag.ynr_config =
3400 (struct atomisp_ynr_config *)
3401 &css_param->ynr_config;
3404 if (arg->fc_config && (from_user || !cur_config->fc_config)) {
3405 if (copy_from_compatible(&css_param->fc_config,
3407 sizeof(struct ia_css_fc_config),
3410 css_param->update_flag.fc_config =
3411 (struct atomisp_fc_config *)&css_param->fc_config;
3414 if (arg->macc_config && (from_user || !cur_config->macc_config)) {
3415 if (copy_from_compatible(&css_param->macc_config,
3417 sizeof(struct ia_css_macc_config),
3420 css_param->update_flag.macc_config =
3421 (struct atomisp_macc_config *)
3422 &css_param->macc_config;
3425 if (arg->aa_config && (from_user || !cur_config->aa_config)) {
3426 if (copy_from_compatible(&css_param->aa_config, arg->aa_config,
3427 sizeof(struct ia_css_aa_config),
3430 css_param->update_flag.aa_config =
3431 (struct atomisp_aa_config *)&css_param->aa_config;
3434 if (arg->anr_config && (from_user || !cur_config->anr_config)) {
3435 if (copy_from_compatible(&css_param->anr_config,
3437 sizeof(struct ia_css_anr_config),
3440 css_param->update_flag.anr_config =
3441 (struct atomisp_anr_config *)
3442 &css_param->anr_config;
3445 if (arg->xnr_config && (from_user || !cur_config->xnr_config)) {
3446 if (copy_from_compatible(&css_param->xnr_config,
3448 sizeof(struct ia_css_xnr_config),
3451 css_param->update_flag.xnr_config =
3452 (struct atomisp_xnr_config *)
3453 &css_param->xnr_config;
3456 if (arg->yuv2rgb_cc_config &&
3457 (from_user || !cur_config->yuv2rgb_cc_config)) {
3458 if (copy_from_compatible(&css_param->yuv2rgb_cc_config,
3459 arg->yuv2rgb_cc_config,
3460 sizeof(struct ia_css_cc_config),
3463 css_param->update_flag.yuv2rgb_cc_config =
3464 (struct atomisp_cc_config *)
3465 &css_param->yuv2rgb_cc_config;
3468 if (arg->rgb2yuv_cc_config &&
3469 (from_user || !cur_config->rgb2yuv_cc_config)) {
3470 if (copy_from_compatible(&css_param->rgb2yuv_cc_config,
3471 arg->rgb2yuv_cc_config,
3472 sizeof(struct ia_css_cc_config),
3475 css_param->update_flag.rgb2yuv_cc_config =
3476 (struct atomisp_cc_config *)
3477 &css_param->rgb2yuv_cc_config;
3480 if (arg->macc_table && (from_user || !cur_config->macc_table)) {
3481 if (copy_from_compatible(&css_param->macc_table,
3483 sizeof(struct ia_css_macc_table),
3486 css_param->update_flag.macc_table =
3487 (struct atomisp_macc_table *)
3488 &css_param->macc_table;
3491 if (arg->xnr_table && (from_user || !cur_config->xnr_table)) {
3492 if (copy_from_compatible(&css_param->xnr_table,
3494 sizeof(struct ia_css_xnr_table),
3497 css_param->update_flag.xnr_table =
3498 (struct atomisp_xnr_table *)&css_param->xnr_table;
3501 if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) {
3502 if (copy_from_compatible(&css_param->r_gamma_table,
3504 sizeof(struct ia_css_rgb_gamma_table),
3507 css_param->update_flag.r_gamma_table =
3508 (struct atomisp_rgb_gamma_table *)
3509 &css_param->r_gamma_table;
3512 if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) {
3513 if (copy_from_compatible(&css_param->g_gamma_table,
3515 sizeof(struct ia_css_rgb_gamma_table),
3518 css_param->update_flag.g_gamma_table =
3519 (struct atomisp_rgb_gamma_table *)
3520 &css_param->g_gamma_table;
3523 if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) {
3524 if (copy_from_compatible(&css_param->b_gamma_table,
3526 sizeof(struct ia_css_rgb_gamma_table),
3529 css_param->update_flag.b_gamma_table =
3530 (struct atomisp_rgb_gamma_table *)
3531 &css_param->b_gamma_table;
3534 if (arg->anr_thres && (from_user || !cur_config->anr_thres)) {
3535 if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres,
3536 sizeof(struct ia_css_anr_thres),
3539 css_param->update_flag.anr_thres =
3540 (struct atomisp_anr_thres *)&css_param->anr_thres;
3544 css_param->isp_config_id = arg->isp_config_id;
3546 * These configurations are on used by ISP1.x, not for ISP2.x,
3547 * so do not handle them. see comments of ia_css_isp_config.
3559 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd,
3560 struct atomisp_shading_table *source_st,
3561 struct atomisp_css_params *css_param,
3565 unsigned int len_table;
3566 struct ia_css_shading_table *shading_table;
3567 struct ia_css_shading_table *old_table;
3568 struct atomisp_shading_table *st, dest_st;
3576 if (!from_user && css_param->update_flag.shading_table)
3580 if (copy_from_compatible(&dest_st, source_st,
3581 sizeof(struct atomisp_shading_table),
3583 dev_err(asd->isp->dev, "copy shading table failed!");
3591 old_table = css_param->shading_table;
3593 /* user config is to disable the shading table. */
3595 /* Generate a minimum table with enable = 0. */
3596 shading_table = atomisp_css_shading_table_alloc(1, 1);
3599 shading_table->enable = 0;
3603 /* Setting a new table. Validate first - all tables must be set */
3604 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3606 dev_err(asd->isp->dev, "shading table validate failed");
3611 /* Shading table size per color */
3612 if (st->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
3613 st->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
3614 dev_err(asd->isp->dev, "shading table w/h validate failed!");
3618 shading_table = atomisp_css_shading_table_alloc(st->width, st->height);
3622 len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE;
3623 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3624 if (copy_from_compatible(shading_table->data[i],
3625 st->data[i], len_table, from_user)) {
3626 atomisp_css_shading_table_free(shading_table);
3630 shading_table->sensor_width = st->sensor_width;
3631 shading_table->sensor_height = st->sensor_height;
3632 shading_table->fraction_bits = st->fraction_bits;
3633 shading_table->enable = st->enable;
3635 /* No need to update shading table if it is the same */
3637 old_table->sensor_width == shading_table->sensor_width &&
3638 old_table->sensor_height == shading_table->sensor_height &&
3639 old_table->width == shading_table->width &&
3640 old_table->height == shading_table->height &&
3641 old_table->fraction_bits == shading_table->fraction_bits &&
3642 old_table->enable == shading_table->enable) {
3643 bool data_is_same = true;
3645 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3646 if (memcmp(shading_table->data[i], old_table->data[i],
3648 data_is_same = false;
3654 atomisp_css_shading_table_free(shading_table);
3660 /* set LSC to CSS */
3661 css_param->shading_table = shading_table;
3662 css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table;
3663 asd->params.sc_en = shading_table;
3666 atomisp_css_shading_table_free(old_table);
3671 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd,
3672 struct ia_css_dvs2_coefficients *coefs,
3673 struct atomisp_css_params *css_param,
3676 struct ia_css_dvs_grid_info *cur =
3677 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3678 int dvs_hor_coef_bytes, dvs_ver_coef_bytes;
3679 struct ia_css_dvs2_coefficients dvs2_coefs;
3684 if (!from_user && css_param->update_flag.dvs2_coefs)
3688 if (sizeof(*cur) != sizeof(coefs->grid) ||
3689 memcmp(&coefs->grid, cur, sizeof(coefs->grid))) {
3690 dev_err(asd->isp->dev, "dvs grid mis-match!\n");
3691 /* If the grid info in the argument differs from the current
3692 grid info, we tell the caller to reset the grid size and
3697 if (!coefs->hor_coefs.odd_real ||
3698 !coefs->hor_coefs.odd_imag ||
3699 !coefs->hor_coefs.even_real ||
3700 !coefs->hor_coefs.even_imag ||
3701 !coefs->ver_coefs.odd_real ||
3702 !coefs->ver_coefs.odd_imag ||
3703 !coefs->ver_coefs.even_real ||
3704 !coefs->ver_coefs.even_imag)
3707 if (!css_param->dvs2_coeff) {
3708 /* DIS coefficients. */
3709 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3710 if (!css_param->dvs2_coeff)
3714 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3715 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3716 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3717 coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3718 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3719 coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3720 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3721 coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3722 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3723 coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3724 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3725 coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3726 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3727 coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3728 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3729 coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3730 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3731 coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3732 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3733 css_param->dvs2_coeff = NULL;
3737 if (copy_from_compatible(&dvs2_coefs, coefs,
3738 sizeof(struct ia_css_dvs2_coefficients),
3740 dev_err(asd->isp->dev, "copy dvs2 coef failed");
3744 if (sizeof(*cur) != sizeof(dvs2_coefs.grid) ||
3745 memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) {
3746 dev_err(asd->isp->dev, "dvs grid mis-match!\n");
3747 /* If the grid info in the argument differs from the current
3748 grid info, we tell the caller to reset the grid size and
3753 if (!dvs2_coefs.hor_coefs.odd_real ||
3754 !dvs2_coefs.hor_coefs.odd_imag ||
3755 !dvs2_coefs.hor_coefs.even_real ||
3756 !dvs2_coefs.hor_coefs.even_imag ||
3757 !dvs2_coefs.ver_coefs.odd_real ||
3758 !dvs2_coefs.ver_coefs.odd_imag ||
3759 !dvs2_coefs.ver_coefs.even_real ||
3760 !dvs2_coefs.ver_coefs.even_imag)
3763 if (!css_param->dvs2_coeff) {
3764 /* DIS coefficients. */
3765 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3766 if (!css_param->dvs2_coeff)
3770 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3771 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3772 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3773 dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3774 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3775 dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3776 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3777 dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3778 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3779 dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3780 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3781 dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3782 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3783 dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3784 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3785 dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3786 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3787 dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3788 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3789 css_param->dvs2_coeff = NULL;
3794 css_param->update_flag.dvs2_coefs =
3795 (struct atomisp_dis_coefficients *)css_param->dvs2_coeff;
3799 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd,
3800 struct atomisp_dvs_6axis_config *source_6axis_config,
3801 struct atomisp_css_params *css_param,
3804 struct ia_css_dvs_6axis_config *dvs_6axis_config;
3805 struct ia_css_dvs_6axis_config *old_6axis_config;
3806 struct ia_css_stream *stream =
3807 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
3808 struct ia_css_dvs_grid_info *dvs_grid_info =
3809 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3813 dev_err(asd->isp->dev, "%s: internal error!", __func__);
3817 if (!source_6axis_config || !dvs_grid_info)
3820 if (!dvs_grid_info->enable)
3823 if (!from_user && css_param->update_flag.dvs_6axis_config)
3826 /* check whether need to reallocate for 6 axis config */
3827 old_6axis_config = css_param->dvs_6axis;
3828 dvs_6axis_config = old_6axis_config;
3831 struct ia_css_dvs_6axis_config t_6axis_config;
3833 if (copy_from_compatible(&t_6axis_config, source_6axis_config,
3834 sizeof(struct atomisp_dvs_6axis_config),
3836 dev_err(asd->isp->dev, "copy morph table failed!");
3840 if (old_6axis_config &&
3841 (old_6axis_config->width_y != t_6axis_config.width_y ||
3842 old_6axis_config->height_y != t_6axis_config.height_y ||
3843 old_6axis_config->width_uv != t_6axis_config.width_uv ||
3844 old_6axis_config->height_uv != t_6axis_config.height_uv)) {
3845 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3846 css_param->dvs_6axis = NULL;
3848 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3849 if (!dvs_6axis_config)
3851 } else if (!dvs_6axis_config) {
3852 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3853 if (!dvs_6axis_config)
3857 dvs_6axis_config->exp_id = t_6axis_config.exp_id;
3859 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3860 t_6axis_config.xcoords_y,
3861 t_6axis_config.width_y *
3862 t_6axis_config.height_y *
3863 sizeof(*dvs_6axis_config->xcoords_y),
3866 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3867 t_6axis_config.ycoords_y,
3868 t_6axis_config.width_y *
3869 t_6axis_config.height_y *
3870 sizeof(*dvs_6axis_config->ycoords_y),
3873 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3874 t_6axis_config.xcoords_uv,
3875 t_6axis_config.width_uv *
3876 t_6axis_config.height_uv *
3877 sizeof(*dvs_6axis_config->xcoords_uv),
3880 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3881 t_6axis_config.ycoords_uv,
3882 t_6axis_config.width_uv *
3883 t_6axis_config.height_uv *
3884 sizeof(*dvs_6axis_config->ycoords_uv),
3888 if (old_6axis_config &&
3889 (old_6axis_config->width_y != source_6axis_config->width_y ||
3890 old_6axis_config->height_y != source_6axis_config->height_y ||
3891 old_6axis_config->width_uv != source_6axis_config->width_uv ||
3892 old_6axis_config->height_uv != source_6axis_config->height_uv)) {
3893 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3894 css_param->dvs_6axis = NULL;
3896 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3897 if (!dvs_6axis_config)
3899 } else if (!dvs_6axis_config) {
3900 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3901 if (!dvs_6axis_config)
3905 dvs_6axis_config->exp_id = source_6axis_config->exp_id;
3907 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3908 source_6axis_config->xcoords_y,
3909 source_6axis_config->width_y *
3910 source_6axis_config->height_y *
3911 sizeof(*source_6axis_config->xcoords_y),
3914 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3915 source_6axis_config->ycoords_y,
3916 source_6axis_config->width_y *
3917 source_6axis_config->height_y *
3918 sizeof(*source_6axis_config->ycoords_y),
3921 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3922 source_6axis_config->xcoords_uv,
3923 source_6axis_config->width_uv *
3924 source_6axis_config->height_uv *
3925 sizeof(*source_6axis_config->xcoords_uv),
3928 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3929 source_6axis_config->ycoords_uv,
3930 source_6axis_config->width_uv *
3931 source_6axis_config->height_uv *
3932 sizeof(*source_6axis_config->ycoords_uv),
3936 css_param->dvs_6axis = dvs_6axis_config;
3937 css_param->update_flag.dvs_6axis_config =
3938 (struct atomisp_dvs_6axis_config *)dvs_6axis_config;
3942 if (dvs_6axis_config)
3943 ia_css_dvs2_6axis_config_free(dvs_6axis_config);
3947 int atomisp_cp_morph_table(struct atomisp_sub_device *asd,
3948 struct atomisp_morph_table *source_morph_table,
3949 struct atomisp_css_params *css_param,
3954 struct ia_css_morph_table *morph_table;
3955 struct ia_css_morph_table *old_morph_table;
3957 if (!source_morph_table)
3960 if (!from_user && css_param->update_flag.morph_table)
3963 old_morph_table = css_param->morph_table;
3966 struct ia_css_morph_table mtbl;
3968 if (copy_from_compatible(&mtbl, source_morph_table,
3969 sizeof(struct atomisp_morph_table),
3971 dev_err(asd->isp->dev, "copy morph table failed!");
3975 morph_table = atomisp_css_morph_table_allocate(
3981 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3982 if (copy_from_compatible(morph_table->coordinates_x[i],
3983 (__force void *)source_morph_table->coordinates_x[i],
3984 mtbl.height * mtbl.width *
3985 sizeof(*morph_table->coordinates_x[i]),
3989 if (copy_from_compatible(morph_table->coordinates_y[i],
3990 (__force void *)source_morph_table->coordinates_y[i],
3991 mtbl.height * mtbl.width *
3992 sizeof(*morph_table->coordinates_y[i]),
3997 morph_table = atomisp_css_morph_table_allocate(
3998 source_morph_table->width,
3999 source_morph_table->height);
4003 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
4004 if (copy_from_compatible(morph_table->coordinates_x[i],
4005 (__force void *)source_morph_table->coordinates_x[i],
4006 source_morph_table->height * source_morph_table->width *
4007 sizeof(*source_morph_table->coordinates_x[i]),
4011 if (copy_from_compatible(morph_table->coordinates_y[i],
4012 (__force void *)source_morph_table->coordinates_y[i],
4013 source_morph_table->height * source_morph_table->width *
4014 sizeof(*source_morph_table->coordinates_y[i]),
4020 css_param->morph_table = morph_table;
4021 if (old_morph_table)
4022 atomisp_css_morph_table_free(old_morph_table);
4023 css_param->update_flag.morph_table =
4024 (struct atomisp_morph_table *)morph_table;
4029 atomisp_css_morph_table_free(morph_table);
4033 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd,
4034 struct atomisp_parameters *arg,
4035 struct atomisp_css_params *css_param)
4039 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false);
4042 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false);
4045 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false);
4048 ret = atomisp_css_cp_dvs2_coefs(asd,
4049 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
4053 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
4058 void atomisp_free_css_parameters(struct atomisp_css_params *css_param)
4060 if (css_param->dvs_6axis) {
4061 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
4062 css_param->dvs_6axis = NULL;
4064 if (css_param->dvs2_coeff) {
4065 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
4066 css_param->dvs2_coeff = NULL;
4068 if (css_param->shading_table) {
4069 ia_css_shading_table_free(css_param->shading_table);
4070 css_param->shading_table = NULL;
4072 if (css_param->morph_table) {
4073 ia_css_morph_table_free(css_param->morph_table);
4074 css_param->morph_table = NULL;
4079 * Check parameter queue list and buffer queue list to find out if matched items
4080 * and then set parameter to CSS and enqueue buffer to CSS.
4081 * Of course, if the buffer in buffer waiting list is not bound to a per-frame
4082 * parameter, it will be enqueued into CSS as long as the per-frame setting
4083 * buffers before it get enqueued.
4085 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe)
4087 struct atomisp_sub_device *asd = pipe->asd;
4088 struct videobuf_buffer *vb = NULL, *vb_tmp;
4089 struct atomisp_css_params_with_list *param = NULL, *param_tmp;
4090 struct videobuf_vmalloc_memory *vm_mem = NULL;
4091 unsigned long irqflags;
4092 bool need_to_enqueue_buffer = false;
4095 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
4096 __func__, pipe->vdev.name);
4100 if (atomisp_is_vf_pipe(pipe))
4104 * CSS/FW requires set parameter and enqueue buffer happen after ISP
4107 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
4110 if (list_empty(&pipe->per_frame_params) ||
4111 list_empty(&pipe->buffers_waiting_for_param))
4114 list_for_each_entry_safe(vb, vb_tmp,
4115 &pipe->buffers_waiting_for_param, queue) {
4116 if (pipe->frame_request_config_id[vb->i]) {
4117 list_for_each_entry_safe(param, param_tmp,
4118 &pipe->per_frame_params, list) {
4119 if (pipe->frame_request_config_id[vb->i] !=
4120 param->params.isp_config_id)
4123 list_del(¶m->list);
4124 list_del(&vb->queue);
4126 * clear the request config id as the buffer
4127 * will be handled and enqueued into CSS soon
4129 pipe->frame_request_config_id[vb->i] = 0;
4130 pipe->frame_params[vb->i] = param;
4137 spin_lock_irqsave(&pipe->irq_lock, irqflags);
4138 list_add_tail(&vb->queue, &pipe->activeq);
4139 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
4141 need_to_enqueue_buffer = true;
4143 /* The is the end, stop further loop */
4147 list_del(&vb->queue);
4148 pipe->frame_params[vb->i] = NULL;
4149 spin_lock_irqsave(&pipe->irq_lock, irqflags);
4150 list_add_tail(&vb->queue, &pipe->activeq);
4151 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
4152 need_to_enqueue_buffer = true;
4156 if (!need_to_enqueue_buffer)
4159 atomisp_qbuffers_to_css(asd);
4162 if (!atomisp_is_wdt_running(asd) && atomisp_buffers_queued(asd))
4163 atomisp_wdt_start(asd);
4165 if (atomisp_buffers_queued_pipe(pipe)) {
4166 if (!atomisp_is_wdt_running(pipe))
4167 atomisp_wdt_start_pipe(pipe);
4169 atomisp_wdt_refresh_pipe(pipe,
4170 ATOMISP_WDT_KEEP_CURRENT_DELAY);
4176 * Function to configure ISP parameters
4178 int atomisp_set_parameters(struct video_device *vdev,
4179 struct atomisp_parameters *arg)
4181 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4182 struct atomisp_sub_device *asd = pipe->asd;
4183 struct atomisp_css_params_with_list *param = NULL;
4184 struct atomisp_css_params *css_param = &asd->params.css_param;
4188 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
4189 __func__, vdev->name);
4193 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) {
4194 dev_err(asd->isp->dev, "%s: internal error!\n", __func__);
4198 dev_dbg(asd->isp->dev,
4199 "%s: set parameter(per_frame_setting %d) for asd%d with isp_config_id %d of %s\n",
4200 __func__, arg->per_frame_setting, asd->index,
4201 arg->isp_config_id, vdev->name);
4204 if (atomisp_is_vf_pipe(pipe) && arg->per_frame_setting) {
4205 dev_err(asd->isp->dev, "%s: vf pipe not support per_frame_setting",
4211 if (arg->per_frame_setting && !atomisp_is_vf_pipe(pipe)) {
4213 * Per-frame setting enabled, we allocate a new parameter
4214 * buffer to cache the parameters and only when frame buffers
4215 * are ready, the parameters will be set to CSS.
4216 * per-frame setting only works for the main output frame.
4218 param = kvzalloc(sizeof(*param), GFP_KERNEL);
4220 dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n",
4224 css_param = ¶m->params;
4227 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true);
4229 goto apply_parameter_failed;
4231 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true);
4233 goto apply_parameter_failed;
4235 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true);
4237 goto apply_parameter_failed;
4239 ret = atomisp_css_cp_dvs2_coefs(asd,
4240 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
4243 goto apply_parameter_failed;
4245 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
4248 goto apply_parameter_failed;
4250 if (!(arg->per_frame_setting && !atomisp_is_vf_pipe(pipe))) {
4251 /* indicate to CSS that we have parameters to be updated */
4252 asd->params.css_update_params_needed = true;
4254 list_add_tail(¶m->list, &pipe->per_frame_params);
4255 atomisp_handle_parameter_and_buffer(pipe);
4260 apply_parameter_failed:
4262 atomisp_free_css_parameters(css_param);
4269 * Function to set/get isp parameters to isp
4271 int atomisp_param(struct atomisp_sub_device *asd, int flag,
4272 struct atomisp_parm *config)
4274 struct ia_css_pipe_config *vp_cfg =
4275 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
4276 pipe_configs[IA_CSS_PIPE_ID_VIDEO];
4278 /* Read parameter for 3A binary info */
4280 struct ia_css_dvs_grid_info *dvs_grid_info =
4281 atomisp_css_get_dvs_grid_info(
4282 &asd->params.curr_grid_info);
4284 atomisp_curr_user_grid_info(asd, &config->info);
4286 /* We always return the resolution and stride even if there is
4287 * no valid metadata. This allows the caller to get the
4288 * information needed to allocate user-space buffers. */
4289 config->metadata_config.metadata_height = asd->
4290 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
4291 metadata_info.resolution.height;
4292 config->metadata_config.metadata_stride = asd->
4293 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
4294 metadata_info.stride;
4296 /* update dvs grid info */
4298 memcpy(&config->dvs_grid,
4300 sizeof(struct ia_css_dvs_grid_info));
4302 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
4303 config->dvs_envelop.width = 0;
4304 config->dvs_envelop.height = 0;
4308 /* update dvs envelop info */
4309 if (!asd->continuous_mode->val) {
4310 config->dvs_envelop.width = vp_cfg->dvs_envelope.width;
4311 config->dvs_envelop.height =
4312 vp_cfg->dvs_envelope.height;
4314 unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
4316 dvs_w = vp_cfg->bayer_ds_out_res.width -
4317 vp_cfg->output_info[0].res.width;
4318 dvs_h = vp_cfg->bayer_ds_out_res.height -
4319 vp_cfg->output_info[0].res.height;
4320 dvs_w_max = rounddown(
4321 vp_cfg->output_info[0].res.width / 5,
4322 ATOM_ISP_STEP_WIDTH);
4323 dvs_h_max = rounddown(
4324 vp_cfg->output_info[0].res.height / 5,
4325 ATOM_ISP_STEP_HEIGHT);
4327 config->dvs_envelop.width = min(dvs_w, dvs_w_max);
4328 config->dvs_envelop.height = min(dvs_h, dvs_h_max);
4334 memcpy(&asd->params.css_param.wb_config, &config->wb_config,
4335 sizeof(struct ia_css_wb_config));
4336 memcpy(&asd->params.css_param.ob_config, &config->ob_config,
4337 sizeof(struct ia_css_ob_config));
4338 memcpy(&asd->params.css_param.dp_config, &config->dp_config,
4339 sizeof(struct ia_css_dp_config));
4340 memcpy(&asd->params.css_param.de_config, &config->de_config,
4341 sizeof(struct ia_css_de_config));
4342 memcpy(&asd->params.css_param.dz_config, &config->dz_config,
4343 sizeof(struct ia_css_dz_config));
4344 memcpy(&asd->params.css_param.ce_config, &config->ce_config,
4345 sizeof(struct ia_css_ce_config));
4346 memcpy(&asd->params.css_param.nr_config, &config->nr_config,
4347 sizeof(struct ia_css_nr_config));
4348 memcpy(&asd->params.css_param.ee_config, &config->ee_config,
4349 sizeof(struct ia_css_ee_config));
4350 memcpy(&asd->params.css_param.tnr_config, &config->tnr_config,
4351 sizeof(struct ia_css_tnr_config));
4353 if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) {
4354 asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3];
4355 asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4];
4356 asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5];
4357 asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6];
4358 asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7];
4359 asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8];
4362 if (asd->params.color_effect != V4L2_COLORFX_SEPIA &&
4363 asd->params.color_effect != V4L2_COLORFX_BW) {
4364 memcpy(&asd->params.css_param.cc_config, &config->cc_config,
4365 sizeof(struct ia_css_cc_config));
4366 asd->params.config.cc_config = &asd->params.css_param.cc_config;
4369 asd->params.config.wb_config = &asd->params.css_param.wb_config;
4370 asd->params.config.ob_config = &asd->params.css_param.ob_config;
4371 asd->params.config.de_config = &asd->params.css_param.de_config;
4372 asd->params.config.dz_config = &asd->params.css_param.dz_config;
4373 asd->params.config.ce_config = &asd->params.css_param.ce_config;
4374 asd->params.config.dp_config = &asd->params.css_param.dp_config;
4375 asd->params.config.nr_config = &asd->params.css_param.nr_config;
4376 asd->params.config.ee_config = &asd->params.css_param.ee_config;
4377 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
4378 asd->params.css_update_params_needed = true;
4384 * Function to configure color effect of the image
4386 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag,
4389 struct ia_css_cc_config *cc_config = NULL;
4390 struct ia_css_macc_table *macc_table = NULL;
4391 struct ia_css_ctc_table *ctc_table = NULL;
4393 struct v4l2_control control;
4394 struct atomisp_device *isp = asd->isp;
4397 *effect = asd->params.color_effect;
4401 control.id = V4L2_CID_COLORFX;
4402 control.value = *effect;
4404 v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler,
4407 * if set color effect to sensor successfully, return
4411 asd->params.color_effect = (u32)*effect;
4415 if (*effect == asd->params.color_effect)
4419 * isp_subdev->params.macc_en should be set to false.
4421 asd->params.macc_en = false;
4424 case V4L2_COLORFX_NONE:
4425 macc_table = &asd->params.css_param.macc_table;
4426 asd->params.macc_en = true;
4428 case V4L2_COLORFX_SEPIA:
4429 cc_config = &sepia_cc_config;
4431 case V4L2_COLORFX_NEGATIVE:
4432 cc_config = &nega_cc_config;
4434 case V4L2_COLORFX_BW:
4435 cc_config = &mono_cc_config;
4437 case V4L2_COLORFX_SKY_BLUE:
4438 macc_table = &blue_macc_table;
4439 asd->params.macc_en = true;
4441 case V4L2_COLORFX_GRASS_GREEN:
4442 macc_table = &green_macc_table;
4443 asd->params.macc_en = true;
4445 case V4L2_COLORFX_SKIN_WHITEN_LOW:
4446 macc_table = &skin_low_macc_table;
4447 asd->params.macc_en = true;
4449 case V4L2_COLORFX_SKIN_WHITEN:
4450 macc_table = &skin_medium_macc_table;
4451 asd->params.macc_en = true;
4453 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
4454 macc_table = &skin_high_macc_table;
4455 asd->params.macc_en = true;
4457 case V4L2_COLORFX_VIVID:
4458 ctc_table = &vivid_ctc_table;
4463 atomisp_update_capture_mode(asd);
4466 asd->params.config.cc_config = cc_config;
4468 asd->params.config.macc_table = macc_table;
4470 atomisp_css_set_ctc_table(asd, ctc_table);
4471 asd->params.color_effect = (u32)*effect;
4472 asd->params.css_update_params_needed = true;
4477 * Function to configure bad pixel correction
4479 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag,
4483 *value = asd->params.bad_pixel_en;
4486 asd->params.bad_pixel_en = !!*value;
4492 * Function to configure bad pixel correction params
4494 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag,
4495 struct atomisp_dp_config *config)
4498 /* Get bad pixel from current setup */
4499 if (atomisp_css_get_dp_config(asd, config))
4502 /* Set bad pixel to isp parameters */
4503 memcpy(&asd->params.css_param.dp_config, config,
4504 sizeof(asd->params.css_param.dp_config));
4505 asd->params.config.dp_config = &asd->params.css_param.dp_config;
4506 asd->params.css_update_params_needed = true;
4513 * Function to enable/disable video image stablization
4515 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag,
4519 *value = asd->params.video_dis_en;
4521 asd->params.video_dis_en = !!*value;
4527 * Function to configure fixed pattern noise
4529 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag,
4533 *value = asd->params.fpn_en;
4538 asd->params.fpn_en = false;
4542 /* Add function to get black from from sensor with shutter off */
4547 atomisp_bytesperline_to_padded_width(unsigned int bytesperline,
4548 enum ia_css_frame_format format)
4551 case IA_CSS_FRAME_FORMAT_UYVY:
4552 case IA_CSS_FRAME_FORMAT_YUYV:
4553 case IA_CSS_FRAME_FORMAT_RAW:
4554 case IA_CSS_FRAME_FORMAT_RGB565:
4555 return bytesperline / 2;
4556 case IA_CSS_FRAME_FORMAT_RGBA888:
4557 return bytesperline / 4;
4558 /* The following cases could be removed, but we leave them
4559 in to document the formats that are included. */
4560 case IA_CSS_FRAME_FORMAT_NV11:
4561 case IA_CSS_FRAME_FORMAT_NV12:
4562 case IA_CSS_FRAME_FORMAT_NV16:
4563 case IA_CSS_FRAME_FORMAT_NV21:
4564 case IA_CSS_FRAME_FORMAT_NV61:
4565 case IA_CSS_FRAME_FORMAT_YV12:
4566 case IA_CSS_FRAME_FORMAT_YV16:
4567 case IA_CSS_FRAME_FORMAT_YUV420:
4568 case IA_CSS_FRAME_FORMAT_YUV420_16:
4569 case IA_CSS_FRAME_FORMAT_YUV422:
4570 case IA_CSS_FRAME_FORMAT_YUV422_16:
4571 case IA_CSS_FRAME_FORMAT_YUV444:
4572 case IA_CSS_FRAME_FORMAT_YUV_LINE:
4573 case IA_CSS_FRAME_FORMAT_PLANAR_RGB888:
4574 case IA_CSS_FRAME_FORMAT_QPLANE6:
4575 case IA_CSS_FRAME_FORMAT_BINARY_8:
4577 return bytesperline;
4582 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg,
4583 struct ia_css_frame **result)
4585 struct ia_css_frame *res = NULL;
4586 unsigned int padded_width;
4587 enum ia_css_frame_format sh_format;
4588 char *tmp_buf = NULL;
4591 sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat);
4592 padded_width = atomisp_bytesperline_to_padded_width(
4593 arg->fmt.bytesperline, sh_format);
4595 /* Note: the padded width on an ia_css_frame is in elements, not in
4596 bytes. The RAW frame we use here should always be a 16bit RAW
4597 frame. This is why we bytesperline/2 is equal to the padded with */
4598 if (ia_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height,
4599 sh_format, padded_width, 0)) {
4604 tmp_buf = vmalloc(arg->fmt.sizeimage);
4609 if (copy_from_user(tmp_buf, (void __user __force *)arg->base,
4610 arg->fmt.sizeimage)) {
4615 if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) {
4622 ia_css_frame_free(res);
4630 * Function to configure fixed pattern noise table
4632 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd,
4633 struct v4l2_framebuffer *arg)
4635 struct ia_css_frame *raw_black_frame = NULL;
4641 ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame);
4645 if (sh_css_set_black_frame(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream,
4646 raw_black_frame) != 0)
4649 ia_css_frame_free(raw_black_frame);
4654 * Function to configure false color correction
4656 int atomisp_false_color(struct atomisp_sub_device *asd, int flag,
4659 /* Get nr config from current setup */
4661 *value = asd->params.false_color;
4665 /* Set nr config to isp parameters */
4667 asd->params.config.de_config = NULL;
4669 asd->params.css_param.de_config.pixelnoise = 0;
4670 asd->params.config.de_config = &asd->params.css_param.de_config;
4672 asd->params.css_update_params_needed = true;
4673 asd->params.false_color = *value;
4678 * Function to configure bad pixel correction params
4680 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag,
4681 struct atomisp_de_config *config)
4684 /* Get false color from current setup */
4685 if (atomisp_css_get_de_config(asd, config))
4688 /* Set false color to isp parameters */
4689 memcpy(&asd->params.css_param.de_config, config,
4690 sizeof(asd->params.css_param.de_config));
4691 asd->params.config.de_config = &asd->params.css_param.de_config;
4692 asd->params.css_update_params_needed = true;
4699 * Function to configure white balance params
4701 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag,
4702 struct atomisp_wb_config *config)
4705 /* Get white balance from current setup */
4706 if (atomisp_css_get_wb_config(asd, config))
4709 /* Set white balance to isp parameters */
4710 memcpy(&asd->params.css_param.wb_config, config,
4711 sizeof(asd->params.css_param.wb_config));
4712 asd->params.config.wb_config = &asd->params.css_param.wb_config;
4713 asd->params.css_update_params_needed = true;
4719 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag,
4720 struct atomisp_3a_config *config)
4722 struct atomisp_device *isp = asd->isp;
4724 dev_dbg(isp->dev, ">%s %d\n", __func__, flag);
4727 /* Get white balance from current setup */
4728 if (atomisp_css_get_3a_config(asd, config))
4731 /* Set white balance to isp parameters */
4732 memcpy(&asd->params.css_param.s3a_config, config,
4733 sizeof(asd->params.css_param.s3a_config));
4734 asd->params.config.s3a_config = &asd->params.css_param.s3a_config;
4735 asd->params.css_update_params_needed = true;
4738 dev_dbg(isp->dev, "<%s %d\n", __func__, flag);
4743 * Function to setup digital zoom
4745 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag,
4749 struct atomisp_device *isp = asd->isp;
4751 unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR;
4754 atomisp_css_get_zoom_factor(asd, &zoom);
4755 *value = max_zoom - zoom;
4760 zoom = max_zoom - min_t(u32, max_zoom - 1, *value);
4761 atomisp_css_set_zoom_factor(asd, zoom);
4763 dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom);
4764 asd->params.css_update_params_needed = true;
4771 * Function to get sensor specific info for current resolution,
4772 * which will be used for auto exposure conversion.
4774 int atomisp_get_sensor_mode_data(struct atomisp_sub_device *asd,
4775 struct atomisp_sensor_mode_data *config)
4777 struct camera_mipi_info *mipi_info;
4778 struct atomisp_device *isp = asd->isp;
4780 mipi_info = atomisp_to_sensor_mipi_info(
4781 isp->inputs[asd->input_curr].camera);
4785 memcpy(config, &mipi_info->data, sizeof(*config));
4789 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd,
4790 u16 stream_index, struct atomisp_input_stream_info *stream_info)
4794 /* assign virtual channel id return from sensor driver query */
4795 asd->stream_env[stream_index].ch_id = stream_info->ch_id;
4796 asd->stream_env[stream_index].isys_configs = stream_info->isys_configs;
4797 for (i = 0; i < stream_info->isys_configs; i++) {
4798 asd->stream_env[stream_index].isys_info[i].input_format =
4799 stream_info->isys_info[i].input_format;
4800 asd->stream_env[stream_index].isys_info[i].width =
4801 stream_info->isys_info[i].width;
4802 asd->stream_env[stream_index].isys_info[i].height =
4803 stream_info->isys_info[i].height;
4807 static void __atomisp_init_stream_info(u16 stream_index,
4808 struct atomisp_input_stream_info *stream_info)
4812 stream_info->enable = 1;
4813 stream_info->stream = stream_index;
4814 stream_info->ch_id = 0;
4815 stream_info->isys_configs = 0;
4816 for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) {
4817 stream_info->isys_info[i].input_format = 0;
4818 stream_info->isys_info[i].width = 0;
4819 stream_info->isys_info[i].height = 0;
4823 /* This function looks up the closest available resolution. */
4824 int atomisp_try_fmt(struct video_device *vdev, struct v4l2_pix_format *f,
4827 struct atomisp_device *isp = video_get_drvdata(vdev);
4828 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4829 struct v4l2_subdev_pad_config pad_cfg;
4830 struct v4l2_subdev_state pad_state = {
4833 struct v4l2_subdev_format format = {
4834 .which = V4L2_SUBDEV_FORMAT_TRY,
4837 struct v4l2_mbus_framefmt *snr_mbus_fmt = &format.format;
4838 const struct atomisp_format_bridge *fmt;
4839 struct atomisp_input_stream_info *stream_info =
4840 (struct atomisp_input_stream_info *)snr_mbus_fmt->reserved;
4842 int source_pad = atomisp_subdev_source_pad(vdev);
4846 dev_err(isp->dev, "%s(): asd is NULL, device is %s\n",
4847 __func__, vdev->name);
4851 if (!isp->inputs[asd->input_curr].camera)
4854 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
4855 fmt = atomisp_get_format_bridge(f->pixelformat);
4857 dev_err(isp->dev, "unsupported pixelformat!\n");
4858 fmt = atomisp_output_fmts;
4861 if (f->width <= 0 || f->height <= 0)
4864 snr_mbus_fmt->code = fmt->mbus_code;
4865 snr_mbus_fmt->width = f->width;
4866 snr_mbus_fmt->height = f->height;
4868 __atomisp_init_stream_info(stream_index, stream_info);
4870 dev_dbg(isp->dev, "try_mbus_fmt: asking for %ux%u\n",
4871 snr_mbus_fmt->width, snr_mbus_fmt->height);
4873 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
4874 pad, set_fmt, &pad_state, &format);
4878 dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n",
4879 snr_mbus_fmt->width, snr_mbus_fmt->height);
4881 fmt = atomisp_get_format_bridge_from_mbus(snr_mbus_fmt->code);
4883 dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
4884 snr_mbus_fmt->code);
4888 f->pixelformat = fmt->pixelformat;
4891 * If the format is jpeg or custom RAW, then the width and height will
4892 * not satisfy the normal atomisp requirements and no need to check
4893 * the below conditions. So just assign to what is being returned from
4894 * the sensor driver.
4896 if (f->pixelformat == V4L2_PIX_FMT_JPEG ||
4897 f->pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
4898 f->width = snr_mbus_fmt->width;
4899 f->height = snr_mbus_fmt->height;
4903 if (snr_mbus_fmt->width < f->width
4904 && snr_mbus_fmt->height < f->height) {
4905 f->width = snr_mbus_fmt->width;
4906 f->height = snr_mbus_fmt->height;
4907 /* Set the flag when resolution requested is
4908 * beyond the max value supported by sensor
4911 *res_overflow = true;
4915 f->width = rounddown(clamp_t(u32, f->width, ATOM_ISP_MIN_WIDTH,
4916 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
4917 f->height = rounddown(clamp_t(u32, f->height, ATOM_ISP_MIN_HEIGHT,
4918 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
4924 atomisp_try_fmt_file(struct atomisp_device *isp, struct v4l2_format *f)
4926 u32 width = f->fmt.pix.width;
4927 u32 height = f->fmt.pix.height;
4928 u32 pixelformat = f->fmt.pix.pixelformat;
4929 enum v4l2_field field = f->fmt.pix.field;
4932 if (!atomisp_get_format_bridge(pixelformat)) {
4933 dev_err(isp->dev, "Wrong output pixelformat\n");
4937 depth = atomisp_get_pixel_depth(pixelformat);
4939 if (field == V4L2_FIELD_ANY) {
4940 field = V4L2_FIELD_NONE;
4941 } else if (field != V4L2_FIELD_NONE) {
4942 dev_err(isp->dev, "Wrong output field\n");
4946 f->fmt.pix.field = field;
4947 f->fmt.pix.width = clamp_t(u32,
4948 rounddown(width, (u32)ATOM_ISP_STEP_WIDTH),
4949 ATOM_ISP_MIN_WIDTH, ATOM_ISP_MAX_WIDTH);
4950 f->fmt.pix.height = clamp_t(u32, rounddown(height,
4951 (u32)ATOM_ISP_STEP_HEIGHT),
4952 ATOM_ISP_MIN_HEIGHT, ATOM_ISP_MAX_HEIGHT);
4953 f->fmt.pix.bytesperline = (width * depth) >> 3;
4958 enum mipi_port_id __get_mipi_port(struct atomisp_device *isp,
4959 enum atomisp_camera_port port)
4962 case ATOMISP_CAMERA_PORT_PRIMARY:
4963 return MIPI_PORT0_ID;
4964 case ATOMISP_CAMERA_PORT_SECONDARY:
4965 return MIPI_PORT1_ID;
4966 case ATOMISP_CAMERA_PORT_TERTIARY:
4967 if (MIPI_PORT1_ID + 1 != N_MIPI_PORT_ID)
4968 return MIPI_PORT1_ID + 1;
4971 dev_err(isp->dev, "unsupported port: %d\n", port);
4972 return MIPI_PORT0_ID;
4976 static inline int atomisp_set_sensor_mipi_to_isp(
4977 struct atomisp_sub_device *asd,
4978 enum atomisp_input_stream_id stream_id,
4979 struct camera_mipi_info *mipi_info)
4981 struct v4l2_control ctrl;
4982 struct atomisp_device *isp = asd->isp;
4983 const struct atomisp_in_fmt_conv *fc;
4985 unsigned int input_format, bayer_order;
4987 ctrl.id = V4L2_CID_LINK_FREQ;
4989 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl) == 0)
4990 mipi_freq = ctrl.value;
4992 if (asd->stream_env[stream_id].isys_configs == 1) {
4994 asd->stream_env[stream_id].isys_info[0].input_format;
4995 atomisp_css_isys_set_format(asd, stream_id,
4996 input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX);
4997 } else if (asd->stream_env[stream_id].isys_configs == 2) {
4998 atomisp_css_isys_two_stream_cfg_update_stream1(
5000 asd->stream_env[stream_id].isys_info[0].input_format,
5001 asd->stream_env[stream_id].isys_info[0].width,
5002 asd->stream_env[stream_id].isys_info[0].height);
5004 atomisp_css_isys_two_stream_cfg_update_stream2(
5006 asd->stream_env[stream_id].isys_info[1].input_format,
5007 asd->stream_env[stream_id].isys_info[1].width,
5008 asd->stream_env[stream_id].isys_info[1].height);
5011 /* Compatibility for sensors which provide no media bus code
5012 * in s_mbus_framefmt() nor support pad formats. */
5013 if (mipi_info->input_format != -1) {
5014 bayer_order = mipi_info->raw_bayer_order;
5016 /* Input stream config is still needs configured */
5017 /* TODO: Check if this is necessary */
5018 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5019 mipi_info->input_format);
5022 input_format = fc->atomisp_in_fmt;
5024 struct v4l2_mbus_framefmt *sink;
5026 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5027 V4L2_SUBDEV_FORMAT_ACTIVE,
5028 ATOMISP_SUBDEV_PAD_SINK);
5029 fc = atomisp_find_in_fmt_conv(sink->code);
5032 input_format = fc->atomisp_in_fmt;
5033 bayer_order = fc->bayer_order;
5036 atomisp_css_input_set_format(asd, stream_id, input_format);
5037 atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order);
5039 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5040 mipi_info->metadata_format);
5043 input_format = fc->atomisp_in_fmt;
5044 atomisp_css_input_configure_port(asd,
5045 __get_mipi_port(asd->isp, mipi_info->port),
5046 mipi_info->num_lanes,
5049 mipi_info->metadata_width,
5050 mipi_info->metadata_height);
5054 static int __enable_continuous_mode(struct atomisp_sub_device *asd,
5057 struct atomisp_device *isp = asd->isp;
5060 "continuous mode %d, raw buffers %d, stop preview %d\n",
5061 enable, asd->continuous_raw_buffer_size->val,
5062 !asd->continuous_viewfinder->val);
5065 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
5067 atomisp_update_capture_mode(asd);
5069 /* in case of ANR, force capture pipe to offline mode */
5070 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
5071 asd->params.low_light ? false : !enable);
5072 atomisp_css_preview_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
5074 atomisp_css_enable_continuous(asd, enable);
5075 atomisp_css_enable_cvf(asd, asd->continuous_viewfinder->val);
5077 atomisp_css_continuous_set_num_raw_frames(asd,
5078 asd->continuous_raw_buffer_size->val);
5081 atomisp_css_enable_raw_binning(asd, false);
5082 atomisp_css_input_set_two_pixels_per_clock(asd, false);
5085 if (isp->inputs[asd->input_curr].type != FILE_INPUT)
5086 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
5088 return atomisp_update_run_mode(asd);
5091 static int configure_pp_input_nop(struct atomisp_sub_device *asd,
5092 unsigned int width, unsigned int height)
5097 static int configure_output_nop(struct atomisp_sub_device *asd,
5098 unsigned int width, unsigned int height,
5099 unsigned int min_width,
5100 enum ia_css_frame_format sh_fmt)
5105 static int get_frame_info_nop(struct atomisp_sub_device *asd,
5106 struct ia_css_frame_info *finfo)
5112 * Resets CSS parameters that depend on input resolution.
5114 * Update params like CSS RAW binning, 2ppc mode and pp_input
5115 * which depend on input size, but are not automatically
5116 * handled in CSS when the input resolution is changed.
5118 static int css_input_resolution_changed(struct atomisp_sub_device *asd,
5119 struct v4l2_mbus_framefmt *ffmt)
5121 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
5124 dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n",
5125 ffmt->width, ffmt->height);
5128 atomisp_css_input_set_two_pixels_per_clock(asd, false);
5130 atomisp_css_input_set_two_pixels_per_clock(asd, true);
5132 if (asd->continuous_mode->val) {
5133 /* Note for all checks: ffmt includes pad_w+pad_h */
5134 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
5135 (ffmt->width >= 2048 || ffmt->height >= 1536)) {
5137 * For preview pipe, enable only if resolution
5138 * is >= 3M for ISP2400.
5140 atomisp_css_enable_raw_binning(asd, true);
5144 * If sensor input changed, which means metadata resolution changed
5145 * together. Release all metadata buffers here to let it re-allocated
5146 * next time in reqbufs.
5148 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
5149 list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
5151 atomisp_css_free_metadata_buffer(md_buf);
5152 list_del(&md_buf->list);
5159 * TODO: atomisp_css_preview_configure_pp_input() not
5160 * reset due to CSS bug tracked as PSI BZ 115124
5164 static int atomisp_set_fmt_to_isp(struct video_device *vdev,
5165 struct ia_css_frame_info *output_info,
5166 struct ia_css_frame_info *raw_output_info,
5167 struct v4l2_pix_format *pix,
5168 unsigned int source_pad)
5170 struct camera_mipi_info *mipi_info;
5171 struct atomisp_device *isp = video_get_drvdata(vdev);
5172 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
5173 const struct atomisp_format_bridge *format;
5174 struct v4l2_rect *isp_sink_crop;
5175 enum ia_css_pipe_id pipe_id;
5176 struct v4l2_subdev_fh fh;
5177 int (*configure_output)(struct atomisp_sub_device *asd,
5178 unsigned int width, unsigned int height,
5179 unsigned int min_width,
5180 enum ia_css_frame_format sh_fmt) =
5181 configure_output_nop;
5182 int (*get_frame_info)(struct atomisp_sub_device *asd,
5183 struct ia_css_frame_info *finfo) =
5185 int (*configure_pp_input)(struct atomisp_sub_device *asd,
5186 unsigned int width, unsigned int height) =
5187 configure_pp_input_nop;
5189 const struct atomisp_in_fmt_conv *fc;
5193 dev_err(isp->dev, "%s(): asd is NULL, device is %s\n",
5194 __func__, vdev->name);
5197 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
5199 v4l2_fh_init(&fh.vfh, vdev);
5201 isp_sink_crop = atomisp_subdev_get_rect(
5202 &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
5203 ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP);
5205 format = atomisp_get_format_bridge(pix->pixelformat);
5209 if (isp->inputs[asd->input_curr].type != TEST_PATTERN &&
5210 isp->inputs[asd->input_curr].type != FILE_INPUT) {
5211 mipi_info = atomisp_to_sensor_mipi_info(
5212 isp->inputs[asd->input_curr].camera);
5214 dev_err(isp->dev, "mipi_info is NULL\n");
5217 if (atomisp_set_sensor_mipi_to_isp(asd, stream_index,
5220 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5221 mipi_info->input_format);
5223 fc = atomisp_find_in_fmt_conv(
5224 atomisp_subdev_get_ffmt(&asd->subdev,
5225 NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
5226 ATOMISP_SUBDEV_PAD_SINK)->code);
5229 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW &&
5230 raw_output_format_match_input(fc->atomisp_in_fmt,
5236 * Configure viewfinder also when vfpp is disabled: the
5237 * CSS still requires viewfinder configuration.
5239 if (asd->fmt_auto->val ||
5240 asd->vfpp->val != ATOMISP_VFPP_ENABLE) {
5241 struct v4l2_rect vf_size = {0};
5242 struct v4l2_mbus_framefmt vf_ffmt = {0};
5244 if (pix->width < 640 || pix->height < 480) {
5245 vf_size.width = pix->width;
5246 vf_size.height = pix->height;
5248 vf_size.width = 640;
5249 vf_size.height = 480;
5252 /* FIXME: proper format name for this one. See
5253 atomisp_output_fmts[] in atomisp_v4l2.c */
5254 vf_ffmt.code = V4L2_MBUS_FMT_CUSTOM_YUV420;
5256 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5257 V4L2_SUBDEV_FORMAT_ACTIVE,
5258 ATOMISP_SUBDEV_PAD_SOURCE_VF,
5259 V4L2_SEL_TGT_COMPOSE, 0, &vf_size);
5260 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
5261 V4L2_SUBDEV_FORMAT_ACTIVE,
5262 ATOMISP_SUBDEV_PAD_SOURCE_VF, &vf_ffmt);
5263 asd->video_out_vf.sh_fmt = IA_CSS_FRAME_FORMAT_NV12;
5265 if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
5266 atomisp_css_video_configure_viewfinder(asd,
5267 vf_size.width, vf_size.height, 0,
5268 asd->video_out_vf.sh_fmt);
5269 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5270 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5271 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO)
5272 atomisp_css_video_configure_viewfinder(asd,
5273 vf_size.width, vf_size.height, 0,
5274 asd->video_out_vf.sh_fmt);
5276 atomisp_css_capture_configure_viewfinder(asd,
5277 vf_size.width, vf_size.height, 0,
5278 asd->video_out_vf.sh_fmt);
5279 } else if (source_pad != ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5280 asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
5281 atomisp_css_capture_configure_viewfinder(asd,
5282 vf_size.width, vf_size.height, 0,
5283 asd->video_out_vf.sh_fmt);
5287 if (asd->continuous_mode->val) {
5288 ret = __enable_continuous_mode(asd, true);
5293 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
5295 for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++)
5296 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipe_extra_configs[i].disable_vf_pp = asd->vfpp->val != ATOMISP_VFPP_ENABLE;
5298 /* ISP2401 new input system need to use copy pipe */
5299 if (asd->copy_mode) {
5300 pipe_id = IA_CSS_PIPE_ID_COPY;
5301 atomisp_css_capture_enable_online(asd, stream_index, false);
5302 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
5303 /* video same in continuouscapture and online modes */
5304 configure_output = atomisp_css_video_configure_output;
5305 get_frame_info = atomisp_css_video_get_output_frame_info;
5306 pipe_id = IA_CSS_PIPE_ID_VIDEO;
5307 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5308 if (!asd->continuous_mode->val) {
5309 configure_output = atomisp_css_video_configure_output;
5311 atomisp_css_video_get_output_frame_info;
5312 pipe_id = IA_CSS_PIPE_ID_VIDEO;
5314 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5315 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
5317 atomisp_css_video_configure_output;
5319 atomisp_css_video_get_output_frame_info;
5320 configure_pp_input =
5321 atomisp_css_video_configure_pp_input;
5322 pipe_id = IA_CSS_PIPE_ID_VIDEO;
5325 atomisp_css_capture_configure_output;
5327 atomisp_css_capture_get_output_frame_info;
5328 configure_pp_input =
5329 atomisp_css_capture_configure_pp_input;
5330 pipe_id = IA_CSS_PIPE_ID_CAPTURE;
5332 atomisp_update_capture_mode(asd);
5333 atomisp_css_capture_enable_online(asd, stream_index, false);
5336 } else if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
5337 configure_output = atomisp_css_preview_configure_output;
5338 get_frame_info = atomisp_css_preview_get_output_frame_info;
5339 configure_pp_input = atomisp_css_preview_configure_pp_input;
5340 pipe_id = IA_CSS_PIPE_ID_PREVIEW;
5342 /* CSS doesn't support low light mode on SOC cameras, so disable
5343 * it. FIXME: if this is done elsewhere, it gives corrupted
5344 * colors into thumbnail image.
5346 if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
5347 asd->params.low_light = false;
5349 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
5350 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
5351 atomisp_css_enable_dz(asd, false);
5353 atomisp_update_capture_mode(asd);
5356 if (!asd->continuous_mode->val)
5357 /* in case of ANR, force capture pipe to offline mode */
5358 atomisp_css_capture_enable_online(asd, stream_index,
5359 asd->params.low_light ?
5360 false : asd->params.online_process);
5362 configure_output = atomisp_css_capture_configure_output;
5363 get_frame_info = atomisp_css_capture_get_output_frame_info;
5364 configure_pp_input = atomisp_css_capture_configure_pp_input;
5365 pipe_id = IA_CSS_PIPE_ID_CAPTURE;
5367 if (!asd->params.online_process &&
5368 !asd->continuous_mode->val) {
5369 ret = atomisp_css_capture_get_output_raw_frame_info(asd,
5374 if (!asd->continuous_mode->val && asd->run_mode->val
5375 != ATOMISP_RUN_MODE_STILL_CAPTURE) {
5377 "Need to set the running mode first\n");
5378 asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE;
5383 * to SOC camera, use yuvpp pipe.
5385 if (ATOMISP_USE_YUVPP(asd))
5386 pipe_id = IA_CSS_PIPE_ID_YUVPP;
5389 ret = atomisp_css_copy_configure_output(asd, stream_index,
5390 pix->width, pix->height,
5391 format->planar ? pix->bytesperline :
5392 pix->bytesperline * 8 / format->depth,
5395 ret = configure_output(asd, pix->width, pix->height,
5396 format->planar ? pix->bytesperline :
5397 pix->bytesperline * 8 / format->depth,
5400 dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n",
5401 pix->width, pix->height, format->sh_fmt);
5405 ret = configure_pp_input(asd, isp_sink_crop->width, isp_sink_crop->height);
5407 dev_err(isp->dev, "configure_pp_input %ux%u\n",
5408 isp_sink_crop->width,
5409 isp_sink_crop->height);
5413 ret = atomisp_css_copy_get_output_frame_info(asd, stream_index,
5416 ret = get_frame_info(asd, output_info);
5418 dev_err(isp->dev, "__get_frame_info %ux%u (padded to %u) returned %d\n",
5419 pix->width, pix->height, pix->bytesperline, ret);
5423 atomisp_update_grid_info(asd, pipe_id, source_pad);
5425 /* Free the raw_dump buffer first */
5426 ia_css_frame_free(asd->raw_output_frame);
5427 asd->raw_output_frame = NULL;
5429 if (!asd->continuous_mode->val &&
5430 !asd->params.online_process && !isp->sw_contex.file_input &&
5431 ia_css_frame_allocate_from_info(&asd->raw_output_frame,
5438 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd,
5439 unsigned int width, unsigned int height,
5440 unsigned int *dvs_env_w, unsigned int *dvs_env_h)
5442 struct atomisp_device *isp = asd->isp;
5444 /* if subdev type is SOC camera,we do not need to set DVS */
5445 if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
5446 asd->params.video_dis_en = false;
5448 if (asd->params.video_dis_en &&
5449 asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5450 /* envelope is 20% of the output resolution */
5452 * dvs envelope cannot be round up.
5453 * it would cause ISP timeout and color switch issue
5455 *dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH);
5456 *dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT);
5459 asd->params.dis_proj_data_valid = false;
5460 asd->params.css_update_params_needed = true;
5463 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd,
5464 int source_pad, struct v4l2_pix_format *f)
5466 struct v4l2_mbus_framefmt *sink, *src;
5469 /* Only used for the new input system */
5470 asd->copy_mode = false;
5474 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5475 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK);
5476 src = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5477 V4L2_SUBDEV_FORMAT_ACTIVE, source_pad);
5479 if ((sink->code == src->code &&
5480 sink->width == f->width &&
5481 sink->height == f->height) ||
5482 ((asd->isp->inputs[asd->input_curr].type == SOC_CAMERA) &&
5483 (asd->isp->inputs[asd->input_curr].camera_caps->
5484 sensor[asd->sensor_curr].stream_num > 1)))
5485 asd->copy_mode = true;
5487 asd->copy_mode = false;
5489 dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode);
5492 static int atomisp_set_fmt_to_snr(struct video_device *vdev,
5493 struct v4l2_pix_format *f, unsigned int pixelformat,
5494 unsigned int padding_w, unsigned int padding_h,
5495 unsigned int dvs_env_w, unsigned int dvs_env_h)
5497 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
5498 struct atomisp_sub_device *asd = pipe->asd;
5499 const struct atomisp_format_bridge *format;
5500 struct v4l2_subdev_pad_config pad_cfg;
5501 struct v4l2_subdev_state pad_state = {
5504 struct v4l2_subdev_format vformat = {
5505 .which = V4L2_SUBDEV_FORMAT_TRY,
5507 struct v4l2_mbus_framefmt *ffmt = &vformat.format;
5508 struct v4l2_mbus_framefmt *req_ffmt;
5509 struct atomisp_device *isp;
5510 struct atomisp_input_stream_info *stream_info =
5511 (struct atomisp_input_stream_info *)ffmt->reserved;
5512 u16 stream_index = ATOMISP_INPUT_STREAM_GENERAL;
5513 int source_pad = atomisp_subdev_source_pad(vdev);
5514 struct v4l2_subdev_fh fh;
5518 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
5519 __func__, vdev->name);
5525 v4l2_fh_init(&fh.vfh, vdev);
5527 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
5529 format = atomisp_get_format_bridge(pixelformat);
5533 v4l2_fill_mbus_format(ffmt, f, format->mbus_code);
5534 ffmt->height += padding_h + dvs_env_h;
5535 ffmt->width += padding_w + dvs_env_w;
5537 dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n",
5538 ffmt->width, ffmt->height, padding_w, padding_h,
5539 dvs_env_w, dvs_env_h);
5541 __atomisp_init_stream_info(stream_index, stream_info);
5545 /* Disable dvs if resolution can't be supported by sensor */
5546 if (asd->params.video_dis_en &&
5547 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
5548 vformat.which = V4L2_SUBDEV_FORMAT_TRY;
5549 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
5550 pad, set_fmt, &pad_state, &vformat);
5554 dev_dbg(isp->dev, "video dis: sensor width: %d, height: %d\n",
5555 ffmt->width, ffmt->height);
5557 if (ffmt->width < req_ffmt->width ||
5558 ffmt->height < req_ffmt->height) {
5559 req_ffmt->height -= dvs_env_h;
5560 req_ffmt->width -= dvs_env_w;
5563 "can not enable video dis due to sensor limitation.");
5564 asd->params.video_dis_en = false;
5567 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
5568 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
5569 set_fmt, NULL, &vformat);
5573 __atomisp_update_stream_env(asd, stream_index, stream_info);
5575 dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
5576 ffmt->width, ffmt->height);
5578 if (ffmt->width < ATOM_ISP_STEP_WIDTH ||
5579 ffmt->height < ATOM_ISP_STEP_HEIGHT)
5582 if (asd->params.video_dis_en &&
5583 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
5584 (ffmt->width < req_ffmt->width || ffmt->height < req_ffmt->height)) {
5586 "can not enable video dis due to sensor limitation.");
5587 asd->params.video_dis_en = false;
5590 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
5591 V4L2_SUBDEV_FORMAT_ACTIVE,
5592 ATOMISP_SUBDEV_PAD_SINK, ffmt);
5594 return css_input_resolution_changed(asd, ffmt);
5597 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f)
5599 struct atomisp_device *isp = video_get_drvdata(vdev);
5600 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
5601 struct atomisp_sub_device *asd = pipe->asd;
5602 const struct atomisp_format_bridge *format_bridge;
5603 const struct atomisp_format_bridge *snr_format_bridge;
5604 struct ia_css_frame_info output_info, raw_output_info;
5605 struct v4l2_pix_format snr_fmt;
5606 struct v4l2_pix_format backup_fmt, s_fmt;
5607 unsigned int dvs_env_w = 0, dvs_env_h = 0;
5608 unsigned int padding_w = pad_w, padding_h = pad_h;
5609 bool res_overflow = false, crop_needs_override = false;
5610 struct v4l2_mbus_framefmt *isp_sink_fmt;
5611 struct v4l2_mbus_framefmt isp_source_fmt = {0};
5612 struct v4l2_subdev_format vformat = {
5613 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
5615 struct v4l2_mbus_framefmt *ffmt = &vformat.format;
5616 struct v4l2_rect isp_sink_crop;
5617 u16 source_pad = atomisp_subdev_source_pad(vdev);
5618 struct v4l2_subdev_fh fh;
5622 dev_err(isp->dev, "%s(): asd is NULL, device is %s\n",
5623 __func__, vdev->name);
5627 if (source_pad >= ATOMISP_SUBDEV_PADS_NUM)
5630 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED) {
5631 dev_warn(isp->dev, "ISP does not support set format while at streaming!\n");
5636 "setting resolution %ux%u on pad %u for asd%d, bytesperline %u\n",
5637 f->fmt.pix.width, f->fmt.pix.height, source_pad,
5638 asd->index, f->fmt.pix.bytesperline);
5640 v4l2_fh_init(&fh.vfh, vdev);
5642 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
5646 /* Currently, raw formats are broken!!! */
5648 if (format_bridge->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
5649 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
5651 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
5655 pipe->sh_fmt = format_bridge->sh_fmt;
5656 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
5658 /* Ensure that the resolution is equal or below the maximum supported */
5660 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
5661 v4l2_fill_mbus_format(ffmt, &f->fmt.pix, format_bridge->mbus_code);
5662 ffmt->height += padding_h;
5663 ffmt->width += padding_w;
5665 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
5666 set_fmt, NULL, &vformat);
5670 f->fmt.pix.width = ffmt->width - padding_w;
5671 f->fmt.pix.height = ffmt->height - padding_h;
5673 snr_fmt = f->fmt.pix;
5674 backup_fmt = snr_fmt;
5676 /**********************************************************************/
5678 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VF ||
5679 (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5680 && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)) {
5681 if (asd->fmt_auto->val) {
5682 struct v4l2_rect *capture_comp;
5683 struct v4l2_rect r = {0};
5685 r.width = f->fmt.pix.width;
5686 r.height = f->fmt.pix.height;
5688 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW)
5689 capture_comp = atomisp_subdev_get_rect(
5691 V4L2_SUBDEV_FORMAT_ACTIVE,
5692 ATOMISP_SUBDEV_PAD_SOURCE_VIDEO,
5693 V4L2_SEL_TGT_COMPOSE);
5695 capture_comp = atomisp_subdev_get_rect(
5697 V4L2_SUBDEV_FORMAT_ACTIVE,
5698 ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5699 V4L2_SEL_TGT_COMPOSE);
5701 if (capture_comp->width < r.width
5702 || capture_comp->height < r.height) {
5703 r.width = capture_comp->width;
5704 r.height = capture_comp->height;
5707 atomisp_subdev_set_selection(
5708 &asd->subdev, fh.state,
5709 V4L2_SUBDEV_FORMAT_ACTIVE, source_pad,
5710 V4L2_SEL_TGT_COMPOSE, 0, &r);
5712 f->fmt.pix.width = r.width;
5713 f->fmt.pix.height = r.height;
5716 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW &&
5717 (asd->isp->inputs[asd->input_curr].type == SOC_CAMERA) &&
5718 (asd->isp->inputs[asd->input_curr].camera_caps->
5719 sensor[asd->sensor_curr].stream_num > 1)) {
5720 /* For M10MO outputing YUV preview images. */
5722 atomisp_source_pad_to_stream_id(asd,
5723 ATOMISP_SUBDEV_PAD_SOURCE_VIDEO);
5725 ret = atomisp_css_copy_get_output_frame_info(asd,
5726 video_index, &output_info);
5729 "copy_get_output_frame_info ret %i", ret);
5732 if (!asd->yuvpp_mode) {
5734 * If viewfinder was configured into copy_mode,
5735 * we switch to using yuvpp pipe instead.
5737 asd->yuvpp_mode = true;
5738 ret = atomisp_css_copy_configure_output(
5739 asd, video_index, 0, 0, 0, 0);
5742 "failed to disable copy pipe");
5745 ret = atomisp_css_yuvpp_configure_output(
5747 output_info.res.width,
5748 output_info.res.height,
5749 output_info.padded_width,
5750 output_info.format);
5753 "failed to set up yuvpp pipe\n");
5756 atomisp_css_video_enable_online(asd, false);
5757 atomisp_css_preview_enable_online(asd,
5758 ATOMISP_INPUT_STREAM_GENERAL, false);
5760 atomisp_css_yuvpp_configure_viewfinder(asd, video_index,
5761 f->fmt.pix.width, f->fmt.pix.height,
5762 format_bridge->planar ? f->fmt.pix.bytesperline
5763 : f->fmt.pix.bytesperline * 8
5764 / format_bridge->depth, format_bridge->sh_fmt);
5765 atomisp_css_yuvpp_get_viewfinder_frame_info(
5766 asd, video_index, &output_info);
5767 } else if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
5768 atomisp_css_video_configure_viewfinder(asd,
5769 f->fmt.pix.width, f->fmt.pix.height,
5770 format_bridge->planar ? f->fmt.pix.bytesperline
5771 : f->fmt.pix.bytesperline * 8
5772 / format_bridge->depth, format_bridge->sh_fmt);
5773 atomisp_css_video_get_viewfinder_frame_info(asd,
5775 asd->copy_mode = false;
5777 atomisp_css_capture_configure_viewfinder(asd,
5778 f->fmt.pix.width, f->fmt.pix.height,
5779 format_bridge->planar ? f->fmt.pix.bytesperline
5780 : f->fmt.pix.bytesperline * 8
5781 / format_bridge->depth, format_bridge->sh_fmt);
5782 atomisp_css_capture_get_viewfinder_frame_info(asd,
5784 asd->copy_mode = false;
5790 * Check whether main resolution configured smaller
5791 * than snapshot resolution. If so, force main resolution
5792 * to be the same as snapshot resolution
5794 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE) {
5795 struct v4l2_rect *r;
5797 r = atomisp_subdev_get_rect(
5799 V4L2_SUBDEV_FORMAT_ACTIVE,
5800 ATOMISP_SUBDEV_PAD_SOURCE_VF, V4L2_SEL_TGT_COMPOSE);
5802 if (r->width && r->height
5803 && (r->width > f->fmt.pix.width
5804 || r->height > f->fmt.pix.height))
5806 "Main Resolution config smaller then Vf Resolution. Force to be equal with Vf Resolution.");
5809 /* Pipeline configuration done through subdevs. Bail out now. */
5810 if (!asd->fmt_auto->val)
5811 goto set_fmt_to_isp;
5813 /* get sensor resolution and format */
5814 ret = atomisp_try_fmt(vdev, &snr_fmt, &res_overflow);
5816 dev_warn(isp->dev, "Try format failed with error %d\n", ret);
5819 f->fmt.pix.width = snr_fmt.width;
5820 f->fmt.pix.height = snr_fmt.height;
5822 snr_format_bridge = atomisp_get_format_bridge(snr_fmt.pixelformat);
5823 if (!snr_format_bridge) {
5824 dev_warn(isp->dev, "Can't find bridge format\n");
5828 atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5829 V4L2_SUBDEV_FORMAT_ACTIVE,
5830 ATOMISP_SUBDEV_PAD_SINK)->code =
5831 snr_format_bridge->mbus_code;
5833 isp_sink_fmt = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5834 V4L2_SUBDEV_FORMAT_ACTIVE,
5835 ATOMISP_SUBDEV_PAD_SINK);
5837 isp_source_fmt.code = format_bridge->mbus_code;
5838 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
5839 V4L2_SUBDEV_FORMAT_ACTIVE,
5840 source_pad, &isp_source_fmt);
5842 if (!atomisp_subdev_format_conversion(asd, source_pad)) {
5845 } else if (IS_BYT) {
5850 /* construct resolution supported by isp */
5851 if (res_overflow && !asd->continuous_mode->val) {
5852 f->fmt.pix.width = rounddown(
5853 clamp_t(u32, f->fmt.pix.width - padding_w,
5855 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
5856 f->fmt.pix.height = rounddown(
5857 clamp_t(u32, f->fmt.pix.height - padding_h,
5858 ATOM_ISP_MIN_HEIGHT,
5859 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
5862 atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height,
5863 &dvs_env_w, &dvs_env_h);
5865 if (asd->continuous_mode->val) {
5866 struct v4l2_rect *r;
5868 r = atomisp_subdev_get_rect(
5870 V4L2_SUBDEV_FORMAT_ACTIVE,
5871 ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5872 V4L2_SEL_TGT_COMPOSE);
5874 * The ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE should get resolutions
5875 * properly set otherwise, it should not be the capture_pad.
5877 if (r->width && r->height)
5878 asd->capture_pad = ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE;
5880 asd->capture_pad = source_pad;
5882 asd->capture_pad = source_pad;
5885 * set format info to sensor
5886 * In continuous mode, resolution is set only if it is higher than
5887 * existing value. This because preview pipe will be configured after
5888 * capture pipe and usually has lower resolution than capture pipe.
5890 if (!asd->continuous_mode->val ||
5891 isp_sink_fmt->width < (f->fmt.pix.width + padding_w + dvs_env_w) ||
5892 isp_sink_fmt->height < (f->fmt.pix.height + padding_h +
5895 * For jpeg or custom raw format the sensor will return constant
5896 * width and height. Because we already had quried try_mbus_fmt,
5897 * f->fmt.pix.width and f->fmt.pix.height has been changed to
5898 * this fixed width and height. So we cannot select the correct
5899 * resolution with that information. So use the original width
5900 * and height while set_mbus_fmt() so actual resolutions are
5901 * being used in while set media bus format.
5904 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG ||
5905 f->fmt.pix.pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
5906 s_fmt.width = backup_fmt.width;
5907 s_fmt.height = backup_fmt.height;
5909 ret = atomisp_set_fmt_to_snr(vdev, &s_fmt,
5910 f->fmt.pix.pixelformat, padding_w,
5911 padding_h, dvs_env_w, dvs_env_h);
5914 "Set format to sensor failed with %d\n", ret);
5918 atomisp_csi_lane_config(isp);
5919 crop_needs_override = true;
5922 atomisp_check_copy_mode(asd, source_pad, &backup_fmt);
5923 asd->yuvpp_mode = false; /* Reset variable */
5925 isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL,
5926 V4L2_SUBDEV_FORMAT_ACTIVE,
5927 ATOMISP_SUBDEV_PAD_SINK,
5930 /* Try to enable YUV downscaling if ISP input is 10 % (either
5931 * width or height) bigger than the desired result. */
5932 if (isp_sink_crop.width * 9 / 10 < f->fmt.pix.width ||
5933 isp_sink_crop.height * 9 / 10 < f->fmt.pix.height ||
5934 (atomisp_subdev_format_conversion(asd, source_pad) &&
5935 ((asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
5936 !asd->continuous_mode->val) ||
5937 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) {
5938 /* for continuous mode, preview size might be smaller than
5939 * still capture size. if preview size still needs crop,
5940 * pick the larger one between crop size of preview and
5943 if (asd->continuous_mode->val
5944 && source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5945 && !crop_needs_override) {
5946 isp_sink_crop.width =
5947 max_t(unsigned int, f->fmt.pix.width,
5948 isp_sink_crop.width);
5949 isp_sink_crop.height =
5950 max_t(unsigned int, f->fmt.pix.height,
5951 isp_sink_crop.height);
5953 isp_sink_crop.width = f->fmt.pix.width;
5954 isp_sink_crop.height = f->fmt.pix.height;
5957 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5958 V4L2_SUBDEV_FORMAT_ACTIVE,
5959 ATOMISP_SUBDEV_PAD_SINK,
5961 V4L2_SEL_FLAG_KEEP_CONFIG,
5963 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5964 V4L2_SUBDEV_FORMAT_ACTIVE,
5965 source_pad, V4L2_SEL_TGT_COMPOSE,
5967 } else if (IS_MOFD) {
5968 struct v4l2_rect main_compose = {0};
5970 main_compose.width = isp_sink_crop.width;
5971 main_compose.height =
5972 DIV_ROUND_UP(main_compose.width * f->fmt.pix.height,
5974 if (main_compose.height > isp_sink_crop.height) {
5975 main_compose.height = isp_sink_crop.height;
5976 main_compose.width =
5977 DIV_ROUND_UP(main_compose.height *
5982 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5983 V4L2_SUBDEV_FORMAT_ACTIVE,
5985 V4L2_SEL_TGT_COMPOSE, 0,
5988 struct v4l2_rect sink_crop = {0};
5989 struct v4l2_rect main_compose = {0};
5991 main_compose.width = f->fmt.pix.width;
5992 main_compose.height = f->fmt.pix.height;
5994 /* WORKAROUND: this override is universally enabled in
5995 * GMIN to work around a CTS failures (GMINL-539)
5996 * which appears to be related by a hardware
5997 * performance limitation. It's unclear why this
5998 * particular code triggers the issue. */
5999 if (crop_needs_override) {
6000 if (isp_sink_crop.width * main_compose.height >
6001 isp_sink_crop.height * main_compose.width) {
6002 sink_crop.height = isp_sink_crop.height;
6003 sink_crop.width = DIV_NEAREST_STEP(
6007 ATOM_ISP_STEP_WIDTH);
6009 sink_crop.width = isp_sink_crop.width;
6010 sink_crop.height = DIV_NEAREST_STEP(
6014 ATOM_ISP_STEP_HEIGHT);
6016 atomisp_subdev_set_selection(&asd->subdev, fh.state,
6017 V4L2_SUBDEV_FORMAT_ACTIVE,
6018 ATOMISP_SUBDEV_PAD_SINK,
6020 V4L2_SEL_FLAG_KEEP_CONFIG,
6023 atomisp_subdev_set_selection(&asd->subdev, fh.state,
6024 V4L2_SUBDEV_FORMAT_ACTIVE,
6026 V4L2_SEL_TGT_COMPOSE, 0,
6031 ret = atomisp_set_fmt_to_isp(vdev, &output_info, &raw_output_info,
6032 &f->fmt.pix, source_pad);
6034 dev_warn(isp->dev, "Can't set format on ISP. Error %d\n", ret);
6038 pipe->pix.width = f->fmt.pix.width;
6039 pipe->pix.height = f->fmt.pix.height;
6040 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
6042 * FIXME: do we need to setup this differently, depending on the
6043 * sensor or the pipeline?
6045 pipe->pix.colorspace = V4L2_COLORSPACE_REC709;
6046 pipe->pix.ycbcr_enc = V4L2_YCBCR_ENC_709;
6047 pipe->pix.xfer_func = V4L2_XFER_FUNC_709;
6049 if (format_bridge->planar) {
6050 pipe->pix.bytesperline = output_info.padded_width;
6051 pipe->pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height *
6052 DIV_ROUND_UP(format_bridge->depth *
6053 output_info.padded_width, 8));
6055 pipe->pix.bytesperline =
6056 DIV_ROUND_UP(format_bridge->depth *
6057 output_info.padded_width, 8);
6058 pipe->pix.sizeimage =
6059 PAGE_ALIGN(f->fmt.pix.height * pipe->pix.bytesperline);
6061 dev_dbg(isp->dev, "%s: image size: %d, %d bytes per line\n",
6062 __func__, pipe->pix.sizeimage, pipe->pix.bytesperline);
6064 if (f->fmt.pix.field == V4L2_FIELD_ANY)
6065 f->fmt.pix.field = V4L2_FIELD_NONE;
6066 pipe->pix.field = f->fmt.pix.field;
6068 f->fmt.pix = pipe->pix;
6069 f->fmt.pix.priv = PAGE_ALIGN(pipe->pix.width *
6070 pipe->pix.height * 2);
6072 pipe->capq.field = f->fmt.pix.field;
6075 * If in video 480P case, no GFX throttle
6077 if (asd->run_mode->val == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
6078 f->fmt.pix.width == 720 && f->fmt.pix.height == 480)
6079 isp->need_gfx_throttle = false;
6081 isp->need_gfx_throttle = true;
6083 /* Report the needed sizes */
6084 f->fmt.pix.sizeimage = pipe->pix.sizeimage;
6085 f->fmt.pix.bytesperline = pipe->pix.bytesperline;
6087 dev_dbg(isp->dev, "%s: %dx%d, image size: %d, %d bytes per line\n",
6089 f->fmt.pix.width, f->fmt.pix.height,
6090 f->fmt.pix.sizeimage, f->fmt.pix.bytesperline);
6095 int atomisp_set_fmt_file(struct video_device *vdev, struct v4l2_format *f)
6097 struct atomisp_device *isp = video_get_drvdata(vdev);
6098 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
6099 struct atomisp_sub_device *asd = pipe->asd;
6100 struct v4l2_mbus_framefmt ffmt = {0};
6101 const struct atomisp_format_bridge *format_bridge;
6102 struct v4l2_subdev_fh fh;
6106 dev_err(isp->dev, "%s(): asd is NULL, device is %s\n",
6107 __func__, vdev->name);
6111 v4l2_fh_init(&fh.vfh, vdev);
6113 dev_dbg(isp->dev, "setting fmt %ux%u 0x%x for file inject\n",
6114 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
6115 ret = atomisp_try_fmt_file(isp, f);
6117 dev_err(isp->dev, "atomisp_try_fmt_file err: %d\n", ret);
6121 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
6122 if (!format_bridge) {
6123 dev_dbg(isp->dev, "atomisp_get_format_bridge err! fmt:0x%x\n",
6124 f->fmt.pix.pixelformat);
6128 pipe->pix = f->fmt.pix;
6129 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_FIFO);
6130 atomisp_css_input_configure_port(asd,
6131 __get_mipi_port(isp, ATOMISP_CAMERA_PORT_PRIMARY), 2, 0xffff4,
6133 ffmt.width = f->fmt.pix.width;
6134 ffmt.height = f->fmt.pix.height;
6135 ffmt.code = format_bridge->mbus_code;
6137 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
6138 V4L2_SUBDEV_FORMAT_ACTIVE,
6139 ATOMISP_SUBDEV_PAD_SINK, &ffmt);
6144 int atomisp_set_shading_table(struct atomisp_sub_device *asd,
6145 struct atomisp_shading_table *user_shading_table)
6147 struct ia_css_shading_table *shading_table;
6148 struct ia_css_shading_table *free_table;
6149 unsigned int len_table;
6153 if (!user_shading_table)
6156 if (!user_shading_table->enable) {
6157 asd->params.config.shading_table = NULL;
6158 asd->params.sc_en = false;
6162 /* If enabling, all tables must be set */
6163 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
6164 if (!user_shading_table->data[i])
6168 /* Shading table size per color */
6169 if (user_shading_table->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
6170 user_shading_table->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
6173 shading_table = atomisp_css_shading_table_alloc(
6174 user_shading_table->width, user_shading_table->height);
6178 len_table = user_shading_table->width * user_shading_table->height *
6179 ATOMISP_SC_TYPE_SIZE;
6180 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
6181 ret = copy_from_user(shading_table->data[i],
6182 (void __user *)user_shading_table->data[i],
6185 free_table = shading_table;
6190 shading_table->sensor_width = user_shading_table->sensor_width;
6191 shading_table->sensor_height = user_shading_table->sensor_height;
6192 shading_table->fraction_bits = user_shading_table->fraction_bits;
6194 free_table = asd->params.css_param.shading_table;
6195 asd->params.css_param.shading_table = shading_table;
6196 asd->params.config.shading_table = shading_table;
6197 asd->params.sc_en = true;
6201 atomisp_css_shading_table_free(free_table);
6206 /*Turn off ISP dphy */
6207 int atomisp_ospm_dphy_down(struct atomisp_device *isp)
6209 struct pci_dev *pdev = to_pci_dev(isp->dev);
6210 unsigned long flags;
6213 dev_dbg(isp->dev, "%s\n", __func__);
6215 /* if ISP timeout, we can force powerdown */
6216 if (isp->isp_timeout)
6219 if (!atomisp_dev_users(isp))
6222 spin_lock_irqsave(&isp->lock, flags);
6223 isp->sw_contex.power_state = ATOM_ISP_POWER_DOWN;
6224 spin_unlock_irqrestore(&isp->lock, flags);
6227 * MRFLD IUNIT DPHY is located in an always-power-on island
6228 * MRFLD HW design need all CSI ports are disabled before
6229 * powering down the IUNIT.
6231 pci_read_config_dword(pdev, MRFLD_PCI_CSI_CONTROL, ®);
6232 reg |= MRFLD_ALL_CSI_PORTS_OFF_MASK;
6233 pci_write_config_dword(pdev, MRFLD_PCI_CSI_CONTROL, reg);
6237 /*Turn on ISP dphy */
6238 int atomisp_ospm_dphy_up(struct atomisp_device *isp)
6240 unsigned long flags;
6242 dev_dbg(isp->dev, "%s\n", __func__);
6244 spin_lock_irqsave(&isp->lock, flags);
6245 isp->sw_contex.power_state = ATOM_ISP_POWER_UP;
6246 spin_unlock_irqrestore(&isp->lock, flags);
6251 int atomisp_exif_makernote(struct atomisp_sub_device *asd,
6252 struct atomisp_makernote_info *config)
6254 struct v4l2_control ctrl;
6255 struct atomisp_device *isp = asd->isp;
6257 ctrl.id = V4L2_CID_FOCAL_ABSOLUTE;
6259 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6260 dev_warn(isp->dev, "failed to g_ctrl for focal length\n");
6263 config->focal_length = ctrl.value;
6266 ctrl.id = V4L2_CID_FNUMBER_ABSOLUTE;
6268 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6269 dev_warn(isp->dev, "failed to g_ctrl for f-number\n");
6272 config->f_number_curr = ctrl.value;
6275 ctrl.id = V4L2_CID_FNUMBER_RANGE;
6277 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6278 dev_warn(isp->dev, "failed to g_ctrl for f number range\n");
6281 config->f_number_range = ctrl.value;
6287 int atomisp_offline_capture_configure(struct atomisp_sub_device *asd,
6288 struct atomisp_cont_capture_conf *cvf_config)
6290 struct v4l2_ctrl *c;
6293 * In case of M10MO ZSL capture case, we need to issue a separate
6294 * capture request to M10MO which will output captured jpeg image
6297 asd->isp->inputs[asd->input_curr].camera->ctrl_handler,
6298 V4L2_CID_START_ZSL_CAPTURE);
6302 dev_dbg(asd->isp->dev, "%s trigger ZSL capture request\n",
6304 /* TODO: use the cvf_config */
6305 ret = v4l2_ctrl_s_ctrl(c, 1);
6309 return v4l2_ctrl_s_ctrl(c, 0);
6312 asd->params.offline_parm = *cvf_config;
6314 if (asd->params.offline_parm.num_captures) {
6315 if (asd->streaming == ATOMISP_DEVICE_STREAMING_DISABLED) {
6316 unsigned int init_raw_num;
6318 if (asd->enable_raw_buffer_lock->val) {
6320 ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES_LOCK_EN;
6321 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
6322 asd->params.video_dis_en)
6324 ATOMISP_CSS2_NUM_DVS_FRAME_DELAY;
6327 ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES;
6330 /* TODO: this can be removed once user-space
6331 * has been updated to use control API */
6332 asd->continuous_raw_buffer_size->val =
6334 asd->continuous_raw_buffer_size->val,
6335 asd->params.offline_parm.
6336 num_captures + init_raw_num);
6337 asd->continuous_raw_buffer_size->val =
6338 min_t(int, ATOMISP_CONT_RAW_FRAMES,
6339 asd->continuous_raw_buffer_size->val);
6341 asd->continuous_mode->val = true;
6343 asd->continuous_mode->val = false;
6344 __enable_continuous_mode(asd, false);
6351 * set auto exposure metering window to camera sensor
6353 int atomisp_s_ae_window(struct atomisp_sub_device *asd,
6354 struct atomisp_ae_window *arg)
6356 struct atomisp_device *isp = asd->isp;
6357 /* Coverity CID 298071 - initialzize struct */
6358 struct v4l2_subdev_selection sel = { 0 };
6360 sel.r.left = arg->x_left;
6361 sel.r.top = arg->y_top;
6362 sel.r.width = arg->x_right - arg->x_left + 1;
6363 sel.r.height = arg->y_bottom - arg->y_top + 1;
6365 if (v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
6366 pad, set_selection, NULL, &sel)) {
6367 dev_err(isp->dev, "failed to call sensor set_selection.\n");
6374 int atomisp_flash_enable(struct atomisp_sub_device *asd, int num_frames)
6376 struct atomisp_device *isp = asd->isp;
6378 if (num_frames < 0) {
6379 dev_dbg(isp->dev, "%s ERROR: num_frames: %d\n", __func__,
6383 /* a requested flash is still in progress. */
6384 if (num_frames && asd->params.flash_state != ATOMISP_FLASH_IDLE) {
6385 dev_dbg(isp->dev, "%s flash busy: %d frames left: %d\n",
6386 __func__, asd->params.flash_state,
6387 asd->params.num_flash_frames);
6391 asd->params.num_flash_frames = num_frames;
6392 asd->params.flash_state = ATOMISP_FLASH_REQUESTED;
6396 int atomisp_source_pad_to_stream_id(struct atomisp_sub_device *asd,
6397 uint16_t source_pad)
6400 struct atomisp_device *isp = asd->isp;
6402 if (isp->inputs[asd->input_curr].camera_caps->
6403 sensor[asd->sensor_curr].stream_num == 1)
6404 return ATOMISP_INPUT_STREAM_GENERAL;
6406 switch (source_pad) {
6407 case ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE:
6408 stream_id = ATOMISP_INPUT_STREAM_CAPTURE;
6410 case ATOMISP_SUBDEV_PAD_SOURCE_VF:
6411 stream_id = ATOMISP_INPUT_STREAM_POSTVIEW;
6413 case ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW:
6414 stream_id = ATOMISP_INPUT_STREAM_PREVIEW;
6416 case ATOMISP_SUBDEV_PAD_SOURCE_VIDEO:
6417 stream_id = ATOMISP_INPUT_STREAM_VIDEO;
6420 stream_id = ATOMISP_INPUT_STREAM_GENERAL;
6426 bool atomisp_is_vf_pipe(struct atomisp_video_pipe *pipe)
6428 struct atomisp_sub_device *asd = pipe->asd;
6431 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
6432 __func__, pipe->vdev.name);
6436 if (pipe == &asd->video_out_vf)
6439 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
6440 pipe == &asd->video_out_preview)
6446 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id)
6448 struct atomisp_device *isp = asd->isp;
6450 if (!asd->enable_raw_buffer_lock->val) {
6451 dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__);
6454 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED) {
6455 dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n",
6456 __func__, exp_id, asd->streaming);
6459 if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) {
6460 dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id);
6466 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd)
6468 unsigned long flags;
6470 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6471 memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap));
6472 asd->raw_buffer_locked_count = 0;
6473 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6476 int atomisp_set_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
6479 unsigned long flags;
6481 if (__checking_exp_id(asd, exp_id))
6484 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6486 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6487 (*bitmap) |= (1 << bit);
6488 asd->raw_buffer_locked_count++;
6489 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6491 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
6492 __func__, exp_id, asd->raw_buffer_locked_count);
6494 /* Check if the raw buffer after next is still locked!!! */
6496 if (exp_id > ATOMISP_MAX_EXP_ID)
6497 exp_id -= ATOMISP_MAX_EXP_ID;
6498 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6500 if ((*bitmap) & (1 << bit)) {
6503 /* WORKAROUND unlock the raw buffer compulsively */
6504 ret = atomisp_css_exp_id_unlock(asd, exp_id);
6506 dev_err(asd->isp->dev,
6507 "%s exp_id is wrapping back to %d but force unlock failed,, err %d.\n",
6508 __func__, exp_id, ret);
6512 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6513 (*bitmap) &= ~(1 << bit);
6514 asd->raw_buffer_locked_count--;
6515 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6516 dev_warn(asd->isp->dev,
6517 "%s exp_id is wrapping back to %d but it is still locked so force unlock it, raw_buffer_locked_count %d\n",
6518 __func__, exp_id, asd->raw_buffer_locked_count);
6523 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id)
6526 unsigned long flags;
6529 if (__checking_exp_id(asd, exp_id))
6532 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6534 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6535 ret = ((*bitmap) & (1 << bit));
6536 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6540 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
6543 unsigned long flags;
6545 if (__is_raw_buffer_locked(asd, exp_id))
6548 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6550 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6551 (*bitmap) &= ~(1 << bit);
6552 asd->raw_buffer_locked_count--;
6553 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6555 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
6556 __func__, exp_id, asd->raw_buffer_locked_count);
6560 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id)
6562 struct atomisp_device *isp = asd->isp;
6563 int value = *exp_id;
6566 ret = __is_raw_buffer_locked(asd, value);
6568 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
6572 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
6573 ret = atomisp_css_exp_id_capture(asd, value);
6575 dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value);
6581 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id)
6583 struct atomisp_device *isp = asd->isp;
6584 int value = *exp_id;
6587 ret = __clear_raw_buffer_bitmap(asd, value);
6589 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
6593 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
6594 ret = atomisp_css_exp_id_unlock(asd, value);
6596 dev_err(isp->dev, "%s exp_id %d failed, err %d.\n",
6597 __func__, value, ret);
6602 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd,
6603 unsigned int *enable)
6610 value = *enable > 0;
6612 atomisp_en_dz_capt_pipe(asd, value);
6617 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event)
6619 if (!event || asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
6622 dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n",
6626 case V4L2_EVENT_FRAME_SYNC:
6627 atomisp_sof_event(asd);
6629 case V4L2_EVENT_FRAME_END:
6630 atomisp_eof_event(asd, 0);
6632 case V4L2_EVENT_ATOMISP_3A_STATS_READY:
6633 atomisp_3a_stats_ready_event(asd, 0);
6635 case V4L2_EVENT_ATOMISP_METADATA_READY:
6636 atomisp_metadata_ready_event(asd, 0);
6645 static int atomisp_get_pipe_id(struct atomisp_video_pipe *pipe)
6647 struct atomisp_sub_device *asd = pipe->asd;
6650 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
6651 __func__, pipe->vdev.name);
6655 if (ATOMISP_USE_YUVPP(asd)) {
6656 return IA_CSS_PIPE_ID_YUVPP;
6657 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
6658 return IA_CSS_PIPE_ID_VIDEO;
6659 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
6660 return IA_CSS_PIPE_ID_CAPTURE;
6661 } else if (pipe == &asd->video_out_video_capture) {
6662 return IA_CSS_PIPE_ID_VIDEO;
6663 } else if (pipe == &asd->video_out_vf) {
6664 return IA_CSS_PIPE_ID_CAPTURE;
6665 } else if (pipe == &asd->video_out_preview) {
6666 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
6667 return IA_CSS_PIPE_ID_VIDEO;
6669 return IA_CSS_PIPE_ID_PREVIEW;
6670 } else if (pipe == &asd->video_out_capture) {
6672 return IA_CSS_PIPE_ID_COPY;
6674 return IA_CSS_PIPE_ID_CAPTURE;
6678 dev_warn(asd->isp->dev, "%s failed to find proper pipe\n",
6680 return IA_CSS_PIPE_ID_CAPTURE;
6683 int atomisp_get_invalid_frame_num(struct video_device *vdev,
6684 int *invalid_frame_num)
6686 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
6687 struct atomisp_sub_device *asd = pipe->asd;
6688 enum ia_css_pipe_id pipe_id;
6689 struct ia_css_pipe_info p_info;
6693 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
6694 __func__, vdev->name);
6698 if (asd->isp->inputs[asd->input_curr].camera_caps->
6699 sensor[asd->sensor_curr].stream_num > 1) {
6701 *invalid_frame_num = 0;
6705 pipe_id = atomisp_get_pipe_id(pipe);
6706 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipes[pipe_id]) {
6707 dev_warn(asd->isp->dev,
6708 "%s pipe %d has not been created yet, do SET_FMT first!\n",
6713 ret = ia_css_pipe_get_info(
6714 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
6715 .pipes[pipe_id], &p_info);
6717 *invalid_frame_num = p_info.num_invalid_frames;
6720 dev_warn(asd->isp->dev, "%s get pipe infor failed %d\n",