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_state_active(isp->inputs[asd->input_curr].camera,
109 pad, get_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 * Function to enable/disable lens geometry distortion correction (GDC) and
1253 * chromatic aberration correction (CAC)
1255 int atomisp_gdc_cac(struct atomisp_sub_device *asd, int flag,
1259 *value = asd->params.gdc_cac_en;
1263 asd->params.gdc_cac_en = !!*value;
1264 if (asd->params.gdc_cac_en) {
1265 asd->params.config.morph_table = asd->params.css_param.morph_table;
1267 asd->params.config.morph_table = NULL;
1269 asd->params.css_update_params_needed = true;
1270 atomisp_update_capture_mode(asd);
1275 * Function to enable/disable low light mode including ANR
1277 int atomisp_low_light(struct atomisp_sub_device *asd, int flag,
1281 *value = asd->params.low_light;
1285 asd->params.low_light = (*value != 0);
1286 atomisp_update_capture_mode(asd);
1291 * Function to enable/disable extra noise reduction (XNR) in low light
1294 int atomisp_xnr(struct atomisp_sub_device *asd, int flag,
1298 *xnr_enable = asd->params.xnr_en;
1302 atomisp_css_capture_enable_xnr(asd, !!*xnr_enable);
1308 * Function to configure bayer noise reduction
1310 int atomisp_nr(struct atomisp_sub_device *asd, int flag,
1311 struct atomisp_nr_config *arg)
1314 /* Get nr config from current setup */
1315 if (atomisp_css_get_nr_config(asd, arg))
1318 /* Set nr config to isp parameters */
1319 memcpy(&asd->params.css_param.nr_config, arg,
1320 sizeof(struct ia_css_nr_config));
1321 asd->params.config.nr_config = &asd->params.css_param.nr_config;
1322 asd->params.css_update_params_needed = true;
1328 * Function to configure temporal noise reduction (TNR)
1330 int atomisp_tnr(struct atomisp_sub_device *asd, int flag,
1331 struct atomisp_tnr_config *config)
1333 /* Get tnr config from current setup */
1335 /* Get tnr config from current setup */
1336 if (atomisp_css_get_tnr_config(asd, config))
1339 /* Set tnr config to isp parameters */
1340 memcpy(&asd->params.css_param.tnr_config, config,
1341 sizeof(struct ia_css_tnr_config));
1342 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
1343 asd->params.css_update_params_needed = true;
1350 * Function to configure black level compensation
1352 int atomisp_black_level(struct atomisp_sub_device *asd, int flag,
1353 struct atomisp_ob_config *config)
1356 /* Get ob config from current setup */
1357 if (atomisp_css_get_ob_config(asd, config))
1360 /* Set ob config to isp parameters */
1361 memcpy(&asd->params.css_param.ob_config, config,
1362 sizeof(struct ia_css_ob_config));
1363 asd->params.config.ob_config = &asd->params.css_param.ob_config;
1364 asd->params.css_update_params_needed = true;
1371 * Function to configure edge enhancement
1373 int atomisp_ee(struct atomisp_sub_device *asd, int flag,
1374 struct atomisp_ee_config *config)
1377 /* Get ee config from current setup */
1378 if (atomisp_css_get_ee_config(asd, config))
1381 /* Set ee config to isp parameters */
1382 memcpy(&asd->params.css_param.ee_config, config,
1383 sizeof(asd->params.css_param.ee_config));
1384 asd->params.config.ee_config = &asd->params.css_param.ee_config;
1385 asd->params.css_update_params_needed = true;
1392 * Function to update Gamma table for gamma, brightness and contrast config
1394 int atomisp_gamma(struct atomisp_sub_device *asd, int flag,
1395 struct atomisp_gamma_table *config)
1398 /* Get gamma table from current setup */
1399 if (atomisp_css_get_gamma_table(asd, config))
1402 /* Set gamma table to isp parameters */
1403 memcpy(&asd->params.css_param.gamma_table, config,
1404 sizeof(asd->params.css_param.gamma_table));
1405 asd->params.config.gamma_table = &asd->params.css_param.gamma_table;
1412 * Function to update Ctc table for Chroma Enhancement
1414 int atomisp_ctc(struct atomisp_sub_device *asd, int flag,
1415 struct atomisp_ctc_table *config)
1418 /* Get ctc table from current setup */
1419 if (atomisp_css_get_ctc_table(asd, config))
1422 /* Set ctc table to isp parameters */
1423 memcpy(&asd->params.css_param.ctc_table, config,
1424 sizeof(asd->params.css_param.ctc_table));
1425 atomisp_css_set_ctc_table(asd, &asd->params.css_param.ctc_table);
1432 * Function to update gamma correction parameters
1434 int atomisp_gamma_correction(struct atomisp_sub_device *asd, int flag,
1435 struct atomisp_gc_config *config)
1438 /* Get gamma correction params from current setup */
1439 if (atomisp_css_get_gc_config(asd, config))
1442 /* Set gamma correction params to isp parameters */
1443 memcpy(&asd->params.css_param.gc_config, config,
1444 sizeof(asd->params.css_param.gc_config));
1445 asd->params.config.gc_config = &asd->params.css_param.gc_config;
1446 asd->params.css_update_params_needed = true;
1453 * Function to update narrow gamma flag
1455 int atomisp_formats(struct atomisp_sub_device *asd, int flag,
1456 struct atomisp_formats_config *config)
1459 /* Get narrow gamma flag from current setup */
1460 if (atomisp_css_get_formats_config(asd, config))
1463 /* Set narrow gamma flag to isp parameters */
1464 memcpy(&asd->params.css_param.formats_config, config,
1465 sizeof(asd->params.css_param.formats_config));
1466 asd->params.config.formats_config = &asd->params.css_param.formats_config;
1472 void atomisp_free_internal_buffers(struct atomisp_sub_device *asd)
1474 atomisp_free_css_parameters(&asd->params.css_param);
1477 static void atomisp_update_grid_info(struct atomisp_sub_device *asd,
1478 enum ia_css_pipe_id pipe_id)
1480 struct atomisp_device *isp = asd->isp;
1483 if (atomisp_css_get_grid_info(asd, pipe_id))
1486 /* We must free all buffers because they no longer match
1488 atomisp_css_free_stat_buffers(asd);
1490 err = atomisp_alloc_css_stat_bufs(asd, ATOMISP_INPUT_STREAM_GENERAL);
1492 dev_err(isp->dev, "stat_buf allocate error\n");
1496 if (atomisp_alloc_3a_output_buf(asd)) {
1497 /* Failure for 3A buffers does not influence DIS buffers */
1498 if (asd->params.s3a_output_bytes != 0) {
1499 /* For SOC sensor happens s3a_output_bytes == 0,
1500 * using if condition to exclude false error log */
1501 dev_err(isp->dev, "Failed to allocate memory for 3A statistics\n");
1506 if (atomisp_alloc_dis_coef_buf(asd)) {
1508 "Failed to allocate memory for DIS statistics\n");
1512 if (atomisp_alloc_metadata_output_buf(asd)) {
1513 dev_err(isp->dev, "Failed to allocate memory for metadata\n");
1520 atomisp_css_free_stat_buffers(asd);
1524 static void atomisp_curr_user_grid_info(struct atomisp_sub_device *asd,
1525 struct atomisp_grid_info *info)
1527 memcpy(info, &asd->params.curr_grid_info.s3a_grid,
1528 sizeof(struct ia_css_3a_grid_info));
1531 int atomisp_compare_grid(struct atomisp_sub_device *asd,
1532 struct atomisp_grid_info *atomgrid)
1534 struct atomisp_grid_info tmp = {0};
1536 atomisp_curr_user_grid_info(asd, &tmp);
1537 return memcmp(atomgrid, &tmp, sizeof(tmp));
1541 * Function to update Gdc table for gdc
1543 int atomisp_gdc_cac_table(struct atomisp_sub_device *asd, int flag,
1544 struct atomisp_morph_table *config)
1548 struct atomisp_device *isp = asd->isp;
1551 /* Get gdc table from current setup */
1552 struct ia_css_morph_table tab = {0};
1554 atomisp_css_get_morph_table(asd, &tab);
1556 config->width = tab.width;
1557 config->height = tab.height;
1559 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1560 ret = copy_to_user(config->coordinates_x[i],
1561 tab.coordinates_x[i], tab.height *
1562 tab.width * sizeof(*tab.coordinates_x[i]));
1565 "Failed to copy to User for x\n");
1568 ret = copy_to_user(config->coordinates_y[i],
1569 tab.coordinates_y[i], tab.height *
1570 tab.width * sizeof(*tab.coordinates_y[i]));
1573 "Failed to copy to User for y\n");
1578 struct ia_css_morph_table *tab =
1579 asd->params.css_param.morph_table;
1581 /* free first if we have one */
1583 atomisp_css_morph_table_free(tab);
1584 asd->params.css_param.morph_table = NULL;
1587 /* allocate new one */
1588 tab = atomisp_css_morph_table_allocate(config->width,
1592 dev_err(isp->dev, "out of memory\n");
1596 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1597 ret = copy_from_user(tab->coordinates_x[i],
1598 config->coordinates_x[i],
1599 config->height * config->width *
1600 sizeof(*config->coordinates_x[i]));
1603 "Failed to copy from User for x, ret %d\n",
1605 atomisp_css_morph_table_free(tab);
1608 ret = copy_from_user(tab->coordinates_y[i],
1609 config->coordinates_y[i],
1610 config->height * config->width *
1611 sizeof(*config->coordinates_y[i]));
1614 "Failed to copy from User for y, ret is %d\n",
1616 atomisp_css_morph_table_free(tab);
1620 asd->params.css_param.morph_table = tab;
1621 if (asd->params.gdc_cac_en)
1622 asd->params.config.morph_table = tab;
1628 int atomisp_macc_table(struct atomisp_sub_device *asd, int flag,
1629 struct atomisp_macc_config *config)
1631 struct ia_css_macc_table *macc_table;
1633 switch (config->color_effect) {
1634 case V4L2_COLORFX_NONE:
1635 macc_table = &asd->params.css_param.macc_table;
1637 case V4L2_COLORFX_SKY_BLUE:
1638 macc_table = &blue_macc_table;
1640 case V4L2_COLORFX_GRASS_GREEN:
1641 macc_table = &green_macc_table;
1643 case V4L2_COLORFX_SKIN_WHITEN_LOW:
1644 macc_table = &skin_low_macc_table;
1646 case V4L2_COLORFX_SKIN_WHITEN:
1647 macc_table = &skin_medium_macc_table;
1649 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
1650 macc_table = &skin_high_macc_table;
1657 /* Get macc table from current setup */
1658 memcpy(&config->table, macc_table,
1659 sizeof(struct ia_css_macc_table));
1661 memcpy(macc_table, &config->table,
1662 sizeof(struct ia_css_macc_table));
1663 if (config->color_effect == asd->params.color_effect)
1664 asd->params.config.macc_table = macc_table;
1670 int atomisp_set_dis_vector(struct atomisp_sub_device *asd,
1671 struct atomisp_dis_vector *vector)
1673 atomisp_css_video_set_dis_vector(asd, vector);
1675 asd->params.dis_proj_data_valid = false;
1676 asd->params.css_update_params_needed = true;
1681 * Function to set/get image stablization statistics
1683 int atomisp_get_dis_stat(struct atomisp_sub_device *asd,
1684 struct atomisp_dis_statistics *stats)
1686 return atomisp_css_get_dis_stat(asd, stats);
1690 * Function set camrea_prefiles.xml current sensor pixel array size
1692 int atomisp_set_array_res(struct atomisp_sub_device *asd,
1693 struct atomisp_resolution *config)
1695 dev_dbg(asd->isp->dev, ">%s start\n", __func__);
1697 dev_err(asd->isp->dev, "Set sensor array size is not valid\n");
1701 asd->sensor_array_res.width = config->width;
1702 asd->sensor_array_res.height = config->height;
1707 * Function to get DVS2 BQ resolution settings
1709 int atomisp_get_dvs2_bq_resolutions(struct atomisp_sub_device *asd,
1710 struct atomisp_dvs2_bq_resolutions *bq_res)
1712 struct ia_css_pipe_config *pipe_cfg = NULL;
1714 struct ia_css_stream *stream =
1715 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
1717 dev_warn(asd->isp->dev, "stream is not created");
1721 pipe_cfg = &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL]
1722 .pipe_configs[IA_CSS_PIPE_ID_VIDEO];
1727 /* the GDC output resolution */
1728 bq_res->output_bq.width_bq = pipe_cfg->output_info[0].res.width / 2;
1729 bq_res->output_bq.height_bq = pipe_cfg->output_info[0].res.height / 2;
1731 bq_res->envelope_bq.width_bq = 0;
1732 bq_res->envelope_bq.height_bq = 0;
1733 /* the GDC input resolution */
1734 bq_res->source_bq.width_bq = bq_res->output_bq.width_bq +
1735 pipe_cfg->dvs_envelope.width / 2;
1736 bq_res->source_bq.height_bq = bq_res->output_bq.height_bq +
1737 pipe_cfg->dvs_envelope.height / 2;
1739 * Bad pixels caused by spatial filter processing
1740 * ISP filter resolution should be given by CSS/FW, but for now
1741 * there is not such API to query, and it is fixed value, so
1744 bq_res->ispfilter_bq.width_bq = 12 / 2;
1745 bq_res->ispfilter_bq.height_bq = 12 / 2;
1746 /* spatial filter shift, always 4 pixels */
1747 bq_res->gdc_shift_bq.width_bq = 4 / 2;
1748 bq_res->gdc_shift_bq.height_bq = 4 / 2;
1750 if (asd->params.video_dis_en) {
1751 bq_res->envelope_bq.width_bq = pipe_cfg->dvs_envelope.width / 2 -
1752 bq_res->ispfilter_bq.width_bq;
1753 bq_res->envelope_bq.height_bq = pipe_cfg->dvs_envelope.height / 2 -
1754 bq_res->ispfilter_bq.height_bq;
1757 dev_dbg(asd->isp->dev,
1758 "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",
1759 bq_res->source_bq.width_bq, bq_res->source_bq.height_bq,
1760 bq_res->ispfilter_bq.width_bq, bq_res->ispfilter_bq.height_bq,
1761 bq_res->gdc_shift_bq.width_bq, bq_res->gdc_shift_bq.height_bq,
1762 bq_res->envelope_bq.width_bq, bq_res->envelope_bq.height_bq,
1763 bq_res->output_bq.width_bq, bq_res->output_bq.height_bq);
1768 int atomisp_set_dis_coefs(struct atomisp_sub_device *asd,
1769 struct atomisp_dis_coefficients *coefs)
1771 return atomisp_css_set_dis_coefs(asd, coefs);
1775 * Function to set/get 3A stat from isp
1777 int atomisp_3a_stat(struct atomisp_sub_device *asd, int flag,
1778 struct atomisp_3a_statistics *config)
1780 struct atomisp_device *isp = asd->isp;
1781 struct atomisp_s3a_buf *s3a_buf;
1787 /* sanity check to avoid writing into unallocated memory. */
1788 if (asd->params.s3a_output_bytes == 0)
1791 if (atomisp_compare_grid(asd, &config->grid_info) != 0) {
1792 /* If the grid info in the argument differs from the current
1793 grid info, we tell the caller to reset the grid size and
1798 if (list_empty(&asd->s3a_stats_ready)) {
1799 dev_err(isp->dev, "3a statistics is not valid.\n");
1803 s3a_buf = list_entry(asd->s3a_stats_ready.next,
1804 struct atomisp_s3a_buf, list);
1805 if (s3a_buf->s3a_map)
1806 ia_css_translate_3a_statistics(
1807 asd->params.s3a_user_stat, s3a_buf->s3a_map);
1809 ia_css_get_3a_statistics(asd->params.s3a_user_stat,
1812 config->exp_id = s3a_buf->s3a_data->exp_id;
1813 config->isp_config_id = s3a_buf->s3a_data->isp_config_id;
1815 ret = copy_to_user(config->data, asd->params.s3a_user_stat->data,
1816 asd->params.s3a_output_bytes);
1818 dev_err(isp->dev, "copy to user failed: copied %lu bytes\n",
1823 /* Move to free buffer list */
1824 list_del_init(&s3a_buf->list);
1825 list_add_tail(&s3a_buf->list, &asd->s3a_stats);
1826 dev_dbg(isp->dev, "%s: finish getting exp_id %d 3a stat, isp_config_id %d\n",
1828 config->exp_id, config->isp_config_id);
1833 * Function to calculate real zoom region for every pipe
1835 int atomisp_calculate_real_zoom_region(struct atomisp_sub_device *asd,
1836 struct ia_css_dz_config *dz_config,
1837 enum ia_css_pipe_id css_pipe_id)
1840 struct atomisp_stream_env *stream_env =
1841 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL];
1842 struct atomisp_resolution eff_res, out_res;
1843 int w_offset, h_offset;
1845 memset(&eff_res, 0, sizeof(eff_res));
1846 memset(&out_res, 0, sizeof(out_res));
1848 if (dz_config->dx || dz_config->dy)
1851 if (css_pipe_id != IA_CSS_PIPE_ID_PREVIEW
1852 && css_pipe_id != IA_CSS_PIPE_ID_CAPTURE) {
1853 dev_err(asd->isp->dev, "%s the set pipe no support crop region"
1859 stream_env->stream_config.input_config.effective_res.width;
1861 stream_env->stream_config.input_config.effective_res.height;
1862 if (eff_res.width == 0 || eff_res.height == 0) {
1863 dev_err(asd->isp->dev, "%s err effective resolution"
1868 if (dz_config->zoom_region.resolution.width
1869 == asd->sensor_array_res.width
1870 || dz_config->zoom_region.resolution.height
1871 == asd->sensor_array_res.height) {
1872 /*no need crop region*/
1873 dz_config->zoom_region.origin.x = 0;
1874 dz_config->zoom_region.origin.y = 0;
1875 dz_config->zoom_region.resolution.width = eff_res.width;
1876 dz_config->zoom_region.resolution.height = eff_res.height;
1881 * This is not the correct implementation with Google's definition, due
1882 * to firmware limitation.
1883 * map real crop region base on above calculating base max crop region.
1887 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
1889 / asd->sensor_array_res.width;
1890 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
1892 / asd->sensor_array_res.height;
1893 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
1895 / asd->sensor_array_res.width;
1896 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
1898 / asd->sensor_array_res.height;
1900 * Set same ratio of crop region resolution and current pipe output
1903 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
1904 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
1905 if (out_res.width == 0 || out_res.height == 0) {
1906 dev_err(asd->isp->dev, "%s err current pipe output resolution"
1911 out_res.width = stream_env->pipe_configs[css_pipe_id].output_info[0].res.width;
1912 out_res.height = stream_env->pipe_configs[css_pipe_id].output_info[0].res.height;
1913 if (out_res.width == 0 || out_res.height == 0) {
1914 dev_err(asd->isp->dev, "%s err current pipe output resolution"
1919 if (asd->sensor_array_res.width * out_res.height
1920 < out_res.width * asd->sensor_array_res.height) {
1921 h_offset = asd->sensor_array_res.height
1922 - asd->sensor_array_res.width
1923 * out_res.height / out_res.width;
1924 h_offset = h_offset / 2;
1925 if (dz_config->zoom_region.origin.y < h_offset)
1926 dz_config->zoom_region.origin.y = 0;
1928 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y - h_offset;
1931 w_offset = asd->sensor_array_res.width
1932 - asd->sensor_array_res.height
1933 * out_res.width / out_res.height;
1934 w_offset = w_offset / 2;
1935 if (dz_config->zoom_region.origin.x < w_offset)
1936 dz_config->zoom_region.origin.x = 0;
1938 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x - w_offset;
1941 dz_config->zoom_region.origin.x = dz_config->zoom_region.origin.x
1943 / (asd->sensor_array_res.width - 2 * w_offset);
1944 dz_config->zoom_region.origin.y = dz_config->zoom_region.origin.y
1946 / (asd->sensor_array_res.height - 2 * h_offset);
1947 dz_config->zoom_region.resolution.width = dz_config->zoom_region.resolution.width
1949 / (asd->sensor_array_res.width - 2 * w_offset);
1950 dz_config->zoom_region.resolution.height = dz_config->zoom_region.resolution.height
1952 / (asd->sensor_array_res.height - 2 * h_offset);
1955 if (out_res.width * dz_config->zoom_region.resolution.height
1956 > dz_config->zoom_region.resolution.width * out_res.height) {
1957 dz_config->zoom_region.resolution.height =
1958 dz_config->zoom_region.resolution.width
1959 * out_res.height / out_res.width;
1961 dz_config->zoom_region.resolution.width =
1962 dz_config->zoom_region.resolution.height
1963 * out_res.width / out_res.height;
1965 dev_dbg(asd->isp->dev,
1966 "%s crop region:(%d,%d),(%d,%d) eff_res(%d, %d) array_size(%d,%d) out_res(%d, %d)\n",
1967 __func__, dz_config->zoom_region.origin.x,
1968 dz_config->zoom_region.origin.y,
1969 dz_config->zoom_region.resolution.width,
1970 dz_config->zoom_region.resolution.height,
1971 eff_res.width, eff_res.height,
1972 asd->sensor_array_res.width,
1973 asd->sensor_array_res.height,
1974 out_res.width, out_res.height);
1976 if ((dz_config->zoom_region.origin.x +
1977 dz_config->zoom_region.resolution.width
1979 (dz_config->zoom_region.origin.y +
1980 dz_config->zoom_region.resolution.height
1988 * Function to check the zoom region whether is effective
1990 static bool atomisp_check_zoom_region(
1991 struct atomisp_sub_device *asd,
1992 struct ia_css_dz_config *dz_config)
1994 struct atomisp_resolution config;
1998 memset(&config, 0, sizeof(struct atomisp_resolution));
2000 if (dz_config->dx && dz_config->dy)
2003 config.width = asd->sensor_array_res.width;
2004 config.height = asd->sensor_array_res.height;
2005 w = dz_config->zoom_region.origin.x +
2006 dz_config->zoom_region.resolution.width;
2007 h = dz_config->zoom_region.origin.y +
2008 dz_config->zoom_region.resolution.height;
2010 if ((w <= config.width) && (h <= config.height) && w > 0 && h > 0)
2013 /* setting error zoom region */
2014 dev_err(asd->isp->dev,
2015 "%s zoom region ERROR:dz_config:(%d,%d),(%d,%d)array_res(%d, %d)\n",
2016 __func__, dz_config->zoom_region.origin.x,
2017 dz_config->zoom_region.origin.y,
2018 dz_config->zoom_region.resolution.width,
2019 dz_config->zoom_region.resolution.height,
2020 config.width, config.height);
2025 void atomisp_apply_css_parameters(
2026 struct atomisp_sub_device *asd,
2027 struct atomisp_css_params *css_param)
2029 if (css_param->update_flag.wb_config)
2030 asd->params.config.wb_config = &css_param->wb_config;
2032 if (css_param->update_flag.ob_config)
2033 asd->params.config.ob_config = &css_param->ob_config;
2035 if (css_param->update_flag.dp_config)
2036 asd->params.config.dp_config = &css_param->dp_config;
2038 if (css_param->update_flag.nr_config)
2039 asd->params.config.nr_config = &css_param->nr_config;
2041 if (css_param->update_flag.ee_config)
2042 asd->params.config.ee_config = &css_param->ee_config;
2044 if (css_param->update_flag.tnr_config)
2045 asd->params.config.tnr_config = &css_param->tnr_config;
2047 if (css_param->update_flag.a3a_config)
2048 asd->params.config.s3a_config = &css_param->s3a_config;
2050 if (css_param->update_flag.ctc_config)
2051 asd->params.config.ctc_config = &css_param->ctc_config;
2053 if (css_param->update_flag.cnr_config)
2054 asd->params.config.cnr_config = &css_param->cnr_config;
2056 if (css_param->update_flag.ecd_config)
2057 asd->params.config.ecd_config = &css_param->ecd_config;
2059 if (css_param->update_flag.ynr_config)
2060 asd->params.config.ynr_config = &css_param->ynr_config;
2062 if (css_param->update_flag.fc_config)
2063 asd->params.config.fc_config = &css_param->fc_config;
2065 if (css_param->update_flag.macc_config)
2066 asd->params.config.macc_config = &css_param->macc_config;
2068 if (css_param->update_flag.aa_config)
2069 asd->params.config.aa_config = &css_param->aa_config;
2071 if (css_param->update_flag.anr_config)
2072 asd->params.config.anr_config = &css_param->anr_config;
2074 if (css_param->update_flag.xnr_config)
2075 asd->params.config.xnr_config = &css_param->xnr_config;
2077 if (css_param->update_flag.yuv2rgb_cc_config)
2078 asd->params.config.yuv2rgb_cc_config = &css_param->yuv2rgb_cc_config;
2080 if (css_param->update_flag.rgb2yuv_cc_config)
2081 asd->params.config.rgb2yuv_cc_config = &css_param->rgb2yuv_cc_config;
2083 if (css_param->update_flag.macc_table)
2084 asd->params.config.macc_table = &css_param->macc_table;
2086 if (css_param->update_flag.xnr_table)
2087 asd->params.config.xnr_table = &css_param->xnr_table;
2089 if (css_param->update_flag.r_gamma_table)
2090 asd->params.config.r_gamma_table = &css_param->r_gamma_table;
2092 if (css_param->update_flag.g_gamma_table)
2093 asd->params.config.g_gamma_table = &css_param->g_gamma_table;
2095 if (css_param->update_flag.b_gamma_table)
2096 asd->params.config.b_gamma_table = &css_param->b_gamma_table;
2098 if (css_param->update_flag.anr_thres)
2099 atomisp_css_set_anr_thres(asd, &css_param->anr_thres);
2101 if (css_param->update_flag.shading_table)
2102 asd->params.config.shading_table = css_param->shading_table;
2104 if (css_param->update_flag.morph_table && asd->params.gdc_cac_en)
2105 asd->params.config.morph_table = css_param->morph_table;
2107 if (css_param->update_flag.dvs2_coefs) {
2108 struct ia_css_dvs_grid_info *dvs_grid_info =
2109 atomisp_css_get_dvs_grid_info(
2110 &asd->params.curr_grid_info);
2112 if (dvs_grid_info && dvs_grid_info->enable)
2113 atomisp_css_set_dvs2_coefs(asd, css_param->dvs2_coeff);
2116 if (css_param->update_flag.dvs_6axis_config)
2117 atomisp_css_set_dvs_6axis(asd, css_param->dvs_6axis);
2119 atomisp_css_set_isp_config_id(asd, css_param->isp_config_id);
2121 * These configurations are on used by ISP1.x, not for ISP2.x,
2122 * so do not handle them. see comments of ia_css_isp_config.
2133 static unsigned int long copy_from_compatible(void *to, const void *from,
2134 unsigned long n, bool from_user)
2137 return copy_from_user(to, (void __user *)from, n);
2139 memcpy(to, from, n);
2143 int atomisp_cp_general_isp_parameters(struct atomisp_sub_device *asd,
2144 struct atomisp_parameters *arg,
2145 struct atomisp_css_params *css_param,
2148 struct atomisp_parameters *cur_config = &css_param->update_flag;
2150 if (!arg || !asd || !css_param)
2153 if (arg->wb_config && (from_user || !cur_config->wb_config)) {
2154 if (copy_from_compatible(&css_param->wb_config, arg->wb_config,
2155 sizeof(struct ia_css_wb_config),
2158 css_param->update_flag.wb_config =
2159 (struct atomisp_wb_config *)&css_param->wb_config;
2162 if (arg->ob_config && (from_user || !cur_config->ob_config)) {
2163 if (copy_from_compatible(&css_param->ob_config, arg->ob_config,
2164 sizeof(struct ia_css_ob_config),
2167 css_param->update_flag.ob_config =
2168 (struct atomisp_ob_config *)&css_param->ob_config;
2171 if (arg->dp_config && (from_user || !cur_config->dp_config)) {
2172 if (copy_from_compatible(&css_param->dp_config, arg->dp_config,
2173 sizeof(struct ia_css_dp_config),
2176 css_param->update_flag.dp_config =
2177 (struct atomisp_dp_config *)&css_param->dp_config;
2180 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
2181 if (arg->dz_config && (from_user || !cur_config->dz_config)) {
2182 if (copy_from_compatible(&css_param->dz_config,
2184 sizeof(struct ia_css_dz_config),
2187 if (!atomisp_check_zoom_region(asd,
2188 &css_param->dz_config)) {
2189 dev_err(asd->isp->dev, "crop region error!");
2192 css_param->update_flag.dz_config =
2193 (struct atomisp_dz_config *)
2194 &css_param->dz_config;
2198 if (arg->nr_config && (from_user || !cur_config->nr_config)) {
2199 if (copy_from_compatible(&css_param->nr_config, arg->nr_config,
2200 sizeof(struct ia_css_nr_config),
2203 css_param->update_flag.nr_config =
2204 (struct atomisp_nr_config *)&css_param->nr_config;
2207 if (arg->ee_config && (from_user || !cur_config->ee_config)) {
2208 if (copy_from_compatible(&css_param->ee_config, arg->ee_config,
2209 sizeof(struct ia_css_ee_config),
2212 css_param->update_flag.ee_config =
2213 (struct atomisp_ee_config *)&css_param->ee_config;
2216 if (arg->tnr_config && (from_user || !cur_config->tnr_config)) {
2217 if (copy_from_compatible(&css_param->tnr_config,
2219 sizeof(struct ia_css_tnr_config),
2222 css_param->update_flag.tnr_config =
2223 (struct atomisp_tnr_config *)
2224 &css_param->tnr_config;
2227 if (arg->a3a_config && (from_user || !cur_config->a3a_config)) {
2228 if (copy_from_compatible(&css_param->s3a_config,
2230 sizeof(struct ia_css_3a_config),
2233 css_param->update_flag.a3a_config =
2234 (struct atomisp_3a_config *)&css_param->s3a_config;
2237 if (arg->ctc_config && (from_user || !cur_config->ctc_config)) {
2238 if (copy_from_compatible(&css_param->ctc_config,
2240 sizeof(struct ia_css_ctc_config),
2243 css_param->update_flag.ctc_config =
2244 (struct atomisp_ctc_config *)
2245 &css_param->ctc_config;
2248 if (arg->cnr_config && (from_user || !cur_config->cnr_config)) {
2249 if (copy_from_compatible(&css_param->cnr_config,
2251 sizeof(struct ia_css_cnr_config),
2254 css_param->update_flag.cnr_config =
2255 (struct atomisp_cnr_config *)
2256 &css_param->cnr_config;
2259 if (arg->ecd_config && (from_user || !cur_config->ecd_config)) {
2260 if (copy_from_compatible(&css_param->ecd_config,
2262 sizeof(struct ia_css_ecd_config),
2265 css_param->update_flag.ecd_config =
2266 (struct atomisp_ecd_config *)
2267 &css_param->ecd_config;
2270 if (arg->ynr_config && (from_user || !cur_config->ynr_config)) {
2271 if (copy_from_compatible(&css_param->ynr_config,
2273 sizeof(struct ia_css_ynr_config),
2276 css_param->update_flag.ynr_config =
2277 (struct atomisp_ynr_config *)
2278 &css_param->ynr_config;
2281 if (arg->fc_config && (from_user || !cur_config->fc_config)) {
2282 if (copy_from_compatible(&css_param->fc_config,
2284 sizeof(struct ia_css_fc_config),
2287 css_param->update_flag.fc_config =
2288 (struct atomisp_fc_config *)&css_param->fc_config;
2291 if (arg->macc_config && (from_user || !cur_config->macc_config)) {
2292 if (copy_from_compatible(&css_param->macc_config,
2294 sizeof(struct ia_css_macc_config),
2297 css_param->update_flag.macc_config =
2298 (struct atomisp_macc_config *)
2299 &css_param->macc_config;
2302 if (arg->aa_config && (from_user || !cur_config->aa_config)) {
2303 if (copy_from_compatible(&css_param->aa_config, arg->aa_config,
2304 sizeof(struct ia_css_aa_config),
2307 css_param->update_flag.aa_config =
2308 (struct atomisp_aa_config *)&css_param->aa_config;
2311 if (arg->anr_config && (from_user || !cur_config->anr_config)) {
2312 if (copy_from_compatible(&css_param->anr_config,
2314 sizeof(struct ia_css_anr_config),
2317 css_param->update_flag.anr_config =
2318 (struct atomisp_anr_config *)
2319 &css_param->anr_config;
2322 if (arg->xnr_config && (from_user || !cur_config->xnr_config)) {
2323 if (copy_from_compatible(&css_param->xnr_config,
2325 sizeof(struct ia_css_xnr_config),
2328 css_param->update_flag.xnr_config =
2329 (struct atomisp_xnr_config *)
2330 &css_param->xnr_config;
2333 if (arg->yuv2rgb_cc_config &&
2334 (from_user || !cur_config->yuv2rgb_cc_config)) {
2335 if (copy_from_compatible(&css_param->yuv2rgb_cc_config,
2336 arg->yuv2rgb_cc_config,
2337 sizeof(struct ia_css_cc_config),
2340 css_param->update_flag.yuv2rgb_cc_config =
2341 (struct atomisp_cc_config *)
2342 &css_param->yuv2rgb_cc_config;
2345 if (arg->rgb2yuv_cc_config &&
2346 (from_user || !cur_config->rgb2yuv_cc_config)) {
2347 if (copy_from_compatible(&css_param->rgb2yuv_cc_config,
2348 arg->rgb2yuv_cc_config,
2349 sizeof(struct ia_css_cc_config),
2352 css_param->update_flag.rgb2yuv_cc_config =
2353 (struct atomisp_cc_config *)
2354 &css_param->rgb2yuv_cc_config;
2357 if (arg->macc_table && (from_user || !cur_config->macc_table)) {
2358 if (copy_from_compatible(&css_param->macc_table,
2360 sizeof(struct ia_css_macc_table),
2363 css_param->update_flag.macc_table =
2364 (struct atomisp_macc_table *)
2365 &css_param->macc_table;
2368 if (arg->xnr_table && (from_user || !cur_config->xnr_table)) {
2369 if (copy_from_compatible(&css_param->xnr_table,
2371 sizeof(struct ia_css_xnr_table),
2374 css_param->update_flag.xnr_table =
2375 (struct atomisp_xnr_table *)&css_param->xnr_table;
2378 if (arg->r_gamma_table && (from_user || !cur_config->r_gamma_table)) {
2379 if (copy_from_compatible(&css_param->r_gamma_table,
2381 sizeof(struct ia_css_rgb_gamma_table),
2384 css_param->update_flag.r_gamma_table =
2385 (struct atomisp_rgb_gamma_table *)
2386 &css_param->r_gamma_table;
2389 if (arg->g_gamma_table && (from_user || !cur_config->g_gamma_table)) {
2390 if (copy_from_compatible(&css_param->g_gamma_table,
2392 sizeof(struct ia_css_rgb_gamma_table),
2395 css_param->update_flag.g_gamma_table =
2396 (struct atomisp_rgb_gamma_table *)
2397 &css_param->g_gamma_table;
2400 if (arg->b_gamma_table && (from_user || !cur_config->b_gamma_table)) {
2401 if (copy_from_compatible(&css_param->b_gamma_table,
2403 sizeof(struct ia_css_rgb_gamma_table),
2406 css_param->update_flag.b_gamma_table =
2407 (struct atomisp_rgb_gamma_table *)
2408 &css_param->b_gamma_table;
2411 if (arg->anr_thres && (from_user || !cur_config->anr_thres)) {
2412 if (copy_from_compatible(&css_param->anr_thres, arg->anr_thres,
2413 sizeof(struct ia_css_anr_thres),
2416 css_param->update_flag.anr_thres =
2417 (struct atomisp_anr_thres *)&css_param->anr_thres;
2421 css_param->isp_config_id = arg->isp_config_id;
2423 * These configurations are on used by ISP1.x, not for ISP2.x,
2424 * so do not handle them. see comments of ia_css_isp_config.
2436 int atomisp_cp_lsc_table(struct atomisp_sub_device *asd,
2437 struct atomisp_shading_table *source_st,
2438 struct atomisp_css_params *css_param,
2442 unsigned int len_table;
2443 struct ia_css_shading_table *shading_table;
2444 struct ia_css_shading_table *old_table;
2445 struct atomisp_shading_table *st, dest_st;
2453 if (!from_user && css_param->update_flag.shading_table)
2457 if (copy_from_compatible(&dest_st, source_st,
2458 sizeof(struct atomisp_shading_table),
2460 dev_err(asd->isp->dev, "copy shading table failed!");
2468 old_table = css_param->shading_table;
2470 /* user config is to disable the shading table. */
2472 /* Generate a minimum table with enable = 0. */
2473 shading_table = atomisp_css_shading_table_alloc(1, 1);
2476 shading_table->enable = 0;
2480 /* Setting a new table. Validate first - all tables must be set */
2481 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
2483 dev_err(asd->isp->dev, "shading table validate failed");
2488 /* Shading table size per color */
2489 if (st->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
2490 st->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR) {
2491 dev_err(asd->isp->dev, "shading table w/h validate failed!");
2495 shading_table = atomisp_css_shading_table_alloc(st->width, st->height);
2499 len_table = st->width * st->height * ATOMISP_SC_TYPE_SIZE;
2500 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
2501 if (copy_from_compatible(shading_table->data[i],
2502 st->data[i], len_table, from_user)) {
2503 atomisp_css_shading_table_free(shading_table);
2507 shading_table->sensor_width = st->sensor_width;
2508 shading_table->sensor_height = st->sensor_height;
2509 shading_table->fraction_bits = st->fraction_bits;
2510 shading_table->enable = st->enable;
2512 /* No need to update shading table if it is the same */
2514 old_table->sensor_width == shading_table->sensor_width &&
2515 old_table->sensor_height == shading_table->sensor_height &&
2516 old_table->width == shading_table->width &&
2517 old_table->height == shading_table->height &&
2518 old_table->fraction_bits == shading_table->fraction_bits &&
2519 old_table->enable == shading_table->enable) {
2520 bool data_is_same = true;
2522 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
2523 if (memcmp(shading_table->data[i], old_table->data[i],
2525 data_is_same = false;
2531 atomisp_css_shading_table_free(shading_table);
2537 /* set LSC to CSS */
2538 css_param->shading_table = shading_table;
2539 css_param->update_flag.shading_table = (struct atomisp_shading_table *)shading_table;
2540 asd->params.sc_en = shading_table;
2543 atomisp_css_shading_table_free(old_table);
2548 int atomisp_css_cp_dvs2_coefs(struct atomisp_sub_device *asd,
2549 struct ia_css_dvs2_coefficients *coefs,
2550 struct atomisp_css_params *css_param,
2553 struct ia_css_dvs_grid_info *cur =
2554 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
2555 int dvs_hor_coef_bytes, dvs_ver_coef_bytes;
2556 struct ia_css_dvs2_coefficients dvs2_coefs;
2561 if (!from_user && css_param->update_flag.dvs2_coefs)
2565 if (sizeof(*cur) != sizeof(coefs->grid) ||
2566 memcmp(&coefs->grid, cur, sizeof(coefs->grid))) {
2567 dev_err(asd->isp->dev, "dvs grid mismatch!\n");
2568 /* If the grid info in the argument differs from the current
2569 grid info, we tell the caller to reset the grid size and
2574 if (!coefs->hor_coefs.odd_real ||
2575 !coefs->hor_coefs.odd_imag ||
2576 !coefs->hor_coefs.even_real ||
2577 !coefs->hor_coefs.even_imag ||
2578 !coefs->ver_coefs.odd_real ||
2579 !coefs->ver_coefs.odd_imag ||
2580 !coefs->ver_coefs.even_real ||
2581 !coefs->ver_coefs.even_imag)
2584 if (!css_param->dvs2_coeff) {
2585 /* DIS coefficients. */
2586 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
2587 if (!css_param->dvs2_coeff)
2591 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
2592 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
2593 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
2594 coefs->hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
2595 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
2596 coefs->hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
2597 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
2598 coefs->hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
2599 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
2600 coefs->hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
2601 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
2602 coefs->ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
2603 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
2604 coefs->ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
2605 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
2606 coefs->ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
2607 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
2608 coefs->ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
2609 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
2610 css_param->dvs2_coeff = NULL;
2614 if (copy_from_compatible(&dvs2_coefs, coefs,
2615 sizeof(struct ia_css_dvs2_coefficients),
2617 dev_err(asd->isp->dev, "copy dvs2 coef failed");
2621 if (sizeof(*cur) != sizeof(dvs2_coefs.grid) ||
2622 memcmp(&dvs2_coefs.grid, cur, sizeof(dvs2_coefs.grid))) {
2623 dev_err(asd->isp->dev, "dvs grid mismatch!\n");
2624 /* If the grid info in the argument differs from the current
2625 grid info, we tell the caller to reset the grid size and
2630 if (!dvs2_coefs.hor_coefs.odd_real ||
2631 !dvs2_coefs.hor_coefs.odd_imag ||
2632 !dvs2_coefs.hor_coefs.even_real ||
2633 !dvs2_coefs.hor_coefs.even_imag ||
2634 !dvs2_coefs.ver_coefs.odd_real ||
2635 !dvs2_coefs.ver_coefs.odd_imag ||
2636 !dvs2_coefs.ver_coefs.even_real ||
2637 !dvs2_coefs.ver_coefs.even_imag)
2640 if (!css_param->dvs2_coeff) {
2641 /* DIS coefficients. */
2642 css_param->dvs2_coeff = ia_css_dvs2_coefficients_allocate(cur);
2643 if (!css_param->dvs2_coeff)
2647 dvs_hor_coef_bytes = asd->params.dvs_hor_coef_bytes;
2648 dvs_ver_coef_bytes = asd->params.dvs_ver_coef_bytes;
2649 if (copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_real,
2650 dvs2_coefs.hor_coefs.odd_real, dvs_hor_coef_bytes, from_user) ||
2651 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.odd_imag,
2652 dvs2_coefs.hor_coefs.odd_imag, dvs_hor_coef_bytes, from_user) ||
2653 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_real,
2654 dvs2_coefs.hor_coefs.even_real, dvs_hor_coef_bytes, from_user) ||
2655 copy_from_compatible(css_param->dvs2_coeff->hor_coefs.even_imag,
2656 dvs2_coefs.hor_coefs.even_imag, dvs_hor_coef_bytes, from_user) ||
2657 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_real,
2658 dvs2_coefs.ver_coefs.odd_real, dvs_ver_coef_bytes, from_user) ||
2659 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.odd_imag,
2660 dvs2_coefs.ver_coefs.odd_imag, dvs_ver_coef_bytes, from_user) ||
2661 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_real,
2662 dvs2_coefs.ver_coefs.even_real, dvs_ver_coef_bytes, from_user) ||
2663 copy_from_compatible(css_param->dvs2_coeff->ver_coefs.even_imag,
2664 dvs2_coefs.ver_coefs.even_imag, dvs_ver_coef_bytes, from_user)) {
2665 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
2666 css_param->dvs2_coeff = NULL;
2671 css_param->update_flag.dvs2_coefs =
2672 (struct atomisp_dis_coefficients *)css_param->dvs2_coeff;
2676 int atomisp_cp_dvs_6axis_config(struct atomisp_sub_device *asd,
2677 struct atomisp_dvs_6axis_config *source_6axis_config,
2678 struct atomisp_css_params *css_param,
2681 struct ia_css_dvs_6axis_config *dvs_6axis_config;
2682 struct ia_css_dvs_6axis_config *old_6axis_config;
2683 struct ia_css_stream *stream =
2684 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream;
2685 struct ia_css_dvs_grid_info *dvs_grid_info =
2686 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
2690 dev_err(asd->isp->dev, "%s: internal error!", __func__);
2694 if (!source_6axis_config || !dvs_grid_info)
2697 if (!dvs_grid_info->enable)
2700 if (!from_user && css_param->update_flag.dvs_6axis_config)
2703 /* check whether need to reallocate for 6 axis config */
2704 old_6axis_config = css_param->dvs_6axis;
2705 dvs_6axis_config = old_6axis_config;
2708 struct ia_css_dvs_6axis_config t_6axis_config;
2710 if (copy_from_compatible(&t_6axis_config, source_6axis_config,
2711 sizeof(struct atomisp_dvs_6axis_config),
2713 dev_err(asd->isp->dev, "copy morph table failed!");
2717 if (old_6axis_config &&
2718 (old_6axis_config->width_y != t_6axis_config.width_y ||
2719 old_6axis_config->height_y != t_6axis_config.height_y ||
2720 old_6axis_config->width_uv != t_6axis_config.width_uv ||
2721 old_6axis_config->height_uv != t_6axis_config.height_uv)) {
2722 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
2723 css_param->dvs_6axis = NULL;
2725 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2726 if (!dvs_6axis_config)
2728 } else if (!dvs_6axis_config) {
2729 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2730 if (!dvs_6axis_config)
2734 dvs_6axis_config->exp_id = t_6axis_config.exp_id;
2736 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
2737 t_6axis_config.xcoords_y,
2738 t_6axis_config.width_y *
2739 t_6axis_config.height_y *
2740 sizeof(*dvs_6axis_config->xcoords_y),
2743 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
2744 t_6axis_config.ycoords_y,
2745 t_6axis_config.width_y *
2746 t_6axis_config.height_y *
2747 sizeof(*dvs_6axis_config->ycoords_y),
2750 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
2751 t_6axis_config.xcoords_uv,
2752 t_6axis_config.width_uv *
2753 t_6axis_config.height_uv *
2754 sizeof(*dvs_6axis_config->xcoords_uv),
2757 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
2758 t_6axis_config.ycoords_uv,
2759 t_6axis_config.width_uv *
2760 t_6axis_config.height_uv *
2761 sizeof(*dvs_6axis_config->ycoords_uv),
2765 if (old_6axis_config &&
2766 (old_6axis_config->width_y != source_6axis_config->width_y ||
2767 old_6axis_config->height_y != source_6axis_config->height_y ||
2768 old_6axis_config->width_uv != source_6axis_config->width_uv ||
2769 old_6axis_config->height_uv != source_6axis_config->height_uv)) {
2770 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
2771 css_param->dvs_6axis = NULL;
2773 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2774 if (!dvs_6axis_config) {
2778 } else if (!dvs_6axis_config) {
2779 dvs_6axis_config = ia_css_dvs2_6axis_config_allocate(stream);
2780 if (!dvs_6axis_config) {
2786 dvs_6axis_config->exp_id = source_6axis_config->exp_id;
2788 if (copy_from_compatible(dvs_6axis_config->xcoords_y,
2789 source_6axis_config->xcoords_y,
2790 source_6axis_config->width_y *
2791 source_6axis_config->height_y *
2792 sizeof(*source_6axis_config->xcoords_y),
2795 if (copy_from_compatible(dvs_6axis_config->ycoords_y,
2796 source_6axis_config->ycoords_y,
2797 source_6axis_config->width_y *
2798 source_6axis_config->height_y *
2799 sizeof(*source_6axis_config->ycoords_y),
2802 if (copy_from_compatible(dvs_6axis_config->xcoords_uv,
2803 source_6axis_config->xcoords_uv,
2804 source_6axis_config->width_uv *
2805 source_6axis_config->height_uv *
2806 sizeof(*source_6axis_config->xcoords_uv),
2809 if (copy_from_compatible(dvs_6axis_config->ycoords_uv,
2810 source_6axis_config->ycoords_uv,
2811 source_6axis_config->width_uv *
2812 source_6axis_config->height_uv *
2813 sizeof(*source_6axis_config->ycoords_uv),
2817 css_param->dvs_6axis = dvs_6axis_config;
2818 css_param->update_flag.dvs_6axis_config =
2819 (struct atomisp_dvs_6axis_config *)dvs_6axis_config;
2823 if (dvs_6axis_config)
2824 ia_css_dvs2_6axis_config_free(dvs_6axis_config);
2828 int atomisp_cp_morph_table(struct atomisp_sub_device *asd,
2829 struct atomisp_morph_table *source_morph_table,
2830 struct atomisp_css_params *css_param,
2835 struct ia_css_morph_table *morph_table;
2836 struct ia_css_morph_table *old_morph_table;
2838 if (!source_morph_table)
2841 if (!from_user && css_param->update_flag.morph_table)
2844 old_morph_table = css_param->morph_table;
2847 struct ia_css_morph_table mtbl;
2849 if (copy_from_compatible(&mtbl, source_morph_table,
2850 sizeof(struct atomisp_morph_table),
2852 dev_err(asd->isp->dev, "copy morph table failed!");
2856 morph_table = atomisp_css_morph_table_allocate(
2862 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
2863 if (copy_from_compatible(morph_table->coordinates_x[i],
2864 (__force void *)source_morph_table->coordinates_x[i],
2865 mtbl.height * mtbl.width *
2866 sizeof(*morph_table->coordinates_x[i]),
2870 if (copy_from_compatible(morph_table->coordinates_y[i],
2871 (__force void *)source_morph_table->coordinates_y[i],
2872 mtbl.height * mtbl.width *
2873 sizeof(*morph_table->coordinates_y[i]),
2878 morph_table = atomisp_css_morph_table_allocate(
2879 source_morph_table->width,
2880 source_morph_table->height);
2886 for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
2887 if (copy_from_compatible(morph_table->coordinates_x[i],
2888 (__force void *)source_morph_table->coordinates_x[i],
2889 source_morph_table->height * source_morph_table->width *
2890 sizeof(*source_morph_table->coordinates_x[i]),
2894 if (copy_from_compatible(morph_table->coordinates_y[i],
2895 (__force void *)source_morph_table->coordinates_y[i],
2896 source_morph_table->height * source_morph_table->width *
2897 sizeof(*source_morph_table->coordinates_y[i]),
2903 css_param->morph_table = morph_table;
2904 if (old_morph_table)
2905 atomisp_css_morph_table_free(old_morph_table);
2906 css_param->update_flag.morph_table =
2907 (struct atomisp_morph_table *)morph_table;
2912 atomisp_css_morph_table_free(morph_table);
2916 int atomisp_makeup_css_parameters(struct atomisp_sub_device *asd,
2917 struct atomisp_parameters *arg,
2918 struct atomisp_css_params *css_param)
2922 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, false);
2925 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, false);
2928 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, false);
2931 ret = atomisp_css_cp_dvs2_coefs(asd,
2932 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
2936 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
2941 void atomisp_free_css_parameters(struct atomisp_css_params *css_param)
2943 if (css_param->dvs_6axis) {
2944 ia_css_dvs2_6axis_config_free(css_param->dvs_6axis);
2945 css_param->dvs_6axis = NULL;
2947 if (css_param->dvs2_coeff) {
2948 ia_css_dvs2_coefficients_free(css_param->dvs2_coeff);
2949 css_param->dvs2_coeff = NULL;
2951 if (css_param->shading_table) {
2952 ia_css_shading_table_free(css_param->shading_table);
2953 css_param->shading_table = NULL;
2955 if (css_param->morph_table) {
2956 ia_css_morph_table_free(css_param->morph_table);
2957 css_param->morph_table = NULL;
2961 static void atomisp_move_frame_to_activeq(struct ia_css_frame *frame,
2962 struct atomisp_css_params_with_list *param)
2964 struct atomisp_video_pipe *pipe = vb_to_pipe(&frame->vb.vb2_buf);
2965 unsigned long irqflags;
2967 pipe->frame_params[frame->vb.vb2_buf.index] = param;
2968 spin_lock_irqsave(&pipe->irq_lock, irqflags);
2969 list_move_tail(&frame->queue, &pipe->activeq);
2970 spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
2974 * Check parameter queue list and buffer queue list to find out if matched items
2975 * and then set parameter to CSS and enqueue buffer to CSS.
2976 * Of course, if the buffer in buffer waiting list is not bound to a per-frame
2977 * parameter, it will be enqueued into CSS as long as the per-frame setting
2978 * buffers before it get enqueued.
2980 void atomisp_handle_parameter_and_buffer(struct atomisp_video_pipe *pipe)
2982 struct atomisp_sub_device *asd = pipe->asd;
2983 struct ia_css_frame *frame = NULL, *frame_tmp;
2984 struct atomisp_css_params_with_list *param = NULL, *param_tmp;
2985 bool need_to_enqueue_buffer = false;
2988 lockdep_assert_held(&asd->isp->mutex);
2991 * CSS/FW requires set parameter and enqueue buffer happen after ISP
2994 if (!asd->streaming)
2997 if (list_empty(&pipe->per_frame_params) ||
2998 list_empty(&pipe->buffers_waiting_for_param))
3001 list_for_each_entry_safe(frame, frame_tmp,
3002 &pipe->buffers_waiting_for_param, queue) {
3003 i = frame->vb.vb2_buf.index;
3004 if (pipe->frame_request_config_id[i]) {
3005 list_for_each_entry_safe(param, param_tmp,
3006 &pipe->per_frame_params, list) {
3007 if (pipe->frame_request_config_id[i] != param->params.isp_config_id)
3010 list_del(¶m->list);
3013 * clear the request config id as the buffer
3014 * will be handled and enqueued into CSS soon
3016 pipe->frame_request_config_id[i] = 0;
3017 atomisp_move_frame_to_activeq(frame, param);
3018 need_to_enqueue_buffer = true;
3022 /* If this is the end, stop further loop */
3023 if (list_entry_is_head(param, &pipe->per_frame_params, list))
3026 atomisp_move_frame_to_activeq(frame, NULL);
3027 need_to_enqueue_buffer = true;
3031 if (!need_to_enqueue_buffer)
3034 atomisp_qbuffers_to_css(asd);
3038 * Function to configure ISP parameters
3040 int atomisp_set_parameters(struct video_device *vdev,
3041 struct atomisp_parameters *arg)
3043 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
3044 struct atomisp_sub_device *asd = pipe->asd;
3045 struct atomisp_css_params_with_list *param = NULL;
3046 struct atomisp_css_params *css_param = &asd->params.css_param;
3049 lockdep_assert_held(&asd->isp->mutex);
3051 if (!asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream) {
3052 dev_err(asd->isp->dev, "%s: internal error!\n", __func__);
3056 dev_dbg(asd->isp->dev, "set parameter(per_frame_setting %d) isp_config_id %d of %s\n",
3057 arg->per_frame_setting, arg->isp_config_id, vdev->name);
3059 if (arg->per_frame_setting) {
3061 * Per-frame setting enabled, we allocate a new parameter
3062 * buffer to cache the parameters and only when frame buffers
3063 * are ready, the parameters will be set to CSS.
3064 * per-frame setting only works for the main output frame.
3066 param = kvzalloc(sizeof(*param), GFP_KERNEL);
3068 dev_err(asd->isp->dev, "%s: failed to alloc params buffer\n",
3072 css_param = ¶m->params;
3075 ret = atomisp_cp_general_isp_parameters(asd, arg, css_param, true);
3077 goto apply_parameter_failed;
3079 ret = atomisp_cp_lsc_table(asd, arg->shading_table, css_param, true);
3081 goto apply_parameter_failed;
3083 ret = atomisp_cp_morph_table(asd, arg->morph_table, css_param, true);
3085 goto apply_parameter_failed;
3087 ret = atomisp_css_cp_dvs2_coefs(asd,
3088 (struct ia_css_dvs2_coefficients *)arg->dvs2_coefs,
3091 goto apply_parameter_failed;
3093 ret = atomisp_cp_dvs_6axis_config(asd, arg->dvs_6axis_config,
3096 goto apply_parameter_failed;
3098 if (!arg->per_frame_setting) {
3099 /* indicate to CSS that we have parameters to be updated */
3100 asd->params.css_update_params_needed = true;
3102 list_add_tail(¶m->list, &pipe->per_frame_params);
3103 atomisp_handle_parameter_and_buffer(pipe);
3108 apply_parameter_failed:
3110 atomisp_free_css_parameters(css_param);
3117 * Function to set/get isp parameters to isp
3119 int atomisp_param(struct atomisp_sub_device *asd, int flag,
3120 struct atomisp_parm *config)
3122 struct ia_css_pipe_config *vp_cfg =
3123 &asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].
3124 pipe_configs[IA_CSS_PIPE_ID_VIDEO];
3126 /* Read parameter for 3A binary info */
3128 struct ia_css_dvs_grid_info *dvs_grid_info =
3129 atomisp_css_get_dvs_grid_info(
3130 &asd->params.curr_grid_info);
3132 atomisp_curr_user_grid_info(asd, &config->info);
3134 /* We always return the resolution and stride even if there is
3135 * no valid metadata. This allows the caller to get the
3136 * information needed to allocate user-space buffers. */
3137 config->metadata_config.metadata_height = asd->
3138 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
3139 metadata_info.resolution.height;
3140 config->metadata_config.metadata_stride = asd->
3141 stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream_info.
3142 metadata_info.stride;
3144 /* update dvs grid info */
3146 memcpy(&config->dvs_grid,
3148 sizeof(struct ia_css_dvs_grid_info));
3150 if (asd->run_mode->val != ATOMISP_RUN_MODE_VIDEO) {
3151 config->dvs_envelop.width = 0;
3152 config->dvs_envelop.height = 0;
3156 /* update dvs envelop info */
3157 config->dvs_envelop.width = vp_cfg->dvs_envelope.width;
3158 config->dvs_envelop.height = vp_cfg->dvs_envelope.height;
3162 memcpy(&asd->params.css_param.wb_config, &config->wb_config,
3163 sizeof(struct ia_css_wb_config));
3164 memcpy(&asd->params.css_param.ob_config, &config->ob_config,
3165 sizeof(struct ia_css_ob_config));
3166 memcpy(&asd->params.css_param.dp_config, &config->dp_config,
3167 sizeof(struct ia_css_dp_config));
3168 memcpy(&asd->params.css_param.de_config, &config->de_config,
3169 sizeof(struct ia_css_de_config));
3170 memcpy(&asd->params.css_param.dz_config, &config->dz_config,
3171 sizeof(struct ia_css_dz_config));
3172 memcpy(&asd->params.css_param.ce_config, &config->ce_config,
3173 sizeof(struct ia_css_ce_config));
3174 memcpy(&asd->params.css_param.nr_config, &config->nr_config,
3175 sizeof(struct ia_css_nr_config));
3176 memcpy(&asd->params.css_param.ee_config, &config->ee_config,
3177 sizeof(struct ia_css_ee_config));
3178 memcpy(&asd->params.css_param.tnr_config, &config->tnr_config,
3179 sizeof(struct ia_css_tnr_config));
3181 if (asd->params.color_effect == V4L2_COLORFX_NEGATIVE) {
3182 asd->params.css_param.cc_config.matrix[3] = -config->cc_config.matrix[3];
3183 asd->params.css_param.cc_config.matrix[4] = -config->cc_config.matrix[4];
3184 asd->params.css_param.cc_config.matrix[5] = -config->cc_config.matrix[5];
3185 asd->params.css_param.cc_config.matrix[6] = -config->cc_config.matrix[6];
3186 asd->params.css_param.cc_config.matrix[7] = -config->cc_config.matrix[7];
3187 asd->params.css_param.cc_config.matrix[8] = -config->cc_config.matrix[8];
3190 if (asd->params.color_effect != V4L2_COLORFX_SEPIA &&
3191 asd->params.color_effect != V4L2_COLORFX_BW) {
3192 memcpy(&asd->params.css_param.cc_config, &config->cc_config,
3193 sizeof(struct ia_css_cc_config));
3194 asd->params.config.cc_config = &asd->params.css_param.cc_config;
3197 asd->params.config.wb_config = &asd->params.css_param.wb_config;
3198 asd->params.config.ob_config = &asd->params.css_param.ob_config;
3199 asd->params.config.de_config = &asd->params.css_param.de_config;
3200 asd->params.config.dz_config = &asd->params.css_param.dz_config;
3201 asd->params.config.ce_config = &asd->params.css_param.ce_config;
3202 asd->params.config.dp_config = &asd->params.css_param.dp_config;
3203 asd->params.config.nr_config = &asd->params.css_param.nr_config;
3204 asd->params.config.ee_config = &asd->params.css_param.ee_config;
3205 asd->params.config.tnr_config = &asd->params.css_param.tnr_config;
3206 asd->params.css_update_params_needed = true;
3212 * Function to configure color effect of the image
3214 int atomisp_color_effect(struct atomisp_sub_device *asd, int flag,
3217 struct ia_css_cc_config *cc_config = NULL;
3218 struct ia_css_macc_table *macc_table = NULL;
3219 struct ia_css_ctc_table *ctc_table = NULL;
3221 struct v4l2_control control;
3222 struct atomisp_device *isp = asd->isp;
3225 *effect = asd->params.color_effect;
3229 control.id = V4L2_CID_COLORFX;
3230 control.value = *effect;
3232 v4l2_s_ctrl(NULL, isp->inputs[asd->input_curr].camera->ctrl_handler,
3235 * if set color effect to sensor successfully, return
3239 asd->params.color_effect = (u32)*effect;
3243 if (*effect == asd->params.color_effect)
3247 * isp_subdev->params.macc_en should be set to false.
3249 asd->params.macc_en = false;
3252 case V4L2_COLORFX_NONE:
3253 macc_table = &asd->params.css_param.macc_table;
3254 asd->params.macc_en = true;
3256 case V4L2_COLORFX_SEPIA:
3257 cc_config = &sepia_cc_config;
3259 case V4L2_COLORFX_NEGATIVE:
3260 cc_config = &nega_cc_config;
3262 case V4L2_COLORFX_BW:
3263 cc_config = &mono_cc_config;
3265 case V4L2_COLORFX_SKY_BLUE:
3266 macc_table = &blue_macc_table;
3267 asd->params.macc_en = true;
3269 case V4L2_COLORFX_GRASS_GREEN:
3270 macc_table = &green_macc_table;
3271 asd->params.macc_en = true;
3273 case V4L2_COLORFX_SKIN_WHITEN_LOW:
3274 macc_table = &skin_low_macc_table;
3275 asd->params.macc_en = true;
3277 case V4L2_COLORFX_SKIN_WHITEN:
3278 macc_table = &skin_medium_macc_table;
3279 asd->params.macc_en = true;
3281 case V4L2_COLORFX_SKIN_WHITEN_HIGH:
3282 macc_table = &skin_high_macc_table;
3283 asd->params.macc_en = true;
3285 case V4L2_COLORFX_VIVID:
3286 ctc_table = &vivid_ctc_table;
3291 atomisp_update_capture_mode(asd);
3294 asd->params.config.cc_config = cc_config;
3296 asd->params.config.macc_table = macc_table;
3298 atomisp_css_set_ctc_table(asd, ctc_table);
3299 asd->params.color_effect = (u32)*effect;
3300 asd->params.css_update_params_needed = true;
3305 * Function to configure bad pixel correction
3307 int atomisp_bad_pixel(struct atomisp_sub_device *asd, int flag,
3311 *value = asd->params.bad_pixel_en;
3314 asd->params.bad_pixel_en = !!*value;
3320 * Function to configure bad pixel correction params
3322 int atomisp_bad_pixel_param(struct atomisp_sub_device *asd, int flag,
3323 struct atomisp_dp_config *config)
3326 /* Get bad pixel from current setup */
3327 if (atomisp_css_get_dp_config(asd, config))
3330 /* Set bad pixel to isp parameters */
3331 memcpy(&asd->params.css_param.dp_config, config,
3332 sizeof(asd->params.css_param.dp_config));
3333 asd->params.config.dp_config = &asd->params.css_param.dp_config;
3334 asd->params.css_update_params_needed = true;
3341 * Function to enable/disable video image stablization
3343 int atomisp_video_stable(struct atomisp_sub_device *asd, int flag,
3347 *value = asd->params.video_dis_en;
3349 asd->params.video_dis_en = !!*value;
3355 * Function to configure fixed pattern noise
3357 int atomisp_fixed_pattern(struct atomisp_sub_device *asd, int flag,
3361 *value = asd->params.fpn_en;
3366 asd->params.fpn_en = false;
3370 /* Add function to get black from from sensor with shutter off */
3375 atomisp_bytesperline_to_padded_width(unsigned int bytesperline,
3376 enum ia_css_frame_format format)
3379 case IA_CSS_FRAME_FORMAT_UYVY:
3380 case IA_CSS_FRAME_FORMAT_YUYV:
3381 case IA_CSS_FRAME_FORMAT_RAW:
3382 case IA_CSS_FRAME_FORMAT_RGB565:
3383 return bytesperline / 2;
3384 case IA_CSS_FRAME_FORMAT_RGBA888:
3385 return bytesperline / 4;
3386 /* The following cases could be removed, but we leave them
3387 in to document the formats that are included. */
3388 case IA_CSS_FRAME_FORMAT_NV11:
3389 case IA_CSS_FRAME_FORMAT_NV12:
3390 case IA_CSS_FRAME_FORMAT_NV16:
3391 case IA_CSS_FRAME_FORMAT_NV21:
3392 case IA_CSS_FRAME_FORMAT_NV61:
3393 case IA_CSS_FRAME_FORMAT_YV12:
3394 case IA_CSS_FRAME_FORMAT_YV16:
3395 case IA_CSS_FRAME_FORMAT_YUV420:
3396 case IA_CSS_FRAME_FORMAT_YUV420_16:
3397 case IA_CSS_FRAME_FORMAT_YUV422:
3398 case IA_CSS_FRAME_FORMAT_YUV422_16:
3399 case IA_CSS_FRAME_FORMAT_YUV444:
3400 case IA_CSS_FRAME_FORMAT_YUV_LINE:
3401 case IA_CSS_FRAME_FORMAT_PLANAR_RGB888:
3402 case IA_CSS_FRAME_FORMAT_QPLANE6:
3403 case IA_CSS_FRAME_FORMAT_BINARY_8:
3405 return bytesperline;
3410 atomisp_v4l2_framebuffer_to_css_frame(const struct v4l2_framebuffer *arg,
3411 struct ia_css_frame **result)
3413 struct ia_css_frame *res = NULL;
3414 unsigned int padded_width;
3415 enum ia_css_frame_format sh_format;
3416 char *tmp_buf = NULL;
3419 sh_format = v4l2_fmt_to_sh_fmt(arg->fmt.pixelformat);
3420 padded_width = atomisp_bytesperline_to_padded_width(
3421 arg->fmt.bytesperline, sh_format);
3423 /* Note: the padded width on an ia_css_frame is in elements, not in
3424 bytes. The RAW frame we use here should always be a 16bit RAW
3425 frame. This is why we bytesperline/2 is equal to the padded with */
3426 if (ia_css_frame_allocate(&res, arg->fmt.width, arg->fmt.height,
3427 sh_format, padded_width, 0)) {
3432 tmp_buf = vmalloc(arg->fmt.sizeimage);
3437 if (copy_from_user(tmp_buf, (void __user __force *)arg->base,
3438 arg->fmt.sizeimage)) {
3443 if (hmm_store(res->data, tmp_buf, arg->fmt.sizeimage)) {
3450 ia_css_frame_free(res);
3458 * Function to configure fixed pattern noise table
3460 int atomisp_fixed_pattern_table(struct atomisp_sub_device *asd,
3461 struct v4l2_framebuffer *arg)
3463 struct ia_css_frame *raw_black_frame = NULL;
3469 ret = atomisp_v4l2_framebuffer_to_css_frame(arg, &raw_black_frame);
3473 if (sh_css_set_black_frame(asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].stream,
3474 raw_black_frame) != 0)
3477 ia_css_frame_free(raw_black_frame);
3482 * Function to configure false color correction
3484 int atomisp_false_color(struct atomisp_sub_device *asd, int flag,
3487 /* Get nr config from current setup */
3489 *value = asd->params.false_color;
3493 /* Set nr config to isp parameters */
3495 asd->params.config.de_config = NULL;
3497 asd->params.css_param.de_config.pixelnoise = 0;
3498 asd->params.config.de_config = &asd->params.css_param.de_config;
3500 asd->params.css_update_params_needed = true;
3501 asd->params.false_color = *value;
3506 * Function to configure bad pixel correction params
3508 int atomisp_false_color_param(struct atomisp_sub_device *asd, int flag,
3509 struct atomisp_de_config *config)
3512 /* Get false color from current setup */
3513 if (atomisp_css_get_de_config(asd, config))
3516 /* Set false color to isp parameters */
3517 memcpy(&asd->params.css_param.de_config, config,
3518 sizeof(asd->params.css_param.de_config));
3519 asd->params.config.de_config = &asd->params.css_param.de_config;
3520 asd->params.css_update_params_needed = true;
3527 * Function to configure white balance params
3529 int atomisp_white_balance_param(struct atomisp_sub_device *asd, int flag,
3530 struct atomisp_wb_config *config)
3533 /* Get white balance from current setup */
3534 if (atomisp_css_get_wb_config(asd, config))
3537 /* Set white balance to isp parameters */
3538 memcpy(&asd->params.css_param.wb_config, config,
3539 sizeof(asd->params.css_param.wb_config));
3540 asd->params.config.wb_config = &asd->params.css_param.wb_config;
3541 asd->params.css_update_params_needed = true;
3547 int atomisp_3a_config_param(struct atomisp_sub_device *asd, int flag,
3548 struct atomisp_3a_config *config)
3550 struct atomisp_device *isp = asd->isp;
3552 dev_dbg(isp->dev, ">%s %d\n", __func__, flag);
3555 /* Get white balance from current setup */
3556 if (atomisp_css_get_3a_config(asd, config))
3559 /* Set white balance to isp parameters */
3560 memcpy(&asd->params.css_param.s3a_config, config,
3561 sizeof(asd->params.css_param.s3a_config));
3562 asd->params.config.s3a_config = &asd->params.css_param.s3a_config;
3563 asd->params.css_update_params_needed = true;
3566 dev_dbg(isp->dev, "<%s %d\n", __func__, flag);
3571 * Function to setup digital zoom
3573 int atomisp_digital_zoom(struct atomisp_sub_device *asd, int flag,
3577 struct atomisp_device *isp = asd->isp;
3579 unsigned int max_zoom = MRFLD_MAX_ZOOM_FACTOR;
3582 atomisp_css_get_zoom_factor(asd, &zoom);
3583 *value = max_zoom - zoom;
3588 zoom = max_zoom - min_t(u32, max_zoom - 1, *value);
3589 atomisp_css_set_zoom_factor(asd, zoom);
3591 dev_dbg(isp->dev, "%s, zoom: %d\n", __func__, zoom);
3592 asd->params.css_update_params_needed = true;
3598 static void __atomisp_update_stream_env(struct atomisp_sub_device *asd,
3599 u16 stream_index, struct atomisp_input_stream_info *stream_info)
3603 /* assign virtual channel id return from sensor driver query */
3604 asd->stream_env[stream_index].ch_id = stream_info->ch_id;
3605 asd->stream_env[stream_index].isys_configs = stream_info->isys_configs;
3606 for (i = 0; i < stream_info->isys_configs; i++) {
3607 asd->stream_env[stream_index].isys_info[i].input_format =
3608 stream_info->isys_info[i].input_format;
3609 asd->stream_env[stream_index].isys_info[i].width =
3610 stream_info->isys_info[i].width;
3611 asd->stream_env[stream_index].isys_info[i].height =
3612 stream_info->isys_info[i].height;
3616 static void __atomisp_init_stream_info(u16 stream_index,
3617 struct atomisp_input_stream_info *stream_info)
3621 stream_info->enable = 1;
3622 stream_info->stream = stream_index;
3623 stream_info->ch_id = 0;
3624 stream_info->isys_configs = 0;
3625 for (i = 0; i < MAX_STREAMS_PER_CHANNEL; i++) {
3626 stream_info->isys_info[i].input_format = 0;
3627 stream_info->isys_info[i].width = 0;
3628 stream_info->isys_info[i].height = 0;
3632 static void atomisp_fill_pix_format(struct v4l2_pix_format *f,
3633 u32 width, u32 height,
3634 const struct atomisp_format_bridge *br_fmt)
3640 f->pixelformat = br_fmt->pixelformat;
3642 /* Adding padding to width for bytesperline calculation */
3643 width = ia_css_frame_pad_width(width, br_fmt->sh_fmt);
3644 bytes = BITS_TO_BYTES(br_fmt->depth * width);
3647 f->bytesperline = width;
3649 f->bytesperline = bytes;
3651 f->sizeimage = PAGE_ALIGN(height * bytes);
3653 if (f->field == V4L2_FIELD_ANY)
3654 f->field = V4L2_FIELD_NONE;
3657 * FIXME: do we need to set this up differently, depending on the
3658 * sensor or the pipeline?
3660 f->colorspace = V4L2_COLORSPACE_REC709;
3661 f->ycbcr_enc = V4L2_YCBCR_ENC_709;
3662 f->xfer_func = V4L2_XFER_FUNC_709;
3665 /* Get sensor padding values for the non padded width x height resolution */
3666 void atomisp_get_padding(struct atomisp_device *isp, u32 width, u32 height,
3667 u32 *padding_w, u32 *padding_h)
3669 struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr];
3670 struct v4l2_rect native_rect = input->native_rect;
3671 const struct atomisp_in_fmt_conv *fc = NULL;
3672 u32 min_pad_w = ISP2400_MIN_PAD_W;
3673 u32 min_pad_h = ISP2400_MIN_PAD_H;
3674 struct v4l2_mbus_framefmt *sink;
3676 if (!input->crop_support) {
3682 width = min(width, input->active_rect.width);
3683 height = min(height, input->active_rect.height);
3685 if (input->binning_support && width <= (input->active_rect.width / 2) &&
3686 height <= (input->active_rect.height / 2)) {
3687 native_rect.width /= 2;
3688 native_rect.height /= 2;
3691 *padding_w = min_t(u32, (native_rect.width - width) & ~1, pad_w);
3692 *padding_h = min_t(u32, (native_rect.height - height) & ~1, pad_h);
3694 /* The below minimum padding requirements are for BYT / ISP2400 only */
3698 sink = atomisp_subdev_get_ffmt(&isp->asd.subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
3699 ATOMISP_SUBDEV_PAD_SINK);
3701 fc = atomisp_find_in_fmt_conv(sink->code);
3703 dev_warn(isp->dev, "%s: Could not get sensor format\n", __func__);
3704 goto apply_min_padding;
3708 * The ISP only supports GRBG for other bayer-orders additional padding
3709 * is used so that the raw sensor data can be cropped to fix the order.
3711 if (fc->bayer_order == IA_CSS_BAYER_ORDER_RGGB ||
3712 fc->bayer_order == IA_CSS_BAYER_ORDER_GBRG)
3715 if (fc->bayer_order == IA_CSS_BAYER_ORDER_BGGR ||
3716 fc->bayer_order == IA_CSS_BAYER_ORDER_GBRG)
3720 *padding_w = max_t(u32, *padding_w, min_pad_w);
3721 *padding_h = max_t(u32, *padding_h, min_pad_h);
3724 static int atomisp_set_crop(struct atomisp_device *isp,
3725 const struct v4l2_mbus_framefmt *format,
3726 struct v4l2_subdev_state *sd_state,
3729 struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr];
3730 struct v4l2_subdev_selection sel = {
3732 .target = V4L2_SEL_TGT_CROP,
3733 .r.width = format->width,
3734 .r.height = format->height,
3738 if (!input->crop_support)
3741 /* Cropping is done before binning, when binning double the crop rect */
3742 if (input->binning_support && sel.r.width <= (input->native_rect.width / 2) &&
3743 sel.r.height <= (input->native_rect.height / 2)) {
3748 /* Clamp to avoid top/left calculations overflowing */
3749 sel.r.width = min(sel.r.width, input->native_rect.width);
3750 sel.r.height = min(sel.r.height, input->native_rect.height);
3752 sel.r.left = ((input->native_rect.width - sel.r.width) / 2) & ~1;
3753 sel.r.top = ((input->native_rect.height - sel.r.height) / 2) & ~1;
3755 ret = v4l2_subdev_call(input->camera, pad, set_selection, sd_state, &sel);
3757 dev_err(isp->dev, "Error setting crop to %ux%u @%ux%u: %d\n",
3758 sel.r.width, sel.r.height, sel.r.left, sel.r.top, ret);
3763 /* This function looks up the closest available resolution. */
3764 int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
3765 const struct atomisp_format_bridge **fmt_ret,
3766 const struct atomisp_format_bridge **snr_fmt_ret)
3768 const struct atomisp_format_bridge *fmt, *snr_fmt;
3769 struct atomisp_sub_device *asd = &isp->asd;
3770 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
3771 struct v4l2_subdev_format format = {
3772 .which = V4L2_SUBDEV_FORMAT_TRY,
3774 u32 padding_w, padding_h;
3780 fmt = atomisp_get_format_bridge(f->pixelformat);
3781 /* Currently, raw formats are broken!!! */
3782 if (!fmt || fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
3783 f->pixelformat = V4L2_PIX_FMT_YUV420;
3785 fmt = atomisp_get_format_bridge(f->pixelformat);
3790 /* The preview pipeline does not support width > 1920 */
3791 if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW)
3792 f->width = min_t(u32, f->width, 1920);
3795 * atomisp_set_fmt() will set the sensor resolution to the requested
3796 * resolution + padding. Add padding here and remove it again after
3797 * the set_fmt call, like atomisp_set_fmt_to_snr() does.
3799 atomisp_get_padding(isp, f->width, f->height, &padding_w, &padding_h);
3800 v4l2_fill_mbus_format(&format.format, f, fmt->mbus_code);
3801 format.format.width += padding_w;
3802 format.format.height += padding_h;
3804 dev_dbg(isp->dev, "try_mbus_fmt: asking for %ux%u\n",
3805 format.format.width, format.format.height);
3807 v4l2_subdev_lock_state(input->try_sd_state);
3809 ret = atomisp_set_crop(isp, &format.format, input->try_sd_state,
3810 V4L2_SUBDEV_FORMAT_TRY);
3812 ret = v4l2_subdev_call(input->camera, pad, set_fmt,
3813 input->try_sd_state, &format);
3815 v4l2_subdev_unlock_state(input->try_sd_state);
3820 dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n",
3821 format.format.width, format.format.height);
3823 snr_fmt = atomisp_get_format_bridge_from_mbus(format.format.code);
3825 dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
3826 format.format.code);
3830 f->width = format.format.width - padding_w;
3831 f->height = format.format.height - padding_h;
3834 * If the format is jpeg or custom RAW, then the width and height will
3835 * not satisfy the normal atomisp requirements and no need to check
3836 * the below conditions. So just assign to what is being returned from
3837 * the sensor driver.
3839 if (f->pixelformat == V4L2_PIX_FMT_JPEG ||
3840 f->pixelformat == V4L2_PIX_FMT_CUSTOM_M10MO_RAW)
3841 goto out_fill_pix_format;
3844 f->width = rounddown(clamp_t(u32, f->width, ATOM_ISP_MIN_WIDTH,
3845 ATOM_ISP_MAX_WIDTH), ATOM_ISP_STEP_WIDTH);
3846 f->height = rounddown(clamp_t(u32, f->height, ATOM_ISP_MIN_HEIGHT,
3847 ATOM_ISP_MAX_HEIGHT), ATOM_ISP_STEP_HEIGHT);
3849 out_fill_pix_format:
3850 atomisp_fill_pix_format(f, f->width, f->height, fmt);
3856 *snr_fmt_ret = snr_fmt;
3861 enum mipi_port_id atomisp_port_to_mipi_port(struct atomisp_device *isp,
3862 enum atomisp_camera_port port)
3865 case ATOMISP_CAMERA_PORT_PRIMARY:
3866 return MIPI_PORT0_ID;
3867 case ATOMISP_CAMERA_PORT_SECONDARY:
3868 return MIPI_PORT1_ID;
3869 case ATOMISP_CAMERA_PORT_TERTIARY:
3870 return MIPI_PORT2_ID;
3872 dev_err(isp->dev, "unsupported port: %d\n", port);
3873 return MIPI_PORT0_ID;
3877 static inline int atomisp_set_sensor_mipi_to_isp(
3878 struct atomisp_sub_device *asd,
3879 enum atomisp_input_stream_id stream_id,
3880 struct camera_mipi_info *mipi_info)
3882 struct v4l2_control ctrl;
3883 struct atomisp_device *isp = asd->isp;
3884 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
3885 const struct atomisp_in_fmt_conv *fc;
3887 unsigned int input_format, bayer_order;
3888 enum atomisp_input_format metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED;
3889 u32 mipi_port, metadata_width = 0, metadata_height = 0;
3891 ctrl.id = V4L2_CID_LINK_FREQ;
3892 if (v4l2_g_ctrl(input->camera->ctrl_handler, &ctrl) == 0)
3893 mipi_freq = ctrl.value;
3895 if (asd->stream_env[stream_id].isys_configs == 1) {
3897 asd->stream_env[stream_id].isys_info[0].input_format;
3898 atomisp_css_isys_set_format(asd, stream_id,
3899 input_format, IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX);
3900 } else if (asd->stream_env[stream_id].isys_configs == 2) {
3901 atomisp_css_isys_two_stream_cfg_update_stream1(
3903 asd->stream_env[stream_id].isys_info[0].input_format,
3904 asd->stream_env[stream_id].isys_info[0].width,
3905 asd->stream_env[stream_id].isys_info[0].height);
3907 atomisp_css_isys_two_stream_cfg_update_stream2(
3909 asd->stream_env[stream_id].isys_info[1].input_format,
3910 asd->stream_env[stream_id].isys_info[1].width,
3911 asd->stream_env[stream_id].isys_info[1].height);
3914 /* Compatibility for sensors which provide no media bus code
3915 * in s_mbus_framefmt() nor support pad formats. */
3916 if (mipi_info && mipi_info->input_format != -1) {
3917 bayer_order = mipi_info->raw_bayer_order;
3919 /* Input stream config is still needs configured */
3920 /* TODO: Check if this is necessary */
3921 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
3922 mipi_info->input_format);
3925 input_format = fc->atomisp_in_fmt;
3926 metadata_format = mipi_info->metadata_format;
3927 metadata_width = mipi_info->metadata_width;
3928 metadata_height = mipi_info->metadata_height;
3930 struct v4l2_mbus_framefmt *sink;
3932 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
3933 V4L2_SUBDEV_FORMAT_ACTIVE,
3934 ATOMISP_SUBDEV_PAD_SINK);
3935 fc = atomisp_find_in_fmt_conv(sink->code);
3938 input_format = fc->atomisp_in_fmt;
3939 bayer_order = fc->bayer_order;
3942 atomisp_css_input_set_format(asd, stream_id, input_format);
3943 atomisp_css_input_set_bayer_order(asd, stream_id, bayer_order);
3945 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(metadata_format);
3949 input_format = fc->atomisp_in_fmt;
3950 mipi_port = atomisp_port_to_mipi_port(isp, input->port);
3951 atomisp_css_input_configure_port(asd, mipi_port,
3952 isp->sensor_lanes[mipi_port],
3955 metadata_width, metadata_height);
3959 static int configure_pp_input_nop(struct atomisp_sub_device *asd,
3960 unsigned int width, unsigned int height)
3965 static int configure_output_nop(struct atomisp_sub_device *asd,
3966 unsigned int width, unsigned int height,
3967 unsigned int min_width,
3968 enum ia_css_frame_format sh_fmt)
3973 static int get_frame_info_nop(struct atomisp_sub_device *asd,
3974 struct ia_css_frame_info *finfo)
3980 * Resets CSS parameters that depend on input resolution.
3982 * Update params like CSS RAW binning, 2ppc mode and pp_input
3983 * which depend on input size, but are not automatically
3984 * handled in CSS when the input resolution is changed.
3986 static int css_input_resolution_changed(struct atomisp_sub_device *asd,
3987 struct v4l2_mbus_framefmt *ffmt)
3989 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
3992 dev_dbg(asd->isp->dev, "css_input_resolution_changed to %ux%u\n",
3993 ffmt->width, ffmt->height);
3996 atomisp_css_input_set_two_pixels_per_clock(asd, false);
3998 atomisp_css_input_set_two_pixels_per_clock(asd, true);
4001 * If sensor input changed, which means metadata resolution changed
4002 * together. Release all metadata buffers here to let it re-allocated
4003 * next time in reqbufs.
4005 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
4006 list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
4008 atomisp_css_free_metadata_buffer(md_buf);
4009 list_del(&md_buf->list);
4016 * TODO: atomisp_css_preview_configure_pp_input() not
4017 * reset due to CSS bug tracked as PSI BZ 115124
4021 static int atomisp_set_fmt_to_isp(struct video_device *vdev,
4022 struct ia_css_frame_info *output_info,
4023 const struct v4l2_pix_format *pix)
4025 struct camera_mipi_info *mipi_info;
4026 struct atomisp_device *isp = video_get_drvdata(vdev);
4027 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
4028 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
4029 const struct atomisp_format_bridge *format;
4030 struct v4l2_rect *isp_sink_crop;
4031 enum ia_css_pipe_id pipe_id;
4032 int (*configure_output)(struct atomisp_sub_device *asd,
4033 unsigned int width, unsigned int height,
4034 unsigned int min_width,
4035 enum ia_css_frame_format sh_fmt) =
4036 configure_output_nop;
4037 int (*get_frame_info)(struct atomisp_sub_device *asd,
4038 struct ia_css_frame_info *finfo) =
4040 int (*configure_pp_input)(struct atomisp_sub_device *asd,
4041 unsigned int width, unsigned int height) =
4042 configure_pp_input_nop;
4043 const struct atomisp_in_fmt_conv *fc = NULL;
4046 isp_sink_crop = atomisp_subdev_get_rect(
4047 &asd->subdev, NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
4048 ATOMISP_SUBDEV_PAD_SINK, V4L2_SEL_TGT_CROP);
4050 format = atomisp_get_format_bridge(pix->pixelformat);
4054 if (input->type != TEST_PATTERN) {
4055 mipi_info = atomisp_to_sensor_mipi_info(input->camera);
4057 if (atomisp_set_sensor_mipi_to_isp(asd, ATOMISP_INPUT_STREAM_GENERAL,
4062 fc = atomisp_find_in_fmt_conv_by_atomisp_in_fmt(mipi_info->input_format);
4065 fc = atomisp_find_in_fmt_conv(
4066 atomisp_subdev_get_ffmt(&asd->subdev,
4067 NULL, V4L2_SUBDEV_FORMAT_ACTIVE,
4068 ATOMISP_SUBDEV_PAD_SINK)->code);
4071 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW &&
4072 raw_output_format_match_input(fc->atomisp_in_fmt,
4078 * Configure viewfinder also when vfpp is disabled: the
4079 * CSS still requires viewfinder configuration.
4084 if (pix->width < 640 || pix->height < 480) {
4086 height = pix->height;
4092 if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
4093 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4094 atomisp_css_video_configure_viewfinder(asd, width, height, 0,
4095 IA_CSS_FRAME_FORMAT_NV12);
4096 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_STILL_CAPTURE ||
4097 asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT) {
4098 atomisp_css_capture_configure_viewfinder(asd, width, height, 0,
4099 IA_CSS_FRAME_FORMAT_NV12);
4103 atomisp_css_input_set_mode(asd, IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
4105 for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++)
4106 asd->stream_env[ATOMISP_INPUT_STREAM_GENERAL].pipe_extra_configs[i].disable_vf_pp = asd->vfpp->val != ATOMISP_VFPP_ENABLE;
4108 /* ISP2401 new input system need to use copy pipe */
4109 if (asd->copy_mode) {
4110 pipe_id = IA_CSS_PIPE_ID_COPY;
4111 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL, false);
4112 } else if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER) {
4113 /* video same in continuouscapture and online modes */
4114 configure_output = atomisp_css_video_configure_output;
4115 get_frame_info = atomisp_css_video_get_output_frame_info;
4116 pipe_id = IA_CSS_PIPE_ID_VIDEO;
4117 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4118 configure_output = atomisp_css_video_configure_output;
4119 get_frame_info = atomisp_css_video_get_output_frame_info;
4120 pipe_id = IA_CSS_PIPE_ID_VIDEO;
4121 } else if (asd->run_mode->val == ATOMISP_RUN_MODE_PREVIEW) {
4122 configure_output = atomisp_css_preview_configure_output;
4123 get_frame_info = atomisp_css_preview_get_output_frame_info;
4124 configure_pp_input = atomisp_css_preview_configure_pp_input;
4125 pipe_id = IA_CSS_PIPE_ID_PREVIEW;
4127 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
4128 atomisp_css_capture_set_mode(asd, IA_CSS_CAPTURE_MODE_RAW);
4129 atomisp_css_enable_dz(asd, false);
4131 atomisp_update_capture_mode(asd);
4134 /* in case of ANR, force capture pipe to offline mode */
4135 atomisp_css_capture_enable_online(asd, ATOMISP_INPUT_STREAM_GENERAL,
4136 !asd->params.low_light);
4138 configure_output = atomisp_css_capture_configure_output;
4139 get_frame_info = atomisp_css_capture_get_output_frame_info;
4140 configure_pp_input = atomisp_css_capture_configure_pp_input;
4141 pipe_id = IA_CSS_PIPE_ID_CAPTURE;
4143 if (asd->run_mode->val != ATOMISP_RUN_MODE_STILL_CAPTURE) {
4145 "Need to set the running mode first\n");
4146 asd->run_mode->val = ATOMISP_RUN_MODE_STILL_CAPTURE;
4151 ret = atomisp_css_copy_configure_output(asd, ATOMISP_INPUT_STREAM_GENERAL,
4152 pix->width, pix->height,
4153 format->planar ? pix->bytesperline :
4154 pix->bytesperline * 8 / format->depth,
4157 ret = configure_output(asd, pix->width, pix->height,
4158 format->planar ? pix->bytesperline :
4159 pix->bytesperline * 8 / format->depth,
4162 dev_err(isp->dev, "configure_output %ux%u, format %8.8x\n",
4163 pix->width, pix->height, format->sh_fmt);
4167 ret = configure_pp_input(asd, isp_sink_crop->width, isp_sink_crop->height);
4169 dev_err(isp->dev, "configure_pp_input %ux%u\n",
4170 isp_sink_crop->width,
4171 isp_sink_crop->height);
4175 ret = atomisp_css_copy_get_output_frame_info(asd,
4176 ATOMISP_INPUT_STREAM_GENERAL,
4179 ret = get_frame_info(asd, output_info);
4181 dev_err(isp->dev, "__get_frame_info %ux%u (padded to %u) returned %d\n",
4182 pix->width, pix->height, pix->bytesperline, ret);
4186 atomisp_update_grid_info(asd, pipe_id);
4190 static void atomisp_get_dis_envelop(struct atomisp_sub_device *asd,
4191 unsigned int width, unsigned int height,
4192 unsigned int *dvs_env_w, unsigned int *dvs_env_h)
4194 if (asd->params.video_dis_en &&
4195 asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4196 /* envelope is 20% of the output resolution */
4198 * dvs envelope cannot be round up.
4199 * it would cause ISP timeout and color switch issue
4201 *dvs_env_w = rounddown(width / 5, ATOM_ISP_STEP_WIDTH);
4202 *dvs_env_h = rounddown(height / 5, ATOM_ISP_STEP_HEIGHT);
4205 asd->params.dis_proj_data_valid = false;
4206 asd->params.css_update_params_needed = true;
4209 static void atomisp_check_copy_mode(struct atomisp_sub_device *asd,
4210 const struct v4l2_pix_format *f)
4212 struct v4l2_mbus_framefmt *sink, *src;
4215 /* Only used for the new input system */
4216 asd->copy_mode = false;
4220 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4221 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SINK);
4222 src = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4223 V4L2_SUBDEV_FORMAT_ACTIVE, ATOMISP_SUBDEV_PAD_SOURCE);
4225 if (sink->code == src->code && sink->width == f->width && sink->height == f->height)
4226 asd->copy_mode = true;
4228 asd->copy_mode = false;
4230 dev_dbg(asd->isp->dev, "copy_mode: %d\n", asd->copy_mode);
4233 static int atomisp_set_fmt_to_snr(struct video_device *vdev, const struct v4l2_pix_format *f,
4234 unsigned int dvs_env_w, unsigned int dvs_env_h)
4236 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4237 struct atomisp_sub_device *asd = pipe->asd;
4238 struct atomisp_device *isp = asd->isp;
4239 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
4240 const struct atomisp_format_bridge *format;
4241 struct v4l2_subdev_state *act_sd_state;
4242 struct v4l2_subdev_format vformat = {
4243 .which = V4L2_SUBDEV_FORMAT_TRY,
4245 struct v4l2_mbus_framefmt *ffmt = &vformat.format;
4246 struct v4l2_mbus_framefmt *req_ffmt;
4247 struct atomisp_input_stream_info *stream_info =
4248 (struct atomisp_input_stream_info *)ffmt->reserved;
4251 format = atomisp_get_format_bridge(f->pixelformat);
4255 v4l2_fill_mbus_format(ffmt, f, format->mbus_code);
4256 ffmt->height += asd->sink_pad_padding_h + dvs_env_h;
4257 ffmt->width += asd->sink_pad_padding_w + dvs_env_w;
4259 dev_dbg(isp->dev, "s_mbus_fmt: ask %ux%u (padding %ux%u, dvs %ux%u)\n",
4260 ffmt->width, ffmt->height, asd->sink_pad_padding_w, asd->sink_pad_padding_h,
4261 dvs_env_w, dvs_env_h);
4263 __atomisp_init_stream_info(ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4267 /* Disable dvs if resolution can't be supported by sensor */
4268 if (asd->params.video_dis_en && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO) {
4269 v4l2_subdev_lock_state(input->try_sd_state);
4271 ret = atomisp_set_crop(isp, &vformat.format, input->try_sd_state,
4272 V4L2_SUBDEV_FORMAT_TRY);
4274 vformat.which = V4L2_SUBDEV_FORMAT_TRY;
4275 ret = v4l2_subdev_call(input->camera, pad, set_fmt,
4276 input->try_sd_state, &vformat);
4279 v4l2_subdev_unlock_state(input->try_sd_state);
4284 dev_dbg(isp->dev, "video dis: sensor width: %d, height: %d\n",
4285 ffmt->width, ffmt->height);
4287 if (ffmt->width < req_ffmt->width ||
4288 ffmt->height < req_ffmt->height) {
4289 req_ffmt->height -= dvs_env_h;
4290 req_ffmt->width -= dvs_env_w;
4293 "can not enable video dis due to sensor limitation.");
4294 asd->params.video_dis_en = false;
4298 act_sd_state = v4l2_subdev_lock_and_get_active_state(input->camera);
4300 ret = atomisp_set_crop(isp, &vformat.format, act_sd_state,
4301 V4L2_SUBDEV_FORMAT_ACTIVE);
4303 vformat.which = V4L2_SUBDEV_FORMAT_ACTIVE;
4304 ret = v4l2_subdev_call(input->camera, pad, set_fmt, act_sd_state, &vformat);
4308 v4l2_subdev_unlock_state(act_sd_state);
4313 __atomisp_update_stream_env(asd, ATOMISP_INPUT_STREAM_GENERAL, stream_info);
4315 dev_dbg(isp->dev, "sensor width: %d, height: %d\n",
4316 ffmt->width, ffmt->height);
4318 if (ffmt->width < ATOM_ISP_STEP_WIDTH ||
4319 ffmt->height < ATOM_ISP_STEP_HEIGHT)
4322 if (asd->params.video_dis_en && asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO &&
4323 (ffmt->width < req_ffmt->width || ffmt->height < req_ffmt->height)) {
4325 "can not enable video dis due to sensor limitation.");
4326 asd->params.video_dis_en = false;
4329 atomisp_subdev_set_ffmt(&asd->subdev, NULL,
4330 V4L2_SUBDEV_FORMAT_ACTIVE,
4331 ATOMISP_SUBDEV_PAD_SINK, ffmt);
4333 return css_input_resolution_changed(asd, ffmt);
4336 int atomisp_set_fmt(struct video_device *vdev, struct v4l2_format *f)
4338 struct atomisp_device *isp = video_get_drvdata(vdev);
4339 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
4340 struct atomisp_sub_device *asd = pipe->asd;
4341 const struct atomisp_format_bridge *format_bridge;
4342 const struct atomisp_format_bridge *snr_format_bridge;
4343 struct ia_css_frame_info output_info;
4344 unsigned int dvs_env_w = 0, dvs_env_h = 0;
4345 struct v4l2_mbus_framefmt isp_source_fmt = {0};
4346 struct v4l2_rect isp_sink_crop;
4349 ret = atomisp_pipe_check(pipe, true);
4354 "setting resolution %ux%u bytesperline %u\n",
4355 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.bytesperline);
4357 /* Ensure that the resolution is equal or below the maximum supported */
4358 ret = atomisp_try_fmt(isp, &f->fmt.pix, &format_bridge, &snr_format_bridge);
4362 pipe->sh_fmt = format_bridge->sh_fmt;
4363 pipe->pix.pixelformat = format_bridge->pixelformat;
4365 atomisp_subdev_get_ffmt(&asd->subdev, NULL,
4366 V4L2_SUBDEV_FORMAT_ACTIVE,
4367 ATOMISP_SUBDEV_PAD_SINK)->code =
4368 snr_format_bridge->mbus_code;
4370 isp_source_fmt.code = format_bridge->mbus_code;
4371 atomisp_subdev_set_ffmt(&asd->subdev, NULL,
4372 V4L2_SUBDEV_FORMAT_ACTIVE,
4373 ATOMISP_SUBDEV_PAD_SOURCE, &isp_source_fmt);
4375 if (atomisp_subdev_format_conversion(asd)) {
4376 atomisp_get_padding(isp, f->fmt.pix.width, f->fmt.pix.height,
4377 &asd->sink_pad_padding_w, &asd->sink_pad_padding_h);
4379 asd->sink_pad_padding_w = 0;
4380 asd->sink_pad_padding_h = 0;
4383 atomisp_get_dis_envelop(asd, f->fmt.pix.width, f->fmt.pix.height,
4384 &dvs_env_w, &dvs_env_h);
4386 ret = atomisp_set_fmt_to_snr(vdev, &f->fmt.pix, dvs_env_w, dvs_env_h);
4389 "Set format to sensor failed with %d\n", ret);
4393 atomisp_csi_lane_config(isp);
4395 atomisp_check_copy_mode(asd, &f->fmt.pix);
4397 isp_sink_crop = *atomisp_subdev_get_rect(&asd->subdev, NULL,
4398 V4L2_SUBDEV_FORMAT_ACTIVE,
4399 ATOMISP_SUBDEV_PAD_SINK,
4402 /* Try to enable YUV downscaling if ISP input is 10 % (either
4403 * width or height) bigger than the desired result. */
4405 isp_sink_crop.width * 9 / 10 < f->fmt.pix.width ||
4406 isp_sink_crop.height * 9 / 10 < f->fmt.pix.height ||
4407 (atomisp_subdev_format_conversion(asd) &&
4408 (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
4409 asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER))) {
4410 isp_sink_crop.width = f->fmt.pix.width;
4411 isp_sink_crop.height = f->fmt.pix.height;
4413 atomisp_subdev_set_selection(&asd->subdev, NULL,
4414 V4L2_SUBDEV_FORMAT_ACTIVE,
4415 ATOMISP_SUBDEV_PAD_SOURCE, V4L2_SEL_TGT_COMPOSE,
4418 struct v4l2_rect main_compose = {0};
4420 main_compose.width = isp_sink_crop.width;
4421 main_compose.height =
4422 DIV_ROUND_UP(main_compose.width * f->fmt.pix.height,
4424 if (main_compose.height > isp_sink_crop.height) {
4425 main_compose.height = isp_sink_crop.height;
4426 main_compose.width =
4427 DIV_ROUND_UP(main_compose.height *
4432 atomisp_subdev_set_selection(&asd->subdev, NULL,
4433 V4L2_SUBDEV_FORMAT_ACTIVE,
4434 ATOMISP_SUBDEV_PAD_SOURCE,
4435 V4L2_SEL_TGT_COMPOSE, 0,
4439 ret = atomisp_set_fmt_to_isp(vdev, &output_info, &f->fmt.pix);
4441 dev_warn(isp->dev, "Can't set format on ISP. Error %d\n", ret);
4445 atomisp_fill_pix_format(&pipe->pix, f->fmt.pix.width, f->fmt.pix.height, format_bridge);
4447 f->fmt.pix = pipe->pix;
4448 f->fmt.pix.priv = PAGE_ALIGN(pipe->pix.width *
4449 pipe->pix.height * 2);
4451 dev_dbg(isp->dev, "%s: %dx%d, image size: %d, %d bytes per line\n",
4453 f->fmt.pix.width, f->fmt.pix.height,
4454 f->fmt.pix.sizeimage, f->fmt.pix.bytesperline);
4459 int atomisp_set_shading_table(struct atomisp_sub_device *asd,
4460 struct atomisp_shading_table *user_shading_table)
4462 struct ia_css_shading_table *shading_table;
4463 struct ia_css_shading_table *free_table;
4464 unsigned int len_table;
4468 if (!user_shading_table)
4471 if (!user_shading_table->enable) {
4472 asd->params.config.shading_table = NULL;
4473 asd->params.sc_en = false;
4477 /* If enabling, all tables must be set */
4478 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
4479 if (!user_shading_table->data[i])
4483 /* Shading table size per color */
4484 if (user_shading_table->width > SH_CSS_MAX_SCTBL_WIDTH_PER_COLOR ||
4485 user_shading_table->height > SH_CSS_MAX_SCTBL_HEIGHT_PER_COLOR)
4488 shading_table = atomisp_css_shading_table_alloc(
4489 user_shading_table->width, user_shading_table->height);
4493 len_table = user_shading_table->width * user_shading_table->height *
4494 ATOMISP_SC_TYPE_SIZE;
4495 for (i = 0; i < ATOMISP_NUM_SC_COLORS; i++) {
4496 ret = copy_from_user(shading_table->data[i],
4497 (void __user *)user_shading_table->data[i],
4500 free_table = shading_table;
4505 shading_table->sensor_width = user_shading_table->sensor_width;
4506 shading_table->sensor_height = user_shading_table->sensor_height;
4507 shading_table->fraction_bits = user_shading_table->fraction_bits;
4509 free_table = asd->params.css_param.shading_table;
4510 asd->params.css_param.shading_table = shading_table;
4511 asd->params.config.shading_table = shading_table;
4512 asd->params.sc_en = true;
4516 atomisp_css_shading_table_free(free_table);
4521 int atomisp_flash_enable(struct atomisp_sub_device *asd, int num_frames)
4523 struct atomisp_device *isp = asd->isp;
4525 if (num_frames < 0) {
4526 dev_dbg(isp->dev, "%s ERROR: num_frames: %d\n", __func__,
4530 /* a requested flash is still in progress. */
4531 if (num_frames && asd->params.flash_state != ATOMISP_FLASH_IDLE) {
4532 dev_dbg(isp->dev, "%s flash busy: %d frames left: %d\n",
4533 __func__, asd->params.flash_state,
4534 asd->params.num_flash_frames);
4538 asd->params.num_flash_frames = num_frames;
4539 asd->params.flash_state = ATOMISP_FLASH_REQUESTED;
4543 static int __checking_exp_id(struct atomisp_sub_device *asd, int exp_id)
4545 struct atomisp_device *isp = asd->isp;
4547 if (!asd->enable_raw_buffer_lock->val) {
4548 dev_warn(isp->dev, "%s Raw Buffer Lock is disable.\n", __func__);
4551 if (!asd->streaming) {
4552 dev_err(isp->dev, "%s streaming %d invalid exp_id %d.\n",
4553 __func__, exp_id, asd->streaming);
4556 if ((exp_id > ATOMISP_MAX_EXP_ID) || (exp_id <= 0)) {
4557 dev_err(isp->dev, "%s exp_id %d invalid.\n", __func__, exp_id);
4563 void atomisp_init_raw_buffer_bitmap(struct atomisp_sub_device *asd)
4565 unsigned long flags;
4567 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
4568 memset(asd->raw_buffer_bitmap, 0, sizeof(asd->raw_buffer_bitmap));
4569 asd->raw_buffer_locked_count = 0;
4570 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
4573 static int __is_raw_buffer_locked(struct atomisp_sub_device *asd, int exp_id)
4576 unsigned long flags;
4579 if (__checking_exp_id(asd, exp_id))
4582 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
4584 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
4585 ret = ((*bitmap) & (1 << bit));
4586 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
4590 static int __clear_raw_buffer_bitmap(struct atomisp_sub_device *asd, int exp_id)
4593 unsigned long flags;
4595 if (__is_raw_buffer_locked(asd, exp_id))
4598 bitmap = asd->raw_buffer_bitmap + exp_id / 32;
4600 spin_lock_irqsave(&asd->raw_buffer_bitmap_lock, flags);
4601 (*bitmap) &= ~(1 << bit);
4602 asd->raw_buffer_locked_count--;
4603 spin_unlock_irqrestore(&asd->raw_buffer_bitmap_lock, flags);
4605 dev_dbg(asd->isp->dev, "%s: exp_id %d, raw_buffer_locked_count %d\n",
4606 __func__, exp_id, asd->raw_buffer_locked_count);
4610 int atomisp_exp_id_capture(struct atomisp_sub_device *asd, int *exp_id)
4612 struct atomisp_device *isp = asd->isp;
4613 int value = *exp_id;
4616 lockdep_assert_held(&isp->mutex);
4618 ret = __is_raw_buffer_locked(asd, value);
4620 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
4624 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
4625 ret = atomisp_css_exp_id_capture(asd, value);
4627 dev_err(isp->dev, "%s exp_id %d failed.\n", __func__, value);
4633 int atomisp_exp_id_unlock(struct atomisp_sub_device *asd, int *exp_id)
4635 struct atomisp_device *isp = asd->isp;
4636 int value = *exp_id;
4639 lockdep_assert_held(&isp->mutex);
4641 ret = __clear_raw_buffer_bitmap(asd, value);
4643 dev_err(isp->dev, "%s exp_id %d invalid %d.\n", __func__, value, ret);
4647 dev_dbg(isp->dev, "%s exp_id %d\n", __func__, value);
4648 ret = atomisp_css_exp_id_unlock(asd, value);
4650 dev_err(isp->dev, "%s exp_id %d failed, err %d.\n",
4651 __func__, value, ret);
4656 int atomisp_enable_dz_capt_pipe(struct atomisp_sub_device *asd,
4657 unsigned int *enable)
4664 value = *enable > 0;
4666 atomisp_en_dz_capt_pipe(asd, value);
4671 int atomisp_inject_a_fake_event(struct atomisp_sub_device *asd, int *event)
4673 if (!event || !asd->streaming)
4676 lockdep_assert_held(&asd->isp->mutex);
4678 dev_dbg(asd->isp->dev, "%s: trying to inject a fake event 0x%x\n",
4682 case V4L2_EVENT_FRAME_SYNC:
4683 atomisp_sof_event(asd);
4685 case V4L2_EVENT_FRAME_END:
4686 atomisp_eof_event(asd, 0);
4688 case V4L2_EVENT_ATOMISP_3A_STATS_READY:
4689 atomisp_3a_stats_ready_event(asd, 0);
4691 case V4L2_EVENT_ATOMISP_METADATA_READY:
4692 atomisp_metadata_ready_event(asd, 0);