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 if (buf_type == IA_CSS_BUFFER_TYPE_OUTPUT_FRAME)
862 return &asd->video_out_video_capture;
863 return &asd->video_out_preview;
865 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
866 /* For online preview or ZSL preview pipe. */
867 if (css_pipe_id == IA_CSS_PIPE_ID_PREVIEW ||
868 css_pipe_id == IA_CSS_PIPE_ID_COPY)
869 return &asd->video_out_preview;
871 /* For capture pipe. */
872 if (buf_type == IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME)
873 return &asd->video_out_vf;
874 return &asd->video_out_capture;
877 enum atomisp_metadata_type
878 atomisp_get_metadata_type(struct atomisp_sub_device *asd,
879 enum ia_css_pipe_id pipe_id)
881 if (!asd->continuous_mode->val)
882 return ATOMISP_MAIN_METADATA;
884 if (pipe_id == IA_CSS_PIPE_ID_CAPTURE) /* online capture pipe */
885 return ATOMISP_SEC_METADATA;
887 return ATOMISP_MAIN_METADATA;
890 void atomisp_buf_done(struct atomisp_sub_device *asd, int error,
891 enum ia_css_buffer_type buf_type,
892 enum ia_css_pipe_id css_pipe_id,
893 bool q_buffers, enum atomisp_input_stream_id stream_id)
895 struct videobuf_buffer *vb = NULL;
896 struct atomisp_video_pipe *pipe = NULL;
897 struct atomisp_css_buffer buffer;
898 bool requeue = false;
900 unsigned long irqflags;
901 struct ia_css_frame *frame = NULL;
902 struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf_tmp;
903 struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf_tmp;
904 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf_tmp;
905 enum atomisp_metadata_type md_type;
906 struct atomisp_device *isp = asd->isp;
907 struct v4l2_control ctrl;
908 bool reset_wdt_timer = false;
911 buf_type != IA_CSS_BUFFER_TYPE_METADATA &&
912 buf_type != IA_CSS_BUFFER_TYPE_3A_STATISTICS &&
913 buf_type != IA_CSS_BUFFER_TYPE_DIS_STATISTICS &&
914 buf_type != IA_CSS_BUFFER_TYPE_OUTPUT_FRAME &&
915 buf_type != IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
916 buf_type != IA_CSS_BUFFER_TYPE_RAW_OUTPUT_FRAME &&
917 buf_type != IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
918 buf_type != IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
919 dev_err(isp->dev, "%s, unsupported buffer type: %d\n",
924 memset(&buffer, 0, sizeof(struct atomisp_css_buffer));
925 buffer.css_buffer.type = buf_type;
926 err = atomisp_css_dequeue_buffer(asd, stream_id, css_pipe_id,
930 "atomisp_css_dequeue_buffer failed: 0x%x\n", err);
934 /* need to know the atomisp pipe for frame buffers */
935 pipe = __atomisp_get_pipe(asd, stream_id, css_pipe_id, buf_type);
937 dev_err(isp->dev, "error getting atomisp pipe\n");
942 case IA_CSS_BUFFER_TYPE_3A_STATISTICS:
943 list_for_each_entry_safe(s3a_buf, _s3a_buf_tmp,
944 &asd->s3a_stats_in_css, list) {
945 if (s3a_buf->s3a_data ==
946 buffer.css_buffer.data.stats_3a) {
947 list_del_init(&s3a_buf->list);
948 list_add_tail(&s3a_buf->list,
949 &asd->s3a_stats_ready);
954 asd->s3a_bufs_in_css[css_pipe_id]--;
955 atomisp_3a_stats_ready_event(asd, buffer.css_buffer.exp_id);
956 dev_dbg(isp->dev, "%s: s3a stat with exp_id %d is ready\n",
957 __func__, s3a_buf->s3a_data->exp_id);
959 case IA_CSS_BUFFER_TYPE_METADATA:
963 md_type = atomisp_get_metadata_type(asd, css_pipe_id);
964 list_for_each_entry_safe(md_buf, _md_buf_tmp,
965 &asd->metadata_in_css[md_type], list) {
966 if (md_buf->metadata ==
967 buffer.css_buffer.data.metadata) {
968 list_del_init(&md_buf->list);
969 list_add_tail(&md_buf->list,
970 &asd->metadata_ready[md_type]);
974 asd->metadata_bufs_in_css[stream_id][css_pipe_id]--;
975 atomisp_metadata_ready_event(asd, md_type);
976 dev_dbg(isp->dev, "%s: metadata with exp_id %d is ready\n",
977 __func__, md_buf->metadata->exp_id);
979 case IA_CSS_BUFFER_TYPE_DIS_STATISTICS:
980 list_for_each_entry_safe(dis_buf, _dis_buf_tmp,
981 &asd->dis_stats_in_css, list) {
982 if (dis_buf->dis_data ==
983 buffer.css_buffer.data.stats_dvs) {
984 spin_lock_irqsave(&asd->dis_stats_lock,
986 list_del_init(&dis_buf->list);
987 list_add(&dis_buf->list, &asd->dis_stats);
988 asd->params.dis_proj_data_valid = true;
989 spin_unlock_irqrestore(&asd->dis_stats_lock,
994 asd->dis_bufs_in_css--;
995 dev_dbg(isp->dev, "%s: dis stat with exp_id %d is ready\n",
996 __func__, dis_buf->dis_data->exp_id);
998 case IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME:
999 case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
1001 reset_wdt_timer = true;
1003 pipe->buffers_in_css--;
1004 frame = buffer.css_buffer.data.frame;
1013 * YUVPP doesn't set postview exp_id correctlly in SDV mode.
1014 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
1016 if (IS_BYT && buf_type == IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
1017 asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
1018 frame->exp_id = (asd->postview_exp_id++) %
1019 (ATOMISP_MAX_EXP_ID + 1);
1021 dev_dbg(isp->dev, "%s: vf frame with exp_id %d is ready\n",
1022 __func__, frame->exp_id);
1023 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
1024 if (frame->flash_state
1025 == IA_CSS_FRAME_FLASH_STATE_PARTIAL)
1026 dev_dbg(isp->dev, "%s thumb partially flashed\n",
1028 else if (frame->flash_state
1029 == IA_CSS_FRAME_FLASH_STATE_FULL)
1030 dev_dbg(isp->dev, "%s thumb completely flashed\n",
1033 dev_dbg(isp->dev, "%s thumb no flash in this frame\n",
1036 vb = atomisp_css_frame_to_vbuf(pipe, frame);
1039 pipe->frame_config_id[vb->i] = frame->isp_config_id;
1040 if (css_pipe_id == IA_CSS_PIPE_ID_CAPTURE &&
1041 asd->pending_capture_request > 0) {
1042 err = atomisp_css_offline_capture_configure(asd,
1043 asd->params.offline_parm.num_captures,
1044 asd->params.offline_parm.skip_frames,
1045 asd->params.offline_parm.offset);
1047 asd->pending_capture_request--;
1050 asd->re_trigger_capture = false;
1052 dev_dbg(isp->dev, "Trigger capture again for new buffer. err=%d\n",
1054 } else if (IS_ISP2401) {
1055 asd->re_trigger_capture = true;
1058 case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
1059 case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
1061 reset_wdt_timer = true;
1063 pipe->buffers_in_css--;
1064 frame = buffer.css_buffer.data.frame;
1074 * YUVPP doesn't set preview exp_id correctlly in ZSL mode.
1075 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
1077 if (IS_BYT && buf_type == IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
1078 asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
1079 frame->exp_id = (asd->preview_exp_id++) %
1080 (ATOMISP_MAX_EXP_ID + 1);
1082 dev_dbg(isp->dev, "%s: main frame with exp_id %d is ready\n",
1083 __func__, frame->exp_id);
1084 vb = atomisp_css_frame_to_vbuf(pipe, frame);
1090 /* free the parameters */
1091 if (pipe->frame_params[vb->i]) {
1092 if (asd->params.dvs_6axis ==
1093 pipe->frame_params[vb->i]->params.dvs_6axis)
1094 asd->params.dvs_6axis = NULL;
1095 atomisp_free_css_parameters(
1096 &pipe->frame_params[vb->i]->params);
1097 kvfree(pipe->frame_params[vb->i]);
1098 pipe->frame_params[vb->i] = NULL;
1101 pipe->frame_config_id[vb->i] = frame->isp_config_id;
1102 ctrl.id = V4L2_CID_FLASH_MODE;
1103 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
1104 if (frame->flash_state
1105 == IA_CSS_FRAME_FLASH_STATE_PARTIAL) {
1106 asd->frame_status[vb->i] =
1107 ATOMISP_FRAME_STATUS_FLASH_PARTIAL;
1108 dev_dbg(isp->dev, "%s partially flashed\n",
1110 } else if (frame->flash_state
1111 == IA_CSS_FRAME_FLASH_STATE_FULL) {
1112 asd->frame_status[vb->i] =
1113 ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
1114 asd->params.num_flash_frames--;
1115 dev_dbg(isp->dev, "%s completely flashed\n",
1118 asd->frame_status[vb->i] =
1119 ATOMISP_FRAME_STATUS_OK;
1121 "%s no flash in this frame\n",
1125 /* Check if flashing sequence is done */
1126 if (asd->frame_status[vb->i] ==
1127 ATOMISP_FRAME_STATUS_FLASH_EXPOSED)
1128 asd->params.flash_state = ATOMISP_FLASH_DONE;
1129 } else if (isp->flash) {
1130 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) ==
1131 0 && ctrl.value == ATOMISP_FLASH_MODE_TORCH) {
1132 ctrl.id = V4L2_CID_FLASH_TORCH_INTENSITY;
1133 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl)
1134 == 0 && ctrl.value > 0) {
1135 asd->frame_status[vb->i] =
1136 ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
1138 asd->frame_status[vb->i] =
1139 ATOMISP_FRAME_STATUS_OK;
1142 asd->frame_status[vb->i] =
1143 ATOMISP_FRAME_STATUS_OK;
1146 asd->frame_status[vb->i] = ATOMISP_FRAME_STATUS_OK;
1149 asd->params.last_frame_status = asd->frame_status[vb->i];
1151 if (asd->continuous_mode->val) {
1152 if (css_pipe_id == IA_CSS_PIPE_ID_PREVIEW ||
1153 css_pipe_id == IA_CSS_PIPE_ID_VIDEO) {
1154 asd->latest_preview_exp_id = frame->exp_id;
1155 } else if (css_pipe_id ==
1156 IA_CSS_PIPE_ID_CAPTURE) {
1157 if (asd->run_mode->val ==
1158 ATOMISP_RUN_MODE_VIDEO)
1159 dev_dbg(isp->dev, "SDV capture raw buffer id: %u\n",
1162 dev_dbg(isp->dev, "ZSL capture raw buffer id: %u\n",
1167 * Only after enabled the raw buffer lock
1168 * and in continuous mode.
1169 * in preview/video pipe, each buffer will
1170 * be locked automatically, so record it here.
1172 if (((css_pipe_id == IA_CSS_PIPE_ID_PREVIEW) ||
1173 (css_pipe_id == IA_CSS_PIPE_ID_VIDEO)) &&
1174 asd->enable_raw_buffer_lock->val &&
1175 asd->continuous_mode->val) {
1176 atomisp_set_raw_buffer_bitmap(asd, frame->exp_id);
1177 WARN_ON(frame->exp_id > ATOMISP_MAX_EXP_ID);
1180 if (asd->params.css_update_params_needed) {
1181 atomisp_apply_css_parameters(asd,
1182 &asd->params.css_param);
1183 if (asd->params.css_param.update_flag.dz_config)
1184 asd->params.config.dz_config = &asd->params.css_param.dz_config;
1185 /* New global dvs 6axis config should be blocked
1186 * here if there's a buffer with per-frame parameters
1187 * pending in CSS frame buffer queue.
1188 * This is to aviod zooming vibration since global
1189 * parameters take effect immediately while
1190 * per-frame parameters are taken after previous
1191 * buffers in CSS got processed.
1193 if (asd->params.dvs_6axis)
1194 atomisp_css_set_dvs_6axis(asd,
1195 asd->params.dvs_6axis);
1197 asd->params.css_update_params_needed = false;
1198 /* The update flag should not be cleaned here
1199 * since it is still going to be used to make up
1200 * following per-frame parameters.
1201 * This will introduce more copy work since each
1202 * time when updating global parameters, the whole
1203 * parameter set are applied.
1204 * FIXME: A new set of parameter copy functions can
1205 * be added to make up per-frame parameters based on
1206 * solid structures stored in asd->params.css_param
1207 * instead of using shadow pointers in update flag.
1209 atomisp_css_update_isp_params(asd);
1216 vb->ts = ktime_get_ns();
1217 vb->field_count = atomic_read(&asd->sequence) << 1;
1218 /*mark videobuffer done for dequeue*/
1219 spin_lock_irqsave(&pipe->irq_lock, irqflags);
1220 vb->state = !error ? VIDEOBUF_DONE : VIDEOBUF_ERROR;
1221 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
1224 * Frame capture done, wake up any process block on
1225 * current active buffer
1226 * possibly hold by videobuf_dqbuf()
1231 atomic_set(&pipe->wdt_count, 0);
1234 * Requeue should only be done for 3a and dis buffers.
1235 * Queue/dequeue order will change if driver recycles image buffers.
1238 err = atomisp_css_queue_buffer(asd,
1239 stream_id, css_pipe_id,
1242 dev_err(isp->dev, "%s, q to css fails: %d\n",
1246 if (!error && q_buffers)
1247 atomisp_qbuffers_to_css(asd);
1250 /* If there are no buffers queued then
1251 * delete wdt timer. */
1252 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1254 if (!atomisp_buffers_queued_pipe(pipe))
1255 atomisp_wdt_stop_pipe(pipe, false);
1256 else if (reset_wdt_timer)
1257 /* SOF irq should not reset wdt timer. */
1258 atomisp_wdt_refresh_pipe(pipe,
1259 ATOMISP_WDT_KEEP_CURRENT_DELAY);
1263 void atomisp_delayed_init_work(struct work_struct *work)
1265 struct atomisp_sub_device *asd = container_of(work,
1266 struct atomisp_sub_device,
1269 * to SOC camera, use yuvpp pipe and no support continuous mode.
1271 if (!ATOMISP_USE_YUVPP(asd)) {
1272 struct v4l2_event event = {0};
1273 struct ia_css_stream *stream;
1275 stream = asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
1278 if (ia_css_alloc_continuous_frame_remain(stream))
1281 ia_css_update_continuous_frames(stream);
1283 event.type = V4L2_EVENT_ATOMISP_RAW_BUFFERS_ALLOC_DONE;
1284 v4l2_event_queue(asd->subdev.devnode, &event);
1287 /* signal streamon after delayed init is done */
1288 asd->delayed_init = ATOMISP_DELAYED_INIT_DONE;
1289 complete(&asd->init_done);
1292 static void __atomisp_css_recover(struct atomisp_device *isp, bool isp_timeout)
1294 struct pci_dev *pdev = to_pci_dev(isp->dev);
1295 enum ia_css_pipe_id css_pipe_id;
1296 bool stream_restart[MAX_STREAM_NUM] = {0};
1297 bool depth_mode = false;
1298 int i, ret, depth_cnt = 0;
1300 if (!isp->sw_contex.file_input)
1301 atomisp_css_irq_enable(isp,
1302 IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
1304 BUG_ON(isp->num_of_streams > MAX_STREAM_NUM);
1306 for (i = 0; i < isp->num_of_streams; i++) {
1307 struct atomisp_sub_device *asd = &isp->asd[i];
1308 struct ia_css_pipeline *acc_pipeline;
1309 struct ia_css_pipe *acc_pipe = NULL;
1311 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED &&
1312 !asd->stream_prepared)
1316 * AtomISP::waitStageUpdate is blocked when WDT happens.
1317 * By calling acc_done() for all loaded fw_handles,
1318 * HAL will be unblocked.
1320 acc_pipe = asd->stream_env[i].pipes[IA_CSS_PIPE_ID_ACC];
1322 acc_pipeline = ia_css_pipe_get_pipeline(acc_pipe);
1324 struct ia_css_pipeline_stage *stage;
1326 for (stage = acc_pipeline->stages; stage;
1327 stage = stage->next) {
1328 const struct ia_css_fw_info *fw;
1330 fw = stage->firmware;
1331 atomisp_acc_done(asd, fw->handle);
1338 if (asd->delayed_init == ATOMISP_DELAYED_INIT_QUEUED)
1339 cancel_work_sync(&asd->delayed_init_work);
1341 complete(&asd->init_done);
1342 asd->delayed_init = ATOMISP_DELAYED_INIT_NOT_QUEUED;
1344 stream_restart[asd->index] = true;
1346 asd->streaming = ATOMISP_DEVICE_STREAMING_STOPPING;
1348 /* stream off sensor */
1349 ret = v4l2_subdev_call(
1350 isp->inputs[asd->input_curr].
1351 camera, video, s_stream, 0);
1354 "can't stop streaming on sensor!\n");
1356 atomisp_acc_unload_extensions(asd);
1358 atomisp_clear_css_buffer_counters(asd);
1360 css_pipe_id = atomisp_get_css_pipe_id(asd);
1361 atomisp_css_stop(asd, css_pipe_id, true);
1363 asd->streaming = ATOMISP_DEVICE_STREAMING_DISABLED;
1365 asd->preview_exp_id = 1;
1366 asd->postview_exp_id = 1;
1367 /* notify HAL the CSS reset */
1369 "send reset event to %s\n", asd->subdev.devnode->name);
1370 atomisp_reset_event(asd);
1374 disable_isp_irq(hrt_isp_css_irq_sp);
1375 clear_isp_irq(hrt_isp_css_irq_sp);
1377 /* Set the SRSE to 3 before resetting */
1378 pci_write_config_dword(pdev, PCI_I_CONTROL,
1379 isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
1381 /* reset ISP and restore its state */
1382 isp->isp_timeout = true;
1384 isp->isp_timeout = false;
1387 for (i = 0; i < isp->num_of_streams; i++) {
1388 if (isp->asd[i].depth_mode->val)
1393 for (i = 0; i < isp->num_of_streams; i++) {
1394 struct atomisp_sub_device *asd = &isp->asd[i];
1396 if (!stream_restart[i])
1399 if (isp->inputs[asd->input_curr].type != FILE_INPUT)
1400 atomisp_css_input_set_mode(asd,
1401 IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
1403 css_pipe_id = atomisp_get_css_pipe_id(asd);
1404 if (atomisp_css_start(asd, css_pipe_id, true))
1406 "start SP failed, so do not set streaming to be enable!\n");
1408 asd->streaming = ATOMISP_DEVICE_STREAMING_ENABLED;
1410 atomisp_csi2_configure(asd);
1413 if (!isp->sw_contex.file_input) {
1414 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
1415 atomisp_css_valid_sof(isp));
1417 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, true) < 0)
1418 dev_dbg(isp->dev, "DFS auto failed while recovering!\n");
1420 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_MAX, true) < 0)
1421 dev_dbg(isp->dev, "DFS max failed while recovering!\n");
1424 for (i = 0; i < isp->num_of_streams; i++) {
1425 struct atomisp_sub_device *asd;
1429 if (!stream_restart[i])
1432 if (asd->continuous_mode->val &&
1433 asd->delayed_init == ATOMISP_DELAYED_INIT_NOT_QUEUED) {
1434 reinit_completion(&asd->init_done);
1435 asd->delayed_init = ATOMISP_DELAYED_INIT_QUEUED;
1436 queue_work(asd->delayed_init_workq,
1437 &asd->delayed_init_work);
1440 * dequeueing buffers is not needed. CSS will recycle
1441 * buffers that it has.
1443 atomisp_flush_bufs_and_wakeup(asd);
1445 /* Requeue unprocessed per-frame parameters. */
1446 atomisp_recover_params_queue(&asd->video_out_capture);
1447 atomisp_recover_params_queue(&asd->video_out_preview);
1448 atomisp_recover_params_queue(&asd->video_out_video_capture);
1450 if ((asd->depth_mode->val) &&
1451 (depth_cnt == ATOMISP_DEPTH_SENSOR_STREAMON_COUNT)) {
1456 ret = v4l2_subdev_call(
1457 isp->inputs[asd->input_curr].camera, video,
1461 "can't start streaming on sensor!\n");
1465 if (atomisp_stream_on_master_slave_sensor(isp, true))
1467 "master slave sensor stream on failed!\n");
1471 void atomisp_wdt_work(struct work_struct *work)
1473 struct atomisp_device *isp = container_of(work, struct atomisp_device,
1476 unsigned int pipe_wdt_cnt[MAX_STREAM_NUM][4] = { {0} };
1477 bool css_recover = false;
1479 rt_mutex_lock(&isp->mutex);
1480 if (!atomisp_streaming_count(isp)) {
1481 atomic_set(&isp->wdt_work_queued, 0);
1482 rt_mutex_unlock(&isp->mutex);
1487 dev_err(isp->dev, "timeout %d of %d\n",
1488 atomic_read(&isp->wdt_count) + 1,
1489 ATOMISP_ISP_MAX_TIMEOUT_COUNT);
1491 if (atomic_inc_return(&isp->wdt_count) < ATOMISP_ISP_MAX_TIMEOUT_COUNT)
1496 for (i = 0; i < isp->num_of_streams; i++) {
1497 struct atomisp_sub_device *asd = &isp->asd[i];
1499 pipe_wdt_cnt[i][0] +=
1500 atomic_read(&asd->video_out_capture.wdt_count);
1501 pipe_wdt_cnt[i][1] +=
1502 atomic_read(&asd->video_out_vf.wdt_count);
1503 pipe_wdt_cnt[i][2] +=
1504 atomic_read(&asd->video_out_preview.wdt_count);
1505 pipe_wdt_cnt[i][3] +=
1506 atomic_read(&asd->video_out_video_capture.wdt_count);
1508 (pipe_wdt_cnt[i][0] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1509 pipe_wdt_cnt[i][1] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1510 pipe_wdt_cnt[i][2] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT &&
1511 pipe_wdt_cnt[i][3] <= ATOMISP_ISP_MAX_TIMEOUT_COUNT)
1514 "pipe on asd%d timeout cnt: (%d, %d, %d, %d) of %d, recover = %d\n",
1515 asd->index, pipe_wdt_cnt[i][0], pipe_wdt_cnt[i][1],
1516 pipe_wdt_cnt[i][2], pipe_wdt_cnt[i][3],
1517 ATOMISP_ISP_MAX_TIMEOUT_COUNT, css_recover);
1522 ia_css_debug_dump_sp_sw_debug_info();
1523 ia_css_debug_dump_debug_info(__func__);
1524 for (i = 0; i < isp->num_of_streams; i++) {
1525 struct atomisp_sub_device *asd = &isp->asd[i];
1527 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1529 dev_err(isp->dev, "%s, vdev %s buffers in css: %d\n",
1531 asd->video_out_capture.vdev.name,
1532 asd->video_out_capture.
1535 "%s, vdev %s buffers in css: %d\n",
1537 asd->video_out_vf.vdev.name,
1541 "%s, vdev %s buffers in css: %d\n",
1543 asd->video_out_preview.vdev.name,
1544 asd->video_out_preview.
1547 "%s, vdev %s buffers in css: %d\n",
1549 asd->video_out_video_capture.vdev.name,
1550 asd->video_out_video_capture.
1553 "%s, s3a buffers in css preview pipe:%d\n",
1555 asd->s3a_bufs_in_css[IA_CSS_PIPE_ID_PREVIEW]);
1557 "%s, s3a buffers in css capture pipe:%d\n",
1559 asd->s3a_bufs_in_css[IA_CSS_PIPE_ID_CAPTURE]);
1561 "%s, s3a buffers in css video pipe:%d\n",
1563 asd->s3a_bufs_in_css[IA_CSS_PIPE_ID_VIDEO]);
1565 "%s, dis buffers in css: %d\n",
1566 __func__, asd->dis_bufs_in_css);
1568 "%s, metadata buffers in css preview pipe:%d\n",
1570 asd->metadata_bufs_in_css
1571 [ATOMISP_INPUT_STREAM_GENERAL]
1572 [IA_CSS_PIPE_ID_PREVIEW]);
1574 "%s, metadata buffers in css capture pipe:%d\n",
1576 asd->metadata_bufs_in_css
1577 [ATOMISP_INPUT_STREAM_GENERAL]
1578 [IA_CSS_PIPE_ID_CAPTURE]);
1580 "%s, metadata buffers in css video pipe:%d\n",
1582 asd->metadata_bufs_in_css
1583 [ATOMISP_INPUT_STREAM_GENERAL]
1584 [IA_CSS_PIPE_ID_VIDEO]);
1585 if (asd->enable_raw_buffer_lock->val) {
1588 dev_err(isp->dev, "%s, raw_buffer_locked_count %d\n",
1589 __func__, asd->raw_buffer_locked_count);
1590 for (j = 0; j <= ATOMISP_MAX_EXP_ID / 32; j++)
1591 dev_err(isp->dev, "%s, raw_buffer_bitmap[%d]: 0x%x\n",
1593 asd->raw_buffer_bitmap[j]);
1597 /*sh_css_dump_sp_state();*/
1598 /*sh_css_dump_isp_state();*/
1600 for (i = 0; i < isp->num_of_streams; i++) {
1601 struct atomisp_sub_device *asd = &isp->asd[i];
1603 if (asd->streaming ==
1604 ATOMISP_DEVICE_STREAMING_ENABLED) {
1605 atomisp_clear_css_buffer_counters(asd);
1606 atomisp_flush_bufs_and_wakeup(asd);
1607 complete(&asd->init_done);
1610 atomisp_wdt_stop(asd, false);
1614 atomic_set(&isp->wdt_count, 0);
1616 isp->isp_fatal_error = true;
1617 atomic_set(&isp->wdt_work_queued, 0);
1619 rt_mutex_unlock(&isp->mutex);
1624 __atomisp_css_recover(isp, true);
1626 for (i = 0; i < isp->num_of_streams; i++) {
1627 struct atomisp_sub_device *asd = &isp->asd[i];
1629 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1632 atomisp_wdt_refresh(asd,
1633 isp->sw_contex.file_input ?
1634 ATOMISP_ISP_FILE_TIMEOUT_DURATION :
1635 ATOMISP_ISP_TIMEOUT_DURATION);
1639 dev_err(isp->dev, "timeout recovery handling done\n");
1640 atomic_set(&isp->wdt_work_queued, 0);
1642 rt_mutex_unlock(&isp->mutex);
1645 void atomisp_css_flush(struct atomisp_device *isp)
1649 if (!atomisp_streaming_count(isp))
1653 for (i = 0; i < isp->num_of_streams; i++) {
1654 struct atomisp_sub_device *asd = &isp->asd[i];
1656 atomisp_wdt_stop(asd, true);
1660 __atomisp_css_recover(isp, false);
1662 for (i = 0; i < isp->num_of_streams; i++) {
1663 struct atomisp_sub_device *asd = &isp->asd[i];
1665 if (asd->streaming !=
1666 ATOMISP_DEVICE_STREAMING_ENABLED)
1669 atomisp_wdt_refresh(asd,
1670 isp->sw_contex.file_input ?
1671 ATOMISP_ISP_FILE_TIMEOUT_DURATION :
1672 ATOMISP_ISP_TIMEOUT_DURATION);
1674 dev_dbg(isp->dev, "atomisp css flush done\n");
1677 void atomisp_wdt(struct timer_list *t)
1679 struct atomisp_sub_device *asd;
1680 struct atomisp_device *isp;
1683 asd = from_timer(asd, t, wdt);
1686 struct atomisp_video_pipe *pipe = from_timer(pipe, t, wdt);
1691 atomic_inc(&pipe->wdt_count);
1693 "[WARNING]asd %d pipe %s ISP timeout %d!\n",
1694 asd->index, pipe->vdev.name,
1695 atomic_read(&pipe->wdt_count));
1698 if (atomic_read(&isp->wdt_work_queued)) {
1699 dev_dbg(isp->dev, "ISP watchdog was put into workqueue\n");
1702 atomic_set(&isp->wdt_work_queued, 1);
1703 queue_work(isp->wdt_work_queue, &isp->wdt_work);
1707 void atomisp_wdt_start(struct atomisp_sub_device *asd)
1709 atomisp_wdt_refresh(asd, ATOMISP_ISP_TIMEOUT_DURATION);
1713 void atomisp_wdt_refresh_pipe(struct atomisp_video_pipe *pipe,
1718 if (delay != ATOMISP_WDT_KEEP_CURRENT_DELAY)
1719 pipe->wdt_duration = delay;
1721 next = jiffies + pipe->wdt_duration;
1723 /* Override next if it has been pushed beyon the "next" time */
1724 if (atomisp_is_wdt_running(pipe) && time_after(pipe->wdt_expires, next))
1725 next = pipe->wdt_expires;
1727 pipe->wdt_expires = next;
1729 if (atomisp_is_wdt_running(pipe))
1730 dev_dbg(pipe->asd->isp->dev, "WDT will hit after %d ms (%s)\n",
1731 ((int)(next - jiffies) * 1000 / HZ), pipe->vdev.name);
1733 dev_dbg(pipe->asd->isp->dev, "WDT starts with %d ms period (%s)\n",
1734 ((int)(next - jiffies) * 1000 / HZ), pipe->vdev.name);
1736 mod_timer(&pipe->wdt, next);
1739 void atomisp_wdt_refresh(struct atomisp_sub_device *asd, unsigned int delay)
1744 if (delay != ATOMISP_WDT_KEEP_CURRENT_DELAY)
1745 asd->wdt_duration = delay;
1747 next = jiffies + asd->wdt_duration;
1749 /* Override next if it has been pushed beyon the "next" time */
1750 if (atomisp_is_wdt_running(asd) && time_after(asd->wdt_expires, next))
1751 next = asd->wdt_expires;
1753 asd->wdt_expires = next;
1755 if (atomisp_is_wdt_running(asd))
1756 dev_dbg(asd->isp->dev, "WDT will hit after %d ms\n",
1757 ((int)(next - jiffies) * 1000 / HZ));
1759 dev_dbg(asd->isp->dev, "WDT starts with %d ms period\n",
1760 ((int)(next - jiffies) * 1000 / HZ));
1762 mod_timer(&asd->wdt, next);
1763 atomic_set(&asd->isp->wdt_count, 0);
1765 dev_dbg(asd->isp->dev, "WDT refresh all:\n");
1766 if (atomisp_is_wdt_running(&asd->video_out_capture))
1767 atomisp_wdt_refresh_pipe(&asd->video_out_capture, delay);
1768 if (atomisp_is_wdt_running(&asd->video_out_preview))
1769 atomisp_wdt_refresh_pipe(&asd->video_out_preview, delay);
1770 if (atomisp_is_wdt_running(&asd->video_out_vf))
1771 atomisp_wdt_refresh_pipe(&asd->video_out_vf, delay);
1772 if (atomisp_is_wdt_running(&asd->video_out_video_capture))
1773 atomisp_wdt_refresh_pipe(&asd->video_out_video_capture, delay);
1778 void atomisp_wdt_stop_pipe(struct atomisp_video_pipe *pipe, bool sync)
1780 if (!atomisp_is_wdt_running(pipe))
1783 dev_dbg(pipe->asd->isp->dev,
1784 "WDT stop asd %d (%s)\n", pipe->asd->index, pipe->vdev.name);
1787 del_timer_sync(&pipe->wdt);
1788 cancel_work_sync(&pipe->asd->isp->wdt_work);
1790 del_timer(&pipe->wdt);
1795 void atomisp_wdt_start_pipe(struct atomisp_video_pipe *pipe)
1797 atomisp_wdt_refresh_pipe(pipe, ATOMISP_ISP_TIMEOUT_DURATION);
1800 void atomisp_wdt_stop(struct atomisp_sub_device *asd, bool sync)
1802 dev_dbg(asd->isp->dev, "WDT stop:\n");
1806 del_timer_sync(&asd->wdt);
1807 cancel_work_sync(&asd->isp->wdt_work);
1809 del_timer(&asd->wdt);
1812 atomisp_wdt_stop_pipe(&asd->video_out_capture, sync);
1813 atomisp_wdt_stop_pipe(&asd->video_out_preview, sync);
1814 atomisp_wdt_stop_pipe(&asd->video_out_vf, sync);
1815 atomisp_wdt_stop_pipe(&asd->video_out_video_capture, sync);
1819 void atomisp_setup_flash(struct atomisp_sub_device *asd)
1821 struct atomisp_device *isp = asd->isp;
1822 struct v4l2_control ctrl;
1827 if (asd->params.flash_state != ATOMISP_FLASH_REQUESTED &&
1828 asd->params.flash_state != ATOMISP_FLASH_DONE)
1831 if (asd->params.num_flash_frames) {
1832 /* make sure the timeout is set before setting flash mode */
1833 ctrl.id = V4L2_CID_FLASH_TIMEOUT;
1834 ctrl.value = FLASH_TIMEOUT;
1836 if (v4l2_s_ctrl(NULL, isp->flash->ctrl_handler, &ctrl)) {
1837 dev_err(isp->dev, "flash timeout configure failed\n");
1841 ia_css_stream_request_flash(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream);
1843 asd->params.flash_state = ATOMISP_FLASH_ONGOING;
1845 asd->params.flash_state = ATOMISP_FLASH_IDLE;
1849 irqreturn_t atomisp_isr_thread(int irq, void *isp_ptr)
1851 struct atomisp_device *isp = isp_ptr;
1852 unsigned long flags;
1853 bool frame_done_found[MAX_STREAM_NUM] = {0};
1854 bool css_pipe_done[MAX_STREAM_NUM] = {0};
1856 struct atomisp_sub_device *asd;
1858 dev_dbg(isp->dev, ">%s\n", __func__);
1860 spin_lock_irqsave(&isp->lock, flags);
1862 if (!atomisp_streaming_count(isp) && !atomisp_is_acc_enabled(isp)) {
1863 spin_unlock_irqrestore(&isp->lock, flags);
1867 spin_unlock_irqrestore(&isp->lock, flags);
1870 * The standard CSS2.0 API tells the following calling sequence of
1871 * dequeue ready buffers:
1872 * while (ia_css_dequeue_event(...)) {
1873 * switch (event.type) {
1875 * ia_css_pipe_dequeue_buffer()
1878 * That is, dequeue event and buffer are one after another.
1880 * But the following implementation is to first deuque all the event
1881 * to a FIFO, then process the event in the FIFO.
1882 * This will not have issue in single stream mode, but it do have some
1883 * issue in multiple stream case. The issue is that
1884 * ia_css_pipe_dequeue_buffer() will not return the corrent buffer in
1887 * This is due to ia_css_pipe_dequeue_buffer() does not take the
1888 * ia_css_pipe parameter.
1891 * For CSS2.0: we change the way to not dequeue all the event at one
1892 * time, instead, dequue one and process one, then another
1894 rt_mutex_lock(&isp->mutex);
1895 if (atomisp_css_isr_thread(isp, frame_done_found, css_pipe_done))
1898 for (i = 0; i < isp->num_of_streams; i++) {
1900 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1902 atomisp_setup_flash(asd);
1905 rt_mutex_unlock(&isp->mutex);
1906 for (i = 0; i < isp->num_of_streams; i++) {
1908 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED
1909 && css_pipe_done[asd->index]
1910 && isp->sw_contex.file_input)
1911 v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1912 video, s_stream, 1);
1913 /* FIXME! FIX ACC implementation */
1914 if (asd->acc.pipeline && css_pipe_done[asd->index])
1915 atomisp_css_acc_done(asd);
1917 dev_dbg(isp->dev, "<%s\n", __func__);
1923 * utils for buffer allocation/free
1926 int atomisp_get_frame_pgnr(struct atomisp_device *isp,
1927 const struct ia_css_frame *frame, u32 *p_pgnr)
1930 dev_err(isp->dev, "%s: NULL frame pointer ERROR.\n", __func__);
1934 *p_pgnr = DIV_ROUND_UP(frame->data_bytes, PAGE_SIZE);
1939 * Get internal fmt according to V4L2 fmt
1941 static enum ia_css_frame_format
1942 v4l2_fmt_to_sh_fmt(u32 fmt)
1945 case V4L2_PIX_FMT_YUV420:
1946 return IA_CSS_FRAME_FORMAT_YUV420;
1947 case V4L2_PIX_FMT_YVU420:
1948 return IA_CSS_FRAME_FORMAT_YV12;
1949 case V4L2_PIX_FMT_YUV422P:
1950 return IA_CSS_FRAME_FORMAT_YUV422;
1951 case V4L2_PIX_FMT_YUV444:
1952 return IA_CSS_FRAME_FORMAT_YUV444;
1953 case V4L2_PIX_FMT_NV12:
1954 return IA_CSS_FRAME_FORMAT_NV12;
1955 case V4L2_PIX_FMT_NV21:
1956 return IA_CSS_FRAME_FORMAT_NV21;
1957 case V4L2_PIX_FMT_NV16:
1958 return IA_CSS_FRAME_FORMAT_NV16;
1959 case V4L2_PIX_FMT_NV61:
1960 return IA_CSS_FRAME_FORMAT_NV61;
1961 case V4L2_PIX_FMT_UYVY:
1962 return IA_CSS_FRAME_FORMAT_UYVY;
1963 case V4L2_PIX_FMT_YUYV:
1964 return IA_CSS_FRAME_FORMAT_YUYV;
1965 case V4L2_PIX_FMT_RGB24:
1966 return IA_CSS_FRAME_FORMAT_PLANAR_RGB888;
1967 case V4L2_PIX_FMT_RGB32:
1968 return IA_CSS_FRAME_FORMAT_RGBA888;
1969 case V4L2_PIX_FMT_RGB565:
1970 return IA_CSS_FRAME_FORMAT_RGB565;
1971 case V4L2_PIX_FMT_JPEG:
1972 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1973 return IA_CSS_FRAME_FORMAT_BINARY_8;
1974 case V4L2_PIX_FMT_SBGGR16:
1975 case V4L2_PIX_FMT_SBGGR10:
1976 case V4L2_PIX_FMT_SGBRG10:
1977 case V4L2_PIX_FMT_SGRBG10:
1978 case V4L2_PIX_FMT_SRGGB10:
1979 case V4L2_PIX_FMT_SBGGR12:
1980 case V4L2_PIX_FMT_SGBRG12:
1981 case V4L2_PIX_FMT_SGRBG12:
1982 case V4L2_PIX_FMT_SRGGB12:
1983 case V4L2_PIX_FMT_SBGGR8:
1984 case V4L2_PIX_FMT_SGBRG8:
1985 case V4L2_PIX_FMT_SGRBG8:
1986 case V4L2_PIX_FMT_SRGGB8:
1987 return IA_CSS_FRAME_FORMAT_RAW;
1994 * raw format match between SH format and V4L2 format
1996 static int raw_output_format_match_input(u32 input, u32 output)
1998 if ((input == ATOMISP_INPUT_FORMAT_RAW_12) &&
1999 ((output == V4L2_PIX_FMT_SRGGB12) ||
2000 (output == V4L2_PIX_FMT_SGRBG12) ||
2001 (output == V4L2_PIX_FMT_SBGGR12) ||
2002 (output == V4L2_PIX_FMT_SGBRG12)))
2005 if ((input == ATOMISP_INPUT_FORMAT_RAW_10) &&
2006 ((output == V4L2_PIX_FMT_SRGGB10) ||
2007 (output == V4L2_PIX_FMT_SGRBG10) ||
2008 (output == V4L2_PIX_FMT_SBGGR10) ||
2009 (output == V4L2_PIX_FMT_SGBRG10)))
2012 if ((input == ATOMISP_INPUT_FORMAT_RAW_8) &&
2013 ((output == V4L2_PIX_FMT_SRGGB8) ||
2014 (output == V4L2_PIX_FMT_SGRBG8) ||
2015 (output == V4L2_PIX_FMT_SBGGR8) ||
2016 (output == V4L2_PIX_FMT_SGBRG8)))
2019 if ((input == ATOMISP_INPUT_FORMAT_RAW_16) && (output == V4L2_PIX_FMT_SBGGR16))
2025 static u32 get_pixel_depth(u32 pixelformat)
2027 switch (pixelformat) {
2028 case V4L2_PIX_FMT_YUV420:
2029 case V4L2_PIX_FMT_NV12:
2030 case V4L2_PIX_FMT_NV21:
2031 case V4L2_PIX_FMT_YVU420:
2033 case V4L2_PIX_FMT_YUV422P:
2034 case V4L2_PIX_FMT_YUYV:
2035 case V4L2_PIX_FMT_UYVY:
2036 case V4L2_PIX_FMT_NV16:
2037 case V4L2_PIX_FMT_NV61:
2038 case V4L2_PIX_FMT_RGB565:
2039 case V4L2_PIX_FMT_SBGGR16:
2040 case V4L2_PIX_FMT_SBGGR12:
2041 case V4L2_PIX_FMT_SGBRG12:
2042 case V4L2_PIX_FMT_SGRBG12:
2043 case V4L2_PIX_FMT_SRGGB12:
2044 case V4L2_PIX_FMT_SBGGR10:
2045 case V4L2_PIX_FMT_SGBRG10:
2046 case V4L2_PIX_FMT_SGRBG10:
2047 case V4L2_PIX_FMT_SRGGB10:
2049 case V4L2_PIX_FMT_RGB24:
2050 case V4L2_PIX_FMT_YUV444:
2052 case V4L2_PIX_FMT_RGB32:
2054 case V4L2_PIX_FMT_JPEG:
2055 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
2056 case V4L2_PIX_FMT_SBGGR8:
2057 case V4L2_PIX_FMT_SGBRG8:
2058 case V4L2_PIX_FMT_SGRBG8:
2059 case V4L2_PIX_FMT_SRGGB8:
2062 return 8 * 2; /* raw type now */
2066 bool atomisp_is_mbuscode_raw(uint32_t code)
2068 return code >= 0x3000 && code < 0x4000;
2072 * ISP features control function
2076 * Set ISP capture mode based on current settings
2078 static void atomisp_update_capture_mode(struct atomisp_sub_device *asd)
2080 if (asd->params.gdc_cac_en)
2081 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_ADVANCED);
2082 else if (asd->params.low_light)
2083 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_LOW_LIGHT);
2084 else if (asd->video_out_capture.sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
2085 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
2087 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
2091 int atomisp_set_sensor_runmode(struct atomisp_sub_device *asd,
2092 struct atomisp_s_runmode *runmode)
2094 struct atomisp_device *isp = asd->isp;
2095 struct v4l2_ctrl *c;
2098 if (!(runmode && (runmode->mode & RUNMODE_MASK)))
2101 mutex_lock(asd->ctrl_handler.lock);
2102 c = v4l2_ctrl_find(isp->inputs[asd->input_curr].camera->ctrl_handler,
2106 ret = v4l2_ctrl_s_ctrl(c, runmode->mode);
2108 mutex_unlock(asd->ctrl_handler.lock);
2113 * Function to enable/disable lens geometry distortion correction (GDC) and
2114 * chromatic aberration correction (CAC)
2116 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag,
2120 *value = asd->params.gdc_cac_en;
2124 asd->params.gdc_cac_en = !!*value;
2125 if (asd->params.gdc_cac_en) {
2126 asd->params.config.morph_table = asd->params.css_param.morph_table;
2128 asd->params.config.morph_table = NULL;
2130 asd->params.css_update_params_needed = true;
2131 atomisp_update_capture_mode(asd);
2136 * Function to enable/disable low light mode including ANR
2138 int atomisp_low_light(struct atomisp_sub_device *asd, int flag,
2142 *value = asd->params.low_light;
2146 asd->params.low_light = (*value != 0);
2147 atomisp_update_capture_mode(asd);
2152 * Function to enable/disable extra noise reduction (XNR) in low light
2155 int atomisp_xnr(struct atomisp_sub_device *asd, int flag,
2159 *xnr_enable = asd->params.xnr_en;
2163 atomisp_css_capture_enable_xnr(asd, !!*xnr_enable);
2169 * Function to configure bayer noise reduction
2171 int atomisp_nr(struct atomisp_sub_device *asd, int flag,
2172 struct atomisp_nr_config *arg)
2175 /* Get nr config from current setup */
2176 if (atomisp_css_get_nr_config(asd, arg))
2179 /* Set nr config to isp parameters */
2180 memcpy(&asd->params.css_param.nr_config, arg,
2181 sizeof(struct ia_css_nr_config));
2182 asd->params.config.nr_config = &asd->params.css_param.nr_config;
2183 asd->params.css_update_params_needed = true;
2189 * Function to configure temporal noise reduction (TNR)
2191 int atomisp_tnr(struct atomisp_sub_device *asd, int flag,
2192 struct atomisp_tnr_config *config)
2194 /* Get tnr config from current setup */
2196 /* Get tnr config from current setup */
2197 if (atomisp_css_get_tnr_config(asd, config))
2200 /* Set tnr config to isp parameters */
2201 memcpy(&asd->params.css_param.tnr_config, config,
2202 sizeof(struct ia_css_tnr_config));
2203 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
2204 asd->params.css_update_params_needed = true;
2211 * Function to configure black level compensation
2213 int atomisp_black_level(struct atomisp_sub_device *asd, int flag,
2214 struct atomisp_ob_config *config)
2217 /* Get ob config from current setup */
2218 if (atomisp_css_get_ob_config(asd, config))
2221 /* Set ob config to isp parameters */
2222 memcpy(&asd->params.css_param.ob_config, config,
2223 sizeof(struct ia_css_ob_config));
2224 asd->params.config.ob_config = &asd->params.css_param.ob_config;
2225 asd->params.css_update_params_needed = true;
2232 * Function to configure edge enhancement
2234 int atomisp_ee(struct atomisp_sub_device *asd, int flag,
2235 struct atomisp_ee_config *config)
2238 /* Get ee config from current setup */
2239 if (atomisp_css_get_ee_config(asd, config))
2242 /* Set ee config to isp parameters */
2243 memcpy(&asd->params.css_param.ee_config, config,
2244 sizeof(asd->params.css_param.ee_config));
2245 asd->params.config.ee_config = &asd->params.css_param.ee_config;
2246 asd->params.css_update_params_needed = true;
2253 * Function to update Gamma table for gamma, brightness and contrast config
2255 int atomisp_gamma(struct atomisp_sub_device *asd, int flag,
2256 struct atomisp_gamma_table *config)
2259 /* Get gamma table from current setup */
2260 if (atomisp_css_get_gamma_table(asd, config))
2263 /* Set gamma table to isp parameters */
2264 memcpy(&asd->params.css_param.gamma_table, config,
2265 sizeof(asd->params.css_param.gamma_table));
2266 asd->params.config.gamma_table = &asd->params.css_param.gamma_table;
2273 * Function to update Ctc table for Chroma Enhancement
2275 int atomisp_ctc(struct atomisp_sub_device *asd, int flag,
2276 struct atomisp_ctc_table *config)
2279 /* Get ctc table from current setup */
2280 if (atomisp_css_get_ctc_table(asd, config))
2283 /* Set ctc table to isp parameters */
2284 memcpy(&asd->params.css_param.ctc_table, config,
2285 sizeof(asd->params.css_param.ctc_table));
2286 atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table);
2293 * Function to update gamma correction parameters
2295 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag,
2296 struct atomisp_gc_config *config)
2299 /* Get gamma correction params from current setup */
2300 if (atomisp_css_get_gc_config(asd, config))
2303 /* Set gamma correction params to isp parameters */
2304 memcpy(&asd->params.css_param.gc_config, config,
2305 sizeof(asd->params.css_param.gc_config));
2306 asd->params.config.gc_config = &asd->params.css_param.gc_config;
2307 asd->params.css_update_params_needed = true;
2314 * Function to update narrow gamma flag
2316 int atomisp_formats(struct atomisp_sub_device *asd, int flag,
2317 struct atomisp_formats_config *config)
2320 /* Get narrow gamma flag from current setup */
2321 if (atomisp_css_get_formats_config(asd, config))
2324 /* Set narrow gamma flag to isp parameters */
2325 memcpy(&asd->params.css_param.formats_config, config,
2326 sizeof(asd->params.css_param.formats_config));
2327 asd->params.config.formats_config = &asd->params.css_param.formats_config;
2333 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd)
2335 atomisp_free_css_parameters(&asd->params.css_param);
2337 if (asd->raw_output_frame) {
2338 ia_css_frame_free(asd->raw_output_frame);
2339 asd->raw_output_frame = NULL;
2343 static void atomisp_update_grid_info(struct atomisp_sub_device *asd,
2344 enum ia_css_pipe_id pipe_id,
2347 struct atomisp_device *isp = asd->isp;
2349 u16 stream_id = atomisp_source_pad_to_stream_id(asd, source_pad);
2351 if (atomisp_css_get_grid_info(asd, pipe_id, source_pad))
2354 /* We must free all buffers because they no longer match
2356 atomisp_css_free_stat_buffers(asd);
2358 err = atomisp_alloc_css_stat_bufs(asd, stream_id);
2360 dev_err(isp->dev, "stat_buf allocate error\n");
2364 if (atomisp_alloc_3a_output_buf(asd)) {
2365 /* Failure for 3A buffers does not influence DIS buffers */
2366 if (asd->params.s3a_output_bytes != 0) {
2367 /* For SOC sensor happens s3a_output_bytes == 0,
2368 * using if condition to exclude false error log */
2369 dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n");
2374 if (atomisp_alloc_dis_coef_buf(asd)) {
2376 "Failed to allocate memory for DIS statistics\n");
2380 if (atomisp_alloc_metadata_output_buf(asd)) {
2381 dev_err(isp->dev, "Failed to allocate memory for metadata\n");
2388 atomisp_css_free_stat_buffers(asd);
2392 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd,
2393 struct atomisp_grid_info *info)
2395 memcpy(info, &asd->params.curr_grid_info.s3a_grid,
2396 sizeof(struct ia_css_3a_grid_info));
2399 int atomisp_compare_grid(struct atomisp_sub_device *asd,
2400 struct atomisp_grid_info *atomgrid)
2402 struct atomisp_grid_info tmp = {0};
2404 atomisp_curr_user_grid_info(asd, &tmp);
2405 return memcmp(atomgrid, &tmp, sizeof(tmp));
2409 * Function to update Gdc table for gdc
2411 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag,
2412 struct atomisp_morph_table *config)
2416 struct atomisp_device *isp = asd->isp;
2419 /* Get gdc table from current setup */
2420 struct ia_css_morph_table tab = {0};
2422 atomisp_css_get_morph_table(asd, &tab);
2424 config->width = tab.width;
2425 config->height = tab.height;
2427 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
2428 ret = copy_to_user(config->coordinates_x[i],
2429 tab.coordinates_x[i], tab.height *
2430 tab.width * sizeof(*tab.coordinates_x[i]));
2433 "Failed to copy to User for x\n");
2436 ret = copy_to_user(config->coordinates_y[i],
2437 tab.coordinates_y[i], tab.height *
2438 tab.width * sizeof(*tab.coordinates_y[i]));
2441 "Failed to copy to User for y\n");
2446 struct ia_css_morph_table *tab =
2447 asd->params.css_param.morph_table;
2449 /* free first if we have one */
2451 atomisp_css_morph_table_free(tab);
2452 asd->params.css_param.morph_table = NULL;
2455 /* allocate new one */
2456 tab = atomisp_css_morph_table_allocate(config->width,
2460 dev_err(isp->dev, "out of memory\n");
2464 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
2465 ret = copy_from_user(tab->coordinates_x[i],
2466 config->coordinates_x[i],
2467 config->height * config->width *
2468 sizeof(*config->coordinates_x[i]));
2471 "Failed to copy from User for x, ret %d\n",
2473 atomisp_css_morph_table_free(tab);
2476 ret = copy_from_user(tab->coordinates_y[i],
2477 config->coordinates_y[i],
2478 config->height * config->width *
2479 sizeof(*config->coordinates_y[i]));
2482 "Failed to copy from User for y, ret is %d\n",
2484 atomisp_css_morph_table_free(tab);
2488 asd->params.css_param.morph_table = tab;
2489 if (asd->params.gdc_cac_en)
2490 asd->params.config.morph_table = tab;
2496 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag,
2497 struct atomisp_macc_config *config)
2499 struct ia_css_macc_table *macc_table;
2501 switch (config->color_effect) {
2502 case V4L2_COLORFX_NONE:
2503 macc_table = &asd->params.css_param.macc_table;
2505 case V4L2_COLORFX_SKY_BLUE:
2506 macc_table = &blue_macc_table;
2508 case V4L2_COLORFX_GRASS_GREEN:
2509 macc_table = &green_macc_table;
2511 case V4L2_COLORFX_SKIN_WHITEN_LOW:
2512 macc_table = &skin_low_macc_table;
2514 case V4L2_COLORFX_SKIN_WHITEN:
2515 macc_table = &skin_medium_macc_table;
2517 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
2518 macc_table = &skin_high_macc_table;
2525 /* Get macc table from current setup */
2526 memcpy(&config->table, macc_table,
2527 sizeof(struct ia_css_macc_table));
2529 memcpy(macc_table, &config->table,
2530 sizeof(struct ia_css_macc_table));
2531 if (config->color_effect == asd->params.color_effect)
2532 asd->params.config.macc_table = macc_table;
2538 int atomisp_set_dis_vector(struct atomisp_sub_device *asd,
2539 struct atomisp_dis_vector *vector)
2541 atomisp_css_video_set_dis_vector(asd, vector);
2543 asd->params.dis_proj_data_valid = false;
2544 asd->params.css_update_params_needed = true;
2549 * Function to set/get image stablization statistics
2551 int atomisp_get_dis_stat(struct atomisp_sub_device *asd,
2552 struct atomisp_dis_statistics *stats)
2554 return atomisp_css_get_dis_stat(asd, stats);
2558 * Function set camrea_prefiles.xml current sensor pixel array size
2560 int atomisp_set_array_res(struct atomisp_sub_device *asd,
2561 struct atomisp_resolution *config)
2563 dev_dbg(asd->isp->dev, ">%s start\n", __func__);
2565 dev_err(asd->isp->dev, "Set sensor array size is not valid\n");
2569 asd->sensor_array_res.width = config->width;
2570 asd->sensor_array_res.height = config->height;
2575 * Function to get DVS2 BQ resolution settings
2577 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd,
2578 struct atomisp_dvs2_bq_resolutions *bq_res)
2580 struct ia_css_pipe_config *pipe_cfg = NULL;
2581 struct ia_css_stream_config *stream_cfg = NULL;
2582 struct ia_css_stream_input_config *input_config = NULL;
2584 struct ia_css_stream *stream =
2585 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
2587 dev_warn(asd->isp->dev, "stream is not created");
2591 pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2592 .pipe_configs[IA_CSS_PIPE_ID_VIDEO];
2593 stream_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2595 input_config = &stream_cfg->input_config;
2600 /* the GDC output resolution */
2601 bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2;
2602 bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2;
2604 bq_res->envelope_bq.width_bq = 0;
2605 bq_res->envelope_bq.height_bq = 0;
2606 /* the GDC input resolution */
2607 if (!asd->continuous_mode->val) {
2608 bq_res->source_bq.width_bq = bq_res->output_bq.width_bq +
2609 pipe_cfg->dvs_envelope.width / 2;
2610 bq_res->source_bq.height_bq = bq_res->output_bq.height_bq +
2611 pipe_cfg->dvs_envelope.height / 2;
2613 * Bad pixels caused by spatial filter processing
2614 * ISP filter resolution should be given by CSS/FW, but for now
2615 * there is not such API to query, and it is fixed value, so
2618 bq_res->ispfilter_bq.width_bq = 12 / 2;
2619 bq_res->ispfilter_bq.height_bq = 12 / 2;
2620 /* spatial filter shift, always 4 pixels */
2621 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2622 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2624 if (asd->params.video_dis_en) {
2625 bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width
2626 / 2 - bq_res->ispfilter_bq.width_bq;
2627 bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height
2628 / 2 - bq_res->ispfilter_bq.height_bq;
2631 unsigned int w_padding;
2632 unsigned int gdc_effective_input = 0;
2635 * gdc_effective_input = effective_input + envelope
2637 * From the comment and formula in BZ1786,
2638 * we see the source_bq should be:
2639 * effective_input / bayer_ds_ratio
2641 bq_res->source_bq.width_bq =
2642 (input_config->effective_res.width *
2643 pipe_cfg->bayer_ds_out_res.width /
2644 input_config->effective_res.width + 1) / 2;
2645 bq_res->source_bq.height_bq =
2646 (input_config->effective_res.height *
2647 pipe_cfg->bayer_ds_out_res.height /
2648 input_config->effective_res.height + 1) / 2;
2650 if (!asd->params.video_dis_en) {
2652 * We adjust the ispfilter_bq to:
2653 * ispfilter_bq = 128/BDS
2654 * we still need firmware team to provide an offical
2657 bq_res->ispfilter_bq.width_bq = 128 *
2658 pipe_cfg->bayer_ds_out_res.width /
2659 input_config->effective_res.width / 2;
2660 bq_res->ispfilter_bq.height_bq = 128 *
2661 pipe_cfg->bayer_ds_out_res.width /
2662 input_config->effective_res.width / 2;
2664 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2665 /* No additional left padding for ISYS2401 */
2666 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2667 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2670 * For the w_padding and gdc_shift_bq cacluation
2671 * Please see the BZ 1786 and 4358 for more info.
2672 * Just test that this formula can work now,
2673 * but we still have no offical formula.
2675 * w_padding = ceiling(gdc_effective_input
2676 * /128, 1) * 128 - effective_width
2677 * gdc_shift_bq = w_padding/BDS/2 + ispfilter_bq/2
2679 gdc_effective_input =
2680 input_config->effective_res.width +
2681 pipe_cfg->dvs_envelope.width;
2682 w_padding = roundup(gdc_effective_input, 128) -
2683 input_config->effective_res.width;
2684 w_padding = w_padding *
2685 pipe_cfg->bayer_ds_out_res.width /
2686 input_config->effective_res.width + 1;
2687 w_padding = roundup(w_padding / 2, 1);
2689 bq_res->gdc_shift_bq.width_bq = bq_res->ispfilter_bq.width_bq / 2
2691 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2694 unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
2696 bq_res->ispfilter_bq.width_bq = 8 / 2;
2697 bq_res->ispfilter_bq.height_bq = 8 / 2;
2699 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2700 /* No additional left padding for ISYS2401 */
2701 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2702 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2705 roundup(input_config->effective_res.width, 128) -
2706 input_config->effective_res.width;
2709 bq_res->gdc_shift_bq.width_bq = 4 / 2 +
2711 pipe_cfg->bayer_ds_out_res.width /
2712 input_config->effective_res.width + 1) / 2;
2713 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2716 dvs_w = pipe_cfg->bayer_ds_out_res.width -
2717 pipe_cfg->output_info[0].res.width;
2718 dvs_h = pipe_cfg->bayer_ds_out_res.height -
2719 pipe_cfg->output_info[0].res.height;
2720 dvs_w_max = rounddown(
2721 pipe_cfg->output_info[0].res.width / 5,
2722 ATOM_ISP_STEP_WIDTH);
2723 dvs_h_max = rounddown(
2724 pipe_cfg->output_info[0].res.height / 5,
2725 ATOM_ISP_STEP_HEIGHT);
2726 bq_res->envelope_bq.width_bq =
2727 min((dvs_w / 2), (dvs_w_max / 2)) -
2728 bq_res->ispfilter_bq.width_bq;
2729 bq_res->envelope_bq.height_bq =
2730 min((dvs_h / 2), (dvs_h_max / 2)) -
2731 bq_res->ispfilter_bq.height_bq;
2735 dev_dbg(asd->isp->dev,
2736 "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",
2737 bq_res->source_bq.width_bq, bq_res->source_bq.height_bq,
2738 bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq,
2739 bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq,
2740 bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq,
2741 bq_res->output_bq.width_bq, bq_res->output_bq.height_bq);
2746 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd,
2747 struct atomisp_dis_coefficients *coefs)
2749 return atomisp_css_set_dis_coefs(asd, coefs);
2753 * Function to set/get 3A stat from isp
2755 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag,
2756 struct atomisp_3a_statistics *config)
2758 struct atomisp_device *isp = asd->isp;
2759 struct atomisp_s3a_buf *s3a_buf;
2765 /* sanity check to avoid writing into unallocated memory. */
2766 if (asd->params.s3a_output_bytes == 0)
2769 if (atomisp_compare_grid(asd, &config->grid_info) != 0) {
2770 /* If the grid info in the argument differs from the current
2771 grid info, we tell the caller to reset the grid size and
2776 if (list_empty(&asd->s3a_stats_ready)) {
2777 dev_err(isp->dev, "3a statistics is not valid.\n");
2781 s3a_buf = list_entry(asd->s3a_stats_ready.next,
2782 struct atomisp_s3a_buf, list);
2783 if (s3a_buf->s3a_map)
2784 ia_css_translate_3a_statistics(
2785 asd->params.s3a_user_stat, s3a_buf->s3a_map);
2787 ia_css_get_3a_statistics(asd->params.s3a_user_stat,
2790 config->exp_id = s3a_buf->s3a_data->exp_id;
2791 config->isp_config_id = s3a_buf->s3a_data->isp_config_id;
2793 ret = copy_to_user(config->data, asd->params.s3a_user_stat->data,
2794 asd->params.s3a_output_bytes);
2796 dev_err(isp->dev, "copy to user failed: copied %lu bytes\n",
2801 /* Move to free buffer list */
2802 list_del_init(&s3a_buf->list);
2803 list_add_tail(&s3a_buf->list, &asd->s3a_stats);
2804 dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n",
2806 config->exp_id, config->isp_config_id);
2810 int atomisp_get_metadata(struct atomisp_sub_device *asd, int flag,
2811 struct atomisp_metadata *md)
2813 struct atomisp_device *isp = asd->isp;
2814 struct ia_css_stream_info *stream_info;
2815 struct camera_mipi_info *mipi_info;
2816 struct atomisp_metadata_buf *md_buf;
2817 enum atomisp_metadata_type md_type = ATOMISP_MAIN_METADATA;
2823 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2826 /* We always return the resolution and stride even if there is
2827 * no valid metadata. This allows the caller to get the information
2828 * needed to allocate user-space buffers. */
2829 md->width = stream_info->metadata_info.resolution.width;
2830 md->height = stream_info->metadata_info.resolution.height;
2831 md->stride = stream_info->metadata_info.stride;
2833 /* sanity check to avoid writing into unallocated memory.
2834 * This does not return an error because it is a valid way
2835 * for applications to detect that metadata is not enabled. */
2836 if (md->width == 0 || md->height == 0 || !md->data)
2839 /* This is done in the atomisp_buf_done() */
2840 if (list_empty(&asd->metadata_ready[md_type])) {
2841 dev_warn(isp->dev, "Metadata queue is empty now!\n");
2845 mipi_info = atomisp_to_sensor_mipi_info(
2846 isp->inputs[asd->input_curr].camera);
2850 if (mipi_info->metadata_effective_width) {
2851 for (i = 0; i < md->height; i++)
2852 md->effective_width[i] =
2853 mipi_info->metadata_effective_width[i];
2856 md_buf = list_entry(asd->metadata_ready[md_type].next,
2857 struct atomisp_metadata_buf, list);
2858 md->exp_id = md_buf->metadata->exp_id;
2859 if (md_buf->md_vptr) {
2860 ret = copy_to_user(md->data,
2862 stream_info->metadata_info.size);
2864 hmm_load(md_buf->metadata->address,
2865 asd->params.metadata_user[md_type],
2866 stream_info->metadata_info.size);
2868 ret = copy_to_user(md->data,
2869 asd->params.metadata_user[md_type],
2870 stream_info->metadata_info.size);
2873 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2878 list_del_init(&md_buf->list);
2879 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2881 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2882 __func__, md_type, md->exp_id);
2886 int atomisp_get_metadata_by_type(struct atomisp_sub_device *asd, int flag,
2887 struct atomisp_metadata_with_type *md)
2889 struct atomisp_device *isp = asd->isp;
2890 struct ia_css_stream_info *stream_info;
2891 struct camera_mipi_info *mipi_info;
2892 struct atomisp_metadata_buf *md_buf;
2893 enum atomisp_metadata_type md_type;
2899 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2902 /* We always return the resolution and stride even if there is
2903 * no valid metadata. This allows the caller to get the information
2904 * needed to allocate user-space buffers. */
2905 md->width = stream_info->metadata_info.resolution.width;
2906 md->height = stream_info->metadata_info.resolution.height;
2907 md->stride = stream_info->metadata_info.stride;
2909 /* sanity check to avoid writing into unallocated memory.
2910 * This does not return an error because it is a valid way
2911 * for applications to detect that metadata is not enabled. */
2912 if (md->width == 0 || md->height == 0 || !md->data)
2916 if (md_type < 0 || md_type >= ATOMISP_METADATA_TYPE_NUM)
2919 /* This is done in the atomisp_buf_done() */
2920 if (list_empty(&asd->metadata_ready[md_type])) {
2921 dev_warn(isp->dev, "Metadata queue is empty now!\n");
2925 mipi_info = atomisp_to_sensor_mipi_info(
2926 isp->inputs[asd->input_curr].camera);
2930 if (mipi_info->metadata_effective_width) {
2931 for (i = 0; i < md->height; i++)
2932 md->effective_width[i] =
2933 mipi_info->metadata_effective_width[i];
2936 md_buf = list_entry(asd->metadata_ready[md_type].next,
2937 struct atomisp_metadata_buf, list);
2938 md->exp_id = md_buf->metadata->exp_id;
2939 if (md_buf->md_vptr) {
2940 ret = copy_to_user(md->data,
2942 stream_info->metadata_info.size);
2944 hmm_load(md_buf->metadata->address,
2945 asd->params.metadata_user[md_type],
2946 stream_info->metadata_info.size);
2948 ret = copy_to_user(md->data,
2949 asd->params.metadata_user[md_type],
2950 stream_info->metadata_info.size);
2953 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2957 list_del_init(&md_buf->list);
2958 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2960 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2961 __func__, md_type, md->exp_id);
2966 * Function to calculate real zoom region for every pipe
2968 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd,
2969 struct ia_css_dz_config *dz_config,
2970 enum ia_css_pipe_id css_pipe_id)
2973 struct atomisp_stream_env *stream_env =
2974 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL];
2975 struct atomisp_resolution eff_res, out_res;
2976 int w_offset, h_offset;
2978 memset(&eff_res, 0, sizeof(eff_res));
2979 memset(&out_res, 0, sizeof(out_res));
2981 if (dz_config->dx || dz_config->dy)
2984 if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW
2985 && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) {
2986 dev_err(asd->isp->dev, "%s the set pipe no support crop region"
2992 stream_env->stream_config.input_config.effective_res.width;
2994 stream_env->stream_config.input_config.effective_res.height;
2995 if (eff_res.width == 0 || eff_res.height == 0) {
2996 dev_err(asd->isp->dev, "%s err effective resolution"
3001 if (dz_config->zoom_region.resolution.width
3002 == asd->sensor_array_res.width
3003 || dz_config->zoom_region.resolution.height
3004 == asd->sensor_array_res.height) {
3005 /*no need crop region*/
3006 dz_config->zoom_region.origin.x = 0;
3007 dz_config->zoom_region.origin.y = 0;
3008 dz_config->zoom_region.resolution.width = eff_res.width;
3009 dz_config->zoom_region.resolution.height = eff_res.height;
3014 * This is not the correct implementation with Google's definition, due
3015 * to firmware limitation.
3016 * map real crop region base on above calculating base max crop region.
3020 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
3022 / asd->sensor_array_res.width;
3023 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
3025 / asd->sensor_array_res.height;
3026 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
3028 / asd->sensor_array_res.width;
3029 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
3031 / asd->sensor_array_res.height;
3033 * Set same ratio of crop region resolution and current pipe output
3036 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
3037 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
3038 if (out_res.width == 0 || out_res.height == 0) {
3039 dev_err(asd->isp->dev, "%s err current pipe output resolution"
3044 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
3045 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
3046 if (out_res.width == 0 || out_res.height == 0) {
3047 dev_err(asd->isp->dev, "%s err current pipe output resolution"
3052 if (asd->sensor_array_res.width * out_res.height
3053 < out_res.width * asd->sensor_array_res.height) {
3054 h_offset = asd->sensor_array_res.height
3055 - asd->sensor_array_res.width
3056 * out_res.height / out_res.width;
3057 h_offset = h_offset / 2;
3058 if (dz_config->zoom_region.origin.y < h_offset)
3059 dz_config->zoom_region.origin.y = 0;
3061 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset;
3064 w_offset = asd->sensor_array_res.width
3065 - asd->sensor_array_res.height
3066 * out_res.width / out_res.height;
3067 w_offset = w_offset / 2;
3068 if (dz_config->zoom_region.origin.x < w_offset)
3069 dz_config->zoom_region.origin.x = 0;
3071 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset;
3074 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
3076 / (asd->sensor_array_res.width - 2 * w_offset);
3077 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
3079 / (asd->sensor_array_res.height - 2 * h_offset);
3080 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
3082 / (asd->sensor_array_res.width - 2 * w_offset);
3083 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
3085 / (asd->sensor_array_res.height - 2 * h_offset);
3088 if (out_res.width * dz_config->zoom_region.resolution.height
3089 > dz_config->zoom_region.resolution.width * out_res.height) {
3090 dz_config->zoom_region.resolution.height =
3091 dz_config->zoom_region.resolution.width
3092 * out_res.height / out_res.width;
3094 dz_config->zoom_region.resolution.width =
3095 dz_config->zoom_region.resolution.height
3096 * out_res.width / out_res.height;
3098 dev_dbg(asd->isp->dev,
3099 "%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n",
3100 __func__, dz_config->zoom_region.origin.x,
3101 dz_config->zoom_region.origin.y,
3102 dz_config->zoom_region.resolution.width,
3103 dz_config->zoom_region.resolution.height,
3104 eff_res.width, eff_res.height,
3105 asd->sensor_array_res.width,
3106 asd->sensor_array_res.height,
3107 out_res.width, out_res.height);
3109 if ((dz_config->zoom_region.origin.x +
3110 dz_config->zoom_region.resolution.width
3112 (dz_config->zoom_region.origin.y +
3113 dz_config->zoom_region.resolution.height
3121 * Function to check the zoom region whether is effective
3123 static bool atomisp_check_zoom_region(
3124 struct atomisp_sub_device *asd,
3125 struct ia_css_dz_config *dz_config)
3127 struct atomisp_resolution config;
3131 memset(&config, 0, sizeof(struct atomisp_resolution));
3133 if (dz_config->dx && dz_config->dy)
3136 config.width = asd->sensor_array_res.width;
3137 config.height = asd->sensor_array_res.height;
3138 w = dz_config->zoom_region.origin.x +
3139 dz_config->zoom_region.resolution.width;
3140 h = dz_config->zoom_region.origin.y +
3141 dz_config->zoom_region.resolution.height;
3143 if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0)
3146 /* setting error zoom region */
3147 dev_err(asd->isp->dev,
3148 "%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n",
3149 __func__, dz_config->zoom_region.origin.x,
3150 dz_config->zoom_region.origin.y,
3151 dz_config->zoom_region.resolution.width,
3152 dz_config->zoom_region.resolution.height,
3153 config.width, config.height);
3158 void atomisp_apply_css_parameters(
3159 struct atomisp_sub_device *asd,
3160 struct atomisp_css_params *css_param)
3162 if (css_param->update_flag.wb_config)
3163 asd->params.config.wb_config = &css_param->wb_config;
3165 if (css_param->update_flag.ob_config)
3166 asd->params.config.ob_config = &css_param->ob_config;
3168 if (css_param->update_flag.dp_config)
3169 asd->params.config.dp_config = &css_param->dp_config;
3171 if (css_param->update_flag.nr_config)
3172 asd->params.config.nr_config = &css_param->nr_config;
3174 if (css_param->update_flag.ee_config)
3175 asd->params.config.ee_config = &css_param->ee_config;
3177 if (css_param->update_flag.tnr_config)
3178 asd->params.config.tnr_config = &css_param->tnr_config;
3180 if (css_param->update_flag.a3a_config)
3181 asd->params.config.s3a_config = &css_param->s3a_config;
3183 if (css_param->update_flag.ctc_config)
3184 asd->params.config.ctc_config = &css_param->ctc_config;
3186 if (css_param->update_flag.cnr_config)
3187 asd->params.config.cnr_config = &css_param->cnr_config;
3189 if (css_param->update_flag.ecd_config)
3190 asd->params.config.ecd_config = &css_param->ecd_config;
3192 if (css_param->update_flag.ynr_config)
3193 asd->params.config.ynr_config = &css_param->ynr_config;
3195 if (css_param->update_flag.fc_config)
3196 asd->params.config.fc_config = &css_param->fc_config;
3198 if (css_param->update_flag.macc_config)
3199 asd->params.config.macc_config = &css_param->macc_config;
3201 if (css_param->update_flag.aa_config)
3202 asd->params.config.aa_config = &css_param->aa_config;
3204 if (css_param->update_flag.anr_config)
3205 asd->params.config.anr_config = &css_param->anr_config;
3207 if (css_param->update_flag.xnr_config)
3208 asd->params.config.xnr_config = &css_param->xnr_config;
3210 if (css_param->update_flag.yuv2rgb_cc_config)
3211 asd->params.config.yuv2rgb_cc_config = &css_param->yuv2rgb_cc_config;
3213 if (css_param->update_flag.rgb2yuv_cc_config)
3214 asd->params.config.rgb2yuv_cc_config = &css_param->rgb2yuv_cc_config;
3216 if (css_param->update_flag.macc_table)
3217 asd->params.config.macc_table = &css_param->macc_table;
3219 if (css_param->update_flag.xnr_table)
3220 asd->params.config.xnr_table = &css_param->xnr_table;
3222 if (css_param->update_flag.r_gamma_table)
3223 asd->params.config.r_gamma_table = &css_param->r_gamma_table;
3225 if (css_param->update_flag.g_gamma_table)
3226 asd->params.config.g_gamma_table = &css_param->g_gamma_table;
3228 if (css_param->update_flag.b_gamma_table)
3229 asd->params.config.b_gamma_table = &css_param->b_gamma_table;
3231 if (css_param->update_flag.anr_thres)
3232 atomisp_css_set_anr_thres(asd, &css_param->anr_thres);
3234 if (css_param->update_flag.shading_table)
3235 asd->params.config.shading_table = css_param->shading_table;
3237 if (css_param->update_flag.morph_table && asd->params.gdc_cac_en)
3238 asd->params.config.morph_table = css_param->morph_table;
3240 if (css_param->update_flag.dvs2_coefs) {
3241 struct ia_css_dvs_grid_info *dvs_grid_info =
3242 atomisp_css_get_dvs_grid_info(
3243 &asd->params.curr_grid_info);
3245 if (dvs_grid_info && dvs_grid_info->enable)
3246 atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff);
3249 if (css_param->update_flag.dvs_6axis_config)
3250 atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis);
3252 atomisp_css_set_isp_config_id(asd, css_param->isp_config_id);
3254 * These configurations are on used by ISP1.x, not for ISP2.x,
3255 * so do not handle them. see comments of ia_css_isp_config.
3266 static unsigned int long copy_from_compatible(void *to, const void *from,
3267 unsigned long n, bool from_user)
3270 return copy_from_user(to, (void __user *)from, n);
3272 memcpy(to, from, n);
3276 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd,
3277 struct atomisp_parameters *arg,
3278 struct atomisp_css_params *css_param,
3281 struct atomisp_parameters *cur_config = &css_param->update_flag;
3283 if (!arg || !asd || !css_param)
3286 if (arg->wb_config && (from_user || !cur_config->wb_config)) {
3287 if (copy_from_compatible(&css_param->wb_config, arg->wb_config,
3288 sizeof(struct ia_css_wb_config),
3291 css_param->update_flag.wb_config =
3292 (struct atomisp_wb_config *)&css_param->wb_config;
3295 if (arg->ob_config && (from_user || !cur_config->ob_config)) {
3296 if (copy_from_compatible(&css_param->ob_config, arg->ob_config,
3297 sizeof(struct ia_css_ob_config),
3300 css_param->update_flag.ob_config =
3301 (struct atomisp_ob_config *)&css_param->ob_config;
3304 if (arg->dp_config && (from_user || !cur_config->dp_config)) {
3305 if (copy_from_compatible(&css_param->dp_config, arg->dp_config,
3306 sizeof(struct ia_css_dp_config),
3309 css_param->update_flag.dp_config =
3310 (struct atomisp_dp_config *)&css_param->dp_config;
3313 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
3314 if (arg->dz_config && (from_user || !cur_config->dz_config)) {
3315 if (copy_from_compatible(&css_param->dz_config,
3317 sizeof(struct ia_css_dz_config),
3320 if (!atomisp_check_zoom_region(asd,
3321 &css_param->dz_config)) {
3322 dev_err(asd->isp->dev, "crop region error!");
3325 css_param->update_flag.dz_config =
3326 (struct atomisp_dz_config *)
3327 &css_param->dz_config;
3331 if (arg->nr_config && (from_user || !cur_config->nr_config)) {
3332 if (copy_from_compatible(&css_param->nr_config, arg->nr_config,
3333 sizeof(struct ia_css_nr_config),
3336 css_param->update_flag.nr_config =
3337 (struct atomisp_nr_config *)&css_param->nr_config;
3340 if (arg->ee_config && (from_user || !cur_config->ee_config)) {
3341 if (copy_from_compatible(&css_param->ee_config, arg->ee_config,
3342 sizeof(struct ia_css_ee_config),
3345 css_param->update_flag.ee_config =
3346 (struct atomisp_ee_config *)&css_param->ee_config;
3349 if (arg->tnr_config && (from_user || !cur_config->tnr_config)) {
3350 if (copy_from_compatible(&css_param->tnr_config,
3352 sizeof(struct ia_css_tnr_config),
3355 css_param->update_flag.tnr_config =
3356 (struct atomisp_tnr_config *)
3357 &css_param->tnr_config;
3360 if (arg->a3a_config && (from_user || !cur_config->a3a_config)) {
3361 if (copy_from_compatible(&css_param->s3a_config,
3363 sizeof(struct ia_css_3a_config),
3366 css_param->update_flag.a3a_config =
3367 (struct atomisp_3a_config *)&css_param->s3a_config;
3370 if (arg->ctc_config && (from_user || !cur_config->ctc_config)) {
3371 if (copy_from_compatible(&css_param->ctc_config,
3373 sizeof(struct ia_css_ctc_config),
3376 css_param->update_flag.ctc_config =
3377 (struct atomisp_ctc_config *)
3378 &css_param->ctc_config;
3381 if (arg->cnr_config && (from_user || !cur_config->cnr_config)) {
3382 if (copy_from_compatible(&css_param->cnr_config,
3384 sizeof(struct ia_css_cnr_config),
3387 css_param->update_flag.cnr_config =
3388 (struct atomisp_cnr_config *)
3389 &css_param->cnr_config;
3392 if (arg->ecd_config && (from_user || !cur_config->ecd_config)) {
3393 if (copy_from_compatible(&css_param->ecd_config,
3395 sizeof(struct ia_css_ecd_config),
3398 css_param->update_flag.ecd_config =
3399 (struct atomisp_ecd_config *)
3400 &css_param->ecd_config;
3403 if (arg->ynr_config && (from_user || !cur_config->ynr_config)) {
3404 if (copy_from_compatible(&css_param->ynr_config,
3406 sizeof(struct ia_css_ynr_config),
3409 css_param->update_flag.ynr_config =
3410 (struct atomisp_ynr_config *)
3411 &css_param->ynr_config;
3414 if (arg->fc_config && (from_user || !cur_config->fc_config)) {
3415 if (copy_from_compatible(&css_param->fc_config,
3417 sizeof(struct ia_css_fc_config),
3420 css_param->update_flag.fc_config =
3421 (struct atomisp_fc_config *)&css_param->fc_config;
3424 if (arg->macc_config && (from_user || !cur_config->macc_config)) {
3425 if (copy_from_compatible(&css_param->macc_config,
3427 sizeof(struct ia_css_macc_config),
3430 css_param->update_flag.macc_config =
3431 (struct atomisp_macc_config *)
3432 &css_param->macc_config;
3435 if (arg->aa_config && (from_user || !cur_config->aa_config)) {
3436 if (copy_from_compatible(&css_param->aa_config, arg->aa_config,
3437 sizeof(struct ia_css_aa_config),
3440 css_param->update_flag.aa_config =
3441 (struct atomisp_aa_config *)&css_param->aa_config;
3444 if (arg->anr_config && (from_user || !cur_config->anr_config)) {
3445 if (copy_from_compatible(&css_param->anr_config,
3447 sizeof(struct ia_css_anr_config),
3450 css_param->update_flag.anr_config =
3451 (struct atomisp_anr_config *)
3452 &css_param->anr_config;
3455 if (arg->xnr_config && (from_user || !cur_config->xnr_config)) {
3456 if (copy_from_compatible(&css_param->xnr_config,
3458 sizeof(struct ia_css_xnr_config),
3461 css_param->update_flag.xnr_config =
3462 (struct atomisp_xnr_config *)
3463 &css_param->xnr_config;
3466 if (arg->yuv2rgb_cc_config &&
3467 (from_user || !cur_config->yuv2rgb_cc_config)) {
3468 if (copy_from_compatible(&css_param->yuv2rgb_cc_config,
3469 arg->yuv2rgb_cc_config,
3470 sizeof(struct ia_css_cc_config),
3473 css_param->update_flag.yuv2rgb_cc_config =
3474 (struct atomisp_cc_config *)
3475 &css_param->yuv2rgb_cc_config;
3478 if (arg->rgb2yuv_cc_config &&
3479 (from_user || !cur_config->rgb2yuv_cc_config)) {
3480 if (copy_from_compatible(&css_param->rgb2yuv_cc_config,
3481 arg->rgb2yuv_cc_config,
3482 sizeof(struct ia_css_cc_config),
3485 css_param->update_flag.rgb2yuv_cc_config =
3486 (struct atomisp_cc_config *)
3487 &css_param->rgb2yuv_cc_config;
3490 if (arg->macc_table && (from_user || !cur_config->macc_table)) {
3491 if (copy_from_compatible(&css_param->macc_table,
3493 sizeof(struct ia_css_macc_table),
3496 css_param->update_flag.macc_table =
3497 (struct atomisp_macc_table *)
3498 &css_param->macc_table;
3501 if (arg->xnr_table && (from_user || !cur_config->xnr_table)) {
3502 if (copy_from_compatible(&css_param->xnr_table,
3504 sizeof(struct ia_css_xnr_table),
3507 css_param->update_flag.xnr_table =
3508 (struct atomisp_xnr_table *)&css_param->xnr_table;
3511 if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) {
3512 if (copy_from_compatible(&css_param->r_gamma_table,
3514 sizeof(struct ia_css_rgb_gamma_table),
3517 css_param->update_flag.r_gamma_table =
3518 (struct atomisp_rgb_gamma_table *)
3519 &css_param->r_gamma_table;
3522 if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) {
3523 if (copy_from_compatible(&css_param->g_gamma_table,
3525 sizeof(struct ia_css_rgb_gamma_table),
3528 css_param->update_flag.g_gamma_table =
3529 (struct atomisp_rgb_gamma_table *)
3530 &css_param->g_gamma_table;
3533 if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) {
3534 if (copy_from_compatible(&css_param->b_gamma_table,
3536 sizeof(struct ia_css_rgb_gamma_table),
3539 css_param->update_flag.b_gamma_table =
3540 (struct atomisp_rgb_gamma_table *)
3541 &css_param->b_gamma_table;
3544 if (arg->anr_thres && (from_user || !cur_config->anr_thres)) {
3545 if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres,
3546 sizeof(struct ia_css_anr_thres),
3549 css_param->update_flag.anr_thres =
3550 (struct atomisp_anr_thres *)&css_param->anr_thres;
3554 css_param->isp_config_id = arg->isp_config_id;
3556 * These configurations are on used by ISP1.x, not for ISP2.x,
3557 * so do not handle them. see comments of ia_css_isp_config.
3569 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd,
3570 struct atomisp_shading_table *source_st,
3571 struct atomisp_css_params *css_param,
3575 unsigned int len_table;
3576 struct ia_css_shading_table *shading_table;
3577 struct ia_css_shading_table *old_table;
3578 struct atomisp_shading_table *st, dest_st;
3586 if (!from_user && css_param->update_flag.shading_table)
3590 if (copy_from_compatible(&dest_st, source_st,
3591 sizeof(struct atomisp_shading_table),
3593 dev_err(asd->isp->dev, "copy shading table failed!");
3601 old_table = css_param->shading_table;
3603 /* user config is to disable the shading table. */
3605 /* Generate a minimum table with enable = 0. */
3606 shading_table = atomisp_css_shading_table_alloc(1, 1);
3609 shading_table->enable = 0;
3613 /* Setting a new table. Validate first - all tables must be set */
3614 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3616 dev_err(asd->isp->dev, "shading table validate failed");
3621 /* Shading table size per color */
3623 if (st->width > ISP2400_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
3624 st->height > ISP2400_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
3625 dev_err(asd->isp->dev, "shading table w/h validate failed!");
3629 if (st->width > ISP2401_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
3630 st->height > ISP2401_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
3631 dev_err(asd->isp->dev, "shading table w/h validate failed!");
3636 shading_table = atomisp_css_shading_table_alloc(st->width, st->height);
3640 len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE;
3641 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3642 if (copy_from_compatible(shading_table->data[i],
3643 st->data[i], len_table, from_user)) {
3644 atomisp_css_shading_table_free(shading_table);
3648 shading_table->sensor_width = st->sensor_width;
3649 shading_table->sensor_height = st->sensor_height;
3650 shading_table->fraction_bits = st->fraction_bits;
3651 shading_table->enable = st->enable;
3653 /* No need to update shading table if it is the same */
3655 old_table->sensor_width == shading_table->sensor_width &&
3656 old_table->sensor_height == shading_table->sensor_height &&
3657 old_table->width == shading_table->width &&
3658 old_table->height == shading_table->height &&
3659 old_table->fraction_bits == shading_table->fraction_bits &&
3660 old_table->enable == shading_table->enable) {
3661 bool data_is_same = true;
3663 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3664 if (memcmp(shading_table->data[i], old_table->data[i],
3666 data_is_same = false;
3672 atomisp_css_shading_table_free(shading_table);
3678 /* set LSC to CSS */
3679 css_param->shading_table = shading_table;
3680 css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table;
3681 asd->params.sc_en = shading_table;
3684 atomisp_css_shading_table_free(old_table);
3689 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd,
3690 struct ia_css_dvs2_coefficients *coefs,
3691 struct atomisp_css_params *css_param,
3694 struct ia_css_dvs_grid_info *cur =
3695 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3696 int dvs_hor_coef_bytes, dvs_ver_coef_bytes;
3697 struct ia_css_dvs2_coefficients dvs2_coefs;
3702 if (!from_user && css_param->update_flag.dvs2_coefs)
3706 if (sizeof(*cur) != sizeof(coefs->grid) ||
3707 memcmp(&coefs->grid, cur, sizeof(coefs->grid))) {
3708 dev_err(asd->isp->dev, "dvs grid mis-match!\n");
3709 /* If the grid info in the argument differs from the current
3710 grid info, we tell the caller to reset the grid size and
3715 if (!coefs->hor_coefs.odd_real ||
3716 !coefs->hor_coefs.odd_imag ||
3717 !coefs->hor_coefs.even_real ||
3718 !coefs->hor_coefs.even_imag ||
3719 !coefs->ver_coefs.odd_real ||
3720 !coefs->ver_coefs.odd_imag ||
3721 !coefs->ver_coefs.even_real ||
3722 !coefs->ver_coefs.even_imag)
3725 if (!css_param->dvs2_coeff) {
3726 /* DIS coefficients. */
3727 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3728 if (!css_param->dvs2_coeff)
3732 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3733 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3734 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3735 coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3736 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3737 coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3738 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3739 coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3740 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3741 coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3742 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3743 coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3744 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3745 coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3746 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3747 coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3748 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3749 coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3750 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3751 css_param->dvs2_coeff = NULL;
3755 if (copy_from_compatible(&dvs2_coefs, coefs,
3756 sizeof(struct ia_css_dvs2_coefficients),
3758 dev_err(asd->isp->dev, "copy dvs2 coef failed");
3762 if (sizeof(*cur) != sizeof(dvs2_coefs.grid) ||
3763 memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) {
3764 dev_err(asd->isp->dev, "dvs grid mis-match!\n");
3765 /* If the grid info in the argument differs from the current
3766 grid info, we tell the caller to reset the grid size and
3771 if (!dvs2_coefs.hor_coefs.odd_real ||
3772 !dvs2_coefs.hor_coefs.odd_imag ||
3773 !dvs2_coefs.hor_coefs.even_real ||
3774 !dvs2_coefs.hor_coefs.even_imag ||
3775 !dvs2_coefs.ver_coefs.odd_real ||
3776 !dvs2_coefs.ver_coefs.odd_imag ||
3777 !dvs2_coefs.ver_coefs.even_real ||
3778 !dvs2_coefs.ver_coefs.even_imag)
3781 if (!css_param->dvs2_coeff) {
3782 /* DIS coefficients. */
3783 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3784 if (!css_param->dvs2_coeff)
3788 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3789 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3790 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3791 dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3792 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3793 dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3794 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3795 dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3796 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3797 dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3798 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3799 dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3800 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3801 dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3802 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3803 dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3804 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3805 dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3806 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3807 css_param->dvs2_coeff = NULL;
3812 css_param->update_flag.dvs2_coefs =
3813 (struct atomisp_dis_coefficients *)css_param->dvs2_coeff;
3817 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd,
3818 struct atomisp_dvs_6axis_config *source_6axis_config,
3819 struct atomisp_css_params *css_param,
3822 struct ia_css_dvs_6axis_config *dvs_6axis_config;
3823 struct ia_css_dvs_6axis_config *old_6axis_config;
3824 struct ia_css_stream *stream =
3825 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
3826 struct ia_css_dvs_grid_info *dvs_grid_info =
3827 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3831 dev_err(asd->isp->dev, "%s: internal error!", __func__);
3835 if (!source_6axis_config || !dvs_grid_info)
3838 if (!dvs_grid_info->enable)
3841 if (!from_user && css_param->update_flag.dvs_6axis_config)
3844 /* check whether need to reallocate for 6 axis config */
3845 old_6axis_config = css_param->dvs_6axis;
3846 dvs_6axis_config = old_6axis_config;
3849 struct ia_css_dvs_6axis_config t_6axis_config;
3851 if (copy_from_compatible(&t_6axis_config, source_6axis_config,
3852 sizeof(struct atomisp_dvs_6axis_config),
3854 dev_err(asd->isp->dev, "copy morph table failed!");
3858 if (old_6axis_config &&
3859 (old_6axis_config->width_y != t_6axis_config.width_y ||
3860 old_6axis_config->height_y != t_6axis_config.height_y ||
3861 old_6axis_config->width_uv != t_6axis_config.width_uv ||
3862 old_6axis_config->height_uv != t_6axis_config.height_uv)) {
3863 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3864 css_param->dvs_6axis = NULL;
3866 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3867 if (!dvs_6axis_config)
3869 } else if (!dvs_6axis_config) {
3870 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3871 if (!dvs_6axis_config)
3875 dvs_6axis_config->exp_id = t_6axis_config.exp_id;
3877 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3878 t_6axis_config.xcoords_y,
3879 t_6axis_config.width_y *
3880 t_6axis_config.height_y *
3881 sizeof(*dvs_6axis_config->xcoords_y),
3884 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3885 t_6axis_config.ycoords_y,
3886 t_6axis_config.width_y *
3887 t_6axis_config.height_y *
3888 sizeof(*dvs_6axis_config->ycoords_y),
3891 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3892 t_6axis_config.xcoords_uv,
3893 t_6axis_config.width_uv *
3894 t_6axis_config.height_uv *
3895 sizeof(*dvs_6axis_config->xcoords_uv),
3898 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3899 t_6axis_config.ycoords_uv,
3900 t_6axis_config.width_uv *
3901 t_6axis_config.height_uv *
3902 sizeof(*dvs_6axis_config->ycoords_uv),
3906 if (old_6axis_config &&
3907 (old_6axis_config->width_y != source_6axis_config->width_y ||
3908 old_6axis_config->height_y != source_6axis_config->height_y ||
3909 old_6axis_config->width_uv != source_6axis_config->width_uv ||
3910 old_6axis_config->height_uv != source_6axis_config->height_uv)) {
3911 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3912 css_param->dvs_6axis = NULL;
3914 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3915 if (!dvs_6axis_config)
3917 } else if (!dvs_6axis_config) {
3918 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3919 if (!dvs_6axis_config)
3923 dvs_6axis_config->exp_id = source_6axis_config->exp_id;
3925 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3926 source_6axis_config->xcoords_y,
3927 source_6axis_config->width_y *
3928 source_6axis_config->height_y *
3929 sizeof(*source_6axis_config->xcoords_y),
3932 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3933 source_6axis_config->ycoords_y,
3934 source_6axis_config->width_y *
3935 source_6axis_config->height_y *
3936 sizeof(*source_6axis_config->ycoords_y),
3939 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3940 source_6axis_config->xcoords_uv,
3941 source_6axis_config->width_uv *
3942 source_6axis_config->height_uv *
3943 sizeof(*source_6axis_config->xcoords_uv),
3946 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3947 source_6axis_config->ycoords_uv,
3948 source_6axis_config->width_uv *
3949 source_6axis_config->height_uv *
3950 sizeof(*source_6axis_config->ycoords_uv),
3954 css_param->dvs_6axis = dvs_6axis_config;
3955 css_param->update_flag.dvs_6axis_config =
3956 (struct atomisp_dvs_6axis_config *)dvs_6axis_config;
3960 if (dvs_6axis_config)
3961 ia_css_dvs2_6axis_config_free(dvs_6axis_config);
3965 int atomisp_cp_morph_table(struct atomisp_sub_device *asd,
3966 struct atomisp_morph_table *source_morph_table,
3967 struct atomisp_css_params *css_param,
3972 struct ia_css_morph_table *morph_table;
3973 struct ia_css_morph_table *old_morph_table;
3975 if (!source_morph_table)
3978 if (!from_user && css_param->update_flag.morph_table)
3981 old_morph_table = css_param->morph_table;
3984 struct ia_css_morph_table mtbl;
3986 if (copy_from_compatible(&mtbl, source_morph_table,
3987 sizeof(struct atomisp_morph_table),
3989 dev_err(asd->isp->dev, "copy morph table failed!");
3993 morph_table = atomisp_css_morph_table_allocate(
3999 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
4000 if (copy_from_compatible(morph_table->coordinates_x[i],
4001 (__force void *)source_morph_table->coordinates_x[i],
4002 mtbl.height * mtbl.width *
4003 sizeof(*morph_table->coordinates_x[i]),
4007 if (copy_from_compatible(morph_table->coordinates_y[i],
4008 (__force void *)source_morph_table->coordinates_y[i],
4009 mtbl.height * mtbl.width *
4010 sizeof(*morph_table->coordinates_y[i]),
4015 morph_table = atomisp_css_morph_table_allocate(
4016 source_morph_table->width,
4017 source_morph_table->height);
4021 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
4022 if (copy_from_compatible(morph_table->coordinates_x[i],
4023 (__force void *)source_morph_table->coordinates_x[i],
4024 source_morph_table->height * source_morph_table->width *
4025 sizeof(*source_morph_table->coordinates_x[i]),
4029 if (copy_from_compatible(morph_table->coordinates_y[i],
4030 (__force void *)source_morph_table->coordinates_y[i],
4031 source_morph_table->height * source_morph_table->width *
4032 sizeof(*source_morph_table->coordinates_y[i]),
4038 css_param->morph_table = morph_table;
4039 if (old_morph_table)
4040 atomisp_css_morph_table_free(old_morph_table);
4041 css_param->update_flag.morph_table =
4042 (struct atomisp_morph_table *)morph_table;
4047 atomisp_css_morph_table_free(morph_table);
4051 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd,
4052 struct atomisp_parameters *arg,
4053 struct atomisp_css_params *css_param)
4057 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false);
4060 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false);
4063 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false);
4066 ret = atomisp_css_cp_dvs2_coefs(asd,
4067 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
4071 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
4076 void atomisp_free_css_parameters(struct atomisp_css_params *css_param)
4078 if (css_param->dvs_6axis) {
4079 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
4080 css_param->dvs_6axis = NULL;
4082 if (css_param->dvs2_coeff) {
4083 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
4084 css_param->dvs2_coeff = NULL;
4086 if (css_param->shading_table) {
4087 ia_css_shading_table_free(css_param->shading_table);
4088 css_param->shading_table = NULL;
4090 if (css_param->morph_table) {
4091 ia_css_morph_table_free(css_param->morph_table);
4092 css_param->morph_table = NULL;
4097 * Check parameter queue list and buffer queue list to find out if matched items
4098 * and then set parameter to CSS and enqueue buffer to CSS.
4099 * Of course, if the buffer in buffer waiting list is not bound to a per-frame
4100 * parameter, it will be enqueued into CSS as long as the per-frame setting
4101 * buffers before it get enqueued.
4103 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe)
4105 struct atomisp_sub_device *asd = pipe->asd;
4106 struct videobuf_buffer *vb = NULL, *vb_tmp;
4107 struct atomisp_css_params_with_list *param = NULL, *param_tmp;
4108 struct videobuf_vmalloc_memory *vm_mem = NULL;
4109 unsigned long irqflags;
4110 bool need_to_enqueue_buffer = false;
4112 if (atomisp_is_vf_pipe(pipe))
4116 * CSS/FW requires set parameter and enqueue buffer happen after ISP
4119 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
4122 if (list_empty(&pipe->per_frame_params) ||
4123 list_empty(&pipe->buffers_waiting_for_param))
4126 list_for_each_entry_safe(vb, vb_tmp,
4127 &pipe->buffers_waiting_for_param, queue) {
4128 if (pipe->frame_request_config_id[vb->i]) {
4129 list_for_each_entry_safe(param, param_tmp,
4130 &pipe->per_frame_params, list) {
4131 if (pipe->frame_request_config_id[vb->i] !=
4132 param->params.isp_config_id)
4135 list_del(¶m->list);
4136 list_del(&vb->queue);
4138 * clear the request config id as the buffer
4139 * will be handled and enqueued into CSS soon
4141 pipe->frame_request_config_id[vb->i] = 0;
4142 pipe->frame_params[vb->i] = param;
4149 spin_lock_irqsave(&pipe->irq_lock, irqflags);
4150 list_add_tail(&vb->queue, &pipe->activeq);
4151 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
4153 need_to_enqueue_buffer = true;
4155 /* The is the end, stop further loop */
4159 list_del(&vb->queue);
4160 pipe->frame_params[vb->i] = NULL;
4161 spin_lock_irqsave(&pipe->irq_lock, irqflags);
4162 list_add_tail(&vb->queue, &pipe->activeq);
4163 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
4164 need_to_enqueue_buffer = true;
4168 if (!need_to_enqueue_buffer)
4171 atomisp_qbuffers_to_css(asd);
4174 if (!atomisp_is_wdt_running(asd) && atomisp_buffers_queued(asd))
4175 atomisp_wdt_start(asd);
4177 if (atomisp_buffers_queued_pipe(pipe)) {
4178 if (!atomisp_is_wdt_running(pipe))
4179 atomisp_wdt_start_pipe(pipe);
4181 atomisp_wdt_refresh_pipe(pipe,
4182 ATOMISP_WDT_KEEP_CURRENT_DELAY);
4188 * Function to configure ISP parameters
4190 int atomisp_set_parameters(struct video_device *vdev,
4191 struct atomisp_parameters *arg)
4193 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4194 struct atomisp_sub_device *asd = pipe->asd;
4195 struct atomisp_css_params_with_list *param = NULL;
4196 struct atomisp_css_params *css_param = &asd->params.css_param;
4199 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) {
4200 dev_err(asd->isp->dev, "%s: internal error!\n", __func__);
4204 dev_dbg(asd->isp->dev,
4205 "%s: set parameter(per_frame_setting %d) for asd%d with isp_config_id %d of %s\n",
4206 __func__, arg->per_frame_setting, asd->index,
4207 arg->isp_config_id, vdev->name);
4210 if (atomisp_is_vf_pipe(pipe) && arg->per_frame_setting) {
4211 dev_err(asd->isp->dev, "%s: vf pipe not support per_frame_setting",
4217 if (arg->per_frame_setting && !atomisp_is_vf_pipe(pipe)) {
4219 * Per-frame setting enabled, we allocate a new parameter
4220 * buffer to cache the parameters and only when frame buffers
4221 * are ready, the parameters will be set to CSS.
4222 * per-frame setting only works for the main output frame.
4224 param = kvzalloc(sizeof(*param), GFP_KERNEL);
4226 dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n",
4230 css_param = ¶m->params;
4233 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true);
4235 goto apply_parameter_failed;
4237 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true);
4239 goto apply_parameter_failed;
4241 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true);
4243 goto apply_parameter_failed;
4245 ret = atomisp_css_cp_dvs2_coefs(asd,
4246 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
4249 goto apply_parameter_failed;
4251 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
4254 goto apply_parameter_failed;
4256 if (!(arg->per_frame_setting && !atomisp_is_vf_pipe(pipe))) {
4257 /* indicate to CSS that we have parameters to be updated */
4258 asd->params.css_update_params_needed = true;
4260 list_add_tail(¶m->list, &pipe->per_frame_params);
4261 atomisp_handle_parameter_and_buffer(pipe);
4266 apply_parameter_failed:
4268 atomisp_free_css_parameters(css_param);
4275 * Function to set/get isp parameters to isp
4277 int atomisp_param(struct atomisp_sub_device *asd, int flag,
4278 struct atomisp_parm *config)
4280 struct ia_css_pipe_config *vp_cfg =
4281 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
4282 pipe_configs[IA_CSS_PIPE_ID_VIDEO];
4284 /* Read parameter for 3A binary info */
4286 struct ia_css_dvs_grid_info *dvs_grid_info =
4287 atomisp_css_get_dvs_grid_info(
4288 &asd->params.curr_grid_info);
4290 atomisp_curr_user_grid_info(asd, &config->info);
4292 /* We always return the resolution and stride even if there is
4293 * no valid metadata. This allows the caller to get the
4294 * information needed to allocate user-space buffers. */
4295 config->metadata_config.metadata_height = asd->
4296 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
4297 metadata_info.resolution.height;
4298 config->metadata_config.metadata_stride = asd->
4299 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
4300 metadata_info.stride;
4302 /* update dvs grid info */
4304 memcpy(&config->dvs_grid,
4306 sizeof(struct ia_css_dvs_grid_info));
4308 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
4309 config->dvs_envelop.width = 0;
4310 config->dvs_envelop.height = 0;
4314 /* update dvs envelop info */
4315 if (!asd->continuous_mode->val) {
4316 config->dvs_envelop.width = vp_cfg->dvs_envelope.width;
4317 config->dvs_envelop.height =
4318 vp_cfg->dvs_envelope.height;
4320 unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
4322 dvs_w = vp_cfg->bayer_ds_out_res.width -
4323 vp_cfg->output_info[0].res.width;
4324 dvs_h = vp_cfg->bayer_ds_out_res.height -
4325 vp_cfg->output_info[0].res.height;
4326 dvs_w_max = rounddown(
4327 vp_cfg->output_info[0].res.width / 5,
4328 ATOM_ISP_STEP_WIDTH);
4329 dvs_h_max = rounddown(
4330 vp_cfg->output_info[0].res.height / 5,
4331 ATOM_ISP_STEP_HEIGHT);
4333 config->dvs_envelop.width = min(dvs_w, dvs_w_max);
4334 config->dvs_envelop.height = min(dvs_h, dvs_h_max);
4340 memcpy(&asd->params.css_param.wb_config, &config->wb_config,
4341 sizeof(struct ia_css_wb_config));
4342 memcpy(&asd->params.css_param.ob_config, &config->ob_config,
4343 sizeof(struct ia_css_ob_config));
4344 memcpy(&asd->params.css_param.dp_config, &config->dp_config,
4345 sizeof(struct ia_css_dp_config));
4346 memcpy(&asd->params.css_param.de_config, &config->de_config,
4347 sizeof(struct ia_css_de_config));
4348 memcpy(&asd->params.css_param.dz_config, &config->dz_config,
4349 sizeof(struct ia_css_dz_config));
4350 memcpy(&asd->params.css_param.ce_config, &config->ce_config,
4351 sizeof(struct ia_css_ce_config));
4352 memcpy(&asd->params.css_param.nr_config, &config->nr_config,
4353 sizeof(struct ia_css_nr_config));
4354 memcpy(&asd->params.css_param.ee_config, &config->ee_config,
4355 sizeof(struct ia_css_ee_config));
4356 memcpy(&asd->params.css_param.tnr_config, &config->tnr_config,
4357 sizeof(struct ia_css_tnr_config));
4359 if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) {
4360 asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3];
4361 asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4];
4362 asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5];
4363 asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6];
4364 asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7];
4365 asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8];
4368 if (asd->params.color_effect != V4L2_COLORFX_SEPIA &&
4369 asd->params.color_effect != V4L2_COLORFX_BW) {
4370 memcpy(&asd->params.css_param.cc_config, &config->cc_config,
4371 sizeof(struct ia_css_cc_config));
4372 asd->params.config.cc_config = &asd->params.css_param.cc_config;
4375 asd->params.config.wb_config = &asd->params.css_param.wb_config;
4376 asd->params.config.ob_config = &asd->params.css_param.ob_config;
4377 asd->params.config.de_config = &asd->params.css_param.de_config;
4378 asd->params.config.dz_config = &asd->params.css_param.dz_config;
4379 asd->params.config.ce_config = &asd->params.css_param.ce_config;
4380 asd->params.config.dp_config = &asd->params.css_param.dp_config;
4381 asd->params.config.nr_config = &asd->params.css_param.nr_config;
4382 asd->params.config.ee_config = &asd->params.css_param.ee_config;
4383 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
4384 asd->params.css_update_params_needed = true;
4390 * Function to configure color effect of the image
4392 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag,
4395 struct ia_css_cc_config *cc_config = NULL;
4396 struct ia_css_macc_table *macc_table = NULL;
4397 struct ia_css_ctc_table *ctc_table = NULL;
4399 struct v4l2_control control;
4400 struct atomisp_device *isp = asd->isp;
4403 *effect = asd->params.color_effect;
4407 control.id = V4L2_CID_COLORFX;
4408 control.value = *effect;
4410 v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler,
4413 * if set color effect to sensor successfully, return
4417 asd->params.color_effect = (u32)*effect;
4421 if (*effect == asd->params.color_effect)
4425 * isp_subdev->params.macc_en should be set to false.
4427 asd->params.macc_en = false;
4430 case V4L2_COLORFX_NONE:
4431 macc_table = &asd->params.css_param.macc_table;
4432 asd->params.macc_en = true;
4434 case V4L2_COLORFX_SEPIA:
4435 cc_config = &sepia_cc_config;
4437 case V4L2_COLORFX_NEGATIVE:
4438 cc_config = &nega_cc_config;
4440 case V4L2_COLORFX_BW:
4441 cc_config = &mono_cc_config;
4443 case V4L2_COLORFX_SKY_BLUE:
4444 macc_table = &blue_macc_table;
4445 asd->params.macc_en = true;
4447 case V4L2_COLORFX_GRASS_GREEN:
4448 macc_table = &green_macc_table;
4449 asd->params.macc_en = true;
4451 case V4L2_COLORFX_SKIN_WHITEN_LOW:
4452 macc_table = &skin_low_macc_table;
4453 asd->params.macc_en = true;
4455 case V4L2_COLORFX_SKIN_WHITEN:
4456 macc_table = &skin_medium_macc_table;
4457 asd->params.macc_en = true;
4459 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
4460 macc_table = &skin_high_macc_table;
4461 asd->params.macc_en = true;
4463 case V4L2_COLORFX_VIVID:
4464 ctc_table = &vivid_ctc_table;
4469 atomisp_update_capture_mode(asd);
4472 asd->params.config.cc_config = cc_config;
4474 asd->params.config.macc_table = macc_table;
4476 atomisp_css_set_ctc_table(asd, ctc_table);
4477 asd->params.color_effect = (u32)*effect;
4478 asd->params.css_update_params_needed = true;
4483 * Function to configure bad pixel correction
4485 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag,
4489 *value = asd->params.bad_pixel_en;
4492 asd->params.bad_pixel_en = !!*value;
4498 * Function to configure bad pixel correction params
4500 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag,
4501 struct atomisp_dp_config *config)
4504 /* Get bad pixel from current setup */
4505 if (atomisp_css_get_dp_config(asd, config))
4508 /* Set bad pixel to isp parameters */
4509 memcpy(&asd->params.css_param.dp_config, config,
4510 sizeof(asd->params.css_param.dp_config));
4511 asd->params.config.dp_config = &asd->params.css_param.dp_config;
4512 asd->params.css_update_params_needed = true;
4519 * Function to enable/disable video image stablization
4521 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag,
4525 *value = asd->params.video_dis_en;
4527 asd->params.video_dis_en = !!*value;
4533 * Function to configure fixed pattern noise
4535 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag,
4539 *value = asd->params.fpn_en;
4544 asd->params.fpn_en = false;
4548 /* Add function to get black from from sensor with shutter off */
4553 atomisp_bytesperline_to_padded_width(unsigned int bytesperline,
4554 enum ia_css_frame_format format)
4557 case IA_CSS_FRAME_FORMAT_UYVY:
4558 case IA_CSS_FRAME_FORMAT_YUYV:
4559 case IA_CSS_FRAME_FORMAT_RAW:
4560 case IA_CSS_FRAME_FORMAT_RGB565:
4561 return bytesperline / 2;
4562 case IA_CSS_FRAME_FORMAT_RGBA888:
4563 return bytesperline / 4;
4564 /* The following cases could be removed, but we leave them
4565 in to document the formats that are included. */
4566 case IA_CSS_FRAME_FORMAT_NV11:
4567 case IA_CSS_FRAME_FORMAT_NV12:
4568 case IA_CSS_FRAME_FORMAT_NV16:
4569 case IA_CSS_FRAME_FORMAT_NV21:
4570 case IA_CSS_FRAME_FORMAT_NV61:
4571 case IA_CSS_FRAME_FORMAT_YV12:
4572 case IA_CSS_FRAME_FORMAT_YV16:
4573 case IA_CSS_FRAME_FORMAT_YUV420:
4574 case IA_CSS_FRAME_FORMAT_YUV420_16:
4575 case IA_CSS_FRAME_FORMAT_YUV422:
4576 case IA_CSS_FRAME_FORMAT_YUV422_16:
4577 case IA_CSS_FRAME_FORMAT_YUV444:
4578 case IA_CSS_FRAME_FORMAT_YUV_LINE:
4579 case IA_CSS_FRAME_FORMAT_PLANAR_RGB888:
4580 case IA_CSS_FRAME_FORMAT_QPLANE6:
4581 case IA_CSS_FRAME_FORMAT_BINARY_8:
4583 return bytesperline;
4588 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg,
4589 struct ia_css_frame **result)
4591 struct ia_css_frame *res = NULL;
4592 unsigned int padded_width;
4593 enum ia_css_frame_format sh_format;
4594 char *tmp_buf = NULL;
4597 sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat);
4598 padded_width = atomisp_bytesperline_to_padded_width(
4599 arg->fmt.bytesperline, sh_format);
4601 /* Note: the padded width on an ia_css_frame is in elements, not in
4602 bytes. The RAW frame we use here should always be a 16bit RAW
4603 frame. This is why we bytesperline/2 is equal to the padded with */
4604 if (ia_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height,
4605 sh_format, padded_width, 0)) {
4610 tmp_buf = vmalloc(arg->fmt.sizeimage);
4615 if (copy_from_user(tmp_buf, (void __user __force *)arg->base,
4616 arg->fmt.sizeimage)) {
4621 if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) {
4628 ia_css_frame_free(res);
4636 * Function to configure fixed pattern noise table
4638 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd,
4639 struct v4l2_framebuffer *arg)
4641 struct ia_css_frame *raw_black_frame = NULL;
4647 ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame);
4651 if (sh_css_set_black_frame(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream,
4652 raw_black_frame) != 0)
4655 ia_css_frame_free(raw_black_frame);
4660 * Function to configure false color correction
4662 int atomisp_false_color(struct atomisp_sub_device *asd, int flag,
4665 /* Get nr config from current setup */
4667 *value = asd->params.false_color;
4671 /* Set nr config to isp parameters */
4673 asd->params.config.de_config = NULL;
4675 asd->params.css_param.de_config.pixelnoise = 0;
4676 asd->params.config.de_config = &asd->params.css_param.de_config;
4678 asd->params.css_update_params_needed = true;
4679 asd->params.false_color = *value;
4684 * Function to configure bad pixel correction params
4686 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag,
4687 struct atomisp_de_config *config)
4690 /* Get false color from current setup */
4691 if (atomisp_css_get_de_config(asd, config))
4694 /* Set false color to isp parameters */
4695 memcpy(&asd->params.css_param.de_config, config,
4696 sizeof(asd->params.css_param.de_config));
4697 asd->params.config.de_config = &asd->params.css_param.de_config;
4698 asd->params.css_update_params_needed = true;
4705 * Function to configure white balance params
4707 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag,
4708 struct atomisp_wb_config *config)
4711 /* Get white balance from current setup */
4712 if (atomisp_css_get_wb_config(asd, config))
4715 /* Set white balance to isp parameters */
4716 memcpy(&asd->params.css_param.wb_config, config,
4717 sizeof(asd->params.css_param.wb_config));
4718 asd->params.config.wb_config = &asd->params.css_param.wb_config;
4719 asd->params.css_update_params_needed = true;
4725 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag,
4726 struct atomisp_3a_config *config)
4728 struct atomisp_device *isp = asd->isp;
4730 dev_dbg(isp->dev, ">%s %d\n", __func__, flag);
4733 /* Get white balance from current setup */
4734 if (atomisp_css_get_3a_config(asd, config))
4737 /* Set white balance to isp parameters */
4738 memcpy(&asd->params.css_param.s3a_config, config,
4739 sizeof(asd->params.css_param.s3a_config));
4740 asd->params.config.s3a_config = &asd->params.css_param.s3a_config;
4741 asd->params.css_update_params_needed = true;
4744 dev_dbg(isp->dev, "<%s %d\n", __func__, flag);
4749 * Function to setup digital zoom
4751 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag,
4755 struct atomisp_device *isp = asd->isp;
4757 unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR;
4760 atomisp_css_get_zoom_factor(asd, &zoom);
4761 *value = max_zoom - zoom;
4766 zoom = max_zoom - min_t(u32, max_zoom - 1, *value);
4767 atomisp_css_set_zoom_factor(asd, zoom);
4769 dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom);
4770 asd->params.css_update_params_needed = true;
4777 * Function to get sensor specific info for current resolution,
4778 * which will be used for auto exposure conversion.
4780 int atomisp_get_sensor_mode_data(struct atomisp_sub_device *asd,
4781 struct atomisp_sensor_mode_data *config)
4783 struct camera_mipi_info *mipi_info;
4784 struct atomisp_device *isp = asd->isp;
4786 mipi_info = atomisp_to_sensor_mipi_info(
4787 isp->inputs[asd->input_curr].camera);
4791 memcpy(config, &mipi_info->data, sizeof(*config));
4795 int atomisp_get_fmt(struct video_device *vdev, struct v4l2_format *f)
4797 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4799 f->fmt.pix = pipe->pix;
4804 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd,
4805 u16 stream_index, struct atomisp_input_stream_info *stream_info)
4809 /* assign virtual channel id return from sensor driver query */
4810 asd->stream_env[stream_index].ch_id = stream_info->ch_id;
4811 asd->stream_env[stream_index].isys_configs = stream_info->isys_configs;
4812 for (i = 0; i < stream_info->isys_configs; i++) {
4813 asd->stream_env[stream_index].isys_info[i].input_format =
4814 stream_info->isys_info[i].input_format;
4815 asd->stream_env[stream_index].isys_info[i].width =
4816 stream_info->isys_info[i].width;
4817 asd->stream_env[stream_index].isys_info[i].height =
4818 stream_info->isys_info[i].height;
4822 static void __atomisp_init_stream_info(u16 stream_index,
4823 struct atomisp_input_stream_info *stream_info)
4827 stream_info->enable = 1;
4828 stream_info->stream = stream_index;
4829 stream_info->ch_id = 0;
4830 stream_info->isys_configs = 0;
4831 for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) {
4832 stream_info->isys_info[i].input_format = 0;
4833 stream_info->isys_info[i].width = 0;
4834 stream_info->isys_info[i].height = 0;
4838 /* This function looks up the closest available resolution. */
4839 int atomisp_try_fmt(struct video_device *vdev, struct v4l2_pix_format *f,
4842 struct atomisp_device *isp = video_get_drvdata(vdev);
4843 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4844 struct v4l2_subdev_pad_config pad_cfg;
4845 struct v4l2_subdev_state pad_state = {
4848 struct v4l2_subdev_format format = {
4849 .which = V4L2_SUBDEV_FORMAT_TRY,
4852 struct v4l2_mbus_framefmt *snr_mbus_fmt = &format.format;
4853 const struct atomisp_format_bridge *fmt;
4854 struct atomisp_input_stream_info *stream_info =
4855 (struct atomisp_input_stream_info *)snr_mbus_fmt->reserved;
4857 int source_pad = atomisp_subdev_source_pad(vdev);
4860 if (!isp->inputs[asd->input_curr].camera)
4863 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
4864 fmt = atomisp_get_format_bridge(f->pixelformat);
4866 dev_err(isp->dev, "unsupported pixelformat!\n");
4867 fmt = atomisp_output_fmts;
4870 if (f->width <= 0 || f->height <= 0)
4873 snr_mbus_fmt->code = fmt->mbus_code;
4874 snr_mbus_fmt->width = f->width;
4875 snr_mbus_fmt->height = f->height;
4877 __atomisp_init_stream_info(stream_index, stream_info);
4879 dev_dbg(isp->dev, "try_mbus_fmt: asking for %ux%u\n",
4880 snr_mbus_fmt->width, snr_mbus_fmt->height);
4882 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
4883 pad, set_fmt, &pad_state, &format);
4887 dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n",
4888 snr_mbus_fmt->width, snr_mbus_fmt->height);
4890 fmt = atomisp_get_format_bridge_from_mbus(snr_mbus_fmt->code);
4892 dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
4893 snr_mbus_fmt->code);
4897 f->pixelformat = fmt->pixelformat;
4900 * If the format is jpeg or custom RAW, then the width and height will
4901 * not satisfy the normal atomisp requirements and no need to check
4902 * the below conditions. So just assign to what is being returned from
4903 * the sensor driver.
4905 if (f->pixelformat == V4L2_PIX_FMT_JPEG ||
4906 f->pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
4907 f->width = snr_mbus_fmt->width;
4908 f->height = snr_mbus_fmt->height;
4912 if (snr_mbus_fmt->width < f->width
4913 && snr_mbus_fmt->height < f->height) {
4914 f->width = snr_mbus_fmt->width;
4915 f->height = snr_mbus_fmt->height;
4916 /* Set the flag when resolution requested is
4917 * beyond the max value supported by sensor
4920 *res_overflow = true;
4924 f->width = rounddown(clamp_t(u32, f->width, ATOM_ISP_MIN_WIDTH,
4925 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
4926 f->height = rounddown(clamp_t(u32, f->height, ATOM_ISP_MIN_HEIGHT,
4927 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
4933 atomisp_try_fmt_file(struct atomisp_device *isp, struct v4l2_format *f)
4935 u32 width = f->fmt.pix.width;
4936 u32 height = f->fmt.pix.height;
4937 u32 pixelformat = f->fmt.pix.pixelformat;
4938 enum v4l2_field field = f->fmt.pix.field;
4941 if (!atomisp_get_format_bridge(pixelformat)) {
4942 dev_err(isp->dev, "Wrong output pixelformat\n");
4946 depth = get_pixel_depth(pixelformat);
4948 if (field == V4L2_FIELD_ANY) {
4949 field = V4L2_FIELD_NONE;
4950 } else if (field != V4L2_FIELD_NONE) {
4951 dev_err(isp->dev, "Wrong output field\n");
4955 f->fmt.pix.field = field;
4956 f->fmt.pix.width = clamp_t(u32,
4957 rounddown(width, (u32)ATOM_ISP_STEP_WIDTH),
4958 ATOM_ISP_MIN_WIDTH, ATOM_ISP_MAX_WIDTH);
4959 f->fmt.pix.height = clamp_t(u32, rounddown(height,
4960 (u32)ATOM_ISP_STEP_HEIGHT),
4961 ATOM_ISP_MIN_HEIGHT, ATOM_ISP_MAX_HEIGHT);
4962 f->fmt.pix.bytesperline = (width * depth) >> 3;
4967 enum mipi_port_id __get_mipi_port(struct atomisp_device *isp,
4968 enum atomisp_camera_port port)
4971 case ATOMISP_CAMERA_PORT_PRIMARY:
4972 return MIPI_PORT0_ID;
4973 case ATOMISP_CAMERA_PORT_SECONDARY:
4974 return MIPI_PORT1_ID;
4975 case ATOMISP_CAMERA_PORT_TERTIARY:
4976 if (MIPI_PORT1_ID + 1 != N_MIPI_PORT_ID)
4977 return MIPI_PORT1_ID + 1;
4980 dev_err(isp->dev, "unsupported port: %d\n", port);
4981 return MIPI_PORT0_ID;
4985 static inline int atomisp_set_sensor_mipi_to_isp(
4986 struct atomisp_sub_device *asd,
4987 enum atomisp_input_stream_id stream_id,
4988 struct camera_mipi_info *mipi_info)
4990 struct v4l2_control ctrl;
4991 struct atomisp_device *isp = asd->isp;
4992 const struct atomisp_in_fmt_conv *fc;
4994 unsigned int input_format, bayer_order;
4996 ctrl.id = V4L2_CID_LINK_FREQ;
4998 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl) == 0)
4999 mipi_freq = ctrl.value;
5001 if (asd->stream_env[stream_id].isys_configs == 1) {
5003 asd->stream_env[stream_id].isys_info[0].input_format;
5004 atomisp_css_isys_set_format(asd, stream_id,
5005 input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX);
5006 } else if (asd->stream_env[stream_id].isys_configs == 2) {
5007 atomisp_css_isys_two_stream_cfg_update_stream1(
5009 asd->stream_env[stream_id].isys_info[0].input_format,
5010 asd->stream_env[stream_id].isys_info[0].width,
5011 asd->stream_env[stream_id].isys_info[0].height);
5013 atomisp_css_isys_two_stream_cfg_update_stream2(
5015 asd->stream_env[stream_id].isys_info[1].input_format,
5016 asd->stream_env[stream_id].isys_info[1].width,
5017 asd->stream_env[stream_id].isys_info[1].height);
5020 /* Compatibility for sensors which provide no media bus code
5021 * in s_mbus_framefmt() nor support pad formats. */
5022 if (mipi_info->input_format != -1) {
5023 bayer_order = mipi_info->raw_bayer_order;
5025 /* Input stream config is still needs configured */
5026 /* TODO: Check if this is necessary */
5027 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5028 mipi_info->input_format);
5031 input_format = fc->atomisp_in_fmt;
5033 struct v4l2_mbus_framefmt *sink;
5035 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5036 V4L2_SUBDEV_FORMAT_ACTIVE,
5037 ATOMISP_SUBDEV_PAD_SINK);
5038 fc = atomisp_find_in_fmt_conv(sink->code);
5041 input_format = fc->atomisp_in_fmt;
5042 bayer_order = fc->bayer_order;
5045 atomisp_css_input_set_format(asd, stream_id, input_format);
5046 atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order);
5048 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5049 mipi_info->metadata_format);
5052 input_format = fc->atomisp_in_fmt;
5053 atomisp_css_input_configure_port(asd,
5054 __get_mipi_port(asd->isp, mipi_info->port),
5055 mipi_info->num_lanes,
5058 mipi_info->metadata_width,
5059 mipi_info->metadata_height);
5063 static int __enable_continuous_mode(struct atomisp_sub_device *asd,
5066 struct atomisp_device *isp = asd->isp;
5069 "continuous mode %d, raw buffers %d, stop preview %d\n",
5070 enable, asd->continuous_raw_buffer_size->val,
5071 !asd->continuous_viewfinder->val);
5074 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
5076 atomisp_update_capture_mode(asd);
5078 /* in case of ANR, force capture pipe to offline mode */
5079 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
5080 asd->params.low_light ? false : !enable);
5081 atomisp_css_preview_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
5083 atomisp_css_enable_continuous(asd, enable);
5084 atomisp_css_enable_cvf(asd, asd->continuous_viewfinder->val);
5086 atomisp_css_continuous_set_num_raw_frames(asd,
5087 asd->continuous_raw_buffer_size->val);
5090 atomisp_css_enable_raw_binning(asd, false);
5091 atomisp_css_input_set_two_pixels_per_clock(asd, false);
5094 if (isp->inputs[asd->input_curr].type != FILE_INPUT)
5095 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
5097 return atomisp_update_run_mode(asd);
5100 static int configure_pp_input_nop(struct atomisp_sub_device *asd,
5101 unsigned int width, unsigned int height)
5106 static int configure_output_nop(struct atomisp_sub_device *asd,
5107 unsigned int width, unsigned int height,
5108 unsigned int min_width,
5109 enum ia_css_frame_format sh_fmt)
5114 static int get_frame_info_nop(struct atomisp_sub_device *asd,
5115 struct ia_css_frame_info *finfo)
5121 * Resets CSS parameters that depend on input resolution.
5123 * Update params like CSS RAW binning, 2ppc mode and pp_input
5124 * which depend on input size, but are not automatically
5125 * handled in CSS when the input resolution is changed.
5127 static int css_input_resolution_changed(struct atomisp_sub_device *asd,
5128 struct v4l2_mbus_framefmt *ffmt)
5130 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
5133 dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n",
5134 ffmt->width, ffmt->height);
5136 #if defined(ISP2401_NEW_INPUT_SYSTEM)
5137 atomisp_css_input_set_two_pixels_per_clock(asd, false);
5139 atomisp_css_input_set_two_pixels_per_clock(asd, true);
5141 if (asd->continuous_mode->val) {
5142 /* Note for all checks: ffmt includes pad_w+pad_h */
5143 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
5144 (ffmt->width >= 2048 || ffmt->height >= 1536)) {
5146 * For preview pipe, enable only if resolution
5147 * is >= 3M for ISP2400.
5149 atomisp_css_enable_raw_binning(asd, true);
5153 * If sensor input changed, which means metadata resolution changed
5154 * together. Release all metadata buffers here to let it re-allocated
5155 * next time in reqbufs.
5157 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
5158 list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
5160 atomisp_css_free_metadata_buffer(md_buf);
5161 list_del(&md_buf->list);
5168 * TODO: atomisp_css_preview_configure_pp_input() not
5169 * reset due to CSS bug tracked as PSI BZ 115124
5173 static int atomisp_set_fmt_to_isp(struct video_device *vdev,
5174 struct ia_css_frame_info *output_info,
5175 struct ia_css_frame_info *raw_output_info,
5176 struct v4l2_pix_format *pix,
5177 unsigned int source_pad)
5179 struct camera_mipi_info *mipi_info;
5180 struct atomisp_device *isp = video_get_drvdata(vdev);
5181 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
5182 const struct atomisp_format_bridge *format;
5183 struct v4l2_rect *isp_sink_crop;
5184 enum ia_css_pipe_id pipe_id;
5185 struct v4l2_subdev_fh fh;
5186 int (*configure_output)(struct atomisp_sub_device *asd,
5187 unsigned int width, unsigned int height,
5188 unsigned int min_width,
5189 enum ia_css_frame_format sh_fmt) =
5190 configure_output_nop;
5191 int (*get_frame_info)(struct atomisp_sub_device *asd,
5192 struct ia_css_frame_info *finfo) =
5194 int (*configure_pp_input)(struct atomisp_sub_device *asd,
5195 unsigned int width, unsigned int height) =
5196 configure_pp_input_nop;
5197 u16 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
5198 const struct atomisp_in_fmt_conv *fc;
5201 v4l2_fh_init(&fh.vfh, vdev);
5203 isp_sink_crop = atomisp_subdev_get_rect(
5204 &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
5205 ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP);
5207 format = atomisp_get_format_bridge(pix->pixelformat);
5211 if (isp->inputs[asd->input_curr].type != TEST_PATTERN &&
5212 isp->inputs[asd->input_curr].type != FILE_INPUT) {
5213 mipi_info = atomisp_to_sensor_mipi_info(
5214 isp->inputs[asd->input_curr].camera);
5216 dev_err(isp->dev, "mipi_info is NULL\n");
5219 if (atomisp_set_sensor_mipi_to_isp(asd, stream_index,
5222 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
5223 mipi_info->input_format);
5225 fc = atomisp_find_in_fmt_conv(
5226 atomisp_subdev_get_ffmt(&asd->subdev,
5227 NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
5228 ATOMISP_SUBDEV_PAD_SINK)->code);
5231 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW &&
5232 raw_output_format_match_input(fc->atomisp_in_fmt,
5238 * Configure viewfinder also when vfpp is disabled: the
5239 * CSS still requires viewfinder configuration.
5241 if (asd->fmt_auto->val ||
5242 asd->vfpp->val != ATOMISP_VFPP_ENABLE) {
5243 struct v4l2_rect vf_size = {0};
5244 struct v4l2_mbus_framefmt vf_ffmt = {0};
5246 if (pix->width < 640 || pix->height < 480) {
5247 vf_size.width = pix->width;
5248 vf_size.height = pix->height;
5250 vf_size.width = 640;
5251 vf_size.height = 480;
5254 /* FIXME: proper format name for this one. See
5255 atomisp_output_fmts[] in atomisp_v4l2.c */
5256 vf_ffmt.code = V4L2_MBUS_FMT_CUSTOM_YUV420;
5258 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5259 V4L2_SUBDEV_FORMAT_ACTIVE,
5260 ATOMISP_SUBDEV_PAD_SOURCE_VF,
5261 V4L2_SEL_TGT_COMPOSE, 0, &vf_size);
5262 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
5263 V4L2_SUBDEV_FORMAT_ACTIVE,
5264 ATOMISP_SUBDEV_PAD_SOURCE_VF, &vf_ffmt);
5265 asd->video_out_vf.sh_fmt = IA_CSS_FRAME_FORMAT_NV12;
5267 if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
5268 atomisp_css_video_configure_viewfinder(asd,
5269 vf_size.width, vf_size.height, 0,
5270 asd->video_out_vf.sh_fmt);
5271 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5272 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5273 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO)
5274 atomisp_css_video_configure_viewfinder(asd,
5275 vf_size.width, vf_size.height, 0,
5276 asd->video_out_vf.sh_fmt);
5278 atomisp_css_capture_configure_viewfinder(asd,
5279 vf_size.width, vf_size.height, 0,
5280 asd->video_out_vf.sh_fmt);
5281 } else if (source_pad != ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5282 asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
5283 atomisp_css_capture_configure_viewfinder(asd,
5284 vf_size.width, vf_size.height, 0,
5285 asd->video_out_vf.sh_fmt);
5289 if (asd->continuous_mode->val) {
5290 ret = __enable_continuous_mode(asd, true);
5295 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
5297 for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++)
5298 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipe_extra_configs[i].disable_vf_pp = asd->vfpp->val != ATOMISP_VFPP_ENABLE;
5300 /* ISP2401 new input system need to use copy pipe */
5301 if (asd->copy_mode) {
5302 pipe_id = IA_CSS_PIPE_ID_COPY;
5303 atomisp_css_capture_enable_online(asd, stream_index, false);
5304 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
5305 /* video same in continuouscapture and online modes */
5306 configure_output = atomisp_css_video_configure_output;
5307 get_frame_info = atomisp_css_video_get_output_frame_info;
5308 pipe_id = IA_CSS_PIPE_ID_VIDEO;
5309 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5310 if (!asd->continuous_mode->val) {
5311 configure_output = atomisp_css_video_configure_output;
5313 atomisp_css_video_get_output_frame_info;
5314 pipe_id = IA_CSS_PIPE_ID_VIDEO;
5316 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
5317 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
5319 atomisp_css_video_configure_output;
5321 atomisp_css_video_get_output_frame_info;
5322 configure_pp_input =
5323 atomisp_css_video_configure_pp_input;
5324 pipe_id = IA_CSS_PIPE_ID_VIDEO;
5327 atomisp_css_capture_configure_output;
5329 atomisp_css_capture_get_output_frame_info;
5330 configure_pp_input =
5331 atomisp_css_capture_configure_pp_input;
5332 pipe_id = IA_CSS_PIPE_ID_CAPTURE;
5334 atomisp_update_capture_mode(asd);
5335 atomisp_css_capture_enable_online(asd, stream_index, false);
5338 } else if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
5339 configure_output = atomisp_css_preview_configure_output;
5340 get_frame_info = atomisp_css_preview_get_output_frame_info;
5341 configure_pp_input = atomisp_css_preview_configure_pp_input;
5342 pipe_id = IA_CSS_PIPE_ID_PREVIEW;
5344 /* CSS doesn't support low light mode on SOC cameras, so disable
5345 * it. FIXME: if this is done elsewhere, it gives corrupted
5346 * colors into thumbnail image.
5348 if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
5349 asd->params.low_light = false;
5351 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
5352 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
5353 atomisp_css_enable_dz(asd, false);
5355 atomisp_update_capture_mode(asd);
5358 if (!asd->continuous_mode->val)
5359 /* in case of ANR, force capture pipe to offline mode */
5360 atomisp_css_capture_enable_online(asd, stream_index,
5361 asd->params.low_light ?
5362 false : asd->params.online_process);
5364 configure_output = atomisp_css_capture_configure_output;
5365 get_frame_info = atomisp_css_capture_get_output_frame_info;
5366 configure_pp_input = atomisp_css_capture_configure_pp_input;
5367 pipe_id = IA_CSS_PIPE_ID_CAPTURE;
5369 if (!asd->params.online_process &&
5370 !asd->continuous_mode->val) {
5371 ret = atomisp_css_capture_get_output_raw_frame_info(asd,
5376 if (!asd->continuous_mode->val && asd->run_mode->val
5377 != ATOMISP_RUN_MODE_STILL_CAPTURE) {
5379 "Need to set the running mode first\n");
5380 asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE;
5385 * to SOC camera, use yuvpp pipe.
5387 if (ATOMISP_USE_YUVPP(asd))
5388 pipe_id = IA_CSS_PIPE_ID_YUVPP;
5391 ret = atomisp_css_copy_configure_output(asd, stream_index,
5392 pix->width, pix->height,
5393 format->planar ? pix->bytesperline :
5394 pix->bytesperline * 8 / format->depth,
5397 ret = configure_output(asd, pix->width, pix->height,
5398 format->planar ? pix->bytesperline :
5399 pix->bytesperline * 8 / format->depth,
5402 dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n",
5403 pix->width, pix->height, format->sh_fmt);
5407 ret = configure_pp_input(asd, isp_sink_crop->width, isp_sink_crop->height);
5409 dev_err(isp->dev, "configure_pp_input %ux%u\n",
5410 isp_sink_crop->width,
5411 isp_sink_crop->height);
5415 ret = atomisp_css_copy_get_output_frame_info(asd, stream_index,
5418 ret = get_frame_info(asd, output_info);
5420 dev_err(isp->dev, "get_frame_info %ux%u (padded to %u)\n",
5421 pix->width, pix->height, pix->bytesperline);
5425 atomisp_update_grid_info(asd, pipe_id, source_pad);
5427 /* Free the raw_dump buffer first */
5428 ia_css_frame_free(asd->raw_output_frame);
5429 asd->raw_output_frame = NULL;
5431 if (!asd->continuous_mode->val &&
5432 !asd->params.online_process && !isp->sw_contex.file_input &&
5433 ia_css_frame_allocate_from_info(&asd->raw_output_frame,
5440 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd,
5441 unsigned int width, unsigned int height,
5442 unsigned int *dvs_env_w, unsigned int *dvs_env_h)
5444 struct atomisp_device *isp = asd->isp;
5446 /* if subdev type is SOC camera,we do not need to set DVS */
5447 if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
5448 asd->params.video_dis_en = false;
5450 if (asd->params.video_dis_en &&
5451 asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
5452 /* envelope is 20% of the output resolution */
5454 * dvs envelope cannot be round up.
5455 * it would cause ISP timeout and color switch issue
5457 *dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH);
5458 *dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT);
5461 asd->params.dis_proj_data_valid = false;
5462 asd->params.css_update_params_needed = true;
5465 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd,
5466 int source_pad, struct v4l2_pix_format *f)
5468 #if defined(ISP2401_NEW_INPUT_SYSTEM)
5469 struct v4l2_mbus_framefmt *sink, *src;
5471 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5472 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK);
5473 src = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5474 V4L2_SUBDEV_FORMAT_ACTIVE, source_pad);
5476 if ((sink->code == src->code &&
5477 sink->width == f->width &&
5478 sink->height == f->height) ||
5479 ((asd->isp->inputs[asd->input_curr].type == SOC_CAMERA) &&
5480 (asd->isp->inputs[asd->input_curr].camera_caps->
5481 sensor[asd->sensor_curr].stream_num > 1)))
5482 asd->copy_mode = true;
5485 /* Only used for the new input system */
5486 asd->copy_mode = false;
5488 dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode);
5491 static int atomisp_set_fmt_to_snr(struct video_device *vdev,
5492 struct v4l2_pix_format *f, unsigned int pixelformat,
5493 unsigned int padding_w, unsigned int padding_h,
5494 unsigned int dvs_env_w, unsigned int dvs_env_h)
5496 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
5497 const struct atomisp_format_bridge *format;
5498 struct v4l2_subdev_pad_config pad_cfg;
5499 struct v4l2_subdev_state pad_state = {
5502 struct v4l2_subdev_format vformat = {
5503 .which = V4L2_SUBDEV_FORMAT_TRY,
5505 struct v4l2_mbus_framefmt *ffmt = &vformat.format;
5506 struct v4l2_mbus_framefmt *req_ffmt;
5507 struct atomisp_device *isp = asd->isp;
5508 struct atomisp_input_stream_info *stream_info =
5509 (struct atomisp_input_stream_info *)ffmt->reserved;
5510 u16 stream_index = ATOMISP_INPUT_STREAM_GENERAL;
5511 int source_pad = atomisp_subdev_source_pad(vdev);
5512 struct v4l2_subdev_fh fh;
5515 v4l2_fh_init(&fh.vfh, vdev);
5517 stream_index = atomisp_source_pad_to_stream_id(asd, source_pad);
5519 format = atomisp_get_format_bridge(pixelformat);
5523 v4l2_fill_mbus_format(ffmt, f, format->mbus_code);
5524 ffmt->height += padding_h + dvs_env_h;
5525 ffmt->width += padding_w + dvs_env_w;
5527 dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n",
5528 ffmt->width, ffmt->height, padding_w, padding_h,
5529 dvs_env_w, dvs_env_h);
5531 __atomisp_init_stream_info(stream_index, stream_info);
5535 /* Disable dvs if resolution can't be supported by sensor */
5536 if (asd->params.video_dis_en &&
5537 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
5538 vformat.which = V4L2_SUBDEV_FORMAT_TRY;
5539 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
5540 pad, set_fmt, &pad_state, &vformat);
5543 if (ffmt->width < req_ffmt->width ||
5544 ffmt->height < req_ffmt->height) {
5545 req_ffmt->height -= dvs_env_h;
5546 req_ffmt->width -= dvs_env_w;
5549 "can not enable video dis due to sensor limitation.");
5550 asd->params.video_dis_en = false;
5553 dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
5554 ffmt->width, ffmt->height);
5555 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
5556 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
5557 set_fmt, NULL, &vformat);
5561 __atomisp_update_stream_env(asd, stream_index, stream_info);
5563 dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
5564 ffmt->width, ffmt->height);
5566 if (ffmt->width < ATOM_ISP_STEP_WIDTH ||
5567 ffmt->height < ATOM_ISP_STEP_HEIGHT)
5570 if (asd->params.video_dis_en &&
5571 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
5572 (ffmt->width < req_ffmt->width || ffmt->height < req_ffmt->height)) {
5574 "can not enable video dis due to sensor limitation.");
5575 asd->params.video_dis_en = false;
5578 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
5579 V4L2_SUBDEV_FORMAT_ACTIVE,
5580 ATOMISP_SUBDEV_PAD_SINK, ffmt);
5582 return css_input_resolution_changed(asd, ffmt);
5585 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f)
5587 struct atomisp_device *isp = video_get_drvdata(vdev);
5588 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
5589 struct atomisp_sub_device *asd = pipe->asd;
5590 const struct atomisp_format_bridge *format_bridge;
5591 const struct atomisp_format_bridge *snr_format_bridge;
5592 struct ia_css_frame_info output_info, raw_output_info;
5593 struct v4l2_pix_format snr_fmt = f->fmt.pix;
5594 struct v4l2_pix_format backup_fmt = snr_fmt, s_fmt;
5595 unsigned int dvs_env_w = 0, dvs_env_h = 0;
5596 unsigned int padding_w = pad_w, padding_h = pad_h;
5597 bool res_overflow = false, crop_needs_override = false;
5598 struct v4l2_mbus_framefmt *isp_sink_fmt;
5599 struct v4l2_mbus_framefmt isp_source_fmt = {0};
5600 struct v4l2_rect isp_sink_crop;
5601 u16 source_pad = atomisp_subdev_source_pad(vdev);
5602 struct v4l2_subdev_fh fh;
5605 if (source_pad >= ATOMISP_SUBDEV_PADS_NUM)
5608 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED) {
5609 dev_warn(isp->dev, "ISP does not support set format while at streaming!\n");
5614 "setting resolution %ux%u on pad %u for asd%d, bytesperline %u\n",
5615 f->fmt.pix.width, f->fmt.pix.height, source_pad,
5616 asd->index, f->fmt.pix.bytesperline);
5618 v4l2_fh_init(&fh.vfh, vdev);
5620 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
5624 pipe->sh_fmt = format_bridge->sh_fmt;
5625 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
5627 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VF ||
5628 (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5629 && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)) {
5630 if (asd->fmt_auto->val) {
5631 struct v4l2_rect *capture_comp;
5632 struct v4l2_rect r = {0};
5634 r.width = f->fmt.pix.width;
5635 r.height = f->fmt.pix.height;
5637 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW)
5638 capture_comp = atomisp_subdev_get_rect(
5640 V4L2_SUBDEV_FORMAT_ACTIVE,
5641 ATOMISP_SUBDEV_PAD_SOURCE_VIDEO,
5642 V4L2_SEL_TGT_COMPOSE);
5644 capture_comp = atomisp_subdev_get_rect(
5646 V4L2_SUBDEV_FORMAT_ACTIVE,
5647 ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5648 V4L2_SEL_TGT_COMPOSE);
5650 if (capture_comp->width < r.width
5651 || capture_comp->height < r.height) {
5652 r.width = capture_comp->width;
5653 r.height = capture_comp->height;
5656 atomisp_subdev_set_selection(
5657 &asd->subdev, fh.state,
5658 V4L2_SUBDEV_FORMAT_ACTIVE, source_pad,
5659 V4L2_SEL_TGT_COMPOSE, 0, &r);
5661 f->fmt.pix.width = r.width;
5662 f->fmt.pix.height = r.height;
5665 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW &&
5666 (asd->isp->inputs[asd->input_curr].type == SOC_CAMERA) &&
5667 (asd->isp->inputs[asd->input_curr].camera_caps->
5668 sensor[asd->sensor_curr].stream_num > 1)) {
5669 /* For M10MO outputing YUV preview images. */
5671 atomisp_source_pad_to_stream_id(asd,
5672 ATOMISP_SUBDEV_PAD_SOURCE_VIDEO);
5674 ret = atomisp_css_copy_get_output_frame_info(asd,
5675 video_index, &output_info);
5678 "copy_get_output_frame_info ret %i", ret);
5681 if (!asd->yuvpp_mode) {
5683 * If viewfinder was configured into copy_mode,
5684 * we switch to using yuvpp pipe instead.
5686 asd->yuvpp_mode = true;
5687 ret = atomisp_css_copy_configure_output(
5688 asd, video_index, 0, 0, 0, 0);
5691 "failed to disable copy pipe");
5694 ret = atomisp_css_yuvpp_configure_output(
5696 output_info.res.width,
5697 output_info.res.height,
5698 output_info.padded_width,
5699 output_info.format);
5702 "failed to set up yuvpp pipe\n");
5705 atomisp_css_video_enable_online(asd, false);
5706 atomisp_css_preview_enable_online(asd,
5707 ATOMISP_INPUT_STREAM_GENERAL, false);
5709 atomisp_css_yuvpp_configure_viewfinder(asd, video_index,
5710 f->fmt.pix.width, f->fmt.pix.height,
5711 format_bridge->planar ? f->fmt.pix.bytesperline
5712 : f->fmt.pix.bytesperline * 8
5713 / format_bridge->depth, format_bridge->sh_fmt);
5714 atomisp_css_yuvpp_get_viewfinder_frame_info(
5715 asd, video_index, &output_info);
5716 } else if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
5717 atomisp_css_video_configure_viewfinder(asd,
5718 f->fmt.pix.width, f->fmt.pix.height,
5719 format_bridge->planar ? f->fmt.pix.bytesperline
5720 : f->fmt.pix.bytesperline * 8
5721 / format_bridge->depth, format_bridge->sh_fmt);
5722 atomisp_css_video_get_viewfinder_frame_info(asd,
5724 asd->copy_mode = false;
5726 atomisp_css_capture_configure_viewfinder(asd,
5727 f->fmt.pix.width, f->fmt.pix.height,
5728 format_bridge->planar ? f->fmt.pix.bytesperline
5729 : f->fmt.pix.bytesperline * 8
5730 / format_bridge->depth, format_bridge->sh_fmt);
5731 atomisp_css_capture_get_viewfinder_frame_info(asd,
5733 asd->copy_mode = false;
5739 * Check whether main resolution configured smaller
5740 * than snapshot resolution. If so, force main resolution
5741 * to be the same as snapshot resolution
5743 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE) {
5744 struct v4l2_rect *r;
5746 r = atomisp_subdev_get_rect(
5748 V4L2_SUBDEV_FORMAT_ACTIVE,
5749 ATOMISP_SUBDEV_PAD_SOURCE_VF, V4L2_SEL_TGT_COMPOSE);
5751 if (r->width && r->height
5752 && (r->width > f->fmt.pix.width
5753 || r->height > f->fmt.pix.height))
5755 "Main Resolution config smaller then Vf Resolution. Force to be equal with Vf Resolution.");
5758 /* Pipeline configuration done through subdevs. Bail out now. */
5759 if (!asd->fmt_auto->val)
5760 goto set_fmt_to_isp;
5762 /* get sensor resolution and format */
5763 ret = atomisp_try_fmt(vdev, &snr_fmt, &res_overflow);
5765 dev_warn(isp->dev, "Try format failed with error %d\n", ret);
5768 f->fmt.pix.width = snr_fmt.width;
5769 f->fmt.pix.height = snr_fmt.height;
5771 snr_format_bridge = atomisp_get_format_bridge(snr_fmt.pixelformat);
5772 if (!snr_format_bridge) {
5773 dev_warn(isp->dev, "Can't find bridge format\n");
5777 atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5778 V4L2_SUBDEV_FORMAT_ACTIVE,
5779 ATOMISP_SUBDEV_PAD_SINK)->code =
5780 snr_format_bridge->mbus_code;
5782 isp_sink_fmt = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5783 V4L2_SUBDEV_FORMAT_ACTIVE,
5784 ATOMISP_SUBDEV_PAD_SINK);
5786 isp_source_fmt.code = format_bridge->mbus_code;
5787 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
5788 V4L2_SUBDEV_FORMAT_ACTIVE,
5789 source_pad, &isp_source_fmt);
5791 if (!atomisp_subdev_format_conversion(asd, source_pad)) {
5794 } else if (IS_BYT) {
5799 /* construct resolution supported by isp */
5800 if (res_overflow && !asd->continuous_mode->val) {
5801 f->fmt.pix.width = rounddown(
5802 clamp_t(u32, f->fmt.pix.width - padding_w,
5804 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
5805 f->fmt.pix.height = rounddown(
5806 clamp_t(u32, f->fmt.pix.height - padding_h,
5807 ATOM_ISP_MIN_HEIGHT,
5808 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
5811 atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height,
5812 &dvs_env_w, &dvs_env_h);
5814 if (asd->continuous_mode->val) {
5815 struct v4l2_rect *r;
5817 r = atomisp_subdev_get_rect(
5819 V4L2_SUBDEV_FORMAT_ACTIVE,
5820 ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5821 V4L2_SEL_TGT_COMPOSE);
5823 * The ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE should get resolutions
5824 * properly set otherwise, it should not be the capture_pad.
5826 if (r->width && r->height)
5827 asd->capture_pad = ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE;
5829 asd->capture_pad = source_pad;
5831 asd->capture_pad = source_pad;
5834 * set format info to sensor
5835 * In continuous mode, resolution is set only if it is higher than
5836 * existing value. This because preview pipe will be configured after
5837 * capture pipe and usually has lower resolution than capture pipe.
5839 if (!asd->continuous_mode->val ||
5840 isp_sink_fmt->width < (f->fmt.pix.width + padding_w + dvs_env_w) ||
5841 isp_sink_fmt->height < (f->fmt.pix.height + padding_h +
5844 * For jpeg or custom raw format the sensor will return constant
5845 * width and height. Because we already had quried try_mbus_fmt,
5846 * f->fmt.pix.width and f->fmt.pix.height has been changed to
5847 * this fixed width and height. So we cannot select the correct
5848 * resolution with that information. So use the original width
5849 * and height while set_mbus_fmt() so actual resolutions are
5850 * being used in while set media bus format.
5853 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG ||
5854 f->fmt.pix.pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
5855 s_fmt.width = backup_fmt.width;
5856 s_fmt.height = backup_fmt.height;
5858 ret = atomisp_set_fmt_to_snr(vdev, &s_fmt,
5859 f->fmt.pix.pixelformat, padding_w,
5860 padding_h, dvs_env_w, dvs_env_h);
5863 "Set format to sensor failed with %d\n", ret);
5867 atomisp_csi_lane_config(isp);
5868 crop_needs_override = true;
5871 atomisp_check_copy_mode(asd, source_pad, &backup_fmt);
5872 asd->yuvpp_mode = false; /* Reset variable */
5874 isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL,
5875 V4L2_SUBDEV_FORMAT_ACTIVE,
5876 ATOMISP_SUBDEV_PAD_SINK,
5879 /* Try to enable YUV downscaling if ISP input is 10 % (either
5880 * width or height) bigger than the desired result. */
5881 if (isp_sink_crop.width * 9 / 10 < f->fmt.pix.width ||
5882 isp_sink_crop.height * 9 / 10 < f->fmt.pix.height ||
5883 (atomisp_subdev_format_conversion(asd, source_pad) &&
5884 ((asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
5885 !asd->continuous_mode->val) ||
5886 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) {
5887 /* for continuous mode, preview size might be smaller than
5888 * still capture size. if preview size still needs crop,
5889 * pick the larger one between crop size of preview and
5892 if (asd->continuous_mode->val
5893 && source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5894 && !crop_needs_override) {
5895 isp_sink_crop.width =
5896 max_t(unsigned int, f->fmt.pix.width,
5897 isp_sink_crop.width);
5898 isp_sink_crop.height =
5899 max_t(unsigned int, f->fmt.pix.height,
5900 isp_sink_crop.height);
5902 isp_sink_crop.width = f->fmt.pix.width;
5903 isp_sink_crop.height = f->fmt.pix.height;
5906 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5907 V4L2_SUBDEV_FORMAT_ACTIVE,
5908 ATOMISP_SUBDEV_PAD_SINK,
5910 V4L2_SEL_FLAG_KEEP_CONFIG,
5912 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5913 V4L2_SUBDEV_FORMAT_ACTIVE,
5914 source_pad, V4L2_SEL_TGT_COMPOSE,
5916 } else if (IS_MOFD) {
5917 struct v4l2_rect main_compose = {0};
5919 main_compose.width = isp_sink_crop.width;
5920 main_compose.height =
5921 DIV_ROUND_UP(main_compose.width * f->fmt.pix.height,
5923 if (main_compose.height > isp_sink_crop.height) {
5924 main_compose.height = isp_sink_crop.height;
5925 main_compose.width =
5926 DIV_ROUND_UP(main_compose.height *
5931 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5932 V4L2_SUBDEV_FORMAT_ACTIVE,
5934 V4L2_SEL_TGT_COMPOSE, 0,
5937 struct v4l2_rect sink_crop = {0};
5938 struct v4l2_rect main_compose = {0};
5940 main_compose.width = f->fmt.pix.width;
5941 main_compose.height = f->fmt.pix.height;
5943 /* WORKAROUND: this override is universally enabled in
5944 * GMIN to work around a CTS failures (GMINL-539)
5945 * which appears to be related by a hardware
5946 * performance limitation. It's unclear why this
5947 * particular code triggers the issue. */
5948 if (!IS_ISP2401 || crop_needs_override) {
5949 if (isp_sink_crop.width * main_compose.height >
5950 isp_sink_crop.height * main_compose.width) {
5951 sink_crop.height = isp_sink_crop.height;
5952 sink_crop.width = DIV_NEAREST_STEP(
5956 ATOM_ISP_STEP_WIDTH);
5958 sink_crop.width = isp_sink_crop.width;
5959 sink_crop.height = DIV_NEAREST_STEP(
5963 ATOM_ISP_STEP_HEIGHT);
5965 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5966 V4L2_SUBDEV_FORMAT_ACTIVE,
5967 ATOMISP_SUBDEV_PAD_SINK,
5969 V4L2_SEL_FLAG_KEEP_CONFIG,
5972 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5973 V4L2_SUBDEV_FORMAT_ACTIVE,
5975 V4L2_SEL_TGT_COMPOSE, 0,
5980 ret = atomisp_set_fmt_to_isp(vdev, &output_info, &raw_output_info,
5981 &f->fmt.pix, source_pad);
5983 dev_warn(isp->dev, "Can't set format on ISP. Error %d\n", ret);
5987 pipe->pix.width = f->fmt.pix.width;
5988 pipe->pix.height = f->fmt.pix.height;
5989 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
5990 if (format_bridge->planar) {
5991 pipe->pix.bytesperline = output_info.padded_width;
5992 pipe->pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height *
5993 DIV_ROUND_UP(format_bridge->depth *
5994 output_info.padded_width, 8));
5996 pipe->pix.bytesperline =
5997 DIV_ROUND_UP(format_bridge->depth *
5998 output_info.padded_width, 8);
5999 pipe->pix.sizeimage =
6000 PAGE_ALIGN(f->fmt.pix.height * pipe->pix.bytesperline);
6002 dev_dbg(isp->dev, "%s: image size: %d, %d bytes per line\n",
6003 __func__, pipe->pix.sizeimage, pipe->pix.bytesperline);
6005 if (f->fmt.pix.field == V4L2_FIELD_ANY)
6006 f->fmt.pix.field = V4L2_FIELD_NONE;
6007 pipe->pix.field = f->fmt.pix.field;
6009 f->fmt.pix = pipe->pix;
6010 f->fmt.pix.priv = PAGE_ALIGN(pipe->pix.width *
6011 pipe->pix.height * 2);
6013 pipe->capq.field = f->fmt.pix.field;
6016 * If in video 480P case, no GFX throttle
6018 if (asd->run_mode->val == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
6019 f->fmt.pix.width == 720 && f->fmt.pix.height == 480)
6020 isp->need_gfx_throttle = false;
6022 isp->need_gfx_throttle = true;
6027 int atomisp_set_fmt_file(struct video_device *vdev, struct v4l2_format *f)
6029 struct atomisp_device *isp = video_get_drvdata(vdev);
6030 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
6031 struct atomisp_sub_device *asd = pipe->asd;
6032 struct v4l2_mbus_framefmt ffmt = {0};
6033 const struct atomisp_format_bridge *format_bridge;
6034 struct v4l2_subdev_fh fh;
6037 v4l2_fh_init(&fh.vfh, vdev);
6039 dev_dbg(isp->dev, "setting fmt %ux%u 0x%x for file inject\n",
6040 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
6041 ret = atomisp_try_fmt_file(isp, f);
6043 dev_err(isp->dev, "atomisp_try_fmt_file err: %d\n", ret);
6047 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
6048 if (!format_bridge) {
6049 dev_dbg(isp->dev, "atomisp_get_format_bridge err! fmt:0x%x\n",
6050 f->fmt.pix.pixelformat);
6054 pipe->pix = f->fmt.pix;
6055 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_FIFO);
6056 atomisp_css_input_configure_port(asd,
6057 __get_mipi_port(isp, ATOMISP_CAMERA_PORT_PRIMARY), 2, 0xffff4,
6059 ffmt.width = f->fmt.pix.width;
6060 ffmt.height = f->fmt.pix.height;
6061 ffmt.code = format_bridge->mbus_code;
6063 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
6064 V4L2_SUBDEV_FORMAT_ACTIVE,
6065 ATOMISP_SUBDEV_PAD_SINK, &ffmt);
6070 int atomisp_set_shading_table(struct atomisp_sub_device *asd,
6071 struct atomisp_shading_table *user_shading_table)
6073 struct ia_css_shading_table *shading_table;
6074 struct ia_css_shading_table *free_table;
6075 unsigned int len_table;
6079 if (!user_shading_table)
6082 if (!user_shading_table->enable) {
6083 asd->params.config.shading_table = NULL;
6084 asd->params.sc_en = false;
6088 /* If enabling, all tables must be set */
6089 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
6090 if (!user_shading_table->data[i])
6094 /* Shading table size per color */
6096 if (user_shading_table->width > ISP2400_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
6097 user_shading_table->height > ISP2400_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
6100 if (user_shading_table->width > ISP2401_SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
6101 user_shading_table->height > ISP2401_SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
6105 shading_table = atomisp_css_shading_table_alloc(
6106 user_shading_table->width, user_shading_table->height);
6110 len_table = user_shading_table->width * user_shading_table->height *
6111 ATOMISP_SC_TYPE_SIZE;
6112 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
6113 ret = copy_from_user(shading_table->data[i],
6114 (void __user *)user_shading_table->data[i],
6117 free_table = shading_table;
6122 shading_table->sensor_width = user_shading_table->sensor_width;
6123 shading_table->sensor_height = user_shading_table->sensor_height;
6124 shading_table->fraction_bits = user_shading_table->fraction_bits;
6126 free_table = asd->params.css_param.shading_table;
6127 asd->params.css_param.shading_table = shading_table;
6128 asd->params.config.shading_table = shading_table;
6129 asd->params.sc_en = true;
6133 atomisp_css_shading_table_free(free_table);
6138 /*Turn off ISP dphy */
6139 int atomisp_ospm_dphy_down(struct atomisp_device *isp)
6141 struct pci_dev *pdev = to_pci_dev(isp->dev);
6142 unsigned long flags;
6145 dev_dbg(isp->dev, "%s\n", __func__);
6147 /* if ISP timeout, we can force powerdown */
6148 if (isp->isp_timeout)
6151 if (!atomisp_dev_users(isp))
6154 spin_lock_irqsave(&isp->lock, flags);
6155 isp->sw_contex.power_state = ATOM_ISP_POWER_DOWN;
6156 spin_unlock_irqrestore(&isp->lock, flags);
6159 * MRFLD IUNIT DPHY is located in an always-power-on island
6160 * MRFLD HW design need all CSI ports are disabled before
6161 * powering down the IUNIT.
6163 pci_read_config_dword(pdev, MRFLD_PCI_CSI_CONTROL, ®);
6164 reg |= MRFLD_ALL_CSI_PORTS_OFF_MASK;
6165 pci_write_config_dword(pdev, MRFLD_PCI_CSI_CONTROL, reg);
6169 /*Turn on ISP dphy */
6170 int atomisp_ospm_dphy_up(struct atomisp_device *isp)
6172 unsigned long flags;
6174 dev_dbg(isp->dev, "%s\n", __func__);
6176 spin_lock_irqsave(&isp->lock, flags);
6177 isp->sw_contex.power_state = ATOM_ISP_POWER_UP;
6178 spin_unlock_irqrestore(&isp->lock, flags);
6183 int atomisp_exif_makernote(struct atomisp_sub_device *asd,
6184 struct atomisp_makernote_info *config)
6186 struct v4l2_control ctrl;
6187 struct atomisp_device *isp = asd->isp;
6189 ctrl.id = V4L2_CID_FOCAL_ABSOLUTE;
6191 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6192 dev_warn(isp->dev, "failed to g_ctrl for focal length\n");
6195 config->focal_length = ctrl.value;
6198 ctrl.id = V4L2_CID_FNUMBER_ABSOLUTE;
6200 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6201 dev_warn(isp->dev, "failed to g_ctrl for f-number\n");
6204 config->f_number_curr = ctrl.value;
6207 ctrl.id = V4L2_CID_FNUMBER_RANGE;
6209 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl)) {
6210 dev_warn(isp->dev, "failed to g_ctrl for f number range\n");
6213 config->f_number_range = ctrl.value;
6219 int atomisp_offline_capture_configure(struct atomisp_sub_device *asd,
6220 struct atomisp_cont_capture_conf *cvf_config)
6222 struct v4l2_ctrl *c;
6225 * In case of M10MO ZSL capture case, we need to issue a separate
6226 * capture request to M10MO which will output captured jpeg image
6229 asd->isp->inputs[asd->input_curr].camera->ctrl_handler,
6230 V4L2_CID_START_ZSL_CAPTURE);
6234 dev_dbg(asd->isp->dev, "%s trigger ZSL capture request\n",
6236 /* TODO: use the cvf_config */
6237 ret = v4l2_ctrl_s_ctrl(c, 1);
6241 return v4l2_ctrl_s_ctrl(c, 0);
6244 asd->params.offline_parm = *cvf_config;
6246 if (asd->params.offline_parm.num_captures) {
6247 if (asd->streaming == ATOMISP_DEVICE_STREAMING_DISABLED) {
6248 unsigned int init_raw_num;
6250 if (asd->enable_raw_buffer_lock->val) {
6252 ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES_LOCK_EN;
6253 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
6254 asd->params.video_dis_en)
6256 ATOMISP_CSS2_NUM_DVS_FRAME_DELAY;
6259 ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES;
6262 /* TODO: this can be removed once user-space
6263 * has been updated to use control API */
6264 asd->continuous_raw_buffer_size->val =
6266 asd->continuous_raw_buffer_size->val,
6267 asd->params.offline_parm.
6268 num_captures + init_raw_num);
6269 asd->continuous_raw_buffer_size->val =
6270 min_t(int, ATOMISP_CONT_RAW_FRAMES,
6271 asd->continuous_raw_buffer_size->val);
6273 asd->continuous_mode->val = true;
6275 asd->continuous_mode->val = false;
6276 __enable_continuous_mode(asd, false);
6283 * set auto exposure metering window to camera sensor
6285 int atomisp_s_ae_window(struct atomisp_sub_device *asd,
6286 struct atomisp_ae_window *arg)
6288 struct atomisp_device *isp = asd->isp;
6289 /* Coverity CID 298071 - initialzize struct */
6290 struct v4l2_subdev_selection sel = { 0 };
6292 sel.r.left = arg->x_left;
6293 sel.r.top = arg->y_top;
6294 sel.r.width = arg->x_right - arg->x_left + 1;
6295 sel.r.height = arg->y_bottom - arg->y_top + 1;
6297 if (v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
6298 pad, set_selection, NULL, &sel)) {
6299 dev_err(isp->dev, "failed to call sensor set_selection.\n");
6306 int atomisp_flash_enable(struct atomisp_sub_device *asd, int num_frames)
6308 struct atomisp_device *isp = asd->isp;
6310 if (num_frames < 0) {
6311 dev_dbg(isp->dev, "%s ERROR: num_frames: %d\n", __func__,
6315 /* a requested flash is still in progress. */
6316 if (num_frames && asd->params.flash_state != ATOMISP_FLASH_IDLE) {
6317 dev_dbg(isp->dev, "%s flash busy: %d frames left: %d\n",
6318 __func__, asd->params.flash_state,
6319 asd->params.num_flash_frames);
6323 asd->params.num_flash_frames = num_frames;
6324 asd->params.flash_state = ATOMISP_FLASH_REQUESTED;
6328 int atomisp_source_pad_to_stream_id(struct atomisp_sub_device *asd,
6329 uint16_t source_pad)
6332 struct atomisp_device *isp = asd->isp;
6334 if (isp->inputs[asd->input_curr].camera_caps->
6335 sensor[asd->sensor_curr].stream_num == 1)
6336 return ATOMISP_INPUT_STREAM_GENERAL;
6338 switch (source_pad) {
6339 case ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE:
6340 stream_id = ATOMISP_INPUT_STREAM_CAPTURE;
6342 case ATOMISP_SUBDEV_PAD_SOURCE_VF:
6343 stream_id = ATOMISP_INPUT_STREAM_POSTVIEW;
6345 case ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW:
6346 stream_id = ATOMISP_INPUT_STREAM_PREVIEW;
6348 case ATOMISP_SUBDEV_PAD_SOURCE_VIDEO:
6349 stream_id = ATOMISP_INPUT_STREAM_VIDEO;
6352 stream_id = ATOMISP_INPUT_STREAM_GENERAL;
6358 bool atomisp_is_vf_pipe(struct atomisp_video_pipe *pipe)
6360 struct atomisp_sub_device *asd = pipe->asd;
6362 if (pipe == &asd->video_out_vf)
6365 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
6366 pipe == &asd->video_out_preview)
6372 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id)
6374 struct atomisp_device *isp = asd->isp;
6376 if (!asd->enable_raw_buffer_lock->val) {
6377 dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__);
6380 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED) {
6381 dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n",
6382 __func__, exp_id, asd->streaming);
6385 if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) {
6386 dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id);
6392 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd)
6394 unsigned long flags;
6396 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6397 memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap));
6398 asd->raw_buffer_locked_count = 0;
6399 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6402 int atomisp_set_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
6405 unsigned long flags;
6407 if (__checking_exp_id(asd, exp_id))
6410 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6412 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6413 (*bitmap) |= (1 << bit);
6414 asd->raw_buffer_locked_count++;
6415 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6417 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
6418 __func__, exp_id, asd->raw_buffer_locked_count);
6420 /* Check if the raw buffer after next is still locked!!! */
6422 if (exp_id > ATOMISP_MAX_EXP_ID)
6423 exp_id -= ATOMISP_MAX_EXP_ID;
6424 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6426 if ((*bitmap) & (1 << bit)) {
6429 /* WORKAROUND unlock the raw buffer compulsively */
6430 ret = atomisp_css_exp_id_unlock(asd, exp_id);
6432 dev_err(asd->isp->dev,
6433 "%s exp_id is wrapping back to %d but force unlock failed,, err %d.\n",
6434 __func__, exp_id, ret);
6438 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6439 (*bitmap) &= ~(1 << bit);
6440 asd->raw_buffer_locked_count--;
6441 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6442 dev_warn(asd->isp->dev,
6443 "%s exp_id is wrapping back to %d but it is still locked so force unlock it, raw_buffer_locked_count %d\n",
6444 __func__, exp_id, asd->raw_buffer_locked_count);
6449 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id)
6452 unsigned long flags;
6455 if (__checking_exp_id(asd, exp_id))
6458 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6460 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6461 ret = ((*bitmap) & (1 << bit));
6462 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6466 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
6469 unsigned long flags;
6471 if (__is_raw_buffer_locked(asd, exp_id))
6474 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
6476 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
6477 (*bitmap) &= ~(1 << bit);
6478 asd->raw_buffer_locked_count--;
6479 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
6481 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
6482 __func__, exp_id, asd->raw_buffer_locked_count);
6486 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id)
6488 struct atomisp_device *isp = asd->isp;
6489 int value = *exp_id;
6492 ret = __is_raw_buffer_locked(asd, value);
6494 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
6498 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
6499 ret = atomisp_css_exp_id_capture(asd, value);
6501 dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value);
6507 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id)
6509 struct atomisp_device *isp = asd->isp;
6510 int value = *exp_id;
6513 ret = __clear_raw_buffer_bitmap(asd, value);
6515 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
6519 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
6520 ret = atomisp_css_exp_id_unlock(asd, value);
6522 dev_err(isp->dev, "%s exp_id %d failed, err %d.\n",
6523 __func__, value, ret);
6528 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd,
6529 unsigned int *enable)
6536 value = *enable > 0;
6538 atomisp_en_dz_capt_pipe(asd, value);
6543 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event)
6545 if (!event || asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
6548 dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n",
6552 case V4L2_EVENT_FRAME_SYNC:
6553 atomisp_sof_event(asd);
6555 case V4L2_EVENT_FRAME_END:
6556 atomisp_eof_event(asd, 0);
6558 case V4L2_EVENT_ATOMISP_3A_STATS_READY:
6559 atomisp_3a_stats_ready_event(asd, 0);
6561 case V4L2_EVENT_ATOMISP_METADATA_READY:
6562 atomisp_metadata_ready_event(asd, 0);
6571 static int atomisp_get_pipe_id(struct atomisp_video_pipe *pipe)
6573 struct atomisp_sub_device *asd = pipe->asd;
6575 if (ATOMISP_USE_YUVPP(asd)) {
6576 return IA_CSS_PIPE_ID_YUVPP;
6577 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
6578 return IA_CSS_PIPE_ID_VIDEO;
6579 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
6580 return IA_CSS_PIPE_ID_CAPTURE;
6581 } else if (pipe == &asd->video_out_video_capture) {
6582 return IA_CSS_PIPE_ID_VIDEO;
6583 } else if (pipe == &asd->video_out_vf) {
6584 return IA_CSS_PIPE_ID_CAPTURE;
6585 } else if (pipe == &asd->video_out_preview) {
6586 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
6587 return IA_CSS_PIPE_ID_VIDEO;
6589 return IA_CSS_PIPE_ID_PREVIEW;
6590 } else if (pipe == &asd->video_out_capture) {
6592 return IA_CSS_PIPE_ID_COPY;
6594 return IA_CSS_PIPE_ID_CAPTURE;
6598 dev_warn(asd->isp->dev, "%s failed to find proper pipe\n",
6600 return IA_CSS_PIPE_ID_CAPTURE;
6603 int atomisp_get_invalid_frame_num(struct video_device *vdev,
6604 int *invalid_frame_num)
6606 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
6607 struct atomisp_sub_device *asd = pipe->asd;
6608 enum ia_css_pipe_id pipe_id;
6609 struct ia_css_pipe_info p_info;
6612 if (asd->isp->inputs[asd->input_curr].camera_caps->
6613 sensor[asd->sensor_curr].stream_num > 1) {
6615 *invalid_frame_num = 0;
6619 pipe_id = atomisp_get_pipe_id(pipe);
6620 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipes[pipe_id]) {
6621 dev_warn(asd->isp->dev,
6622 "%s pipe %d has not been created yet, do SET_FMT first!\n",
6627 ret = ia_css_pipe_get_info(
6628 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
6629 .pipes[pipe_id], &p_info);
6631 *invalid_frame_num = p_info.num_invalid_frames;
6634 dev_warn(asd->isp->dev, "%s get pipe infor failed %d\n",