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;
83 * get sensor:dis71430/ov2720 related info from v4l2_subdev->priv data field.
84 * subdev->priv is set in mrst.c
86 struct camera_mipi_info *atomisp_to_sensor_mipi_info(struct v4l2_subdev *sd)
88 return (struct camera_mipi_info *)v4l2_get_subdev_hostdata(sd);
92 * get struct atomisp_video_pipe from v4l2 video_device
94 struct atomisp_video_pipe *atomisp_to_video_pipe(struct video_device *dev)
96 return (struct atomisp_video_pipe *)
97 container_of(dev, struct atomisp_video_pipe, vdev);
100 static unsigned short atomisp_get_sensor_fps(struct atomisp_sub_device *asd)
102 struct v4l2_subdev_frame_interval fi = { 0 };
103 struct atomisp_device *isp = asd->isp;
105 unsigned short fps = 0;
108 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
109 video, g_frame_interval, &fi);
111 if (!ret && fi.interval.numerator)
112 fps = fi.interval.denominator / fi.interval.numerator;
118 * DFS progress is shown as follows:
119 * 1. Target frequency is calculated according to FPS/Resolution/ISP running
121 * 2. Ratio is calculated using formula: 2 * HPLL / target frequency - 1
122 * with proper rounding.
123 * 3. Set ratio to ISPFREQ40, 1 to FREQVALID and ISPFREQGUAR40
124 * to 200MHz in ISPSSPM1.
125 * 4. Wait for FREQVALID to be cleared by P-Unit.
126 * 5. Wait for field ISPFREQSTAT40 in ISPSSPM1 turn to ratio set in 3.
128 static int write_target_freq_to_hw(struct atomisp_device *isp,
129 unsigned int new_freq)
131 unsigned int ratio, timeout, guar_ratio;
135 if (!isp->hpll_freq) {
136 dev_err(isp->dev, "failed to get hpll_freq. no change to freq\n");
140 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
141 if (isp_sspm1 & ISP_FREQ_VALID_MASK) {
142 dev_dbg(isp->dev, "clearing ISPSSPM1 valid bit.\n");
143 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
144 isp_sspm1 & ~(1 << ISP_FREQ_VALID_OFFSET));
147 ratio = (2 * isp->hpll_freq + new_freq / 2) / new_freq - 1;
148 guar_ratio = (2 * isp->hpll_freq + 200 / 2) / 200 - 1;
150 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
151 isp_sspm1 &= ~(0x1F << ISP_REQ_FREQ_OFFSET);
153 for (i = 0; i < ISP_DFS_TRY_TIMES; i++) {
154 iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, ISPSSPM1,
156 | ratio << ISP_REQ_FREQ_OFFSET
157 | 1 << ISP_FREQ_VALID_OFFSET
158 | guar_ratio << ISP_REQ_GUAR_FREQ_OFFSET);
160 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
162 while ((isp_sspm1 & ISP_FREQ_VALID_MASK) && timeout) {
163 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
164 dev_dbg(isp->dev, "waiting for ISPSSPM1 valid bit to be 0.\n");
174 dev_err(isp->dev, "DFS failed due to HW error.\n");
178 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
180 while (((isp_sspm1 >> ISP_FREQ_STAT_OFFSET) != ratio) && timeout) {
181 iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM1, &isp_sspm1);
182 dev_dbg(isp->dev, "waiting for ISPSSPM1 status bit to be 0x%x.\n",
188 dev_err(isp->dev, "DFS target freq is rejected by HW.\n");
195 int atomisp_freq_scaling(struct atomisp_device *isp,
196 enum atomisp_dfs_mode mode,
199 const struct atomisp_dfs_config *dfs;
200 unsigned int new_freq;
201 struct atomisp_freq_scaling_rule curr_rules;
203 unsigned short fps = 0;
207 if (dfs->lowest_freq == 0 || dfs->max_freq_at_vmin == 0 ||
208 dfs->highest_freq == 0 || dfs->dfs_table_size == 0 ||
210 dev_err(isp->dev, "DFS configuration is invalid.\n");
214 if (mode == ATOMISP_DFS_MODE_LOW) {
215 new_freq = dfs->lowest_freq;
219 if (mode == ATOMISP_DFS_MODE_MAX) {
220 new_freq = dfs->highest_freq;
224 fps = atomisp_get_sensor_fps(&isp->asd);
227 "Sensor didn't report FPS. Using DFS max mode.\n");
228 new_freq = dfs->highest_freq;
232 curr_rules.width = isp->asd.fmt[ATOMISP_SUBDEV_PAD_SOURCE].fmt.width;
233 curr_rules.height = isp->asd.fmt[ATOMISP_SUBDEV_PAD_SOURCE].fmt.height;
234 curr_rules.fps = fps;
235 curr_rules.run_mode = isp->asd.run_mode->val;
237 /* search for the target frequency by looping freq rules*/
238 for (i = 0; i < dfs->dfs_table_size; i++) {
239 if (curr_rules.width != dfs->dfs_table[i].width &&
240 dfs->dfs_table[i].width != ISP_FREQ_RULE_ANY)
242 if (curr_rules.height != dfs->dfs_table[i].height &&
243 dfs->dfs_table[i].height != ISP_FREQ_RULE_ANY)
245 if (curr_rules.fps != dfs->dfs_table[i].fps &&
246 dfs->dfs_table[i].fps != ISP_FREQ_RULE_ANY)
248 if (curr_rules.run_mode != dfs->dfs_table[i].run_mode &&
249 dfs->dfs_table[i].run_mode != ISP_FREQ_RULE_ANY)
254 if (i == dfs->dfs_table_size)
255 new_freq = dfs->max_freq_at_vmin;
257 new_freq = dfs->dfs_table[i].isp_freq;
260 dev_dbg(isp->dev, "DFS target frequency=%d.\n", new_freq);
262 if ((new_freq == isp->running_freq) && !force)
265 dev_dbg(isp->dev, "Programming DFS frequency to %d\n", new_freq);
267 ret = write_target_freq_to_hw(isp, new_freq);
269 isp->running_freq = new_freq;
270 trace_ipu_pstate(new_freq, -1);
276 * reset and restore ISP
278 int atomisp_reset(struct atomisp_device *isp)
280 /* Reset ISP by power-cycling it */
283 dev_dbg(isp->dev, "%s\n", __func__);
285 ret = atomisp_power_off(isp->dev);
287 dev_err(isp->dev, "atomisp_power_off failed, %d\n", ret);
289 ret = atomisp_power_on(isp->dev);
291 dev_err(isp->dev, "atomisp_power_on failed, %d\n", ret);
292 isp->isp_fatal_error = true;
299 * interrupt disable functions
301 static void disable_isp_irq(enum hrt_isp_css_irq irq)
303 irq_disable_channel(IRQ0_ID, irq);
305 if (irq != hrt_isp_css_irq_sp)
308 cnd_sp_irq_enable(SP0_ID, false);
312 * interrupt clean function
314 static void clear_isp_irq(enum hrt_isp_css_irq irq)
316 irq_clear_all(IRQ0_ID);
319 void atomisp_msi_irq_init(struct atomisp_device *isp)
321 struct pci_dev *pdev = to_pci_dev(isp->dev);
325 pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32);
326 msg32 |= 1 << MSI_ENABLE_BIT;
327 pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32);
329 msg32 = (1 << INTR_IER) | (1 << INTR_IIR);
330 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32);
332 pci_read_config_word(pdev, PCI_COMMAND, &msg16);
333 msg16 |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
334 PCI_COMMAND_INTX_DISABLE);
335 pci_write_config_word(pdev, PCI_COMMAND, msg16);
338 void atomisp_msi_irq_uninit(struct atomisp_device *isp)
340 struct pci_dev *pdev = to_pci_dev(isp->dev);
344 pci_read_config_dword(pdev, PCI_MSI_CAPID, &msg32);
345 msg32 &= ~(1 << MSI_ENABLE_BIT);
346 pci_write_config_dword(pdev, PCI_MSI_CAPID, msg32);
349 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg32);
351 pci_read_config_word(pdev, PCI_COMMAND, &msg16);
352 msg16 &= ~(PCI_COMMAND_MASTER);
353 pci_write_config_word(pdev, PCI_COMMAND, msg16);
356 static void atomisp_sof_event(struct atomisp_sub_device *asd)
358 struct v4l2_event event = {0};
360 event.type = V4L2_EVENT_FRAME_SYNC;
361 event.u.frame_sync.frame_sequence = atomic_read(&asd->sof_count);
363 v4l2_event_queue(asd->subdev.devnode, &event);
366 void atomisp_eof_event(struct atomisp_sub_device *asd, uint8_t exp_id)
368 struct v4l2_event event = {0};
370 event.type = V4L2_EVENT_FRAME_END;
371 event.u.frame_sync.frame_sequence = exp_id;
373 v4l2_event_queue(asd->subdev.devnode, &event);
376 static void atomisp_3a_stats_ready_event(struct atomisp_sub_device *asd,
379 struct v4l2_event event = {0};
381 event.type = V4L2_EVENT_ATOMISP_3A_STATS_READY;
382 event.u.frame_sync.frame_sequence = exp_id;
384 v4l2_event_queue(asd->subdev.devnode, &event);
387 static void atomisp_metadata_ready_event(struct atomisp_sub_device *asd,
388 enum atomisp_metadata_type md_type)
390 struct v4l2_event event = {0};
392 event.type = V4L2_EVENT_ATOMISP_METADATA_READY;
393 event.u.data[0] = md_type;
395 v4l2_event_queue(asd->subdev.devnode, &event);
398 static void atomisp_reset_event(struct atomisp_sub_device *asd)
400 struct v4l2_event event = {0};
402 event.type = V4L2_EVENT_ATOMISP_CSS_RESET;
404 v4l2_event_queue(asd->subdev.devnode, &event);
407 static void print_csi_rx_errors(enum mipi_port_id port,
408 struct atomisp_device *isp)
412 atomisp_css_rx_get_irq_info(port, &infos);
414 dev_err(isp->dev, "CSI Receiver port %d errors:\n", port);
415 if (infos & IA_CSS_RX_IRQ_INFO_BUFFER_OVERRUN)
416 dev_err(isp->dev, " buffer overrun");
417 if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT)
418 dev_err(isp->dev, " start-of-transmission error");
419 if (infos & IA_CSS_RX_IRQ_INFO_ERR_SOT_SYNC)
420 dev_err(isp->dev, " start-of-transmission sync error");
421 if (infos & IA_CSS_RX_IRQ_INFO_ERR_CONTROL)
422 dev_err(isp->dev, " control error");
423 if (infos & IA_CSS_RX_IRQ_INFO_ERR_ECC_DOUBLE)
424 dev_err(isp->dev, " 2 or more ECC errors");
425 if (infos & IA_CSS_RX_IRQ_INFO_ERR_CRC)
426 dev_err(isp->dev, " CRC mismatch");
427 if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ID)
428 dev_err(isp->dev, " unknown error");
429 if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_SYNC)
430 dev_err(isp->dev, " frame sync error");
431 if (infos & IA_CSS_RX_IRQ_INFO_ERR_FRAME_DATA)
432 dev_err(isp->dev, " frame data error");
433 if (infos & IA_CSS_RX_IRQ_INFO_ERR_DATA_TIMEOUT)
434 dev_err(isp->dev, " data timeout");
435 if (infos & IA_CSS_RX_IRQ_INFO_ERR_UNKNOWN_ESC)
436 dev_err(isp->dev, " unknown escape command entry");
437 if (infos & IA_CSS_RX_IRQ_INFO_ERR_LINE_SYNC)
438 dev_err(isp->dev, " line sync error");
442 static void clear_irq_reg(struct atomisp_device *isp)
444 struct pci_dev *pdev = to_pci_dev(isp->dev);
447 pci_read_config_dword(pdev, PCI_INTERRUPT_CTRL, &msg_ret);
448 msg_ret |= 1 << INTR_IIR;
449 pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, msg_ret);
452 /* interrupt handling function*/
453 irqreturn_t atomisp_isr(int irq, void *dev)
455 struct atomisp_device *isp = (struct atomisp_device *)dev;
456 struct atomisp_css_event eof_event;
457 unsigned int irq_infos = 0;
461 spin_lock_irqsave(&isp->lock, flags);
463 if (!isp->css_initialized) {
464 spin_unlock_irqrestore(&isp->lock, flags);
467 err = atomisp_css_irq_translate(isp, &irq_infos);
469 spin_unlock_irqrestore(&isp->lock, flags);
475 if (!isp->asd.streaming)
478 if (irq_infos & IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF) {
479 atomic_inc(&isp->asd.sof_count);
480 atomisp_sof_event(&isp->asd);
483 * If sequence_temp and sequence are the same there where no frames
484 * lost so we can increase sequence_temp.
485 * If not then processing of frame is still in progress and driver
486 * needs to keep old sequence_temp value.
487 * NOTE: There is assumption here that ISP will not start processing
488 * next frame from sensor before old one is completely done.
490 if (atomic_read(&isp->asd.sequence) == atomic_read(&isp->asd.sequence_temp))
491 atomic_set(&isp->asd.sequence_temp, atomic_read(&isp->asd.sof_count));
493 dev_dbg_ratelimited(isp->dev, "irq:0x%x (SOF)\n", irq_infos);
494 irq_infos &= ~IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF;
497 if (irq_infos & IA_CSS_IRQ_INFO_EVENTS_READY)
498 atomic_set(&isp->asd.sequence, atomic_read(&isp->asd.sequence_temp));
500 if ((irq_infos & IA_CSS_IRQ_INFO_INPUT_SYSTEM_ERROR) ||
501 (irq_infos & IA_CSS_IRQ_INFO_IF_ERROR)) {
502 /* handle mipi receiver error */
504 enum mipi_port_id port;
506 for (port = MIPI_PORT0_ID; port <= MIPI_PORT2_ID;
508 print_csi_rx_errors(port, isp);
509 atomisp_css_rx_get_irq_info(port, &rx_infos);
510 atomisp_css_rx_clear_irq_info(port, rx_infos);
514 if (irq_infos & IA_CSS_IRQ_INFO_ISYS_EVENTS_READY) {
515 while (ia_css_dequeue_isys_event(&eof_event.event) == 0) {
516 atomisp_eof_event(&isp->asd, eof_event.event.exp_id);
517 dev_dbg_ratelimited(isp->dev, "ISYS event: EOF exp_id %d\n",
518 eof_event.event.exp_id);
521 irq_infos &= ~IA_CSS_IRQ_INFO_ISYS_EVENTS_READY;
526 spin_unlock_irqrestore(&isp->lock, flags);
528 dev_dbg_ratelimited(isp->dev, "irq:0x%x (unhandled)\n", irq_infos);
530 return IRQ_WAKE_THREAD;
533 spin_unlock_irqrestore(&isp->lock, flags);
536 dev_dbg_ratelimited(isp->dev, "irq:0x%x (ignored, as not streaming anymore)\n",
542 void atomisp_clear_css_buffer_counters(struct atomisp_sub_device *asd)
546 memset(asd->s3a_bufs_in_css, 0, sizeof(asd->s3a_bufs_in_css));
547 for (i = 0; i < ATOMISP_INPUT_STREAM_NUM; i++)
548 memset(asd->metadata_bufs_in_css[i], 0,
549 sizeof(asd->metadata_bufs_in_css[i]));
550 asd->dis_bufs_in_css = 0;
553 /* 0x100000 is the start of dmem inside SP */
554 #define SP_DMEM_BASE 0x100000
556 void dump_sp_dmem(struct atomisp_device *isp, unsigned int addr,
559 unsigned int data = 0;
560 unsigned int size32 = DIV_ROUND_UP(size, sizeof(u32));
562 dev_dbg(isp->dev, "atomisp mmio base: %p\n", isp->base);
563 dev_dbg(isp->dev, "%s, addr:0x%x, size: %d, size32: %d\n", __func__,
565 if (size32 * 4 + addr > 0x4000) {
566 dev_err(isp->dev, "illegal size (%d) or addr (0x%x)\n",
570 addr += SP_DMEM_BASE;
573 data = readl(isp->base + addr);
574 dev_dbg(isp->dev, "%s, \t [0x%x]:0x%x\n", __func__, addr, data);
579 int atomisp_buffers_in_css(struct atomisp_video_pipe *pipe)
581 unsigned long irqflags;
582 struct list_head *pos;
583 int buffers_in_css = 0;
585 spin_lock_irqsave(&pipe->irq_lock, irqflags);
587 list_for_each(pos, &pipe->buffers_in_css)
590 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
592 return buffers_in_css;
595 void atomisp_buffer_done(struct ia_css_frame *frame, enum vb2_buffer_state state)
597 struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf);
599 lockdep_assert_held(&pipe->irq_lock);
601 frame->vb.vb2_buf.timestamp = ktime_get_ns();
602 frame->vb.field = pipe->pix.field;
603 frame->vb.sequence = atomic_read(&pipe->asd->sequence);
604 list_del(&frame->queue);
605 if (state == VB2_BUF_STATE_DONE)
606 vb2_set_plane_payload(&frame->vb.vb2_buf, 0, pipe->pix.sizeimage);
607 vb2_buffer_done(&frame->vb.vb2_buf, state);
610 void atomisp_flush_video_pipe(struct atomisp_video_pipe *pipe, enum vb2_buffer_state state,
611 bool warn_on_css_frames)
613 struct ia_css_frame *frame, *_frame;
614 unsigned long irqflags;
616 spin_lock_irqsave(&pipe->irq_lock, irqflags);
618 list_for_each_entry_safe(frame, _frame, &pipe->buffers_in_css, queue) {
619 if (warn_on_css_frames)
620 dev_warn(pipe->isp->dev, "Warning: CSS frames queued on flush\n");
621 atomisp_buffer_done(frame, state);
624 list_for_each_entry_safe(frame, _frame, &pipe->activeq, queue)
625 atomisp_buffer_done(frame, state);
627 list_for_each_entry_safe(frame, _frame, &pipe->buffers_waiting_for_param, queue) {
628 pipe->frame_request_config_id[frame->vb.vb2_buf.index] = 0;
629 atomisp_buffer_done(frame, state);
632 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
635 /* clean out the parameters that did not apply */
636 void atomisp_flush_params_queue(struct atomisp_video_pipe *pipe)
638 struct atomisp_css_params_with_list *param;
640 while (!list_empty(&pipe->per_frame_params)) {
641 param = list_entry(pipe->per_frame_params.next,
642 struct atomisp_css_params_with_list, list);
643 list_del(¶m->list);
644 atomisp_free_css_parameters(¶m->params);
649 /* Re-queue per-frame parameters */
650 static void atomisp_recover_params_queue(struct atomisp_video_pipe *pipe)
652 struct atomisp_css_params_with_list *param;
655 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
656 param = pipe->frame_params[i];
658 list_add_tail(¶m->list, &pipe->per_frame_params);
659 pipe->frame_params[i] = NULL;
661 atomisp_handle_parameter_and_buffer(pipe);
664 void atomisp_buf_done(struct atomisp_sub_device *asd, int error,
665 enum ia_css_buffer_type buf_type,
666 enum ia_css_pipe_id css_pipe_id,
667 bool q_buffers, enum atomisp_input_stream_id stream_id)
669 struct atomisp_video_pipe *pipe = NULL;
670 struct atomisp_css_buffer buffer;
671 bool requeue = false;
672 unsigned long irqflags;
673 struct ia_css_frame *frame = NULL;
674 struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf_tmp, *s3a_iter;
675 struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf_tmp, *dis_iter;
676 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf_tmp, *md_iter;
677 enum atomisp_metadata_type md_type;
678 struct atomisp_device *isp = asd->isp;
679 struct v4l2_control ctrl;
682 lockdep_assert_held(&isp->mutex);
685 buf_type != IA_CSS_BUFFER_TYPE_METADATA &&
686 buf_type != IA_CSS_BUFFER_TYPE_3A_STATISTICS &&
687 buf_type != IA_CSS_BUFFER_TYPE_DIS_STATISTICS &&
688 buf_type != IA_CSS_BUFFER_TYPE_OUTPUT_FRAME &&
689 buf_type != IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME &&
690 buf_type != IA_CSS_BUFFER_TYPE_RAW_OUTPUT_FRAME &&
691 buf_type != IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME &&
692 buf_type != IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
693 dev_err(isp->dev, "%s, unsupported buffer type: %d\n",
698 memset(&buffer, 0, sizeof(struct atomisp_css_buffer));
699 buffer.css_buffer.type = buf_type;
700 err = atomisp_css_dequeue_buffer(asd, stream_id, css_pipe_id,
704 "atomisp_css_dequeue_buffer failed: 0x%x\n", err);
709 case IA_CSS_BUFFER_TYPE_3A_STATISTICS:
710 list_for_each_entry_safe(s3a_iter, _s3a_buf_tmp,
711 &asd->s3a_stats_in_css, list) {
712 if (s3a_iter->s3a_data ==
713 buffer.css_buffer.data.stats_3a) {
714 list_del_init(&s3a_iter->list);
715 list_add_tail(&s3a_iter->list,
716 &asd->s3a_stats_ready);
722 asd->s3a_bufs_in_css[css_pipe_id]--;
723 atomisp_3a_stats_ready_event(asd, buffer.css_buffer.exp_id);
725 dev_dbg(isp->dev, "%s: s3a stat with exp_id %d is ready\n",
726 __func__, s3a_buf->s3a_data->exp_id);
728 dev_dbg(isp->dev, "%s: s3a stat is ready with no exp_id found\n",
731 case IA_CSS_BUFFER_TYPE_METADATA:
735 md_type = ATOMISP_MAIN_METADATA;
736 list_for_each_entry_safe(md_iter, _md_buf_tmp,
737 &asd->metadata_in_css[md_type], list) {
738 if (md_iter->metadata ==
739 buffer.css_buffer.data.metadata) {
740 list_del_init(&md_iter->list);
741 list_add_tail(&md_iter->list,
742 &asd->metadata_ready[md_type]);
747 asd->metadata_bufs_in_css[stream_id][css_pipe_id]--;
748 atomisp_metadata_ready_event(asd, md_type);
750 dev_dbg(isp->dev, "%s: metadata with exp_id %d is ready\n",
751 __func__, md_buf->metadata->exp_id);
753 dev_dbg(isp->dev, "%s: metadata is ready with no exp_id found\n",
756 case IA_CSS_BUFFER_TYPE_DIS_STATISTICS:
757 list_for_each_entry_safe(dis_iter, _dis_buf_tmp,
758 &asd->dis_stats_in_css, list) {
759 if (dis_iter->dis_data ==
760 buffer.css_buffer.data.stats_dvs) {
761 spin_lock_irqsave(&asd->dis_stats_lock,
763 list_del_init(&dis_iter->list);
764 list_add(&dis_iter->list, &asd->dis_stats);
765 asd->params.dis_proj_data_valid = true;
766 spin_unlock_irqrestore(&asd->dis_stats_lock,
772 asd->dis_bufs_in_css--;
774 dev_dbg(isp->dev, "%s: dis stat with exp_id %d is ready\n",
775 __func__, dis_buf->dis_data->exp_id);
777 dev_dbg(isp->dev, "%s: dis stat is ready with no exp_id found\n",
780 case IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME:
781 case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
782 frame = buffer.css_buffer.data.frame;
790 pipe = vb_to_pipe(&frame->vb.vb2_buf);
792 dev_dbg(isp->dev, "%s: vf frame with exp_id %d is ready\n",
793 __func__, frame->exp_id);
794 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
795 if (frame->flash_state
796 == IA_CSS_FRAME_FLASH_STATE_PARTIAL)
797 dev_dbg(isp->dev, "%s thumb partially flashed\n",
799 else if (frame->flash_state
800 == IA_CSS_FRAME_FLASH_STATE_FULL)
801 dev_dbg(isp->dev, "%s thumb completely flashed\n",
804 dev_dbg(isp->dev, "%s thumb no flash in this frame\n",
807 pipe->frame_config_id[frame->vb.vb2_buf.index] = frame->isp_config_id;
809 case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
810 case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
811 frame = buffer.css_buffer.data.frame;
820 pipe = vb_to_pipe(&frame->vb.vb2_buf);
822 dev_dbg(isp->dev, "%s: main frame with exp_id %d is ready\n",
823 __func__, frame->exp_id);
825 i = frame->vb.vb2_buf.index;
827 /* free the parameters */
828 if (pipe->frame_params[i]) {
829 if (asd->params.dvs_6axis == pipe->frame_params[i]->params.dvs_6axis)
830 asd->params.dvs_6axis = NULL;
831 atomisp_free_css_parameters(&pipe->frame_params[i]->params);
832 kvfree(pipe->frame_params[i]);
833 pipe->frame_params[i] = NULL;
836 pipe->frame_config_id[i] = frame->isp_config_id;
837 ctrl.id = V4L2_CID_FLASH_MODE;
838 if (asd->params.flash_state == ATOMISP_FLASH_ONGOING) {
839 if (frame->flash_state == IA_CSS_FRAME_FLASH_STATE_PARTIAL) {
840 asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_PARTIAL;
841 dev_dbg(isp->dev, "%s partially flashed\n", __func__);
842 } else if (frame->flash_state == IA_CSS_FRAME_FLASH_STATE_FULL) {
843 asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
844 asd->params.num_flash_frames--;
845 dev_dbg(isp->dev, "%s completely flashed\n", __func__);
847 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
848 dev_dbg(isp->dev, "%s no flash in this frame\n", __func__);
851 /* Check if flashing sequence is done */
852 if (asd->frame_status[i] == ATOMISP_FRAME_STATUS_FLASH_EXPOSED)
853 asd->params.flash_state = ATOMISP_FLASH_DONE;
854 } else if (isp->flash) {
855 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) == 0 &&
856 ctrl.value == ATOMISP_FLASH_MODE_TORCH) {
857 ctrl.id = V4L2_CID_FLASH_TORCH_INTENSITY;
858 if (v4l2_g_ctrl(isp->flash->ctrl_handler, &ctrl) == 0 &&
860 asd->frame_status[i] = ATOMISP_FRAME_STATUS_FLASH_EXPOSED;
862 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
864 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
867 asd->frame_status[i] = ATOMISP_FRAME_STATUS_OK;
870 asd->params.last_frame_status = asd->frame_status[i];
872 if (asd->params.css_update_params_needed) {
873 atomisp_apply_css_parameters(asd,
874 &asd->params.css_param);
875 if (asd->params.css_param.update_flag.dz_config)
876 asd->params.config.dz_config = &asd->params.css_param.dz_config;
877 /* New global dvs 6axis config should be blocked
878 * here if there's a buffer with per-frame parameters
879 * pending in CSS frame buffer queue.
880 * This is to aviod zooming vibration since global
881 * parameters take effect immediately while
882 * per-frame parameters are taken after previous
883 * buffers in CSS got processed.
885 if (asd->params.dvs_6axis)
886 atomisp_css_set_dvs_6axis(asd,
887 asd->params.dvs_6axis);
889 asd->params.css_update_params_needed = false;
890 /* The update flag should not be cleaned here
891 * since it is still going to be used to make up
892 * following per-frame parameters.
893 * This will introduce more copy work since each
894 * time when updating global parameters, the whole
895 * parameter set are applied.
896 * FIXME: A new set of parameter copy functions can
897 * be added to make up per-frame parameters based on
898 * solid structures stored in asd->params.css_param
899 * instead of using shadow pointers in update flag.
901 atomisp_css_update_isp_params(asd);
908 spin_lock_irqsave(&pipe->irq_lock, irqflags);
909 atomisp_buffer_done(frame, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
910 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
914 * Requeue should only be done for 3a and dis buffers.
915 * Queue/dequeue order will change if driver recycles image buffers.
918 err = atomisp_css_queue_buffer(asd,
919 stream_id, css_pipe_id,
922 dev_err(isp->dev, "%s, q to css fails: %d\n",
926 if (!error && q_buffers)
927 atomisp_qbuffers_to_css(asd);
930 void atomisp_assert_recovery_work(struct work_struct *work)
932 struct atomisp_device *isp = container_of(work, struct atomisp_device,
933 assert_recovery_work);
934 struct pci_dev *pdev = to_pci_dev(isp->dev);
938 mutex_lock(&isp->mutex);
940 if (!isp->asd.streaming)
943 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
945 spin_lock_irqsave(&isp->lock, flags);
946 isp->asd.streaming = false;
947 spin_unlock_irqrestore(&isp->lock, flags);
949 /* stream off sensor */
950 ret = v4l2_subdev_call(isp->inputs[isp->asd.input_curr].camera, video, s_stream, 0);
952 dev_warn(isp->dev, "Stopping sensor stream failed: %d\n", ret);
954 atomisp_clear_css_buffer_counters(&isp->asd);
956 atomisp_css_stop(&isp->asd, true);
958 isp->asd.preview_exp_id = 1;
959 isp->asd.postview_exp_id = 1;
960 /* notify HAL the CSS reset */
961 dev_dbg(isp->dev, "send reset event to %s\n", isp->asd.subdev.devnode->name);
962 atomisp_reset_event(&isp->asd);
965 disable_isp_irq(hrt_isp_css_irq_sp);
966 clear_isp_irq(hrt_isp_css_irq_sp);
968 /* Set the SRSE to 3 before resetting */
969 pci_write_config_dword(pdev, PCI_I_CONTROL,
970 isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
972 /* reset ISP and restore its state */
975 atomisp_css_input_set_mode(&isp->asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
977 /* Recreate streams destroyed by atomisp_css_stop() */
978 atomisp_create_pipes_stream(&isp->asd);
980 /* Invalidate caches. FIXME: should flush only necessary buffers */
983 if (atomisp_css_start(&isp->asd)) {
984 dev_warn(isp->dev, "start SP failed, so do not set streaming to be enable!\n");
986 spin_lock_irqsave(&isp->lock, flags);
987 isp->asd.streaming = true;
988 spin_unlock_irqrestore(&isp->lock, flags);
991 atomisp_csi2_configure(&isp->asd);
993 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
994 atomisp_css_valid_sof(isp));
996 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, true) < 0)
997 dev_dbg(isp->dev, "DFS auto failed while recovering!\n");
999 /* Dequeueing buffers is not needed, CSS will recycle buffers that it has */
1000 atomisp_flush_video_pipe(&isp->asd.video_out, VB2_BUF_STATE_ERROR, false);
1002 /* Requeue unprocessed per-frame parameters. */
1003 atomisp_recover_params_queue(&isp->asd.video_out);
1005 ret = v4l2_subdev_call(isp->inputs[isp->asd.input_curr].camera, video, s_stream, 1);
1007 dev_err(isp->dev, "Starting sensor stream failed: %d\n", ret);
1010 mutex_unlock(&isp->mutex);
1013 void atomisp_setup_flash(struct atomisp_sub_device *asd)
1015 struct atomisp_device *isp = asd->isp;
1016 struct v4l2_control ctrl;
1021 if (asd->params.flash_state != ATOMISP_FLASH_REQUESTED &&
1022 asd->params.flash_state != ATOMISP_FLASH_DONE)
1025 if (asd->params.num_flash_frames) {
1026 /* make sure the timeout is set before setting flash mode */
1027 ctrl.id = V4L2_CID_FLASH_TIMEOUT;
1028 ctrl.value = FLASH_TIMEOUT;
1030 if (v4l2_s_ctrl(NULL, isp->flash->ctrl_handler, &ctrl)) {
1031 dev_err(isp->dev, "flash timeout configure failed\n");
1035 ia_css_stream_request_flash(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream);
1037 asd->params.flash_state = ATOMISP_FLASH_ONGOING;
1039 asd->params.flash_state = ATOMISP_FLASH_IDLE;
1043 irqreturn_t atomisp_isr_thread(int irq, void *isp_ptr)
1045 struct atomisp_device *isp = isp_ptr;
1046 unsigned long flags;
1048 dev_dbg(isp->dev, ">%s\n", __func__);
1050 spin_lock_irqsave(&isp->lock, flags);
1052 if (!isp->asd.streaming) {
1053 spin_unlock_irqrestore(&isp->lock, flags);
1057 spin_unlock_irqrestore(&isp->lock, flags);
1060 * The standard CSS2.0 API tells the following calling sequence of
1061 * dequeue ready buffers:
1062 * while (ia_css_dequeue_psys_event(...)) {
1063 * switch (event.type) {
1065 * ia_css_pipe_dequeue_buffer()
1068 * That is, dequeue event and buffer are one after another.
1070 * But the following implementation is to first deuque all the event
1071 * to a FIFO, then process the event in the FIFO.
1072 * This will not have issue in single stream mode, but it do have some
1073 * issue in multiple stream case. The issue is that
1074 * ia_css_pipe_dequeue_buffer() will not return the corrent buffer in
1077 * This is due to ia_css_pipe_dequeue_buffer() does not take the
1078 * ia_css_pipe parameter.
1081 * For CSS2.0: we change the way to not dequeue all the event at one
1082 * time, instead, dequue one and process one, then another
1084 mutex_lock(&isp->mutex);
1085 if (atomisp_css_isr_thread(isp))
1088 if (isp->asd.streaming)
1089 atomisp_setup_flash(&isp->asd);
1091 mutex_unlock(&isp->mutex);
1092 dev_dbg(isp->dev, "<%s\n", __func__);
1098 * Get internal fmt according to V4L2 fmt
1100 static enum ia_css_frame_format
1101 v4l2_fmt_to_sh_fmt(u32 fmt)
1104 case V4L2_PIX_FMT_YUV420:
1105 return IA_CSS_FRAME_FORMAT_YUV420;
1106 case V4L2_PIX_FMT_YVU420:
1107 return IA_CSS_FRAME_FORMAT_YV12;
1108 case V4L2_PIX_FMT_YUV422P:
1109 return IA_CSS_FRAME_FORMAT_YUV422;
1110 case V4L2_PIX_FMT_YUV444:
1111 return IA_CSS_FRAME_FORMAT_YUV444;
1112 case V4L2_PIX_FMT_NV12:
1113 return IA_CSS_FRAME_FORMAT_NV12;
1114 case V4L2_PIX_FMT_NV21:
1115 return IA_CSS_FRAME_FORMAT_NV21;
1116 case V4L2_PIX_FMT_NV16:
1117 return IA_CSS_FRAME_FORMAT_NV16;
1118 case V4L2_PIX_FMT_NV61:
1119 return IA_CSS_FRAME_FORMAT_NV61;
1120 case V4L2_PIX_FMT_UYVY:
1121 return IA_CSS_FRAME_FORMAT_UYVY;
1122 case V4L2_PIX_FMT_YUYV:
1123 return IA_CSS_FRAME_FORMAT_YUYV;
1124 case V4L2_PIX_FMT_RGB24:
1125 return IA_CSS_FRAME_FORMAT_PLANAR_RGB888;
1126 case V4L2_PIX_FMT_RGB32:
1127 return IA_CSS_FRAME_FORMAT_RGBA888;
1128 case V4L2_PIX_FMT_RGB565:
1129 return IA_CSS_FRAME_FORMAT_RGB565;
1131 case V4L2_PIX_FMT_JPEG:
1132 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1133 return IA_CSS_FRAME_FORMAT_BINARY_8;
1135 case V4L2_PIX_FMT_SBGGR16:
1136 case V4L2_PIX_FMT_SBGGR10:
1137 case V4L2_PIX_FMT_SGBRG10:
1138 case V4L2_PIX_FMT_SGRBG10:
1139 case V4L2_PIX_FMT_SRGGB10:
1140 case V4L2_PIX_FMT_SBGGR12:
1141 case V4L2_PIX_FMT_SGBRG12:
1142 case V4L2_PIX_FMT_SGRBG12:
1143 case V4L2_PIX_FMT_SRGGB12:
1144 case V4L2_PIX_FMT_SBGGR8:
1145 case V4L2_PIX_FMT_SGBRG8:
1146 case V4L2_PIX_FMT_SGRBG8:
1147 case V4L2_PIX_FMT_SRGGB8:
1148 return IA_CSS_FRAME_FORMAT_RAW;
1155 * raw format match between SH format and V4L2 format
1157 static int raw_output_format_match_input(u32 input, u32 output)
1159 if ((input == ATOMISP_INPUT_FORMAT_RAW_12) &&
1160 ((output == V4L2_PIX_FMT_SRGGB12) ||
1161 (output == V4L2_PIX_FMT_SGRBG12) ||
1162 (output == V4L2_PIX_FMT_SBGGR12) ||
1163 (output == V4L2_PIX_FMT_SGBRG12)))
1166 if ((input == ATOMISP_INPUT_FORMAT_RAW_10) &&
1167 ((output == V4L2_PIX_FMT_SRGGB10) ||
1168 (output == V4L2_PIX_FMT_SGRBG10) ||
1169 (output == V4L2_PIX_FMT_SBGGR10) ||
1170 (output == V4L2_PIX_FMT_SGBRG10)))
1173 if ((input == ATOMISP_INPUT_FORMAT_RAW_8) &&
1174 ((output == V4L2_PIX_FMT_SRGGB8) ||
1175 (output == V4L2_PIX_FMT_SGRBG8) ||
1176 (output == V4L2_PIX_FMT_SBGGR8) ||
1177 (output == V4L2_PIX_FMT_SGBRG8)))
1180 if ((input == ATOMISP_INPUT_FORMAT_RAW_16) && (output == V4L2_PIX_FMT_SBGGR16))
1186 u32 atomisp_get_pixel_depth(u32 pixelformat)
1188 switch (pixelformat) {
1189 case V4L2_PIX_FMT_YUV420:
1190 case V4L2_PIX_FMT_NV12:
1191 case V4L2_PIX_FMT_NV21:
1192 case V4L2_PIX_FMT_YVU420:
1194 case V4L2_PIX_FMT_YUV422P:
1195 case V4L2_PIX_FMT_YUYV:
1196 case V4L2_PIX_FMT_UYVY:
1197 case V4L2_PIX_FMT_NV16:
1198 case V4L2_PIX_FMT_NV61:
1199 case V4L2_PIX_FMT_RGB565:
1200 case V4L2_PIX_FMT_SBGGR16:
1201 case V4L2_PIX_FMT_SBGGR12:
1202 case V4L2_PIX_FMT_SGBRG12:
1203 case V4L2_PIX_FMT_SGRBG12:
1204 case V4L2_PIX_FMT_SRGGB12:
1205 case V4L2_PIX_FMT_SBGGR10:
1206 case V4L2_PIX_FMT_SGBRG10:
1207 case V4L2_PIX_FMT_SGRBG10:
1208 case V4L2_PIX_FMT_SRGGB10:
1210 case V4L2_PIX_FMT_RGB24:
1211 case V4L2_PIX_FMT_YUV444:
1213 case V4L2_PIX_FMT_RGB32:
1215 case V4L2_PIX_FMT_JPEG:
1216 case V4L2_PIX_FMT_CUSTOM_M10MO_RAW:
1217 case V4L2_PIX_FMT_SBGGR8:
1218 case V4L2_PIX_FMT_SGBRG8:
1219 case V4L2_PIX_FMT_SGRBG8:
1220 case V4L2_PIX_FMT_SRGGB8:
1223 return 8 * 2; /* raw type now */
1227 bool atomisp_is_mbuscode_raw(uint32_t code)
1229 return code >= 0x3000 && code < 0x4000;
1233 * ISP features control function
1237 * Set ISP capture mode based on current settings
1239 static void atomisp_update_capture_mode(struct atomisp_sub_device *asd)
1241 if (asd->params.gdc_cac_en)
1242 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_ADVANCED);
1243 else if (asd->params.low_light)
1244 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_LOW_LIGHT);
1245 else if (asd->video_out.sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
1246 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
1248 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_PRIMARY);
1252 int atomisp_set_sensor_runmode(struct atomisp_sub_device *asd,
1253 struct atomisp_s_runmode *runmode)
1255 struct atomisp_device *isp = asd->isp;
1256 struct v4l2_ctrl *c;
1259 if (!(runmode && (runmode->mode & RUNMODE_MASK)))
1262 mutex_lock(asd->ctrl_handler.lock);
1263 c = v4l2_ctrl_find(isp->inputs[asd->input_curr].camera->ctrl_handler,
1267 ret = v4l2_ctrl_s_ctrl(c, runmode->mode);
1269 mutex_unlock(asd->ctrl_handler.lock);
1274 * Function to enable/disable lens geometry distortion correction (GDC) and
1275 * chromatic aberration correction (CAC)
1277 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag,
1281 *value = asd->params.gdc_cac_en;
1285 asd->params.gdc_cac_en = !!*value;
1286 if (asd->params.gdc_cac_en) {
1287 asd->params.config.morph_table = asd->params.css_param.morph_table;
1289 asd->params.config.morph_table = NULL;
1291 asd->params.css_update_params_needed = true;
1292 atomisp_update_capture_mode(asd);
1297 * Function to enable/disable low light mode including ANR
1299 int atomisp_low_light(struct atomisp_sub_device *asd, int flag,
1303 *value = asd->params.low_light;
1307 asd->params.low_light = (*value != 0);
1308 atomisp_update_capture_mode(asd);
1313 * Function to enable/disable extra noise reduction (XNR) in low light
1316 int atomisp_xnr(struct atomisp_sub_device *asd, int flag,
1320 *xnr_enable = asd->params.xnr_en;
1324 atomisp_css_capture_enable_xnr(asd, !!*xnr_enable);
1330 * Function to configure bayer noise reduction
1332 int atomisp_nr(struct atomisp_sub_device *asd, int flag,
1333 struct atomisp_nr_config *arg)
1336 /* Get nr config from current setup */
1337 if (atomisp_css_get_nr_config(asd, arg))
1340 /* Set nr config to isp parameters */
1341 memcpy(&asd->params.css_param.nr_config, arg,
1342 sizeof(struct ia_css_nr_config));
1343 asd->params.config.nr_config = &asd->params.css_param.nr_config;
1344 asd->params.css_update_params_needed = true;
1350 * Function to configure temporal noise reduction (TNR)
1352 int atomisp_tnr(struct atomisp_sub_device *asd, int flag,
1353 struct atomisp_tnr_config *config)
1355 /* Get tnr config from current setup */
1357 /* Get tnr config from current setup */
1358 if (atomisp_css_get_tnr_config(asd, config))
1361 /* Set tnr config to isp parameters */
1362 memcpy(&asd->params.css_param.tnr_config, config,
1363 sizeof(struct ia_css_tnr_config));
1364 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
1365 asd->params.css_update_params_needed = true;
1372 * Function to configure black level compensation
1374 int atomisp_black_level(struct atomisp_sub_device *asd, int flag,
1375 struct atomisp_ob_config *config)
1378 /* Get ob config from current setup */
1379 if (atomisp_css_get_ob_config(asd, config))
1382 /* Set ob config to isp parameters */
1383 memcpy(&asd->params.css_param.ob_config, config,
1384 sizeof(struct ia_css_ob_config));
1385 asd->params.config.ob_config = &asd->params.css_param.ob_config;
1386 asd->params.css_update_params_needed = true;
1393 * Function to configure edge enhancement
1395 int atomisp_ee(struct atomisp_sub_device *asd, int flag,
1396 struct atomisp_ee_config *config)
1399 /* Get ee config from current setup */
1400 if (atomisp_css_get_ee_config(asd, config))
1403 /* Set ee config to isp parameters */
1404 memcpy(&asd->params.css_param.ee_config, config,
1405 sizeof(asd->params.css_param.ee_config));
1406 asd->params.config.ee_config = &asd->params.css_param.ee_config;
1407 asd->params.css_update_params_needed = true;
1414 * Function to update Gamma table for gamma, brightness and contrast config
1416 int atomisp_gamma(struct atomisp_sub_device *asd, int flag,
1417 struct atomisp_gamma_table *config)
1420 /* Get gamma table from current setup */
1421 if (atomisp_css_get_gamma_table(asd, config))
1424 /* Set gamma table to isp parameters */
1425 memcpy(&asd->params.css_param.gamma_table, config,
1426 sizeof(asd->params.css_param.gamma_table));
1427 asd->params.config.gamma_table = &asd->params.css_param.gamma_table;
1434 * Function to update Ctc table for Chroma Enhancement
1436 int atomisp_ctc(struct atomisp_sub_device *asd, int flag,
1437 struct atomisp_ctc_table *config)
1440 /* Get ctc table from current setup */
1441 if (atomisp_css_get_ctc_table(asd, config))
1444 /* Set ctc table to isp parameters */
1445 memcpy(&asd->params.css_param.ctc_table, config,
1446 sizeof(asd->params.css_param.ctc_table));
1447 atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table);
1454 * Function to update gamma correction parameters
1456 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag,
1457 struct atomisp_gc_config *config)
1460 /* Get gamma correction params from current setup */
1461 if (atomisp_css_get_gc_config(asd, config))
1464 /* Set gamma correction params to isp parameters */
1465 memcpy(&asd->params.css_param.gc_config, config,
1466 sizeof(asd->params.css_param.gc_config));
1467 asd->params.config.gc_config = &asd->params.css_param.gc_config;
1468 asd->params.css_update_params_needed = true;
1475 * Function to update narrow gamma flag
1477 int atomisp_formats(struct atomisp_sub_device *asd, int flag,
1478 struct atomisp_formats_config *config)
1481 /* Get narrow gamma flag from current setup */
1482 if (atomisp_css_get_formats_config(asd, config))
1485 /* Set narrow gamma flag to isp parameters */
1486 memcpy(&asd->params.css_param.formats_config, config,
1487 sizeof(asd->params.css_param.formats_config));
1488 asd->params.config.formats_config = &asd->params.css_param.formats_config;
1494 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd)
1496 atomisp_free_css_parameters(&asd->params.css_param);
1499 static void atomisp_update_grid_info(struct atomisp_sub_device *asd,
1500 enum ia_css_pipe_id pipe_id)
1502 struct atomisp_device *isp = asd->isp;
1505 if (atomisp_css_get_grid_info(asd, pipe_id))
1508 /* We must free all buffers because they no longer match
1510 atomisp_css_free_stat_buffers(asd);
1512 err = atomisp_alloc_css_stat_bufs(asd, ATOMISP_INPUT_STREAM_GENERAL);
1514 dev_err(isp->dev, "stat_buf allocate error\n");
1518 if (atomisp_alloc_3a_output_buf(asd)) {
1519 /* Failure for 3A buffers does not influence DIS buffers */
1520 if (asd->params.s3a_output_bytes != 0) {
1521 /* For SOC sensor happens s3a_output_bytes == 0,
1522 * using if condition to exclude false error log */
1523 dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n");
1528 if (atomisp_alloc_dis_coef_buf(asd)) {
1530 "Failed to allocate memory for DIS statistics\n");
1534 if (atomisp_alloc_metadata_output_buf(asd)) {
1535 dev_err(isp->dev, "Failed to allocate memory for metadata\n");
1542 atomisp_css_free_stat_buffers(asd);
1546 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd,
1547 struct atomisp_grid_info *info)
1549 memcpy(info, &asd->params.curr_grid_info.s3a_grid,
1550 sizeof(struct ia_css_3a_grid_info));
1553 int atomisp_compare_grid(struct atomisp_sub_device *asd,
1554 struct atomisp_grid_info *atomgrid)
1556 struct atomisp_grid_info tmp = {0};
1558 atomisp_curr_user_grid_info(asd, &tmp);
1559 return memcmp(atomgrid, &tmp, sizeof(tmp));
1563 * Function to update Gdc table for gdc
1565 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag,
1566 struct atomisp_morph_table *config)
1570 struct atomisp_device *isp = asd->isp;
1573 /* Get gdc table from current setup */
1574 struct ia_css_morph_table tab = {0};
1576 atomisp_css_get_morph_table(asd, &tab);
1578 config->width = tab.width;
1579 config->height = tab.height;
1581 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1582 ret = copy_to_user(config->coordinates_x[i],
1583 tab.coordinates_x[i], tab.height *
1584 tab.width * sizeof(*tab.coordinates_x[i]));
1587 "Failed to copy to User for x\n");
1590 ret = copy_to_user(config->coordinates_y[i],
1591 tab.coordinates_y[i], tab.height *
1592 tab.width * sizeof(*tab.coordinates_y[i]));
1595 "Failed to copy to User for y\n");
1600 struct ia_css_morph_table *tab =
1601 asd->params.css_param.morph_table;
1603 /* free first if we have one */
1605 atomisp_css_morph_table_free(tab);
1606 asd->params.css_param.morph_table = NULL;
1609 /* allocate new one */
1610 tab = atomisp_css_morph_table_allocate(config->width,
1614 dev_err(isp->dev, "out of memory\n");
1618 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1619 ret = copy_from_user(tab->coordinates_x[i],
1620 config->coordinates_x[i],
1621 config->height * config->width *
1622 sizeof(*config->coordinates_x[i]));
1625 "Failed to copy from User for x, ret %d\n",
1627 atomisp_css_morph_table_free(tab);
1630 ret = copy_from_user(tab->coordinates_y[i],
1631 config->coordinates_y[i],
1632 config->height * config->width *
1633 sizeof(*config->coordinates_y[i]));
1636 "Failed to copy from User for y, ret is %d\n",
1638 atomisp_css_morph_table_free(tab);
1642 asd->params.css_param.morph_table = tab;
1643 if (asd->params.gdc_cac_en)
1644 asd->params.config.morph_table = tab;
1650 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag,
1651 struct atomisp_macc_config *config)
1653 struct ia_css_macc_table *macc_table;
1655 switch (config->color_effect) {
1656 case V4L2_COLORFX_NONE:
1657 macc_table = &asd->params.css_param.macc_table;
1659 case V4L2_COLORFX_SKY_BLUE:
1660 macc_table = &blue_macc_table;
1662 case V4L2_COLORFX_GRASS_GREEN:
1663 macc_table = &green_macc_table;
1665 case V4L2_COLORFX_SKIN_WHITEN_LOW:
1666 macc_table = &skin_low_macc_table;
1668 case V4L2_COLORFX_SKIN_WHITEN:
1669 macc_table = &skin_medium_macc_table;
1671 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
1672 macc_table = &skin_high_macc_table;
1679 /* Get macc table from current setup */
1680 memcpy(&config->table, macc_table,
1681 sizeof(struct ia_css_macc_table));
1683 memcpy(macc_table, &config->table,
1684 sizeof(struct ia_css_macc_table));
1685 if (config->color_effect == asd->params.color_effect)
1686 asd->params.config.macc_table = macc_table;
1692 int atomisp_set_dis_vector(struct atomisp_sub_device *asd,
1693 struct atomisp_dis_vector *vector)
1695 atomisp_css_video_set_dis_vector(asd, vector);
1697 asd->params.dis_proj_data_valid = false;
1698 asd->params.css_update_params_needed = true;
1703 * Function to set/get image stablization statistics
1705 int atomisp_get_dis_stat(struct atomisp_sub_device *asd,
1706 struct atomisp_dis_statistics *stats)
1708 return atomisp_css_get_dis_stat(asd, stats);
1712 * Function set camrea_prefiles.xml current sensor pixel array size
1714 int atomisp_set_array_res(struct atomisp_sub_device *asd,
1715 struct atomisp_resolution *config)
1717 dev_dbg(asd->isp->dev, ">%s start\n", __func__);
1719 dev_err(asd->isp->dev, "Set sensor array size is not valid\n");
1723 asd->sensor_array_res.width = config->width;
1724 asd->sensor_array_res.height = config->height;
1729 * Function to get DVS2 BQ resolution settings
1731 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd,
1732 struct atomisp_dvs2_bq_resolutions *bq_res)
1734 struct ia_css_pipe_config *pipe_cfg = NULL;
1736 struct ia_css_stream *stream =
1737 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
1739 dev_warn(asd->isp->dev, "stream is not created");
1743 pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
1744 .pipe_configs[IA_CSS_PIPE_ID_VIDEO];
1749 /* the GDC output resolution */
1750 bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2;
1751 bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2;
1753 bq_res->envelope_bq.width_bq = 0;
1754 bq_res->envelope_bq.height_bq = 0;
1755 /* the GDC input resolution */
1756 bq_res->source_bq.width_bq = bq_res->output_bq.width_bq +
1757 pipe_cfg->dvs_envelope.width / 2;
1758 bq_res->source_bq.height_bq = bq_res->output_bq.height_bq +
1759 pipe_cfg->dvs_envelope.height / 2;
1761 * Bad pixels caused by spatial filter processing
1762 * ISP filter resolution should be given by CSS/FW, but for now
1763 * there is not such API to query, and it is fixed value, so
1766 bq_res->ispfilter_bq.width_bq = 12 / 2;
1767 bq_res->ispfilter_bq.height_bq = 12 / 2;
1768 /* spatial filter shift, always 4 pixels */
1769 bq_res->gdc_shift_bq.width_bq = 4 / 2;
1770 bq_res->gdc_shift_bq.height_bq = 4 / 2;
1772 if (asd->params.video_dis_en) {
1773 bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width / 2 -
1774 bq_res->ispfilter_bq.width_bq;
1775 bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height / 2 -
1776 bq_res->ispfilter_bq.height_bq;
1779 dev_dbg(asd->isp->dev,
1780 "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",
1781 bq_res->source_bq.width_bq, bq_res->source_bq.height_bq,
1782 bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq,
1783 bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq,
1784 bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq,
1785 bq_res->output_bq.width_bq, bq_res->output_bq.height_bq);
1790 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd,
1791 struct atomisp_dis_coefficients *coefs)
1793 return atomisp_css_set_dis_coefs(asd, coefs);
1797 * Function to set/get 3A stat from isp
1799 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag,
1800 struct atomisp_3a_statistics *config)
1802 struct atomisp_device *isp = asd->isp;
1803 struct atomisp_s3a_buf *s3a_buf;
1809 /* sanity check to avoid writing into unallocated memory. */
1810 if (asd->params.s3a_output_bytes == 0)
1813 if (atomisp_compare_grid(asd, &config->grid_info) != 0) {
1814 /* If the grid info in the argument differs from the current
1815 grid info, we tell the caller to reset the grid size and
1820 if (list_empty(&asd->s3a_stats_ready)) {
1821 dev_err(isp->dev, "3a statistics is not valid.\n");
1825 s3a_buf = list_entry(asd->s3a_stats_ready.next,
1826 struct atomisp_s3a_buf, list);
1827 if (s3a_buf->s3a_map)
1828 ia_css_translate_3a_statistics(
1829 asd->params.s3a_user_stat, s3a_buf->s3a_map);
1831 ia_css_get_3a_statistics(asd->params.s3a_user_stat,
1834 config->exp_id = s3a_buf->s3a_data->exp_id;
1835 config->isp_config_id = s3a_buf->s3a_data->isp_config_id;
1837 ret = copy_to_user(config->data, asd->params.s3a_user_stat->data,
1838 asd->params.s3a_output_bytes);
1840 dev_err(isp->dev, "copy to user failed: copied %lu bytes\n",
1845 /* Move to free buffer list */
1846 list_del_init(&s3a_buf->list);
1847 list_add_tail(&s3a_buf->list, &asd->s3a_stats);
1848 dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n",
1850 config->exp_id, config->isp_config_id);
1854 int atomisp_get_metadata(struct atomisp_sub_device *asd, int flag,
1855 struct atomisp_metadata *md)
1857 struct atomisp_device *isp = asd->isp;
1858 struct ia_css_stream_info *stream_info;
1859 struct camera_mipi_info *mipi_info;
1860 struct atomisp_metadata_buf *md_buf;
1861 enum atomisp_metadata_type md_type = ATOMISP_MAIN_METADATA;
1867 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
1870 /* We always return the resolution and stride even if there is
1871 * no valid metadata. This allows the caller to get the information
1872 * needed to allocate user-space buffers. */
1873 md->width = stream_info->metadata_info.resolution.width;
1874 md->height = stream_info->metadata_info.resolution.height;
1875 md->stride = stream_info->metadata_info.stride;
1877 /* sanity check to avoid writing into unallocated memory.
1878 * This does not return an error because it is a valid way
1879 * for applications to detect that metadata is not enabled. */
1880 if (md->width == 0 || md->height == 0 || !md->data)
1883 /* This is done in the atomisp_buf_done() */
1884 if (list_empty(&asd->metadata_ready[md_type])) {
1885 dev_warn(isp->dev, "Metadata queue is empty now!\n");
1889 mipi_info = atomisp_to_sensor_mipi_info(
1890 isp->inputs[asd->input_curr].camera);
1894 if (mipi_info->metadata_effective_width) {
1895 for (i = 0; i < md->height; i++)
1896 md->effective_width[i] =
1897 mipi_info->metadata_effective_width[i];
1900 md_buf = list_entry(asd->metadata_ready[md_type].next,
1901 struct atomisp_metadata_buf, list);
1902 md->exp_id = md_buf->metadata->exp_id;
1903 if (md_buf->md_vptr) {
1904 ret = copy_to_user(md->data,
1906 stream_info->metadata_info.size);
1908 hmm_load(md_buf->metadata->address,
1909 asd->params.metadata_user[md_type],
1910 stream_info->metadata_info.size);
1912 ret = copy_to_user(md->data,
1913 asd->params.metadata_user[md_type],
1914 stream_info->metadata_info.size);
1917 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
1922 list_del_init(&md_buf->list);
1923 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
1925 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
1926 __func__, md_type, md->exp_id);
1930 int atomisp_get_metadata_by_type(struct atomisp_sub_device *asd, int flag,
1931 struct atomisp_metadata_with_type *md)
1933 struct atomisp_device *isp = asd->isp;
1934 struct ia_css_stream_info *stream_info;
1935 struct camera_mipi_info *mipi_info;
1936 struct atomisp_metadata_buf *md_buf;
1937 enum atomisp_metadata_type md_type;
1943 stream_info = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
1946 /* We always return the resolution and stride even if there is
1947 * no valid metadata. This allows the caller to get the information
1948 * needed to allocate user-space buffers. */
1949 md->width = stream_info->metadata_info.resolution.width;
1950 md->height = stream_info->metadata_info.resolution.height;
1951 md->stride = stream_info->metadata_info.stride;
1953 /* sanity check to avoid writing into unallocated memory.
1954 * This does not return an error because it is a valid way
1955 * for applications to detect that metadata is not enabled. */
1956 if (md->width == 0 || md->height == 0 || !md->data)
1960 if (md_type < 0 || md_type >= ATOMISP_METADATA_TYPE_NUM)
1963 /* This is done in the atomisp_buf_done() */
1964 if (list_empty(&asd->metadata_ready[md_type])) {
1965 dev_warn(isp->dev, "Metadata queue is empty now!\n");
1969 mipi_info = atomisp_to_sensor_mipi_info(
1970 isp->inputs[asd->input_curr].camera);
1974 if (mipi_info->metadata_effective_width) {
1975 for (i = 0; i < md->height; i++)
1976 md->effective_width[i] =
1977 mipi_info->metadata_effective_width[i];
1980 md_buf = list_entry(asd->metadata_ready[md_type].next,
1981 struct atomisp_metadata_buf, list);
1982 md->exp_id = md_buf->metadata->exp_id;
1983 if (md_buf->md_vptr) {
1984 ret = copy_to_user(md->data,
1986 stream_info->metadata_info.size);
1988 hmm_load(md_buf->metadata->address,
1989 asd->params.metadata_user[md_type],
1990 stream_info->metadata_info.size);
1992 ret = copy_to_user(md->data,
1993 asd->params.metadata_user[md_type],
1994 stream_info->metadata_info.size);
1997 dev_err(isp->dev, "copy to user failed: copied %d bytes\n",
2001 list_del_init(&md_buf->list);
2002 list_add_tail(&md_buf->list, &asd->metadata[md_type]);
2004 dev_dbg(isp->dev, "%s: HAL de-queued metadata type %d with exp_id %d\n",
2005 __func__, md_type, md->exp_id);
2010 * Function to calculate real zoom region for every pipe
2012 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd,
2013 struct ia_css_dz_config *dz_config,
2014 enum ia_css_pipe_id css_pipe_id)
2017 struct atomisp_stream_env *stream_env =
2018 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL];
2019 struct atomisp_resolution eff_res, out_res;
2020 int w_offset, h_offset;
2022 memset(&eff_res, 0, sizeof(eff_res));
2023 memset(&out_res, 0, sizeof(out_res));
2025 if (dz_config->dx || dz_config->dy)
2028 if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW
2029 && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) {
2030 dev_err(asd->isp->dev, "%s the set pipe no support crop region"
2036 stream_env->stream_config.input_config.effective_res.width;
2038 stream_env->stream_config.input_config.effective_res.height;
2039 if (eff_res.width == 0 || eff_res.height == 0) {
2040 dev_err(asd->isp->dev, "%s err effective resolution"
2045 if (dz_config->zoom_region.resolution.width
2046 == asd->sensor_array_res.width
2047 || dz_config->zoom_region.resolution.height
2048 == asd->sensor_array_res.height) {
2049 /*no need crop region*/
2050 dz_config->zoom_region.origin.x = 0;
2051 dz_config->zoom_region.origin.y = 0;
2052 dz_config->zoom_region.resolution.width = eff_res.width;
2053 dz_config->zoom_region.resolution.height = eff_res.height;
2058 * This is not the correct implementation with Google's definition, due
2059 * to firmware limitation.
2060 * map real crop region base on above calculating base max crop region.
2064 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
2066 / asd->sensor_array_res.width;
2067 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
2069 / asd->sensor_array_res.height;
2070 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
2072 / asd->sensor_array_res.width;
2073 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
2075 / asd->sensor_array_res.height;
2077 * Set same ratio of crop region resolution and current pipe output
2080 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
2081 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
2082 if (out_res.width == 0 || out_res.height == 0) {
2083 dev_err(asd->isp->dev, "%s err current pipe output resolution"
2088 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
2089 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
2090 if (out_res.width == 0 || out_res.height == 0) {
2091 dev_err(asd->isp->dev, "%s err current pipe output resolution"
2096 if (asd->sensor_array_res.width * out_res.height
2097 < out_res.width * asd->sensor_array_res.height) {
2098 h_offset = asd->sensor_array_res.height
2099 - asd->sensor_array_res.width
2100 * out_res.height / out_res.width;
2101 h_offset = h_offset / 2;
2102 if (dz_config->zoom_region.origin.y < h_offset)
2103 dz_config->zoom_region.origin.y = 0;
2105 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset;
2108 w_offset = asd->sensor_array_res.width
2109 - asd->sensor_array_res.height
2110 * out_res.width / out_res.height;
2111 w_offset = w_offset / 2;
2112 if (dz_config->zoom_region.origin.x < w_offset)
2113 dz_config->zoom_region.origin.x = 0;
2115 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset;
2118 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
2120 / (asd->sensor_array_res.width - 2 * w_offset);
2121 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
2123 / (asd->sensor_array_res.height - 2 * h_offset);
2124 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
2126 / (asd->sensor_array_res.width - 2 * w_offset);
2127 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
2129 / (asd->sensor_array_res.height - 2 * h_offset);
2132 if (out_res.width * dz_config->zoom_region.resolution.height
2133 > dz_config->zoom_region.resolution.width * out_res.height) {
2134 dz_config->zoom_region.resolution.height =
2135 dz_config->zoom_region.resolution.width
2136 * out_res.height / out_res.width;
2138 dz_config->zoom_region.resolution.width =
2139 dz_config->zoom_region.resolution.height
2140 * out_res.width / out_res.height;
2142 dev_dbg(asd->isp->dev,
2143 "%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n",
2144 __func__, dz_config->zoom_region.origin.x,
2145 dz_config->zoom_region.origin.y,
2146 dz_config->zoom_region.resolution.width,
2147 dz_config->zoom_region.resolution.height,
2148 eff_res.width, eff_res.height,
2149 asd->sensor_array_res.width,
2150 asd->sensor_array_res.height,
2151 out_res.width, out_res.height);
2153 if ((dz_config->zoom_region.origin.x +
2154 dz_config->zoom_region.resolution.width
2156 (dz_config->zoom_region.origin.y +
2157 dz_config->zoom_region.resolution.height
2165 * Function to check the zoom region whether is effective
2167 static bool atomisp_check_zoom_region(
2168 struct atomisp_sub_device *asd,
2169 struct ia_css_dz_config *dz_config)
2171 struct atomisp_resolution config;
2175 memset(&config, 0, sizeof(struct atomisp_resolution));
2177 if (dz_config->dx && dz_config->dy)
2180 config.width = asd->sensor_array_res.width;
2181 config.height = asd->sensor_array_res.height;
2182 w = dz_config->zoom_region.origin.x +
2183 dz_config->zoom_region.resolution.width;
2184 h = dz_config->zoom_region.origin.y +
2185 dz_config->zoom_region.resolution.height;
2187 if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0)
2190 /* setting error zoom region */
2191 dev_err(asd->isp->dev,
2192 "%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n",
2193 __func__, dz_config->zoom_region.origin.x,
2194 dz_config->zoom_region.origin.y,
2195 dz_config->zoom_region.resolution.width,
2196 dz_config->zoom_region.resolution.height,
2197 config.width, config.height);
2202 void atomisp_apply_css_parameters(
2203 struct atomisp_sub_device *asd,
2204 struct atomisp_css_params *css_param)
2206 if (css_param->update_flag.wb_config)
2207 asd->params.config.wb_config = &css_param->wb_config;
2209 if (css_param->update_flag.ob_config)
2210 asd->params.config.ob_config = &css_param->ob_config;
2212 if (css_param->update_flag.dp_config)
2213 asd->params.config.dp_config = &css_param->dp_config;
2215 if (css_param->update_flag.nr_config)
2216 asd->params.config.nr_config = &css_param->nr_config;
2218 if (css_param->update_flag.ee_config)
2219 asd->params.config.ee_config = &css_param->ee_config;
2221 if (css_param->update_flag.tnr_config)
2222 asd->params.config.tnr_config = &css_param->tnr_config;
2224 if (css_param->update_flag.a3a_config)
2225 asd->params.config.s3a_config = &css_param->s3a_config;
2227 if (css_param->update_flag.ctc_config)
2228 asd->params.config.ctc_config = &css_param->ctc_config;
2230 if (css_param->update_flag.cnr_config)
2231 asd->params.config.cnr_config = &css_param->cnr_config;
2233 if (css_param->update_flag.ecd_config)
2234 asd->params.config.ecd_config = &css_param->ecd_config;
2236 if (css_param->update_flag.ynr_config)
2237 asd->params.config.ynr_config = &css_param->ynr_config;
2239 if (css_param->update_flag.fc_config)
2240 asd->params.config.fc_config = &css_param->fc_config;
2242 if (css_param->update_flag.macc_config)
2243 asd->params.config.macc_config = &css_param->macc_config;
2245 if (css_param->update_flag.aa_config)
2246 asd->params.config.aa_config = &css_param->aa_config;
2248 if (css_param->update_flag.anr_config)
2249 asd->params.config.anr_config = &css_param->anr_config;
2251 if (css_param->update_flag.xnr_config)
2252 asd->params.config.xnr_config = &css_param->xnr_config;
2254 if (css_param->update_flag.yuv2rgb_cc_config)
2255 asd->params.config.yuv2rgb_cc_config = &css_param->yuv2rgb_cc_config;
2257 if (css_param->update_flag.rgb2yuv_cc_config)
2258 asd->params.config.rgb2yuv_cc_config = &css_param->rgb2yuv_cc_config;
2260 if (css_param->update_flag.macc_table)
2261 asd->params.config.macc_table = &css_param->macc_table;
2263 if (css_param->update_flag.xnr_table)
2264 asd->params.config.xnr_table = &css_param->xnr_table;
2266 if (css_param->update_flag.r_gamma_table)
2267 asd->params.config.r_gamma_table = &css_param->r_gamma_table;
2269 if (css_param->update_flag.g_gamma_table)
2270 asd->params.config.g_gamma_table = &css_param->g_gamma_table;
2272 if (css_param->update_flag.b_gamma_table)
2273 asd->params.config.b_gamma_table = &css_param->b_gamma_table;
2275 if (css_param->update_flag.anr_thres)
2276 atomisp_css_set_anr_thres(asd, &css_param->anr_thres);
2278 if (css_param->update_flag.shading_table)
2279 asd->params.config.shading_table = css_param->shading_table;
2281 if (css_param->update_flag.morph_table && asd->params.gdc_cac_en)
2282 asd->params.config.morph_table = css_param->morph_table;
2284 if (css_param->update_flag.dvs2_coefs) {
2285 struct ia_css_dvs_grid_info *dvs_grid_info =
2286 atomisp_css_get_dvs_grid_info(
2287 &asd->params.curr_grid_info);
2289 if (dvs_grid_info && dvs_grid_info->enable)
2290 atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff);
2293 if (css_param->update_flag.dvs_6axis_config)
2294 atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis);
2296 atomisp_css_set_isp_config_id(asd, css_param->isp_config_id);
2298 * These configurations are on used by ISP1.x, not for ISP2.x,
2299 * so do not handle them. see comments of ia_css_isp_config.
2310 static unsigned int long copy_from_compatible(void *to, const void *from,
2311 unsigned long n, bool from_user)
2314 return copy_from_user(to, (void __user *)from, n);
2316 memcpy(to, from, n);
2320 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd,
2321 struct atomisp_parameters *arg,
2322 struct atomisp_css_params *css_param,
2325 struct atomisp_parameters *cur_config = &css_param->update_flag;
2327 if (!arg || !asd || !css_param)
2330 if (arg->wb_config && (from_user || !cur_config->wb_config)) {
2331 if (copy_from_compatible(&css_param->wb_config, arg->wb_config,
2332 sizeof(struct ia_css_wb_config),
2335 css_param->update_flag.wb_config =
2336 (struct atomisp_wb_config *)&css_param->wb_config;
2339 if (arg->ob_config && (from_user || !cur_config->ob_config)) {
2340 if (copy_from_compatible(&css_param->ob_config, arg->ob_config,
2341 sizeof(struct ia_css_ob_config),
2344 css_param->update_flag.ob_config =
2345 (struct atomisp_ob_config *)&css_param->ob_config;
2348 if (arg->dp_config && (from_user || !cur_config->dp_config)) {
2349 if (copy_from_compatible(&css_param->dp_config, arg->dp_config,
2350 sizeof(struct ia_css_dp_config),
2353 css_param->update_flag.dp_config =
2354 (struct atomisp_dp_config *)&css_param->dp_config;
2357 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
2358 if (arg->dz_config && (from_user || !cur_config->dz_config)) {
2359 if (copy_from_compatible(&css_param->dz_config,
2361 sizeof(struct ia_css_dz_config),
2364 if (!atomisp_check_zoom_region(asd,
2365 &css_param->dz_config)) {
2366 dev_err(asd->isp->dev, "crop region error!");
2369 css_param->update_flag.dz_config =
2370 (struct atomisp_dz_config *)
2371 &css_param->dz_config;
2375 if (arg->nr_config && (from_user || !cur_config->nr_config)) {
2376 if (copy_from_compatible(&css_param->nr_config, arg->nr_config,
2377 sizeof(struct ia_css_nr_config),
2380 css_param->update_flag.nr_config =
2381 (struct atomisp_nr_config *)&css_param->nr_config;
2384 if (arg->ee_config && (from_user || !cur_config->ee_config)) {
2385 if (copy_from_compatible(&css_param->ee_config, arg->ee_config,
2386 sizeof(struct ia_css_ee_config),
2389 css_param->update_flag.ee_config =
2390 (struct atomisp_ee_config *)&css_param->ee_config;
2393 if (arg->tnr_config && (from_user || !cur_config->tnr_config)) {
2394 if (copy_from_compatible(&css_param->tnr_config,
2396 sizeof(struct ia_css_tnr_config),
2399 css_param->update_flag.tnr_config =
2400 (struct atomisp_tnr_config *)
2401 &css_param->tnr_config;
2404 if (arg->a3a_config && (from_user || !cur_config->a3a_config)) {
2405 if (copy_from_compatible(&css_param->s3a_config,
2407 sizeof(struct ia_css_3a_config),
2410 css_param->update_flag.a3a_config =
2411 (struct atomisp_3a_config *)&css_param->s3a_config;
2414 if (arg->ctc_config && (from_user || !cur_config->ctc_config)) {
2415 if (copy_from_compatible(&css_param->ctc_config,
2417 sizeof(struct ia_css_ctc_config),
2420 css_param->update_flag.ctc_config =
2421 (struct atomisp_ctc_config *)
2422 &css_param->ctc_config;
2425 if (arg->cnr_config && (from_user || !cur_config->cnr_config)) {
2426 if (copy_from_compatible(&css_param->cnr_config,
2428 sizeof(struct ia_css_cnr_config),
2431 css_param->update_flag.cnr_config =
2432 (struct atomisp_cnr_config *)
2433 &css_param->cnr_config;
2436 if (arg->ecd_config && (from_user || !cur_config->ecd_config)) {
2437 if (copy_from_compatible(&css_param->ecd_config,
2439 sizeof(struct ia_css_ecd_config),
2442 css_param->update_flag.ecd_config =
2443 (struct atomisp_ecd_config *)
2444 &css_param->ecd_config;
2447 if (arg->ynr_config && (from_user || !cur_config->ynr_config)) {
2448 if (copy_from_compatible(&css_param->ynr_config,
2450 sizeof(struct ia_css_ynr_config),
2453 css_param->update_flag.ynr_config =
2454 (struct atomisp_ynr_config *)
2455 &css_param->ynr_config;
2458 if (arg->fc_config && (from_user || !cur_config->fc_config)) {
2459 if (copy_from_compatible(&css_param->fc_config,
2461 sizeof(struct ia_css_fc_config),
2464 css_param->update_flag.fc_config =
2465 (struct atomisp_fc_config *)&css_param->fc_config;
2468 if (arg->macc_config && (from_user || !cur_config->macc_config)) {
2469 if (copy_from_compatible(&css_param->macc_config,
2471 sizeof(struct ia_css_macc_config),
2474 css_param->update_flag.macc_config =
2475 (struct atomisp_macc_config *)
2476 &css_param->macc_config;
2479 if (arg->aa_config && (from_user || !cur_config->aa_config)) {
2480 if (copy_from_compatible(&css_param->aa_config, arg->aa_config,
2481 sizeof(struct ia_css_aa_config),
2484 css_param->update_flag.aa_config =
2485 (struct atomisp_aa_config *)&css_param->aa_config;
2488 if (arg->anr_config && (from_user || !cur_config->anr_config)) {
2489 if (copy_from_compatible(&css_param->anr_config,
2491 sizeof(struct ia_css_anr_config),
2494 css_param->update_flag.anr_config =
2495 (struct atomisp_anr_config *)
2496 &css_param->anr_config;
2499 if (arg->xnr_config && (from_user || !cur_config->xnr_config)) {
2500 if (copy_from_compatible(&css_param->xnr_config,
2502 sizeof(struct ia_css_xnr_config),
2505 css_param->update_flag.xnr_config =
2506 (struct atomisp_xnr_config *)
2507 &css_param->xnr_config;
2510 if (arg->yuv2rgb_cc_config &&
2511 (from_user || !cur_config->yuv2rgb_cc_config)) {
2512 if (copy_from_compatible(&css_param->yuv2rgb_cc_config,
2513 arg->yuv2rgb_cc_config,
2514 sizeof(struct ia_css_cc_config),
2517 css_param->update_flag.yuv2rgb_cc_config =
2518 (struct atomisp_cc_config *)
2519 &css_param->yuv2rgb_cc_config;
2522 if (arg->rgb2yuv_cc_config &&
2523 (from_user || !cur_config->rgb2yuv_cc_config)) {
2524 if (copy_from_compatible(&css_param->rgb2yuv_cc_config,
2525 arg->rgb2yuv_cc_config,
2526 sizeof(struct ia_css_cc_config),
2529 css_param->update_flag.rgb2yuv_cc_config =
2530 (struct atomisp_cc_config *)
2531 &css_param->rgb2yuv_cc_config;
2534 if (arg->macc_table && (from_user || !cur_config->macc_table)) {
2535 if (copy_from_compatible(&css_param->macc_table,
2537 sizeof(struct ia_css_macc_table),
2540 css_param->update_flag.macc_table =
2541 (struct atomisp_macc_table *)
2542 &css_param->macc_table;
2545 if (arg->xnr_table && (from_user || !cur_config->xnr_table)) {
2546 if (copy_from_compatible(&css_param->xnr_table,
2548 sizeof(struct ia_css_xnr_table),
2551 css_param->update_flag.xnr_table =
2552 (struct atomisp_xnr_table *)&css_param->xnr_table;
2555 if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) {
2556 if (copy_from_compatible(&css_param->r_gamma_table,
2558 sizeof(struct ia_css_rgb_gamma_table),
2561 css_param->update_flag.r_gamma_table =
2562 (struct atomisp_rgb_gamma_table *)
2563 &css_param->r_gamma_table;
2566 if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) {
2567 if (copy_from_compatible(&css_param->g_gamma_table,
2569 sizeof(struct ia_css_rgb_gamma_table),
2572 css_param->update_flag.g_gamma_table =
2573 (struct atomisp_rgb_gamma_table *)
2574 &css_param->g_gamma_table;
2577 if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) {
2578 if (copy_from_compatible(&css_param->b_gamma_table,
2580 sizeof(struct ia_css_rgb_gamma_table),
2583 css_param->update_flag.b_gamma_table =
2584 (struct atomisp_rgb_gamma_table *)
2585 &css_param->b_gamma_table;
2588 if (arg->anr_thres && (from_user || !cur_config->anr_thres)) {
2589 if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres,
2590 sizeof(struct ia_css_anr_thres),
2593 css_param->update_flag.anr_thres =
2594 (struct atomisp_anr_thres *)&css_param->anr_thres;
2598 css_param->isp_config_id = arg->isp_config_id;
2600 * These configurations are on used by ISP1.x, not for ISP2.x,
2601 * so do not handle them. see comments of ia_css_isp_config.
2613 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd,
2614 struct atomisp_shading_table *source_st,
2615 struct atomisp_css_params *css_param,
2619 unsigned int len_table;
2620 struct ia_css_shading_table *shading_table;
2621 struct ia_css_shading_table *old_table;
2622 struct atomisp_shading_table *st, dest_st;
2630 if (!from_user && css_param->update_flag.shading_table)
2634 if (copy_from_compatible(&dest_st, source_st,
2635 sizeof(struct atomisp_shading_table),
2637 dev_err(asd->isp->dev, "copy shading table failed!");
2645 old_table = css_param->shading_table;
2647 /* user config is to disable the shading table. */
2649 /* Generate a minimum table with enable = 0. */
2650 shading_table = atomisp_css_shading_table_alloc(1, 1);
2653 shading_table->enable = 0;
2657 /* Setting a new table. Validate first - all tables must be set */
2658 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
2660 dev_err(asd->isp->dev, "shading table validate failed");
2665 /* Shading table size per color */
2666 if (st->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
2667 st->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
2668 dev_err(asd->isp->dev, "shading table w/h validate failed!");
2672 shading_table = atomisp_css_shading_table_alloc(st->width, st->height);
2676 len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE;
2677 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
2678 if (copy_from_compatible(shading_table->data[i],
2679 st->data[i], len_table, from_user)) {
2680 atomisp_css_shading_table_free(shading_table);
2684 shading_table->sensor_width = st->sensor_width;
2685 shading_table->sensor_height = st->sensor_height;
2686 shading_table->fraction_bits = st->fraction_bits;
2687 shading_table->enable = st->enable;
2689 /* No need to update shading table if it is the same */
2691 old_table->sensor_width == shading_table->sensor_width &&
2692 old_table->sensor_height == shading_table->sensor_height &&
2693 old_table->width == shading_table->width &&
2694 old_table->height == shading_table->height &&
2695 old_table->fraction_bits == shading_table->fraction_bits &&
2696 old_table->enable == shading_table->enable) {
2697 bool data_is_same = true;
2699 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
2700 if (memcmp(shading_table->data[i], old_table->data[i],
2702 data_is_same = false;
2708 atomisp_css_shading_table_free(shading_table);
2714 /* set LSC to CSS */
2715 css_param->shading_table = shading_table;
2716 css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table;
2717 asd->params.sc_en = shading_table;
2720 atomisp_css_shading_table_free(old_table);
2725 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd,
2726 struct ia_css_dvs2_coefficients *coefs,
2727 struct atomisp_css_params *css_param,
2730 struct ia_css_dvs_grid_info *cur =
2731 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
2732 int dvs_hor_coef_bytes, dvs_ver_coef_bytes;
2733 struct ia_css_dvs2_coefficients dvs2_coefs;
2738 if (!from_user && css_param->update_flag.dvs2_coefs)
2742 if (sizeof(*cur) != sizeof(coefs->grid) ||
2743 memcmp(&coefs->grid, cur, sizeof(coefs->grid))) {
2744 dev_err(asd->isp->dev, "dvs grid mismatch!\n");
2745 /* If the grid info in the argument differs from the current
2746 grid info, we tell the caller to reset the grid size and
2751 if (!coefs->hor_coefs.odd_real ||
2752 !coefs->hor_coefs.odd_imag ||
2753 !coefs->hor_coefs.even_real ||
2754 !coefs->hor_coefs.even_imag ||
2755 !coefs->ver_coefs.odd_real ||
2756 !coefs->ver_coefs.odd_imag ||
2757 !coefs->ver_coefs.even_real ||
2758 !coefs->ver_coefs.even_imag)
2761 if (!css_param->dvs2_coeff) {
2762 /* DIS coefficients. */
2763 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
2764 if (!css_param->dvs2_coeff)
2768 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
2769 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
2770 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
2771 coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
2772 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
2773 coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
2774 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
2775 coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
2776 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
2777 coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
2778 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
2779 coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
2780 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
2781 coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
2782 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
2783 coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
2784 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
2785 coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
2786 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
2787 css_param->dvs2_coeff = NULL;
2791 if (copy_from_compatible(&dvs2_coefs, coefs,
2792 sizeof(struct ia_css_dvs2_coefficients),
2794 dev_err(asd->isp->dev, "copy dvs2 coef failed");
2798 if (sizeof(*cur) != sizeof(dvs2_coefs.grid) ||
2799 memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) {
2800 dev_err(asd->isp->dev, "dvs grid mismatch!\n");
2801 /* If the grid info in the argument differs from the current
2802 grid info, we tell the caller to reset the grid size and
2807 if (!dvs2_coefs.hor_coefs.odd_real ||
2808 !dvs2_coefs.hor_coefs.odd_imag ||
2809 !dvs2_coefs.hor_coefs.even_real ||
2810 !dvs2_coefs.hor_coefs.even_imag ||
2811 !dvs2_coefs.ver_coefs.odd_real ||
2812 !dvs2_coefs.ver_coefs.odd_imag ||
2813 !dvs2_coefs.ver_coefs.even_real ||
2814 !dvs2_coefs.ver_coefs.even_imag)
2817 if (!css_param->dvs2_coeff) {
2818 /* DIS coefficients. */
2819 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
2820 if (!css_param->dvs2_coeff)
2824 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
2825 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
2826 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
2827 dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
2828 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
2829 dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
2830 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
2831 dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
2832 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
2833 dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
2834 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
2835 dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
2836 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
2837 dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
2838 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
2839 dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
2840 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
2841 dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
2842 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
2843 css_param->dvs2_coeff = NULL;
2848 css_param->update_flag.dvs2_coefs =
2849 (struct atomisp_dis_coefficients *)css_param->dvs2_coeff;
2853 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd,
2854 struct atomisp_dvs_6axis_config *source_6axis_config,
2855 struct atomisp_css_params *css_param,
2858 struct ia_css_dvs_6axis_config *dvs_6axis_config;
2859 struct ia_css_dvs_6axis_config *old_6axis_config;
2860 struct ia_css_stream *stream =
2861 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
2862 struct ia_css_dvs_grid_info *dvs_grid_info =
2863 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
2867 dev_err(asd->isp->dev, "%s: internal error!", __func__);
2871 if (!source_6axis_config || !dvs_grid_info)
2874 if (!dvs_grid_info->enable)
2877 if (!from_user && css_param->update_flag.dvs_6axis_config)
2880 /* check whether need to reallocate for 6 axis config */
2881 old_6axis_config = css_param->dvs_6axis;
2882 dvs_6axis_config = old_6axis_config;
2885 struct ia_css_dvs_6axis_config t_6axis_config;
2887 if (copy_from_compatible(&t_6axis_config, source_6axis_config,
2888 sizeof(struct atomisp_dvs_6axis_config),
2890 dev_err(asd->isp->dev, "copy morph table failed!");
2894 if (old_6axis_config &&
2895 (old_6axis_config->width_y != t_6axis_config.width_y ||
2896 old_6axis_config->height_y != t_6axis_config.height_y ||
2897 old_6axis_config->width_uv != t_6axis_config.width_uv ||
2898 old_6axis_config->height_uv != t_6axis_config.height_uv)) {
2899 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
2900 css_param->dvs_6axis = NULL;
2902 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2903 if (!dvs_6axis_config)
2905 } else if (!dvs_6axis_config) {
2906 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2907 if (!dvs_6axis_config)
2911 dvs_6axis_config->exp_id = t_6axis_config.exp_id;
2913 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
2914 t_6axis_config.xcoords_y,
2915 t_6axis_config.width_y *
2916 t_6axis_config.height_y *
2917 sizeof(*dvs_6axis_config->xcoords_y),
2920 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
2921 t_6axis_config.ycoords_y,
2922 t_6axis_config.width_y *
2923 t_6axis_config.height_y *
2924 sizeof(*dvs_6axis_config->ycoords_y),
2927 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
2928 t_6axis_config.xcoords_uv,
2929 t_6axis_config.width_uv *
2930 t_6axis_config.height_uv *
2931 sizeof(*dvs_6axis_config->xcoords_uv),
2934 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
2935 t_6axis_config.ycoords_uv,
2936 t_6axis_config.width_uv *
2937 t_6axis_config.height_uv *
2938 sizeof(*dvs_6axis_config->ycoords_uv),
2942 if (old_6axis_config &&
2943 (old_6axis_config->width_y != source_6axis_config->width_y ||
2944 old_6axis_config->height_y != source_6axis_config->height_y ||
2945 old_6axis_config->width_uv != source_6axis_config->width_uv ||
2946 old_6axis_config->height_uv != source_6axis_config->height_uv)) {
2947 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
2948 css_param->dvs_6axis = NULL;
2950 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2951 if (!dvs_6axis_config)
2953 } else if (!dvs_6axis_config) {
2954 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2955 if (!dvs_6axis_config)
2959 dvs_6axis_config->exp_id = source_6axis_config->exp_id;
2961 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
2962 source_6axis_config->xcoords_y,
2963 source_6axis_config->width_y *
2964 source_6axis_config->height_y *
2965 sizeof(*source_6axis_config->xcoords_y),
2968 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
2969 source_6axis_config->ycoords_y,
2970 source_6axis_config->width_y *
2971 source_6axis_config->height_y *
2972 sizeof(*source_6axis_config->ycoords_y),
2975 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
2976 source_6axis_config->xcoords_uv,
2977 source_6axis_config->width_uv *
2978 source_6axis_config->height_uv *
2979 sizeof(*source_6axis_config->xcoords_uv),
2982 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
2983 source_6axis_config->ycoords_uv,
2984 source_6axis_config->width_uv *
2985 source_6axis_config->height_uv *
2986 sizeof(*source_6axis_config->ycoords_uv),
2990 css_param->dvs_6axis = dvs_6axis_config;
2991 css_param->update_flag.dvs_6axis_config =
2992 (struct atomisp_dvs_6axis_config *)dvs_6axis_config;
2996 if (dvs_6axis_config)
2997 ia_css_dvs2_6axis_config_free(dvs_6axis_config);
3001 int atomisp_cp_morph_table(struct atomisp_sub_device *asd,
3002 struct atomisp_morph_table *source_morph_table,
3003 struct atomisp_css_params *css_param,
3008 struct ia_css_morph_table *morph_table;
3009 struct ia_css_morph_table *old_morph_table;
3011 if (!source_morph_table)
3014 if (!from_user && css_param->update_flag.morph_table)
3017 old_morph_table = css_param->morph_table;
3020 struct ia_css_morph_table mtbl;
3022 if (copy_from_compatible(&mtbl, source_morph_table,
3023 sizeof(struct atomisp_morph_table),
3025 dev_err(asd->isp->dev, "copy morph table failed!");
3029 morph_table = atomisp_css_morph_table_allocate(
3035 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3036 if (copy_from_compatible(morph_table->coordinates_x[i],
3037 (__force void *)source_morph_table->coordinates_x[i],
3038 mtbl.height * mtbl.width *
3039 sizeof(*morph_table->coordinates_x[i]),
3043 if (copy_from_compatible(morph_table->coordinates_y[i],
3044 (__force void *)source_morph_table->coordinates_y[i],
3045 mtbl.height * mtbl.width *
3046 sizeof(*morph_table->coordinates_y[i]),
3051 morph_table = atomisp_css_morph_table_allocate(
3052 source_morph_table->width,
3053 source_morph_table->height);
3057 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3058 if (copy_from_compatible(morph_table->coordinates_x[i],
3059 (__force void *)source_morph_table->coordinates_x[i],
3060 source_morph_table->height * source_morph_table->width *
3061 sizeof(*source_morph_table->coordinates_x[i]),
3065 if (copy_from_compatible(morph_table->coordinates_y[i],
3066 (__force void *)source_morph_table->coordinates_y[i],
3067 source_morph_table->height * source_morph_table->width *
3068 sizeof(*source_morph_table->coordinates_y[i]),
3074 css_param->morph_table = morph_table;
3075 if (old_morph_table)
3076 atomisp_css_morph_table_free(old_morph_table);
3077 css_param->update_flag.morph_table =
3078 (struct atomisp_morph_table *)morph_table;
3083 atomisp_css_morph_table_free(morph_table);
3087 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd,
3088 struct atomisp_parameters *arg,
3089 struct atomisp_css_params *css_param)
3093 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false);
3096 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false);
3099 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false);
3102 ret = atomisp_css_cp_dvs2_coefs(asd,
3103 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
3107 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
3112 void atomisp_free_css_parameters(struct atomisp_css_params *css_param)
3114 if (css_param->dvs_6axis) {
3115 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
3116 css_param->dvs_6axis = NULL;
3118 if (css_param->dvs2_coeff) {
3119 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
3120 css_param->dvs2_coeff = NULL;
3122 if (css_param->shading_table) {
3123 ia_css_shading_table_free(css_param->shading_table);
3124 css_param->shading_table = NULL;
3126 if (css_param->morph_table) {
3127 ia_css_morph_table_free(css_param->morph_table);
3128 css_param->morph_table = NULL;
3132 static void atomisp_move_frame_to_activeq(struct ia_css_frame *frame,
3133 struct atomisp_css_params_with_list *param)
3135 struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf);
3136 unsigned long irqflags;
3138 pipe->frame_params[frame->vb.vb2_buf.index] = param;
3139 spin_lock_irqsave(&pipe->irq_lock, irqflags);
3140 list_move_tail(&frame->queue, &pipe->activeq);
3141 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
3145 * Check parameter queue list and buffer queue list to find out if matched items
3146 * and then set parameter to CSS and enqueue buffer to CSS.
3147 * Of course, if the buffer in buffer waiting list is not bound to a per-frame
3148 * parameter, it will be enqueued into CSS as long as the per-frame setting
3149 * buffers before it get enqueued.
3151 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe)
3153 struct atomisp_sub_device *asd = pipe->asd;
3154 struct ia_css_frame *frame = NULL, *frame_tmp;
3155 struct atomisp_css_params_with_list *param = NULL, *param_tmp;
3156 bool need_to_enqueue_buffer = false;
3160 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
3161 __func__, pipe->vdev.name);
3165 lockdep_assert_held(&asd->isp->mutex);
3168 * CSS/FW requires set parameter and enqueue buffer happen after ISP
3171 if (!asd->streaming)
3174 if (list_empty(&pipe->per_frame_params) ||
3175 list_empty(&pipe->buffers_waiting_for_param))
3178 list_for_each_entry_safe(frame, frame_tmp,
3179 &pipe->buffers_waiting_for_param, queue) {
3180 i = frame->vb.vb2_buf.index;
3181 if (pipe->frame_request_config_id[i]) {
3182 list_for_each_entry_safe(param, param_tmp,
3183 &pipe->per_frame_params, list) {
3184 if (pipe->frame_request_config_id[i] != param->params.isp_config_id)
3187 list_del(¶m->list);
3190 * clear the request config id as the buffer
3191 * will be handled and enqueued into CSS soon
3193 pipe->frame_request_config_id[i] = 0;
3194 atomisp_move_frame_to_activeq(frame, param);
3195 need_to_enqueue_buffer = true;
3199 /* If this is the end, stop further loop */
3200 if (list_entry_is_head(param, &pipe->per_frame_params, list))
3203 atomisp_move_frame_to_activeq(frame, NULL);
3204 need_to_enqueue_buffer = true;
3208 if (!need_to_enqueue_buffer)
3211 atomisp_qbuffers_to_css(asd);
3215 * Function to configure ISP parameters
3217 int atomisp_set_parameters(struct video_device *vdev,
3218 struct atomisp_parameters *arg)
3220 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
3221 struct atomisp_sub_device *asd = pipe->asd;
3222 struct atomisp_css_params_with_list *param = NULL;
3223 struct atomisp_css_params *css_param = &asd->params.css_param;
3227 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
3228 __func__, vdev->name);
3232 lockdep_assert_held(&asd->isp->mutex);
3234 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) {
3235 dev_err(asd->isp->dev, "%s: internal error!\n", __func__);
3239 dev_dbg(asd->isp->dev, "set parameter(per_frame_setting %d) isp_config_id %d of %s\n",
3240 arg->per_frame_setting, arg->isp_config_id, vdev->name);
3242 if (arg->per_frame_setting) {
3244 * Per-frame setting enabled, we allocate a new parameter
3245 * buffer to cache the parameters and only when frame buffers
3246 * are ready, the parameters will be set to CSS.
3247 * per-frame setting only works for the main output frame.
3249 param = kvzalloc(sizeof(*param), GFP_KERNEL);
3251 dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n",
3255 css_param = ¶m->params;
3258 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true);
3260 goto apply_parameter_failed;
3262 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true);
3264 goto apply_parameter_failed;
3266 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true);
3268 goto apply_parameter_failed;
3270 ret = atomisp_css_cp_dvs2_coefs(asd,
3271 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
3274 goto apply_parameter_failed;
3276 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
3279 goto apply_parameter_failed;
3281 if (!arg->per_frame_setting) {
3282 /* indicate to CSS that we have parameters to be updated */
3283 asd->params.css_update_params_needed = true;
3285 list_add_tail(¶m->list, &pipe->per_frame_params);
3286 atomisp_handle_parameter_and_buffer(pipe);
3291 apply_parameter_failed:
3293 atomisp_free_css_parameters(css_param);
3300 * Function to set/get isp parameters to isp
3302 int atomisp_param(struct atomisp_sub_device *asd, int flag,
3303 struct atomisp_parm *config)
3305 struct ia_css_pipe_config *vp_cfg =
3306 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
3307 pipe_configs[IA_CSS_PIPE_ID_VIDEO];
3309 /* Read parameter for 3A binary info */
3311 struct ia_css_dvs_grid_info *dvs_grid_info =
3312 atomisp_css_get_dvs_grid_info(
3313 &asd->params.curr_grid_info);
3315 atomisp_curr_user_grid_info(asd, &config->info);
3317 /* We always return the resolution and stride even if there is
3318 * no valid metadata. This allows the caller to get the
3319 * information needed to allocate user-space buffers. */
3320 config->metadata_config.metadata_height = asd->
3321 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
3322 metadata_info.resolution.height;
3323 config->metadata_config.metadata_stride = asd->
3324 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
3325 metadata_info.stride;
3327 /* update dvs grid info */
3329 memcpy(&config->dvs_grid,
3331 sizeof(struct ia_css_dvs_grid_info));
3333 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
3334 config->dvs_envelop.width = 0;
3335 config->dvs_envelop.height = 0;
3339 /* update dvs envelop info */
3340 config->dvs_envelop.width = vp_cfg->dvs_envelope.width;
3341 config->dvs_envelop.height = vp_cfg->dvs_envelope.height;
3345 memcpy(&asd->params.css_param.wb_config, &config->wb_config,
3346 sizeof(struct ia_css_wb_config));
3347 memcpy(&asd->params.css_param.ob_config, &config->ob_config,
3348 sizeof(struct ia_css_ob_config));
3349 memcpy(&asd->params.css_param.dp_config, &config->dp_config,
3350 sizeof(struct ia_css_dp_config));
3351 memcpy(&asd->params.css_param.de_config, &config->de_config,
3352 sizeof(struct ia_css_de_config));
3353 memcpy(&asd->params.css_param.dz_config, &config->dz_config,
3354 sizeof(struct ia_css_dz_config));
3355 memcpy(&asd->params.css_param.ce_config, &config->ce_config,
3356 sizeof(struct ia_css_ce_config));
3357 memcpy(&asd->params.css_param.nr_config, &config->nr_config,
3358 sizeof(struct ia_css_nr_config));
3359 memcpy(&asd->params.css_param.ee_config, &config->ee_config,
3360 sizeof(struct ia_css_ee_config));
3361 memcpy(&asd->params.css_param.tnr_config, &config->tnr_config,
3362 sizeof(struct ia_css_tnr_config));
3364 if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) {
3365 asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3];
3366 asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4];
3367 asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5];
3368 asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6];
3369 asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7];
3370 asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8];
3373 if (asd->params.color_effect != V4L2_COLORFX_SEPIA &&
3374 asd->params.color_effect != V4L2_COLORFX_BW) {
3375 memcpy(&asd->params.css_param.cc_config, &config->cc_config,
3376 sizeof(struct ia_css_cc_config));
3377 asd->params.config.cc_config = &asd->params.css_param.cc_config;
3380 asd->params.config.wb_config = &asd->params.css_param.wb_config;
3381 asd->params.config.ob_config = &asd->params.css_param.ob_config;
3382 asd->params.config.de_config = &asd->params.css_param.de_config;
3383 asd->params.config.dz_config = &asd->params.css_param.dz_config;
3384 asd->params.config.ce_config = &asd->params.css_param.ce_config;
3385 asd->params.config.dp_config = &asd->params.css_param.dp_config;
3386 asd->params.config.nr_config = &asd->params.css_param.nr_config;
3387 asd->params.config.ee_config = &asd->params.css_param.ee_config;
3388 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
3389 asd->params.css_update_params_needed = true;
3395 * Function to configure color effect of the image
3397 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag,
3400 struct ia_css_cc_config *cc_config = NULL;
3401 struct ia_css_macc_table *macc_table = NULL;
3402 struct ia_css_ctc_table *ctc_table = NULL;
3404 struct v4l2_control control;
3405 struct atomisp_device *isp = asd->isp;
3408 *effect = asd->params.color_effect;
3412 control.id = V4L2_CID_COLORFX;
3413 control.value = *effect;
3415 v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler,
3418 * if set color effect to sensor successfully, return
3422 asd->params.color_effect = (u32)*effect;
3426 if (*effect == asd->params.color_effect)
3430 * isp_subdev->params.macc_en should be set to false.
3432 asd->params.macc_en = false;
3435 case V4L2_COLORFX_NONE:
3436 macc_table = &asd->params.css_param.macc_table;
3437 asd->params.macc_en = true;
3439 case V4L2_COLORFX_SEPIA:
3440 cc_config = &sepia_cc_config;
3442 case V4L2_COLORFX_NEGATIVE:
3443 cc_config = &nega_cc_config;
3445 case V4L2_COLORFX_BW:
3446 cc_config = &mono_cc_config;
3448 case V4L2_COLORFX_SKY_BLUE:
3449 macc_table = &blue_macc_table;
3450 asd->params.macc_en = true;
3452 case V4L2_COLORFX_GRASS_GREEN:
3453 macc_table = &green_macc_table;
3454 asd->params.macc_en = true;
3456 case V4L2_COLORFX_SKIN_WHITEN_LOW:
3457 macc_table = &skin_low_macc_table;
3458 asd->params.macc_en = true;
3460 case V4L2_COLORFX_SKIN_WHITEN:
3461 macc_table = &skin_medium_macc_table;
3462 asd->params.macc_en = true;
3464 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
3465 macc_table = &skin_high_macc_table;
3466 asd->params.macc_en = true;
3468 case V4L2_COLORFX_VIVID:
3469 ctc_table = &vivid_ctc_table;
3474 atomisp_update_capture_mode(asd);
3477 asd->params.config.cc_config = cc_config;
3479 asd->params.config.macc_table = macc_table;
3481 atomisp_css_set_ctc_table(asd, ctc_table);
3482 asd->params.color_effect = (u32)*effect;
3483 asd->params.css_update_params_needed = true;
3488 * Function to configure bad pixel correction
3490 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag,
3494 *value = asd->params.bad_pixel_en;
3497 asd->params.bad_pixel_en = !!*value;
3503 * Function to configure bad pixel correction params
3505 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag,
3506 struct atomisp_dp_config *config)
3509 /* Get bad pixel from current setup */
3510 if (atomisp_css_get_dp_config(asd, config))
3513 /* Set bad pixel to isp parameters */
3514 memcpy(&asd->params.css_param.dp_config, config,
3515 sizeof(asd->params.css_param.dp_config));
3516 asd->params.config.dp_config = &asd->params.css_param.dp_config;
3517 asd->params.css_update_params_needed = true;
3524 * Function to enable/disable video image stablization
3526 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag,
3530 *value = asd->params.video_dis_en;
3532 asd->params.video_dis_en = !!*value;
3538 * Function to configure fixed pattern noise
3540 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag,
3544 *value = asd->params.fpn_en;
3549 asd->params.fpn_en = false;
3553 /* Add function to get black from from sensor with shutter off */
3558 atomisp_bytesperline_to_padded_width(unsigned int bytesperline,
3559 enum ia_css_frame_format format)
3562 case IA_CSS_FRAME_FORMAT_UYVY:
3563 case IA_CSS_FRAME_FORMAT_YUYV:
3564 case IA_CSS_FRAME_FORMAT_RAW:
3565 case IA_CSS_FRAME_FORMAT_RGB565:
3566 return bytesperline / 2;
3567 case IA_CSS_FRAME_FORMAT_RGBA888:
3568 return bytesperline / 4;
3569 /* The following cases could be removed, but we leave them
3570 in to document the formats that are included. */
3571 case IA_CSS_FRAME_FORMAT_NV11:
3572 case IA_CSS_FRAME_FORMAT_NV12:
3573 case IA_CSS_FRAME_FORMAT_NV16:
3574 case IA_CSS_FRAME_FORMAT_NV21:
3575 case IA_CSS_FRAME_FORMAT_NV61:
3576 case IA_CSS_FRAME_FORMAT_YV12:
3577 case IA_CSS_FRAME_FORMAT_YV16:
3578 case IA_CSS_FRAME_FORMAT_YUV420:
3579 case IA_CSS_FRAME_FORMAT_YUV420_16:
3580 case IA_CSS_FRAME_FORMAT_YUV422:
3581 case IA_CSS_FRAME_FORMAT_YUV422_16:
3582 case IA_CSS_FRAME_FORMAT_YUV444:
3583 case IA_CSS_FRAME_FORMAT_YUV_LINE:
3584 case IA_CSS_FRAME_FORMAT_PLANAR_RGB888:
3585 case IA_CSS_FRAME_FORMAT_QPLANE6:
3586 case IA_CSS_FRAME_FORMAT_BINARY_8:
3588 return bytesperline;
3593 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg,
3594 struct ia_css_frame **result)
3596 struct ia_css_frame *res = NULL;
3597 unsigned int padded_width;
3598 enum ia_css_frame_format sh_format;
3599 char *tmp_buf = NULL;
3602 sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat);
3603 padded_width = atomisp_bytesperline_to_padded_width(
3604 arg->fmt.bytesperline, sh_format);
3606 /* Note: the padded width on an ia_css_frame is in elements, not in
3607 bytes. The RAW frame we use here should always be a 16bit RAW
3608 frame. This is why we bytesperline/2 is equal to the padded with */
3609 if (ia_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height,
3610 sh_format, padded_width, 0)) {
3615 tmp_buf = vmalloc(arg->fmt.sizeimage);
3620 if (copy_from_user(tmp_buf, (void __user __force *)arg->base,
3621 arg->fmt.sizeimage)) {
3626 if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) {
3633 ia_css_frame_free(res);
3641 * Function to configure fixed pattern noise table
3643 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd,
3644 struct v4l2_framebuffer *arg)
3646 struct ia_css_frame *raw_black_frame = NULL;
3652 ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame);
3656 if (sh_css_set_black_frame(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream,
3657 raw_black_frame) != 0)
3660 ia_css_frame_free(raw_black_frame);
3665 * Function to configure false color correction
3667 int atomisp_false_color(struct atomisp_sub_device *asd, int flag,
3670 /* Get nr config from current setup */
3672 *value = asd->params.false_color;
3676 /* Set nr config to isp parameters */
3678 asd->params.config.de_config = NULL;
3680 asd->params.css_param.de_config.pixelnoise = 0;
3681 asd->params.config.de_config = &asd->params.css_param.de_config;
3683 asd->params.css_update_params_needed = true;
3684 asd->params.false_color = *value;
3689 * Function to configure bad pixel correction params
3691 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag,
3692 struct atomisp_de_config *config)
3695 /* Get false color from current setup */
3696 if (atomisp_css_get_de_config(asd, config))
3699 /* Set false color to isp parameters */
3700 memcpy(&asd->params.css_param.de_config, config,
3701 sizeof(asd->params.css_param.de_config));
3702 asd->params.config.de_config = &asd->params.css_param.de_config;
3703 asd->params.css_update_params_needed = true;
3710 * Function to configure white balance params
3712 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag,
3713 struct atomisp_wb_config *config)
3716 /* Get white balance from current setup */
3717 if (atomisp_css_get_wb_config(asd, config))
3720 /* Set white balance to isp parameters */
3721 memcpy(&asd->params.css_param.wb_config, config,
3722 sizeof(asd->params.css_param.wb_config));
3723 asd->params.config.wb_config = &asd->params.css_param.wb_config;
3724 asd->params.css_update_params_needed = true;
3730 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag,
3731 struct atomisp_3a_config *config)
3733 struct atomisp_device *isp = asd->isp;
3735 dev_dbg(isp->dev, ">%s %d\n", __func__, flag);
3738 /* Get white balance from current setup */
3739 if (atomisp_css_get_3a_config(asd, config))
3742 /* Set white balance to isp parameters */
3743 memcpy(&asd->params.css_param.s3a_config, config,
3744 sizeof(asd->params.css_param.s3a_config));
3745 asd->params.config.s3a_config = &asd->params.css_param.s3a_config;
3746 asd->params.css_update_params_needed = true;
3749 dev_dbg(isp->dev, "<%s %d\n", __func__, flag);
3754 * Function to setup digital zoom
3756 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag,
3760 struct atomisp_device *isp = asd->isp;
3762 unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR;
3765 atomisp_css_get_zoom_factor(asd, &zoom);
3766 *value = max_zoom - zoom;
3771 zoom = max_zoom - min_t(u32, max_zoom - 1, *value);
3772 atomisp_css_set_zoom_factor(asd, zoom);
3774 dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom);
3775 asd->params.css_update_params_needed = true;
3781 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd,
3782 u16 stream_index, struct atomisp_input_stream_info *stream_info)
3786 /* assign virtual channel id return from sensor driver query */
3787 asd->stream_env[stream_index].ch_id = stream_info->ch_id;
3788 asd->stream_env[stream_index].isys_configs = stream_info->isys_configs;
3789 for (i = 0; i < stream_info->isys_configs; i++) {
3790 asd->stream_env[stream_index].isys_info[i].input_format =
3791 stream_info->isys_info[i].input_format;
3792 asd->stream_env[stream_index].isys_info[i].width =
3793 stream_info->isys_info[i].width;
3794 asd->stream_env[stream_index].isys_info[i].height =
3795 stream_info->isys_info[i].height;
3799 static void __atomisp_init_stream_info(u16 stream_index,
3800 struct atomisp_input_stream_info *stream_info)
3804 stream_info->enable = 1;
3805 stream_info->stream = stream_index;
3806 stream_info->ch_id = 0;
3807 stream_info->isys_configs = 0;
3808 for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) {
3809 stream_info->isys_info[i].input_format = 0;
3810 stream_info->isys_info[i].width = 0;
3811 stream_info->isys_info[i].height = 0;
3815 /* This function looks up the closest available resolution. */
3816 int atomisp_try_fmt(struct video_device *vdev, struct v4l2_pix_format *f)
3818 struct atomisp_device *isp = video_get_drvdata(vdev);
3819 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
3820 struct v4l2_subdev_pad_config pad_cfg;
3821 struct v4l2_subdev_state pad_state = {
3824 struct v4l2_subdev_format format = {
3825 .which = V4L2_SUBDEV_FORMAT_TRY,
3827 const struct atomisp_format_bridge *fmt;
3831 dev_err(isp->dev, "%s(): asd is NULL, device is %s\n",
3832 __func__, vdev->name);
3836 if (!isp->inputs[asd->input_curr].camera)
3839 fmt = atomisp_get_format_bridge(f->pixelformat);
3841 dev_err(isp->dev, "unsupported pixelformat!\n");
3842 fmt = atomisp_output_fmts;
3845 if (f->width <= 0 || f->height <= 0)
3848 format.format.code = fmt->mbus_code;
3849 format.format.width = f->width;
3850 format.format.height = f->height;
3852 __atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL,
3853 (struct atomisp_input_stream_info *)format.format.reserved);
3855 dev_dbg(isp->dev, "try_mbus_fmt: asking for %ux%u\n",
3856 format.format.width, format.format.height);
3858 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
3859 pad, set_fmt, &pad_state, &format);
3863 dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n",
3864 format.format.width, format.format.height);
3866 fmt = atomisp_get_format_bridge_from_mbus(format.format.code);
3868 dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
3869 format.format.code);
3873 f->pixelformat = fmt->pixelformat;
3874 f->width = format.format.width;
3875 f->height = format.format.height;
3878 * If the format is jpeg or custom RAW, then the width and height will
3879 * not satisfy the normal atomisp requirements and no need to check
3880 * the below conditions. So just assign to what is being returned from
3881 * the sensor driver.
3883 if (f->pixelformat == V4L2_PIX_FMT_JPEG ||
3884 f->pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW)
3888 f->width = rounddown(clamp_t(u32, f->width, ATOM_ISP_MIN_WIDTH,
3889 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
3890 f->height = rounddown(clamp_t(u32, f->height, ATOM_ISP_MIN_HEIGHT,
3891 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
3896 enum mipi_port_id atomisp_port_to_mipi_port(struct atomisp_device *isp,
3897 enum atomisp_camera_port port)
3900 case ATOMISP_CAMERA_PORT_PRIMARY:
3901 return MIPI_PORT0_ID;
3902 case ATOMISP_CAMERA_PORT_SECONDARY:
3903 return MIPI_PORT1_ID;
3904 case ATOMISP_CAMERA_PORT_TERTIARY:
3905 return MIPI_PORT2_ID;
3907 dev_err(isp->dev, "unsupported port: %d\n", port);
3908 return MIPI_PORT0_ID;
3912 static inline int atomisp_set_sensor_mipi_to_isp(
3913 struct atomisp_sub_device *asd,
3914 enum atomisp_input_stream_id stream_id,
3915 struct camera_mipi_info *mipi_info)
3917 struct v4l2_control ctrl;
3918 struct atomisp_device *isp = asd->isp;
3919 const struct atomisp_in_fmt_conv *fc;
3921 unsigned int input_format, bayer_order;
3922 enum atomisp_input_format metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED;
3923 u32 mipi_port, metadata_width = 0, metadata_height = 0;
3925 ctrl.id = V4L2_CID_LINK_FREQ;
3927 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl) == 0)
3928 mipi_freq = ctrl.value;
3930 if (asd->stream_env[stream_id].isys_configs == 1) {
3932 asd->stream_env[stream_id].isys_info[0].input_format;
3933 atomisp_css_isys_set_format(asd, stream_id,
3934 input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX);
3935 } else if (asd->stream_env[stream_id].isys_configs == 2) {
3936 atomisp_css_isys_two_stream_cfg_update_stream1(
3938 asd->stream_env[stream_id].isys_info[0].input_format,
3939 asd->stream_env[stream_id].isys_info[0].width,
3940 asd->stream_env[stream_id].isys_info[0].height);
3942 atomisp_css_isys_two_stream_cfg_update_stream2(
3944 asd->stream_env[stream_id].isys_info[1].input_format,
3945 asd->stream_env[stream_id].isys_info[1].width,
3946 asd->stream_env[stream_id].isys_info[1].height);
3949 /* Compatibility for sensors which provide no media bus code
3950 * in s_mbus_framefmt() nor support pad formats. */
3951 if (mipi_info && mipi_info->input_format != -1) {
3952 bayer_order = mipi_info->raw_bayer_order;
3954 /* Input stream config is still needs configured */
3955 /* TODO: Check if this is necessary */
3956 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
3957 mipi_info->input_format);
3960 input_format = fc->atomisp_in_fmt;
3961 metadata_format = mipi_info->metadata_format;
3962 metadata_width = mipi_info->metadata_width;
3963 metadata_height = mipi_info->metadata_height;
3965 struct v4l2_mbus_framefmt *sink;
3967 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
3968 V4L2_SUBDEV_FORMAT_ACTIVE,
3969 ATOMISP_SUBDEV_PAD_SINK);
3970 fc = atomisp_find_in_fmt_conv(sink->code);
3973 input_format = fc->atomisp_in_fmt;
3974 bayer_order = fc->bayer_order;
3977 atomisp_css_input_set_format(asd, stream_id, input_format);
3978 atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order);
3980 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(metadata_format);
3984 input_format = fc->atomisp_in_fmt;
3985 mipi_port = atomisp_port_to_mipi_port(isp, isp->inputs[asd->input_curr].port);
3986 atomisp_css_input_configure_port(asd, mipi_port,
3987 isp->sensor_lanes[mipi_port],
3990 metadata_width, metadata_height);
3994 static int configure_pp_input_nop(struct atomisp_sub_device *asd,
3995 unsigned int width, unsigned int height)
4000 static int configure_output_nop(struct atomisp_sub_device *asd,
4001 unsigned int width, unsigned int height,
4002 unsigned int min_width,
4003 enum ia_css_frame_format sh_fmt)
4008 static int get_frame_info_nop(struct atomisp_sub_device *asd,
4009 struct ia_css_frame_info *finfo)
4015 * Resets CSS parameters that depend on input resolution.
4017 * Update params like CSS RAW binning, 2ppc mode and pp_input
4018 * which depend on input size, but are not automatically
4019 * handled in CSS when the input resolution is changed.
4021 static int css_input_resolution_changed(struct atomisp_sub_device *asd,
4022 struct v4l2_mbus_framefmt *ffmt)
4024 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
4027 dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n",
4028 ffmt->width, ffmt->height);
4031 atomisp_css_input_set_two_pixels_per_clock(asd, false);
4033 atomisp_css_input_set_two_pixels_per_clock(asd, true);
4036 * If sensor input changed, which means metadata resolution changed
4037 * together. Release all metadata buffers here to let it re-allocated
4038 * next time in reqbufs.
4040 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
4041 list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
4043 atomisp_css_free_metadata_buffer(md_buf);
4044 list_del(&md_buf->list);
4051 * TODO: atomisp_css_preview_configure_pp_input() not
4052 * reset due to CSS bug tracked as PSI BZ 115124
4056 static int atomisp_set_fmt_to_isp(struct video_device *vdev,
4057 struct ia_css_frame_info *output_info,
4058 struct v4l2_pix_format *pix)
4060 struct camera_mipi_info *mipi_info;
4061 struct atomisp_device *isp = video_get_drvdata(vdev);
4062 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4063 const struct atomisp_format_bridge *format;
4064 struct v4l2_rect *isp_sink_crop;
4065 enum ia_css_pipe_id pipe_id;
4066 struct v4l2_subdev_fh fh;
4067 int (*configure_output)(struct atomisp_sub_device *asd,
4068 unsigned int width, unsigned int height,
4069 unsigned int min_width,
4070 enum ia_css_frame_format sh_fmt) =
4071 configure_output_nop;
4072 int (*get_frame_info)(struct atomisp_sub_device *asd,
4073 struct ia_css_frame_info *finfo) =
4075 int (*configure_pp_input)(struct atomisp_sub_device *asd,
4076 unsigned int width, unsigned int height) =
4077 configure_pp_input_nop;
4078 const struct atomisp_in_fmt_conv *fc = NULL;
4082 dev_err(isp->dev, "%s(): asd is NULL, device is %s\n",
4083 __func__, vdev->name);
4087 v4l2_fh_init(&fh.vfh, vdev);
4089 isp_sink_crop = atomisp_subdev_get_rect(
4090 &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
4091 ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP);
4093 format = atomisp_get_format_bridge(pix->pixelformat);
4097 if (isp->inputs[asd->input_curr].type != TEST_PATTERN) {
4098 mipi_info = atomisp_to_sensor_mipi_info(
4099 isp->inputs[asd->input_curr].camera);
4101 if (atomisp_set_sensor_mipi_to_isp(asd, ATOMISP_INPUT_STREAM_GENERAL,
4106 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(mipi_info->input_format);
4109 fc = atomisp_find_in_fmt_conv(
4110 atomisp_subdev_get_ffmt(&asd->subdev,
4111 NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
4112 ATOMISP_SUBDEV_PAD_SINK)->code);
4115 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW &&
4116 raw_output_format_match_input(fc->atomisp_in_fmt,
4122 * Configure viewfinder also when vfpp is disabled: the
4123 * CSS still requires viewfinder configuration.
4128 if (pix->width < 640 || pix->height < 480) {
4130 height = pix->height;
4136 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
4137 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4138 atomisp_css_video_configure_viewfinder(asd, width, height, 0,
4139 IA_CSS_FRAME_FORMAT_NV12);
4140 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_STILL_CAPTURE ||
4141 asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
4142 atomisp_css_capture_configure_viewfinder(asd, width, height, 0,
4143 IA_CSS_FRAME_FORMAT_NV12);
4147 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
4149 for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++)
4150 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipe_extra_configs[i].disable_vf_pp = asd->vfpp->val != ATOMISP_VFPP_ENABLE;
4152 /* ISP2401 new input system need to use copy pipe */
4153 if (asd->copy_mode) {
4154 pipe_id = IA_CSS_PIPE_ID_COPY;
4155 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL, false);
4156 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4157 /* video same in continuouscapture and online modes */
4158 configure_output = atomisp_css_video_configure_output;
4159 get_frame_info = atomisp_css_video_get_output_frame_info;
4160 pipe_id = IA_CSS_PIPE_ID_VIDEO;
4161 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4162 configure_output = atomisp_css_video_configure_output;
4163 get_frame_info = atomisp_css_video_get_output_frame_info;
4164 pipe_id = IA_CSS_PIPE_ID_VIDEO;
4165 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
4166 configure_output = atomisp_css_preview_configure_output;
4167 get_frame_info = atomisp_css_preview_get_output_frame_info;
4168 configure_pp_input = atomisp_css_preview_configure_pp_input;
4169 pipe_id = IA_CSS_PIPE_ID_PREVIEW;
4171 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
4172 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
4173 atomisp_css_enable_dz(asd, false);
4175 atomisp_update_capture_mode(asd);
4178 /* in case of ANR, force capture pipe to offline mode */
4179 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
4180 !asd->params.low_light);
4182 configure_output = atomisp_css_capture_configure_output;
4183 get_frame_info = atomisp_css_capture_get_output_frame_info;
4184 configure_pp_input = atomisp_css_capture_configure_pp_input;
4185 pipe_id = IA_CSS_PIPE_ID_CAPTURE;
4187 if (asd->run_mode->val != ATOMISP_RUN_MODE_STILL_CAPTURE) {
4189 "Need to set the running mode first\n");
4190 asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE;
4195 ret = atomisp_css_copy_configure_output(asd, ATOMISP_INPUT_STREAM_GENERAL,
4196 pix->width, pix->height,
4197 format->planar ? pix->bytesperline :
4198 pix->bytesperline * 8 / format->depth,
4201 ret = configure_output(asd, pix->width, pix->height,
4202 format->planar ? pix->bytesperline :
4203 pix->bytesperline * 8 / format->depth,
4206 dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n",
4207 pix->width, pix->height, format->sh_fmt);
4211 ret = configure_pp_input(asd, isp_sink_crop->width, isp_sink_crop->height);
4213 dev_err(isp->dev, "configure_pp_input %ux%u\n",
4214 isp_sink_crop->width,
4215 isp_sink_crop->height);
4219 ret = atomisp_css_copy_get_output_frame_info(asd,
4220 ATOMISP_INPUT_STREAM_GENERAL,
4223 ret = get_frame_info(asd, output_info);
4225 dev_err(isp->dev, "__get_frame_info %ux%u (padded to %u) returned %d\n",
4226 pix->width, pix->height, pix->bytesperline, ret);
4230 atomisp_update_grid_info(asd, pipe_id);
4234 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd,
4235 unsigned int width, unsigned int height,
4236 unsigned int *dvs_env_w, unsigned int *dvs_env_h)
4238 if (asd->params.video_dis_en &&
4239 asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4240 /* envelope is 20% of the output resolution */
4242 * dvs envelope cannot be round up.
4243 * it would cause ISP timeout and color switch issue
4245 *dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH);
4246 *dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT);
4249 asd->params.dis_proj_data_valid = false;
4250 asd->params.css_update_params_needed = true;
4253 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd,
4254 const struct v4l2_pix_format *f)
4256 struct v4l2_mbus_framefmt *sink, *src;
4259 /* Only used for the new input system */
4260 asd->copy_mode = false;
4264 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4265 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK);
4266 src = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4267 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SOURCE);
4269 if (sink->code == src->code && sink->width == f->width && sink->height == f->height)
4270 asd->copy_mode = true;
4272 asd->copy_mode = false;
4274 dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode);
4277 static int atomisp_set_fmt_to_snr(struct video_device *vdev, const struct v4l2_pix_format *f,
4278 unsigned int padding_w, unsigned int padding_h,
4279 unsigned int dvs_env_w, unsigned int dvs_env_h)
4281 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4282 struct atomisp_sub_device *asd = pipe->asd;
4283 const struct atomisp_format_bridge *format;
4284 struct v4l2_subdev_pad_config pad_cfg;
4285 struct v4l2_subdev_state pad_state = {
4288 struct v4l2_subdev_format vformat = {
4289 .which = V4L2_SUBDEV_FORMAT_TRY,
4291 struct v4l2_mbus_framefmt *ffmt = &vformat.format;
4292 struct v4l2_mbus_framefmt *req_ffmt;
4293 struct atomisp_device *isp;
4294 struct atomisp_input_stream_info *stream_info =
4295 (struct atomisp_input_stream_info *)ffmt->reserved;
4296 struct v4l2_subdev_fh fh;
4300 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
4301 __func__, vdev->name);
4307 v4l2_fh_init(&fh.vfh, vdev);
4309 format = atomisp_get_format_bridge(f->pixelformat);
4313 v4l2_fill_mbus_format(ffmt, f, format->mbus_code);
4314 ffmt->height += padding_h + dvs_env_h;
4315 ffmt->width += padding_w + dvs_env_w;
4317 dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n",
4318 ffmt->width, ffmt->height, padding_w, padding_h,
4319 dvs_env_w, dvs_env_h);
4321 __atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4325 /* Disable dvs if resolution can't be supported by sensor */
4326 if (asd->params.video_dis_en && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4327 vformat.which = V4L2_SUBDEV_FORMAT_TRY;
4328 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
4329 pad, set_fmt, &pad_state, &vformat);
4333 dev_dbg(isp->dev, "video dis: sensor width: %d, height: %d\n",
4334 ffmt->width, ffmt->height);
4336 if (ffmt->width < req_ffmt->width ||
4337 ffmt->height < req_ffmt->height) {
4338 req_ffmt->height -= dvs_env_h;
4339 req_ffmt->width -= dvs_env_w;
4342 "can not enable video dis due to sensor limitation.");
4343 asd->params.video_dis_en = false;
4346 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
4347 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
4348 set_fmt, NULL, &vformat);
4352 __atomisp_update_stream_env(asd, ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4354 dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
4355 ffmt->width, ffmt->height);
4357 if (ffmt->width < ATOM_ISP_STEP_WIDTH ||
4358 ffmt->height < ATOM_ISP_STEP_HEIGHT)
4361 if (asd->params.video_dis_en && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
4362 (ffmt->width < req_ffmt->width || ffmt->height < req_ffmt->height)) {
4364 "can not enable video dis due to sensor limitation.");
4365 asd->params.video_dis_en = false;
4368 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
4369 V4L2_SUBDEV_FORMAT_ACTIVE,
4370 ATOMISP_SUBDEV_PAD_SINK, ffmt);
4372 return css_input_resolution_changed(asd, ffmt);
4375 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f)
4377 struct atomisp_device *isp = video_get_drvdata(vdev);
4378 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4379 struct atomisp_sub_device *asd = pipe->asd;
4380 const struct atomisp_format_bridge *format_bridge;
4381 const struct atomisp_format_bridge *snr_format_bridge;
4382 struct ia_css_frame_info output_info;
4383 unsigned int dvs_env_w = 0, dvs_env_h = 0;
4384 unsigned int padding_w = pad_w, padding_h = pad_h;
4385 struct v4l2_mbus_framefmt isp_source_fmt = {0};
4386 struct v4l2_subdev_format vformat = {
4387 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
4389 struct v4l2_rect isp_sink_crop;
4390 struct v4l2_subdev_fh fh;
4393 ret = atomisp_pipe_check(pipe, true);
4398 "setting resolution %ux%u bytesperline %u\n",
4399 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.bytesperline);
4401 v4l2_fh_init(&fh.vfh, vdev);
4403 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
4407 /* Currently, raw formats are broken!!! */
4409 if (format_bridge->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
4410 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
4412 format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
4416 pipe->sh_fmt = format_bridge->sh_fmt;
4417 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
4419 /* Ensure that the resolution is equal or below the maximum supported */
4421 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
4422 v4l2_fill_mbus_format(&vformat.format, &f->fmt.pix, format_bridge->mbus_code);
4423 vformat.format.height += padding_h;
4424 vformat.format.width += padding_w;
4426 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
4427 set_fmt, NULL, &vformat);
4431 f->fmt.pix.width = vformat.format.width - padding_w;
4432 f->fmt.pix.height = vformat.format.height - padding_h;
4434 snr_format_bridge = atomisp_get_format_bridge_from_mbus(vformat.format.code);
4435 if (!snr_format_bridge) {
4436 dev_warn(isp->dev, "Can't find bridge format\n");
4440 atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4441 V4L2_SUBDEV_FORMAT_ACTIVE,
4442 ATOMISP_SUBDEV_PAD_SINK)->code =
4443 snr_format_bridge->mbus_code;
4445 isp_source_fmt.code = format_bridge->mbus_code;
4446 atomisp_subdev_set_ffmt(&asd->subdev, fh.state,
4447 V4L2_SUBDEV_FORMAT_ACTIVE,
4448 ATOMISP_SUBDEV_PAD_SOURCE, &isp_source_fmt);
4450 if (!atomisp_subdev_format_conversion(asd)) {
4455 atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height,
4456 &dvs_env_w, &dvs_env_h);
4458 ret = atomisp_set_fmt_to_snr(vdev, &f->fmt.pix,
4459 padding_w, padding_h, dvs_env_w, dvs_env_h);
4462 "Set format to sensor failed with %d\n", ret);
4466 atomisp_csi_lane_config(isp);
4468 atomisp_check_copy_mode(asd, &f->fmt.pix);
4470 isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL,
4471 V4L2_SUBDEV_FORMAT_ACTIVE,
4472 ATOMISP_SUBDEV_PAD_SINK,
4475 /* Try to enable YUV downscaling if ISP input is 10 % (either
4476 * width or height) bigger than the desired result. */
4477 if (isp_sink_crop.width * 9 / 10 < f->fmt.pix.width ||
4478 isp_sink_crop.height * 9 / 10 < f->fmt.pix.height ||
4479 (atomisp_subdev_format_conversion(asd) &&
4480 (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
4481 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) {
4482 isp_sink_crop.width = f->fmt.pix.width;
4483 isp_sink_crop.height = f->fmt.pix.height;
4485 atomisp_subdev_set_selection(&asd->subdev, fh.state,
4486 V4L2_SUBDEV_FORMAT_ACTIVE,
4487 ATOMISP_SUBDEV_PAD_SINK,
4489 V4L2_SEL_FLAG_KEEP_CONFIG,
4491 atomisp_subdev_set_selection(&asd->subdev, fh.state,
4492 V4L2_SUBDEV_FORMAT_ACTIVE,
4493 ATOMISP_SUBDEV_PAD_SOURCE, V4L2_SEL_TGT_COMPOSE,
4495 } else if (IS_MOFD) {
4496 struct v4l2_rect main_compose = {0};
4498 main_compose.width = isp_sink_crop.width;
4499 main_compose.height =
4500 DIV_ROUND_UP(main_compose.width * f->fmt.pix.height,
4502 if (main_compose.height > isp_sink_crop.height) {
4503 main_compose.height = isp_sink_crop.height;
4504 main_compose.width =
4505 DIV_ROUND_UP(main_compose.height *
4510 atomisp_subdev_set_selection(&asd->subdev, fh.state,
4511 V4L2_SUBDEV_FORMAT_ACTIVE,
4512 ATOMISP_SUBDEV_PAD_SOURCE,
4513 V4L2_SEL_TGT_COMPOSE, 0,
4516 struct v4l2_rect sink_crop = {0};
4517 struct v4l2_rect main_compose = {0};
4519 main_compose.width = f->fmt.pix.width;
4520 main_compose.height = f->fmt.pix.height;
4522 /* WORKAROUND: this override is universally enabled in
4523 * GMIN to work around a CTS failures (GMINL-539)
4524 * which appears to be related by a hardware
4525 * performance limitation. It's unclear why this
4526 * particular code triggers the issue. */
4527 if (isp_sink_crop.width * main_compose.height >
4528 isp_sink_crop.height * main_compose.width) {
4529 sink_crop.height = isp_sink_crop.height;
4531 DIV_NEAREST_STEP(sink_crop.height * f->fmt.pix.width,
4533 ATOM_ISP_STEP_WIDTH);
4535 sink_crop.width = isp_sink_crop.width;
4537 DIV_NEAREST_STEP(sink_crop.width * f->fmt.pix.height,
4539 ATOM_ISP_STEP_HEIGHT);
4541 atomisp_subdev_set_selection(&asd->subdev, fh.state,
4542 V4L2_SUBDEV_FORMAT_ACTIVE,
4543 ATOMISP_SUBDEV_PAD_SINK,
4545 V4L2_SEL_FLAG_KEEP_CONFIG,
4548 atomisp_subdev_set_selection(&asd->subdev, fh.state,
4549 V4L2_SUBDEV_FORMAT_ACTIVE,
4550 ATOMISP_SUBDEV_PAD_SOURCE,
4551 V4L2_SEL_TGT_COMPOSE, 0,
4555 ret = atomisp_set_fmt_to_isp(vdev, &output_info, &f->fmt.pix);
4557 dev_warn(isp->dev, "Can't set format on ISP. Error %d\n", ret);
4561 pipe->pix.width = f->fmt.pix.width;
4562 pipe->pix.height = f->fmt.pix.height;
4563 pipe->pix.pixelformat = f->fmt.pix.pixelformat;
4565 * FIXME: do we need to setup this differently, depending on the
4566 * sensor or the pipeline?
4568 pipe->pix.colorspace = V4L2_COLORSPACE_REC709;
4569 pipe->pix.ycbcr_enc = V4L2_YCBCR_ENC_709;
4570 pipe->pix.xfer_func = V4L2_XFER_FUNC_709;
4572 if (format_bridge->planar) {
4573 pipe->pix.bytesperline = output_info.padded_width;
4574 pipe->pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height *
4575 DIV_ROUND_UP(format_bridge->depth *
4576 output_info.padded_width, 8));
4578 pipe->pix.bytesperline =
4579 DIV_ROUND_UP(format_bridge->depth *
4580 output_info.padded_width, 8);
4581 pipe->pix.sizeimage =
4582 PAGE_ALIGN(f->fmt.pix.height * pipe->pix.bytesperline);
4584 dev_dbg(isp->dev, "%s: image size: %d, %d bytes per line\n",
4585 __func__, pipe->pix.sizeimage, pipe->pix.bytesperline);
4587 if (f->fmt.pix.field == V4L2_FIELD_ANY)
4588 f->fmt.pix.field = V4L2_FIELD_NONE;
4589 pipe->pix.field = f->fmt.pix.field;
4591 f->fmt.pix = pipe->pix;
4592 f->fmt.pix.priv = PAGE_ALIGN(pipe->pix.width *
4593 pipe->pix.height * 2);
4594 /* Report the needed sizes */
4595 f->fmt.pix.sizeimage = pipe->pix.sizeimage;
4596 f->fmt.pix.bytesperline = pipe->pix.bytesperline;
4598 dev_dbg(isp->dev, "%s: %dx%d, image size: %d, %d bytes per line\n",
4600 f->fmt.pix.width, f->fmt.pix.height,
4601 f->fmt.pix.sizeimage, f->fmt.pix.bytesperline);
4606 int atomisp_set_shading_table(struct atomisp_sub_device *asd,
4607 struct atomisp_shading_table *user_shading_table)
4609 struct ia_css_shading_table *shading_table;
4610 struct ia_css_shading_table *free_table;
4611 unsigned int len_table;
4615 if (!user_shading_table)
4618 if (!user_shading_table->enable) {
4619 asd->params.config.shading_table = NULL;
4620 asd->params.sc_en = false;
4624 /* If enabling, all tables must be set */
4625 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
4626 if (!user_shading_table->data[i])
4630 /* Shading table size per color */
4631 if (user_shading_table->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
4632 user_shading_table->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
4635 shading_table = atomisp_css_shading_table_alloc(
4636 user_shading_table->width, user_shading_table->height);
4640 len_table = user_shading_table->width * user_shading_table->height *
4641 ATOMISP_SC_TYPE_SIZE;
4642 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
4643 ret = copy_from_user(shading_table->data[i],
4644 (void __user *)user_shading_table->data[i],
4647 free_table = shading_table;
4652 shading_table->sensor_width = user_shading_table->sensor_width;
4653 shading_table->sensor_height = user_shading_table->sensor_height;
4654 shading_table->fraction_bits = user_shading_table->fraction_bits;
4656 free_table = asd->params.css_param.shading_table;
4657 asd->params.css_param.shading_table = shading_table;
4658 asd->params.config.shading_table = shading_table;
4659 asd->params.sc_en = true;
4663 atomisp_css_shading_table_free(free_table);
4669 * set auto exposure metering window to camera sensor
4671 int atomisp_s_ae_window(struct atomisp_sub_device *asd,
4672 struct atomisp_ae_window *arg)
4674 struct atomisp_device *isp = asd->isp;
4675 /* Coverity CID 298071 - initialzize struct */
4676 struct v4l2_subdev_selection sel = { 0 };
4678 sel.r.left = arg->x_left;
4679 sel.r.top = arg->y_top;
4680 sel.r.width = arg->x_right - arg->x_left + 1;
4681 sel.r.height = arg->y_bottom - arg->y_top + 1;
4683 if (v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
4684 pad, set_selection, NULL, &sel)) {
4685 dev_err(isp->dev, "failed to call sensor set_selection.\n");
4692 int atomisp_flash_enable(struct atomisp_sub_device *asd, int num_frames)
4694 struct atomisp_device *isp = asd->isp;
4696 if (num_frames < 0) {
4697 dev_dbg(isp->dev, "%s ERROR: num_frames: %d\n", __func__,
4701 /* a requested flash is still in progress. */
4702 if (num_frames && asd->params.flash_state != ATOMISP_FLASH_IDLE) {
4703 dev_dbg(isp->dev, "%s flash busy: %d frames left: %d\n",
4704 __func__, asd->params.flash_state,
4705 asd->params.num_flash_frames);
4709 asd->params.num_flash_frames = num_frames;
4710 asd->params.flash_state = ATOMISP_FLASH_REQUESTED;
4714 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id)
4716 struct atomisp_device *isp = asd->isp;
4718 if (!asd->enable_raw_buffer_lock->val) {
4719 dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__);
4722 if (!asd->streaming) {
4723 dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n",
4724 __func__, exp_id, asd->streaming);
4727 if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) {
4728 dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id);
4734 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd)
4736 unsigned long flags;
4738 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
4739 memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap));
4740 asd->raw_buffer_locked_count = 0;
4741 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
4744 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id)
4747 unsigned long flags;
4750 if (__checking_exp_id(asd, exp_id))
4753 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
4755 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
4756 ret = ((*bitmap) & (1 << bit));
4757 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
4761 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
4764 unsigned long flags;
4766 if (__is_raw_buffer_locked(asd, exp_id))
4769 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
4771 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
4772 (*bitmap) &= ~(1 << bit);
4773 asd->raw_buffer_locked_count--;
4774 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
4776 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
4777 __func__, exp_id, asd->raw_buffer_locked_count);
4781 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id)
4783 struct atomisp_device *isp = asd->isp;
4784 int value = *exp_id;
4787 lockdep_assert_held(&isp->mutex);
4789 ret = __is_raw_buffer_locked(asd, value);
4791 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
4795 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
4796 ret = atomisp_css_exp_id_capture(asd, value);
4798 dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value);
4804 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id)
4806 struct atomisp_device *isp = asd->isp;
4807 int value = *exp_id;
4810 lockdep_assert_held(&isp->mutex);
4812 ret = __clear_raw_buffer_bitmap(asd, value);
4814 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
4818 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
4819 ret = atomisp_css_exp_id_unlock(asd, value);
4821 dev_err(isp->dev, "%s exp_id %d failed, err %d.\n",
4822 __func__, value, ret);
4827 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd,
4828 unsigned int *enable)
4835 value = *enable > 0;
4837 atomisp_en_dz_capt_pipe(asd, value);
4842 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event)
4844 if (!event || !asd->streaming)
4847 lockdep_assert_held(&asd->isp->mutex);
4849 dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n",
4853 case V4L2_EVENT_FRAME_SYNC:
4854 atomisp_sof_event(asd);
4856 case V4L2_EVENT_FRAME_END:
4857 atomisp_eof_event(asd, 0);
4859 case V4L2_EVENT_ATOMISP_3A_STATS_READY:
4860 atomisp_3a_stats_ready_event(asd, 0);
4862 case V4L2_EVENT_ATOMISP_METADATA_READY:
4863 atomisp_metadata_ready_event(asd, 0);
4872 static int atomisp_get_pipe_id(struct atomisp_video_pipe *pipe)
4874 struct atomisp_sub_device *asd = pipe->asd;
4877 dev_err(pipe->isp->dev, "%s(): asd is NULL, device is %s\n",
4878 __func__, pipe->vdev.name);
4882 if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4883 return IA_CSS_PIPE_ID_VIDEO;
4884 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
4885 return IA_CSS_PIPE_ID_CAPTURE;
4886 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4887 return IA_CSS_PIPE_ID_VIDEO;
4888 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
4889 return IA_CSS_PIPE_ID_PREVIEW;
4890 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_STILL_CAPTURE) {
4892 return IA_CSS_PIPE_ID_COPY;
4894 return IA_CSS_PIPE_ID_CAPTURE;
4898 dev_warn(asd->isp->dev, "%s failed to find proper pipe\n",
4900 return IA_CSS_PIPE_ID_CAPTURE;
4903 int atomisp_get_invalid_frame_num(struct video_device *vdev,
4904 int *invalid_frame_num)
4906 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4907 struct atomisp_sub_device *asd = pipe->asd;
4908 enum ia_css_pipe_id pipe_id;
4909 struct ia_css_pipe_info p_info;
4912 pipe_id = atomisp_get_pipe_id(pipe);
4913 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipes[pipe_id]) {
4914 dev_warn(asd->isp->dev,
4915 "%s pipe %d has not been created yet, do SET_FMT first!\n",
4920 ret = ia_css_pipe_get_info(
4921 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
4922 .pipes[pipe_id], &p_info);
4924 *invalid_frame_num = p_info.num_invalid_frames;
4927 dev_warn(asd->isp->dev, "%s get pipe infor failed %d\n",