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>
34 #define CREATE_TRACE_POINTS
35 #include "atomisp_trace_event.h"
37 #include "atomisp_cmd.h"
38 #include "atomisp_common.h"
39 #include "atomisp_fops.h"
40 #include "atomisp_internal.h"
41 #include "atomisp_ioctl.h"
42 #include "atomisp-regs.h"
43 #include "atomisp_tables.h"
44 #include "atomisp_compat.h"
45 #include "atomisp_subdev.h"
46 #include "atomisp_dfs_tables.h"
50 #include "sh_css_hrt.h"
51 #include "sh_css_defs.h"
52 #include "system_global.h"
53 #include "sh_css_internal.h"
54 #include "sh_css_sp.h"
55 #include "gp_device.h"
56 #include "device_access.h"
59 #include "ia_css_types.h"
60 #include "ia_css_stream.h"
61 #include "ia_css_debug.h"
64 /* We should never need to run the flash for more than 2 frames.
65 * At 15fps this means 133ms. We set the timeout a bit longer.
66 * Each flash driver is supposed to set its own timeout, but
67 * just in case someone else changed the timeout, we set it
68 * here to make sure we don't damage the flash hardware. */
69 #define FLASH_TIMEOUT 800 /* ms */
74 void __user *user_ptr;
82 static int atomisp_set_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id);
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);
102 static unsigned short atomisp_get_sensor_fps(struct atomisp_sub_device *asd)
104 struct v4l2_subdev_frame_interval fi = { 0 };
105 struct atomisp_device *isp = asd->isp;
107 unsigned short fps = 0;
110 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
111 video, g_frame_interval, &fi);
113 if (!ret && fi.interval.numerator)
114 fps = fi.interval.denominator / fi.interval.numerator;
120 * DFS progress is shown as follows:
121 * 1. Target frequency is calculated according to FPS/Resolution/ISP running
123 * 2. Ratio is calculated using formula: 2 * HPLL / target frequency - 1
124 * with proper rounding.
125 * 3. Set ratio to ISPFREQ40, 1 to FREQVALID and ISPFREQGUAR40
126 * to 200MHz in ISPSSPM1.
127 * 4. Wait for FREQVALID to be cleared by P-Unit.
128 * 5. Wait for field ISPFREQSTAT40 in ISPSSPM1 turn to ratio set in 3.
130 static int write_target_freq_to_hw(struct atomisp_device *isp,
131 unsigned int new_freq)
133 unsigned int ratio, timeout, guar_ratio;
137 if (!isp->hpll_freq) {
138 dev_err(isp->dev, "failed to get hpll_freq. no change to freq\n");
142 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
143 if (isp_sspm1 & ISP_FREQ_VALID_MASK) {
144 dev_dbg(isp->dev, "clearing ISPSSPM1 valid bit.\n");
145 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
146 isp_sspm1 & ~(1 << ISP_FREQ_VALID_OFFSET));
149 ratio = (2 * isp->hpll_freq + new_freq / 2) / new_freq - 1;
150 guar_ratio = (2 * isp->hpll_freq + 200 / 2) / 200 - 1;
152 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
153 isp_sspm1 &= ~(0x1F << ISP_REQ_FREQ_OFFSET);
155 for (i = 0; i < ISP_DFS_TRY_TIMES; i++) {
156 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
158 | ratio << ISP_REQ_FREQ_OFFSET
159 | 1 << ISP_FREQ_VALID_OFFSET
160 | guar_ratio << ISP_REQ_GUAR_FREQ_OFFSET);
162 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
164 while ((isp_sspm1 & ISP_FREQ_VALID_MASK) && timeout) {
165 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
166 dev_dbg(isp->dev, "waiting for ISPSSPM1 valid bit to be 0.\n");
176 dev_err(isp->dev, "DFS failed due to HW error.\n");
180 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
182 while (((isp_sspm1 >> ISP_FREQ_STAT_OFFSET) != ratio) && timeout) {
183 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
184 dev_dbg(isp->dev, "waiting for ISPSSPM1 status bit to be 0x%x.\n",
190 dev_err(isp->dev, "DFS target freq is rejected by HW.\n");
197 int atomisp_freq_scaling(struct atomisp_device *isp,
198 enum atomisp_dfs_mode mode,
201 struct pci_dev *pdev = to_pci_dev(isp->dev);
202 /* FIXME! Only use subdev[0] status yet */
203 struct atomisp_sub_device *asd = &isp->asd[0];
204 const struct atomisp_dfs_config *dfs;
205 unsigned int new_freq;
206 struct atomisp_freq_scaling_rule curr_rules;
208 unsigned short fps = 0;
210 if ((pdev->device & ATOMISP_PCI_DEVICE_SOC_MASK) ==
211 ATOMISP_PCI_DEVICE_SOC_CHT && ATOMISP_USE_YUVPP(asd))
212 isp->dfs = &dfs_config_cht_soc;
216 if (dfs->lowest_freq == 0 || dfs->max_freq_at_vmin == 0 ||
217 dfs->highest_freq == 0 || dfs->dfs_table_size == 0 ||
219 dev_err(isp->dev, "DFS configuration is invalid.\n");
223 if (mode == ATOMISP_DFS_MODE_LOW) {
224 new_freq = dfs->lowest_freq;
228 if (mode == ATOMISP_DFS_MODE_MAX) {
229 new_freq = dfs->highest_freq;
233 fps = atomisp_get_sensor_fps(asd);
236 "Sensor didn't report FPS. Using DFS max mode.\n");
237 new_freq = dfs->highest_freq;
241 curr_rules.width = asd->fmt[asd->capture_pad].fmt.width;
242 curr_rules.height = asd->fmt[asd->capture_pad].fmt.height;
243 curr_rules.fps = fps;
244 curr_rules.run_mode = asd->run_mode->val;
246 * For continuous mode, we need to make the capture setting applied
247 * since preview mode, because there is no chance to do this when
248 * starting image capture.
250 if (asd->continuous_mode->val) {
251 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
252 curr_rules.run_mode = ATOMISP_RUN_MODE_SDV;
254 curr_rules.run_mode =
255 ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE;
258 /* search for the target frequency by looping freq rules*/
259 for (i = 0; i < dfs->dfs_table_size; i++) {
260 if (curr_rules.width != dfs->dfs_table[i].width &&
261 dfs->dfs_table[i].width != ISP_FREQ_RULE_ANY)
263 if (curr_rules.height != dfs->dfs_table[i].height &&
264 dfs->dfs_table[i].height != ISP_FREQ_RULE_ANY)
266 if (curr_rules.fps != dfs->dfs_table[i].fps &&
267 dfs->dfs_table[i].fps != ISP_FREQ_RULE_ANY)
269 if (curr_rules.run_mode != dfs->dfs_table[i].run_mode &&
270 dfs->dfs_table[i].run_mode != ISP_FREQ_RULE_ANY)
275 if (i == dfs->dfs_table_size)
276 new_freq = dfs->max_freq_at_vmin;
278 new_freq = dfs->dfs_table[i].isp_freq;
281 dev_dbg(isp->dev, "DFS target frequency=%d.\n", new_freq);
283 if ((new_freq == isp->running_freq) && !force)
286 dev_dbg(isp->dev, "Programming DFS frequency to %d\n", new_freq);
288 ret = write_target_freq_to_hw(isp, new_freq);
290 isp->running_freq = new_freq;
291 trace_ipu_pstate(new_freq, -1);
297 * reset and restore ISP
299 int atomisp_reset(struct atomisp_device *isp)
301 /* Reset ISP by power-cycling it */
304 dev_dbg(isp->dev, "%s\n", __func__);
306 ret = atomisp_power_off(isp->dev);
308 dev_err(isp->dev, "atomisp_power_off failed, %d\n", ret);
310 ret = atomisp_power_on(isp->dev);
312 dev_err(isp->dev, "atomisp_power_on failed, %d\n", ret);
313 isp->isp_fatal_error = true;
320 * interrupt disable functions
322 static void disable_isp_irq(enum hrt_isp_css_irq irq)
324 irq_disable_channel(IRQ0_ID, irq);
326 if (irq != hrt_isp_css_irq_sp)
329 cnd_sp_irq_enable(SP0_ID, false);
333 * interrupt clean function
335 static void clear_isp_irq(enum hrt_isp_css_irq irq)
337 irq_clear_all(IRQ0_ID);
340 void atomisp_msi_irq_init(struct atomisp_device *isp)
342 struct pci_dev *pdev = to_pci_dev(isp->dev);
346 pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32);
347 msg32 |= 1 << MSI_ENABLE_BIT;
348 pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32);
350 msg32 = (1 << INTR_IER) | (1 << INTR_IIR);
351 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32);
353 pci_read_config_word(pdev, PCI_COMMAND, &msg16);
354 msg16 |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
355 PCI_COMMAND_INTX_DISABLE);
356 pci_write_config_word(pdev, PCI_COMMAND, msg16);
359 void atomisp_msi_irq_uninit(struct atomisp_device *isp)
361 struct pci_dev *pdev = to_pci_dev(isp->dev);
365 pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32);
366 msg32 &= ~(1 << MSI_ENABLE_BIT);
367 pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32);
370 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32);
372 pci_read_config_word(pdev, PCI_COMMAND, &msg16);
373 msg16 &= ~(PCI_COMMAND_MASTER);
374 pci_write_config_word(pdev, PCI_COMMAND, msg16);
377 static void atomisp_sof_event(struct atomisp_sub_device *asd)
379 struct v4l2_event event = {0};
381 event.type = V4L2_EVENT_FRAME_SYNC;
382 event.u.frame_sync.frame_sequence = atomic_read(&asd->sof_count);
384 v4l2_event_queue(asd->subdev.devnode, &event);
387 void atomisp_eof_event(struct atomisp_sub_device *asd, uint8_t exp_id)
389 struct v4l2_event event = {0};
391 event.type = V4L2_EVENT_FRAME_END;
392 event.u.frame_sync.frame_sequence = exp_id;
394 v4l2_event_queue(asd->subdev.devnode, &event);
397 static void atomisp_3a_stats_ready_event(struct atomisp_sub_device *asd,
400 struct v4l2_event event = {0};
402 event.type = V4L2_EVENT_ATOMISP_3A_STATS_READY;
403 event.u.frame_sync.frame_sequence = exp_id;
405 v4l2_event_queue(asd->subdev.devnode, &event);
408 static void atomisp_metadata_ready_event(struct atomisp_sub_device *asd,
409 enum atomisp_metadata_type md_type)
411 struct v4l2_event event = {0};
413 event.type = V4L2_EVENT_ATOMISP_METADATA_READY;
414 event.u.data[0] = md_type;
416 v4l2_event_queue(asd->subdev.devnode, &event);
419 static void atomisp_reset_event(struct atomisp_sub_device *asd)
421 struct v4l2_event event = {0};
423 event.type = V4L2_EVENT_ATOMISP_CSS_RESET;
425 v4l2_event_queue(asd->subdev.devnode, &event);
428 static void print_csi_rx_errors(enum mipi_port_id port,
429 struct atomisp_device *isp)
433 atomisp_css_rx_get_irq_info(port, &infos);
435 dev_err(isp->dev, "CSI Receiver port %d errors:\n", port);
436 if (infos & IA_CSS_RX_IRQ_INFO_BUFFER_OVERRUN)
437 dev_err(isp->dev, " buffer overrun");
438 if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT)
439 dev_err(isp->dev, " start-of-transmission error");
440 if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT_SYNC)
441 dev_err(isp->dev, " start-of-transmission sync error");
442 if (infos & IA_CSS_RX_IRQ_INFO_ERR_CONTROL)
443 dev_err(isp->dev, " control error");
444 if (infos & IA_CSS_RX_IRQ_INFO_ERR_ECC_DOUBLE)
445 dev_err(isp->dev, " 2 or more ECC errors");
446 if (infos & IA_CSS_RX_IRQ_INFO_ERR_CRC)
447 dev_err(isp->dev, " CRC mismatch");
448 if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ID)
449 dev_err(isp->dev, " unknown error");
450 if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_SYNC)
451 dev_err(isp->dev, " frame sync error");
452 if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_DATA)
453 dev_err(isp->dev, " frame data error");
454 if (infos & IA_CSS_RX_IRQ_INFO_ERR_DATA_TIMEOUT)
455 dev_err(isp->dev, " data timeout");
456 if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ESC)
457 dev_err(isp->dev, " unknown escape command entry");
458 if (infos & IA_CSS_RX_IRQ_INFO_ERR_LINE_SYNC)
459 dev_err(isp->dev, " line sync error");
463 static void clear_irq_reg(struct atomisp_device *isp)
465 struct pci_dev *pdev = to_pci_dev(isp->dev);
468 pci_read_config_dword(pdev, PCI_INTERRUPT_CTRL, &msg_ret);
469 msg_ret |= 1 << INTR_IIR;
470 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg_ret);
473 static struct atomisp_sub_device *
474 __get_asd_from_port(struct atomisp_device *isp, enum mipi_port_id port)
478 /* Check which isp subdev to send eof */
479 for (i = 0; i < isp->num_of_streams; i++) {
480 struct atomisp_sub_device *asd = &isp->asd[i];
481 struct camera_mipi_info *mipi_info;
483 mipi_info = atomisp_to_sensor_mipi_info(
484 isp->inputs[asd->input_curr].camera);
486 if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED &&
487 __get_mipi_port(isp, mipi_info->port) == port) {
495 /* interrupt handling function*/
496 irqreturn_t atomisp_isr(int irq, void *dev)
498 struct atomisp_device *isp = (struct atomisp_device *)dev;
499 struct atomisp_sub_device *asd;
500 struct atomisp_css_event eof_event;
501 unsigned int irq_infos = 0;
506 spin_lock_irqsave(&isp->lock, flags);
508 if (!isp->css_initialized) {
509 spin_unlock_irqrestore(&isp->lock, flags);
512 err = atomisp_css_irq_translate(isp, &irq_infos);
514 spin_unlock_irqrestore(&isp->lock, flags);
520 if (!atomisp_streaming_count(isp))
523 for (i = 0; i < isp->num_of_streams; i++) {
526 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
529 * Current SOF only support one stream, so the SOF only valid
530 * either solely one stream is running
532 if (irq_infos & IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF) {
533 atomic_inc(&asd->sof_count);
534 atomisp_sof_event(asd);
536 /* If sequence_temp and sequence are the same
537 * there where no frames lost so we can increase
539 * If not then processing of frame is still in progress
540 * and driver needs to keep old sequence_temp value.
541 * NOTE: There is assumption here that ISP will not
542 * start processing next frame from sensor before old
543 * one is completely done. */
544 if (atomic_read(&asd->sequence) == atomic_read(
545 &asd->sequence_temp))
546 atomic_set(&asd->sequence_temp,
547 atomic_read(&asd->sof_count));
549 if (irq_infos & IA_CSS_IRQ_INFO_EVENTS_READY)
550 atomic_set(&asd->sequence,
551 atomic_read(&asd->sequence_temp));
554 if (irq_infos & IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF) {
555 dev_dbg_ratelimited(isp->dev,
558 irq_infos &= ~IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF;
561 if ((irq_infos & IA_CSS_IRQ_INFO_INPUT_SYSTEM_ERROR) ||
562 (irq_infos & IA_CSS_IRQ_INFO_IF_ERROR)) {
563 /* handle mipi receiver error */
565 enum mipi_port_id port;
567 for (port = MIPI_PORT0_ID; port <= MIPI_PORT2_ID;
569 print_csi_rx_errors(port, isp);
570 atomisp_css_rx_get_irq_info(port, &rx_infos);
571 atomisp_css_rx_clear_irq_info(port, rx_infos);
575 if (irq_infos & IA_CSS_IRQ_INFO_ISYS_EVENTS_READY) {
576 while (ia_css_dequeue_isys_event(&eof_event.event) ==
578 /* EOF Event does not have the css_pipe returned */
579 asd = __get_asd_from_port(isp, eof_event.event.port);
581 dev_err(isp->dev, "%s: ISYS event, but no subdev.event:%d",
582 __func__, eof_event.event.type);
586 atomisp_eof_event(asd, eof_event.event.exp_id);
587 dev_dbg_ratelimited(isp->dev,
588 "%s ISYS event: EOF exp_id %d, asd %d\n",
589 __func__, eof_event.event.exp_id,
593 irq_infos &= ~IA_CSS_IRQ_INFO_ISYS_EVENTS_READY;
598 spin_unlock_irqrestore(&isp->lock, flags);
600 dev_dbg_ratelimited(isp->dev, "irq:0x%x (unhandled)\n", irq_infos);
602 return IRQ_WAKE_THREAD;
605 spin_unlock_irqrestore(&isp->lock, flags);
608 dev_dbg_ratelimited(isp->dev, "irq:0x%x (ignored, as not streaming anymore)\n",
614 void atomisp_clear_css_buffer_counters(struct atomisp_sub_device *asd)
618 memset(asd->s3a_bufs_in_css, 0, sizeof(asd->s3a_bufs_in_css));
619 for (i = 0; i < ATOMISP_INPUT_STREAM_NUM; i++)
620 memset(asd->metadata_bufs_in_css[i], 0,
621 sizeof(asd->metadata_bufs_in_css[i]));
622 asd->dis_bufs_in_css = 0;
625 /* 0x100000 is the start of dmem inside SP */
626 #define SP_DMEM_BASE 0x100000
628 void dump_sp_dmem(struct atomisp_device *isp, unsigned int addr,
631 unsigned int data = 0;
632 unsigned int size32 = DIV_ROUND_UP(size, sizeof(u32));
634 dev_dbg(isp->dev, "atomisp mmio base: %p\n", isp->base);
635 dev_dbg(isp->dev, "%s, addr:0x%x, size: %d, size32: %d\n", __func__,
637 if (size32 * 4 + addr > 0x4000) {
638 dev_err(isp->dev, "illegal size (%d) or addr (0x%x)\n",
642 addr += SP_DMEM_BASE;
645 data = readl(isp->base + addr);
646 dev_dbg(isp->dev, "%s, \t [0x%x]:0x%x\n", __func__, addr, data);
651 int atomisp_buffers_in_css(struct atomisp_video_pipe *pipe)
653 unsigned long irqflags;
654 struct list_head *pos;
655 int buffers_in_css = 0;
657 spin_lock_irqsave(&pipe->irq_lock, irqflags);
659 list_for_each(pos, &pipe->buffers_in_css)
662 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
664 return buffers_in_css;
667 void atomisp_buffer_done(struct ia_css_frame *frame, enum vb2_buffer_state state)
669 struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf);
671 lockdep_assert_held(&pipe->irq_lock);
673 frame->vb.vb2_buf.timestamp = ktime_get_ns();
674 frame->vb.field = pipe->pix.field;
675 frame->vb.sequence = atomic_read(&pipe->asd->sequence);
676 list_del(&frame->queue);
677 if (state == VB2_BUF_STATE_DONE)
678 vb2_set_plane_payload(&frame->vb.vb2_buf, 0, pipe->pix.sizeimage);
679 vb2_buffer_done(&frame->vb.vb2_buf, state);
682 void atomisp_flush_video_pipe(struct atomisp_video_pipe *pipe, enum vb2_buffer_state state,
683 bool warn_on_css_frames)
685 struct ia_css_frame *frame, *_frame;
686 unsigned long irqflags;
688 spin_lock_irqsave(&pipe->irq_lock, irqflags);
690 list_for_each_entry_safe(frame, _frame, &pipe->buffers_in_css, queue) {
691 if (warn_on_css_frames)
692 dev_warn(pipe->isp->dev, "Warning: CSS frames queued on flush\n");
693 atomisp_buffer_done(frame, state);
696 list_for_each_entry_safe(frame, _frame, &pipe->activeq, queue)
697 atomisp_buffer_done(frame, state);
699 list_for_each_entry_safe(frame, _frame, &pipe->buffers_waiting_for_param, queue) {
700 pipe->frame_request_config_id[frame->vb.vb2_buf.index] = 0;
701 atomisp_buffer_done(frame, state);
704 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
707 /* Returns queued buffers back to video-core */
708 void atomisp_flush_bufs_and_wakeup(struct atomisp_sub_device *asd)
710 atomisp_flush_video_pipe(&asd->video_out_capture, VB2_BUF_STATE_ERROR, false);
711 atomisp_flush_video_pipe(&asd->video_out_vf, VB2_BUF_STATE_ERROR, false);
712 atomisp_flush_video_pipe(&asd->video_out_preview, VB2_BUF_STATE_ERROR, false);
713 atomisp_flush_video_pipe(&asd->video_out_video_capture, VB2_BUF_STATE_ERROR, false);
716 /* clean out the parameters that did not apply */
717 void atomisp_flush_params_queue(struct atomisp_video_pipe *pipe)
719 struct atomisp_css_params_with_list *param;
721 while (!list_empty(&pipe->per_frame_params)) {
722 param = list_entry(pipe->per_frame_params.next,
723 struct atomisp_css_params_with_list, list);
724 list_del(¶m->list);
725 atomisp_free_css_parameters(¶m->params);
730 /* Re-queue per-frame parameters */
731 static void atomisp_recover_params_queue(struct atomisp_video_pipe *pipe)
733 struct atomisp_css_params_with_list *param;
736 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
737 param = pipe->frame_params[i];
739 list_add_tail(¶m->list, &pipe->per_frame_params);
740 pipe->frame_params[i] = NULL;
742 atomisp_handle_parameter_and_buffer(pipe);
745 enum atomisp_metadata_type
746 atomisp_get_metadata_type(struct atomisp_sub_device *asd,
747 enum ia_css_pipe_id pipe_id)
749 if (!asd->continuous_mode->val)
750 return ATOMISP_MAIN_METADATA;
752 if (pipe_id == IA_CSS_PIPE_ID_CAPTURE) /* online capture pipe */
753 return ATOMISP_SEC_METADATA;
755 return ATOMISP_MAIN_METADATA;
758 void atomisp_buf_done(struct atomisp_sub_device *asd, int error,
759 enum ia_css_buffer_type buf_type,
760 enum ia_css_pipe_id css_pipe_id,
761 bool q_buffers, enum atomisp_input_stream_id stream_id)
763 struct atomisp_video_pipe *pipe = NULL;
764 struct atomisp_css_buffer buffer;
765 bool requeue = false;
766 unsigned long irqflags;
767 struct ia_css_frame *frame = NULL;
768 struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf_tmp, *s3a_iter;
769 struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf_tmp, *dis_iter;
770 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf_tmp, *md_iter;
771 enum atomisp_metadata_type md_type;
772 struct atomisp_device *isp = asd->isp;
773 struct v4l2_control ctrl;
776 lockdep_assert_held(&isp->mutex);
779 buf_type != IA_CSS_BUFFER_TYPE_METADATA &&
780 buf_type != IA_CSS_BUFFER_TYPE_3A_STATISTICS &&
781 buf_type != IA_CSS_BUFFER_TYPE_DIS_STATISTICS &&
782 buf_type != IA_CSS_BUFFER_TYPE_OUTPUT_FRAME &&
783 buf_type != IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
784 buf_type != IA_CSS_BUFFER_TYPE_RAW_OUTPUT_FRAME &&
785 buf_type != IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
786 buf_type != IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
787 dev_err(isp->dev, "%s, unsupported buffer type: %d\n",
792 memset(&buffer, 0, sizeof(struct atomisp_css_buffer));
793 buffer.css_buffer.type = buf_type;
794 err = atomisp_css_dequeue_buffer(asd, stream_id, css_pipe_id,
798 "atomisp_css_dequeue_buffer failed: 0x%x\n", err);
803 case IA_CSS_BUFFER_TYPE_3A_STATISTICS:
804 list_for_each_entry_safe(s3a_iter, _s3a_buf_tmp,
805 &asd->s3a_stats_in_css, list) {
806 if (s3a_iter->s3a_data ==
807 buffer.css_buffer.data.stats_3a) {
808 list_del_init(&s3a_iter->list);
809 list_add_tail(&s3a_iter->list,
810 &asd->s3a_stats_ready);
816 asd->s3a_bufs_in_css[css_pipe_id]--;
817 atomisp_3a_stats_ready_event(asd, buffer.css_buffer.exp_id);
819 dev_dbg(isp->dev, "%s: s3a stat with exp_id %d is ready\n",
820 __func__, s3a_buf->s3a_data->exp_id);
822 dev_dbg(isp->dev, "%s: s3a stat is ready with no exp_id found\n",
825 case IA_CSS_BUFFER_TYPE_METADATA:
829 md_type = atomisp_get_metadata_type(asd, css_pipe_id);
830 list_for_each_entry_safe(md_iter, _md_buf_tmp,
831 &asd->metadata_in_css[md_type], list) {
832 if (md_iter->metadata ==
833 buffer.css_buffer.data.metadata) {
834 list_del_init(&md_iter->list);
835 list_add_tail(&md_iter->list,
836 &asd->metadata_ready[md_type]);
841 asd->metadata_bufs_in_css[stream_id][css_pipe_id]--;
842 atomisp_metadata_ready_event(asd, md_type);
844 dev_dbg(isp->dev, "%s: metadata with exp_id %d is ready\n",
845 __func__, md_buf->metadata->exp_id);
847 dev_dbg(isp->dev, "%s: metadata is ready with no exp_id found\n",
850 case IA_CSS_BUFFER_TYPE_DIS_STATISTICS:
851 list_for_each_entry_safe(dis_iter, _dis_buf_tmp,
852 &asd->dis_stats_in_css, list) {
853 if (dis_iter->dis_data ==
854 buffer.css_buffer.data.stats_dvs) {
855 spin_lock_irqsave(&asd->dis_stats_lock,
857 list_del_init(&dis_iter->list);
858 list_add(&dis_iter->list, &asd->dis_stats);
859 asd->params.dis_proj_data_valid = true;
860 spin_unlock_irqrestore(&asd->dis_stats_lock,
866 asd->dis_bufs_in_css--;
868 dev_dbg(isp->dev, "%s: dis stat with exp_id %d is ready\n",
869 __func__, dis_buf->dis_data->exp_id);
871 dev_dbg(isp->dev, "%s: dis stat is ready with no exp_id found\n",
874 case IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME:
875 case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
876 frame = buffer.css_buffer.data.frame;
884 pipe = vb_to_pipe(&frame->vb.vb2_buf);
887 * YUVPP doesn't set postview exp_id correctlly in SDV mode.
888 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
890 if (IS_BYT && buf_type == IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
891 asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
892 frame->exp_id = (asd->postview_exp_id++) %
893 (ATOMISP_MAX_EXP_ID + 1);
895 dev_dbg(isp->dev, "%s: vf frame with exp_id %d is ready\n",
896 __func__, frame->exp_id);
897 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
898 if (frame->flash_state
899 == IA_CSS_FRAME_FLASH_STATE_PARTIAL)
900 dev_dbg(isp->dev, "%s thumb partially flashed\n",
902 else if (frame->flash_state
903 == IA_CSS_FRAME_FLASH_STATE_FULL)
904 dev_dbg(isp->dev, "%s thumb completely flashed\n",
907 dev_dbg(isp->dev, "%s thumb no flash in this frame\n",
910 pipe->frame_config_id[frame->vb.vb2_buf.index] = frame->isp_config_id;
911 if (css_pipe_id == IA_CSS_PIPE_ID_CAPTURE &&
912 asd->pending_capture_request > 0) {
913 err = atomisp_css_offline_capture_configure(asd,
914 asd->params.offline_parm.num_captures,
915 asd->params.offline_parm.skip_frames,
916 asd->params.offline_parm.offset);
918 asd->pending_capture_request--;
920 dev_dbg(isp->dev, "Trigger capture again for new buffer. err=%d\n",
924 case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
925 case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
926 frame = buffer.css_buffer.data.frame;
935 pipe = vb_to_pipe(&frame->vb.vb2_buf);
938 * YUVPP doesn't set preview exp_id correctlly in ZSL mode.
939 * This is a WORKAROUND to set exp_id. see HSDES-1503911606.
941 if (IS_BYT && buf_type == IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
942 asd->continuous_mode->val && ATOMISP_USE_YUVPP(asd))
943 frame->exp_id = (asd->preview_exp_id++) %
944 (ATOMISP_MAX_EXP_ID + 1);
946 dev_dbg(isp->dev, "%s: main frame with exp_id %d is ready\n",
947 __func__, frame->exp_id);
949 i = frame->vb.vb2_buf.index;
951 /* free the parameters */
952 if (pipe->frame_params[i]) {
953 if (asd->params.dvs_6axis == pipe->frame_params[i]->params.dvs_6axis)
954 asd->params.dvs_6axis = NULL;
955 atomisp_free_css_parameters(&pipe->frame_params[i]->params);
956 kvfree(pipe->frame_params[i]);
957 pipe->frame_params[i] = NULL;
960 pipe->frame_config_id[i] = frame->isp_config_id;
961 ctrl.id = V4L2_CID_FLASH_MODE;
962 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
963 if (frame->flash_state == IA_CSS_FRAME_FLASH_STATE_PARTIAL) {
964 asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_PARTIAL;
965 dev_dbg(isp->dev, "%s partially flashed\n", __func__);
966 } else if (frame->flash_state == IA_CSS_FRAME_FLASH_STATE_FULL) {
967 asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
968 asd->params.num_flash_frames--;
969 dev_dbg(isp->dev, "%s completely flashed\n", __func__);
971 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
972 dev_dbg(isp->dev, "%s no flash in this frame\n", __func__);
975 /* Check if flashing sequence is done */
976 if (asd->frame_status[i] == ATOMISP_FRAME_STATUS_FLASH_EXPOSED)
977 asd->params.flash_state = ATOMISP_FLASH_DONE;
978 } else if (isp->flash) {
979 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) == 0 &&
980 ctrl.value == ATOMISP_FLASH_MODE_TORCH) {
981 ctrl.id = V4L2_CID_FLASH_TORCH_INTENSITY;
982 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) == 0 &&
984 asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
986 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
988 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
991 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
994 asd->params.last_frame_status = asd->frame_status[i];
996 if (asd->continuous_mode->val) {
997 if (css_pipe_id == IA_CSS_PIPE_ID_PREVIEW ||
998 css_pipe_id == IA_CSS_PIPE_ID_VIDEO) {
999 asd->latest_preview_exp_id = frame->exp_id;
1000 } else if (css_pipe_id ==
1001 IA_CSS_PIPE_ID_CAPTURE) {
1002 if (asd->run_mode->val ==
1003 ATOMISP_RUN_MODE_VIDEO)
1004 dev_dbg(isp->dev, "SDV capture raw buffer id: %u\n",
1007 dev_dbg(isp->dev, "ZSL capture raw buffer id: %u\n",
1012 * Only after enabled the raw buffer lock
1013 * and in continuous mode.
1014 * in preview/video pipe, each buffer will
1015 * be locked automatically, so record it here.
1017 if (((css_pipe_id == IA_CSS_PIPE_ID_PREVIEW) ||
1018 (css_pipe_id == IA_CSS_PIPE_ID_VIDEO)) &&
1019 asd->enable_raw_buffer_lock->val &&
1020 asd->continuous_mode->val) {
1021 atomisp_set_raw_buffer_bitmap(asd, frame->exp_id);
1022 WARN_ON(frame->exp_id > ATOMISP_MAX_EXP_ID);
1025 if (asd->params.css_update_params_needed) {
1026 atomisp_apply_css_parameters(asd,
1027 &asd->params.css_param);
1028 if (asd->params.css_param.update_flag.dz_config)
1029 asd->params.config.dz_config = &asd->params.css_param.dz_config;
1030 /* New global dvs 6axis config should be blocked
1031 * here if there's a buffer with per-frame parameters
1032 * pending in CSS frame buffer queue.
1033 * This is to aviod zooming vibration since global
1034 * parameters take effect immediately while
1035 * per-frame parameters are taken after previous
1036 * buffers in CSS got processed.
1038 if (asd->params.dvs_6axis)
1039 atomisp_css_set_dvs_6axis(asd,
1040 asd->params.dvs_6axis);
1042 asd->params.css_update_params_needed = false;
1043 /* The update flag should not be cleaned here
1044 * since it is still going to be used to make up
1045 * following per-frame parameters.
1046 * This will introduce more copy work since each
1047 * time when updating global parameters, the whole
1048 * parameter set are applied.
1049 * FIXME: A new set of parameter copy functions can
1050 * be added to make up per-frame parameters based on
1051 * solid structures stored in asd->params.css_param
1052 * instead of using shadow pointers in update flag.
1054 atomisp_css_update_isp_params(asd);
1061 spin_lock_irqsave(&pipe->irq_lock, irqflags);
1062 atomisp_buffer_done(frame, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
1063 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
1067 * Requeue should only be done for 3a and dis buffers.
1068 * Queue/dequeue order will change if driver recycles image buffers.
1071 err = atomisp_css_queue_buffer(asd,
1072 stream_id, css_pipe_id,
1075 dev_err(isp->dev, "%s, q to css fails: %d\n",
1079 if (!error && q_buffers)
1080 atomisp_qbuffers_to_css(asd);
1083 void atomisp_delayed_init_work(struct work_struct *work)
1085 struct atomisp_sub_device *asd = container_of(work,
1086 struct atomisp_sub_device,
1089 * to SOC camera, use yuvpp pipe and no support continuous mode.
1091 if (!ATOMISP_USE_YUVPP(asd)) {
1092 struct v4l2_event event = {0};
1093 struct ia_css_stream *stream;
1095 stream = asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
1098 if (ia_css_alloc_continuous_frame_remain(stream))
1101 ia_css_update_continuous_frames(stream);
1103 event.type = V4L2_EVENT_ATOMISP_RAW_BUFFERS_ALLOC_DONE;
1104 v4l2_event_queue(asd->subdev.devnode, &event);
1107 /* signal streamon after delayed init is done */
1108 asd->delayed_init = ATOMISP_DELAYED_INIT_DONE;
1109 complete(&asd->init_done);
1112 static void __atomisp_css_recover(struct atomisp_device *isp, bool isp_timeout)
1114 struct pci_dev *pdev = to_pci_dev(isp->dev);
1115 enum ia_css_pipe_id css_pipe_id;
1116 bool stream_restart[MAX_STREAM_NUM] = {0};
1117 bool depth_mode = false;
1118 int i, ret, depth_cnt = 0;
1119 unsigned long flags;
1121 lockdep_assert_held(&isp->mutex);
1123 if (!atomisp_streaming_count(isp))
1126 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
1128 BUG_ON(isp->num_of_streams > MAX_STREAM_NUM);
1130 for (i = 0; i < isp->num_of_streams; i++) {
1131 struct atomisp_sub_device *asd = &isp->asd[i];
1133 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED &&
1134 !asd->stream_prepared)
1139 if (asd->delayed_init == ATOMISP_DELAYED_INIT_QUEUED)
1140 cancel_work_sync(&asd->delayed_init_work);
1142 complete(&asd->init_done);
1143 asd->delayed_init = ATOMISP_DELAYED_INIT_NOT_QUEUED;
1145 stream_restart[asd->index] = true;
1147 spin_lock_irqsave(&isp->lock, flags);
1148 asd->streaming = ATOMISP_DEVICE_STREAMING_STOPPING;
1149 spin_unlock_irqrestore(&isp->lock, flags);
1151 /* stream off sensor */
1152 ret = v4l2_subdev_call(
1153 isp->inputs[asd->input_curr].
1154 camera, video, s_stream, 0);
1157 "can't stop streaming on sensor!\n");
1159 atomisp_clear_css_buffer_counters(asd);
1161 css_pipe_id = atomisp_get_css_pipe_id(asd);
1162 atomisp_css_stop(asd, css_pipe_id, true);
1164 spin_lock_irqsave(&isp->lock, flags);
1165 asd->streaming = ATOMISP_DEVICE_STREAMING_DISABLED;
1166 spin_unlock_irqrestore(&isp->lock, flags);
1168 asd->preview_exp_id = 1;
1169 asd->postview_exp_id = 1;
1170 /* notify HAL the CSS reset */
1172 "send reset event to %s\n", asd->subdev.devnode->name);
1173 atomisp_reset_event(asd);
1177 disable_isp_irq(hrt_isp_css_irq_sp);
1178 clear_isp_irq(hrt_isp_css_irq_sp);
1180 /* Set the SRSE to 3 before resetting */
1181 pci_write_config_dword(pdev, PCI_I_CONTROL,
1182 isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
1184 /* reset ISP and restore its state */
1185 isp->isp_timeout = true;
1187 isp->isp_timeout = false;
1190 for (i = 0; i < isp->num_of_streams; i++) {
1191 if (isp->asd[i].depth_mode->val)
1196 for (i = 0; i < isp->num_of_streams; i++) {
1197 struct atomisp_sub_device *asd = &isp->asd[i];
1199 if (!stream_restart[i])
1202 if (isp->inputs[asd->input_curr].type != FILE_INPUT)
1203 atomisp_css_input_set_mode(asd,
1204 IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
1206 css_pipe_id = atomisp_get_css_pipe_id(asd);
1207 if (atomisp_css_start(asd, css_pipe_id, true)) {
1209 "start SP failed, so do not set streaming to be enable!\n");
1211 spin_lock_irqsave(&isp->lock, flags);
1212 asd->streaming = ATOMISP_DEVICE_STREAMING_ENABLED;
1213 spin_unlock_irqrestore(&isp->lock, flags);
1216 atomisp_csi2_configure(asd);
1219 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
1220 atomisp_css_valid_sof(isp));
1222 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, true) < 0)
1223 dev_dbg(isp->dev, "DFS auto failed while recovering!\n");
1225 for (i = 0; i < isp->num_of_streams; i++) {
1226 struct atomisp_sub_device *asd;
1230 if (!stream_restart[i])
1233 if (asd->continuous_mode->val &&
1234 asd->delayed_init == ATOMISP_DELAYED_INIT_NOT_QUEUED) {
1235 reinit_completion(&asd->init_done);
1236 asd->delayed_init = ATOMISP_DELAYED_INIT_QUEUED;
1237 queue_work(asd->delayed_init_workq,
1238 &asd->delayed_init_work);
1241 * dequeueing buffers is not needed. CSS will recycle
1242 * buffers that it has.
1244 atomisp_flush_bufs_and_wakeup(asd);
1246 /* Requeue unprocessed per-frame parameters. */
1247 atomisp_recover_params_queue(&asd->video_out_capture);
1248 atomisp_recover_params_queue(&asd->video_out_preview);
1249 atomisp_recover_params_queue(&asd->video_out_video_capture);
1251 if ((asd->depth_mode->val) &&
1252 (depth_cnt == ATOMISP_DEPTH_SENSOR_STREAMON_COUNT)) {
1257 ret = v4l2_subdev_call(
1258 isp->inputs[asd->input_curr].camera, video,
1262 "can't start streaming on sensor!\n");
1266 if (atomisp_stream_on_master_slave_sensor(isp, true))
1268 "master slave sensor stream on failed!\n");
1272 void atomisp_assert_recovery_work(struct work_struct *work)
1274 struct atomisp_device *isp = container_of(work, struct atomisp_device,
1275 assert_recovery_work);
1277 mutex_lock(&isp->mutex);
1278 __atomisp_css_recover(isp, true);
1279 mutex_unlock(&isp->mutex);
1282 void atomisp_css_flush(struct atomisp_device *isp)
1285 __atomisp_css_recover(isp, false);
1287 dev_dbg(isp->dev, "atomisp css flush done\n");
1290 void atomisp_setup_flash(struct atomisp_sub_device *asd)
1292 struct atomisp_device *isp = asd->isp;
1293 struct v4l2_control ctrl;
1298 if (asd->params.flash_state != ATOMISP_FLASH_REQUESTED &&
1299 asd->params.flash_state != ATOMISP_FLASH_DONE)
1302 if (asd->params.num_flash_frames) {
1303 /* make sure the timeout is set before setting flash mode */
1304 ctrl.id = V4L2_CID_FLASH_TIMEOUT;
1305 ctrl.value = FLASH_TIMEOUT;
1307 if (v4l2_s_ctrl(NULL, isp->flash->ctrl_handler, &ctrl)) {
1308 dev_err(isp->dev, "flash timeout configure failed\n");
1312 ia_css_stream_request_flash(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream);
1314 asd->params.flash_state = ATOMISP_FLASH_ONGOING;
1316 asd->params.flash_state = ATOMISP_FLASH_IDLE;
1320 irqreturn_t atomisp_isr_thread(int irq, void *isp_ptr)
1322 struct atomisp_device *isp = isp_ptr;
1323 unsigned long flags;
1324 bool frame_done_found[MAX_STREAM_NUM] = {0};
1325 bool css_pipe_done[MAX_STREAM_NUM] = {0};
1327 struct atomisp_sub_device *asd;
1329 dev_dbg(isp->dev, ">%s\n", __func__);
1331 spin_lock_irqsave(&isp->lock, flags);
1333 if (!atomisp_streaming_count(isp)) {
1334 spin_unlock_irqrestore(&isp->lock, flags);
1338 spin_unlock_irqrestore(&isp->lock, flags);
1341 * The standard CSS2.0 API tells the following calling sequence of
1342 * dequeue ready buffers:
1343 * while (ia_css_dequeue_psys_event(...)) {
1344 * switch (event.type) {
1346 * ia_css_pipe_dequeue_buffer()
1349 * That is, dequeue event and buffer are one after another.
1351 * But the following implementation is to first deuque all the event
1352 * to a FIFO, then process the event in the FIFO.
1353 * This will not have issue in single stream mode, but it do have some
1354 * issue in multiple stream case. The issue is that
1355 * ia_css_pipe_dequeue_buffer() will not return the corrent buffer in
1358 * This is due to ia_css_pipe_dequeue_buffer() does not take the
1359 * ia_css_pipe parameter.
1362 * For CSS2.0: we change the way to not dequeue all the event at one
1363 * time, instead, dequue one and process one, then another
1365 mutex_lock(&isp->mutex);
1366 if (atomisp_css_isr_thread(isp, frame_done_found, css_pipe_done))
1369 for (i = 0; i < isp->num_of_streams; i++) {
1371 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
1373 atomisp_setup_flash(asd);
1376 mutex_unlock(&isp->mutex);
1377 dev_dbg(isp->dev, "<%s\n", __func__);
1383 * Get internal fmt according to V4L2 fmt
1385 static enum ia_css_frame_format
1386 v4l2_fmt_to_sh_fmt(u32 fmt)
1389 case V4L2_PIX_FMT_YUV420:
1390 return IA_CSS_FRAME_FORMAT_YUV420;
1391 case V4L2_PIX_FMT_YVU420:
1392 return IA_CSS_FRAME_FORMAT_YV12;
1393 case V4L2_PIX_FMT_YUV422P:
1394 return IA_CSS_FRAME_FORMAT_YUV422;
1395 case V4L2_PIX_FMT_YUV444:
1396 return IA_CSS_FRAME_FORMAT_YUV444;
1397 case V4L2_PIX_FMT_NV12:
1398 return IA_CSS_FRAME_FORMAT_NV12;
1399 case V4L2_PIX_FMT_NV21:
1400 return IA_CSS_FRAME_FORMAT_NV21;
1401 case V4L2_PIX_FMT_NV16:
1402 return IA_CSS_FRAME_FORMAT_NV16;
1403 case V4L2_PIX_FMT_NV61:
1404 return IA_CSS_FRAME_FORMAT_NV61;
1405 case V4L2_PIX_FMT_UYVY:
1406 return IA_CSS_FRAME_FORMAT_UYVY;
1407 case V4L2_PIX_FMT_YUYV:
1408 return IA_CSS_FRAME_FORMAT_YUYV;
1409 case V4L2_PIX_FMT_RGB24:
1410 return IA_CSS_FRAME_FORMAT_PLANAR_RGB888;
1411 case V4L2_PIX_FMT_RGB32:
1412 return IA_CSS_FRAME_FORMAT_RGBA888;
1413 case V4L2_PIX_FMT_RGB565:
1414 return IA_CSS_FRAME_FORMAT_RGB565;
1416 case V4L2_PIX_FMT_JPEG:
1417 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1418 return IA_CSS_FRAME_FORMAT_BINARY_8;
1420 case V4L2_PIX_FMT_SBGGR16:
1421 case V4L2_PIX_FMT_SBGGR10:
1422 case V4L2_PIX_FMT_SGBRG10:
1423 case V4L2_PIX_FMT_SGRBG10:
1424 case V4L2_PIX_FMT_SRGGB10:
1425 case V4L2_PIX_FMT_SBGGR12:
1426 case V4L2_PIX_FMT_SGBRG12:
1427 case V4L2_PIX_FMT_SGRBG12:
1428 case V4L2_PIX_FMT_SRGGB12:
1429 case V4L2_PIX_FMT_SBGGR8:
1430 case V4L2_PIX_FMT_SGBRG8:
1431 case V4L2_PIX_FMT_SGRBG8:
1432 case V4L2_PIX_FMT_SRGGB8:
1433 return IA_CSS_FRAME_FORMAT_RAW;
1440 * raw format match between SH format and V4L2 format
1442 static int raw_output_format_match_input(u32 input, u32 output)
1444 if ((input == ATOMISP_INPUT_FORMAT_RAW_12) &&
1445 ((output == V4L2_PIX_FMT_SRGGB12) ||
1446 (output == V4L2_PIX_FMT_SGRBG12) ||
1447 (output == V4L2_PIX_FMT_SBGGR12) ||
1448 (output == V4L2_PIX_FMT_SGBRG12)))
1451 if ((input == ATOMISP_INPUT_FORMAT_RAW_10) &&
1452 ((output == V4L2_PIX_FMT_SRGGB10) ||
1453 (output == V4L2_PIX_FMT_SGRBG10) ||
1454 (output == V4L2_PIX_FMT_SBGGR10) ||
1455 (output == V4L2_PIX_FMT_SGBRG10)))
1458 if ((input == ATOMISP_INPUT_FORMAT_RAW_8) &&
1459 ((output == V4L2_PIX_FMT_SRGGB8) ||
1460 (output == V4L2_PIX_FMT_SGRBG8) ||
1461 (output == V4L2_PIX_FMT_SBGGR8) ||
1462 (output == V4L2_PIX_FMT_SGBRG8)))
1465 if ((input == ATOMISP_INPUT_FORMAT_RAW_16) && (output == V4L2_PIX_FMT_SBGGR16))
1471 u32 atomisp_get_pixel_depth(u32 pixelformat)
1473 switch (pixelformat) {
1474 case V4L2_PIX_FMT_YUV420:
1475 case V4L2_PIX_FMT_NV12:
1476 case V4L2_PIX_FMT_NV21:
1477 case V4L2_PIX_FMT_YVU420:
1479 case V4L2_PIX_FMT_YUV422P:
1480 case V4L2_PIX_FMT_YUYV:
1481 case V4L2_PIX_FMT_UYVY:
1482 case V4L2_PIX_FMT_NV16:
1483 case V4L2_PIX_FMT_NV61:
1484 case V4L2_PIX_FMT_RGB565:
1485 case V4L2_PIX_FMT_SBGGR16:
1486 case V4L2_PIX_FMT_SBGGR12:
1487 case V4L2_PIX_FMT_SGBRG12:
1488 case V4L2_PIX_FMT_SGRBG12:
1489 case V4L2_PIX_FMT_SRGGB12:
1490 case V4L2_PIX_FMT_SBGGR10:
1491 case V4L2_PIX_FMT_SGBRG10:
1492 case V4L2_PIX_FMT_SGRBG10:
1493 case V4L2_PIX_FMT_SRGGB10:
1495 case V4L2_PIX_FMT_RGB24:
1496 case V4L2_PIX_FMT_YUV444:
1498 case V4L2_PIX_FMT_RGB32:
1500 case V4L2_PIX_FMT_JPEG:
1501 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1502 case V4L2_PIX_FMT_SBGGR8:
1503 case V4L2_PIX_FMT_SGBRG8:
1504 case V4L2_PIX_FMT_SGRBG8:
1505 case V4L2_PIX_FMT_SRGGB8:
1508 return 8 * 2; /* raw type now */
1512 bool atomisp_is_mbuscode_raw(uint32_t code)
1514 return code >= 0x3000 && code < 0x4000;
1518 * ISP features control function
1522 * Set ISP capture mode based on current settings
1524 static void atomisp_update_capture_mode(struct atomisp_sub_device *asd)
1526 if (asd->params.gdc_cac_en)
1527 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_ADVANCED);
1528 else if (asd->params.low_light)
1529 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_LOW_LIGHT);
1530 else if (asd->video_out_capture.sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
1531 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
1533 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
1537 int atomisp_set_sensor_runmode(struct atomisp_sub_device *asd,
1538 struct atomisp_s_runmode *runmode)
1540 struct atomisp_device *isp = asd->isp;
1541 struct v4l2_ctrl *c;
1544 if (!(runmode && (runmode->mode & RUNMODE_MASK)))
1547 mutex_lock(asd->ctrl_handler.lock);
1548 c = v4l2_ctrl_find(isp->inputs[asd->input_curr].camera->ctrl_handler,
1552 ret = v4l2_ctrl_s_ctrl(c, runmode->mode);
1554 mutex_unlock(asd->ctrl_handler.lock);
1559 * Function to enable/disable lens geometry distortion correction (GDC) and
1560 * chromatic aberration correction (CAC)
1562 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag,
1566 *value = asd->params.gdc_cac_en;
1570 asd->params.gdc_cac_en = !!*value;
1571 if (asd->params.gdc_cac_en) {
1572 asd->params.config.morph_table = asd->params.css_param.morph_table;
1574 asd->params.config.morph_table = NULL;
1576 asd->params.css_update_params_needed = true;
1577 atomisp_update_capture_mode(asd);
1582 * Function to enable/disable low light mode including ANR
1584 int atomisp_low_light(struct atomisp_sub_device *asd, int flag,
1588 *value = asd->params.low_light;
1592 asd->params.low_light = (*value != 0);
1593 atomisp_update_capture_mode(asd);
1598 * Function to enable/disable extra noise reduction (XNR) in low light
1601 int atomisp_xnr(struct atomisp_sub_device *asd, int flag,
1605 *xnr_enable = asd->params.xnr_en;
1609 atomisp_css_capture_enable_xnr(asd, !!*xnr_enable);
1615 * Function to configure bayer noise reduction
1617 int atomisp_nr(struct atomisp_sub_device *asd, int flag,
1618 struct atomisp_nr_config *arg)
1621 /* Get nr config from current setup */
1622 if (atomisp_css_get_nr_config(asd, arg))
1625 /* Set nr config to isp parameters */
1626 memcpy(&asd->params.css_param.nr_config, arg,
1627 sizeof(struct ia_css_nr_config));
1628 asd->params.config.nr_config = &asd->params.css_param.nr_config;
1629 asd->params.css_update_params_needed = true;
1635 * Function to configure temporal noise reduction (TNR)
1637 int atomisp_tnr(struct atomisp_sub_device *asd, int flag,
1638 struct atomisp_tnr_config *config)
1640 /* Get tnr config from current setup */
1642 /* Get tnr config from current setup */
1643 if (atomisp_css_get_tnr_config(asd, config))
1646 /* Set tnr config to isp parameters */
1647 memcpy(&asd->params.css_param.tnr_config, config,
1648 sizeof(struct ia_css_tnr_config));
1649 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
1650 asd->params.css_update_params_needed = true;
1657 * Function to configure black level compensation
1659 int atomisp_black_level(struct atomisp_sub_device *asd, int flag,
1660 struct atomisp_ob_config *config)
1663 /* Get ob config from current setup */
1664 if (atomisp_css_get_ob_config(asd, config))
1667 /* Set ob config to isp parameters */
1668 memcpy(&asd->params.css_param.ob_config, config,
1669 sizeof(struct ia_css_ob_config));
1670 asd->params.config.ob_config = &asd->params.css_param.ob_config;
1671 asd->params.css_update_params_needed = true;
1678 * Function to configure edge enhancement
1680 int atomisp_ee(struct atomisp_sub_device *asd, int flag,
1681 struct atomisp_ee_config *config)
1684 /* Get ee config from current setup */
1685 if (atomisp_css_get_ee_config(asd, config))
1688 /* Set ee config to isp parameters */
1689 memcpy(&asd->params.css_param.ee_config, config,
1690 sizeof(asd->params.css_param.ee_config));
1691 asd->params.config.ee_config = &asd->params.css_param.ee_config;
1692 asd->params.css_update_params_needed = true;
1699 * Function to update Gamma table for gamma, brightness and contrast config
1701 int atomisp_gamma(struct atomisp_sub_device *asd, int flag,
1702 struct atomisp_gamma_table *config)
1705 /* Get gamma table from current setup */
1706 if (atomisp_css_get_gamma_table(asd, config))
1709 /* Set gamma table to isp parameters */
1710 memcpy(&asd->params.css_param.gamma_table, config,
1711 sizeof(asd->params.css_param.gamma_table));
1712 asd->params.config.gamma_table = &asd->params.css_param.gamma_table;
1719 * Function to update Ctc table for Chroma Enhancement
1721 int atomisp_ctc(struct atomisp_sub_device *asd, int flag,
1722 struct atomisp_ctc_table *config)
1725 /* Get ctc table from current setup */
1726 if (atomisp_css_get_ctc_table(asd, config))
1729 /* Set ctc table to isp parameters */
1730 memcpy(&asd->params.css_param.ctc_table, config,
1731 sizeof(asd->params.css_param.ctc_table));
1732 atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table);
1739 * Function to update gamma correction parameters
1741 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag,
1742 struct atomisp_gc_config *config)
1745 /* Get gamma correction params from current setup */
1746 if (atomisp_css_get_gc_config(asd, config))
1749 /* Set gamma correction params to isp parameters */
1750 memcpy(&asd->params.css_param.gc_config, config,
1751 sizeof(asd->params.css_param.gc_config));
1752 asd->params.config.gc_config = &asd->params.css_param.gc_config;
1753 asd->params.css_update_params_needed = true;
1760 * Function to update narrow gamma flag
1762 int atomisp_formats(struct atomisp_sub_device *asd, int flag,
1763 struct atomisp_formats_config *config)
1766 /* Get narrow gamma flag from current setup */
1767 if (atomisp_css_get_formats_config(asd, config))
1770 /* Set narrow gamma flag to isp parameters */
1771 memcpy(&asd->params.css_param.formats_config, config,
1772 sizeof(asd->params.css_param.formats_config));
1773 asd->params.config.formats_config = &asd->params.css_param.formats_config;
1779 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd)
1781 atomisp_free_css_parameters(&asd->params.css_param);
1783 if (asd->raw_output_frame) {
1784 ia_css_frame_free(asd->raw_output_frame);
1785 asd->raw_output_frame = NULL;
1789 static void atomisp_update_grid_info(struct atomisp_sub_device *asd,
1790 enum ia_css_pipe_id pipe_id,
1793 struct atomisp_device *isp = asd->isp;
1796 if (atomisp_css_get_grid_info(asd, pipe_id, source_pad))
1799 /* We must free all buffers because they no longer match
1801 atomisp_css_free_stat_buffers(asd);
1803 err = atomisp_alloc_css_stat_bufs(asd, ATOMISP_INPUT_STREAM_GENERAL);
1805 dev_err(isp->dev, "stat_buf allocate error\n");
1809 if (atomisp_alloc_3a_output_buf(asd)) {
1810 /* Failure for 3A buffers does not influence DIS buffers */
1811 if (asd->params.s3a_output_bytes != 0) {
1812 /* For SOC sensor happens s3a_output_bytes == 0,
1813 * using if condition to exclude false error log */
1814 dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n");
1819 if (atomisp_alloc_dis_coef_buf(asd)) {
1821 "Failed to allocate memory for DIS statistics\n");
1825 if (atomisp_alloc_metadata_output_buf(asd)) {
1826 dev_err(isp->dev, "Failed to allocate memory for metadata\n");
1833 atomisp_css_free_stat_buffers(asd);
1837 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd,
1838 struct atomisp_grid_info *info)
1840 memcpy(info, &asd->params.curr_grid_info.s3a_grid,
1841 sizeof(struct ia_css_3a_grid_info));
1844 int atomisp_compare_grid(struct atomisp_sub_device *asd,
1845 struct atomisp_grid_info *atomgrid)
1847 struct atomisp_grid_info tmp = {0};
1849 atomisp_curr_user_grid_info(asd, &tmp);
1850 return memcmp(atomgrid, &tmp, sizeof(tmp));
1854 * Function to update Gdc table for gdc
1856 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag,
1857 struct atomisp_morph_table *config)
1861 struct atomisp_device *isp = asd->isp;
1864 /* Get gdc table from current setup */
1865 struct ia_css_morph_table tab = {0};
1867 atomisp_css_get_morph_table(asd, &tab);
1869 config->width = tab.width;
1870 config->height = tab.height;
1872 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1873 ret = copy_to_user(config->coordinates_x[i],
1874 tab.coordinates_x[i], tab.height *
1875 tab.width * sizeof(*tab.coordinates_x[i]));
1878 "Failed to copy to User for x\n");
1881 ret = copy_to_user(config->coordinates_y[i],
1882 tab.coordinates_y[i], tab.height *
1883 tab.width * sizeof(*tab.coordinates_y[i]));
1886 "Failed to copy to User for y\n");
1891 struct ia_css_morph_table *tab =
1892 asd->params.css_param.morph_table;
1894 /* free first if we have one */
1896 atomisp_css_morph_table_free(tab);
1897 asd->params.css_param.morph_table = NULL;
1900 /* allocate new one */
1901 tab = atomisp_css_morph_table_allocate(config->width,
1905 dev_err(isp->dev, "out of memory\n");
1909 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1910 ret = copy_from_user(tab->coordinates_x[i],
1911 config->coordinates_x[i],
1912 config->height * config->width *
1913 sizeof(*config->coordinates_x[i]));
1916 "Failed to copy from User for x, ret %d\n",
1918 atomisp_css_morph_table_free(tab);
1921 ret = copy_from_user(tab->coordinates_y[i],
1922 config->coordinates_y[i],
1923 config->height * config->width *
1924 sizeof(*config->coordinates_y[i]));
1927 "Failed to copy from User for y, ret is %d\n",
1929 atomisp_css_morph_table_free(tab);
1933 asd->params.css_param.morph_table = tab;
1934 if (asd->params.gdc_cac_en)
1935 asd->params.config.morph_table = tab;
1941 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag,
1942 struct atomisp_macc_config *config)
1944 struct ia_css_macc_table *macc_table;
1946 switch (config->color_effect) {
1947 case V4L2_COLORFX_NONE:
1948 macc_table = &asd->params.css_param.macc_table;
1950 case V4L2_COLORFX_SKY_BLUE:
1951 macc_table = &blue_macc_table;
1953 case V4L2_COLORFX_GRASS_GREEN:
1954 macc_table = &green_macc_table;
1956 case V4L2_COLORFX_SKIN_WHITEN_LOW:
1957 macc_table = &skin_low_macc_table;
1959 case V4L2_COLORFX_SKIN_WHITEN:
1960 macc_table = &skin_medium_macc_table;
1962 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
1963 macc_table = &skin_high_macc_table;
1970 /* Get macc table from current setup */
1971 memcpy(&config->table, macc_table,
1972 sizeof(struct ia_css_macc_table));
1974 memcpy(macc_table, &config->table,
1975 sizeof(struct ia_css_macc_table));
1976 if (config->color_effect == asd->params.color_effect)
1977 asd->params.config.macc_table = macc_table;
1983 int atomisp_set_dis_vector(struct atomisp_sub_device *asd,
1984 struct atomisp_dis_vector *vector)
1986 atomisp_css_video_set_dis_vector(asd, vector);
1988 asd->params.dis_proj_data_valid = false;
1989 asd->params.css_update_params_needed = true;
1994 * Function to set/get image stablization statistics
1996 int atomisp_get_dis_stat(struct atomisp_sub_device *asd,
1997 struct atomisp_dis_statistics *stats)
1999 return atomisp_css_get_dis_stat(asd, stats);
2003 * Function set camrea_prefiles.xml current sensor pixel array size
2005 int atomisp_set_array_res(struct atomisp_sub_device *asd,
2006 struct atomisp_resolution *config)
2008 dev_dbg(asd->isp->dev, ">%s start\n", __func__);
2010 dev_err(asd->isp->dev, "Set sensor array size is not valid\n");
2014 asd->sensor_array_res.width = config->width;
2015 asd->sensor_array_res.height = config->height;
2020 * Function to get DVS2 BQ resolution settings
2022 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd,
2023 struct atomisp_dvs2_bq_resolutions *bq_res)
2025 struct ia_css_pipe_config *pipe_cfg = NULL;
2026 struct ia_css_stream_config *stream_cfg = NULL;
2027 struct ia_css_stream_input_config *input_config = NULL;
2029 struct ia_css_stream *stream =
2030 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
2032 dev_warn(asd->isp->dev, "stream is not created");
2036 pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2037 .pipe_configs[IA_CSS_PIPE_ID_VIDEO];
2038 stream_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
2040 input_config = &stream_cfg->input_config;
2045 /* the GDC output resolution */
2046 bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2;
2047 bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2;
2049 bq_res->envelope_bq.width_bq = 0;
2050 bq_res->envelope_bq.height_bq = 0;
2051 /* the GDC input resolution */
2052 if (!asd->continuous_mode->val) {
2053 bq_res->source_bq.width_bq = bq_res->output_bq.width_bq +
2054 pipe_cfg->dvs_envelope.width / 2;
2055 bq_res->source_bq.height_bq = bq_res->output_bq.height_bq +
2056 pipe_cfg->dvs_envelope.height / 2;
2058 * Bad pixels caused by spatial filter processing
2059 * ISP filter resolution should be given by CSS/FW, but for now
2060 * there is not such API to query, and it is fixed value, so
2063 bq_res->ispfilter_bq.width_bq = 12 / 2;
2064 bq_res->ispfilter_bq.height_bq = 12 / 2;
2065 /* spatial filter shift, always 4 pixels */
2066 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2067 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2069 if (asd->params.video_dis_en) {
2070 bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width
2071 / 2 - bq_res->ispfilter_bq.width_bq;
2072 bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height
2073 / 2 - bq_res->ispfilter_bq.height_bq;
2076 unsigned int w_padding;
2077 unsigned int gdc_effective_input = 0;
2080 * gdc_effective_input = effective_input + envelope
2082 * From the comment and formula in BZ1786,
2083 * we see the source_bq should be:
2084 * effective_input / bayer_ds_ratio
2086 bq_res->source_bq.width_bq =
2087 (input_config->effective_res.width *
2088 pipe_cfg->bayer_ds_out_res.width /
2089 input_config->effective_res.width + 1) / 2;
2090 bq_res->source_bq.height_bq =
2091 (input_config->effective_res.height *
2092 pipe_cfg->bayer_ds_out_res.height /
2093 input_config->effective_res.height + 1) / 2;
2095 if (!asd->params.video_dis_en) {
2097 * We adjust the ispfilter_bq to:
2098 * ispfilter_bq = 128/BDS
2099 * we still need firmware team to provide an offical
2102 bq_res->ispfilter_bq.width_bq = 128 *
2103 pipe_cfg->bayer_ds_out_res.width /
2104 input_config->effective_res.width / 2;
2105 bq_res->ispfilter_bq.height_bq = 128 *
2106 pipe_cfg->bayer_ds_out_res.width /
2107 input_config->effective_res.width / 2;
2109 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2110 /* No additional left padding for ISYS2401 */
2111 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2112 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2115 * For the w_padding and gdc_shift_bq cacluation
2116 * Please see the BZ 1786 and 4358 for more info.
2117 * Just test that this formula can work now,
2118 * but we still have no offical formula.
2120 * w_padding = ceiling(gdc_effective_input
2121 * /128, 1) * 128 - effective_width
2122 * gdc_shift_bq = w_padding/BDS/2 + ispfilter_bq/2
2124 gdc_effective_input =
2125 input_config->effective_res.width +
2126 pipe_cfg->dvs_envelope.width;
2127 w_padding = roundup(gdc_effective_input, 128) -
2128 input_config->effective_res.width;
2129 w_padding = w_padding *
2130 pipe_cfg->bayer_ds_out_res.width /
2131 input_config->effective_res.width + 1;
2132 w_padding = roundup(w_padding / 2, 1);
2134 bq_res->gdc_shift_bq.width_bq = bq_res->ispfilter_bq.width_bq / 2
2136 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2139 unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
2141 bq_res->ispfilter_bq.width_bq = 8 / 2;
2142 bq_res->ispfilter_bq.height_bq = 8 / 2;
2144 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401)) {
2145 /* No additional left padding for ISYS2401 */
2146 bq_res->gdc_shift_bq.width_bq = 4 / 2;
2147 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2150 roundup(input_config->effective_res.width, 128) -
2151 input_config->effective_res.width;
2154 bq_res->gdc_shift_bq.width_bq = 4 / 2 +
2156 pipe_cfg->bayer_ds_out_res.width /
2157 input_config->effective_res.width + 1) / 2;
2158 bq_res->gdc_shift_bq.height_bq = 4 / 2;
2161 dvs_w = pipe_cfg->bayer_ds_out_res.width -
2162 pipe_cfg->output_info[0].res.width;
2163 dvs_h = pipe_cfg->bayer_ds_out_res.height -
2164 pipe_cfg->output_info[0].res.height;
2165 dvs_w_max = rounddown(
2166 pipe_cfg->output_info[0].res.width / 5,
2167 ATOM_ISP_STEP_WIDTH);
2168 dvs_h_max = rounddown(
2169 pipe_cfg->output_info[0].res.height / 5,
2170 ATOM_ISP_STEP_HEIGHT);
2171 bq_res->envelope_bq.width_bq =
2172 min((dvs_w / 2), (dvs_w_max / 2)) -
2173 bq_res->ispfilter_bq.width_bq;
2174 bq_res->envelope_bq.height_bq =
2175 min((dvs_h / 2), (dvs_h_max / 2)) -
2176 bq_res->ispfilter_bq.height_bq;
2180 dev_dbg(asd->isp->dev,
2181 "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",
2182 bq_res->source_bq.width_bq, bq_res->source_bq.height_bq,
2183 bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq,
2184 bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq,
2185 bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq,
2186 bq_res->output_bq.width_bq, bq_res->output_bq.height_bq);
2191 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd,
2192 struct atomisp_dis_coefficients *coefs)
2194 return atomisp_css_set_dis_coefs(asd, coefs);
2198 * Function to set/get 3A stat from isp
2200 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag,
2201 struct atomisp_3a_statistics *config)
2203 struct atomisp_device *isp = asd->isp;
2204 struct atomisp_s3a_buf *s3a_buf;
2210 /* sanity check to avoid writing into unallocated memory. */
2211 if (asd->params.s3a_output_bytes == 0)
2214 if (atomisp_compare_grid(asd, &config->grid_info) != 0) {
2215 /* If the grid info in the argument differs from the current
2216 grid info, we tell the caller to reset the grid size and
2221 if (list_empty(&asd->s3a_stats_ready)) {
2222 dev_err(isp->dev, "3a statistics is not valid.\n");
2226 s3a_buf = list_entry(asd->s3a_stats_ready.next,
2227 struct atomisp_s3a_buf, list);
2228 if (s3a_buf->s3a_map)
2229 ia_css_translate_3a_statistics(
2230 asd->params.s3a_user_stat, s3a_buf->s3a_map);
2232 ia_css_get_3a_statistics(asd->params.s3a_user_stat,
2235 config->exp_id = s3a_buf->s3a_data->exp_id;
2236 config->isp_config_id = s3a_buf->s3a_data->isp_config_id;
2238 ret = copy_to_user(config->data, asd->params.s3a_user_stat->data,
2239 asd->params.s3a_output_bytes);
2241 dev_err(isp->dev, "copy to user failed: copied %lu bytes\n",
2246 /* Move to free buffer list */
2247 list_del_init(&s3a_buf->list);
2248 list_add_tail(&s3a_buf->list, &asd->s3a_stats);
2249 dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n",
2251 config->exp_id, config->isp_config_id);
2255 int atomisp_get_metadata(struct atomisp_sub_device *asd, int flag,
2256 struct atomisp_metadata *md)
2258 struct atomisp_device *isp = asd->isp;
2259 struct ia_css_stream_info *stream_info;
2260 struct camera_mipi_info *mipi_info;
2261 struct atomisp_metadata_buf *md_buf;
2262 enum atomisp_metadata_type md_type = ATOMISP_MAIN_METADATA;
2268 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2271 /* We always return the resolution and stride even if there is
2272 * no valid metadata. This allows the caller to get the information
2273 * needed to allocate user-space buffers. */
2274 md->width = stream_info->metadata_info.resolution.width;
2275 md->height = stream_info->metadata_info.resolution.height;
2276 md->stride = stream_info->metadata_info.stride;
2278 /* sanity check to avoid writing into unallocated memory.
2279 * This does not return an error because it is a valid way
2280 * for applications to detect that metadata is not enabled. */
2281 if (md->width == 0 || md->height == 0 || !md->data)
2284 /* This is done in the atomisp_buf_done() */
2285 if (list_empty(&asd->metadata_ready[md_type])) {
2286 dev_warn(isp->dev, "Metadata queue is empty now!\n");
2290 mipi_info = atomisp_to_sensor_mipi_info(
2291 isp->inputs[asd->input_curr].camera);
2295 if (mipi_info->metadata_effective_width) {
2296 for (i = 0; i < md->height; i++)
2297 md->effective_width[i] =
2298 mipi_info->metadata_effective_width[i];
2301 md_buf = list_entry(asd->metadata_ready[md_type].next,
2302 struct atomisp_metadata_buf, list);
2303 md->exp_id = md_buf->metadata->exp_id;
2304 if (md_buf->md_vptr) {
2305 ret = copy_to_user(md->data,
2307 stream_info->metadata_info.size);
2309 hmm_load(md_buf->metadata->address,
2310 asd->params.metadata_user[md_type],
2311 stream_info->metadata_info.size);
2313 ret = copy_to_user(md->data,
2314 asd->params.metadata_user[md_type],
2315 stream_info->metadata_info.size);
2318 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2323 list_del_init(&md_buf->list);
2324 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2326 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2327 __func__, md_type, md->exp_id);
2331 int atomisp_get_metadata_by_type(struct atomisp_sub_device *asd, int flag,
2332 struct atomisp_metadata_with_type *md)
2334 struct atomisp_device *isp = asd->isp;
2335 struct ia_css_stream_info *stream_info;
2336 struct camera_mipi_info *mipi_info;
2337 struct atomisp_metadata_buf *md_buf;
2338 enum atomisp_metadata_type md_type;
2344 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
2347 /* We always return the resolution and stride even if there is
2348 * no valid metadata. This allows the caller to get the information
2349 * needed to allocate user-space buffers. */
2350 md->width = stream_info->metadata_info.resolution.width;
2351 md->height = stream_info->metadata_info.resolution.height;
2352 md->stride = stream_info->metadata_info.stride;
2354 /* sanity check to avoid writing into unallocated memory.
2355 * This does not return an error because it is a valid way
2356 * for applications to detect that metadata is not enabled. */
2357 if (md->width == 0 || md->height == 0 || !md->data)
2361 if (md_type < 0 || md_type >= ATOMISP_METADATA_TYPE_NUM)
2364 /* This is done in the atomisp_buf_done() */
2365 if (list_empty(&asd->metadata_ready[md_type])) {
2366 dev_warn(isp->dev, "Metadata queue is empty now!\n");
2370 mipi_info = atomisp_to_sensor_mipi_info(
2371 isp->inputs[asd->input_curr].camera);
2375 if (mipi_info->metadata_effective_width) {
2376 for (i = 0; i < md->height; i++)
2377 md->effective_width[i] =
2378 mipi_info->metadata_effective_width[i];
2381 md_buf = list_entry(asd->metadata_ready[md_type].next,
2382 struct atomisp_metadata_buf, list);
2383 md->exp_id = md_buf->metadata->exp_id;
2384 if (md_buf->md_vptr) {
2385 ret = copy_to_user(md->data,
2387 stream_info->metadata_info.size);
2389 hmm_load(md_buf->metadata->address,
2390 asd->params.metadata_user[md_type],
2391 stream_info->metadata_info.size);
2393 ret = copy_to_user(md->data,
2394 asd->params.metadata_user[md_type],
2395 stream_info->metadata_info.size);
2398 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2402 list_del_init(&md_buf->list);
2403 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2405 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2406 __func__, md_type, md->exp_id);
2411 * Function to calculate real zoom region for every pipe
2413 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd,
2414 struct ia_css_dz_config *dz_config,
2415 enum ia_css_pipe_id css_pipe_id)
2418 struct atomisp_stream_env *stream_env =
2419 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL];
2420 struct atomisp_resolution eff_res, out_res;
2421 int w_offset, h_offset;
2423 memset(&eff_res, 0, sizeof(eff_res));
2424 memset(&out_res, 0, sizeof(out_res));
2426 if (dz_config->dx || dz_config->dy)
2429 if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW
2430 && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) {
2431 dev_err(asd->isp->dev, "%s the set pipe no support crop region"
2437 stream_env->stream_config.input_config.effective_res.width;
2439 stream_env->stream_config.input_config.effective_res.height;
2440 if (eff_res.width == 0 || eff_res.height == 0) {
2441 dev_err(asd->isp->dev, "%s err effective resolution"
2446 if (dz_config->zoom_region.resolution.width
2447 == asd->sensor_array_res.width
2448 || dz_config->zoom_region.resolution.height
2449 == asd->sensor_array_res.height) {
2450 /*no need crop region*/
2451 dz_config->zoom_region.origin.x = 0;
2452 dz_config->zoom_region.origin.y = 0;
2453 dz_config->zoom_region.resolution.width = eff_res.width;
2454 dz_config->zoom_region.resolution.height = eff_res.height;
2459 * This is not the correct implementation with Google's definition, due
2460 * to firmware limitation.
2461 * map real crop region base on above calculating base max crop region.
2465 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
2467 / asd->sensor_array_res.width;
2468 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
2470 / asd->sensor_array_res.height;
2471 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
2473 / asd->sensor_array_res.width;
2474 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
2476 / asd->sensor_array_res.height;
2478 * Set same ratio of crop region resolution and current pipe output
2481 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
2482 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
2483 if (out_res.width == 0 || out_res.height == 0) {
2484 dev_err(asd->isp->dev, "%s err current pipe output resolution"
2489 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
2490 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
2491 if (out_res.width == 0 || out_res.height == 0) {
2492 dev_err(asd->isp->dev, "%s err current pipe output resolution"
2497 if (asd->sensor_array_res.width * out_res.height
2498 < out_res.width * asd->sensor_array_res.height) {
2499 h_offset = asd->sensor_array_res.height
2500 - asd->sensor_array_res.width
2501 * out_res.height / out_res.width;
2502 h_offset = h_offset / 2;
2503 if (dz_config->zoom_region.origin.y < h_offset)
2504 dz_config->zoom_region.origin.y = 0;
2506 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset;
2509 w_offset = asd->sensor_array_res.width
2510 - asd->sensor_array_res.height
2511 * out_res.width / out_res.height;
2512 w_offset = w_offset / 2;
2513 if (dz_config->zoom_region.origin.x < w_offset)
2514 dz_config->zoom_region.origin.x = 0;
2516 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset;
2519 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
2521 / (asd->sensor_array_res.width - 2 * w_offset);
2522 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
2524 / (asd->sensor_array_res.height - 2 * h_offset);
2525 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
2527 / (asd->sensor_array_res.width - 2 * w_offset);
2528 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
2530 / (asd->sensor_array_res.height - 2 * h_offset);
2533 if (out_res.width * dz_config->zoom_region.resolution.height
2534 > dz_config->zoom_region.resolution.width * out_res.height) {
2535 dz_config->zoom_region.resolution.height =
2536 dz_config->zoom_region.resolution.width
2537 * out_res.height / out_res.width;
2539 dz_config->zoom_region.resolution.width =
2540 dz_config->zoom_region.resolution.height
2541 * out_res.width / out_res.height;
2543 dev_dbg(asd->isp->dev,
2544 "%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n",
2545 __func__, dz_config->zoom_region.origin.x,
2546 dz_config->zoom_region.origin.y,
2547 dz_config->zoom_region.resolution.width,
2548 dz_config->zoom_region.resolution.height,
2549 eff_res.width, eff_res.height,
2550 asd->sensor_array_res.width,
2551 asd->sensor_array_res.height,
2552 out_res.width, out_res.height);
2554 if ((dz_config->zoom_region.origin.x +
2555 dz_config->zoom_region.resolution.width
2557 (dz_config->zoom_region.origin.y +
2558 dz_config->zoom_region.resolution.height
2566 * Function to check the zoom region whether is effective
2568 static bool atomisp_check_zoom_region(
2569 struct atomisp_sub_device *asd,
2570 struct ia_css_dz_config *dz_config)
2572 struct atomisp_resolution config;
2576 memset(&config, 0, sizeof(struct atomisp_resolution));
2578 if (dz_config->dx && dz_config->dy)
2581 config.width = asd->sensor_array_res.width;
2582 config.height = asd->sensor_array_res.height;
2583 w = dz_config->zoom_region.origin.x +
2584 dz_config->zoom_region.resolution.width;
2585 h = dz_config->zoom_region.origin.y +
2586 dz_config->zoom_region.resolution.height;
2588 if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0)
2591 /* setting error zoom region */
2592 dev_err(asd->isp->dev,
2593 "%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n",
2594 __func__, dz_config->zoom_region.origin.x,
2595 dz_config->zoom_region.origin.y,
2596 dz_config->zoom_region.resolution.width,
2597 dz_config->zoom_region.resolution.height,
2598 config.width, config.height);
2603 void atomisp_apply_css_parameters(
2604 struct atomisp_sub_device *asd,
2605 struct atomisp_css_params *css_param)
2607 if (css_param->update_flag.wb_config)
2608 asd->params.config.wb_config = &css_param->wb_config;
2610 if (css_param->update_flag.ob_config)
2611 asd->params.config.ob_config = &css_param->ob_config;
2613 if (css_param->update_flag.dp_config)
2614 asd->params.config.dp_config = &css_param->dp_config;
2616 if (css_param->update_flag.nr_config)
2617 asd->params.config.nr_config = &css_param->nr_config;
2619 if (css_param->update_flag.ee_config)
2620 asd->params.config.ee_config = &css_param->ee_config;
2622 if (css_param->update_flag.tnr_config)
2623 asd->params.config.tnr_config = &css_param->tnr_config;
2625 if (css_param->update_flag.a3a_config)
2626 asd->params.config.s3a_config = &css_param->s3a_config;
2628 if (css_param->update_flag.ctc_config)
2629 asd->params.config.ctc_config = &css_param->ctc_config;
2631 if (css_param->update_flag.cnr_config)
2632 asd->params.config.cnr_config = &css_param->cnr_config;
2634 if (css_param->update_flag.ecd_config)
2635 asd->params.config.ecd_config = &css_param->ecd_config;
2637 if (css_param->update_flag.ynr_config)
2638 asd->params.config.ynr_config = &css_param->ynr_config;
2640 if (css_param->update_flag.fc_config)
2641 asd->params.config.fc_config = &css_param->fc_config;
2643 if (css_param->update_flag.macc_config)
2644 asd->params.config.macc_config = &css_param->macc_config;
2646 if (css_param->update_flag.aa_config)
2647 asd->params.config.aa_config = &css_param->aa_config;
2649 if (css_param->update_flag.anr_config)
2650 asd->params.config.anr_config = &css_param->anr_config;
2652 if (css_param->update_flag.xnr_config)
2653 asd->params.config.xnr_config = &css_param->xnr_config;
2655 if (css_param->update_flag.yuv2rgb_cc_config)
2656 asd->params.config.yuv2rgb_cc_config = &css_param->yuv2rgb_cc_config;
2658 if (css_param->update_flag.rgb2yuv_cc_config)
2659 asd->params.config.rgb2yuv_cc_config = &css_param->rgb2yuv_cc_config;
2661 if (css_param->update_flag.macc_table)
2662 asd->params.config.macc_table = &css_param->macc_table;
2664 if (css_param->update_flag.xnr_table)
2665 asd->params.config.xnr_table = &css_param->xnr_table;
2667 if (css_param->update_flag.r_gamma_table)
2668 asd->params.config.r_gamma_table = &css_param->r_gamma_table;
2670 if (css_param->update_flag.g_gamma_table)
2671 asd->params.config.g_gamma_table = &css_param->g_gamma_table;
2673 if (css_param->update_flag.b_gamma_table)
2674 asd->params.config.b_gamma_table = &css_param->b_gamma_table;
2676 if (css_param->update_flag.anr_thres)
2677 atomisp_css_set_anr_thres(asd, &css_param->anr_thres);
2679 if (css_param->update_flag.shading_table)
2680 asd->params.config.shading_table = css_param->shading_table;
2682 if (css_param->update_flag.morph_table && asd->params.gdc_cac_en)
2683 asd->params.config.morph_table = css_param->morph_table;
2685 if (css_param->update_flag.dvs2_coefs) {
2686 struct ia_css_dvs_grid_info *dvs_grid_info =
2687 atomisp_css_get_dvs_grid_info(
2688 &asd->params.curr_grid_info);
2690 if (dvs_grid_info && dvs_grid_info->enable)
2691 atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff);
2694 if (css_param->update_flag.dvs_6axis_config)
2695 atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis);
2697 atomisp_css_set_isp_config_id(asd, css_param->isp_config_id);
2699 * These configurations are on used by ISP1.x, not for ISP2.x,
2700 * so do not handle them. see comments of ia_css_isp_config.
2711 static unsigned int long copy_from_compatible(void *to, const void *from,
2712 unsigned long n, bool from_user)
2715 return copy_from_user(to, (void __user *)from, n);
2717 memcpy(to, from, n);
2721 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd,
2722 struct atomisp_parameters *arg,
2723 struct atomisp_css_params *css_param,
2726 struct atomisp_parameters *cur_config = &css_param->update_flag;
2728 if (!arg || !asd || !css_param)
2731 if (arg->wb_config && (from_user || !cur_config->wb_config)) {
2732 if (copy_from_compatible(&css_param->wb_config, arg->wb_config,
2733 sizeof(struct ia_css_wb_config),
2736 css_param->update_flag.wb_config =
2737 (struct atomisp_wb_config *)&css_param->wb_config;
2740 if (arg->ob_config && (from_user || !cur_config->ob_config)) {
2741 if (copy_from_compatible(&css_param->ob_config, arg->ob_config,
2742 sizeof(struct ia_css_ob_config),
2745 css_param->update_flag.ob_config =
2746 (struct atomisp_ob_config *)&css_param->ob_config;
2749 if (arg->dp_config && (from_user || !cur_config->dp_config)) {
2750 if (copy_from_compatible(&css_param->dp_config, arg->dp_config,
2751 sizeof(struct ia_css_dp_config),
2754 css_param->update_flag.dp_config =
2755 (struct atomisp_dp_config *)&css_param->dp_config;
2758 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
2759 if (arg->dz_config && (from_user || !cur_config->dz_config)) {
2760 if (copy_from_compatible(&css_param->dz_config,
2762 sizeof(struct ia_css_dz_config),
2765 if (!atomisp_check_zoom_region(asd,
2766 &css_param->dz_config)) {
2767 dev_err(asd->isp->dev, "crop region error!");
2770 css_param->update_flag.dz_config =
2771 (struct atomisp_dz_config *)
2772 &css_param->dz_config;
2776 if (arg->nr_config && (from_user || !cur_config->nr_config)) {
2777 if (copy_from_compatible(&css_param->nr_config, arg->nr_config,
2778 sizeof(struct ia_css_nr_config),
2781 css_param->update_flag.nr_config =
2782 (struct atomisp_nr_config *)&css_param->nr_config;
2785 if (arg->ee_config && (from_user || !cur_config->ee_config)) {
2786 if (copy_from_compatible(&css_param->ee_config, arg->ee_config,
2787 sizeof(struct ia_css_ee_config),
2790 css_param->update_flag.ee_config =
2791 (struct atomisp_ee_config *)&css_param->ee_config;
2794 if (arg->tnr_config && (from_user || !cur_config->tnr_config)) {
2795 if (copy_from_compatible(&css_param->tnr_config,
2797 sizeof(struct ia_css_tnr_config),
2800 css_param->update_flag.tnr_config =
2801 (struct atomisp_tnr_config *)
2802 &css_param->tnr_config;
2805 if (arg->a3a_config && (from_user || !cur_config->a3a_config)) {
2806 if (copy_from_compatible(&css_param->s3a_config,
2808 sizeof(struct ia_css_3a_config),
2811 css_param->update_flag.a3a_config =
2812 (struct atomisp_3a_config *)&css_param->s3a_config;
2815 if (arg->ctc_config && (from_user || !cur_config->ctc_config)) {
2816 if (copy_from_compatible(&css_param->ctc_config,
2818 sizeof(struct ia_css_ctc_config),
2821 css_param->update_flag.ctc_config =
2822 (struct atomisp_ctc_config *)
2823 &css_param->ctc_config;
2826 if (arg->cnr_config && (from_user || !cur_config->cnr_config)) {
2827 if (copy_from_compatible(&css_param->cnr_config,
2829 sizeof(struct ia_css_cnr_config),
2832 css_param->update_flag.cnr_config =
2833 (struct atomisp_cnr_config *)
2834 &css_param->cnr_config;
2837 if (arg->ecd_config && (from_user || !cur_config->ecd_config)) {
2838 if (copy_from_compatible(&css_param->ecd_config,
2840 sizeof(struct ia_css_ecd_config),
2843 css_param->update_flag.ecd_config =
2844 (struct atomisp_ecd_config *)
2845 &css_param->ecd_config;
2848 if (arg->ynr_config && (from_user || !cur_config->ynr_config)) {
2849 if (copy_from_compatible(&css_param->ynr_config,
2851 sizeof(struct ia_css_ynr_config),
2854 css_param->update_flag.ynr_config =
2855 (struct atomisp_ynr_config *)
2856 &css_param->ynr_config;
2859 if (arg->fc_config && (from_user || !cur_config->fc_config)) {
2860 if (copy_from_compatible(&css_param->fc_config,
2862 sizeof(struct ia_css_fc_config),
2865 css_param->update_flag.fc_config =
2866 (struct atomisp_fc_config *)&css_param->fc_config;
2869 if (arg->macc_config && (from_user || !cur_config->macc_config)) {
2870 if (copy_from_compatible(&css_param->macc_config,
2872 sizeof(struct ia_css_macc_config),
2875 css_param->update_flag.macc_config =
2876 (struct atomisp_macc_config *)
2877 &css_param->macc_config;
2880 if (arg->aa_config && (from_user || !cur_config->aa_config)) {
2881 if (copy_from_compatible(&css_param->aa_config, arg->aa_config,
2882 sizeof(struct ia_css_aa_config),
2885 css_param->update_flag.aa_config =
2886 (struct atomisp_aa_config *)&css_param->aa_config;
2889 if (arg->anr_config && (from_user || !cur_config->anr_config)) {
2890 if (copy_from_compatible(&css_param->anr_config,
2892 sizeof(struct ia_css_anr_config),
2895 css_param->update_flag.anr_config =
2896 (struct atomisp_anr_config *)
2897 &css_param->anr_config;
2900 if (arg->xnr_config && (from_user || !cur_config->xnr_config)) {
2901 if (copy_from_compatible(&css_param->xnr_config,
2903 sizeof(struct ia_css_xnr_config),
2906 css_param->update_flag.xnr_config =
2907 (struct atomisp_xnr_config *)
2908 &css_param->xnr_config;
2911 if (arg->yuv2rgb_cc_config &&
2912 (from_user || !cur_config->yuv2rgb_cc_config)) {
2913 if (copy_from_compatible(&css_param->yuv2rgb_cc_config,
2914 arg->yuv2rgb_cc_config,
2915 sizeof(struct ia_css_cc_config),
2918 css_param->update_flag.yuv2rgb_cc_config =
2919 (struct atomisp_cc_config *)
2920 &css_param->yuv2rgb_cc_config;
2923 if (arg->rgb2yuv_cc_config &&
2924 (from_user || !cur_config->rgb2yuv_cc_config)) {
2925 if (copy_from_compatible(&css_param->rgb2yuv_cc_config,
2926 arg->rgb2yuv_cc_config,
2927 sizeof(struct ia_css_cc_config),
2930 css_param->update_flag.rgb2yuv_cc_config =
2931 (struct atomisp_cc_config *)
2932 &css_param->rgb2yuv_cc_config;
2935 if (arg->macc_table && (from_user || !cur_config->macc_table)) {
2936 if (copy_from_compatible(&css_param->macc_table,
2938 sizeof(struct ia_css_macc_table),
2941 css_param->update_flag.macc_table =
2942 (struct atomisp_macc_table *)
2943 &css_param->macc_table;
2946 if (arg->xnr_table && (from_user || !cur_config->xnr_table)) {
2947 if (copy_from_compatible(&css_param->xnr_table,
2949 sizeof(struct ia_css_xnr_table),
2952 css_param->update_flag.xnr_table =
2953 (struct atomisp_xnr_table *)&css_param->xnr_table;
2956 if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) {
2957 if (copy_from_compatible(&css_param->r_gamma_table,
2959 sizeof(struct ia_css_rgb_gamma_table),
2962 css_param->update_flag.r_gamma_table =
2963 (struct atomisp_rgb_gamma_table *)
2964 &css_param->r_gamma_table;
2967 if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) {
2968 if (copy_from_compatible(&css_param->g_gamma_table,
2970 sizeof(struct ia_css_rgb_gamma_table),
2973 css_param->update_flag.g_gamma_table =
2974 (struct atomisp_rgb_gamma_table *)
2975 &css_param->g_gamma_table;
2978 if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) {
2979 if (copy_from_compatible(&css_param->b_gamma_table,
2981 sizeof(struct ia_css_rgb_gamma_table),
2984 css_param->update_flag.b_gamma_table =
2985 (struct atomisp_rgb_gamma_table *)
2986 &css_param->b_gamma_table;
2989 if (arg->anr_thres && (from_user || !cur_config->anr_thres)) {
2990 if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres,
2991 sizeof(struct ia_css_anr_thres),
2994 css_param->update_flag.anr_thres =
2995 (struct atomisp_anr_thres *)&css_param->anr_thres;
2999 css_param->isp_config_id = arg->isp_config_id;
3001 * These configurations are on used by ISP1.x, not for ISP2.x,
3002 * so do not handle them. see comments of ia_css_isp_config.
3014 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd,
3015 struct atomisp_shading_table *source_st,
3016 struct atomisp_css_params *css_param,
3020 unsigned int len_table;
3021 struct ia_css_shading_table *shading_table;
3022 struct ia_css_shading_table *old_table;
3023 struct atomisp_shading_table *st, dest_st;
3031 if (!from_user && css_param->update_flag.shading_table)
3035 if (copy_from_compatible(&dest_st, source_st,
3036 sizeof(struct atomisp_shading_table),
3038 dev_err(asd->isp->dev, "copy shading table failed!");
3046 old_table = css_param->shading_table;
3048 /* user config is to disable the shading table. */
3050 /* Generate a minimum table with enable = 0. */
3051 shading_table = atomisp_css_shading_table_alloc(1, 1);
3054 shading_table->enable = 0;
3058 /* Setting a new table. Validate first - all tables must be set */
3059 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3061 dev_err(asd->isp->dev, "shading table validate failed");
3066 /* Shading table size per color */
3067 if (st->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
3068 st->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
3069 dev_err(asd->isp->dev, "shading table w/h validate failed!");
3073 shading_table = atomisp_css_shading_table_alloc(st->width, st->height);
3077 len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE;
3078 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3079 if (copy_from_compatible(shading_table->data[i],
3080 st->data[i], len_table, from_user)) {
3081 atomisp_css_shading_table_free(shading_table);
3085 shading_table->sensor_width = st->sensor_width;
3086 shading_table->sensor_height = st->sensor_height;
3087 shading_table->fraction_bits = st->fraction_bits;
3088 shading_table->enable = st->enable;
3090 /* No need to update shading table if it is the same */
3092 old_table->sensor_width == shading_table->sensor_width &&
3093 old_table->sensor_height == shading_table->sensor_height &&
3094 old_table->width == shading_table->width &&
3095 old_table->height == shading_table->height &&
3096 old_table->fraction_bits == shading_table->fraction_bits &&
3097 old_table->enable == shading_table->enable) {
3098 bool data_is_same = true;
3100 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
3101 if (memcmp(shading_table->data[i], old_table->data[i],
3103 data_is_same = false;
3109 atomisp_css_shading_table_free(shading_table);
3115 /* set LSC to CSS */
3116 css_param->shading_table = shading_table;
3117 css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table;
3118 asd->params.sc_en = shading_table;
3121 atomisp_css_shading_table_free(old_table);
3126 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd,
3127 struct ia_css_dvs2_coefficients *coefs,
3128 struct atomisp_css_params *css_param,
3131 struct ia_css_dvs_grid_info *cur =
3132 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3133 int dvs_hor_coef_bytes, dvs_ver_coef_bytes;
3134 struct ia_css_dvs2_coefficients dvs2_coefs;
3139 if (!from_user && css_param->update_flag.dvs2_coefs)
3143 if (sizeof(*cur) != sizeof(coefs->grid) ||
3144 memcmp(&coefs->grid, cur, sizeof(coefs->grid))) {
3145 dev_err(asd->isp->dev, "dvs grid mismatch!\n");
3146 /* If the grid info in the argument differs from the current
3147 grid info, we tell the caller to reset the grid size and
3152 if (!coefs->hor_coefs.odd_real ||
3153 !coefs->hor_coefs.odd_imag ||
3154 !coefs->hor_coefs.even_real ||
3155 !coefs->hor_coefs.even_imag ||
3156 !coefs->ver_coefs.odd_real ||
3157 !coefs->ver_coefs.odd_imag ||
3158 !coefs->ver_coefs.even_real ||
3159 !coefs->ver_coefs.even_imag)
3162 if (!css_param->dvs2_coeff) {
3163 /* DIS coefficients. */
3164 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3165 if (!css_param->dvs2_coeff)
3169 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3170 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3171 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3172 coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3173 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3174 coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3175 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3176 coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3177 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3178 coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3179 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3180 coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3181 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3182 coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3183 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3184 coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3185 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3186 coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3187 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3188 css_param->dvs2_coeff = NULL;
3192 if (copy_from_compatible(&dvs2_coefs, coefs,
3193 sizeof(struct ia_css_dvs2_coefficients),
3195 dev_err(asd->isp->dev, "copy dvs2 coef failed");
3199 if (sizeof(*cur) != sizeof(dvs2_coefs.grid) ||
3200 memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) {
3201 dev_err(asd->isp->dev, "dvs grid mismatch!\n");
3202 /* If the grid info in the argument differs from the current
3203 grid info, we tell the caller to reset the grid size and
3208 if (!dvs2_coefs.hor_coefs.odd_real ||
3209 !dvs2_coefs.hor_coefs.odd_imag ||
3210 !dvs2_coefs.hor_coefs.even_real ||
3211 !dvs2_coefs.hor_coefs.even_imag ||
3212 !dvs2_coefs.ver_coefs.odd_real ||
3213 !dvs2_coefs.ver_coefs.odd_imag ||
3214 !dvs2_coefs.ver_coefs.even_real ||
3215 !dvs2_coefs.ver_coefs.even_imag)
3218 if (!css_param->dvs2_coeff) {
3219 /* DIS coefficients. */
3220 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
3221 if (!css_param->dvs2_coeff)
3225 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
3226 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
3227 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
3228 dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
3229 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
3230 dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
3231 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
3232 dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
3233 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
3234 dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
3235 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
3236 dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
3237 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
3238 dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
3239 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
3240 dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
3241 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
3242 dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
3243 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3244 css_param->dvs2_coeff = NULL;
3249 css_param->update_flag.dvs2_coefs =
3250 (struct atomisp_dis_coefficients *)css_param->dvs2_coeff;
3254 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd,
3255 struct atomisp_dvs_6axis_config *source_6axis_config,
3256 struct atomisp_css_params *css_param,
3259 struct ia_css_dvs_6axis_config *dvs_6axis_config;
3260 struct ia_css_dvs_6axis_config *old_6axis_config;
3261 struct ia_css_stream *stream =
3262 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
3263 struct ia_css_dvs_grid_info *dvs_grid_info =
3264 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
3268 dev_err(asd->isp->dev, "%s: internal error!", __func__);
3272 if (!source_6axis_config || !dvs_grid_info)
3275 if (!dvs_grid_info->enable)
3278 if (!from_user && css_param->update_flag.dvs_6axis_config)
3281 /* check whether need to reallocate for 6 axis config */
3282 old_6axis_config = css_param->dvs_6axis;
3283 dvs_6axis_config = old_6axis_config;
3286 struct ia_css_dvs_6axis_config t_6axis_config;
3288 if (copy_from_compatible(&t_6axis_config, source_6axis_config,
3289 sizeof(struct atomisp_dvs_6axis_config),
3291 dev_err(asd->isp->dev, "copy morph table failed!");
3295 if (old_6axis_config &&
3296 (old_6axis_config->width_y != t_6axis_config.width_y ||
3297 old_6axis_config->height_y != t_6axis_config.height_y ||
3298 old_6axis_config->width_uv != t_6axis_config.width_uv ||
3299 old_6axis_config->height_uv != t_6axis_config.height_uv)) {
3300 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3301 css_param->dvs_6axis = NULL;
3303 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3304 if (!dvs_6axis_config)
3306 } else if (!dvs_6axis_config) {
3307 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3308 if (!dvs_6axis_config)
3312 dvs_6axis_config->exp_id = t_6axis_config.exp_id;
3314 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3315 t_6axis_config.xcoords_y,
3316 t_6axis_config.width_y *
3317 t_6axis_config.height_y *
3318 sizeof(*dvs_6axis_config->xcoords_y),
3321 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3322 t_6axis_config.ycoords_y,
3323 t_6axis_config.width_y *
3324 t_6axis_config.height_y *
3325 sizeof(*dvs_6axis_config->ycoords_y),
3328 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3329 t_6axis_config.xcoords_uv,
3330 t_6axis_config.width_uv *
3331 t_6axis_config.height_uv *
3332 sizeof(*dvs_6axis_config->xcoords_uv),
3335 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3336 t_6axis_config.ycoords_uv,
3337 t_6axis_config.width_uv *
3338 t_6axis_config.height_uv *
3339 sizeof(*dvs_6axis_config->ycoords_uv),
3343 if (old_6axis_config &&
3344 (old_6axis_config->width_y != source_6axis_config->width_y ||
3345 old_6axis_config->height_y != source_6axis_config->height_y ||
3346 old_6axis_config->width_uv != source_6axis_config->width_uv ||
3347 old_6axis_config->height_uv != source_6axis_config->height_uv)) {
3348 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3349 css_param->dvs_6axis = NULL;
3351 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3352 if (!dvs_6axis_config)
3354 } else if (!dvs_6axis_config) {
3355 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
3356 if (!dvs_6axis_config)
3360 dvs_6axis_config->exp_id = source_6axis_config->exp_id;
3362 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
3363 source_6axis_config->xcoords_y,
3364 source_6axis_config->width_y *
3365 source_6axis_config->height_y *
3366 sizeof(*source_6axis_config->xcoords_y),
3369 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
3370 source_6axis_config->ycoords_y,
3371 source_6axis_config->width_y *
3372 source_6axis_config->height_y *
3373 sizeof(*source_6axis_config->ycoords_y),
3376 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
3377 source_6axis_config->xcoords_uv,
3378 source_6axis_config->width_uv *
3379 source_6axis_config->height_uv *
3380 sizeof(*source_6axis_config->xcoords_uv),
3383 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
3384 source_6axis_config->ycoords_uv,
3385 source_6axis_config->width_uv *
3386 source_6axis_config->height_uv *
3387 sizeof(*source_6axis_config->ycoords_uv),
3391 css_param->dvs_6axis = dvs_6axis_config;
3392 css_param->update_flag.dvs_6axis_config =
3393 (struct atomisp_dvs_6axis_config *)dvs_6axis_config;
3397 if (dvs_6axis_config)
3398 ia_css_dvs2_6axis_config_free(dvs_6axis_config);
3402 int atomisp_cp_morph_table(struct atomisp_sub_device *asd,
3403 struct atomisp_morph_table *source_morph_table,
3404 struct atomisp_css_params *css_param,
3409 struct ia_css_morph_table *morph_table;
3410 struct ia_css_morph_table *old_morph_table;
3412 if (!source_morph_table)
3415 if (!from_user && css_param->update_flag.morph_table)
3418 old_morph_table = css_param->morph_table;
3421 struct ia_css_morph_table mtbl;
3423 if (copy_from_compatible(&mtbl, source_morph_table,
3424 sizeof(struct atomisp_morph_table),
3426 dev_err(asd->isp->dev, "copy morph table failed!");
3430 morph_table = atomisp_css_morph_table_allocate(
3436 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3437 if (copy_from_compatible(morph_table->coordinates_x[i],
3438 (__force void *)source_morph_table->coordinates_x[i],
3439 mtbl.height * mtbl.width *
3440 sizeof(*morph_table->coordinates_x[i]),
3444 if (copy_from_compatible(morph_table->coordinates_y[i],
3445 (__force void *)source_morph_table->coordinates_y[i],
3446 mtbl.height * mtbl.width *
3447 sizeof(*morph_table->coordinates_y[i]),
3452 morph_table = atomisp_css_morph_table_allocate(
3453 source_morph_table->width,
3454 source_morph_table->height);
3458 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3459 if (copy_from_compatible(morph_table->coordinates_x[i],
3460 (__force void *)source_morph_table->coordinates_x[i],
3461 source_morph_table->height * source_morph_table->width *
3462 sizeof(*source_morph_table->coordinates_x[i]),
3466 if (copy_from_compatible(morph_table->coordinates_y[i],
3467 (__force void *)source_morph_table->coordinates_y[i],
3468 source_morph_table->height * source_morph_table->width *
3469 sizeof(*source_morph_table->coordinates_y[i]),
3475 css_param->morph_table = morph_table;
3476 if (old_morph_table)
3477 atomisp_css_morph_table_free(old_morph_table);
3478 css_param->update_flag.morph_table =
3479 (struct atomisp_morph_table *)morph_table;
3484 atomisp_css_morph_table_free(morph_table);
3488 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd,
3489 struct atomisp_parameters *arg,
3490 struct atomisp_css_params *css_param)
3494 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false);
3497 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false);
3500 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false);
3503 ret = atomisp_css_cp_dvs2_coefs(asd,
3504 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
3508 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
3513 void atomisp_free_css_parameters(struct atomisp_css_params *css_param)
3515 if (css_param->dvs_6axis) {
3516 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3517 css_param->dvs_6axis = NULL;
3519 if (css_param->dvs2_coeff) {
3520 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3521 css_param->dvs2_coeff = NULL;
3523 if (css_param->shading_table) {
3524 ia_css_shading_table_free(css_param->shading_table);
3525 css_param->shading_table = NULL;
3527 if (css_param->morph_table) {
3528 ia_css_morph_table_free(css_param->morph_table);
3529 css_param->morph_table = NULL;
3533 static void atomisp_move_frame_to_activeq(struct ia_css_frame *frame,
3534 struct atomisp_css_params_with_list *param)
3536 struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf);
3537 unsigned long irqflags;
3539 pipe->frame_params[frame->vb.vb2_buf.index] = param;
3540 spin_lock_irqsave(&pipe->irq_lock, irqflags);
3541 list_move_tail(&frame->queue, &pipe->activeq);
3542 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
3546 * Check parameter queue list and buffer queue list to find out if matched items
3547 * and then set parameter to CSS and enqueue buffer to CSS.
3548 * Of course, if the buffer in buffer waiting list is not bound to a per-frame
3549 * parameter, it will be enqueued into CSS as long as the per-frame setting
3550 * buffers before it get enqueued.
3552 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe)
3554 struct atomisp_sub_device *asd = pipe->asd;
3555 struct ia_css_frame *frame = NULL, *frame_tmp;
3556 struct atomisp_css_params_with_list *param = NULL, *param_tmp;
3557 bool need_to_enqueue_buffer = false;
3561 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
3562 __func__, pipe->vdev.name);
3566 lockdep_assert_held(&asd->isp->mutex);
3568 if (atomisp_is_vf_pipe(pipe))
3572 * CSS/FW requires set parameter and enqueue buffer happen after ISP
3575 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
3578 if (list_empty(&pipe->per_frame_params) ||
3579 list_empty(&pipe->buffers_waiting_for_param))
3582 list_for_each_entry_safe(frame, frame_tmp,
3583 &pipe->buffers_waiting_for_param, queue) {
3584 i = frame->vb.vb2_buf.index;
3585 if (pipe->frame_request_config_id[i]) {
3586 list_for_each_entry_safe(param, param_tmp,
3587 &pipe->per_frame_params, list) {
3588 if (pipe->frame_request_config_id[i] != param->params.isp_config_id)
3591 list_del(¶m->list);
3594 * clear the request config id as the buffer
3595 * will be handled and enqueued into CSS soon
3597 pipe->frame_request_config_id[i] = 0;
3598 atomisp_move_frame_to_activeq(frame, param);
3599 need_to_enqueue_buffer = true;
3603 /* If this is the end, stop further loop */
3604 if (list_entry_is_head(param, &pipe->per_frame_params, list))
3607 atomisp_move_frame_to_activeq(frame, NULL);
3608 need_to_enqueue_buffer = true;
3612 if (!need_to_enqueue_buffer)
3615 atomisp_qbuffers_to_css(asd);
3619 * Function to configure ISP parameters
3621 int atomisp_set_parameters(struct video_device *vdev,
3622 struct atomisp_parameters *arg)
3624 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
3625 struct atomisp_sub_device *asd = pipe->asd;
3626 struct atomisp_css_params_with_list *param = NULL;
3627 struct atomisp_css_params *css_param = &asd->params.css_param;
3631 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
3632 __func__, vdev->name);
3636 lockdep_assert_held(&asd->isp->mutex);
3638 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) {
3639 dev_err(asd->isp->dev, "%s: internal error!\n", __func__);
3643 dev_dbg(asd->isp->dev,
3644 "%s: set parameter(per_frame_setting %d) for asd%d with isp_config_id %d of %s\n",
3645 __func__, arg->per_frame_setting, asd->index,
3646 arg->isp_config_id, vdev->name);
3649 if (atomisp_is_vf_pipe(pipe) && arg->per_frame_setting) {
3650 dev_err(asd->isp->dev, "%s: vf pipe not support per_frame_setting",
3656 if (arg->per_frame_setting && !atomisp_is_vf_pipe(pipe)) {
3658 * Per-frame setting enabled, we allocate a new parameter
3659 * buffer to cache the parameters and only when frame buffers
3660 * are ready, the parameters will be set to CSS.
3661 * per-frame setting only works for the main output frame.
3663 param = kvzalloc(sizeof(*param), GFP_KERNEL);
3665 dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n",
3669 css_param = ¶m->params;
3672 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true);
3674 goto apply_parameter_failed;
3676 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true);
3678 goto apply_parameter_failed;
3680 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true);
3682 goto apply_parameter_failed;
3684 ret = atomisp_css_cp_dvs2_coefs(asd,
3685 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
3688 goto apply_parameter_failed;
3690 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
3693 goto apply_parameter_failed;
3695 if (!(arg->per_frame_setting && !atomisp_is_vf_pipe(pipe))) {
3696 /* indicate to CSS that we have parameters to be updated */
3697 asd->params.css_update_params_needed = true;
3699 list_add_tail(¶m->list, &pipe->per_frame_params);
3700 atomisp_handle_parameter_and_buffer(pipe);
3705 apply_parameter_failed:
3707 atomisp_free_css_parameters(css_param);
3714 * Function to set/get isp parameters to isp
3716 int atomisp_param(struct atomisp_sub_device *asd, int flag,
3717 struct atomisp_parm *config)
3719 struct ia_css_pipe_config *vp_cfg =
3720 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
3721 pipe_configs[IA_CSS_PIPE_ID_VIDEO];
3723 /* Read parameter for 3A binary info */
3725 struct ia_css_dvs_grid_info *dvs_grid_info =
3726 atomisp_css_get_dvs_grid_info(
3727 &asd->params.curr_grid_info);
3729 atomisp_curr_user_grid_info(asd, &config->info);
3731 /* We always return the resolution and stride even if there is
3732 * no valid metadata. This allows the caller to get the
3733 * information needed to allocate user-space buffers. */
3734 config->metadata_config.metadata_height = asd->
3735 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
3736 metadata_info.resolution.height;
3737 config->metadata_config.metadata_stride = asd->
3738 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
3739 metadata_info.stride;
3741 /* update dvs grid info */
3743 memcpy(&config->dvs_grid,
3745 sizeof(struct ia_css_dvs_grid_info));
3747 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
3748 config->dvs_envelop.width = 0;
3749 config->dvs_envelop.height = 0;
3753 /* update dvs envelop info */
3754 if (!asd->continuous_mode->val) {
3755 config->dvs_envelop.width = vp_cfg->dvs_envelope.width;
3756 config->dvs_envelop.height =
3757 vp_cfg->dvs_envelope.height;
3759 unsigned int dvs_w, dvs_h, dvs_w_max, dvs_h_max;
3761 dvs_w = vp_cfg->bayer_ds_out_res.width -
3762 vp_cfg->output_info[0].res.width;
3763 dvs_h = vp_cfg->bayer_ds_out_res.height -
3764 vp_cfg->output_info[0].res.height;
3765 dvs_w_max = rounddown(
3766 vp_cfg->output_info[0].res.width / 5,
3767 ATOM_ISP_STEP_WIDTH);
3768 dvs_h_max = rounddown(
3769 vp_cfg->output_info[0].res.height / 5,
3770 ATOM_ISP_STEP_HEIGHT);
3772 config->dvs_envelop.width = min(dvs_w, dvs_w_max);
3773 config->dvs_envelop.height = min(dvs_h, dvs_h_max);
3779 memcpy(&asd->params.css_param.wb_config, &config->wb_config,
3780 sizeof(struct ia_css_wb_config));
3781 memcpy(&asd->params.css_param.ob_config, &config->ob_config,
3782 sizeof(struct ia_css_ob_config));
3783 memcpy(&asd->params.css_param.dp_config, &config->dp_config,
3784 sizeof(struct ia_css_dp_config));
3785 memcpy(&asd->params.css_param.de_config, &config->de_config,
3786 sizeof(struct ia_css_de_config));
3787 memcpy(&asd->params.css_param.dz_config, &config->dz_config,
3788 sizeof(struct ia_css_dz_config));
3789 memcpy(&asd->params.css_param.ce_config, &config->ce_config,
3790 sizeof(struct ia_css_ce_config));
3791 memcpy(&asd->params.css_param.nr_config, &config->nr_config,
3792 sizeof(struct ia_css_nr_config));
3793 memcpy(&asd->params.css_param.ee_config, &config->ee_config,
3794 sizeof(struct ia_css_ee_config));
3795 memcpy(&asd->params.css_param.tnr_config, &config->tnr_config,
3796 sizeof(struct ia_css_tnr_config));
3798 if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) {
3799 asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3];
3800 asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4];
3801 asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5];
3802 asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6];
3803 asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7];
3804 asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8];
3807 if (asd->params.color_effect != V4L2_COLORFX_SEPIA &&
3808 asd->params.color_effect != V4L2_COLORFX_BW) {
3809 memcpy(&asd->params.css_param.cc_config, &config->cc_config,
3810 sizeof(struct ia_css_cc_config));
3811 asd->params.config.cc_config = &asd->params.css_param.cc_config;
3814 asd->params.config.wb_config = &asd->params.css_param.wb_config;
3815 asd->params.config.ob_config = &asd->params.css_param.ob_config;
3816 asd->params.config.de_config = &asd->params.css_param.de_config;
3817 asd->params.config.dz_config = &asd->params.css_param.dz_config;
3818 asd->params.config.ce_config = &asd->params.css_param.ce_config;
3819 asd->params.config.dp_config = &asd->params.css_param.dp_config;
3820 asd->params.config.nr_config = &asd->params.css_param.nr_config;
3821 asd->params.config.ee_config = &asd->params.css_param.ee_config;
3822 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
3823 asd->params.css_update_params_needed = true;
3829 * Function to configure color effect of the image
3831 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag,
3834 struct ia_css_cc_config *cc_config = NULL;
3835 struct ia_css_macc_table *macc_table = NULL;
3836 struct ia_css_ctc_table *ctc_table = NULL;
3838 struct v4l2_control control;
3839 struct atomisp_device *isp = asd->isp;
3842 *effect = asd->params.color_effect;
3846 control.id = V4L2_CID_COLORFX;
3847 control.value = *effect;
3849 v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler,
3852 * if set color effect to sensor successfully, return
3856 asd->params.color_effect = (u32)*effect;
3860 if (*effect == asd->params.color_effect)
3864 * isp_subdev->params.macc_en should be set to false.
3866 asd->params.macc_en = false;
3869 case V4L2_COLORFX_NONE:
3870 macc_table = &asd->params.css_param.macc_table;
3871 asd->params.macc_en = true;
3873 case V4L2_COLORFX_SEPIA:
3874 cc_config = &sepia_cc_config;
3876 case V4L2_COLORFX_NEGATIVE:
3877 cc_config = &nega_cc_config;
3879 case V4L2_COLORFX_BW:
3880 cc_config = &mono_cc_config;
3882 case V4L2_COLORFX_SKY_BLUE:
3883 macc_table = &blue_macc_table;
3884 asd->params.macc_en = true;
3886 case V4L2_COLORFX_GRASS_GREEN:
3887 macc_table = &green_macc_table;
3888 asd->params.macc_en = true;
3890 case V4L2_COLORFX_SKIN_WHITEN_LOW:
3891 macc_table = &skin_low_macc_table;
3892 asd->params.macc_en = true;
3894 case V4L2_COLORFX_SKIN_WHITEN:
3895 macc_table = &skin_medium_macc_table;
3896 asd->params.macc_en = true;
3898 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
3899 macc_table = &skin_high_macc_table;
3900 asd->params.macc_en = true;
3902 case V4L2_COLORFX_VIVID:
3903 ctc_table = &vivid_ctc_table;
3908 atomisp_update_capture_mode(asd);
3911 asd->params.config.cc_config = cc_config;
3913 asd->params.config.macc_table = macc_table;
3915 atomisp_css_set_ctc_table(asd, ctc_table);
3916 asd->params.color_effect = (u32)*effect;
3917 asd->params.css_update_params_needed = true;
3922 * Function to configure bad pixel correction
3924 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag,
3928 *value = asd->params.bad_pixel_en;
3931 asd->params.bad_pixel_en = !!*value;
3937 * Function to configure bad pixel correction params
3939 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag,
3940 struct atomisp_dp_config *config)
3943 /* Get bad pixel from current setup */
3944 if (atomisp_css_get_dp_config(asd, config))
3947 /* Set bad pixel to isp parameters */
3948 memcpy(&asd->params.css_param.dp_config, config,
3949 sizeof(asd->params.css_param.dp_config));
3950 asd->params.config.dp_config = &asd->params.css_param.dp_config;
3951 asd->params.css_update_params_needed = true;
3958 * Function to enable/disable video image stablization
3960 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag,
3964 *value = asd->params.video_dis_en;
3966 asd->params.video_dis_en = !!*value;
3972 * Function to configure fixed pattern noise
3974 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag,
3978 *value = asd->params.fpn_en;
3983 asd->params.fpn_en = false;
3987 /* Add function to get black from from sensor with shutter off */
3992 atomisp_bytesperline_to_padded_width(unsigned int bytesperline,
3993 enum ia_css_frame_format format)
3996 case IA_CSS_FRAME_FORMAT_UYVY:
3997 case IA_CSS_FRAME_FORMAT_YUYV:
3998 case IA_CSS_FRAME_FORMAT_RAW:
3999 case IA_CSS_FRAME_FORMAT_RGB565:
4000 return bytesperline / 2;
4001 case IA_CSS_FRAME_FORMAT_RGBA888:
4002 return bytesperline / 4;
4003 /* The following cases could be removed, but we leave them
4004 in to document the formats that are included. */
4005 case IA_CSS_FRAME_FORMAT_NV11:
4006 case IA_CSS_FRAME_FORMAT_NV12:
4007 case IA_CSS_FRAME_FORMAT_NV16:
4008 case IA_CSS_FRAME_FORMAT_NV21:
4009 case IA_CSS_FRAME_FORMAT_NV61:
4010 case IA_CSS_FRAME_FORMAT_YV12:
4011 case IA_CSS_FRAME_FORMAT_YV16:
4012 case IA_CSS_FRAME_FORMAT_YUV420:
4013 case IA_CSS_FRAME_FORMAT_YUV420_16:
4014 case IA_CSS_FRAME_FORMAT_YUV422:
4015 case IA_CSS_FRAME_FORMAT_YUV422_16:
4016 case IA_CSS_FRAME_FORMAT_YUV444:
4017 case IA_CSS_FRAME_FORMAT_YUV_LINE:
4018 case IA_CSS_FRAME_FORMAT_PLANAR_RGB888:
4019 case IA_CSS_FRAME_FORMAT_QPLANE6:
4020 case IA_CSS_FRAME_FORMAT_BINARY_8:
4022 return bytesperline;
4027 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg,
4028 struct ia_css_frame **result)
4030 struct ia_css_frame *res = NULL;
4031 unsigned int padded_width;
4032 enum ia_css_frame_format sh_format;
4033 char *tmp_buf = NULL;
4036 sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat);
4037 padded_width = atomisp_bytesperline_to_padded_width(
4038 arg->fmt.bytesperline, sh_format);
4040 /* Note: the padded width on an ia_css_frame is in elements, not in
4041 bytes. The RAW frame we use here should always be a 16bit RAW
4042 frame. This is why we bytesperline/2 is equal to the padded with */
4043 if (ia_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height,
4044 sh_format, padded_width, 0)) {
4049 tmp_buf = vmalloc(arg->fmt.sizeimage);
4054 if (copy_from_user(tmp_buf, (void __user __force *)arg->base,
4055 arg->fmt.sizeimage)) {
4060 if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) {
4067 ia_css_frame_free(res);
4075 * Function to configure fixed pattern noise table
4077 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd,
4078 struct v4l2_framebuffer *arg)
4080 struct ia_css_frame *raw_black_frame = NULL;
4086 ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame);
4090 if (sh_css_set_black_frame(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream,
4091 raw_black_frame) != 0)
4094 ia_css_frame_free(raw_black_frame);
4099 * Function to configure false color correction
4101 int atomisp_false_color(struct atomisp_sub_device *asd, int flag,
4104 /* Get nr config from current setup */
4106 *value = asd->params.false_color;
4110 /* Set nr config to isp parameters */
4112 asd->params.config.de_config = NULL;
4114 asd->params.css_param.de_config.pixelnoise = 0;
4115 asd->params.config.de_config = &asd->params.css_param.de_config;
4117 asd->params.css_update_params_needed = true;
4118 asd->params.false_color = *value;
4123 * Function to configure bad pixel correction params
4125 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag,
4126 struct atomisp_de_config *config)
4129 /* Get false color from current setup */
4130 if (atomisp_css_get_de_config(asd, config))
4133 /* Set false color to isp parameters */
4134 memcpy(&asd->params.css_param.de_config, config,
4135 sizeof(asd->params.css_param.de_config));
4136 asd->params.config.de_config = &asd->params.css_param.de_config;
4137 asd->params.css_update_params_needed = true;
4144 * Function to configure white balance params
4146 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag,
4147 struct atomisp_wb_config *config)
4150 /* Get white balance from current setup */
4151 if (atomisp_css_get_wb_config(asd, config))
4154 /* Set white balance to isp parameters */
4155 memcpy(&asd->params.css_param.wb_config, config,
4156 sizeof(asd->params.css_param.wb_config));
4157 asd->params.config.wb_config = &asd->params.css_param.wb_config;
4158 asd->params.css_update_params_needed = true;
4164 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag,
4165 struct atomisp_3a_config *config)
4167 struct atomisp_device *isp = asd->isp;
4169 dev_dbg(isp->dev, ">%s %d\n", __func__, flag);
4172 /* Get white balance from current setup */
4173 if (atomisp_css_get_3a_config(asd, config))
4176 /* Set white balance to isp parameters */
4177 memcpy(&asd->params.css_param.s3a_config, config,
4178 sizeof(asd->params.css_param.s3a_config));
4179 asd->params.config.s3a_config = &asd->params.css_param.s3a_config;
4180 asd->params.css_update_params_needed = true;
4183 dev_dbg(isp->dev, "<%s %d\n", __func__, flag);
4188 * Function to setup digital zoom
4190 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag,
4194 struct atomisp_device *isp = asd->isp;
4196 unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR;
4199 atomisp_css_get_zoom_factor(asd, &zoom);
4200 *value = max_zoom - zoom;
4205 zoom = max_zoom - min_t(u32, max_zoom - 1, *value);
4206 atomisp_css_set_zoom_factor(asd, zoom);
4208 dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom);
4209 asd->params.css_update_params_needed = true;
4215 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd,
4216 u16 stream_index, struct atomisp_input_stream_info *stream_info)
4220 /* assign virtual channel id return from sensor driver query */
4221 asd->stream_env[stream_index].ch_id = stream_info->ch_id;
4222 asd->stream_env[stream_index].isys_configs = stream_info->isys_configs;
4223 for (i = 0; i < stream_info->isys_configs; i++) {
4224 asd->stream_env[stream_index].isys_info[i].input_format =
4225 stream_info->isys_info[i].input_format;
4226 asd->stream_env[stream_index].isys_info[i].width =
4227 stream_info->isys_info[i].width;
4228 asd->stream_env[stream_index].isys_info[i].height =
4229 stream_info->isys_info[i].height;
4233 static void __atomisp_init_stream_info(u16 stream_index,
4234 struct atomisp_input_stream_info *stream_info)
4238 stream_info->enable = 1;
4239 stream_info->stream = stream_index;
4240 stream_info->ch_id = 0;
4241 stream_info->isys_configs = 0;
4242 for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) {
4243 stream_info->isys_info[i].input_format = 0;
4244 stream_info->isys_info[i].width = 0;
4245 stream_info->isys_info[i].height = 0;
4249 /* This function looks up the closest available resolution. */
4250 int atomisp_try_fmt(struct video_device *vdev, struct v4l2_pix_format *f,
4253 struct atomisp_device *isp = video_get_drvdata(vdev);
4254 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4255 struct v4l2_subdev_pad_config pad_cfg;
4256 struct v4l2_subdev_state pad_state = {
4259 struct v4l2_subdev_format format = {
4260 .which = V4L2_SUBDEV_FORMAT_TRY,
4263 struct v4l2_mbus_framefmt *snr_mbus_fmt = &format.format;
4264 const struct atomisp_format_bridge *fmt;
4265 struct atomisp_input_stream_info *stream_info =
4266 (struct atomisp_input_stream_info *)snr_mbus_fmt->reserved;
4270 dev_err(isp->dev, "%s(): asd is NULL, device is %s\n",
4271 __func__, vdev->name);
4275 if (!isp->inputs[asd->input_curr].camera)
4278 fmt = atomisp_get_format_bridge(f->pixelformat);
4280 dev_err(isp->dev, "unsupported pixelformat!\n");
4281 fmt = atomisp_output_fmts;
4284 if (f->width <= 0 || f->height <= 0)
4287 snr_mbus_fmt->code = fmt->mbus_code;
4288 snr_mbus_fmt->width = f->width;
4289 snr_mbus_fmt->height = f->height;
4291 __atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4293 dev_dbg(isp->dev, "try_mbus_fmt: asking for %ux%u\n",
4294 snr_mbus_fmt->width, snr_mbus_fmt->height);
4296 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
4297 pad, set_fmt, &pad_state, &format);
4301 dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n",
4302 snr_mbus_fmt->width, snr_mbus_fmt->height);
4304 fmt = atomisp_get_format_bridge_from_mbus(snr_mbus_fmt->code);
4306 dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
4307 snr_mbus_fmt->code);
4311 f->pixelformat = fmt->pixelformat;
4314 * If the format is jpeg or custom RAW, then the width and height will
4315 * not satisfy the normal atomisp requirements and no need to check
4316 * the below conditions. So just assign to what is being returned from
4317 * the sensor driver.
4319 if (f->pixelformat == V4L2_PIX_FMT_JPEG ||
4320 f->pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
4321 f->width = snr_mbus_fmt->width;
4322 f->height = snr_mbus_fmt->height;
4326 if (!res_overflow || (snr_mbus_fmt->width < f->width &&
4327 snr_mbus_fmt->height < f->height)) {
4328 f->width = snr_mbus_fmt->width;
4329 f->height = snr_mbus_fmt->height;
4330 /* Set the flag when resolution requested is
4331 * beyond the max value supported by sensor
4334 *res_overflow = true;
4338 f->width = rounddown(clamp_t(u32, f->width, ATOM_ISP_MIN_WIDTH,
4339 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
4340 f->height = rounddown(clamp_t(u32, f->height, ATOM_ISP_MIN_HEIGHT,
4341 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
4346 enum mipi_port_id __get_mipi_port(struct atomisp_device *isp,
4347 enum atomisp_camera_port port)
4350 case ATOMISP_CAMERA_PORT_PRIMARY:
4351 return MIPI_PORT0_ID;
4352 case ATOMISP_CAMERA_PORT_SECONDARY:
4353 return MIPI_PORT1_ID;
4354 case ATOMISP_CAMERA_PORT_TERTIARY:
4355 if (MIPI_PORT1_ID + 1 != N_MIPI_PORT_ID)
4356 return MIPI_PORT1_ID + 1;
4359 dev_err(isp->dev, "unsupported port: %d\n", port);
4360 return MIPI_PORT0_ID;
4364 static inline int atomisp_set_sensor_mipi_to_isp(
4365 struct atomisp_sub_device *asd,
4366 enum atomisp_input_stream_id stream_id,
4367 struct camera_mipi_info *mipi_info)
4369 struct v4l2_control ctrl;
4370 struct atomisp_device *isp = asd->isp;
4371 const struct atomisp_in_fmt_conv *fc;
4373 unsigned int input_format, bayer_order;
4375 ctrl.id = V4L2_CID_LINK_FREQ;
4377 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl) == 0)
4378 mipi_freq = ctrl.value;
4380 if (asd->stream_env[stream_id].isys_configs == 1) {
4382 asd->stream_env[stream_id].isys_info[0].input_format;
4383 atomisp_css_isys_set_format(asd, stream_id,
4384 input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX);
4385 } else if (asd->stream_env[stream_id].isys_configs == 2) {
4386 atomisp_css_isys_two_stream_cfg_update_stream1(
4388 asd->stream_env[stream_id].isys_info[0].input_format,
4389 asd->stream_env[stream_id].isys_info[0].width,
4390 asd->stream_env[stream_id].isys_info[0].height);
4392 atomisp_css_isys_two_stream_cfg_update_stream2(
4394 asd->stream_env[stream_id].isys_info[1].input_format,
4395 asd->stream_env[stream_id].isys_info[1].width,
4396 asd->stream_env[stream_id].isys_info[1].height);
4399 /* Compatibility for sensors which provide no media bus code
4400 * in s_mbus_framefmt() nor support pad formats. */
4401 if (mipi_info->input_format != -1) {
4402 bayer_order = mipi_info->raw_bayer_order;
4404 /* Input stream config is still needs configured */
4405 /* TODO: Check if this is necessary */
4406 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
4407 mipi_info->input_format);
4410 input_format = fc->atomisp_in_fmt;
4412 struct v4l2_mbus_framefmt *sink;
4414 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4415 V4L2_SUBDEV_FORMAT_ACTIVE,
4416 ATOMISP_SUBDEV_PAD_SINK);
4417 fc = atomisp_find_in_fmt_conv(sink->code);
4420 input_format = fc->atomisp_in_fmt;
4421 bayer_order = fc->bayer_order;
4424 atomisp_css_input_set_format(asd, stream_id, input_format);
4425 atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order);
4427 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
4428 mipi_info->metadata_format);
4431 input_format = fc->atomisp_in_fmt;
4432 atomisp_css_input_configure_port(asd,
4433 __get_mipi_port(asd->isp, mipi_info->port),
4434 mipi_info->num_lanes,
4437 mipi_info->metadata_width,
4438 mipi_info->metadata_height);
4442 static int __enable_continuous_mode(struct atomisp_sub_device *asd,
4445 struct atomisp_device *isp = asd->isp;
4448 "continuous mode %d, raw buffers %d, stop preview %d\n",
4449 enable, asd->continuous_raw_buffer_size->val,
4450 !asd->continuous_viewfinder->val);
4453 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
4455 atomisp_update_capture_mode(asd);
4457 /* in case of ANR, force capture pipe to offline mode */
4458 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
4459 asd->params.low_light ? false : !enable);
4460 atomisp_css_preview_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
4462 atomisp_css_enable_continuous(asd, enable);
4463 atomisp_css_enable_cvf(asd, asd->continuous_viewfinder->val);
4465 atomisp_css_continuous_set_num_raw_frames(asd,
4466 asd->continuous_raw_buffer_size->val);
4469 atomisp_css_enable_raw_binning(asd, false);
4470 atomisp_css_input_set_two_pixels_per_clock(asd, false);
4473 if (isp->inputs[asd->input_curr].type != FILE_INPUT)
4474 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
4476 return atomisp_update_run_mode(asd);
4479 static int configure_pp_input_nop(struct atomisp_sub_device *asd,
4480 unsigned int width, unsigned int height)
4485 static int configure_output_nop(struct atomisp_sub_device *asd,
4486 unsigned int width, unsigned int height,
4487 unsigned int min_width,
4488 enum ia_css_frame_format sh_fmt)
4493 static int get_frame_info_nop(struct atomisp_sub_device *asd,
4494 struct ia_css_frame_info *finfo)
4500 * Resets CSS parameters that depend on input resolution.
4502 * Update params like CSS RAW binning, 2ppc mode and pp_input
4503 * which depend on input size, but are not automatically
4504 * handled in CSS when the input resolution is changed.
4506 static int css_input_resolution_changed(struct atomisp_sub_device *asd,
4507 struct v4l2_mbus_framefmt *ffmt)
4509 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
4512 dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n",
4513 ffmt->width, ffmt->height);
4516 atomisp_css_input_set_two_pixels_per_clock(asd, false);
4518 atomisp_css_input_set_two_pixels_per_clock(asd, true);
4520 if (asd->continuous_mode->val) {
4521 /* Note for all checks: ffmt includes pad_w+pad_h */
4522 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
4523 (ffmt->width >= 2048 || ffmt->height >= 1536)) {
4525 * For preview pipe, enable only if resolution
4526 * is >= 3M for ISP2400.
4528 atomisp_css_enable_raw_binning(asd, true);
4532 * If sensor input changed, which means metadata resolution changed
4533 * together. Release all metadata buffers here to let it re-allocated
4534 * next time in reqbufs.
4536 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
4537 list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
4539 atomisp_css_free_metadata_buffer(md_buf);
4540 list_del(&md_buf->list);
4547 * TODO: atomisp_css_preview_configure_pp_input() not
4548 * reset due to CSS bug tracked as PSI BZ 115124
4552 static int atomisp_set_fmt_to_isp(struct video_device *vdev,
4553 struct ia_css_frame_info *output_info,
4554 struct ia_css_frame_info *raw_output_info,
4555 struct v4l2_pix_format *pix,
4556 unsigned int source_pad)
4558 struct camera_mipi_info *mipi_info;
4559 struct atomisp_device *isp = video_get_drvdata(vdev);
4560 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4561 const struct atomisp_format_bridge *format;
4562 struct v4l2_rect *isp_sink_crop;
4563 enum ia_css_pipe_id pipe_id;
4564 struct v4l2_subdev_fh fh;
4565 int (*configure_output)(struct atomisp_sub_device *asd,
4566 unsigned int width, unsigned int height,
4567 unsigned int min_width,
4568 enum ia_css_frame_format sh_fmt) =
4569 configure_output_nop;
4570 int (*get_frame_info)(struct atomisp_sub_device *asd,
4571 struct ia_css_frame_info *finfo) =
4573 int (*configure_pp_input)(struct atomisp_sub_device *asd,
4574 unsigned int width, unsigned int height) =
4575 configure_pp_input_nop;
4576 const struct atomisp_in_fmt_conv *fc;
4580 dev_err(isp->dev, "%s(): asd is NULL, device is %s\n",
4581 __func__, vdev->name);
4585 v4l2_fh_init(&fh.vfh, vdev);
4587 isp_sink_crop = atomisp_subdev_get_rect(
4588 &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
4589 ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP);
4591 format = atomisp_get_format_bridge(pix->pixelformat);
4595 if (isp->inputs[asd->input_curr].type != TEST_PATTERN &&
4596 isp->inputs[asd->input_curr].type != FILE_INPUT) {
4597 mipi_info = atomisp_to_sensor_mipi_info(
4598 isp->inputs[asd->input_curr].camera);
4600 dev_err(isp->dev, "mipi_info is NULL\n");
4603 if (atomisp_set_sensor_mipi_to_isp(asd, ATOMISP_INPUT_STREAM_GENERAL,
4606 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
4607 mipi_info->input_format);
4609 fc = atomisp_find_in_fmt_conv(
4610 atomisp_subdev_get_ffmt(&asd->subdev,
4611 NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
4612 ATOMISP_SUBDEV_PAD_SINK)->code);
4615 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW &&
4616 raw_output_format_match_input(fc->atomisp_in_fmt,
4622 * Configure viewfinder also when vfpp is disabled: the
4623 * CSS still requires viewfinder configuration.
4625 if (asd->fmt_auto->val ||
4626 asd->vfpp->val != ATOMISP_VFPP_ENABLE) {
4627 struct v4l2_rect vf_size = {0};
4628 struct v4l2_mbus_framefmt vf_ffmt = {0};
4630 if (pix->width < 640 || pix->height < 480) {
4631 vf_size.width = pix->width;
4632 vf_size.height = pix->height;
4634 vf_size.width = 640;
4635 vf_size.height = 480;
4638 /* FIXME: proper format name for this one. See
4639 atomisp_output_fmts[] in atomisp_v4l2.c */
4640 vf_ffmt.code = V4L2_MBUS_FMT_CUSTOM_YUV420;
4642 atomisp_subdev_set_selection(&asd->subdev, fh.state,
4643 V4L2_SUBDEV_FORMAT_ACTIVE,
4644 ATOMISP_SUBDEV_PAD_SOURCE_VF,
4645 V4L2_SEL_TGT_COMPOSE, 0, &vf_size);
4646 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
4647 V4L2_SUBDEV_FORMAT_ACTIVE,
4648 ATOMISP_SUBDEV_PAD_SOURCE_VF, &vf_ffmt);
4649 asd->video_out_vf.sh_fmt = IA_CSS_FRAME_FORMAT_NV12;
4651 if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4652 atomisp_css_video_configure_viewfinder(asd,
4653 vf_size.width, vf_size.height, 0,
4654 asd->video_out_vf.sh_fmt);
4655 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4656 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
4657 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO)
4658 atomisp_css_video_configure_viewfinder(asd,
4659 vf_size.width, vf_size.height, 0,
4660 asd->video_out_vf.sh_fmt);
4662 atomisp_css_capture_configure_viewfinder(asd,
4663 vf_size.width, vf_size.height, 0,
4664 asd->video_out_vf.sh_fmt);
4665 } else if (source_pad != ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
4666 asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
4667 atomisp_css_capture_configure_viewfinder(asd,
4668 vf_size.width, vf_size.height, 0,
4669 asd->video_out_vf.sh_fmt);
4673 if (asd->continuous_mode->val) {
4674 ret = __enable_continuous_mode(asd, true);
4679 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
4681 for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++)
4682 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipe_extra_configs[i].disable_vf_pp = asd->vfpp->val != ATOMISP_VFPP_ENABLE;
4684 /* ISP2401 new input system need to use copy pipe */
4685 if (asd->copy_mode) {
4686 pipe_id = IA_CSS_PIPE_ID_COPY;
4687 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL, false);
4688 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4689 /* video same in continuouscapture and online modes */
4690 configure_output = atomisp_css_video_configure_output;
4691 get_frame_info = atomisp_css_video_get_output_frame_info;
4692 pipe_id = IA_CSS_PIPE_ID_VIDEO;
4693 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4694 if (!asd->continuous_mode->val) {
4695 configure_output = atomisp_css_video_configure_output;
4697 atomisp_css_video_get_output_frame_info;
4698 pipe_id = IA_CSS_PIPE_ID_VIDEO;
4700 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW ||
4701 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
4703 atomisp_css_video_configure_output;
4705 atomisp_css_video_get_output_frame_info;
4706 configure_pp_input =
4707 atomisp_css_video_configure_pp_input;
4708 pipe_id = IA_CSS_PIPE_ID_VIDEO;
4711 atomisp_css_capture_configure_output;
4713 atomisp_css_capture_get_output_frame_info;
4714 configure_pp_input =
4715 atomisp_css_capture_configure_pp_input;
4716 pipe_id = IA_CSS_PIPE_ID_CAPTURE;
4718 atomisp_update_capture_mode(asd);
4719 atomisp_css_capture_enable_online(asd,
4720 ATOMISP_INPUT_STREAM_GENERAL,
4724 } else if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
4725 configure_output = atomisp_css_preview_configure_output;
4726 get_frame_info = atomisp_css_preview_get_output_frame_info;
4727 configure_pp_input = atomisp_css_preview_configure_pp_input;
4728 pipe_id = IA_CSS_PIPE_ID_PREVIEW;
4730 /* CSS doesn't support low light mode on SOC cameras, so disable
4731 * it. FIXME: if this is done elsewhere, it gives corrupted
4732 * colors into thumbnail image.
4734 if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
4735 asd->params.low_light = false;
4737 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
4738 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
4739 atomisp_css_enable_dz(asd, false);
4741 atomisp_update_capture_mode(asd);
4744 if (!asd->continuous_mode->val)
4745 /* in case of ANR, force capture pipe to offline mode */
4746 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
4747 asd->params.low_light ?
4748 false : asd->params.online_process);
4750 configure_output = atomisp_css_capture_configure_output;
4751 get_frame_info = atomisp_css_capture_get_output_frame_info;
4752 configure_pp_input = atomisp_css_capture_configure_pp_input;
4753 pipe_id = IA_CSS_PIPE_ID_CAPTURE;
4755 if (!asd->params.online_process &&
4756 !asd->continuous_mode->val) {
4757 ret = atomisp_css_capture_get_output_raw_frame_info(asd,
4762 if (!asd->continuous_mode->val && asd->run_mode->val
4763 != ATOMISP_RUN_MODE_STILL_CAPTURE) {
4765 "Need to set the running mode first\n");
4766 asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE;
4771 * to SOC camera, use yuvpp pipe.
4773 if (ATOMISP_USE_YUVPP(asd))
4774 pipe_id = IA_CSS_PIPE_ID_YUVPP;
4777 ret = atomisp_css_copy_configure_output(asd, ATOMISP_INPUT_STREAM_GENERAL,
4778 pix->width, pix->height,
4779 format->planar ? pix->bytesperline :
4780 pix->bytesperline * 8 / format->depth,
4783 ret = configure_output(asd, pix->width, pix->height,
4784 format->planar ? pix->bytesperline :
4785 pix->bytesperline * 8 / format->depth,
4788 dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n",
4789 pix->width, pix->height, format->sh_fmt);
4793 ret = configure_pp_input(asd, isp_sink_crop->width, isp_sink_crop->height);
4795 dev_err(isp->dev, "configure_pp_input %ux%u\n",
4796 isp_sink_crop->width,
4797 isp_sink_crop->height);
4801 ret = atomisp_css_copy_get_output_frame_info(asd,
4802 ATOMISP_INPUT_STREAM_GENERAL,
4805 ret = get_frame_info(asd, output_info);
4807 dev_err(isp->dev, "__get_frame_info %ux%u (padded to %u) returned %d\n",
4808 pix->width, pix->height, pix->bytesperline, ret);
4812 atomisp_update_grid_info(asd, pipe_id, source_pad);
4814 /* Free the raw_dump buffer first */
4815 ia_css_frame_free(asd->raw_output_frame);
4816 asd->raw_output_frame = NULL;
4818 if (!asd->continuous_mode->val && !asd->params.online_process &&
4819 ia_css_frame_allocate_from_info(&asd->raw_output_frame,
4826 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd,
4827 unsigned int width, unsigned int height,
4828 unsigned int *dvs_env_w, unsigned int *dvs_env_h)
4830 struct atomisp_device *isp = asd->isp;
4832 /* if subdev type is SOC camera,we do not need to set DVS */
4833 if (isp->inputs[asd->input_curr].type == SOC_CAMERA)
4834 asd->params.video_dis_en = false;
4836 if (asd->params.video_dis_en &&
4837 asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4838 /* envelope is 20% of the output resolution */
4840 * dvs envelope cannot be round up.
4841 * it would cause ISP timeout and color switch issue
4843 *dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH);
4844 *dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT);
4847 asd->params.dis_proj_data_valid = false;
4848 asd->params.css_update_params_needed = true;
4851 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd,
4852 int source_pad, struct v4l2_pix_format *f)
4854 struct v4l2_mbus_framefmt *sink, *src;
4857 /* Only used for the new input system */
4858 asd->copy_mode = false;
4862 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4863 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK);
4864 src = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4865 V4L2_SUBDEV_FORMAT_ACTIVE, source_pad);
4867 if (sink->code == src->code && sink->width == f->width && sink->height == f->height)
4868 asd->copy_mode = true;
4870 asd->copy_mode = false;
4872 dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode);
4875 static int atomisp_set_fmt_to_snr(struct video_device *vdev,
4876 struct v4l2_pix_format *f, unsigned int pixelformat,
4877 unsigned int padding_w, unsigned int padding_h,
4878 unsigned int dvs_env_w, unsigned int dvs_env_h)
4880 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4881 struct atomisp_sub_device *asd = pipe->asd;
4882 const struct atomisp_format_bridge *format;
4883 struct v4l2_subdev_pad_config pad_cfg;
4884 struct v4l2_subdev_state pad_state = {
4887 struct v4l2_subdev_format vformat = {
4888 .which = V4L2_SUBDEV_FORMAT_TRY,
4890 struct v4l2_mbus_framefmt *ffmt = &vformat.format;
4891 struct v4l2_mbus_framefmt *req_ffmt;
4892 struct atomisp_device *isp;
4893 struct atomisp_input_stream_info *stream_info =
4894 (struct atomisp_input_stream_info *)ffmt->reserved;
4895 int source_pad = atomisp_subdev_source_pad(vdev);
4896 struct v4l2_subdev_fh fh;
4900 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
4901 __func__, vdev->name);
4907 v4l2_fh_init(&fh.vfh, vdev);
4909 format = atomisp_get_format_bridge(pixelformat);
4913 v4l2_fill_mbus_format(ffmt, f, format->mbus_code);
4914 ffmt->height += padding_h + dvs_env_h;
4915 ffmt->width += padding_w + dvs_env_w;
4917 dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n",
4918 ffmt->width, ffmt->height, padding_w, padding_h,
4919 dvs_env_w, dvs_env_h);
4921 __atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4925 /* Disable dvs if resolution can't be supported by sensor */
4926 if (asd->params.video_dis_en &&
4927 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
4928 vformat.which = V4L2_SUBDEV_FORMAT_TRY;
4929 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
4930 pad, set_fmt, &pad_state, &vformat);
4934 dev_dbg(isp->dev, "video dis: sensor width: %d, height: %d\n",
4935 ffmt->width, ffmt->height);
4937 if (ffmt->width < req_ffmt->width ||
4938 ffmt->height < req_ffmt->height) {
4939 req_ffmt->height -= dvs_env_h;
4940 req_ffmt->width -= dvs_env_w;
4943 "can not enable video dis due to sensor limitation.");
4944 asd->params.video_dis_en = false;
4947 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
4948 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
4949 set_fmt, NULL, &vformat);
4953 __atomisp_update_stream_env(asd, ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4955 dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
4956 ffmt->width, ffmt->height);
4958 if (ffmt->width < ATOM_ISP_STEP_WIDTH ||
4959 ffmt->height < ATOM_ISP_STEP_HEIGHT)
4962 if (asd->params.video_dis_en &&
4963 source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
4964 (ffmt->width < req_ffmt->width || ffmt->height < req_ffmt->height)) {
4966 "can not enable video dis due to sensor limitation.");
4967 asd->params.video_dis_en = false;
4970 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
4971 V4L2_SUBDEV_FORMAT_ACTIVE,
4972 ATOMISP_SUBDEV_PAD_SINK, ffmt);
4974 return css_input_resolution_changed(asd, ffmt);
4977 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f)
4979 struct atomisp_device *isp = video_get_drvdata(vdev);
4980 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4981 struct atomisp_sub_device *asd = pipe->asd;
4982 const struct atomisp_format_bridge *format_bridge;
4983 const struct atomisp_format_bridge *snr_format_bridge;
4984 struct ia_css_frame_info output_info, raw_output_info;
4985 struct v4l2_pix_format snr_fmt;
4986 struct v4l2_pix_format backup_fmt, s_fmt;
4987 unsigned int dvs_env_w = 0, dvs_env_h = 0;
4988 unsigned int padding_w = pad_w, padding_h = pad_h;
4989 bool res_overflow = false, crop_needs_override = false;
4990 struct v4l2_mbus_framefmt *isp_sink_fmt;
4991 struct v4l2_mbus_framefmt isp_source_fmt = {0};
4992 struct v4l2_subdev_format vformat = {
4993 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
4995 struct v4l2_mbus_framefmt *ffmt = &vformat.format;
4996 struct v4l2_rect isp_sink_crop;
4997 u16 source_pad = atomisp_subdev_source_pad(vdev);
4998 struct v4l2_subdev_fh fh;
5001 ret = atomisp_pipe_check(pipe, true);
5005 if (source_pad >= ATOMISP_SUBDEV_PADS_NUM)
5009 "setting resolution %ux%u on pad %u for asd%d, bytesperline %u\n",
5010 f->fmt.pix.width, f->fmt.pix.height, source_pad,
5011 asd->index, f->fmt.pix.bytesperline);
5013 v4l2_fh_init(&fh.vfh, vdev);
5015 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
5019 /* Currently, raw formats are broken!!! */
5021 if (format_bridge->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
5022 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
5024 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
5028 pipe->sh_fmt = format_bridge->sh_fmt;
5029 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
5031 /* Ensure that the resolution is equal or below the maximum supported */
5033 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
5034 v4l2_fill_mbus_format(ffmt, &f->fmt.pix, format_bridge->mbus_code);
5035 ffmt->height += padding_h;
5036 ffmt->width += padding_w;
5038 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
5039 set_fmt, NULL, &vformat);
5043 f->fmt.pix.width = ffmt->width - padding_w;
5044 f->fmt.pix.height = ffmt->height - padding_h;
5046 snr_fmt = f->fmt.pix;
5047 backup_fmt = snr_fmt;
5049 /**********************************************************************/
5051 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_VF ||
5052 (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5053 && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)) {
5054 if (asd->fmt_auto->val) {
5055 struct v4l2_rect *capture_comp;
5056 struct v4l2_rect r = {0};
5058 r.width = f->fmt.pix.width;
5059 r.height = f->fmt.pix.height;
5061 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW)
5062 capture_comp = atomisp_subdev_get_rect(
5064 V4L2_SUBDEV_FORMAT_ACTIVE,
5065 ATOMISP_SUBDEV_PAD_SOURCE_VIDEO,
5066 V4L2_SEL_TGT_COMPOSE);
5068 capture_comp = atomisp_subdev_get_rect(
5070 V4L2_SUBDEV_FORMAT_ACTIVE,
5071 ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5072 V4L2_SEL_TGT_COMPOSE);
5074 if (capture_comp->width < r.width
5075 || capture_comp->height < r.height) {
5076 r.width = capture_comp->width;
5077 r.height = capture_comp->height;
5080 atomisp_subdev_set_selection(
5081 &asd->subdev, fh.state,
5082 V4L2_SUBDEV_FORMAT_ACTIVE, source_pad,
5083 V4L2_SEL_TGT_COMPOSE, 0, &r);
5085 f->fmt.pix.width = r.width;
5086 f->fmt.pix.height = r.height;
5089 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW) {
5090 atomisp_css_video_configure_viewfinder(asd,
5091 f->fmt.pix.width, f->fmt.pix.height,
5092 format_bridge->planar ? f->fmt.pix.bytesperline
5093 : f->fmt.pix.bytesperline * 8
5094 / format_bridge->depth, format_bridge->sh_fmt);
5095 atomisp_css_video_get_viewfinder_frame_info(asd,
5097 asd->copy_mode = false;
5099 atomisp_css_capture_configure_viewfinder(asd,
5100 f->fmt.pix.width, f->fmt.pix.height,
5101 format_bridge->planar ? f->fmt.pix.bytesperline
5102 : f->fmt.pix.bytesperline * 8
5103 / format_bridge->depth, format_bridge->sh_fmt);
5104 atomisp_css_capture_get_viewfinder_frame_info(asd,
5106 asd->copy_mode = false;
5112 * Check whether main resolution configured smaller
5113 * than snapshot resolution. If so, force main resolution
5114 * to be the same as snapshot resolution
5116 if (source_pad == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE) {
5117 struct v4l2_rect *r;
5119 r = atomisp_subdev_get_rect(
5121 V4L2_SUBDEV_FORMAT_ACTIVE,
5122 ATOMISP_SUBDEV_PAD_SOURCE_VF, V4L2_SEL_TGT_COMPOSE);
5124 if (r->width && r->height
5125 && (r->width > f->fmt.pix.width
5126 || r->height > f->fmt.pix.height))
5128 "Main Resolution config smaller then Vf Resolution. Force to be equal with Vf Resolution.");
5131 /* Pipeline configuration done through subdevs. Bail out now. */
5132 if (!asd->fmt_auto->val)
5133 goto set_fmt_to_isp;
5135 /* get sensor resolution and format */
5136 ret = atomisp_try_fmt(vdev, &snr_fmt, &res_overflow);
5138 dev_warn(isp->dev, "Try format failed with error %d\n", ret);
5141 f->fmt.pix.width = snr_fmt.width;
5142 f->fmt.pix.height = snr_fmt.height;
5144 snr_format_bridge = atomisp_get_format_bridge(snr_fmt.pixelformat);
5145 if (!snr_format_bridge) {
5146 dev_warn(isp->dev, "Can't find bridge format\n");
5150 atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5151 V4L2_SUBDEV_FORMAT_ACTIVE,
5152 ATOMISP_SUBDEV_PAD_SINK)->code =
5153 snr_format_bridge->mbus_code;
5155 isp_sink_fmt = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
5156 V4L2_SUBDEV_FORMAT_ACTIVE,
5157 ATOMISP_SUBDEV_PAD_SINK);
5159 isp_source_fmt.code = format_bridge->mbus_code;
5160 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
5161 V4L2_SUBDEV_FORMAT_ACTIVE,
5162 source_pad, &isp_source_fmt);
5164 if (!atomisp_subdev_format_conversion(asd, source_pad)) {
5167 } else if (IS_BYT) {
5172 /* construct resolution supported by isp */
5173 if (res_overflow && !asd->continuous_mode->val) {
5174 f->fmt.pix.width = rounddown(
5175 clamp_t(u32, f->fmt.pix.width - padding_w,
5177 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
5178 f->fmt.pix.height = rounddown(
5179 clamp_t(u32, f->fmt.pix.height - padding_h,
5180 ATOM_ISP_MIN_HEIGHT,
5181 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
5184 atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height,
5185 &dvs_env_w, &dvs_env_h);
5187 if (asd->continuous_mode->val) {
5188 struct v4l2_rect *r;
5190 r = atomisp_subdev_get_rect(
5192 V4L2_SUBDEV_FORMAT_ACTIVE,
5193 ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE,
5194 V4L2_SEL_TGT_COMPOSE);
5196 * The ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE should get resolutions
5197 * properly set otherwise, it should not be the capture_pad.
5199 if (r->width && r->height)
5200 asd->capture_pad = ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE;
5202 asd->capture_pad = source_pad;
5204 asd->capture_pad = source_pad;
5207 * set format info to sensor
5208 * In continuous mode, resolution is set only if it is higher than
5209 * existing value. This because preview pipe will be configured after
5210 * capture pipe and usually has lower resolution than capture pipe.
5212 if (!asd->continuous_mode->val ||
5213 isp_sink_fmt->width < (f->fmt.pix.width + padding_w + dvs_env_w) ||
5214 isp_sink_fmt->height < (f->fmt.pix.height + padding_h +
5217 * For jpeg or custom raw format the sensor will return constant
5218 * width and height. Because we already had quried try_mbus_fmt,
5219 * f->fmt.pix.width and f->fmt.pix.height has been changed to
5220 * this fixed width and height. So we cannot select the correct
5221 * resolution with that information. So use the original width
5222 * and height while set_mbus_fmt() so actual resolutions are
5223 * being used in while set media bus format.
5226 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG ||
5227 f->fmt.pix.pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW) {
5228 s_fmt.width = backup_fmt.width;
5229 s_fmt.height = backup_fmt.height;
5231 ret = atomisp_set_fmt_to_snr(vdev, &s_fmt,
5232 f->fmt.pix.pixelformat, padding_w,
5233 padding_h, dvs_env_w, dvs_env_h);
5236 "Set format to sensor failed with %d\n", ret);
5240 atomisp_csi_lane_config(isp);
5241 crop_needs_override = true;
5244 atomisp_check_copy_mode(asd, source_pad, &backup_fmt);
5245 asd->yuvpp_mode = false; /* Reset variable */
5247 isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL,
5248 V4L2_SUBDEV_FORMAT_ACTIVE,
5249 ATOMISP_SUBDEV_PAD_SINK,
5252 /* Try to enable YUV downscaling if ISP input is 10 % (either
5253 * width or height) bigger than the desired result. */
5254 if (isp_sink_crop.width * 9 / 10 < f->fmt.pix.width ||
5255 isp_sink_crop.height * 9 / 10 < f->fmt.pix.height ||
5256 (atomisp_subdev_format_conversion(asd, source_pad) &&
5257 ((asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
5258 !asd->continuous_mode->val) ||
5259 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) {
5260 /* for continuous mode, preview size might be smaller than
5261 * still capture size. if preview size still needs crop,
5262 * pick the larger one between crop size of preview and
5265 if (asd->continuous_mode->val
5266 && source_pad == ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW
5267 && !crop_needs_override) {
5268 isp_sink_crop.width =
5269 max_t(unsigned int, f->fmt.pix.width,
5270 isp_sink_crop.width);
5271 isp_sink_crop.height =
5272 max_t(unsigned int, f->fmt.pix.height,
5273 isp_sink_crop.height);
5275 isp_sink_crop.width = f->fmt.pix.width;
5276 isp_sink_crop.height = f->fmt.pix.height;
5279 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5280 V4L2_SUBDEV_FORMAT_ACTIVE,
5281 ATOMISP_SUBDEV_PAD_SINK,
5283 V4L2_SEL_FLAG_KEEP_CONFIG,
5285 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5286 V4L2_SUBDEV_FORMAT_ACTIVE,
5287 source_pad, V4L2_SEL_TGT_COMPOSE,
5289 } else if (IS_MOFD) {
5290 struct v4l2_rect main_compose = {0};
5292 main_compose.width = isp_sink_crop.width;
5293 main_compose.height =
5294 DIV_ROUND_UP(main_compose.width * f->fmt.pix.height,
5296 if (main_compose.height > isp_sink_crop.height) {
5297 main_compose.height = isp_sink_crop.height;
5298 main_compose.width =
5299 DIV_ROUND_UP(main_compose.height *
5304 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5305 V4L2_SUBDEV_FORMAT_ACTIVE,
5307 V4L2_SEL_TGT_COMPOSE, 0,
5310 struct v4l2_rect sink_crop = {0};
5311 struct v4l2_rect main_compose = {0};
5313 main_compose.width = f->fmt.pix.width;
5314 main_compose.height = f->fmt.pix.height;
5316 /* WORKAROUND: this override is universally enabled in
5317 * GMIN to work around a CTS failures (GMINL-539)
5318 * which appears to be related by a hardware
5319 * performance limitation. It's unclear why this
5320 * particular code triggers the issue. */
5321 if (crop_needs_override) {
5322 if (isp_sink_crop.width * main_compose.height >
5323 isp_sink_crop.height * main_compose.width) {
5324 sink_crop.height = isp_sink_crop.height;
5325 sink_crop.width = DIV_NEAREST_STEP(
5329 ATOM_ISP_STEP_WIDTH);
5331 sink_crop.width = isp_sink_crop.width;
5332 sink_crop.height = DIV_NEAREST_STEP(
5336 ATOM_ISP_STEP_HEIGHT);
5338 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5339 V4L2_SUBDEV_FORMAT_ACTIVE,
5340 ATOMISP_SUBDEV_PAD_SINK,
5342 V4L2_SEL_FLAG_KEEP_CONFIG,
5345 atomisp_subdev_set_selection(&asd->subdev, fh.state,
5346 V4L2_SUBDEV_FORMAT_ACTIVE,
5348 V4L2_SEL_TGT_COMPOSE, 0,
5353 ret = atomisp_set_fmt_to_isp(vdev, &output_info, &raw_output_info,
5354 &f->fmt.pix, source_pad);
5356 dev_warn(isp->dev, "Can't set format on ISP. Error %d\n", ret);
5360 pipe->pix.width = f->fmt.pix.width;
5361 pipe->pix.height = f->fmt.pix.height;
5362 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
5364 * FIXME: do we need to setup this differently, depending on the
5365 * sensor or the pipeline?
5367 pipe->pix.colorspace = V4L2_COLORSPACE_REC709;
5368 pipe->pix.ycbcr_enc = V4L2_YCBCR_ENC_709;
5369 pipe->pix.xfer_func = V4L2_XFER_FUNC_709;
5371 if (format_bridge->planar) {
5372 pipe->pix.bytesperline = output_info.padded_width;
5373 pipe->pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height *
5374 DIV_ROUND_UP(format_bridge->depth *
5375 output_info.padded_width, 8));
5377 pipe->pix.bytesperline =
5378 DIV_ROUND_UP(format_bridge->depth *
5379 output_info.padded_width, 8);
5380 pipe->pix.sizeimage =
5381 PAGE_ALIGN(f->fmt.pix.height * pipe->pix.bytesperline);
5383 dev_dbg(isp->dev, "%s: image size: %d, %d bytes per line\n",
5384 __func__, pipe->pix.sizeimage, pipe->pix.bytesperline);
5386 if (f->fmt.pix.field == V4L2_FIELD_ANY)
5387 f->fmt.pix.field = V4L2_FIELD_NONE;
5388 pipe->pix.field = f->fmt.pix.field;
5390 f->fmt.pix = pipe->pix;
5391 f->fmt.pix.priv = PAGE_ALIGN(pipe->pix.width *
5392 pipe->pix.height * 2);
5395 * If in video 480P case, no GFX throttle
5397 if (asd->run_mode->val == ATOMISP_SUBDEV_PAD_SOURCE_VIDEO &&
5398 f->fmt.pix.width == 720 && f->fmt.pix.height == 480)
5399 isp->need_gfx_throttle = false;
5401 isp->need_gfx_throttle = true;
5403 /* Report the needed sizes */
5404 f->fmt.pix.sizeimage = pipe->pix.sizeimage;
5405 f->fmt.pix.bytesperline = pipe->pix.bytesperline;
5407 dev_dbg(isp->dev, "%s: %dx%d, image size: %d, %d bytes per line\n",
5409 f->fmt.pix.width, f->fmt.pix.height,
5410 f->fmt.pix.sizeimage, f->fmt.pix.bytesperline);
5415 int atomisp_set_shading_table(struct atomisp_sub_device *asd,
5416 struct atomisp_shading_table *user_shading_table)
5418 struct ia_css_shading_table *shading_table;
5419 struct ia_css_shading_table *free_table;
5420 unsigned int len_table;
5424 if (!user_shading_table)
5427 if (!user_shading_table->enable) {
5428 asd->params.config.shading_table = NULL;
5429 asd->params.sc_en = false;
5433 /* If enabling, all tables must be set */
5434 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
5435 if (!user_shading_table->data[i])
5439 /* Shading table size per color */
5440 if (user_shading_table->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
5441 user_shading_table->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
5444 shading_table = atomisp_css_shading_table_alloc(
5445 user_shading_table->width, user_shading_table->height);
5449 len_table = user_shading_table->width * user_shading_table->height *
5450 ATOMISP_SC_TYPE_SIZE;
5451 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
5452 ret = copy_from_user(shading_table->data[i],
5453 (void __user *)user_shading_table->data[i],
5456 free_table = shading_table;
5461 shading_table->sensor_width = user_shading_table->sensor_width;
5462 shading_table->sensor_height = user_shading_table->sensor_height;
5463 shading_table->fraction_bits = user_shading_table->fraction_bits;
5465 free_table = asd->params.css_param.shading_table;
5466 asd->params.css_param.shading_table = shading_table;
5467 asd->params.config.shading_table = shading_table;
5468 asd->params.sc_en = true;
5472 atomisp_css_shading_table_free(free_table);
5477 int atomisp_offline_capture_configure(struct atomisp_sub_device *asd,
5478 struct atomisp_cont_capture_conf *cvf_config)
5480 struct v4l2_ctrl *c;
5482 lockdep_assert_held(&asd->isp->mutex);
5485 * In case of M10MO ZSL capture case, we need to issue a separate
5486 * capture request to M10MO which will output captured jpeg image
5489 asd->isp->inputs[asd->input_curr].camera->ctrl_handler,
5490 V4L2_CID_START_ZSL_CAPTURE);
5494 dev_dbg(asd->isp->dev, "%s trigger ZSL capture request\n",
5496 /* TODO: use the cvf_config */
5497 ret = v4l2_ctrl_s_ctrl(c, 1);
5501 return v4l2_ctrl_s_ctrl(c, 0);
5504 asd->params.offline_parm = *cvf_config;
5506 if (asd->params.offline_parm.num_captures) {
5507 if (asd->streaming == ATOMISP_DEVICE_STREAMING_DISABLED) {
5508 unsigned int init_raw_num;
5510 if (asd->enable_raw_buffer_lock->val) {
5512 ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES_LOCK_EN;
5513 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
5514 asd->params.video_dis_en)
5516 ATOMISP_CSS2_NUM_DVS_FRAME_DELAY;
5519 ATOMISP_CSS2_NUM_OFFLINE_INIT_CONTINUOUS_FRAMES;
5522 /* TODO: this can be removed once user-space
5523 * has been updated to use control API */
5524 asd->continuous_raw_buffer_size->val =
5526 asd->continuous_raw_buffer_size->val,
5527 asd->params.offline_parm.
5528 num_captures + init_raw_num);
5529 asd->continuous_raw_buffer_size->val =
5530 min_t(int, ATOMISP_CONT_RAW_FRAMES,
5531 asd->continuous_raw_buffer_size->val);
5533 asd->continuous_mode->val = true;
5535 asd->continuous_mode->val = false;
5536 __enable_continuous_mode(asd, false);
5543 * set auto exposure metering window to camera sensor
5545 int atomisp_s_ae_window(struct atomisp_sub_device *asd,
5546 struct atomisp_ae_window *arg)
5548 struct atomisp_device *isp = asd->isp;
5549 /* Coverity CID 298071 - initialzize struct */
5550 struct v4l2_subdev_selection sel = { 0 };
5552 sel.r.left = arg->x_left;
5553 sel.r.top = arg->y_top;
5554 sel.r.width = arg->x_right - arg->x_left + 1;
5555 sel.r.height = arg->y_bottom - arg->y_top + 1;
5557 if (v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
5558 pad, set_selection, NULL, &sel)) {
5559 dev_err(isp->dev, "failed to call sensor set_selection.\n");
5566 int atomisp_flash_enable(struct atomisp_sub_device *asd, int num_frames)
5568 struct atomisp_device *isp = asd->isp;
5570 if (num_frames < 0) {
5571 dev_dbg(isp->dev, "%s ERROR: num_frames: %d\n", __func__,
5575 /* a requested flash is still in progress. */
5576 if (num_frames && asd->params.flash_state != ATOMISP_FLASH_IDLE) {
5577 dev_dbg(isp->dev, "%s flash busy: %d frames left: %d\n",
5578 __func__, asd->params.flash_state,
5579 asd->params.num_flash_frames);
5583 asd->params.num_flash_frames = num_frames;
5584 asd->params.flash_state = ATOMISP_FLASH_REQUESTED;
5588 bool atomisp_is_vf_pipe(struct atomisp_video_pipe *pipe)
5590 struct atomisp_sub_device *asd = pipe->asd;
5593 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
5594 __func__, pipe->vdev.name);
5598 if (pipe == &asd->video_out_vf)
5601 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
5602 pipe == &asd->video_out_preview)
5608 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id)
5610 struct atomisp_device *isp = asd->isp;
5612 if (!asd->enable_raw_buffer_lock->val) {
5613 dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__);
5616 if (asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED) {
5617 dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n",
5618 __func__, exp_id, asd->streaming);
5621 if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) {
5622 dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id);
5628 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd)
5630 unsigned long flags;
5632 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
5633 memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap));
5634 asd->raw_buffer_locked_count = 0;
5635 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
5638 static int atomisp_set_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
5641 unsigned long flags;
5643 if (__checking_exp_id(asd, exp_id))
5646 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
5648 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
5649 (*bitmap) |= (1 << bit);
5650 asd->raw_buffer_locked_count++;
5651 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
5653 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
5654 __func__, exp_id, asd->raw_buffer_locked_count);
5656 /* Check if the raw buffer after next is still locked!!! */
5658 if (exp_id > ATOMISP_MAX_EXP_ID)
5659 exp_id -= ATOMISP_MAX_EXP_ID;
5660 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
5662 if ((*bitmap) & (1 << bit)) {
5665 /* WORKAROUND unlock the raw buffer compulsively */
5666 ret = atomisp_css_exp_id_unlock(asd, exp_id);
5668 dev_err(asd->isp->dev,
5669 "%s exp_id is wrapping back to %d but force unlock failed, err %d.\n",
5670 __func__, exp_id, ret);
5674 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
5675 (*bitmap) &= ~(1 << bit);
5676 asd->raw_buffer_locked_count--;
5677 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
5678 dev_warn(asd->isp->dev,
5679 "%s exp_id is wrapping back to %d but it is still locked so force unlock it, raw_buffer_locked_count %d\n",
5680 __func__, exp_id, asd->raw_buffer_locked_count);
5685 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id)
5688 unsigned long flags;
5691 if (__checking_exp_id(asd, exp_id))
5694 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
5696 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
5697 ret = ((*bitmap) & (1 << bit));
5698 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
5702 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
5705 unsigned long flags;
5707 if (__is_raw_buffer_locked(asd, exp_id))
5710 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
5712 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
5713 (*bitmap) &= ~(1 << bit);
5714 asd->raw_buffer_locked_count--;
5715 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
5717 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
5718 __func__, exp_id, asd->raw_buffer_locked_count);
5722 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id)
5724 struct atomisp_device *isp = asd->isp;
5725 int value = *exp_id;
5728 lockdep_assert_held(&isp->mutex);
5730 ret = __is_raw_buffer_locked(asd, value);
5732 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
5736 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
5737 ret = atomisp_css_exp_id_capture(asd, value);
5739 dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value);
5745 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id)
5747 struct atomisp_device *isp = asd->isp;
5748 int value = *exp_id;
5751 lockdep_assert_held(&isp->mutex);
5753 ret = __clear_raw_buffer_bitmap(asd, value);
5755 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
5759 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
5760 ret = atomisp_css_exp_id_unlock(asd, value);
5762 dev_err(isp->dev, "%s exp_id %d failed, err %d.\n",
5763 __func__, value, ret);
5768 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd,
5769 unsigned int *enable)
5776 value = *enable > 0;
5778 atomisp_en_dz_capt_pipe(asd, value);
5783 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event)
5785 if (!event || asd->streaming != ATOMISP_DEVICE_STREAMING_ENABLED)
5788 lockdep_assert_held(&asd->isp->mutex);
5790 dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n",
5794 case V4L2_EVENT_FRAME_SYNC:
5795 atomisp_sof_event(asd);
5797 case V4L2_EVENT_FRAME_END:
5798 atomisp_eof_event(asd, 0);
5800 case V4L2_EVENT_ATOMISP_3A_STATS_READY:
5801 atomisp_3a_stats_ready_event(asd, 0);
5803 case V4L2_EVENT_ATOMISP_METADATA_READY:
5804 atomisp_metadata_ready_event(asd, 0);
5813 static int atomisp_get_pipe_id(struct atomisp_video_pipe *pipe)
5815 struct atomisp_sub_device *asd = pipe->asd;
5818 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
5819 __func__, pipe->vdev.name);
5823 if (ATOMISP_USE_YUVPP(asd)) {
5824 return IA_CSS_PIPE_ID_YUVPP;
5825 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
5826 return IA_CSS_PIPE_ID_VIDEO;
5827 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
5828 return IA_CSS_PIPE_ID_CAPTURE;
5829 } else if (pipe == &asd->video_out_video_capture) {
5830 return IA_CSS_PIPE_ID_VIDEO;
5831 } else if (pipe == &asd->video_out_vf) {
5832 return IA_CSS_PIPE_ID_CAPTURE;
5833 } else if (pipe == &asd->video_out_preview) {
5834 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
5835 return IA_CSS_PIPE_ID_VIDEO;
5837 return IA_CSS_PIPE_ID_PREVIEW;
5838 } else if (pipe == &asd->video_out_capture) {
5840 return IA_CSS_PIPE_ID_COPY;
5842 return IA_CSS_PIPE_ID_CAPTURE;
5846 dev_warn(asd->isp->dev, "%s failed to find proper pipe\n",
5848 return IA_CSS_PIPE_ID_CAPTURE;
5851 int atomisp_get_invalid_frame_num(struct video_device *vdev,
5852 int *invalid_frame_num)
5854 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
5855 struct atomisp_sub_device *asd = pipe->asd;
5856 enum ia_css_pipe_id pipe_id;
5857 struct ia_css_pipe_info p_info;
5860 pipe_id = atomisp_get_pipe_id(pipe);
5861 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipes[pipe_id]) {
5862 dev_warn(asd->isp->dev,
5863 "%s pipe %d has not been created yet, do SET_FMT first!\n",
5868 ret = ia_css_pipe_get_info(
5869 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
5870 .pipes[pipe_id], &p_info);
5872 *invalid_frame_num = p_info.num_invalid_frames;
5875 dev_warn(asd->isp->dev, "%s get pipe infor failed %d\n",